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"),
76 /**********************************************************************/
77 /* internal functions */
80 * validate_firmware_response -
81 * function to return whether the firmware response was proper
82 * in case of error don't complete so that waiting for proper
85 void validate_firmware_response(struct kim_data_s
*kim_gdata
)
87 struct sk_buff
*skb
= kim_gdata
->rx_skb
;
88 if (unlikely(skb
->data
[5] != 0)) {
89 pr_err("no proper response during fw download");
90 pr_err("data6 %x", skb
->data
[5]);
91 return; /* keep waiting for the proper response */
93 /* becos of all the script being downloaded */
94 complete_all(&kim_gdata
->kim_rcvd
);
98 /* check for data len received inside kim_int_recv
99 * most often hit the last case to update state to waiting for data
101 static inline int kim_check_data_len(struct kim_data_s
*kim_gdata
, int len
)
103 register int room
= skb_tailroom(kim_gdata
->rx_skb
);
105 pr_debug("len %d room %d", len
, room
);
108 validate_firmware_response(kim_gdata
);
109 } else if (len
> room
) {
110 /* Received packet's payload length is larger.
111 * We can't accommodate it in created skb.
113 pr_err("Data length is too large len %d room %d", len
,
115 kfree_skb(kim_gdata
->rx_skb
);
117 /* Packet header has non-zero payload length and
118 * we have enough space in created skb. Lets read
120 kim_gdata
->rx_state
= ST_BT_W4_DATA
;
121 kim_gdata
->rx_count
= len
;
125 /* Change ST LL state to continue to process next
127 kim_gdata
->rx_state
= ST_W4_PACKET_TYPE
;
128 kim_gdata
->rx_skb
= NULL
;
129 kim_gdata
->rx_count
= 0;
135 * kim_int_recv - receive function called during firmware download
136 * firmware download responses on different UART drivers
137 * have been observed to come in bursts of different
138 * tty_receive and hence the logic
140 void kim_int_recv(struct kim_data_s
*kim_gdata
,
141 const unsigned char *data
, long count
)
144 struct hci_event_hdr
*eh
;
145 register int len
= 0, type
= 0;
147 pr_debug("%s", __func__
);
148 /* Decode received bytes here */
150 if (unlikely(ptr
== NULL
)) {
151 pr_err(" received null from TTY ");
155 if (kim_gdata
->rx_count
) {
156 len
= min_t(unsigned int, kim_gdata
->rx_count
, count
);
157 memcpy(skb_put(kim_gdata
->rx_skb
, len
), ptr
, len
);
158 kim_gdata
->rx_count
-= len
;
162 if (kim_gdata
->rx_count
)
165 /* Check ST RX state machine , where are we? */
166 switch (kim_gdata
->rx_state
) {
167 /* Waiting for complete packet ? */
169 pr_debug("Complete pkt received");
170 validate_firmware_response(kim_gdata
);
171 kim_gdata
->rx_state
= ST_W4_PACKET_TYPE
;
172 kim_gdata
->rx_skb
= NULL
;
174 /* Waiting for Bluetooth event header ? */
175 case ST_BT_W4_EVENT_HDR
:
176 eh
= (struct hci_event_hdr
*)kim_gdata
->
178 pr_debug("Event header: evt 0x%2.2x"
179 "plen %d", eh
->evt
, eh
->plen
);
180 kim_check_data_len(kim_gdata
, eh
->plen
);
182 } /* end of switch */
183 } /* end of if rx_state */
185 /* Bluetooth event packet? */
187 pr_info("Event packet");
188 kim_gdata
->rx_state
= ST_BT_W4_EVENT_HDR
;
189 kim_gdata
->rx_count
= HCI_EVENT_HDR_SIZE
;
190 type
= HCI_EVENT_PKT
;
193 pr_info("unknown packet");
201 bt_skb_alloc(HCI_MAX_FRAME_SIZE
, GFP_ATOMIC
);
202 if (!kim_gdata
->rx_skb
) {
203 pr_err("can't allocate mem for new packet");
204 kim_gdata
->rx_state
= ST_W4_PACKET_TYPE
;
205 kim_gdata
->rx_count
= 0;
208 bt_cb(kim_gdata
->rx_skb
)->pkt_type
= type
;
210 pr_info("done %s", __func__
);
214 static long read_local_version(struct kim_data_s
*kim_gdata
, char *bts_scr_name
)
216 unsigned short version
= 0, chip
= 0, min_ver
= 0, maj_ver
= 0;
217 char read_ver_cmd
[] = { 0x01, 0x01, 0x10, 0x00 };
219 pr_debug("%s", __func__
);
221 INIT_COMPLETION(kim_gdata
->kim_rcvd
);
222 if (4 != st_int_write(kim_gdata
->core_data
, read_ver_cmd
, 4)) {
223 pr_err("kim: couldn't write 4 bytes");
227 if (!wait_for_completion_timeout
228 (&kim_gdata
->kim_rcvd
, msecs_to_jiffies(CMD_RESP_TIME
))) {
229 pr_err(" waiting for ver info- timed out ");
234 MAKEWORD(kim_gdata
->resp_buffer
[13],
235 kim_gdata
->resp_buffer
[14]);
236 chip
= (version
& 0x7C00) >> 10;
237 min_ver
= (version
& 0x007F);
238 maj_ver
= (version
& 0x0380) >> 7;
240 if (version
& 0x8000)
243 sprintf(bts_scr_name
, "TIInit_%d.%d.%d.bts", chip
, maj_ver
, min_ver
);
245 /* to be accessed later via sysfs entry */
246 kim_gdata
->version
.full
= version
;
247 kim_gdata
->version
.chip
= chip
;
248 kim_gdata
->version
.maj_ver
= maj_ver
;
249 kim_gdata
->version
.min_ver
= min_ver
;
251 pr_info("%s", bts_scr_name
);
256 * download_firmware -
257 * internal function which parses through the .bts firmware
258 * script file intreprets SEND, DELAY actions only as of now
260 static long download_firmware(struct kim_data_s
*kim_gdata
)
264 register unsigned char *ptr
= NULL
;
265 register unsigned char *action_ptr
= NULL
;
266 unsigned char bts_scr_name
[30] = { 0 }; /* 30 char long bts scr name? */
268 err
= read_local_version(kim_gdata
, bts_scr_name
);
270 pr_err("kim: failed to read local ver");
274 request_firmware(&kim_gdata
->fw_entry
, bts_scr_name
,
275 &kim_gdata
->kim_pdev
->dev
);
276 if (unlikely((err
!= 0) || (kim_gdata
->fw_entry
->data
== NULL
) ||
277 (kim_gdata
->fw_entry
->size
== 0))) {
278 pr_err(" request_firmware failed(errno %ld) for %s", err
,
282 ptr
= (void *)kim_gdata
->fw_entry
->data
;
283 len
= kim_gdata
->fw_entry
->size
;
284 /* bts_header to remove out magic number and
287 ptr
+= sizeof(struct bts_header
);
288 len
-= sizeof(struct bts_header
);
290 while (len
> 0 && ptr
) {
291 pr_debug(" action size %d, type %d ",
292 ((struct bts_action
*)ptr
)->size
,
293 ((struct bts_action
*)ptr
)->type
);
295 switch (((struct bts_action
*)ptr
)->type
) {
296 case ACTION_SEND_COMMAND
: /* action send */
297 action_ptr
= &(((struct bts_action
*)ptr
)->data
[0]);
299 (((struct hci_command
*)action_ptr
)->opcode
==
301 /* ignore remote change
302 * baud rate HCI VS command */
304 (" change remote baud"
305 " rate command in firmware");
309 INIT_COMPLETION(kim_gdata
->kim_rcvd
);
310 err
= st_int_write(kim_gdata
->core_data
,
311 ((struct bts_action_send
*)action_ptr
)->data
,
312 ((struct bts_action
*)ptr
)->size
);
313 if (unlikely(err
< 0)) {
314 release_firmware(kim_gdata
->fw_entry
);
317 if (!wait_for_completion_timeout
318 (&kim_gdata
->kim_rcvd
,
319 msecs_to_jiffies(CMD_RESP_TIME
))) {
321 (" response timeout during fw download ");
323 release_firmware(kim_gdata
->fw_entry
);
327 case ACTION_DELAY
: /* sleep */
328 pr_info("sleep command in scr");
329 action_ptr
= &(((struct bts_action
*)ptr
)->data
[0]);
330 mdelay(((struct bts_action_delay
*)action_ptr
)->msec
);
334 len
- (sizeof(struct bts_action
) +
335 ((struct bts_action
*)ptr
)->size
);
337 ptr
+ sizeof(struct bts_action
) +
338 ((struct bts_action
*)ptr
)->size
;
340 /* fw download complete */
341 release_firmware(kim_gdata
->fw_entry
);
345 /**********************************************************************/
346 /* functions called from ST core */
347 /* function to toggle the GPIO
348 * needs to know whether the GPIO is active high or active low
350 void st_kim_chip_toggle(enum proto_type type
, enum kim_gpio_state state
)
352 struct platform_device
*kim_pdev
;
353 struct kim_data_s
*kim_gdata
;
354 pr_info(" %s ", __func__
);
356 kim_pdev
= st_get_plat_device();
357 kim_gdata
= dev_get_drvdata(&kim_pdev
->dev
);
359 if (kim_gdata
->gpios
[type
] == -1) {
360 pr_info(" gpio not requested for protocol %s",
361 protocol_names
[type
]);
370 if (state
== KIM_GPIO_ACTIVE
)
371 gpio_set_value(kim_gdata
->gpios
[ST_FM
], GPIO_LOW
);
373 gpio_set_value(kim_gdata
->gpios
[ST_FM
], GPIO_HIGH
);
377 if (state
== KIM_GPIO_ACTIVE
)
378 gpio_set_value(kim_gdata
->gpios
[ST_GPS
], GPIO_HIGH
);
380 gpio_set_value(kim_gdata
->gpios
[ST_GPS
], GPIO_LOW
);
391 /* called from ST Core, when REG_IN_PROGRESS (registration in progress)
393 * 1. response to read local version
394 * 2. during send/recv's of firmware download
396 void st_kim_recv(void *disc_data
, const unsigned char *data
, long count
)
398 struct st_data_s
*st_gdata
= (struct st_data_s
*)disc_data
;
399 struct kim_data_s
*kim_gdata
= st_gdata
->kim_data
;
401 pr_info(" %s ", __func__
);
402 /* copy to local buffer */
403 if (unlikely(data
[4] == 0x01 && data
[5] == 0x10 && data
[0] == 0x04)) {
404 /* must be the read_ver_cmd */
405 memcpy(kim_gdata
->resp_buffer
, data
, count
);
406 complete_all(&kim_gdata
->kim_rcvd
);
409 kim_int_recv(kim_gdata
, data
, count
);
410 /* either completes or times out */
415 /* to signal completion of line discipline installation
416 * called from ST Core, upon tty_open
418 void st_kim_complete(void *kim_data
)
420 struct kim_data_s
*kim_gdata
= (struct kim_data_s
*)kim_data
;
421 complete(&kim_gdata
->ldisc_installed
);
425 * st_kim_start - called from ST Core upon 1st registration
426 * This involves toggling the chip enable gpio, reading
427 * the firmware version from chip, forming the fw file name
428 * based on the chip version, requesting the fw, parsing it
429 * and perform download(send/recv).
431 long st_kim_start(void *kim_data
)
434 long retry
= POR_RETRY_COUNT
;
435 struct kim_data_s
*kim_gdata
= (struct kim_data_s
*)kim_data
;
437 pr_info(" %s", __func__
);
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);
445 /* Configure BT nShutdown to HIGH state */
446 gpio_set_value(kim_gdata
->gpios
[ST_BT
], GPIO_LOW
);
447 mdelay(5); /* FIXME: a proper toggle */
448 gpio_set_value(kim_gdata
->gpios
[ST_BT
], GPIO_HIGH
);
450 /* re-initialize the completion */
451 INIT_COMPLETION(kim_gdata
->ldisc_installed
);
452 #if 0 /* older way of signalling user-space UIM */
453 /* send signal to UIM */
454 err
= kill_pid(find_get_pid(kim_gdata
->uim_pid
), SIGUSR2
, 0);
456 pr_info(" sending SIGUSR2 to uim failed %ld", err
);
461 /* unblock and send event to UIM via /dev/rfkill */
462 rfkill_set_hw_state(kim_gdata
->rfkill
[ST_BT
], 0);
463 /* wait for ldisc to be installed */
464 err
= wait_for_completion_timeout(&kim_gdata
->ldisc_installed
,
465 msecs_to_jiffies(LDISC_TIME
));
466 if (!err
) { /* timeout */
467 pr_err("line disc installation timed out ");
471 /* ldisc installed now */
472 pr_info(" line discipline installed ");
473 err
= download_firmware(kim_gdata
);
475 pr_err("download firmware failed");
477 } else { /* on success don't retry */
486 * st_kim_stop - called from ST Core, on the last un-registration
487 * toggle low the chip enable gpio
489 long st_kim_stop(void *kim_data
)
492 struct kim_data_s
*kim_gdata
= (struct kim_data_s
*)kim_data
;
494 INIT_COMPLETION(kim_gdata
->ldisc_installed
);
495 #if 0 /* older way of signalling user-space UIM */
496 /* send signal to UIM */
497 err
= kill_pid(find_get_pid(kim_gdata
->uim_pid
), SIGUSR2
, 1);
499 pr_err("sending SIGUSR2 to uim failed %ld", err
);
503 /* set BT rfkill to be blocked */
504 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");
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 */
525 /* called when debugfs entry is read from */
527 static int show_version(struct seq_file
*s
, void *unused
)
529 struct kim_data_s
*kim_gdata
= (struct kim_data_s
*)s
->private;
530 seq_printf(s
, "%04X %d.%d.%d\n", kim_gdata
->version
.full
,
531 kim_gdata
->version
.chip
, kim_gdata
->version
.maj_ver
,
532 kim_gdata
->version
.min_ver
);
536 static int show_list(struct seq_file
*s
, void *unused
)
538 struct kim_data_s
*kim_gdata
= (struct kim_data_s
*)s
->private;
539 kim_st_list_protocols(kim_gdata
->core_data
, s
);
543 /* function called from rfkill subsystem, when someone from
544 * user space would write 0/1 on the sysfs entry
545 * /sys/class/rfkill/rfkill0,1,3/state
547 static int kim_toggle_radio(void *data
, bool blocked
)
549 enum proto_type type
= *((enum proto_type
*)data
);
550 pr_debug(" %s: %d ", __func__
, type
);
559 st_kim_chip_toggle(type
, KIM_GPIO_INACTIVE
);
561 st_kim_chip_toggle(type
, KIM_GPIO_ACTIVE
);
564 pr_err(" wrong proto type ");
571 * st_kim_ref - reference the core's data
572 * This references the per-ST platform device in the arch/xx/
574 * This would enable multiple such platform devices to exist
575 * on a given platform
577 void st_kim_ref(struct st_data_s
**core_data
)
579 struct platform_device
*pdev
;
580 struct kim_data_s
*kim_gdata
;
581 /* get kim_gdata reference from platform device */
582 pdev
= st_get_plat_device();
583 kim_gdata
= dev_get_drvdata(&pdev
->dev
);
584 *core_data
= kim_gdata
->core_data
;
587 static int kim_version_open(struct inode
*i
, struct file
*f
)
589 return single_open(f
, show_version
, i
->i_private
);
592 static int kim_list_open(struct inode
*i
, struct file
*f
)
594 return single_open(f
, show_list
, i
->i_private
);
597 static const struct file_operations version_debugfs_fops
= {
599 .open
= kim_version_open
,
602 .release
= single_release
,
604 static const struct file_operations list_debugfs_fops
= {
606 .open
= kim_list_open
,
609 .release
= single_release
,
612 /**********************************************************************/
613 /* functions called from platform device driver subsystem
614 * need to have a relevant platform device entry in the platform's
618 struct dentry
*kim_debugfs_dir
;
619 static int kim_probe(struct platform_device
*pdev
)
623 long *gpios
= pdev
->dev
.platform_data
;
624 struct kim_data_s
*kim_gdata
;
626 kim_gdata
= kzalloc(sizeof(struct kim_data_s
), GFP_ATOMIC
);
628 pr_err("no mem to allocate");
631 dev_set_drvdata(&pdev
->dev
, kim_gdata
);
633 status
= st_core_init(&kim_gdata
->core_data
);
635 pr_err(" ST core init failed");
638 /* refer to itself */
639 kim_gdata
->core_data
->kim_data
= kim_gdata
;
641 for (proto
= 0; proto
< ST_MAX
; proto
++) {
642 kim_gdata
->gpios
[proto
] = gpios
[proto
];
643 pr_info(" %ld gpio to be requested", gpios
[proto
]);
646 for (proto
= 0; (proto
< ST_MAX
) && (gpios
[proto
] != -1); proto
++) {
647 /* Claim the Bluetooth/FM/GPIO
648 * nShutdown gpio from the system
650 status
= gpio_request(gpios
[proto
], "kim");
651 if (unlikely(status
)) {
652 pr_err(" gpio %ld request failed ", gpios
[proto
]);
655 if (gpios
[proto
] != -1)
656 gpio_free(gpios
[proto
]);
661 /* Configure nShutdown GPIO as output=0 */
663 gpio_direction_output(gpios
[proto
], 0);
664 if (unlikely(status
)) {
665 pr_err(" unable to configure gpio %ld",
669 if (gpios
[proto
] != -1)
670 gpio_free(gpios
[proto
]);
675 /* get reference of pdev for request_firmware
677 kim_gdata
->kim_pdev
= pdev
;
678 init_completion(&kim_gdata
->kim_rcvd
);
679 init_completion(&kim_gdata
->ldisc_installed
);
681 for (proto
= 0; (proto
< ST_MAX
) && (gpios
[proto
] != -1); proto
++) {
682 /* TODO: should all types be rfkill_type_bt ? */
683 kim_gdata
->rf_protos
[proto
] = proto
;
684 kim_gdata
->rfkill
[proto
] = rfkill_alloc(protocol_names
[proto
],
685 &pdev
->dev
, RFKILL_TYPE_BLUETOOTH
,
686 &kim_rfkill_ops
, &kim_gdata
->rf_protos
[proto
]);
687 if (kim_gdata
->rfkill
[proto
] == NULL
) {
688 pr_err("cannot create rfkill entry for gpio %ld",
692 /* block upon creation */
693 rfkill_init_sw_state(kim_gdata
->rfkill
[proto
], 1);
694 status
= rfkill_register(kim_gdata
->rfkill
[proto
]);
695 if (unlikely(status
)) {
696 pr_err("rfkill registration failed for gpio %ld",
698 rfkill_unregister(kim_gdata
->rfkill
[proto
]);
701 pr_info("rfkill entry created for %ld", gpios
[proto
]);
704 kim_debugfs_dir
= debugfs_create_dir("ti-st", NULL
);
705 if (IS_ERR(kim_debugfs_dir
)) {
706 pr_err(" debugfs entries creation failed ");
707 kim_debugfs_dir
= NULL
;
711 debugfs_create_file("version", S_IRUGO
, kim_debugfs_dir
,
712 kim_gdata
, &version_debugfs_fops
);
713 debugfs_create_file("protocols", S_IRUGO
, kim_debugfs_dir
,
714 kim_gdata
, &list_debugfs_fops
);
715 pr_info(" debugfs entries created ");
719 static int kim_remove(struct platform_device
*pdev
)
721 /* free the GPIOs requested
723 long *gpios
= pdev
->dev
.platform_data
;
725 struct kim_data_s
*kim_gdata
;
727 kim_gdata
= dev_get_drvdata(&pdev
->dev
);
729 for (proto
= 0; (proto
< ST_MAX
) && (gpios
[proto
] != -1); proto
++) {
730 /* Claim the Bluetooth/FM/GPIO
731 * nShutdown gpio from the system
733 gpio_free(gpios
[proto
]);
734 rfkill_unregister(kim_gdata
->rfkill
[proto
]);
735 rfkill_destroy(kim_gdata
->rfkill
[proto
]);
736 kim_gdata
->rfkill
[proto
] = NULL
;
738 pr_info("kim: GPIO Freed");
739 debugfs_remove_recursive(kim_debugfs_dir
);
740 kim_gdata
->kim_pdev
= NULL
;
741 st_core_exit(kim_gdata
->core_data
);
748 /**********************************************************************/
749 /* entry point for ST KIM module, called in from ST Core */
751 static int __init
st_kim_init(void)
754 ret
= platform_driver_register(&kim_platform_driver
);
756 pr_err("platform drv registration failed");
762 static void __exit
st_kim_deinit(void)
764 /* the following returns void */
765 platform_driver_unregister(&kim_platform_driver
);
769 module_init(st_kim_init
);
770 module_exit(st_kim_deinit
);
771 MODULE_AUTHOR("Pavan Savoy <pavan_savoy@ti.com>");
772 MODULE_DESCRIPTION("Shared Transport Driver for TI BT/FM/GPS combo chips ");
773 MODULE_LICENSE("GPL");