blob: 5a321b4076aababf90bdaac7b6bdb1cae173e4f6 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 *
4 * AVM BlueFRITZ! USB driver
5 *
Marcel Holtmann9c724352006-07-06 15:45:23 +02006 * Copyright (C) 2003-2006 Marcel Holtmann <marcel@holtmann.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/module.h>
10
11#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/slab.h>
14#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/errno.h>
16#include <linux/skbuff.h>
17
18#include <linux/device.h>
19#include <linux/firmware.h>
20
21#include <linux/usb.h>
22
23#include <net/bluetooth/bluetooth.h>
24#include <net/bluetooth/hci_core.h>
25
Marcel Holtmann943d56b2008-08-07 22:26:55 +020026#define VERSION "1.2"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28static struct usb_driver bfusb_driver;
29
Marcel Holtmann9712d592013-10-11 07:46:19 -070030static const struct usb_device_id bfusb_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 /* AVM BlueFRITZ! USB */
32 { USB_DEVICE(0x057c, 0x2200) },
33
34 { } /* Terminating entry */
35};
36
37MODULE_DEVICE_TABLE(usb, bfusb_table);
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#define BFUSB_MAX_BLOCK_SIZE 256
40
41#define BFUSB_BLOCK_TIMEOUT 3000
42
43#define BFUSB_TX_PROCESS 1
44#define BFUSB_TX_WAKEUP 2
45
46#define BFUSB_MAX_BULK_TX 2
47#define BFUSB_MAX_BULK_RX 2
48
Marcel Holtmann9c724352006-07-06 15:45:23 +020049struct bfusb_data {
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 struct hci_dev *hdev;
51
52 unsigned long state;
53
54 struct usb_device *udev;
55
56 unsigned int bulk_in_ep;
57 unsigned int bulk_out_ep;
58 unsigned int bulk_pkt_size;
59
60 rwlock_t lock;
61
62 struct sk_buff_head transmit_q;
63
64 struct sk_buff *reassembly;
65
66 atomic_t pending_tx;
67 struct sk_buff_head pending_q;
68 struct sk_buff_head completed_q;
69};
70
Marcel Holtmann9c724352006-07-06 15:45:23 +020071struct bfusb_data_scb {
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 struct urb *urb;
73};
74
David Howells7d12e782006-10-05 14:55:46 +010075static void bfusb_tx_complete(struct urb *urb);
76static void bfusb_rx_complete(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Marcel Holtmann9c724352006-07-06 15:45:23 +020078static struct urb *bfusb_get_completed(struct bfusb_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
80 struct sk_buff *skb;
81 struct urb *urb = NULL;
82
Marcel Holtmann9c724352006-07-06 15:45:23 +020083 BT_DBG("bfusb %p", data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Marcel Holtmann9c724352006-07-06 15:45:23 +020085 skb = skb_dequeue(&data->completed_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 if (skb) {
Marcel Holtmann9c724352006-07-06 15:45:23 +020087 urb = ((struct bfusb_data_scb *) skb->cb)->urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 kfree_skb(skb);
89 }
90
91 return urb;
92}
93
Marcel Holtmann9c724352006-07-06 15:45:23 +020094static void bfusb_unlink_urbs(struct bfusb_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 struct sk_buff *skb;
97 struct urb *urb;
98
Marcel Holtmann9c724352006-07-06 15:45:23 +020099 BT_DBG("bfusb %p", data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Marcel Holtmann9c724352006-07-06 15:45:23 +0200101 while ((skb = skb_dequeue(&data->pending_q))) {
102 urb = ((struct bfusb_data_scb *) skb->cb)->urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 usb_kill_urb(urb);
Marcel Holtmann9c724352006-07-06 15:45:23 +0200104 skb_queue_tail(&data->completed_q, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
106
Marcel Holtmann9c724352006-07-06 15:45:23 +0200107 while ((urb = bfusb_get_completed(data)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 usb_free_urb(urb);
109}
110
Marcel Holtmann9c724352006-07-06 15:45:23 +0200111static int bfusb_send_bulk(struct bfusb_data *data, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Marcel Holtmann9c724352006-07-06 15:45:23 +0200113 struct bfusb_data_scb *scb = (void *) skb->cb;
114 struct urb *urb = bfusb_get_completed(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 int err, pipe;
116
Marcel Holtmann9c724352006-07-06 15:45:23 +0200117 BT_DBG("bfusb %p skb %p len %d", data, skb, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Valentin Iliea08b15e2013-08-12 18:46:00 +0300119 if (!urb) {
120 urb = usb_alloc_urb(0, GFP_ATOMIC);
121 if (!urb)
122 return -ENOMEM;
123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Marcel Holtmann9c724352006-07-06 15:45:23 +0200125 pipe = usb_sndbulkpipe(data->udev, data->bulk_out_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Marcel Holtmann9c724352006-07-06 15:45:23 +0200127 usb_fill_bulk_urb(urb, data->udev, pipe, skb->data, skb->len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 bfusb_tx_complete, skb);
129
130 scb->urb = urb;
131
Marcel Holtmann9c724352006-07-06 15:45:23 +0200132 skb_queue_tail(&data->pending_q, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 err = usb_submit_urb(urb, GFP_ATOMIC);
135 if (err) {
Marcel Holtmann2a4f3902020-03-09 22:44:56 +0100136 bt_dev_err(data->hdev, "bulk tx submit failed urb %p err %d",
137 urb, err);
Marcel Holtmann9c724352006-07-06 15:45:23 +0200138 skb_unlink(skb, &data->pending_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 usb_free_urb(urb);
140 } else
Marcel Holtmann9c724352006-07-06 15:45:23 +0200141 atomic_inc(&data->pending_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 return err;
144}
145
Marcel Holtmann9c724352006-07-06 15:45:23 +0200146static void bfusb_tx_wakeup(struct bfusb_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
148 struct sk_buff *skb;
149
Marcel Holtmann9c724352006-07-06 15:45:23 +0200150 BT_DBG("bfusb %p", data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Marcel Holtmann9c724352006-07-06 15:45:23 +0200152 if (test_and_set_bit(BFUSB_TX_PROCESS, &data->state)) {
153 set_bit(BFUSB_TX_WAKEUP, &data->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 return;
155 }
156
157 do {
Marcel Holtmann9c724352006-07-06 15:45:23 +0200158 clear_bit(BFUSB_TX_WAKEUP, &data->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Marcel Holtmann9c724352006-07-06 15:45:23 +0200160 while ((atomic_read(&data->pending_tx) < BFUSB_MAX_BULK_TX) &&
161 (skb = skb_dequeue(&data->transmit_q))) {
162 if (bfusb_send_bulk(data, skb) < 0) {
163 skb_queue_head(&data->transmit_q, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 break;
165 }
166 }
167
Marcel Holtmann9c724352006-07-06 15:45:23 +0200168 } while (test_bit(BFUSB_TX_WAKEUP, &data->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Marcel Holtmann9c724352006-07-06 15:45:23 +0200170 clear_bit(BFUSB_TX_PROCESS, &data->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
David Howells7d12e782006-10-05 14:55:46 +0100173static void bfusb_tx_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
175 struct sk_buff *skb = (struct sk_buff *) urb->context;
Marcel Holtmann9c724352006-07-06 15:45:23 +0200176 struct bfusb_data *data = (struct bfusb_data *) skb->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Marcel Holtmann9c724352006-07-06 15:45:23 +0200178 BT_DBG("bfusb %p urb %p skb %p len %d", data, urb, skb, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Marcel Holtmann9c724352006-07-06 15:45:23 +0200180 atomic_dec(&data->pending_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Marcel Holtmann9c724352006-07-06 15:45:23 +0200182 if (!test_bit(HCI_RUNNING, &data->hdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 return;
184
185 if (!urb->status)
Marcel Holtmann9c724352006-07-06 15:45:23 +0200186 data->hdev->stat.byte_tx += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 else
Marcel Holtmann9c724352006-07-06 15:45:23 +0200188 data->hdev->stat.err_tx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Marcel Holtmann9c724352006-07-06 15:45:23 +0200190 read_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Marcel Holtmann9c724352006-07-06 15:45:23 +0200192 skb_unlink(skb, &data->pending_q);
193 skb_queue_tail(&data->completed_q, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Marcel Holtmann9c724352006-07-06 15:45:23 +0200195 bfusb_tx_wakeup(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Marcel Holtmann9c724352006-07-06 15:45:23 +0200197 read_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
200
Marcel Holtmann9c724352006-07-06 15:45:23 +0200201static int bfusb_rx_submit(struct bfusb_data *data, struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Marcel Holtmann9c724352006-07-06 15:45:23 +0200203 struct bfusb_data_scb *scb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 struct sk_buff *skb;
205 int err, pipe, size = HCI_MAX_FRAME_SIZE + 32;
206
Marcel Holtmanna418b892008-11-30 12:17:28 +0100207 BT_DBG("bfusb %p urb %p", data, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Valentin Iliea08b15e2013-08-12 18:46:00 +0300209 if (!urb) {
210 urb = usb_alloc_urb(0, GFP_ATOMIC);
211 if (!urb)
212 return -ENOMEM;
213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Marcel Holtmann9c724352006-07-06 15:45:23 +0200215 skb = bt_skb_alloc(size, GFP_ATOMIC);
216 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 usb_free_urb(urb);
218 return -ENOMEM;
219 }
220
Marcel Holtmann9c724352006-07-06 15:45:23 +0200221 skb->dev = (void *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Marcel Holtmann9c724352006-07-06 15:45:23 +0200223 scb = (struct bfusb_data_scb *) skb->cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 scb->urb = urb;
225
Marcel Holtmann9c724352006-07-06 15:45:23 +0200226 pipe = usb_rcvbulkpipe(data->udev, data->bulk_in_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Marcel Holtmann9c724352006-07-06 15:45:23 +0200228 usb_fill_bulk_urb(urb, data->udev, pipe, skb->data, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 bfusb_rx_complete, skb);
230
Marcel Holtmann9c724352006-07-06 15:45:23 +0200231 skb_queue_tail(&data->pending_q, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 err = usb_submit_urb(urb, GFP_ATOMIC);
234 if (err) {
Marcel Holtmann2a4f3902020-03-09 22:44:56 +0100235 bt_dev_err(data->hdev, "bulk rx submit failed urb %p err %d",
236 urb, err);
Marcel Holtmann9c724352006-07-06 15:45:23 +0200237 skb_unlink(skb, &data->pending_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 kfree_skb(skb);
239 usb_free_urb(urb);
240 }
241
242 return err;
243}
244
Marcel Holtmann9c724352006-07-06 15:45:23 +0200245static inline int bfusb_recv_block(struct bfusb_data *data, int hdr, unsigned char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
Marcel Holtmann9c724352006-07-06 15:45:23 +0200247 BT_DBG("bfusb %p hdr 0x%02x data %p len %d", data, hdr, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 if (hdr & 0x10) {
Marcel Holtmann2a4f3902020-03-09 22:44:56 +0100250 bt_dev_err(data->hdev, "error in block");
Wei Yongjunb1fb0682009-02-25 18:09:33 +0800251 kfree_skb(data->reassembly);
Marcel Holtmann9c724352006-07-06 15:45:23 +0200252 data->reassembly = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return -EIO;
254 }
255
256 if (hdr & 0x04) {
257 struct sk_buff *skb;
258 unsigned char pkt_type;
259 int pkt_len = 0;
260
Marcel Holtmann9c724352006-07-06 15:45:23 +0200261 if (data->reassembly) {
Marcel Holtmann2a4f3902020-03-09 22:44:56 +0100262 bt_dev_err(data->hdev, "unexpected start block");
Marcel Holtmann9c724352006-07-06 15:45:23 +0200263 kfree_skb(data->reassembly);
264 data->reassembly = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
266
267 if (len < 1) {
Marcel Holtmann2a4f3902020-03-09 22:44:56 +0100268 bt_dev_err(data->hdev, "no packet type found");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 return -EPROTO;
270 }
271
Marcel Holtmann9c724352006-07-06 15:45:23 +0200272 pkt_type = *buf++; len--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 switch (pkt_type) {
275 case HCI_EVENT_PKT:
276 if (len >= HCI_EVENT_HDR_SIZE) {
Marcel Holtmann9c724352006-07-06 15:45:23 +0200277 struct hci_event_hdr *hdr = (struct hci_event_hdr *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 pkt_len = HCI_EVENT_HDR_SIZE + hdr->plen;
279 } else {
Marcel Holtmann2a4f3902020-03-09 22:44:56 +0100280 bt_dev_err(data->hdev, "event block is too short");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 return -EILSEQ;
282 }
283 break;
284
285 case HCI_ACLDATA_PKT:
286 if (len >= HCI_ACL_HDR_SIZE) {
Marcel Holtmann9c724352006-07-06 15:45:23 +0200287 struct hci_acl_hdr *hdr = (struct hci_acl_hdr *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 pkt_len = HCI_ACL_HDR_SIZE + __le16_to_cpu(hdr->dlen);
289 } else {
Marcel Holtmann2a4f3902020-03-09 22:44:56 +0100290 bt_dev_err(data->hdev, "data block is too short");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return -EILSEQ;
292 }
293 break;
294
295 case HCI_SCODATA_PKT:
296 if (len >= HCI_SCO_HDR_SIZE) {
Marcel Holtmann9c724352006-07-06 15:45:23 +0200297 struct hci_sco_hdr *hdr = (struct hci_sco_hdr *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 pkt_len = HCI_SCO_HDR_SIZE + hdr->dlen;
299 } else {
Marcel Holtmann2a4f3902020-03-09 22:44:56 +0100300 bt_dev_err(data->hdev, "audio block is too short");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return -EILSEQ;
302 }
303 break;
304 }
305
306 skb = bt_skb_alloc(pkt_len, GFP_ATOMIC);
307 if (!skb) {
Marcel Holtmann2a4f3902020-03-09 22:44:56 +0100308 bt_dev_err(data->hdev, "no memory for the packet");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 return -ENOMEM;
310 }
311
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100312 hci_skb_pkt_type(skb) = pkt_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Marcel Holtmann9c724352006-07-06 15:45:23 +0200314 data->reassembly = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 } else {
Marcel Holtmann9c724352006-07-06 15:45:23 +0200316 if (!data->reassembly) {
Marcel Holtmann2a4f3902020-03-09 22:44:56 +0100317 bt_dev_err(data->hdev, "unexpected continuation block");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return -EIO;
319 }
320 }
321
322 if (len > 0)
Johannes Berg59ae1d12017-06-16 14:29:20 +0200323 skb_put_data(data->reassembly, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 if (hdr & 0x08) {
Marcel Holtmanne1a26172013-10-10 16:52:43 -0700326 hci_recv_frame(data->hdev, data->reassembly);
Marcel Holtmann9c724352006-07-06 15:45:23 +0200327 data->reassembly = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
329
330 return 0;
331}
332
David Howells7d12e782006-10-05 14:55:46 +0100333static void bfusb_rx_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
335 struct sk_buff *skb = (struct sk_buff *) urb->context;
Marcel Holtmann9c724352006-07-06 15:45:23 +0200336 struct bfusb_data *data = (struct bfusb_data *) skb->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 unsigned char *buf = urb->transfer_buffer;
338 int count = urb->actual_length;
339 int err, hdr, len;
340
Marcel Holtmanna418b892008-11-30 12:17:28 +0100341 BT_DBG("bfusb %p urb %p skb %p len %d", data, urb, skb, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Marcel Holtmann9c724352006-07-06 15:45:23 +0200343 read_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Marcel Holtmann9c724352006-07-06 15:45:23 +0200345 if (!test_bit(HCI_RUNNING, &data->hdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 goto unlock;
347
348 if (urb->status || !count)
349 goto resubmit;
350
Marcel Holtmann9c724352006-07-06 15:45:23 +0200351 data->hdev->stat.byte_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 skb_put(skb, count);
354
355 while (count) {
356 hdr = buf[0] | (buf[1] << 8);
357
358 if (hdr & 0x4000) {
359 len = 0;
360 count -= 2;
361 buf += 2;
362 } else {
363 len = (buf[2] == 0) ? 256 : buf[2];
364 count -= 3;
365 buf += 3;
366 }
367
368 if (count < len) {
Marcel Holtmann2a4f3902020-03-09 22:44:56 +0100369 bt_dev_err(data->hdev, "block extends over URB buffer ranges");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 }
371
372 if ((hdr & 0xe1) == 0xc1)
Marcel Holtmann9c724352006-07-06 15:45:23 +0200373 bfusb_recv_block(data, hdr, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 count -= len;
376 buf += len;
377 }
378
Marcel Holtmann9c724352006-07-06 15:45:23 +0200379 skb_unlink(skb, &data->pending_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 kfree_skb(skb);
381
Marcel Holtmann9c724352006-07-06 15:45:23 +0200382 bfusb_rx_submit(data, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Marcel Holtmann9c724352006-07-06 15:45:23 +0200384 read_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 return;
387
388resubmit:
Marcel Holtmann9c724352006-07-06 15:45:23 +0200389 urb->dev = data->udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 err = usb_submit_urb(urb, GFP_ATOMIC);
392 if (err) {
Marcel Holtmann2a4f3902020-03-09 22:44:56 +0100393 bt_dev_err(data->hdev, "bulk resubmit failed urb %p err %d",
394 urb, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
396
397unlock:
Marcel Holtmann9c724352006-07-06 15:45:23 +0200398 read_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401static int bfusb_open(struct hci_dev *hdev)
402{
David Herrmann155961e2012-02-09 21:58:32 +0100403 struct bfusb_data *data = hci_get_drvdata(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 unsigned long flags;
405 int i, err;
406
Marcel Holtmann9c724352006-07-06 15:45:23 +0200407 BT_DBG("hdev %p bfusb %p", hdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Marcel Holtmann9c724352006-07-06 15:45:23 +0200409 write_lock_irqsave(&data->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Marcel Holtmann9c724352006-07-06 15:45:23 +0200411 err = bfusb_rx_submit(data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (!err) {
413 for (i = 1; i < BFUSB_MAX_BULK_RX; i++)
Marcel Holtmann9c724352006-07-06 15:45:23 +0200414 bfusb_rx_submit(data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
416
Marcel Holtmann9c724352006-07-06 15:45:23 +0200417 write_unlock_irqrestore(&data->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 return err;
420}
421
422static int bfusb_flush(struct hci_dev *hdev)
423{
David Herrmann155961e2012-02-09 21:58:32 +0100424 struct bfusb_data *data = hci_get_drvdata(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Marcel Holtmann9c724352006-07-06 15:45:23 +0200426 BT_DBG("hdev %p bfusb %p", hdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Marcel Holtmann9c724352006-07-06 15:45:23 +0200428 skb_queue_purge(&data->transmit_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 return 0;
431}
432
433static int bfusb_close(struct hci_dev *hdev)
434{
David Herrmann155961e2012-02-09 21:58:32 +0100435 struct bfusb_data *data = hci_get_drvdata(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 unsigned long flags;
437
Marcel Holtmann9c724352006-07-06 15:45:23 +0200438 BT_DBG("hdev %p bfusb %p", hdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Marcel Holtmann9c724352006-07-06 15:45:23 +0200440 write_lock_irqsave(&data->lock, flags);
441 write_unlock_irqrestore(&data->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Marcel Holtmann9c724352006-07-06 15:45:23 +0200443 bfusb_unlink_urbs(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 bfusb_flush(hdev);
445
446 return 0;
447}
448
Marcel Holtmann7bd8f092013-10-11 06:19:18 -0700449static int bfusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Marcel Holtmannaae26272013-10-11 07:00:57 -0700451 struct bfusb_data *data = hci_get_drvdata(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 struct sk_buff *nskb;
453 unsigned char buf[3];
454 int sent = 0, size, count;
455
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100456 BT_DBG("hdev %p skb %p type %d len %d", hdev, skb,
457 hci_skb_pkt_type(skb), skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100459 switch (hci_skb_pkt_type(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 case HCI_COMMAND_PKT:
461 hdev->stat.cmd_tx++;
462 break;
463 case HCI_ACLDATA_PKT:
464 hdev->stat.acl_tx++;
465 break;
466 case HCI_SCODATA_PKT:
467 hdev->stat.sco_tx++;
468 break;
Prasanna Karthika03e33d2015-07-05 09:49:27 +0000469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 /* Prepend skb with frame type */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100472 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 count = skb->len;
475
476 /* Max HCI frame size seems to be 1511 + 1 */
Jia-Ju Bai67095142018-07-23 11:09:00 +0800477 nskb = bt_skb_alloc(count + 32, GFP_KERNEL);
Marcel Holtmann9c724352006-07-06 15:45:23 +0200478 if (!nskb) {
Marcel Holtmann2a4f3902020-03-09 22:44:56 +0100479 bt_dev_err(hdev, "Can't allocate memory for new packet");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return -ENOMEM;
481 }
482
Marcel Holtmann9c724352006-07-06 15:45:23 +0200483 nskb->dev = (void *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 while (count) {
486 size = min_t(uint, count, BFUSB_MAX_BLOCK_SIZE);
487
488 buf[0] = 0xc1 | ((sent == 0) ? 0x04 : 0) | ((count == size) ? 0x08 : 0);
489 buf[1] = 0x00;
490 buf[2] = (size == BFUSB_MAX_BLOCK_SIZE) ? 0 : size;
491
Johannes Berg59ae1d12017-06-16 14:29:20 +0200492 skb_put_data(nskb, buf, 3);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300493 skb_copy_from_linear_data_offset(skb, sent, skb_put(nskb, size), size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 sent += size;
496 count -= size;
497 }
498
499 /* Don't send frame with multiple size of bulk max packet */
Marcel Holtmann9c724352006-07-06 15:45:23 +0200500 if ((nskb->len % data->bulk_pkt_size) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 buf[0] = 0xdd;
502 buf[1] = 0x00;
Johannes Berg59ae1d12017-06-16 14:29:20 +0200503 skb_put_data(nskb, buf, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
505
Marcel Holtmann9c724352006-07-06 15:45:23 +0200506 read_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Marcel Holtmann9c724352006-07-06 15:45:23 +0200508 skb_queue_tail(&data->transmit_q, nskb);
509 bfusb_tx_wakeup(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Marcel Holtmann9c724352006-07-06 15:45:23 +0200511 read_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 kfree_skb(skb);
514
515 return 0;
516}
517
David Woodhouse8187b4f2008-05-23 23:56:51 +0100518static int bfusb_load_firmware(struct bfusb_data *data,
519 const unsigned char *firmware, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
521 unsigned char *buf;
522 int err, pipe, len, size, sent = 0;
523
Marcel Holtmann9c724352006-07-06 15:45:23 +0200524 BT_DBG("bfusb %p udev %p", data, data->udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 BT_INFO("BlueFRITZ! USB loading firmware");
527
David Herrmann7f103a02011-10-26 11:22:46 +0200528 buf = kmalloc(BFUSB_MAX_BLOCK_SIZE + 3, GFP_KERNEL);
529 if (!buf) {
530 BT_ERR("Can't allocate memory chunk for firmware");
531 return -ENOMEM;
532 }
533
Marcel Holtmann9c724352006-07-06 15:45:23 +0200534 pipe = usb_sndctrlpipe(data->udev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Marcel Holtmann9c724352006-07-06 15:45:23 +0200536 if (usb_control_msg(data->udev, pipe, USB_REQ_SET_CONFIGURATION,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 0, 1, 0, NULL, 0, USB_CTRL_SET_TIMEOUT) < 0) {
538 BT_ERR("Can't change to loading configuration");
David Herrmann7f103a02011-10-26 11:22:46 +0200539 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return -EBUSY;
541 }
542
Marcel Holtmann9c724352006-07-06 15:45:23 +0200543 data->udev->toggle[0] = data->udev->toggle[1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Marcel Holtmann9c724352006-07-06 15:45:23 +0200545 pipe = usb_sndbulkpipe(data->udev, data->bulk_out_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 while (count) {
548 size = min_t(uint, count, BFUSB_MAX_BLOCK_SIZE + 3);
549
550 memcpy(buf, firmware + sent, size);
551
Marcel Holtmann9c724352006-07-06 15:45:23 +0200552 err = usb_bulk_msg(data->udev, pipe, buf, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 &len, BFUSB_BLOCK_TIMEOUT);
554
555 if (err || (len != size)) {
556 BT_ERR("Error in firmware loading");
557 goto error;
558 }
559
560 sent += size;
561 count -= size;
562 }
563
Marcel Holtmann9c724352006-07-06 15:45:23 +0200564 err = usb_bulk_msg(data->udev, pipe, NULL, 0,
565 &len, BFUSB_BLOCK_TIMEOUT);
566 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 BT_ERR("Error in null packet request");
568 goto error;
569 }
570
Marcel Holtmann9c724352006-07-06 15:45:23 +0200571 pipe = usb_sndctrlpipe(data->udev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Marcel Holtmann9c724352006-07-06 15:45:23 +0200573 err = usb_control_msg(data->udev, pipe, USB_REQ_SET_CONFIGURATION,
574 0, 2, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
575 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 BT_ERR("Can't change to running configuration");
577 goto error;
578 }
579
Marcel Holtmann9c724352006-07-06 15:45:23 +0200580 data->udev->toggle[0] = data->udev->toggle[1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 BT_INFO("BlueFRITZ! USB device ready");
583
584 kfree(buf);
585 return 0;
586
587error:
588 kfree(buf);
589
Marcel Holtmann9c724352006-07-06 15:45:23 +0200590 pipe = usb_sndctrlpipe(data->udev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Marcel Holtmann9c724352006-07-06 15:45:23 +0200592 usb_control_msg(data->udev, pipe, USB_REQ_SET_CONFIGURATION,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 0, 0, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
594
595 return err;
596}
597
598static int bfusb_probe(struct usb_interface *intf, const struct usb_device_id *id)
599{
600 const struct firmware *firmware;
601 struct usb_device *udev = interface_to_usbdev(intf);
602 struct usb_host_endpoint *bulk_out_ep;
603 struct usb_host_endpoint *bulk_in_ep;
604 struct hci_dev *hdev;
Marcel Holtmann9c724352006-07-06 15:45:23 +0200605 struct bfusb_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 BT_DBG("intf %p id %p", intf, id);
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 /* Check number of endpoints */
610 if (intf->cur_altsetting->desc.bNumEndpoints < 2)
611 return -EIO;
612
613 bulk_out_ep = &intf->cur_altsetting->endpoint[0];
614 bulk_in_ep = &intf->cur_altsetting->endpoint[1];
615
616 if (!bulk_out_ep || !bulk_in_ep) {
617 BT_ERR("Bulk endpoints not found");
618 goto done;
619 }
620
621 /* Initialize control structure and load firmware */
Sachin Kamat0213cd82012-07-27 12:38:32 +0530622 data = devm_kzalloc(&intf->dev, sizeof(struct bfusb_data), GFP_KERNEL);
Syam Sidhardhanf71e8232015-12-22 19:30:19 +0530623 if (!data)
Syam Sidhardhan7ddb6922015-12-22 19:30:20 +0530624 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Marcel Holtmann9c724352006-07-06 15:45:23 +0200626 data->udev = udev;
627 data->bulk_in_ep = bulk_in_ep->desc.bEndpointAddress;
628 data->bulk_out_ep = bulk_out_ep->desc.bEndpointAddress;
629 data->bulk_pkt_size = le16_to_cpu(bulk_out_ep->desc.wMaxPacketSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Marcel Holtmann9c724352006-07-06 15:45:23 +0200631 rwlock_init(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Marcel Holtmann9c724352006-07-06 15:45:23 +0200633 data->reassembly = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Marcel Holtmann9c724352006-07-06 15:45:23 +0200635 skb_queue_head_init(&data->transmit_q);
636 skb_queue_head_init(&data->pending_q);
637 skb_queue_head_init(&data->completed_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 if (request_firmware(&firmware, "bfubase.frm", &udev->dev) < 0) {
640 BT_ERR("Firmware request failed");
Sachin Kamat0213cd82012-07-27 12:38:32 +0530641 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
643
Marcel Holtmanna418b892008-11-30 12:17:28 +0100644 BT_DBG("firmware data %p size %zu", firmware->data, firmware->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Marcel Holtmann9c724352006-07-06 15:45:23 +0200646 if (bfusb_load_firmware(data, firmware->data, firmware->size) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 BT_ERR("Firmware loading failed");
648 goto release;
649 }
650
651 release_firmware(firmware);
652
653 /* Initialize and register HCI device */
654 hdev = hci_alloc_dev();
655 if (!hdev) {
656 BT_ERR("Can't allocate HCI device");
Sachin Kamat0213cd82012-07-27 12:38:32 +0530657 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
659
Marcel Holtmann9c724352006-07-06 15:45:23 +0200660 data->hdev = hdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Marcel Holtmannc13854c2010-02-08 15:27:07 +0100662 hdev->bus = HCI_USB;
David Herrmann155961e2012-02-09 21:58:32 +0100663 hci_set_drvdata(hdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 SET_HCIDEV_DEV(hdev, &intf->dev);
665
Marcel Holtmann19cf55a2013-10-10 10:50:00 -0700666 hdev->open = bfusb_open;
667 hdev->close = bfusb_close;
668 hdev->flush = bfusb_flush;
669 hdev->send = bfusb_send_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Marcel Holtmann9ef80d402014-12-26 04:42:32 +0100671 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 if (hci_register_dev(hdev) < 0) {
674 BT_ERR("Can't register HCI device");
675 hci_free_dev(hdev);
Sachin Kamat0213cd82012-07-27 12:38:32 +0530676 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
678
Marcel Holtmann9c724352006-07-06 15:45:23 +0200679 usb_set_intfdata(intf, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 return 0;
682
683release:
684 release_firmware(firmware);
685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686done:
687 return -EIO;
688}
689
690static void bfusb_disconnect(struct usb_interface *intf)
691{
Marcel Holtmann9c724352006-07-06 15:45:23 +0200692 struct bfusb_data *data = usb_get_intfdata(intf);
693 struct hci_dev *hdev = data->hdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 BT_DBG("intf %p", intf);
696
697 if (!hdev)
698 return;
699
700 usb_set_intfdata(intf, NULL);
701
702 bfusb_close(hdev);
703
David Herrmann13ea4012011-10-26 10:43:18 +0200704 hci_unregister_dev(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 hci_free_dev(hdev);
706}
707
708static struct usb_driver bfusb_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 .name = "bfusb",
710 .probe = bfusb_probe,
711 .disconnect = bfusb_disconnect,
712 .id_table = bfusb_table,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -0700713 .disable_hub_initiated_lpm = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714};
715
Greg Kroah-Hartman93f15082011-11-18 09:47:34 -0800716module_usb_driver(bfusb_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
719MODULE_DESCRIPTION("BlueFRITZ! USB driver ver " VERSION);
720MODULE_VERSION(VERSION);
721MODULE_LICENSE("GPL");
Marcel Holtmann23121192007-02-17 23:59:02 +0100722MODULE_FIRMWARE("bfubase.frm");