]> err.no Git - linux-2.6/blob - drivers/media/video/pvrusb2/pvrusb2-dvb.c
V4L/DVB (7708): pvrusb2-dvb: Fix stuck thread on streaming abort
[linux-2.6] / drivers / media / video / pvrusb2 / pvrusb2-dvb.c
1 /*
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
21 #include <linux/kthread.h>
22 #include <linux/freezer.h>
23 #include "dvbdev.h"
24 #include "pvrusb2-hdw-internal.h"
25 #include "pvrusb2-hdw.h"
26 #include "pvrusb2-io.h"
27 #include "pvrusb2-dvb.h"
28
29 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
30
31 static int pvr2_dvb_feed_func(struct pvr2_dvb_adapter *adap)
32 {
33         int ret;
34         unsigned int count;
35         struct pvr2_buffer *bp;
36         struct pvr2_stream *stream;
37
38         printk(KERN_DEBUG "dvb thread started\n");
39         set_freezable();
40
41         stream = adap->channel.stream->stream;
42
43         for (;;) {
44                 if (adap->feed_thread_stop) break;
45                 if (kthread_should_stop()) break;
46
47                 /* Not sure about this... */
48                 try_to_freeze();
49
50                 bp = pvr2_stream_get_ready_buffer(stream);
51                 if (bp != NULL) {
52                         count = pvr2_buffer_get_count(bp);
53                         if (count) {
54                                 dvb_dmx_swfilter(
55                                         &adap->demux,
56                                         adap->buffer_storage[
57                                             pvr2_buffer_get_id(bp)],
58                                         count);
59                         } else {
60                                 ret = pvr2_buffer_get_status(bp);
61                                 if (ret < 0) break;
62                         }
63                         ret = pvr2_buffer_queue(bp);
64                         if (ret < 0) break;
65
66                         /* Since we know we did something to a buffer,
67                            just go back and try again.  No point in
68                            blocking unless we really ran out of
69                            buffers to process. */
70                         continue;
71                 }
72
73
74                 /* Wait until more buffers become available or we're
75                    told not to wait any longer. */
76                 ret = wait_event_interruptible(
77                     adap->buffer_wait_data,
78                     (pvr2_stream_get_ready_count(stream) > 0) ||
79                     adap->feed_thread_stop);
80                 if (ret < 0) break;
81         }
82
83         /* If we get here and ret is < 0, then an error has occurred.
84            Probably would be a good idea to communicate that to DVB core... */
85
86         printk(KERN_DEBUG "dvb thread stopped\n");
87
88         return 0;
89 }
90
91 static int pvr2_dvb_feed_thread(void *data)
92 {
93         int stat = pvr2_dvb_feed_func(data);
94         /* from videobuf-dvb.c: */
95         while (!kthread_should_stop()) {
96                 set_current_state(TASK_INTERRUPTIBLE);
97                 schedule();
98         }
99         return stat;
100 }
101
102 static void pvr2_dvb_notify(struct pvr2_dvb_adapter *adap)
103 {
104         wake_up(&adap->buffer_wait_data);
105 }
106
107 static void pvr2_dvb_stream_end(struct pvr2_dvb_adapter *adap)
108 {
109         unsigned int idx;
110         struct pvr2_stream *stream;
111
112         if (adap->thread) {
113                 adap->feed_thread_stop = !0;
114                 pvr2_dvb_notify(adap);
115                 kthread_stop(adap->thread);
116                 adap->thread = NULL;
117         }
118
119         if (adap->channel.stream) {
120                 stream = adap->channel.stream->stream;
121         } else {
122                 stream = NULL;
123         }
124         if (stream) {
125                 pvr2_hdw_set_streaming(adap->channel.hdw, 0);
126                 pvr2_stream_set_callback(stream, NULL, NULL);
127                 pvr2_stream_kill(stream);
128                 pvr2_stream_set_buffer_count(stream, 0);
129                 pvr2_channel_claim_stream(&adap->channel, NULL);
130         }
131
132         if (adap->stream_run) {
133                 for (idx = 0; idx < PVR2_DVB_BUFFER_COUNT; idx++) {
134                         if (!(adap->buffer_storage[idx])) continue;
135                         kfree(adap->buffer_storage[idx]);
136                         adap->buffer_storage[idx] = 0;
137                 }
138                 adap->stream_run = 0;
139         }
140 }
141
142 static int pvr2_dvb_stream_do_start(struct pvr2_dvb_adapter *adap)
143 {
144         struct pvr2_context *pvr = adap->channel.mc_head;
145         unsigned int idx;
146         int ret;
147         struct pvr2_buffer *bp;
148         struct pvr2_stream *stream = 0;
149
150         if (adap->stream_run) return -EIO;
151
152         ret = pvr2_channel_claim_stream(&adap->channel, &pvr->video_stream);
153         /* somebody else already has the stream */
154         if (ret < 0) return ret;
155
156         stream = adap->channel.stream->stream;
157
158         for (idx = 0; idx < PVR2_DVB_BUFFER_COUNT; idx++) {
159                 adap->buffer_storage[idx] = kmalloc(PVR2_DVB_BUFFER_SIZE,
160                                                     GFP_KERNEL);
161                 if (!(adap->buffer_storage[idx])) return -ENOMEM;
162         }
163
164         pvr2_stream_set_callback(pvr->video_stream.stream,
165                                  (pvr2_stream_callback) pvr2_dvb_notify, adap);
166
167         ret = pvr2_stream_set_buffer_count(stream, PVR2_DVB_BUFFER_COUNT);
168         if (ret < 0) return ret;
169
170         for (idx = 0; idx < PVR2_DVB_BUFFER_COUNT; idx++) {
171                 bp = pvr2_stream_get_buffer(stream, idx);
172                 pvr2_buffer_set_buffer(bp,
173                                        adap->buffer_storage[idx],
174                                        PVR2_DVB_BUFFER_SIZE);
175         }
176
177         ret = pvr2_hdw_set_streaming(adap->channel.hdw, 1);
178         if (ret < 0) return ret;
179
180         while ((bp = pvr2_stream_get_idle_buffer(stream)) != 0) {
181                 ret = pvr2_buffer_queue(bp);
182                 if (ret < 0) return ret;
183         }
184
185         adap->feed_thread_stop = 0;
186         adap->thread = kthread_run(pvr2_dvb_feed_thread, adap, "pvrusb2-dvb");
187
188         if (IS_ERR(adap->thread)) {
189                 ret = PTR_ERR(adap->thread);
190                 adap->thread = NULL;
191                 return ret;
192         }
193
194         adap->stream_run = !0;
195
196         return 0;
197 }
198
199 static int pvr2_dvb_stream_start(struct pvr2_dvb_adapter *adap)
200 {
201         int ret = pvr2_dvb_stream_do_start(adap);
202         if (ret < 0) pvr2_dvb_stream_end(adap);
203         return ret;
204 }
205
206 static int pvr2_dvb_ctrl_feed(struct dvb_demux_feed *dvbdmxfeed, int onoff)
207 {
208         struct pvr2_dvb_adapter *adap = dvbdmxfeed->demux->priv;
209         int ret = 0;
210
211         if (adap == NULL) return -ENODEV;
212
213         mutex_lock(&adap->lock);
214         do {
215                 if (onoff) {
216                         if (!adap->feedcount) {
217                                 printk(KERN_DEBUG "start feeding\n");
218                                 ret = pvr2_dvb_stream_start(adap);
219                                 if (ret < 0) break;
220                         }
221                         (adap->feedcount)++;
222                 } else if (adap->feedcount > 0) {
223                         (adap->feedcount)--;
224                         if (!adap->feedcount) {
225                                 printk(KERN_DEBUG "stop feeding\n");
226                                 pvr2_dvb_stream_end(adap);
227                         }
228                 }
229         } while (0);
230         mutex_unlock(&adap->lock);
231
232         return ret;
233 }
234
235 static int pvr2_dvb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
236 {
237         printk(KERN_DEBUG "start pid: 0x%04x, feedtype: %d\n",
238                dvbdmxfeed->pid, dvbdmxfeed->type);
239         return pvr2_dvb_ctrl_feed(dvbdmxfeed, 1);
240 }
241
242 static int pvr2_dvb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
243 {
244         printk(KERN_DEBUG "stop pid: 0x%04x, feedtype: %d\n",
245                dvbdmxfeed->pid, dvbdmxfeed->type);
246         return pvr2_dvb_ctrl_feed(dvbdmxfeed, 0);
247 }
248
249 static int pvr2_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
250 {
251         /* TO DO: This function will call into the core and request for
252          * input to be set to 'dtv' if (acquire) and if it isn't set already.
253          *
254          * If (!acquire) then we should do nothing -- don't switch inputs
255          * again unless the analog side of the driver requests the bus.
256          */
257         return 0;
258 }
259
260 static int pvr2_dvb_adapter_init(struct pvr2_dvb_adapter *adap)
261 {
262         int ret;
263
264         ret = dvb_register_adapter(&adap->dvb_adap, "pvrusb2-dvb",
265                                    THIS_MODULE/*&hdw->usb_dev->owner*/,
266                                    &adap->channel.hdw->usb_dev->dev,
267                                    adapter_nr);
268         if (ret < 0) {
269                 err("dvb_register_adapter failed: error %d", ret);
270                 goto err;
271         }
272         adap->dvb_adap.priv = adap;
273
274         adap->demux.dmx.capabilities = DMX_TS_FILTERING |
275                                        DMX_SECTION_FILTERING |
276                                        DMX_MEMORY_BASED_FILTERING;
277         adap->demux.priv             = adap;
278         adap->demux.filternum        = 256;
279         adap->demux.feednum          = 256;
280         adap->demux.start_feed       = pvr2_dvb_start_feed;
281         adap->demux.stop_feed        = pvr2_dvb_stop_feed;
282         adap->demux.write_to_decoder = NULL;
283
284         ret = dvb_dmx_init(&adap->demux);
285         if (ret < 0) {
286                 err("dvb_dmx_init failed: error %d", ret);
287                 goto err_dmx;
288         }
289
290         adap->dmxdev.filternum       = adap->demux.filternum;
291         adap->dmxdev.demux           = &adap->demux.dmx;
292         adap->dmxdev.capabilities    = 0;
293
294         ret = dvb_dmxdev_init(&adap->dmxdev, &adap->dvb_adap);
295         if (ret < 0) {
296                 err("dvb_dmxdev_init failed: error %d", ret);
297                 goto err_dmx_dev;
298         }
299
300         dvb_net_init(&adap->dvb_adap, &adap->dvb_net, &adap->demux.dmx);
301
302         return 0;
303
304 err_dmx_dev:
305         dvb_dmx_release(&adap->demux);
306 err_dmx:
307         dvb_unregister_adapter(&adap->dvb_adap);
308 err:
309         return ret;
310 }
311
312 static int pvr2_dvb_adapter_exit(struct pvr2_dvb_adapter *adap)
313 {
314         printk(KERN_DEBUG "unregistering DVB devices\n");
315         dvb_net_release(&adap->dvb_net);
316         adap->demux.dmx.close(&adap->demux.dmx);
317         dvb_dmxdev_release(&adap->dmxdev);
318         dvb_dmx_release(&adap->demux);
319         dvb_unregister_adapter(&adap->dvb_adap);
320         return 0;
321 }
322
323 static int pvr2_dvb_frontend_init(struct pvr2_dvb_adapter *adap)
324 {
325         struct pvr2_hdw *hdw = adap->channel.hdw;
326         struct pvr2_dvb_props *dvb_props = hdw->hdw_desc->dvb_props;
327         int ret;
328
329         if (dvb_props == NULL) {
330                 err("fe_props not defined!");
331                 return -EINVAL;
332         }
333
334         /* FIXME: This code should be moved into the core,
335          * and should only be called if we don't already have
336          * control of the bus.
337          *
338          * We can't call "pvr2_dvb_bus_ctrl(adap->fe, 1)" from here,
339          * because adap->fe isn't defined yet.
340          */
341         ret = pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_by_id(hdw,
342                                                           PVR2_CID_INPUT),
343                                   PVR2_CVAL_INPUT_DTV);
344         if (ret != 0)
345                 return ret;
346
347         pvr2_hdw_commit_ctl(hdw);
348
349
350         if (dvb_props->frontend_attach == NULL) {
351                 err("frontend_attach not defined!");
352                 return -EINVAL;
353         }
354
355         if ((dvb_props->frontend_attach(adap) == 0) && (adap->fe)) {
356
357                 if (dvb_register_frontend(&adap->dvb_adap, adap->fe)) {
358                         err("frontend registration failed!");
359                         dvb_frontend_detach(adap->fe);
360                         adap->fe = NULL;
361                         return -ENODEV;
362                 }
363
364                 if (dvb_props->tuner_attach)
365                         dvb_props->tuner_attach(adap);
366
367                 if (adap->fe->ops.analog_ops.standby)
368                         adap->fe->ops.analog_ops.standby(adap->fe);
369
370                 /* Ensure all frontends negotiate bus access */
371                 adap->fe->ops.ts_bus_ctrl = pvr2_dvb_bus_ctrl;
372
373         } else {
374                 err("no frontend was attached!");
375                 return -ENODEV;
376         }
377
378         return 0;
379 }
380
381 static int pvr2_dvb_frontend_exit(struct pvr2_dvb_adapter *adap)
382 {
383         if (adap->fe != NULL) {
384                 dvb_unregister_frontend(adap->fe);
385                 dvb_frontend_detach(adap->fe);
386         }
387         return 0;
388 }
389
390 static void pvr2_dvb_destroy(struct pvr2_dvb_adapter *adap)
391 {
392         pvr2_dvb_stream_end(adap);
393         pvr2_dvb_frontend_exit(adap);
394         pvr2_dvb_adapter_exit(adap);
395         pvr2_channel_done(&adap->channel);
396         kfree(adap);
397 }
398
399 static void pvr2_dvb_internal_check(struct pvr2_channel *chp)
400 {
401         struct pvr2_dvb_adapter *adap;
402         adap = container_of(chp, struct pvr2_dvb_adapter, channel);
403         if (!adap->channel.mc_head->disconnect_flag) return;
404         pvr2_dvb_destroy(adap);
405 }
406
407 struct pvr2_dvb_adapter *pvr2_dvb_create(struct pvr2_context *pvr)
408 {
409         int ret = 0;
410         struct pvr2_dvb_adapter *adap;
411         if (!pvr->hdw->hdw_desc->dvb_props) {
412                 /* Device lacks a digital interface so don't set up
413                    the DVB side of the driver either.  For now. */
414                 return NULL;
415         }
416         adap = kzalloc(sizeof(*adap), GFP_KERNEL);
417         if (!adap) return adap;
418         pvr2_channel_init(&adap->channel, pvr);
419         adap->channel.check_func = pvr2_dvb_internal_check;
420         init_waitqueue_head(&adap->buffer_wait_data);
421         mutex_init(&adap->lock);
422         ret = pvr2_dvb_adapter_init(adap);
423         if (ret < 0) goto fail1;
424         ret = pvr2_dvb_frontend_init(adap);
425         if (ret < 0) goto fail2;
426         return adap;
427
428 fail2:
429         pvr2_dvb_adapter_exit(adap);
430 fail1:
431         pvr2_channel_done(&adap->channel);
432         return NULL;
433 }
434