2 #include <linux/kernel.h>
4 #include <linux/time.h>
5 #include <linux/dvb/dmx.h>
6 #include <linux/delay.h>
9 #include "vendorcmds.h"
10 #include <linux/sched.h>
11 #include <linux/atomic.h>
13 static void dvb_urb_cleanup(struct pd_dvb_adapter
*pd_dvb
);
15 static int dvb_bandwidth
[][2] = {
16 { TLG_BW_8
, 8000000 },
17 { TLG_BW_7
, 7000000 },
20 static int dvb_bandwidth_length
= ARRAY_SIZE(dvb_bandwidth
);
22 static s32
dvb_start_streaming(struct pd_dvb_adapter
*pd_dvb
);
23 static int poseidon_check_mode_dvbt(struct poseidon
*pd
)
25 s32 ret
= 0, cmd_status
= 0;
27 set_current_state(TASK_INTERRUPTIBLE
);
28 schedule_timeout(HZ
/4);
30 ret
= usb_set_interface(pd
->udev
, 0, BULK_ALTERNATE_IFACE
);
34 ret
= set_tuner_mode(pd
, TLG_MODE_CAPS_DVB_T
);
39 ret
= send_set_req(pd
, SGNL_SRC_SEL
, TLG_SIG_SRC_ANTENNA
, &cmd_status
);
50 static int poseidon_ts_bus_ctrl(struct dvb_frontend
*fe
, int acquire
)
52 struct poseidon
*pd
= fe
->demodulator_priv
;
53 struct pd_dvb_adapter
*pd_dvb
;
59 pd_dvb
= container_of(fe
, struct pd_dvb_adapter
, dvb_fe
);
61 mutex_lock(&pd
->lock
);
62 if (pd
->state
& POSEIDON_STATE_DISCONNECT
) {
67 if (pd
->state
&& !(pd
->state
& POSEIDON_STATE_DVBT
)) {
72 usb_autopm_get_interface(pd
->interface
);
74 ret
= poseidon_check_mode_dvbt(pd
);
76 usb_autopm_put_interface(pd
->interface
);
79 pd
->state
|= POSEIDON_STATE_DVBT
;
80 pd_dvb
->bandwidth
= 0;
81 pd_dvb
->prev_freq
= 0;
83 atomic_inc(&pd_dvb
->users
);
86 mutex_unlock(&pd
->lock
);
88 dvb_stop_streaming(pd_dvb
);
90 if (atomic_dec_and_test(&pd_dvb
->users
)) {
91 mutex_lock(&pd
->lock
);
92 pd
->state
&= ~POSEIDON_STATE_DVBT
;
93 mutex_unlock(&pd
->lock
);
95 kref_put(&pd
->kref
, poseidon_delete
);
96 usb_autopm_put_interface(pd
->interface
);
102 static void poseidon_fe_release(struct dvb_frontend
*fe
)
104 struct poseidon
*pd
= fe
->demodulator_priv
;
106 pd
->pm_suspend
= NULL
;
107 pd
->pm_resume
= NULL
;
110 #define poseidon_fe_release NULL
113 static s32
poseidon_fe_sleep(struct dvb_frontend
*fe
)
119 * return true if we can satisfy the conditions, else return false.
121 static bool check_scan_ok(__u32 freq
, int bandwidth
,
122 struct pd_dvb_adapter
*adapter
)
127 if (adapter
->prev_freq
== freq
128 && adapter
->bandwidth
== bandwidth
) {
129 long nl
= jiffies
- adapter
->last_jiffies
;
132 msec
= jiffies_to_msecs(abs(nl
));
133 return msec
> 15000 ? true : false;
139 * Check if the firmware delays too long for an invalid frequency.
141 static int fw_delay_overflow(struct pd_dvb_adapter
*adapter
)
143 long nl
= jiffies
- adapter
->last_jiffies
;
146 msec
= jiffies_to_msecs(abs(nl
));
147 return msec
> 800 ? true : false;
150 static int poseidon_set_fe(struct dvb_frontend
*fe
)
152 struct dtv_frontend_properties
*fep
= &fe
->dtv_property_cache
;
153 s32 ret
= 0, cmd_status
= 0;
154 s32 i
, bandwidth
= -1;
155 struct poseidon
*pd
= fe
->demodulator_priv
;
156 struct pd_dvb_adapter
*pd_dvb
= &pd
->dvb_data
;
158 if (in_hibernation(pd
))
161 mutex_lock(&pd
->lock
);
162 for (i
= 0; i
< dvb_bandwidth_length
; i
++)
163 if (fep
->bandwidth_hz
== dvb_bandwidth
[i
][1])
164 bandwidth
= dvb_bandwidth
[i
][0];
166 if (check_scan_ok(fep
->frequency
, bandwidth
, pd_dvb
)) {
167 ret
= send_set_req(pd
, TUNE_FREQ_SELECT
,
168 fep
->frequency
/ 1000, &cmd_status
);
169 if (ret
| cmd_status
) {
174 ret
= send_set_req(pd
, DVBT_BANDW_SEL
,
175 bandwidth
, &cmd_status
);
176 if (ret
| cmd_status
) {
181 ret
= send_set_req(pd
, TAKE_REQUEST
, 0, &cmd_status
);
182 if (ret
| cmd_status
) {
187 /* save the context for future */
188 memcpy(&pd_dvb
->fe_param
, fep
, sizeof(*fep
));
189 pd_dvb
->bandwidth
= bandwidth
;
190 pd_dvb
->prev_freq
= fep
->frequency
;
191 pd_dvb
->last_jiffies
= jiffies
;
194 mutex_unlock(&pd
->lock
);
199 static int pm_dvb_suspend(struct poseidon
*pd
)
201 struct pd_dvb_adapter
*pd_dvb
= &pd
->dvb_data
;
202 dvb_stop_streaming(pd_dvb
);
203 dvb_urb_cleanup(pd_dvb
);
208 static int pm_dvb_resume(struct poseidon
*pd
)
210 struct pd_dvb_adapter
*pd_dvb
= &pd
->dvb_data
;
212 poseidon_check_mode_dvbt(pd
);
214 poseidon_set_fe(&pd_dvb
->dvb_fe
);
216 dvb_start_streaming(pd_dvb
);
221 static s32
poseidon_fe_init(struct dvb_frontend
*fe
)
223 struct poseidon
*pd
= fe
->demodulator_priv
;
224 struct pd_dvb_adapter
*pd_dvb
= &pd
->dvb_data
;
227 pd
->pm_suspend
= pm_dvb_suspend
;
228 pd
->pm_resume
= pm_dvb_resume
;
230 memset(&pd_dvb
->fe_param
, 0,
231 sizeof(struct dtv_frontend_properties
));
235 static int poseidon_get_fe(struct dvb_frontend
*fe
)
237 struct dtv_frontend_properties
*fep
= &fe
->dtv_property_cache
;
238 struct poseidon
*pd
= fe
->demodulator_priv
;
239 struct pd_dvb_adapter
*pd_dvb
= &pd
->dvb_data
;
241 memcpy(fep
, &pd_dvb
->fe_param
, sizeof(*fep
));
245 static int poseidon_fe_get_tune_settings(struct dvb_frontend
*fe
,
246 struct dvb_frontend_tune_settings
*tune
)
248 tune
->min_delay_ms
= 1000;
252 static int poseidon_read_status(struct dvb_frontend
*fe
, fe_status_t
*stat
)
254 struct poseidon
*pd
= fe
->demodulator_priv
;
255 s32 ret
= -1, cmd_status
;
256 struct tuner_dtv_sig_stat_s status
= {};
258 if (in_hibernation(pd
))
260 mutex_lock(&pd
->lock
);
262 ret
= send_get_req(pd
, TUNER_STATUS
, TLG_MODE_DVB_T
,
263 &status
, &cmd_status
, sizeof(status
));
264 if (ret
| cmd_status
) {
265 log("get tuner status error");
270 log("P : %d, L %d, LB :%d", status
.sig_present
,
271 status
.sig_locked
, status
.sig_lock_busy
);
273 if (status
.sig_lock_busy
) {
275 } else if (status
.sig_present
|| status
.sig_locked
) {
276 *stat
|= FE_HAS_LOCK
| FE_HAS_SIGNAL
| FE_HAS_CARRIER
277 | FE_HAS_SYNC
| FE_HAS_VITERBI
;
279 if (fw_delay_overflow(&pd
->dvb_data
))
280 *stat
|= FE_TIMEDOUT
;
283 mutex_unlock(&pd
->lock
);
287 static int poseidon_read_ber(struct dvb_frontend
*fe
, u32
*ber
)
289 struct poseidon
*pd
= fe
->demodulator_priv
;
290 struct tuner_ber_rate_s tlg_ber
= {};
291 s32 ret
= -1, cmd_status
;
293 mutex_lock(&pd
->lock
);
294 ret
= send_get_req(pd
, TUNER_BER_RATE
, 0,
295 &tlg_ber
, &cmd_status
, sizeof(tlg_ber
));
296 if (ret
| cmd_status
)
298 *ber
= tlg_ber
.ber_rate
;
300 mutex_unlock(&pd
->lock
);
304 static s32
poseidon_read_signal_strength(struct dvb_frontend
*fe
, u16
*strength
)
306 struct poseidon
*pd
= fe
->demodulator_priv
;
307 struct tuner_dtv_sig_stat_s status
= {};
308 s32 ret
= 0, cmd_status
;
310 mutex_lock(&pd
->lock
);
311 ret
= send_get_req(pd
, TUNER_STATUS
, TLG_MODE_DVB_T
,
312 &status
, &cmd_status
, sizeof(status
));
313 if (ret
| cmd_status
)
315 if ((status
.sig_present
|| status
.sig_locked
) && !status
.sig_strength
)
318 *strength
= status
.sig_strength
;
320 mutex_unlock(&pd
->lock
);
324 static int poseidon_read_snr(struct dvb_frontend
*fe
, u16
*snr
)
329 static int poseidon_read_unc_blocks(struct dvb_frontend
*fe
, u32
*unc
)
335 static struct dvb_frontend_ops poseidon_frontend_ops
= {
336 .delsys
= { SYS_DVBT
},
338 .name
= "Poseidon DVB-T",
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
|
349 FE_CAN_HIERARCHY_AUTO
,
352 .release
= poseidon_fe_release
,
354 .init
= poseidon_fe_init
,
355 .sleep
= poseidon_fe_sleep
,
357 .set_frontend
= poseidon_set_fe
,
358 .get_frontend
= poseidon_get_fe
,
359 .get_tune_settings
= poseidon_fe_get_tune_settings
,
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
,
367 .ts_bus_ctrl
= poseidon_ts_bus_ctrl
,
370 static void dvb_urb_irq(struct urb
*urb
)
372 struct pd_dvb_adapter
*pd_dvb
= urb
->context
;
373 int len
= urb
->transfer_buffer_length
;
374 struct dvb_demux
*demux
= &pd_dvb
->demux
;
377 if (!pd_dvb
->is_streaming
|| urb
->status
) {
378 if (urb
->status
== -EPROTO
)
383 if (urb
->actual_length
== len
)
384 dvb_dmx_swfilter(demux
, urb
->transfer_buffer
, len
);
385 else if (urb
->actual_length
== len
- 4) {
387 u8
*buf
= urb
->transfer_buffer
;
390 * The packet size is 512,
391 * last packet contains 456 bytes tsp data
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 */
399 len
-= offset
+ 52 + 4;
400 dvb_dmx_swfilter(demux
, buf
, len
);
408 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
410 log(" usb_submit_urb failed: error %d", ret
);
413 static int dvb_urb_init(struct pd_dvb_adapter
*pd_dvb
)
415 if (pd_dvb
->urb_array
[0])
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
);
425 static void dvb_urb_cleanup(struct pd_dvb_adapter
*pd_dvb
)
427 free_all_urb_generic(pd_dvb
->urb_array
, DVB_SBUF_NUM
);
430 static s32
dvb_start_streaming(struct pd_dvb_adapter
*pd_dvb
)
432 struct poseidon
*pd
= pd_dvb
->pd_device
;
435 if (pd
->state
& POSEIDON_STATE_DISCONNECT
)
438 mutex_lock(&pd
->lock
);
439 if (!pd_dvb
->is_streaming
) {
440 s32 i
, cmd_status
= 0;
442 * Once upon a time, there was a difficult bug lying here.
443 * ret = send_set_req(pd, TAKE_REQUEST, 0, &cmd_status);
446 ret
= send_set_req(pd
, PLAY_SERVICE
, 1, &cmd_status
);
447 if (ret
| cmd_status
)
450 ret
= dvb_urb_init(pd_dvb
);
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
],
459 log(" submit urb error %d", ret
);
465 mutex_unlock(&pd
->lock
);
469 void dvb_stop_streaming(struct pd_dvb_adapter
*pd_dvb
)
471 struct poseidon
*pd
= pd_dvb
->pd_device
;
473 mutex_lock(&pd
->lock
);
474 if (pd_dvb
->is_streaming
) {
475 s32 i
, ret
, cmd_status
= 0;
477 pd_dvb
->is_streaming
= 0;
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
]);
483 ret
= send_set_req(pd
, PLAY_SERVICE
, TLG_TUNE_PLAY_SVC_STOP
,
485 if (ret
| cmd_status
)
488 mutex_unlock(&pd
->lock
);
491 static int pd_start_feed(struct dvb_demux_feed
*feed
)
493 struct pd_dvb_adapter
*pd_dvb
= feed
->demux
->priv
;
498 if (atomic_inc_return(&pd_dvb
->active_feed
) == 1)
499 ret
= dvb_start_streaming(pd_dvb
);
503 static int pd_stop_feed(struct dvb_demux_feed
*feed
)
505 struct pd_dvb_adapter
*pd_dvb
= feed
->demux
->priv
;
509 if (atomic_dec_and_test(&pd_dvb
->active_feed
))
510 dvb_stop_streaming(pd_dvb
);
514 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr
);
515 int pd_dvb_usb_device_init(struct poseidon
*pd
)
517 struct pd_dvb_adapter
*pd_dvb
= &pd
->dvb_data
;
518 struct dvb_demux
*dvbdemux
;
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
;
526 ret
= dvb_register_adapter(&pd_dvb
->dvb_adap
,
527 "Poseidon dvbt adapter",
529 NULL
/* for hibernation correctly*/,
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
);
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
;
551 ret
= dvb_dmx_init(dvbdemux
);
555 pd_dvb
->dmxdev
.filternum
= pd_dvb
->demux
.filternum
;
556 pd_dvb
->dmxdev
.demux
= &pd_dvb
->demux
.dmx
;
557 pd_dvb
->dmxdev
.capabilities
= 0;
559 ret
= dvb_dmxdev_init(&pd_dvb
->dmxdev
, &pd_dvb
->dvb_adap
);
565 dvb_unregister_frontend(&pd_dvb
->dvb_fe
);
567 dvb_unregister_adapter(&pd_dvb
->dvb_adap
);
572 void pd_dvb_usb_device_exit(struct poseidon
*pd
)
574 struct pd_dvb_adapter
*pd_dvb
= &pd
->dvb_data
;
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
);
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
);
587 void pd_dvb_usb_device_cleanup(struct poseidon
*pd
)
589 struct pd_dvb_adapter
*pd_dvb
= &pd
->dvb_data
;
591 dvb_urb_cleanup(pd_dvb
);
594 int pd_dvb_get_adapter_num(struct pd_dvb_adapter
*pd_dvb
)
596 return pd_dvb
->dvb_adap
.num
;