Michael Krufky | 04910bd | 2008-02-03 23:46:16 -0300 | [diff] [blame] | 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 | |
Michael Krufky | d8abe97 | 2008-02-03 23:55:07 -0300 | [diff] [blame] | 21 | #include <linux/kthread.h> |
Mike Isely | bb8ce9d | 2008-02-04 00:00:19 -0300 | [diff] [blame] | 22 | #include <linux/freezer.h> |
Michael Krufky | 04910bd | 2008-02-03 23:46:16 -0300 | [diff] [blame] | 23 | #include "dvbdev.h" |
| 24 | #include "pvrusb2-hdw-internal.h" |
| 25 | #include "pvrusb2-hdw.h" |
Mike Isely | bb8ce9d | 2008-02-04 00:00:19 -0300 | [diff] [blame] | 26 | #include "pvrusb2-io.h" |
Michael Krufky | 04910bd | 2008-02-03 23:46:16 -0300 | [diff] [blame] | 27 | #include "pvrusb2-dvb.h" |
| 28 | |
| 29 | DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr); |
| 30 | |
Mike Isely | ceb4340 | 2008-02-06 04:24:51 -0300 | [diff] [blame] | 31 | static int pvr2_dvb_feed_func(struct pvr2_dvb_adapter *adap) |
Mike Isely | bb8ce9d | 2008-02-04 00:00:19 -0300 | [diff] [blame] | 32 | { |
Mike Isely | bb8ce9d | 2008-02-04 00:00:19 -0300 | [diff] [blame] | 33 | int ret; |
| 34 | unsigned int count; |
| 35 | struct pvr2_buffer *bp; |
Mike Isely | a36416d | 2008-02-06 06:59:48 -0300 | [diff] [blame] | 36 | struct pvr2_stream *stream; |
Mike Isely | bb8ce9d | 2008-02-04 00:00:19 -0300 | [diff] [blame] | 37 | |
| 38 | printk(KERN_DEBUG "dvb thread started\n"); |
| 39 | set_freezable(); |
| 40 | |
Mike Isely | a36416d | 2008-02-06 06:59:48 -0300 | [diff] [blame] | 41 | stream = adap->channel.stream->stream; |
Mike Isely | bb8ce9d | 2008-02-04 00:00:19 -0300 | [diff] [blame] | 42 | |
| 43 | for (;;) { |
Mike Isely | a36416d | 2008-02-06 06:59:48 -0300 | [diff] [blame] | 44 | if (kthread_should_stop()) break; |
Mike Isely | bb8ce9d | 2008-02-04 00:00:19 -0300 | [diff] [blame] | 45 | |
| 46 | /* Not sure about this... */ |
| 47 | try_to_freeze(); |
| 48 | |
Mike Isely | a36416d | 2008-02-06 06:59:48 -0300 | [diff] [blame] | 49 | bp = pvr2_stream_get_ready_buffer(stream); |
Mike Isely | bb8ce9d | 2008-02-04 00:00:19 -0300 | [diff] [blame] | 50 | if (bp != NULL) { |
| 51 | count = pvr2_buffer_get_count(bp); |
| 52 | if (count) { |
| 53 | dvb_dmx_swfilter( |
| 54 | &adap->demux, |
Mike Isely | a36416d | 2008-02-06 06:59:48 -0300 | [diff] [blame] | 55 | adap->buffer_storage[ |
| 56 | pvr2_buffer_get_id(bp)], |
Mike Isely | bb8ce9d | 2008-02-04 00:00:19 -0300 | [diff] [blame] | 57 | count); |
| 58 | } else { |
| 59 | ret = pvr2_buffer_get_status(bp); |
Mike Isely | a36416d | 2008-02-06 06:59:48 -0300 | [diff] [blame] | 60 | if (ret < 0) break; |
Mike Isely | bb8ce9d | 2008-02-04 00:00:19 -0300 | [diff] [blame] | 61 | } |
| 62 | ret = pvr2_buffer_queue(bp); |
Mike Isely | a36416d | 2008-02-06 06:59:48 -0300 | [diff] [blame] | 63 | if (ret < 0) break; |
Mike Isely | bb8ce9d | 2008-02-04 00:00:19 -0300 | [diff] [blame] | 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 | |
Mike Isely | 07b8026 | 2008-03-30 20:36:31 -0300 | [diff] [blame] | 73 | /* Wait until more buffers become available or we're |
| 74 | told not to wait any longer. */ |
Mike Isely | bb8ce9d | 2008-02-04 00:00:19 -0300 | [diff] [blame] | 75 | ret = wait_event_interruptible( |
Mike Isely | a36416d | 2008-02-06 06:59:48 -0300 | [diff] [blame] | 76 | adap->buffer_wait_data, |
Mike Isely | 07b8026 | 2008-03-30 20:36:31 -0300 | [diff] [blame] | 77 | (pvr2_stream_get_ready_count(stream) > 0) || |
Mike Isely | 13e027a | 2008-04-07 02:57:13 -0300 | [diff] [blame] | 78 | kthread_should_stop()); |
Mike Isely | a36416d | 2008-02-06 06:59:48 -0300 | [diff] [blame] | 79 | if (ret < 0) break; |
Mike Isely | bb8ce9d | 2008-02-04 00:00:19 -0300 | [diff] [blame] | 80 | } |
| 81 | |
Mike Isely | bb8ce9d | 2008-02-04 00:00:19 -0300 | [diff] [blame] | 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 | |
Mike Isely | ceb4340 | 2008-02-06 04:24:51 -0300 | [diff] [blame] | 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | static int pvr2_dvb_feed_thread(void *data) |
| 91 | { |
| 92 | int stat = pvr2_dvb_feed_func(data); |
Mike Isely | bb8ce9d | 2008-02-04 00:00:19 -0300 | [diff] [blame] | 93 | /* from videobuf-dvb.c: */ |
| 94 | while (!kthread_should_stop()) { |
| 95 | set_current_state(TASK_INTERRUPTIBLE); |
| 96 | schedule(); |
| 97 | } |
Mike Isely | ceb4340 | 2008-02-06 04:24:51 -0300 | [diff] [blame] | 98 | return stat; |
Mike Isely | bb8ce9d | 2008-02-04 00:00:19 -0300 | [diff] [blame] | 99 | } |
| 100 | |
Mike Isely | a36416d | 2008-02-06 06:59:48 -0300 | [diff] [blame] | 101 | static void pvr2_dvb_notify(struct pvr2_dvb_adapter *adap) |
Michael Krufky | d8abe97 | 2008-02-03 23:55:07 -0300 | [diff] [blame] | 102 | { |
Mike Isely | a36416d | 2008-02-06 06:59:48 -0300 | [diff] [blame] | 103 | wake_up(&adap->buffer_wait_data); |
| 104 | } |
Michael Krufky | d8abe97 | 2008-02-03 23:55:07 -0300 | [diff] [blame] | 105 | |
Mike Isely | a36416d | 2008-02-06 06:59:48 -0300 | [diff] [blame] | 106 | static void pvr2_dvb_stream_end(struct pvr2_dvb_adapter *adap) |
| 107 | { |
| 108 | unsigned int idx; |
| 109 | struct pvr2_stream *stream; |
Michael Krufky | d8abe97 | 2008-02-03 23:55:07 -0300 | [diff] [blame] | 110 | |
Mike Isely | a36416d | 2008-02-06 06:59:48 -0300 | [diff] [blame] | 111 | if (adap->thread) { |
| 112 | kthread_stop(adap->thread); |
Michael Krufky | d8abe97 | 2008-02-03 23:55:07 -0300 | [diff] [blame] | 113 | adap->thread = NULL; |
| 114 | } |
| 115 | |
Mike Isely | a36416d | 2008-02-06 06:59:48 -0300 | [diff] [blame] | 116 | if (adap->channel.stream) { |
| 117 | stream = adap->channel.stream->stream; |
| 118 | } else { |
| 119 | stream = NULL; |
Michael Krufky | d8abe97 | 2008-02-03 23:55:07 -0300 | [diff] [blame] | 120 | } |
Mike Isely | a36416d | 2008-02-06 06:59:48 -0300 | [diff] [blame] | 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] = 0; |
| 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 = 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 | |
| 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); |
Michael Krufky | d8abe97 | 2008-02-03 23:55:07 -0300 | [diff] [blame] | 226 | mutex_unlock(&adap->lock); |
| 227 | |
| 228 | return ret; |
| 229 | } |
| 230 | |
Michael Krufky | 04910bd | 2008-02-03 23:46:16 -0300 | [diff] [blame] | 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); |
Michael Krufky | d8abe97 | 2008-02-03 23:55:07 -0300 | [diff] [blame] | 235 | return pvr2_dvb_ctrl_feed(dvbdmxfeed, 1); |
Michael Krufky | 04910bd | 2008-02-03 23:46:16 -0300 | [diff] [blame] | 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); |
Michael Krufky | d8abe97 | 2008-02-03 23:55:07 -0300 | [diff] [blame] | 242 | return pvr2_dvb_ctrl_feed(dvbdmxfeed, 0); |
Michael Krufky | 04910bd | 2008-02-03 23:46:16 -0300 | [diff] [blame] | 243 | } |
| 244 | |
Michael Krufky | 99443ae | 2008-02-03 23:48:09 -0300 | [diff] [blame] | 245 | static int pvr2_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire) |
| 246 | { |
Mike Isely | 1cb03b7 | 2008-04-21 03:47:43 -0300 | [diff] [blame^] | 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)); |
Michael Krufky | 99443ae | 2008-02-03 23:48:09 -0300 | [diff] [blame] | 251 | } |
| 252 | |
Michael Krufky | 04910bd | 2008-02-03 23:46:16 -0300 | [diff] [blame] | 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*/, |
Mike Isely | a36416d | 2008-02-06 06:59:48 -0300 | [diff] [blame] | 259 | &adap->channel.hdw->usb_dev->dev, |
Michael Krufky | 04910bd | 2008-02-03 23:46:16 -0300 | [diff] [blame] | 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 | |
Michael Krufky | 04910bd | 2008-02-03 23:46:16 -0300 | [diff] [blame] | 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 | { |
Mike Isely | 7dcc48f | 2008-02-09 15:55:54 -0300 | [diff] [blame] | 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); |
Michael Krufky | 04910bd | 2008-02-03 23:46:16 -0300 | [diff] [blame] | 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | static int pvr2_dvb_frontend_init(struct pvr2_dvb_adapter *adap) |
| 317 | { |
Mike Isely | a36416d | 2008-02-06 06:59:48 -0300 | [diff] [blame] | 318 | struct pvr2_hdw *hdw = adap->channel.hdw; |
Michael Krufky | bde316a | 2008-02-10 05:33:37 -0300 | [diff] [blame] | 319 | struct pvr2_dvb_props *dvb_props = hdw->hdw_desc->dvb_props; |
Mike Isely | 1cb03b7 | 2008-04-21 03:47:43 -0300 | [diff] [blame^] | 320 | int ret = 0; |
Michael Krufky | 04910bd | 2008-02-03 23:46:16 -0300 | [diff] [blame] | 321 | |
| 322 | if (dvb_props == NULL) { |
| 323 | err("fe_props not defined!"); |
| 324 | return -EINVAL; |
| 325 | } |
| 326 | |
Mike Isely | 1cb03b7 | 2008-04-21 03:47:43 -0300 | [diff] [blame^] | 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); |
Michael Krufky | bde316a | 2008-02-10 05:33:37 -0300 | [diff] [blame] | 333 | return ret; |
Mike Isely | 1cb03b7 | 2008-04-21 03:47:43 -0300 | [diff] [blame^] | 334 | } |
Michael Krufky | bde316a | 2008-02-10 05:33:37 -0300 | [diff] [blame] | 335 | |
Michael Krufky | 04910bd | 2008-02-03 23:46:16 -0300 | [diff] [blame] | 336 | if (dvb_props->frontend_attach == NULL) { |
| 337 | err("frontend_attach not defined!"); |
Mike Isely | 1cb03b7 | 2008-04-21 03:47:43 -0300 | [diff] [blame^] | 338 | ret = -EINVAL; |
| 339 | goto done; |
Michael Krufky | 04910bd | 2008-02-03 23:46:16 -0300 | [diff] [blame] | 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; |
Mike Isely | 1cb03b7 | 2008-04-21 03:47:43 -0300 | [diff] [blame^] | 348 | ret = -ENODEV; |
| 349 | goto done; |
Michael Krufky | 04910bd | 2008-02-03 23:46:16 -0300 | [diff] [blame] | 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 | |
Michael Krufky | 99443ae | 2008-02-03 23:48:09 -0300 | [diff] [blame] | 358 | /* Ensure all frontends negotiate bus access */ |
| 359 | adap->fe->ops.ts_bus_ctrl = pvr2_dvb_bus_ctrl; |
| 360 | |
Michael Krufky | 04910bd | 2008-02-03 23:46:16 -0300 | [diff] [blame] | 361 | } else { |
| 362 | err("no frontend was attached!"); |
Mike Isely | 1cb03b7 | 2008-04-21 03:47:43 -0300 | [diff] [blame^] | 363 | ret = -ENODEV; |
| 364 | return ret; |
Michael Krufky | 04910bd | 2008-02-03 23:46:16 -0300 | [diff] [blame] | 365 | } |
| 366 | |
Mike Isely | 1cb03b7 | 2008-04-21 03:47:43 -0300 | [diff] [blame^] | 367 | done: |
| 368 | pvr2_channel_limit_inputs(&adap->channel, 0); |
| 369 | return ret; |
Michael Krufky | 04910bd | 2008-02-03 23:46:16 -0300 | [diff] [blame] | 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 | |
Mike Isely | c5317b1 | 2008-02-09 19:47:07 -0300 | [diff] [blame] | 381 | static void pvr2_dvb_destroy(struct pvr2_dvb_adapter *adap) |
Mike Isely | 891d99e | 2008-02-09 15:44:30 -0300 | [diff] [blame] | 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); |
Mike Isely | c5317b1 | 2008-02-09 19:47:07 -0300 | [diff] [blame] | 387 | kfree(adap); |
Mike Isely | 891d99e | 2008-02-09 15:44:30 -0300 | [diff] [blame] | 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; |
Mike Isely | c5317b1 | 2008-02-09 19:47:07 -0300 | [diff] [blame] | 395 | pvr2_dvb_destroy(adap); |
Mike Isely | 891d99e | 2008-02-09 15:44:30 -0300 | [diff] [blame] | 396 | } |
| 397 | |
Mike Isely | c5317b1 | 2008-02-09 19:47:07 -0300 | [diff] [blame] | 398 | struct pvr2_dvb_adapter *pvr2_dvb_create(struct pvr2_context *pvr) |
Michael Krufky | 04910bd | 2008-02-03 23:46:16 -0300 | [diff] [blame] | 399 | { |
| 400 | int ret = 0; |
Mike Isely | a36416d | 2008-02-06 06:59:48 -0300 | [diff] [blame] | 401 | struct pvr2_dvb_adapter *adap; |
Mike Isely | 129a2f5 | 2008-02-09 16:29:52 -0300 | [diff] [blame] | 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. */ |
Mike Isely | e6d1186 | 2008-02-09 19:47:52 -0300 | [diff] [blame] | 405 | return NULL; |
Mike Isely | 129a2f5 | 2008-02-09 16:29:52 -0300 | [diff] [blame] | 406 | } |
Mike Isely | c5317b1 | 2008-02-09 19:47:07 -0300 | [diff] [blame] | 407 | adap = kzalloc(sizeof(*adap), GFP_KERNEL); |
| 408 | if (!adap) return adap; |
Mike Isely | a36416d | 2008-02-06 06:59:48 -0300 | [diff] [blame] | 409 | pvr2_channel_init(&adap->channel, pvr); |
Mike Isely | 891d99e | 2008-02-09 15:44:30 -0300 | [diff] [blame] | 410 | adap->channel.check_func = pvr2_dvb_internal_check; |
Mike Isely | a36416d | 2008-02-06 06:59:48 -0300 | [diff] [blame] | 411 | init_waitqueue_head(&adap->buffer_wait_data); |
Mike Isely | c5317b1 | 2008-02-09 19:47:07 -0300 | [diff] [blame] | 412 | mutex_init(&adap->lock); |
| 413 | ret = pvr2_dvb_adapter_init(adap); |
Mike Isely | 129a2f5 | 2008-02-09 16:29:52 -0300 | [diff] [blame] | 414 | if (ret < 0) goto fail1; |
Mike Isely | c5317b1 | 2008-02-09 19:47:07 -0300 | [diff] [blame] | 415 | ret = pvr2_dvb_frontend_init(adap); |
Mike Isely | 129a2f5 | 2008-02-09 16:29:52 -0300 | [diff] [blame] | 416 | if (ret < 0) goto fail2; |
Mike Isely | c5317b1 | 2008-02-09 19:47:07 -0300 | [diff] [blame] | 417 | return adap; |
Mike Isely | 129a2f5 | 2008-02-09 16:29:52 -0300 | [diff] [blame] | 418 | |
| 419 | fail2: |
| 420 | pvr2_dvb_adapter_exit(adap); |
| 421 | fail1: |
Mike Isely | 891d99e | 2008-02-09 15:44:30 -0300 | [diff] [blame] | 422 | pvr2_channel_done(&adap->channel); |
Mike Isely | c5317b1 | 2008-02-09 19:47:07 -0300 | [diff] [blame] | 423 | return NULL; |
Michael Krufky | 04910bd | 2008-02-03 23:46:16 -0300 | [diff] [blame] | 424 | } |
| 425 | |