2 * pvrusb2-dvb.c - linux-dvb api interface to the pvrusb2 driver.
4 * Copyright (C) 2007, 2008 Michael Krufky <mkrufky@linuxtv.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
17 #include <linux/kthread.h>
18 #include <linux/freezer.h>
19 #include <linux/slab.h>
21 #include <media/dvbdev.h>
22 #include "pvrusb2-debug.h"
23 #include "pvrusb2-hdw-internal.h"
24 #include "pvrusb2-hdw.h"
25 #include "pvrusb2-io.h"
26 #include "pvrusb2-dvb.h"
28 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr
);
30 static int pvr2_dvb_feed_func(struct pvr2_dvb_adapter
*adap
)
34 struct pvr2_buffer
*bp
;
35 struct pvr2_stream
*stream
;
37 pvr2_trace(PVR2_TRACE_DVB_FEED
, "dvb feed thread started");
40 stream
= adap
->channel
.stream
->stream
;
43 if (kthread_should_stop()) break;
45 /* Not sure about this... */
48 bp
= pvr2_stream_get_ready_buffer(stream
);
50 count
= pvr2_buffer_get_count(bp
);
55 pvr2_buffer_get_id(bp
)],
58 ret
= pvr2_buffer_get_status(bp
);
61 ret
= pvr2_buffer_queue(bp
);
64 /* Since we know we did something to a buffer,
65 just go back and try again. No point in
66 blocking unless we really ran out of
67 buffers to process. */
72 /* Wait until more buffers become available or we're
73 told not to wait any longer. */
74 ret
= wait_event_interruptible(
75 adap
->buffer_wait_data
,
76 (pvr2_stream_get_ready_count(stream
) > 0) ||
77 kthread_should_stop());
81 /* If we get here and ret is < 0, then an error has occurred.
82 Probably would be a good idea to communicate that to DVB core... */
84 pvr2_trace(PVR2_TRACE_DVB_FEED
, "dvb feed thread stopped");
89 static int pvr2_dvb_feed_thread(void *data
)
91 int stat
= pvr2_dvb_feed_func(data
);
92 /* from videobuf-dvb.c: */
93 while (!kthread_should_stop()) {
94 set_current_state(TASK_INTERRUPTIBLE
);
100 static void pvr2_dvb_notify(struct pvr2_dvb_adapter
*adap
)
102 wake_up(&adap
->buffer_wait_data
);
105 static void pvr2_dvb_stream_end(struct pvr2_dvb_adapter
*adap
)
108 struct pvr2_stream
*stream
;
111 kthread_stop(adap
->thread
);
115 if (adap
->channel
.stream
) {
116 stream
= adap
->channel
.stream
->stream
;
121 pvr2_hdw_set_streaming(adap
->channel
.hdw
, 0);
122 pvr2_stream_set_callback(stream
, NULL
, NULL
);
123 pvr2_stream_kill(stream
);
124 pvr2_stream_set_buffer_count(stream
, 0);
125 pvr2_channel_claim_stream(&adap
->channel
, NULL
);
128 if (adap
->stream_run
) {
129 for (idx
= 0; idx
< PVR2_DVB_BUFFER_COUNT
; idx
++) {
130 if (!(adap
->buffer_storage
[idx
])) continue;
131 kfree(adap
->buffer_storage
[idx
]);
132 adap
->buffer_storage
[idx
] = NULL
;
134 adap
->stream_run
= 0;
138 static int pvr2_dvb_stream_do_start(struct pvr2_dvb_adapter
*adap
)
140 struct pvr2_context
*pvr
= adap
->channel
.mc_head
;
143 struct pvr2_buffer
*bp
;
144 struct pvr2_stream
*stream
= NULL
;
146 if (adap
->stream_run
) return -EIO
;
148 ret
= pvr2_channel_claim_stream(&adap
->channel
, &pvr
->video_stream
);
149 /* somebody else already has the stream */
150 if (ret
< 0) return ret
;
152 stream
= adap
->channel
.stream
->stream
;
154 for (idx
= 0; idx
< PVR2_DVB_BUFFER_COUNT
; idx
++) {
155 adap
->buffer_storage
[idx
] = kmalloc(PVR2_DVB_BUFFER_SIZE
,
157 if (!(adap
->buffer_storage
[idx
])) return -ENOMEM
;
160 pvr2_stream_set_callback(pvr
->video_stream
.stream
,
161 (pvr2_stream_callback
) pvr2_dvb_notify
, adap
);
163 ret
= pvr2_stream_set_buffer_count(stream
, PVR2_DVB_BUFFER_COUNT
);
164 if (ret
< 0) return ret
;
166 for (idx
= 0; idx
< PVR2_DVB_BUFFER_COUNT
; idx
++) {
167 bp
= pvr2_stream_get_buffer(stream
, idx
);
168 pvr2_buffer_set_buffer(bp
,
169 adap
->buffer_storage
[idx
],
170 PVR2_DVB_BUFFER_SIZE
);
173 ret
= pvr2_hdw_set_streaming(adap
->channel
.hdw
, 1);
174 if (ret
< 0) return ret
;
176 while ((bp
= pvr2_stream_get_idle_buffer(stream
)) != NULL
) {
177 ret
= pvr2_buffer_queue(bp
);
178 if (ret
< 0) return ret
;
181 adap
->thread
= kthread_run(pvr2_dvb_feed_thread
, adap
, "pvrusb2-dvb");
183 if (IS_ERR(adap
->thread
)) {
184 ret
= PTR_ERR(adap
->thread
);
189 adap
->stream_run
= !0;
194 static int pvr2_dvb_stream_start(struct pvr2_dvb_adapter
*adap
)
196 int ret
= pvr2_dvb_stream_do_start(adap
);
197 if (ret
< 0) pvr2_dvb_stream_end(adap
);
201 static int pvr2_dvb_ctrl_feed(struct dvb_demux_feed
*dvbdmxfeed
, int onoff
)
203 struct pvr2_dvb_adapter
*adap
= dvbdmxfeed
->demux
->priv
;
206 if (adap
== NULL
) return -ENODEV
;
208 mutex_lock(&adap
->lock
);
211 if (!adap
->feedcount
) {
212 pvr2_trace(PVR2_TRACE_DVB_FEED
,
213 "start feeding demux");
214 ret
= pvr2_dvb_stream_start(adap
);
218 } else if (adap
->feedcount
> 0) {
220 if (!adap
->feedcount
) {
221 pvr2_trace(PVR2_TRACE_DVB_FEED
,
222 "stop feeding demux");
223 pvr2_dvb_stream_end(adap
);
227 mutex_unlock(&adap
->lock
);
232 static int pvr2_dvb_start_feed(struct dvb_demux_feed
*dvbdmxfeed
)
234 pvr2_trace(PVR2_TRACE_DVB_FEED
, "start pid: 0x%04x", dvbdmxfeed
->pid
);
235 return pvr2_dvb_ctrl_feed(dvbdmxfeed
, 1);
238 static int pvr2_dvb_stop_feed(struct dvb_demux_feed
*dvbdmxfeed
)
240 pvr2_trace(PVR2_TRACE_DVB_FEED
, "stop pid: 0x%04x", dvbdmxfeed
->pid
);
241 return pvr2_dvb_ctrl_feed(dvbdmxfeed
, 0);
244 static int pvr2_dvb_bus_ctrl(struct dvb_frontend
*fe
, int acquire
)
246 struct pvr2_dvb_adapter
*adap
= fe
->dvb
->priv
;
247 return pvr2_channel_limit_inputs(
249 (acquire
? (1 << PVR2_CVAL_INPUT_DTV
) : 0));
252 static int pvr2_dvb_adapter_init(struct pvr2_dvb_adapter
*adap
)
256 ret
= dvb_register_adapter(&adap
->dvb_adap
, "pvrusb2-dvb",
257 THIS_MODULE
/*&hdw->usb_dev->owner*/,
258 &adap
->channel
.hdw
->usb_dev
->dev
,
261 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
262 "dvb_register_adapter failed: error %d", ret
);
265 adap
->dvb_adap
.priv
= adap
;
267 adap
->demux
.dmx
.capabilities
= DMX_TS_FILTERING
|
268 DMX_SECTION_FILTERING
|
269 DMX_MEMORY_BASED_FILTERING
;
270 adap
->demux
.priv
= adap
;
271 adap
->demux
.filternum
= 256;
272 adap
->demux
.feednum
= 256;
273 adap
->demux
.start_feed
= pvr2_dvb_start_feed
;
274 adap
->demux
.stop_feed
= pvr2_dvb_stop_feed
;
275 adap
->demux
.write_to_decoder
= NULL
;
277 ret
= dvb_dmx_init(&adap
->demux
);
279 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
280 "dvb_dmx_init failed: error %d", ret
);
284 adap
->dmxdev
.filternum
= adap
->demux
.filternum
;
285 adap
->dmxdev
.demux
= &adap
->demux
.dmx
;
286 adap
->dmxdev
.capabilities
= 0;
288 ret
= dvb_dmxdev_init(&adap
->dmxdev
, &adap
->dvb_adap
);
290 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
291 "dvb_dmxdev_init failed: error %d", ret
);
295 dvb_net_init(&adap
->dvb_adap
, &adap
->dvb_net
, &adap
->demux
.dmx
);
300 dvb_dmx_release(&adap
->demux
);
302 dvb_unregister_adapter(&adap
->dvb_adap
);
307 static int pvr2_dvb_adapter_exit(struct pvr2_dvb_adapter
*adap
)
309 pvr2_trace(PVR2_TRACE_INFO
, "unregistering DVB devices");
310 dvb_net_release(&adap
->dvb_net
);
311 adap
->demux
.dmx
.close(&adap
->demux
.dmx
);
312 dvb_dmxdev_release(&adap
->dmxdev
);
313 dvb_dmx_release(&adap
->demux
);
314 dvb_unregister_adapter(&adap
->dvb_adap
);
318 static int pvr2_dvb_frontend_init(struct pvr2_dvb_adapter
*adap
)
320 struct pvr2_hdw
*hdw
= adap
->channel
.hdw
;
321 const struct pvr2_dvb_props
*dvb_props
= hdw
->hdw_desc
->dvb_props
;
324 if (dvb_props
== NULL
) {
325 pvr2_trace(PVR2_TRACE_ERROR_LEGS
, "fe_props not defined!");
329 ret
= pvr2_channel_limit_inputs(
331 (1 << PVR2_CVAL_INPUT_DTV
));
333 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
334 "failed to grab control of dtv input (code=%d)",
339 if (dvb_props
->frontend_attach
== NULL
) {
340 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
341 "frontend_attach not defined!");
346 if ((dvb_props
->frontend_attach(adap
) == 0) && (adap
->fe
)) {
348 if (dvb_register_frontend(&adap
->dvb_adap
, adap
->fe
)) {
349 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
350 "frontend registration failed!");
351 dvb_frontend_detach(adap
->fe
);
357 if (dvb_props
->tuner_attach
)
358 dvb_props
->tuner_attach(adap
);
360 if (adap
->fe
->ops
.analog_ops
.standby
)
361 adap
->fe
->ops
.analog_ops
.standby(adap
->fe
);
363 /* Ensure all frontends negotiate bus access */
364 adap
->fe
->ops
.ts_bus_ctrl
= pvr2_dvb_bus_ctrl
;
367 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
368 "no frontend was attached!");
374 pvr2_channel_limit_inputs(&adap
->channel
, 0);
378 static int pvr2_dvb_frontend_exit(struct pvr2_dvb_adapter
*adap
)
380 if (adap
->fe
!= NULL
) {
381 dvb_unregister_frontend(adap
->fe
);
382 dvb_frontend_detach(adap
->fe
);
387 static void pvr2_dvb_destroy(struct pvr2_dvb_adapter
*adap
)
389 pvr2_dvb_stream_end(adap
);
390 pvr2_dvb_frontend_exit(adap
);
391 pvr2_dvb_adapter_exit(adap
);
392 pvr2_channel_done(&adap
->channel
);
396 static void pvr2_dvb_internal_check(struct pvr2_channel
*chp
)
398 struct pvr2_dvb_adapter
*adap
;
399 adap
= container_of(chp
, struct pvr2_dvb_adapter
, channel
);
400 if (!adap
->channel
.mc_head
->disconnect_flag
) return;
401 pvr2_dvb_destroy(adap
);
404 struct pvr2_dvb_adapter
*pvr2_dvb_create(struct pvr2_context
*pvr
)
407 struct pvr2_dvb_adapter
*adap
;
408 if (!pvr
->hdw
->hdw_desc
->dvb_props
) {
409 /* Device lacks a digital interface so don't set up
410 the DVB side of the driver either. For now. */
413 adap
= kzalloc(sizeof(*adap
), GFP_KERNEL
);
414 if (!adap
) return adap
;
415 pvr2_channel_init(&adap
->channel
, pvr
);
416 adap
->channel
.check_func
= pvr2_dvb_internal_check
;
417 init_waitqueue_head(&adap
->buffer_wait_data
);
418 mutex_init(&adap
->lock
);
419 ret
= pvr2_dvb_adapter_init(adap
);
420 if (ret
< 0) goto fail1
;
421 ret
= pvr2_dvb_frontend_init(adap
);
422 if (ret
< 0) goto fail2
;
426 pvr2_dvb_adapter_exit(adap
);
428 pvr2_channel_done(&adap
->channel
);