Commit | Line | Data |
---|---|---|
d0088ce1 PS |
1 | /* |
2 | * Shared Transport Line discipline driver Core | |
3 | * Init Manager module responsible for GPIO control | |
4 | * and firmware download | |
a0cc2f3b PS |
5 | * Copyright (C) 2009-2010 Texas Instruments |
6 | * Author: Pavan Savoy <pavan_savoy@ti.com> | |
d0088ce1 PS |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License version 2 as | |
10 | * published by the Free Software Foundation. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 | * | |
21 | */ | |
22 | ||
23 | #define pr_fmt(fmt) "(stk) :" fmt | |
24 | #include <linux/platform_device.h> | |
25 | #include <linux/jiffies.h> | |
26 | #include <linux/firmware.h> | |
27 | #include <linux/delay.h> | |
28 | #include <linux/wait.h> | |
29 | #include <linux/gpio.h> | |
c1afac15 PS |
30 | #include <linux/debugfs.h> |
31 | #include <linux/seq_file.h> | |
d0088ce1 | 32 | #include <linux/sched.h> |
773d6790 | 33 | #include <linux/sysfs.h> |
ec60d0ad | 34 | #include <linux/tty.h> |
d0088ce1 | 35 | |
5c88b021 | 36 | #include <linux/skbuff.h> |
e5558679 | 37 | #include <linux/ti_wilink_st.h> |
eb12a679 | 38 | #include <linux/module.h> |
83ef41f0 | 39 | |
d0088ce1 | 40 | |
dbd3a870 | 41 | #define MAX_ST_DEVICES 3 /* Imagine 1 on each UART for now */ |
73f12e8d | 42 | static struct platform_device *st_kim_devices[MAX_ST_DEVICES]; |
d0088ce1 PS |
43 | |
44 | /**********************************************************************/ | |
45 | /* internal functions */ | |
46 | ||
dbd3a870 PS |
47 | /** |
48 | * st_get_plat_device - | |
49 | * function which returns the reference to the platform device | |
50 | * requested by id. As of now only 1 such device exists (id=0) | |
51 | * the context requesting for reference can get the id to be | |
52 | * requested by a. The protocol driver which is registering or | |
53 | * b. the tty device which is opened. | |
54 | */ | |
55 | static struct platform_device *st_get_plat_device(int id) | |
56 | { | |
57 | return st_kim_devices[id]; | |
58 | } | |
59 | ||
36b5aee4 PS |
60 | /** |
61 | * validate_firmware_response - | |
62 | * function to return whether the firmware response was proper | |
63 | * in case of error don't complete so that waiting for proper | |
64 | * response times out | |
d0088ce1 | 65 | */ |
38d9df49 | 66 | void validate_firmware_response(struct kim_data_s *kim_gdata) |
d0088ce1 | 67 | { |
38d9df49 | 68 | struct sk_buff *skb = kim_gdata->rx_skb; |
d0088ce1 PS |
69 | if (unlikely(skb->data[5] != 0)) { |
70 | pr_err("no proper response during fw download"); | |
71 | pr_err("data6 %x", skb->data[5]); | |
76ff0e64 | 72 | kfree_skb(skb); |
d0088ce1 PS |
73 | return; /* keep waiting for the proper response */ |
74 | } | |
75 | /* becos of all the script being downloaded */ | |
76 | complete_all(&kim_gdata->kim_rcvd); | |
77 | kfree_skb(skb); | |
78 | } | |
79 | ||
80 | /* check for data len received inside kim_int_recv | |
81 | * most often hit the last case to update state to waiting for data | |
82 | */ | |
38d9df49 | 83 | static inline int kim_check_data_len(struct kim_data_s *kim_gdata, int len) |
d0088ce1 PS |
84 | { |
85 | register int room = skb_tailroom(kim_gdata->rx_skb); | |
86 | ||
e6d9e64e | 87 | pr_debug("len %d room %d", len, room); |
d0088ce1 PS |
88 | |
89 | if (!len) { | |
38d9df49 | 90 | validate_firmware_response(kim_gdata); |
d0088ce1 PS |
91 | } else if (len > room) { |
92 | /* Received packet's payload length is larger. | |
93 | * We can't accommodate it in created skb. | |
94 | */ | |
95 | pr_err("Data length is too large len %d room %d", len, | |
96 | room); | |
97 | kfree_skb(kim_gdata->rx_skb); | |
98 | } else { | |
99 | /* Packet header has non-zero payload length and | |
100 | * we have enough space in created skb. Lets read | |
101 | * payload data */ | |
5c88b021 | 102 | kim_gdata->rx_state = ST_W4_DATA; |
d0088ce1 PS |
103 | kim_gdata->rx_count = len; |
104 | return len; | |
105 | } | |
106 | ||
107 | /* Change ST LL state to continue to process next | |
108 | * packet */ | |
109 | kim_gdata->rx_state = ST_W4_PACKET_TYPE; | |
110 | kim_gdata->rx_skb = NULL; | |
111 | kim_gdata->rx_count = 0; | |
112 | ||
113 | return 0; | |
114 | } | |
115 | ||
36b5aee4 PS |
116 | /** |
117 | * kim_int_recv - receive function called during firmware download | |
118 | * firmware download responses on different UART drivers | |
119 | * have been observed to come in bursts of different | |
120 | * tty_receive and hence the logic | |
d0088ce1 | 121 | */ |
38d9df49 PS |
122 | void kim_int_recv(struct kim_data_s *kim_gdata, |
123 | const unsigned char *data, long count) | |
d0088ce1 | 124 | { |
73f12e8d | 125 | const unsigned char *ptr; |
73f12e8d | 126 | int len = 0, type = 0; |
5c88b021 | 127 | unsigned char *plen; |
d0088ce1 | 128 | |
e6d9e64e | 129 | pr_debug("%s", __func__); |
d0088ce1 | 130 | /* Decode received bytes here */ |
73f12e8d | 131 | ptr = data; |
d0088ce1 PS |
132 | if (unlikely(ptr == NULL)) { |
133 | pr_err(" received null from TTY "); | |
134 | return; | |
135 | } | |
73f12e8d | 136 | |
d0088ce1 PS |
137 | while (count) { |
138 | if (kim_gdata->rx_count) { | |
139 | len = min_t(unsigned int, kim_gdata->rx_count, count); | |
140 | memcpy(skb_put(kim_gdata->rx_skb, len), ptr, len); | |
141 | kim_gdata->rx_count -= len; | |
142 | count -= len; | |
143 | ptr += len; | |
144 | ||
145 | if (kim_gdata->rx_count) | |
146 | continue; | |
147 | ||
148 | /* Check ST RX state machine , where are we? */ | |
149 | switch (kim_gdata->rx_state) { | |
150 | /* Waiting for complete packet ? */ | |
5c88b021 | 151 | case ST_W4_DATA: |
e6d9e64e | 152 | pr_debug("Complete pkt received"); |
38d9df49 | 153 | validate_firmware_response(kim_gdata); |
d0088ce1 PS |
154 | kim_gdata->rx_state = ST_W4_PACKET_TYPE; |
155 | kim_gdata->rx_skb = NULL; | |
156 | continue; | |
157 | /* Waiting for Bluetooth event header ? */ | |
5c88b021 PS |
158 | case ST_W4_HEADER: |
159 | plen = | |
160 | (unsigned char *)&kim_gdata->rx_skb->data[1]; | |
161 | pr_debug("event hdr: plen 0x%02x\n", *plen); | |
162 | kim_check_data_len(kim_gdata, *plen); | |
d0088ce1 PS |
163 | continue; |
164 | } /* end of switch */ | |
165 | } /* end of if rx_state */ | |
166 | switch (*ptr) { | |
167 | /* Bluetooth event packet? */ | |
5c88b021 PS |
168 | case 0x04: |
169 | kim_gdata->rx_state = ST_W4_HEADER; | |
170 | kim_gdata->rx_count = 2; | |
171 | type = *ptr; | |
d0088ce1 PS |
172 | break; |
173 | default: | |
174 | pr_info("unknown packet"); | |
175 | ptr++; | |
176 | count--; | |
177 | continue; | |
36b5aee4 | 178 | } |
d0088ce1 PS |
179 | ptr++; |
180 | count--; | |
181 | kim_gdata->rx_skb = | |
5c88b021 | 182 | alloc_skb(1024+8, GFP_ATOMIC); |
d0088ce1 PS |
183 | if (!kim_gdata->rx_skb) { |
184 | pr_err("can't allocate mem for new packet"); | |
185 | kim_gdata->rx_state = ST_W4_PACKET_TYPE; | |
186 | kim_gdata->rx_count = 0; | |
187 | return; | |
36b5aee4 | 188 | } |
5c88b021 PS |
189 | skb_reserve(kim_gdata->rx_skb, 8); |
190 | kim_gdata->rx_skb->cb[0] = 4; | |
191 | kim_gdata->rx_skb->cb[1] = 0; | |
192 | ||
36b5aee4 | 193 | } |
d0088ce1 PS |
194 | return; |
195 | } | |
196 | ||
38d9df49 | 197 | static long read_local_version(struct kim_data_s *kim_gdata, char *bts_scr_name) |
d0088ce1 PS |
198 | { |
199 | unsigned short version = 0, chip = 0, min_ver = 0, maj_ver = 0; | |
73f12e8d | 200 | const char read_ver_cmd[] = { 0x01, 0x01, 0x10, 0x00 }; |
d0088ce1 | 201 | |
e6d9e64e | 202 | pr_debug("%s", __func__); |
d0088ce1 PS |
203 | |
204 | INIT_COMPLETION(kim_gdata->kim_rcvd); | |
205 | if (4 != st_int_write(kim_gdata->core_data, read_ver_cmd, 4)) { | |
206 | pr_err("kim: couldn't write 4 bytes"); | |
70442664 | 207 | return -EIO; |
d0088ce1 PS |
208 | } |
209 | ||
210 | if (!wait_for_completion_timeout | |
211 | (&kim_gdata->kim_rcvd, msecs_to_jiffies(CMD_RESP_TIME))) { | |
212 | pr_err(" waiting for ver info- timed out "); | |
70442664 | 213 | return -ETIMEDOUT; |
d0088ce1 | 214 | } |
74a4fcf1 | 215 | INIT_COMPLETION(kim_gdata->kim_rcvd); |
d0088ce1 PS |
216 | |
217 | version = | |
e2a53282 NJ |
218 | MAKEWORD(kim_gdata->resp_buffer[13], |
219 | kim_gdata->resp_buffer[14]); | |
d0088ce1 PS |
220 | chip = (version & 0x7C00) >> 10; |
221 | min_ver = (version & 0x007F); | |
222 | maj_ver = (version & 0x0380) >> 7; | |
223 | ||
224 | if (version & 0x8000) | |
225 | maj_ver |= 0x0008; | |
226 | ||
227 | sprintf(bts_scr_name, "TIInit_%d.%d.%d.bts", chip, maj_ver, min_ver); | |
e2a53282 NJ |
228 | |
229 | /* to be accessed later via sysfs entry */ | |
230 | kim_gdata->version.full = version; | |
231 | kim_gdata->version.chip = chip; | |
232 | kim_gdata->version.maj_ver = maj_ver; | |
233 | kim_gdata->version.min_ver = min_ver; | |
234 | ||
d0088ce1 | 235 | pr_info("%s", bts_scr_name); |
320920cb | 236 | return 0; |
d0088ce1 PS |
237 | } |
238 | ||
ef04d121 PS |
239 | void skip_change_remote_baud(unsigned char **ptr, long *len) |
240 | { | |
241 | unsigned char *nxt_action, *cur_action; | |
242 | cur_action = *ptr; | |
243 | ||
244 | nxt_action = cur_action + sizeof(struct bts_action) + | |
245 | ((struct bts_action *) cur_action)->size; | |
246 | ||
247 | if (((struct bts_action *) nxt_action)->type != ACTION_WAIT_EVENT) { | |
248 | pr_err("invalid action after change remote baud command"); | |
249 | } else { | |
250 | *ptr = *ptr + sizeof(struct bts_action) + | |
9d031d94 | 251 | ((struct bts_action *)cur_action)->size; |
ef04d121 | 252 | *len = *len - (sizeof(struct bts_action) + |
9d031d94 | 253 | ((struct bts_action *)cur_action)->size); |
ef04d121 PS |
254 | /* warn user on not commenting these in firmware */ |
255 | pr_warn("skipping the wait event of change remote baud"); | |
256 | } | |
257 | } | |
258 | ||
36b5aee4 PS |
259 | /** |
260 | * download_firmware - | |
261 | * internal function which parses through the .bts firmware | |
262 | * script file intreprets SEND, DELAY actions only as of now | |
d0088ce1 | 263 | */ |
38d9df49 | 264 | static long download_firmware(struct kim_data_s *kim_gdata) |
d0088ce1 | 265 | { |
320920cb | 266 | long err = 0; |
d0088ce1 | 267 | long len = 0; |
73f12e8d PS |
268 | unsigned char *ptr = NULL; |
269 | unsigned char *action_ptr = NULL; | |
d0088ce1 | 270 | unsigned char bts_scr_name[30] = { 0 }; /* 30 char long bts scr name? */ |
ef04d121 PS |
271 | int wr_room_space; |
272 | int cmd_size; | |
273 | unsigned long timeout; | |
d0088ce1 | 274 | |
38d9df49 | 275 | err = read_local_version(kim_gdata, bts_scr_name); |
320920cb | 276 | if (err != 0) { |
d0088ce1 PS |
277 | pr_err("kim: failed to read local ver"); |
278 | return err; | |
279 | } | |
280 | err = | |
281 | request_firmware(&kim_gdata->fw_entry, bts_scr_name, | |
282 | &kim_gdata->kim_pdev->dev); | |
283 | if (unlikely((err != 0) || (kim_gdata->fw_entry->data == NULL) || | |
284 | (kim_gdata->fw_entry->size == 0))) { | |
285 | pr_err(" request_firmware failed(errno %ld) for %s", err, | |
286 | bts_scr_name); | |
70442664 | 287 | return -EINVAL; |
d0088ce1 PS |
288 | } |
289 | ptr = (void *)kim_gdata->fw_entry->data; | |
290 | len = kim_gdata->fw_entry->size; | |
291 | /* bts_header to remove out magic number and | |
292 | * version | |
293 | */ | |
294 | ptr += sizeof(struct bts_header); | |
295 | len -= sizeof(struct bts_header); | |
296 | ||
297 | while (len > 0 && ptr) { | |
e6d9e64e | 298 | pr_debug(" action size %d, type %d ", |
d0088ce1 PS |
299 | ((struct bts_action *)ptr)->size, |
300 | ((struct bts_action *)ptr)->type); | |
301 | ||
302 | switch (((struct bts_action *)ptr)->type) { | |
303 | case ACTION_SEND_COMMAND: /* action send */ | |
2f81a02c | 304 | pr_debug("S"); |
d0088ce1 PS |
305 | action_ptr = &(((struct bts_action *)ptr)->data[0]); |
306 | if (unlikely | |
307 | (((struct hci_command *)action_ptr)->opcode == | |
308 | 0xFF36)) { | |
309 | /* ignore remote change | |
310 | * baud rate HCI VS command */ | |
ef04d121 | 311 | pr_warn("change remote baud" |
e6d9e64e | 312 | " rate command in firmware"); |
ef04d121 | 313 | skip_change_remote_baud(&ptr, &len); |
d0088ce1 PS |
314 | break; |
315 | } | |
ef04d121 PS |
316 | /* |
317 | * Make sure we have enough free space in uart | |
318 | * tx buffer to write current firmware command | |
319 | */ | |
320 | cmd_size = ((struct bts_action *)ptr)->size; | |
321 | timeout = jiffies + msecs_to_jiffies(CMD_WR_TIME); | |
322 | do { | |
323 | wr_room_space = | |
324 | st_get_uart_wr_room(kim_gdata->core_data); | |
325 | if (wr_room_space < 0) { | |
326 | pr_err("Unable to get free " | |
327 | "space info from uart tx buffer"); | |
328 | release_firmware(kim_gdata->fw_entry); | |
329 | return wr_room_space; | |
330 | } | |
331 | mdelay(1); /* wait 1ms before checking room */ | |
332 | } while ((wr_room_space < cmd_size) && | |
333 | time_before(jiffies, timeout)); | |
334 | ||
335 | /* Timeout happened ? */ | |
336 | if (time_after_eq(jiffies, timeout)) { | |
337 | pr_err("Timeout while waiting for free " | |
338 | "free space in uart tx buffer"); | |
339 | release_firmware(kim_gdata->fw_entry); | |
340 | return -ETIMEDOUT; | |
341 | } | |
2f81a02c PS |
342 | /* reinit completion before sending for the |
343 | * relevant wait | |
344 | */ | |
345 | INIT_COMPLETION(kim_gdata->kim_rcvd); | |
d0088ce1 | 346 | |
ef04d121 PS |
347 | /* |
348 | * Free space found in uart buffer, call st_int_write | |
349 | * to send current firmware command to the uart tx | |
350 | * buffer. | |
351 | */ | |
d0088ce1 PS |
352 | err = st_int_write(kim_gdata->core_data, |
353 | ((struct bts_action_send *)action_ptr)->data, | |
354 | ((struct bts_action *)ptr)->size); | |
355 | if (unlikely(err < 0)) { | |
356 | release_firmware(kim_gdata->fw_entry); | |
70442664 | 357 | return err; |
d0088ce1 | 358 | } |
ef04d121 PS |
359 | /* |
360 | * Check number of bytes written to the uart tx buffer | |
361 | * and requested command write size | |
362 | */ | |
363 | if (err != cmd_size) { | |
364 | pr_err("Number of bytes written to uart " | |
365 | "tx buffer are not matching with " | |
366 | "requested cmd write size"); | |
367 | release_firmware(kim_gdata->fw_entry); | |
368 | return -EIO; | |
369 | } | |
370 | break; | |
371 | case ACTION_WAIT_EVENT: /* wait */ | |
2f81a02c | 372 | pr_debug("W"); |
d0088ce1 | 373 | if (!wait_for_completion_timeout |
ef04d121 PS |
374 | (&kim_gdata->kim_rcvd, |
375 | msecs_to_jiffies(CMD_RESP_TIME))) { | |
376 | pr_err("response timeout during fw download "); | |
d0088ce1 PS |
377 | /* timed out */ |
378 | release_firmware(kim_gdata->fw_entry); | |
70442664 | 379 | return -ETIMEDOUT; |
d0088ce1 | 380 | } |
ef04d121 | 381 | INIT_COMPLETION(kim_gdata->kim_rcvd); |
d0088ce1 PS |
382 | break; |
383 | case ACTION_DELAY: /* sleep */ | |
384 | pr_info("sleep command in scr"); | |
385 | action_ptr = &(((struct bts_action *)ptr)->data[0]); | |
386 | mdelay(((struct bts_action_delay *)action_ptr)->msec); | |
387 | break; | |
388 | } | |
389 | len = | |
390 | len - (sizeof(struct bts_action) + | |
391 | ((struct bts_action *)ptr)->size); | |
392 | ptr = | |
393 | ptr + sizeof(struct bts_action) + | |
394 | ((struct bts_action *)ptr)->size; | |
395 | } | |
396 | /* fw download complete */ | |
397 | release_firmware(kim_gdata->fw_entry); | |
320920cb | 398 | return 0; |
d0088ce1 PS |
399 | } |
400 | ||
401 | /**********************************************************************/ | |
402 | /* functions called from ST core */ | |
d0088ce1 PS |
403 | /* called from ST Core, when REG_IN_PROGRESS (registration in progress) |
404 | * can be because of | |
405 | * 1. response to read local version | |
406 | * 2. during send/recv's of firmware download | |
407 | */ | |
408 | void st_kim_recv(void *disc_data, const unsigned char *data, long count) | |
409 | { | |
38d9df49 PS |
410 | struct st_data_s *st_gdata = (struct st_data_s *)disc_data; |
411 | struct kim_data_s *kim_gdata = st_gdata->kim_data; | |
412 | ||
d0088ce1 PS |
413 | /* copy to local buffer */ |
414 | if (unlikely(data[4] == 0x01 && data[5] == 0x10 && data[0] == 0x04)) { | |
415 | /* must be the read_ver_cmd */ | |
416 | memcpy(kim_gdata->resp_buffer, data, count); | |
417 | complete_all(&kim_gdata->kim_rcvd); | |
418 | return; | |
419 | } else { | |
38d9df49 | 420 | kim_int_recv(kim_gdata, data, count); |
d0088ce1 PS |
421 | /* either completes or times out */ |
422 | } | |
423 | return; | |
424 | } | |
425 | ||
426 | /* to signal completion of line discipline installation | |
427 | * called from ST Core, upon tty_open | |
428 | */ | |
38d9df49 | 429 | void st_kim_complete(void *kim_data) |
d0088ce1 | 430 | { |
38d9df49 | 431 | struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data; |
d0088ce1 PS |
432 | complete(&kim_gdata->ldisc_installed); |
433 | } | |
434 | ||
36b5aee4 PS |
435 | /** |
436 | * st_kim_start - called from ST Core upon 1st registration | |
437 | * This involves toggling the chip enable gpio, reading | |
438 | * the firmware version from chip, forming the fw file name | |
439 | * based on the chip version, requesting the fw, parsing it | |
440 | * and perform download(send/recv). | |
441 | */ | |
38d9df49 | 442 | long st_kim_start(void *kim_data) |
d0088ce1 | 443 | { |
320920cb | 444 | long err = 0; |
d0088ce1 | 445 | long retry = POR_RETRY_COUNT; |
0d7c5f25 | 446 | struct ti_st_plat_data *pdata; |
38d9df49 PS |
447 | struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data; |
448 | ||
d0088ce1 | 449 | pr_info(" %s", __func__); |
0d7c5f25 | 450 | pdata = kim_gdata->kim_pdev->dev.platform_data; |
d0088ce1 PS |
451 | |
452 | do { | |
0d7c5f25 PS |
453 | /* platform specific enabling code here */ |
454 | if (pdata->chip_enable) | |
455 | pdata->chip_enable(kim_gdata); | |
456 | ||
d0088ce1 | 457 | /* Configure BT nShutdown to HIGH state */ |
781a7395 | 458 | gpio_set_value(kim_gdata->nshutdown, GPIO_LOW); |
d0088ce1 | 459 | mdelay(5); /* FIXME: a proper toggle */ |
781a7395 | 460 | gpio_set_value(kim_gdata->nshutdown, GPIO_HIGH); |
d0088ce1 PS |
461 | mdelay(100); |
462 | /* re-initialize the completion */ | |
463 | INIT_COMPLETION(kim_gdata->ldisc_installed); | |
ec60d0ad PS |
464 | /* send notification to UIM */ |
465 | kim_gdata->ldisc_install = 1; | |
466 | pr_info("ldisc_install = 1"); | |
467 | sysfs_notify(&kim_gdata->kim_pdev->dev.kobj, | |
468 | NULL, "install"); | |
d0088ce1 PS |
469 | /* wait for ldisc to be installed */ |
470 | err = wait_for_completion_timeout(&kim_gdata->ldisc_installed, | |
471 | msecs_to_jiffies(LDISC_TIME)); | |
472 | if (!err) { /* timeout */ | |
473 | pr_err("line disc installation timed out "); | |
ec60d0ad PS |
474 | kim_gdata->ldisc_install = 0; |
475 | pr_info("ldisc_install = 0"); | |
476 | sysfs_notify(&kim_gdata->kim_pdev->dev.kobj, | |
477 | NULL, "install"); | |
d0344ef6 PS |
478 | /* the following wait is never going to be completed, |
479 | * since the ldisc was never installed, hence serving | |
480 | * as a mdelay of LDISC_TIME msecs */ | |
481 | err = wait_for_completion_timeout | |
482 | (&kim_gdata->ldisc_installed, | |
483 | msecs_to_jiffies(LDISC_TIME)); | |
70442664 | 484 | err = -ETIMEDOUT; |
d0088ce1 PS |
485 | continue; |
486 | } else { | |
487 | /* ldisc installed now */ | |
488 | pr_info(" line discipline installed "); | |
38d9df49 | 489 | err = download_firmware(kim_gdata); |
320920cb | 490 | if (err != 0) { |
d0088ce1 | 491 | pr_err("download firmware failed"); |
ec60d0ad PS |
492 | kim_gdata->ldisc_install = 0; |
493 | pr_info("ldisc_install = 0"); | |
494 | sysfs_notify(&kim_gdata->kim_pdev->dev.kobj, | |
495 | NULL, "install"); | |
d0344ef6 PS |
496 | /* this wait might be completed, though in the |
497 | * tty_close() since the ldisc is already | |
498 | * installed */ | |
499 | err = wait_for_completion_timeout | |
500 | (&kim_gdata->ldisc_installed, | |
501 | msecs_to_jiffies(LDISC_TIME)); | |
502 | err = -EINVAL; | |
d0088ce1 PS |
503 | continue; |
504 | } else { /* on success don't retry */ | |
505 | break; | |
506 | } | |
507 | } | |
508 | } while (retry--); | |
509 | return err; | |
510 | } | |
511 | ||
36b5aee4 PS |
512 | /** |
513 | * st_kim_stop - called from ST Core, on the last un-registration | |
514 | * toggle low the chip enable gpio | |
515 | */ | |
38d9df49 | 516 | long st_kim_stop(void *kim_data) |
d0088ce1 | 517 | { |
320920cb | 518 | long err = 0; |
38d9df49 | 519 | struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data; |
0d7c5f25 PS |
520 | struct ti_st_plat_data *pdata = |
521 | kim_gdata->kim_pdev->dev.platform_data; | |
d0088ce1 PS |
522 | |
523 | INIT_COMPLETION(kim_gdata->ldisc_installed); | |
ec60d0ad PS |
524 | |
525 | /* Flush any pending characters in the driver and discipline. */ | |
526 | tty_ldisc_flush(kim_gdata->core_data->tty); | |
527 | tty_driver_flush_buffer(kim_gdata->core_data->tty); | |
528 | ||
529 | /* send uninstall notification to UIM */ | |
530 | pr_info("ldisc_install = 0"); | |
531 | kim_gdata->ldisc_install = 0; | |
532 | sysfs_notify(&kim_gdata->kim_pdev->dev.kobj, NULL, "install"); | |
d0088ce1 PS |
533 | |
534 | /* wait for ldisc to be un-installed */ | |
535 | err = wait_for_completion_timeout(&kim_gdata->ldisc_installed, | |
536 | msecs_to_jiffies(LDISC_TIME)); | |
537 | if (!err) { /* timeout */ | |
538 | pr_err(" timed out waiting for ldisc to be un-installed"); | |
70442664 | 539 | return -ETIMEDOUT; |
d0088ce1 PS |
540 | } |
541 | ||
542 | /* By default configure BT nShutdown to LOW state */ | |
781a7395 | 543 | gpio_set_value(kim_gdata->nshutdown, GPIO_LOW); |
d0088ce1 | 544 | mdelay(1); |
781a7395 | 545 | gpio_set_value(kim_gdata->nshutdown, GPIO_HIGH); |
d0088ce1 | 546 | mdelay(1); |
781a7395 | 547 | gpio_set_value(kim_gdata->nshutdown, GPIO_LOW); |
0d7c5f25 PS |
548 | |
549 | /* platform specific disable */ | |
550 | if (pdata->chip_disable) | |
551 | pdata->chip_disable(kim_gdata); | |
d0088ce1 PS |
552 | return err; |
553 | } | |
554 | ||
555 | /**********************************************************************/ | |
556 | /* functions called from subsystems */ | |
c1afac15 | 557 | /* called when debugfs entry is read from */ |
e2a53282 | 558 | |
c1afac15 | 559 | static int show_version(struct seq_file *s, void *unused) |
e2a53282 | 560 | { |
c1afac15 PS |
561 | struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private; |
562 | seq_printf(s, "%04X %d.%d.%d\n", kim_gdata->version.full, | |
e2a53282 NJ |
563 | kim_gdata->version.chip, kim_gdata->version.maj_ver, |
564 | kim_gdata->version.min_ver); | |
c1afac15 | 565 | return 0; |
d0088ce1 PS |
566 | } |
567 | ||
c1afac15 | 568 | static int show_list(struct seq_file *s, void *unused) |
d0088ce1 | 569 | { |
c1afac15 PS |
570 | struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private; |
571 | kim_st_list_protocols(kim_gdata->core_data, s); | |
572 | return 0; | |
d0088ce1 PS |
573 | } |
574 | ||
ec60d0ad PS |
575 | static ssize_t show_install(struct device *dev, |
576 | struct device_attribute *attr, char *buf) | |
d0088ce1 | 577 | { |
ec60d0ad PS |
578 | struct kim_data_s *kim_data = dev_get_drvdata(dev); |
579 | return sprintf(buf, "%d\n", kim_data->ldisc_install); | |
580 | } | |
d0088ce1 | 581 | |
ec60d0ad PS |
582 | static ssize_t show_dev_name(struct device *dev, |
583 | struct device_attribute *attr, char *buf) | |
584 | { | |
585 | struct kim_data_s *kim_data = dev_get_drvdata(dev); | |
586 | return sprintf(buf, "%s\n", kim_data->dev_name); | |
587 | } | |
588 | ||
589 | static ssize_t show_baud_rate(struct device *dev, | |
590 | struct device_attribute *attr, char *buf) | |
591 | { | |
592 | struct kim_data_s *kim_data = dev_get_drvdata(dev); | |
593 | return sprintf(buf, "%ld\n", kim_data->baud_rate); | |
594 | } | |
595 | ||
596 | static ssize_t show_flow_cntrl(struct device *dev, | |
597 | struct device_attribute *attr, char *buf) | |
598 | { | |
599 | struct kim_data_s *kim_data = dev_get_drvdata(dev); | |
600 | return sprintf(buf, "%d\n", kim_data->flow_cntrl); | |
d0088ce1 PS |
601 | } |
602 | ||
ec60d0ad PS |
603 | /* structures specific for sysfs entries */ |
604 | static struct kobj_attribute ldisc_install = | |
605 | __ATTR(install, 0444, (void *)show_install, NULL); | |
606 | ||
607 | static struct kobj_attribute uart_dev_name = | |
608 | __ATTR(dev_name, 0444, (void *)show_dev_name, NULL); | |
609 | ||
610 | static struct kobj_attribute uart_baud_rate = | |
611 | __ATTR(baud_rate, 0444, (void *)show_baud_rate, NULL); | |
612 | ||
613 | static struct kobj_attribute uart_flow_cntrl = | |
614 | __ATTR(flow_cntrl, 0444, (void *)show_flow_cntrl, NULL); | |
615 | ||
616 | static struct attribute *uim_attrs[] = { | |
617 | &ldisc_install.attr, | |
618 | &uart_dev_name.attr, | |
619 | &uart_baud_rate.attr, | |
620 | &uart_flow_cntrl.attr, | |
621 | NULL, | |
622 | }; | |
623 | ||
624 | static struct attribute_group uim_attr_grp = { | |
625 | .attrs = uim_attrs, | |
626 | }; | |
627 | ||
36b5aee4 PS |
628 | /** |
629 | * st_kim_ref - reference the core's data | |
630 | * This references the per-ST platform device in the arch/xx/ | |
631 | * board-xx.c file. | |
632 | * This would enable multiple such platform devices to exist | |
633 | * on a given platform | |
634 | */ | |
dbd3a870 | 635 | void st_kim_ref(struct st_data_s **core_data, int id) |
d0088ce1 | 636 | { |
38d9df49 PS |
637 | struct platform_device *pdev; |
638 | struct kim_data_s *kim_gdata; | |
639 | /* get kim_gdata reference from platform device */ | |
dbd3a870 | 640 | pdev = st_get_plat_device(id); |
7316a9f2 SR |
641 | if (!pdev) { |
642 | *core_data = NULL; | |
643 | return; | |
644 | } | |
38d9df49 | 645 | kim_gdata = dev_get_drvdata(&pdev->dev); |
d0088ce1 PS |
646 | *core_data = kim_gdata->core_data; |
647 | } | |
648 | ||
c1afac15 PS |
649 | static int kim_version_open(struct inode *i, struct file *f) |
650 | { | |
651 | return single_open(f, show_version, i->i_private); | |
652 | } | |
653 | ||
654 | static int kim_list_open(struct inode *i, struct file *f) | |
655 | { | |
656 | return single_open(f, show_list, i->i_private); | |
657 | } | |
658 | ||
659 | static const struct file_operations version_debugfs_fops = { | |
660 | /* version info */ | |
661 | .open = kim_version_open, | |
662 | .read = seq_read, | |
663 | .llseek = seq_lseek, | |
664 | .release = single_release, | |
665 | }; | |
666 | static const struct file_operations list_debugfs_fops = { | |
667 | /* protocols info */ | |
668 | .open = kim_list_open, | |
669 | .read = seq_read, | |
670 | .llseek = seq_lseek, | |
671 | .release = single_release, | |
672 | }; | |
673 | ||
d0088ce1 PS |
674 | /**********************************************************************/ |
675 | /* functions called from platform device driver subsystem | |
676 | * need to have a relevant platform device entry in the platform's | |
677 | * board-*.c file | |
678 | */ | |
679 | ||
c1afac15 | 680 | struct dentry *kim_debugfs_dir; |
d0088ce1 PS |
681 | static int kim_probe(struct platform_device *pdev) |
682 | { | |
683 | long status; | |
38d9df49 | 684 | struct kim_data_s *kim_gdata; |
ec60d0ad | 685 | struct ti_st_plat_data *pdata = pdev->dev.platform_data; |
38d9df49 | 686 | |
dfb7ef7d PS |
687 | if ((pdev->id != -1) && (pdev->id < MAX_ST_DEVICES)) { |
688 | /* multiple devices could exist */ | |
689 | st_kim_devices[pdev->id] = pdev; | |
690 | } else { | |
25985edc | 691 | /* platform's sure about existence of 1 device */ |
dfb7ef7d PS |
692 | st_kim_devices[0] = pdev; |
693 | } | |
694 | ||
38d9df49 PS |
695 | kim_gdata = kzalloc(sizeof(struct kim_data_s), GFP_ATOMIC); |
696 | if (!kim_gdata) { | |
697 | pr_err("no mem to allocate"); | |
698 | return -ENOMEM; | |
699 | } | |
700 | dev_set_drvdata(&pdev->dev, kim_gdata); | |
d0088ce1 PS |
701 | |
702 | status = st_core_init(&kim_gdata->core_data); | |
703 | if (status != 0) { | |
704 | pr_err(" ST core init failed"); | |
70442664 | 705 | return -EIO; |
d0088ce1 | 706 | } |
38d9df49 PS |
707 | /* refer to itself */ |
708 | kim_gdata->core_data->kim_data = kim_gdata; | |
d0088ce1 | 709 | |
781a7395 PS |
710 | /* Claim the chip enable nShutdown gpio from the system */ |
711 | kim_gdata->nshutdown = pdata->nshutdown_gpio; | |
712 | status = gpio_request(kim_gdata->nshutdown, "kim"); | |
713 | if (unlikely(status)) { | |
714 | pr_err(" gpio %ld request failed ", kim_gdata->nshutdown); | |
715 | return status; | |
d0088ce1 PS |
716 | } |
717 | ||
781a7395 PS |
718 | /* Configure nShutdown GPIO as output=0 */ |
719 | status = gpio_direction_output(kim_gdata->nshutdown, 0); | |
720 | if (unlikely(status)) { | |
721 | pr_err(" unable to configure gpio %ld", kim_gdata->nshutdown); | |
722 | return status; | |
d0088ce1 | 723 | } |
d0088ce1 PS |
724 | /* get reference of pdev for request_firmware |
725 | */ | |
726 | kim_gdata->kim_pdev = pdev; | |
727 | init_completion(&kim_gdata->kim_rcvd); | |
728 | init_completion(&kim_gdata->ldisc_installed); | |
b38fc2d9 | 729 | |
ec60d0ad PS |
730 | status = sysfs_create_group(&pdev->dev.kobj, &uim_attr_grp); |
731 | if (status) { | |
732 | pr_err("failed to create sysfs entries"); | |
733 | return status; | |
d0088ce1 | 734 | } |
e2a53282 | 735 | |
ec60d0ad PS |
736 | /* copying platform data */ |
737 | strncpy(kim_gdata->dev_name, pdata->dev_name, UART_DEV_NAME_LEN); | |
738 | kim_gdata->flow_cntrl = pdata->flow_cntrl; | |
739 | kim_gdata->baud_rate = pdata->baud_rate; | |
740 | pr_info("sysfs entries created\n"); | |
741 | ||
c1afac15 PS |
742 | kim_debugfs_dir = debugfs_create_dir("ti-st", NULL); |
743 | if (IS_ERR(kim_debugfs_dir)) { | |
744 | pr_err(" debugfs entries creation failed "); | |
745 | kim_debugfs_dir = NULL; | |
70442664 | 746 | return -EIO; |
e2a53282 | 747 | } |
c1afac15 PS |
748 | |
749 | debugfs_create_file("version", S_IRUGO, kim_debugfs_dir, | |
750 | kim_gdata, &version_debugfs_fops); | |
751 | debugfs_create_file("protocols", S_IRUGO, kim_debugfs_dir, | |
752 | kim_gdata, &list_debugfs_fops); | |
753 | pr_info(" debugfs entries created "); | |
320920cb | 754 | return 0; |
d0088ce1 PS |
755 | } |
756 | ||
757 | static int kim_remove(struct platform_device *pdev) | |
758 | { | |
ec60d0ad PS |
759 | /* free the GPIOs requested */ |
760 | struct ti_st_plat_data *pdata = pdev->dev.platform_data; | |
38d9df49 PS |
761 | struct kim_data_s *kim_gdata; |
762 | ||
763 | kim_gdata = dev_get_drvdata(&pdev->dev); | |
d0088ce1 | 764 | |
781a7395 PS |
765 | /* Free the Bluetooth/FM/GPIO |
766 | * nShutdown gpio from the system | |
767 | */ | |
768 | gpio_free(pdata->nshutdown_gpio); | |
769 | pr_info("nshutdown GPIO Freed"); | |
ec60d0ad | 770 | |
781a7395 | 771 | debugfs_remove_recursive(kim_debugfs_dir); |
ec60d0ad | 772 | sysfs_remove_group(&pdev->dev.kobj, &uim_attr_grp); |
781a7395 PS |
773 | pr_info("sysfs entries removed"); |
774 | ||
d0088ce1 PS |
775 | kim_gdata->kim_pdev = NULL; |
776 | st_core_exit(kim_gdata->core_data); | |
38d9df49 PS |
777 | |
778 | kfree(kim_gdata); | |
779 | kim_gdata = NULL; | |
320920cb | 780 | return 0; |
d0088ce1 PS |
781 | } |
782 | ||
ec60d0ad PS |
783 | int kim_suspend(struct platform_device *pdev, pm_message_t state) |
784 | { | |
785 | struct ti_st_plat_data *pdata = pdev->dev.platform_data; | |
786 | ||
787 | if (pdata->suspend) | |
788 | return pdata->suspend(pdev, state); | |
789 | ||
790 | return -EOPNOTSUPP; | |
791 | } | |
792 | ||
793 | int kim_resume(struct platform_device *pdev) | |
794 | { | |
795 | struct ti_st_plat_data *pdata = pdev->dev.platform_data; | |
796 | ||
797 | if (pdata->resume) | |
798 | return pdata->resume(pdev); | |
799 | ||
800 | return -EOPNOTSUPP; | |
801 | } | |
802 | ||
d0088ce1 PS |
803 | /**********************************************************************/ |
804 | /* entry point for ST KIM module, called in from ST Core */ | |
ec60d0ad PS |
805 | static struct platform_driver kim_platform_driver = { |
806 | .probe = kim_probe, | |
807 | .remove = kim_remove, | |
808 | .suspend = kim_suspend, | |
809 | .resume = kim_resume, | |
810 | .driver = { | |
811 | .name = "kim", | |
812 | .owner = THIS_MODULE, | |
813 | }, | |
814 | }; | |
d0088ce1 PS |
815 | |
816 | static int __init st_kim_init(void) | |
817 | { | |
ec60d0ad | 818 | return platform_driver_register(&kim_platform_driver); |
d0088ce1 PS |
819 | } |
820 | ||
821 | static void __exit st_kim_deinit(void) | |
822 | { | |
d0088ce1 | 823 | platform_driver_unregister(&kim_platform_driver); |
d0088ce1 PS |
824 | } |
825 | ||
826 | ||
827 | module_init(st_kim_init); | |
828 | module_exit(st_kim_deinit); | |
829 | MODULE_AUTHOR("Pavan Savoy <pavan_savoy@ti.com>"); | |
830 | MODULE_DESCRIPTION("Shared Transport Driver for TI BT/FM/GPS combo chips "); | |
831 | MODULE_LICENSE("GPL"); |