V4L/DVB (6715): ivtv: Remove unnecessary register update
[linux-2.6/verdex.git] / drivers / media / dvb / dvb-core / dvbdev.c
blob18738faecbbc51adc7f9311c066779cafdee0b88
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 "dvbdev.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",
49 "net", "osd"
52 #define DVB_MAX_ADAPTERS 8
53 #define DVB_MAX_IDS 4
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)
67 return dev;
70 return NULL;
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) {
81 int err = 0;
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);
87 if(file->f_op->open)
88 err = file->f_op->open(inode,file);
89 if (err) {
90 fops_put(file->f_op);
91 file->f_op = fops_get(old_fops);
93 fops_put(old_fops);
94 return err;
96 return -ENODEV;
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;
112 if (!dvbdev)
113 return -ENODEV;
115 if (!dvbdev->users)
116 return -EBUSY;
118 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
119 if (!dvbdev->readers)
120 return -EBUSY;
121 dvbdev->readers--;
122 } else {
123 if (!dvbdev->writers)
124 return -EBUSY;
125 dvbdev->writers--;
128 dvbdev->users--;
129 return 0;
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;
138 if (!dvbdev)
139 return -ENODEV;
141 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
142 dvbdev->readers++;
143 } else {
144 dvbdev->writers++;
147 dvbdev->users++;
148 return 0;
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;
158 if (!dvbdev)
159 return -ENODEV;
161 if (!dvbdev->kernel_ioctl)
162 return -EINVAL;
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)
171 u32 id = 0;
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)
177 goto skip;
178 return id;
179 skip:
180 id++;
182 return -ENFILE;
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;
192 int id;
194 mutex_lock(&dvbdev_register_lock);
196 if ((id = dvbdev_get_free_id (adap, type)) < 0){
197 mutex_unlock(&dvbdev_register_lock);
198 *pdvbdev = NULL;
199 printk(KERN_ERR "%s: couldn't find free device id\n", __FUNCTION__);
200 return -ENFILE;
203 *pdvbdev = dvbdev = kmalloc(sizeof(struct dvb_device), GFP_KERNEL);
205 if (!dvbdev){
206 mutex_unlock(&dvbdev_register_lock);
207 return -ENOMEM;
210 dvbdevfops = kzalloc(sizeof(struct file_operations), GFP_KERNEL);
212 if (!dvbdevfops){
213 kfree (dvbdev);
214 mutex_unlock(&dvbdev_register_lock);
215 return -ENOMEM;
218 memcpy(dvbdev, template, sizeof(struct dvb_device));
219 dvbdev->type = type;
220 dvbdev->id = id;
221 dvbdev->adapter = adap;
222 dvbdev->priv = priv;
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));
246 return 0;
248 EXPORT_SYMBOL(dvb_register_device);
251 void dvb_unregister_device(struct dvb_device *dvbdev)
253 if (!dvbdev)
254 return;
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);
261 kfree (dvbdev);
263 EXPORT_SYMBOL(dvb_unregister_device);
266 static int dvbdev_get_free_adapter_num (void)
268 int num = 0;
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)
274 goto skip;
275 return num;
276 skip:
277 num++;
280 return -ENFILE;
284 int dvb_register_adapter(struct dvb_adapter *adap, const char *name, struct module *module, struct device *device)
286 int num;
288 mutex_lock(&dvbdev_register_lock);
290 if ((num = dvbdev_get_free_adapter_num ()) < 0) {
291 mutex_unlock(&dvbdev_register_lock);
292 return -ENFILE;
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);
300 adap->num = num;
301 adap->name = 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);
309 return num;
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);
319 return 0;
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))
333 char sbuf[128];
334 void *mbuf = NULL;
335 void *parg = NULL;
336 int err = -EINVAL;
338 /* Copy arguments into temp kernel buffer */
339 switch (_IOC_DIR(cmd)) {
340 case _IOC_NONE:
342 * For this command, the pointer is actually an integer
343 * argument.
345 parg = (void *) arg;
346 break;
347 case _IOC_READ: /* some v4l ioctls are marked wrong ... */
348 case _IOC_WRITE:
349 case (_IOC_WRITE | _IOC_READ):
350 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
351 parg = sbuf;
352 } else {
353 /* too big to allocate from stack */
354 mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
355 if (NULL == mbuf)
356 return -ENOMEM;
357 parg = mbuf;
360 err = -EFAULT;
361 if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
362 goto out;
363 break;
366 /* call driver */
367 if ((err = func(inode, file, cmd, parg)) == -ENOIOCTLCMD)
368 err = -EINVAL;
370 if (err < 0)
371 goto out;
373 /* Copy results into user buffer */
374 switch (_IOC_DIR(cmd))
376 case _IOC_READ:
377 case (_IOC_WRITE | _IOC_READ):
378 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
379 err = -EFAULT;
380 break;
383 out:
384 kfree(mbuf);
385 return err;
388 static int __init init_dvbdev(void)
390 int retval;
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);
395 return retval;
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");
401 goto error;
404 dvb_class = class_create(THIS_MODULE, "dvb");
405 if (IS_ERR(dvb_class)) {
406 retval = PTR_ERR(dvb_class);
407 goto error;
409 return 0;
411 error:
412 cdev_del(&dvb_device_cdev);
413 unregister_chrdev_region(dev, MAX_DVB_MINORS);
414 return retval;
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");