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; |
| 61 | |
| 62 | /* usb specific */ |
| 63 | struct usb_device *udev; |
| 64 | struct usb_interface *ifc; |
| 65 | __u8 in_epAddr; |
| 66 | __u8 out_epAddr; |
| 67 | unsigned int in_pipe; |
| 68 | unsigned int out_pipe; |
| 69 | struct usb_anchor submitted; |
| 70 | |
| 71 | unsigned long flags; |
| 72 | unsigned int alloced_read_pkts; |
| 73 | |
| 74 | #define DBG_MSG_LEN 40 |
| 75 | #define DBG_MAX_MSG 500 |
| 76 | unsigned int dbg_idx; |
| 77 | rwlock_t dbg_lock; |
| 78 | char (dbgbuf[DBG_MAX_MSG])[DBG_MSG_LEN]; /* buffer */ |
| 79 | }; |
| 80 | struct ks_bridge *__ksb[NO_BRIDGE_INSTANCES]; |
| 81 | |
| 82 | /* by default debugging is enabled */ |
| 83 | static unsigned int enable_dbg = 1; |
| 84 | module_param(enable_dbg, uint, S_IRUGO | S_IWUSR); |
| 85 | |
| 86 | static void |
| 87 | dbg_log_event(struct ks_bridge *ksb, char *event, int d1, int d2) |
| 88 | { |
| 89 | unsigned long flags; |
| 90 | unsigned long long t; |
| 91 | unsigned long nanosec; |
| 92 | |
| 93 | if (!enable_dbg) |
| 94 | return; |
| 95 | |
| 96 | write_lock_irqsave(&ksb->dbg_lock, flags); |
| 97 | t = cpu_clock(smp_processor_id()); |
| 98 | nanosec = do_div(t, 1000000000)/1000; |
| 99 | scnprintf(ksb->dbgbuf[ksb->dbg_idx], DBG_MSG_LEN, "%5lu.%06lu:%s:%x:%x", |
| 100 | (unsigned long)t, nanosec, event, d1, d2); |
| 101 | |
| 102 | ksb->dbg_idx++; |
| 103 | ksb->dbg_idx = ksb->dbg_idx % DBG_MAX_MSG; |
| 104 | write_unlock_irqrestore(&ksb->dbg_lock, flags); |
| 105 | } |
| 106 | |
| 107 | static |
| 108 | struct data_pkt *ksb_alloc_data_pkt(size_t count, gfp_t flags, void *ctxt) |
| 109 | { |
| 110 | struct data_pkt *pkt; |
| 111 | |
| 112 | pkt = kzalloc(sizeof(struct data_pkt), flags); |
| 113 | if (!pkt) { |
| 114 | pr_err("failed to allocate data packet\n"); |
| 115 | return ERR_PTR(-ENOMEM); |
| 116 | } |
| 117 | |
| 118 | pkt->buf = kmalloc(count, flags); |
| 119 | if (!pkt->buf) { |
| 120 | pr_err("failed to allocate data buffer\n"); |
| 121 | kfree(pkt); |
| 122 | return ERR_PTR(-ENOMEM); |
| 123 | } |
| 124 | |
| 125 | pkt->len = count; |
| 126 | INIT_LIST_HEAD(&pkt->list); |
| 127 | pkt->ctxt = ctxt; |
| 128 | |
| 129 | return pkt; |
| 130 | } |
| 131 | |
| 132 | static void ksb_free_data_pkt(struct data_pkt *pkt) |
| 133 | { |
| 134 | kfree(pkt->buf); |
| 135 | kfree(pkt); |
| 136 | } |
| 137 | |
| 138 | |
| 139 | static ssize_t ksb_fs_read(struct file *fp, char __user *buf, |
| 140 | size_t count, loff_t *pos) |
| 141 | { |
| 142 | int ret; |
| 143 | unsigned long flags; |
| 144 | struct ks_bridge *ksb = fp->private_data; |
| 145 | struct data_pkt *pkt; |
| 146 | size_t space, copied; |
| 147 | |
| 148 | read_start: |
| 149 | if (!test_bit(USB_DEV_CONNECTED, &ksb->flags)) |
| 150 | return -ENODEV; |
| 151 | |
| 152 | spin_lock_irqsave(&ksb->lock, flags); |
| 153 | if (list_empty(&ksb->to_ks_list)) { |
| 154 | spin_unlock_irqrestore(&ksb->lock, flags); |
| 155 | ret = wait_event_interruptible(ksb->ks_wait_q, |
| 156 | !list_empty(&ksb->to_ks_list) || |
| 157 | !test_bit(USB_DEV_CONNECTED, &ksb->flags)); |
| 158 | if (ret < 0) |
| 159 | return ret; |
| 160 | |
| 161 | goto read_start; |
| 162 | } |
| 163 | |
| 164 | space = count; |
| 165 | copied = 0; |
| 166 | while (!list_empty(&ksb->to_ks_list) && space) { |
| 167 | size_t len; |
| 168 | |
| 169 | pkt = list_first_entry(&ksb->to_ks_list, struct data_pkt, list); |
| 170 | len = min_t(size_t, space, pkt->len); |
| 171 | pkt->n_read += len; |
| 172 | spin_unlock_irqrestore(&ksb->lock, flags); |
| 173 | |
| 174 | ret = copy_to_user(buf + copied, pkt->buf, len); |
| 175 | if (ret) { |
| 176 | pr_err("copy_to_user failed err:%d\n", ret); |
| 177 | ksb_free_data_pkt(pkt); |
| 178 | ksb->alloced_read_pkts--; |
| 179 | return ret; |
| 180 | } |
| 181 | |
| 182 | space -= len; |
| 183 | copied += len; |
| 184 | |
| 185 | spin_lock_irqsave(&ksb->lock, flags); |
| 186 | if (pkt->n_read == pkt->len) { |
| 187 | list_del_init(&pkt->list); |
| 188 | ksb_free_data_pkt(pkt); |
| 189 | ksb->alloced_read_pkts--; |
| 190 | } |
| 191 | } |
| 192 | spin_unlock_irqrestore(&ksb->lock, flags); |
| 193 | |
| 194 | dbg_log_event(ksb, "KS_READ", copied, 0); |
| 195 | |
| 196 | pr_debug("count:%d space:%d copied:%d", count, space, copied); |
| 197 | |
| 198 | return copied; |
| 199 | } |
| 200 | |
| 201 | static void ksb_tx_cb(struct urb *urb) |
| 202 | { |
| 203 | struct data_pkt *pkt = urb->context; |
| 204 | struct ks_bridge *ksb = pkt->ctxt; |
| 205 | |
| 206 | dbg_log_event(ksb, "C TX_URB", urb->status, 0); |
| 207 | pr_debug("status:%d", urb->status); |
| 208 | |
| 209 | if (ksb->ifc) |
| 210 | usb_autopm_put_interface_async(ksb->ifc); |
| 211 | |
| 212 | if (urb->status < 0) |
| 213 | pr_err_ratelimited("urb failed with err:%d", urb->status); |
| 214 | |
| 215 | ksb_free_data_pkt(pkt); |
| 216 | } |
| 217 | |
| 218 | static void ksb_tomdm_work(struct work_struct *w) |
| 219 | { |
| 220 | struct ks_bridge *ksb = container_of(w, struct ks_bridge, to_mdm_work); |
| 221 | struct data_pkt *pkt; |
| 222 | unsigned long flags; |
| 223 | struct urb *urb; |
| 224 | int ret; |
| 225 | |
| 226 | spin_lock_irqsave(&ksb->lock, flags); |
| 227 | while (!list_empty(&ksb->to_mdm_list) |
| 228 | && test_bit(USB_DEV_CONNECTED, &ksb->flags)) { |
| 229 | pkt = list_first_entry(&ksb->to_mdm_list, |
| 230 | struct data_pkt, list); |
| 231 | list_del_init(&pkt->list); |
| 232 | spin_unlock_irqrestore(&ksb->lock, flags); |
| 233 | |
| 234 | urb = usb_alloc_urb(0, GFP_KERNEL); |
| 235 | if (!urb) { |
| 236 | pr_err_ratelimited("unable to allocate urb"); |
| 237 | ksb_free_data_pkt(pkt); |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | ret = usb_autopm_get_interface(ksb->ifc); |
| 242 | if (ret < 0 && ret != -EAGAIN && ret != -EACCES) { |
| 243 | pr_err_ratelimited("autopm_get failed:%d", ret); |
| 244 | usb_free_urb(urb); |
| 245 | ksb_free_data_pkt(pkt); |
| 246 | return; |
| 247 | } |
| 248 | usb_fill_bulk_urb(urb, ksb->udev, ksb->out_pipe, |
| 249 | pkt->buf, pkt->len, ksb_tx_cb, pkt); |
| 250 | usb_anchor_urb(urb, &ksb->submitted); |
| 251 | |
| 252 | dbg_log_event(ksb, "S TX_URB", pkt->len, 0); |
| 253 | |
| 254 | ret = usb_submit_urb(urb, GFP_KERNEL); |
| 255 | if (ret) { |
| 256 | pr_err("out urb submission failed"); |
| 257 | usb_unanchor_urb(urb); |
| 258 | usb_free_urb(urb); |
| 259 | ksb_free_data_pkt(pkt); |
| 260 | usb_autopm_put_interface(ksb->ifc); |
| 261 | return; |
| 262 | } |
| 263 | |
| 264 | spin_lock_irqsave(&ksb->lock, flags); |
| 265 | } |
| 266 | spin_unlock_irqrestore(&ksb->lock, flags); |
| 267 | } |
| 268 | |
| 269 | static ssize_t ksb_fs_write(struct file *fp, const char __user *buf, |
| 270 | size_t count, loff_t *pos) |
| 271 | { |
| 272 | int ret; |
| 273 | struct data_pkt *pkt; |
| 274 | unsigned long flags; |
| 275 | struct ks_bridge *ksb = fp->private_data; |
| 276 | |
| 277 | pkt = ksb_alloc_data_pkt(count, GFP_KERNEL, ksb); |
| 278 | if (IS_ERR(pkt)) { |
| 279 | pr_err("unable to allocate data packet"); |
| 280 | return PTR_ERR(pkt); |
| 281 | } |
| 282 | |
| 283 | ret = copy_from_user(pkt->buf, buf, count); |
| 284 | if (ret) { |
| 285 | pr_err("copy_from_user failed: err:%d", ret); |
| 286 | ksb_free_data_pkt(pkt); |
| 287 | return ret; |
| 288 | } |
| 289 | |
| 290 | spin_lock_irqsave(&ksb->lock, flags); |
| 291 | list_add_tail(&pkt->list, &ksb->to_mdm_list); |
| 292 | spin_unlock_irqrestore(&ksb->lock, flags); |
| 293 | |
| 294 | queue_work(ksb->wq, &ksb->to_mdm_work); |
| 295 | |
| 296 | return count; |
| 297 | } |
| 298 | |
| 299 | static int efs_fs_open(struct inode *ip, struct file *fp) |
| 300 | { |
| 301 | struct ks_bridge *ksb = __ksb[EFS_BRIDGE_INDEX]; |
| 302 | |
| 303 | pr_debug(":%s", ksb->name); |
| 304 | dbg_log_event(ksb, "EFS-FS-OPEN", 0, 0); |
| 305 | |
| 306 | if (!ksb) { |
| 307 | pr_err("ksb is being removed"); |
| 308 | return -ENODEV; |
| 309 | } |
| 310 | |
| 311 | fp->private_data = ksb; |
| 312 | set_bit(FILE_OPENED, &ksb->flags); |
| 313 | |
| 314 | if (test_bit(USB_DEV_CONNECTED, &ksb->flags)) |
| 315 | queue_work(ksb->wq, &ksb->start_rx_work); |
| 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | static int ksb_fs_open(struct inode *ip, struct file *fp) |
| 321 | { |
| 322 | struct ks_bridge *ksb = __ksb[BOOT_BRIDGE_INDEX]; |
| 323 | |
| 324 | pr_debug(":%s", ksb->name); |
| 325 | dbg_log_event(ksb, "KS-FS-OPEN", 0, 0); |
| 326 | |
| 327 | if (!ksb) { |
| 328 | pr_err("ksb is being removed"); |
| 329 | return -ENODEV; |
| 330 | } |
| 331 | |
| 332 | fp->private_data = ksb; |
| 333 | set_bit(FILE_OPENED, &ksb->flags); |
| 334 | |
| 335 | if (test_bit(USB_DEV_CONNECTED, &ksb->flags)) |
| 336 | queue_work(ksb->wq, &ksb->start_rx_work); |
| 337 | |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | static int ksb_fs_release(struct inode *ip, struct file *fp) |
| 342 | { |
| 343 | struct ks_bridge *ksb = fp->private_data; |
| 344 | |
| 345 | pr_debug(":%s", ksb->name); |
| 346 | dbg_log_event(ksb, "FS-RELEASE", 0, 0); |
| 347 | |
| 348 | clear_bit(FILE_OPENED, &ksb->flags); |
| 349 | fp->private_data = NULL; |
| 350 | |
| 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | static const struct file_operations ksb_fops = { |
| 355 | .owner = THIS_MODULE, |
| 356 | .read = ksb_fs_read, |
| 357 | .write = ksb_fs_write, |
| 358 | .open = ksb_fs_open, |
| 359 | .release = ksb_fs_release, |
| 360 | }; |
| 361 | |
| 362 | static struct miscdevice ksb_fboot_dev = { |
| 363 | .minor = MISC_DYNAMIC_MINOR, |
| 364 | .name = "ks_bridge", |
| 365 | .fops = &ksb_fops, |
| 366 | }; |
| 367 | |
| 368 | static const struct file_operations efs_fops = { |
| 369 | .owner = THIS_MODULE, |
| 370 | .read = ksb_fs_read, |
| 371 | .write = ksb_fs_write, |
| 372 | .open = efs_fs_open, |
| 373 | .release = ksb_fs_release, |
| 374 | }; |
| 375 | |
| 376 | static struct miscdevice ksb_efs_dev = { |
| 377 | .minor = MISC_DYNAMIC_MINOR, |
| 378 | .name = "efs_bridge", |
| 379 | .fops = &efs_fops, |
| 380 | }; |
| 381 | |
| 382 | static const struct usb_device_id ksb_usb_ids[] = { |
| 383 | { USB_DEVICE(0x5c6, 0x9008), |
| 384 | .driver_info = (unsigned long)&ksb_fboot_dev, }, |
| 385 | { USB_DEVICE(0x5c6, 0x9048), |
| 386 | .driver_info = (unsigned long)&ksb_efs_dev, }, |
| 387 | { USB_DEVICE(0x5c6, 0x904C), |
| 388 | .driver_info = (unsigned long)&ksb_efs_dev, }, |
| 389 | |
| 390 | {} /* terminating entry */ |
| 391 | }; |
| 392 | MODULE_DEVICE_TABLE(usb, ksb_usb_ids); |
| 393 | |
| 394 | static void ksb_rx_cb(struct urb *urb); |
| 395 | static void submit_one_urb(struct ks_bridge *ksb) |
| 396 | { |
| 397 | struct data_pkt *pkt; |
| 398 | struct urb *urb; |
| 399 | int ret; |
| 400 | |
| 401 | pkt = ksb_alloc_data_pkt(MAX_DATA_PKT_SIZE, GFP_ATOMIC, ksb); |
| 402 | if (IS_ERR(pkt)) { |
| 403 | pr_err("unable to allocate data pkt"); |
| 404 | return; |
| 405 | } |
| 406 | |
| 407 | urb = usb_alloc_urb(0, GFP_ATOMIC); |
| 408 | if (!urb) { |
| 409 | pr_err("unable to allocate urb"); |
| 410 | ksb_free_data_pkt(pkt); |
| 411 | return; |
| 412 | } |
| 413 | ksb->alloced_read_pkts++; |
| 414 | |
| 415 | usb_fill_bulk_urb(urb, ksb->udev, ksb->in_pipe, |
| 416 | pkt->buf, pkt->len, |
| 417 | ksb_rx_cb, pkt); |
| 418 | usb_anchor_urb(urb, &ksb->submitted); |
| 419 | |
| 420 | dbg_log_event(ksb, "S RX_URB", pkt->len, 0); |
| 421 | |
| 422 | ret = usb_submit_urb(urb, GFP_ATOMIC); |
| 423 | if (ret) { |
| 424 | pr_err("in urb submission failed"); |
| 425 | usb_unanchor_urb(urb); |
| 426 | usb_free_urb(urb); |
| 427 | ksb_free_data_pkt(pkt); |
| 428 | ksb->alloced_read_pkts--; |
| 429 | return; |
| 430 | } |
| 431 | |
| 432 | usb_free_urb(urb); |
| 433 | } |
| 434 | static void ksb_rx_cb(struct urb *urb) |
| 435 | { |
| 436 | struct data_pkt *pkt = urb->context; |
| 437 | struct ks_bridge *ksb = pkt->ctxt; |
| 438 | |
| 439 | dbg_log_event(ksb, "C RX_URB", urb->status, urb->actual_length); |
| 440 | |
| 441 | pr_debug("status:%d actual:%d", urb->status, urb->actual_length); |
| 442 | |
Hemant Kumar | a6194c1 | 2012-08-31 18:38:32 -0700 | [diff] [blame^] | 443 | /*non zero len of data received while unlinking urb*/ |
| 444 | if (urb->status == -ENOENT && urb->actual_length > 0) |
| 445 | goto add_to_list; |
| 446 | |
Vamsi Krishna | 5b94471 | 2012-06-29 18:36:23 -0700 | [diff] [blame] | 447 | if (urb->status < 0) { |
Hemant Kumar | a6194c1 | 2012-08-31 18:38:32 -0700 | [diff] [blame^] | 448 | if (urb->status != -ESHUTDOWN && urb->status != -ENOENT |
| 449 | && urb->status != -EPROTO) |
Vamsi Krishna | 5b94471 | 2012-06-29 18:36:23 -0700 | [diff] [blame] | 450 | pr_err_ratelimited("urb failed with err:%d", |
| 451 | urb->status); |
| 452 | ksb_free_data_pkt(pkt); |
| 453 | ksb->alloced_read_pkts--; |
| 454 | return; |
| 455 | } |
| 456 | |
| 457 | if (urb->actual_length == 0) { |
| 458 | ksb_free_data_pkt(pkt); |
| 459 | ksb->alloced_read_pkts--; |
| 460 | goto resubmit_urb; |
| 461 | } |
| 462 | |
Hemant Kumar | a6194c1 | 2012-08-31 18:38:32 -0700 | [diff] [blame^] | 463 | add_to_list: |
Vamsi Krishna | 5b94471 | 2012-06-29 18:36:23 -0700 | [diff] [blame] | 464 | spin_lock(&ksb->lock); |
| 465 | pkt->len = urb->actual_length; |
| 466 | list_add_tail(&pkt->list, &ksb->to_ks_list); |
| 467 | spin_unlock(&ksb->lock); |
| 468 | |
| 469 | /* wake up read thread */ |
| 470 | wake_up(&ksb->ks_wait_q); |
| 471 | |
| 472 | resubmit_urb: |
| 473 | submit_one_urb(ksb); |
| 474 | |
| 475 | } |
| 476 | |
| 477 | static void ksb_start_rx_work(struct work_struct *w) |
| 478 | { |
| 479 | struct ks_bridge *ksb = |
| 480 | container_of(w, struct ks_bridge, start_rx_work); |
| 481 | struct data_pkt *pkt; |
| 482 | struct urb *urb; |
| 483 | int i = 0; |
| 484 | int ret; |
| 485 | |
| 486 | for (i = 0; i < NO_RX_REQS; i++) { |
| 487 | pkt = ksb_alloc_data_pkt(MAX_DATA_PKT_SIZE, GFP_KERNEL, ksb); |
| 488 | if (IS_ERR(pkt)) { |
| 489 | pr_err("unable to allocate data pkt"); |
| 490 | return; |
| 491 | } |
| 492 | |
| 493 | urb = usb_alloc_urb(0, GFP_KERNEL); |
| 494 | if (!urb) { |
| 495 | pr_err("unable to allocate urb"); |
| 496 | ksb_free_data_pkt(pkt); |
| 497 | return; |
| 498 | } |
| 499 | |
| 500 | ret = usb_autopm_get_interface(ksb->ifc); |
| 501 | if (ret < 0 && ret != -EAGAIN && ret != -EACCES) { |
| 502 | pr_err_ratelimited("autopm_get failed:%d", ret); |
| 503 | usb_free_urb(urb); |
| 504 | ksb_free_data_pkt(pkt); |
| 505 | return; |
| 506 | } |
| 507 | ksb->alloced_read_pkts++; |
| 508 | |
| 509 | usb_fill_bulk_urb(urb, ksb->udev, ksb->in_pipe, |
| 510 | pkt->buf, pkt->len, |
| 511 | ksb_rx_cb, pkt); |
| 512 | usb_anchor_urb(urb, &ksb->submitted); |
| 513 | |
| 514 | dbg_log_event(ksb, "S RX_URB", pkt->len, 0); |
| 515 | |
| 516 | ret = usb_submit_urb(urb, GFP_KERNEL); |
| 517 | if (ret) { |
| 518 | pr_err("in urb submission failed"); |
| 519 | usb_unanchor_urb(urb); |
| 520 | usb_free_urb(urb); |
| 521 | ksb_free_data_pkt(pkt); |
| 522 | ksb->alloced_read_pkts--; |
| 523 | usb_autopm_put_interface(ksb->ifc); |
| 524 | return; |
| 525 | } |
| 526 | |
| 527 | usb_autopm_put_interface_async(ksb->ifc); |
| 528 | usb_free_urb(urb); |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | static int |
| 533 | ksb_usb_probe(struct usb_interface *ifc, const struct usb_device_id *id) |
| 534 | { |
| 535 | __u8 ifc_num; |
| 536 | struct usb_host_interface *ifc_desc; |
| 537 | struct usb_endpoint_descriptor *ep_desc; |
| 538 | int i; |
| 539 | struct ks_bridge *ksb; |
| 540 | struct miscdevice *fs_dev; |
| 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 | |
| 594 | fs_dev = (struct miscdevice *)id->driver_info; |
| 595 | misc_register(fs_dev); |
| 596 | |
Hemant Kumar | f292a32 | 2012-09-07 19:00:21 -0700 | [diff] [blame] | 597 | ifc->needs_remote_wakeup = 1; |
Vamsi Krishna | 5b94471 | 2012-06-29 18:36:23 -0700 | [diff] [blame] | 598 | usb_enable_autosuspend(ksb->udev); |
| 599 | |
| 600 | pr_debug("usb dev connected"); |
| 601 | |
| 602 | return 0; |
| 603 | } |
| 604 | |
| 605 | static int ksb_usb_suspend(struct usb_interface *ifc, pm_message_t message) |
| 606 | { |
| 607 | struct ks_bridge *ksb = usb_get_intfdata(ifc); |
| 608 | |
| 609 | dbg_log_event(ksb, "SUSPEND", 0, 0); |
| 610 | |
Hemant Kumar | a6194c1 | 2012-08-31 18:38:32 -0700 | [diff] [blame^] | 611 | pr_debug("read cnt: %d", ksb->alloced_read_pkts); |
Vamsi Krishna | 5b94471 | 2012-06-29 18:36:23 -0700 | [diff] [blame] | 612 | |
| 613 | usb_kill_anchored_urbs(&ksb->submitted); |
| 614 | |
| 615 | return 0; |
| 616 | } |
| 617 | |
| 618 | static int ksb_usb_resume(struct usb_interface *ifc) |
| 619 | { |
| 620 | struct ks_bridge *ksb = usb_get_intfdata(ifc); |
| 621 | |
| 622 | dbg_log_event(ksb, "RESUME", 0, 0); |
| 623 | |
| 624 | if (test_bit(FILE_OPENED, &ksb->flags)) |
| 625 | queue_work(ksb->wq, &ksb->start_rx_work); |
| 626 | |
| 627 | return 0; |
| 628 | } |
| 629 | |
| 630 | static void ksb_usb_disconnect(struct usb_interface *ifc) |
| 631 | { |
| 632 | struct ks_bridge *ksb = usb_get_intfdata(ifc); |
| 633 | unsigned long flags; |
| 634 | struct data_pkt *pkt; |
| 635 | |
| 636 | dbg_log_event(ksb, "PID-DETACH", 0, 0); |
| 637 | |
| 638 | clear_bit(USB_DEV_CONNECTED, &ksb->flags); |
| 639 | wake_up(&ksb->ks_wait_q); |
| 640 | cancel_work_sync(&ksb->to_mdm_work); |
| 641 | |
| 642 | usb_kill_anchored_urbs(&ksb->submitted); |
| 643 | |
| 644 | spin_lock_irqsave(&ksb->lock, flags); |
| 645 | while (!list_empty(&ksb->to_ks_list)) { |
| 646 | pkt = list_first_entry(&ksb->to_ks_list, |
| 647 | struct data_pkt, list); |
| 648 | list_del_init(&pkt->list); |
| 649 | ksb_free_data_pkt(pkt); |
| 650 | } |
| 651 | while (!list_empty(&ksb->to_mdm_list)) { |
| 652 | pkt = list_first_entry(&ksb->to_mdm_list, |
| 653 | struct data_pkt, list); |
| 654 | list_del_init(&pkt->list); |
| 655 | ksb_free_data_pkt(pkt); |
| 656 | } |
| 657 | spin_unlock_irqrestore(&ksb->lock, flags); |
| 658 | |
Hemant Kumar | f292a32 | 2012-09-07 19:00:21 -0700 | [diff] [blame] | 659 | ifc->needs_remote_wakeup = 0; |
Vamsi Krishna | 5b94471 | 2012-06-29 18:36:23 -0700 | [diff] [blame] | 660 | usb_put_dev(ksb->udev); |
| 661 | ksb->ifc = NULL; |
| 662 | usb_set_intfdata(ifc, NULL); |
| 663 | |
| 664 | return; |
| 665 | } |
| 666 | |
| 667 | static struct usb_driver ksb_usb_driver = { |
| 668 | .name = "ks_bridge", |
| 669 | .probe = ksb_usb_probe, |
| 670 | .disconnect = ksb_usb_disconnect, |
| 671 | .suspend = ksb_usb_suspend, |
| 672 | .resume = ksb_usb_resume, |
| 673 | .id_table = ksb_usb_ids, |
| 674 | .supports_autosuspend = 1, |
| 675 | }; |
| 676 | |
| 677 | static ssize_t ksb_debug_show(struct seq_file *s, void *unused) |
| 678 | { |
| 679 | unsigned long flags; |
| 680 | struct ks_bridge *ksb = s->private; |
| 681 | int i; |
| 682 | |
| 683 | read_lock_irqsave(&ksb->dbg_lock, flags); |
| 684 | for (i = 0; i < DBG_MAX_MSG; i++) { |
| 685 | if (i == (ksb->dbg_idx - 1)) |
| 686 | seq_printf(s, "-->%s\n", ksb->dbgbuf[i]); |
| 687 | else |
| 688 | seq_printf(s, "%s\n", ksb->dbgbuf[i]); |
| 689 | } |
| 690 | read_unlock_irqrestore(&ksb->dbg_lock, flags); |
| 691 | |
| 692 | return 0; |
| 693 | } |
| 694 | |
| 695 | static int ksb_debug_open(struct inode *ip, struct file *fp) |
| 696 | { |
| 697 | return single_open(fp, ksb_debug_show, ip->i_private); |
| 698 | |
| 699 | return 0; |
| 700 | } |
| 701 | |
| 702 | static const struct file_operations dbg_fops = { |
| 703 | .open = ksb_debug_open, |
| 704 | .read = seq_read, |
| 705 | .llseek = seq_lseek, |
| 706 | .release = single_release, |
| 707 | }; |
| 708 | static struct dentry *dbg_dir; |
| 709 | static int __init ksb_init(void) |
| 710 | { |
| 711 | struct ks_bridge *ksb; |
| 712 | int num_instances = 0; |
| 713 | int ret = 0; |
| 714 | int i; |
| 715 | |
| 716 | dbg_dir = debugfs_create_dir("ks_bridge", NULL); |
| 717 | if (IS_ERR(dbg_dir)) |
| 718 | pr_err("unable to create debug dir"); |
| 719 | |
| 720 | for (i = 0; i < NO_BRIDGE_INSTANCES; i++) { |
| 721 | ksb = kzalloc(sizeof(struct ks_bridge), GFP_KERNEL); |
| 722 | if (!ksb) { |
| 723 | pr_err("unable to allocat mem for ks_bridge"); |
| 724 | return -ENOMEM; |
| 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"); |