diag_bridge.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. /* Copyright (c) 2011-2013, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. /* add additional information to our printk's */
  13. #define pr_fmt(fmt) "%s: " fmt "\n", __func__
  14. #include <linux/kernel.h>
  15. #include <linux/errno.h>
  16. #include <linux/init.h>
  17. #include <linux/slab.h>
  18. #include <linux/module.h>
  19. #include <linux/kref.h>
  20. #include <linux/mutex.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 <mach/diag_bridge.h>
  27. #define DRIVER_DESC "USB host diag bridge driver"
  28. #define DRIVER_VERSION "1.0"
  29. #define MAX_DIAG_BRIDGE_DEVS 2
  30. #define AUTOSUSP_DELAY_WITH_USB 1000
  31. struct diag_bridge {
  32. struct usb_device *udev;
  33. struct usb_interface *ifc;
  34. struct usb_anchor submitted;
  35. __u8 in_epAddr;
  36. __u8 out_epAddr;
  37. int err;
  38. struct kref kref;
  39. struct mutex ifc_mutex;
  40. struct diag_bridge_ops *ops;
  41. struct platform_device *pdev;
  42. unsigned default_autosusp_delay;
  43. int id;
  44. /* debugging counters */
  45. unsigned long bytes_to_host;
  46. unsigned long bytes_to_mdm;
  47. unsigned pending_reads;
  48. unsigned pending_writes;
  49. };
  50. struct diag_bridge *__dev[MAX_DIAG_BRIDGE_DEVS];
  51. int diag_bridge_open(int id, struct diag_bridge_ops *ops)
  52. {
  53. struct diag_bridge *dev;
  54. if (id < 0 || id >= MAX_DIAG_BRIDGE_DEVS) {
  55. pr_err("Invalid device ID");
  56. return -ENODEV;
  57. }
  58. dev = __dev[id];
  59. if (!dev) {
  60. pr_err("dev is null");
  61. return -ENODEV;
  62. }
  63. if (dev->ops) {
  64. pr_err("bridge already opened");
  65. return -EALREADY;
  66. }
  67. dev->ops = ops;
  68. dev->err = 0;
  69. #ifdef CONFIG_PM_RUNTIME
  70. dev->default_autosusp_delay = dev->udev->dev.power.autosuspend_delay;
  71. #endif
  72. pm_runtime_set_autosuspend_delay(&dev->udev->dev,
  73. AUTOSUSP_DELAY_WITH_USB);
  74. kref_get(&dev->kref);
  75. return 0;
  76. }
  77. EXPORT_SYMBOL(diag_bridge_open);
  78. static void diag_bridge_delete(struct kref *kref)
  79. {
  80. struct diag_bridge *dev = container_of(kref, struct diag_bridge, kref);
  81. int id = dev->id;
  82. usb_put_dev(dev->udev);
  83. __dev[id] = 0;
  84. kfree(dev);
  85. }
  86. void diag_bridge_close(int id)
  87. {
  88. struct diag_bridge *dev;
  89. if (id < 0 || id >= MAX_DIAG_BRIDGE_DEVS) {
  90. pr_err("Invalid device ID");
  91. return;
  92. }
  93. dev = __dev[id];
  94. if (!dev) {
  95. pr_err("dev is null");
  96. return;
  97. }
  98. if (!dev->ops) {
  99. pr_err("can't close bridge that was not open");
  100. return;
  101. }
  102. dev_dbg(&dev->ifc->dev, "%s:\n", __func__);
  103. usb_kill_anchored_urbs(&dev->submitted);
  104. dev->ops = 0;
  105. pm_runtime_set_autosuspend_delay(&dev->udev->dev,
  106. dev->default_autosusp_delay);
  107. kref_put(&dev->kref, diag_bridge_delete);
  108. }
  109. EXPORT_SYMBOL(diag_bridge_close);
  110. static void diag_bridge_read_cb(struct urb *urb)
  111. {
  112. struct diag_bridge *dev = urb->context;
  113. struct diag_bridge_ops *cbs = dev->ops;
  114. dev_dbg(&dev->ifc->dev, "%s: status:%d actual:%d\n", __func__,
  115. urb->status, urb->actual_length);
  116. /* save error so that subsequent read/write returns ENODEV */
  117. if (urb->status == -EPROTO)
  118. dev->err = urb->status;
  119. if (cbs && cbs->read_complete_cb)
  120. cbs->read_complete_cb(cbs->ctxt,
  121. urb->transfer_buffer,
  122. urb->transfer_buffer_length,
  123. urb->status < 0 ? urb->status : urb->actual_length);
  124. dev->bytes_to_host += urb->actual_length;
  125. dev->pending_reads--;
  126. kref_put(&dev->kref, diag_bridge_delete);
  127. }
  128. int diag_bridge_read(int id, char *data, int size)
  129. {
  130. struct urb *urb = NULL;
  131. unsigned int pipe;
  132. struct diag_bridge *dev;
  133. int ret;
  134. if (id < 0 || id >= MAX_DIAG_BRIDGE_DEVS) {
  135. pr_err("Invalid device ID");
  136. return -ENODEV;
  137. }
  138. pr_debug("reading %d bytes", size);
  139. dev = __dev[id];
  140. if (!dev) {
  141. pr_err("device is disconnected");
  142. return -ENODEV;
  143. }
  144. mutex_lock(&dev->ifc_mutex);
  145. if (!dev->ifc) {
  146. ret = -ENODEV;
  147. goto error;
  148. }
  149. if (!dev->ops) {
  150. pr_err("bridge is not open");
  151. ret = -ENODEV;
  152. goto error;
  153. }
  154. if (!size) {
  155. dev_err(&dev->ifc->dev, "invalid size:%d\n", size);
  156. ret = -EINVAL;
  157. goto error;
  158. }
  159. /* if there was a previous unrecoverable error, just quit */
  160. if (dev->err) {
  161. ret = -ENODEV;
  162. goto error;
  163. }
  164. kref_get(&dev->kref);
  165. urb = usb_alloc_urb(0, GFP_KERNEL);
  166. if (!urb) {
  167. dev_err(&dev->ifc->dev, "unable to allocate urb\n");
  168. ret = -ENOMEM;
  169. goto put_error;
  170. }
  171. ret = usb_autopm_get_interface(dev->ifc);
  172. if (ret < 0 && ret != -EAGAIN && ret != -EACCES) {
  173. pr_err_ratelimited("read: autopm_get failed:%d", ret);
  174. goto free_error;
  175. }
  176. pipe = usb_rcvbulkpipe(dev->udev, dev->in_epAddr);
  177. usb_fill_bulk_urb(urb, dev->udev, pipe, data, size,
  178. diag_bridge_read_cb, dev);
  179. usb_anchor_urb(urb, &dev->submitted);
  180. dev->pending_reads++;
  181. ret = usb_submit_urb(urb, GFP_KERNEL);
  182. if (ret) {
  183. pr_err_ratelimited("submitting urb failed err:%d", ret);
  184. dev->pending_reads--;
  185. usb_unanchor_urb(urb);
  186. }
  187. usb_autopm_put_interface(dev->ifc);
  188. free_error:
  189. usb_free_urb(urb);
  190. put_error:
  191. if (ret) /* otherwise this is done in the completion handler */
  192. kref_put(&dev->kref, diag_bridge_delete);
  193. error:
  194. mutex_unlock(&dev->ifc_mutex);
  195. return ret;
  196. }
  197. EXPORT_SYMBOL(diag_bridge_read);
  198. static void diag_bridge_write_cb(struct urb *urb)
  199. {
  200. struct diag_bridge *dev = urb->context;
  201. struct diag_bridge_ops *cbs = dev->ops;
  202. dev_dbg(&dev->ifc->dev, "%s:\n", __func__);
  203. usb_autopm_put_interface_async(dev->ifc);
  204. /* save error so that subsequent read/write returns ENODEV */
  205. if (urb->status == -EPROTO)
  206. dev->err = urb->status;
  207. if (cbs && cbs->write_complete_cb)
  208. cbs->write_complete_cb(cbs->ctxt,
  209. urb->transfer_buffer,
  210. urb->transfer_buffer_length,
  211. urb->status < 0 ? urb->status : urb->actual_length);
  212. dev->bytes_to_mdm += urb->actual_length;
  213. dev->pending_writes--;
  214. kref_put(&dev->kref, diag_bridge_delete);
  215. }
  216. int diag_bridge_write(int id, char *data, int size)
  217. {
  218. struct urb *urb = NULL;
  219. unsigned int pipe;
  220. struct diag_bridge *dev;
  221. int ret;
  222. if (id < 0 || id >= MAX_DIAG_BRIDGE_DEVS) {
  223. pr_err("Invalid device ID");
  224. return -ENODEV;
  225. }
  226. pr_debug("writing %d bytes", size);
  227. dev = __dev[id];
  228. if (!dev) {
  229. pr_err("device is disconnected");
  230. return -ENODEV;
  231. }
  232. mutex_lock(&dev->ifc_mutex);
  233. if (!dev->ifc) {
  234. ret = -ENODEV;
  235. goto error;
  236. }
  237. if (!dev->ops) {
  238. pr_err("bridge is not open");
  239. ret = -ENODEV;
  240. goto error;
  241. }
  242. if (!size) {
  243. dev_err(&dev->ifc->dev, "invalid size:%d\n", size);
  244. ret = -EINVAL;
  245. goto error;
  246. }
  247. /* if there was a previous unrecoverable error, just quit */
  248. if (dev->err) {
  249. ret = -ENODEV;
  250. goto error;
  251. }
  252. kref_get(&dev->kref);
  253. urb = usb_alloc_urb(0, GFP_KERNEL);
  254. if (!urb) {
  255. dev_err(&dev->ifc->dev, "unable to allocate urb\n");
  256. ret = -ENOMEM;
  257. goto put_error;
  258. }
  259. ret = usb_autopm_get_interface(dev->ifc);
  260. if (ret < 0 && ret != -EAGAIN && ret != -EACCES) {
  261. pr_err_ratelimited("write: autopm_get failed:%d", ret);
  262. goto free_error;
  263. }
  264. pipe = usb_sndbulkpipe(dev->udev, dev->out_epAddr);
  265. usb_fill_bulk_urb(urb, dev->udev, pipe, data, size,
  266. diag_bridge_write_cb, dev);
  267. urb->transfer_flags |= URB_ZERO_PACKET;
  268. usb_anchor_urb(urb, &dev->submitted);
  269. dev->pending_writes++;
  270. ret = usb_submit_urb(urb, GFP_KERNEL);
  271. if (ret) {
  272. pr_err_ratelimited("submitting urb failed err:%d", ret);
  273. dev->pending_writes--;
  274. usb_unanchor_urb(urb);
  275. usb_autopm_put_interface(dev->ifc);
  276. goto free_error;
  277. }
  278. free_error:
  279. usb_free_urb(urb);
  280. put_error:
  281. if (ret) /* otherwise this is done in the completion handler */
  282. kref_put(&dev->kref, diag_bridge_delete);
  283. error:
  284. mutex_unlock(&dev->ifc_mutex);
  285. return ret;
  286. }
  287. EXPORT_SYMBOL(diag_bridge_write);
  288. #if defined(CONFIG_DEBUG_FS)
  289. #define DEBUG_BUF_SIZE 512
  290. static ssize_t diag_read_stats(struct file *file, char __user *ubuf,
  291. size_t count, loff_t *ppos)
  292. {
  293. char *buf;
  294. int i, ret = 0;
  295. buf = kzalloc(sizeof(char) * DEBUG_BUF_SIZE, GFP_KERNEL);
  296. if (!buf)
  297. return -ENOMEM;
  298. for (i = 0; i < MAX_DIAG_BRIDGE_DEVS; i++) {
  299. struct diag_bridge *dev = __dev[i];
  300. if (!dev)
  301. continue;
  302. ret += scnprintf(buf, DEBUG_BUF_SIZE,
  303. "epin:%d, epout:%d\n"
  304. "bytes to host: %lu\n"
  305. "bytes to mdm: %lu\n"
  306. "pending reads: %u\n"
  307. "pending writes: %u\n"
  308. "last error: %d\n",
  309. dev->in_epAddr, dev->out_epAddr,
  310. dev->bytes_to_host, dev->bytes_to_mdm,
  311. dev->pending_reads, dev->pending_writes,
  312. dev->err);
  313. }
  314. ret = simple_read_from_buffer(ubuf, count, ppos, buf, ret);
  315. kfree(buf);
  316. return ret;
  317. }
  318. static ssize_t diag_reset_stats(struct file *file, const char __user *buf,
  319. size_t count, loff_t *ppos)
  320. {
  321. int i;
  322. for (i = 0; i < MAX_DIAG_BRIDGE_DEVS; i++) {
  323. struct diag_bridge *dev = __dev[i];
  324. if (dev) {
  325. dev->bytes_to_host = dev->bytes_to_mdm = 0;
  326. dev->pending_reads = dev->pending_writes = 0;
  327. }
  328. }
  329. return count;
  330. }
  331. const struct file_operations diag_stats_ops = {
  332. .read = diag_read_stats,
  333. .write = diag_reset_stats,
  334. };
  335. static struct dentry *dent;
  336. static void diag_bridge_debugfs_init(void)
  337. {
  338. struct dentry *dfile;
  339. dent = debugfs_create_dir("diag_bridge", 0);
  340. if (IS_ERR(dent))
  341. return;
  342. dfile = debugfs_create_file("status", 0444, dent, 0, &diag_stats_ops);
  343. if (!dfile || IS_ERR(dfile))
  344. debugfs_remove(dent);
  345. }
  346. static void diag_bridge_debugfs_cleanup(void)
  347. {
  348. if (dent) {
  349. debugfs_remove_recursive(dent);
  350. dent = NULL;
  351. }
  352. }
  353. #else
  354. static inline void diag_bridge_debugfs_init(void) { }
  355. static inline void diag_bridge_debugfs_cleanup(void) { }
  356. #endif
  357. static int
  358. diag_bridge_probe(struct usb_interface *ifc, const struct usb_device_id *id)
  359. {
  360. struct diag_bridge *dev;
  361. struct usb_host_interface *ifc_desc;
  362. struct usb_endpoint_descriptor *ep_desc;
  363. int i, devid, ret = -ENOMEM;
  364. __u8 ifc_num;
  365. pr_debug("id:%lu", id->driver_info);
  366. ifc_num = ifc->cur_altsetting->desc.bInterfaceNumber;
  367. /* is this interface supported ? */
  368. if (ifc_num != (id->driver_info & 0xFF))
  369. return -ENODEV;
  370. devid = (id->driver_info >> 8) & 0xFF;
  371. if (devid < 0 || devid >= MAX_DIAG_BRIDGE_DEVS)
  372. return -ENODEV;
  373. /* already probed? */
  374. if (__dev[devid]) {
  375. pr_err("Diag device already probed");
  376. return -ENODEV;
  377. }
  378. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  379. if (!dev) {
  380. pr_err("unable to allocate dev");
  381. return -ENOMEM;
  382. }
  383. __dev[devid] = dev;
  384. dev->id = devid;
  385. dev->udev = usb_get_dev(interface_to_usbdev(ifc));
  386. dev->ifc = ifc;
  387. kref_init(&dev->kref);
  388. mutex_init(&dev->ifc_mutex);
  389. init_usb_anchor(&dev->submitted);
  390. ifc_desc = ifc->cur_altsetting;
  391. for (i = 0; i < ifc_desc->desc.bNumEndpoints; i++) {
  392. ep_desc = &ifc_desc->endpoint[i].desc;
  393. if (!dev->in_epAddr && usb_endpoint_is_bulk_in(ep_desc))
  394. dev->in_epAddr = ep_desc->bEndpointAddress;
  395. if (!dev->out_epAddr && usb_endpoint_is_bulk_out(ep_desc))
  396. dev->out_epAddr = ep_desc->bEndpointAddress;
  397. }
  398. if (!(dev->in_epAddr && dev->out_epAddr)) {
  399. pr_err("could not find bulk in and bulk out endpoints");
  400. ret = -ENODEV;
  401. goto error;
  402. }
  403. usb_set_intfdata(ifc, dev);
  404. diag_bridge_debugfs_init();
  405. dev->pdev = platform_device_register_simple("diag_bridge", devid,
  406. NULL, 0);
  407. if (IS_ERR(dev->pdev)) {
  408. pr_err("unable to allocate platform device");
  409. ret = PTR_ERR(dev->pdev);
  410. goto error;
  411. }
  412. dev_dbg(&dev->ifc->dev, "%s: complete\n", __func__);
  413. return 0;
  414. error:
  415. if (dev) {
  416. platform_device_put(dev->pdev);
  417. kref_put(&dev->kref, diag_bridge_delete);
  418. }
  419. return ret;
  420. }
  421. static void diag_bridge_disconnect(struct usb_interface *ifc)
  422. {
  423. struct diag_bridge *dev = usb_get_intfdata(ifc);
  424. dev_dbg(&dev->ifc->dev, "%s:\n", __func__);
  425. platform_device_unregister(dev->pdev);
  426. mutex_lock(&dev->ifc_mutex);
  427. dev->ifc = NULL;
  428. mutex_unlock(&dev->ifc_mutex);
  429. diag_bridge_debugfs_cleanup();
  430. kref_put(&dev->kref, diag_bridge_delete);
  431. usb_set_intfdata(ifc, NULL);
  432. }
  433. static int diag_bridge_suspend(struct usb_interface *ifc, pm_message_t message)
  434. {
  435. struct diag_bridge *dev = usb_get_intfdata(ifc);
  436. struct diag_bridge_ops *cbs = dev->ops;
  437. int ret = 0;
  438. if (cbs && cbs->suspend) {
  439. ret = cbs->suspend(cbs->ctxt);
  440. if (ret) {
  441. dev_dbg(&dev->ifc->dev,
  442. "%s: diag veto'd suspend\n", __func__);
  443. return ret;
  444. }
  445. usb_kill_anchored_urbs(&dev->submitted);
  446. }
  447. return ret;
  448. }
  449. static int diag_bridge_resume(struct usb_interface *ifc)
  450. {
  451. struct diag_bridge *dev = usb_get_intfdata(ifc);
  452. struct diag_bridge_ops *cbs = dev->ops;
  453. if (cbs && cbs->resume)
  454. cbs->resume(cbs->ctxt);
  455. return 0;
  456. }
  457. #define VALID_INTERFACE_NUM 0
  458. #define DEV_ID(n) ((n)<<8)
  459. static const struct usb_device_id diag_bridge_ids[] = {
  460. { USB_DEVICE(0x5c6, 0x9001),
  461. .driver_info = VALID_INTERFACE_NUM | DEV_ID(0), },
  462. { USB_DEVICE(0x5c6, 0x9034),
  463. .driver_info = VALID_INTERFACE_NUM | DEV_ID(0), },
  464. { USB_DEVICE(0x5c6, 0x9048),
  465. .driver_info = VALID_INTERFACE_NUM | DEV_ID(0), },
  466. { USB_DEVICE(0x5c6, 0x904C),
  467. .driver_info = VALID_INTERFACE_NUM | DEV_ID(0), },
  468. { USB_DEVICE(0x5c6, 0x9075),
  469. .driver_info = VALID_INTERFACE_NUM | DEV_ID(0), },
  470. { USB_DEVICE(0x5c6, 0x9079),
  471. .driver_info = VALID_INTERFACE_NUM | DEV_ID(1), },
  472. {} /* terminating entry */
  473. };
  474. MODULE_DEVICE_TABLE(usb, diag_bridge_ids);
  475. static struct usb_driver diag_bridge_driver = {
  476. .name = "diag_bridge",
  477. .probe = diag_bridge_probe,
  478. .disconnect = diag_bridge_disconnect,
  479. .suspend = diag_bridge_suspend,
  480. .resume = diag_bridge_resume,
  481. .id_table = diag_bridge_ids,
  482. .supports_autosuspend = 1,
  483. };
  484. static int __init diag_bridge_init(void)
  485. {
  486. int ret;
  487. ret = usb_register(&diag_bridge_driver);
  488. if (ret) {
  489. pr_err("unable to register diag driver");
  490. return ret;
  491. }
  492. return 0;
  493. }
  494. static void __exit diag_bridge_exit(void)
  495. {
  496. usb_deregister(&diag_bridge_driver);
  497. }
  498. module_init(diag_bridge_init);
  499. module_exit(diag_bridge_exit);
  500. MODULE_DESCRIPTION(DRIVER_DESC);
  501. MODULE_VERSION(DRIVER_VERSION);
  502. MODULE_LICENSE("GPL v2");