blob: 3b1843b061ba359c869a8dd28defa7a4926b350e [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)
142 * @config_work: Work item schedule after interface is configured to notify
143 * CONNECT event to diag char driver and updating product id
144 * and serial number to MODEM/IMEM.
145 * @lock: Spinlock to proctect read_pool, write_pool lists
146 * @cdev: USB composite device struct
147 * @ch: USB diag channel
148 *
149 */
150struct diag_context {
151 struct usb_function function;
152 struct usb_ep *out;
153 struct usb_ep *in;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700154 struct list_head read_pool;
155 struct list_head write_pool;
156 struct work_struct config_work;
157 spinlock_t lock;
158 unsigned configured;
159 struct usb_composite_dev *cdev;
160 int (*update_pid_and_serial_num)(uint32_t, const char *);
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200161 struct usb_diag_ch *ch;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700162
163 /* pkt counters */
164 unsigned long dpkts_tolaptop;
165 unsigned long dpkts_tomodem;
166 unsigned dpkts_tolaptop_pending;
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200167
168 /* A list node inside the diag_dev_list */
169 struct list_head list_item;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700170};
171
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200172static struct list_head diag_dev_list;
173
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700174static inline struct diag_context *func_to_diag(struct usb_function *f)
175{
176 return container_of(f, struct diag_context, function);
177}
178
179static void usb_config_work_func(struct work_struct *work)
180{
181 struct diag_context *ctxt = container_of(work,
182 struct diag_context, config_work);
183 struct usb_composite_dev *cdev = ctxt->cdev;
184 struct usb_gadget_strings *table;
185 struct usb_string *s;
186
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200187 if (!ctxt->ch)
188 return;
189
190 if (ctxt->ch->notify)
191 ctxt->ch->notify(ctxt->ch->priv, USB_DIAG_CONNECT, NULL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700192
193 if (!ctxt->update_pid_and_serial_num)
194 return;
195
196 /* pass on product id and serial number to dload */
197 if (!cdev->desc.iSerialNumber) {
198 ctxt->update_pid_and_serial_num(
199 cdev->desc.idProduct, 0);
200 return;
201 }
202
203 /*
204 * Serial number is filled by the composite driver. So
205 * it is fair enough to assume that it will always be
206 * found at first table of strings.
207 */
208 table = *(cdev->driver->strings);
209 for (s = table->strings; s && s->s; s++)
210 if (s->id == cdev->desc.iSerialNumber) {
211 ctxt->update_pid_and_serial_num(
212 cdev->desc.idProduct, s->s);
213 break;
214 }
215}
216
217static void diag_write_complete(struct usb_ep *ep,
218 struct usb_request *req)
219{
220 struct diag_context *ctxt = ep->driver_data;
221 struct diag_request *d_req = req->context;
222 unsigned long flags;
223
224 ctxt->dpkts_tolaptop_pending--;
225
226 if (!req->status) {
227 if ((req->length >= ep->maxpacket) &&
228 ((req->length % ep->maxpacket) == 0)) {
229 ctxt->dpkts_tolaptop_pending++;
230 req->length = 0;
231 d_req->actual = req->actual;
232 d_req->status = req->status;
233 /* Queue zero length packet */
234 usb_ep_queue(ctxt->in, req, GFP_ATOMIC);
235 return;
236 }
237 }
238
239 spin_lock_irqsave(&ctxt->lock, flags);
240 list_add_tail(&req->list, &ctxt->write_pool);
241 if (req->length != 0) {
242 d_req->actual = req->actual;
243 d_req->status = req->status;
244 }
245 spin_unlock_irqrestore(&ctxt->lock, flags);
246
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200247 if (ctxt->ch && ctxt->ch->notify)
248 ctxt->ch->notify(ctxt->ch->priv, USB_DIAG_WRITE_DONE, d_req);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700249}
250
251static void diag_read_complete(struct usb_ep *ep,
252 struct usb_request *req)
253{
254 struct diag_context *ctxt = ep->driver_data;
255 struct diag_request *d_req = req->context;
256 unsigned long flags;
257
258 d_req->actual = req->actual;
259 d_req->status = req->status;
260
261 spin_lock_irqsave(&ctxt->lock, flags);
262 list_add_tail(&req->list, &ctxt->read_pool);
263 spin_unlock_irqrestore(&ctxt->lock, flags);
264
265 ctxt->dpkts_tomodem++;
266
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200267 if (ctxt->ch && ctxt->ch->notify)
268 ctxt->ch->notify(ctxt->ch->priv, USB_DIAG_READ_DONE, d_req);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700269}
270
271/**
272 * usb_diag_open() - Open a diag channel over USB
273 * @name: Name of the channel
274 * @priv: Private structure pointer which will be passed in notify()
275 * @notify: Callback function to receive notifications
276 *
277 * This function iterates overs the available channels and returns
278 * the channel handler if the name matches. The notify callback is called
279 * for CONNECT, DISCONNECT, READ_DONE and WRITE_DONE events.
280 *
281 */
282struct usb_diag_ch *usb_diag_open(const char *name, void *priv,
283 void (*notify)(void *, unsigned, struct diag_request *))
284{
285 struct usb_diag_ch *ch;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700286 unsigned long flags;
287 int found = 0;
288
289 spin_lock_irqsave(&ch_lock, flags);
290 /* Check if we already have a channel with this name */
291 list_for_each_entry(ch, &usb_diag_ch_list, list) {
292 if (!strcmp(name, ch->name)) {
293 found = 1;
294 break;
295 }
296 }
297 spin_unlock_irqrestore(&ch_lock, flags);
298
299 if (!found) {
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200300 ch = kzalloc(sizeof(*ch), GFP_KERNEL);
301 if (!ch)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700302 return ERR_PTR(-ENOMEM);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700303 }
304
305 ch->name = name;
306 ch->priv = priv;
307 ch->notify = notify;
308
309 spin_lock_irqsave(&ch_lock, flags);
310 list_add_tail(&ch->list, &usb_diag_ch_list);
311 spin_unlock_irqrestore(&ch_lock, flags);
312
313 return ch;
314}
315EXPORT_SYMBOL(usb_diag_open);
316
317/**
318 * usb_diag_close() - Close a diag channel over USB
319 * @ch: Channel handler
320 *
321 * This function closes the diag channel.
322 *
323 */
324void usb_diag_close(struct usb_diag_ch *ch)
325{
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200326 struct diag_context *dev = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700327 unsigned long flags;
328
329 spin_lock_irqsave(&ch_lock, flags);
330 ch->priv = NULL;
331 ch->notify = NULL;
332 /* Free-up the resources if channel is no more active */
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200333 list_del(&ch->list);
334 list_for_each_entry(dev, &diag_dev_list, list_item)
335 if (dev->ch == ch)
336 dev->ch = NULL;
337 kfree(ch);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700338
339 spin_unlock_irqrestore(&ch_lock, flags);
340}
341EXPORT_SYMBOL(usb_diag_close);
342
Pavankumar Kondetieb1ed0e2013-01-02 14:46:58 +0530343static void free_reqs(struct diag_context *ctxt)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700344{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700345 struct list_head *act, *tmp;
Pavankumar Kondetieb1ed0e2013-01-02 14:46:58 +0530346 struct usb_request *req;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700347
348 list_for_each_safe(act, tmp, &ctxt->write_pool) {
349 req = list_entry(act, struct usb_request, list);
350 list_del(&req->list);
351 usb_ep_free_request(ctxt->in, req);
352 }
353
354 list_for_each_safe(act, tmp, &ctxt->read_pool) {
355 req = list_entry(act, struct usb_request, list);
356 list_del(&req->list);
357 usb_ep_free_request(ctxt->out, req);
358 }
359}
Pavankumar Kondetieb1ed0e2013-01-02 14:46:58 +0530360
361/**
362 * usb_diag_free_req() - Free USB requests
363 * @ch: Channel handler
364 *
365 * This function free read and write USB requests for the interface
366 * associated with this channel.
367 *
368 */
369void usb_diag_free_req(struct usb_diag_ch *ch)
370{
371 struct diag_context *ctxt = ch->priv_usb;
372 unsigned long flags;
373
374 if (ctxt) {
375 spin_lock_irqsave(&ctxt->lock, flags);
376 free_reqs(ctxt);
377 spin_unlock_irqrestore(&ctxt->lock, flags);
378 }
379
380}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700381EXPORT_SYMBOL(usb_diag_free_req);
382
383/**
384 * usb_diag_alloc_req() - Allocate USB requests
385 * @ch: Channel handler
386 * @n_write: Number of requests for Tx
387 * @n_read: Number of requests for Rx
388 *
389 * This function allocate read and write USB requests for the interface
390 * associated with this channel. The actual buffer is not allocated.
391 * The buffer is passed by diag char driver.
392 *
393 */
394int usb_diag_alloc_req(struct usb_diag_ch *ch, int n_write, int n_read)
395{
396 struct diag_context *ctxt = ch->priv_usb;
397 struct usb_request *req;
398 int i;
Pavankumar Kondetieb1ed0e2013-01-02 14:46:58 +0530399 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700400
401 if (!ctxt)
402 return -ENODEV;
403
Pavankumar Kondetieb1ed0e2013-01-02 14:46:58 +0530404 spin_lock_irqsave(&ctxt->lock, flags);
405 /* Free previous session's stale requests */
406 free_reqs(ctxt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700407 for (i = 0; i < n_write; i++) {
408 req = usb_ep_alloc_request(ctxt->in, GFP_ATOMIC);
409 if (!req)
410 goto fail;
411 req->complete = diag_write_complete;
412 list_add_tail(&req->list, &ctxt->write_pool);
413 }
414
415 for (i = 0; i < n_read; i++) {
416 req = usb_ep_alloc_request(ctxt->out, GFP_ATOMIC);
417 if (!req)
418 goto fail;
419 req->complete = diag_read_complete;
420 list_add_tail(&req->list, &ctxt->read_pool);
421 }
Pavankumar Kondetieb1ed0e2013-01-02 14:46:58 +0530422 spin_unlock_irqrestore(&ctxt->lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700423 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700424fail:
Pavankumar Kondetieb1ed0e2013-01-02 14:46:58 +0530425 free_reqs(ctxt);
426 spin_unlock_irqrestore(&ctxt->lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700427 return -ENOMEM;
428
429}
430EXPORT_SYMBOL(usb_diag_alloc_req);
431
432/**
433 * usb_diag_read() - Read data from USB diag channel
434 * @ch: Channel handler
435 * @d_req: Diag request struct
436 *
437 * Enqueue a request on OUT endpoint of the interface corresponding to this
438 * channel. This function returns proper error code when interface is not
439 * in configured state, no Rx requests available and ep queue is failed.
440 *
441 * This function operates asynchronously. READ_DONE event is notified after
442 * completion of OUT request.
443 *
444 */
445int usb_diag_read(struct usb_diag_ch *ch, struct diag_request *d_req)
446{
447 struct diag_context *ctxt = ch->priv_usb;
448 unsigned long flags;
449 struct usb_request *req;
Pavankumar Kondetiac3aa912012-11-26 14:52:37 +0530450 static DEFINE_RATELIMIT_STATE(rl, 10*HZ, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700451
Manu Gautamb33f33a2011-09-09 14:35:57 +0530452 if (!ctxt)
453 return -ENODEV;
454
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700455 spin_lock_irqsave(&ctxt->lock, flags);
456
457 if (!ctxt->configured) {
458 spin_unlock_irqrestore(&ctxt->lock, flags);
459 return -EIO;
460 }
461
462 if (list_empty(&ctxt->read_pool)) {
463 spin_unlock_irqrestore(&ctxt->lock, flags);
464 ERROR(ctxt->cdev, "%s: no requests available\n", __func__);
465 return -EAGAIN;
466 }
467
468 req = list_first_entry(&ctxt->read_pool, struct usb_request, list);
469 list_del(&req->list);
470 spin_unlock_irqrestore(&ctxt->lock, flags);
471
472 req->buf = d_req->buf;
473 req->length = d_req->length;
474 req->context = d_req;
475 if (usb_ep_queue(ctxt->out, req, GFP_ATOMIC)) {
476 /* If error add the link to linked list again*/
477 spin_lock_irqsave(&ctxt->lock, flags);
478 list_add_tail(&req->list, &ctxt->read_pool);
479 spin_unlock_irqrestore(&ctxt->lock, flags);
Pavankumar Kondetiac3aa912012-11-26 14:52:37 +0530480 /* 1 error message for every 10 sec */
481 if (__ratelimit(&rl))
482 ERROR(ctxt->cdev, "%s: cannot queue"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700483 " read request\n", __func__);
484 return -EIO;
485 }
486
487 return 0;
488}
489EXPORT_SYMBOL(usb_diag_read);
490
491/**
492 * usb_diag_write() - Write data from USB diag channel
493 * @ch: Channel handler
494 * @d_req: Diag request struct
495 *
496 * Enqueue a request on IN endpoint of the interface corresponding to this
497 * channel. This function returns proper error code when interface is not
498 * in configured state, no Tx requests available and ep queue is failed.
499 *
500 * This function operates asynchronously. WRITE_DONE event is notified after
501 * completion of IN request.
502 *
503 */
504int usb_diag_write(struct usb_diag_ch *ch, struct diag_request *d_req)
505{
506 struct diag_context *ctxt = ch->priv_usb;
507 unsigned long flags;
508 struct usb_request *req = NULL;
Pavankumar Kondetiac3aa912012-11-26 14:52:37 +0530509 static DEFINE_RATELIMIT_STATE(rl, 10*HZ, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700510
Manu Gautamb33f33a2011-09-09 14:35:57 +0530511 if (!ctxt)
512 return -ENODEV;
513
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700514 spin_lock_irqsave(&ctxt->lock, flags);
515
516 if (!ctxt->configured) {
517 spin_unlock_irqrestore(&ctxt->lock, flags);
518 return -EIO;
519 }
520
521 if (list_empty(&ctxt->write_pool)) {
522 spin_unlock_irqrestore(&ctxt->lock, flags);
523 ERROR(ctxt->cdev, "%s: no requests available\n", __func__);
524 return -EAGAIN;
525 }
526
527 req = list_first_entry(&ctxt->write_pool, struct usb_request, list);
528 list_del(&req->list);
529 spin_unlock_irqrestore(&ctxt->lock, flags);
530
531 req->buf = d_req->buf;
532 req->length = d_req->length;
533 req->context = d_req;
534 if (usb_ep_queue(ctxt->in, req, GFP_ATOMIC)) {
535 /* If error add the link to linked list again*/
536 spin_lock_irqsave(&ctxt->lock, flags);
537 list_add_tail(&req->list, &ctxt->write_pool);
538 spin_unlock_irqrestore(&ctxt->lock, flags);
Pavankumar Kondetiac3aa912012-11-26 14:52:37 +0530539 /* 1 error message for every 10 sec */
540 if (__ratelimit(&rl))
541 ERROR(ctxt->cdev, "%s: cannot queue"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700542 " read request\n", __func__);
543 return -EIO;
544 }
545
546 ctxt->dpkts_tolaptop++;
547 ctxt->dpkts_tolaptop_pending++;
548
549 return 0;
550}
551EXPORT_SYMBOL(usb_diag_write);
552
553static void diag_function_disable(struct usb_function *f)
554{
555 struct diag_context *dev = func_to_diag(f);
556 unsigned long flags;
557
558 DBG(dev->cdev, "diag_function_disable\n");
559
560 spin_lock_irqsave(&dev->lock, flags);
561 dev->configured = 0;
562 spin_unlock_irqrestore(&dev->lock, flags);
563
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200564 if (dev->ch && dev->ch->notify)
565 dev->ch->notify(dev->ch->priv, USB_DIAG_DISCONNECT, NULL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700566
567 usb_ep_disable(dev->in);
568 dev->in->driver_data = NULL;
569
570 usb_ep_disable(dev->out);
571 dev->out->driver_data = NULL;
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200572 if (dev->ch)
573 dev->ch->priv_usb = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700574}
575
576static int diag_function_set_alt(struct usb_function *f,
577 unsigned intf, unsigned alt)
578{
579 struct diag_context *dev = func_to_diag(f);
580 struct usb_composite_dev *cdev = f->config->cdev;
581 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700582 int rc = 0;
583
Tatyana Brokhman31ac3522011-06-28 15:33:50 +0200584 if (config_ep_by_speed(cdev->gadget, f, dev->in) ||
585 config_ep_by_speed(cdev->gadget, f, dev->out)) {
586 dev->in->desc = NULL;
587 dev->out->desc = NULL;
588 return -EINVAL;
589 }
590
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200591 if (!dev->ch)
592 return -ENODEV;
593
594 /*
595 * Indicate to the diag channel that the active diag device is dev.
596 * Since a few diag devices can point to the same channel.
597 */
598 dev->ch->priv_usb = dev;
599
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700600 dev->in->driver_data = dev;
Tatyana Brokhmancf709c12011-06-28 16:33:48 +0300601 rc = usb_ep_enable(dev->in);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700602 if (rc) {
603 ERROR(dev->cdev, "can't enable %s, result %d\n",
604 dev->in->name, rc);
605 return rc;
606 }
607 dev->out->driver_data = dev;
Tatyana Brokhmancf709c12011-06-28 16:33:48 +0300608 rc = usb_ep_enable(dev->out);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700609 if (rc) {
610 ERROR(dev->cdev, "can't enable %s, result %d\n",
611 dev->out->name, rc);
612 usb_ep_disable(dev->in);
613 return rc;
614 }
615 schedule_work(&dev->config_work);
616
Manu Gautam0c611072011-11-11 17:40:05 +0530617 dev->dpkts_tolaptop = 0;
618 dev->dpkts_tomodem = 0;
619 dev->dpkts_tolaptop_pending = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700620
621 spin_lock_irqsave(&dev->lock, flags);
622 dev->configured = 1;
623 spin_unlock_irqrestore(&dev->lock, flags);
624
625 return rc;
626}
627
628static void diag_function_unbind(struct usb_configuration *c,
629 struct usb_function *f)
630{
631 struct diag_context *ctxt = func_to_diag(f);
632
Pavankumar Kondeti6f94bc92012-08-03 09:34:32 +0530633 if (gadget_is_superspeed(c->cdev->gadget))
634 usb_free_descriptors(f->ss_descriptors);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700635 if (gadget_is_dualspeed(c->cdev->gadget))
636 usb_free_descriptors(f->hs_descriptors);
637
638 usb_free_descriptors(f->descriptors);
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200639
640 /*
641 * Channel priv_usb may point to other diag function.
642 * Clear the priv_usb only if the channel is used by the
643 * diag dev we unbind here.
644 */
645 if (ctxt->ch && ctxt->ch->priv_usb == ctxt)
646 ctxt->ch->priv_usb = NULL;
647 list_del(&ctxt->list_item);
648 kfree(ctxt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700649}
650
651static int diag_function_bind(struct usb_configuration *c,
652 struct usb_function *f)
653{
654 struct usb_composite_dev *cdev = c->cdev;
655 struct diag_context *ctxt = func_to_diag(f);
656 struct usb_ep *ep;
657 int status = -ENODEV;
658
659 intf_desc.bInterfaceNumber = usb_interface_id(c, f);
660
661 ep = usb_ep_autoconfig(cdev->gadget, &fs_bulk_in_desc);
Vamsi Krishnae606a7b2011-08-18 12:21:44 -0700662 if (!ep)
663 goto fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700664 ctxt->in = ep;
665 ep->driver_data = ctxt;
666
667 ep = usb_ep_autoconfig(cdev->gadget, &fs_bulk_out_desc);
Vamsi Krishnae606a7b2011-08-18 12:21:44 -0700668 if (!ep)
669 goto fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700670 ctxt->out = ep;
671 ep->driver_data = ctxt;
672
Pavankumar Kondeti6f94bc92012-08-03 09:34:32 +0530673 status = -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700674 /* copy descriptors, and track endpoint copies */
675 f->descriptors = usb_copy_descriptors(fs_diag_desc);
676 if (!f->descriptors)
677 goto fail;
678
679 if (gadget_is_dualspeed(c->cdev->gadget)) {
680 hs_bulk_in_desc.bEndpointAddress =
681 fs_bulk_in_desc.bEndpointAddress;
682 hs_bulk_out_desc.bEndpointAddress =
683 fs_bulk_out_desc.bEndpointAddress;
684
685 /* copy descriptors, and track endpoint copies */
686 f->hs_descriptors = usb_copy_descriptors(hs_diag_desc);
Pavankumar Kondeti6f94bc92012-08-03 09:34:32 +0530687 if (!f->hs_descriptors)
688 goto fail;
689 }
690
691 if (gadget_is_superspeed(c->cdev->gadget)) {
692 ss_bulk_in_desc.bEndpointAddress =
693 fs_bulk_in_desc.bEndpointAddress;
694 ss_bulk_out_desc.bEndpointAddress =
695 fs_bulk_out_desc.bEndpointAddress;
696
697 /* copy descriptors, and track endpoint copies */
698 f->ss_descriptors = usb_copy_descriptors(ss_diag_desc);
699 if (!f->ss_descriptors)
700 goto fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700701 }
702 return 0;
703fail:
Pavankumar Kondeti6f94bc92012-08-03 09:34:32 +0530704 if (f->ss_descriptors)
705 usb_free_descriptors(f->ss_descriptors);
706 if (f->hs_descriptors)
707 usb_free_descriptors(f->hs_descriptors);
708 if (f->descriptors)
709 usb_free_descriptors(f->descriptors);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700710 if (ctxt->out)
711 ctxt->out->driver_data = NULL;
712 if (ctxt->in)
713 ctxt->in->driver_data = NULL;
714 return status;
715
716}
717
718int diag_function_add(struct usb_configuration *c, const char *name,
719 int (*update_pid)(uint32_t, const char *))
720{
721 struct diag_context *dev;
722 struct usb_diag_ch *_ch;
723 int found = 0, ret;
724
725 DBG(c->cdev, "diag_function_add\n");
726
727 list_for_each_entry(_ch, &usb_diag_ch_list, list) {
728 if (!strcmp(name, _ch->name)) {
729 found = 1;
730 break;
731 }
732 }
733 if (!found) {
734 ERROR(c->cdev, "unable to get diag usb channel\n");
735 return -ENODEV;
736 }
737
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200738 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
739 if (!dev)
740 return -ENOMEM;
741
742 list_add_tail(&dev->list_item, &diag_dev_list);
743
744 /*
745 * A few diag devices can point to the same channel, in case that
746 * the diag devices belong to different configurations, however
747 * only the active diag device will claim the channel by setting
748 * the ch->priv_usb (see diag_function_set_alt).
749 */
750 dev->ch = _ch;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700751
Tatyana Brokhmancf709c12011-06-28 16:33:48 +0300752 dev->update_pid_and_serial_num = update_pid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700753 dev->cdev = c->cdev;
754 dev->function.name = _ch->name;
755 dev->function.descriptors = fs_diag_desc;
756 dev->function.hs_descriptors = hs_diag_desc;
757 dev->function.bind = diag_function_bind;
758 dev->function.unbind = diag_function_unbind;
759 dev->function.set_alt = diag_function_set_alt;
760 dev->function.disable = diag_function_disable;
761 spin_lock_init(&dev->lock);
762 INIT_LIST_HEAD(&dev->read_pool);
763 INIT_LIST_HEAD(&dev->write_pool);
764 INIT_WORK(&dev->config_work, usb_config_work_func);
765
766 ret = usb_add_function(c, &dev->function);
767 if (ret) {
768 INFO(c->cdev, "usb_add_function failed\n");
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200769 list_del(&dev->list_item);
770 kfree(dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700771 }
772
773 return ret;
774}
775
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700776#if defined(CONFIG_DEBUG_FS)
777static char debug_buffer[PAGE_SIZE];
778
779static ssize_t debug_read_stats(struct file *file, char __user *ubuf,
780 size_t count, loff_t *ppos)
781{
782 char *buf = debug_buffer;
783 int temp = 0;
784 struct usb_diag_ch *ch;
785
786 list_for_each_entry(ch, &usb_diag_ch_list, list) {
Jack Pham1c20e3f2012-04-04 19:29:14 -0700787 struct diag_context *ctxt = ch->priv_usb;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700788
Jack Pham1c20e3f2012-04-04 19:29:14 -0700789 if (ctxt)
790 temp += scnprintf(buf + temp, PAGE_SIZE - temp,
791 "---Name: %s---\n"
792 "endpoints: %s, %s\n"
793 "dpkts_tolaptop: %lu\n"
794 "dpkts_tomodem: %lu\n"
795 "pkts_tolaptop_pending: %u\n",
796 ch->name,
797 ctxt->in->name, ctxt->out->name,
798 ctxt->dpkts_tolaptop,
799 ctxt->dpkts_tomodem,
800 ctxt->dpkts_tolaptop_pending);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700801 }
802
803 return simple_read_from_buffer(ubuf, count, ppos, buf, temp);
804}
805
806static ssize_t debug_reset_stats(struct file *file, const char __user *buf,
807 size_t count, loff_t *ppos)
808{
809 struct usb_diag_ch *ch;
810
811 list_for_each_entry(ch, &usb_diag_ch_list, list) {
Jack Pham1c20e3f2012-04-04 19:29:14 -0700812 struct diag_context *ctxt = ch->priv_usb;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700813
Jack Pham1c20e3f2012-04-04 19:29:14 -0700814 if (ctxt) {
815 ctxt->dpkts_tolaptop = 0;
816 ctxt->dpkts_tomodem = 0;
817 ctxt->dpkts_tolaptop_pending = 0;
818 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700819 }
820
821 return count;
822}
823
824static int debug_open(struct inode *inode, struct file *file)
825{
826 return 0;
827}
828
829static const struct file_operations debug_fdiag_ops = {
830 .open = debug_open,
831 .read = debug_read_stats,
832 .write = debug_reset_stats,
833};
834
Manu Gautamd9f56a12011-09-26 14:04:49 +0530835struct dentry *dent_diag;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700836static void fdiag_debugfs_init(void)
837{
Manu Gautamd9f56a12011-09-26 14:04:49 +0530838 dent_diag = debugfs_create_dir("usb_diag", 0);
839 if (IS_ERR(dent_diag))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700840 return;
841
Manu Gautamd9f56a12011-09-26 14:04:49 +0530842 debugfs_create_file("status", 0444, dent_diag, 0, &debug_fdiag_ops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700843}
844#else
845static void fdiag_debugfs_init(void)
846{
847 return;
848}
849#endif
850
851static void diag_cleanup(void)
852{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700853 struct list_head *act, *tmp;
854 struct usb_diag_ch *_ch;
855 unsigned long flags;
856
Manu Gautamd9f56a12011-09-26 14:04:49 +0530857 debugfs_remove_recursive(dent_diag);
858
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700859 list_for_each_safe(act, tmp, &usb_diag_ch_list) {
860 _ch = list_entry(act, struct usb_diag_ch, list);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700861
862 spin_lock_irqsave(&ch_lock, flags);
863 /* Free if diagchar is not using the channel anymore */
864 if (!_ch->priv) {
865 list_del(&_ch->list);
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200866 kfree(_ch);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700867 }
868 spin_unlock_irqrestore(&ch_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700869 }
870}
871
872static int diag_setup(void)
873{
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200874 INIT_LIST_HEAD(&diag_dev_list);
875
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700876 fdiag_debugfs_init();
877
878 return 0;
879}