Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * |
| 4 | * AVM BlueFRITZ! USB driver |
| 5 | * |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 6 | * Copyright (C) 2003-2006 Marcel Holtmann <marcel@holtmann.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #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 Holtmann | 943d56b | 2008-08-07 22:26:55 +0200 | [diff] [blame] | 26 | #define VERSION "1.2" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
| 28 | static struct usb_driver bfusb_driver; |
| 29 | |
Marcel Holtmann | 9712d59 | 2013-10-11 07:46:19 -0700 | [diff] [blame] | 30 | static const struct usb_device_id bfusb_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | /* AVM BlueFRITZ! USB */ |
| 32 | { USB_DEVICE(0x057c, 0x2200) }, |
| 33 | |
| 34 | { } /* Terminating entry */ |
| 35 | }; |
| 36 | |
| 37 | MODULE_DEVICE_TABLE(usb, bfusb_table); |
| 38 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #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 Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 49 | struct bfusb_data { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | 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 Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 71 | struct bfusb_data_scb { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | struct urb *urb; |
| 73 | }; |
| 74 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 75 | static void bfusb_tx_complete(struct urb *urb); |
| 76 | static void bfusb_rx_complete(struct urb *urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 78 | static struct urb *bfusb_get_completed(struct bfusb_data *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | { |
| 80 | struct sk_buff *skb; |
| 81 | struct urb *urb = NULL; |
| 82 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 83 | BT_DBG("bfusb %p", data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 85 | skb = skb_dequeue(&data->completed_q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | if (skb) { |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 87 | urb = ((struct bfusb_data_scb *) skb->cb)->urb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | kfree_skb(skb); |
| 89 | } |
| 90 | |
| 91 | return urb; |
| 92 | } |
| 93 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 94 | static void bfusb_unlink_urbs(struct bfusb_data *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | { |
| 96 | struct sk_buff *skb; |
| 97 | struct urb *urb; |
| 98 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 99 | BT_DBG("bfusb %p", data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 101 | while ((skb = skb_dequeue(&data->pending_q))) { |
| 102 | urb = ((struct bfusb_data_scb *) skb->cb)->urb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | usb_kill_urb(urb); |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 104 | skb_queue_tail(&data->completed_q, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 107 | while ((urb = bfusb_get_completed(data))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | usb_free_urb(urb); |
| 109 | } |
| 110 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 111 | static int bfusb_send_bulk(struct bfusb_data *data, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | { |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 113 | struct bfusb_data_scb *scb = (void *) skb->cb; |
| 114 | struct urb *urb = bfusb_get_completed(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | int err, pipe; |
| 116 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 117 | BT_DBG("bfusb %p skb %p len %d", data, skb, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
Valentin Ilie | a08b15e | 2013-08-12 18:46:00 +0300 | [diff] [blame] | 119 | if (!urb) { |
| 120 | urb = usb_alloc_urb(0, GFP_ATOMIC); |
| 121 | if (!urb) |
| 122 | return -ENOMEM; |
| 123 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 125 | pipe = usb_sndbulkpipe(data->udev, data->bulk_out_ep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 127 | usb_fill_bulk_urb(urb, data->udev, pipe, skb->data, skb->len, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | bfusb_tx_complete, skb); |
| 129 | |
| 130 | scb->urb = urb; |
| 131 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 132 | skb_queue_tail(&data->pending_q, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | |
| 134 | err = usb_submit_urb(urb, GFP_ATOMIC); |
| 135 | if (err) { |
Marcel Holtmann | 2a4f390 | 2020-03-09 22:44:56 +0100 | [diff] [blame] | 136 | bt_dev_err(data->hdev, "bulk tx submit failed urb %p err %d", |
| 137 | urb, err); |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 138 | skb_unlink(skb, &data->pending_q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | usb_free_urb(urb); |
| 140 | } else |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 141 | atomic_inc(&data->pending_tx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
| 143 | return err; |
| 144 | } |
| 145 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 146 | static void bfusb_tx_wakeup(struct bfusb_data *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | { |
| 148 | struct sk_buff *skb; |
| 149 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 150 | BT_DBG("bfusb %p", data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 152 | if (test_and_set_bit(BFUSB_TX_PROCESS, &data->state)) { |
| 153 | set_bit(BFUSB_TX_WAKEUP, &data->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | return; |
| 155 | } |
| 156 | |
| 157 | do { |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 158 | clear_bit(BFUSB_TX_WAKEUP, &data->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 160 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | break; |
| 165 | } |
| 166 | } |
| 167 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 168 | } while (test_bit(BFUSB_TX_WAKEUP, &data->state)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 170 | clear_bit(BFUSB_TX_PROCESS, &data->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | } |
| 172 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 173 | static void bfusb_tx_complete(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | { |
| 175 | struct sk_buff *skb = (struct sk_buff *) urb->context; |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 176 | struct bfusb_data *data = (struct bfusb_data *) skb->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 178 | BT_DBG("bfusb %p urb %p skb %p len %d", data, urb, skb, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 180 | atomic_dec(&data->pending_tx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 182 | if (!test_bit(HCI_RUNNING, &data->hdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | return; |
| 184 | |
| 185 | if (!urb->status) |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 186 | data->hdev->stat.byte_tx += skb->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | else |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 188 | data->hdev->stat.err_tx++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 190 | read_lock(&data->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 192 | skb_unlink(skb, &data->pending_q); |
| 193 | skb_queue_tail(&data->completed_q, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 195 | bfusb_tx_wakeup(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 197 | read_unlock(&data->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 201 | static int bfusb_rx_submit(struct bfusb_data *data, struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | { |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 203 | struct bfusb_data_scb *scb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | struct sk_buff *skb; |
| 205 | int err, pipe, size = HCI_MAX_FRAME_SIZE + 32; |
| 206 | |
Marcel Holtmann | a418b89 | 2008-11-30 12:17:28 +0100 | [diff] [blame] | 207 | BT_DBG("bfusb %p urb %p", data, urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
Valentin Ilie | a08b15e | 2013-08-12 18:46:00 +0300 | [diff] [blame] | 209 | if (!urb) { |
| 210 | urb = usb_alloc_urb(0, GFP_ATOMIC); |
| 211 | if (!urb) |
| 212 | return -ENOMEM; |
| 213 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 215 | skb = bt_skb_alloc(size, GFP_ATOMIC); |
| 216 | if (!skb) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | usb_free_urb(urb); |
| 218 | return -ENOMEM; |
| 219 | } |
| 220 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 221 | skb->dev = (void *) data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 223 | scb = (struct bfusb_data_scb *) skb->cb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | scb->urb = urb; |
| 225 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 226 | pipe = usb_rcvbulkpipe(data->udev, data->bulk_in_ep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 228 | usb_fill_bulk_urb(urb, data->udev, pipe, skb->data, size, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | bfusb_rx_complete, skb); |
| 230 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 231 | skb_queue_tail(&data->pending_q, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | |
| 233 | err = usb_submit_urb(urb, GFP_ATOMIC); |
| 234 | if (err) { |
Marcel Holtmann | 2a4f390 | 2020-03-09 22:44:56 +0100 | [diff] [blame] | 235 | bt_dev_err(data->hdev, "bulk rx submit failed urb %p err %d", |
| 236 | urb, err); |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 237 | skb_unlink(skb, &data->pending_q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | kfree_skb(skb); |
| 239 | usb_free_urb(urb); |
| 240 | } |
| 241 | |
| 242 | return err; |
| 243 | } |
| 244 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 245 | static inline int bfusb_recv_block(struct bfusb_data *data, int hdr, unsigned char *buf, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | { |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 247 | BT_DBG("bfusb %p hdr 0x%02x data %p len %d", data, hdr, buf, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | |
| 249 | if (hdr & 0x10) { |
Marcel Holtmann | 2a4f390 | 2020-03-09 22:44:56 +0100 | [diff] [blame] | 250 | bt_dev_err(data->hdev, "error in block"); |
Wei Yongjun | b1fb068 | 2009-02-25 18:09:33 +0800 | [diff] [blame] | 251 | kfree_skb(data->reassembly); |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 252 | data->reassembly = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | 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 Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 261 | if (data->reassembly) { |
Marcel Holtmann | 2a4f390 | 2020-03-09 22:44:56 +0100 | [diff] [blame] | 262 | bt_dev_err(data->hdev, "unexpected start block"); |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 263 | kfree_skb(data->reassembly); |
| 264 | data->reassembly = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | if (len < 1) { |
Marcel Holtmann | 2a4f390 | 2020-03-09 22:44:56 +0100 | [diff] [blame] | 268 | bt_dev_err(data->hdev, "no packet type found"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | return -EPROTO; |
| 270 | } |
| 271 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 272 | pkt_type = *buf++; len--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | |
| 274 | switch (pkt_type) { |
| 275 | case HCI_EVENT_PKT: |
| 276 | if (len >= HCI_EVENT_HDR_SIZE) { |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 277 | struct hci_event_hdr *hdr = (struct hci_event_hdr *) buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | pkt_len = HCI_EVENT_HDR_SIZE + hdr->plen; |
| 279 | } else { |
Marcel Holtmann | 2a4f390 | 2020-03-09 22:44:56 +0100 | [diff] [blame] | 280 | bt_dev_err(data->hdev, "event block is too short"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | return -EILSEQ; |
| 282 | } |
| 283 | break; |
| 284 | |
| 285 | case HCI_ACLDATA_PKT: |
| 286 | if (len >= HCI_ACL_HDR_SIZE) { |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 287 | struct hci_acl_hdr *hdr = (struct hci_acl_hdr *) buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | pkt_len = HCI_ACL_HDR_SIZE + __le16_to_cpu(hdr->dlen); |
| 289 | } else { |
Marcel Holtmann | 2a4f390 | 2020-03-09 22:44:56 +0100 | [diff] [blame] | 290 | bt_dev_err(data->hdev, "data block is too short"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | return -EILSEQ; |
| 292 | } |
| 293 | break; |
| 294 | |
| 295 | case HCI_SCODATA_PKT: |
| 296 | if (len >= HCI_SCO_HDR_SIZE) { |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 297 | struct hci_sco_hdr *hdr = (struct hci_sco_hdr *) buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | pkt_len = HCI_SCO_HDR_SIZE + hdr->dlen; |
| 299 | } else { |
Marcel Holtmann | 2a4f390 | 2020-03-09 22:44:56 +0100 | [diff] [blame] | 300 | bt_dev_err(data->hdev, "audio block is too short"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | return -EILSEQ; |
| 302 | } |
| 303 | break; |
| 304 | } |
| 305 | |
| 306 | skb = bt_skb_alloc(pkt_len, GFP_ATOMIC); |
| 307 | if (!skb) { |
Marcel Holtmann | 2a4f390 | 2020-03-09 22:44:56 +0100 | [diff] [blame] | 308 | bt_dev_err(data->hdev, "no memory for the packet"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | return -ENOMEM; |
| 310 | } |
| 311 | |
Marcel Holtmann | 618e8bc | 2015-11-05 07:33:56 +0100 | [diff] [blame] | 312 | hci_skb_pkt_type(skb) = pkt_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 314 | data->reassembly = skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | } else { |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 316 | if (!data->reassembly) { |
Marcel Holtmann | 2a4f390 | 2020-03-09 22:44:56 +0100 | [diff] [blame] | 317 | bt_dev_err(data->hdev, "unexpected continuation block"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | return -EIO; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | if (len > 0) |
Johannes Berg | 59ae1d1 | 2017-06-16 14:29:20 +0200 | [diff] [blame] | 323 | skb_put_data(data->reassembly, buf, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | |
| 325 | if (hdr & 0x08) { |
Marcel Holtmann | e1a2617 | 2013-10-10 16:52:43 -0700 | [diff] [blame] | 326 | hci_recv_frame(data->hdev, data->reassembly); |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 327 | data->reassembly = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | return 0; |
| 331 | } |
| 332 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 333 | static void bfusb_rx_complete(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | { |
| 335 | struct sk_buff *skb = (struct sk_buff *) urb->context; |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 336 | struct bfusb_data *data = (struct bfusb_data *) skb->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | unsigned char *buf = urb->transfer_buffer; |
| 338 | int count = urb->actual_length; |
| 339 | int err, hdr, len; |
| 340 | |
Marcel Holtmann | a418b89 | 2008-11-30 12:17:28 +0100 | [diff] [blame] | 341 | BT_DBG("bfusb %p urb %p skb %p len %d", data, urb, skb, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 343 | read_lock(&data->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 345 | if (!test_bit(HCI_RUNNING, &data->hdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | goto unlock; |
| 347 | |
| 348 | if (urb->status || !count) |
| 349 | goto resubmit; |
| 350 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 351 | data->hdev->stat.byte_rx += count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | |
| 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 Holtmann | 2a4f390 | 2020-03-09 22:44:56 +0100 | [diff] [blame] | 369 | bt_dev_err(data->hdev, "block extends over URB buffer ranges"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | if ((hdr & 0xe1) == 0xc1) |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 373 | bfusb_recv_block(data, hdr, buf, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | |
| 375 | count -= len; |
| 376 | buf += len; |
| 377 | } |
| 378 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 379 | skb_unlink(skb, &data->pending_q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | kfree_skb(skb); |
| 381 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 382 | bfusb_rx_submit(data, urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 384 | read_unlock(&data->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | |
| 386 | return; |
| 387 | |
| 388 | resubmit: |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 389 | urb->dev = data->udev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | |
| 391 | err = usb_submit_urb(urb, GFP_ATOMIC); |
| 392 | if (err) { |
Marcel Holtmann | 2a4f390 | 2020-03-09 22:44:56 +0100 | [diff] [blame] | 393 | bt_dev_err(data->hdev, "bulk resubmit failed urb %p err %d", |
| 394 | urb, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | unlock: |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 398 | read_unlock(&data->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | static int bfusb_open(struct hci_dev *hdev) |
| 402 | { |
David Herrmann | 155961e | 2012-02-09 21:58:32 +0100 | [diff] [blame] | 403 | struct bfusb_data *data = hci_get_drvdata(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | unsigned long flags; |
| 405 | int i, err; |
| 406 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 407 | BT_DBG("hdev %p bfusb %p", hdev, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 409 | write_lock_irqsave(&data->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 411 | err = bfusb_rx_submit(data, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | if (!err) { |
| 413 | for (i = 1; i < BFUSB_MAX_BULK_RX; i++) |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 414 | bfusb_rx_submit(data, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 417 | write_unlock_irqrestore(&data->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | |
| 419 | return err; |
| 420 | } |
| 421 | |
| 422 | static int bfusb_flush(struct hci_dev *hdev) |
| 423 | { |
David Herrmann | 155961e | 2012-02-09 21:58:32 +0100 | [diff] [blame] | 424 | struct bfusb_data *data = hci_get_drvdata(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 426 | BT_DBG("hdev %p bfusb %p", hdev, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 428 | skb_queue_purge(&data->transmit_q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | |
| 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | static int bfusb_close(struct hci_dev *hdev) |
| 434 | { |
David Herrmann | 155961e | 2012-02-09 21:58:32 +0100 | [diff] [blame] | 435 | struct bfusb_data *data = hci_get_drvdata(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | unsigned long flags; |
| 437 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 438 | BT_DBG("hdev %p bfusb %p", hdev, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 440 | write_lock_irqsave(&data->lock, flags); |
| 441 | write_unlock_irqrestore(&data->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 443 | bfusb_unlink_urbs(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | bfusb_flush(hdev); |
| 445 | |
| 446 | return 0; |
| 447 | } |
| 448 | |
Marcel Holtmann | 7bd8f09 | 2013-10-11 06:19:18 -0700 | [diff] [blame] | 449 | static int bfusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | { |
Marcel Holtmann | aae2627 | 2013-10-11 07:00:57 -0700 | [diff] [blame] | 451 | struct bfusb_data *data = hci_get_drvdata(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | struct sk_buff *nskb; |
| 453 | unsigned char buf[3]; |
| 454 | int sent = 0, size, count; |
| 455 | |
Marcel Holtmann | 618e8bc | 2015-11-05 07:33:56 +0100 | [diff] [blame] | 456 | BT_DBG("hdev %p skb %p type %d len %d", hdev, skb, |
| 457 | hci_skb_pkt_type(skb), skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | |
Marcel Holtmann | 618e8bc | 2015-11-05 07:33:56 +0100 | [diff] [blame] | 459 | switch (hci_skb_pkt_type(skb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | 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 Karthik | a03e33d | 2015-07-05 09:49:27 +0000 | [diff] [blame] | 469 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | |
| 471 | /* Prepend skb with frame type */ |
Marcel Holtmann | 618e8bc | 2015-11-05 07:33:56 +0100 | [diff] [blame] | 472 | memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | |
| 474 | count = skb->len; |
| 475 | |
| 476 | /* Max HCI frame size seems to be 1511 + 1 */ |
Jia-Ju Bai | 6709514 | 2018-07-23 11:09:00 +0800 | [diff] [blame] | 477 | nskb = bt_skb_alloc(count + 32, GFP_KERNEL); |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 478 | if (!nskb) { |
Marcel Holtmann | 2a4f390 | 2020-03-09 22:44:56 +0100 | [diff] [blame] | 479 | bt_dev_err(hdev, "Can't allocate memory for new packet"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | return -ENOMEM; |
| 481 | } |
| 482 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 483 | nskb->dev = (void *) data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | |
| 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 Berg | 59ae1d1 | 2017-06-16 14:29:20 +0200 | [diff] [blame] | 492 | skb_put_data(nskb, buf, 3); |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 493 | skb_copy_from_linear_data_offset(skb, sent, skb_put(nskb, size), size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | |
| 495 | sent += size; |
| 496 | count -= size; |
| 497 | } |
| 498 | |
| 499 | /* Don't send frame with multiple size of bulk max packet */ |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 500 | if ((nskb->len % data->bulk_pkt_size) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | buf[0] = 0xdd; |
| 502 | buf[1] = 0x00; |
Johannes Berg | 59ae1d1 | 2017-06-16 14:29:20 +0200 | [diff] [blame] | 503 | skb_put_data(nskb, buf, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | } |
| 505 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 506 | read_lock(&data->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 508 | skb_queue_tail(&data->transmit_q, nskb); |
| 509 | bfusb_tx_wakeup(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 511 | read_unlock(&data->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | |
| 513 | kfree_skb(skb); |
| 514 | |
| 515 | return 0; |
| 516 | } |
| 517 | |
David Woodhouse | 8187b4f | 2008-05-23 23:56:51 +0100 | [diff] [blame] | 518 | static int bfusb_load_firmware(struct bfusb_data *data, |
| 519 | const unsigned char *firmware, int count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | { |
| 521 | unsigned char *buf; |
| 522 | int err, pipe, len, size, sent = 0; |
| 523 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 524 | BT_DBG("bfusb %p udev %p", data, data->udev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | |
| 526 | BT_INFO("BlueFRITZ! USB loading firmware"); |
| 527 | |
David Herrmann | 7f103a0 | 2011-10-26 11:22:46 +0200 | [diff] [blame] | 528 | 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 Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 534 | pipe = usb_sndctrlpipe(data->udev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 536 | if (usb_control_msg(data->udev, pipe, USB_REQ_SET_CONFIGURATION, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | 0, 1, 0, NULL, 0, USB_CTRL_SET_TIMEOUT) < 0) { |
| 538 | BT_ERR("Can't change to loading configuration"); |
David Herrmann | 7f103a0 | 2011-10-26 11:22:46 +0200 | [diff] [blame] | 539 | kfree(buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | return -EBUSY; |
| 541 | } |
| 542 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 543 | data->udev->toggle[0] = data->udev->toggle[1] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 545 | pipe = usb_sndbulkpipe(data->udev, data->bulk_out_ep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | |
| 547 | while (count) { |
| 548 | size = min_t(uint, count, BFUSB_MAX_BLOCK_SIZE + 3); |
| 549 | |
| 550 | memcpy(buf, firmware + sent, size); |
| 551 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 552 | err = usb_bulk_msg(data->udev, pipe, buf, size, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | &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 Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 564 | err = usb_bulk_msg(data->udev, pipe, NULL, 0, |
| 565 | &len, BFUSB_BLOCK_TIMEOUT); |
| 566 | if (err < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | BT_ERR("Error in null packet request"); |
| 568 | goto error; |
| 569 | } |
| 570 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 571 | pipe = usb_sndctrlpipe(data->udev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 573 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | BT_ERR("Can't change to running configuration"); |
| 577 | goto error; |
| 578 | } |
| 579 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 580 | data->udev->toggle[0] = data->udev->toggle[1] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | |
| 582 | BT_INFO("BlueFRITZ! USB device ready"); |
| 583 | |
| 584 | kfree(buf); |
| 585 | return 0; |
| 586 | |
| 587 | error: |
| 588 | kfree(buf); |
| 589 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 590 | pipe = usb_sndctrlpipe(data->udev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 592 | usb_control_msg(data->udev, pipe, USB_REQ_SET_CONFIGURATION, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | 0, 0, 0, NULL, 0, USB_CTRL_SET_TIMEOUT); |
| 594 | |
| 595 | return err; |
| 596 | } |
| 597 | |
| 598 | static 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 Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 605 | struct bfusb_data *data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | |
| 607 | BT_DBG("intf %p id %p", intf, id); |
| 608 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | /* 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 Kamat | 0213cd8 | 2012-07-27 12:38:32 +0530 | [diff] [blame] | 622 | data = devm_kzalloc(&intf->dev, sizeof(struct bfusb_data), GFP_KERNEL); |
Syam Sidhardhan | f71e823 | 2015-12-22 19:30:19 +0530 | [diff] [blame] | 623 | if (!data) |
Syam Sidhardhan | 7ddb692 | 2015-12-22 19:30:20 +0530 | [diff] [blame] | 624 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 626 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 631 | rwlock_init(&data->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 633 | data->reassembly = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 635 | skb_queue_head_init(&data->transmit_q); |
| 636 | skb_queue_head_init(&data->pending_q); |
| 637 | skb_queue_head_init(&data->completed_q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | |
| 639 | if (request_firmware(&firmware, "bfubase.frm", &udev->dev) < 0) { |
| 640 | BT_ERR("Firmware request failed"); |
Sachin Kamat | 0213cd8 | 2012-07-27 12:38:32 +0530 | [diff] [blame] | 641 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | } |
| 643 | |
Marcel Holtmann | a418b89 | 2008-11-30 12:17:28 +0100 | [diff] [blame] | 644 | BT_DBG("firmware data %p size %zu", firmware->data, firmware->size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 646 | if (bfusb_load_firmware(data, firmware->data, firmware->size) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | 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 Kamat | 0213cd8 | 2012-07-27 12:38:32 +0530 | [diff] [blame] | 657 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | } |
| 659 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 660 | data->hdev = hdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | |
Marcel Holtmann | c13854c | 2010-02-08 15:27:07 +0100 | [diff] [blame] | 662 | hdev->bus = HCI_USB; |
David Herrmann | 155961e | 2012-02-09 21:58:32 +0100 | [diff] [blame] | 663 | hci_set_drvdata(hdev, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | SET_HCIDEV_DEV(hdev, &intf->dev); |
| 665 | |
Marcel Holtmann | 19cf55a | 2013-10-10 10:50:00 -0700 | [diff] [blame] | 666 | hdev->open = bfusb_open; |
| 667 | hdev->close = bfusb_close; |
| 668 | hdev->flush = bfusb_flush; |
| 669 | hdev->send = bfusb_send_frame; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | |
Marcel Holtmann | 9ef80d40 | 2014-12-26 04:42:32 +0100 | [diff] [blame] | 671 | set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks); |
| 672 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | if (hci_register_dev(hdev) < 0) { |
| 674 | BT_ERR("Can't register HCI device"); |
| 675 | hci_free_dev(hdev); |
Sachin Kamat | 0213cd8 | 2012-07-27 12:38:32 +0530 | [diff] [blame] | 676 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | } |
| 678 | |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 679 | usb_set_intfdata(intf, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | |
| 681 | return 0; |
| 682 | |
| 683 | release: |
| 684 | release_firmware(firmware); |
| 685 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | done: |
| 687 | return -EIO; |
| 688 | } |
| 689 | |
| 690 | static void bfusb_disconnect(struct usb_interface *intf) |
| 691 | { |
Marcel Holtmann | 9c72435 | 2006-07-06 15:45:23 +0200 | [diff] [blame] | 692 | struct bfusb_data *data = usb_get_intfdata(intf); |
| 693 | struct hci_dev *hdev = data->hdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | |
| 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 Herrmann | 13ea401 | 2011-10-26 10:43:18 +0200 | [diff] [blame] | 704 | hci_unregister_dev(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | hci_free_dev(hdev); |
| 706 | } |
| 707 | |
| 708 | static struct usb_driver bfusb_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | .name = "bfusb", |
| 710 | .probe = bfusb_probe, |
| 711 | .disconnect = bfusb_disconnect, |
| 712 | .id_table = bfusb_table, |
Sarah Sharp | e1f12eb | 2012-04-23 10:08:51 -0700 | [diff] [blame] | 713 | .disable_hub_initiated_lpm = 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | }; |
| 715 | |
Greg Kroah-Hartman | 93f1508 | 2011-11-18 09:47:34 -0800 | [diff] [blame] | 716 | module_usb_driver(bfusb_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>"); |
| 719 | MODULE_DESCRIPTION("BlueFRITZ! USB driver ver " VERSION); |
| 720 | MODULE_VERSION(VERSION); |
| 721 | MODULE_LICENSE("GPL"); |
Marcel Holtmann | 2312119 | 2007-02-17 23:59:02 +0100 | [diff] [blame] | 722 | MODULE_FIRMWARE("bfubase.frm"); |