treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / bluetooth / bfusb.c
blob0e5954cac98e0c69fca26e450e972f8424cfe548
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
4 * AVM BlueFRITZ! USB driver
6 * Copyright (C) 2003-2006 Marcel Holtmann <marcel@holtmann.org>
7 */
9 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/slab.h>
14 #include <linux/types.h>
15 #include <linux/errno.h>
16 #include <linux/skbuff.h>
18 #include <linux/device.h>
19 #include <linux/firmware.h>
21 #include <linux/usb.h>
23 #include <net/bluetooth/bluetooth.h>
24 #include <net/bluetooth/hci_core.h>
26 #define VERSION "1.2"
28 static struct usb_driver bfusb_driver;
30 static const struct usb_device_id bfusb_table[] = {
31 /* AVM BlueFRITZ! USB */
32 { USB_DEVICE(0x057c, 0x2200) },
34 { } /* Terminating entry */
37 MODULE_DEVICE_TABLE(usb, bfusb_table);
39 #define BFUSB_MAX_BLOCK_SIZE 256
41 #define BFUSB_BLOCK_TIMEOUT 3000
43 #define BFUSB_TX_PROCESS 1
44 #define BFUSB_TX_WAKEUP 2
46 #define BFUSB_MAX_BULK_TX 2
47 #define BFUSB_MAX_BULK_RX 2
49 struct bfusb_data {
50 struct hci_dev *hdev;
52 unsigned long state;
54 struct usb_device *udev;
56 unsigned int bulk_in_ep;
57 unsigned int bulk_out_ep;
58 unsigned int bulk_pkt_size;
60 rwlock_t lock;
62 struct sk_buff_head transmit_q;
64 struct sk_buff *reassembly;
66 atomic_t pending_tx;
67 struct sk_buff_head pending_q;
68 struct sk_buff_head completed_q;
71 struct bfusb_data_scb {
72 struct urb *urb;
75 static void bfusb_tx_complete(struct urb *urb);
76 static void bfusb_rx_complete(struct urb *urb);
78 static struct urb *bfusb_get_completed(struct bfusb_data *data)
80 struct sk_buff *skb;
81 struct urb *urb = NULL;
83 BT_DBG("bfusb %p", data);
85 skb = skb_dequeue(&data->completed_q);
86 if (skb) {
87 urb = ((struct bfusb_data_scb *) skb->cb)->urb;
88 kfree_skb(skb);
91 return urb;
94 static void bfusb_unlink_urbs(struct bfusb_data *data)
96 struct sk_buff *skb;
97 struct urb *urb;
99 BT_DBG("bfusb %p", data);
101 while ((skb = skb_dequeue(&data->pending_q))) {
102 urb = ((struct bfusb_data_scb *) skb->cb)->urb;
103 usb_kill_urb(urb);
104 skb_queue_tail(&data->completed_q, skb);
107 while ((urb = bfusb_get_completed(data)))
108 usb_free_urb(urb);
111 static int bfusb_send_bulk(struct bfusb_data *data, struct sk_buff *skb)
113 struct bfusb_data_scb *scb = (void *) skb->cb;
114 struct urb *urb = bfusb_get_completed(data);
115 int err, pipe;
117 BT_DBG("bfusb %p skb %p len %d", data, skb, skb->len);
119 if (!urb) {
120 urb = usb_alloc_urb(0, GFP_ATOMIC);
121 if (!urb)
122 return -ENOMEM;
125 pipe = usb_sndbulkpipe(data->udev, data->bulk_out_ep);
127 usb_fill_bulk_urb(urb, data->udev, pipe, skb->data, skb->len,
128 bfusb_tx_complete, skb);
130 scb->urb = urb;
132 skb_queue_tail(&data->pending_q, skb);
134 err = usb_submit_urb(urb, GFP_ATOMIC);
135 if (err) {
136 BT_ERR("%s bulk tx submit failed urb %p err %d",
137 data->hdev->name, urb, err);
138 skb_unlink(skb, &data->pending_q);
139 usb_free_urb(urb);
140 } else
141 atomic_inc(&data->pending_tx);
143 return err;
146 static void bfusb_tx_wakeup(struct bfusb_data *data)
148 struct sk_buff *skb;
150 BT_DBG("bfusb %p", data);
152 if (test_and_set_bit(BFUSB_TX_PROCESS, &data->state)) {
153 set_bit(BFUSB_TX_WAKEUP, &data->state);
154 return;
157 do {
158 clear_bit(BFUSB_TX_WAKEUP, &data->state);
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);
164 break;
168 } while (test_bit(BFUSB_TX_WAKEUP, &data->state));
170 clear_bit(BFUSB_TX_PROCESS, &data->state);
173 static void bfusb_tx_complete(struct urb *urb)
175 struct sk_buff *skb = (struct sk_buff *) urb->context;
176 struct bfusb_data *data = (struct bfusb_data *) skb->dev;
178 BT_DBG("bfusb %p urb %p skb %p len %d", data, urb, skb, skb->len);
180 atomic_dec(&data->pending_tx);
182 if (!test_bit(HCI_RUNNING, &data->hdev->flags))
183 return;
185 if (!urb->status)
186 data->hdev->stat.byte_tx += skb->len;
187 else
188 data->hdev->stat.err_tx++;
190 read_lock(&data->lock);
192 skb_unlink(skb, &data->pending_q);
193 skb_queue_tail(&data->completed_q, skb);
195 bfusb_tx_wakeup(data);
197 read_unlock(&data->lock);
201 static int bfusb_rx_submit(struct bfusb_data *data, struct urb *urb)
203 struct bfusb_data_scb *scb;
204 struct sk_buff *skb;
205 int err, pipe, size = HCI_MAX_FRAME_SIZE + 32;
207 BT_DBG("bfusb %p urb %p", data, urb);
209 if (!urb) {
210 urb = usb_alloc_urb(0, GFP_ATOMIC);
211 if (!urb)
212 return -ENOMEM;
215 skb = bt_skb_alloc(size, GFP_ATOMIC);
216 if (!skb) {
217 usb_free_urb(urb);
218 return -ENOMEM;
221 skb->dev = (void *) data;
223 scb = (struct bfusb_data_scb *) skb->cb;
224 scb->urb = urb;
226 pipe = usb_rcvbulkpipe(data->udev, data->bulk_in_ep);
228 usb_fill_bulk_urb(urb, data->udev, pipe, skb->data, size,
229 bfusb_rx_complete, skb);
231 skb_queue_tail(&data->pending_q, skb);
233 err = usb_submit_urb(urb, GFP_ATOMIC);
234 if (err) {
235 BT_ERR("%s bulk rx submit failed urb %p err %d",
236 data->hdev->name, urb, err);
237 skb_unlink(skb, &data->pending_q);
238 kfree_skb(skb);
239 usb_free_urb(urb);
242 return err;
245 static inline int bfusb_recv_block(struct bfusb_data *data, int hdr, unsigned char *buf, int len)
247 BT_DBG("bfusb %p hdr 0x%02x data %p len %d", data, hdr, buf, len);
249 if (hdr & 0x10) {
250 BT_ERR("%s error in block", data->hdev->name);
251 kfree_skb(data->reassembly);
252 data->reassembly = NULL;
253 return -EIO;
256 if (hdr & 0x04) {
257 struct sk_buff *skb;
258 unsigned char pkt_type;
259 int pkt_len = 0;
261 if (data->reassembly) {
262 BT_ERR("%s unexpected start block", data->hdev->name);
263 kfree_skb(data->reassembly);
264 data->reassembly = NULL;
267 if (len < 1) {
268 BT_ERR("%s no packet type found", data->hdev->name);
269 return -EPROTO;
272 pkt_type = *buf++; len--;
274 switch (pkt_type) {
275 case HCI_EVENT_PKT:
276 if (len >= HCI_EVENT_HDR_SIZE) {
277 struct hci_event_hdr *hdr = (struct hci_event_hdr *) buf;
278 pkt_len = HCI_EVENT_HDR_SIZE + hdr->plen;
279 } else {
280 BT_ERR("%s event block is too short", data->hdev->name);
281 return -EILSEQ;
283 break;
285 case HCI_ACLDATA_PKT:
286 if (len >= HCI_ACL_HDR_SIZE) {
287 struct hci_acl_hdr *hdr = (struct hci_acl_hdr *) buf;
288 pkt_len = HCI_ACL_HDR_SIZE + __le16_to_cpu(hdr->dlen);
289 } else {
290 BT_ERR("%s data block is too short", data->hdev->name);
291 return -EILSEQ;
293 break;
295 case HCI_SCODATA_PKT:
296 if (len >= HCI_SCO_HDR_SIZE) {
297 struct hci_sco_hdr *hdr = (struct hci_sco_hdr *) buf;
298 pkt_len = HCI_SCO_HDR_SIZE + hdr->dlen;
299 } else {
300 BT_ERR("%s audio block is too short", data->hdev->name);
301 return -EILSEQ;
303 break;
306 skb = bt_skb_alloc(pkt_len, GFP_ATOMIC);
307 if (!skb) {
308 BT_ERR("%s no memory for the packet", data->hdev->name);
309 return -ENOMEM;
312 hci_skb_pkt_type(skb) = pkt_type;
314 data->reassembly = skb;
315 } else {
316 if (!data->reassembly) {
317 BT_ERR("%s unexpected continuation block", data->hdev->name);
318 return -EIO;
322 if (len > 0)
323 skb_put_data(data->reassembly, buf, len);
325 if (hdr & 0x08) {
326 hci_recv_frame(data->hdev, data->reassembly);
327 data->reassembly = NULL;
330 return 0;
333 static void bfusb_rx_complete(struct urb *urb)
335 struct sk_buff *skb = (struct sk_buff *) urb->context;
336 struct bfusb_data *data = (struct bfusb_data *) skb->dev;
337 unsigned char *buf = urb->transfer_buffer;
338 int count = urb->actual_length;
339 int err, hdr, len;
341 BT_DBG("bfusb %p urb %p skb %p len %d", data, urb, skb, skb->len);
343 read_lock(&data->lock);
345 if (!test_bit(HCI_RUNNING, &data->hdev->flags))
346 goto unlock;
348 if (urb->status || !count)
349 goto resubmit;
351 data->hdev->stat.byte_rx += count;
353 skb_put(skb, count);
355 while (count) {
356 hdr = buf[0] | (buf[1] << 8);
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;
368 if (count < len) {
369 BT_ERR("%s block extends over URB buffer ranges",
370 data->hdev->name);
373 if ((hdr & 0xe1) == 0xc1)
374 bfusb_recv_block(data, hdr, buf, len);
376 count -= len;
377 buf += len;
380 skb_unlink(skb, &data->pending_q);
381 kfree_skb(skb);
383 bfusb_rx_submit(data, urb);
385 read_unlock(&data->lock);
387 return;
389 resubmit:
390 urb->dev = data->udev;
392 err = usb_submit_urb(urb, GFP_ATOMIC);
393 if (err) {
394 BT_ERR("%s bulk resubmit failed urb %p err %d",
395 data->hdev->name, urb, err);
398 unlock:
399 read_unlock(&data->lock);
402 static int bfusb_open(struct hci_dev *hdev)
404 struct bfusb_data *data = hci_get_drvdata(hdev);
405 unsigned long flags;
406 int i, err;
408 BT_DBG("hdev %p bfusb %p", hdev, data);
410 write_lock_irqsave(&data->lock, flags);
412 err = bfusb_rx_submit(data, NULL);
413 if (!err) {
414 for (i = 1; i < BFUSB_MAX_BULK_RX; i++)
415 bfusb_rx_submit(data, NULL);
418 write_unlock_irqrestore(&data->lock, flags);
420 return err;
423 static int bfusb_flush(struct hci_dev *hdev)
425 struct bfusb_data *data = hci_get_drvdata(hdev);
427 BT_DBG("hdev %p bfusb %p", hdev, data);
429 skb_queue_purge(&data->transmit_q);
431 return 0;
434 static int bfusb_close(struct hci_dev *hdev)
436 struct bfusb_data *data = hci_get_drvdata(hdev);
437 unsigned long flags;
439 BT_DBG("hdev %p bfusb %p", hdev, data);
441 write_lock_irqsave(&data->lock, flags);
442 write_unlock_irqrestore(&data->lock, flags);
444 bfusb_unlink_urbs(data);
445 bfusb_flush(hdev);
447 return 0;
450 static int bfusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
452 struct bfusb_data *data = hci_get_drvdata(hdev);
453 struct sk_buff *nskb;
454 unsigned char buf[3];
455 int sent = 0, size, count;
457 BT_DBG("hdev %p skb %p type %d len %d", hdev, skb,
458 hci_skb_pkt_type(skb), skb->len);
460 switch (hci_skb_pkt_type(skb)) {
461 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;
472 /* Prepend skb with frame type */
473 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
475 count = skb->len;
477 /* Max HCI frame size seems to be 1511 + 1 */
478 nskb = bt_skb_alloc(count + 32, GFP_KERNEL);
479 if (!nskb) {
480 BT_ERR("Can't allocate memory for new packet");
481 return -ENOMEM;
484 nskb->dev = (void *) data;
486 while (count) {
487 size = min_t(uint, count, BFUSB_MAX_BLOCK_SIZE);
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;
493 skb_put_data(nskb, buf, 3);
494 skb_copy_from_linear_data_offset(skb, sent, skb_put(nskb, size), size);
496 sent += size;
497 count -= size;
500 /* Don't send frame with multiple size of bulk max packet */
501 if ((nskb->len % data->bulk_pkt_size) == 0) {
502 buf[0] = 0xdd;
503 buf[1] = 0x00;
504 skb_put_data(nskb, buf, 2);
507 read_lock(&data->lock);
509 skb_queue_tail(&data->transmit_q, nskb);
510 bfusb_tx_wakeup(data);
512 read_unlock(&data->lock);
514 kfree_skb(skb);
516 return 0;
519 static int bfusb_load_firmware(struct bfusb_data *data,
520 const unsigned char *firmware, int count)
522 unsigned char *buf;
523 int err, pipe, len, size, sent = 0;
525 BT_DBG("bfusb %p udev %p", data, data->udev);
527 BT_INFO("BlueFRITZ! USB loading firmware");
529 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;
535 pipe = usb_sndctrlpipe(data->udev, 0);
537 if (usb_control_msg(data->udev, pipe, USB_REQ_SET_CONFIGURATION,
538 0, 1, 0, NULL, 0, USB_CTRL_SET_TIMEOUT) < 0) {
539 BT_ERR("Can't change to loading configuration");
540 kfree(buf);
541 return -EBUSY;
544 data->udev->toggle[0] = data->udev->toggle[1] = 0;
546 pipe = usb_sndbulkpipe(data->udev, data->bulk_out_ep);
548 while (count) {
549 size = min_t(uint, count, BFUSB_MAX_BLOCK_SIZE + 3);
551 memcpy(buf, firmware + sent, size);
553 err = usb_bulk_msg(data->udev, pipe, buf, size,
554 &len, BFUSB_BLOCK_TIMEOUT);
556 if (err || (len != size)) {
557 BT_ERR("Error in firmware loading");
558 goto error;
561 sent += size;
562 count -= size;
565 err = usb_bulk_msg(data->udev, pipe, NULL, 0,
566 &len, BFUSB_BLOCK_TIMEOUT);
567 if (err < 0) {
568 BT_ERR("Error in null packet request");
569 goto error;
572 pipe = usb_sndctrlpipe(data->udev, 0);
574 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) {
577 BT_ERR("Can't change to running configuration");
578 goto error;
581 data->udev->toggle[0] = data->udev->toggle[1] = 0;
583 BT_INFO("BlueFRITZ! USB device ready");
585 kfree(buf);
586 return 0;
588 error:
589 kfree(buf);
591 pipe = usb_sndctrlpipe(data->udev, 0);
593 usb_control_msg(data->udev, pipe, USB_REQ_SET_CONFIGURATION,
594 0, 0, 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
596 return err;
599 static int bfusb_probe(struct usb_interface *intf, const struct usb_device_id *id)
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;
606 struct bfusb_data *data;
608 BT_DBG("intf %p id %p", intf, id);
610 /* Check number of endpoints */
611 if (intf->cur_altsetting->desc.bNumEndpoints < 2)
612 return -EIO;
614 bulk_out_ep = &intf->cur_altsetting->endpoint[0];
615 bulk_in_ep = &intf->cur_altsetting->endpoint[1];
617 if (!bulk_out_ep || !bulk_in_ep) {
618 BT_ERR("Bulk endpoints not found");
619 goto done;
622 /* Initialize control structure and load firmware */
623 data = devm_kzalloc(&intf->dev, sizeof(struct bfusb_data), GFP_KERNEL);
624 if (!data)
625 return -ENOMEM;
627 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);
632 rwlock_init(&data->lock);
634 data->reassembly = NULL;
636 skb_queue_head_init(&data->transmit_q);
637 skb_queue_head_init(&data->pending_q);
638 skb_queue_head_init(&data->completed_q);
640 if (request_firmware(&firmware, "bfubase.frm", &udev->dev) < 0) {
641 BT_ERR("Firmware request failed");
642 goto done;
645 BT_DBG("firmware data %p size %zu", firmware->data, firmware->size);
647 if (bfusb_load_firmware(data, firmware->data, firmware->size) < 0) {
648 BT_ERR("Firmware loading failed");
649 goto release;
652 release_firmware(firmware);
654 /* Initialize and register HCI device */
655 hdev = hci_alloc_dev();
656 if (!hdev) {
657 BT_ERR("Can't allocate HCI device");
658 goto done;
661 data->hdev = hdev;
663 hdev->bus = HCI_USB;
664 hci_set_drvdata(hdev, data);
665 SET_HCIDEV_DEV(hdev, &intf->dev);
667 hdev->open = bfusb_open;
668 hdev->close = bfusb_close;
669 hdev->flush = bfusb_flush;
670 hdev->send = bfusb_send_frame;
672 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
674 if (hci_register_dev(hdev) < 0) {
675 BT_ERR("Can't register HCI device");
676 hci_free_dev(hdev);
677 goto done;
680 usb_set_intfdata(intf, data);
682 return 0;
684 release:
685 release_firmware(firmware);
687 done:
688 return -EIO;
691 static void bfusb_disconnect(struct usb_interface *intf)
693 struct bfusb_data *data = usb_get_intfdata(intf);
694 struct hci_dev *hdev = data->hdev;
696 BT_DBG("intf %p", intf);
698 if (!hdev)
699 return;
701 usb_set_intfdata(intf, NULL);
703 bfusb_close(hdev);
705 hci_unregister_dev(hdev);
706 hci_free_dev(hdev);
709 static struct usb_driver bfusb_driver = {
710 .name = "bfusb",
711 .probe = bfusb_probe,
712 .disconnect = bfusb_disconnect,
713 .id_table = bfusb_table,
714 .disable_hub_initiated_lpm = 1,
717 module_usb_driver(bfusb_driver);
719 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
720 MODULE_DESCRIPTION("BlueFRITZ! USB driver ver " VERSION);
721 MODULE_VERSION(VERSION);
722 MODULE_LICENSE("GPL");
723 MODULE_FIRMWARE("bfubase.frm");