2 * Driver for the Siano SMS1xxx USB dongle
4 * author: Anatoly Greenblat
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;
53 static int smsdvb_onresponse(void *context, struct smscore_buffer_t *cb)
55 struct smsdvb_client_t *client = (struct smsdvb_client_t *) context;
56 struct SmsMsgHdr_ST *phdr =
57 (struct SmsMsgHdr_ST *)(((u8 *) cb->p) + cb->offset);
59 switch (phdr->msgType) {
60 case MSG_SMS_DVBT_BDA_DATA:
61 dvb_dmx_swfilter(&client->demux, (u8 *)(phdr + 1),
62 cb->size - sizeof(struct SmsMsgHdr_ST));
65 case MSG_SMS_RF_TUNE_RES:
66 complete(&client->tune_done);
69 case MSG_SMS_GET_STATISTICS_RES:
71 struct SmsMsgStatisticsInfo_ST *p =
72 (struct SmsMsgStatisticsInfo_ST *)(phdr + 1);
74 if (p->Stat.IsDemodLocked) {
75 client->fe_status = FE_HAS_SIGNAL |
81 client->fe_snr = p->Stat.SNR;
82 client->fe_ber = p->Stat.BER;
83 client->fe_unc = p->Stat.BERErrorCount;
85 if (p->Stat.InBandPwr < -95)
86 client->fe_signal_strength = 0;
87 else if (p->Stat.InBandPwr > -29)
88 client->fe_signal_strength = 100;
90 client->fe_signal_strength =
91 (p->Stat.InBandPwr + 95) * 3 / 2;
93 client->fe_status = 0;
97 client->fe_signal_strength = 0;
100 complete(&client->stat_done);
104 smscore_putbuffer(client->coredev, cb);
109 static void smsdvb_unregister_client(struct smsdvb_client_t *client)
111 /* must be called under clientslock */
113 list_del(&client->entry);
115 smscore_unregister_client(client->smsclient);
116 dvb_unregister_frontend(&client->frontend);
117 dvb_dmxdev_release(&client->dmxdev);
118 dvb_dmx_release(&client->demux);
119 dvb_unregister_adapter(&client->adapter);
123 static void smsdvb_onremove(void *context)
125 kmutex_lock(&g_smsdvb_clientslock);
127 smsdvb_unregister_client((struct smsdvb_client_t *) context);
129 kmutex_unlock(&g_smsdvb_clientslock);
132 static int smsdvb_start_feed(struct dvb_demux_feed *feed)
134 struct smsdvb_client_t *client =
135 container_of(feed->demux, struct smsdvb_client_t, demux);
136 struct SmsMsgData_ST PidMsg;
138 sms_debug("add pid %d(%x)",
139 feed->pid, feed->pid);
141 PidMsg.xMsgHeader.msgSrcId = DVBT_BDA_CONTROL_MSG_ID;
142 PidMsg.xMsgHeader.msgDstId = HIF_TASK;
143 PidMsg.xMsgHeader.msgFlags = 0;
144 PidMsg.xMsgHeader.msgType = MSG_SMS_ADD_PID_FILTER_REQ;
145 PidMsg.xMsgHeader.msgLength = sizeof(PidMsg);
146 PidMsg.msgData[0] = feed->pid;
148 return smsclient_sendrequest(client->smsclient,
149 &PidMsg, sizeof(PidMsg));
152 static int smsdvb_stop_feed(struct dvb_demux_feed *feed)
154 struct smsdvb_client_t *client =
155 container_of(feed->demux, struct smsdvb_client_t, demux);
156 struct SmsMsgData_ST PidMsg;
158 sms_debug("remove pid %d(%x)",
159 feed->pid, feed->pid);
161 PidMsg.xMsgHeader.msgSrcId = DVBT_BDA_CONTROL_MSG_ID;
162 PidMsg.xMsgHeader.msgDstId = HIF_TASK;
163 PidMsg.xMsgHeader.msgFlags = 0;
164 PidMsg.xMsgHeader.msgType = MSG_SMS_REMOVE_PID_FILTER_REQ;
165 PidMsg.xMsgHeader.msgLength = sizeof(PidMsg);
166 PidMsg.msgData[0] = feed->pid;
168 return smsclient_sendrequest(client->smsclient,
169 &PidMsg, sizeof(PidMsg));
172 static int smsdvb_sendrequest_and_wait(struct smsdvb_client_t *client,
173 void *buffer, size_t size,
174 struct completion *completion)
176 int rc = smsclient_sendrequest(client->smsclient, buffer, size);
180 return wait_for_completion_timeout(completion,
181 msecs_to_jiffies(2000)) ?
185 static int smsdvb_send_statistics_request(struct smsdvb_client_t *client)
187 struct SmsMsgHdr_ST Msg = { MSG_SMS_GET_STATISTICS_REQ,
188 DVBT_BDA_CONTROL_MSG_ID,
189 HIF_TASK, sizeof(struct SmsMsgHdr_ST), 0 };
190 int ret = smsdvb_sendrequest_and_wait(client, &Msg, sizeof(Msg),
195 if (client->fe_status & FE_HAS_LOCK)
196 sms_board_led_feedback(client->coredev,
197 (client->fe_unc == 0) ?
198 SMS_LED_HI : SMS_LED_LO);
200 sms_board_led_feedback(client->coredev, SMS_LED_OFF);
204 static int smsdvb_read_status(struct dvb_frontend *fe, fe_status_t *stat)
206 struct smsdvb_client_t *client =
207 container_of(fe, struct smsdvb_client_t, frontend);
208 int rc = smsdvb_send_statistics_request(client);
211 *stat = client->fe_status;
216 static int smsdvb_read_ber(struct dvb_frontend *fe, u32 *ber)
218 struct smsdvb_client_t *client =
219 container_of(fe, struct smsdvb_client_t, frontend);
220 int rc = smsdvb_send_statistics_request(client);
223 *ber = client->fe_ber;
228 static int smsdvb_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
230 struct smsdvb_client_t *client =
231 container_of(fe, struct smsdvb_client_t, frontend);
232 int rc = smsdvb_send_statistics_request(client);
235 *strength = client->fe_signal_strength;
240 static int smsdvb_read_snr(struct dvb_frontend *fe, u16 *snr)
242 struct smsdvb_client_t *client =
243 container_of(fe, struct smsdvb_client_t, frontend);
244 int rc = smsdvb_send_statistics_request(client);
247 *snr = client->fe_snr;
252 static int smsdvb_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
254 struct smsdvb_client_t *client =
255 container_of(fe, struct smsdvb_client_t, frontend);
256 int rc = smsdvb_send_statistics_request(client);
259 *ucblocks = client->fe_unc;
264 static int smsdvb_get_tune_settings(struct dvb_frontend *fe,
265 struct dvb_frontend_tune_settings *tune)
269 tune->min_delay_ms = 400;
270 tune->step_size = 250000;
275 static int smsdvb_set_frontend(struct dvb_frontend *fe,
276 struct dvb_frontend_parameters *fep)
278 struct smsdvb_client_t *client =
279 container_of(fe, struct smsdvb_client_t, frontend);
282 struct SmsMsgHdr_ST Msg;
287 Msg.Msg.msgSrcId = DVBT_BDA_CONTROL_MSG_ID;
288 Msg.Msg.msgDstId = HIF_TASK;
289 Msg.Msg.msgFlags = 0;
290 Msg.Msg.msgType = MSG_SMS_RF_TUNE_REQ;
291 Msg.Msg.msgLength = sizeof(Msg);
292 Msg.Data[0] = fep->frequency;
293 Msg.Data[2] = 12000000;
295 sms_debug("freq %d band %d",
296 fep->frequency, fep->u.ofdm.bandwidth);
298 switch (fep->u.ofdm.bandwidth) {
299 case BANDWIDTH_8_MHZ: Msg.Data[1] = BW_8_MHZ; break;
300 case BANDWIDTH_7_MHZ: Msg.Data[1] = BW_7_MHZ; break;
301 case BANDWIDTH_6_MHZ: Msg.Data[1] = BW_6_MHZ; break;
302 case BANDWIDTH_AUTO: return -EOPNOTSUPP;
303 default: return -EINVAL;
306 /* Disable LNA, if any. An error is returned if no LNA is present */
307 ret = sms_board_lna_control(client->coredev, 0);
311 /* tune with LNA off at first */
312 ret = smsdvb_sendrequest_and_wait(client, &Msg, sizeof(Msg),
315 smsdvb_read_status(fe, &status);
317 if (status & FE_HAS_LOCK)
320 /* previous tune didnt lock - enable LNA and tune again */
321 sms_board_lna_control(client->coredev, 1);
324 return smsdvb_sendrequest_and_wait(client, &Msg, sizeof(Msg),
328 static int smsdvb_get_frontend(struct dvb_frontend *fe,
329 struct dvb_frontend_parameters *fep)
331 struct smsdvb_client_t *client =
332 container_of(fe, struct smsdvb_client_t, frontend);
337 memcpy(fep, &client->fe_params,
338 sizeof(struct dvb_frontend_parameters));
343 static int smsdvb_init(struct dvb_frontend *fe)
345 struct smsdvb_client_t *client =
346 container_of(fe, struct smsdvb_client_t, frontend);
348 sms_board_power(client->coredev, 1);
353 static int smsdvb_sleep(struct dvb_frontend *fe)
355 struct smsdvb_client_t *client =
356 container_of(fe, struct smsdvb_client_t, frontend);
358 sms_board_led_feedback(client->coredev, SMS_LED_OFF);
359 sms_board_power(client->coredev, 0);
364 static void smsdvb_release(struct dvb_frontend *fe)
369 static struct dvb_frontend_ops smsdvb_fe_ops = {
371 .name = "Siano Mobile Digital SMS1xxx",
373 .frequency_min = 44250000,
374 .frequency_max = 867250000,
375 .frequency_stepsize = 250000,
376 .caps = FE_CAN_INVERSION_AUTO |
377 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
378 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
379 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
380 FE_CAN_QAM_AUTO | FE_CAN_TRANSMISSION_MODE_AUTO |
381 FE_CAN_GUARD_INTERVAL_AUTO |
383 FE_CAN_HIERARCHY_AUTO,
386 .release = smsdvb_release,
388 .set_frontend = smsdvb_set_frontend,
389 .get_frontend = smsdvb_get_frontend,
390 .get_tune_settings = smsdvb_get_tune_settings,
392 .read_status = smsdvb_read_status,
393 .read_ber = smsdvb_read_ber,
394 .read_signal_strength = smsdvb_read_signal_strength,
395 .read_snr = smsdvb_read_snr,
396 .read_ucblocks = smsdvb_read_ucblocks,
399 .sleep = smsdvb_sleep,
402 static int smsdvb_hotplug(struct smscore_device_t *coredev,
403 struct device *device, int arrival)
405 struct smsclient_params_t params;
406 struct smsdvb_client_t *client;
409 /* device removal handled by onremove callback */
413 if (smscore_get_device_mode(coredev) != 4) {
414 sms_err("SMS Device mode is not set for "
419 client = kzalloc(sizeof(struct smsdvb_client_t), GFP_KERNEL);
421 sms_err("kmalloc() failed");
425 /* register dvb adapter */
426 rc = dvb_register_adapter(&client->adapter,
428 smscore_get_board_id(coredev))->name,
429 THIS_MODULE, device, adapter_nr);
431 sms_err("dvb_register_adapter() failed %d", rc);
436 client->demux.dmx.capabilities = DMX_TS_FILTERING;
437 client->demux.filternum = 32; /* todo: nova ??? */
438 client->demux.feednum = 32;
439 client->demux.start_feed = smsdvb_start_feed;
440 client->demux.stop_feed = smsdvb_stop_feed;
442 rc = dvb_dmx_init(&client->demux);
444 sms_err("dvb_dmx_init failed %d", rc);
449 client->dmxdev.filternum = 32;
450 client->dmxdev.demux = &client->demux.dmx;
451 client->dmxdev.capabilities = 0;
453 rc = dvb_dmxdev_init(&client->dmxdev, &client->adapter);
455 sms_err("dvb_dmxdev_init failed %d", rc);
459 /* init and register frontend */
460 memcpy(&client->frontend.ops, &smsdvb_fe_ops,
461 sizeof(struct dvb_frontend_ops));
463 rc = dvb_register_frontend(&client->adapter, &client->frontend);
465 sms_err("frontend registration failed %d", rc);
469 params.initial_id = 1;
470 params.data_type = MSG_SMS_DVBT_BDA_DATA;
471 params.onresponse_handler = smsdvb_onresponse;
472 params.onremove_handler = smsdvb_onremove;
473 params.context = client;
475 rc = smscore_register_client(coredev, ¶ms, &client->smsclient);
477 sms_err("smscore_register_client() failed %d", rc);
481 client->coredev = coredev;
483 init_completion(&client->tune_done);
484 init_completion(&client->stat_done);
486 kmutex_lock(&g_smsdvb_clientslock);
488 list_add(&client->entry, &g_smsdvb_clients);
490 kmutex_unlock(&g_smsdvb_clientslock);
494 sms_board_setup(coredev);
499 dvb_unregister_frontend(&client->frontend);
502 dvb_dmxdev_release(&client->dmxdev);
505 dvb_dmx_release(&client->demux);
508 dvb_unregister_adapter(&client->adapter);
515 int smsdvb_register(void)
519 INIT_LIST_HEAD(&g_smsdvb_clients);
520 kmutex_init(&g_smsdvb_clientslock);
522 rc = smscore_register_hotplug(smsdvb_hotplug);
529 void smsdvb_unregister(void)
531 smscore_unregister_hotplug(smsdvb_hotplug);
533 kmutex_lock(&g_smsdvb_clientslock);
535 while (!list_empty(&g_smsdvb_clients))
536 smsdvb_unregister_client(
537 (struct smsdvb_client_t *) g_smsdvb_clients.next);
539 kmutex_unlock(&g_smsdvb_clientslock);