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 //#define CONFIG_SCO_OVER_HCI
40
41 #ifdef CONFIG_SCO_OVER_HCI
42 #include <linux/usb/audio.h>
43 #include <sound/core.h>
44 #include <sound/initval.h>
45 #include <sound/pcm.h>
46 #include <sound/pcm_params.h>
47
48 #define RTK_SCO_ID "snd_sco_rtk"
49 enum {
50 USB_CAPTURE_RUNNING,
51 USB_PLAYBACK_RUNNING,
52 ALSA_CAPTURE_OPEN,
53 ALSA_PLAYBACK_OPEN,
54 ALSA_CAPTURE_RUNNING,
55 ALSA_PLAYBACK_RUNNING,
56 CAPTURE_URB_COMPLETED,
57 PLAYBACK_URB_COMPLETED,
58 DISCONNECTED,
59 };
60
61 // RTK sound card
62 typedef struct RTK_sco_card {
63 struct snd_card *card;
64 struct snd_pcm *pcm;
65 struct usb_device *dev;
66 struct btusb_data *usb_data;
67 unsigned long states;
68 struct rtk_sco_stream {
69 struct snd_pcm_substream *substream;
70 unsigned int sco_packet_bytes;
71 snd_pcm_uframes_t buffer_pos;
72 } capture, playback;
73 spinlock_t capture_lock;
74 spinlock_t playback_lock;
75 struct work_struct send_sco_work;
76 } RTK_sco_card_t;
77 #endif
78 /* Some Android system may use standard Linux kernel, while
79 * standard Linux may also implement early suspend feature.
80 * So exclude earysuspend.h from CONFIG_BLUEDROID.
81 */
82 #ifdef CONFIG_HAS_EARLYSUSPEND
83 #include <linux/earlysuspend.h>
84 #endif
85
86 #if CONFIG_BLUEDROID
87 #else
88 #include <net/bluetooth/bluetooth.h>
89 #include <net/bluetooth/hci_core.h>
90 #include <net/bluetooth/hci.h>
91 #endif
92
93
94 /***********************************
95 ** Realtek - For rtk_btusb driver **
96 ***********************************/
97 #define URB_CANCELING_DELAY_MS 10
98 /* when OS suspended, module is still powered,usb is not powered,
99 * this may set to 1, and must comply with special patch code.
100 */
101 #define CONFIG_RESET_RESUME 1
102 #define PRINT_CMD_EVENT 0
103 #define PRINT_ACL_DATA 0
104 #define PRINT_SCO_DATA 0
105
106 #define RTKBT_DBG_FLAG 0
107
108 #if RTKBT_DBG_FLAG
109 #define RTKBT_DBG(fmt, arg...) printk(KERN_INFO "rtk_btusb: " fmt "\n" , ## arg)
110 #else
111 #define RTKBT_DBG(fmt, arg...)
112 #endif
113 #define RTKBT_INFO(fmt, arg...) printk(KERN_INFO "rtk_btusb: " fmt "\n" , ## arg)
114 #define RTKBT_WARN(fmt, arg...) printk(KERN_WARNING "rtk_btusb: " fmt "\n" , ## arg)
115 #define RTKBT_ERR(fmt, arg...) printk(KERN_ERR "rtk_btusb: " fmt "\n" , ## arg)
116
117
118 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 33)
119 #define HDEV_BUS hdev->bus
120 #define USB_RPM 1
121 #else
122 #define HDEV_BUS hdev->type
123 #define USB_RPM 0
124 #endif
125
126 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 38)
127 #define NUM_REASSEMBLY 3
128 #endif
129
130 #if LINUX_VERSION_CODE > KERNEL_VERSION(3, 4, 0)
131 #define GET_DRV_DATA(x) hci_get_drvdata(x)
132 #else
133 #define GET_DRV_DATA(x) x->driver_data
134 #endif
135
136 #define BTUSB_RPM (0 * USB_RPM) /* 1 SS enable; 0 SS disable */
137 #define BTUSB_WAKEUP_HOST 0 /* 1 enable; 0 disable */
138 #define BTUSB_MAX_ISOC_FRAMES 48
139 #define BTUSB_INTR_RUNNING 0
140 #define BTUSB_BULK_RUNNING 1
141 #define BTUSB_ISOC_RUNNING 2
142 #define BTUSB_SUSPENDING 3
143 #define BTUSB_DID_ISO_RESUME 4
144
145 #define HCI_CMD_READ_BD_ADDR 0x1009
146 #define HCI_VENDOR_CHANGE_BDRATE 0xfc17
147 #define HCI_VENDOR_READ_RTK_ROM_VERISION 0xfc6d
148 #define HCI_VENDOR_READ_LMP_VERISION 0x1001
149 #define HCI_VENDOR_FORCE_RESET_AND_PATCHABLE 0xfc66
150 #define HCI_VENDOR_RESET 0x0C03
151 #define HCI_VENDOR_ADD_WAKE_UP_DEVICE 0xfc7b
152 #define HCI_VENDOR_REMOVE_WAKE_UP_DEVICE 0xfc7c
153 #define HCI_VENDOR_CLEAR_POWERON_LIST 0xfc7d
154
155 #define HCI_VENDOR_USB_DISC_HARDWARE_ERROR 0xFF
156
157 #define SET_WAKEUP_DEVICE_CONF "/data/misc/bluedroid/rtkbt_wakeup_ble.conf"
158
159 #define DRV_NORMAL_MODE 0
160 #define DRV_MP_MODE 1
161 int mp_drv_mode = 0; /* 1 Mptool Fw; 0 Normal Fw */
162
163 #define ROM_LMP_NONE 0x0000
164 #define ROM_LMP_8723a 0x1200
165 #define ROM_LMP_8723b 0x8723
166 #define ROM_LMP_8821a 0X8821
167 #define ROM_LMP_8761a 0X8761
168 #define ROM_LMP_8703a 0x8723
169 #define ROM_LMP_8763a 0x8763
170 #define ROM_LMP_8703b 0x8703
171 #define ROM_LMP_8723c 0x8703
172 #define ROM_LMP_8822b 0x8822
173 #define ROM_LMP_8723d 0x8723
174 #define ROM_LMP_8821c 0x8821
175
176 /* signature: Realtek */
177 const uint8_t RTK_EPATCH_SIGNATURE[8] = {0x52,0x65,0x61,0x6C,0x74,0x65,0x63,0x68};
178 /* Extension Section IGNATURE:0x77FD0451 */
179 const uint8_t EXTENSION_SECTION_SIGNATURE[4] = {0x51,0x04,0xFD,0x77};
180
181 uint16_t project_id[] = {
182 ROM_LMP_8723a,
183 ROM_LMP_8723b,
184 ROM_LMP_8821a,
185 ROM_LMP_8761a,
186 ROM_LMP_8703a,
187 ROM_LMP_8763a,
188 ROM_LMP_8703b,
189 ROM_LMP_8723c,
190 ROM_LMP_8822b,
191 ROM_LMP_8723d,
192 ROM_LMP_8821c,
193 ROM_LMP_NONE
194 };
195 struct rtk_eversion_evt {
196 uint8_t status;
197 uint8_t version;
198 } __attribute__ ((packed));
199
200 /*modified by lamparten 1020*/
201 struct rtk_reset_evt {
202 uint8_t status;
203 } __attribute__ ((packed));
204 /*modified by lamparten 1020*/
205
206 struct rtk_localversion_evt {
207 uint8_t status;
208 uint8_t hci_version;
209 uint16_t hci_revision;
210 uint8_t lmp_version;
211 uint16_t lmp_manufacture;
212 uint16_t lmp_subversion;
213 } __attribute__ ((packed));
214
215 struct rtk_epatch_entry {
216 uint16_t chip_id;
217 uint16_t patch_length;
218 uint32_t start_offset;
219 uint32_t coex_version;
220 uint32_t svn_version;
221 uint32_t fw_version;
222 } __attribute__ ((packed));
223
224 struct rtk_epatch {
225 uint8_t signature[8];
226 uint32_t fw_version;
227 uint16_t number_of_total_patch;
228 struct rtk_epatch_entry entry[0];
229 } __attribute__ ((packed));
230
231 struct rtk_extension_entry {
232 uint8_t opcode;
233 uint8_t length;
234 uint8_t *data;
235 } __attribute__ ((packed));
236
237 struct rtk_bt_vendor_config_entry{
238 uint16_t offset;
239 uint8_t entry_len;
240 uint8_t entry_data[0];
241 } __attribute__ ((packed));
242
243 struct rtk_bt_vendor_config{
244 uint32_t signature;
245 uint16_t data_len;
246 struct rtk_bt_vendor_config_entry entry[0];
247 } __attribute__ ((packed));
248
249 /* Realtek - For rtk_btusb driver end */
250
251 #if CONFIG_BLUEDROID
252 #define QUEUE_SIZE 500
253
254 /***************************************
255 ** Realtek - Integrate from bluetooth.h **
256 *****************************************/
257 /* Reserv for core and drivers use */
258 #define BT_SKB_RESERVE 8
259
260 /* BD Address */
261 typedef struct {
262 __u8 b[6];
263 } __packed bdaddr_t;
264
265 /* Skb helpers */
266 struct bt_skb_cb {
267 __u8 pkt_type;
268 __u8 incoming;
269 __u16 expect;
270 __u16 tx_seq;
271 __u8 retries;
272 __u8 sar;
273 __u8 force_active;
274 };
275
276 #define bt_cb(skb) ((struct bt_skb_cb *)((skb)->cb))
277
bt_skb_alloc(unsigned int len,gfp_t how)278 static inline struct sk_buff *bt_skb_alloc(unsigned int len, gfp_t how)
279 {
280 struct sk_buff *skb;
281
282 if ((skb = alloc_skb(len + BT_SKB_RESERVE, how))) {
283 skb_reserve(skb, BT_SKB_RESERVE);
284 bt_cb(skb)->incoming = 0;
285 }
286 return skb;
287 }
288 /* Realtek - Integrate from bluetooth.h end */
289
290 /***********************************
291 ** Realtek - Integrate from hci.h **
292 ***********************************/
293 #define HCI_MAX_ACL_SIZE 1024
294 #define HCI_MAX_SCO_SIZE 255
295 #define HCI_MAX_EVENT_SIZE 260
296 #define HCI_MAX_FRAME_SIZE (HCI_MAX_ACL_SIZE + 4)
297
298 /* HCI bus types */
299 #define HCI_VIRTUAL 0
300 #define HCI_USB 1
301 #define HCI_PCCARD 2
302 #define HCI_UART 3
303 #define HCI_RS232 4
304 #define HCI_PCI 5
305 #define HCI_SDIO 6
306
307 /* HCI controller types */
308 #define HCI_BREDR 0x00
309 #define HCI_AMP 0x01
310
311 /* HCI device flags */
312 enum {
313 HCI_UP,
314 HCI_INIT,
315 HCI_RUNNING,
316
317 HCI_PSCAN,
318 HCI_ISCAN,
319 HCI_AUTH,
320 HCI_ENCRYPT,
321 HCI_INQUIRY,
322
323 HCI_RAW,
324
325 HCI_RESET,
326 };
327
328 /*
329 * BR/EDR and/or LE controller flags: the flags defined here should represent
330 * states from the controller.
331 */
332 enum {
333 HCI_SETUP,
334 HCI_AUTO_OFF,
335 HCI_MGMT,
336 HCI_PAIRABLE,
337 HCI_SERVICE_CACHE,
338 HCI_LINK_KEYS,
339 HCI_DEBUG_KEYS,
340 HCI_UNREGISTER,
341
342 HCI_LE_SCAN,
343 HCI_SSP_ENABLED,
344 HCI_HS_ENABLED,
345 HCI_LE_ENABLED,
346 HCI_CONNECTABLE,
347 HCI_DISCOVERABLE,
348 HCI_LINK_SECURITY,
349 HCI_PENDING_CLASS,
350 };
351
352 /* HCI data types */
353 #define HCI_COMMAND_PKT 0x01
354 #define HCI_ACLDATA_PKT 0x02
355 #define HCI_SCODATA_PKT 0x03
356 #define HCI_EVENT_PKT 0x04
357 #define HCI_VENDOR_PKT 0xff
358
359 #define HCI_MAX_NAME_LENGTH 248
360 #define HCI_MAX_EIR_LENGTH 240
361
362 #define HCI_OP_READ_LOCAL_VERSION 0x1001
363 struct hci_rp_read_local_version {
364 __u8 status;
365 __u8 hci_ver;
366 __le16 hci_rev;
367 __u8 lmp_ver;
368 __le16 manufacturer;
369 __le16 lmp_subver;
370 } __packed;
371
372 #define HCI_EV_CMD_COMPLETE 0x0e
373 struct hci_ev_cmd_complete {
374 __u8 ncmd;
375 __le16 opcode;
376 } __packed;
377
378 /* ---- HCI Packet structures ---- */
379 #define HCI_COMMAND_HDR_SIZE 3
380 #define HCI_EVENT_HDR_SIZE 2
381 #define HCI_ACL_HDR_SIZE 4
382 #define HCI_SCO_HDR_SIZE 3
383
384 struct hci_command_hdr {
385 __le16 opcode; /* OCF & OGF */
386 __u8 plen;
387 } __packed;
388
389 struct hci_event_hdr {
390 __u8 evt;
391 __u8 plen;
392 } __packed;
393
394 struct hci_acl_hdr {
395 __le16 handle; /* Handle & Flags(PB, BC) */
396 __le16 dlen;
397 } __packed;
398
399 struct hci_sco_hdr {
400 __le16 handle;
401 __u8 dlen;
402 } __packed;
403
hci_event_hdr(const struct sk_buff * skb)404 static inline struct hci_event_hdr *hci_event_hdr(const struct sk_buff *skb)
405 {
406 return (struct hci_event_hdr *) skb->data;
407 }
408
hci_acl_hdr(const struct sk_buff * skb)409 static inline struct hci_acl_hdr *hci_acl_hdr(const struct sk_buff *skb)
410 {
411 return (struct hci_acl_hdr *) skb->data;
412 }
413
hci_sco_hdr(const struct sk_buff * skb)414 static inline struct hci_sco_hdr *hci_sco_hdr(const struct sk_buff *skb)
415 {
416 return (struct hci_sco_hdr *) skb->data;
417 }
418
419 /* ---- HCI Ioctl requests structures ---- */
420 struct hci_dev_stats {
421 __u32 err_rx;
422 __u32 err_tx;
423 __u32 cmd_tx;
424 __u32 evt_rx;
425 __u32 acl_tx;
426 __u32 acl_rx;
427 __u32 sco_tx;
428 __u32 sco_rx;
429 __u32 byte_rx;
430 __u32 byte_tx;
431 };
432 /* Realtek - Integrate from hci.h end */
433
434 /*****************************************
435 ** Realtek - Integrate from hci_core.h **
436 *****************************************/
437 struct hci_conn_hash {
438 struct list_head list;
439 unsigned int acl_num;
440 unsigned int sco_num;
441 unsigned int le_num;
442 };
443
444 #define HCI_MAX_SHORT_NAME_LENGTH 10
445
446 #define NUM_REASSEMBLY 4
447 struct hci_dev {
448 struct mutex lock;
449
450 char name[8];
451 unsigned long flags;
452 __u16 id;
453 __u8 bus;
454 __u8 dev_type;
455
456 struct sk_buff *reassembly[NUM_REASSEMBLY];
457
458 struct hci_conn_hash conn_hash;
459
460 struct hci_dev_stats stat;
461
462 #if LINUX_VERSION_CODE <= KERNEL_VERSION(3, 4, 0)
463 atomic_t refcnt;
464 struct module *owner;
465 void *driver_data;
466 #endif
467
468 atomic_t promisc;
469
470 struct device *parent;
471 struct device dev;
472
473 unsigned long dev_flags;
474
475 int (*open)(struct hci_dev *hdev);
476 int (*close)(struct hci_dev *hdev);
477 int (*flush)(struct hci_dev *hdev);
478 int (*send)(struct sk_buff *skb);
479 #if LINUX_VERSION_CODE <= KERNEL_VERSION(3, 4, 0)
480 void (*destruct)(struct hci_dev *hdev);
481 #endif
482 __u16 voice_setting;
483 void (*notify)(struct hci_dev *hdev, unsigned int evt);
484 int (*ioctl)(struct hci_dev *hdev, unsigned int cmd, unsigned long arg);
485 };
486
487 #if LINUX_VERSION_CODE <= KERNEL_VERSION(3, 4, 0)
__hci_dev_hold(struct hci_dev * d)488 static inline struct hci_dev *__hci_dev_hold(struct hci_dev *d)
489 {
490 atomic_inc(&d->refcnt);
491 return d;
492 }
493
__hci_dev_put(struct hci_dev * d)494 static inline void __hci_dev_put(struct hci_dev *d)
495 {
496 if (atomic_dec_and_test(&d->refcnt))
497 d->destruct(d);
498 }
499 #endif
500
hci_get_drvdata(struct hci_dev * hdev)501 static inline void *hci_get_drvdata(struct hci_dev *hdev)
502 {
503 return dev_get_drvdata(&hdev->dev);
504 }
505
hci_set_drvdata(struct hci_dev * hdev,void * data)506 static inline void hci_set_drvdata(struct hci_dev *hdev, void *data)
507 {
508 dev_set_drvdata(&hdev->dev, data);
509 }
510
511 #define SET_HCIDEV_DEV(hdev, pdev) ((hdev)->parent = (pdev))
512 /* Realtek - Integrate from hci_core.h end */
513
514 /* ----- HCI Commands ---- */
515 #define HCI_OP_INQUIRY 0x0401
516 #define HCI_OP_INQUIRY_CANCEL 0x0402
517 #define HCI_OP_EXIT_PERIODIC_INQ 0x0404
518 #define HCI_OP_CREATE_CONN 0x0405
519 #define HCI_OP_DISCONNECT 0x0406
520 #define HCI_OP_ADD_SCO 0x0407
521 #define HCI_OP_CREATE_CONN_CANCEL 0x0408
522 #define HCI_OP_ACCEPT_CONN_REQ 0x0409
523 #define HCI_OP_REJECT_CONN_REQ 0x040a
524 #define HCI_OP_LINK_KEY_REPLY 0x040b
525 #define HCI_OP_LINK_KEY_NEG_REPLY 0x040c
526 #define HCI_OP_PIN_CODE_REPLY 0x040d
527 #define HCI_OP_PIN_CODE_NEG_REPLY 0x040e
528 #define HCI_OP_CHANGE_CONN_PTYPE 0x040f
529 #define HCI_OP_AUTH_REQUESTED 0x0411
530 #define HCI_OP_SET_CONN_ENCRYPT 0x0413
531 #define HCI_OP_CHANGE_CONN_LINK_KEY 0x0415
532 #define HCI_OP_REMOTE_NAME_REQ 0x0419
533 #define HCI_OP_REMOTE_NAME_REQ_CANCEL 0x041a
534 #define HCI_OP_READ_REMOTE_FEATURES 0x041b
535 #define HCI_OP_READ_REMOTE_EXT_FEATURES 0x041c
536 #define HCI_OP_READ_REMOTE_VERSION 0x041d
537 #define HCI_OP_SETUP_SYNC_CONN 0x0428
538 #define HCI_OP_ACCEPT_SYNC_CONN_REQ 0x0429
539 #define HCI_OP_REJECT_SYNC_CONN_REQ 0x042a
540 #define HCI_OP_SNIFF_MODE 0x0803
541 #define HCI_OP_EXIT_SNIFF_MODE 0x0804
542 #define HCI_OP_ROLE_DISCOVERY 0x0809
543 #define HCI_OP_SWITCH_ROLE 0x080b
544 #define HCI_OP_READ_LINK_POLICY 0x080c
545 #define HCI_OP_WRITE_LINK_POLICY 0x080d
546 #define HCI_OP_READ_DEF_LINK_POLICY 0x080e
547 #define HCI_OP_WRITE_DEF_LINK_POLICY 0x080f
548 #define HCI_OP_SNIFF_SUBRATE 0x0811
549 #define HCI_OP_SET_EVENT_MASK 0x0c01
550 #define HCI_OP_RESET 0x0c03
551 #define HCI_OP_SET_EVENT_FLT 0x0c05
552 #define HCI_OP_Write_Extended_Inquiry_Response 0x0c52
553
554 /* ----- HCI events---- */
555 #define HCI_OP_DISCONNECT 0x0406
556 #define HCI_EV_INQUIRY_COMPLETE 0x01
557 #define HCI_EV_INQUIRY_RESULT 0x02
558 #define HCI_EV_CONN_COMPLETE 0x03
559 #define HCI_EV_CONN_REQUEST 0x04
560 #define HCI_EV_DISCONN_COMPLETE 0x05
561 #define HCI_EV_AUTH_COMPLETE 0x06
562 #define HCI_EV_REMOTE_NAME 0x07
563 #define HCI_EV_ENCRYPT_CHANGE 0x08
564 #define HCI_EV_CHANGE_LINK_KEY_COMPLETE 0x09
565
566 #define HCI_EV_REMOTE_FEATURES 0x0b
567 #define HCI_EV_REMOTE_VERSION 0x0c
568 #define HCI_EV_QOS_SETUP_COMPLETE 0x0d
569 #define HCI_EV_CMD_COMPLETE 0x0e
570 #define HCI_EV_CMD_STATUS 0x0f
571
572 #define HCI_EV_ROLE_CHANGE 0x12
573 #define HCI_EV_NUM_COMP_PKTS 0x13
574 #define HCI_EV_MODE_CHANGE 0x14
575 #define HCI_EV_PIN_CODE_REQ 0x16
576 #define HCI_EV_LINK_KEY_REQ 0x17
577 #define HCI_EV_LINK_KEY_NOTIFY 0x18
578 #define HCI_EV_CLOCK_OFFSET 0x1c
579 #define HCI_EV_PKT_TYPE_CHANGE 0x1d
580 #define HCI_EV_PSCAN_REP_MODE 0x20
581
582 #define HCI_EV_INQUIRY_RESULT_WITH_RSSI 0x22
583 #define HCI_EV_REMOTE_EXT_FEATURES 0x23
584 #define HCI_EV_SYNC_CONN_COMPLETE 0x2c
585 #define HCI_EV_SYNC_CONN_CHANGED 0x2d
586 #define HCI_EV_SNIFF_SUBRATE 0x2e
587 #define HCI_EV_EXTENDED_INQUIRY_RESULT 0x2f
588 #define HCI_EV_IO_CAPA_REQUEST 0x31
589 #define HCI_EV_SIMPLE_PAIR_COMPLETE 0x36
590 #define HCI_EV_REMOTE_HOST_FEATURES 0x3d
591
592 #define CONFIG_MAC_OFFSET_GEN_1_2 (0x3C) //MAC's OFFSET in config/efuse for realtek generation 1~2 bluetooth chip
593 #define CONFIG_MAC_OFFSET_GEN_3PLUS (0x44) //MAC's OFFSET in config/efuse for rtk generation 3+ bluetooth chip
594
595 /*******************************
596 ** Reasil patch code
597 ********************************/
598 #define CMD_CMP_EVT 0x0e
599 #define PKT_LEN 300
600 #define MSG_TO 1000
601 #define PATCH_SEG_MAX 252
602 #define DATA_END 0x80
603 #define DOWNLOAD_OPCODE 0xfc20
604 #define BTOFF_OPCODE 0xfc28
605 #define TRUE 1
606 #define FALSE 0
607 #define CMD_HDR_LEN sizeof(struct hci_command_hdr)
608 #define EVT_HDR_LEN sizeof(struct hci_event_hdr)
609 #define CMD_CMP_LEN sizeof(struct hci_ev_cmd_complete)
610 #define MAX_PATCH_SIZE_24K (1024*24)
611 #define MAX_PATCH_SIZE_40K (1024*40)
612
613 enum rtk_endpoit {
614 CTRL_EP = 0,
615 INTR_EP = 1,
616 BULK_EP = 2,
617 ISOC_EP = 3
618 };
619
620 typedef struct {
621 uint16_t vid;
622 uint16_t pid;
623 uint16_t lmp_sub_default;
624 uint16_t lmp_sub;
625 uint16_t eversion;
626 char *mp_patch_name;
627 char *patch_name;
628 char *config_name;
629 uint8_t *fw_cache;
630 int fw_len;
631 uint16_t mac_offset;
632 uint32_t max_patch_size;
633 } patch_info;
634
635 typedef struct {
636 struct usb_interface *intf;
637 struct usb_device *udev;
638 patch_info *patch_entry;
639 int pipe_in, pipe_out;
640 uint8_t *send_pkt;
641 uint8_t *rcv_pkt;
642 struct hci_command_hdr *cmd_hdr;
643 struct hci_event_hdr *evt_hdr;
644 struct hci_ev_cmd_complete *cmd_cmp;
645 uint8_t *req_para, *rsp_para;
646 uint8_t *fw_data;
647 int pkt_len;
648 int fw_len;
649 } firmware_info;
650
651 typedef struct {
652 uint8_t index;
653 uint8_t data[PATCH_SEG_MAX];
654 } __attribute__((packed)) download_cp;
655
656 typedef struct {
657 uint8_t status;
658 uint8_t index;
659 } __attribute__((packed)) download_rp;
660
661
662
663 //Define ioctl cmd the same as HCIDEVUP in the kernel
664 #define DOWN_FW_CFG _IOW('H', 201, int)
665 #ifdef CONFIG_SCO_OVER_HCI
666 #define SET_ISO_CFG _IOW('H', 202, int)
667 #endif
668 #define GET_USB_INFO _IOW('H', 203, int)
669 #define RESET_CONTROLLER _IOW('H', 204, int)
670
671 /* for altsettings*/
672 #include <linux/fs.h>
673 #define BDADDR_FILE "/data/misc/bluetooth/bdaddr"
674 #define FACTORY_BT_BDADDR_STORAGE_LEN 17
675
getmacaddr(uint8_t * vnd_local_bd_addr)676 static inline int getmacaddr(uint8_t * vnd_local_bd_addr)
677 {
678 struct file *bdaddr_file;
679 mm_segment_t oldfs;
680 char buf[FACTORY_BT_BDADDR_STORAGE_LEN];
681 int32_t i = 0;
682 int ret = -1;
683 memset(buf, 0, FACTORY_BT_BDADDR_STORAGE_LEN);
684 bdaddr_file = filp_open(BDADDR_FILE, O_RDONLY, 0);
685 if (IS_ERR(bdaddr_file)){
686 RTKBT_INFO("No Mac Config for BT\n");
687 return -1;
688 }
689 oldfs = get_fs(); set_fs(KERNEL_DS);
690 bdaddr_file->f_op->llseek(bdaddr_file, 0, 0);
691 ret = vfs_read(bdaddr_file, buf, FACTORY_BT_BDADDR_STORAGE_LEN, &bdaddr_file->f_pos);
692 set_fs(oldfs);
693 filp_close(bdaddr_file, NULL);
694 if(ret == FACTORY_BT_BDADDR_STORAGE_LEN)
695 {
696 for (i = 0; i < 6; i++) {
697 if(buf[3*i]>'9')
698 {
699 if(buf[3*i]>'Z')
700 buf[3*i] -=('a'-'A'); //change a to A
701 buf[3*i] -= ('A'-'9'-1);
702 }
703 if(buf[3*i+1]>'9')
704 {
705 if(buf[3*i+1]>'Z')
706 buf[3*i+1] -=('a'-'A'); //change a to A
707 buf[3*i+1] -= ('A'-'9'-1);
708 }
709 vnd_local_bd_addr[5-i] = ((uint8_t)buf[3*i]-'0')*16 + ((uint8_t)buf[3*i+1]-'0');
710 }
711 return 0;
712 }
713 return -1;
714 }
715
getAltSettings(patch_info * patch_entry,unsigned short * offset,int max_group_cnt)716 static inline int getAltSettings(patch_info *patch_entry, unsigned short *offset, int max_group_cnt)
717 {
718 int n = 0;
719 if(patch_entry)
720 offset[n++] = patch_entry->mac_offset;
721 /*
722 //sample code, add special settings
723
724 offset[n++] = 0x15B;
725 */
726 return n;
727 }
getAltSettingVal(patch_info * patch_entry,unsigned short offset,unsigned char * val)728 static inline int getAltSettingVal(patch_info *patch_entry, unsigned short offset, unsigned char * val)
729 {
730 int res = 0;
731
732 switch(offset)
733 {
734 /*
735 //sample code, add special settings
736 case 0x15B:
737 val[0] = 0x0B;
738 val[1] = 0x0B;
739 val[2] = 0x0B;
740 val[3] = 0x0B;
741 res = 4;
742 break;
743 */
744 default:
745 res = 0;
746 break;
747 }
748
749 if((patch_entry)&&(offset == patch_entry->mac_offset)&&(res == 0))
750 {
751 if(getmacaddr(val) == 0){
752 RTKBT_INFO("MAC: %02x:%02x:%02x:%02x:%02x:%02x", val[5], val[4], val[3], val[2], val[1], val[0]);
753 res = 6;
754 }
755 }
756 return res;
757 }
758
759 #endif /* CONFIG_BLUEDROID */
760