usb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /**
  2. * Marvell NFC-over-USB driver: USB interface related functions
  3. *
  4. * Copyright (C) 2014, Marvell International Ltd.
  5. *
  6. * This software file (the "File") is distributed by Marvell International
  7. * Ltd. under the terms of the GNU General Public License Version 2, June 1991
  8. * (the "License"). You may use, redistribute and/or modify this File in
  9. * accordance with the terms and conditions of the License, a copy of which
  10. * is available on the worldwide web at
  11. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
  12. *
  13. * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
  14. * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
  15. * ARE EXPRESSLY DISCLAIMED. The License provides additional details about
  16. * this warranty disclaimer.
  17. **/
  18. #include <linux/module.h>
  19. #include <linux/usb.h>
  20. #include <linux/nfc.h>
  21. #include <net/nfc/nci.h>
  22. #include <net/nfc/nci_core.h>
  23. #include "nfcmrvl.h"
  24. static struct usb_device_id nfcmrvl_table[] = {
  25. { USB_DEVICE_AND_INTERFACE_INFO(0x1286, 0x2046,
  26. USB_CLASS_VENDOR_SPEC, 4, 1) },
  27. { } /* Terminating entry */
  28. };
  29. MODULE_DEVICE_TABLE(usb, nfcmrvl_table);
  30. #define NFCMRVL_USB_BULK_RUNNING 1
  31. #define NFCMRVL_USB_SUSPENDING 2
  32. struct nfcmrvl_usb_drv_data {
  33. struct usb_device *udev;
  34. struct usb_interface *intf;
  35. unsigned long flags;
  36. struct work_struct waker;
  37. struct usb_anchor tx_anchor;
  38. struct usb_anchor bulk_anchor;
  39. struct usb_anchor deferred;
  40. int tx_in_flight;
  41. /* protects tx_in_flight */
  42. spinlock_t txlock;
  43. struct usb_endpoint_descriptor *bulk_tx_ep;
  44. struct usb_endpoint_descriptor *bulk_rx_ep;
  45. int suspend_count;
  46. struct nfcmrvl_private *priv;
  47. };
  48. static int nfcmrvl_inc_tx(struct nfcmrvl_usb_drv_data *drv_data)
  49. {
  50. unsigned long flags;
  51. int rv;
  52. spin_lock_irqsave(&drv_data->txlock, flags);
  53. rv = test_bit(NFCMRVL_USB_SUSPENDING, &drv_data->flags);
  54. if (!rv)
  55. drv_data->tx_in_flight++;
  56. spin_unlock_irqrestore(&drv_data->txlock, flags);
  57. return rv;
  58. }
  59. static void nfcmrvl_bulk_complete(struct urb *urb)
  60. {
  61. struct nfcmrvl_usb_drv_data *drv_data = urb->context;
  62. struct sk_buff *skb;
  63. int err;
  64. dev_dbg(&drv_data->udev->dev, "urb %p status %d count %d\n",
  65. urb, urb->status, urb->actual_length);
  66. if (!test_bit(NFCMRVL_NCI_RUNNING, &drv_data->flags))
  67. return;
  68. if (!urb->status) {
  69. skb = nci_skb_alloc(drv_data->priv->ndev, urb->actual_length,
  70. GFP_ATOMIC);
  71. if (!skb) {
  72. nfc_err(&drv_data->udev->dev, "failed to alloc mem\n");
  73. } else {
  74. memcpy(skb_put(skb, urb->actual_length),
  75. urb->transfer_buffer, urb->actual_length);
  76. if (nfcmrvl_nci_recv_frame(drv_data->priv, skb) < 0)
  77. nfc_err(&drv_data->udev->dev,
  78. "corrupted Rx packet\n");
  79. }
  80. }
  81. if (!test_bit(NFCMRVL_USB_BULK_RUNNING, &drv_data->flags))
  82. return;
  83. usb_anchor_urb(urb, &drv_data->bulk_anchor);
  84. usb_mark_last_busy(drv_data->udev);
  85. err = usb_submit_urb(urb, GFP_ATOMIC);
  86. if (err) {
  87. /* -EPERM: urb is being killed;
  88. * -ENODEV: device got disconnected
  89. */
  90. if (err != -EPERM && err != -ENODEV)
  91. nfc_err(&drv_data->udev->dev,
  92. "urb %p failed to resubmit (%d)\n", urb, -err);
  93. usb_unanchor_urb(urb);
  94. }
  95. }
  96. static int
  97. nfcmrvl_submit_bulk_urb(struct nfcmrvl_usb_drv_data *drv_data, gfp_t mem_flags)
  98. {
  99. struct urb *urb;
  100. unsigned char *buf;
  101. unsigned int pipe;
  102. int err, size = NFCMRVL_NCI_MAX_EVENT_SIZE;
  103. if (!drv_data->bulk_rx_ep)
  104. return -ENODEV;
  105. urb = usb_alloc_urb(0, mem_flags);
  106. if (!urb)
  107. return -ENOMEM;
  108. buf = kmalloc(size, mem_flags);
  109. if (!buf) {
  110. usb_free_urb(urb);
  111. return -ENOMEM;
  112. }
  113. pipe = usb_rcvbulkpipe(drv_data->udev,
  114. drv_data->bulk_rx_ep->bEndpointAddress);
  115. usb_fill_bulk_urb(urb, drv_data->udev, pipe, buf, size,
  116. nfcmrvl_bulk_complete, drv_data);
  117. urb->transfer_flags |= URB_FREE_BUFFER;
  118. usb_mark_last_busy(drv_data->udev);
  119. usb_anchor_urb(urb, &drv_data->bulk_anchor);
  120. err = usb_submit_urb(urb, mem_flags);
  121. if (err) {
  122. if (err != -EPERM && err != -ENODEV)
  123. nfc_err(&drv_data->udev->dev,
  124. "urb %p submission failed (%d)\n", urb, -err);
  125. usb_unanchor_urb(urb);
  126. }
  127. usb_free_urb(urb);
  128. return err;
  129. }
  130. static void nfcmrvl_tx_complete(struct urb *urb)
  131. {
  132. struct sk_buff *skb = urb->context;
  133. struct nci_dev *ndev = (struct nci_dev *)skb->dev;
  134. struct nfcmrvl_private *priv = nci_get_drvdata(ndev);
  135. struct nfcmrvl_usb_drv_data *drv_data = priv->drv_data;
  136. nfc_info(priv->dev, "urb %p status %d count %d\n",
  137. urb, urb->status, urb->actual_length);
  138. spin_lock(&drv_data->txlock);
  139. drv_data->tx_in_flight--;
  140. spin_unlock(&drv_data->txlock);
  141. kfree(urb->setup_packet);
  142. kfree_skb(skb);
  143. }
  144. static int nfcmrvl_usb_nci_open(struct nfcmrvl_private *priv)
  145. {
  146. struct nfcmrvl_usb_drv_data *drv_data = priv->drv_data;
  147. int err;
  148. err = usb_autopm_get_interface(drv_data->intf);
  149. if (err)
  150. return err;
  151. drv_data->intf->needs_remote_wakeup = 1;
  152. err = nfcmrvl_submit_bulk_urb(drv_data, GFP_KERNEL);
  153. if (err)
  154. goto failed;
  155. set_bit(NFCMRVL_USB_BULK_RUNNING, &drv_data->flags);
  156. nfcmrvl_submit_bulk_urb(drv_data, GFP_KERNEL);
  157. usb_autopm_put_interface(drv_data->intf);
  158. return 0;
  159. failed:
  160. usb_autopm_put_interface(drv_data->intf);
  161. return err;
  162. }
  163. static void nfcmrvl_usb_stop_traffic(struct nfcmrvl_usb_drv_data *drv_data)
  164. {
  165. usb_kill_anchored_urbs(&drv_data->bulk_anchor);
  166. }
  167. static int nfcmrvl_usb_nci_close(struct nfcmrvl_private *priv)
  168. {
  169. struct nfcmrvl_usb_drv_data *drv_data = priv->drv_data;
  170. int err;
  171. cancel_work_sync(&drv_data->waker);
  172. clear_bit(NFCMRVL_USB_BULK_RUNNING, &drv_data->flags);
  173. nfcmrvl_usb_stop_traffic(drv_data);
  174. usb_kill_anchored_urbs(&drv_data->tx_anchor);
  175. err = usb_autopm_get_interface(drv_data->intf);
  176. if (err)
  177. goto failed;
  178. drv_data->intf->needs_remote_wakeup = 0;
  179. usb_autopm_put_interface(drv_data->intf);
  180. failed:
  181. usb_scuttle_anchored_urbs(&drv_data->deferred);
  182. return 0;
  183. }
  184. static int nfcmrvl_usb_nci_send(struct nfcmrvl_private *priv,
  185. struct sk_buff *skb)
  186. {
  187. struct nfcmrvl_usb_drv_data *drv_data = priv->drv_data;
  188. struct urb *urb;
  189. unsigned int pipe;
  190. int err;
  191. if (!drv_data->bulk_tx_ep)
  192. return -ENODEV;
  193. urb = usb_alloc_urb(0, GFP_ATOMIC);
  194. if (!urb)
  195. return -ENOMEM;
  196. pipe = usb_sndbulkpipe(drv_data->udev,
  197. drv_data->bulk_tx_ep->bEndpointAddress);
  198. usb_fill_bulk_urb(urb, drv_data->udev, pipe, skb->data, skb->len,
  199. nfcmrvl_tx_complete, skb);
  200. err = nfcmrvl_inc_tx(drv_data);
  201. if (err) {
  202. usb_anchor_urb(urb, &drv_data->deferred);
  203. schedule_work(&drv_data->waker);
  204. err = 0;
  205. goto done;
  206. }
  207. usb_anchor_urb(urb, &drv_data->tx_anchor);
  208. err = usb_submit_urb(urb, GFP_ATOMIC);
  209. if (err) {
  210. if (err != -EPERM && err != -ENODEV)
  211. nfc_err(&drv_data->udev->dev,
  212. "urb %p submission failed (%d)\n", urb, -err);
  213. kfree(urb->setup_packet);
  214. usb_unanchor_urb(urb);
  215. } else {
  216. usb_mark_last_busy(drv_data->udev);
  217. }
  218. done:
  219. usb_free_urb(urb);
  220. return err;
  221. }
  222. static struct nfcmrvl_if_ops usb_ops = {
  223. .nci_open = nfcmrvl_usb_nci_open,
  224. .nci_close = nfcmrvl_usb_nci_close,
  225. .nci_send = nfcmrvl_usb_nci_send,
  226. };
  227. static void nfcmrvl_waker(struct work_struct *work)
  228. {
  229. struct nfcmrvl_usb_drv_data *drv_data =
  230. container_of(work, struct nfcmrvl_usb_drv_data, waker);
  231. int err;
  232. err = usb_autopm_get_interface(drv_data->intf);
  233. if (err)
  234. return;
  235. usb_autopm_put_interface(drv_data->intf);
  236. }
  237. static int nfcmrvl_probe(struct usb_interface *intf,
  238. const struct usb_device_id *id)
  239. {
  240. struct usb_endpoint_descriptor *ep_desc;
  241. struct nfcmrvl_usb_drv_data *drv_data;
  242. struct nfcmrvl_private *priv;
  243. int i;
  244. struct usb_device *udev = interface_to_usbdev(intf);
  245. struct nfcmrvl_platform_data config;
  246. /* No configuration for USB */
  247. memset(&config, 0, sizeof(config));
  248. nfc_info(&udev->dev, "intf %p id %p\n", intf, id);
  249. drv_data = devm_kzalloc(&intf->dev, sizeof(*drv_data), GFP_KERNEL);
  250. if (!drv_data)
  251. return -ENOMEM;
  252. for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
  253. ep_desc = &intf->cur_altsetting->endpoint[i].desc;
  254. if (!drv_data->bulk_tx_ep &&
  255. usb_endpoint_is_bulk_out(ep_desc)) {
  256. drv_data->bulk_tx_ep = ep_desc;
  257. continue;
  258. }
  259. if (!drv_data->bulk_rx_ep &&
  260. usb_endpoint_is_bulk_in(ep_desc)) {
  261. drv_data->bulk_rx_ep = ep_desc;
  262. continue;
  263. }
  264. }
  265. if (!drv_data->bulk_tx_ep || !drv_data->bulk_rx_ep)
  266. return -ENODEV;
  267. drv_data->udev = udev;
  268. drv_data->intf = intf;
  269. INIT_WORK(&drv_data->waker, nfcmrvl_waker);
  270. spin_lock_init(&drv_data->txlock);
  271. init_usb_anchor(&drv_data->tx_anchor);
  272. init_usb_anchor(&drv_data->bulk_anchor);
  273. init_usb_anchor(&drv_data->deferred);
  274. priv = nfcmrvl_nci_register_dev(NFCMRVL_PHY_USB, drv_data, &usb_ops,
  275. &drv_data->udev->dev, &config);
  276. if (IS_ERR(priv))
  277. return PTR_ERR(priv);
  278. drv_data->priv = priv;
  279. drv_data->priv->support_fw_dnld = false;
  280. priv->dev = &drv_data->udev->dev;
  281. usb_set_intfdata(intf, drv_data);
  282. return 0;
  283. }
  284. static void nfcmrvl_disconnect(struct usb_interface *intf)
  285. {
  286. struct nfcmrvl_usb_drv_data *drv_data = usb_get_intfdata(intf);
  287. if (!drv_data)
  288. return;
  289. nfc_info(&drv_data->udev->dev, "intf %p\n", intf);
  290. nfcmrvl_nci_unregister_dev(drv_data->priv);
  291. usb_set_intfdata(drv_data->intf, NULL);
  292. }
  293. #ifdef CONFIG_PM
  294. static int nfcmrvl_suspend(struct usb_interface *intf, pm_message_t message)
  295. {
  296. struct nfcmrvl_usb_drv_data *drv_data = usb_get_intfdata(intf);
  297. nfc_info(&drv_data->udev->dev, "intf %p\n", intf);
  298. if (drv_data->suspend_count++)
  299. return 0;
  300. spin_lock_irq(&drv_data->txlock);
  301. if (!(PMSG_IS_AUTO(message) && drv_data->tx_in_flight)) {
  302. set_bit(NFCMRVL_USB_SUSPENDING, &drv_data->flags);
  303. spin_unlock_irq(&drv_data->txlock);
  304. } else {
  305. spin_unlock_irq(&drv_data->txlock);
  306. drv_data->suspend_count--;
  307. return -EBUSY;
  308. }
  309. nfcmrvl_usb_stop_traffic(drv_data);
  310. usb_kill_anchored_urbs(&drv_data->tx_anchor);
  311. return 0;
  312. }
  313. static void nfcmrvl_play_deferred(struct nfcmrvl_usb_drv_data *drv_data)
  314. {
  315. struct urb *urb;
  316. int err;
  317. while ((urb = usb_get_from_anchor(&drv_data->deferred))) {
  318. err = usb_submit_urb(urb, GFP_ATOMIC);
  319. if (err)
  320. break;
  321. drv_data->tx_in_flight++;
  322. }
  323. usb_scuttle_anchored_urbs(&drv_data->deferred);
  324. }
  325. static int nfcmrvl_resume(struct usb_interface *intf)
  326. {
  327. struct nfcmrvl_usb_drv_data *drv_data = usb_get_intfdata(intf);
  328. int err = 0;
  329. nfc_info(&drv_data->udev->dev, "intf %p\n", intf);
  330. if (--drv_data->suspend_count)
  331. return 0;
  332. if (!test_bit(NFCMRVL_NCI_RUNNING, &drv_data->flags))
  333. goto done;
  334. if (test_bit(NFCMRVL_USB_BULK_RUNNING, &drv_data->flags)) {
  335. err = nfcmrvl_submit_bulk_urb(drv_data, GFP_NOIO);
  336. if (err) {
  337. clear_bit(NFCMRVL_USB_BULK_RUNNING, &drv_data->flags);
  338. goto failed;
  339. }
  340. nfcmrvl_submit_bulk_urb(drv_data, GFP_NOIO);
  341. }
  342. spin_lock_irq(&drv_data->txlock);
  343. nfcmrvl_play_deferred(drv_data);
  344. clear_bit(NFCMRVL_USB_SUSPENDING, &drv_data->flags);
  345. spin_unlock_irq(&drv_data->txlock);
  346. return 0;
  347. failed:
  348. usb_scuttle_anchored_urbs(&drv_data->deferred);
  349. done:
  350. spin_lock_irq(&drv_data->txlock);
  351. clear_bit(NFCMRVL_USB_SUSPENDING, &drv_data->flags);
  352. spin_unlock_irq(&drv_data->txlock);
  353. return err;
  354. }
  355. #endif
  356. static struct usb_driver nfcmrvl_usb_driver = {
  357. .name = "nfcmrvl",
  358. .probe = nfcmrvl_probe,
  359. .disconnect = nfcmrvl_disconnect,
  360. #ifdef CONFIG_PM
  361. .suspend = nfcmrvl_suspend,
  362. .resume = nfcmrvl_resume,
  363. .reset_resume = nfcmrvl_resume,
  364. #endif
  365. .id_table = nfcmrvl_table,
  366. .supports_autosuspend = 1,
  367. .disable_hub_initiated_lpm = 1,
  368. .soft_unbind = 1,
  369. };
  370. module_usb_driver(nfcmrvl_usb_driver);
  371. MODULE_AUTHOR("Marvell International Ltd.");
  372. MODULE_DESCRIPTION("Marvell NFC-over-USB driver");
  373. MODULE_LICENSE("GPL v2");