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 int dvbdev_debug;
39 module_param(dvbdev_debug, int, 0644);
40 MODULE_PARM_DESC(dvbdev_debug, "Turn on/off device debugging (default:off).");
42 #define dprintk if (dvbdev_debug) printk
44 static LIST_HEAD(dvb_adapter_list);
45 static DEFINE_MUTEX(dvbdev_register_lock);
47 static const char * const dnames[] = {
48 "video", "audio", "sec", "frontend", "demux", "dvr", "ca",
52 #define DVB_MAX_ADAPTERS 8
54 #define nums2minor(num,type,id) ((num << 6) | (id << 4) | type)
55 #define MAX_DVB_MINORS (DVB_MAX_ADAPTERS*64)
57 static struct class *dvb_class;
59 static struct dvb_device* dvbdev_find_device (int minor)
61 struct dvb_adapter *adap;
63 list_for_each_entry(adap, &dvb_adapter_list, list_head) {
64 struct dvb_device *dev;
65 list_for_each_entry(dev, &adap->device_list, list_head)
66 if (nums2minor(adap->num, dev->type, dev->id) == minor)
74 static int dvb_device_open(struct inode *inode, struct file *file)
76 struct dvb_device *dvbdev;
78 dvbdev = dvbdev_find_device (iminor(inode));
80 if (dvbdev && dvbdev->fops) {
82 const struct file_operations *old_fops;
84 file->private_data = dvbdev;
85 old_fops = file->f_op;
86 file->f_op = fops_get(dvbdev->fops);
88 err = file->f_op->open(inode,file);
91 file->f_op = fops_get(old_fops);
100 static struct file_operations dvb_device_fops =
102 .owner = THIS_MODULE,
103 .open = dvb_device_open,
106 static struct cdev dvb_device_cdev;
108 int dvb_generic_open(struct inode *inode, struct file *file)
110 struct dvb_device *dvbdev = file->private_data;
118 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
119 if (!dvbdev->readers)
123 if (!dvbdev->writers)
131 EXPORT_SYMBOL(dvb_generic_open);
134 int dvb_generic_release(struct inode *inode, struct file *file)
136 struct dvb_device *dvbdev = file->private_data;
141 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
150 EXPORT_SYMBOL(dvb_generic_release);
153 int dvb_generic_ioctl(struct inode *inode, struct file *file,
154 unsigned int cmd, unsigned long arg)
156 struct dvb_device *dvbdev = file->private_data;
161 if (!dvbdev->kernel_ioctl)
164 return dvb_usercopy (inode, file, cmd, arg, dvbdev->kernel_ioctl);
166 EXPORT_SYMBOL(dvb_generic_ioctl);
169 static int dvbdev_get_free_id (struct dvb_adapter *adap, int type)
173 while (id < DVB_MAX_IDS) {
174 struct dvb_device *dev;
175 list_for_each_entry(dev, &adap->device_list, list_head)
176 if (dev->type == type && dev->id == id)
186 int dvb_register_device(struct dvb_adapter *adap, struct dvb_device **pdvbdev,
187 const struct dvb_device *template, void *priv, int type)
189 struct dvb_device *dvbdev;
190 struct file_operations *dvbdevfops;
191 struct device *clsdev;
194 mutex_lock(&dvbdev_register_lock);
196 if ((id = dvbdev_get_free_id (adap, type)) < 0){
197 mutex_unlock(&dvbdev_register_lock);
199 printk(KERN_ERR "%s: couldn't find free device id\n", __FUNCTION__);
203 *pdvbdev = dvbdev = kmalloc(sizeof(struct dvb_device), GFP_KERNEL);
206 mutex_unlock(&dvbdev_register_lock);
210 dvbdevfops = kzalloc(sizeof(struct file_operations), GFP_KERNEL);
214 mutex_unlock(&dvbdev_register_lock);
218 memcpy(dvbdev, template, sizeof(struct dvb_device));
221 dvbdev->adapter = adap;
223 dvbdev->fops = dvbdevfops;
224 init_waitqueue_head (&dvbdev->wait_queue);
226 memcpy(dvbdev->fops, template->fops, sizeof(struct file_operations));
227 dvbdev->fops->owner = adap->module;
229 list_add_tail (&dvbdev->list_head, &adap->device_list);
231 mutex_unlock(&dvbdev_register_lock);
233 clsdev = device_create(dvb_class, adap->device,
234 MKDEV(DVB_MAJOR, nums2minor(adap->num, type, id)),
235 "dvb%d.%s%d", adap->num, dnames[type], id);
236 if (IS_ERR(clsdev)) {
237 printk(KERN_ERR "%s: failed to create device dvb%d.%s%d (%ld)\n",
238 __FUNCTION__, adap->num, dnames[type], id, PTR_ERR(clsdev));
239 return PTR_ERR(clsdev);
242 dprintk(KERN_DEBUG "DVB: register adapter%d/%s%d @ minor: %i (0x%02x)\n",
243 adap->num, dnames[type], id, nums2minor(adap->num, type, id),
244 nums2minor(adap->num, type, id));
248 EXPORT_SYMBOL(dvb_register_device);
251 void dvb_unregister_device(struct dvb_device *dvbdev)
256 device_destroy(dvb_class, MKDEV(DVB_MAJOR, nums2minor(dvbdev->adapter->num,
257 dvbdev->type, dvbdev->id)));
259 list_del (&dvbdev->list_head);
260 kfree (dvbdev->fops);
263 EXPORT_SYMBOL(dvb_unregister_device);
266 static int dvbdev_get_free_adapter_num (void)
270 while (num < DVB_MAX_ADAPTERS) {
271 struct dvb_adapter *adap;
272 list_for_each_entry(adap, &dvb_adapter_list, list_head)
273 if (adap->num == num)
284 int dvb_register_adapter(struct dvb_adapter *adap, const char *name, struct module *module, struct device *device)
288 mutex_lock(&dvbdev_register_lock);
290 if ((num = dvbdev_get_free_adapter_num ()) < 0) {
291 mutex_unlock(&dvbdev_register_lock);
295 memset (adap, 0, sizeof(struct dvb_adapter));
296 INIT_LIST_HEAD (&adap->device_list);
298 printk(KERN_INFO "DVB: registering new adapter (%s)\n", name);
302 adap->module = module;
303 adap->device = device;
305 list_add_tail (&adap->list_head, &dvb_adapter_list);
307 mutex_unlock(&dvbdev_register_lock);
311 EXPORT_SYMBOL(dvb_register_adapter);
314 int dvb_unregister_adapter(struct dvb_adapter *adap)
316 mutex_lock(&dvbdev_register_lock);
317 list_del (&adap->list_head);
318 mutex_unlock(&dvbdev_register_lock);
321 EXPORT_SYMBOL(dvb_unregister_adapter);
323 /* if the miracle happens and "generic_usercopy()" is included into
324 the kernel, then this can vanish. please don't make the mistake and
325 define this as video_usercopy(). this will introduce a dependecy
326 to the v4l "videodev.o" module, which is unnecessary for some
327 cards (ie. the budget dvb-cards don't need the v4l module...) */
328 int dvb_usercopy(struct inode *inode, struct file *file,
329 unsigned int cmd, unsigned long arg,
330 int (*func)(struct inode *inode, struct file *file,
331 unsigned int cmd, void *arg))
338 /* Copy arguments into temp kernel buffer */
339 switch (_IOC_DIR(cmd)) {
342 * For this command, the pointer is actually an integer
347 case _IOC_READ: /* some v4l ioctls are marked wrong ... */
349 case (_IOC_WRITE | _IOC_READ):
350 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
353 /* too big to allocate from stack */
354 mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
361 if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
367 if ((err = func(inode, file, cmd, parg)) == -ENOIOCTLCMD)
373 /* Copy results into user buffer */
374 switch (_IOC_DIR(cmd))
377 case (_IOC_WRITE | _IOC_READ):
378 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
388 static int __init init_dvbdev(void)
391 dev_t dev = MKDEV(DVB_MAJOR, 0);
393 if ((retval = register_chrdev_region(dev, MAX_DVB_MINORS, "DVB")) != 0) {
394 printk(KERN_ERR "dvb-core: unable to get major %d\n", DVB_MAJOR);
398 cdev_init(&dvb_device_cdev, &dvb_device_fops);
399 if ((retval = cdev_add(&dvb_device_cdev, dev, MAX_DVB_MINORS)) != 0) {
400 printk(KERN_ERR "dvb-core: unable register character device\n");
404 dvb_class = class_create(THIS_MODULE, "dvb");
405 if (IS_ERR(dvb_class)) {
406 retval = PTR_ERR(dvb_class);
412 cdev_del(&dvb_device_cdev);
413 unregister_chrdev_region(dev, MAX_DVB_MINORS);
418 static void __exit exit_dvbdev(void)
420 class_destroy(dvb_class);
421 cdev_del(&dvb_device_cdev);
422 unregister_chrdev_region(MKDEV(DVB_MAJOR, 0), MAX_DVB_MINORS);
425 subsys_initcall(init_dvbdev);
426 module_exit(exit_dvbdev);
428 MODULE_DESCRIPTION("DVB Core Driver");
429 MODULE_AUTHOR("Marcus Metzler, Ralph Metzler, Holger Waechtler");
430 MODULE_LICENSE("GPL");