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