blob: 3355e19b33085a6232f883ea72ed862050d07f4c [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.
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +02005 * Copyright (c) 2008-2013, The 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 Kondetiac3aa912012-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
Pavankumar Kondeti6f94bc92012-08-03 09:34:32 +053077static struct usb_endpoint_descriptor ss_bulk_in_desc = {
78 .bLength = USB_DT_ENDPOINT_SIZE,
79 .bDescriptorType = USB_DT_ENDPOINT,
80 .bEndpointAddress = USB_DIR_IN,
81 .bmAttributes = USB_ENDPOINT_XFER_BULK,
82 .wMaxPacketSize = __constant_cpu_to_le16(1024),
83};
84
85static struct usb_ss_ep_comp_descriptor ss_bulk_in_comp_desc = {
86 .bLength = sizeof ss_bulk_in_comp_desc,
87 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
88
89 /* the following 2 values can be tweaked if necessary */
90 /* .bMaxBurst = 0, */
91 /* .bmAttributes = 0, */
92};
93
94static struct usb_endpoint_descriptor ss_bulk_out_desc = {
95 .bLength = USB_DT_ENDPOINT_SIZE,
96 .bDescriptorType = USB_DT_ENDPOINT,
97 .bEndpointAddress = USB_DIR_OUT,
98 .bmAttributes = USB_ENDPOINT_XFER_BULK,
99 .wMaxPacketSize = __constant_cpu_to_le16(1024),
100};
101
102static struct usb_ss_ep_comp_descriptor ss_bulk_out_comp_desc = {
103 .bLength = sizeof ss_bulk_out_comp_desc,
104 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
105
106 /* the following 2 values can be tweaked if necessary */
107 /* .bMaxBurst = 0, */
108 /* .bmAttributes = 0, */
109};
110
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700111static struct usb_descriptor_header *fs_diag_desc[] = {
112 (struct usb_descriptor_header *) &intf_desc,
113 (struct usb_descriptor_header *) &fs_bulk_in_desc,
114 (struct usb_descriptor_header *) &fs_bulk_out_desc,
115 NULL,
116 };
117static struct usb_descriptor_header *hs_diag_desc[] = {
118 (struct usb_descriptor_header *) &intf_desc,
119 (struct usb_descriptor_header *) &hs_bulk_in_desc,
120 (struct usb_descriptor_header *) &hs_bulk_out_desc,
121 NULL,
122};
123
Pavankumar Kondeti6f94bc92012-08-03 09:34:32 +0530124static struct usb_descriptor_header *ss_diag_desc[] = {
125 (struct usb_descriptor_header *) &intf_desc,
126 (struct usb_descriptor_header *) &ss_bulk_in_desc,
127 (struct usb_descriptor_header *) &ss_bulk_in_comp_desc,
128 (struct usb_descriptor_header *) &ss_bulk_out_desc,
129 (struct usb_descriptor_header *) &ss_bulk_out_comp_desc,
130 NULL,
131};
132
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700133/**
134 * struct diag_context - USB diag function driver private structure
135 * @function: function structure for USB interface
136 * @out: USB OUT endpoint struct
137 * @in: USB IN endpoint struct
138 * @in_desc: USB IN endpoint descriptor struct
139 * @out_desc: USB OUT endpoint descriptor struct
140 * @read_pool: List of requests used for Rx (OUT ep)
141 * @write_pool: List of requests used for Tx (IN ep)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700142 * @lock: Spinlock to proctect read_pool, write_pool lists
143 * @cdev: USB composite device struct
144 * @ch: USB diag channel
145 *
146 */
147struct diag_context {
148 struct usb_function function;
149 struct usb_ep *out;
150 struct usb_ep *in;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700151 struct list_head read_pool;
152 struct list_head write_pool;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700153 spinlock_t lock;
154 unsigned configured;
155 struct usb_composite_dev *cdev;
156 int (*update_pid_and_serial_num)(uint32_t, const char *);
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200157 struct usb_diag_ch *ch;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700158
159 /* pkt counters */
160 unsigned long dpkts_tolaptop;
161 unsigned long dpkts_tomodem;
162 unsigned dpkts_tolaptop_pending;
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200163
164 /* A list node inside the diag_dev_list */
165 struct list_head list_item;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700166};
167
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200168static struct list_head diag_dev_list;
169
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700170static inline struct diag_context *func_to_diag(struct usb_function *f)
171{
172 return container_of(f, struct diag_context, function);
173}
174
Pavankumar Kondetic3586582013-04-18 11:02:28 +0530175static void diag_update_pid_and_serial_num(struct diag_context *ctxt)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700176{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700177 struct usb_composite_dev *cdev = ctxt->cdev;
178 struct usb_gadget_strings *table;
179 struct usb_string *s;
180
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700181 if (!ctxt->update_pid_and_serial_num)
182 return;
183
Vijayavardhan Vennapusa38775042013-04-18 15:42:38 +0530184 /*
185 * update pid and serail number to dload only if diag
186 * interface is zeroth interface.
187 */
188 if (intf_desc.bInterfaceNumber)
189 return;
190
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700191 /* pass on product id and serial number to dload */
192 if (!cdev->desc.iSerialNumber) {
193 ctxt->update_pid_and_serial_num(
194 cdev->desc.idProduct, 0);
195 return;
196 }
197
198 /*
199 * Serial number is filled by the composite driver. So
200 * it is fair enough to assume that it will always be
201 * found at first table of strings.
202 */
203 table = *(cdev->driver->strings);
204 for (s = table->strings; s && s->s; s++)
205 if (s->id == cdev->desc.iSerialNumber) {
206 ctxt->update_pid_and_serial_num(
207 cdev->desc.idProduct, s->s);
208 break;
209 }
210}
211
212static void diag_write_complete(struct usb_ep *ep,
213 struct usb_request *req)
214{
215 struct diag_context *ctxt = ep->driver_data;
216 struct diag_request *d_req = req->context;
217 unsigned long flags;
218
219 ctxt->dpkts_tolaptop_pending--;
220
221 if (!req->status) {
222 if ((req->length >= ep->maxpacket) &&
223 ((req->length % ep->maxpacket) == 0)) {
224 ctxt->dpkts_tolaptop_pending++;
225 req->length = 0;
226 d_req->actual = req->actual;
227 d_req->status = req->status;
228 /* Queue zero length packet */
229 usb_ep_queue(ctxt->in, req, GFP_ATOMIC);
230 return;
231 }
232 }
233
234 spin_lock_irqsave(&ctxt->lock, flags);
235 list_add_tail(&req->list, &ctxt->write_pool);
236 if (req->length != 0) {
237 d_req->actual = req->actual;
238 d_req->status = req->status;
239 }
240 spin_unlock_irqrestore(&ctxt->lock, flags);
241
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200242 if (ctxt->ch && ctxt->ch->notify)
243 ctxt->ch->notify(ctxt->ch->priv, USB_DIAG_WRITE_DONE, d_req);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700244}
245
246static void diag_read_complete(struct usb_ep *ep,
247 struct usb_request *req)
248{
249 struct diag_context *ctxt = ep->driver_data;
250 struct diag_request *d_req = req->context;
251 unsigned long flags;
252
253 d_req->actual = req->actual;
254 d_req->status = req->status;
255
256 spin_lock_irqsave(&ctxt->lock, flags);
257 list_add_tail(&req->list, &ctxt->read_pool);
258 spin_unlock_irqrestore(&ctxt->lock, flags);
259
260 ctxt->dpkts_tomodem++;
261
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200262 if (ctxt->ch && ctxt->ch->notify)
263 ctxt->ch->notify(ctxt->ch->priv, USB_DIAG_READ_DONE, d_req);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700264}
265
266/**
267 * usb_diag_open() - Open a diag channel over USB
268 * @name: Name of the channel
269 * @priv: Private structure pointer which will be passed in notify()
270 * @notify: Callback function to receive notifications
271 *
272 * This function iterates overs the available channels and returns
273 * the channel handler if the name matches. The notify callback is called
274 * for CONNECT, DISCONNECT, READ_DONE and WRITE_DONE events.
275 *
276 */
277struct usb_diag_ch *usb_diag_open(const char *name, void *priv,
278 void (*notify)(void *, unsigned, struct diag_request *))
279{
280 struct usb_diag_ch *ch;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700281 unsigned long flags;
282 int found = 0;
283
284 spin_lock_irqsave(&ch_lock, flags);
285 /* Check if we already have a channel with this name */
286 list_for_each_entry(ch, &usb_diag_ch_list, list) {
287 if (!strcmp(name, ch->name)) {
288 found = 1;
289 break;
290 }
291 }
292 spin_unlock_irqrestore(&ch_lock, flags);
293
294 if (!found) {
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200295 ch = kzalloc(sizeof(*ch), GFP_KERNEL);
296 if (!ch)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700297 return ERR_PTR(-ENOMEM);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700298 }
299
300 ch->name = name;
301 ch->priv = priv;
302 ch->notify = notify;
303
304 spin_lock_irqsave(&ch_lock, flags);
305 list_add_tail(&ch->list, &usb_diag_ch_list);
306 spin_unlock_irqrestore(&ch_lock, flags);
307
308 return ch;
309}
310EXPORT_SYMBOL(usb_diag_open);
311
312/**
313 * usb_diag_close() - Close a diag channel over USB
314 * @ch: Channel handler
315 *
316 * This function closes the diag channel.
317 *
318 */
319void usb_diag_close(struct usb_diag_ch *ch)
320{
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200321 struct diag_context *dev = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700322 unsigned long flags;
323
324 spin_lock_irqsave(&ch_lock, flags);
325 ch->priv = NULL;
326 ch->notify = NULL;
327 /* Free-up the resources if channel is no more active */
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200328 list_del(&ch->list);
329 list_for_each_entry(dev, &diag_dev_list, list_item)
330 if (dev->ch == ch)
331 dev->ch = NULL;
332 kfree(ch);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700333
334 spin_unlock_irqrestore(&ch_lock, flags);
335}
336EXPORT_SYMBOL(usb_diag_close);
337
Pavankumar Kondetieb1ed0e2013-01-02 14:46:58 +0530338static void free_reqs(struct diag_context *ctxt)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700339{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700340 struct list_head *act, *tmp;
Pavankumar Kondetieb1ed0e2013-01-02 14:46:58 +0530341 struct usb_request *req;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700342
343 list_for_each_safe(act, tmp, &ctxt->write_pool) {
344 req = list_entry(act, struct usb_request, list);
345 list_del(&req->list);
346 usb_ep_free_request(ctxt->in, req);
347 }
348
349 list_for_each_safe(act, tmp, &ctxt->read_pool) {
350 req = list_entry(act, struct usb_request, list);
351 list_del(&req->list);
352 usb_ep_free_request(ctxt->out, req);
353 }
354}
Pavankumar Kondetieb1ed0e2013-01-02 14:46:58 +0530355
356/**
357 * usb_diag_free_req() - Free USB requests
358 * @ch: Channel handler
359 *
360 * This function free read and write USB requests for the interface
361 * associated with this channel.
362 *
363 */
364void usb_diag_free_req(struct usb_diag_ch *ch)
365{
366 struct diag_context *ctxt = ch->priv_usb;
367 unsigned long flags;
368
369 if (ctxt) {
370 spin_lock_irqsave(&ctxt->lock, flags);
371 free_reqs(ctxt);
372 spin_unlock_irqrestore(&ctxt->lock, flags);
373 }
374
375}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700376EXPORT_SYMBOL(usb_diag_free_req);
377
378/**
379 * usb_diag_alloc_req() - Allocate USB requests
380 * @ch: Channel handler
381 * @n_write: Number of requests for Tx
382 * @n_read: Number of requests for Rx
383 *
384 * This function allocate read and write USB requests for the interface
385 * associated with this channel. The actual buffer is not allocated.
386 * The buffer is passed by diag char driver.
387 *
388 */
389int usb_diag_alloc_req(struct usb_diag_ch *ch, int n_write, int n_read)
390{
391 struct diag_context *ctxt = ch->priv_usb;
392 struct usb_request *req;
393 int i;
Pavankumar Kondetieb1ed0e2013-01-02 14:46:58 +0530394 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700395
396 if (!ctxt)
397 return -ENODEV;
398
Pavankumar Kondetieb1ed0e2013-01-02 14:46:58 +0530399 spin_lock_irqsave(&ctxt->lock, flags);
400 /* Free previous session's stale requests */
401 free_reqs(ctxt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700402 for (i = 0; i < n_write; i++) {
403 req = usb_ep_alloc_request(ctxt->in, GFP_ATOMIC);
404 if (!req)
405 goto fail;
406 req->complete = diag_write_complete;
407 list_add_tail(&req->list, &ctxt->write_pool);
408 }
409
410 for (i = 0; i < n_read; i++) {
411 req = usb_ep_alloc_request(ctxt->out, GFP_ATOMIC);
412 if (!req)
413 goto fail;
414 req->complete = diag_read_complete;
415 list_add_tail(&req->list, &ctxt->read_pool);
416 }
Pavankumar Kondetieb1ed0e2013-01-02 14:46:58 +0530417 spin_unlock_irqrestore(&ctxt->lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700418 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700419fail:
Pavankumar Kondetieb1ed0e2013-01-02 14:46:58 +0530420 free_reqs(ctxt);
421 spin_unlock_irqrestore(&ctxt->lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700422 return -ENOMEM;
423
424}
425EXPORT_SYMBOL(usb_diag_alloc_req);
426
427/**
428 * usb_diag_read() - Read data from USB diag channel
429 * @ch: Channel handler
430 * @d_req: Diag request struct
431 *
432 * Enqueue a request on OUT endpoint of the interface corresponding to this
433 * channel. This function returns proper error code when interface is not
434 * in configured state, no Rx requests available and ep queue is failed.
435 *
436 * This function operates asynchronously. READ_DONE event is notified after
437 * completion of OUT request.
438 *
439 */
440int usb_diag_read(struct usb_diag_ch *ch, struct diag_request *d_req)
441{
442 struct diag_context *ctxt = ch->priv_usb;
443 unsigned long flags;
444 struct usb_request *req;
Pavankumar Kondetiac3aa912012-11-26 14:52:37 +0530445 static DEFINE_RATELIMIT_STATE(rl, 10*HZ, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700446
Manu Gautamb33f33a2011-09-09 14:35:57 +0530447 if (!ctxt)
448 return -ENODEV;
449
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700450 spin_lock_irqsave(&ctxt->lock, flags);
451
452 if (!ctxt->configured) {
453 spin_unlock_irqrestore(&ctxt->lock, flags);
454 return -EIO;
455 }
456
457 if (list_empty(&ctxt->read_pool)) {
458 spin_unlock_irqrestore(&ctxt->lock, flags);
459 ERROR(ctxt->cdev, "%s: no requests available\n", __func__);
460 return -EAGAIN;
461 }
462
463 req = list_first_entry(&ctxt->read_pool, struct usb_request, list);
464 list_del(&req->list);
465 spin_unlock_irqrestore(&ctxt->lock, flags);
466
467 req->buf = d_req->buf;
468 req->length = d_req->length;
469 req->context = d_req;
470 if (usb_ep_queue(ctxt->out, req, GFP_ATOMIC)) {
471 /* If error add the link to linked list again*/
472 spin_lock_irqsave(&ctxt->lock, flags);
473 list_add_tail(&req->list, &ctxt->read_pool);
474 spin_unlock_irqrestore(&ctxt->lock, flags);
Pavankumar Kondetiac3aa912012-11-26 14:52:37 +0530475 /* 1 error message for every 10 sec */
476 if (__ratelimit(&rl))
477 ERROR(ctxt->cdev, "%s: cannot queue"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700478 " read request\n", __func__);
479 return -EIO;
480 }
481
482 return 0;
483}
484EXPORT_SYMBOL(usb_diag_read);
485
486/**
487 * usb_diag_write() - Write data from USB diag channel
488 * @ch: Channel handler
489 * @d_req: Diag request struct
490 *
491 * Enqueue a request on IN endpoint of the interface corresponding to this
492 * channel. This function returns proper error code when interface is not
493 * in configured state, no Tx requests available and ep queue is failed.
494 *
495 * This function operates asynchronously. WRITE_DONE event is notified after
496 * completion of IN request.
497 *
498 */
499int usb_diag_write(struct usb_diag_ch *ch, struct diag_request *d_req)
500{
501 struct diag_context *ctxt = ch->priv_usb;
502 unsigned long flags;
503 struct usb_request *req = NULL;
Pavankumar Kondetiac3aa912012-11-26 14:52:37 +0530504 static DEFINE_RATELIMIT_STATE(rl, 10*HZ, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700505
Manu Gautamb33f33a2011-09-09 14:35:57 +0530506 if (!ctxt)
507 return -ENODEV;
508
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700509 spin_lock_irqsave(&ctxt->lock, flags);
510
511 if (!ctxt->configured) {
512 spin_unlock_irqrestore(&ctxt->lock, flags);
513 return -EIO;
514 }
515
516 if (list_empty(&ctxt->write_pool)) {
517 spin_unlock_irqrestore(&ctxt->lock, flags);
518 ERROR(ctxt->cdev, "%s: no requests available\n", __func__);
519 return -EAGAIN;
520 }
521
522 req = list_first_entry(&ctxt->write_pool, struct usb_request, list);
523 list_del(&req->list);
524 spin_unlock_irqrestore(&ctxt->lock, flags);
525
526 req->buf = d_req->buf;
527 req->length = d_req->length;
528 req->context = d_req;
529 if (usb_ep_queue(ctxt->in, req, GFP_ATOMIC)) {
530 /* If error add the link to linked list again*/
531 spin_lock_irqsave(&ctxt->lock, flags);
532 list_add_tail(&req->list, &ctxt->write_pool);
533 spin_unlock_irqrestore(&ctxt->lock, flags);
Pavankumar Kondetiac3aa912012-11-26 14:52:37 +0530534 /* 1 error message for every 10 sec */
535 if (__ratelimit(&rl))
536 ERROR(ctxt->cdev, "%s: cannot queue"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700537 " read request\n", __func__);
538 return -EIO;
539 }
540
541 ctxt->dpkts_tolaptop++;
542 ctxt->dpkts_tolaptop_pending++;
543
544 return 0;
545}
546EXPORT_SYMBOL(usb_diag_write);
547
548static void diag_function_disable(struct usb_function *f)
549{
550 struct diag_context *dev = func_to_diag(f);
551 unsigned long flags;
552
553 DBG(dev->cdev, "diag_function_disable\n");
554
555 spin_lock_irqsave(&dev->lock, flags);
556 dev->configured = 0;
557 spin_unlock_irqrestore(&dev->lock, flags);
558
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200559 if (dev->ch && dev->ch->notify)
560 dev->ch->notify(dev->ch->priv, USB_DIAG_DISCONNECT, NULL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700561
562 usb_ep_disable(dev->in);
563 dev->in->driver_data = NULL;
564
565 usb_ep_disable(dev->out);
566 dev->out->driver_data = NULL;
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200567 if (dev->ch)
568 dev->ch->priv_usb = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700569}
570
571static int diag_function_set_alt(struct usb_function *f,
572 unsigned intf, unsigned alt)
573{
574 struct diag_context *dev = func_to_diag(f);
575 struct usb_composite_dev *cdev = f->config->cdev;
576 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700577 int rc = 0;
578
Tatyana Brokhman31ac3522011-06-28 15:33:50 +0200579 if (config_ep_by_speed(cdev->gadget, f, dev->in) ||
580 config_ep_by_speed(cdev->gadget, f, dev->out)) {
581 dev->in->desc = NULL;
582 dev->out->desc = NULL;
583 return -EINVAL;
584 }
585
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200586 if (!dev->ch)
587 return -ENODEV;
588
589 /*
590 * Indicate to the diag channel that the active diag device is dev.
591 * Since a few diag devices can point to the same channel.
592 */
593 dev->ch->priv_usb = dev;
594
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700595 dev->in->driver_data = dev;
Tatyana Brokhmancf709c12011-06-28 16:33:48 +0300596 rc = usb_ep_enable(dev->in);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700597 if (rc) {
598 ERROR(dev->cdev, "can't enable %s, result %d\n",
599 dev->in->name, rc);
600 return rc;
601 }
602 dev->out->driver_data = dev;
Tatyana Brokhmancf709c12011-06-28 16:33:48 +0300603 rc = usb_ep_enable(dev->out);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700604 if (rc) {
605 ERROR(dev->cdev, "can't enable %s, result %d\n",
606 dev->out->name, rc);
607 usb_ep_disable(dev->in);
608 return rc;
609 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700610
Manu Gautam0c611072011-11-11 17:40:05 +0530611 dev->dpkts_tolaptop = 0;
612 dev->dpkts_tomodem = 0;
613 dev->dpkts_tolaptop_pending = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700614
615 spin_lock_irqsave(&dev->lock, flags);
616 dev->configured = 1;
617 spin_unlock_irqrestore(&dev->lock, flags);
618
Pavankumar Kondetic3586582013-04-18 11:02:28 +0530619 if (dev->ch->notify)
620 dev->ch->notify(dev->ch->priv, USB_DIAG_CONNECT, NULL);
621
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700622 return rc;
623}
624
625static void diag_function_unbind(struct usb_configuration *c,
626 struct usb_function *f)
627{
628 struct diag_context *ctxt = func_to_diag(f);
629
Pavankumar Kondeti6f94bc92012-08-03 09:34:32 +0530630 if (gadget_is_superspeed(c->cdev->gadget))
631 usb_free_descriptors(f->ss_descriptors);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700632 if (gadget_is_dualspeed(c->cdev->gadget))
633 usb_free_descriptors(f->hs_descriptors);
634
635 usb_free_descriptors(f->descriptors);
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200636
637 /*
638 * Channel priv_usb may point to other diag function.
639 * Clear the priv_usb only if the channel is used by the
640 * diag dev we unbind here.
641 */
642 if (ctxt->ch && ctxt->ch->priv_usb == ctxt)
643 ctxt->ch->priv_usb = NULL;
644 list_del(&ctxt->list_item);
645 kfree(ctxt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700646}
647
648static int diag_function_bind(struct usb_configuration *c,
649 struct usb_function *f)
650{
651 struct usb_composite_dev *cdev = c->cdev;
652 struct diag_context *ctxt = func_to_diag(f);
653 struct usb_ep *ep;
654 int status = -ENODEV;
655
656 intf_desc.bInterfaceNumber = usb_interface_id(c, f);
657
658 ep = usb_ep_autoconfig(cdev->gadget, &fs_bulk_in_desc);
Vamsi Krishnae606a7b2011-08-18 12:21:44 -0700659 if (!ep)
660 goto fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700661 ctxt->in = ep;
662 ep->driver_data = ctxt;
663
664 ep = usb_ep_autoconfig(cdev->gadget, &fs_bulk_out_desc);
Vamsi Krishnae606a7b2011-08-18 12:21:44 -0700665 if (!ep)
666 goto fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700667 ctxt->out = ep;
668 ep->driver_data = ctxt;
669
Pavankumar Kondeti6f94bc92012-08-03 09:34:32 +0530670 status = -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700671 /* copy descriptors, and track endpoint copies */
672 f->descriptors = usb_copy_descriptors(fs_diag_desc);
673 if (!f->descriptors)
674 goto fail;
675
676 if (gadget_is_dualspeed(c->cdev->gadget)) {
677 hs_bulk_in_desc.bEndpointAddress =
678 fs_bulk_in_desc.bEndpointAddress;
679 hs_bulk_out_desc.bEndpointAddress =
680 fs_bulk_out_desc.bEndpointAddress;
681
682 /* copy descriptors, and track endpoint copies */
683 f->hs_descriptors = usb_copy_descriptors(hs_diag_desc);
Pavankumar Kondeti6f94bc92012-08-03 09:34:32 +0530684 if (!f->hs_descriptors)
685 goto fail;
686 }
687
688 if (gadget_is_superspeed(c->cdev->gadget)) {
689 ss_bulk_in_desc.bEndpointAddress =
690 fs_bulk_in_desc.bEndpointAddress;
691 ss_bulk_out_desc.bEndpointAddress =
692 fs_bulk_out_desc.bEndpointAddress;
693
694 /* copy descriptors, and track endpoint copies */
695 f->ss_descriptors = usb_copy_descriptors(ss_diag_desc);
696 if (!f->ss_descriptors)
697 goto fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700698 }
Pavankumar Kondetic3586582013-04-18 11:02:28 +0530699 diag_update_pid_and_serial_num(ctxt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700700 return 0;
701fail:
Pavankumar Kondeti6f94bc92012-08-03 09:34:32 +0530702 if (f->ss_descriptors)
703 usb_free_descriptors(f->ss_descriptors);
704 if (f->hs_descriptors)
705 usb_free_descriptors(f->hs_descriptors);
706 if (f->descriptors)
707 usb_free_descriptors(f->descriptors);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700708 if (ctxt->out)
709 ctxt->out->driver_data = NULL;
710 if (ctxt->in)
711 ctxt->in->driver_data = NULL;
712 return status;
713
714}
715
716int diag_function_add(struct usb_configuration *c, const char *name,
717 int (*update_pid)(uint32_t, const char *))
718{
719 struct diag_context *dev;
720 struct usb_diag_ch *_ch;
721 int found = 0, ret;
722
723 DBG(c->cdev, "diag_function_add\n");
724
725 list_for_each_entry(_ch, &usb_diag_ch_list, list) {
726 if (!strcmp(name, _ch->name)) {
727 found = 1;
728 break;
729 }
730 }
731 if (!found) {
732 ERROR(c->cdev, "unable to get diag usb channel\n");
733 return -ENODEV;
734 }
735
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200736 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
737 if (!dev)
738 return -ENOMEM;
739
740 list_add_tail(&dev->list_item, &diag_dev_list);
741
742 /*
743 * A few diag devices can point to the same channel, in case that
744 * the diag devices belong to different configurations, however
745 * only the active diag device will claim the channel by setting
746 * the ch->priv_usb (see diag_function_set_alt).
747 */
748 dev->ch = _ch;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700749
Tatyana Brokhmancf709c12011-06-28 16:33:48 +0300750 dev->update_pid_and_serial_num = update_pid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700751 dev->cdev = c->cdev;
752 dev->function.name = _ch->name;
753 dev->function.descriptors = fs_diag_desc;
754 dev->function.hs_descriptors = hs_diag_desc;
755 dev->function.bind = diag_function_bind;
756 dev->function.unbind = diag_function_unbind;
757 dev->function.set_alt = diag_function_set_alt;
758 dev->function.disable = diag_function_disable;
759 spin_lock_init(&dev->lock);
760 INIT_LIST_HEAD(&dev->read_pool);
761 INIT_LIST_HEAD(&dev->write_pool);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700762
763 ret = usb_add_function(c, &dev->function);
764 if (ret) {
765 INFO(c->cdev, "usb_add_function failed\n");
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200766 list_del(&dev->list_item);
767 kfree(dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700768 }
769
770 return ret;
771}
772
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700773#if defined(CONFIG_DEBUG_FS)
774static char debug_buffer[PAGE_SIZE];
775
776static ssize_t debug_read_stats(struct file *file, char __user *ubuf,
777 size_t count, loff_t *ppos)
778{
779 char *buf = debug_buffer;
780 int temp = 0;
781 struct usb_diag_ch *ch;
782
783 list_for_each_entry(ch, &usb_diag_ch_list, list) {
Jack Pham1c20e3f2012-04-04 19:29:14 -0700784 struct diag_context *ctxt = ch->priv_usb;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700785
Jack Pham1c20e3f2012-04-04 19:29:14 -0700786 if (ctxt)
787 temp += scnprintf(buf + temp, PAGE_SIZE - temp,
788 "---Name: %s---\n"
789 "endpoints: %s, %s\n"
790 "dpkts_tolaptop: %lu\n"
791 "dpkts_tomodem: %lu\n"
792 "pkts_tolaptop_pending: %u\n",
793 ch->name,
794 ctxt->in->name, ctxt->out->name,
795 ctxt->dpkts_tolaptop,
796 ctxt->dpkts_tomodem,
797 ctxt->dpkts_tolaptop_pending);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700798 }
799
800 return simple_read_from_buffer(ubuf, count, ppos, buf, temp);
801}
802
803static ssize_t debug_reset_stats(struct file *file, const char __user *buf,
804 size_t count, loff_t *ppos)
805{
806 struct usb_diag_ch *ch;
807
808 list_for_each_entry(ch, &usb_diag_ch_list, list) {
Jack Pham1c20e3f2012-04-04 19:29:14 -0700809 struct diag_context *ctxt = ch->priv_usb;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700810
Jack Pham1c20e3f2012-04-04 19:29:14 -0700811 if (ctxt) {
812 ctxt->dpkts_tolaptop = 0;
813 ctxt->dpkts_tomodem = 0;
814 ctxt->dpkts_tolaptop_pending = 0;
815 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700816 }
817
818 return count;
819}
820
821static int debug_open(struct inode *inode, struct file *file)
822{
823 return 0;
824}
825
826static const struct file_operations debug_fdiag_ops = {
827 .open = debug_open,
828 .read = debug_read_stats,
829 .write = debug_reset_stats,
830};
831
Manu Gautamd9f56a12011-09-26 14:04:49 +0530832struct dentry *dent_diag;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700833static void fdiag_debugfs_init(void)
834{
Manu Gautamd9f56a12011-09-26 14:04:49 +0530835 dent_diag = debugfs_create_dir("usb_diag", 0);
836 if (IS_ERR(dent_diag))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700837 return;
838
Manu Gautamd9f56a12011-09-26 14:04:49 +0530839 debugfs_create_file("status", 0444, dent_diag, 0, &debug_fdiag_ops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700840}
841#else
842static void fdiag_debugfs_init(void)
843{
844 return;
845}
846#endif
847
848static void diag_cleanup(void)
849{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700850 struct list_head *act, *tmp;
851 struct usb_diag_ch *_ch;
852 unsigned long flags;
853
Manu Gautamd9f56a12011-09-26 14:04:49 +0530854 debugfs_remove_recursive(dent_diag);
855
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700856 list_for_each_safe(act, tmp, &usb_diag_ch_list) {
857 _ch = list_entry(act, struct usb_diag_ch, list);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700858
859 spin_lock_irqsave(&ch_lock, flags);
860 /* Free if diagchar is not using the channel anymore */
861 if (!_ch->priv) {
862 list_del(&_ch->list);
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200863 kfree(_ch);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700864 }
865 spin_unlock_irqrestore(&ch_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700866 }
867}
868
869static int diag_setup(void)
870{
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200871 INIT_LIST_HEAD(&diag_dev_list);
872
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700873 fdiag_debugfs_init();
874
875 return 0;
876}