bpa10x.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /*
  2. *
  3. * Digianswer Bluetooth USB driver
  4. *
  5. * Copyright (C) 2004-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. #ifndef CONFIG_BT_HCIBPA10X_DEBUG
  35. #undef BT_DBG
  36. #define BT_DBG(D...)
  37. #endif
  38. #define VERSION "0.9"
  39. static int ignore = 0;
  40. static struct usb_device_id bpa10x_table[] = {
  41. /* Tektronix BPA 100/105 (Digianswer) */
  42. { USB_DEVICE(0x08fd, 0x0002) },
  43. { } /* Terminating entry */
  44. };
  45. MODULE_DEVICE_TABLE(usb, bpa10x_table);
  46. struct bpa10x_data {
  47. struct hci_dev *hdev;
  48. struct usb_device *udev;
  49. struct usb_anchor tx_anchor;
  50. struct usb_anchor rx_anchor;
  51. struct sk_buff *rx_skb[2];
  52. };
  53. #define HCI_VENDOR_HDR_SIZE 5
  54. struct hci_vendor_hdr {
  55. __u8 type;
  56. __le16 snum;
  57. __le16 dlen;
  58. } __attribute__ ((packed));
  59. static int bpa10x_recv(struct hci_dev *hdev, int queue, void *buf, int count)
  60. {
  61. struct bpa10x_data *data = hdev->driver_data;
  62. BT_DBG("%s queue %d buffer %p count %d", hdev->name,
  63. queue, buf, count);
  64. if (queue < 0 || queue > 1)
  65. return -EILSEQ;
  66. hdev->stat.byte_rx += count;
  67. while (count) {
  68. struct sk_buff *skb = data->rx_skb[queue];
  69. struct { __u8 type; int expect; } *scb;
  70. int type, len = 0;
  71. if (!skb) {
  72. /* Start of the frame */
  73. type = *((__u8 *) buf);
  74. count--; buf++;
  75. switch (type) {
  76. case HCI_EVENT_PKT:
  77. if (count >= HCI_EVENT_HDR_SIZE) {
  78. struct hci_event_hdr *h = buf;
  79. len = HCI_EVENT_HDR_SIZE + h->plen;
  80. } else
  81. return -EILSEQ;
  82. break;
  83. case HCI_ACLDATA_PKT:
  84. if (count >= HCI_ACL_HDR_SIZE) {
  85. struct hci_acl_hdr *h = buf;
  86. len = HCI_ACL_HDR_SIZE +
  87. __le16_to_cpu(h->dlen);
  88. } else
  89. return -EILSEQ;
  90. break;
  91. case HCI_SCODATA_PKT:
  92. if (count >= HCI_SCO_HDR_SIZE) {
  93. struct hci_sco_hdr *h = buf;
  94. len = HCI_SCO_HDR_SIZE + h->dlen;
  95. } else
  96. return -EILSEQ;
  97. break;
  98. case HCI_VENDOR_PKT:
  99. if (count >= HCI_VENDOR_HDR_SIZE) {
  100. struct hci_vendor_hdr *h = buf;
  101. len = HCI_VENDOR_HDR_SIZE +
  102. __le16_to_cpu(h->dlen);
  103. } else
  104. return -EILSEQ;
  105. break;
  106. }
  107. skb = bt_skb_alloc(len, GFP_ATOMIC);
  108. if (!skb) {
  109. BT_ERR("%s no memory for packet", hdev->name);
  110. return -ENOMEM;
  111. }
  112. skb->dev = (void *) hdev;
  113. data->rx_skb[queue] = skb;
  114. scb = (void *) skb->cb;
  115. scb->type = type;
  116. scb->expect = len;
  117. } else {
  118. /* Continuation */
  119. scb = (void *) skb->cb;
  120. len = scb->expect;
  121. }
  122. len = min(len, count);
  123. memcpy(skb_put(skb, len), buf, len);
  124. scb->expect -= len;
  125. if (scb->expect == 0) {
  126. /* Complete frame */
  127. data->rx_skb[queue] = NULL;
  128. bt_cb(skb)->pkt_type = scb->type;
  129. hci_recv_frame(skb);
  130. }
  131. count -= len; buf += len;
  132. }
  133. return 0;
  134. }
  135. static void bpa10x_tx_complete(struct urb *urb)
  136. {
  137. struct sk_buff *skb = urb->context;
  138. struct hci_dev *hdev = (struct hci_dev *) skb->dev;
  139. BT_DBG("%s urb %p status %d count %d", hdev->name,
  140. urb, urb->status, urb->actual_length);
  141. if (!test_bit(HCI_RUNNING, &hdev->flags))
  142. goto done;
  143. if (!urb->status)
  144. hdev->stat.byte_tx += urb->transfer_buffer_length;
  145. else
  146. hdev->stat.err_tx++;
  147. done:
  148. kfree(urb->setup_packet);
  149. kfree_skb(skb);
  150. }
  151. static void bpa10x_rx_complete(struct urb *urb)
  152. {
  153. struct hci_dev *hdev = urb->context;
  154. struct bpa10x_data *data = hdev->driver_data;
  155. int err;
  156. BT_DBG("%s urb %p status %d count %d", hdev->name,
  157. urb, urb->status, urb->actual_length);
  158. if (!test_bit(HCI_RUNNING, &hdev->flags))
  159. return;
  160. if (urb->status == 0) {
  161. if (bpa10x_recv(hdev, usb_pipebulk(urb->pipe),
  162. urb->transfer_buffer,
  163. urb->actual_length) < 0) {
  164. BT_ERR("%s corrupted event packet", hdev->name);
  165. hdev->stat.err_rx++;
  166. }
  167. }
  168. usb_anchor_urb(urb, &data->rx_anchor);
  169. err = usb_submit_urb(urb, GFP_ATOMIC);
  170. if (err < 0) {
  171. BT_ERR("%s urb %p failed to resubmit (%d)",
  172. hdev->name, urb, -err);
  173. usb_unanchor_urb(urb);
  174. }
  175. }
  176. static inline int bpa10x_submit_intr_urb(struct hci_dev *hdev)
  177. {
  178. struct bpa10x_data *data = hdev->driver_data;
  179. struct urb *urb;
  180. unsigned char *buf;
  181. unsigned int pipe;
  182. int err, size = 16;
  183. BT_DBG("%s", hdev->name);
  184. urb = usb_alloc_urb(0, GFP_KERNEL);
  185. if (!urb)
  186. return -ENOMEM;
  187. buf = kmalloc(size, GFP_KERNEL);
  188. if (!buf) {
  189. usb_free_urb(urb);
  190. return -ENOMEM;
  191. }
  192. pipe = usb_rcvintpipe(data->udev, 0x81);
  193. usb_fill_int_urb(urb, data->udev, pipe, buf, size,
  194. bpa10x_rx_complete, hdev, 1);
  195. urb->transfer_flags |= URB_FREE_BUFFER;
  196. usb_anchor_urb(urb, &data->rx_anchor);
  197. err = usb_submit_urb(urb, GFP_KERNEL);
  198. if (err < 0) {
  199. BT_ERR("%s urb %p submission failed (%d)",
  200. hdev->name, urb, -err);
  201. usb_unanchor_urb(urb);
  202. kfree(buf);
  203. }
  204. usb_free_urb(urb);
  205. return err;
  206. }
  207. static inline int bpa10x_submit_bulk_urb(struct hci_dev *hdev)
  208. {
  209. struct bpa10x_data *data = hdev->driver_data;
  210. struct urb *urb;
  211. unsigned char *buf;
  212. unsigned int pipe;
  213. int err, size = 64;
  214. BT_DBG("%s", hdev->name);
  215. urb = usb_alloc_urb(0, GFP_KERNEL);
  216. if (!urb)
  217. return -ENOMEM;
  218. buf = kmalloc(size, GFP_KERNEL);
  219. if (!buf) {
  220. usb_free_urb(urb);
  221. return -ENOMEM;
  222. }
  223. pipe = usb_rcvbulkpipe(data->udev, 0x82);
  224. usb_fill_bulk_urb(urb, data->udev, pipe,
  225. buf, size, bpa10x_rx_complete, hdev);
  226. urb->transfer_flags |= URB_FREE_BUFFER;
  227. usb_anchor_urb(urb, &data->rx_anchor);
  228. err = usb_submit_urb(urb, GFP_KERNEL);
  229. if (err < 0) {
  230. BT_ERR("%s urb %p submission failed (%d)",
  231. hdev->name, urb, -err);
  232. usb_unanchor_urb(urb);
  233. kfree(buf);
  234. }
  235. usb_free_urb(urb);
  236. return err;
  237. }
  238. static int bpa10x_open(struct hci_dev *hdev)
  239. {
  240. struct bpa10x_data *data = hdev->driver_data;
  241. int err;
  242. BT_DBG("%s", hdev->name);
  243. if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
  244. return 0;
  245. err = bpa10x_submit_intr_urb(hdev);
  246. if (err < 0)
  247. goto error;
  248. err = bpa10x_submit_bulk_urb(hdev);
  249. if (err < 0)
  250. goto error;
  251. return 0;
  252. error:
  253. usb_kill_anchored_urbs(&data->rx_anchor);
  254. clear_bit(HCI_RUNNING, &hdev->flags);
  255. return err;
  256. }
  257. static int bpa10x_close(struct hci_dev *hdev)
  258. {
  259. struct bpa10x_data *data = hdev->driver_data;
  260. BT_DBG("%s", hdev->name);
  261. if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
  262. return 0;
  263. usb_kill_anchored_urbs(&data->rx_anchor);
  264. return 0;
  265. }
  266. static int bpa10x_flush(struct hci_dev *hdev)
  267. {
  268. struct bpa10x_data *data = hdev->driver_data;
  269. BT_DBG("%s", hdev->name);
  270. usb_kill_anchored_urbs(&data->tx_anchor);
  271. return 0;
  272. }
  273. static int bpa10x_send_frame(struct sk_buff *skb)
  274. {
  275. struct hci_dev *hdev = (struct hci_dev *) skb->dev;
  276. struct bpa10x_data *data = hdev->driver_data;
  277. struct usb_ctrlrequest *dr;
  278. struct urb *urb;
  279. unsigned int pipe;
  280. int err;
  281. BT_DBG("%s", hdev->name);
  282. if (!test_bit(HCI_RUNNING, &hdev->flags))
  283. return -EBUSY;
  284. urb = usb_alloc_urb(0, GFP_ATOMIC);
  285. if (!urb)
  286. return -ENOMEM;
  287. /* Prepend skb with frame type */
  288. *skb_push(skb, 1) = bt_cb(skb)->pkt_type;
  289. switch (bt_cb(skb)->pkt_type) {
  290. case HCI_COMMAND_PKT:
  291. dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
  292. if (!dr) {
  293. usb_free_urb(urb);
  294. return -ENOMEM;
  295. }
  296. dr->bRequestType = USB_TYPE_VENDOR;
  297. dr->bRequest = 0;
  298. dr->wIndex = 0;
  299. dr->wValue = 0;
  300. dr->wLength = __cpu_to_le16(skb->len);
  301. pipe = usb_sndctrlpipe(data->udev, 0x00);
  302. usb_fill_control_urb(urb, data->udev, pipe, (void *) dr,
  303. skb->data, skb->len, bpa10x_tx_complete, skb);
  304. hdev->stat.cmd_tx++;
  305. break;
  306. case HCI_ACLDATA_PKT:
  307. pipe = usb_sndbulkpipe(data->udev, 0x02);
  308. usb_fill_bulk_urb(urb, data->udev, pipe,
  309. skb->data, skb->len, bpa10x_tx_complete, skb);
  310. hdev->stat.acl_tx++;
  311. break;
  312. case HCI_SCODATA_PKT:
  313. pipe = usb_sndbulkpipe(data->udev, 0x02);
  314. usb_fill_bulk_urb(urb, data->udev, pipe,
  315. skb->data, skb->len, bpa10x_tx_complete, skb);
  316. hdev->stat.sco_tx++;
  317. break;
  318. default:
  319. return -EILSEQ;
  320. }
  321. usb_anchor_urb(urb, &data->tx_anchor);
  322. err = usb_submit_urb(urb, GFP_ATOMIC);
  323. if (err < 0) {
  324. BT_ERR("%s urb %p submission failed", hdev->name, urb);
  325. kfree(urb->setup_packet);
  326. usb_unanchor_urb(urb);
  327. }
  328. usb_free_urb(urb);
  329. return 0;
  330. }
  331. static void bpa10x_destruct(struct hci_dev *hdev)
  332. {
  333. struct bpa10x_data *data = hdev->driver_data;
  334. BT_DBG("%s", hdev->name);
  335. kfree(data->rx_skb[0]);
  336. kfree(data->rx_skb[1]);
  337. kfree(data);
  338. }
  339. static int bpa10x_probe(struct usb_interface *intf, const struct usb_device_id *id)
  340. {
  341. struct bpa10x_data *data;
  342. struct hci_dev *hdev;
  343. int err;
  344. BT_DBG("intf %p id %p", intf, id);
  345. if (ignore)
  346. return -ENODEV;
  347. if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
  348. return -ENODEV;
  349. data = kzalloc(sizeof(*data), GFP_KERNEL);
  350. if (!data)
  351. return -ENOMEM;
  352. data->udev = interface_to_usbdev(intf);
  353. init_usb_anchor(&data->tx_anchor);
  354. init_usb_anchor(&data->rx_anchor);
  355. hdev = hci_alloc_dev();
  356. if (!hdev) {
  357. kfree(data);
  358. return -ENOMEM;
  359. }
  360. hdev->type = HCI_USB;
  361. hdev->driver_data = data;
  362. data->hdev = hdev;
  363. SET_HCIDEV_DEV(hdev, &intf->dev);
  364. hdev->open = bpa10x_open;
  365. hdev->close = bpa10x_close;
  366. hdev->flush = bpa10x_flush;
  367. hdev->send = bpa10x_send_frame;
  368. hdev->destruct = bpa10x_destruct;
  369. hdev->owner = THIS_MODULE;
  370. err = hci_register_dev(hdev);
  371. if (err < 0) {
  372. hci_free_dev(hdev);
  373. kfree(data);
  374. return err;
  375. }
  376. usb_set_intfdata(intf, data);
  377. return 0;
  378. }
  379. static void bpa10x_disconnect(struct usb_interface *intf)
  380. {
  381. struct bpa10x_data *data = usb_get_intfdata(intf);
  382. BT_DBG("intf %p", intf);
  383. if (!data)
  384. return;
  385. usb_set_intfdata(intf, NULL);
  386. hci_unregister_dev(data->hdev);
  387. hci_free_dev(data->hdev);
  388. }
  389. static struct usb_driver bpa10x_driver = {
  390. .name = "bpa10x",
  391. .probe = bpa10x_probe,
  392. .disconnect = bpa10x_disconnect,
  393. .id_table = bpa10x_table,
  394. };
  395. static int __init bpa10x_init(void)
  396. {
  397. BT_INFO("Digianswer Bluetooth USB driver ver %s", VERSION);
  398. return usb_register(&bpa10x_driver);
  399. }
  400. static void __exit bpa10x_exit(void)
  401. {
  402. usb_deregister(&bpa10x_driver);
  403. }
  404. module_init(bpa10x_init);
  405. module_exit(bpa10x_exit);
  406. module_param(ignore, bool, 0644);
  407. MODULE_PARM_DESC(ignore, "Ignore devices from the matching table");
  408. MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
  409. MODULE_DESCRIPTION("Digianswer Bluetooth USB driver ver " VERSION);
  410. MODULE_VERSION(VERSION);
  411. MODULE_LICENSE("GPL");