]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/media/dvb/dvb-core/dvbdev.c
[PATCH] remove many unneeded #includes of sched.h
[linux-2.6-omap-h63xx.git] / drivers / media / dvb / dvb-core / dvbdev.c
1 /*
2  * dvbdev.c
3  *
4  * Copyright (C) 2000 Ralph  Metzler <ralph@convergence.de>
5  *                  & Marcus Metzler <marcus@convergence.de>
6  *                    for convergence integrated media GmbH
7  *
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.
12  *
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.
17  *
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.
21  *
22  */
23
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"
37
38 static int dvbdev_debug;
39
40 module_param(dvbdev_debug, int, 0644);
41 MODULE_PARM_DESC(dvbdev_debug, "Turn on/off device debugging (default:off).");
42
43 #define dprintk if (dvbdev_debug) printk
44
45 static LIST_HEAD(dvb_adapter_list);
46 static DEFINE_MUTEX(dvbdev_register_lock);
47
48 static const char * const dnames[] = {
49         "video", "audio", "sec", "frontend", "demux", "dvr", "ca",
50         "net", "osd"
51 };
52
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)
57
58 static struct class *dvb_class;
59
60 static struct dvb_device* dvbdev_find_device (int minor)
61 {
62         struct list_head *entry;
63
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;
73                 }
74         }
75
76         return NULL;
77 }
78
79
80 static int dvb_device_open(struct inode *inode, struct file *file)
81 {
82         struct dvb_device *dvbdev;
83
84         dvbdev = dvbdev_find_device (iminor(inode));
85
86         if (dvbdev && dvbdev->fops) {
87                 int err = 0;
88                 const struct file_operations *old_fops;
89
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);
98                 }
99                 fops_put(old_fops);
100                 return err;
101         }
102         return -ENODEV;
103 }
104
105
106 static struct file_operations dvb_device_fops =
107 {
108         .owner =        THIS_MODULE,
109         .open =         dvb_device_open,
110 };
111
112 static struct cdev dvb_device_cdev = {
113         .kobj   = {.name = "dvb", },
114         .owner  =       THIS_MODULE,
115 };
116
117 int dvb_generic_open(struct inode *inode, struct file *file)
118 {
119         struct dvb_device *dvbdev = file->private_data;
120
121         if (!dvbdev)
122                 return -ENODEV;
123
124         if (!dvbdev->users)
125                 return -EBUSY;
126
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--;
135         }
136
137         dvbdev->users--;
138         return 0;
139 }
140 EXPORT_SYMBOL(dvb_generic_open);
141
142
143 int dvb_generic_release(struct inode *inode, struct file *file)
144 {
145         struct dvb_device *dvbdev = file->private_data;
146
147         if (!dvbdev)
148                 return -ENODEV;
149
150         if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
151                 dvbdev->readers++;
152         } else {
153                 dvbdev->writers++;
154         }
155
156         dvbdev->users++;
157         return 0;
158 }
159 EXPORT_SYMBOL(dvb_generic_release);
160
161
162 int dvb_generic_ioctl(struct inode *inode, struct file *file,
163                       unsigned int cmd, unsigned long arg)
164 {
165         struct dvb_device *dvbdev = file->private_data;
166
167         if (!dvbdev)
168                 return -ENODEV;
169
170         if (!dvbdev->kernel_ioctl)
171                 return -EINVAL;
172
173         return dvb_usercopy (inode, file, cmd, arg, dvbdev->kernel_ioctl);
174 }
175 EXPORT_SYMBOL(dvb_generic_ioctl);
176
177
178 static int dvbdev_get_free_id (struct dvb_adapter *adap, int type)
179 {
180         u32 id = 0;
181
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;
189                 }
190                 return id;
191 skip:
192                 id++;
193         }
194         return -ENFILE;
195 }
196
197
198 int dvb_register_device(struct dvb_adapter *adap, struct dvb_device **pdvbdev,
199                         const struct dvb_device *template, void *priv, int type)
200 {
201         struct dvb_device *dvbdev;
202         int id;
203
204         if (mutex_lock_interruptible(&dvbdev_register_lock))
205                 return -ERESTARTSYS;
206
207         if ((id = dvbdev_get_free_id (adap, type)) < 0) {
208                 mutex_unlock(&dvbdev_register_lock);
209                 *pdvbdev = NULL;
210                 printk ("%s: could get find free device id...\n", __FUNCTION__);
211                 return -ENFILE;
212         }
213
214         *pdvbdev = dvbdev = kmalloc(sizeof(struct dvb_device), GFP_KERNEL);
215
216         if (!dvbdev) {
217                 mutex_unlock(&dvbdev_register_lock);
218                 return -ENOMEM;
219         }
220
221         memcpy(dvbdev, template, sizeof(struct dvb_device));
222         dvbdev->type = type;
223         dvbdev->id = id;
224         dvbdev->adapter = adap;
225         dvbdev->priv = priv;
226
227         dvbdev->fops->owner = adap->module;
228
229         list_add_tail (&dvbdev->list_head, &adap->device_list);
230
231         mutex_unlock(&dvbdev_register_lock);
232
233         class_device_create(dvb_class, NULL, MKDEV(DVB_MAJOR, nums2minor(adap->num, type, id)),
234                             adap->device, "dvb%d.%s%d", adap->num, dnames[type], id);
235
236         dprintk("DVB: register adapter%d/%s%d @ minor: %i (0x%02x)\n",
237                 adap->num, dnames[type], id, nums2minor(adap->num, type, id),
238                 nums2minor(adap->num, type, id));
239
240         return 0;
241 }
242 EXPORT_SYMBOL(dvb_register_device);
243
244
245 void dvb_unregister_device(struct dvb_device *dvbdev)
246 {
247         if (!dvbdev)
248                 return;
249
250         class_device_destroy(dvb_class, MKDEV(DVB_MAJOR, nums2minor(dvbdev->adapter->num,
251                                         dvbdev->type, dvbdev->id)));
252
253         list_del (&dvbdev->list_head);
254         kfree (dvbdev);
255 }
256 EXPORT_SYMBOL(dvb_unregister_device);
257
258
259 static int dvbdev_get_free_adapter_num (void)
260 {
261         int num = 0;
262
263         while (num < DVB_MAX_ADAPTERS) {
264                 struct list_head *entry;
265                 list_for_each (entry, &dvb_adapter_list) {
266                         struct dvb_adapter *adap;
267                         adap = list_entry (entry, struct dvb_adapter, list_head);
268                         if (adap->num == num)
269                                 goto skip;
270                 }
271                 return num;
272 skip:
273                 num++;
274         }
275
276         return -ENFILE;
277 }
278
279
280 int dvb_register_adapter(struct dvb_adapter *adap, const char *name, struct module *module, struct device *device)
281 {
282         int num;
283
284         if (mutex_lock_interruptible(&dvbdev_register_lock))
285                 return -ERESTARTSYS;
286
287         if ((num = dvbdev_get_free_adapter_num ()) < 0) {
288                 mutex_unlock(&dvbdev_register_lock);
289                 return -ENFILE;
290         }
291
292         memset (adap, 0, sizeof(struct dvb_adapter));
293         INIT_LIST_HEAD (&adap->device_list);
294
295         printk ("DVB: registering new adapter (%s).\n", name);
296
297         adap->num = num;
298         adap->name = name;
299         adap->module = module;
300         adap->device = device;
301
302         list_add_tail (&adap->list_head, &dvb_adapter_list);
303
304         mutex_unlock(&dvbdev_register_lock);
305
306         return num;
307 }
308 EXPORT_SYMBOL(dvb_register_adapter);
309
310
311 int dvb_unregister_adapter(struct dvb_adapter *adap)
312 {
313         if (mutex_lock_interruptible(&dvbdev_register_lock))
314                 return -ERESTARTSYS;
315         list_del (&adap->list_head);
316         mutex_unlock(&dvbdev_register_lock);
317         return 0;
318 }
319 EXPORT_SYMBOL(dvb_unregister_adapter);
320
321 /* if the miracle happens and "generic_usercopy()" is included into
322    the kernel, then this can vanish. please don't make the mistake and
323    define this as video_usercopy(). this will introduce a dependecy
324    to the v4l "videodev.o" module, which is unnecessary for some
325    cards (ie. the budget dvb-cards don't need the v4l module...) */
326 int dvb_usercopy(struct inode *inode, struct file *file,
327                      unsigned int cmd, unsigned long arg,
328                      int (*func)(struct inode *inode, struct file *file,
329                      unsigned int cmd, void *arg))
330 {
331         char    sbuf[128];
332         void    *mbuf = NULL;
333         void    *parg = NULL;
334         int     err  = -EINVAL;
335
336         /*  Copy arguments into temp kernel buffer  */
337         switch (_IOC_DIR(cmd)) {
338         case _IOC_NONE:
339                 /*
340                  * For this command, the pointer is actually an integer
341                  * argument.
342                  */
343                 parg = (void *) arg;
344                 break;
345         case _IOC_READ: /* some v4l ioctls are marked wrong ... */
346         case _IOC_WRITE:
347         case (_IOC_WRITE | _IOC_READ):
348                 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
349                         parg = sbuf;
350                 } else {
351                         /* too big to allocate from stack */
352                         mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
353                         if (NULL == mbuf)
354                                 return -ENOMEM;
355                         parg = mbuf;
356                 }
357
358                 err = -EFAULT;
359                 if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
360                         goto out;
361                 break;
362         }
363
364         /* call driver */
365         if ((err = func(inode, file, cmd, parg)) == -ENOIOCTLCMD)
366                 err = -EINVAL;
367
368         if (err < 0)
369                 goto out;
370
371         /*  Copy results into user buffer  */
372         switch (_IOC_DIR(cmd))
373         {
374         case _IOC_READ:
375         case (_IOC_WRITE | _IOC_READ):
376                 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
377                         err = -EFAULT;
378                 break;
379         }
380
381 out:
382         kfree(mbuf);
383         return err;
384 }
385
386 static int __init init_dvbdev(void)
387 {
388         int retval;
389         dev_t dev = MKDEV(DVB_MAJOR, 0);
390
391         if ((retval = register_chrdev_region(dev, MAX_DVB_MINORS, "DVB")) != 0) {
392                 printk("dvb-core: unable to get major %d\n", DVB_MAJOR);
393                 return retval;
394         }
395
396         cdev_init(&dvb_device_cdev, &dvb_device_fops);
397         if ((retval = cdev_add(&dvb_device_cdev, dev, MAX_DVB_MINORS)) != 0) {
398                 printk("dvb-core: unable to get major %d\n", DVB_MAJOR);
399                 goto error;
400         }
401
402         dvb_class = class_create(THIS_MODULE, "dvb");
403         if (IS_ERR(dvb_class)) {
404                 retval = PTR_ERR(dvb_class);
405                 goto error;
406         }
407         return 0;
408
409 error:
410         cdev_del(&dvb_device_cdev);
411         unregister_chrdev_region(dev, MAX_DVB_MINORS);
412         return retval;
413 }
414
415
416 static void __exit exit_dvbdev(void)
417 {
418         class_destroy(dvb_class);
419         cdev_del(&dvb_device_cdev);
420         unregister_chrdev_region(MKDEV(DVB_MAJOR, 0), MAX_DVB_MINORS);
421 }
422
423 module_init(init_dvbdev);
424 module_exit(exit_dvbdev);
425
426 MODULE_DESCRIPTION("DVB Core Driver");
427 MODULE_AUTHOR("Marcus Metzler, Ralph Metzler, Holger Waechtler");
428 MODULE_LICENSE("GPL");