blob: 6a32680dbb1b07575f546d75d3132747a34f503f [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#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 Molnar1e4baed2006-01-15 07:52:23 -020034#include <linux/mutex.h>
Jonathan Corbet5794e1b2008-05-15 16:26:57 -060035#include <linux/smp_lock.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
Andreas Oberritter5dd3f302008-10-23 12:11:19 -030053#ifdef CONFIG_DVB_DYNAMIC_MINORS
54#define MAX_DVB_MINORS 256
55#define DVB_MAX_IDS MAX_DVB_MINORS
56#else
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#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 Oberritter5dd3f302008-10-23 12:11:19 -030060#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
gregkh@suse.de56b22932005-03-23 10:01:41 -080062static struct class *dvb_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Andreas Oberritter5dd3f302008-10-23 12:11:19 -030064static struct dvb_device *dvb_minors[MAX_DVB_MINORS];
65static DECLARE_RWSEM(minor_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67static int dvb_device_open(struct inode *inode, struct file *file)
68{
69 struct dvb_device *dvbdev;
70
Jonathan Corbet5794e1b2008-05-15 16:26:57 -060071 lock_kernel();
Andreas Oberritter5dd3f302008-10-23 12:11:19 -030072 down_read(&minor_rwsem);
73 dvbdev = dvb_minors[iminor(inode)];
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 if (dvbdev && dvbdev->fops) {
76 int err = 0;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -080077 const struct file_operations *old_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 file->private_data = dvbdev;
80 old_fops = file->f_op;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -080081 file->f_op = fops_get(dvbdev->fops);
Laurent Pinchartf41ced82009-01-06 14:40:40 -080082 if (file->f_op == NULL) {
83 file->f_op = old_fops;
84 goto fail;
85 }
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -080086 if(file->f_op->open)
Michael Krufky50c25ff2006-01-09 15:25:34 -020087 err = file->f_op->open(inode,file);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -080088 if (err) {
Michael Krufky50c25ff2006-01-09 15:25:34 -020089 fops_put(file->f_op);
90 file->f_op = fops_get(old_fops);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -080091 }
92 fops_put(old_fops);
Andreas Oberritter5dd3f302008-10-23 12:11:19 -030093 up_read(&minor_rwsem);
Jonathan Corbet5794e1b2008-05-15 16:26:57 -060094 unlock_kernel();
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -080095 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 }
Laurent Pinchartf41ced82009-01-06 14:40:40 -080097fail:
Andreas Oberritter5dd3f302008-10-23 12:11:19 -030098 up_read(&minor_rwsem);
Jonathan Corbet5794e1b2008-05-15 16:26:57 -060099 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return -ENODEV;
101}
102
103
Jan Engelhardt27a643b2008-04-22 14:42:01 -0300104static const struct file_operations dvb_device_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
106 .owner = THIS_MODULE,
107 .open = dvb_device_open,
108};
109
Greg Kroah-Hartman7e7654a2007-09-12 15:06:57 -0700110static struct cdev dvb_device_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112int dvb_generic_open(struct inode *inode, struct file *file)
113{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800114 struct dvb_device *dvbdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800116 if (!dvbdev)
117 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 if (!dvbdev->users)
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800120 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800123 if (!dvbdev->readers)
124 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 dvbdev->readers--;
126 } else {
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800127 if (!dvbdev->writers)
128 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 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{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800140 struct dvb_device *dvbdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 if (!dvbdev)
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800143 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
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{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800160 struct dvb_device *dvbdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800162 if (!dvbdev)
163 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
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) {
Trent Piepho79482612007-10-10 05:37:39 -0300178 struct dvb_device *dev;
179 list_for_each_entry(dev, &adap->device_list, list_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 if (dev->type == type && dev->id == id)
181 goto skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 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;
Marcel Siegertb6190102007-02-13 09:46:55 -0300194 struct file_operations *dvbdevfops;
Kay Sievers5f553382007-08-15 14:00:09 -0300195 struct device *clsdev;
Andreas Oberritter5dd3f302008-10-23 12:11:19 -0300196 int minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 int id;
198
Simon Arlottc2788502007-03-10 06:21:25 -0300199 mutex_lock(&dvbdev_register_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Marcel Siegertb6190102007-02-13 09:46:55 -0300201 if ((id = dvbdev_get_free_id (adap, type)) < 0){
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200202 mutex_unlock(&dvbdev_register_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 *pdvbdev = NULL;
Harvey Harrison46b4f7c2008-04-08 23:20:00 -0300204 printk(KERN_ERR "%s: couldn't find free device id\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 return -ENFILE;
206 }
207
208 *pdvbdev = dvbdev = kmalloc(sizeof(struct dvb_device), GFP_KERNEL);
209
Marcel Siegertb6190102007-02-13 09:46:55 -0300210 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);
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200219 mutex_unlock(&dvbdev_register_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return -ENOMEM;
221 }
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 memcpy(dvbdev, template, sizeof(struct dvb_device));
224 dvbdev->type = type;
225 dvbdev->id = id;
226 dvbdev->adapter = adap;
227 dvbdev->priv = priv;
Marcel Siegertb6190102007-02-13 09:46:55 -0300228 dvbdev->fops = dvbdevfops;
Markus Rechbergerca5be9c2007-04-14 10:18:58 -0300229 init_waitqueue_head (&dvbdev->wait_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Marcel Siegertb6190102007-02-13 09:46:55 -0300231 memcpy(dvbdev->fops, template->fops, sizeof(struct file_operations));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 dvbdev->fops->owner = adap->module;
233
234 list_add_tail (&dvbdev->list_head, &adap->device_list);
235
Andreas Oberritter5dd3f302008-10-23 12:11:19 -0300236 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
Andrew de Quinceyf47f4762006-04-04 09:41:47 -0300256 mutex_unlock(&dvbdev_register_lock);
257
Greg Kroah-Hartmana9b12612008-07-21 20:03:34 -0700258 clsdev = device_create(dvb_class, adap->device,
Hans Verkuilb7496782008-11-03 05:38:43 -0300259 MKDEV(DVB_MAJOR, minor),
Kay Sieversa5f4c0c2008-10-27 22:27:37 -0300260 dvbdev, "dvb%d.%s%d", adap->num, dnames[type], id);
Simon Arlott4abdcf92007-05-06 20:56:14 -0300261 if (IS_ERR(clsdev)) {
262 printk(KERN_ERR "%s: failed to create device dvb%d.%s%d (%ld)\n",
Harvey Harrison46b4f7c2008-04-08 23:20:00 -0300263 __func__, adap->num, dnames[type], id, PTR_ERR(clsdev));
Simon Arlott4abdcf92007-05-06 20:56:14 -0300264 return PTR_ERR(clsdev);
265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Simon Arlott900858e2007-05-06 21:06:32 -0300267 dprintk(KERN_DEBUG "DVB: register adapter%d/%s%d @ minor: %i (0x%02x)\n",
Andreas Oberritter5dd3f302008-10-23 12:11:19 -0300268 adap->num, dnames[type], id, minor, minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
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
Andreas Oberritter5dd3f302008-10-23 12:11:19 -0300280 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));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 list_del (&dvbdev->list_head);
Marcel Siegertb6190102007-02-13 09:46:55 -0300287 kfree (dvbdev->fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 kfree (dvbdev);
289}
290EXPORT_SYMBOL(dvb_unregister_device);
291
Janne Grunau78e92002008-04-09 19:13:13 -0300292static 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}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304static int dvbdev_get_free_adapter_num (void)
305{
306 int num = 0;
307
308 while (num < DVB_MAX_ADAPTERS) {
Janne Grunau78e92002008-04-09 19:13:13 -0300309 if (dvbdev_check_free_adapter_num(num))
310 return num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 num++;
312 }
313
314 return -ENFILE;
315}
316
317
Janne Grunau78e92002008-04-09 19:13:13 -0300318int dvb_register_adapter(struct dvb_adapter *adap, const char *name,
319 struct module *module, struct device *device,
320 short *adapter_nums)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Janne Grunau78e92002008-04-09 19:13:13 -0300322 int i, num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Simon Arlottc2788502007-03-10 06:21:25 -0300324 mutex_lock(&dvbdev_register_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Janne Grunau78e92002008-04-09 19:13:13 -0300326 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) {
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200340 mutex_unlock(&dvbdev_register_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return -ENFILE;
342 }
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 memset (adap, 0, sizeof(struct dvb_adapter));
345 INIT_LIST_HEAD (&adap->device_list);
346
Simon Arlott900858e2007-05-06 21:06:32 -0300347 printk(KERN_INFO "DVB: registering new adapter (%s)\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 adap->num = num;
350 adap->name = name;
351 adap->module = module;
Andrew de Quinceyd09dbf92006-04-10 09:27:37 -0300352 adap->device = device;
Darron Broad59b18422008-10-11 11:44:05 -0300353 adap->mfe_shared = 0;
354 adap->mfe_dvbdev = NULL;
355 mutex_init (&adap->mfe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 list_add_tail (&adap->list_head, &dvb_adapter_list);
358
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200359 mutex_unlock(&dvbdev_register_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 return num;
362}
363EXPORT_SYMBOL(dvb_register_adapter);
364
365
366int dvb_unregister_adapter(struct dvb_adapter *adap)
367{
Simon Arlottc2788502007-03-10 06:21:25 -0300368 mutex_lock(&dvbdev_register_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 list_del (&adap->list_head);
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200370 mutex_unlock(&dvbdev_register_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 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,
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800381 unsigned int cmd, unsigned long arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 int (*func)(struct inode *inode, struct file *file,
383 unsigned int cmd, void *arg))
384{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800385 char sbuf[128];
386 void *mbuf = NULL;
387 void *parg = NULL;
388 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800390 /* Copy arguments into temp kernel buffer */
391 switch (_IOC_DIR(cmd)) {
392 case _IOC_NONE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 /*
394 * For this command, the pointer is actually an integer
395 * argument.
396 */
397 parg = (void *) arg;
398 break;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800399 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)) {
Michael Krufky50c25ff2006-01-09 15:25:34 -0200403 parg = sbuf;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800404 } else {
Michael Krufky50c25ff2006-01-09 15:25:34 -0200405 /* too big to allocate from stack */
406 mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
407 if (NULL == mbuf)
408 return -ENOMEM;
409 parg = mbuf;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800412 err = -EFAULT;
413 if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
Michael Krufky50c25ff2006-01-09 15:25:34 -0200414 goto out;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800415 break;
416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800418 /* call driver */
419 if ((err = func(inode, file, cmd, parg)) == -ENOIOCTLCMD)
420 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800422 if (err < 0)
423 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800425 /* 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)))
Michael Krufky50c25ff2006-01-09 15:25:34 -0200431 err = -EFAULT;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800432 break;
433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435out:
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800436 kfree(mbuf);
437 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
Kay Sieversa5f4c0c2008-10-27 22:27:37 -0300440static int dvb_uevent(struct device *dev, struct kobj_uevent_env *env)
441{
442 struct dvb_device *dvbdev = dev_get_drvdata(dev);
443
Kay Sieversa5f4c0c2008-10-27 22:27:37 -0300444 add_uevent_var(env, "DVB_ADAPTER_NUM=%d", dvbdev->adapter->num);
Kay Sievers763d19b2008-12-31 23:35:24 -0300445 add_uevent_var(env, "DVB_DEVICE_TYPE=%s", dnames[dvbdev->type]);
446 add_uevent_var(env, "DVB_DEVICE_NUM=%d", dvbdev->id);
Kay Sieversa5f4c0c2008-10-27 22:27:37 -0300447 return 0;
448}
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450static int __init init_dvbdev(void)
451{
452 int retval;
453 dev_t dev = MKDEV(DVB_MAJOR, 0);
454
455 if ((retval = register_chrdev_region(dev, MAX_DVB_MINORS, "DVB")) != 0) {
Simon Arlott900858e2007-05-06 21:06:32 -0300456 printk(KERN_ERR "dvb-core: unable to get major %d\n", DVB_MAJOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return retval;
458 }
459
460 cdev_init(&dvb_device_cdev, &dvb_device_fops);
461 if ((retval = cdev_add(&dvb_device_cdev, dev, MAX_DVB_MINORS)) != 0) {
Simon Arlott900858e2007-05-06 21:06:32 -0300462 printk(KERN_ERR "dvb-core: unable register character device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 goto error;
464 }
465
gregkh@suse.de56b22932005-03-23 10:01:41 -0800466 dvb_class = class_create(THIS_MODULE, "dvb");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (IS_ERR(dvb_class)) {
468 retval = PTR_ERR(dvb_class);
469 goto error;
470 }
Kay Sieversa5f4c0c2008-10-27 22:27:37 -0300471 dvb_class->dev_uevent = dvb_uevent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return 0;
473
474error:
475 cdev_del(&dvb_device_cdev);
476 unregister_chrdev_region(dev, MAX_DVB_MINORS);
477 return retval;
478}
479
480
481static void __exit exit_dvbdev(void)
482{
gregkh@suse.de56b22932005-03-23 10:01:41 -0800483 class_destroy(dvb_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 cdev_del(&dvb_device_cdev);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800485 unregister_chrdev_region(MKDEV(DVB_MAJOR, 0), MAX_DVB_MINORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
Simon Arlott4abdcf92007-05-06 20:56:14 -0300488subsys_initcall(init_dvbdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489module_exit(exit_dvbdev);
490
491MODULE_DESCRIPTION("DVB Core Driver");
492MODULE_AUTHOR("Marcus Metzler, Ralph Metzler, Holger Waechtler");
493MODULE_LICENSE("GPL");