2 * Driver for the Siano SMS1xxx USB dongle
6 * Copyright (c), 2005-2008 Siano Mobile Silicon, Inc.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation;
12 * Software distributed under the License is distributed on an "AS IS"
13 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
15 * See the 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, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/module.h>
23 #include <linux/init.h>
25 #include "smscoreapi.h"
26 #include "sms-cards.h"
28 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
30 struct smsdvb_client_t {
31 struct list_head entry;
33 struct smscore_device_t *coredev;
34 struct smscore_client_t *smsclient;
36 struct dvb_adapter adapter;
37 struct dvb_demux demux;
39 struct dvb_frontend frontend;
41 fe_status_t fe_status;
42 int fe_ber, fe_snr, fe_unc, fe_signal_strength;
44 struct completion tune_done, stat_done;
46 /* todo: save freq/band instead whole struct */
47 struct dvb_frontend_parameters fe_params;
50 static struct list_head g_smsdvb_clients;
51 static struct mutex g_smsdvb_clientslock;
54 module_param_named(debug, sms_dbg, int, 0644);
55 MODULE_PARM_DESC(debug, "set debug level (info=1, adv=2 (or-able))");
57 static int smsdvb_onresponse(void *context, struct smscore_buffer_t *cb)
59 struct smsdvb_client_t *client = (struct smsdvb_client_t *) context;
60 struct SmsMsgHdr_ST *phdr =
61 (struct SmsMsgHdr_ST *)(((u8 *) cb->p) + cb->offset);
63 switch (phdr->msgType) {
64 case MSG_SMS_DVBT_BDA_DATA:
65 dvb_dmx_swfilter(&client->demux, (u8 *)(phdr + 1),
66 cb->size - sizeof(struct SmsMsgHdr_ST));
69 case MSG_SMS_RF_TUNE_RES:
70 complete(&client->tune_done);
73 case MSG_SMS_GET_STATISTICS_RES:
75 struct SmsMsgStatisticsInfo_ST *p =
76 (struct SmsMsgStatisticsInfo_ST *)(phdr + 1);
78 if (p->Stat.IsDemodLocked) {
79 client->fe_status = FE_HAS_SIGNAL |
85 client->fe_snr = p->Stat.SNR;
86 client->fe_ber = p->Stat.BER;
87 client->fe_unc = p->Stat.BERErrorCount;
89 if (p->Stat.InBandPwr < -95)
90 client->fe_signal_strength = 0;
91 else if (p->Stat.InBandPwr > -29)
92 client->fe_signal_strength = 100;
94 client->fe_signal_strength =
95 (p->Stat.InBandPwr + 95) * 3 / 2;
97 client->fe_status = 0;
101 client->fe_signal_strength = 0;
104 complete(&client->stat_done);
108 smscore_putbuffer(client->coredev, cb);
113 static void smsdvb_unregister_client(struct smsdvb_client_t *client)
115 /* must be called under clientslock */
117 list_del(&client->entry);
119 smscore_unregister_client(client->smsclient);
120 dvb_unregister_frontend(&client->frontend);
121 dvb_dmxdev_release(&client->dmxdev);
122 dvb_dmx_release(&client->demux);
123 dvb_unregister_adapter(&client->adapter);
127 static void smsdvb_onremove(void *context)
129 kmutex_lock(&g_smsdvb_clientslock);
131 smsdvb_unregister_client((struct smsdvb_client_t *) context);
133 kmutex_unlock(&g_smsdvb_clientslock);
136 static int smsdvb_start_feed(struct dvb_demux_feed *feed)
138 struct smsdvb_client_t *client =
139 container_of(feed->demux, struct smsdvb_client_t, demux);
140 struct SmsMsgData_ST PidMsg;
142 sms_debug("add pid %d(%x)",
143 feed->pid, feed->pid);
145 PidMsg.xMsgHeader.msgSrcId = DVBT_BDA_CONTROL_MSG_ID;
146 PidMsg.xMsgHeader.msgDstId = HIF_TASK;
147 PidMsg.xMsgHeader.msgFlags = 0;
148 PidMsg.xMsgHeader.msgType = MSG_SMS_ADD_PID_FILTER_REQ;
149 PidMsg.xMsgHeader.msgLength = sizeof(PidMsg);
150 PidMsg.msgData[0] = feed->pid;
152 return smsclient_sendrequest(client->smsclient,
153 &PidMsg, sizeof(PidMsg));
156 static int smsdvb_stop_feed(struct dvb_demux_feed *feed)
158 struct smsdvb_client_t *client =
159 container_of(feed->demux, struct smsdvb_client_t, demux);
160 struct SmsMsgData_ST PidMsg;
162 sms_debug("remove pid %d(%x)",
163 feed->pid, feed->pid);
165 PidMsg.xMsgHeader.msgSrcId = DVBT_BDA_CONTROL_MSG_ID;
166 PidMsg.xMsgHeader.msgDstId = HIF_TASK;
167 PidMsg.xMsgHeader.msgFlags = 0;
168 PidMsg.xMsgHeader.msgType = MSG_SMS_REMOVE_PID_FILTER_REQ;
169 PidMsg.xMsgHeader.msgLength = sizeof(PidMsg);
170 PidMsg.msgData[0] = feed->pid;
172 return smsclient_sendrequest(client->smsclient,
173 &PidMsg, sizeof(PidMsg));
176 static int smsdvb_sendrequest_and_wait(struct smsdvb_client_t *client,
177 void *buffer, size_t size,
178 struct completion *completion)
180 int rc = smsclient_sendrequest(client->smsclient, buffer, size);
184 return wait_for_completion_timeout(completion,
185 msecs_to_jiffies(2000)) ?
189 static int smsdvb_send_statistics_request(struct smsdvb_client_t *client)
191 struct SmsMsgHdr_ST Msg = { MSG_SMS_GET_STATISTICS_REQ,
192 DVBT_BDA_CONTROL_MSG_ID,
193 HIF_TASK, sizeof(struct SmsMsgHdr_ST), 0 };
194 int ret = smsdvb_sendrequest_and_wait(client, &Msg, sizeof(Msg),
199 if (client->fe_status & FE_HAS_LOCK)
200 sms_board_led_feedback(client->coredev,
201 (client->fe_unc == 0) ?
202 SMS_LED_HI : SMS_LED_LO);
204 sms_board_led_feedback(client->coredev, SMS_LED_OFF);
208 static int smsdvb_read_status(struct dvb_frontend *fe, fe_status_t *stat)
210 struct smsdvb_client_t *client =
211 container_of(fe, struct smsdvb_client_t, frontend);
212 int rc = smsdvb_send_statistics_request(client);
215 *stat = client->fe_status;
220 static int smsdvb_read_ber(struct dvb_frontend *fe, u32 *ber)
222 struct smsdvb_client_t *client =
223 container_of(fe, struct smsdvb_client_t, frontend);
224 int rc = smsdvb_send_statistics_request(client);
227 *ber = client->fe_ber;
232 static int smsdvb_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
234 struct smsdvb_client_t *client =
235 container_of(fe, struct smsdvb_client_t, frontend);
236 int rc = smsdvb_send_statistics_request(client);
239 *strength = client->fe_signal_strength;
244 static int smsdvb_read_snr(struct dvb_frontend *fe, u16 *snr)
246 struct smsdvb_client_t *client =
247 container_of(fe, struct smsdvb_client_t, frontend);
248 int rc = smsdvb_send_statistics_request(client);
251 *snr = client->fe_snr;
256 static int smsdvb_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
258 struct smsdvb_client_t *client =
259 container_of(fe, struct smsdvb_client_t, frontend);
260 int rc = smsdvb_send_statistics_request(client);
263 *ucblocks = client->fe_unc;
268 static int smsdvb_get_tune_settings(struct dvb_frontend *fe,
269 struct dvb_frontend_tune_settings *tune)
273 tune->min_delay_ms = 400;
274 tune->step_size = 250000;
279 static int smsdvb_set_frontend(struct dvb_frontend *fe,
280 struct dvb_frontend_parameters *fep)
282 struct smsdvb_client_t *client =
283 container_of(fe, struct smsdvb_client_t, frontend);
286 struct SmsMsgHdr_ST Msg;
291 Msg.Msg.msgSrcId = DVBT_BDA_CONTROL_MSG_ID;
292 Msg.Msg.msgDstId = HIF_TASK;
293 Msg.Msg.msgFlags = 0;
294 Msg.Msg.msgType = MSG_SMS_RF_TUNE_REQ;
295 Msg.Msg.msgLength = sizeof(Msg);
296 Msg.Data[0] = fep->frequency;
297 Msg.Data[2] = 12000000;
299 sms_debug("freq %d band %d",
300 fep->frequency, fep->u.ofdm.bandwidth);
302 switch (fep->u.ofdm.bandwidth) {
303 case BANDWIDTH_8_MHZ: Msg.Data[1] = BW_8_MHZ; break;
304 case BANDWIDTH_7_MHZ: Msg.Data[1] = BW_7_MHZ; break;
305 case BANDWIDTH_6_MHZ: Msg.Data[1] = BW_6_MHZ; break;
306 case BANDWIDTH_AUTO: return -EOPNOTSUPP;
307 default: return -EINVAL;
310 /* Disable LNA, if any. An error is returned if no LNA is present */
311 ret = sms_board_lna_control(client->coredev, 0);
315 /* tune with LNA off at first */
316 ret = smsdvb_sendrequest_and_wait(client, &Msg, sizeof(Msg),
319 smsdvb_read_status(fe, &status);
321 if (status & FE_HAS_LOCK)
324 /* previous tune didnt lock - enable LNA and tune again */
325 sms_board_lna_control(client->coredev, 1);
328 return smsdvb_sendrequest_and_wait(client, &Msg, sizeof(Msg),
332 static int smsdvb_get_frontend(struct dvb_frontend *fe,
333 struct dvb_frontend_parameters *fep)
335 struct smsdvb_client_t *client =
336 container_of(fe, struct smsdvb_client_t, frontend);
341 memcpy(fep, &client->fe_params,
342 sizeof(struct dvb_frontend_parameters));
347 static int smsdvb_init(struct dvb_frontend *fe)
349 struct smsdvb_client_t *client =
350 container_of(fe, struct smsdvb_client_t, frontend);
352 sms_board_power(client->coredev, 1);
357 static int smsdvb_sleep(struct dvb_frontend *fe)
359 struct smsdvb_client_t *client =
360 container_of(fe, struct smsdvb_client_t, frontend);
362 sms_board_led_feedback(client->coredev, SMS_LED_OFF);
363 sms_board_power(client->coredev, 0);
368 static void smsdvb_release(struct dvb_frontend *fe)
373 static struct dvb_frontend_ops smsdvb_fe_ops = {
375 .name = "Siano Mobile Digital MDTV Receiver",
377 .frequency_min = 44250000,
378 .frequency_max = 867250000,
379 .frequency_stepsize = 250000,
380 .caps = FE_CAN_INVERSION_AUTO |
381 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
382 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
383 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
384 FE_CAN_QAM_AUTO | FE_CAN_TRANSMISSION_MODE_AUTO |
385 FE_CAN_GUARD_INTERVAL_AUTO |
387 FE_CAN_HIERARCHY_AUTO,
390 .release = smsdvb_release,
392 .set_frontend = smsdvb_set_frontend,
393 .get_frontend = smsdvb_get_frontend,
394 .get_tune_settings = smsdvb_get_tune_settings,
396 .read_status = smsdvb_read_status,
397 .read_ber = smsdvb_read_ber,
398 .read_signal_strength = smsdvb_read_signal_strength,
399 .read_snr = smsdvb_read_snr,
400 .read_ucblocks = smsdvb_read_ucblocks,
403 .sleep = smsdvb_sleep,
406 static int smsdvb_hotplug(struct smscore_device_t *coredev,
407 struct device *device, int arrival)
409 struct smsclient_params_t params;
410 struct smsdvb_client_t *client;
413 /* device removal handled by onremove callback */
417 if (smscore_get_device_mode(coredev) != DEVICE_MODE_DVBT_BDA) {
418 sms_err("SMS Device mode is not set for "
423 client = kzalloc(sizeof(struct smsdvb_client_t), GFP_KERNEL);
425 sms_err("kmalloc() failed");
429 /* register dvb adapter */
430 rc = dvb_register_adapter(&client->adapter,
432 smscore_get_board_id(coredev))->name,
433 THIS_MODULE, device, adapter_nr);
435 sms_err("dvb_register_adapter() failed %d", rc);
440 client->demux.dmx.capabilities = DMX_TS_FILTERING;
441 client->demux.filternum = 32; /* todo: nova ??? */
442 client->demux.feednum = 32;
443 client->demux.start_feed = smsdvb_start_feed;
444 client->demux.stop_feed = smsdvb_stop_feed;
446 rc = dvb_dmx_init(&client->demux);
448 sms_err("dvb_dmx_init failed %d", rc);
453 client->dmxdev.filternum = 32;
454 client->dmxdev.demux = &client->demux.dmx;
455 client->dmxdev.capabilities = 0;
457 rc = dvb_dmxdev_init(&client->dmxdev, &client->adapter);
459 sms_err("dvb_dmxdev_init failed %d", rc);
463 /* init and register frontend */
464 memcpy(&client->frontend.ops, &smsdvb_fe_ops,
465 sizeof(struct dvb_frontend_ops));
467 rc = dvb_register_frontend(&client->adapter, &client->frontend);
469 sms_err("frontend registration failed %d", rc);
473 params.initial_id = 1;
474 params.data_type = MSG_SMS_DVBT_BDA_DATA;
475 params.onresponse_handler = smsdvb_onresponse;
476 params.onremove_handler = smsdvb_onremove;
477 params.context = client;
479 rc = smscore_register_client(coredev, ¶ms, &client->smsclient);
481 sms_err("smscore_register_client() failed %d", rc);
485 client->coredev = coredev;
487 init_completion(&client->tune_done);
488 init_completion(&client->stat_done);
490 kmutex_lock(&g_smsdvb_clientslock);
492 list_add(&client->entry, &g_smsdvb_clients);
494 kmutex_unlock(&g_smsdvb_clientslock);
498 sms_board_setup(coredev);
503 dvb_unregister_frontend(&client->frontend);
506 dvb_dmxdev_release(&client->dmxdev);
509 dvb_dmx_release(&client->demux);
512 dvb_unregister_adapter(&client->adapter);
519 int smsdvb_module_init(void)
523 INIT_LIST_HEAD(&g_smsdvb_clients);
524 kmutex_init(&g_smsdvb_clientslock);
526 rc = smscore_register_hotplug(smsdvb_hotplug);
533 void smsdvb_module_exit(void)
535 smscore_unregister_hotplug(smsdvb_hotplug);
537 kmutex_lock(&g_smsdvb_clientslock);
539 while (!list_empty(&g_smsdvb_clients))
540 smsdvb_unregister_client(
541 (struct smsdvb_client_t *) g_smsdvb_clients.next);
543 kmutex_unlock(&g_smsdvb_clientslock);
546 module_init(smsdvb_module_init);
547 module_exit(smsdvb_module_exit);
549 MODULE_DESCRIPTION("SMS DVB subsystem adaptation module");
550 MODULE_AUTHOR("Siano Mobile Silicon, INC. (uris@siano-ms.com)");
551 MODULE_LICENSE("GPL");