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