Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [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 | |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 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 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 30 | #define ADB_BULK_BUFFER_SIZE 4096 |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 31 | |
Mike Lockwood | a9e8c44 | 2009-12-19 18:22:09 -0500 | [diff] [blame] | 32 | /* number of tx requests to allocate */ |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 33 | #define TX_REQ_MAX 4 |
| 34 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 35 | static const char adb_shortname[] = "android_adb"; |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 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; |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [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; |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 53 | |
| 54 | wait_queue_head_t read_wq; |
| 55 | wait_queue_head_t write_wq; |
Mike Lockwood | a9e8c44 | 2009-12-19 18:22:09 -0500 | [diff] [blame] | 56 | struct usb_request *rx_req; |
| 57 | int rx_done; |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | static 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 | |
| 70 | static 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 | |
| 78 | static 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 | |
| 86 | static 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 | |
| 93 | static 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 | |
| 100 | static 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 | |
| 107 | static 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 Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 114 | |
| 115 | /* temporary variable used between adb_open() and adb_gadget_bind() */ |
| 116 | static struct adb_dev *_adb_dev; |
| 117 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 118 | static inline struct adb_dev *func_to_adb(struct usb_function *f) |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 119 | { |
| 120 | return container_of(f, struct adb_dev, function); |
| 121 | } |
| 122 | |
| 123 | |
| 124 | static 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 | |
| 140 | static 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 Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 148 | static inline int adb_lock(atomic_t *excl) |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 149 | { |
| 150 | if (atomic_inc_return(excl) == 1) { |
| 151 | return 0; |
| 152 | } else { |
| 153 | atomic_dec(excl); |
| 154 | return -1; |
| 155 | } |
| 156 | } |
| 157 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 158 | static inline void adb_unlock(atomic_t *excl) |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 159 | { |
| 160 | atomic_dec(excl); |
| 161 | } |
| 162 | |
| 163 | /* add a request to the tail of a list */ |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 164 | void adb_req_put(struct adb_dev *dev, struct list_head *head, |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 165 | 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 Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 175 | struct usb_request *adb_req_get(struct adb_dev *dev, struct list_head *head) |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 176 | { |
| 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 | |
| 191 | static 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) |
Manu Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 196 | atomic_set(&dev->error, 1); |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 197 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 198 | adb_req_put(dev, &dev->tx_idle, req); |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 199 | |
| 200 | wake_up(&dev->write_wq); |
| 201 | } |
| 202 | |
| 203 | static void adb_complete_out(struct usb_ep *ep, struct usb_request *req) |
| 204 | { |
| 205 | struct adb_dev *dev = _adb_dev; |
| 206 | |
Mike Lockwood | a9e8c44 | 2009-12-19 18:22:09 -0500 | [diff] [blame] | 207 | dev->rx_done = 1; |
| 208 | if (req->status != 0) |
Manu Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 209 | atomic_set(&dev->error, 1); |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 210 | |
| 211 | wake_up(&dev->read_wq); |
| 212 | } |
| 213 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 214 | static int adb_create_bulk_endpoints(struct adb_dev *dev, |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 215 | 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 Lockwood | 789ef23 | 2010-01-12 10:33:59 -0800 | [diff] [blame] | 231 | ep->driver_data = dev; /* claim the endpoint */ |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 232 | 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 Lockwood | 789ef23 | 2010-01-12 10:33:59 -0800 | [diff] [blame] | 240 | ep->driver_data = dev; /* claim the endpoint */ |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 241 | dev->ep_out = ep; |
| 242 | |
| 243 | /* now allocate requests for our endpoints */ |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 244 | req = adb_request_new(dev->ep_out, ADB_BULK_BUFFER_SIZE); |
Mike Lockwood | a9e8c44 | 2009-12-19 18:22:09 -0500 | [diff] [blame] | 245 | if (!req) |
| 246 | goto fail; |
| 247 | req->complete = adb_complete_out; |
| 248 | dev->rx_req = req; |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 249 | |
| 250 | for (i = 0; i < TX_REQ_MAX; i++) { |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 251 | req = adb_request_new(dev->ep_in, ADB_BULK_BUFFER_SIZE); |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 252 | if (!req) |
| 253 | goto fail; |
| 254 | req->complete = adb_complete_in; |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 255 | adb_req_put(dev, &dev->tx_idle, req); |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | return 0; |
| 259 | |
| 260 | fail: |
| 261 | printk(KERN_ERR "adb_bind() could not allocate requests\n"); |
| 262 | return -1; |
| 263 | } |
| 264 | |
| 265 | static 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 Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 269 | struct usb_request *req; |
| 270 | int r = count, xfer; |
| 271 | int ret; |
| 272 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 273 | pr_debug("adb_read(%d)\n", count); |
| 274 | if (!_adb_dev) |
| 275 | return -ENODEV; |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 276 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 277 | if (count > ADB_BULK_BUFFER_SIZE) |
Mike Lockwood | a9e8c44 | 2009-12-19 18:22:09 -0500 | [diff] [blame] | 278 | return -EINVAL; |
| 279 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 280 | if (adb_lock(&dev->read_excl)) |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 281 | return -EBUSY; |
| 282 | |
| 283 | /* we will block until we're online */ |
Manu Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 284 | while (!(atomic_read(&dev->online) || atomic_read(&dev->error))) { |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 285 | pr_debug("adb_read: waiting for online state\n"); |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 286 | ret = wait_event_interruptible(dev->read_wq, |
Manu Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 287 | (atomic_read(&dev->online) || |
| 288 | atomic_read(&dev->error))); |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 289 | if (ret < 0) { |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 290 | adb_unlock(&dev->read_excl); |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 291 | return ret; |
| 292 | } |
| 293 | } |
Manu Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 294 | if (atomic_read(&dev->error)) { |
Mike Lockwood | a9e8c44 | 2009-12-19 18:22:09 -0500 | [diff] [blame] | 295 | r = -EIO; |
| 296 | goto done; |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 297 | } |
| 298 | |
Mike Lockwood | a9e8c44 | 2009-12-19 18:22:09 -0500 | [diff] [blame] | 299 | requeue_req: |
| 300 | /* queue a request */ |
| 301 | req = dev->rx_req; |
| 302 | req->length = count; |
| 303 | dev->rx_done = 0; |
| 304 | ret = usb_ep_queue(dev->ep_out, req, GFP_ATOMIC); |
| 305 | if (ret < 0) { |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 306 | pr_debug("adb_read: failed to queue req %p (%d)\n", req, ret); |
Mike Lockwood | a9e8c44 | 2009-12-19 18:22:09 -0500 | [diff] [blame] | 307 | r = -EIO; |
Manu Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 308 | atomic_set(&dev->error, 1); |
Mike Lockwood | a9e8c44 | 2009-12-19 18:22:09 -0500 | [diff] [blame] | 309 | goto done; |
| 310 | } else { |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 311 | pr_debug("rx %p queue\n", req); |
Mike Lockwood | a9e8c44 | 2009-12-19 18:22:09 -0500 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | /* wait for a request to complete */ |
| 315 | ret = wait_event_interruptible(dev->read_wq, dev->rx_done); |
| 316 | if (ret < 0) { |
Manu Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 317 | atomic_set(&dev->error, 1); |
Mike Lockwood | a9e8c44 | 2009-12-19 18:22:09 -0500 | [diff] [blame] | 318 | r = ret; |
Iliyan Malchev | 7d52503 | 2011-02-18 11:28:32 -0800 | [diff] [blame] | 319 | usb_ep_dequeue(dev->ep_out, req); |
Mike Lockwood | a9e8c44 | 2009-12-19 18:22:09 -0500 | [diff] [blame] | 320 | goto done; |
| 321 | } |
Manu Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 322 | if (!atomic_read(&dev->error)) { |
Mike Lockwood | a9e8c44 | 2009-12-19 18:22:09 -0500 | [diff] [blame] | 323 | /* If we got a 0-len packet, throw it back and try again. */ |
| 324 | if (req->actual == 0) |
| 325 | goto requeue_req; |
| 326 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 327 | pr_debug("rx %p %d\n", req, req->actual); |
Mike Lockwood | a9e8c44 | 2009-12-19 18:22:09 -0500 | [diff] [blame] | 328 | xfer = (req->actual < count) ? req->actual : count; |
| 329 | if (copy_to_user(buf, req->buf, xfer)) |
| 330 | r = -EFAULT; |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 331 | |
Mike Lockwood | a9e8c44 | 2009-12-19 18:22:09 -0500 | [diff] [blame] | 332 | } else |
| 333 | r = -EIO; |
| 334 | |
| 335 | done: |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 336 | adb_unlock(&dev->read_excl); |
| 337 | pr_debug("adb_read returning %d\n", r); |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 338 | return r; |
| 339 | } |
| 340 | |
| 341 | static ssize_t adb_write(struct file *fp, const char __user *buf, |
| 342 | size_t count, loff_t *pos) |
| 343 | { |
| 344 | struct adb_dev *dev = fp->private_data; |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 345 | struct usb_request *req = 0; |
| 346 | int r = count, xfer; |
| 347 | int ret; |
| 348 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 349 | if (!_adb_dev) |
| 350 | return -ENODEV; |
| 351 | pr_debug("adb_write(%d)\n", count); |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 352 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 353 | if (adb_lock(&dev->write_excl)) |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 354 | return -EBUSY; |
| 355 | |
| 356 | while (count > 0) { |
Manu Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 357 | if (atomic_read(&dev->error)) { |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 358 | pr_debug("adb_write dev->error\n"); |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 359 | r = -EIO; |
| 360 | break; |
| 361 | } |
| 362 | |
| 363 | /* get an idle tx request to use */ |
| 364 | req = 0; |
| 365 | ret = wait_event_interruptible(dev->write_wq, |
Manu Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 366 | ((req = adb_req_get(dev, &dev->tx_idle)) || |
| 367 | atomic_read(&dev->error))); |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 368 | |
| 369 | if (ret < 0) { |
| 370 | r = ret; |
| 371 | break; |
| 372 | } |
| 373 | |
| 374 | if (req != 0) { |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 375 | if (count > ADB_BULK_BUFFER_SIZE) |
| 376 | xfer = ADB_BULK_BUFFER_SIZE; |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 377 | else |
| 378 | xfer = count; |
| 379 | if (copy_from_user(req->buf, buf, xfer)) { |
| 380 | r = -EFAULT; |
| 381 | break; |
| 382 | } |
| 383 | |
| 384 | req->length = xfer; |
| 385 | ret = usb_ep_queue(dev->ep_in, req, GFP_ATOMIC); |
| 386 | if (ret < 0) { |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 387 | pr_debug("adb_write: xfer error %d\n", ret); |
Manu Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 388 | atomic_set(&dev->error, 1); |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 389 | r = -EIO; |
| 390 | break; |
| 391 | } |
| 392 | |
| 393 | buf += xfer; |
| 394 | count -= xfer; |
| 395 | |
| 396 | /* zero this so we don't try to free it on error exit */ |
| 397 | req = 0; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | if (req) |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 402 | adb_req_put(dev, &dev->tx_idle, req); |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 403 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 404 | adb_unlock(&dev->write_excl); |
| 405 | pr_debug("adb_write returning %d\n", r); |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 406 | return r; |
| 407 | } |
| 408 | |
| 409 | static int adb_open(struct inode *ip, struct file *fp) |
| 410 | { |
| 411 | printk(KERN_INFO "adb_open\n"); |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 412 | if (!_adb_dev) |
| 413 | return -ENODEV; |
| 414 | |
| 415 | if (adb_lock(&_adb_dev->open_excl)) |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 416 | return -EBUSY; |
| 417 | |
| 418 | fp->private_data = _adb_dev; |
| 419 | |
| 420 | /* clear the error latch */ |
Manu Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 421 | atomic_set(&_adb_dev->error, 0); |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 422 | |
| 423 | return 0; |
| 424 | } |
| 425 | |
| 426 | static int adb_release(struct inode *ip, struct file *fp) |
| 427 | { |
| 428 | printk(KERN_INFO "adb_release\n"); |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 429 | adb_unlock(&_adb_dev->open_excl); |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | /* file operations for ADB device /dev/android_adb */ |
| 434 | static struct file_operations adb_fops = { |
| 435 | .owner = THIS_MODULE, |
| 436 | .read = adb_read, |
| 437 | .write = adb_write, |
| 438 | .open = adb_open, |
| 439 | .release = adb_release, |
| 440 | }; |
| 441 | |
| 442 | static struct miscdevice adb_device = { |
| 443 | .minor = MISC_DYNAMIC_MINOR, |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 444 | .name = adb_shortname, |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 445 | .fops = &adb_fops, |
| 446 | }; |
| 447 | |
Krishna, Vamsi | 83814ea | 2009-02-11 21:07:20 +0530 | [diff] [blame] | 448 | |
Krishna, Vamsi | 83814ea | 2009-02-11 21:07:20 +0530 | [diff] [blame] | 449 | |
Krishna, Vamsi | 83814ea | 2009-02-11 21:07:20 +0530 | [diff] [blame] | 450 | |
| 451 | static int |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 452 | adb_function_bind(struct usb_configuration *c, struct usb_function *f) |
| 453 | { |
| 454 | struct usb_composite_dev *cdev = c->cdev; |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 455 | struct adb_dev *dev = func_to_adb(f); |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 456 | int id; |
| 457 | int ret; |
| 458 | |
| 459 | dev->cdev = cdev; |
| 460 | DBG(cdev, "adb_function_bind dev: %p\n", dev); |
| 461 | |
| 462 | /* allocate interface ID(s) */ |
| 463 | id = usb_interface_id(c, f); |
| 464 | if (id < 0) |
| 465 | return id; |
| 466 | adb_interface_desc.bInterfaceNumber = id; |
| 467 | |
| 468 | /* allocate endpoints */ |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 469 | ret = adb_create_bulk_endpoints(dev, &adb_fullspeed_in_desc, |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 470 | &adb_fullspeed_out_desc); |
| 471 | if (ret) |
| 472 | return ret; |
| 473 | |
| 474 | /* support high speed hardware */ |
| 475 | if (gadget_is_dualspeed(c->cdev->gadget)) { |
| 476 | adb_highspeed_in_desc.bEndpointAddress = |
| 477 | adb_fullspeed_in_desc.bEndpointAddress; |
| 478 | adb_highspeed_out_desc.bEndpointAddress = |
| 479 | adb_fullspeed_out_desc.bEndpointAddress; |
| 480 | } |
| 481 | |
| 482 | DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n", |
| 483 | gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full", |
| 484 | f->name, dev->ep_in->name, dev->ep_out->name); |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | static void |
| 489 | adb_function_unbind(struct usb_configuration *c, struct usb_function *f) |
| 490 | { |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 491 | struct adb_dev *dev = func_to_adb(f); |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 492 | struct usb_request *req; |
| 493 | |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 494 | |
Manu Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 495 | atomic_set(&dev->online, 0); |
| 496 | atomic_set(&dev->error, 1); |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 497 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 498 | wake_up(&dev->read_wq); |
| 499 | |
| 500 | adb_request_free(dev->rx_req, dev->ep_out); |
| 501 | while ((req = adb_req_get(dev, &dev->tx_idle))) |
| 502 | adb_request_free(req, dev->ep_in); |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | static int adb_function_set_alt(struct usb_function *f, |
| 506 | unsigned intf, unsigned alt) |
| 507 | { |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 508 | struct adb_dev *dev = func_to_adb(f); |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 509 | struct usb_composite_dev *cdev = f->config->cdev; |
| 510 | int ret; |
| 511 | |
| 512 | DBG(cdev, "adb_function_set_alt intf: %d alt: %d\n", intf, alt); |
| 513 | ret = usb_ep_enable(dev->ep_in, |
| 514 | ep_choose(cdev->gadget, |
| 515 | &adb_highspeed_in_desc, |
| 516 | &adb_fullspeed_in_desc)); |
| 517 | if (ret) |
| 518 | return ret; |
| 519 | ret = usb_ep_enable(dev->ep_out, |
| 520 | ep_choose(cdev->gadget, |
| 521 | &adb_highspeed_out_desc, |
| 522 | &adb_fullspeed_out_desc)); |
| 523 | if (ret) { |
| 524 | usb_ep_disable(dev->ep_in); |
| 525 | return ret; |
| 526 | } |
Manu Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 527 | atomic_set(&dev->online, 1); |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 528 | |
| 529 | /* readers may be blocked waiting for us to go online */ |
| 530 | wake_up(&dev->read_wq); |
| 531 | return 0; |
| 532 | } |
| 533 | |
| 534 | static void adb_function_disable(struct usb_function *f) |
| 535 | { |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 536 | struct adb_dev *dev = func_to_adb(f); |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 537 | struct usb_composite_dev *cdev = dev->cdev; |
| 538 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 539 | DBG(cdev, "adb_function_disable cdev %p\n", cdev); |
Manu Gautam | 8f68795 | 2011-09-30 15:07:36 +0530 | [diff] [blame] | 540 | atomic_set(&dev->online, 0); |
| 541 | atomic_set(&dev->error, 1); |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 542 | usb_ep_disable(dev->ep_in); |
| 543 | usb_ep_disable(dev->ep_out); |
| 544 | |
| 545 | /* readers may be blocked waiting for us to go online */ |
| 546 | wake_up(&dev->read_wq); |
| 547 | |
| 548 | VDBG(cdev, "%s disabled\n", dev->function.name); |
| 549 | } |
| 550 | |
Krishna, Vamsi | 83814ea | 2009-02-11 21:07:20 +0530 | [diff] [blame] | 551 | static int adb_bind_config(struct usb_configuration *c) |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 552 | { |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 553 | struct adb_dev *dev = _adb_dev; |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 554 | |
Krishna, Vamsi | 83814ea | 2009-02-11 21:07:20 +0530 | [diff] [blame] | 555 | printk(KERN_INFO "adb_bind_config\n"); |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 556 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 557 | dev->cdev = c->cdev; |
| 558 | dev->function.name = "adb"; |
| 559 | dev->function.descriptors = fs_adb_descs; |
| 560 | dev->function.hs_descriptors = hs_adb_descs; |
| 561 | dev->function.bind = adb_function_bind; |
| 562 | dev->function.unbind = adb_function_unbind; |
| 563 | dev->function.set_alt = adb_function_set_alt; |
| 564 | dev->function.disable = adb_function_disable; |
| 565 | |
| 566 | return usb_add_function(c, &dev->function); |
| 567 | } |
| 568 | |
| 569 | static int adb_setup(void) |
| 570 | { |
| 571 | struct adb_dev *dev; |
| 572 | int ret; |
| 573 | |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 574 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 575 | if (!dev) |
| 576 | return -ENOMEM; |
| 577 | |
| 578 | spin_lock_init(&dev->lock); |
| 579 | |
| 580 | init_waitqueue_head(&dev->read_wq); |
| 581 | init_waitqueue_head(&dev->write_wq); |
| 582 | |
| 583 | atomic_set(&dev->open_excl, 0); |
| 584 | atomic_set(&dev->read_excl, 0); |
| 585 | atomic_set(&dev->write_excl, 0); |
| 586 | |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 587 | INIT_LIST_HEAD(&dev->tx_idle); |
| 588 | |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 589 | _adb_dev = dev; |
| 590 | |
| 591 | ret = misc_register(&adb_device); |
| 592 | if (ret) |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 593 | goto err; |
Krishna, Vamsi | 83814ea | 2009-02-11 21:07:20 +0530 | [diff] [blame] | 594 | |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 595 | return 0; |
| 596 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 597 | err: |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 598 | kfree(dev); |
| 599 | printk(KERN_ERR "adb gadget driver failed to initialize\n"); |
| 600 | return ret; |
| 601 | } |
| 602 | |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 603 | static void adb_cleanup(void) |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 604 | { |
Benoit Goby | aab9681 | 2011-04-19 20:37:33 -0700 | [diff] [blame] | 605 | misc_deregister(&adb_device); |
| 606 | |
| 607 | kfree(_adb_dev); |
| 608 | _adb_dev = NULL; |
Mike Lockwood | 7f0d7bd | 2008-12-02 22:01:33 -0500 | [diff] [blame] | 609 | } |