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