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