blob: 33280db9c4d9a1c9d2e529c719c2282432f667ad [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;
493
494 dbg_log_event(ksb, "C RX_URB", urb->status, urb->actual_length);
495
Hemant Kumar54aeb822013-01-16 18:55:14 -0800496 dev_dbg(&ksb->udev->dev, "status:%d actual:%d", urb->status,
497 urb->actual_length);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700498
Hemant Kumara6194c12012-08-31 18:38:32 -0700499 /*non zero len of data received while unlinking urb*/
500 if (urb->status == -ENOENT && urb->actual_length > 0)
501 goto add_to_list;
502
Vamsi Krishna5b944712012-06-29 18:36:23 -0700503 if (urb->status < 0) {
Hemant Kumara6194c12012-08-31 18:38:32 -0700504 if (urb->status != -ESHUTDOWN && urb->status != -ENOENT
505 && urb->status != -EPROTO)
Hemant Kumar54aeb822013-01-16 18:55:14 -0800506 pr_err_ratelimited("%s: urb failed with err:%d",
507 ksb->fs_dev.name, urb->status);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700508 ksb_free_data_pkt(pkt);
Hemant Kumarea4b0b02012-09-16 20:28:56 -0700509 goto done;
Vamsi Krishna5b944712012-06-29 18:36:23 -0700510 }
511
512 if (urb->actual_length == 0) {
Pavankumar Kondeti009bbd62012-12-14 15:12:46 +0530513 submit_one_urb(ksb, GFP_ATOMIC, pkt);
Hemant Kumarea4b0b02012-09-16 20:28:56 -0700514 goto done;
Vamsi Krishna5b944712012-06-29 18:36:23 -0700515 }
516
Hemant Kumara6194c12012-08-31 18:38:32 -0700517add_to_list:
Vamsi Krishna5b944712012-06-29 18:36:23 -0700518 spin_lock(&ksb->lock);
519 pkt->len = urb->actual_length;
520 list_add_tail(&pkt->list, &ksb->to_ks_list);
521 spin_unlock(&ksb->lock);
522
523 /* wake up read thread */
524 wake_up(&ksb->ks_wait_q);
Hemant Kumarea4b0b02012-09-16 20:28:56 -0700525done:
526 atomic_dec(&ksb->rx_pending_cnt);
527 wake_up(&ksb->pending_urb_wait);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700528}
529
530static void ksb_start_rx_work(struct work_struct *w)
531{
532 struct ks_bridge *ksb =
533 container_of(w, struct ks_bridge, start_rx_work);
534 struct data_pkt *pkt;
535 struct urb *urb;
536 int i = 0;
537 int ret;
Pavankumar Kondeti76fe5292013-01-21 21:14:26 +0530538 bool put = true;
Vamsi Krishna5b944712012-06-29 18:36:23 -0700539
Pavankumar Kondeti76fe5292013-01-21 21:14:26 +0530540 ret = usb_autopm_get_interface(ksb->ifc);
541 if (ret < 0) {
542 if (ret != -EAGAIN && ret != -EACCES) {
543 pr_err_ratelimited("autopm_get failed:%d", ret);
544 return;
545 }
546 put = false;
547 }
Vamsi Krishna5b944712012-06-29 18:36:23 -0700548 for (i = 0; i < NO_RX_REQS; i++) {
Hemant Kumarea4b0b02012-09-16 20:28:56 -0700549
550 if (!test_bit(USB_DEV_CONNECTED, &ksb->flags))
551 return;
552
Vamsi Krishna5b944712012-06-29 18:36:23 -0700553 pkt = ksb_alloc_data_pkt(MAX_DATA_PKT_SIZE, GFP_KERNEL, ksb);
554 if (IS_ERR(pkt)) {
Hemant Kumar54aeb822013-01-16 18:55:14 -0800555 dev_err(&ksb->udev->dev, "unable to allocate data pkt");
Pavankumar Kondeti76fe5292013-01-21 21:14:26 +0530556 break;
Vamsi Krishna5b944712012-06-29 18:36:23 -0700557 }
558
559 urb = usb_alloc_urb(0, GFP_KERNEL);
560 if (!urb) {
Hemant Kumar54aeb822013-01-16 18:55:14 -0800561 dev_err(&ksb->udev->dev, "unable to allocate urb");
Vamsi Krishna5b944712012-06-29 18:36:23 -0700562 ksb_free_data_pkt(pkt);
Pavankumar Kondeti76fe5292013-01-21 21:14:26 +0530563 break;
Vamsi Krishna5b944712012-06-29 18:36:23 -0700564 }
Vamsi Krishna5b944712012-06-29 18:36:23 -0700565
566 usb_fill_bulk_urb(urb, ksb->udev, ksb->in_pipe,
567 pkt->buf, pkt->len,
568 ksb_rx_cb, pkt);
569 usb_anchor_urb(urb, &ksb->submitted);
570
571 dbg_log_event(ksb, "S RX_URB", pkt->len, 0);
572
Hemant Kumarea4b0b02012-09-16 20:28:56 -0700573 atomic_inc(&ksb->rx_pending_cnt);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700574 ret = usb_submit_urb(urb, GFP_KERNEL);
575 if (ret) {
Hemant Kumar54aeb822013-01-16 18:55:14 -0800576 dev_err(&ksb->udev->dev, "in urb submission failed");
Vamsi Krishna5b944712012-06-29 18:36:23 -0700577 usb_unanchor_urb(urb);
578 usb_free_urb(urb);
579 ksb_free_data_pkt(pkt);
Hemant Kumarea4b0b02012-09-16 20:28:56 -0700580 atomic_dec(&ksb->rx_pending_cnt);
581 wake_up(&ksb->pending_urb_wait);
Pavankumar Kondeti76fe5292013-01-21 21:14:26 +0530582 break;
Vamsi Krishna5b944712012-06-29 18:36:23 -0700583 }
584
Vamsi Krishna5b944712012-06-29 18:36:23 -0700585 usb_free_urb(urb);
586 }
Pavankumar Kondeti76fe5292013-01-21 21:14:26 +0530587 if (put)
588 usb_autopm_put_interface_async(ksb->ifc);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700589}
590
591static int
592ksb_usb_probe(struct usb_interface *ifc, const struct usb_device_id *id)
593{
594 __u8 ifc_num;
595 struct usb_host_interface *ifc_desc;
596 struct usb_endpoint_descriptor *ep_desc;
597 int i;
598 struct ks_bridge *ksb;
Hemant Kumar54aeb822013-01-16 18:55:14 -0800599 struct miscdevice *mdev, *fbdev;
600 struct usb_device *udev;
601 unsigned int bus_id;
Hemant Kumarc999d432012-09-17 14:05:54 -0700602 unsigned long flags;
603 struct data_pkt *pkt;
Vamsi Krishna5b944712012-06-29 18:36:23 -0700604
605 ifc_num = ifc->cur_altsetting->desc.bInterfaceNumber;
606
Hemant Kumar54aeb822013-01-16 18:55:14 -0800607 udev = interface_to_usbdev(ifc);
608 fbdev = mdev = (struct miscdevice *)id->driver_info;
609
610 bus_id = str_to_busid(udev->bus->bus_name);
611 if (bus_id == BUS_UNDEF) {
612 dev_err(&udev->dev, "unknown usb bus %s, probe failed\n",
613 udev->bus->bus_name);
614 return -ENODEV;
615 }
616
Vamsi Krishna5b944712012-06-29 18:36:23 -0700617 switch (id->idProduct) {
618 case 0x9008:
619 if (ifc_num != 0)
620 return -ENODEV;
Hemant Kumar54aeb822013-01-16 18:55:14 -0800621 ksb = __ksb[bus_id];
622 mdev = &fbdev[bus_id];
Vamsi Krishna5b944712012-06-29 18:36:23 -0700623 break;
624 case 0x9048:
625 case 0x904C:
626 if (ifc_num != 2)
627 return -ENODEV;
Hemant Kumar54aeb822013-01-16 18:55:14 -0800628 ksb = __ksb[EFS_HSIC_BRIDGE_INDEX];
629 break;
630 case 0x9079:
631 if (ifc_num != 2)
632 return -ENODEV;
633 ksb = __ksb[EFS_USB_BRIDGE_INDEX];
Vamsi Krishna5b944712012-06-29 18:36:23 -0700634 break;
635 default:
636 return -ENODEV;
637 }
638
639 if (!ksb) {
640 pr_err("ksb is not initialized");
641 return -ENODEV;
642 }
643
644 ksb->udev = usb_get_dev(interface_to_usbdev(ifc));
645 ksb->ifc = ifc;
646 ifc_desc = ifc->cur_altsetting;
647
648 for (i = 0; i < ifc_desc->desc.bNumEndpoints; i++) {
649 ep_desc = &ifc_desc->endpoint[i].desc;
650
651 if (!ksb->in_epAddr && usb_endpoint_is_bulk_in(ep_desc))
652 ksb->in_epAddr = ep_desc->bEndpointAddress;
653
654 if (!ksb->out_epAddr && usb_endpoint_is_bulk_out(ep_desc))
655 ksb->out_epAddr = ep_desc->bEndpointAddress;
656 }
657
658 if (!(ksb->in_epAddr && ksb->out_epAddr)) {
Hemant Kumar54aeb822013-01-16 18:55:14 -0800659 dev_err(&udev->dev,
660 "could not find bulk in and bulk out endpoints");
Vamsi Krishna5b944712012-06-29 18:36:23 -0700661 usb_put_dev(ksb->udev);
662 ksb->ifc = NULL;
663 return -ENODEV;
664 }
665
666 ksb->in_pipe = usb_rcvbulkpipe(ksb->udev, ksb->in_epAddr);
667 ksb->out_pipe = usb_sndbulkpipe(ksb->udev, ksb->out_epAddr);
668
669 usb_set_intfdata(ifc, ksb);
670 set_bit(USB_DEV_CONNECTED, &ksb->flags);
Hemant Kumarea4b0b02012-09-16 20:28:56 -0700671 atomic_set(&ksb->tx_pending_cnt, 0);
672 atomic_set(&ksb->rx_pending_cnt, 0);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700673
674 dbg_log_event(ksb, "PID-ATT", id->idProduct, 0);
675
Hemant Kumarc999d432012-09-17 14:05:54 -0700676 /*free up stale buffers if any from previous disconnect*/
677 spin_lock_irqsave(&ksb->lock, flags);
678 while (!list_empty(&ksb->to_ks_list)) {
679 pkt = list_first_entry(&ksb->to_ks_list,
680 struct data_pkt, list);
681 list_del_init(&pkt->list);
682 ksb_free_data_pkt(pkt);
683 }
684 while (!list_empty(&ksb->to_mdm_list)) {
685 pkt = list_first_entry(&ksb->to_mdm_list,
686 struct data_pkt, list);
687 list_del_init(&pkt->list);
688 ksb_free_data_pkt(pkt);
689 }
690 spin_unlock_irqrestore(&ksb->lock, flags);
691
Hemant Kumar54aeb822013-01-16 18:55:14 -0800692 ksb->fs_dev = *mdev;
693 misc_register(&ksb->fs_dev);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700694
Hemant Kumarf292a322012-09-07 19:00:21 -0700695 ifc->needs_remote_wakeup = 1;
Vamsi Krishna5b944712012-06-29 18:36:23 -0700696 usb_enable_autosuspend(ksb->udev);
697
Hemant Kumar54aeb822013-01-16 18:55:14 -0800698 dev_dbg(&udev->dev, "usb dev connected");
Vamsi Krishna5b944712012-06-29 18:36:23 -0700699
700 return 0;
701}
702
703static int ksb_usb_suspend(struct usb_interface *ifc, pm_message_t message)
704{
705 struct ks_bridge *ksb = usb_get_intfdata(ifc);
706
707 dbg_log_event(ksb, "SUSPEND", 0, 0);
708
Vamsi Krishna5b944712012-06-29 18:36:23 -0700709 usb_kill_anchored_urbs(&ksb->submitted);
710
711 return 0;
712}
713
714static int ksb_usb_resume(struct usb_interface *ifc)
715{
716 struct ks_bridge *ksb = usb_get_intfdata(ifc);
717
718 dbg_log_event(ksb, "RESUME", 0, 0);
719
720 if (test_bit(FILE_OPENED, &ksb->flags))
721 queue_work(ksb->wq, &ksb->start_rx_work);
722
723 return 0;
724}
725
726static void ksb_usb_disconnect(struct usb_interface *ifc)
727{
728 struct ks_bridge *ksb = usb_get_intfdata(ifc);
729 unsigned long flags;
730 struct data_pkt *pkt;
731
732 dbg_log_event(ksb, "PID-DETACH", 0, 0);
733
734 clear_bit(USB_DEV_CONNECTED, &ksb->flags);
735 wake_up(&ksb->ks_wait_q);
736 cancel_work_sync(&ksb->to_mdm_work);
Hemant Kumarea4b0b02012-09-16 20:28:56 -0700737 cancel_work_sync(&ksb->start_rx_work);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700738
Hemant Kumar54aeb822013-01-16 18:55:14 -0800739 misc_deregister(&ksb->fs_dev);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700740
Hemant Kumar38980ec42012-08-24 19:42:31 -0700741 usb_kill_anchored_urbs(&ksb->submitted);
Hemant Kumarea4b0b02012-09-16 20:28:56 -0700742
743 wait_event_interruptible_timeout(
744 ksb->pending_urb_wait,
745 !atomic_read(&ksb->tx_pending_cnt) &&
746 !atomic_read(&ksb->rx_pending_cnt),
747 msecs_to_jiffies(PENDING_URB_TIMEOUT));
748
Vamsi Krishna5b944712012-06-29 18:36:23 -0700749 spin_lock_irqsave(&ksb->lock, flags);
750 while (!list_empty(&ksb->to_ks_list)) {
751 pkt = list_first_entry(&ksb->to_ks_list,
752 struct data_pkt, list);
753 list_del_init(&pkt->list);
754 ksb_free_data_pkt(pkt);
755 }
756 while (!list_empty(&ksb->to_mdm_list)) {
757 pkt = list_first_entry(&ksb->to_mdm_list,
758 struct data_pkt, list);
759 list_del_init(&pkt->list);
760 ksb_free_data_pkt(pkt);
761 }
762 spin_unlock_irqrestore(&ksb->lock, flags);
763
Hemant Kumarf292a322012-09-07 19:00:21 -0700764 ifc->needs_remote_wakeup = 0;
Vamsi Krishna5b944712012-06-29 18:36:23 -0700765 usb_put_dev(ksb->udev);
766 ksb->ifc = NULL;
767 usb_set_intfdata(ifc, NULL);
768
769 return;
770}
771
772static struct usb_driver ksb_usb_driver = {
773 .name = "ks_bridge",
774 .probe = ksb_usb_probe,
775 .disconnect = ksb_usb_disconnect,
776 .suspend = ksb_usb_suspend,
777 .resume = ksb_usb_resume,
778 .id_table = ksb_usb_ids,
779 .supports_autosuspend = 1,
780};
781
782static ssize_t ksb_debug_show(struct seq_file *s, void *unused)
783{
784 unsigned long flags;
785 struct ks_bridge *ksb = s->private;
786 int i;
787
788 read_lock_irqsave(&ksb->dbg_lock, flags);
789 for (i = 0; i < DBG_MAX_MSG; i++) {
790 if (i == (ksb->dbg_idx - 1))
791 seq_printf(s, "-->%s\n", ksb->dbgbuf[i]);
792 else
793 seq_printf(s, "%s\n", ksb->dbgbuf[i]);
794 }
795 read_unlock_irqrestore(&ksb->dbg_lock, flags);
796
797 return 0;
798}
799
800static int ksb_debug_open(struct inode *ip, struct file *fp)
801{
802 return single_open(fp, ksb_debug_show, ip->i_private);
803
804 return 0;
805}
806
807static const struct file_operations dbg_fops = {
808 .open = ksb_debug_open,
809 .read = seq_read,
810 .llseek = seq_lseek,
811 .release = single_release,
812};
813static struct dentry *dbg_dir;
814static int __init ksb_init(void)
815{
816 struct ks_bridge *ksb;
817 int num_instances = 0;
818 int ret = 0;
819 int i;
820
821 dbg_dir = debugfs_create_dir("ks_bridge", NULL);
822 if (IS_ERR(dbg_dir))
823 pr_err("unable to create debug dir");
824
825 for (i = 0; i < NO_BRIDGE_INSTANCES; i++) {
826 ksb = kzalloc(sizeof(struct ks_bridge), GFP_KERNEL);
827 if (!ksb) {
828 pr_err("unable to allocat mem for ks_bridge");
Hemant Kumar38980ec42012-08-24 19:42:31 -0700829 ret = -ENOMEM;
830 goto dev_free;
Vamsi Krishna5b944712012-06-29 18:36:23 -0700831 }
832 __ksb[i] = ksb;
833
834 ksb->name = kasprintf(GFP_KERNEL, "ks_bridge:%i", i + 1);
835 if (!ksb->name) {
836 pr_info("unable to allocate name");
837 kfree(ksb);
838 ret = -ENOMEM;
839 goto dev_free;
840 }
841
842 spin_lock_init(&ksb->lock);
843 INIT_LIST_HEAD(&ksb->to_mdm_list);
844 INIT_LIST_HEAD(&ksb->to_ks_list);
845 init_waitqueue_head(&ksb->ks_wait_q);
Hemant Kumarea4b0b02012-09-16 20:28:56 -0700846 init_waitqueue_head(&ksb->pending_urb_wait);
Vamsi Krishna5b944712012-06-29 18:36:23 -0700847 ksb->wq = create_singlethread_workqueue(ksb->name);
848 if (!ksb->wq) {
849 pr_err("unable to allocate workqueue");
850 kfree(ksb->name);
851 kfree(ksb);
852 ret = -ENOMEM;
853 goto dev_free;
854 }
855
856 INIT_WORK(&ksb->to_mdm_work, ksb_tomdm_work);
857 INIT_WORK(&ksb->start_rx_work, ksb_start_rx_work);
858 init_usb_anchor(&ksb->submitted);
859
860 ksb->dbg_idx = 0;
861 ksb->dbg_lock = __RW_LOCK_UNLOCKED(lck);
862
863 if (!IS_ERR(dbg_dir))
864 debugfs_create_file(ksb->name, S_IRUGO, dbg_dir,
865 ksb, &dbg_fops);
866
867 num_instances++;
868 }
869
870 ret = usb_register(&ksb_usb_driver);
871 if (ret) {
872 pr_err("unable to register ks bridge driver");
873 goto dev_free;
874 }
875
876 pr_info("init done");
877
878 return 0;
879
880dev_free:
881 if (!IS_ERR(dbg_dir))
882 debugfs_remove_recursive(dbg_dir);
883
884 for (i = 0; i < num_instances; i++) {
885 ksb = __ksb[i];
886
887 destroy_workqueue(ksb->wq);
888 kfree(ksb->name);
889 kfree(ksb);
890 }
891
892 return ret;
893
894}
895
896static void __exit ksb_exit(void)
897{
898 struct ks_bridge *ksb;
899 int i;
900
901 if (!IS_ERR(dbg_dir))
902 debugfs_remove_recursive(dbg_dir);
903
904 usb_deregister(&ksb_usb_driver);
905
906 for (i = 0; i < NO_BRIDGE_INSTANCES; i++) {
907 ksb = __ksb[i];
908
909 destroy_workqueue(ksb->wq);
910 kfree(ksb->name);
911 kfree(ksb);
912 }
913}
914
915module_init(ksb_init);
916module_exit(ksb_exit);
917
918MODULE_DESCRIPTION(DRIVER_DESC);
919MODULE_VERSION(DRIVER_VERSION);
920MODULE_LICENSE("GPL v2");