at v2.6.33 503 lines 12 kB view raw
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/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 <linux/smp_lock.h> 36#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); 46static DEFINE_MUTEX(dvbdev_register_lock); 47 48static const char * const dnames[] = { 49 "video", "audio", "sec", "frontend", "demux", "dvr", "ca", 50 "net", "osd" 51}; 52 53#ifdef CONFIG_DVB_DYNAMIC_MINORS 54#define MAX_DVB_MINORS 256 55#define DVB_MAX_IDS MAX_DVB_MINORS 56#else 57#define DVB_MAX_IDS 4 58#define nums2minor(num,type,id) ((num << 6) | (id << 4) | type) 59#define MAX_DVB_MINORS (DVB_MAX_ADAPTERS*64) 60#endif 61 62static struct class *dvb_class; 63 64static struct dvb_device *dvb_minors[MAX_DVB_MINORS]; 65static DECLARE_RWSEM(minor_rwsem); 66 67static int dvb_device_open(struct inode *inode, struct file *file) 68{ 69 struct dvb_device *dvbdev; 70 71 lock_kernel(); 72 down_read(&minor_rwsem); 73 dvbdev = dvb_minors[iminor(inode)]; 74 75 if (dvbdev && dvbdev->fops) { 76 int err = 0; 77 const struct file_operations *old_fops; 78 79 file->private_data = dvbdev; 80 old_fops = file->f_op; 81 file->f_op = fops_get(dvbdev->fops); 82 if (file->f_op == NULL) { 83 file->f_op = old_fops; 84 goto fail; 85 } 86 if(file->f_op->open) 87 err = file->f_op->open(inode,file); 88 if (err) { 89 fops_put(file->f_op); 90 file->f_op = fops_get(old_fops); 91 } 92 fops_put(old_fops); 93 up_read(&minor_rwsem); 94 unlock_kernel(); 95 return err; 96 } 97fail: 98 up_read(&minor_rwsem); 99 unlock_kernel(); 100 return -ENODEV; 101} 102 103 104static const struct file_operations dvb_device_fops = 105{ 106 .owner = THIS_MODULE, 107 .open = dvb_device_open, 108}; 109 110static struct cdev dvb_device_cdev; 111 112int dvb_generic_open(struct inode *inode, struct file *file) 113{ 114 struct dvb_device *dvbdev = file->private_data; 115 116 if (!dvbdev) 117 return -ENODEV; 118 119 if (!dvbdev->users) 120 return -EBUSY; 121 122 if ((file->f_flags & O_ACCMODE) == O_RDONLY) { 123 if (!dvbdev->readers) 124 return -EBUSY; 125 dvbdev->readers--; 126 } else { 127 if (!dvbdev->writers) 128 return -EBUSY; 129 dvbdev->writers--; 130 } 131 132 dvbdev->users--; 133 return 0; 134} 135EXPORT_SYMBOL(dvb_generic_open); 136 137 138int dvb_generic_release(struct inode *inode, struct file *file) 139{ 140 struct dvb_device *dvbdev = file->private_data; 141 142 if (!dvbdev) 143 return -ENODEV; 144 145 if ((file->f_flags & O_ACCMODE) == O_RDONLY) { 146 dvbdev->readers++; 147 } else { 148 dvbdev->writers++; 149 } 150 151 dvbdev->users++; 152 return 0; 153} 154EXPORT_SYMBOL(dvb_generic_release); 155 156 157int dvb_generic_ioctl(struct inode *inode, struct file *file, 158 unsigned int cmd, unsigned long arg) 159{ 160 struct dvb_device *dvbdev = file->private_data; 161 162 if (!dvbdev) 163 return -ENODEV; 164 165 if (!dvbdev->kernel_ioctl) 166 return -EINVAL; 167 168 return dvb_usercopy (inode, file, cmd, arg, dvbdev->kernel_ioctl); 169} 170EXPORT_SYMBOL(dvb_generic_ioctl); 171 172 173static int dvbdev_get_free_id (struct dvb_adapter *adap, int type) 174{ 175 u32 id = 0; 176 177 while (id < DVB_MAX_IDS) { 178 struct dvb_device *dev; 179 list_for_each_entry(dev, &adap->device_list, list_head) 180 if (dev->type == type && dev->id == id) 181 goto skip; 182 return id; 183skip: 184 id++; 185 } 186 return -ENFILE; 187} 188 189 190int dvb_register_device(struct dvb_adapter *adap, struct dvb_device **pdvbdev, 191 const struct dvb_device *template, void *priv, int type) 192{ 193 struct dvb_device *dvbdev; 194 struct file_operations *dvbdevfops; 195 struct device *clsdev; 196 int minor; 197 int id; 198 199 mutex_lock(&dvbdev_register_lock); 200 201 if ((id = dvbdev_get_free_id (adap, type)) < 0){ 202 mutex_unlock(&dvbdev_register_lock); 203 *pdvbdev = NULL; 204 printk(KERN_ERR "%s: couldn't find free device id\n", __func__); 205 return -ENFILE; 206 } 207 208 *pdvbdev = dvbdev = kmalloc(sizeof(struct dvb_device), GFP_KERNEL); 209 210 if (!dvbdev){ 211 mutex_unlock(&dvbdev_register_lock); 212 return -ENOMEM; 213 } 214 215 dvbdevfops = kzalloc(sizeof(struct file_operations), GFP_KERNEL); 216 217 if (!dvbdevfops){ 218 kfree (dvbdev); 219 mutex_unlock(&dvbdev_register_lock); 220 return -ENOMEM; 221 } 222 223 memcpy(dvbdev, template, sizeof(struct dvb_device)); 224 dvbdev->type = type; 225 dvbdev->id = id; 226 dvbdev->adapter = adap; 227 dvbdev->priv = priv; 228 dvbdev->fops = dvbdevfops; 229 init_waitqueue_head (&dvbdev->wait_queue); 230 231 memcpy(dvbdevfops, template->fops, sizeof(struct file_operations)); 232 dvbdevfops->owner = adap->module; 233 234 list_add_tail (&dvbdev->list_head, &adap->device_list); 235 236 down_write(&minor_rwsem); 237#ifdef CONFIG_DVB_DYNAMIC_MINORS 238 for (minor = 0; minor < MAX_DVB_MINORS; minor++) 239 if (dvb_minors[minor] == NULL) 240 break; 241 242 if (minor == MAX_DVB_MINORS) { 243 kfree(dvbdevfops); 244 kfree(dvbdev); 245 mutex_unlock(&dvbdev_register_lock); 246 return -EINVAL; 247 } 248#else 249 minor = nums2minor(adap->num, type, id); 250#endif 251 252 dvbdev->minor = minor; 253 dvb_minors[minor] = dvbdev; 254 up_write(&minor_rwsem); 255 256 mutex_unlock(&dvbdev_register_lock); 257 258 clsdev = device_create(dvb_class, adap->device, 259 MKDEV(DVB_MAJOR, minor), 260 dvbdev, "dvb%d.%s%d", adap->num, dnames[type], id); 261 if (IS_ERR(clsdev)) { 262 printk(KERN_ERR "%s: failed to create device dvb%d.%s%d (%ld)\n", 263 __func__, adap->num, dnames[type], id, PTR_ERR(clsdev)); 264 return PTR_ERR(clsdev); 265 } 266 267 dprintk(KERN_DEBUG "DVB: register adapter%d/%s%d @ minor: %i (0x%02x)\n", 268 adap->num, dnames[type], id, minor, minor); 269 270 return 0; 271} 272EXPORT_SYMBOL(dvb_register_device); 273 274 275void dvb_unregister_device(struct dvb_device *dvbdev) 276{ 277 if (!dvbdev) 278 return; 279 280 down_write(&minor_rwsem); 281 dvb_minors[dvbdev->minor] = NULL; 282 up_write(&minor_rwsem); 283 284 device_destroy(dvb_class, MKDEV(DVB_MAJOR, dvbdev->minor)); 285 286 list_del (&dvbdev->list_head); 287 kfree (dvbdev->fops); 288 kfree (dvbdev); 289} 290EXPORT_SYMBOL(dvb_unregister_device); 291 292static int dvbdev_check_free_adapter_num(int num) 293{ 294 struct list_head *entry; 295 list_for_each(entry, &dvb_adapter_list) { 296 struct dvb_adapter *adap; 297 adap = list_entry(entry, struct dvb_adapter, list_head); 298 if (adap->num == num) 299 return 0; 300 } 301 return 1; 302} 303 304static int dvbdev_get_free_adapter_num (void) 305{ 306 int num = 0; 307 308 while (num < DVB_MAX_ADAPTERS) { 309 if (dvbdev_check_free_adapter_num(num)) 310 return num; 311 num++; 312 } 313 314 return -ENFILE; 315} 316 317 318int dvb_register_adapter(struct dvb_adapter *adap, const char *name, 319 struct module *module, struct device *device, 320 short *adapter_nums) 321{ 322 int i, num; 323 324 mutex_lock(&dvbdev_register_lock); 325 326 for (i = 0; i < DVB_MAX_ADAPTERS; ++i) { 327 num = adapter_nums[i]; 328 if (num >= 0 && num < DVB_MAX_ADAPTERS) { 329 /* use the one the driver asked for */ 330 if (dvbdev_check_free_adapter_num(num)) 331 break; 332 } else { 333 num = dvbdev_get_free_adapter_num(); 334 break; 335 } 336 num = -1; 337 } 338 339 if (num < 0) { 340 mutex_unlock(&dvbdev_register_lock); 341 return -ENFILE; 342 } 343 344 memset (adap, 0, sizeof(struct dvb_adapter)); 345 INIT_LIST_HEAD (&adap->device_list); 346 347 printk(KERN_INFO "DVB: registering new adapter (%s)\n", name); 348 349 adap->num = num; 350 adap->name = name; 351 adap->module = module; 352 adap->device = device; 353 adap->mfe_shared = 0; 354 adap->mfe_dvbdev = NULL; 355 mutex_init (&adap->mfe_lock); 356 357 list_add_tail (&adap->list_head, &dvb_adapter_list); 358 359 mutex_unlock(&dvbdev_register_lock); 360 361 return num; 362} 363EXPORT_SYMBOL(dvb_register_adapter); 364 365 366int dvb_unregister_adapter(struct dvb_adapter *adap) 367{ 368 mutex_lock(&dvbdev_register_lock); 369 list_del (&adap->list_head); 370 mutex_unlock(&dvbdev_register_lock); 371 return 0; 372} 373EXPORT_SYMBOL(dvb_unregister_adapter); 374 375/* if the miracle happens and "generic_usercopy()" is included into 376 the kernel, then this can vanish. please don't make the mistake and 377 define this as video_usercopy(). this will introduce a dependecy 378 to the v4l "videodev.o" module, which is unnecessary for some 379 cards (ie. the budget dvb-cards don't need the v4l module...) */ 380int dvb_usercopy(struct inode *inode, struct file *file, 381 unsigned int cmd, unsigned long arg, 382 int (*func)(struct inode *inode, struct file *file, 383 unsigned int cmd, void *arg)) 384{ 385 char sbuf[128]; 386 void *mbuf = NULL; 387 void *parg = NULL; 388 int err = -EINVAL; 389 390 /* Copy arguments into temp kernel buffer */ 391 switch (_IOC_DIR(cmd)) { 392 case _IOC_NONE: 393 /* 394 * For this command, the pointer is actually an integer 395 * argument. 396 */ 397 parg = (void *) arg; 398 break; 399 case _IOC_READ: /* some v4l ioctls are marked wrong ... */ 400 case _IOC_WRITE: 401 case (_IOC_WRITE | _IOC_READ): 402 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) { 403 parg = sbuf; 404 } else { 405 /* too big to allocate from stack */ 406 mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL); 407 if (NULL == mbuf) 408 return -ENOMEM; 409 parg = mbuf; 410 } 411 412 err = -EFAULT; 413 if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd))) 414 goto out; 415 break; 416 } 417 418 /* call driver */ 419 if ((err = func(inode, file, cmd, parg)) == -ENOIOCTLCMD) 420 err = -EINVAL; 421 422 if (err < 0) 423 goto out; 424 425 /* Copy results into user buffer */ 426 switch (_IOC_DIR(cmd)) 427 { 428 case _IOC_READ: 429 case (_IOC_WRITE | _IOC_READ): 430 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd))) 431 err = -EFAULT; 432 break; 433 } 434 435out: 436 kfree(mbuf); 437 return err; 438} 439 440static int dvb_uevent(struct device *dev, struct kobj_uevent_env *env) 441{ 442 struct dvb_device *dvbdev = dev_get_drvdata(dev); 443 444 add_uevent_var(env, "DVB_ADAPTER_NUM=%d", dvbdev->adapter->num); 445 add_uevent_var(env, "DVB_DEVICE_TYPE=%s", dnames[dvbdev->type]); 446 add_uevent_var(env, "DVB_DEVICE_NUM=%d", dvbdev->id); 447 return 0; 448} 449 450static char *dvb_devnode(struct device *dev, mode_t *mode) 451{ 452 struct dvb_device *dvbdev = dev_get_drvdata(dev); 453 454 return kasprintf(GFP_KERNEL, "dvb/adapter%d/%s%d", 455 dvbdev->adapter->num, dnames[dvbdev->type], dvbdev->id); 456} 457 458 459static int __init init_dvbdev(void) 460{ 461 int retval; 462 dev_t dev = MKDEV(DVB_MAJOR, 0); 463 464 if ((retval = register_chrdev_region(dev, MAX_DVB_MINORS, "DVB")) != 0) { 465 printk(KERN_ERR "dvb-core: unable to get major %d\n", DVB_MAJOR); 466 return retval; 467 } 468 469 cdev_init(&dvb_device_cdev, &dvb_device_fops); 470 if ((retval = cdev_add(&dvb_device_cdev, dev, MAX_DVB_MINORS)) != 0) { 471 printk(KERN_ERR "dvb-core: unable register character device\n"); 472 goto error; 473 } 474 475 dvb_class = class_create(THIS_MODULE, "dvb"); 476 if (IS_ERR(dvb_class)) { 477 retval = PTR_ERR(dvb_class); 478 goto error; 479 } 480 dvb_class->dev_uevent = dvb_uevent; 481 dvb_class->devnode = dvb_devnode; 482 return 0; 483 484error: 485 cdev_del(&dvb_device_cdev); 486 unregister_chrdev_region(dev, MAX_DVB_MINORS); 487 return retval; 488} 489 490 491static void __exit exit_dvbdev(void) 492{ 493 class_destroy(dvb_class); 494 cdev_del(&dvb_device_cdev); 495 unregister_chrdev_region(MKDEV(DVB_MAJOR, 0), MAX_DVB_MINORS); 496} 497 498subsys_initcall(init_dvbdev); 499module_exit(exit_dvbdev); 500 501MODULE_DESCRIPTION("DVB Core Driver"); 502MODULE_AUTHOR("Marcus Metzler, Ralph Metzler, Holger Waechtler"); 503MODULE_LICENSE("GPL");