blob: 7966a79a68550f1c031ab74d47640895339e8c04 [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;
Benoit Goby2b6862d2011-12-19 14:38:41 -080059};
60
61static struct usb_interface_descriptor adb_interface_desc = {
62 .bLength = USB_DT_INTERFACE_SIZE,
63 .bDescriptorType = USB_DT_INTERFACE,
64 .bInterfaceNumber = 0,
65 .bNumEndpoints = 2,
66 .bInterfaceClass = 0xFF,
67 .bInterfaceSubClass = 0x42,
68 .bInterfaceProtocol = 1,
69};
70
71static struct usb_endpoint_descriptor adb_highspeed_in_desc = {
72 .bLength = USB_DT_ENDPOINT_SIZE,
73 .bDescriptorType = USB_DT_ENDPOINT,
74 .bEndpointAddress = USB_DIR_IN,
75 .bmAttributes = USB_ENDPOINT_XFER_BULK,
76 .wMaxPacketSize = __constant_cpu_to_le16(512),
77};
78
79static struct usb_endpoint_descriptor adb_highspeed_out_desc = {
80 .bLength = USB_DT_ENDPOINT_SIZE,
81 .bDescriptorType = USB_DT_ENDPOINT,
82 .bEndpointAddress = USB_DIR_OUT,
83 .bmAttributes = USB_ENDPOINT_XFER_BULK,
84 .wMaxPacketSize = __constant_cpu_to_le16(512),
85};
86
87static struct usb_endpoint_descriptor adb_fullspeed_in_desc = {
88 .bLength = USB_DT_ENDPOINT_SIZE,
89 .bDescriptorType = USB_DT_ENDPOINT,
90 .bEndpointAddress = USB_DIR_IN,
91 .bmAttributes = USB_ENDPOINT_XFER_BULK,
92};
93
94static struct usb_endpoint_descriptor adb_fullspeed_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};
100
101static struct usb_descriptor_header *fs_adb_descs[] = {
102 (struct usb_descriptor_header *) &adb_interface_desc,
103 (struct usb_descriptor_header *) &adb_fullspeed_in_desc,
104 (struct usb_descriptor_header *) &adb_fullspeed_out_desc,
105 NULL,
106};
107
108static struct usb_descriptor_header *hs_adb_descs[] = {
109 (struct usb_descriptor_header *) &adb_interface_desc,
110 (struct usb_descriptor_header *) &adb_highspeed_in_desc,
111 (struct usb_descriptor_header *) &adb_highspeed_out_desc,
112 NULL,
113};
114
Benoit Goby80ba14d2012-03-19 18:56:52 -0700115static void adb_ready_callback(void);
116static void adb_closed_callback(void);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800117
118/* temporary variable used between adb_open() and adb_gadget_bind() */
119static struct adb_dev *_adb_dev;
120
121static inline struct adb_dev *func_to_adb(struct usb_function *f)
122{
123 return container_of(f, struct adb_dev, function);
124}
125
126
127static struct usb_request *adb_request_new(struct usb_ep *ep, int buffer_size)
128{
129 struct usb_request *req = usb_ep_alloc_request(ep, GFP_KERNEL);
130 if (!req)
131 return NULL;
132
133 /* now allocate buffers for the requests */
134 req->buf = kmalloc(buffer_size, GFP_KERNEL);
135 if (!req->buf) {
136 usb_ep_free_request(ep, req);
137 return NULL;
138 }
139
140 return req;
141}
142
143static void adb_request_free(struct usb_request *req, struct usb_ep *ep)
144{
145 if (req) {
146 kfree(req->buf);
147 usb_ep_free_request(ep, req);
148 }
149}
150
151static inline int adb_lock(atomic_t *excl)
152{
153 if (atomic_inc_return(excl) == 1) {
154 return 0;
155 } else {
156 atomic_dec(excl);
157 return -1;
158 }
159}
160
161static inline void adb_unlock(atomic_t *excl)
162{
163 atomic_dec(excl);
164}
165
166/* add a request to the tail of a list */
167void adb_req_put(struct adb_dev *dev, struct list_head *head,
168 struct usb_request *req)
169{
170 unsigned long flags;
171
172 spin_lock_irqsave(&dev->lock, flags);
173 list_add_tail(&req->list, head);
174 spin_unlock_irqrestore(&dev->lock, flags);
175}
176
177/* remove a request from the head of a list */
178struct usb_request *adb_req_get(struct adb_dev *dev, struct list_head *head)
179{
180 unsigned long flags;
181 struct usb_request *req;
182
183 spin_lock_irqsave(&dev->lock, flags);
184 if (list_empty(head)) {
185 req = 0;
186 } else {
187 req = list_first_entry(head, struct usb_request, list);
188 list_del(&req->list);
189 }
190 spin_unlock_irqrestore(&dev->lock, flags);
191 return req;
192}
193
194static void adb_complete_in(struct usb_ep *ep, struct usb_request *req)
195{
196 struct adb_dev *dev = _adb_dev;
197
198 if (req->status != 0)
Manu Gautam8f687952011-09-30 15:07:36 +0530199 atomic_set(&dev->error, 1);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800200
201 adb_req_put(dev, &dev->tx_idle, req);
202
203 wake_up(&dev->write_wq);
204}
205
206static void adb_complete_out(struct usb_ep *ep, struct usb_request *req)
207{
208 struct adb_dev *dev = _adb_dev;
209
210 dev->rx_done = 1;
Colin Cross8492aa12012-03-08 17:57:51 -0800211 if (req->status != 0 && req->status != -ECONNRESET)
Manu Gautam8f687952011-09-30 15:07:36 +0530212 atomic_set(&dev->error, 1);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800213
214 wake_up(&dev->read_wq);
215}
216
217static int adb_create_bulk_endpoints(struct adb_dev *dev,
218 struct usb_endpoint_descriptor *in_desc,
219 struct usb_endpoint_descriptor *out_desc)
220{
221 struct usb_composite_dev *cdev = dev->cdev;
222 struct usb_request *req;
223 struct usb_ep *ep;
224 int i;
225
226 DBG(cdev, "create_bulk_endpoints dev: %p\n", dev);
227
228 ep = usb_ep_autoconfig(cdev->gadget, in_desc);
229 if (!ep) {
230 DBG(cdev, "usb_ep_autoconfig for ep_in failed\n");
231 return -ENODEV;
232 }
233 DBG(cdev, "usb_ep_autoconfig for ep_in got %s\n", ep->name);
234 ep->driver_data = dev; /* claim the endpoint */
235 dev->ep_in = ep;
236
237 ep = usb_ep_autoconfig(cdev->gadget, out_desc);
238 if (!ep) {
239 DBG(cdev, "usb_ep_autoconfig for ep_out failed\n");
240 return -ENODEV;
241 }
242 DBG(cdev, "usb_ep_autoconfig for adb ep_out got %s\n", ep->name);
243 ep->driver_data = dev; /* claim the endpoint */
244 dev->ep_out = ep;
245
246 /* now allocate requests for our endpoints */
247 req = adb_request_new(dev->ep_out, ADB_BULK_BUFFER_SIZE);
248 if (!req)
249 goto fail;
250 req->complete = adb_complete_out;
251 dev->rx_req = req;
252
253 for (i = 0; i < TX_REQ_MAX; i++) {
254 req = adb_request_new(dev->ep_in, ADB_BULK_BUFFER_SIZE);
255 if (!req)
256 goto fail;
257 req->complete = adb_complete_in;
258 adb_req_put(dev, &dev->tx_idle, req);
259 }
260
261 return 0;
262
263fail:
264 printk(KERN_ERR "adb_bind() could not allocate requests\n");
265 return -1;
266}
267
268static ssize_t adb_read(struct file *fp, char __user *buf,
269 size_t count, loff_t *pos)
270{
271 struct adb_dev *dev = fp->private_data;
272 struct usb_request *req;
273 int r = count, xfer;
274 int ret;
275
276 pr_debug("adb_read(%d)\n", count);
277 if (!_adb_dev)
278 return -ENODEV;
279
280 if (count > ADB_BULK_BUFFER_SIZE)
281 return -EINVAL;
282
283 if (adb_lock(&dev->read_excl))
284 return -EBUSY;
285
286 /* we will block until we're online */
Manu Gautam8f687952011-09-30 15:07:36 +0530287 while (!(atomic_read(&dev->online) || atomic_read(&dev->error))) {
Benoit Goby2b6862d2011-12-19 14:38:41 -0800288 pr_debug("adb_read: waiting for online state\n");
289 ret = wait_event_interruptible(dev->read_wq,
Manu Gautam8f687952011-09-30 15:07:36 +0530290 (atomic_read(&dev->online) ||
291 atomic_read(&dev->error)));
Benoit Goby2b6862d2011-12-19 14:38:41 -0800292 if (ret < 0) {
293 adb_unlock(&dev->read_excl);
294 return ret;
295 }
296 }
Manu Gautam8f687952011-09-30 15:07:36 +0530297 if (atomic_read(&dev->error)) {
Benoit Goby2b6862d2011-12-19 14:38:41 -0800298 r = -EIO;
299 goto done;
300 }
301
302requeue_req:
303 /* queue a request */
304 req = dev->rx_req;
Ido Shayevitzf73ad6c2012-08-23 05:30:04 +0300305 req->length = ADB_BULK_BUFFER_SIZE;
Benoit Goby2b6862d2011-12-19 14:38:41 -0800306 dev->rx_done = 0;
307 ret = usb_ep_queue(dev->ep_out, req, GFP_ATOMIC);
308 if (ret < 0) {
309 pr_debug("adb_read: failed to queue req %p (%d)\n", req, ret);
310 r = -EIO;
Manu Gautam8f687952011-09-30 15:07:36 +0530311 atomic_set(&dev->error, 1);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800312 goto done;
313 } else {
314 pr_debug("rx %p queue\n", req);
315 }
316
317 /* wait for a request to complete */
318 ret = wait_event_interruptible(dev->read_wq, dev->rx_done);
319 if (ret < 0) {
Colin Cross42582f22012-03-05 13:29:45 -0800320 if (ret != -ERESTARTSYS)
Manu Gautam8f687952011-09-30 15:07:36 +0530321 atomic_set(&dev->error, 1);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800322 r = ret;
323 usb_ep_dequeue(dev->ep_out, req);
324 goto done;
325 }
Manu Gautam8f687952011-09-30 15:07:36 +0530326 if (!atomic_read(&dev->error)) {
Benoit Goby2b6862d2011-12-19 14:38:41 -0800327 /* If we got a 0-len packet, throw it back and try again. */
328 if (req->actual == 0)
329 goto requeue_req;
330
331 pr_debug("rx %p %d\n", req, req->actual);
332 xfer = (req->actual < count) ? req->actual : count;
333 if (copy_to_user(buf, req->buf, xfer))
334 r = -EFAULT;
335
336 } else
337 r = -EIO;
338
339done:
340 adb_unlock(&dev->read_excl);
341 pr_debug("adb_read returning %d\n", r);
342 return r;
343}
344
345static ssize_t adb_write(struct file *fp, const char __user *buf,
346 size_t count, loff_t *pos)
347{
348 struct adb_dev *dev = fp->private_data;
349 struct usb_request *req = 0;
350 int r = count, xfer;
351 int ret;
352
353 if (!_adb_dev)
354 return -ENODEV;
355 pr_debug("adb_write(%d)\n", count);
356
357 if (adb_lock(&dev->write_excl))
358 return -EBUSY;
359
360 while (count > 0) {
Manu Gautam8f687952011-09-30 15:07:36 +0530361 if (atomic_read(&dev->error)) {
Benoit Goby2b6862d2011-12-19 14:38:41 -0800362 pr_debug("adb_write dev->error\n");
363 r = -EIO;
364 break;
365 }
366
367 /* get an idle tx request to use */
368 req = 0;
369 ret = wait_event_interruptible(dev->write_wq,
Manu Gautam8f687952011-09-30 15:07:36 +0530370 ((req = adb_req_get(dev, &dev->tx_idle)) ||
371 atomic_read(&dev->error)));
Benoit Goby2b6862d2011-12-19 14:38:41 -0800372
373 if (ret < 0) {
374 r = ret;
375 break;
376 }
377
378 if (req != 0) {
379 if (count > ADB_BULK_BUFFER_SIZE)
380 xfer = ADB_BULK_BUFFER_SIZE;
381 else
382 xfer = count;
383 if (copy_from_user(req->buf, buf, xfer)) {
384 r = -EFAULT;
385 break;
386 }
387
388 req->length = xfer;
389 ret = usb_ep_queue(dev->ep_in, req, GFP_ATOMIC);
390 if (ret < 0) {
391 pr_debug("adb_write: xfer error %d\n", ret);
Manu Gautam8f687952011-09-30 15:07:36 +0530392 atomic_set(&dev->error, 1);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800393 r = -EIO;
394 break;
395 }
396
397 buf += xfer;
398 count -= xfer;
399
400 /* zero this so we don't try to free it on error exit */
401 req = 0;
402 }
403 }
404
405 if (req)
406 adb_req_put(dev, &dev->tx_idle, req);
407
408 adb_unlock(&dev->write_excl);
409 pr_debug("adb_write returning %d\n", r);
410 return r;
411}
412
413static int adb_open(struct inode *ip, struct file *fp)
414{
Benoit Goby80ba14d2012-03-19 18:56:52 -0700415 pr_info("adb_open\n");
Benoit Goby2b6862d2011-12-19 14:38:41 -0800416 if (!_adb_dev)
417 return -ENODEV;
418
419 if (adb_lock(&_adb_dev->open_excl))
420 return -EBUSY;
421
422 fp->private_data = _adb_dev;
423
424 /* clear the error latch */
Manu Gautam8f687952011-09-30 15:07:36 +0530425 atomic_set(&_adb_dev->error, 0);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800426
Pavankumar Kondeti1aa235a2012-10-09 17:47:44 +0530427 if (_adb_dev->notify_close)
428 adb_ready_callback();
Benoit Goby80ba14d2012-03-19 18:56:52 -0700429
Pavankumar Kondeti1aa235a2012-10-09 17:47:44 +0530430 _adb_dev->notify_close = true;
Benoit Goby2b6862d2011-12-19 14:38:41 -0800431 return 0;
432}
433
434static int adb_release(struct inode *ip, struct file *fp)
435{
Benoit Goby80ba14d2012-03-19 18:56:52 -0700436 pr_info("adb_release\n");
437
Pavankumar Kondeti1aa235a2012-10-09 17:47:44 +0530438 /*
439 * ADB daemon closes the device file after I/O error. The
440 * I/O error happen when Rx requests are flushed during
441 * cable disconnect or bus reset in configured state. Disabling
442 * USB configuration and pull-up during these scenarios are
443 * undesired. We want to force bus reset only for certain
444 * commands like "adb root" and "adb usb".
445 */
446 if (_adb_dev->notify_close)
447 adb_closed_callback();
Benoit Goby80ba14d2012-03-19 18:56:52 -0700448
Benoit Goby2b6862d2011-12-19 14:38:41 -0800449 adb_unlock(&_adb_dev->open_excl);
450 return 0;
451}
452
453/* file operations for ADB device /dev/android_adb */
454static const struct file_operations adb_fops = {
455 .owner = THIS_MODULE,
456 .read = adb_read,
457 .write = adb_write,
458 .open = adb_open,
459 .release = adb_release,
460};
461
462static struct miscdevice adb_device = {
463 .minor = MISC_DYNAMIC_MINOR,
464 .name = adb_shortname,
465 .fops = &adb_fops,
466};
467
468
469
470
471static int
472adb_function_bind(struct usb_configuration *c, struct usb_function *f)
473{
474 struct usb_composite_dev *cdev = c->cdev;
475 struct adb_dev *dev = func_to_adb(f);
476 int id;
477 int ret;
478
479 dev->cdev = cdev;
480 DBG(cdev, "adb_function_bind dev: %p\n", dev);
481
482 /* allocate interface ID(s) */
483 id = usb_interface_id(c, f);
484 if (id < 0)
485 return id;
486 adb_interface_desc.bInterfaceNumber = id;
487
488 /* allocate endpoints */
489 ret = adb_create_bulk_endpoints(dev, &adb_fullspeed_in_desc,
490 &adb_fullspeed_out_desc);
491 if (ret)
492 return ret;
493
494 /* support high speed hardware */
495 if (gadget_is_dualspeed(c->cdev->gadget)) {
496 adb_highspeed_in_desc.bEndpointAddress =
497 adb_fullspeed_in_desc.bEndpointAddress;
498 adb_highspeed_out_desc.bEndpointAddress =
499 adb_fullspeed_out_desc.bEndpointAddress;
500 }
501
502 DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
503 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
504 f->name, dev->ep_in->name, dev->ep_out->name);
505 return 0;
506}
507
508static void
509adb_function_unbind(struct usb_configuration *c, struct usb_function *f)
510{
511 struct adb_dev *dev = func_to_adb(f);
512 struct usb_request *req;
513
514
Manu Gautam8f687952011-09-30 15:07:36 +0530515 atomic_set(&dev->online, 0);
516 atomic_set(&dev->error, 1);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800517
518 wake_up(&dev->read_wq);
519
520 adb_request_free(dev->rx_req, dev->ep_out);
521 while ((req = adb_req_get(dev, &dev->tx_idle)))
522 adb_request_free(req, dev->ep_in);
523}
524
525static int adb_function_set_alt(struct usb_function *f,
526 unsigned intf, unsigned alt)
527{
528 struct adb_dev *dev = func_to_adb(f);
529 struct usb_composite_dev *cdev = f->config->cdev;
530 int ret;
531
532 DBG(cdev, "adb_function_set_alt intf: %d alt: %d\n", intf, alt);
533
534 ret = config_ep_by_speed(cdev->gadget, f, dev->ep_in);
Tatyana Brokhmanebd3f392011-06-28 16:33:50 +0300535 if (ret) {
Tatyana Brokhman31ac3522011-06-28 15:33:50 +0200536 dev->ep_in->desc = NULL;
537 ERROR(cdev, "config_ep_by_speed failes for ep %s, result %d\n",
538 dev->ep_in->name, ret);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800539 return ret;
Tatyana Brokhman31ac3522011-06-28 15:33:50 +0200540 }
Benoit Goby2b6862d2011-12-19 14:38:41 -0800541 ret = usb_ep_enable(dev->ep_in);
Tatyana Brokhman31ac3522011-06-28 15:33:50 +0200542 if (ret) {
543 ERROR(cdev, "failed to enable ep %s, result %d\n",
544 dev->ep_in->name, ret);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800545 return ret;
Tatyana Brokhman31ac3522011-06-28 15:33:50 +0200546 }
Benoit Goby2b6862d2011-12-19 14:38:41 -0800547
548 ret = config_ep_by_speed(cdev->gadget, f, dev->ep_out);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800549 if (ret) {
Tatyana Brokhman31ac3522011-06-28 15:33:50 +0200550 dev->ep_out->desc = NULL;
551 ERROR(cdev, "config_ep_by_speed failes for ep %s, result %d\n",
552 dev->ep_out->name, ret);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800553 usb_ep_disable(dev->ep_in);
554 return ret;
555 }
Tatyana Brokhman31ac3522011-06-28 15:33:50 +0200556 ret = usb_ep_enable(dev->ep_out);
557 if (ret) {
558 ERROR(cdev, "failed to enable ep %s, result %d\n",
559 dev->ep_out->name, ret);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500560 usb_ep_disable(dev->ep_in);
561 return ret;
562 }
Manu Gautam8f687952011-09-30 15:07:36 +0530563 atomic_set(&dev->online, 1);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800564
565 /* readers may be blocked waiting for us to go online */
566 wake_up(&dev->read_wq);
567 return 0;
568}
569
570static void adb_function_disable(struct usb_function *f)
571{
572 struct adb_dev *dev = func_to_adb(f);
573 struct usb_composite_dev *cdev = dev->cdev;
574
575 DBG(cdev, "adb_function_disable cdev %p\n", cdev);
Pavankumar Kondeti1aa235a2012-10-09 17:47:44 +0530576 /*
577 * Bus reset happened or cable disconnected. No
578 * need to disable the configuration now. We will
579 * set noify_close to true when device file is re-opened.
580 */
581 dev->notify_close = false;
Manu Gautam8f687952011-09-30 15:07:36 +0530582 atomic_set(&dev->online, 0);
583 atomic_set(&dev->error, 1);
Benoit Goby2b6862d2011-12-19 14:38:41 -0800584 usb_ep_disable(dev->ep_in);
585 usb_ep_disable(dev->ep_out);
586
587 /* readers may be blocked waiting for us to go online */
588 wake_up(&dev->read_wq);
589
590 VDBG(cdev, "%s disabled\n", dev->function.name);
591}
592
593static int adb_bind_config(struct usb_configuration *c)
594{
595 struct adb_dev *dev = _adb_dev;
596
597 printk(KERN_INFO "adb_bind_config\n");
598
599 dev->cdev = c->cdev;
600 dev->function.name = "adb";
601 dev->function.descriptors = fs_adb_descs;
602 dev->function.hs_descriptors = hs_adb_descs;
603 dev->function.bind = adb_function_bind;
604 dev->function.unbind = adb_function_unbind;
605 dev->function.set_alt = adb_function_set_alt;
606 dev->function.disable = adb_function_disable;
607
608 return usb_add_function(c, &dev->function);
609}
610
611static int adb_setup(void)
612{
613 struct adb_dev *dev;
614 int ret;
615
616 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
617 if (!dev)
618 return -ENOMEM;
619
620 spin_lock_init(&dev->lock);
621
622 init_waitqueue_head(&dev->read_wq);
623 init_waitqueue_head(&dev->write_wq);
624
625 atomic_set(&dev->open_excl, 0);
626 atomic_set(&dev->read_excl, 0);
627 atomic_set(&dev->write_excl, 0);
Pavankumar Kondeti1aa235a2012-10-09 17:47:44 +0530628 dev->notify_close = true;
Benoit Goby2b6862d2011-12-19 14:38:41 -0800629
Benoit Goby2b6862d2011-12-19 14:38:41 -0800630 INIT_LIST_HEAD(&dev->tx_idle);
631
632 _adb_dev = dev;
633
634 ret = misc_register(&adb_device);
635 if (ret)
636 goto err;
637
638 return 0;
639
640err:
641 kfree(dev);
642 printk(KERN_ERR "adb gadget driver failed to initialize\n");
643 return ret;
644}
645
646static void adb_cleanup(void)
647{
648 misc_deregister(&adb_device);
649
650 kfree(_adb_dev);
651 _adb_dev = NULL;
652}