1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * budget-core.ko: base-driver for the SAA7146 based Budget DVB cards 4 * 5 * Compiled from various sources by Michael Hunold <michael@mihu.de> 6 * 7 * Copyright (C) 2002 Ralph Metzler <rjkm@metzlerbros.de> 8 * 9 * Copyright (C) 1999-2002 Ralph Metzler 10 * & Marcus Metzler for convergence integrated media GmbH 11 * 12 * 26feb2004 Support for FS Activy Card (Grundig tuner) by 13 * Michael Dreher <michael@5dot1.de>, 14 * Oliver Endriss <o.endriss@gmx.de>, 15 * Andreas 'randy' Weinberger 16 * 17 * the project's page is at https://linuxtv.org 18 */ 19 20 21 #include "budget.h" 22 #include "ttpci-eeprom.h" 23 24 #define TS_WIDTH (2 * TS_SIZE) 25 #define TS_WIDTH_ACTIVY TS_SIZE 26 #define TS_WIDTH_DVBC TS_SIZE 27 #define TS_HEIGHT_MASK 0xf00 28 #define TS_HEIGHT_MASK_ACTIVY 0xc00 29 #define TS_HEIGHT_MASK_DVBC 0xe00 30 #define TS_MIN_BUFSIZE_K 188 31 #define TS_MAX_BUFSIZE_K 1410 32 #define TS_MAX_BUFSIZE_K_ACTIVY 564 33 #define TS_MAX_BUFSIZE_K_DVBC 1316 34 #define BUFFER_WARNING_WAIT (30*HZ) 35 36 int budget_debug; 37 EXPORT_SYMBOL_GPL(budget_debug); 38 static int dma_buffer_size = TS_MIN_BUFSIZE_K; 39 module_param_named(debug, budget_debug, int, 0644); 40 module_param_named(bufsize, dma_buffer_size, int, 0444); 41 MODULE_PARM_DESC(debug, "Turn on/off budget debugging (default:off)."); 42 MODULE_PARM_DESC(bufsize, "DMA buffer size in KB, default: 188, min: 188, max: 1410 (Activy: 564)"); 43 44 /**************************************************************************** 45 * TT budget / WinTV Nova 46 ****************************************************************************/ 47 48 static int stop_ts_capture(struct budget *budget) 49 { 50 dprintk(2, "budget: %p\n", budget); 51 52 saa7146_write(budget->dev, MC1, MASK_20); // DMA3 off 53 SAA7146_IER_DISABLE(budget->dev, MASK_10); 54 return 0; 55 } 56 57 static int start_ts_capture(struct budget *budget) 58 { 59 struct saa7146_dev *dev = budget->dev; 60 61 dprintk(2, "budget: %p\n", budget); 62 63 if (!budget->feeding || !budget->fe_synced) 64 return 0; 65 66 saa7146_write(dev, MC1, MASK_20); // DMA3 off 67 68 memset(budget->grabbing, 0x00, budget->buffer_size); 69 70 saa7146_write(dev, PCI_BT_V1, 0x001c0000 | (saa7146_read(dev, PCI_BT_V1) & ~0x001f0000)); 71 72 budget->ttbp = 0; 73 74 /* 75 * Signal path on the Activy: 76 * 77 * tuner -> SAA7146 port A -> SAA7146 BRS -> SAA7146 DMA3 -> memory 78 * 79 * Since the tuner feeds 204 bytes packets into the SAA7146, 80 * DMA3 is configured to strip the trailing 16 FEC bytes: 81 * Pitch: 188, NumBytes3: 188, NumLines3: 1024 82 */ 83 84 switch (budget->card->type) { 85 case BUDGET_FS_ACTIVY: 86 saa7146_write(dev, DD1_INIT, 0x04000000); 87 saa7146_write(dev, MC2, (MASK_09 | MASK_25)); 88 saa7146_write(dev, BRS_CTRL, 0x00000000); 89 break; 90 case BUDGET_PATCH: 91 saa7146_write(dev, DD1_INIT, 0x00000200); 92 saa7146_write(dev, MC2, (MASK_10 | MASK_26)); 93 saa7146_write(dev, BRS_CTRL, 0x60000000); 94 break; 95 case BUDGET_CIN1200C_MK3: 96 case BUDGET_KNC1C_MK3: 97 case BUDGET_KNC1C_TDA10024: 98 case BUDGET_KNC1CP_MK3: 99 if (budget->video_port == BUDGET_VIDEO_PORTA) { 100 saa7146_write(dev, DD1_INIT, 0x06000200); 101 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26)); 102 saa7146_write(dev, BRS_CTRL, 0x00000000); 103 } else { 104 saa7146_write(dev, DD1_INIT, 0x00000600); 105 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26)); 106 saa7146_write(dev, BRS_CTRL, 0x60000000); 107 } 108 break; 109 default: 110 if (budget->video_port == BUDGET_VIDEO_PORTA) { 111 saa7146_write(dev, DD1_INIT, 0x06000200); 112 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26)); 113 saa7146_write(dev, BRS_CTRL, 0x00000000); 114 } else { 115 saa7146_write(dev, DD1_INIT, 0x02000600); 116 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26)); 117 saa7146_write(dev, BRS_CTRL, 0x60000000); 118 } 119 } 120 121 saa7146_write(dev, MC2, (MASK_08 | MASK_24)); 122 mdelay(10); 123 124 saa7146_write(dev, BASE_ODD3, 0); 125 if (budget->buffer_size > budget->buffer_height * budget->buffer_width) { 126 // using odd/even buffers 127 saa7146_write(dev, BASE_EVEN3, budget->buffer_height * budget->buffer_width); 128 } else { 129 // using a single buffer 130 saa7146_write(dev, BASE_EVEN3, 0); 131 } 132 saa7146_write(dev, PROT_ADDR3, budget->buffer_size); 133 saa7146_write(dev, BASE_PAGE3, budget->pt.dma | ME1 | 0x90); 134 135 saa7146_write(dev, PITCH3, budget->buffer_width); 136 saa7146_write(dev, NUM_LINE_BYTE3, 137 (budget->buffer_height << 16) | budget->buffer_width); 138 139 saa7146_write(dev, MC2, (MASK_04 | MASK_20)); 140 141 SAA7146_ISR_CLEAR(budget->dev, MASK_10); /* VPE */ 142 SAA7146_IER_ENABLE(budget->dev, MASK_10); /* VPE */ 143 saa7146_write(dev, MC1, (MASK_04 | MASK_20)); /* DMA3 on */ 144 145 return 0; 146 } 147 148 static int budget_read_fe_status(struct dvb_frontend *fe, 149 enum fe_status *status) 150 { 151 struct budget *budget = fe->dvb->priv; 152 int synced; 153 int ret; 154 155 if (budget->read_fe_status) 156 ret = budget->read_fe_status(fe, status); 157 else 158 ret = -EINVAL; 159 160 if (!ret) { 161 synced = (*status & FE_HAS_LOCK); 162 if (synced != budget->fe_synced) { 163 budget->fe_synced = synced; 164 spin_lock(&budget->feedlock); 165 if (synced) 166 start_ts_capture(budget); 167 else 168 stop_ts_capture(budget); 169 spin_unlock(&budget->feedlock); 170 } 171 } 172 return ret; 173 } 174 175 static void vpeirq(struct work_struct *t) 176 { 177 struct budget *budget = from_work(budget, t, vpe_bh_work); 178 u8 *mem = (u8 *) (budget->grabbing); 179 u32 olddma = budget->ttbp; 180 u32 newdma = saa7146_read(budget->dev, PCI_VDP3); 181 u32 count; 182 183 /* Ensure streamed PCI data is synced to CPU */ 184 dma_sync_sg_for_cpu(&budget->dev->pci->dev, budget->pt.slist, 185 budget->pt.nents, DMA_FROM_DEVICE); 186 187 /* nearest lower position divisible by 188 */ 188 newdma -= newdma % 188; 189 190 if (newdma >= budget->buffer_size) 191 return; 192 193 budget->ttbp = newdma; 194 195 if (budget->feeding == 0 || newdma == olddma) 196 return; 197 198 if (newdma > olddma) { /* no wraparound, dump olddma..newdma */ 199 count = newdma - olddma; 200 dvb_dmx_swfilter_packets(&budget->demux, mem + olddma, count / 188); 201 } else { /* wraparound, dump olddma..buflen and 0..newdma */ 202 count = budget->buffer_size - olddma; 203 dvb_dmx_swfilter_packets(&budget->demux, mem + olddma, count / 188); 204 count += newdma; 205 dvb_dmx_swfilter_packets(&budget->demux, mem, newdma / 188); 206 } 207 208 if (count > budget->buffer_warning_threshold) 209 budget->buffer_warnings++; 210 211 if (budget->buffer_warnings && time_after(jiffies, budget->buffer_warning_time)) { 212 pr_warn("%s %s: used %d times >80%% of buffer (%u bytes now)\n", 213 budget->dev->name, __func__, budget->buffer_warnings, count); 214 budget->buffer_warning_time = jiffies + BUFFER_WARNING_WAIT; 215 budget->buffer_warnings = 0; 216 } 217 } 218 219 220 static int ttpci_budget_debiread_nolock(struct budget *budget, u32 config, 221 int addr, int count, int nobusyloop) 222 { 223 struct saa7146_dev *saa = budget->dev; 224 int result; 225 226 result = saa7146_wait_for_debi_done(saa, nobusyloop); 227 if (result < 0) 228 return result; 229 230 saa7146_write(saa, DEBI_COMMAND, (count << 17) | 0x10000 | (addr & 0xffff)); 231 saa7146_write(saa, DEBI_CONFIG, config); 232 saa7146_write(saa, DEBI_PAGE, 0); 233 saa7146_write(saa, MC2, (2 << 16) | 2); 234 235 result = saa7146_wait_for_debi_done(saa, nobusyloop); 236 if (result < 0) 237 return result; 238 239 result = saa7146_read(saa, DEBI_AD); 240 result &= (0xffffffffUL >> ((4 - count) * 8)); 241 return result; 242 } 243 244 int ttpci_budget_debiread(struct budget *budget, u32 config, int addr, int count, 245 int uselocks, int nobusyloop) 246 { 247 if (count > 4 || count <= 0) 248 return 0; 249 250 if (uselocks) { 251 unsigned long flags; 252 int result; 253 254 spin_lock_irqsave(&budget->debilock, flags); 255 result = ttpci_budget_debiread_nolock(budget, config, addr, 256 count, nobusyloop); 257 spin_unlock_irqrestore(&budget->debilock, flags); 258 return result; 259 } 260 return ttpci_budget_debiread_nolock(budget, config, addr, 261 count, nobusyloop); 262 } 263 EXPORT_SYMBOL_GPL(ttpci_budget_debiread); 264 265 static int ttpci_budget_debiwrite_nolock(struct budget *budget, u32 config, 266 int addr, int count, u32 value, int nobusyloop) 267 { 268 struct saa7146_dev *saa = budget->dev; 269 int result; 270 271 result = saa7146_wait_for_debi_done(saa, nobusyloop); 272 if (result < 0) 273 return result; 274 275 saa7146_write(saa, DEBI_COMMAND, (count << 17) | 0x00000 | (addr & 0xffff)); 276 saa7146_write(saa, DEBI_CONFIG, config); 277 saa7146_write(saa, DEBI_PAGE, 0); 278 saa7146_write(saa, DEBI_AD, value); 279 saa7146_write(saa, MC2, (2 << 16) | 2); 280 281 result = saa7146_wait_for_debi_done(saa, nobusyloop); 282 return result < 0 ? result : 0; 283 } 284 285 int ttpci_budget_debiwrite(struct budget *budget, u32 config, int addr, 286 int count, u32 value, int uselocks, int nobusyloop) 287 { 288 if (count > 4 || count <= 0) 289 return 0; 290 291 if (uselocks) { 292 unsigned long flags; 293 int result; 294 295 spin_lock_irqsave(&budget->debilock, flags); 296 result = ttpci_budget_debiwrite_nolock(budget, config, addr, 297 count, value, nobusyloop); 298 spin_unlock_irqrestore(&budget->debilock, flags); 299 return result; 300 } 301 return ttpci_budget_debiwrite_nolock(budget, config, addr, 302 count, value, nobusyloop); 303 } 304 EXPORT_SYMBOL_GPL(ttpci_budget_debiwrite); 305 306 307 /**************************************************************************** 308 * DVB API SECTION 309 ****************************************************************************/ 310 311 static int budget_start_feed(struct dvb_demux_feed *feed) 312 { 313 struct dvb_demux *demux = feed->demux; 314 struct budget *budget = demux->priv; 315 int status = 0; 316 317 dprintk(2, "budget: %p\n", budget); 318 319 if (!demux->dmx.frontend) 320 return -EINVAL; 321 322 spin_lock(&budget->feedlock); 323 feed->pusi_seen = false; /* have a clean section start */ 324 if (budget->feeding++ == 0) 325 status = start_ts_capture(budget); 326 spin_unlock(&budget->feedlock); 327 return status; 328 } 329 330 static int budget_stop_feed(struct dvb_demux_feed *feed) 331 { 332 struct dvb_demux *demux = feed->demux; 333 struct budget *budget = demux->priv; 334 int status = 0; 335 336 dprintk(2, "budget: %p\n", budget); 337 338 spin_lock(&budget->feedlock); 339 if (--budget->feeding == 0) 340 status = stop_ts_capture(budget); 341 spin_unlock(&budget->feedlock); 342 return status; 343 } 344 345 static int budget_register(struct budget *budget) 346 { 347 struct dvb_demux *dvbdemux = &budget->demux; 348 int ret; 349 350 dprintk(2, "budget: %p\n", budget); 351 352 dvbdemux->priv = (void *) budget; 353 354 dvbdemux->filternum = 256; 355 dvbdemux->feednum = 256; 356 dvbdemux->start_feed = budget_start_feed; 357 dvbdemux->stop_feed = budget_stop_feed; 358 dvbdemux->write_to_decoder = NULL; 359 360 dvbdemux->dmx.capabilities = (DMX_TS_FILTERING | DMX_SECTION_FILTERING | 361 DMX_MEMORY_BASED_FILTERING); 362 363 dvb_dmx_init(&budget->demux); 364 365 budget->dmxdev.filternum = 256; 366 budget->dmxdev.demux = &dvbdemux->dmx; 367 budget->dmxdev.capabilities = 0; 368 369 dvb_dmxdev_init(&budget->dmxdev, &budget->dvb_adapter); 370 371 budget->hw_frontend.source = DMX_FRONTEND_0; 372 373 ret = dvbdemux->dmx.add_frontend(&dvbdemux->dmx, &budget->hw_frontend); 374 375 if (ret < 0) 376 goto err_release_dmx; 377 378 budget->mem_frontend.source = DMX_MEMORY_FE; 379 ret = dvbdemux->dmx.add_frontend(&dvbdemux->dmx, &budget->mem_frontend); 380 if (ret < 0) 381 goto err_release_dmx; 382 383 ret = dvbdemux->dmx.connect_frontend(&dvbdemux->dmx, &budget->hw_frontend); 384 if (ret < 0) 385 goto err_release_dmx; 386 387 dvb_net_init(&budget->dvb_adapter, &budget->dvb_net, &dvbdemux->dmx); 388 389 return 0; 390 391 err_release_dmx: 392 dvb_dmxdev_release(&budget->dmxdev); 393 dvb_dmx_release(&budget->demux); 394 return ret; 395 } 396 397 static void budget_unregister(struct budget *budget) 398 { 399 struct dvb_demux *dvbdemux = &budget->demux; 400 401 dprintk(2, "budget: %p\n", budget); 402 403 dvb_net_release(&budget->dvb_net); 404 405 dvbdemux->dmx.close(&dvbdemux->dmx); 406 dvbdemux->dmx.remove_frontend(&dvbdemux->dmx, &budget->hw_frontend); 407 dvbdemux->dmx.remove_frontend(&dvbdemux->dmx, &budget->mem_frontend); 408 409 dvb_dmxdev_release(&budget->dmxdev); 410 dvb_dmx_release(&budget->demux); 411 } 412 413 int ttpci_budget_init(struct budget *budget, struct saa7146_dev *dev, 414 struct saa7146_pci_extension_data *info, 415 struct module *owner, short *adapter_nums) 416 { 417 int ret = 0; 418 struct budget_info *bi = info->ext_priv; 419 int max_bufsize; 420 int height_mask; 421 422 memset(budget, 0, sizeof(struct budget)); 423 424 dprintk(2, "dev: %p, budget: %p\n", dev, budget); 425 426 budget->card = bi; 427 budget->dev = (struct saa7146_dev *) dev; 428 429 switch (budget->card->type) { 430 case BUDGET_FS_ACTIVY: 431 budget->buffer_width = TS_WIDTH_ACTIVY; 432 max_bufsize = TS_MAX_BUFSIZE_K_ACTIVY; 433 height_mask = TS_HEIGHT_MASK_ACTIVY; 434 break; 435 436 case BUDGET_KNC1C: 437 case BUDGET_KNC1CP: 438 case BUDGET_CIN1200C: 439 case BUDGET_KNC1C_MK3: 440 case BUDGET_KNC1C_TDA10024: 441 case BUDGET_KNC1CP_MK3: 442 case BUDGET_CIN1200C_MK3: 443 budget->buffer_width = TS_WIDTH_DVBC; 444 max_bufsize = TS_MAX_BUFSIZE_K_DVBC; 445 height_mask = TS_HEIGHT_MASK_DVBC; 446 break; 447 448 default: 449 budget->buffer_width = TS_WIDTH; 450 max_bufsize = TS_MAX_BUFSIZE_K; 451 height_mask = TS_HEIGHT_MASK; 452 } 453 454 if (dma_buffer_size < TS_MIN_BUFSIZE_K) 455 dma_buffer_size = TS_MIN_BUFSIZE_K; 456 else if (dma_buffer_size > max_bufsize) 457 dma_buffer_size = max_bufsize; 458 459 budget->buffer_height = dma_buffer_size * 1024 / budget->buffer_width; 460 if (budget->buffer_height > 0xfff) { 461 budget->buffer_height /= 2; 462 budget->buffer_height &= height_mask; 463 budget->buffer_size = 2 * budget->buffer_height * budget->buffer_width; 464 } else { 465 budget->buffer_height &= height_mask; 466 budget->buffer_size = budget->buffer_height * budget->buffer_width; 467 } 468 budget->buffer_warning_threshold = budget->buffer_size * 80/100; 469 budget->buffer_warnings = 0; 470 budget->buffer_warning_time = jiffies; 471 472 dprintk(2, "%s: buffer type = %s, width = %d, height = %d\n", 473 budget->dev->name, 474 budget->buffer_size > budget->buffer_width * budget->buffer_height ? "odd/even" : "single", 475 budget->buffer_width, budget->buffer_height); 476 pr_info("%s: dma buffer size %u\n", budget->dev->name, budget->buffer_size); 477 478 ret = dvb_register_adapter(&budget->dvb_adapter, budget->card->name, 479 owner, &budget->dev->pci->dev, adapter_nums); 480 if (ret < 0) 481 return ret; 482 483 /* set dd1 stream a & b */ 484 saa7146_write(dev, DD1_STREAM_B, 0x00000000); 485 saa7146_write(dev, MC2, (MASK_09 | MASK_25)); 486 saa7146_write(dev, MC2, (MASK_10 | MASK_26)); 487 saa7146_write(dev, DD1_INIT, 0x02000000); 488 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26)); 489 490 if (bi->type != BUDGET_FS_ACTIVY) 491 budget->video_port = BUDGET_VIDEO_PORTB; 492 else 493 budget->video_port = BUDGET_VIDEO_PORTA; 494 spin_lock_init(&budget->feedlock); 495 spin_lock_init(&budget->debilock); 496 497 /* 498 * the Siemens DVB needs this if you want to have the i2c chips 499 * get recognized before the main driver is loaded 500 */ 501 if (bi->type != BUDGET_FS_ACTIVY) 502 saa7146_write(dev, GPIO_CTRL, 0x500000); /* GPIO 3 = 1 */ 503 504 strscpy(budget->i2c_adap.name, budget->card->name, 505 sizeof(budget->i2c_adap.name)); 506 507 saa7146_i2c_adapter_prepare(dev, &budget->i2c_adap, SAA7146_I2C_BUS_BIT_RATE_120); 508 strscpy(budget->i2c_adap.name, budget->card->name, 509 sizeof(budget->i2c_adap.name)); 510 511 if (i2c_add_adapter(&budget->i2c_adap) < 0) { 512 ret = -ENOMEM; 513 goto err_dvb_unregister; 514 } 515 516 ttpci_eeprom_parse_mac(&budget->i2c_adap, budget->dvb_adapter.proposed_mac); 517 518 budget->grabbing = saa7146_vmalloc_build_pgtable(dev->pci, budget->buffer_size, &budget->pt); 519 if (budget->grabbing == NULL) { 520 ret = -ENOMEM; 521 goto err_del_i2c; 522 } 523 524 saa7146_write(dev, PCI_BT_V1, 0x001c0000); 525 /* upload all */ 526 saa7146_write(dev, GPIO_CTRL, 0x000000); 527 528 INIT_WORK(&budget->vpe_bh_work, vpeirq); 529 530 /* frontend power on */ 531 if (bi->type != BUDGET_FS_ACTIVY) 532 saa7146_setgpio(dev, 2, SAA7146_GPIO_OUTHI); 533 534 ret = budget_register(budget); 535 if (ret == 0) 536 return 0; /* Everything OK */ 537 538 /* An error occurred, cleanup resources */ 539 saa7146_vfree_destroy_pgtable(dev->pci, budget->grabbing, &budget->pt); 540 541 err_del_i2c: 542 i2c_del_adapter(&budget->i2c_adap); 543 544 err_dvb_unregister: 545 dvb_unregister_adapter(&budget->dvb_adapter); 546 547 return ret; 548 } 549 EXPORT_SYMBOL_GPL(ttpci_budget_init); 550 551 void ttpci_budget_init_hooks(struct budget *budget) 552 { 553 if (budget->dvb_frontend && !budget->read_fe_status) { 554 budget->read_fe_status = budget->dvb_frontend->ops.read_status; 555 budget->dvb_frontend->ops.read_status = budget_read_fe_status; 556 } 557 } 558 EXPORT_SYMBOL_GPL(ttpci_budget_init_hooks); 559 560 int ttpci_budget_deinit(struct budget *budget) 561 { 562 struct saa7146_dev *dev = budget->dev; 563 564 dprintk(2, "budget: %p\n", budget); 565 566 budget_unregister(budget); 567 568 cancel_work_sync(&budget->vpe_bh_work); 569 570 saa7146_vfree_destroy_pgtable(dev->pci, budget->grabbing, &budget->pt); 571 572 i2c_del_adapter(&budget->i2c_adap); 573 574 dvb_unregister_adapter(&budget->dvb_adapter); 575 576 return 0; 577 } 578 EXPORT_SYMBOL_GPL(ttpci_budget_deinit); 579 580 void ttpci_budget_irq10_handler(struct saa7146_dev *dev, u32 *isr) 581 { 582 struct budget *budget = dev->ext_priv; 583 584 dprintk(8, "dev: %p, budget: %p\n", dev, budget); 585 586 if (*isr & MASK_10) 587 queue_work(system_bh_wq, &budget->vpe_bh_work); 588 } 589 EXPORT_SYMBOL_GPL(ttpci_budget_irq10_handler); 590 591 void ttpci_budget_set_video_port(struct saa7146_dev *dev, int video_port) 592 { 593 struct budget *budget = dev->ext_priv; 594 595 spin_lock(&budget->feedlock); 596 budget->video_port = video_port; 597 if (budget->feeding) { 598 stop_ts_capture(budget); 599 start_ts_capture(budget); 600 } 601 spin_unlock(&budget->feedlock); 602 } 603 EXPORT_SYMBOL_GPL(ttpci_budget_set_video_port); 604 605 MODULE_DESCRIPTION("base driver for the SAA7146 based Budget DVB cards"); 606 MODULE_LICENSE("GPL"); 607