bpa10x.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. #include "hci_uart.h"
  35. #define VERSION "0.11"
  36. static const struct usb_device_id bpa10x_table[] = {
  37. /* Tektronix BPA 100/105 (Digianswer) */
  38. { USB_DEVICE(0x08fd, 0x0002) },
  39. { } /* Terminating entry */
  40. };
  41. MODULE_DEVICE_TABLE(usb, bpa10x_table);
  42. struct bpa10x_data {
  43. struct hci_dev *hdev;
  44. struct usb_device *udev;
  45. struct usb_anchor tx_anchor;
  46. struct usb_anchor rx_anchor;
  47. struct sk_buff *rx_skb[2];
  48. };
  49. static void bpa10x_tx_complete(struct urb *urb)
  50. {
  51. struct sk_buff *skb = urb->context;
  52. struct hci_dev *hdev = (struct hci_dev *) skb->dev;
  53. BT_DBG("%s urb %p status %d count %d", hdev->name,
  54. urb, urb->status, urb->actual_length);
  55. if (!test_bit(HCI_RUNNING, &hdev->flags))
  56. goto done;
  57. if (!urb->status)
  58. hdev->stat.byte_tx += urb->transfer_buffer_length;
  59. else
  60. hdev->stat.err_tx++;
  61. done:
  62. kfree(urb->setup_packet);
  63. kfree_skb(skb);
  64. }
  65. #define HCI_VENDOR_HDR_SIZE 5
  66. #define HCI_RECV_VENDOR \
  67. .type = HCI_VENDOR_PKT, \
  68. .hlen = HCI_VENDOR_HDR_SIZE, \
  69. .loff = 3, \
  70. .lsize = 2, \
  71. .maxlen = HCI_MAX_FRAME_SIZE
  72. static const struct h4_recv_pkt bpa10x_recv_pkts[] = {
  73. { H4_RECV_ACL, .recv = hci_recv_frame },
  74. { H4_RECV_SCO, .recv = hci_recv_frame },
  75. { H4_RECV_EVENT, .recv = hci_recv_frame },
  76. { HCI_RECV_VENDOR, .recv = hci_recv_diag },
  77. };
  78. static void bpa10x_rx_complete(struct urb *urb)
  79. {
  80. struct hci_dev *hdev = urb->context;
  81. struct bpa10x_data *data = hci_get_drvdata(hdev);
  82. int err;
  83. BT_DBG("%s urb %p status %d count %d", hdev->name,
  84. urb, urb->status, urb->actual_length);
  85. if (!test_bit(HCI_RUNNING, &hdev->flags))
  86. return;
  87. if (urb->status == 0) {
  88. bool idx = usb_pipebulk(urb->pipe);
  89. data->rx_skb[idx] = h4_recv_buf(hdev, data->rx_skb[idx],
  90. urb->transfer_buffer,
  91. urb->actual_length,
  92. bpa10x_recv_pkts,
  93. ARRAY_SIZE(bpa10x_recv_pkts));
  94. if (IS_ERR(data->rx_skb[idx])) {
  95. BT_ERR("%s corrupted event packet", hdev->name);
  96. hdev->stat.err_rx++;
  97. data->rx_skb[idx] = NULL;
  98. }
  99. }
  100. usb_anchor_urb(urb, &data->rx_anchor);
  101. err = usb_submit_urb(urb, GFP_ATOMIC);
  102. if (err < 0) {
  103. BT_ERR("%s urb %p failed to resubmit (%d)",
  104. hdev->name, urb, -err);
  105. usb_unanchor_urb(urb);
  106. }
  107. }
  108. static inline int bpa10x_submit_intr_urb(struct hci_dev *hdev)
  109. {
  110. struct bpa10x_data *data = hci_get_drvdata(hdev);
  111. struct urb *urb;
  112. unsigned char *buf;
  113. unsigned int pipe;
  114. int err, size = 16;
  115. BT_DBG("%s", hdev->name);
  116. urb = usb_alloc_urb(0, GFP_KERNEL);
  117. if (!urb)
  118. return -ENOMEM;
  119. buf = kmalloc(size, GFP_KERNEL);
  120. if (!buf) {
  121. usb_free_urb(urb);
  122. return -ENOMEM;
  123. }
  124. pipe = usb_rcvintpipe(data->udev, 0x81);
  125. usb_fill_int_urb(urb, data->udev, pipe, buf, size,
  126. bpa10x_rx_complete, hdev, 1);
  127. urb->transfer_flags |= URB_FREE_BUFFER;
  128. usb_anchor_urb(urb, &data->rx_anchor);
  129. err = usb_submit_urb(urb, GFP_KERNEL);
  130. if (err < 0) {
  131. BT_ERR("%s urb %p submission failed (%d)",
  132. hdev->name, urb, -err);
  133. usb_unanchor_urb(urb);
  134. }
  135. usb_free_urb(urb);
  136. return err;
  137. }
  138. static inline int bpa10x_submit_bulk_urb(struct hci_dev *hdev)
  139. {
  140. struct bpa10x_data *data = hci_get_drvdata(hdev);
  141. struct urb *urb;
  142. unsigned char *buf;
  143. unsigned int pipe;
  144. int err, size = 64;
  145. BT_DBG("%s", hdev->name);
  146. urb = usb_alloc_urb(0, GFP_KERNEL);
  147. if (!urb)
  148. return -ENOMEM;
  149. buf = kmalloc(size, GFP_KERNEL);
  150. if (!buf) {
  151. usb_free_urb(urb);
  152. return -ENOMEM;
  153. }
  154. pipe = usb_rcvbulkpipe(data->udev, 0x82);
  155. usb_fill_bulk_urb(urb, data->udev, pipe,
  156. buf, size, bpa10x_rx_complete, hdev);
  157. urb->transfer_flags |= URB_FREE_BUFFER;
  158. usb_anchor_urb(urb, &data->rx_anchor);
  159. err = usb_submit_urb(urb, GFP_KERNEL);
  160. if (err < 0) {
  161. BT_ERR("%s urb %p submission failed (%d)",
  162. hdev->name, urb, -err);
  163. usb_unanchor_urb(urb);
  164. }
  165. usb_free_urb(urb);
  166. return err;
  167. }
  168. static int bpa10x_open(struct hci_dev *hdev)
  169. {
  170. struct bpa10x_data *data = hci_get_drvdata(hdev);
  171. int err;
  172. BT_DBG("%s", hdev->name);
  173. err = bpa10x_submit_intr_urb(hdev);
  174. if (err < 0)
  175. goto error;
  176. err = bpa10x_submit_bulk_urb(hdev);
  177. if (err < 0)
  178. goto error;
  179. return 0;
  180. error:
  181. usb_kill_anchored_urbs(&data->rx_anchor);
  182. return err;
  183. }
  184. static int bpa10x_close(struct hci_dev *hdev)
  185. {
  186. struct bpa10x_data *data = hci_get_drvdata(hdev);
  187. BT_DBG("%s", hdev->name);
  188. usb_kill_anchored_urbs(&data->rx_anchor);
  189. return 0;
  190. }
  191. static int bpa10x_flush(struct hci_dev *hdev)
  192. {
  193. struct bpa10x_data *data = hci_get_drvdata(hdev);
  194. BT_DBG("%s", hdev->name);
  195. usb_kill_anchored_urbs(&data->tx_anchor);
  196. return 0;
  197. }
  198. static int bpa10x_setup(struct hci_dev *hdev)
  199. {
  200. const u8 req[] = { 0x07 };
  201. struct sk_buff *skb;
  202. BT_DBG("%s", hdev->name);
  203. /* Read revision string */
  204. skb = __hci_cmd_sync(hdev, 0xfc0e, sizeof(req), req, HCI_INIT_TIMEOUT);
  205. if (IS_ERR(skb))
  206. return PTR_ERR(skb);
  207. BT_INFO("%s: %s", hdev->name, (char *)(skb->data + 1));
  208. kfree_skb(skb);
  209. return 0;
  210. }
  211. static int bpa10x_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
  212. {
  213. struct bpa10x_data *data = hci_get_drvdata(hdev);
  214. struct usb_ctrlrequest *dr;
  215. struct urb *urb;
  216. unsigned int pipe;
  217. int err;
  218. BT_DBG("%s", hdev->name);
  219. skb->dev = (void *) hdev;
  220. urb = usb_alloc_urb(0, GFP_ATOMIC);
  221. if (!urb)
  222. return -ENOMEM;
  223. /* Prepend skb with frame type */
  224. *skb_push(skb, 1) = bt_cb(skb)->pkt_type;
  225. switch (bt_cb(skb)->pkt_type) {
  226. case HCI_COMMAND_PKT:
  227. dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
  228. if (!dr) {
  229. usb_free_urb(urb);
  230. return -ENOMEM;
  231. }
  232. dr->bRequestType = USB_TYPE_VENDOR;
  233. dr->bRequest = 0;
  234. dr->wIndex = 0;
  235. dr->wValue = 0;
  236. dr->wLength = __cpu_to_le16(skb->len);
  237. pipe = usb_sndctrlpipe(data->udev, 0x00);
  238. usb_fill_control_urb(urb, data->udev, pipe, (void *) dr,
  239. skb->data, skb->len, bpa10x_tx_complete, skb);
  240. hdev->stat.cmd_tx++;
  241. break;
  242. case HCI_ACLDATA_PKT:
  243. pipe = usb_sndbulkpipe(data->udev, 0x02);
  244. usb_fill_bulk_urb(urb, data->udev, pipe,
  245. skb->data, skb->len, bpa10x_tx_complete, skb);
  246. hdev->stat.acl_tx++;
  247. break;
  248. case HCI_SCODATA_PKT:
  249. pipe = usb_sndbulkpipe(data->udev, 0x02);
  250. usb_fill_bulk_urb(urb, data->udev, pipe,
  251. skb->data, skb->len, bpa10x_tx_complete, skb);
  252. hdev->stat.sco_tx++;
  253. break;
  254. default:
  255. usb_free_urb(urb);
  256. return -EILSEQ;
  257. }
  258. usb_anchor_urb(urb, &data->tx_anchor);
  259. err = usb_submit_urb(urb, GFP_ATOMIC);
  260. if (err < 0) {
  261. BT_ERR("%s urb %p submission failed", hdev->name, urb);
  262. kfree(urb->setup_packet);
  263. usb_unanchor_urb(urb);
  264. }
  265. usb_free_urb(urb);
  266. return 0;
  267. }
  268. static int bpa10x_set_diag(struct hci_dev *hdev, bool enable)
  269. {
  270. const u8 req[] = { 0x00, enable };
  271. struct sk_buff *skb;
  272. BT_DBG("%s", hdev->name);
  273. if (!test_bit(HCI_RUNNING, &hdev->flags))
  274. return -ENETDOWN;
  275. /* Enable sniffer operation */
  276. skb = __hci_cmd_sync(hdev, 0xfc0e, sizeof(req), req, HCI_INIT_TIMEOUT);
  277. if (IS_ERR(skb))
  278. return PTR_ERR(skb);
  279. kfree_skb(skb);
  280. return 0;
  281. }
  282. static int bpa10x_probe(struct usb_interface *intf, const struct usb_device_id *id)
  283. {
  284. struct bpa10x_data *data;
  285. struct hci_dev *hdev;
  286. int err;
  287. BT_DBG("intf %p id %p", intf, id);
  288. if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
  289. return -ENODEV;
  290. data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
  291. if (!data)
  292. return -ENOMEM;
  293. data->udev = interface_to_usbdev(intf);
  294. init_usb_anchor(&data->tx_anchor);
  295. init_usb_anchor(&data->rx_anchor);
  296. hdev = hci_alloc_dev();
  297. if (!hdev)
  298. return -ENOMEM;
  299. hdev->bus = HCI_USB;
  300. hci_set_drvdata(hdev, data);
  301. data->hdev = hdev;
  302. SET_HCIDEV_DEV(hdev, &intf->dev);
  303. hdev->open = bpa10x_open;
  304. hdev->close = bpa10x_close;
  305. hdev->flush = bpa10x_flush;
  306. hdev->setup = bpa10x_setup;
  307. hdev->send = bpa10x_send_frame;
  308. hdev->set_diag = bpa10x_set_diag;
  309. set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
  310. err = hci_register_dev(hdev);
  311. if (err < 0) {
  312. hci_free_dev(hdev);
  313. return err;
  314. }
  315. usb_set_intfdata(intf, data);
  316. return 0;
  317. }
  318. static void bpa10x_disconnect(struct usb_interface *intf)
  319. {
  320. struct bpa10x_data *data = usb_get_intfdata(intf);
  321. BT_DBG("intf %p", intf);
  322. if (!data)
  323. return;
  324. usb_set_intfdata(intf, NULL);
  325. hci_unregister_dev(data->hdev);
  326. hci_free_dev(data->hdev);
  327. kfree_skb(data->rx_skb[0]);
  328. kfree_skb(data->rx_skb[1]);
  329. }
  330. static struct usb_driver bpa10x_driver = {
  331. .name = "bpa10x",
  332. .probe = bpa10x_probe,
  333. .disconnect = bpa10x_disconnect,
  334. .id_table = bpa10x_table,
  335. .disable_hub_initiated_lpm = 1,
  336. };
  337. module_usb_driver(bpa10x_driver);
  338. MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
  339. MODULE_DESCRIPTION("Digianswer Bluetooth USB driver ver " VERSION);
  340. MODULE_VERSION(VERSION);
  341. MODULE_LICENSE("GPL");