blob: 73e1e8085930a55565c9968ea9df59a72ee43dc7 [file] [log] [blame]
Vamsi Krishna5b944712012-06-29 18:36:23 -07001/*
Pavankumar Kondeti4fde7d42012-12-28 17:15:41 +05302 * Copyright (c) 2012-2013, Linux Foundation. All rights reserved.
Vamsi Krishna5b944712012-06-29 18:36:23 -07003 *
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
Hemant Kumar54aeb822013-01-16 18:55:14 -080036enum bus_id {
37 BUS_HSIC,
38 BUS_USB,
39 BUS_UNDEF,
40};
41
42#define BUSNAME_LEN 20
43
44static enum bus_id str_to_busid(const char *name)
45{
46 if (!strncasecmp("msm_hsic_host", name, BUSNAME_LEN))
47 return BUS_HSIC;
48 if (!strncasecmp("msm_ehci_host.0", name, BUSNAME_LEN))
49 return BUS_USB;
50
51 return BUS_UNDEF;
52}
53
Vamsi Krishna5b944712012-06-29 18:36:23 -070054struct data_pkt {
55 int n_read;
56 char *buf;
57 size_t len;
58 struct list_head list;
59 void *ctxt;
60};
61
62#define FILE_OPENED BIT(0)
63#define USB_DEV_CONNECTED BIT(1)
64#define NO_RX_REQS 10
Hemant Kumar54aeb822013-01-16 18:55:14 -080065#define NO_BRIDGE_INSTANCES 4
66#define EFS_HSIC_BRIDGE_INDEX 2
67#define EFS_USB_BRIDGE_INDEX 3
Vamsi Krishna5b944712012-06-29 18:36:23 -070068#define MAX_DATA_PKT_SIZE 16384
Hemant Kumarea4b0b02012-09-16 20:28:56 -070069#define PENDING_URB_TIMEOUT 10
Vamsi Krishna5b944712012-06-29 18:36:23 -070070
71struct ks_bridge {
72 char *name;
73 spinlock_t lock;
74 struct workqueue_struct *wq;
75 struct work_struct to_mdm_work;
76 struct work_struct start_rx_work;
77 struct list_head to_mdm_list;
78 struct list_head to_ks_list;
79 wait_queue_head_t ks_wait_q;
Hemant Kumar54aeb822013-01-16 18:55:14 -080080 struct miscdevice fs_dev;
Hemant Kumarea4b0b02012-09-16 20:28:56 -070081 wait_queue_head_t pending_urb_wait;
82 atomic_t tx_pending_cnt;
83 atomic_t rx_pending_cnt;
Vamsi Krishna5b944712012-06-29 18:36:23 -070084
85 /* usb specific */
86 struct usb_device *udev;
87 struct usb_interface *ifc;
88 __u8 in_epAddr;
89 __u8 out_epAddr;
90 unsigned int in_pipe;
91 unsigned int out_pipe;
92 struct usb_anchor submitted;
93
94 unsigned long flags;
Vamsi Krishna5b944712012-06-29 18:36:23 -070095
96#define DBG_MSG_LEN 40
97#define DBG_MAX_MSG 500
98 unsigned int dbg_idx;
99 rwlock_t dbg_lock;
100 char (dbgbuf[DBG_MAX_MSG])[DBG_MSG_LEN]; /* buffer */
101};
102struct ks_bridge *__ksb[NO_BRIDGE_INSTANCES];
103
104/* by default debugging is enabled */
105static unsigned int enable_dbg = 1;
106module_param(enable_dbg, uint, S_IRUGO | S_IWUSR);
107
108static void
109dbg_log_event(struct ks_bridge *ksb, char *event, int d1, int d2)
110{
111 unsigned long flags;
112 unsigned long long t;
113 unsigned long nanosec;
114
115 if (!enable_dbg)
116 return;
117
118 write_lock_irqsave(&ksb->dbg_lock, flags);
119 t = cpu_clock(smp_processor_id());
120 nanosec = do_div(t, 1000000000)/1000;
121 scnprintf(ksb->dbgbuf[ksb->dbg_idx], DBG_MSG_LEN, "%5lu.%06lu:%s:%x:%x",
122 (unsigned long)t, nanosec, event, d1, d2);
123
124 ksb->dbg_idx++;
125 ksb->dbg_idx = ksb->dbg_idx % DBG_MAX_MSG;
126 write_unlock_irqrestore(&ksb->dbg_lock, flags);
127}
128
129static
130struct data_pkt *ksb_alloc_data_pkt(size_t count, gfp_t flags, void *ctxt)
131{
132 struct data_pkt *pkt;
133
134 pkt = kzalloc(sizeof(struct data_pkt), flags);
135 if (!pkt) {
136 pr_err("failed to allocate data packet\n");
137 return ERR_PTR(-ENOMEM);
138 }
139
140 pkt->buf = kmalloc(count, flags);
141 if (!pkt->buf) {
142 pr_err("failed to allocate data buffer\n");
143 kfree(pkt);
144 return ERR_PTR(-ENOMEM);
145 }
146
147 pkt->len = count;
148 INIT_LIST_HEAD(&pkt->list);
149 pkt->ctxt = ctxt;
150
151 return pkt;
152}
153
154static void ksb_free_data_pkt(struct data_pkt *pkt)
155{
156 kfree(pkt->buf);
157 kfree(pkt);
158}
159
160
Pavankumar Kondeti009bbd62012-12-14 15:12:46 +0530161static void
162submit_one_urb(struct ks_bridge *ksb, gfp_t flags, struct data_pkt *pkt);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700163static ssize_t ksb_fs_read(struct file *fp, char __user *buf,
164 size_t count, loff_t *pos)
165{
166 int ret;
167 unsigned long flags;
168 struct ks_bridge *ksb = fp->private_data;
Pavankumar Kondeti8ccaa1d2013-02-02 09:42:33 +0530169 struct data_pkt *pkt = NULL;
Vamsi Krishna5b944712012-06-29 18:36:23 -0700170 size_t space, copied;
171
172read_start:
173 if (!test_bit(USB_DEV_CONNECTED, &ksb->flags))
174 return -ENODEV;
175
176 spin_lock_irqsave(&ksb->lock, flags);
177 if (list_empty(&ksb->to_ks_list)) {
178 spin_unlock_irqrestore(&ksb->lock, flags);
179 ret = wait_event_interruptible(ksb->ks_wait_q,
180 !list_empty(&ksb->to_ks_list) ||
181 !test_bit(USB_DEV_CONNECTED, &ksb->flags));
182 if (ret < 0)
183 return ret;
184
185 goto read_start;
186 }
187
188 space = count;
189 copied = 0;
Pavankumar Kondeti8ccaa1d2013-02-02 09:42:33 +0530190 while (!list_empty(&ksb->to_ks_list) && space &&
191 test_bit(USB_DEV_CONNECTED, &ksb->flags)) {
Vamsi Krishna5b944712012-06-29 18:36:23 -0700192 size_t len;
193
194 pkt = list_first_entry(&ksb->to_ks_list, struct data_pkt, list);
Pavankumar Kondeti8ccaa1d2013-02-02 09:42:33 +0530195 list_del_init(&pkt->list);
Hemant Kumard45f96c2012-09-24 12:32:32 -0700196 len = min_t(size_t, space, pkt->len - pkt->n_read);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700197 spin_unlock_irqrestore(&ksb->lock, flags);
198
Hemant Kumard45f96c2012-09-24 12:32:32 -0700199 ret = copy_to_user(buf + copied, pkt->buf + pkt->n_read, len);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700200 if (ret) {
Hemant Kumar54aeb822013-01-16 18:55:14 -0800201 dev_err(ksb->fs_dev.this_device,
202 "copy_to_user failed err:%d\n", ret);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700203 ksb_free_data_pkt(pkt);
Pavankumar Kondeti8ccaa1d2013-02-02 09:42:33 +0530204 return -EFAULT;
Vamsi Krishna5b944712012-06-29 18:36:23 -0700205 }
206
Hemant Kumard45f96c2012-09-24 12:32:32 -0700207 pkt->n_read += len;
Vamsi Krishna5b944712012-06-29 18:36:23 -0700208 space -= len;
209 copied += len;
210
Vamsi Krishna5b944712012-06-29 18:36:23 -0700211 if (pkt->n_read == pkt->len) {
Pavankumar Kondeti009bbd62012-12-14 15:12:46 +0530212 /*
213 * re-init the packet and queue it
214 * for more data.
215 */
Pavankumar Kondeti009bbd62012-12-14 15:12:46 +0530216 pkt->n_read = 0;
217 pkt->len = MAX_DATA_PKT_SIZE;
Pavankumar Kondeti009bbd62012-12-14 15:12:46 +0530218 submit_one_urb(ksb, GFP_KERNEL, pkt);
Pavankumar Kondeti8ccaa1d2013-02-02 09:42:33 +0530219 pkt = NULL;
Vamsi Krishna5b944712012-06-29 18:36:23 -0700220 }
Pavankumar Kondeti8ccaa1d2013-02-02 09:42:33 +0530221 spin_lock_irqsave(&ksb->lock, flags);
222 }
223
224 /* put the partial packet back in the list */
225 if (!space && pkt && pkt->n_read != pkt->len) {
226 if (test_bit(USB_DEV_CONNECTED, &ksb->flags))
227 list_add(&pkt->list, &ksb->to_ks_list);
228 else
229 ksb_free_data_pkt(pkt);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700230 }
231 spin_unlock_irqrestore(&ksb->lock, flags);
232
233 dbg_log_event(ksb, "KS_READ", copied, 0);
234
Hemant Kumar54aeb822013-01-16 18:55:14 -0800235 dev_dbg(ksb->fs_dev.this_device, "count:%d space:%d copied:%d", count,
236 space, copied);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700237
238 return copied;
239}
240
241static void ksb_tx_cb(struct urb *urb)
242{
243 struct data_pkt *pkt = urb->context;
244 struct ks_bridge *ksb = pkt->ctxt;
245
246 dbg_log_event(ksb, "C TX_URB", urb->status, 0);
Hemant Kumar54aeb822013-01-16 18:55:14 -0800247 dev_dbg(&ksb->udev->dev, "status:%d", urb->status);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700248
Hemant Kumarea4b0b02012-09-16 20:28:56 -0700249 if (test_bit(USB_DEV_CONNECTED, &ksb->flags))
Vamsi Krishna5b944712012-06-29 18:36:23 -0700250 usb_autopm_put_interface_async(ksb->ifc);
251
252 if (urb->status < 0)
Hemant Kumar54aeb822013-01-16 18:55:14 -0800253 pr_err_ratelimited("%s: urb failed with err:%d",
254 ksb->fs_dev.name, urb->status);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700255
256 ksb_free_data_pkt(pkt);
Hemant Kumarea4b0b02012-09-16 20:28:56 -0700257
258 atomic_dec(&ksb->tx_pending_cnt);
259 wake_up(&ksb->pending_urb_wait);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700260}
261
262static void ksb_tomdm_work(struct work_struct *w)
263{
264 struct ks_bridge *ksb = container_of(w, struct ks_bridge, to_mdm_work);
265 struct data_pkt *pkt;
266 unsigned long flags;
267 struct urb *urb;
268 int ret;
269
270 spin_lock_irqsave(&ksb->lock, flags);
271 while (!list_empty(&ksb->to_mdm_list)
272 && test_bit(USB_DEV_CONNECTED, &ksb->flags)) {
273 pkt = list_first_entry(&ksb->to_mdm_list,
274 struct data_pkt, list);
275 list_del_init(&pkt->list);
276 spin_unlock_irqrestore(&ksb->lock, flags);
277
278 urb = usb_alloc_urb(0, GFP_KERNEL);
279 if (!urb) {
Hemant Kumar54aeb822013-01-16 18:55:14 -0800280 pr_err_ratelimited("%s: unable to allocate urb",
281 ksb->fs_dev.name);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700282 ksb_free_data_pkt(pkt);
283 return;
284 }
285
286 ret = usb_autopm_get_interface(ksb->ifc);
287 if (ret < 0 && ret != -EAGAIN && ret != -EACCES) {
Hemant Kumar54aeb822013-01-16 18:55:14 -0800288 pr_err_ratelimited("%s: autopm_get failed:%d",
289 ksb->fs_dev.name, ret);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700290 usb_free_urb(urb);
291 ksb_free_data_pkt(pkt);
292 return;
293 }
294 usb_fill_bulk_urb(urb, ksb->udev, ksb->out_pipe,
295 pkt->buf, pkt->len, ksb_tx_cb, pkt);
296 usb_anchor_urb(urb, &ksb->submitted);
297
298 dbg_log_event(ksb, "S TX_URB", pkt->len, 0);
299
Hemant Kumarea4b0b02012-09-16 20:28:56 -0700300 atomic_inc(&ksb->tx_pending_cnt);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700301 ret = usb_submit_urb(urb, GFP_KERNEL);
302 if (ret) {
Hemant Kumar54aeb822013-01-16 18:55:14 -0800303 dev_err(&ksb->udev->dev, "out urb submission failed");
Vamsi Krishna5b944712012-06-29 18:36:23 -0700304 usb_unanchor_urb(urb);
305 usb_free_urb(urb);
306 ksb_free_data_pkt(pkt);
307 usb_autopm_put_interface(ksb->ifc);
Hemant Kumarea4b0b02012-09-16 20:28:56 -0700308 atomic_dec(&ksb->tx_pending_cnt);
309 wake_up(&ksb->pending_urb_wait);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700310 return;
311 }
312
Hemant Kumar9b230b52012-09-19 12:40:11 -0700313 usb_free_urb(urb);
314
Vamsi Krishna5b944712012-06-29 18:36:23 -0700315 spin_lock_irqsave(&ksb->lock, flags);
316 }
317 spin_unlock_irqrestore(&ksb->lock, flags);
318}
319
320static ssize_t ksb_fs_write(struct file *fp, const char __user *buf,
321 size_t count, loff_t *pos)
322{
323 int ret;
324 struct data_pkt *pkt;
325 unsigned long flags;
326 struct ks_bridge *ksb = fp->private_data;
327
Hemant Kumarc999d432012-09-17 14:05:54 -0700328 if (!test_bit(USB_DEV_CONNECTED, &ksb->flags))
329 return -ENODEV;
Pavankumar Kondeti4fde7d42012-12-28 17:15:41 +0530330
331 if (count > MAX_DATA_PKT_SIZE)
332 count = MAX_DATA_PKT_SIZE;
333
Vamsi Krishna5b944712012-06-29 18:36:23 -0700334 pkt = ksb_alloc_data_pkt(count, GFP_KERNEL, ksb);
335 if (IS_ERR(pkt)) {
Hemant Kumar54aeb822013-01-16 18:55:14 -0800336 dev_err(ksb->fs_dev.this_device,
337 "unable to allocate data packet");
Vamsi Krishna5b944712012-06-29 18:36:23 -0700338 return PTR_ERR(pkt);
339 }
340
341 ret = copy_from_user(pkt->buf, buf, count);
342 if (ret) {
Hemant Kumar54aeb822013-01-16 18:55:14 -0800343 dev_err(ksb->fs_dev.this_device,
344 "copy_from_user failed: err:%d", ret);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700345 ksb_free_data_pkt(pkt);
346 return ret;
347 }
348
349 spin_lock_irqsave(&ksb->lock, flags);
350 list_add_tail(&pkt->list, &ksb->to_mdm_list);
351 spin_unlock_irqrestore(&ksb->lock, flags);
352
353 queue_work(ksb->wq, &ksb->to_mdm_work);
354
355 return count;
356}
357
Vamsi Krishna5b944712012-06-29 18:36:23 -0700358static int ksb_fs_open(struct inode *ip, struct file *fp)
359{
Hemant Kumar54aeb822013-01-16 18:55:14 -0800360 struct miscdevice *mdev = fp->private_data;
361 struct ks_bridge *ksb = container_of(mdev, struct ks_bridge, fs_dev);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700362
Hemant Kumar54aeb822013-01-16 18:55:14 -0800363 if (IS_ERR(ksb)) {
364 pr_err("ksb device not found");
Vamsi Krishna5b944712012-06-29 18:36:23 -0700365 return -ENODEV;
366 }
367
Hemant Kumar54aeb822013-01-16 18:55:14 -0800368 dev_dbg(ksb->fs_dev.this_device, ":%s", ksb->fs_dev.name);
369 dbg_log_event(ksb, "FS-OPEN", 0, 0);
370
Vamsi Krishna5b944712012-06-29 18:36:23 -0700371 fp->private_data = ksb;
372 set_bit(FILE_OPENED, &ksb->flags);
373
374 if (test_bit(USB_DEV_CONNECTED, &ksb->flags))
375 queue_work(ksb->wq, &ksb->start_rx_work);
376
377 return 0;
378}
379
380static int ksb_fs_release(struct inode *ip, struct file *fp)
381{
382 struct ks_bridge *ksb = fp->private_data;
383
Hemant Kumar54aeb822013-01-16 18:55:14 -0800384 dev_dbg(ksb->fs_dev.this_device, ":%s", ksb->fs_dev.name);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700385 dbg_log_event(ksb, "FS-RELEASE", 0, 0);
386
387 clear_bit(FILE_OPENED, &ksb->flags);
388 fp->private_data = NULL;
389
390 return 0;
391}
392
393static const struct file_operations ksb_fops = {
394 .owner = THIS_MODULE,
395 .read = ksb_fs_read,
396 .write = ksb_fs_write,
397 .open = ksb_fs_open,
398 .release = ksb_fs_release,
399};
400
Hemant Kumar54aeb822013-01-16 18:55:14 -0800401static struct miscdevice ksb_fboot_dev[] = {
402 {
403 .minor = MISC_DYNAMIC_MINOR,
404 .name = "ks_hsic_bridge",
405 .fops = &ksb_fops,
406 },
407 {
408 .minor = MISC_DYNAMIC_MINOR,
409 .name = "ks_usb_bridge",
410 .fops = &ksb_fops,
411 },
Vamsi Krishna5b944712012-06-29 18:36:23 -0700412};
413
414static const struct file_operations efs_fops = {
415 .owner = THIS_MODULE,
416 .read = ksb_fs_read,
417 .write = ksb_fs_write,
Hemant Kumar54aeb822013-01-16 18:55:14 -0800418 .open = ksb_fs_open,
Vamsi Krishna5b944712012-06-29 18:36:23 -0700419 .release = ksb_fs_release,
420};
421
Hemant Kumar54aeb822013-01-16 18:55:14 -0800422static struct miscdevice ksb_efs_hsic_dev = {
Vamsi Krishna5b944712012-06-29 18:36:23 -0700423 .minor = MISC_DYNAMIC_MINOR,
Hemant Kumar54aeb822013-01-16 18:55:14 -0800424 .name = "efs_hsic_bridge",
Vamsi Krishna5b944712012-06-29 18:36:23 -0700425 .fops = &efs_fops,
426};
427
Hemant Kumar54aeb822013-01-16 18:55:14 -0800428static struct miscdevice ksb_efs_usb_dev = {
429 .minor = MISC_DYNAMIC_MINOR,
430 .name = "efs_usb_bridge",
431 .fops = &efs_fops,
432};
Vamsi Krishna5b944712012-06-29 18:36:23 -0700433static const struct usb_device_id ksb_usb_ids[] = {
434 { USB_DEVICE(0x5c6, 0x9008),
435 .driver_info = (unsigned long)&ksb_fboot_dev, },
436 { USB_DEVICE(0x5c6, 0x9048),
Hemant Kumar54aeb822013-01-16 18:55:14 -0800437 .driver_info = (unsigned long)&ksb_efs_hsic_dev, },
Vamsi Krishna5b944712012-06-29 18:36:23 -0700438 { USB_DEVICE(0x5c6, 0x904C),
Hemant Kumar54aeb822013-01-16 18:55:14 -0800439 .driver_info = (unsigned long)&ksb_efs_hsic_dev, },
440 { USB_DEVICE(0x5c6, 0x9079),
441 .driver_info = (unsigned long)&ksb_efs_usb_dev, },
Vamsi Krishna5b944712012-06-29 18:36:23 -0700442
443 {} /* terminating entry */
444};
445MODULE_DEVICE_TABLE(usb, ksb_usb_ids);
446
447static void ksb_rx_cb(struct urb *urb);
Pavankumar Kondeti009bbd62012-12-14 15:12:46 +0530448static void
449submit_one_urb(struct ks_bridge *ksb, gfp_t flags, struct data_pkt *pkt)
Vamsi Krishna5b944712012-06-29 18:36:23 -0700450{
Vamsi Krishna5b944712012-06-29 18:36:23 -0700451 struct urb *urb;
452 int ret;
453
Pavankumar Kondeti009bbd62012-12-14 15:12:46 +0530454 urb = usb_alloc_urb(0, flags);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700455 if (!urb) {
Hemant Kumar54aeb822013-01-16 18:55:14 -0800456 dev_err(&ksb->udev->dev, "unable to allocate urb");
Vamsi Krishna5b944712012-06-29 18:36:23 -0700457 ksb_free_data_pkt(pkt);
458 return;
459 }
Vamsi Krishna5b944712012-06-29 18:36:23 -0700460
461 usb_fill_bulk_urb(urb, ksb->udev, ksb->in_pipe,
462 pkt->buf, pkt->len,
463 ksb_rx_cb, pkt);
464 usb_anchor_urb(urb, &ksb->submitted);
465
Hemant Kumarea4b0b02012-09-16 20:28:56 -0700466 if (!test_bit(USB_DEV_CONNECTED, &ksb->flags)) {
Vamsi Krishna5b944712012-06-29 18:36:23 -0700467 usb_unanchor_urb(urb);
468 usb_free_urb(urb);
469 ksb_free_data_pkt(pkt);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700470 return;
471 }
472
Hemant Kumarea4b0b02012-09-16 20:28:56 -0700473 atomic_inc(&ksb->rx_pending_cnt);
474 ret = usb_submit_urb(urb, GFP_ATOMIC);
475 if (ret) {
476 dev_err(&ksb->udev->dev, "in urb submission failed");
477 usb_unanchor_urb(urb);
478 usb_free_urb(urb);
479 ksb_free_data_pkt(pkt);
480 atomic_dec(&ksb->rx_pending_cnt);
481 wake_up(&ksb->pending_urb_wait);
482 return;
483 }
484
485 dbg_log_event(ksb, "S RX_URB", pkt->len, 0);
486
Vamsi Krishna5b944712012-06-29 18:36:23 -0700487 usb_free_urb(urb);
488}
489static void ksb_rx_cb(struct urb *urb)
490{
491 struct data_pkt *pkt = urb->context;
492 struct ks_bridge *ksb = pkt->ctxt;
Pavankumar Kondetida0e9372013-01-30 16:17:16 +0530493 bool wakeup = true;
Vamsi Krishna5b944712012-06-29 18:36:23 -0700494
495 dbg_log_event(ksb, "C RX_URB", urb->status, urb->actual_length);
496
Hemant Kumar54aeb822013-01-16 18:55:14 -0800497 dev_dbg(&ksb->udev->dev, "status:%d actual:%d", urb->status,
498 urb->actual_length);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700499
Hemant Kumara6194c12012-08-31 18:38:32 -0700500 /*non zero len of data received while unlinking urb*/
Pavankumar Kondetida0e9372013-01-30 16:17:16 +0530501 if (urb->status == -ENOENT && (urb->actual_length > 0)) {
502 /*
503 * If we wakeup the reader process now, it may
504 * queue the URB before its reject flag gets
505 * cleared.
506 */
507 wakeup = false;
Hemant Kumara6194c12012-08-31 18:38:32 -0700508 goto add_to_list;
Pavankumar Kondetida0e9372013-01-30 16:17:16 +0530509 }
Hemant Kumara6194c12012-08-31 18:38:32 -0700510
Vamsi Krishna5b944712012-06-29 18:36:23 -0700511 if (urb->status < 0) {
Hemant Kumara6194c12012-08-31 18:38:32 -0700512 if (urb->status != -ESHUTDOWN && urb->status != -ENOENT
513 && urb->status != -EPROTO)
Hemant Kumar54aeb822013-01-16 18:55:14 -0800514 pr_err_ratelimited("%s: urb failed with err:%d",
515 ksb->fs_dev.name, urb->status);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700516 ksb_free_data_pkt(pkt);
Hemant Kumarea4b0b02012-09-16 20:28:56 -0700517 goto done;
Vamsi Krishna5b944712012-06-29 18:36:23 -0700518 }
519
520 if (urb->actual_length == 0) {
Pavankumar Kondeti009bbd62012-12-14 15:12:46 +0530521 submit_one_urb(ksb, GFP_ATOMIC, pkt);
Hemant Kumarea4b0b02012-09-16 20:28:56 -0700522 goto done;
Vamsi Krishna5b944712012-06-29 18:36:23 -0700523 }
524
Hemant Kumara6194c12012-08-31 18:38:32 -0700525add_to_list:
Vamsi Krishna5b944712012-06-29 18:36:23 -0700526 spin_lock(&ksb->lock);
527 pkt->len = urb->actual_length;
528 list_add_tail(&pkt->list, &ksb->to_ks_list);
529 spin_unlock(&ksb->lock);
530
531 /* wake up read thread */
Pavankumar Kondetida0e9372013-01-30 16:17:16 +0530532 if (wakeup)
533 wake_up(&ksb->ks_wait_q);
Hemant Kumarea4b0b02012-09-16 20:28:56 -0700534done:
535 atomic_dec(&ksb->rx_pending_cnt);
536 wake_up(&ksb->pending_urb_wait);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700537}
538
539static void ksb_start_rx_work(struct work_struct *w)
540{
541 struct ks_bridge *ksb =
542 container_of(w, struct ks_bridge, start_rx_work);
543 struct data_pkt *pkt;
544 struct urb *urb;
545 int i = 0;
546 int ret;
Pavankumar Kondeti76fe5292013-01-21 21:14:26 +0530547 bool put = true;
Vamsi Krishna5b944712012-06-29 18:36:23 -0700548
Pavankumar Kondeti76fe5292013-01-21 21:14:26 +0530549 ret = usb_autopm_get_interface(ksb->ifc);
550 if (ret < 0) {
551 if (ret != -EAGAIN && ret != -EACCES) {
552 pr_err_ratelimited("autopm_get failed:%d", ret);
553 return;
554 }
555 put = false;
556 }
Vamsi Krishna5b944712012-06-29 18:36:23 -0700557 for (i = 0; i < NO_RX_REQS; i++) {
Hemant Kumarea4b0b02012-09-16 20:28:56 -0700558
559 if (!test_bit(USB_DEV_CONNECTED, &ksb->flags))
560 return;
561
Vamsi Krishna5b944712012-06-29 18:36:23 -0700562 pkt = ksb_alloc_data_pkt(MAX_DATA_PKT_SIZE, GFP_KERNEL, ksb);
563 if (IS_ERR(pkt)) {
Hemant Kumar54aeb822013-01-16 18:55:14 -0800564 dev_err(&ksb->udev->dev, "unable to allocate data pkt");
Pavankumar Kondeti76fe5292013-01-21 21:14:26 +0530565 break;
Vamsi Krishna5b944712012-06-29 18:36:23 -0700566 }
567
568 urb = usb_alloc_urb(0, GFP_KERNEL);
569 if (!urb) {
Hemant Kumar54aeb822013-01-16 18:55:14 -0800570 dev_err(&ksb->udev->dev, "unable to allocate urb");
Vamsi Krishna5b944712012-06-29 18:36:23 -0700571 ksb_free_data_pkt(pkt);
Pavankumar Kondeti76fe5292013-01-21 21:14:26 +0530572 break;
Vamsi Krishna5b944712012-06-29 18:36:23 -0700573 }
Vamsi Krishna5b944712012-06-29 18:36:23 -0700574
575 usb_fill_bulk_urb(urb, ksb->udev, ksb->in_pipe,
576 pkt->buf, pkt->len,
577 ksb_rx_cb, pkt);
578 usb_anchor_urb(urb, &ksb->submitted);
579
580 dbg_log_event(ksb, "S RX_URB", pkt->len, 0);
581
Hemant Kumarea4b0b02012-09-16 20:28:56 -0700582 atomic_inc(&ksb->rx_pending_cnt);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700583 ret = usb_submit_urb(urb, GFP_KERNEL);
584 if (ret) {
Hemant Kumar54aeb822013-01-16 18:55:14 -0800585 dev_err(&ksb->udev->dev, "in urb submission failed");
Vamsi Krishna5b944712012-06-29 18:36:23 -0700586 usb_unanchor_urb(urb);
587 usb_free_urb(urb);
588 ksb_free_data_pkt(pkt);
Hemant Kumarea4b0b02012-09-16 20:28:56 -0700589 atomic_dec(&ksb->rx_pending_cnt);
590 wake_up(&ksb->pending_urb_wait);
Pavankumar Kondeti76fe5292013-01-21 21:14:26 +0530591 break;
Vamsi Krishna5b944712012-06-29 18:36:23 -0700592 }
593
Vamsi Krishna5b944712012-06-29 18:36:23 -0700594 usb_free_urb(urb);
595 }
Pavankumar Kondeti76fe5292013-01-21 21:14:26 +0530596 if (put)
597 usb_autopm_put_interface_async(ksb->ifc);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700598}
599
600static int
601ksb_usb_probe(struct usb_interface *ifc, const struct usb_device_id *id)
602{
603 __u8 ifc_num;
604 struct usb_host_interface *ifc_desc;
605 struct usb_endpoint_descriptor *ep_desc;
606 int i;
607 struct ks_bridge *ksb;
Hemant Kumar54aeb822013-01-16 18:55:14 -0800608 struct miscdevice *mdev, *fbdev;
609 struct usb_device *udev;
610 unsigned int bus_id;
Hemant Kumarc999d432012-09-17 14:05:54 -0700611 unsigned long flags;
612 struct data_pkt *pkt;
Vamsi Krishna5b944712012-06-29 18:36:23 -0700613
614 ifc_num = ifc->cur_altsetting->desc.bInterfaceNumber;
615
Hemant Kumar54aeb822013-01-16 18:55:14 -0800616 udev = interface_to_usbdev(ifc);
617 fbdev = mdev = (struct miscdevice *)id->driver_info;
618
619 bus_id = str_to_busid(udev->bus->bus_name);
620 if (bus_id == BUS_UNDEF) {
621 dev_err(&udev->dev, "unknown usb bus %s, probe failed\n",
622 udev->bus->bus_name);
623 return -ENODEV;
624 }
625
Vamsi Krishna5b944712012-06-29 18:36:23 -0700626 switch (id->idProduct) {
627 case 0x9008:
628 if (ifc_num != 0)
629 return -ENODEV;
Hemant Kumar54aeb822013-01-16 18:55:14 -0800630 ksb = __ksb[bus_id];
631 mdev = &fbdev[bus_id];
Vamsi Krishna5b944712012-06-29 18:36:23 -0700632 break;
633 case 0x9048:
634 case 0x904C:
635 if (ifc_num != 2)
636 return -ENODEV;
Hemant Kumar54aeb822013-01-16 18:55:14 -0800637 ksb = __ksb[EFS_HSIC_BRIDGE_INDEX];
638 break;
639 case 0x9079:
640 if (ifc_num != 2)
641 return -ENODEV;
642 ksb = __ksb[EFS_USB_BRIDGE_INDEX];
Vamsi Krishna5b944712012-06-29 18:36:23 -0700643 break;
644 default:
645 return -ENODEV;
646 }
647
648 if (!ksb) {
649 pr_err("ksb is not initialized");
650 return -ENODEV;
651 }
652
653 ksb->udev = usb_get_dev(interface_to_usbdev(ifc));
654 ksb->ifc = ifc;
655 ifc_desc = ifc->cur_altsetting;
656
657 for (i = 0; i < ifc_desc->desc.bNumEndpoints; i++) {
658 ep_desc = &ifc_desc->endpoint[i].desc;
659
660 if (!ksb->in_epAddr && usb_endpoint_is_bulk_in(ep_desc))
661 ksb->in_epAddr = ep_desc->bEndpointAddress;
662
663 if (!ksb->out_epAddr && usb_endpoint_is_bulk_out(ep_desc))
664 ksb->out_epAddr = ep_desc->bEndpointAddress;
665 }
666
667 if (!(ksb->in_epAddr && ksb->out_epAddr)) {
Hemant Kumar54aeb822013-01-16 18:55:14 -0800668 dev_err(&udev->dev,
669 "could not find bulk in and bulk out endpoints");
Vamsi Krishna5b944712012-06-29 18:36:23 -0700670 usb_put_dev(ksb->udev);
671 ksb->ifc = NULL;
672 return -ENODEV;
673 }
674
675 ksb->in_pipe = usb_rcvbulkpipe(ksb->udev, ksb->in_epAddr);
676 ksb->out_pipe = usb_sndbulkpipe(ksb->udev, ksb->out_epAddr);
677
678 usb_set_intfdata(ifc, ksb);
679 set_bit(USB_DEV_CONNECTED, &ksb->flags);
Hemant Kumarea4b0b02012-09-16 20:28:56 -0700680 atomic_set(&ksb->tx_pending_cnt, 0);
681 atomic_set(&ksb->rx_pending_cnt, 0);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700682
683 dbg_log_event(ksb, "PID-ATT", id->idProduct, 0);
684
Hemant Kumarc999d432012-09-17 14:05:54 -0700685 /*free up stale buffers if any from previous disconnect*/
686 spin_lock_irqsave(&ksb->lock, flags);
687 while (!list_empty(&ksb->to_ks_list)) {
688 pkt = list_first_entry(&ksb->to_ks_list,
689 struct data_pkt, list);
690 list_del_init(&pkt->list);
691 ksb_free_data_pkt(pkt);
692 }
693 while (!list_empty(&ksb->to_mdm_list)) {
694 pkt = list_first_entry(&ksb->to_mdm_list,
695 struct data_pkt, list);
696 list_del_init(&pkt->list);
697 ksb_free_data_pkt(pkt);
698 }
699 spin_unlock_irqrestore(&ksb->lock, flags);
700
Hemant Kumar54aeb822013-01-16 18:55:14 -0800701 ksb->fs_dev = *mdev;
702 misc_register(&ksb->fs_dev);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700703
Pavankumar Kondetidff38fb2013-02-05 15:38:03 +0530704 if (device_can_wakeup(&ksb->udev->dev)) {
705 ifc->needs_remote_wakeup = 1;
706 usb_enable_autosuspend(ksb->udev);
707 }
Vamsi Krishna5b944712012-06-29 18:36:23 -0700708
Hemant Kumar54aeb822013-01-16 18:55:14 -0800709 dev_dbg(&udev->dev, "usb dev connected");
Vamsi Krishna5b944712012-06-29 18:36:23 -0700710
711 return 0;
712}
713
714static int ksb_usb_suspend(struct usb_interface *ifc, pm_message_t message)
715{
716 struct ks_bridge *ksb = usb_get_intfdata(ifc);
Pavankumar Kondetida0e9372013-01-30 16:17:16 +0530717 unsigned long flags;
Vamsi Krishna5b944712012-06-29 18:36:23 -0700718
719 dbg_log_event(ksb, "SUSPEND", 0, 0);
720
Vamsi Krishna5b944712012-06-29 18:36:23 -0700721 usb_kill_anchored_urbs(&ksb->submitted);
722
Pavankumar Kondetida0e9372013-01-30 16:17:16 +0530723 spin_lock_irqsave(&ksb->lock, flags);
724 if (!list_empty(&ksb->to_ks_list)) {
725 spin_unlock_irqrestore(&ksb->lock, flags);
726 dbg_log_event(ksb, "SUSPEND ABORT", 0, 0);
727 /*
728 * Now wakeup the reader process and queue
729 * Rx URBs for more data.
730 */
731 wake_up(&ksb->ks_wait_q);
732 queue_work(ksb->wq, &ksb->start_rx_work);
733 return -EBUSY;
734 }
735 spin_unlock_irqrestore(&ksb->lock, flags);
736
Vamsi Krishna5b944712012-06-29 18:36:23 -0700737 return 0;
738}
739
740static int ksb_usb_resume(struct usb_interface *ifc)
741{
742 struct ks_bridge *ksb = usb_get_intfdata(ifc);
743
744 dbg_log_event(ksb, "RESUME", 0, 0);
745
746 if (test_bit(FILE_OPENED, &ksb->flags))
747 queue_work(ksb->wq, &ksb->start_rx_work);
748
749 return 0;
750}
751
752static void ksb_usb_disconnect(struct usb_interface *ifc)
753{
754 struct ks_bridge *ksb = usb_get_intfdata(ifc);
755 unsigned long flags;
756 struct data_pkt *pkt;
757
758 dbg_log_event(ksb, "PID-DETACH", 0, 0);
759
760 clear_bit(USB_DEV_CONNECTED, &ksb->flags);
761 wake_up(&ksb->ks_wait_q);
762 cancel_work_sync(&ksb->to_mdm_work);
Hemant Kumarea4b0b02012-09-16 20:28:56 -0700763 cancel_work_sync(&ksb->start_rx_work);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700764
Hemant Kumar54aeb822013-01-16 18:55:14 -0800765 misc_deregister(&ksb->fs_dev);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700766
Hemant Kumar38980ec42012-08-24 19:42:31 -0700767 usb_kill_anchored_urbs(&ksb->submitted);
Hemant Kumarea4b0b02012-09-16 20:28:56 -0700768
769 wait_event_interruptible_timeout(
770 ksb->pending_urb_wait,
771 !atomic_read(&ksb->tx_pending_cnt) &&
772 !atomic_read(&ksb->rx_pending_cnt),
773 msecs_to_jiffies(PENDING_URB_TIMEOUT));
774
Vamsi Krishna5b944712012-06-29 18:36:23 -0700775 spin_lock_irqsave(&ksb->lock, flags);
776 while (!list_empty(&ksb->to_ks_list)) {
777 pkt = list_first_entry(&ksb->to_ks_list,
778 struct data_pkt, list);
779 list_del_init(&pkt->list);
780 ksb_free_data_pkt(pkt);
781 }
782 while (!list_empty(&ksb->to_mdm_list)) {
783 pkt = list_first_entry(&ksb->to_mdm_list,
784 struct data_pkt, list);
785 list_del_init(&pkt->list);
786 ksb_free_data_pkt(pkt);
787 }
788 spin_unlock_irqrestore(&ksb->lock, flags);
789
Hemant Kumarf292a322012-09-07 19:00:21 -0700790 ifc->needs_remote_wakeup = 0;
Vamsi Krishna5b944712012-06-29 18:36:23 -0700791 usb_put_dev(ksb->udev);
792 ksb->ifc = NULL;
793 usb_set_intfdata(ifc, NULL);
794
795 return;
796}
797
798static struct usb_driver ksb_usb_driver = {
799 .name = "ks_bridge",
800 .probe = ksb_usb_probe,
801 .disconnect = ksb_usb_disconnect,
802 .suspend = ksb_usb_suspend,
803 .resume = ksb_usb_resume,
804 .id_table = ksb_usb_ids,
805 .supports_autosuspend = 1,
806};
807
808static ssize_t ksb_debug_show(struct seq_file *s, void *unused)
809{
810 unsigned long flags;
811 struct ks_bridge *ksb = s->private;
812 int i;
813
814 read_lock_irqsave(&ksb->dbg_lock, flags);
815 for (i = 0; i < DBG_MAX_MSG; i++) {
816 if (i == (ksb->dbg_idx - 1))
817 seq_printf(s, "-->%s\n", ksb->dbgbuf[i]);
818 else
819 seq_printf(s, "%s\n", ksb->dbgbuf[i]);
820 }
821 read_unlock_irqrestore(&ksb->dbg_lock, flags);
822
823 return 0;
824}
825
826static int ksb_debug_open(struct inode *ip, struct file *fp)
827{
828 return single_open(fp, ksb_debug_show, ip->i_private);
829
830 return 0;
831}
832
833static const struct file_operations dbg_fops = {
834 .open = ksb_debug_open,
835 .read = seq_read,
836 .llseek = seq_lseek,
837 .release = single_release,
838};
839static struct dentry *dbg_dir;
840static int __init ksb_init(void)
841{
842 struct ks_bridge *ksb;
843 int num_instances = 0;
844 int ret = 0;
845 int i;
846
847 dbg_dir = debugfs_create_dir("ks_bridge", NULL);
848 if (IS_ERR(dbg_dir))
849 pr_err("unable to create debug dir");
850
851 for (i = 0; i < NO_BRIDGE_INSTANCES; i++) {
852 ksb = kzalloc(sizeof(struct ks_bridge), GFP_KERNEL);
853 if (!ksb) {
854 pr_err("unable to allocat mem for ks_bridge");
Hemant Kumar38980ec42012-08-24 19:42:31 -0700855 ret = -ENOMEM;
856 goto dev_free;
Vamsi Krishna5b944712012-06-29 18:36:23 -0700857 }
858 __ksb[i] = ksb;
859
860 ksb->name = kasprintf(GFP_KERNEL, "ks_bridge:%i", i + 1);
861 if (!ksb->name) {
862 pr_info("unable to allocate name");
863 kfree(ksb);
864 ret = -ENOMEM;
865 goto dev_free;
866 }
867
868 spin_lock_init(&ksb->lock);
869 INIT_LIST_HEAD(&ksb->to_mdm_list);
870 INIT_LIST_HEAD(&ksb->to_ks_list);
871 init_waitqueue_head(&ksb->ks_wait_q);
Hemant Kumarea4b0b02012-09-16 20:28:56 -0700872 init_waitqueue_head(&ksb->pending_urb_wait);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700873 ksb->wq = create_singlethread_workqueue(ksb->name);
874 if (!ksb->wq) {
875 pr_err("unable to allocate workqueue");
876 kfree(ksb->name);
877 kfree(ksb);
878 ret = -ENOMEM;
879 goto dev_free;
880 }
881
882 INIT_WORK(&ksb->to_mdm_work, ksb_tomdm_work);
883 INIT_WORK(&ksb->start_rx_work, ksb_start_rx_work);
884 init_usb_anchor(&ksb->submitted);
885
886 ksb->dbg_idx = 0;
887 ksb->dbg_lock = __RW_LOCK_UNLOCKED(lck);
888
889 if (!IS_ERR(dbg_dir))
890 debugfs_create_file(ksb->name, S_IRUGO, dbg_dir,
891 ksb, &dbg_fops);
892
893 num_instances++;
894 }
895
896 ret = usb_register(&ksb_usb_driver);
897 if (ret) {
898 pr_err("unable to register ks bridge driver");
899 goto dev_free;
900 }
901
902 pr_info("init done");
903
904 return 0;
905
906dev_free:
907 if (!IS_ERR(dbg_dir))
908 debugfs_remove_recursive(dbg_dir);
909
910 for (i = 0; i < num_instances; i++) {
911 ksb = __ksb[i];
912
913 destroy_workqueue(ksb->wq);
914 kfree(ksb->name);
915 kfree(ksb);
916 }
917
918 return ret;
919
920}
921
922static void __exit ksb_exit(void)
923{
924 struct ks_bridge *ksb;
925 int i;
926
927 if (!IS_ERR(dbg_dir))
928 debugfs_remove_recursive(dbg_dir);
929
930 usb_deregister(&ksb_usb_driver);
931
932 for (i = 0; i < NO_BRIDGE_INSTANCES; i++) {
933 ksb = __ksb[i];
934
935 destroy_workqueue(ksb->wq);
936 kfree(ksb->name);
937 kfree(ksb);
938 }
939}
940
941module_init(ksb_init);
942module_exit(ksb_exit);
943
944MODULE_DESCRIPTION(DRIVER_DESC);
945MODULE_VERSION(DRIVER_VERSION);
946MODULE_LICENSE("GPL v2");