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>
35 #include <linux/smp_lock.h>
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;
72 down_read(&minor_rwsem);
73 dvbdev = dvb_minors[iminor(inode)];
75 if (dvbdev && dvbdev->fops) {
77 const struct file_operations *old_fops;
79 file->private_data = dvbdev;
80 old_fops = file->f_op;
81 file->f_op = fops_get(dvbdev->fops);
82 if (file->f_op == NULL) {
83 file->f_op = old_fops;
87 err = file->f_op->open(inode,file);
90 file->f_op = fops_get(old_fops);
93 up_read(&minor_rwsem);
98 up_read(&minor_rwsem);
104 static const struct file_operations dvb_device_fops =
106 .owner = THIS_MODULE,
107 .open = dvb_device_open,
108 .llseek = noop_llseek,
111 static struct cdev dvb_device_cdev;
113 int dvb_generic_open(struct inode *inode, struct file *file)
115 struct dvb_device *dvbdev = file->private_data;
123 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
124 if (!dvbdev->readers)
128 if (!dvbdev->writers)
136 EXPORT_SYMBOL(dvb_generic_open);
139 int dvb_generic_release(struct inode *inode, struct file *file)
141 struct dvb_device *dvbdev = file->private_data;
146 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
155 EXPORT_SYMBOL(dvb_generic_release);
158 long dvb_generic_ioctl(struct file *file,
159 unsigned int cmd, unsigned long arg)
161 struct dvb_device *dvbdev = file->private_data;
167 if (!dvbdev->kernel_ioctl)
171 ret = dvb_usercopy(file, cmd, arg, dvbdev->kernel_ioctl);
176 EXPORT_SYMBOL(dvb_generic_ioctl);
179 static int dvbdev_get_free_id (struct dvb_adapter *adap, int type)
183 while (id < DVB_MAX_IDS) {
184 struct dvb_device *dev;
185 list_for_each_entry(dev, &adap->device_list, list_head)
186 if (dev->type == type && dev->id == id)
196 int dvb_register_device(struct dvb_adapter *adap, struct dvb_device **pdvbdev,
197 const struct dvb_device *template, void *priv, int type)
199 struct dvb_device *dvbdev;
200 struct file_operations *dvbdevfops;
201 struct device *clsdev;
205 mutex_lock(&dvbdev_register_lock);
207 if ((id = dvbdev_get_free_id (adap, type)) < 0){
208 mutex_unlock(&dvbdev_register_lock);
210 printk(KERN_ERR "%s: couldn't find free device id\n", __func__);
214 *pdvbdev = dvbdev = kmalloc(sizeof(struct dvb_device), GFP_KERNEL);
217 mutex_unlock(&dvbdev_register_lock);
221 dvbdevfops = kzalloc(sizeof(struct file_operations), GFP_KERNEL);
225 mutex_unlock(&dvbdev_register_lock);
229 memcpy(dvbdev, template, sizeof(struct dvb_device));
232 dvbdev->adapter = adap;
234 dvbdev->fops = dvbdevfops;
235 init_waitqueue_head (&dvbdev->wait_queue);
237 memcpy(dvbdevfops, template->fops, sizeof(struct file_operations));
238 dvbdevfops->owner = adap->module;
240 list_add_tail (&dvbdev->list_head, &adap->device_list);
242 down_write(&minor_rwsem);
243 #ifdef CONFIG_DVB_DYNAMIC_MINORS
244 for (minor = 0; minor < MAX_DVB_MINORS; minor++)
245 if (dvb_minors[minor] == NULL)
248 if (minor == MAX_DVB_MINORS) {
251 mutex_unlock(&dvbdev_register_lock);
255 minor = nums2minor(adap->num, type, id);
258 dvbdev->minor = minor;
259 dvb_minors[minor] = dvbdev;
260 up_write(&minor_rwsem);
262 mutex_unlock(&dvbdev_register_lock);
264 clsdev = device_create(dvb_class, adap->device,
265 MKDEV(DVB_MAJOR, minor),
266 dvbdev, "dvb%d.%s%d", adap->num, dnames[type], id);
267 if (IS_ERR(clsdev)) {
268 printk(KERN_ERR "%s: failed to create device dvb%d.%s%d (%ld)\n",
269 __func__, adap->num, dnames[type], id, PTR_ERR(clsdev));
270 return PTR_ERR(clsdev);
273 dprintk(KERN_DEBUG "DVB: register adapter%d/%s%d @ minor: %i (0x%02x)\n",
274 adap->num, dnames[type], id, minor, minor);
278 EXPORT_SYMBOL(dvb_register_device);
281 void dvb_unregister_device(struct dvb_device *dvbdev)
286 down_write(&minor_rwsem);
287 dvb_minors[dvbdev->minor] = NULL;
288 up_write(&minor_rwsem);
290 device_destroy(dvb_class, MKDEV(DVB_MAJOR, dvbdev->minor));
292 list_del (&dvbdev->list_head);
293 kfree (dvbdev->fops);
296 EXPORT_SYMBOL(dvb_unregister_device);
298 static int dvbdev_check_free_adapter_num(int num)
300 struct list_head *entry;
301 list_for_each(entry, &dvb_adapter_list) {
302 struct dvb_adapter *adap;
303 adap = list_entry(entry, struct dvb_adapter, list_head);
304 if (adap->num == num)
310 static int dvbdev_get_free_adapter_num (void)
314 while (num < DVB_MAX_ADAPTERS) {
315 if (dvbdev_check_free_adapter_num(num))
324 int dvb_register_adapter(struct dvb_adapter *adap, const char *name,
325 struct module *module, struct device *device,
330 mutex_lock(&dvbdev_register_lock);
332 for (i = 0; i < DVB_MAX_ADAPTERS; ++i) {
333 num = adapter_nums[i];
334 if (num >= 0 && num < DVB_MAX_ADAPTERS) {
335 /* use the one the driver asked for */
336 if (dvbdev_check_free_adapter_num(num))
339 num = dvbdev_get_free_adapter_num();
346 mutex_unlock(&dvbdev_register_lock);
350 memset (adap, 0, sizeof(struct dvb_adapter));
351 INIT_LIST_HEAD (&adap->device_list);
353 printk(KERN_INFO "DVB: registering new adapter (%s)\n", name);
357 adap->module = module;
358 adap->device = device;
359 adap->mfe_shared = 0;
360 adap->mfe_dvbdev = NULL;
361 mutex_init (&adap->mfe_lock);
363 list_add_tail (&adap->list_head, &dvb_adapter_list);
365 mutex_unlock(&dvbdev_register_lock);
369 EXPORT_SYMBOL(dvb_register_adapter);
372 int dvb_unregister_adapter(struct dvb_adapter *adap)
374 mutex_lock(&dvbdev_register_lock);
375 list_del (&adap->list_head);
376 mutex_unlock(&dvbdev_register_lock);
379 EXPORT_SYMBOL(dvb_unregister_adapter);
381 /* if the miracle happens and "generic_usercopy()" is included into
382 the kernel, then this can vanish. please don't make the mistake and
383 define this as video_usercopy(). this will introduce a dependecy
384 to the v4l "videodev.o" module, which is unnecessary for some
385 cards (ie. the budget dvb-cards don't need the v4l module...) */
386 int dvb_usercopy(struct file *file,
387 unsigned int cmd, unsigned long arg,
388 int (*func)(struct file *file,
389 unsigned int cmd, void *arg))
396 /* Copy arguments into temp kernel buffer */
397 switch (_IOC_DIR(cmd)) {
400 * For this command, the pointer is actually an integer
405 case _IOC_READ: /* some v4l ioctls are marked wrong ... */
407 case (_IOC_WRITE | _IOC_READ):
408 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
411 /* too big to allocate from stack */
412 mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
419 if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
425 if ((err = func(file, cmd, parg)) == -ENOIOCTLCMD)
431 /* Copy results into user buffer */
432 switch (_IOC_DIR(cmd))
435 case (_IOC_WRITE | _IOC_READ):
436 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
446 static int dvb_uevent(struct device *dev, struct kobj_uevent_env *env)
448 struct dvb_device *dvbdev = dev_get_drvdata(dev);
450 add_uevent_var(env, "DVB_ADAPTER_NUM=%d", dvbdev->adapter->num);
451 add_uevent_var(env, "DVB_DEVICE_TYPE=%s", dnames[dvbdev->type]);
452 add_uevent_var(env, "DVB_DEVICE_NUM=%d", dvbdev->id);
456 static char *dvb_devnode(struct device *dev, mode_t *mode)
458 struct dvb_device *dvbdev = dev_get_drvdata(dev);
460 return kasprintf(GFP_KERNEL, "dvb/adapter%d/%s%d",
461 dvbdev->adapter->num, dnames[dvbdev->type], dvbdev->id);
465 static int __init init_dvbdev(void)
468 dev_t dev = MKDEV(DVB_MAJOR, 0);
470 if ((retval = register_chrdev_region(dev, MAX_DVB_MINORS, "DVB")) != 0) {
471 printk(KERN_ERR "dvb-core: unable to get major %d\n", DVB_MAJOR);
475 cdev_init(&dvb_device_cdev, &dvb_device_fops);
476 if ((retval = cdev_add(&dvb_device_cdev, dev, MAX_DVB_MINORS)) != 0) {
477 printk(KERN_ERR "dvb-core: unable register character device\n");
481 dvb_class = class_create(THIS_MODULE, "dvb");
482 if (IS_ERR(dvb_class)) {
483 retval = PTR_ERR(dvb_class);
486 dvb_class->dev_uevent = dvb_uevent;
487 dvb_class->devnode = dvb_devnode;
491 cdev_del(&dvb_device_cdev);
492 unregister_chrdev_region(dev, MAX_DVB_MINORS);
497 static void __exit exit_dvbdev(void)
499 class_destroy(dvb_class);
500 cdev_del(&dvb_device_cdev);
501 unregister_chrdev_region(MKDEV(DVB_MAJOR, 0), MAX_DVB_MINORS);
504 subsys_initcall(init_dvbdev);
505 module_exit(exit_dvbdev);
507 MODULE_DESCRIPTION("DVB Core Driver");
508 MODULE_AUTHOR("Marcus Metzler, Ralph Metzler, Holger Waechtler");
509 MODULE_LICENSE("GPL");