3 * Realtek Bluetooth USB driver
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <linux/interrupt.h>
22 #include <linux/module.h>
23 #include <linux/slab.h>
24 #include <linux/types.h>
25 #include <linux/sched.h>
26 #include <linux/skbuff.h>
27 #include <linux/errno.h>
28 #include <linux/usb.h>
29 #include <linux/cdev.h>
30 #include <linux/device.h>
31 #include <linux/poll.h>
33 #include <linux/version.h>
34 #include <linux/pm_runtime.h>
35 #include <linux/firmware.h>
36 #include <linux/suspend.h>
38 #define CONFIG_BLUEDROID 1 /* bleuz 0, bluedroid 1 */
41 /* Some Android system may use standard Linux kernel, while
42 * standard Linux may also implement early suspend feature.
43 * So exclude earysuspend.h from CONFIG_BLUEDROID.
45 #ifdef CONFIG_HAS_EARLYSUSPEND
46 #include <linux/earlysuspend.h>
51 #include <net/bluetooth/bluetooth.h>
52 #include <net/bluetooth/hci_core.h>
53 #include <net/bluetooth/hci.h>
57 /***********************************
58 ** Realtek - For rtk_btusb driver **
59 ***********************************/
60 #define URB_CANCELING_DELAY_MS 10
61 /* when OS suspended, module is still powered,usb is not powered,
62 * this may set to 1, and must comply with special patch code.
64 #define CONFIG_RESET_RESUME 1
65 #define PRINT_CMD_EVENT 0
66 #define PRINT_ACL_DATA 0
67 #define PRINT_SCO_DATA 0
69 #define RTKBT_DBG_FLAG 0
72 #define RTKBT_DBG(fmt, arg...) printk(KERN_INFO "rtk_btusb: " fmt "\n" , ## arg)
74 #define RTKBT_DBG(fmt, arg...)
76 #define RTKBT_INFO(fmt, arg...) printk(KERN_INFO "rtk_btusb: " fmt "\n" , ## arg)
77 #define RTKBT_WARN(fmt, arg...) printk(KERN_WARNING "rtk_btusb: " fmt "\n" , ## arg)
78 #define RTKBT_ERR(fmt, arg...) printk(KERN_ERR "rtk_btusb: " fmt "\n" , ## arg)
81 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 33)
82 #define HDEV_BUS hdev->bus
85 #define HDEV_BUS hdev->type
89 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 38)
90 #define NUM_REASSEMBLY 3
93 #if LINUX_VERSION_CODE > KERNEL_VERSION(3, 4, 0)
94 #define GET_DRV_DATA(x) hci_get_drvdata(x)
96 #define GET_DRV_DATA(x) x->driver_data
99 #define SCO_NUM hdev->conn_hash.sco_num
102 #define BTUSB_RPM (0 * USB_RPM) /* 1 SS enable; 0 SS disable */
103 #define BTUSB_WAKEUP_HOST 0 /* 1 enable; 0 disable */
104 #define BTUSB_MAX_ISOC_FRAMES 10
105 #define BTUSB_INTR_RUNNING 0
106 #define BTUSB_BULK_RUNNING 1
107 #define BTUSB_ISOC_RUNNING 2
108 #define BTUSB_SUSPENDING 3
109 #define BTUSB_DID_ISO_RESUME 4
111 #define HCI_CMD_READ_BD_ADDR 0x1009
112 #define HCI_VENDOR_CHANGE_BDRATE 0xfc17
113 #define HCI_VENDOR_READ_RTK_ROM_VERISION 0xfc6d
114 #define HCI_VENDOR_READ_LMP_VERISION 0x1001
115 #define HCI_VENDOR_FORCE_RESET_AND_PATCHABLE 0xfc66
116 #define HCI_VENDOR_RESET 0x0C03
117 #define HCI_VENDOR_ADD_WAKE_UP_DEVICE 0xfc7b
118 #define HCI_VENDOR_REMOVE_WAKE_UP_DEVICE 0xfc7c
119 #define HCI_VENDOR_CLEAR_POWERON_LIST 0xfc7d
121 #define HCI_VENDOR_USB_DISC_HARDWARE_ERROR 0xFF
123 #define SET_WAKEUP_DEVICE_CONF "/data/misc/bluedroid/rtkbt_wakeup_ble.conf"
125 #define DRV_NORMAL_MODE 0
126 #define DRV_MP_MODE 1
127 int mp_drv_mode = 0; /* 1 Mptool Fw; 0 Normal Fw */
129 #define ROM_LMP_NONE 0x0000
130 #define ROM_LMP_8723a 0x1200
131 #define ROM_LMP_8723b 0x8723
132 #define ROM_LMP_8821a 0X8821
133 #define ROM_LMP_8761a 0X8761
134 #define ROM_LMP_8703a 0x8723
135 #define ROM_LMP_8763a 0x8763
136 #define ROM_LMP_8703b 0x8703
137 #define ROM_LMP_8723c 0x8703
138 #define ROM_LMP_8822b 0x8822
139 #define ROM_LMP_8723d 0x8723
140 #define ROM_LMP_8821c 0x8821
142 /* signature: Realtek */
143 const uint8_t RTK_EPATCH_SIGNATURE[8] = {0x52,0x65,0x61,0x6C,0x74,0x65,0x63,0x68};
144 /* Extension Section IGNATURE:0x77FD0451 */
145 const uint8_t EXTENSION_SECTION_SIGNATURE[4] = {0x51,0x04,0xFD,0x77};
147 uint16_t project_id[] = {
161 struct rtk_eversion_evt {
164 } __attribute__ ((packed));
166 /*modified by lamparten 1020*/
167 struct rtk_reset_evt {
169 } __attribute__ ((packed));
170 /*modified by lamparten 1020*/
172 struct rtk_localversion_evt {
175 uint16_t hci_revision;
177 uint16_t lmp_manufacture;
178 uint16_t lmp_subversion;
179 } __attribute__ ((packed));
181 struct rtk_epatch_entry {
183 uint16_t patch_length;
184 uint32_t start_offset;
185 uint32_t coex_version;
186 uint32_t svn_version;
188 } __attribute__ ((packed));
191 uint8_t signature[8];
193 uint16_t number_of_total_patch;
194 struct rtk_epatch_entry entry[0];
195 } __attribute__ ((packed));
197 struct rtk_extension_entry {
201 } __attribute__ ((packed));
203 struct rtk_bt_vendor_config_entry{
206 uint8_t entry_data[0];
207 } __attribute__ ((packed));
209 struct rtk_bt_vendor_config{
212 struct rtk_bt_vendor_config_entry entry[0];
213 } __attribute__ ((packed));
215 /* Realtek - For rtk_btusb driver end */
218 #define QUEUE_SIZE 500
220 /***************************************
221 ** Realtek - Integrate from bluetooth.h **
222 *****************************************/
223 /* Reserv for core and drivers use */
224 #define BT_SKB_RESERVE 8
242 #define bt_cb(skb) ((struct bt_skb_cb *)((skb)->cb))
244 static inline struct sk_buff *bt_skb_alloc(unsigned int len, gfp_t how)
248 if ((skb = alloc_skb(len + BT_SKB_RESERVE, how))) {
249 skb_reserve(skb, BT_SKB_RESERVE);
250 bt_cb(skb)->incoming = 0;
254 /* Realtek - Integrate from bluetooth.h end */
256 /***********************************
257 ** Realtek - Integrate from hci.h **
258 ***********************************/
259 #define HCI_MAX_ACL_SIZE 1024
260 #define HCI_MAX_SCO_SIZE 255
261 #define HCI_MAX_EVENT_SIZE 260
262 #define HCI_MAX_FRAME_SIZE (HCI_MAX_ACL_SIZE + 4)
265 #define HCI_VIRTUAL 0
273 /* HCI controller types */
274 #define HCI_BREDR 0x00
277 /* HCI device flags */
295 * BR/EDR and/or LE controller flags: the flags defined here should represent
296 * states from the controller.
319 #define HCI_COMMAND_PKT 0x01
320 #define HCI_ACLDATA_PKT 0x02
321 #define HCI_SCODATA_PKT 0x03
322 #define HCI_EVENT_PKT 0x04
323 #define HCI_VENDOR_PKT 0xff
325 #define HCI_MAX_NAME_LENGTH 248
326 #define HCI_MAX_EIR_LENGTH 240
328 #define HCI_OP_READ_LOCAL_VERSION 0x1001
329 struct hci_rp_read_local_version {
338 #define HCI_EV_CMD_COMPLETE 0x0e
339 struct hci_ev_cmd_complete {
344 /* ---- HCI Packet structures ---- */
345 #define HCI_COMMAND_HDR_SIZE 3
346 #define HCI_EVENT_HDR_SIZE 2
347 #define HCI_ACL_HDR_SIZE 4
348 #define HCI_SCO_HDR_SIZE 3
350 struct hci_command_hdr {
351 __le16 opcode; /* OCF & OGF */
355 struct hci_event_hdr {
361 __le16 handle; /* Handle & Flags(PB, BC) */
370 static inline struct hci_event_hdr *hci_event_hdr(const struct sk_buff *skb)
372 return (struct hci_event_hdr *) skb->data;
375 static inline struct hci_acl_hdr *hci_acl_hdr(const struct sk_buff *skb)
377 return (struct hci_acl_hdr *) skb->data;
380 static inline struct hci_sco_hdr *hci_sco_hdr(const struct sk_buff *skb)
382 return (struct hci_sco_hdr *) skb->data;
385 /* ---- HCI Ioctl requests structures ---- */
386 struct hci_dev_stats {
398 /* Realtek - Integrate from hci.h end */
400 /*****************************************
401 ** Realtek - Integrate from hci_core.h **
402 *****************************************/
403 struct hci_conn_hash {
404 struct list_head list;
405 unsigned int acl_num;
406 unsigned int sco_num;
410 #define HCI_MAX_SHORT_NAME_LENGTH 10
412 #define NUM_REASSEMBLY 4
422 struct sk_buff *reassembly[NUM_REASSEMBLY];
424 struct hci_conn_hash conn_hash;
426 struct hci_dev_stats stat;
428 #if LINUX_VERSION_CODE <= KERNEL_VERSION(3, 4, 0)
430 struct module *owner;
436 struct device *parent;
439 unsigned long dev_flags;
441 int (*open)(struct hci_dev *hdev);
442 int (*close)(struct hci_dev *hdev);
443 int (*flush)(struct hci_dev *hdev);
444 int (*send)(struct sk_buff *skb);
445 #if LINUX_VERSION_CODE <= KERNEL_VERSION(3, 4, 0)
446 void (*destruct)(struct hci_dev *hdev);
448 #if LINUX_VERSION_CODE > KERNEL_VERSION(3, 7, 1)
451 void (*notify)(struct hci_dev *hdev, unsigned int evt);
452 int (*ioctl)(struct hci_dev *hdev, unsigned int cmd, unsigned long arg);
455 #if LINUX_VERSION_CODE <= KERNEL_VERSION(3, 4, 0)
456 static inline struct hci_dev *__hci_dev_hold(struct hci_dev *d)
458 atomic_inc(&d->refcnt);
462 static inline void __hci_dev_put(struct hci_dev *d)
464 if (atomic_dec_and_test(&d->refcnt))
469 static inline void *hci_get_drvdata(struct hci_dev *hdev)
471 return dev_get_drvdata(&hdev->dev);
474 static inline void hci_set_drvdata(struct hci_dev *hdev, void *data)
476 dev_set_drvdata(&hdev->dev, data);
479 #define SET_HCIDEV_DEV(hdev, pdev) ((hdev)->parent = (pdev))
480 /* Realtek - Integrate from hci_core.h end */
482 /* ----- HCI Commands ---- */
483 #define HCI_OP_INQUIRY 0x0401
484 #define HCI_OP_INQUIRY_CANCEL 0x0402
485 #define HCI_OP_EXIT_PERIODIC_INQ 0x0404
486 #define HCI_OP_CREATE_CONN 0x0405
487 #define HCI_OP_DISCONNECT 0x0406
488 #define HCI_OP_ADD_SCO 0x0407
489 #define HCI_OP_CREATE_CONN_CANCEL 0x0408
490 #define HCI_OP_ACCEPT_CONN_REQ 0x0409
491 #define HCI_OP_REJECT_CONN_REQ 0x040a
492 #define HCI_OP_LINK_KEY_REPLY 0x040b
493 #define HCI_OP_LINK_KEY_NEG_REPLY 0x040c
494 #define HCI_OP_PIN_CODE_REPLY 0x040d
495 #define HCI_OP_PIN_CODE_NEG_REPLY 0x040e
496 #define HCI_OP_CHANGE_CONN_PTYPE 0x040f
497 #define HCI_OP_AUTH_REQUESTED 0x0411
498 #define HCI_OP_SET_CONN_ENCRYPT 0x0413
499 #define HCI_OP_CHANGE_CONN_LINK_KEY 0x0415
500 #define HCI_OP_REMOTE_NAME_REQ 0x0419
501 #define HCI_OP_REMOTE_NAME_REQ_CANCEL 0x041a
502 #define HCI_OP_READ_REMOTE_FEATURES 0x041b
503 #define HCI_OP_READ_REMOTE_EXT_FEATURES 0x041c
504 #define HCI_OP_READ_REMOTE_VERSION 0x041d
505 #define HCI_OP_SETUP_SYNC_CONN 0x0428
506 #define HCI_OP_ACCEPT_SYNC_CONN_REQ 0x0429
507 #define HCI_OP_REJECT_SYNC_CONN_REQ 0x042a
508 #define HCI_OP_SNIFF_MODE 0x0803
509 #define HCI_OP_EXIT_SNIFF_MODE 0x0804
510 #define HCI_OP_ROLE_DISCOVERY 0x0809
511 #define HCI_OP_SWITCH_ROLE 0x080b
512 #define HCI_OP_READ_LINK_POLICY 0x080c
513 #define HCI_OP_WRITE_LINK_POLICY 0x080d
514 #define HCI_OP_READ_DEF_LINK_POLICY 0x080e
515 #define HCI_OP_WRITE_DEF_LINK_POLICY 0x080f
516 #define HCI_OP_SNIFF_SUBRATE 0x0811
517 #define HCI_OP_SET_EVENT_MASK 0x0c01
518 #define HCI_OP_RESET 0x0c03
519 #define HCI_OP_SET_EVENT_FLT 0x0c05
520 #define HCI_OP_Write_Extended_Inquiry_Response 0x0c52
522 /* ----- HCI events---- */
523 #define HCI_OP_DISCONNECT 0x0406
524 #define HCI_EV_INQUIRY_COMPLETE 0x01
525 #define HCI_EV_INQUIRY_RESULT 0x02
526 #define HCI_EV_CONN_COMPLETE 0x03
527 #define HCI_EV_CONN_REQUEST 0x04
528 #define HCI_EV_DISCONN_COMPLETE 0x05
529 #define HCI_EV_AUTH_COMPLETE 0x06
530 #define HCI_EV_REMOTE_NAME 0x07
531 #define HCI_EV_ENCRYPT_CHANGE 0x08
532 #define HCI_EV_CHANGE_LINK_KEY_COMPLETE 0x09
534 #define HCI_EV_REMOTE_FEATURES 0x0b
535 #define HCI_EV_REMOTE_VERSION 0x0c
536 #define HCI_EV_QOS_SETUP_COMPLETE 0x0d
537 #define HCI_EV_CMD_COMPLETE 0x0e
538 #define HCI_EV_CMD_STATUS 0x0f
540 #define HCI_EV_ROLE_CHANGE 0x12
541 #define HCI_EV_NUM_COMP_PKTS 0x13
542 #define HCI_EV_MODE_CHANGE 0x14
543 #define HCI_EV_PIN_CODE_REQ 0x16
544 #define HCI_EV_LINK_KEY_REQ 0x17
545 #define HCI_EV_LINK_KEY_NOTIFY 0x18
546 #define HCI_EV_CLOCK_OFFSET 0x1c
547 #define HCI_EV_PKT_TYPE_CHANGE 0x1d
548 #define HCI_EV_PSCAN_REP_MODE 0x20
550 #define HCI_EV_INQUIRY_RESULT_WITH_RSSI 0x22
551 #define HCI_EV_REMOTE_EXT_FEATURES 0x23
552 #define HCI_EV_SYNC_CONN_COMPLETE 0x2c
553 #define HCI_EV_SYNC_CONN_CHANGED 0x2d
554 #define HCI_EV_SNIFF_SUBRATE 0x2e
555 #define HCI_EV_EXTENDED_INQUIRY_RESULT 0x2f
556 #define HCI_EV_IO_CAPA_REQUEST 0x31
557 #define HCI_EV_SIMPLE_PAIR_COMPLETE 0x36
558 #define HCI_EV_REMOTE_HOST_FEATURES 0x3d
560 #define CONFIG_MAC_OFFSET_GEN_1_2 (0x3C) //MAC's OFFSET in config/efuse for realtek generation 1~2 bluetooth chip
561 #define CONFIG_MAC_OFFSET_GEN_3PLUS (0x44) //MAC's OFFSET in config/efuse for rtk generation 3+ bluetooth chip
563 /*******************************
565 ********************************/
566 #define CMD_CMP_EVT 0x0e
569 #define PATCH_SEG_MAX 252
570 #define DATA_END 0x80
571 #define DOWNLOAD_OPCODE 0xfc20
572 #define BTOFF_OPCODE 0xfc28
575 #define CMD_HDR_LEN sizeof(struct hci_command_hdr)
576 #define EVT_HDR_LEN sizeof(struct hci_event_hdr)
577 #define CMD_CMP_LEN sizeof(struct hci_ev_cmd_complete)
578 #define MAX_PATCH_SIZE_24K (1024*24)
579 #define MAX_PATCH_SIZE_40K (1024*40)
591 uint16_t lmp_sub_default;
600 uint32_t max_patch_size;
604 struct usb_interface *intf;
605 struct usb_device *udev;
606 patch_info *patch_entry;
607 int pipe_in, pipe_out;
610 struct hci_command_hdr *cmd_hdr;
611 struct hci_event_hdr *evt_hdr;
612 struct hci_ev_cmd_complete *cmd_cmp;
613 uint8_t *req_para, *rsp_para;
621 uint8_t data[PATCH_SEG_MAX];
622 } __attribute__((packed)) download_cp;
627 } __attribute__((packed)) download_rp;
631 //Define ioctl cmd the same as HCIDEVUP in the kernel
632 #define DOWN_FW_CFG _IOW('H', 201, int)
636 #include <linux/fs.h>
637 #define BDADDR_FILE "/data/misc/bluetooth/bdaddr"
638 #define FACTORY_BT_BDADDR_STORAGE_LEN 17
640 static inline int getmacaddr(uint8_t * vnd_local_bd_addr)
642 struct file *bdaddr_file;
644 char buf[FACTORY_BT_BDADDR_STORAGE_LEN];
646 memset(buf, 0, FACTORY_BT_BDADDR_STORAGE_LEN);
647 bdaddr_file = filp_open(BDADDR_FILE, O_RDONLY, 0);
648 if (IS_ERR(bdaddr_file)){
649 RTKBT_INFO("No Mac Config for BT\n");
652 oldfs = get_fs(); set_fs(KERNEL_DS);
653 bdaddr_file->f_op->llseek(bdaddr_file, 0, 0);
654 bdaddr_file->f_op->read(bdaddr_file, buf, FACTORY_BT_BDADDR_STORAGE_LEN, &bdaddr_file->f_pos);
655 for (i = 0; i < 6; i++) {
659 buf[3*i] -=('a'-'A'); //change a to A
660 buf[3*i] -= ('A'-'9'-1);
665 buf[3*i+1] -=('a'-'A'); //change a to A
666 buf[3*i+1] -= ('A'-'9'-1);
668 vnd_local_bd_addr[5-i] = ((uint8_t)buf[3*i]-'0')*16 + ((uint8_t)buf[3*i+1]-'0');
671 filp_close(bdaddr_file, NULL);
675 static inline int getAltSettings(patch_info *patch_entry, unsigned short *offset, int max_group_cnt)
679 offset[n++] = patch_entry->mac_offset;
681 //sample code, add special settings
687 static inline int getAltSettingVal(patch_info *patch_entry, unsigned short offset, unsigned char * val)
694 //sample code, add special settings
708 if((patch_entry)&&(offset == patch_entry->mac_offset)&&(res == 0))
710 if(getmacaddr(val) == 0){
711 RTKBT_INFO("MAC: %02x:%02x:%02x:%02x:%02x:%02x", val[5], val[4], val[3], val[2], val[1], val[0]);
718 #endif /* CONFIG_BLUEDROID */