ks_bridge.c 22 KB

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