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