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_thread(void *data)
143 struct pvr2_dvb_adapter *adap = data;
144 struct pvr2_dvb_fh fh;
147 struct pvr2_buffer *bp;
149 printk(KERN_DEBUG "dvb thread started\n");
152 memset(&fh, 0, sizeof(fh));
154 ret = pvr2_dvb_fh_init(&fh, adap);
159 if ((0 == adap->feedcount) || (kthread_should_stop()))
162 /* Not sure about this... */
165 bp = pvr2_stream_get_ready_buffer(fh.stream);
167 count = pvr2_buffer_get_count(bp);
172 pvr2_buffer_get_id(bp)],
175 ret = pvr2_buffer_get_status(bp);
179 ret = pvr2_buffer_queue(bp);
183 /* Since we know we did something to a buffer,
184 just go back and try again. No point in
185 blocking unless we really ran out of
186 buffers to process. */
191 /* Wait until more buffers become available. */
192 ret = wait_event_interruptible(
194 pvr2_stream_get_ready_count(fh.stream) > 0);
199 pvr2_dvb_fh_done(&fh);
201 /* If we get here and ret is < 0, then an error has occurred.
202 Probably would be a good idea to communicate that to DVB core... */
204 printk(KERN_DEBUG "dvb thread stopped\n");
206 /* from videobuf-dvb.c: */
207 while (!kthread_should_stop()) {
208 set_current_state(TASK_INTERRUPTIBLE);
214 static int pvr2_dvb_ctrl_feed(struct dvb_demux_feed *dvbdmxfeed, int onoff)
216 struct pvr2_dvb_adapter *adap = dvbdmxfeed->demux->priv;
217 int newfeedcount, ret = 0;
222 mutex_lock(&adap->lock);
223 newfeedcount = adap->feedcount + (onoff ? 1 : -1);
225 if (newfeedcount == 0) {
226 printk(KERN_DEBUG "stop feeding\n");
228 ret = kthread_stop(adap->thread);
232 adap->feedcount = newfeedcount;
234 if (adap->feedcount == onoff && adap->feedcount > 0) {
235 if (NULL != adap->thread)
238 printk(KERN_DEBUG "start feeding\n");
240 adap->thread = kthread_run(pvr2_dvb_feed_thread,
241 adap, "pvrusb2-dvb");
242 if (IS_ERR(adap->thread)) {
243 ret = PTR_ERR(adap->thread);
246 //ret = newfeedcount;
249 mutex_unlock(&adap->lock);
254 static int pvr2_dvb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
256 printk(KERN_DEBUG "start pid: 0x%04x, feedtype: %d\n",
257 dvbdmxfeed->pid, dvbdmxfeed->type);
258 return pvr2_dvb_ctrl_feed(dvbdmxfeed, 1);
261 static int pvr2_dvb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
263 printk(KERN_DEBUG "stop pid: 0x%04x, feedtype: %d\n",
264 dvbdmxfeed->pid, dvbdmxfeed->type);
265 return pvr2_dvb_ctrl_feed(dvbdmxfeed, 0);
268 static int pvr2_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
270 /* TO DO: This function will call into the core and request for
271 * input to be set to 'dtv' if (acquire) and if it isn't set already.
273 * If (!acquire) then we should do nothing -- don't switch inputs
274 * again unless the analog side of the driver requests the bus.
279 static int pvr2_dvb_adapter_init(struct pvr2_dvb_adapter *adap)
283 ret = dvb_register_adapter(&adap->dvb_adap, "pvrusb2-dvb",
284 THIS_MODULE/*&hdw->usb_dev->owner*/,
285 &adap->pvr->hdw->usb_dev->dev,
288 err("dvb_register_adapter failed: error %d", ret);
291 adap->dvb_adap.priv = adap;
293 adap->demux.dmx.capabilities = DMX_TS_FILTERING |
294 DMX_SECTION_FILTERING |
295 DMX_MEMORY_BASED_FILTERING;
296 adap->demux.priv = adap;
297 adap->demux.filternum = 256;
298 adap->demux.feednum = 256;
299 adap->demux.start_feed = pvr2_dvb_start_feed;
300 adap->demux.stop_feed = pvr2_dvb_stop_feed;
301 adap->demux.write_to_decoder = NULL;
303 ret = dvb_dmx_init(&adap->demux);
305 err("dvb_dmx_init failed: error %d", ret);
309 adap->dmxdev.filternum = adap->demux.filternum;
310 adap->dmxdev.demux = &adap->demux.dmx;
311 adap->dmxdev.capabilities = 0;
313 ret = dvb_dmxdev_init(&adap->dmxdev, &adap->dvb_adap);
315 err("dvb_dmxdev_init failed: error %d", ret);
319 dvb_net_init(&adap->dvb_adap, &adap->dvb_net, &adap->demux.dmx);
321 adap->digital_up = 1;
326 dvb_dmx_release(&adap->demux);
328 dvb_unregister_adapter(&adap->dvb_adap);
333 static int pvr2_dvb_adapter_exit(struct pvr2_dvb_adapter *adap)
335 if (adap->digital_up) {
336 printk(KERN_DEBUG "unregistering DVB devices\n");
337 dvb_net_release(&adap->dvb_net);
338 adap->demux.dmx.close(&adap->demux.dmx);
339 dvb_dmxdev_release(&adap->dmxdev);
340 dvb_dmx_release(&adap->demux);
341 dvb_unregister_adapter(&adap->dvb_adap);
342 adap->digital_up = 0;
347 static int pvr2_dvb_frontend_init(struct pvr2_dvb_adapter *adap)
349 struct pvr2_dvb_props *dvb_props = adap->pvr->hdw->hdw_desc->dvb_props;
351 if (dvb_props == NULL) {
352 err("fe_props not defined!");
356 if (dvb_props->frontend_attach == NULL) {
357 err("frontend_attach not defined!");
361 if ((dvb_props->frontend_attach(adap) == 0) && (adap->fe)) {
363 if (dvb_register_frontend(&adap->dvb_adap, adap->fe)) {
364 err("frontend registration failed!");
365 dvb_frontend_detach(adap->fe);
370 if (dvb_props->tuner_attach)
371 dvb_props->tuner_attach(adap);
373 if (adap->fe->ops.analog_ops.standby)
374 adap->fe->ops.analog_ops.standby(adap->fe);
376 /* Ensure all frontends negotiate bus access */
377 adap->fe->ops.ts_bus_ctrl = pvr2_dvb_bus_ctrl;
380 err("no frontend was attached!");
387 static int pvr2_dvb_frontend_exit(struct pvr2_dvb_adapter *adap)
389 if (adap->fe != NULL) {
390 dvb_unregister_frontend(adap->fe);
391 dvb_frontend_detach(adap->fe);
396 int pvr2_dvb_init(struct pvr2_context *pvr)
400 pvr->hdw->dvb.pvr = pvr;
401 mutex_init(&pvr->hdw->dvb.lock);
403 ret = pvr2_dvb_adapter_init(&pvr->hdw->dvb);
407 ret = pvr2_dvb_frontend_init(&pvr->hdw->dvb);
412 int pvr2_dvb_exit(struct pvr2_context *pvr)
414 pvr2_dvb_frontend_exit(&pvr->hdw->dvb);
415 pvr2_dvb_adapter_exit(&pvr->hdw->dvb);
417 pvr->hdw->dvb.pvr = NULL;