35 #include <linux/module.h>
64 static u8 default_radio_region;
69 static u32 default_rds_buf = 300;
74 static u32 radio_nr = -1;
79 static void fm_irq_send_flag_getcmd(
struct fmdev *);
80 static void fm_irq_handle_flag_getcmd_resp(
struct fmdev *);
81 static void fm_irq_handle_hw_malfunction(
struct fmdev *);
82 static void fm_irq_handle_rds_start(
struct fmdev *);
83 static void fm_irq_send_rdsdata_getcmd(
struct fmdev *);
84 static void fm_irq_handle_rdsdata_getcmd_resp(
struct fmdev *);
85 static void fm_irq_handle_rds_finish(
struct fmdev *);
86 static void fm_irq_handle_tune_op_ended(
struct fmdev *);
87 static void fm_irq_handle_power_enb(
struct fmdev *);
88 static void fm_irq_handle_low_rssi_start(
struct fmdev *);
89 static void fm_irq_afjump_set_pi(
struct fmdev *);
90 static void fm_irq_handle_set_pi_resp(
struct fmdev *);
91 static void fm_irq_afjump_set_pimask(
struct fmdev *);
92 static void fm_irq_handle_set_pimask_resp(
struct fmdev *);
93 static void fm_irq_afjump_setfreq(
struct fmdev *);
94 static void fm_irq_handle_setfreq_resp(
struct fmdev *);
95 static void fm_irq_afjump_enableint(
struct fmdev *);
96 static void fm_irq_afjump_enableint_resp(
struct fmdev *);
97 static void fm_irq_start_afjump(
struct fmdev *);
98 static void fm_irq_handle_start_afjump_resp(
struct fmdev *);
99 static void fm_irq_afjump_rd_freq(
struct fmdev *);
100 static void fm_irq_afjump_rd_freq_resp(
struct fmdev *);
101 static void fm_irq_handle_low_rssi_finish(
struct fmdev *);
102 static void fm_irq_send_intmsk_cmd(
struct fmdev *);
103 static void fm_irq_handle_intmsk_cmd_resp(
struct fmdev *);
151 fm_irq_send_flag_getcmd,
152 fm_irq_handle_flag_getcmd_resp,
153 fm_irq_handle_hw_malfunction,
154 fm_irq_handle_rds_start,
155 fm_irq_send_rdsdata_getcmd,
156 fm_irq_handle_rdsdata_getcmd_resp,
157 fm_irq_handle_rds_finish,
158 fm_irq_handle_tune_op_ended,
159 fm_irq_handle_power_enb,
160 fm_irq_handle_low_rssi_start,
161 fm_irq_afjump_set_pi,
162 fm_irq_handle_set_pi_resp,
163 fm_irq_afjump_set_pimask,
164 fm_irq_handle_set_pimask_resp,
165 fm_irq_afjump_setfreq,
166 fm_irq_handle_setfreq_resp,
167 fm_irq_afjump_enableint,
168 fm_irq_afjump_enableint_resp,
170 fm_irq_handle_start_afjump_resp,
171 fm_irq_afjump_rd_freq,
172 fm_irq_afjump_rd_freq_resp,
173 fm_irq_handle_low_rssi_finish,
174 fm_irq_send_intmsk_cmd,
175 fm_irq_handle_intmsk_cmd_resp
179 static struct completion wait_for_fmdrv_reg_comp;
181 static inline void fm_irq_call(
struct fmdev *
fmdev)
187 static inline void fm_irq_call_stage(
struct fmdev *
fmdev,
u8 stage)
193 static inline void fm_irq_timeout_stage(
struct fmdev *
fmdev,
u8 stage)
199 #ifdef FM_DUMP_TXRX_PKT
201 inline void dump_tx_skb_data(
struct sk_buff *
skb)
208 printk(
KERN_INFO "<<%shdr:%02x len:%02x opcode:%02x type:%s dlen:%02x",
209 fm_cb(skb)->completion ?
" " :
"*", cmd_hdr->
hdr,
210 cmd_hdr->
len, cmd_hdr->
op,
211 cmd_hdr->
rd_wr ?
"RD" :
"WR", cmd_hdr->
dlen);
216 len =
min(len_org, 14);
217 for (index = 0; index <
len; index++)
220 printk(
"%s", (len_org > 14) ?
".." :
"");
226 inline void dump_rx_skb_data(
struct sk_buff *skb)
234 "opcode:%02x type:%s dlen:%02x", evt_hdr->hdr, evt_hdr->
len,
236 (evt_hdr->
rd_wr) ?
"RD" :
"WR", evt_hdr->
dlen);
241 len =
min(len_org, 14);
242 for (index = 0; index <
len; index++)
245 printk(
"%s", (len_org > 14) ?
".." :
"");
253 fmdev->
rx.region = region_configs[region_to_set];
260 static void recv_tasklet(
unsigned long arg)
269 fmdev = (
struct fmdev *)arg;
274 fmerr(
"skb(%p) has only %d bytes, "
275 "at least need %zu bytes to decode\n", skb,
281 evt_hdr = (
void *)skb->
data;
289 if (irq_info->
stage != 0) {
290 fmerr(
"Inval stage resetting to zero\n");
318 fmerr(
"Response SKB ptr not NULL\n");
330 fmerr(
"Nobody claimed SKB(%p),purging\n", skb);
338 if (!skb_queue_empty(&fmdev->
tx_q))
339 tasklet_schedule(&fmdev->
tx_task);
344 static void send_tasklet(
unsigned long arg)
350 fmdev = (
struct fmdev *)arg;
357 fmerr(
"TX timeout occurred\n");
370 fmerr(
"Response completion handler is not NULL\n");
379 fmerr(
"TX tasklet failed to send skb(%p)\n", skb);
390 static int fm_send_cmd(
struct fmdev *fmdev,
u8 fm_op,
u16 type,
void *
payload,
398 fmerr(
"Invalid fm opcode - %d\n", fm_op);
402 fmerr(
"Payload data is NULL during fw download\n");
413 fmerr(
"No memory to create new SKB\n");
427 hdr->
len = ((payload ==
NULL) ? 0 : payload_len) + 3;
435 fm_cb(skb)->fm_op = fm_op;
445 }
else if (payload !=
NULL) {
446 fm_cb(skb)->fm_op = *((
u8 *)payload + 2);
451 fm_cb(skb)->completion = wait_completion;
453 tasklet_schedule(&fmdev->
tx_task);
460 unsigned int payload_len,
void *
response,
int *response_len)
468 ret = fm_send_cmd(fmdev, fm_op, type, payload, payload_len,
475 fmerr(
"Timeout(%d sec),didn't get reg"
476 "completion signal from RX tasklet\n",
481 fmerr(
"Response SKB is missing\n");
489 evt_hdr = (
void *)skb->
data;
491 fmerr(
"Received event pkt status(%d) is not zero\n",
497 if (response !=
NULL && response_len !=
NULL && evt_hdr->
dlen) {
501 *response_len = evt_hdr->
dlen;
502 }
else if (response_len !=
NULL && evt_hdr->
dlen == 0) {
511 static inline int check_cmdresp_status(
struct fmdev *fmdev,
524 fm_evt_hdr = (
void *)(*skb)->data;
526 fmerr(
"irq: opcode %x response status is not zero "
527 "Initiating irq recovery process\n",
537 static inline void fm_irq_common_cmd_resp_helper(
struct fmdev *fmdev,
u8 stage)
541 if (!check_cmdresp_status(fmdev, &skb))
542 fm_irq_call_stage(fmdev, stage);
552 static void int_timeout_handler(
unsigned long data)
557 fmdbg(
"irq: timeout,trying to re-enable fm interrupts\n");
558 fmdev = (
struct fmdev *)data;
567 fmerr(
"Recovery action failed during"
568 "irq processing, max retry reached\n");
575 static void fm_irq_send_flag_getcmd(
struct fmdev *fmdev)
584 static void fm_irq_handle_flag_getcmd_resp(
struct fmdev *fmdev)
589 if (check_cmdresp_status(fmdev, &skb))
592 fm_evt_hdr = (
void *)skb->
data;
605 static void fm_irq_handle_hw_malfunction(
struct fmdev *fmdev)
608 fmerr(
"irq: HW MAL int received - do nothing\n");
614 static void fm_irq_handle_rds_start(
struct fmdev *fmdev)
617 fmdbg(
"irq: rds threshold reached\n");
627 static void fm_irq_send_rdsdata_getcmd(
struct fmdev *fmdev)
636 static void fm_rx_update_af_cache(
struct fmdev *fmdev,
u8 af)
639 u8 reg_idx = fmdev->
rx.region.fm_band;
646 fmdev->
rx.stat_info.afcache_size = 0;
647 fmdbg(
"No of expected AF : %d\n", fmdev->
rx.stat_info.af_list_max);
658 freq = fmdev->
rx.region.bot_freq + (af * 100);
659 if (freq == fmdev->
rx.freq) {
660 fmdbg(
"Current freq(%d) is matching with received AF(%d)\n",
661 fmdev->
rx.freq, freq);
665 for (index = 0; index < stat_info->
afcache_size; index++) {
666 if (stat_info->
af_cache[index] == freq)
671 fmdbg(
"AF cache is full\n");
679 fmdbg(
"Storing AF %d to cache index %d\n", freq, index);
689 static void fm_rdsparse_swapbytes(
struct fmdev *fmdev,
701 if (fmdev->
asci_id != 0x6350) {
704 byte1 = rds_buff[
index];
705 rds_buff[
index] = rds_buff[index + 1];
706 rds_buff[index + 1] = byte1;
712 static void fm_irq_handle_rdsdata_getcmd_resp(
struct fmdev *fmdev)
716 struct fm_rds *rds = &fmdev->
rx.rds;
717 unsigned long group_idx,
flags;
723 if (check_cmdresp_status(fmdev, &skb))
728 rds_data = skb->
data;
733 meta_data = rds_data[2];
735 type = (meta_data & 0x07);
739 fmdbg(
"Block index:%d(%s)\n", blk_idx,
746 fmdbg(
"Block sequence mismatch\n");
752 memcpy(&rds_fmt.data.groupdatabuff.
759 if (blk_idx == FM_RDS_BLK_IDX_D) {
760 fmdbg(
"Good block received\n");
761 fm_rdsparse_swapbytes(fmdev, &rds_fmt);
767 cur_picode =
be16_to_cpu(rds_fmt.data.groupgeneral.pidata);
768 if (fmdev->
rx.stat_info.picode != cur_picode)
769 fmdev->
rx.stat_info.picode = cur_picode;
771 fmdbg(
"picode:%d\n", cur_picode);
773 group_idx = (rds_fmt.data.groupgeneral.blk_b[0] >> 3);
774 fmdbg(
"(fmdrv):Group:%ld%s\n", group_idx/2,
775 (group_idx % 2) ?
"B" :
"A");
777 group_idx = 1 << (rds_fmt.data.groupgeneral.blk_b[0] >> 3);
779 fm_rx_update_af_cache(fmdev, rds_fmt.data.group0A.af[0]);
780 fm_rx_update_af_cache(fmdev, rds_fmt.data.group0A.af[1]);
788 rds_data = skb->
data;
792 while (rds_len > 0) {
797 type = (rds_data[2] & 0x07);
800 tmpbuf[2] |= blk_idx << 3;
803 tmpbuf[0] = rds_data[0];
804 tmpbuf[1] = rds_data[1];
811 fmdbg(
"RDS buffer overflow\n");
828 static void fm_irq_handle_rds_finish(
struct fmdev *fmdev)
833 static void fm_irq_handle_tune_op_ended(
struct fmdev *fmdev)
837 fmdbg(
"irq: tune ended/bandlimit reached\n");
850 static void fm_irq_handle_power_enb(
struct fmdev *fmdev)
853 fmdbg(
"irq: Power Enabled/Disabled\n");
860 static void fm_irq_handle_low_rssi_start(
struct fmdev *fmdev)
865 (fmdev->
rx.stat_info.afcache_size != 0)) {
866 fmdbg(
"irq: rssi level has fallen below threshold level\n");
871 fmdev->
rx.afjump_idx = 0;
872 fmdev->
rx.freq_before_jump = fmdev->
rx.freq;
882 static void fm_irq_afjump_set_pi(
struct fmdev *fmdev)
887 payload = fmdev->
rx.stat_info.picode;
892 static void fm_irq_handle_set_pi_resp(
struct fmdev *fmdev)
902 static void fm_irq_afjump_set_pimask(
struct fmdev *fmdev)
911 static void fm_irq_handle_set_pimask_resp(
struct fmdev *fmdev)
916 static void fm_irq_afjump_setfreq(
struct fmdev *fmdev)
921 fmdbg(
"Swtich to %d KHz\n", fmdev->
rx.stat_info.af_cache[fmdev->
rx.afjump_idx]);
922 frq_index = (fmdev->
rx.stat_info.af_cache[fmdev->
rx.afjump_idx] -
930 static void fm_irq_handle_setfreq_resp(
struct fmdev *fmdev)
935 static void fm_irq_afjump_enableint(
struct fmdev *fmdev)
945 static void fm_irq_afjump_enableint_resp(
struct fmdev *fmdev)
950 static void fm_irq_start_afjump(
struct fmdev *fmdev)
956 sizeof(payload),
NULL))
960 static void fm_irq_handle_start_afjump_resp(
struct fmdev *fmdev)
964 if (check_cmdresp_status(fmdev, &skb))
972 static void fm_irq_afjump_rd_freq(
struct fmdev *fmdev)
980 static void fm_irq_afjump_rd_freq_resp(
struct fmdev *fmdev)
984 u32 curr_freq, jumped_freq;
986 if (check_cmdresp_status(fmdev, &skb))
991 memcpy(&read_freq, skb->
data,
sizeof(read_freq));
995 jumped_freq = fmdev->
rx.stat_info.af_cache[fmdev->
rx.afjump_idx];
998 if ((curr_freq != fmdev->
rx.freq_before_jump) && (curr_freq == jumped_freq)) {
999 fmdbg(
"Successfully switched to alternate freq %d\n", curr_freq);
1000 fmdev->
rx.freq = curr_freq;
1009 fmdev->
rx.afjump_idx++;
1012 if (fmdev->
rx.afjump_idx >= fmdev->
rx.stat_info.afcache_size) {
1013 fmdbg(
"AF switch processing failed\n");
1017 fmdbg(
"Trying next freq in AF cache\n");
1024 static void fm_irq_handle_low_rssi_finish(
struct fmdev *fmdev)
1029 static void fm_irq_send_intmsk_cmd(
struct fmdev *fmdev)
1037 sizeof(payload),
NULL))
1041 static void fm_irq_handle_intmsk_cmd_resp(
struct fmdev *fmdev)
1045 if (check_cmdresp_status(fmdev, &skb))
1064 poll_wait(file, &fmdev->
rx.rds.read_queue, pts);
1065 if (fmdev->
rx.rds.rd_idx != fmdev->
rx.rds.wr_idx)
1076 unsigned long flags;
1079 if (fmdev->
rx.rds.wr_idx == fmdev->
rx.rds.rd_idx) {
1084 (fmdev->
rx.rds.wr_idx != fmdev->
rx.rds.rd_idx));
1096 while (block_count < count) {
1097 if (fmdev->
rx.rds.wr_idx == fmdev->
rx.rds.rd_idx)
1105 if (fmdev->
rx.rds.rd_idx >= fmdev->
rx.rds.buf_size)
1106 fmdev->
rx.rds.rd_idx = 0;
1133 fmerr(
"RX frequency is not set\n");
1136 if (cur_tuned_frq ==
NULL) {
1137 fmerr(
"Invalid memory\n");
1143 *cur_tuned_frq = fmdev->
rx.freq;
1213 static int fm_power_down(
struct fmdev *fmdev)
1219 fmerr(
"FM core is not ready\n");
1223 fmdbg(
"FM chip is already in OFF state\n");
1237 static int fm_download_firmware(
struct fmdev *fmdev,
const u8 *
fw_name)
1244 int ret, fw_len, cmd_cnt;
1252 fmerr(
"Unable to read firmware(%s) content\n", fw_name);
1255 fmdbg(
"Firmware(%s) length : %d bytes\n", fw_name, fw_entry->
size);
1257 fw_data = (
void *)fw_entry->
data;
1258 fw_len = fw_entry->
size;
1262 fmerr(
"%s not a legal TI firmware file\n", fw_name);
1266 fmdbg(
"FW(%s) magic number : 0x%x\n", fw_name, fw_header->
magic);
1272 while (fw_data && fw_len > 0) {
1275 switch (action->
type) {
1290 fw_data += (
sizeof(
struct bts_action) + (action->size));
1291 fw_len -= (
sizeof(
struct bts_action) + (action->size));
1293 fmdbg(
"Firmware commands(%d) loaded to chip\n", cmd_cnt);
1302 static int load_default_rx_configuration(
struct fmdev *fmdev)
1314 static int fm_power_up(
struct fmdev *fmdev,
u8 mode)
1321 fmerr(
"Invalid firmware download option\n");
1331 fmerr(
"Unable to prepare FM Common\n");
1344 sizeof(asic_id), &asic_id, &resp_len))
1348 sizeof(asic_ver), &asic_ver, &resp_len))
1351 fmdbg(
"ASIC ID: 0x%x , ASIC Version: %d\n",
1357 ret = fm_download_firmware(fmdev, fw_name);
1359 fmdbg(
"Failed to download firmware file %s\n", fw_name);
1366 ret = fm_download_firmware(fmdev, fw_name);
1368 fmdbg(
"Failed to download firmware file %s\n", fw_name);
1382 fmerr(
"Invalid FM mode\n");
1386 fmdbg(
"Already fm is in mode(%d)\n", fm_mode);
1392 ret = fm_power_down(fmdev);
1394 fmerr(
"Failed to set OFF mode\n");
1403 ret = fm_power_down(fmdev);
1405 fmerr(
"Failed to set OFF mode\n");
1410 ret = fm_power_up(fmdev, fm_mode);
1412 fmerr(
"Failed to load firmware\n");
1420 fmdbg(
"Loading default rx configuration..\n");
1421 ret = load_default_rx_configuration(fmdev);
1423 fmerr(
"Failed to load default values\n");
1433 fmerr(
"FM core is not ready\n");
1436 if (fmmode ==
NULL) {
1437 fmerr(
"Invalid memory\n");
1446 static long fm_st_receive(
void *arg,
struct sk_buff *skb)
1448 struct fmdev *fmdev;
1450 fmdev = (
struct fmdev *)arg;
1453 fmerr(
"Invalid SKB received from ST\n");
1458 fmerr(
"Received SKB (%p) is not FM Channel 8 pkt\n", skb);
1464 tasklet_schedule(&fmdev->
rx_task);
1473 static void fm_st_reg_comp_cb(
void *arg,
char data)
1475 struct fmdev *fmdev;
1477 fmdev = (
struct fmdev *)arg;
1479 complete(&wait_for_fmdrv_reg_comp);
1492 fmdbg(
"FM Core is already up\n");
1496 memset(&fm_st_proto, 0,
sizeof(fm_st_proto));
1497 fm_st_proto.
recv = fm_st_receive;
1511 init_completion(&wait_for_fmdrv_reg_comp);
1513 fmdbg(
"%s waiting for ST reg completion signal\n", __func__);
1517 fmerr(
"Timeout(%d sec), didn't get reg "
1518 "completion signal from ST\n",
1523 fmerr(
"ST reg comp CB called with error "
1529 }
else if (ret == -1) {
1530 fmerr(
"st_register failed %d\n", ret);
1537 fmerr(
"Failed to get ST write func pointer\n");
1540 fmerr(
"st_unregister failed %d\n", ret);
1548 skb_queue_head_init(&fmdev->
tx_q);
1552 skb_queue_head_init(&fmdev->rx_q);
1553 tasklet_init(&fmdev->rx_task, recv_tasklet, (
unsigned long)fmdev);
1555 fmdev->irq_info.stage = 0;
1557 fmdev->resp_comp =
NULL;
1560 fmdev->irq_info.timer.function = &int_timeout_handler;
1561 fmdev->irq_info.timer.data = (
unsigned long)fmdev;
1566 memcpy(&fmdev->rx.region, ®ion_configs[default_radio_region],
1575 fmdev->irq_info.retry = 0;
1596 fmdbg(
"FM Core is already down\n");
1611 memset(&fm_st_proto, 0,
sizeof(fm_st_proto));
1617 fmerr(
"Failed to de-register FM from ST %d\n", ret);
1619 fmdbg(
"Successfully unregistered from ST\n");
1629 static int __init fm_drv_init(
void)
1631 struct fmdev *fmdev =
NULL;
1636 fmdev = kzalloc(
sizeof(
struct fmdev),
GFP_KERNEL);
1637 if (
NULL == fmdev) {
1638 fmerr(
"Can't allocate operation structure memory\n");
1642 fmdev->
rx.rds.buff = kzalloc(fmdev->
rx.rds.buf_size,
GFP_KERNEL);
1643 if (
NULL == fmdev->
rx.rds.buff) {
1644 fmerr(
"Can't allocate rds ring buffer\n");
1652 fmdev->
irq_info.handlers = int_handler_table;
1667 static void __exit fm_drv_exit(
void)
1669 struct fmdev *fmdev =
NULL;
1672 if (fmdev !=
NULL) {