1 // SPDX-License-Identifier: LGPL-2.1-or-later 2 /* 3 * dvbdev.c 4 * 5 * Copyright (C) 2000 Ralph Metzler <ralph@convergence.de> 6 * & Marcus Metzler <marcus@convergence.de> 7 * for convergence integrated media GmbH 8 */ 9 10 #define pr_fmt(fmt) "dvbdev: " fmt 11 12 #include <linux/types.h> 13 #include <linux/errno.h> 14 #include <linux/string.h> 15 #include <linux/module.h> 16 #include <linux/kernel.h> 17 #include <linux/i2c.h> 18 #include <linux/init.h> 19 #include <linux/slab.h> 20 #include <linux/device.h> 21 #include <linux/fs.h> 22 #include <linux/cdev.h> 23 #include <linux/mutex.h> 24 #include <media/dvbdev.h> 25 26 /* Due to enum tuner_pad_index */ 27 #include <media/tuner.h> 28 29 static DEFINE_MUTEX(dvbdev_mutex); 30 static LIST_HEAD(dvbdevfops_list); 31 static int dvbdev_debug; 32 33 module_param(dvbdev_debug, int, 0644); 34 MODULE_PARM_DESC(dvbdev_debug, "Turn on/off device debugging (default:off)."); 35 36 #define dprintk(fmt, arg...) do { \ 37 if (dvbdev_debug) \ 38 printk(KERN_DEBUG pr_fmt("%s: " fmt), \ 39 __func__, ##arg); \ 40 } while (0) 41 42 static LIST_HEAD(dvb_adapter_list); 43 static DEFINE_MUTEX(dvbdev_register_lock); 44 45 static const char * const dnames[] = { 46 [DVB_DEVICE_VIDEO] = "video", 47 [DVB_DEVICE_AUDIO] = "audio", 48 [DVB_DEVICE_SEC] = "sec", 49 [DVB_DEVICE_FRONTEND] = "frontend", 50 [DVB_DEVICE_DEMUX] = "demux", 51 [DVB_DEVICE_DVR] = "dvr", 52 [DVB_DEVICE_CA] = "ca", 53 [DVB_DEVICE_NET] = "net", 54 [DVB_DEVICE_OSD] = "osd" 55 }; 56 57 #ifdef CONFIG_DVB_DYNAMIC_MINORS 58 #define MAX_DVB_MINORS 256 59 #define DVB_MAX_IDS MAX_DVB_MINORS 60 #else 61 #define DVB_MAX_IDS 4 62 63 static const u8 minor_type[] = { 64 [DVB_DEVICE_VIDEO] = 0, 65 [DVB_DEVICE_AUDIO] = 1, 66 [DVB_DEVICE_SEC] = 2, 67 [DVB_DEVICE_FRONTEND] = 3, 68 [DVB_DEVICE_DEMUX] = 4, 69 [DVB_DEVICE_DVR] = 5, 70 [DVB_DEVICE_CA] = 6, 71 [DVB_DEVICE_NET] = 7, 72 [DVB_DEVICE_OSD] = 8, 73 }; 74 75 #define nums2minor(num, type, id) \ 76 (((num) << 6) | ((id) << 4) | minor_type[type]) 77 78 #define MAX_DVB_MINORS (DVB_MAX_ADAPTERS*64) 79 #endif 80 81 static struct class *dvb_class; 82 83 static struct dvb_device *dvb_minors[MAX_DVB_MINORS]; 84 static DECLARE_RWSEM(minor_rwsem); 85 86 static int dvb_device_open(struct inode *inode, struct file *file) 87 { 88 struct dvb_device *dvbdev; 89 90 mutex_lock(&dvbdev_mutex); 91 down_read(&minor_rwsem); 92 dvbdev = dvb_minors[iminor(inode)]; 93 94 if (dvbdev && dvbdev->fops) { 95 int err = 0; 96 const struct file_operations *new_fops; 97 98 new_fops = fops_get(dvbdev->fops); 99 if (!new_fops) 100 goto fail; 101 file->private_data = dvb_device_get(dvbdev); 102 replace_fops(file, new_fops); 103 if (file->f_op->open) 104 err = file->f_op->open(inode, file); 105 up_read(&minor_rwsem); 106 mutex_unlock(&dvbdev_mutex); 107 return err; 108 } 109 fail: 110 up_read(&minor_rwsem); 111 mutex_unlock(&dvbdev_mutex); 112 return -ENODEV; 113 } 114 115 116 static const struct file_operations dvb_device_fops = 117 { 118 .owner = THIS_MODULE, 119 .open = dvb_device_open, 120 .llseek = noop_llseek, 121 }; 122 123 static struct cdev dvb_device_cdev; 124 125 int dvb_generic_open(struct inode *inode, struct file *file) 126 { 127 struct dvb_device *dvbdev = file->private_data; 128 129 if (!dvbdev) 130 return -ENODEV; 131 132 if (!dvbdev->users) 133 return -EBUSY; 134 135 if ((file->f_flags & O_ACCMODE) == O_RDONLY) { 136 if (!dvbdev->readers) 137 return -EBUSY; 138 dvbdev->readers--; 139 } else { 140 if (!dvbdev->writers) 141 return -EBUSY; 142 dvbdev->writers--; 143 } 144 145 dvbdev->users--; 146 return 0; 147 } 148 EXPORT_SYMBOL(dvb_generic_open); 149 150 151 int dvb_generic_release(struct inode *inode, struct file *file) 152 { 153 struct dvb_device *dvbdev = file->private_data; 154 155 if (!dvbdev) 156 return -ENODEV; 157 158 if ((file->f_flags & O_ACCMODE) == O_RDONLY) { 159 dvbdev->readers++; 160 } else { 161 dvbdev->writers++; 162 } 163 164 dvbdev->users++; 165 166 dvb_device_put(dvbdev); 167 168 return 0; 169 } 170 EXPORT_SYMBOL(dvb_generic_release); 171 172 173 long dvb_generic_ioctl(struct file *file, 174 unsigned int cmd, unsigned long arg) 175 { 176 struct dvb_device *dvbdev = file->private_data; 177 178 if (!dvbdev) 179 return -ENODEV; 180 181 if (!dvbdev->kernel_ioctl) 182 return -EINVAL; 183 184 return dvb_usercopy(file, cmd, arg, dvbdev->kernel_ioctl); 185 } 186 EXPORT_SYMBOL(dvb_generic_ioctl); 187 188 189 static int dvbdev_get_free_id (struct dvb_adapter *adap, int type) 190 { 191 u32 id = 0; 192 193 while (id < DVB_MAX_IDS) { 194 struct dvb_device *dev; 195 list_for_each_entry(dev, &adap->device_list, list_head) 196 if (dev->type == type && dev->id == id) 197 goto skip; 198 return id; 199 skip: 200 id++; 201 } 202 return -ENFILE; 203 } 204 205 static void dvb_media_device_free(struct dvb_device *dvbdev) 206 { 207 #if defined(CONFIG_MEDIA_CONTROLLER_DVB) 208 if (dvbdev->entity) { 209 media_device_unregister_entity(dvbdev->entity); 210 kfree(dvbdev->entity); 211 kfree(dvbdev->pads); 212 dvbdev->entity = NULL; 213 dvbdev->pads = NULL; 214 } 215 216 if (dvbdev->tsout_entity) { 217 int i; 218 219 for (i = 0; i < dvbdev->tsout_num_entities; i++) { 220 media_device_unregister_entity(&dvbdev->tsout_entity[i]); 221 kfree(dvbdev->tsout_entity[i].name); 222 } 223 kfree(dvbdev->tsout_entity); 224 kfree(dvbdev->tsout_pads); 225 dvbdev->tsout_entity = NULL; 226 dvbdev->tsout_pads = NULL; 227 228 dvbdev->tsout_num_entities = 0; 229 } 230 231 if (dvbdev->intf_devnode) { 232 media_devnode_remove(dvbdev->intf_devnode); 233 dvbdev->intf_devnode = NULL; 234 } 235 236 if (dvbdev->adapter->conn) { 237 media_device_unregister_entity(dvbdev->adapter->conn); 238 kfree(dvbdev->adapter->conn); 239 dvbdev->adapter->conn = NULL; 240 kfree(dvbdev->adapter->conn_pads); 241 dvbdev->adapter->conn_pads = NULL; 242 } 243 #endif 244 } 245 246 #if defined(CONFIG_MEDIA_CONTROLLER_DVB) 247 static int dvb_create_tsout_entity(struct dvb_device *dvbdev, 248 const char *name, int npads) 249 { 250 int i; 251 252 dvbdev->tsout_pads = kcalloc(npads, sizeof(*dvbdev->tsout_pads), 253 GFP_KERNEL); 254 if (!dvbdev->tsout_pads) 255 return -ENOMEM; 256 257 dvbdev->tsout_entity = kcalloc(npads, sizeof(*dvbdev->tsout_entity), 258 GFP_KERNEL); 259 if (!dvbdev->tsout_entity) 260 return -ENOMEM; 261 262 dvbdev->tsout_num_entities = npads; 263 264 for (i = 0; i < npads; i++) { 265 struct media_pad *pads = &dvbdev->tsout_pads[i]; 266 struct media_entity *entity = &dvbdev->tsout_entity[i]; 267 int ret; 268 269 entity->name = kasprintf(GFP_KERNEL, "%s #%d", name, i); 270 if (!entity->name) 271 return -ENOMEM; 272 273 entity->function = MEDIA_ENT_F_IO_DTV; 274 pads->flags = MEDIA_PAD_FL_SINK; 275 276 ret = media_entity_pads_init(entity, 1, pads); 277 if (ret < 0) 278 return ret; 279 280 ret = media_device_register_entity(dvbdev->adapter->mdev, 281 entity); 282 if (ret < 0) 283 return ret; 284 } 285 return 0; 286 } 287 288 #define DEMUX_TSOUT "demux-tsout" 289 #define DVR_TSOUT "dvr-tsout" 290 291 static int dvb_create_media_entity(struct dvb_device *dvbdev, 292 int type, int demux_sink_pads) 293 { 294 int i, ret, npads; 295 296 switch (type) { 297 case DVB_DEVICE_FRONTEND: 298 npads = 2; 299 break; 300 case DVB_DEVICE_DVR: 301 ret = dvb_create_tsout_entity(dvbdev, DVR_TSOUT, 302 demux_sink_pads); 303 return ret; 304 case DVB_DEVICE_DEMUX: 305 npads = 1 + demux_sink_pads; 306 ret = dvb_create_tsout_entity(dvbdev, DEMUX_TSOUT, 307 demux_sink_pads); 308 if (ret < 0) 309 return ret; 310 break; 311 case DVB_DEVICE_CA: 312 npads = 2; 313 break; 314 case DVB_DEVICE_NET: 315 /* 316 * We should be creating entities for the MPE/ULE 317 * decapsulation hardware (or software implementation). 318 * 319 * However, the number of for the MPE/ULE decaps may not be 320 * fixed. As we don't have yet dynamic support for PADs at 321 * the Media Controller, let's not create the decap 322 * entities yet. 323 */ 324 return 0; 325 default: 326 return 0; 327 } 328 329 dvbdev->entity = kzalloc(sizeof(*dvbdev->entity), GFP_KERNEL); 330 if (!dvbdev->entity) 331 return -ENOMEM; 332 333 dvbdev->entity->name = dvbdev->name; 334 335 if (npads) { 336 dvbdev->pads = kcalloc(npads, sizeof(*dvbdev->pads), 337 GFP_KERNEL); 338 if (!dvbdev->pads) { 339 kfree(dvbdev->entity); 340 dvbdev->entity = NULL; 341 return -ENOMEM; 342 } 343 } 344 345 switch (type) { 346 case DVB_DEVICE_FRONTEND: 347 dvbdev->entity->function = MEDIA_ENT_F_DTV_DEMOD; 348 dvbdev->pads[0].flags = MEDIA_PAD_FL_SINK; 349 dvbdev->pads[1].flags = MEDIA_PAD_FL_SOURCE; 350 break; 351 case DVB_DEVICE_DEMUX: 352 dvbdev->entity->function = MEDIA_ENT_F_TS_DEMUX; 353 dvbdev->pads[0].flags = MEDIA_PAD_FL_SINK; 354 for (i = 1; i < npads; i++) 355 dvbdev->pads[i].flags = MEDIA_PAD_FL_SOURCE; 356 break; 357 case DVB_DEVICE_CA: 358 dvbdev->entity->function = MEDIA_ENT_F_DTV_CA; 359 dvbdev->pads[0].flags = MEDIA_PAD_FL_SINK; 360 dvbdev->pads[1].flags = MEDIA_PAD_FL_SOURCE; 361 break; 362 default: 363 /* Should never happen, as the first switch prevents it */ 364 kfree(dvbdev->entity); 365 kfree(dvbdev->pads); 366 dvbdev->entity = NULL; 367 dvbdev->pads = NULL; 368 return 0; 369 } 370 371 if (npads) { 372 ret = media_entity_pads_init(dvbdev->entity, npads, dvbdev->pads); 373 if (ret) 374 return ret; 375 } 376 ret = media_device_register_entity(dvbdev->adapter->mdev, 377 dvbdev->entity); 378 if (ret) 379 return ret; 380 381 pr_info("%s: media entity '%s' registered.\n", 382 __func__, dvbdev->entity->name); 383 384 return 0; 385 } 386 #endif 387 388 static int dvb_register_media_device(struct dvb_device *dvbdev, 389 int type, int minor, 390 unsigned demux_sink_pads) 391 { 392 #if defined(CONFIG_MEDIA_CONTROLLER_DVB) 393 struct media_link *link; 394 u32 intf_type; 395 int ret; 396 397 if (!dvbdev->adapter->mdev) 398 return 0; 399 400 ret = dvb_create_media_entity(dvbdev, type, demux_sink_pads); 401 if (ret) 402 return ret; 403 404 switch (type) { 405 case DVB_DEVICE_FRONTEND: 406 intf_type = MEDIA_INTF_T_DVB_FE; 407 break; 408 case DVB_DEVICE_DEMUX: 409 intf_type = MEDIA_INTF_T_DVB_DEMUX; 410 break; 411 case DVB_DEVICE_DVR: 412 intf_type = MEDIA_INTF_T_DVB_DVR; 413 break; 414 case DVB_DEVICE_CA: 415 intf_type = MEDIA_INTF_T_DVB_CA; 416 break; 417 case DVB_DEVICE_NET: 418 intf_type = MEDIA_INTF_T_DVB_NET; 419 break; 420 default: 421 return 0; 422 } 423 424 dvbdev->intf_devnode = media_devnode_create(dvbdev->adapter->mdev, 425 intf_type, 0, 426 DVB_MAJOR, minor); 427 428 if (!dvbdev->intf_devnode) 429 return -ENOMEM; 430 431 /* 432 * Create the "obvious" link, e. g. the ones that represent 433 * a direct association between an interface and an entity. 434 * Other links should be created elsewhere, like: 435 * DVB FE intf -> tuner 436 * DVB demux intf -> dvr 437 */ 438 439 if (!dvbdev->entity) 440 return 0; 441 442 link = media_create_intf_link(dvbdev->entity, 443 &dvbdev->intf_devnode->intf, 444 MEDIA_LNK_FL_ENABLED | 445 MEDIA_LNK_FL_IMMUTABLE); 446 if (!link) 447 return -ENOMEM; 448 #endif 449 return 0; 450 } 451 452 int dvb_register_device(struct dvb_adapter *adap, struct dvb_device **pdvbdev, 453 const struct dvb_device *template, void *priv, 454 enum dvb_device_type type, int demux_sink_pads) 455 { 456 struct dvb_device *dvbdev; 457 struct file_operations *dvbdevfops = NULL; 458 struct dvbdevfops_node *node = NULL, *new_node = NULL; 459 struct device *clsdev; 460 int minor; 461 int id, ret; 462 463 mutex_lock(&dvbdev_register_lock); 464 465 if ((id = dvbdev_get_free_id (adap, type)) < 0) { 466 mutex_unlock(&dvbdev_register_lock); 467 *pdvbdev = NULL; 468 pr_err("%s: couldn't find free device id\n", __func__); 469 return -ENFILE; 470 } 471 472 *pdvbdev = dvbdev = kzalloc(sizeof(*dvbdev), GFP_KERNEL); 473 if (!dvbdev){ 474 mutex_unlock(&dvbdev_register_lock); 475 return -ENOMEM; 476 } 477 478 /* 479 * When a device of the same type is probe()d more than once, 480 * the first allocated fops are used. This prevents memory leaks 481 * that can occur when the same device is probe()d repeatedly. 482 */ 483 list_for_each_entry(node, &dvbdevfops_list, list_head) { 484 if (node->fops->owner == adap->module && 485 node->type == type && 486 node->template == template) { 487 dvbdevfops = node->fops; 488 break; 489 } 490 } 491 492 if (dvbdevfops == NULL) { 493 dvbdevfops = kmemdup(template->fops, sizeof(*dvbdevfops), GFP_KERNEL); 494 if (!dvbdevfops) { 495 kfree(dvbdev); 496 mutex_unlock(&dvbdev_register_lock); 497 return -ENOMEM; 498 } 499 500 new_node = kzalloc(sizeof(struct dvbdevfops_node), GFP_KERNEL); 501 if (!new_node) { 502 kfree(dvbdevfops); 503 kfree(dvbdev); 504 mutex_unlock(&dvbdev_register_lock); 505 return -ENOMEM; 506 } 507 508 new_node->fops = dvbdevfops; 509 new_node->type = type; 510 new_node->template = template; 511 list_add_tail (&new_node->list_head, &dvbdevfops_list); 512 } 513 514 memcpy(dvbdev, template, sizeof(struct dvb_device)); 515 kref_init(&dvbdev->ref); 516 dvbdev->type = type; 517 dvbdev->id = id; 518 dvbdev->adapter = adap; 519 dvbdev->priv = priv; 520 dvbdev->fops = dvbdevfops; 521 init_waitqueue_head (&dvbdev->wait_queue); 522 dvbdevfops->owner = adap->module; 523 list_add_tail (&dvbdev->list_head, &adap->device_list); 524 down_write(&minor_rwsem); 525 #ifdef CONFIG_DVB_DYNAMIC_MINORS 526 for (minor = 0; minor < MAX_DVB_MINORS; minor++) 527 if (dvb_minors[minor] == NULL) 528 break; 529 if (minor == MAX_DVB_MINORS) { 530 if (new_node) { 531 list_del (&new_node->list_head); 532 kfree(dvbdevfops); 533 kfree(new_node); 534 } 535 list_del (&dvbdev->list_head); 536 kfree(dvbdev); 537 up_write(&minor_rwsem); 538 mutex_unlock(&dvbdev_register_lock); 539 return -EINVAL; 540 } 541 #else 542 minor = nums2minor(adap->num, type, id); 543 #endif 544 dvbdev->minor = minor; 545 dvb_minors[minor] = dvb_device_get(dvbdev); 546 up_write(&minor_rwsem); 547 ret = dvb_register_media_device(dvbdev, type, minor, demux_sink_pads); 548 if (ret) { 549 pr_err("%s: dvb_register_media_device failed to create the mediagraph\n", 550 __func__); 551 if (new_node) { 552 list_del (&new_node->list_head); 553 kfree(dvbdevfops); 554 kfree(new_node); 555 } 556 dvb_media_device_free(dvbdev); 557 list_del (&dvbdev->list_head); 558 kfree(dvbdev); 559 mutex_unlock(&dvbdev_register_lock); 560 return ret; 561 } 562 563 clsdev = device_create(dvb_class, adap->device, 564 MKDEV(DVB_MAJOR, minor), 565 dvbdev, "dvb%d.%s%d", adap->num, dnames[type], id); 566 if (IS_ERR(clsdev)) { 567 pr_err("%s: failed to create device dvb%d.%s%d (%ld)\n", 568 __func__, adap->num, dnames[type], id, PTR_ERR(clsdev)); 569 if (new_node) { 570 list_del (&new_node->list_head); 571 kfree(dvbdevfops); 572 kfree(new_node); 573 } 574 dvb_media_device_free(dvbdev); 575 list_del (&dvbdev->list_head); 576 kfree(dvbdev); 577 mutex_unlock(&dvbdev_register_lock); 578 return PTR_ERR(clsdev); 579 } 580 581 dprintk("DVB: register adapter%d/%s%d @ minor: %i (0x%02x)\n", 582 adap->num, dnames[type], id, minor, minor); 583 584 mutex_unlock(&dvbdev_register_lock); 585 return 0; 586 } 587 EXPORT_SYMBOL(dvb_register_device); 588 589 590 void dvb_remove_device(struct dvb_device *dvbdev) 591 { 592 if (!dvbdev) 593 return; 594 595 down_write(&minor_rwsem); 596 dvb_minors[dvbdev->minor] = NULL; 597 dvb_device_put(dvbdev); 598 up_write(&minor_rwsem); 599 600 dvb_media_device_free(dvbdev); 601 602 device_destroy(dvb_class, MKDEV(DVB_MAJOR, dvbdev->minor)); 603 604 list_del (&dvbdev->list_head); 605 } 606 EXPORT_SYMBOL(dvb_remove_device); 607 608 609 static void dvb_free_device(struct kref *ref) 610 { 611 struct dvb_device *dvbdev = container_of(ref, struct dvb_device, ref); 612 613 kfree (dvbdev); 614 } 615 616 617 struct dvb_device *dvb_device_get(struct dvb_device *dvbdev) 618 { 619 kref_get(&dvbdev->ref); 620 return dvbdev; 621 } 622 EXPORT_SYMBOL(dvb_device_get); 623 624 625 void dvb_device_put(struct dvb_device *dvbdev) 626 { 627 if (dvbdev) 628 kref_put(&dvbdev->ref, dvb_free_device); 629 } 630 631 632 void dvb_unregister_device(struct dvb_device *dvbdev) 633 { 634 dvb_remove_device(dvbdev); 635 dvb_device_put(dvbdev); 636 } 637 EXPORT_SYMBOL(dvb_unregister_device); 638 639 640 #ifdef CONFIG_MEDIA_CONTROLLER_DVB 641 642 static int dvb_create_io_intf_links(struct dvb_adapter *adap, 643 struct media_interface *intf, 644 char *name) 645 { 646 struct media_device *mdev = adap->mdev; 647 struct media_entity *entity; 648 struct media_link *link; 649 650 media_device_for_each_entity(entity, mdev) { 651 if (entity->function == MEDIA_ENT_F_IO_DTV) { 652 if (strncmp(entity->name, name, strlen(name))) 653 continue; 654 link = media_create_intf_link(entity, intf, 655 MEDIA_LNK_FL_ENABLED | 656 MEDIA_LNK_FL_IMMUTABLE); 657 if (!link) 658 return -ENOMEM; 659 } 660 } 661 return 0; 662 } 663 664 int dvb_create_media_graph(struct dvb_adapter *adap, 665 bool create_rf_connector) 666 { 667 struct media_device *mdev = adap->mdev; 668 struct media_entity *entity, *tuner = NULL, *demod = NULL, *conn; 669 struct media_entity *demux = NULL, *ca = NULL; 670 struct media_link *link; 671 struct media_interface *intf; 672 unsigned demux_pad = 0; 673 unsigned dvr_pad = 0; 674 unsigned ntuner = 0, ndemod = 0; 675 int ret, pad_source, pad_sink; 676 static const char *connector_name = "Television"; 677 678 if (!mdev) 679 return 0; 680 681 media_device_for_each_entity(entity, mdev) { 682 switch (entity->function) { 683 case MEDIA_ENT_F_TUNER: 684 tuner = entity; 685 ntuner++; 686 break; 687 case MEDIA_ENT_F_DTV_DEMOD: 688 demod = entity; 689 ndemod++; 690 break; 691 case MEDIA_ENT_F_TS_DEMUX: 692 demux = entity; 693 break; 694 case MEDIA_ENT_F_DTV_CA: 695 ca = entity; 696 break; 697 } 698 } 699 700 /* 701 * Prepare to signalize to media_create_pad_links() that multiple 702 * entities of the same type exists and a 1:n or n:1 links need to be 703 * created. 704 * NOTE: if both tuner and demod have multiple instances, it is up 705 * to the caller driver to create such links. 706 */ 707 if (ntuner > 1) 708 tuner = NULL; 709 if (ndemod > 1) 710 demod = NULL; 711 712 if (create_rf_connector) { 713 conn = kzalloc(sizeof(*conn), GFP_KERNEL); 714 if (!conn) 715 return -ENOMEM; 716 adap->conn = conn; 717 718 adap->conn_pads = kzalloc(sizeof(*adap->conn_pads), GFP_KERNEL); 719 if (!adap->conn_pads) 720 return -ENOMEM; 721 722 conn->flags = MEDIA_ENT_FL_CONNECTOR; 723 conn->function = MEDIA_ENT_F_CONN_RF; 724 conn->name = connector_name; 725 adap->conn_pads->flags = MEDIA_PAD_FL_SOURCE; 726 727 ret = media_entity_pads_init(conn, 1, adap->conn_pads); 728 if (ret) 729 return ret; 730 731 ret = media_device_register_entity(mdev, conn); 732 if (ret) 733 return ret; 734 735 if (!ntuner) { 736 ret = media_create_pad_links(mdev, 737 MEDIA_ENT_F_CONN_RF, 738 conn, 0, 739 MEDIA_ENT_F_DTV_DEMOD, 740 demod, 0, 741 MEDIA_LNK_FL_ENABLED, 742 false); 743 } else { 744 pad_sink = media_get_pad_index(tuner, true, 745 PAD_SIGNAL_ANALOG); 746 if (pad_sink < 0) 747 return -EINVAL; 748 ret = media_create_pad_links(mdev, 749 MEDIA_ENT_F_CONN_RF, 750 conn, 0, 751 MEDIA_ENT_F_TUNER, 752 tuner, pad_sink, 753 MEDIA_LNK_FL_ENABLED, 754 false); 755 } 756 if (ret) 757 return ret; 758 } 759 760 if (ntuner && ndemod) { 761 /* NOTE: first found tuner source pad presumed correct */ 762 pad_source = media_get_pad_index(tuner, false, 763 PAD_SIGNAL_ANALOG); 764 if (pad_source < 0) 765 return -EINVAL; 766 ret = media_create_pad_links(mdev, 767 MEDIA_ENT_F_TUNER, 768 tuner, pad_source, 769 MEDIA_ENT_F_DTV_DEMOD, 770 demod, 0, MEDIA_LNK_FL_ENABLED, 771 false); 772 if (ret) 773 return ret; 774 } 775 776 if (ndemod && demux) { 777 ret = media_create_pad_links(mdev, 778 MEDIA_ENT_F_DTV_DEMOD, 779 demod, 1, 780 MEDIA_ENT_F_TS_DEMUX, 781 demux, 0, MEDIA_LNK_FL_ENABLED, 782 false); 783 if (ret) 784 return ret; 785 } 786 if (demux && ca) { 787 ret = media_create_pad_link(demux, 1, ca, 788 0, MEDIA_LNK_FL_ENABLED); 789 if (ret) 790 return ret; 791 } 792 793 /* Create demux links for each ringbuffer/pad */ 794 if (demux) { 795 media_device_for_each_entity(entity, mdev) { 796 if (entity->function == MEDIA_ENT_F_IO_DTV) { 797 if (!strncmp(entity->name, DVR_TSOUT, 798 strlen(DVR_TSOUT))) { 799 ret = media_create_pad_link(demux, 800 ++dvr_pad, 801 entity, 0, 0); 802 if (ret) 803 return ret; 804 } 805 if (!strncmp(entity->name, DEMUX_TSOUT, 806 strlen(DEMUX_TSOUT))) { 807 ret = media_create_pad_link(demux, 808 ++demux_pad, 809 entity, 0, 0); 810 if (ret) 811 return ret; 812 } 813 } 814 } 815 } 816 817 /* Create interface links for FE->tuner, DVR->demux and CA->ca */ 818 media_device_for_each_intf(intf, mdev) { 819 if (intf->type == MEDIA_INTF_T_DVB_CA && ca) { 820 link = media_create_intf_link(ca, intf, 821 MEDIA_LNK_FL_ENABLED | 822 MEDIA_LNK_FL_IMMUTABLE); 823 if (!link) 824 return -ENOMEM; 825 } 826 827 if (intf->type == MEDIA_INTF_T_DVB_FE && tuner) { 828 link = media_create_intf_link(tuner, intf, 829 MEDIA_LNK_FL_ENABLED | 830 MEDIA_LNK_FL_IMMUTABLE); 831 if (!link) 832 return -ENOMEM; 833 } 834 #if 0 835 /* 836 * Indirect link - let's not create yet, as we don't know how 837 * to handle indirect links, nor if this will 838 * actually be needed. 839 */ 840 if (intf->type == MEDIA_INTF_T_DVB_DVR && demux) { 841 link = media_create_intf_link(demux, intf, 842 MEDIA_LNK_FL_ENABLED | 843 MEDIA_LNK_FL_IMMUTABLE); 844 if (!link) 845 return -ENOMEM; 846 } 847 #endif 848 if (intf->type == MEDIA_INTF_T_DVB_DVR) { 849 ret = dvb_create_io_intf_links(adap, intf, DVR_TSOUT); 850 if (ret) 851 return ret; 852 } 853 if (intf->type == MEDIA_INTF_T_DVB_DEMUX) { 854 ret = dvb_create_io_intf_links(adap, intf, DEMUX_TSOUT); 855 if (ret) 856 return ret; 857 } 858 } 859 return 0; 860 } 861 EXPORT_SYMBOL_GPL(dvb_create_media_graph); 862 #endif 863 864 static int dvbdev_check_free_adapter_num(int num) 865 { 866 struct list_head *entry; 867 list_for_each(entry, &dvb_adapter_list) { 868 struct dvb_adapter *adap; 869 adap = list_entry(entry, struct dvb_adapter, list_head); 870 if (adap->num == num) 871 return 0; 872 } 873 return 1; 874 } 875 876 static int dvbdev_get_free_adapter_num (void) 877 { 878 int num = 0; 879 880 while (num < DVB_MAX_ADAPTERS) { 881 if (dvbdev_check_free_adapter_num(num)) 882 return num; 883 num++; 884 } 885 886 return -ENFILE; 887 } 888 889 890 int dvb_register_adapter(struct dvb_adapter *adap, const char *name, 891 struct module *module, struct device *device, 892 short *adapter_nums) 893 { 894 int i, num; 895 896 mutex_lock(&dvbdev_register_lock); 897 898 for (i = 0; i < DVB_MAX_ADAPTERS; ++i) { 899 num = adapter_nums[i]; 900 if (num >= 0 && num < DVB_MAX_ADAPTERS) { 901 /* use the one the driver asked for */ 902 if (dvbdev_check_free_adapter_num(num)) 903 break; 904 } else { 905 num = dvbdev_get_free_adapter_num(); 906 break; 907 } 908 num = -1; 909 } 910 911 if (num < 0) { 912 mutex_unlock(&dvbdev_register_lock); 913 return -ENFILE; 914 } 915 916 memset (adap, 0, sizeof(struct dvb_adapter)); 917 INIT_LIST_HEAD (&adap->device_list); 918 919 pr_info("DVB: registering new adapter (%s)\n", name); 920 921 adap->num = num; 922 adap->name = name; 923 adap->module = module; 924 adap->device = device; 925 adap->mfe_shared = 0; 926 adap->mfe_dvbdev = NULL; 927 mutex_init (&adap->mfe_lock); 928 929 #ifdef CONFIG_MEDIA_CONTROLLER_DVB 930 mutex_init(&adap->mdev_lock); 931 #endif 932 933 list_add_tail (&adap->list_head, &dvb_adapter_list); 934 935 mutex_unlock(&dvbdev_register_lock); 936 937 return num; 938 } 939 EXPORT_SYMBOL(dvb_register_adapter); 940 941 942 int dvb_unregister_adapter(struct dvb_adapter *adap) 943 { 944 mutex_lock(&dvbdev_register_lock); 945 list_del (&adap->list_head); 946 mutex_unlock(&dvbdev_register_lock); 947 return 0; 948 } 949 EXPORT_SYMBOL(dvb_unregister_adapter); 950 951 /* if the miracle happens and "generic_usercopy()" is included into 952 the kernel, then this can vanish. please don't make the mistake and 953 define this as video_usercopy(). this will introduce a dependency 954 to the v4l "videodev.o" module, which is unnecessary for some 955 cards (ie. the budget dvb-cards don't need the v4l module...) */ 956 int dvb_usercopy(struct file *file, 957 unsigned int cmd, unsigned long arg, 958 int (*func)(struct file *file, 959 unsigned int cmd, void *arg)) 960 { 961 char sbuf[128]; 962 void *mbuf = NULL; 963 void *parg = NULL; 964 int err = -EINVAL; 965 966 /* Copy arguments into temp kernel buffer */ 967 switch (_IOC_DIR(cmd)) { 968 case _IOC_NONE: 969 /* 970 * For this command, the pointer is actually an integer 971 * argument. 972 */ 973 parg = (void *) arg; 974 break; 975 case _IOC_READ: /* some v4l ioctls are marked wrong ... */ 976 case _IOC_WRITE: 977 case (_IOC_WRITE | _IOC_READ): 978 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) { 979 parg = sbuf; 980 } else { 981 /* too big to allocate from stack */ 982 mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL); 983 if (NULL == mbuf) 984 return -ENOMEM; 985 parg = mbuf; 986 } 987 988 err = -EFAULT; 989 if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd))) 990 goto out; 991 break; 992 } 993 994 /* call driver */ 995 if ((err = func(file, cmd, parg)) == -ENOIOCTLCMD) 996 err = -ENOTTY; 997 998 if (err < 0) 999 goto out; 1000 1001 /* Copy results into user buffer */ 1002 switch (_IOC_DIR(cmd)) 1003 { 1004 case _IOC_READ: 1005 case (_IOC_WRITE | _IOC_READ): 1006 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd))) 1007 err = -EFAULT; 1008 break; 1009 } 1010 1011 out: 1012 kfree(mbuf); 1013 return err; 1014 } 1015 1016 #if IS_ENABLED(CONFIG_I2C) 1017 struct i2c_client *dvb_module_probe(const char *module_name, 1018 const char *name, 1019 struct i2c_adapter *adap, 1020 unsigned char addr, 1021 void *platform_data) 1022 { 1023 struct i2c_client *client; 1024 struct i2c_board_info *board_info; 1025 1026 board_info = kzalloc(sizeof(*board_info), GFP_KERNEL); 1027 if (!board_info) 1028 return NULL; 1029 1030 if (name) 1031 strscpy(board_info->type, name, I2C_NAME_SIZE); 1032 else 1033 strscpy(board_info->type, module_name, I2C_NAME_SIZE); 1034 1035 board_info->addr = addr; 1036 board_info->platform_data = platform_data; 1037 request_module(module_name); 1038 client = i2c_new_client_device(adap, board_info); 1039 if (!i2c_client_has_driver(client)) { 1040 kfree(board_info); 1041 return NULL; 1042 } 1043 1044 if (!try_module_get(client->dev.driver->owner)) { 1045 i2c_unregister_device(client); 1046 client = NULL; 1047 } 1048 1049 kfree(board_info); 1050 return client; 1051 } 1052 EXPORT_SYMBOL_GPL(dvb_module_probe); 1053 1054 void dvb_module_release(struct i2c_client *client) 1055 { 1056 if (!client) 1057 return; 1058 1059 module_put(client->dev.driver->owner); 1060 i2c_unregister_device(client); 1061 } 1062 EXPORT_SYMBOL_GPL(dvb_module_release); 1063 #endif 1064 1065 static int dvb_uevent(const struct device *dev, struct kobj_uevent_env *env) 1066 { 1067 const struct dvb_device *dvbdev = dev_get_drvdata(dev); 1068 1069 add_uevent_var(env, "DVB_ADAPTER_NUM=%d", dvbdev->adapter->num); 1070 add_uevent_var(env, "DVB_DEVICE_TYPE=%s", dnames[dvbdev->type]); 1071 add_uevent_var(env, "DVB_DEVICE_NUM=%d", dvbdev->id); 1072 return 0; 1073 } 1074 1075 static char *dvb_devnode(const struct device *dev, umode_t *mode) 1076 { 1077 const struct dvb_device *dvbdev = dev_get_drvdata(dev); 1078 1079 return kasprintf(GFP_KERNEL, "dvb/adapter%d/%s%d", 1080 dvbdev->adapter->num, dnames[dvbdev->type], dvbdev->id); 1081 } 1082 1083 1084 static int __init init_dvbdev(void) 1085 { 1086 int retval; 1087 dev_t dev = MKDEV(DVB_MAJOR, 0); 1088 1089 if ((retval = register_chrdev_region(dev, MAX_DVB_MINORS, "DVB")) != 0) { 1090 pr_err("dvb-core: unable to get major %d\n", DVB_MAJOR); 1091 return retval; 1092 } 1093 1094 cdev_init(&dvb_device_cdev, &dvb_device_fops); 1095 if ((retval = cdev_add(&dvb_device_cdev, dev, MAX_DVB_MINORS)) != 0) { 1096 pr_err("dvb-core: unable register character device\n"); 1097 goto error; 1098 } 1099 1100 dvb_class = class_create("dvb"); 1101 if (IS_ERR(dvb_class)) { 1102 retval = PTR_ERR(dvb_class); 1103 goto error; 1104 } 1105 dvb_class->dev_uevent = dvb_uevent; 1106 dvb_class->devnode = dvb_devnode; 1107 return 0; 1108 1109 error: 1110 cdev_del(&dvb_device_cdev); 1111 unregister_chrdev_region(dev, MAX_DVB_MINORS); 1112 return retval; 1113 } 1114 1115 1116 static void __exit exit_dvbdev(void) 1117 { 1118 struct dvbdevfops_node *node, *next; 1119 1120 class_destroy(dvb_class); 1121 cdev_del(&dvb_device_cdev); 1122 unregister_chrdev_region(MKDEV(DVB_MAJOR, 0), MAX_DVB_MINORS); 1123 1124 list_for_each_entry_safe(node, next, &dvbdevfops_list, list_head) { 1125 list_del (&node->list_head); 1126 kfree(node->fops); 1127 kfree(node); 1128 } 1129 } 1130 1131 subsys_initcall(init_dvbdev); 1132 module_exit(exit_dvbdev); 1133 1134 MODULE_DESCRIPTION("DVB Core Driver"); 1135 MODULE_AUTHOR("Marcus Metzler, Ralph Metzler, Holger Waechtler"); 1136 MODULE_LICENSE("GPL"); 1137