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 */
19
20 #define pr_fmt(fmt) "dvbdev: " fmt
21
22 #include <linux/types.h>
23 #include <linux/errno.h>
24 #include <linux/string.h>
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/i2c.h>
28 #include <linux/init.h>
29 #include <linux/slab.h>
30 #include <linux/device.h>
31 #include <linux/fs.h>
32 #include <linux/cdev.h>
33 #include <linux/mutex.h>
34 #include <media/dvbdev.h>
35
36 /* Due to enum tuner_pad_index */
37 #include <media/tuner.h>
38
39 static DEFINE_MUTEX(dvbdev_mutex);
40 static LIST_HEAD(dvbdevfops_list);
41 static int dvbdev_debug;
42
43 module_param(dvbdev_debug, int, 0644);
44 MODULE_PARM_DESC(dvbdev_debug, "Turn on/off device debugging (default:off).");
45
46 #define dprintk(fmt, arg...) do { \
47 if (dvbdev_debug) \
48 printk(KERN_DEBUG pr_fmt("%s: " fmt), \
49 __func__, ##arg); \
50 } while (0)
51
52 static LIST_HEAD(dvb_adapter_list);
53 static DEFINE_MUTEX(dvbdev_register_lock);
54
55 static const char * const dnames[] = {
56 [DVB_DEVICE_VIDEO] = "video",
57 [DVB_DEVICE_AUDIO] = "audio",
58 [DVB_DEVICE_SEC] = "sec",
59 [DVB_DEVICE_FRONTEND] = "frontend",
60 [DVB_DEVICE_DEMUX] = "demux",
61 [DVB_DEVICE_DVR] = "dvr",
62 [DVB_DEVICE_CA] = "ca",
63 [DVB_DEVICE_NET] = "net",
64 [DVB_DEVICE_OSD] = "osd"
65 };
66
67 #ifdef CONFIG_DVB_DYNAMIC_MINORS
68 #define MAX_DVB_MINORS 256
69 #define DVB_MAX_IDS MAX_DVB_MINORS
70 #else
71 #define DVB_MAX_IDS 4
72
73 static const u8 minor_type[] = {
74 [DVB_DEVICE_VIDEO] = 0,
75 [DVB_DEVICE_AUDIO] = 1,
76 [DVB_DEVICE_SEC] = 2,
77 [DVB_DEVICE_FRONTEND] = 3,
78 [DVB_DEVICE_DEMUX] = 4,
79 [DVB_DEVICE_DVR] = 5,
80 [DVB_DEVICE_CA] = 6,
81 [DVB_DEVICE_NET] = 7,
82 [DVB_DEVICE_OSD] = 8,
83 };
84
85 #define nums2minor(num, type, id) \
86 (((num) << 6) | ((id) << 4) | minor_type[type])
87
88 #define MAX_DVB_MINORS (DVB_MAX_ADAPTERS*64)
89 #endif
90
91 static struct class *dvb_class;
92
93 static struct dvb_device *dvb_minors[MAX_DVB_MINORS];
94 static DECLARE_RWSEM(minor_rwsem);
95
dvb_device_open(struct inode * inode,struct file * file)96 static int dvb_device_open(struct inode *inode, struct file *file)
97 {
98 struct dvb_device *dvbdev;
99
100 mutex_lock(&dvbdev_mutex);
101 down_read(&minor_rwsem);
102 dvbdev = dvb_minors[iminor(inode)];
103
104 if (dvbdev && dvbdev->fops) {
105 int err = 0;
106 const struct file_operations *new_fops;
107
108 new_fops = fops_get(dvbdev->fops);
109 if (!new_fops)
110 goto fail;
111 file->private_data = dvb_device_get(dvbdev);
112 replace_fops(file, new_fops);
113 if (file->f_op->open)
114 err = file->f_op->open(inode, file);
115 up_read(&minor_rwsem);
116 mutex_unlock(&dvbdev_mutex);
117 if (err)
118 dvb_device_put(dvbdev);
119 return err;
120 }
121 fail:
122 up_read(&minor_rwsem);
123 mutex_unlock(&dvbdev_mutex);
124 return -ENODEV;
125 }
126
127
128 static const struct file_operations dvb_device_fops =
129 {
130 .owner = THIS_MODULE,
131 .open = dvb_device_open,
132 .llseek = noop_llseek,
133 };
134
135 static struct cdev dvb_device_cdev;
136
dvb_generic_open(struct inode * inode,struct file * file)137 int dvb_generic_open(struct inode *inode, struct file *file)
138 {
139 struct dvb_device *dvbdev = file->private_data;
140
141 if (!dvbdev)
142 return -ENODEV;
143
144 if (!dvbdev->users)
145 return -EBUSY;
146
147 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
148 if (!dvbdev->readers)
149 return -EBUSY;
150 dvbdev->readers--;
151 } else {
152 if (!dvbdev->writers)
153 return -EBUSY;
154 dvbdev->writers--;
155 }
156
157 dvbdev->users--;
158 return 0;
159 }
160 EXPORT_SYMBOL(dvb_generic_open);
161
162
dvb_generic_release(struct inode * inode,struct file * file)163 int dvb_generic_release(struct inode *inode, struct file *file)
164 {
165 struct dvb_device *dvbdev = file->private_data;
166
167 if (!dvbdev)
168 return -ENODEV;
169
170 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
171 dvbdev->readers++;
172 } else {
173 dvbdev->writers++;
174 }
175
176 dvbdev->users++;
177
178 dvb_device_put(dvbdev);
179
180 return 0;
181 }
182 EXPORT_SYMBOL(dvb_generic_release);
183
184
dvb_generic_ioctl(struct file * file,unsigned int cmd,unsigned long arg)185 long dvb_generic_ioctl(struct file *file,
186 unsigned int cmd, unsigned long arg)
187 {
188 struct dvb_device *dvbdev = file->private_data;
189
190 if (!dvbdev)
191 return -ENODEV;
192
193 if (!dvbdev->kernel_ioctl)
194 return -EINVAL;
195
196 return dvb_usercopy(file, cmd, arg, dvbdev->kernel_ioctl);
197 }
198 EXPORT_SYMBOL(dvb_generic_ioctl);
199
200
dvbdev_get_free_id(struct dvb_adapter * adap,int type)201 static int dvbdev_get_free_id (struct dvb_adapter *adap, int type)
202 {
203 u32 id = 0;
204
205 while (id < DVB_MAX_IDS) {
206 struct dvb_device *dev;
207 list_for_each_entry(dev, &adap->device_list, list_head)
208 if (dev->type == type && dev->id == id)
209 goto skip;
210 return id;
211 skip:
212 id++;
213 }
214 return -ENFILE;
215 }
216
dvb_media_device_free(struct dvb_device * dvbdev)217 static void dvb_media_device_free(struct dvb_device *dvbdev)
218 {
219 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
220 if (dvbdev->entity) {
221 media_device_unregister_entity(dvbdev->entity);
222 kfree(dvbdev->entity);
223 kfree(dvbdev->pads);
224 dvbdev->entity = NULL;
225 dvbdev->pads = NULL;
226 }
227
228 if (dvbdev->tsout_entity) {
229 int i;
230
231 for (i = 0; i < dvbdev->tsout_num_entities; i++) {
232 media_device_unregister_entity(&dvbdev->tsout_entity[i]);
233 kfree(dvbdev->tsout_entity[i].name);
234 }
235 kfree(dvbdev->tsout_entity);
236 kfree(dvbdev->tsout_pads);
237 dvbdev->tsout_entity = NULL;
238 dvbdev->tsout_pads = NULL;
239
240 dvbdev->tsout_num_entities = 0;
241 }
242
243 if (dvbdev->intf_devnode) {
244 media_devnode_remove(dvbdev->intf_devnode);
245 dvbdev->intf_devnode = NULL;
246 }
247
248 if (dvbdev->adapter->conn) {
249 media_device_unregister_entity(dvbdev->adapter->conn);
250 kfree(dvbdev->adapter->conn);
251 dvbdev->adapter->conn = NULL;
252 kfree(dvbdev->adapter->conn_pads);
253 dvbdev->adapter->conn_pads = NULL;
254 }
255 #endif
256 }
257
258 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
dvb_create_tsout_entity(struct dvb_device * dvbdev,const char * name,int npads)259 static int dvb_create_tsout_entity(struct dvb_device *dvbdev,
260 const char *name, int npads)
261 {
262 int i, ret = 0;
263
264 dvbdev->tsout_pads = kcalloc(npads, sizeof(*dvbdev->tsout_pads),
265 GFP_KERNEL);
266 if (!dvbdev->tsout_pads)
267 return -ENOMEM;
268
269 dvbdev->tsout_entity = kcalloc(npads, sizeof(*dvbdev->tsout_entity),
270 GFP_KERNEL);
271 if (!dvbdev->tsout_entity)
272 return -ENOMEM;
273
274 dvbdev->tsout_num_entities = npads;
275
276 for (i = 0; i < npads; i++) {
277 struct media_pad *pads = &dvbdev->tsout_pads[i];
278 struct media_entity *entity = &dvbdev->tsout_entity[i];
279
280 entity->name = kasprintf(GFP_KERNEL, "%s #%d", name, i);
281 if (!entity->name)
282 return -ENOMEM;
283
284 entity->function = MEDIA_ENT_F_IO_DTV;
285 pads->flags = MEDIA_PAD_FL_SINK;
286
287 ret = media_entity_pads_init(entity, 1, pads);
288 if (ret < 0)
289 return ret;
290
291 ret = media_device_register_entity(dvbdev->adapter->mdev,
292 entity);
293 if (ret < 0)
294 return ret;
295 }
296 return 0;
297 }
298
299 #define DEMUX_TSOUT "demux-tsout"
300 #define DVR_TSOUT "dvr-tsout"
301
dvb_create_media_entity(struct dvb_device * dvbdev,int type,int demux_sink_pads)302 static int dvb_create_media_entity(struct dvb_device *dvbdev,
303 int type, int demux_sink_pads)
304 {
305 int i, ret, npads;
306
307 switch (type) {
308 case DVB_DEVICE_FRONTEND:
309 npads = 2;
310 break;
311 case DVB_DEVICE_DVR:
312 ret = dvb_create_tsout_entity(dvbdev, DVR_TSOUT,
313 demux_sink_pads);
314 return ret;
315 case DVB_DEVICE_DEMUX:
316 npads = 1 + demux_sink_pads;
317 ret = dvb_create_tsout_entity(dvbdev, DEMUX_TSOUT,
318 demux_sink_pads);
319 if (ret < 0)
320 return ret;
321 break;
322 case DVB_DEVICE_CA:
323 npads = 2;
324 break;
325 case DVB_DEVICE_NET:
326 /*
327 * We should be creating entities for the MPE/ULE
328 * decapsulation hardware (or software implementation).
329 *
330 * However, the number of for the MPE/ULE decaps may not be
331 * fixed. As we don't have yet dynamic support for PADs at
332 * the Media Controller, let's not create the decap
333 * entities yet.
334 */
335 return 0;
336 default:
337 return 0;
338 }
339
340 dvbdev->entity = kzalloc(sizeof(*dvbdev->entity), GFP_KERNEL);
341 if (!dvbdev->entity)
342 return -ENOMEM;
343
344 dvbdev->entity->name = dvbdev->name;
345
346 if (npads) {
347 dvbdev->pads = kcalloc(npads, sizeof(*dvbdev->pads),
348 GFP_KERNEL);
349 if (!dvbdev->pads) {
350 kfree(dvbdev->entity);
351 dvbdev->entity = NULL;
352 return -ENOMEM;
353 }
354 }
355
356 switch (type) {
357 case DVB_DEVICE_FRONTEND:
358 dvbdev->entity->function = MEDIA_ENT_F_DTV_DEMOD;
359 dvbdev->pads[0].flags = MEDIA_PAD_FL_SINK;
360 dvbdev->pads[1].flags = MEDIA_PAD_FL_SOURCE;
361 break;
362 case DVB_DEVICE_DEMUX:
363 dvbdev->entity->function = MEDIA_ENT_F_TS_DEMUX;
364 dvbdev->pads[0].flags = MEDIA_PAD_FL_SINK;
365 for (i = 1; i < npads; i++)
366 dvbdev->pads[i].flags = MEDIA_PAD_FL_SOURCE;
367 break;
368 case DVB_DEVICE_CA:
369 dvbdev->entity->function = MEDIA_ENT_F_DTV_CA;
370 dvbdev->pads[0].flags = MEDIA_PAD_FL_SINK;
371 dvbdev->pads[1].flags = MEDIA_PAD_FL_SOURCE;
372 break;
373 default:
374 /* Should never happen, as the first switch prevents it */
375 kfree(dvbdev->entity);
376 kfree(dvbdev->pads);
377 dvbdev->entity = NULL;
378 dvbdev->pads = NULL;
379 return 0;
380 }
381
382 if (npads) {
383 ret = media_entity_pads_init(dvbdev->entity, npads, dvbdev->pads);
384 if (ret)
385 return ret;
386 }
387 ret = media_device_register_entity(dvbdev->adapter->mdev,
388 dvbdev->entity);
389 if (ret)
390 return ret;
391
392 pr_info("%s: media entity '%s' registered.\n",
393 __func__, dvbdev->entity->name);
394
395 return 0;
396 }
397 #endif
398
dvb_register_media_device(struct dvb_device * dvbdev,int type,int minor,unsigned demux_sink_pads)399 static int dvb_register_media_device(struct dvb_device *dvbdev,
400 int type, int minor,
401 unsigned demux_sink_pads)
402 {
403 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
404 struct media_link *link;
405 u32 intf_type;
406 int ret;
407
408 if (!dvbdev->adapter->mdev)
409 return 0;
410
411 ret = dvb_create_media_entity(dvbdev, type, demux_sink_pads);
412 if (ret)
413 return ret;
414
415 switch (type) {
416 case DVB_DEVICE_FRONTEND:
417 intf_type = MEDIA_INTF_T_DVB_FE;
418 break;
419 case DVB_DEVICE_DEMUX:
420 intf_type = MEDIA_INTF_T_DVB_DEMUX;
421 break;
422 case DVB_DEVICE_DVR:
423 intf_type = MEDIA_INTF_T_DVB_DVR;
424 break;
425 case DVB_DEVICE_CA:
426 intf_type = MEDIA_INTF_T_DVB_CA;
427 break;
428 case DVB_DEVICE_NET:
429 intf_type = MEDIA_INTF_T_DVB_NET;
430 break;
431 default:
432 return 0;
433 }
434
435 dvbdev->intf_devnode = media_devnode_create(dvbdev->adapter->mdev,
436 intf_type, 0,
437 DVB_MAJOR, minor);
438
439 if (!dvbdev->intf_devnode)
440 return -ENOMEM;
441
442 /*
443 * Create the "obvious" link, e. g. the ones that represent
444 * a direct association between an interface and an entity.
445 * Other links should be created elsewhere, like:
446 * DVB FE intf -> tuner
447 * DVB demux intf -> dvr
448 */
449
450 if (!dvbdev->entity)
451 return 0;
452
453 link = media_create_intf_link(dvbdev->entity,
454 &dvbdev->intf_devnode->intf,
455 MEDIA_LNK_FL_ENABLED |
456 MEDIA_LNK_FL_IMMUTABLE);
457 if (!link)
458 return -ENOMEM;
459 #endif
460 return 0;
461 }
462
dvb_register_device(struct dvb_adapter * adap,struct dvb_device ** pdvbdev,const struct dvb_device * template,void * priv,enum dvb_device_type type,int demux_sink_pads)463 int dvb_register_device(struct dvb_adapter *adap, struct dvb_device **pdvbdev,
464 const struct dvb_device *template, void *priv,
465 enum dvb_device_type type, int demux_sink_pads)
466 {
467 struct dvb_device *dvbdev;
468 struct file_operations *dvbdevfops = NULL;
469 struct dvbdevfops_node *node = NULL, *new_node = NULL;
470 struct device *clsdev;
471 int minor;
472 int id, ret;
473
474 mutex_lock(&dvbdev_register_lock);
475
476 if ((id = dvbdev_get_free_id (adap, type)) < 0) {
477 mutex_unlock(&dvbdev_register_lock);
478 *pdvbdev = NULL;
479 pr_err("%s: couldn't find free device id\n", __func__);
480 return -ENFILE;
481 }
482
483 *pdvbdev = dvbdev = kzalloc(sizeof(*dvbdev), GFP_KERNEL);
484 if (!dvbdev){
485 mutex_unlock(&dvbdev_register_lock);
486 return -ENOMEM;
487 }
488
489 /*
490 * When a device of the same type is probe()d more than once,
491 * the first allocated fops are used. This prevents memory leaks
492 * that can occur when the same device is probe()d repeatedly.
493 */
494 list_for_each_entry(node, &dvbdevfops_list, list_head) {
495 if (node->fops->owner == adap->module &&
496 node->type == type &&
497 node->template == template) {
498 dvbdevfops = node->fops;
499 break;
500 }
501 }
502
503 if (dvbdevfops == NULL) {
504 dvbdevfops = kmemdup(template->fops, sizeof(*dvbdevfops), GFP_KERNEL);
505 if (!dvbdevfops) {
506 kfree(dvbdev);
507 mutex_unlock(&dvbdev_register_lock);
508 return -ENOMEM;
509 }
510
511 new_node = kzalloc(sizeof(struct dvbdevfops_node), GFP_KERNEL);
512 if (!new_node) {
513 kfree(dvbdevfops);
514 kfree(dvbdev);
515 mutex_unlock(&dvbdev_register_lock);
516 return -ENOMEM;
517 }
518
519 new_node->fops = dvbdevfops;
520 new_node->type = type;
521 new_node->template = template;
522 list_add_tail (&new_node->list_head, &dvbdevfops_list);
523 }
524
525 memcpy(dvbdev, template, sizeof(struct dvb_device));
526 kref_init(&dvbdev->ref);
527 dvbdev->type = type;
528 dvbdev->id = id;
529 dvbdev->adapter = adap;
530 dvbdev->priv = priv;
531 dvbdev->fops = dvbdevfops;
532 init_waitqueue_head (&dvbdev->wait_queue);
533 dvbdevfops->owner = adap->module;
534 list_add_tail (&dvbdev->list_head, &adap->device_list);
535 down_write(&minor_rwsem);
536 #ifdef CONFIG_DVB_DYNAMIC_MINORS
537 for (minor = 0; minor < MAX_DVB_MINORS; minor++)
538 if (dvb_minors[minor] == NULL)
539 break;
540 if (minor == MAX_DVB_MINORS) {
541 if (new_node) {
542 list_del (&new_node->list_head);
543 kfree(dvbdevfops);
544 kfree(new_node);
545 }
546 list_del (&dvbdev->list_head);
547 kfree(dvbdev);
548 up_write(&minor_rwsem);
549 mutex_unlock(&dvbdev_register_lock);
550 return -EINVAL;
551 }
552 #else
553 minor = nums2minor(adap->num, type, id);
554 #endif
555 dvbdev->minor = minor;
556 dvb_minors[minor] = dvb_device_get(dvbdev);
557 up_write(&minor_rwsem);
558 ret = dvb_register_media_device(dvbdev, type, minor, demux_sink_pads);
559 if (ret) {
560 pr_err("%s: dvb_register_media_device failed to create the mediagraph\n",
561 __func__);
562 if (new_node) {
563 list_del (&new_node->list_head);
564 kfree(dvbdevfops);
565 kfree(new_node);
566 }
567 dvb_media_device_free(dvbdev);
568 list_del (&dvbdev->list_head);
569 kfree(dvbdev);
570 mutex_unlock(&dvbdev_register_lock);
571 return ret;
572 }
573
574 clsdev = device_create(dvb_class, adap->device,
575 MKDEV(DVB_MAJOR, minor),
576 dvbdev, "dvb%d.%s%d", adap->num, dnames[type], id);
577 if (IS_ERR(clsdev)) {
578 pr_err("%s: failed to create device dvb%d.%s%d (%ld)\n",
579 __func__, adap->num, dnames[type], id, PTR_ERR(clsdev));
580 if (new_node) {
581 list_del (&new_node->list_head);
582 kfree(dvbdevfops);
583 kfree(new_node);
584 }
585 dvb_media_device_free(dvbdev);
586 list_del (&dvbdev->list_head);
587 kfree(dvbdev);
588 mutex_unlock(&dvbdev_register_lock);
589 return PTR_ERR(clsdev);
590 }
591
592 dprintk("DVB: register adapter%d/%s%d @ minor: %i (0x%02x)\n",
593 adap->num, dnames[type], id, minor, minor);
594
595 mutex_unlock(&dvbdev_register_lock);
596 return 0;
597 }
598 EXPORT_SYMBOL(dvb_register_device);
599
600
dvb_remove_device(struct dvb_device * dvbdev)601 void dvb_remove_device(struct dvb_device *dvbdev)
602 {
603 if (!dvbdev)
604 return;
605
606 down_write(&minor_rwsem);
607 dvb_minors[dvbdev->minor] = NULL;
608 dvb_device_put(dvbdev);
609 up_write(&minor_rwsem);
610
611 dvb_media_device_free(dvbdev);
612
613 device_destroy(dvb_class, MKDEV(DVB_MAJOR, dvbdev->minor));
614
615 list_del (&dvbdev->list_head);
616 }
617 EXPORT_SYMBOL(dvb_remove_device);
618
619
dvb_free_device(struct kref * ref)620 static void dvb_free_device(struct kref *ref)
621 {
622 struct dvb_device *dvbdev = container_of(ref, struct dvb_device, ref);
623
624 kfree (dvbdev);
625 }
626
627
dvb_device_get(struct dvb_device * dvbdev)628 struct dvb_device *dvb_device_get(struct dvb_device *dvbdev)
629 {
630 kref_get(&dvbdev->ref);
631 return dvbdev;
632 }
633 EXPORT_SYMBOL(dvb_device_get);
634
635
dvb_device_put(struct dvb_device * dvbdev)636 void dvb_device_put(struct dvb_device *dvbdev)
637 {
638 if (dvbdev)
639 kref_put(&dvbdev->ref, dvb_free_device);
640 }
641
642
dvb_unregister_device(struct dvb_device * dvbdev)643 void dvb_unregister_device(struct dvb_device *dvbdev)
644 {
645 dvb_remove_device(dvbdev);
646 dvb_device_put(dvbdev);
647 }
648 EXPORT_SYMBOL(dvb_unregister_device);
649
650
651 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
652
dvb_create_io_intf_links(struct dvb_adapter * adap,struct media_interface * intf,char * name)653 static int dvb_create_io_intf_links(struct dvb_adapter *adap,
654 struct media_interface *intf,
655 char *name)
656 {
657 struct media_device *mdev = adap->mdev;
658 struct media_entity *entity;
659 struct media_link *link;
660
661 media_device_for_each_entity(entity, mdev) {
662 if (entity->function == MEDIA_ENT_F_IO_DTV) {
663 if (strncmp(entity->name, name, strlen(name)))
664 continue;
665 link = media_create_intf_link(entity, intf,
666 MEDIA_LNK_FL_ENABLED |
667 MEDIA_LNK_FL_IMMUTABLE);
668 if (!link)
669 return -ENOMEM;
670 }
671 }
672 return 0;
673 }
674
dvb_create_media_graph(struct dvb_adapter * adap,bool create_rf_connector)675 int dvb_create_media_graph(struct dvb_adapter *adap,
676 bool create_rf_connector)
677 {
678 struct media_device *mdev = adap->mdev;
679 struct media_entity *entity, *tuner = NULL, *demod = NULL, *conn;
680 struct media_entity *demux = NULL, *ca = NULL;
681 struct media_link *link;
682 struct media_interface *intf;
683 unsigned demux_pad = 0;
684 unsigned dvr_pad = 0;
685 unsigned ntuner = 0, ndemod = 0;
686 int ret, pad_source, pad_sink;
687 static const char *connector_name = "Television";
688
689 if (!mdev)
690 return 0;
691
692 media_device_for_each_entity(entity, mdev) {
693 switch (entity->function) {
694 case MEDIA_ENT_F_TUNER:
695 tuner = entity;
696 ntuner++;
697 break;
698 case MEDIA_ENT_F_DTV_DEMOD:
699 demod = entity;
700 ndemod++;
701 break;
702 case MEDIA_ENT_F_TS_DEMUX:
703 demux = entity;
704 break;
705 case MEDIA_ENT_F_DTV_CA:
706 ca = entity;
707 break;
708 }
709 }
710
711 /*
712 * Prepare to signalize to media_create_pad_links() that multiple
713 * entities of the same type exists and a 1:n or n:1 links need to be
714 * created.
715 * NOTE: if both tuner and demod have multiple instances, it is up
716 * to the caller driver to create such links.
717 */
718 if (ntuner > 1)
719 tuner = NULL;
720 if (ndemod > 1)
721 demod = NULL;
722
723 if (create_rf_connector) {
724 conn = kzalloc(sizeof(*conn), GFP_KERNEL);
725 if (!conn)
726 return -ENOMEM;
727 adap->conn = conn;
728
729 adap->conn_pads = kzalloc(sizeof(*adap->conn_pads), GFP_KERNEL);
730 if (!adap->conn_pads)
731 return -ENOMEM;
732
733 conn->flags = MEDIA_ENT_FL_CONNECTOR;
734 conn->function = MEDIA_ENT_F_CONN_RF;
735 conn->name = connector_name;
736 adap->conn_pads->flags = MEDIA_PAD_FL_SOURCE;
737
738 ret = media_entity_pads_init(conn, 1, adap->conn_pads);
739 if (ret)
740 return ret;
741
742 ret = media_device_register_entity(mdev, conn);
743 if (ret)
744 return ret;
745
746 if (!ntuner) {
747 ret = media_create_pad_links(mdev,
748 MEDIA_ENT_F_CONN_RF,
749 conn, 0,
750 MEDIA_ENT_F_DTV_DEMOD,
751 demod, 0,
752 MEDIA_LNK_FL_ENABLED,
753 false);
754 } else {
755 pad_sink = media_get_pad_index(tuner, true,
756 PAD_SIGNAL_ANALOG);
757 if (pad_sink < 0)
758 return -EINVAL;
759 ret = media_create_pad_links(mdev,
760 MEDIA_ENT_F_CONN_RF,
761 conn, 0,
762 MEDIA_ENT_F_TUNER,
763 tuner, pad_sink,
764 MEDIA_LNK_FL_ENABLED,
765 false);
766 }
767 if (ret)
768 return ret;
769 }
770
771 if (ntuner && ndemod) {
772 /* NOTE: first found tuner source pad presumed correct */
773 pad_source = media_get_pad_index(tuner, false,
774 PAD_SIGNAL_ANALOG);
775 if (pad_source < 0)
776 return -EINVAL;
777 ret = media_create_pad_links(mdev,
778 MEDIA_ENT_F_TUNER,
779 tuner, pad_source,
780 MEDIA_ENT_F_DTV_DEMOD,
781 demod, 0, MEDIA_LNK_FL_ENABLED,
782 false);
783 if (ret)
784 return ret;
785 }
786
787 if (ndemod && demux) {
788 ret = media_create_pad_links(mdev,
789 MEDIA_ENT_F_DTV_DEMOD,
790 demod, 1,
791 MEDIA_ENT_F_TS_DEMUX,
792 demux, 0, MEDIA_LNK_FL_ENABLED,
793 false);
794 if (ret)
795 return ret;
796 }
797 if (demux && ca) {
798 ret = media_create_pad_link(demux, 1, ca,
799 0, MEDIA_LNK_FL_ENABLED);
800 if (ret)
801 return ret;
802 }
803
804 /* Create demux links for each ringbuffer/pad */
805 if (demux) {
806 media_device_for_each_entity(entity, mdev) {
807 if (entity->function == MEDIA_ENT_F_IO_DTV) {
808 if (!strncmp(entity->name, DVR_TSOUT,
809 strlen(DVR_TSOUT))) {
810 ret = media_create_pad_link(demux,
811 ++dvr_pad,
812 entity, 0, 0);
813 if (ret)
814 return ret;
815 }
816 if (!strncmp(entity->name, DEMUX_TSOUT,
817 strlen(DEMUX_TSOUT))) {
818 ret = media_create_pad_link(demux,
819 ++demux_pad,
820 entity, 0, 0);
821 if (ret)
822 return ret;
823 }
824 }
825 }
826 }
827
828 /* Create interface links for FE->tuner, DVR->demux and CA->ca */
829 media_device_for_each_intf(intf, mdev) {
830 if (intf->type == MEDIA_INTF_T_DVB_CA && ca) {
831 link = media_create_intf_link(ca, intf,
832 MEDIA_LNK_FL_ENABLED |
833 MEDIA_LNK_FL_IMMUTABLE);
834 if (!link)
835 return -ENOMEM;
836 }
837
838 if (intf->type == MEDIA_INTF_T_DVB_FE && tuner) {
839 link = media_create_intf_link(tuner, intf,
840 MEDIA_LNK_FL_ENABLED |
841 MEDIA_LNK_FL_IMMUTABLE);
842 if (!link)
843 return -ENOMEM;
844 }
845 #if 0
846 /*
847 * Indirect link - let's not create yet, as we don't know how
848 * to handle indirect links, nor if this will
849 * actually be needed.
850 */
851 if (intf->type == MEDIA_INTF_T_DVB_DVR && demux) {
852 link = media_create_intf_link(demux, intf,
853 MEDIA_LNK_FL_ENABLED |
854 MEDIA_LNK_FL_IMMUTABLE);
855 if (!link)
856 return -ENOMEM;
857 }
858 #endif
859 if (intf->type == MEDIA_INTF_T_DVB_DVR) {
860 ret = dvb_create_io_intf_links(adap, intf, DVR_TSOUT);
861 if (ret)
862 return ret;
863 }
864 if (intf->type == MEDIA_INTF_T_DVB_DEMUX) {
865 ret = dvb_create_io_intf_links(adap, intf, DEMUX_TSOUT);
866 if (ret)
867 return ret;
868 }
869 }
870 return 0;
871 }
872 EXPORT_SYMBOL_GPL(dvb_create_media_graph);
873 #endif
874
dvbdev_check_free_adapter_num(int num)875 static int dvbdev_check_free_adapter_num(int num)
876 {
877 struct list_head *entry;
878 list_for_each(entry, &dvb_adapter_list) {
879 struct dvb_adapter *adap;
880 adap = list_entry(entry, struct dvb_adapter, list_head);
881 if (adap->num == num)
882 return 0;
883 }
884 return 1;
885 }
886
dvbdev_get_free_adapter_num(void)887 static int dvbdev_get_free_adapter_num (void)
888 {
889 int num = 0;
890
891 while (num < DVB_MAX_ADAPTERS) {
892 if (dvbdev_check_free_adapter_num(num))
893 return num;
894 num++;
895 }
896
897 return -ENFILE;
898 }
899
900
dvb_register_adapter(struct dvb_adapter * adap,const char * name,struct module * module,struct device * device,short * adapter_nums)901 int dvb_register_adapter(struct dvb_adapter *adap, const char *name,
902 struct module *module, struct device *device,
903 short *adapter_nums)
904 {
905 int i, num;
906
907 mutex_lock(&dvbdev_register_lock);
908
909 for (i = 0; i < DVB_MAX_ADAPTERS; ++i) {
910 num = adapter_nums[i];
911 if (num >= 0 && num < DVB_MAX_ADAPTERS) {
912 /* use the one the driver asked for */
913 if (dvbdev_check_free_adapter_num(num))
914 break;
915 } else {
916 num = dvbdev_get_free_adapter_num();
917 break;
918 }
919 num = -1;
920 }
921
922 if (num < 0) {
923 mutex_unlock(&dvbdev_register_lock);
924 return -ENFILE;
925 }
926
927 memset (adap, 0, sizeof(struct dvb_adapter));
928 INIT_LIST_HEAD (&adap->device_list);
929
930 pr_info("DVB: registering new adapter (%s)\n", name);
931
932 adap->num = num;
933 adap->name = name;
934 adap->module = module;
935 adap->device = device;
936 adap->mfe_shared = 0;
937 adap->mfe_dvbdev = NULL;
938 mutex_init (&adap->mfe_lock);
939
940 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
941 mutex_init(&adap->mdev_lock);
942 #endif
943
944 list_add_tail (&adap->list_head, &dvb_adapter_list);
945
946 mutex_unlock(&dvbdev_register_lock);
947
948 return num;
949 }
950 EXPORT_SYMBOL(dvb_register_adapter);
951
952
dvb_unregister_adapter(struct dvb_adapter * adap)953 int dvb_unregister_adapter(struct dvb_adapter *adap)
954 {
955 mutex_lock(&dvbdev_register_lock);
956 list_del (&adap->list_head);
957 mutex_unlock(&dvbdev_register_lock);
958 return 0;
959 }
960 EXPORT_SYMBOL(dvb_unregister_adapter);
961
962 /* if the miracle happens and "generic_usercopy()" is included into
963 the kernel, then this can vanish. please don't make the mistake and
964 define this as video_usercopy(). this will introduce a dependency
965 to the v4l "videodev.o" module, which is unnecessary for some
966 cards (ie. the budget dvb-cards don't need the v4l module...) */
dvb_usercopy(struct file * file,unsigned int cmd,unsigned long arg,int (* func)(struct file * file,unsigned int cmd,void * arg))967 int dvb_usercopy(struct file *file,
968 unsigned int cmd, unsigned long arg,
969 int (*func)(struct file *file,
970 unsigned int cmd, void *arg))
971 {
972 char sbuf[128];
973 void *mbuf = NULL;
974 void *parg = NULL;
975 int err = -EINVAL;
976
977 /* Copy arguments into temp kernel buffer */
978 switch (_IOC_DIR(cmd)) {
979 case _IOC_NONE:
980 /*
981 * For this command, the pointer is actually an integer
982 * argument.
983 */
984 parg = (void *) arg;
985 break;
986 case _IOC_READ: /* some v4l ioctls are marked wrong ... */
987 case _IOC_WRITE:
988 case (_IOC_WRITE | _IOC_READ):
989 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
990 parg = sbuf;
991 } else {
992 /* too big to allocate from stack */
993 mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
994 if (NULL == mbuf)
995 return -ENOMEM;
996 parg = mbuf;
997 }
998
999 err = -EFAULT;
1000 if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
1001 goto out;
1002 break;
1003 }
1004
1005 /* call driver */
1006 if ((err = func(file, cmd, parg)) == -ENOIOCTLCMD)
1007 err = -ENOTTY;
1008
1009 if (err < 0)
1010 goto out;
1011
1012 /* Copy results into user buffer */
1013 switch (_IOC_DIR(cmd))
1014 {
1015 case _IOC_READ:
1016 case (_IOC_WRITE | _IOC_READ):
1017 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
1018 err = -EFAULT;
1019 break;
1020 }
1021
1022 out:
1023 kfree(mbuf);
1024 return err;
1025 }
1026
1027 #if IS_ENABLED(CONFIG_I2C)
dvb_module_probe(const char * module_name,const char * name,struct i2c_adapter * adap,unsigned char addr,void * platform_data)1028 struct i2c_client *dvb_module_probe(const char *module_name,
1029 const char *name,
1030 struct i2c_adapter *adap,
1031 unsigned char addr,
1032 void *platform_data)
1033 {
1034 struct i2c_client *client;
1035 struct i2c_board_info *board_info;
1036
1037 board_info = kzalloc(sizeof(*board_info), GFP_KERNEL);
1038 if (!board_info)
1039 return NULL;
1040
1041 if (name)
1042 strscpy(board_info->type, name, I2C_NAME_SIZE);
1043 else
1044 strscpy(board_info->type, module_name, I2C_NAME_SIZE);
1045
1046 board_info->addr = addr;
1047 board_info->platform_data = platform_data;
1048 request_module(module_name);
1049 client = i2c_new_client_device(adap, board_info);
1050 if (!i2c_client_has_driver(client)) {
1051 kfree(board_info);
1052 return NULL;
1053 }
1054
1055 if (!try_module_get(client->dev.driver->owner)) {
1056 i2c_unregister_device(client);
1057 client = NULL;
1058 }
1059
1060 kfree(board_info);
1061 return client;
1062 }
1063 EXPORT_SYMBOL_GPL(dvb_module_probe);
1064
dvb_module_release(struct i2c_client * client)1065 void dvb_module_release(struct i2c_client *client)
1066 {
1067 if (!client)
1068 return;
1069
1070 module_put(client->dev.driver->owner);
1071 i2c_unregister_device(client);
1072 }
1073 EXPORT_SYMBOL_GPL(dvb_module_release);
1074 #endif
1075
dvb_uevent(struct device * dev,struct kobj_uevent_env * env)1076 static int dvb_uevent(struct device *dev, struct kobj_uevent_env *env)
1077 {
1078 struct dvb_device *dvbdev = dev_get_drvdata(dev);
1079
1080 add_uevent_var(env, "DVB_ADAPTER_NUM=%d", dvbdev->adapter->num);
1081 add_uevent_var(env, "DVB_DEVICE_TYPE=%s", dnames[dvbdev->type]);
1082 add_uevent_var(env, "DVB_DEVICE_NUM=%d", dvbdev->id);
1083 return 0;
1084 }
1085
dvb_devnode(struct device * dev,umode_t * mode)1086 static char *dvb_devnode(struct device *dev, umode_t *mode)
1087 {
1088 struct dvb_device *dvbdev = dev_get_drvdata(dev);
1089
1090 return kasprintf(GFP_KERNEL, "dvb/adapter%d/%s%d",
1091 dvbdev->adapter->num, dnames[dvbdev->type], dvbdev->id);
1092 }
1093
1094
init_dvbdev(void)1095 static int __init init_dvbdev(void)
1096 {
1097 int retval;
1098 dev_t dev = MKDEV(DVB_MAJOR, 0);
1099
1100 if ((retval = register_chrdev_region(dev, MAX_DVB_MINORS, "DVB")) != 0) {
1101 pr_err("dvb-core: unable to get major %d\n", DVB_MAJOR);
1102 return retval;
1103 }
1104
1105 cdev_init(&dvb_device_cdev, &dvb_device_fops);
1106 if ((retval = cdev_add(&dvb_device_cdev, dev, MAX_DVB_MINORS)) != 0) {
1107 pr_err("dvb-core: unable register character device\n");
1108 goto error;
1109 }
1110
1111 dvb_class = class_create(THIS_MODULE, "dvb");
1112 if (IS_ERR(dvb_class)) {
1113 retval = PTR_ERR(dvb_class);
1114 goto error;
1115 }
1116 dvb_class->dev_uevent = dvb_uevent;
1117 dvb_class->devnode = dvb_devnode;
1118 return 0;
1119
1120 error:
1121 cdev_del(&dvb_device_cdev);
1122 unregister_chrdev_region(dev, MAX_DVB_MINORS);
1123 return retval;
1124 }
1125
1126
exit_dvbdev(void)1127 static void __exit exit_dvbdev(void)
1128 {
1129 struct dvbdevfops_node *node, *next;
1130
1131 class_destroy(dvb_class);
1132 cdev_del(&dvb_device_cdev);
1133 unregister_chrdev_region(MKDEV(DVB_MAJOR, 0), MAX_DVB_MINORS);
1134
1135 list_for_each_entry_safe(node, next, &dvbdevfops_list, list_head) {
1136 list_del (&node->list_head);
1137 kfree(node->fops);
1138 kfree(node);
1139 }
1140 }
1141
1142 subsys_initcall(init_dvbdev);
1143 module_exit(exit_dvbdev);
1144
1145 MODULE_DESCRIPTION("DVB Core Driver");
1146 MODULE_AUTHOR("Marcus Metzler, Ralph Metzler, Holger Waechtler");
1147 MODULE_LICENSE("GPL");
1148