bfusb.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. /*
  2. *
  3. * AVM BlueFRITZ! USB driver
  4. *
  5. * Copyright (C) 2003-2006 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/module.h>
  24. #include <linux/kernel.h>
  25. #include <linux/init.h>
  26. #include <linux/slab.h>
  27. #include <linux/types.h>
  28. #include <linux/errno.h>
  29. #include <linux/skbuff.h>
  30. #include <linux/device.h>
  31. #include <linux/firmware.h>
  32. #include <linux/usb.h>
  33. #include <net/bluetooth/bluetooth.h>
  34. #include <net/bluetooth/hci_core.h>
  35. #define VERSION "1.2"
  36. static struct usb_driver bfusb_driver;
  37. static struct usb_device_id bfusb_table[] = {
  38. /* AVM BlueFRITZ! USB */
  39. { USB_DEVICE(0x057c, 0x2200) },
  40. { } /* Terminating entry */
  41. };
  42. MODULE_DEVICE_TABLE(usb, bfusb_table);
  43. #define BFUSB_MAX_BLOCK_SIZE 256
  44. #define BFUSB_BLOCK_TIMEOUT 3000
  45. #define BFUSB_TX_PROCESS 1
  46. #define BFUSB_TX_WAKEUP 2
  47. #define BFUSB_MAX_BULK_TX 2
  48. #define BFUSB_MAX_BULK_RX 2
  49. struct bfusb_data {
  50. struct hci_dev *hdev;
  51. unsigned long state;
  52. struct usb_device *udev;
  53. unsigned int bulk_in_ep;
  54. unsigned int bulk_out_ep;
  55. unsigned int bulk_pkt_size;
  56. rwlock_t lock;
  57. struct sk_buff_head transmit_q;
  58. struct sk_buff *reassembly;
  59. atomic_t pending_tx;
  60. struct sk_buff_head pending_q;
  61. struct sk_buff_head completed_q;
  62. };
  63. struct bfusb_data_scb {
  64. struct urb *urb;
  65. };
  66. static void bfusb_tx_complete(struct urb *urb);
  67. static void bfusb_rx_complete(struct urb *urb);
  68. static struct urb *bfusb_get_completed(struct bfusb_data *data)
  69. {
  70. struct sk_buff *skb;
  71. struct urb *urb = NULL;
  72. BT_DBG("bfusb %p", data);
  73. skb = skb_dequeue(&data->completed_q);
  74. if (skb) {
  75. urb = ((struct bfusb_data_scb *) skb->cb)->urb;
  76. kfree_skb(skb);
  77. }
  78. return urb;
  79. }
  80. static void bfusb_unlink_urbs(struct bfusb_data *data)
  81. {
  82. struct sk_buff *skb;
  83. struct urb *urb;
  84. BT_DBG("bfusb %p", data);
  85. while ((skb = skb_dequeue(&data->pending_q))) {
  86. urb = ((struct bfusb_data_scb *) skb->cb)->urb;
  87. usb_kill_urb(urb);
  88. skb_queue_tail(&data->completed_q, skb);
  89. }
  90. while ((urb = bfusb_get_completed(data)))
  91. usb_free_urb(urb);
  92. }
  93. static int bfusb_send_bulk(struct bfusb_data *data, struct sk_buff *skb)
  94. {
  95. struct bfusb_data_scb *scb = (void *) skb->cb;
  96. struct urb *urb = bfusb_get_completed(data);
  97. int err, pipe;
  98. BT_DBG("bfusb %p skb %p len %d", data, skb, skb->len);
  99. if (!urb && !(urb = usb_alloc_urb(0, GFP_ATOMIC)))
  100. return -ENOMEM;
  101. pipe = usb_sndbulkpipe(data->udev, data->bulk_out_ep);
  102. usb_fill_bulk_urb(urb, data->udev, pipe, skb->data, skb->len,
  103. bfusb_tx_complete, skb);
  104. scb->urb = urb;
  105. skb_queue_tail(&data->pending_q, skb);
  106. err = usb_submit_urb(urb, GFP_ATOMIC);
  107. if (err) {
  108. BT_ERR("%s bulk tx submit failed urb %p err %d",
  109. data->hdev->name, urb, err);
  110. skb_unlink(skb, &data->pending_q);
  111. usb_free_urb(urb);
  112. } else
  113. atomic_inc(&data->pending_tx);
  114. return err;
  115. }
  116. static void bfusb_tx_wakeup(struct bfusb_data *data)
  117. {
  118. struct sk_buff *skb;
  119. BT_DBG("bfusb %p", data);
  120. if (test_and_set_bit(BFUSB_TX_PROCESS, &data->state)) {
  121. set_bit(BFUSB_TX_WAKEUP, &data->state);
  122. return;
  123. }
  124. do {
  125. clear_bit(BFUSB_TX_WAKEUP, &data->state);
  126. while ((atomic_read(&data->pending_tx) < BFUSB_MAX_BULK_TX) &&
  127. (skb = skb_dequeue(&data->transmit_q))) {
  128. if (bfusb_send_bulk(data, skb) < 0) {
  129. skb_queue_head(&data->transmit_q, skb);
  130. break;
  131. }
  132. }
  133. } while (test_bit(BFUSB_TX_WAKEUP, &data->state));
  134. clear_bit(BFUSB_TX_PROCESS, &data->state);
  135. }
  136. static void bfusb_tx_complete(struct urb *urb)
  137. {
  138. struct sk_buff *skb = (struct sk_buff *) urb->context;
  139. struct bfusb_data *data = (struct bfusb_data *) skb->dev;
  140. BT_DBG("bfusb %p urb %p skb %p len %d", data, urb, skb, skb->len);
  141. atomic_dec(&data->pending_tx);
  142. if (!test_bit(HCI_RUNNING, &data->hdev->flags))
  143. return;
  144. if (!urb->status)
  145. data->hdev->stat.byte_tx += skb->len;
  146. else
  147. data->hdev->stat.err_tx++;
  148. read_lock(&data->lock);
  149. skb_unlink(skb, &data->pending_q);
  150. skb_queue_tail(&data->completed_q, skb);
  151. bfusb_tx_wakeup(data);
  152. read_unlock(&data->lock);
  153. }
  154. static int bfusb_rx_submit(struct bfusb_data *data, struct urb *urb)
  155. {
  156. struct bfusb_data_scb *scb;
  157. struct sk_buff *skb;
  158. int err, pipe, size = HCI_MAX_FRAME_SIZE + 32;
  159. BT_DBG("bfusb %p urb %p", data, urb);
  160. if (!urb && !(urb = usb_alloc_urb(0, GFP_ATOMIC)))
  161. return -ENOMEM;
  162. skb = bt_skb_alloc(size, GFP_ATOMIC);
  163. if (!skb) {
  164. usb_free_urb(urb);
  165. return -ENOMEM;
  166. }
  167. skb->dev = (void *) data;
  168. scb = (struct bfusb_data_scb *) skb->cb;
  169. scb->urb = urb;
  170. pipe = usb_rcvbulkpipe(data->udev, data->bulk_in_ep);
  171. usb_fill_bulk_urb(urb, data->udev, pipe, skb->data, size,
  172. bfusb_rx_complete, skb);
  173. skb_queue_tail(&data->pending_q, skb);
  174. err = usb_submit_urb(urb, GFP_ATOMIC);
  175. if (err) {
  176. BT_ERR("%s bulk rx submit failed urb %p err %d",
  177. data->hdev->name, urb, err);
  178. skb_unlink(skb, &data->pending_q);
  179. kfree_skb(skb);
  180. usb_free_urb(urb);
  181. }
  182. return err;
  183. }
  184. static inline int bfusb_recv_block(struct bfusb_data *data, int hdr, unsigned char *buf, int len)
  185. {
  186. BT_DBG("bfusb %p hdr 0x%02x data %p len %d", data, hdr, buf, len);
  187. if (hdr & 0x10) {
  188. BT_ERR("%s error in block", data->hdev->name);
  189. kfree_skb(data->reassembly);
  190. data->reassembly = NULL;
  191. return -EIO;
  192. }
  193. if (hdr & 0x04) {
  194. struct sk_buff *skb;
  195. unsigned char pkt_type;
  196. int pkt_len = 0;
  197. if (data->reassembly) {
  198. BT_ERR("%s unexpected start block", data->hdev->name);
  199. kfree_skb(data->reassembly);
  200. data->reassembly = NULL;
  201. }
  202. if (len < 1) {
  203. BT_ERR("%s no packet type found", data->hdev->name);
  204. return -EPROTO;
  205. }
  206. pkt_type = *buf++; len--;
  207. switch (pkt_type) {
  208. case HCI_EVENT_PKT:
  209. if (len >= HCI_EVENT_HDR_SIZE) {
  210. struct hci_event_hdr *hdr = (struct hci_event_hdr *) buf;
  211. pkt_len = HCI_EVENT_HDR_SIZE + hdr->plen;
  212. } else {
  213. BT_ERR("%s event block is too short", data->hdev->name);
  214. return -EILSEQ;
  215. }
  216. break;
  217. case HCI_ACLDATA_PKT:
  218. if (len >= HCI_ACL_HDR_SIZE) {
  219. struct hci_acl_hdr *hdr = (struct hci_acl_hdr *) buf;
  220. pkt_len = HCI_ACL_HDR_SIZE + __le16_to_cpu(hdr->dlen);
  221. } else {
  222. BT_ERR("%s data block is too short", data->hdev->name);
  223. return -EILSEQ;
  224. }
  225. break;
  226. case HCI_SCODATA_PKT:
  227. if (len >= HCI_SCO_HDR_SIZE) {
  228. struct hci_sco_hdr *hdr = (struct hci_sco_hdr *) buf;
  229. pkt_len = HCI_SCO_HDR_SIZE + hdr->dlen;
  230. } else {
  231. BT_ERR("%s audio block is too short", data->hdev->name);
  232. return -EILSEQ;
  233. }
  234. break;
  235. }
  236. skb = bt_skb_alloc(pkt_len, GFP_ATOMIC);
  237. if (!skb) {
  238. BT_ERR("%s no memory for the packet", data->hdev->name);
  239. return -ENOMEM;
  240. }
  241. skb->dev = (void *) data->hdev;
  242. bt_cb(skb)->pkt_type = pkt_type;
  243. data->reassembly = skb;
  244. } else {
  245. if (!data->reassembly) {
  246. BT_ERR("%s unexpected continuation block", data->hdev->name);
  247. return -EIO;
  248. }
  249. }
  250. if (len > 0)
  251. memcpy(skb_put(data->reassembly, len), buf, len);
  252. if (hdr & 0x08) {
  253. hci_recv_frame(data->reassembly);
  254. data->reassembly = NULL;
  255. }
  256. return 0;
  257. }
  258. static void bfusb_rx_complete(struct urb *urb)
  259. {
  260. struct sk_buff *skb = (struct sk_buff *) urb->context;
  261. struct bfusb_data *data = (struct bfusb_data *) skb->dev;
  262. unsigned char *buf = urb->transfer_buffer;
  263. int count = urb->actual_length;
  264. int err, hdr, len;
  265. BT_DBG("bfusb %p urb %p skb %p len %d", data, urb, skb, skb->len);
  266. read_lock(&data->lock);
  267. if (!test_bit(HCI_RUNNING, &data->hdev->flags))
  268. goto unlock;
  269. if (urb->status || !count)
  270. goto resubmit;
  271. data->hdev->stat.byte_rx += count;
  272. skb_put(skb, count);
  273. while (count) {
  274. hdr = buf[0] | (buf[1] << 8);
  275. if (hdr & 0x4000) {
  276. len = 0;
  277. count -= 2;
  278. buf += 2;
  279. } else {
  280. len = (buf[2] == 0) ? 256 : buf[2];
  281. count -= 3;
  282. buf += 3;
  283. }
  284. if (count < len) {
  285. BT_ERR("%s block extends over URB buffer ranges",
  286. data->hdev->name);
  287. }
  288. if ((hdr & 0xe1) == 0xc1)
  289. bfusb_recv_block(data, hdr, buf, len);
  290. count -= len;
  291. buf += len;
  292. }
  293. skb_unlink(skb, &data->pending_q);
  294. kfree_skb(skb);
  295. bfusb_rx_submit(data, urb);
  296. read_unlock(&data->lock);
  297. return;
  298. resubmit:
  299. urb->dev = data->udev;
  300. err = usb_submit_urb(urb, GFP_ATOMIC);
  301. if (err) {
  302. BT_ERR("%s bulk resubmit failed urb %p err %d",
  303. data->hdev->name, urb, err);
  304. }
  305. unlock:
  306. read_unlock(&data->lock);
  307. }
  308. static int bfusb_open(struct hci_dev *hdev)
  309. {
  310. struct bfusb_data *data = hdev->driver_data;
  311. unsigned long flags;
  312. int i, err;
  313. BT_DBG("hdev %p bfusb %p", hdev, data);
  314. if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
  315. return 0;
  316. write_lock_irqsave(&data->lock, flags);
  317. err = bfusb_rx_submit(data, NULL);
  318. if (!err) {
  319. for (i = 1; i < BFUSB_MAX_BULK_RX; i++)
  320. bfusb_rx_submit(data, NULL);
  321. } else {
  322. clear_bit(HCI_RUNNING, &hdev->flags);
  323. }
  324. write_unlock_irqrestore(&data->lock, flags);
  325. return err;
  326. }
  327. static int bfusb_flush(struct hci_dev *hdev)
  328. {
  329. struct bfusb_data *data = hdev->driver_data;
  330. BT_DBG("hdev %p bfusb %p", hdev, data);
  331. skb_queue_purge(&data->transmit_q);
  332. return 0;
  333. }
  334. static int bfusb_close(struct hci_dev *hdev)
  335. {
  336. struct bfusb_data *data = hdev->driver_data;
  337. unsigned long flags;
  338. BT_DBG("hdev %p bfusb %p", hdev, data);
  339. if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
  340. return 0;
  341. write_lock_irqsave(&data->lock, flags);
  342. write_unlock_irqrestore(&data->lock, flags);
  343. bfusb_unlink_urbs(data);
  344. bfusb_flush(hdev);
  345. return 0;
  346. }
  347. static int bfusb_send_frame(struct sk_buff *skb)
  348. {
  349. struct hci_dev *hdev = (struct hci_dev *) skb->dev;
  350. struct bfusb_data *data;
  351. struct sk_buff *nskb;
  352. unsigned char buf[3];
  353. int sent = 0, size, count;
  354. BT_DBG("hdev %p skb %p type %d len %d", hdev, skb, bt_cb(skb)->pkt_type, skb->len);
  355. if (!hdev) {
  356. BT_ERR("Frame for unknown HCI device (hdev=NULL)");
  357. return -ENODEV;
  358. }
  359. if (!test_bit(HCI_RUNNING, &hdev->flags))
  360. return -EBUSY;
  361. data = hdev->driver_data;
  362. switch (bt_cb(skb)->pkt_type) {
  363. case HCI_COMMAND_PKT:
  364. hdev->stat.cmd_tx++;
  365. break;
  366. case HCI_ACLDATA_PKT:
  367. hdev->stat.acl_tx++;
  368. break;
  369. case HCI_SCODATA_PKT:
  370. hdev->stat.sco_tx++;
  371. break;
  372. };
  373. /* Prepend skb with frame type */
  374. memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
  375. count = skb->len;
  376. /* Max HCI frame size seems to be 1511 + 1 */
  377. nskb = bt_skb_alloc(count + 32, GFP_ATOMIC);
  378. if (!nskb) {
  379. BT_ERR("Can't allocate memory for new packet");
  380. return -ENOMEM;
  381. }
  382. nskb->dev = (void *) data;
  383. while (count) {
  384. size = min_t(uint, count, BFUSB_MAX_BLOCK_SIZE);
  385. buf[0] = 0xc1 | ((sent == 0) ? 0x04 : 0) | ((count == size) ? 0x08 : 0);
  386. buf[1] = 0x00;
  387. buf[2] = (size == BFUSB_MAX_BLOCK_SIZE) ? 0 : size;
  388. memcpy(skb_put(nskb, 3), buf, 3);
  389. skb_copy_from_linear_data_offset(skb, sent, skb_put(nskb, size), size);
  390. sent += size;
  391. count -= size;
  392. }
  393. /* Don't send frame with multiple size of bulk max packet */
  394. if ((nskb->len % data->bulk_pkt_size) == 0) {
  395. buf[0] = 0xdd;
  396. buf[1] = 0x00;
  397. memcpy(skb_put(nskb, 2), buf, 2);
  398. }
  399. read_lock(&data->lock);
  400. skb_queue_tail(&data->transmit_q, nskb);
  401. bfusb_tx_wakeup(data);
  402. read_unlock(&data->lock);
  403. kfree_skb(skb);
  404. return 0;
  405. }
  406. static void bfusb_destruct(struct hci_dev *hdev)
  407. {
  408. struct bfusb_data *data = hdev->driver_data;
  409. BT_DBG("hdev %p bfusb %p", hdev, data);
  410. kfree(data);
  411. }
  412. static int bfusb_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg)
  413. {
  414. return -ENOIOCTLCMD;
  415. }
  416. static int bfusb_load_firmware(struct bfusb_data *data,
  417. const unsigned char *firmware, int count)
  418. {
  419. unsigned char *buf;
  420. int err, pipe, len, size, sent = 0;
  421. BT_DBG("bfusb %p udev %p", data, data->udev);
  422. BT_INFO("BlueFRITZ! USB loading firmware");
  423. pipe = usb_sndctrlpipe(data->udev, 0);
  424. if (usb_control_msg(data->udev, pipe, USB_REQ_SET_CONFIGURATION,
  425. 0, 1, 0, NULL, 0, USB_CTRL_SET_TIMEOUT) < 0) {
  426. BT_ERR("Can't change to loading configuration");
  427. return -EBUSY;
  428. }
  429. data->udev->toggle[0] = data->udev->toggle[1] = 0;
  430. buf = kmalloc(BFUSB_MAX_BLOCK_SIZE + 3, GFP_ATOMIC);
  431. if (!buf) {
  432. BT_ERR("Can't allocate memory chunk for firmware");
  433. return -ENOMEM;
  434. }
  435. pipe = usb_sndbulkpipe(data->udev, data->bulk_out_ep);
  436. while (count) {
  437. size = min_t(uint, count, BFUSB_MAX_BLOCK_SIZE + 3);
  438. memcpy(buf, firmware + sent, size);
  439. err = usb_bulk_msg(data->udev, pipe, buf, size,
  440. &len, BFUSB_BLOCK_TIMEOUT);
  441. if (err || (len != size)) {
  442. BT_ERR("Error in firmware loading");
  443. goto error;
  444. }
  445. sent += size;
  446. count -= size;
  447. }
  448. err = usb_bulk_msg(data->udev, pipe, NULL, 0,
  449. &len, BFUSB_BLOCK_TIMEOUT);
  450. if (err < 0) {
  451. BT_ERR("Error in null packet request");
  452. goto error;
  453. }
  454. pipe = usb_sndctrlpipe(data->udev, 0);
  455. err = usb_control_msg(data->udev, pipe, USB_REQ_SET_CONFIGURATION,
  456. 0, 2, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
  457. if (err < 0) {
  458. BT_ERR("Can't change to running configuration");
  459. goto error;
  460. }
  461. data->udev->toggle[0] = data->udev->toggle[1] = 0;
  462. BT_INFO("BlueFRITZ! USB device ready");
  463. kfree(buf);
  464. return 0;
  465. error:
  466. kfree(buf);
  467. pipe = usb_sndctrlpipe(data->udev, 0);
  468. usb_control_msg(data->udev, pipe, USB_REQ_SET_CONFIGURATION,
  469. 0, 0, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
  470. return err;
  471. }
  472. static int bfusb_probe(struct usb_interface *intf, const struct usb_device_id *id)
  473. {
  474. const struct firmware *firmware;
  475. struct usb_device *udev = interface_to_usbdev(intf);
  476. struct usb_host_endpoint *bulk_out_ep;
  477. struct usb_host_endpoint *bulk_in_ep;
  478. struct hci_dev *hdev;
  479. struct bfusb_data *data;
  480. BT_DBG("intf %p id %p", intf, id);
  481. /* Check number of endpoints */
  482. if (intf->cur_altsetting->desc.bNumEndpoints < 2)
  483. return -EIO;
  484. bulk_out_ep = &intf->cur_altsetting->endpoint[0];
  485. bulk_in_ep = &intf->cur_altsetting->endpoint[1];
  486. if (!bulk_out_ep || !bulk_in_ep) {
  487. BT_ERR("Bulk endpoints not found");
  488. goto done;
  489. }
  490. /* Initialize control structure and load firmware */
  491. data = kzalloc(sizeof(struct bfusb_data), GFP_KERNEL);
  492. if (!data) {
  493. BT_ERR("Can't allocate memory for control structure");
  494. goto done;
  495. }
  496. data->udev = udev;
  497. data->bulk_in_ep = bulk_in_ep->desc.bEndpointAddress;
  498. data->bulk_out_ep = bulk_out_ep->desc.bEndpointAddress;
  499. data->bulk_pkt_size = le16_to_cpu(bulk_out_ep->desc.wMaxPacketSize);
  500. rwlock_init(&data->lock);
  501. data->reassembly = NULL;
  502. skb_queue_head_init(&data->transmit_q);
  503. skb_queue_head_init(&data->pending_q);
  504. skb_queue_head_init(&data->completed_q);
  505. if (request_firmware(&firmware, "bfubase.frm", &udev->dev) < 0) {
  506. BT_ERR("Firmware request failed");
  507. goto error;
  508. }
  509. BT_DBG("firmware data %p size %zu", firmware->data, firmware->size);
  510. if (bfusb_load_firmware(data, firmware->data, firmware->size) < 0) {
  511. BT_ERR("Firmware loading failed");
  512. goto release;
  513. }
  514. release_firmware(firmware);
  515. /* Initialize and register HCI device */
  516. hdev = hci_alloc_dev();
  517. if (!hdev) {
  518. BT_ERR("Can't allocate HCI device");
  519. goto error;
  520. }
  521. data->hdev = hdev;
  522. hdev->bus = HCI_USB;
  523. hdev->driver_data = data;
  524. SET_HCIDEV_DEV(hdev, &intf->dev);
  525. hdev->open = bfusb_open;
  526. hdev->close = bfusb_close;
  527. hdev->flush = bfusb_flush;
  528. hdev->send = bfusb_send_frame;
  529. hdev->destruct = bfusb_destruct;
  530. hdev->ioctl = bfusb_ioctl;
  531. hdev->owner = THIS_MODULE;
  532. if (hci_register_dev(hdev) < 0) {
  533. BT_ERR("Can't register HCI device");
  534. hci_free_dev(hdev);
  535. goto error;
  536. }
  537. usb_set_intfdata(intf, data);
  538. return 0;
  539. release:
  540. release_firmware(firmware);
  541. error:
  542. kfree(data);
  543. done:
  544. return -EIO;
  545. }
  546. static void bfusb_disconnect(struct usb_interface *intf)
  547. {
  548. struct bfusb_data *data = usb_get_intfdata(intf);
  549. struct hci_dev *hdev = data->hdev;
  550. BT_DBG("intf %p", intf);
  551. if (!hdev)
  552. return;
  553. usb_set_intfdata(intf, NULL);
  554. bfusb_close(hdev);
  555. if (hci_unregister_dev(hdev) < 0)
  556. BT_ERR("Can't unregister HCI device %s", hdev->name);
  557. hci_free_dev(hdev);
  558. }
  559. static struct usb_driver bfusb_driver = {
  560. .name = "bfusb",
  561. .probe = bfusb_probe,
  562. .disconnect = bfusb_disconnect,
  563. .id_table = bfusb_table,
  564. };
  565. static int __init bfusb_init(void)
  566. {
  567. int err;
  568. BT_INFO("BlueFRITZ! USB driver ver %s", VERSION);
  569. err = usb_register(&bfusb_driver);
  570. if (err < 0)
  571. BT_ERR("Failed to register BlueFRITZ! USB driver");
  572. return err;
  573. }
  574. static void __exit bfusb_exit(void)
  575. {
  576. usb_deregister(&bfusb_driver);
  577. }
  578. module_init(bfusb_init);
  579. module_exit(bfusb_exit);
  580. MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
  581. MODULE_DESCRIPTION("BlueFRITZ! USB driver ver " VERSION);
  582. MODULE_VERSION(VERSION);
  583. MODULE_LICENSE("GPL");
  584. MODULE_FIRMWARE("bfubase.frm");