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