Pavankumar Kondeti | 88683c8 | 2013-03-06 17:30:49 +0530 | [diff] [blame] | 1 | /* Copyright (c) 2011-2013, The Linux Foundation. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/errno.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/mutex.h> |
| 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/uaccess.h> |
| 21 | #include <linux/usb.h> |
| 22 | #include <linux/usb/ch9.h> |
| 23 | #include <linux/usb/cdc.h> |
| 24 | #include <linux/debugfs.h> |
| 25 | |
| 26 | #include <mach/ipc_bridge.h> |
| 27 | |
| 28 | enum ipc_bridge_rx_state { |
| 29 | RX_IDLE, /* inturb is not queued */ |
| 30 | RX_WAIT, /* inturb is queued and waiting for data */ |
| 31 | RX_BUSY, /* inturb is completed. processing RX */ |
| 32 | }; |
| 33 | |
| 34 | struct ctl_pkt { |
| 35 | u32 len; |
| 36 | void *buf; |
| 37 | struct list_head list; |
| 38 | }; |
| 39 | |
| 40 | struct ipc_bridge { |
| 41 | struct usb_device *udev; |
| 42 | struct usb_interface *intf; |
| 43 | struct urb *inturb; |
| 44 | struct urb *readurb; |
| 45 | struct urb *writeurb; |
| 46 | struct usb_ctrlrequest *in_ctlreq; |
| 47 | struct usb_ctrlrequest *out_ctlreq; |
| 48 | void *readbuf; |
| 49 | void *intbuf; |
| 50 | |
| 51 | spinlock_t lock; |
| 52 | struct list_head rx_list; |
| 53 | enum ipc_bridge_rx_state rx_state; |
| 54 | |
| 55 | struct platform_device *pdev; |
| 56 | struct mutex open_mutex; |
| 57 | struct mutex read_mutex; |
| 58 | struct mutex write_mutex; |
| 59 | bool opened; |
| 60 | struct completion write_done; |
| 61 | int write_result; |
| 62 | wait_queue_head_t read_wait_q; |
| 63 | |
| 64 | unsigned int snd_encap_cmd; |
| 65 | unsigned int get_encap_resp; |
| 66 | unsigned int susp_fail_cnt; |
| 67 | }; |
| 68 | |
| 69 | #define IPC_BRIDGE_MAX_READ_SZ (8 * 1024) |
| 70 | #define IPC_BRIDGE_MAX_WRITE_SZ (8 * 1024) |
| 71 | |
| 72 | static struct ipc_bridge *__ipc_bridge_dev; |
| 73 | |
| 74 | static int ipc_bridge_submit_inturb(struct ipc_bridge *dev, gfp_t gfp_flags) |
| 75 | { |
| 76 | int ret; |
| 77 | unsigned long flags; |
| 78 | |
| 79 | ret = usb_submit_urb(dev->inturb, gfp_flags); |
| 80 | if (ret < 0 && ret != -EPERM) |
| 81 | dev_err(&dev->intf->dev, "int urb submit err %d\n", ret); |
| 82 | |
| 83 | spin_lock_irqsave(&dev->lock, flags); |
| 84 | if (ret) |
| 85 | dev->rx_state = RX_IDLE; |
| 86 | else |
| 87 | dev->rx_state = RX_WAIT; |
| 88 | spin_unlock_irqrestore(&dev->lock, flags); |
| 89 | |
| 90 | return ret; |
| 91 | } |
| 92 | |
| 93 | static void ipc_bridge_write_cb(struct urb *urb) |
| 94 | { |
| 95 | struct ipc_bridge *dev = urb->context; |
| 96 | |
| 97 | usb_autopm_put_interface_async(dev->intf); |
| 98 | |
| 99 | if (urb->dev->state == USB_STATE_NOTATTACHED) |
| 100 | dev->write_result = -ENODEV; |
| 101 | else if (urb->status < 0) |
| 102 | dev->write_result = urb->status; |
| 103 | else |
| 104 | dev->write_result = urb->actual_length; |
| 105 | |
| 106 | complete(&dev->write_done); |
| 107 | } |
| 108 | |
| 109 | static void ipc_bridge_read_cb(struct urb *urb) |
| 110 | { |
| 111 | struct ipc_bridge *dev = urb->context; |
| 112 | bool resubmit = true; |
| 113 | struct ctl_pkt *pkt; |
| 114 | unsigned long flags; |
| 115 | |
| 116 | usb_autopm_put_interface_async(dev->intf); |
| 117 | if (urb->dev->state == USB_STATE_NOTATTACHED) { |
| 118 | wake_up(&dev->read_wait_q); |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | switch (urb->status) { |
| 123 | case 0: |
| 124 | break; |
| 125 | |
| 126 | case -ENOENT: |
| 127 | case -ESHUTDOWN: |
| 128 | case -ECONNRESET: |
| 129 | case -EPROTO: |
| 130 | case -EPIPE: |
| 131 | resubmit = false; |
| 132 | goto done; |
| 133 | |
| 134 | case -EOVERFLOW: |
| 135 | default: |
| 136 | goto done; |
| 137 | } |
| 138 | |
| 139 | if (!urb->actual_length) |
| 140 | goto done; |
| 141 | |
| 142 | pkt = kmalloc(sizeof(*pkt), GFP_ATOMIC); |
| 143 | if (!pkt) { |
| 144 | dev_err(&dev->intf->dev, "fail to allocate pkt\n"); |
| 145 | resubmit = false; |
| 146 | goto done; |
| 147 | } |
| 148 | pkt->len = urb->actual_length; |
| 149 | pkt->buf = kmalloc(pkt->len, GFP_ATOMIC); |
| 150 | if (!pkt->buf) { |
| 151 | kfree(pkt); |
| 152 | dev_err(&dev->intf->dev, "fail to allocate pkt buffer\n"); |
| 153 | resubmit = false; |
| 154 | goto done; |
| 155 | } |
| 156 | |
| 157 | memcpy(pkt->buf, urb->transfer_buffer, pkt->len); |
| 158 | |
| 159 | spin_lock_irqsave(&dev->lock, flags); |
| 160 | list_add_tail(&pkt->list, &dev->rx_list); |
| 161 | spin_unlock_irqrestore(&dev->lock, flags); |
| 162 | wake_up(&dev->read_wait_q); |
| 163 | |
| 164 | done: |
| 165 | if (resubmit) { |
| 166 | ipc_bridge_submit_inturb(dev, GFP_ATOMIC); |
| 167 | } else { |
| 168 | spin_lock_irqsave(&dev->lock, flags); |
| 169 | dev->rx_state = RX_IDLE; |
| 170 | spin_unlock_irqrestore(&dev->lock, flags); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | static void ipc_bridge_int_cb(struct urb *urb) |
| 175 | { |
| 176 | struct ipc_bridge *dev = urb->context; |
| 177 | struct usb_cdc_notification *ctl; |
| 178 | int status; |
| 179 | unsigned long flags; |
| 180 | |
| 181 | if (urb->dev->state == USB_STATE_NOTATTACHED) |
| 182 | return; |
| 183 | |
| 184 | spin_lock_irqsave(&dev->lock, flags); |
| 185 | dev->rx_state = RX_IDLE; |
| 186 | spin_unlock_irqrestore(&dev->lock, flags); |
| 187 | |
| 188 | switch (urb->status) { |
| 189 | case 0: |
| 190 | case -ENOENT: |
| 191 | break; |
| 192 | |
| 193 | case -ESHUTDOWN: |
| 194 | case -ECONNRESET: |
| 195 | case -EPROTO: |
| 196 | case -EPIPE: |
| 197 | return; |
| 198 | |
| 199 | case -EOVERFLOW: |
| 200 | default: |
| 201 | ipc_bridge_submit_inturb(dev, GFP_ATOMIC); |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | if (!urb->actual_length) |
| 206 | return; |
| 207 | |
| 208 | ctl = urb->transfer_buffer; |
| 209 | |
| 210 | switch (ctl->bNotificationType) { |
| 211 | case USB_CDC_NOTIFY_RESPONSE_AVAILABLE: |
| 212 | |
| 213 | spin_lock_irqsave(&dev->lock, flags); |
| 214 | dev->rx_state = RX_BUSY; |
| 215 | spin_unlock_irqrestore(&dev->lock, flags); |
| 216 | |
| 217 | usb_fill_control_urb(dev->readurb, dev->udev, |
| 218 | usb_rcvctrlpipe(dev->udev, 0), |
| 219 | (unsigned char *)dev->in_ctlreq, |
| 220 | dev->readbuf, IPC_BRIDGE_MAX_READ_SZ, |
| 221 | ipc_bridge_read_cb, dev); |
| 222 | |
| 223 | status = usb_submit_urb(dev->readurb, GFP_ATOMIC); |
| 224 | if (status) { |
| 225 | dev_err(&dev->intf->dev, "read urb submit err %d\n", |
| 226 | status); |
| 227 | goto resubmit_int_urb; |
| 228 | } |
| 229 | dev->get_encap_resp++; |
| 230 | /* Tell runtime pm core that we are busy */ |
| 231 | usb_autopm_get_interface_no_resume(dev->intf); |
| 232 | return; |
| 233 | default: |
| 234 | dev_err(&dev->intf->dev, "unknown data on int ep\n"); |
| 235 | } |
| 236 | |
| 237 | resubmit_int_urb: |
| 238 | ipc_bridge_submit_inturb(dev, GFP_ATOMIC); |
| 239 | } |
| 240 | |
| 241 | static int ipc_bridge_open(struct platform_device *pdev) |
| 242 | { |
| 243 | struct ipc_bridge *dev = __ipc_bridge_dev; |
| 244 | |
| 245 | if (dev->pdev != pdev) |
| 246 | return -EINVAL; |
| 247 | |
| 248 | mutex_lock(&dev->open_mutex); |
| 249 | if (dev->opened) { |
| 250 | mutex_unlock(&dev->open_mutex); |
| 251 | dev_dbg(&dev->intf->dev, "bridge already opened\n"); |
| 252 | return -EBUSY; |
| 253 | } |
| 254 | dev->opened = true; |
| 255 | mutex_unlock(&dev->open_mutex); |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | static int ipc_bridge_rx_list_empty(struct ipc_bridge *dev) |
| 260 | { |
| 261 | int ret; |
| 262 | unsigned long flags; |
| 263 | |
| 264 | spin_lock_irqsave(&dev->lock, flags); |
| 265 | ret = list_empty(&dev->rx_list); |
| 266 | spin_unlock_irqrestore(&dev->lock, flags); |
| 267 | |
| 268 | return ret; |
| 269 | } |
| 270 | |
| 271 | static int |
| 272 | ipc_bridge_read(struct platform_device *pdev, char *buf, unsigned int count) |
| 273 | { |
| 274 | struct ipc_bridge *dev = __ipc_bridge_dev; |
| 275 | struct ctl_pkt *pkt; |
| 276 | int ret; |
| 277 | unsigned long flags; |
| 278 | |
| 279 | if (dev->pdev != pdev) |
| 280 | return -EINVAL; |
| 281 | if (!dev->opened) |
| 282 | return -EPERM; |
| 283 | if (count > IPC_BRIDGE_MAX_READ_SZ) |
| 284 | return -ENOSPC; |
| 285 | |
| 286 | mutex_lock(&dev->read_mutex); |
| 287 | |
| 288 | wait_event(dev->read_wait_q, (!ipc_bridge_rx_list_empty(dev) || |
| 289 | (dev->udev->state == USB_STATE_NOTATTACHED))); |
| 290 | |
| 291 | if (dev->udev->state == USB_STATE_NOTATTACHED) { |
| 292 | ret = -ENODEV; |
| 293 | goto done; |
| 294 | } |
| 295 | |
| 296 | spin_lock_irqsave(&dev->lock, flags); |
| 297 | pkt = list_first_entry(&dev->rx_list, struct ctl_pkt, list); |
| 298 | if (pkt->len > count) { |
| 299 | spin_unlock_irqrestore(&dev->lock, flags); |
| 300 | dev_err(&dev->intf->dev, "large RX packet\n"); |
| 301 | ret = -ENOSPC; |
| 302 | goto done; |
| 303 | } |
| 304 | list_del(&pkt->list); |
| 305 | spin_unlock_irqrestore(&dev->lock, flags); |
| 306 | |
| 307 | memcpy(buf, pkt->buf, pkt->len); |
| 308 | ret = pkt->len; |
| 309 | kfree(pkt->buf); |
| 310 | kfree(pkt); |
| 311 | done: |
| 312 | mutex_unlock(&dev->read_mutex); |
| 313 | return ret; |
| 314 | } |
| 315 | |
| 316 | static int |
| 317 | ipc_bridge_write(struct platform_device *pdev, char *buf, unsigned int count) |
| 318 | { |
| 319 | struct ipc_bridge *dev = __ipc_bridge_dev; |
| 320 | int ret; |
| 321 | |
| 322 | if (dev->pdev != pdev) |
| 323 | return -EINVAL; |
| 324 | if (!dev->opened) |
| 325 | return -EPERM; |
| 326 | if (count > IPC_BRIDGE_MAX_WRITE_SZ) |
| 327 | return -EINVAL; |
| 328 | |
| 329 | mutex_lock(&dev->write_mutex); |
| 330 | |
| 331 | dev->out_ctlreq->wLength = cpu_to_le16(count); |
| 332 | usb_fill_control_urb(dev->writeurb, dev->udev, |
| 333 | usb_sndctrlpipe(dev->udev, 0), |
| 334 | (unsigned char *)dev->out_ctlreq, |
| 335 | (void *)buf, count, |
| 336 | ipc_bridge_write_cb, dev); |
| 337 | |
| 338 | ret = usb_autopm_get_interface(dev->intf); |
| 339 | if (ret < 0) { |
| 340 | dev_err(&dev->intf->dev, "write auto pm fail %d\n", ret); |
| 341 | goto done; |
| 342 | } |
| 343 | ret = usb_submit_urb(dev->writeurb, GFP_KERNEL); |
| 344 | if (ret < 0) { |
| 345 | dev_err(&dev->intf->dev, "write urb submit err %d\n", ret); |
| 346 | usb_autopm_put_interface_async(dev->intf); |
| 347 | goto done; |
| 348 | } |
| 349 | dev->snd_encap_cmd++; |
| 350 | wait_for_completion(&dev->write_done); |
| 351 | ret = dev->write_result; |
| 352 | done: |
| 353 | mutex_unlock(&dev->write_mutex); |
| 354 | return ret; |
| 355 | } |
| 356 | |
| 357 | static void ipc_bridge_close(struct platform_device *pdev) |
| 358 | { |
| 359 | struct ipc_bridge *dev = __ipc_bridge_dev; |
| 360 | |
| 361 | WARN_ON(dev->pdev != pdev); |
| 362 | WARN_ON(dev->udev->state != USB_STATE_NOTATTACHED); |
| 363 | |
| 364 | mutex_lock(&dev->open_mutex); |
| 365 | if (!dev->opened) { |
| 366 | mutex_unlock(&dev->open_mutex); |
| 367 | dev_dbg(&dev->intf->dev, "bridge not opened\n"); |
| 368 | return; |
| 369 | } |
| 370 | dev->opened = false; |
| 371 | mutex_unlock(&dev->open_mutex); |
| 372 | } |
| 373 | |
| 374 | static const struct ipc_bridge_platform_data ipc_bridge_pdata = { |
| 375 | .max_read_size = IPC_BRIDGE_MAX_READ_SZ, |
| 376 | .max_write_size = IPC_BRIDGE_MAX_WRITE_SZ, |
| 377 | .open = ipc_bridge_open, |
| 378 | .read = ipc_bridge_read, |
| 379 | .write = ipc_bridge_write, |
| 380 | .close = ipc_bridge_close, |
| 381 | }; |
| 382 | |
| 383 | static int ipc_bridge_suspend(struct usb_interface *intf, pm_message_t message) |
| 384 | { |
| 385 | struct ipc_bridge *dev = usb_get_intfdata(intf); |
| 386 | unsigned long flags; |
| 387 | int ret = 0; |
| 388 | |
| 389 | spin_lock_irqsave(&dev->lock, flags); |
| 390 | if (dev->rx_state == RX_BUSY) { |
| 391 | dev->susp_fail_cnt++; |
| 392 | ret = -EBUSY; |
| 393 | goto done; |
| 394 | } |
| 395 | spin_unlock_irqrestore(&dev->lock, flags); |
| 396 | |
| 397 | usb_kill_urb(dev->inturb); |
| 398 | |
| 399 | spin_lock_irqsave(&dev->lock, flags); |
| 400 | if (dev->rx_state != RX_IDLE) { |
| 401 | dev->susp_fail_cnt++; |
| 402 | ret = -EBUSY; |
| 403 | goto done; |
| 404 | } |
| 405 | done: |
| 406 | spin_unlock_irqrestore(&dev->lock, flags); |
| 407 | return ret; |
| 408 | } |
| 409 | |
| 410 | static int ipc_bridge_resume(struct usb_interface *intf) |
| 411 | { |
| 412 | struct ipc_bridge *dev = usb_get_intfdata(intf); |
| 413 | |
| 414 | return ipc_bridge_submit_inturb(dev, GFP_KERNEL); |
| 415 | } |
| 416 | |
| 417 | #define DEBUG_BUF_SIZE 512 |
| 418 | static ssize_t ipc_bridge_read_stats(struct file *file, char __user *ubuf, |
| 419 | size_t count, loff_t *ppos) |
| 420 | { |
| 421 | struct ipc_bridge *dev = __ipc_bridge_dev; |
| 422 | char *buf; |
| 423 | int ret; |
| 424 | |
| 425 | buf = kzalloc(DEBUG_BUF_SIZE, GFP_KERNEL); |
| 426 | if (!buf) |
| 427 | return -ENOMEM; |
| 428 | |
| 429 | ret = scnprintf(buf, DEBUG_BUF_SIZE, |
| 430 | "ch opened: %d\n" |
| 431 | "encap cmd sent: %u\n" |
| 432 | "encap resp recvd: %u\n" |
| 433 | "suspend fail cnt: %u\n", |
| 434 | dev->opened, |
| 435 | dev->snd_encap_cmd, |
| 436 | dev->get_encap_resp, |
| 437 | dev->susp_fail_cnt |
| 438 | ); |
| 439 | |
| 440 | ret = simple_read_from_buffer(ubuf, count, ppos, buf, ret); |
| 441 | kfree(buf); |
| 442 | return ret; |
| 443 | } |
| 444 | |
| 445 | const struct file_operations ipc_stats_ops = { |
| 446 | .read = ipc_bridge_read_stats, |
| 447 | }; |
| 448 | |
| 449 | static struct dentry *dir; |
| 450 | |
| 451 | static void ipc_bridge_debugfs_init(void) |
| 452 | { |
| 453 | struct dentry *dfile; |
| 454 | |
| 455 | dir = debugfs_create_dir("ipc_bridge", 0); |
| 456 | if (IS_ERR_OR_NULL(dir)) |
| 457 | return; |
| 458 | |
| 459 | dfile = debugfs_create_file("status", 0444, dir, 0, &ipc_stats_ops); |
| 460 | if (IS_ERR_OR_NULL(dfile)) |
| 461 | debugfs_remove(dir); |
| 462 | } |
| 463 | |
| 464 | static void ipc_bridge_debugfs_cleanup(void) |
| 465 | { |
| 466 | debugfs_remove_recursive(dir); |
| 467 | } |
| 468 | |
| 469 | static int |
| 470 | ipc_bridge_probe(struct usb_interface *intf, const struct usb_device_id *id) |
| 471 | { |
| 472 | struct ipc_bridge *dev; |
| 473 | struct usb_device *udev = interface_to_usbdev(intf); |
| 474 | struct usb_host_interface *intf_desc; |
| 475 | struct usb_endpoint_descriptor *ep; |
| 476 | u16 wMaxPacketSize; |
| 477 | int ret; |
| 478 | |
| 479 | intf_desc = intf->cur_altsetting; |
| 480 | if (intf_desc->desc.bNumEndpoints != 1 || !usb_endpoint_is_int_in( |
| 481 | &intf_desc->endpoint[0].desc)) { |
| 482 | dev_err(&intf->dev, "driver expects only 1 int ep\n"); |
| 483 | return -ENODEV; |
| 484 | } |
| 485 | |
| 486 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 487 | if (!dev) { |
| 488 | dev_err(&intf->dev, "fail to allocate dev\n"); |
| 489 | return -ENOMEM; |
| 490 | } |
| 491 | __ipc_bridge_dev = dev; |
| 492 | |
| 493 | dev->inturb = usb_alloc_urb(0, GFP_KERNEL); |
| 494 | if (!dev->inturb) { |
| 495 | dev_err(&intf->dev, "fail to allocate int urb\n"); |
| 496 | ret = -ENOMEM; |
| 497 | goto free_dev; |
| 498 | } |
| 499 | |
| 500 | ep = &intf->cur_altsetting->endpoint[0].desc; |
| 501 | wMaxPacketSize = le16_to_cpu(ep->wMaxPacketSize); |
| 502 | |
| 503 | dev->intbuf = kmalloc(wMaxPacketSize, GFP_KERNEL); |
| 504 | if (!dev->intbuf) { |
| 505 | dev_err(&intf->dev, "%s: error allocating int buffer\n", |
| 506 | __func__); |
| 507 | ret = -ENOMEM; |
| 508 | goto free_inturb; |
| 509 | } |
| 510 | |
| 511 | usb_fill_int_urb(dev->inturb, udev, |
| 512 | usb_rcvintpipe(udev, ep->bEndpointAddress), |
| 513 | dev->intbuf, wMaxPacketSize, |
| 514 | ipc_bridge_int_cb, dev, ep->bInterval); |
| 515 | |
| 516 | dev->in_ctlreq = kmalloc(sizeof(*dev->in_ctlreq), GFP_KERNEL); |
| 517 | if (!dev->in_ctlreq) { |
| 518 | dev_err(&intf->dev, "error allocating IN control req\n"); |
| 519 | ret = -ENOMEM; |
| 520 | goto free_intbuf; |
| 521 | } |
| 522 | |
| 523 | dev->in_ctlreq->bRequestType = |
| 524 | (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE); |
| 525 | dev->in_ctlreq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE; |
| 526 | dev->in_ctlreq->wValue = 0; |
| 527 | dev->in_ctlreq->wIndex = intf->cur_altsetting->desc.bInterfaceNumber; |
| 528 | dev->in_ctlreq->wLength = cpu_to_le16(IPC_BRIDGE_MAX_READ_SZ); |
| 529 | |
| 530 | dev->readurb = usb_alloc_urb(0, GFP_KERNEL); |
| 531 | if (!dev->readurb) { |
| 532 | dev_err(&intf->dev, "fail to allocate read urb\n"); |
| 533 | ret = -ENOMEM; |
| 534 | goto free_in_ctlreq; |
| 535 | } |
| 536 | |
| 537 | dev->readbuf = kmalloc(IPC_BRIDGE_MAX_READ_SZ, GFP_KERNEL); |
| 538 | if (!dev->readbuf) { |
| 539 | dev_err(&intf->dev, "fail to allocate read buffer\n"); |
| 540 | ret = -ENOMEM; |
| 541 | goto free_readurb; |
| 542 | } |
| 543 | |
| 544 | dev->out_ctlreq = kmalloc(sizeof(*dev->out_ctlreq), GFP_KERNEL); |
| 545 | if (!dev->out_ctlreq) { |
| 546 | dev_err(&intf->dev, "error allocating OUT control req\n"); |
| 547 | ret = -ENOMEM; |
| 548 | goto free_readbuf; |
| 549 | } |
| 550 | |
| 551 | dev->out_ctlreq->bRequestType = |
| 552 | (USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE); |
| 553 | dev->out_ctlreq->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND; |
| 554 | dev->out_ctlreq->wValue = 0; |
| 555 | dev->out_ctlreq->wIndex = intf->cur_altsetting->desc.bInterfaceNumber; |
| 556 | |
| 557 | dev->writeurb = usb_alloc_urb(0, GFP_KERNEL); |
| 558 | if (!dev->writeurb) { |
| 559 | dev_err(&intf->dev, "fail to allocate write urb\n"); |
| 560 | ret = -ENOMEM; |
| 561 | goto free_out_ctlreq; |
| 562 | } |
| 563 | |
| 564 | dev->udev = usb_get_dev(interface_to_usbdev(intf)); |
| 565 | dev->intf = intf; |
| 566 | spin_lock_init(&dev->lock); |
| 567 | init_completion(&dev->write_done); |
| 568 | init_waitqueue_head(&dev->read_wait_q); |
| 569 | INIT_LIST_HEAD(&dev->rx_list); |
| 570 | mutex_init(&dev->open_mutex); |
| 571 | mutex_init(&dev->read_mutex); |
| 572 | mutex_init(&dev->write_mutex); |
| 573 | usb_set_intfdata(intf, dev); |
| 574 | usb_enable_autosuspend(udev); |
| 575 | |
| 576 | dev->pdev = platform_device_alloc("ipc_bridge", -1); |
| 577 | if (!dev->pdev) { |
| 578 | dev_err(&intf->dev, "fail to allocate pdev\n"); |
| 579 | ret = -ENOMEM; |
| 580 | goto destroy_mutex; |
| 581 | } |
| 582 | |
| 583 | ret = platform_device_add_data(dev->pdev, &ipc_bridge_pdata, |
| 584 | sizeof(struct ipc_bridge_platform_data)); |
| 585 | if (ret) { |
| 586 | dev_err(&intf->dev, "fail to add pdata\n"); |
| 587 | goto put_pdev; |
| 588 | } |
| 589 | |
| 590 | ret = platform_device_add(dev->pdev); |
| 591 | if (ret) { |
| 592 | dev_err(&intf->dev, "fail to add pdev\n"); |
| 593 | goto put_pdev; |
| 594 | } |
| 595 | |
| 596 | ret = ipc_bridge_submit_inturb(dev, GFP_KERNEL); |
| 597 | if (ret) { |
| 598 | dev_err(&intf->dev, "fail to start reading\n"); |
| 599 | goto del_pdev; |
| 600 | } |
| 601 | |
| 602 | ipc_bridge_debugfs_init(); |
| 603 | return 0; |
| 604 | |
| 605 | del_pdev: |
| 606 | platform_device_del(dev->pdev); |
| 607 | put_pdev: |
| 608 | platform_device_put(dev->pdev); |
| 609 | destroy_mutex: |
| 610 | usb_disable_autosuspend(udev); |
| 611 | mutex_destroy(&dev->write_mutex); |
| 612 | mutex_destroy(&dev->read_mutex); |
| 613 | mutex_destroy(&dev->open_mutex); |
| 614 | usb_put_dev(dev->udev); |
| 615 | usb_free_urb(dev->writeurb); |
| 616 | free_out_ctlreq: |
| 617 | kfree(dev->out_ctlreq); |
| 618 | free_readbuf: |
| 619 | kfree(dev->readbuf); |
| 620 | free_readurb: |
| 621 | usb_free_urb(dev->readurb); |
| 622 | free_in_ctlreq: |
| 623 | kfree(dev->in_ctlreq); |
| 624 | free_intbuf: |
| 625 | kfree(dev->intbuf); |
| 626 | free_inturb: |
| 627 | usb_free_urb(dev->inturb); |
| 628 | free_dev: |
| 629 | kfree(dev); |
| 630 | __ipc_bridge_dev = NULL; |
| 631 | |
| 632 | return ret; |
| 633 | } |
| 634 | |
| 635 | static void ipc_bridge_disconnect(struct usb_interface *intf) |
| 636 | { |
| 637 | struct ipc_bridge *dev = usb_get_intfdata(intf); |
| 638 | struct ctl_pkt *pkt; |
| 639 | unsigned long flags; |
| 640 | |
| 641 | ipc_bridge_debugfs_cleanup(); |
| 642 | |
| 643 | usb_kill_urb(dev->writeurb); |
| 644 | usb_kill_urb(dev->inturb); |
| 645 | usb_kill_urb(dev->readurb); |
| 646 | |
| 647 | /* |
| 648 | * The readurb may not be active at the time of |
| 649 | * unlink. Wake up the reader explicitly before |
| 650 | * unregistering the platform device. |
| 651 | */ |
| 652 | wake_up(&dev->read_wait_q); |
| 653 | platform_device_unregister(dev->pdev); |
| 654 | WARN_ON(dev->opened); |
| 655 | |
| 656 | spin_lock_irqsave(&dev->lock, flags); |
| 657 | while (!list_empty(&dev->rx_list)) { |
| 658 | pkt = list_first_entry(&dev->rx_list, struct ctl_pkt, list); |
| 659 | list_del(&pkt->list); |
| 660 | kfree(pkt->buf); |
| 661 | kfree(pkt); |
| 662 | } |
| 663 | spin_unlock_irqrestore(&dev->lock, flags); |
| 664 | |
| 665 | mutex_destroy(&dev->open_mutex); |
| 666 | mutex_destroy(&dev->read_mutex); |
| 667 | mutex_destroy(&dev->write_mutex); |
| 668 | usb_free_urb(dev->writeurb); |
| 669 | kfree(dev->out_ctlreq); |
| 670 | usb_free_urb(dev->readurb); |
| 671 | kfree(dev->in_ctlreq); |
| 672 | kfree(dev->intbuf); |
| 673 | usb_free_urb(dev->inturb); |
| 674 | usb_put_dev(dev->udev); |
| 675 | kfree(dev); |
| 676 | __ipc_bridge_dev = NULL; |
| 677 | } |
| 678 | |
| 679 | static const struct usb_device_id ipc_bridge_ids[] = { |
| 680 | { USB_DEVICE_INTERFACE_NUMBER(0x5c6, 0x908A, 7) }, |
| 681 | |
| 682 | {} /* terminating entry */ |
| 683 | }; |
| 684 | MODULE_DEVICE_TABLE(usb, ipc_bridge_ids); |
| 685 | |
| 686 | static struct usb_driver ipc_bridge_driver = { |
| 687 | .name = "ipc_bridge", |
| 688 | .probe = ipc_bridge_probe, |
| 689 | .disconnect = ipc_bridge_disconnect, |
| 690 | .suspend = ipc_bridge_suspend, |
| 691 | .resume = ipc_bridge_resume, |
| 692 | .id_table = ipc_bridge_ids, |
| 693 | .supports_autosuspend = 1, |
| 694 | }; |
| 695 | |
| 696 | module_usb_driver(ipc_bridge_driver); |
| 697 | |
| 698 | MODULE_DESCRIPTION("USB IPC bridge driver"); |
| 699 | MODULE_LICENSE("GPL v2"); |