GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / media / dvb / dvb-core / dvbdev.c
blobb915c39d782f1edc994472d8788aaedc0b722b8c
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 #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>
32 #include <linux/fs.h>
33 #include <linux/cdev.h>
34 #include <linux/mutex.h>
35 #include <linux/smp_lock.h>
36 #include "dvbdev.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",
50 "net", "osd"
53 #ifdef CONFIG_DVB_DYNAMIC_MINORS
54 #define MAX_DVB_MINORS 256
55 #define DVB_MAX_IDS MAX_DVB_MINORS
56 #else
57 #define DVB_MAX_IDS 4
58 #define nums2minor(num,type,id) ((num << 6) | (id << 4) | type)
59 #define MAX_DVB_MINORS (DVB_MAX_ADAPTERS*64)
60 #endif
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 lock_kernel();
72 down_read(&minor_rwsem);
73 dvbdev = dvb_minors[iminor(inode)];
75 if (dvbdev && dvbdev->fops) {
76 int err = 0;
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;
84 goto fail;
86 if(file->f_op->open)
87 err = file->f_op->open(inode,file);
88 if (err) {
89 fops_put(file->f_op);
90 file->f_op = fops_get(old_fops);
92 fops_put(old_fops);
93 up_read(&minor_rwsem);
94 unlock_kernel();
95 return err;
97 fail:
98 up_read(&minor_rwsem);
99 unlock_kernel();
100 return -ENODEV;
104 static const struct file_operations dvb_device_fops =
106 .owner = THIS_MODULE,
107 .open = dvb_device_open,
110 static struct cdev dvb_device_cdev;
112 int dvb_generic_open(struct inode *inode, struct file *file)
114 struct dvb_device *dvbdev = file->private_data;
116 if (!dvbdev)
117 return -ENODEV;
119 if (!dvbdev->users)
120 return -EBUSY;
122 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
123 if (!dvbdev->readers)
124 return -EBUSY;
125 dvbdev->readers--;
126 } else {
127 if (!dvbdev->writers)
128 return -EBUSY;
129 dvbdev->writers--;
132 dvbdev->users--;
133 return 0;
135 EXPORT_SYMBOL(dvb_generic_open);
138 int dvb_generic_release(struct inode *inode, struct file *file)
140 struct dvb_device *dvbdev = file->private_data;
142 if (!dvbdev)
143 return -ENODEV;
145 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
146 dvbdev->readers++;
147 } else {
148 dvbdev->writers++;
151 dvbdev->users++;
152 return 0;
154 EXPORT_SYMBOL(dvb_generic_release);
157 long dvb_generic_ioctl(struct file *file,
158 unsigned int cmd, unsigned long arg)
160 struct dvb_device *dvbdev = file->private_data;
161 int ret;
163 if (!dvbdev)
164 return -ENODEV;
166 if (!dvbdev->kernel_ioctl)
167 return -EINVAL;
169 lock_kernel();
170 ret = dvb_usercopy(file, cmd, arg, dvbdev->kernel_ioctl);
171 unlock_kernel();
173 return ret;
175 EXPORT_SYMBOL(dvb_generic_ioctl);
178 static int dvbdev_get_free_id (struct dvb_adapter *adap, int type)
180 u32 id = 0;
182 while (id < DVB_MAX_IDS) {
183 struct dvb_device *dev;
184 list_for_each_entry(dev, &adap->device_list, list_head)
185 if (dev->type == type && dev->id == id)
186 goto skip;
187 return id;
188 skip:
189 id++;
191 return -ENFILE;
195 int dvb_register_device(struct dvb_adapter *adap, struct dvb_device **pdvbdev,
196 const struct dvb_device *template, void *priv, int type)
198 struct dvb_device *dvbdev;
199 struct file_operations *dvbdevfops;
200 struct device *clsdev;
201 int minor;
202 int id;
204 mutex_lock(&dvbdev_register_lock);
206 if ((id = dvbdev_get_free_id (adap, type)) < 0){
207 mutex_unlock(&dvbdev_register_lock);
208 *pdvbdev = NULL;
209 printk(KERN_ERR "%s: couldn't find free device id\n", __func__);
210 return -ENFILE;
213 *pdvbdev = dvbdev = kmalloc(sizeof(struct dvb_device), GFP_KERNEL);
215 if (!dvbdev){
216 mutex_unlock(&dvbdev_register_lock);
217 return -ENOMEM;
220 dvbdevfops = kzalloc(sizeof(struct file_operations), GFP_KERNEL);
222 if (!dvbdevfops){
223 kfree (dvbdev);
224 mutex_unlock(&dvbdev_register_lock);
225 return -ENOMEM;
228 memcpy(dvbdev, template, sizeof(struct dvb_device));
229 dvbdev->type = type;
230 dvbdev->id = id;
231 dvbdev->adapter = adap;
232 dvbdev->priv = priv;
233 dvbdev->fops = dvbdevfops;
234 init_waitqueue_head (&dvbdev->wait_queue);
236 memcpy(dvbdevfops, template->fops, sizeof(struct file_operations));
237 dvbdevfops->owner = adap->module;
239 list_add_tail (&dvbdev->list_head, &adap->device_list);
241 down_write(&minor_rwsem);
242 #ifdef CONFIG_DVB_DYNAMIC_MINORS
243 for (minor = 0; minor < MAX_DVB_MINORS; minor++)
244 if (dvb_minors[minor] == NULL)
245 break;
247 if (minor == MAX_DVB_MINORS) {
248 kfree(dvbdevfops);
249 kfree(dvbdev);
250 mutex_unlock(&dvbdev_register_lock);
251 return -EINVAL;
253 #else
254 minor = nums2minor(adap->num, type, id);
255 #endif
257 dvbdev->minor = minor;
258 dvb_minors[minor] = dvbdev;
259 up_write(&minor_rwsem);
261 mutex_unlock(&dvbdev_register_lock);
263 clsdev = device_create(dvb_class, adap->device,
264 MKDEV(DVB_MAJOR, minor),
265 dvbdev, "dvb%d.%s%d", adap->num, dnames[type], id);
266 if (IS_ERR(clsdev)) {
267 printk(KERN_ERR "%s: failed to create device dvb%d.%s%d (%ld)\n",
268 __func__, adap->num, dnames[type], id, PTR_ERR(clsdev));
269 return PTR_ERR(clsdev);
272 dprintk(KERN_DEBUG "DVB: register adapter%d/%s%d @ minor: %i (0x%02x)\n",
273 adap->num, dnames[type], id, minor, minor);
275 return 0;
277 EXPORT_SYMBOL(dvb_register_device);
280 void dvb_unregister_device(struct dvb_device *dvbdev)
282 if (!dvbdev)
283 return;
285 down_write(&minor_rwsem);
286 dvb_minors[dvbdev->minor] = NULL;
287 up_write(&minor_rwsem);
289 device_destroy(dvb_class, MKDEV(DVB_MAJOR, dvbdev->minor));
291 list_del (&dvbdev->list_head);
292 kfree (dvbdev->fops);
293 kfree (dvbdev);
295 EXPORT_SYMBOL(dvb_unregister_device);
297 static int dvbdev_check_free_adapter_num(int num)
299 struct list_head *entry;
300 list_for_each(entry, &dvb_adapter_list) {
301 struct dvb_adapter *adap;
302 adap = list_entry(entry, struct dvb_adapter, list_head);
303 if (adap->num == num)
304 return 0;
306 return 1;
309 static int dvbdev_get_free_adapter_num (void)
311 int num = 0;
313 while (num < DVB_MAX_ADAPTERS) {
314 if (dvbdev_check_free_adapter_num(num))
315 return num;
316 num++;
319 return -ENFILE;
323 int dvb_register_adapter(struct dvb_adapter *adap, const char *name,
324 struct module *module, struct device *device,
325 short *adapter_nums)
327 int i, num;
329 mutex_lock(&dvbdev_register_lock);
331 for (i = 0; i < DVB_MAX_ADAPTERS; ++i) {
332 num = adapter_nums[i];
333 if (num >= 0 && num < DVB_MAX_ADAPTERS) {
334 /* use the one the driver asked for */
335 if (dvbdev_check_free_adapter_num(num))
336 break;
337 } else {
338 num = dvbdev_get_free_adapter_num();
339 break;
341 num = -1;
344 if (num < 0) {
345 mutex_unlock(&dvbdev_register_lock);
346 return -ENFILE;
349 memset (adap, 0, sizeof(struct dvb_adapter));
350 INIT_LIST_HEAD (&adap->device_list);
352 printk(KERN_INFO "DVB: registering new adapter (%s)\n", name);
354 adap->num = num;
355 adap->name = name;
356 adap->module = module;
357 adap->device = device;
358 adap->mfe_shared = 0;
359 adap->mfe_dvbdev = NULL;
360 mutex_init (&adap->mfe_lock);
362 list_add_tail (&adap->list_head, &dvb_adapter_list);
364 mutex_unlock(&dvbdev_register_lock);
366 return num;
368 EXPORT_SYMBOL(dvb_register_adapter);
371 int dvb_unregister_adapter(struct dvb_adapter *adap)
373 mutex_lock(&dvbdev_register_lock);
374 list_del (&adap->list_head);
375 mutex_unlock(&dvbdev_register_lock);
376 return 0;
378 EXPORT_SYMBOL(dvb_unregister_adapter);
380 /* if the miracle happens and "generic_usercopy()" is included into
381 the kernel, then this can vanish. please don't make the mistake and
382 define this as video_usercopy(). this will introduce a dependecy
383 to the v4l "videodev.o" module, which is unnecessary for some
384 cards (ie. the budget dvb-cards don't need the v4l module...) */
385 int dvb_usercopy(struct file *file,
386 unsigned int cmd, unsigned long arg,
387 int (*func)(struct file *file,
388 unsigned int cmd, void *arg))
390 char sbuf[128];
391 void *mbuf = NULL;
392 void *parg = NULL;
393 int err = -EINVAL;
395 /* Copy arguments into temp kernel buffer */
396 switch (_IOC_DIR(cmd)) {
397 case _IOC_NONE:
399 * For this command, the pointer is actually an integer
400 * argument.
402 parg = (void *) arg;
403 break;
404 case _IOC_READ: /* some v4l ioctls are marked wrong ... */
405 case _IOC_WRITE:
406 case (_IOC_WRITE | _IOC_READ):
407 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
408 parg = sbuf;
409 } else {
410 /* too big to allocate from stack */
411 mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
412 if (NULL == mbuf)
413 return -ENOMEM;
414 parg = mbuf;
417 err = -EFAULT;
418 if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
419 goto out;
420 break;
423 /* call driver */
424 if ((err = func(file, cmd, parg)) == -ENOIOCTLCMD)
425 err = -EINVAL;
427 if (err < 0)
428 goto out;
430 /* Copy results into user buffer */
431 switch (_IOC_DIR(cmd))
433 case _IOC_READ:
434 case (_IOC_WRITE | _IOC_READ):
435 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
436 err = -EFAULT;
437 break;
440 out:
441 kfree(mbuf);
442 return err;
445 static int dvb_uevent(struct device *dev, struct kobj_uevent_env *env)
447 struct dvb_device *dvbdev = dev_get_drvdata(dev);
449 add_uevent_var(env, "DVB_ADAPTER_NUM=%d", dvbdev->adapter->num);
450 add_uevent_var(env, "DVB_DEVICE_TYPE=%s", dnames[dvbdev->type]);
451 add_uevent_var(env, "DVB_DEVICE_NUM=%d", dvbdev->id);
452 return 0;
455 static char *dvb_devnode(struct device *dev, mode_t *mode)
457 struct dvb_device *dvbdev = dev_get_drvdata(dev);
459 return kasprintf(GFP_KERNEL, "dvb/adapter%d/%s%d",
460 dvbdev->adapter->num, dnames[dvbdev->type], dvbdev->id);
464 static int __init init_dvbdev(void)
466 int retval;
467 dev_t dev = MKDEV(DVB_MAJOR, 0);
469 if ((retval = register_chrdev_region(dev, MAX_DVB_MINORS, "DVB")) != 0) {
470 printk(KERN_ERR "dvb-core: unable to get major %d\n", DVB_MAJOR);
471 return retval;
474 cdev_init(&dvb_device_cdev, &dvb_device_fops);
475 if ((retval = cdev_add(&dvb_device_cdev, dev, MAX_DVB_MINORS)) != 0) {
476 printk(KERN_ERR "dvb-core: unable register character device\n");
477 goto error;
480 dvb_class = class_create(THIS_MODULE, "dvb");
481 if (IS_ERR(dvb_class)) {
482 retval = PTR_ERR(dvb_class);
483 goto error;
485 dvb_class->dev_uevent = dvb_uevent;
486 dvb_class->devnode = dvb_devnode;
487 return 0;
489 error:
490 cdev_del(&dvb_device_cdev);
491 unregister_chrdev_region(dev, MAX_DVB_MINORS);
492 return retval;
496 static void __exit exit_dvbdev(void)
498 class_destroy(dvb_class);
499 cdev_del(&dvb_device_cdev);
500 unregister_chrdev_region(MKDEV(DVB_MAJOR, 0), MAX_DVB_MINORS);
503 subsys_initcall(init_dvbdev);
504 module_exit(exit_dvbdev);
506 MODULE_DESCRIPTION("DVB Core Driver");
507 MODULE_AUTHOR("Marcus Metzler, Ralph Metzler, Holger Waechtler");
508 MODULE_LICENSE("GPL");