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 ?
56 static int kim_toggle_radio(void*, bool);
57 static const struct rfkill_ops kim_rfkill_ops
= {
58 .set_block
= kim_toggle_radio
,
61 /* strings to be used for rfkill entries and by
62 * ST Core to be used for sysfs debug entry
64 #define PROTO_ENTRY(type, name) name
65 const unsigned char *protocol_names
[] = {
66 PROTO_ENTRY(ST_BT
, "Bluetooth"),
67 PROTO_ENTRY(ST_FM
, "FM"),
68 PROTO_ENTRY(ST_GPS
, "GPS"),
71 #define MAX_ST_DEVICES 3 /* Imagine 1 on each UART for now */
72 struct platform_device
*st_kim_devices
[MAX_ST_DEVICES
];
74 /**********************************************************************/
75 /* internal functions */
78 * st_get_plat_device -
79 * function which returns the reference to the platform device
80 * requested by id. As of now only 1 such device exists (id=0)
81 * the context requesting for reference can get the id to be
82 * requested by a. The protocol driver which is registering or
83 * b. the tty device which is opened.
85 static struct platform_device
*st_get_plat_device(int id
)
87 return st_kim_devices
[id
];
91 * validate_firmware_response -
92 * function to return whether the firmware response was proper
93 * in case of error don't complete so that waiting for proper
96 void validate_firmware_response(struct kim_data_s
*kim_gdata
)
98 struct sk_buff
*skb
= kim_gdata
->rx_skb
;
99 if (unlikely(skb
->data
[5] != 0)) {
100 pr_err("no proper response during fw download");
101 pr_err("data6 %x", skb
->data
[5]);
102 return; /* keep waiting for the proper response */
104 /* becos of all the script being downloaded */
105 complete_all(&kim_gdata
->kim_rcvd
);
109 /* check for data len received inside kim_int_recv
110 * most often hit the last case to update state to waiting for data
112 static inline int kim_check_data_len(struct kim_data_s
*kim_gdata
, int len
)
114 register int room
= skb_tailroom(kim_gdata
->rx_skb
);
116 pr_debug("len %d room %d", len
, room
);
119 validate_firmware_response(kim_gdata
);
120 } else if (len
> room
) {
121 /* Received packet's payload length is larger.
122 * We can't accommodate it in created skb.
124 pr_err("Data length is too large len %d room %d", len
,
126 kfree_skb(kim_gdata
->rx_skb
);
128 /* Packet header has non-zero payload length and
129 * we have enough space in created skb. Lets read
131 kim_gdata
->rx_state
= ST_BT_W4_DATA
;
132 kim_gdata
->rx_count
= len
;
136 /* Change ST LL state to continue to process next
138 kim_gdata
->rx_state
= ST_W4_PACKET_TYPE
;
139 kim_gdata
->rx_skb
= NULL
;
140 kim_gdata
->rx_count
= 0;
146 * kim_int_recv - receive function called during firmware download
147 * firmware download responses on different UART drivers
148 * have been observed to come in bursts of different
149 * tty_receive and hence the logic
151 void kim_int_recv(struct kim_data_s
*kim_gdata
,
152 const unsigned char *data
, long count
)
155 struct hci_event_hdr
*eh
;
156 register int len
= 0, type
= 0;
158 pr_debug("%s", __func__
);
159 /* Decode received bytes here */
161 if (unlikely(ptr
== NULL
)) {
162 pr_err(" received null from TTY ");
166 if (kim_gdata
->rx_count
) {
167 len
= min_t(unsigned int, kim_gdata
->rx_count
, count
);
168 memcpy(skb_put(kim_gdata
->rx_skb
, len
), ptr
, len
);
169 kim_gdata
->rx_count
-= len
;
173 if (kim_gdata
->rx_count
)
176 /* Check ST RX state machine , where are we? */
177 switch (kim_gdata
->rx_state
) {
178 /* Waiting for complete packet ? */
180 pr_debug("Complete pkt received");
181 validate_firmware_response(kim_gdata
);
182 kim_gdata
->rx_state
= ST_W4_PACKET_TYPE
;
183 kim_gdata
->rx_skb
= NULL
;
185 /* Waiting for Bluetooth event header ? */
186 case ST_BT_W4_EVENT_HDR
:
187 eh
= (struct hci_event_hdr
*)kim_gdata
->
189 pr_debug("Event header: evt 0x%2.2x"
190 "plen %d", eh
->evt
, eh
->plen
);
191 kim_check_data_len(kim_gdata
, eh
->plen
);
193 } /* end of switch */
194 } /* end of if rx_state */
196 /* Bluetooth event packet? */
198 pr_info("Event packet");
199 kim_gdata
->rx_state
= ST_BT_W4_EVENT_HDR
;
200 kim_gdata
->rx_count
= HCI_EVENT_HDR_SIZE
;
201 type
= HCI_EVENT_PKT
;
204 pr_info("unknown packet");
212 bt_skb_alloc(HCI_MAX_FRAME_SIZE
, GFP_ATOMIC
);
213 if (!kim_gdata
->rx_skb
) {
214 pr_err("can't allocate mem for new packet");
215 kim_gdata
->rx_state
= ST_W4_PACKET_TYPE
;
216 kim_gdata
->rx_count
= 0;
219 bt_cb(kim_gdata
->rx_skb
)->pkt_type
= type
;
221 pr_info("done %s", __func__
);
225 static long read_local_version(struct kim_data_s
*kim_gdata
, char *bts_scr_name
)
227 unsigned short version
= 0, chip
= 0, min_ver
= 0, maj_ver
= 0;
228 char read_ver_cmd
[] = { 0x01, 0x01, 0x10, 0x00 };
230 pr_debug("%s", __func__
);
232 INIT_COMPLETION(kim_gdata
->kim_rcvd
);
233 if (4 != st_int_write(kim_gdata
->core_data
, read_ver_cmd
, 4)) {
234 pr_err("kim: couldn't write 4 bytes");
238 if (!wait_for_completion_timeout
239 (&kim_gdata
->kim_rcvd
, msecs_to_jiffies(CMD_RESP_TIME
))) {
240 pr_err(" waiting for ver info- timed out ");
245 MAKEWORD(kim_gdata
->resp_buffer
[13],
246 kim_gdata
->resp_buffer
[14]);
247 chip
= (version
& 0x7C00) >> 10;
248 min_ver
= (version
& 0x007F);
249 maj_ver
= (version
& 0x0380) >> 7;
251 if (version
& 0x8000)
254 sprintf(bts_scr_name
, "TIInit_%d.%d.%d.bts", chip
, maj_ver
, min_ver
);
256 /* to be accessed later via sysfs entry */
257 kim_gdata
->version
.full
= version
;
258 kim_gdata
->version
.chip
= chip
;
259 kim_gdata
->version
.maj_ver
= maj_ver
;
260 kim_gdata
->version
.min_ver
= min_ver
;
262 pr_info("%s", bts_scr_name
);
267 * download_firmware -
268 * internal function which parses through the .bts firmware
269 * script file intreprets SEND, DELAY actions only as of now
271 static long download_firmware(struct kim_data_s
*kim_gdata
)
275 register unsigned char *ptr
= NULL
;
276 register unsigned char *action_ptr
= NULL
;
277 unsigned char bts_scr_name
[30] = { 0 }; /* 30 char long bts scr name? */
279 err
= read_local_version(kim_gdata
, bts_scr_name
);
281 pr_err("kim: failed to read local ver");
285 request_firmware(&kim_gdata
->fw_entry
, bts_scr_name
,
286 &kim_gdata
->kim_pdev
->dev
);
287 if (unlikely((err
!= 0) || (kim_gdata
->fw_entry
->data
== NULL
) ||
288 (kim_gdata
->fw_entry
->size
== 0))) {
289 pr_err(" request_firmware failed(errno %ld) for %s", err
,
293 ptr
= (void *)kim_gdata
->fw_entry
->data
;
294 len
= kim_gdata
->fw_entry
->size
;
295 /* bts_header to remove out magic number and
298 ptr
+= sizeof(struct bts_header
);
299 len
-= sizeof(struct bts_header
);
301 while (len
> 0 && ptr
) {
302 pr_debug(" action size %d, type %d ",
303 ((struct bts_action
*)ptr
)->size
,
304 ((struct bts_action
*)ptr
)->type
);
306 switch (((struct bts_action
*)ptr
)->type
) {
307 case ACTION_SEND_COMMAND
: /* action send */
308 action_ptr
= &(((struct bts_action
*)ptr
)->data
[0]);
310 (((struct hci_command
*)action_ptr
)->opcode
==
312 /* ignore remote change
313 * baud rate HCI VS command */
315 (" change remote baud"
316 " rate command in firmware");
320 INIT_COMPLETION(kim_gdata
->kim_rcvd
);
321 err
= st_int_write(kim_gdata
->core_data
,
322 ((struct bts_action_send
*)action_ptr
)->data
,
323 ((struct bts_action
*)ptr
)->size
);
324 if (unlikely(err
< 0)) {
325 release_firmware(kim_gdata
->fw_entry
);
328 if (!wait_for_completion_timeout
329 (&kim_gdata
->kim_rcvd
,
330 msecs_to_jiffies(CMD_RESP_TIME
))) {
332 (" response timeout during fw download ");
334 release_firmware(kim_gdata
->fw_entry
);
338 case ACTION_DELAY
: /* sleep */
339 pr_info("sleep command in scr");
340 action_ptr
= &(((struct bts_action
*)ptr
)->data
[0]);
341 mdelay(((struct bts_action_delay
*)action_ptr
)->msec
);
345 len
- (sizeof(struct bts_action
) +
346 ((struct bts_action
*)ptr
)->size
);
348 ptr
+ sizeof(struct bts_action
) +
349 ((struct bts_action
*)ptr
)->size
;
351 /* fw download complete */
352 release_firmware(kim_gdata
->fw_entry
);
356 /**********************************************************************/
357 /* functions called from ST core */
358 /* function to toggle the GPIO
359 * needs to know whether the GPIO is active high or active low
361 void st_kim_chip_toggle(enum proto_type type
, enum kim_gpio_state state
)
363 struct platform_device
*kim_pdev
;
364 struct kim_data_s
*kim_gdata
;
365 pr_info(" %s ", __func__
);
367 kim_pdev
= st_get_plat_device(0);
368 kim_gdata
= dev_get_drvdata(&kim_pdev
->dev
);
370 if (kim_gdata
->gpios
[type
] == -1) {
371 pr_info(" gpio not requested for protocol %s",
372 protocol_names
[type
]);
381 if (state
== KIM_GPIO_ACTIVE
)
382 gpio_set_value(kim_gdata
->gpios
[ST_FM
], GPIO_LOW
);
384 gpio_set_value(kim_gdata
->gpios
[ST_FM
], GPIO_HIGH
);
388 if (state
== KIM_GPIO_ACTIVE
)
389 gpio_set_value(kim_gdata
->gpios
[ST_GPS
], GPIO_HIGH
);
391 gpio_set_value(kim_gdata
->gpios
[ST_GPS
], GPIO_LOW
);
402 /* called from ST Core, when REG_IN_PROGRESS (registration in progress)
404 * 1. response to read local version
405 * 2. during send/recv's of firmware download
407 void st_kim_recv(void *disc_data
, const unsigned char *data
, long count
)
409 struct st_data_s
*st_gdata
= (struct st_data_s
*)disc_data
;
410 struct kim_data_s
*kim_gdata
= st_gdata
->kim_data
;
412 pr_info(" %s ", __func__
);
413 /* copy to local buffer */
414 if (unlikely(data
[4] == 0x01 && data
[5] == 0x10 && data
[0] == 0x04)) {
415 /* must be the read_ver_cmd */
416 memcpy(kim_gdata
->resp_buffer
, data
, count
);
417 complete_all(&kim_gdata
->kim_rcvd
);
420 kim_int_recv(kim_gdata
, data
, count
);
421 /* either completes or times out */
426 /* to signal completion of line discipline installation
427 * called from ST Core, upon tty_open
429 void st_kim_complete(void *kim_data
)
431 struct kim_data_s
*kim_gdata
= (struct kim_data_s
*)kim_data
;
432 complete(&kim_gdata
->ldisc_installed
);
436 * st_kim_start - called from ST Core upon 1st registration
437 * This involves toggling the chip enable gpio, reading
438 * the firmware version from chip, forming the fw file name
439 * based on the chip version, requesting the fw, parsing it
440 * and perform download(send/recv).
442 long st_kim_start(void *kim_data
)
445 long retry
= POR_RETRY_COUNT
;
446 struct kim_data_s
*kim_gdata
= (struct kim_data_s
*)kim_data
;
448 pr_info(" %s", __func__
);
451 /* TODO: this is only because rfkill sub-system
452 * doesn't send events to user-space if the state
455 rfkill_set_hw_state(kim_gdata
->rfkill
[ST_BT
], 1);
456 /* Configure BT nShutdown to HIGH state */
457 gpio_set_value(kim_gdata
->gpios
[ST_BT
], GPIO_LOW
);
459 gpio_set_value(kim_gdata
->gpios
[ST_BT
], GPIO_HIGH
);
461 /* re-initialize the completion */
462 INIT_COMPLETION(kim_gdata
->ldisc_installed
);
463 /* unblock and send event to UIM via /dev/rfkill */
464 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 ");
473 /* ldisc installed now */
474 pr_info(" line discipline installed ");
475 err
= download_firmware(kim_gdata
);
477 pr_err("download firmware failed");
479 } else { /* on success don't retry */
488 * st_kim_stop - called from ST Core, on the last un-registration
489 * toggle low the chip enable gpio
491 long st_kim_stop(void *kim_data
)
494 struct kim_data_s
*kim_gdata
= (struct kim_data_s
*)kim_data
;
496 INIT_COMPLETION(kim_gdata
->ldisc_installed
);
497 /* set BT rfkill to be blocked */
498 err
= rfkill_set_hw_state(kim_gdata
->rfkill
[ST_BT
], 1);
500 /* wait for ldisc to be un-installed */
501 err
= wait_for_completion_timeout(&kim_gdata
->ldisc_installed
,
502 msecs_to_jiffies(LDISC_TIME
));
503 if (!err
) { /* timeout */
504 pr_err(" timed out waiting for ldisc to be un-installed");
508 /* By default configure BT nShutdown to LOW state */
509 gpio_set_value(kim_gdata
->gpios
[ST_BT
], GPIO_LOW
);
511 gpio_set_value(kim_gdata
->gpios
[ST_BT
], GPIO_HIGH
);
513 gpio_set_value(kim_gdata
->gpios
[ST_BT
], GPIO_LOW
);
517 /**********************************************************************/
518 /* functions called from subsystems */
519 /* called when debugfs entry is read from */
521 static int show_version(struct seq_file
*s
, void *unused
)
523 struct kim_data_s
*kim_gdata
= (struct kim_data_s
*)s
->private;
524 seq_printf(s
, "%04X %d.%d.%d\n", kim_gdata
->version
.full
,
525 kim_gdata
->version
.chip
, kim_gdata
->version
.maj_ver
,
526 kim_gdata
->version
.min_ver
);
530 static int show_list(struct seq_file
*s
, void *unused
)
532 struct kim_data_s
*kim_gdata
= (struct kim_data_s
*)s
->private;
533 kim_st_list_protocols(kim_gdata
->core_data
, s
);
537 /* function called from rfkill subsystem, when someone from
538 * user space would write 0/1 on the sysfs entry
539 * /sys/class/rfkill/rfkill0,1,3/state
541 static int kim_toggle_radio(void *data
, bool blocked
)
543 enum proto_type type
= *((enum proto_type
*)data
);
544 pr_debug(" %s: %d ", __func__
, type
);
553 st_kim_chip_toggle(type
, KIM_GPIO_INACTIVE
);
555 st_kim_chip_toggle(type
, KIM_GPIO_ACTIVE
);
558 pr_err(" wrong proto type ");
565 * st_kim_ref - reference the core's data
566 * This references the per-ST platform device in the arch/xx/
568 * This would enable multiple such platform devices to exist
569 * on a given platform
571 void st_kim_ref(struct st_data_s
**core_data
, int id
)
573 struct platform_device
*pdev
;
574 struct kim_data_s
*kim_gdata
;
575 /* get kim_gdata reference from platform device */
576 pdev
= st_get_plat_device(id
);
577 kim_gdata
= dev_get_drvdata(&pdev
->dev
);
578 *core_data
= kim_gdata
->core_data
;
581 static int kim_version_open(struct inode
*i
, struct file
*f
)
583 return single_open(f
, show_version
, i
->i_private
);
586 static int kim_list_open(struct inode
*i
, struct file
*f
)
588 return single_open(f
, show_list
, i
->i_private
);
591 static const struct file_operations version_debugfs_fops
= {
593 .open
= kim_version_open
,
596 .release
= single_release
,
598 static const struct file_operations list_debugfs_fops
= {
600 .open
= kim_list_open
,
603 .release
= single_release
,
606 /**********************************************************************/
607 /* functions called from platform device driver subsystem
608 * need to have a relevant platform device entry in the platform's
612 struct dentry
*kim_debugfs_dir
;
613 static int kim_probe(struct platform_device
*pdev
)
617 long *gpios
= pdev
->dev
.platform_data
;
618 struct kim_data_s
*kim_gdata
;
620 st_kim_devices
[pdev
->id
] = pdev
;
621 kim_gdata
= kzalloc(sizeof(struct kim_data_s
), GFP_ATOMIC
);
623 pr_err("no mem to allocate");
626 dev_set_drvdata(&pdev
->dev
, kim_gdata
);
628 status
= st_core_init(&kim_gdata
->core_data
);
630 pr_err(" ST core init failed");
633 /* refer to itself */
634 kim_gdata
->core_data
->kim_data
= kim_gdata
;
636 for (proto
= 0; proto
< ST_MAX
; proto
++) {
637 kim_gdata
->gpios
[proto
] = gpios
[proto
];
638 pr_info(" %ld gpio to be requested", gpios
[proto
]);
641 for (proto
= 0; (proto
< ST_MAX
) && (gpios
[proto
] != -1); proto
++) {
642 /* Claim the Bluetooth/FM/GPIO
643 * nShutdown gpio from the system
645 status
= gpio_request(gpios
[proto
], "kim");
646 if (unlikely(status
)) {
647 pr_err(" gpio %ld request failed ", gpios
[proto
]);
650 if (gpios
[proto
] != -1)
651 gpio_free(gpios
[proto
]);
656 /* Configure nShutdown GPIO as output=0 */
658 gpio_direction_output(gpios
[proto
], 0);
659 if (unlikely(status
)) {
660 pr_err(" unable to configure gpio %ld",
664 if (gpios
[proto
] != -1)
665 gpio_free(gpios
[proto
]);
670 /* get reference of pdev for request_firmware
672 kim_gdata
->kim_pdev
= pdev
;
673 init_completion(&kim_gdata
->kim_rcvd
);
674 init_completion(&kim_gdata
->ldisc_installed
);
676 for (proto
= 0; (proto
< ST_MAX
) && (gpios
[proto
] != -1); proto
++) {
677 /* TODO: should all types be rfkill_type_bt ? */
678 kim_gdata
->rf_protos
[proto
] = proto
;
679 kim_gdata
->rfkill
[proto
] = rfkill_alloc(protocol_names
[proto
],
680 &pdev
->dev
, RFKILL_TYPE_BLUETOOTH
,
681 &kim_rfkill_ops
, &kim_gdata
->rf_protos
[proto
]);
682 if (kim_gdata
->rfkill
[proto
] == NULL
) {
683 pr_err("cannot create rfkill entry for gpio %ld",
687 /* block upon creation */
688 rfkill_init_sw_state(kim_gdata
->rfkill
[proto
], 1);
689 status
= rfkill_register(kim_gdata
->rfkill
[proto
]);
690 if (unlikely(status
)) {
691 pr_err("rfkill registration failed for gpio %ld",
693 rfkill_unregister(kim_gdata
->rfkill
[proto
]);
696 pr_info("rfkill entry created for %ld", gpios
[proto
]);
699 kim_debugfs_dir
= debugfs_create_dir("ti-st", NULL
);
700 if (IS_ERR(kim_debugfs_dir
)) {
701 pr_err(" debugfs entries creation failed ");
702 kim_debugfs_dir
= NULL
;
706 debugfs_create_file("version", S_IRUGO
, kim_debugfs_dir
,
707 kim_gdata
, &version_debugfs_fops
);
708 debugfs_create_file("protocols", S_IRUGO
, kim_debugfs_dir
,
709 kim_gdata
, &list_debugfs_fops
);
710 pr_info(" debugfs entries created ");
714 static int kim_remove(struct platform_device
*pdev
)
716 /* free the GPIOs requested
718 long *gpios
= pdev
->dev
.platform_data
;
720 struct kim_data_s
*kim_gdata
;
722 kim_gdata
= dev_get_drvdata(&pdev
->dev
);
724 for (proto
= 0; (proto
< ST_MAX
) && (gpios
[proto
] != -1); proto
++) {
725 /* Claim the Bluetooth/FM/GPIO
726 * nShutdown gpio from the system
728 gpio_free(gpios
[proto
]);
729 rfkill_unregister(kim_gdata
->rfkill
[proto
]);
730 rfkill_destroy(kim_gdata
->rfkill
[proto
]);
731 kim_gdata
->rfkill
[proto
] = NULL
;
733 pr_info("kim: GPIO Freed");
734 debugfs_remove_recursive(kim_debugfs_dir
);
735 kim_gdata
->kim_pdev
= NULL
;
736 st_core_exit(kim_gdata
->core_data
);
743 /**********************************************************************/
744 /* entry point for ST KIM module, called in from ST Core */
746 static int __init
st_kim_init(void)
749 ret
= platform_driver_register(&kim_platform_driver
);
751 pr_err("platform drv registration failed");
757 static void __exit
st_kim_deinit(void)
759 /* the following returns void */
760 platform_driver_unregister(&kim_platform_driver
);
764 module_init(st_kim_init
);
765 module_exit(st_kim_deinit
);
766 MODULE_AUTHOR("Pavan Savoy <pavan_savoy@ti.com>");
767 MODULE_DESCRIPTION("Shared Transport Driver for TI BT/FM/GPS combo chips ");
768 MODULE_LICENSE("GPL");