bpa10x.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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 const 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 = hci_get_drvdata(hdev);
  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. data->rx_skb[queue] = skb;
  108. scb = (void *) skb->cb;
  109. scb->type = type;
  110. scb->expect = len;
  111. } else {
  112. /* Continuation */
  113. scb = (void *) skb->cb;
  114. len = scb->expect;
  115. }
  116. len = min(len, count);
  117. memcpy(skb_put(skb, len), buf, len);
  118. scb->expect -= len;
  119. if (scb->expect == 0) {
  120. /* Complete frame */
  121. data->rx_skb[queue] = NULL;
  122. bt_cb(skb)->pkt_type = scb->type;
  123. hci_recv_frame(hdev, skb);
  124. }
  125. count -= len; buf += len;
  126. }
  127. return 0;
  128. }
  129. static void bpa10x_tx_complete(struct urb *urb)
  130. {
  131. struct sk_buff *skb = urb->context;
  132. struct hci_dev *hdev = (struct hci_dev *) skb->dev;
  133. BT_DBG("%s urb %p status %d count %d", hdev->name,
  134. urb, urb->status, urb->actual_length);
  135. if (!test_bit(HCI_RUNNING, &hdev->flags))
  136. goto done;
  137. if (!urb->status)
  138. hdev->stat.byte_tx += urb->transfer_buffer_length;
  139. else
  140. hdev->stat.err_tx++;
  141. done:
  142. kfree(urb->setup_packet);
  143. kfree_skb(skb);
  144. }
  145. static void bpa10x_rx_complete(struct urb *urb)
  146. {
  147. struct hci_dev *hdev = urb->context;
  148. struct bpa10x_data *data = hci_get_drvdata(hdev);
  149. int err;
  150. BT_DBG("%s urb %p status %d count %d", hdev->name,
  151. urb, urb->status, urb->actual_length);
  152. if (!test_bit(HCI_RUNNING, &hdev->flags))
  153. return;
  154. if (urb->status == 0) {
  155. if (bpa10x_recv(hdev, usb_pipebulk(urb->pipe),
  156. urb->transfer_buffer,
  157. urb->actual_length) < 0) {
  158. BT_ERR("%s corrupted event packet", hdev->name);
  159. hdev->stat.err_rx++;
  160. }
  161. }
  162. usb_anchor_urb(urb, &data->rx_anchor);
  163. err = usb_submit_urb(urb, GFP_ATOMIC);
  164. if (err < 0) {
  165. BT_ERR("%s urb %p failed to resubmit (%d)",
  166. hdev->name, urb, -err);
  167. usb_unanchor_urb(urb);
  168. }
  169. }
  170. static inline int bpa10x_submit_intr_urb(struct hci_dev *hdev)
  171. {
  172. struct bpa10x_data *data = hci_get_drvdata(hdev);
  173. struct urb *urb;
  174. unsigned char *buf;
  175. unsigned int pipe;
  176. int err, size = 16;
  177. BT_DBG("%s", hdev->name);
  178. urb = usb_alloc_urb(0, GFP_KERNEL);
  179. if (!urb)
  180. return -ENOMEM;
  181. buf = kmalloc(size, GFP_KERNEL);
  182. if (!buf) {
  183. usb_free_urb(urb);
  184. return -ENOMEM;
  185. }
  186. pipe = usb_rcvintpipe(data->udev, 0x81);
  187. usb_fill_int_urb(urb, data->udev, pipe, buf, size,
  188. bpa10x_rx_complete, hdev, 1);
  189. urb->transfer_flags |= URB_FREE_BUFFER;
  190. usb_anchor_urb(urb, &data->rx_anchor);
  191. err = usb_submit_urb(urb, GFP_KERNEL);
  192. if (err < 0) {
  193. BT_ERR("%s urb %p submission failed (%d)",
  194. hdev->name, urb, -err);
  195. usb_unanchor_urb(urb);
  196. }
  197. usb_free_urb(urb);
  198. return err;
  199. }
  200. static inline int bpa10x_submit_bulk_urb(struct hci_dev *hdev)
  201. {
  202. struct bpa10x_data *data = hci_get_drvdata(hdev);
  203. struct urb *urb;
  204. unsigned char *buf;
  205. unsigned int pipe;
  206. int err, size = 64;
  207. BT_DBG("%s", hdev->name);
  208. urb = usb_alloc_urb(0, GFP_KERNEL);
  209. if (!urb)
  210. return -ENOMEM;
  211. buf = kmalloc(size, GFP_KERNEL);
  212. if (!buf) {
  213. usb_free_urb(urb);
  214. return -ENOMEM;
  215. }
  216. pipe = usb_rcvbulkpipe(data->udev, 0x82);
  217. usb_fill_bulk_urb(urb, data->udev, pipe,
  218. buf, size, bpa10x_rx_complete, hdev);
  219. urb->transfer_flags |= URB_FREE_BUFFER;
  220. usb_anchor_urb(urb, &data->rx_anchor);
  221. err = usb_submit_urb(urb, GFP_KERNEL);
  222. if (err < 0) {
  223. BT_ERR("%s urb %p submission failed (%d)",
  224. hdev->name, urb, -err);
  225. usb_unanchor_urb(urb);
  226. }
  227. usb_free_urb(urb);
  228. return err;
  229. }
  230. static int bpa10x_open(struct hci_dev *hdev)
  231. {
  232. struct bpa10x_data *data = hci_get_drvdata(hdev);
  233. int err;
  234. BT_DBG("%s", hdev->name);
  235. if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
  236. return 0;
  237. err = bpa10x_submit_intr_urb(hdev);
  238. if (err < 0)
  239. goto error;
  240. err = bpa10x_submit_bulk_urb(hdev);
  241. if (err < 0)
  242. goto error;
  243. return 0;
  244. error:
  245. usb_kill_anchored_urbs(&data->rx_anchor);
  246. clear_bit(HCI_RUNNING, &hdev->flags);
  247. return err;
  248. }
  249. static int bpa10x_close(struct hci_dev *hdev)
  250. {
  251. struct bpa10x_data *data = hci_get_drvdata(hdev);
  252. BT_DBG("%s", hdev->name);
  253. if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
  254. return 0;
  255. usb_kill_anchored_urbs(&data->rx_anchor);
  256. return 0;
  257. }
  258. static int bpa10x_flush(struct hci_dev *hdev)
  259. {
  260. struct bpa10x_data *data = hci_get_drvdata(hdev);
  261. BT_DBG("%s", hdev->name);
  262. usb_kill_anchored_urbs(&data->tx_anchor);
  263. return 0;
  264. }
  265. static int bpa10x_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
  266. {
  267. struct bpa10x_data *data = hci_get_drvdata(hdev);
  268. struct usb_ctrlrequest *dr;
  269. struct urb *urb;
  270. unsigned int pipe;
  271. int err;
  272. BT_DBG("%s", hdev->name);
  273. if (!test_bit(HCI_RUNNING, &hdev->flags))
  274. return -EBUSY;
  275. skb->dev = (void *) hdev;
  276. urb = usb_alloc_urb(0, GFP_ATOMIC);
  277. if (!urb)
  278. return -ENOMEM;
  279. /* Prepend skb with frame type */
  280. *skb_push(skb, 1) = bt_cb(skb)->pkt_type;
  281. switch (bt_cb(skb)->pkt_type) {
  282. case HCI_COMMAND_PKT:
  283. dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
  284. if (!dr) {
  285. usb_free_urb(urb);
  286. return -ENOMEM;
  287. }
  288. dr->bRequestType = USB_TYPE_VENDOR;
  289. dr->bRequest = 0;
  290. dr->wIndex = 0;
  291. dr->wValue = 0;
  292. dr->wLength = __cpu_to_le16(skb->len);
  293. pipe = usb_sndctrlpipe(data->udev, 0x00);
  294. usb_fill_control_urb(urb, data->udev, pipe, (void *) dr,
  295. skb->data, skb->len, bpa10x_tx_complete, skb);
  296. hdev->stat.cmd_tx++;
  297. break;
  298. case HCI_ACLDATA_PKT:
  299. pipe = usb_sndbulkpipe(data->udev, 0x02);
  300. usb_fill_bulk_urb(urb, data->udev, pipe,
  301. skb->data, skb->len, bpa10x_tx_complete, skb);
  302. hdev->stat.acl_tx++;
  303. break;
  304. case HCI_SCODATA_PKT:
  305. pipe = usb_sndbulkpipe(data->udev, 0x02);
  306. usb_fill_bulk_urb(urb, data->udev, pipe,
  307. skb->data, skb->len, bpa10x_tx_complete, skb);
  308. hdev->stat.sco_tx++;
  309. break;
  310. default:
  311. usb_free_urb(urb);
  312. return -EILSEQ;
  313. }
  314. usb_anchor_urb(urb, &data->tx_anchor);
  315. err = usb_submit_urb(urb, GFP_ATOMIC);
  316. if (err < 0) {
  317. BT_ERR("%s urb %p submission failed", hdev->name, urb);
  318. kfree(urb->setup_packet);
  319. usb_unanchor_urb(urb);
  320. }
  321. usb_free_urb(urb);
  322. return 0;
  323. }
  324. static int bpa10x_probe(struct usb_interface *intf, const struct usb_device_id *id)
  325. {
  326. struct bpa10x_data *data;
  327. struct hci_dev *hdev;
  328. int err;
  329. BT_DBG("intf %p id %p", intf, id);
  330. if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
  331. return -ENODEV;
  332. data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
  333. if (!data)
  334. return -ENOMEM;
  335. data->udev = interface_to_usbdev(intf);
  336. init_usb_anchor(&data->tx_anchor);
  337. init_usb_anchor(&data->rx_anchor);
  338. hdev = hci_alloc_dev();
  339. if (!hdev)
  340. return -ENOMEM;
  341. hdev->bus = HCI_USB;
  342. hci_set_drvdata(hdev, data);
  343. data->hdev = hdev;
  344. SET_HCIDEV_DEV(hdev, &intf->dev);
  345. hdev->open = bpa10x_open;
  346. hdev->close = bpa10x_close;
  347. hdev->flush = bpa10x_flush;
  348. hdev->send = bpa10x_send_frame;
  349. set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
  350. err = hci_register_dev(hdev);
  351. if (err < 0) {
  352. hci_free_dev(hdev);
  353. return err;
  354. }
  355. usb_set_intfdata(intf, data);
  356. return 0;
  357. }
  358. static void bpa10x_disconnect(struct usb_interface *intf)
  359. {
  360. struct bpa10x_data *data = usb_get_intfdata(intf);
  361. BT_DBG("intf %p", intf);
  362. if (!data)
  363. return;
  364. usb_set_intfdata(intf, NULL);
  365. hci_unregister_dev(data->hdev);
  366. hci_free_dev(data->hdev);
  367. kfree_skb(data->rx_skb[0]);
  368. kfree_skb(data->rx_skb[1]);
  369. }
  370. static struct usb_driver bpa10x_driver = {
  371. .name = "bpa10x",
  372. .probe = bpa10x_probe,
  373. .disconnect = bpa10x_disconnect,
  374. .id_table = bpa10x_table,
  375. .disable_hub_initiated_lpm = 1,
  376. };
  377. module_usb_driver(bpa10x_driver);
  378. MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
  379. MODULE_DESCRIPTION("Digianswer Bluetooth USB driver ver " VERSION);
  380. MODULE_VERSION(VERSION);
  381. MODULE_LICENSE("GPL");