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 | * |
Michael Krufky | 04910bd | 2008-02-03 23:46:16 -0300 | [diff] [blame] | 15 | */ |
| 16 | |
Michael Krufky | d8abe97 | 2008-02-03 23:55:07 -0300 | [diff] [blame] | 17 | #include <linux/kthread.h> |
Mike Isely | bb8ce9d | 2008-02-04 00:00:19 -0300 | [diff] [blame] | 18 | #include <linux/freezer.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/slab.h> |
Andrea Righi | 27ac792 | 2008-07-23 21:28:13 -0700 | [diff] [blame] | 20 | #include <linux/mm.h> |
Mauro Carvalho Chehab | fada193 | 2017-12-28 13:03:51 -0500 | [diff] [blame^] | 21 | #include <media/dvbdev.h> |
Michael Krufky | 485fcae | 2008-04-26 19:44:59 -0300 | [diff] [blame] | 22 | #include "pvrusb2-debug.h" |
Michael Krufky | 04910bd | 2008-02-03 23:46:16 -0300 | [diff] [blame] | 23 | #include "pvrusb2-hdw-internal.h" |
| 24 | #include "pvrusb2-hdw.h" |
Mike Isely | bb8ce9d | 2008-02-04 00:00:19 -0300 | [diff] [blame] | 25 | #include "pvrusb2-io.h" |
Michael Krufky | 04910bd | 2008-02-03 23:46:16 -0300 | [diff] [blame] | 26 | #include "pvrusb2-dvb.h" |
| 27 | |
| 28 | DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr); |
| 29 | |
Mike Isely | ceb4340 | 2008-02-06 04:24:51 -0300 | [diff] [blame] | 30 | static int pvr2_dvb_feed_func(struct pvr2_dvb_adapter *adap) |
Mike Isely | bb8ce9d | 2008-02-04 00:00:19 -0300 | [diff] [blame] | 31 | { |
Mike Isely | bb8ce9d | 2008-02-04 00:00:19 -0300 | [diff] [blame] | 32 | int ret; |
| 33 | unsigned int count; |
| 34 | struct pvr2_buffer *bp; |
Mike Isely | a36416d | 2008-02-06 06:59:48 -0300 | [diff] [blame] | 35 | struct pvr2_stream *stream; |
Mike Isely | bb8ce9d | 2008-02-04 00:00:19 -0300 | [diff] [blame] | 36 | |
Michael Krufky | 485fcae | 2008-04-26 19:44:59 -0300 | [diff] [blame] | 37 | pvr2_trace(PVR2_TRACE_DVB_FEED, "dvb feed thread started"); |
Mike Isely | bb8ce9d | 2008-02-04 00:00:19 -0300 | [diff] [blame] | 38 | set_freezable(); |
| 39 | |
Mike Isely | a36416d | 2008-02-06 06:59:48 -0300 | [diff] [blame] | 40 | stream = adap->channel.stream->stream; |
Mike Isely | bb8ce9d | 2008-02-04 00:00:19 -0300 | [diff] [blame] | 41 | |
| 42 | for (;;) { |
Mike Isely | a36416d | 2008-02-06 06:59:48 -0300 | [diff] [blame] | 43 | if (kthread_should_stop()) break; |
Mike Isely | bb8ce9d | 2008-02-04 00:00:19 -0300 | [diff] [blame] | 44 | |
| 45 | /* Not sure about this... */ |
| 46 | try_to_freeze(); |
| 47 | |
Mike Isely | a36416d | 2008-02-06 06:59:48 -0300 | [diff] [blame] | 48 | bp = pvr2_stream_get_ready_buffer(stream); |
Mike Isely | bb8ce9d | 2008-02-04 00:00:19 -0300 | [diff] [blame] | 49 | if (bp != NULL) { |
| 50 | count = pvr2_buffer_get_count(bp); |
| 51 | if (count) { |
| 52 | dvb_dmx_swfilter( |
| 53 | &adap->demux, |
Mike Isely | a36416d | 2008-02-06 06:59:48 -0300 | [diff] [blame] | 54 | adap->buffer_storage[ |
| 55 | pvr2_buffer_get_id(bp)], |
Mike Isely | bb8ce9d | 2008-02-04 00:00:19 -0300 | [diff] [blame] | 56 | count); |
| 57 | } else { |
| 58 | ret = pvr2_buffer_get_status(bp); |
Mike Isely | a36416d | 2008-02-06 06:59:48 -0300 | [diff] [blame] | 59 | if (ret < 0) break; |
Mike Isely | bb8ce9d | 2008-02-04 00:00:19 -0300 | [diff] [blame] | 60 | } |
| 61 | ret = pvr2_buffer_queue(bp); |
Mike Isely | a36416d | 2008-02-06 06:59:48 -0300 | [diff] [blame] | 62 | if (ret < 0) break; |
Mike Isely | bb8ce9d | 2008-02-04 00:00:19 -0300 | [diff] [blame] | 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 | |
Mike Isely | 07b8026 | 2008-03-30 20:36:31 -0300 | [diff] [blame] | 72 | /* Wait until more buffers become available or we're |
| 73 | told not to wait any longer. */ |
Mike Isely | bb8ce9d | 2008-02-04 00:00:19 -0300 | [diff] [blame] | 74 | ret = wait_event_interruptible( |
Mike Isely | a36416d | 2008-02-06 06:59:48 -0300 | [diff] [blame] | 75 | adap->buffer_wait_data, |
Mike Isely | 07b8026 | 2008-03-30 20:36:31 -0300 | [diff] [blame] | 76 | (pvr2_stream_get_ready_count(stream) > 0) || |
Mike Isely | 13e027a | 2008-04-07 02:57:13 -0300 | [diff] [blame] | 77 | kthread_should_stop()); |
Mike Isely | a36416d | 2008-02-06 06:59:48 -0300 | [diff] [blame] | 78 | if (ret < 0) break; |
Mike Isely | bb8ce9d | 2008-02-04 00:00:19 -0300 | [diff] [blame] | 79 | } |
| 80 | |
Mike Isely | bb8ce9d | 2008-02-04 00:00:19 -0300 | [diff] [blame] | 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 | |
Michael Krufky | 485fcae | 2008-04-26 19:44:59 -0300 | [diff] [blame] | 84 | pvr2_trace(PVR2_TRACE_DVB_FEED, "dvb feed thread stopped"); |
Mike Isely | bb8ce9d | 2008-02-04 00:00:19 -0300 | [diff] [blame] | 85 | |
Mike Isely | ceb4340 | 2008-02-06 04:24:51 -0300 | [diff] [blame] | 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | static int pvr2_dvb_feed_thread(void *data) |
| 90 | { |
| 91 | int stat = pvr2_dvb_feed_func(data); |
Mike Isely | bb8ce9d | 2008-02-04 00:00:19 -0300 | [diff] [blame] | 92 | /* from videobuf-dvb.c: */ |
| 93 | while (!kthread_should_stop()) { |
| 94 | set_current_state(TASK_INTERRUPTIBLE); |
| 95 | schedule(); |
| 96 | } |
Mike Isely | ceb4340 | 2008-02-06 04:24:51 -0300 | [diff] [blame] | 97 | return stat; |
Mike Isely | bb8ce9d | 2008-02-04 00:00:19 -0300 | [diff] [blame] | 98 | } |
| 99 | |
Mike Isely | a36416d | 2008-02-06 06:59:48 -0300 | [diff] [blame] | 100 | static void pvr2_dvb_notify(struct pvr2_dvb_adapter *adap) |
Michael Krufky | d8abe97 | 2008-02-03 23:55:07 -0300 | [diff] [blame] | 101 | { |
Mike Isely | a36416d | 2008-02-06 06:59:48 -0300 | [diff] [blame] | 102 | wake_up(&adap->buffer_wait_data); |
| 103 | } |
Michael Krufky | d8abe97 | 2008-02-03 23:55:07 -0300 | [diff] [blame] | 104 | |
Mike Isely | a36416d | 2008-02-06 06:59:48 -0300 | [diff] [blame] | 105 | static void pvr2_dvb_stream_end(struct pvr2_dvb_adapter *adap) |
| 106 | { |
| 107 | unsigned int idx; |
| 108 | struct pvr2_stream *stream; |
Michael Krufky | d8abe97 | 2008-02-03 23:55:07 -0300 | [diff] [blame] | 109 | |
Mike Isely | a36416d | 2008-02-06 06:59:48 -0300 | [diff] [blame] | 110 | if (adap->thread) { |
| 111 | kthread_stop(adap->thread); |
Michael Krufky | d8abe97 | 2008-02-03 23:55:07 -0300 | [diff] [blame] | 112 | adap->thread = NULL; |
| 113 | } |
| 114 | |
Mike Isely | a36416d | 2008-02-06 06:59:48 -0300 | [diff] [blame] | 115 | if (adap->channel.stream) { |
| 116 | stream = adap->channel.stream->stream; |
| 117 | } else { |
| 118 | stream = NULL; |
Michael Krufky | d8abe97 | 2008-02-03 23:55:07 -0300 | [diff] [blame] | 119 | } |
Mike Isely | a36416d | 2008-02-06 06:59:48 -0300 | [diff] [blame] | 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]); |
Harvey Harrison | a6a3a17 | 2008-04-28 16:50:03 -0700 | [diff] [blame] | 132 | adap->buffer_storage[idx] = NULL; |
Mike Isely | a36416d | 2008-02-06 06:59:48 -0300 | [diff] [blame] | 133 | } |
| 134 | adap->stream_run = 0; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | static 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; |
Harvey Harrison | a6a3a17 | 2008-04-28 16:50:03 -0700 | [diff] [blame] | 144 | struct pvr2_stream *stream = NULL; |
Mike Isely | a36416d | 2008-02-06 06:59:48 -0300 | [diff] [blame] | 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 | |
Harvey Harrison | a6a3a17 | 2008-04-28 16:50:03 -0700 | [diff] [blame] | 176 | while ((bp = pvr2_stream_get_idle_buffer(stream)) != NULL) { |
Mike Isely | a36416d | 2008-02-06 06:59:48 -0300 | [diff] [blame] | 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 | |
| 194 | static 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 | |
| 201 | static 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) { |
Michael Krufky | 485fcae | 2008-04-26 19:44:59 -0300 | [diff] [blame] | 212 | pvr2_trace(PVR2_TRACE_DVB_FEED, |
| 213 | "start feeding demux"); |
Mike Isely | a36416d | 2008-02-06 06:59:48 -0300 | [diff] [blame] | 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) { |
Michael Krufky | 485fcae | 2008-04-26 19:44:59 -0300 | [diff] [blame] | 221 | pvr2_trace(PVR2_TRACE_DVB_FEED, |
| 222 | "stop feeding demux"); |
Mike Isely | a36416d | 2008-02-06 06:59:48 -0300 | [diff] [blame] | 223 | pvr2_dvb_stream_end(adap); |
| 224 | } |
| 225 | } |
| 226 | } while (0); |
Michael Krufky | d8abe97 | 2008-02-03 23:55:07 -0300 | [diff] [blame] | 227 | mutex_unlock(&adap->lock); |
| 228 | |
| 229 | return ret; |
| 230 | } |
| 231 | |
Michael Krufky | 04910bd | 2008-02-03 23:46:16 -0300 | [diff] [blame] | 232 | static int pvr2_dvb_start_feed(struct dvb_demux_feed *dvbdmxfeed) |
| 233 | { |
Michael Krufky | 485fcae | 2008-04-26 19:44:59 -0300 | [diff] [blame] | 234 | pvr2_trace(PVR2_TRACE_DVB_FEED, "start pid: 0x%04x", dvbdmxfeed->pid); |
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 | { |
Michael Krufky | 485fcae | 2008-04-26 19:44:59 -0300 | [diff] [blame] | 240 | pvr2_trace(PVR2_TRACE_DVB_FEED, "stop pid: 0x%04x", dvbdmxfeed->pid); |
Michael Krufky | d8abe97 | 2008-02-03 23:55:07 -0300 | [diff] [blame] | 241 | return pvr2_dvb_ctrl_feed(dvbdmxfeed, 0); |
Michael Krufky | 04910bd | 2008-02-03 23:46:16 -0300 | [diff] [blame] | 242 | } |
| 243 | |
Michael Krufky | 99443ae | 2008-02-03 23:48:09 -0300 | [diff] [blame] | 244 | static int pvr2_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire) |
| 245 | { |
Mike Isely | 1cb03b7 | 2008-04-21 03:47:43 -0300 | [diff] [blame] | 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)); |
Michael Krufky | 99443ae | 2008-02-03 23:48:09 -0300 | [diff] [blame] | 250 | } |
| 251 | |
Michael Krufky | 04910bd | 2008-02-03 23:46:16 -0300 | [diff] [blame] | 252 | static 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*/, |
Mike Isely | a36416d | 2008-02-06 06:59:48 -0300 | [diff] [blame] | 258 | &adap->channel.hdw->usb_dev->dev, |
Michael Krufky | 04910bd | 2008-02-03 23:46:16 -0300 | [diff] [blame] | 259 | adapter_nr); |
| 260 | if (ret < 0) { |
Michael Krufky | 485fcae | 2008-04-26 19:44:59 -0300 | [diff] [blame] | 261 | pvr2_trace(PVR2_TRACE_ERROR_LEGS, |
| 262 | "dvb_register_adapter failed: error %d", ret); |
Michael Krufky | 04910bd | 2008-02-03 23:46:16 -0300 | [diff] [blame] | 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) { |
Michael Krufky | 485fcae | 2008-04-26 19:44:59 -0300 | [diff] [blame] | 279 | pvr2_trace(PVR2_TRACE_ERROR_LEGS, |
| 280 | "dvb_dmx_init failed: error %d", ret); |
Michael Krufky | 04910bd | 2008-02-03 23:46:16 -0300 | [diff] [blame] | 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) { |
Michael Krufky | 485fcae | 2008-04-26 19:44:59 -0300 | [diff] [blame] | 290 | pvr2_trace(PVR2_TRACE_ERROR_LEGS, |
| 291 | "dvb_dmxdev_init failed: error %d", ret); |
Michael Krufky | 04910bd | 2008-02-03 23:46:16 -0300 | [diff] [blame] | 292 | goto err_dmx_dev; |
| 293 | } |
| 294 | |
| 295 | dvb_net_init(&adap->dvb_adap, &adap->dvb_net, &adap->demux.dmx); |
| 296 | |
Michael Krufky | 04910bd | 2008-02-03 23:46:16 -0300 | [diff] [blame] | 297 | return 0; |
| 298 | |
| 299 | err_dmx_dev: |
| 300 | dvb_dmx_release(&adap->demux); |
| 301 | err_dmx: |
| 302 | dvb_unregister_adapter(&adap->dvb_adap); |
| 303 | err: |
| 304 | return ret; |
| 305 | } |
| 306 | |
| 307 | static int pvr2_dvb_adapter_exit(struct pvr2_dvb_adapter *adap) |
| 308 | { |
Michael Krufky | 485fcae | 2008-04-26 19:44:59 -0300 | [diff] [blame] | 309 | pvr2_trace(PVR2_TRACE_INFO, "unregistering DVB devices"); |
Mike Isely | 7dcc48f | 2008-02-09 15:55:54 -0300 | [diff] [blame] | 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); |
Michael Krufky | 04910bd | 2008-02-03 23:46:16 -0300 | [diff] [blame] | 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | static int pvr2_dvb_frontend_init(struct pvr2_dvb_adapter *adap) |
| 319 | { |
Mike Isely | a36416d | 2008-02-06 06:59:48 -0300 | [diff] [blame] | 320 | struct pvr2_hdw *hdw = adap->channel.hdw; |
Mike Isely | 69ea3c1 | 2009-03-07 02:08:58 -0300 | [diff] [blame] | 321 | const struct pvr2_dvb_props *dvb_props = hdw->hdw_desc->dvb_props; |
Mike Isely | 1cb03b7 | 2008-04-21 03:47:43 -0300 | [diff] [blame] | 322 | int ret = 0; |
Michael Krufky | 04910bd | 2008-02-03 23:46:16 -0300 | [diff] [blame] | 323 | |
| 324 | if (dvb_props == NULL) { |
Michael Krufky | 485fcae | 2008-04-26 19:44:59 -0300 | [diff] [blame] | 325 | pvr2_trace(PVR2_TRACE_ERROR_LEGS, "fe_props not defined!"); |
Michael Krufky | 04910bd | 2008-02-03 23:46:16 -0300 | [diff] [blame] | 326 | return -EINVAL; |
| 327 | } |
| 328 | |
Mike Isely | 1cb03b7 | 2008-04-21 03:47:43 -0300 | [diff] [blame] | 329 | ret = pvr2_channel_limit_inputs( |
| 330 | &adap->channel, |
| 331 | (1 << PVR2_CVAL_INPUT_DTV)); |
| 332 | if (ret) { |
Michael Krufky | 485fcae | 2008-04-26 19:44:59 -0300 | [diff] [blame] | 333 | pvr2_trace(PVR2_TRACE_ERROR_LEGS, |
| 334 | "failed to grab control of dtv input (code=%d)", |
Mike Isely | 1cb03b7 | 2008-04-21 03:47:43 -0300 | [diff] [blame] | 335 | ret); |
Michael Krufky | bde316a | 2008-02-10 05:33:37 -0300 | [diff] [blame] | 336 | return ret; |
Mike Isely | 1cb03b7 | 2008-04-21 03:47:43 -0300 | [diff] [blame] | 337 | } |
Michael Krufky | bde316a | 2008-02-10 05:33:37 -0300 | [diff] [blame] | 338 | |
Michael Krufky | 04910bd | 2008-02-03 23:46:16 -0300 | [diff] [blame] | 339 | if (dvb_props->frontend_attach == NULL) { |
Michael Krufky | 485fcae | 2008-04-26 19:44:59 -0300 | [diff] [blame] | 340 | pvr2_trace(PVR2_TRACE_ERROR_LEGS, |
| 341 | "frontend_attach not defined!"); |
Mike Isely | 1cb03b7 | 2008-04-21 03:47:43 -0300 | [diff] [blame] | 342 | ret = -EINVAL; |
| 343 | goto done; |
Michael Krufky | 04910bd | 2008-02-03 23:46:16 -0300 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | if ((dvb_props->frontend_attach(adap) == 0) && (adap->fe)) { |
| 347 | |
| 348 | if (dvb_register_frontend(&adap->dvb_adap, adap->fe)) { |
Michael Krufky | 485fcae | 2008-04-26 19:44:59 -0300 | [diff] [blame] | 349 | pvr2_trace(PVR2_TRACE_ERROR_LEGS, |
| 350 | "frontend registration failed!"); |
Michael Krufky | 04910bd | 2008-02-03 23:46:16 -0300 | [diff] [blame] | 351 | dvb_frontend_detach(adap->fe); |
| 352 | adap->fe = NULL; |
Mike Isely | 1cb03b7 | 2008-04-21 03:47:43 -0300 | [diff] [blame] | 353 | ret = -ENODEV; |
| 354 | goto done; |
Michael Krufky | 04910bd | 2008-02-03 23:46:16 -0300 | [diff] [blame] | 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 | |
Michael Krufky | 99443ae | 2008-02-03 23:48:09 -0300 | [diff] [blame] | 363 | /* Ensure all frontends negotiate bus access */ |
| 364 | adap->fe->ops.ts_bus_ctrl = pvr2_dvb_bus_ctrl; |
| 365 | |
Michael Krufky | 04910bd | 2008-02-03 23:46:16 -0300 | [diff] [blame] | 366 | } else { |
Michael Krufky | 485fcae | 2008-04-26 19:44:59 -0300 | [diff] [blame] | 367 | pvr2_trace(PVR2_TRACE_ERROR_LEGS, |
| 368 | "no frontend was attached!"); |
Mike Isely | 1cb03b7 | 2008-04-21 03:47:43 -0300 | [diff] [blame] | 369 | ret = -ENODEV; |
| 370 | return ret; |
Michael Krufky | 04910bd | 2008-02-03 23:46:16 -0300 | [diff] [blame] | 371 | } |
| 372 | |
Mike Isely | 1cb03b7 | 2008-04-21 03:47:43 -0300 | [diff] [blame] | 373 | done: |
| 374 | pvr2_channel_limit_inputs(&adap->channel, 0); |
| 375 | return ret; |
Michael Krufky | 04910bd | 2008-02-03 23:46:16 -0300 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | static 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 | |
Mike Isely | c5317b1 | 2008-02-09 19:47:07 -0300 | [diff] [blame] | 387 | static void pvr2_dvb_destroy(struct pvr2_dvb_adapter *adap) |
Mike Isely | 891d99e | 2008-02-09 15:44:30 -0300 | [diff] [blame] | 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); |
Mike Isely | c5317b1 | 2008-02-09 19:47:07 -0300 | [diff] [blame] | 393 | kfree(adap); |
Mike Isely | 891d99e | 2008-02-09 15:44:30 -0300 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | static 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; |
Mike Isely | c5317b1 | 2008-02-09 19:47:07 -0300 | [diff] [blame] | 401 | pvr2_dvb_destroy(adap); |
Mike Isely | 891d99e | 2008-02-09 15:44:30 -0300 | [diff] [blame] | 402 | } |
| 403 | |
Mike Isely | c5317b1 | 2008-02-09 19:47:07 -0300 | [diff] [blame] | 404 | struct pvr2_dvb_adapter *pvr2_dvb_create(struct pvr2_context *pvr) |
Michael Krufky | 04910bd | 2008-02-03 23:46:16 -0300 | [diff] [blame] | 405 | { |
| 406 | int ret = 0; |
Mike Isely | a36416d | 2008-02-06 06:59:48 -0300 | [diff] [blame] | 407 | struct pvr2_dvb_adapter *adap; |
Mike Isely | 129a2f5 | 2008-02-09 16:29:52 -0300 | [diff] [blame] | 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. */ |
Mike Isely | e6d1186 | 2008-02-09 19:47:52 -0300 | [diff] [blame] | 411 | return NULL; |
Mike Isely | 129a2f5 | 2008-02-09 16:29:52 -0300 | [diff] [blame] | 412 | } |
Mike Isely | c5317b1 | 2008-02-09 19:47:07 -0300 | [diff] [blame] | 413 | adap = kzalloc(sizeof(*adap), GFP_KERNEL); |
| 414 | if (!adap) return adap; |
Mike Isely | a36416d | 2008-02-06 06:59:48 -0300 | [diff] [blame] | 415 | pvr2_channel_init(&adap->channel, pvr); |
Mike Isely | 891d99e | 2008-02-09 15:44:30 -0300 | [diff] [blame] | 416 | adap->channel.check_func = pvr2_dvb_internal_check; |
Mike Isely | a36416d | 2008-02-06 06:59:48 -0300 | [diff] [blame] | 417 | init_waitqueue_head(&adap->buffer_wait_data); |
Mike Isely | c5317b1 | 2008-02-09 19:47:07 -0300 | [diff] [blame] | 418 | mutex_init(&adap->lock); |
| 419 | ret = pvr2_dvb_adapter_init(adap); |
Mike Isely | 129a2f5 | 2008-02-09 16:29:52 -0300 | [diff] [blame] | 420 | if (ret < 0) goto fail1; |
Mike Isely | c5317b1 | 2008-02-09 19:47:07 -0300 | [diff] [blame] | 421 | ret = pvr2_dvb_frontend_init(adap); |
Mike Isely | 129a2f5 | 2008-02-09 16:29:52 -0300 | [diff] [blame] | 422 | if (ret < 0) goto fail2; |
Mike Isely | c5317b1 | 2008-02-09 19:47:07 -0300 | [diff] [blame] | 423 | return adap; |
Mike Isely | 129a2f5 | 2008-02-09 16:29:52 -0300 | [diff] [blame] | 424 | |
| 425 | fail2: |
| 426 | pvr2_dvb_adapter_exit(adap); |
| 427 | fail1: |
Mike Isely | 891d99e | 2008-02-09 15:44:30 -0300 | [diff] [blame] | 428 | pvr2_channel_done(&adap->channel); |
Mike Isely | c5317b1 | 2008-02-09 19:47:07 -0300 | [diff] [blame] | 429 | return NULL; |
Michael Krufky | 04910bd | 2008-02-03 23:46:16 -0300 | [diff] [blame] | 430 | } |
| 431 | |