summaryrefslogtreecommitdiffstats
path: root/drivers/media/usb/tlg2300/pd-dvb.c
blob: 30fcb117e89899a25208428061900d22eefb829c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
#include "pd-common.h"
#include <linux/kernel.h>
#include <linux/usb.h>
#include <linux/dvb/dmx.h>
#include <linux/delay.h>
#include <linux/gfp.h>

#include "vendorcmds.h"
#include <linux/sched.h>
#include <linux/atomic.h>

static void dvb_urb_cleanup(struct pd_dvb_adapter *pd_dvb);

static int dvb_bandwidth[][2] = {
	{ TLG_BW_8, 8000000 },
	{ TLG_BW_7, 7000000 },
	{ TLG_BW_6, 6000000 }
};
static int dvb_bandwidth_length = ARRAY_SIZE(dvb_bandwidth);

static s32 dvb_start_streaming(struct pd_dvb_adapter *pd_dvb);
static int poseidon_check_mode_dvbt(struct poseidon *pd)
{
	s32 ret = 0, cmd_status = 0;

	set_current_state(TASK_INTERRUPTIBLE);
	schedule_timeout(HZ/4);

	ret = usb_set_interface(pd->udev, 0, BULK_ALTERNATE_IFACE);
	if (ret != 0)
		return ret;

	ret = set_tuner_mode(pd, TLG_MODE_CAPS_DVB_T);
	if (ret)
		return ret;

	/* signal source */
	ret = send_set_req(pd, SGNL_SRC_SEL, TLG_SIG_SRC_ANTENNA, &cmd_status);
	if (ret|cmd_status)
		return ret;

	return 0;
}

/* acquire :
 * 	1 == open
 * 	0 == release
 */
static int poseidon_ts_bus_ctrl(struct dvb_frontend *fe, int acquire)
{
	struct poseidon *pd = fe->demodulator_priv;
	struct pd_dvb_adapter *pd_dvb;
	int ret = 0;

	if (!pd)
		return -ENODEV;

	pd_dvb = container_of(fe, struct pd_dvb_adapter, dvb_fe);
	if (acquire) {
		mutex_lock(&pd->lock);
		if (pd->state & POSEIDON_STATE_DISCONNECT) {
			ret = -ENODEV;
			goto open_out;
		}

		if (pd->state && !(pd->state & POSEIDON_STATE_DVBT)) {
			ret = -EBUSY;
			goto open_out;
		}

		usb_autopm_get_interface(pd->interface);
		if (0 == pd->state) {
			ret = poseidon_check_mode_dvbt(pd);
			if (ret < 0) {
				usb_autopm_put_interface(pd->interface);
				goto open_out;
			}
			pd->state |= POSEIDON_STATE_DVBT;
			pd_dvb->bandwidth = 0;
			pd_dvb->prev_freq = 0;
		}
		atomic_inc(&pd_dvb->users);
		kref_get(&pd->kref);
open_out:
		mutex_unlock(&pd->lock);
	} else {
		dvb_stop_streaming(pd_dvb);

		if (atomic_dec_and_test(&pd_dvb->users)) {
			mutex_lock(&pd->lock);
			pd->state &= ~POSEIDON_STATE_DVBT;
			mutex_unlock(&pd->lock);
		}
		kref_put(&pd->kref, poseidon_delete);
		usb_autopm_put_interface(pd->interface);
	}
	return ret;
}

#ifdef CONFIG_PM
static void poseidon_fe_release(struct dvb_frontend *fe)
{
	struct poseidon *pd = fe->demodulator_priv;

	pd->pm_suspend = NULL;
	pd->pm_resume  = NULL;
}
#else
#define poseidon_fe_release NULL
#endif

static s32 poseidon_fe_sleep(struct dvb_frontend *fe)
{
	return 0;
}

/*
 * return true if we can satisfy the conditions, else return false.
 */
static bool check_scan_ok(__u32 freq, int bandwidth,
			struct pd_dvb_adapter *adapter)
{
	if (bandwidth < 0)
		return false;

	if (adapter->prev_freq == freq
		&& adapter->bandwidth == bandwidth) {
		long nl = jiffies - adapter->last_jiffies;
		unsigned int msec ;

		msec = jiffies_to_msecs(abs(nl));
		return msec > 15000 ? true : false;
	}
	return true;
}

/*
 * Check if the firmware delays too long for an invalid frequency.
 */
static int fw_delay_overflow(struct pd_dvb_adapter *adapter)
{
	long nl = jiffies - adapter->last_jiffies;
	unsigned int msec ;

	msec = jiffies_to_msecs(abs(nl));
	return msec > 800 ? true : false;
}

static int poseidon_set_fe(struct dvb_frontend *fe)
{
	struct dtv_frontend_properties *fep = &fe->dtv_property_cache;
	s32 ret = 0, cmd_status = 0;
	s32 i, bandwidth = -1;
	struct poseidon *pd = fe->demodulator_priv;
	struct pd_dvb_adapter *pd_dvb = &pd->dvb_data;

	if (in_hibernation(pd))
		return -EBUSY;

	mutex_lock(&pd->lock);
	for (i = 0; i < dvb_bandwidth_length; i++)
		if (fep->bandwidth_hz == dvb_bandwidth[i][1])
			bandwidth = dvb_bandwidth[i][0];

	if (check_scan_ok(fep->frequency, bandwidth, pd_dvb)) {
		ret = send_set_req(pd, TUNE_FREQ_SELECT,
					fep->frequency / 1000, &cmd_status);
		if (ret | cmd_status) {
			log("error line");
			goto front_out;
		}

		ret = send_set_req(pd, DVBT_BANDW_SEL,
						bandwidth, &cmd_status);
		if (ret | cmd_status) {
			log("error line");
			goto front_out;
		}

		ret = send_set_req(pd, TAKE_REQUEST, 0, &cmd_status);
		if (ret | cmd_status) {
			log("error line");
			goto front_out;
		}

		/* save the context for future */
		memcpy(&pd_dvb->fe_param, fep, sizeof(*fep));
		pd_dvb->bandwidth = bandwidth;
		pd_dvb->prev_freq = fep->frequency;
		pd_dvb->last_jiffies = jiffies;
	}
front_out:
	mutex_unlock(&pd->lock);
	return ret;
}

#ifdef CONFIG_PM
static int pm_dvb_suspend(struct poseidon *pd)
{
	struct pd_dvb_adapter *pd_dvb = &pd->dvb_data;
	dvb_stop_streaming(pd_dvb);
	dvb_urb_cleanup(pd_dvb);
	msleep(500);
	return 0;
}

static int pm_dvb_resume(struct poseidon *pd)
{
	struct pd_dvb_adapter *pd_dvb = &pd->dvb_data;

	poseidon_check_mode_dvbt(pd);
	msleep(300);
	poseidon_set_fe(&pd_dvb->dvb_fe);

	dvb_start_streaming(pd_dvb);
	return 0;
}
#endif

static s32 poseidon_fe_init(struct dvb_frontend *fe)
{
	struct poseidon *pd = fe->demodulator_priv;
	struct pd_dvb_adapter *pd_dvb = &pd->dvb_data;

#ifdef CONFIG_PM
	pd->pm_suspend = pm_dvb_suspend;
	pd->pm_resume  = pm_dvb_resume;
#endif
	memset(&pd_dvb->fe_param, 0,
			sizeof(struct dtv_frontend_properties));
	return 0;
}

static int poseidon_get_fe(struct dvb_frontend *fe)
{
	struct dtv_frontend_properties *fep = &fe->dtv_property_cache;
	struct poseidon *pd = fe->demodulator_priv;
	struct pd_dvb_adapter *pd_dvb = &pd->dvb_data;

	memcpy(fep, &pd_dvb->fe_param, sizeof(*fep));
	return 0;
}

static int poseidon_fe_get_tune_settings(struct dvb_frontend *fe,
				struct dvb_frontend_tune_settings *tune)
{
	tune->min_delay_ms = 1000;
	return 0;
}

static int poseidon_read_status(struct dvb_frontend *fe, fe_status_t *stat)
{
	struct poseidon *pd = fe->demodulator_priv;
	s32 ret = -1, cmd_status;
	struct tuner_dtv_sig_stat_s status = {};

	if (in_hibernation(pd))
		return -EBUSY;
	mutex_lock(&pd->lock);

	ret = send_get_req(pd, TUNER_STATUS, TLG_MODE_DVB_T,
				&status, &cmd_status, sizeof(status));
	if (ret | cmd_status) {
		log("get tuner status error");
		goto out;
	}

	if (debug_mode)
		log("P : %d, L %d, LB :%d", status.sig_present,
			status.sig_locked, status.sig_lock_busy);

	if (status.sig_lock_busy) {
		goto out;
	} else if (status.sig_present || status.sig_locked) {
		*stat |= FE_HAS_LOCK | FE_HAS_SIGNAL | FE_HAS_CARRIER
				| FE_HAS_SYNC | FE_HAS_VITERBI;
	} else {
		if (fw_delay_overflow(&pd->dvb_data))
			*stat |= FE_TIMEDOUT;
	}
out:
	mutex_unlock(&pd->lock);
	return ret;
}

static int poseidon_read_ber(struct dvb_frontend *fe, u32 *ber)
{
	struct poseidon *pd = fe->demodulator_priv;
	struct tuner_ber_rate_s tlg_ber = {};
	s32 ret = -1, cmd_status;

	mutex_lock(&pd->lock);
	ret = send_get_req(pd, TUNER_BER_RATE, 0,
				&tlg_ber, &cmd_status, sizeof(tlg_ber));
	if (ret | cmd_status)
		goto out;
	*ber = tlg_ber.ber_rate;
out:
	mutex_unlock(&pd->lock);
	return ret;
}

static s32 poseidon_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
{
	struct poseidon *pd = fe->demodulator_priv;
	struct tuner_dtv_sig_stat_s status = {};
	s32 ret = 0, cmd_status;

	mutex_lock(&pd->lock);
	ret = send_get_req(pd, TUNER_STATUS, TLG_MODE_DVB_T,
				&status, &cmd_status, sizeof(status));
	if (ret | cmd_status)
		goto out;
	if ((status.sig_present || status.sig_locked) && !status.sig_strength)
		*strength = 0xFFFF;
	else
		*strength = status.sig_strength;
out:
	mutex_unlock(&pd->lock);
	return ret;
}

static int poseidon_read_snr(struct dvb_frontend *fe, u16 *snr)
{
	return 0;
}

static int poseidon_read_unc_blocks(struct dvb_frontend *fe, u32 *unc)
{
	*unc = 0;
	return 0;
}

static struct dvb_frontend_ops poseidon_frontend_ops = {
	.delsys = { SYS_DVBT },
	.info = {
		.name		= "Poseidon DVB-T",
		.frequency_min	= 174000000,
		.frequency_max  = 862000000,
		.frequency_stepsize	  = 62500,/* FIXME */
		.caps = FE_CAN_INVERSION_AUTO |
			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,
	},

	.release = poseidon_fe_release,

	.init = poseidon_fe_init,
	.sleep = poseidon_fe_sleep,

	.set_frontend = poseidon_set_fe,
	.get_frontend = poseidon_get_fe,
	.get_tune_settings = poseidon_fe_get_tune_settings,

	.read_status	= poseidon_read_status,
	.read_ber	= poseidon_read_ber,
	.read_signal_strength = poseidon_read_signal_strength,
	.read_snr	= poseidon_read_snr,
	.read_ucblocks	= poseidon_read_unc_blocks,

	.ts_bus_ctrl = poseidon_ts_bus_ctrl,
};

static void dvb_urb_irq(struct urb *urb)
{
	struct pd_dvb_adapter *pd_dvb = urb->context;
	int len = urb->transfer_buffer_length;
	struct dvb_demux *demux = &pd_dvb->demux;
	s32 ret;

	if (!pd_dvb->is_streaming || urb->status) {
		if (urb->status == -EPROTO)
			goto resend;
		return;
	}

	if (urb->actual_length == len)
		dvb_dmx_swfilter(demux, urb->transfer_buffer, len);
	else if (urb->actual_length == len - 4) {
		int offset;
		u8 *buf = urb->transfer_buffer;

		/*
		 * The packet size is 512,
		 * last packet contains 456 bytes tsp data
		 */
		for (offset = 456; offset < len; offset += 512) {
			if (!strncmp(buf + offset, "DVHS", 4)) {
				dvb_dmx_swfilter(demux, buf, offset);
				if (len > offset + 52 + 4) {
					/*16 bytes trailer + 36 bytes padding */
					buf += offset + 52;
					len -= offset + 52 + 4;
					dvb_dmx_swfilter(demux, buf, len);
				}
				break;
			}
		}
	}

resend:
	ret = usb_submit_urb(urb, GFP_ATOMIC);
	if (ret)
		log(" usb_submit_urb failed: error %d", ret);
}

static int dvb_urb_init(struct pd_dvb_adapter *pd_dvb)
{
	if (pd_dvb->urb_array[0])
		return 0;

	alloc_bulk_urbs_generic(pd_dvb->urb_array, DVB_SBUF_NUM,
			pd_dvb->pd_device->udev, pd_dvb->ep_addr,
			DVB_URB_BUF_SIZE, GFP_KERNEL,
			dvb_urb_irq, pd_dvb);
	return 0;
}

static void dvb_urb_cleanup(struct pd_dvb_adapter *pd_dvb)
{
	free_all_urb_generic(pd_dvb->urb_array, DVB_SBUF_NUM);
}

static s32 dvb_start_streaming(struct pd_dvb_adapter *pd_dvb)
{
	struct poseidon *pd = pd_dvb->pd_device;
	int ret = 0;

	if (pd->state & POSEIDON_STATE_DISCONNECT)
		return -ENODEV;

	mutex_lock(&pd->lock);
	if (!pd_dvb->is_streaming) {
		s32 i, cmd_status = 0;
		/*
		 * Once upon a time, there was a difficult bug lying here.
		 * ret = send_set_req(pd, TAKE_REQUEST, 0, &cmd_status);
		 */

		ret = send_set_req(pd, PLAY_SERVICE, 1, &cmd_status);
		if (ret | cmd_status)
			goto out;

		ret = dvb_urb_init(pd_dvb);
		if (ret < 0)
			goto out;

		pd_dvb->is_streaming = 1;
		for (i = 0; i < DVB_SBUF_NUM; i++) {
			ret = usb_submit_urb(pd_dvb->urb_array[i],
						       GFP_KERNEL);
			if (ret) {
				log(" submit urb error %d", ret);
				goto out;
			}
		}
	}
out:
	mutex_unlock(&pd->lock);
	return ret;
}

void dvb_stop_streaming(struct pd_dvb_adapter *pd_dvb)
{
	struct poseidon *pd = pd_dvb->pd_device;

	mutex_lock(&pd->lock);
	if (pd_dvb->is_streaming) {
		s32 i, ret, cmd_status = 0;

		pd_dvb->is_streaming = 0;

		for (i = 0; i < DVB_SBUF_NUM; i++)
			if (pd_dvb->urb_array[i])
				usb_kill_urb(pd_dvb->urb_array[i]);

		ret = send_set_req(pd, PLAY_SERVICE, TLG_TUNE_PLAY_SVC_STOP,
					&cmd_status);
		if (ret | cmd_status)
			log("error");
	}
	mutex_unlock(&pd->lock);
}

static int pd_start_feed(struct dvb_demux_feed *feed)
{
	struct pd_dvb_adapter *pd_dvb = feed->demux->priv;
	int ret = 0;

	if (!pd_dvb)
		return -1;
	if (atomic_inc_return(&pd_dvb->active_feed) == 1)
		ret = dvb_start_streaming(pd_dvb);
	return ret;
}

static int pd_stop_feed(struct dvb_demux_feed *feed)
{
	struct pd_dvb_adapter *pd_dvb = feed->demux->priv;

	if (!pd_dvb)
		return -1;
	if (atomic_dec_and_test(&pd_dvb->active_feed))
		dvb_stop_streaming(pd_dvb);
	return 0;
}

DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
int pd_dvb_usb_device_init(struct poseidon *pd)
{
	struct pd_dvb_adapter *pd_dvb = &pd->dvb_data;
	struct dvb_demux *dvbdemux;
	int ret = 0;

	pd_dvb->ep_addr = 0x82;
	atomic_set(&pd_dvb->users, 0);
	atomic_set(&pd_dvb->active_feed, 0);
	pd_dvb->pd_device = pd;

	ret = dvb_register_adapter(&pd_dvb->dvb_adap,
				"Poseidon dvbt adapter",
				THIS_MODULE,
				NULL /* for hibernation correctly*/,
				adapter_nr);
	if (ret < 0)
		goto error1;

	/* register frontend */
	pd_dvb->dvb_fe.demodulator_priv = pd;
	memcpy(&pd_dvb->dvb_fe.ops, &poseidon_frontend_ops,
			sizeof(struct dvb_frontend_ops));
	ret = dvb_register_frontend(&pd_dvb->dvb_adap, &pd_dvb->dvb_fe);
	if (ret < 0)
		goto error2;

	/* register demux device */
	dvbdemux = &pd_dvb->demux;
	dvbdemux->dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING;
	dvbdemux->priv = pd_dvb;
	dvbdemux->feednum = dvbdemux->filternum = 64;
	dvbdemux->start_feed = pd_start_feed;
	dvbdemux->stop_feed = pd_stop_feed;
	dvbdemux->write_to_decoder = NULL;

	ret = dvb_dmx_init(dvbdemux);
	if (ret < 0)
		goto error3;

	pd_dvb->dmxdev.filternum = pd_dvb->demux.filternum;
	pd_dvb->dmxdev.demux = &pd_dvb->demux.dmx;
	pd_dvb->dmxdev.capabilities = 0;

	ret = dvb_dmxdev_init(&pd_dvb->dmxdev, &pd_dvb->dvb_adap);
	if (ret < 0)
		goto error3;
	return 0;

error3:
	dvb_unregister_frontend(&pd_dvb->dvb_fe);
error2:
	dvb_unregister_adapter(&pd_dvb->dvb_adap);
error1:
	return ret;
}

void pd_dvb_usb_device_exit(struct poseidon *pd)
{
	struct pd_dvb_adapter *pd_dvb = &pd->dvb_data;

	while (atomic_read(&pd_dvb->users) != 0
		|| atomic_read(&pd_dvb->active_feed) != 0) {
		set_current_state(TASK_INTERRUPTIBLE);
		schedule_timeout(HZ);
	}
	dvb_dmxdev_release(&pd_dvb->dmxdev);
	dvb_unregister_frontend(&pd_dvb->dvb_fe);
	dvb_unregister_adapter(&pd_dvb->dvb_adap);
	pd_dvb_usb_device_cleanup(pd);
}

void pd_dvb_usb_device_cleanup(struct poseidon *pd)
{
	struct pd_dvb_adapter *pd_dvb = &pd->dvb_data;

	dvb_urb_cleanup(pd_dvb);
}

int pd_dvb_get_adapter_num(struct pd_dvb_adapter *pd_dvb)
{
	return pd_dvb->dvb_adap.num;
}
OpenPOWER on IntegriCloud