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