blob: 799881165ebe2548c45abcf5f881e74918e4f443 [file] [log] [blame]
Huang Shijie5b3f03f2010-02-02 04:07:47 -03001#include "pd-common.h"
2#include <linux/kernel.h>
3#include <linux/usb.h>
4#include <linux/dvb/dmx.h>
5#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09006#include <linux/gfp.h>
Huang Shijie5b3f03f2010-02-02 04:07:47 -03007
8#include "vendorcmds.h"
9#include <linux/sched.h>
Arun Sharma600634972011-07-26 16:09:06 -070010#include <linux/atomic.h>
Huang Shijie5b3f03f2010-02-02 04:07:47 -030011
12static void dvb_urb_cleanup(struct pd_dvb_adapter *pd_dvb);
13
14static int dvb_bandwidth[][2] = {
Mauro Carvalho Chehab2827e1f2011-12-26 16:40:17 -030015 { TLG_BW_8, 8000000 },
16 { TLG_BW_7, 7000000 },
17 { TLG_BW_6, 6000000 }
Huang Shijie5b3f03f2010-02-02 04:07:47 -030018};
19static int dvb_bandwidth_length = ARRAY_SIZE(dvb_bandwidth);
20
21static s32 dvb_start_streaming(struct pd_dvb_adapter *pd_dvb);
22static int poseidon_check_mode_dvbt(struct poseidon *pd)
23{
24 s32 ret = 0, cmd_status = 0;
25
26 set_current_state(TASK_INTERRUPTIBLE);
27 schedule_timeout(HZ/4);
28
29 ret = usb_set_interface(pd->udev, 0, BULK_ALTERNATE_IFACE);
30 if (ret != 0)
31 return ret;
32
33 ret = set_tuner_mode(pd, TLG_MODE_CAPS_DVB_T);
34 if (ret)
35 return ret;
36
37 /* signal source */
38 ret = send_set_req(pd, SGNL_SRC_SEL, TLG_SIG_SRC_ANTENNA, &cmd_status);
39 if (ret|cmd_status)
40 return ret;
41
42 return 0;
43}
44
45/* acquire :
46 * 1 == open
47 * 0 == release
48 */
49static int poseidon_ts_bus_ctrl(struct dvb_frontend *fe, int acquire)
50{
51 struct poseidon *pd = fe->demodulator_priv;
52 struct pd_dvb_adapter *pd_dvb;
53 int ret = 0;
54
55 if (!pd)
56 return -ENODEV;
57
58 pd_dvb = container_of(fe, struct pd_dvb_adapter, dvb_fe);
59 if (acquire) {
60 mutex_lock(&pd->lock);
61 if (pd->state & POSEIDON_STATE_DISCONNECT) {
62 ret = -ENODEV;
63 goto open_out;
64 }
65
66 if (pd->state && !(pd->state & POSEIDON_STATE_DVBT)) {
67 ret = -EBUSY;
68 goto open_out;
69 }
70
71 usb_autopm_get_interface(pd->interface);
72 if (0 == pd->state) {
73 ret = poseidon_check_mode_dvbt(pd);
74 if (ret < 0) {
75 usb_autopm_put_interface(pd->interface);
76 goto open_out;
77 }
78 pd->state |= POSEIDON_STATE_DVBT;
79 pd_dvb->bandwidth = 0;
80 pd_dvb->prev_freq = 0;
81 }
82 atomic_inc(&pd_dvb->users);
83 kref_get(&pd->kref);
84open_out:
85 mutex_unlock(&pd->lock);
86 } else {
87 dvb_stop_streaming(pd_dvb);
88
89 if (atomic_dec_and_test(&pd_dvb->users)) {
90 mutex_lock(&pd->lock);
91 pd->state &= ~POSEIDON_STATE_DVBT;
92 mutex_unlock(&pd->lock);
93 }
94 kref_put(&pd->kref, poseidon_delete);
95 usb_autopm_put_interface(pd->interface);
96 }
97 return ret;
98}
99
Márton Németh6c9eaa82010-02-28 11:19:47 -0300100#ifdef CONFIG_PM
Huang Shijie5b3f03f2010-02-02 04:07:47 -0300101static void poseidon_fe_release(struct dvb_frontend *fe)
102{
103 struct poseidon *pd = fe->demodulator_priv;
104
Huang Shijie5b3f03f2010-02-02 04:07:47 -0300105 pd->pm_suspend = NULL;
106 pd->pm_resume = NULL;
Huang Shijie5b3f03f2010-02-02 04:07:47 -0300107}
Márton Németh6c9eaa82010-02-28 11:19:47 -0300108#else
109#define poseidon_fe_release NULL
110#endif
Huang Shijie5b3f03f2010-02-02 04:07:47 -0300111
112static s32 poseidon_fe_sleep(struct dvb_frontend *fe)
113{
114 return 0;
115}
116
117/*
118 * return true if we can satisfy the conditions, else return false.
119 */
120static bool check_scan_ok(__u32 freq, int bandwidth,
121 struct pd_dvb_adapter *adapter)
122{
123 if (bandwidth < 0)
124 return false;
125
126 if (adapter->prev_freq == freq
127 && adapter->bandwidth == bandwidth) {
128 long nl = jiffies - adapter->last_jiffies;
129 unsigned int msec ;
130
131 msec = jiffies_to_msecs(abs(nl));
132 return msec > 15000 ? true : false;
133 }
134 return true;
135}
136
137/*
138 * Check if the firmware delays too long for an invalid frequency.
139 */
140static int fw_delay_overflow(struct pd_dvb_adapter *adapter)
141{
142 long nl = jiffies - adapter->last_jiffies;
143 unsigned int msec ;
144
145 msec = jiffies_to_msecs(abs(nl));
146 return msec > 800 ? true : false;
147}
148
Mauro Carvalho Chehab2827e1f2011-12-26 16:40:17 -0300149static int poseidon_set_fe(struct dvb_frontend *fe)
Huang Shijie5b3f03f2010-02-02 04:07:47 -0300150{
Mauro Carvalho Chehab2827e1f2011-12-26 16:40:17 -0300151 struct dtv_frontend_properties *fep = &fe->dtv_property_cache;
Huang Shijie5b3f03f2010-02-02 04:07:47 -0300152 s32 ret = 0, cmd_status = 0;
153 s32 i, bandwidth = -1;
154 struct poseidon *pd = fe->demodulator_priv;
155 struct pd_dvb_adapter *pd_dvb = &pd->dvb_data;
156
157 if (in_hibernation(pd))
158 return -EBUSY;
159
160 mutex_lock(&pd->lock);
161 for (i = 0; i < dvb_bandwidth_length; i++)
Mauro Carvalho Chehab2827e1f2011-12-26 16:40:17 -0300162 if (fep->bandwidth_hz == dvb_bandwidth[i][1])
Huang Shijie5b3f03f2010-02-02 04:07:47 -0300163 bandwidth = dvb_bandwidth[i][0];
164
165 if (check_scan_ok(fep->frequency, bandwidth, pd_dvb)) {
166 ret = send_set_req(pd, TUNE_FREQ_SELECT,
167 fep->frequency / 1000, &cmd_status);
168 if (ret | cmd_status) {
169 log("error line");
170 goto front_out;
171 }
172
173 ret = send_set_req(pd, DVBT_BANDW_SEL,
174 bandwidth, &cmd_status);
175 if (ret | cmd_status) {
176 log("error line");
177 goto front_out;
178 }
179
180 ret = send_set_req(pd, TAKE_REQUEST, 0, &cmd_status);
181 if (ret | cmd_status) {
182 log("error line");
183 goto front_out;
184 }
185
186 /* save the context for future */
187 memcpy(&pd_dvb->fe_param, fep, sizeof(*fep));
188 pd_dvb->bandwidth = bandwidth;
189 pd_dvb->prev_freq = fep->frequency;
190 pd_dvb->last_jiffies = jiffies;
191 }
192front_out:
193 mutex_unlock(&pd->lock);
194 return ret;
195}
196
197#ifdef CONFIG_PM
198static int pm_dvb_suspend(struct poseidon *pd)
199{
200 struct pd_dvb_adapter *pd_dvb = &pd->dvb_data;
201 dvb_stop_streaming(pd_dvb);
202 dvb_urb_cleanup(pd_dvb);
203 msleep(500);
204 return 0;
205}
206
207static int pm_dvb_resume(struct poseidon *pd)
208{
209 struct pd_dvb_adapter *pd_dvb = &pd->dvb_data;
210
211 poseidon_check_mode_dvbt(pd);
212 msleep(300);
Mauro Carvalho Chehab2827e1f2011-12-26 16:40:17 -0300213 poseidon_set_fe(&pd_dvb->dvb_fe);
Huang Shijie5b3f03f2010-02-02 04:07:47 -0300214
215 dvb_start_streaming(pd_dvb);
216 return 0;
217}
218#endif
219
220static s32 poseidon_fe_init(struct dvb_frontend *fe)
221{
222 struct poseidon *pd = fe->demodulator_priv;
223 struct pd_dvb_adapter *pd_dvb = &pd->dvb_data;
224
225#ifdef CONFIG_PM
226 pd->pm_suspend = pm_dvb_suspend;
227 pd->pm_resume = pm_dvb_resume;
228#endif
229 memset(&pd_dvb->fe_param, 0,
Mauro Carvalho Chehab2827e1f2011-12-26 16:40:17 -0300230 sizeof(struct dtv_frontend_properties));
Huang Shijie5b3f03f2010-02-02 04:07:47 -0300231 return 0;
232}
233
234static int poseidon_get_fe(struct dvb_frontend *fe,
Mauro Carvalho Chehab2827e1f2011-12-26 16:40:17 -0300235 struct dtv_frontend_properties *fep)
Huang Shijie5b3f03f2010-02-02 04:07:47 -0300236{
237 struct poseidon *pd = fe->demodulator_priv;
238 struct pd_dvb_adapter *pd_dvb = &pd->dvb_data;
239
240 memcpy(fep, &pd_dvb->fe_param, sizeof(*fep));
241 return 0;
242}
243
244static int poseidon_fe_get_tune_settings(struct dvb_frontend *fe,
245 struct dvb_frontend_tune_settings *tune)
246{
247 tune->min_delay_ms = 1000;
248 return 0;
249}
250
251static int poseidon_read_status(struct dvb_frontend *fe, fe_status_t *stat)
252{
253 struct poseidon *pd = fe->demodulator_priv;
254 s32 ret = -1, cmd_status;
255 struct tuner_dtv_sig_stat_s status = {};
256
257 if (in_hibernation(pd))
258 return -EBUSY;
259 mutex_lock(&pd->lock);
260
261 ret = send_get_req(pd, TUNER_STATUS, TLG_MODE_DVB_T,
262 &status, &cmd_status, sizeof(status));
263 if (ret | cmd_status) {
264 log("get tuner status error");
265 goto out;
266 }
267
268 if (debug_mode)
269 log("P : %d, L %d, LB :%d", status.sig_present,
270 status.sig_locked, status.sig_lock_busy);
271
272 if (status.sig_lock_busy) {
273 goto out;
274 } else if (status.sig_present || status.sig_locked) {
275 *stat |= FE_HAS_LOCK | FE_HAS_SIGNAL | FE_HAS_CARRIER
276 | FE_HAS_SYNC | FE_HAS_VITERBI;
277 } else {
278 if (fw_delay_overflow(&pd->dvb_data))
279 *stat |= FE_TIMEDOUT;
280 }
281out:
282 mutex_unlock(&pd->lock);
283 return ret;
284}
285
286static int poseidon_read_ber(struct dvb_frontend *fe, u32 *ber)
287{
288 struct poseidon *pd = fe->demodulator_priv;
289 struct tuner_ber_rate_s tlg_ber = {};
290 s32 ret = -1, cmd_status;
291
292 mutex_lock(&pd->lock);
293 ret = send_get_req(pd, TUNER_BER_RATE, 0,
294 &tlg_ber, &cmd_status, sizeof(tlg_ber));
295 if (ret | cmd_status)
296 goto out;
297 *ber = tlg_ber.ber_rate;
298out:
299 mutex_unlock(&pd->lock);
300 return ret;
301}
302
303static s32 poseidon_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
304{
305 struct poseidon *pd = fe->demodulator_priv;
306 struct tuner_dtv_sig_stat_s status = {};
307 s32 ret = 0, cmd_status;
308
309 mutex_lock(&pd->lock);
310 ret = send_get_req(pd, TUNER_STATUS, TLG_MODE_DVB_T,
311 &status, &cmd_status, sizeof(status));
312 if (ret | cmd_status)
313 goto out;
314 if ((status.sig_present || status.sig_locked) && !status.sig_strength)
315 *strength = 0xFFFF;
316 else
317 *strength = status.sig_strength;
318out:
319 mutex_unlock(&pd->lock);
320 return ret;
321}
322
323static int poseidon_read_snr(struct dvb_frontend *fe, u16 *snr)
324{
325 return 0;
326}
327
328static int poseidon_read_unc_blocks(struct dvb_frontend *fe, u32 *unc)
329{
330 *unc = 0;
331 return 0;
332}
333
334static struct dvb_frontend_ops poseidon_frontend_ops = {
Mauro Carvalho Chehab2827e1f2011-12-26 16:40:17 -0300335 .delsys = { SYS_DVBT },
Huang Shijie5b3f03f2010-02-02 04:07:47 -0300336 .info = {
337 .name = "Poseidon DVB-T",
338 .type = FE_OFDM,
339 .frequency_min = 174000000,
340 .frequency_max = 862000000,
341 .frequency_stepsize = 62500,/* FIXME */
342 .caps = FE_CAN_INVERSION_AUTO |
343 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
344 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
345 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
346 FE_CAN_QAM_AUTO | FE_CAN_TRANSMISSION_MODE_AUTO |
347 FE_CAN_GUARD_INTERVAL_AUTO |
348 FE_CAN_RECOVER |
349 FE_CAN_HIERARCHY_AUTO,
350 },
351
352 .release = poseidon_fe_release,
353
354 .init = poseidon_fe_init,
355 .sleep = poseidon_fe_sleep,
356
Mauro Carvalho Chehab2827e1f2011-12-26 16:40:17 -0300357 .set_frontend = poseidon_set_fe,
358 .get_frontend = poseidon_get_fe,
Huang Shijie5b3f03f2010-02-02 04:07:47 -0300359 .get_tune_settings = poseidon_fe_get_tune_settings,
360
361 .read_status = poseidon_read_status,
362 .read_ber = poseidon_read_ber,
363 .read_signal_strength = poseidon_read_signal_strength,
364 .read_snr = poseidon_read_snr,
365 .read_ucblocks = poseidon_read_unc_blocks,
366
367 .ts_bus_ctrl = poseidon_ts_bus_ctrl,
368};
369
370static void dvb_urb_irq(struct urb *urb)
371{
372 struct pd_dvb_adapter *pd_dvb = urb->context;
373 int len = urb->transfer_buffer_length;
374 struct dvb_demux *demux = &pd_dvb->demux;
375 s32 ret;
376
377 if (!pd_dvb->is_streaming || urb->status) {
378 if (urb->status == -EPROTO)
379 goto resend;
380 return;
381 }
382
383 if (urb->actual_length == len)
384 dvb_dmx_swfilter(demux, urb->transfer_buffer, len);
385 else if (urb->actual_length == len - 4) {
386 int offset;
387 u8 *buf = urb->transfer_buffer;
388
389 /*
390 * The packet size is 512,
391 * last packet contains 456 bytes tsp data
392 */
393 for (offset = 456; offset < len; offset += 512) {
394 if (!strncmp(buf + offset, "DVHS", 4)) {
395 dvb_dmx_swfilter(demux, buf, offset);
396 if (len > offset + 52 + 4) {
397 /*16 bytes trailer + 36 bytes padding */
398 buf += offset + 52;
399 len -= offset + 52 + 4;
400 dvb_dmx_swfilter(demux, buf, len);
401 }
402 break;
403 }
404 }
405 }
406
407resend:
408 ret = usb_submit_urb(urb, GFP_ATOMIC);
409 if (ret)
410 log(" usb_submit_urb failed: error %d", ret);
411}
412
413static int dvb_urb_init(struct pd_dvb_adapter *pd_dvb)
414{
415 if (pd_dvb->urb_array[0])
416 return 0;
417
418 alloc_bulk_urbs_generic(pd_dvb->urb_array, DVB_SBUF_NUM,
419 pd_dvb->pd_device->udev, pd_dvb->ep_addr,
420 DVB_URB_BUF_SIZE, GFP_KERNEL,
421 dvb_urb_irq, pd_dvb);
422 return 0;
423}
424
425static void dvb_urb_cleanup(struct pd_dvb_adapter *pd_dvb)
426{
427 free_all_urb_generic(pd_dvb->urb_array, DVB_SBUF_NUM);
428}
429
430static s32 dvb_start_streaming(struct pd_dvb_adapter *pd_dvb)
431{
432 struct poseidon *pd = pd_dvb->pd_device;
433 int ret = 0;
434
435 if (pd->state & POSEIDON_STATE_DISCONNECT)
436 return -ENODEV;
437
438 mutex_lock(&pd->lock);
439 if (!pd_dvb->is_streaming) {
440 s32 i, cmd_status = 0;
441 /*
442 * Once upon a time, there was a difficult bug lying here.
443 * ret = send_set_req(pd, TAKE_REQUEST, 0, &cmd_status);
444 */
445
446 ret = send_set_req(pd, PLAY_SERVICE, 1, &cmd_status);
447 if (ret | cmd_status)
448 goto out;
449
450 ret = dvb_urb_init(pd_dvb);
451 if (ret < 0)
452 goto out;
453
454 pd_dvb->is_streaming = 1;
455 for (i = 0; i < DVB_SBUF_NUM; i++) {
456 ret = usb_submit_urb(pd_dvb->urb_array[i],
457 GFP_KERNEL);
458 if (ret) {
459 log(" submit urb error %d", ret);
460 goto out;
461 }
462 }
463 }
464out:
465 mutex_unlock(&pd->lock);
466 return ret;
467}
468
469void dvb_stop_streaming(struct pd_dvb_adapter *pd_dvb)
470{
471 struct poseidon *pd = pd_dvb->pd_device;
472
473 mutex_lock(&pd->lock);
474 if (pd_dvb->is_streaming) {
475 s32 i, ret, cmd_status = 0;
476
477 pd_dvb->is_streaming = 0;
478
479 for (i = 0; i < DVB_SBUF_NUM; i++)
480 if (pd_dvb->urb_array[i])
481 usb_kill_urb(pd_dvb->urb_array[i]);
482
483 ret = send_set_req(pd, PLAY_SERVICE, TLG_TUNE_PLAY_SVC_STOP,
484 &cmd_status);
485 if (ret | cmd_status)
486 log("error");
487 }
488 mutex_unlock(&pd->lock);
489}
490
491static int pd_start_feed(struct dvb_demux_feed *feed)
492{
493 struct pd_dvb_adapter *pd_dvb = feed->demux->priv;
494 int ret = 0;
495
496 if (!pd_dvb)
497 return -1;
498 if (atomic_inc_return(&pd_dvb->active_feed) == 1)
499 ret = dvb_start_streaming(pd_dvb);
500 return ret;
501}
502
503static int pd_stop_feed(struct dvb_demux_feed *feed)
504{
505 struct pd_dvb_adapter *pd_dvb = feed->demux->priv;
506
507 if (!pd_dvb)
508 return -1;
509 if (atomic_dec_and_test(&pd_dvb->active_feed))
510 dvb_stop_streaming(pd_dvb);
511 return 0;
512}
513
514DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
515int pd_dvb_usb_device_init(struct poseidon *pd)
516{
517 struct pd_dvb_adapter *pd_dvb = &pd->dvb_data;
518 struct dvb_demux *dvbdemux;
519 int ret = 0;
520
521 pd_dvb->ep_addr = 0x82;
522 atomic_set(&pd_dvb->users, 0);
523 atomic_set(&pd_dvb->active_feed, 0);
524 pd_dvb->pd_device = pd;
525
526 ret = dvb_register_adapter(&pd_dvb->dvb_adap,
527 "Poseidon dvbt adapter",
528 THIS_MODULE,
529 NULL /* for hibernation correctly*/,
530 adapter_nr);
531 if (ret < 0)
532 goto error1;
533
534 /* register frontend */
535 pd_dvb->dvb_fe.demodulator_priv = pd;
536 memcpy(&pd_dvb->dvb_fe.ops, &poseidon_frontend_ops,
537 sizeof(struct dvb_frontend_ops));
538 ret = dvb_register_frontend(&pd_dvb->dvb_adap, &pd_dvb->dvb_fe);
539 if (ret < 0)
540 goto error2;
541
542 /* register demux device */
543 dvbdemux = &pd_dvb->demux;
544 dvbdemux->dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING;
545 dvbdemux->priv = pd_dvb;
546 dvbdemux->feednum = dvbdemux->filternum = 64;
547 dvbdemux->start_feed = pd_start_feed;
548 dvbdemux->stop_feed = pd_stop_feed;
549 dvbdemux->write_to_decoder = NULL;
550
551 ret = dvb_dmx_init(dvbdemux);
552 if (ret < 0)
553 goto error3;
554
555 pd_dvb->dmxdev.filternum = pd_dvb->demux.filternum;
556 pd_dvb->dmxdev.demux = &pd_dvb->demux.dmx;
557 pd_dvb->dmxdev.capabilities = 0;
558
559 ret = dvb_dmxdev_init(&pd_dvb->dmxdev, &pd_dvb->dvb_adap);
560 if (ret < 0)
561 goto error3;
562 return 0;
563
564error3:
565 dvb_unregister_frontend(&pd_dvb->dvb_fe);
566error2:
567 dvb_unregister_adapter(&pd_dvb->dvb_adap);
568error1:
569 return ret;
570}
571
572void pd_dvb_usb_device_exit(struct poseidon *pd)
573{
574 struct pd_dvb_adapter *pd_dvb = &pd->dvb_data;
575
576 while (atomic_read(&pd_dvb->users) != 0
577 || atomic_read(&pd_dvb->active_feed) != 0) {
578 set_current_state(TASK_INTERRUPTIBLE);
579 schedule_timeout(HZ);
580 }
581 dvb_dmxdev_release(&pd_dvb->dmxdev);
582 dvb_unregister_frontend(&pd_dvb->dvb_fe);
583 dvb_unregister_adapter(&pd_dvb->dvb_adap);
584 pd_dvb_usb_device_cleanup(pd);
585}
586
587void pd_dvb_usb_device_cleanup(struct poseidon *pd)
588{
589 struct pd_dvb_adapter *pd_dvb = &pd->dvb_data;
590
591 dvb_urb_cleanup(pd_dvb);
592}
593
594int pd_dvb_get_adapter_num(struct pd_dvb_adapter *pd_dvb)
595{
596 return pd_dvb->dvb_adap.num;
597}