hci_usb.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:19k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* 
  2.    BlueZ - Bluetooth protocol stack for Linux
  3.    Copyright (C) 2000-2001 Qualcomm Incorporated
  4.    Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License version 2 as
  7.    published by the Free Software Foundation;
  8.    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  9.    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  10.    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
  11.    IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
  12.    CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
  13.    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
  14.    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
  15.    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16.    ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
  17.    COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
  18.    SOFTWARE IS DISCLAIMED.
  19. */
  20. /*
  21.  * BlueZ HCI USB driver.
  22.  * Based on original USB Bluetooth driver for Linux kernel
  23.  *    Copyright (c) 2000 Greg Kroah-Hartman        <greg@kroah.com>
  24.  *    Copyright (c) 2000 Mark Douglas Corner       <mcorner@umich.edu>
  25.  *
  26.  * $Id: hci_usb.c,v 1.8 2002/07/18 17:23:09 maxk Exp $    
  27.  */
  28. #define VERSION "2.1"
  29. #include <linux/config.h>
  30. #include <linux/module.h>
  31. #define __KERNEL_SYSCALLS__
  32. #include <linux/version.h>
  33. #include <linux/kernel.h>
  34. #include <linux/init.h>
  35. #include <linux/sched.h>
  36. #include <linux/unistd.h>
  37. #include <linux/types.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/slab.h>
  40. #include <linux/errno.h>
  41. #include <linux/string.h>
  42. #include <linux/skbuff.h>
  43. #include <linux/kmod.h>
  44. #include <linux/usb.h>
  45. #include <net/bluetooth/bluetooth.h>
  46. #include <net/bluetooth/hci_core.h>
  47. #include "hci_usb.h"
  48. #define HCI_MAX_PENDING (HCI_MAX_BULK_RX + HCI_MAX_BULK_TX + 1)
  49. #ifndef HCI_USB_DEBUG
  50. #undef  BT_DBG
  51. #define BT_DBG( A... )
  52. #undef  BT_DMP
  53. #define BT_DMP( A... )
  54. #endif
  55. #ifndef CONFIG_BLUEZ_USB_ZERO_PACKET
  56. #undef  USB_ZERO_PACKET
  57. #define USB_ZERO_PACKET 0
  58. #endif
  59. static struct usb_driver hci_usb_driver; 
  60. static struct usb_device_id bluetooth_ids[] = {
  61. /* Generic Bluetooth USB device */
  62. { USB_DEVICE_INFO(HCI_DEV_CLASS, HCI_DEV_SUBCLASS, HCI_DEV_PROTOCOL) },
  63. /* Ericsson with non-standard id */
  64. { USB_DEVICE(0x0bdb, 0x1002) },
  65. { } /* Terminating entry */
  66. };
  67. MODULE_DEVICE_TABLE (usb, bluetooth_ids);
  68. static struct usb_device_id ignore_ids[] = {
  69. /* Broadcom BCM2033 without firmware */
  70. { USB_DEVICE(0x0a5c, 0x2033) },
  71. { } /* Terminating entry */
  72. };
  73. static void hci_usb_interrupt(struct urb *urb);
  74. static void hci_usb_rx_complete(struct urb *urb);
  75. static void hci_usb_tx_complete(struct urb *urb);
  76. static struct urb *hci_usb_get_completed(struct hci_usb *husb)
  77. {
  78. struct sk_buff *skb;
  79. struct urb *urb = NULL;
  80. skb = skb_dequeue(&husb->completed_q);
  81. if (skb) {
  82. urb = ((struct hci_usb_scb *) skb->cb)->urb;
  83. kfree_skb(skb);
  84. }
  85. BT_DBG("%s urb %p", husb->hdev.name, urb);
  86. return urb;
  87. }
  88. static int hci_usb_enable_intr(struct hci_usb *husb)
  89. {
  90. struct urb *urb;
  91. int pipe, size;
  92. void *buf;
  93. BT_DBG("%s", husb->hdev.name);
  94.   if (!(urb = usb_alloc_urb(0)))
  95. return -ENOMEM;
  96. if (!(buf = kmalloc(HCI_MAX_EVENT_SIZE, GFP_KERNEL))) {
  97. usb_free_urb(urb);
  98. return -ENOMEM;
  99. }
  100. husb->intr_urb = urb;
  101.         pipe = usb_rcvintpipe(husb->udev, husb->intr_ep);
  102.         size = usb_maxpacket(husb->udev, pipe, usb_pipeout(pipe));
  103. FILL_INT_URB(urb, husb->udev, pipe, buf, size, 
  104. hci_usb_interrupt, husb, husb->intr_interval);
  105. return usb_submit_urb(urb);
  106. }
  107. static int hci_usb_disable_intr(struct hci_usb *husb)
  108. {
  109. struct urb *urb = husb->intr_urb;
  110. struct sk_buff *skb;
  111. BT_DBG("%s", husb->hdev.name);
  112. usb_unlink_urb(urb); usb_free_urb(urb);
  113. husb->intr_urb = NULL;
  114. skb = husb->intr_skb;
  115. if (skb) {
  116. husb->intr_skb = NULL;
  117. kfree_skb(skb);
  118. }
  119. return 0;
  120. }
  121. static int hci_usb_rx_submit(struct hci_usb *husb, struct urb *urb)
  122. {
  123. struct hci_usb_scb *scb;
  124. struct sk_buff *skb;
  125. int    pipe, size, err;
  126. if (!urb && !(urb = usb_alloc_urb(0)))
  127. return -ENOMEM;
  128.         size = HCI_MAX_FRAME_SIZE;
  129. if (!(skb = bluez_skb_alloc(size, GFP_ATOMIC))) {
  130. usb_free_urb(urb);
  131. return -ENOMEM;
  132. }
  133. BT_DBG("%s urb %p", husb->hdev.name, urb);
  134. skb->dev = (void *) &husb->hdev;
  135. skb->pkt_type = HCI_ACLDATA_PKT;
  136. scb = (struct hci_usb_scb *) skb->cb;
  137. scb->urb = urb;
  138.         pipe = usb_rcvbulkpipe(husb->udev, husb->bulk_in_ep);
  139.         FILL_BULK_URB(urb, husb->udev, pipe, skb->data, size, hci_usb_rx_complete, skb);
  140.         urb->transfer_flags = USB_QUEUE_BULK;
  141. skb_queue_tail(&husb->pending_q, skb);
  142. err = usb_submit_urb(urb);
  143. if (err) {
  144. BT_ERR("%s bulk rx submit failed urb %p err %d",
  145. husb->hdev.name, urb, err);
  146. skb_unlink(skb);
  147. usb_free_urb(urb);
  148. }
  149. return err;
  150. }
  151. /* Initialize device */
  152. static int hci_usb_open(struct hci_dev *hdev)
  153. {
  154. struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
  155. int i, err;
  156. unsigned long flags;
  157. BT_DBG("%s", hdev->name);
  158. if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
  159. return 0;
  160. MOD_INC_USE_COUNT;
  161. write_lock_irqsave(&husb->completion_lock, flags);
  162. err = hci_usb_enable_intr(husb);
  163. if (!err) {
  164. for (i = 0; i < HCI_MAX_BULK_TX; i++)
  165. hci_usb_rx_submit(husb, NULL);
  166. } else {
  167. clear_bit(HCI_RUNNING, &hdev->flags);
  168. MOD_DEC_USE_COUNT;
  169. }
  170. write_unlock_irqrestore(&husb->completion_lock, flags);
  171. return err;
  172. }
  173. /* Reset device */
  174. static int hci_usb_flush(struct hci_dev *hdev)
  175. {
  176. struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
  177. BT_DBG("%s", hdev->name);
  178. skb_queue_purge(&husb->cmd_q);
  179. skb_queue_purge(&husb->acl_q);
  180. return 0;
  181. }
  182. static inline void hci_usb_unlink_urbs(struct hci_usb *husb)
  183. {
  184. struct sk_buff *skb;
  185. struct urb *urb;
  186. BT_DBG("%s", husb->hdev.name);
  187. while ((skb = skb_dequeue(&husb->pending_q))) {
  188. urb = ((struct hci_usb_scb *) skb->cb)->urb;
  189. usb_unlink_urb(urb);
  190. kfree_skb(skb);
  191. }
  192. while ((urb = hci_usb_get_completed(husb)))
  193. usb_free_urb(urb);
  194. }
  195. /* Close device */
  196. static int hci_usb_close(struct hci_dev *hdev)
  197. {
  198. struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
  199. unsigned long flags;
  200. if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
  201. return 0;
  202. BT_DBG("%s", hdev->name);
  203. write_lock_irqsave(&husb->completion_lock, flags);
  204. hci_usb_disable_intr(husb);
  205. hci_usb_unlink_urbs(husb);
  206. hci_usb_flush(hdev);
  207. write_unlock_irqrestore(&husb->completion_lock, flags);
  208. MOD_DEC_USE_COUNT;
  209. return 0;
  210. }
  211. static inline int hci_usb_send_ctrl(struct hci_usb *husb, struct sk_buff *skb)
  212. {
  213. struct hci_usb_scb *scb = (void *) skb->cb;
  214. struct urb *urb = hci_usb_get_completed(husb);
  215. struct usb_ctrlrequest *dr;
  216. int pipe, err;
  217. if (!urb && !(urb = usb_alloc_urb(0)))
  218. return -ENOMEM;
  219. if (!(dr = kmalloc(sizeof(*dr), GFP_ATOMIC))) {
  220. usb_free_urb(urb);
  221. return -ENOMEM;
  222. }
  223. pipe = usb_sndctrlpipe(husb->udev, 0);
  224. dr->bRequestType = HCI_CTRL_REQ;
  225. dr->bRequest = 0;
  226. dr->wIndex   = 0;
  227. dr->wValue   = 0;
  228. dr->wLength  = __cpu_to_le16(skb->len);
  229. FILL_CONTROL_URB(urb, husb->udev, pipe, (void *) dr,
  230. skb->data, skb->len, hci_usb_tx_complete, skb);
  231. BT_DBG("%s urb %p len %d", husb->hdev.name, urb, skb->len);
  232. scb->urb = urb;
  233. skb_queue_tail(&husb->pending_q, skb);
  234. err = usb_submit_urb(urb);
  235. if (err) {
  236. BT_ERR("%s ctrl tx submit failed urb %p err %d", 
  237. husb->hdev.name, urb, err);
  238. skb_unlink(skb);
  239. usb_free_urb(urb); kfree(dr);
  240. }
  241. return err;
  242. }
  243. static inline int hci_usb_send_bulk(struct hci_usb *husb, struct sk_buff *skb)
  244. {
  245. struct hci_usb_scb *scb = (void *) skb->cb;
  246. struct urb *urb = hci_usb_get_completed(husb);
  247. int pipe, err;
  248. if (!urb && !(urb = usb_alloc_urb(0)))
  249. return -ENOMEM;
  250. pipe = usb_sndbulkpipe(husb->udev, husb->bulk_out_ep);
  251.         
  252. FILL_BULK_URB(urb, husb->udev, pipe, skb->data, skb->len,
  253.               hci_usb_tx_complete, skb);
  254. urb->transfer_flags = USB_QUEUE_BULK | USB_ZERO_PACKET;
  255. BT_DBG("%s urb %p len %d", husb->hdev.name, urb, skb->len);
  256. scb->urb = urb;
  257. skb_queue_tail(&husb->pending_q, skb);
  258. err = usb_submit_urb(urb);
  259. if (err) {
  260. BT_ERR("%s bulk tx submit failed urb %p err %d", 
  261. husb->hdev.name, urb, err);
  262. skb_unlink(skb);
  263. usb_free_urb(urb);
  264. }
  265. return err;
  266. }
  267. static void hci_usb_tx_process(struct hci_usb *husb)
  268. {
  269. struct sk_buff *skb;
  270. BT_DBG("%s", husb->hdev.name);
  271. do {
  272. clear_bit(HCI_USB_TX_WAKEUP, &husb->state);
  273. /* Process ACL queue */
  274. while (skb_queue_len(&husb->pending_q) < HCI_MAX_PENDING &&
  275. (skb = skb_dequeue(&husb->acl_q))) {
  276. if (hci_usb_send_bulk(husb, skb) < 0) {
  277. skb_queue_head(&husb->acl_q, skb);
  278. break;
  279. }
  280. }
  281. /* Process command queue */
  282. if (!test_bit(HCI_USB_CTRL_TX, &husb->state) &&
  283. (skb = skb_dequeue(&husb->cmd_q)) != NULL) {
  284. set_bit(HCI_USB_CTRL_TX, &husb->state);
  285. if (hci_usb_send_ctrl(husb, skb) < 0) {
  286. skb_queue_head(&husb->cmd_q, skb);
  287. clear_bit(HCI_USB_CTRL_TX, &husb->state);
  288. }
  289. }
  290. } while(test_bit(HCI_USB_TX_WAKEUP, &husb->state));
  291. }
  292. static inline void hci_usb_tx_wakeup(struct hci_usb *husb)
  293. {
  294. /* Serialize TX queue processing to avoid data reordering */
  295. if (!test_and_set_bit(HCI_USB_TX_PROCESS, &husb->state)) {
  296. hci_usb_tx_process(husb);
  297. clear_bit(HCI_USB_TX_PROCESS, &husb->state);
  298. } else
  299. set_bit(HCI_USB_TX_WAKEUP, &husb->state);
  300. }
  301. /* Send frames from HCI layer */
  302. int hci_usb_send_frame(struct sk_buff *skb)
  303. {
  304. struct hci_dev *hdev = (struct hci_dev *) skb->dev;
  305. struct hci_usb *husb;
  306. if (!hdev) {
  307. BT_ERR("frame for uknown device (hdev=NULL)");
  308. return -ENODEV;
  309. }
  310. if (!test_bit(HCI_RUNNING, &hdev->flags))
  311. return -EBUSY;
  312. husb = (struct hci_usb *) hdev->driver_data;
  313. BT_DBG("%s type %d len %d", hdev->name, skb->pkt_type, skb->len);
  314. read_lock(&husb->completion_lock);
  315. switch (skb->pkt_type) {
  316. case HCI_COMMAND_PKT:
  317. skb_queue_tail(&husb->cmd_q, skb);
  318. hdev->stat.cmd_tx++;
  319. break;
  320. case HCI_ACLDATA_PKT:
  321. skb_queue_tail(&husb->acl_q, skb);
  322. hdev->stat.acl_tx++;
  323. break;
  324. case HCI_SCODATA_PKT:
  325. default:
  326. kfree_skb(skb);
  327. break;
  328. }
  329. hci_usb_tx_wakeup(husb);
  330. read_unlock(&husb->completion_lock);
  331. return 0;
  332. }
  333. static void hci_usb_interrupt(struct urb *urb)
  334. {
  335. struct hci_usb *husb = (void *) urb->context;
  336. struct hci_usb_scb *scb;
  337. struct sk_buff *skb;
  338. hci_event_hdr *eh;
  339. __u8 *data = urb->transfer_buffer;
  340. int count = urb->actual_length;
  341. int len = HCI_EVENT_HDR_SIZE;
  342. BT_DBG("%s urb %p count %d", husb->hdev.name, urb, count);
  343. if (!test_bit(HCI_RUNNING, &husb->hdev.flags))
  344. return;
  345. if (urb->status || !count) {
  346. BT_DBG("%s intr status %d, count %d", 
  347. husb->hdev.name, urb->status, count);
  348. return;
  349. }
  350. read_lock(&husb->completion_lock);
  351. husb->hdev.stat.byte_rx += count;
  352. if (!(skb = husb->intr_skb)) {
  353. /* Start of the frame */
  354. if (count < HCI_EVENT_HDR_SIZE)
  355. goto bad_len;
  356. eh  = (hci_event_hdr *) data;
  357. len = eh->plen + HCI_EVENT_HDR_SIZE;
  358. if (count > len)
  359. goto bad_len;
  360. skb = bluez_skb_alloc(len, GFP_ATOMIC);
  361. if (!skb) {
  362. BT_ERR("%s no memory for event packet", husb->hdev.name);
  363. goto done;
  364. }
  365. scb = (void *) skb->cb;
  366. skb->dev = (void *) &husb->hdev;
  367. skb->pkt_type = HCI_EVENT_PKT;
  368. husb->intr_skb = skb;
  369. scb->intr_len  = len;
  370. } else {
  371. /* Continuation */
  372. scb = (void *) skb->cb;
  373. len = scb->intr_len;
  374. if (count > len) {
  375. husb->intr_skb = NULL;
  376. kfree_skb(skb);
  377. goto bad_len;
  378. }
  379. }
  380. memcpy(skb_put(skb, count), data, count);
  381. scb->intr_len -= count;
  382. if (!scb->intr_len) {
  383. /* Complete frame */
  384. husb->intr_skb = NULL;
  385. hci_recv_frame(skb);
  386. }
  387. done:
  388. read_unlock(&husb->completion_lock);
  389. return;
  390. bad_len:
  391. BT_ERR("%s bad frame len %d expected %d", husb->hdev.name, count, len);
  392. husb->hdev.stat.err_rx++;
  393. read_unlock(&husb->completion_lock);
  394. }
  395. static void hci_usb_tx_complete(struct urb *urb)
  396. {
  397. struct sk_buff *skb  = (struct sk_buff *) urb->context;
  398. struct hci_dev *hdev = (struct hci_dev *) skb->dev;
  399. struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
  400. BT_DBG("%s urb %p status %d flags %x", husb->hdev.name, urb,
  401. urb->status, urb->transfer_flags);
  402. if (urb->pipe == usb_sndctrlpipe(husb->udev, 0)) {
  403. kfree(urb->setup_packet);
  404. clear_bit(HCI_USB_CTRL_TX, &husb->state);
  405. }
  406. if (!test_bit(HCI_RUNNING, &hdev->flags))
  407. return;
  408. read_lock(&husb->completion_lock);
  409. if (!urb->status)
  410. husb->hdev.stat.byte_tx += skb->len;
  411. else
  412. husb->hdev.stat.err_tx++;
  413. skb_unlink(skb);
  414. skb_queue_tail(&husb->completed_q, skb);
  415. hci_usb_tx_wakeup(husb);
  416. read_unlock(&husb->completion_lock);
  417. return;
  418. }
  419. static void hci_usb_rx_complete(struct urb *urb)
  420. {
  421. struct sk_buff *skb  = (struct sk_buff *) urb->context;
  422. struct hci_dev *hdev = (struct hci_dev *) skb->dev;
  423. struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
  424. int status, count = urb->actual_length;
  425. hci_acl_hdr *ah;
  426. int dlen, size;
  427. BT_DBG("%s urb %p status %d count %d flags %x", husb->hdev.name, urb,
  428. urb->status, count, urb->transfer_flags);
  429. if (!test_bit(HCI_RUNNING, &hdev->flags))
  430. return;
  431. read_lock(&husb->completion_lock);
  432. if (urb->status || !count)
  433. goto resubmit;
  434. husb->hdev.stat.byte_rx += count;
  435. ah   = (hci_acl_hdr *) skb->data;
  436. dlen = __le16_to_cpu(ah->dlen);
  437. size = HCI_ACL_HDR_SIZE + dlen;
  438. /* Verify frame len and completeness */
  439. if (count != size) {
  440. BT_ERR("%s corrupted ACL packet: count %d, dlen %d",
  441. husb->hdev.name, count, dlen);
  442. bluez_dump("hci_usb", skb->data, count);
  443. husb->hdev.stat.err_rx++;
  444. goto resubmit;
  445. }
  446. skb_unlink(skb);
  447. skb_put(skb, count);
  448. hci_recv_frame(skb);
  449. hci_usb_rx_submit(husb, urb);
  450. read_unlock(&husb->completion_lock);
  451. return;
  452. resubmit:
  453. urb->dev = husb->udev;
  454. status   = usb_submit_urb(urb);
  455. BT_DBG("%s URB resubmit status %d", husb->hdev.name, status);
  456. read_unlock(&husb->completion_lock);
  457. }
  458. static void hci_usb_destruct(struct hci_dev *hdev)
  459. {
  460. struct hci_usb *husb;
  461. if (!hdev) return;
  462. BT_DBG("%s", hdev->name);
  463. husb = (struct hci_usb *) hdev->driver_data;
  464. kfree(husb);
  465. }
  466. static void *hci_usb_probe(struct usb_device *udev, unsigned int ifnum, const struct usb_device_id *id)
  467. {
  468. struct usb_endpoint_descriptor *bulk_out_ep[HCI_MAX_IFACE_NUM];
  469. struct usb_endpoint_descriptor *isoc_out_ep[HCI_MAX_IFACE_NUM];
  470. struct usb_endpoint_descriptor *bulk_in_ep[HCI_MAX_IFACE_NUM];
  471. struct usb_endpoint_descriptor *isoc_in_ep[HCI_MAX_IFACE_NUM];
  472. struct usb_endpoint_descriptor *intr_in_ep[HCI_MAX_IFACE_NUM];
  473. struct usb_interface_descriptor *uif;
  474. struct usb_endpoint_descriptor *ep;
  475. struct usb_interface *iface, *isoc_iface;
  476. struct hci_usb *husb;
  477. struct hci_dev *hdev;
  478. int i, a, e, size, ifn, isoc_ifnum, isoc_alts;
  479. BT_DBG("udev %p ifnum %d", udev, ifnum);
  480. iface = &udev->actconfig->interface[0];
  481. /* Check our black list */
  482. if (usb_match_id(udev, iface, ignore_ids))
  483. return NULL;
  484. /* Check number of endpoints */
  485. if (udev->actconfig->interface[ifnum].altsetting[0].bNumEndpoints < 3)
  486. return NULL;
  487. memset(bulk_out_ep, 0, sizeof(bulk_out_ep));
  488. memset(isoc_out_ep, 0, sizeof(isoc_out_ep));
  489. memset(bulk_in_ep,  0, sizeof(bulk_in_ep));
  490. memset(isoc_in_ep,  0, sizeof(isoc_in_ep));
  491. memset(intr_in_ep,  0, sizeof(intr_in_ep));
  492. size = 0; 
  493. isoc_iface = NULL;
  494. isoc_alts  = isoc_ifnum = 0;
  495. /* Find endpoints that we need */
  496. ifn = MIN(udev->actconfig->bNumInterfaces, HCI_MAX_IFACE_NUM);
  497. for (i = 0; i < ifn; i++) {
  498. iface = &udev->actconfig->interface[i];
  499. for (a = 0; a < iface->num_altsetting; a++) {
  500. uif = &iface->altsetting[a];
  501. for (e = 0; e < uif->bNumEndpoints; e++) {
  502. ep = &uif->endpoint[e];
  503. switch (ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
  504. case USB_ENDPOINT_XFER_INT:
  505. if (ep->bEndpointAddress & USB_DIR_IN)
  506. intr_in_ep[i] = ep;
  507. break;
  508. case USB_ENDPOINT_XFER_BULK:
  509. if (ep->bEndpointAddress & USB_DIR_IN)
  510. bulk_in_ep[i]  = ep;
  511. else
  512. bulk_out_ep[i] = ep;
  513. break;
  514. case USB_ENDPOINT_XFER_ISOC:
  515. if (ep->wMaxPacketSize < size)
  516. break;
  517. size = ep->wMaxPacketSize;
  518. isoc_iface = iface;
  519. isoc_alts  = a;
  520. isoc_ifnum = i;
  521. if (ep->bEndpointAddress & USB_DIR_IN)
  522. isoc_in_ep[i]  = ep;
  523. else
  524. isoc_out_ep[i] = ep;
  525. break;
  526. }
  527. }
  528. }
  529. }
  530. if (!bulk_in_ep[0] || !bulk_out_ep[0] || !intr_in_ep[0]) {
  531. BT_DBG("Bulk endpoints not found");
  532. goto done;
  533. }
  534. if (!isoc_in_ep[1] || !isoc_out_ep[1]) {
  535. BT_DBG("Isoc endpoints not found");
  536. isoc_iface = NULL;
  537. }
  538. if (!(husb = kmalloc(sizeof(struct hci_usb), GFP_KERNEL))) {
  539. BT_ERR("Can't allocate: control structure");
  540. goto done;
  541. }
  542. memset(husb, 0, sizeof(struct hci_usb));
  543. husb->udev = udev;
  544. husb->bulk_out_ep = bulk_out_ep[0]->bEndpointAddress;
  545. husb->bulk_in_ep  = bulk_in_ep[0]->bEndpointAddress;
  546. husb->intr_ep = intr_in_ep[0]->bEndpointAddress;
  547. husb->intr_interval = intr_in_ep[0]->bInterval;
  548. if (isoc_iface) {
  549. if (usb_set_interface(udev, isoc_ifnum, isoc_alts)) {
  550. BT_ERR("Can't set isoc interface settings");
  551. isoc_iface = NULL;
  552. }
  553. usb_driver_claim_interface(&hci_usb_driver, isoc_iface, husb);
  554. husb->isoc_iface  = isoc_iface;
  555. husb->isoc_in_ep  = isoc_in_ep[1]->bEndpointAddress;
  556. husb->isoc_out_ep = isoc_in_ep[1]->bEndpointAddress;
  557. }
  558. husb->completion_lock = RW_LOCK_UNLOCKED;
  559. skb_queue_head_init(&husb->acl_q);
  560. skb_queue_head_init(&husb->cmd_q);
  561. skb_queue_head_init(&husb->pending_q);
  562. skb_queue_head_init(&husb->completed_q);
  563. /* Initialize and register HCI device */
  564. hdev = &husb->hdev;
  565. hdev->type = HCI_USB;
  566. hdev->driver_data = husb;
  567. hdev->open  = hci_usb_open;
  568. hdev->close = hci_usb_close;
  569. hdev->flush = hci_usb_flush;
  570. hdev->send  = hci_usb_send_frame;
  571. hdev->destruct = hci_usb_destruct;
  572. if (hci_register_dev(hdev) < 0) {
  573. BT_ERR("Can't register HCI device");
  574. goto probe_error;
  575. }
  576. return husb;
  577. probe_error:
  578. kfree(husb);
  579. done:
  580. return NULL;
  581. }
  582. static void hci_usb_disconnect(struct usb_device *udev, void *ptr)
  583. {
  584. struct hci_usb *husb = (struct hci_usb *) ptr;
  585. struct hci_dev *hdev = &husb->hdev;
  586. if (!husb)
  587. return;
  588. BT_DBG("%s", hdev->name);
  589. hci_usb_close(hdev);
  590. if (husb->isoc_iface)
  591. usb_driver_release_interface(&hci_usb_driver, husb->isoc_iface);
  592. if (hci_unregister_dev(hdev) < 0)
  593. BT_ERR("Can't unregister HCI device %s", hdev->name);
  594. }
  595. static struct usb_driver hci_usb_driver = {
  596. name:           "hci_usb",
  597. probe:          hci_usb_probe,
  598. disconnect:     hci_usb_disconnect,
  599. id_table:       bluetooth_ids,
  600. };
  601. int hci_usb_init(void)
  602. {
  603. int err;
  604. BT_INFO("BlueZ HCI USB driver ver %s Copyright (C) 2000,2001 Qualcomm Inc",  
  605. VERSION);
  606. BT_INFO("Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>");
  607. if ((err = usb_register(&hci_usb_driver)) < 0)
  608. BT_ERR("Failed to register HCI USB driver");
  609. return err;
  610. }
  611. void hci_usb_cleanup(void)
  612. {
  613. usb_deregister(&hci_usb_driver);
  614. }
  615. module_init(hci_usb_init);
  616. module_exit(hci_usb_cleanup);
  617. MODULE_AUTHOR("Maxim Krasnyansky <maxk@qualcomm.com>");
  618. MODULE_DESCRIPTION("BlueZ HCI USB driver ver " VERSION);
  619. MODULE_LICENSE("GPL");