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