smsdvb.c 10.9 KB
Newer Older
1 2 3
/*
 *  Driver for the Siano SMS10xx USB dongle
 *
4 5 6
 *  author: Anatoly Greenblat
 *
 *  Copyright (c), 2005-2008 Siano Mobile Silicon, Inc.
7 8
 *
 *  This program is free software; you can redistribute it and/or modify
9 10
 *  it under the terms of the GNU General Public License version 3 as
 *  published by the Free Software Foundation;
11
 *
12 13
 *  Software distributed under the License is distributed on an "AS IS"
 *  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
14
 *
15
 *  See the GNU General Public License for more details.
16 17 18 19 20 21
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

22 23 24 25 26
#include <linux/module.h>
#include <linux/init.h>

#include "smscoreapi.h"

27 28
DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);

29 30 31 32 33 34
struct list_head g_smsdvb_clients;
kmutex_t g_smsdvb_clientslock;

int smsdvb_onresponse(void *context, smscore_buffer_t *cb)
{
	smsdvb_client_t *client = (smsdvb_client_t *) context;
35
	SmsMsgHdr_ST *phdr = (SmsMsgHdr_ST *)(((u8 *) cb->p) + cb->offset);
36

37
	switch (phdr->msgType) {
38 39 40 41 42 43 44 45
	case MSG_SMS_DVBT_BDA_DATA:
		dvb_dmx_swfilter(&client->demux, (u8 *)(phdr + 1),
				 cb->size - sizeof(SmsMsgHdr_ST));
		break;

	case MSG_SMS_RF_TUNE_RES:
		complete(&client->tune_done);
		break;
46

47 48 49 50
	case MSG_SMS_GET_STATISTICS_RES:
	{
		SmsMsgStatisticsInfo_ST *p =
			(SmsMsgStatisticsInfo_ST *)(phdr + 1);
51

52
		if (p->Stat.IsDemodLocked) {
53 54 55 56 57 58 59 60
			client->fe_status = FE_HAS_SIGNAL |
					    FE_HAS_CARRIER |
					    FE_HAS_VITERBI |
					    FE_HAS_SYNC |
					    FE_HAS_LOCK;

			client->fe_snr = p->Stat.SNR;
			client->fe_ber = p->Stat.BER;
61

62 63 64 65 66 67 68 69 70 71 72 73
			if (p->Stat.InBandPwr < -95)
				client->fe_signal_strength = 0;
			else if (p->Stat.InBandPwr > -29)
				client->fe_signal_strength = 100;
			else
				client->fe_signal_strength =
					(p->Stat.InBandPwr + 95) * 3 / 2;
		} else {
			client->fe_status = 0;
			client->fe_snr =
			client->fe_ber =
			client->fe_signal_strength = 0;
74
		}
75 76 77 78

		complete(&client->stat_done);
		break;
	} }
79 80 81 82 83 84

	smscore_putbuffer(client->coredev, cb);

	return 0;
}

85
void smsdvb_unregister_client(smsdvb_client_t *client)
86
{
87
	/* must be called under clientslock */
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

	list_del(&client->entry);

	smscore_unregister_client(client->smsclient);
	dvb_unregister_frontend(&client->frontend);
	dvb_dmxdev_release(&client->dmxdev);
	dvb_dmx_release(&client->demux);
	dvb_unregister_adapter(&client->adapter);
	kfree(client);
}

void smsdvb_onremove(void *context)
{
	kmutex_lock(&g_smsdvb_clientslock);

103
	smsdvb_unregister_client((smsdvb_client_t *) context);
104 105 106 107 108 109

	kmutex_unlock(&g_smsdvb_clientslock);
}

static int smsdvb_start_feed(struct dvb_demux_feed *feed)
{
110 111
	smsdvb_client_t *client =
		container_of(feed->demux, smsdvb_client_t, demux);
112 113
	SmsMsgData_ST PidMsg;

114 115
	printk(KERN_DEBUG "%s add pid %d(%x)\n", __func__,
	       feed->pid, feed->pid);
116 117 118 119 120 121 122 123

	PidMsg.xMsgHeader.msgSrcId = DVBT_BDA_CONTROL_MSG_ID;
	PidMsg.xMsgHeader.msgDstId = HIF_TASK;
	PidMsg.xMsgHeader.msgFlags = 0;
	PidMsg.xMsgHeader.msgType  = MSG_SMS_ADD_PID_FILTER_REQ;
	PidMsg.xMsgHeader.msgLength = sizeof(PidMsg);
	PidMsg.msgData[0] = feed->pid;

124 125
	return smsclient_sendrequest(client->smsclient,
				     &PidMsg, sizeof(PidMsg));
126 127 128 129
}

static int smsdvb_stop_feed(struct dvb_demux_feed *feed)
{
130 131
	smsdvb_client_t *client =
		container_of(feed->demux, smsdvb_client_t, demux);
132 133
	SmsMsgData_ST PidMsg;

134 135
	printk(KERN_DEBUG "%s remove pid %d(%x)\n", __func__,
	       feed->pid, feed->pid);
136 137 138 139 140 141 142 143

	PidMsg.xMsgHeader.msgSrcId = DVBT_BDA_CONTROL_MSG_ID;
	PidMsg.xMsgHeader.msgDstId = HIF_TASK;
	PidMsg.xMsgHeader.msgFlags = 0;
	PidMsg.xMsgHeader.msgType  = MSG_SMS_REMOVE_PID_FILTER_REQ;
	PidMsg.xMsgHeader.msgLength = sizeof(PidMsg);
	PidMsg.msgData[0] = feed->pid;

144 145
	return smsclient_sendrequest(client->smsclient,
				     &PidMsg, sizeof(PidMsg));
146 147
}

148 149 150
static int smsdvb_sendrequest_and_wait(smsdvb_client_t *client,
					void *buffer, size_t size,
					struct completion *completion)
151 152 153 154 155
{
	int rc = smsclient_sendrequest(client->smsclient, buffer, size);
	if (rc < 0)
		return rc;

156 157 158
	return wait_for_completion_timeout(completion,
					   msecs_to_jiffies(2000)) ?
						0 : -ETIME;
159 160 161 162
}

static int smsdvb_send_statistics_request(smsdvb_client_t *client)
{
163 164 165 166 167
	SmsMsgHdr_ST Msg = { MSG_SMS_GET_STATISTICS_REQ,
			     DVBT_BDA_CONTROL_MSG_ID,
			     HIF_TASK, sizeof(SmsMsgHdr_ST), 0 };
	return smsdvb_sendrequest_and_wait(client, &Msg, sizeof(Msg),
					   &client->stat_done);
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
}

static int smsdvb_read_status(struct dvb_frontend *fe, fe_status_t *stat)
{
	smsdvb_client_t *client = container_of(fe, smsdvb_client_t, frontend);
	int rc = smsdvb_send_statistics_request(client);

	if (!rc)
		*stat = client->fe_status;

	return rc;
}

static int smsdvb_read_ber(struct dvb_frontend *fe, u32 *ber)
{
	smsdvb_client_t *client = container_of(fe, smsdvb_client_t, frontend);
	int rc = smsdvb_send_statistics_request(client);

	if (!rc)
		*ber = client->fe_ber;

	return rc;
}

static int smsdvb_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
{
	smsdvb_client_t *client = container_of(fe, smsdvb_client_t, frontend);
	int rc = smsdvb_send_statistics_request(client);

	if (!rc)
		*strength = client->fe_signal_strength;

	return rc;
}

static int smsdvb_read_snr(struct dvb_frontend *fe, u16 *snr)
{
	smsdvb_client_t *client = container_of(fe, smsdvb_client_t, frontend);
	int rc = smsdvb_send_statistics_request(client);

	if (!rc)
		*snr = client->fe_snr;

	return rc;
}

214 215
static int smsdvb_get_tune_settings(struct dvb_frontend *fe,
				    struct dvb_frontend_tune_settings *tune)
216
{
217
	printk(KERN_DEBUG "%s\n", __func__);
218 219 220 221 222 223 224

	tune->min_delay_ms = 400;
	tune->step_size = 250000;
	tune->max_drift = 0;
	return 0;
}

225 226
static int smsdvb_set_frontend(struct dvb_frontend *fe,
			       struct dvb_frontend_parameters *fep)
227 228 229 230 231 232
{
	smsdvb_client_t *client = container_of(fe, smsdvb_client_t, frontend);

	struct
	{
		SmsMsgHdr_ST	Msg;
233
		u32		Data[3];
234 235 236 237 238 239 240 241 242 243
	} Msg;

	Msg.Msg.msgSrcId  = DVBT_BDA_CONTROL_MSG_ID;
	Msg.Msg.msgDstId  = HIF_TASK;
	Msg.Msg.msgFlags  = 0;
	Msg.Msg.msgType   = MSG_SMS_RF_TUNE_REQ;
	Msg.Msg.msgLength = sizeof(Msg);
	Msg.Data[0] = fep->frequency;
	Msg.Data[2] = 12000000;

244
	printk(KERN_DEBUG "%s freq %d band %d\n", __func__,
245
	       fep->frequency, fep->u.ofdm.bandwidth);
246

247 248 249 250 251 252
	switch (fep->u.ofdm.bandwidth) {
	case BANDWIDTH_8_MHZ: Msg.Data[1] = BW_8_MHZ; break;
	case BANDWIDTH_7_MHZ: Msg.Data[1] = BW_7_MHZ; break;
	case BANDWIDTH_6_MHZ: Msg.Data[1] = BW_6_MHZ; break;
	case BANDWIDTH_AUTO: return -EOPNOTSUPP;
	default: return -EINVAL;
253 254
	}

255 256
	return smsdvb_sendrequest_and_wait(client, &Msg, sizeof(Msg),
					   &client->tune_done);
257 258
}

259 260
static int smsdvb_get_frontend(struct dvb_frontend *fe,
			       struct dvb_frontend_parameters *fep)
261 262 263
{
	smsdvb_client_t *client = container_of(fe, smsdvb_client_t, frontend);

264
	printk(KERN_DEBUG "%s\n", __func__);
265

266
	/* todo: */
267 268
	memcpy(fep, &client->fe_params,
	       sizeof(struct dvb_frontend_parameters));
269 270 271 272 273
	return 0;
}

static void smsdvb_release(struct dvb_frontend *fe)
{
274
	/* do nothing */
275 276 277 278
}

static struct dvb_frontend_ops smsdvb_fe_ops = {
	.info = {
279 280
		.name			= "Siano Mobile Digital SMS10xx",
		.type			= FE_OFDM,
281 282 283 284
		.frequency_min		= 44250000,
		.frequency_max		= 867250000,
		.frequency_stepsize	= 250000,
		.caps = FE_CAN_INVERSION_AUTO |
285 286 287 288 289 290 291
			FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
			FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
			FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
			FE_CAN_QAM_AUTO | FE_CAN_TRANSMISSION_MODE_AUTO |
			FE_CAN_GUARD_INTERVAL_AUTO |
			FE_CAN_RECOVER |
			FE_CAN_HIERARCHY_AUTO,
292 293 294 295 296 297 298 299 300 301 302 303 304 305
	},

	.release = smsdvb_release,

	.set_frontend = smsdvb_set_frontend,
	.get_frontend = smsdvb_get_frontend,
	.get_tune_settings = smsdvb_get_tune_settings,

	.read_status = smsdvb_read_status,
	.read_ber = smsdvb_read_ber,
	.read_signal_strength = smsdvb_read_signal_strength,
	.read_snr = smsdvb_read_snr,
};

306 307
int smsdvb_hotplug(smscore_device_t *coredev,
		   struct device *device, int arrival)
308 309
{
	smsclient_params_t params;
310
	smsdvb_client_t *client;
311 312
	int rc;

313
	/* device removal handled by onremove callback */
314 315 316
	if (!arrival)
		return 0;

317
	if (smscore_get_device_mode(coredev) != 4) {
318 319
		printk(KERN_ERR "%sSMS Device mode is not set for "
		       "DVB operation.\n", __func__);
320
		return 0;
321 322 323
	}

	client = kzalloc(sizeof(smsdvb_client_t), GFP_KERNEL);
324
	if (!client) {
325
		printk(KERN_INFO "%s kmalloc() failed\n", __func__);
326 327 328
		return -ENOMEM;
	}

329
	/* register dvb adapter */
330 331 332
	rc = dvb_register_adapter(&client->adapter, "Siano Digital Receiver",
				  THIS_MODULE, device, adapter_nr);
	if (rc < 0) {
333 334
		printk(KERN_ERR "%s dvb_register_adapter() failed %d\n",
		       __func__, rc);
335 336 337
		goto adapter_error;
	}

338
	/* init dvb demux */
339
	client->demux.dmx.capabilities = DMX_TS_FILTERING;
340
	client->demux.filternum = 32; /* todo: nova ??? */
341 342 343 344 345
	client->demux.feednum = 32;
	client->demux.start_feed = smsdvb_start_feed;
	client->demux.stop_feed = smsdvb_stop_feed;

	rc = dvb_dmx_init(&client->demux);
346
	if (rc < 0) {
347 348
		printk(KERN_ERR "%s dvb_dmx_init failed %d\n\n",
		       __func__, rc);
349 350 351
		goto dvbdmx_error;
	}

352
	/* init dmxdev */
353 354 355 356 357
	client->dmxdev.filternum = 32;
	client->dmxdev.demux = &client->demux.dmx;
	client->dmxdev.capabilities = 0;

	rc = dvb_dmxdev_init(&client->dmxdev, &client->adapter);
358
	if (rc < 0) {
359 360
		printk(KERN_ERR "%s dvb_dmxdev_init failed %d\n",
		       __func__, rc);
361 362 363
		goto dmxdev_error;
	}

364
	/* init and register frontend */
365 366
	memcpy(&client->frontend.ops, &smsdvb_fe_ops,
	       sizeof(struct dvb_frontend_ops));
367 368

	rc = dvb_register_frontend(&client->adapter, &client->frontend);
369
	if (rc < 0) {
370 371
		printk(KERN_ERR "%s frontend registration failed %d\n",
		       __func__, rc);
372 373 374
		goto frontend_error;
	}

375
	params.initial_id = 1;
376 377 378 379 380 381
	params.data_type = MSG_SMS_DVBT_BDA_DATA;
	params.onresponse_handler = smsdvb_onresponse;
	params.onremove_handler = smsdvb_onremove;
	params.context = client;

	rc = smscore_register_client(coredev, &params, &client->smsclient);
382 383 384
	if (rc < 0) {
		printk(KERN_INFO "%s smscore_register_client() failed %d\n",
		       __func__, rc);
385 386 387 388 389 390 391 392 393 394 395 396 397 398
		goto client_error;
	}

	client->coredev = coredev;

	init_completion(&client->tune_done);
	init_completion(&client->stat_done);

	kmutex_lock(&g_smsdvb_clientslock);

	list_add(&client->entry, &g_smsdvb_clients);

	kmutex_unlock(&g_smsdvb_clientslock);

399
	printk(KERN_INFO "%s success\n", __func__);
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419

	return 0;

client_error:
	dvb_unregister_frontend(&client->frontend);

frontend_error:
	dvb_dmxdev_release(&client->dmxdev);

dmxdev_error:
	dvb_dmx_release(&client->demux);

dvbdmx_error:
	dvb_unregister_adapter(&client->adapter);

adapter_error:
	kfree(client);
	return rc;
}

420 421 422 423 424 425 426 427 428
int smsdvb_register(void)
{
	int rc;

	INIT_LIST_HEAD(&g_smsdvb_clients);
	kmutex_init(&g_smsdvb_clientslock);

	rc = smscore_register_hotplug(smsdvb_hotplug);

429
	printk(KERN_INFO "%s\n", __func__);
430 431 432 433 434 435 436 437 438 439 440

	return rc;
}

void smsdvb_unregister(void)
{
	smscore_unregister_hotplug(smsdvb_hotplug);

	kmutex_lock(&g_smsdvb_clientslock);

	while (!list_empty(&g_smsdvb_clients))
441 442
	       smsdvb_unregister_client(
			(smsdvb_client_t *) g_smsdvb_clients.next);
443 444 445

	kmutex_unlock(&g_smsdvb_clientslock);
}