blob: 4445dc288319a91b8f3812b11e3ba51793d31783 [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
Anh Nguyen64376432012-10-17 16:08:48 -070031#define DEBUG 1
Benoit Goby2b6862d2011-12-19 14:38:41 -080032/* 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 Kondeti53111082012-10-09 17:47:44 +053058 bool notify_close;
Pavankumar Kondeti2f2e4302012-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
72static struct usb_endpoint_descriptor adb_highspeed_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(512),
78};
79
80static struct usb_endpoint_descriptor adb_highspeed_out_desc = {
81 .bLength = USB_DT_ENDPOINT_SIZE,
82 .bDescriptorType = USB_DT_ENDPOINT,
83 .bEndpointAddress = USB_DIR_OUT,
84 .bmAttributes = USB_ENDPOINT_XFER_BULK,
85 .wMaxPacketSize = __constant_cpu_to_le16(512),
86};
87
88static struct usb_endpoint_descriptor adb_fullspeed_in_desc = {
89 .bLength = USB_DT_ENDPOINT_SIZE,
90 .bDescriptorType = USB_DT_ENDPOINT,
91 .bEndpointAddress = USB_DIR_IN,
92 .bmAttributes = USB_ENDPOINT_XFER_BULK,
93};
94
95static struct usb_endpoint_descriptor adb_fullspeed_out_desc = {
96 .bLength = USB_DT_ENDPOINT_SIZE,
97 .bDescriptorType = USB_DT_ENDPOINT,
98 .bEndpointAddress = USB_DIR_OUT,
99 .bmAttributes = USB_ENDPOINT_XFER_BULK,
100};
101
102static struct usb_descriptor_header *fs_adb_descs[] = {
103 (struct usb_descriptor_header *) &adb_interface_desc,
104 (struct usb_descriptor_header *) &adb_fullspeed_in_desc,
105 (struct usb_descriptor_header *) &adb_fullspeed_out_desc,
106 NULL,
107};
108
109static struct usb_descriptor_header *hs_adb_descs[] = {
110 (struct usb_descriptor_header *) &adb_interface_desc,
111 (struct usb_descriptor_header *) &adb_highspeed_in_desc,
112 (struct usb_descriptor_header *) &adb_highspeed_out_desc,
113 NULL,
114};
115
Benoit Goby80ba14d2012-03-19 18:56:52 -0700116static void adb_ready_callback(void);
117static void adb_closed_callback(void);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800118
119/* temporary variable used between adb_open() and adb_gadget_bind() */
120static struct adb_dev *_adb_dev;
121
122static inline struct adb_dev *func_to_adb(struct usb_function *f)
123{
124 return container_of(f, struct adb_dev, function);
125}
126
127
128static struct usb_request *adb_request_new(struct usb_ep *ep, int buffer_size)
129{
130 struct usb_request *req = usb_ep_alloc_request(ep, GFP_KERNEL);
131 if (!req)
132 return NULL;
133
134 /* now allocate buffers for the requests */
135 req->buf = kmalloc(buffer_size, GFP_KERNEL);
136 if (!req->buf) {
137 usb_ep_free_request(ep, req);
138 return NULL;
139 }
140
141 return req;
142}
143
144static void adb_request_free(struct usb_request *req, struct usb_ep *ep)
145{
146 if (req) {
147 kfree(req->buf);
148 usb_ep_free_request(ep, req);
149 }
150}
151
152static inline int adb_lock(atomic_t *excl)
153{
154 if (atomic_inc_return(excl) == 1) {
155 return 0;
156 } else {
157 atomic_dec(excl);
158 return -1;
159 }
160}
161
162static inline void adb_unlock(atomic_t *excl)
163{
164 atomic_dec(excl);
165}
166
167/* add a request to the tail of a list */
168void adb_req_put(struct adb_dev *dev, struct list_head *head,
169 struct usb_request *req)
170{
171 unsigned long flags;
172
173 spin_lock_irqsave(&dev->lock, flags);
174 list_add_tail(&req->list, head);
175 spin_unlock_irqrestore(&dev->lock, flags);
176}
177
178/* remove a request from the head of a list */
179struct usb_request *adb_req_get(struct adb_dev *dev, struct list_head *head)
180{
181 unsigned long flags;
182 struct usb_request *req;
183
184 spin_lock_irqsave(&dev->lock, flags);
185 if (list_empty(head)) {
186 req = 0;
187 } else {
188 req = list_first_entry(head, struct usb_request, list);
189 list_del(&req->list);
190 }
191 spin_unlock_irqrestore(&dev->lock, flags);
192 return req;
193}
194
195static void adb_complete_in(struct usb_ep *ep, struct usb_request *req)
196{
197 struct adb_dev *dev = _adb_dev;
198
199 if (req->status != 0)
Manu Gautam8f687952011-09-30 15:07:36 +0530200 atomic_set(&dev->error, 1);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800201
202 adb_req_put(dev, &dev->tx_idle, req);
203
204 wake_up(&dev->write_wq);
205}
206
207static void adb_complete_out(struct usb_ep *ep, struct usb_request *req)
208{
209 struct adb_dev *dev = _adb_dev;
210
211 dev->rx_done = 1;
Colin Cross8492aa12012-03-08 17:57:51 -0800212 if (req->status != 0 && req->status != -ECONNRESET)
Manu Gautam8f687952011-09-30 15:07:36 +0530213 atomic_set(&dev->error, 1);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800214
215 wake_up(&dev->read_wq);
216}
217
218static int adb_create_bulk_endpoints(struct adb_dev *dev,
219 struct usb_endpoint_descriptor *in_desc,
220 struct usb_endpoint_descriptor *out_desc)
221{
222 struct usb_composite_dev *cdev = dev->cdev;
223 struct usb_request *req;
224 struct usb_ep *ep;
225 int i;
226
227 DBG(cdev, "create_bulk_endpoints dev: %p\n", dev);
228
229 ep = usb_ep_autoconfig(cdev->gadget, in_desc);
230 if (!ep) {
231 DBG(cdev, "usb_ep_autoconfig for ep_in failed\n");
232 return -ENODEV;
233 }
234 DBG(cdev, "usb_ep_autoconfig for ep_in got %s\n", ep->name);
235 ep->driver_data = dev; /* claim the endpoint */
236 dev->ep_in = ep;
237
238 ep = usb_ep_autoconfig(cdev->gadget, out_desc);
239 if (!ep) {
240 DBG(cdev, "usb_ep_autoconfig for ep_out failed\n");
241 return -ENODEV;
242 }
243 DBG(cdev, "usb_ep_autoconfig for adb ep_out got %s\n", ep->name);
244 ep->driver_data = dev; /* claim the endpoint */
245 dev->ep_out = ep;
246
247 /* now allocate requests for our endpoints */
248 req = adb_request_new(dev->ep_out, ADB_BULK_BUFFER_SIZE);
249 if (!req)
250 goto fail;
251 req->complete = adb_complete_out;
252 dev->rx_req = req;
253
254 for (i = 0; i < TX_REQ_MAX; i++) {
255 req = adb_request_new(dev->ep_in, ADB_BULK_BUFFER_SIZE);
256 if (!req)
257 goto fail;
258 req->complete = adb_complete_in;
259 adb_req_put(dev, &dev->tx_idle, req);
260 }
261
262 return 0;
263
264fail:
265 printk(KERN_ERR "adb_bind() could not allocate requests\n");
266 return -1;
267}
268
269static ssize_t adb_read(struct file *fp, char __user *buf,
270 size_t count, loff_t *pos)
271{
272 struct adb_dev *dev = fp->private_data;
273 struct usb_request *req;
274 int r = count, xfer;
275 int ret;
276
277 pr_debug("adb_read(%d)\n", count);
278 if (!_adb_dev)
279 return -ENODEV;
280
281 if (count > ADB_BULK_BUFFER_SIZE)
282 return -EINVAL;
283
284 if (adb_lock(&dev->read_excl))
285 return -EBUSY;
286
287 /* we will block until we're online */
Manu Gautam8f687952011-09-30 15:07:36 +0530288 while (!(atomic_read(&dev->online) || atomic_read(&dev->error))) {
Benoit Goby2b6862d2011-12-19 14:38:41 -0800289 pr_debug("adb_read: waiting for online state\n");
290 ret = wait_event_interruptible(dev->read_wq,
Manu Gautam8f687952011-09-30 15:07:36 +0530291 (atomic_read(&dev->online) ||
292 atomic_read(&dev->error)));
Benoit Goby2b6862d2011-12-19 14:38:41 -0800293 if (ret < 0) {
294 adb_unlock(&dev->read_excl);
295 return ret;
296 }
297 }
Manu Gautam8f687952011-09-30 15:07:36 +0530298 if (atomic_read(&dev->error)) {
Benoit Goby2b6862d2011-12-19 14:38:41 -0800299 r = -EIO;
300 goto done;
301 }
302
303requeue_req:
304 /* queue a request */
305 req = dev->rx_req;
306 req->length = count;
307 dev->rx_done = 0;
308 ret = usb_ep_queue(dev->ep_out, req, GFP_ATOMIC);
309 if (ret < 0) {
310 pr_debug("adb_read: failed to queue req %p (%d)\n", req, ret);
311 r = -EIO;
Manu Gautam8f687952011-09-30 15:07:36 +0530312 atomic_set(&dev->error, 1);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800313 goto done;
314 } else {
315 pr_debug("rx %p queue\n", req);
316 }
317
318 /* wait for a request to complete */
Pavankumar Kondetic9b36342013-02-08 14:16:37 +0530319 ret = wait_event_interruptible(dev->read_wq, dev->rx_done ||
320 atomic_read(&dev->error));
Benoit Goby2b6862d2011-12-19 14:38:41 -0800321 if (ret < 0) {
Colin Cross42582f22012-03-05 13:29:45 -0800322 if (ret != -ERESTARTSYS)
Manu Gautam8f687952011-09-30 15:07:36 +0530323 atomic_set(&dev->error, 1);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800324 r = ret;
325 usb_ep_dequeue(dev->ep_out, req);
326 goto done;
327 }
Manu Gautam8f687952011-09-30 15:07:36 +0530328 if (!atomic_read(&dev->error)) {
Benoit Goby2b6862d2011-12-19 14:38:41 -0800329 /* If we got a 0-len packet, throw it back and try again. */
330 if (req->actual == 0)
331 goto requeue_req;
332
333 pr_debug("rx %p %d\n", req, req->actual);
334 xfer = (req->actual < count) ? req->actual : count;
335 if (copy_to_user(buf, req->buf, xfer))
336 r = -EFAULT;
337
338 } else
339 r = -EIO;
340
341done:
Pavankumar Kondetic9b36342013-02-08 14:16:37 +0530342 if (atomic_read(&dev->error))
343 wake_up(&dev->write_wq);
344
Benoit Goby2b6862d2011-12-19 14:38:41 -0800345 adb_unlock(&dev->read_excl);
346 pr_debug("adb_read returning %d\n", r);
347 return r;
348}
349
350static ssize_t adb_write(struct file *fp, const char __user *buf,
351 size_t count, loff_t *pos)
352{
353 struct adb_dev *dev = fp->private_data;
354 struct usb_request *req = 0;
355 int r = count, xfer;
356 int ret;
357
358 if (!_adb_dev)
359 return -ENODEV;
360 pr_debug("adb_write(%d)\n", count);
361
362 if (adb_lock(&dev->write_excl))
363 return -EBUSY;
364
365 while (count > 0) {
Manu Gautam8f687952011-09-30 15:07:36 +0530366 if (atomic_read(&dev->error)) {
Benoit Goby2b6862d2011-12-19 14:38:41 -0800367 pr_debug("adb_write dev->error\n");
368 r = -EIO;
369 break;
370 }
371
372 /* get an idle tx request to use */
373 req = 0;
374 ret = wait_event_interruptible(dev->write_wq,
Manu Gautam8f687952011-09-30 15:07:36 +0530375 ((req = adb_req_get(dev, &dev->tx_idle)) ||
376 atomic_read(&dev->error)));
Benoit Goby2b6862d2011-12-19 14:38:41 -0800377
378 if (ret < 0) {
379 r = ret;
380 break;
381 }
382
383 if (req != 0) {
384 if (count > ADB_BULK_BUFFER_SIZE)
385 xfer = ADB_BULK_BUFFER_SIZE;
386 else
387 xfer = count;
388 if (copy_from_user(req->buf, buf, xfer)) {
389 r = -EFAULT;
390 break;
391 }
392
393 req->length = xfer;
394 ret = usb_ep_queue(dev->ep_in, req, GFP_ATOMIC);
395 if (ret < 0) {
396 pr_debug("adb_write: xfer error %d\n", ret);
Manu Gautam8f687952011-09-30 15:07:36 +0530397 atomic_set(&dev->error, 1);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800398 r = -EIO;
399 break;
400 }
401
402 buf += xfer;
403 count -= xfer;
404
405 /* zero this so we don't try to free it on error exit */
406 req = 0;
407 }
408 }
409
410 if (req)
411 adb_req_put(dev, &dev->tx_idle, req);
412
Pavankumar Kondetic9b36342013-02-08 14:16:37 +0530413 if (atomic_read(&dev->error))
414 wake_up(&dev->read_wq);
415
Benoit Goby2b6862d2011-12-19 14:38:41 -0800416 adb_unlock(&dev->write_excl);
417 pr_debug("adb_write returning %d\n", r);
418 return r;
419}
420
421static int adb_open(struct inode *ip, struct file *fp)
422{
Pavankumar Kondeti9f3b2ee2013-02-28 10:11:37 +0530423 static DEFINE_RATELIMIT_STATE(rl, 10*HZ, 1);
424
425 if (__ratelimit(&rl))
426 pr_info("adb_open\n");
Benoit Goby2b6862d2011-12-19 14:38:41 -0800427 if (!_adb_dev)
428 return -ENODEV;
429
430 if (adb_lock(&_adb_dev->open_excl))
431 return -EBUSY;
432
433 fp->private_data = _adb_dev;
434
435 /* clear the error latch */
Manu Gautam8f687952011-09-30 15:07:36 +0530436 atomic_set(&_adb_dev->error, 0);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800437
Pavankumar Kondeti2f2e4302012-10-26 11:15:54 +0530438 if (_adb_dev->close_notified) {
439 _adb_dev->close_notified = false;
Pavankumar Kondeti53111082012-10-09 17:47:44 +0530440 adb_ready_callback();
Pavankumar Kondeti2f2e4302012-10-26 11:15:54 +0530441 }
Benoit Goby80ba14d2012-03-19 18:56:52 -0700442
Pavankumar Kondeti53111082012-10-09 17:47:44 +0530443 _adb_dev->notify_close = true;
Benoit Goby2b6862d2011-12-19 14:38:41 -0800444 return 0;
445}
446
447static int adb_release(struct inode *ip, struct file *fp)
448{
Pavankumar Kondeti9f3b2ee2013-02-28 10:11:37 +0530449 static DEFINE_RATELIMIT_STATE(rl, 10*HZ, 1);
450
451 if (__ratelimit(&rl))
452 pr_info("adb_release\n");
Benoit Goby80ba14d2012-03-19 18:56:52 -0700453
Pavankumar Kondeti53111082012-10-09 17:47:44 +0530454 /*
455 * ADB daemon closes the device file after I/O error. The
456 * I/O error happen when Rx requests are flushed during
457 * cable disconnect or bus reset in configured state. Disabling
458 * USB configuration and pull-up during these scenarios are
459 * undesired. We want to force bus reset only for certain
460 * commands like "adb root" and "adb usb".
461 */
Pavankumar Kondeti2f2e4302012-10-26 11:15:54 +0530462 if (_adb_dev->notify_close) {
Pavankumar Kondeti53111082012-10-09 17:47:44 +0530463 adb_closed_callback();
Pavankumar Kondeti2f2e4302012-10-26 11:15:54 +0530464 _adb_dev->close_notified = true;
465 }
Benoit Goby80ba14d2012-03-19 18:56:52 -0700466
Benoit Goby2b6862d2011-12-19 14:38:41 -0800467 adb_unlock(&_adb_dev->open_excl);
468 return 0;
469}
470
471/* file operations for ADB device /dev/android_adb */
472static const struct file_operations adb_fops = {
473 .owner = THIS_MODULE,
474 .read = adb_read,
475 .write = adb_write,
476 .open = adb_open,
477 .release = adb_release,
478};
479
480static struct miscdevice adb_device = {
481 .minor = MISC_DYNAMIC_MINOR,
482 .name = adb_shortname,
483 .fops = &adb_fops,
484};
485
486
487
488
489static int
490adb_function_bind(struct usb_configuration *c, struct usb_function *f)
491{
492 struct usb_composite_dev *cdev = c->cdev;
493 struct adb_dev *dev = func_to_adb(f);
494 int id;
495 int ret;
496
497 dev->cdev = cdev;
498 DBG(cdev, "adb_function_bind dev: %p\n", dev);
499
500 /* allocate interface ID(s) */
501 id = usb_interface_id(c, f);
502 if (id < 0)
503 return id;
504 adb_interface_desc.bInterfaceNumber = id;
505
506 /* allocate endpoints */
507 ret = adb_create_bulk_endpoints(dev, &adb_fullspeed_in_desc,
508 &adb_fullspeed_out_desc);
509 if (ret)
510 return ret;
511
512 /* support high speed hardware */
513 if (gadget_is_dualspeed(c->cdev->gadget)) {
514 adb_highspeed_in_desc.bEndpointAddress =
515 adb_fullspeed_in_desc.bEndpointAddress;
516 adb_highspeed_out_desc.bEndpointAddress =
517 adb_fullspeed_out_desc.bEndpointAddress;
518 }
519
520 DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
521 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
522 f->name, dev->ep_in->name, dev->ep_out->name);
523 return 0;
524}
525
526static void
527adb_function_unbind(struct usb_configuration *c, struct usb_function *f)
528{
529 struct adb_dev *dev = func_to_adb(f);
530 struct usb_request *req;
531
532
Manu Gautam8f687952011-09-30 15:07:36 +0530533 atomic_set(&dev->online, 0);
534 atomic_set(&dev->error, 1);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800535
536 wake_up(&dev->read_wq);
537
538 adb_request_free(dev->rx_req, dev->ep_out);
539 while ((req = adb_req_get(dev, &dev->tx_idle)))
540 adb_request_free(req, dev->ep_in);
541}
542
543static int adb_function_set_alt(struct usb_function *f,
544 unsigned intf, unsigned alt)
545{
546 struct adb_dev *dev = func_to_adb(f);
547 struct usb_composite_dev *cdev = f->config->cdev;
548 int ret;
549
550 DBG(cdev, "adb_function_set_alt intf: %d alt: %d\n", intf, alt);
551
552 ret = config_ep_by_speed(cdev->gadget, f, dev->ep_in);
Tatyana Brokhmanebd3f392011-06-28 16:33:50 +0300553 if (ret) {
Tatyana Brokhman31ac3522011-06-28 15:33:50 +0200554 dev->ep_in->desc = NULL;
555 ERROR(cdev, "config_ep_by_speed failes for ep %s, result %d\n",
556 dev->ep_in->name, ret);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800557 return ret;
Tatyana Brokhman31ac3522011-06-28 15:33:50 +0200558 }
Benoit Goby2b6862d2011-12-19 14:38:41 -0800559 ret = usb_ep_enable(dev->ep_in);
Tatyana Brokhman31ac3522011-06-28 15:33:50 +0200560 if (ret) {
561 ERROR(cdev, "failed to enable ep %s, result %d\n",
562 dev->ep_in->name, ret);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800563 return ret;
Tatyana Brokhman31ac3522011-06-28 15:33:50 +0200564 }
Benoit Goby2b6862d2011-12-19 14:38:41 -0800565
566 ret = config_ep_by_speed(cdev->gadget, f, dev->ep_out);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800567 if (ret) {
Tatyana Brokhman31ac3522011-06-28 15:33:50 +0200568 dev->ep_out->desc = NULL;
569 ERROR(cdev, "config_ep_by_speed failes for ep %s, result %d\n",
570 dev->ep_out->name, ret);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800571 usb_ep_disable(dev->ep_in);
572 return ret;
573 }
Tatyana Brokhman31ac3522011-06-28 15:33:50 +0200574 ret = usb_ep_enable(dev->ep_out);
575 if (ret) {
576 ERROR(cdev, "failed to enable ep %s, result %d\n",
577 dev->ep_out->name, ret);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500578 usb_ep_disable(dev->ep_in);
579 return ret;
580 }
Manu Gautam8f687952011-09-30 15:07:36 +0530581 atomic_set(&dev->online, 1);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800582
583 /* readers may be blocked waiting for us to go online */
584 wake_up(&dev->read_wq);
585 return 0;
586}
587
588static void adb_function_disable(struct usb_function *f)
589{
590 struct adb_dev *dev = func_to_adb(f);
591 struct usb_composite_dev *cdev = dev->cdev;
592
593 DBG(cdev, "adb_function_disable cdev %p\n", cdev);
Pavankumar Kondeti53111082012-10-09 17:47:44 +0530594 /*
595 * Bus reset happened or cable disconnected. No
596 * need to disable the configuration now. We will
597 * set noify_close to true when device file is re-opened.
598 */
599 dev->notify_close = false;
Manu Gautam8f687952011-09-30 15:07:36 +0530600 atomic_set(&dev->online, 0);
601 atomic_set(&dev->error, 1);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800602 usb_ep_disable(dev->ep_in);
603 usb_ep_disable(dev->ep_out);
604
605 /* readers may be blocked waiting for us to go online */
606 wake_up(&dev->read_wq);
607
608 VDBG(cdev, "%s disabled\n", dev->function.name);
609}
610
611static int adb_bind_config(struct usb_configuration *c)
612{
613 struct adb_dev *dev = _adb_dev;
614
615 printk(KERN_INFO "adb_bind_config\n");
616
617 dev->cdev = c->cdev;
618 dev->function.name = "adb";
619 dev->function.descriptors = fs_adb_descs;
620 dev->function.hs_descriptors = hs_adb_descs;
621 dev->function.bind = adb_function_bind;
622 dev->function.unbind = adb_function_unbind;
623 dev->function.set_alt = adb_function_set_alt;
624 dev->function.disable = adb_function_disable;
625
626 return usb_add_function(c, &dev->function);
627}
628
629static int adb_setup(void)
630{
631 struct adb_dev *dev;
632 int ret;
633
634 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
635 if (!dev)
636 return -ENOMEM;
637
638 spin_lock_init(&dev->lock);
639
640 init_waitqueue_head(&dev->read_wq);
641 init_waitqueue_head(&dev->write_wq);
642
643 atomic_set(&dev->open_excl, 0);
644 atomic_set(&dev->read_excl, 0);
645 atomic_set(&dev->write_excl, 0);
Pavankumar Kondeti2f2e4302012-10-26 11:15:54 +0530646
647 /* config is disabled by default if adb is present. */
648 dev->close_notified = true;
Benoit Goby2b6862d2011-12-19 14:38:41 -0800649
Benoit Goby2b6862d2011-12-19 14:38:41 -0800650 INIT_LIST_HEAD(&dev->tx_idle);
651
652 _adb_dev = dev;
653
654 ret = misc_register(&adb_device);
655 if (ret)
656 goto err;
657
658 return 0;
659
660err:
661 kfree(dev);
662 printk(KERN_ERR "adb gadget driver failed to initialize\n");
663 return ret;
664}
665
666static void adb_cleanup(void)
667{
668 misc_deregister(&adb_device);
669
670 kfree(_adb_dev);
671 _adb_dev = NULL;
672}