• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Driver for the Siano SMS1xxx USB dongle
3  *
4  *  author: Anatoly Greenblat
5  *
6  *  Copyright (c), 2005-2008 Siano Mobile Silicon, Inc.
7  *
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;
11  *
12  *  Software distributed under the License is distributed on an "AS IS"
13  *  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
14  *
15  *  See the 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, write to the Free Software
19  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21 
22 #include <linux/module.h>
23 #include <linux/init.h>
24 
25 #include "smscoreapi.h"
26 #include "sms-cards.h"
27 
28 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
29 
30 static struct list_head g_smsdvb_clients;
31 static struct mutex g_smsdvb_clientslock;
32 
smsdvb_onresponse(void * context,struct smscore_buffer_t * cb)33 static int smsdvb_onresponse(void *context, struct smscore_buffer_t *cb)
34 {
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);
38 
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));
43 		break;
44 
45 	case MSG_SMS_RF_TUNE_RES:
46 		complete(&client->tune_done);
47 		break;
48 
49 	case MSG_SMS_GET_STATISTICS_RES:
50 	{
51 		struct SmsMsgStatisticsInfo_ST *p =
52 			(struct SmsMsgStatisticsInfo_ST *)(phdr + 1);
53 
54 		if (p->Stat.IsDemodLocked) {
55 			client->fe_status = FE_HAS_SIGNAL |
56 					    FE_HAS_CARRIER |
57 					    FE_HAS_VITERBI |
58 					    FE_HAS_SYNC |
59 					    FE_HAS_LOCK;
60 
61 			client->fe_snr = p->Stat.SNR;
62 			client->fe_ber = p->Stat.BER;
63 			client->fe_unc = p->Stat.BERErrorCount;
64 
65 			if (p->Stat.InBandPwr < -95)
66 				client->fe_signal_strength = 0;
67 			else if (p->Stat.InBandPwr > -29)
68 				client->fe_signal_strength = 100;
69 			else
70 				client->fe_signal_strength =
71 					(p->Stat.InBandPwr + 95) * 3 / 2;
72 		} else {
73 			client->fe_status = 0;
74 			client->fe_snr =
75 			client->fe_ber =
76 			client->fe_unc =
77 			client->fe_signal_strength = 0;
78 		}
79 
80 		complete(&client->stat_done);
81 		break;
82 	} }
83 
84 	smscore_putbuffer(client->coredev, cb);
85 
86 	return 0;
87 }
88 
smsdvb_unregister_client(struct smsdvb_client_t * client)89 static void smsdvb_unregister_client(struct smsdvb_client_t *client)
90 {
91 	/* must be called under clientslock */
92 
93 	list_del(&client->entry);
94 
95 	smscore_unregister_client(client->smsclient);
96 	dvb_unregister_frontend(&client->frontend);
97 	dvb_dmxdev_release(&client->dmxdev);
98 	dvb_dmx_release(&client->demux);
99 	dvb_unregister_adapter(&client->adapter);
100 	kfree(client);
101 }
102 
smsdvb_onremove(void * context)103 static void smsdvb_onremove(void *context)
104 {
105 	kmutex_lock(&g_smsdvb_clientslock);
106 
107 	smsdvb_unregister_client((struct smsdvb_client_t *) context);
108 
109 	kmutex_unlock(&g_smsdvb_clientslock);
110 }
111 
smsdvb_start_feed(struct dvb_demux_feed * feed)112 static int smsdvb_start_feed(struct dvb_demux_feed *feed)
113 {
114 	struct smsdvb_client_t *client =
115 		container_of(feed->demux, struct smsdvb_client_t, demux);
116 	struct SmsMsgData_ST PidMsg;
117 
118 	sms_debug("add pid %d(%x)",
119 		  feed->pid, feed->pid);
120 
121 	PidMsg.xMsgHeader.msgSrcId = DVBT_BDA_CONTROL_MSG_ID;
122 	PidMsg.xMsgHeader.msgDstId = HIF_TASK;
123 	PidMsg.xMsgHeader.msgFlags = 0;
124 	PidMsg.xMsgHeader.msgType  = MSG_SMS_ADD_PID_FILTER_REQ;
125 	PidMsg.xMsgHeader.msgLength = sizeof(PidMsg);
126 	PidMsg.msgData[0] = feed->pid;
127 
128 	return smsclient_sendrequest(client->smsclient,
129 				     &PidMsg, sizeof(PidMsg));
130 }
131 
smsdvb_stop_feed(struct dvb_demux_feed * feed)132 static int smsdvb_stop_feed(struct dvb_demux_feed *feed)
133 {
134 	struct smsdvb_client_t *client =
135 		container_of(feed->demux, struct smsdvb_client_t, demux);
136 	struct SmsMsgData_ST PidMsg;
137 
138 	sms_debug("remove pid %d(%x)",
139 		  feed->pid, feed->pid);
140 
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_REMOVE_PID_FILTER_REQ;
145 	PidMsg.xMsgHeader.msgLength = sizeof(PidMsg);
146 	PidMsg.msgData[0] = feed->pid;
147 
148 	return smsclient_sendrequest(client->smsclient,
149 				     &PidMsg, sizeof(PidMsg));
150 }
151 
smsdvb_sendrequest_and_wait(struct smsdvb_client_t * client,void * buffer,size_t size,struct completion * completion)152 static int smsdvb_sendrequest_and_wait(struct smsdvb_client_t *client,
153 					void *buffer, size_t size,
154 					struct completion *completion)
155 {
156 	int rc = smsclient_sendrequest(client->smsclient, buffer, size);
157 	if (rc < 0)
158 		return rc;
159 
160 	return wait_for_completion_timeout(completion,
161 					   msecs_to_jiffies(2000)) ?
162 						0 : -ETIME;
163 }
164 
smsdvb_send_statistics_request(struct smsdvb_client_t * client)165 static int smsdvb_send_statistics_request(struct smsdvb_client_t *client)
166 {
167 	struct SmsMsgHdr_ST Msg = { MSG_SMS_GET_STATISTICS_REQ,
168 			     DVBT_BDA_CONTROL_MSG_ID,
169 			     HIF_TASK, sizeof(struct SmsMsgHdr_ST), 0 };
170 	int ret = smsdvb_sendrequest_and_wait(client, &Msg, sizeof(Msg),
171 					      &client->stat_done);
172 	if (ret < 0)
173 		return ret;
174 
175 	if (client->fe_status & FE_HAS_LOCK)
176 		sms_board_led_feedback(client->coredev,
177 				       (client->fe_unc == 0) ?
178 				       SMS_LED_HI : SMS_LED_LO);
179 	else
180 		sms_board_led_feedback(client->coredev, SMS_LED_OFF);
181 	return ret;
182 }
183 
smsdvb_read_status(struct dvb_frontend * fe,fe_status_t * stat)184 static int smsdvb_read_status(struct dvb_frontend *fe, fe_status_t *stat)
185 {
186 	struct smsdvb_client_t *client =
187 		container_of(fe, struct smsdvb_client_t, frontend);
188 	int rc = smsdvb_send_statistics_request(client);
189 
190 	if (!rc)
191 		*stat = client->fe_status;
192 
193 	return rc;
194 }
195 
smsdvb_read_ber(struct dvb_frontend * fe,u32 * ber)196 static int smsdvb_read_ber(struct dvb_frontend *fe, u32 *ber)
197 {
198 	struct smsdvb_client_t *client =
199 		container_of(fe, struct smsdvb_client_t, frontend);
200 	int rc = smsdvb_send_statistics_request(client);
201 
202 	if (!rc)
203 		*ber = client->fe_ber;
204 
205 	return rc;
206 }
207 
smsdvb_read_signal_strength(struct dvb_frontend * fe,u16 * strength)208 static int smsdvb_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
209 {
210 	struct smsdvb_client_t *client =
211 		container_of(fe, struct smsdvb_client_t, frontend);
212 	int rc = smsdvb_send_statistics_request(client);
213 
214 	if (!rc)
215 		*strength = client->fe_signal_strength;
216 
217 	return rc;
218 }
219 
smsdvb_read_snr(struct dvb_frontend * fe,u16 * snr)220 static int smsdvb_read_snr(struct dvb_frontend *fe, u16 *snr)
221 {
222 	struct smsdvb_client_t *client =
223 		container_of(fe, struct smsdvb_client_t, frontend);
224 	int rc = smsdvb_send_statistics_request(client);
225 
226 	if (!rc)
227 		*snr = client->fe_snr;
228 
229 	return rc;
230 }
231 
smsdvb_read_ucblocks(struct dvb_frontend * fe,u32 * ucblocks)232 static int smsdvb_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
233 {
234 	struct smsdvb_client_t *client =
235 		container_of(fe, struct smsdvb_client_t, frontend);
236 	int rc = smsdvb_send_statistics_request(client);
237 
238 	if (!rc)
239 		*ucblocks = client->fe_unc;
240 
241 	return rc;
242 }
243 
smsdvb_get_tune_settings(struct dvb_frontend * fe,struct dvb_frontend_tune_settings * tune)244 static int smsdvb_get_tune_settings(struct dvb_frontend *fe,
245 				    struct dvb_frontend_tune_settings *tune)
246 {
247 	sms_debug("");
248 
249 	tune->min_delay_ms = 400;
250 	tune->step_size = 250000;
251 	tune->max_drift = 0;
252 	return 0;
253 }
254 
smsdvb_set_frontend(struct dvb_frontend * fe,struct dvb_frontend_parameters * fep)255 static int smsdvb_set_frontend(struct dvb_frontend *fe,
256 			       struct dvb_frontend_parameters *fep)
257 {
258 	struct smsdvb_client_t *client =
259 		container_of(fe, struct smsdvb_client_t, frontend);
260 
261 	struct {
262 		struct SmsMsgHdr_ST	Msg;
263 		u32		Data[3];
264 	} Msg;
265 
266 	Msg.Msg.msgSrcId  = DVBT_BDA_CONTROL_MSG_ID;
267 	Msg.Msg.msgDstId  = HIF_TASK;
268 	Msg.Msg.msgFlags  = 0;
269 	Msg.Msg.msgType   = MSG_SMS_RF_TUNE_REQ;
270 	Msg.Msg.msgLength = sizeof(Msg);
271 	Msg.Data[0] = fep->frequency;
272 	Msg.Data[2] = 12000000;
273 
274 	sms_debug("freq %d band %d",
275 		  fep->frequency, fep->u.ofdm.bandwidth);
276 
277 	switch (fep->u.ofdm.bandwidth) {
278 	case BANDWIDTH_8_MHZ: Msg.Data[1] = BW_8_MHZ; break;
279 	case BANDWIDTH_7_MHZ: Msg.Data[1] = BW_7_MHZ; break;
280 	case BANDWIDTH_6_MHZ: Msg.Data[1] = BW_6_MHZ; break;
281 	case BANDWIDTH_AUTO: return -EOPNOTSUPP;
282 	default: return -EINVAL;
283 	}
284 
285 	return smsdvb_sendrequest_and_wait(client, &Msg, sizeof(Msg),
286 					   &client->tune_done);
287 }
288 
smsdvb_get_frontend(struct dvb_frontend * fe,struct dvb_frontend_parameters * fep)289 static int smsdvb_get_frontend(struct dvb_frontend *fe,
290 			       struct dvb_frontend_parameters *fep)
291 {
292 	struct smsdvb_client_t *client =
293 		container_of(fe, struct smsdvb_client_t, frontend);
294 
295 	sms_debug("");
296 
297 	/* todo: */
298 	memcpy(fep, &client->fe_params,
299 	       sizeof(struct dvb_frontend_parameters));
300 
301 	return 0;
302 }
303 
smsdvb_init(struct dvb_frontend * fe)304 static int smsdvb_init(struct dvb_frontend *fe)
305 {
306 	struct smsdvb_client_t *client =
307 		container_of(fe, struct smsdvb_client_t, frontend);
308 
309 	sms_board_power(client->coredev, 1);
310 
311 	return 0;
312 }
313 
smsdvb_sleep(struct dvb_frontend * fe)314 static int smsdvb_sleep(struct dvb_frontend *fe)
315 {
316 	struct smsdvb_client_t *client =
317 		container_of(fe, struct smsdvb_client_t, frontend);
318 
319 	sms_board_led_feedback(client->coredev, SMS_LED_OFF);
320 	sms_board_power(client->coredev, 0);
321 
322 	return 0;
323 }
324 
smsdvb_release(struct dvb_frontend * fe)325 static void smsdvb_release(struct dvb_frontend *fe)
326 {
327 	/* do nothing */
328 }
329 
330 static struct dvb_frontend_ops smsdvb_fe_ops = {
331 	.info = {
332 		.name			= "Siano Mobile Digital SMS1xxx",
333 		.type			= FE_OFDM,
334 		.frequency_min		= 44250000,
335 		.frequency_max		= 867250000,
336 		.frequency_stepsize	= 250000,
337 		.caps = FE_CAN_INVERSION_AUTO |
338 			FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
339 			FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
340 			FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
341 			FE_CAN_QAM_AUTO | FE_CAN_TRANSMISSION_MODE_AUTO |
342 			FE_CAN_GUARD_INTERVAL_AUTO |
343 			FE_CAN_RECOVER |
344 			FE_CAN_HIERARCHY_AUTO,
345 	},
346 
347 	.release = smsdvb_release,
348 
349 	.set_frontend = smsdvb_set_frontend,
350 	.get_frontend = smsdvb_get_frontend,
351 	.get_tune_settings = smsdvb_get_tune_settings,
352 
353 	.read_status = smsdvb_read_status,
354 	.read_ber = smsdvb_read_ber,
355 	.read_signal_strength = smsdvb_read_signal_strength,
356 	.read_snr = smsdvb_read_snr,
357 	.read_ucblocks = smsdvb_read_ucblocks,
358 
359 	.init = smsdvb_init,
360 	.sleep = smsdvb_sleep,
361 };
362 
smsdvb_hotplug(struct smscore_device_t * coredev,struct device * device,int arrival)363 static int smsdvb_hotplug(struct smscore_device_t *coredev,
364 			  struct device *device, int arrival)
365 {
366 	struct smsclient_params_t params;
367 	struct smsdvb_client_t *client;
368 	int rc;
369 
370 	/* device removal handled by onremove callback */
371 	if (!arrival)
372 		return 0;
373 
374 	if (smscore_get_device_mode(coredev) != 4) {
375 		sms_err("SMS Device mode is not set for "
376 			"DVB operation.");
377 		return 0;
378 	}
379 
380 	client = kzalloc(sizeof(struct smsdvb_client_t), GFP_KERNEL);
381 	if (!client) {
382 		sms_err("kmalloc() failed");
383 		return -ENOMEM;
384 	}
385 
386 	/* register dvb adapter */
387 	rc = dvb_register_adapter(&client->adapter,
388 				  sms_get_board(
389 					smscore_get_board_id(coredev))->name,
390 				  THIS_MODULE, device, adapter_nr);
391 	if (rc < 0) {
392 		sms_err("dvb_register_adapter() failed %d", rc);
393 		goto adapter_error;
394 	}
395 
396 	/* init dvb demux */
397 	client->demux.dmx.capabilities = DMX_TS_FILTERING;
398 	client->demux.filternum = 32; /* todo: nova ??? */
399 	client->demux.feednum = 32;
400 	client->demux.start_feed = smsdvb_start_feed;
401 	client->demux.stop_feed = smsdvb_stop_feed;
402 
403 	rc = dvb_dmx_init(&client->demux);
404 	if (rc < 0) {
405 		sms_err("dvb_dmx_init failed %d", rc);
406 		goto dvbdmx_error;
407 	}
408 
409 	/* init dmxdev */
410 	client->dmxdev.filternum = 32;
411 	client->dmxdev.demux = &client->demux.dmx;
412 	client->dmxdev.capabilities = 0;
413 
414 	rc = dvb_dmxdev_init(&client->dmxdev, &client->adapter);
415 	if (rc < 0) {
416 		sms_err("dvb_dmxdev_init failed %d", rc);
417 		goto dmxdev_error;
418 	}
419 
420 	/* init and register frontend */
421 	memcpy(&client->frontend.ops, &smsdvb_fe_ops,
422 	       sizeof(struct dvb_frontend_ops));
423 
424 	rc = dvb_register_frontend(&client->adapter, &client->frontend);
425 	if (rc < 0) {
426 		sms_err("frontend registration failed %d", rc);
427 		goto frontend_error;
428 	}
429 
430 	params.initial_id = 1;
431 	params.data_type = MSG_SMS_DVBT_BDA_DATA;
432 	params.onresponse_handler = smsdvb_onresponse;
433 	params.onremove_handler = smsdvb_onremove;
434 	params.context = client;
435 
436 	rc = smscore_register_client(coredev, &params, &client->smsclient);
437 	if (rc < 0) {
438 		sms_err("smscore_register_client() failed %d", rc);
439 		goto client_error;
440 	}
441 
442 	client->coredev = coredev;
443 
444 	init_completion(&client->tune_done);
445 	init_completion(&client->stat_done);
446 
447 	kmutex_lock(&g_smsdvb_clientslock);
448 
449 	list_add(&client->entry, &g_smsdvb_clients);
450 
451 	kmutex_unlock(&g_smsdvb_clientslock);
452 
453 	sms_info("success");
454 
455 	sms_board_setup(coredev);
456 
457 	return 0;
458 
459 client_error:
460 	dvb_unregister_frontend(&client->frontend);
461 
462 frontend_error:
463 	dvb_dmxdev_release(&client->dmxdev);
464 
465 dmxdev_error:
466 	dvb_dmx_release(&client->demux);
467 
468 dvbdmx_error:
469 	dvb_unregister_adapter(&client->adapter);
470 
471 adapter_error:
472 	kfree(client);
473 	return rc;
474 }
475 
smsdvb_register(void)476 int smsdvb_register(void)
477 {
478 	int rc;
479 
480 	INIT_LIST_HEAD(&g_smsdvb_clients);
481 	kmutex_init(&g_smsdvb_clientslock);
482 
483 	rc = smscore_register_hotplug(smsdvb_hotplug);
484 
485 	sms_debug("");
486 
487 	return rc;
488 }
489 
smsdvb_unregister(void)490 void smsdvb_unregister(void)
491 {
492 	smscore_unregister_hotplug(smsdvb_hotplug);
493 
494 	kmutex_lock(&g_smsdvb_clientslock);
495 
496 	while (!list_empty(&g_smsdvb_clients))
497 	       smsdvb_unregister_client(
498 			(struct smsdvb_client_t *) g_smsdvb_clients.next);
499 
500 	kmutex_unlock(&g_smsdvb_clientslock);
501 }
502