sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / media / dvb-core / dvbdev.c
blob38c844667789361d1385f6609b30fedade294bcd
1 /*
2 * dvbdev.c
4 * Copyright (C) 2000 Ralph Metzler <ralph@convergence.de>
5 * & Marcus Metzler <marcus@convergence.de>
6 * for convergence integrated media GmbH
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.
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.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #define pr_fmt(fmt) "dvbdev: " fmt
26 #include <linux/types.h>
27 #include <linux/errno.h>
28 #include <linux/string.h>
29 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/init.h>
32 #include <linux/slab.h>
33 #include <linux/device.h>
34 #include <linux/fs.h>
35 #include <linux/cdev.h>
36 #include <linux/mutex.h>
37 #include "dvbdev.h"
39 /* Due to enum tuner_pad_index */
40 #include <media/tuner.h>
42 static DEFINE_MUTEX(dvbdev_mutex);
43 static int dvbdev_debug;
45 module_param(dvbdev_debug, int, 0644);
46 MODULE_PARM_DESC(dvbdev_debug, "Turn on/off device debugging (default:off).");
48 #define dprintk(fmt, arg...) do { \
49 if (dvbdev_debug) \
50 printk(KERN_DEBUG pr_fmt("%s: " fmt), \
51 __func__, ##arg); \
52 } while (0)
54 static LIST_HEAD(dvb_adapter_list);
55 static DEFINE_MUTEX(dvbdev_register_lock);
57 static const char * const dnames[] = {
58 "video", "audio", "sec", "frontend", "demux", "dvr", "ca",
59 "net", "osd"
62 #ifdef CONFIG_DVB_DYNAMIC_MINORS
63 #define MAX_DVB_MINORS 256
64 #define DVB_MAX_IDS MAX_DVB_MINORS
65 #else
66 #define DVB_MAX_IDS 4
67 #define nums2minor(num, type, id) ((num << 6) | (id << 4) | type)
68 #define MAX_DVB_MINORS (DVB_MAX_ADAPTERS*64)
69 #endif
71 static struct class *dvb_class;
73 static struct dvb_device *dvb_minors[MAX_DVB_MINORS];
74 static DECLARE_RWSEM(minor_rwsem);
76 static int dvb_device_open(struct inode *inode, struct file *file)
78 struct dvb_device *dvbdev;
80 mutex_lock(&dvbdev_mutex);
81 down_read(&minor_rwsem);
82 dvbdev = dvb_minors[iminor(inode)];
84 if (dvbdev && dvbdev->fops) {
85 int err = 0;
86 const struct file_operations *new_fops;
88 new_fops = fops_get(dvbdev->fops);
89 if (!new_fops)
90 goto fail;
91 file->private_data = dvbdev;
92 replace_fops(file, new_fops);
93 if (file->f_op->open)
94 err = file->f_op->open(inode, file);
95 up_read(&minor_rwsem);
96 mutex_unlock(&dvbdev_mutex);
97 return err;
99 fail:
100 up_read(&minor_rwsem);
101 mutex_unlock(&dvbdev_mutex);
102 return -ENODEV;
106 static const struct file_operations dvb_device_fops =
108 .owner = THIS_MODULE,
109 .open = dvb_device_open,
110 .llseek = noop_llseek,
113 static struct cdev dvb_device_cdev;
115 int dvb_generic_open(struct inode *inode, struct file *file)
117 struct dvb_device *dvbdev = file->private_data;
119 if (!dvbdev)
120 return -ENODEV;
122 if (!dvbdev->users)
123 return -EBUSY;
125 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
126 if (!dvbdev->readers)
127 return -EBUSY;
128 dvbdev->readers--;
129 } else {
130 if (!dvbdev->writers)
131 return -EBUSY;
132 dvbdev->writers--;
135 dvbdev->users--;
136 return 0;
138 EXPORT_SYMBOL(dvb_generic_open);
141 int dvb_generic_release(struct inode *inode, struct file *file)
143 struct dvb_device *dvbdev = file->private_data;
145 if (!dvbdev)
146 return -ENODEV;
148 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
149 dvbdev->readers++;
150 } else {
151 dvbdev->writers++;
154 dvbdev->users++;
155 return 0;
157 EXPORT_SYMBOL(dvb_generic_release);
160 long dvb_generic_ioctl(struct file *file,
161 unsigned int cmd, unsigned long arg)
163 struct dvb_device *dvbdev = file->private_data;
165 if (!dvbdev)
166 return -ENODEV;
168 if (!dvbdev->kernel_ioctl)
169 return -EINVAL;
171 return dvb_usercopy(file, cmd, arg, dvbdev->kernel_ioctl);
173 EXPORT_SYMBOL(dvb_generic_ioctl);
176 static int dvbdev_get_free_id (struct dvb_adapter *adap, int type)
178 u32 id = 0;
180 while (id < DVB_MAX_IDS) {
181 struct dvb_device *dev;
182 list_for_each_entry(dev, &adap->device_list, list_head)
183 if (dev->type == type && dev->id == id)
184 goto skip;
185 return id;
186 skip:
187 id++;
189 return -ENFILE;
192 static void dvb_media_device_free(struct dvb_device *dvbdev)
194 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
195 if (dvbdev->entity) {
196 media_device_unregister_entity(dvbdev->entity);
197 kfree(dvbdev->entity);
198 kfree(dvbdev->pads);
199 dvbdev->entity = NULL;
200 dvbdev->pads = NULL;
203 if (dvbdev->tsout_entity) {
204 int i;
206 for (i = 0; i < dvbdev->tsout_num_entities; i++) {
207 media_device_unregister_entity(&dvbdev->tsout_entity[i]);
208 kfree(dvbdev->tsout_entity[i].name);
210 kfree(dvbdev->tsout_entity);
211 kfree(dvbdev->tsout_pads);
212 dvbdev->tsout_entity = NULL;
213 dvbdev->tsout_pads = NULL;
215 dvbdev->tsout_num_entities = 0;
218 if (dvbdev->intf_devnode) {
219 media_devnode_remove(dvbdev->intf_devnode);
220 dvbdev->intf_devnode = NULL;
223 if (dvbdev->adapter->conn) {
224 media_device_unregister_entity(dvbdev->adapter->conn);
225 dvbdev->adapter->conn = NULL;
226 kfree(dvbdev->adapter->conn_pads);
227 dvbdev->adapter->conn_pads = NULL;
229 #endif
232 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
233 static int dvb_create_tsout_entity(struct dvb_device *dvbdev,
234 const char *name, int npads)
236 int i, ret = 0;
238 dvbdev->tsout_pads = kcalloc(npads, sizeof(*dvbdev->tsout_pads),
239 GFP_KERNEL);
240 if (!dvbdev->tsout_pads)
241 return -ENOMEM;
243 dvbdev->tsout_entity = kcalloc(npads, sizeof(*dvbdev->tsout_entity),
244 GFP_KERNEL);
245 if (!dvbdev->tsout_entity)
246 return -ENOMEM;
248 dvbdev->tsout_num_entities = npads;
250 for (i = 0; i < npads; i++) {
251 struct media_pad *pads = &dvbdev->tsout_pads[i];
252 struct media_entity *entity = &dvbdev->tsout_entity[i];
254 entity->name = kasprintf(GFP_KERNEL, "%s #%d", name, i);
255 if (!entity->name)
256 return -ENOMEM;
258 entity->function = MEDIA_ENT_F_IO_DTV;
259 pads->flags = MEDIA_PAD_FL_SINK;
261 ret = media_entity_pads_init(entity, 1, pads);
262 if (ret < 0)
263 return ret;
265 ret = media_device_register_entity(dvbdev->adapter->mdev,
266 entity);
267 if (ret < 0)
268 return ret;
270 return 0;
273 #define DEMUX_TSOUT "demux-tsout"
274 #define DVR_TSOUT "dvr-tsout"
276 static int dvb_create_media_entity(struct dvb_device *dvbdev,
277 int type, int demux_sink_pads)
279 int i, ret, npads;
281 switch (type) {
282 case DVB_DEVICE_FRONTEND:
283 npads = 2;
284 break;
285 case DVB_DEVICE_DVR:
286 ret = dvb_create_tsout_entity(dvbdev, DVR_TSOUT,
287 demux_sink_pads);
288 return ret;
289 case DVB_DEVICE_DEMUX:
290 npads = 1 + demux_sink_pads;
291 ret = dvb_create_tsout_entity(dvbdev, DEMUX_TSOUT,
292 demux_sink_pads);
293 if (ret < 0)
294 return ret;
295 break;
296 case DVB_DEVICE_CA:
297 npads = 2;
298 break;
299 case DVB_DEVICE_NET:
301 * We should be creating entities for the MPE/ULE
302 * decapsulation hardware (or software implementation).
304 * However, the number of for the MPE/ULE decaps may not be
305 * fixed. As we don't have yet dynamic support for PADs at
306 * the Media Controller, let's not create the decap
307 * entities yet.
309 return 0;
310 default:
311 return 0;
314 dvbdev->entity = kzalloc(sizeof(*dvbdev->entity), GFP_KERNEL);
315 if (!dvbdev->entity)
316 return -ENOMEM;
318 dvbdev->entity->name = dvbdev->name;
320 if (npads) {
321 dvbdev->pads = kcalloc(npads, sizeof(*dvbdev->pads),
322 GFP_KERNEL);
323 if (!dvbdev->pads)
324 return -ENOMEM;
327 switch (type) {
328 case DVB_DEVICE_FRONTEND:
329 dvbdev->entity->function = MEDIA_ENT_F_DTV_DEMOD;
330 dvbdev->pads[0].flags = MEDIA_PAD_FL_SINK;
331 dvbdev->pads[1].flags = MEDIA_PAD_FL_SOURCE;
332 break;
333 case DVB_DEVICE_DEMUX:
334 dvbdev->entity->function = MEDIA_ENT_F_TS_DEMUX;
335 dvbdev->pads[0].flags = MEDIA_PAD_FL_SINK;
336 for (i = 1; i < npads; i++)
337 dvbdev->pads[i].flags = MEDIA_PAD_FL_SOURCE;
338 break;
339 case DVB_DEVICE_CA:
340 dvbdev->entity->function = MEDIA_ENT_F_DTV_CA;
341 dvbdev->pads[0].flags = MEDIA_PAD_FL_SINK;
342 dvbdev->pads[1].flags = MEDIA_PAD_FL_SOURCE;
343 break;
344 default:
345 /* Should never happen, as the first switch prevents it */
346 kfree(dvbdev->entity);
347 kfree(dvbdev->pads);
348 dvbdev->entity = NULL;
349 dvbdev->pads = NULL;
350 return 0;
353 if (npads) {
354 ret = media_entity_pads_init(dvbdev->entity, npads, dvbdev->pads);
355 if (ret)
356 return ret;
358 ret = media_device_register_entity(dvbdev->adapter->mdev,
359 dvbdev->entity);
360 if (ret)
361 return ret;
363 pr_info("%s: media entity '%s' registered.\n",
364 __func__, dvbdev->entity->name);
366 return 0;
368 #endif
370 static int dvb_register_media_device(struct dvb_device *dvbdev,
371 int type, int minor,
372 unsigned demux_sink_pads)
374 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
375 struct media_link *link;
376 u32 intf_type;
377 int ret;
379 if (!dvbdev->adapter->mdev)
380 return 0;
382 ret = dvb_create_media_entity(dvbdev, type, demux_sink_pads);
383 if (ret)
384 return ret;
386 switch (type) {
387 case DVB_DEVICE_FRONTEND:
388 intf_type = MEDIA_INTF_T_DVB_FE;
389 break;
390 case DVB_DEVICE_DEMUX:
391 intf_type = MEDIA_INTF_T_DVB_DEMUX;
392 break;
393 case DVB_DEVICE_DVR:
394 intf_type = MEDIA_INTF_T_DVB_DVR;
395 break;
396 case DVB_DEVICE_CA:
397 intf_type = MEDIA_INTF_T_DVB_CA;
398 break;
399 case DVB_DEVICE_NET:
400 intf_type = MEDIA_INTF_T_DVB_NET;
401 break;
402 default:
403 return 0;
406 dvbdev->intf_devnode = media_devnode_create(dvbdev->adapter->mdev,
407 intf_type, 0,
408 DVB_MAJOR, minor);
410 if (!dvbdev->intf_devnode)
411 return -ENOMEM;
414 * Create the "obvious" link, e. g. the ones that represent
415 * a direct association between an interface and an entity.
416 * Other links should be created elsewhere, like:
417 * DVB FE intf -> tuner
418 * DVB demux intf -> dvr
421 if (!dvbdev->entity)
422 return 0;
424 link = media_create_intf_link(dvbdev->entity, &dvbdev->intf_devnode->intf,
425 MEDIA_LNK_FL_ENABLED);
426 if (!link)
427 return -ENOMEM;
428 #endif
429 return 0;
432 int dvb_register_device(struct dvb_adapter *adap, struct dvb_device **pdvbdev,
433 const struct dvb_device *template, void *priv, int type,
434 int demux_sink_pads)
436 struct dvb_device *dvbdev;
437 struct file_operations *dvbdevfops;
438 struct device *clsdev;
439 int minor;
440 int id, ret;
442 mutex_lock(&dvbdev_register_lock);
444 if ((id = dvbdev_get_free_id (adap, type)) < 0){
445 mutex_unlock(&dvbdev_register_lock);
446 *pdvbdev = NULL;
447 pr_err("%s: couldn't find free device id\n", __func__);
448 return -ENFILE;
451 *pdvbdev = dvbdev = kzalloc(sizeof(*dvbdev), GFP_KERNEL);
453 if (!dvbdev){
454 mutex_unlock(&dvbdev_register_lock);
455 return -ENOMEM;
458 dvbdevfops = kzalloc(sizeof(struct file_operations), GFP_KERNEL);
460 if (!dvbdevfops){
461 kfree (dvbdev);
462 mutex_unlock(&dvbdev_register_lock);
463 return -ENOMEM;
466 memcpy(dvbdev, template, sizeof(struct dvb_device));
467 dvbdev->type = type;
468 dvbdev->id = id;
469 dvbdev->adapter = adap;
470 dvbdev->priv = priv;
471 dvbdev->fops = dvbdevfops;
472 init_waitqueue_head (&dvbdev->wait_queue);
474 memcpy(dvbdevfops, template->fops, sizeof(struct file_operations));
475 dvbdevfops->owner = adap->module;
477 list_add_tail (&dvbdev->list_head, &adap->device_list);
479 down_write(&minor_rwsem);
480 #ifdef CONFIG_DVB_DYNAMIC_MINORS
481 for (minor = 0; minor < MAX_DVB_MINORS; minor++)
482 if (dvb_minors[minor] == NULL)
483 break;
485 if (minor == MAX_DVB_MINORS) {
486 kfree(dvbdevfops);
487 kfree(dvbdev);
488 up_write(&minor_rwsem);
489 mutex_unlock(&dvbdev_register_lock);
490 return -EINVAL;
492 #else
493 minor = nums2minor(adap->num, type, id);
494 #endif
496 dvbdev->minor = minor;
497 dvb_minors[minor] = dvbdev;
498 up_write(&minor_rwsem);
500 ret = dvb_register_media_device(dvbdev, type, minor, demux_sink_pads);
501 if (ret) {
502 pr_err("%s: dvb_register_media_device failed to create the mediagraph\n",
503 __func__);
505 dvb_media_device_free(dvbdev);
506 kfree(dvbdevfops);
507 kfree(dvbdev);
508 up_write(&minor_rwsem);
509 mutex_unlock(&dvbdev_register_lock);
510 return ret;
513 mutex_unlock(&dvbdev_register_lock);
515 clsdev = device_create(dvb_class, adap->device,
516 MKDEV(DVB_MAJOR, minor),
517 dvbdev, "dvb%d.%s%d", adap->num, dnames[type], id);
518 if (IS_ERR(clsdev)) {
519 pr_err("%s: failed to create device dvb%d.%s%d (%ld)\n",
520 __func__, adap->num, dnames[type], id, PTR_ERR(clsdev));
521 return PTR_ERR(clsdev);
523 dprintk("DVB: register adapter%d/%s%d @ minor: %i (0x%02x)\n",
524 adap->num, dnames[type], id, minor, minor);
526 return 0;
528 EXPORT_SYMBOL(dvb_register_device);
531 void dvb_remove_device(struct dvb_device *dvbdev)
533 if (!dvbdev)
534 return;
536 down_write(&minor_rwsem);
537 dvb_minors[dvbdev->minor] = NULL;
538 up_write(&minor_rwsem);
540 dvb_media_device_free(dvbdev);
542 device_destroy(dvb_class, MKDEV(DVB_MAJOR, dvbdev->minor));
544 list_del (&dvbdev->list_head);
546 EXPORT_SYMBOL(dvb_remove_device);
549 void dvb_free_device(struct dvb_device *dvbdev)
551 if (!dvbdev)
552 return;
554 kfree (dvbdev->fops);
555 kfree (dvbdev);
557 EXPORT_SYMBOL(dvb_free_device);
560 void dvb_unregister_device(struct dvb_device *dvbdev)
562 dvb_remove_device(dvbdev);
563 dvb_free_device(dvbdev);
565 EXPORT_SYMBOL(dvb_unregister_device);
568 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
570 static int dvb_create_io_intf_links(struct dvb_adapter *adap,
571 struct media_interface *intf,
572 char *name)
574 struct media_device *mdev = adap->mdev;
575 struct media_entity *entity;
576 struct media_link *link;
578 media_device_for_each_entity(entity, mdev) {
579 if (entity->function == MEDIA_ENT_F_IO_DTV) {
580 if (strncmp(entity->name, name, strlen(name)))
581 continue;
582 link = media_create_intf_link(entity, intf,
583 MEDIA_LNK_FL_ENABLED);
584 if (!link)
585 return -ENOMEM;
588 return 0;
591 int dvb_create_media_graph(struct dvb_adapter *adap,
592 bool create_rf_connector)
594 struct media_device *mdev = adap->mdev;
595 struct media_entity *entity, *tuner = NULL, *demod = NULL, *conn;
596 struct media_entity *demux = NULL, *ca = NULL;
597 struct media_link *link;
598 struct media_interface *intf;
599 unsigned demux_pad = 0;
600 unsigned dvr_pad = 0;
601 unsigned ntuner = 0, ndemod = 0;
602 int ret;
603 static const char *connector_name = "Television";
605 if (!mdev)
606 return 0;
608 media_device_for_each_entity(entity, mdev) {
609 switch (entity->function) {
610 case MEDIA_ENT_F_TUNER:
611 tuner = entity;
612 ntuner++;
613 break;
614 case MEDIA_ENT_F_DTV_DEMOD:
615 demod = entity;
616 ndemod++;
617 break;
618 case MEDIA_ENT_F_TS_DEMUX:
619 demux = entity;
620 break;
621 case MEDIA_ENT_F_DTV_CA:
622 ca = entity;
623 break;
628 * Prepare to signalize to media_create_pad_links() that multiple
629 * entities of the same type exists and a 1:n or n:1 links need to be
630 * created.
631 * NOTE: if both tuner and demod have multiple instances, it is up
632 * to the caller driver to create such links.
634 if (ntuner > 1)
635 tuner = NULL;
636 if (ndemod > 1)
637 demod = NULL;
639 if (create_rf_connector) {
640 conn = kzalloc(sizeof(*conn), GFP_KERNEL);
641 if (!conn)
642 return -ENOMEM;
643 adap->conn = conn;
645 adap->conn_pads = kzalloc(sizeof(*adap->conn_pads), GFP_KERNEL);
646 if (!adap->conn_pads)
647 return -ENOMEM;
649 conn->flags = MEDIA_ENT_FL_CONNECTOR;
650 conn->function = MEDIA_ENT_F_CONN_RF;
651 conn->name = connector_name;
652 adap->conn_pads->flags = MEDIA_PAD_FL_SOURCE;
654 ret = media_entity_pads_init(conn, 1, adap->conn_pads);
655 if (ret)
656 return ret;
658 ret = media_device_register_entity(mdev, conn);
659 if (ret)
660 return ret;
662 if (!ntuner)
663 ret = media_create_pad_links(mdev,
664 MEDIA_ENT_F_CONN_RF,
665 conn, 0,
666 MEDIA_ENT_F_DTV_DEMOD,
667 demod, 0,
668 MEDIA_LNK_FL_ENABLED,
669 false);
670 else
671 ret = media_create_pad_links(mdev,
672 MEDIA_ENT_F_CONN_RF,
673 conn, 0,
674 MEDIA_ENT_F_TUNER,
675 tuner, TUNER_PAD_RF_INPUT,
676 MEDIA_LNK_FL_ENABLED,
677 false);
678 if (ret)
679 return ret;
682 if (ntuner && ndemod) {
683 ret = media_create_pad_links(mdev,
684 MEDIA_ENT_F_TUNER,
685 tuner, TUNER_PAD_OUTPUT,
686 MEDIA_ENT_F_DTV_DEMOD,
687 demod, 0, MEDIA_LNK_FL_ENABLED,
688 false);
689 if (ret)
690 return ret;
693 if (ndemod && demux) {
694 ret = media_create_pad_links(mdev,
695 MEDIA_ENT_F_DTV_DEMOD,
696 demod, 1,
697 MEDIA_ENT_F_TS_DEMUX,
698 demux, 0, MEDIA_LNK_FL_ENABLED,
699 false);
700 if (ret)
701 return ret;
703 if (demux && ca) {
704 ret = media_create_pad_link(demux, 1, ca,
705 0, MEDIA_LNK_FL_ENABLED);
706 if (ret)
707 return ret;
710 /* Create demux links for each ringbuffer/pad */
711 if (demux) {
712 media_device_for_each_entity(entity, mdev) {
713 if (entity->function == MEDIA_ENT_F_IO_DTV) {
714 if (!strncmp(entity->name, DVR_TSOUT,
715 strlen(DVR_TSOUT))) {
716 ret = media_create_pad_link(demux,
717 ++dvr_pad,
718 entity, 0, 0);
719 if (ret)
720 return ret;
722 if (!strncmp(entity->name, DEMUX_TSOUT,
723 strlen(DEMUX_TSOUT))) {
724 ret = media_create_pad_link(demux,
725 ++demux_pad,
726 entity, 0, 0);
727 if (ret)
728 return ret;
734 /* Create interface links for FE->tuner, DVR->demux and CA->ca */
735 media_device_for_each_intf(intf, mdev) {
736 if (intf->type == MEDIA_INTF_T_DVB_CA && ca) {
737 link = media_create_intf_link(ca, intf,
738 MEDIA_LNK_FL_ENABLED);
739 if (!link)
740 return -ENOMEM;
743 if (intf->type == MEDIA_INTF_T_DVB_FE && tuner) {
744 link = media_create_intf_link(tuner, intf,
745 MEDIA_LNK_FL_ENABLED);
746 if (!link)
747 return -ENOMEM;
749 #if 0
751 * Indirect link - let's not create yet, as we don't know how
752 * to handle indirect links, nor if this will
753 * actually be needed.
755 if (intf->type == MEDIA_INTF_T_DVB_DVR && demux) {
756 link = media_create_intf_link(demux, intf,
757 MEDIA_LNK_FL_ENABLED);
758 if (!link)
759 return -ENOMEM;
761 #endif
762 if (intf->type == MEDIA_INTF_T_DVB_DVR) {
763 ret = dvb_create_io_intf_links(adap, intf, DVR_TSOUT);
764 if (ret)
765 return ret;
767 if (intf->type == MEDIA_INTF_T_DVB_DEMUX) {
768 ret = dvb_create_io_intf_links(adap, intf, DEMUX_TSOUT);
769 if (ret)
770 return ret;
773 return 0;
775 EXPORT_SYMBOL_GPL(dvb_create_media_graph);
776 #endif
778 static int dvbdev_check_free_adapter_num(int num)
780 struct list_head *entry;
781 list_for_each(entry, &dvb_adapter_list) {
782 struct dvb_adapter *adap;
783 adap = list_entry(entry, struct dvb_adapter, list_head);
784 if (adap->num == num)
785 return 0;
787 return 1;
790 static int dvbdev_get_free_adapter_num (void)
792 int num = 0;
794 while (num < DVB_MAX_ADAPTERS) {
795 if (dvbdev_check_free_adapter_num(num))
796 return num;
797 num++;
800 return -ENFILE;
804 int dvb_register_adapter(struct dvb_adapter *adap, const char *name,
805 struct module *module, struct device *device,
806 short *adapter_nums)
808 int i, num;
810 mutex_lock(&dvbdev_register_lock);
812 for (i = 0; i < DVB_MAX_ADAPTERS; ++i) {
813 num = adapter_nums[i];
814 if (num >= 0 && num < DVB_MAX_ADAPTERS) {
815 /* use the one the driver asked for */
816 if (dvbdev_check_free_adapter_num(num))
817 break;
818 } else {
819 num = dvbdev_get_free_adapter_num();
820 break;
822 num = -1;
825 if (num < 0) {
826 mutex_unlock(&dvbdev_register_lock);
827 return -ENFILE;
830 memset (adap, 0, sizeof(struct dvb_adapter));
831 INIT_LIST_HEAD (&adap->device_list);
833 pr_info("DVB: registering new adapter (%s)\n", name);
835 adap->num = num;
836 adap->name = name;
837 adap->module = module;
838 adap->device = device;
839 adap->mfe_shared = 0;
840 adap->mfe_dvbdev = NULL;
841 mutex_init (&adap->mfe_lock);
843 list_add_tail (&adap->list_head, &dvb_adapter_list);
845 mutex_unlock(&dvbdev_register_lock);
847 return num;
849 EXPORT_SYMBOL(dvb_register_adapter);
852 int dvb_unregister_adapter(struct dvb_adapter *adap)
854 mutex_lock(&dvbdev_register_lock);
855 list_del (&adap->list_head);
856 mutex_unlock(&dvbdev_register_lock);
857 return 0;
859 EXPORT_SYMBOL(dvb_unregister_adapter);
861 /* if the miracle happens and "generic_usercopy()" is included into
862 the kernel, then this can vanish. please don't make the mistake and
863 define this as video_usercopy(). this will introduce a dependecy
864 to the v4l "videodev.o" module, which is unnecessary for some
865 cards (ie. the budget dvb-cards don't need the v4l module...) */
866 int dvb_usercopy(struct file *file,
867 unsigned int cmd, unsigned long arg,
868 int (*func)(struct file *file,
869 unsigned int cmd, void *arg))
871 char sbuf[128];
872 void *mbuf = NULL;
873 void *parg = NULL;
874 int err = -EINVAL;
876 /* Copy arguments into temp kernel buffer */
877 switch (_IOC_DIR(cmd)) {
878 case _IOC_NONE:
880 * For this command, the pointer is actually an integer
881 * argument.
883 parg = (void *) arg;
884 break;
885 case _IOC_READ: /* some v4l ioctls are marked wrong ... */
886 case _IOC_WRITE:
887 case (_IOC_WRITE | _IOC_READ):
888 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
889 parg = sbuf;
890 } else {
891 /* too big to allocate from stack */
892 mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
893 if (NULL == mbuf)
894 return -ENOMEM;
895 parg = mbuf;
898 err = -EFAULT;
899 if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
900 goto out;
901 break;
904 /* call driver */
905 if ((err = func(file, cmd, parg)) == -ENOIOCTLCMD)
906 err = -ENOTTY;
908 if (err < 0)
909 goto out;
911 /* Copy results into user buffer */
912 switch (_IOC_DIR(cmd))
914 case _IOC_READ:
915 case (_IOC_WRITE | _IOC_READ):
916 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
917 err = -EFAULT;
918 break;
921 out:
922 kfree(mbuf);
923 return err;
926 static int dvb_uevent(struct device *dev, struct kobj_uevent_env *env)
928 struct dvb_device *dvbdev = dev_get_drvdata(dev);
930 add_uevent_var(env, "DVB_ADAPTER_NUM=%d", dvbdev->adapter->num);
931 add_uevent_var(env, "DVB_DEVICE_TYPE=%s", dnames[dvbdev->type]);
932 add_uevent_var(env, "DVB_DEVICE_NUM=%d", dvbdev->id);
933 return 0;
936 static char *dvb_devnode(struct device *dev, umode_t *mode)
938 struct dvb_device *dvbdev = dev_get_drvdata(dev);
940 return kasprintf(GFP_KERNEL, "dvb/adapter%d/%s%d",
941 dvbdev->adapter->num, dnames[dvbdev->type], dvbdev->id);
945 static int __init init_dvbdev(void)
947 int retval;
948 dev_t dev = MKDEV(DVB_MAJOR, 0);
950 if ((retval = register_chrdev_region(dev, MAX_DVB_MINORS, "DVB")) != 0) {
951 pr_err("dvb-core: unable to get major %d\n", DVB_MAJOR);
952 return retval;
955 cdev_init(&dvb_device_cdev, &dvb_device_fops);
956 if ((retval = cdev_add(&dvb_device_cdev, dev, MAX_DVB_MINORS)) != 0) {
957 pr_err("dvb-core: unable register character device\n");
958 goto error;
961 dvb_class = class_create(THIS_MODULE, "dvb");
962 if (IS_ERR(dvb_class)) {
963 retval = PTR_ERR(dvb_class);
964 goto error;
966 dvb_class->dev_uevent = dvb_uevent;
967 dvb_class->devnode = dvb_devnode;
968 return 0;
970 error:
971 cdev_del(&dvb_device_cdev);
972 unregister_chrdev_region(dev, MAX_DVB_MINORS);
973 return retval;
977 static void __exit exit_dvbdev(void)
979 class_destroy(dvb_class);
980 cdev_del(&dvb_device_cdev);
981 unregister_chrdev_region(MKDEV(DVB_MAJOR, 0), MAX_DVB_MINORS);
984 subsys_initcall(init_dvbdev);
985 module_exit(exit_dvbdev);
987 MODULE_DESCRIPTION("DVB Core Driver");
988 MODULE_AUTHOR("Marcus Metzler, Ralph Metzler, Holger Waechtler");
989 MODULE_LICENSE("GPL");