Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Ingo Molnar | 1e4baed | 2006-01-15 07:52:23 -0200 | [diff] [blame] | 36 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include "dvbdev.h" |
| 38 | |
| 39 | static int dvbdev_debug; |
| 40 | |
| 41 | module_param(dvbdev_debug, int, 0644); |
| 42 | MODULE_PARM_DESC(dvbdev_debug, "Turn on/off device debugging (default:off)."); |
| 43 | |
| 44 | #define dprintk if (dvbdev_debug) printk |
| 45 | |
| 46 | static LIST_HEAD(dvb_adapter_list); |
Ingo Molnar | 1e4baed | 2006-01-15 07:52:23 -0200 | [diff] [blame] | 47 | static DEFINE_MUTEX(dvbdev_register_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
| 49 | static const char * const dnames[] = { |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 50 | "video", "audio", "sec", "frontend", "demux", "dvr", "ca", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
gregkh@suse.de | 56b2293 | 2005-03-23 10:01:41 -0800 | [diff] [blame] | 59 | static struct class *dvb_class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
| 61 | static 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 | |
| 81 | static 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; |
Arjan van de Ven | 99ac48f | 2006-03-28 01:56:41 -0800 | [diff] [blame] | 89 | const struct file_operations *old_fops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
| 91 | file->private_data = dvbdev; |
| 92 | old_fops = file->f_op; |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 93 | file->f_op = fops_get(dvbdev->fops); |
| 94 | if(file->f_op->open) |
Michael Krufky | 50c25ff | 2006-01-09 15:25:34 -0200 | [diff] [blame] | 95 | err = file->f_op->open(inode,file); |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 96 | if (err) { |
Michael Krufky | 50c25ff | 2006-01-09 15:25:34 -0200 | [diff] [blame] | 97 | fops_put(file->f_op); |
| 98 | file->f_op = fops_get(old_fops); |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 99 | } |
| 100 | fops_put(old_fops); |
| 101 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | } |
| 103 | return -ENODEV; |
| 104 | } |
| 105 | |
| 106 | |
| 107 | static struct file_operations dvb_device_fops = |
| 108 | { |
| 109 | .owner = THIS_MODULE, |
| 110 | .open = dvb_device_open, |
| 111 | }; |
| 112 | |
| 113 | static struct cdev dvb_device_cdev = { |
| 114 | .kobj = {.name = "dvb", }, |
| 115 | .owner = THIS_MODULE, |
| 116 | }; |
| 117 | |
| 118 | int dvb_generic_open(struct inode *inode, struct file *file) |
| 119 | { |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 120 | struct dvb_device *dvbdev = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 122 | if (!dvbdev) |
| 123 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | |
| 125 | if (!dvbdev->users) |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 126 | return -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
| 128 | if ((file->f_flags & O_ACCMODE) == O_RDONLY) { |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 129 | if (!dvbdev->readers) |
| 130 | return -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | dvbdev->readers--; |
| 132 | } else { |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 133 | if (!dvbdev->writers) |
| 134 | return -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | dvbdev->writers--; |
| 136 | } |
| 137 | |
| 138 | dvbdev->users--; |
| 139 | return 0; |
| 140 | } |
| 141 | EXPORT_SYMBOL(dvb_generic_open); |
| 142 | |
| 143 | |
| 144 | int dvb_generic_release(struct inode *inode, struct file *file) |
| 145 | { |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 146 | struct dvb_device *dvbdev = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
| 148 | if (!dvbdev) |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 149 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
| 160 | EXPORT_SYMBOL(dvb_generic_release); |
| 161 | |
| 162 | |
| 163 | int dvb_generic_ioctl(struct inode *inode, struct file *file, |
| 164 | unsigned int cmd, unsigned long arg) |
| 165 | { |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 166 | struct dvb_device *dvbdev = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 168 | if (!dvbdev) |
| 169 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
| 171 | if (!dvbdev->kernel_ioctl) |
| 172 | return -EINVAL; |
| 173 | |
| 174 | return dvb_usercopy (inode, file, cmd, arg, dvbdev->kernel_ioctl); |
| 175 | } |
| 176 | EXPORT_SYMBOL(dvb_generic_ioctl); |
| 177 | |
| 178 | |
| 179 | static 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; |
| 192 | skip: |
| 193 | id++; |
| 194 | } |
| 195 | return -ENFILE; |
| 196 | } |
| 197 | |
| 198 | |
| 199 | int 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 | |
Ingo Molnar | 1e4baed | 2006-01-15 07:52:23 -0200 | [diff] [blame] | 205 | if (mutex_lock_interruptible(&dvbdev_register_lock)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | return -ERESTARTSYS; |
| 207 | |
| 208 | if ((id = dvbdev_get_free_id (adap, type)) < 0) { |
Ingo Molnar | 1e4baed | 2006-01-15 07:52:23 -0200 | [diff] [blame] | 209 | mutex_unlock(&dvbdev_register_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) { |
Ingo Molnar | 1e4baed | 2006-01-15 07:52:23 -0200 | [diff] [blame] | 218 | mutex_unlock(&dvbdev_register_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | return -ENOMEM; |
| 220 | } |
| 221 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Andrew de Quincey | f47f476 | 2006-04-04 09:41:47 -0300 | [diff] [blame] | 232 | mutex_unlock(&dvbdev_register_lock); |
| 233 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | devfs_mk_cdev(MKDEV(DVB_MAJOR, nums2minor(adap->num, type, id)), |
| 235 | S_IFCHR | S_IRUSR | S_IWUSR, |
| 236 | "dvb/adapter%d/%s%d", adap->num, dnames[type], id); |
| 237 | |
Greg Kroah-Hartman | 53f4654 | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 238 | class_device_create(dvb_class, NULL, MKDEV(DVB_MAJOR, nums2minor(adap->num, type, id)), |
Andrew de Quincey | d09dbf9 | 2006-04-10 09:27:37 -0300 | [diff] [blame^] | 239 | adap->device, "dvb%d.%s%d", adap->num, dnames[type], id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | |
| 241 | dprintk("DVB: register adapter%d/%s%d @ minor: %i (0x%02x)\n", |
| 242 | adap->num, dnames[type], id, nums2minor(adap->num, type, id), |
| 243 | nums2minor(adap->num, type, id)); |
| 244 | |
| 245 | return 0; |
| 246 | } |
| 247 | EXPORT_SYMBOL(dvb_register_device); |
| 248 | |
| 249 | |
| 250 | void dvb_unregister_device(struct dvb_device *dvbdev) |
| 251 | { |
| 252 | if (!dvbdev) |
| 253 | return; |
| 254 | |
| 255 | devfs_remove("dvb/adapter%d/%s%d", dvbdev->adapter->num, |
| 256 | dnames[dvbdev->type], dvbdev->id); |
| 257 | |
gregkh@suse.de | 56b2293 | 2005-03-23 10:01:41 -0800 | [diff] [blame] | 258 | class_device_destroy(dvb_class, MKDEV(DVB_MAJOR, nums2minor(dvbdev->adapter->num, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | dvbdev->type, dvbdev->id))); |
| 260 | |
| 261 | list_del (&dvbdev->list_head); |
| 262 | kfree (dvbdev); |
| 263 | } |
| 264 | EXPORT_SYMBOL(dvb_unregister_device); |
| 265 | |
| 266 | |
| 267 | static int dvbdev_get_free_adapter_num (void) |
| 268 | { |
| 269 | int num = 0; |
| 270 | |
| 271 | while (num < DVB_MAX_ADAPTERS) { |
| 272 | struct list_head *entry; |
| 273 | list_for_each (entry, &dvb_adapter_list) { |
| 274 | struct dvb_adapter *adap; |
| 275 | adap = list_entry (entry, struct dvb_adapter, list_head); |
| 276 | if (adap->num == num) |
| 277 | goto skip; |
| 278 | } |
| 279 | return num; |
| 280 | skip: |
| 281 | num++; |
| 282 | } |
| 283 | |
| 284 | return -ENFILE; |
| 285 | } |
| 286 | |
| 287 | |
Andrew de Quincey | d09dbf9 | 2006-04-10 09:27:37 -0300 | [diff] [blame^] | 288 | int dvb_register_adapter(struct dvb_adapter *adap, const char *name, struct module *module, struct device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | int num; |
| 291 | |
Ingo Molnar | 1e4baed | 2006-01-15 07:52:23 -0200 | [diff] [blame] | 292 | if (mutex_lock_interruptible(&dvbdev_register_lock)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | return -ERESTARTSYS; |
| 294 | |
| 295 | if ((num = dvbdev_get_free_adapter_num ()) < 0) { |
Ingo Molnar | 1e4baed | 2006-01-15 07:52:23 -0200 | [diff] [blame] | 296 | mutex_unlock(&dvbdev_register_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | return -ENFILE; |
| 298 | } |
| 299 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | memset (adap, 0, sizeof(struct dvb_adapter)); |
| 301 | INIT_LIST_HEAD (&adap->device_list); |
| 302 | |
| 303 | printk ("DVB: registering new adapter (%s).\n", name); |
| 304 | |
| 305 | devfs_mk_dir("dvb/adapter%d", num); |
| 306 | adap->num = num; |
| 307 | adap->name = name; |
| 308 | adap->module = module; |
Andrew de Quincey | d09dbf9 | 2006-04-10 09:27:37 -0300 | [diff] [blame^] | 309 | adap->device = device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | |
| 311 | list_add_tail (&adap->list_head, &dvb_adapter_list); |
| 312 | |
Ingo Molnar | 1e4baed | 2006-01-15 07:52:23 -0200 | [diff] [blame] | 313 | mutex_unlock(&dvbdev_register_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | |
| 315 | return num; |
| 316 | } |
| 317 | EXPORT_SYMBOL(dvb_register_adapter); |
| 318 | |
| 319 | |
| 320 | int dvb_unregister_adapter(struct dvb_adapter *adap) |
| 321 | { |
| 322 | devfs_remove("dvb/adapter%d", adap->num); |
| 323 | |
Ingo Molnar | 1e4baed | 2006-01-15 07:52:23 -0200 | [diff] [blame] | 324 | if (mutex_lock_interruptible(&dvbdev_register_lock)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | return -ERESTARTSYS; |
| 326 | list_del (&adap->list_head); |
Ingo Molnar | 1e4baed | 2006-01-15 07:52:23 -0200 | [diff] [blame] | 327 | mutex_unlock(&dvbdev_register_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | return 0; |
| 329 | } |
| 330 | EXPORT_SYMBOL(dvb_unregister_adapter); |
| 331 | |
| 332 | /* if the miracle happens and "generic_usercopy()" is included into |
| 333 | the kernel, then this can vanish. please don't make the mistake and |
| 334 | define this as video_usercopy(). this will introduce a dependecy |
| 335 | to the v4l "videodev.o" module, which is unnecessary for some |
| 336 | cards (ie. the budget dvb-cards don't need the v4l module...) */ |
| 337 | int dvb_usercopy(struct inode *inode, struct file *file, |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 338 | unsigned int cmd, unsigned long arg, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | int (*func)(struct inode *inode, struct file *file, |
| 340 | unsigned int cmd, void *arg)) |
| 341 | { |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 342 | char sbuf[128]; |
| 343 | void *mbuf = NULL; |
| 344 | void *parg = NULL; |
| 345 | int err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 347 | /* Copy arguments into temp kernel buffer */ |
| 348 | switch (_IOC_DIR(cmd)) { |
| 349 | case _IOC_NONE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | /* |
| 351 | * For this command, the pointer is actually an integer |
| 352 | * argument. |
| 353 | */ |
| 354 | parg = (void *) arg; |
| 355 | break; |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 356 | case _IOC_READ: /* some v4l ioctls are marked wrong ... */ |
| 357 | case _IOC_WRITE: |
| 358 | case (_IOC_WRITE | _IOC_READ): |
| 359 | if (_IOC_SIZE(cmd) <= sizeof(sbuf)) { |
Michael Krufky | 50c25ff | 2006-01-09 15:25:34 -0200 | [diff] [blame] | 360 | parg = sbuf; |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 361 | } else { |
Michael Krufky | 50c25ff | 2006-01-09 15:25:34 -0200 | [diff] [blame] | 362 | /* too big to allocate from stack */ |
| 363 | mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL); |
| 364 | if (NULL == mbuf) |
| 365 | return -ENOMEM; |
| 366 | parg = mbuf; |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 367 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 369 | err = -EFAULT; |
| 370 | if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd))) |
Michael Krufky | 50c25ff | 2006-01-09 15:25:34 -0200 | [diff] [blame] | 371 | goto out; |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 372 | break; |
| 373 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 375 | /* call driver */ |
| 376 | if ((err = func(inode, file, cmd, parg)) == -ENOIOCTLCMD) |
| 377 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 379 | if (err < 0) |
| 380 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 382 | /* Copy results into user buffer */ |
| 383 | switch (_IOC_DIR(cmd)) |
| 384 | { |
| 385 | case _IOC_READ: |
| 386 | case (_IOC_WRITE | _IOC_READ): |
| 387 | if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd))) |
Michael Krufky | 50c25ff | 2006-01-09 15:25:34 -0200 | [diff] [blame] | 388 | err = -EFAULT; |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 389 | break; |
| 390 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | |
| 392 | out: |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 393 | kfree(mbuf); |
| 394 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | static int __init init_dvbdev(void) |
| 398 | { |
| 399 | int retval; |
| 400 | dev_t dev = MKDEV(DVB_MAJOR, 0); |
| 401 | |
| 402 | if ((retval = register_chrdev_region(dev, MAX_DVB_MINORS, "DVB")) != 0) { |
| 403 | printk("dvb-core: unable to get major %d\n", DVB_MAJOR); |
| 404 | return retval; |
| 405 | } |
| 406 | |
| 407 | cdev_init(&dvb_device_cdev, &dvb_device_fops); |
| 408 | if ((retval = cdev_add(&dvb_device_cdev, dev, MAX_DVB_MINORS)) != 0) { |
| 409 | printk("dvb-core: unable to get major %d\n", DVB_MAJOR); |
| 410 | goto error; |
| 411 | } |
| 412 | |
| 413 | devfs_mk_dir("dvb"); |
| 414 | |
gregkh@suse.de | 56b2293 | 2005-03-23 10:01:41 -0800 | [diff] [blame] | 415 | dvb_class = class_create(THIS_MODULE, "dvb"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | if (IS_ERR(dvb_class)) { |
| 417 | retval = PTR_ERR(dvb_class); |
| 418 | goto error; |
| 419 | } |
| 420 | return 0; |
| 421 | |
| 422 | error: |
| 423 | cdev_del(&dvb_device_cdev); |
| 424 | unregister_chrdev_region(dev, MAX_DVB_MINORS); |
| 425 | return retval; |
| 426 | } |
| 427 | |
| 428 | |
| 429 | static void __exit exit_dvbdev(void) |
| 430 | { |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 431 | devfs_remove("dvb"); |
gregkh@suse.de | 56b2293 | 2005-03-23 10:01:41 -0800 | [diff] [blame] | 432 | class_destroy(dvb_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | cdev_del(&dvb_device_cdev); |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 434 | unregister_chrdev_region(MKDEV(DVB_MAJOR, 0), MAX_DVB_MINORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | module_init(init_dvbdev); |
| 438 | module_exit(exit_dvbdev); |
| 439 | |
| 440 | MODULE_DESCRIPTION("DVB Core Driver"); |
| 441 | MODULE_AUTHOR("Marcus Metzler, Ralph Metzler, Holger Waechtler"); |
| 442 | MODULE_LICENSE("GPL"); |