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",
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
;
79 dvbdev
= dvbdev_find_device (iminor(inode
));
81 if (dvbdev
&& dvbdev
->fops
) {
83 const struct file_operations
*old_fops
;
85 file
->private_data
= dvbdev
;
86 old_fops
= file
->f_op
;
87 file
->f_op
= fops_get(dvbdev
->fops
);
89 err
= file
->f_op
->open(inode
,file
);
92 file
->f_op
= fops_get(old_fops
);
103 static const struct file_operations dvb_device_fops
=
105 .owner
= THIS_MODULE
,
106 .open
= dvb_device_open
,
109 static struct cdev dvb_device_cdev
;
111 int dvb_generic_open(struct inode
*inode
, struct file
*file
)
113 struct dvb_device
*dvbdev
= file
->private_data
;
121 if ((file
->f_flags
& O_ACCMODE
) == O_RDONLY
) {
122 if (!dvbdev
->readers
)
126 if (!dvbdev
->writers
)
134 EXPORT_SYMBOL(dvb_generic_open
);
137 int dvb_generic_release(struct inode
*inode
, struct file
*file
)
139 struct dvb_device
*dvbdev
= file
->private_data
;
144 if ((file
->f_flags
& O_ACCMODE
) == O_RDONLY
) {
153 EXPORT_SYMBOL(dvb_generic_release
);
156 int dvb_generic_ioctl(struct inode
*inode
, struct file
*file
,
157 unsigned int cmd
, unsigned long arg
)
159 struct dvb_device
*dvbdev
= file
->private_data
;
164 if (!dvbdev
->kernel_ioctl
)
167 return dvb_usercopy (inode
, file
, cmd
, arg
, dvbdev
->kernel_ioctl
);
169 EXPORT_SYMBOL(dvb_generic_ioctl
);
172 static int dvbdev_get_free_id (struct dvb_adapter
*adap
, int type
)
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
)
189 int dvb_register_device(struct dvb_adapter
*adap
, struct dvb_device
**pdvbdev
,
190 const struct dvb_device
*template, void *priv
, int type
)
192 struct dvb_device
*dvbdev
;
193 struct file_operations
*dvbdevfops
;
194 struct device
*clsdev
;
197 mutex_lock(&dvbdev_register_lock
);
199 if ((id
= dvbdev_get_free_id (adap
, type
)) < 0){
200 mutex_unlock(&dvbdev_register_lock
);
202 printk(KERN_ERR
"%s: couldn't find free device id\n", __func__
);
206 *pdvbdev
= dvbdev
= kmalloc(sizeof(struct dvb_device
), GFP_KERNEL
);
209 mutex_unlock(&dvbdev_register_lock
);
213 dvbdevfops
= kzalloc(sizeof(struct file_operations
), GFP_KERNEL
);
217 mutex_unlock(&dvbdev_register_lock
);
221 memcpy(dvbdev
, template, sizeof(struct dvb_device
));
224 dvbdev
->adapter
= adap
;
226 dvbdev
->fops
= dvbdevfops
;
227 init_waitqueue_head (&dvbdev
->wait_queue
);
229 memcpy(dvbdev
->fops
, template->fops
, sizeof(struct file_operations
));
230 dvbdev
->fops
->owner
= adap
->module
;
232 list_add_tail (&dvbdev
->list_head
, &adap
->device_list
);
234 mutex_unlock(&dvbdev_register_lock
);
236 clsdev
= device_create_drvdata(dvb_class
, adap
->device
,
237 MKDEV(DVB_MAJOR
, nums2minor(adap
->num
, type
, id
)),
238 NULL
, "dvb%d.%s%d", adap
->num
, dnames
[type
], id
);
239 if (IS_ERR(clsdev
)) {
240 printk(KERN_ERR
"%s: failed to create device dvb%d.%s%d (%ld)\n",
241 __func__
, adap
->num
, dnames
[type
], id
, PTR_ERR(clsdev
));
242 return PTR_ERR(clsdev
);
245 dprintk(KERN_DEBUG
"DVB: register adapter%d/%s%d @ minor: %i (0x%02x)\n",
246 adap
->num
, dnames
[type
], id
, nums2minor(adap
->num
, type
, id
),
247 nums2minor(adap
->num
, type
, id
));
251 EXPORT_SYMBOL(dvb_register_device
);
254 void dvb_unregister_device(struct dvb_device
*dvbdev
)
259 device_destroy(dvb_class
, MKDEV(DVB_MAJOR
, nums2minor(dvbdev
->adapter
->num
,
260 dvbdev
->type
, dvbdev
->id
)));
262 list_del (&dvbdev
->list_head
);
263 kfree (dvbdev
->fops
);
266 EXPORT_SYMBOL(dvb_unregister_device
);
268 static int dvbdev_check_free_adapter_num(int num
)
270 struct list_head
*entry
;
271 list_for_each(entry
, &dvb_adapter_list
) {
272 struct dvb_adapter
*adap
;
273 adap
= list_entry(entry
, struct dvb_adapter
, list_head
);
274 if (adap
->num
== num
)
280 static int dvbdev_get_free_adapter_num (void)
284 while (num
< DVB_MAX_ADAPTERS
) {
285 if (dvbdev_check_free_adapter_num(num
))
294 int dvb_register_adapter(struct dvb_adapter
*adap
, const char *name
,
295 struct module
*module
, struct device
*device
,
300 mutex_lock(&dvbdev_register_lock
);
302 for (i
= 0; i
< DVB_MAX_ADAPTERS
; ++i
) {
303 num
= adapter_nums
[i
];
304 if (num
>= 0 && num
< DVB_MAX_ADAPTERS
) {
305 /* use the one the driver asked for */
306 if (dvbdev_check_free_adapter_num(num
))
309 num
= dvbdev_get_free_adapter_num();
316 mutex_unlock(&dvbdev_register_lock
);
320 memset (adap
, 0, sizeof(struct dvb_adapter
));
321 INIT_LIST_HEAD (&adap
->device_list
);
323 printk(KERN_INFO
"DVB: registering new adapter (%s)\n", name
);
327 adap
->module
= module
;
328 adap
->device
= device
;
330 list_add_tail (&adap
->list_head
, &dvb_adapter_list
);
332 mutex_unlock(&dvbdev_register_lock
);
336 EXPORT_SYMBOL(dvb_register_adapter
);
339 int dvb_unregister_adapter(struct dvb_adapter
*adap
)
341 mutex_lock(&dvbdev_register_lock
);
342 list_del (&adap
->list_head
);
343 mutex_unlock(&dvbdev_register_lock
);
346 EXPORT_SYMBOL(dvb_unregister_adapter
);
348 /* if the miracle happens and "generic_usercopy()" is included into
349 the kernel, then this can vanish. please don't make the mistake and
350 define this as video_usercopy(). this will introduce a dependecy
351 to the v4l "videodev.o" module, which is unnecessary for some
352 cards (ie. the budget dvb-cards don't need the v4l module...) */
353 int dvb_usercopy(struct inode
*inode
, struct file
*file
,
354 unsigned int cmd
, unsigned long arg
,
355 int (*func
)(struct inode
*inode
, struct file
*file
,
356 unsigned int cmd
, void *arg
))
363 /* Copy arguments into temp kernel buffer */
364 switch (_IOC_DIR(cmd
)) {
367 * For this command, the pointer is actually an integer
372 case _IOC_READ
: /* some v4l ioctls are marked wrong ... */
374 case (_IOC_WRITE
| _IOC_READ
):
375 if (_IOC_SIZE(cmd
) <= sizeof(sbuf
)) {
378 /* too big to allocate from stack */
379 mbuf
= kmalloc(_IOC_SIZE(cmd
),GFP_KERNEL
);
386 if (copy_from_user(parg
, (void __user
*)arg
, _IOC_SIZE(cmd
)))
392 if ((err
= func(inode
, file
, cmd
, parg
)) == -ENOIOCTLCMD
)
398 /* Copy results into user buffer */
399 switch (_IOC_DIR(cmd
))
402 case (_IOC_WRITE
| _IOC_READ
):
403 if (copy_to_user((void __user
*)arg
, parg
, _IOC_SIZE(cmd
)))
413 static int __init
init_dvbdev(void)
416 dev_t dev
= MKDEV(DVB_MAJOR
, 0);
418 if ((retval
= register_chrdev_region(dev
, MAX_DVB_MINORS
, "DVB")) != 0) {
419 printk(KERN_ERR
"dvb-core: unable to get major %d\n", DVB_MAJOR
);
423 cdev_init(&dvb_device_cdev
, &dvb_device_fops
);
424 if ((retval
= cdev_add(&dvb_device_cdev
, dev
, MAX_DVB_MINORS
)) != 0) {
425 printk(KERN_ERR
"dvb-core: unable register character device\n");
429 dvb_class
= class_create(THIS_MODULE
, "dvb");
430 if (IS_ERR(dvb_class
)) {
431 retval
= PTR_ERR(dvb_class
);
437 cdev_del(&dvb_device_cdev
);
438 unregister_chrdev_region(dev
, MAX_DVB_MINORS
);
443 static void __exit
exit_dvbdev(void)
445 class_destroy(dvb_class
);
446 cdev_del(&dvb_device_cdev
);
447 unregister_chrdev_region(MKDEV(DVB_MAJOR
, 0), MAX_DVB_MINORS
);
450 subsys_initcall(init_dvbdev
);
451 module_exit(exit_dvbdev
);
453 MODULE_DESCRIPTION("DVB Core Driver");
454 MODULE_AUTHOR("Marcus Metzler, Ralph Metzler, Holger Waechtler");
455 MODULE_LICENSE("GPL");