blob: 2b4f98c3766dfe3759824bca35fafc0ac5e00fb5 [file] [log] [blame]
Vamsi Krishna5b944712012-06-29 18:36:23 -07001/*
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
36struct 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
Hemant Kumarb3779d12012-09-16 20:28:56 -070051#define PENDING_URB_TIMEOUT 10
Vamsi Krishna5b944712012-06-29 18:36:23 -070052
53struct ks_bridge {
54 char *name;
55 spinlock_t lock;
56 struct workqueue_struct *wq;
57 struct work_struct to_mdm_work;
58 struct work_struct start_rx_work;
59 struct list_head to_mdm_list;
60 struct list_head to_ks_list;
61 wait_queue_head_t ks_wait_q;
Hemant Kumarb3779d12012-09-16 20:28:56 -070062 wait_queue_head_t pending_urb_wait;
Hemant Kumarda86bd22012-08-24 19:42:31 -070063 struct miscdevice *fs_dev;
Hemant Kumarb3779d12012-09-16 20:28:56 -070064 atomic_t tx_pending_cnt;
65 atomic_t rx_pending_cnt;
Vamsi Krishna5b944712012-06-29 18:36:23 -070066
67 /* usb specific */
68 struct usb_device *udev;
69 struct usb_interface *ifc;
70 __u8 in_epAddr;
71 __u8 out_epAddr;
72 unsigned int in_pipe;
73 unsigned int out_pipe;
74 struct usb_anchor submitted;
75
76 unsigned long flags;
Vamsi Krishna5b944712012-06-29 18:36:23 -070077
78#define DBG_MSG_LEN 40
79#define DBG_MAX_MSG 500
80 unsigned int dbg_idx;
81 rwlock_t dbg_lock;
82 char (dbgbuf[DBG_MAX_MSG])[DBG_MSG_LEN]; /* buffer */
83};
84struct ks_bridge *__ksb[NO_BRIDGE_INSTANCES];
85
86/* by default debugging is enabled */
87static unsigned int enable_dbg = 1;
88module_param(enable_dbg, uint, S_IRUGO | S_IWUSR);
89
90static void
91dbg_log_event(struct ks_bridge *ksb, char *event, int d1, int d2)
92{
93 unsigned long flags;
94 unsigned long long t;
95 unsigned long nanosec;
96
97 if (!enable_dbg)
98 return;
99
100 write_lock_irqsave(&ksb->dbg_lock, flags);
101 t = cpu_clock(smp_processor_id());
102 nanosec = do_div(t, 1000000000)/1000;
103 scnprintf(ksb->dbgbuf[ksb->dbg_idx], DBG_MSG_LEN, "%5lu.%06lu:%s:%x:%x",
104 (unsigned long)t, nanosec, event, d1, d2);
105
106 ksb->dbg_idx++;
107 ksb->dbg_idx = ksb->dbg_idx % DBG_MAX_MSG;
108 write_unlock_irqrestore(&ksb->dbg_lock, flags);
109}
110
111static
112struct data_pkt *ksb_alloc_data_pkt(size_t count, gfp_t flags, void *ctxt)
113{
114 struct data_pkt *pkt;
115
116 pkt = kzalloc(sizeof(struct data_pkt), flags);
117 if (!pkt) {
118 pr_err("failed to allocate data packet\n");
119 return ERR_PTR(-ENOMEM);
120 }
121
122 pkt->buf = kmalloc(count, flags);
123 if (!pkt->buf) {
124 pr_err("failed to allocate data buffer\n");
125 kfree(pkt);
126 return ERR_PTR(-ENOMEM);
127 }
128
129 pkt->len = count;
130 INIT_LIST_HEAD(&pkt->list);
131 pkt->ctxt = ctxt;
132
133 return pkt;
134}
135
136static void ksb_free_data_pkt(struct data_pkt *pkt)
137{
138 kfree(pkt->buf);
139 kfree(pkt);
140}
141
142
Pavankumar Kondeti5c915c12012-12-14 15:12:46 +0530143static void
144submit_one_urb(struct ks_bridge *ksb, gfp_t flags, struct data_pkt *pkt);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700145static ssize_t ksb_fs_read(struct file *fp, char __user *buf,
146 size_t count, loff_t *pos)
147{
148 int ret;
149 unsigned long flags;
150 struct ks_bridge *ksb = fp->private_data;
151 struct data_pkt *pkt;
152 size_t space, copied;
153
154read_start:
155 if (!test_bit(USB_DEV_CONNECTED, &ksb->flags))
156 return -ENODEV;
157
158 spin_lock_irqsave(&ksb->lock, flags);
159 if (list_empty(&ksb->to_ks_list)) {
160 spin_unlock_irqrestore(&ksb->lock, flags);
161 ret = wait_event_interruptible(ksb->ks_wait_q,
162 !list_empty(&ksb->to_ks_list) ||
163 !test_bit(USB_DEV_CONNECTED, &ksb->flags));
164 if (ret < 0)
165 return ret;
166
167 goto read_start;
168 }
169
170 space = count;
171 copied = 0;
172 while (!list_empty(&ksb->to_ks_list) && space) {
173 size_t len;
174
175 pkt = list_first_entry(&ksb->to_ks_list, struct data_pkt, list);
Hemant Kumarb48f4732012-09-24 12:32:32 -0700176 len = min_t(size_t, space, pkt->len - pkt->n_read);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700177 spin_unlock_irqrestore(&ksb->lock, flags);
178
Hemant Kumarb48f4732012-09-24 12:32:32 -0700179 ret = copy_to_user(buf + copied, pkt->buf + pkt->n_read, len);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700180 if (ret) {
181 pr_err("copy_to_user failed err:%d\n", ret);
182 ksb_free_data_pkt(pkt);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700183 return ret;
184 }
185
Hemant Kumarb48f4732012-09-24 12:32:32 -0700186 pkt->n_read += len;
Vamsi Krishna5b944712012-06-29 18:36:23 -0700187 space -= len;
188 copied += len;
189
190 spin_lock_irqsave(&ksb->lock, flags);
191 if (pkt->n_read == pkt->len) {
Pavankumar Kondeti5c915c12012-12-14 15:12:46 +0530192 /*
193 * re-init the packet and queue it
194 * for more data.
195 */
Vamsi Krishna5b944712012-06-29 18:36:23 -0700196 list_del_init(&pkt->list);
Pavankumar Kondeti5c915c12012-12-14 15:12:46 +0530197 pkt->n_read = 0;
198 pkt->len = MAX_DATA_PKT_SIZE;
199 spin_unlock_irqrestore(&ksb->lock, flags);
200 submit_one_urb(ksb, GFP_KERNEL, pkt);
201 spin_lock_irqsave(&ksb->lock, flags);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700202 }
203 }
204 spin_unlock_irqrestore(&ksb->lock, flags);
205
206 dbg_log_event(ksb, "KS_READ", copied, 0);
207
208 pr_debug("count:%d space:%d copied:%d", count, space, copied);
209
210 return copied;
211}
212
213static void ksb_tx_cb(struct urb *urb)
214{
215 struct data_pkt *pkt = urb->context;
216 struct ks_bridge *ksb = pkt->ctxt;
217
218 dbg_log_event(ksb, "C TX_URB", urb->status, 0);
219 pr_debug("status:%d", urb->status);
220
Hemant Kumarb3779d12012-09-16 20:28:56 -0700221 if (test_bit(USB_DEV_CONNECTED, &ksb->flags))
Vamsi Krishna5b944712012-06-29 18:36:23 -0700222 usb_autopm_put_interface_async(ksb->ifc);
223
224 if (urb->status < 0)
225 pr_err_ratelimited("urb failed with err:%d", urb->status);
226
227 ksb_free_data_pkt(pkt);
Hemant Kumarb3779d12012-09-16 20:28:56 -0700228
229 atomic_dec(&ksb->tx_pending_cnt);
230 wake_up(&ksb->pending_urb_wait);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700231}
232
233static void ksb_tomdm_work(struct work_struct *w)
234{
235 struct ks_bridge *ksb = container_of(w, struct ks_bridge, to_mdm_work);
236 struct data_pkt *pkt;
237 unsigned long flags;
238 struct urb *urb;
239 int ret;
240
241 spin_lock_irqsave(&ksb->lock, flags);
242 while (!list_empty(&ksb->to_mdm_list)
243 && test_bit(USB_DEV_CONNECTED, &ksb->flags)) {
244 pkt = list_first_entry(&ksb->to_mdm_list,
245 struct data_pkt, list);
246 list_del_init(&pkt->list);
247 spin_unlock_irqrestore(&ksb->lock, flags);
248
249 urb = usb_alloc_urb(0, GFP_KERNEL);
250 if (!urb) {
251 pr_err_ratelimited("unable to allocate urb");
252 ksb_free_data_pkt(pkt);
253 return;
254 }
255
256 ret = usb_autopm_get_interface(ksb->ifc);
257 if (ret < 0 && ret != -EAGAIN && ret != -EACCES) {
258 pr_err_ratelimited("autopm_get failed:%d", ret);
259 usb_free_urb(urb);
260 ksb_free_data_pkt(pkt);
261 return;
262 }
263 usb_fill_bulk_urb(urb, ksb->udev, ksb->out_pipe,
264 pkt->buf, pkt->len, ksb_tx_cb, pkt);
265 usb_anchor_urb(urb, &ksb->submitted);
266
267 dbg_log_event(ksb, "S TX_URB", pkt->len, 0);
268
Hemant Kumarb3779d12012-09-16 20:28:56 -0700269 atomic_inc(&ksb->tx_pending_cnt);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700270 ret = usb_submit_urb(urb, GFP_KERNEL);
271 if (ret) {
272 pr_err("out urb submission failed");
273 usb_unanchor_urb(urb);
274 usb_free_urb(urb);
275 ksb_free_data_pkt(pkt);
276 usb_autopm_put_interface(ksb->ifc);
Hemant Kumarb3779d12012-09-16 20:28:56 -0700277 atomic_dec(&ksb->tx_pending_cnt);
278 wake_up(&ksb->pending_urb_wait);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700279 return;
280 }
281
Hemant Kumar1432f6f2012-09-19 12:40:11 -0700282 usb_free_urb(urb);
283
Vamsi Krishna5b944712012-06-29 18:36:23 -0700284 spin_lock_irqsave(&ksb->lock, flags);
285 }
286 spin_unlock_irqrestore(&ksb->lock, flags);
287}
288
289static ssize_t ksb_fs_write(struct file *fp, const char __user *buf,
290 size_t count, loff_t *pos)
291{
292 int ret;
293 struct data_pkt *pkt;
294 unsigned long flags;
295 struct ks_bridge *ksb = fp->private_data;
296
Hemant Kumar2f20bda2012-09-17 14:05:54 -0700297 if (!test_bit(USB_DEV_CONNECTED, &ksb->flags))
298 return -ENODEV;
299
Vamsi Krishna5b944712012-06-29 18:36:23 -0700300 pkt = ksb_alloc_data_pkt(count, GFP_KERNEL, ksb);
301 if (IS_ERR(pkt)) {
302 pr_err("unable to allocate data packet");
303 return PTR_ERR(pkt);
304 }
305
306 ret = copy_from_user(pkt->buf, buf, count);
307 if (ret) {
308 pr_err("copy_from_user failed: err:%d", ret);
309 ksb_free_data_pkt(pkt);
310 return ret;
311 }
312
313 spin_lock_irqsave(&ksb->lock, flags);
314 list_add_tail(&pkt->list, &ksb->to_mdm_list);
315 spin_unlock_irqrestore(&ksb->lock, flags);
316
317 queue_work(ksb->wq, &ksb->to_mdm_work);
318
319 return count;
320}
321
322static int efs_fs_open(struct inode *ip, struct file *fp)
323{
324 struct ks_bridge *ksb = __ksb[EFS_BRIDGE_INDEX];
325
326 pr_debug(":%s", ksb->name);
327 dbg_log_event(ksb, "EFS-FS-OPEN", 0, 0);
328
329 if (!ksb) {
330 pr_err("ksb is being removed");
331 return -ENODEV;
332 }
333
334 fp->private_data = ksb;
335 set_bit(FILE_OPENED, &ksb->flags);
336
337 if (test_bit(USB_DEV_CONNECTED, &ksb->flags))
338 queue_work(ksb->wq, &ksb->start_rx_work);
339
340 return 0;
341}
342
343static int ksb_fs_open(struct inode *ip, struct file *fp)
344{
345 struct ks_bridge *ksb = __ksb[BOOT_BRIDGE_INDEX];
346
347 pr_debug(":%s", ksb->name);
348 dbg_log_event(ksb, "KS-FS-OPEN", 0, 0);
349
350 if (!ksb) {
351 pr_err("ksb is being removed");
352 return -ENODEV;
353 }
354
355 fp->private_data = ksb;
356 set_bit(FILE_OPENED, &ksb->flags);
357
358 if (test_bit(USB_DEV_CONNECTED, &ksb->flags))
359 queue_work(ksb->wq, &ksb->start_rx_work);
360
361 return 0;
362}
363
364static int ksb_fs_release(struct inode *ip, struct file *fp)
365{
366 struct ks_bridge *ksb = fp->private_data;
367
368 pr_debug(":%s", ksb->name);
369 dbg_log_event(ksb, "FS-RELEASE", 0, 0);
370
371 clear_bit(FILE_OPENED, &ksb->flags);
372 fp->private_data = NULL;
373
374 return 0;
375}
376
377static const struct file_operations ksb_fops = {
378 .owner = THIS_MODULE,
379 .read = ksb_fs_read,
380 .write = ksb_fs_write,
381 .open = ksb_fs_open,
382 .release = ksb_fs_release,
383};
384
385static struct miscdevice ksb_fboot_dev = {
386 .minor = MISC_DYNAMIC_MINOR,
387 .name = "ks_bridge",
388 .fops = &ksb_fops,
389};
390
391static const struct file_operations efs_fops = {
392 .owner = THIS_MODULE,
393 .read = ksb_fs_read,
394 .write = ksb_fs_write,
395 .open = efs_fs_open,
396 .release = ksb_fs_release,
397};
398
399static struct miscdevice ksb_efs_dev = {
400 .minor = MISC_DYNAMIC_MINOR,
401 .name = "efs_bridge",
402 .fops = &efs_fops,
403};
404
405static const struct usb_device_id ksb_usb_ids[] = {
406 { USB_DEVICE(0x5c6, 0x9008),
407 .driver_info = (unsigned long)&ksb_fboot_dev, },
408 { USB_DEVICE(0x5c6, 0x9048),
409 .driver_info = (unsigned long)&ksb_efs_dev, },
410 { USB_DEVICE(0x5c6, 0x904C),
411 .driver_info = (unsigned long)&ksb_efs_dev, },
Hemant Kumarce3c5bf2012-12-06 15:52:02 -0800412 { USB_DEVICE(0x5c6, 0x9075),
413 .driver_info = (unsigned long)&ksb_efs_dev, },
Vamsi Krishna5b944712012-06-29 18:36:23 -0700414
415 {} /* terminating entry */
416};
417MODULE_DEVICE_TABLE(usb, ksb_usb_ids);
418
419static void ksb_rx_cb(struct urb *urb);
Pavankumar Kondeti5c915c12012-12-14 15:12:46 +0530420static void
421submit_one_urb(struct ks_bridge *ksb, gfp_t flags, struct data_pkt *pkt)
Vamsi Krishna5b944712012-06-29 18:36:23 -0700422{
Vamsi Krishna5b944712012-06-29 18:36:23 -0700423 struct urb *urb;
424 int ret;
425
Pavankumar Kondeti5c915c12012-12-14 15:12:46 +0530426 urb = usb_alloc_urb(0, flags);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700427 if (!urb) {
428 pr_err("unable to allocate urb");
429 ksb_free_data_pkt(pkt);
430 return;
431 }
Vamsi Krishna5b944712012-06-29 18:36:23 -0700432
433 usb_fill_bulk_urb(urb, ksb->udev, ksb->in_pipe,
434 pkt->buf, pkt->len,
435 ksb_rx_cb, pkt);
436 usb_anchor_urb(urb, &ksb->submitted);
437
Hemant Kumarb3779d12012-09-16 20:28:56 -0700438 if (!test_bit(USB_DEV_CONNECTED, &ksb->flags)) {
439 usb_unanchor_urb(urb);
440 usb_free_urb(urb);
441 ksb_free_data_pkt(pkt);
Hemant Kumarb3779d12012-09-16 20:28:56 -0700442 return;
443 }
Vamsi Krishna5b944712012-06-29 18:36:23 -0700444
Hemant Kumarb3779d12012-09-16 20:28:56 -0700445 atomic_inc(&ksb->rx_pending_cnt);
Pavankumar Kondeti5c915c12012-12-14 15:12:46 +0530446 ret = usb_submit_urb(urb, flags);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700447 if (ret) {
448 pr_err("in urb submission failed");
449 usb_unanchor_urb(urb);
450 usb_free_urb(urb);
451 ksb_free_data_pkt(pkt);
Hemant Kumarb3779d12012-09-16 20:28:56 -0700452 atomic_dec(&ksb->rx_pending_cnt);
453 wake_up(&ksb->pending_urb_wait);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700454 return;
455 }
456
Hemant Kumarb3779d12012-09-16 20:28:56 -0700457 dbg_log_event(ksb, "S RX_URB", pkt->len, 0);
458
Vamsi Krishna5b944712012-06-29 18:36:23 -0700459 usb_free_urb(urb);
460}
461static void ksb_rx_cb(struct urb *urb)
462{
463 struct data_pkt *pkt = urb->context;
464 struct ks_bridge *ksb = pkt->ctxt;
465
466 dbg_log_event(ksb, "C RX_URB", urb->status, urb->actual_length);
467
468 pr_debug("status:%d actual:%d", urb->status, urb->actual_length);
469
Hemant Kumar853ebf62012-08-31 18:38:32 -0700470 /*non zero len of data received while unlinking urb*/
471 if (urb->status == -ENOENT && urb->actual_length > 0)
472 goto add_to_list;
473
Vamsi Krishna5b944712012-06-29 18:36:23 -0700474 if (urb->status < 0) {
Hemant Kumar853ebf62012-08-31 18:38:32 -0700475 if (urb->status != -ESHUTDOWN && urb->status != -ENOENT
476 && urb->status != -EPROTO)
Vamsi Krishna5b944712012-06-29 18:36:23 -0700477 pr_err_ratelimited("urb failed with err:%d",
478 urb->status);
479 ksb_free_data_pkt(pkt);
Hemant Kumarb3779d12012-09-16 20:28:56 -0700480 goto done;
Vamsi Krishna5b944712012-06-29 18:36:23 -0700481 }
482
483 if (urb->actual_length == 0) {
Pavankumar Kondeti5c915c12012-12-14 15:12:46 +0530484 submit_one_urb(ksb, GFP_ATOMIC, pkt);
485 goto done;
Vamsi Krishna5b944712012-06-29 18:36:23 -0700486 }
487
Hemant Kumar853ebf62012-08-31 18:38:32 -0700488add_to_list:
Vamsi Krishna5b944712012-06-29 18:36:23 -0700489 spin_lock(&ksb->lock);
490 pkt->len = urb->actual_length;
491 list_add_tail(&pkt->list, &ksb->to_ks_list);
492 spin_unlock(&ksb->lock);
493
494 /* wake up read thread */
495 wake_up(&ksb->ks_wait_q);
496
Hemant Kumarb3779d12012-09-16 20:28:56 -0700497done:
498 atomic_dec(&ksb->rx_pending_cnt);
499 wake_up(&ksb->pending_urb_wait);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700500}
501
502static void ksb_start_rx_work(struct work_struct *w)
503{
504 struct ks_bridge *ksb =
505 container_of(w, struct ks_bridge, start_rx_work);
506 struct data_pkt *pkt;
507 struct urb *urb;
508 int i = 0;
509 int ret;
510
511 for (i = 0; i < NO_RX_REQS; i++) {
Hemant Kumarb3779d12012-09-16 20:28:56 -0700512
513 if (!test_bit(USB_DEV_CONNECTED, &ksb->flags))
514 return;
515
Vamsi Krishna5b944712012-06-29 18:36:23 -0700516 pkt = ksb_alloc_data_pkt(MAX_DATA_PKT_SIZE, GFP_KERNEL, ksb);
517 if (IS_ERR(pkt)) {
518 pr_err("unable to allocate data pkt");
519 return;
520 }
521
522 urb = usb_alloc_urb(0, GFP_KERNEL);
523 if (!urb) {
524 pr_err("unable to allocate urb");
525 ksb_free_data_pkt(pkt);
526 return;
527 }
528
529 ret = usb_autopm_get_interface(ksb->ifc);
530 if (ret < 0 && ret != -EAGAIN && ret != -EACCES) {
531 pr_err_ratelimited("autopm_get failed:%d", ret);
532 usb_free_urb(urb);
533 ksb_free_data_pkt(pkt);
534 return;
535 }
Vamsi Krishna5b944712012-06-29 18:36:23 -0700536
537 usb_fill_bulk_urb(urb, ksb->udev, ksb->in_pipe,
538 pkt->buf, pkt->len,
539 ksb_rx_cb, pkt);
540 usb_anchor_urb(urb, &ksb->submitted);
541
542 dbg_log_event(ksb, "S RX_URB", pkt->len, 0);
543
Hemant Kumarb3779d12012-09-16 20:28:56 -0700544 atomic_inc(&ksb->rx_pending_cnt);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700545 ret = usb_submit_urb(urb, GFP_KERNEL);
546 if (ret) {
547 pr_err("in urb submission failed");
548 usb_unanchor_urb(urb);
549 usb_free_urb(urb);
550 ksb_free_data_pkt(pkt);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700551 usb_autopm_put_interface(ksb->ifc);
Hemant Kumarb3779d12012-09-16 20:28:56 -0700552 atomic_dec(&ksb->rx_pending_cnt);
553 wake_up(&ksb->pending_urb_wait);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700554 return;
555 }
556
557 usb_autopm_put_interface_async(ksb->ifc);
558 usb_free_urb(urb);
559 }
560}
561
562static int
563ksb_usb_probe(struct usb_interface *ifc, const struct usb_device_id *id)
564{
565 __u8 ifc_num;
566 struct usb_host_interface *ifc_desc;
567 struct usb_endpoint_descriptor *ep_desc;
568 int i;
569 struct ks_bridge *ksb;
Hemant Kumar2f20bda2012-09-17 14:05:54 -0700570 unsigned long flags;
571 struct data_pkt *pkt;
Vamsi Krishna5b944712012-06-29 18:36:23 -0700572
573 ifc_num = ifc->cur_altsetting->desc.bInterfaceNumber;
574
575 switch (id->idProduct) {
576 case 0x9008:
577 if (ifc_num != 0)
578 return -ENODEV;
579 ksb = __ksb[BOOT_BRIDGE_INDEX];
580 break;
581 case 0x9048:
582 case 0x904C:
583 if (ifc_num != 2)
584 return -ENODEV;
585 ksb = __ksb[EFS_BRIDGE_INDEX];
586 break;
587 default:
588 return -ENODEV;
589 }
590
591 if (!ksb) {
592 pr_err("ksb is not initialized");
593 return -ENODEV;
594 }
595
596 ksb->udev = usb_get_dev(interface_to_usbdev(ifc));
597 ksb->ifc = ifc;
598 ifc_desc = ifc->cur_altsetting;
599
600 for (i = 0; i < ifc_desc->desc.bNumEndpoints; i++) {
601 ep_desc = &ifc_desc->endpoint[i].desc;
602
603 if (!ksb->in_epAddr && usb_endpoint_is_bulk_in(ep_desc))
604 ksb->in_epAddr = ep_desc->bEndpointAddress;
605
606 if (!ksb->out_epAddr && usb_endpoint_is_bulk_out(ep_desc))
607 ksb->out_epAddr = ep_desc->bEndpointAddress;
608 }
609
610 if (!(ksb->in_epAddr && ksb->out_epAddr)) {
611 pr_err("could not find bulk in and bulk out endpoints");
612 usb_put_dev(ksb->udev);
613 ksb->ifc = NULL;
614 return -ENODEV;
615 }
616
617 ksb->in_pipe = usb_rcvbulkpipe(ksb->udev, ksb->in_epAddr);
618 ksb->out_pipe = usb_sndbulkpipe(ksb->udev, ksb->out_epAddr);
619
620 usb_set_intfdata(ifc, ksb);
621 set_bit(USB_DEV_CONNECTED, &ksb->flags);
Hemant Kumarb3779d12012-09-16 20:28:56 -0700622 atomic_set(&ksb->tx_pending_cnt, 0);
623 atomic_set(&ksb->rx_pending_cnt, 0);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700624
625 dbg_log_event(ksb, "PID-ATT", id->idProduct, 0);
626
Hemant Kumar2f20bda2012-09-17 14:05:54 -0700627 /*free up stale buffers if any from previous disconnect*/
628 spin_lock_irqsave(&ksb->lock, flags);
629 while (!list_empty(&ksb->to_ks_list)) {
630 pkt = list_first_entry(&ksb->to_ks_list,
631 struct data_pkt, list);
632 list_del_init(&pkt->list);
633 ksb_free_data_pkt(pkt);
Hemant Kumar2f20bda2012-09-17 14:05:54 -0700634 }
635 while (!list_empty(&ksb->to_mdm_list)) {
636 pkt = list_first_entry(&ksb->to_mdm_list,
637 struct data_pkt, list);
638 list_del_init(&pkt->list);
639 ksb_free_data_pkt(pkt);
640 }
641 spin_unlock_irqrestore(&ksb->lock, flags);
642
Hemant Kumarda86bd22012-08-24 19:42:31 -0700643 ksb->fs_dev = (struct miscdevice *)id->driver_info;
644 misc_register(ksb->fs_dev);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700645
Hemant Kumard26076c2012-09-07 19:00:21 -0700646 ifc->needs_remote_wakeup = 1;
Vamsi Krishna5b944712012-06-29 18:36:23 -0700647 usb_enable_autosuspend(ksb->udev);
648
649 pr_debug("usb dev connected");
650
651 return 0;
652}
653
654static int ksb_usb_suspend(struct usb_interface *ifc, pm_message_t message)
655{
656 struct ks_bridge *ksb = usb_get_intfdata(ifc);
657
658 dbg_log_event(ksb, "SUSPEND", 0, 0);
659
Vamsi Krishna5b944712012-06-29 18:36:23 -0700660 usb_kill_anchored_urbs(&ksb->submitted);
661
662 return 0;
663}
664
665static int ksb_usb_resume(struct usb_interface *ifc)
666{
667 struct ks_bridge *ksb = usb_get_intfdata(ifc);
668
669 dbg_log_event(ksb, "RESUME", 0, 0);
670
671 if (test_bit(FILE_OPENED, &ksb->flags))
672 queue_work(ksb->wq, &ksb->start_rx_work);
673
674 return 0;
675}
676
677static void ksb_usb_disconnect(struct usb_interface *ifc)
678{
679 struct ks_bridge *ksb = usb_get_intfdata(ifc);
680 unsigned long flags;
681 struct data_pkt *pkt;
682
683 dbg_log_event(ksb, "PID-DETACH", 0, 0);
684
685 clear_bit(USB_DEV_CONNECTED, &ksb->flags);
686 wake_up(&ksb->ks_wait_q);
687 cancel_work_sync(&ksb->to_mdm_work);
Hemant Kumarb3779d12012-09-16 20:28:56 -0700688 cancel_work_sync(&ksb->start_rx_work);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700689
Hemant Kumar2f20bda2012-09-17 14:05:54 -0700690 misc_deregister(ksb->fs_dev);
691
Vamsi Krishna5b944712012-06-29 18:36:23 -0700692 usb_kill_anchored_urbs(&ksb->submitted);
693
Hemant Kumarb3779d12012-09-16 20:28:56 -0700694 wait_event_interruptible_timeout(
695 ksb->pending_urb_wait,
696 !atomic_read(&ksb->tx_pending_cnt) &&
697 !atomic_read(&ksb->rx_pending_cnt),
698 msecs_to_jiffies(PENDING_URB_TIMEOUT));
699
Vamsi Krishna5b944712012-06-29 18:36:23 -0700700 spin_lock_irqsave(&ksb->lock, flags);
701 while (!list_empty(&ksb->to_ks_list)) {
702 pkt = list_first_entry(&ksb->to_ks_list,
703 struct data_pkt, list);
704 list_del_init(&pkt->list);
705 ksb_free_data_pkt(pkt);
706 }
707 while (!list_empty(&ksb->to_mdm_list)) {
708 pkt = list_first_entry(&ksb->to_mdm_list,
709 struct data_pkt, list);
710 list_del_init(&pkt->list);
711 ksb_free_data_pkt(pkt);
712 }
713 spin_unlock_irqrestore(&ksb->lock, flags);
714
Hemant Kumard26076c2012-09-07 19:00:21 -0700715 ifc->needs_remote_wakeup = 0;
Vamsi Krishna5b944712012-06-29 18:36:23 -0700716 usb_put_dev(ksb->udev);
717 ksb->ifc = NULL;
718 usb_set_intfdata(ifc, NULL);
719
720 return;
721}
722
723static struct usb_driver ksb_usb_driver = {
724 .name = "ks_bridge",
725 .probe = ksb_usb_probe,
726 .disconnect = ksb_usb_disconnect,
727 .suspend = ksb_usb_suspend,
728 .resume = ksb_usb_resume,
729 .id_table = ksb_usb_ids,
730 .supports_autosuspend = 1,
731};
732
733static ssize_t ksb_debug_show(struct seq_file *s, void *unused)
734{
735 unsigned long flags;
736 struct ks_bridge *ksb = s->private;
737 int i;
738
739 read_lock_irqsave(&ksb->dbg_lock, flags);
740 for (i = 0; i < DBG_MAX_MSG; i++) {
741 if (i == (ksb->dbg_idx - 1))
742 seq_printf(s, "-->%s\n", ksb->dbgbuf[i]);
743 else
744 seq_printf(s, "%s\n", ksb->dbgbuf[i]);
745 }
746 read_unlock_irqrestore(&ksb->dbg_lock, flags);
747
748 return 0;
749}
750
751static int ksb_debug_open(struct inode *ip, struct file *fp)
752{
753 return single_open(fp, ksb_debug_show, ip->i_private);
754
755 return 0;
756}
757
758static const struct file_operations dbg_fops = {
759 .open = ksb_debug_open,
760 .read = seq_read,
761 .llseek = seq_lseek,
762 .release = single_release,
763};
764static struct dentry *dbg_dir;
765static int __init ksb_init(void)
766{
767 struct ks_bridge *ksb;
768 int num_instances = 0;
769 int ret = 0;
770 int i;
771
772 dbg_dir = debugfs_create_dir("ks_bridge", NULL);
773 if (IS_ERR(dbg_dir))
774 pr_err("unable to create debug dir");
775
776 for (i = 0; i < NO_BRIDGE_INSTANCES; i++) {
777 ksb = kzalloc(sizeof(struct ks_bridge), GFP_KERNEL);
778 if (!ksb) {
779 pr_err("unable to allocat mem for ks_bridge");
Hemant Kumarda86bd22012-08-24 19:42:31 -0700780 ret = -ENOMEM;
781 goto dev_free;
Vamsi Krishna5b944712012-06-29 18:36:23 -0700782 }
783 __ksb[i] = ksb;
784
785 ksb->name = kasprintf(GFP_KERNEL, "ks_bridge:%i", i + 1);
786 if (!ksb->name) {
787 pr_info("unable to allocate name");
788 kfree(ksb);
789 ret = -ENOMEM;
790 goto dev_free;
791 }
792
793 spin_lock_init(&ksb->lock);
794 INIT_LIST_HEAD(&ksb->to_mdm_list);
795 INIT_LIST_HEAD(&ksb->to_ks_list);
796 init_waitqueue_head(&ksb->ks_wait_q);
Hemant Kumarb3779d12012-09-16 20:28:56 -0700797 init_waitqueue_head(&ksb->pending_urb_wait);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700798 ksb->wq = create_singlethread_workqueue(ksb->name);
799 if (!ksb->wq) {
800 pr_err("unable to allocate workqueue");
801 kfree(ksb->name);
802 kfree(ksb);
803 ret = -ENOMEM;
804 goto dev_free;
805 }
806
807 INIT_WORK(&ksb->to_mdm_work, ksb_tomdm_work);
808 INIT_WORK(&ksb->start_rx_work, ksb_start_rx_work);
809 init_usb_anchor(&ksb->submitted);
810
811 ksb->dbg_idx = 0;
812 ksb->dbg_lock = __RW_LOCK_UNLOCKED(lck);
813
814 if (!IS_ERR(dbg_dir))
815 debugfs_create_file(ksb->name, S_IRUGO, dbg_dir,
816 ksb, &dbg_fops);
817
818 num_instances++;
819 }
820
821 ret = usb_register(&ksb_usb_driver);
822 if (ret) {
823 pr_err("unable to register ks bridge driver");
824 goto dev_free;
825 }
826
827 pr_info("init done");
828
829 return 0;
830
831dev_free:
832 if (!IS_ERR(dbg_dir))
833 debugfs_remove_recursive(dbg_dir);
834
835 for (i = 0; i < num_instances; i++) {
836 ksb = __ksb[i];
837
838 destroy_workqueue(ksb->wq);
839 kfree(ksb->name);
840 kfree(ksb);
841 }
842
843 return ret;
844
845}
846
847static void __exit ksb_exit(void)
848{
849 struct ks_bridge *ksb;
850 int i;
851
852 if (!IS_ERR(dbg_dir))
853 debugfs_remove_recursive(dbg_dir);
854
855 usb_deregister(&ksb_usb_driver);
856
857 for (i = 0; i < NO_BRIDGE_INSTANCES; i++) {
858 ksb = __ksb[i];
859
860 destroy_workqueue(ksb->wq);
861 kfree(ksb->name);
862 kfree(ksb);
863 }
864}
865
866module_init(ksb_init);
867module_exit(ksb_exit);
868
869MODULE_DESCRIPTION(DRIVER_DESC);
870MODULE_VERSION(DRIVER_VERSION);
871MODULE_LICENSE("GPL v2");