[ARM] Support register switch in nommu mode
[linux-2.6/verdex.git] / drivers / media / dvb / dvb-core / dvbdev.c
blob06b696e9acbdf0857ac9a4fd34473accc43fa3b8
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/sched.h>
31 #include <linux/init.h>
32 #include <linux/slab.h>
33 #include <linux/device.h>
34 #include <linux/fs.h>
35 #include <linux/cdev.h>
37 #include "dvbdev.h"
39 static int dvbdev_debug;
41 module_param(dvbdev_debug, int, 0644);
42 MODULE_PARM_DESC(dvbdev_debug, "Turn on/off device debugging (default:off).");
44 #define dprintk if (dvbdev_debug) printk
46 static LIST_HEAD(dvb_adapter_list);
47 static DECLARE_MUTEX(dvbdev_register_lock);
49 static const char * const dnames[] = {
50 "video", "audio", "sec", "frontend", "demux", "dvr", "ca",
51 "net", "osd"
54 #define DVB_MAX_ADAPTERS 8
55 #define DVB_MAX_IDS 4
56 #define nums2minor(num,type,id) ((num << 6) | (id << 4) | type)
57 #define MAX_DVB_MINORS (DVB_MAX_ADAPTERS*64)
59 static struct class *dvb_class;
61 static struct dvb_device* dvbdev_find_device (int minor)
63 struct list_head *entry;
65 list_for_each (entry, &dvb_adapter_list) {
66 struct list_head *entry0;
67 struct dvb_adapter *adap;
68 adap = list_entry (entry, struct dvb_adapter, list_head);
69 list_for_each (entry0, &adap->device_list) {
70 struct dvb_device *dev;
71 dev = list_entry (entry0, struct dvb_device, list_head);
72 if (nums2minor(adap->num, dev->type, dev->id) == minor)
73 return dev;
77 return NULL;
81 static int dvb_device_open(struct inode *inode, struct file *file)
83 struct dvb_device *dvbdev;
85 dvbdev = dvbdev_find_device (iminor(inode));
87 if (dvbdev && dvbdev->fops) {
88 int err = 0;
89 struct file_operations *old_fops;
91 file->private_data = dvbdev;
92 old_fops = file->f_op;
93 file->f_op = fops_get(dvbdev->fops);
94 if(file->f_op->open)
95 err = file->f_op->open(inode,file);
96 if (err) {
97 fops_put(file->f_op);
98 file->f_op = fops_get(old_fops);
100 fops_put(old_fops);
101 return err;
103 return -ENODEV;
107 static struct file_operations dvb_device_fops =
109 .owner = THIS_MODULE,
110 .open = dvb_device_open,
113 static struct cdev dvb_device_cdev = {
114 .kobj = {.name = "dvb", },
115 .owner = THIS_MODULE,
118 int dvb_generic_open(struct inode *inode, struct file *file)
120 struct dvb_device *dvbdev = file->private_data;
122 if (!dvbdev)
123 return -ENODEV;
125 if (!dvbdev->users)
126 return -EBUSY;
128 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
129 if (!dvbdev->readers)
130 return -EBUSY;
131 dvbdev->readers--;
132 } else {
133 if (!dvbdev->writers)
134 return -EBUSY;
135 dvbdev->writers--;
138 dvbdev->users--;
139 return 0;
141 EXPORT_SYMBOL(dvb_generic_open);
144 int dvb_generic_release(struct inode *inode, struct file *file)
146 struct dvb_device *dvbdev = file->private_data;
148 if (!dvbdev)
149 return -ENODEV;
151 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
152 dvbdev->readers++;
153 } else {
154 dvbdev->writers++;
157 dvbdev->users++;
158 return 0;
160 EXPORT_SYMBOL(dvb_generic_release);
163 int dvb_generic_ioctl(struct inode *inode, struct file *file,
164 unsigned int cmd, unsigned long arg)
166 struct dvb_device *dvbdev = file->private_data;
168 if (!dvbdev)
169 return -ENODEV;
171 if (!dvbdev->kernel_ioctl)
172 return -EINVAL;
174 return dvb_usercopy (inode, 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)
181 u32 id = 0;
183 while (id < DVB_MAX_IDS) {
184 struct list_head *entry;
185 list_for_each (entry, &adap->device_list) {
186 struct dvb_device *dev;
187 dev = list_entry (entry, struct dvb_device, list_head);
188 if (dev->type == type && dev->id == id)
189 goto skip;
191 return id;
192 skip:
193 id++;
195 return -ENFILE;
199 int dvb_register_device(struct dvb_adapter *adap, struct dvb_device **pdvbdev,
200 const struct dvb_device *template, void *priv, int type)
202 struct dvb_device *dvbdev;
203 int id;
205 if (down_interruptible (&dvbdev_register_lock))
206 return -ERESTARTSYS;
208 if ((id = dvbdev_get_free_id (adap, type)) < 0) {
209 up (&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 up(&dvbdev_register_lock);
219 return -ENOMEM;
222 up (&dvbdev_register_lock);
224 memcpy(dvbdev, template, sizeof(struct dvb_device));
225 dvbdev->type = type;
226 dvbdev->id = id;
227 dvbdev->adapter = adap;
228 dvbdev->priv = priv;
230 dvbdev->fops->owner = adap->module;
232 list_add_tail (&dvbdev->list_head, &adap->device_list);
234 devfs_mk_cdev(MKDEV(DVB_MAJOR, nums2minor(adap->num, type, id)),
235 S_IFCHR | S_IRUSR | S_IWUSR,
236 "dvb/adapter%d/%s%d", adap->num, dnames[type], id);
238 class_device_create(dvb_class, NULL, MKDEV(DVB_MAJOR, nums2minor(adap->num, type, id)),
239 NULL, "dvb%d.%s%d", adap->num, dnames[type], id);
241 dprintk("DVB: register adapter%d/%s%d @ minor: %i (0x%02x)\n",
242 adap->num, dnames[type], id, nums2minor(adap->num, type, id),
243 nums2minor(adap->num, type, id));
245 return 0;
247 EXPORT_SYMBOL(dvb_register_device);
250 void dvb_unregister_device(struct dvb_device *dvbdev)
252 if (!dvbdev)
253 return;
255 devfs_remove("dvb/adapter%d/%s%d", dvbdev->adapter->num,
256 dnames[dvbdev->type], dvbdev->id);
258 class_device_destroy(dvb_class, MKDEV(DVB_MAJOR, nums2minor(dvbdev->adapter->num,
259 dvbdev->type, dvbdev->id)));
261 list_del (&dvbdev->list_head);
262 kfree (dvbdev);
264 EXPORT_SYMBOL(dvb_unregister_device);
267 static int dvbdev_get_free_adapter_num (void)
269 int num = 0;
271 while (num < DVB_MAX_ADAPTERS) {
272 struct list_head *entry;
273 list_for_each (entry, &dvb_adapter_list) {
274 struct dvb_adapter *adap;
275 adap = list_entry (entry, struct dvb_adapter, list_head);
276 if (adap->num == num)
277 goto skip;
279 return num;
280 skip:
281 num++;
284 return -ENFILE;
288 int dvb_register_adapter(struct dvb_adapter *adap, const char *name, struct module *module)
290 int num;
292 if (down_interruptible (&dvbdev_register_lock))
293 return -ERESTARTSYS;
295 if ((num = dvbdev_get_free_adapter_num ()) < 0) {
296 up (&dvbdev_register_lock);
297 return -ENFILE;
300 memset (adap, 0, sizeof(struct dvb_adapter));
301 INIT_LIST_HEAD (&adap->device_list);
303 printk ("DVB: registering new adapter (%s).\n", name);
305 devfs_mk_dir("dvb/adapter%d", num);
306 adap->num = num;
307 adap->name = name;
308 adap->module = module;
310 list_add_tail (&adap->list_head, &dvb_adapter_list);
312 up (&dvbdev_register_lock);
314 return num;
316 EXPORT_SYMBOL(dvb_register_adapter);
319 int dvb_unregister_adapter(struct dvb_adapter *adap)
321 devfs_remove("dvb/adapter%d", adap->num);
323 if (down_interruptible (&dvbdev_register_lock))
324 return -ERESTARTSYS;
325 list_del (&adap->list_head);
326 up (&dvbdev_register_lock);
327 return 0;
329 EXPORT_SYMBOL(dvb_unregister_adapter);
331 /* if the miracle happens and "generic_usercopy()" is included into
332 the kernel, then this can vanish. please don't make the mistake and
333 define this as video_usercopy(). this will introduce a dependecy
334 to the v4l "videodev.o" module, which is unnecessary for some
335 cards (ie. the budget dvb-cards don't need the v4l module...) */
336 int dvb_usercopy(struct inode *inode, struct file *file,
337 unsigned int cmd, unsigned long arg,
338 int (*func)(struct inode *inode, struct file *file,
339 unsigned int cmd, void *arg))
341 char sbuf[128];
342 void *mbuf = NULL;
343 void *parg = NULL;
344 int err = -EINVAL;
346 /* Copy arguments into temp kernel buffer */
347 switch (_IOC_DIR(cmd)) {
348 case _IOC_NONE:
350 * For this command, the pointer is actually an integer
351 * argument.
353 parg = (void *) arg;
354 break;
355 case _IOC_READ: /* some v4l ioctls are marked wrong ... */
356 case _IOC_WRITE:
357 case (_IOC_WRITE | _IOC_READ):
358 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
359 parg = sbuf;
360 } else {
361 /* too big to allocate from stack */
362 mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
363 if (NULL == mbuf)
364 return -ENOMEM;
365 parg = mbuf;
368 err = -EFAULT;
369 if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
370 goto out;
371 break;
374 /* call driver */
375 if ((err = func(inode, file, cmd, parg)) == -ENOIOCTLCMD)
376 err = -EINVAL;
378 if (err < 0)
379 goto out;
381 /* Copy results into user buffer */
382 switch (_IOC_DIR(cmd))
384 case _IOC_READ:
385 case (_IOC_WRITE | _IOC_READ):
386 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
387 err = -EFAULT;
388 break;
391 out:
392 kfree(mbuf);
393 return err;
396 static int __init init_dvbdev(void)
398 int retval;
399 dev_t dev = MKDEV(DVB_MAJOR, 0);
401 if ((retval = register_chrdev_region(dev, MAX_DVB_MINORS, "DVB")) != 0) {
402 printk("dvb-core: unable to get major %d\n", DVB_MAJOR);
403 return retval;
406 cdev_init(&dvb_device_cdev, &dvb_device_fops);
407 if ((retval = cdev_add(&dvb_device_cdev, dev, MAX_DVB_MINORS)) != 0) {
408 printk("dvb-core: unable to get major %d\n", DVB_MAJOR);
409 goto error;
412 devfs_mk_dir("dvb");
414 dvb_class = class_create(THIS_MODULE, "dvb");
415 if (IS_ERR(dvb_class)) {
416 retval = PTR_ERR(dvb_class);
417 goto error;
419 return 0;
421 error:
422 cdev_del(&dvb_device_cdev);
423 unregister_chrdev_region(dev, MAX_DVB_MINORS);
424 return retval;
428 static void __exit exit_dvbdev(void)
430 devfs_remove("dvb");
431 class_destroy(dvb_class);
432 cdev_del(&dvb_device_cdev);
433 unregister_chrdev_region(MKDEV(DVB_MAJOR, 0), MAX_DVB_MINORS);
436 module_init(init_dvbdev);
437 module_exit(exit_dvbdev);
439 MODULE_DESCRIPTION("DVB Core Driver");
440 MODULE_AUTHOR("Marcus Metzler, Ralph Metzler, Holger Waechtler");
441 MODULE_LICENSE("GPL");