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