2 * Shared Transport Line discipline driver Core
3 * Init Manager module responsible for GPIO control
4 * and firmware download
5 * Copyright (C) 2009-2010 Texas Instruments
6 * Author: Pavan Savoy <pavan_savoy@ti.com>
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.
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.
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
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>
30 #include <linux/debugfs.h>
31 #include <linux/seq_file.h>
32 #include <linux/sched.h>
33 #include <linux/rfkill.h>
35 /* understand BT events for fw response */
36 #include <net/bluetooth/bluetooth.h>
37 #include <net/bluetooth/hci_core.h>
38 #include <net/bluetooth/hci.h>
40 #include <linux/ti_wilink_st.h>
43 static int kim_probe(struct platform_device *pdev);
44 static int kim_remove(struct platform_device *pdev);
46 /* KIM platform device driver structure */
47 static struct platform_driver kim_platform_driver = {
50 /* TODO: ST driver power management during suspend/resume ?
53 .suspend = kim_suspend,
62 static int kim_toggle_radio(void*, bool);
63 static const struct rfkill_ops kim_rfkill_ops = {
64 .set_block = kim_toggle_radio,
67 /* strings to be used for rfkill entries and by
68 * ST Core to be used for sysfs debug entry
70 #define PROTO_ENTRY(type, name) name
71 const unsigned char *protocol_names[] = {
72 PROTO_ENTRY(ST_BT, "Bluetooth"),
73 PROTO_ENTRY(ST_FM, "FM"),
74 PROTO_ENTRY(ST_GPS, "GPS"),
77 #define MAX_ST_DEVICES 3 /* Imagine 1 on each UART for now */
78 static struct platform_device *st_kim_devices[MAX_ST_DEVICES];
80 /**********************************************************************/
81 /* internal functions */
84 * st_get_plat_device -
85 * function which returns the reference to the platform device
86 * requested by id. As of now only 1 such device exists (id=0)
87 * the context requesting for reference can get the id to be
88 * requested by a. The protocol driver which is registering or
89 * b. the tty device which is opened.
91 static struct platform_device *st_get_plat_device(int id)
93 return st_kim_devices[id];
97 * validate_firmware_response -
98 * function to return whether the firmware response was proper
99 * in case of error don't complete so that waiting for proper
102 void validate_firmware_response(struct kim_data_s *kim_gdata)
104 struct sk_buff *skb = kim_gdata->rx_skb;
105 if (unlikely(skb->data[5] != 0)) {
106 pr_err("no proper response during fw download");
107 pr_err("data6 %x", skb->data[5]);
108 return; /* keep waiting for the proper response */
110 /* becos of all the script being downloaded */
111 complete_all(&kim_gdata->kim_rcvd);
115 /* check for data len received inside kim_int_recv
116 * most often hit the last case to update state to waiting for data
118 static inline int kim_check_data_len(struct kim_data_s *kim_gdata, int len)
120 register int room = skb_tailroom(kim_gdata->rx_skb);
122 pr_debug("len %d room %d", len, room);
125 validate_firmware_response(kim_gdata);
126 } else if (len > room) {
127 /* Received packet's payload length is larger.
128 * We can't accommodate it in created skb.
130 pr_err("Data length is too large len %d room %d", len,
132 kfree_skb(kim_gdata->rx_skb);
134 /* Packet header has non-zero payload length and
135 * we have enough space in created skb. Lets read
137 kim_gdata->rx_state = ST_BT_W4_DATA;
138 kim_gdata->rx_count = len;
142 /* Change ST LL state to continue to process next
144 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
145 kim_gdata->rx_skb = NULL;
146 kim_gdata->rx_count = 0;
152 * kim_int_recv - receive function called during firmware download
153 * firmware download responses on different UART drivers
154 * have been observed to come in bursts of different
155 * tty_receive and hence the logic
157 void kim_int_recv(struct kim_data_s *kim_gdata,
158 const unsigned char *data, long count)
160 const unsigned char *ptr;
161 struct hci_event_hdr *eh;
162 int len = 0, type = 0;
164 pr_debug("%s", __func__);
165 /* Decode received bytes here */
167 if (unlikely(ptr == NULL)) {
168 pr_err(" received null from TTY ");
173 if (kim_gdata->rx_count) {
174 len = min_t(unsigned int, kim_gdata->rx_count, count);
175 memcpy(skb_put(kim_gdata->rx_skb, len), ptr, len);
176 kim_gdata->rx_count -= len;
180 if (kim_gdata->rx_count)
183 /* Check ST RX state machine , where are we? */
184 switch (kim_gdata->rx_state) {
185 /* Waiting for complete packet ? */
187 pr_debug("Complete pkt received");
188 validate_firmware_response(kim_gdata);
189 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
190 kim_gdata->rx_skb = NULL;
192 /* Waiting for Bluetooth event header ? */
193 case ST_BT_W4_EVENT_HDR:
194 eh = (struct hci_event_hdr *)kim_gdata->
196 pr_debug("Event header: evt 0x%2.2x"
197 "plen %d", eh->evt, eh->plen);
198 kim_check_data_len(kim_gdata, eh->plen);
200 } /* end of switch */
201 } /* end of if rx_state */
203 /* Bluetooth event packet? */
205 pr_info("Event packet");
206 kim_gdata->rx_state = ST_BT_W4_EVENT_HDR;
207 kim_gdata->rx_count = HCI_EVENT_HDR_SIZE;
208 type = HCI_EVENT_PKT;
211 pr_info("unknown packet");
219 bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
220 if (!kim_gdata->rx_skb) {
221 pr_err("can't allocate mem for new packet");
222 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
223 kim_gdata->rx_count = 0;
226 bt_cb(kim_gdata->rx_skb)->pkt_type = type;
228 pr_info("done %s", __func__);
232 static long read_local_version(struct kim_data_s *kim_gdata, char *bts_scr_name)
234 unsigned short version = 0, chip = 0, min_ver = 0, maj_ver = 0;
235 const char read_ver_cmd[] = { 0x01, 0x01, 0x10, 0x00 };
237 pr_debug("%s", __func__);
239 INIT_COMPLETION(kim_gdata->kim_rcvd);
240 if (4 != st_int_write(kim_gdata->core_data, read_ver_cmd, 4)) {
241 pr_err("kim: couldn't write 4 bytes");
245 if (!wait_for_completion_timeout
246 (&kim_gdata->kim_rcvd, msecs_to_jiffies(CMD_RESP_TIME))) {
247 pr_err(" waiting for ver info- timed out ");
252 MAKEWORD(kim_gdata->resp_buffer[13],
253 kim_gdata->resp_buffer[14]);
254 chip = (version & 0x7C00) >> 10;
255 min_ver = (version & 0x007F);
256 maj_ver = (version & 0x0380) >> 7;
258 if (version & 0x8000)
261 sprintf(bts_scr_name, "TIInit_%d.%d.%d.bts", chip, maj_ver, min_ver);
263 /* to be accessed later via sysfs entry */
264 kim_gdata->version.full = version;
265 kim_gdata->version.chip = chip;
266 kim_gdata->version.maj_ver = maj_ver;
267 kim_gdata->version.min_ver = min_ver;
269 pr_info("%s", bts_scr_name);
274 * download_firmware -
275 * internal function which parses through the .bts firmware
276 * script file intreprets SEND, DELAY actions only as of now
278 static long download_firmware(struct kim_data_s *kim_gdata)
282 unsigned char *ptr = NULL;
283 unsigned char *action_ptr = NULL;
284 unsigned char bts_scr_name[30] = { 0 }; /* 30 char long bts scr name? */
286 err = read_local_version(kim_gdata, bts_scr_name);
288 pr_err("kim: failed to read local ver");
292 request_firmware(&kim_gdata->fw_entry, bts_scr_name,
293 &kim_gdata->kim_pdev->dev);
294 if (unlikely((err != 0) || (kim_gdata->fw_entry->data == NULL) ||
295 (kim_gdata->fw_entry->size == 0))) {
296 pr_err(" request_firmware failed(errno %ld) for %s", err,
300 ptr = (void *)kim_gdata->fw_entry->data;
301 len = kim_gdata->fw_entry->size;
302 /* bts_header to remove out magic number and
305 ptr += sizeof(struct bts_header);
306 len -= sizeof(struct bts_header);
308 while (len > 0 && ptr) {
309 pr_debug(" action size %d, type %d ",
310 ((struct bts_action *)ptr)->size,
311 ((struct bts_action *)ptr)->type);
313 switch (((struct bts_action *)ptr)->type) {
314 case ACTION_SEND_COMMAND: /* action send */
315 action_ptr = &(((struct bts_action *)ptr)->data[0]);
317 (((struct hci_command *)action_ptr)->opcode ==
319 /* ignore remote change
320 * baud rate HCI VS command */
322 (" change remote baud"
323 " rate command in firmware");
327 INIT_COMPLETION(kim_gdata->kim_rcvd);
328 err = st_int_write(kim_gdata->core_data,
329 ((struct bts_action_send *)action_ptr)->data,
330 ((struct bts_action *)ptr)->size);
331 if (unlikely(err < 0)) {
332 release_firmware(kim_gdata->fw_entry);
335 if (!wait_for_completion_timeout
336 (&kim_gdata->kim_rcvd,
337 msecs_to_jiffies(CMD_RESP_TIME))) {
339 (" response timeout during fw download ");
341 release_firmware(kim_gdata->fw_entry);
345 case ACTION_DELAY: /* sleep */
346 pr_info("sleep command in scr");
347 action_ptr = &(((struct bts_action *)ptr)->data[0]);
348 mdelay(((struct bts_action_delay *)action_ptr)->msec);
352 len - (sizeof(struct bts_action) +
353 ((struct bts_action *)ptr)->size);
355 ptr + sizeof(struct bts_action) +
356 ((struct bts_action *)ptr)->size;
358 /* fw download complete */
359 release_firmware(kim_gdata->fw_entry);
363 /**********************************************************************/
364 /* functions called from ST core */
365 /* function to toggle the GPIO
366 * needs to know whether the GPIO is active high or active low
368 void st_kim_chip_toggle(enum proto_type type, enum kim_gpio_state state)
370 struct platform_device *kim_pdev;
371 struct kim_data_s *kim_gdata;
372 pr_info(" %s ", __func__);
374 kim_pdev = st_get_plat_device(0);
375 kim_gdata = dev_get_drvdata(&kim_pdev->dev);
377 if (kim_gdata->gpios[type] == -1) {
378 pr_info(" gpio not requested for protocol %s",
379 protocol_names[type]);
388 if (state == KIM_GPIO_ACTIVE)
389 gpio_set_value(kim_gdata->gpios[ST_FM], GPIO_LOW);
391 gpio_set_value(kim_gdata->gpios[ST_FM], GPIO_HIGH);
395 if (state == KIM_GPIO_ACTIVE)
396 gpio_set_value(kim_gdata->gpios[ST_GPS], GPIO_HIGH);
398 gpio_set_value(kim_gdata->gpios[ST_GPS], GPIO_LOW);
409 /* called from ST Core, when REG_IN_PROGRESS (registration in progress)
411 * 1. response to read local version
412 * 2. during send/recv's of firmware download
414 void st_kim_recv(void *disc_data, const unsigned char *data, long count)
416 struct st_data_s *st_gdata = (struct st_data_s *)disc_data;
417 struct kim_data_s *kim_gdata = st_gdata->kim_data;
419 pr_info(" %s ", __func__);
420 /* copy to local buffer */
421 if (unlikely(data[4] == 0x01 && data[5] == 0x10 && data[0] == 0x04)) {
422 /* must be the read_ver_cmd */
423 memcpy(kim_gdata->resp_buffer, data, count);
424 complete_all(&kim_gdata->kim_rcvd);
427 kim_int_recv(kim_gdata, data, count);
428 /* either completes or times out */
433 /* to signal completion of line discipline installation
434 * called from ST Core, upon tty_open
436 void st_kim_complete(void *kim_data)
438 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
439 complete(&kim_gdata->ldisc_installed);
443 * st_kim_start - called from ST Core upon 1st registration
444 * This involves toggling the chip enable gpio, reading
445 * the firmware version from chip, forming the fw file name
446 * based on the chip version, requesting the fw, parsing it
447 * and perform download(send/recv).
449 long st_kim_start(void *kim_data)
452 long retry = POR_RETRY_COUNT;
453 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
455 pr_info(" %s", __func__);
458 /* TODO: this is only because rfkill sub-system
459 * doesn't send events to user-space if the state
462 rfkill_set_hw_state(kim_gdata->rfkill[ST_BT], 1);
463 /* Configure BT nShutdown to HIGH state */
464 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
465 mdelay(5); /* FIXME: a proper toggle */
466 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_HIGH);
468 /* re-initialize the completion */
469 INIT_COMPLETION(kim_gdata->ldisc_installed);
470 #if 0 /* older way of signalling user-space UIM */
471 /* send signal to UIM */
472 err = kill_pid(find_get_pid(kim_gdata->uim_pid), SIGUSR2, 0);
474 pr_info(" sending SIGUSR2 to uim failed %ld", err);
479 /* unblock and send event to UIM via /dev/rfkill */
480 rfkill_set_hw_state(kim_gdata->rfkill[ST_BT], 0);
481 /* wait for ldisc to be installed */
482 err = wait_for_completion_timeout(&kim_gdata->ldisc_installed,
483 msecs_to_jiffies(LDISC_TIME));
484 if (!err) { /* timeout */
485 pr_err("line disc installation timed out ");
489 /* ldisc installed now */
490 pr_info(" line discipline installed ");
491 err = download_firmware(kim_gdata);
493 pr_err("download firmware failed");
495 } else { /* on success don't retry */
504 * st_kim_stop - called from ST Core, on the last un-registration
505 * toggle low the chip enable gpio
507 long st_kim_stop(void *kim_data)
510 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
512 INIT_COMPLETION(kim_gdata->ldisc_installed);
513 #if 0 /* older way of signalling user-space UIM */
514 /* send signal to UIM */
515 err = kill_pid(find_get_pid(kim_gdata->uim_pid), SIGUSR2, 1);
517 pr_err("sending SIGUSR2 to uim failed %ld", err);
521 /* set BT rfkill to be blocked */
522 err = rfkill_set_hw_state(kim_gdata->rfkill[ST_BT], 1);
524 /* wait for ldisc to be un-installed */
525 err = wait_for_completion_timeout(&kim_gdata->ldisc_installed,
526 msecs_to_jiffies(LDISC_TIME));
527 if (!err) { /* timeout */
528 pr_err(" timed out waiting for ldisc to be un-installed");
532 /* By default configure BT nShutdown to LOW state */
533 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
535 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_HIGH);
537 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
541 /**********************************************************************/
542 /* functions called from subsystems */
543 /* called when debugfs entry is read from */
545 static int show_version(struct seq_file *s, void *unused)
547 struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private;
548 seq_printf(s, "%04X %d.%d.%d\n", kim_gdata->version.full,
549 kim_gdata->version.chip, kim_gdata->version.maj_ver,
550 kim_gdata->version.min_ver);
554 static int show_list(struct seq_file *s, void *unused)
556 struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private;
557 kim_st_list_protocols(kim_gdata->core_data, s);
561 /* function called from rfkill subsystem, when someone from
562 * user space would write 0/1 on the sysfs entry
563 * /sys/class/rfkill/rfkill0,1,3/state
565 static int kim_toggle_radio(void *data, bool blocked)
567 enum proto_type type = *((enum proto_type *)data);
568 pr_debug(" %s: %d ", __func__, type);
577 st_kim_chip_toggle(type, KIM_GPIO_INACTIVE);
579 st_kim_chip_toggle(type, KIM_GPIO_ACTIVE);
582 pr_err(" wrong proto type ");
589 * st_kim_ref - reference the core's data
590 * This references the per-ST platform device in the arch/xx/
592 * This would enable multiple such platform devices to exist
593 * on a given platform
595 void st_kim_ref(struct st_data_s **core_data, int id)
597 struct platform_device *pdev;
598 struct kim_data_s *kim_gdata;
599 /* get kim_gdata reference from platform device */
600 pdev = st_get_plat_device(id);
601 kim_gdata = dev_get_drvdata(&pdev->dev);
602 *core_data = kim_gdata->core_data;
605 static int kim_version_open(struct inode *i, struct file *f)
607 return single_open(f, show_version, i->i_private);
610 static int kim_list_open(struct inode *i, struct file *f)
612 return single_open(f, show_list, i->i_private);
615 static const struct file_operations version_debugfs_fops = {
617 .open = kim_version_open,
620 .release = single_release,
622 static const struct file_operations list_debugfs_fops = {
624 .open = kim_list_open,
627 .release = single_release,
630 /**********************************************************************/
631 /* functions called from platform device driver subsystem
632 * need to have a relevant platform device entry in the platform's
636 struct dentry *kim_debugfs_dir;
637 static int kim_probe(struct platform_device *pdev)
641 long *gpios = pdev->dev.platform_data;
642 struct kim_data_s *kim_gdata;
644 if ((pdev->id != -1) && (pdev->id < MAX_ST_DEVICES)) {
645 /* multiple devices could exist */
646 st_kim_devices[pdev->id] = pdev;
648 /* platform's sure about existance of 1 device */
649 st_kim_devices[0] = pdev;
652 kim_gdata = kzalloc(sizeof(struct kim_data_s), GFP_ATOMIC);
654 pr_err("no mem to allocate");
657 dev_set_drvdata(&pdev->dev, kim_gdata);
659 status = st_core_init(&kim_gdata->core_data);
661 pr_err(" ST core init failed");
664 /* refer to itself */
665 kim_gdata->core_data->kim_data = kim_gdata;
667 for (proto = 0; proto < ST_MAX; proto++) {
668 kim_gdata->gpios[proto] = gpios[proto];
669 pr_info(" %ld gpio to be requested", gpios[proto]);
672 for (proto = 0; (proto < ST_MAX) && (gpios[proto] != -1); proto++) {
673 /* Claim the Bluetooth/FM/GPIO
674 * nShutdown gpio from the system
676 status = gpio_request(gpios[proto], "kim");
677 if (unlikely(status)) {
678 pr_err(" gpio %ld request failed ", gpios[proto]);
681 if (gpios[proto] != -1)
682 gpio_free(gpios[proto]);
687 /* Configure nShutdown GPIO as output=0 */
689 gpio_direction_output(gpios[proto], 0);
690 if (unlikely(status)) {
691 pr_err(" unable to configure gpio %ld",
695 if (gpios[proto] != -1)
696 gpio_free(gpios[proto]);
701 /* get reference of pdev for request_firmware
703 kim_gdata->kim_pdev = pdev;
704 init_completion(&kim_gdata->kim_rcvd);
705 init_completion(&kim_gdata->ldisc_installed);
707 for (proto = 0; (proto < ST_MAX) && (gpios[proto] != -1); proto++) {
708 /* TODO: should all types be rfkill_type_bt ? */
709 kim_gdata->rf_protos[proto] = proto;
710 kim_gdata->rfkill[proto] = rfkill_alloc(protocol_names[proto],
711 &pdev->dev, RFKILL_TYPE_BLUETOOTH,
712 &kim_rfkill_ops, &kim_gdata->rf_protos[proto]);
713 if (kim_gdata->rfkill[proto] == NULL) {
714 pr_err("cannot create rfkill entry for gpio %ld",
718 /* block upon creation */
719 rfkill_init_sw_state(kim_gdata->rfkill[proto], 1);
720 status = rfkill_register(kim_gdata->rfkill[proto]);
721 if (unlikely(status)) {
722 pr_err("rfkill registration failed for gpio %ld",
724 rfkill_unregister(kim_gdata->rfkill[proto]);
727 pr_info("rfkill entry created for %ld", gpios[proto]);
730 kim_debugfs_dir = debugfs_create_dir("ti-st", NULL);
731 if (IS_ERR(kim_debugfs_dir)) {
732 pr_err(" debugfs entries creation failed ");
733 kim_debugfs_dir = NULL;
737 debugfs_create_file("version", S_IRUGO, kim_debugfs_dir,
738 kim_gdata, &version_debugfs_fops);
739 debugfs_create_file("protocols", S_IRUGO, kim_debugfs_dir,
740 kim_gdata, &list_debugfs_fops);
741 pr_info(" debugfs entries created ");
745 static int kim_remove(struct platform_device *pdev)
747 /* free the GPIOs requested
749 long *gpios = pdev->dev.platform_data;
751 struct kim_data_s *kim_gdata;
753 kim_gdata = dev_get_drvdata(&pdev->dev);
755 for (proto = 0; (proto < ST_MAX) && (gpios[proto] != -1); proto++) {
756 /* Claim the Bluetooth/FM/GPIO
757 * nShutdown gpio from the system
759 gpio_free(gpios[proto]);
760 rfkill_unregister(kim_gdata->rfkill[proto]);
761 rfkill_destroy(kim_gdata->rfkill[proto]);
762 kim_gdata->rfkill[proto] = NULL;
764 pr_info("kim: GPIO Freed");
765 debugfs_remove_recursive(kim_debugfs_dir);
766 kim_gdata->kim_pdev = NULL;
767 st_core_exit(kim_gdata->core_data);
774 /**********************************************************************/
775 /* entry point for ST KIM module, called in from ST Core */
777 static int __init st_kim_init(void)
780 ret = platform_driver_register(&kim_platform_driver);
782 pr_err("platform drv registration failed");
788 static void __exit st_kim_deinit(void)
790 /* the following returns void */
791 platform_driver_unregister(&kim_platform_driver);
795 module_init(st_kim_init);
796 module_exit(st_kim_deinit);
797 MODULE_AUTHOR("Pavan Savoy <pavan_savoy@ti.com>");
798 MODULE_DESCRIPTION("Shared Transport Driver for TI BT/FM/GPS combo chips ");
799 MODULE_LICENSE("GPL");