btusb.c 28 KB

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