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