Merge branch 'fix/pcm-hwptr' into for-linus
[linux/fpc-iii.git] / drivers / media / dvb / siano / smsdvb.c
blob3ee1c3902c56dcc9d56a26380e5dadb07459cd99
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>
25 #include "dmxdev.h"
26 #include "dvbdev.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;
44 struct dmxdev dmxdev;
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;
55 int event_fe_state;
56 int event_unc_state;
59 static struct list_head g_smsdvb_clients;
60 static struct mutex g_smsdvb_clientslock;
62 static int sms_dbg;
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) {
70 struct smscore_device_t *coredev = client->coredev;
71 switch (event) {
72 case DVB3_EVENT_INIT:
73 sms_debug("DVB3_EVENT_INIT");
74 sms_board_event(coredev, BOARD_EVENT_BIND);
75 break;
76 case DVB3_EVENT_SLEEP:
77 sms_debug("DVB3_EVENT_SLEEP");
78 sms_board_event(coredev, BOARD_EVENT_POWER_SUSPEND);
79 break;
80 case DVB3_EVENT_HOTPLUG:
81 sms_debug("DVB3_EVENT_HOTPLUG");
82 sms_board_event(coredev, BOARD_EVENT_POWER_INIT);
83 break;
84 case DVB3_EVENT_FE_LOCK:
85 if (client->event_fe_state != DVB3_EVENT_FE_LOCK) {
86 client->event_fe_state = DVB3_EVENT_FE_LOCK;
87 sms_debug("DVB3_EVENT_FE_LOCK");
88 sms_board_event(coredev, BOARD_EVENT_FE_LOCK);
90 break;
91 case DVB3_EVENT_FE_UNLOCK:
92 if (client->event_fe_state != DVB3_EVENT_FE_UNLOCK) {
93 client->event_fe_state = DVB3_EVENT_FE_UNLOCK;
94 sms_debug("DVB3_EVENT_FE_UNLOCK");
95 sms_board_event(coredev, BOARD_EVENT_FE_UNLOCK);
97 break;
98 case DVB3_EVENT_UNC_OK:
99 if (client->event_unc_state != DVB3_EVENT_UNC_OK) {
100 client->event_unc_state = DVB3_EVENT_UNC_OK;
101 sms_debug("DVB3_EVENT_UNC_OK");
102 sms_board_event(coredev, BOARD_EVENT_MULTIPLEX_OK);
104 break;
105 case DVB3_EVENT_UNC_ERR:
106 if (client->event_unc_state != DVB3_EVENT_UNC_ERR) {
107 client->event_unc_state = DVB3_EVENT_UNC_ERR;
108 sms_debug("DVB3_EVENT_UNC_ERR");
109 sms_board_event(coredev, BOARD_EVENT_MULTIPLEX_ERRORS);
111 break;
113 default:
114 sms_err("Unknown dvb3 api event");
115 break;
119 static int smsdvb_onresponse(void *context, struct smscore_buffer_t *cb)
121 struct smsdvb_client_t *client = (struct smsdvb_client_t *) context;
122 struct SmsMsgHdr_ST *phdr = (struct SmsMsgHdr_ST *) (((u8 *) cb->p)
123 + cb->offset);
124 u32 *pMsgData = (u32 *) phdr + 1;
125 /*u32 MsgDataLen = phdr->msgLength - sizeof(struct SmsMsgHdr_ST);*/
126 bool is_status_update = false;
128 smsendian_handle_rx_message((struct SmsMsgData_ST *) phdr);
130 switch (phdr->msgType) {
131 case MSG_SMS_DVBT_BDA_DATA:
132 dvb_dmx_swfilter(&client->demux, (u8 *)(phdr + 1),
133 cb->size - sizeof(struct SmsMsgHdr_ST));
134 break;
136 case MSG_SMS_RF_TUNE_RES:
137 complete(&client->tune_done);
138 break;
140 case MSG_SMS_SIGNAL_DETECTED_IND:
141 sms_info("MSG_SMS_SIGNAL_DETECTED_IND");
142 client->sms_stat_dvb.TransmissionData.IsDemodLocked = true;
143 is_status_update = true;
144 break;
146 case MSG_SMS_NO_SIGNAL_IND:
147 sms_info("MSG_SMS_NO_SIGNAL_IND");
148 client->sms_stat_dvb.TransmissionData.IsDemodLocked = false;
149 is_status_update = true;
150 break;
152 case MSG_SMS_TRANSMISSION_IND: {
153 sms_info("MSG_SMS_TRANSMISSION_IND");
155 pMsgData++;
156 memcpy(&client->sms_stat_dvb.TransmissionData, pMsgData,
157 sizeof(struct TRANSMISSION_STATISTICS_S));
159 /* Mo need to correct guard interval
160 * (as opposed to old statistics message).
162 CORRECT_STAT_BANDWIDTH(client->sms_stat_dvb.TransmissionData);
163 CORRECT_STAT_TRANSMISSON_MODE(
164 client->sms_stat_dvb.TransmissionData);
165 is_status_update = true;
166 break;
168 case MSG_SMS_HO_PER_SLICES_IND: {
169 struct RECEPTION_STATISTICS_S *pReceptionData =
170 &client->sms_stat_dvb.ReceptionData;
171 struct SRVM_SIGNAL_STATUS_S SignalStatusData;
173 /*sms_info("MSG_SMS_HO_PER_SLICES_IND");*/
174 pMsgData++;
175 SignalStatusData.result = pMsgData[0];
176 SignalStatusData.snr = pMsgData[1];
177 SignalStatusData.inBandPower = (s32) pMsgData[2];
178 SignalStatusData.tsPackets = pMsgData[3];
179 SignalStatusData.etsPackets = pMsgData[4];
180 SignalStatusData.constellation = pMsgData[5];
181 SignalStatusData.hpCode = pMsgData[6];
182 SignalStatusData.tpsSrvIndLP = pMsgData[7] & 0x03;
183 SignalStatusData.tpsSrvIndHP = pMsgData[8] & 0x03;
184 SignalStatusData.cellId = pMsgData[9] & 0xFFFF;
185 SignalStatusData.reason = pMsgData[10];
186 SignalStatusData.requestId = pMsgData[11];
187 pReceptionData->IsRfLocked = pMsgData[16];
188 pReceptionData->IsDemodLocked = pMsgData[17];
189 pReceptionData->ModemState = pMsgData[12];
190 pReceptionData->SNR = pMsgData[1];
191 pReceptionData->BER = pMsgData[13];
192 pReceptionData->RSSI = pMsgData[14];
193 CORRECT_STAT_RSSI(client->sms_stat_dvb.ReceptionData);
195 pReceptionData->InBandPwr = (s32) pMsgData[2];
196 pReceptionData->CarrierOffset = (s32) pMsgData[15];
197 pReceptionData->TotalTSPackets = pMsgData[3];
198 pReceptionData->ErrorTSPackets = pMsgData[4];
200 /* TS PER */
201 if ((SignalStatusData.tsPackets + SignalStatusData.etsPackets)
202 > 0) {
203 pReceptionData->TS_PER = (SignalStatusData.etsPackets
204 * 100) / (SignalStatusData.tsPackets
205 + SignalStatusData.etsPackets);
206 } else {
207 pReceptionData->TS_PER = 0;
210 pReceptionData->BERBitCount = pMsgData[18];
211 pReceptionData->BERErrorCount = pMsgData[19];
213 pReceptionData->MRC_SNR = pMsgData[20];
214 pReceptionData->MRC_InBandPwr = pMsgData[21];
215 pReceptionData->MRC_RSSI = pMsgData[22];
217 is_status_update = true;
218 break;
221 smscore_putbuffer(client->coredev, cb);
223 if (is_status_update) {
224 if (client->sms_stat_dvb.ReceptionData.IsDemodLocked) {
225 client->fe_status = FE_HAS_SIGNAL | FE_HAS_CARRIER
226 | FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
227 sms_board_dvb3_event(client, DVB3_EVENT_FE_LOCK);
228 if (client->sms_stat_dvb.ReceptionData.ErrorTSPackets
229 == 0)
230 sms_board_dvb3_event(client, DVB3_EVENT_UNC_OK);
231 else
232 sms_board_dvb3_event(client,
233 DVB3_EVENT_UNC_ERR);
235 } else {
236 /*client->fe_status =
237 (phdr->msgType == MSG_SMS_NO_SIGNAL_IND) ?
238 0 : FE_HAS_SIGNAL;*/
239 client->fe_status = 0;
240 sms_board_dvb3_event(client, DVB3_EVENT_FE_UNLOCK);
244 return 0;
247 static void smsdvb_unregister_client(struct smsdvb_client_t *client)
249 /* must be called under clientslock */
251 list_del(&client->entry);
253 smscore_unregister_client(client->smsclient);
254 dvb_unregister_frontend(&client->frontend);
255 dvb_dmxdev_release(&client->dmxdev);
256 dvb_dmx_release(&client->demux);
257 dvb_unregister_adapter(&client->adapter);
258 kfree(client);
261 static void smsdvb_onremove(void *context)
263 kmutex_lock(&g_smsdvb_clientslock);
265 smsdvb_unregister_client((struct smsdvb_client_t *) context);
267 kmutex_unlock(&g_smsdvb_clientslock);
270 static int smsdvb_start_feed(struct dvb_demux_feed *feed)
272 struct smsdvb_client_t *client =
273 container_of(feed->demux, struct smsdvb_client_t, demux);
274 struct SmsMsgData_ST PidMsg;
276 sms_debug("add pid %d(%x)",
277 feed->pid, feed->pid);
279 PidMsg.xMsgHeader.msgSrcId = DVBT_BDA_CONTROL_MSG_ID;
280 PidMsg.xMsgHeader.msgDstId = HIF_TASK;
281 PidMsg.xMsgHeader.msgFlags = 0;
282 PidMsg.xMsgHeader.msgType = MSG_SMS_ADD_PID_FILTER_REQ;
283 PidMsg.xMsgHeader.msgLength = sizeof(PidMsg);
284 PidMsg.msgData[0] = feed->pid;
286 smsendian_handle_tx_message((struct SmsMsgHdr_ST *)&PidMsg);
287 return smsclient_sendrequest(client->smsclient,
288 &PidMsg, sizeof(PidMsg));
291 static int smsdvb_stop_feed(struct dvb_demux_feed *feed)
293 struct smsdvb_client_t *client =
294 container_of(feed->demux, struct smsdvb_client_t, demux);
295 struct SmsMsgData_ST PidMsg;
297 sms_debug("remove pid %d(%x)",
298 feed->pid, feed->pid);
300 PidMsg.xMsgHeader.msgSrcId = DVBT_BDA_CONTROL_MSG_ID;
301 PidMsg.xMsgHeader.msgDstId = HIF_TASK;
302 PidMsg.xMsgHeader.msgFlags = 0;
303 PidMsg.xMsgHeader.msgType = MSG_SMS_REMOVE_PID_FILTER_REQ;
304 PidMsg.xMsgHeader.msgLength = sizeof(PidMsg);
305 PidMsg.msgData[0] = feed->pid;
307 smsendian_handle_tx_message((struct SmsMsgHdr_ST *)&PidMsg);
308 return smsclient_sendrequest(client->smsclient,
309 &PidMsg, sizeof(PidMsg));
312 static int smsdvb_sendrequest_and_wait(struct smsdvb_client_t *client,
313 void *buffer, size_t size,
314 struct completion *completion)
316 int rc;
318 smsendian_handle_tx_message((struct SmsMsgHdr_ST *)buffer);
319 rc = smsclient_sendrequest(client->smsclient, buffer, size);
320 if (rc < 0)
321 return rc;
323 return wait_for_completion_timeout(completion,
324 msecs_to_jiffies(2000)) ?
325 0 : -ETIME;
328 static int smsdvb_read_status(struct dvb_frontend *fe, fe_status_t *stat)
330 struct smsdvb_client_t *client;
331 client = container_of(fe, struct smsdvb_client_t, frontend);
333 *stat = client->fe_status;
335 return 0;
338 static int smsdvb_read_ber(struct dvb_frontend *fe, u32 *ber)
340 struct smsdvb_client_t *client;
341 client = container_of(fe, struct smsdvb_client_t, frontend);
343 *ber = client->sms_stat_dvb.ReceptionData.BER;
345 return 0;
348 static int smsdvb_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
350 struct smsdvb_client_t *client;
351 client = container_of(fe, struct smsdvb_client_t, frontend);
353 if (client->sms_stat_dvb.ReceptionData.InBandPwr < -95)
354 *strength = 0;
355 else if (client->sms_stat_dvb.ReceptionData.InBandPwr > -29)
356 *strength = 100;
357 else
358 *strength =
359 (client->sms_stat_dvb.ReceptionData.InBandPwr
360 + 95) * 3 / 2;
362 return 0;
365 static int smsdvb_read_snr(struct dvb_frontend *fe, u16 *snr)
367 struct smsdvb_client_t *client;
368 client = container_of(fe, struct smsdvb_client_t, frontend);
370 *snr = client->sms_stat_dvb.ReceptionData.SNR;
372 return 0;
375 static int smsdvb_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
377 struct smsdvb_client_t *client;
378 client = container_of(fe, struct smsdvb_client_t, frontend);
380 *ucblocks = client->sms_stat_dvb.ReceptionData.ErrorTSPackets;
382 return 0;
385 static int smsdvb_get_tune_settings(struct dvb_frontend *fe,
386 struct dvb_frontend_tune_settings *tune)
388 sms_debug("");
390 tune->min_delay_ms = 400;
391 tune->step_size = 250000;
392 tune->max_drift = 0;
393 return 0;
396 static int smsdvb_set_frontend(struct dvb_frontend *fe,
397 struct dvb_frontend_parameters *fep)
399 struct smsdvb_client_t *client =
400 container_of(fe, struct smsdvb_client_t, frontend);
402 struct {
403 struct SmsMsgHdr_ST Msg;
404 u32 Data[3];
405 } Msg;
407 client->fe_status = FE_HAS_SIGNAL;
408 client->event_fe_state = -1;
409 client->event_unc_state = -1;
411 Msg.Msg.msgSrcId = DVBT_BDA_CONTROL_MSG_ID;
412 Msg.Msg.msgDstId = HIF_TASK;
413 Msg.Msg.msgFlags = 0;
414 Msg.Msg.msgType = MSG_SMS_RF_TUNE_REQ;
415 Msg.Msg.msgLength = sizeof(Msg);
416 Msg.Data[0] = fep->frequency;
417 Msg.Data[2] = 12000000;
419 sms_debug("freq %d band %d",
420 fep->frequency, fep->u.ofdm.bandwidth);
422 switch (fep->u.ofdm.bandwidth) {
423 case BANDWIDTH_8_MHZ: Msg.Data[1] = BW_8_MHZ; break;
424 case BANDWIDTH_7_MHZ: Msg.Data[1] = BW_7_MHZ; break;
425 case BANDWIDTH_6_MHZ: Msg.Data[1] = BW_6_MHZ; break;
426 case BANDWIDTH_AUTO: return -EOPNOTSUPP;
427 default: return -EINVAL;
430 return smsdvb_sendrequest_and_wait(client, &Msg, sizeof(Msg),
431 &client->tune_done);
434 static int smsdvb_get_frontend(struct dvb_frontend *fe,
435 struct dvb_frontend_parameters *fep)
437 struct smsdvb_client_t *client =
438 container_of(fe, struct smsdvb_client_t, frontend);
440 sms_debug("");
442 /* todo: */
443 memcpy(fep, &client->fe_params,
444 sizeof(struct dvb_frontend_parameters));
446 return 0;
449 static int smsdvb_init(struct dvb_frontend *fe)
451 struct smsdvb_client_t *client =
452 container_of(fe, struct smsdvb_client_t, frontend);
454 sms_board_dvb3_event(client, DVB3_EVENT_INIT);
455 return 0;
458 static int smsdvb_sleep(struct dvb_frontend *fe)
460 struct smsdvb_client_t *client =
461 container_of(fe, struct smsdvb_client_t, frontend);
463 sms_board_dvb3_event(client, DVB3_EVENT_SLEEP);
465 return 0;
468 static void smsdvb_release(struct dvb_frontend *fe)
470 /* do nothing */
473 static struct dvb_frontend_ops smsdvb_fe_ops = {
474 .info = {
475 .name = "Siano Mobile Digital MDTV Receiver",
476 .type = FE_OFDM,
477 .frequency_min = 44250000,
478 .frequency_max = 867250000,
479 .frequency_stepsize = 250000,
480 .caps = FE_CAN_INVERSION_AUTO |
481 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
482 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
483 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
484 FE_CAN_QAM_AUTO | FE_CAN_TRANSMISSION_MODE_AUTO |
485 FE_CAN_GUARD_INTERVAL_AUTO |
486 FE_CAN_RECOVER |
487 FE_CAN_HIERARCHY_AUTO,
490 .release = smsdvb_release,
492 .set_frontend = smsdvb_set_frontend,
493 .get_frontend = smsdvb_get_frontend,
494 .get_tune_settings = smsdvb_get_tune_settings,
496 .read_status = smsdvb_read_status,
497 .read_ber = smsdvb_read_ber,
498 .read_signal_strength = smsdvb_read_signal_strength,
499 .read_snr = smsdvb_read_snr,
500 .read_ucblocks = smsdvb_read_ucblocks,
502 .init = smsdvb_init,
503 .sleep = smsdvb_sleep,
506 static int smsdvb_hotplug(struct smscore_device_t *coredev,
507 struct device *device, int arrival)
509 struct smsclient_params_t params;
510 struct smsdvb_client_t *client;
511 int rc;
513 /* device removal handled by onremove callback */
514 if (!arrival)
515 return 0;
517 if (smscore_get_device_mode(coredev) != DEVICE_MODE_DVBT_BDA) {
518 sms_err("SMS Device mode is not set for "
519 "DVB operation.");
520 return 0;
523 client = kzalloc(sizeof(struct smsdvb_client_t), GFP_KERNEL);
524 if (!client) {
525 sms_err("kmalloc() failed");
526 return -ENOMEM;
529 /* register dvb adapter */
530 rc = dvb_register_adapter(&client->adapter,
531 sms_get_board(
532 smscore_get_board_id(coredev))->name,
533 THIS_MODULE, device, adapter_nr);
534 if (rc < 0) {
535 sms_err("dvb_register_adapter() failed %d", rc);
536 goto adapter_error;
539 /* init dvb demux */
540 client->demux.dmx.capabilities = DMX_TS_FILTERING;
541 client->demux.filternum = 32; /* todo: nova ??? */
542 client->demux.feednum = 32;
543 client->demux.start_feed = smsdvb_start_feed;
544 client->demux.stop_feed = smsdvb_stop_feed;
546 rc = dvb_dmx_init(&client->demux);
547 if (rc < 0) {
548 sms_err("dvb_dmx_init failed %d", rc);
549 goto dvbdmx_error;
552 /* init dmxdev */
553 client->dmxdev.filternum = 32;
554 client->dmxdev.demux = &client->demux.dmx;
555 client->dmxdev.capabilities = 0;
557 rc = dvb_dmxdev_init(&client->dmxdev, &client->adapter);
558 if (rc < 0) {
559 sms_err("dvb_dmxdev_init failed %d", rc);
560 goto dmxdev_error;
563 /* init and register frontend */
564 memcpy(&client->frontend.ops, &smsdvb_fe_ops,
565 sizeof(struct dvb_frontend_ops));
567 rc = dvb_register_frontend(&client->adapter, &client->frontend);
568 if (rc < 0) {
569 sms_err("frontend registration failed %d", rc);
570 goto frontend_error;
573 params.initial_id = 1;
574 params.data_type = MSG_SMS_DVBT_BDA_DATA;
575 params.onresponse_handler = smsdvb_onresponse;
576 params.onremove_handler = smsdvb_onremove;
577 params.context = client;
579 rc = smscore_register_client(coredev, &params, &client->smsclient);
580 if (rc < 0) {
581 sms_err("smscore_register_client() failed %d", rc);
582 goto client_error;
585 client->coredev = coredev;
587 init_completion(&client->tune_done);
589 kmutex_lock(&g_smsdvb_clientslock);
591 list_add(&client->entry, &g_smsdvb_clients);
593 kmutex_unlock(&g_smsdvb_clientslock);
595 client->event_fe_state = -1;
596 client->event_unc_state = -1;
597 sms_board_dvb3_event(client, DVB3_EVENT_HOTPLUG);
599 sms_info("success");
600 sms_board_setup(coredev);
602 return 0;
604 client_error:
605 dvb_unregister_frontend(&client->frontend);
607 frontend_error:
608 dvb_dmxdev_release(&client->dmxdev);
610 dmxdev_error:
611 dvb_dmx_release(&client->demux);
613 dvbdmx_error:
614 dvb_unregister_adapter(&client->adapter);
616 adapter_error:
617 kfree(client);
618 return rc;
621 int smsdvb_module_init(void)
623 int rc;
625 INIT_LIST_HEAD(&g_smsdvb_clients);
626 kmutex_init(&g_smsdvb_clientslock);
628 rc = smscore_register_hotplug(smsdvb_hotplug);
630 sms_debug("");
632 return rc;
635 void smsdvb_module_exit(void)
637 smscore_unregister_hotplug(smsdvb_hotplug);
639 kmutex_lock(&g_smsdvb_clientslock);
641 while (!list_empty(&g_smsdvb_clients))
642 smsdvb_unregister_client(
643 (struct smsdvb_client_t *) g_smsdvb_clients.next);
645 kmutex_unlock(&g_smsdvb_clientslock);
648 module_init(smsdvb_module_init);
649 module_exit(smsdvb_module_exit);
651 MODULE_DESCRIPTION("SMS DVB subsystem adaptation module");
652 MODULE_AUTHOR("Siano Mobile Silicon, Inc. (uris@siano-ms.com)");
653 MODULE_LICENSE("GPL");