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 static ssize_t
show_pid(struct device
*dev
, struct device_attribute
60 static ssize_t
store_pid(struct device
*dev
, struct device_attribute
61 *devattr
, char *buf
, size_t count
);
62 static ssize_t
show_list(struct device
*dev
, struct device_attribute
64 static ssize_t
show_version(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 kobj_attribute chip_version
=
74 __ATTR(version
, 0444, (void *)show_version
, NULL
);
76 static struct attribute
*uim_attrs
[] = {
78 /* add more debug sysfs entries */
84 static struct attribute_group uim_attr_grp
= {
88 static int kim_toggle_radio(void*, bool);
89 static const struct rfkill_ops kim_rfkill_ops
= {
90 .set_block
= kim_toggle_radio
,
93 /* strings to be used for rfkill entries and by
94 * ST Core to be used for sysfs debug entry
96 #define PROTO_ENTRY(type, name) name
97 const unsigned char *protocol_names
[] = {
98 PROTO_ENTRY(ST_BT
, "Bluetooth"),
99 PROTO_ENTRY(ST_FM
, "FM"),
100 PROTO_ENTRY(ST_GPS
, "GPS"),
104 /**********************************************************************/
105 /* internal functions */
108 * function to return whether the firmware response was proper
109 * in case of error don't complete so that waiting for proper
112 void validate_firmware_response(struct kim_data_s
*kim_gdata
)
114 struct sk_buff
*skb
= kim_gdata
->rx_skb
;
115 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 */
120 /* becos of all the script being downloaded */
121 complete_all(&kim_gdata
->kim_rcvd
);
125 /* check for data len received inside kim_int_recv
126 * most often hit the last case to update state to waiting for data
128 static inline int kim_check_data_len(struct kim_data_s
*kim_gdata
, int len
)
130 register int room
= skb_tailroom(kim_gdata
->rx_skb
);
132 pr_info("len %d room %d", len
, room
);
135 validate_firmware_response(kim_gdata
);
136 } else if (len
> room
) {
137 /* Received packet's payload length is larger.
138 * We can't accommodate it in created skb.
140 pr_err("Data length is too large len %d room %d", len
,
142 kfree_skb(kim_gdata
->rx_skb
);
144 /* Packet header has non-zero payload length and
145 * we have enough space in created skb. Lets read
147 kim_gdata
->rx_state
= ST_BT_W4_DATA
;
148 kim_gdata
->rx_count
= len
;
152 /* Change ST LL state to continue to process next
154 kim_gdata
->rx_state
= ST_W4_PACKET_TYPE
;
155 kim_gdata
->rx_skb
= NULL
;
156 kim_gdata
->rx_count
= 0;
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
166 void kim_int_recv(struct kim_data_s
*kim_gdata
,
167 const unsigned char *data
, long count
)
170 struct hci_event_hdr
*eh
;
171 register int len
= 0, type
= 0;
173 pr_info("%s", __func__
);
174 /* Decode received bytes here */
176 if (unlikely(ptr
== NULL
)) {
177 pr_err(" received null from TTY ");
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
;
188 if (kim_gdata
->rx_count
)
191 /* Check ST RX state machine , where are we? */
192 switch (kim_gdata
->rx_state
) {
193 /* Waiting for complete packet ? */
195 pr_info("Complete pkt received");
196 validate_firmware_response(kim_gdata
);
197 kim_gdata
->rx_state
= ST_W4_PACKET_TYPE
;
198 kim_gdata
->rx_skb
= NULL
;
200 /* Waiting for Bluetooth event header ? */
201 case ST_BT_W4_EVENT_HDR
:
202 eh
= (struct hci_event_hdr
*)kim_gdata
->
204 pr_info("Event header: evt 0x%2.2x"
205 "plen %d", eh
->evt
, eh
->plen
);
206 kim_check_data_len(kim_gdata
, eh
->plen
);
208 } /* end of switch */
209 } /* end of if rx_state */
211 /* Bluetooth event packet? */
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
;
219 pr_info("unknown packet");
223 } /* end of switch *ptr */
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;
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__
);
240 static long read_local_version(struct kim_data_s
*kim_gdata
, char *bts_scr_name
)
242 unsigned short version
= 0, chip
= 0, min_ver
= 0, maj_ver
= 0;
243 char read_ver_cmd
[] = { 0x01, 0x01, 0x10, 0x00 };
245 pr_info("%s", __func__
);
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");
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 ");
260 MAKEWORD(kim_gdata
->resp_buffer
[13],
261 kim_gdata
->resp_buffer
[14]);
262 chip
= (version
& 0x7C00) >> 10;
263 min_ver
= (version
& 0x007F);
264 maj_ver
= (version
& 0x0380) >> 7;
266 if (version
& 0x8000)
269 sprintf(bts_scr_name
, "TIInit_%d.%d.%d.bts", chip
, maj_ver
, min_ver
);
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
;
277 pr_info("%s", bts_scr_name
);
281 /* internal function which parses through the .bts firmware script file
282 * intreprets SEND, DELAY actions only as of now
284 static long download_firmware(struct kim_data_s
*kim_gdata
)
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? */
292 pr_info("%s", __func__
);
294 err
= read_local_version(kim_gdata
, bts_scr_name
);
296 pr_err("kim: failed to read local ver");
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
,
308 ptr
= (void *)kim_gdata
->fw_entry
->data
;
309 len
= kim_gdata
->fw_entry
->size
;
310 /* bts_header to remove out magic number and
313 ptr
+= sizeof(struct bts_header
);
314 len
-= sizeof(struct bts_header
);
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
);
321 switch (((struct bts_action
*)ptr
)->type
) {
322 case ACTION_SEND_COMMAND
: /* action send */
323 action_ptr
= &(((struct bts_action
*)ptr
)->data
[0]);
325 (((struct hci_command
*)action_ptr
)->opcode
==
327 /* ignore remote change
328 * baud rate HCI VS command */
330 (" change remote baud\
331 rate command in firmware");
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
);
343 if (!wait_for_completion_timeout
344 (&kim_gdata
->kim_rcvd
,
345 msecs_to_jiffies(CMD_RESP_TIME
))) {
347 (" response timeout during fw download ");
349 release_firmware(kim_gdata
->fw_entry
);
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
);
360 len
- (sizeof(struct bts_action
) +
361 ((struct bts_action
*)ptr
)->size
);
363 ptr
+ sizeof(struct bts_action
) +
364 ((struct bts_action
*)ptr
)->size
;
366 /* fw download complete */
367 release_firmware(kim_gdata
->fw_entry
);
371 /**********************************************************************/
372 /* functions called from ST core */
373 /* function to toggle the GPIO
374 * needs to know whether the GPIO is active high or active low
376 void st_kim_chip_toggle(enum proto_type type
, enum kim_gpio_state state
)
378 struct platform_device
*kim_pdev
;
379 struct kim_data_s
*kim_gdata
;
380 pr_info(" %s ", __func__
);
382 kim_pdev
= st_get_plat_device();
383 kim_gdata
= dev_get_drvdata(&kim_pdev
->dev
);
385 if (kim_gdata
->gpios
[type
] == -1) {
386 pr_info(" gpio not requested for protocol %s",
387 protocol_names
[type
]);
396 if (state
== KIM_GPIO_ACTIVE
)
397 gpio_set_value(kim_gdata
->gpios
[ST_FM
], GPIO_LOW
);
399 gpio_set_value(kim_gdata
->gpios
[ST_FM
], GPIO_HIGH
);
403 if (state
== KIM_GPIO_ACTIVE
)
404 gpio_set_value(kim_gdata
->gpios
[ST_GPS
], GPIO_HIGH
);
406 gpio_set_value(kim_gdata
->gpios
[ST_GPS
], GPIO_LOW
);
417 /* called from ST Core, when REG_IN_PROGRESS (registration in progress)
419 * 1. response to read local version
420 * 2. during send/recv's of firmware download
422 void st_kim_recv(void *disc_data
, const unsigned char *data
, long count
)
424 struct st_data_s
*st_gdata
= (struct st_data_s
*)disc_data
;
425 struct kim_data_s
*kim_gdata
= st_gdata
->kim_data
;
427 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
);
435 kim_int_recv(kim_gdata
, data
, count
);
436 /* either completes or times out */
441 /* to signal completion of line discipline installation
442 * called from ST Core, upon tty_open
444 void st_kim_complete(void *kim_data
)
446 struct kim_data_s
*kim_gdata
= (struct kim_data_s
*)kim_data
;
447 complete(&kim_gdata
->ldisc_installed
);
450 /* called from ST Core upon 1st registration
452 long st_kim_start(void *kim_data
)
455 long retry
= POR_RETRY_COUNT
;
456 struct kim_data_s
*kim_gdata
= (struct kim_data_s
*)kim_data
;
458 pr_info(" %s", __func__
);
461 /* TODO: this is only because rfkill sub-system
462 * doesn't send events to user-space if the state
465 rfkill_set_hw_state(kim_gdata
->rfkill
[ST_BT
], 1);
466 /* 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
);
471 /* re-initialize the completion */
472 INIT_COMPLETION(kim_gdata
->ldisc_installed
);
473 #if 0 /* older way of signalling user-space UIM */
474 /* send signal to UIM */
475 err
= kill_pid(find_get_pid(kim_gdata
->uim_pid
), SIGUSR2
, 0);
477 pr_info(" sending SIGUSR2 to uim failed %ld", err
);
482 /* unblock and send event to UIM via /dev/rfkill */
483 rfkill_set_hw_state(kim_gdata
->rfkill
[ST_BT
], 0);
484 /* 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 ");
492 /* ldisc installed now */
493 pr_info(" line discipline installed ");
494 err
= download_firmware(kim_gdata
);
496 pr_err("download firmware failed");
498 } else { /* on success don't retry */
506 /* called from ST Core, on the last un-registration
508 long st_kim_stop(void *kim_data
)
511 struct kim_data_s
*kim_gdata
= (struct kim_data_s
*)kim_data
;
513 INIT_COMPLETION(kim_gdata
->ldisc_installed
);
514 #if 0 /* older way of signalling user-space UIM */
515 /* send signal to UIM */
516 err
= kill_pid(find_get_pid(kim_gdata
->uim_pid
), SIGUSR2
, 1);
518 pr_err("sending SIGUSR2 to uim failed %ld", err
);
522 /* set BT rfkill to be blocked */
523 err
= rfkill_set_hw_state(kim_gdata
->rfkill
[ST_BT
], 1);
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");
533 /* By default configure BT nShutdown to LOW state */
534 gpio_set_value(kim_gdata
->gpios
[ST_BT
], GPIO_LOW
);
536 gpio_set_value(kim_gdata
->gpios
[ST_BT
], GPIO_HIGH
);
538 gpio_set_value(kim_gdata
->gpios
[ST_BT
], GPIO_LOW
);
542 /**********************************************************************/
543 /* functions called from subsystems */
544 /* called when sysfs entry is read from */
546 static ssize_t
show_version(struct device
*dev
, struct device_attribute
549 struct kim_data_s
*kim_gdata
= dev_get_drvdata(dev
);
550 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
);
556 /* called when sysfs entry is written to */
557 static ssize_t
store_pid(struct device
*dev
, struct device_attribute
558 *devattr
, char *buf
, size_t count
)
560 struct kim_data_s
*kim_gdata
= dev_get_drvdata(dev
);
561 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
568 /* called when sysfs entry is read from */
569 static ssize_t
show_pid(struct device
*dev
, struct device_attribute
572 struct kim_data_s
*kim_gdata
= dev_get_drvdata(dev
);
573 sprintf(buf
, "%ld", kim_gdata
->uim_pid
);
577 /* called when sysfs entry is read from */
578 static ssize_t
show_list(struct device
*dev
, struct device_attribute
581 struct kim_data_s
*kim_gdata
= dev_get_drvdata(dev
);
582 kim_st_list_protocols(kim_gdata
->core_data
, buf
);
586 /* 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
590 static int kim_toggle_radio(void *data
, bool blocked
)
592 enum proto_type type
= *((enum proto_type
*)data
);
593 pr_info(" %s: %d ", __func__
, type
);
602 st_kim_chip_toggle(type
, KIM_GPIO_INACTIVE
);
604 st_kim_chip_toggle(type
, KIM_GPIO_ACTIVE
);
607 pr_err(" wrong proto type ");
613 void st_kim_ref(struct st_data_s
**core_data
)
615 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
);
620 *core_data
= kim_gdata
->core_data
;
623 /**********************************************************************/
624 /* functions called from platform device driver subsystem
625 * need to have a relevant platform device entry in the platform's
629 static int kim_probe(struct platform_device
*pdev
)
633 long *gpios
= pdev
->dev
.platform_data
;
634 struct kim_data_s
*kim_gdata
;
636 kim_gdata
= kzalloc(sizeof(struct kim_data_s
), GFP_ATOMIC
);
638 pr_err("no mem to allocate");
641 dev_set_drvdata(&pdev
->dev
, kim_gdata
);
643 status
= st_core_init(&kim_gdata
->core_data
);
645 pr_err(" ST core init failed");
648 /* refer to itself */
649 kim_gdata
->core_data
->kim_data
= kim_gdata
;
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
]);
656 for (proto
= 0; (proto
< ST_MAX
) && (gpios
[proto
] != -1); proto
++) {
657 /* Claim the Bluetooth/FM/GPIO
658 * nShutdown gpio from the system
660 status
= gpio_request(gpios
[proto
], "kim");
661 if (unlikely(status
)) {
662 pr_err(" gpio %ld request failed ", gpios
[proto
]);
665 if (gpios
[proto
] != -1)
666 gpio_free(gpios
[proto
]);
671 /* Configure nShutdown GPIO as output=0 */
673 gpio_direction_output(gpios
[proto
], 0);
674 if (unlikely(status
)) {
675 pr_err(" unable to configure gpio %ld",
679 if (gpios
[proto
] != -1)
680 gpio_free(gpios
[proto
]);
685 /* get reference of pdev for request_firmware
687 kim_gdata
->kim_pdev
= pdev
;
688 init_completion(&kim_gdata
->kim_rcvd
);
689 init_completion(&kim_gdata
->ldisc_installed
);
691 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",
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",
708 rfkill_unregister(kim_gdata
->rfkill
[proto
]);
711 pr_info("rfkill entry created for %ld", gpios
[proto
]);
714 if (sysfs_create_group(&pdev
->dev
.kobj
, &uim_attr_grp
)) {
715 pr_err(" sysfs entry creation failed");
718 pr_info(" sysfs entries created ");
722 static int kim_remove(struct platform_device
*pdev
)
724 /* free the GPIOs requested
726 long *gpios
= pdev
->dev
.platform_data
;
728 struct kim_data_s
*kim_gdata
;
730 kim_gdata
= dev_get_drvdata(&pdev
->dev
);
732 for (proto
= 0; (proto
< ST_MAX
) && (gpios
[proto
] != -1); proto
++) {
733 /* Claim the Bluetooth/FM/GPIO
734 * nShutdown gpio from the system
736 gpio_free(gpios
[proto
]);
737 rfkill_unregister(kim_gdata
->rfkill
[proto
]);
738 rfkill_destroy(kim_gdata
->rfkill
[proto
]);
739 kim_gdata
->rfkill
[proto
] = NULL
;
741 pr_info("kim: GPIO Freed");
742 sysfs_remove_group(&pdev
->dev
.kobj
, &uim_attr_grp
);
743 kim_gdata
->kim_pdev
= NULL
;
744 st_core_exit(kim_gdata
->core_data
);
751 /**********************************************************************/
752 /* entry point for ST KIM module, called in from ST Core */
754 static int __init
st_kim_init(void)
757 ret
= platform_driver_register(&kim_platform_driver
);
759 pr_err("platform drv registration failed");
765 static void __exit
st_kim_deinit(void)
767 /* the following returns void */
768 platform_driver_unregister(&kim_platform_driver
);
772 module_init(st_kim_init
);
773 module_exit(st_kim_deinit
);
774 MODULE_AUTHOR("Pavan Savoy <pavan_savoy@ti.com>");
775 MODULE_DESCRIPTION("Shared Transport Driver for TI BT/FM/GPS combo chips ");
776 MODULE_LICENSE("GPL");