blob: e276be6565743057f2751fb4ed5893cdbdc5a36e [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;
515 struct usb_diag_ch *ch;
516 int rc = 0;
517
518 dev->in_desc = ep_choose(cdev->gadget,
519 &hs_bulk_in_desc, &fs_bulk_in_desc);
520 dev->out_desc = ep_choose(cdev->gadget,
521 &hs_bulk_out_desc, &fs_bulk_out_desc);
522 dev->in->driver_data = dev;
523 rc = usb_ep_enable(dev->in, dev->in_desc);
524 if (rc) {
525 ERROR(dev->cdev, "can't enable %s, result %d\n",
526 dev->in->name, rc);
527 return rc;
528 }
529 dev->out->driver_data = dev;
530 rc = usb_ep_enable(dev->out, dev->out_desc);
531 if (rc) {
532 ERROR(dev->cdev, "can't enable %s, result %d\n",
533 dev->out->name, rc);
534 usb_ep_disable(dev->in);
535 return rc;
536 }
537 schedule_work(&dev->config_work);
538
539 list_for_each_entry(ch, &usb_diag_ch_list, list) {
540 struct diag_context *ctxt;
541
542 ctxt = ch->priv_usb;
543 ctxt->dpkts_tolaptop = 0;
544 ctxt->dpkts_tomodem = 0;
545 ctxt->dpkts_tolaptop_pending = 0;
546 }
547
548 spin_lock_irqsave(&dev->lock, flags);
549 dev->configured = 1;
550 spin_unlock_irqrestore(&dev->lock, flags);
551
552 return rc;
553}
554
555static void diag_function_unbind(struct usb_configuration *c,
556 struct usb_function *f)
557{
558 struct diag_context *ctxt = func_to_diag(f);
559
560 if (gadget_is_dualspeed(c->cdev->gadget))
561 usb_free_descriptors(f->hs_descriptors);
562
563 usb_free_descriptors(f->descriptors);
564 ctxt->ch.priv_usb = NULL;
565}
566
567static int diag_function_bind(struct usb_configuration *c,
568 struct usb_function *f)
569{
570 struct usb_composite_dev *cdev = c->cdev;
571 struct diag_context *ctxt = func_to_diag(f);
572 struct usb_ep *ep;
573 int status = -ENODEV;
574
575 intf_desc.bInterfaceNumber = usb_interface_id(c, f);
576
577 ep = usb_ep_autoconfig(cdev->gadget, &fs_bulk_in_desc);
Vamsi Krishnae606a7b2011-08-18 12:21:44 -0700578 if (!ep)
579 goto fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700580 ctxt->in = ep;
581 ep->driver_data = ctxt;
582
583 ep = usb_ep_autoconfig(cdev->gadget, &fs_bulk_out_desc);
Vamsi Krishnae606a7b2011-08-18 12:21:44 -0700584 if (!ep)
585 goto fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700586 ctxt->out = ep;
587 ep->driver_data = ctxt;
588
589 /* copy descriptors, and track endpoint copies */
590 f->descriptors = usb_copy_descriptors(fs_diag_desc);
591 if (!f->descriptors)
592 goto fail;
593
594 if (gadget_is_dualspeed(c->cdev->gadget)) {
595 hs_bulk_in_desc.bEndpointAddress =
596 fs_bulk_in_desc.bEndpointAddress;
597 hs_bulk_out_desc.bEndpointAddress =
598 fs_bulk_out_desc.bEndpointAddress;
599
600 /* copy descriptors, and track endpoint copies */
601 f->hs_descriptors = usb_copy_descriptors(hs_diag_desc);
602 }
603 return 0;
604fail:
605 if (ctxt->out)
606 ctxt->out->driver_data = NULL;
607 if (ctxt->in)
608 ctxt->in->driver_data = NULL;
609 return status;
610
611}
612
613int diag_function_add(struct usb_configuration *c, const char *name,
614 int (*update_pid)(uint32_t, const char *))
615{
616 struct diag_context *dev;
617 struct usb_diag_ch *_ch;
618 int found = 0, ret;
619
620 DBG(c->cdev, "diag_function_add\n");
621
622 list_for_each_entry(_ch, &usb_diag_ch_list, list) {
623 if (!strcmp(name, _ch->name)) {
624 found = 1;
625 break;
626 }
627 }
628 if (!found) {
629 ERROR(c->cdev, "unable to get diag usb channel\n");
630 return -ENODEV;
631 }
632
633 dev = container_of(_ch, struct diag_context, ch);
634 /* claim the channel for this USB interface */
635 _ch->priv_usb = dev;
636
637 dev->update_pid_and_serial_num = update_pid;
638 dev->cdev = c->cdev;
639 dev->function.name = _ch->name;
640 dev->function.descriptors = fs_diag_desc;
641 dev->function.hs_descriptors = hs_diag_desc;
642 dev->function.bind = diag_function_bind;
643 dev->function.unbind = diag_function_unbind;
644 dev->function.set_alt = diag_function_set_alt;
645 dev->function.disable = diag_function_disable;
646 spin_lock_init(&dev->lock);
647 INIT_LIST_HEAD(&dev->read_pool);
648 INIT_LIST_HEAD(&dev->write_pool);
649 INIT_WORK(&dev->config_work, usb_config_work_func);
650
651 ret = usb_add_function(c, &dev->function);
652 if (ret) {
653 INFO(c->cdev, "usb_add_function failed\n");
654 _ch->priv_usb = NULL;
655 }
656
657 return ret;
658}
659
660
661#if defined(CONFIG_DEBUG_FS)
662static char debug_buffer[PAGE_SIZE];
663
664static ssize_t debug_read_stats(struct file *file, char __user *ubuf,
665 size_t count, loff_t *ppos)
666{
667 char *buf = debug_buffer;
668 int temp = 0;
669 struct usb_diag_ch *ch;
670
671 list_for_each_entry(ch, &usb_diag_ch_list, list) {
672 struct diag_context *ctxt;
673
674 ctxt = ch->priv_usb;
675
676 temp += scnprintf(buf + temp, PAGE_SIZE - temp,
677 "---Name: %s---\n"
678 "dpkts_tolaptop: %lu\n"
679 "dpkts_tomodem: %lu\n"
680 "pkts_tolaptop_pending: %u\n",
681 ch->name, ctxt->dpkts_tolaptop,
682 ctxt->dpkts_tomodem,
683 ctxt->dpkts_tolaptop_pending);
684 }
685
686 return simple_read_from_buffer(ubuf, count, ppos, buf, temp);
687}
688
689static ssize_t debug_reset_stats(struct file *file, const char __user *buf,
690 size_t count, loff_t *ppos)
691{
692 struct usb_diag_ch *ch;
693
694 list_for_each_entry(ch, &usb_diag_ch_list, list) {
695 struct diag_context *ctxt;
696
697 ctxt = ch->priv_usb;
698
699 ctxt->dpkts_tolaptop = 0;
700 ctxt->dpkts_tomodem = 0;
701 ctxt->dpkts_tolaptop_pending = 0;
702 }
703
704 return count;
705}
706
707static int debug_open(struct inode *inode, struct file *file)
708{
709 return 0;
710}
711
712static const struct file_operations debug_fdiag_ops = {
713 .open = debug_open,
714 .read = debug_read_stats,
715 .write = debug_reset_stats,
716};
717
Manu Gautamd9f56a12011-09-26 14:04:49 +0530718struct dentry *dent_diag;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700719static void fdiag_debugfs_init(void)
720{
Manu Gautamd9f56a12011-09-26 14:04:49 +0530721 dent_diag = debugfs_create_dir("usb_diag", 0);
722 if (IS_ERR(dent_diag))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700723 return;
724
Manu Gautamd9f56a12011-09-26 14:04:49 +0530725 debugfs_create_file("status", 0444, dent_diag, 0, &debug_fdiag_ops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700726}
727#else
728static void fdiag_debugfs_init(void)
729{
730 return;
731}
732#endif
733
734static void diag_cleanup(void)
735{
736 struct diag_context *dev;
737 struct list_head *act, *tmp;
738 struct usb_diag_ch *_ch;
739 unsigned long flags;
740
Manu Gautamd9f56a12011-09-26 14:04:49 +0530741 debugfs_remove_recursive(dent_diag);
742
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700743 list_for_each_safe(act, tmp, &usb_diag_ch_list) {
744 _ch = list_entry(act, struct usb_diag_ch, list);
745 dev = container_of(_ch, struct diag_context, ch);
746
747 spin_lock_irqsave(&ch_lock, flags);
748 /* Free if diagchar is not using the channel anymore */
749 if (!_ch->priv) {
750 list_del(&_ch->list);
751 kfree(dev);
752 }
753 spin_unlock_irqrestore(&ch_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700754 }
755}
756
757static int diag_setup(void)
758{
759 fdiag_debugfs_init();
760
761 return 0;
762}