blob: d4fd2c28b8218cf8c1a9f8a83c6e1b692b15613a [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
132 pr_info("len %d room %d", len, room);
133
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
173 pr_info("%s", __func__);
174 /* 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:
195 pr_info("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;
204 pr_info("Event header: evt 0x%2.2x"
205 "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
245 pr_info("%s", __func__);
246
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");
250 return ST_ERR_FAILURE;
251 }
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 ");
256 return ST_ERR_FAILURE;
257 }
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);
278 return ST_SUCCESS;
279}
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{
286 long err = ST_SUCCESS;
287 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
292 pr_info("%s", __func__);
293
Pavan Savoy38d9df42010-07-08 08:55:17 -0500294 err = read_local_version(kim_gdata, bts_scr_name);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500295 if (err != ST_SUCCESS) {
296 pr_err("kim: failed to read local ver");
297 return err;
298 }
299 err =
300 request_firmware(&kim_gdata->fw_entry, bts_scr_name,
301 &kim_gdata->kim_pdev->dev);
302 if (unlikely((err != 0) || (kim_gdata->fw_entry->data == NULL) ||
303 (kim_gdata->fw_entry->size == 0))) {
304 pr_err(" request_firmware failed(errno %ld) for %s", err,
305 bts_scr_name);
306 return ST_ERR_FAILURE;
307 }
308 ptr = (void *)kim_gdata->fw_entry->data;
309 len = kim_gdata->fw_entry->size;
310 /* bts_header to remove out magic number and
311 * version
312 */
313 ptr += sizeof(struct bts_header);
314 len -= sizeof(struct bts_header);
315
316 while (len > 0 && ptr) {
317 pr_info(" action size %d, type %d ",
318 ((struct bts_action *)ptr)->size,
319 ((struct bts_action *)ptr)->type);
320
321 switch (((struct bts_action *)ptr)->type) {
322 case ACTION_SEND_COMMAND: /* action send */
323 action_ptr = &(((struct bts_action *)ptr)->data[0]);
324 if (unlikely
325 (((struct hci_command *)action_ptr)->opcode ==
326 0xFF36)) {
327 /* ignore remote change
328 * baud rate HCI VS command */
329 pr_err
330 (" change remote baud\
331 rate command in firmware");
332 break;
333 }
334
335 INIT_COMPLETION(kim_gdata->kim_rcvd);
336 err = st_int_write(kim_gdata->core_data,
337 ((struct bts_action_send *)action_ptr)->data,
338 ((struct bts_action *)ptr)->size);
339 if (unlikely(err < 0)) {
340 release_firmware(kim_gdata->fw_entry);
341 return ST_ERR_FAILURE;
342 }
343 if (!wait_for_completion_timeout
344 (&kim_gdata->kim_rcvd,
345 msecs_to_jiffies(CMD_RESP_TIME))) {
346 pr_err
347 (" response timeout during fw download ");
348 /* timed out */
349 release_firmware(kim_gdata->fw_entry);
350 return ST_ERR_FAILURE;
351 }
352 break;
353 case ACTION_DELAY: /* sleep */
354 pr_info("sleep command in scr");
355 action_ptr = &(((struct bts_action *)ptr)->data[0]);
356 mdelay(((struct bts_action_delay *)action_ptr)->msec);
357 break;
358 }
359 len =
360 len - (sizeof(struct bts_action) +
361 ((struct bts_action *)ptr)->size);
362 ptr =
363 ptr + sizeof(struct bts_action) +
364 ((struct bts_action *)ptr)->size;
365 }
366 /* fw download complete */
367 release_firmware(kim_gdata->fw_entry);
368 return ST_SUCCESS;
369}
370
371/**********************************************************************/
372/* functions called from ST core */
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500373/* function to toggle the GPIO
374 * needs to know whether the GPIO is active high or active low
375 */
376void st_kim_chip_toggle(enum proto_type type, enum kim_gpio_state state)
377{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500378 struct platform_device *kim_pdev;
379 struct kim_data_s *kim_gdata;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500380 pr_info(" %s ", __func__);
381
Pavan Savoy38d9df42010-07-08 08:55:17 -0500382 kim_pdev = st_get_plat_device();
383 kim_gdata = dev_get_drvdata(&kim_pdev->dev);
384
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500385 if (kim_gdata->gpios[type] == -1) {
386 pr_info(" gpio not requested for protocol %s",
387 protocol_names[type]);
388 return;
389 }
390 switch (type) {
391 case ST_BT:
392 /*Do Nothing */
393 break;
394
395 case ST_FM:
396 if (state == KIM_GPIO_ACTIVE)
397 gpio_set_value(kim_gdata->gpios[ST_FM], GPIO_LOW);
398 else
399 gpio_set_value(kim_gdata->gpios[ST_FM], GPIO_HIGH);
400 break;
401
402 case ST_GPS:
403 if (state == KIM_GPIO_ACTIVE)
404 gpio_set_value(kim_gdata->gpios[ST_GPS], GPIO_HIGH);
405 else
406 gpio_set_value(kim_gdata->gpios[ST_GPS], GPIO_LOW);
407 break;
408
409 case ST_MAX:
410 default:
411 break;
412 }
413
414 return;
415}
416
417/* called from ST Core, when REG_IN_PROGRESS (registration in progress)
418 * can be because of
419 * 1. response to read local version
420 * 2. during send/recv's of firmware download
421 */
422void st_kim_recv(void *disc_data, const unsigned char *data, long count)
423{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500424 struct st_data_s *st_gdata = (struct st_data_s *)disc_data;
425 struct kim_data_s *kim_gdata = st_gdata->kim_data;
426
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500427 pr_info(" %s ", __func__);
428 /* copy to local buffer */
429 if (unlikely(data[4] == 0x01 && data[5] == 0x10 && data[0] == 0x04)) {
430 /* must be the read_ver_cmd */
431 memcpy(kim_gdata->resp_buffer, data, count);
432 complete_all(&kim_gdata->kim_rcvd);
433 return;
434 } else {
Pavan Savoy38d9df42010-07-08 08:55:17 -0500435 kim_int_recv(kim_gdata, data, count);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500436 /* either completes or times out */
437 }
438 return;
439}
440
441/* to signal completion of line discipline installation
442 * called from ST Core, upon tty_open
443 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500444void st_kim_complete(void *kim_data)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500445{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500446 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500447 complete(&kim_gdata->ldisc_installed);
448}
449
450/* called from ST Core upon 1st registration
451*/
Pavan Savoy38d9df42010-07-08 08:55:17 -0500452long st_kim_start(void *kim_data)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500453{
454 long err = ST_SUCCESS;
455 long retry = POR_RETRY_COUNT;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500456 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
457
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500458 pr_info(" %s", __func__);
459
460 do {
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500461 /* TODO: this is only because rfkill sub-system
462 * doesn't send events to user-space if the state
463 * isn't changed
464 */
465 rfkill_set_hw_state(kim_gdata->rfkill[ST_BT], 1);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500466 /* Configure BT nShutdown to HIGH state */
467 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
468 mdelay(5); /* FIXME: a proper toggle */
469 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_HIGH);
470 mdelay(100);
471 /* re-initialize the completion */
472 INIT_COMPLETION(kim_gdata->ldisc_installed);
Naveen Jainb38fc2d2010-06-09 03:45:32 -0500473#if 0 /* older way of signalling user-space UIM */
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500474 /* send signal to UIM */
475 err = kill_pid(find_get_pid(kim_gdata->uim_pid), SIGUSR2, 0);
476 if (err != 0) {
477 pr_info(" sending SIGUSR2 to uim failed %ld", err);
478 err = ST_ERR_FAILURE;
479 continue;
480 }
Naveen Jainb38fc2d2010-06-09 03:45:32 -0500481#endif
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500482 /* unblock and send event to UIM via /dev/rfkill */
483 rfkill_set_hw_state(kim_gdata->rfkill[ST_BT], 0);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500484 /* wait for ldisc to be installed */
485 err = wait_for_completion_timeout(&kim_gdata->ldisc_installed,
486 msecs_to_jiffies(LDISC_TIME));
487 if (!err) { /* timeout */
488 pr_err("line disc installation timed out ");
489 err = ST_ERR_FAILURE;
490 continue;
491 } else {
492 /* ldisc installed now */
493 pr_info(" line discipline installed ");
Pavan Savoy38d9df42010-07-08 08:55:17 -0500494 err = download_firmware(kim_gdata);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500495 if (err != ST_SUCCESS) {
496 pr_err("download firmware failed");
497 continue;
498 } else { /* on success don't retry */
499 break;
500 }
501 }
502 } while (retry--);
503 return err;
504}
505
506/* called from ST Core, on the last un-registration
507*/
Pavan Savoy38d9df42010-07-08 08:55:17 -0500508long st_kim_stop(void *kim_data)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500509{
510 long err = ST_SUCCESS;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500511 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500512
513 INIT_COMPLETION(kim_gdata->ldisc_installed);
Naveen Jainb38fc2d2010-06-09 03:45:32 -0500514#if 0 /* older way of signalling user-space UIM */
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500515 /* send signal to UIM */
516 err = kill_pid(find_get_pid(kim_gdata->uim_pid), SIGUSR2, 1);
517 if (err != 0) {
518 pr_err("sending SIGUSR2 to uim failed %ld", err);
519 return ST_ERR_FAILURE;
520 }
Naveen Jainb38fc2d2010-06-09 03:45:32 -0500521#endif
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500522 /* set BT rfkill to be blocked */
523 err = rfkill_set_hw_state(kim_gdata->rfkill[ST_BT], 1);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500524
525 /* wait for ldisc to be un-installed */
526 err = wait_for_completion_timeout(&kim_gdata->ldisc_installed,
527 msecs_to_jiffies(LDISC_TIME));
528 if (!err) { /* timeout */
529 pr_err(" timed out waiting for ldisc to be un-installed");
530 return ST_ERR_FAILURE;
531 }
532
533 /* By default configure BT nShutdown to LOW state */
534 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
535 mdelay(1);
536 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_HIGH);
537 mdelay(1);
538 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
539 return err;
540}
541
542/**********************************************************************/
543/* functions called from subsystems */
Naveen Jaine2a53282010-06-09 03:45:33 -0500544/* called when sysfs entry is read from */
545
546static ssize_t show_version(struct device *dev, struct device_attribute
547 *attr, char *buf)
548{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500549 struct kim_data_s *kim_gdata = dev_get_drvdata(dev);
Naveen Jaine2a53282010-06-09 03:45:33 -0500550 sprintf(buf, "%04X %d.%d.%d", kim_gdata->version.full,
551 kim_gdata->version.chip, kim_gdata->version.maj_ver,
552 kim_gdata->version.min_ver);
553 return strlen(buf);
554}
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500555
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500556/* called when sysfs entry is written to */
557static ssize_t store_pid(struct device *dev, struct device_attribute
558 *devattr, char *buf, size_t count)
559{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500560 struct kim_data_s *kim_gdata = dev_get_drvdata(dev);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500561 pr_info("%s: pid %s ", __func__, buf);
562 sscanf(buf, "%ld", &kim_gdata->uim_pid);
563 /* to be made use by kim_start to signal SIGUSR2
564 */
565 return strlen(buf);
566}
567
568/* called when sysfs entry is read from */
569static ssize_t show_pid(struct device *dev, struct device_attribute
570 *attr, char *buf)
571{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500572 struct kim_data_s *kim_gdata = dev_get_drvdata(dev);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500573 sprintf(buf, "%ld", kim_gdata->uim_pid);
574 return strlen(buf);
575}
576
577/* called when sysfs entry is read from */
578static ssize_t show_list(struct device *dev, struct device_attribute
579 *attr, char *buf)
580{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500581 struct kim_data_s *kim_gdata = dev_get_drvdata(dev);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500582 kim_st_list_protocols(kim_gdata->core_data, buf);
583 return strlen(buf);
584}
585
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500586/* function called from rfkill subsystem, when someone from
587 * user space would write 0/1 on the sysfs entry
588 * /sys/class/rfkill/rfkill0,1,3/state
589 */
590static int kim_toggle_radio(void *data, bool blocked)
591{
592 enum proto_type type = *((enum proto_type *)data);
593 pr_info(" %s: %d ", __func__, type);
594
595 switch (type) {
596 case ST_BT:
597 /* do nothing */
598 break;
599 case ST_FM:
600 case ST_GPS:
601 if (blocked)
602 st_kim_chip_toggle(type, KIM_GPIO_INACTIVE);
603 else
604 st_kim_chip_toggle(type, KIM_GPIO_ACTIVE);
605 break;
606 case ST_MAX:
607 pr_err(" wrong proto type ");
608 break;
609 }
610 return ST_SUCCESS;
611}
612
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500613void st_kim_ref(struct st_data_s **core_data)
614{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500615 struct platform_device *pdev;
616 struct kim_data_s *kim_gdata;
617 /* get kim_gdata reference from platform device */
618 pdev = st_get_plat_device();
619 kim_gdata = dev_get_drvdata(&pdev->dev);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500620 *core_data = kim_gdata->core_data;
621}
622
623/**********************************************************************/
624/* functions called from platform device driver subsystem
625 * need to have a relevant platform device entry in the platform's
626 * board-*.c file
627 */
628
629static int kim_probe(struct platform_device *pdev)
630{
631 long status;
632 long proto;
633 long *gpios = pdev->dev.platform_data;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500634 struct kim_data_s *kim_gdata;
635
636 kim_gdata = kzalloc(sizeof(struct kim_data_s), GFP_ATOMIC);
637 if (!kim_gdata) {
638 pr_err("no mem to allocate");
639 return -ENOMEM;
640 }
641 dev_set_drvdata(&pdev->dev, kim_gdata);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500642
643 status = st_core_init(&kim_gdata->core_data);
644 if (status != 0) {
645 pr_err(" ST core init failed");
646 return ST_ERR_FAILURE;
647 }
Pavan Savoy38d9df42010-07-08 08:55:17 -0500648 /* refer to itself */
649 kim_gdata->core_data->kim_data = kim_gdata;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500650
651 for (proto = 0; proto < ST_MAX; proto++) {
652 kim_gdata->gpios[proto] = gpios[proto];
653 pr_info(" %ld gpio to be requested", gpios[proto]);
654 }
655
656 for (proto = 0; (proto < ST_MAX) && (gpios[proto] != -1); proto++) {
657 /* Claim the Bluetooth/FM/GPIO
658 * nShutdown gpio from the system
659 */
660 status = gpio_request(gpios[proto], "kim");
661 if (unlikely(status)) {
662 pr_err(" gpio %ld request failed ", gpios[proto]);
663 proto -= 1;
664 while (proto >= 0) {
665 if (gpios[proto] != -1)
666 gpio_free(gpios[proto]);
667 }
668 return status;
669 }
670
671 /* Configure nShutdown GPIO as output=0 */
672 status =
673 gpio_direction_output(gpios[proto], 0);
674 if (unlikely(status)) {
675 pr_err(" unable to configure gpio %ld",
676 gpios[proto]);
677 proto -= 1;
678 while (proto >= 0) {
679 if (gpios[proto] != -1)
680 gpio_free(gpios[proto]);
681 }
682 return status;
683 }
684 }
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500685 /* get reference of pdev for request_firmware
686 */
687 kim_gdata->kim_pdev = pdev;
688 init_completion(&kim_gdata->kim_rcvd);
689 init_completion(&kim_gdata->ldisc_installed);
Naveen Jainb38fc2d2010-06-09 03:45:32 -0500690
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500691 for (proto = 0; (proto < ST_MAX) && (gpios[proto] != -1); proto++) {
692 /* TODO: should all types be rfkill_type_bt ? */
693 kim_gdata->rf_protos[proto] = proto;
694 kim_gdata->rfkill[proto] = rfkill_alloc(protocol_names[proto],
695 &pdev->dev, RFKILL_TYPE_BLUETOOTH,
696 &kim_rfkill_ops, &kim_gdata->rf_protos[proto]);
697 if (kim_gdata->rfkill[proto] == NULL) {
698 pr_err("cannot create rfkill entry for gpio %ld",
699 gpios[proto]);
700 continue;
701 }
702 /* block upon creation */
703 rfkill_init_sw_state(kim_gdata->rfkill[proto], 1);
704 status = rfkill_register(kim_gdata->rfkill[proto]);
705 if (unlikely(status)) {
706 pr_err("rfkill registration failed for gpio %ld",
707 gpios[proto]);
708 rfkill_unregister(kim_gdata->rfkill[proto]);
709 continue;
710 }
711 pr_info("rfkill entry created for %ld", gpios[proto]);
712 }
Naveen Jaine2a53282010-06-09 03:45:33 -0500713
714 if (sysfs_create_group(&pdev->dev.kobj, &uim_attr_grp)) {
715 pr_err(" sysfs entry creation failed");
716 return -1;
717 }
718 pr_info(" sysfs entries created ");
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500719 return ST_SUCCESS;
720}
721
722static int kim_remove(struct platform_device *pdev)
723{
724 /* free the GPIOs requested
725 */
726 long *gpios = pdev->dev.platform_data;
727 long proto;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500728 struct kim_data_s *kim_gdata;
729
730 kim_gdata = dev_get_drvdata(&pdev->dev);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500731
732 for (proto = 0; (proto < ST_MAX) && (gpios[proto] != -1); proto++) {
733 /* Claim the Bluetooth/FM/GPIO
734 * nShutdown gpio from the system
735 */
736 gpio_free(gpios[proto]);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500737 rfkill_unregister(kim_gdata->rfkill[proto]);
738 rfkill_destroy(kim_gdata->rfkill[proto]);
739 kim_gdata->rfkill[proto] = NULL;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500740 }
741 pr_info("kim: GPIO Freed");
Naveen Jaine2a53282010-06-09 03:45:33 -0500742 sysfs_remove_group(&pdev->dev.kobj, &uim_attr_grp);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500743 kim_gdata->kim_pdev = NULL;
744 st_core_exit(kim_gdata->core_data);
Pavan Savoy38d9df42010-07-08 08:55:17 -0500745
746 kfree(kim_gdata);
747 kim_gdata = NULL;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500748 return ST_SUCCESS;
749}
750
751/**********************************************************************/
752/* entry point for ST KIM module, called in from ST Core */
753
754static int __init st_kim_init(void)
755{
756 long ret = ST_SUCCESS;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500757 ret = platform_driver_register(&kim_platform_driver);
758 if (ret != 0) {
759 pr_err("platform drv registration failed");
760 return ST_ERR_FAILURE;
761 }
762 return ST_SUCCESS;
763}
764
765static void __exit st_kim_deinit(void)
766{
767 /* the following returns void */
768 platform_driver_unregister(&kim_platform_driver);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500769}
770
771
772module_init(st_kim_init);
773module_exit(st_kim_deinit);
774MODULE_AUTHOR("Pavan Savoy <pavan_savoy@ti.com>");
775MODULE_DESCRIPTION("Shared Transport Driver for TI BT/FM/GPS combo chips ");
776MODULE_LICENSE("GPL");