Vamsi Krishna | 5b94471 | 2012-06-29 18:36:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012, Code Aurora Forum. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 and |
| 6 | * only version 2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | */ |
| 13 | |
| 14 | /* add additional information to our printk's */ |
| 15 | #define pr_fmt(fmt) "%s: " fmt "\n", __func__ |
| 16 | |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/errno.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/kref.h> |
| 23 | #include <linux/platform_device.h> |
| 24 | #include <linux/ratelimit.h> |
| 25 | #include <linux/uaccess.h> |
| 26 | #include <linux/usb.h> |
| 27 | #include <linux/debugfs.h> |
| 28 | #include <linux/seq_file.h> |
| 29 | #include <linux/miscdevice.h> |
| 30 | #include <linux/list.h> |
| 31 | #include <linux/wait.h> |
| 32 | |
| 33 | #define DRIVER_DESC "USB host ks bridge driver" |
| 34 | #define DRIVER_VERSION "1.0" |
| 35 | |
| 36 | struct data_pkt { |
| 37 | int n_read; |
| 38 | char *buf; |
| 39 | size_t len; |
| 40 | struct list_head list; |
| 41 | void *ctxt; |
| 42 | }; |
| 43 | |
| 44 | #define FILE_OPENED BIT(0) |
| 45 | #define USB_DEV_CONNECTED BIT(1) |
| 46 | #define NO_RX_REQS 10 |
| 47 | #define NO_BRIDGE_INSTANCES 2 |
| 48 | #define BOOT_BRIDGE_INDEX 0 |
| 49 | #define EFS_BRIDGE_INDEX 1 |
| 50 | #define MAX_DATA_PKT_SIZE 16384 |
| 51 | |
| 52 | struct ks_bridge { |
| 53 | char *name; |
| 54 | spinlock_t lock; |
| 55 | struct workqueue_struct *wq; |
| 56 | struct work_struct to_mdm_work; |
| 57 | struct work_struct start_rx_work; |
| 58 | struct list_head to_mdm_list; |
| 59 | struct list_head to_ks_list; |
| 60 | wait_queue_head_t ks_wait_q; |
Hemant Kumar | da86bd2 | 2012-08-24 19:42:31 -0700 | [diff] [blame] | 61 | struct miscdevice *fs_dev; |
Vamsi Krishna | 5b94471 | 2012-06-29 18:36:23 -0700 | [diff] [blame] | 62 | |
| 63 | /* usb specific */ |
| 64 | struct usb_device *udev; |
| 65 | struct usb_interface *ifc; |
| 66 | __u8 in_epAddr; |
| 67 | __u8 out_epAddr; |
| 68 | unsigned int in_pipe; |
| 69 | unsigned int out_pipe; |
| 70 | struct usb_anchor submitted; |
| 71 | |
| 72 | unsigned long flags; |
| 73 | unsigned int alloced_read_pkts; |
| 74 | |
| 75 | #define DBG_MSG_LEN 40 |
| 76 | #define DBG_MAX_MSG 500 |
| 77 | unsigned int dbg_idx; |
| 78 | rwlock_t dbg_lock; |
| 79 | char (dbgbuf[DBG_MAX_MSG])[DBG_MSG_LEN]; /* buffer */ |
| 80 | }; |
| 81 | struct ks_bridge *__ksb[NO_BRIDGE_INSTANCES]; |
| 82 | |
| 83 | /* by default debugging is enabled */ |
| 84 | static unsigned int enable_dbg = 1; |
| 85 | module_param(enable_dbg, uint, S_IRUGO | S_IWUSR); |
| 86 | |
| 87 | static void |
| 88 | dbg_log_event(struct ks_bridge *ksb, char *event, int d1, int d2) |
| 89 | { |
| 90 | unsigned long flags; |
| 91 | unsigned long long t; |
| 92 | unsigned long nanosec; |
| 93 | |
| 94 | if (!enable_dbg) |
| 95 | return; |
| 96 | |
| 97 | write_lock_irqsave(&ksb->dbg_lock, flags); |
| 98 | t = cpu_clock(smp_processor_id()); |
| 99 | nanosec = do_div(t, 1000000000)/1000; |
| 100 | scnprintf(ksb->dbgbuf[ksb->dbg_idx], DBG_MSG_LEN, "%5lu.%06lu:%s:%x:%x", |
| 101 | (unsigned long)t, nanosec, event, d1, d2); |
| 102 | |
| 103 | ksb->dbg_idx++; |
| 104 | ksb->dbg_idx = ksb->dbg_idx % DBG_MAX_MSG; |
| 105 | write_unlock_irqrestore(&ksb->dbg_lock, flags); |
| 106 | } |
| 107 | |
| 108 | static |
| 109 | struct data_pkt *ksb_alloc_data_pkt(size_t count, gfp_t flags, void *ctxt) |
| 110 | { |
| 111 | struct data_pkt *pkt; |
| 112 | |
| 113 | pkt = kzalloc(sizeof(struct data_pkt), flags); |
| 114 | if (!pkt) { |
| 115 | pr_err("failed to allocate data packet\n"); |
| 116 | return ERR_PTR(-ENOMEM); |
| 117 | } |
| 118 | |
| 119 | pkt->buf = kmalloc(count, flags); |
| 120 | if (!pkt->buf) { |
| 121 | pr_err("failed to allocate data buffer\n"); |
| 122 | kfree(pkt); |
| 123 | return ERR_PTR(-ENOMEM); |
| 124 | } |
| 125 | |
| 126 | pkt->len = count; |
| 127 | INIT_LIST_HEAD(&pkt->list); |
| 128 | pkt->ctxt = ctxt; |
| 129 | |
| 130 | return pkt; |
| 131 | } |
| 132 | |
| 133 | static void ksb_free_data_pkt(struct data_pkt *pkt) |
| 134 | { |
| 135 | kfree(pkt->buf); |
| 136 | kfree(pkt); |
| 137 | } |
| 138 | |
| 139 | |
| 140 | static ssize_t ksb_fs_read(struct file *fp, char __user *buf, |
| 141 | size_t count, loff_t *pos) |
| 142 | { |
| 143 | int ret; |
| 144 | unsigned long flags; |
| 145 | struct ks_bridge *ksb = fp->private_data; |
| 146 | struct data_pkt *pkt; |
| 147 | size_t space, copied; |
| 148 | |
| 149 | read_start: |
| 150 | if (!test_bit(USB_DEV_CONNECTED, &ksb->flags)) |
| 151 | return -ENODEV; |
| 152 | |
| 153 | spin_lock_irqsave(&ksb->lock, flags); |
| 154 | if (list_empty(&ksb->to_ks_list)) { |
| 155 | spin_unlock_irqrestore(&ksb->lock, flags); |
| 156 | ret = wait_event_interruptible(ksb->ks_wait_q, |
| 157 | !list_empty(&ksb->to_ks_list) || |
| 158 | !test_bit(USB_DEV_CONNECTED, &ksb->flags)); |
| 159 | if (ret < 0) |
| 160 | return ret; |
| 161 | |
| 162 | goto read_start; |
| 163 | } |
| 164 | |
| 165 | space = count; |
| 166 | copied = 0; |
| 167 | while (!list_empty(&ksb->to_ks_list) && space) { |
| 168 | size_t len; |
| 169 | |
| 170 | pkt = list_first_entry(&ksb->to_ks_list, struct data_pkt, list); |
| 171 | len = min_t(size_t, space, pkt->len); |
| 172 | pkt->n_read += len; |
| 173 | spin_unlock_irqrestore(&ksb->lock, flags); |
| 174 | |
| 175 | ret = copy_to_user(buf + copied, pkt->buf, len); |
| 176 | if (ret) { |
| 177 | pr_err("copy_to_user failed err:%d\n", ret); |
| 178 | ksb_free_data_pkt(pkt); |
| 179 | ksb->alloced_read_pkts--; |
| 180 | return ret; |
| 181 | } |
| 182 | |
| 183 | space -= len; |
| 184 | copied += len; |
| 185 | |
| 186 | spin_lock_irqsave(&ksb->lock, flags); |
| 187 | if (pkt->n_read == pkt->len) { |
| 188 | list_del_init(&pkt->list); |
| 189 | ksb_free_data_pkt(pkt); |
| 190 | ksb->alloced_read_pkts--; |
| 191 | } |
| 192 | } |
| 193 | spin_unlock_irqrestore(&ksb->lock, flags); |
| 194 | |
| 195 | dbg_log_event(ksb, "KS_READ", copied, 0); |
| 196 | |
| 197 | pr_debug("count:%d space:%d copied:%d", count, space, copied); |
| 198 | |
| 199 | return copied; |
| 200 | } |
| 201 | |
| 202 | static void ksb_tx_cb(struct urb *urb) |
| 203 | { |
| 204 | struct data_pkt *pkt = urb->context; |
| 205 | struct ks_bridge *ksb = pkt->ctxt; |
| 206 | |
| 207 | dbg_log_event(ksb, "C TX_URB", urb->status, 0); |
| 208 | pr_debug("status:%d", urb->status); |
| 209 | |
| 210 | if (ksb->ifc) |
| 211 | usb_autopm_put_interface_async(ksb->ifc); |
| 212 | |
| 213 | if (urb->status < 0) |
| 214 | pr_err_ratelimited("urb failed with err:%d", urb->status); |
| 215 | |
| 216 | ksb_free_data_pkt(pkt); |
| 217 | } |
| 218 | |
| 219 | static void ksb_tomdm_work(struct work_struct *w) |
| 220 | { |
| 221 | struct ks_bridge *ksb = container_of(w, struct ks_bridge, to_mdm_work); |
| 222 | struct data_pkt *pkt; |
| 223 | unsigned long flags; |
| 224 | struct urb *urb; |
| 225 | int ret; |
| 226 | |
| 227 | spin_lock_irqsave(&ksb->lock, flags); |
| 228 | while (!list_empty(&ksb->to_mdm_list) |
| 229 | && test_bit(USB_DEV_CONNECTED, &ksb->flags)) { |
| 230 | pkt = list_first_entry(&ksb->to_mdm_list, |
| 231 | struct data_pkt, list); |
| 232 | list_del_init(&pkt->list); |
| 233 | spin_unlock_irqrestore(&ksb->lock, flags); |
| 234 | |
| 235 | urb = usb_alloc_urb(0, GFP_KERNEL); |
| 236 | if (!urb) { |
| 237 | pr_err_ratelimited("unable to allocate urb"); |
| 238 | ksb_free_data_pkt(pkt); |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | ret = usb_autopm_get_interface(ksb->ifc); |
| 243 | if (ret < 0 && ret != -EAGAIN && ret != -EACCES) { |
| 244 | pr_err_ratelimited("autopm_get failed:%d", ret); |
| 245 | usb_free_urb(urb); |
| 246 | ksb_free_data_pkt(pkt); |
| 247 | return; |
| 248 | } |
| 249 | usb_fill_bulk_urb(urb, ksb->udev, ksb->out_pipe, |
| 250 | pkt->buf, pkt->len, ksb_tx_cb, pkt); |
| 251 | usb_anchor_urb(urb, &ksb->submitted); |
| 252 | |
| 253 | dbg_log_event(ksb, "S TX_URB", pkt->len, 0); |
| 254 | |
| 255 | ret = usb_submit_urb(urb, GFP_KERNEL); |
| 256 | if (ret) { |
| 257 | pr_err("out urb submission failed"); |
| 258 | usb_unanchor_urb(urb); |
| 259 | usb_free_urb(urb); |
| 260 | ksb_free_data_pkt(pkt); |
| 261 | usb_autopm_put_interface(ksb->ifc); |
| 262 | return; |
| 263 | } |
| 264 | |
| 265 | spin_lock_irqsave(&ksb->lock, flags); |
| 266 | } |
| 267 | spin_unlock_irqrestore(&ksb->lock, flags); |
| 268 | } |
| 269 | |
| 270 | static ssize_t ksb_fs_write(struct file *fp, const char __user *buf, |
| 271 | size_t count, loff_t *pos) |
| 272 | { |
| 273 | int ret; |
| 274 | struct data_pkt *pkt; |
| 275 | unsigned long flags; |
| 276 | struct ks_bridge *ksb = fp->private_data; |
| 277 | |
| 278 | pkt = ksb_alloc_data_pkt(count, GFP_KERNEL, ksb); |
| 279 | if (IS_ERR(pkt)) { |
| 280 | pr_err("unable to allocate data packet"); |
| 281 | return PTR_ERR(pkt); |
| 282 | } |
| 283 | |
| 284 | ret = copy_from_user(pkt->buf, buf, count); |
| 285 | if (ret) { |
| 286 | pr_err("copy_from_user failed: err:%d", ret); |
| 287 | ksb_free_data_pkt(pkt); |
| 288 | return ret; |
| 289 | } |
| 290 | |
| 291 | spin_lock_irqsave(&ksb->lock, flags); |
| 292 | list_add_tail(&pkt->list, &ksb->to_mdm_list); |
| 293 | spin_unlock_irqrestore(&ksb->lock, flags); |
| 294 | |
| 295 | queue_work(ksb->wq, &ksb->to_mdm_work); |
| 296 | |
| 297 | return count; |
| 298 | } |
| 299 | |
| 300 | static int efs_fs_open(struct inode *ip, struct file *fp) |
| 301 | { |
| 302 | struct ks_bridge *ksb = __ksb[EFS_BRIDGE_INDEX]; |
| 303 | |
| 304 | pr_debug(":%s", ksb->name); |
| 305 | dbg_log_event(ksb, "EFS-FS-OPEN", 0, 0); |
| 306 | |
| 307 | if (!ksb) { |
| 308 | pr_err("ksb is being removed"); |
| 309 | return -ENODEV; |
| 310 | } |
| 311 | |
| 312 | fp->private_data = ksb; |
| 313 | set_bit(FILE_OPENED, &ksb->flags); |
| 314 | |
| 315 | if (test_bit(USB_DEV_CONNECTED, &ksb->flags)) |
| 316 | queue_work(ksb->wq, &ksb->start_rx_work); |
| 317 | |
| 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | static int ksb_fs_open(struct inode *ip, struct file *fp) |
| 322 | { |
| 323 | struct ks_bridge *ksb = __ksb[BOOT_BRIDGE_INDEX]; |
| 324 | |
| 325 | pr_debug(":%s", ksb->name); |
| 326 | dbg_log_event(ksb, "KS-FS-OPEN", 0, 0); |
| 327 | |
| 328 | if (!ksb) { |
| 329 | pr_err("ksb is being removed"); |
| 330 | return -ENODEV; |
| 331 | } |
| 332 | |
| 333 | fp->private_data = ksb; |
| 334 | set_bit(FILE_OPENED, &ksb->flags); |
| 335 | |
| 336 | if (test_bit(USB_DEV_CONNECTED, &ksb->flags)) |
| 337 | queue_work(ksb->wq, &ksb->start_rx_work); |
| 338 | |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | static int ksb_fs_release(struct inode *ip, struct file *fp) |
| 343 | { |
| 344 | struct ks_bridge *ksb = fp->private_data; |
| 345 | |
| 346 | pr_debug(":%s", ksb->name); |
| 347 | dbg_log_event(ksb, "FS-RELEASE", 0, 0); |
| 348 | |
| 349 | clear_bit(FILE_OPENED, &ksb->flags); |
| 350 | fp->private_data = NULL; |
| 351 | |
| 352 | return 0; |
| 353 | } |
| 354 | |
| 355 | static const struct file_operations ksb_fops = { |
| 356 | .owner = THIS_MODULE, |
| 357 | .read = ksb_fs_read, |
| 358 | .write = ksb_fs_write, |
| 359 | .open = ksb_fs_open, |
| 360 | .release = ksb_fs_release, |
| 361 | }; |
| 362 | |
| 363 | static struct miscdevice ksb_fboot_dev = { |
| 364 | .minor = MISC_DYNAMIC_MINOR, |
| 365 | .name = "ks_bridge", |
| 366 | .fops = &ksb_fops, |
| 367 | }; |
| 368 | |
| 369 | static const struct file_operations efs_fops = { |
| 370 | .owner = THIS_MODULE, |
| 371 | .read = ksb_fs_read, |
| 372 | .write = ksb_fs_write, |
| 373 | .open = efs_fs_open, |
| 374 | .release = ksb_fs_release, |
| 375 | }; |
| 376 | |
| 377 | static struct miscdevice ksb_efs_dev = { |
| 378 | .minor = MISC_DYNAMIC_MINOR, |
| 379 | .name = "efs_bridge", |
| 380 | .fops = &efs_fops, |
| 381 | }; |
| 382 | |
| 383 | static const struct usb_device_id ksb_usb_ids[] = { |
| 384 | { USB_DEVICE(0x5c6, 0x9008), |
| 385 | .driver_info = (unsigned long)&ksb_fboot_dev, }, |
| 386 | { USB_DEVICE(0x5c6, 0x9048), |
| 387 | .driver_info = (unsigned long)&ksb_efs_dev, }, |
| 388 | { USB_DEVICE(0x5c6, 0x904C), |
| 389 | .driver_info = (unsigned long)&ksb_efs_dev, }, |
| 390 | |
| 391 | {} /* terminating entry */ |
| 392 | }; |
| 393 | MODULE_DEVICE_TABLE(usb, ksb_usb_ids); |
| 394 | |
| 395 | static void ksb_rx_cb(struct urb *urb); |
| 396 | static void submit_one_urb(struct ks_bridge *ksb) |
| 397 | { |
| 398 | struct data_pkt *pkt; |
| 399 | struct urb *urb; |
| 400 | int ret; |
| 401 | |
| 402 | pkt = ksb_alloc_data_pkt(MAX_DATA_PKT_SIZE, GFP_ATOMIC, ksb); |
| 403 | if (IS_ERR(pkt)) { |
| 404 | pr_err("unable to allocate data pkt"); |
| 405 | return; |
| 406 | } |
| 407 | |
| 408 | urb = usb_alloc_urb(0, GFP_ATOMIC); |
| 409 | if (!urb) { |
| 410 | pr_err("unable to allocate urb"); |
| 411 | ksb_free_data_pkt(pkt); |
| 412 | return; |
| 413 | } |
| 414 | ksb->alloced_read_pkts++; |
| 415 | |
| 416 | usb_fill_bulk_urb(urb, ksb->udev, ksb->in_pipe, |
| 417 | pkt->buf, pkt->len, |
| 418 | ksb_rx_cb, pkt); |
| 419 | usb_anchor_urb(urb, &ksb->submitted); |
| 420 | |
| 421 | dbg_log_event(ksb, "S RX_URB", pkt->len, 0); |
| 422 | |
| 423 | ret = usb_submit_urb(urb, GFP_ATOMIC); |
| 424 | if (ret) { |
| 425 | pr_err("in urb submission failed"); |
| 426 | usb_unanchor_urb(urb); |
| 427 | usb_free_urb(urb); |
| 428 | ksb_free_data_pkt(pkt); |
| 429 | ksb->alloced_read_pkts--; |
| 430 | return; |
| 431 | } |
| 432 | |
| 433 | usb_free_urb(urb); |
| 434 | } |
| 435 | static void ksb_rx_cb(struct urb *urb) |
| 436 | { |
| 437 | struct data_pkt *pkt = urb->context; |
| 438 | struct ks_bridge *ksb = pkt->ctxt; |
| 439 | |
| 440 | dbg_log_event(ksb, "C RX_URB", urb->status, urb->actual_length); |
| 441 | |
| 442 | pr_debug("status:%d actual:%d", urb->status, urb->actual_length); |
| 443 | |
Hemant Kumar | 853ebf6 | 2012-08-31 18:38:32 -0700 | [diff] [blame^] | 444 | /*non zero len of data received while unlinking urb*/ |
| 445 | if (urb->status == -ENOENT && urb->actual_length > 0) |
| 446 | goto add_to_list; |
| 447 | |
Vamsi Krishna | 5b94471 | 2012-06-29 18:36:23 -0700 | [diff] [blame] | 448 | if (urb->status < 0) { |
Hemant Kumar | 853ebf6 | 2012-08-31 18:38:32 -0700 | [diff] [blame^] | 449 | if (urb->status != -ESHUTDOWN && urb->status != -ENOENT |
| 450 | && urb->status != -EPROTO) |
Vamsi Krishna | 5b94471 | 2012-06-29 18:36:23 -0700 | [diff] [blame] | 451 | pr_err_ratelimited("urb failed with err:%d", |
| 452 | urb->status); |
| 453 | ksb_free_data_pkt(pkt); |
| 454 | ksb->alloced_read_pkts--; |
| 455 | return; |
| 456 | } |
| 457 | |
| 458 | if (urb->actual_length == 0) { |
| 459 | ksb_free_data_pkt(pkt); |
| 460 | ksb->alloced_read_pkts--; |
| 461 | goto resubmit_urb; |
| 462 | } |
| 463 | |
Hemant Kumar | 853ebf6 | 2012-08-31 18:38:32 -0700 | [diff] [blame^] | 464 | add_to_list: |
Vamsi Krishna | 5b94471 | 2012-06-29 18:36:23 -0700 | [diff] [blame] | 465 | spin_lock(&ksb->lock); |
| 466 | pkt->len = urb->actual_length; |
| 467 | list_add_tail(&pkt->list, &ksb->to_ks_list); |
| 468 | spin_unlock(&ksb->lock); |
| 469 | |
| 470 | /* wake up read thread */ |
| 471 | wake_up(&ksb->ks_wait_q); |
| 472 | |
| 473 | resubmit_urb: |
| 474 | submit_one_urb(ksb); |
| 475 | |
| 476 | } |
| 477 | |
| 478 | static void ksb_start_rx_work(struct work_struct *w) |
| 479 | { |
| 480 | struct ks_bridge *ksb = |
| 481 | container_of(w, struct ks_bridge, start_rx_work); |
| 482 | struct data_pkt *pkt; |
| 483 | struct urb *urb; |
| 484 | int i = 0; |
| 485 | int ret; |
| 486 | |
| 487 | for (i = 0; i < NO_RX_REQS; i++) { |
| 488 | pkt = ksb_alloc_data_pkt(MAX_DATA_PKT_SIZE, GFP_KERNEL, ksb); |
| 489 | if (IS_ERR(pkt)) { |
| 490 | pr_err("unable to allocate data pkt"); |
| 491 | return; |
| 492 | } |
| 493 | |
| 494 | urb = usb_alloc_urb(0, GFP_KERNEL); |
| 495 | if (!urb) { |
| 496 | pr_err("unable to allocate urb"); |
| 497 | ksb_free_data_pkt(pkt); |
| 498 | return; |
| 499 | } |
| 500 | |
| 501 | ret = usb_autopm_get_interface(ksb->ifc); |
| 502 | if (ret < 0 && ret != -EAGAIN && ret != -EACCES) { |
| 503 | pr_err_ratelimited("autopm_get failed:%d", ret); |
| 504 | usb_free_urb(urb); |
| 505 | ksb_free_data_pkt(pkt); |
| 506 | return; |
| 507 | } |
| 508 | ksb->alloced_read_pkts++; |
| 509 | |
| 510 | usb_fill_bulk_urb(urb, ksb->udev, ksb->in_pipe, |
| 511 | pkt->buf, pkt->len, |
| 512 | ksb_rx_cb, pkt); |
| 513 | usb_anchor_urb(urb, &ksb->submitted); |
| 514 | |
| 515 | dbg_log_event(ksb, "S RX_URB", pkt->len, 0); |
| 516 | |
| 517 | ret = usb_submit_urb(urb, GFP_KERNEL); |
| 518 | if (ret) { |
| 519 | pr_err("in urb submission failed"); |
| 520 | usb_unanchor_urb(urb); |
| 521 | usb_free_urb(urb); |
| 522 | ksb_free_data_pkt(pkt); |
| 523 | ksb->alloced_read_pkts--; |
| 524 | usb_autopm_put_interface(ksb->ifc); |
| 525 | return; |
| 526 | } |
| 527 | |
| 528 | usb_autopm_put_interface_async(ksb->ifc); |
| 529 | usb_free_urb(urb); |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | static int |
| 534 | ksb_usb_probe(struct usb_interface *ifc, const struct usb_device_id *id) |
| 535 | { |
| 536 | __u8 ifc_num; |
| 537 | struct usb_host_interface *ifc_desc; |
| 538 | struct usb_endpoint_descriptor *ep_desc; |
| 539 | int i; |
| 540 | struct ks_bridge *ksb; |
Vamsi Krishna | 5b94471 | 2012-06-29 18:36:23 -0700 | [diff] [blame] | 541 | |
| 542 | ifc_num = ifc->cur_altsetting->desc.bInterfaceNumber; |
| 543 | |
| 544 | switch (id->idProduct) { |
| 545 | case 0x9008: |
| 546 | if (ifc_num != 0) |
| 547 | return -ENODEV; |
| 548 | ksb = __ksb[BOOT_BRIDGE_INDEX]; |
| 549 | break; |
| 550 | case 0x9048: |
| 551 | case 0x904C: |
| 552 | if (ifc_num != 2) |
| 553 | return -ENODEV; |
| 554 | ksb = __ksb[EFS_BRIDGE_INDEX]; |
| 555 | break; |
| 556 | default: |
| 557 | return -ENODEV; |
| 558 | } |
| 559 | |
| 560 | if (!ksb) { |
| 561 | pr_err("ksb is not initialized"); |
| 562 | return -ENODEV; |
| 563 | } |
| 564 | |
| 565 | ksb->udev = usb_get_dev(interface_to_usbdev(ifc)); |
| 566 | ksb->ifc = ifc; |
| 567 | ifc_desc = ifc->cur_altsetting; |
| 568 | |
| 569 | for (i = 0; i < ifc_desc->desc.bNumEndpoints; i++) { |
| 570 | ep_desc = &ifc_desc->endpoint[i].desc; |
| 571 | |
| 572 | if (!ksb->in_epAddr && usb_endpoint_is_bulk_in(ep_desc)) |
| 573 | ksb->in_epAddr = ep_desc->bEndpointAddress; |
| 574 | |
| 575 | if (!ksb->out_epAddr && usb_endpoint_is_bulk_out(ep_desc)) |
| 576 | ksb->out_epAddr = ep_desc->bEndpointAddress; |
| 577 | } |
| 578 | |
| 579 | if (!(ksb->in_epAddr && ksb->out_epAddr)) { |
| 580 | pr_err("could not find bulk in and bulk out endpoints"); |
| 581 | usb_put_dev(ksb->udev); |
| 582 | ksb->ifc = NULL; |
| 583 | return -ENODEV; |
| 584 | } |
| 585 | |
| 586 | ksb->in_pipe = usb_rcvbulkpipe(ksb->udev, ksb->in_epAddr); |
| 587 | ksb->out_pipe = usb_sndbulkpipe(ksb->udev, ksb->out_epAddr); |
| 588 | |
| 589 | usb_set_intfdata(ifc, ksb); |
| 590 | set_bit(USB_DEV_CONNECTED, &ksb->flags); |
| 591 | |
| 592 | dbg_log_event(ksb, "PID-ATT", id->idProduct, 0); |
| 593 | |
Hemant Kumar | da86bd2 | 2012-08-24 19:42:31 -0700 | [diff] [blame] | 594 | ksb->fs_dev = (struct miscdevice *)id->driver_info; |
| 595 | misc_register(ksb->fs_dev); |
Vamsi Krishna | 5b94471 | 2012-06-29 18:36:23 -0700 | [diff] [blame] | 596 | |
| 597 | usb_enable_autosuspend(ksb->udev); |
| 598 | |
| 599 | pr_debug("usb dev connected"); |
| 600 | |
| 601 | return 0; |
| 602 | } |
| 603 | |
| 604 | static int ksb_usb_suspend(struct usb_interface *ifc, pm_message_t message) |
| 605 | { |
| 606 | struct ks_bridge *ksb = usb_get_intfdata(ifc); |
| 607 | |
| 608 | dbg_log_event(ksb, "SUSPEND", 0, 0); |
| 609 | |
Hemant Kumar | 853ebf6 | 2012-08-31 18:38:32 -0700 | [diff] [blame^] | 610 | pr_debug("read cnt: %d", ksb->alloced_read_pkts); |
Vamsi Krishna | 5b94471 | 2012-06-29 18:36:23 -0700 | [diff] [blame] | 611 | |
| 612 | usb_kill_anchored_urbs(&ksb->submitted); |
| 613 | |
| 614 | return 0; |
| 615 | } |
| 616 | |
| 617 | static int ksb_usb_resume(struct usb_interface *ifc) |
| 618 | { |
| 619 | struct ks_bridge *ksb = usb_get_intfdata(ifc); |
| 620 | |
| 621 | dbg_log_event(ksb, "RESUME", 0, 0); |
| 622 | |
| 623 | if (test_bit(FILE_OPENED, &ksb->flags)) |
| 624 | queue_work(ksb->wq, &ksb->start_rx_work); |
| 625 | |
| 626 | return 0; |
| 627 | } |
| 628 | |
| 629 | static void ksb_usb_disconnect(struct usb_interface *ifc) |
| 630 | { |
| 631 | struct ks_bridge *ksb = usb_get_intfdata(ifc); |
| 632 | unsigned long flags; |
| 633 | struct data_pkt *pkt; |
| 634 | |
| 635 | dbg_log_event(ksb, "PID-DETACH", 0, 0); |
| 636 | |
| 637 | clear_bit(USB_DEV_CONNECTED, &ksb->flags); |
| 638 | wake_up(&ksb->ks_wait_q); |
| 639 | cancel_work_sync(&ksb->to_mdm_work); |
| 640 | |
| 641 | usb_kill_anchored_urbs(&ksb->submitted); |
| 642 | |
| 643 | spin_lock_irqsave(&ksb->lock, flags); |
| 644 | while (!list_empty(&ksb->to_ks_list)) { |
| 645 | pkt = list_first_entry(&ksb->to_ks_list, |
| 646 | struct data_pkt, list); |
| 647 | list_del_init(&pkt->list); |
| 648 | ksb_free_data_pkt(pkt); |
| 649 | } |
| 650 | while (!list_empty(&ksb->to_mdm_list)) { |
| 651 | pkt = list_first_entry(&ksb->to_mdm_list, |
| 652 | struct data_pkt, list); |
| 653 | list_del_init(&pkt->list); |
| 654 | ksb_free_data_pkt(pkt); |
| 655 | } |
| 656 | spin_unlock_irqrestore(&ksb->lock, flags); |
| 657 | |
Hemant Kumar | da86bd2 | 2012-08-24 19:42:31 -0700 | [diff] [blame] | 658 | misc_deregister(ksb->fs_dev); |
Vamsi Krishna | 5b94471 | 2012-06-29 18:36:23 -0700 | [diff] [blame] | 659 | usb_put_dev(ksb->udev); |
| 660 | ksb->ifc = NULL; |
| 661 | usb_set_intfdata(ifc, NULL); |
| 662 | |
| 663 | return; |
| 664 | } |
| 665 | |
| 666 | static struct usb_driver ksb_usb_driver = { |
| 667 | .name = "ks_bridge", |
| 668 | .probe = ksb_usb_probe, |
| 669 | .disconnect = ksb_usb_disconnect, |
| 670 | .suspend = ksb_usb_suspend, |
| 671 | .resume = ksb_usb_resume, |
| 672 | .id_table = ksb_usb_ids, |
| 673 | .supports_autosuspend = 1, |
| 674 | }; |
| 675 | |
| 676 | static ssize_t ksb_debug_show(struct seq_file *s, void *unused) |
| 677 | { |
| 678 | unsigned long flags; |
| 679 | struct ks_bridge *ksb = s->private; |
| 680 | int i; |
| 681 | |
| 682 | read_lock_irqsave(&ksb->dbg_lock, flags); |
| 683 | for (i = 0; i < DBG_MAX_MSG; i++) { |
| 684 | if (i == (ksb->dbg_idx - 1)) |
| 685 | seq_printf(s, "-->%s\n", ksb->dbgbuf[i]); |
| 686 | else |
| 687 | seq_printf(s, "%s\n", ksb->dbgbuf[i]); |
| 688 | } |
| 689 | read_unlock_irqrestore(&ksb->dbg_lock, flags); |
| 690 | |
| 691 | return 0; |
| 692 | } |
| 693 | |
| 694 | static int ksb_debug_open(struct inode *ip, struct file *fp) |
| 695 | { |
| 696 | return single_open(fp, ksb_debug_show, ip->i_private); |
| 697 | |
| 698 | return 0; |
| 699 | } |
| 700 | |
| 701 | static const struct file_operations dbg_fops = { |
| 702 | .open = ksb_debug_open, |
| 703 | .read = seq_read, |
| 704 | .llseek = seq_lseek, |
| 705 | .release = single_release, |
| 706 | }; |
| 707 | static struct dentry *dbg_dir; |
| 708 | static int __init ksb_init(void) |
| 709 | { |
| 710 | struct ks_bridge *ksb; |
| 711 | int num_instances = 0; |
| 712 | int ret = 0; |
| 713 | int i; |
| 714 | |
| 715 | dbg_dir = debugfs_create_dir("ks_bridge", NULL); |
| 716 | if (IS_ERR(dbg_dir)) |
| 717 | pr_err("unable to create debug dir"); |
| 718 | |
| 719 | for (i = 0; i < NO_BRIDGE_INSTANCES; i++) { |
| 720 | ksb = kzalloc(sizeof(struct ks_bridge), GFP_KERNEL); |
| 721 | if (!ksb) { |
| 722 | pr_err("unable to allocat mem for ks_bridge"); |
Hemant Kumar | da86bd2 | 2012-08-24 19:42:31 -0700 | [diff] [blame] | 723 | ret = -ENOMEM; |
| 724 | goto dev_free; |
Vamsi Krishna | 5b94471 | 2012-06-29 18:36:23 -0700 | [diff] [blame] | 725 | } |
| 726 | __ksb[i] = ksb; |
| 727 | |
| 728 | ksb->name = kasprintf(GFP_KERNEL, "ks_bridge:%i", i + 1); |
| 729 | if (!ksb->name) { |
| 730 | pr_info("unable to allocate name"); |
| 731 | kfree(ksb); |
| 732 | ret = -ENOMEM; |
| 733 | goto dev_free; |
| 734 | } |
| 735 | |
| 736 | spin_lock_init(&ksb->lock); |
| 737 | INIT_LIST_HEAD(&ksb->to_mdm_list); |
| 738 | INIT_LIST_HEAD(&ksb->to_ks_list); |
| 739 | init_waitqueue_head(&ksb->ks_wait_q); |
| 740 | ksb->wq = create_singlethread_workqueue(ksb->name); |
| 741 | if (!ksb->wq) { |
| 742 | pr_err("unable to allocate workqueue"); |
| 743 | kfree(ksb->name); |
| 744 | kfree(ksb); |
| 745 | ret = -ENOMEM; |
| 746 | goto dev_free; |
| 747 | } |
| 748 | |
| 749 | INIT_WORK(&ksb->to_mdm_work, ksb_tomdm_work); |
| 750 | INIT_WORK(&ksb->start_rx_work, ksb_start_rx_work); |
| 751 | init_usb_anchor(&ksb->submitted); |
| 752 | |
| 753 | ksb->dbg_idx = 0; |
| 754 | ksb->dbg_lock = __RW_LOCK_UNLOCKED(lck); |
| 755 | |
| 756 | if (!IS_ERR(dbg_dir)) |
| 757 | debugfs_create_file(ksb->name, S_IRUGO, dbg_dir, |
| 758 | ksb, &dbg_fops); |
| 759 | |
| 760 | num_instances++; |
| 761 | } |
| 762 | |
| 763 | ret = usb_register(&ksb_usb_driver); |
| 764 | if (ret) { |
| 765 | pr_err("unable to register ks bridge driver"); |
| 766 | goto dev_free; |
| 767 | } |
| 768 | |
| 769 | pr_info("init done"); |
| 770 | |
| 771 | return 0; |
| 772 | |
| 773 | dev_free: |
| 774 | if (!IS_ERR(dbg_dir)) |
| 775 | debugfs_remove_recursive(dbg_dir); |
| 776 | |
| 777 | for (i = 0; i < num_instances; i++) { |
| 778 | ksb = __ksb[i]; |
| 779 | |
| 780 | destroy_workqueue(ksb->wq); |
| 781 | kfree(ksb->name); |
| 782 | kfree(ksb); |
| 783 | } |
| 784 | |
| 785 | return ret; |
| 786 | |
| 787 | } |
| 788 | |
| 789 | static void __exit ksb_exit(void) |
| 790 | { |
| 791 | struct ks_bridge *ksb; |
| 792 | int i; |
| 793 | |
| 794 | if (!IS_ERR(dbg_dir)) |
| 795 | debugfs_remove_recursive(dbg_dir); |
| 796 | |
| 797 | usb_deregister(&ksb_usb_driver); |
| 798 | |
| 799 | for (i = 0; i < NO_BRIDGE_INSTANCES; i++) { |
| 800 | ksb = __ksb[i]; |
| 801 | |
| 802 | destroy_workqueue(ksb->wq); |
| 803 | kfree(ksb->name); |
| 804 | kfree(ksb); |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | module_init(ksb_init); |
| 809 | module_exit(ksb_exit); |
| 810 | |
| 811 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 812 | MODULE_VERSION(DRIVER_VERSION); |
| 813 | MODULE_LICENSE("GPL v2"); |