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