Merge tag lsk-v3.10-15.03-android
[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 #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
94
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
99
100 #define ROM_LMP_8723a               0x1200
101 #define ROM_LMP_8723b               0x8723
102 #define ROM_LMP_8821a               0X8821
103 #define ROM_LMP_8761a               0X8761
104
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};
109
110 uint16_t project_id[] = {
111         ROM_LMP_8723a,
112         ROM_LMP_8723b,
113         ROM_LMP_8821a,
114         ROM_LMP_8761a
115 };
116 struct rtk_eversion_evt {
117         uint8_t status;
118         uint8_t version;
119 } __attribute__ ((packed));
120
121 struct rtk_epatch_entry {
122         uint16_t chip_id;
123         uint16_t patch_length;
124         uint32_t start_offset;
125         uint32_t coex_version;
126         uint32_t svn_version;
127         uint32_t fw_version;
128 } __attribute__ ((packed));
129
130 struct rtk_epatch {
131         uint8_t signature[8];
132         uint32_t fw_version;
133         uint16_t number_of_total_patch;
134         struct rtk_epatch_entry entry[0];
135 } __attribute__ ((packed));
136
137 struct rtk_extension_entry {
138         uint8_t opcode;
139         uint8_t length;
140         uint8_t *data;
141 } __attribute__ ((packed));
142 /* Realtek - For rtk_btusb driver end */
143
144 #if CONFIG_BLUEDROID
145 #define QUEUE_SIZE 500
146
147 /***************************************
148 ** Realtek - Integrate from bluetooth.h **
149 *****************************************/
150 /* Reserv for core and drivers use */
151 #define BT_SKB_RESERVE  8
152
153 /* BD Address */
154 typedef struct {
155         __u8 b[6];
156 } __packed bdaddr_t;
157
158 /* Skb helpers */
159 struct bt_skb_cb {
160         __u8 pkt_type;
161         __u8 incoming;
162         __u16 expect;
163         __u16 tx_seq;
164         __u8 retries;
165         __u8 sar;
166         __u8 force_active;
167 };
168
169 #define bt_cb(skb) ((struct bt_skb_cb *)((skb)->cb))
170
171 static inline struct sk_buff *bt_skb_alloc(unsigned int len, gfp_t how)
172 {
173         struct sk_buff *skb;
174
175         if ((skb = alloc_skb(len + BT_SKB_RESERVE, how))) {
176                 skb_reserve(skb, BT_SKB_RESERVE);
177                 bt_cb(skb)->incoming  = 0;
178         }
179         return skb;
180 }
181 /* Realtek - Integrate from bluetooth.h end */
182
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)
190
191 /* HCI bus types */
192 #define HCI_VIRTUAL     0
193 #define HCI_USB         1
194 #define HCI_PCCARD      2
195 #define HCI_UART        3
196 #define HCI_RS232       4
197 #define HCI_PCI         5
198 #define HCI_SDIO        6
199
200 /* HCI controller types */
201 #define HCI_BREDR       0x00
202 #define HCI_AMP         0x01
203
204 /* HCI device flags */
205 enum {
206         HCI_UP,
207         HCI_INIT,
208         HCI_RUNNING,
209
210         HCI_PSCAN,
211         HCI_ISCAN,
212         HCI_AUTH,
213         HCI_ENCRYPT,
214         HCI_INQUIRY,
215
216         HCI_RAW,
217
218         HCI_RESET,
219 };
220
221 /*
222  * BR/EDR and/or LE controller flags: the flags defined here should represent
223  * states from the controller.
224  */
225 enum {
226         HCI_SETUP,
227         HCI_AUTO_OFF,
228         HCI_MGMT,
229         HCI_PAIRABLE,
230         HCI_SERVICE_CACHE,
231         HCI_LINK_KEYS,
232         HCI_DEBUG_KEYS,
233         HCI_UNREGISTER,
234
235         HCI_LE_SCAN,
236         HCI_SSP_ENABLED,
237         HCI_HS_ENABLED,
238         HCI_LE_ENABLED,
239         HCI_CONNECTABLE,
240         HCI_DISCOVERABLE,
241         HCI_LINK_SECURITY,
242         HCI_PENDING_CLASS,
243 };
244
245 /* HCI data types */
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
251
252 #define HCI_MAX_NAME_LENGTH             248
253 #define HCI_MAX_EIR_LENGTH              240
254
255 #define HCI_OP_READ_LOCAL_VERSION       0x1001
256 struct hci_rp_read_local_version {
257         __u8     status;
258         __u8     hci_ver;
259         __le16   hci_rev;
260         __u8     lmp_ver;
261         __le16   manufacturer;
262         __le16   lmp_subver;
263 } __packed;
264
265 #define HCI_EV_CMD_COMPLETE             0x0e
266 struct hci_ev_cmd_complete {
267         __u8     ncmd;
268         __le16   opcode;
269 } __packed;
270
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
276
277 struct hci_command_hdr {
278         __le16  opcode;         /* OCF & OGF */
279         __u8    plen;
280 } __packed;
281
282 struct hci_event_hdr {
283         __u8    evt;
284         __u8    plen;
285 } __packed;
286
287 struct hci_acl_hdr {
288         __le16  handle;         /* Handle & Flags(PB, BC) */
289         __le16  dlen;
290 } __packed;
291
292 struct hci_sco_hdr {
293         __le16  handle;
294         __u8    dlen;
295 } __packed;
296
297 static inline struct hci_event_hdr *hci_event_hdr(const struct sk_buff *skb)
298 {
299         return (struct hci_event_hdr *) skb->data;
300 }
301
302 static inline struct hci_acl_hdr *hci_acl_hdr(const struct sk_buff *skb)
303 {
304         return (struct hci_acl_hdr *) skb->data;
305 }
306
307 static inline struct hci_sco_hdr *hci_sco_hdr(const struct sk_buff *skb)
308 {
309         return (struct hci_sco_hdr *) skb->data;
310 }
311
312 /* ---- HCI Ioctl requests structures ---- */
313 struct hci_dev_stats {
314         __u32 err_rx;
315         __u32 err_tx;
316         __u32 cmd_tx;
317         __u32 evt_rx;
318         __u32 acl_tx;
319         __u32 acl_rx;
320         __u32 sco_tx;
321         __u32 sco_rx;
322         __u32 byte_rx;
323         __u32 byte_tx;
324 };
325 /* Realtek - Integrate from hci.h end */
326
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;
334         unsigned int     le_num;
335 };
336
337 #define HCI_MAX_SHORT_NAME_LENGTH       10
338
339 #define NUM_REASSEMBLY 4
340 struct hci_dev {
341         struct mutex    lock;
342
343         char            name[8];
344         unsigned long   flags;
345         __u16           id;
346         __u8            bus;
347         __u8            dev_type;
348
349         struct sk_buff          *reassembly[NUM_REASSEMBLY];
350
351         struct hci_conn_hash    conn_hash;
352
353         struct hci_dev_stats    stat;
354
355 #if LINUX_VERSION_CODE <= KERNEL_VERSION(3, 4, 0)
356         atomic_t        refcnt;
357         struct module           *owner;
358         void                    *driver_data;
359 #endif
360
361         atomic_t                promisc;
362
363     struct device               *parent;
364         struct device           dev;
365
366         unsigned long           dev_flags;
367
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);
374 #endif
375 #if LINUX_VERSION_CODE > KERNEL_VERSION(3, 7, 1)
376         __u16               voice_setting;
377 #endif
378         void (*notify)(struct hci_dev *hdev, unsigned int evt);
379         int (*ioctl)(struct hci_dev *hdev, unsigned int cmd, unsigned long arg);
380 };
381
382 #if LINUX_VERSION_CODE <= KERNEL_VERSION(3, 4, 0)
383 static inline struct hci_dev *__hci_dev_hold(struct hci_dev *d)
384 {
385         atomic_inc(&d->refcnt);
386         return d;
387 }
388
389 static inline void __hci_dev_put(struct hci_dev *d)
390 {
391         if (atomic_dec_and_test(&d->refcnt))
392                 d->destruct(d);
393 }
394 #endif
395
396 static inline void *hci_get_drvdata(struct hci_dev *hdev)
397 {
398         return dev_get_drvdata(&hdev->dev);
399 }
400
401 static inline int hci_set_drvdata(struct hci_dev *hdev, void *data)
402 {
403         return dev_set_drvdata(&hdev->dev, data);
404 }
405
406 #define SET_HCIDEV_DEV(hdev, pdev) ((hdev)->parent = (pdev))
407 /* Realtek - Integrate from hci_core.h end */
408
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
447
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
459
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
465
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
475
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
485
486 #endif /* CONFIG_BLUEDROID */