blob: 7e34e4d44c313b9fdb38b3d71250178f36549ed7 [file] [log] [blame]
Pavan Savoyd0088ce2010-04-08 13:16:54 -05001/*
2 * Shared Transport Line discipline driver Core
3 * Init Manager module responsible for GPIO control
4 * and firmware download
5 * Copyright (C) 2009 Texas Instruments
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#define pr_fmt(fmt) "(stk) :" fmt
23#include <linux/platform_device.h>
24#include <linux/jiffies.h>
25#include <linux/firmware.h>
26#include <linux/delay.h>
27#include <linux/wait.h>
28#include <linux/gpio.h>
29
30#include <linux/sched.h>
31
32#include "st_kim.h"
33/* understand BT events for fw response */
34#include <net/bluetooth/bluetooth.h>
35#include <net/bluetooth/hci_core.h>
36#include <net/bluetooth/hci.h>
37
38
39static int kim_probe(struct platform_device *pdev);
40static int kim_remove(struct platform_device *pdev);
41
42/* KIM platform device driver structure */
43static struct platform_driver kim_platform_driver = {
44 .probe = kim_probe,
45 .remove = kim_remove,
46 /* TODO: ST driver power management during suspend/resume ?
47 */
48#if 0
49 .suspend = kim_suspend,
50 .resume = kim_resume,
51#endif
52 .driver = {
53 .name = "kim",
54 .owner = THIS_MODULE,
55 },
56};
57
Pavan Savoyd0088ce2010-04-08 13:16:54 -050058static ssize_t show_pid(struct device *dev, struct device_attribute
Naveen Jaine2a53282010-06-09 03:45:33 -050059 *attr, char *buf);
Pavan Savoyd0088ce2010-04-08 13:16:54 -050060static ssize_t store_pid(struct device *dev, struct device_attribute
Naveen Jaine2a53282010-06-09 03:45:33 -050061 *devattr, char *buf, size_t count);
Pavan Savoyd0088ce2010-04-08 13:16:54 -050062static ssize_t show_list(struct device *dev, struct device_attribute
Naveen Jaine2a53282010-06-09 03:45:33 -050063 *attr, char *buf);
64static ssize_t show_version(struct device *dev, struct device_attribute
65 *attr, char *buf);
Pavan Savoyd0088ce2010-04-08 13:16:54 -050066/* structures specific for sysfs entries */
67static struct kobj_attribute pid_attr =
68__ATTR(pid, 0644, (void *)show_pid, (void *)store_pid);
69
70static struct kobj_attribute list_protocols =
71__ATTR(protocols, 0444, (void *)show_list, NULL);
72
Naveen Jaine2a53282010-06-09 03:45:33 -050073static struct kobj_attribute chip_version =
74__ATTR(version, 0444, (void *)show_version, NULL);
75
Pavan Savoyd0088ce2010-04-08 13:16:54 -050076static struct attribute *uim_attrs[] = {
77 &pid_attr.attr,
78 /* add more debug sysfs entries */
79 &list_protocols.attr,
Naveen Jaine2a53282010-06-09 03:45:33 -050080 &chip_version.attr,
Pavan Savoyd0088ce2010-04-08 13:16:54 -050081 NULL,
82};
83
84static struct attribute_group uim_attr_grp = {
85 .attrs = uim_attrs,
86};
Naveen Jainb38fc2d2010-06-09 03:45:32 -050087
Pavan Savoyd0088ce2010-04-08 13:16:54 -050088static int kim_toggle_radio(void*, bool);
89static const struct rfkill_ops kim_rfkill_ops = {
90 .set_block = kim_toggle_radio,
91};
Pavan Savoyd0088ce2010-04-08 13:16:54 -050092
93/* strings to be used for rfkill entries and by
94 * ST Core to be used for sysfs debug entry
95 */
96#define PROTO_ENTRY(type, name) name
97const unsigned char *protocol_names[] = {
98 PROTO_ENTRY(ST_BT, "Bluetooth"),
99 PROTO_ENTRY(ST_FM, "FM"),
100 PROTO_ENTRY(ST_GPS, "GPS"),
101};
102
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500103
104/**********************************************************************/
105/* internal functions */
106
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500107/**
108 * validate_firmware_response -
109 * function to return whether the firmware response was proper
110 * in case of error don't complete so that waiting for proper
111 * response times out
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500112 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500113void validate_firmware_response(struct kim_data_s *kim_gdata)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500114{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500115 struct sk_buff *skb = kim_gdata->rx_skb;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500116 if (unlikely(skb->data[5] != 0)) {
117 pr_err("no proper response during fw download");
118 pr_err("data6 %x", skb->data[5]);
119 return; /* keep waiting for the proper response */
120 }
121 /* becos of all the script being downloaded */
122 complete_all(&kim_gdata->kim_rcvd);
123 kfree_skb(skb);
124}
125
126/* check for data len received inside kim_int_recv
127 * most often hit the last case to update state to waiting for data
128 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500129static inline int kim_check_data_len(struct kim_data_s *kim_gdata, int len)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500130{
131 register int room = skb_tailroom(kim_gdata->rx_skb);
132
Pavan Savoye6d9e642010-07-22 05:32:05 -0500133 pr_debug("len %d room %d", len, room);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500134
135 if (!len) {
Pavan Savoy38d9df42010-07-08 08:55:17 -0500136 validate_firmware_response(kim_gdata);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500137 } else if (len > room) {
138 /* Received packet's payload length is larger.
139 * We can't accommodate it in created skb.
140 */
141 pr_err("Data length is too large len %d room %d", len,
142 room);
143 kfree_skb(kim_gdata->rx_skb);
144 } else {
145 /* Packet header has non-zero payload length and
146 * we have enough space in created skb. Lets read
147 * payload data */
148 kim_gdata->rx_state = ST_BT_W4_DATA;
149 kim_gdata->rx_count = len;
150 return len;
151 }
152
153 /* Change ST LL state to continue to process next
154 * packet */
155 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
156 kim_gdata->rx_skb = NULL;
157 kim_gdata->rx_count = 0;
158
159 return 0;
160}
161
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500162/**
163 * kim_int_recv - receive function called during firmware download
164 * firmware download responses on different UART drivers
165 * have been observed to come in bursts of different
166 * tty_receive and hence the logic
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500167 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500168void kim_int_recv(struct kim_data_s *kim_gdata,
169 const unsigned char *data, long count)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500170{
171 register char *ptr;
172 struct hci_event_hdr *eh;
173 register int len = 0, type = 0;
174
Pavan Savoye6d9e642010-07-22 05:32:05 -0500175 pr_debug("%s", __func__);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500176 /* Decode received bytes here */
177 ptr = (char *)data;
178 if (unlikely(ptr == NULL)) {
179 pr_err(" received null from TTY ");
180 return;
181 }
182 while (count) {
183 if (kim_gdata->rx_count) {
184 len = min_t(unsigned int, kim_gdata->rx_count, count);
185 memcpy(skb_put(kim_gdata->rx_skb, len), ptr, len);
186 kim_gdata->rx_count -= len;
187 count -= len;
188 ptr += len;
189
190 if (kim_gdata->rx_count)
191 continue;
192
193 /* Check ST RX state machine , where are we? */
194 switch (kim_gdata->rx_state) {
195 /* Waiting for complete packet ? */
196 case ST_BT_W4_DATA:
Pavan Savoye6d9e642010-07-22 05:32:05 -0500197 pr_debug("Complete pkt received");
Pavan Savoy38d9df42010-07-08 08:55:17 -0500198 validate_firmware_response(kim_gdata);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500199 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
200 kim_gdata->rx_skb = NULL;
201 continue;
202 /* Waiting for Bluetooth event header ? */
203 case ST_BT_W4_EVENT_HDR:
204 eh = (struct hci_event_hdr *)kim_gdata->
205 rx_skb->data;
Pavan Savoye6d9e642010-07-22 05:32:05 -0500206 pr_debug("Event header: evt 0x%2.2x"
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500207 "plen %d", eh->evt, eh->plen);
Pavan Savoy38d9df42010-07-08 08:55:17 -0500208 kim_check_data_len(kim_gdata, eh->plen);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500209 continue;
210 } /* end of switch */
211 } /* end of if rx_state */
212 switch (*ptr) {
213 /* Bluetooth event packet? */
214 case HCI_EVENT_PKT:
215 pr_info("Event packet");
216 kim_gdata->rx_state = ST_BT_W4_EVENT_HDR;
217 kim_gdata->rx_count = HCI_EVENT_HDR_SIZE;
218 type = HCI_EVENT_PKT;
219 break;
220 default:
221 pr_info("unknown packet");
222 ptr++;
223 count--;
224 continue;
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500225 }
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500226 ptr++;
227 count--;
228 kim_gdata->rx_skb =
229 bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
230 if (!kim_gdata->rx_skb) {
231 pr_err("can't allocate mem for new packet");
232 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
233 kim_gdata->rx_count = 0;
234 return;
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500235 }
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500236 bt_cb(kim_gdata->rx_skb)->pkt_type = type;
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500237 }
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500238 pr_info("done %s", __func__);
239 return;
240}
241
Pavan Savoy38d9df42010-07-08 08:55:17 -0500242static long read_local_version(struct kim_data_s *kim_gdata, char *bts_scr_name)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500243{
244 unsigned short version = 0, chip = 0, min_ver = 0, maj_ver = 0;
245 char read_ver_cmd[] = { 0x01, 0x01, 0x10, 0x00 };
246
Pavan Savoye6d9e642010-07-22 05:32:05 -0500247 pr_debug("%s", __func__);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500248
249 INIT_COMPLETION(kim_gdata->kim_rcvd);
250 if (4 != st_int_write(kim_gdata->core_data, read_ver_cmd, 4)) {
251 pr_err("kim: couldn't write 4 bytes");
Pavan Savoy320920c2010-07-14 08:21:12 -0500252 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500253 }
254
255 if (!wait_for_completion_timeout
256 (&kim_gdata->kim_rcvd, msecs_to_jiffies(CMD_RESP_TIME))) {
257 pr_err(" waiting for ver info- timed out ");
Pavan Savoy320920c2010-07-14 08:21:12 -0500258 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500259 }
260
261 version =
Naveen Jaine2a53282010-06-09 03:45:33 -0500262 MAKEWORD(kim_gdata->resp_buffer[13],
263 kim_gdata->resp_buffer[14]);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500264 chip = (version & 0x7C00) >> 10;
265 min_ver = (version & 0x007F);
266 maj_ver = (version & 0x0380) >> 7;
267
268 if (version & 0x8000)
269 maj_ver |= 0x0008;
270
271 sprintf(bts_scr_name, "TIInit_%d.%d.%d.bts", chip, maj_ver, min_ver);
Naveen Jaine2a53282010-06-09 03:45:33 -0500272
273 /* to be accessed later via sysfs entry */
274 kim_gdata->version.full = version;
275 kim_gdata->version.chip = chip;
276 kim_gdata->version.maj_ver = maj_ver;
277 kim_gdata->version.min_ver = min_ver;
278
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500279 pr_info("%s", bts_scr_name);
Pavan Savoy320920c2010-07-14 08:21:12 -0500280 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500281}
282
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500283/**
284 * download_firmware -
285 * internal function which parses through the .bts firmware
286 * script file intreprets SEND, DELAY actions only as of now
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500287 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500288static long download_firmware(struct kim_data_s *kim_gdata)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500289{
Pavan Savoy320920c2010-07-14 08:21:12 -0500290 long err = 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500291 long len = 0;
292 register unsigned char *ptr = NULL;
293 register unsigned char *action_ptr = NULL;
294 unsigned char bts_scr_name[30] = { 0 }; /* 30 char long bts scr name? */
295
Pavan Savoy38d9df42010-07-08 08:55:17 -0500296 err = read_local_version(kim_gdata, bts_scr_name);
Pavan Savoy320920c2010-07-14 08:21:12 -0500297 if (err != 0) {
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500298 pr_err("kim: failed to read local ver");
299 return err;
300 }
301 err =
302 request_firmware(&kim_gdata->fw_entry, bts_scr_name,
303 &kim_gdata->kim_pdev->dev);
304 if (unlikely((err != 0) || (kim_gdata->fw_entry->data == NULL) ||
305 (kim_gdata->fw_entry->size == 0))) {
306 pr_err(" request_firmware failed(errno %ld) for %s", err,
307 bts_scr_name);
Pavan Savoy320920c2010-07-14 08:21:12 -0500308 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500309 }
310 ptr = (void *)kim_gdata->fw_entry->data;
311 len = kim_gdata->fw_entry->size;
312 /* bts_header to remove out magic number and
313 * version
314 */
315 ptr += sizeof(struct bts_header);
316 len -= sizeof(struct bts_header);
317
318 while (len > 0 && ptr) {
Pavan Savoye6d9e642010-07-22 05:32:05 -0500319 pr_debug(" action size %d, type %d ",
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500320 ((struct bts_action *)ptr)->size,
321 ((struct bts_action *)ptr)->type);
322
323 switch (((struct bts_action *)ptr)->type) {
324 case ACTION_SEND_COMMAND: /* action send */
325 action_ptr = &(((struct bts_action *)ptr)->data[0]);
326 if (unlikely
327 (((struct hci_command *)action_ptr)->opcode ==
328 0xFF36)) {
329 /* ignore remote change
330 * baud rate HCI VS command */
331 pr_err
Pavan Savoye6d9e642010-07-22 05:32:05 -0500332 (" change remote baud"
333 " rate command in firmware");
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500334 break;
335 }
336
337 INIT_COMPLETION(kim_gdata->kim_rcvd);
338 err = st_int_write(kim_gdata->core_data,
339 ((struct bts_action_send *)action_ptr)->data,
340 ((struct bts_action *)ptr)->size);
341 if (unlikely(err < 0)) {
342 release_firmware(kim_gdata->fw_entry);
Pavan Savoy320920c2010-07-14 08:21:12 -0500343 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500344 }
345 if (!wait_for_completion_timeout
346 (&kim_gdata->kim_rcvd,
347 msecs_to_jiffies(CMD_RESP_TIME))) {
348 pr_err
349 (" response timeout during fw download ");
350 /* timed out */
351 release_firmware(kim_gdata->fw_entry);
Pavan Savoy320920c2010-07-14 08:21:12 -0500352 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500353 }
354 break;
355 case ACTION_DELAY: /* sleep */
356 pr_info("sleep command in scr");
357 action_ptr = &(((struct bts_action *)ptr)->data[0]);
358 mdelay(((struct bts_action_delay *)action_ptr)->msec);
359 break;
360 }
361 len =
362 len - (sizeof(struct bts_action) +
363 ((struct bts_action *)ptr)->size);
364 ptr =
365 ptr + sizeof(struct bts_action) +
366 ((struct bts_action *)ptr)->size;
367 }
368 /* fw download complete */
369 release_firmware(kim_gdata->fw_entry);
Pavan Savoy320920c2010-07-14 08:21:12 -0500370 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500371}
372
373/**********************************************************************/
374/* functions called from ST core */
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500375/* function to toggle the GPIO
376 * needs to know whether the GPIO is active high or active low
377 */
378void st_kim_chip_toggle(enum proto_type type, enum kim_gpio_state state)
379{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500380 struct platform_device *kim_pdev;
381 struct kim_data_s *kim_gdata;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500382 pr_info(" %s ", __func__);
383
Pavan Savoy38d9df42010-07-08 08:55:17 -0500384 kim_pdev = st_get_plat_device();
385 kim_gdata = dev_get_drvdata(&kim_pdev->dev);
386
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500387 if (kim_gdata->gpios[type] == -1) {
388 pr_info(" gpio not requested for protocol %s",
389 protocol_names[type]);
390 return;
391 }
392 switch (type) {
393 case ST_BT:
394 /*Do Nothing */
395 break;
396
397 case ST_FM:
398 if (state == KIM_GPIO_ACTIVE)
399 gpio_set_value(kim_gdata->gpios[ST_FM], GPIO_LOW);
400 else
401 gpio_set_value(kim_gdata->gpios[ST_FM], GPIO_HIGH);
402 break;
403
404 case ST_GPS:
405 if (state == KIM_GPIO_ACTIVE)
406 gpio_set_value(kim_gdata->gpios[ST_GPS], GPIO_HIGH);
407 else
408 gpio_set_value(kim_gdata->gpios[ST_GPS], GPIO_LOW);
409 break;
410
411 case ST_MAX:
412 default:
413 break;
414 }
415
416 return;
417}
418
419/* called from ST Core, when REG_IN_PROGRESS (registration in progress)
420 * can be because of
421 * 1. response to read local version
422 * 2. during send/recv's of firmware download
423 */
424void st_kim_recv(void *disc_data, const unsigned char *data, long count)
425{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500426 struct st_data_s *st_gdata = (struct st_data_s *)disc_data;
427 struct kim_data_s *kim_gdata = st_gdata->kim_data;
428
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500429 pr_info(" %s ", __func__);
430 /* copy to local buffer */
431 if (unlikely(data[4] == 0x01 && data[5] == 0x10 && data[0] == 0x04)) {
432 /* must be the read_ver_cmd */
433 memcpy(kim_gdata->resp_buffer, data, count);
434 complete_all(&kim_gdata->kim_rcvd);
435 return;
436 } else {
Pavan Savoy38d9df42010-07-08 08:55:17 -0500437 kim_int_recv(kim_gdata, data, count);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500438 /* either completes or times out */
439 }
440 return;
441}
442
443/* to signal completion of line discipline installation
444 * called from ST Core, upon tty_open
445 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500446void st_kim_complete(void *kim_data)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500447{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500448 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500449 complete(&kim_gdata->ldisc_installed);
450}
451
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500452/**
453 * st_kim_start - called from ST Core upon 1st registration
454 * This involves toggling the chip enable gpio, reading
455 * the firmware version from chip, forming the fw file name
456 * based on the chip version, requesting the fw, parsing it
457 * and perform download(send/recv).
458 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500459long st_kim_start(void *kim_data)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500460{
Pavan Savoy320920c2010-07-14 08:21:12 -0500461 long err = 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500462 long retry = POR_RETRY_COUNT;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500463 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
464
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500465 pr_info(" %s", __func__);
466
467 do {
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500468 /* TODO: this is only because rfkill sub-system
469 * doesn't send events to user-space if the state
470 * isn't changed
471 */
472 rfkill_set_hw_state(kim_gdata->rfkill[ST_BT], 1);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500473 /* Configure BT nShutdown to HIGH state */
474 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
475 mdelay(5); /* FIXME: a proper toggle */
476 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_HIGH);
477 mdelay(100);
478 /* re-initialize the completion */
479 INIT_COMPLETION(kim_gdata->ldisc_installed);
Naveen Jainb38fc2d2010-06-09 03:45:32 -0500480#if 0 /* older way of signalling user-space UIM */
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500481 /* send signal to UIM */
482 err = kill_pid(find_get_pid(kim_gdata->uim_pid), SIGUSR2, 0);
483 if (err != 0) {
484 pr_info(" sending SIGUSR2 to uim failed %ld", err);
Pavan Savoy320920c2010-07-14 08:21:12 -0500485 err = -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500486 continue;
487 }
Naveen Jainb38fc2d2010-06-09 03:45:32 -0500488#endif
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500489 /* unblock and send event to UIM via /dev/rfkill */
490 rfkill_set_hw_state(kim_gdata->rfkill[ST_BT], 0);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500491 /* wait for ldisc to be installed */
492 err = wait_for_completion_timeout(&kim_gdata->ldisc_installed,
493 msecs_to_jiffies(LDISC_TIME));
494 if (!err) { /* timeout */
495 pr_err("line disc installation timed out ");
Pavan Savoy320920c2010-07-14 08:21:12 -0500496 err = -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500497 continue;
498 } else {
499 /* ldisc installed now */
500 pr_info(" line discipline installed ");
Pavan Savoy38d9df42010-07-08 08:55:17 -0500501 err = download_firmware(kim_gdata);
Pavan Savoy320920c2010-07-14 08:21:12 -0500502 if (err != 0) {
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500503 pr_err("download firmware failed");
504 continue;
505 } else { /* on success don't retry */
506 break;
507 }
508 }
509 } while (retry--);
510 return err;
511}
512
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500513/**
514 * st_kim_stop - called from ST Core, on the last un-registration
515 * toggle low the chip enable gpio
516 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500517long st_kim_stop(void *kim_data)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500518{
Pavan Savoy320920c2010-07-14 08:21:12 -0500519 long err = 0;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500520 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500521
522 INIT_COMPLETION(kim_gdata->ldisc_installed);
Naveen Jainb38fc2d2010-06-09 03:45:32 -0500523#if 0 /* older way of signalling user-space UIM */
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500524 /* send signal to UIM */
525 err = kill_pid(find_get_pid(kim_gdata->uim_pid), SIGUSR2, 1);
526 if (err != 0) {
527 pr_err("sending SIGUSR2 to uim failed %ld", err);
Pavan Savoy320920c2010-07-14 08:21:12 -0500528 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500529 }
Naveen Jainb38fc2d2010-06-09 03:45:32 -0500530#endif
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500531 /* set BT rfkill to be blocked */
532 err = rfkill_set_hw_state(kim_gdata->rfkill[ST_BT], 1);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500533
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");
Pavan Savoy320920c2010-07-14 08:21:12 -0500539 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500540 }
541
542 /* By default configure BT nShutdown to LOW state */
543 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
544 mdelay(1);
545 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_HIGH);
546 mdelay(1);
547 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
548 return err;
549}
550
551/**********************************************************************/
552/* functions called from subsystems */
Naveen Jaine2a53282010-06-09 03:45:33 -0500553/* called when sysfs entry is read from */
554
555static ssize_t show_version(struct device *dev, struct device_attribute
556 *attr, char *buf)
557{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500558 struct kim_data_s *kim_gdata = dev_get_drvdata(dev);
Naveen Jaine2a53282010-06-09 03:45:33 -0500559 sprintf(buf, "%04X %d.%d.%d", kim_gdata->version.full,
560 kim_gdata->version.chip, kim_gdata->version.maj_ver,
561 kim_gdata->version.min_ver);
562 return strlen(buf);
563}
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500564
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500565/* called when sysfs entry is written to */
566static ssize_t store_pid(struct device *dev, struct device_attribute
567 *devattr, char *buf, size_t count)
568{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500569 struct kim_data_s *kim_gdata = dev_get_drvdata(dev);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500570 sscanf(buf, "%ld", &kim_gdata->uim_pid);
571 /* to be made use by kim_start to signal SIGUSR2
572 */
573 return strlen(buf);
574}
575
576/* called when sysfs entry is read from */
577static ssize_t show_pid(struct device *dev, struct device_attribute
578 *attr, char *buf)
579{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500580 struct kim_data_s *kim_gdata = dev_get_drvdata(dev);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500581 sprintf(buf, "%ld", kim_gdata->uim_pid);
582 return strlen(buf);
583}
584
585/* called when sysfs entry is read from */
586static ssize_t show_list(struct device *dev, struct device_attribute
587 *attr, char *buf)
588{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500589 struct kim_data_s *kim_gdata = dev_get_drvdata(dev);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500590 kim_st_list_protocols(kim_gdata->core_data, buf);
591 return strlen(buf);
592}
593
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500594/* function called from rfkill subsystem, when someone from
595 * user space would write 0/1 on the sysfs entry
596 * /sys/class/rfkill/rfkill0,1,3/state
597 */
598static int kim_toggle_radio(void *data, bool blocked)
599{
600 enum proto_type type = *((enum proto_type *)data);
Pavan Savoye6d9e642010-07-22 05:32:05 -0500601 pr_debug(" %s: %d ", __func__, type);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500602
603 switch (type) {
604 case ST_BT:
605 /* do nothing */
606 break;
607 case ST_FM:
608 case ST_GPS:
609 if (blocked)
610 st_kim_chip_toggle(type, KIM_GPIO_INACTIVE);
611 else
612 st_kim_chip_toggle(type, KIM_GPIO_ACTIVE);
613 break;
614 case ST_MAX:
615 pr_err(" wrong proto type ");
616 break;
617 }
Pavan Savoy320920c2010-07-14 08:21:12 -0500618 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500619}
620
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500621/**
622 * st_kim_ref - reference the core's data
623 * This references the per-ST platform device in the arch/xx/
624 * board-xx.c file.
625 * This would enable multiple such platform devices to exist
626 * on a given platform
627 */
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500628void st_kim_ref(struct st_data_s **core_data)
629{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500630 struct platform_device *pdev;
631 struct kim_data_s *kim_gdata;
632 /* get kim_gdata reference from platform device */
633 pdev = st_get_plat_device();
634 kim_gdata = dev_get_drvdata(&pdev->dev);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500635 *core_data = kim_gdata->core_data;
636}
637
638/**********************************************************************/
639/* functions called from platform device driver subsystem
640 * need to have a relevant platform device entry in the platform's
641 * board-*.c file
642 */
643
644static int kim_probe(struct platform_device *pdev)
645{
646 long status;
647 long proto;
648 long *gpios = pdev->dev.platform_data;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500649 struct kim_data_s *kim_gdata;
650
651 kim_gdata = kzalloc(sizeof(struct kim_data_s), GFP_ATOMIC);
652 if (!kim_gdata) {
653 pr_err("no mem to allocate");
654 return -ENOMEM;
655 }
656 dev_set_drvdata(&pdev->dev, kim_gdata);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500657
658 status = st_core_init(&kim_gdata->core_data);
659 if (status != 0) {
660 pr_err(" ST core init failed");
Pavan Savoy320920c2010-07-14 08:21:12 -0500661 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500662 }
Pavan Savoy38d9df42010-07-08 08:55:17 -0500663 /* refer to itself */
664 kim_gdata->core_data->kim_data = kim_gdata;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500665
666 for (proto = 0; proto < ST_MAX; proto++) {
667 kim_gdata->gpios[proto] = gpios[proto];
668 pr_info(" %ld gpio to be requested", gpios[proto]);
669 }
670
671 for (proto = 0; (proto < ST_MAX) && (gpios[proto] != -1); proto++) {
672 /* Claim the Bluetooth/FM/GPIO
673 * nShutdown gpio from the system
674 */
675 status = gpio_request(gpios[proto], "kim");
676 if (unlikely(status)) {
677 pr_err(" gpio %ld request failed ", gpios[proto]);
678 proto -= 1;
679 while (proto >= 0) {
680 if (gpios[proto] != -1)
681 gpio_free(gpios[proto]);
682 }
683 return status;
684 }
685
686 /* Configure nShutdown GPIO as output=0 */
687 status =
688 gpio_direction_output(gpios[proto], 0);
689 if (unlikely(status)) {
690 pr_err(" unable to configure gpio %ld",
691 gpios[proto]);
692 proto -= 1;
693 while (proto >= 0) {
694 if (gpios[proto] != -1)
695 gpio_free(gpios[proto]);
696 }
697 return status;
698 }
699 }
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500700 /* get reference of pdev for request_firmware
701 */
702 kim_gdata->kim_pdev = pdev;
703 init_completion(&kim_gdata->kim_rcvd);
704 init_completion(&kim_gdata->ldisc_installed);
Naveen Jainb38fc2d2010-06-09 03:45:32 -0500705
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500706 for (proto = 0; (proto < ST_MAX) && (gpios[proto] != -1); proto++) {
707 /* TODO: should all types be rfkill_type_bt ? */
708 kim_gdata->rf_protos[proto] = proto;
709 kim_gdata->rfkill[proto] = rfkill_alloc(protocol_names[proto],
710 &pdev->dev, RFKILL_TYPE_BLUETOOTH,
711 &kim_rfkill_ops, &kim_gdata->rf_protos[proto]);
712 if (kim_gdata->rfkill[proto] == NULL) {
713 pr_err("cannot create rfkill entry for gpio %ld",
714 gpios[proto]);
715 continue;
716 }
717 /* block upon creation */
718 rfkill_init_sw_state(kim_gdata->rfkill[proto], 1);
719 status = rfkill_register(kim_gdata->rfkill[proto]);
720 if (unlikely(status)) {
721 pr_err("rfkill registration failed for gpio %ld",
722 gpios[proto]);
723 rfkill_unregister(kim_gdata->rfkill[proto]);
724 continue;
725 }
726 pr_info("rfkill entry created for %ld", gpios[proto]);
727 }
Naveen Jaine2a53282010-06-09 03:45:33 -0500728
729 if (sysfs_create_group(&pdev->dev.kobj, &uim_attr_grp)) {
730 pr_err(" sysfs entry creation failed");
731 return -1;
732 }
733 pr_info(" sysfs entries created ");
Pavan Savoy320920c2010-07-14 08:21:12 -0500734 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500735}
736
737static int kim_remove(struct platform_device *pdev)
738{
739 /* free the GPIOs requested
740 */
741 long *gpios = pdev->dev.platform_data;
742 long proto;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500743 struct kim_data_s *kim_gdata;
744
745 kim_gdata = dev_get_drvdata(&pdev->dev);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500746
747 for (proto = 0; (proto < ST_MAX) && (gpios[proto] != -1); proto++) {
748 /* Claim the Bluetooth/FM/GPIO
749 * nShutdown gpio from the system
750 */
751 gpio_free(gpios[proto]);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500752 rfkill_unregister(kim_gdata->rfkill[proto]);
753 rfkill_destroy(kim_gdata->rfkill[proto]);
754 kim_gdata->rfkill[proto] = NULL;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500755 }
756 pr_info("kim: GPIO Freed");
Naveen Jaine2a53282010-06-09 03:45:33 -0500757 sysfs_remove_group(&pdev->dev.kobj, &uim_attr_grp);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500758 kim_gdata->kim_pdev = NULL;
759 st_core_exit(kim_gdata->core_data);
Pavan Savoy38d9df42010-07-08 08:55:17 -0500760
761 kfree(kim_gdata);
762 kim_gdata = NULL;
Pavan Savoy320920c2010-07-14 08:21:12 -0500763 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500764}
765
766/**********************************************************************/
767/* entry point for ST KIM module, called in from ST Core */
768
769static int __init st_kim_init(void)
770{
Pavan Savoy320920c2010-07-14 08:21:12 -0500771 long ret = 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500772 ret = platform_driver_register(&kim_platform_driver);
773 if (ret != 0) {
774 pr_err("platform drv registration failed");
Pavan Savoy320920c2010-07-14 08:21:12 -0500775 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500776 }
Pavan Savoy320920c2010-07-14 08:21:12 -0500777 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500778}
779
780static void __exit st_kim_deinit(void)
781{
782 /* the following returns void */
783 platform_driver_unregister(&kim_platform_driver);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500784}
785
786
787module_init(st_kim_init);
788module_exit(st_kim_deinit);
789MODULE_AUTHOR("Pavan Savoy <pavan_savoy@ti.com>");
790MODULE_DESCRIPTION("Shared Transport Driver for TI BT/FM/GPS combo chips ");
791MODULE_LICENSE("GPL");