Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  *  Realtek Bluetooth USB driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  *  This program is free software; you can redistribute it and/or modify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  *  it under the terms of the GNU General Public License as published by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  *  the Free Software Foundation; either version 2 of the License, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  *  (at your option) any later version.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  *  This program is distributed in the hope that it will be useful,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13)  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14)  *  GNU General Public License for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16)  *  You should have received a copy of the GNU General Public License
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17)  *  along with this program; if not, write to the Free Software
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18)  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #include <linux/usb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include <linux/dcache.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #include <net/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #include <asm/unaligned.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #include "rtk_bt.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #include "rtk_misc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) #define VERSION "3.1.6d45ddf.20220519-142432"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) #ifdef BTCOEX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) #include "rtk_coex.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) #ifdef RTKBT_SWITCH_PATCH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) #include <linux/semaphore.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) #include <net/bluetooth/hci_core.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) DEFINE_SEMAPHORE(switch_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) #if HCI_VERSION_CODE >= KERNEL_VERSION(3, 7, 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) static bool reset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) static struct usb_driver btusb_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) static struct usb_device_id btusb_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 		.match_flags = USB_DEVICE_ID_MATCH_VENDOR |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 			USB_DEVICE_ID_MATCH_INT_INFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 		.idVendor = 0x0bda,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 		.bInterfaceClass = 0xe0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 		.bInterfaceSubClass = 0x01,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 		.bInterfaceProtocol = 0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 	}, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 		.match_flags = USB_DEVICE_ID_MATCH_VENDOR |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 			USB_DEVICE_ID_MATCH_INT_INFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 		.idVendor = 0x13d3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 		.bInterfaceClass = 0xe0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 		.bInterfaceSubClass = 0x01,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 		.bInterfaceProtocol = 0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 	}, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 		.match_flags = USB_DEVICE_ID_MATCH_VENDOR |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 			USB_DEVICE_ID_MATCH_INT_INFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 		.idVendor = 0x0489,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 		.bInterfaceClass = 0xe0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 		.bInterfaceSubClass = 0x01,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 		.bInterfaceProtocol = 0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 	}, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 		.match_flags = USB_DEVICE_ID_MATCH_VENDOR |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 			USB_DEVICE_ID_MATCH_INT_INFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 		.idVendor = 0x1358,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 		.bInterfaceClass = 0xe0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 		.bInterfaceSubClass = 0x01,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 		.bInterfaceProtocol = 0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 	}, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 		.match_flags = USB_DEVICE_ID_MATCH_VENDOR |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 			USB_DEVICE_ID_MATCH_INT_INFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 		.idVendor = 0x04ca,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 		.bInterfaceClass = 0xe0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 		.bInterfaceSubClass = 0x01,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 		.bInterfaceProtocol = 0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 	}, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 		.match_flags = USB_DEVICE_ID_MATCH_VENDOR |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 			USB_DEVICE_ID_MATCH_INT_INFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 		.idVendor = 0x2ff8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 		.bInterfaceClass = 0xe0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 		.bInterfaceSubClass = 0x01,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 		.bInterfaceProtocol = 0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 	}, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 		.match_flags = USB_DEVICE_ID_MATCH_VENDOR |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 			USB_DEVICE_ID_MATCH_INT_INFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 		.idVendor = 0x0b05,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 		.bInterfaceClass = 0xe0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 		.bInterfaceSubClass = 0x01,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 		.bInterfaceProtocol = 0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 	}, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 		.match_flags = USB_DEVICE_ID_MATCH_VENDOR |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 			USB_DEVICE_ID_MATCH_INT_INFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 		.idVendor = 0x0930,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 		.bInterfaceClass = 0xe0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 		.bInterfaceSubClass = 0x01,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 		.bInterfaceProtocol = 0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	}, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 		.match_flags = USB_DEVICE_ID_MATCH_VENDOR |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 			USB_DEVICE_ID_MATCH_INT_INFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 		.idVendor = 0x10ec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 		.bInterfaceClass = 0xe0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 		.bInterfaceSubClass = 0x01,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 		.bInterfaceProtocol = 0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 	}, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 		.match_flags = USB_DEVICE_ID_MATCH_VENDOR |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 			USB_DEVICE_ID_MATCH_INT_INFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 		.idVendor = 0x04c5,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 		.bInterfaceClass = 0xe0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 		.bInterfaceSubClass = 0x01,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 		.bInterfaceProtocol = 0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 	}, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 		.match_flags = USB_DEVICE_ID_MATCH_VENDOR |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 			USB_DEVICE_ID_MATCH_INT_INFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 		.idVendor = 0x0cb5,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 		.bInterfaceClass = 0xe0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 		.bInterfaceSubClass = 0x01,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 		.bInterfaceProtocol = 0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	}, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 		.match_flags = USB_DEVICE_ID_MATCH_VENDOR |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 			USB_DEVICE_ID_MATCH_INT_INFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 		.idVendor = 0x0cb8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 		.bInterfaceClass = 0xe0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 		.bInterfaceSubClass = 0x01,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 		.bInterfaceProtocol = 0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	}, { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) static void rtk_free(struct btusb_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 	kfree(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) static struct btusb_data *rtk_alloc(struct usb_interface *intf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 	struct btusb_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	data = kzalloc(sizeof(*data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 	return data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) MODULE_DEVICE_TABLE(usb, btusb_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) static int inc_tx(struct btusb_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 	int rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 	spin_lock_irqsave(&data->txlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 	rv = test_bit(BTUSB_SUSPENDING, &data->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	if (!rv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 		data->tx_in_flight++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 	spin_unlock_irqrestore(&data->txlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 	return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) #if HCI_VERSION_CODE >= KERNEL_VERSION(3, 18, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) static inline void btusb_free_frags(struct btusb_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	spin_lock_irqsave(&data->rxlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 	kfree_skb(data->evt_skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	data->evt_skb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	kfree_skb(data->acl_skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	data->acl_skb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	kfree_skb(data->sco_skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	data->sco_skb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 	spin_unlock_irqrestore(&data->rxlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 	spin_lock(&data->rxlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	skb = data->evt_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 	while (count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 		int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 		if (!skb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 			skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 			if (!skb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 				err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 			bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 			bt_cb(skb)->expect = HCI_EVENT_HDR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 		len = min_t(uint, bt_cb(skb)->expect, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 		memcpy(skb_put(skb, len), buffer, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 		count -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 		buffer += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 		bt_cb(skb)->expect -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 		if (skb->len == HCI_EVENT_HDR_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 			/* Complete event header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 			bt_cb(skb)->expect = hci_event_hdr(skb)->plen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 			if (skb_tailroom(skb) < bt_cb(skb)->expect) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 				kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 				skb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 				err = -EILSEQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 		if (bt_cb(skb)->expect == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 			/* Complete frame */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 			hci_recv_frame(data->hdev, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 			skb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	data->evt_skb = skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 	spin_unlock(&data->rxlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 	spin_lock(&data->rxlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 	skb = data->acl_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 	while (count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 		int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 		if (!skb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 			skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 			if (!skb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 				err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 			bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 			bt_cb(skb)->expect = HCI_ACL_HDR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 		len = min_t(uint, bt_cb(skb)->expect, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 		memcpy(skb_put(skb, len), buffer, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 		count -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 		buffer += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 		bt_cb(skb)->expect -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 		if (skb->len == HCI_ACL_HDR_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 			__le16 dlen = hci_acl_hdr(skb)->dlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 			/* Complete ACL header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 			bt_cb(skb)->expect = __le16_to_cpu(dlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 			if (skb_tailroom(skb) < bt_cb(skb)->expect) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 				kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 				skb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 				err = -EILSEQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 		if (bt_cb(skb)->expect == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 			/* Complete frame */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 			hci_recv_frame(data->hdev, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 			skb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	data->acl_skb = skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	spin_unlock(&data->rxlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	spin_lock(&data->rxlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 	skb = data->sco_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	while (count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 		int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 		if (!skb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 			skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 			if (!skb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 				err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 			bt_cb(skb)->pkt_type = HCI_SCODATA_PKT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 			bt_cb(skb)->expect = HCI_SCO_HDR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 		len = min_t(uint, bt_cb(skb)->expect, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 		memcpy(skb_put(skb, len), buffer, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 		count -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 		buffer += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 		bt_cb(skb)->expect -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 		if (skb->len == HCI_SCO_HDR_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 			/* Complete SCO header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 			bt_cb(skb)->expect = hci_sco_hdr(skb)->dlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 			if (skb_tailroom(skb) < bt_cb(skb)->expect) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 				kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 				skb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 				err = -EILSEQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 		if (bt_cb(skb)->expect == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 			/* Complete frame */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 			hci_recv_frame(data->hdev, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 			skb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 	data->sco_skb = skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 	spin_unlock(&data->rxlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) static void btusb_intr_complete(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 	struct hci_dev *hdev = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 	struct btusb_data *data = GET_DRV_DATA(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 	//RTKBT_DBG("%s: urb %p status %d count %d ", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	//urb, urb->status, urb->actual_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 	if (!test_bit(HCI_RUNNING, &hdev->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 	if (urb->status == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 		hdev->stat.byte_rx += urb->actual_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) #ifdef BTCOEX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 		rtk_btcoex_parse_event(urb->transfer_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 				urb->actual_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) #if HCI_VERSION_CODE < KERNEL_VERSION(3, 18, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 		if (hci_recv_fragment(hdev, HCI_EVENT_PKT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 				      urb->transfer_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 				      urb->actual_length) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 			RTKBT_ERR("%s: Corrupted event packet", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 			hdev->stat.err_rx++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 		if (btusb_recv_intr(data, urb->transfer_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 				    urb->actual_length) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 			RTKBT_ERR("%s corrupted event packet", hdev->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 			hdev->stat.err_rx++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 	/* Avoid suspend failed when usb_kill_urb */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	else if (urb->status == -ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	usb_mark_last_busy(data->udev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	usb_anchor_urb(urb, &data->intr_anchor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	err = usb_submit_urb(urb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 		/* -EPERM: urb is being killed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 		 * -ENODEV: device got disconnected */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 		if (err != -EPERM && err != -ENODEV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 			RTKBT_ERR("%s: Failed to re-submit urb %p, err %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 				  __func__, urb, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 		usb_unanchor_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	struct btusb_data *data = GET_DRV_DATA(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 	struct urb *urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 	unsigned char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 	unsigned int pipe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	int err, size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	//RTKBT_DBG("%s", hdev->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	if (!data->intr_ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 	urb = usb_alloc_urb(0, mem_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	if (!urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 	size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 	buf = kmalloc(size, mem_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	if (!buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 		usb_free_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 	pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 	usb_fill_int_urb(urb, data->udev, pipe, buf, size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 			 btusb_intr_complete, hdev, data->intr_ep->bInterval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	urb->transfer_flags |= URB_FREE_BUFFER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 	usb_anchor_urb(urb, &data->intr_anchor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 	err = usb_submit_urb(urb, mem_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 		RTKBT_ERR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 		    ("btusb_submit_intr_urb %s urb %p submission failed (%d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 		     hdev->name, urb, -err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 		usb_unanchor_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	usb_free_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) static void btusb_bulk_complete(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	struct hci_dev *hdev = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	struct btusb_data *data = GET_DRV_DATA(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	//RTKBT_DBG("%s: urb %p status %d count %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 	//__func__, urb, urb->status, urb->actual_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 	if (!test_bit(HCI_RUNNING, &hdev->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) #ifdef BTCOEX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	if (urb->status == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 		rtk_btcoex_parse_l2cap_data_rx(urb->transfer_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 				urb->actual_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 	if (urb->status == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 		hdev->stat.byte_rx += urb->actual_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) #if HCI_VERSION_CODE < KERNEL_VERSION(3, 18, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 		if (hci_recv_fragment(hdev, HCI_ACLDATA_PKT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 				      urb->transfer_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 				      urb->actual_length) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 			RTKBT_ERR("%s: Corrupted ACL packet", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 			hdev->stat.err_rx++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 		if (data->recv_bulk(data, urb->transfer_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 				    urb->actual_length) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 			RTKBT_ERR("%s corrupted ACL packet", hdev->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 			hdev->stat.err_rx++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	/* Avoid suspend failed when usb_kill_urb */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	else if (urb->status == -ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	usb_anchor_urb(urb, &data->bulk_anchor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	usb_mark_last_busy(data->udev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 	err = usb_submit_urb(urb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 		/* -EPERM: urb is being killed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 		 * -ENODEV: device got disconnected */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 		if (err != -EPERM && err != -ENODEV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 			RTKBT_ERR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 			    ("btusb_bulk_complete %s urb %p failed to resubmit (%d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 			     hdev->name, urb, -err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 		usb_unanchor_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	struct btusb_data *data = GET_DRV_DATA(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	struct urb *urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	unsigned char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	unsigned int pipe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 	int err, size = HCI_MAX_FRAME_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	//RTKBT_DBG("%s: hdev name %s", __func__, hdev->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	if (!data->bulk_rx_ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	urb = usb_alloc_urb(0, mem_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	if (!urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	buf = kmalloc(size, mem_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	if (!buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 		usb_free_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	usb_fill_bulk_urb(urb, data->udev, pipe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 			  buf, size, btusb_bulk_complete, hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	urb->transfer_flags |= URB_FREE_BUFFER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 	usb_mark_last_busy(data->udev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	usb_anchor_urb(urb, &data->bulk_anchor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	err = usb_submit_urb(urb, mem_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 		RTKBT_ERR("%s: Failed to submit urb %p, err %d", __func__, urb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 			  err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 		usb_unanchor_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	usb_free_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) static void btusb_isoc_complete(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 	struct hci_dev *hdev = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 	struct btusb_data *data = GET_DRV_DATA(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 	int i, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 	   RTKBT_DBG("%s urb %p status %d count %d", hdev->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 	   urb, urb->status, urb->actual_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	if (!test_bit(HCI_RUNNING, &hdev->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 	if (urb->status == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 		for (i = 0; i < urb->number_of_packets; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 			unsigned int offset = urb->iso_frame_desc[i].offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 			unsigned int length =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 			    urb->iso_frame_desc[i].actual_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 			if (urb->iso_frame_desc[i].status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 			hdev->stat.byte_rx += length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) #if HCI_VERSION_CODE < KERNEL_VERSION(3, 18, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 			if (hci_recv_fragment(hdev, HCI_SCODATA_PKT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 					      urb->transfer_buffer + offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 					      length) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 				RTKBT_ERR("%s: Corrupted SCO packet", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 				hdev->stat.err_rx++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 			if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 					    length) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 				RTKBT_ERR("%s corrupted SCO packet",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 					  hdev->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 				hdev->stat.err_rx++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	/* Avoid suspend failed when usb_kill_urb */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 	else if (urb->status == -ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	usb_anchor_urb(urb, &data->isoc_anchor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 	err = usb_submit_urb(urb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 		/* -EPERM: urb is being killed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 		 * -ENODEV: device got disconnected */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 		if (err != -EPERM && err != -ENODEV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 			RTKBT_ERR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 			    ("%s: Failed to re-sumbit urb %p, retry %d, err %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 			     __func__, urb, i, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 		if (i < 10) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 			i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 			mdelay(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 			goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 		usb_unanchor_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	int i, offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	//RTKBT_DBG("len %d mtu %d", len, mtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 	for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 	     i++, offset += mtu, len -= mtu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 		urb->iso_frame_desc[i].offset = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 		urb->iso_frame_desc[i].length = mtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	if (len && i < BTUSB_MAX_ISOC_FRAMES) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 		urb->iso_frame_desc[i].offset = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 		urb->iso_frame_desc[i].length = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 		i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 	urb->number_of_packets = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 	struct btusb_data *data = GET_DRV_DATA(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 	struct urb *urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	unsigned char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 	unsigned int pipe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 	int err, size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	//RTKBT_DBG("%s", hdev->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 	if (!data->isoc_rx_ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 	if (!urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 	size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	    BTUSB_MAX_ISOC_FRAMES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	buf = kmalloc(size, mem_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	if (!buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 		usb_free_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	urb->dev = data->udev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	urb->pipe = pipe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	urb->context = hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	urb->complete = btusb_isoc_complete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	urb->interval = data->isoc_rx_ep->bInterval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 	urb->transfer_buffer = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	urb->transfer_buffer_length = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 	__fill_isoc_descriptor(urb, size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 			       le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	usb_anchor_urb(urb, &data->isoc_anchor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	err = usb_submit_urb(urb, mem_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 		RTKBT_ERR("%s %s urb %p submission failed (%d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 			  __func__, hdev->name, urb, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 		usb_unanchor_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	usb_free_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) static void btusb_tx_complete(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	struct sk_buff *skb = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	struct hci_dev *hdev = (struct hci_dev *)skb->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 	struct btusb_data *data = GET_DRV_DATA(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) //      RTKBT_DBG("btusb_tx_complete %s urb %p status %d count %d", hdev->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) //                                      urb, urb->status, urb->actual_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 	if (!test_bit(HCI_RUNNING, &hdev->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	if (!urb->status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 		hdev->stat.byte_tx += urb->transfer_buffer_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 		hdev->stat.err_tx++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 	spin_lock(&data->txlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	data->tx_in_flight--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	spin_unlock(&data->txlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	kfree(urb->setup_packet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) static void btusb_isoc_tx_complete(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	struct sk_buff *skb = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 	struct hci_dev *hdev = (struct hci_dev *)skb->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 	RTKBT_DBG("%s: urb %p status %d count %d",__func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 		       	urb, urb->status, urb->actual_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 	if (!test_bit(HCI_RUNNING, &hdev->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	if (!urb->status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 		hdev->stat.byte_tx += urb->transfer_buffer_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 		hdev->stat.err_tx++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	kfree(urb->setup_packet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 	kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) static int btusb_open(struct hci_dev *hdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 	struct btusb_data *data = GET_DRV_DATA(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 	err = usb_autopm_get_interface(data->intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	data->intf->needs_remote_wakeup = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	RTKBT_DBG("%s start", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	/*******************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	if (0 == atomic_read(&hdev->promisc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 		RTKBT_ERR("btusb_open hdev->promisc ==0");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 		//err = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 		//goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 	err = download_patch(data->intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 		goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 	/*******************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	RTKBT_INFO("%s set HCI_RUNNING", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 	if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 	if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 	err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 		goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 	err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 		mdelay(URB_CANCELING_DELAY_MS);	// Added by Realtek
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 		usb_kill_anchored_urbs(&data->intr_anchor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 		goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	set_bit(BTUSB_BULK_RUNNING, &data->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	btusb_submit_bulk_urb(hdev, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	usb_autopm_put_interface(data->intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) #ifdef BTCOEX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	rtk_btcoex_open(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	RTKBT_DBG("%s end", __FUNCTION__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	clear_bit(BTUSB_INTR_RUNNING, &data->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 	clear_bit(HCI_RUNNING, &hdev->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 	usb_autopm_put_interface(data->intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 	RTKBT_ERR("%s failed", __FUNCTION__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) static void btusb_stop_traffic(struct btusb_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	mdelay(URB_CANCELING_DELAY_MS);	// Added by Realtek
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 	usb_kill_anchored_urbs(&data->intr_anchor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 	usb_kill_anchored_urbs(&data->bulk_anchor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	usb_kill_anchored_urbs(&data->isoc_anchor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) static int btusb_close(struct hci_dev *hdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 	struct btusb_data *data = GET_DRV_DATA(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) #if HCI_VERSION_CODE < KERNEL_VERSION(4, 1, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 	/* When in kernel 4.4.0 and greater, the HCI_RUNNING bit is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 	 * cleared in hci_dev_do_close(). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) #if HCI_VERSION_CODE < KERNEL_VERSION(4, 4, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 	if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	if (test_bit(HCI_RUNNING, &hdev->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 		RTKBT_ERR("HCI_RUNNING is not cleared before.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 	RTKBT_DBG("btusb_close");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) #if HCI_VERSION_CODE < KERNEL_VERSION(4, 1, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	/*******************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 	for (i = 0; i < NUM_REASSEMBLY; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 		if (hdev->reassembly[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 			kfree_skb(hdev->reassembly[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 			hdev->reassembly[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 			RTKBT_DBG("%s free ressembly i=%d", __FUNCTION__, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	/*******************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	cancel_work_sync(&data->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	cancel_work_sync(&data->waker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 	clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	clear_bit(BTUSB_BULK_RUNNING, &data->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 	clear_bit(BTUSB_INTR_RUNNING, &data->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 	btusb_stop_traffic(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) #if HCI_VERSION_CODE >= KERNEL_VERSION(3, 18, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 	btusb_free_frags(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 	err = usb_autopm_get_interface(data->intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 		goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	data->intf->needs_remote_wakeup = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 	usb_autopm_put_interface(data->intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) #ifdef BTCOEX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	rtk_btcoex_close();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	mdelay(URB_CANCELING_DELAY_MS);	// Added by Realtek
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	usb_scuttle_anchored_urbs(&data->deferred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) #ifdef RTKBT_SWITCH_PATCH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	down(&switch_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	if (data->context) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 		struct api_context *ctx = data->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 		if (ctx->flags & RTLBT_CLOSE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 			ctx->flags &= ~RTLBT_CLOSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 			ctx->status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 			complete(&ctx->done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 	up(&switch_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) static int btusb_flush(struct hci_dev *hdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	struct btusb_data *data = GET_DRV_DATA(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 	RTKBT_DBG("%s add delay ", __FUNCTION__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 	mdelay(URB_CANCELING_DELAY_MS);	// Added by Realtek
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 	usb_kill_anchored_urbs(&data->tx_anchor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) #if HCI_VERSION_CODE >= KERNEL_VERSION(3, 18, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 	btusb_free_frags(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) const char pkt_ind[][8] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	[HCI_COMMAND_PKT] = "cmd",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 	[HCI_ACLDATA_PKT] = "acl",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	[HCI_SCODATA_PKT] = "sco",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) #if HCI_VERSION_CODE >= KERNEL_VERSION(3, 13, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) int btusb_send_frame(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	struct hci_dev *hdev = (struct hci_dev *)skb->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 	struct btusb_data *data = GET_DRV_DATA(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 	struct usb_ctrlrequest *dr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	struct urb *urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 	unsigned int pipe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 	//RTKBT_DBG("%s", hdev->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	if (!test_bit(HCI_RUNNING, &hdev->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 		/* If the parameter is wrong, the hdev isn't the correct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 		 * one. Then no HCI commands can be sent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 		 * This issue is related to the wrong HCI_VERSION_CODE set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 		RTKBT_ERR("HCI is not running");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 	/* Before kernel/hci version 3.13.0, the skb->dev is set before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 	 * entering btusb_send_frame(). So there is no need to set it here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 	 * The skb->dev will be used in the callbacks when urb transfer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 	 * completes. See btusb_tx_complete() and btusb_isoc_tx_complete() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) #if HCI_VERSION_CODE >= KERNEL_VERSION(3, 13, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 	skb->dev = (void *)hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	switch (bt_cb(skb)->pkt_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	case HCI_COMMAND_PKT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 		print_command(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) #ifdef BTCOEX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 		rtk_btcoex_parse_cmd(skb->data, skb->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 		urb = usb_alloc_urb(0, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 		if (!urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 		dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 		if (!dr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 			usb_free_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 		dr->bRequestType = data->cmdreq_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 		dr->bRequest = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 		dr->wIndex = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 		dr->wValue = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 		dr->wLength = __cpu_to_le16(skb->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 		pipe = usb_sndctrlpipe(data->udev, 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 		usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 				     skb->data, skb->len, btusb_tx_complete,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 				     skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 		hdev->stat.cmd_tx++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	case HCI_ACLDATA_PKT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 		print_acl(skb, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) #ifdef BTCOEX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 		rtk_btcoex_parse_l2cap_data_tx(skb->data, skb->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 		if (!data->bulk_tx_ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 			return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 		urb = usb_alloc_urb(0, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 		if (!urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 		pipe = usb_sndbulkpipe(data->udev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 				       data->bulk_tx_ep->bEndpointAddress);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 		usb_fill_bulk_urb(urb, data->udev, pipe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 				  skb->data, skb->len, btusb_tx_complete, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 		hdev->stat.acl_tx++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 	case HCI_SCODATA_PKT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 		if (!data->isoc_tx_ep || SCO_NUM < 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 			return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 		urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 		if (!urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 		pipe = usb_sndisocpipe(data->udev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 				       data->isoc_tx_ep->bEndpointAddress);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 		usb_fill_int_urb(urb, data->udev, pipe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 				 skb->data, skb->len, btusb_isoc_tx_complete,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 				 skb, data->isoc_tx_ep->bInterval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 		urb->transfer_flags = URB_ISO_ASAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 		__fill_isoc_descriptor(urb, skb->len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 				       le16_to_cpu(data->isoc_tx_ep->
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 						   wMaxPacketSize));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 		hdev->stat.sco_tx++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 		goto skip_waking;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 		return -EILSEQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	err = inc_tx(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 		usb_anchor_urb(urb, &data->deferred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 		schedule_work(&data->waker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 		err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) skip_waking:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 	usb_anchor_urb(urb, &data->tx_anchor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 	err = usb_submit_urb(urb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 		RTKBT_ERR("%s %s urb %p submission for %s failed, err %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 			  __func__, hdev->name, urb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 			  pkt_ind[bt_cb(skb)->pkt_type], err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 		kfree(urb->setup_packet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 		usb_unanchor_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 		usb_mark_last_busy(data->udev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 	usb_free_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) #if HCI_VERSION_CODE < KERNEL_VERSION(3, 4, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) static void btusb_destruct(struct hci_dev *hdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 	RTKBT_DBG("btusb_destruct %s", hdev->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 	hci_free_dev(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 	struct btusb_data *data = GET_DRV_DATA(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 	RTKBT_DBG("%s: %s evt %d", __func__, hdev->name, evt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 	if (SCO_NUM != data->sco_num) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 		data->sco_num = SCO_NUM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 		RTKBT_DBG("%s: Update sco num %d", __func__, data->sco_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) #if HCI_VERSION_CODE >= KERNEL_VERSION(5, 8, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 		data->air_mode = evt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 		schedule_work(&data->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 	struct btusb_data *data = GET_DRV_DATA(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 	struct usb_interface *intf = data->isoc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 	struct usb_endpoint_descriptor *ep_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 	int i, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 	if (!data->isoc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	RTKBT_INFO("set isoc interface: alt %d", altsetting);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 	err = usb_set_interface(data->udev, 1, altsetting);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 		RTKBT_ERR("%s setting interface failed (%d)", hdev->name, -err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 	data->isoc_altsetting = altsetting;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 	data->isoc_tx_ep = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 	data->isoc_rx_ep = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 	for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 		ep_desc = &intf->cur_altsetting->endpoint[i].desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 		if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 			data->isoc_tx_ep = ep_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 		if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 			data->isoc_rx_ep = ep_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 	if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 		RTKBT_ERR("%s invalid SCO descriptors", hdev->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) #if HCI_VERSION_CODE >= KERNEL_VERSION(5, 8, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) static int btusb_switch_alt_setting(struct hci_dev *hdev, int new_alts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 	struct btusb_data *data = hci_get_drvdata(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 	if (data->isoc_altsetting != new_alts) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 		unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 		clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 		usb_kill_anchored_urbs(&data->isoc_anchor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 		/* When isochronous alternate setting needs to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 		 * changed, because SCO connection has been added
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 		 * or removed, a packet fragment may be left in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 		 * reassembling state. This could lead to wrongly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 		 * assembled fragments.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 		 * Clear outstanding fragment when selecting a new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 		 * alternate setting.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 		spin_lock_irqsave(&data->rxlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 		kfree_skb(data->sco_skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 		data->sco_skb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 		spin_unlock_irqrestore(&data->rxlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 		err = __set_isoc_interface(hdev, new_alts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 		if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 	if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 		if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 			clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 			btusb_submit_isoc_urb(hdev, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) static struct usb_host_interface *btusb_find_altsetting(struct btusb_data *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 							int alt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 	struct usb_interface *intf = data->isoc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 	BT_DBG("Looking for Alt no :%d", alt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 	for (i = 0; i < intf->num_altsetting; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 		if (intf->altsetting[i].desc.bAlternateSetting == alt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 			return &intf->altsetting[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) static void btusb_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 	struct btusb_data *data = container_of(work, struct btusb_data, work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 	struct hci_dev *hdev = data->hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 	int new_alts = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 	RTKBT_DBG("%s: sco num %d", __func__, data->sco_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 	if (data->sco_num > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 		if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 			err =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 			    usb_autopm_get_interface(data->isoc ? data->
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 						     isoc : data->intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 			if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 				clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 				mdelay(URB_CANCELING_DELAY_MS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 				usb_kill_anchored_urbs(&data->isoc_anchor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 				return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 			set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) #if HCI_VERSION_CODE >= KERNEL_VERSION(5, 8, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 		if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_CVSD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 			if (hdev->voice_setting & 0x0020) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 				static const int alts[3] = { 2, 4, 5 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 				new_alts = alts[data->sco_num - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 				new_alts = data->sco_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 		} else if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_TRANSP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 			new_alts = btusb_find_altsetting(data, 6) ? 6 : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 		if (btusb_switch_alt_setting(hdev, new_alts) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 				RTKBT_ERR("set USB alt:(%d) failed!", new_alts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) #if HCI_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 		if (hdev->voice_setting & 0x0020) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 			static const int alts[3] = { 2, 4, 5 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 			new_alts = alts[data->sco_num - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 			new_alts = data->sco_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 		if (data->isoc_altsetting != new_alts) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 		if (data->isoc_altsetting != 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 			new_alts = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 			clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 			mdelay(URB_CANCELING_DELAY_MS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 			usb_kill_anchored_urbs(&data->isoc_anchor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 			if (__set_isoc_interface(hdev, new_alts) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 				return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 		if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 			RTKBT_INFO("submit SCO RX urb.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 			if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 				clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 				btusb_submit_isoc_urb(hdev, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 		clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 		mdelay(URB_CANCELING_DELAY_MS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 		usb_kill_anchored_urbs(&data->isoc_anchor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 		__set_isoc_interface(hdev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 		if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 			usb_autopm_put_interface(data->isoc ? data->
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 						 isoc : data->intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) static void btusb_waker(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 	struct btusb_data *data = container_of(work, struct btusb_data, waker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 	err = usb_autopm_get_interface(data->intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 	RTKBT_DBG("%s start", __FUNCTION__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 	usb_autopm_put_interface(data->intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 	RTKBT_DBG("%s end", __FUNCTION__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) #ifdef RTKBT_TV_POWERON_WHITELIST
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) static int rtkbt_lookup_le_device_poweron_whitelist(struct hci_dev *hdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 						struct usb_device *udev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 	struct hci_conn_params *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 	u8 *cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 	int result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 	hci_dev_lock(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 	list_for_each_entry(p, &hdev->le_conn_params, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) #if 0 // for debug message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 		RTKBT_DBG("%s(): auto_connect = %d", __FUNCTION__, p->auto_connect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 		RTKBT_DBG("%s(): addr_type = 0x%02x", __FUNCTION__, p->addr_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 		RTKBT_DBG("%s(): addr=%02x:%02x:%02x:%02x:%02x:%02x", __FUNCTION__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309)                                 p->addr.b[5], p->addr.b[4], p->addr.b[3],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310)                                 p->addr.b[2], p->addr.b[1], p->addr.b[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 		if ( p->auto_connect == HCI_AUTO_CONN_ALWAYS &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 			p->addr_type == ADDR_LE_DEV_PUBLIC ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 			RTKBT_DBG("%s(): Set RTKBT LE Power-on Whitelist for "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 				"%02x:%02x:%02x:%02x:%02x:%02x", __FUNCTION__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317)                                 p->addr.b[5], p->addr.b[4], p->addr.b[3],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318)                                 p->addr.b[2], p->addr.b[1], p->addr.b[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 			cmd = kzalloc(16, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 			if (!cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 				RTKBT_ERR("Can't allocate memory for cmd");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 				return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 			cmd[0] = 0x7b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 			cmd[1] = 0xfc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 			cmd[2] = 0x07;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 			cmd[3] = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 			cmd[4] = p->addr.b[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 			cmd[5] = p->addr.b[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 			cmd[6] = p->addr.b[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 			cmd[7] = p->addr.b[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 			cmd[8] = p->addr.b[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 			cmd[9] = p->addr.b[5];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 			result = __rtk_send_hci_cmd(udev, cmd, 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 			kfree(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 	hci_dev_unlock(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 	return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) int rtkbt_pm_notify(struct notifier_block *notifier,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 		    ulong pm_event, void *unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 	struct btusb_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 	struct usb_device *udev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 	struct usb_interface *intf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 	struct hci_dev *hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 	/* int err; */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) #if defined RTKBT_SWITCH_PATCH || defined RTKBT_TV_POWERON_WHITELIST
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 	int result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) #ifdef RTKBT_SWITCH_PATCH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 	u8 *cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 	static u8 hci_state = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 	struct api_context ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 	data = container_of(notifier, struct btusb_data, pm_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 	udev = data->udev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 	intf = data->intf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 	hdev = data->hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 	RTKBT_DBG("%s: pm_event %ld", __func__, pm_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 	switch (pm_event) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 	case PM_SUSPEND_PREPARE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 	case PM_HIBERNATION_PREPARE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 		/* No need to load firmware because the download firmware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 		 * process is deprecated in resume.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 		 * We use rebind after resume instead */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 		/* err = usb_autopm_get_interface(data->intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 		 * if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 		 * 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 		 * patch_entry->fw_len =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 		 *     load_firmware(dev_entry, &patch_entry->fw_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 		 * usb_autopm_put_interface(data->intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 		 * if (patch_entry->fw_len <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 		 * 	RTKBT_DBG("rtkbt_pm_notify return NOTIFY_BAD");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 		 * 	return NOTIFY_BAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 		 * } */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 		RTKBT_DBG("%s: suspend prepare", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 		if (!device_may_wakeup(&udev->dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) #ifdef CONFIG_NEEDS_BINDING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 			intf->needs_binding = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 			RTKBT_DBG("Remote wakeup not support, set "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 				  "intf->needs_binding = 1");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 			RTKBT_DBG("Remote wakeup not support, no needs binding");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) #ifdef RTKBT_SWITCH_PATCH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 		if (test_bit(HCI_UP, &hdev->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 			unsigned long expire;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 			init_completion(&ctx.done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 			hci_state = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 			down(&switch_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 			data->context = &ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 			ctx.flags = RTLBT_CLOSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 			queue_work(hdev->req_workqueue, &hdev->power_off.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 			up(&switch_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 			expire = msecs_to_jiffies(1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 			if (!wait_for_completion_timeout(&ctx.done, expire))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 				RTKBT_ERR("hdev close timeout");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 			down(&switch_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 			data->context = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 			up(&switch_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 		cmd = kzalloc(16, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 		if (!cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 			RTKBT_ERR("Can't allocate memory for cmd");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 		/* Clear patch */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 		cmd[0] = 0x66;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 		cmd[1] = 0xfc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 		cmd[2] = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 		result = __rtk_send_hci_cmd(udev, cmd, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 		kfree(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 		msleep(100); /* From FW colleague's recommendation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 		result = download_lps_patch(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 		/* Tell the controller to wake up host if received special
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 		 * advertising packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 		set_scan(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 		/* Send special vendor commands */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) #ifdef RTKBT_TV_POWERON_WHITELIST
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 		result = rtkbt_lookup_le_device_poweron_whitelist(hdev, udev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 		if (result < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 			RTKBT_ERR("rtkbt_lookup_le_device_poweron_whitelist error: %d", result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 	case PM_POST_SUSPEND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 	case PM_POST_HIBERNATION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 	case PM_POST_RESTORE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 		/* if (patch_entry->fw_len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 		 * 	kfree(patch_entry->fw_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 		 * 	patch_entry->fw_cache = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 		 * 	patch_entry->fw_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 		 * } */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) #ifdef RTKBT_SWITCH_PATCH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 		cmd = kzalloc(16, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 		if (!cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 			RTKBT_ERR("Can't allocate memory for cmd");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 		/* Clear patch */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 		cmd[0] = 0x66;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 		cmd[1] = 0xfc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 		cmd[2] = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 		result = __rtk_send_hci_cmd(udev, cmd, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 		kfree(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 		msleep(100); /* From FW colleague's recommendation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 		result = download_patch(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 		if (hci_state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 			hci_state = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 			queue_work(hdev->req_workqueue, &hdev->power_on);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) #ifdef BTUSB_RPM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 		RTKBT_DBG("%s: Re-enable autosuspend", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 		/* pm_runtime_use_autosuspend(&udev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 		 * pm_runtime_set_autosuspend_delay(&udev->dev, 2000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 		 * pm_runtime_set_active(&udev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 		 * pm_runtime_allow(&udev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 		 * pm_runtime_mark_last_busy(&udev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 		 * pm_runtime_autosuspend(&udev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 		 * pm_runtime_put_autosuspend(&udev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 		 * usb_disable_autosuspend(udev); */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 		/* FIXME: usb_enable_autosuspend(udev) is useless here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 		 * Because it is always enabled after enabled in btusb_probe()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 		usb_enable_autosuspend(udev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 		pm_runtime_mark_last_busy(&udev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 	return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) static int btusb_probe(struct usb_interface *intf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 		       const struct usb_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 	struct usb_endpoint_descriptor *ep_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 	struct btusb_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 	struct hci_dev *hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 	int i, err, flag1, flag2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 	struct usb_device *udev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 	udev = interface_to_usbdev(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 	RTKBT_DBG("btusb_probe intf->cur_altsetting->desc.bInterfaceNumber %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 		  intf->cur_altsetting->desc.bInterfaceNumber);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 	/* interface numbers are hardcoded in the spec */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 	if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 	/*******************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 	flag1 = device_can_wakeup(&udev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 	flag2 = device_may_wakeup(&udev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 	RTKBT_DBG("btusb_probe can_wakeup %x, may wakeup %x", flag1, flag2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) #ifdef BTUSB_WAKEUP_HOST
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 	device_wakeup_enable(&udev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 	//device_wakeup_enable(&udev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 	/*device_wakeup_disable(&udev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 	   flag1=device_can_wakeup(&udev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 	   flag2=device_may_wakeup(&udev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 	   RTKBT_DBG("btusb_probe can_wakeup=%x  flag2=%x",flag1,flag2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 	err = patch_add(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 	/*******************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 	data = rtk_alloc(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 	if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 	for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 		ep_desc = &intf->cur_altsetting->endpoint[i].desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 		if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 			data->intr_ep = ep_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 		if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 			data->bulk_tx_ep = ep_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 		if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 			data->bulk_rx_ep = ep_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 	if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 		rtk_free(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 	data->cmdreq_type = USB_TYPE_CLASS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 	data->udev = interface_to_usbdev(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 	data->intf = intf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 	spin_lock_init(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 	INIT_WORK(&data->work, btusb_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 	INIT_WORK(&data->waker, btusb_waker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 	spin_lock_init(&data->txlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 	init_usb_anchor(&data->tx_anchor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 	init_usb_anchor(&data->intr_anchor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 	init_usb_anchor(&data->bulk_anchor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 	init_usb_anchor(&data->isoc_anchor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 	init_usb_anchor(&data->deferred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) #if HCI_VERSION_CODE >= KERNEL_VERSION(3, 18, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 	spin_lock_init(&data->rxlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 	data->recv_bulk = btusb_recv_bulk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 	hdev = hci_alloc_dev();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 	if (!hdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 		rtk_free(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 	HDEV_BUS = HCI_USB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 	data->hdev = hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 	SET_HCIDEV_DEV(hdev, &intf->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 	hdev->open = btusb_open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 	hdev->close = btusb_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 	hdev->flush = btusb_flush;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 	hdev->send = btusb_send_frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 	hdev->notify = btusb_notify;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) #if HCI_VERSION_CODE >= KERNEL_VERSION(3, 4, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 	hci_set_drvdata(hdev, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 	hdev->driver_data = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 	hdev->destruct = btusb_destruct;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 	hdev->owner = THIS_MODULE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) #if HCI_VERSION_CODE >= KERNEL_VERSION(3, 7, 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 	if (!reset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 		set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 	RTKBT_DBG("set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 	/* Interface numbers are hardcoded in the specification */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 	data->isoc = usb_ifnum_to_if(data->udev, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 	if (data->isoc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 		err = usb_driver_claim_interface(&btusb_driver,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 						 data->isoc, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 		if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 			hci_free_dev(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 			rtk_free(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) #if HCI_VERSION_CODE >= KERNEL_VERSION(4, 1, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 	set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 	err = hci_register_dev(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 		hci_free_dev(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 		rtk_free(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 	usb_set_intfdata(intf, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 	/* Register PM notifier */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 	data->pm_notifier.notifier_call = rtkbt_pm_notify;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 	register_pm_notifier(&data->pm_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) #ifdef BTCOEX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 	rtk_btcoex_probe(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 	RTKBT_DBG("%s: done", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) static void btusb_disconnect(struct usb_interface *intf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 	struct btusb_data *data = usb_get_intfdata(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 	struct hci_dev *hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 	struct usb_device *udev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 	udev = interface_to_usbdev(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 	if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 	if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 	RTKBT_DBG("btusb_disconnect");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 	/* Un-register PM notifier */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 	unregister_pm_notifier(&data->pm_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 	/*******************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 	patch_remove(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 	/*******************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 	hdev = data->hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) #if HCI_VERSION_CODE < KERNEL_VERSION(3, 4, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 	__hci_dev_hold(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 	usb_set_intfdata(data->intf, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 	if (data->isoc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 		usb_set_intfdata(data->isoc, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 	hci_unregister_dev(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 	if (intf == data->isoc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 		usb_driver_release_interface(&btusb_driver, data->intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 	else if (data->isoc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 		usb_driver_release_interface(&btusb_driver, data->isoc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) #if HCI_VERSION_CODE < KERNEL_VERSION(3, 4, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 	__hci_dev_put(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) #if HCI_VERSION_CODE >= KERNEL_VERSION(3, 18, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 	btusb_free_frags(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 	hci_free_dev(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 	rtk_free(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) #ifdef CONFIG_PM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 	struct btusb_data *data = usb_get_intfdata(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 	if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 	/*******************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 	RTKBT_DBG("btusb_suspend message.event 0x%x, data->suspend_count %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 		  message.event, data->suspend_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 	if (!test_bit(HCI_RUNNING, &data->hdev->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 		RTKBT_INFO("%s: hdev is not HCI_RUNNING", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 		/* set_scan(data->intf); */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 	/*******************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 	if (data->suspend_count++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 	spin_lock_irq(&data->txlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 	if (!((message.event & PM_EVENT_AUTO) && data->tx_in_flight)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 		set_bit(BTUSB_SUSPENDING, &data->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 		spin_unlock_irq(&data->txlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 		RTKBT_INFO("%s: suspending...", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 		spin_unlock_irq(&data->txlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 		data->suspend_count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 	cancel_work_sync(&data->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 	btusb_stop_traffic(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 	mdelay(URB_CANCELING_DELAY_MS);	// Added by Realtek
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 	usb_kill_anchored_urbs(&data->tx_anchor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) static void play_deferred(struct btusb_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 	struct urb *urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 	while ((urb = usb_get_from_anchor(&data->deferred))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 	    /************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 		usb_anchor_urb(urb, &data->tx_anchor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 		err = usb_submit_urb(urb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 		if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 			RTKBT_ERR("play_deferred urb %p submission failed",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 				  urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 			kfree(urb->setup_packet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 			usb_unanchor_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 			usb_mark_last_busy(data->udev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 		usb_free_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 		/************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 		data->tx_in_flight++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) 	mdelay(URB_CANCELING_DELAY_MS);	// Added by Realtek
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) 	usb_scuttle_anchored_urbs(&data->deferred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) static int btusb_resume(struct usb_interface *intf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 	struct btusb_data *data = usb_get_intfdata(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 	struct hci_dev *hdev = data->hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 	if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 	/*******************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 	RTKBT_DBG("%s: data->suspend_count %d", __func__, data->suspend_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 	/* if intf->needs_binding is set, driver will be rebind.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) 	 * The probe will be called instead of resume */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 	/* if (!test_bit(HCI_RUNNING, &hdev->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 	 * 	RTKBT_DBG("btusb_resume-----bt is off,download patch");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) 	 * 	download_patch(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 	 * } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 	 * 	RTKBT_DBG("btusb_resume,----bt is on");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 	/*******************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 	if (--data->suspend_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 	if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 		err = btusb_submit_intr_urb(hdev, GFP_NOIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 		if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 			clear_bit(BTUSB_INTR_RUNNING, &data->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 			goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 	if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 		err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 		if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 			clear_bit(BTUSB_BULK_RUNNING, &data->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 			goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 		btusb_submit_bulk_urb(hdev, GFP_NOIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) 	if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 		if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) 			clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 			btusb_submit_isoc_urb(hdev, GFP_NOIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 	spin_lock_irq(&data->txlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) 	play_deferred(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 	clear_bit(BTUSB_SUSPENDING, &data->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) 	spin_unlock_irq(&data->txlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 	schedule_work(&data->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 	RTKBT_DBG("%s: data->suspend_count %d, done", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 		  data->suspend_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 	mdelay(URB_CANCELING_DELAY_MS);	// Added by Realtek
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 	usb_scuttle_anchored_urbs(&data->deferred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) //done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 	spin_lock_irq(&data->txlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 	clear_bit(BTUSB_SUSPENDING, &data->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 	spin_unlock_irq(&data->txlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 	RTKBT_DBG("%s: data->suspend_count %d, fail", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 		  data->suspend_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) static struct usb_driver btusb_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) 	.name = "rtk_btusb",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 	.probe = btusb_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 	.disconnect = btusb_disconnect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) #ifdef CONFIG_PM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 	.suspend = btusb_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) 	.resume = btusb_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) #ifdef RTKBT_SWITCH_PATCH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 	.reset_resume = btusb_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 	.id_table = btusb_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 	.supports_autosuspend = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) #if LINUX_VERSION_CODE > KERNEL_VERSION(3, 7, 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 	.disable_hub_initiated_lpm = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) static int __init btusb_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) 	RTKBT_DBG("Realtek Bluetooth USB driver ver %s", VERSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) #ifdef BTCOEX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) 	rtk_btcoex_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 	return usb_register(&btusb_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) static void __exit btusb_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 	RTKBT_DBG("rtk_btusb: btusb_exit");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 	usb_deregister(&btusb_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) #ifdef BTCOEX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) 	rtk_btcoex_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) module_init(btusb_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) module_exit(btusb_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) MODULE_AUTHOR("");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) MODULE_DESCRIPTION("Realtek Bluetooth USB driver ver " VERSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) MODULE_VERSION(VERSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) MODULE_LICENSE("GPL");