blob: ccfd2e30c3fed14c9b2831b617330e565d5efdbb [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* drivers/usb/gadget/f_diag.c
2 * Diag Function Device - Route ARM9 and ARM11 DIAG messages
3 * between HOST and DEVICE.
4 * Copyright (C) 2007 Google, Inc.
5 * Copyright (c) 2008-2011, Code Aurora Forum. All rights reserved.
6 * Author: Brian Swetland <swetland@google.com>
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
17#include <linux/init.h>
18#include <linux/module.h>
19#include <linux/kernel.h>
20#include <linux/platform_device.h>
21
22#include <mach/usbdiag.h>
23#include <mach/rpc_hsusb.h>
24
25#include <linux/usb/composite.h>
26#include <linux/usb/gadget.h>
27#include <linux/workqueue.h>
28#include <linux/debugfs.h>
29
30static DEFINE_SPINLOCK(ch_lock);
31static LIST_HEAD(usb_diag_ch_list);
32
33static struct usb_interface_descriptor intf_desc = {
34 .bLength = sizeof intf_desc,
35 .bDescriptorType = USB_DT_INTERFACE,
36 .bNumEndpoints = 2,
37 .bInterfaceClass = 0xFF,
38 .bInterfaceSubClass = 0xFF,
39 .bInterfaceProtocol = 0xFF,
40};
41
42static struct usb_endpoint_descriptor hs_bulk_in_desc = {
43 .bLength = USB_DT_ENDPOINT_SIZE,
44 .bDescriptorType = USB_DT_ENDPOINT,
45 .bEndpointAddress = USB_DIR_IN,
46 .bmAttributes = USB_ENDPOINT_XFER_BULK,
47 .wMaxPacketSize = __constant_cpu_to_le16(512),
48 .bInterval = 0,
49};
50static struct usb_endpoint_descriptor fs_bulk_in_desc = {
51 .bLength = USB_DT_ENDPOINT_SIZE,
52 .bDescriptorType = USB_DT_ENDPOINT,
53 .bEndpointAddress = USB_DIR_IN,
54 .bmAttributes = USB_ENDPOINT_XFER_BULK,
55 .wMaxPacketSize = __constant_cpu_to_le16(64),
56 .bInterval = 0,
57};
58
59static struct usb_endpoint_descriptor hs_bulk_out_desc = {
60 .bLength = USB_DT_ENDPOINT_SIZE,
61 .bDescriptorType = USB_DT_ENDPOINT,
62 .bEndpointAddress = USB_DIR_OUT,
63 .bmAttributes = USB_ENDPOINT_XFER_BULK,
64 .wMaxPacketSize = __constant_cpu_to_le16(512),
65 .bInterval = 0,
66};
67
68static struct usb_endpoint_descriptor fs_bulk_out_desc = {
69 .bLength = USB_DT_ENDPOINT_SIZE,
70 .bDescriptorType = USB_DT_ENDPOINT,
71 .bEndpointAddress = USB_DIR_OUT,
72 .bmAttributes = USB_ENDPOINT_XFER_BULK,
73 .wMaxPacketSize = __constant_cpu_to_le16(64),
74 .bInterval = 0,
75};
76
77static struct usb_descriptor_header *fs_diag_desc[] = {
78 (struct usb_descriptor_header *) &intf_desc,
79 (struct usb_descriptor_header *) &fs_bulk_in_desc,
80 (struct usb_descriptor_header *) &fs_bulk_out_desc,
81 NULL,
82 };
83static struct usb_descriptor_header *hs_diag_desc[] = {
84 (struct usb_descriptor_header *) &intf_desc,
85 (struct usb_descriptor_header *) &hs_bulk_in_desc,
86 (struct usb_descriptor_header *) &hs_bulk_out_desc,
87 NULL,
88};
89
90/**
91 * struct diag_context - USB diag function driver private structure
92 * @function: function structure for USB interface
93 * @out: USB OUT endpoint struct
94 * @in: USB IN endpoint struct
95 * @in_desc: USB IN endpoint descriptor struct
96 * @out_desc: USB OUT endpoint descriptor struct
97 * @read_pool: List of requests used for Rx (OUT ep)
98 * @write_pool: List of requests used for Tx (IN ep)
99 * @config_work: Work item schedule after interface is configured to notify
100 * CONNECT event to diag char driver and updating product id
101 * and serial number to MODEM/IMEM.
102 * @lock: Spinlock to proctect read_pool, write_pool lists
103 * @cdev: USB composite device struct
104 * @ch: USB diag channel
105 *
106 */
107struct diag_context {
108 struct usb_function function;
109 struct usb_ep *out;
110 struct usb_ep *in;
111 struct usb_endpoint_descriptor *in_desc;
112 struct usb_endpoint_descriptor *out_desc;
113 struct list_head read_pool;
114 struct list_head write_pool;
115 struct work_struct config_work;
116 spinlock_t lock;
117 unsigned configured;
118 struct usb_composite_dev *cdev;
119 int (*update_pid_and_serial_num)(uint32_t, const char *);
120 struct usb_diag_ch ch;
121
122 /* pkt counters */
123 unsigned long dpkts_tolaptop;
124 unsigned long dpkts_tomodem;
125 unsigned dpkts_tolaptop_pending;
126};
127
128static inline struct diag_context *func_to_diag(struct usb_function *f)
129{
130 return container_of(f, struct diag_context, function);
131}
132
133static void usb_config_work_func(struct work_struct *work)
134{
135 struct diag_context *ctxt = container_of(work,
136 struct diag_context, config_work);
137 struct usb_composite_dev *cdev = ctxt->cdev;
138 struct usb_gadget_strings *table;
139 struct usb_string *s;
140
141 if (ctxt->ch.notify)
142 ctxt->ch.notify(ctxt->ch.priv, USB_DIAG_CONNECT, NULL);
143
144 if (!ctxt->update_pid_and_serial_num)
145 return;
146
147 /* pass on product id and serial number to dload */
148 if (!cdev->desc.iSerialNumber) {
149 ctxt->update_pid_and_serial_num(
150 cdev->desc.idProduct, 0);
151 return;
152 }
153
154 /*
155 * Serial number is filled by the composite driver. So
156 * it is fair enough to assume that it will always be
157 * found at first table of strings.
158 */
159 table = *(cdev->driver->strings);
160 for (s = table->strings; s && s->s; s++)
161 if (s->id == cdev->desc.iSerialNumber) {
162 ctxt->update_pid_and_serial_num(
163 cdev->desc.idProduct, s->s);
164 break;
165 }
166}
167
168static void diag_write_complete(struct usb_ep *ep,
169 struct usb_request *req)
170{
171 struct diag_context *ctxt = ep->driver_data;
172 struct diag_request *d_req = req->context;
173 unsigned long flags;
174
175 ctxt->dpkts_tolaptop_pending--;
176
177 if (!req->status) {
178 if ((req->length >= ep->maxpacket) &&
179 ((req->length % ep->maxpacket) == 0)) {
180 ctxt->dpkts_tolaptop_pending++;
181 req->length = 0;
182 d_req->actual = req->actual;
183 d_req->status = req->status;
184 /* Queue zero length packet */
185 usb_ep_queue(ctxt->in, req, GFP_ATOMIC);
186 return;
187 }
188 }
189
190 spin_lock_irqsave(&ctxt->lock, flags);
191 list_add_tail(&req->list, &ctxt->write_pool);
192 if (req->length != 0) {
193 d_req->actual = req->actual;
194 d_req->status = req->status;
195 }
196 spin_unlock_irqrestore(&ctxt->lock, flags);
197
198 if (ctxt->ch.notify)
199 ctxt->ch.notify(ctxt->ch.priv, USB_DIAG_WRITE_DONE, d_req);
200}
201
202static void diag_read_complete(struct usb_ep *ep,
203 struct usb_request *req)
204{
205 struct diag_context *ctxt = ep->driver_data;
206 struct diag_request *d_req = req->context;
207 unsigned long flags;
208
209 d_req->actual = req->actual;
210 d_req->status = req->status;
211
212 spin_lock_irqsave(&ctxt->lock, flags);
213 list_add_tail(&req->list, &ctxt->read_pool);
214 spin_unlock_irqrestore(&ctxt->lock, flags);
215
216 ctxt->dpkts_tomodem++;
217
218 if (ctxt->ch.notify)
219 ctxt->ch.notify(ctxt->ch.priv, USB_DIAG_READ_DONE, d_req);
220}
221
222/**
223 * usb_diag_open() - Open a diag channel over USB
224 * @name: Name of the channel
225 * @priv: Private structure pointer which will be passed in notify()
226 * @notify: Callback function to receive notifications
227 *
228 * This function iterates overs the available channels and returns
229 * the channel handler if the name matches. The notify callback is called
230 * for CONNECT, DISCONNECT, READ_DONE and WRITE_DONE events.
231 *
232 */
233struct usb_diag_ch *usb_diag_open(const char *name, void *priv,
234 void (*notify)(void *, unsigned, struct diag_request *))
235{
236 struct usb_diag_ch *ch;
237 struct diag_context *ctxt;
238 unsigned long flags;
239 int found = 0;
240
241 spin_lock_irqsave(&ch_lock, flags);
242 /* Check if we already have a channel with this name */
243 list_for_each_entry(ch, &usb_diag_ch_list, list) {
244 if (!strcmp(name, ch->name)) {
245 found = 1;
246 break;
247 }
248 }
249 spin_unlock_irqrestore(&ch_lock, flags);
250
251 if (!found) {
252 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
253 if (!ctxt)
254 return ERR_PTR(-ENOMEM);
255
256 ch = &ctxt->ch;
257 }
258
259 ch->name = name;
260 ch->priv = priv;
261 ch->notify = notify;
262
263 spin_lock_irqsave(&ch_lock, flags);
264 list_add_tail(&ch->list, &usb_diag_ch_list);
265 spin_unlock_irqrestore(&ch_lock, flags);
266
267 return ch;
268}
269EXPORT_SYMBOL(usb_diag_open);
270
271/**
272 * usb_diag_close() - Close a diag channel over USB
273 * @ch: Channel handler
274 *
275 * This function closes the diag channel.
276 *
277 */
278void usb_diag_close(struct usb_diag_ch *ch)
279{
280 struct diag_context *dev = container_of(ch, struct diag_context, ch);
281 unsigned long flags;
282
283 spin_lock_irqsave(&ch_lock, flags);
284 ch->priv = NULL;
285 ch->notify = NULL;
286 /* Free-up the resources if channel is no more active */
287 if (!ch->priv_usb) {
288 list_del(&ch->list);
289 kfree(dev);
290 }
291
292 spin_unlock_irqrestore(&ch_lock, flags);
293}
294EXPORT_SYMBOL(usb_diag_close);
295
296/**
297 * usb_diag_free_req() - Free USB requests
298 * @ch: Channel handler
299 *
300 * This function free read and write USB requests for the interface
301 * associated with this channel.
302 *
303 */
304void usb_diag_free_req(struct usb_diag_ch *ch)
305{
306 struct diag_context *ctxt = ch->priv_usb;
307 struct usb_request *req;
308 struct list_head *act, *tmp;
309
310 if (!ctxt)
311 return;
312
313 list_for_each_safe(act, tmp, &ctxt->write_pool) {
314 req = list_entry(act, struct usb_request, list);
315 list_del(&req->list);
316 usb_ep_free_request(ctxt->in, req);
317 }
318
319 list_for_each_safe(act, tmp, &ctxt->read_pool) {
320 req = list_entry(act, struct usb_request, list);
321 list_del(&req->list);
322 usb_ep_free_request(ctxt->out, req);
323 }
324}
325EXPORT_SYMBOL(usb_diag_free_req);
326
327/**
328 * usb_diag_alloc_req() - Allocate USB requests
329 * @ch: Channel handler
330 * @n_write: Number of requests for Tx
331 * @n_read: Number of requests for Rx
332 *
333 * This function allocate read and write USB requests for the interface
334 * associated with this channel. The actual buffer is not allocated.
335 * The buffer is passed by diag char driver.
336 *
337 */
338int usb_diag_alloc_req(struct usb_diag_ch *ch, int n_write, int n_read)
339{
340 struct diag_context *ctxt = ch->priv_usb;
341 struct usb_request *req;
342 int i;
343
344 if (!ctxt)
345 return -ENODEV;
346
347 for (i = 0; i < n_write; i++) {
348 req = usb_ep_alloc_request(ctxt->in, GFP_ATOMIC);
349 if (!req)
350 goto fail;
351 req->complete = diag_write_complete;
352 list_add_tail(&req->list, &ctxt->write_pool);
353 }
354
355 for (i = 0; i < n_read; i++) {
356 req = usb_ep_alloc_request(ctxt->out, GFP_ATOMIC);
357 if (!req)
358 goto fail;
359 req->complete = diag_read_complete;
360 list_add_tail(&req->list, &ctxt->read_pool);
361 }
362
363 return 0;
364
365fail:
366 usb_diag_free_req(ch);
367 return -ENOMEM;
368
369}
370EXPORT_SYMBOL(usb_diag_alloc_req);
371
372/**
373 * usb_diag_read() - Read data from USB diag channel
374 * @ch: Channel handler
375 * @d_req: Diag request struct
376 *
377 * Enqueue a request on OUT endpoint of the interface corresponding to this
378 * channel. This function returns proper error code when interface is not
379 * in configured state, no Rx requests available and ep queue is failed.
380 *
381 * This function operates asynchronously. READ_DONE event is notified after
382 * completion of OUT request.
383 *
384 */
385int usb_diag_read(struct usb_diag_ch *ch, struct diag_request *d_req)
386{
387 struct diag_context *ctxt = ch->priv_usb;
388 unsigned long flags;
389 struct usb_request *req;
390
Manu Gautamb33f33a2011-09-09 14:35:57 +0530391 if (!ctxt)
392 return -ENODEV;
393
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700394 spin_lock_irqsave(&ctxt->lock, flags);
395
396 if (!ctxt->configured) {
397 spin_unlock_irqrestore(&ctxt->lock, flags);
398 return -EIO;
399 }
400
401 if (list_empty(&ctxt->read_pool)) {
402 spin_unlock_irqrestore(&ctxt->lock, flags);
403 ERROR(ctxt->cdev, "%s: no requests available\n", __func__);
404 return -EAGAIN;
405 }
406
407 req = list_first_entry(&ctxt->read_pool, struct usb_request, list);
408 list_del(&req->list);
409 spin_unlock_irqrestore(&ctxt->lock, flags);
410
411 req->buf = d_req->buf;
412 req->length = d_req->length;
413 req->context = d_req;
414 if (usb_ep_queue(ctxt->out, req, GFP_ATOMIC)) {
415 /* If error add the link to linked list again*/
416 spin_lock_irqsave(&ctxt->lock, flags);
417 list_add_tail(&req->list, &ctxt->read_pool);
418 spin_unlock_irqrestore(&ctxt->lock, flags);
419 ERROR(ctxt->cdev, "%s: cannot queue"
420 " read request\n", __func__);
421 return -EIO;
422 }
423
424 return 0;
425}
426EXPORT_SYMBOL(usb_diag_read);
427
428/**
429 * usb_diag_write() - Write data from USB diag channel
430 * @ch: Channel handler
431 * @d_req: Diag request struct
432 *
433 * Enqueue a request on IN endpoint of the interface corresponding to this
434 * channel. This function returns proper error code when interface is not
435 * in configured state, no Tx requests available and ep queue is failed.
436 *
437 * This function operates asynchronously. WRITE_DONE event is notified after
438 * completion of IN request.
439 *
440 */
441int usb_diag_write(struct usb_diag_ch *ch, struct diag_request *d_req)
442{
443 struct diag_context *ctxt = ch->priv_usb;
444 unsigned long flags;
445 struct usb_request *req = NULL;
446
Manu Gautamb33f33a2011-09-09 14:35:57 +0530447 if (!ctxt)
448 return -ENODEV;
449
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700450 spin_lock_irqsave(&ctxt->lock, flags);
451
452 if (!ctxt->configured) {
453 spin_unlock_irqrestore(&ctxt->lock, flags);
454 return -EIO;
455 }
456
457 if (list_empty(&ctxt->write_pool)) {
458 spin_unlock_irqrestore(&ctxt->lock, flags);
459 ERROR(ctxt->cdev, "%s: no requests available\n", __func__);
460 return -EAGAIN;
461 }
462
463 req = list_first_entry(&ctxt->write_pool, struct usb_request, list);
464 list_del(&req->list);
465 spin_unlock_irqrestore(&ctxt->lock, flags);
466
467 req->buf = d_req->buf;
468 req->length = d_req->length;
469 req->context = d_req;
470 if (usb_ep_queue(ctxt->in, req, GFP_ATOMIC)) {
471 /* If error add the link to linked list again*/
472 spin_lock_irqsave(&ctxt->lock, flags);
473 list_add_tail(&req->list, &ctxt->write_pool);
474 spin_unlock_irqrestore(&ctxt->lock, flags);
475 ERROR(ctxt->cdev, "%s: cannot queue"
476 " read request\n", __func__);
477 return -EIO;
478 }
479
480 ctxt->dpkts_tolaptop++;
481 ctxt->dpkts_tolaptop_pending++;
482
483 return 0;
484}
485EXPORT_SYMBOL(usb_diag_write);
486
487static void diag_function_disable(struct usb_function *f)
488{
489 struct diag_context *dev = func_to_diag(f);
490 unsigned long flags;
491
492 DBG(dev->cdev, "diag_function_disable\n");
493
494 spin_lock_irqsave(&dev->lock, flags);
495 dev->configured = 0;
496 spin_unlock_irqrestore(&dev->lock, flags);
497
498 if (dev->ch.notify)
499 dev->ch.notify(dev->ch.priv, USB_DIAG_DISCONNECT, NULL);
500
501 usb_ep_disable(dev->in);
502 dev->in->driver_data = NULL;
503
504 usb_ep_disable(dev->out);
505 dev->out->driver_data = NULL;
506
507}
508
509static int diag_function_set_alt(struct usb_function *f,
510 unsigned intf, unsigned alt)
511{
512 struct diag_context *dev = func_to_diag(f);
513 struct usb_composite_dev *cdev = f->config->cdev;
514 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700515 int rc = 0;
516
517 dev->in_desc = ep_choose(cdev->gadget,
518 &hs_bulk_in_desc, &fs_bulk_in_desc);
519 dev->out_desc = ep_choose(cdev->gadget,
520 &hs_bulk_out_desc, &fs_bulk_out_desc);
521 dev->in->driver_data = dev;
522 rc = usb_ep_enable(dev->in, dev->in_desc);
523 if (rc) {
524 ERROR(dev->cdev, "can't enable %s, result %d\n",
525 dev->in->name, rc);
526 return rc;
527 }
528 dev->out->driver_data = dev;
529 rc = usb_ep_enable(dev->out, dev->out_desc);
530 if (rc) {
531 ERROR(dev->cdev, "can't enable %s, result %d\n",
532 dev->out->name, rc);
533 usb_ep_disable(dev->in);
534 return rc;
535 }
536 schedule_work(&dev->config_work);
537
Manu Gautam0c611072011-11-11 17:40:05 +0530538 dev->dpkts_tolaptop = 0;
539 dev->dpkts_tomodem = 0;
540 dev->dpkts_tolaptop_pending = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700541
542 spin_lock_irqsave(&dev->lock, flags);
543 dev->configured = 1;
544 spin_unlock_irqrestore(&dev->lock, flags);
545
546 return rc;
547}
548
549static void diag_function_unbind(struct usb_configuration *c,
550 struct usb_function *f)
551{
552 struct diag_context *ctxt = func_to_diag(f);
553
554 if (gadget_is_dualspeed(c->cdev->gadget))
555 usb_free_descriptors(f->hs_descriptors);
556
557 usb_free_descriptors(f->descriptors);
558 ctxt->ch.priv_usb = NULL;
559}
560
561static int diag_function_bind(struct usb_configuration *c,
562 struct usb_function *f)
563{
564 struct usb_composite_dev *cdev = c->cdev;
565 struct diag_context *ctxt = func_to_diag(f);
566 struct usb_ep *ep;
567 int status = -ENODEV;
568
569 intf_desc.bInterfaceNumber = usb_interface_id(c, f);
570
571 ep = usb_ep_autoconfig(cdev->gadget, &fs_bulk_in_desc);
Vamsi Krishnae606a7b2011-08-18 12:21:44 -0700572 if (!ep)
573 goto fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700574 ctxt->in = ep;
575 ep->driver_data = ctxt;
576
577 ep = usb_ep_autoconfig(cdev->gadget, &fs_bulk_out_desc);
Vamsi Krishnae606a7b2011-08-18 12:21:44 -0700578 if (!ep)
579 goto fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700580 ctxt->out = ep;
581 ep->driver_data = ctxt;
582
583 /* copy descriptors, and track endpoint copies */
584 f->descriptors = usb_copy_descriptors(fs_diag_desc);
585 if (!f->descriptors)
586 goto fail;
587
588 if (gadget_is_dualspeed(c->cdev->gadget)) {
589 hs_bulk_in_desc.bEndpointAddress =
590 fs_bulk_in_desc.bEndpointAddress;
591 hs_bulk_out_desc.bEndpointAddress =
592 fs_bulk_out_desc.bEndpointAddress;
593
594 /* copy descriptors, and track endpoint copies */
595 f->hs_descriptors = usb_copy_descriptors(hs_diag_desc);
596 }
597 return 0;
598fail:
599 if (ctxt->out)
600 ctxt->out->driver_data = NULL;
601 if (ctxt->in)
602 ctxt->in->driver_data = NULL;
603 return status;
604
605}
606
607int diag_function_add(struct usb_configuration *c, const char *name,
608 int (*update_pid)(uint32_t, const char *))
609{
610 struct diag_context *dev;
611 struct usb_diag_ch *_ch;
612 int found = 0, ret;
613
614 DBG(c->cdev, "diag_function_add\n");
615
616 list_for_each_entry(_ch, &usb_diag_ch_list, list) {
617 if (!strcmp(name, _ch->name)) {
618 found = 1;
619 break;
620 }
621 }
622 if (!found) {
623 ERROR(c->cdev, "unable to get diag usb channel\n");
624 return -ENODEV;
625 }
626
627 dev = container_of(_ch, struct diag_context, ch);
628 /* claim the channel for this USB interface */
629 _ch->priv_usb = dev;
630
631 dev->update_pid_and_serial_num = update_pid;
632 dev->cdev = c->cdev;
633 dev->function.name = _ch->name;
634 dev->function.descriptors = fs_diag_desc;
635 dev->function.hs_descriptors = hs_diag_desc;
636 dev->function.bind = diag_function_bind;
637 dev->function.unbind = diag_function_unbind;
638 dev->function.set_alt = diag_function_set_alt;
639 dev->function.disable = diag_function_disable;
640 spin_lock_init(&dev->lock);
641 INIT_LIST_HEAD(&dev->read_pool);
642 INIT_LIST_HEAD(&dev->write_pool);
643 INIT_WORK(&dev->config_work, usb_config_work_func);
644
645 ret = usb_add_function(c, &dev->function);
646 if (ret) {
647 INFO(c->cdev, "usb_add_function failed\n");
648 _ch->priv_usb = NULL;
649 }
650
651 return ret;
652}
653
654
655#if defined(CONFIG_DEBUG_FS)
656static char debug_buffer[PAGE_SIZE];
657
658static ssize_t debug_read_stats(struct file *file, char __user *ubuf,
659 size_t count, loff_t *ppos)
660{
661 char *buf = debug_buffer;
662 int temp = 0;
663 struct usb_diag_ch *ch;
664
665 list_for_each_entry(ch, &usb_diag_ch_list, list) {
666 struct diag_context *ctxt;
667
668 ctxt = ch->priv_usb;
669
670 temp += scnprintf(buf + temp, PAGE_SIZE - temp,
671 "---Name: %s---\n"
672 "dpkts_tolaptop: %lu\n"
673 "dpkts_tomodem: %lu\n"
674 "pkts_tolaptop_pending: %u\n",
675 ch->name, ctxt->dpkts_tolaptop,
676 ctxt->dpkts_tomodem,
677 ctxt->dpkts_tolaptop_pending);
678 }
679
680 return simple_read_from_buffer(ubuf, count, ppos, buf, temp);
681}
682
683static ssize_t debug_reset_stats(struct file *file, const char __user *buf,
684 size_t count, loff_t *ppos)
685{
686 struct usb_diag_ch *ch;
687
688 list_for_each_entry(ch, &usb_diag_ch_list, list) {
689 struct diag_context *ctxt;
690
691 ctxt = ch->priv_usb;
692
693 ctxt->dpkts_tolaptop = 0;
694 ctxt->dpkts_tomodem = 0;
695 ctxt->dpkts_tolaptop_pending = 0;
696 }
697
698 return count;
699}
700
701static int debug_open(struct inode *inode, struct file *file)
702{
703 return 0;
704}
705
706static const struct file_operations debug_fdiag_ops = {
707 .open = debug_open,
708 .read = debug_read_stats,
709 .write = debug_reset_stats,
710};
711
Manu Gautamd9f56a12011-09-26 14:04:49 +0530712struct dentry *dent_diag;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700713static void fdiag_debugfs_init(void)
714{
Manu Gautamd9f56a12011-09-26 14:04:49 +0530715 dent_diag = debugfs_create_dir("usb_diag", 0);
716 if (IS_ERR(dent_diag))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700717 return;
718
Manu Gautamd9f56a12011-09-26 14:04:49 +0530719 debugfs_create_file("status", 0444, dent_diag, 0, &debug_fdiag_ops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700720}
721#else
722static void fdiag_debugfs_init(void)
723{
724 return;
725}
726#endif
727
728static void diag_cleanup(void)
729{
730 struct diag_context *dev;
731 struct list_head *act, *tmp;
732 struct usb_diag_ch *_ch;
733 unsigned long flags;
734
Manu Gautamd9f56a12011-09-26 14:04:49 +0530735 debugfs_remove_recursive(dent_diag);
736
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700737 list_for_each_safe(act, tmp, &usb_diag_ch_list) {
738 _ch = list_entry(act, struct usb_diag_ch, list);
739 dev = container_of(_ch, struct diag_context, ch);
740
741 spin_lock_irqsave(&ch_lock, flags);
742 /* Free if diagchar is not using the channel anymore */
743 if (!_ch->priv) {
744 list_del(&_ch->list);
745 kfree(dev);
746 }
747 spin_unlock_irqrestore(&ch_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700748 }
749}
750
751static int diag_setup(void)
752{
753 fdiag_debugfs_init();
754
755 return 0;
756}