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