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 #include <linux/types.h>
25 #include <linux/errno.h>
26 #include <linux/string.h>
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/init.h>
30 #include <linux/slab.h>
31 #include <linux/device.h>
33 #include <linux/cdev.h>
34 #include <linux/mutex.h>
37 static DEFINE_MUTEX(dvbdev_mutex);
38 static int dvbdev_debug;
40 module_param(dvbdev_debug, int, 0644);
41 MODULE_PARM_DESC(dvbdev_debug, "Turn on/off device debugging (default:off).");
43 #define dprintk if (dvbdev_debug) printk
45 static LIST_HEAD(dvb_adapter_list);
46 static DEFINE_MUTEX(dvbdev_register_lock);
48 static const char * const dnames[] = {
49 "video", "audio", "sec", "frontend", "demux", "dvr", "ca",
53 #ifdef CONFIG_DVB_DYNAMIC_MINORS
54 #define MAX_DVB_MINORS 256
55 #define DVB_MAX_IDS MAX_DVB_MINORS
58 #define nums2minor(num,type,id) ((num << 6) | (id << 4) | type)
59 #define MAX_DVB_MINORS (DVB_MAX_ADAPTERS*64)
62 static struct class *dvb_class;
64 static struct dvb_device *dvb_minors[MAX_DVB_MINORS];
65 static DECLARE_RWSEM(minor_rwsem);
67 static int dvb_device_open(struct inode *inode, struct file *file)
69 struct dvb_device *dvbdev;
71 mutex_lock(&dvbdev_mutex);
72 down_read(&minor_rwsem);
73 dvbdev = dvb_minors[iminor(inode)];
75 if (dvbdev && dvbdev->fops) {
77 const struct file_operations *new_fops;
79 new_fops = fops_get(dvbdev->fops);
82 file->private_data = dvbdev;
83 replace_fops(file, new_fops);
85 err = file->f_op->open(inode,file);
86 up_read(&minor_rwsem);
87 mutex_unlock(&dvbdev_mutex);
91 up_read(&minor_rwsem);
92 mutex_unlock(&dvbdev_mutex);
97 static const struct file_operations dvb_device_fops =
100 .open = dvb_device_open,
101 .llseek = noop_llseek,
104 static struct cdev dvb_device_cdev;
106 int dvb_generic_open(struct inode *inode, struct file *file)
108 struct dvb_device *dvbdev = file->private_data;
116 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
117 if (!dvbdev->readers)
121 if (!dvbdev->writers)
129 EXPORT_SYMBOL(dvb_generic_open);
132 int dvb_generic_release(struct inode *inode, struct file *file)
134 struct dvb_device *dvbdev = file->private_data;
139 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
148 EXPORT_SYMBOL(dvb_generic_release);
151 long dvb_generic_ioctl(struct file *file,
152 unsigned int cmd, unsigned long arg)
154 struct dvb_device *dvbdev = file->private_data;
159 if (!dvbdev->kernel_ioctl)
162 return dvb_usercopy(file, cmd, arg, dvbdev->kernel_ioctl);
164 EXPORT_SYMBOL(dvb_generic_ioctl);
167 static int dvbdev_get_free_id (struct dvb_adapter *adap, int type)
171 while (id < DVB_MAX_IDS) {
172 struct dvb_device *dev;
173 list_for_each_entry(dev, &adap->device_list, list_head)
174 if (dev->type == type && dev->id == id)
183 static void dvb_register_media_device(struct dvb_device *dvbdev,
186 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
189 if (!dvbdev->adapter->mdev)
192 dvbdev->entity = kzalloc(sizeof(*dvbdev->entity), GFP_KERNEL);
196 dvbdev->entity->info.dev.major = DVB_MAJOR;
197 dvbdev->entity->info.dev.minor = minor;
198 dvbdev->entity->name = dvbdev->name;
202 case DVB_DEVICE_DEMUX:
213 dvbdev->pads = kcalloc(npads, sizeof(*dvbdev->pads),
216 kfree(dvbdev->entity);
222 case DVB_DEVICE_FRONTEND:
223 dvbdev->entity->type = MEDIA_ENT_T_DEVNODE_DVB_FE;
224 dvbdev->pads[0].flags = MEDIA_PAD_FL_SOURCE;
226 case DVB_DEVICE_DEMUX:
227 dvbdev->entity->type = MEDIA_ENT_T_DEVNODE_DVB_DEMUX;
228 dvbdev->pads[0].flags = MEDIA_PAD_FL_SOURCE;
229 dvbdev->pads[1].flags = MEDIA_PAD_FL_SINK;
232 dvbdev->entity->type = MEDIA_ENT_T_DEVNODE_DVB_DVR;
233 dvbdev->pads[0].flags = MEDIA_PAD_FL_SINK;
236 dvbdev->entity->type = MEDIA_ENT_T_DEVNODE_DVB_CA;
237 dvbdev->pads[0].flags = MEDIA_PAD_FL_SOURCE;
238 dvbdev->pads[1].flags = MEDIA_PAD_FL_SINK;
241 dvbdev->entity->type = MEDIA_ENT_T_DEVNODE_DVB_NET;
244 kfree(dvbdev->entity);
245 dvbdev->entity = NULL;
250 ret = media_entity_init(dvbdev->entity, npads, dvbdev->pads, 0);
252 ret = media_device_register_entity(dvbdev->adapter->mdev,
256 "%s: media_device_register_entity failed for %s\n",
257 __func__, dvbdev->entity->name);
259 kfree(dvbdev->entity);
260 dvbdev->entity = NULL;
264 printk(KERN_DEBUG "%s: media device '%s' registered.\n",
265 __func__, dvbdev->entity->name);
269 int dvb_register_device(struct dvb_adapter *adap, struct dvb_device **pdvbdev,
270 const struct dvb_device *template, void *priv, int type)
272 struct dvb_device *dvbdev;
273 struct file_operations *dvbdevfops;
274 struct device *clsdev;
278 mutex_lock(&dvbdev_register_lock);
280 if ((id = dvbdev_get_free_id (adap, type)) < 0){
281 mutex_unlock(&dvbdev_register_lock);
283 printk(KERN_ERR "%s: couldn't find free device id\n", __func__);
287 *pdvbdev = dvbdev = kmalloc(sizeof(struct dvb_device), GFP_KERNEL);
290 mutex_unlock(&dvbdev_register_lock);
294 dvbdevfops = kzalloc(sizeof(struct file_operations), GFP_KERNEL);
298 mutex_unlock(&dvbdev_register_lock);
302 memcpy(dvbdev, template, sizeof(struct dvb_device));
305 dvbdev->adapter = adap;
307 dvbdev->fops = dvbdevfops;
308 init_waitqueue_head (&dvbdev->wait_queue);
310 memcpy(dvbdevfops, template->fops, sizeof(struct file_operations));
311 dvbdevfops->owner = adap->module;
313 list_add_tail (&dvbdev->list_head, &adap->device_list);
315 down_write(&minor_rwsem);
316 #ifdef CONFIG_DVB_DYNAMIC_MINORS
317 for (minor = 0; minor < MAX_DVB_MINORS; minor++)
318 if (dvb_minors[minor] == NULL)
321 if (minor == MAX_DVB_MINORS) {
324 up_write(&minor_rwsem);
325 mutex_unlock(&dvbdev_register_lock);
329 minor = nums2minor(adap->num, type, id);
332 dvbdev->minor = minor;
333 dvb_minors[minor] = dvbdev;
334 up_write(&minor_rwsem);
336 mutex_unlock(&dvbdev_register_lock);
338 clsdev = device_create(dvb_class, adap->device,
339 MKDEV(DVB_MAJOR, minor),
340 dvbdev, "dvb%d.%s%d", adap->num, dnames[type], id);
341 if (IS_ERR(clsdev)) {
342 printk(KERN_ERR "%s: failed to create device dvb%d.%s%d (%ld)\n",
343 __func__, adap->num, dnames[type], id, PTR_ERR(clsdev));
344 return PTR_ERR(clsdev);
346 dprintk(KERN_DEBUG "DVB: register adapter%d/%s%d @ minor: %i (0x%02x)\n",
347 adap->num, dnames[type], id, minor, minor);
349 dvb_register_media_device(dvbdev, type, minor);
353 EXPORT_SYMBOL(dvb_register_device);
356 void dvb_unregister_device(struct dvb_device *dvbdev)
361 down_write(&minor_rwsem);
362 dvb_minors[dvbdev->minor] = NULL;
363 up_write(&minor_rwsem);
365 device_destroy(dvb_class, MKDEV(DVB_MAJOR, dvbdev->minor));
367 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
368 if (dvbdev->entity) {
369 media_device_unregister_entity(dvbdev->entity);
370 kfree(dvbdev->entity);
375 list_del (&dvbdev->list_head);
376 kfree (dvbdev->fops);
379 EXPORT_SYMBOL(dvb_unregister_device);
381 static int dvbdev_check_free_adapter_num(int num)
383 struct list_head *entry;
384 list_for_each(entry, &dvb_adapter_list) {
385 struct dvb_adapter *adap;
386 adap = list_entry(entry, struct dvb_adapter, list_head);
387 if (adap->num == num)
393 static int dvbdev_get_free_adapter_num (void)
397 while (num < DVB_MAX_ADAPTERS) {
398 if (dvbdev_check_free_adapter_num(num))
407 int dvb_register_adapter(struct dvb_adapter *adap, const char *name,
408 struct module *module, struct device *device,
413 mutex_lock(&dvbdev_register_lock);
415 for (i = 0; i < DVB_MAX_ADAPTERS; ++i) {
416 num = adapter_nums[i];
417 if (num >= 0 && num < DVB_MAX_ADAPTERS) {
418 /* use the one the driver asked for */
419 if (dvbdev_check_free_adapter_num(num))
422 num = dvbdev_get_free_adapter_num();
429 mutex_unlock(&dvbdev_register_lock);
433 memset (adap, 0, sizeof(struct dvb_adapter));
434 INIT_LIST_HEAD (&adap->device_list);
436 printk(KERN_INFO "DVB: registering new adapter (%s)\n", name);
440 adap->module = module;
441 adap->device = device;
442 adap->mfe_shared = 0;
443 adap->mfe_dvbdev = NULL;
444 mutex_init (&adap->mfe_lock);
446 list_add_tail (&adap->list_head, &dvb_adapter_list);
448 mutex_unlock(&dvbdev_register_lock);
452 EXPORT_SYMBOL(dvb_register_adapter);
455 int dvb_unregister_adapter(struct dvb_adapter *adap)
457 mutex_lock(&dvbdev_register_lock);
458 list_del (&adap->list_head);
459 mutex_unlock(&dvbdev_register_lock);
462 EXPORT_SYMBOL(dvb_unregister_adapter);
464 /* if the miracle happens and "generic_usercopy()" is included into
465 the kernel, then this can vanish. please don't make the mistake and
466 define this as video_usercopy(). this will introduce a dependecy
467 to the v4l "videodev.o" module, which is unnecessary for some
468 cards (ie. the budget dvb-cards don't need the v4l module...) */
469 int dvb_usercopy(struct file *file,
470 unsigned int cmd, unsigned long arg,
471 int (*func)(struct file *file,
472 unsigned int cmd, void *arg))
479 /* Copy arguments into temp kernel buffer */
480 switch (_IOC_DIR(cmd)) {
483 * For this command, the pointer is actually an integer
488 case _IOC_READ: /* some v4l ioctls are marked wrong ... */
490 case (_IOC_WRITE | _IOC_READ):
491 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
494 /* too big to allocate from stack */
495 mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
502 if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
508 if ((err = func(file, cmd, parg)) == -ENOIOCTLCMD)
514 /* Copy results into user buffer */
515 switch (_IOC_DIR(cmd))
518 case (_IOC_WRITE | _IOC_READ):
519 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
529 static int dvb_uevent(struct device *dev, struct kobj_uevent_env *env)
531 struct dvb_device *dvbdev = dev_get_drvdata(dev);
533 add_uevent_var(env, "DVB_ADAPTER_NUM=%d", dvbdev->adapter->num);
534 add_uevent_var(env, "DVB_DEVICE_TYPE=%s", dnames[dvbdev->type]);
535 add_uevent_var(env, "DVB_DEVICE_NUM=%d", dvbdev->id);
539 static char *dvb_devnode(struct device *dev, umode_t *mode)
541 struct dvb_device *dvbdev = dev_get_drvdata(dev);
543 return kasprintf(GFP_KERNEL, "dvb/adapter%d/%s%d",
544 dvbdev->adapter->num, dnames[dvbdev->type], dvbdev->id);
548 static int __init init_dvbdev(void)
551 dev_t dev = MKDEV(DVB_MAJOR, 0);
553 if ((retval = register_chrdev_region(dev, MAX_DVB_MINORS, "DVB")) != 0) {
554 printk(KERN_ERR "dvb-core: unable to get major %d\n", DVB_MAJOR);
558 cdev_init(&dvb_device_cdev, &dvb_device_fops);
559 if ((retval = cdev_add(&dvb_device_cdev, dev, MAX_DVB_MINORS)) != 0) {
560 printk(KERN_ERR "dvb-core: unable register character device\n");
564 dvb_class = class_create(THIS_MODULE, "dvb");
565 if (IS_ERR(dvb_class)) {
566 retval = PTR_ERR(dvb_class);
569 dvb_class->dev_uevent = dvb_uevent;
570 dvb_class->devnode = dvb_devnode;
574 cdev_del(&dvb_device_cdev);
575 unregister_chrdev_region(dev, MAX_DVB_MINORS);
580 static void __exit exit_dvbdev(void)
582 class_destroy(dvb_class);
583 cdev_del(&dvb_device_cdev);
584 unregister_chrdev_region(MKDEV(DVB_MAJOR, 0), MAX_DVB_MINORS);
587 subsys_initcall(init_dvbdev);
588 module_exit(exit_dvbdev);
590 MODULE_DESCRIPTION("DVB Core Driver");
591 MODULE_AUTHOR("Marcus Metzler, Ralph Metzler, Holger Waechtler");
592 MODULE_LICENSE("GPL");