blob: 9fd687a1f593da3671693848afa6e1d02eff1831 [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
Mauro Carvalho Chehab188d2d52015-08-31 13:23:03 -030037/* Due to enum tuner_pad_index */
38#include <media/tuner.h>
39
Arnd Bergmann72024f12010-09-11 19:56:45 +020040static DEFINE_MUTEX(dvbdev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041static int dvbdev_debug;
42
43module_param(dvbdev_debug, int, 0644);
44MODULE_PARM_DESC(dvbdev_debug, "Turn on/off device debugging (default:off).");
45
46#define dprintk if (dvbdev_debug) printk
47
48static LIST_HEAD(dvb_adapter_list);
Ingo Molnar1e4baed2006-01-15 07:52:23 -020049static DEFINE_MUTEX(dvbdev_register_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51static const char * const dnames[] = {
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -080052 "video", "audio", "sec", "frontend", "demux", "dvr", "ca",
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 "net", "osd"
54};
55
Andreas Oberritter5dd3f302008-10-23 12:11:19 -030056#ifdef CONFIG_DVB_DYNAMIC_MINORS
57#define MAX_DVB_MINORS 256
58#define DVB_MAX_IDS MAX_DVB_MINORS
59#else
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#define DVB_MAX_IDS 4
61#define nums2minor(num,type,id) ((num << 6) | (id << 4) | type)
62#define MAX_DVB_MINORS (DVB_MAX_ADAPTERS*64)
Andreas Oberritter5dd3f302008-10-23 12:11:19 -030063#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
gregkh@suse.de56b22932005-03-23 10:01:41 -080065static struct class *dvb_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Andreas Oberritter5dd3f302008-10-23 12:11:19 -030067static struct dvb_device *dvb_minors[MAX_DVB_MINORS];
68static DECLARE_RWSEM(minor_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70static int dvb_device_open(struct inode *inode, struct file *file)
71{
72 struct dvb_device *dvbdev;
73
Arnd Bergmann72024f12010-09-11 19:56:45 +020074 mutex_lock(&dvbdev_mutex);
Andreas Oberritter5dd3f302008-10-23 12:11:19 -030075 down_read(&minor_rwsem);
76 dvbdev = dvb_minors[iminor(inode)];
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 if (dvbdev && dvbdev->fops) {
79 int err = 0;
Al Viroe84f9e52013-09-22 14:17:15 -040080 const struct file_operations *new_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Al Viroe84f9e52013-09-22 14:17:15 -040082 new_fops = fops_get(dvbdev->fops);
83 if (!new_fops)
Laurent Pinchartf41ced82009-01-06 14:40:40 -080084 goto fail;
Al Viroe84f9e52013-09-22 14:17:15 -040085 file->private_data = dvbdev;
86 replace_fops(file, new_fops);
87 if (file->f_op->open)
Michael Krufky50c25ff2006-01-09 15:25:34 -020088 err = file->f_op->open(inode,file);
Andreas Oberritter5dd3f302008-10-23 12:11:19 -030089 up_read(&minor_rwsem);
Arnd Bergmann72024f12010-09-11 19:56:45 +020090 mutex_unlock(&dvbdev_mutex);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -080091 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 }
Laurent Pinchartf41ced82009-01-06 14:40:40 -080093fail:
Andreas Oberritter5dd3f302008-10-23 12:11:19 -030094 up_read(&minor_rwsem);
Arnd Bergmann72024f12010-09-11 19:56:45 +020095 mutex_unlock(&dvbdev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 return -ENODEV;
97}
98
99
Jan Engelhardt27a643b2008-04-22 14:42:01 -0300100static const struct file_operations dvb_device_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
102 .owner = THIS_MODULE,
103 .open = dvb_device_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200104 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105};
106
Greg Kroah-Hartman7e7654a2007-09-12 15:06:57 -0700107static struct cdev dvb_device_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109int dvb_generic_open(struct inode *inode, struct file *file)
110{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800111 struct dvb_device *dvbdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800113 if (!dvbdev)
114 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 if (!dvbdev->users)
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800117 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800120 if (!dvbdev->readers)
121 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 dvbdev->readers--;
123 } else {
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800124 if (!dvbdev->writers)
125 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 dvbdev->writers--;
127 }
128
129 dvbdev->users--;
130 return 0;
131}
132EXPORT_SYMBOL(dvb_generic_open);
133
134
135int dvb_generic_release(struct inode *inode, struct file *file)
136{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800137 struct dvb_device *dvbdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 if (!dvbdev)
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800140 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
143 dvbdev->readers++;
144 } else {
145 dvbdev->writers++;
146 }
147
148 dvbdev->users++;
149 return 0;
150}
151EXPORT_SYMBOL(dvb_generic_release);
152
153
Arnd Bergmann16ef8de2010-04-27 00:24:00 +0200154long dvb_generic_ioctl(struct file *file,
155 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800157 struct dvb_device *dvbdev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800159 if (!dvbdev)
160 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 if (!dvbdev->kernel_ioctl)
163 return -EINVAL;
164
Arnd Bergmann72024f12010-09-11 19:56:45 +0200165 return dvb_usercopy(file, cmd, arg, dvbdev->kernel_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167EXPORT_SYMBOL(dvb_generic_ioctl);
168
169
170static int dvbdev_get_free_id (struct dvb_adapter *adap, int type)
171{
172 u32 id = 0;
173
174 while (id < DVB_MAX_IDS) {
Trent Piepho79482612007-10-10 05:37:39 -0300175 struct dvb_device *dev;
176 list_for_each_entry(dev, &adap->device_list, list_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 if (dev->type == type && dev->id == id)
178 goto skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return id;
180skip:
181 id++;
182 }
183 return -ENFILE;
184}
185
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300186static void dvb_media_device_free(struct dvb_device *dvbdev)
187{
188#if defined(CONFIG_MEDIA_CONTROLLER_DVB)
189 if (dvbdev->entity) {
190 media_device_unregister_entity(dvbdev->entity);
191 kfree(dvbdev->entity);
192 kfree(dvbdev->pads);
193 dvbdev->entity = NULL;
194 dvbdev->pads = NULL;
195 }
196
197 if (dvbdev->tsout_entity) {
198 int i;
199
200 for (i = 0; i < dvbdev->tsout_num_entities; i++) {
201 media_device_unregister_entity(&dvbdev->tsout_entity[i]);
202 kfree(dvbdev->tsout_entity[i].name);
203 }
204 kfree(dvbdev->tsout_entity);
205 kfree(dvbdev->tsout_pads);
206 dvbdev->tsout_entity = NULL;
207 dvbdev->tsout_pads = NULL;
208
209 dvbdev->tsout_num_entities = 0;
210 }
211
212 if (dvbdev->intf_devnode) {
213 media_devnode_remove(dvbdev->intf_devnode);
214 dvbdev->intf_devnode = NULL;
215 }
Mauro Carvalho Chehab0230d602015-12-29 11:52:23 -0200216
217 if (dvbdev->adapter->conn) {
218 media_device_unregister_entity(dvbdev->adapter->conn);
219 dvbdev->adapter->conn = NULL;
220 kfree(dvbdev->adapter->conn_pads);
221 dvbdev->adapter->conn_pads = NULL;
222 }
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300223#endif
224}
225
Mauro Carvalho Chehab0820eb52015-12-28 10:30:06 -0200226#if defined(CONFIG_MEDIA_CONTROLLER_DVB)
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300227static int dvb_create_tsout_entity(struct dvb_device *dvbdev,
Mauro Carvalho Chehabdf2f94e2015-08-21 16:18:18 -0300228 const char *name, int npads)
Mauro Carvalho Chehaba0246e02015-01-02 12:19:51 -0300229{
Mauro Carvalho Chehabdf2f94e2015-08-21 16:18:18 -0300230 int i, ret = 0;
231
232 dvbdev->tsout_pads = kcalloc(npads, sizeof(*dvbdev->tsout_pads),
233 GFP_KERNEL);
234 if (!dvbdev->tsout_pads)
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300235 return -ENOMEM;
236
Mauro Carvalho Chehabdf2f94e2015-08-21 16:18:18 -0300237 dvbdev->tsout_entity = kcalloc(npads, sizeof(*dvbdev->tsout_entity),
238 GFP_KERNEL);
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300239 if (!dvbdev->tsout_entity)
240 return -ENOMEM;
241
242 dvbdev->tsout_num_entities = npads;
243
Mauro Carvalho Chehabdf2f94e2015-08-21 16:18:18 -0300244 for (i = 0; i < npads; i++) {
245 struct media_pad *pads = &dvbdev->tsout_pads[i];
246 struct media_entity *entity = &dvbdev->tsout_entity[i];
247
248 entity->name = kasprintf(GFP_KERNEL, "%s #%d", name, i);
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300249 if (!entity->name)
250 return -ENOMEM;
Mauro Carvalho Chehabdf2f94e2015-08-21 16:18:18 -0300251
Mauro Carvalho Chehab4ca72ef2015-12-10 17:25:41 -0200252 entity->function = MEDIA_ENT_F_IO_DTV;
Mauro Carvalho Chehabdf2f94e2015-08-21 16:18:18 -0300253 pads->flags = MEDIA_PAD_FL_SINK;
254
Mauro Carvalho Chehabab22e772015-12-11 07:44:40 -0200255 ret = media_entity_pads_init(entity, 1, pads);
Mauro Carvalho Chehabdf2f94e2015-08-21 16:18:18 -0300256 if (ret < 0)
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300257 return ret;
Mauro Carvalho Chehabdf2f94e2015-08-21 16:18:18 -0300258
259 ret = media_device_register_entity(dvbdev->adapter->mdev,
260 entity);
261 if (ret < 0)
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300262 return ret;
Mauro Carvalho Chehabdf2f94e2015-08-21 16:18:18 -0300263 }
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300264 return 0;
Mauro Carvalho Chehabdf2f94e2015-08-21 16:18:18 -0300265}
266
267#define DEMUX_TSOUT "demux-tsout"
268#define DVR_TSOUT "dvr-tsout"
269
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300270static int dvb_create_media_entity(struct dvb_device *dvbdev,
271 int type, int demux_sink_pads)
Mauro Carvalho Chehabdf2f94e2015-08-21 16:18:18 -0300272{
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300273 int i, ret, npads;
Mauro Carvalho Chehaba0246e02015-01-02 12:19:51 -0300274
Mauro Carvalho Chehab8211b182015-08-21 08:20:22 -0300275 switch (type) {
276 case DVB_DEVICE_FRONTEND:
277 npads = 2;
278 break;
Mauro Carvalho Chehabdf2f94e2015-08-21 16:18:18 -0300279 case DVB_DEVICE_DVR:
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300280 ret = dvb_create_tsout_entity(dvbdev, DVR_TSOUT,
281 demux_sink_pads);
282 return ret;
Mauro Carvalho Chehab8211b182015-08-21 08:20:22 -0300283 case DVB_DEVICE_DEMUX:
Mauro Carvalho Chehabdf2f94e2015-08-21 16:18:18 -0300284 npads = 1 + demux_sink_pads;
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300285 ret = dvb_create_tsout_entity(dvbdev, DEMUX_TSOUT,
286 demux_sink_pads);
287 if (ret < 0)
288 return ret;
Mauro Carvalho Chehab8211b182015-08-21 08:20:22 -0300289 break;
290 case DVB_DEVICE_CA:
291 npads = 2;
292 break;
293 case DVB_DEVICE_NET:
294 /*
295 * We should be creating entities for the MPE/ULE
296 * decapsulation hardware (or software implementation).
297 *
298 * However, the number of for the MPE/ULE decaps may not be
299 * fixed. As we don't have yet dynamic support for PADs at
300 * the Media Controller, let's not create the decap
301 * entities yet.
302 */
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300303 return 0;
Mauro Carvalho Chehab8211b182015-08-21 08:20:22 -0300304 default:
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300305 return 0;
Mauro Carvalho Chehab8211b182015-08-21 08:20:22 -0300306 }
Mauro Carvalho Chehaba0246e02015-01-02 12:19:51 -0300307
308 dvbdev->entity = kzalloc(sizeof(*dvbdev->entity), GFP_KERNEL);
309 if (!dvbdev->entity)
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300310 return -ENOMEM;
Mauro Carvalho Chehaba0246e02015-01-02 12:19:51 -0300311
Mauro Carvalho Chehaba0246e02015-01-02 12:19:51 -0300312 dvbdev->entity->name = dvbdev->name;
Mauro Carvalho Chehab172e9d32015-01-03 01:59:53 -0300313
Mauro Carvalho Chehab172e9d32015-01-03 01:59:53 -0300314 if (npads) {
315 dvbdev->pads = kcalloc(npads, sizeof(*dvbdev->pads),
316 GFP_KERNEL);
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300317 if (!dvbdev->pads)
318 return -ENOMEM;
Mauro Carvalho Chehab172e9d32015-01-03 01:59:53 -0300319 }
320
Mauro Carvalho Chehaba0246e02015-01-02 12:19:51 -0300321 switch (type) {
322 case DVB_DEVICE_FRONTEND:
Mauro Carvalho Chehab4ca72ef2015-12-10 17:25:41 -0200323 dvbdev->entity->function = MEDIA_ENT_F_DTV_DEMOD;
Mauro Carvalho Chehab3bde1b72015-01-03 16:35:53 -0300324 dvbdev->pads[0].flags = MEDIA_PAD_FL_SINK;
325 dvbdev->pads[1].flags = MEDIA_PAD_FL_SOURCE;
Mauro Carvalho Chehaba0246e02015-01-02 12:19:51 -0300326 break;
327 case DVB_DEVICE_DEMUX:
Mauro Carvalho Chehab4ca72ef2015-12-10 17:25:41 -0200328 dvbdev->entity->function = MEDIA_ENT_F_TS_DEMUX;
Mauro Carvalho Chehab3bde1b72015-01-03 16:35:53 -0300329 dvbdev->pads[0].flags = MEDIA_PAD_FL_SINK;
Mauro Carvalho Chehabdf2f94e2015-08-21 16:18:18 -0300330 for (i = 1; i < npads; i++)
331 dvbdev->pads[i].flags = MEDIA_PAD_FL_SOURCE;
Mauro Carvalho Chehaba0246e02015-01-02 12:19:51 -0300332 break;
Mauro Carvalho Chehaba0246e02015-01-02 12:19:51 -0300333 case DVB_DEVICE_CA:
Mauro Carvalho Chehab4ca72ef2015-12-10 17:25:41 -0200334 dvbdev->entity->function = MEDIA_ENT_F_DTV_CA;
Mauro Carvalho Chehab3bde1b72015-01-03 16:35:53 -0300335 dvbdev->pads[0].flags = MEDIA_PAD_FL_SINK;
336 dvbdev->pads[1].flags = MEDIA_PAD_FL_SOURCE;
Mauro Carvalho Chehaba0246e02015-01-02 12:19:51 -0300337 break;
Mauro Carvalho Chehaba0246e02015-01-02 12:19:51 -0300338 default:
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300339 /* Should never happen, as the first switch prevents it */
Mauro Carvalho Chehaba0246e02015-01-02 12:19:51 -0300340 kfree(dvbdev->entity);
Mauro Carvalho Chehab172e9d32015-01-03 01:59:53 -0300341 kfree(dvbdev->pads);
Mauro Carvalho Chehaba0246e02015-01-02 12:19:51 -0300342 dvbdev->entity = NULL;
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300343 dvbdev->pads = NULL;
344 return 0;
Mauro Carvalho Chehaba0246e02015-01-02 12:19:51 -0300345 }
346
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300347 if (npads) {
Mauro Carvalho Chehabab22e772015-12-11 07:44:40 -0200348 ret = media_entity_pads_init(dvbdev->entity, npads, dvbdev->pads);
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300349 if (ret)
350 return ret;
351 }
352 ret = media_device_register_entity(dvbdev->adapter->mdev,
353 dvbdev->entity);
354 if (ret)
Xiubo Lieac287a2016-01-15 03:15:00 -0200355 return ret;
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300356
Mauro Carvalho Chehab8211b182015-08-21 08:20:22 -0300357 printk(KERN_DEBUG "%s: media entity '%s' registered.\n",
Mauro Carvalho Chehaba0246e02015-01-02 12:19:51 -0300358 __func__, dvbdev->entity->name);
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300359
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300360 return 0;
Mauro Carvalho Chehaba0246e02015-01-02 12:19:51 -0300361}
Mauro Carvalho Chehab0820eb52015-12-28 10:30:06 -0200362#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300364static int dvb_register_media_device(struct dvb_device *dvbdev,
365 int type, int minor,
366 unsigned demux_sink_pads)
Mauro Carvalho Chehab8211b182015-08-21 08:20:22 -0300367{
368#if defined(CONFIG_MEDIA_CONTROLLER_DVB)
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300369 struct media_link *link;
Mauro Carvalho Chehab8211b182015-08-21 08:20:22 -0300370 u32 intf_type;
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300371 int ret;
Mauro Carvalho Chehab8211b182015-08-21 08:20:22 -0300372
373 if (!dvbdev->adapter->mdev)
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300374 return 0;
Mauro Carvalho Chehab8211b182015-08-21 08:20:22 -0300375
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300376 ret = dvb_create_media_entity(dvbdev, type, demux_sink_pads);
377 if (ret)
378 return ret;
Mauro Carvalho Chehab8211b182015-08-21 08:20:22 -0300379
380 switch (type) {
381 case DVB_DEVICE_FRONTEND:
382 intf_type = MEDIA_INTF_T_DVB_FE;
383 break;
384 case DVB_DEVICE_DEMUX:
385 intf_type = MEDIA_INTF_T_DVB_DEMUX;
386 break;
387 case DVB_DEVICE_DVR:
388 intf_type = MEDIA_INTF_T_DVB_DVR;
389 break;
390 case DVB_DEVICE_CA:
391 intf_type = MEDIA_INTF_T_DVB_CA;
392 break;
393 case DVB_DEVICE_NET:
394 intf_type = MEDIA_INTF_T_DVB_NET;
395 break;
396 default:
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300397 return 0;
Mauro Carvalho Chehab8211b182015-08-21 08:20:22 -0300398 }
399
400 dvbdev->intf_devnode = media_devnode_create(dvbdev->adapter->mdev,
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300401 intf_type, 0,
Mauro Carvalho Chehab0b3b72df92015-09-09 08:19:25 -0300402 DVB_MAJOR, minor);
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300403
404 if (!dvbdev->intf_devnode)
405 return -ENOMEM;
Mauro Carvalho Chehab8211b182015-08-21 08:20:22 -0300406
407 /*
408 * Create the "obvious" link, e. g. the ones that represent
409 * a direct association between an interface and an entity.
410 * Other links should be created elsewhere, like:
411 * DVB FE intf -> tuner
412 * DVB demux intf -> dvr
413 */
414
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300415 if (!dvbdev->entity)
416 return 0;
Mauro Carvalho Chehab8211b182015-08-21 08:20:22 -0300417
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300418 link = media_create_intf_link(dvbdev->entity, &dvbdev->intf_devnode->intf,
419 MEDIA_LNK_FL_ENABLED);
420 if (!link)
421 return -ENOMEM;
Mauro Carvalho Chehab8211b182015-08-21 08:20:22 -0300422#endif
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300423 return 0;
Mauro Carvalho Chehab8211b182015-08-21 08:20:22 -0300424}
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426int dvb_register_device(struct dvb_adapter *adap, struct dvb_device **pdvbdev,
Mauro Carvalho Chehabdf2f94e2015-08-21 16:18:18 -0300427 const struct dvb_device *template, void *priv, int type,
428 int demux_sink_pads)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
430 struct dvb_device *dvbdev;
Marcel Siegertb6190102007-02-13 09:46:55 -0300431 struct file_operations *dvbdevfops;
Kay Sievers5f553382007-08-15 14:00:09 -0300432 struct device *clsdev;
Andreas Oberritter5dd3f302008-10-23 12:11:19 -0300433 int minor;
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300434 int id, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Simon Arlottc2788502007-03-10 06:21:25 -0300436 mutex_lock(&dvbdev_register_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Marcel Siegertb6190102007-02-13 09:46:55 -0300438 if ((id = dvbdev_get_free_id (adap, type)) < 0){
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200439 mutex_unlock(&dvbdev_register_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 *pdvbdev = NULL;
Harvey Harrison46b4f7c2008-04-08 23:20:00 -0300441 printk(KERN_ERR "%s: couldn't find free device id\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return -ENFILE;
443 }
444
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300445 *pdvbdev = dvbdev = kzalloc(sizeof(*dvbdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Marcel Siegertb6190102007-02-13 09:46:55 -0300447 if (!dvbdev){
448 mutex_unlock(&dvbdev_register_lock);
449 return -ENOMEM;
450 }
451
452 dvbdevfops = kzalloc(sizeof(struct file_operations), GFP_KERNEL);
453
454 if (!dvbdevfops){
455 kfree (dvbdev);
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200456 mutex_unlock(&dvbdev_register_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return -ENOMEM;
458 }
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 memcpy(dvbdev, template, sizeof(struct dvb_device));
461 dvbdev->type = type;
462 dvbdev->id = id;
463 dvbdev->adapter = adap;
464 dvbdev->priv = priv;
Marcel Siegertb6190102007-02-13 09:46:55 -0300465 dvbdev->fops = dvbdevfops;
Markus Rechbergerca5be9c2007-04-14 10:18:58 -0300466 init_waitqueue_head (&dvbdev->wait_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Jan Engelhardt784e29d2009-01-11 06:12:43 -0300468 memcpy(dvbdevfops, template->fops, sizeof(struct file_operations));
469 dvbdevfops->owner = adap->module;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 list_add_tail (&dvbdev->list_head, &adap->device_list);
472
Andreas Oberritter5dd3f302008-10-23 12:11:19 -0300473 down_write(&minor_rwsem);
474#ifdef CONFIG_DVB_DYNAMIC_MINORS
475 for (minor = 0; minor < MAX_DVB_MINORS; minor++)
476 if (dvb_minors[minor] == NULL)
477 break;
478
479 if (minor == MAX_DVB_MINORS) {
480 kfree(dvbdevfops);
481 kfree(dvbdev);
Santosh Nayak82163ed2012-06-23 07:59:54 -0300482 up_write(&minor_rwsem);
Andreas Oberritter5dd3f302008-10-23 12:11:19 -0300483 mutex_unlock(&dvbdev_register_lock);
484 return -EINVAL;
485 }
486#else
487 minor = nums2minor(adap->num, type, id);
488#endif
489
490 dvbdev->minor = minor;
491 dvb_minors[minor] = dvbdev;
492 up_write(&minor_rwsem);
493
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300494 ret = dvb_register_media_device(dvbdev, type, minor, demux_sink_pads);
495 if (ret) {
496 printk(KERN_ERR
497 "%s: dvb_register_media_device failed to create the mediagraph\n",
498 __func__);
499
500 dvb_media_device_free(dvbdev);
501 kfree(dvbdevfops);
502 kfree(dvbdev);
503 up_write(&minor_rwsem);
504 mutex_unlock(&dvbdev_register_lock);
505 return ret;
506 }
507
Andrew de Quinceyf47f4762006-04-04 09:41:47 -0300508 mutex_unlock(&dvbdev_register_lock);
509
Greg Kroah-Hartmana9b12612008-07-21 20:03:34 -0700510 clsdev = device_create(dvb_class, adap->device,
Hans Verkuilb7496782008-11-03 05:38:43 -0300511 MKDEV(DVB_MAJOR, minor),
Kay Sieversa5f4c0c2008-10-27 22:27:37 -0300512 dvbdev, "dvb%d.%s%d", adap->num, dnames[type], id);
Simon Arlott4abdcf92007-05-06 20:56:14 -0300513 if (IS_ERR(clsdev)) {
514 printk(KERN_ERR "%s: failed to create device dvb%d.%s%d (%ld)\n",
Harvey Harrison46b4f7c2008-04-08 23:20:00 -0300515 __func__, adap->num, dnames[type], id, PTR_ERR(clsdev));
Simon Arlott4abdcf92007-05-06 20:56:14 -0300516 return PTR_ERR(clsdev);
517 }
Simon Arlott900858e2007-05-06 21:06:32 -0300518 dprintk(KERN_DEBUG "DVB: register adapter%d/%s%d @ minor: %i (0x%02x)\n",
Andreas Oberritter5dd3f302008-10-23 12:11:19 -0300519 adap->num, dnames[type], id, minor, minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 return 0;
522}
523EXPORT_SYMBOL(dvb_register_device);
524
525
526void dvb_unregister_device(struct dvb_device *dvbdev)
527{
528 if (!dvbdev)
529 return;
530
Andreas Oberritter5dd3f302008-10-23 12:11:19 -0300531 down_write(&minor_rwsem);
532 dvb_minors[dvbdev->minor] = NULL;
533 up_write(&minor_rwsem);
534
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300535 dvb_media_device_free(dvbdev);
536
Andreas Oberritter5dd3f302008-10-23 12:11:19 -0300537 device_destroy(dvb_class, MKDEV(DVB_MAJOR, dvbdev->minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 list_del (&dvbdev->list_head);
Marcel Siegertb6190102007-02-13 09:46:55 -0300540 kfree (dvbdev->fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 kfree (dvbdev);
542}
543EXPORT_SYMBOL(dvb_unregister_device);
544
Mauro Carvalho Chehab41551092015-01-03 16:52:34 -0300545
Mauro Carvalho Chehab41551092015-01-03 16:52:34 -0300546#ifdef CONFIG_MEDIA_CONTROLLER_DVB
Mauro Carvalho Chehab17813e22015-09-06 13:28:48 -0300547
548static int dvb_create_io_intf_links(struct dvb_adapter *adap,
549 struct media_interface *intf,
550 char *name)
551{
552 struct media_device *mdev = adap->mdev;
553 struct media_entity *entity;
554 struct media_link *link;
555
556 media_device_for_each_entity(entity, mdev) {
557 if (entity->function == MEDIA_ENT_F_IO_DTV) {
558 if (strncmp(entity->name, name, strlen(name)))
559 continue;
560 link = media_create_intf_link(entity, intf,
561 MEDIA_LNK_FL_ENABLED);
562 if (!link)
563 return -ENOMEM;
564 }
565 }
566 return 0;
567}
568
Mauro Carvalho Chehab0230d602015-12-29 11:52:23 -0200569int dvb_create_media_graph(struct dvb_adapter *adap,
570 bool create_rf_connector)
Mauro Carvalho Chehab480884b2015-03-02 10:49:04 -0300571{
572 struct media_device *mdev = adap->mdev;
Mauro Carvalho Chehab0230d602015-12-29 11:52:23 -0200573 struct media_entity *entity, *tuner = NULL, *demod = NULL, *conn;
Mauro Carvalho Chehabdf2f94e2015-08-21 16:18:18 -0300574 struct media_entity *demux = NULL, *ca = NULL;
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300575 struct media_link *link;
Mauro Carvalho Chehab8ddb90d2015-08-21 09:32:38 -0300576 struct media_interface *intf;
Mauro Carvalho Chehabdf2f94e2015-08-21 16:18:18 -0300577 unsigned demux_pad = 0;
578 unsigned dvr_pad = 0;
Mauro Carvalho Chehaba0cce2a2015-12-30 10:11:53 -0200579 unsigned ntuner = 0, ndemod = 0;
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300580 int ret;
Mauro Carvalho Chehab0230d602015-12-29 11:52:23 -0200581 static const char *connector_name = "Television";
Mauro Carvalho Chehab41551092015-01-03 16:52:34 -0300582
583 if (!mdev)
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300584 return 0;
Mauro Carvalho Chehab41551092015-01-03 16:52:34 -0300585
586 media_device_for_each_entity(entity, mdev) {
Mauro Carvalho Chehab0e576b72015-09-06 09:33:39 -0300587 switch (entity->function) {
Mauro Carvalho Chehab4ca72ef2015-12-10 17:25:41 -0200588 case MEDIA_ENT_F_TUNER:
Mauro Carvalho Chehab41551092015-01-03 16:52:34 -0300589 tuner = entity;
Mauro Carvalho Chehaba0cce2a2015-12-30 10:11:53 -0200590 ntuner++;
Mauro Carvalho Chehab41551092015-01-03 16:52:34 -0300591 break;
Mauro Carvalho Chehab4ca72ef2015-12-10 17:25:41 -0200592 case MEDIA_ENT_F_DTV_DEMOD:
Mauro Carvalho Chehab32fdc0e2015-08-21 11:40:34 -0300593 demod = entity;
Mauro Carvalho Chehaba0cce2a2015-12-30 10:11:53 -0200594 ndemod++;
Mauro Carvalho Chehab41551092015-01-03 16:52:34 -0300595 break;
Mauro Carvalho Chehab4ca72ef2015-12-10 17:25:41 -0200596 case MEDIA_ENT_F_TS_DEMUX:
Mauro Carvalho Chehab41551092015-01-03 16:52:34 -0300597 demux = entity;
598 break;
Mauro Carvalho Chehab4ca72ef2015-12-10 17:25:41 -0200599 case MEDIA_ENT_F_DTV_CA:
Mauro Carvalho Chehab41551092015-01-03 16:52:34 -0300600 ca = entity;
601 break;
602 }
603 }
604
Mauro Carvalho Chehaba0cce2a2015-12-30 10:11:53 -0200605 /*
606 * Prepare to signalize to media_create_pad_links() that multiple
607 * entities of the same type exists and a 1:n or n:1 links need to be
608 * created.
609 * NOTE: if both tuner and demod have multiple instances, it is up
610 * to the caller driver to create such links.
611 */
612 if (ntuner > 1)
613 tuner = NULL;
614 if (ndemod > 1)
615 demod = NULL;
616
Mauro Carvalho Chehab0230d602015-12-29 11:52:23 -0200617 if (create_rf_connector) {
618 conn = kzalloc(sizeof(*conn), GFP_KERNEL);
619 if (!conn)
620 return -ENOMEM;
621 adap->conn = conn;
622
Xiubo Li5c3fbc52016-01-15 03:14:58 -0200623 adap->conn_pads = kzalloc(sizeof(*adap->conn_pads), GFP_KERNEL);
Mauro Carvalho Chehab0230d602015-12-29 11:52:23 -0200624 if (!adap->conn_pads)
625 return -ENOMEM;
626
627 conn->flags = MEDIA_ENT_FL_CONNECTOR;
628 conn->function = MEDIA_ENT_F_CONN_RF;
629 conn->name = connector_name;
630 adap->conn_pads->flags = MEDIA_PAD_FL_SOURCE;
631
632 ret = media_entity_pads_init(conn, 1, adap->conn_pads);
633 if (ret)
634 return ret;
635
636 ret = media_device_register_entity(mdev, conn);
637 if (ret)
638 return ret;
639
Mauro Carvalho Chehaba0cce2a2015-12-30 10:11:53 -0200640 if (!ntuner)
641 ret = media_create_pad_links(mdev,
642 MEDIA_ENT_F_CONN_RF,
643 conn, 0,
644 MEDIA_ENT_F_DTV_DEMOD,
645 demod, 0,
646 MEDIA_LNK_FL_ENABLED,
647 false);
Mauro Carvalho Chehab0230d602015-12-29 11:52:23 -0200648 else
Mauro Carvalho Chehaba0cce2a2015-12-30 10:11:53 -0200649 ret = media_create_pad_links(mdev,
650 MEDIA_ENT_F_CONN_RF,
651 conn, 0,
652 MEDIA_ENT_F_TUNER,
653 tuner, TUNER_PAD_RF_INPUT,
654 MEDIA_LNK_FL_ENABLED,
655 false);
Mauro Carvalho Chehab0230d602015-12-29 11:52:23 -0200656 if (ret)
657 return ret;
658 }
659
Mauro Carvalho Chehaba0cce2a2015-12-30 10:11:53 -0200660 if (ntuner && ndemod) {
661 ret = media_create_pad_links(mdev,
662 MEDIA_ENT_F_TUNER,
Mauro Carvalho Chehab163c9bc2016-01-27 08:39:33 -0200663 tuner, TUNER_PAD_OUTPUT,
Mauro Carvalho Chehaba0cce2a2015-12-30 10:11:53 -0200664 MEDIA_ENT_F_DTV_DEMOD,
665 demod, 0, MEDIA_LNK_FL_ENABLED,
666 false);
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300667 if (ret)
668 return ret;
669 }
Mauro Carvalho Chehab41551092015-01-03 16:52:34 -0300670
Mauro Carvalho Chehaba0cce2a2015-12-30 10:11:53 -0200671 if (ndemod && demux) {
672 ret = media_create_pad_links(mdev,
673 MEDIA_ENT_F_DTV_DEMOD,
674 demod, 1,
675 MEDIA_ENT_F_TS_DEMUX,
676 demux, 0, MEDIA_LNK_FL_ENABLED,
677 false);
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300678 if (ret)
679 return -ENOMEM;
680 }
681 if (demux && ca) {
682 ret = media_create_pad_link(demux, 1, ca,
683 0, MEDIA_LNK_FL_ENABLED);
684 if (!ret)
685 return -ENOMEM;
686 }
Mauro Carvalho Chehab8ddb90d2015-08-21 09:32:38 -0300687
Mauro Carvalho Chehabdf2f94e2015-08-21 16:18:18 -0300688 /* Create demux links for each ringbuffer/pad */
689 if (demux) {
690 media_device_for_each_entity(entity, mdev) {
Mauro Carvalho Chehab4ca72ef2015-12-10 17:25:41 -0200691 if (entity->function == MEDIA_ENT_F_IO_DTV) {
Mauro Carvalho Chehabdf2f94e2015-08-21 16:18:18 -0300692 if (!strncmp(entity->name, DVR_TSOUT,
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300693 strlen(DVR_TSOUT))) {
694 ret = media_create_pad_link(demux,
695 ++dvr_pad,
696 entity, 0, 0);
697 if (ret)
698 return ret;
699 }
Mauro Carvalho Chehabdf2f94e2015-08-21 16:18:18 -0300700 if (!strncmp(entity->name, DEMUX_TSOUT,
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300701 strlen(DEMUX_TSOUT))) {
702 ret = media_create_pad_link(demux,
Mauro Carvalho Chehabdf2f94e2015-08-21 16:18:18 -0300703 ++demux_pad,
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300704 entity, 0, 0);
705 if (ret)
706 return ret;
707 }
Mauro Carvalho Chehabdf2f94e2015-08-21 16:18:18 -0300708 }
709 }
710 }
711
Mauro Carvalho Chehab8ed07142015-09-06 13:38:23 -0300712 /* Create interface links for FE->tuner, DVR->demux and CA->ca */
Mauro Carvalho Chehabcf975a42015-08-23 07:51:22 -0300713 media_device_for_each_intf(intf, mdev) {
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300714 if (intf->type == MEDIA_INTF_T_DVB_CA && ca) {
715 link = media_create_intf_link(ca, intf,
716 MEDIA_LNK_FL_ENABLED);
717 if (!link)
718 return -ENOMEM;
719 }
Mauro Carvalho Chehabcf975a42015-08-23 07:51:22 -0300720
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300721 if (intf->type == MEDIA_INTF_T_DVB_FE && tuner) {
722 link = media_create_intf_link(tuner, intf,
723 MEDIA_LNK_FL_ENABLED);
724 if (!link)
725 return -ENOMEM;
726 }
Mauro Carvalho Chehab8ed07142015-09-06 13:38:23 -0300727#if 0
728 /*
729 * Indirect link - let's not create yet, as we don't know how
730 * to handle indirect links, nor if this will
731 * actually be needed.
732 */
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300733 if (intf->type == MEDIA_INTF_T_DVB_DVR && demux) {
734 link = media_create_intf_link(demux, intf,
735 MEDIA_LNK_FL_ENABLED);
736 if (!link)
737 return -ENOMEM;
738 }
Mauro Carvalho Chehab8ed07142015-09-06 13:38:23 -0300739#endif
Mauro Carvalho Chehab17813e22015-09-06 13:28:48 -0300740 if (intf->type == MEDIA_INTF_T_DVB_DVR) {
741 ret = dvb_create_io_intf_links(adap, intf, DVR_TSOUT);
742 if (ret)
743 return ret;
744 }
745 if (intf->type == MEDIA_INTF_T_DVB_DEMUX) {
746 ret = dvb_create_io_intf_links(adap, intf, DEMUX_TSOUT);
747 if (ret)
748 return ret;
Mauro Carvalho Chehabdf2f94e2015-08-21 16:18:18 -0300749 }
Mauro Carvalho Chehab8ddb90d2015-08-21 09:32:38 -0300750 }
Mauro Carvalho Chehabf50d5162015-09-04 15:10:29 -0300751 return 0;
Mauro Carvalho Chehab41551092015-01-03 16:52:34 -0300752}
753EXPORT_SYMBOL_GPL(dvb_create_media_graph);
Mauro Carvalho Chehab480884b2015-03-02 10:49:04 -0300754#endif
Mauro Carvalho Chehab41551092015-01-03 16:52:34 -0300755
Janne Grunau78e92002008-04-09 19:13:13 -0300756static int dvbdev_check_free_adapter_num(int num)
757{
758 struct list_head *entry;
759 list_for_each(entry, &dvb_adapter_list) {
760 struct dvb_adapter *adap;
761 adap = list_entry(entry, struct dvb_adapter, list_head);
762 if (adap->num == num)
763 return 0;
764 }
765 return 1;
766}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768static int dvbdev_get_free_adapter_num (void)
769{
770 int num = 0;
771
772 while (num < DVB_MAX_ADAPTERS) {
Janne Grunau78e92002008-04-09 19:13:13 -0300773 if (dvbdev_check_free_adapter_num(num))
774 return num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 num++;
776 }
777
778 return -ENFILE;
779}
780
781
Janne Grunau78e92002008-04-09 19:13:13 -0300782int dvb_register_adapter(struct dvb_adapter *adap, const char *name,
783 struct module *module, struct device *device,
784 short *adapter_nums)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
Janne Grunau78e92002008-04-09 19:13:13 -0300786 int i, num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Simon Arlottc2788502007-03-10 06:21:25 -0300788 mutex_lock(&dvbdev_register_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Janne Grunau78e92002008-04-09 19:13:13 -0300790 for (i = 0; i < DVB_MAX_ADAPTERS; ++i) {
791 num = adapter_nums[i];
792 if (num >= 0 && num < DVB_MAX_ADAPTERS) {
793 /* use the one the driver asked for */
794 if (dvbdev_check_free_adapter_num(num))
795 break;
796 } else {
797 num = dvbdev_get_free_adapter_num();
798 break;
799 }
800 num = -1;
801 }
802
803 if (num < 0) {
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200804 mutex_unlock(&dvbdev_register_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 return -ENFILE;
806 }
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 memset (adap, 0, sizeof(struct dvb_adapter));
809 INIT_LIST_HEAD (&adap->device_list);
810
Simon Arlott900858e2007-05-06 21:06:32 -0300811 printk(KERN_INFO "DVB: registering new adapter (%s)\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 adap->num = num;
814 adap->name = name;
815 adap->module = module;
Andrew de Quinceyd09dbf92006-04-10 09:27:37 -0300816 adap->device = device;
Darron Broad59b18422008-10-11 11:44:05 -0300817 adap->mfe_shared = 0;
818 adap->mfe_dvbdev = NULL;
819 mutex_init (&adap->mfe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 list_add_tail (&adap->list_head, &dvb_adapter_list);
822
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200823 mutex_unlock(&dvbdev_register_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 return num;
826}
827EXPORT_SYMBOL(dvb_register_adapter);
828
829
830int dvb_unregister_adapter(struct dvb_adapter *adap)
831{
Simon Arlottc2788502007-03-10 06:21:25 -0300832 mutex_lock(&dvbdev_register_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 list_del (&adap->list_head);
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200834 mutex_unlock(&dvbdev_register_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return 0;
836}
837EXPORT_SYMBOL(dvb_unregister_adapter);
838
839/* if the miracle happens and "generic_usercopy()" is included into
840 the kernel, then this can vanish. please don't make the mistake and
841 define this as video_usercopy(). this will introduce a dependecy
842 to the v4l "videodev.o" module, which is unnecessary for some
843 cards (ie. the budget dvb-cards don't need the v4l module...) */
Arnd Bergmann16ef8de2010-04-27 00:24:00 +0200844int dvb_usercopy(struct file *file,
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800845 unsigned int cmd, unsigned long arg,
Arnd Bergmann16ef8de2010-04-27 00:24:00 +0200846 int (*func)(struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 unsigned int cmd, void *arg))
848{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800849 char sbuf[128];
850 void *mbuf = NULL;
851 void *parg = NULL;
852 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800854 /* Copy arguments into temp kernel buffer */
855 switch (_IOC_DIR(cmd)) {
856 case _IOC_NONE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 /*
858 * For this command, the pointer is actually an integer
859 * argument.
860 */
861 parg = (void *) arg;
862 break;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800863 case _IOC_READ: /* some v4l ioctls are marked wrong ... */
864 case _IOC_WRITE:
865 case (_IOC_WRITE | _IOC_READ):
866 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
Michael Krufky50c25ff2006-01-09 15:25:34 -0200867 parg = sbuf;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800868 } else {
Michael Krufky50c25ff2006-01-09 15:25:34 -0200869 /* too big to allocate from stack */
870 mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
871 if (NULL == mbuf)
872 return -ENOMEM;
873 parg = mbuf;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800876 err = -EFAULT;
877 if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
Michael Krufky50c25ff2006-01-09 15:25:34 -0200878 goto out;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800879 break;
880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800882 /* call driver */
Arnd Bergmann16ef8de2010-04-27 00:24:00 +0200883 if ((err = func(file, cmd, parg)) == -ENOIOCTLCMD)
Wanlong Gaod9751fd2012-08-27 03:23:14 -0300884 err = -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800886 if (err < 0)
887 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800889 /* Copy results into user buffer */
890 switch (_IOC_DIR(cmd))
891 {
892 case _IOC_READ:
893 case (_IOC_WRITE | _IOC_READ):
894 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
Michael Krufky50c25ff2006-01-09 15:25:34 -0200895 err = -EFAULT;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800896 break;
897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899out:
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800900 kfree(mbuf);
901 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
903
Kay Sieversa5f4c0c2008-10-27 22:27:37 -0300904static int dvb_uevent(struct device *dev, struct kobj_uevent_env *env)
905{
906 struct dvb_device *dvbdev = dev_get_drvdata(dev);
907
Kay Sieversa5f4c0c2008-10-27 22:27:37 -0300908 add_uevent_var(env, "DVB_ADAPTER_NUM=%d", dvbdev->adapter->num);
Kay Sievers763d19b2008-12-31 23:35:24 -0300909 add_uevent_var(env, "DVB_DEVICE_TYPE=%s", dnames[dvbdev->type]);
910 add_uevent_var(env, "DVB_DEVICE_NUM=%d", dvbdev->id);
Kay Sieversa5f4c0c2008-10-27 22:27:37 -0300911 return 0;
912}
913
Al Viro2c9ede52011-07-23 20:24:48 -0400914static char *dvb_devnode(struct device *dev, umode_t *mode)
Kay Sievers8a8bdcc2009-04-30 15:23:42 +0200915{
916 struct dvb_device *dvbdev = dev_get_drvdata(dev);
917
918 return kasprintf(GFP_KERNEL, "dvb/adapter%d/%s%d",
919 dvbdev->adapter->num, dnames[dvbdev->type], dvbdev->id);
920}
921
922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923static int __init init_dvbdev(void)
924{
925 int retval;
926 dev_t dev = MKDEV(DVB_MAJOR, 0);
927
928 if ((retval = register_chrdev_region(dev, MAX_DVB_MINORS, "DVB")) != 0) {
Simon Arlott900858e2007-05-06 21:06:32 -0300929 printk(KERN_ERR "dvb-core: unable to get major %d\n", DVB_MAJOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 return retval;
931 }
932
933 cdev_init(&dvb_device_cdev, &dvb_device_fops);
934 if ((retval = cdev_add(&dvb_device_cdev, dev, MAX_DVB_MINORS)) != 0) {
Simon Arlott900858e2007-05-06 21:06:32 -0300935 printk(KERN_ERR "dvb-core: unable register character device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 goto error;
937 }
938
gregkh@suse.de56b22932005-03-23 10:01:41 -0800939 dvb_class = class_create(THIS_MODULE, "dvb");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 if (IS_ERR(dvb_class)) {
941 retval = PTR_ERR(dvb_class);
942 goto error;
943 }
Kay Sieversa5f4c0c2008-10-27 22:27:37 -0300944 dvb_class->dev_uevent = dvb_uevent;
Kay Sieverse454cea2009-09-18 23:01:12 +0200945 dvb_class->devnode = dvb_devnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 return 0;
947
948error:
949 cdev_del(&dvb_device_cdev);
950 unregister_chrdev_region(dev, MAX_DVB_MINORS);
951 return retval;
952}
953
954
955static void __exit exit_dvbdev(void)
956{
gregkh@suse.de56b22932005-03-23 10:01:41 -0800957 class_destroy(dvb_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 cdev_del(&dvb_device_cdev);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800959 unregister_chrdev_region(MKDEV(DVB_MAJOR, 0), MAX_DVB_MINORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960}
961
Simon Arlott4abdcf92007-05-06 20:56:14 -0300962subsys_initcall(init_dvbdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963module_exit(exit_dvbdev);
964
965MODULE_DESCRIPTION("DVB Core Driver");
966MODULE_AUTHOR("Marcus Metzler, Ralph Metzler, Holger Waechtler");
967MODULE_LICENSE("GPL");