blob: fe4455e50d10a0096d08fcb0af1c5f45729479ac [file] [log] [blame]
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -05001/*
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
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050018#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
Benoit Gobyaab96812011-04-19 20:37:33 -070030#define ADB_BULK_BUFFER_SIZE 4096
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050031
Mike Lockwooda9e8c442009-12-19 18:22:09 -050032/* number of tx requests to allocate */
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050033#define TX_REQ_MAX 4
34
Benoit Gobyaab96812011-04-19 20:37:33 -070035static const char adb_shortname[] = "android_adb";
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050036
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
45 int online;
46 int error;
47
48 atomic_t read_excl;
49 atomic_t write_excl;
50 atomic_t open_excl;
51
52 struct list_head tx_idle;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050053
54 wait_queue_head_t read_wq;
55 wait_queue_head_t write_wq;
Mike Lockwooda9e8c442009-12-19 18:22:09 -050056 struct usb_request *rx_req;
57 int rx_done;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -050058};
59
60static struct usb_interface_descriptor adb_interface_desc = {
61 .bLength = USB_DT_INTERFACE_SIZE,
62 .bDescriptorType = USB_DT_INTERFACE,
63 .bInterfaceNumber = 0,
64 .bNumEndpoints = 2,
65 .bInterfaceClass = 0xFF,
66 .bInterfaceSubClass = 0x42,
67 .bInterfaceProtocol = 1,
68};
69
70static struct usb_endpoint_descriptor adb_highspeed_in_desc = {
71 .bLength = USB_DT_ENDPOINT_SIZE,
72 .bDescriptorType = USB_DT_ENDPOINT,
73 .bEndpointAddress = USB_DIR_IN,
74 .bmAttributes = USB_ENDPOINT_XFER_BULK,
75 .wMaxPacketSize = __constant_cpu_to_le16(512),
76};
77
78static struct usb_endpoint_descriptor adb_highspeed_out_desc = {
79 .bLength = USB_DT_ENDPOINT_SIZE,
80 .bDescriptorType = USB_DT_ENDPOINT,
81 .bEndpointAddress = USB_DIR_OUT,
82 .bmAttributes = USB_ENDPOINT_XFER_BULK,
83 .wMaxPacketSize = __constant_cpu_to_le16(512),
84};
85
86static struct usb_endpoint_descriptor adb_fullspeed_in_desc = {
87 .bLength = USB_DT_ENDPOINT_SIZE,
88 .bDescriptorType = USB_DT_ENDPOINT,
89 .bEndpointAddress = USB_DIR_IN,
90 .bmAttributes = USB_ENDPOINT_XFER_BULK,
91};
92
93static struct usb_endpoint_descriptor adb_fullspeed_out_desc = {
94 .bLength = USB_DT_ENDPOINT_SIZE,
95 .bDescriptorType = USB_DT_ENDPOINT,
96 .bEndpointAddress = USB_DIR_OUT,
97 .bmAttributes = USB_ENDPOINT_XFER_BULK,
98};
99
100static struct usb_descriptor_header *fs_adb_descs[] = {
101 (struct usb_descriptor_header *) &adb_interface_desc,
102 (struct usb_descriptor_header *) &adb_fullspeed_in_desc,
103 (struct usb_descriptor_header *) &adb_fullspeed_out_desc,
104 NULL,
105};
106
107static struct usb_descriptor_header *hs_adb_descs[] = {
108 (struct usb_descriptor_header *) &adb_interface_desc,
109 (struct usb_descriptor_header *) &adb_highspeed_in_desc,
110 (struct usb_descriptor_header *) &adb_highspeed_out_desc,
111 NULL,
112};
113
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500114
115/* temporary variable used between adb_open() and adb_gadget_bind() */
116static struct adb_dev *_adb_dev;
117
Benoit Gobyaab96812011-04-19 20:37:33 -0700118static inline struct adb_dev *func_to_adb(struct usb_function *f)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500119{
120 return container_of(f, struct adb_dev, function);
121}
122
123
124static struct usb_request *adb_request_new(struct usb_ep *ep, int buffer_size)
125{
126 struct usb_request *req = usb_ep_alloc_request(ep, GFP_KERNEL);
127 if (!req)
128 return NULL;
129
130 /* now allocate buffers for the requests */
131 req->buf = kmalloc(buffer_size, GFP_KERNEL);
132 if (!req->buf) {
133 usb_ep_free_request(ep, req);
134 return NULL;
135 }
136
137 return req;
138}
139
140static void adb_request_free(struct usb_request *req, struct usb_ep *ep)
141{
142 if (req) {
143 kfree(req->buf);
144 usb_ep_free_request(ep, req);
145 }
146}
147
Benoit Gobyaab96812011-04-19 20:37:33 -0700148static inline int adb_lock(atomic_t *excl)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500149{
150 if (atomic_inc_return(excl) == 1) {
151 return 0;
152 } else {
153 atomic_dec(excl);
154 return -1;
155 }
156}
157
Benoit Gobyaab96812011-04-19 20:37:33 -0700158static inline void adb_unlock(atomic_t *excl)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500159{
160 atomic_dec(excl);
161}
162
163/* add a request to the tail of a list */
Benoit Gobyaab96812011-04-19 20:37:33 -0700164void adb_req_put(struct adb_dev *dev, struct list_head *head,
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500165 struct usb_request *req)
166{
167 unsigned long flags;
168
169 spin_lock_irqsave(&dev->lock, flags);
170 list_add_tail(&req->list, head);
171 spin_unlock_irqrestore(&dev->lock, flags);
172}
173
174/* remove a request from the head of a list */
Benoit Gobyaab96812011-04-19 20:37:33 -0700175struct usb_request *adb_req_get(struct adb_dev *dev, struct list_head *head)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500176{
177 unsigned long flags;
178 struct usb_request *req;
179
180 spin_lock_irqsave(&dev->lock, flags);
181 if (list_empty(head)) {
182 req = 0;
183 } else {
184 req = list_first_entry(head, struct usb_request, list);
185 list_del(&req->list);
186 }
187 spin_unlock_irqrestore(&dev->lock, flags);
188 return req;
189}
190
191static void adb_complete_in(struct usb_ep *ep, struct usb_request *req)
192{
193 struct adb_dev *dev = _adb_dev;
194
195 if (req->status != 0)
196 dev->error = 1;
197
Benoit Gobyaab96812011-04-19 20:37:33 -0700198 adb_req_put(dev, &dev->tx_idle, req);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500199
200 wake_up(&dev->write_wq);
201}
202
203static void adb_complete_out(struct usb_ep *ep, struct usb_request *req)
204{
205 struct adb_dev *dev = _adb_dev;
206
Mike Lockwooda9e8c442009-12-19 18:22:09 -0500207 dev->rx_done = 1;
208 if (req->status != 0)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500209 dev->error = 1;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500210
211 wake_up(&dev->read_wq);
212}
213
Benoit Gobyaab96812011-04-19 20:37:33 -0700214static int adb_create_bulk_endpoints(struct adb_dev *dev,
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500215 struct usb_endpoint_descriptor *in_desc,
216 struct usb_endpoint_descriptor *out_desc)
217{
218 struct usb_composite_dev *cdev = dev->cdev;
219 struct usb_request *req;
220 struct usb_ep *ep;
221 int i;
222
223 DBG(cdev, "create_bulk_endpoints dev: %p\n", dev);
224
225 ep = usb_ep_autoconfig(cdev->gadget, in_desc);
226 if (!ep) {
227 DBG(cdev, "usb_ep_autoconfig for ep_in failed\n");
228 return -ENODEV;
229 }
230 DBG(cdev, "usb_ep_autoconfig for ep_in got %s\n", ep->name);
Mike Lockwood789ef232010-01-12 10:33:59 -0800231 ep->driver_data = dev; /* claim the endpoint */
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500232 dev->ep_in = ep;
233
234 ep = usb_ep_autoconfig(cdev->gadget, out_desc);
235 if (!ep) {
236 DBG(cdev, "usb_ep_autoconfig for ep_out failed\n");
237 return -ENODEV;
238 }
239 DBG(cdev, "usb_ep_autoconfig for adb ep_out got %s\n", ep->name);
Mike Lockwood789ef232010-01-12 10:33:59 -0800240 ep->driver_data = dev; /* claim the endpoint */
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500241 dev->ep_out = ep;
242
243 /* now allocate requests for our endpoints */
Benoit Gobyaab96812011-04-19 20:37:33 -0700244 req = adb_request_new(dev->ep_out, ADB_BULK_BUFFER_SIZE);
Mike Lockwooda9e8c442009-12-19 18:22:09 -0500245 if (!req)
246 goto fail;
247 req->complete = adb_complete_out;
248 dev->rx_req = req;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500249
250 for (i = 0; i < TX_REQ_MAX; i++) {
Benoit Gobyaab96812011-04-19 20:37:33 -0700251 req = adb_request_new(dev->ep_in, ADB_BULK_BUFFER_SIZE);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500252 if (!req)
253 goto fail;
254 req->complete = adb_complete_in;
Benoit Gobyaab96812011-04-19 20:37:33 -0700255 adb_req_put(dev, &dev->tx_idle, req);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500256 }
257
258 return 0;
259
260fail:
261 printk(KERN_ERR "adb_bind() could not allocate requests\n");
262 return -1;
263}
264
265static ssize_t adb_read(struct file *fp, char __user *buf,
266 size_t count, loff_t *pos)
267{
268 struct adb_dev *dev = fp->private_data;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500269 struct usb_request *req;
270 int r = count, xfer;
271 int ret;
272
Benoit Gobyaab96812011-04-19 20:37:33 -0700273 pr_debug("adb_read(%d)\n", count);
274 if (!_adb_dev)
275 return -ENODEV;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500276
Benoit Gobyaab96812011-04-19 20:37:33 -0700277 if (count > ADB_BULK_BUFFER_SIZE)
Mike Lockwooda9e8c442009-12-19 18:22:09 -0500278 return -EINVAL;
279
Benoit Gobyaab96812011-04-19 20:37:33 -0700280 if (adb_lock(&dev->read_excl))
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500281 return -EBUSY;
282
283 /* we will block until we're online */
284 while (!(dev->online || dev->error)) {
Benoit Gobyaab96812011-04-19 20:37:33 -0700285 pr_debug("adb_read: waiting for online state\n");
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500286 ret = wait_event_interruptible(dev->read_wq,
287 (dev->online || dev->error));
288 if (ret < 0) {
Benoit Gobyaab96812011-04-19 20:37:33 -0700289 adb_unlock(&dev->read_excl);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500290 return ret;
291 }
292 }
Mike Lockwooda9e8c442009-12-19 18:22:09 -0500293 if (dev->error) {
294 r = -EIO;
295 goto done;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500296 }
297
Mike Lockwooda9e8c442009-12-19 18:22:09 -0500298requeue_req:
299 /* queue a request */
300 req = dev->rx_req;
301 req->length = count;
302 dev->rx_done = 0;
303 ret = usb_ep_queue(dev->ep_out, req, GFP_ATOMIC);
304 if (ret < 0) {
Benoit Gobyaab96812011-04-19 20:37:33 -0700305 pr_debug("adb_read: failed to queue req %p (%d)\n", req, ret);
Mike Lockwooda9e8c442009-12-19 18:22:09 -0500306 r = -EIO;
307 dev->error = 1;
308 goto done;
309 } else {
Benoit Gobyaab96812011-04-19 20:37:33 -0700310 pr_debug("rx %p queue\n", req);
Mike Lockwooda9e8c442009-12-19 18:22:09 -0500311 }
312
313 /* wait for a request to complete */
314 ret = wait_event_interruptible(dev->read_wq, dev->rx_done);
315 if (ret < 0) {
316 dev->error = 1;
317 r = ret;
Iliyan Malchev7d525032011-02-18 11:28:32 -0800318 usb_ep_dequeue(dev->ep_out, req);
Mike Lockwooda9e8c442009-12-19 18:22:09 -0500319 goto done;
320 }
321 if (!dev->error) {
322 /* If we got a 0-len packet, throw it back and try again. */
323 if (req->actual == 0)
324 goto requeue_req;
325
Benoit Gobyaab96812011-04-19 20:37:33 -0700326 pr_debug("rx %p %d\n", req, req->actual);
Mike Lockwooda9e8c442009-12-19 18:22:09 -0500327 xfer = (req->actual < count) ? req->actual : count;
328 if (copy_to_user(buf, req->buf, xfer))
329 r = -EFAULT;
Benoit Gobyaab96812011-04-19 20:37:33 -0700330
Mike Lockwooda9e8c442009-12-19 18:22:09 -0500331 } else
332 r = -EIO;
333
334done:
Benoit Gobyaab96812011-04-19 20:37:33 -0700335 adb_unlock(&dev->read_excl);
336 pr_debug("adb_read returning %d\n", r);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500337 return r;
338}
339
340static ssize_t adb_write(struct file *fp, const char __user *buf,
341 size_t count, loff_t *pos)
342{
343 struct adb_dev *dev = fp->private_data;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500344 struct usb_request *req = 0;
345 int r = count, xfer;
346 int ret;
347
Benoit Gobyaab96812011-04-19 20:37:33 -0700348 if (!_adb_dev)
349 return -ENODEV;
350 pr_debug("adb_write(%d)\n", count);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500351
Benoit Gobyaab96812011-04-19 20:37:33 -0700352 if (adb_lock(&dev->write_excl))
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500353 return -EBUSY;
354
355 while (count > 0) {
356 if (dev->error) {
Benoit Gobyaab96812011-04-19 20:37:33 -0700357 pr_debug("adb_write dev->error\n");
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500358 r = -EIO;
359 break;
360 }
361
362 /* get an idle tx request to use */
363 req = 0;
364 ret = wait_event_interruptible(dev->write_wq,
Benoit Gobyaab96812011-04-19 20:37:33 -0700365 (req = adb_req_get(dev, &dev->tx_idle)) || dev->error);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500366
367 if (ret < 0) {
368 r = ret;
369 break;
370 }
371
372 if (req != 0) {
Benoit Gobyaab96812011-04-19 20:37:33 -0700373 if (count > ADB_BULK_BUFFER_SIZE)
374 xfer = ADB_BULK_BUFFER_SIZE;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500375 else
376 xfer = count;
377 if (copy_from_user(req->buf, buf, xfer)) {
378 r = -EFAULT;
379 break;
380 }
381
382 req->length = xfer;
383 ret = usb_ep_queue(dev->ep_in, req, GFP_ATOMIC);
384 if (ret < 0) {
Benoit Gobyaab96812011-04-19 20:37:33 -0700385 pr_debug("adb_write: xfer error %d\n", ret);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500386 dev->error = 1;
387 r = -EIO;
388 break;
389 }
390
391 buf += xfer;
392 count -= xfer;
393
394 /* zero this so we don't try to free it on error exit */
395 req = 0;
396 }
397 }
398
399 if (req)
Benoit Gobyaab96812011-04-19 20:37:33 -0700400 adb_req_put(dev, &dev->tx_idle, req);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500401
Benoit Gobyaab96812011-04-19 20:37:33 -0700402 adb_unlock(&dev->write_excl);
403 pr_debug("adb_write returning %d\n", r);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500404 return r;
405}
406
407static int adb_open(struct inode *ip, struct file *fp)
408{
409 printk(KERN_INFO "adb_open\n");
Benoit Gobyaab96812011-04-19 20:37:33 -0700410 if (!_adb_dev)
411 return -ENODEV;
412
413 if (adb_lock(&_adb_dev->open_excl))
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500414 return -EBUSY;
415
416 fp->private_data = _adb_dev;
417
418 /* clear the error latch */
419 _adb_dev->error = 0;
420
421 return 0;
422}
423
424static int adb_release(struct inode *ip, struct file *fp)
425{
426 printk(KERN_INFO "adb_release\n");
Benoit Gobyaab96812011-04-19 20:37:33 -0700427 adb_unlock(&_adb_dev->open_excl);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500428 return 0;
429}
430
431/* file operations for ADB device /dev/android_adb */
432static struct file_operations adb_fops = {
433 .owner = THIS_MODULE,
434 .read = adb_read,
435 .write = adb_write,
436 .open = adb_open,
437 .release = adb_release,
438};
439
440static struct miscdevice adb_device = {
441 .minor = MISC_DYNAMIC_MINOR,
Benoit Gobyaab96812011-04-19 20:37:33 -0700442 .name = adb_shortname,
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500443 .fops = &adb_fops,
444};
445
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530446
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530447
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530448
449static int
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500450adb_function_bind(struct usb_configuration *c, struct usb_function *f)
451{
452 struct usb_composite_dev *cdev = c->cdev;
Benoit Gobyaab96812011-04-19 20:37:33 -0700453 struct adb_dev *dev = func_to_adb(f);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500454 int id;
455 int ret;
456
457 dev->cdev = cdev;
458 DBG(cdev, "adb_function_bind dev: %p\n", dev);
459
460 /* allocate interface ID(s) */
461 id = usb_interface_id(c, f);
462 if (id < 0)
463 return id;
464 adb_interface_desc.bInterfaceNumber = id;
465
466 /* allocate endpoints */
Benoit Gobyaab96812011-04-19 20:37:33 -0700467 ret = adb_create_bulk_endpoints(dev, &adb_fullspeed_in_desc,
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500468 &adb_fullspeed_out_desc);
469 if (ret)
470 return ret;
471
472 /* support high speed hardware */
473 if (gadget_is_dualspeed(c->cdev->gadget)) {
474 adb_highspeed_in_desc.bEndpointAddress =
475 adb_fullspeed_in_desc.bEndpointAddress;
476 adb_highspeed_out_desc.bEndpointAddress =
477 adb_fullspeed_out_desc.bEndpointAddress;
478 }
479
480 DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
481 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
482 f->name, dev->ep_in->name, dev->ep_out->name);
483 return 0;
484}
485
486static void
487adb_function_unbind(struct usb_configuration *c, struct usb_function *f)
488{
Benoit Gobyaab96812011-04-19 20:37:33 -0700489 struct adb_dev *dev = func_to_adb(f);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500490 struct usb_request *req;
491
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500492
493 dev->online = 0;
494 dev->error = 1;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500495
Benoit Gobyaab96812011-04-19 20:37:33 -0700496 wake_up(&dev->read_wq);
497
498 adb_request_free(dev->rx_req, dev->ep_out);
499 while ((req = adb_req_get(dev, &dev->tx_idle)))
500 adb_request_free(req, dev->ep_in);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500501}
502
503static int adb_function_set_alt(struct usb_function *f,
504 unsigned intf, unsigned alt)
505{
Benoit Gobyaab96812011-04-19 20:37:33 -0700506 struct adb_dev *dev = func_to_adb(f);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500507 struct usb_composite_dev *cdev = f->config->cdev;
508 int ret;
509
510 DBG(cdev, "adb_function_set_alt intf: %d alt: %d\n", intf, alt);
511 ret = usb_ep_enable(dev->ep_in,
512 ep_choose(cdev->gadget,
513 &adb_highspeed_in_desc,
514 &adb_fullspeed_in_desc));
515 if (ret)
516 return ret;
517 ret = usb_ep_enable(dev->ep_out,
518 ep_choose(cdev->gadget,
519 &adb_highspeed_out_desc,
520 &adb_fullspeed_out_desc));
521 if (ret) {
522 usb_ep_disable(dev->ep_in);
523 return ret;
524 }
525 dev->online = 1;
526
527 /* readers may be blocked waiting for us to go online */
528 wake_up(&dev->read_wq);
529 return 0;
530}
531
532static void adb_function_disable(struct usb_function *f)
533{
Benoit Gobyaab96812011-04-19 20:37:33 -0700534 struct adb_dev *dev = func_to_adb(f);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500535 struct usb_composite_dev *cdev = dev->cdev;
536
Benoit Gobyaab96812011-04-19 20:37:33 -0700537 DBG(cdev, "adb_function_disable cdev %p\n", cdev);
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500538 dev->online = 0;
539 dev->error = 1;
540 usb_ep_disable(dev->ep_in);
541 usb_ep_disable(dev->ep_out);
542
543 /* readers may be blocked waiting for us to go online */
544 wake_up(&dev->read_wq);
545
546 VDBG(cdev, "%s disabled\n", dev->function.name);
547}
548
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530549static int adb_bind_config(struct usb_configuration *c)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500550{
Benoit Gobyaab96812011-04-19 20:37:33 -0700551 struct adb_dev *dev = _adb_dev;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500552
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530553 printk(KERN_INFO "adb_bind_config\n");
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500554
Benoit Gobyaab96812011-04-19 20:37:33 -0700555 dev->cdev = c->cdev;
556 dev->function.name = "adb";
557 dev->function.descriptors = fs_adb_descs;
558 dev->function.hs_descriptors = hs_adb_descs;
559 dev->function.bind = adb_function_bind;
560 dev->function.unbind = adb_function_unbind;
561 dev->function.set_alt = adb_function_set_alt;
562 dev->function.disable = adb_function_disable;
563
564 return usb_add_function(c, &dev->function);
565}
566
567static int adb_setup(void)
568{
569 struct adb_dev *dev;
570 int ret;
571
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500572 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
573 if (!dev)
574 return -ENOMEM;
575
576 spin_lock_init(&dev->lock);
577
578 init_waitqueue_head(&dev->read_wq);
579 init_waitqueue_head(&dev->write_wq);
580
581 atomic_set(&dev->open_excl, 0);
582 atomic_set(&dev->read_excl, 0);
583 atomic_set(&dev->write_excl, 0);
584
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500585 INIT_LIST_HEAD(&dev->tx_idle);
586
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500587 _adb_dev = dev;
588
589 ret = misc_register(&adb_device);
590 if (ret)
Benoit Gobyaab96812011-04-19 20:37:33 -0700591 goto err;
Krishna, Vamsi83814ea2009-02-11 21:07:20 +0530592
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500593 return 0;
594
Benoit Gobyaab96812011-04-19 20:37:33 -0700595err:
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500596 kfree(dev);
597 printk(KERN_ERR "adb gadget driver failed to initialize\n");
598 return ret;
599}
600
Benoit Gobyaab96812011-04-19 20:37:33 -0700601static void adb_cleanup(void)
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500602{
Benoit Gobyaab96812011-04-19 20:37:33 -0700603 misc_deregister(&adb_device);
604
605 kfree(_adb_dev);
606 _adb_dev = NULL;
Mike Lockwood7f0d7bd2008-12-02 22:01:33 -0500607}