blob: 3b9480e3ffa65a190ae33de89d49cef98d6851b7 [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>
Pavankumar Kondeti683077a2012-11-26 14:52:37 +053021#include <linux/ratelimit.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070022
23#include <mach/usbdiag.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070024
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;
Pavankumar Kondeti683077a2012-11-26 14:52:37 +0530388 static DEFINE_RATELIMIT_STATE(rl, 10*HZ, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700389
Manu Gautamb33f33a2011-09-09 14:35:57 +0530390 if (!ctxt)
391 return -ENODEV;
392
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700393 spin_lock_irqsave(&ctxt->lock, flags);
394
395 if (!ctxt->configured) {
396 spin_unlock_irqrestore(&ctxt->lock, flags);
397 return -EIO;
398 }
399
400 if (list_empty(&ctxt->read_pool)) {
401 spin_unlock_irqrestore(&ctxt->lock, flags);
402 ERROR(ctxt->cdev, "%s: no requests available\n", __func__);
403 return -EAGAIN;
404 }
405
406 req = list_first_entry(&ctxt->read_pool, struct usb_request, list);
407 list_del(&req->list);
408 spin_unlock_irqrestore(&ctxt->lock, flags);
409
410 req->buf = d_req->buf;
411 req->length = d_req->length;
412 req->context = d_req;
413 if (usb_ep_queue(ctxt->out, req, GFP_ATOMIC)) {
414 /* If error add the link to linked list again*/
415 spin_lock_irqsave(&ctxt->lock, flags);
416 list_add_tail(&req->list, &ctxt->read_pool);
417 spin_unlock_irqrestore(&ctxt->lock, flags);
Pavankumar Kondeti683077a2012-11-26 14:52:37 +0530418 /* 1 error message for every 10 sec */
419 if (__ratelimit(&rl))
420 ERROR(ctxt->cdev, "%s: cannot queue"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700421 " read request\n", __func__);
422 return -EIO;
423 }
424
425 return 0;
426}
427EXPORT_SYMBOL(usb_diag_read);
428
429/**
430 * usb_diag_write() - Write data from USB diag channel
431 * @ch: Channel handler
432 * @d_req: Diag request struct
433 *
434 * Enqueue a request on IN endpoint of the interface corresponding to this
435 * channel. This function returns proper error code when interface is not
436 * in configured state, no Tx requests available and ep queue is failed.
437 *
438 * This function operates asynchronously. WRITE_DONE event is notified after
439 * completion of IN request.
440 *
441 */
442int usb_diag_write(struct usb_diag_ch *ch, struct diag_request *d_req)
443{
444 struct diag_context *ctxt = ch->priv_usb;
445 unsigned long flags;
446 struct usb_request *req = NULL;
Pavankumar Kondeti683077a2012-11-26 14:52:37 +0530447 static DEFINE_RATELIMIT_STATE(rl, 10*HZ, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700448
Manu Gautamb33f33a2011-09-09 14:35:57 +0530449 if (!ctxt)
450 return -ENODEV;
451
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700452 spin_lock_irqsave(&ctxt->lock, flags);
453
454 if (!ctxt->configured) {
455 spin_unlock_irqrestore(&ctxt->lock, flags);
456 return -EIO;
457 }
458
459 if (list_empty(&ctxt->write_pool)) {
460 spin_unlock_irqrestore(&ctxt->lock, flags);
461 ERROR(ctxt->cdev, "%s: no requests available\n", __func__);
462 return -EAGAIN;
463 }
464
465 req = list_first_entry(&ctxt->write_pool, struct usb_request, list);
466 list_del(&req->list);
467 spin_unlock_irqrestore(&ctxt->lock, flags);
468
469 req->buf = d_req->buf;
470 req->length = d_req->length;
471 req->context = d_req;
472 if (usb_ep_queue(ctxt->in, req, GFP_ATOMIC)) {
473 /* If error add the link to linked list again*/
474 spin_lock_irqsave(&ctxt->lock, flags);
475 list_add_tail(&req->list, &ctxt->write_pool);
476 spin_unlock_irqrestore(&ctxt->lock, flags);
Pavankumar Kondeti683077a2012-11-26 14:52:37 +0530477 /* 1 error message for every 10 sec */
478 if (__ratelimit(&rl))
479 ERROR(ctxt->cdev, "%s: cannot queue"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700480 " read request\n", __func__);
481 return -EIO;
482 }
483
484 ctxt->dpkts_tolaptop++;
485 ctxt->dpkts_tolaptop_pending++;
486
487 return 0;
488}
489EXPORT_SYMBOL(usb_diag_write);
490
491static void diag_function_disable(struct usb_function *f)
492{
493 struct diag_context *dev = func_to_diag(f);
494 unsigned long flags;
495
496 DBG(dev->cdev, "diag_function_disable\n");
497
498 spin_lock_irqsave(&dev->lock, flags);
499 dev->configured = 0;
500 spin_unlock_irqrestore(&dev->lock, flags);
501
502 if (dev->ch.notify)
503 dev->ch.notify(dev->ch.priv, USB_DIAG_DISCONNECT, NULL);
504
505 usb_ep_disable(dev->in);
506 dev->in->driver_data = NULL;
507
508 usb_ep_disable(dev->out);
509 dev->out->driver_data = NULL;
510
511}
512
513static int diag_function_set_alt(struct usb_function *f,
514 unsigned intf, unsigned alt)
515{
516 struct diag_context *dev = func_to_diag(f);
517 struct usb_composite_dev *cdev = f->config->cdev;
518 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700519 int rc = 0;
520
Tatyana Brokhman31ac3522011-06-28 15:33:50 +0200521 if (config_ep_by_speed(cdev->gadget, f, dev->in) ||
522 config_ep_by_speed(cdev->gadget, f, dev->out)) {
523 dev->in->desc = NULL;
524 dev->out->desc = NULL;
525 return -EINVAL;
526 }
527
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700528 dev->in->driver_data = dev;
Tatyana Brokhmancf709c12011-06-28 16:33:48 +0300529 rc = usb_ep_enable(dev->in);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700530 if (rc) {
531 ERROR(dev->cdev, "can't enable %s, result %d\n",
532 dev->in->name, rc);
533 return rc;
534 }
535 dev->out->driver_data = dev;
Tatyana Brokhmancf709c12011-06-28 16:33:48 +0300536 rc = usb_ep_enable(dev->out);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700537 if (rc) {
538 ERROR(dev->cdev, "can't enable %s, result %d\n",
539 dev->out->name, rc);
540 usb_ep_disable(dev->in);
541 return rc;
542 }
543 schedule_work(&dev->config_work);
544
Manu Gautam0c611072011-11-11 17:40:05 +0530545 dev->dpkts_tolaptop = 0;
546 dev->dpkts_tomodem = 0;
547 dev->dpkts_tolaptop_pending = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700548
549 spin_lock_irqsave(&dev->lock, flags);
550 dev->configured = 1;
551 spin_unlock_irqrestore(&dev->lock, flags);
552
553 return rc;
554}
555
556static void diag_function_unbind(struct usb_configuration *c,
557 struct usb_function *f)
558{
559 struct diag_context *ctxt = func_to_diag(f);
560
561 if (gadget_is_dualspeed(c->cdev->gadget))
562 usb_free_descriptors(f->hs_descriptors);
563
564 usb_free_descriptors(f->descriptors);
565 ctxt->ch.priv_usb = NULL;
566}
567
568static int diag_function_bind(struct usb_configuration *c,
569 struct usb_function *f)
570{
571 struct usb_composite_dev *cdev = c->cdev;
572 struct diag_context *ctxt = func_to_diag(f);
573 struct usb_ep *ep;
574 int status = -ENODEV;
575
576 intf_desc.bInterfaceNumber = usb_interface_id(c, f);
577
578 ep = usb_ep_autoconfig(cdev->gadget, &fs_bulk_in_desc);
Vamsi Krishnae606a7b2011-08-18 12:21:44 -0700579 if (!ep)
580 goto fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700581 ctxt->in = ep;
582 ep->driver_data = ctxt;
583
584 ep = usb_ep_autoconfig(cdev->gadget, &fs_bulk_out_desc);
Vamsi Krishnae606a7b2011-08-18 12:21:44 -0700585 if (!ep)
586 goto fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700587 ctxt->out = ep;
588 ep->driver_data = ctxt;
589
590 /* copy descriptors, and track endpoint copies */
591 f->descriptors = usb_copy_descriptors(fs_diag_desc);
592 if (!f->descriptors)
593 goto fail;
594
595 if (gadget_is_dualspeed(c->cdev->gadget)) {
596 hs_bulk_in_desc.bEndpointAddress =
597 fs_bulk_in_desc.bEndpointAddress;
598 hs_bulk_out_desc.bEndpointAddress =
599 fs_bulk_out_desc.bEndpointAddress;
600
601 /* copy descriptors, and track endpoint copies */
602 f->hs_descriptors = usb_copy_descriptors(hs_diag_desc);
603 }
604 return 0;
605fail:
606 if (ctxt->out)
607 ctxt->out->driver_data = NULL;
608 if (ctxt->in)
609 ctxt->in->driver_data = NULL;
610 return status;
611
612}
613
614int diag_function_add(struct usb_configuration *c, const char *name,
615 int (*update_pid)(uint32_t, const char *))
616{
617 struct diag_context *dev;
618 struct usb_diag_ch *_ch;
619 int found = 0, ret;
620
621 DBG(c->cdev, "diag_function_add\n");
622
623 list_for_each_entry(_ch, &usb_diag_ch_list, list) {
624 if (!strcmp(name, _ch->name)) {
625 found = 1;
626 break;
627 }
628 }
629 if (!found) {
630 ERROR(c->cdev, "unable to get diag usb channel\n");
631 return -ENODEV;
632 }
633
634 dev = container_of(_ch, struct diag_context, ch);
635 /* claim the channel for this USB interface */
636 _ch->priv_usb = dev;
637
Tatyana Brokhmancf709c12011-06-28 16:33:48 +0300638 dev->update_pid_and_serial_num = update_pid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700639 dev->cdev = c->cdev;
640 dev->function.name = _ch->name;
641 dev->function.descriptors = fs_diag_desc;
642 dev->function.hs_descriptors = hs_diag_desc;
643 dev->function.bind = diag_function_bind;
644 dev->function.unbind = diag_function_unbind;
645 dev->function.set_alt = diag_function_set_alt;
646 dev->function.disable = diag_function_disable;
647 spin_lock_init(&dev->lock);
648 INIT_LIST_HEAD(&dev->read_pool);
649 INIT_LIST_HEAD(&dev->write_pool);
650 INIT_WORK(&dev->config_work, usb_config_work_func);
651
652 ret = usb_add_function(c, &dev->function);
653 if (ret) {
654 INFO(c->cdev, "usb_add_function failed\n");
655 _ch->priv_usb = NULL;
656 }
657
658 return ret;
659}
660
661
662#if defined(CONFIG_DEBUG_FS)
663static char debug_buffer[PAGE_SIZE];
664
665static ssize_t debug_read_stats(struct file *file, char __user *ubuf,
666 size_t count, loff_t *ppos)
667{
668 char *buf = debug_buffer;
669 int temp = 0;
670 struct usb_diag_ch *ch;
671
672 list_for_each_entry(ch, &usb_diag_ch_list, list) {
Jack Pham1c20e3f2012-04-04 19:29:14 -0700673 struct diag_context *ctxt = ch->priv_usb;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700674
Jack Pham1c20e3f2012-04-04 19:29:14 -0700675 if (ctxt)
676 temp += scnprintf(buf + temp, PAGE_SIZE - temp,
677 "---Name: %s---\n"
678 "endpoints: %s, %s\n"
679 "dpkts_tolaptop: %lu\n"
680 "dpkts_tomodem: %lu\n"
681 "pkts_tolaptop_pending: %u\n",
682 ch->name,
683 ctxt->in->name, ctxt->out->name,
684 ctxt->dpkts_tolaptop,
685 ctxt->dpkts_tomodem,
686 ctxt->dpkts_tolaptop_pending);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700687 }
688
689 return simple_read_from_buffer(ubuf, count, ppos, buf, temp);
690}
691
692static ssize_t debug_reset_stats(struct file *file, const char __user *buf,
693 size_t count, loff_t *ppos)
694{
695 struct usb_diag_ch *ch;
696
697 list_for_each_entry(ch, &usb_diag_ch_list, list) {
Jack Pham1c20e3f2012-04-04 19:29:14 -0700698 struct diag_context *ctxt = ch->priv_usb;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700699
Jack Pham1c20e3f2012-04-04 19:29:14 -0700700 if (ctxt) {
701 ctxt->dpkts_tolaptop = 0;
702 ctxt->dpkts_tomodem = 0;
703 ctxt->dpkts_tolaptop_pending = 0;
704 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700705 }
706
707 return count;
708}
709
710static int debug_open(struct inode *inode, struct file *file)
711{
712 return 0;
713}
714
715static const struct file_operations debug_fdiag_ops = {
716 .open = debug_open,
717 .read = debug_read_stats,
718 .write = debug_reset_stats,
719};
720
Manu Gautamd9f56a12011-09-26 14:04:49 +0530721struct dentry *dent_diag;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700722static void fdiag_debugfs_init(void)
723{
Manu Gautamd9f56a12011-09-26 14:04:49 +0530724 dent_diag = debugfs_create_dir("usb_diag", 0);
725 if (IS_ERR(dent_diag))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700726 return;
727
Manu Gautamd9f56a12011-09-26 14:04:49 +0530728 debugfs_create_file("status", 0444, dent_diag, 0, &debug_fdiag_ops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700729}
730#else
731static void fdiag_debugfs_init(void)
732{
733 return;
734}
735#endif
736
737static void diag_cleanup(void)
738{
739 struct diag_context *dev;
740 struct list_head *act, *tmp;
741 struct usb_diag_ch *_ch;
742 unsigned long flags;
743
Manu Gautamd9f56a12011-09-26 14:04:49 +0530744 debugfs_remove_recursive(dent_diag);
745
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700746 list_for_each_safe(act, tmp, &usb_diag_ch_list) {
747 _ch = list_entry(act, struct usb_diag_ch, list);
748 dev = container_of(_ch, struct diag_context, ch);
749
750 spin_lock_irqsave(&ch_lock, flags);
751 /* Free if diagchar is not using the channel anymore */
752 if (!_ch->priv) {
753 list_del(&_ch->list);
754 kfree(dev);
755 }
756 spin_unlock_irqrestore(&ch_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700757 }
758}
759
760static int diag_setup(void)
761{
762 fdiag_debugfs_init();
763
764 return 0;
765}