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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <linux/kthread.h>
22 #include <linux/freezer.h>
24 #include "pvrusb2-hdw-internal.h"
25 #include "pvrusb2-hdw.h"
26 #include "pvrusb2-io.h"
27 #include "pvrusb2-dvb.h"
29 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr
);
31 #define BUFFER_COUNT 32
32 #define BUFFER_SIZE PAGE_ALIGN(0x4000)
35 struct pvr2_channel channel
;
36 struct pvr2_stream
*stream
;
37 struct pvr2_dvb_adapter
*adap
;
38 wait_queue_head_t wait_data
;
39 char *buffer_storage
[BUFFER_COUNT
];
42 static void pvr2_dvb_notify(struct pvr2_dvb_fh
*fhp
)
44 wake_up(&fhp
->wait_data
);
47 static int pvr2_dvb_fh_init(struct pvr2_dvb_fh
*fh
,
48 struct pvr2_dvb_adapter
*adap
)
50 struct pvr2_context
*pvr
= adap
->pvr
;
53 struct pvr2_buffer
*bp
;
55 init_waitqueue_head(&fh
->wait_data
);
59 pvr2_channel_init(&fh
->channel
, adap
->pvr
);
61 ret
= pvr2_channel_claim_stream(&fh
->channel
, &pvr
->video_stream
);
62 /* somebody else already has the stream */
66 fh
->stream
= pvr
->video_stream
.stream
;
68 for (idx
= 0; idx
< BUFFER_COUNT
; idx
++) {
69 fh
->buffer_storage
[idx
] = kmalloc(BUFFER_SIZE
, GFP_KERNEL
);
70 if (!(fh
->buffer_storage
[idx
]))
74 if (idx
< BUFFER_COUNT
) {
75 /* An allocation appears to have failed */
80 pvr2_stream_set_callback(pvr
->video_stream
.stream
,
81 (pvr2_stream_callback
) pvr2_dvb_notify
, fh
);
83 ret
= pvr2_stream_set_buffer_count(fh
->stream
, BUFFER_COUNT
);
87 for (idx
= 0; idx
< BUFFER_COUNT
; idx
++) {
88 bp
= pvr2_stream_get_buffer(fh
->stream
, idx
);
89 pvr2_buffer_set_buffer(bp
,
90 fh
->buffer_storage
[idx
],
94 ret
= pvr2_hdw_set_streaming(fh
->channel
.hdw
, 1);
98 while ((bp
= pvr2_stream_get_idle_buffer(fh
->stream
)) != 0) {
99 ret
= pvr2_buffer_queue(bp
);
108 pvr2_stream_kill(fh
->stream
);
110 for (idx
= 0; idx
< BUFFER_COUNT
; idx
++) {
111 if (!(fh
->buffer_storage
[idx
]))
114 kfree(fh
->buffer_storage
[idx
]);
116 pvr2_channel_done(&fh
->channel
);
121 static void pvr2_dvb_fh_done(struct pvr2_dvb_fh
*fh
)
125 pvr2_hdw_set_streaming(fh
->channel
.hdw
, 0);
127 pvr2_stream_kill(fh
->stream
);
129 // pvr2_channel_claim_stream(&fh->channel, NULL);
131 for (idx
= 0; idx
< BUFFER_COUNT
; idx
++) {
132 if (!(fh
->buffer_storage
[idx
]))
135 kfree(fh
->buffer_storage
[idx
]);
138 pvr2_channel_done(&fh
->channel
);
141 static int pvr2_dvb_feed_func(struct pvr2_dvb_adapter
*adap
)
143 struct pvr2_dvb_fh fh
;
146 struct pvr2_buffer
*bp
;
148 printk(KERN_DEBUG
"dvb thread started\n");
151 memset(&fh
, 0, sizeof(fh
));
153 ret
= pvr2_dvb_fh_init(&fh
, adap
);
158 if ((0 == adap
->feedcount
) || (kthread_should_stop()))
161 /* Not sure about this... */
164 bp
= pvr2_stream_get_ready_buffer(fh
.stream
);
166 count
= pvr2_buffer_get_count(bp
);
171 pvr2_buffer_get_id(bp
)],
174 ret
= pvr2_buffer_get_status(bp
);
178 ret
= pvr2_buffer_queue(bp
);
182 /* Since we know we did something to a buffer,
183 just go back and try again. No point in
184 blocking unless we really ran out of
185 buffers to process. */
190 /* Wait until more buffers become available. */
191 ret
= wait_event_interruptible(
193 pvr2_stream_get_ready_count(fh
.stream
) > 0);
198 pvr2_dvb_fh_done(&fh
);
200 /* If we get here and ret is < 0, then an error has occurred.
201 Probably would be a good idea to communicate that to DVB core... */
203 printk(KERN_DEBUG
"dvb thread stopped\n");
208 static int pvr2_dvb_feed_thread(void *data
)
210 int stat
= pvr2_dvb_feed_func(data
);
211 /* from videobuf-dvb.c: */
212 while (!kthread_should_stop()) {
213 set_current_state(TASK_INTERRUPTIBLE
);
219 static int pvr2_dvb_ctrl_feed(struct dvb_demux_feed
*dvbdmxfeed
, int onoff
)
221 struct pvr2_dvb_adapter
*adap
= dvbdmxfeed
->demux
->priv
;
222 int newfeedcount
, ret
= 0;
227 mutex_lock(&adap
->lock
);
228 newfeedcount
= adap
->feedcount
+ (onoff
? 1 : -1);
230 if (newfeedcount
== 0) {
231 printk(KERN_DEBUG
"stop feeding\n");
233 ret
= kthread_stop(adap
->thread
);
237 adap
->feedcount
= newfeedcount
;
239 if (adap
->feedcount
== onoff
&& adap
->feedcount
> 0) {
240 if (NULL
!= adap
->thread
)
243 printk(KERN_DEBUG
"start feeding\n");
245 adap
->thread
= kthread_run(pvr2_dvb_feed_thread
,
246 adap
, "pvrusb2-dvb");
247 if (IS_ERR(adap
->thread
)) {
248 ret
= PTR_ERR(adap
->thread
);
251 //ret = newfeedcount;
254 mutex_unlock(&adap
->lock
);
259 static int pvr2_dvb_start_feed(struct dvb_demux_feed
*dvbdmxfeed
)
261 printk(KERN_DEBUG
"start pid: 0x%04x, feedtype: %d\n",
262 dvbdmxfeed
->pid
, dvbdmxfeed
->type
);
263 return pvr2_dvb_ctrl_feed(dvbdmxfeed
, 1);
266 static int pvr2_dvb_stop_feed(struct dvb_demux_feed
*dvbdmxfeed
)
268 printk(KERN_DEBUG
"stop pid: 0x%04x, feedtype: %d\n",
269 dvbdmxfeed
->pid
, dvbdmxfeed
->type
);
270 return pvr2_dvb_ctrl_feed(dvbdmxfeed
, 0);
273 static int pvr2_dvb_bus_ctrl(struct dvb_frontend
*fe
, int acquire
)
275 /* TO DO: This function will call into the core and request for
276 * input to be set to 'dtv' if (acquire) and if it isn't set already.
278 * If (!acquire) then we should do nothing -- don't switch inputs
279 * again unless the analog side of the driver requests the bus.
284 static int pvr2_dvb_adapter_init(struct pvr2_dvb_adapter
*adap
)
288 ret
= dvb_register_adapter(&adap
->dvb_adap
, "pvrusb2-dvb",
289 THIS_MODULE
/*&hdw->usb_dev->owner*/,
290 &adap
->pvr
->hdw
->usb_dev
->dev
,
293 err("dvb_register_adapter failed: error %d", ret
);
296 adap
->dvb_adap
.priv
= adap
;
298 adap
->demux
.dmx
.capabilities
= DMX_TS_FILTERING
|
299 DMX_SECTION_FILTERING
|
300 DMX_MEMORY_BASED_FILTERING
;
301 adap
->demux
.priv
= adap
;
302 adap
->demux
.filternum
= 256;
303 adap
->demux
.feednum
= 256;
304 adap
->demux
.start_feed
= pvr2_dvb_start_feed
;
305 adap
->demux
.stop_feed
= pvr2_dvb_stop_feed
;
306 adap
->demux
.write_to_decoder
= NULL
;
308 ret
= dvb_dmx_init(&adap
->demux
);
310 err("dvb_dmx_init failed: error %d", ret
);
314 adap
->dmxdev
.filternum
= adap
->demux
.filternum
;
315 adap
->dmxdev
.demux
= &adap
->demux
.dmx
;
316 adap
->dmxdev
.capabilities
= 0;
318 ret
= dvb_dmxdev_init(&adap
->dmxdev
, &adap
->dvb_adap
);
320 err("dvb_dmxdev_init failed: error %d", ret
);
324 dvb_net_init(&adap
->dvb_adap
, &adap
->dvb_net
, &adap
->demux
.dmx
);
326 adap
->digital_up
= 1;
331 dvb_dmx_release(&adap
->demux
);
333 dvb_unregister_adapter(&adap
->dvb_adap
);
338 static int pvr2_dvb_adapter_exit(struct pvr2_dvb_adapter
*adap
)
340 if (adap
->digital_up
) {
341 printk(KERN_DEBUG
"unregistering DVB devices\n");
342 dvb_net_release(&adap
->dvb_net
);
343 adap
->demux
.dmx
.close(&adap
->demux
.dmx
);
344 dvb_dmxdev_release(&adap
->dmxdev
);
345 dvb_dmx_release(&adap
->demux
);
346 dvb_unregister_adapter(&adap
->dvb_adap
);
347 adap
->digital_up
= 0;
352 static int pvr2_dvb_frontend_init(struct pvr2_dvb_adapter
*adap
)
354 struct pvr2_hdw
*hdw
= adap
->pvr
->hdw
;
355 struct pvr2_dvb_props
*dvb_props
= hdw
->hdw_desc
->dvb_props
;
358 if (dvb_props
== NULL
) {
359 err("fe_props not defined!");
363 /* FIXME: This code should be moved into the core,
364 * and should only be called if we don't already have
365 * control of the bus.
367 * We can't call "pvr2_dvb_bus_ctrl(adap->fe, 1)" from here,
368 * because adap->fe isn't defined yet.
370 ret
= pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_by_id(hdw
,
372 PVR2_CVAL_INPUT_DTV
);
376 pvr2_hdw_commit_ctl(hdw
);
379 if (dvb_props
->frontend_attach
== NULL
) {
380 err("frontend_attach not defined!");
384 if ((dvb_props
->frontend_attach(adap
) == 0) && (adap
->fe
)) {
386 if (dvb_register_frontend(&adap
->dvb_adap
, adap
->fe
)) {
387 err("frontend registration failed!");
388 dvb_frontend_detach(adap
->fe
);
393 if (dvb_props
->tuner_attach
)
394 dvb_props
->tuner_attach(adap
);
396 if (adap
->fe
->ops
.analog_ops
.standby
)
397 adap
->fe
->ops
.analog_ops
.standby(adap
->fe
);
399 /* Ensure all frontends negotiate bus access */
400 adap
->fe
->ops
.ts_bus_ctrl
= pvr2_dvb_bus_ctrl
;
403 err("no frontend was attached!");
410 static int pvr2_dvb_frontend_exit(struct pvr2_dvb_adapter
*adap
)
412 if (adap
->fe
!= NULL
) {
413 dvb_unregister_frontend(adap
->fe
);
414 dvb_frontend_detach(adap
->fe
);
419 int pvr2_dvb_init(struct pvr2_context
*pvr
)
423 pvr
->hdw
->dvb
.pvr
= pvr
;
424 mutex_init(&pvr
->hdw
->dvb
.lock
);
426 ret
= pvr2_dvb_adapter_init(&pvr
->hdw
->dvb
);
430 ret
= pvr2_dvb_frontend_init(&pvr
->hdw
->dvb
);
435 int pvr2_dvb_exit(struct pvr2_context
*pvr
)
437 pvr2_dvb_frontend_exit(&pvr
->hdw
->dvb
);
438 pvr2_dvb_adapter_exit(&pvr
->hdw
->dvb
);
440 pvr
->hdw
->dvb
.pvr
= NULL
;