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 | |
Pavankumar Kondeti | 6f94bc9 | 2012-08-03 09:34:32 +0530 | [diff] [blame] | 72 | static 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 | |
| 80 | static 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 | |
| 89 | static 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 | |
| 97 | static 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 Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 106 | static 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 | |
| 114 | static 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 | |
| 122 | static 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 | |
| 129 | static 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 | |
| 136 | static 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 | |
| 143 | static 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 Kondeti | 6f94bc9 | 2012-08-03 09:34:32 +0530 | [diff] [blame] | 150 | static 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 Goby | 80ba14d | 2012-03-19 18:56:52 -0700 | [diff] [blame] | 159 | static void adb_ready_callback(void); |
| 160 | static void adb_closed_callback(void); |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 161 | |
| 162 | /* temporary variable used between adb_open() and adb_gadget_bind() */ |
| 163 | static struct adb_dev *_adb_dev; |
| 164 | |
| 165 | static inline struct adb_dev *func_to_adb(struct usb_function *f) |
| 166 | { |
| 167 | return container_of(f, struct adb_dev, function); |
| 168 | } |
| 169 | |
| 170 | |
| 171 | static 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 | |
| 187 | static 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 | |
| 195 | static 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 | |
| 205 | static inline void adb_unlock(atomic_t *excl) |
| 206 | { |
| 207 | atomic_dec(excl); |
| 208 | } |
| 209 | |
| 210 | /* add a request to the tail of a list */ |
| 211 | void 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 */ |
| 222 | struct 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 | |
| 238 | static 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 Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 243 | atomic_set(&dev->error, 1); |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 244 | |
| 245 | adb_req_put(dev, &dev->tx_idle, req); |
| 246 | |
| 247 | wake_up(&dev->write_wq); |
| 248 | } |
| 249 | |
| 250 | static 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 Cross | 8492aa1 | 2012-03-08 17:57:51 -0800 | [diff] [blame] | 255 | if (req->status != 0 && req->status != -ECONNRESET) |
Manu Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 256 | atomic_set(&dev->error, 1); |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 257 | |
| 258 | wake_up(&dev->read_wq); |
| 259 | } |
| 260 | |
| 261 | static 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 | |
| 307 | fail: |
| 308 | printk(KERN_ERR "adb_bind() could not allocate requests\n"); |
| 309 | return -1; |
| 310 | } |
| 311 | |
| 312 | static 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 Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 331 | while (!(atomic_read(&dev->online) || atomic_read(&dev->error))) { |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 332 | pr_debug("adb_read: waiting for online state\n"); |
| 333 | ret = wait_event_interruptible(dev->read_wq, |
Manu Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 334 | (atomic_read(&dev->online) || |
| 335 | atomic_read(&dev->error))); |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 336 | if (ret < 0) { |
| 337 | adb_unlock(&dev->read_excl); |
| 338 | return ret; |
| 339 | } |
| 340 | } |
Manu Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 341 | if (atomic_read(&dev->error)) { |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 342 | r = -EIO; |
| 343 | goto done; |
| 344 | } |
| 345 | |
| 346 | requeue_req: |
| 347 | /* queue a request */ |
| 348 | req = dev->rx_req; |
Ido Shayevitz | f73ad6c | 2012-08-23 05:30:04 +0300 | [diff] [blame] | 349 | req->length = ADB_BULK_BUFFER_SIZE; |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 350 | 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 Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 355 | atomic_set(&dev->error, 1); |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 356 | goto done; |
| 357 | } else { |
| 358 | pr_debug("rx %p queue\n", req); |
| 359 | } |
| 360 | |
| 361 | /* wait for a request to complete */ |
Pavankumar Kondeti | e8757ca | 2013-02-08 14:16:37 +0530 | [diff] [blame] | 362 | ret = wait_event_interruptible(dev->read_wq, dev->rx_done || |
| 363 | atomic_read(&dev->error)); |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 364 | if (ret < 0) { |
Colin Cross | 42582f2 | 2012-03-05 13:29:45 -0800 | [diff] [blame] | 365 | if (ret != -ERESTARTSYS) |
Manu Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 366 | atomic_set(&dev->error, 1); |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 367 | r = ret; |
| 368 | usb_ep_dequeue(dev->ep_out, req); |
| 369 | goto done; |
| 370 | } |
Manu Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 371 | if (!atomic_read(&dev->error)) { |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 372 | /* 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 | |
| 384 | done: |
Pavankumar Kondeti | e8757ca | 2013-02-08 14:16:37 +0530 | [diff] [blame] | 385 | if (atomic_read(&dev->error)) |
| 386 | wake_up(&dev->write_wq); |
| 387 | |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 388 | adb_unlock(&dev->read_excl); |
| 389 | pr_debug("adb_read returning %d\n", r); |
| 390 | return r; |
| 391 | } |
| 392 | |
| 393 | static 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 Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 409 | if (atomic_read(&dev->error)) { |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 410 | 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 Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 418 | ((req = adb_req_get(dev, &dev->tx_idle)) || |
| 419 | atomic_read(&dev->error))); |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 420 | |
| 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 Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 440 | atomic_set(&dev->error, 1); |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 441 | 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 Kondeti | e8757ca | 2013-02-08 14:16:37 +0530 | [diff] [blame] | 456 | if (atomic_read(&dev->error)) |
| 457 | wake_up(&dev->read_wq); |
| 458 | |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 459 | adb_unlock(&dev->write_excl); |
| 460 | pr_debug("adb_write returning %d\n", r); |
| 461 | return r; |
| 462 | } |
| 463 | |
| 464 | static int adb_open(struct inode *ip, struct file *fp) |
| 465 | { |
Pavankumar Kondeti | 755e487 | 2013-02-28 10:11:37 +0530 | [diff] [blame] | 466 | static DEFINE_RATELIMIT_STATE(rl, 10*HZ, 1); |
| 467 | |
| 468 | if (__ratelimit(&rl)) |
| 469 | pr_info("adb_open\n"); |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 470 | 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 Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 479 | atomic_set(&_adb_dev->error, 0); |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 480 | |
Pavankumar Kondeti | c4d8e2c | 2012-10-26 11:15:54 +0530 | [diff] [blame] | 481 | if (_adb_dev->close_notified) { |
| 482 | _adb_dev->close_notified = false; |
Pavankumar Kondeti | 1aa235a | 2012-10-09 17:47:44 +0530 | [diff] [blame] | 483 | adb_ready_callback(); |
Pavankumar Kondeti | c4d8e2c | 2012-10-26 11:15:54 +0530 | [diff] [blame] | 484 | } |
Benoit Goby | 80ba14d | 2012-03-19 18:56:52 -0700 | [diff] [blame] | 485 | |
Pavankumar Kondeti | 1aa235a | 2012-10-09 17:47:44 +0530 | [diff] [blame] | 486 | _adb_dev->notify_close = true; |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 487 | return 0; |
| 488 | } |
| 489 | |
| 490 | static int adb_release(struct inode *ip, struct file *fp) |
| 491 | { |
Pavankumar Kondeti | 755e487 | 2013-02-28 10:11:37 +0530 | [diff] [blame] | 492 | static DEFINE_RATELIMIT_STATE(rl, 10*HZ, 1); |
| 493 | |
| 494 | if (__ratelimit(&rl)) |
| 495 | pr_info("adb_release\n"); |
Benoit Goby | 80ba14d | 2012-03-19 18:56:52 -0700 | [diff] [blame] | 496 | |
Pavankumar Kondeti | 1aa235a | 2012-10-09 17:47:44 +0530 | [diff] [blame] | 497 | /* |
| 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 Kondeti | c4d8e2c | 2012-10-26 11:15:54 +0530 | [diff] [blame] | 505 | if (_adb_dev->notify_close) { |
Pavankumar Kondeti | 1aa235a | 2012-10-09 17:47:44 +0530 | [diff] [blame] | 506 | adb_closed_callback(); |
Pavankumar Kondeti | c4d8e2c | 2012-10-26 11:15:54 +0530 | [diff] [blame] | 507 | _adb_dev->close_notified = true; |
| 508 | } |
Benoit Goby | 80ba14d | 2012-03-19 18:56:52 -0700 | [diff] [blame] | 509 | |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 510 | adb_unlock(&_adb_dev->open_excl); |
| 511 | return 0; |
| 512 | } |
| 513 | |
| 514 | /* file operations for ADB device /dev/android_adb */ |
| 515 | static 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 | |
| 523 | static struct miscdevice adb_device = { |
| 524 | .minor = MISC_DYNAMIC_MINOR, |
| 525 | .name = adb_shortname, |
| 526 | .fops = &adb_fops, |
| 527 | }; |
| 528 | |
| 529 | |
| 530 | |
| 531 | |
| 532 | static int |
| 533 | adb_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 Kondeti | 6f94bc9 | 2012-08-03 09:34:32 +0530 | [diff] [blame] | 562 | /* 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 Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 569 | |
| 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 | |
| 576 | static void |
| 577 | adb_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 Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 583 | atomic_set(&dev->online, 0); |
| 584 | atomic_set(&dev->error, 1); |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 585 | |
| 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 | |
| 593 | static 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 Brokhman | ebd3f39 | 2011-06-28 16:33:50 +0300 | [diff] [blame] | 603 | if (ret) { |
Tatyana Brokhman | 31ac352 | 2011-06-28 15:33:50 +0200 | [diff] [blame] | 604 | 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 Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 607 | return ret; |
Tatyana Brokhman | 31ac352 | 2011-06-28 15:33:50 +0200 | [diff] [blame] | 608 | } |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 609 | ret = usb_ep_enable(dev->ep_in); |
Tatyana Brokhman | 31ac352 | 2011-06-28 15:33:50 +0200 | [diff] [blame] | 610 | if (ret) { |
| 611 | ERROR(cdev, "failed to enable ep %s, result %d\n", |
| 612 | dev->ep_in->name, ret); |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 613 | return ret; |
Tatyana Brokhman | 31ac352 | 2011-06-28 15:33:50 +0200 | [diff] [blame] | 614 | } |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 615 | |
| 616 | ret = config_ep_by_speed(cdev->gadget, f, dev->ep_out); |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 617 | if (ret) { |
Tatyana Brokhman | 31ac352 | 2011-06-28 15:33:50 +0200 | [diff] [blame] | 618 | 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 Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 621 | usb_ep_disable(dev->ep_in); |
| 622 | return ret; |
| 623 | } |
Tatyana Brokhman | 31ac352 | 2011-06-28 15:33:50 +0200 | [diff] [blame] | 624 | 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 Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 628 | usb_ep_disable(dev->ep_in); |
| 629 | return ret; |
| 630 | } |
Manu Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 631 | atomic_set(&dev->online, 1); |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 632 | |
| 633 | /* readers may be blocked waiting for us to go online */ |
| 634 | wake_up(&dev->read_wq); |
| 635 | return 0; |
| 636 | } |
| 637 | |
| 638 | static 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 Kondeti | 1aa235a | 2012-10-09 17:47:44 +0530 | [diff] [blame] | 644 | /* |
| 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 Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 650 | atomic_set(&dev->online, 0); |
| 651 | atomic_set(&dev->error, 1); |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 652 | 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 | |
| 661 | static int adb_bind_config(struct usb_configuration *c) |
| 662 | { |
| 663 | struct adb_dev *dev = _adb_dev; |
| 664 | |
Mayank Rana | ce1aaa5 | 2013-06-12 12:29:54 +0530 | [diff] [blame] | 665 | pr_debug("adb_bind_config\n"); |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 666 | |
| 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 Kondeti | 6f94bc9 | 2012-08-03 09:34:32 +0530 | [diff] [blame] | 671 | if (gadget_is_superspeed(c->cdev->gadget)) |
| 672 | dev->function.ss_descriptors = ss_adb_descs; |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 673 | 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 | |
| 681 | static 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 Kondeti | c4d8e2c | 2012-10-26 11:15:54 +0530 | [diff] [blame] | 698 | |
| 699 | /* config is disabled by default if adb is present. */ |
| 700 | dev->close_notified = true; |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 701 | |
Benoit Goby | 2b6862d | 2011-12-19 14:38:41 -0800 | [diff] [blame] | 702 | 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 | |
| 712 | err: |
| 713 | kfree(dev); |
| 714 | printk(KERN_ERR "adb gadget driver failed to initialize\n"); |
| 715 | return ret; |
| 716 | } |
| 717 | |
| 718 | static void adb_cleanup(void) |
| 719 | { |
| 720 | misc_deregister(&adb_device); |
| 721 | |
| 722 | kfree(_adb_dev); |
| 723 | _adb_dev = NULL; |
| 724 | } |