blob: d22d9b34a9e1f27d5efa98ba0db68e41de886232 [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
184 /* pass on product id and serial number to dload */
185 if (!cdev->desc.iSerialNumber) {
186 ctxt->update_pid_and_serial_num(
187 cdev->desc.idProduct, 0);
188 return;
189 }
190
191 /*
192 * Serial number is filled by the composite driver. So
193 * it is fair enough to assume that it will always be
194 * found at first table of strings.
195 */
196 table = *(cdev->driver->strings);
197 for (s = table->strings; s && s->s; s++)
198 if (s->id == cdev->desc.iSerialNumber) {
199 ctxt->update_pid_and_serial_num(
200 cdev->desc.idProduct, s->s);
201 break;
202 }
203}
204
205static void diag_write_complete(struct usb_ep *ep,
206 struct usb_request *req)
207{
208 struct diag_context *ctxt = ep->driver_data;
209 struct diag_request *d_req = req->context;
210 unsigned long flags;
211
212 ctxt->dpkts_tolaptop_pending--;
213
214 if (!req->status) {
215 if ((req->length >= ep->maxpacket) &&
216 ((req->length % ep->maxpacket) == 0)) {
217 ctxt->dpkts_tolaptop_pending++;
218 req->length = 0;
219 d_req->actual = req->actual;
220 d_req->status = req->status;
221 /* Queue zero length packet */
222 usb_ep_queue(ctxt->in, req, GFP_ATOMIC);
223 return;
224 }
225 }
226
227 spin_lock_irqsave(&ctxt->lock, flags);
228 list_add_tail(&req->list, &ctxt->write_pool);
229 if (req->length != 0) {
230 d_req->actual = req->actual;
231 d_req->status = req->status;
232 }
233 spin_unlock_irqrestore(&ctxt->lock, flags);
234
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200235 if (ctxt->ch && ctxt->ch->notify)
236 ctxt->ch->notify(ctxt->ch->priv, USB_DIAG_WRITE_DONE, d_req);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700237}
238
239static void diag_read_complete(struct usb_ep *ep,
240 struct usb_request *req)
241{
242 struct diag_context *ctxt = ep->driver_data;
243 struct diag_request *d_req = req->context;
244 unsigned long flags;
245
246 d_req->actual = req->actual;
247 d_req->status = req->status;
248
249 spin_lock_irqsave(&ctxt->lock, flags);
250 list_add_tail(&req->list, &ctxt->read_pool);
251 spin_unlock_irqrestore(&ctxt->lock, flags);
252
253 ctxt->dpkts_tomodem++;
254
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200255 if (ctxt->ch && ctxt->ch->notify)
256 ctxt->ch->notify(ctxt->ch->priv, USB_DIAG_READ_DONE, d_req);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700257}
258
259/**
260 * usb_diag_open() - Open a diag channel over USB
261 * @name: Name of the channel
262 * @priv: Private structure pointer which will be passed in notify()
263 * @notify: Callback function to receive notifications
264 *
265 * This function iterates overs the available channels and returns
266 * the channel handler if the name matches. The notify callback is called
267 * for CONNECT, DISCONNECT, READ_DONE and WRITE_DONE events.
268 *
269 */
270struct usb_diag_ch *usb_diag_open(const char *name, void *priv,
271 void (*notify)(void *, unsigned, struct diag_request *))
272{
273 struct usb_diag_ch *ch;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700274 unsigned long flags;
275 int found = 0;
276
277 spin_lock_irqsave(&ch_lock, flags);
278 /* Check if we already have a channel with this name */
279 list_for_each_entry(ch, &usb_diag_ch_list, list) {
280 if (!strcmp(name, ch->name)) {
281 found = 1;
282 break;
283 }
284 }
285 spin_unlock_irqrestore(&ch_lock, flags);
286
287 if (!found) {
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200288 ch = kzalloc(sizeof(*ch), GFP_KERNEL);
289 if (!ch)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700290 return ERR_PTR(-ENOMEM);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700291 }
292
293 ch->name = name;
294 ch->priv = priv;
295 ch->notify = notify;
296
297 spin_lock_irqsave(&ch_lock, flags);
298 list_add_tail(&ch->list, &usb_diag_ch_list);
299 spin_unlock_irqrestore(&ch_lock, flags);
300
301 return ch;
302}
303EXPORT_SYMBOL(usb_diag_open);
304
305/**
306 * usb_diag_close() - Close a diag channel over USB
307 * @ch: Channel handler
308 *
309 * This function closes the diag channel.
310 *
311 */
312void usb_diag_close(struct usb_diag_ch *ch)
313{
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200314 struct diag_context *dev = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700315 unsigned long flags;
316
317 spin_lock_irqsave(&ch_lock, flags);
318 ch->priv = NULL;
319 ch->notify = NULL;
320 /* Free-up the resources if channel is no more active */
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200321 list_del(&ch->list);
322 list_for_each_entry(dev, &diag_dev_list, list_item)
323 if (dev->ch == ch)
324 dev->ch = NULL;
325 kfree(ch);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700326
327 spin_unlock_irqrestore(&ch_lock, flags);
328}
329EXPORT_SYMBOL(usb_diag_close);
330
Pavankumar Kondetieb1ed0e2013-01-02 14:46:58 +0530331static void free_reqs(struct diag_context *ctxt)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700332{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700333 struct list_head *act, *tmp;
Pavankumar Kondetieb1ed0e2013-01-02 14:46:58 +0530334 struct usb_request *req;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700335
336 list_for_each_safe(act, tmp, &ctxt->write_pool) {
337 req = list_entry(act, struct usb_request, list);
338 list_del(&req->list);
339 usb_ep_free_request(ctxt->in, req);
340 }
341
342 list_for_each_safe(act, tmp, &ctxt->read_pool) {
343 req = list_entry(act, struct usb_request, list);
344 list_del(&req->list);
345 usb_ep_free_request(ctxt->out, req);
346 }
347}
Pavankumar Kondetieb1ed0e2013-01-02 14:46:58 +0530348
349/**
350 * usb_diag_free_req() - Free USB requests
351 * @ch: Channel handler
352 *
353 * This function free read and write USB requests for the interface
354 * associated with this channel.
355 *
356 */
357void usb_diag_free_req(struct usb_diag_ch *ch)
358{
359 struct diag_context *ctxt = ch->priv_usb;
360 unsigned long flags;
361
362 if (ctxt) {
363 spin_lock_irqsave(&ctxt->lock, flags);
364 free_reqs(ctxt);
365 spin_unlock_irqrestore(&ctxt->lock, flags);
366 }
367
368}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700369EXPORT_SYMBOL(usb_diag_free_req);
370
371/**
372 * usb_diag_alloc_req() - Allocate USB requests
373 * @ch: Channel handler
374 * @n_write: Number of requests for Tx
375 * @n_read: Number of requests for Rx
376 *
377 * This function allocate read and write USB requests for the interface
378 * associated with this channel. The actual buffer is not allocated.
379 * The buffer is passed by diag char driver.
380 *
381 */
382int usb_diag_alloc_req(struct usb_diag_ch *ch, int n_write, int n_read)
383{
384 struct diag_context *ctxt = ch->priv_usb;
385 struct usb_request *req;
386 int i;
Pavankumar Kondetieb1ed0e2013-01-02 14:46:58 +0530387 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700388
389 if (!ctxt)
390 return -ENODEV;
391
Pavankumar Kondetieb1ed0e2013-01-02 14:46:58 +0530392 spin_lock_irqsave(&ctxt->lock, flags);
393 /* Free previous session's stale requests */
394 free_reqs(ctxt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700395 for (i = 0; i < n_write; i++) {
396 req = usb_ep_alloc_request(ctxt->in, GFP_ATOMIC);
397 if (!req)
398 goto fail;
399 req->complete = diag_write_complete;
400 list_add_tail(&req->list, &ctxt->write_pool);
401 }
402
403 for (i = 0; i < n_read; i++) {
404 req = usb_ep_alloc_request(ctxt->out, GFP_ATOMIC);
405 if (!req)
406 goto fail;
407 req->complete = diag_read_complete;
408 list_add_tail(&req->list, &ctxt->read_pool);
409 }
Pavankumar Kondetieb1ed0e2013-01-02 14:46:58 +0530410 spin_unlock_irqrestore(&ctxt->lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700411 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700412fail:
Pavankumar Kondetieb1ed0e2013-01-02 14:46:58 +0530413 free_reqs(ctxt);
414 spin_unlock_irqrestore(&ctxt->lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700415 return -ENOMEM;
416
417}
418EXPORT_SYMBOL(usb_diag_alloc_req);
419
420/**
421 * usb_diag_read() - Read data from USB diag channel
422 * @ch: Channel handler
423 * @d_req: Diag request struct
424 *
425 * Enqueue a request on OUT endpoint of the interface corresponding to this
426 * channel. This function returns proper error code when interface is not
427 * in configured state, no Rx requests available and ep queue is failed.
428 *
429 * This function operates asynchronously. READ_DONE event is notified after
430 * completion of OUT request.
431 *
432 */
433int usb_diag_read(struct usb_diag_ch *ch, struct diag_request *d_req)
434{
435 struct diag_context *ctxt = ch->priv_usb;
436 unsigned long flags;
437 struct usb_request *req;
Pavankumar Kondetiac3aa912012-11-26 14:52:37 +0530438 static DEFINE_RATELIMIT_STATE(rl, 10*HZ, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700439
Manu Gautamb33f33a2011-09-09 14:35:57 +0530440 if (!ctxt)
441 return -ENODEV;
442
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700443 spin_lock_irqsave(&ctxt->lock, flags);
444
445 if (!ctxt->configured) {
446 spin_unlock_irqrestore(&ctxt->lock, flags);
447 return -EIO;
448 }
449
450 if (list_empty(&ctxt->read_pool)) {
451 spin_unlock_irqrestore(&ctxt->lock, flags);
452 ERROR(ctxt->cdev, "%s: no requests available\n", __func__);
453 return -EAGAIN;
454 }
455
456 req = list_first_entry(&ctxt->read_pool, struct usb_request, list);
457 list_del(&req->list);
458 spin_unlock_irqrestore(&ctxt->lock, flags);
459
460 req->buf = d_req->buf;
461 req->length = d_req->length;
462 req->context = d_req;
463 if (usb_ep_queue(ctxt->out, req, GFP_ATOMIC)) {
464 /* If error add the link to linked list again*/
465 spin_lock_irqsave(&ctxt->lock, flags);
466 list_add_tail(&req->list, &ctxt->read_pool);
467 spin_unlock_irqrestore(&ctxt->lock, flags);
Pavankumar Kondetiac3aa912012-11-26 14:52:37 +0530468 /* 1 error message for every 10 sec */
469 if (__ratelimit(&rl))
470 ERROR(ctxt->cdev, "%s: cannot queue"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700471 " read request\n", __func__);
472 return -EIO;
473 }
474
475 return 0;
476}
477EXPORT_SYMBOL(usb_diag_read);
478
479/**
480 * usb_diag_write() - Write data from USB diag channel
481 * @ch: Channel handler
482 * @d_req: Diag request struct
483 *
484 * Enqueue a request on IN endpoint of the interface corresponding to this
485 * channel. This function returns proper error code when interface is not
486 * in configured state, no Tx requests available and ep queue is failed.
487 *
488 * This function operates asynchronously. WRITE_DONE event is notified after
489 * completion of IN request.
490 *
491 */
492int usb_diag_write(struct usb_diag_ch *ch, struct diag_request *d_req)
493{
494 struct diag_context *ctxt = ch->priv_usb;
495 unsigned long flags;
496 struct usb_request *req = NULL;
Pavankumar Kondetiac3aa912012-11-26 14:52:37 +0530497 static DEFINE_RATELIMIT_STATE(rl, 10*HZ, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700498
Manu Gautamb33f33a2011-09-09 14:35:57 +0530499 if (!ctxt)
500 return -ENODEV;
501
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700502 spin_lock_irqsave(&ctxt->lock, flags);
503
504 if (!ctxt->configured) {
505 spin_unlock_irqrestore(&ctxt->lock, flags);
506 return -EIO;
507 }
508
509 if (list_empty(&ctxt->write_pool)) {
510 spin_unlock_irqrestore(&ctxt->lock, flags);
511 ERROR(ctxt->cdev, "%s: no requests available\n", __func__);
512 return -EAGAIN;
513 }
514
515 req = list_first_entry(&ctxt->write_pool, struct usb_request, list);
516 list_del(&req->list);
517 spin_unlock_irqrestore(&ctxt->lock, flags);
518
519 req->buf = d_req->buf;
520 req->length = d_req->length;
521 req->context = d_req;
522 if (usb_ep_queue(ctxt->in, req, GFP_ATOMIC)) {
523 /* If error add the link to linked list again*/
524 spin_lock_irqsave(&ctxt->lock, flags);
525 list_add_tail(&req->list, &ctxt->write_pool);
526 spin_unlock_irqrestore(&ctxt->lock, flags);
Pavankumar Kondetiac3aa912012-11-26 14:52:37 +0530527 /* 1 error message for every 10 sec */
528 if (__ratelimit(&rl))
529 ERROR(ctxt->cdev, "%s: cannot queue"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700530 " read request\n", __func__);
531 return -EIO;
532 }
533
534 ctxt->dpkts_tolaptop++;
535 ctxt->dpkts_tolaptop_pending++;
536
537 return 0;
538}
539EXPORT_SYMBOL(usb_diag_write);
540
541static void diag_function_disable(struct usb_function *f)
542{
543 struct diag_context *dev = func_to_diag(f);
544 unsigned long flags;
545
546 DBG(dev->cdev, "diag_function_disable\n");
547
548 spin_lock_irqsave(&dev->lock, flags);
549 dev->configured = 0;
550 spin_unlock_irqrestore(&dev->lock, flags);
551
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200552 if (dev->ch && dev->ch->notify)
553 dev->ch->notify(dev->ch->priv, USB_DIAG_DISCONNECT, NULL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700554
555 usb_ep_disable(dev->in);
556 dev->in->driver_data = NULL;
557
558 usb_ep_disable(dev->out);
559 dev->out->driver_data = NULL;
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200560 if (dev->ch)
561 dev->ch->priv_usb = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700562}
563
564static int diag_function_set_alt(struct usb_function *f,
565 unsigned intf, unsigned alt)
566{
567 struct diag_context *dev = func_to_diag(f);
568 struct usb_composite_dev *cdev = f->config->cdev;
569 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700570 int rc = 0;
571
Tatyana Brokhman31ac3522011-06-28 15:33:50 +0200572 if (config_ep_by_speed(cdev->gadget, f, dev->in) ||
573 config_ep_by_speed(cdev->gadget, f, dev->out)) {
574 dev->in->desc = NULL;
575 dev->out->desc = NULL;
576 return -EINVAL;
577 }
578
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200579 if (!dev->ch)
580 return -ENODEV;
581
582 /*
583 * Indicate to the diag channel that the active diag device is dev.
584 * Since a few diag devices can point to the same channel.
585 */
586 dev->ch->priv_usb = dev;
587
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700588 dev->in->driver_data = dev;
Tatyana Brokhmancf709c12011-06-28 16:33:48 +0300589 rc = usb_ep_enable(dev->in);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700590 if (rc) {
591 ERROR(dev->cdev, "can't enable %s, result %d\n",
592 dev->in->name, rc);
593 return rc;
594 }
595 dev->out->driver_data = dev;
Tatyana Brokhmancf709c12011-06-28 16:33:48 +0300596 rc = usb_ep_enable(dev->out);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700597 if (rc) {
598 ERROR(dev->cdev, "can't enable %s, result %d\n",
599 dev->out->name, rc);
600 usb_ep_disable(dev->in);
601 return rc;
602 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700603
Manu Gautam0c611072011-11-11 17:40:05 +0530604 dev->dpkts_tolaptop = 0;
605 dev->dpkts_tomodem = 0;
606 dev->dpkts_tolaptop_pending = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700607
608 spin_lock_irqsave(&dev->lock, flags);
609 dev->configured = 1;
610 spin_unlock_irqrestore(&dev->lock, flags);
611
Pavankumar Kondetic3586582013-04-18 11:02:28 +0530612 if (dev->ch->notify)
613 dev->ch->notify(dev->ch->priv, USB_DIAG_CONNECT, NULL);
614
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700615 return rc;
616}
617
618static void diag_function_unbind(struct usb_configuration *c,
619 struct usb_function *f)
620{
621 struct diag_context *ctxt = func_to_diag(f);
622
Pavankumar Kondeti6f94bc92012-08-03 09:34:32 +0530623 if (gadget_is_superspeed(c->cdev->gadget))
624 usb_free_descriptors(f->ss_descriptors);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700625 if (gadget_is_dualspeed(c->cdev->gadget))
626 usb_free_descriptors(f->hs_descriptors);
627
628 usb_free_descriptors(f->descriptors);
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200629
630 /*
631 * Channel priv_usb may point to other diag function.
632 * Clear the priv_usb only if the channel is used by the
633 * diag dev we unbind here.
634 */
635 if (ctxt->ch && ctxt->ch->priv_usb == ctxt)
636 ctxt->ch->priv_usb = NULL;
637 list_del(&ctxt->list_item);
638 kfree(ctxt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700639}
640
641static int diag_function_bind(struct usb_configuration *c,
642 struct usb_function *f)
643{
644 struct usb_composite_dev *cdev = c->cdev;
645 struct diag_context *ctxt = func_to_diag(f);
646 struct usb_ep *ep;
647 int status = -ENODEV;
648
649 intf_desc.bInterfaceNumber = usb_interface_id(c, f);
650
651 ep = usb_ep_autoconfig(cdev->gadget, &fs_bulk_in_desc);
Vamsi Krishnae606a7b2011-08-18 12:21:44 -0700652 if (!ep)
653 goto fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700654 ctxt->in = ep;
655 ep->driver_data = ctxt;
656
657 ep = usb_ep_autoconfig(cdev->gadget, &fs_bulk_out_desc);
Vamsi Krishnae606a7b2011-08-18 12:21:44 -0700658 if (!ep)
659 goto fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700660 ctxt->out = ep;
661 ep->driver_data = ctxt;
662
Pavankumar Kondeti6f94bc92012-08-03 09:34:32 +0530663 status = -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700664 /* copy descriptors, and track endpoint copies */
665 f->descriptors = usb_copy_descriptors(fs_diag_desc);
666 if (!f->descriptors)
667 goto fail;
668
669 if (gadget_is_dualspeed(c->cdev->gadget)) {
670 hs_bulk_in_desc.bEndpointAddress =
671 fs_bulk_in_desc.bEndpointAddress;
672 hs_bulk_out_desc.bEndpointAddress =
673 fs_bulk_out_desc.bEndpointAddress;
674
675 /* copy descriptors, and track endpoint copies */
676 f->hs_descriptors = usb_copy_descriptors(hs_diag_desc);
Pavankumar Kondeti6f94bc92012-08-03 09:34:32 +0530677 if (!f->hs_descriptors)
678 goto fail;
679 }
680
681 if (gadget_is_superspeed(c->cdev->gadget)) {
682 ss_bulk_in_desc.bEndpointAddress =
683 fs_bulk_in_desc.bEndpointAddress;
684 ss_bulk_out_desc.bEndpointAddress =
685 fs_bulk_out_desc.bEndpointAddress;
686
687 /* copy descriptors, and track endpoint copies */
688 f->ss_descriptors = usb_copy_descriptors(ss_diag_desc);
689 if (!f->ss_descriptors)
690 goto fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700691 }
Pavankumar Kondetic3586582013-04-18 11:02:28 +0530692 diag_update_pid_and_serial_num(ctxt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700693 return 0;
694fail:
Pavankumar Kondeti6f94bc92012-08-03 09:34:32 +0530695 if (f->ss_descriptors)
696 usb_free_descriptors(f->ss_descriptors);
697 if (f->hs_descriptors)
698 usb_free_descriptors(f->hs_descriptors);
699 if (f->descriptors)
700 usb_free_descriptors(f->descriptors);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700701 if (ctxt->out)
702 ctxt->out->driver_data = NULL;
703 if (ctxt->in)
704 ctxt->in->driver_data = NULL;
705 return status;
706
707}
708
709int diag_function_add(struct usb_configuration *c, const char *name,
710 int (*update_pid)(uint32_t, const char *))
711{
712 struct diag_context *dev;
713 struct usb_diag_ch *_ch;
714 int found = 0, ret;
715
716 DBG(c->cdev, "diag_function_add\n");
717
718 list_for_each_entry(_ch, &usb_diag_ch_list, list) {
719 if (!strcmp(name, _ch->name)) {
720 found = 1;
721 break;
722 }
723 }
724 if (!found) {
725 ERROR(c->cdev, "unable to get diag usb channel\n");
726 return -ENODEV;
727 }
728
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200729 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
730 if (!dev)
731 return -ENOMEM;
732
733 list_add_tail(&dev->list_item, &diag_dev_list);
734
735 /*
736 * A few diag devices can point to the same channel, in case that
737 * the diag devices belong to different configurations, however
738 * only the active diag device will claim the channel by setting
739 * the ch->priv_usb (see diag_function_set_alt).
740 */
741 dev->ch = _ch;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700742
Tatyana Brokhmancf709c12011-06-28 16:33:48 +0300743 dev->update_pid_and_serial_num = update_pid;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700744 dev->cdev = c->cdev;
745 dev->function.name = _ch->name;
746 dev->function.descriptors = fs_diag_desc;
747 dev->function.hs_descriptors = hs_diag_desc;
748 dev->function.bind = diag_function_bind;
749 dev->function.unbind = diag_function_unbind;
750 dev->function.set_alt = diag_function_set_alt;
751 dev->function.disable = diag_function_disable;
752 spin_lock_init(&dev->lock);
753 INIT_LIST_HEAD(&dev->read_pool);
754 INIT_LIST_HEAD(&dev->write_pool);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700755
756 ret = usb_add_function(c, &dev->function);
757 if (ret) {
758 INFO(c->cdev, "usb_add_function failed\n");
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200759 list_del(&dev->list_item);
760 kfree(dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700761 }
762
763 return ret;
764}
765
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700766#if defined(CONFIG_DEBUG_FS)
767static char debug_buffer[PAGE_SIZE];
768
769static ssize_t debug_read_stats(struct file *file, char __user *ubuf,
770 size_t count, loff_t *ppos)
771{
772 char *buf = debug_buffer;
773 int temp = 0;
774 struct usb_diag_ch *ch;
775
776 list_for_each_entry(ch, &usb_diag_ch_list, list) {
Jack Pham1c20e3f2012-04-04 19:29:14 -0700777 struct diag_context *ctxt = ch->priv_usb;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700778
Jack Pham1c20e3f2012-04-04 19:29:14 -0700779 if (ctxt)
780 temp += scnprintf(buf + temp, PAGE_SIZE - temp,
781 "---Name: %s---\n"
782 "endpoints: %s, %s\n"
783 "dpkts_tolaptop: %lu\n"
784 "dpkts_tomodem: %lu\n"
785 "pkts_tolaptop_pending: %u\n",
786 ch->name,
787 ctxt->in->name, ctxt->out->name,
788 ctxt->dpkts_tolaptop,
789 ctxt->dpkts_tomodem,
790 ctxt->dpkts_tolaptop_pending);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700791 }
792
793 return simple_read_from_buffer(ubuf, count, ppos, buf, temp);
794}
795
796static ssize_t debug_reset_stats(struct file *file, const char __user *buf,
797 size_t count, loff_t *ppos)
798{
799 struct usb_diag_ch *ch;
800
801 list_for_each_entry(ch, &usb_diag_ch_list, list) {
Jack Pham1c20e3f2012-04-04 19:29:14 -0700802 struct diag_context *ctxt = ch->priv_usb;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700803
Jack Pham1c20e3f2012-04-04 19:29:14 -0700804 if (ctxt) {
805 ctxt->dpkts_tolaptop = 0;
806 ctxt->dpkts_tomodem = 0;
807 ctxt->dpkts_tolaptop_pending = 0;
808 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700809 }
810
811 return count;
812}
813
814static int debug_open(struct inode *inode, struct file *file)
815{
816 return 0;
817}
818
819static const struct file_operations debug_fdiag_ops = {
820 .open = debug_open,
821 .read = debug_read_stats,
822 .write = debug_reset_stats,
823};
824
Manu Gautamd9f56a12011-09-26 14:04:49 +0530825struct dentry *dent_diag;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700826static void fdiag_debugfs_init(void)
827{
Manu Gautamd9f56a12011-09-26 14:04:49 +0530828 dent_diag = debugfs_create_dir("usb_diag", 0);
829 if (IS_ERR(dent_diag))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700830 return;
831
Manu Gautamd9f56a12011-09-26 14:04:49 +0530832 debugfs_create_file("status", 0444, dent_diag, 0, &debug_fdiag_ops);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700833}
834#else
835static void fdiag_debugfs_init(void)
836{
837 return;
838}
839#endif
840
841static void diag_cleanup(void)
842{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700843 struct list_head *act, *tmp;
844 struct usb_diag_ch *_ch;
845 unsigned long flags;
846
Manu Gautamd9f56a12011-09-26 14:04:49 +0530847 debugfs_remove_recursive(dent_diag);
848
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700849 list_for_each_safe(act, tmp, &usb_diag_ch_list) {
850 _ch = list_entry(act, struct usb_diag_ch, list);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700851
852 spin_lock_irqsave(&ch_lock, flags);
853 /* Free if diagchar is not using the channel anymore */
854 if (!_ch->priv) {
855 list_del(&_ch->list);
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200856 kfree(_ch);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700857 }
858 spin_unlock_irqrestore(&ch_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700859 }
860}
861
862static int diag_setup(void)
863{
Ido Shayevitz6c00b3d2013-01-18 18:05:40 +0200864 INIT_LIST_HEAD(&diag_dev_list);
865
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700866 fdiag_debugfs_init();
867
868 return 0;
869}