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 static struct list_head g_smsdvb_clients;
31 static struct mutex g_smsdvb_clientslock;
33 static int smsdvb_onresponse(void *context, struct smscore_buffer_t *cb)
35 struct smsdvb_client_t *client = (struct smsdvb_client_t *) context;
36 struct SmsMsgHdr_ST *phdr =
37 (struct SmsMsgHdr_ST *)(((u8 *) cb->p) + cb->offset);
39 switch (phdr->msgType) {
40 case MSG_SMS_DVBT_BDA_DATA:
41 dvb_dmx_swfilter(&client->demux, (u8 *)(phdr + 1),
42 cb->size - sizeof(struct SmsMsgHdr_ST));
45 case MSG_SMS_RF_TUNE_RES:
46 complete(&client->tune_done);
49 case MSG_SMS_GET_STATISTICS_RES:
51 struct SmsMsgStatisticsInfo_ST *p =
52 (struct SmsMsgStatisticsInfo_ST *)(phdr + 1);
54 if (p->Stat.IsDemodLocked) {
55 client->fe_status = FE_HAS_SIGNAL |
61 client->fe_snr = p->Stat.SNR;
62 client->fe_ber = p->Stat.BER;
64 if (p->Stat.InBandPwr < -95)
65 client->fe_signal_strength = 0;
66 else if (p->Stat.InBandPwr > -29)
67 client->fe_signal_strength = 100;
69 client->fe_signal_strength =
70 (p->Stat.InBandPwr + 95) * 3 / 2;
72 client->fe_status = 0;
75 client->fe_signal_strength = 0;
78 complete(&client->stat_done);
82 smscore_putbuffer(client->coredev, cb);
87 static void smsdvb_unregister_client(struct smsdvb_client_t *client)
89 /* must be called under clientslock */
91 list_del(&client->entry);
93 smscore_unregister_client(client->smsclient);
94 dvb_unregister_frontend(&client->frontend);
95 dvb_dmxdev_release(&client->dmxdev);
96 dvb_dmx_release(&client->demux);
97 dvb_unregister_adapter(&client->adapter);
101 static void smsdvb_onremove(void *context)
103 kmutex_lock(&g_smsdvb_clientslock);
105 smsdvb_unregister_client((struct smsdvb_client_t *) context);
107 kmutex_unlock(&g_smsdvb_clientslock);
110 static int smsdvb_start_feed(struct dvb_demux_feed *feed)
112 struct smsdvb_client_t *client =
113 container_of(feed->demux, struct smsdvb_client_t, demux);
114 struct SmsMsgData_ST PidMsg;
116 sms_debug("add pid %d(%x)",
117 feed->pid, feed->pid);
119 PidMsg.xMsgHeader.msgSrcId = DVBT_BDA_CONTROL_MSG_ID;
120 PidMsg.xMsgHeader.msgDstId = HIF_TASK;
121 PidMsg.xMsgHeader.msgFlags = 0;
122 PidMsg.xMsgHeader.msgType = MSG_SMS_ADD_PID_FILTER_REQ;
123 PidMsg.xMsgHeader.msgLength = sizeof(PidMsg);
124 PidMsg.msgData[0] = feed->pid;
126 return smsclient_sendrequest(client->smsclient,
127 &PidMsg, sizeof(PidMsg));
130 static int smsdvb_stop_feed(struct dvb_demux_feed *feed)
132 struct smsdvb_client_t *client =
133 container_of(feed->demux, struct smsdvb_client_t, demux);
134 struct SmsMsgData_ST PidMsg;
136 sms_debug("remove pid %d(%x)",
137 feed->pid, feed->pid);
139 PidMsg.xMsgHeader.msgSrcId = DVBT_BDA_CONTROL_MSG_ID;
140 PidMsg.xMsgHeader.msgDstId = HIF_TASK;
141 PidMsg.xMsgHeader.msgFlags = 0;
142 PidMsg.xMsgHeader.msgType = MSG_SMS_REMOVE_PID_FILTER_REQ;
143 PidMsg.xMsgHeader.msgLength = sizeof(PidMsg);
144 PidMsg.msgData[0] = feed->pid;
146 return smsclient_sendrequest(client->smsclient,
147 &PidMsg, sizeof(PidMsg));
150 static int smsdvb_sendrequest_and_wait(struct smsdvb_client_t *client,
151 void *buffer, size_t size,
152 struct completion *completion)
154 int rc = smsclient_sendrequest(client->smsclient, buffer, size);
158 return wait_for_completion_timeout(completion,
159 msecs_to_jiffies(2000)) ?
163 static int smsdvb_send_statistics_request(struct smsdvb_client_t *client)
165 struct SmsMsgHdr_ST Msg = { MSG_SMS_GET_STATISTICS_REQ,
166 DVBT_BDA_CONTROL_MSG_ID,
167 HIF_TASK, sizeof(struct SmsMsgHdr_ST), 0 };
168 return smsdvb_sendrequest_and_wait(client, &Msg, sizeof(Msg),
172 static int smsdvb_read_status(struct dvb_frontend *fe, fe_status_t *stat)
174 struct smsdvb_client_t *client =
175 container_of(fe, struct smsdvb_client_t, frontend);
176 int rc = smsdvb_send_statistics_request(client);
179 *stat = client->fe_status;
184 static int smsdvb_read_ber(struct dvb_frontend *fe, u32 *ber)
186 struct smsdvb_client_t *client =
187 container_of(fe, struct smsdvb_client_t, frontend);
188 int rc = smsdvb_send_statistics_request(client);
191 *ber = client->fe_ber;
196 static int smsdvb_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
198 struct smsdvb_client_t *client =
199 container_of(fe, struct smsdvb_client_t, frontend);
200 int rc = smsdvb_send_statistics_request(client);
203 *strength = client->fe_signal_strength;
208 static int smsdvb_read_snr(struct dvb_frontend *fe, u16 *snr)
210 struct smsdvb_client_t *client =
211 container_of(fe, struct smsdvb_client_t, frontend);
212 int rc = smsdvb_send_statistics_request(client);
215 *snr = client->fe_snr;
220 static int smsdvb_get_tune_settings(struct dvb_frontend *fe,
221 struct dvb_frontend_tune_settings *tune)
225 tune->min_delay_ms = 400;
226 tune->step_size = 250000;
231 static int smsdvb_set_frontend(struct dvb_frontend *fe,
232 struct dvb_frontend_parameters *fep)
234 struct smsdvb_client_t *client =
235 container_of(fe, struct smsdvb_client_t, frontend);
238 struct SmsMsgHdr_ST Msg;
242 Msg.Msg.msgSrcId = DVBT_BDA_CONTROL_MSG_ID;
243 Msg.Msg.msgDstId = HIF_TASK;
244 Msg.Msg.msgFlags = 0;
245 Msg.Msg.msgType = MSG_SMS_RF_TUNE_REQ;
246 Msg.Msg.msgLength = sizeof(Msg);
247 Msg.Data[0] = fep->frequency;
248 Msg.Data[2] = 12000000;
250 sms_debug("freq %d band %d",
251 fep->frequency, fep->u.ofdm.bandwidth);
253 switch (fep->u.ofdm.bandwidth) {
254 case BANDWIDTH_8_MHZ: Msg.Data[1] = BW_8_MHZ; break;
255 case BANDWIDTH_7_MHZ: Msg.Data[1] = BW_7_MHZ; break;
256 case BANDWIDTH_6_MHZ: Msg.Data[1] = BW_6_MHZ; break;
257 case BANDWIDTH_AUTO: return -EOPNOTSUPP;
258 default: return -EINVAL;
261 return smsdvb_sendrequest_and_wait(client, &Msg, sizeof(Msg),
265 static int smsdvb_get_frontend(struct dvb_frontend *fe,
266 struct dvb_frontend_parameters *fep)
268 struct smsdvb_client_t *client =
269 container_of(fe, struct smsdvb_client_t, frontend);
274 memcpy(fep, &client->fe_params,
275 sizeof(struct dvb_frontend_parameters));
279 static void smsdvb_release(struct dvb_frontend *fe)
284 static struct dvb_frontend_ops smsdvb_fe_ops = {
286 .name = "Siano Mobile Digital SMS1xxx",
288 .frequency_min = 44250000,
289 .frequency_max = 867250000,
290 .frequency_stepsize = 250000,
291 .caps = FE_CAN_INVERSION_AUTO |
292 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
293 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
294 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
295 FE_CAN_QAM_AUTO | FE_CAN_TRANSMISSION_MODE_AUTO |
296 FE_CAN_GUARD_INTERVAL_AUTO |
298 FE_CAN_HIERARCHY_AUTO,
301 .release = smsdvb_release,
303 .set_frontend = smsdvb_set_frontend,
304 .get_frontend = smsdvb_get_frontend,
305 .get_tune_settings = smsdvb_get_tune_settings,
307 .read_status = smsdvb_read_status,
308 .read_ber = smsdvb_read_ber,
309 .read_signal_strength = smsdvb_read_signal_strength,
310 .read_snr = smsdvb_read_snr,
313 static int smsdvb_hotplug(struct smscore_device_t *coredev,
314 struct device *device, int arrival)
316 struct smsclient_params_t params;
317 struct smsdvb_client_t *client;
320 /* device removal handled by onremove callback */
324 if (smscore_get_device_mode(coredev) != 4) {
325 sms_err("SMS Device mode is not set for "
330 client = kzalloc(sizeof(struct smsdvb_client_t), GFP_KERNEL);
332 sms_err("kmalloc() failed");
336 /* register dvb adapter */
337 rc = dvb_register_adapter(&client->adapter,
339 smscore_get_board_id(coredev))->name,
340 THIS_MODULE, device, adapter_nr);
342 sms_err("dvb_register_adapter() failed %d", rc);
347 client->demux.dmx.capabilities = DMX_TS_FILTERING;
348 client->demux.filternum = 32; /* todo: nova ??? */
349 client->demux.feednum = 32;
350 client->demux.start_feed = smsdvb_start_feed;
351 client->demux.stop_feed = smsdvb_stop_feed;
353 rc = dvb_dmx_init(&client->demux);
355 sms_err("dvb_dmx_init failed %d", rc);
360 client->dmxdev.filternum = 32;
361 client->dmxdev.demux = &client->demux.dmx;
362 client->dmxdev.capabilities = 0;
364 rc = dvb_dmxdev_init(&client->dmxdev, &client->adapter);
366 sms_err("dvb_dmxdev_init failed %d", rc);
370 /* init and register frontend */
371 memcpy(&client->frontend.ops, &smsdvb_fe_ops,
372 sizeof(struct dvb_frontend_ops));
374 rc = dvb_register_frontend(&client->adapter, &client->frontend);
376 sms_err("frontend registration failed %d", rc);
380 params.initial_id = 1;
381 params.data_type = MSG_SMS_DVBT_BDA_DATA;
382 params.onresponse_handler = smsdvb_onresponse;
383 params.onremove_handler = smsdvb_onremove;
384 params.context = client;
386 rc = smscore_register_client(coredev, ¶ms, &client->smsclient);
388 sms_err("smscore_register_client() failed %d", rc);
392 client->coredev = coredev;
394 init_completion(&client->tune_done);
395 init_completion(&client->stat_done);
397 kmutex_lock(&g_smsdvb_clientslock);
399 list_add(&client->entry, &g_smsdvb_clients);
401 kmutex_unlock(&g_smsdvb_clientslock);
408 dvb_unregister_frontend(&client->frontend);
411 dvb_dmxdev_release(&client->dmxdev);
414 dvb_dmx_release(&client->demux);
417 dvb_unregister_adapter(&client->adapter);
424 int smsdvb_register(void)
428 INIT_LIST_HEAD(&g_smsdvb_clients);
429 kmutex_init(&g_smsdvb_clientslock);
431 rc = smscore_register_hotplug(smsdvb_hotplug);
438 void smsdvb_unregister(void)
440 smscore_unregister_hotplug(smsdvb_hotplug);
442 kmutex_lock(&g_smsdvb_clientslock);
444 while (!list_empty(&g_smsdvb_clients))
445 smsdvb_unregister_client(
446 (struct smsdvb_client_t *) g_smsdvb_clients.next);
448 kmutex_unlock(&g_smsdvb_clientslock);