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