15e23b923SMarcel Holtmann /* 25e23b923SMarcel Holtmann * 35e23b923SMarcel Holtmann * Generic Bluetooth USB driver 45e23b923SMarcel Holtmann * 59bfa35feSMarcel Holtmann * Copyright (C) 2005-2008 Marcel Holtmann <marcel@holtmann.org> 65e23b923SMarcel Holtmann * 75e23b923SMarcel Holtmann * 85e23b923SMarcel Holtmann * This program is free software; you can redistribute it and/or modify 95e23b923SMarcel Holtmann * it under the terms of the GNU General Public License as published by 105e23b923SMarcel Holtmann * the Free Software Foundation; either version 2 of the License, or 115e23b923SMarcel Holtmann * (at your option) any later version. 125e23b923SMarcel Holtmann * 135e23b923SMarcel Holtmann * This program is distributed in the hope that it will be useful, 145e23b923SMarcel Holtmann * but WITHOUT ANY WARRANTY; without even the implied warranty of 155e23b923SMarcel Holtmann * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 165e23b923SMarcel Holtmann * GNU General Public License for more details. 175e23b923SMarcel Holtmann * 185e23b923SMarcel Holtmann * You should have received a copy of the GNU General Public License 195e23b923SMarcel Holtmann * along with this program; if not, write to the Free Software 205e23b923SMarcel Holtmann * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 215e23b923SMarcel Holtmann * 225e23b923SMarcel Holtmann */ 235e23b923SMarcel Holtmann 245e23b923SMarcel Holtmann #include <linux/kernel.h> 255e23b923SMarcel Holtmann #include <linux/module.h> 265e23b923SMarcel Holtmann #include <linux/init.h> 275e23b923SMarcel Holtmann #include <linux/slab.h> 285e23b923SMarcel Holtmann #include <linux/types.h> 295e23b923SMarcel Holtmann #include <linux/sched.h> 305e23b923SMarcel Holtmann #include <linux/errno.h> 315e23b923SMarcel Holtmann #include <linux/skbuff.h> 325e23b923SMarcel Holtmann 335e23b923SMarcel Holtmann #include <linux/usb.h> 345e23b923SMarcel Holtmann 355e23b923SMarcel Holtmann #include <net/bluetooth/bluetooth.h> 365e23b923SMarcel Holtmann #include <net/bluetooth/hci_core.h> 375e23b923SMarcel Holtmann 387bee549eSOliver Neukum #define VERSION "0.6" 39cfeb4145SMarcel Holtmann 4090ab5ee9SRusty Russell static bool ignore_dga; 4190ab5ee9SRusty Russell static bool ignore_csr; 4290ab5ee9SRusty Russell static bool ignore_sniffer; 4390ab5ee9SRusty Russell static bool disable_scofix; 4490ab5ee9SRusty Russell static bool force_scofix; 457a9d4020SMarcel Holtmann 4690ab5ee9SRusty Russell static bool reset = 1; 47cfeb4145SMarcel Holtmann 48cfeb4145SMarcel Holtmann static struct usb_driver btusb_driver; 49cfeb4145SMarcel Holtmann 50cfeb4145SMarcel Holtmann #define BTUSB_IGNORE 0x01 517a9d4020SMarcel Holtmann #define BTUSB_DIGIANSWER 0x02 527a9d4020SMarcel Holtmann #define BTUSB_CSR 0x04 537a9d4020SMarcel Holtmann #define BTUSB_SNIFFER 0x08 547a9d4020SMarcel Holtmann #define BTUSB_BCM92035 0x10 557a9d4020SMarcel Holtmann #define BTUSB_BROKEN_ISOC 0x20 567a9d4020SMarcel Holtmann #define BTUSB_WRONG_SCO_MTU 0x40 572d25f8b4SSteven.Li #define BTUSB_ATH3012 0x80 585e23b923SMarcel Holtmann 595e23b923SMarcel Holtmann static struct usb_device_id btusb_table[] = { 605e23b923SMarcel Holtmann /* Generic Bluetooth USB device */ 615e23b923SMarcel Holtmann { USB_DEVICE_INFO(0xe0, 0x01, 0x01) }, 625e23b923SMarcel Holtmann 63c510eae3SOliver Neukum /* Broadcom SoftSailing reporting vendor specific */ 642e8b5063SDon Zickus { USB_DEVICE(0x0a5c, 0x21e1) }, 65c510eae3SOliver Neukum 663cd01976SNobuhiro Iwamatsu /* Apple MacBookPro 7,1 */ 673cd01976SNobuhiro Iwamatsu { USB_DEVICE(0x05ac, 0x8213) }, 683cd01976SNobuhiro Iwamatsu 690a79f674SCyril Lacoux /* Apple iMac11,1 */ 700a79f674SCyril Lacoux { USB_DEVICE(0x05ac, 0x8215) }, 710a79f674SCyril Lacoux 729c047157SNobuhiro Iwamatsu /* Apple MacBookPro6,2 */ 739c047157SNobuhiro Iwamatsu { USB_DEVICE(0x05ac, 0x8218) }, 749c047157SNobuhiro Iwamatsu 753e3ede7dSEdgar (gimli) Hucek /* Apple MacBookAir3,1, MacBookAir3,2 */ 763e3ede7dSEdgar (gimli) Hucek { USB_DEVICE(0x05ac, 0x821b) }, 773e3ede7dSEdgar (gimli) Hucek 78a63b723dSPieter-Augustijn Van Malleghem /* Apple MacBookAir4,1 */ 79a63b723dSPieter-Augustijn Van Malleghem { USB_DEVICE(0x05ac, 0x821f) }, 80a63b723dSPieter-Augustijn Van Malleghem 8188d377b6SMarc-Antoine Perennou /* Apple MacBookPro8,2 */ 8288d377b6SMarc-Antoine Perennou { USB_DEVICE(0x05ac, 0x821a) }, 8388d377b6SMarc-Antoine Perennou 84f78b6826SJurgen Kramer /* Apple MacMini5,1 */ 85f78b6826SJurgen Kramer { USB_DEVICE(0x05ac, 0x8281) }, 86f78b6826SJurgen Kramer 87cfeb4145SMarcel Holtmann /* AVM BlueFRITZ! USB v2.0 */ 88cfeb4145SMarcel Holtmann { USB_DEVICE(0x057c, 0x3800) }, 89cfeb4145SMarcel Holtmann 90cfeb4145SMarcel Holtmann /* Bluetooth Ultraport Module from IBM */ 91cfeb4145SMarcel Holtmann { USB_DEVICE(0x04bf, 0x030a) }, 92cfeb4145SMarcel Holtmann 93cfeb4145SMarcel Holtmann /* ALPS Modules with non-standard id */ 94cfeb4145SMarcel Holtmann { USB_DEVICE(0x044e, 0x3001) }, 95cfeb4145SMarcel Holtmann { USB_DEVICE(0x044e, 0x3002) }, 96cfeb4145SMarcel Holtmann 97cfeb4145SMarcel Holtmann /* Ericsson with non-standard id */ 98cfeb4145SMarcel Holtmann { USB_DEVICE(0x0bdb, 0x1002) }, 99cfeb4145SMarcel Holtmann 100cfeb4145SMarcel Holtmann /* Canyon CN-BTU1 with HID interfaces */ 1017a9d4020SMarcel Holtmann { USB_DEVICE(0x0c10, 0x0000) }, 102cfeb4145SMarcel Holtmann 103d13431caSWen-chien Jesse Sung /* Broadcom BCM20702A0 */ 10479cd7602SManoj Iyer { USB_DEVICE(0x0489, 0xe042) }, 105c0190925SJesse Sung { USB_DEVICE(0x0a5c, 0x21e3) }, 1060a4eaeebSJames M. Leddy { USB_DEVICE(0x0a5c, 0x21e6) }, 1076dfc326fSJoão Paulo Rechi Vita { USB_DEVICE(0x0a5c, 0x21e8) }, 10837305cf6SManoj Iyer { USB_DEVICE(0x0a5c, 0x21f3) }, 109d13431caSWen-chien Jesse Sung { USB_DEVICE(0x413c, 0x8197) }, 110d13431caSWen-chien Jesse Sung 1115e23b923SMarcel Holtmann { } /* Terminating entry */ 1125e23b923SMarcel Holtmann }; 1135e23b923SMarcel Holtmann 1145e23b923SMarcel Holtmann MODULE_DEVICE_TABLE(usb, btusb_table); 1155e23b923SMarcel Holtmann 1165e23b923SMarcel Holtmann static struct usb_device_id blacklist_table[] = { 117cfeb4145SMarcel Holtmann /* CSR BlueCore devices */ 118cfeb4145SMarcel Holtmann { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR }, 119cfeb4145SMarcel Holtmann 120cfeb4145SMarcel Holtmann /* Broadcom BCM2033 without firmware */ 121cfeb4145SMarcel Holtmann { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE }, 122cfeb4145SMarcel Holtmann 123be93112aSBala Shanmugam /* Atheros 3011 with sflash firmware */ 124be93112aSBala Shanmugam { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE }, 1252a7bccccSAndy Ross { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE }, 1268e7c3d2eSRicardo Mendoza { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE }, 1276b6ba88bSKeng-Yu Lin { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE }, 128be93112aSBala Shanmugam 129509e7861SCho, Yu-Chen /* Atheros AR9285 Malbec with sflash firmware */ 130509e7861SCho, Yu-Chen { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE }, 131509e7861SCho, Yu-Chen 132d9f51b51SBala Shanmugam /* Atheros 3012 with sflash firmware */ 1332d25f8b4SSteven.Li { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 }, 13407c0ea87SCho, Yu-Chen { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 }, 1359498ba7aSEran { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 }, 13655ed7d4dSAceLan Kao { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 }, 13787522a43SAceLan Kao { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 }, 138*ac71311eSAceLan Kao { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 }, 139d9f51b51SBala Shanmugam 140e9036e33SCho, Yu-Chen /* Atheros AR5BBU12 with sflash firmware */ 141e9036e33SCho, Yu-Chen { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE }, 142e9036e33SCho, Yu-Chen 143cfeb4145SMarcel Holtmann /* Broadcom BCM2035 */ 1447a9d4020SMarcel Holtmann { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU }, 1457a9d4020SMarcel Holtmann { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU }, 1467a9d4020SMarcel Holtmann { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 }, 147cfeb4145SMarcel Holtmann 148cfeb4145SMarcel Holtmann /* Broadcom BCM2045 */ 1497a9d4020SMarcel Holtmann { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU }, 1507a9d4020SMarcel Holtmann { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU }, 151bdbef3d6SMarcel Holtmann 152cfeb4145SMarcel Holtmann /* IBM/Lenovo ThinkPad with Broadcom chip */ 1537a9d4020SMarcel Holtmann { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU }, 1547a9d4020SMarcel Holtmann { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU }, 155cfeb4145SMarcel Holtmann 156cfeb4145SMarcel Holtmann /* HP laptop with Broadcom chip */ 1577a9d4020SMarcel Holtmann { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU }, 158cfeb4145SMarcel Holtmann 159cfeb4145SMarcel Holtmann /* Dell laptop with Broadcom chip */ 1607a9d4020SMarcel Holtmann { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU }, 161cfeb4145SMarcel Holtmann 1625ddd4a60SMarcel Holtmann /* Dell Wireless 370 and 410 devices */ 1637a9d4020SMarcel Holtmann { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU }, 1645ddd4a60SMarcel Holtmann { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU }, 165cfeb4145SMarcel Holtmann 1667a9d4020SMarcel Holtmann /* Belkin F8T012 and F8T013 devices */ 1677a9d4020SMarcel Holtmann { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU }, 1687a9d4020SMarcel Holtmann { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU }, 169cfeb4145SMarcel Holtmann 1705ddd4a60SMarcel Holtmann /* Asus WL-BTD202 device */ 1715ddd4a60SMarcel Holtmann { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU }, 1725ddd4a60SMarcel Holtmann 1735ddd4a60SMarcel Holtmann /* Kensington Bluetooth USB adapter */ 1745ddd4a60SMarcel Holtmann { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU }, 1755ddd4a60SMarcel Holtmann 176cfeb4145SMarcel Holtmann /* RTX Telecom based adapters with buggy SCO support */ 177cfeb4145SMarcel Holtmann { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC }, 178cfeb4145SMarcel Holtmann { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC }, 179cfeb4145SMarcel Holtmann 180cfeb4145SMarcel Holtmann /* CONWISE Technology based adapters with buggy SCO support */ 181cfeb4145SMarcel Holtmann { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC }, 182cfeb4145SMarcel Holtmann 183cfeb4145SMarcel Holtmann /* Digianswer devices */ 184cfeb4145SMarcel Holtmann { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER }, 185cfeb4145SMarcel Holtmann { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE }, 186cfeb4145SMarcel Holtmann 187cfeb4145SMarcel Holtmann /* CSR BlueCore Bluetooth Sniffer */ 188cfeb4145SMarcel Holtmann { USB_DEVICE(0x0a12, 0x0002), .driver_info = BTUSB_SNIFFER }, 189cfeb4145SMarcel Holtmann 190cfeb4145SMarcel Holtmann /* Frontline ComProbe Bluetooth Sniffer */ 191cfeb4145SMarcel Holtmann { USB_DEVICE(0x16d3, 0x0002), .driver_info = BTUSB_SNIFFER }, 192cfeb4145SMarcel Holtmann 1935e23b923SMarcel Holtmann { } /* Terminating entry */ 1945e23b923SMarcel Holtmann }; 1955e23b923SMarcel Holtmann 1969bfa35feSMarcel Holtmann #define BTUSB_MAX_ISOC_FRAMES 10 1979bfa35feSMarcel Holtmann 1985e23b923SMarcel Holtmann #define BTUSB_INTR_RUNNING 0 1995e23b923SMarcel Holtmann #define BTUSB_BULK_RUNNING 1 2009bfa35feSMarcel Holtmann #define BTUSB_ISOC_RUNNING 2 2017bee549eSOliver Neukum #define BTUSB_SUSPENDING 3 20208b8b6c4SGustavo F. Padovan #define BTUSB_DID_ISO_RESUME 4 2035e23b923SMarcel Holtmann 2045e23b923SMarcel Holtmann struct btusb_data { 2055e23b923SMarcel Holtmann struct hci_dev *hdev; 2065e23b923SMarcel Holtmann struct usb_device *udev; 2075fbcd260SMarcel Holtmann struct usb_interface *intf; 2089bfa35feSMarcel Holtmann struct usb_interface *isoc; 2095e23b923SMarcel Holtmann 2105e23b923SMarcel Holtmann spinlock_t lock; 2115e23b923SMarcel Holtmann 2125e23b923SMarcel Holtmann unsigned long flags; 2135e23b923SMarcel Holtmann 2145e23b923SMarcel Holtmann struct work_struct work; 2157bee549eSOliver Neukum struct work_struct waker; 2165e23b923SMarcel Holtmann 2175e23b923SMarcel Holtmann struct usb_anchor tx_anchor; 2185e23b923SMarcel Holtmann struct usb_anchor intr_anchor; 2195e23b923SMarcel Holtmann struct usb_anchor bulk_anchor; 2209bfa35feSMarcel Holtmann struct usb_anchor isoc_anchor; 2217bee549eSOliver Neukum struct usb_anchor deferred; 2227bee549eSOliver Neukum int tx_in_flight; 2237bee549eSOliver Neukum spinlock_t txlock; 2245e23b923SMarcel Holtmann 2255e23b923SMarcel Holtmann struct usb_endpoint_descriptor *intr_ep; 2265e23b923SMarcel Holtmann struct usb_endpoint_descriptor *bulk_tx_ep; 2275e23b923SMarcel Holtmann struct usb_endpoint_descriptor *bulk_rx_ep; 2289bfa35feSMarcel Holtmann struct usb_endpoint_descriptor *isoc_tx_ep; 2299bfa35feSMarcel Holtmann struct usb_endpoint_descriptor *isoc_rx_ep; 2309bfa35feSMarcel Holtmann 2317a9d4020SMarcel Holtmann __u8 cmdreq_type; 2327a9d4020SMarcel Holtmann 23343c2e57fSMarcel Holtmann unsigned int sco_num; 2349bfa35feSMarcel Holtmann int isoc_altsetting; 2356a88adf2SMarcel Holtmann int suspend_count; 2365e23b923SMarcel Holtmann }; 2375e23b923SMarcel Holtmann 2387bee549eSOliver Neukum static int inc_tx(struct btusb_data *data) 2397bee549eSOliver Neukum { 2407bee549eSOliver Neukum unsigned long flags; 2417bee549eSOliver Neukum int rv; 2427bee549eSOliver Neukum 2437bee549eSOliver Neukum spin_lock_irqsave(&data->txlock, flags); 2447bee549eSOliver Neukum rv = test_bit(BTUSB_SUSPENDING, &data->flags); 2457bee549eSOliver Neukum if (!rv) 2467bee549eSOliver Neukum data->tx_in_flight++; 2477bee549eSOliver Neukum spin_unlock_irqrestore(&data->txlock, flags); 2487bee549eSOliver Neukum 2497bee549eSOliver Neukum return rv; 2507bee549eSOliver Neukum } 2517bee549eSOliver Neukum 2525e23b923SMarcel Holtmann static void btusb_intr_complete(struct urb *urb) 2535e23b923SMarcel Holtmann { 2545e23b923SMarcel Holtmann struct hci_dev *hdev = urb->context; 255155961e8SDavid Herrmann struct btusb_data *data = hci_get_drvdata(hdev); 2565e23b923SMarcel Holtmann int err; 2575e23b923SMarcel Holtmann 2585e23b923SMarcel Holtmann BT_DBG("%s urb %p status %d count %d", hdev->name, 2595e23b923SMarcel Holtmann urb, urb->status, urb->actual_length); 2605e23b923SMarcel Holtmann 2615e23b923SMarcel Holtmann if (!test_bit(HCI_RUNNING, &hdev->flags)) 2625e23b923SMarcel Holtmann return; 2635e23b923SMarcel Holtmann 2645e23b923SMarcel Holtmann if (urb->status == 0) { 2659bfa35feSMarcel Holtmann hdev->stat.byte_rx += urb->actual_length; 2669bfa35feSMarcel Holtmann 2675e23b923SMarcel Holtmann if (hci_recv_fragment(hdev, HCI_EVENT_PKT, 2685e23b923SMarcel Holtmann urb->transfer_buffer, 2695e23b923SMarcel Holtmann urb->actual_length) < 0) { 2705e23b923SMarcel Holtmann BT_ERR("%s corrupted event packet", hdev->name); 2715e23b923SMarcel Holtmann hdev->stat.err_rx++; 2725e23b923SMarcel Holtmann } 2735e23b923SMarcel Holtmann } 2745e23b923SMarcel Holtmann 2755e23b923SMarcel Holtmann if (!test_bit(BTUSB_INTR_RUNNING, &data->flags)) 2765e23b923SMarcel Holtmann return; 2775e23b923SMarcel Holtmann 2787bee549eSOliver Neukum usb_mark_last_busy(data->udev); 2795e23b923SMarcel Holtmann usb_anchor_urb(urb, &data->intr_anchor); 2805e23b923SMarcel Holtmann 2815e23b923SMarcel Holtmann err = usb_submit_urb(urb, GFP_ATOMIC); 2825e23b923SMarcel Holtmann if (err < 0) { 2834935f1c1SPaul Bolle /* -EPERM: urb is being killed; 2844935f1c1SPaul Bolle * -ENODEV: device got disconnected */ 2854935f1c1SPaul Bolle if (err != -EPERM && err != -ENODEV) 2865e23b923SMarcel Holtmann BT_ERR("%s urb %p failed to resubmit (%d)", 2875e23b923SMarcel Holtmann hdev->name, urb, -err); 2885e23b923SMarcel Holtmann usb_unanchor_urb(urb); 2895e23b923SMarcel Holtmann } 2905e23b923SMarcel Holtmann } 2915e23b923SMarcel Holtmann 2922eda66f4SMarcel Holtmann static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags) 2935e23b923SMarcel Holtmann { 294155961e8SDavid Herrmann struct btusb_data *data = hci_get_drvdata(hdev); 2955e23b923SMarcel Holtmann struct urb *urb; 2965e23b923SMarcel Holtmann unsigned char *buf; 2975e23b923SMarcel Holtmann unsigned int pipe; 2985e23b923SMarcel Holtmann int err, size; 2995e23b923SMarcel Holtmann 3005e23b923SMarcel Holtmann BT_DBG("%s", hdev->name); 3015e23b923SMarcel Holtmann 3029bfa35feSMarcel Holtmann if (!data->intr_ep) 3039bfa35feSMarcel Holtmann return -ENODEV; 3049bfa35feSMarcel Holtmann 3052eda66f4SMarcel Holtmann urb = usb_alloc_urb(0, mem_flags); 3065e23b923SMarcel Holtmann if (!urb) 3075e23b923SMarcel Holtmann return -ENOMEM; 3085e23b923SMarcel Holtmann 3095e23b923SMarcel Holtmann size = le16_to_cpu(data->intr_ep->wMaxPacketSize); 3105e23b923SMarcel Holtmann 3112eda66f4SMarcel Holtmann buf = kmalloc(size, mem_flags); 3125e23b923SMarcel Holtmann if (!buf) { 3135e23b923SMarcel Holtmann usb_free_urb(urb); 3145e23b923SMarcel Holtmann return -ENOMEM; 3155e23b923SMarcel Holtmann } 3165e23b923SMarcel Holtmann 3175e23b923SMarcel Holtmann pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress); 3185e23b923SMarcel Holtmann 3195e23b923SMarcel Holtmann usb_fill_int_urb(urb, data->udev, pipe, buf, size, 3205e23b923SMarcel Holtmann btusb_intr_complete, hdev, 3215e23b923SMarcel Holtmann data->intr_ep->bInterval); 3225e23b923SMarcel Holtmann 3235e23b923SMarcel Holtmann urb->transfer_flags |= URB_FREE_BUFFER; 3245e23b923SMarcel Holtmann 3255e23b923SMarcel Holtmann usb_anchor_urb(urb, &data->intr_anchor); 3265e23b923SMarcel Holtmann 3272eda66f4SMarcel Holtmann err = usb_submit_urb(urb, mem_flags); 3285e23b923SMarcel Holtmann if (err < 0) { 329d4b8d1c9SPaul Bolle if (err != -EPERM && err != -ENODEV) 3305e23b923SMarcel Holtmann BT_ERR("%s urb %p submission failed (%d)", 3315e23b923SMarcel Holtmann hdev->name, urb, -err); 3325e23b923SMarcel Holtmann usb_unanchor_urb(urb); 3335e23b923SMarcel Holtmann } 3345e23b923SMarcel Holtmann 3355e23b923SMarcel Holtmann usb_free_urb(urb); 3365e23b923SMarcel Holtmann 3375e23b923SMarcel Holtmann return err; 3385e23b923SMarcel Holtmann } 3395e23b923SMarcel Holtmann 3405e23b923SMarcel Holtmann static void btusb_bulk_complete(struct urb *urb) 3415e23b923SMarcel Holtmann { 3425e23b923SMarcel Holtmann struct hci_dev *hdev = urb->context; 343155961e8SDavid Herrmann struct btusb_data *data = hci_get_drvdata(hdev); 3445e23b923SMarcel Holtmann int err; 3455e23b923SMarcel Holtmann 3465e23b923SMarcel Holtmann BT_DBG("%s urb %p status %d count %d", hdev->name, 3475e23b923SMarcel Holtmann urb, urb->status, urb->actual_length); 3485e23b923SMarcel Holtmann 3495e23b923SMarcel Holtmann if (!test_bit(HCI_RUNNING, &hdev->flags)) 3505e23b923SMarcel Holtmann return; 3515e23b923SMarcel Holtmann 3525e23b923SMarcel Holtmann if (urb->status == 0) { 3539bfa35feSMarcel Holtmann hdev->stat.byte_rx += urb->actual_length; 3549bfa35feSMarcel Holtmann 3555e23b923SMarcel Holtmann if (hci_recv_fragment(hdev, HCI_ACLDATA_PKT, 3565e23b923SMarcel Holtmann urb->transfer_buffer, 3575e23b923SMarcel Holtmann urb->actual_length) < 0) { 3585e23b923SMarcel Holtmann BT_ERR("%s corrupted ACL packet", hdev->name); 3595e23b923SMarcel Holtmann hdev->stat.err_rx++; 3605e23b923SMarcel Holtmann } 3615e23b923SMarcel Holtmann } 3625e23b923SMarcel Holtmann 3635e23b923SMarcel Holtmann if (!test_bit(BTUSB_BULK_RUNNING, &data->flags)) 3645e23b923SMarcel Holtmann return; 3655e23b923SMarcel Holtmann 3665e23b923SMarcel Holtmann usb_anchor_urb(urb, &data->bulk_anchor); 367652fd781SOliver Neukum usb_mark_last_busy(data->udev); 3685e23b923SMarcel Holtmann 3695e23b923SMarcel Holtmann err = usb_submit_urb(urb, GFP_ATOMIC); 3705e23b923SMarcel Holtmann if (err < 0) { 3714935f1c1SPaul Bolle /* -EPERM: urb is being killed; 3724935f1c1SPaul Bolle * -ENODEV: device got disconnected */ 3734935f1c1SPaul Bolle if (err != -EPERM && err != -ENODEV) 3745e23b923SMarcel Holtmann BT_ERR("%s urb %p failed to resubmit (%d)", 3755e23b923SMarcel Holtmann hdev->name, urb, -err); 3765e23b923SMarcel Holtmann usb_unanchor_urb(urb); 3775e23b923SMarcel Holtmann } 3785e23b923SMarcel Holtmann } 3795e23b923SMarcel Holtmann 3802eda66f4SMarcel Holtmann static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags) 3815e23b923SMarcel Holtmann { 382155961e8SDavid Herrmann struct btusb_data *data = hci_get_drvdata(hdev); 3835e23b923SMarcel Holtmann struct urb *urb; 3845e23b923SMarcel Holtmann unsigned char *buf; 3855e23b923SMarcel Holtmann unsigned int pipe; 386290ba200SVikram Kandukuri int err, size = HCI_MAX_FRAME_SIZE; 3875e23b923SMarcel Holtmann 3885e23b923SMarcel Holtmann BT_DBG("%s", hdev->name); 3895e23b923SMarcel Holtmann 3909bfa35feSMarcel Holtmann if (!data->bulk_rx_ep) 3919bfa35feSMarcel Holtmann return -ENODEV; 3929bfa35feSMarcel Holtmann 3932eda66f4SMarcel Holtmann urb = usb_alloc_urb(0, mem_flags); 3945e23b923SMarcel Holtmann if (!urb) 3955e23b923SMarcel Holtmann return -ENOMEM; 3965e23b923SMarcel Holtmann 3972eda66f4SMarcel Holtmann buf = kmalloc(size, mem_flags); 3985e23b923SMarcel Holtmann if (!buf) { 3995e23b923SMarcel Holtmann usb_free_urb(urb); 4005e23b923SMarcel Holtmann return -ENOMEM; 4015e23b923SMarcel Holtmann } 4025e23b923SMarcel Holtmann 4035e23b923SMarcel Holtmann pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress); 4045e23b923SMarcel Holtmann 4055e23b923SMarcel Holtmann usb_fill_bulk_urb(urb, data->udev, pipe, 4065e23b923SMarcel Holtmann buf, size, btusb_bulk_complete, hdev); 4075e23b923SMarcel Holtmann 4085e23b923SMarcel Holtmann urb->transfer_flags |= URB_FREE_BUFFER; 4095e23b923SMarcel Holtmann 4107bee549eSOliver Neukum usb_mark_last_busy(data->udev); 4115e23b923SMarcel Holtmann usb_anchor_urb(urb, &data->bulk_anchor); 4125e23b923SMarcel Holtmann 4132eda66f4SMarcel Holtmann err = usb_submit_urb(urb, mem_flags); 4145e23b923SMarcel Holtmann if (err < 0) { 415d4b8d1c9SPaul Bolle if (err != -EPERM && err != -ENODEV) 4165e23b923SMarcel Holtmann BT_ERR("%s urb %p submission failed (%d)", 4175e23b923SMarcel Holtmann hdev->name, urb, -err); 4185e23b923SMarcel Holtmann usb_unanchor_urb(urb); 4195e23b923SMarcel Holtmann } 4205e23b923SMarcel Holtmann 4215e23b923SMarcel Holtmann usb_free_urb(urb); 4225e23b923SMarcel Holtmann 4235e23b923SMarcel Holtmann return err; 4245e23b923SMarcel Holtmann } 4255e23b923SMarcel Holtmann 4269bfa35feSMarcel Holtmann static void btusb_isoc_complete(struct urb *urb) 4279bfa35feSMarcel Holtmann { 4289bfa35feSMarcel Holtmann struct hci_dev *hdev = urb->context; 429155961e8SDavid Herrmann struct btusb_data *data = hci_get_drvdata(hdev); 4309bfa35feSMarcel Holtmann int i, err; 4319bfa35feSMarcel Holtmann 4329bfa35feSMarcel Holtmann BT_DBG("%s urb %p status %d count %d", hdev->name, 4339bfa35feSMarcel Holtmann urb, urb->status, urb->actual_length); 4349bfa35feSMarcel Holtmann 4359bfa35feSMarcel Holtmann if (!test_bit(HCI_RUNNING, &hdev->flags)) 4369bfa35feSMarcel Holtmann return; 4379bfa35feSMarcel Holtmann 4389bfa35feSMarcel Holtmann if (urb->status == 0) { 4399bfa35feSMarcel Holtmann for (i = 0; i < urb->number_of_packets; i++) { 4409bfa35feSMarcel Holtmann unsigned int offset = urb->iso_frame_desc[i].offset; 4419bfa35feSMarcel Holtmann unsigned int length = urb->iso_frame_desc[i].actual_length; 4429bfa35feSMarcel Holtmann 4439bfa35feSMarcel Holtmann if (urb->iso_frame_desc[i].status) 4449bfa35feSMarcel Holtmann continue; 4459bfa35feSMarcel Holtmann 4469bfa35feSMarcel Holtmann hdev->stat.byte_rx += length; 4479bfa35feSMarcel Holtmann 4489bfa35feSMarcel Holtmann if (hci_recv_fragment(hdev, HCI_SCODATA_PKT, 4499bfa35feSMarcel Holtmann urb->transfer_buffer + offset, 4509bfa35feSMarcel Holtmann length) < 0) { 4519bfa35feSMarcel Holtmann BT_ERR("%s corrupted SCO packet", hdev->name); 4529bfa35feSMarcel Holtmann hdev->stat.err_rx++; 4539bfa35feSMarcel Holtmann } 4549bfa35feSMarcel Holtmann } 4559bfa35feSMarcel Holtmann } 4569bfa35feSMarcel Holtmann 4579bfa35feSMarcel Holtmann if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags)) 4589bfa35feSMarcel Holtmann return; 4599bfa35feSMarcel Holtmann 4609bfa35feSMarcel Holtmann usb_anchor_urb(urb, &data->isoc_anchor); 4619bfa35feSMarcel Holtmann 4629bfa35feSMarcel Holtmann err = usb_submit_urb(urb, GFP_ATOMIC); 4639bfa35feSMarcel Holtmann if (err < 0) { 4644935f1c1SPaul Bolle /* -EPERM: urb is being killed; 4654935f1c1SPaul Bolle * -ENODEV: device got disconnected */ 4664935f1c1SPaul Bolle if (err != -EPERM && err != -ENODEV) 4679bfa35feSMarcel Holtmann BT_ERR("%s urb %p failed to resubmit (%d)", 4689bfa35feSMarcel Holtmann hdev->name, urb, -err); 4699bfa35feSMarcel Holtmann usb_unanchor_urb(urb); 4709bfa35feSMarcel Holtmann } 4719bfa35feSMarcel Holtmann } 4729bfa35feSMarcel Holtmann 47342b16b3fSJesper Juhl static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu) 4749bfa35feSMarcel Holtmann { 4759bfa35feSMarcel Holtmann int i, offset = 0; 4769bfa35feSMarcel Holtmann 4779bfa35feSMarcel Holtmann BT_DBG("len %d mtu %d", len, mtu); 4789bfa35feSMarcel Holtmann 4799bfa35feSMarcel Holtmann for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu; 4809bfa35feSMarcel Holtmann i++, offset += mtu, len -= mtu) { 4819bfa35feSMarcel Holtmann urb->iso_frame_desc[i].offset = offset; 4829bfa35feSMarcel Holtmann urb->iso_frame_desc[i].length = mtu; 4839bfa35feSMarcel Holtmann } 4849bfa35feSMarcel Holtmann 4859bfa35feSMarcel Holtmann if (len && i < BTUSB_MAX_ISOC_FRAMES) { 4869bfa35feSMarcel Holtmann urb->iso_frame_desc[i].offset = offset; 4879bfa35feSMarcel Holtmann urb->iso_frame_desc[i].length = len; 4889bfa35feSMarcel Holtmann i++; 4899bfa35feSMarcel Holtmann } 4909bfa35feSMarcel Holtmann 4919bfa35feSMarcel Holtmann urb->number_of_packets = i; 4929bfa35feSMarcel Holtmann } 4939bfa35feSMarcel Holtmann 4942eda66f4SMarcel Holtmann static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags) 4959bfa35feSMarcel Holtmann { 496155961e8SDavid Herrmann struct btusb_data *data = hci_get_drvdata(hdev); 4979bfa35feSMarcel Holtmann struct urb *urb; 4989bfa35feSMarcel Holtmann unsigned char *buf; 4999bfa35feSMarcel Holtmann unsigned int pipe; 5009bfa35feSMarcel Holtmann int err, size; 5019bfa35feSMarcel Holtmann 5029bfa35feSMarcel Holtmann BT_DBG("%s", hdev->name); 5039bfa35feSMarcel Holtmann 5049bfa35feSMarcel Holtmann if (!data->isoc_rx_ep) 5059bfa35feSMarcel Holtmann return -ENODEV; 5069bfa35feSMarcel Holtmann 5072eda66f4SMarcel Holtmann urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags); 5089bfa35feSMarcel Holtmann if (!urb) 5099bfa35feSMarcel Holtmann return -ENOMEM; 5109bfa35feSMarcel Holtmann 5119bfa35feSMarcel Holtmann size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) * 5129bfa35feSMarcel Holtmann BTUSB_MAX_ISOC_FRAMES; 5139bfa35feSMarcel Holtmann 5142eda66f4SMarcel Holtmann buf = kmalloc(size, mem_flags); 5159bfa35feSMarcel Holtmann if (!buf) { 5169bfa35feSMarcel Holtmann usb_free_urb(urb); 5179bfa35feSMarcel Holtmann return -ENOMEM; 5189bfa35feSMarcel Holtmann } 5199bfa35feSMarcel Holtmann 5209bfa35feSMarcel Holtmann pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress); 5219bfa35feSMarcel Holtmann 522fa0fb93fSBing Zhao usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete, 523fa0fb93fSBing Zhao hdev, data->isoc_rx_ep->bInterval); 5249bfa35feSMarcel Holtmann 5259bfa35feSMarcel Holtmann urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP; 5269bfa35feSMarcel Holtmann 5279bfa35feSMarcel Holtmann __fill_isoc_descriptor(urb, size, 5289bfa35feSMarcel Holtmann le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize)); 5299bfa35feSMarcel Holtmann 5309bfa35feSMarcel Holtmann usb_anchor_urb(urb, &data->isoc_anchor); 5319bfa35feSMarcel Holtmann 5322eda66f4SMarcel Holtmann err = usb_submit_urb(urb, mem_flags); 5339bfa35feSMarcel Holtmann if (err < 0) { 534d4b8d1c9SPaul Bolle if (err != -EPERM && err != -ENODEV) 5359bfa35feSMarcel Holtmann BT_ERR("%s urb %p submission failed (%d)", 5369bfa35feSMarcel Holtmann hdev->name, urb, -err); 5379bfa35feSMarcel Holtmann usb_unanchor_urb(urb); 5389bfa35feSMarcel Holtmann } 5399bfa35feSMarcel Holtmann 5409bfa35feSMarcel Holtmann usb_free_urb(urb); 5419bfa35feSMarcel Holtmann 5429bfa35feSMarcel Holtmann return err; 5439bfa35feSMarcel Holtmann } 5449bfa35feSMarcel Holtmann 5455e23b923SMarcel Holtmann static void btusb_tx_complete(struct urb *urb) 5465e23b923SMarcel Holtmann { 5475e23b923SMarcel Holtmann struct sk_buff *skb = urb->context; 5485e23b923SMarcel Holtmann struct hci_dev *hdev = (struct hci_dev *) skb->dev; 549155961e8SDavid Herrmann struct btusb_data *data = hci_get_drvdata(hdev); 5507bee549eSOliver Neukum 5517bee549eSOliver Neukum BT_DBG("%s urb %p status %d count %d", hdev->name, 5527bee549eSOliver Neukum urb, urb->status, urb->actual_length); 5537bee549eSOliver Neukum 5547bee549eSOliver Neukum if (!test_bit(HCI_RUNNING, &hdev->flags)) 5557bee549eSOliver Neukum goto done; 5567bee549eSOliver Neukum 5577bee549eSOliver Neukum if (!urb->status) 5587bee549eSOliver Neukum hdev->stat.byte_tx += urb->transfer_buffer_length; 5597bee549eSOliver Neukum else 5607bee549eSOliver Neukum hdev->stat.err_tx++; 5617bee549eSOliver Neukum 5627bee549eSOliver Neukum done: 5637bee549eSOliver Neukum spin_lock(&data->txlock); 5647bee549eSOliver Neukum data->tx_in_flight--; 5657bee549eSOliver Neukum spin_unlock(&data->txlock); 5667bee549eSOliver Neukum 5677bee549eSOliver Neukum kfree(urb->setup_packet); 5687bee549eSOliver Neukum 5697bee549eSOliver Neukum kfree_skb(skb); 5707bee549eSOliver Neukum } 5717bee549eSOliver Neukum 5727bee549eSOliver Neukum static void btusb_isoc_tx_complete(struct urb *urb) 5737bee549eSOliver Neukum { 5747bee549eSOliver Neukum struct sk_buff *skb = urb->context; 5757bee549eSOliver Neukum struct hci_dev *hdev = (struct hci_dev *) skb->dev; 5765e23b923SMarcel Holtmann 5775e23b923SMarcel Holtmann BT_DBG("%s urb %p status %d count %d", hdev->name, 5785e23b923SMarcel Holtmann urb, urb->status, urb->actual_length); 5795e23b923SMarcel Holtmann 5805e23b923SMarcel Holtmann if (!test_bit(HCI_RUNNING, &hdev->flags)) 5815e23b923SMarcel Holtmann goto done; 5825e23b923SMarcel Holtmann 5835e23b923SMarcel Holtmann if (!urb->status) 5845e23b923SMarcel Holtmann hdev->stat.byte_tx += urb->transfer_buffer_length; 5855e23b923SMarcel Holtmann else 5865e23b923SMarcel Holtmann hdev->stat.err_tx++; 5875e23b923SMarcel Holtmann 5885e23b923SMarcel Holtmann done: 5895e23b923SMarcel Holtmann kfree(urb->setup_packet); 5905e23b923SMarcel Holtmann 5915e23b923SMarcel Holtmann kfree_skb(skb); 5925e23b923SMarcel Holtmann } 5935e23b923SMarcel Holtmann 5945e23b923SMarcel Holtmann static int btusb_open(struct hci_dev *hdev) 5955e23b923SMarcel Holtmann { 596155961e8SDavid Herrmann struct btusb_data *data = hci_get_drvdata(hdev); 5975e23b923SMarcel Holtmann int err; 5985e23b923SMarcel Holtmann 5995e23b923SMarcel Holtmann BT_DBG("%s", hdev->name); 6005e23b923SMarcel Holtmann 6017bee549eSOliver Neukum err = usb_autopm_get_interface(data->intf); 6027bee549eSOliver Neukum if (err < 0) 6037bee549eSOliver Neukum return err; 6047bee549eSOliver Neukum 6057bee549eSOliver Neukum data->intf->needs_remote_wakeup = 1; 6067bee549eSOliver Neukum 6075e23b923SMarcel Holtmann if (test_and_set_bit(HCI_RUNNING, &hdev->flags)) 6087bee549eSOliver Neukum goto done; 6095e23b923SMarcel Holtmann 6105e23b923SMarcel Holtmann if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags)) 6117bee549eSOliver Neukum goto done; 6125e23b923SMarcel Holtmann 6132eda66f4SMarcel Holtmann err = btusb_submit_intr_urb(hdev, GFP_KERNEL); 61443c2e57fSMarcel Holtmann if (err < 0) 61543c2e57fSMarcel Holtmann goto failed; 61643c2e57fSMarcel Holtmann 61743c2e57fSMarcel Holtmann err = btusb_submit_bulk_urb(hdev, GFP_KERNEL); 6185e23b923SMarcel Holtmann if (err < 0) { 61943c2e57fSMarcel Holtmann usb_kill_anchored_urbs(&data->intr_anchor); 62043c2e57fSMarcel Holtmann goto failed; 6215e23b923SMarcel Holtmann } 6225e23b923SMarcel Holtmann 62343c2e57fSMarcel Holtmann set_bit(BTUSB_BULK_RUNNING, &data->flags); 62443c2e57fSMarcel Holtmann btusb_submit_bulk_urb(hdev, GFP_KERNEL); 62543c2e57fSMarcel Holtmann 6267bee549eSOliver Neukum done: 6277bee549eSOliver Neukum usb_autopm_put_interface(data->intf); 62843c2e57fSMarcel Holtmann return 0; 62943c2e57fSMarcel Holtmann 63043c2e57fSMarcel Holtmann failed: 63143c2e57fSMarcel Holtmann clear_bit(BTUSB_INTR_RUNNING, &data->flags); 63243c2e57fSMarcel Holtmann clear_bit(HCI_RUNNING, &hdev->flags); 6337bee549eSOliver Neukum usb_autopm_put_interface(data->intf); 6345e23b923SMarcel Holtmann return err; 6355e23b923SMarcel Holtmann } 6365e23b923SMarcel Holtmann 6377bee549eSOliver Neukum static void btusb_stop_traffic(struct btusb_data *data) 6387bee549eSOliver Neukum { 6397bee549eSOliver Neukum usb_kill_anchored_urbs(&data->intr_anchor); 6407bee549eSOliver Neukum usb_kill_anchored_urbs(&data->bulk_anchor); 6417bee549eSOliver Neukum usb_kill_anchored_urbs(&data->isoc_anchor); 6427bee549eSOliver Neukum } 6437bee549eSOliver Neukum 6445e23b923SMarcel Holtmann static int btusb_close(struct hci_dev *hdev) 6455e23b923SMarcel Holtmann { 646155961e8SDavid Herrmann struct btusb_data *data = hci_get_drvdata(hdev); 6477bee549eSOliver Neukum int err; 6485e23b923SMarcel Holtmann 6495e23b923SMarcel Holtmann BT_DBG("%s", hdev->name); 6505e23b923SMarcel Holtmann 6515e23b923SMarcel Holtmann if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags)) 6525e23b923SMarcel Holtmann return 0; 6535e23b923SMarcel Holtmann 654e8c3c3d2SMarcel Holtmann cancel_work_sync(&data->work); 655404291acSLinus Torvalds cancel_work_sync(&data->waker); 656e8c3c3d2SMarcel Holtmann 6579bfa35feSMarcel Holtmann clear_bit(BTUSB_ISOC_RUNNING, &data->flags); 6585e23b923SMarcel Holtmann clear_bit(BTUSB_BULK_RUNNING, &data->flags); 6595e23b923SMarcel Holtmann clear_bit(BTUSB_INTR_RUNNING, &data->flags); 6607bee549eSOliver Neukum 6617bee549eSOliver Neukum btusb_stop_traffic(data); 6627bee549eSOliver Neukum err = usb_autopm_get_interface(data->intf); 6637bee549eSOliver Neukum if (err < 0) 6647b8e2c1dSOliver Neukum goto failed; 6657bee549eSOliver Neukum 6667bee549eSOliver Neukum data->intf->needs_remote_wakeup = 0; 6677bee549eSOliver Neukum usb_autopm_put_interface(data->intf); 6685e23b923SMarcel Holtmann 6697b8e2c1dSOliver Neukum failed: 6707b8e2c1dSOliver Neukum usb_scuttle_anchored_urbs(&data->deferred); 6715e23b923SMarcel Holtmann return 0; 6725e23b923SMarcel Holtmann } 6735e23b923SMarcel Holtmann 6745e23b923SMarcel Holtmann static int btusb_flush(struct hci_dev *hdev) 6755e23b923SMarcel Holtmann { 676155961e8SDavid Herrmann struct btusb_data *data = hci_get_drvdata(hdev); 6775e23b923SMarcel Holtmann 6785e23b923SMarcel Holtmann BT_DBG("%s", hdev->name); 6795e23b923SMarcel Holtmann 6805e23b923SMarcel Holtmann usb_kill_anchored_urbs(&data->tx_anchor); 6815e23b923SMarcel Holtmann 6825e23b923SMarcel Holtmann return 0; 6835e23b923SMarcel Holtmann } 6845e23b923SMarcel Holtmann 6855e23b923SMarcel Holtmann static int btusb_send_frame(struct sk_buff *skb) 6865e23b923SMarcel Holtmann { 6875e23b923SMarcel Holtmann struct hci_dev *hdev = (struct hci_dev *) skb->dev; 688155961e8SDavid Herrmann struct btusb_data *data = hci_get_drvdata(hdev); 6895e23b923SMarcel Holtmann struct usb_ctrlrequest *dr; 6905e23b923SMarcel Holtmann struct urb *urb; 6915e23b923SMarcel Holtmann unsigned int pipe; 6925e23b923SMarcel Holtmann int err; 6935e23b923SMarcel Holtmann 6945e23b923SMarcel Holtmann BT_DBG("%s", hdev->name); 6955e23b923SMarcel Holtmann 6965e23b923SMarcel Holtmann if (!test_bit(HCI_RUNNING, &hdev->flags)) 6975e23b923SMarcel Holtmann return -EBUSY; 6985e23b923SMarcel Holtmann 6995e23b923SMarcel Holtmann switch (bt_cb(skb)->pkt_type) { 7005e23b923SMarcel Holtmann case HCI_COMMAND_PKT: 7015e23b923SMarcel Holtmann urb = usb_alloc_urb(0, GFP_ATOMIC); 7025e23b923SMarcel Holtmann if (!urb) 7035e23b923SMarcel Holtmann return -ENOMEM; 7045e23b923SMarcel Holtmann 7055e23b923SMarcel Holtmann dr = kmalloc(sizeof(*dr), GFP_ATOMIC); 7065e23b923SMarcel Holtmann if (!dr) { 7075e23b923SMarcel Holtmann usb_free_urb(urb); 7085e23b923SMarcel Holtmann return -ENOMEM; 7095e23b923SMarcel Holtmann } 7105e23b923SMarcel Holtmann 7117a9d4020SMarcel Holtmann dr->bRequestType = data->cmdreq_type; 7125e23b923SMarcel Holtmann dr->bRequest = 0; 7135e23b923SMarcel Holtmann dr->wIndex = 0; 7145e23b923SMarcel Holtmann dr->wValue = 0; 7155e23b923SMarcel Holtmann dr->wLength = __cpu_to_le16(skb->len); 7165e23b923SMarcel Holtmann 7175e23b923SMarcel Holtmann pipe = usb_sndctrlpipe(data->udev, 0x00); 7185e23b923SMarcel Holtmann 7195e23b923SMarcel Holtmann usb_fill_control_urb(urb, data->udev, pipe, (void *) dr, 7205e23b923SMarcel Holtmann skb->data, skb->len, btusb_tx_complete, skb); 7215e23b923SMarcel Holtmann 7225e23b923SMarcel Holtmann hdev->stat.cmd_tx++; 7235e23b923SMarcel Holtmann break; 7245e23b923SMarcel Holtmann 7255e23b923SMarcel Holtmann case HCI_ACLDATA_PKT: 7269fd481e0SPeter Hurley if (!data->bulk_tx_ep) 7279bfa35feSMarcel Holtmann return -ENODEV; 7289bfa35feSMarcel Holtmann 7295e23b923SMarcel Holtmann urb = usb_alloc_urb(0, GFP_ATOMIC); 7305e23b923SMarcel Holtmann if (!urb) 7315e23b923SMarcel Holtmann return -ENOMEM; 7325e23b923SMarcel Holtmann 7335e23b923SMarcel Holtmann pipe = usb_sndbulkpipe(data->udev, 7345e23b923SMarcel Holtmann data->bulk_tx_ep->bEndpointAddress); 7355e23b923SMarcel Holtmann 7365e23b923SMarcel Holtmann usb_fill_bulk_urb(urb, data->udev, pipe, 7375e23b923SMarcel Holtmann skb->data, skb->len, btusb_tx_complete, skb); 7385e23b923SMarcel Holtmann 7395e23b923SMarcel Holtmann hdev->stat.acl_tx++; 7405e23b923SMarcel Holtmann break; 7415e23b923SMarcel Holtmann 7425e23b923SMarcel Holtmann case HCI_SCODATA_PKT: 7439bfa35feSMarcel Holtmann if (!data->isoc_tx_ep || hdev->conn_hash.sco_num < 1) 7449bfa35feSMarcel Holtmann return -ENODEV; 7459bfa35feSMarcel Holtmann 7469bfa35feSMarcel Holtmann urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_ATOMIC); 7479bfa35feSMarcel Holtmann if (!urb) 7489bfa35feSMarcel Holtmann return -ENOMEM; 7499bfa35feSMarcel Holtmann 7509bfa35feSMarcel Holtmann pipe = usb_sndisocpipe(data->udev, 7519bfa35feSMarcel Holtmann data->isoc_tx_ep->bEndpointAddress); 7529bfa35feSMarcel Holtmann 75303c2d0e8SGustavo F. Padovan usb_fill_int_urb(urb, data->udev, pipe, 75403c2d0e8SGustavo F. Padovan skb->data, skb->len, btusb_isoc_tx_complete, 75503c2d0e8SGustavo F. Padovan skb, data->isoc_tx_ep->bInterval); 7569bfa35feSMarcel Holtmann 7579bfa35feSMarcel Holtmann urb->transfer_flags = URB_ISO_ASAP; 7589bfa35feSMarcel Holtmann 7599bfa35feSMarcel Holtmann __fill_isoc_descriptor(urb, skb->len, 7609bfa35feSMarcel Holtmann le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize)); 7619bfa35feSMarcel Holtmann 7625e23b923SMarcel Holtmann hdev->stat.sco_tx++; 7637bee549eSOliver Neukum goto skip_waking; 7645e23b923SMarcel Holtmann 7655e23b923SMarcel Holtmann default: 7665e23b923SMarcel Holtmann return -EILSEQ; 7675e23b923SMarcel Holtmann } 7685e23b923SMarcel Holtmann 7697bee549eSOliver Neukum err = inc_tx(data); 7707bee549eSOliver Neukum if (err) { 7717bee549eSOliver Neukum usb_anchor_urb(urb, &data->deferred); 7727bee549eSOliver Neukum schedule_work(&data->waker); 7737bee549eSOliver Neukum err = 0; 7747bee549eSOliver Neukum goto done; 7757bee549eSOliver Neukum } 7767bee549eSOliver Neukum 7777bee549eSOliver Neukum skip_waking: 7785e23b923SMarcel Holtmann usb_anchor_urb(urb, &data->tx_anchor); 7795e23b923SMarcel Holtmann 7805e23b923SMarcel Holtmann err = usb_submit_urb(urb, GFP_ATOMIC); 7815e23b923SMarcel Holtmann if (err < 0) { 7825a9b80e2SPaul Bolle if (err != -EPERM && err != -ENODEV) 7835a9b80e2SPaul Bolle BT_ERR("%s urb %p submission failed (%d)", 7845a9b80e2SPaul Bolle hdev->name, urb, -err); 7855e23b923SMarcel Holtmann kfree(urb->setup_packet); 7865e23b923SMarcel Holtmann usb_unanchor_urb(urb); 7877bee549eSOliver Neukum } else { 7887bee549eSOliver Neukum usb_mark_last_busy(data->udev); 7895e23b923SMarcel Holtmann } 7905e23b923SMarcel Holtmann 7917bee549eSOliver Neukum done: 79254a8a79cSCong Wang usb_free_urb(urb); 7935e23b923SMarcel Holtmann return err; 7945e23b923SMarcel Holtmann } 7955e23b923SMarcel Holtmann 7965e23b923SMarcel Holtmann static void btusb_notify(struct hci_dev *hdev, unsigned int evt) 7975e23b923SMarcel Holtmann { 798155961e8SDavid Herrmann struct btusb_data *data = hci_get_drvdata(hdev); 7995e23b923SMarcel Holtmann 8005e23b923SMarcel Holtmann BT_DBG("%s evt %d", hdev->name, evt); 8015e23b923SMarcel Holtmann 80243c2e57fSMarcel Holtmann if (hdev->conn_hash.sco_num != data->sco_num) { 80343c2e57fSMarcel Holtmann data->sco_num = hdev->conn_hash.sco_num; 8045e23b923SMarcel Holtmann schedule_work(&data->work); 8055e23b923SMarcel Holtmann } 80643c2e57fSMarcel Holtmann } 8075e23b923SMarcel Holtmann 80842b16b3fSJesper Juhl static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting) 8099bfa35feSMarcel Holtmann { 810155961e8SDavid Herrmann struct btusb_data *data = hci_get_drvdata(hdev); 8119bfa35feSMarcel Holtmann struct usb_interface *intf = data->isoc; 8129bfa35feSMarcel Holtmann struct usb_endpoint_descriptor *ep_desc; 8139bfa35feSMarcel Holtmann int i, err; 8149bfa35feSMarcel Holtmann 8159bfa35feSMarcel Holtmann if (!data->isoc) 8169bfa35feSMarcel Holtmann return -ENODEV; 8179bfa35feSMarcel Holtmann 8189bfa35feSMarcel Holtmann err = usb_set_interface(data->udev, 1, altsetting); 8199bfa35feSMarcel Holtmann if (err < 0) { 8209bfa35feSMarcel Holtmann BT_ERR("%s setting interface failed (%d)", hdev->name, -err); 8219bfa35feSMarcel Holtmann return err; 8229bfa35feSMarcel Holtmann } 8239bfa35feSMarcel Holtmann 8249bfa35feSMarcel Holtmann data->isoc_altsetting = altsetting; 8259bfa35feSMarcel Holtmann 8269bfa35feSMarcel Holtmann data->isoc_tx_ep = NULL; 8279bfa35feSMarcel Holtmann data->isoc_rx_ep = NULL; 8289bfa35feSMarcel Holtmann 8299bfa35feSMarcel Holtmann for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) { 8309bfa35feSMarcel Holtmann ep_desc = &intf->cur_altsetting->endpoint[i].desc; 8319bfa35feSMarcel Holtmann 8329bfa35feSMarcel Holtmann if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) { 8339bfa35feSMarcel Holtmann data->isoc_tx_ep = ep_desc; 8349bfa35feSMarcel Holtmann continue; 8359bfa35feSMarcel Holtmann } 8369bfa35feSMarcel Holtmann 8379bfa35feSMarcel Holtmann if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) { 8389bfa35feSMarcel Holtmann data->isoc_rx_ep = ep_desc; 8399bfa35feSMarcel Holtmann continue; 8409bfa35feSMarcel Holtmann } 8419bfa35feSMarcel Holtmann } 8429bfa35feSMarcel Holtmann 8439bfa35feSMarcel Holtmann if (!data->isoc_tx_ep || !data->isoc_rx_ep) { 8449bfa35feSMarcel Holtmann BT_ERR("%s invalid SCO descriptors", hdev->name); 8459bfa35feSMarcel Holtmann return -ENODEV; 8469bfa35feSMarcel Holtmann } 8479bfa35feSMarcel Holtmann 8489bfa35feSMarcel Holtmann return 0; 8499bfa35feSMarcel Holtmann } 8509bfa35feSMarcel Holtmann 8515e23b923SMarcel Holtmann static void btusb_work(struct work_struct *work) 8525e23b923SMarcel Holtmann { 8535e23b923SMarcel Holtmann struct btusb_data *data = container_of(work, struct btusb_data, work); 8545e23b923SMarcel Holtmann struct hci_dev *hdev = data->hdev; 8557bee549eSOliver Neukum int err; 8565e23b923SMarcel Holtmann 8579bfa35feSMarcel Holtmann if (hdev->conn_hash.sco_num > 0) { 85808b8b6c4SGustavo F. Padovan if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) { 8598efdd0cdSOliver Neukum err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf); 8607bee549eSOliver Neukum if (err < 0) { 8617bee549eSOliver Neukum clear_bit(BTUSB_ISOC_RUNNING, &data->flags); 8627bee549eSOliver Neukum usb_kill_anchored_urbs(&data->isoc_anchor); 8637bee549eSOliver Neukum return; 8647bee549eSOliver Neukum } 8657bee549eSOliver Neukum 86608b8b6c4SGustavo F. Padovan set_bit(BTUSB_DID_ISO_RESUME, &data->flags); 8677bee549eSOliver Neukum } 8689bfa35feSMarcel Holtmann if (data->isoc_altsetting != 2) { 8699bfa35feSMarcel Holtmann clear_bit(BTUSB_ISOC_RUNNING, &data->flags); 8709bfa35feSMarcel Holtmann usb_kill_anchored_urbs(&data->isoc_anchor); 8719bfa35feSMarcel Holtmann 8729bfa35feSMarcel Holtmann if (__set_isoc_interface(hdev, 2) < 0) 8739bfa35feSMarcel Holtmann return; 8749bfa35feSMarcel Holtmann } 8759bfa35feSMarcel Holtmann 8769bfa35feSMarcel Holtmann if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) { 8772eda66f4SMarcel Holtmann if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0) 8789bfa35feSMarcel Holtmann clear_bit(BTUSB_ISOC_RUNNING, &data->flags); 8799bfa35feSMarcel Holtmann else 8802eda66f4SMarcel Holtmann btusb_submit_isoc_urb(hdev, GFP_KERNEL); 8819bfa35feSMarcel Holtmann } 8829bfa35feSMarcel Holtmann } else { 8839bfa35feSMarcel Holtmann clear_bit(BTUSB_ISOC_RUNNING, &data->flags); 8849bfa35feSMarcel Holtmann usb_kill_anchored_urbs(&data->isoc_anchor); 8859bfa35feSMarcel Holtmann 8869bfa35feSMarcel Holtmann __set_isoc_interface(hdev, 0); 88708b8b6c4SGustavo F. Padovan if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags)) 8888efdd0cdSOliver Neukum usb_autopm_put_interface(data->isoc ? data->isoc : data->intf); 8899bfa35feSMarcel Holtmann } 8905e23b923SMarcel Holtmann } 8917bee549eSOliver Neukum 8927bee549eSOliver Neukum static void btusb_waker(struct work_struct *work) 8937bee549eSOliver Neukum { 8947bee549eSOliver Neukum struct btusb_data *data = container_of(work, struct btusb_data, waker); 8957bee549eSOliver Neukum int err; 8967bee549eSOliver Neukum 8977bee549eSOliver Neukum err = usb_autopm_get_interface(data->intf); 8987bee549eSOliver Neukum if (err < 0) 8997bee549eSOliver Neukum return; 9007bee549eSOliver Neukum 9017bee549eSOliver Neukum usb_autopm_put_interface(data->intf); 9027bee549eSOliver Neukum } 9035e23b923SMarcel Holtmann 9045e23b923SMarcel Holtmann static int btusb_probe(struct usb_interface *intf, 9055e23b923SMarcel Holtmann const struct usb_device_id *id) 9065e23b923SMarcel Holtmann { 9075e23b923SMarcel Holtmann struct usb_endpoint_descriptor *ep_desc; 9085e23b923SMarcel Holtmann struct btusb_data *data; 9095e23b923SMarcel Holtmann struct hci_dev *hdev; 9105e23b923SMarcel Holtmann int i, err; 9115e23b923SMarcel Holtmann 9125e23b923SMarcel Holtmann BT_DBG("intf %p id %p", intf, id); 9135e23b923SMarcel Holtmann 914cfeb4145SMarcel Holtmann /* interface numbers are hardcoded in the spec */ 9155e23b923SMarcel Holtmann if (intf->cur_altsetting->desc.bInterfaceNumber != 0) 9165e23b923SMarcel Holtmann return -ENODEV; 9175e23b923SMarcel Holtmann 9185e23b923SMarcel Holtmann if (!id->driver_info) { 9195e23b923SMarcel Holtmann const struct usb_device_id *match; 9205e23b923SMarcel Holtmann match = usb_match_id(intf, blacklist_table); 9215e23b923SMarcel Holtmann if (match) 9225e23b923SMarcel Holtmann id = match; 9235e23b923SMarcel Holtmann } 9245e23b923SMarcel Holtmann 925cfeb4145SMarcel Holtmann if (id->driver_info == BTUSB_IGNORE) 926cfeb4145SMarcel Holtmann return -ENODEV; 927cfeb4145SMarcel Holtmann 928cfeb4145SMarcel Holtmann if (ignore_dga && id->driver_info & BTUSB_DIGIANSWER) 929cfeb4145SMarcel Holtmann return -ENODEV; 930cfeb4145SMarcel Holtmann 931cfeb4145SMarcel Holtmann if (ignore_csr && id->driver_info & BTUSB_CSR) 932cfeb4145SMarcel Holtmann return -ENODEV; 933cfeb4145SMarcel Holtmann 934cfeb4145SMarcel Holtmann if (ignore_sniffer && id->driver_info & BTUSB_SNIFFER) 935cfeb4145SMarcel Holtmann return -ENODEV; 936cfeb4145SMarcel Holtmann 9372d25f8b4SSteven.Li if (id->driver_info & BTUSB_ATH3012) { 9382d25f8b4SSteven.Li struct usb_device *udev = interface_to_usbdev(intf); 9392d25f8b4SSteven.Li 9402d25f8b4SSteven.Li /* Old firmware would otherwise let ath3k driver load 9412d25f8b4SSteven.Li * patch and sysconfig files */ 9422d25f8b4SSteven.Li if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001) 9432d25f8b4SSteven.Li return -ENODEV; 9442d25f8b4SSteven.Li } 9452d25f8b4SSteven.Li 9465e23b923SMarcel Holtmann data = kzalloc(sizeof(*data), GFP_KERNEL); 9475e23b923SMarcel Holtmann if (!data) 9485e23b923SMarcel Holtmann return -ENOMEM; 9495e23b923SMarcel Holtmann 9505e23b923SMarcel Holtmann for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) { 9515e23b923SMarcel Holtmann ep_desc = &intf->cur_altsetting->endpoint[i].desc; 9525e23b923SMarcel Holtmann 9535e23b923SMarcel Holtmann if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) { 9545e23b923SMarcel Holtmann data->intr_ep = ep_desc; 9555e23b923SMarcel Holtmann continue; 9565e23b923SMarcel Holtmann } 9575e23b923SMarcel Holtmann 9585e23b923SMarcel Holtmann if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) { 9595e23b923SMarcel Holtmann data->bulk_tx_ep = ep_desc; 9605e23b923SMarcel Holtmann continue; 9615e23b923SMarcel Holtmann } 9625e23b923SMarcel Holtmann 9635e23b923SMarcel Holtmann if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) { 9645e23b923SMarcel Holtmann data->bulk_rx_ep = ep_desc; 9655e23b923SMarcel Holtmann continue; 9665e23b923SMarcel Holtmann } 9675e23b923SMarcel Holtmann } 9685e23b923SMarcel Holtmann 9695e23b923SMarcel Holtmann if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep) { 9705e23b923SMarcel Holtmann kfree(data); 9715e23b923SMarcel Holtmann return -ENODEV; 9725e23b923SMarcel Holtmann } 9735e23b923SMarcel Holtmann 9747a9d4020SMarcel Holtmann data->cmdreq_type = USB_TYPE_CLASS; 9757a9d4020SMarcel Holtmann 9765e23b923SMarcel Holtmann data->udev = interface_to_usbdev(intf); 9775fbcd260SMarcel Holtmann data->intf = intf; 9785e23b923SMarcel Holtmann 9795e23b923SMarcel Holtmann spin_lock_init(&data->lock); 9805e23b923SMarcel Holtmann 9815e23b923SMarcel Holtmann INIT_WORK(&data->work, btusb_work); 9827bee549eSOliver Neukum INIT_WORK(&data->waker, btusb_waker); 9837bee549eSOliver Neukum spin_lock_init(&data->txlock); 9845e23b923SMarcel Holtmann 9855e23b923SMarcel Holtmann init_usb_anchor(&data->tx_anchor); 9865e23b923SMarcel Holtmann init_usb_anchor(&data->intr_anchor); 9875e23b923SMarcel Holtmann init_usb_anchor(&data->bulk_anchor); 9889bfa35feSMarcel Holtmann init_usb_anchor(&data->isoc_anchor); 9897bee549eSOliver Neukum init_usb_anchor(&data->deferred); 9905e23b923SMarcel Holtmann 9915e23b923SMarcel Holtmann hdev = hci_alloc_dev(); 9925e23b923SMarcel Holtmann if (!hdev) { 9935e23b923SMarcel Holtmann kfree(data); 9945e23b923SMarcel Holtmann return -ENOMEM; 9955e23b923SMarcel Holtmann } 9965e23b923SMarcel Holtmann 997c13854ceSMarcel Holtmann hdev->bus = HCI_USB; 998155961e8SDavid Herrmann hci_set_drvdata(hdev, data); 9995e23b923SMarcel Holtmann 10005e23b923SMarcel Holtmann data->hdev = hdev; 10015e23b923SMarcel Holtmann 10025e23b923SMarcel Holtmann SET_HCIDEV_DEV(hdev, &intf->dev); 10035e23b923SMarcel Holtmann 10045e23b923SMarcel Holtmann hdev->open = btusb_open; 10055e23b923SMarcel Holtmann hdev->close = btusb_close; 10065e23b923SMarcel Holtmann hdev->flush = btusb_flush; 10075e23b923SMarcel Holtmann hdev->send = btusb_send_frame; 10085e23b923SMarcel Holtmann hdev->notify = btusb_notify; 10095e23b923SMarcel Holtmann 10107a9d4020SMarcel Holtmann /* Interface numbers are hardcoded in the specification */ 10119bfa35feSMarcel Holtmann data->isoc = usb_ifnum_to_if(data->udev, 1); 10129bfa35feSMarcel Holtmann 10137a9d4020SMarcel Holtmann if (!reset) 10147a9d4020SMarcel Holtmann set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks); 10155e23b923SMarcel Holtmann 1016cfeb4145SMarcel Holtmann if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) { 1017cfeb4145SMarcel Holtmann if (!disable_scofix) 1018cfeb4145SMarcel Holtmann set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks); 1019cfeb4145SMarcel Holtmann } 1020cfeb4145SMarcel Holtmann 10219bfa35feSMarcel Holtmann if (id->driver_info & BTUSB_BROKEN_ISOC) 10229bfa35feSMarcel Holtmann data->isoc = NULL; 10239bfa35feSMarcel Holtmann 10247a9d4020SMarcel Holtmann if (id->driver_info & BTUSB_DIGIANSWER) { 10257a9d4020SMarcel Holtmann data->cmdreq_type = USB_TYPE_VENDOR; 10267a9d4020SMarcel Holtmann set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks); 10277a9d4020SMarcel Holtmann } 10287a9d4020SMarcel Holtmann 10297a9d4020SMarcel Holtmann if (id->driver_info & BTUSB_CSR) { 10307a9d4020SMarcel Holtmann struct usb_device *udev = data->udev; 10317a9d4020SMarcel Holtmann 10327a9d4020SMarcel Holtmann /* Old firmware would otherwise execute USB reset */ 10337a9d4020SMarcel Holtmann if (le16_to_cpu(udev->descriptor.bcdDevice) < 0x117) 10347a9d4020SMarcel Holtmann set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks); 10357a9d4020SMarcel Holtmann } 10367a9d4020SMarcel Holtmann 1037cfeb4145SMarcel Holtmann if (id->driver_info & BTUSB_SNIFFER) { 10389bfa35feSMarcel Holtmann struct usb_device *udev = data->udev; 1039cfeb4145SMarcel Holtmann 10407a9d4020SMarcel Holtmann /* New sniffer firmware has crippled HCI interface */ 1041cfeb4145SMarcel Holtmann if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997) 1042cfeb4145SMarcel Holtmann set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks); 10439bfa35feSMarcel Holtmann 10449bfa35feSMarcel Holtmann data->isoc = NULL; 1045cfeb4145SMarcel Holtmann } 1046cfeb4145SMarcel Holtmann 1047cfeb4145SMarcel Holtmann if (id->driver_info & BTUSB_BCM92035) { 1048cfeb4145SMarcel Holtmann unsigned char cmd[] = { 0x3b, 0xfc, 0x01, 0x00 }; 1049cfeb4145SMarcel Holtmann struct sk_buff *skb; 1050cfeb4145SMarcel Holtmann 1051cfeb4145SMarcel Holtmann skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL); 1052cfeb4145SMarcel Holtmann if (skb) { 1053cfeb4145SMarcel Holtmann memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd)); 1054cfeb4145SMarcel Holtmann skb_queue_tail(&hdev->driver_init, skb); 1055cfeb4145SMarcel Holtmann } 1056cfeb4145SMarcel Holtmann } 1057cfeb4145SMarcel Holtmann 10589bfa35feSMarcel Holtmann if (data->isoc) { 10599bfa35feSMarcel Holtmann err = usb_driver_claim_interface(&btusb_driver, 10605fbcd260SMarcel Holtmann data->isoc, data); 10619bfa35feSMarcel Holtmann if (err < 0) { 10629bfa35feSMarcel Holtmann hci_free_dev(hdev); 10639bfa35feSMarcel Holtmann kfree(data); 10649bfa35feSMarcel Holtmann return err; 10659bfa35feSMarcel Holtmann } 10669bfa35feSMarcel Holtmann } 10679bfa35feSMarcel Holtmann 10685e23b923SMarcel Holtmann err = hci_register_dev(hdev); 10695e23b923SMarcel Holtmann if (err < 0) { 10705e23b923SMarcel Holtmann hci_free_dev(hdev); 10715e23b923SMarcel Holtmann kfree(data); 10725e23b923SMarcel Holtmann return err; 10735e23b923SMarcel Holtmann } 10745e23b923SMarcel Holtmann 10755e23b923SMarcel Holtmann usb_set_intfdata(intf, data); 10765e23b923SMarcel Holtmann 10775e23b923SMarcel Holtmann return 0; 10785e23b923SMarcel Holtmann } 10795e23b923SMarcel Holtmann 10805e23b923SMarcel Holtmann static void btusb_disconnect(struct usb_interface *intf) 10815e23b923SMarcel Holtmann { 10825e23b923SMarcel Holtmann struct btusb_data *data = usb_get_intfdata(intf); 10835e23b923SMarcel Holtmann struct hci_dev *hdev; 10845e23b923SMarcel Holtmann 10855e23b923SMarcel Holtmann BT_DBG("intf %p", intf); 10865e23b923SMarcel Holtmann 10875e23b923SMarcel Holtmann if (!data) 10885e23b923SMarcel Holtmann return; 10895e23b923SMarcel Holtmann 10905e23b923SMarcel Holtmann hdev = data->hdev; 10915fbcd260SMarcel Holtmann usb_set_intfdata(data->intf, NULL); 10925fbcd260SMarcel Holtmann 10935fbcd260SMarcel Holtmann if (data->isoc) 10945fbcd260SMarcel Holtmann usb_set_intfdata(data->isoc, NULL); 10955e23b923SMarcel Holtmann 10965e23b923SMarcel Holtmann hci_unregister_dev(hdev); 10975e23b923SMarcel Holtmann 10985fbcd260SMarcel Holtmann if (intf == data->isoc) 10995fbcd260SMarcel Holtmann usb_driver_release_interface(&btusb_driver, data->intf); 11005fbcd260SMarcel Holtmann else if (data->isoc) 11015fbcd260SMarcel Holtmann usb_driver_release_interface(&btusb_driver, data->isoc); 11025fbcd260SMarcel Holtmann 11035e23b923SMarcel Holtmann hci_free_dev(hdev); 11048381088eSDavid Herrmann kfree(data); 11055e23b923SMarcel Holtmann } 11065e23b923SMarcel Holtmann 11077bee549eSOliver Neukum #ifdef CONFIG_PM 11086a88adf2SMarcel Holtmann static int btusb_suspend(struct usb_interface *intf, pm_message_t message) 11096a88adf2SMarcel Holtmann { 11106a88adf2SMarcel Holtmann struct btusb_data *data = usb_get_intfdata(intf); 11116a88adf2SMarcel Holtmann 11126a88adf2SMarcel Holtmann BT_DBG("intf %p", intf); 11136a88adf2SMarcel Holtmann 11146a88adf2SMarcel Holtmann if (data->suspend_count++) 11156a88adf2SMarcel Holtmann return 0; 11166a88adf2SMarcel Holtmann 11177bee549eSOliver Neukum spin_lock_irq(&data->txlock); 11185b1b0b81SAlan Stern if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) { 11197bee549eSOliver Neukum set_bit(BTUSB_SUSPENDING, &data->flags); 11207bee549eSOliver Neukum spin_unlock_irq(&data->txlock); 11217bee549eSOliver Neukum } else { 11227bee549eSOliver Neukum spin_unlock_irq(&data->txlock); 11237bee549eSOliver Neukum data->suspend_count--; 11247bee549eSOliver Neukum return -EBUSY; 11257bee549eSOliver Neukum } 11267bee549eSOliver Neukum 11276a88adf2SMarcel Holtmann cancel_work_sync(&data->work); 11286a88adf2SMarcel Holtmann 11297bee549eSOliver Neukum btusb_stop_traffic(data); 11306a88adf2SMarcel Holtmann usb_kill_anchored_urbs(&data->tx_anchor); 11316a88adf2SMarcel Holtmann 11326a88adf2SMarcel Holtmann return 0; 11336a88adf2SMarcel Holtmann } 11346a88adf2SMarcel Holtmann 11357bee549eSOliver Neukum static void play_deferred(struct btusb_data *data) 11367bee549eSOliver Neukum { 11377bee549eSOliver Neukum struct urb *urb; 11387bee549eSOliver Neukum int err; 11397bee549eSOliver Neukum 11407bee549eSOliver Neukum while ((urb = usb_get_from_anchor(&data->deferred))) { 11417bee549eSOliver Neukum err = usb_submit_urb(urb, GFP_ATOMIC); 11427bee549eSOliver Neukum if (err < 0) 11437bee549eSOliver Neukum break; 11447bee549eSOliver Neukum 11457bee549eSOliver Neukum data->tx_in_flight++; 11467bee549eSOliver Neukum } 11477bee549eSOliver Neukum usb_scuttle_anchored_urbs(&data->deferred); 11487bee549eSOliver Neukum } 11497bee549eSOliver Neukum 11506a88adf2SMarcel Holtmann static int btusb_resume(struct usb_interface *intf) 11516a88adf2SMarcel Holtmann { 11526a88adf2SMarcel Holtmann struct btusb_data *data = usb_get_intfdata(intf); 11536a88adf2SMarcel Holtmann struct hci_dev *hdev = data->hdev; 11547bee549eSOliver Neukum int err = 0; 11556a88adf2SMarcel Holtmann 11566a88adf2SMarcel Holtmann BT_DBG("intf %p", intf); 11576a88adf2SMarcel Holtmann 11586a88adf2SMarcel Holtmann if (--data->suspend_count) 11596a88adf2SMarcel Holtmann return 0; 11606a88adf2SMarcel Holtmann 11616a88adf2SMarcel Holtmann if (!test_bit(HCI_RUNNING, &hdev->flags)) 11627bee549eSOliver Neukum goto done; 11636a88adf2SMarcel Holtmann 11646a88adf2SMarcel Holtmann if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) { 11656a88adf2SMarcel Holtmann err = btusb_submit_intr_urb(hdev, GFP_NOIO); 11666a88adf2SMarcel Holtmann if (err < 0) { 11676a88adf2SMarcel Holtmann clear_bit(BTUSB_INTR_RUNNING, &data->flags); 11687bee549eSOliver Neukum goto failed; 11696a88adf2SMarcel Holtmann } 11706a88adf2SMarcel Holtmann } 11716a88adf2SMarcel Holtmann 11726a88adf2SMarcel Holtmann if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) { 117343c2e57fSMarcel Holtmann err = btusb_submit_bulk_urb(hdev, GFP_NOIO); 117443c2e57fSMarcel Holtmann if (err < 0) { 11756a88adf2SMarcel Holtmann clear_bit(BTUSB_BULK_RUNNING, &data->flags); 11767bee549eSOliver Neukum goto failed; 11777bee549eSOliver Neukum } 11787bee549eSOliver Neukum 11796a88adf2SMarcel Holtmann btusb_submit_bulk_urb(hdev, GFP_NOIO); 11806a88adf2SMarcel Holtmann } 11816a88adf2SMarcel Holtmann 11826a88adf2SMarcel Holtmann if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) { 11836a88adf2SMarcel Holtmann if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0) 11846a88adf2SMarcel Holtmann clear_bit(BTUSB_ISOC_RUNNING, &data->flags); 11856a88adf2SMarcel Holtmann else 11866a88adf2SMarcel Holtmann btusb_submit_isoc_urb(hdev, GFP_NOIO); 11876a88adf2SMarcel Holtmann } 11886a88adf2SMarcel Holtmann 11897bee549eSOliver Neukum spin_lock_irq(&data->txlock); 11907bee549eSOliver Neukum play_deferred(data); 11917bee549eSOliver Neukum clear_bit(BTUSB_SUSPENDING, &data->flags); 11927bee549eSOliver Neukum spin_unlock_irq(&data->txlock); 11937bee549eSOliver Neukum schedule_work(&data->work); 11947bee549eSOliver Neukum 11956a88adf2SMarcel Holtmann return 0; 11967bee549eSOliver Neukum 11977bee549eSOliver Neukum failed: 11987bee549eSOliver Neukum usb_scuttle_anchored_urbs(&data->deferred); 11997bee549eSOliver Neukum done: 12007bee549eSOliver Neukum spin_lock_irq(&data->txlock); 12017bee549eSOliver Neukum clear_bit(BTUSB_SUSPENDING, &data->flags); 12027bee549eSOliver Neukum spin_unlock_irq(&data->txlock); 12037bee549eSOliver Neukum 12047bee549eSOliver Neukum return err; 12056a88adf2SMarcel Holtmann } 12067bee549eSOliver Neukum #endif 12076a88adf2SMarcel Holtmann 12085e23b923SMarcel Holtmann static struct usb_driver btusb_driver = { 12095e23b923SMarcel Holtmann .name = "btusb", 12105e23b923SMarcel Holtmann .probe = btusb_probe, 12115e23b923SMarcel Holtmann .disconnect = btusb_disconnect, 12127bee549eSOliver Neukum #ifdef CONFIG_PM 12136a88adf2SMarcel Holtmann .suspend = btusb_suspend, 12146a88adf2SMarcel Holtmann .resume = btusb_resume, 12157bee549eSOliver Neukum #endif 12165e23b923SMarcel Holtmann .id_table = btusb_table, 12177bee549eSOliver Neukum .supports_autosuspend = 1, 12185e23b923SMarcel Holtmann }; 12195e23b923SMarcel Holtmann 122093f1508cSGreg Kroah-Hartman module_usb_driver(btusb_driver); 12215e23b923SMarcel Holtmann 1222cfeb4145SMarcel Holtmann module_param(ignore_dga, bool, 0644); 1223cfeb4145SMarcel Holtmann MODULE_PARM_DESC(ignore_dga, "Ignore devices with id 08fd:0001"); 1224cfeb4145SMarcel Holtmann 1225cfeb4145SMarcel Holtmann module_param(ignore_csr, bool, 0644); 1226cfeb4145SMarcel Holtmann MODULE_PARM_DESC(ignore_csr, "Ignore devices with id 0a12:0001"); 1227cfeb4145SMarcel Holtmann 1228cfeb4145SMarcel Holtmann module_param(ignore_sniffer, bool, 0644); 1229cfeb4145SMarcel Holtmann MODULE_PARM_DESC(ignore_sniffer, "Ignore devices with id 0a12:0002"); 1230cfeb4145SMarcel Holtmann 1231cfeb4145SMarcel Holtmann module_param(disable_scofix, bool, 0644); 1232cfeb4145SMarcel Holtmann MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size"); 1233cfeb4145SMarcel Holtmann 1234cfeb4145SMarcel Holtmann module_param(force_scofix, bool, 0644); 1235cfeb4145SMarcel Holtmann MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size"); 1236cfeb4145SMarcel Holtmann 1237cfeb4145SMarcel Holtmann module_param(reset, bool, 0644); 1238cfeb4145SMarcel Holtmann MODULE_PARM_DESC(reset, "Send HCI reset command on initialization"); 1239cfeb4145SMarcel Holtmann 12405e23b923SMarcel Holtmann MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>"); 12415e23b923SMarcel Holtmann MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION); 12425e23b923SMarcel Holtmann MODULE_VERSION(VERSION); 12435e23b923SMarcel Holtmann MODULE_LICENSE("GPL"); 1244