1 /****************************************************************
3 Siano Mobile Silicon, Inc.
4 MDTV receiver kernel modules.
5 Copyright (C) 2006-2008, Uri Shkolnik
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ****************************************************************/
22 #include <linux/module.h>
23 #include <linux/init.h>
27 #include "dvb_demux.h"
28 #include "dvb_frontend.h"
30 #include "smscoreapi.h"
31 #include "smsendian.h"
32 #include "sms-cards.h"
34 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
36 struct smsdvb_client_t {
37 struct list_head entry;
39 struct smscore_device_t *coredev;
40 struct smscore_client_t *smsclient;
42 struct dvb_adapter adapter;
43 struct dvb_demux demux;
45 struct dvb_frontend frontend;
47 fe_status_t fe_status;
49 struct completion tune_done;
51 /* todo: save freq/band instead whole struct */
52 struct dvb_frontend_parameters fe_params;
54 struct SMSHOSTLIB_STATISTICS_DVB_S sms_stat_dvb;
59 static struct list_head g_smsdvb_clients;
60 static struct mutex g_smsdvb_clientslock;
63 module_param_named(debug, sms_dbg, int, 0644);
64 MODULE_PARM_DESC(debug, "set debug level (info=1, adv=2 (or-able))");
66 /* Events that may come from DVB v3 adapter */
67 static void sms_board_dvb3_event(struct smsdvb_client_t *client,
68 enum SMS_DVB3_EVENTS event) {
71 static int smsdvb_onresponse(void *context, struct smscore_buffer_t *cb)
73 struct smsdvb_client_t *client = (struct smsdvb_client_t *) context;
74 struct SmsMsgHdr_ST *phdr = (struct SmsMsgHdr_ST *) (((u8 *) cb->p)
76 u32 *pMsgData = (u32 *) phdr + 1;
77 /*u32 MsgDataLen = phdr->msgLength - sizeof(struct SmsMsgHdr_ST);*/
78 bool is_status_update = false;
80 smsendian_handle_rx_message((struct SmsMsgData_ST *) phdr);
82 switch (phdr->msgType) {
83 case MSG_SMS_DVBT_BDA_DATA:
84 dvb_dmx_swfilter(&client->demux, (u8 *)(phdr + 1),
85 cb->size - sizeof(struct SmsMsgHdr_ST));
88 case MSG_SMS_RF_TUNE_RES:
89 complete(&client->tune_done);
92 case MSG_SMS_SIGNAL_DETECTED_IND:
93 sms_info("MSG_SMS_SIGNAL_DETECTED_IND");
94 client->sms_stat_dvb.TransmissionData.IsDemodLocked = true;
95 is_status_update = true;
98 case MSG_SMS_NO_SIGNAL_IND:
99 sms_info("MSG_SMS_NO_SIGNAL_IND");
100 client->sms_stat_dvb.TransmissionData.IsDemodLocked = false;
101 is_status_update = true;
104 case MSG_SMS_TRANSMISSION_IND: {
105 sms_info("MSG_SMS_TRANSMISSION_IND");
108 memcpy(&client->sms_stat_dvb.TransmissionData, pMsgData,
109 sizeof(struct TRANSMISSION_STATISTICS_S));
111 /* Mo need to correct guard interval
112 * (as opposed to old statistics message).
114 CORRECT_STAT_BANDWIDTH(client->sms_stat_dvb.TransmissionData);
115 CORRECT_STAT_TRANSMISSON_MODE(
116 client->sms_stat_dvb.TransmissionData);
117 is_status_update = true;
120 case MSG_SMS_HO_PER_SLICES_IND: {
121 struct RECEPTION_STATISTICS_S *pReceptionData =
122 &client->sms_stat_dvb.ReceptionData;
123 struct SRVM_SIGNAL_STATUS_S SignalStatusData;
125 /*sms_info("MSG_SMS_HO_PER_SLICES_IND");*/
127 SignalStatusData.result = pMsgData[0];
128 SignalStatusData.snr = pMsgData[1];
129 SignalStatusData.inBandPower = (s32) pMsgData[2];
130 SignalStatusData.tsPackets = pMsgData[3];
131 SignalStatusData.etsPackets = pMsgData[4];
132 SignalStatusData.constellation = pMsgData[5];
133 SignalStatusData.hpCode = pMsgData[6];
134 SignalStatusData.tpsSrvIndLP = pMsgData[7] & 0x03;
135 SignalStatusData.tpsSrvIndHP = pMsgData[8] & 0x03;
136 SignalStatusData.cellId = pMsgData[9] & 0xFFFF;
137 SignalStatusData.reason = pMsgData[10];
138 SignalStatusData.requestId = pMsgData[11];
139 pReceptionData->IsRfLocked = pMsgData[16];
140 pReceptionData->IsDemodLocked = pMsgData[17];
141 pReceptionData->ModemState = pMsgData[12];
142 pReceptionData->SNR = pMsgData[1];
143 pReceptionData->BER = pMsgData[13];
144 pReceptionData->RSSI = pMsgData[14];
145 CORRECT_STAT_RSSI(client->sms_stat_dvb.ReceptionData);
147 pReceptionData->InBandPwr = (s32) pMsgData[2];
148 pReceptionData->CarrierOffset = (s32) pMsgData[15];
149 pReceptionData->TotalTSPackets = pMsgData[3];
150 pReceptionData->ErrorTSPackets = pMsgData[4];
153 if ((SignalStatusData.tsPackets + SignalStatusData.etsPackets)
155 pReceptionData->TS_PER = (SignalStatusData.etsPackets
156 * 100) / (SignalStatusData.tsPackets
157 + SignalStatusData.etsPackets);
159 pReceptionData->TS_PER = 0;
162 pReceptionData->BERBitCount = pMsgData[18];
163 pReceptionData->BERErrorCount = pMsgData[19];
165 pReceptionData->MRC_SNR = pMsgData[20];
166 pReceptionData->MRC_InBandPwr = pMsgData[21];
167 pReceptionData->MRC_RSSI = pMsgData[22];
169 is_status_update = true;
173 smscore_putbuffer(client->coredev, cb);
175 if (is_status_update) {
176 if (client->sms_stat_dvb.ReceptionData.IsDemodLocked) {
177 client->fe_status = FE_HAS_SIGNAL | FE_HAS_CARRIER
178 | FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
179 sms_board_dvb3_event(client, DVB3_EVENT_FE_LOCK);
180 if (client->sms_stat_dvb.ReceptionData.ErrorTSPackets
182 sms_board_dvb3_event(client, DVB3_EVENT_UNC_OK);
184 sms_board_dvb3_event(client,
188 /*client->fe_status =
189 (phdr->msgType == MSG_SMS_NO_SIGNAL_IND) ?
191 client->fe_status = 0;
192 sms_board_dvb3_event(client, DVB3_EVENT_FE_UNLOCK);
199 static void smsdvb_unregister_client(struct smsdvb_client_t *client)
201 /* must be called under clientslock */
203 list_del(&client->entry);
205 smscore_unregister_client(client->smsclient);
206 dvb_unregister_frontend(&client->frontend);
207 dvb_dmxdev_release(&client->dmxdev);
208 dvb_dmx_release(&client->demux);
209 dvb_unregister_adapter(&client->adapter);
213 static void smsdvb_onremove(void *context)
215 kmutex_lock(&g_smsdvb_clientslock);
217 smsdvb_unregister_client((struct smsdvb_client_t *) context);
219 kmutex_unlock(&g_smsdvb_clientslock);
222 static int smsdvb_start_feed(struct dvb_demux_feed *feed)
224 struct smsdvb_client_t *client =
225 container_of(feed->demux, struct smsdvb_client_t, demux);
226 struct SmsMsgData_ST PidMsg;
228 sms_debug("add pid %d(%x)",
229 feed->pid, feed->pid);
231 PidMsg.xMsgHeader.msgSrcId = DVBT_BDA_CONTROL_MSG_ID;
232 PidMsg.xMsgHeader.msgDstId = HIF_TASK;
233 PidMsg.xMsgHeader.msgFlags = 0;
234 PidMsg.xMsgHeader.msgType = MSG_SMS_ADD_PID_FILTER_REQ;
235 PidMsg.xMsgHeader.msgLength = sizeof(PidMsg);
236 PidMsg.msgData[0] = feed->pid;
238 smsendian_handle_tx_message((struct SmsMsgHdr_ST *)&PidMsg);
239 return smsclient_sendrequest(client->smsclient,
240 &PidMsg, sizeof(PidMsg));
243 static int smsdvb_stop_feed(struct dvb_demux_feed *feed)
245 struct smsdvb_client_t *client =
246 container_of(feed->demux, struct smsdvb_client_t, demux);
247 struct SmsMsgData_ST PidMsg;
249 sms_debug("remove pid %d(%x)",
250 feed->pid, feed->pid);
252 PidMsg.xMsgHeader.msgSrcId = DVBT_BDA_CONTROL_MSG_ID;
253 PidMsg.xMsgHeader.msgDstId = HIF_TASK;
254 PidMsg.xMsgHeader.msgFlags = 0;
255 PidMsg.xMsgHeader.msgType = MSG_SMS_REMOVE_PID_FILTER_REQ;
256 PidMsg.xMsgHeader.msgLength = sizeof(PidMsg);
257 PidMsg.msgData[0] = feed->pid;
259 smsendian_handle_tx_message((struct SmsMsgHdr_ST *)&PidMsg);
260 return smsclient_sendrequest(client->smsclient,
261 &PidMsg, sizeof(PidMsg));
264 static int smsdvb_sendrequest_and_wait(struct smsdvb_client_t *client,
265 void *buffer, size_t size,
266 struct completion *completion)
270 smsendian_handle_tx_message((struct SmsMsgHdr_ST *)buffer);
271 rc = smsclient_sendrequest(client->smsclient, buffer, size);
275 return wait_for_completion_timeout(completion,
276 msecs_to_jiffies(2000)) ?
280 static int smsdvb_read_status(struct dvb_frontend *fe, fe_status_t *stat)
282 struct smsdvb_client_t *client;
283 client = container_of(fe, struct smsdvb_client_t, frontend);
285 *stat = client->fe_status;
290 static int smsdvb_read_ber(struct dvb_frontend *fe, u32 *ber)
292 struct smsdvb_client_t *client;
293 client = container_of(fe, struct smsdvb_client_t, frontend);
295 *ber = client->sms_stat_dvb.ReceptionData.BER;
300 static int smsdvb_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
302 struct smsdvb_client_t *client;
303 client = container_of(fe, struct smsdvb_client_t, frontend);
305 if (client->sms_stat_dvb.ReceptionData.InBandPwr < -95)
307 else if (client->sms_stat_dvb.ReceptionData.InBandPwr > -29)
311 (client->sms_stat_dvb.ReceptionData.InBandPwr
317 static int smsdvb_read_snr(struct dvb_frontend *fe, u16 *snr)
319 struct smsdvb_client_t *client;
320 client = container_of(fe, struct smsdvb_client_t, frontend);
322 *snr = client->sms_stat_dvb.ReceptionData.SNR;
327 static int smsdvb_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
329 struct smsdvb_client_t *client;
330 client = container_of(fe, struct smsdvb_client_t, frontend);
332 *ucblocks = client->sms_stat_dvb.ReceptionData.ErrorTSPackets;
337 static int smsdvb_get_tune_settings(struct dvb_frontend *fe,
338 struct dvb_frontend_tune_settings *tune)
342 tune->min_delay_ms = 400;
343 tune->step_size = 250000;
348 static int smsdvb_set_frontend(struct dvb_frontend *fe,
349 struct dvb_frontend_parameters *fep)
351 struct smsdvb_client_t *client =
352 container_of(fe, struct smsdvb_client_t, frontend);
355 struct SmsMsgHdr_ST Msg;
359 client->fe_status = FE_HAS_SIGNAL;
360 client->event_fe_state = -1;
361 client->event_unc_state = -1;
363 Msg.Msg.msgSrcId = DVBT_BDA_CONTROL_MSG_ID;
364 Msg.Msg.msgDstId = HIF_TASK;
365 Msg.Msg.msgFlags = 0;
366 Msg.Msg.msgType = MSG_SMS_RF_TUNE_REQ;
367 Msg.Msg.msgLength = sizeof(Msg);
368 Msg.Data[0] = fep->frequency;
369 Msg.Data[2] = 12000000;
371 sms_debug("freq %d band %d",
372 fep->frequency, fep->u.ofdm.bandwidth);
374 switch (fep->u.ofdm.bandwidth) {
375 case BANDWIDTH_8_MHZ: Msg.Data[1] = BW_8_MHZ; break;
376 case BANDWIDTH_7_MHZ: Msg.Data[1] = BW_7_MHZ; break;
377 case BANDWIDTH_6_MHZ: Msg.Data[1] = BW_6_MHZ; break;
378 case BANDWIDTH_AUTO: return -EOPNOTSUPP;
379 default: return -EINVAL;
382 return smsdvb_sendrequest_and_wait(client, &Msg, sizeof(Msg),
386 static int smsdvb_get_frontend(struct dvb_frontend *fe,
387 struct dvb_frontend_parameters *fep)
389 struct smsdvb_client_t *client =
390 container_of(fe, struct smsdvb_client_t, frontend);
395 memcpy(fep, &client->fe_params,
396 sizeof(struct dvb_frontend_parameters));
401 static int smsdvb_init(struct dvb_frontend *fe)
403 struct smsdvb_client_t *client =
404 container_of(fe, struct smsdvb_client_t, frontend);
406 sms_board_dvb3_event(client, DVB3_EVENT_INIT);
410 static int smsdvb_sleep(struct dvb_frontend *fe)
412 struct smsdvb_client_t *client =
413 container_of(fe, struct smsdvb_client_t, frontend);
415 sms_board_dvb3_event(client, DVB3_EVENT_SLEEP);
420 static void smsdvb_release(struct dvb_frontend *fe)
425 static struct dvb_frontend_ops smsdvb_fe_ops = {
427 .name = "Siano Mobile Digital MDTV Receiver",
429 .frequency_min = 44250000,
430 .frequency_max = 867250000,
431 .frequency_stepsize = 250000,
432 .caps = FE_CAN_INVERSION_AUTO |
433 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
434 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
435 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
436 FE_CAN_QAM_AUTO | FE_CAN_TRANSMISSION_MODE_AUTO |
437 FE_CAN_GUARD_INTERVAL_AUTO |
439 FE_CAN_HIERARCHY_AUTO,
442 .release = smsdvb_release,
444 .set_frontend = smsdvb_set_frontend,
445 .get_frontend = smsdvb_get_frontend,
446 .get_tune_settings = smsdvb_get_tune_settings,
448 .read_status = smsdvb_read_status,
449 .read_ber = smsdvb_read_ber,
450 .read_signal_strength = smsdvb_read_signal_strength,
451 .read_snr = smsdvb_read_snr,
452 .read_ucblocks = smsdvb_read_ucblocks,
455 .sleep = smsdvb_sleep,
458 static int smsdvb_hotplug(struct smscore_device_t *coredev,
459 struct device *device, int arrival)
461 struct smsclient_params_t params;
462 struct smsdvb_client_t *client;
465 /* device removal handled by onremove callback */
469 if (smscore_get_device_mode(coredev) != DEVICE_MODE_DVBT_BDA) {
470 sms_err("SMS Device mode is not set for "
475 client = kzalloc(sizeof(struct smsdvb_client_t), GFP_KERNEL);
477 sms_err("kmalloc() failed");
481 /* register dvb adapter */
482 rc = dvb_register_adapter(&client->adapter,
484 smscore_get_board_id(coredev))->name,
485 THIS_MODULE, device, adapter_nr);
487 sms_err("dvb_register_adapter() failed %d", rc);
492 client->demux.dmx.capabilities = DMX_TS_FILTERING;
493 client->demux.filternum = 32; /* todo: nova ??? */
494 client->demux.feednum = 32;
495 client->demux.start_feed = smsdvb_start_feed;
496 client->demux.stop_feed = smsdvb_stop_feed;
498 rc = dvb_dmx_init(&client->demux);
500 sms_err("dvb_dmx_init failed %d", rc);
505 client->dmxdev.filternum = 32;
506 client->dmxdev.demux = &client->demux.dmx;
507 client->dmxdev.capabilities = 0;
509 rc = dvb_dmxdev_init(&client->dmxdev, &client->adapter);
511 sms_err("dvb_dmxdev_init failed %d", rc);
515 /* init and register frontend */
516 memcpy(&client->frontend.ops, &smsdvb_fe_ops,
517 sizeof(struct dvb_frontend_ops));
519 rc = dvb_register_frontend(&client->adapter, &client->frontend);
521 sms_err("frontend registration failed %d", rc);
525 params.initial_id = 1;
526 params.data_type = MSG_SMS_DVBT_BDA_DATA;
527 params.onresponse_handler = smsdvb_onresponse;
528 params.onremove_handler = smsdvb_onremove;
529 params.context = client;
531 rc = smscore_register_client(coredev, ¶ms, &client->smsclient);
533 sms_err("smscore_register_client() failed %d", rc);
537 client->coredev = coredev;
539 init_completion(&client->tune_done);
541 kmutex_lock(&g_smsdvb_clientslock);
543 list_add(&client->entry, &g_smsdvb_clients);
545 kmutex_unlock(&g_smsdvb_clientslock);
547 client->event_fe_state = -1;
548 client->event_unc_state = -1;
549 sms_board_dvb3_event(client, DVB3_EVENT_HOTPLUG);
552 sms_board_setup(coredev);
557 dvb_unregister_frontend(&client->frontend);
560 dvb_dmxdev_release(&client->dmxdev);
563 dvb_dmx_release(&client->demux);
566 dvb_unregister_adapter(&client->adapter);
573 int smsdvb_module_init(void)
577 INIT_LIST_HEAD(&g_smsdvb_clients);
578 kmutex_init(&g_smsdvb_clientslock);
580 rc = smscore_register_hotplug(smsdvb_hotplug);
587 void smsdvb_module_exit(void)
589 smscore_unregister_hotplug(smsdvb_hotplug);
591 kmutex_lock(&g_smsdvb_clientslock);
593 while (!list_empty(&g_smsdvb_clients))
594 smsdvb_unregister_client(
595 (struct smsdvb_client_t *) g_smsdvb_clients.next);
597 kmutex_unlock(&g_smsdvb_clientslock);
600 module_init(smsdvb_module_init);
601 module_exit(smsdvb_module_exit);
603 MODULE_DESCRIPTION("SMS DVB subsystem adaptation module");
604 MODULE_AUTHOR("Siano Mobile Silicon, Inc. (uris@siano-ms.com)");
605 MODULE_LICENSE("GPL");