blob: 9ef0c00605ee80a3d4a08b14f4be7f826a50d91f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/init.h>
31#include <linux/slab.h>
32#include <linux/device.h>
33#include <linux/fs.h>
34#include <linux/cdev.h>
Ingo Molnar1e4baed2006-01-15 07:52:23 -020035#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "dvbdev.h"
37
38static int dvbdev_debug;
39
40module_param(dvbdev_debug, int, 0644);
41MODULE_PARM_DESC(dvbdev_debug, "Turn on/off device debugging (default:off).");
42
43#define dprintk if (dvbdev_debug) printk
44
45static LIST_HEAD(dvb_adapter_list);
Ingo Molnar1e4baed2006-01-15 07:52:23 -020046static DEFINE_MUTEX(dvbdev_register_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48static const char * const dnames[] = {
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -080049 "video", "audio", "sec", "frontend", "demux", "dvr", "ca",
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 "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
gregkh@suse.de56b22932005-03-23 10:01:41 -080058static struct class *dvb_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60static 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
80static 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;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -080088 const struct file_operations *old_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 file->private_data = dvbdev;
91 old_fops = file->f_op;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -080092 file->f_op = fops_get(dvbdev->fops);
93 if(file->f_op->open)
Michael Krufky50c25ff2006-01-09 15:25:34 -020094 err = file->f_op->open(inode,file);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -080095 if (err) {
Michael Krufky50c25ff2006-01-09 15:25:34 -020096 fops_put(file->f_op);
97 file->f_op = fops_get(old_fops);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -080098 }
99 fops_put(old_fops);
100 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 }
102 return -ENODEV;
103}
104
105
106static struct file_operations dvb_device_fops =
107{
108 .owner = THIS_MODULE,
109 .open = dvb_device_open,
110};
111
112static struct cdev dvb_device_cdev = {
113 .kobj = {.name = "dvb", },
114 .owner = THIS_MODULE,
115};
116
117int dvb_generic_open(struct inode *inode, struct file *file)
118{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800119 struct dvb_device *dvbdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800121 if (!dvbdev)
122 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 if (!dvbdev->users)
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800125 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800128 if (!dvbdev->readers)
129 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 dvbdev->readers--;
131 } else {
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800132 if (!dvbdev->writers)
133 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 dvbdev->writers--;
135 }
136
137 dvbdev->users--;
138 return 0;
139}
140EXPORT_SYMBOL(dvb_generic_open);
141
142
143int dvb_generic_release(struct inode *inode, struct file *file)
144{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800145 struct dvb_device *dvbdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 if (!dvbdev)
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800148 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
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}
159EXPORT_SYMBOL(dvb_generic_release);
160
161
162int dvb_generic_ioctl(struct inode *inode, struct file *file,
163 unsigned int cmd, unsigned long arg)
164{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800165 struct dvb_device *dvbdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800167 if (!dvbdev)
168 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 if (!dvbdev->kernel_ioctl)
171 return -EINVAL;
172
173 return dvb_usercopy (inode, file, cmd, arg, dvbdev->kernel_ioctl);
174}
175EXPORT_SYMBOL(dvb_generic_ioctl);
176
177
178static 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;
191skip:
192 id++;
193 }
194 return -ENFILE;
195}
196
197
198int 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;
Marcel Siegertb6190102007-02-13 09:46:55 -0300202 struct file_operations *dvbdevfops;
Simon Arlott4abdcf92007-05-06 20:56:14 -0300203 struct class_device *clsdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 int id;
205
Simon Arlottc2788502007-03-10 06:21:25 -0300206 mutex_lock(&dvbdev_register_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Marcel Siegertb6190102007-02-13 09:46:55 -0300208 if ((id = dvbdev_get_free_id (adap, type)) < 0){
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200209 mutex_unlock(&dvbdev_register_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 *pdvbdev = NULL;
Simon Arlott900858e2007-05-06 21:06:32 -0300211 printk(KERN_ERR "%s: couldn't find free device id\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return -ENFILE;
213 }
214
215 *pdvbdev = dvbdev = kmalloc(sizeof(struct dvb_device), GFP_KERNEL);
216
Marcel Siegertb6190102007-02-13 09:46:55 -0300217 if (!dvbdev){
218 mutex_unlock(&dvbdev_register_lock);
219 return -ENOMEM;
220 }
221
222 dvbdevfops = kzalloc(sizeof(struct file_operations), GFP_KERNEL);
223
224 if (!dvbdevfops){
225 kfree (dvbdev);
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200226 mutex_unlock(&dvbdev_register_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return -ENOMEM;
228 }
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 memcpy(dvbdev, template, sizeof(struct dvb_device));
231 dvbdev->type = type;
232 dvbdev->id = id;
233 dvbdev->adapter = adap;
234 dvbdev->priv = priv;
Marcel Siegertb6190102007-02-13 09:46:55 -0300235 dvbdev->fops = dvbdevfops;
Markus Rechbergerca5be9c2007-04-14 10:18:58 -0300236 init_waitqueue_head (&dvbdev->wait_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Marcel Siegertb6190102007-02-13 09:46:55 -0300238 memcpy(dvbdev->fops, template->fops, sizeof(struct file_operations));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 dvbdev->fops->owner = adap->module;
240
241 list_add_tail (&dvbdev->list_head, &adap->device_list);
242
Andrew de Quinceyf47f4762006-04-04 09:41:47 -0300243 mutex_unlock(&dvbdev_register_lock);
244
Simon Arlott4abdcf92007-05-06 20:56:14 -0300245 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);
253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Simon Arlott900858e2007-05-06 21:06:32 -0300255 dprintk(KERN_DEBUG "DVB: register adapter%d/%s%d @ minor: %i (0x%02x)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 adap->num, dnames[type], id, nums2minor(adap->num, type, id),
257 nums2minor(adap->num, type, id));
258
259 return 0;
260}
261EXPORT_SYMBOL(dvb_register_device);
262
263
264void dvb_unregister_device(struct dvb_device *dvbdev)
265{
266 if (!dvbdev)
267 return;
268
gregkh@suse.de56b22932005-03-23 10:01:41 -0800269 class_device_destroy(dvb_class, MKDEV(DVB_MAJOR, nums2minor(dvbdev->adapter->num,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 dvbdev->type, dvbdev->id)));
271
272 list_del (&dvbdev->list_head);
Marcel Siegertb6190102007-02-13 09:46:55 -0300273 kfree (dvbdev->fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 kfree (dvbdev);
275}
276EXPORT_SYMBOL(dvb_unregister_device);
277
278
279static int dvbdev_get_free_adapter_num (void)
280{
281 int num = 0;
282
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;
290 }
291 return num;
292skip:
293 num++;
294 }
295
296 return -ENFILE;
297}
298
299
Andrew de Quinceyd09dbf92006-04-10 09:27:37 -0300300int dvb_register_adapter(struct dvb_adapter *adap, const char *name, struct module *module, struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 int num;
303
Simon Arlottc2788502007-03-10 06:21:25 -0300304 mutex_lock(&dvbdev_register_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 if ((num = dvbdev_get_free_adapter_num ()) < 0) {
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200307 mutex_unlock(&dvbdev_register_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 return -ENFILE;
309 }
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 memset (adap, 0, sizeof(struct dvb_adapter));
312 INIT_LIST_HEAD (&adap->device_list);
313
Simon Arlott900858e2007-05-06 21:06:32 -0300314 printk(KERN_INFO "DVB: registering new adapter (%s)\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 adap->num = num;
317 adap->name = name;
318 adap->module = module;
Andrew de Quinceyd09dbf92006-04-10 09:27:37 -0300319 adap->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 list_add_tail (&adap->list_head, &dvb_adapter_list);
322
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200323 mutex_unlock(&dvbdev_register_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 return num;
326}
327EXPORT_SYMBOL(dvb_register_adapter);
328
329
330int dvb_unregister_adapter(struct dvb_adapter *adap)
331{
Simon Arlottc2788502007-03-10 06:21:25 -0300332 mutex_lock(&dvbdev_register_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 list_del (&adap->list_head);
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200334 mutex_unlock(&dvbdev_register_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 return 0;
336}
337EXPORT_SYMBOL(dvb_unregister_adapter);
338
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...) */
344int dvb_usercopy(struct inode *inode, struct file *file,
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800345 unsigned int cmd, unsigned long arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 int (*func)(struct inode *inode, struct file *file,
347 unsigned int cmd, void *arg))
348{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800349 char sbuf[128];
350 void *mbuf = NULL;
351 void *parg = NULL;
352 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800354 /* Copy arguments into temp kernel buffer */
355 switch (_IOC_DIR(cmd)) {
356 case _IOC_NONE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 /*
358 * For this command, the pointer is actually an integer
359 * argument.
360 */
361 parg = (void *) arg;
362 break;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800363 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)) {
Michael Krufky50c25ff2006-01-09 15:25:34 -0200367 parg = sbuf;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800368 } else {
Michael Krufky50c25ff2006-01-09 15:25:34 -0200369 /* too big to allocate from stack */
370 mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
371 if (NULL == mbuf)
372 return -ENOMEM;
373 parg = mbuf;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800376 err = -EFAULT;
377 if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
Michael Krufky50c25ff2006-01-09 15:25:34 -0200378 goto out;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800379 break;
380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800382 /* call driver */
383 if ((err = func(inode, file, cmd, parg)) == -ENOIOCTLCMD)
384 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800386 if (err < 0)
387 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800389 /* Copy results into user buffer */
390 switch (_IOC_DIR(cmd))
391 {
392 case _IOC_READ:
393 case (_IOC_WRITE | _IOC_READ):
394 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
Michael Krufky50c25ff2006-01-09 15:25:34 -0200395 err = -EFAULT;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800396 break;
397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399out:
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800400 kfree(mbuf);
401 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
404static int __init init_dvbdev(void)
405{
406 int retval;
407 dev_t dev = MKDEV(DVB_MAJOR, 0);
408
409 if ((retval = register_chrdev_region(dev, MAX_DVB_MINORS, "DVB")) != 0) {
Simon Arlott900858e2007-05-06 21:06:32 -0300410 printk(KERN_ERR "dvb-core: unable to get major %d\n", DVB_MAJOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return retval;
412 }
413
414 cdev_init(&dvb_device_cdev, &dvb_device_fops);
415 if ((retval = cdev_add(&dvb_device_cdev, dev, MAX_DVB_MINORS)) != 0) {
Simon Arlott900858e2007-05-06 21:06:32 -0300416 printk(KERN_ERR "dvb-core: unable register character device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 goto error;
418 }
419
gregkh@suse.de56b22932005-03-23 10:01:41 -0800420 dvb_class = class_create(THIS_MODULE, "dvb");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if (IS_ERR(dvb_class)) {
422 retval = PTR_ERR(dvb_class);
423 goto error;
424 }
425 return 0;
426
427error:
428 cdev_del(&dvb_device_cdev);
429 unregister_chrdev_region(dev, MAX_DVB_MINORS);
430 return retval;
431}
432
433
434static void __exit exit_dvbdev(void)
435{
gregkh@suse.de56b22932005-03-23 10:01:41 -0800436 class_destroy(dvb_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 cdev_del(&dvb_device_cdev);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800438 unregister_chrdev_region(MKDEV(DVB_MAJOR, 0), MAX_DVB_MINORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440
Simon Arlott4abdcf92007-05-06 20:56:14 -0300441subsys_initcall(init_dvbdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442module_exit(exit_dvbdev);
443
444MODULE_DESCRIPTION("DVB Core Driver");
445MODULE_AUTHOR("Marcus Metzler, Ralph Metzler, Holger Waechtler");
446MODULE_LICENSE("GPL");