blob: 5ab69d12be0633feafeae8e88ab0882996ce66e7 [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
107/*
108 * function to return whether the firmware response was proper
109 * in case of error don't complete so that waiting for proper
110 * response times out
111 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500112void validate_firmware_response(struct kim_data_s *kim_gdata)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500113{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500114 struct sk_buff *skb = kim_gdata->rx_skb;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500115 if (unlikely(skb->data[5] != 0)) {
116 pr_err("no proper response during fw download");
117 pr_err("data6 %x", skb->data[5]);
118 return; /* keep waiting for the proper response */
119 }
120 /* becos of all the script being downloaded */
121 complete_all(&kim_gdata->kim_rcvd);
122 kfree_skb(skb);
123}
124
125/* check for data len received inside kim_int_recv
126 * most often hit the last case to update state to waiting for data
127 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500128static inline int kim_check_data_len(struct kim_data_s *kim_gdata, int len)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500129{
130 register int room = skb_tailroom(kim_gdata->rx_skb);
131
Pavan Savoye6d9e642010-07-22 05:32:05 -0500132 pr_debug("len %d room %d", len, room);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500133
134 if (!len) {
Pavan Savoy38d9df42010-07-08 08:55:17 -0500135 validate_firmware_response(kim_gdata);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500136 } else if (len > room) {
137 /* Received packet's payload length is larger.
138 * We can't accommodate it in created skb.
139 */
140 pr_err("Data length is too large len %d room %d", len,
141 room);
142 kfree_skb(kim_gdata->rx_skb);
143 } else {
144 /* Packet header has non-zero payload length and
145 * we have enough space in created skb. Lets read
146 * payload data */
147 kim_gdata->rx_state = ST_BT_W4_DATA;
148 kim_gdata->rx_count = len;
149 return len;
150 }
151
152 /* Change ST LL state to continue to process next
153 * packet */
154 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
155 kim_gdata->rx_skb = NULL;
156 kim_gdata->rx_count = 0;
157
158 return 0;
159}
160
161/* receive function called during firmware download
162 * - firmware download responses on different UART drivers
163 * have been observed to come in bursts of different
164 * tty_receive and hence the logic
165 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500166void kim_int_recv(struct kim_data_s *kim_gdata,
167 const unsigned char *data, long count)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500168{
169 register char *ptr;
170 struct hci_event_hdr *eh;
171 register int len = 0, type = 0;
172
Pavan Savoye6d9e642010-07-22 05:32:05 -0500173 pr_debug("%s", __func__);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500174 /* Decode received bytes here */
175 ptr = (char *)data;
176 if (unlikely(ptr == NULL)) {
177 pr_err(" received null from TTY ");
178 return;
179 }
180 while (count) {
181 if (kim_gdata->rx_count) {
182 len = min_t(unsigned int, kim_gdata->rx_count, count);
183 memcpy(skb_put(kim_gdata->rx_skb, len), ptr, len);
184 kim_gdata->rx_count -= len;
185 count -= len;
186 ptr += len;
187
188 if (kim_gdata->rx_count)
189 continue;
190
191 /* Check ST RX state machine , where are we? */
192 switch (kim_gdata->rx_state) {
193 /* Waiting for complete packet ? */
194 case ST_BT_W4_DATA:
Pavan Savoye6d9e642010-07-22 05:32:05 -0500195 pr_debug("Complete pkt received");
Pavan Savoy38d9df42010-07-08 08:55:17 -0500196 validate_firmware_response(kim_gdata);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500197 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
198 kim_gdata->rx_skb = NULL;
199 continue;
200 /* Waiting for Bluetooth event header ? */
201 case ST_BT_W4_EVENT_HDR:
202 eh = (struct hci_event_hdr *)kim_gdata->
203 rx_skb->data;
Pavan Savoye6d9e642010-07-22 05:32:05 -0500204 pr_debug("Event header: evt 0x%2.2x"
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500205 "plen %d", eh->evt, eh->plen);
Pavan Savoy38d9df42010-07-08 08:55:17 -0500206 kim_check_data_len(kim_gdata, eh->plen);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500207 continue;
208 } /* end of switch */
209 } /* end of if rx_state */
210 switch (*ptr) {
211 /* Bluetooth event packet? */
212 case HCI_EVENT_PKT:
213 pr_info("Event packet");
214 kim_gdata->rx_state = ST_BT_W4_EVENT_HDR;
215 kim_gdata->rx_count = HCI_EVENT_HDR_SIZE;
216 type = HCI_EVENT_PKT;
217 break;
218 default:
219 pr_info("unknown packet");
220 ptr++;
221 count--;
222 continue;
223 } /* end of switch *ptr */
224 ptr++;
225 count--;
226 kim_gdata->rx_skb =
227 bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
228 if (!kim_gdata->rx_skb) {
229 pr_err("can't allocate mem for new packet");
230 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
231 kim_gdata->rx_count = 0;
232 return;
233 } /* not necessary in this case */
234 bt_cb(kim_gdata->rx_skb)->pkt_type = type;
235 } /* end of while count */
236 pr_info("done %s", __func__);
237 return;
238}
239
Pavan Savoy38d9df42010-07-08 08:55:17 -0500240static long read_local_version(struct kim_data_s *kim_gdata, char *bts_scr_name)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500241{
242 unsigned short version = 0, chip = 0, min_ver = 0, maj_ver = 0;
243 char read_ver_cmd[] = { 0x01, 0x01, 0x10, 0x00 };
244
Pavan Savoye6d9e642010-07-22 05:32:05 -0500245 pr_debug("%s", __func__);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500246
247 INIT_COMPLETION(kim_gdata->kim_rcvd);
248 if (4 != st_int_write(kim_gdata->core_data, read_ver_cmd, 4)) {
249 pr_err("kim: couldn't write 4 bytes");
Pavan Savoy320920c2010-07-14 08:21:12 -0500250 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500251 }
252
253 if (!wait_for_completion_timeout
254 (&kim_gdata->kim_rcvd, msecs_to_jiffies(CMD_RESP_TIME))) {
255 pr_err(" waiting for ver info- timed out ");
Pavan Savoy320920c2010-07-14 08:21:12 -0500256 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500257 }
258
259 version =
Naveen Jaine2a53282010-06-09 03:45:33 -0500260 MAKEWORD(kim_gdata->resp_buffer[13],
261 kim_gdata->resp_buffer[14]);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500262 chip = (version & 0x7C00) >> 10;
263 min_ver = (version & 0x007F);
264 maj_ver = (version & 0x0380) >> 7;
265
266 if (version & 0x8000)
267 maj_ver |= 0x0008;
268
269 sprintf(bts_scr_name, "TIInit_%d.%d.%d.bts", chip, maj_ver, min_ver);
Naveen Jaine2a53282010-06-09 03:45:33 -0500270
271 /* to be accessed later via sysfs entry */
272 kim_gdata->version.full = version;
273 kim_gdata->version.chip = chip;
274 kim_gdata->version.maj_ver = maj_ver;
275 kim_gdata->version.min_ver = min_ver;
276
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500277 pr_info("%s", bts_scr_name);
Pavan Savoy320920c2010-07-14 08:21:12 -0500278 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500279}
280
281/* internal function which parses through the .bts firmware script file
282 * intreprets SEND, DELAY actions only as of now
283 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500284static long download_firmware(struct kim_data_s *kim_gdata)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500285{
Pavan Savoy320920c2010-07-14 08:21:12 -0500286 long err = 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500287 long len = 0;
288 register unsigned char *ptr = NULL;
289 register unsigned char *action_ptr = NULL;
290 unsigned char bts_scr_name[30] = { 0 }; /* 30 char long bts scr name? */
291
Pavan Savoy38d9df42010-07-08 08:55:17 -0500292 err = read_local_version(kim_gdata, bts_scr_name);
Pavan Savoy320920c2010-07-14 08:21:12 -0500293 if (err != 0) {
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500294 pr_err("kim: failed to read local ver");
295 return err;
296 }
297 err =
298 request_firmware(&kim_gdata->fw_entry, bts_scr_name,
299 &kim_gdata->kim_pdev->dev);
300 if (unlikely((err != 0) || (kim_gdata->fw_entry->data == NULL) ||
301 (kim_gdata->fw_entry->size == 0))) {
302 pr_err(" request_firmware failed(errno %ld) for %s", err,
303 bts_scr_name);
Pavan Savoy320920c2010-07-14 08:21:12 -0500304 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500305 }
306 ptr = (void *)kim_gdata->fw_entry->data;
307 len = kim_gdata->fw_entry->size;
308 /* bts_header to remove out magic number and
309 * version
310 */
311 ptr += sizeof(struct bts_header);
312 len -= sizeof(struct bts_header);
313
314 while (len > 0 && ptr) {
Pavan Savoye6d9e642010-07-22 05:32:05 -0500315 pr_debug(" action size %d, type %d ",
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500316 ((struct bts_action *)ptr)->size,
317 ((struct bts_action *)ptr)->type);
318
319 switch (((struct bts_action *)ptr)->type) {
320 case ACTION_SEND_COMMAND: /* action send */
321 action_ptr = &(((struct bts_action *)ptr)->data[0]);
322 if (unlikely
323 (((struct hci_command *)action_ptr)->opcode ==
324 0xFF36)) {
325 /* ignore remote change
326 * baud rate HCI VS command */
327 pr_err
Pavan Savoye6d9e642010-07-22 05:32:05 -0500328 (" change remote baud"
329 " rate command in firmware");
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500330 break;
331 }
332
333 INIT_COMPLETION(kim_gdata->kim_rcvd);
334 err = st_int_write(kim_gdata->core_data,
335 ((struct bts_action_send *)action_ptr)->data,
336 ((struct bts_action *)ptr)->size);
337 if (unlikely(err < 0)) {
338 release_firmware(kim_gdata->fw_entry);
Pavan Savoy320920c2010-07-14 08:21:12 -0500339 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500340 }
341 if (!wait_for_completion_timeout
342 (&kim_gdata->kim_rcvd,
343 msecs_to_jiffies(CMD_RESP_TIME))) {
344 pr_err
345 (" response timeout during fw download ");
346 /* timed out */
347 release_firmware(kim_gdata->fw_entry);
Pavan Savoy320920c2010-07-14 08:21:12 -0500348 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500349 }
350 break;
351 case ACTION_DELAY: /* sleep */
352 pr_info("sleep command in scr");
353 action_ptr = &(((struct bts_action *)ptr)->data[0]);
354 mdelay(((struct bts_action_delay *)action_ptr)->msec);
355 break;
356 }
357 len =
358 len - (sizeof(struct bts_action) +
359 ((struct bts_action *)ptr)->size);
360 ptr =
361 ptr + sizeof(struct bts_action) +
362 ((struct bts_action *)ptr)->size;
363 }
364 /* fw download complete */
365 release_firmware(kim_gdata->fw_entry);
Pavan Savoy320920c2010-07-14 08:21:12 -0500366 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500367}
368
369/**********************************************************************/
370/* functions called from ST core */
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500371/* function to toggle the GPIO
372 * needs to know whether the GPIO is active high or active low
373 */
374void st_kim_chip_toggle(enum proto_type type, enum kim_gpio_state state)
375{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500376 struct platform_device *kim_pdev;
377 struct kim_data_s *kim_gdata;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500378 pr_info(" %s ", __func__);
379
Pavan Savoy38d9df42010-07-08 08:55:17 -0500380 kim_pdev = st_get_plat_device();
381 kim_gdata = dev_get_drvdata(&kim_pdev->dev);
382
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500383 if (kim_gdata->gpios[type] == -1) {
384 pr_info(" gpio not requested for protocol %s",
385 protocol_names[type]);
386 return;
387 }
388 switch (type) {
389 case ST_BT:
390 /*Do Nothing */
391 break;
392
393 case ST_FM:
394 if (state == KIM_GPIO_ACTIVE)
395 gpio_set_value(kim_gdata->gpios[ST_FM], GPIO_LOW);
396 else
397 gpio_set_value(kim_gdata->gpios[ST_FM], GPIO_HIGH);
398 break;
399
400 case ST_GPS:
401 if (state == KIM_GPIO_ACTIVE)
402 gpio_set_value(kim_gdata->gpios[ST_GPS], GPIO_HIGH);
403 else
404 gpio_set_value(kim_gdata->gpios[ST_GPS], GPIO_LOW);
405 break;
406
407 case ST_MAX:
408 default:
409 break;
410 }
411
412 return;
413}
414
415/* called from ST Core, when REG_IN_PROGRESS (registration in progress)
416 * can be because of
417 * 1. response to read local version
418 * 2. during send/recv's of firmware download
419 */
420void st_kim_recv(void *disc_data, const unsigned char *data, long count)
421{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500422 struct st_data_s *st_gdata = (struct st_data_s *)disc_data;
423 struct kim_data_s *kim_gdata = st_gdata->kim_data;
424
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500425 pr_info(" %s ", __func__);
426 /* copy to local buffer */
427 if (unlikely(data[4] == 0x01 && data[5] == 0x10 && data[0] == 0x04)) {
428 /* must be the read_ver_cmd */
429 memcpy(kim_gdata->resp_buffer, data, count);
430 complete_all(&kim_gdata->kim_rcvd);
431 return;
432 } else {
Pavan Savoy38d9df42010-07-08 08:55:17 -0500433 kim_int_recv(kim_gdata, data, count);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500434 /* either completes or times out */
435 }
436 return;
437}
438
439/* to signal completion of line discipline installation
440 * called from ST Core, upon tty_open
441 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500442void st_kim_complete(void *kim_data)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500443{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500444 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500445 complete(&kim_gdata->ldisc_installed);
446}
447
448/* called from ST Core upon 1st registration
449*/
Pavan Savoy38d9df42010-07-08 08:55:17 -0500450long st_kim_start(void *kim_data)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500451{
Pavan Savoy320920c2010-07-14 08:21:12 -0500452 long err = 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500453 long retry = POR_RETRY_COUNT;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500454 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
455
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500456 pr_info(" %s", __func__);
457
458 do {
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500459 /* TODO: this is only because rfkill sub-system
460 * doesn't send events to user-space if the state
461 * isn't changed
462 */
463 rfkill_set_hw_state(kim_gdata->rfkill[ST_BT], 1);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500464 /* Configure BT nShutdown to HIGH state */
465 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
466 mdelay(5); /* FIXME: a proper toggle */
467 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_HIGH);
468 mdelay(100);
469 /* re-initialize the completion */
470 INIT_COMPLETION(kim_gdata->ldisc_installed);
Naveen Jainb38fc2d2010-06-09 03:45:32 -0500471#if 0 /* older way of signalling user-space UIM */
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500472 /* send signal to UIM */
473 err = kill_pid(find_get_pid(kim_gdata->uim_pid), SIGUSR2, 0);
474 if (err != 0) {
475 pr_info(" sending SIGUSR2 to uim failed %ld", err);
Pavan Savoy320920c2010-07-14 08:21:12 -0500476 err = -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500477 continue;
478 }
Naveen Jainb38fc2d2010-06-09 03:45:32 -0500479#endif
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500480 /* unblock and send event to UIM via /dev/rfkill */
481 rfkill_set_hw_state(kim_gdata->rfkill[ST_BT], 0);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500482 /* wait for ldisc to be installed */
483 err = wait_for_completion_timeout(&kim_gdata->ldisc_installed,
484 msecs_to_jiffies(LDISC_TIME));
485 if (!err) { /* timeout */
486 pr_err("line disc installation timed out ");
Pavan Savoy320920c2010-07-14 08:21:12 -0500487 err = -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500488 continue;
489 } else {
490 /* ldisc installed now */
491 pr_info(" line discipline installed ");
Pavan Savoy38d9df42010-07-08 08:55:17 -0500492 err = download_firmware(kim_gdata);
Pavan Savoy320920c2010-07-14 08:21:12 -0500493 if (err != 0) {
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500494 pr_err("download firmware failed");
495 continue;
496 } else { /* on success don't retry */
497 break;
498 }
499 }
500 } while (retry--);
501 return err;
502}
503
504/* called from ST Core, on the last un-registration
505*/
Pavan Savoy38d9df42010-07-08 08:55:17 -0500506long st_kim_stop(void *kim_data)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500507{
Pavan Savoy320920c2010-07-14 08:21:12 -0500508 long err = 0;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500509 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500510
511 INIT_COMPLETION(kim_gdata->ldisc_installed);
Naveen Jainb38fc2d2010-06-09 03:45:32 -0500512#if 0 /* older way of signalling user-space UIM */
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500513 /* send signal to UIM */
514 err = kill_pid(find_get_pid(kim_gdata->uim_pid), SIGUSR2, 1);
515 if (err != 0) {
516 pr_err("sending SIGUSR2 to uim failed %ld", err);
Pavan Savoy320920c2010-07-14 08:21:12 -0500517 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500518 }
Naveen Jainb38fc2d2010-06-09 03:45:32 -0500519#endif
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500520 /* set BT rfkill to be blocked */
521 err = rfkill_set_hw_state(kim_gdata->rfkill[ST_BT], 1);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500522
523 /* wait for ldisc to be un-installed */
524 err = wait_for_completion_timeout(&kim_gdata->ldisc_installed,
525 msecs_to_jiffies(LDISC_TIME));
526 if (!err) { /* timeout */
527 pr_err(" timed out waiting for ldisc to be un-installed");
Pavan Savoy320920c2010-07-14 08:21:12 -0500528 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500529 }
530
531 /* By default configure BT nShutdown to LOW state */
532 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
533 mdelay(1);
534 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_HIGH);
535 mdelay(1);
536 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
537 return err;
538}
539
540/**********************************************************************/
541/* functions called from subsystems */
Naveen Jaine2a53282010-06-09 03:45:33 -0500542/* called when sysfs entry is read from */
543
544static ssize_t show_version(struct device *dev, struct device_attribute
545 *attr, char *buf)
546{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500547 struct kim_data_s *kim_gdata = dev_get_drvdata(dev);
Naveen Jaine2a53282010-06-09 03:45:33 -0500548 sprintf(buf, "%04X %d.%d.%d", kim_gdata->version.full,
549 kim_gdata->version.chip, kim_gdata->version.maj_ver,
550 kim_gdata->version.min_ver);
551 return strlen(buf);
552}
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500553
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500554/* called when sysfs entry is written to */
555static ssize_t store_pid(struct device *dev, struct device_attribute
556 *devattr, char *buf, size_t count)
557{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500558 struct kim_data_s *kim_gdata = dev_get_drvdata(dev);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500559 sscanf(buf, "%ld", &kim_gdata->uim_pid);
560 /* to be made use by kim_start to signal SIGUSR2
561 */
562 return strlen(buf);
563}
564
565/* called when sysfs entry is read from */
566static ssize_t show_pid(struct device *dev, struct device_attribute
567 *attr, char *buf)
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 sprintf(buf, "%ld", kim_gdata->uim_pid);
571 return strlen(buf);
572}
573
574/* called when sysfs entry is read from */
575static ssize_t show_list(struct device *dev, struct device_attribute
576 *attr, char *buf)
577{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500578 struct kim_data_s *kim_gdata = dev_get_drvdata(dev);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500579 kim_st_list_protocols(kim_gdata->core_data, buf);
580 return strlen(buf);
581}
582
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500583/* function called from rfkill subsystem, when someone from
584 * user space would write 0/1 on the sysfs entry
585 * /sys/class/rfkill/rfkill0,1,3/state
586 */
587static int kim_toggle_radio(void *data, bool blocked)
588{
589 enum proto_type type = *((enum proto_type *)data);
Pavan Savoye6d9e642010-07-22 05:32:05 -0500590 pr_debug(" %s: %d ", __func__, type);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500591
592 switch (type) {
593 case ST_BT:
594 /* do nothing */
595 break;
596 case ST_FM:
597 case ST_GPS:
598 if (blocked)
599 st_kim_chip_toggle(type, KIM_GPIO_INACTIVE);
600 else
601 st_kim_chip_toggle(type, KIM_GPIO_ACTIVE);
602 break;
603 case ST_MAX:
604 pr_err(" wrong proto type ");
605 break;
606 }
Pavan Savoy320920c2010-07-14 08:21:12 -0500607 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500608}
609
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500610void st_kim_ref(struct st_data_s **core_data)
611{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500612 struct platform_device *pdev;
613 struct kim_data_s *kim_gdata;
614 /* get kim_gdata reference from platform device */
615 pdev = st_get_plat_device();
616 kim_gdata = dev_get_drvdata(&pdev->dev);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500617 *core_data = kim_gdata->core_data;
618}
619
620/**********************************************************************/
621/* functions called from platform device driver subsystem
622 * need to have a relevant platform device entry in the platform's
623 * board-*.c file
624 */
625
626static int kim_probe(struct platform_device *pdev)
627{
628 long status;
629 long proto;
630 long *gpios = pdev->dev.platform_data;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500631 struct kim_data_s *kim_gdata;
632
633 kim_gdata = kzalloc(sizeof(struct kim_data_s), GFP_ATOMIC);
634 if (!kim_gdata) {
635 pr_err("no mem to allocate");
636 return -ENOMEM;
637 }
638 dev_set_drvdata(&pdev->dev, kim_gdata);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500639
640 status = st_core_init(&kim_gdata->core_data);
641 if (status != 0) {
642 pr_err(" ST core init failed");
Pavan Savoy320920c2010-07-14 08:21:12 -0500643 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500644 }
Pavan Savoy38d9df42010-07-08 08:55:17 -0500645 /* refer to itself */
646 kim_gdata->core_data->kim_data = kim_gdata;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500647
648 for (proto = 0; proto < ST_MAX; proto++) {
649 kim_gdata->gpios[proto] = gpios[proto];
650 pr_info(" %ld gpio to be requested", gpios[proto]);
651 }
652
653 for (proto = 0; (proto < ST_MAX) && (gpios[proto] != -1); proto++) {
654 /* Claim the Bluetooth/FM/GPIO
655 * nShutdown gpio from the system
656 */
657 status = gpio_request(gpios[proto], "kim");
658 if (unlikely(status)) {
659 pr_err(" gpio %ld request failed ", gpios[proto]);
660 proto -= 1;
661 while (proto >= 0) {
662 if (gpios[proto] != -1)
663 gpio_free(gpios[proto]);
664 }
665 return status;
666 }
667
668 /* Configure nShutdown GPIO as output=0 */
669 status =
670 gpio_direction_output(gpios[proto], 0);
671 if (unlikely(status)) {
672 pr_err(" unable to configure gpio %ld",
673 gpios[proto]);
674 proto -= 1;
675 while (proto >= 0) {
676 if (gpios[proto] != -1)
677 gpio_free(gpios[proto]);
678 }
679 return status;
680 }
681 }
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500682 /* get reference of pdev for request_firmware
683 */
684 kim_gdata->kim_pdev = pdev;
685 init_completion(&kim_gdata->kim_rcvd);
686 init_completion(&kim_gdata->ldisc_installed);
Naveen Jainb38fc2d2010-06-09 03:45:32 -0500687
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500688 for (proto = 0; (proto < ST_MAX) && (gpios[proto] != -1); proto++) {
689 /* TODO: should all types be rfkill_type_bt ? */
690 kim_gdata->rf_protos[proto] = proto;
691 kim_gdata->rfkill[proto] = rfkill_alloc(protocol_names[proto],
692 &pdev->dev, RFKILL_TYPE_BLUETOOTH,
693 &kim_rfkill_ops, &kim_gdata->rf_protos[proto]);
694 if (kim_gdata->rfkill[proto] == NULL) {
695 pr_err("cannot create rfkill entry for gpio %ld",
696 gpios[proto]);
697 continue;
698 }
699 /* block upon creation */
700 rfkill_init_sw_state(kim_gdata->rfkill[proto], 1);
701 status = rfkill_register(kim_gdata->rfkill[proto]);
702 if (unlikely(status)) {
703 pr_err("rfkill registration failed for gpio %ld",
704 gpios[proto]);
705 rfkill_unregister(kim_gdata->rfkill[proto]);
706 continue;
707 }
708 pr_info("rfkill entry created for %ld", gpios[proto]);
709 }
Naveen Jaine2a53282010-06-09 03:45:33 -0500710
711 if (sysfs_create_group(&pdev->dev.kobj, &uim_attr_grp)) {
712 pr_err(" sysfs entry creation failed");
713 return -1;
714 }
715 pr_info(" sysfs entries created ");
Pavan Savoy320920c2010-07-14 08:21:12 -0500716 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500717}
718
719static int kim_remove(struct platform_device *pdev)
720{
721 /* free the GPIOs requested
722 */
723 long *gpios = pdev->dev.platform_data;
724 long proto;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500725 struct kim_data_s *kim_gdata;
726
727 kim_gdata = dev_get_drvdata(&pdev->dev);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500728
729 for (proto = 0; (proto < ST_MAX) && (gpios[proto] != -1); proto++) {
730 /* Claim the Bluetooth/FM/GPIO
731 * nShutdown gpio from the system
732 */
733 gpio_free(gpios[proto]);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500734 rfkill_unregister(kim_gdata->rfkill[proto]);
735 rfkill_destroy(kim_gdata->rfkill[proto]);
736 kim_gdata->rfkill[proto] = NULL;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500737 }
738 pr_info("kim: GPIO Freed");
Naveen Jaine2a53282010-06-09 03:45:33 -0500739 sysfs_remove_group(&pdev->dev.kobj, &uim_attr_grp);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500740 kim_gdata->kim_pdev = NULL;
741 st_core_exit(kim_gdata->core_data);
Pavan Savoy38d9df42010-07-08 08:55:17 -0500742
743 kfree(kim_gdata);
744 kim_gdata = NULL;
Pavan Savoy320920c2010-07-14 08:21:12 -0500745 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500746}
747
748/**********************************************************************/
749/* entry point for ST KIM module, called in from ST Core */
750
751static int __init st_kim_init(void)
752{
Pavan Savoy320920c2010-07-14 08:21:12 -0500753 long ret = 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500754 ret = platform_driver_register(&kim_platform_driver);
755 if (ret != 0) {
756 pr_err("platform drv registration failed");
Pavan Savoy320920c2010-07-14 08:21:12 -0500757 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500758 }
Pavan Savoy320920c2010-07-14 08:21:12 -0500759 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500760}
761
762static void __exit st_kim_deinit(void)
763{
764 /* the following returns void */
765 platform_driver_unregister(&kim_platform_driver);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500766}
767
768
769module_init(st_kim_init);
770module_exit(st_kim_deinit);
771MODULE_AUTHOR("Pavan Savoy <pavan_savoy@ti.com>");
772MODULE_DESCRIPTION("Shared Transport Driver for TI BT/FM/GPS combo chips ");
773MODULE_LICENSE("GPL");