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 */
40 /* Some Android system may use standard Linux kernel, while
41 * standard Linux may also implement early suspend feature.
42 * So exclude earysuspend.h from CONFIG_BLUEDROID.
44 #ifdef CONFIG_HAS_EARLYSUSPEND
45 #include <linux/earlysuspend.h>
50 #include <net/bluetooth/bluetooth.h>
51 #include <net/bluetooth/hci_core.h>
52 #include <net/bluetooth/hci.h>
56 /***********************************
57 ** Realtek - For rtk_btusb driver **
58 ***********************************/
59 #define URB_CANCELING_DELAY_MS 10
60 /* when OS suspended, module is still powered,usb is not powered,
61 * this may set to 1, and must comply with special patch code.
63 #define CONFIG_RESET_RESUME 0
64 #define PRINT_CMD_EVENT 0
65 #define PRINT_ACL_DATA 0
66 #define PRINT_SCO_DATA 0
68 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 33)
69 #define HDEV_BUS hdev->bus
72 #define HDEV_BUS hdev->type
76 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 38)
77 #define NUM_REASSEMBLY 3
80 #if LINUX_VERSION_CODE > KERNEL_VERSION(3, 4, 0)
81 #define GET_DRV_DATA(x) hci_get_drvdata(x)
83 #define GET_DRV_DATA(x) x->driver_data
86 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 13, 0)
87 #define SCO_NUM hdev->conn_hash.sco_num
89 #define SCO_NUM hci_conn_num(hdev, SCO_LINK)
92 #define BTUSB_RPM (0 * USB_RPM) /* 1 SS enable; 0 SS disable */
93 #define BTUSB_WAKEUP_HOST 0 /* 1 enable; 0 disable */
94 #define BTUSB_MAX_ISOC_FRAMES 10
95 #define BTUSB_INTR_RUNNING 0
96 #define BTUSB_BULK_RUNNING 1
97 #define BTUSB_ISOC_RUNNING 2
98 #define BTUSB_SUSPENDING 3
99 #define BTUSB_DID_ISO_RESUME 4
101 #define HCI_CMD_READ_BD_ADDR 0x1009
102 #define HCI_VENDOR_CHANGE_BDRATE 0xfc17
103 #define HCI_VENDOR_READ_RTK_ROM_VERISION 0xfc6d
104 #define HCI_VENDOR_READ_LMP_VERISION 0x1001
106 #define ROM_LMP_8723a 0x1200
107 #define ROM_LMP_8723b 0x8723
108 #define ROM_LMP_8821a 0X8821
109 #define ROM_LMP_8761a 0X8761
111 /* signature: Realtek */
112 const uint8_t RTK_EPATCH_SIGNATURE[8] = {0x52,0x65,0x61,0x6C,0x74,0x65,0x63,0x68};
113 /* Extension Section IGNATURE:0x77FD0451 */
114 const uint8_t EXTENSION_SECTION_SIGNATURE[4] = {0x51,0x04,0xFD,0x77};
116 uint16_t project_id[] = {
122 struct rtk_eversion_evt {
125 } __attribute__ ((packed));
127 struct rtk_epatch_entry {
129 uint16_t patch_length;
130 uint32_t start_offset;
131 uint32_t coex_version;
132 uint32_t svn_version;
134 } __attribute__ ((packed));
137 uint8_t signature[8];
139 uint16_t number_of_total_patch;
140 struct rtk_epatch_entry entry[0];
141 } __attribute__ ((packed));
143 struct rtk_extension_entry {
147 } __attribute__ ((packed));
148 /* Realtek - For rtk_btusb driver end */
151 #define QUEUE_SIZE 500
153 /***************************************
154 ** Realtek - Integrate from bluetooth.h **
155 *****************************************/
156 /* Reserv for core and drivers use */
157 #define BT_SKB_RESERVE 8
175 #define bt_cb(skb) ((struct bt_skb_cb *)((skb)->cb))
177 static inline struct sk_buff *bt_skb_alloc(unsigned int len, gfp_t how)
181 if ((skb = alloc_skb(len + BT_SKB_RESERVE, how))) {
182 skb_reserve(skb, BT_SKB_RESERVE);
183 bt_cb(skb)->incoming = 0;
187 /* Realtek - Integrate from bluetooth.h end */
189 /***********************************
190 ** Realtek - Integrate from hci.h **
191 ***********************************/
192 #define HCI_MAX_ACL_SIZE 1024
193 #define HCI_MAX_SCO_SIZE 255
194 #define HCI_MAX_EVENT_SIZE 260
195 #define HCI_MAX_FRAME_SIZE (HCI_MAX_ACL_SIZE + 4)
198 #define HCI_VIRTUAL 0
206 /* HCI controller types */
207 #define HCI_BREDR 0x00
210 /* HCI device flags */
228 * BR/EDR and/or LE controller flags: the flags defined here should represent
229 * states from the controller.
252 #define HCI_COMMAND_PKT 0x01
253 #define HCI_ACLDATA_PKT 0x02
254 #define HCI_SCODATA_PKT 0x03
255 #define HCI_EVENT_PKT 0x04
256 #define HCI_VENDOR_PKT 0xff
258 #define HCI_MAX_NAME_LENGTH 248
259 #define HCI_MAX_EIR_LENGTH 240
261 #define HCI_OP_READ_LOCAL_VERSION 0x1001
262 struct hci_rp_read_local_version {
271 #define HCI_EV_CMD_COMPLETE 0x0e
272 struct hci_ev_cmd_complete {
277 /* ---- HCI Packet structures ---- */
278 #define HCI_COMMAND_HDR_SIZE 3
279 #define HCI_EVENT_HDR_SIZE 2
280 #define HCI_ACL_HDR_SIZE 4
281 #define HCI_SCO_HDR_SIZE 3
283 struct hci_command_hdr {
284 __le16 opcode; /* OCF & OGF */
288 struct hci_event_hdr {
294 __le16 handle; /* Handle & Flags(PB, BC) */
303 static inline struct hci_event_hdr *hci_event_hdr(const struct sk_buff *skb)
305 return (struct hci_event_hdr *) skb->data;
308 static inline struct hci_acl_hdr *hci_acl_hdr(const struct sk_buff *skb)
310 return (struct hci_acl_hdr *) skb->data;
313 static inline struct hci_sco_hdr *hci_sco_hdr(const struct sk_buff *skb)
315 return (struct hci_sco_hdr *) skb->data;
318 /* ---- HCI Ioctl requests structures ---- */
319 struct hci_dev_stats {
331 /* Realtek - Integrate from hci.h end */
333 /*****************************************
334 ** Realtek - Integrate from hci_core.h **
335 *****************************************/
336 struct hci_conn_hash {
337 struct list_head list;
338 unsigned int acl_num;
339 unsigned int sco_num;
343 #define HCI_MAX_SHORT_NAME_LENGTH 10
345 #define NUM_REASSEMBLY 4
355 struct sk_buff *reassembly[NUM_REASSEMBLY];
357 struct hci_conn_hash conn_hash;
359 struct hci_dev_stats stat;
361 #if LINUX_VERSION_CODE <= KERNEL_VERSION(3, 4, 0)
363 struct module *owner;
369 struct device *parent;
372 unsigned long dev_flags;
374 int (*open)(struct hci_dev *hdev);
375 int (*close)(struct hci_dev *hdev);
376 int (*flush)(struct hci_dev *hdev);
377 int (*send)(struct sk_buff *skb);
378 #if LINUX_VERSION_CODE <= KERNEL_VERSION(3, 4, 0)
379 void (*destruct)(struct hci_dev *hdev);
381 #if LINUX_VERSION_CODE > KERNEL_VERSION(3, 7, 1)
384 void (*notify)(struct hci_dev *hdev, unsigned int evt);
385 int (*ioctl)(struct hci_dev *hdev, unsigned int cmd, unsigned long arg);
388 #if LINUX_VERSION_CODE <= KERNEL_VERSION(3, 4, 0)
389 static inline struct hci_dev *__hci_dev_hold(struct hci_dev *d)
391 atomic_inc(&d->refcnt);
395 static inline void __hci_dev_put(struct hci_dev *d)
397 if (atomic_dec_and_test(&d->refcnt))
402 static inline void *hci_get_drvdata(struct hci_dev *hdev)
404 return dev_get_drvdata(&hdev->dev);
407 static inline int hci_set_drvdata(struct hci_dev *hdev, void *data)
409 return dev_set_drvdata(&hdev->dev, data);
412 #define SET_HCIDEV_DEV(hdev, pdev) ((hdev)->parent = (pdev))
413 /* Realtek - Integrate from hci_core.h end */
415 /* ----- HCI Commands ---- */
416 #define HCI_OP_INQUIRY 0x0401
417 #define HCI_OP_INQUIRY_CANCEL 0x0402
418 #define HCI_OP_EXIT_PERIODIC_INQ 0x0404
419 #define HCI_OP_CREATE_CONN 0x0405
420 #define HCI_OP_DISCONNECT 0x0406
421 #define HCI_OP_ADD_SCO 0x0407
422 #define HCI_OP_CREATE_CONN_CANCEL 0x0408
423 #define HCI_OP_ACCEPT_CONN_REQ 0x0409
424 #define HCI_OP_REJECT_CONN_REQ 0x040a
425 #define HCI_OP_LINK_KEY_REPLY 0x040b
426 #define HCI_OP_LINK_KEY_NEG_REPLY 0x040c
427 #define HCI_OP_PIN_CODE_REPLY 0x040d
428 #define HCI_OP_PIN_CODE_NEG_REPLY 0x040e
429 #define HCI_OP_CHANGE_CONN_PTYPE 0x040f
430 #define HCI_OP_AUTH_REQUESTED 0x0411
431 #define HCI_OP_SET_CONN_ENCRYPT 0x0413
432 #define HCI_OP_CHANGE_CONN_LINK_KEY 0x0415
433 #define HCI_OP_REMOTE_NAME_REQ 0x0419
434 #define HCI_OP_REMOTE_NAME_REQ_CANCEL 0x041a
435 #define HCI_OP_READ_REMOTE_FEATURES 0x041b
436 #define HCI_OP_READ_REMOTE_EXT_FEATURES 0x041c
437 #define HCI_OP_READ_REMOTE_VERSION 0x041d
438 #define HCI_OP_SETUP_SYNC_CONN 0x0428
439 #define HCI_OP_ACCEPT_SYNC_CONN_REQ 0x0429
440 #define HCI_OP_REJECT_SYNC_CONN_REQ 0x042a
441 #define HCI_OP_SNIFF_MODE 0x0803
442 #define HCI_OP_EXIT_SNIFF_MODE 0x0804
443 #define HCI_OP_ROLE_DISCOVERY 0x0809
444 #define HCI_OP_SWITCH_ROLE 0x080b
445 #define HCI_OP_READ_LINK_POLICY 0x080c
446 #define HCI_OP_WRITE_LINK_POLICY 0x080d
447 #define HCI_OP_READ_DEF_LINK_POLICY 0x080e
448 #define HCI_OP_WRITE_DEF_LINK_POLICY 0x080f
449 #define HCI_OP_SNIFF_SUBRATE 0x0811
450 #define HCI_OP_SET_EVENT_MASK 0x0c01
451 #define HCI_OP_RESET 0x0c03
452 #define HCI_OP_SET_EVENT_FLT 0x0c05
454 /* ----- HCI events---- */
455 #define HCI_OP_DISCONNECT 0x0406
456 #define HCI_EV_INQUIRY_COMPLETE 0x01
457 #define HCI_EV_INQUIRY_RESULT 0x02
458 #define HCI_EV_CONN_COMPLETE 0x03
459 #define HCI_EV_CONN_REQUEST 0x04
460 #define HCI_EV_DISCONN_COMPLETE 0x05
461 #define HCI_EV_AUTH_COMPLETE 0x06
462 #define HCI_EV_REMOTE_NAME 0x07
463 #define HCI_EV_ENCRYPT_CHANGE 0x08
464 #define HCI_EV_CHANGE_LINK_KEY_COMPLETE 0x09
466 #define HCI_EV_REMOTE_FEATURES 0x0b
467 #define HCI_EV_REMOTE_VERSION 0x0c
468 #define HCI_EV_QOS_SETUP_COMPLETE 0x0d
469 #define HCI_EV_CMD_COMPLETE 0x0e
470 #define HCI_EV_CMD_STATUS 0x0f
472 #define HCI_EV_ROLE_CHANGE 0x12
473 #define HCI_EV_NUM_COMP_PKTS 0x13
474 #define HCI_EV_MODE_CHANGE 0x14
475 #define HCI_EV_PIN_CODE_REQ 0x16
476 #define HCI_EV_LINK_KEY_REQ 0x17
477 #define HCI_EV_LINK_KEY_NOTIFY 0x18
478 #define HCI_EV_CLOCK_OFFSET 0x1c
479 #define HCI_EV_PKT_TYPE_CHANGE 0x1d
480 #define HCI_EV_PSCAN_REP_MODE 0x20
482 #define HCI_EV_INQUIRY_RESULT_WITH_RSSI 0x22
483 #define HCI_EV_REMOTE_EXT_FEATURES 0x23
484 #define HCI_EV_SYNC_CONN_COMPLETE 0x2c
485 #define HCI_EV_SYNC_CONN_CHANGED 0x2d
486 #define HCI_EV_SNIFF_SUBRATE 0x2e
487 #define HCI_EV_EXTENDED_INQUIRY_RESULT 0x2f
488 #define HCI_EV_IO_CAPA_REQUEST 0x31
489 #define HCI_EV_SIMPLE_PAIR_COMPLETE 0x36
490 #define HCI_EV_REMOTE_HOST_FEATURES 0x3d
492 #endif /* CONFIG_BLUEDROID */