mfd: fusb302: change to host when connect type-c to standard-a cable
[firefly-linux-kernel-4.4.55.git] / drivers / bluetooth / rtk_btusb.h
1 /*
2  *
3  *  Realtek Bluetooth USB driver
4  *
5  *
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.
10  *
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.
15  *
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
19  *
20  */
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>
32
33 #include <linux/version.h>
34 #include <linux/pm_runtime.h>
35 #include <linux/firmware.h>
36 #include <linux/suspend.h>
37
38 #define CONFIG_BLUEDROID                1 /* bleuz 0, bluedroid 1 */
39
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.
43  */
44 #ifdef CONFIG_HAS_EARLYSUSPEND
45 #include <linux/earlysuspend.h>
46 #endif
47
48 #if CONFIG_BLUEDROID
49 #else
50 #include <net/bluetooth/bluetooth.h>
51 #include <net/bluetooth/hci_core.h>
52 #include <net/bluetooth/hci.h>
53 #endif
54
55
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.
62  */
63 #define CONFIG_RESET_RESUME             0
64 #define PRINT_CMD_EVENT                 0
65 #define PRINT_ACL_DATA                  0
66 #define PRINT_SCO_DATA                  0
67
68 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 33)
69 #define HDEV_BUS                hdev->bus
70 #define USB_RPM                 1
71 #else
72 #define HDEV_BUS                hdev->type
73 #define USB_RPM                 0
74 #endif
75
76 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 38)
77 #define NUM_REASSEMBLY 3
78 #endif
79
80 #if LINUX_VERSION_CODE > KERNEL_VERSION(3, 4, 0)
81 #define GET_DRV_DATA(x)         hci_get_drvdata(x)
82 #else
83 #define GET_DRV_DATA(x)         x->driver_data
84 #endif
85
86 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 13, 0)
87 #define SCO_NUM    hdev->conn_hash.sco_num
88 #else
89 #define SCO_NUM     hci_conn_num(hdev, SCO_LINK)
90 #endif
91
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
100
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
105
106 #define ROM_LMP_8723a               0x1200
107 #define ROM_LMP_8723b               0x8723
108 #define ROM_LMP_8821a               0X8821
109 #define ROM_LMP_8761a               0X8761
110
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};
115
116 uint16_t project_id[] = {
117         ROM_LMP_8723a,
118         ROM_LMP_8723b,
119         ROM_LMP_8821a,
120         ROM_LMP_8761a
121 };
122 struct rtk_eversion_evt {
123         uint8_t status;
124         uint8_t version;
125 } __attribute__ ((packed));
126
127 struct rtk_epatch_entry {
128         uint16_t chip_id;
129         uint16_t patch_length;
130         uint32_t start_offset;
131         uint32_t coex_version;
132         uint32_t svn_version;
133         uint32_t fw_version;
134 } __attribute__ ((packed));
135
136 struct rtk_epatch {
137         uint8_t signature[8];
138         uint32_t fw_version;
139         uint16_t number_of_total_patch;
140         struct rtk_epatch_entry entry[0];
141 } __attribute__ ((packed));
142
143 struct rtk_extension_entry {
144         uint8_t opcode;
145         uint8_t length;
146         uint8_t *data;
147 } __attribute__ ((packed));
148 /* Realtek - For rtk_btusb driver end */
149
150 #if CONFIG_BLUEDROID
151 #define QUEUE_SIZE 500
152
153 /***************************************
154 ** Realtek - Integrate from bluetooth.h **
155 *****************************************/
156 /* Reserv for core and drivers use */
157 #define BT_SKB_RESERVE  8
158
159 /* BD Address */
160 typedef struct {
161         __u8 b[6];
162 } __packed bdaddr_t;
163
164 /* Skb helpers */
165 struct bt_skb_cb {
166         __u8 pkt_type;
167         __u8 incoming;
168         __u16 expect;
169         __u16 tx_seq;
170         __u8 retries;
171         __u8 sar;
172         __u8 force_active;
173 };
174
175 #define bt_cb(skb) ((struct bt_skb_cb *)((skb)->cb))
176
177 static inline struct sk_buff *bt_skb_alloc(unsigned int len, gfp_t how)
178 {
179         struct sk_buff *skb;
180
181         if ((skb = alloc_skb(len + BT_SKB_RESERVE, how))) {
182                 skb_reserve(skb, BT_SKB_RESERVE);
183                 bt_cb(skb)->incoming  = 0;
184         }
185         return skb;
186 }
187 /* Realtek - Integrate from bluetooth.h end */
188
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)
196
197 /* HCI bus types */
198 #define HCI_VIRTUAL     0
199 #define HCI_USB         1
200 #define HCI_PCCARD      2
201 #define HCI_UART        3
202 #define HCI_RS232       4
203 #define HCI_PCI         5
204 #define HCI_SDIO        6
205
206 /* HCI controller types */
207 #define HCI_BREDR       0x00
208 #define HCI_AMP         0x01
209
210 /* HCI device flags */
211 enum {
212         HCI_UP,
213         HCI_INIT,
214         HCI_RUNNING,
215
216         HCI_PSCAN,
217         HCI_ISCAN,
218         HCI_AUTH,
219         HCI_ENCRYPT,
220         HCI_INQUIRY,
221
222         HCI_RAW,
223
224         HCI_RESET,
225 };
226
227 /*
228  * BR/EDR and/or LE controller flags: the flags defined here should represent
229  * states from the controller.
230  */
231 enum {
232         HCI_SETUP,
233         HCI_AUTO_OFF,
234         HCI_MGMT,
235         HCI_PAIRABLE,
236         HCI_SERVICE_CACHE,
237         HCI_LINK_KEYS,
238         HCI_DEBUG_KEYS,
239         HCI_UNREGISTER,
240
241         HCI_LE_SCAN,
242         HCI_SSP_ENABLED,
243         HCI_HS_ENABLED,
244         HCI_LE_ENABLED,
245         HCI_CONNECTABLE,
246         HCI_DISCOVERABLE,
247         HCI_LINK_SECURITY,
248         HCI_PENDING_CLASS,
249 };
250
251 /* HCI data types */
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
257
258 #define HCI_MAX_NAME_LENGTH             248
259 #define HCI_MAX_EIR_LENGTH              240
260
261 #define HCI_OP_READ_LOCAL_VERSION       0x1001
262 struct hci_rp_read_local_version {
263         __u8     status;
264         __u8     hci_ver;
265         __le16   hci_rev;
266         __u8     lmp_ver;
267         __le16   manufacturer;
268         __le16   lmp_subver;
269 } __packed;
270
271 #define HCI_EV_CMD_COMPLETE             0x0e
272 struct hci_ev_cmd_complete {
273         __u8     ncmd;
274         __le16   opcode;
275 } __packed;
276
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
282
283 struct hci_command_hdr {
284         __le16  opcode;         /* OCF & OGF */
285         __u8    plen;
286 } __packed;
287
288 struct hci_event_hdr {
289         __u8    evt;
290         __u8    plen;
291 } __packed;
292
293 struct hci_acl_hdr {
294         __le16  handle;         /* Handle & Flags(PB, BC) */
295         __le16  dlen;
296 } __packed;
297
298 struct hci_sco_hdr {
299         __le16  handle;
300         __u8    dlen;
301 } __packed;
302
303 static inline struct hci_event_hdr *hci_event_hdr(const struct sk_buff *skb)
304 {
305         return (struct hci_event_hdr *) skb->data;
306 }
307
308 static inline struct hci_acl_hdr *hci_acl_hdr(const struct sk_buff *skb)
309 {
310         return (struct hci_acl_hdr *) skb->data;
311 }
312
313 static inline struct hci_sco_hdr *hci_sco_hdr(const struct sk_buff *skb)
314 {
315         return (struct hci_sco_hdr *) skb->data;
316 }
317
318 /* ---- HCI Ioctl requests structures ---- */
319 struct hci_dev_stats {
320         __u32 err_rx;
321         __u32 err_tx;
322         __u32 cmd_tx;
323         __u32 evt_rx;
324         __u32 acl_tx;
325         __u32 acl_rx;
326         __u32 sco_tx;
327         __u32 sco_rx;
328         __u32 byte_rx;
329         __u32 byte_tx;
330 };
331 /* Realtek - Integrate from hci.h end */
332
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;
340         unsigned int     le_num;
341 };
342
343 #define HCI_MAX_SHORT_NAME_LENGTH       10
344
345 #define NUM_REASSEMBLY 4
346 struct hci_dev {
347         struct mutex    lock;
348
349         char            name[8];
350         unsigned long   flags;
351         __u16           id;
352         __u8            bus;
353         __u8            dev_type;
354
355         struct sk_buff          *reassembly[NUM_REASSEMBLY];
356
357         struct hci_conn_hash    conn_hash;
358
359         struct hci_dev_stats    stat;
360
361 #if LINUX_VERSION_CODE <= KERNEL_VERSION(3, 4, 0)
362         atomic_t        refcnt;
363         struct module           *owner;
364         void                    *driver_data;
365 #endif
366
367         atomic_t                promisc;
368
369     struct device               *parent;
370         struct device           dev;
371
372         unsigned long           dev_flags;
373
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);
380 #endif
381 #if LINUX_VERSION_CODE > KERNEL_VERSION(3, 7, 1)
382         __u16               voice_setting;
383 #endif
384         void (*notify)(struct hci_dev *hdev, unsigned int evt);
385         int (*ioctl)(struct hci_dev *hdev, unsigned int cmd, unsigned long arg);
386 };
387
388 #if LINUX_VERSION_CODE <= KERNEL_VERSION(3, 4, 0)
389 static inline struct hci_dev *__hci_dev_hold(struct hci_dev *d)
390 {
391         atomic_inc(&d->refcnt);
392         return d;
393 }
394
395 static inline void __hci_dev_put(struct hci_dev *d)
396 {
397         if (atomic_dec_and_test(&d->refcnt))
398                 d->destruct(d);
399 }
400 #endif
401
402 static inline void *hci_get_drvdata(struct hci_dev *hdev)
403 {
404         return dev_get_drvdata(&hdev->dev);
405 }
406
407 static inline int hci_set_drvdata(struct hci_dev *hdev, void *data)
408 {
409         return dev_set_drvdata(&hdev->dev, data);
410 }
411
412 #define SET_HCIDEV_DEV(hdev, pdev) ((hdev)->parent = (pdev))
413 /* Realtek - Integrate from hci_core.h end */
414
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
453
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
465
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
471
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
481
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
491
492 #endif /* CONFIG_BLUEDROID */