btusb.c 24 KB

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