Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Gadget Function Driver for MTP |
| 3 | * |
| 4 | * Copyright (C) 2010 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 | /* #define DEBUG */ |
| 19 | /* #define VERBOSE_DEBUG */ |
| 20 | |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/poll.h> |
| 24 | #include <linux/delay.h> |
| 25 | #include <linux/wait.h> |
| 26 | #include <linux/err.h> |
| 27 | #include <linux/interrupt.h> |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 28 | |
| 29 | #include <linux/types.h> |
| 30 | #include <linux/file.h> |
| 31 | #include <linux/device.h> |
| 32 | #include <linux/miscdevice.h> |
| 33 | |
| 34 | #include <linux/usb.h> |
| 35 | #include <linux/usb_usual.h> |
| 36 | #include <linux/usb/ch9.h> |
| 37 | #include <linux/usb/android_composite.h> |
| 38 | #include <linux/usb/f_mtp.h> |
| 39 | |
| 40 | #define BULK_BUFFER_SIZE 16384 |
Mike Lockwood | 1de4d4d | 2010-07-06 19:27:52 -0400 | [diff] [blame] | 41 | #define INTR_BUFFER_SIZE 28 |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 42 | |
| 43 | /* String IDs */ |
| 44 | #define INTERFACE_STRING_INDEX 0 |
| 45 | |
| 46 | /* values for mtp_dev.state */ |
| 47 | #define STATE_OFFLINE 0 /* initial state, disconnected */ |
| 48 | #define STATE_READY 1 /* ready for userspace calls */ |
| 49 | #define STATE_BUSY 2 /* processing userspace calls */ |
| 50 | #define STATE_CANCELED 3 /* transaction canceled by host */ |
| 51 | #define STATE_ERROR 4 /* error from completion routine */ |
| 52 | |
| 53 | /* number of tx and rx requests to allocate */ |
| 54 | #define TX_REQ_MAX 4 |
| 55 | #define RX_REQ_MAX 2 |
| 56 | |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 57 | /* ID for Microsoft MTP OS String */ |
| 58 | #define MTP_OS_STRING_ID 0xEE |
| 59 | |
| 60 | /* MTP class reqeusts */ |
| 61 | #define MTP_REQ_CANCEL 0x64 |
| 62 | #define MTP_REQ_GET_EXT_EVENT_DATA 0x65 |
| 63 | #define MTP_REQ_RESET 0x66 |
| 64 | #define MTP_REQ_GET_DEVICE_STATUS 0x67 |
| 65 | |
| 66 | /* constants for device status */ |
| 67 | #define MTP_RESPONSE_OK 0x2001 |
| 68 | #define MTP_RESPONSE_DEVICE_BUSY 0x2019 |
| 69 | |
| 70 | static const char shortname[] = "mtp_usb"; |
| 71 | |
| 72 | struct mtp_dev { |
| 73 | struct usb_function function; |
| 74 | struct usb_composite_dev *cdev; |
| 75 | spinlock_t lock; |
| 76 | |
Mike Lockwood | 1de4d4d | 2010-07-06 19:27:52 -0400 | [diff] [blame] | 77 | /* appear as MTP or PTP when enumerating */ |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 78 | int interface_mode; |
| 79 | |
| 80 | struct usb_ep *ep_in; |
| 81 | struct usb_ep *ep_out; |
| 82 | struct usb_ep *ep_intr; |
| 83 | |
| 84 | int state; |
| 85 | |
Mike Lockwood | 1de4d4d | 2010-07-06 19:27:52 -0400 | [diff] [blame] | 86 | /* synchronize access to our device file */ |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 87 | atomic_t open_excl; |
Mike Lockwood | 491d418 | 2010-11-08 10:41:31 -0500 | [diff] [blame^] | 88 | /* to enforce only one ioctl at a time */ |
| 89 | atomic_t ioctl_excl; |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 90 | |
| 91 | struct list_head tx_idle; |
| 92 | |
| 93 | wait_queue_head_t read_wq; |
| 94 | wait_queue_head_t write_wq; |
Mike Lockwood | 1de4d4d | 2010-07-06 19:27:52 -0400 | [diff] [blame] | 95 | wait_queue_head_t intr_wq; |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 96 | struct usb_request *rx_req[RX_REQ_MAX]; |
Mike Lockwood | 1de4d4d | 2010-07-06 19:27:52 -0400 | [diff] [blame] | 97 | struct usb_request *intr_req; |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 98 | int rx_done; |
Mike Lockwood | 1de4d4d | 2010-07-06 19:27:52 -0400 | [diff] [blame] | 99 | /* true if interrupt endpoint is busy */ |
| 100 | int intr_busy; |
| 101 | |
Mike Lockwood | 491d418 | 2010-11-08 10:41:31 -0500 | [diff] [blame^] | 102 | /* for processing MTP_SEND_FILE and MTP_RECEIVE_FILE |
| 103 | * ioctls on a work queue |
| 104 | */ |
| 105 | struct workqueue_struct *wq; |
| 106 | struct work_struct send_file_work; |
| 107 | struct work_struct receive_file_work; |
| 108 | struct file *xfer_file; |
| 109 | loff_t xfer_file_offset; |
| 110 | size_t xfer_file_length; |
| 111 | int xfer_result; |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | static struct usb_interface_descriptor mtp_interface_desc = { |
| 115 | .bLength = USB_DT_INTERFACE_SIZE, |
| 116 | .bDescriptorType = USB_DT_INTERFACE, |
| 117 | .bInterfaceNumber = 0, |
| 118 | .bNumEndpoints = 3, |
| 119 | .bInterfaceClass = USB_CLASS_VENDOR_SPEC, |
| 120 | .bInterfaceSubClass = USB_SUBCLASS_VENDOR_SPEC, |
| 121 | .bInterfaceProtocol = 0, |
| 122 | }; |
| 123 | |
| 124 | static struct usb_interface_descriptor ptp_interface_desc = { |
| 125 | .bLength = USB_DT_INTERFACE_SIZE, |
| 126 | .bDescriptorType = USB_DT_INTERFACE, |
| 127 | .bInterfaceNumber = 0, |
| 128 | .bNumEndpoints = 3, |
| 129 | .bInterfaceClass = USB_CLASS_STILL_IMAGE, |
| 130 | .bInterfaceSubClass = 1, |
| 131 | .bInterfaceProtocol = 1, |
| 132 | }; |
| 133 | |
| 134 | static struct usb_endpoint_descriptor mtp_highspeed_in_desc = { |
| 135 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 136 | .bDescriptorType = USB_DT_ENDPOINT, |
| 137 | .bEndpointAddress = USB_DIR_IN, |
| 138 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 139 | .wMaxPacketSize = __constant_cpu_to_le16(512), |
| 140 | }; |
| 141 | |
| 142 | static struct usb_endpoint_descriptor mtp_highspeed_out_desc = { |
| 143 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 144 | .bDescriptorType = USB_DT_ENDPOINT, |
| 145 | .bEndpointAddress = USB_DIR_OUT, |
| 146 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 147 | .wMaxPacketSize = __constant_cpu_to_le16(512), |
| 148 | }; |
| 149 | |
| 150 | static struct usb_endpoint_descriptor mtp_fullspeed_in_desc = { |
| 151 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 152 | .bDescriptorType = USB_DT_ENDPOINT, |
| 153 | .bEndpointAddress = USB_DIR_IN, |
| 154 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 155 | }; |
| 156 | |
| 157 | static struct usb_endpoint_descriptor mtp_fullspeed_out_desc = { |
| 158 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 159 | .bDescriptorType = USB_DT_ENDPOINT, |
| 160 | .bEndpointAddress = USB_DIR_OUT, |
| 161 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 162 | }; |
| 163 | |
| 164 | static struct usb_endpoint_descriptor mtp_intr_desc = { |
| 165 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 166 | .bDescriptorType = USB_DT_ENDPOINT, |
| 167 | .bEndpointAddress = USB_DIR_IN, |
| 168 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
Mike Lockwood | 1de4d4d | 2010-07-06 19:27:52 -0400 | [diff] [blame] | 169 | .wMaxPacketSize = __constant_cpu_to_le16(INTR_BUFFER_SIZE), |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 170 | .bInterval = 6, |
| 171 | }; |
| 172 | |
| 173 | static struct usb_descriptor_header *fs_mtp_descs[] = { |
| 174 | (struct usb_descriptor_header *) &mtp_interface_desc, |
| 175 | (struct usb_descriptor_header *) &mtp_fullspeed_in_desc, |
| 176 | (struct usb_descriptor_header *) &mtp_fullspeed_out_desc, |
| 177 | (struct usb_descriptor_header *) &mtp_intr_desc, |
| 178 | NULL, |
| 179 | }; |
| 180 | |
| 181 | static struct usb_descriptor_header *hs_mtp_descs[] = { |
| 182 | (struct usb_descriptor_header *) &mtp_interface_desc, |
| 183 | (struct usb_descriptor_header *) &mtp_highspeed_in_desc, |
| 184 | (struct usb_descriptor_header *) &mtp_highspeed_out_desc, |
| 185 | (struct usb_descriptor_header *) &mtp_intr_desc, |
| 186 | NULL, |
| 187 | }; |
| 188 | |
| 189 | static struct usb_descriptor_header *fs_ptp_descs[] = { |
| 190 | (struct usb_descriptor_header *) &ptp_interface_desc, |
| 191 | (struct usb_descriptor_header *) &mtp_fullspeed_in_desc, |
| 192 | (struct usb_descriptor_header *) &mtp_fullspeed_out_desc, |
| 193 | (struct usb_descriptor_header *) &mtp_intr_desc, |
| 194 | NULL, |
| 195 | }; |
| 196 | |
| 197 | static struct usb_descriptor_header *hs_ptp_descs[] = { |
| 198 | (struct usb_descriptor_header *) &ptp_interface_desc, |
| 199 | (struct usb_descriptor_header *) &mtp_highspeed_in_desc, |
| 200 | (struct usb_descriptor_header *) &mtp_highspeed_out_desc, |
| 201 | (struct usb_descriptor_header *) &mtp_intr_desc, |
| 202 | NULL, |
| 203 | }; |
| 204 | |
| 205 | static struct usb_string mtp_string_defs[] = { |
| 206 | /* Naming interface "MTP" so libmtp will recognize us */ |
| 207 | [INTERFACE_STRING_INDEX].s = "MTP", |
| 208 | { }, /* end of list */ |
| 209 | }; |
| 210 | |
| 211 | static struct usb_gadget_strings mtp_string_table = { |
| 212 | .language = 0x0409, /* en-US */ |
| 213 | .strings = mtp_string_defs, |
| 214 | }; |
| 215 | |
| 216 | static struct usb_gadget_strings *mtp_strings[] = { |
| 217 | &mtp_string_table, |
| 218 | NULL, |
| 219 | }; |
| 220 | |
| 221 | /* Microsoft MTP OS String */ |
| 222 | static u8 mtp_os_string[] = { |
| 223 | 18, /* sizeof(mtp_os_string) */ |
| 224 | USB_DT_STRING, |
| 225 | /* Signature field: "MSFT100" */ |
| 226 | 'M', 0, 'S', 0, 'F', 0, 'T', 0, '1', 0, '0', 0, '0', 0, |
| 227 | /* vendor code */ |
| 228 | 1, |
| 229 | /* padding */ |
| 230 | 0 |
| 231 | }; |
| 232 | |
| 233 | /* Microsoft Extended Configuration Descriptor Header Section */ |
| 234 | struct mtp_ext_config_desc_header { |
| 235 | __le32 dwLength; |
| 236 | __u16 bcdVersion; |
| 237 | __le16 wIndex; |
| 238 | __u8 bCount; |
| 239 | __u8 reserved[7]; |
| 240 | }; |
| 241 | |
| 242 | /* Microsoft Extended Configuration Descriptor Function Section */ |
| 243 | struct mtp_ext_config_desc_function { |
| 244 | __u8 bFirstInterfaceNumber; |
| 245 | __u8 bInterfaceCount; |
| 246 | __u8 compatibleID[8]; |
| 247 | __u8 subCompatibleID[8]; |
| 248 | __u8 reserved[6]; |
| 249 | }; |
| 250 | |
| 251 | /* MTP Extended Configuration Descriptor */ |
| 252 | struct { |
| 253 | struct mtp_ext_config_desc_header header; |
| 254 | struct mtp_ext_config_desc_function function; |
| 255 | } mtp_ext_config_desc = { |
| 256 | .header = { |
| 257 | .dwLength = __constant_cpu_to_le32(sizeof(mtp_ext_config_desc)), |
| 258 | .bcdVersion = __constant_cpu_to_le16(0x0100), |
| 259 | .wIndex = __constant_cpu_to_le16(4), |
| 260 | .bCount = __constant_cpu_to_le16(1), |
| 261 | }, |
| 262 | .function = { |
| 263 | .bFirstInterfaceNumber = 0, |
| 264 | .bInterfaceCount = 1, |
| 265 | .compatibleID = { 'M', 'T', 'P' }, |
| 266 | }, |
| 267 | }; |
| 268 | |
| 269 | struct mtp_device_status { |
| 270 | __le16 wLength; |
| 271 | __le16 wCode; |
| 272 | }; |
| 273 | |
| 274 | /* temporary variable used between mtp_open() and mtp_gadget_bind() */ |
| 275 | static struct mtp_dev *_mtp_dev; |
| 276 | |
| 277 | static inline struct mtp_dev *func_to_dev(struct usb_function *f) |
| 278 | { |
| 279 | return container_of(f, struct mtp_dev, function); |
| 280 | } |
| 281 | |
| 282 | static struct usb_request *mtp_request_new(struct usb_ep *ep, int buffer_size) |
| 283 | { |
| 284 | struct usb_request *req = usb_ep_alloc_request(ep, GFP_KERNEL); |
| 285 | if (!req) |
| 286 | return NULL; |
| 287 | |
| 288 | /* now allocate buffers for the requests */ |
| 289 | req->buf = kmalloc(buffer_size, GFP_KERNEL); |
| 290 | if (!req->buf) { |
| 291 | usb_ep_free_request(ep, req); |
| 292 | return NULL; |
| 293 | } |
| 294 | |
| 295 | return req; |
| 296 | } |
| 297 | |
| 298 | static void mtp_request_free(struct usb_request *req, struct usb_ep *ep) |
| 299 | { |
| 300 | if (req) { |
| 301 | kfree(req->buf); |
| 302 | usb_ep_free_request(ep, req); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | static inline int _lock(atomic_t *excl) |
| 307 | { |
| 308 | if (atomic_inc_return(excl) == 1) { |
| 309 | return 0; |
| 310 | } else { |
| 311 | atomic_dec(excl); |
| 312 | return -1; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | static inline void _unlock(atomic_t *excl) |
| 317 | { |
| 318 | atomic_dec(excl); |
| 319 | } |
| 320 | |
| 321 | /* add a request to the tail of a list */ |
| 322 | static void req_put(struct mtp_dev *dev, struct list_head *head, |
| 323 | struct usb_request *req) |
| 324 | { |
| 325 | unsigned long flags; |
| 326 | |
| 327 | spin_lock_irqsave(&dev->lock, flags); |
| 328 | list_add_tail(&req->list, head); |
| 329 | spin_unlock_irqrestore(&dev->lock, flags); |
| 330 | } |
| 331 | |
| 332 | /* remove a request from the head of a list */ |
| 333 | static struct usb_request *req_get(struct mtp_dev *dev, struct list_head *head) |
| 334 | { |
| 335 | unsigned long flags; |
| 336 | struct usb_request *req; |
| 337 | |
| 338 | spin_lock_irqsave(&dev->lock, flags); |
| 339 | if (list_empty(head)) { |
| 340 | req = 0; |
| 341 | } else { |
| 342 | req = list_first_entry(head, struct usb_request, list); |
| 343 | list_del(&req->list); |
| 344 | } |
| 345 | spin_unlock_irqrestore(&dev->lock, flags); |
| 346 | return req; |
| 347 | } |
| 348 | |
| 349 | static void mtp_complete_in(struct usb_ep *ep, struct usb_request *req) |
| 350 | { |
| 351 | struct mtp_dev *dev = _mtp_dev; |
| 352 | |
| 353 | if (req->status != 0) |
| 354 | dev->state = STATE_ERROR; |
| 355 | |
| 356 | req_put(dev, &dev->tx_idle, req); |
| 357 | |
| 358 | wake_up(&dev->write_wq); |
| 359 | } |
| 360 | |
| 361 | static void mtp_complete_out(struct usb_ep *ep, struct usb_request *req) |
| 362 | { |
| 363 | struct mtp_dev *dev = _mtp_dev; |
| 364 | |
| 365 | dev->rx_done = 1; |
| 366 | if (req->status != 0) |
| 367 | dev->state = STATE_ERROR; |
| 368 | |
| 369 | wake_up(&dev->read_wq); |
| 370 | } |
| 371 | |
Mike Lockwood | 1de4d4d | 2010-07-06 19:27:52 -0400 | [diff] [blame] | 372 | static void mtp_complete_intr(struct usb_ep *ep, struct usb_request *req) |
| 373 | { |
| 374 | struct mtp_dev *dev = _mtp_dev; |
| 375 | |
| 376 | DBG(dev->cdev, "mtp_complete_intr status: %d actual: %d\n", req->status, req->actual); |
| 377 | dev->intr_busy = 0; |
| 378 | if (req->status != 0) |
| 379 | dev->state = STATE_ERROR; |
| 380 | |
| 381 | wake_up(&dev->intr_wq); |
| 382 | } |
| 383 | |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 384 | static int __init create_bulk_endpoints(struct mtp_dev *dev, |
| 385 | struct usb_endpoint_descriptor *in_desc, |
| 386 | struct usb_endpoint_descriptor *out_desc, |
| 387 | struct usb_endpoint_descriptor *intr_desc) |
| 388 | { |
| 389 | struct usb_composite_dev *cdev = dev->cdev; |
| 390 | struct usb_request *req; |
| 391 | struct usb_ep *ep; |
| 392 | int i; |
| 393 | |
| 394 | DBG(cdev, "create_bulk_endpoints dev: %p\n", dev); |
| 395 | |
| 396 | ep = usb_ep_autoconfig(cdev->gadget, in_desc); |
| 397 | if (!ep) { |
| 398 | DBG(cdev, "usb_ep_autoconfig for ep_in failed\n"); |
| 399 | return -ENODEV; |
| 400 | } |
| 401 | DBG(cdev, "usb_ep_autoconfig for ep_in got %s\n", ep->name); |
| 402 | ep->driver_data = dev; /* claim the endpoint */ |
| 403 | dev->ep_in = ep; |
| 404 | |
| 405 | ep = usb_ep_autoconfig(cdev->gadget, out_desc); |
| 406 | if (!ep) { |
| 407 | DBG(cdev, "usb_ep_autoconfig for ep_out failed\n"); |
| 408 | return -ENODEV; |
| 409 | } |
| 410 | DBG(cdev, "usb_ep_autoconfig for mtp ep_out got %s\n", ep->name); |
| 411 | ep->driver_data = dev; /* claim the endpoint */ |
| 412 | dev->ep_out = ep; |
| 413 | |
| 414 | ep = usb_ep_autoconfig(cdev->gadget, out_desc); |
| 415 | if (!ep) { |
| 416 | DBG(cdev, "usb_ep_autoconfig for ep_out failed\n"); |
| 417 | return -ENODEV; |
| 418 | } |
| 419 | DBG(cdev, "usb_ep_autoconfig for mtp ep_out got %s\n", ep->name); |
| 420 | ep->driver_data = dev; /* claim the endpoint */ |
| 421 | dev->ep_out = ep; |
| 422 | |
| 423 | ep = usb_ep_autoconfig(cdev->gadget, intr_desc); |
| 424 | if (!ep) { |
| 425 | DBG(cdev, "usb_ep_autoconfig for ep_intr failed\n"); |
| 426 | return -ENODEV; |
| 427 | } |
| 428 | DBG(cdev, "usb_ep_autoconfig for mtp ep_intr got %s\n", ep->name); |
| 429 | ep->driver_data = dev; /* claim the endpoint */ |
| 430 | dev->ep_intr = ep; |
| 431 | |
| 432 | /* now allocate requests for our endpoints */ |
| 433 | for (i = 0; i < TX_REQ_MAX; i++) { |
| 434 | req = mtp_request_new(dev->ep_in, BULK_BUFFER_SIZE); |
| 435 | if (!req) |
| 436 | goto fail; |
| 437 | req->complete = mtp_complete_in; |
| 438 | req_put(dev, &dev->tx_idle, req); |
| 439 | } |
| 440 | for (i = 0; i < RX_REQ_MAX; i++) { |
| 441 | req = mtp_request_new(dev->ep_out, BULK_BUFFER_SIZE); |
| 442 | if (!req) |
| 443 | goto fail; |
| 444 | req->complete = mtp_complete_out; |
| 445 | dev->rx_req[i] = req; |
| 446 | } |
Mike Lockwood | 1de4d4d | 2010-07-06 19:27:52 -0400 | [diff] [blame] | 447 | req = mtp_request_new(dev->ep_intr, INTR_BUFFER_SIZE); |
| 448 | if (!req) |
| 449 | goto fail; |
| 450 | req->complete = mtp_complete_intr; |
| 451 | dev->intr_req = req; |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 452 | |
| 453 | return 0; |
| 454 | |
| 455 | fail: |
| 456 | printk(KERN_ERR "mtp_bind() could not allocate requests\n"); |
| 457 | return -1; |
| 458 | } |
| 459 | |
| 460 | static ssize_t mtp_read(struct file *fp, char __user *buf, |
| 461 | size_t count, loff_t *pos) |
| 462 | { |
| 463 | struct mtp_dev *dev = fp->private_data; |
| 464 | struct usb_composite_dev *cdev = dev->cdev; |
| 465 | struct usb_request *req; |
| 466 | int r = count, xfer; |
| 467 | int ret = 0; |
| 468 | |
| 469 | DBG(cdev, "mtp_read(%d)\n", count); |
| 470 | |
| 471 | if (count > BULK_BUFFER_SIZE) |
| 472 | return -EINVAL; |
| 473 | |
| 474 | /* we will block until we're online */ |
| 475 | DBG(cdev, "mtp_read: waiting for online state\n"); |
| 476 | ret = wait_event_interruptible(dev->read_wq, |
| 477 | dev->state != STATE_OFFLINE); |
| 478 | if (ret < 0) { |
| 479 | r = ret; |
| 480 | goto done; |
| 481 | } |
| 482 | spin_lock_irq(&dev->lock); |
| 483 | if (dev->state == STATE_CANCELED) { |
| 484 | /* report cancelation to userspace */ |
| 485 | dev->state = STATE_READY; |
| 486 | spin_unlock_irq(&dev->lock); |
| 487 | return -ECANCELED; |
| 488 | } |
| 489 | dev->state = STATE_BUSY; |
| 490 | spin_unlock_irq(&dev->lock); |
| 491 | |
| 492 | requeue_req: |
| 493 | /* queue a request */ |
| 494 | req = dev->rx_req[0]; |
| 495 | req->length = count; |
| 496 | dev->rx_done = 0; |
Mike Lockwood | 1de4d4d | 2010-07-06 19:27:52 -0400 | [diff] [blame] | 497 | ret = usb_ep_queue(dev->ep_out, req, GFP_KERNEL); |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 498 | if (ret < 0) { |
| 499 | r = -EIO; |
| 500 | goto done; |
| 501 | } else { |
| 502 | DBG(cdev, "rx %p queue\n", req); |
| 503 | } |
| 504 | |
| 505 | /* wait for a request to complete */ |
| 506 | ret = wait_event_interruptible(dev->read_wq, dev->rx_done); |
| 507 | if (ret < 0) { |
| 508 | r = ret; |
| 509 | goto done; |
| 510 | } |
| 511 | if (dev->state == STATE_BUSY) { |
| 512 | /* If we got a 0-len packet, throw it back and try again. */ |
| 513 | if (req->actual == 0) |
| 514 | goto requeue_req; |
| 515 | |
| 516 | DBG(cdev, "rx %p %d\n", req, req->actual); |
| 517 | xfer = (req->actual < count) ? req->actual : count; |
| 518 | r = xfer; |
| 519 | if (copy_to_user(buf, req->buf, xfer)) |
| 520 | r = -EFAULT; |
| 521 | } else |
| 522 | r = -EIO; |
| 523 | |
| 524 | done: |
| 525 | spin_lock_irq(&dev->lock); |
| 526 | if (dev->state == STATE_CANCELED) |
| 527 | r = -ECANCELED; |
| 528 | else if (dev->state != STATE_OFFLINE) |
| 529 | dev->state = STATE_READY; |
| 530 | spin_unlock_irq(&dev->lock); |
| 531 | |
| 532 | DBG(cdev, "mtp_read returning %d\n", r); |
| 533 | return r; |
| 534 | } |
| 535 | |
| 536 | static ssize_t mtp_write(struct file *fp, const char __user *buf, |
| 537 | size_t count, loff_t *pos) |
| 538 | { |
| 539 | struct mtp_dev *dev = fp->private_data; |
| 540 | struct usb_composite_dev *cdev = dev->cdev; |
| 541 | struct usb_request *req = 0; |
| 542 | int r = count, xfer; |
| 543 | int ret; |
| 544 | |
| 545 | DBG(cdev, "mtp_write(%d)\n", count); |
| 546 | |
| 547 | spin_lock_irq(&dev->lock); |
| 548 | if (dev->state == STATE_CANCELED) { |
| 549 | /* report cancelation to userspace */ |
| 550 | dev->state = STATE_READY; |
| 551 | spin_unlock_irq(&dev->lock); |
| 552 | return -ECANCELED; |
| 553 | } |
| 554 | if (dev->state == STATE_OFFLINE) { |
| 555 | spin_unlock_irq(&dev->lock); |
| 556 | return -ENODEV; |
| 557 | } |
| 558 | dev->state = STATE_BUSY; |
| 559 | spin_unlock_irq(&dev->lock); |
| 560 | |
| 561 | while (count > 0) { |
| 562 | if (dev->state != STATE_BUSY) { |
| 563 | DBG(cdev, "mtp_write dev->error\n"); |
| 564 | r = -EIO; |
| 565 | break; |
| 566 | } |
| 567 | |
| 568 | /* get an idle tx request to use */ |
| 569 | req = 0; |
| 570 | ret = wait_event_interruptible(dev->write_wq, |
| 571 | ((req = req_get(dev, &dev->tx_idle)) |
| 572 | || dev->state != STATE_BUSY)); |
| 573 | if (!req) { |
| 574 | r = ret; |
| 575 | break; |
| 576 | } |
| 577 | |
| 578 | if (count > BULK_BUFFER_SIZE) |
| 579 | xfer = BULK_BUFFER_SIZE; |
| 580 | else |
| 581 | xfer = count; |
| 582 | if (copy_from_user(req->buf, buf, xfer)) { |
| 583 | r = -EFAULT; |
| 584 | break; |
| 585 | } |
| 586 | |
| 587 | req->length = xfer; |
Mike Lockwood | 1de4d4d | 2010-07-06 19:27:52 -0400 | [diff] [blame] | 588 | ret = usb_ep_queue(dev->ep_in, req, GFP_KERNEL); |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 589 | if (ret < 0) { |
| 590 | DBG(cdev, "mtp_write: xfer error %d\n", ret); |
| 591 | r = -EIO; |
| 592 | break; |
| 593 | } |
| 594 | |
| 595 | buf += xfer; |
| 596 | count -= xfer; |
| 597 | |
| 598 | /* zero this so we don't try to free it on error exit */ |
| 599 | req = 0; |
| 600 | } |
| 601 | |
| 602 | if (req) |
| 603 | req_put(dev, &dev->tx_idle, req); |
| 604 | |
| 605 | spin_lock_irq(&dev->lock); |
| 606 | if (dev->state == STATE_CANCELED) |
| 607 | r = -ECANCELED; |
| 608 | else if (dev->state != STATE_OFFLINE) |
| 609 | dev->state = STATE_READY; |
| 610 | spin_unlock_irq(&dev->lock); |
| 611 | |
| 612 | DBG(cdev, "mtp_write returning %d\n", r); |
| 613 | return r; |
| 614 | } |
| 615 | |
Mike Lockwood | 491d418 | 2010-11-08 10:41:31 -0500 | [diff] [blame^] | 616 | /* read from a local file and write to USB */ |
| 617 | static void send_file_work(struct work_struct *data) { |
| 618 | struct mtp_dev *dev = container_of(data, struct mtp_dev, send_file_work); |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 619 | struct usb_composite_dev *cdev = dev->cdev; |
| 620 | struct usb_request *req = 0; |
Mike Lockwood | 491d418 | 2010-11-08 10:41:31 -0500 | [diff] [blame^] | 621 | struct file *filp; |
| 622 | loff_t offset; |
| 623 | size_t count; |
| 624 | int r, xfer, ret; |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 625 | |
Mike Lockwood | 491d418 | 2010-11-08 10:41:31 -0500 | [diff] [blame^] | 626 | /* read our parameters */ |
| 627 | smp_rmb(); |
| 628 | filp = dev->xfer_file; |
| 629 | offset = dev->xfer_file_offset; |
| 630 | r = count = dev->xfer_file_length; |
| 631 | |
| 632 | DBG(cdev, "send_file_work(%lld %d)\n", offset, count); |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 633 | |
| 634 | while (count > 0) { |
| 635 | /* get an idle tx request to use */ |
| 636 | req = 0; |
| 637 | ret = wait_event_interruptible(dev->write_wq, |
| 638 | (req = req_get(dev, &dev->tx_idle)) |
| 639 | || dev->state != STATE_BUSY); |
| 640 | if (!req) { |
| 641 | r = ret; |
| 642 | break; |
| 643 | } |
| 644 | |
| 645 | if (count > BULK_BUFFER_SIZE) |
| 646 | xfer = BULK_BUFFER_SIZE; |
| 647 | else |
| 648 | xfer = count; |
| 649 | ret = vfs_read(filp, req->buf, xfer, &offset); |
| 650 | if (ret < 0) { |
| 651 | r = ret; |
| 652 | break; |
| 653 | } |
| 654 | xfer = ret; |
| 655 | |
| 656 | req->length = xfer; |
Mike Lockwood | 1de4d4d | 2010-07-06 19:27:52 -0400 | [diff] [blame] | 657 | ret = usb_ep_queue(dev->ep_in, req, GFP_KERNEL); |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 658 | if (ret < 0) { |
Mike Lockwood | 491d418 | 2010-11-08 10:41:31 -0500 | [diff] [blame^] | 659 | DBG(cdev, "send_file_work: xfer error %d\n", ret); |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 660 | dev->state = STATE_ERROR; |
| 661 | r = -EIO; |
| 662 | break; |
| 663 | } |
| 664 | |
| 665 | count -= xfer; |
| 666 | |
| 667 | /* zero this so we don't try to free it on error exit */ |
| 668 | req = 0; |
| 669 | } |
| 670 | |
| 671 | if (req) |
| 672 | req_put(dev, &dev->tx_idle, req); |
| 673 | |
Mike Lockwood | 491d418 | 2010-11-08 10:41:31 -0500 | [diff] [blame^] | 674 | DBG(cdev, "send_file_work returning %d\n", r); |
| 675 | /* write the result */ |
| 676 | dev->xfer_result = r; |
| 677 | smp_wmb(); |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 678 | } |
| 679 | |
Mike Lockwood | 491d418 | 2010-11-08 10:41:31 -0500 | [diff] [blame^] | 680 | /* read from USB and write to a local file */ |
| 681 | static void receive_file_work(struct work_struct *data) |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 682 | { |
Mike Lockwood | 491d418 | 2010-11-08 10:41:31 -0500 | [diff] [blame^] | 683 | struct mtp_dev *dev = container_of(data, struct mtp_dev, receive_file_work); |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 684 | struct usb_composite_dev *cdev = dev->cdev; |
| 685 | struct usb_request *read_req = NULL, *write_req = NULL; |
Mike Lockwood | 491d418 | 2010-11-08 10:41:31 -0500 | [diff] [blame^] | 686 | struct file *filp; |
| 687 | loff_t offset; |
| 688 | size_t count; |
| 689 | int r, ret, cur_buf = 0; |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 690 | |
Mike Lockwood | 491d418 | 2010-11-08 10:41:31 -0500 | [diff] [blame^] | 691 | /* read our parameters */ |
| 692 | smp_rmb(); |
| 693 | filp = dev->xfer_file; |
| 694 | offset = dev->xfer_file_offset; |
| 695 | r = count = dev->xfer_file_length; |
| 696 | |
| 697 | DBG(cdev, "receive_file_work(%d)\n", count); |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 698 | |
| 699 | while (count > 0 || write_req) { |
| 700 | if (count > 0) { |
| 701 | /* queue a request */ |
| 702 | read_req = dev->rx_req[cur_buf]; |
| 703 | cur_buf = (cur_buf + 1) % RX_REQ_MAX; |
| 704 | |
| 705 | read_req->length = (count > BULK_BUFFER_SIZE |
| 706 | ? BULK_BUFFER_SIZE : count); |
| 707 | dev->rx_done = 0; |
Mike Lockwood | 1de4d4d | 2010-07-06 19:27:52 -0400 | [diff] [blame] | 708 | ret = usb_ep_queue(dev->ep_out, read_req, GFP_KERNEL); |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 709 | if (ret < 0) { |
| 710 | r = -EIO; |
| 711 | dev->state = STATE_ERROR; |
| 712 | break; |
| 713 | } |
| 714 | count -= ret; |
| 715 | } |
| 716 | |
| 717 | if (write_req) { |
| 718 | DBG(cdev, "rx %p %d\n", write_req, write_req->actual); |
| 719 | ret = vfs_write(filp, write_req->buf, write_req->actual, |
| 720 | &offset); |
| 721 | DBG(cdev, "vfs_write %d\n", ret); |
| 722 | if (ret != write_req->actual) { |
| 723 | r = -EIO; |
| 724 | dev->state = STATE_ERROR; |
| 725 | break; |
| 726 | } |
| 727 | write_req = NULL; |
| 728 | } |
| 729 | |
| 730 | if (read_req) { |
| 731 | /* wait for our last read to complete */ |
| 732 | ret = wait_event_interruptible(dev->read_wq, |
| 733 | dev->rx_done || dev->state != STATE_BUSY); |
| 734 | if (ret < 0 || dev->state != STATE_BUSY) { |
| 735 | r = ret; |
| 736 | break; |
| 737 | } |
| 738 | count -= read_req->actual; |
| 739 | write_req = read_req; |
| 740 | read_req = NULL; |
| 741 | } |
| 742 | } |
| 743 | |
Mike Lockwood | 491d418 | 2010-11-08 10:41:31 -0500 | [diff] [blame^] | 744 | DBG(cdev, "receive_file_work returning %d\n", r); |
| 745 | /* write the result */ |
| 746 | dev->xfer_result = r; |
| 747 | smp_wmb(); |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 748 | } |
| 749 | |
Mike Lockwood | 1de4d4d | 2010-07-06 19:27:52 -0400 | [diff] [blame] | 750 | static int mtp_send_event(struct mtp_dev *dev, struct mtp_event *event) |
| 751 | { |
| 752 | struct usb_request *req; |
| 753 | int ret; |
| 754 | int length = event->length; |
| 755 | |
| 756 | DBG(dev->cdev, "mtp_send_event(%d)\n", event->length); |
| 757 | |
| 758 | if (length < 0 || length > INTR_BUFFER_SIZE) |
| 759 | return -EINVAL; |
| 760 | |
Mike Lockwood | 1de4d4d | 2010-07-06 19:27:52 -0400 | [diff] [blame] | 761 | /* wait for a request to complete */ |
| 762 | ret = wait_event_interruptible(dev->intr_wq, !dev->intr_busy || dev->state == STATE_OFFLINE); |
| 763 | if (ret < 0) |
Mike Lockwood | 491d418 | 2010-11-08 10:41:31 -0500 | [diff] [blame^] | 764 | return ret; |
| 765 | if (dev->state == STATE_OFFLINE) |
| 766 | return -ENODEV; |
Mike Lockwood | 1de4d4d | 2010-07-06 19:27:52 -0400 | [diff] [blame] | 767 | req = dev->intr_req; |
Mike Lockwood | 491d418 | 2010-11-08 10:41:31 -0500 | [diff] [blame^] | 768 | if (copy_from_user(req->buf, (void __user *)event->data, length)) |
| 769 | return -EFAULT; |
Mike Lockwood | 1de4d4d | 2010-07-06 19:27:52 -0400 | [diff] [blame] | 770 | req->length = length; |
| 771 | dev->intr_busy = 1; |
| 772 | ret = usb_ep_queue(dev->ep_intr, req, GFP_KERNEL); |
| 773 | if (ret) |
| 774 | dev->intr_busy = 0; |
| 775 | |
Mike Lockwood | 1de4d4d | 2010-07-06 19:27:52 -0400 | [diff] [blame] | 776 | return ret; |
| 777 | } |
| 778 | |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 779 | static long mtp_ioctl(struct file *fp, unsigned code, unsigned long value) |
| 780 | { |
| 781 | struct mtp_dev *dev = fp->private_data; |
| 782 | struct file *filp = NULL; |
| 783 | int ret = -EINVAL; |
| 784 | |
Mike Lockwood | 491d418 | 2010-11-08 10:41:31 -0500 | [diff] [blame^] | 785 | if (_lock(&dev->ioctl_excl)) |
| 786 | return -EBUSY; |
| 787 | |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 788 | switch (code) { |
| 789 | case MTP_SEND_FILE: |
| 790 | case MTP_RECEIVE_FILE: |
| 791 | { |
| 792 | struct mtp_file_range mfr; |
Mike Lockwood | 491d418 | 2010-11-08 10:41:31 -0500 | [diff] [blame^] | 793 | struct work_struct *work; |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 794 | |
| 795 | spin_lock_irq(&dev->lock); |
| 796 | if (dev->state == STATE_CANCELED) { |
| 797 | /* report cancelation to userspace */ |
| 798 | dev->state = STATE_READY; |
| 799 | spin_unlock_irq(&dev->lock); |
Mike Lockwood | 491d418 | 2010-11-08 10:41:31 -0500 | [diff] [blame^] | 800 | ret = -ECANCELED; |
| 801 | goto out; |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 802 | } |
| 803 | if (dev->state == STATE_OFFLINE) { |
| 804 | spin_unlock_irq(&dev->lock); |
Mike Lockwood | 491d418 | 2010-11-08 10:41:31 -0500 | [diff] [blame^] | 805 | ret = -ENODEV; |
| 806 | goto out; |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 807 | } |
| 808 | dev->state = STATE_BUSY; |
| 809 | spin_unlock_irq(&dev->lock); |
| 810 | |
| 811 | if (copy_from_user(&mfr, (void __user *)value, sizeof(mfr))) { |
| 812 | ret = -EFAULT; |
| 813 | goto fail; |
| 814 | } |
Mike Lockwood | 491d418 | 2010-11-08 10:41:31 -0500 | [diff] [blame^] | 815 | /* hold a reference to the file while we are working with it */ |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 816 | filp = fget(mfr.fd); |
| 817 | if (!filp) { |
| 818 | ret = -EBADF; |
| 819 | goto fail; |
| 820 | } |
| 821 | |
Mike Lockwood | 491d418 | 2010-11-08 10:41:31 -0500 | [diff] [blame^] | 822 | /* write the parameters */ |
| 823 | dev->xfer_file = filp; |
| 824 | dev->xfer_file_offset = mfr.offset; |
| 825 | dev->xfer_file_length = mfr.length; |
| 826 | smp_wmb(); |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 827 | |
| 828 | if (code == MTP_SEND_FILE) |
Mike Lockwood | 491d418 | 2010-11-08 10:41:31 -0500 | [diff] [blame^] | 829 | work = &dev->send_file_work; |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 830 | else |
Mike Lockwood | 491d418 | 2010-11-08 10:41:31 -0500 | [diff] [blame^] | 831 | work = &dev->receive_file_work; |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 832 | |
Mike Lockwood | 491d418 | 2010-11-08 10:41:31 -0500 | [diff] [blame^] | 833 | /* We do the file transfer on a work queue so it will run |
| 834 | * in kernel context, which is necessary for vfs_read and |
| 835 | * vfs_write to use our buffers in the kernel address space. |
| 836 | */ |
| 837 | queue_work(dev->wq, work); |
| 838 | /* wait for operation to complete */ |
| 839 | flush_workqueue(dev->wq); |
| 840 | fput(filp); |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 841 | |
Mike Lockwood | 491d418 | 2010-11-08 10:41:31 -0500 | [diff] [blame^] | 842 | /* read the result */ |
| 843 | smp_rmb(); |
| 844 | ret = dev->xfer_result; |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 845 | break; |
| 846 | } |
| 847 | case MTP_SET_INTERFACE_MODE: |
| 848 | if (value == MTP_INTERFACE_MODE_MTP || |
| 849 | value == MTP_INTERFACE_MODE_PTP) { |
| 850 | dev->interface_mode = value; |
| 851 | if (value == MTP_INTERFACE_MODE_PTP) { |
| 852 | dev->function.descriptors = fs_ptp_descs; |
| 853 | dev->function.hs_descriptors = hs_ptp_descs; |
| 854 | } else { |
| 855 | dev->function.descriptors = fs_mtp_descs; |
| 856 | dev->function.hs_descriptors = hs_mtp_descs; |
| 857 | } |
| 858 | ret = 0; |
| 859 | } |
| 860 | break; |
Mike Lockwood | 1de4d4d | 2010-07-06 19:27:52 -0400 | [diff] [blame] | 861 | case MTP_SEND_EVENT: |
| 862 | { |
| 863 | struct mtp_event event; |
| 864 | /* return here so we don't change dev->state below, |
| 865 | * which would interfere with bulk transfer state. |
| 866 | */ |
| 867 | if (copy_from_user(&event, (void __user *)value, sizeof(event))) |
Mike Lockwood | 491d418 | 2010-11-08 10:41:31 -0500 | [diff] [blame^] | 868 | ret = -EFAULT; |
Mike Lockwood | 1de4d4d | 2010-07-06 19:27:52 -0400 | [diff] [blame] | 869 | else |
Mike Lockwood | 491d418 | 2010-11-08 10:41:31 -0500 | [diff] [blame^] | 870 | ret = mtp_send_event(dev, &event); |
| 871 | goto out; |
Mike Lockwood | 1de4d4d | 2010-07-06 19:27:52 -0400 | [diff] [blame] | 872 | } |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | fail: |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 876 | spin_lock_irq(&dev->lock); |
| 877 | if (dev->state == STATE_CANCELED) |
| 878 | ret = -ECANCELED; |
| 879 | else if (dev->state != STATE_OFFLINE) |
| 880 | dev->state = STATE_READY; |
| 881 | spin_unlock_irq(&dev->lock); |
Mike Lockwood | 491d418 | 2010-11-08 10:41:31 -0500 | [diff] [blame^] | 882 | out: |
| 883 | _unlock(&dev->ioctl_excl); |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 884 | DBG(dev->cdev, "ioctl returning %d\n", ret); |
| 885 | return ret; |
| 886 | } |
| 887 | |
| 888 | static int mtp_open(struct inode *ip, struct file *fp) |
| 889 | { |
| 890 | printk(KERN_INFO "mtp_open\n"); |
| 891 | if (_lock(&_mtp_dev->open_excl)) |
| 892 | return -EBUSY; |
| 893 | |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 894 | /* clear any error condition */ |
| 895 | if (_mtp_dev->state != STATE_OFFLINE) |
| 896 | _mtp_dev->state = STATE_READY; |
| 897 | |
| 898 | fp->private_data = _mtp_dev; |
| 899 | return 0; |
| 900 | } |
| 901 | |
| 902 | static int mtp_release(struct inode *ip, struct file *fp) |
| 903 | { |
| 904 | printk(KERN_INFO "mtp_release\n"); |
| 905 | |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 906 | _unlock(&_mtp_dev->open_excl); |
| 907 | return 0; |
| 908 | } |
| 909 | |
| 910 | /* file operations for /dev/mtp_usb */ |
| 911 | static const struct file_operations mtp_fops = { |
| 912 | .owner = THIS_MODULE, |
| 913 | .read = mtp_read, |
| 914 | .write = mtp_write, |
| 915 | .unlocked_ioctl = mtp_ioctl, |
| 916 | .open = mtp_open, |
| 917 | .release = mtp_release, |
| 918 | }; |
| 919 | |
| 920 | static struct miscdevice mtp_device = { |
| 921 | .minor = MISC_DYNAMIC_MINOR, |
| 922 | .name = shortname, |
| 923 | .fops = &mtp_fops, |
| 924 | }; |
| 925 | |
| 926 | static int |
| 927 | mtp_function_bind(struct usb_configuration *c, struct usb_function *f) |
| 928 | { |
| 929 | struct usb_composite_dev *cdev = c->cdev; |
| 930 | struct mtp_dev *dev = func_to_dev(f); |
| 931 | int id; |
| 932 | int ret; |
| 933 | |
| 934 | dev->cdev = cdev; |
| 935 | DBG(cdev, "mtp_function_bind dev: %p\n", dev); |
| 936 | |
| 937 | /* allocate interface ID(s) */ |
| 938 | id = usb_interface_id(c, f); |
| 939 | if (id < 0) |
| 940 | return id; |
| 941 | mtp_interface_desc.bInterfaceNumber = id; |
| 942 | |
| 943 | /* allocate endpoints */ |
| 944 | ret = create_bulk_endpoints(dev, &mtp_fullspeed_in_desc, |
| 945 | &mtp_fullspeed_out_desc, &mtp_intr_desc); |
| 946 | if (ret) |
| 947 | return ret; |
| 948 | |
| 949 | /* support high speed hardware */ |
| 950 | if (gadget_is_dualspeed(c->cdev->gadget)) { |
| 951 | mtp_highspeed_in_desc.bEndpointAddress = |
| 952 | mtp_fullspeed_in_desc.bEndpointAddress; |
| 953 | mtp_highspeed_out_desc.bEndpointAddress = |
| 954 | mtp_fullspeed_out_desc.bEndpointAddress; |
| 955 | } |
| 956 | |
| 957 | DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n", |
| 958 | gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full", |
| 959 | f->name, dev->ep_in->name, dev->ep_out->name); |
| 960 | return 0; |
| 961 | } |
| 962 | |
| 963 | static void |
| 964 | mtp_function_unbind(struct usb_configuration *c, struct usb_function *f) |
| 965 | { |
| 966 | struct mtp_dev *dev = func_to_dev(f); |
| 967 | struct usb_request *req; |
| 968 | int i; |
| 969 | |
| 970 | spin_lock_irq(&dev->lock); |
| 971 | while ((req = req_get(dev, &dev->tx_idle))) |
| 972 | mtp_request_free(req, dev->ep_in); |
| 973 | for (i = 0; i < RX_REQ_MAX; i++) |
| 974 | mtp_request_free(dev->rx_req[i], dev->ep_out); |
Mike Lockwood | 1de4d4d | 2010-07-06 19:27:52 -0400 | [diff] [blame] | 975 | mtp_request_free(dev->intr_req, dev->ep_intr); |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 976 | dev->state = STATE_OFFLINE; |
| 977 | spin_unlock_irq(&dev->lock); |
Mike Lockwood | 1de4d4d | 2010-07-06 19:27:52 -0400 | [diff] [blame] | 978 | wake_up(&dev->intr_wq); |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 979 | |
| 980 | misc_deregister(&mtp_device); |
| 981 | kfree(_mtp_dev); |
| 982 | _mtp_dev = NULL; |
| 983 | } |
| 984 | |
| 985 | static int mtp_function_setup(struct usb_function *f, |
| 986 | const struct usb_ctrlrequest *ctrl) |
| 987 | { |
| 988 | struct mtp_dev *dev = func_to_dev(f); |
| 989 | struct usb_composite_dev *cdev = dev->cdev; |
| 990 | int value = -EOPNOTSUPP; |
| 991 | u16 w_index = le16_to_cpu(ctrl->wIndex); |
| 992 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 993 | u16 w_length = le16_to_cpu(ctrl->wLength); |
| 994 | unsigned long flags; |
| 995 | |
| 996 | /* do nothing if we are disabled */ |
| 997 | if (dev->function.disabled) |
| 998 | return value; |
| 999 | |
| 1000 | VDBG(cdev, "mtp_function_setup " |
| 1001 | "%02x.%02x v%04x i%04x l%u\n", |
| 1002 | ctrl->bRequestType, ctrl->bRequest, |
| 1003 | w_value, w_index, w_length); |
| 1004 | |
| 1005 | /* Handle MTP OS string */ |
| 1006 | if (dev->interface_mode == MTP_INTERFACE_MODE_MTP |
| 1007 | && ctrl->bRequestType == |
| 1008 | (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE) |
| 1009 | && ctrl->bRequest == USB_REQ_GET_DESCRIPTOR |
| 1010 | && (w_value >> 8) == USB_DT_STRING |
| 1011 | && (w_value & 0xFF) == MTP_OS_STRING_ID) { |
| 1012 | value = (w_length < sizeof(mtp_os_string) |
| 1013 | ? w_length : sizeof(mtp_os_string)); |
| 1014 | memcpy(cdev->req->buf, mtp_os_string, value); |
| 1015 | /* return here since composite.c will send for us */ |
| 1016 | return value; |
| 1017 | } |
| 1018 | if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR) { |
| 1019 | /* Handle MTP OS descriptor */ |
| 1020 | DBG(cdev, "vendor request: %d index: %d value: %d length: %d\n", |
| 1021 | ctrl->bRequest, w_index, w_value, w_length); |
| 1022 | |
| 1023 | if (dev->interface_mode == MTP_INTERFACE_MODE_MTP |
| 1024 | && ctrl->bRequest == 1 |
| 1025 | && (ctrl->bRequestType & USB_DIR_IN) |
| 1026 | && (w_index == 4 || w_index == 5)) { |
| 1027 | value = (w_length < sizeof(mtp_ext_config_desc) ? |
| 1028 | w_length : sizeof(mtp_ext_config_desc)); |
| 1029 | memcpy(cdev->req->buf, &mtp_ext_config_desc, value); |
| 1030 | } |
| 1031 | } |
| 1032 | if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS) { |
| 1033 | DBG(cdev, "class request: %d index: %d value: %d length: %d\n", |
| 1034 | ctrl->bRequest, w_index, w_value, w_length); |
| 1035 | |
| 1036 | if (ctrl->bRequest == MTP_REQ_CANCEL && w_index == 0 |
| 1037 | && w_value == 0) { |
| 1038 | DBG(cdev, "MTP_REQ_CANCEL\n"); |
| 1039 | |
| 1040 | spin_lock_irqsave(&dev->lock, flags); |
| 1041 | if (dev->state == STATE_BUSY) { |
| 1042 | dev->state = STATE_CANCELED; |
| 1043 | wake_up(&dev->read_wq); |
| 1044 | wake_up(&dev->write_wq); |
| 1045 | } |
| 1046 | spin_unlock_irqrestore(&dev->lock, flags); |
| 1047 | |
| 1048 | /* We need to queue a request to read the remaining |
| 1049 | * bytes, but we don't actually need to look at |
| 1050 | * the contents. |
| 1051 | */ |
| 1052 | value = w_length; |
| 1053 | } else if (ctrl->bRequest == MTP_REQ_GET_DEVICE_STATUS |
| 1054 | && w_index == 0 && w_value == 0) { |
| 1055 | struct mtp_device_status *status = cdev->req->buf; |
| 1056 | status->wLength = |
| 1057 | __constant_cpu_to_le16(sizeof(*status)); |
| 1058 | |
| 1059 | DBG(cdev, "MTP_REQ_GET_DEVICE_STATUS\n"); |
| 1060 | spin_lock_irqsave(&dev->lock, flags); |
| 1061 | /* device status is "busy" until we report |
| 1062 | * the cancelation to userspace |
| 1063 | */ |
| 1064 | if (dev->state == STATE_BUSY |
| 1065 | || dev->state == STATE_CANCELED) |
| 1066 | status->wCode = |
| 1067 | __cpu_to_le16(MTP_RESPONSE_DEVICE_BUSY); |
| 1068 | else |
| 1069 | status->wCode = |
| 1070 | __cpu_to_le16(MTP_RESPONSE_OK); |
| 1071 | spin_unlock_irqrestore(&dev->lock, flags); |
| 1072 | value = sizeof(*status); |
| 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | /* respond with data transfer or status phase? */ |
| 1077 | if (value >= 0) { |
| 1078 | int rc; |
| 1079 | cdev->req->zero = value < w_length; |
| 1080 | cdev->req->length = value; |
| 1081 | rc = usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC); |
| 1082 | if (rc < 0) |
| 1083 | ERROR(cdev, "%s setup response queue error\n", __func__); |
| 1084 | } |
| 1085 | |
| 1086 | if (value == -EOPNOTSUPP) |
| 1087 | VDBG(cdev, |
| 1088 | "unknown class-specific control req " |
| 1089 | "%02x.%02x v%04x i%04x l%u\n", |
| 1090 | ctrl->bRequestType, ctrl->bRequest, |
| 1091 | w_value, w_index, w_length); |
| 1092 | return value; |
| 1093 | } |
| 1094 | |
| 1095 | static int mtp_function_set_alt(struct usb_function *f, |
| 1096 | unsigned intf, unsigned alt) |
| 1097 | { |
| 1098 | struct mtp_dev *dev = func_to_dev(f); |
| 1099 | struct usb_composite_dev *cdev = f->config->cdev; |
| 1100 | int ret; |
| 1101 | |
| 1102 | DBG(cdev, "mtp_function_set_alt intf: %d alt: %d\n", intf, alt); |
| 1103 | ret = usb_ep_enable(dev->ep_in, |
| 1104 | ep_choose(cdev->gadget, |
| 1105 | &mtp_highspeed_in_desc, |
| 1106 | &mtp_fullspeed_in_desc)); |
| 1107 | if (ret) |
| 1108 | return ret; |
| 1109 | ret = usb_ep_enable(dev->ep_out, |
| 1110 | ep_choose(cdev->gadget, |
| 1111 | &mtp_highspeed_out_desc, |
| 1112 | &mtp_fullspeed_out_desc)); |
| 1113 | if (ret) { |
| 1114 | usb_ep_disable(dev->ep_in); |
| 1115 | return ret; |
| 1116 | } |
| 1117 | ret = usb_ep_enable(dev->ep_intr, &mtp_intr_desc); |
| 1118 | if (ret) { |
| 1119 | usb_ep_disable(dev->ep_out); |
| 1120 | usb_ep_disable(dev->ep_in); |
| 1121 | return ret; |
| 1122 | } |
| 1123 | dev->state = STATE_READY; |
| 1124 | |
| 1125 | /* readers may be blocked waiting for us to go online */ |
| 1126 | wake_up(&dev->read_wq); |
| 1127 | return 0; |
| 1128 | } |
| 1129 | |
| 1130 | static void mtp_function_disable(struct usb_function *f) |
| 1131 | { |
| 1132 | struct mtp_dev *dev = func_to_dev(f); |
| 1133 | struct usb_composite_dev *cdev = dev->cdev; |
| 1134 | |
| 1135 | DBG(cdev, "mtp_function_disable\n"); |
| 1136 | dev->state = STATE_OFFLINE; |
| 1137 | usb_ep_disable(dev->ep_in); |
| 1138 | usb_ep_disable(dev->ep_out); |
| 1139 | usb_ep_disable(dev->ep_intr); |
| 1140 | |
| 1141 | /* readers may be blocked waiting for us to go online */ |
| 1142 | wake_up(&dev->read_wq); |
Mike Lockwood | 1de4d4d | 2010-07-06 19:27:52 -0400 | [diff] [blame] | 1143 | wake_up(&dev->intr_wq); |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 1144 | |
| 1145 | VDBG(cdev, "%s disabled\n", dev->function.name); |
| 1146 | } |
| 1147 | |
| 1148 | static int mtp_bind_config(struct usb_configuration *c) |
| 1149 | { |
| 1150 | struct mtp_dev *dev; |
| 1151 | int ret; |
| 1152 | |
| 1153 | printk(KERN_INFO "mtp_bind_config\n"); |
| 1154 | |
| 1155 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 1156 | if (!dev) |
| 1157 | return -ENOMEM; |
| 1158 | |
| 1159 | /* allocate a string ID for our interface */ |
| 1160 | if (mtp_string_defs[INTERFACE_STRING_INDEX].id == 0) { |
| 1161 | ret = usb_string_id(c->cdev); |
| 1162 | if (ret < 0) |
| 1163 | return ret; |
| 1164 | mtp_string_defs[INTERFACE_STRING_INDEX].id = ret; |
| 1165 | mtp_interface_desc.iInterface = ret; |
| 1166 | } |
| 1167 | |
| 1168 | spin_lock_init(&dev->lock); |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 1169 | init_waitqueue_head(&dev->read_wq); |
| 1170 | init_waitqueue_head(&dev->write_wq); |
Mike Lockwood | 1de4d4d | 2010-07-06 19:27:52 -0400 | [diff] [blame] | 1171 | init_waitqueue_head(&dev->intr_wq); |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 1172 | atomic_set(&dev->open_excl, 0); |
Mike Lockwood | 491d418 | 2010-11-08 10:41:31 -0500 | [diff] [blame^] | 1173 | atomic_set(&dev->ioctl_excl, 0); |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 1174 | INIT_LIST_HEAD(&dev->tx_idle); |
Mike Lockwood | 491d418 | 2010-11-08 10:41:31 -0500 | [diff] [blame^] | 1175 | |
| 1176 | dev->wq = create_singlethread_workqueue("f_mtp"); |
| 1177 | if (!dev->wq) |
| 1178 | goto err1; |
| 1179 | INIT_WORK(&dev->send_file_work, send_file_work); |
| 1180 | INIT_WORK(&dev->receive_file_work, receive_file_work); |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 1181 | |
| 1182 | dev->cdev = c->cdev; |
| 1183 | dev->function.name = "mtp"; |
| 1184 | dev->function.strings = mtp_strings, |
| 1185 | dev->function.descriptors = fs_mtp_descs; |
| 1186 | dev->function.hs_descriptors = hs_mtp_descs; |
| 1187 | dev->function.bind = mtp_function_bind; |
| 1188 | dev->function.unbind = mtp_function_unbind; |
| 1189 | dev->function.setup = mtp_function_setup; |
| 1190 | dev->function.set_alt = mtp_function_set_alt; |
| 1191 | dev->function.disable = mtp_function_disable; |
| 1192 | |
| 1193 | /* MTP mode by default */ |
| 1194 | dev->interface_mode = MTP_INTERFACE_MODE_MTP; |
| 1195 | |
| 1196 | /* _mtp_dev must be set before calling usb_gadget_register_driver */ |
| 1197 | _mtp_dev = dev; |
| 1198 | |
| 1199 | ret = misc_register(&mtp_device); |
| 1200 | if (ret) |
| 1201 | goto err1; |
| 1202 | |
| 1203 | ret = usb_add_function(c, &dev->function); |
| 1204 | if (ret) |
| 1205 | goto err2; |
| 1206 | |
| 1207 | return 0; |
| 1208 | |
| 1209 | err2: |
| 1210 | misc_deregister(&mtp_device); |
| 1211 | err1: |
Mike Lockwood | 491d418 | 2010-11-08 10:41:31 -0500 | [diff] [blame^] | 1212 | if (dev->wq) |
| 1213 | destroy_workqueue(dev->wq); |
Mike Lockwood | ba83b01 | 2010-04-16 10:39:22 -0400 | [diff] [blame] | 1214 | kfree(dev); |
| 1215 | printk(KERN_ERR "mtp gadget driver failed to initialize\n"); |
| 1216 | return ret; |
| 1217 | } |
| 1218 | |
| 1219 | static struct android_usb_function mtp_function = { |
| 1220 | .name = "mtp", |
| 1221 | .bind_config = mtp_bind_config, |
| 1222 | }; |
| 1223 | |
| 1224 | static int __init init(void) |
| 1225 | { |
| 1226 | printk(KERN_INFO "f_mtp init\n"); |
| 1227 | android_register_function(&mtp_function); |
| 1228 | return 0; |
| 1229 | } |
| 1230 | module_init(init); |