blob: 2fdcbb5f000a95e10ac775cbc8337cad03ef00a5 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "dvbdev.h"
36
Arnd Bergmann72024f12010-09-11 19:56:45 +020037static DEFINE_MUTEX(dvbdev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038static 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
Arnd Bergmann72024f12010-09-11 19:56:45 +020071 mutex_lock(&dvbdev_mutex);
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;
Al Viroe84f9e52013-09-22 14:17:15 -040077 const struct file_operations *new_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Al Viroe84f9e52013-09-22 14:17:15 -040079 new_fops = fops_get(dvbdev->fops);
80 if (!new_fops)
Laurent Pinchartf41ced82009-01-06 14:40:40 -080081 goto fail;
Al Viroe84f9e52013-09-22 14:17:15 -040082 file->private_data = dvbdev;
83 replace_fops(file, new_fops);
84 if (file->f_op->open)
Michael Krufky50c25ff2006-01-09 15:25:34 -020085 err = file->f_op->open(inode,file);
Andreas Oberritter5dd3f302008-10-23 12:11:19 -030086 up_read(&minor_rwsem);
Arnd Bergmann72024f12010-09-11 19:56:45 +020087 mutex_unlock(&dvbdev_mutex);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -080088 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 }
Laurent Pinchartf41ced82009-01-06 14:40:40 -080090fail:
Andreas Oberritter5dd3f302008-10-23 12:11:19 -030091 up_read(&minor_rwsem);
Arnd Bergmann72024f12010-09-11 19:56:45 +020092 mutex_unlock(&dvbdev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 return -ENODEV;
94}
95
96
Jan Engelhardt27a643b2008-04-22 14:42:01 -030097static const struct file_operations dvb_device_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
99 .owner = THIS_MODULE,
100 .open = dvb_device_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200101 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102};
103
Greg Kroah-Hartman7e7654a2007-09-12 15:06:57 -0700104static struct cdev dvb_device_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106int dvb_generic_open(struct inode *inode, struct file *file)
107{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800108 struct dvb_device *dvbdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800110 if (!dvbdev)
111 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 if (!dvbdev->users)
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800114 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800117 if (!dvbdev->readers)
118 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 dvbdev->readers--;
120 } else {
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800121 if (!dvbdev->writers)
122 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 dvbdev->writers--;
124 }
125
126 dvbdev->users--;
127 return 0;
128}
129EXPORT_SYMBOL(dvb_generic_open);
130
131
132int dvb_generic_release(struct inode *inode, struct file *file)
133{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800134 struct dvb_device *dvbdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136 if (!dvbdev)
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800137 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
140 dvbdev->readers++;
141 } else {
142 dvbdev->writers++;
143 }
144
145 dvbdev->users++;
146 return 0;
147}
148EXPORT_SYMBOL(dvb_generic_release);
149
150
Arnd Bergmann16ef8de2010-04-27 00:24:00 +0200151long dvb_generic_ioctl(struct file *file,
152 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800154 struct dvb_device *dvbdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800156 if (!dvbdev)
157 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 if (!dvbdev->kernel_ioctl)
160 return -EINVAL;
161
Arnd Bergmann72024f12010-09-11 19:56:45 +0200162 return dvb_usercopy(file, cmd, arg, dvbdev->kernel_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164EXPORT_SYMBOL(dvb_generic_ioctl);
165
166
167static int dvbdev_get_free_id (struct dvb_adapter *adap, int type)
168{
169 u32 id = 0;
170
171 while (id < DVB_MAX_IDS) {
Trent Piepho79482612007-10-10 05:37:39 -0300172 struct dvb_device *dev;
173 list_for_each_entry(dev, &adap->device_list, list_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 if (dev->type == type && dev->id == id)
175 goto skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 return id;
177skip:
178 id++;
179 }
180 return -ENFILE;
181}
182
Mauro Carvalho Chehaba0246e02015-01-02 12:19:51 -0300183static void dvb_register_media_device(struct dvb_device *dvbdev,
184 int type, int minor)
185{
186#if defined(CONFIG_MEDIA_CONTROLLER_DVB)
Mauro Carvalho Chehab172e9d32015-01-03 01:59:53 -0300187 int ret = 0, npads;
Mauro Carvalho Chehaba0246e02015-01-02 12:19:51 -0300188
189 if (!dvbdev->adapter->mdev)
190 return;
191
192 dvbdev->entity = kzalloc(sizeof(*dvbdev->entity), GFP_KERNEL);
193 if (!dvbdev->entity)
194 return;
195
196 dvbdev->entity->info.dev.major = DVB_MAJOR;
197 dvbdev->entity->info.dev.minor = minor;
198 dvbdev->entity->name = dvbdev->name;
Mauro Carvalho Chehab172e9d32015-01-03 01:59:53 -0300199
200 switch (type) {
201 case DVB_DEVICE_CA:
202 case DVB_DEVICE_DEMUX:
Mauro Carvalho Chehab3bde1b72015-01-03 16:35:53 -0300203 case DVB_DEVICE_FRONTEND:
Mauro Carvalho Chehab172e9d32015-01-03 01:59:53 -0300204 npads = 2;
205 break;
206 case DVB_DEVICE_NET:
207 npads = 0;
208 break;
209 default:
210 npads = 1;
211 }
212
213 if (npads) {
214 dvbdev->pads = kcalloc(npads, sizeof(*dvbdev->pads),
215 GFP_KERNEL);
216 if (!dvbdev->pads) {
217 kfree(dvbdev->entity);
218 return;
219 }
220 }
221
Mauro Carvalho Chehaba0246e02015-01-02 12:19:51 -0300222 switch (type) {
223 case DVB_DEVICE_FRONTEND:
224 dvbdev->entity->type = MEDIA_ENT_T_DEVNODE_DVB_FE;
Mauro Carvalho Chehab3bde1b72015-01-03 16:35:53 -0300225 dvbdev->pads[0].flags = MEDIA_PAD_FL_SINK;
226 dvbdev->pads[1].flags = MEDIA_PAD_FL_SOURCE;
Mauro Carvalho Chehaba0246e02015-01-02 12:19:51 -0300227 break;
228 case DVB_DEVICE_DEMUX:
229 dvbdev->entity->type = MEDIA_ENT_T_DEVNODE_DVB_DEMUX;
Mauro Carvalho Chehab3bde1b72015-01-03 16:35:53 -0300230 dvbdev->pads[0].flags = MEDIA_PAD_FL_SINK;
231 dvbdev->pads[1].flags = MEDIA_PAD_FL_SOURCE;
Mauro Carvalho Chehaba0246e02015-01-02 12:19:51 -0300232 break;
233 case DVB_DEVICE_DVR:
234 dvbdev->entity->type = MEDIA_ENT_T_DEVNODE_DVB_DVR;
Mauro Carvalho Chehab172e9d32015-01-03 01:59:53 -0300235 dvbdev->pads[0].flags = MEDIA_PAD_FL_SINK;
Mauro Carvalho Chehaba0246e02015-01-02 12:19:51 -0300236 break;
237 case DVB_DEVICE_CA:
238 dvbdev->entity->type = MEDIA_ENT_T_DEVNODE_DVB_CA;
Mauro Carvalho Chehab3bde1b72015-01-03 16:35:53 -0300239 dvbdev->pads[0].flags = MEDIA_PAD_FL_SINK;
240 dvbdev->pads[1].flags = MEDIA_PAD_FL_SOURCE;
Mauro Carvalho Chehaba0246e02015-01-02 12:19:51 -0300241 break;
242 case DVB_DEVICE_NET:
243 dvbdev->entity->type = MEDIA_ENT_T_DEVNODE_DVB_NET;
244 break;
245 default:
246 kfree(dvbdev->entity);
247 dvbdev->entity = NULL;
248 return;
249 }
250
Mauro Carvalho Chehab172e9d32015-01-03 01:59:53 -0300251 if (npads)
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -0300252 ret = media_entity_init(dvbdev->entity, npads, dvbdev->pads);
Mauro Carvalho Chehab172e9d32015-01-03 01:59:53 -0300253 if (!ret)
254 ret = media_device_register_entity(dvbdev->adapter->mdev,
255 dvbdev->entity);
Mauro Carvalho Chehaba0246e02015-01-02 12:19:51 -0300256 if (ret < 0) {
257 printk(KERN_ERR
258 "%s: media_device_register_entity failed for %s\n",
259 __func__, dvbdev->entity->name);
Mauro Carvalho Chehab172e9d32015-01-03 01:59:53 -0300260 kfree(dvbdev->pads);
Mauro Carvalho Chehaba0246e02015-01-02 12:19:51 -0300261 kfree(dvbdev->entity);
262 dvbdev->entity = NULL;
263 return;
264 }
265
266 printk(KERN_DEBUG "%s: media device '%s' registered.\n",
267 __func__, dvbdev->entity->name);
268#endif
269}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271int dvb_register_device(struct dvb_adapter *adap, struct dvb_device **pdvbdev,
272 const struct dvb_device *template, void *priv, int type)
273{
274 struct dvb_device *dvbdev;
Marcel Siegertb6190102007-02-13 09:46:55 -0300275 struct file_operations *dvbdevfops;
Kay Sievers5f553382007-08-15 14:00:09 -0300276 struct device *clsdev;
Andreas Oberritter5dd3f302008-10-23 12:11:19 -0300277 int minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 int id;
279
Simon Arlottc2788502007-03-10 06:21:25 -0300280 mutex_lock(&dvbdev_register_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Marcel Siegertb6190102007-02-13 09:46:55 -0300282 if ((id = dvbdev_get_free_id (adap, type)) < 0){
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200283 mutex_unlock(&dvbdev_register_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 *pdvbdev = NULL;
Harvey Harrison46b4f7c2008-04-08 23:20:00 -0300285 printk(KERN_ERR "%s: couldn't find free device id\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return -ENFILE;
287 }
288
289 *pdvbdev = dvbdev = kmalloc(sizeof(struct dvb_device), GFP_KERNEL);
290
Marcel Siegertb6190102007-02-13 09:46:55 -0300291 if (!dvbdev){
292 mutex_unlock(&dvbdev_register_lock);
293 return -ENOMEM;
294 }
295
296 dvbdevfops = kzalloc(sizeof(struct file_operations), GFP_KERNEL);
297
298 if (!dvbdevfops){
299 kfree (dvbdev);
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200300 mutex_unlock(&dvbdev_register_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return -ENOMEM;
302 }
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 memcpy(dvbdev, template, sizeof(struct dvb_device));
305 dvbdev->type = type;
306 dvbdev->id = id;
307 dvbdev->adapter = adap;
308 dvbdev->priv = priv;
Marcel Siegertb6190102007-02-13 09:46:55 -0300309 dvbdev->fops = dvbdevfops;
Markus Rechbergerca5be9c2007-04-14 10:18:58 -0300310 init_waitqueue_head (&dvbdev->wait_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Jan Engelhardt784e29d2009-01-11 06:12:43 -0300312 memcpy(dvbdevfops, template->fops, sizeof(struct file_operations));
313 dvbdevfops->owner = adap->module;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 list_add_tail (&dvbdev->list_head, &adap->device_list);
316
Andreas Oberritter5dd3f302008-10-23 12:11:19 -0300317 down_write(&minor_rwsem);
318#ifdef CONFIG_DVB_DYNAMIC_MINORS
319 for (minor = 0; minor < MAX_DVB_MINORS; minor++)
320 if (dvb_minors[minor] == NULL)
321 break;
322
323 if (minor == MAX_DVB_MINORS) {
324 kfree(dvbdevfops);
325 kfree(dvbdev);
Santosh Nayak82163ed2012-06-23 07:59:54 -0300326 up_write(&minor_rwsem);
Andreas Oberritter5dd3f302008-10-23 12:11:19 -0300327 mutex_unlock(&dvbdev_register_lock);
328 return -EINVAL;
329 }
330#else
331 minor = nums2minor(adap->num, type, id);
332#endif
333
334 dvbdev->minor = minor;
335 dvb_minors[minor] = dvbdev;
336 up_write(&minor_rwsem);
337
Andrew de Quinceyf47f4762006-04-04 09:41:47 -0300338 mutex_unlock(&dvbdev_register_lock);
339
Greg Kroah-Hartmana9b12612008-07-21 20:03:34 -0700340 clsdev = device_create(dvb_class, adap->device,
Hans Verkuilb7496782008-11-03 05:38:43 -0300341 MKDEV(DVB_MAJOR, minor),
Kay Sieversa5f4c0c2008-10-27 22:27:37 -0300342 dvbdev, "dvb%d.%s%d", adap->num, dnames[type], id);
Simon Arlott4abdcf92007-05-06 20:56:14 -0300343 if (IS_ERR(clsdev)) {
344 printk(KERN_ERR "%s: failed to create device dvb%d.%s%d (%ld)\n",
Harvey Harrison46b4f7c2008-04-08 23:20:00 -0300345 __func__, adap->num, dnames[type], id, PTR_ERR(clsdev));
Simon Arlott4abdcf92007-05-06 20:56:14 -0300346 return PTR_ERR(clsdev);
347 }
Simon Arlott900858e2007-05-06 21:06:32 -0300348 dprintk(KERN_DEBUG "DVB: register adapter%d/%s%d @ minor: %i (0x%02x)\n",
Andreas Oberritter5dd3f302008-10-23 12:11:19 -0300349 adap->num, dnames[type], id, minor, minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Mauro Carvalho Chehaba0246e02015-01-02 12:19:51 -0300351 dvb_register_media_device(dvbdev, type, minor);
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return 0;
354}
355EXPORT_SYMBOL(dvb_register_device);
356
357
358void dvb_unregister_device(struct dvb_device *dvbdev)
359{
360 if (!dvbdev)
361 return;
362
Andreas Oberritter5dd3f302008-10-23 12:11:19 -0300363 down_write(&minor_rwsem);
364 dvb_minors[dvbdev->minor] = NULL;
365 up_write(&minor_rwsem);
366
367 device_destroy(dvb_class, MKDEV(DVB_MAJOR, dvbdev->minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Mauro Carvalho Chehaba0246e02015-01-02 12:19:51 -0300369#if defined(CONFIG_MEDIA_CONTROLLER_DVB)
370 if (dvbdev->entity) {
371 media_device_unregister_entity(dvbdev->entity);
372 kfree(dvbdev->entity);
Mauro Carvalho Chehab172e9d32015-01-03 01:59:53 -0300373 kfree(dvbdev->pads);
Mauro Carvalho Chehaba0246e02015-01-02 12:19:51 -0300374 }
375#endif
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 list_del (&dvbdev->list_head);
Marcel Siegertb6190102007-02-13 09:46:55 -0300378 kfree (dvbdev->fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 kfree (dvbdev);
380}
381EXPORT_SYMBOL(dvb_unregister_device);
382
Mauro Carvalho Chehab41551092015-01-03 16:52:34 -0300383
Mauro Carvalho Chehab41551092015-01-03 16:52:34 -0300384#ifdef CONFIG_MEDIA_CONTROLLER_DVB
Mauro Carvalho Chehab480884b2015-03-02 10:49:04 -0300385void dvb_create_media_graph(struct dvb_adapter *adap)
386{
387 struct media_device *mdev = adap->mdev;
Mauro Carvalho Chehab41551092015-01-03 16:52:34 -0300388 struct media_entity *entity, *tuner = NULL, *fe = NULL;
389 struct media_entity *demux = NULL, *dvr = NULL, *ca = NULL;
390
391 if (!mdev)
392 return;
393
394 media_device_for_each_entity(entity, mdev) {
395 switch (entity->type) {
396 case MEDIA_ENT_T_V4L2_SUBDEV_TUNER:
397 tuner = entity;
398 break;
399 case MEDIA_ENT_T_DEVNODE_DVB_FE:
400 fe = entity;
401 break;
402 case MEDIA_ENT_T_DEVNODE_DVB_DEMUX:
403 demux = entity;
404 break;
405 case MEDIA_ENT_T_DEVNODE_DVB_DVR:
406 dvr = entity;
407 break;
408 case MEDIA_ENT_T_DEVNODE_DVB_CA:
409 ca = entity;
410 break;
411 }
412 }
413
414 if (tuner && fe)
415 media_entity_create_link(tuner, 0, fe, 0, 0);
416
417 if (fe && demux)
Mauro Carvalho Chehab6bb0b182015-01-06 13:13:31 -0300418 media_entity_create_link(fe, 1, demux, 0, MEDIA_LNK_FL_ENABLED);
Mauro Carvalho Chehab41551092015-01-03 16:52:34 -0300419
420 if (demux && dvr)
Mauro Carvalho Chehab6bb0b182015-01-06 13:13:31 -0300421 media_entity_create_link(demux, 1, dvr, 0, MEDIA_LNK_FL_ENABLED);
Mauro Carvalho Chehab41551092015-01-03 16:52:34 -0300422
423 if (demux && ca)
Mauro Carvalho Chehab6bb0b182015-01-06 13:13:31 -0300424 media_entity_create_link(demux, 1, ca, 0, MEDIA_LNK_FL_ENABLED);
Mauro Carvalho Chehab41551092015-01-03 16:52:34 -0300425}
426EXPORT_SYMBOL_GPL(dvb_create_media_graph);
Mauro Carvalho Chehab480884b2015-03-02 10:49:04 -0300427#endif
Mauro Carvalho Chehab41551092015-01-03 16:52:34 -0300428
Janne Grunau78e92002008-04-09 19:13:13 -0300429static int dvbdev_check_free_adapter_num(int num)
430{
431 struct list_head *entry;
432 list_for_each(entry, &dvb_adapter_list) {
433 struct dvb_adapter *adap;
434 adap = list_entry(entry, struct dvb_adapter, list_head);
435 if (adap->num == num)
436 return 0;
437 }
438 return 1;
439}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441static int dvbdev_get_free_adapter_num (void)
442{
443 int num = 0;
444
445 while (num < DVB_MAX_ADAPTERS) {
Janne Grunau78e92002008-04-09 19:13:13 -0300446 if (dvbdev_check_free_adapter_num(num))
447 return num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 num++;
449 }
450
451 return -ENFILE;
452}
453
454
Janne Grunau78e92002008-04-09 19:13:13 -0300455int dvb_register_adapter(struct dvb_adapter *adap, const char *name,
456 struct module *module, struct device *device,
457 short *adapter_nums)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Janne Grunau78e92002008-04-09 19:13:13 -0300459 int i, num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Simon Arlottc2788502007-03-10 06:21:25 -0300461 mutex_lock(&dvbdev_register_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Janne Grunau78e92002008-04-09 19:13:13 -0300463 for (i = 0; i < DVB_MAX_ADAPTERS; ++i) {
464 num = adapter_nums[i];
465 if (num >= 0 && num < DVB_MAX_ADAPTERS) {
466 /* use the one the driver asked for */
467 if (dvbdev_check_free_adapter_num(num))
468 break;
469 } else {
470 num = dvbdev_get_free_adapter_num();
471 break;
472 }
473 num = -1;
474 }
475
476 if (num < 0) {
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200477 mutex_unlock(&dvbdev_register_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return -ENFILE;
479 }
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 memset (adap, 0, sizeof(struct dvb_adapter));
482 INIT_LIST_HEAD (&adap->device_list);
483
Simon Arlott900858e2007-05-06 21:06:32 -0300484 printk(KERN_INFO "DVB: registering new adapter (%s)\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 adap->num = num;
487 adap->name = name;
488 adap->module = module;
Andrew de Quinceyd09dbf92006-04-10 09:27:37 -0300489 adap->device = device;
Darron Broad59b18422008-10-11 11:44:05 -0300490 adap->mfe_shared = 0;
491 adap->mfe_dvbdev = NULL;
492 mutex_init (&adap->mfe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 list_add_tail (&adap->list_head, &dvb_adapter_list);
495
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200496 mutex_unlock(&dvbdev_register_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 return num;
499}
500EXPORT_SYMBOL(dvb_register_adapter);
501
502
503int dvb_unregister_adapter(struct dvb_adapter *adap)
504{
Simon Arlottc2788502007-03-10 06:21:25 -0300505 mutex_lock(&dvbdev_register_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 list_del (&adap->list_head);
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200507 mutex_unlock(&dvbdev_register_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 return 0;
509}
510EXPORT_SYMBOL(dvb_unregister_adapter);
511
512/* if the miracle happens and "generic_usercopy()" is included into
513 the kernel, then this can vanish. please don't make the mistake and
514 define this as video_usercopy(). this will introduce a dependecy
515 to the v4l "videodev.o" module, which is unnecessary for some
516 cards (ie. the budget dvb-cards don't need the v4l module...) */
Arnd Bergmann16ef8de2010-04-27 00:24:00 +0200517int dvb_usercopy(struct file *file,
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800518 unsigned int cmd, unsigned long arg,
Arnd Bergmann16ef8de2010-04-27 00:24:00 +0200519 int (*func)(struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 unsigned int cmd, void *arg))
521{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800522 char sbuf[128];
523 void *mbuf = NULL;
524 void *parg = NULL;
525 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800527 /* Copy arguments into temp kernel buffer */
528 switch (_IOC_DIR(cmd)) {
529 case _IOC_NONE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 /*
531 * For this command, the pointer is actually an integer
532 * argument.
533 */
534 parg = (void *) arg;
535 break;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800536 case _IOC_READ: /* some v4l ioctls are marked wrong ... */
537 case _IOC_WRITE:
538 case (_IOC_WRITE | _IOC_READ):
539 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
Michael Krufky50c25ff2006-01-09 15:25:34 -0200540 parg = sbuf;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800541 } else {
Michael Krufky50c25ff2006-01-09 15:25:34 -0200542 /* too big to allocate from stack */
543 mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
544 if (NULL == mbuf)
545 return -ENOMEM;
546 parg = mbuf;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800549 err = -EFAULT;
550 if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
Michael Krufky50c25ff2006-01-09 15:25:34 -0200551 goto out;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800552 break;
553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800555 /* call driver */
Arnd Bergmann16ef8de2010-04-27 00:24:00 +0200556 if ((err = func(file, cmd, parg)) == -ENOIOCTLCMD)
Wanlong Gaod9751fd2012-08-27 03:23:14 -0300557 err = -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800559 if (err < 0)
560 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800562 /* Copy results into user buffer */
563 switch (_IOC_DIR(cmd))
564 {
565 case _IOC_READ:
566 case (_IOC_WRITE | _IOC_READ):
567 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
Michael Krufky50c25ff2006-01-09 15:25:34 -0200568 err = -EFAULT;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800569 break;
570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572out:
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800573 kfree(mbuf);
574 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
Kay Sieversa5f4c0c2008-10-27 22:27:37 -0300577static int dvb_uevent(struct device *dev, struct kobj_uevent_env *env)
578{
579 struct dvb_device *dvbdev = dev_get_drvdata(dev);
580
Kay Sieversa5f4c0c2008-10-27 22:27:37 -0300581 add_uevent_var(env, "DVB_ADAPTER_NUM=%d", dvbdev->adapter->num);
Kay Sievers763d19b2008-12-31 23:35:24 -0300582 add_uevent_var(env, "DVB_DEVICE_TYPE=%s", dnames[dvbdev->type]);
583 add_uevent_var(env, "DVB_DEVICE_NUM=%d", dvbdev->id);
Kay Sieversa5f4c0c2008-10-27 22:27:37 -0300584 return 0;
585}
586
Al Viro2c9ede52011-07-23 20:24:48 -0400587static char *dvb_devnode(struct device *dev, umode_t *mode)
Kay Sievers8a8bdcc2009-04-30 15:23:42 +0200588{
589 struct dvb_device *dvbdev = dev_get_drvdata(dev);
590
591 return kasprintf(GFP_KERNEL, "dvb/adapter%d/%s%d",
592 dvbdev->adapter->num, dnames[dvbdev->type], dvbdev->id);
593}
594
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596static int __init init_dvbdev(void)
597{
598 int retval;
599 dev_t dev = MKDEV(DVB_MAJOR, 0);
600
601 if ((retval = register_chrdev_region(dev, MAX_DVB_MINORS, "DVB")) != 0) {
Simon Arlott900858e2007-05-06 21:06:32 -0300602 printk(KERN_ERR "dvb-core: unable to get major %d\n", DVB_MAJOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 return retval;
604 }
605
606 cdev_init(&dvb_device_cdev, &dvb_device_fops);
607 if ((retval = cdev_add(&dvb_device_cdev, dev, MAX_DVB_MINORS)) != 0) {
Simon Arlott900858e2007-05-06 21:06:32 -0300608 printk(KERN_ERR "dvb-core: unable register character device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 goto error;
610 }
611
gregkh@suse.de56b22932005-03-23 10:01:41 -0800612 dvb_class = class_create(THIS_MODULE, "dvb");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (IS_ERR(dvb_class)) {
614 retval = PTR_ERR(dvb_class);
615 goto error;
616 }
Kay Sieversa5f4c0c2008-10-27 22:27:37 -0300617 dvb_class->dev_uevent = dvb_uevent;
Kay Sieverse454cea2009-09-18 23:01:12 +0200618 dvb_class->devnode = dvb_devnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 return 0;
620
621error:
622 cdev_del(&dvb_device_cdev);
623 unregister_chrdev_region(dev, MAX_DVB_MINORS);
624 return retval;
625}
626
627
628static void __exit exit_dvbdev(void)
629{
gregkh@suse.de56b22932005-03-23 10:01:41 -0800630 class_destroy(dvb_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 cdev_del(&dvb_device_cdev);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800632 unregister_chrdev_region(MKDEV(DVB_MAJOR, 0), MAX_DVB_MINORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634
Simon Arlott4abdcf92007-05-06 20:56:14 -0300635subsys_initcall(init_dvbdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636module_exit(exit_dvbdev);
637
638MODULE_DESCRIPTION("DVB Core Driver");
639MODULE_AUTHOR("Marcus Metzler, Ralph Metzler, Holger Waechtler");
640MODULE_LICENSE("GPL");