V4L/DVB (8377): ivtv/cx18: ensure the default control values are correct
[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-debug.h"
25 #include "pvrusb2-hdw-internal.h"
26 #include "pvrusb2-hdw.h"
27 #include "pvrusb2-io.h"
28 #include "pvrusb2-dvb.h"
29
30 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
31
32 static int pvr2_dvb_feed_func(struct pvr2_dvb_adapter *adap)
33 {
34         int ret;
35         unsigned int count;
36         struct pvr2_buffer *bp;
37         struct pvr2_stream *stream;
38
39         pvr2_trace(PVR2_TRACE_DVB_FEED, "dvb feed thread started");
40         set_freezable();
41
42         stream = adap->channel.stream->stream;
43
44         for (;;) {
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                     kthread_should_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         pvr2_trace(PVR2_TRACE_DVB_FEED, "dvb feed thread stopped");
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                 kthread_stop(adap->thread);
114                 adap->thread = NULL;
115         }
116
117         if (adap->channel.stream) {
118                 stream = adap->channel.stream->stream;
119         } else {
120                 stream = NULL;
121         }
122         if (stream) {
123                 pvr2_hdw_set_streaming(adap->channel.hdw, 0);
124                 pvr2_stream_set_callback(stream, NULL, NULL);
125                 pvr2_stream_kill(stream);
126                 pvr2_stream_set_buffer_count(stream, 0);
127                 pvr2_channel_claim_stream(&adap->channel, NULL);
128         }
129
130         if (adap->stream_run) {
131                 for (idx = 0; idx < PVR2_DVB_BUFFER_COUNT; idx++) {
132                         if (!(adap->buffer_storage[idx])) continue;
133                         kfree(adap->buffer_storage[idx]);
134                         adap->buffer_storage[idx] = NULL;
135                 }
136                 adap->stream_run = 0;
137         }
138 }
139
140 static int pvr2_dvb_stream_do_start(struct pvr2_dvb_adapter *adap)
141 {
142         struct pvr2_context *pvr = adap->channel.mc_head;
143         unsigned int idx;
144         int ret;
145         struct pvr2_buffer *bp;
146         struct pvr2_stream *stream = NULL;
147
148         if (adap->stream_run) return -EIO;
149
150         ret = pvr2_channel_claim_stream(&adap->channel, &pvr->video_stream);
151         /* somebody else already has the stream */
152         if (ret < 0) return ret;
153
154         stream = adap->channel.stream->stream;
155
156         for (idx = 0; idx < PVR2_DVB_BUFFER_COUNT; idx++) {
157                 adap->buffer_storage[idx] = kmalloc(PVR2_DVB_BUFFER_SIZE,
158                                                     GFP_KERNEL);
159                 if (!(adap->buffer_storage[idx])) return -ENOMEM;
160         }
161
162         pvr2_stream_set_callback(pvr->video_stream.stream,
163                                  (pvr2_stream_callback) pvr2_dvb_notify, adap);
164
165         ret = pvr2_stream_set_buffer_count(stream, PVR2_DVB_BUFFER_COUNT);
166         if (ret < 0) return ret;
167
168         for (idx = 0; idx < PVR2_DVB_BUFFER_COUNT; idx++) {
169                 bp = pvr2_stream_get_buffer(stream, idx);
170                 pvr2_buffer_set_buffer(bp,
171                                        adap->buffer_storage[idx],
172                                        PVR2_DVB_BUFFER_SIZE);
173         }
174
175         ret = pvr2_hdw_set_streaming(adap->channel.hdw, 1);
176         if (ret < 0) return ret;
177
178         while ((bp = pvr2_stream_get_idle_buffer(stream)) != NULL) {
179                 ret = pvr2_buffer_queue(bp);
180                 if (ret < 0) return ret;
181         }
182
183         adap->thread = kthread_run(pvr2_dvb_feed_thread, adap, "pvrusb2-dvb");
184
185         if (IS_ERR(adap->thread)) {
186                 ret = PTR_ERR(adap->thread);
187                 adap->thread = NULL;
188                 return ret;
189         }
190
191         adap->stream_run = !0;
192
193         return 0;
194 }
195
196 static int pvr2_dvb_stream_start(struct pvr2_dvb_adapter *adap)
197 {
198         int ret = pvr2_dvb_stream_do_start(adap);
199         if (ret < 0) pvr2_dvb_stream_end(adap);
200         return ret;
201 }
202
203 static int pvr2_dvb_ctrl_feed(struct dvb_demux_feed *dvbdmxfeed, int onoff)
204 {
205         struct pvr2_dvb_adapter *adap = dvbdmxfeed->demux->priv;
206         int ret = 0;
207
208         if (adap == NULL) return -ENODEV;
209
210         mutex_lock(&adap->lock);
211         do {
212                 if (onoff) {
213                         if (!adap->feedcount) {
214                                 pvr2_trace(PVR2_TRACE_DVB_FEED,
215                                            "start feeding demux");
216                                 ret = pvr2_dvb_stream_start(adap);
217                                 if (ret < 0) break;
218                         }
219                         (adap->feedcount)++;
220                 } else if (adap->feedcount > 0) {
221                         (adap->feedcount)--;
222                         if (!adap->feedcount) {
223                                 pvr2_trace(PVR2_TRACE_DVB_FEED,
224                                            "stop feeding demux");
225                                 pvr2_dvb_stream_end(adap);
226                         }
227                 }
228         } while (0);
229         mutex_unlock(&adap->lock);
230
231         return ret;
232 }
233
234 static int pvr2_dvb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
235 {
236         pvr2_trace(PVR2_TRACE_DVB_FEED, "start pid: 0x%04x", dvbdmxfeed->pid);
237         return pvr2_dvb_ctrl_feed(dvbdmxfeed, 1);
238 }
239
240 static int pvr2_dvb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
241 {
242         pvr2_trace(PVR2_TRACE_DVB_FEED, "stop pid: 0x%04x", dvbdmxfeed->pid);
243         return pvr2_dvb_ctrl_feed(dvbdmxfeed, 0);
244 }
245
246 static int pvr2_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
247 {
248         struct pvr2_dvb_adapter *adap = fe->dvb->priv;
249         return pvr2_channel_limit_inputs(
250             &adap->channel,
251             (acquire ? (1 << PVR2_CVAL_INPUT_DTV) : 0));
252 }
253
254 static int pvr2_dvb_adapter_init(struct pvr2_dvb_adapter *adap)
255 {
256         int ret;
257
258         ret = dvb_register_adapter(&adap->dvb_adap, "pvrusb2-dvb",
259                                    THIS_MODULE/*&hdw->usb_dev->owner*/,
260                                    &adap->channel.hdw->usb_dev->dev,
261                                    adapter_nr);
262         if (ret < 0) {
263                 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
264                            "dvb_register_adapter failed: error %d", ret);
265                 goto err;
266         }
267         adap->dvb_adap.priv = adap;
268
269         adap->demux.dmx.capabilities = DMX_TS_FILTERING |
270                                        DMX_SECTION_FILTERING |
271                                        DMX_MEMORY_BASED_FILTERING;
272         adap->demux.priv             = adap;
273         adap->demux.filternum        = 256;
274         adap->demux.feednum          = 256;
275         adap->demux.start_feed       = pvr2_dvb_start_feed;
276         adap->demux.stop_feed        = pvr2_dvb_stop_feed;
277         adap->demux.write_to_decoder = NULL;
278
279         ret = dvb_dmx_init(&adap->demux);
280         if (ret < 0) {
281                 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
282                            "dvb_dmx_init failed: error %d", ret);
283                 goto err_dmx;
284         }
285
286         adap->dmxdev.filternum       = adap->demux.filternum;
287         adap->dmxdev.demux           = &adap->demux.dmx;
288         adap->dmxdev.capabilities    = 0;
289
290         ret = dvb_dmxdev_init(&adap->dmxdev, &adap->dvb_adap);
291         if (ret < 0) {
292                 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
293                            "dvb_dmxdev_init failed: error %d", ret);
294                 goto err_dmx_dev;
295         }
296
297         dvb_net_init(&adap->dvb_adap, &adap->dvb_net, &adap->demux.dmx);
298
299         return 0;
300
301 err_dmx_dev:
302         dvb_dmx_release(&adap->demux);
303 err_dmx:
304         dvb_unregister_adapter(&adap->dvb_adap);
305 err:
306         return ret;
307 }
308
309 static int pvr2_dvb_adapter_exit(struct pvr2_dvb_adapter *adap)
310 {
311         pvr2_trace(PVR2_TRACE_INFO, "unregistering DVB devices");
312         dvb_net_release(&adap->dvb_net);
313         adap->demux.dmx.close(&adap->demux.dmx);
314         dvb_dmxdev_release(&adap->dmxdev);
315         dvb_dmx_release(&adap->demux);
316         dvb_unregister_adapter(&adap->dvb_adap);
317         return 0;
318 }
319
320 static int pvr2_dvb_frontend_init(struct pvr2_dvb_adapter *adap)
321 {
322         struct pvr2_hdw *hdw = adap->channel.hdw;
323         struct pvr2_dvb_props *dvb_props = hdw->hdw_desc->dvb_props;
324         int ret = 0;
325
326         if (dvb_props == NULL) {
327                 pvr2_trace(PVR2_TRACE_ERROR_LEGS, "fe_props not defined!");
328                 return -EINVAL;
329         }
330
331         ret = pvr2_channel_limit_inputs(
332             &adap->channel,
333             (1 << PVR2_CVAL_INPUT_DTV));
334         if (ret) {
335                 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
336                            "failed to grab control of dtv input (code=%d)",
337                     ret);
338                 return ret;
339         }
340
341         if (dvb_props->frontend_attach == NULL) {
342                 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
343                            "frontend_attach not defined!");
344                 ret = -EINVAL;
345                 goto done;
346         }
347
348         if ((dvb_props->frontend_attach(adap) == 0) && (adap->fe)) {
349
350                 if (dvb_register_frontend(&adap->dvb_adap, adap->fe)) {
351                         pvr2_trace(PVR2_TRACE_ERROR_LEGS,
352                                    "frontend registration failed!");
353                         dvb_frontend_detach(adap->fe);
354                         adap->fe = NULL;
355                         ret = -ENODEV;
356                         goto done;
357                 }
358
359                 if (dvb_props->tuner_attach)
360                         dvb_props->tuner_attach(adap);
361
362                 if (adap->fe->ops.analog_ops.standby)
363                         adap->fe->ops.analog_ops.standby(adap->fe);
364
365                 /* Ensure all frontends negotiate bus access */
366                 adap->fe->ops.ts_bus_ctrl = pvr2_dvb_bus_ctrl;
367
368         } else {
369                 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
370                            "no frontend was attached!");
371                 ret = -ENODEV;
372                 return ret;
373         }
374
375  done:
376         pvr2_channel_limit_inputs(&adap->channel, 0);
377         return ret;
378 }
379
380 static int pvr2_dvb_frontend_exit(struct pvr2_dvb_adapter *adap)
381 {
382         if (adap->fe != NULL) {
383                 dvb_unregister_frontend(adap->fe);
384                 dvb_frontend_detach(adap->fe);
385         }
386         return 0;
387 }
388
389 static void pvr2_dvb_destroy(struct pvr2_dvb_adapter *adap)
390 {
391         pvr2_dvb_stream_end(adap);
392         pvr2_dvb_frontend_exit(adap);
393         pvr2_dvb_adapter_exit(adap);
394         pvr2_channel_done(&adap->channel);
395         kfree(adap);
396 }
397
398 static void pvr2_dvb_internal_check(struct pvr2_channel *chp)
399 {
400         struct pvr2_dvb_adapter *adap;
401         adap = container_of(chp, struct pvr2_dvb_adapter, channel);
402         if (!adap->channel.mc_head->disconnect_flag) return;
403         pvr2_dvb_destroy(adap);
404 }
405
406 struct pvr2_dvb_adapter *pvr2_dvb_create(struct pvr2_context *pvr)
407 {
408         int ret = 0;
409         struct pvr2_dvb_adapter *adap;
410         if (!pvr->hdw->hdw_desc->dvb_props) {
411                 /* Device lacks a digital interface so don't set up
412                    the DVB side of the driver either.  For now. */
413                 return NULL;
414         }
415         adap = kzalloc(sizeof(*adap), GFP_KERNEL);
416         if (!adap) return adap;
417         pvr2_channel_init(&adap->channel, pvr);
418         adap->channel.check_func = pvr2_dvb_internal_check;
419         init_waitqueue_head(&adap->buffer_wait_data);
420         mutex_init(&adap->lock);
421         ret = pvr2_dvb_adapter_init(adap);
422         if (ret < 0) goto fail1;
423         ret = pvr2_dvb_frontend_init(adap);
424         if (ret < 0) goto fail2;
425         return adap;
426
427 fail2:
428         pvr2_dvb_adapter_exit(adap);
429 fail1:
430         pvr2_channel_done(&adap->channel);
431         return NULL;
432 }
433