blob: bccc504b2569f0eba7a6c5efc1db03196639d9f6 [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);
Maria Yu7637ceb2013-07-26 17:49:44 +0800629 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700630
Pavankumar Kondeti6f94bc92012-08-03 09:34:32 +0530631 if (gadget_is_superspeed(c->cdev->gadget))
632 usb_free_descriptors(f->ss_descriptors);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700633 if (gadget_is_dualspeed(c->cdev->gadget))
634 usb_free_descriptors(f->hs_descriptors);
635
636 usb_free_descriptors(f->descriptors);
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200637
638 /*
639 * Channel priv_usb may point to other diag function.
640 * Clear the priv_usb only if the channel is used by the
641 * diag dev we unbind here.
642 */
643 if (ctxt->ch && ctxt->ch->priv_usb == ctxt)
644 ctxt->ch->priv_usb = NULL;
645 list_del(&ctxt->list_item);
Manu Gautam3919cb62013-07-08 15:43:28 +0530646 /* Free any pending USB requests from last session */
Maria Yu7637ceb2013-07-26 17:49:44 +0800647 spin_lock_irqsave(&ctxt->lock, flags);
Manu Gautam3919cb62013-07-08 15:43:28 +0530648 free_reqs(ctxt);
Maria Yu7637ceb2013-07-26 17:49:44 +0800649 spin_unlock_irqrestore(&ctxt->lock, flags);
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200650 kfree(ctxt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700651}
652
653static int diag_function_bind(struct usb_configuration *c,
654 struct usb_function *f)
655{
656 struct usb_composite_dev *cdev = c->cdev;
657 struct diag_context *ctxt = func_to_diag(f);
658 struct usb_ep *ep;
659 int status = -ENODEV;
660
661 intf_desc.bInterfaceNumber = usb_interface_id(c, f);
662
663 ep = usb_ep_autoconfig(cdev->gadget, &fs_bulk_in_desc);
Vamsi Krishnae606a7b2011-08-18 12:21:44 -0700664 if (!ep)
665 goto fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700666 ctxt->in = ep;
667 ep->driver_data = ctxt;
668
669 ep = usb_ep_autoconfig(cdev->gadget, &fs_bulk_out_desc);
Vamsi Krishnae606a7b2011-08-18 12:21:44 -0700670 if (!ep)
671 goto fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700672 ctxt->out = ep;
673 ep->driver_data = ctxt;
674
Pavankumar Kondeti6f94bc92012-08-03 09:34:32 +0530675 status = -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700676 /* copy descriptors, and track endpoint copies */
677 f->descriptors = usb_copy_descriptors(fs_diag_desc);
678 if (!f->descriptors)
679 goto fail;
680
681 if (gadget_is_dualspeed(c->cdev->gadget)) {
682 hs_bulk_in_desc.bEndpointAddress =
683 fs_bulk_in_desc.bEndpointAddress;
684 hs_bulk_out_desc.bEndpointAddress =
685 fs_bulk_out_desc.bEndpointAddress;
686
687 /* copy descriptors, and track endpoint copies */
688 f->hs_descriptors = usb_copy_descriptors(hs_diag_desc);
Pavankumar Kondeti6f94bc92012-08-03 09:34:32 +0530689 if (!f->hs_descriptors)
690 goto fail;
691 }
692
693 if (gadget_is_superspeed(c->cdev->gadget)) {
694 ss_bulk_in_desc.bEndpointAddress =
695 fs_bulk_in_desc.bEndpointAddress;
696 ss_bulk_out_desc.bEndpointAddress =
697 fs_bulk_out_desc.bEndpointAddress;
698
699 /* copy descriptors, and track endpoint copies */
700 f->ss_descriptors = usb_copy_descriptors(ss_diag_desc);
701 if (!f->ss_descriptors)
702 goto fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700703 }
Pavankumar Kondetic3586582013-04-18 11:02:28 +0530704 diag_update_pid_and_serial_num(ctxt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700705 return 0;
706fail:
Pavankumar Kondeti6f94bc92012-08-03 09:34:32 +0530707 if (f->ss_descriptors)
708 usb_free_descriptors(f->ss_descriptors);
709 if (f->hs_descriptors)
710 usb_free_descriptors(f->hs_descriptors);
711 if (f->descriptors)
712 usb_free_descriptors(f->descriptors);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700713 if (ctxt->out)
714 ctxt->out->driver_data = NULL;
715 if (ctxt->in)
716 ctxt->in->driver_data = NULL;
717 return status;
718
719}
720
721int diag_function_add(struct usb_configuration *c, const char *name,
722 int (*update_pid)(uint32_t, const char *))
723{
724 struct diag_context *dev;
725 struct usb_diag_ch *_ch;
726 int found = 0, ret;
727
728 DBG(c->cdev, "diag_function_add\n");
729
730 list_for_each_entry(_ch, &usb_diag_ch_list, list) {
731 if (!strcmp(name, _ch->name)) {
732 found = 1;
733 break;
734 }
735 }
736 if (!found) {
737 ERROR(c->cdev, "unable to get diag usb channel\n");
738 return -ENODEV;
739 }
740
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200741 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
742 if (!dev)
743 return -ENOMEM;
744
745 list_add_tail(&dev->list_item, &diag_dev_list);
746
747 /*
748 * A few diag devices can point to the same channel, in case that
749 * the diag devices belong to different configurations, however
750 * only the active diag device will claim the channel by setting
751 * the ch->priv_usb (see diag_function_set_alt).
752 */
753 dev->ch = _ch;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700754
Tatyana Brokhmancf709c12011-06-28 16:33:48 +0300755 dev->update_pid_and_serial_num = update_pid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700756 dev->cdev = c->cdev;
757 dev->function.name = _ch->name;
758 dev->function.descriptors = fs_diag_desc;
759 dev->function.hs_descriptors = hs_diag_desc;
760 dev->function.bind = diag_function_bind;
761 dev->function.unbind = diag_function_unbind;
762 dev->function.set_alt = diag_function_set_alt;
763 dev->function.disable = diag_function_disable;
764 spin_lock_init(&dev->lock);
765 INIT_LIST_HEAD(&dev->read_pool);
766 INIT_LIST_HEAD(&dev->write_pool);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700767
768 ret = usb_add_function(c, &dev->function);
769 if (ret) {
770 INFO(c->cdev, "usb_add_function failed\n");
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200771 list_del(&dev->list_item);
772 kfree(dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700773 }
774
775 return ret;
776}
777
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700778#if defined(CONFIG_DEBUG_FS)
779static char debug_buffer[PAGE_SIZE];
780
781static ssize_t debug_read_stats(struct file *file, char __user *ubuf,
782 size_t count, loff_t *ppos)
783{
784 char *buf = debug_buffer;
785 int temp = 0;
786 struct usb_diag_ch *ch;
787
788 list_for_each_entry(ch, &usb_diag_ch_list, list) {
Jack Pham1c20e3f2012-04-04 19:29:14 -0700789 struct diag_context *ctxt = ch->priv_usb;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700790
Jack Pham1c20e3f2012-04-04 19:29:14 -0700791 if (ctxt)
792 temp += scnprintf(buf + temp, PAGE_SIZE - temp,
793 "---Name: %s---\n"
794 "endpoints: %s, %s\n"
795 "dpkts_tolaptop: %lu\n"
796 "dpkts_tomodem: %lu\n"
797 "pkts_tolaptop_pending: %u\n",
798 ch->name,
799 ctxt->in->name, ctxt->out->name,
800 ctxt->dpkts_tolaptop,
801 ctxt->dpkts_tomodem,
802 ctxt->dpkts_tolaptop_pending);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700803 }
804
805 return simple_read_from_buffer(ubuf, count, ppos, buf, temp);
806}
807
808static ssize_t debug_reset_stats(struct file *file, const char __user *buf,
809 size_t count, loff_t *ppos)
810{
811 struct usb_diag_ch *ch;
812
813 list_for_each_entry(ch, &usb_diag_ch_list, list) {
Jack Pham1c20e3f2012-04-04 19:29:14 -0700814 struct diag_context *ctxt = ch->priv_usb;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700815
Jack Pham1c20e3f2012-04-04 19:29:14 -0700816 if (ctxt) {
817 ctxt->dpkts_tolaptop = 0;
818 ctxt->dpkts_tomodem = 0;
819 ctxt->dpkts_tolaptop_pending = 0;
820 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700821 }
822
823 return count;
824}
825
826static int debug_open(struct inode *inode, struct file *file)
827{
828 return 0;
829}
830
831static const struct file_operations debug_fdiag_ops = {
832 .open = debug_open,
833 .read = debug_read_stats,
834 .write = debug_reset_stats,
835};
836
Manu Gautamd9f56a12011-09-26 14:04:49 +0530837struct dentry *dent_diag;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700838static void fdiag_debugfs_init(void)
839{
Manu Gautamd9f56a12011-09-26 14:04:49 +0530840 dent_diag = debugfs_create_dir("usb_diag", 0);
841 if (IS_ERR(dent_diag))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700842 return;
843
Manu Gautamd9f56a12011-09-26 14:04:49 +0530844 debugfs_create_file("status", 0444, dent_diag, 0, &debug_fdiag_ops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700845}
846#else
847static void fdiag_debugfs_init(void)
848{
849 return;
850}
851#endif
852
853static void diag_cleanup(void)
854{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700855 struct list_head *act, *tmp;
856 struct usb_diag_ch *_ch;
857 unsigned long flags;
858
Manu Gautamd9f56a12011-09-26 14:04:49 +0530859 debugfs_remove_recursive(dent_diag);
860
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700861 list_for_each_safe(act, tmp, &usb_diag_ch_list) {
862 _ch = list_entry(act, struct usb_diag_ch, list);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700863
864 spin_lock_irqsave(&ch_lock, flags);
865 /* Free if diagchar is not using the channel anymore */
866 if (!_ch->priv) {
867 list_del(&_ch->list);
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200868 kfree(_ch);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700869 }
870 spin_unlock_irqrestore(&ch_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700871 }
872}
873
874static int diag_setup(void)
875{
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200876 INIT_LIST_HEAD(&diag_dev_list);
877
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700878 fdiag_debugfs_init();
879
880 return 0;
881}