RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / drivers / media / dvb / dvb-core / dvbdev.c
bloba9fa3337dd81d653e20ae80c3acdb7235f2a37ab
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/moduleparam.h>
29 #include <linux/kernel.h>
30 #include <linux/init.h>
31 #include <linux/slab.h>
32 #include <linux/device.h>
33 #include <linux/fs.h>
34 #include <linux/cdev.h>
35 #include <linux/mutex.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 #define DVB_MAX_ADAPTERS 8
54 #define DVB_MAX_IDS 4
55 #define nums2minor(num,type,id) ((num << 6) | (id << 4) | type)
56 #define MAX_DVB_MINORS (DVB_MAX_ADAPTERS*64)
58 static struct class *dvb_class;
60 static struct dvb_device* dvbdev_find_device (int minor)
62 struct list_head *entry;
64 list_for_each (entry, &dvb_adapter_list) {
65 struct list_head *entry0;
66 struct dvb_adapter *adap;
67 adap = list_entry (entry, struct dvb_adapter, list_head);
68 list_for_each (entry0, &adap->device_list) {
69 struct dvb_device *dev;
70 dev = list_entry (entry0, struct dvb_device, list_head);
71 if (nums2minor(adap->num, dev->type, dev->id) == minor)
72 return dev;
76 return NULL;
80 static int dvb_device_open(struct inode *inode, struct file *file)
82 struct dvb_device *dvbdev;
84 dvbdev = dvbdev_find_device (iminor(inode));
86 if (dvbdev && dvbdev->fops) {
87 int err = 0;
88 const struct file_operations *old_fops;
90 file->private_data = dvbdev;
91 old_fops = file->f_op;
92 file->f_op = fops_get(dvbdev->fops);
93 if(file->f_op->open)
94 err = file->f_op->open(inode,file);
95 if (err) {
96 fops_put(file->f_op);
97 file->f_op = fops_get(old_fops);
99 fops_put(old_fops);
100 return err;
102 return -ENODEV;
106 static struct file_operations dvb_device_fops =
108 .owner = THIS_MODULE,
109 .open = dvb_device_open,
112 static struct cdev dvb_device_cdev = {
113 .kobj = {.name = "dvb", },
114 .owner = THIS_MODULE,
117 int dvb_generic_open(struct inode *inode, struct file *file)
119 struct dvb_device *dvbdev = file->private_data;
121 if (!dvbdev)
122 return -ENODEV;
124 if (!dvbdev->users)
125 return -EBUSY;
127 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
128 if (!dvbdev->readers)
129 return -EBUSY;
130 dvbdev->readers--;
131 } else {
132 if (!dvbdev->writers)
133 return -EBUSY;
134 dvbdev->writers--;
137 dvbdev->users--;
138 return 0;
140 EXPORT_SYMBOL(dvb_generic_open);
143 int dvb_generic_release(struct inode *inode, struct file *file)
145 struct dvb_device *dvbdev = file->private_data;
147 if (!dvbdev)
148 return -ENODEV;
150 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
151 dvbdev->readers++;
152 } else {
153 dvbdev->writers++;
156 dvbdev->users++;
157 return 0;
159 EXPORT_SYMBOL(dvb_generic_release);
162 int dvb_generic_ioctl(struct inode *inode, struct file *file,
163 unsigned int cmd, unsigned long arg)
165 struct dvb_device *dvbdev = file->private_data;
167 if (!dvbdev)
168 return -ENODEV;
170 if (!dvbdev->kernel_ioctl)
171 return -EINVAL;
173 return dvb_usercopy (inode, file, cmd, arg, dvbdev->kernel_ioctl);
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 list_head *entry;
184 list_for_each (entry, &adap->device_list) {
185 struct dvb_device *dev;
186 dev = list_entry (entry, struct dvb_device, list_head);
187 if (dev->type == type && dev->id == id)
188 goto skip;
190 return id;
191 skip:
192 id++;
194 return -ENFILE;
198 int dvb_register_device(struct dvb_adapter *adap, struct dvb_device **pdvbdev,
199 const struct dvb_device *template, void *priv, int type)
201 struct dvb_device *dvbdev;
202 struct file_operations *dvbdevfops;
203 struct class_device *clsdev;
204 int id;
206 mutex_lock(&dvbdev_register_lock);
208 if ((id = dvbdev_get_free_id (adap, type)) < 0){
209 mutex_unlock(&dvbdev_register_lock);
210 *pdvbdev = NULL;
211 printk ("%s: could get find free device id...\n", __FUNCTION__);
212 return -ENFILE;
215 *pdvbdev = dvbdev = kmalloc(sizeof(struct dvb_device), GFP_KERNEL);
217 if (!dvbdev){
218 mutex_unlock(&dvbdev_register_lock);
219 return -ENOMEM;
222 dvbdevfops = kzalloc(sizeof(struct file_operations), GFP_KERNEL);
224 if (!dvbdevfops){
225 kfree (dvbdev);
226 mutex_unlock(&dvbdev_register_lock);
227 return -ENOMEM;
230 memcpy(dvbdev, template, sizeof(struct dvb_device));
231 dvbdev->type = type;
232 dvbdev->id = id;
233 dvbdev->adapter = adap;
234 dvbdev->priv = priv;
235 dvbdev->fops = dvbdevfops;
236 init_waitqueue_head (&dvbdev->wait_queue);
238 memcpy(dvbdev->fops, template->fops, sizeof(struct file_operations));
239 dvbdev->fops->owner = adap->module;
241 list_add_tail (&dvbdev->list_head, &adap->device_list);
243 mutex_unlock(&dvbdev_register_lock);
245 clsdev = class_device_create(dvb_class, NULL, MKDEV(DVB_MAJOR,
246 nums2minor(adap->num, type, id)),
247 adap->device, "dvb%d.%s%d", adap->num,
248 dnames[type], id);
249 if (IS_ERR(clsdev)) {
250 printk(KERN_ERR "%s: failed to create device dvb%d.%s%d (%ld)\n",
251 __FUNCTION__, adap->num, dnames[type], id, PTR_ERR(clsdev));
252 return PTR_ERR(clsdev);
255 dprintk("DVB: register adapter%d/%s%d @ minor: %i (0x%02x)\n",
256 adap->num, dnames[type], id, nums2minor(adap->num, type, id),
257 nums2minor(adap->num, type, id));
259 return 0;
261 EXPORT_SYMBOL(dvb_register_device);
264 void dvb_unregister_device(struct dvb_device *dvbdev)
266 if (!dvbdev)
267 return;
269 class_device_destroy(dvb_class, MKDEV(DVB_MAJOR, nums2minor(dvbdev->adapter->num,
270 dvbdev->type, dvbdev->id)));
272 list_del (&dvbdev->list_head);
273 kfree (dvbdev->fops);
274 kfree (dvbdev);
276 EXPORT_SYMBOL(dvb_unregister_device);
279 static int dvbdev_get_free_adapter_num (void)
281 int num = 0;
283 while (num < DVB_MAX_ADAPTERS) {
284 struct list_head *entry;
285 list_for_each (entry, &dvb_adapter_list) {
286 struct dvb_adapter *adap;
287 adap = list_entry (entry, struct dvb_adapter, list_head);
288 if (adap->num == num)
289 goto skip;
291 return num;
292 skip:
293 num++;
296 return -ENFILE;
300 int dvb_register_adapter(struct dvb_adapter *adap, const char *name, struct module *module, struct device *device)
302 int num;
304 mutex_lock(&dvbdev_register_lock);
306 if ((num = dvbdev_get_free_adapter_num ()) < 0) {
307 mutex_unlock(&dvbdev_register_lock);
308 return -ENFILE;
311 memset (adap, 0, sizeof(struct dvb_adapter));
312 INIT_LIST_HEAD (&adap->device_list);
314 printk ("DVB: registering new adapter (%s).\n", name);
316 adap->num = num;
317 adap->name = name;
318 adap->module = module;
319 adap->device = device;
321 list_add_tail (&adap->list_head, &dvb_adapter_list);
323 mutex_unlock(&dvbdev_register_lock);
325 return num;
327 EXPORT_SYMBOL(dvb_register_adapter);
330 int dvb_unregister_adapter(struct dvb_adapter *adap)
332 mutex_lock(&dvbdev_register_lock);
333 list_del (&adap->list_head);
334 mutex_unlock(&dvbdev_register_lock);
335 return 0;
337 EXPORT_SYMBOL(dvb_unregister_adapter);
339 /* if the miracle happens and "generic_usercopy()" is included into
340 the kernel, then this can vanish. please don't make the mistake and
341 define this as video_usercopy(). this will introduce a dependecy
342 to the v4l "videodev.o" module, which is unnecessary for some
343 cards (ie. the budget dvb-cards don't need the v4l module...) */
344 int dvb_usercopy(struct inode *inode, struct file *file,
345 unsigned int cmd, unsigned long arg,
346 int (*func)(struct inode *inode, struct file *file,
347 unsigned int cmd, void *arg))
349 char sbuf[128];
350 void *mbuf = NULL;
351 void *parg = NULL;
352 int err = -EINVAL;
354 /* Copy arguments into temp kernel buffer */
355 switch (_IOC_DIR(cmd)) {
356 case _IOC_NONE:
358 * For this command, the pointer is actually an integer
359 * argument.
361 parg = (void *) arg;
362 break;
363 case _IOC_READ: /* some v4l ioctls are marked wrong ... */
364 case _IOC_WRITE:
365 case (_IOC_WRITE | _IOC_READ):
366 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
367 parg = sbuf;
368 } else {
369 /* too big to allocate from stack */
370 mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
371 if (NULL == mbuf)
372 return -ENOMEM;
373 parg = mbuf;
376 err = -EFAULT;
377 if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
378 goto out;
379 break;
382 /* call driver */
383 if ((err = func(inode, file, cmd, parg)) == -ENOIOCTLCMD)
384 err = -EINVAL;
386 if (err < 0)
387 goto out;
389 /* Copy results into user buffer */
390 switch (_IOC_DIR(cmd))
392 case _IOC_READ:
393 case (_IOC_WRITE | _IOC_READ):
394 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
395 err = -EFAULT;
396 break;
399 out:
400 kfree(mbuf);
401 return err;
404 static int __init init_dvbdev(void)
406 int retval;
407 dev_t dev = MKDEV(DVB_MAJOR, 0);
409 if ((retval = register_chrdev_region(dev, MAX_DVB_MINORS, "DVB")) != 0) {
410 printk("dvb-core: unable to get major %d\n", DVB_MAJOR);
411 return retval;
414 cdev_init(&dvb_device_cdev, &dvb_device_fops);
415 if ((retval = cdev_add(&dvb_device_cdev, dev, MAX_DVB_MINORS)) != 0) {
416 printk("dvb-core: unable to get major %d\n", DVB_MAJOR);
417 goto error;
420 dvb_class = class_create(THIS_MODULE, "dvb");
421 if (IS_ERR(dvb_class)) {
422 retval = PTR_ERR(dvb_class);
423 goto error;
425 return 0;
427 error:
428 cdev_del(&dvb_device_cdev);
429 unregister_chrdev_region(dev, MAX_DVB_MINORS);
430 return retval;
434 static void __exit exit_dvbdev(void)
436 class_destroy(dvb_class);
437 cdev_del(&dvb_device_cdev);
438 unregister_chrdev_region(MKDEV(DVB_MAJOR, 0), MAX_DVB_MINORS);
441 subsys_initcall(init_dvbdev);
442 module_exit(exit_dvbdev);
444 MODULE_DESCRIPTION("DVB Core Driver");
445 MODULE_AUTHOR("Marcus Metzler, Ralph Metzler, Holger Waechtler");
446 MODULE_LICENSE("GPL");