blob: 2785474ef73db011991aefa87454838c2294f936 [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.
Anna Perel97b8c222012-01-18 10:08:14 +02005 * Copyright (c) 2008-2012, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07006 * 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;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700111 struct list_head read_pool;
112 struct list_head write_pool;
113 struct work_struct config_work;
114 spinlock_t lock;
115 unsigned configured;
116 struct usb_composite_dev *cdev;
117 int (*update_pid_and_serial_num)(uint32_t, const char *);
118 struct usb_diag_ch ch;
119
120 /* pkt counters */
121 unsigned long dpkts_tolaptop;
122 unsigned long dpkts_tomodem;
123 unsigned dpkts_tolaptop_pending;
124};
125
126static inline struct diag_context *func_to_diag(struct usb_function *f)
127{
128 return container_of(f, struct diag_context, function);
129}
130
131static void usb_config_work_func(struct work_struct *work)
132{
133 struct diag_context *ctxt = container_of(work,
134 struct diag_context, config_work);
135 struct usb_composite_dev *cdev = ctxt->cdev;
136 struct usb_gadget_strings *table;
137 struct usb_string *s;
138
139 if (ctxt->ch.notify)
140 ctxt->ch.notify(ctxt->ch.priv, USB_DIAG_CONNECT, NULL);
141
142 if (!ctxt->update_pid_and_serial_num)
143 return;
144
145 /* pass on product id and serial number to dload */
146 if (!cdev->desc.iSerialNumber) {
147 ctxt->update_pid_and_serial_num(
148 cdev->desc.idProduct, 0);
149 return;
150 }
151
152 /*
153 * Serial number is filled by the composite driver. So
154 * it is fair enough to assume that it will always be
155 * found at first table of strings.
156 */
157 table = *(cdev->driver->strings);
158 for (s = table->strings; s && s->s; s++)
159 if (s->id == cdev->desc.iSerialNumber) {
160 ctxt->update_pid_and_serial_num(
161 cdev->desc.idProduct, s->s);
162 break;
163 }
164}
165
166static void diag_write_complete(struct usb_ep *ep,
167 struct usb_request *req)
168{
169 struct diag_context *ctxt = ep->driver_data;
170 struct diag_request *d_req = req->context;
171 unsigned long flags;
172
173 ctxt->dpkts_tolaptop_pending--;
174
175 if (!req->status) {
176 if ((req->length >= ep->maxpacket) &&
177 ((req->length % ep->maxpacket) == 0)) {
178 ctxt->dpkts_tolaptop_pending++;
179 req->length = 0;
180 d_req->actual = req->actual;
181 d_req->status = req->status;
182 /* Queue zero length packet */
183 usb_ep_queue(ctxt->in, req, GFP_ATOMIC);
184 return;
185 }
186 }
187
188 spin_lock_irqsave(&ctxt->lock, flags);
189 list_add_tail(&req->list, &ctxt->write_pool);
190 if (req->length != 0) {
191 d_req->actual = req->actual;
192 d_req->status = req->status;
193 }
194 spin_unlock_irqrestore(&ctxt->lock, flags);
195
196 if (ctxt->ch.notify)
197 ctxt->ch.notify(ctxt->ch.priv, USB_DIAG_WRITE_DONE, d_req);
198}
199
200static void diag_read_complete(struct usb_ep *ep,
201 struct usb_request *req)
202{
203 struct diag_context *ctxt = ep->driver_data;
204 struct diag_request *d_req = req->context;
205 unsigned long flags;
206
207 d_req->actual = req->actual;
208 d_req->status = req->status;
209
210 spin_lock_irqsave(&ctxt->lock, flags);
211 list_add_tail(&req->list, &ctxt->read_pool);
212 spin_unlock_irqrestore(&ctxt->lock, flags);
213
214 ctxt->dpkts_tomodem++;
215
216 if (ctxt->ch.notify)
217 ctxt->ch.notify(ctxt->ch.priv, USB_DIAG_READ_DONE, d_req);
218}
219
220/**
221 * usb_diag_open() - Open a diag channel over USB
222 * @name: Name of the channel
223 * @priv: Private structure pointer which will be passed in notify()
224 * @notify: Callback function to receive notifications
225 *
226 * This function iterates overs the available channels and returns
227 * the channel handler if the name matches. The notify callback is called
228 * for CONNECT, DISCONNECT, READ_DONE and WRITE_DONE events.
229 *
230 */
231struct usb_diag_ch *usb_diag_open(const char *name, void *priv,
232 void (*notify)(void *, unsigned, struct diag_request *))
233{
234 struct usb_diag_ch *ch;
235 struct diag_context *ctxt;
236 unsigned long flags;
237 int found = 0;
238
239 spin_lock_irqsave(&ch_lock, flags);
240 /* Check if we already have a channel with this name */
241 list_for_each_entry(ch, &usb_diag_ch_list, list) {
242 if (!strcmp(name, ch->name)) {
243 found = 1;
244 break;
245 }
246 }
247 spin_unlock_irqrestore(&ch_lock, flags);
248
249 if (!found) {
250 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
251 if (!ctxt)
252 return ERR_PTR(-ENOMEM);
253
254 ch = &ctxt->ch;
255 }
256
257 ch->name = name;
258 ch->priv = priv;
259 ch->notify = notify;
260
261 spin_lock_irqsave(&ch_lock, flags);
262 list_add_tail(&ch->list, &usb_diag_ch_list);
263 spin_unlock_irqrestore(&ch_lock, flags);
264
265 return ch;
266}
267EXPORT_SYMBOL(usb_diag_open);
268
269/**
270 * usb_diag_close() - Close a diag channel over USB
271 * @ch: Channel handler
272 *
273 * This function closes the diag channel.
274 *
275 */
276void usb_diag_close(struct usb_diag_ch *ch)
277{
278 struct diag_context *dev = container_of(ch, struct diag_context, ch);
279 unsigned long flags;
280
281 spin_lock_irqsave(&ch_lock, flags);
282 ch->priv = NULL;
283 ch->notify = NULL;
284 /* Free-up the resources if channel is no more active */
285 if (!ch->priv_usb) {
286 list_del(&ch->list);
287 kfree(dev);
288 }
289
290 spin_unlock_irqrestore(&ch_lock, flags);
291}
292EXPORT_SYMBOL(usb_diag_close);
293
294/**
295 * usb_diag_free_req() - Free USB requests
296 * @ch: Channel handler
297 *
298 * This function free read and write USB requests for the interface
299 * associated with this channel.
300 *
301 */
302void usb_diag_free_req(struct usb_diag_ch *ch)
303{
304 struct diag_context *ctxt = ch->priv_usb;
305 struct usb_request *req;
306 struct list_head *act, *tmp;
307
308 if (!ctxt)
309 return;
310
311 list_for_each_safe(act, tmp, &ctxt->write_pool) {
312 req = list_entry(act, struct usb_request, list);
313 list_del(&req->list);
314 usb_ep_free_request(ctxt->in, req);
315 }
316
317 list_for_each_safe(act, tmp, &ctxt->read_pool) {
318 req = list_entry(act, struct usb_request, list);
319 list_del(&req->list);
320 usb_ep_free_request(ctxt->out, req);
321 }
322}
323EXPORT_SYMBOL(usb_diag_free_req);
324
325/**
326 * usb_diag_alloc_req() - Allocate USB requests
327 * @ch: Channel handler
328 * @n_write: Number of requests for Tx
329 * @n_read: Number of requests for Rx
330 *
331 * This function allocate read and write USB requests for the interface
332 * associated with this channel. The actual buffer is not allocated.
333 * The buffer is passed by diag char driver.
334 *
335 */
336int usb_diag_alloc_req(struct usb_diag_ch *ch, int n_write, int n_read)
337{
338 struct diag_context *ctxt = ch->priv_usb;
339 struct usb_request *req;
340 int i;
341
342 if (!ctxt)
343 return -ENODEV;
344
345 for (i = 0; i < n_write; i++) {
346 req = usb_ep_alloc_request(ctxt->in, GFP_ATOMIC);
347 if (!req)
348 goto fail;
349 req->complete = diag_write_complete;
350 list_add_tail(&req->list, &ctxt->write_pool);
351 }
352
353 for (i = 0; i < n_read; i++) {
354 req = usb_ep_alloc_request(ctxt->out, GFP_ATOMIC);
355 if (!req)
356 goto fail;
357 req->complete = diag_read_complete;
358 list_add_tail(&req->list, &ctxt->read_pool);
359 }
360
361 return 0;
362
363fail:
364 usb_diag_free_req(ch);
365 return -ENOMEM;
366
367}
368EXPORT_SYMBOL(usb_diag_alloc_req);
369
370/**
371 * usb_diag_read() - Read data from USB diag channel
372 * @ch: Channel handler
373 * @d_req: Diag request struct
374 *
375 * Enqueue a request on OUT endpoint of the interface corresponding to this
376 * channel. This function returns proper error code when interface is not
377 * in configured state, no Rx requests available and ep queue is failed.
378 *
379 * This function operates asynchronously. READ_DONE event is notified after
380 * completion of OUT request.
381 *
382 */
383int usb_diag_read(struct usb_diag_ch *ch, struct diag_request *d_req)
384{
385 struct diag_context *ctxt = ch->priv_usb;
386 unsigned long flags;
387 struct usb_request *req;
388
Manu Gautamb33f33a2011-09-09 14:35:57 +0530389 if (!ctxt)
390 return -ENODEV;
391
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700392 spin_lock_irqsave(&ctxt->lock, flags);
393
394 if (!ctxt->configured) {
395 spin_unlock_irqrestore(&ctxt->lock, flags);
396 return -EIO;
397 }
398
399 if (list_empty(&ctxt->read_pool)) {
400 spin_unlock_irqrestore(&ctxt->lock, flags);
401 ERROR(ctxt->cdev, "%s: no requests available\n", __func__);
402 return -EAGAIN;
403 }
404
405 req = list_first_entry(&ctxt->read_pool, struct usb_request, list);
406 list_del(&req->list);
407 spin_unlock_irqrestore(&ctxt->lock, flags);
408
409 req->buf = d_req->buf;
410 req->length = d_req->length;
411 req->context = d_req;
412 if (usb_ep_queue(ctxt->out, req, GFP_ATOMIC)) {
413 /* If error add the link to linked list again*/
414 spin_lock_irqsave(&ctxt->lock, flags);
415 list_add_tail(&req->list, &ctxt->read_pool);
416 spin_unlock_irqrestore(&ctxt->lock, flags);
417 ERROR(ctxt->cdev, "%s: cannot queue"
418 " read request\n", __func__);
419 return -EIO;
420 }
421
422 return 0;
423}
424EXPORT_SYMBOL(usb_diag_read);
425
426/**
427 * usb_diag_write() - Write data from USB diag channel
428 * @ch: Channel handler
429 * @d_req: Diag request struct
430 *
431 * Enqueue a request on IN endpoint of the interface corresponding to this
432 * channel. This function returns proper error code when interface is not
433 * in configured state, no Tx requests available and ep queue is failed.
434 *
435 * This function operates asynchronously. WRITE_DONE event is notified after
436 * completion of IN request.
437 *
438 */
439int usb_diag_write(struct usb_diag_ch *ch, struct diag_request *d_req)
440{
441 struct diag_context *ctxt = ch->priv_usb;
442 unsigned long flags;
443 struct usb_request *req = NULL;
444
Manu Gautamb33f33a2011-09-09 14:35:57 +0530445 if (!ctxt)
446 return -ENODEV;
447
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700448 spin_lock_irqsave(&ctxt->lock, flags);
449
450 if (!ctxt->configured) {
451 spin_unlock_irqrestore(&ctxt->lock, flags);
452 return -EIO;
453 }
454
455 if (list_empty(&ctxt->write_pool)) {
456 spin_unlock_irqrestore(&ctxt->lock, flags);
457 ERROR(ctxt->cdev, "%s: no requests available\n", __func__);
458 return -EAGAIN;
459 }
460
461 req = list_first_entry(&ctxt->write_pool, struct usb_request, list);
462 list_del(&req->list);
463 spin_unlock_irqrestore(&ctxt->lock, flags);
464
465 req->buf = d_req->buf;
466 req->length = d_req->length;
467 req->context = d_req;
468 if (usb_ep_queue(ctxt->in, req, GFP_ATOMIC)) {
469 /* If error add the link to linked list again*/
470 spin_lock_irqsave(&ctxt->lock, flags);
471 list_add_tail(&req->list, &ctxt->write_pool);
472 spin_unlock_irqrestore(&ctxt->lock, flags);
473 ERROR(ctxt->cdev, "%s: cannot queue"
474 " read request\n", __func__);
475 return -EIO;
476 }
477
478 ctxt->dpkts_tolaptop++;
479 ctxt->dpkts_tolaptop_pending++;
480
481 return 0;
482}
483EXPORT_SYMBOL(usb_diag_write);
484
485static void diag_function_disable(struct usb_function *f)
486{
487 struct diag_context *dev = func_to_diag(f);
488 unsigned long flags;
489
490 DBG(dev->cdev, "diag_function_disable\n");
491
492 spin_lock_irqsave(&dev->lock, flags);
493 dev->configured = 0;
494 spin_unlock_irqrestore(&dev->lock, flags);
495
496 if (dev->ch.notify)
497 dev->ch.notify(dev->ch.priv, USB_DIAG_DISCONNECT, NULL);
498
499 usb_ep_disable(dev->in);
500 dev->in->driver_data = NULL;
501
502 usb_ep_disable(dev->out);
503 dev->out->driver_data = NULL;
504
505}
506
507static int diag_function_set_alt(struct usb_function *f,
508 unsigned intf, unsigned alt)
509{
510 struct diag_context *dev = func_to_diag(f);
511 struct usb_composite_dev *cdev = f->config->cdev;
512 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700513 int rc = 0;
514
Tatyana Brokhmancf709c12011-06-28 16:33:48 +0300515 dev->in->desc = ep_choose(cdev->gadget,
David Brownac5d1542012-02-06 10:37:22 -0800516 (struct usb_endpoint_descriptor *)f->hs_descriptors[1],
517 (struct usb_endpoint_descriptor *)f->descriptors[1]);
Tatyana Brokhmancf709c12011-06-28 16:33:48 +0300518 dev->out->desc = ep_choose(cdev->gadget,
David Brownac5d1542012-02-06 10:37:22 -0800519 (struct usb_endpoint_descriptor *)f->hs_descriptors[2],
520 (struct usb_endpoint_descriptor *)f->descriptors[2]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700521 dev->in->driver_data = dev;
Tatyana Brokhmancf709c12011-06-28 16:33:48 +0300522 rc = usb_ep_enable(dev->in);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700523 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;
Tatyana Brokhmancf709c12011-06-28 16:33:48 +0300529 rc = usb_ep_enable(dev->out);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700530 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
Tatyana Brokhmancf709c12011-06-28 16:33:48 +0300631 dev->update_pid_and_serial_num = update_pid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700632 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"
Jack Phamb830a6c2011-12-12 22:35:27 -0800672 "endpoints: %s, %s\n"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700673 "dpkts_tolaptop: %lu\n"
674 "dpkts_tomodem: %lu\n"
675 "pkts_tolaptop_pending: %u\n",
Jack Phamb830a6c2011-12-12 22:35:27 -0800676 ch->name,
677 ctxt->in->name, ctxt->out->name,
678 ctxt->dpkts_tolaptop,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700679 ctxt->dpkts_tomodem,
680 ctxt->dpkts_tolaptop_pending);
681 }
682
683 return simple_read_from_buffer(ubuf, count, ppos, buf, temp);
684}
685
686static ssize_t debug_reset_stats(struct file *file, const char __user *buf,
687 size_t count, loff_t *ppos)
688{
689 struct usb_diag_ch *ch;
690
691 list_for_each_entry(ch, &usb_diag_ch_list, list) {
692 struct diag_context *ctxt;
693
694 ctxt = ch->priv_usb;
695
696 ctxt->dpkts_tolaptop = 0;
697 ctxt->dpkts_tomodem = 0;
698 ctxt->dpkts_tolaptop_pending = 0;
699 }
700
701 return count;
702}
703
704static int debug_open(struct inode *inode, struct file *file)
705{
706 return 0;
707}
708
709static const struct file_operations debug_fdiag_ops = {
710 .open = debug_open,
711 .read = debug_read_stats,
712 .write = debug_reset_stats,
713};
714
Manu Gautamd9f56a12011-09-26 14:04:49 +0530715struct dentry *dent_diag;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700716static void fdiag_debugfs_init(void)
717{
Manu Gautamd9f56a12011-09-26 14:04:49 +0530718 dent_diag = debugfs_create_dir("usb_diag", 0);
719 if (IS_ERR(dent_diag))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700720 return;
721
Manu Gautamd9f56a12011-09-26 14:04:49 +0530722 debugfs_create_file("status", 0444, dent_diag, 0, &debug_fdiag_ops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700723}
724#else
725static void fdiag_debugfs_init(void)
726{
727 return;
728}
729#endif
730
731static void diag_cleanup(void)
732{
733 struct diag_context *dev;
734 struct list_head *act, *tmp;
735 struct usb_diag_ch *_ch;
736 unsigned long flags;
737
Manu Gautamd9f56a12011-09-26 14:04:49 +0530738 debugfs_remove_recursive(dent_diag);
739
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700740 list_for_each_safe(act, tmp, &usb_diag_ch_list) {
741 _ch = list_entry(act, struct usb_diag_ch, list);
742 dev = container_of(_ch, struct diag_context, ch);
743
744 spin_lock_irqsave(&ch_lock, flags);
745 /* Free if diagchar is not using the channel anymore */
746 if (!_ch->priv) {
747 list_del(&_ch->list);
748 kfree(dev);
749 }
750 spin_unlock_irqrestore(&ch_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700751 }
752}
753
754static int diag_setup(void)
755{
756 fdiag_debugfs_init();
757
758 return 0;
759}