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>
30 #include <linux/sched.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>
39 static int kim_probe(struct platform_device *pdev);
40 static int kim_remove(struct platform_device *pdev);
42 /* KIM platform device driver structure */
43 static struct platform_driver kim_platform_driver = {
46 /* TODO: ST driver power management during suspend/resume ?
49 .suspend = kim_suspend,
58 #ifndef LEGACY_RFKILL_SUPPORT
59 static ssize_t show_pid(struct device *dev, struct device_attribute
61 static ssize_t store_pid(struct device *dev, struct device_attribute
62 *devattr, char *buf, size_t count);
63 static ssize_t show_list(struct device *dev, struct device_attribute
66 /* structures specific for sysfs entries */
67 static struct kobj_attribute pid_attr =
68 __ATTR(pid, 0644, (void *)show_pid, (void *)store_pid);
70 static struct kobj_attribute list_protocols =
71 __ATTR(protocols, 0444, (void *)show_list, NULL);
73 static struct attribute *uim_attrs[] = {
75 /* add more debug sysfs entries */
80 static struct attribute_group uim_attr_grp = {
84 static int kim_toggle_radio(void*, bool);
85 static const struct rfkill_ops kim_rfkill_ops = {
86 .set_block = kim_toggle_radio,
88 #endif /* LEGACY_RFKILL_SUPPORT */
90 /* strings to be used for rfkill entries and by
91 * ST Core to be used for sysfs debug entry
93 #define PROTO_ENTRY(type, name) name
94 const unsigned char *protocol_names[] = {
95 PROTO_ENTRY(ST_BT, "Bluetooth"),
96 PROTO_ENTRY(ST_FM, "FM"),
97 PROTO_ENTRY(ST_GPS, "GPS"),
100 struct kim_data_s *kim_gdata;
102 /**********************************************************************/
103 /* internal functions */
106 * function to return whether the firmware response was proper
107 * in case of error don't complete so that waiting for proper
110 void validate_firmware_response(struct sk_buff *skb)
112 if (unlikely(skb->data[5] != 0)) {
113 pr_err("no proper response during fw download");
114 pr_err("data6 %x", skb->data[5]);
115 return; /* keep waiting for the proper response */
117 /* becos of all the script being downloaded */
118 complete_all(&kim_gdata->kim_rcvd);
122 /* check for data len received inside kim_int_recv
123 * most often hit the last case to update state to waiting for data
125 static inline int kim_check_data_len(int len)
127 register int room = skb_tailroom(kim_gdata->rx_skb);
129 pr_info("len %d room %d", len, room);
132 validate_firmware_response(kim_gdata->rx_skb);
133 } else if (len > room) {
134 /* Received packet's payload length is larger.
135 * We can't accommodate it in created skb.
137 pr_err("Data length is too large len %d room %d", len,
139 kfree_skb(kim_gdata->rx_skb);
141 /* Packet header has non-zero payload length and
142 * we have enough space in created skb. Lets read
144 kim_gdata->rx_state = ST_BT_W4_DATA;
145 kim_gdata->rx_count = len;
149 /* Change ST LL state to continue to process next
151 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
152 kim_gdata->rx_skb = NULL;
153 kim_gdata->rx_count = 0;
158 /* receive function called during firmware download
159 * - firmware download responses on different UART drivers
160 * have been observed to come in bursts of different
161 * tty_receive and hence the logic
163 void kim_int_recv(const unsigned char *data, long count)
166 struct hci_event_hdr *eh;
167 register int len = 0, type = 0;
169 pr_info("%s", __func__);
170 /* Decode received bytes here */
172 if (unlikely(ptr == NULL)) {
173 pr_err(" received null from TTY ");
177 if (kim_gdata->rx_count) {
178 len = min_t(unsigned int, kim_gdata->rx_count, count);
179 memcpy(skb_put(kim_gdata->rx_skb, len), ptr, len);
180 kim_gdata->rx_count -= len;
184 if (kim_gdata->rx_count)
187 /* Check ST RX state machine , where are we? */
188 switch (kim_gdata->rx_state) {
189 /* Waiting for complete packet ? */
191 pr_info("Complete pkt received");
192 validate_firmware_response(kim_gdata->rx_skb);
193 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
194 kim_gdata->rx_skb = NULL;
196 /* Waiting for Bluetooth event header ? */
197 case ST_BT_W4_EVENT_HDR:
198 eh = (struct hci_event_hdr *)kim_gdata->
200 pr_info("Event header: evt 0x%2.2x"
201 "plen %d", eh->evt, eh->plen);
202 kim_check_data_len(eh->plen);
204 } /* end of switch */
205 } /* end of if rx_state */
207 /* Bluetooth event packet? */
209 pr_info("Event packet");
210 kim_gdata->rx_state = ST_BT_W4_EVENT_HDR;
211 kim_gdata->rx_count = HCI_EVENT_HDR_SIZE;
212 type = HCI_EVENT_PKT;
215 pr_info("unknown packet");
219 } /* end of switch *ptr */
223 bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
224 if (!kim_gdata->rx_skb) {
225 pr_err("can't allocate mem for new packet");
226 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
227 kim_gdata->rx_count = 0;
229 } /* not necessary in this case */
230 bt_cb(kim_gdata->rx_skb)->pkt_type = type;
231 } /* end of while count */
232 pr_info("done %s", __func__);
236 static long read_local_version(char *bts_scr_name)
238 unsigned short version = 0, chip = 0, min_ver = 0, maj_ver = 0;
239 char read_ver_cmd[] = { 0x01, 0x01, 0x10, 0x00 };
241 pr_info("%s", __func__);
243 INIT_COMPLETION(kim_gdata->kim_rcvd);
244 if (4 != st_int_write(kim_gdata->core_data, read_ver_cmd, 4)) {
245 pr_err("kim: couldn't write 4 bytes");
246 return ST_ERR_FAILURE;
249 if (!wait_for_completion_timeout
250 (&kim_gdata->kim_rcvd, msecs_to_jiffies(CMD_RESP_TIME))) {
251 pr_err(" waiting for ver info- timed out ");
252 return ST_ERR_FAILURE;
256 MAKEWORD(kim_gdata->resp_buffer[13], kim_gdata->resp_buffer[14]);
257 chip = (version & 0x7C00) >> 10;
258 min_ver = (version & 0x007F);
259 maj_ver = (version & 0x0380) >> 7;
261 if (version & 0x8000)
264 sprintf(bts_scr_name, "TIInit_%d.%d.%d.bts", chip, maj_ver, min_ver);
265 pr_info("%s", bts_scr_name);
269 /* internal function which parses through the .bts firmware script file
270 * intreprets SEND, DELAY actions only as of now
272 static long download_firmware(void)
274 long err = ST_SUCCESS;
276 register unsigned char *ptr = NULL;
277 register unsigned char *action_ptr = NULL;
278 unsigned char bts_scr_name[30] = { 0 }; /* 30 char long bts scr name? */
280 pr_info("%s", __func__);
282 err = read_local_version(bts_scr_name);
283 if (err != ST_SUCCESS) {
284 pr_err("kim: failed to read local ver");
288 request_firmware(&kim_gdata->fw_entry, bts_scr_name,
289 &kim_gdata->kim_pdev->dev);
290 if (unlikely((err != 0) || (kim_gdata->fw_entry->data == NULL) ||
291 (kim_gdata->fw_entry->size == 0))) {
292 pr_err(" request_firmware failed(errno %ld) for %s", err,
294 return ST_ERR_FAILURE;
296 ptr = (void *)kim_gdata->fw_entry->data;
297 len = kim_gdata->fw_entry->size;
298 /* bts_header to remove out magic number and
301 ptr += sizeof(struct bts_header);
302 len -= sizeof(struct bts_header);
304 while (len > 0 && ptr) {
305 pr_info(" action size %d, type %d ",
306 ((struct bts_action *)ptr)->size,
307 ((struct bts_action *)ptr)->type);
309 switch (((struct bts_action *)ptr)->type) {
310 case ACTION_SEND_COMMAND: /* action send */
311 action_ptr = &(((struct bts_action *)ptr)->data[0]);
313 (((struct hci_command *)action_ptr)->opcode ==
315 /* ignore remote change
316 * baud rate HCI VS command */
318 (" change remote baud\
319 rate command in firmware");
323 INIT_COMPLETION(kim_gdata->kim_rcvd);
324 err = st_int_write(kim_gdata->core_data,
325 ((struct bts_action_send *)action_ptr)->data,
326 ((struct bts_action *)ptr)->size);
327 if (unlikely(err < 0)) {
328 release_firmware(kim_gdata->fw_entry);
329 return ST_ERR_FAILURE;
331 if (!wait_for_completion_timeout
332 (&kim_gdata->kim_rcvd,
333 msecs_to_jiffies(CMD_RESP_TIME))) {
335 (" response timeout during fw download ");
337 release_firmware(kim_gdata->fw_entry);
338 return ST_ERR_FAILURE;
341 case ACTION_DELAY: /* sleep */
342 pr_info("sleep command in scr");
343 action_ptr = &(((struct bts_action *)ptr)->data[0]);
344 mdelay(((struct bts_action_delay *)action_ptr)->msec);
348 len - (sizeof(struct bts_action) +
349 ((struct bts_action *)ptr)->size);
351 ptr + sizeof(struct bts_action) +
352 ((struct bts_action *)ptr)->size;
354 /* fw download complete */
355 release_firmware(kim_gdata->fw_entry);
359 /**********************************************************************/
360 /* 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 pr_info(" %s ", __func__);
369 if (kim_gdata->gpios[type] == -1) {
370 pr_info(" gpio not requested for protocol %s",
371 protocol_names[type]);
380 if (state == KIM_GPIO_ACTIVE)
381 gpio_set_value(kim_gdata->gpios[ST_FM], GPIO_LOW);
383 gpio_set_value(kim_gdata->gpios[ST_FM], GPIO_HIGH);
387 if (state == KIM_GPIO_ACTIVE)
388 gpio_set_value(kim_gdata->gpios[ST_GPS], GPIO_HIGH);
390 gpio_set_value(kim_gdata->gpios[ST_GPS], GPIO_LOW);
401 /* called from ST Core, when REG_IN_PROGRESS (registration in progress)
403 * 1. response to read local version
404 * 2. during send/recv's of firmware download
406 void st_kim_recv(void *disc_data, const unsigned char *data, long count)
408 pr_info(" %s ", __func__);
409 /* copy to local buffer */
410 if (unlikely(data[4] == 0x01 && data[5] == 0x10 && data[0] == 0x04)) {
411 /* must be the read_ver_cmd */
412 memcpy(kim_gdata->resp_buffer, data, count);
413 complete_all(&kim_gdata->kim_rcvd);
416 kim_int_recv(data, count);
417 /* either completes or times out */
422 /* to signal completion of line discipline installation
423 * called from ST Core, upon tty_open
425 void st_kim_complete(void)
427 complete(&kim_gdata->ldisc_installed);
430 /* called from ST Core upon 1st registration
432 long st_kim_start(void)
434 long err = ST_SUCCESS;
435 long retry = POR_RETRY_COUNT;
436 pr_info(" %s", __func__);
439 #ifdef LEGACY_RFKILL_SUPPORT
440 /* TODO: this is only because rfkill sub-system
441 * doesn't send events to user-space if the state
444 rfkill_set_hw_state(kim_gdata->rfkill[ST_BT], 1);
446 /* Configure BT nShutdown to HIGH state */
447 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
448 mdelay(5); /* FIXME: a proper toggle */
449 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_HIGH);
451 /* re-initialize the completion */
452 INIT_COMPLETION(kim_gdata->ldisc_installed);
453 #ifndef LEGACY_RFKILL_SUPPORT
454 /* send signal to UIM */
455 err = kill_pid(find_get_pid(kim_gdata->uim_pid), SIGUSR2, 0);
457 pr_info(" sending SIGUSR2 to uim failed %ld", err);
458 err = ST_ERR_FAILURE;
462 /* unblock and send event to UIM via /dev/rfkill */
463 rfkill_set_hw_state(kim_gdata->rfkill[ST_BT], 0);
465 /* wait for ldisc to be installed */
466 err = wait_for_completion_timeout(&kim_gdata->ldisc_installed,
467 msecs_to_jiffies(LDISC_TIME));
468 if (!err) { /* timeout */
469 pr_err("line disc installation timed out ");
470 err = ST_ERR_FAILURE;
473 /* ldisc installed now */
474 pr_info(" line discipline installed ");
475 err = download_firmware();
476 if (err != ST_SUCCESS) {
477 pr_err("download firmware failed");
479 } else { /* on success don't retry */
487 /* called from ST Core, on the last un-registration
489 long st_kim_stop(void)
491 long err = ST_SUCCESS;
493 INIT_COMPLETION(kim_gdata->ldisc_installed);
494 #ifndef LEGACY_RFKILL_SUPPORT
495 /* send signal to UIM */
496 err = kill_pid(find_get_pid(kim_gdata->uim_pid), SIGUSR2, 1);
498 pr_err("sending SIGUSR2 to uim failed %ld", err);
499 return ST_ERR_FAILURE;
502 /* set BT rfkill to be blocked */
503 err = rfkill_set_hw_state(kim_gdata->rfkill[ST_BT], 1);
506 /* wait for ldisc to be un-installed */
507 err = wait_for_completion_timeout(&kim_gdata->ldisc_installed,
508 msecs_to_jiffies(LDISC_TIME));
509 if (!err) { /* timeout */
510 pr_err(" timed out waiting for ldisc to be un-installed");
511 return ST_ERR_FAILURE;
514 /* By default configure BT nShutdown to LOW state */
515 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
517 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_HIGH);
519 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
523 /**********************************************************************/
524 /* functions called from subsystems */
526 #ifndef LEGACY_RFKILL_SUPPORT
527 /* called when sysfs entry is written to */
528 static ssize_t store_pid(struct device *dev, struct device_attribute
529 *devattr, char *buf, size_t count)
531 pr_info("%s: pid %s ", __func__, buf);
532 sscanf(buf, "%ld", &kim_gdata->uim_pid);
533 /* to be made use by kim_start to signal SIGUSR2
538 /* called when sysfs entry is read from */
539 static ssize_t show_pid(struct device *dev, struct device_attribute
542 sprintf(buf, "%ld", kim_gdata->uim_pid);
546 /* called when sysfs entry is read from */
547 static ssize_t show_list(struct device *dev, struct device_attribute
550 kim_st_list_protocols(kim_gdata->core_data, buf);
554 #else /* LEGACY_RFKILL_SUPPORT */
556 /* function called from rfkill subsystem, when someone from
557 * user space would write 0/1 on the sysfs entry
558 * /sys/class/rfkill/rfkill0,1,3/state
560 static int kim_toggle_radio(void *data, bool blocked)
562 enum proto_type type = *((enum proto_type *)data);
563 pr_info(" %s: %d ", __func__, type);
572 st_kim_chip_toggle(type, KIM_GPIO_INACTIVE);
574 st_kim_chip_toggle(type, KIM_GPIO_ACTIVE);
577 pr_err(" wrong proto type ");
583 #endif /* LEGACY_RFKILL_SUPPORT */
585 void st_kim_ref(struct st_data_s **core_data)
587 *core_data = kim_gdata->core_data;
590 /**********************************************************************/
591 /* functions called from platform device driver subsystem
592 * need to have a relevant platform device entry in the platform's
596 static int kim_probe(struct platform_device *pdev)
600 long *gpios = pdev->dev.platform_data;
602 status = st_core_init(&kim_gdata->core_data);
604 pr_err(" ST core init failed");
605 return ST_ERR_FAILURE;
608 for (proto = 0; proto < ST_MAX; proto++) {
609 kim_gdata->gpios[proto] = gpios[proto];
610 pr_info(" %ld gpio to be requested", gpios[proto]);
613 for (proto = 0; (proto < ST_MAX) && (gpios[proto] != -1); proto++) {
614 /* Claim the Bluetooth/FM/GPIO
615 * nShutdown gpio from the system
617 status = gpio_request(gpios[proto], "kim");
618 if (unlikely(status)) {
619 pr_err(" gpio %ld request failed ", gpios[proto]);
622 if (gpios[proto] != -1)
623 gpio_free(gpios[proto]);
628 /* Configure nShutdown GPIO as output=0 */
630 gpio_direction_output(gpios[proto], 0);
631 if (unlikely(status)) {
632 pr_err(" unable to configure gpio %ld",
636 if (gpios[proto] != -1)
637 gpio_free(gpios[proto]);
642 #ifndef LEGACY_RFKILL_SUPPORT
643 /* pdev to contain BT, FM and GPS enable/N-Shutdown GPIOs
644 * execute request_gpio, set output direction
646 kim_gdata->kim_kobj = kobject_create_and_add("uim", NULL);
647 /* create the sysfs entry for UIM to put in pid */
648 if (sysfs_create_group(kim_gdata->kim_kobj, &uim_attr_grp)) {
649 pr_err(" sysfs entry creation failed");
650 kobject_put(kim_gdata->kim_kobj);
651 /* free requested GPIOs and fail probe */
652 for (proto = ST_BT; proto < ST_MAX; proto++) {
653 if (gpios[proto] != -1)
654 gpio_free(gpios[proto]);
656 return -1; /* fail insmod */
658 pr_info(" sysfs entry created ");
660 /* get reference of pdev for request_firmware
662 kim_gdata->kim_pdev = pdev;
663 init_completion(&kim_gdata->kim_rcvd);
664 init_completion(&kim_gdata->ldisc_installed);
665 #ifdef LEGACY_RFKILL_SUPPORT
666 for (proto = 0; (proto < ST_MAX) && (gpios[proto] != -1); proto++) {
667 /* TODO: should all types be rfkill_type_bt ? */
668 kim_gdata->rf_protos[proto] = proto;
669 kim_gdata->rfkill[proto] = rfkill_alloc(protocol_names[proto],
670 &pdev->dev, RFKILL_TYPE_BLUETOOTH,
671 &kim_rfkill_ops, &kim_gdata->rf_protos[proto]);
672 if (kim_gdata->rfkill[proto] == NULL) {
673 pr_err("cannot create rfkill entry for gpio %ld",
677 /* block upon creation */
678 rfkill_init_sw_state(kim_gdata->rfkill[proto], 1);
679 status = rfkill_register(kim_gdata->rfkill[proto]);
680 if (unlikely(status)) {
681 pr_err("rfkill registration failed for gpio %ld",
683 rfkill_unregister(kim_gdata->rfkill[proto]);
686 pr_info("rfkill entry created for %ld", gpios[proto]);
692 static int kim_remove(struct platform_device *pdev)
694 /* free the GPIOs requested
696 long *gpios = pdev->dev.platform_data;
699 for (proto = 0; (proto < ST_MAX) && (gpios[proto] != -1); proto++) {
700 /* Claim the Bluetooth/FM/GPIO
701 * nShutdown gpio from the system
703 gpio_free(gpios[proto]);
704 #ifdef LEGACY_RFKILL_SUPPORT
705 rfkill_unregister(kim_gdata->rfkill[proto]);
706 rfkill_destroy(kim_gdata->rfkill[proto]);
707 kim_gdata->rfkill[proto] = NULL;
710 pr_info("kim: GPIO Freed");
711 #ifndef LEGACY_RFKILL_SUPPORT
712 /* delete the sysfs entries */
713 sysfs_remove_group(kim_gdata->kim_kobj, &uim_attr_grp);
714 kobject_put(kim_gdata->kim_kobj);
716 kim_gdata->kim_pdev = NULL;
717 st_core_exit(kim_gdata->core_data);
721 /**********************************************************************/
722 /* entry point for ST KIM module, called in from ST Core */
724 static int __init st_kim_init(void)
726 long ret = ST_SUCCESS;
727 kim_gdata = kzalloc(sizeof(struct kim_data_s), GFP_ATOMIC);
729 pr_err("no mem to allocate");
733 ret = platform_driver_register(&kim_platform_driver);
735 pr_err("platform drv registration failed");
736 return ST_ERR_FAILURE;
741 static void __exit st_kim_deinit(void)
743 /* the following returns void */
744 platform_driver_unregister(&kim_platform_driver);
750 module_init(st_kim_init);
751 module_exit(st_kim_deinit);
752 MODULE_AUTHOR("Pavan Savoy <pavan_savoy@ti.com>");
753 MODULE_DESCRIPTION("Shared Transport Driver for TI BT/FM/GPS combo chips ");
754 MODULE_LICENSE("GPL");