blob: 3ba59164a98ff762bcc534c713a25e8327a42b23 [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.
Pavankumar Kondetid580a762013-01-02 14:46:58 +05305 * Copyright (c) 2008-2013, Linux Foundation. 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)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070099 * @lock: Spinlock to proctect read_pool, write_pool lists
100 * @cdev: USB composite device struct
101 * @ch: USB diag channel
102 *
103 */
104struct diag_context {
105 struct usb_function function;
106 struct usb_ep *out;
107 struct usb_ep *in;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700108 struct list_head read_pool;
109 struct list_head write_pool;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700110 spinlock_t lock;
111 unsigned configured;
112 struct usb_composite_dev *cdev;
113 int (*update_pid_and_serial_num)(uint32_t, const char *);
114 struct usb_diag_ch ch;
115
116 /* pkt counters */
117 unsigned long dpkts_tolaptop;
118 unsigned long dpkts_tomodem;
119 unsigned dpkts_tolaptop_pending;
120};
121
122static inline struct diag_context *func_to_diag(struct usb_function *f)
123{
124 return container_of(f, struct diag_context, function);
125}
126
Pavankumar Kondeti3af6c512013-04-18 11:02:28 +0530127static void diag_update_pid_and_serial_num(struct diag_context *ctxt)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700128{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700129 struct usb_composite_dev *cdev = ctxt->cdev;
130 struct usb_gadget_strings *table;
131 struct usb_string *s;
132
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700133 if (!ctxt->update_pid_and_serial_num)
134 return;
135
136 /* pass on product id and serial number to dload */
137 if (!cdev->desc.iSerialNumber) {
138 ctxt->update_pid_and_serial_num(
139 cdev->desc.idProduct, 0);
140 return;
141 }
142
143 /*
144 * Serial number is filled by the composite driver. So
145 * it is fair enough to assume that it will always be
146 * found at first table of strings.
147 */
148 table = *(cdev->driver->strings);
149 for (s = table->strings; s && s->s; s++)
150 if (s->id == cdev->desc.iSerialNumber) {
151 ctxt->update_pid_and_serial_num(
152 cdev->desc.idProduct, s->s);
153 break;
154 }
155}
156
157static void diag_write_complete(struct usb_ep *ep,
158 struct usb_request *req)
159{
160 struct diag_context *ctxt = ep->driver_data;
161 struct diag_request *d_req = req->context;
162 unsigned long flags;
163
164 ctxt->dpkts_tolaptop_pending--;
165
166 if (!req->status) {
167 if ((req->length >= ep->maxpacket) &&
168 ((req->length % ep->maxpacket) == 0)) {
169 ctxt->dpkts_tolaptop_pending++;
170 req->length = 0;
171 d_req->actual = req->actual;
172 d_req->status = req->status;
173 /* Queue zero length packet */
174 usb_ep_queue(ctxt->in, req, GFP_ATOMIC);
175 return;
176 }
177 }
178
179 spin_lock_irqsave(&ctxt->lock, flags);
180 list_add_tail(&req->list, &ctxt->write_pool);
181 if (req->length != 0) {
182 d_req->actual = req->actual;
183 d_req->status = req->status;
184 }
185 spin_unlock_irqrestore(&ctxt->lock, flags);
186
187 if (ctxt->ch.notify)
188 ctxt->ch.notify(ctxt->ch.priv, USB_DIAG_WRITE_DONE, d_req);
189}
190
191static void diag_read_complete(struct usb_ep *ep,
192 struct usb_request *req)
193{
194 struct diag_context *ctxt = ep->driver_data;
195 struct diag_request *d_req = req->context;
196 unsigned long flags;
197
198 d_req->actual = req->actual;
199 d_req->status = req->status;
200
201 spin_lock_irqsave(&ctxt->lock, flags);
202 list_add_tail(&req->list, &ctxt->read_pool);
203 spin_unlock_irqrestore(&ctxt->lock, flags);
204
205 ctxt->dpkts_tomodem++;
206
207 if (ctxt->ch.notify)
208 ctxt->ch.notify(ctxt->ch.priv, USB_DIAG_READ_DONE, d_req);
209}
210
211/**
212 * usb_diag_open() - Open a diag channel over USB
213 * @name: Name of the channel
214 * @priv: Private structure pointer which will be passed in notify()
215 * @notify: Callback function to receive notifications
216 *
217 * This function iterates overs the available channels and returns
218 * the channel handler if the name matches. The notify callback is called
219 * for CONNECT, DISCONNECT, READ_DONE and WRITE_DONE events.
220 *
221 */
222struct usb_diag_ch *usb_diag_open(const char *name, void *priv,
223 void (*notify)(void *, unsigned, struct diag_request *))
224{
225 struct usb_diag_ch *ch;
226 struct diag_context *ctxt;
227 unsigned long flags;
228 int found = 0;
229
230 spin_lock_irqsave(&ch_lock, flags);
231 /* Check if we already have a channel with this name */
232 list_for_each_entry(ch, &usb_diag_ch_list, list) {
233 if (!strcmp(name, ch->name)) {
234 found = 1;
235 break;
236 }
237 }
238 spin_unlock_irqrestore(&ch_lock, flags);
239
240 if (!found) {
241 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
242 if (!ctxt)
243 return ERR_PTR(-ENOMEM);
244
245 ch = &ctxt->ch;
246 }
247
248 ch->name = name;
249 ch->priv = priv;
250 ch->notify = notify;
251
252 spin_lock_irqsave(&ch_lock, flags);
253 list_add_tail(&ch->list, &usb_diag_ch_list);
254 spin_unlock_irqrestore(&ch_lock, flags);
255
256 return ch;
257}
258EXPORT_SYMBOL(usb_diag_open);
259
260/**
261 * usb_diag_close() - Close a diag channel over USB
262 * @ch: Channel handler
263 *
264 * This function closes the diag channel.
265 *
266 */
267void usb_diag_close(struct usb_diag_ch *ch)
268{
269 struct diag_context *dev = container_of(ch, struct diag_context, ch);
270 unsigned long flags;
271
272 spin_lock_irqsave(&ch_lock, flags);
273 ch->priv = NULL;
274 ch->notify = NULL;
275 /* Free-up the resources if channel is no more active */
276 if (!ch->priv_usb) {
277 list_del(&ch->list);
278 kfree(dev);
279 }
280
281 spin_unlock_irqrestore(&ch_lock, flags);
282}
283EXPORT_SYMBOL(usb_diag_close);
284
Pavankumar Kondetid580a762013-01-02 14:46:58 +0530285static void free_reqs(struct diag_context *ctxt)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700286{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700287 struct list_head *act, *tmp;
Pavankumar Kondetid580a762013-01-02 14:46:58 +0530288 struct usb_request *req;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700289
290 list_for_each_safe(act, tmp, &ctxt->write_pool) {
291 req = list_entry(act, struct usb_request, list);
292 list_del(&req->list);
293 usb_ep_free_request(ctxt->in, req);
294 }
295
296 list_for_each_safe(act, tmp, &ctxt->read_pool) {
297 req = list_entry(act, struct usb_request, list);
298 list_del(&req->list);
299 usb_ep_free_request(ctxt->out, req);
300 }
301}
Pavankumar Kondetid580a762013-01-02 14:46:58 +0530302
303/**
304 * usb_diag_free_req() - Free USB requests
305 * @ch: Channel handler
306 *
307 * This function free read and write USB requests for the interface
308 * associated with this channel.
309 *
310 */
311void usb_diag_free_req(struct usb_diag_ch *ch)
312{
313 struct diag_context *ctxt = ch->priv_usb;
314 unsigned long flags;
315
316 if (ctxt) {
317 spin_lock_irqsave(&ctxt->lock, flags);
318 free_reqs(ctxt);
319 spin_unlock_irqrestore(&ctxt->lock, flags);
320 }
321
322}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700323EXPORT_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;
Pavankumar Kondetid580a762013-01-02 14:46:58 +0530341 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700342
343 if (!ctxt)
344 return -ENODEV;
345
Pavankumar Kondetid580a762013-01-02 14:46:58 +0530346 spin_lock_irqsave(&ctxt->lock, flags);
347 /* Free previous session's stale requests */
348 free_reqs(ctxt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700349 for (i = 0; i < n_write; i++) {
350 req = usb_ep_alloc_request(ctxt->in, GFP_ATOMIC);
351 if (!req)
352 goto fail;
353 req->complete = diag_write_complete;
354 list_add_tail(&req->list, &ctxt->write_pool);
355 }
356
357 for (i = 0; i < n_read; i++) {
358 req = usb_ep_alloc_request(ctxt->out, GFP_ATOMIC);
359 if (!req)
360 goto fail;
361 req->complete = diag_read_complete;
362 list_add_tail(&req->list, &ctxt->read_pool);
363 }
Pavankumar Kondetid580a762013-01-02 14:46:58 +0530364 spin_unlock_irqrestore(&ctxt->lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700365 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700366fail:
Pavankumar Kondetid580a762013-01-02 14:46:58 +0530367 free_reqs(ctxt);
368 spin_unlock_irqrestore(&ctxt->lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700369 return -ENOMEM;
370
371}
372EXPORT_SYMBOL(usb_diag_alloc_req);
373
374/**
375 * usb_diag_read() - Read data from USB diag channel
376 * @ch: Channel handler
377 * @d_req: Diag request struct
378 *
379 * Enqueue a request on OUT endpoint of the interface corresponding to this
380 * channel. This function returns proper error code when interface is not
381 * in configured state, no Rx requests available and ep queue is failed.
382 *
383 * This function operates asynchronously. READ_DONE event is notified after
384 * completion of OUT request.
385 *
386 */
387int usb_diag_read(struct usb_diag_ch *ch, struct diag_request *d_req)
388{
389 struct diag_context *ctxt = ch->priv_usb;
390 unsigned long flags;
391 struct usb_request *req;
Pavankumar Kondeti683077a2012-11-26 14:52:37 +0530392 static DEFINE_RATELIMIT_STATE(rl, 10*HZ, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700393
Manu Gautamb33f33a2011-09-09 14:35:57 +0530394 if (!ctxt)
395 return -ENODEV;
396
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700397 spin_lock_irqsave(&ctxt->lock, flags);
398
399 if (!ctxt->configured) {
400 spin_unlock_irqrestore(&ctxt->lock, flags);
401 return -EIO;
402 }
403
404 if (list_empty(&ctxt->read_pool)) {
405 spin_unlock_irqrestore(&ctxt->lock, flags);
406 ERROR(ctxt->cdev, "%s: no requests available\n", __func__);
407 return -EAGAIN;
408 }
409
410 req = list_first_entry(&ctxt->read_pool, struct usb_request, list);
411 list_del(&req->list);
412 spin_unlock_irqrestore(&ctxt->lock, flags);
413
414 req->buf = d_req->buf;
415 req->length = d_req->length;
416 req->context = d_req;
417 if (usb_ep_queue(ctxt->out, req, GFP_ATOMIC)) {
418 /* If error add the link to linked list again*/
419 spin_lock_irqsave(&ctxt->lock, flags);
420 list_add_tail(&req->list, &ctxt->read_pool);
421 spin_unlock_irqrestore(&ctxt->lock, flags);
Pavankumar Kondeti683077a2012-11-26 14:52:37 +0530422 /* 1 error message for every 10 sec */
423 if (__ratelimit(&rl))
424 ERROR(ctxt->cdev, "%s: cannot queue"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700425 " read request\n", __func__);
426 return -EIO;
427 }
428
429 return 0;
430}
431EXPORT_SYMBOL(usb_diag_read);
432
433/**
434 * usb_diag_write() - Write data from USB diag channel
435 * @ch: Channel handler
436 * @d_req: Diag request struct
437 *
438 * Enqueue a request on IN endpoint of the interface corresponding to this
439 * channel. This function returns proper error code when interface is not
440 * in configured state, no Tx requests available and ep queue is failed.
441 *
442 * This function operates asynchronously. WRITE_DONE event is notified after
443 * completion of IN request.
444 *
445 */
446int usb_diag_write(struct usb_diag_ch *ch, struct diag_request *d_req)
447{
448 struct diag_context *ctxt = ch->priv_usb;
449 unsigned long flags;
450 struct usb_request *req = NULL;
Pavankumar Kondeti683077a2012-11-26 14:52:37 +0530451 static DEFINE_RATELIMIT_STATE(rl, 10*HZ, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700452
Manu Gautamb33f33a2011-09-09 14:35:57 +0530453 if (!ctxt)
454 return -ENODEV;
455
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700456 spin_lock_irqsave(&ctxt->lock, flags);
457
458 if (!ctxt->configured) {
459 spin_unlock_irqrestore(&ctxt->lock, flags);
460 return -EIO;
461 }
462
463 if (list_empty(&ctxt->write_pool)) {
464 spin_unlock_irqrestore(&ctxt->lock, flags);
465 ERROR(ctxt->cdev, "%s: no requests available\n", __func__);
466 return -EAGAIN;
467 }
468
469 req = list_first_entry(&ctxt->write_pool, struct usb_request, list);
470 list_del(&req->list);
471 spin_unlock_irqrestore(&ctxt->lock, flags);
472
473 req->buf = d_req->buf;
474 req->length = d_req->length;
475 req->context = d_req;
476 if (usb_ep_queue(ctxt->in, req, GFP_ATOMIC)) {
477 /* If error add the link to linked list again*/
478 spin_lock_irqsave(&ctxt->lock, flags);
479 list_add_tail(&req->list, &ctxt->write_pool);
480 spin_unlock_irqrestore(&ctxt->lock, flags);
Pavankumar Kondeti683077a2012-11-26 14:52:37 +0530481 /* 1 error message for every 10 sec */
482 if (__ratelimit(&rl))
483 ERROR(ctxt->cdev, "%s: cannot queue"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700484 " read request\n", __func__);
485 return -EIO;
486 }
487
488 ctxt->dpkts_tolaptop++;
489 ctxt->dpkts_tolaptop_pending++;
490
491 return 0;
492}
493EXPORT_SYMBOL(usb_diag_write);
494
495static void diag_function_disable(struct usb_function *f)
496{
497 struct diag_context *dev = func_to_diag(f);
498 unsigned long flags;
499
500 DBG(dev->cdev, "diag_function_disable\n");
501
502 spin_lock_irqsave(&dev->lock, flags);
503 dev->configured = 0;
504 spin_unlock_irqrestore(&dev->lock, flags);
505
506 if (dev->ch.notify)
507 dev->ch.notify(dev->ch.priv, USB_DIAG_DISCONNECT, NULL);
508
509 usb_ep_disable(dev->in);
510 dev->in->driver_data = NULL;
511
512 usb_ep_disable(dev->out);
513 dev->out->driver_data = NULL;
514
515}
516
517static int diag_function_set_alt(struct usb_function *f,
518 unsigned intf, unsigned alt)
519{
520 struct diag_context *dev = func_to_diag(f);
521 struct usb_composite_dev *cdev = f->config->cdev;
522 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700523 int rc = 0;
524
Tatyana Brokhman31ac3522011-06-28 15:33:50 +0200525 if (config_ep_by_speed(cdev->gadget, f, dev->in) ||
526 config_ep_by_speed(cdev->gadget, f, dev->out)) {
527 dev->in->desc = NULL;
528 dev->out->desc = NULL;
529 return -EINVAL;
530 }
531
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700532 dev->in->driver_data = dev;
Tatyana Brokhmancf709c12011-06-28 16:33:48 +0300533 rc = usb_ep_enable(dev->in);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700534 if (rc) {
535 ERROR(dev->cdev, "can't enable %s, result %d\n",
536 dev->in->name, rc);
537 return rc;
538 }
539 dev->out->driver_data = dev;
Tatyana Brokhmancf709c12011-06-28 16:33:48 +0300540 rc = usb_ep_enable(dev->out);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700541 if (rc) {
542 ERROR(dev->cdev, "can't enable %s, result %d\n",
543 dev->out->name, rc);
544 usb_ep_disable(dev->in);
545 return rc;
546 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700547
Manu Gautam0c611072011-11-11 17:40:05 +0530548 dev->dpkts_tolaptop = 0;
549 dev->dpkts_tomodem = 0;
550 dev->dpkts_tolaptop_pending = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700551
552 spin_lock_irqsave(&dev->lock, flags);
553 dev->configured = 1;
554 spin_unlock_irqrestore(&dev->lock, flags);
555
Pavankumar Kondeti3af6c512013-04-18 11:02:28 +0530556 if (dev->ch.notify)
557 dev->ch.notify(dev->ch.priv, USB_DIAG_CONNECT, NULL);
558
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700559 return rc;
560}
561
562static void diag_function_unbind(struct usb_configuration *c,
563 struct usb_function *f)
564{
565 struct diag_context *ctxt = func_to_diag(f);
566
567 if (gadget_is_dualspeed(c->cdev->gadget))
568 usb_free_descriptors(f->hs_descriptors);
569
570 usb_free_descriptors(f->descriptors);
571 ctxt->ch.priv_usb = NULL;
572}
573
574static int diag_function_bind(struct usb_configuration *c,
575 struct usb_function *f)
576{
577 struct usb_composite_dev *cdev = c->cdev;
578 struct diag_context *ctxt = func_to_diag(f);
579 struct usb_ep *ep;
580 int status = -ENODEV;
581
582 intf_desc.bInterfaceNumber = usb_interface_id(c, f);
583
584 ep = usb_ep_autoconfig(cdev->gadget, &fs_bulk_in_desc);
Vamsi Krishnae606a7b2011-08-18 12:21:44 -0700585 if (!ep)
586 goto fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700587 ctxt->in = ep;
588 ep->driver_data = ctxt;
589
590 ep = usb_ep_autoconfig(cdev->gadget, &fs_bulk_out_desc);
Vamsi Krishnae606a7b2011-08-18 12:21:44 -0700591 if (!ep)
592 goto fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700593 ctxt->out = ep;
594 ep->driver_data = ctxt;
595
596 /* copy descriptors, and track endpoint copies */
597 f->descriptors = usb_copy_descriptors(fs_diag_desc);
598 if (!f->descriptors)
599 goto fail;
600
601 if (gadget_is_dualspeed(c->cdev->gadget)) {
602 hs_bulk_in_desc.bEndpointAddress =
603 fs_bulk_in_desc.bEndpointAddress;
604 hs_bulk_out_desc.bEndpointAddress =
605 fs_bulk_out_desc.bEndpointAddress;
606
607 /* copy descriptors, and track endpoint copies */
608 f->hs_descriptors = usb_copy_descriptors(hs_diag_desc);
609 }
Pavankumar Kondeti3af6c512013-04-18 11:02:28 +0530610 diag_update_pid_and_serial_num(ctxt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700611 return 0;
612fail:
613 if (ctxt->out)
614 ctxt->out->driver_data = NULL;
615 if (ctxt->in)
616 ctxt->in->driver_data = NULL;
617 return status;
618
619}
620
621int diag_function_add(struct usb_configuration *c, const char *name,
622 int (*update_pid)(uint32_t, const char *))
623{
624 struct diag_context *dev;
625 struct usb_diag_ch *_ch;
626 int found = 0, ret;
627
628 DBG(c->cdev, "diag_function_add\n");
629
630 list_for_each_entry(_ch, &usb_diag_ch_list, list) {
631 if (!strcmp(name, _ch->name)) {
632 found = 1;
633 break;
634 }
635 }
636 if (!found) {
637 ERROR(c->cdev, "unable to get diag usb channel\n");
638 return -ENODEV;
639 }
640
641 dev = container_of(_ch, struct diag_context, ch);
642 /* claim the channel for this USB interface */
643 _ch->priv_usb = dev;
644
Tatyana Brokhmancf709c12011-06-28 16:33:48 +0300645 dev->update_pid_and_serial_num = update_pid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700646 dev->cdev = c->cdev;
647 dev->function.name = _ch->name;
648 dev->function.descriptors = fs_diag_desc;
649 dev->function.hs_descriptors = hs_diag_desc;
650 dev->function.bind = diag_function_bind;
651 dev->function.unbind = diag_function_unbind;
652 dev->function.set_alt = diag_function_set_alt;
653 dev->function.disable = diag_function_disable;
654 spin_lock_init(&dev->lock);
655 INIT_LIST_HEAD(&dev->read_pool);
656 INIT_LIST_HEAD(&dev->write_pool);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700657
658 ret = usb_add_function(c, &dev->function);
659 if (ret) {
660 INFO(c->cdev, "usb_add_function failed\n");
661 _ch->priv_usb = NULL;
662 }
663
664 return ret;
665}
666
667
668#if defined(CONFIG_DEBUG_FS)
669static char debug_buffer[PAGE_SIZE];
670
671static ssize_t debug_read_stats(struct file *file, char __user *ubuf,
672 size_t count, loff_t *ppos)
673{
674 char *buf = debug_buffer;
675 int temp = 0;
676 struct usb_diag_ch *ch;
677
678 list_for_each_entry(ch, &usb_diag_ch_list, list) {
Jack Pham1c20e3f2012-04-04 19:29:14 -0700679 struct diag_context *ctxt = ch->priv_usb;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700680
Jack Pham1c20e3f2012-04-04 19:29:14 -0700681 if (ctxt)
682 temp += scnprintf(buf + temp, PAGE_SIZE - temp,
683 "---Name: %s---\n"
684 "endpoints: %s, %s\n"
685 "dpkts_tolaptop: %lu\n"
686 "dpkts_tomodem: %lu\n"
687 "pkts_tolaptop_pending: %u\n",
688 ch->name,
689 ctxt->in->name, ctxt->out->name,
690 ctxt->dpkts_tolaptop,
691 ctxt->dpkts_tomodem,
692 ctxt->dpkts_tolaptop_pending);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700693 }
694
695 return simple_read_from_buffer(ubuf, count, ppos, buf, temp);
696}
697
698static ssize_t debug_reset_stats(struct file *file, const char __user *buf,
699 size_t count, loff_t *ppos)
700{
701 struct usb_diag_ch *ch;
702
703 list_for_each_entry(ch, &usb_diag_ch_list, list) {
Jack Pham1c20e3f2012-04-04 19:29:14 -0700704 struct diag_context *ctxt = ch->priv_usb;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700705
Jack Pham1c20e3f2012-04-04 19:29:14 -0700706 if (ctxt) {
707 ctxt->dpkts_tolaptop = 0;
708 ctxt->dpkts_tomodem = 0;
709 ctxt->dpkts_tolaptop_pending = 0;
710 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700711 }
712
713 return count;
714}
715
716static int debug_open(struct inode *inode, struct file *file)
717{
718 return 0;
719}
720
721static const struct file_operations debug_fdiag_ops = {
722 .open = debug_open,
723 .read = debug_read_stats,
724 .write = debug_reset_stats,
725};
726
Manu Gautamd9f56a12011-09-26 14:04:49 +0530727struct dentry *dent_diag;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700728static void fdiag_debugfs_init(void)
729{
Manu Gautamd9f56a12011-09-26 14:04:49 +0530730 dent_diag = debugfs_create_dir("usb_diag", 0);
731 if (IS_ERR(dent_diag))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700732 return;
733
Manu Gautamd9f56a12011-09-26 14:04:49 +0530734 debugfs_create_file("status", 0444, dent_diag, 0, &debug_fdiag_ops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700735}
736#else
737static void fdiag_debugfs_init(void)
738{
739 return;
740}
741#endif
742
743static void diag_cleanup(void)
744{
745 struct diag_context *dev;
746 struct list_head *act, *tmp;
747 struct usb_diag_ch *_ch;
748 unsigned long flags;
749
Manu Gautamd9f56a12011-09-26 14:04:49 +0530750 debugfs_remove_recursive(dent_diag);
751
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700752 list_for_each_safe(act, tmp, &usb_diag_ch_list) {
753 _ch = list_entry(act, struct usb_diag_ch, list);
754 dev = container_of(_ch, struct diag_context, ch);
755
756 spin_lock_irqsave(&ch_lock, flags);
757 /* Free if diagchar is not using the channel anymore */
758 if (!_ch->priv) {
759 list_del(&_ch->list);
760 kfree(dev);
761 }
762 spin_unlock_irqrestore(&ch_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700763 }
764}
765
766static int diag_setup(void)
767{
768 fdiag_debugfs_init();
769
770 return 0;
771}