V4L/DVB (11813): Siano: move dvb-api headers' includes to dvb adapter
[linux-2.6] / drivers / media / dvb / siano / smsdvb.c
1 /****************************************************************
2
3 Siano Mobile Silicon, Inc.
4 MDTV receiver kernel modules.
5 Copyright (C) 2006-2008, Uri Shkolnik
6
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.
11
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.
16
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/>.
19
20 ****************************************************************/
21
22 #include <linux/module.h>
23 #include <linux/init.h>
24
25 #include "dmxdev.h"
26 #include "dvbdev.h"
27 #include "dvb_demux.h"
28 #include "dvb_frontend.h"
29
30 #include "smscoreapi.h"
31 #include "smsendian.h"
32 #include "sms-cards.h"
33
34 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
35
36 struct smsdvb_client_t {
37         struct list_head entry;
38
39         struct smscore_device_t *coredev;
40         struct smscore_client_t *smsclient;
41
42         struct dvb_adapter      adapter;
43         struct dvb_demux        demux;
44         struct dmxdev           dmxdev;
45         struct dvb_frontend     frontend;
46
47         fe_status_t             fe_status;
48
49         struct completion       tune_done;
50
51         /* todo: save freq/band instead whole struct */
52         struct dvb_frontend_parameters fe_params;
53
54         struct SMSHOSTLIB_STATISTICS_DVB_S sms_stat_dvb;
55         int event_fe_state;
56         int event_unc_state;
57 };
58
59 static struct list_head g_smsdvb_clients;
60 static struct mutex g_smsdvb_clientslock;
61
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))");
65
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) {
69 }
70
71 static int smsdvb_onresponse(void *context, struct smscore_buffer_t *cb)
72 {
73         struct smsdvb_client_t *client = (struct smsdvb_client_t *) context;
74         struct SmsMsgHdr_ST *phdr = (struct SmsMsgHdr_ST *) (((u8 *) cb->p)
75                         + cb->offset);
76         u32 *pMsgData = (u32 *) phdr + 1;
77         /*u32 MsgDataLen = phdr->msgLength - sizeof(struct SmsMsgHdr_ST);*/
78         bool is_status_update = false;
79
80         smsendian_handle_rx_message((struct SmsMsgData_ST *) phdr);
81
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));
86                 break;
87
88         case MSG_SMS_RF_TUNE_RES:
89                 complete(&client->tune_done);
90                 break;
91
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;
96                 break;
97
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;
102                 break;
103
104         case MSG_SMS_TRANSMISSION_IND: {
105                 sms_info("MSG_SMS_TRANSMISSION_IND");
106
107                 pMsgData++;
108                 memcpy(&client->sms_stat_dvb.TransmissionData, pMsgData,
109                                 sizeof(struct TRANSMISSION_STATISTICS_S));
110
111                 /* Mo need to correct guard interval
112                  * (as opposed to old statistics message).
113                  */
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;
118                 break;
119         }
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;
124
125                 /*sms_info("MSG_SMS_HO_PER_SLICES_IND");*/
126                 pMsgData++;
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);
146
147                 pReceptionData->InBandPwr = (s32) pMsgData[2];
148                 pReceptionData->CarrierOffset = (s32) pMsgData[15];
149                 pReceptionData->TotalTSPackets = pMsgData[3];
150                 pReceptionData->ErrorTSPackets = pMsgData[4];
151
152                 /* TS PER */
153                 if ((SignalStatusData.tsPackets + SignalStatusData.etsPackets)
154                                 > 0) {
155                         pReceptionData->TS_PER = (SignalStatusData.etsPackets
156                                         * 100) / (SignalStatusData.tsPackets
157                                         + SignalStatusData.etsPackets);
158                 } else {
159                         pReceptionData->TS_PER = 0;
160                 }
161
162                 pReceptionData->BERBitCount = pMsgData[18];
163                 pReceptionData->BERErrorCount = pMsgData[19];
164
165                 pReceptionData->MRC_SNR = pMsgData[20];
166                 pReceptionData->MRC_InBandPwr = pMsgData[21];
167                 pReceptionData->MRC_RSSI = pMsgData[22];
168
169                 is_status_update = true;
170                 break;
171         }
172         }
173         smscore_putbuffer(client->coredev, cb);
174
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
181                                         == 0)
182                                 sms_board_dvb3_event(client, DVB3_EVENT_UNC_OK);
183                         else
184                                 sms_board_dvb3_event(client,
185                                                 DVB3_EVENT_UNC_ERR);
186
187                 } else {
188                         /*client->fe_status =
189                                 (phdr->msgType == MSG_SMS_NO_SIGNAL_IND) ?
190                                 0 : FE_HAS_SIGNAL;*/
191                         client->fe_status = 0;
192                         sms_board_dvb3_event(client, DVB3_EVENT_FE_UNLOCK);
193                 }
194         }
195
196         return 0;
197 }
198
199 static void smsdvb_unregister_client(struct smsdvb_client_t *client)
200 {
201         /* must be called under clientslock */
202
203         list_del(&client->entry);
204
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);
210         kfree(client);
211 }
212
213 static void smsdvb_onremove(void *context)
214 {
215         kmutex_lock(&g_smsdvb_clientslock);
216
217         smsdvb_unregister_client((struct smsdvb_client_t *) context);
218
219         kmutex_unlock(&g_smsdvb_clientslock);
220 }
221
222 static int smsdvb_start_feed(struct dvb_demux_feed *feed)
223 {
224         struct smsdvb_client_t *client =
225                 container_of(feed->demux, struct smsdvb_client_t, demux);
226         struct SmsMsgData_ST PidMsg;
227
228         sms_debug("add pid %d(%x)",
229                   feed->pid, feed->pid);
230
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;
237
238         smsendian_handle_tx_message((struct SmsMsgHdr_ST *)&PidMsg);
239         return smsclient_sendrequest(client->smsclient,
240                                      &PidMsg, sizeof(PidMsg));
241 }
242
243 static int smsdvb_stop_feed(struct dvb_demux_feed *feed)
244 {
245         struct smsdvb_client_t *client =
246                 container_of(feed->demux, struct smsdvb_client_t, demux);
247         struct SmsMsgData_ST PidMsg;
248
249         sms_debug("remove pid %d(%x)",
250                   feed->pid, feed->pid);
251
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;
258
259         smsendian_handle_tx_message((struct SmsMsgHdr_ST *)&PidMsg);
260         return smsclient_sendrequest(client->smsclient,
261                                      &PidMsg, sizeof(PidMsg));
262 }
263
264 static int smsdvb_sendrequest_and_wait(struct smsdvb_client_t *client,
265                                         void *buffer, size_t size,
266                                         struct completion *completion)
267 {
268         int rc;
269
270         smsendian_handle_tx_message((struct SmsMsgHdr_ST *)buffer);
271         rc = smsclient_sendrequest(client->smsclient, buffer, size);
272         if (rc < 0)
273                 return rc;
274
275         return wait_for_completion_timeout(completion,
276                                            msecs_to_jiffies(2000)) ?
277                                                 0 : -ETIME;
278 }
279
280 static int smsdvb_read_status(struct dvb_frontend *fe, fe_status_t *stat)
281 {
282         struct smsdvb_client_t *client;
283         client = container_of(fe, struct smsdvb_client_t, frontend);
284
285         *stat = client->fe_status;
286
287         return 0;
288 }
289
290 static int smsdvb_read_ber(struct dvb_frontend *fe, u32 *ber)
291 {
292         struct smsdvb_client_t *client;
293         client = container_of(fe, struct smsdvb_client_t, frontend);
294
295         *ber = client->sms_stat_dvb.ReceptionData.BER;
296
297         return 0;
298 }
299
300 static int smsdvb_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
301 {
302         struct smsdvb_client_t *client;
303         client = container_of(fe, struct smsdvb_client_t, frontend);
304
305         if (client->sms_stat_dvb.ReceptionData.InBandPwr < -95)
306                 *strength = 0;
307                 else if (client->sms_stat_dvb.ReceptionData.InBandPwr > -29)
308                         *strength = 100;
309                 else
310                         *strength =
311                                 (client->sms_stat_dvb.ReceptionData.InBandPwr
312                                 + 95) * 3 / 2;
313
314         return 0;
315 }
316
317 static int smsdvb_read_snr(struct dvb_frontend *fe, u16 *snr)
318 {
319         struct smsdvb_client_t *client;
320         client = container_of(fe, struct smsdvb_client_t, frontend);
321
322         *snr = client->sms_stat_dvb.ReceptionData.SNR;
323
324         return 0;
325 }
326
327 static int smsdvb_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
328 {
329         struct smsdvb_client_t *client;
330         client = container_of(fe, struct smsdvb_client_t, frontend);
331
332         *ucblocks = client->sms_stat_dvb.ReceptionData.ErrorTSPackets;
333
334         return 0;
335 }
336
337 static int smsdvb_get_tune_settings(struct dvb_frontend *fe,
338                                     struct dvb_frontend_tune_settings *tune)
339 {
340         sms_debug("");
341
342         tune->min_delay_ms = 400;
343         tune->step_size = 250000;
344         tune->max_drift = 0;
345         return 0;
346 }
347
348 static int smsdvb_set_frontend(struct dvb_frontend *fe,
349                                struct dvb_frontend_parameters *fep)
350 {
351         struct smsdvb_client_t *client =
352                 container_of(fe, struct smsdvb_client_t, frontend);
353
354         struct {
355                 struct SmsMsgHdr_ST     Msg;
356                 u32             Data[3];
357         } Msg;
358
359         client->fe_status = FE_HAS_SIGNAL;
360         client->event_fe_state = -1;
361         client->event_unc_state = -1;
362
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;
370
371         sms_debug("freq %d band %d",
372                   fep->frequency, fep->u.ofdm.bandwidth);
373
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;
380         }
381
382         return smsdvb_sendrequest_and_wait(client, &Msg, sizeof(Msg),
383                                            &client->tune_done);
384 }
385
386 static int smsdvb_get_frontend(struct dvb_frontend *fe,
387                                struct dvb_frontend_parameters *fep)
388 {
389         struct smsdvb_client_t *client =
390                 container_of(fe, struct smsdvb_client_t, frontend);
391
392         sms_debug("");
393
394         /* todo: */
395         memcpy(fep, &client->fe_params,
396                sizeof(struct dvb_frontend_parameters));
397
398         return 0;
399 }
400
401 static int smsdvb_init(struct dvb_frontend *fe)
402 {
403         struct smsdvb_client_t *client =
404                 container_of(fe, struct smsdvb_client_t, frontend);
405
406         sms_board_dvb3_event(client, DVB3_EVENT_INIT);
407         return 0;
408 }
409
410 static int smsdvb_sleep(struct dvb_frontend *fe)
411 {
412         struct smsdvb_client_t *client =
413                 container_of(fe, struct smsdvb_client_t, frontend);
414
415         sms_board_dvb3_event(client, DVB3_EVENT_SLEEP);
416
417         return 0;
418 }
419
420 static void smsdvb_release(struct dvb_frontend *fe)
421 {
422         /* do nothing */
423 }
424
425 static struct dvb_frontend_ops smsdvb_fe_ops = {
426         .info = {
427                 .name                   = "Siano Mobile Digital MDTV Receiver",
428                 .type                   = FE_OFDM,
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 |
438                         FE_CAN_RECOVER |
439                         FE_CAN_HIERARCHY_AUTO,
440         },
441
442         .release = smsdvb_release,
443
444         .set_frontend = smsdvb_set_frontend,
445         .get_frontend = smsdvb_get_frontend,
446         .get_tune_settings = smsdvb_get_tune_settings,
447
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,
453
454         .init = smsdvb_init,
455         .sleep = smsdvb_sleep,
456 };
457
458 static int smsdvb_hotplug(struct smscore_device_t *coredev,
459                           struct device *device, int arrival)
460 {
461         struct smsclient_params_t params;
462         struct smsdvb_client_t *client;
463         int rc;
464
465         /* device removal handled by onremove callback */
466         if (!arrival)
467                 return 0;
468
469         if (smscore_get_device_mode(coredev) != DEVICE_MODE_DVBT_BDA) {
470                 sms_err("SMS Device mode is not set for "
471                         "DVB operation.");
472                 return 0;
473         }
474
475         client = kzalloc(sizeof(struct smsdvb_client_t), GFP_KERNEL);
476         if (!client) {
477                 sms_err("kmalloc() failed");
478                 return -ENOMEM;
479         }
480
481         /* register dvb adapter */
482         rc = dvb_register_adapter(&client->adapter,
483                                   sms_get_board(
484                                         smscore_get_board_id(coredev))->name,
485                                   THIS_MODULE, device, adapter_nr);
486         if (rc < 0) {
487                 sms_err("dvb_register_adapter() failed %d", rc);
488                 goto adapter_error;
489         }
490
491         /* init dvb demux */
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;
497
498         rc = dvb_dmx_init(&client->demux);
499         if (rc < 0) {
500                 sms_err("dvb_dmx_init failed %d", rc);
501                 goto dvbdmx_error;
502         }
503
504         /* init dmxdev */
505         client->dmxdev.filternum = 32;
506         client->dmxdev.demux = &client->demux.dmx;
507         client->dmxdev.capabilities = 0;
508
509         rc = dvb_dmxdev_init(&client->dmxdev, &client->adapter);
510         if (rc < 0) {
511                 sms_err("dvb_dmxdev_init failed %d", rc);
512                 goto dmxdev_error;
513         }
514
515         /* init and register frontend */
516         memcpy(&client->frontend.ops, &smsdvb_fe_ops,
517                sizeof(struct dvb_frontend_ops));
518
519         rc = dvb_register_frontend(&client->adapter, &client->frontend);
520         if (rc < 0) {
521                 sms_err("frontend registration failed %d", rc);
522                 goto frontend_error;
523         }
524
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;
530
531         rc = smscore_register_client(coredev, &params, &client->smsclient);
532         if (rc < 0) {
533                 sms_err("smscore_register_client() failed %d", rc);
534                 goto client_error;
535         }
536
537         client->coredev = coredev;
538
539         init_completion(&client->tune_done);
540
541         kmutex_lock(&g_smsdvb_clientslock);
542
543         list_add(&client->entry, &g_smsdvb_clients);
544
545         kmutex_unlock(&g_smsdvb_clientslock);
546
547         client->event_fe_state = -1;
548         client->event_unc_state = -1;
549         sms_board_dvb3_event(client, DVB3_EVENT_HOTPLUG);
550
551         sms_info("success");
552         sms_board_setup(coredev);
553
554         return 0;
555
556 client_error:
557         dvb_unregister_frontend(&client->frontend);
558
559 frontend_error:
560         dvb_dmxdev_release(&client->dmxdev);
561
562 dmxdev_error:
563         dvb_dmx_release(&client->demux);
564
565 dvbdmx_error:
566         dvb_unregister_adapter(&client->adapter);
567
568 adapter_error:
569         kfree(client);
570         return rc;
571 }
572
573 int smsdvb_module_init(void)
574 {
575         int rc;
576
577         INIT_LIST_HEAD(&g_smsdvb_clients);
578         kmutex_init(&g_smsdvb_clientslock);
579
580         rc = smscore_register_hotplug(smsdvb_hotplug);
581
582         sms_debug("");
583
584         return rc;
585 }
586
587 void smsdvb_module_exit(void)
588 {
589         smscore_unregister_hotplug(smsdvb_hotplug);
590
591         kmutex_lock(&g_smsdvb_clientslock);
592
593         while (!list_empty(&g_smsdvb_clients))
594                smsdvb_unregister_client(
595                         (struct smsdvb_client_t *) g_smsdvb_clients.next);
596
597         kmutex_unlock(&g_smsdvb_clientslock);
598 }
599
600 module_init(smsdvb_module_init);
601 module_exit(smsdvb_module_exit);
602
603 MODULE_DESCRIPTION("SMS DVB subsystem adaptation module");
604 MODULE_AUTHOR("Siano Mobile Silicon, Inc. (uris@siano-ms.com)");
605 MODULE_LICENSE("GPL");