blob: 55f5b804d06924cd28cbaa8d6cd56cc1675c104f [file] [log] [blame]
Benoit Goby2b6862d2011-12-19 14:38:41 -08001/*
2 * Gadget Driver for Android ADB
3 *
4 * Copyright (C) 2008 Google, Inc.
5 * Author: Mike Lockwood <lockwood@android.com>
6 *
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
18#include <linux/module.h>
19#include <linux/init.h>
20#include <linux/poll.h>
21#include <linux/delay.h>
22#include <linux/wait.h>
23#include <linux/err.h>
24#include <linux/interrupt.h>
25#include <linux/sched.h>
26#include <linux/types.h>
27#include <linux/device.h>
28#include <linux/miscdevice.h>
29
30#define ADB_BULK_BUFFER_SIZE 4096
31
32/* number of tx requests to allocate */
33#define TX_REQ_MAX 4
34
35static const char adb_shortname[] = "android_adb";
36
37struct adb_dev {
38 struct usb_function function;
39 struct usb_composite_dev *cdev;
40 spinlock_t lock;
41
42 struct usb_ep *ep_in;
43 struct usb_ep *ep_out;
44
Manu Gautam8f687952011-09-30 15:07:36 +053045 atomic_t online;
46 atomic_t error;
Benoit Goby2b6862d2011-12-19 14:38:41 -080047
48 atomic_t read_excl;
49 atomic_t write_excl;
50 atomic_t open_excl;
51
52 struct list_head tx_idle;
53
54 wait_queue_head_t read_wq;
55 wait_queue_head_t write_wq;
56 struct usb_request *rx_req;
57 int rx_done;
Pavankumar Kondeti1aa235a2012-10-09 17:47:44 +053058 bool notify_close;
Pavankumar Kondetic4d8e2c2012-10-26 11:15:54 +053059 bool close_notified;
Benoit Goby2b6862d2011-12-19 14:38:41 -080060};
61
62static struct usb_interface_descriptor adb_interface_desc = {
63 .bLength = USB_DT_INTERFACE_SIZE,
64 .bDescriptorType = USB_DT_INTERFACE,
65 .bInterfaceNumber = 0,
66 .bNumEndpoints = 2,
67 .bInterfaceClass = 0xFF,
68 .bInterfaceSubClass = 0x42,
69 .bInterfaceProtocol = 1,
70};
71
Pavankumar Kondeti6f94bc92012-08-03 09:34:32 +053072static struct usb_endpoint_descriptor adb_superspeed_in_desc = {
73 .bLength = USB_DT_ENDPOINT_SIZE,
74 .bDescriptorType = USB_DT_ENDPOINT,
75 .bEndpointAddress = USB_DIR_IN,
76 .bmAttributes = USB_ENDPOINT_XFER_BULK,
77 .wMaxPacketSize = __constant_cpu_to_le16(1024),
78};
79
80static struct usb_ss_ep_comp_descriptor adb_superspeed_in_comp_desc = {
81 .bLength = sizeof adb_superspeed_in_comp_desc,
82 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
83
84 /* the following 2 values can be tweaked if necessary */
85 /* .bMaxBurst = 0, */
86 /* .bmAttributes = 0, */
87};
88
89static struct usb_endpoint_descriptor adb_superspeed_out_desc = {
90 .bLength = USB_DT_ENDPOINT_SIZE,
91 .bDescriptorType = USB_DT_ENDPOINT,
92 .bEndpointAddress = USB_DIR_OUT,
93 .bmAttributes = USB_ENDPOINT_XFER_BULK,
94 .wMaxPacketSize = __constant_cpu_to_le16(1024),
95};
96
97static struct usb_ss_ep_comp_descriptor adb_superspeed_out_comp_desc = {
98 .bLength = sizeof adb_superspeed_out_comp_desc,
99 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
100
101 /* the following 2 values can be tweaked if necessary */
102 /* .bMaxBurst = 0, */
103 /* .bmAttributes = 0, */
104};
105
Benoit Goby2b6862d2011-12-19 14:38:41 -0800106static struct usb_endpoint_descriptor adb_highspeed_in_desc = {
107 .bLength = USB_DT_ENDPOINT_SIZE,
108 .bDescriptorType = USB_DT_ENDPOINT,
109 .bEndpointAddress = USB_DIR_IN,
110 .bmAttributes = USB_ENDPOINT_XFER_BULK,
111 .wMaxPacketSize = __constant_cpu_to_le16(512),
112};
113
114static struct usb_endpoint_descriptor adb_highspeed_out_desc = {
115 .bLength = USB_DT_ENDPOINT_SIZE,
116 .bDescriptorType = USB_DT_ENDPOINT,
117 .bEndpointAddress = USB_DIR_OUT,
118 .bmAttributes = USB_ENDPOINT_XFER_BULK,
119 .wMaxPacketSize = __constant_cpu_to_le16(512),
120};
121
122static struct usb_endpoint_descriptor adb_fullspeed_in_desc = {
123 .bLength = USB_DT_ENDPOINT_SIZE,
124 .bDescriptorType = USB_DT_ENDPOINT,
125 .bEndpointAddress = USB_DIR_IN,
126 .bmAttributes = USB_ENDPOINT_XFER_BULK,
127};
128
129static struct usb_endpoint_descriptor adb_fullspeed_out_desc = {
130 .bLength = USB_DT_ENDPOINT_SIZE,
131 .bDescriptorType = USB_DT_ENDPOINT,
132 .bEndpointAddress = USB_DIR_OUT,
133 .bmAttributes = USB_ENDPOINT_XFER_BULK,
134};
135
136static struct usb_descriptor_header *fs_adb_descs[] = {
137 (struct usb_descriptor_header *) &adb_interface_desc,
138 (struct usb_descriptor_header *) &adb_fullspeed_in_desc,
139 (struct usb_descriptor_header *) &adb_fullspeed_out_desc,
140 NULL,
141};
142
143static struct usb_descriptor_header *hs_adb_descs[] = {
144 (struct usb_descriptor_header *) &adb_interface_desc,
145 (struct usb_descriptor_header *) &adb_highspeed_in_desc,
146 (struct usb_descriptor_header *) &adb_highspeed_out_desc,
147 NULL,
148};
149
Pavankumar Kondeti6f94bc92012-08-03 09:34:32 +0530150static struct usb_descriptor_header *ss_adb_descs[] = {
151 (struct usb_descriptor_header *) &adb_interface_desc,
152 (struct usb_descriptor_header *) &adb_superspeed_in_desc,
153 (struct usb_descriptor_header *) &adb_superspeed_in_comp_desc,
154 (struct usb_descriptor_header *) &adb_superspeed_out_desc,
155 (struct usb_descriptor_header *) &adb_superspeed_out_comp_desc,
156 NULL,
157};
158
Benoit Goby80ba14d2012-03-19 18:56:52 -0700159static void adb_ready_callback(void);
160static void adb_closed_callback(void);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800161
162/* temporary variable used between adb_open() and adb_gadget_bind() */
163static struct adb_dev *_adb_dev;
164
165static inline struct adb_dev *func_to_adb(struct usb_function *f)
166{
167 return container_of(f, struct adb_dev, function);
168}
169
170
171static struct usb_request *adb_request_new(struct usb_ep *ep, int buffer_size)
172{
173 struct usb_request *req = usb_ep_alloc_request(ep, GFP_KERNEL);
174 if (!req)
175 return NULL;
176
177 /* now allocate buffers for the requests */
178 req->buf = kmalloc(buffer_size, GFP_KERNEL);
179 if (!req->buf) {
180 usb_ep_free_request(ep, req);
181 return NULL;
182 }
183
184 return req;
185}
186
187static void adb_request_free(struct usb_request *req, struct usb_ep *ep)
188{
189 if (req) {
190 kfree(req->buf);
191 usb_ep_free_request(ep, req);
192 }
193}
194
195static inline int adb_lock(atomic_t *excl)
196{
197 if (atomic_inc_return(excl) == 1) {
198 return 0;
199 } else {
200 atomic_dec(excl);
201 return -1;
202 }
203}
204
205static inline void adb_unlock(atomic_t *excl)
206{
207 atomic_dec(excl);
208}
209
210/* add a request to the tail of a list */
211void adb_req_put(struct adb_dev *dev, struct list_head *head,
212 struct usb_request *req)
213{
214 unsigned long flags;
215
216 spin_lock_irqsave(&dev->lock, flags);
217 list_add_tail(&req->list, head);
218 spin_unlock_irqrestore(&dev->lock, flags);
219}
220
221/* remove a request from the head of a list */
222struct usb_request *adb_req_get(struct adb_dev *dev, struct list_head *head)
223{
224 unsigned long flags;
225 struct usb_request *req;
226
227 spin_lock_irqsave(&dev->lock, flags);
228 if (list_empty(head)) {
229 req = 0;
230 } else {
231 req = list_first_entry(head, struct usb_request, list);
232 list_del(&req->list);
233 }
234 spin_unlock_irqrestore(&dev->lock, flags);
235 return req;
236}
237
238static void adb_complete_in(struct usb_ep *ep, struct usb_request *req)
239{
240 struct adb_dev *dev = _adb_dev;
241
242 if (req->status != 0)
Manu Gautam8f687952011-09-30 15:07:36 +0530243 atomic_set(&dev->error, 1);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800244
245 adb_req_put(dev, &dev->tx_idle, req);
246
247 wake_up(&dev->write_wq);
248}
249
250static void adb_complete_out(struct usb_ep *ep, struct usb_request *req)
251{
252 struct adb_dev *dev = _adb_dev;
253
254 dev->rx_done = 1;
Colin Cross8492aa12012-03-08 17:57:51 -0800255 if (req->status != 0 && req->status != -ECONNRESET)
Manu Gautam8f687952011-09-30 15:07:36 +0530256 atomic_set(&dev->error, 1);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800257
258 wake_up(&dev->read_wq);
259}
260
261static int adb_create_bulk_endpoints(struct adb_dev *dev,
262 struct usb_endpoint_descriptor *in_desc,
263 struct usb_endpoint_descriptor *out_desc)
264{
265 struct usb_composite_dev *cdev = dev->cdev;
266 struct usb_request *req;
267 struct usb_ep *ep;
268 int i;
269
270 DBG(cdev, "create_bulk_endpoints dev: %p\n", dev);
271
272 ep = usb_ep_autoconfig(cdev->gadget, in_desc);
273 if (!ep) {
274 DBG(cdev, "usb_ep_autoconfig for ep_in failed\n");
275 return -ENODEV;
276 }
277 DBG(cdev, "usb_ep_autoconfig for ep_in got %s\n", ep->name);
278 ep->driver_data = dev; /* claim the endpoint */
279 dev->ep_in = ep;
280
281 ep = usb_ep_autoconfig(cdev->gadget, out_desc);
282 if (!ep) {
283 DBG(cdev, "usb_ep_autoconfig for ep_out failed\n");
284 return -ENODEV;
285 }
286 DBG(cdev, "usb_ep_autoconfig for adb ep_out got %s\n", ep->name);
287 ep->driver_data = dev; /* claim the endpoint */
288 dev->ep_out = ep;
289
290 /* now allocate requests for our endpoints */
291 req = adb_request_new(dev->ep_out, ADB_BULK_BUFFER_SIZE);
292 if (!req)
293 goto fail;
294 req->complete = adb_complete_out;
295 dev->rx_req = req;
296
297 for (i = 0; i < TX_REQ_MAX; i++) {
298 req = adb_request_new(dev->ep_in, ADB_BULK_BUFFER_SIZE);
299 if (!req)
300 goto fail;
301 req->complete = adb_complete_in;
302 adb_req_put(dev, &dev->tx_idle, req);
303 }
304
305 return 0;
306
307fail:
308 printk(KERN_ERR "adb_bind() could not allocate requests\n");
309 return -1;
310}
311
312static ssize_t adb_read(struct file *fp, char __user *buf,
313 size_t count, loff_t *pos)
314{
315 struct adb_dev *dev = fp->private_data;
316 struct usb_request *req;
317 int r = count, xfer;
318 int ret;
319
320 pr_debug("adb_read(%d)\n", count);
321 if (!_adb_dev)
322 return -ENODEV;
323
324 if (count > ADB_BULK_BUFFER_SIZE)
325 return -EINVAL;
326
327 if (adb_lock(&dev->read_excl))
328 return -EBUSY;
329
330 /* we will block until we're online */
Manu Gautam8f687952011-09-30 15:07:36 +0530331 while (!(atomic_read(&dev->online) || atomic_read(&dev->error))) {
Benoit Goby2b6862d2011-12-19 14:38:41 -0800332 pr_debug("adb_read: waiting for online state\n");
333 ret = wait_event_interruptible(dev->read_wq,
Manu Gautam8f687952011-09-30 15:07:36 +0530334 (atomic_read(&dev->online) ||
335 atomic_read(&dev->error)));
Benoit Goby2b6862d2011-12-19 14:38:41 -0800336 if (ret < 0) {
337 adb_unlock(&dev->read_excl);
338 return ret;
339 }
340 }
Manu Gautam8f687952011-09-30 15:07:36 +0530341 if (atomic_read(&dev->error)) {
Benoit Goby2b6862d2011-12-19 14:38:41 -0800342 r = -EIO;
343 goto done;
344 }
345
346requeue_req:
347 /* queue a request */
348 req = dev->rx_req;
Ido Shayevitzf73ad6c2012-08-23 05:30:04 +0300349 req->length = ADB_BULK_BUFFER_SIZE;
Benoit Goby2b6862d2011-12-19 14:38:41 -0800350 dev->rx_done = 0;
351 ret = usb_ep_queue(dev->ep_out, req, GFP_ATOMIC);
352 if (ret < 0) {
353 pr_debug("adb_read: failed to queue req %p (%d)\n", req, ret);
354 r = -EIO;
Manu Gautam8f687952011-09-30 15:07:36 +0530355 atomic_set(&dev->error, 1);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800356 goto done;
357 } else {
358 pr_debug("rx %p queue\n", req);
359 }
360
361 /* wait for a request to complete */
Pavankumar Kondetie8757ca2013-02-08 14:16:37 +0530362 ret = wait_event_interruptible(dev->read_wq, dev->rx_done ||
363 atomic_read(&dev->error));
Benoit Goby2b6862d2011-12-19 14:38:41 -0800364 if (ret < 0) {
Colin Cross42582f22012-03-05 13:29:45 -0800365 if (ret != -ERESTARTSYS)
Manu Gautam8f687952011-09-30 15:07:36 +0530366 atomic_set(&dev->error, 1);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800367 r = ret;
368 usb_ep_dequeue(dev->ep_out, req);
369 goto done;
370 }
Manu Gautam8f687952011-09-30 15:07:36 +0530371 if (!atomic_read(&dev->error)) {
Benoit Goby2b6862d2011-12-19 14:38:41 -0800372 /* If we got a 0-len packet, throw it back and try again. */
373 if (req->actual == 0)
374 goto requeue_req;
375
376 pr_debug("rx %p %d\n", req, req->actual);
377 xfer = (req->actual < count) ? req->actual : count;
378 if (copy_to_user(buf, req->buf, xfer))
379 r = -EFAULT;
380
381 } else
382 r = -EIO;
383
384done:
Pavankumar Kondetie8757ca2013-02-08 14:16:37 +0530385 if (atomic_read(&dev->error))
386 wake_up(&dev->write_wq);
387
Benoit Goby2b6862d2011-12-19 14:38:41 -0800388 adb_unlock(&dev->read_excl);
389 pr_debug("adb_read returning %d\n", r);
390 return r;
391}
392
393static ssize_t adb_write(struct file *fp, const char __user *buf,
394 size_t count, loff_t *pos)
395{
396 struct adb_dev *dev = fp->private_data;
397 struct usb_request *req = 0;
398 int r = count, xfer;
399 int ret;
400
401 if (!_adb_dev)
402 return -ENODEV;
403 pr_debug("adb_write(%d)\n", count);
404
405 if (adb_lock(&dev->write_excl))
406 return -EBUSY;
407
408 while (count > 0) {
Manu Gautam8f687952011-09-30 15:07:36 +0530409 if (atomic_read(&dev->error)) {
Benoit Goby2b6862d2011-12-19 14:38:41 -0800410 pr_debug("adb_write dev->error\n");
411 r = -EIO;
412 break;
413 }
414
415 /* get an idle tx request to use */
416 req = 0;
417 ret = wait_event_interruptible(dev->write_wq,
Manu Gautam8f687952011-09-30 15:07:36 +0530418 ((req = adb_req_get(dev, &dev->tx_idle)) ||
419 atomic_read(&dev->error)));
Benoit Goby2b6862d2011-12-19 14:38:41 -0800420
421 if (ret < 0) {
422 r = ret;
423 break;
424 }
425
426 if (req != 0) {
427 if (count > ADB_BULK_BUFFER_SIZE)
428 xfer = ADB_BULK_BUFFER_SIZE;
429 else
430 xfer = count;
431 if (copy_from_user(req->buf, buf, xfer)) {
432 r = -EFAULT;
433 break;
434 }
435
436 req->length = xfer;
437 ret = usb_ep_queue(dev->ep_in, req, GFP_ATOMIC);
438 if (ret < 0) {
439 pr_debug("adb_write: xfer error %d\n", ret);
Manu Gautam8f687952011-09-30 15:07:36 +0530440 atomic_set(&dev->error, 1);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800441 r = -EIO;
442 break;
443 }
444
445 buf += xfer;
446 count -= xfer;
447
448 /* zero this so we don't try to free it on error exit */
449 req = 0;
450 }
451 }
452
453 if (req)
454 adb_req_put(dev, &dev->tx_idle, req);
455
Pavankumar Kondetie8757ca2013-02-08 14:16:37 +0530456 if (atomic_read(&dev->error))
457 wake_up(&dev->read_wq);
458
Benoit Goby2b6862d2011-12-19 14:38:41 -0800459 adb_unlock(&dev->write_excl);
460 pr_debug("adb_write returning %d\n", r);
461 return r;
462}
463
464static int adb_open(struct inode *ip, struct file *fp)
465{
Pavankumar Kondeti755e4872013-02-28 10:11:37 +0530466 static DEFINE_RATELIMIT_STATE(rl, 10*HZ, 1);
467
468 if (__ratelimit(&rl))
469 pr_info("adb_open\n");
Benoit Goby2b6862d2011-12-19 14:38:41 -0800470 if (!_adb_dev)
471 return -ENODEV;
472
473 if (adb_lock(&_adb_dev->open_excl))
474 return -EBUSY;
475
476 fp->private_data = _adb_dev;
477
478 /* clear the error latch */
Manu Gautam8f687952011-09-30 15:07:36 +0530479 atomic_set(&_adb_dev->error, 0);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800480
Pavankumar Kondetic4d8e2c2012-10-26 11:15:54 +0530481 if (_adb_dev->close_notified) {
482 _adb_dev->close_notified = false;
Pavankumar Kondeti1aa235a2012-10-09 17:47:44 +0530483 adb_ready_callback();
Pavankumar Kondetic4d8e2c2012-10-26 11:15:54 +0530484 }
Benoit Goby80ba14d2012-03-19 18:56:52 -0700485
Pavankumar Kondeti1aa235a2012-10-09 17:47:44 +0530486 _adb_dev->notify_close = true;
Benoit Goby2b6862d2011-12-19 14:38:41 -0800487 return 0;
488}
489
490static int adb_release(struct inode *ip, struct file *fp)
491{
Pavankumar Kondeti755e4872013-02-28 10:11:37 +0530492 static DEFINE_RATELIMIT_STATE(rl, 10*HZ, 1);
493
494 if (__ratelimit(&rl))
495 pr_info("adb_release\n");
Benoit Goby80ba14d2012-03-19 18:56:52 -0700496
Pavankumar Kondeti1aa235a2012-10-09 17:47:44 +0530497 /*
498 * ADB daemon closes the device file after I/O error. The
499 * I/O error happen when Rx requests are flushed during
500 * cable disconnect or bus reset in configured state. Disabling
501 * USB configuration and pull-up during these scenarios are
502 * undesired. We want to force bus reset only for certain
503 * commands like "adb root" and "adb usb".
504 */
Pavankumar Kondetic4d8e2c2012-10-26 11:15:54 +0530505 if (_adb_dev->notify_close) {
Pavankumar Kondeti1aa235a2012-10-09 17:47:44 +0530506 adb_closed_callback();
Pavankumar Kondetic4d8e2c2012-10-26 11:15:54 +0530507 _adb_dev->close_notified = true;
508 }
Benoit Goby80ba14d2012-03-19 18:56:52 -0700509
Benoit Goby2b6862d2011-12-19 14:38:41 -0800510 adb_unlock(&_adb_dev->open_excl);
511 return 0;
512}
513
514/* file operations for ADB device /dev/android_adb */
515static const struct file_operations adb_fops = {
516 .owner = THIS_MODULE,
517 .read = adb_read,
518 .write = adb_write,
519 .open = adb_open,
520 .release = adb_release,
521};
522
523static struct miscdevice adb_device = {
524 .minor = MISC_DYNAMIC_MINOR,
525 .name = adb_shortname,
526 .fops = &adb_fops,
527};
528
529
530
531
532static int
533adb_function_bind(struct usb_configuration *c, struct usb_function *f)
534{
535 struct usb_composite_dev *cdev = c->cdev;
536 struct adb_dev *dev = func_to_adb(f);
537 int id;
538 int ret;
539
540 dev->cdev = cdev;
541 DBG(cdev, "adb_function_bind dev: %p\n", dev);
542
543 /* allocate interface ID(s) */
544 id = usb_interface_id(c, f);
545 if (id < 0)
546 return id;
547 adb_interface_desc.bInterfaceNumber = id;
548
549 /* allocate endpoints */
550 ret = adb_create_bulk_endpoints(dev, &adb_fullspeed_in_desc,
551 &adb_fullspeed_out_desc);
552 if (ret)
553 return ret;
554
555 /* support high speed hardware */
556 if (gadget_is_dualspeed(c->cdev->gadget)) {
557 adb_highspeed_in_desc.bEndpointAddress =
558 adb_fullspeed_in_desc.bEndpointAddress;
559 adb_highspeed_out_desc.bEndpointAddress =
560 adb_fullspeed_out_desc.bEndpointAddress;
561 }
Pavankumar Kondeti6f94bc92012-08-03 09:34:32 +0530562 /* support super speed hardware */
563 if (gadget_is_superspeed(c->cdev->gadget)) {
564 adb_superspeed_in_desc.bEndpointAddress =
565 adb_fullspeed_in_desc.bEndpointAddress;
566 adb_superspeed_out_desc.bEndpointAddress =
567 adb_fullspeed_out_desc.bEndpointAddress;
568 }
Benoit Goby2b6862d2011-12-19 14:38:41 -0800569
570 DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
571 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
572 f->name, dev->ep_in->name, dev->ep_out->name);
573 return 0;
574}
575
576static void
577adb_function_unbind(struct usb_configuration *c, struct usb_function *f)
578{
579 struct adb_dev *dev = func_to_adb(f);
580 struct usb_request *req;
581
582
Manu Gautam8f687952011-09-30 15:07:36 +0530583 atomic_set(&dev->online, 0);
584 atomic_set(&dev->error, 1);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800585
586 wake_up(&dev->read_wq);
587
588 adb_request_free(dev->rx_req, dev->ep_out);
589 while ((req = adb_req_get(dev, &dev->tx_idle)))
590 adb_request_free(req, dev->ep_in);
591}
592
593static int adb_function_set_alt(struct usb_function *f,
594 unsigned intf, unsigned alt)
595{
596 struct adb_dev *dev = func_to_adb(f);
597 struct usb_composite_dev *cdev = f->config->cdev;
598 int ret;
599
600 DBG(cdev, "adb_function_set_alt intf: %d alt: %d\n", intf, alt);
601
602 ret = config_ep_by_speed(cdev->gadget, f, dev->ep_in);
Tatyana Brokhmanebd3f392011-06-28 16:33:50 +0300603 if (ret) {
Tatyana Brokhman31ac3522011-06-28 15:33:50 +0200604 dev->ep_in->desc = NULL;
605 ERROR(cdev, "config_ep_by_speed failes for ep %s, result %d\n",
606 dev->ep_in->name, ret);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800607 return ret;
Tatyana Brokhman31ac3522011-06-28 15:33:50 +0200608 }
Benoit Goby2b6862d2011-12-19 14:38:41 -0800609 ret = usb_ep_enable(dev->ep_in);
Tatyana Brokhman31ac3522011-06-28 15:33:50 +0200610 if (ret) {
611 ERROR(cdev, "failed to enable ep %s, result %d\n",
612 dev->ep_in->name, ret);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800613 return ret;
Tatyana Brokhman31ac3522011-06-28 15:33:50 +0200614 }
Benoit Goby2b6862d2011-12-19 14:38:41 -0800615
616 ret = config_ep_by_speed(cdev->gadget, f, dev->ep_out);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800617 if (ret) {
Tatyana Brokhman31ac3522011-06-28 15:33:50 +0200618 dev->ep_out->desc = NULL;
619 ERROR(cdev, "config_ep_by_speed failes for ep %s, result %d\n",
620 dev->ep_out->name, ret);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800621 usb_ep_disable(dev->ep_in);
622 return ret;
623 }
Tatyana Brokhman31ac3522011-06-28 15:33:50 +0200624 ret = usb_ep_enable(dev->ep_out);
625 if (ret) {
626 ERROR(cdev, "failed to enable ep %s, result %d\n",
627 dev->ep_out->name, ret);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500628 usb_ep_disable(dev->ep_in);
629 return ret;
630 }
Manu Gautam8f687952011-09-30 15:07:36 +0530631 atomic_set(&dev->online, 1);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800632
633 /* readers may be blocked waiting for us to go online */
634 wake_up(&dev->read_wq);
635 return 0;
636}
637
638static void adb_function_disable(struct usb_function *f)
639{
640 struct adb_dev *dev = func_to_adb(f);
641 struct usb_composite_dev *cdev = dev->cdev;
642
643 DBG(cdev, "adb_function_disable cdev %p\n", cdev);
Pavankumar Kondeti1aa235a2012-10-09 17:47:44 +0530644 /*
645 * Bus reset happened or cable disconnected. No
646 * need to disable the configuration now. We will
647 * set noify_close to true when device file is re-opened.
648 */
649 dev->notify_close = false;
Manu Gautam8f687952011-09-30 15:07:36 +0530650 atomic_set(&dev->online, 0);
651 atomic_set(&dev->error, 1);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800652 usb_ep_disable(dev->ep_in);
653 usb_ep_disable(dev->ep_out);
654
655 /* readers may be blocked waiting for us to go online */
656 wake_up(&dev->read_wq);
657
658 VDBG(cdev, "%s disabled\n", dev->function.name);
659}
660
661static int adb_bind_config(struct usb_configuration *c)
662{
663 struct adb_dev *dev = _adb_dev;
664
Mayank Ranace1aaa52013-06-12 12:29:54 +0530665 pr_debug("adb_bind_config\n");
Benoit Goby2b6862d2011-12-19 14:38:41 -0800666
667 dev->cdev = c->cdev;
668 dev->function.name = "adb";
669 dev->function.descriptors = fs_adb_descs;
670 dev->function.hs_descriptors = hs_adb_descs;
Pavankumar Kondeti6f94bc92012-08-03 09:34:32 +0530671 if (gadget_is_superspeed(c->cdev->gadget))
672 dev->function.ss_descriptors = ss_adb_descs;
Benoit Goby2b6862d2011-12-19 14:38:41 -0800673 dev->function.bind = adb_function_bind;
674 dev->function.unbind = adb_function_unbind;
675 dev->function.set_alt = adb_function_set_alt;
676 dev->function.disable = adb_function_disable;
677
678 return usb_add_function(c, &dev->function);
679}
680
681static int adb_setup(void)
682{
683 struct adb_dev *dev;
684 int ret;
685
686 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
687 if (!dev)
688 return -ENOMEM;
689
690 spin_lock_init(&dev->lock);
691
692 init_waitqueue_head(&dev->read_wq);
693 init_waitqueue_head(&dev->write_wq);
694
695 atomic_set(&dev->open_excl, 0);
696 atomic_set(&dev->read_excl, 0);
697 atomic_set(&dev->write_excl, 0);
Pavankumar Kondetic4d8e2c2012-10-26 11:15:54 +0530698
699 /* config is disabled by default if adb is present. */
700 dev->close_notified = true;
Benoit Goby2b6862d2011-12-19 14:38:41 -0800701
Benoit Goby2b6862d2011-12-19 14:38:41 -0800702 INIT_LIST_HEAD(&dev->tx_idle);
703
704 _adb_dev = dev;
705
706 ret = misc_register(&adb_device);
707 if (ret)
708 goto err;
709
710 return 0;
711
712err:
713 kfree(dev);
714 printk(KERN_ERR "adb gadget driver failed to initialize\n");
715 return ret;
716}
717
718static void adb_cleanup(void)
719{
720 misc_deregister(&adb_device);
721
722 kfree(_adb_dev);
723 _adb_dev = NULL;
724}