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