]> err.no Git - linux-2.6/blob - drivers/media/video/pvrusb2/pvrusb2-dvb.c
[POWERPC] spufs: lockdep annotations for spufs_dir_close
[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 (kthread_should_stop()) break;
45
46                 /* Not sure about this... */
47                 try_to_freeze();
48
49                 bp = pvr2_stream_get_ready_buffer(stream);
50                 if (bp != NULL) {
51                         count = pvr2_buffer_get_count(bp);
52                         if (count) {
53                                 dvb_dmx_swfilter(
54                                         &adap->demux,
55                                         adap->buffer_storage[
56                                             pvr2_buffer_get_id(bp)],
57                                         count);
58                         } else {
59                                 ret = pvr2_buffer_get_status(bp);
60                                 if (ret < 0) break;
61                         }
62                         ret = pvr2_buffer_queue(bp);
63                         if (ret < 0) break;
64
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
73                 /* Wait until more buffers become available or we're
74                    told not to wait any longer. */
75                 ret = wait_event_interruptible(
76                     adap->buffer_wait_data,
77                     (pvr2_stream_get_ready_count(stream) > 0) ||
78                     kthread_should_stop());
79                 if (ret < 0) break;
80         }
81
82         /* 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
87         return 0;
88 }
89
90 static int pvr2_dvb_feed_thread(void *data)
91 {
92         int stat = pvr2_dvb_feed_func(data);
93         /* from videobuf-dvb.c: */
94         while (!kthread_should_stop()) {
95                 set_current_state(TASK_INTERRUPTIBLE);
96                 schedule();
97         }
98         return stat;
99 }
100
101 static void pvr2_dvb_notify(struct pvr2_dvb_adapter *adap)
102 {
103         wake_up(&adap->buffer_wait_data);
104 }
105
106 static void pvr2_dvb_stream_end(struct pvr2_dvb_adapter *adap)
107 {
108         unsigned int idx;
109         struct pvr2_stream *stream;
110
111         if (adap->thread) {
112                 kthread_stop(adap->thread);
113                 adap->thread = NULL;
114         }
115
116         if (adap->channel.stream) {
117                 stream = adap->channel.stream->stream;
118         } else {
119                 stream = NULL;
120         }
121         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] = NULL;
134                 }
135                 adap->stream_run = 0;
136         }
137 }
138
139 static 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 = NULL;
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)) != NULL) {
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
195 static 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
202 static 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);
226         mutex_unlock(&adap->lock);
227
228         return ret;
229 }
230
231 static 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);
235         return pvr2_dvb_ctrl_feed(dvbdmxfeed, 1);
236 }
237
238 static 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);
242         return pvr2_dvb_ctrl_feed(dvbdmxfeed, 0);
243 }
244
245 static int pvr2_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
246 {
247         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));
251 }
252
253 static 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*/,
259                                    &adap->channel.hdw->usb_dev->dev,
260                                    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
295         return 0;
296
297 err_dmx_dev:
298         dvb_dmx_release(&adap->demux);
299 err_dmx:
300         dvb_unregister_adapter(&adap->dvb_adap);
301 err:
302         return ret;
303 }
304
305 static int pvr2_dvb_adapter_exit(struct pvr2_dvb_adapter *adap)
306 {
307         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);
313         return 0;
314 }
315
316 static int pvr2_dvb_frontend_init(struct pvr2_dvb_adapter *adap)
317 {
318         struct pvr2_hdw *hdw = adap->channel.hdw;
319         struct pvr2_dvb_props *dvb_props = hdw->hdw_desc->dvb_props;
320         int ret = 0;
321
322         if (dvb_props == NULL) {
323                 err("fe_props not defined!");
324                 return -EINVAL;
325         }
326
327         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);
333                 return ret;
334         }
335
336         if (dvb_props->frontend_attach == NULL) {
337                 err("frontend_attach not defined!");
338                 ret = -EINVAL;
339                 goto done;
340         }
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;
348                         ret = -ENODEV;
349                         goto done;
350                 }
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
358                 /* Ensure all frontends negotiate bus access */
359                 adap->fe->ops.ts_bus_ctrl = pvr2_dvb_bus_ctrl;
360
361         } else {
362                 err("no frontend was attached!");
363                 ret = -ENODEV;
364                 return ret;
365         }
366
367  done:
368         pvr2_channel_limit_inputs(&adap->channel, 0);
369         return ret;
370 }
371
372 static 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
381 static void pvr2_dvb_destroy(struct pvr2_dvb_adapter *adap)
382 {
383         pvr2_dvb_stream_end(adap);
384         pvr2_dvb_frontend_exit(adap);
385         pvr2_dvb_adapter_exit(adap);
386         pvr2_channel_done(&adap->channel);
387         kfree(adap);
388 }
389
390 static 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;
395         pvr2_dvb_destroy(adap);
396 }
397
398 struct pvr2_dvb_adapter *pvr2_dvb_create(struct pvr2_context *pvr)
399 {
400         int ret = 0;
401         struct pvr2_dvb_adapter *adap;
402         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. */
405                 return NULL;
406         }
407         adap = kzalloc(sizeof(*adap), GFP_KERNEL);
408         if (!adap) return adap;
409         pvr2_channel_init(&adap->channel, pvr);
410         adap->channel.check_func = pvr2_dvb_internal_check;
411         init_waitqueue_head(&adap->buffer_wait_data);
412         mutex_init(&adap->lock);
413         ret = pvr2_dvb_adapter_init(adap);
414         if (ret < 0) goto fail1;
415         ret = pvr2_dvb_frontend_init(adap);
416         if (ret < 0) goto fail2;
417         return adap;
418
419 fail2:
420         pvr2_dvb_adapter_exit(adap);
421 fail1:
422         pvr2_channel_done(&adap->channel);
423         return NULL;
424 }
425