Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 35 | static const char adb_shortname[] = "android_adb"; |
| 36 | |
| 37 | struct 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 Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 45 | atomic_t online; |
| 46 | atomic_t error; |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 47 | |
| 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 Kondeti | 1aa235a | 2012-10-09 17:47:44 +0530 | [diff] [blame] | 58 | bool notify_close; |
Pavankumar Kondeti | c4d8e2c | 2012-10-26 11:15:54 +0530 | [diff] [blame] | 59 | bool close_notified; |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | static 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 | |
| 72 | static 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 | |
| 80 | static 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 | |
| 88 | static 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 | |
| 95 | static 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 | |
| 102 | static 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 | |
| 109 | static 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 Goby | 80ba14d | 2012-03-19 18:56:52 -0700 | [diff] [blame] | 116 | static void adb_ready_callback(void); |
| 117 | static void adb_closed_callback(void); |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 118 | |
| 119 | /* temporary variable used between adb_open() and adb_gadget_bind() */ |
| 120 | static struct adb_dev *_adb_dev; |
| 121 | |
| 122 | static inline struct adb_dev *func_to_adb(struct usb_function *f) |
| 123 | { |
| 124 | return container_of(f, struct adb_dev, function); |
| 125 | } |
| 126 | |
| 127 | |
| 128 | static 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 | |
| 144 | static 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 | |
| 152 | static 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 | |
| 162 | static inline void adb_unlock(atomic_t *excl) |
| 163 | { |
| 164 | atomic_dec(excl); |
| 165 | } |
| 166 | |
| 167 | /* add a request to the tail of a list */ |
| 168 | void 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 */ |
| 179 | struct 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 | |
| 195 | static 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 Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 200 | atomic_set(&dev->error, 1); |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 201 | |
| 202 | adb_req_put(dev, &dev->tx_idle, req); |
| 203 | |
| 204 | wake_up(&dev->write_wq); |
| 205 | } |
| 206 | |
| 207 | static 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 Cross | 8492aa1 | 2012-03-08 17:57:51 -0800 | [diff] [blame] | 212 | if (req->status != 0 && req->status != -ECONNRESET) |
Manu Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 213 | atomic_set(&dev->error, 1); |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 214 | |
| 215 | wake_up(&dev->read_wq); |
| 216 | } |
| 217 | |
| 218 | static 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 | |
| 264 | fail: |
| 265 | printk(KERN_ERR "adb_bind() could not allocate requests\n"); |
| 266 | return -1; |
| 267 | } |
| 268 | |
| 269 | static 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 Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 288 | while (!(atomic_read(&dev->online) || atomic_read(&dev->error))) { |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 289 | pr_debug("adb_read: waiting for online state\n"); |
| 290 | ret = wait_event_interruptible(dev->read_wq, |
Manu Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 291 | (atomic_read(&dev->online) || |
| 292 | atomic_read(&dev->error))); |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 293 | if (ret < 0) { |
| 294 | adb_unlock(&dev->read_excl); |
| 295 | return ret; |
| 296 | } |
| 297 | } |
Manu Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 298 | if (atomic_read(&dev->error)) { |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 299 | r = -EIO; |
| 300 | goto done; |
| 301 | } |
| 302 | |
| 303 | requeue_req: |
| 304 | /* queue a request */ |
| 305 | req = dev->rx_req; |
Ido Shayevitz | f73ad6c | 2012-08-23 05:30:04 +0300 | [diff] [blame] | 306 | req->length = ADB_BULK_BUFFER_SIZE; |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 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 Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 312 | atomic_set(&dev->error, 1); |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 313 | goto done; |
| 314 | } else { |
| 315 | pr_debug("rx %p queue\n", req); |
| 316 | } |
| 317 | |
| 318 | /* wait for a request to complete */ |
| 319 | ret = wait_event_interruptible(dev->read_wq, dev->rx_done); |
| 320 | if (ret < 0) { |
Colin Cross | 42582f2 | 2012-03-05 13:29:45 -0800 | [diff] [blame] | 321 | if (ret != -ERESTARTSYS) |
Manu Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 322 | atomic_set(&dev->error, 1); |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 323 | r = ret; |
| 324 | usb_ep_dequeue(dev->ep_out, req); |
| 325 | goto done; |
| 326 | } |
Manu Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 327 | if (!atomic_read(&dev->error)) { |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 328 | /* If we got a 0-len packet, throw it back and try again. */ |
| 329 | if (req->actual == 0) |
| 330 | goto requeue_req; |
| 331 | |
| 332 | pr_debug("rx %p %d\n", req, req->actual); |
| 333 | xfer = (req->actual < count) ? req->actual : count; |
| 334 | if (copy_to_user(buf, req->buf, xfer)) |
| 335 | r = -EFAULT; |
| 336 | |
| 337 | } else |
| 338 | r = -EIO; |
| 339 | |
| 340 | done: |
| 341 | adb_unlock(&dev->read_excl); |
| 342 | pr_debug("adb_read returning %d\n", r); |
| 343 | return r; |
| 344 | } |
| 345 | |
| 346 | static ssize_t adb_write(struct file *fp, const char __user *buf, |
| 347 | size_t count, loff_t *pos) |
| 348 | { |
| 349 | struct adb_dev *dev = fp->private_data; |
| 350 | struct usb_request *req = 0; |
| 351 | int r = count, xfer; |
| 352 | int ret; |
| 353 | |
| 354 | if (!_adb_dev) |
| 355 | return -ENODEV; |
| 356 | pr_debug("adb_write(%d)\n", count); |
| 357 | |
| 358 | if (adb_lock(&dev->write_excl)) |
| 359 | return -EBUSY; |
| 360 | |
| 361 | while (count > 0) { |
Manu Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 362 | if (atomic_read(&dev->error)) { |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 363 | pr_debug("adb_write dev->error\n"); |
| 364 | r = -EIO; |
| 365 | break; |
| 366 | } |
| 367 | |
| 368 | /* get an idle tx request to use */ |
| 369 | req = 0; |
| 370 | ret = wait_event_interruptible(dev->write_wq, |
Manu Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 371 | ((req = adb_req_get(dev, &dev->tx_idle)) || |
| 372 | atomic_read(&dev->error))); |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 373 | |
| 374 | if (ret < 0) { |
| 375 | r = ret; |
| 376 | break; |
| 377 | } |
| 378 | |
| 379 | if (req != 0) { |
| 380 | if (count > ADB_BULK_BUFFER_SIZE) |
| 381 | xfer = ADB_BULK_BUFFER_SIZE; |
| 382 | else |
| 383 | xfer = count; |
| 384 | if (copy_from_user(req->buf, buf, xfer)) { |
| 385 | r = -EFAULT; |
| 386 | break; |
| 387 | } |
| 388 | |
| 389 | req->length = xfer; |
| 390 | ret = usb_ep_queue(dev->ep_in, req, GFP_ATOMIC); |
| 391 | if (ret < 0) { |
| 392 | pr_debug("adb_write: xfer error %d\n", ret); |
Manu Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 393 | atomic_set(&dev->error, 1); |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 394 | r = -EIO; |
| 395 | break; |
| 396 | } |
| 397 | |
| 398 | buf += xfer; |
| 399 | count -= xfer; |
| 400 | |
| 401 | /* zero this so we don't try to free it on error exit */ |
| 402 | req = 0; |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | if (req) |
| 407 | adb_req_put(dev, &dev->tx_idle, req); |
| 408 | |
| 409 | adb_unlock(&dev->write_excl); |
| 410 | pr_debug("adb_write returning %d\n", r); |
| 411 | return r; |
| 412 | } |
| 413 | |
| 414 | static int adb_open(struct inode *ip, struct file *fp) |
| 415 | { |
Benoit Goby | 80ba14d | 2012-03-19 18:56:52 -0700 | [diff] [blame] | 416 | pr_info("adb_open\n"); |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 417 | if (!_adb_dev) |
| 418 | return -ENODEV; |
| 419 | |
| 420 | if (adb_lock(&_adb_dev->open_excl)) |
| 421 | return -EBUSY; |
| 422 | |
| 423 | fp->private_data = _adb_dev; |
| 424 | |
| 425 | /* clear the error latch */ |
Manu Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 426 | atomic_set(&_adb_dev->error, 0); |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 427 | |
Pavankumar Kondeti | c4d8e2c | 2012-10-26 11:15:54 +0530 | [diff] [blame] | 428 | if (_adb_dev->close_notified) { |
| 429 | _adb_dev->close_notified = false; |
Pavankumar Kondeti | 1aa235a | 2012-10-09 17:47:44 +0530 | [diff] [blame] | 430 | adb_ready_callback(); |
Pavankumar Kondeti | c4d8e2c | 2012-10-26 11:15:54 +0530 | [diff] [blame] | 431 | } |
Benoit Goby | 80ba14d | 2012-03-19 18:56:52 -0700 | [diff] [blame] | 432 | |
Pavankumar Kondeti | 1aa235a | 2012-10-09 17:47:44 +0530 | [diff] [blame] | 433 | _adb_dev->notify_close = true; |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | static int adb_release(struct inode *ip, struct file *fp) |
| 438 | { |
Benoit Goby | 80ba14d | 2012-03-19 18:56:52 -0700 | [diff] [blame] | 439 | pr_info("adb_release\n"); |
| 440 | |
Pavankumar Kondeti | 1aa235a | 2012-10-09 17:47:44 +0530 | [diff] [blame] | 441 | /* |
| 442 | * ADB daemon closes the device file after I/O error. The |
| 443 | * I/O error happen when Rx requests are flushed during |
| 444 | * cable disconnect or bus reset in configured state. Disabling |
| 445 | * USB configuration and pull-up during these scenarios are |
| 446 | * undesired. We want to force bus reset only for certain |
| 447 | * commands like "adb root" and "adb usb". |
| 448 | */ |
Pavankumar Kondeti | c4d8e2c | 2012-10-26 11:15:54 +0530 | [diff] [blame] | 449 | if (_adb_dev->notify_close) { |
Pavankumar Kondeti | 1aa235a | 2012-10-09 17:47:44 +0530 | [diff] [blame] | 450 | adb_closed_callback(); |
Pavankumar Kondeti | c4d8e2c | 2012-10-26 11:15:54 +0530 | [diff] [blame] | 451 | _adb_dev->close_notified = true; |
| 452 | } |
Benoit Goby | 80ba14d | 2012-03-19 18:56:52 -0700 | [diff] [blame] | 453 | |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 454 | adb_unlock(&_adb_dev->open_excl); |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | /* file operations for ADB device /dev/android_adb */ |
| 459 | static const struct file_operations adb_fops = { |
| 460 | .owner = THIS_MODULE, |
| 461 | .read = adb_read, |
| 462 | .write = adb_write, |
| 463 | .open = adb_open, |
| 464 | .release = adb_release, |
| 465 | }; |
| 466 | |
| 467 | static struct miscdevice adb_device = { |
| 468 | .minor = MISC_DYNAMIC_MINOR, |
| 469 | .name = adb_shortname, |
| 470 | .fops = &adb_fops, |
| 471 | }; |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | static int |
| 477 | adb_function_bind(struct usb_configuration *c, struct usb_function *f) |
| 478 | { |
| 479 | struct usb_composite_dev *cdev = c->cdev; |
| 480 | struct adb_dev *dev = func_to_adb(f); |
| 481 | int id; |
| 482 | int ret; |
| 483 | |
| 484 | dev->cdev = cdev; |
| 485 | DBG(cdev, "adb_function_bind dev: %p\n", dev); |
| 486 | |
| 487 | /* allocate interface ID(s) */ |
| 488 | id = usb_interface_id(c, f); |
| 489 | if (id < 0) |
| 490 | return id; |
| 491 | adb_interface_desc.bInterfaceNumber = id; |
| 492 | |
| 493 | /* allocate endpoints */ |
| 494 | ret = adb_create_bulk_endpoints(dev, &adb_fullspeed_in_desc, |
| 495 | &adb_fullspeed_out_desc); |
| 496 | if (ret) |
| 497 | return ret; |
| 498 | |
| 499 | /* support high speed hardware */ |
| 500 | if (gadget_is_dualspeed(c->cdev->gadget)) { |
| 501 | adb_highspeed_in_desc.bEndpointAddress = |
| 502 | adb_fullspeed_in_desc.bEndpointAddress; |
| 503 | adb_highspeed_out_desc.bEndpointAddress = |
| 504 | adb_fullspeed_out_desc.bEndpointAddress; |
| 505 | } |
| 506 | |
| 507 | DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n", |
| 508 | gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full", |
| 509 | f->name, dev->ep_in->name, dev->ep_out->name); |
| 510 | return 0; |
| 511 | } |
| 512 | |
| 513 | static void |
| 514 | adb_function_unbind(struct usb_configuration *c, struct usb_function *f) |
| 515 | { |
| 516 | struct adb_dev *dev = func_to_adb(f); |
| 517 | struct usb_request *req; |
| 518 | |
| 519 | |
Manu Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 520 | atomic_set(&dev->online, 0); |
| 521 | atomic_set(&dev->error, 1); |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 522 | |
| 523 | wake_up(&dev->read_wq); |
| 524 | |
| 525 | adb_request_free(dev->rx_req, dev->ep_out); |
| 526 | while ((req = adb_req_get(dev, &dev->tx_idle))) |
| 527 | adb_request_free(req, dev->ep_in); |
| 528 | } |
| 529 | |
| 530 | static int adb_function_set_alt(struct usb_function *f, |
| 531 | unsigned intf, unsigned alt) |
| 532 | { |
| 533 | struct adb_dev *dev = func_to_adb(f); |
| 534 | struct usb_composite_dev *cdev = f->config->cdev; |
| 535 | int ret; |
| 536 | |
| 537 | DBG(cdev, "adb_function_set_alt intf: %d alt: %d\n", intf, alt); |
| 538 | |
| 539 | ret = config_ep_by_speed(cdev->gadget, f, dev->ep_in); |
Tatyana Brokhman | ebd3f39 | 2011-06-28 16:33:50 +0300 | [diff] [blame] | 540 | if (ret) { |
Tatyana Brokhman | 31ac352 | 2011-06-28 15:33:50 +0200 | [diff] [blame] | 541 | dev->ep_in->desc = NULL; |
| 542 | ERROR(cdev, "config_ep_by_speed failes for ep %s, result %d\n", |
| 543 | dev->ep_in->name, ret); |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 544 | return ret; |
Tatyana Brokhman | 31ac352 | 2011-06-28 15:33:50 +0200 | [diff] [blame] | 545 | } |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 546 | ret = usb_ep_enable(dev->ep_in); |
Tatyana Brokhman | 31ac352 | 2011-06-28 15:33:50 +0200 | [diff] [blame] | 547 | if (ret) { |
| 548 | ERROR(cdev, "failed to enable ep %s, result %d\n", |
| 549 | dev->ep_in->name, ret); |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 550 | return ret; |
Tatyana Brokhman | 31ac352 | 2011-06-28 15:33:50 +0200 | [diff] [blame] | 551 | } |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 552 | |
| 553 | ret = config_ep_by_speed(cdev->gadget, f, dev->ep_out); |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 554 | if (ret) { |
Tatyana Brokhman | 31ac352 | 2011-06-28 15:33:50 +0200 | [diff] [blame] | 555 | dev->ep_out->desc = NULL; |
| 556 | ERROR(cdev, "config_ep_by_speed failes for ep %s, result %d\n", |
| 557 | dev->ep_out->name, ret); |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 558 | usb_ep_disable(dev->ep_in); |
| 559 | return ret; |
| 560 | } |
Tatyana Brokhman | 31ac352 | 2011-06-28 15:33:50 +0200 | [diff] [blame] | 561 | ret = usb_ep_enable(dev->ep_out); |
| 562 | if (ret) { |
| 563 | ERROR(cdev, "failed to enable ep %s, result %d\n", |
| 564 | dev->ep_out->name, ret); |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 565 | usb_ep_disable(dev->ep_in); |
| 566 | return ret; |
| 567 | } |
Manu Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 568 | atomic_set(&dev->online, 1); |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 569 | |
| 570 | /* readers may be blocked waiting for us to go online */ |
| 571 | wake_up(&dev->read_wq); |
| 572 | return 0; |
| 573 | } |
| 574 | |
| 575 | static void adb_function_disable(struct usb_function *f) |
| 576 | { |
| 577 | struct adb_dev *dev = func_to_adb(f); |
| 578 | struct usb_composite_dev *cdev = dev->cdev; |
| 579 | |
| 580 | DBG(cdev, "adb_function_disable cdev %p\n", cdev); |
Pavankumar Kondeti | 1aa235a | 2012-10-09 17:47:44 +0530 | [diff] [blame] | 581 | /* |
| 582 | * Bus reset happened or cable disconnected. No |
| 583 | * need to disable the configuration now. We will |
| 584 | * set noify_close to true when device file is re-opened. |
| 585 | */ |
| 586 | dev->notify_close = false; |
Manu Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 587 | atomic_set(&dev->online, 0); |
| 588 | atomic_set(&dev->error, 1); |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 589 | usb_ep_disable(dev->ep_in); |
| 590 | usb_ep_disable(dev->ep_out); |
| 591 | |
| 592 | /* readers may be blocked waiting for us to go online */ |
| 593 | wake_up(&dev->read_wq); |
| 594 | |
| 595 | VDBG(cdev, "%s disabled\n", dev->function.name); |
| 596 | } |
| 597 | |
| 598 | static int adb_bind_config(struct usb_configuration *c) |
| 599 | { |
| 600 | struct adb_dev *dev = _adb_dev; |
| 601 | |
| 602 | printk(KERN_INFO "adb_bind_config\n"); |
| 603 | |
| 604 | dev->cdev = c->cdev; |
| 605 | dev->function.name = "adb"; |
| 606 | dev->function.descriptors = fs_adb_descs; |
| 607 | dev->function.hs_descriptors = hs_adb_descs; |
| 608 | dev->function.bind = adb_function_bind; |
| 609 | dev->function.unbind = adb_function_unbind; |
| 610 | dev->function.set_alt = adb_function_set_alt; |
| 611 | dev->function.disable = adb_function_disable; |
| 612 | |
| 613 | return usb_add_function(c, &dev->function); |
| 614 | } |
| 615 | |
| 616 | static int adb_setup(void) |
| 617 | { |
| 618 | struct adb_dev *dev; |
| 619 | int ret; |
| 620 | |
| 621 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 622 | if (!dev) |
| 623 | return -ENOMEM; |
| 624 | |
| 625 | spin_lock_init(&dev->lock); |
| 626 | |
| 627 | init_waitqueue_head(&dev->read_wq); |
| 628 | init_waitqueue_head(&dev->write_wq); |
| 629 | |
| 630 | atomic_set(&dev->open_excl, 0); |
| 631 | atomic_set(&dev->read_excl, 0); |
| 632 | atomic_set(&dev->write_excl, 0); |
Pavankumar Kondeti | c4d8e2c | 2012-10-26 11:15:54 +0530 | [diff] [blame] | 633 | |
| 634 | /* config is disabled by default if adb is present. */ |
| 635 | dev->close_notified = true; |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 636 | |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 637 | INIT_LIST_HEAD(&dev->tx_idle); |
| 638 | |
| 639 | _adb_dev = dev; |
| 640 | |
| 641 | ret = misc_register(&adb_device); |
| 642 | if (ret) |
| 643 | goto err; |
| 644 | |
| 645 | return 0; |
| 646 | |
| 647 | err: |
| 648 | kfree(dev); |
| 649 | printk(KERN_ERR "adb gadget driver failed to initialize\n"); |
| 650 | return ret; |
| 651 | } |
| 652 | |
| 653 | static void adb_cleanup(void) |
| 654 | { |
| 655 | misc_deregister(&adb_device); |
| 656 | |
| 657 | kfree(_adb_dev); |
| 658 | _adb_dev = NULL; |
| 659 | } |