blob: a2ee182bce7573b01c9716f9364805111ae38593 [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)
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
Pavankumar Kondetid580a762013-01-02 14:46:58 +0530294static void free_reqs(struct diag_context *ctxt)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700295{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700296 struct list_head *act, *tmp;
Pavankumar Kondetid580a762013-01-02 14:46:58 +0530297 struct usb_request *req;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700298
299 list_for_each_safe(act, tmp, &ctxt->write_pool) {
300 req = list_entry(act, struct usb_request, list);
301 list_del(&req->list);
302 usb_ep_free_request(ctxt->in, req);
303 }
304
305 list_for_each_safe(act, tmp, &ctxt->read_pool) {
306 req = list_entry(act, struct usb_request, list);
307 list_del(&req->list);
308 usb_ep_free_request(ctxt->out, req);
309 }
310}
Pavankumar Kondetid580a762013-01-02 14:46:58 +0530311
312/**
313 * usb_diag_free_req() - Free USB requests
314 * @ch: Channel handler
315 *
316 * This function free read and write USB requests for the interface
317 * associated with this channel.
318 *
319 */
320void usb_diag_free_req(struct usb_diag_ch *ch)
321{
322 struct diag_context *ctxt = ch->priv_usb;
323 unsigned long flags;
324
325 if (ctxt) {
326 spin_lock_irqsave(&ctxt->lock, flags);
327 free_reqs(ctxt);
328 spin_unlock_irqrestore(&ctxt->lock, flags);
329 }
330
331}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700332EXPORT_SYMBOL(usb_diag_free_req);
333
334/**
335 * usb_diag_alloc_req() - Allocate USB requests
336 * @ch: Channel handler
337 * @n_write: Number of requests for Tx
338 * @n_read: Number of requests for Rx
339 *
340 * This function allocate read and write USB requests for the interface
341 * associated with this channel. The actual buffer is not allocated.
342 * The buffer is passed by diag char driver.
343 *
344 */
345int usb_diag_alloc_req(struct usb_diag_ch *ch, int n_write, int n_read)
346{
347 struct diag_context *ctxt = ch->priv_usb;
348 struct usb_request *req;
349 int i;
Pavankumar Kondetid580a762013-01-02 14:46:58 +0530350 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700351
352 if (!ctxt)
353 return -ENODEV;
354
Pavankumar Kondetid580a762013-01-02 14:46:58 +0530355 spin_lock_irqsave(&ctxt->lock, flags);
356 /* Free previous session's stale requests */
357 free_reqs(ctxt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700358 for (i = 0; i < n_write; i++) {
359 req = usb_ep_alloc_request(ctxt->in, GFP_ATOMIC);
360 if (!req)
361 goto fail;
362 req->complete = diag_write_complete;
363 list_add_tail(&req->list, &ctxt->write_pool);
364 }
365
366 for (i = 0; i < n_read; i++) {
367 req = usb_ep_alloc_request(ctxt->out, GFP_ATOMIC);
368 if (!req)
369 goto fail;
370 req->complete = diag_read_complete;
371 list_add_tail(&req->list, &ctxt->read_pool);
372 }
Pavankumar Kondetid580a762013-01-02 14:46:58 +0530373 spin_unlock_irqrestore(&ctxt->lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700374 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700375fail:
Pavankumar Kondetid580a762013-01-02 14:46:58 +0530376 free_reqs(ctxt);
377 spin_unlock_irqrestore(&ctxt->lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700378 return -ENOMEM;
379
380}
381EXPORT_SYMBOL(usb_diag_alloc_req);
382
383/**
384 * usb_diag_read() - Read data from USB diag channel
385 * @ch: Channel handler
386 * @d_req: Diag request struct
387 *
388 * Enqueue a request on OUT endpoint of the interface corresponding to this
389 * channel. This function returns proper error code when interface is not
390 * in configured state, no Rx requests available and ep queue is failed.
391 *
392 * This function operates asynchronously. READ_DONE event is notified after
393 * completion of OUT request.
394 *
395 */
396int usb_diag_read(struct usb_diag_ch *ch, struct diag_request *d_req)
397{
398 struct diag_context *ctxt = ch->priv_usb;
399 unsigned long flags;
400 struct usb_request *req;
Pavankumar Kondeti683077a2012-11-26 14:52:37 +0530401 static DEFINE_RATELIMIT_STATE(rl, 10*HZ, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700402
Manu Gautamb33f33a2011-09-09 14:35:57 +0530403 if (!ctxt)
404 return -ENODEV;
405
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700406 spin_lock_irqsave(&ctxt->lock, flags);
407
408 if (!ctxt->configured) {
409 spin_unlock_irqrestore(&ctxt->lock, flags);
410 return -EIO;
411 }
412
413 if (list_empty(&ctxt->read_pool)) {
414 spin_unlock_irqrestore(&ctxt->lock, flags);
415 ERROR(ctxt->cdev, "%s: no requests available\n", __func__);
416 return -EAGAIN;
417 }
418
419 req = list_first_entry(&ctxt->read_pool, struct usb_request, list);
420 list_del(&req->list);
421 spin_unlock_irqrestore(&ctxt->lock, flags);
422
423 req->buf = d_req->buf;
424 req->length = d_req->length;
425 req->context = d_req;
426 if (usb_ep_queue(ctxt->out, req, GFP_ATOMIC)) {
427 /* If error add the link to linked list again*/
428 spin_lock_irqsave(&ctxt->lock, flags);
429 list_add_tail(&req->list, &ctxt->read_pool);
430 spin_unlock_irqrestore(&ctxt->lock, flags);
Pavankumar Kondeti683077a2012-11-26 14:52:37 +0530431 /* 1 error message for every 10 sec */
432 if (__ratelimit(&rl))
433 ERROR(ctxt->cdev, "%s: cannot queue"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700434 " read request\n", __func__);
435 return -EIO;
436 }
437
438 return 0;
439}
440EXPORT_SYMBOL(usb_diag_read);
441
442/**
443 * usb_diag_write() - Write data from USB diag channel
444 * @ch: Channel handler
445 * @d_req: Diag request struct
446 *
447 * Enqueue a request on IN endpoint of the interface corresponding to this
448 * channel. This function returns proper error code when interface is not
449 * in configured state, no Tx requests available and ep queue is failed.
450 *
451 * This function operates asynchronously. WRITE_DONE event is notified after
452 * completion of IN request.
453 *
454 */
455int usb_diag_write(struct usb_diag_ch *ch, struct diag_request *d_req)
456{
457 struct diag_context *ctxt = ch->priv_usb;
458 unsigned long flags;
459 struct usb_request *req = NULL;
Pavankumar Kondeti683077a2012-11-26 14:52:37 +0530460 static DEFINE_RATELIMIT_STATE(rl, 10*HZ, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700461
Manu Gautamb33f33a2011-09-09 14:35:57 +0530462 if (!ctxt)
463 return -ENODEV;
464
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700465 spin_lock_irqsave(&ctxt->lock, flags);
466
467 if (!ctxt->configured) {
468 spin_unlock_irqrestore(&ctxt->lock, flags);
469 return -EIO;
470 }
471
472 if (list_empty(&ctxt->write_pool)) {
473 spin_unlock_irqrestore(&ctxt->lock, flags);
474 ERROR(ctxt->cdev, "%s: no requests available\n", __func__);
475 return -EAGAIN;
476 }
477
478 req = list_first_entry(&ctxt->write_pool, struct usb_request, list);
479 list_del(&req->list);
480 spin_unlock_irqrestore(&ctxt->lock, flags);
481
482 req->buf = d_req->buf;
483 req->length = d_req->length;
484 req->context = d_req;
485 if (usb_ep_queue(ctxt->in, req, GFP_ATOMIC)) {
486 /* If error add the link to linked list again*/
487 spin_lock_irqsave(&ctxt->lock, flags);
488 list_add_tail(&req->list, &ctxt->write_pool);
489 spin_unlock_irqrestore(&ctxt->lock, flags);
Pavankumar Kondeti683077a2012-11-26 14:52:37 +0530490 /* 1 error message for every 10 sec */
491 if (__ratelimit(&rl))
492 ERROR(ctxt->cdev, "%s: cannot queue"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700493 " read request\n", __func__);
494 return -EIO;
495 }
496
497 ctxt->dpkts_tolaptop++;
498 ctxt->dpkts_tolaptop_pending++;
499
500 return 0;
501}
502EXPORT_SYMBOL(usb_diag_write);
503
504static void diag_function_disable(struct usb_function *f)
505{
506 struct diag_context *dev = func_to_diag(f);
507 unsigned long flags;
508
509 DBG(dev->cdev, "diag_function_disable\n");
510
511 spin_lock_irqsave(&dev->lock, flags);
512 dev->configured = 0;
513 spin_unlock_irqrestore(&dev->lock, flags);
514
515 if (dev->ch.notify)
516 dev->ch.notify(dev->ch.priv, USB_DIAG_DISCONNECT, NULL);
517
518 usb_ep_disable(dev->in);
519 dev->in->driver_data = NULL;
520
521 usb_ep_disable(dev->out);
522 dev->out->driver_data = NULL;
523
524}
525
526static int diag_function_set_alt(struct usb_function *f,
527 unsigned intf, unsigned alt)
528{
529 struct diag_context *dev = func_to_diag(f);
530 struct usb_composite_dev *cdev = f->config->cdev;
531 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700532 int rc = 0;
533
Tatyana Brokhman31ac3522011-06-28 15:33:50 +0200534 if (config_ep_by_speed(cdev->gadget, f, dev->in) ||
535 config_ep_by_speed(cdev->gadget, f, dev->out)) {
536 dev->in->desc = NULL;
537 dev->out->desc = NULL;
538 return -EINVAL;
539 }
540
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700541 dev->in->driver_data = dev;
Tatyana Brokhmancf709c12011-06-28 16:33:48 +0300542 rc = usb_ep_enable(dev->in);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700543 if (rc) {
544 ERROR(dev->cdev, "can't enable %s, result %d\n",
545 dev->in->name, rc);
546 return rc;
547 }
548 dev->out->driver_data = dev;
Tatyana Brokhmancf709c12011-06-28 16:33:48 +0300549 rc = usb_ep_enable(dev->out);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700550 if (rc) {
551 ERROR(dev->cdev, "can't enable %s, result %d\n",
552 dev->out->name, rc);
553 usb_ep_disable(dev->in);
554 return rc;
555 }
556 schedule_work(&dev->config_work);
557
Manu Gautam0c611072011-11-11 17:40:05 +0530558 dev->dpkts_tolaptop = 0;
559 dev->dpkts_tomodem = 0;
560 dev->dpkts_tolaptop_pending = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700561
562 spin_lock_irqsave(&dev->lock, flags);
563 dev->configured = 1;
564 spin_unlock_irqrestore(&dev->lock, flags);
565
566 return rc;
567}
568
569static void diag_function_unbind(struct usb_configuration *c,
570 struct usb_function *f)
571{
572 struct diag_context *ctxt = func_to_diag(f);
573
574 if (gadget_is_dualspeed(c->cdev->gadget))
575 usb_free_descriptors(f->hs_descriptors);
576
577 usb_free_descriptors(f->descriptors);
578 ctxt->ch.priv_usb = NULL;
579}
580
581static int diag_function_bind(struct usb_configuration *c,
582 struct usb_function *f)
583{
584 struct usb_composite_dev *cdev = c->cdev;
585 struct diag_context *ctxt = func_to_diag(f);
586 struct usb_ep *ep;
587 int status = -ENODEV;
588
589 intf_desc.bInterfaceNumber = usb_interface_id(c, f);
590
591 ep = usb_ep_autoconfig(cdev->gadget, &fs_bulk_in_desc);
Vamsi Krishnae606a7b2011-08-18 12:21:44 -0700592 if (!ep)
593 goto fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700594 ctxt->in = ep;
595 ep->driver_data = ctxt;
596
597 ep = usb_ep_autoconfig(cdev->gadget, &fs_bulk_out_desc);
Vamsi Krishnae606a7b2011-08-18 12:21:44 -0700598 if (!ep)
599 goto fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700600 ctxt->out = ep;
601 ep->driver_data = ctxt;
602
603 /* copy descriptors, and track endpoint copies */
604 f->descriptors = usb_copy_descriptors(fs_diag_desc);
605 if (!f->descriptors)
606 goto fail;
607
608 if (gadget_is_dualspeed(c->cdev->gadget)) {
609 hs_bulk_in_desc.bEndpointAddress =
610 fs_bulk_in_desc.bEndpointAddress;
611 hs_bulk_out_desc.bEndpointAddress =
612 fs_bulk_out_desc.bEndpointAddress;
613
614 /* copy descriptors, and track endpoint copies */
615 f->hs_descriptors = usb_copy_descriptors(hs_diag_desc);
616 }
617 return 0;
618fail:
619 if (ctxt->out)
620 ctxt->out->driver_data = NULL;
621 if (ctxt->in)
622 ctxt->in->driver_data = NULL;
623 return status;
624
625}
626
627int diag_function_add(struct usb_configuration *c, const char *name,
628 int (*update_pid)(uint32_t, const char *))
629{
630 struct diag_context *dev;
631 struct usb_diag_ch *_ch;
632 int found = 0, ret;
633
634 DBG(c->cdev, "diag_function_add\n");
635
636 list_for_each_entry(_ch, &usb_diag_ch_list, list) {
637 if (!strcmp(name, _ch->name)) {
638 found = 1;
639 break;
640 }
641 }
642 if (!found) {
643 ERROR(c->cdev, "unable to get diag usb channel\n");
644 return -ENODEV;
645 }
646
647 dev = container_of(_ch, struct diag_context, ch);
648 /* claim the channel for this USB interface */
649 _ch->priv_usb = dev;
650
Tatyana Brokhmancf709c12011-06-28 16:33:48 +0300651 dev->update_pid_and_serial_num = update_pid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700652 dev->cdev = c->cdev;
653 dev->function.name = _ch->name;
654 dev->function.descriptors = fs_diag_desc;
655 dev->function.hs_descriptors = hs_diag_desc;
656 dev->function.bind = diag_function_bind;
657 dev->function.unbind = diag_function_unbind;
658 dev->function.set_alt = diag_function_set_alt;
659 dev->function.disable = diag_function_disable;
660 spin_lock_init(&dev->lock);
661 INIT_LIST_HEAD(&dev->read_pool);
662 INIT_LIST_HEAD(&dev->write_pool);
663 INIT_WORK(&dev->config_work, usb_config_work_func);
664
665 ret = usb_add_function(c, &dev->function);
666 if (ret) {
667 INFO(c->cdev, "usb_add_function failed\n");
668 _ch->priv_usb = NULL;
669 }
670
671 return ret;
672}
673
674
675#if defined(CONFIG_DEBUG_FS)
676static char debug_buffer[PAGE_SIZE];
677
678static ssize_t debug_read_stats(struct file *file, char __user *ubuf,
679 size_t count, loff_t *ppos)
680{
681 char *buf = debug_buffer;
682 int temp = 0;
683 struct usb_diag_ch *ch;
684
685 list_for_each_entry(ch, &usb_diag_ch_list, list) {
Jack Pham1c20e3f2012-04-04 19:29:14 -0700686 struct diag_context *ctxt = ch->priv_usb;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700687
Jack Pham1c20e3f2012-04-04 19:29:14 -0700688 if (ctxt)
689 temp += scnprintf(buf + temp, PAGE_SIZE - temp,
690 "---Name: %s---\n"
691 "endpoints: %s, %s\n"
692 "dpkts_tolaptop: %lu\n"
693 "dpkts_tomodem: %lu\n"
694 "pkts_tolaptop_pending: %u\n",
695 ch->name,
696 ctxt->in->name, ctxt->out->name,
697 ctxt->dpkts_tolaptop,
698 ctxt->dpkts_tomodem,
699 ctxt->dpkts_tolaptop_pending);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700700 }
701
702 return simple_read_from_buffer(ubuf, count, ppos, buf, temp);
703}
704
705static ssize_t debug_reset_stats(struct file *file, const char __user *buf,
706 size_t count, loff_t *ppos)
707{
708 struct usb_diag_ch *ch;
709
710 list_for_each_entry(ch, &usb_diag_ch_list, list) {
Jack Pham1c20e3f2012-04-04 19:29:14 -0700711 struct diag_context *ctxt = ch->priv_usb;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700712
Jack Pham1c20e3f2012-04-04 19:29:14 -0700713 if (ctxt) {
714 ctxt->dpkts_tolaptop = 0;
715 ctxt->dpkts_tomodem = 0;
716 ctxt->dpkts_tolaptop_pending = 0;
717 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700718 }
719
720 return count;
721}
722
723static int debug_open(struct inode *inode, struct file *file)
724{
725 return 0;
726}
727
728static const struct file_operations debug_fdiag_ops = {
729 .open = debug_open,
730 .read = debug_read_stats,
731 .write = debug_reset_stats,
732};
733
Manu Gautamd9f56a12011-09-26 14:04:49 +0530734struct dentry *dent_diag;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700735static void fdiag_debugfs_init(void)
736{
Manu Gautamd9f56a12011-09-26 14:04:49 +0530737 dent_diag = debugfs_create_dir("usb_diag", 0);
738 if (IS_ERR(dent_diag))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700739 return;
740
Manu Gautamd9f56a12011-09-26 14:04:49 +0530741 debugfs_create_file("status", 0444, dent_diag, 0, &debug_fdiag_ops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700742}
743#else
744static void fdiag_debugfs_init(void)
745{
746 return;
747}
748#endif
749
750static void diag_cleanup(void)
751{
752 struct diag_context *dev;
753 struct list_head *act, *tmp;
754 struct usb_diag_ch *_ch;
755 unsigned long flags;
756
Manu Gautamd9f56a12011-09-26 14:04:49 +0530757 debugfs_remove_recursive(dent_diag);
758
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700759 list_for_each_safe(act, tmp, &usb_diag_ch_list) {
760 _ch = list_entry(act, struct usb_diag_ch, list);
761 dev = container_of(_ch, struct diag_context, ch);
762
763 spin_lock_irqsave(&ch_lock, flags);
764 /* Free if diagchar is not using the channel anymore */
765 if (!_ch->priv) {
766 list_del(&_ch->list);
767 kfree(dev);
768 }
769 spin_unlock_irqrestore(&ch_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700770 }
771}
772
773static int diag_setup(void)
774{
775 fdiag_debugfs_init();
776
777 return 0;
778}