bpa10x.c 11 KB

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