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 #define BTUSB_RPM (0 * USB_RPM) /* 1 SS enable; 0 SS disable */
87 #define BTUSB_WAKEUP_HOST 0 /* 1 enable; 0 disable */
88 #define BTUSB_MAX_ISOC_FRAMES 10
89 #define BTUSB_INTR_RUNNING 0
90 #define BTUSB_BULK_RUNNING 1
91 #define BTUSB_ISOC_RUNNING 2
92 #define BTUSB_SUSPENDING 3
93 #define BTUSB_DID_ISO_RESUME 4
95 #define HCI_CMD_READ_BD_ADDR 0x1009
96 #define HCI_VENDOR_CHANGE_BDRATE 0xfc17
97 #define HCI_VENDOR_READ_RTK_ROM_VERISION 0xfc6d
98 #define HCI_VENDOR_READ_LMP_VERISION 0x1001
100 #define ROM_LMP_8723a 0x1200
101 #define ROM_LMP_8723b 0x8723
102 #define ROM_LMP_8821a 0X8821
103 #define ROM_LMP_8761a 0X8761
105 /* signature: Realtek */
106 const uint8_t RTK_EPATCH_SIGNATURE[8] = {0x52,0x65,0x61,0x6C,0x74,0x65,0x63,0x68};
107 /* Extension Section IGNATURE:0x77FD0451 */
108 const uint8_t EXTENSION_SECTION_SIGNATURE[4] = {0x51,0x04,0xFD,0x77};
110 uint16_t project_id[] = {
116 struct rtk_eversion_evt {
119 } __attribute__ ((packed));
121 struct rtk_epatch_entry {
123 uint16_t patch_length;
124 uint32_t start_offset;
125 uint32_t coex_version;
126 uint32_t svn_version;
128 } __attribute__ ((packed));
131 uint8_t signature[8];
133 uint16_t number_of_total_patch;
134 struct rtk_epatch_entry entry[0];
135 } __attribute__ ((packed));
137 struct rtk_extension_entry {
141 } __attribute__ ((packed));
142 /* Realtek - For rtk_btusb driver end */
145 #define QUEUE_SIZE 500
147 /***************************************
148 ** Realtek - Integrate from bluetooth.h **
149 *****************************************/
150 /* Reserv for core and drivers use */
151 #define BT_SKB_RESERVE 8
169 #define bt_cb(skb) ((struct bt_skb_cb *)((skb)->cb))
171 static inline struct sk_buff *bt_skb_alloc(unsigned int len, gfp_t how)
175 if ((skb = alloc_skb(len + BT_SKB_RESERVE, how))) {
176 skb_reserve(skb, BT_SKB_RESERVE);
177 bt_cb(skb)->incoming = 0;
181 /* Realtek - Integrate from bluetooth.h end */
183 /***********************************
184 ** Realtek - Integrate from hci.h **
185 ***********************************/
186 #define HCI_MAX_ACL_SIZE 1024
187 #define HCI_MAX_SCO_SIZE 255
188 #define HCI_MAX_EVENT_SIZE 260
189 #define HCI_MAX_FRAME_SIZE (HCI_MAX_ACL_SIZE + 4)
192 #define HCI_VIRTUAL 0
200 /* HCI controller types */
201 #define HCI_BREDR 0x00
204 /* HCI device flags */
222 * BR/EDR and/or LE controller flags: the flags defined here should represent
223 * states from the controller.
246 #define HCI_COMMAND_PKT 0x01
247 #define HCI_ACLDATA_PKT 0x02
248 #define HCI_SCODATA_PKT 0x03
249 #define HCI_EVENT_PKT 0x04
250 #define HCI_VENDOR_PKT 0xff
252 #define HCI_MAX_NAME_LENGTH 248
253 #define HCI_MAX_EIR_LENGTH 240
255 #define HCI_OP_READ_LOCAL_VERSION 0x1001
256 struct hci_rp_read_local_version {
265 #define HCI_EV_CMD_COMPLETE 0x0e
266 struct hci_ev_cmd_complete {
271 /* ---- HCI Packet structures ---- */
272 #define HCI_COMMAND_HDR_SIZE 3
273 #define HCI_EVENT_HDR_SIZE 2
274 #define HCI_ACL_HDR_SIZE 4
275 #define HCI_SCO_HDR_SIZE 3
277 struct hci_command_hdr {
278 __le16 opcode; /* OCF & OGF */
282 struct hci_event_hdr {
288 __le16 handle; /* Handle & Flags(PB, BC) */
297 static inline struct hci_event_hdr *hci_event_hdr(const struct sk_buff *skb)
299 return (struct hci_event_hdr *) skb->data;
302 static inline struct hci_acl_hdr *hci_acl_hdr(const struct sk_buff *skb)
304 return (struct hci_acl_hdr *) skb->data;
307 static inline struct hci_sco_hdr *hci_sco_hdr(const struct sk_buff *skb)
309 return (struct hci_sco_hdr *) skb->data;
312 /* ---- HCI Ioctl requests structures ---- */
313 struct hci_dev_stats {
325 /* Realtek - Integrate from hci.h end */
327 /*****************************************
328 ** Realtek - Integrate from hci_core.h **
329 *****************************************/
330 struct hci_conn_hash {
331 struct list_head list;
332 unsigned int acl_num;
333 unsigned int sco_num;
337 #define HCI_MAX_SHORT_NAME_LENGTH 10
339 #define NUM_REASSEMBLY 4
349 struct sk_buff *reassembly[NUM_REASSEMBLY];
351 struct hci_conn_hash conn_hash;
353 struct hci_dev_stats stat;
355 #if LINUX_VERSION_CODE <= KERNEL_VERSION(3, 4, 0)
357 struct module *owner;
363 struct device *parent;
366 unsigned long dev_flags;
368 int (*open)(struct hci_dev *hdev);
369 int (*close)(struct hci_dev *hdev);
370 int (*flush)(struct hci_dev *hdev);
371 int (*send)(struct sk_buff *skb);
372 #if LINUX_VERSION_CODE <= KERNEL_VERSION(3, 4, 0)
373 void (*destruct)(struct hci_dev *hdev);
375 #if LINUX_VERSION_CODE > KERNEL_VERSION(3, 7, 1)
378 void (*notify)(struct hci_dev *hdev, unsigned int evt);
379 int (*ioctl)(struct hci_dev *hdev, unsigned int cmd, unsigned long arg);
382 #if LINUX_VERSION_CODE <= KERNEL_VERSION(3, 4, 0)
383 static inline struct hci_dev *__hci_dev_hold(struct hci_dev *d)
385 atomic_inc(&d->refcnt);
389 static inline void __hci_dev_put(struct hci_dev *d)
391 if (atomic_dec_and_test(&d->refcnt))
396 static inline void *hci_get_drvdata(struct hci_dev *hdev)
398 return dev_get_drvdata(&hdev->dev);
401 static inline int hci_set_drvdata(struct hci_dev *hdev, void *data)
403 return dev_set_drvdata(&hdev->dev, data);
406 #define SET_HCIDEV_DEV(hdev, pdev) ((hdev)->parent = (pdev))
407 /* Realtek - Integrate from hci_core.h end */
409 /* ----- HCI Commands ---- */
410 #define HCI_OP_INQUIRY 0x0401
411 #define HCI_OP_INQUIRY_CANCEL 0x0402
412 #define HCI_OP_EXIT_PERIODIC_INQ 0x0404
413 #define HCI_OP_CREATE_CONN 0x0405
414 #define HCI_OP_DISCONNECT 0x0406
415 #define HCI_OP_ADD_SCO 0x0407
416 #define HCI_OP_CREATE_CONN_CANCEL 0x0408
417 #define HCI_OP_ACCEPT_CONN_REQ 0x0409
418 #define HCI_OP_REJECT_CONN_REQ 0x040a
419 #define HCI_OP_LINK_KEY_REPLY 0x040b
420 #define HCI_OP_LINK_KEY_NEG_REPLY 0x040c
421 #define HCI_OP_PIN_CODE_REPLY 0x040d
422 #define HCI_OP_PIN_CODE_NEG_REPLY 0x040e
423 #define HCI_OP_CHANGE_CONN_PTYPE 0x040f
424 #define HCI_OP_AUTH_REQUESTED 0x0411
425 #define HCI_OP_SET_CONN_ENCRYPT 0x0413
426 #define HCI_OP_CHANGE_CONN_LINK_KEY 0x0415
427 #define HCI_OP_REMOTE_NAME_REQ 0x0419
428 #define HCI_OP_REMOTE_NAME_REQ_CANCEL 0x041a
429 #define HCI_OP_READ_REMOTE_FEATURES 0x041b
430 #define HCI_OP_READ_REMOTE_EXT_FEATURES 0x041c
431 #define HCI_OP_READ_REMOTE_VERSION 0x041d
432 #define HCI_OP_SETUP_SYNC_CONN 0x0428
433 #define HCI_OP_ACCEPT_SYNC_CONN_REQ 0x0429
434 #define HCI_OP_REJECT_SYNC_CONN_REQ 0x042a
435 #define HCI_OP_SNIFF_MODE 0x0803
436 #define HCI_OP_EXIT_SNIFF_MODE 0x0804
437 #define HCI_OP_ROLE_DISCOVERY 0x0809
438 #define HCI_OP_SWITCH_ROLE 0x080b
439 #define HCI_OP_READ_LINK_POLICY 0x080c
440 #define HCI_OP_WRITE_LINK_POLICY 0x080d
441 #define HCI_OP_READ_DEF_LINK_POLICY 0x080e
442 #define HCI_OP_WRITE_DEF_LINK_POLICY 0x080f
443 #define HCI_OP_SNIFF_SUBRATE 0x0811
444 #define HCI_OP_SET_EVENT_MASK 0x0c01
445 #define HCI_OP_RESET 0x0c03
446 #define HCI_OP_SET_EVENT_FLT 0x0c05
448 /* ----- HCI events---- */
449 #define HCI_OP_DISCONNECT 0x0406
450 #define HCI_EV_INQUIRY_COMPLETE 0x01
451 #define HCI_EV_INQUIRY_RESULT 0x02
452 #define HCI_EV_CONN_COMPLETE 0x03
453 #define HCI_EV_CONN_REQUEST 0x04
454 #define HCI_EV_DISCONN_COMPLETE 0x05
455 #define HCI_EV_AUTH_COMPLETE 0x06
456 #define HCI_EV_REMOTE_NAME 0x07
457 #define HCI_EV_ENCRYPT_CHANGE 0x08
458 #define HCI_EV_CHANGE_LINK_KEY_COMPLETE 0x09
460 #define HCI_EV_REMOTE_FEATURES 0x0b
461 #define HCI_EV_REMOTE_VERSION 0x0c
462 #define HCI_EV_QOS_SETUP_COMPLETE 0x0d
463 #define HCI_EV_CMD_COMPLETE 0x0e
464 #define HCI_EV_CMD_STATUS 0x0f
466 #define HCI_EV_ROLE_CHANGE 0x12
467 #define HCI_EV_NUM_COMP_PKTS 0x13
468 #define HCI_EV_MODE_CHANGE 0x14
469 #define HCI_EV_PIN_CODE_REQ 0x16
470 #define HCI_EV_LINK_KEY_REQ 0x17
471 #define HCI_EV_LINK_KEY_NOTIFY 0x18
472 #define HCI_EV_CLOCK_OFFSET 0x1c
473 #define HCI_EV_PKT_TYPE_CHANGE 0x1d
474 #define HCI_EV_PSCAN_REP_MODE 0x20
476 #define HCI_EV_INQUIRY_RESULT_WITH_RSSI 0x22
477 #define HCI_EV_REMOTE_EXT_FEATURES 0x23
478 #define HCI_EV_SYNC_CONN_COMPLETE 0x2c
479 #define HCI_EV_SYNC_CONN_CHANGED 0x2d
480 #define HCI_EV_SNIFF_SUBRATE 0x2e
481 #define HCI_EV_EXTENDED_INQUIRY_RESULT 0x2f
482 #define HCI_EV_IO_CAPA_REQUEST 0x31
483 #define HCI_EV_SIMPLE_PAIR_COMPLETE 0x36
484 #define HCI_EV_REMOTE_HOST_FEATURES 0x3d
486 #endif /* CONFIG_BLUEDROID */