blob: 0e5954cac98e0c69fca26e450e972f8424cfe548 [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) {
136 BT_ERR("%s bulk tx submit failed urb %p err %d",
Marcel Holtmann9c724352006-07-06 15:45:23 +0200137 data->hdev->name, urb, err);
138 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) {
235 BT_ERR("%s bulk rx submit failed urb %p err %d",
Marcel Holtmann9c724352006-07-06 15:45:23 +0200236 data->hdev->name, urb, err);
237 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 Holtmann9c724352006-07-06 15:45:23 +0200250 BT_ERR("%s error in block", data->hdev->name);
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) {
262 BT_ERR("%s unexpected start block", data->hdev->name);
263 kfree_skb(data->reassembly);
264 data->reassembly = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
266
267 if (len < 1) {
Marcel Holtmann9c724352006-07-06 15:45:23 +0200268 BT_ERR("%s no packet type found", data->hdev->name);
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 Holtmann9c724352006-07-06 15:45:23 +0200280 BT_ERR("%s event block is too short", data->hdev->name);
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 Holtmann9c724352006-07-06 15:45:23 +0200290 BT_ERR("%s data block is too short", data->hdev->name);
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 Holtmann9c724352006-07-06 15:45:23 +0200300 BT_ERR("%s audio block is too short", data->hdev->name);
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 Holtmann9c724352006-07-06 15:45:23 +0200308 BT_ERR("%s no memory for the packet", data->hdev->name);
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) {
317 BT_ERR("%s unexpected continuation block", data->hdev->name);
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) {
369 BT_ERR("%s block extends over URB buffer ranges",
Marcel Holtmann9c724352006-07-06 15:45:23 +0200370 data->hdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
372
373 if ((hdr & 0xe1) == 0xc1)
Marcel Holtmann9c724352006-07-06 15:45:23 +0200374 bfusb_recv_block(data, hdr, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 count -= len;
377 buf += len;
378 }
379
Marcel Holtmann9c724352006-07-06 15:45:23 +0200380 skb_unlink(skb, &data->pending_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 kfree_skb(skb);
382
Marcel Holtmann9c724352006-07-06 15:45:23 +0200383 bfusb_rx_submit(data, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Marcel Holtmann9c724352006-07-06 15:45:23 +0200385 read_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 return;
388
389resubmit:
Marcel Holtmann9c724352006-07-06 15:45:23 +0200390 urb->dev = data->udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 err = usb_submit_urb(urb, GFP_ATOMIC);
393 if (err) {
394 BT_ERR("%s bulk resubmit failed urb %p err %d",
Marcel Holtmann9c724352006-07-06 15:45:23 +0200395 data->hdev->name, urb, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
397
398unlock:
Marcel Holtmann9c724352006-07-06 15:45:23 +0200399 read_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402static int bfusb_open(struct hci_dev *hdev)
403{
David Herrmann155961e2012-02-09 21:58:32 +0100404 struct bfusb_data *data = hci_get_drvdata(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 unsigned long flags;
406 int i, err;
407
Marcel Holtmann9c724352006-07-06 15:45:23 +0200408 BT_DBG("hdev %p bfusb %p", hdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Marcel Holtmann9c724352006-07-06 15:45:23 +0200410 write_lock_irqsave(&data->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Marcel Holtmann9c724352006-07-06 15:45:23 +0200412 err = bfusb_rx_submit(data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 if (!err) {
414 for (i = 1; i < BFUSB_MAX_BULK_RX; i++)
Marcel Holtmann9c724352006-07-06 15:45:23 +0200415 bfusb_rx_submit(data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
417
Marcel Holtmann9c724352006-07-06 15:45:23 +0200418 write_unlock_irqrestore(&data->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 return err;
421}
422
423static int bfusb_flush(struct hci_dev *hdev)
424{
David Herrmann155961e2012-02-09 21:58:32 +0100425 struct bfusb_data *data = hci_get_drvdata(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Marcel Holtmann9c724352006-07-06 15:45:23 +0200427 BT_DBG("hdev %p bfusb %p", hdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Marcel Holtmann9c724352006-07-06 15:45:23 +0200429 skb_queue_purge(&data->transmit_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 return 0;
432}
433
434static int bfusb_close(struct hci_dev *hdev)
435{
David Herrmann155961e2012-02-09 21:58:32 +0100436 struct bfusb_data *data = hci_get_drvdata(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 unsigned long flags;
438
Marcel Holtmann9c724352006-07-06 15:45:23 +0200439 BT_DBG("hdev %p bfusb %p", hdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Marcel Holtmann9c724352006-07-06 15:45:23 +0200441 write_lock_irqsave(&data->lock, flags);
442 write_unlock_irqrestore(&data->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Marcel Holtmann9c724352006-07-06 15:45:23 +0200444 bfusb_unlink_urbs(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 bfusb_flush(hdev);
446
447 return 0;
448}
449
Marcel Holtmann7bd8f092013-10-11 06:19:18 -0700450static int bfusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Marcel Holtmannaae26272013-10-11 07:00:57 -0700452 struct bfusb_data *data = hci_get_drvdata(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 struct sk_buff *nskb;
454 unsigned char buf[3];
455 int sent = 0, size, count;
456
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100457 BT_DBG("hdev %p skb %p type %d len %d", hdev, skb,
458 hci_skb_pkt_type(skb), skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100460 switch (hci_skb_pkt_type(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 case HCI_COMMAND_PKT:
462 hdev->stat.cmd_tx++;
463 break;
464 case HCI_ACLDATA_PKT:
465 hdev->stat.acl_tx++;
466 break;
467 case HCI_SCODATA_PKT:
468 hdev->stat.sco_tx++;
469 break;
Prasanna Karthika03e33d2015-07-05 09:49:27 +0000470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 /* Prepend skb with frame type */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100473 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 count = skb->len;
476
477 /* Max HCI frame size seems to be 1511 + 1 */
Jia-Ju Bai67095142018-07-23 11:09:00 +0800478 nskb = bt_skb_alloc(count + 32, GFP_KERNEL);
Marcel Holtmann9c724352006-07-06 15:45:23 +0200479 if (!nskb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 BT_ERR("Can't allocate memory for new packet");
481 return -ENOMEM;
482 }
483
Marcel Holtmann9c724352006-07-06 15:45:23 +0200484 nskb->dev = (void *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 while (count) {
487 size = min_t(uint, count, BFUSB_MAX_BLOCK_SIZE);
488
489 buf[0] = 0xc1 | ((sent == 0) ? 0x04 : 0) | ((count == size) ? 0x08 : 0);
490 buf[1] = 0x00;
491 buf[2] = (size == BFUSB_MAX_BLOCK_SIZE) ? 0 : size;
492
Johannes Berg59ae1d12017-06-16 14:29:20 +0200493 skb_put_data(nskb, buf, 3);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300494 skb_copy_from_linear_data_offset(skb, sent, skb_put(nskb, size), size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 sent += size;
497 count -= size;
498 }
499
500 /* Don't send frame with multiple size of bulk max packet */
Marcel Holtmann9c724352006-07-06 15:45:23 +0200501 if ((nskb->len % data->bulk_pkt_size) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 buf[0] = 0xdd;
503 buf[1] = 0x00;
Johannes Berg59ae1d12017-06-16 14:29:20 +0200504 skb_put_data(nskb, buf, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 }
506
Marcel Holtmann9c724352006-07-06 15:45:23 +0200507 read_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Marcel Holtmann9c724352006-07-06 15:45:23 +0200509 skb_queue_tail(&data->transmit_q, nskb);
510 bfusb_tx_wakeup(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Marcel Holtmann9c724352006-07-06 15:45:23 +0200512 read_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 kfree_skb(skb);
515
516 return 0;
517}
518
David Woodhouse8187b4f2008-05-23 23:56:51 +0100519static int bfusb_load_firmware(struct bfusb_data *data,
520 const unsigned char *firmware, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
522 unsigned char *buf;
523 int err, pipe, len, size, sent = 0;
524
Marcel Holtmann9c724352006-07-06 15:45:23 +0200525 BT_DBG("bfusb %p udev %p", data, data->udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 BT_INFO("BlueFRITZ! USB loading firmware");
528
David Herrmann7f103a02011-10-26 11:22:46 +0200529 buf = kmalloc(BFUSB_MAX_BLOCK_SIZE + 3, GFP_KERNEL);
530 if (!buf) {
531 BT_ERR("Can't allocate memory chunk for firmware");
532 return -ENOMEM;
533 }
534
Marcel Holtmann9c724352006-07-06 15:45:23 +0200535 pipe = usb_sndctrlpipe(data->udev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Marcel Holtmann9c724352006-07-06 15:45:23 +0200537 if (usb_control_msg(data->udev, pipe, USB_REQ_SET_CONFIGURATION,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 0, 1, 0, NULL, 0, USB_CTRL_SET_TIMEOUT) < 0) {
539 BT_ERR("Can't change to loading configuration");
David Herrmann7f103a02011-10-26 11:22:46 +0200540 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return -EBUSY;
542 }
543
Marcel Holtmann9c724352006-07-06 15:45:23 +0200544 data->udev->toggle[0] = data->udev->toggle[1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Marcel Holtmann9c724352006-07-06 15:45:23 +0200546 pipe = usb_sndbulkpipe(data->udev, data->bulk_out_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 while (count) {
549 size = min_t(uint, count, BFUSB_MAX_BLOCK_SIZE + 3);
550
551 memcpy(buf, firmware + sent, size);
552
Marcel Holtmann9c724352006-07-06 15:45:23 +0200553 err = usb_bulk_msg(data->udev, pipe, buf, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 &len, BFUSB_BLOCK_TIMEOUT);
555
556 if (err || (len != size)) {
557 BT_ERR("Error in firmware loading");
558 goto error;
559 }
560
561 sent += size;
562 count -= size;
563 }
564
Marcel Holtmann9c724352006-07-06 15:45:23 +0200565 err = usb_bulk_msg(data->udev, pipe, NULL, 0,
566 &len, BFUSB_BLOCK_TIMEOUT);
567 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 BT_ERR("Error in null packet request");
569 goto error;
570 }
571
Marcel Holtmann9c724352006-07-06 15:45:23 +0200572 pipe = usb_sndctrlpipe(data->udev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Marcel Holtmann9c724352006-07-06 15:45:23 +0200574 err = usb_control_msg(data->udev, pipe, USB_REQ_SET_CONFIGURATION,
575 0, 2, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
576 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 BT_ERR("Can't change to running configuration");
578 goto error;
579 }
580
Marcel Holtmann9c724352006-07-06 15:45:23 +0200581 data->udev->toggle[0] = data->udev->toggle[1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 BT_INFO("BlueFRITZ! USB device ready");
584
585 kfree(buf);
586 return 0;
587
588error:
589 kfree(buf);
590
Marcel Holtmann9c724352006-07-06 15:45:23 +0200591 pipe = usb_sndctrlpipe(data->udev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Marcel Holtmann9c724352006-07-06 15:45:23 +0200593 usb_control_msg(data->udev, pipe, USB_REQ_SET_CONFIGURATION,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 0, 0, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
595
596 return err;
597}
598
599static int bfusb_probe(struct usb_interface *intf, const struct usb_device_id *id)
600{
601 const struct firmware *firmware;
602 struct usb_device *udev = interface_to_usbdev(intf);
603 struct usb_host_endpoint *bulk_out_ep;
604 struct usb_host_endpoint *bulk_in_ep;
605 struct hci_dev *hdev;
Marcel Holtmann9c724352006-07-06 15:45:23 +0200606 struct bfusb_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 BT_DBG("intf %p id %p", intf, id);
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 /* Check number of endpoints */
611 if (intf->cur_altsetting->desc.bNumEndpoints < 2)
612 return -EIO;
613
614 bulk_out_ep = &intf->cur_altsetting->endpoint[0];
615 bulk_in_ep = &intf->cur_altsetting->endpoint[1];
616
617 if (!bulk_out_ep || !bulk_in_ep) {
618 BT_ERR("Bulk endpoints not found");
619 goto done;
620 }
621
622 /* Initialize control structure and load firmware */
Sachin Kamat0213cd82012-07-27 12:38:32 +0530623 data = devm_kzalloc(&intf->dev, sizeof(struct bfusb_data), GFP_KERNEL);
Syam Sidhardhanf71e8232015-12-22 19:30:19 +0530624 if (!data)
Syam Sidhardhan7ddb6922015-12-22 19:30:20 +0530625 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Marcel Holtmann9c724352006-07-06 15:45:23 +0200627 data->udev = udev;
628 data->bulk_in_ep = bulk_in_ep->desc.bEndpointAddress;
629 data->bulk_out_ep = bulk_out_ep->desc.bEndpointAddress;
630 data->bulk_pkt_size = le16_to_cpu(bulk_out_ep->desc.wMaxPacketSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Marcel Holtmann9c724352006-07-06 15:45:23 +0200632 rwlock_init(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Marcel Holtmann9c724352006-07-06 15:45:23 +0200634 data->reassembly = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Marcel Holtmann9c724352006-07-06 15:45:23 +0200636 skb_queue_head_init(&data->transmit_q);
637 skb_queue_head_init(&data->pending_q);
638 skb_queue_head_init(&data->completed_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 if (request_firmware(&firmware, "bfubase.frm", &udev->dev) < 0) {
641 BT_ERR("Firmware request failed");
Sachin Kamat0213cd82012-07-27 12:38:32 +0530642 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
644
Marcel Holtmanna418b892008-11-30 12:17:28 +0100645 BT_DBG("firmware data %p size %zu", firmware->data, firmware->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Marcel Holtmann9c724352006-07-06 15:45:23 +0200647 if (bfusb_load_firmware(data, firmware->data, firmware->size) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 BT_ERR("Firmware loading failed");
649 goto release;
650 }
651
652 release_firmware(firmware);
653
654 /* Initialize and register HCI device */
655 hdev = hci_alloc_dev();
656 if (!hdev) {
657 BT_ERR("Can't allocate HCI device");
Sachin Kamat0213cd82012-07-27 12:38:32 +0530658 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
660
Marcel Holtmann9c724352006-07-06 15:45:23 +0200661 data->hdev = hdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Marcel Holtmannc13854c2010-02-08 15:27:07 +0100663 hdev->bus = HCI_USB;
David Herrmann155961e2012-02-09 21:58:32 +0100664 hci_set_drvdata(hdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 SET_HCIDEV_DEV(hdev, &intf->dev);
666
Marcel Holtmann19cf55a2013-10-10 10:50:00 -0700667 hdev->open = bfusb_open;
668 hdev->close = bfusb_close;
669 hdev->flush = bfusb_flush;
670 hdev->send = bfusb_send_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Marcel Holtmann9ef80d402014-12-26 04:42:32 +0100672 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (hci_register_dev(hdev) < 0) {
675 BT_ERR("Can't register HCI device");
676 hci_free_dev(hdev);
Sachin Kamat0213cd82012-07-27 12:38:32 +0530677 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
679
Marcel Holtmann9c724352006-07-06 15:45:23 +0200680 usb_set_intfdata(intf, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 return 0;
683
684release:
685 release_firmware(firmware);
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687done:
688 return -EIO;
689}
690
691static void bfusb_disconnect(struct usb_interface *intf)
692{
Marcel Holtmann9c724352006-07-06 15:45:23 +0200693 struct bfusb_data *data = usb_get_intfdata(intf);
694 struct hci_dev *hdev = data->hdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 BT_DBG("intf %p", intf);
697
698 if (!hdev)
699 return;
700
701 usb_set_intfdata(intf, NULL);
702
703 bfusb_close(hdev);
704
David Herrmann13ea4012011-10-26 10:43:18 +0200705 hci_unregister_dev(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 hci_free_dev(hdev);
707}
708
709static struct usb_driver bfusb_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 .name = "bfusb",
711 .probe = bfusb_probe,
712 .disconnect = bfusb_disconnect,
713 .id_table = bfusb_table,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -0700714 .disable_hub_initiated_lpm = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715};
716
Greg Kroah-Hartman93f15082011-11-18 09:47:34 -0800717module_usb_driver(bfusb_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
720MODULE_DESCRIPTION("BlueFRITZ! USB driver ver " VERSION);
721MODULE_VERSION(VERSION);
722MODULE_LICENSE("GPL");
Marcel Holtmann23121192007-02-17 23:59:02 +0100723MODULE_FIRMWARE("bfubase.frm");