blob: 2e64f98d124149404ba76d6f5ab3131e14910662 [file] [log] [blame]
Michael Krufky04910bd2008-02-03 23:46:16 -03001/*
2 * pvrusb2-dvb.c - linux-dvb api interface to the pvrusb2 driver.
3 *
4 * Copyright (C) 2007, 2008 Michael Krufky <mkrufky@linuxtv.org>
5 *
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
9 *
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.
14 *
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
18 *
19 */
20
Michael Krufkyd8abe972008-02-03 23:55:07 -030021#include <linux/kthread.h>
Mike Iselybb8ce9d2008-02-04 00:00:19 -030022#include <linux/freezer.h>
Michael Krufky04910bd2008-02-03 23:46:16 -030023#include "dvbdev.h"
24#include "pvrusb2-hdw-internal.h"
25#include "pvrusb2-hdw.h"
Mike Iselybb8ce9d2008-02-04 00:00:19 -030026#include "pvrusb2-io.h"
Michael Krufky04910bd2008-02-03 23:46:16 -030027#include "pvrusb2-dvb.h"
28
29DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
30
Mike Iselyceb43402008-02-06 04:24:51 -030031static int pvr2_dvb_feed_func(struct pvr2_dvb_adapter *adap)
Mike Iselybb8ce9d2008-02-04 00:00:19 -030032{
Mike Iselybb8ce9d2008-02-04 00:00:19 -030033 int ret;
34 unsigned int count;
35 struct pvr2_buffer *bp;
Mike Iselya36416d2008-02-06 06:59:48 -030036 struct pvr2_stream *stream;
Mike Iselybb8ce9d2008-02-04 00:00:19 -030037
38 printk(KERN_DEBUG "dvb thread started\n");
39 set_freezable();
40
Mike Iselya36416d2008-02-06 06:59:48 -030041 stream = adap->channel.stream->stream;
Mike Iselybb8ce9d2008-02-04 00:00:19 -030042
43 for (;;) {
Mike Iselya36416d2008-02-06 06:59:48 -030044 if (kthread_should_stop()) break;
Mike Iselybb8ce9d2008-02-04 00:00:19 -030045
46 /* Not sure about this... */
47 try_to_freeze();
48
Mike Iselya36416d2008-02-06 06:59:48 -030049 bp = pvr2_stream_get_ready_buffer(stream);
Mike Iselybb8ce9d2008-02-04 00:00:19 -030050 if (bp != NULL) {
51 count = pvr2_buffer_get_count(bp);
52 if (count) {
53 dvb_dmx_swfilter(
54 &adap->demux,
Mike Iselya36416d2008-02-06 06:59:48 -030055 adap->buffer_storage[
56 pvr2_buffer_get_id(bp)],
Mike Iselybb8ce9d2008-02-04 00:00:19 -030057 count);
58 } else {
59 ret = pvr2_buffer_get_status(bp);
Mike Iselya36416d2008-02-06 06:59:48 -030060 if (ret < 0) break;
Mike Iselybb8ce9d2008-02-04 00:00:19 -030061 }
62 ret = pvr2_buffer_queue(bp);
Mike Iselya36416d2008-02-06 06:59:48 -030063 if (ret < 0) break;
Mike Iselybb8ce9d2008-02-04 00:00:19 -030064
65 /* Since we know we did something to a buffer,
66 just go back and try again. No point in
67 blocking unless we really ran out of
68 buffers to process. */
69 continue;
70 }
71
72
Mike Isely07b80262008-03-30 20:36:31 -030073 /* Wait until more buffers become available or we're
74 told not to wait any longer. */
Mike Iselybb8ce9d2008-02-04 00:00:19 -030075 ret = wait_event_interruptible(
Mike Iselya36416d2008-02-06 06:59:48 -030076 adap->buffer_wait_data,
Mike Isely07b80262008-03-30 20:36:31 -030077 (pvr2_stream_get_ready_count(stream) > 0) ||
Mike Isely13e027a2008-04-07 02:57:13 -030078 kthread_should_stop());
Mike Iselya36416d2008-02-06 06:59:48 -030079 if (ret < 0) break;
Mike Iselybb8ce9d2008-02-04 00:00:19 -030080 }
81
Mike Iselybb8ce9d2008-02-04 00:00:19 -030082 /* If we get here and ret is < 0, then an error has occurred.
83 Probably would be a good idea to communicate that to DVB core... */
84
85 printk(KERN_DEBUG "dvb thread stopped\n");
86
Mike Iselyceb43402008-02-06 04:24:51 -030087 return 0;
88}
89
90static int pvr2_dvb_feed_thread(void *data)
91{
92 int stat = pvr2_dvb_feed_func(data);
Mike Iselybb8ce9d2008-02-04 00:00:19 -030093 /* from videobuf-dvb.c: */
94 while (!kthread_should_stop()) {
95 set_current_state(TASK_INTERRUPTIBLE);
96 schedule();
97 }
Mike Iselyceb43402008-02-06 04:24:51 -030098 return stat;
Mike Iselybb8ce9d2008-02-04 00:00:19 -030099}
100
Mike Iselya36416d2008-02-06 06:59:48 -0300101static void pvr2_dvb_notify(struct pvr2_dvb_adapter *adap)
Michael Krufkyd8abe972008-02-03 23:55:07 -0300102{
Mike Iselya36416d2008-02-06 06:59:48 -0300103 wake_up(&adap->buffer_wait_data);
104}
Michael Krufkyd8abe972008-02-03 23:55:07 -0300105
Mike Iselya36416d2008-02-06 06:59:48 -0300106static void pvr2_dvb_stream_end(struct pvr2_dvb_adapter *adap)
107{
108 unsigned int idx;
109 struct pvr2_stream *stream;
Michael Krufkyd8abe972008-02-03 23:55:07 -0300110
Mike Iselya36416d2008-02-06 06:59:48 -0300111 if (adap->thread) {
112 kthread_stop(adap->thread);
Michael Krufkyd8abe972008-02-03 23:55:07 -0300113 adap->thread = NULL;
114 }
115
Mike Iselya36416d2008-02-06 06:59:48 -0300116 if (adap->channel.stream) {
117 stream = adap->channel.stream->stream;
118 } else {
119 stream = NULL;
Michael Krufkyd8abe972008-02-03 23:55:07 -0300120 }
Mike Iselya36416d2008-02-06 06:59:48 -0300121 if (stream) {
122 pvr2_hdw_set_streaming(adap->channel.hdw, 0);
123 pvr2_stream_set_callback(stream, NULL, NULL);
124 pvr2_stream_kill(stream);
125 pvr2_stream_set_buffer_count(stream, 0);
126 pvr2_channel_claim_stream(&adap->channel, NULL);
127 }
128
129 if (adap->stream_run) {
130 for (idx = 0; idx < PVR2_DVB_BUFFER_COUNT; idx++) {
131 if (!(adap->buffer_storage[idx])) continue;
132 kfree(adap->buffer_storage[idx]);
133 adap->buffer_storage[idx] = 0;
134 }
135 adap->stream_run = 0;
136 }
137}
138
139static int pvr2_dvb_stream_do_start(struct pvr2_dvb_adapter *adap)
140{
141 struct pvr2_context *pvr = adap->channel.mc_head;
142 unsigned int idx;
143 int ret;
144 struct pvr2_buffer *bp;
145 struct pvr2_stream *stream = 0;
146
147 if (adap->stream_run) return -EIO;
148
149 ret = pvr2_channel_claim_stream(&adap->channel, &pvr->video_stream);
150 /* somebody else already has the stream */
151 if (ret < 0) return ret;
152
153 stream = adap->channel.stream->stream;
154
155 for (idx = 0; idx < PVR2_DVB_BUFFER_COUNT; idx++) {
156 adap->buffer_storage[idx] = kmalloc(PVR2_DVB_BUFFER_SIZE,
157 GFP_KERNEL);
158 if (!(adap->buffer_storage[idx])) return -ENOMEM;
159 }
160
161 pvr2_stream_set_callback(pvr->video_stream.stream,
162 (pvr2_stream_callback) pvr2_dvb_notify, adap);
163
164 ret = pvr2_stream_set_buffer_count(stream, PVR2_DVB_BUFFER_COUNT);
165 if (ret < 0) return ret;
166
167 for (idx = 0; idx < PVR2_DVB_BUFFER_COUNT; idx++) {
168 bp = pvr2_stream_get_buffer(stream, idx);
169 pvr2_buffer_set_buffer(bp,
170 adap->buffer_storage[idx],
171 PVR2_DVB_BUFFER_SIZE);
172 }
173
174 ret = pvr2_hdw_set_streaming(adap->channel.hdw, 1);
175 if (ret < 0) return ret;
176
177 while ((bp = pvr2_stream_get_idle_buffer(stream)) != 0) {
178 ret = pvr2_buffer_queue(bp);
179 if (ret < 0) return ret;
180 }
181
182 adap->thread = kthread_run(pvr2_dvb_feed_thread, adap, "pvrusb2-dvb");
183
184 if (IS_ERR(adap->thread)) {
185 ret = PTR_ERR(adap->thread);
186 adap->thread = NULL;
187 return ret;
188 }
189
190 adap->stream_run = !0;
191
192 return 0;
193}
194
195static int pvr2_dvb_stream_start(struct pvr2_dvb_adapter *adap)
196{
197 int ret = pvr2_dvb_stream_do_start(adap);
198 if (ret < 0) pvr2_dvb_stream_end(adap);
199 return ret;
200}
201
202static int pvr2_dvb_ctrl_feed(struct dvb_demux_feed *dvbdmxfeed, int onoff)
203{
204 struct pvr2_dvb_adapter *adap = dvbdmxfeed->demux->priv;
205 int ret = 0;
206
207 if (adap == NULL) return -ENODEV;
208
209 mutex_lock(&adap->lock);
210 do {
211 if (onoff) {
212 if (!adap->feedcount) {
213 printk(KERN_DEBUG "start feeding\n");
214 ret = pvr2_dvb_stream_start(adap);
215 if (ret < 0) break;
216 }
217 (adap->feedcount)++;
218 } else if (adap->feedcount > 0) {
219 (adap->feedcount)--;
220 if (!adap->feedcount) {
221 printk(KERN_DEBUG "stop feeding\n");
222 pvr2_dvb_stream_end(adap);
223 }
224 }
225 } while (0);
Michael Krufkyd8abe972008-02-03 23:55:07 -0300226 mutex_unlock(&adap->lock);
227
228 return ret;
229}
230
Michael Krufky04910bd2008-02-03 23:46:16 -0300231static int pvr2_dvb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
232{
233 printk(KERN_DEBUG "start pid: 0x%04x, feedtype: %d\n",
234 dvbdmxfeed->pid, dvbdmxfeed->type);
Michael Krufkyd8abe972008-02-03 23:55:07 -0300235 return pvr2_dvb_ctrl_feed(dvbdmxfeed, 1);
Michael Krufky04910bd2008-02-03 23:46:16 -0300236}
237
238static int pvr2_dvb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
239{
240 printk(KERN_DEBUG "stop pid: 0x%04x, feedtype: %d\n",
241 dvbdmxfeed->pid, dvbdmxfeed->type);
Michael Krufkyd8abe972008-02-03 23:55:07 -0300242 return pvr2_dvb_ctrl_feed(dvbdmxfeed, 0);
Michael Krufky04910bd2008-02-03 23:46:16 -0300243}
244
Michael Krufky99443ae2008-02-03 23:48:09 -0300245static int pvr2_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
246{
Mike Isely1cb03b72008-04-21 03:47:43 -0300247 struct pvr2_dvb_adapter *adap = fe->dvb->priv;
248 return pvr2_channel_limit_inputs(
249 &adap->channel,
250 (acquire ? (1 << PVR2_CVAL_INPUT_DTV) : 0));
Michael Krufky99443ae2008-02-03 23:48:09 -0300251}
252
Michael Krufky04910bd2008-02-03 23:46:16 -0300253static int pvr2_dvb_adapter_init(struct pvr2_dvb_adapter *adap)
254{
255 int ret;
256
257 ret = dvb_register_adapter(&adap->dvb_adap, "pvrusb2-dvb",
258 THIS_MODULE/*&hdw->usb_dev->owner*/,
Mike Iselya36416d2008-02-06 06:59:48 -0300259 &adap->channel.hdw->usb_dev->dev,
Michael Krufky04910bd2008-02-03 23:46:16 -0300260 adapter_nr);
261 if (ret < 0) {
262 err("dvb_register_adapter failed: error %d", ret);
263 goto err;
264 }
265 adap->dvb_adap.priv = adap;
266
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;
276
277 ret = dvb_dmx_init(&adap->demux);
278 if (ret < 0) {
279 err("dvb_dmx_init failed: error %d", ret);
280 goto err_dmx;
281 }
282
283 adap->dmxdev.filternum = adap->demux.filternum;
284 adap->dmxdev.demux = &adap->demux.dmx;
285 adap->dmxdev.capabilities = 0;
286
287 ret = dvb_dmxdev_init(&adap->dmxdev, &adap->dvb_adap);
288 if (ret < 0) {
289 err("dvb_dmxdev_init failed: error %d", ret);
290 goto err_dmx_dev;
291 }
292
293 dvb_net_init(&adap->dvb_adap, &adap->dvb_net, &adap->demux.dmx);
294
Michael Krufky04910bd2008-02-03 23:46:16 -0300295 return 0;
296
297err_dmx_dev:
298 dvb_dmx_release(&adap->demux);
299err_dmx:
300 dvb_unregister_adapter(&adap->dvb_adap);
301err:
302 return ret;
303}
304
305static int pvr2_dvb_adapter_exit(struct pvr2_dvb_adapter *adap)
306{
Mike Isely7dcc48f2008-02-09 15:55:54 -0300307 printk(KERN_DEBUG "unregistering DVB devices\n");
308 dvb_net_release(&adap->dvb_net);
309 adap->demux.dmx.close(&adap->demux.dmx);
310 dvb_dmxdev_release(&adap->dmxdev);
311 dvb_dmx_release(&adap->demux);
312 dvb_unregister_adapter(&adap->dvb_adap);
Michael Krufky04910bd2008-02-03 23:46:16 -0300313 return 0;
314}
315
316static int pvr2_dvb_frontend_init(struct pvr2_dvb_adapter *adap)
317{
Mike Iselya36416d2008-02-06 06:59:48 -0300318 struct pvr2_hdw *hdw = adap->channel.hdw;
Michael Krufkybde316a2008-02-10 05:33:37 -0300319 struct pvr2_dvb_props *dvb_props = hdw->hdw_desc->dvb_props;
Mike Isely1cb03b72008-04-21 03:47:43 -0300320 int ret = 0;
Michael Krufky04910bd2008-02-03 23:46:16 -0300321
322 if (dvb_props == NULL) {
323 err("fe_props not defined!");
324 return -EINVAL;
325 }
326
Mike Isely1cb03b72008-04-21 03:47:43 -0300327 ret = pvr2_channel_limit_inputs(
328 &adap->channel,
329 (1 << PVR2_CVAL_INPUT_DTV));
330 if (ret) {
331 err("failed to grab control of dtv input (code=%d)",
332 ret);
Michael Krufkybde316a2008-02-10 05:33:37 -0300333 return ret;
Mike Isely1cb03b72008-04-21 03:47:43 -0300334 }
Michael Krufkybde316a2008-02-10 05:33:37 -0300335
Michael Krufky04910bd2008-02-03 23:46:16 -0300336 if (dvb_props->frontend_attach == NULL) {
337 err("frontend_attach not defined!");
Mike Isely1cb03b72008-04-21 03:47:43 -0300338 ret = -EINVAL;
339 goto done;
Michael Krufky04910bd2008-02-03 23:46:16 -0300340 }
341
342 if ((dvb_props->frontend_attach(adap) == 0) && (adap->fe)) {
343
344 if (dvb_register_frontend(&adap->dvb_adap, adap->fe)) {
345 err("frontend registration failed!");
346 dvb_frontend_detach(adap->fe);
347 adap->fe = NULL;
Mike Isely1cb03b72008-04-21 03:47:43 -0300348 ret = -ENODEV;
349 goto done;
Michael Krufky04910bd2008-02-03 23:46:16 -0300350 }
351
352 if (dvb_props->tuner_attach)
353 dvb_props->tuner_attach(adap);
354
355 if (adap->fe->ops.analog_ops.standby)
356 adap->fe->ops.analog_ops.standby(adap->fe);
357
Michael Krufky99443ae2008-02-03 23:48:09 -0300358 /* Ensure all frontends negotiate bus access */
359 adap->fe->ops.ts_bus_ctrl = pvr2_dvb_bus_ctrl;
360
Michael Krufky04910bd2008-02-03 23:46:16 -0300361 } else {
362 err("no frontend was attached!");
Mike Isely1cb03b72008-04-21 03:47:43 -0300363 ret = -ENODEV;
364 return ret;
Michael Krufky04910bd2008-02-03 23:46:16 -0300365 }
366
Mike Isely1cb03b72008-04-21 03:47:43 -0300367 done:
368 pvr2_channel_limit_inputs(&adap->channel, 0);
369 return ret;
Michael Krufky04910bd2008-02-03 23:46:16 -0300370}
371
372static int pvr2_dvb_frontend_exit(struct pvr2_dvb_adapter *adap)
373{
374 if (adap->fe != NULL) {
375 dvb_unregister_frontend(adap->fe);
376 dvb_frontend_detach(adap->fe);
377 }
378 return 0;
379}
380
Mike Iselyc5317b12008-02-09 19:47:07 -0300381static void pvr2_dvb_destroy(struct pvr2_dvb_adapter *adap)
Mike Isely891d99e2008-02-09 15:44:30 -0300382{
383 pvr2_dvb_stream_end(adap);
384 pvr2_dvb_frontend_exit(adap);
385 pvr2_dvb_adapter_exit(adap);
386 pvr2_channel_done(&adap->channel);
Mike Iselyc5317b12008-02-09 19:47:07 -0300387 kfree(adap);
Mike Isely891d99e2008-02-09 15:44:30 -0300388}
389
390static void pvr2_dvb_internal_check(struct pvr2_channel *chp)
391{
392 struct pvr2_dvb_adapter *adap;
393 adap = container_of(chp, struct pvr2_dvb_adapter, channel);
394 if (!adap->channel.mc_head->disconnect_flag) return;
Mike Iselyc5317b12008-02-09 19:47:07 -0300395 pvr2_dvb_destroy(adap);
Mike Isely891d99e2008-02-09 15:44:30 -0300396}
397
Mike Iselyc5317b12008-02-09 19:47:07 -0300398struct pvr2_dvb_adapter *pvr2_dvb_create(struct pvr2_context *pvr)
Michael Krufky04910bd2008-02-03 23:46:16 -0300399{
400 int ret = 0;
Mike Iselya36416d2008-02-06 06:59:48 -0300401 struct pvr2_dvb_adapter *adap;
Mike Isely129a2f52008-02-09 16:29:52 -0300402 if (!pvr->hdw->hdw_desc->dvb_props) {
403 /* Device lacks a digital interface so don't set up
404 the DVB side of the driver either. For now. */
Mike Iselye6d11862008-02-09 19:47:52 -0300405 return NULL;
Mike Isely129a2f52008-02-09 16:29:52 -0300406 }
Mike Iselyc5317b12008-02-09 19:47:07 -0300407 adap = kzalloc(sizeof(*adap), GFP_KERNEL);
408 if (!adap) return adap;
Mike Iselya36416d2008-02-06 06:59:48 -0300409 pvr2_channel_init(&adap->channel, pvr);
Mike Isely891d99e2008-02-09 15:44:30 -0300410 adap->channel.check_func = pvr2_dvb_internal_check;
Mike Iselya36416d2008-02-06 06:59:48 -0300411 init_waitqueue_head(&adap->buffer_wait_data);
Mike Iselyc5317b12008-02-09 19:47:07 -0300412 mutex_init(&adap->lock);
413 ret = pvr2_dvb_adapter_init(adap);
Mike Isely129a2f52008-02-09 16:29:52 -0300414 if (ret < 0) goto fail1;
Mike Iselyc5317b12008-02-09 19:47:07 -0300415 ret = pvr2_dvb_frontend_init(adap);
Mike Isely129a2f52008-02-09 16:29:52 -0300416 if (ret < 0) goto fail2;
Mike Iselyc5317b12008-02-09 19:47:07 -0300417 return adap;
Mike Isely129a2f52008-02-09 16:29:52 -0300418
419fail2:
420 pvr2_dvb_adapter_exit(adap);
421fail1:
Mike Isely891d99e2008-02-09 15:44:30 -0300422 pvr2_channel_done(&adap->channel);
Mike Iselyc5317b12008-02-09 19:47:07 -0300423 return NULL;
Michael Krufky04910bd2008-02-03 23:46:16 -0300424}
425