Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * AVM BlueFRITZ! USB driver |
| 4 | * |
| 5 | * Copyright (C) 2003 Marcel Holtmann <marcel@holtmann.org> |
| 6 | * |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | * |
| 22 | */ |
| 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/module.h> |
| 25 | |
| 26 | #include <linux/kernel.h> |
| 27 | #include <linux/init.h> |
| 28 | #include <linux/slab.h> |
| 29 | #include <linux/types.h> |
| 30 | #include <linux/sched.h> |
| 31 | #include <linux/errno.h> |
| 32 | #include <linux/skbuff.h> |
| 33 | |
| 34 | #include <linux/device.h> |
| 35 | #include <linux/firmware.h> |
| 36 | |
| 37 | #include <linux/usb.h> |
| 38 | |
| 39 | #include <net/bluetooth/bluetooth.h> |
| 40 | #include <net/bluetooth/hci_core.h> |
| 41 | |
| 42 | #ifndef CONFIG_BT_HCIBFUSB_DEBUG |
| 43 | #undef BT_DBG |
| 44 | #define BT_DBG(D...) |
| 45 | #endif |
| 46 | |
| 47 | #define VERSION "1.1" |
| 48 | |
| 49 | static int ignore = 0; |
| 50 | |
| 51 | static struct usb_driver bfusb_driver; |
| 52 | |
| 53 | static struct usb_device_id bfusb_table[] = { |
| 54 | /* AVM BlueFRITZ! USB */ |
| 55 | { USB_DEVICE(0x057c, 0x2200) }, |
| 56 | |
| 57 | { } /* Terminating entry */ |
| 58 | }; |
| 59 | |
| 60 | MODULE_DEVICE_TABLE(usb, bfusb_table); |
| 61 | |
| 62 | |
| 63 | #define BFUSB_MAX_BLOCK_SIZE 256 |
| 64 | |
| 65 | #define BFUSB_BLOCK_TIMEOUT 3000 |
| 66 | |
| 67 | #define BFUSB_TX_PROCESS 1 |
| 68 | #define BFUSB_TX_WAKEUP 2 |
| 69 | |
| 70 | #define BFUSB_MAX_BULK_TX 2 |
| 71 | #define BFUSB_MAX_BULK_RX 2 |
| 72 | |
| 73 | struct bfusb { |
| 74 | struct hci_dev *hdev; |
| 75 | |
| 76 | unsigned long state; |
| 77 | |
| 78 | struct usb_device *udev; |
| 79 | |
| 80 | unsigned int bulk_in_ep; |
| 81 | unsigned int bulk_out_ep; |
| 82 | unsigned int bulk_pkt_size; |
| 83 | |
| 84 | rwlock_t lock; |
| 85 | |
| 86 | struct sk_buff_head transmit_q; |
| 87 | |
| 88 | struct sk_buff *reassembly; |
| 89 | |
| 90 | atomic_t pending_tx; |
| 91 | struct sk_buff_head pending_q; |
| 92 | struct sk_buff_head completed_q; |
| 93 | }; |
| 94 | |
| 95 | struct bfusb_scb { |
| 96 | struct urb *urb; |
| 97 | }; |
| 98 | |
| 99 | static void bfusb_tx_complete(struct urb *urb, struct pt_regs *regs); |
| 100 | static void bfusb_rx_complete(struct urb *urb, struct pt_regs *regs); |
| 101 | |
| 102 | static struct urb *bfusb_get_completed(struct bfusb *bfusb) |
| 103 | { |
| 104 | struct sk_buff *skb; |
| 105 | struct urb *urb = NULL; |
| 106 | |
| 107 | BT_DBG("bfusb %p", bfusb); |
| 108 | |
| 109 | skb = skb_dequeue(&bfusb->completed_q); |
| 110 | if (skb) { |
| 111 | urb = ((struct bfusb_scb *) skb->cb)->urb; |
| 112 | kfree_skb(skb); |
| 113 | } |
| 114 | |
| 115 | return urb; |
| 116 | } |
| 117 | |
| 118 | static void bfusb_unlink_urbs(struct bfusb *bfusb) |
| 119 | { |
| 120 | struct sk_buff *skb; |
| 121 | struct urb *urb; |
| 122 | |
| 123 | BT_DBG("bfusb %p", bfusb); |
| 124 | |
| 125 | while ((skb = skb_dequeue(&bfusb->pending_q))) { |
| 126 | urb = ((struct bfusb_scb *) skb->cb)->urb; |
| 127 | usb_kill_urb(urb); |
| 128 | skb_queue_tail(&bfusb->completed_q, skb); |
| 129 | } |
| 130 | |
| 131 | while ((urb = bfusb_get_completed(bfusb))) |
| 132 | usb_free_urb(urb); |
| 133 | } |
| 134 | |
| 135 | |
| 136 | static int bfusb_send_bulk(struct bfusb *bfusb, struct sk_buff *skb) |
| 137 | { |
| 138 | struct bfusb_scb *scb = (void *) skb->cb; |
| 139 | struct urb *urb = bfusb_get_completed(bfusb); |
| 140 | int err, pipe; |
| 141 | |
| 142 | BT_DBG("bfusb %p skb %p len %d", bfusb, skb, skb->len); |
| 143 | |
| 144 | if (!urb && !(urb = usb_alloc_urb(0, GFP_ATOMIC))) |
| 145 | return -ENOMEM; |
| 146 | |
| 147 | pipe = usb_sndbulkpipe(bfusb->udev, bfusb->bulk_out_ep); |
| 148 | |
| 149 | usb_fill_bulk_urb(urb, bfusb->udev, pipe, skb->data, skb->len, |
| 150 | bfusb_tx_complete, skb); |
| 151 | |
| 152 | scb->urb = urb; |
| 153 | |
| 154 | skb_queue_tail(&bfusb->pending_q, skb); |
| 155 | |
| 156 | err = usb_submit_urb(urb, GFP_ATOMIC); |
| 157 | if (err) { |
| 158 | BT_ERR("%s bulk tx submit failed urb %p err %d", |
| 159 | bfusb->hdev->name, urb, err); |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 160 | skb_unlink(skb, &bfusb->pending_q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | usb_free_urb(urb); |
| 162 | } else |
| 163 | atomic_inc(&bfusb->pending_tx); |
| 164 | |
| 165 | return err; |
| 166 | } |
| 167 | |
| 168 | static void bfusb_tx_wakeup(struct bfusb *bfusb) |
| 169 | { |
| 170 | struct sk_buff *skb; |
| 171 | |
| 172 | BT_DBG("bfusb %p", bfusb); |
| 173 | |
| 174 | if (test_and_set_bit(BFUSB_TX_PROCESS, &bfusb->state)) { |
| 175 | set_bit(BFUSB_TX_WAKEUP, &bfusb->state); |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | do { |
| 180 | clear_bit(BFUSB_TX_WAKEUP, &bfusb->state); |
| 181 | |
| 182 | while ((atomic_read(&bfusb->pending_tx) < BFUSB_MAX_BULK_TX) && |
| 183 | (skb = skb_dequeue(&bfusb->transmit_q))) { |
| 184 | if (bfusb_send_bulk(bfusb, skb) < 0) { |
| 185 | skb_queue_head(&bfusb->transmit_q, skb); |
| 186 | break; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | } while (test_bit(BFUSB_TX_WAKEUP, &bfusb->state)); |
| 191 | |
| 192 | clear_bit(BFUSB_TX_PROCESS, &bfusb->state); |
| 193 | } |
| 194 | |
| 195 | static void bfusb_tx_complete(struct urb *urb, struct pt_regs *regs) |
| 196 | { |
| 197 | struct sk_buff *skb = (struct sk_buff *) urb->context; |
| 198 | struct bfusb *bfusb = (struct bfusb *) skb->dev; |
| 199 | |
| 200 | BT_DBG("bfusb %p urb %p skb %p len %d", bfusb, urb, skb, skb->len); |
| 201 | |
| 202 | atomic_dec(&bfusb->pending_tx); |
| 203 | |
| 204 | if (!test_bit(HCI_RUNNING, &bfusb->hdev->flags)) |
| 205 | return; |
| 206 | |
| 207 | if (!urb->status) |
| 208 | bfusb->hdev->stat.byte_tx += skb->len; |
| 209 | else |
| 210 | bfusb->hdev->stat.err_tx++; |
| 211 | |
| 212 | read_lock(&bfusb->lock); |
| 213 | |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 214 | skb_unlink(skb, &bfusb->pending_q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | skb_queue_tail(&bfusb->completed_q, skb); |
| 216 | |
| 217 | bfusb_tx_wakeup(bfusb); |
| 218 | |
| 219 | read_unlock(&bfusb->lock); |
| 220 | } |
| 221 | |
| 222 | |
| 223 | static int bfusb_rx_submit(struct bfusb *bfusb, struct urb *urb) |
| 224 | { |
| 225 | struct bfusb_scb *scb; |
| 226 | struct sk_buff *skb; |
| 227 | int err, pipe, size = HCI_MAX_FRAME_SIZE + 32; |
| 228 | |
| 229 | BT_DBG("bfusb %p urb %p", bfusb, urb); |
| 230 | |
| 231 | if (!urb && !(urb = usb_alloc_urb(0, GFP_ATOMIC))) |
| 232 | return -ENOMEM; |
| 233 | |
| 234 | if (!(skb = bt_skb_alloc(size, GFP_ATOMIC))) { |
| 235 | usb_free_urb(urb); |
| 236 | return -ENOMEM; |
| 237 | } |
| 238 | |
| 239 | skb->dev = (void *) bfusb; |
| 240 | |
| 241 | scb = (struct bfusb_scb *) skb->cb; |
| 242 | scb->urb = urb; |
| 243 | |
| 244 | pipe = usb_rcvbulkpipe(bfusb->udev, bfusb->bulk_in_ep); |
| 245 | |
| 246 | usb_fill_bulk_urb(urb, bfusb->udev, pipe, skb->data, size, |
| 247 | bfusb_rx_complete, skb); |
| 248 | |
| 249 | skb_queue_tail(&bfusb->pending_q, skb); |
| 250 | |
| 251 | err = usb_submit_urb(urb, GFP_ATOMIC); |
| 252 | if (err) { |
| 253 | BT_ERR("%s bulk rx submit failed urb %p err %d", |
| 254 | bfusb->hdev->name, urb, err); |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 255 | skb_unlink(skb, &bfusb->pending_q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | kfree_skb(skb); |
| 257 | usb_free_urb(urb); |
| 258 | } |
| 259 | |
| 260 | return err; |
| 261 | } |
| 262 | |
| 263 | static inline int bfusb_recv_block(struct bfusb *bfusb, int hdr, unsigned char *data, int len) |
| 264 | { |
| 265 | BT_DBG("bfusb %p hdr 0x%02x data %p len %d", bfusb, hdr, data, len); |
| 266 | |
| 267 | if (hdr & 0x10) { |
| 268 | BT_ERR("%s error in block", bfusb->hdev->name); |
| 269 | if (bfusb->reassembly) |
| 270 | kfree_skb(bfusb->reassembly); |
| 271 | bfusb->reassembly = NULL; |
| 272 | return -EIO; |
| 273 | } |
| 274 | |
| 275 | if (hdr & 0x04) { |
| 276 | struct sk_buff *skb; |
| 277 | unsigned char pkt_type; |
| 278 | int pkt_len = 0; |
| 279 | |
| 280 | if (bfusb->reassembly) { |
| 281 | BT_ERR("%s unexpected start block", bfusb->hdev->name); |
| 282 | kfree_skb(bfusb->reassembly); |
| 283 | bfusb->reassembly = NULL; |
| 284 | } |
| 285 | |
| 286 | if (len < 1) { |
| 287 | BT_ERR("%s no packet type found", bfusb->hdev->name); |
| 288 | return -EPROTO; |
| 289 | } |
| 290 | |
| 291 | pkt_type = *data++; len--; |
| 292 | |
| 293 | switch (pkt_type) { |
| 294 | case HCI_EVENT_PKT: |
| 295 | if (len >= HCI_EVENT_HDR_SIZE) { |
| 296 | struct hci_event_hdr *hdr = (struct hci_event_hdr *) data; |
| 297 | pkt_len = HCI_EVENT_HDR_SIZE + hdr->plen; |
| 298 | } else { |
| 299 | BT_ERR("%s event block is too short", bfusb->hdev->name); |
| 300 | return -EILSEQ; |
| 301 | } |
| 302 | break; |
| 303 | |
| 304 | case HCI_ACLDATA_PKT: |
| 305 | if (len >= HCI_ACL_HDR_SIZE) { |
| 306 | struct hci_acl_hdr *hdr = (struct hci_acl_hdr *) data; |
| 307 | pkt_len = HCI_ACL_HDR_SIZE + __le16_to_cpu(hdr->dlen); |
| 308 | } else { |
| 309 | BT_ERR("%s data block is too short", bfusb->hdev->name); |
| 310 | return -EILSEQ; |
| 311 | } |
| 312 | break; |
| 313 | |
| 314 | case HCI_SCODATA_PKT: |
| 315 | if (len >= HCI_SCO_HDR_SIZE) { |
| 316 | struct hci_sco_hdr *hdr = (struct hci_sco_hdr *) data; |
| 317 | pkt_len = HCI_SCO_HDR_SIZE + hdr->dlen; |
| 318 | } else { |
| 319 | BT_ERR("%s audio block is too short", bfusb->hdev->name); |
| 320 | return -EILSEQ; |
| 321 | } |
| 322 | break; |
| 323 | } |
| 324 | |
| 325 | skb = bt_skb_alloc(pkt_len, GFP_ATOMIC); |
| 326 | if (!skb) { |
| 327 | BT_ERR("%s no memory for the packet", bfusb->hdev->name); |
| 328 | return -ENOMEM; |
| 329 | } |
| 330 | |
| 331 | skb->dev = (void *) bfusb->hdev; |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 332 | bt_cb(skb)->pkt_type = pkt_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | |
| 334 | bfusb->reassembly = skb; |
| 335 | } else { |
| 336 | if (!bfusb->reassembly) { |
| 337 | BT_ERR("%s unexpected continuation block", bfusb->hdev->name); |
| 338 | return -EIO; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | if (len > 0) |
| 343 | memcpy(skb_put(bfusb->reassembly, len), data, len); |
| 344 | |
| 345 | if (hdr & 0x08) { |
| 346 | hci_recv_frame(bfusb->reassembly); |
| 347 | bfusb->reassembly = NULL; |
| 348 | } |
| 349 | |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | static void bfusb_rx_complete(struct urb *urb, struct pt_regs *regs) |
| 354 | { |
| 355 | struct sk_buff *skb = (struct sk_buff *) urb->context; |
| 356 | struct bfusb *bfusb = (struct bfusb *) skb->dev; |
| 357 | unsigned char *buf = urb->transfer_buffer; |
| 358 | int count = urb->actual_length; |
| 359 | int err, hdr, len; |
| 360 | |
| 361 | BT_DBG("bfusb %p urb %p skb %p len %d", bfusb, urb, skb, skb->len); |
| 362 | |
| 363 | read_lock(&bfusb->lock); |
| 364 | |
| 365 | if (!test_bit(HCI_RUNNING, &bfusb->hdev->flags)) |
| 366 | goto unlock; |
| 367 | |
| 368 | if (urb->status || !count) |
| 369 | goto resubmit; |
| 370 | |
| 371 | bfusb->hdev->stat.byte_rx += count; |
| 372 | |
| 373 | skb_put(skb, count); |
| 374 | |
| 375 | while (count) { |
| 376 | hdr = buf[0] | (buf[1] << 8); |
| 377 | |
| 378 | if (hdr & 0x4000) { |
| 379 | len = 0; |
| 380 | count -= 2; |
| 381 | buf += 2; |
| 382 | } else { |
| 383 | len = (buf[2] == 0) ? 256 : buf[2]; |
| 384 | count -= 3; |
| 385 | buf += 3; |
| 386 | } |
| 387 | |
| 388 | if (count < len) { |
| 389 | BT_ERR("%s block extends over URB buffer ranges", |
| 390 | bfusb->hdev->name); |
| 391 | } |
| 392 | |
| 393 | if ((hdr & 0xe1) == 0xc1) |
| 394 | bfusb_recv_block(bfusb, hdr, buf, len); |
| 395 | |
| 396 | count -= len; |
| 397 | buf += len; |
| 398 | } |
| 399 | |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 400 | skb_unlink(skb, &bfusb->pending_q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | kfree_skb(skb); |
| 402 | |
| 403 | bfusb_rx_submit(bfusb, urb); |
| 404 | |
| 405 | read_unlock(&bfusb->lock); |
| 406 | |
| 407 | return; |
| 408 | |
| 409 | resubmit: |
| 410 | urb->dev = bfusb->udev; |
| 411 | |
| 412 | err = usb_submit_urb(urb, GFP_ATOMIC); |
| 413 | if (err) { |
| 414 | BT_ERR("%s bulk resubmit failed urb %p err %d", |
| 415 | bfusb->hdev->name, urb, err); |
| 416 | } |
| 417 | |
| 418 | unlock: |
| 419 | read_unlock(&bfusb->lock); |
| 420 | } |
| 421 | |
| 422 | |
| 423 | static int bfusb_open(struct hci_dev *hdev) |
| 424 | { |
| 425 | struct bfusb *bfusb = (struct bfusb *) hdev->driver_data; |
| 426 | unsigned long flags; |
| 427 | int i, err; |
| 428 | |
| 429 | BT_DBG("hdev %p bfusb %p", hdev, bfusb); |
| 430 | |
| 431 | if (test_and_set_bit(HCI_RUNNING, &hdev->flags)) |
| 432 | return 0; |
| 433 | |
| 434 | write_lock_irqsave(&bfusb->lock, flags); |
| 435 | |
| 436 | err = bfusb_rx_submit(bfusb, NULL); |
| 437 | if (!err) { |
| 438 | for (i = 1; i < BFUSB_MAX_BULK_RX; i++) |
| 439 | bfusb_rx_submit(bfusb, NULL); |
| 440 | } else { |
| 441 | clear_bit(HCI_RUNNING, &hdev->flags); |
| 442 | } |
| 443 | |
| 444 | write_unlock_irqrestore(&bfusb->lock, flags); |
| 445 | |
| 446 | return err; |
| 447 | } |
| 448 | |
| 449 | static int bfusb_flush(struct hci_dev *hdev) |
| 450 | { |
| 451 | struct bfusb *bfusb = (struct bfusb *) hdev->driver_data; |
| 452 | |
| 453 | BT_DBG("hdev %p bfusb %p", hdev, bfusb); |
| 454 | |
| 455 | skb_queue_purge(&bfusb->transmit_q); |
| 456 | |
| 457 | return 0; |
| 458 | } |
| 459 | |
| 460 | static int bfusb_close(struct hci_dev *hdev) |
| 461 | { |
| 462 | struct bfusb *bfusb = (struct bfusb *) hdev->driver_data; |
| 463 | unsigned long flags; |
| 464 | |
| 465 | BT_DBG("hdev %p bfusb %p", hdev, bfusb); |
| 466 | |
| 467 | if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags)) |
| 468 | return 0; |
| 469 | |
| 470 | write_lock_irqsave(&bfusb->lock, flags); |
| 471 | write_unlock_irqrestore(&bfusb->lock, flags); |
| 472 | |
| 473 | bfusb_unlink_urbs(bfusb); |
| 474 | bfusb_flush(hdev); |
| 475 | |
| 476 | return 0; |
| 477 | } |
| 478 | |
| 479 | static int bfusb_send_frame(struct sk_buff *skb) |
| 480 | { |
| 481 | struct hci_dev *hdev = (struct hci_dev *) skb->dev; |
| 482 | struct bfusb *bfusb; |
| 483 | struct sk_buff *nskb; |
| 484 | unsigned char buf[3]; |
| 485 | int sent = 0, size, count; |
| 486 | |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 487 | BT_DBG("hdev %p skb %p type %d len %d", hdev, skb, bt_cb(skb)->pkt_type, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | |
| 489 | if (!hdev) { |
| 490 | BT_ERR("Frame for unknown HCI device (hdev=NULL)"); |
| 491 | return -ENODEV; |
| 492 | } |
| 493 | |
| 494 | if (!test_bit(HCI_RUNNING, &hdev->flags)) |
| 495 | return -EBUSY; |
| 496 | |
| 497 | bfusb = (struct bfusb *) hdev->driver_data; |
| 498 | |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 499 | switch (bt_cb(skb)->pkt_type) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | case HCI_COMMAND_PKT: |
| 501 | hdev->stat.cmd_tx++; |
| 502 | break; |
| 503 | case HCI_ACLDATA_PKT: |
| 504 | hdev->stat.acl_tx++; |
| 505 | break; |
| 506 | case HCI_SCODATA_PKT: |
| 507 | hdev->stat.sco_tx++; |
| 508 | break; |
| 509 | }; |
| 510 | |
| 511 | /* Prepend skb with frame type */ |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 512 | memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | |
| 514 | count = skb->len; |
| 515 | |
| 516 | /* Max HCI frame size seems to be 1511 + 1 */ |
| 517 | if (!(nskb = bt_skb_alloc(count + 32, GFP_ATOMIC))) { |
| 518 | BT_ERR("Can't allocate memory for new packet"); |
| 519 | return -ENOMEM; |
| 520 | } |
| 521 | |
| 522 | nskb->dev = (void *) bfusb; |
| 523 | |
| 524 | while (count) { |
| 525 | size = min_t(uint, count, BFUSB_MAX_BLOCK_SIZE); |
| 526 | |
| 527 | buf[0] = 0xc1 | ((sent == 0) ? 0x04 : 0) | ((count == size) ? 0x08 : 0); |
| 528 | buf[1] = 0x00; |
| 529 | buf[2] = (size == BFUSB_MAX_BLOCK_SIZE) ? 0 : size; |
| 530 | |
| 531 | memcpy(skb_put(nskb, 3), buf, 3); |
| 532 | memcpy(skb_put(nskb, size), skb->data + sent, size); |
| 533 | |
| 534 | sent += size; |
| 535 | count -= size; |
| 536 | } |
| 537 | |
| 538 | /* Don't send frame with multiple size of bulk max packet */ |
| 539 | if ((nskb->len % bfusb->bulk_pkt_size) == 0) { |
| 540 | buf[0] = 0xdd; |
| 541 | buf[1] = 0x00; |
| 542 | memcpy(skb_put(nskb, 2), buf, 2); |
| 543 | } |
| 544 | |
| 545 | read_lock(&bfusb->lock); |
| 546 | |
| 547 | skb_queue_tail(&bfusb->transmit_q, nskb); |
| 548 | bfusb_tx_wakeup(bfusb); |
| 549 | |
| 550 | read_unlock(&bfusb->lock); |
| 551 | |
| 552 | kfree_skb(skb); |
| 553 | |
| 554 | return 0; |
| 555 | } |
| 556 | |
| 557 | static void bfusb_destruct(struct hci_dev *hdev) |
| 558 | { |
| 559 | struct bfusb *bfusb = (struct bfusb *) hdev->driver_data; |
| 560 | |
| 561 | BT_DBG("hdev %p bfusb %p", hdev, bfusb); |
| 562 | |
| 563 | kfree(bfusb); |
| 564 | } |
| 565 | |
| 566 | static int bfusb_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg) |
| 567 | { |
| 568 | return -ENOIOCTLCMD; |
| 569 | } |
| 570 | |
| 571 | |
| 572 | static int bfusb_load_firmware(struct bfusb *bfusb, unsigned char *firmware, int count) |
| 573 | { |
| 574 | unsigned char *buf; |
| 575 | int err, pipe, len, size, sent = 0; |
| 576 | |
| 577 | BT_DBG("bfusb %p udev %p", bfusb, bfusb->udev); |
| 578 | |
| 579 | BT_INFO("BlueFRITZ! USB loading firmware"); |
| 580 | |
| 581 | pipe = usb_sndctrlpipe(bfusb->udev, 0); |
| 582 | |
| 583 | if (usb_control_msg(bfusb->udev, pipe, USB_REQ_SET_CONFIGURATION, |
| 584 | 0, 1, 0, NULL, 0, USB_CTRL_SET_TIMEOUT) < 0) { |
| 585 | BT_ERR("Can't change to loading configuration"); |
| 586 | return -EBUSY; |
| 587 | } |
| 588 | |
| 589 | bfusb->udev->toggle[0] = bfusb->udev->toggle[1] = 0; |
| 590 | |
| 591 | buf = kmalloc(BFUSB_MAX_BLOCK_SIZE + 3, GFP_ATOMIC); |
| 592 | if (!buf) { |
| 593 | BT_ERR("Can't allocate memory chunk for firmware"); |
| 594 | return -ENOMEM; |
| 595 | } |
| 596 | |
| 597 | pipe = usb_sndbulkpipe(bfusb->udev, bfusb->bulk_out_ep); |
| 598 | |
| 599 | while (count) { |
| 600 | size = min_t(uint, count, BFUSB_MAX_BLOCK_SIZE + 3); |
| 601 | |
| 602 | memcpy(buf, firmware + sent, size); |
| 603 | |
| 604 | err = usb_bulk_msg(bfusb->udev, pipe, buf, size, |
| 605 | &len, BFUSB_BLOCK_TIMEOUT); |
| 606 | |
| 607 | if (err || (len != size)) { |
| 608 | BT_ERR("Error in firmware loading"); |
| 609 | goto error; |
| 610 | } |
| 611 | |
| 612 | sent += size; |
| 613 | count -= size; |
| 614 | } |
| 615 | |
| 616 | if ((err = usb_bulk_msg(bfusb->udev, pipe, NULL, 0, |
| 617 | &len, BFUSB_BLOCK_TIMEOUT)) < 0) { |
| 618 | BT_ERR("Error in null packet request"); |
| 619 | goto error; |
| 620 | } |
| 621 | |
| 622 | pipe = usb_sndctrlpipe(bfusb->udev, 0); |
| 623 | |
| 624 | if ((err = usb_control_msg(bfusb->udev, pipe, USB_REQ_SET_CONFIGURATION, |
| 625 | 0, 2, 0, NULL, 0, USB_CTRL_SET_TIMEOUT)) < 0) { |
| 626 | BT_ERR("Can't change to running configuration"); |
| 627 | goto error; |
| 628 | } |
| 629 | |
| 630 | bfusb->udev->toggle[0] = bfusb->udev->toggle[1] = 0; |
| 631 | |
| 632 | BT_INFO("BlueFRITZ! USB device ready"); |
| 633 | |
| 634 | kfree(buf); |
| 635 | return 0; |
| 636 | |
| 637 | error: |
| 638 | kfree(buf); |
| 639 | |
| 640 | pipe = usb_sndctrlpipe(bfusb->udev, 0); |
| 641 | |
| 642 | usb_control_msg(bfusb->udev, pipe, USB_REQ_SET_CONFIGURATION, |
| 643 | 0, 0, 0, NULL, 0, USB_CTRL_SET_TIMEOUT); |
| 644 | |
| 645 | return err; |
| 646 | } |
| 647 | |
| 648 | static int bfusb_probe(struct usb_interface *intf, const struct usb_device_id *id) |
| 649 | { |
| 650 | const struct firmware *firmware; |
| 651 | struct usb_device *udev = interface_to_usbdev(intf); |
| 652 | struct usb_host_endpoint *bulk_out_ep; |
| 653 | struct usb_host_endpoint *bulk_in_ep; |
| 654 | struct hci_dev *hdev; |
| 655 | struct bfusb *bfusb; |
| 656 | |
| 657 | BT_DBG("intf %p id %p", intf, id); |
| 658 | |
| 659 | if (ignore) |
| 660 | return -ENODEV; |
| 661 | |
| 662 | /* Check number of endpoints */ |
| 663 | if (intf->cur_altsetting->desc.bNumEndpoints < 2) |
| 664 | return -EIO; |
| 665 | |
| 666 | bulk_out_ep = &intf->cur_altsetting->endpoint[0]; |
| 667 | bulk_in_ep = &intf->cur_altsetting->endpoint[1]; |
| 668 | |
| 669 | if (!bulk_out_ep || !bulk_in_ep) { |
| 670 | BT_ERR("Bulk endpoints not found"); |
| 671 | goto done; |
| 672 | } |
| 673 | |
| 674 | /* Initialize control structure and load firmware */ |
Deepak Saxena | 089b1db | 2005-11-07 01:01:26 -0800 | [diff] [blame] | 675 | if (!(bfusb = kzalloc(sizeof(struct bfusb), GFP_KERNEL))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | BT_ERR("Can't allocate memory for control structure"); |
| 677 | goto done; |
| 678 | } |
| 679 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | bfusb->udev = udev; |
| 681 | bfusb->bulk_in_ep = bulk_in_ep->desc.bEndpointAddress; |
| 682 | bfusb->bulk_out_ep = bulk_out_ep->desc.bEndpointAddress; |
| 683 | bfusb->bulk_pkt_size = le16_to_cpu(bulk_out_ep->desc.wMaxPacketSize); |
| 684 | |
| 685 | rwlock_init(&bfusb->lock); |
| 686 | |
| 687 | bfusb->reassembly = NULL; |
| 688 | |
| 689 | skb_queue_head_init(&bfusb->transmit_q); |
| 690 | skb_queue_head_init(&bfusb->pending_q); |
| 691 | skb_queue_head_init(&bfusb->completed_q); |
| 692 | |
| 693 | if (request_firmware(&firmware, "bfubase.frm", &udev->dev) < 0) { |
| 694 | BT_ERR("Firmware request failed"); |
| 695 | goto error; |
| 696 | } |
| 697 | |
| 698 | BT_DBG("firmware data %p size %d", firmware->data, firmware->size); |
| 699 | |
| 700 | if (bfusb_load_firmware(bfusb, firmware->data, firmware->size) < 0) { |
| 701 | BT_ERR("Firmware loading failed"); |
| 702 | goto release; |
| 703 | } |
| 704 | |
| 705 | release_firmware(firmware); |
| 706 | |
| 707 | /* Initialize and register HCI device */ |
| 708 | hdev = hci_alloc_dev(); |
| 709 | if (!hdev) { |
| 710 | BT_ERR("Can't allocate HCI device"); |
| 711 | goto error; |
| 712 | } |
| 713 | |
| 714 | bfusb->hdev = hdev; |
| 715 | |
| 716 | hdev->type = HCI_USB; |
| 717 | hdev->driver_data = bfusb; |
| 718 | SET_HCIDEV_DEV(hdev, &intf->dev); |
| 719 | |
| 720 | hdev->open = bfusb_open; |
| 721 | hdev->close = bfusb_close; |
| 722 | hdev->flush = bfusb_flush; |
| 723 | hdev->send = bfusb_send_frame; |
| 724 | hdev->destruct = bfusb_destruct; |
| 725 | hdev->ioctl = bfusb_ioctl; |
| 726 | |
| 727 | hdev->owner = THIS_MODULE; |
| 728 | |
| 729 | if (hci_register_dev(hdev) < 0) { |
| 730 | BT_ERR("Can't register HCI device"); |
| 731 | hci_free_dev(hdev); |
| 732 | goto error; |
| 733 | } |
| 734 | |
| 735 | usb_set_intfdata(intf, bfusb); |
| 736 | |
| 737 | return 0; |
| 738 | |
| 739 | release: |
| 740 | release_firmware(firmware); |
| 741 | |
| 742 | error: |
| 743 | kfree(bfusb); |
| 744 | |
| 745 | done: |
| 746 | return -EIO; |
| 747 | } |
| 748 | |
| 749 | static void bfusb_disconnect(struct usb_interface *intf) |
| 750 | { |
| 751 | struct bfusb *bfusb = usb_get_intfdata(intf); |
| 752 | struct hci_dev *hdev = bfusb->hdev; |
| 753 | |
| 754 | BT_DBG("intf %p", intf); |
| 755 | |
| 756 | if (!hdev) |
| 757 | return; |
| 758 | |
| 759 | usb_set_intfdata(intf, NULL); |
| 760 | |
| 761 | bfusb_close(hdev); |
| 762 | |
| 763 | if (hci_unregister_dev(hdev) < 0) |
| 764 | BT_ERR("Can't unregister HCI device %s", hdev->name); |
| 765 | |
| 766 | hci_free_dev(hdev); |
| 767 | } |
| 768 | |
| 769 | static struct usb_driver bfusb_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | .name = "bfusb", |
| 771 | .probe = bfusb_probe, |
| 772 | .disconnect = bfusb_disconnect, |
| 773 | .id_table = bfusb_table, |
| 774 | }; |
| 775 | |
| 776 | static int __init bfusb_init(void) |
| 777 | { |
| 778 | int err; |
| 779 | |
| 780 | BT_INFO("BlueFRITZ! USB driver ver %s", VERSION); |
| 781 | |
| 782 | if ((err = usb_register(&bfusb_driver)) < 0) |
| 783 | BT_ERR("Failed to register BlueFRITZ! USB driver"); |
| 784 | |
| 785 | return err; |
| 786 | } |
| 787 | |
| 788 | static void __exit bfusb_exit(void) |
| 789 | { |
| 790 | usb_deregister(&bfusb_driver); |
| 791 | } |
| 792 | |
| 793 | module_init(bfusb_init); |
| 794 | module_exit(bfusb_exit); |
| 795 | |
| 796 | module_param(ignore, bool, 0644); |
| 797 | MODULE_PARM_DESC(ignore, "Ignore devices from the matching table"); |
| 798 | |
| 799 | MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>"); |
| 800 | MODULE_DESCRIPTION("BlueFRITZ! USB driver ver " VERSION); |
| 801 | MODULE_VERSION(VERSION); |
| 802 | MODULE_LICENSE("GPL"); |