24 #include <linux/module.h>
26 #include <linux/kernel.h>
28 #include <linux/slab.h>
29 #include <linux/types.h>
30 #include <linux/errno.h>
33 #include <linux/device.h>
43 static struct usb_driver bfusb_driver;
47 { USB_DEVICE(0x057c, 0x2200) },
54 #define BFUSB_MAX_BLOCK_SIZE 256
56 #define BFUSB_BLOCK_TIMEOUT 3000
58 #define BFUSB_TX_PROCESS 1
59 #define BFUSB_TX_WAKEUP 2
61 #define BFUSB_MAX_BULK_TX 2
62 #define BFUSB_MAX_BULK_RX 2
90 static void bfusb_tx_complete(
struct urb *
urb);
91 static void bfusb_rx_complete(
struct urb *
urb);
109 static void bfusb_unlink_urbs(
struct bfusb_data *data)
122 while ((urb = bfusb_get_completed(data)))
129 struct urb *urb = bfusb_get_completed(data);
132 BT_DBG(
"bfusb %p skb %p len %d", data, skb, skb->
len);
139 usb_fill_bulk_urb(urb, data->
udev, pipe, skb->
data, skb->
len,
140 bfusb_tx_complete, skb);
148 BT_ERR(
"%s bulk tx submit failed urb %p err %d",
149 data->
hdev->name, urb, err);
158 static void bfusb_tx_wakeup(
struct bfusb_data *data)
174 if (bfusb_send_bulk(data, skb) < 0) {
185 static void bfusb_tx_complete(
struct urb *urb)
190 BT_DBG(
"bfusb %p urb %p skb %p len %d", data, urb, skb, skb->
len);
198 data->
hdev->stat.byte_tx += skb->
len;
200 data->
hdev->stat.err_tx++;
207 bfusb_tx_wakeup(data);
213 static int bfusb_rx_submit(
struct bfusb_data *data,
struct urb *urb)
219 BT_DBG(
"bfusb %p urb %p", data, urb);
230 skb->
dev = (
void *) data;
237 usb_fill_bulk_urb(urb, data->
udev, pipe, skb->
data, size,
238 bfusb_rx_complete, skb);
244 BT_ERR(
"%s bulk rx submit failed urb %p err %d",
245 data->
hdev->name, urb, err);
254 static inline int bfusb_recv_block(
struct bfusb_data *data,
int hdr,
unsigned char *
buf,
int len)
256 BT_DBG(
"bfusb %p hdr 0x%02x data %p len %d", data, hdr, buf, len);
259 BT_ERR(
"%s error in block", data->
hdev->name);
271 BT_ERR(
"%s unexpected start block", data->
hdev->name);
277 BT_ERR(
"%s no packet type found", data->
hdev->name);
281 pkt_type = *buf++; len--;
289 BT_ERR(
"%s event block is too short", data->
hdev->name);
299 BT_ERR(
"%s data block is too short", data->
hdev->name);
309 BT_ERR(
"%s audio block is too short", data->
hdev->name);
317 BT_ERR(
"%s no memory for the packet", data->
hdev->name);
321 skb->
dev = (
void *) data->
hdev;
322 bt_cb(skb)->pkt_type = pkt_type;
327 BT_ERR(
"%s unexpected continuation block", data->
hdev->name);
343 static void bfusb_rx_complete(
struct urb *urb)
347 unsigned char *buf = urb->transfer_buffer;
348 int count = urb->actual_length;
351 BT_DBG(
"bfusb %p urb %p skb %p len %d", data, urb, skb, skb->
len);
358 if (urb->status || !count)
366 hdr = buf[0] | (buf[1] << 8);
373 len = (buf[2] == 0) ? 256 : buf[2];
379 BT_ERR(
"%s block extends over URB buffer ranges",
383 if ((hdr & 0xe1) == 0xc1)
384 bfusb_recv_block(data, hdr, buf, len);
393 bfusb_rx_submit(data, urb);
400 urb->dev = data->
udev;
404 BT_ERR(
"%s bulk resubmit failed urb %p err %d",
405 data->
hdev->name, urb, err);
412 static int bfusb_open(
struct hci_dev *hdev)
414 struct bfusb_data *data = hci_get_drvdata(hdev);
418 BT_DBG(
"hdev %p bfusb %p", hdev, data);
425 err = bfusb_rx_submit(data,
NULL);
428 bfusb_rx_submit(data,
NULL);
438 static int bfusb_flush(
struct hci_dev *hdev)
440 struct bfusb_data *data = hci_get_drvdata(hdev);
442 BT_DBG(
"hdev %p bfusb %p", hdev, data);
449 static int bfusb_close(
struct hci_dev *hdev)
451 struct bfusb_data *data = hci_get_drvdata(hdev);
454 BT_DBG(
"hdev %p bfusb %p", hdev, data);
462 bfusb_unlink_urbs(data);
468 static int bfusb_send_frame(
struct sk_buff *skb)
473 unsigned char buf[3];
474 int sent = 0, size, count;
476 BT_DBG(
"hdev %p skb %p type %d len %d", hdev, skb,
bt_cb(skb)->pkt_type, skb->
len);
479 BT_ERR(
"Frame for unknown HCI device (hdev=NULL)");
486 data = hci_get_drvdata(hdev);
488 switch (
bt_cb(skb)->pkt_type) {
508 BT_ERR(
"Can't allocate memory for new packet");
512 nskb->dev = (
void *) data;
517 buf[0] = 0xc1 | ((sent == 0) ? 0x04 : 0) | ((count ==
size) ? 0x08 : 0);
522 skb_copy_from_linear_data_offset(skb, sent,
skb_put(nskb, size), size);
538 bfusb_tx_wakeup(data);
547 static int bfusb_ioctl(
struct hci_dev *hdev,
unsigned int cmd,
unsigned long arg)
552 static int bfusb_load_firmware(
struct bfusb_data *data,
553 const unsigned char *
firmware,
int count)
560 BT_INFO(
"BlueFRITZ! USB loading firmware");
564 BT_ERR(
"Can't allocate memory chunk for firmware");
568 pipe = usb_sndctrlpipe(data->
udev, 0);
571 0, 1, 0,
NULL, 0, USB_CTRL_SET_TIMEOUT) < 0) {
572 BT_ERR(
"Can't change to loading configuration");
577 data->
udev->toggle[0] = data->
udev->toggle[1] = 0;
584 memcpy(buf, firmware + sent, size);
589 if (err || (len != size)) {
590 BT_ERR(
"Error in firmware loading");
601 BT_ERR(
"Error in null packet request");
605 pipe = usb_sndctrlpipe(data->
udev, 0);
608 0, 2, 0,
NULL, 0, USB_CTRL_SET_TIMEOUT);
610 BT_ERR(
"Can't change to running configuration");
614 data->
udev->toggle[0] = data->
udev->toggle[1] = 0;
616 BT_INFO(
"BlueFRITZ! USB device ready");
624 pipe = usb_sndctrlpipe(data->
udev, 0);
627 0, 0, 0,
NULL, 0, USB_CTRL_SET_TIMEOUT);
634 const struct firmware *firmware;
635 struct usb_device *
udev = interface_to_usbdev(intf);
636 struct usb_host_endpoint *bulk_out_ep;
637 struct usb_host_endpoint *bulk_in_ep;
641 BT_DBG(
"intf %p id %p", intf,
id);
644 if (intf->cur_altsetting->desc.bNumEndpoints < 2)
647 bulk_out_ep = &intf->cur_altsetting->endpoint[0];
648 bulk_in_ep = &intf->cur_altsetting->endpoint[1];
650 if (!bulk_out_ep || !bulk_in_ep) {
651 BT_ERR(
"Bulk endpoints not found");
658 BT_ERR(
"Can't allocate memory for control structure");
663 data->
bulk_in_ep = bulk_in_ep->desc.bEndpointAddress;
664 data->
bulk_out_ep = bulk_out_ep->desc.bEndpointAddress;
676 BT_ERR(
"Firmware request failed");
680 BT_DBG(
"firmware data %p size %zu", firmware->
data, firmware->
size);
682 if (bfusb_load_firmware(data, firmware->
data, firmware->
size) < 0) {
683 BT_ERR(
"Firmware loading failed");
692 BT_ERR(
"Can't allocate HCI device");
699 hci_set_drvdata(hdev, data);
702 hdev->
open = bfusb_open;
703 hdev->
close = bfusb_close;
704 hdev->
flush = bfusb_flush;
705 hdev->
send = bfusb_send_frame;
706 hdev->
ioctl = bfusb_ioctl;
709 BT_ERR(
"Can't register HCI device");
714 usb_set_intfdata(intf, data);
727 struct bfusb_data *data = usb_get_intfdata(intf);
735 usb_set_intfdata(intf,
NULL);
743 static struct usb_driver bfusb_driver = {
745 .probe = bfusb_probe,
746 .disconnect = bfusb_disconnect,
747 .id_table = bfusb_table,
748 .disable_hub_initiated_lpm = 1,