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
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.
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.
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
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 #include <linux/debugfs.h>
30 #include <linux/seq_file.h>
32 #include <linux/sched.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>
41 static int kim_probe(struct platform_device *pdev);
42 static int kim_remove(struct platform_device *pdev);
44 /* KIM platform device driver structure */
45 static struct platform_driver kim_platform_driver = {
48 /* TODO: ST driver power management during suspend/resume ?
51 .suspend = kim_suspend,
60 static int kim_toggle_radio(void*, bool);
61 static const struct rfkill_ops kim_rfkill_ops = {
62 .set_block = kim_toggle_radio,
65 /* strings to be used for rfkill entries and by
66 * ST Core to be used for sysfs debug entry
68 #define PROTO_ENTRY(type, name) name
69 const unsigned char *protocol_names[] = {
70 PROTO_ENTRY(ST_BT, "Bluetooth"),
71 PROTO_ENTRY(ST_FM, "FM"),
72 PROTO_ENTRY(ST_GPS, "GPS"),
75 #define MAX_ST_DEVICES 3 /* Imagine 1 on each UART for now */
76 struct platform_device *st_kim_devices[MAX_ST_DEVICES];
78 /**********************************************************************/
79 /* internal functions */
82 * st_get_plat_device -
83 * function which returns the reference to the platform device
84 * requested by id. As of now only 1 such device exists (id=0)
85 * the context requesting for reference can get the id to be
86 * requested by a. The protocol driver which is registering or
87 * b. the tty device which is opened.
89 static struct platform_device *st_get_plat_device(int id)
91 return st_kim_devices[id];
95 * validate_firmware_response -
96 * function to return whether the firmware response was proper
97 * in case of error don't complete so that waiting for proper
100 void validate_firmware_response(struct kim_data_s *kim_gdata)
102 struct sk_buff *skb = kim_gdata->rx_skb;
103 if (unlikely(skb->data[5] != 0)) {
104 pr_err("no proper response during fw download");
105 pr_err("data6 %x", skb->data[5]);
106 return; /* keep waiting for the proper response */
108 /* becos of all the script being downloaded */
109 complete_all(&kim_gdata->kim_rcvd);
113 /* check for data len received inside kim_int_recv
114 * most often hit the last case to update state to waiting for data
116 static inline int kim_check_data_len(struct kim_data_s *kim_gdata, int len)
118 register int room = skb_tailroom(kim_gdata->rx_skb);
120 pr_debug("len %d room %d", len, room);
123 validate_firmware_response(kim_gdata);
124 } else if (len > room) {
125 /* Received packet's payload length is larger.
126 * We can't accommodate it in created skb.
128 pr_err("Data length is too large len %d room %d", len,
130 kfree_skb(kim_gdata->rx_skb);
132 /* Packet header has non-zero payload length and
133 * we have enough space in created skb. Lets read
135 kim_gdata->rx_state = ST_BT_W4_DATA;
136 kim_gdata->rx_count = len;
140 /* Change ST LL state to continue to process next
142 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
143 kim_gdata->rx_skb = NULL;
144 kim_gdata->rx_count = 0;
150 * kim_int_recv - receive function called during firmware download
151 * firmware download responses on different UART drivers
152 * have been observed to come in bursts of different
153 * tty_receive and hence the logic
155 void kim_int_recv(struct kim_data_s *kim_gdata,
156 const unsigned char *data, long count)
159 struct hci_event_hdr *eh;
160 register int len = 0, type = 0;
162 pr_debug("%s", __func__);
163 /* Decode received bytes here */
165 if (unlikely(ptr == NULL)) {
166 pr_err(" received null from TTY ");
170 if (kim_gdata->rx_count) {
171 len = min_t(unsigned int, kim_gdata->rx_count, count);
172 memcpy(skb_put(kim_gdata->rx_skb, len), ptr, len);
173 kim_gdata->rx_count -= len;
177 if (kim_gdata->rx_count)
180 /* Check ST RX state machine , where are we? */
181 switch (kim_gdata->rx_state) {
182 /* Waiting for complete packet ? */
184 pr_debug("Complete pkt received");
185 validate_firmware_response(kim_gdata);
186 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
187 kim_gdata->rx_skb = NULL;
189 /* Waiting for Bluetooth event header ? */
190 case ST_BT_W4_EVENT_HDR:
191 eh = (struct hci_event_hdr *)kim_gdata->
193 pr_debug("Event header: evt 0x%2.2x"
194 "plen %d", eh->evt, eh->plen);
195 kim_check_data_len(kim_gdata, eh->plen);
197 } /* end of switch */
198 } /* end of if rx_state */
200 /* Bluetooth event packet? */
202 pr_info("Event packet");
203 kim_gdata->rx_state = ST_BT_W4_EVENT_HDR;
204 kim_gdata->rx_count = HCI_EVENT_HDR_SIZE;
205 type = HCI_EVENT_PKT;
208 pr_info("unknown packet");
216 bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
217 if (!kim_gdata->rx_skb) {
218 pr_err("can't allocate mem for new packet");
219 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
220 kim_gdata->rx_count = 0;
223 bt_cb(kim_gdata->rx_skb)->pkt_type = type;
225 pr_info("done %s", __func__);
229 static long read_local_version(struct kim_data_s *kim_gdata, char *bts_scr_name)
231 unsigned short version = 0, chip = 0, min_ver = 0, maj_ver = 0;
232 char read_ver_cmd[] = { 0x01, 0x01, 0x10, 0x00 };
234 pr_debug("%s", __func__);
236 INIT_COMPLETION(kim_gdata->kim_rcvd);
237 if (4 != st_int_write(kim_gdata->core_data, read_ver_cmd, 4)) {
238 pr_err("kim: couldn't write 4 bytes");
242 if (!wait_for_completion_timeout
243 (&kim_gdata->kim_rcvd, msecs_to_jiffies(CMD_RESP_TIME))) {
244 pr_err(" waiting for ver info- timed out ");
249 MAKEWORD(kim_gdata->resp_buffer[13],
250 kim_gdata->resp_buffer[14]);
251 chip = (version & 0x7C00) >> 10;
252 min_ver = (version & 0x007F);
253 maj_ver = (version & 0x0380) >> 7;
255 if (version & 0x8000)
258 sprintf(bts_scr_name, "TIInit_%d.%d.%d.bts", chip, maj_ver, min_ver);
260 /* to be accessed later via sysfs entry */
261 kim_gdata->version.full = version;
262 kim_gdata->version.chip = chip;
263 kim_gdata->version.maj_ver = maj_ver;
264 kim_gdata->version.min_ver = min_ver;
266 pr_info("%s", bts_scr_name);
271 * download_firmware -
272 * internal function which parses through the .bts firmware
273 * script file intreprets SEND, DELAY actions only as of now
275 static long download_firmware(struct kim_data_s *kim_gdata)
279 register unsigned char *ptr = NULL;
280 register unsigned char *action_ptr = NULL;
281 unsigned char bts_scr_name[30] = { 0 }; /* 30 char long bts scr name? */
283 err = read_local_version(kim_gdata, bts_scr_name);
285 pr_err("kim: failed to read local ver");
289 request_firmware(&kim_gdata->fw_entry, bts_scr_name,
290 &kim_gdata->kim_pdev->dev);
291 if (unlikely((err != 0) || (kim_gdata->fw_entry->data == NULL) ||
292 (kim_gdata->fw_entry->size == 0))) {
293 pr_err(" request_firmware failed(errno %ld) for %s", err,
297 ptr = (void *)kim_gdata->fw_entry->data;
298 len = kim_gdata->fw_entry->size;
299 /* bts_header to remove out magic number and
302 ptr += sizeof(struct bts_header);
303 len -= sizeof(struct bts_header);
305 while (len > 0 && ptr) {
306 pr_debug(" action size %d, type %d ",
307 ((struct bts_action *)ptr)->size,
308 ((struct bts_action *)ptr)->type);
310 switch (((struct bts_action *)ptr)->type) {
311 case ACTION_SEND_COMMAND: /* action send */
312 action_ptr = &(((struct bts_action *)ptr)->data[0]);
314 (((struct hci_command *)action_ptr)->opcode ==
316 /* ignore remote change
317 * baud rate HCI VS command */
319 (" change remote baud"
320 " rate command in firmware");
324 INIT_COMPLETION(kim_gdata->kim_rcvd);
325 err = st_int_write(kim_gdata->core_data,
326 ((struct bts_action_send *)action_ptr)->data,
327 ((struct bts_action *)ptr)->size);
328 if (unlikely(err < 0)) {
329 release_firmware(kim_gdata->fw_entry);
332 if (!wait_for_completion_timeout
333 (&kim_gdata->kim_rcvd,
334 msecs_to_jiffies(CMD_RESP_TIME))) {
336 (" response timeout during fw download ");
338 release_firmware(kim_gdata->fw_entry);
342 case ACTION_DELAY: /* sleep */
343 pr_info("sleep command in scr");
344 action_ptr = &(((struct bts_action *)ptr)->data[0]);
345 mdelay(((struct bts_action_delay *)action_ptr)->msec);
349 len - (sizeof(struct bts_action) +
350 ((struct bts_action *)ptr)->size);
352 ptr + sizeof(struct bts_action) +
353 ((struct bts_action *)ptr)->size;
355 /* fw download complete */
356 release_firmware(kim_gdata->fw_entry);
360 /**********************************************************************/
361 /* functions called from ST core */
362 /* function to toggle the GPIO
363 * needs to know whether the GPIO is active high or active low
365 void st_kim_chip_toggle(enum proto_type type, enum kim_gpio_state state)
367 struct platform_device *kim_pdev;
368 struct kim_data_s *kim_gdata;
369 pr_info(" %s ", __func__);
371 kim_pdev = st_get_plat_device(0);
372 kim_gdata = dev_get_drvdata(&kim_pdev->dev);
374 if (kim_gdata->gpios[type] == -1) {
375 pr_info(" gpio not requested for protocol %s",
376 protocol_names[type]);
385 if (state == KIM_GPIO_ACTIVE)
386 gpio_set_value(kim_gdata->gpios[ST_FM], GPIO_LOW);
388 gpio_set_value(kim_gdata->gpios[ST_FM], GPIO_HIGH);
392 if (state == KIM_GPIO_ACTIVE)
393 gpio_set_value(kim_gdata->gpios[ST_GPS], GPIO_HIGH);
395 gpio_set_value(kim_gdata->gpios[ST_GPS], GPIO_LOW);
406 /* called from ST Core, when REG_IN_PROGRESS (registration in progress)
408 * 1. response to read local version
409 * 2. during send/recv's of firmware download
411 void st_kim_recv(void *disc_data, const unsigned char *data, long count)
413 struct st_data_s *st_gdata = (struct st_data_s *)disc_data;
414 struct kim_data_s *kim_gdata = st_gdata->kim_data;
416 pr_info(" %s ", __func__);
417 /* copy to local buffer */
418 if (unlikely(data[4] == 0x01 && data[5] == 0x10 && data[0] == 0x04)) {
419 /* must be the read_ver_cmd */
420 memcpy(kim_gdata->resp_buffer, data, count);
421 complete_all(&kim_gdata->kim_rcvd);
424 kim_int_recv(kim_gdata, data, count);
425 /* either completes or times out */
430 /* to signal completion of line discipline installation
431 * called from ST Core, upon tty_open
433 void st_kim_complete(void *kim_data)
435 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
436 complete(&kim_gdata->ldisc_installed);
440 * st_kim_start - called from ST Core upon 1st registration
441 * This involves toggling the chip enable gpio, reading
442 * the firmware version from chip, forming the fw file name
443 * based on the chip version, requesting the fw, parsing it
444 * and perform download(send/recv).
446 long st_kim_start(void *kim_data)
449 long retry = POR_RETRY_COUNT;
450 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
452 pr_info(" %s", __func__);
455 /* TODO: this is only because rfkill sub-system
456 * doesn't send events to user-space if the state
459 rfkill_set_hw_state(kim_gdata->rfkill[ST_BT], 1);
460 /* Configure BT nShutdown to HIGH state */
461 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
462 mdelay(5); /* FIXME: a proper toggle */
463 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_HIGH);
465 /* re-initialize the completion */
466 INIT_COMPLETION(kim_gdata->ldisc_installed);
467 #if 0 /* older way of signalling user-space UIM */
468 /* send signal to UIM */
469 err = kill_pid(find_get_pid(kim_gdata->uim_pid), SIGUSR2, 0);
471 pr_info(" sending SIGUSR2 to uim failed %ld", err);
476 /* unblock and send event to UIM via /dev/rfkill */
477 rfkill_set_hw_state(kim_gdata->rfkill[ST_BT], 0);
478 /* wait for ldisc to be installed */
479 err = wait_for_completion_timeout(&kim_gdata->ldisc_installed,
480 msecs_to_jiffies(LDISC_TIME));
481 if (!err) { /* timeout */
482 pr_err("line disc installation timed out ");
486 /* ldisc installed now */
487 pr_info(" line discipline installed ");
488 err = download_firmware(kim_gdata);
490 pr_err("download firmware failed");
492 } else { /* on success don't retry */
501 * st_kim_stop - called from ST Core, on the last un-registration
502 * toggle low the chip enable gpio
504 long st_kim_stop(void *kim_data)
507 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
509 INIT_COMPLETION(kim_gdata->ldisc_installed);
510 #if 0 /* older way of signalling user-space UIM */
511 /* send signal to UIM */
512 err = kill_pid(find_get_pid(kim_gdata->uim_pid), SIGUSR2, 1);
514 pr_err("sending SIGUSR2 to uim failed %ld", err);
518 /* set BT rfkill to be blocked */
519 err = rfkill_set_hw_state(kim_gdata->rfkill[ST_BT], 1);
521 /* wait for ldisc to be un-installed */
522 err = wait_for_completion_timeout(&kim_gdata->ldisc_installed,
523 msecs_to_jiffies(LDISC_TIME));
524 if (!err) { /* timeout */
525 pr_err(" timed out waiting for ldisc to be un-installed");
529 /* By default configure BT nShutdown to LOW state */
530 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
532 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_HIGH);
534 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
538 /**********************************************************************/
539 /* functions called from subsystems */
540 /* called when debugfs entry is read from */
542 static int show_version(struct seq_file *s, void *unused)
544 struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private;
545 seq_printf(s, "%04X %d.%d.%d\n", kim_gdata->version.full,
546 kim_gdata->version.chip, kim_gdata->version.maj_ver,
547 kim_gdata->version.min_ver);
551 static int show_list(struct seq_file *s, void *unused)
553 struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private;
554 kim_st_list_protocols(kim_gdata->core_data, s);
558 /* function called from rfkill subsystem, when someone from
559 * user space would write 0/1 on the sysfs entry
560 * /sys/class/rfkill/rfkill0,1,3/state
562 static int kim_toggle_radio(void *data, bool blocked)
564 enum proto_type type = *((enum proto_type *)data);
565 pr_debug(" %s: %d ", __func__, type);
574 st_kim_chip_toggle(type, KIM_GPIO_INACTIVE);
576 st_kim_chip_toggle(type, KIM_GPIO_ACTIVE);
579 pr_err(" wrong proto type ");
586 * st_kim_ref - reference the core's data
587 * This references the per-ST platform device in the arch/xx/
589 * This would enable multiple such platform devices to exist
590 * on a given platform
592 void st_kim_ref(struct st_data_s **core_data, int id)
594 struct platform_device *pdev;
595 struct kim_data_s *kim_gdata;
596 /* get kim_gdata reference from platform device */
597 pdev = st_get_plat_device(id);
598 kim_gdata = dev_get_drvdata(&pdev->dev);
599 *core_data = kim_gdata->core_data;
602 static int kim_version_open(struct inode *i, struct file *f)
604 return single_open(f, show_version, i->i_private);
607 static int kim_list_open(struct inode *i, struct file *f)
609 return single_open(f, show_list, i->i_private);
612 static const struct file_operations version_debugfs_fops = {
614 .open = kim_version_open,
617 .release = single_release,
619 static const struct file_operations list_debugfs_fops = {
621 .open = kim_list_open,
624 .release = single_release,
627 /**********************************************************************/
628 /* functions called from platform device driver subsystem
629 * need to have a relevant platform device entry in the platform's
633 struct dentry *kim_debugfs_dir;
634 static int kim_probe(struct platform_device *pdev)
638 long *gpios = pdev->dev.platform_data;
639 struct kim_data_s *kim_gdata;
641 if ((pdev->id != -1) && (pdev->id < MAX_ST_DEVICES)) {
642 /* multiple devices could exist */
643 st_kim_devices[pdev->id] = pdev;
645 /* platform's sure about existance of 1 device */
646 st_kim_devices[0] = pdev;
649 kim_gdata = kzalloc(sizeof(struct kim_data_s), GFP_ATOMIC);
651 pr_err("no mem to allocate");
654 dev_set_drvdata(&pdev->dev, kim_gdata);
656 status = st_core_init(&kim_gdata->core_data);
658 pr_err(" ST core init failed");
661 /* refer to itself */
662 kim_gdata->core_data->kim_data = kim_gdata;
664 for (proto = 0; proto < ST_MAX; proto++) {
665 kim_gdata->gpios[proto] = gpios[proto];
666 pr_info(" %ld gpio to be requested", gpios[proto]);
669 for (proto = 0; (proto < ST_MAX) && (gpios[proto] != -1); proto++) {
670 /* Claim the Bluetooth/FM/GPIO
671 * nShutdown gpio from the system
673 status = gpio_request(gpios[proto], "kim");
674 if (unlikely(status)) {
675 pr_err(" gpio %ld request failed ", gpios[proto]);
678 if (gpios[proto] != -1)
679 gpio_free(gpios[proto]);
684 /* Configure nShutdown GPIO as output=0 */
686 gpio_direction_output(gpios[proto], 0);
687 if (unlikely(status)) {
688 pr_err(" unable to configure gpio %ld",
692 if (gpios[proto] != -1)
693 gpio_free(gpios[proto]);
698 /* get reference of pdev for request_firmware
700 kim_gdata->kim_pdev = pdev;
701 init_completion(&kim_gdata->kim_rcvd);
702 init_completion(&kim_gdata->ldisc_installed);
704 for (proto = 0; (proto < ST_MAX) && (gpios[proto] != -1); proto++) {
705 /* TODO: should all types be rfkill_type_bt ? */
706 kim_gdata->rf_protos[proto] = proto;
707 kim_gdata->rfkill[proto] = rfkill_alloc(protocol_names[proto],
708 &pdev->dev, RFKILL_TYPE_BLUETOOTH,
709 &kim_rfkill_ops, &kim_gdata->rf_protos[proto]);
710 if (kim_gdata->rfkill[proto] == NULL) {
711 pr_err("cannot create rfkill entry for gpio %ld",
715 /* block upon creation */
716 rfkill_init_sw_state(kim_gdata->rfkill[proto], 1);
717 status = rfkill_register(kim_gdata->rfkill[proto]);
718 if (unlikely(status)) {
719 pr_err("rfkill registration failed for gpio %ld",
721 rfkill_unregister(kim_gdata->rfkill[proto]);
724 pr_info("rfkill entry created for %ld", gpios[proto]);
727 kim_debugfs_dir = debugfs_create_dir("ti-st", NULL);
728 if (IS_ERR(kim_debugfs_dir)) {
729 pr_err(" debugfs entries creation failed ");
730 kim_debugfs_dir = NULL;
734 debugfs_create_file("version", S_IRUGO, kim_debugfs_dir,
735 kim_gdata, &version_debugfs_fops);
736 debugfs_create_file("protocols", S_IRUGO, kim_debugfs_dir,
737 kim_gdata, &list_debugfs_fops);
738 pr_info(" debugfs entries created ");
742 static int kim_remove(struct platform_device *pdev)
744 /* free the GPIOs requested
746 long *gpios = pdev->dev.platform_data;
748 struct kim_data_s *kim_gdata;
750 kim_gdata = dev_get_drvdata(&pdev->dev);
752 for (proto = 0; (proto < ST_MAX) && (gpios[proto] != -1); proto++) {
753 /* Claim the Bluetooth/FM/GPIO
754 * nShutdown gpio from the system
756 gpio_free(gpios[proto]);
757 rfkill_unregister(kim_gdata->rfkill[proto]);
758 rfkill_destroy(kim_gdata->rfkill[proto]);
759 kim_gdata->rfkill[proto] = NULL;
761 pr_info("kim: GPIO Freed");
762 debugfs_remove_recursive(kim_debugfs_dir);
763 kim_gdata->kim_pdev = NULL;
764 st_core_exit(kim_gdata->core_data);
771 /**********************************************************************/
772 /* entry point for ST KIM module, called in from ST Core */
774 static int __init st_kim_init(void)
777 ret = platform_driver_register(&kim_platform_driver);
779 pr_err("platform drv registration failed");
785 static void __exit st_kim_deinit(void)
787 /* the following returns void */
788 platform_driver_unregister(&kim_platform_driver);
792 module_init(st_kim_init);
793 module_exit(st_kim_deinit);
794 MODULE_AUTHOR("Pavan Savoy <pavan_savoy@ti.com>");
795 MODULE_DESCRIPTION("Shared Transport Driver for TI BT/FM/GPS combo chips ");
796 MODULE_LICENSE("GPL");