btusb.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  1. /*
  2. *
  3. * Generic Bluetooth USB driver
  4. *
  5. * Copyright (C) 2005-2008 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.4"
  40. static int ignore_dga;
  41. static int ignore_csr;
  42. static int ignore_sniffer;
  43. static int disable_scofix;
  44. static int force_scofix;
  45. static int reset = 1;
  46. static struct usb_driver btusb_driver;
  47. #define BTUSB_IGNORE 0x01
  48. #define BTUSB_DIGIANSWER 0x02
  49. #define BTUSB_CSR 0x04
  50. #define BTUSB_SNIFFER 0x08
  51. #define BTUSB_BCM92035 0x10
  52. #define BTUSB_BROKEN_ISOC 0x20
  53. #define BTUSB_WRONG_SCO_MTU 0x40
  54. static struct usb_device_id btusb_table[] = {
  55. /* Generic Bluetooth USB device */
  56. { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
  57. /* AVM BlueFRITZ! USB v2.0 */
  58. { USB_DEVICE(0x057c, 0x3800) },
  59. /* Bluetooth Ultraport Module from IBM */
  60. { USB_DEVICE(0x04bf, 0x030a) },
  61. /* ALPS Modules with non-standard id */
  62. { USB_DEVICE(0x044e, 0x3001) },
  63. { USB_DEVICE(0x044e, 0x3002) },
  64. /* Ericsson with non-standard id */
  65. { USB_DEVICE(0x0bdb, 0x1002) },
  66. /* Canyon CN-BTU1 with HID interfaces */
  67. { USB_DEVICE(0x0c10, 0x0000) },
  68. { } /* Terminating entry */
  69. };
  70. MODULE_DEVICE_TABLE(usb, btusb_table);
  71. static struct usb_device_id blacklist_table[] = {
  72. /* CSR BlueCore devices */
  73. { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
  74. /* Broadcom BCM2033 without firmware */
  75. { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
  76. /* Broadcom BCM2035 */
  77. { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
  78. { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
  79. { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
  80. /* Broadcom BCM2045 */
  81. { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
  82. { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
  83. /* IBM/Lenovo ThinkPad with Broadcom chip */
  84. { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
  85. { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
  86. /* HP laptop with Broadcom chip */
  87. { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
  88. /* Dell laptop with Broadcom chip */
  89. { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
  90. /* Dell Wireless 370 and 410 devices */
  91. { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
  92. { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
  93. /* Belkin F8T012 and F8T013 devices */
  94. { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
  95. { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
  96. /* Asus WL-BTD202 device */
  97. { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
  98. /* Kensington Bluetooth USB adapter */
  99. { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
  100. /* RTX Telecom based adapters with buggy SCO support */
  101. { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
  102. { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
  103. /* CONWISE Technology based adapters with buggy SCO support */
  104. { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
  105. /* Digianswer devices */
  106. { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
  107. { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
  108. /* CSR BlueCore Bluetooth Sniffer */
  109. { USB_DEVICE(0x0a12, 0x0002), .driver_info = BTUSB_SNIFFER },
  110. /* Frontline ComProbe Bluetooth Sniffer */
  111. { USB_DEVICE(0x16d3, 0x0002), .driver_info = BTUSB_SNIFFER },
  112. { } /* Terminating entry */
  113. };
  114. #define BTUSB_MAX_ISOC_FRAMES 10
  115. #define BTUSB_INTR_RUNNING 0
  116. #define BTUSB_BULK_RUNNING 1
  117. #define BTUSB_ISOC_RUNNING 2
  118. struct btusb_data {
  119. struct hci_dev *hdev;
  120. struct usb_device *udev;
  121. struct usb_interface *intf;
  122. struct usb_interface *isoc;
  123. spinlock_t lock;
  124. unsigned long flags;
  125. struct work_struct work;
  126. struct usb_anchor tx_anchor;
  127. struct usb_anchor intr_anchor;
  128. struct usb_anchor bulk_anchor;
  129. struct usb_anchor isoc_anchor;
  130. struct usb_endpoint_descriptor *intr_ep;
  131. struct usb_endpoint_descriptor *bulk_tx_ep;
  132. struct usb_endpoint_descriptor *bulk_rx_ep;
  133. struct usb_endpoint_descriptor *isoc_tx_ep;
  134. struct usb_endpoint_descriptor *isoc_rx_ep;
  135. __u8 cmdreq_type;
  136. int isoc_altsetting;
  137. int suspend_count;
  138. };
  139. static void btusb_intr_complete(struct urb *urb)
  140. {
  141. struct hci_dev *hdev = urb->context;
  142. struct btusb_data *data = hdev->driver_data;
  143. int err;
  144. BT_DBG("%s urb %p status %d count %d", hdev->name,
  145. urb, urb->status, urb->actual_length);
  146. if (!test_bit(HCI_RUNNING, &hdev->flags))
  147. return;
  148. if (urb->status == 0) {
  149. hdev->stat.byte_rx += urb->actual_length;
  150. if (hci_recv_fragment(hdev, HCI_EVENT_PKT,
  151. urb->transfer_buffer,
  152. urb->actual_length) < 0) {
  153. BT_ERR("%s corrupted event packet", hdev->name);
  154. hdev->stat.err_rx++;
  155. }
  156. }
  157. if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
  158. return;
  159. usb_anchor_urb(urb, &data->intr_anchor);
  160. err = usb_submit_urb(urb, GFP_ATOMIC);
  161. if (err < 0) {
  162. BT_ERR("%s urb %p failed to resubmit (%d)",
  163. hdev->name, urb, -err);
  164. usb_unanchor_urb(urb);
  165. }
  166. }
  167. static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
  168. {
  169. struct btusb_data *data = hdev->driver_data;
  170. struct urb *urb;
  171. unsigned char *buf;
  172. unsigned int pipe;
  173. int err, size;
  174. BT_DBG("%s", hdev->name);
  175. if (!data->intr_ep)
  176. return -ENODEV;
  177. urb = usb_alloc_urb(0, mem_flags);
  178. if (!urb)
  179. return -ENOMEM;
  180. size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
  181. buf = kmalloc(size, mem_flags);
  182. if (!buf) {
  183. usb_free_urb(urb);
  184. return -ENOMEM;
  185. }
  186. pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
  187. usb_fill_int_urb(urb, data->udev, pipe, buf, size,
  188. btusb_intr_complete, hdev,
  189. data->intr_ep->bInterval);
  190. urb->transfer_flags |= URB_FREE_BUFFER;
  191. usb_anchor_urb(urb, &data->intr_anchor);
  192. err = usb_submit_urb(urb, mem_flags);
  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 void btusb_bulk_complete(struct urb *urb)
  202. {
  203. struct hci_dev *hdev = urb->context;
  204. struct btusb_data *data = hdev->driver_data;
  205. int err;
  206. BT_DBG("%s urb %p status %d count %d", hdev->name,
  207. urb, urb->status, urb->actual_length);
  208. if (!test_bit(HCI_RUNNING, &hdev->flags))
  209. return;
  210. if (urb->status == 0) {
  211. hdev->stat.byte_rx += urb->actual_length;
  212. if (hci_recv_fragment(hdev, HCI_ACLDATA_PKT,
  213. urb->transfer_buffer,
  214. urb->actual_length) < 0) {
  215. BT_ERR("%s corrupted ACL packet", hdev->name);
  216. hdev->stat.err_rx++;
  217. }
  218. }
  219. if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
  220. return;
  221. usb_anchor_urb(urb, &data->bulk_anchor);
  222. err = usb_submit_urb(urb, GFP_ATOMIC);
  223. if (err < 0) {
  224. BT_ERR("%s urb %p failed to resubmit (%d)",
  225. hdev->name, urb, -err);
  226. usb_unanchor_urb(urb);
  227. }
  228. }
  229. static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
  230. {
  231. struct btusb_data *data = hdev->driver_data;
  232. struct urb *urb;
  233. unsigned char *buf;
  234. unsigned int pipe;
  235. int err, size;
  236. BT_DBG("%s", hdev->name);
  237. if (!data->bulk_rx_ep)
  238. return -ENODEV;
  239. urb = usb_alloc_urb(0, mem_flags);
  240. if (!urb)
  241. return -ENOMEM;
  242. size = le16_to_cpu(data->bulk_rx_ep->wMaxPacketSize);
  243. buf = kmalloc(size, mem_flags);
  244. if (!buf) {
  245. usb_free_urb(urb);
  246. return -ENOMEM;
  247. }
  248. pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
  249. usb_fill_bulk_urb(urb, data->udev, pipe,
  250. buf, size, btusb_bulk_complete, hdev);
  251. urb->transfer_flags |= URB_FREE_BUFFER;
  252. usb_anchor_urb(urb, &data->bulk_anchor);
  253. err = usb_submit_urb(urb, mem_flags);
  254. if (err < 0) {
  255. BT_ERR("%s urb %p submission failed (%d)",
  256. hdev->name, urb, -err);
  257. usb_unanchor_urb(urb);
  258. }
  259. usb_free_urb(urb);
  260. return err;
  261. }
  262. static void btusb_isoc_complete(struct urb *urb)
  263. {
  264. struct hci_dev *hdev = urb->context;
  265. struct btusb_data *data = hdev->driver_data;
  266. int i, err;
  267. BT_DBG("%s urb %p status %d count %d", hdev->name,
  268. urb, urb->status, urb->actual_length);
  269. if (!test_bit(HCI_RUNNING, &hdev->flags))
  270. return;
  271. if (urb->status == 0) {
  272. for (i = 0; i < urb->number_of_packets; i++) {
  273. unsigned int offset = urb->iso_frame_desc[i].offset;
  274. unsigned int length = urb->iso_frame_desc[i].actual_length;
  275. if (urb->iso_frame_desc[i].status)
  276. continue;
  277. hdev->stat.byte_rx += length;
  278. if (hci_recv_fragment(hdev, HCI_SCODATA_PKT,
  279. urb->transfer_buffer + offset,
  280. length) < 0) {
  281. BT_ERR("%s corrupted SCO packet", hdev->name);
  282. hdev->stat.err_rx++;
  283. }
  284. }
  285. }
  286. if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
  287. return;
  288. usb_anchor_urb(urb, &data->isoc_anchor);
  289. err = usb_submit_urb(urb, GFP_ATOMIC);
  290. if (err < 0) {
  291. BT_ERR("%s urb %p failed to resubmit (%d)",
  292. hdev->name, urb, -err);
  293. usb_unanchor_urb(urb);
  294. }
  295. }
  296. static void inline __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
  297. {
  298. int i, offset = 0;
  299. BT_DBG("len %d mtu %d", len, mtu);
  300. for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
  301. i++, offset += mtu, len -= mtu) {
  302. urb->iso_frame_desc[i].offset = offset;
  303. urb->iso_frame_desc[i].length = mtu;
  304. }
  305. if (len && i < BTUSB_MAX_ISOC_FRAMES) {
  306. urb->iso_frame_desc[i].offset = offset;
  307. urb->iso_frame_desc[i].length = len;
  308. i++;
  309. }
  310. urb->number_of_packets = i;
  311. }
  312. static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
  313. {
  314. struct btusb_data *data = hdev->driver_data;
  315. struct urb *urb;
  316. unsigned char *buf;
  317. unsigned int pipe;
  318. int err, size;
  319. BT_DBG("%s", hdev->name);
  320. if (!data->isoc_rx_ep)
  321. return -ENODEV;
  322. urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
  323. if (!urb)
  324. return -ENOMEM;
  325. size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
  326. BTUSB_MAX_ISOC_FRAMES;
  327. buf = kmalloc(size, mem_flags);
  328. if (!buf) {
  329. usb_free_urb(urb);
  330. return -ENOMEM;
  331. }
  332. pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
  333. urb->dev = data->udev;
  334. urb->pipe = pipe;
  335. urb->context = hdev;
  336. urb->complete = btusb_isoc_complete;
  337. urb->interval = data->isoc_rx_ep->bInterval;
  338. urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
  339. urb->transfer_buffer = buf;
  340. urb->transfer_buffer_length = size;
  341. __fill_isoc_descriptor(urb, size,
  342. le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
  343. usb_anchor_urb(urb, &data->isoc_anchor);
  344. err = usb_submit_urb(urb, mem_flags);
  345. if (err < 0) {
  346. BT_ERR("%s urb %p submission failed (%d)",
  347. hdev->name, urb, -err);
  348. usb_unanchor_urb(urb);
  349. }
  350. usb_free_urb(urb);
  351. return err;
  352. }
  353. static void btusb_tx_complete(struct urb *urb)
  354. {
  355. struct sk_buff *skb = urb->context;
  356. struct hci_dev *hdev = (struct hci_dev *) skb->dev;
  357. BT_DBG("%s urb %p status %d count %d", hdev->name,
  358. urb, urb->status, urb->actual_length);
  359. if (!test_bit(HCI_RUNNING, &hdev->flags))
  360. goto done;
  361. if (!urb->status)
  362. hdev->stat.byte_tx += urb->transfer_buffer_length;
  363. else
  364. hdev->stat.err_tx++;
  365. done:
  366. kfree(urb->setup_packet);
  367. kfree_skb(skb);
  368. }
  369. static int btusb_open(struct hci_dev *hdev)
  370. {
  371. struct btusb_data *data = hdev->driver_data;
  372. int err;
  373. BT_DBG("%s", hdev->name);
  374. if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
  375. return 0;
  376. if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
  377. return 0;
  378. err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
  379. if (err < 0) {
  380. clear_bit(BTUSB_INTR_RUNNING, &data->flags);
  381. clear_bit(HCI_RUNNING, &hdev->flags);
  382. }
  383. return err;
  384. }
  385. static int btusb_close(struct hci_dev *hdev)
  386. {
  387. struct btusb_data *data = hdev->driver_data;
  388. BT_DBG("%s", hdev->name);
  389. if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
  390. return 0;
  391. cancel_work_sync(&data->work);
  392. clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
  393. usb_kill_anchored_urbs(&data->isoc_anchor);
  394. clear_bit(BTUSB_BULK_RUNNING, &data->flags);
  395. usb_kill_anchored_urbs(&data->bulk_anchor);
  396. clear_bit(BTUSB_INTR_RUNNING, &data->flags);
  397. usb_kill_anchored_urbs(&data->intr_anchor);
  398. return 0;
  399. }
  400. static int btusb_flush(struct hci_dev *hdev)
  401. {
  402. struct btusb_data *data = hdev->driver_data;
  403. BT_DBG("%s", hdev->name);
  404. usb_kill_anchored_urbs(&data->tx_anchor);
  405. return 0;
  406. }
  407. static int btusb_send_frame(struct sk_buff *skb)
  408. {
  409. struct hci_dev *hdev = (struct hci_dev *) skb->dev;
  410. struct btusb_data *data = hdev->driver_data;
  411. struct usb_ctrlrequest *dr;
  412. struct urb *urb;
  413. unsigned int pipe;
  414. int err;
  415. BT_DBG("%s", hdev->name);
  416. if (!test_bit(HCI_RUNNING, &hdev->flags))
  417. return -EBUSY;
  418. switch (bt_cb(skb)->pkt_type) {
  419. case HCI_COMMAND_PKT:
  420. urb = usb_alloc_urb(0, GFP_ATOMIC);
  421. if (!urb)
  422. return -ENOMEM;
  423. dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
  424. if (!dr) {
  425. usb_free_urb(urb);
  426. return -ENOMEM;
  427. }
  428. dr->bRequestType = data->cmdreq_type;
  429. dr->bRequest = 0;
  430. dr->wIndex = 0;
  431. dr->wValue = 0;
  432. dr->wLength = __cpu_to_le16(skb->len);
  433. pipe = usb_sndctrlpipe(data->udev, 0x00);
  434. usb_fill_control_urb(urb, data->udev, pipe, (void *) dr,
  435. skb->data, skb->len, btusb_tx_complete, skb);
  436. hdev->stat.cmd_tx++;
  437. break;
  438. case HCI_ACLDATA_PKT:
  439. if (!data->bulk_tx_ep || hdev->conn_hash.acl_num < 1)
  440. return -ENODEV;
  441. urb = usb_alloc_urb(0, GFP_ATOMIC);
  442. if (!urb)
  443. return -ENOMEM;
  444. pipe = usb_sndbulkpipe(data->udev,
  445. data->bulk_tx_ep->bEndpointAddress);
  446. usb_fill_bulk_urb(urb, data->udev, pipe,
  447. skb->data, skb->len, btusb_tx_complete, skb);
  448. hdev->stat.acl_tx++;
  449. break;
  450. case HCI_SCODATA_PKT:
  451. if (!data->isoc_tx_ep || hdev->conn_hash.sco_num < 1)
  452. return -ENODEV;
  453. urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_ATOMIC);
  454. if (!urb)
  455. return -ENOMEM;
  456. pipe = usb_sndisocpipe(data->udev,
  457. data->isoc_tx_ep->bEndpointAddress);
  458. urb->dev = data->udev;
  459. urb->pipe = pipe;
  460. urb->context = skb;
  461. urb->complete = btusb_tx_complete;
  462. urb->interval = data->isoc_tx_ep->bInterval;
  463. urb->transfer_flags = URB_ISO_ASAP;
  464. urb->transfer_buffer = skb->data;
  465. urb->transfer_buffer_length = skb->len;
  466. __fill_isoc_descriptor(urb, skb->len,
  467. le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
  468. hdev->stat.sco_tx++;
  469. break;
  470. default:
  471. return -EILSEQ;
  472. }
  473. usb_anchor_urb(urb, &data->tx_anchor);
  474. err = usb_submit_urb(urb, GFP_ATOMIC);
  475. if (err < 0) {
  476. BT_ERR("%s urb %p submission failed", hdev->name, urb);
  477. kfree(urb->setup_packet);
  478. usb_unanchor_urb(urb);
  479. }
  480. usb_free_urb(urb);
  481. return err;
  482. }
  483. static void btusb_destruct(struct hci_dev *hdev)
  484. {
  485. struct btusb_data *data = hdev->driver_data;
  486. BT_DBG("%s", hdev->name);
  487. kfree(data);
  488. }
  489. static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
  490. {
  491. struct btusb_data *data = hdev->driver_data;
  492. BT_DBG("%s evt %d", hdev->name, evt);
  493. if (hdev->conn_hash.acl_num > 0) {
  494. if (!test_and_set_bit(BTUSB_BULK_RUNNING, &data->flags)) {
  495. if (btusb_submit_bulk_urb(hdev, GFP_ATOMIC) < 0)
  496. clear_bit(BTUSB_BULK_RUNNING, &data->flags);
  497. else
  498. btusb_submit_bulk_urb(hdev, GFP_ATOMIC);
  499. }
  500. } else {
  501. clear_bit(BTUSB_BULK_RUNNING, &data->flags);
  502. usb_unlink_anchored_urbs(&data->bulk_anchor);
  503. }
  504. schedule_work(&data->work);
  505. }
  506. static int inline __set_isoc_interface(struct hci_dev *hdev, int altsetting)
  507. {
  508. struct btusb_data *data = hdev->driver_data;
  509. struct usb_interface *intf = data->isoc;
  510. struct usb_endpoint_descriptor *ep_desc;
  511. int i, err;
  512. if (!data->isoc)
  513. return -ENODEV;
  514. err = usb_set_interface(data->udev, 1, altsetting);
  515. if (err < 0) {
  516. BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
  517. return err;
  518. }
  519. data->isoc_altsetting = altsetting;
  520. data->isoc_tx_ep = NULL;
  521. data->isoc_rx_ep = NULL;
  522. for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
  523. ep_desc = &intf->cur_altsetting->endpoint[i].desc;
  524. if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
  525. data->isoc_tx_ep = ep_desc;
  526. continue;
  527. }
  528. if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
  529. data->isoc_rx_ep = ep_desc;
  530. continue;
  531. }
  532. }
  533. if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
  534. BT_ERR("%s invalid SCO descriptors", hdev->name);
  535. return -ENODEV;
  536. }
  537. return 0;
  538. }
  539. static void btusb_work(struct work_struct *work)
  540. {
  541. struct btusb_data *data = container_of(work, struct btusb_data, work);
  542. struct hci_dev *hdev = data->hdev;
  543. if (hdev->conn_hash.sco_num > 0) {
  544. if (data->isoc_altsetting != 2) {
  545. clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
  546. usb_kill_anchored_urbs(&data->isoc_anchor);
  547. if (__set_isoc_interface(hdev, 2) < 0)
  548. return;
  549. }
  550. if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
  551. if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
  552. clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
  553. else
  554. btusb_submit_isoc_urb(hdev, GFP_KERNEL);
  555. }
  556. } else {
  557. clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
  558. usb_kill_anchored_urbs(&data->isoc_anchor);
  559. __set_isoc_interface(hdev, 0);
  560. }
  561. }
  562. static int btusb_probe(struct usb_interface *intf,
  563. const struct usb_device_id *id)
  564. {
  565. struct usb_endpoint_descriptor *ep_desc;
  566. struct btusb_data *data;
  567. struct hci_dev *hdev;
  568. int i, err;
  569. BT_DBG("intf %p id %p", intf, id);
  570. /* interface numbers are hardcoded in the spec */
  571. if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
  572. return -ENODEV;
  573. if (!id->driver_info) {
  574. const struct usb_device_id *match;
  575. match = usb_match_id(intf, blacklist_table);
  576. if (match)
  577. id = match;
  578. }
  579. if (id->driver_info == BTUSB_IGNORE)
  580. return -ENODEV;
  581. if (ignore_dga && id->driver_info & BTUSB_DIGIANSWER)
  582. return -ENODEV;
  583. if (ignore_csr && id->driver_info & BTUSB_CSR)
  584. return -ENODEV;
  585. if (ignore_sniffer && id->driver_info & BTUSB_SNIFFER)
  586. return -ENODEV;
  587. data = kzalloc(sizeof(*data), GFP_KERNEL);
  588. if (!data)
  589. return -ENOMEM;
  590. for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
  591. ep_desc = &intf->cur_altsetting->endpoint[i].desc;
  592. if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
  593. data->intr_ep = ep_desc;
  594. continue;
  595. }
  596. if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
  597. data->bulk_tx_ep = ep_desc;
  598. continue;
  599. }
  600. if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
  601. data->bulk_rx_ep = ep_desc;
  602. continue;
  603. }
  604. }
  605. if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep) {
  606. kfree(data);
  607. return -ENODEV;
  608. }
  609. data->cmdreq_type = USB_TYPE_CLASS;
  610. data->udev = interface_to_usbdev(intf);
  611. data->intf = intf;
  612. spin_lock_init(&data->lock);
  613. INIT_WORK(&data->work, btusb_work);
  614. init_usb_anchor(&data->tx_anchor);
  615. init_usb_anchor(&data->intr_anchor);
  616. init_usb_anchor(&data->bulk_anchor);
  617. init_usb_anchor(&data->isoc_anchor);
  618. hdev = hci_alloc_dev();
  619. if (!hdev) {
  620. kfree(data);
  621. return -ENOMEM;
  622. }
  623. hdev->type = HCI_USB;
  624. hdev->driver_data = data;
  625. data->hdev = hdev;
  626. SET_HCIDEV_DEV(hdev, &intf->dev);
  627. hdev->open = btusb_open;
  628. hdev->close = btusb_close;
  629. hdev->flush = btusb_flush;
  630. hdev->send = btusb_send_frame;
  631. hdev->destruct = btusb_destruct;
  632. hdev->notify = btusb_notify;
  633. hdev->owner = THIS_MODULE;
  634. /* Interface numbers are hardcoded in the specification */
  635. data->isoc = usb_ifnum_to_if(data->udev, 1);
  636. if (!reset)
  637. set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
  638. if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
  639. if (!disable_scofix)
  640. set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
  641. }
  642. if (id->driver_info & BTUSB_BROKEN_ISOC)
  643. data->isoc = NULL;
  644. if (id->driver_info & BTUSB_DIGIANSWER) {
  645. data->cmdreq_type = USB_TYPE_VENDOR;
  646. set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
  647. }
  648. if (id->driver_info & BTUSB_CSR) {
  649. struct usb_device *udev = data->udev;
  650. /* Old firmware would otherwise execute USB reset */
  651. if (le16_to_cpu(udev->descriptor.bcdDevice) < 0x117)
  652. set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
  653. }
  654. if (id->driver_info & BTUSB_SNIFFER) {
  655. struct usb_device *udev = data->udev;
  656. /* New sniffer firmware has crippled HCI interface */
  657. if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
  658. set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
  659. data->isoc = NULL;
  660. }
  661. if (id->driver_info & BTUSB_BCM92035) {
  662. unsigned char cmd[] = { 0x3b, 0xfc, 0x01, 0x00 };
  663. struct sk_buff *skb;
  664. skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
  665. if (skb) {
  666. memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
  667. skb_queue_tail(&hdev->driver_init, skb);
  668. }
  669. }
  670. if (data->isoc) {
  671. err = usb_driver_claim_interface(&btusb_driver,
  672. data->isoc, data);
  673. if (err < 0) {
  674. hci_free_dev(hdev);
  675. kfree(data);
  676. return err;
  677. }
  678. }
  679. err = hci_register_dev(hdev);
  680. if (err < 0) {
  681. hci_free_dev(hdev);
  682. kfree(data);
  683. return err;
  684. }
  685. usb_set_intfdata(intf, data);
  686. return 0;
  687. }
  688. static void btusb_disconnect(struct usb_interface *intf)
  689. {
  690. struct btusb_data *data = usb_get_intfdata(intf);
  691. struct hci_dev *hdev;
  692. BT_DBG("intf %p", intf);
  693. if (!data)
  694. return;
  695. hdev = data->hdev;
  696. __hci_dev_hold(hdev);
  697. usb_set_intfdata(data->intf, NULL);
  698. if (data->isoc)
  699. usb_set_intfdata(data->isoc, NULL);
  700. hci_unregister_dev(hdev);
  701. if (intf == data->isoc)
  702. usb_driver_release_interface(&btusb_driver, data->intf);
  703. else if (data->isoc)
  704. usb_driver_release_interface(&btusb_driver, data->isoc);
  705. __hci_dev_put(hdev);
  706. hci_free_dev(hdev);
  707. }
  708. static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
  709. {
  710. struct btusb_data *data = usb_get_intfdata(intf);
  711. BT_DBG("intf %p", intf);
  712. if (data->suspend_count++)
  713. return 0;
  714. cancel_work_sync(&data->work);
  715. usb_kill_anchored_urbs(&data->tx_anchor);
  716. usb_kill_anchored_urbs(&data->isoc_anchor);
  717. usb_kill_anchored_urbs(&data->bulk_anchor);
  718. usb_kill_anchored_urbs(&data->intr_anchor);
  719. return 0;
  720. }
  721. static int btusb_resume(struct usb_interface *intf)
  722. {
  723. struct btusb_data *data = usb_get_intfdata(intf);
  724. struct hci_dev *hdev = data->hdev;
  725. int err;
  726. BT_DBG("intf %p", intf);
  727. if (--data->suspend_count)
  728. return 0;
  729. if (!test_bit(HCI_RUNNING, &hdev->flags))
  730. return 0;
  731. if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
  732. err = btusb_submit_intr_urb(hdev, GFP_NOIO);
  733. if (err < 0) {
  734. clear_bit(BTUSB_INTR_RUNNING, &data->flags);
  735. return err;
  736. }
  737. }
  738. if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
  739. if (btusb_submit_bulk_urb(hdev, GFP_NOIO) < 0)
  740. clear_bit(BTUSB_BULK_RUNNING, &data->flags);
  741. else
  742. btusb_submit_bulk_urb(hdev, GFP_NOIO);
  743. }
  744. if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
  745. if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
  746. clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
  747. else
  748. btusb_submit_isoc_urb(hdev, GFP_NOIO);
  749. }
  750. return 0;
  751. }
  752. static struct usb_driver btusb_driver = {
  753. .name = "btusb",
  754. .probe = btusb_probe,
  755. .disconnect = btusb_disconnect,
  756. .suspend = btusb_suspend,
  757. .resume = btusb_resume,
  758. .id_table = btusb_table,
  759. };
  760. static int __init btusb_init(void)
  761. {
  762. BT_INFO("Generic Bluetooth USB driver ver %s", VERSION);
  763. return usb_register(&btusb_driver);
  764. }
  765. static void __exit btusb_exit(void)
  766. {
  767. usb_deregister(&btusb_driver);
  768. }
  769. module_init(btusb_init);
  770. module_exit(btusb_exit);
  771. module_param(ignore_dga, bool, 0644);
  772. MODULE_PARM_DESC(ignore_dga, "Ignore devices with id 08fd:0001");
  773. module_param(ignore_csr, bool, 0644);
  774. MODULE_PARM_DESC(ignore_csr, "Ignore devices with id 0a12:0001");
  775. module_param(ignore_sniffer, bool, 0644);
  776. MODULE_PARM_DESC(ignore_sniffer, "Ignore devices with id 0a12:0002");
  777. module_param(disable_scofix, bool, 0644);
  778. MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
  779. module_param(force_scofix, bool, 0644);
  780. MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
  781. module_param(reset, bool, 0644);
  782. MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
  783. MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
  784. MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
  785. MODULE_VERSION(VERSION);
  786. MODULE_LICENSE("GPL");