1 /*
2  * Marvell Bluetooth driver: global definitions & declarations
3  *
4  * Copyright (C) 2009, Marvell International Ltd.
5  *
6  * This software file (the "File") is distributed by Marvell International
7  * Ltd. under the terms of the GNU General Public License Version 2, June 1991
8  * (the "License").  You may use, redistribute and/or modify this File in
9  * accordance with the terms and conditions of the License, a copy of which
10  * is available by writing to the Free Software Foundation, Inc.,
11  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
12  * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
13  *
14  *
15  * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
17  * ARE EXPRESSLY DISCLAIMED.  The License provides additional details about
18  * this warranty disclaimer.
19  *
20  */
21 
22 #include <linux/kthread.h>
23 #include <linux/bitops.h>
24 #include <linux/slab.h>
25 #include <net/bluetooth/bluetooth.h>
26 #include <linux/err.h>
27 #include <linux/gpio.h>
28 #include <linux/gfp.h>
29 #include <linux/interrupt.h>
30 #include <linux/io.h>
31 #include <linux/of_gpio.h>
32 #include <linux/of_platform.h>
33 #include <linux/platform_device.h>
34 #include <linux/pm_runtime.h>
35 #include <linux/slab.h>
36 #include <linux/of_irq.h>
37 
38 #define BTM_HEADER_LEN			4
39 #define BTM_UPLD_SIZE			2312
40 
41 /* Time to wait until Host Sleep state change in millisecond */
42 #define WAIT_UNTIL_HS_STATE_CHANGED	msecs_to_jiffies(5000)
43 /* Time to wait for command response in millisecond */
44 #define WAIT_UNTIL_CMD_RESP		msecs_to_jiffies(5000)
45 
46 enum rdwr_status {
47 	RDWR_STATUS_SUCCESS = 0,
48 	RDWR_STATUS_FAILURE = 1,
49 	RDWR_STATUS_DONE = 2
50 };
51 
52 #define FW_DUMP_MAX_NAME_LEN    8
53 #define FW_DUMP_HOST_READY      0xEE
54 #define FW_DUMP_DONE            0xFF
55 #define FW_DUMP_READ_DONE       0xFE
56 
57 struct memory_type_mapping {
58 	u8 mem_name[FW_DUMP_MAX_NAME_LEN];
59 	u8 *mem_ptr;
60 	u32 mem_size;
61 	u8 done_flag;
62 };
63 
64 struct btmrvl_thread {
65 	struct task_struct *task;
66 	wait_queue_head_t wait_q;
67 	void *priv;
68 };
69 
70 struct btmrvl_device {
71 	void *card;
72 	struct hci_dev *hcidev;
73 
74 	u8 dev_type;
75 
76 	u8 tx_dnld_rdy;
77 
78 	u8 psmode;
79 	u8 pscmd;
80 	u8 hsmode;
81 	u8 hscmd;
82 
83 	/* Low byte is gap, high byte is GPIO */
84 	u16 gpio_gap;
85 
86 	u8 hscfgcmd;
87 	u8 sendcmdflag;
88 };
89 
90 struct btmrvl_adapter {
91 	void *hw_regs_buf;
92 	u8 *hw_regs;
93 	u32 int_count;
94 	struct sk_buff_head tx_queue;
95 	u8 psmode;
96 	u8 ps_state;
97 	u8 hs_state;
98 	u8 wakeup_tries;
99 	wait_queue_head_t cmd_wait_q;
100 	wait_queue_head_t event_hs_wait_q;
101 	u8 cmd_complete;
102 	bool is_suspended;
103 	bool is_suspending;
104 };
105 
106 struct btmrvl_private {
107 	struct btmrvl_device btmrvl_dev;
108 	struct btmrvl_adapter *adapter;
109 	struct btmrvl_thread main_thread;
110 	int (*hw_host_to_card)(struct btmrvl_private *priv,
111 				u8 *payload, u16 nb);
112 	int (*hw_wakeup_firmware)(struct btmrvl_private *priv);
113 	int (*hw_process_int_status)(struct btmrvl_private *priv);
114 	void (*firmware_dump)(struct btmrvl_private *priv);
115 	spinlock_t driver_lock;		/* spinlock used by driver */
116 #ifdef CONFIG_DEBUG_FS
117 	void *debugfs_data;
118 #endif
119 	bool surprise_removed;
120 };
121 
122 #define MRVL_VENDOR_PKT			0xFE
123 
124 /* Vendor specific Bluetooth commands */
125 #define BT_CMD_PSCAN_WIN_REPORT_ENABLE	0xFC03
126 #define BT_CMD_ROUTE_SCO_TO_HOST	0xFC1D
127 #define BT_CMD_SET_BDADDR		0xFC22
128 #define BT_CMD_AUTO_SLEEP_MODE		0xFC23
129 #define BT_CMD_HOST_SLEEP_CONFIG	0xFC59
130 #define BT_CMD_HOST_SLEEP_ENABLE	0xFC5A
131 #define BT_CMD_MODULE_CFG_REQ		0xFC5B
132 #define BT_CMD_LOAD_CONFIG_DATA		0xFC61
133 
134 /* Sub-commands: Module Bringup/Shutdown Request/Response */
135 #define MODULE_BRINGUP_REQ		0xF1
136 #define MODULE_BROUGHT_UP		0x00
137 #define MODULE_ALREADY_UP		0x0C
138 
139 #define MODULE_SHUTDOWN_REQ		0xF2
140 
141 /* Vendor specific Bluetooth events */
142 #define BT_EVENT_AUTO_SLEEP_MODE	0x23
143 #define BT_EVENT_HOST_SLEEP_CONFIG	0x59
144 #define BT_EVENT_HOST_SLEEP_ENABLE	0x5A
145 #define BT_EVENT_MODULE_CFG_REQ		0x5B
146 #define BT_EVENT_POWER_STATE		0x20
147 
148 /* Bluetooth Power States */
149 #define BT_PS_ENABLE			0x02
150 #define BT_PS_DISABLE			0x03
151 #define BT_PS_SLEEP			0x01
152 
153 /* Host Sleep states */
154 #define HS_ACTIVATED			0x01
155 #define HS_DEACTIVATED			0x00
156 
157 /* Power Save modes */
158 #define PS_SLEEP			0x01
159 #define PS_AWAKE			0x00
160 
161 #define BT_CAL_HDR_LEN			4
162 #define BT_CAL_DATA_SIZE		28
163 
164 struct btmrvl_event {
165 	u8 ec;		/* event counter */
166 	u8 length;
167 	u8 data[4];
168 } __packed;
169 
170 /* Prototype of global function */
171 
172 int btmrvl_register_hdev(struct btmrvl_private *priv);
173 struct btmrvl_private *btmrvl_add_card(void *card);
174 int btmrvl_remove_card(struct btmrvl_private *priv);
175 
176 void btmrvl_interrupt(struct btmrvl_private *priv);
177 
178 bool btmrvl_check_evtpkt(struct btmrvl_private *priv, struct sk_buff *skb);
179 int btmrvl_process_event(struct btmrvl_private *priv, struct sk_buff *skb);
180 
181 int btmrvl_send_module_cfg_cmd(struct btmrvl_private *priv, u8 subcmd);
182 int btmrvl_pscan_window_reporting(struct btmrvl_private *priv, u8 subcmd);
183 int btmrvl_send_hscfg_cmd(struct btmrvl_private *priv);
184 int btmrvl_enable_ps(struct btmrvl_private *priv);
185 int btmrvl_prepare_command(struct btmrvl_private *priv);
186 int btmrvl_enable_hs(struct btmrvl_private *priv);
187 void btmrvl_firmware_dump(struct btmrvl_private *priv);
188 
189 #ifdef CONFIG_DEBUG_FS
190 void btmrvl_debugfs_init(struct hci_dev *hdev);
191 void btmrvl_debugfs_remove(struct hci_dev *hdev);
192 #endif
193