btusb.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. /*
  2. *
  3. * Generic Bluetooth USB driver
  4. *
  5. * Copyright (C) 2005-2007 Marcel Holtmann <marcel@holtmann.org>
  6. *
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/slab.h>
  27. #include <linux/types.h>
  28. #include <linux/sched.h>
  29. #include <linux/errno.h>
  30. #include <linux/skbuff.h>
  31. #include <linux/usb.h>
  32. #include <net/bluetooth/bluetooth.h>
  33. #include <net/bluetooth/hci_core.h>
  34. //#define CONFIG_BT_HCIBTUSB_DEBUG
  35. #ifndef CONFIG_BT_HCIBTUSB_DEBUG
  36. #undef BT_DBG
  37. #define BT_DBG(D...)
  38. #endif
  39. #define VERSION "0.1"
  40. static struct usb_device_id btusb_table[] = {
  41. /* Generic Bluetooth USB device */
  42. { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
  43. { } /* Terminating entry */
  44. };
  45. MODULE_DEVICE_TABLE(usb, btusb_table);
  46. static struct usb_device_id blacklist_table[] = {
  47. { } /* Terminating entry */
  48. };
  49. #define BTUSB_INTR_RUNNING 0
  50. #define BTUSB_BULK_RUNNING 1
  51. struct btusb_data {
  52. struct hci_dev *hdev;
  53. struct usb_device *udev;
  54. spinlock_t lock;
  55. unsigned long flags;
  56. struct work_struct work;
  57. struct usb_anchor tx_anchor;
  58. struct usb_anchor intr_anchor;
  59. struct usb_anchor bulk_anchor;
  60. struct usb_endpoint_descriptor *intr_ep;
  61. struct usb_endpoint_descriptor *bulk_tx_ep;
  62. struct usb_endpoint_descriptor *bulk_rx_ep;
  63. };
  64. static void btusb_intr_complete(struct urb *urb)
  65. {
  66. struct hci_dev *hdev = urb->context;
  67. struct btusb_data *data = hdev->driver_data;
  68. int err;
  69. BT_DBG("%s urb %p status %d count %d", hdev->name,
  70. urb, urb->status, urb->actual_length);
  71. if (!test_bit(HCI_RUNNING, &hdev->flags))
  72. return;
  73. if (urb->status == 0) {
  74. if (hci_recv_fragment(hdev, HCI_EVENT_PKT,
  75. urb->transfer_buffer,
  76. urb->actual_length) < 0) {
  77. BT_ERR("%s corrupted event packet", hdev->name);
  78. hdev->stat.err_rx++;
  79. }
  80. }
  81. if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
  82. return;
  83. usb_anchor_urb(urb, &data->intr_anchor);
  84. err = usb_submit_urb(urb, GFP_ATOMIC);
  85. if (err < 0) {
  86. BT_ERR("%s urb %p failed to resubmit (%d)",
  87. hdev->name, urb, -err);
  88. usb_unanchor_urb(urb);
  89. }
  90. }
  91. static inline int btusb_submit_intr_urb(struct hci_dev *hdev)
  92. {
  93. struct btusb_data *data = hdev->driver_data;
  94. struct urb *urb;
  95. unsigned char *buf;
  96. unsigned int pipe;
  97. int err, size;
  98. BT_DBG("%s", hdev->name);
  99. urb = usb_alloc_urb(0, GFP_ATOMIC);
  100. if (!urb)
  101. return -ENOMEM;
  102. size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
  103. buf = kmalloc(size, GFP_ATOMIC);
  104. if (!buf) {
  105. usb_free_urb(urb);
  106. return -ENOMEM;
  107. }
  108. pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
  109. usb_fill_int_urb(urb, data->udev, pipe, buf, size,
  110. btusb_intr_complete, hdev,
  111. data->intr_ep->bInterval);
  112. urb->transfer_flags |= URB_FREE_BUFFER;
  113. usb_anchor_urb(urb, &data->intr_anchor);
  114. err = usb_submit_urb(urb, GFP_ATOMIC);
  115. if (err < 0) {
  116. BT_ERR("%s urb %p submission failed (%d)",
  117. hdev->name, urb, -err);
  118. usb_unanchor_urb(urb);
  119. kfree(buf);
  120. }
  121. usb_free_urb(urb);
  122. return err;
  123. }
  124. static void btusb_bulk_complete(struct urb *urb)
  125. {
  126. struct hci_dev *hdev = urb->context;
  127. struct btusb_data *data = hdev->driver_data;
  128. int err;
  129. BT_DBG("%s urb %p status %d count %d", hdev->name,
  130. urb, urb->status, urb->actual_length);
  131. if (!test_bit(HCI_RUNNING, &hdev->flags))
  132. return;
  133. if (urb->status == 0) {
  134. if (hci_recv_fragment(hdev, HCI_ACLDATA_PKT,
  135. urb->transfer_buffer,
  136. urb->actual_length) < 0) {
  137. BT_ERR("%s corrupted ACL packet", hdev->name);
  138. hdev->stat.err_rx++;
  139. }
  140. }
  141. if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
  142. return;
  143. usb_anchor_urb(urb, &data->bulk_anchor);
  144. err = usb_submit_urb(urb, GFP_ATOMIC);
  145. if (err < 0) {
  146. BT_ERR("%s urb %p failed to resubmit (%d)",
  147. hdev->name, urb, -err);
  148. usb_unanchor_urb(urb);
  149. }
  150. }
  151. static inline int btusb_submit_bulk_urb(struct hci_dev *hdev)
  152. {
  153. struct btusb_data *data = hdev->driver_data;
  154. struct urb *urb;
  155. unsigned char *buf;
  156. unsigned int pipe;
  157. int err, size;
  158. BT_DBG("%s", hdev->name);
  159. urb = usb_alloc_urb(0, GFP_KERNEL);
  160. if (!urb)
  161. return -ENOMEM;
  162. size = le16_to_cpu(data->bulk_rx_ep->wMaxPacketSize);
  163. buf = kmalloc(size, GFP_KERNEL);
  164. if (!buf) {
  165. usb_free_urb(urb);
  166. return -ENOMEM;
  167. }
  168. pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
  169. usb_fill_bulk_urb(urb, data->udev, pipe,
  170. buf, size, btusb_bulk_complete, hdev);
  171. urb->transfer_flags |= URB_FREE_BUFFER;
  172. usb_anchor_urb(urb, &data->bulk_anchor);
  173. err = usb_submit_urb(urb, GFP_KERNEL);
  174. if (err < 0) {
  175. BT_ERR("%s urb %p submission failed (%d)",
  176. hdev->name, urb, -err);
  177. usb_unanchor_urb(urb);
  178. kfree(buf);
  179. }
  180. usb_free_urb(urb);
  181. return err;
  182. }
  183. static void btusb_tx_complete(struct urb *urb)
  184. {
  185. struct sk_buff *skb = urb->context;
  186. struct hci_dev *hdev = (struct hci_dev *) skb->dev;
  187. BT_DBG("%s urb %p status %d count %d", hdev->name,
  188. urb, urb->status, urb->actual_length);
  189. if (!test_bit(HCI_RUNNING, &hdev->flags))
  190. goto done;
  191. if (!urb->status)
  192. hdev->stat.byte_tx += urb->transfer_buffer_length;
  193. else
  194. hdev->stat.err_tx++;
  195. done:
  196. kfree(urb->setup_packet);
  197. kfree_skb(skb);
  198. }
  199. static int btusb_open(struct hci_dev *hdev)
  200. {
  201. struct btusb_data *data = hdev->driver_data;
  202. int err;
  203. BT_DBG("%s", hdev->name);
  204. if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
  205. return 0;
  206. if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
  207. return 0;
  208. err = btusb_submit_intr_urb(hdev);
  209. if (err < 0) {
  210. clear_bit(BTUSB_INTR_RUNNING, &hdev->flags);
  211. clear_bit(HCI_RUNNING, &hdev->flags);
  212. }
  213. return err;
  214. }
  215. static int btusb_close(struct hci_dev *hdev)
  216. {
  217. struct btusb_data *data = hdev->driver_data;
  218. BT_DBG("%s", hdev->name);
  219. if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
  220. return 0;
  221. clear_bit(BTUSB_BULK_RUNNING, &data->flags);
  222. usb_kill_anchored_urbs(&data->bulk_anchor);
  223. clear_bit(BTUSB_INTR_RUNNING, &data->flags);
  224. usb_kill_anchored_urbs(&data->intr_anchor);
  225. return 0;
  226. }
  227. static int btusb_flush(struct hci_dev *hdev)
  228. {
  229. struct btusb_data *data = hdev->driver_data;
  230. BT_DBG("%s", hdev->name);
  231. usb_kill_anchored_urbs(&data->tx_anchor);
  232. return 0;
  233. }
  234. static int btusb_send_frame(struct sk_buff *skb)
  235. {
  236. struct hci_dev *hdev = (struct hci_dev *) skb->dev;
  237. struct btusb_data *data = hdev->driver_data;
  238. struct usb_ctrlrequest *dr;
  239. struct urb *urb;
  240. unsigned int pipe;
  241. int err;
  242. BT_DBG("%s", hdev->name);
  243. if (!test_bit(HCI_RUNNING, &hdev->flags))
  244. return -EBUSY;
  245. switch (bt_cb(skb)->pkt_type) {
  246. case HCI_COMMAND_PKT:
  247. urb = usb_alloc_urb(0, GFP_ATOMIC);
  248. if (!urb)
  249. return -ENOMEM;
  250. dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
  251. if (!dr) {
  252. usb_free_urb(urb);
  253. return -ENOMEM;
  254. }
  255. dr->bRequestType = USB_TYPE_CLASS;
  256. dr->bRequest = 0;
  257. dr->wIndex = 0;
  258. dr->wValue = 0;
  259. dr->wLength = __cpu_to_le16(skb->len);
  260. pipe = usb_sndctrlpipe(data->udev, 0x00);
  261. usb_fill_control_urb(urb, data->udev, pipe, (void *) dr,
  262. skb->data, skb->len, btusb_tx_complete, skb);
  263. hdev->stat.cmd_tx++;
  264. break;
  265. case HCI_ACLDATA_PKT:
  266. urb = usb_alloc_urb(0, GFP_ATOMIC);
  267. if (!urb)
  268. return -ENOMEM;
  269. pipe = usb_sndbulkpipe(data->udev,
  270. data->bulk_tx_ep->bEndpointAddress);
  271. usb_fill_bulk_urb(urb, data->udev, pipe,
  272. skb->data, skb->len, btusb_tx_complete, skb);
  273. hdev->stat.acl_tx++;
  274. break;
  275. case HCI_SCODATA_PKT:
  276. hdev->stat.sco_tx++;
  277. kfree_skb(skb);
  278. return 0;
  279. default:
  280. return -EILSEQ;
  281. }
  282. usb_anchor_urb(urb, &data->tx_anchor);
  283. err = usb_submit_urb(urb, GFP_ATOMIC);
  284. if (err < 0) {
  285. BT_ERR("%s urb %p submission failed", hdev->name, urb);
  286. kfree(urb->setup_packet);
  287. usb_unanchor_urb(urb);
  288. }
  289. usb_free_urb(urb);
  290. return err;
  291. }
  292. static void btusb_destruct(struct hci_dev *hdev)
  293. {
  294. struct btusb_data *data = hdev->driver_data;
  295. BT_DBG("%s", hdev->name);
  296. kfree(data);
  297. }
  298. static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
  299. {
  300. struct btusb_data *data = hdev->driver_data;
  301. BT_DBG("%s evt %d", hdev->name, evt);
  302. if (evt == HCI_NOTIFY_CONN_ADD || evt == HCI_NOTIFY_CONN_DEL)
  303. schedule_work(&data->work);
  304. }
  305. static void btusb_work(struct work_struct *work)
  306. {
  307. struct btusb_data *data = container_of(work, struct btusb_data, work);
  308. struct hci_dev *hdev = data->hdev;
  309. if (hdev->conn_hash.acl_num == 0) {
  310. clear_bit(BTUSB_BULK_RUNNING, &data->flags);
  311. usb_kill_anchored_urbs(&data->bulk_anchor);
  312. return;
  313. }
  314. if (!test_and_set_bit(BTUSB_BULK_RUNNING, &data->flags)) {
  315. if (btusb_submit_bulk_urb(hdev) < 0)
  316. clear_bit(BTUSB_BULK_RUNNING, &data->flags);
  317. else
  318. btusb_submit_bulk_urb(hdev);
  319. }
  320. }
  321. static int btusb_probe(struct usb_interface *intf,
  322. const struct usb_device_id *id)
  323. {
  324. struct usb_endpoint_descriptor *ep_desc;
  325. struct btusb_data *data;
  326. struct hci_dev *hdev;
  327. int i, err;
  328. BT_DBG("intf %p id %p", intf, id);
  329. if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
  330. return -ENODEV;
  331. if (!id->driver_info) {
  332. const struct usb_device_id *match;
  333. match = usb_match_id(intf, blacklist_table);
  334. if (match)
  335. id = match;
  336. }
  337. data = kzalloc(sizeof(*data), GFP_KERNEL);
  338. if (!data)
  339. return -ENOMEM;
  340. for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
  341. ep_desc = &intf->cur_altsetting->endpoint[i].desc;
  342. if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
  343. data->intr_ep = ep_desc;
  344. continue;
  345. }
  346. if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
  347. data->bulk_tx_ep = ep_desc;
  348. continue;
  349. }
  350. if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
  351. data->bulk_rx_ep = ep_desc;
  352. continue;
  353. }
  354. }
  355. if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep) {
  356. kfree(data);
  357. return -ENODEV;
  358. }
  359. data->udev = interface_to_usbdev(intf);
  360. spin_lock_init(&data->lock);
  361. INIT_WORK(&data->work, btusb_work);
  362. init_usb_anchor(&data->tx_anchor);
  363. init_usb_anchor(&data->intr_anchor);
  364. init_usb_anchor(&data->bulk_anchor);
  365. hdev = hci_alloc_dev();
  366. if (!hdev) {
  367. kfree(data);
  368. return -ENOMEM;
  369. }
  370. hdev->type = HCI_USB;
  371. hdev->driver_data = data;
  372. data->hdev = hdev;
  373. SET_HCIDEV_DEV(hdev, &intf->dev);
  374. hdev->open = btusb_open;
  375. hdev->close = btusb_close;
  376. hdev->flush = btusb_flush;
  377. hdev->send = btusb_send_frame;
  378. hdev->destruct = btusb_destruct;
  379. hdev->notify = btusb_notify;
  380. hdev->owner = THIS_MODULE;
  381. set_bit(HCI_QUIRK_RESET_ON_INIT, &hdev->quirks);
  382. err = hci_register_dev(hdev);
  383. if (err < 0) {
  384. hci_free_dev(hdev);
  385. kfree(data);
  386. return err;
  387. }
  388. usb_set_intfdata(intf, data);
  389. return 0;
  390. }
  391. static void btusb_disconnect(struct usb_interface *intf)
  392. {
  393. struct btusb_data *data = usb_get_intfdata(intf);
  394. struct hci_dev *hdev;
  395. BT_DBG("intf %p", intf);
  396. if (!data)
  397. return;
  398. hdev = data->hdev;
  399. usb_set_intfdata(intf, NULL);
  400. hci_unregister_dev(hdev);
  401. hci_free_dev(hdev);
  402. }
  403. static struct usb_driver btusb_driver = {
  404. .name = "btusb",
  405. .probe = btusb_probe,
  406. .disconnect = btusb_disconnect,
  407. .id_table = btusb_table,
  408. };
  409. static int __init btusb_init(void)
  410. {
  411. BT_INFO("Generic Bluetooth USB driver ver %s", VERSION);
  412. return usb_register(&btusb_driver);
  413. }
  414. static void __exit btusb_exit(void)
  415. {
  416. usb_deregister(&btusb_driver);
  417. }
  418. module_init(btusb_init);
  419. module_exit(btusb_exit);
  420. MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
  421. MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
  422. MODULE_VERSION(VERSION);
  423. MODULE_LICENSE("GPL");