1 // SPDX-License-Identifier: LGPL-2.1-or-later
7 * for convergence integrated media GmbH
10 #define pr_fmt(fmt) "dvbdev: " fmt
12 #include <linux/types.h>
13 #include <linux/errno.h>
14 #include <linux/string.h>
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/i2c.h>
18 #include <linux/init.h>
19 #include <linux/slab.h>
20 #include <linux/device.h>
22 #include <linux/cdev.h>
23 #include <linux/mutex.h>
24 #include <media/dvbdev.h>
26 /* Due to enum tuner_pad_index */
27 #include <media/tuner.h>
29 static DEFINE_MUTEX(dvbdev_mutex);
30 static LIST_HEAD(dvbdevfops_list);
31 static int dvbdev_debug;
33 module_param(dvbdev_debug, int, 0644);
34 MODULE_PARM_DESC(dvbdev_debug, "Turn on/off device debugging (default:off).");
36 #define dprintk(fmt, arg...) do { \
38 printk(KERN_DEBUG pr_fmt("%s: " fmt), \
42 static LIST_HEAD(dvb_adapter_list);
43 static DEFINE_MUTEX(dvbdev_register_lock);
45 static const char * const dnames[] = {
46 [DVB_DEVICE_VIDEO] = "video",
47 [DVB_DEVICE_AUDIO] = "audio",
48 [DVB_DEVICE_SEC] = "sec",
49 [DVB_DEVICE_FRONTEND] = "frontend",
50 [DVB_DEVICE_DEMUX] = "demux",
51 [DVB_DEVICE_DVR] = "dvr",
52 [DVB_DEVICE_CA] = "ca",
53 [DVB_DEVICE_NET] = "net",
54 [DVB_DEVICE_OSD] = "osd"
57 #ifdef CONFIG_DVB_DYNAMIC_MINORS
58 #define MAX_DVB_MINORS 256
59 #define DVB_MAX_IDS MAX_DVB_MINORS
63 static const u8 minor_type[] = {
64 [DVB_DEVICE_VIDEO] = 0,
65 [DVB_DEVICE_AUDIO] = 1,
67 [DVB_DEVICE_FRONTEND] = 3,
68 [DVB_DEVICE_DEMUX] = 4,
75 #define nums2minor(num, type, id) \
76 (((num) << 6) | ((id) << 4) | minor_type[type])
78 #define MAX_DVB_MINORS (DVB_MAX_ADAPTERS * 64)
81 static struct class *dvb_class;
83 static struct dvb_device *dvb_minors[MAX_DVB_MINORS];
84 static DECLARE_RWSEM(minor_rwsem);
86 static int dvb_device_open(struct inode *inode, struct file *file)
88 struct dvb_device *dvbdev;
90 mutex_lock(&dvbdev_mutex);
91 down_read(&minor_rwsem);
92 dvbdev = dvb_minors[iminor(inode)];
94 if (dvbdev && dvbdev->fops) {
96 const struct file_operations *new_fops;
98 new_fops = fops_get(dvbdev->fops);
101 file->private_data = dvb_device_get(dvbdev);
102 replace_fops(file, new_fops);
103 if (file->f_op->open)
104 err = file->f_op->open(inode, file);
105 up_read(&minor_rwsem);
106 mutex_unlock(&dvbdev_mutex);
110 up_read(&minor_rwsem);
111 mutex_unlock(&dvbdev_mutex);
115 static const struct file_operations dvb_device_fops = {
116 .owner = THIS_MODULE,
117 .open = dvb_device_open,
118 .llseek = noop_llseek,
121 static struct cdev dvb_device_cdev;
123 int dvb_generic_open(struct inode *inode, struct file *file)
125 struct dvb_device *dvbdev = file->private_data;
133 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
134 if (!dvbdev->readers)
138 if (!dvbdev->writers)
146 EXPORT_SYMBOL(dvb_generic_open);
148 int dvb_generic_release(struct inode *inode, struct file *file)
150 struct dvb_device *dvbdev = file->private_data;
155 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
162 dvb_device_put(dvbdev);
166 EXPORT_SYMBOL(dvb_generic_release);
168 long dvb_generic_ioctl(struct file *file,
169 unsigned int cmd, unsigned long arg)
171 struct dvb_device *dvbdev = file->private_data;
176 if (!dvbdev->kernel_ioctl)
179 return dvb_usercopy(file, cmd, arg, dvbdev->kernel_ioctl);
181 EXPORT_SYMBOL(dvb_generic_ioctl);
183 static int dvbdev_get_free_id(struct dvb_adapter *adap, int type)
187 while (id < DVB_MAX_IDS) {
188 struct dvb_device *dev;
190 list_for_each_entry(dev, &adap->device_list, list_head)
191 if (dev->type == type && dev->id == id)
200 static void dvb_media_device_free(struct dvb_device *dvbdev)
202 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
203 if (dvbdev->entity) {
204 media_device_unregister_entity(dvbdev->entity);
205 kfree(dvbdev->entity);
207 dvbdev->entity = NULL;
211 if (dvbdev->tsout_entity) {
214 for (i = 0; i < dvbdev->tsout_num_entities; i++) {
215 media_device_unregister_entity(&dvbdev->tsout_entity[i]);
216 kfree(dvbdev->tsout_entity[i].name);
218 kfree(dvbdev->tsout_entity);
219 kfree(dvbdev->tsout_pads);
220 dvbdev->tsout_entity = NULL;
221 dvbdev->tsout_pads = NULL;
223 dvbdev->tsout_num_entities = 0;
226 if (dvbdev->intf_devnode) {
227 media_devnode_remove(dvbdev->intf_devnode);
228 dvbdev->intf_devnode = NULL;
231 if (dvbdev->adapter->conn) {
232 media_device_unregister_entity(dvbdev->adapter->conn);
233 kfree(dvbdev->adapter->conn);
234 dvbdev->adapter->conn = NULL;
235 kfree(dvbdev->adapter->conn_pads);
236 dvbdev->adapter->conn_pads = NULL;
241 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
242 static int dvb_create_tsout_entity(struct dvb_device *dvbdev,
243 const char *name, int npads)
247 dvbdev->tsout_pads = kcalloc(npads, sizeof(*dvbdev->tsout_pads),
249 if (!dvbdev->tsout_pads)
252 dvbdev->tsout_entity = kcalloc(npads, sizeof(*dvbdev->tsout_entity),
254 if (!dvbdev->tsout_entity)
257 dvbdev->tsout_num_entities = npads;
259 for (i = 0; i < npads; i++) {
260 struct media_pad *pads = &dvbdev->tsout_pads[i];
261 struct media_entity *entity = &dvbdev->tsout_entity[i];
264 entity->name = kasprintf(GFP_KERNEL, "%s #%d", name, i);
268 entity->function = MEDIA_ENT_F_IO_DTV;
269 pads->flags = MEDIA_PAD_FL_SINK;
271 ret = media_entity_pads_init(entity, 1, pads);
275 ret = media_device_register_entity(dvbdev->adapter->mdev,
283 #define DEMUX_TSOUT "demux-tsout"
284 #define DVR_TSOUT "dvr-tsout"
286 static int dvb_create_media_entity(struct dvb_device *dvbdev,
287 int type, int demux_sink_pads)
292 case DVB_DEVICE_FRONTEND:
296 ret = dvb_create_tsout_entity(dvbdev, DVR_TSOUT,
299 case DVB_DEVICE_DEMUX:
300 npads = 1 + demux_sink_pads;
301 ret = dvb_create_tsout_entity(dvbdev, DEMUX_TSOUT,
311 * We should be creating entities for the MPE/ULE
312 * decapsulation hardware (or software implementation).
314 * However, the number of for the MPE/ULE decaps may not be
315 * fixed. As we don't have yet dynamic support for PADs at
316 * the Media Controller, let's not create the decap
324 dvbdev->entity = kzalloc(sizeof(*dvbdev->entity), GFP_KERNEL);
328 dvbdev->entity->name = dvbdev->name;
331 dvbdev->pads = kcalloc(npads, sizeof(*dvbdev->pads),
334 kfree(dvbdev->entity);
335 dvbdev->entity = NULL;
341 case DVB_DEVICE_FRONTEND:
342 dvbdev->entity->function = MEDIA_ENT_F_DTV_DEMOD;
343 dvbdev->pads[0].flags = MEDIA_PAD_FL_SINK;
344 dvbdev->pads[1].flags = MEDIA_PAD_FL_SOURCE;
346 case DVB_DEVICE_DEMUX:
347 dvbdev->entity->function = MEDIA_ENT_F_TS_DEMUX;
348 dvbdev->pads[0].flags = MEDIA_PAD_FL_SINK;
349 for (i = 1; i < npads; i++)
350 dvbdev->pads[i].flags = MEDIA_PAD_FL_SOURCE;
353 dvbdev->entity->function = MEDIA_ENT_F_DTV_CA;
354 dvbdev->pads[0].flags = MEDIA_PAD_FL_SINK;
355 dvbdev->pads[1].flags = MEDIA_PAD_FL_SOURCE;
358 /* Should never happen, as the first switch prevents it */
359 kfree(dvbdev->entity);
361 dvbdev->entity = NULL;
367 ret = media_entity_pads_init(dvbdev->entity, npads, dvbdev->pads);
371 ret = media_device_register_entity(dvbdev->adapter->mdev,
376 pr_info("%s: media entity '%s' registered.\n",
377 __func__, dvbdev->entity->name);
383 static int dvb_register_media_device(struct dvb_device *dvbdev,
385 unsigned int demux_sink_pads)
387 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
388 struct media_link *link;
392 if (!dvbdev->adapter->mdev)
395 ret = dvb_create_media_entity(dvbdev, type, demux_sink_pads);
400 case DVB_DEVICE_FRONTEND:
401 intf_type = MEDIA_INTF_T_DVB_FE;
403 case DVB_DEVICE_DEMUX:
404 intf_type = MEDIA_INTF_T_DVB_DEMUX;
407 intf_type = MEDIA_INTF_T_DVB_DVR;
410 intf_type = MEDIA_INTF_T_DVB_CA;
413 intf_type = MEDIA_INTF_T_DVB_NET;
419 dvbdev->intf_devnode = media_devnode_create(dvbdev->adapter->mdev,
423 if (!dvbdev->intf_devnode)
427 * Create the "obvious" link, e. g. the ones that represent
428 * a direct association between an interface and an entity.
429 * Other links should be created elsewhere, like:
430 * DVB FE intf -> tuner
431 * DVB demux intf -> dvr
437 link = media_create_intf_link(dvbdev->entity,
438 &dvbdev->intf_devnode->intf,
439 MEDIA_LNK_FL_ENABLED |
440 MEDIA_LNK_FL_IMMUTABLE);
447 int dvb_register_device(struct dvb_adapter *adap, struct dvb_device **pdvbdev,
448 const struct dvb_device *template, void *priv,
449 enum dvb_device_type type, int demux_sink_pads)
451 struct dvb_device *dvbdev;
452 struct file_operations *dvbdevfops = NULL;
453 struct dvbdevfops_node *node = NULL, *new_node = NULL;
454 struct device *clsdev;
458 mutex_lock(&dvbdev_register_lock);
460 id = dvbdev_get_free_id(adap, type);
462 mutex_unlock(&dvbdev_register_lock);
464 pr_err("%s: couldn't find free device id\n", __func__);
468 *pdvbdev = dvbdev = kzalloc(sizeof(*dvbdev), GFP_KERNEL);
470 mutex_unlock(&dvbdev_register_lock);
475 * When a device of the same type is probe()d more than once,
476 * the first allocated fops are used. This prevents memory leaks
477 * that can occur when the same device is probe()d repeatedly.
479 list_for_each_entry(node, &dvbdevfops_list, list_head) {
480 if (node->fops->owner == adap->module &&
481 node->type == type && node->template == template) {
482 dvbdevfops = node->fops;
488 dvbdevfops = kmemdup(template->fops, sizeof(*dvbdevfops), GFP_KERNEL);
491 mutex_unlock(&dvbdev_register_lock);
495 new_node = kzalloc(sizeof(*new_node), GFP_KERNEL);
499 mutex_unlock(&dvbdev_register_lock);
503 new_node->fops = dvbdevfops;
504 new_node->type = type;
505 new_node->template = template;
506 list_add_tail(&new_node->list_head, &dvbdevfops_list);
509 memcpy(dvbdev, template, sizeof(struct dvb_device));
510 kref_init(&dvbdev->ref);
513 dvbdev->adapter = adap;
515 dvbdev->fops = dvbdevfops;
516 init_waitqueue_head(&dvbdev->wait_queue);
517 dvbdevfops->owner = adap->module;
518 list_add_tail(&dvbdev->list_head, &adap->device_list);
519 down_write(&minor_rwsem);
520 #ifdef CONFIG_DVB_DYNAMIC_MINORS
521 for (minor = 0; minor < MAX_DVB_MINORS; minor++)
522 if (!dvb_minors[minor])
524 if (minor == MAX_DVB_MINORS) {
526 list_del(&new_node->list_head);
530 list_del(&dvbdev->list_head);
532 up_write(&minor_rwsem);
533 mutex_unlock(&dvbdev_register_lock);
537 minor = nums2minor(adap->num, type, id);
539 dvbdev->minor = minor;
540 dvb_minors[minor] = dvb_device_get(dvbdev);
541 up_write(&minor_rwsem);
542 ret = dvb_register_media_device(dvbdev, type, minor, demux_sink_pads);
544 pr_err("%s: dvb_register_media_device failed to create the mediagraph\n",
547 list_del(&new_node->list_head);
551 dvb_media_device_free(dvbdev);
552 list_del(&dvbdev->list_head);
554 mutex_unlock(&dvbdev_register_lock);
558 clsdev = device_create(dvb_class, adap->device,
559 MKDEV(DVB_MAJOR, minor),
560 dvbdev, "dvb%d.%s%d", adap->num, dnames[type], id);
561 if (IS_ERR(clsdev)) {
562 pr_err("%s: failed to create device dvb%d.%s%d (%ld)\n",
563 __func__, adap->num, dnames[type], id, PTR_ERR(clsdev));
565 list_del(&new_node->list_head);
569 dvb_media_device_free(dvbdev);
570 list_del(&dvbdev->list_head);
572 mutex_unlock(&dvbdev_register_lock);
573 return PTR_ERR(clsdev);
576 dprintk("DVB: register adapter%d/%s%d @ minor: %i (0x%02x)\n",
577 adap->num, dnames[type], id, minor, minor);
579 mutex_unlock(&dvbdev_register_lock);
582 EXPORT_SYMBOL(dvb_register_device);
584 void dvb_remove_device(struct dvb_device *dvbdev)
589 down_write(&minor_rwsem);
590 dvb_minors[dvbdev->minor] = NULL;
591 dvb_device_put(dvbdev);
592 up_write(&minor_rwsem);
594 dvb_media_device_free(dvbdev);
596 device_destroy(dvb_class, MKDEV(DVB_MAJOR, dvbdev->minor));
598 list_del(&dvbdev->list_head);
600 EXPORT_SYMBOL(dvb_remove_device);
602 static void dvb_free_device(struct kref *ref)
604 struct dvb_device *dvbdev = container_of(ref, struct dvb_device, ref);
609 struct dvb_device *dvb_device_get(struct dvb_device *dvbdev)
611 kref_get(&dvbdev->ref);
614 EXPORT_SYMBOL(dvb_device_get);
616 void dvb_device_put(struct dvb_device *dvbdev)
619 kref_put(&dvbdev->ref, dvb_free_device);
622 void dvb_unregister_device(struct dvb_device *dvbdev)
624 dvb_remove_device(dvbdev);
625 dvb_device_put(dvbdev);
627 EXPORT_SYMBOL(dvb_unregister_device);
629 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
631 static int dvb_create_io_intf_links(struct dvb_adapter *adap,
632 struct media_interface *intf,
635 struct media_device *mdev = adap->mdev;
636 struct media_entity *entity;
637 struct media_link *link;
639 media_device_for_each_entity(entity, mdev) {
640 if (entity->function == MEDIA_ENT_F_IO_DTV) {
641 if (strncmp(entity->name, name, strlen(name)))
643 link = media_create_intf_link(entity, intf,
644 MEDIA_LNK_FL_ENABLED |
645 MEDIA_LNK_FL_IMMUTABLE);
653 int dvb_create_media_graph(struct dvb_adapter *adap,
654 bool create_rf_connector)
656 struct media_device *mdev = adap->mdev;
657 struct media_entity *entity, *tuner = NULL, *demod = NULL, *conn;
658 struct media_entity *demux = NULL, *ca = NULL;
659 struct media_link *link;
660 struct media_interface *intf;
661 unsigned int demux_pad = 0;
662 unsigned int dvr_pad = 0;
663 unsigned int ntuner = 0, ndemod = 0;
664 int ret, pad_source, pad_sink;
665 static const char *connector_name = "Television";
670 media_device_for_each_entity(entity, mdev) {
671 switch (entity->function) {
672 case MEDIA_ENT_F_TUNER:
676 case MEDIA_ENT_F_DTV_DEMOD:
680 case MEDIA_ENT_F_TS_DEMUX:
683 case MEDIA_ENT_F_DTV_CA:
690 * Prepare to signalize to media_create_pad_links() that multiple
691 * entities of the same type exists and a 1:n or n:1 links need to be
693 * NOTE: if both tuner and demod have multiple instances, it is up
694 * to the caller driver to create such links.
701 if (create_rf_connector) {
702 conn = kzalloc(sizeof(*conn), GFP_KERNEL);
707 adap->conn_pads = kzalloc(sizeof(*adap->conn_pads), GFP_KERNEL);
708 if (!adap->conn_pads)
711 conn->flags = MEDIA_ENT_FL_CONNECTOR;
712 conn->function = MEDIA_ENT_F_CONN_RF;
713 conn->name = connector_name;
714 adap->conn_pads->flags = MEDIA_PAD_FL_SOURCE;
716 ret = media_entity_pads_init(conn, 1, adap->conn_pads);
720 ret = media_device_register_entity(mdev, conn);
725 ret = media_create_pad_links(mdev,
728 MEDIA_ENT_F_DTV_DEMOD,
730 MEDIA_LNK_FL_ENABLED,
733 pad_sink = media_get_pad_index(tuner, MEDIA_PAD_FL_SINK,
737 ret = media_create_pad_links(mdev,
742 MEDIA_LNK_FL_ENABLED,
749 if (ntuner && ndemod) {
750 /* NOTE: first found tuner source pad presumed correct */
751 pad_source = media_get_pad_index(tuner, MEDIA_PAD_FL_SOURCE,
755 ret = media_create_pad_links(mdev,
758 MEDIA_ENT_F_DTV_DEMOD,
759 demod, 0, MEDIA_LNK_FL_ENABLED,
765 if (ndemod && demux) {
766 ret = media_create_pad_links(mdev,
767 MEDIA_ENT_F_DTV_DEMOD,
769 MEDIA_ENT_F_TS_DEMUX,
770 demux, 0, MEDIA_LNK_FL_ENABLED,
776 ret = media_create_pad_link(demux, 1, ca,
777 0, MEDIA_LNK_FL_ENABLED);
782 /* Create demux links for each ringbuffer/pad */
784 media_device_for_each_entity(entity, mdev) {
785 if (entity->function == MEDIA_ENT_F_IO_DTV) {
786 if (!strncmp(entity->name, DVR_TSOUT,
787 strlen(DVR_TSOUT))) {
788 ret = media_create_pad_link(demux,
794 if (!strncmp(entity->name, DEMUX_TSOUT,
795 strlen(DEMUX_TSOUT))) {
796 ret = media_create_pad_link(demux,
806 /* Create interface links for FE->tuner, DVR->demux and CA->ca */
807 media_device_for_each_intf(intf, mdev) {
808 if (intf->type == MEDIA_INTF_T_DVB_CA && ca) {
809 link = media_create_intf_link(ca, intf,
810 MEDIA_LNK_FL_ENABLED |
811 MEDIA_LNK_FL_IMMUTABLE);
816 if (intf->type == MEDIA_INTF_T_DVB_FE && tuner) {
817 link = media_create_intf_link(tuner, intf,
818 MEDIA_LNK_FL_ENABLED |
819 MEDIA_LNK_FL_IMMUTABLE);
825 * Indirect link - let's not create yet, as we don't know how
826 * to handle indirect links, nor if this will
827 * actually be needed.
829 if (intf->type == MEDIA_INTF_T_DVB_DVR && demux) {
830 link = media_create_intf_link(demux, intf,
831 MEDIA_LNK_FL_ENABLED |
832 MEDIA_LNK_FL_IMMUTABLE);
837 if (intf->type == MEDIA_INTF_T_DVB_DVR) {
838 ret = dvb_create_io_intf_links(adap, intf, DVR_TSOUT);
842 if (intf->type == MEDIA_INTF_T_DVB_DEMUX) {
843 ret = dvb_create_io_intf_links(adap, intf, DEMUX_TSOUT);
850 EXPORT_SYMBOL_GPL(dvb_create_media_graph);
853 static int dvbdev_check_free_adapter_num(int num)
855 struct list_head *entry;
857 list_for_each(entry, &dvb_adapter_list) {
858 struct dvb_adapter *adap;
860 adap = list_entry(entry, struct dvb_adapter, list_head);
861 if (adap->num == num)
867 static int dvbdev_get_free_adapter_num(void)
871 while (num < DVB_MAX_ADAPTERS) {
872 if (dvbdev_check_free_adapter_num(num))
880 int dvb_register_adapter(struct dvb_adapter *adap, const char *name,
881 struct module *module, struct device *device,
886 mutex_lock(&dvbdev_register_lock);
888 for (i = 0; i < DVB_MAX_ADAPTERS; ++i) {
889 num = adapter_nums[i];
890 if (num >= 0 && num < DVB_MAX_ADAPTERS) {
891 /* use the one the driver asked for */
892 if (dvbdev_check_free_adapter_num(num))
895 num = dvbdev_get_free_adapter_num();
902 mutex_unlock(&dvbdev_register_lock);
906 memset(adap, 0, sizeof(struct dvb_adapter));
907 INIT_LIST_HEAD(&adap->device_list);
909 pr_info("DVB: registering new adapter (%s)\n", name);
913 adap->module = module;
914 adap->device = device;
915 adap->mfe_shared = 0;
916 adap->mfe_dvbdev = NULL;
917 mutex_init(&adap->mfe_lock);
919 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
920 mutex_init(&adap->mdev_lock);
923 list_add_tail(&adap->list_head, &dvb_adapter_list);
925 mutex_unlock(&dvbdev_register_lock);
929 EXPORT_SYMBOL(dvb_register_adapter);
931 int dvb_unregister_adapter(struct dvb_adapter *adap)
933 mutex_lock(&dvbdev_register_lock);
934 list_del(&adap->list_head);
935 mutex_unlock(&dvbdev_register_lock);
938 EXPORT_SYMBOL(dvb_unregister_adapter);
941 * if the miracle happens and "generic_usercopy()" is included into
942 * the kernel, then this can vanish. please don't make the mistake and
943 * define this as video_usercopy(). this will introduce a dependency
944 * to the v4l "videodev.o" module, which is unnecessary for some
945 * cards (ie. the budget dvb-cards don't need the v4l module...)
947 int dvb_usercopy(struct file *file,
948 unsigned int cmd, unsigned long arg,
949 int (*func)(struct file *file,
950 unsigned int cmd, void *arg))
957 /* Copy arguments into temp kernel buffer */
958 switch (_IOC_DIR(cmd)) {
961 * For this command, the pointer is actually an integer
966 case _IOC_READ: /* some v4l ioctls are marked wrong ... */
968 case (_IOC_WRITE | _IOC_READ):
969 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
972 /* too big to allocate from stack */
973 mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
980 if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
986 err = func(file, cmd, parg);
987 if (err == -ENOIOCTLCMD)
993 /* Copy results into user buffer */
994 switch (_IOC_DIR(cmd)) {
996 case (_IOC_WRITE | _IOC_READ):
997 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
1007 #if IS_ENABLED(CONFIG_I2C)
1008 struct i2c_client *dvb_module_probe(const char *module_name,
1010 struct i2c_adapter *adap,
1012 void *platform_data)
1014 struct i2c_client *client;
1015 struct i2c_board_info *board_info;
1017 board_info = kzalloc(sizeof(*board_info), GFP_KERNEL);
1022 strscpy(board_info->type, name, I2C_NAME_SIZE);
1024 strscpy(board_info->type, module_name, I2C_NAME_SIZE);
1026 board_info->addr = addr;
1027 board_info->platform_data = platform_data;
1028 request_module(module_name);
1029 client = i2c_new_client_device(adap, board_info);
1030 if (!i2c_client_has_driver(client)) {
1035 if (!try_module_get(client->dev.driver->owner)) {
1036 i2c_unregister_device(client);
1043 EXPORT_SYMBOL_GPL(dvb_module_probe);
1045 void dvb_module_release(struct i2c_client *client)
1050 module_put(client->dev.driver->owner);
1051 i2c_unregister_device(client);
1053 EXPORT_SYMBOL_GPL(dvb_module_release);
1056 static int dvb_uevent(const struct device *dev, struct kobj_uevent_env *env)
1058 const struct dvb_device *dvbdev = dev_get_drvdata(dev);
1060 add_uevent_var(env, "DVB_ADAPTER_NUM=%d", dvbdev->adapter->num);
1061 add_uevent_var(env, "DVB_DEVICE_TYPE=%s", dnames[dvbdev->type]);
1062 add_uevent_var(env, "DVB_DEVICE_NUM=%d", dvbdev->id);
1066 static char *dvb_devnode(const struct device *dev, umode_t *mode)
1068 const struct dvb_device *dvbdev = dev_get_drvdata(dev);
1070 return kasprintf(GFP_KERNEL, "dvb/adapter%d/%s%d",
1071 dvbdev->adapter->num, dnames[dvbdev->type], dvbdev->id);
1074 static int __init init_dvbdev(void)
1077 dev_t dev = MKDEV(DVB_MAJOR, 0);
1079 retval = register_chrdev_region(dev, MAX_DVB_MINORS, "DVB");
1081 pr_err("dvb-core: unable to get major %d\n", DVB_MAJOR);
1085 cdev_init(&dvb_device_cdev, &dvb_device_fops);
1086 retval = cdev_add(&dvb_device_cdev, dev, MAX_DVB_MINORS);
1088 pr_err("dvb-core: unable register character device\n");
1092 dvb_class = class_create("dvb");
1093 if (IS_ERR(dvb_class)) {
1094 retval = PTR_ERR(dvb_class);
1097 dvb_class->dev_uevent = dvb_uevent;
1098 dvb_class->devnode = dvb_devnode;
1102 cdev_del(&dvb_device_cdev);
1103 unregister_chrdev_region(dev, MAX_DVB_MINORS);
1107 static void __exit exit_dvbdev(void)
1109 struct dvbdevfops_node *node, *next;
1111 class_destroy(dvb_class);
1112 cdev_del(&dvb_device_cdev);
1113 unregister_chrdev_region(MKDEV(DVB_MAJOR, 0), MAX_DVB_MINORS);
1115 list_for_each_entry_safe(node, next, &dvbdevfops_list, list_head) {
1116 list_del(&node->list_head);
1122 subsys_initcall(init_dvbdev);
1123 module_exit(exit_dvbdev);
1125 MODULE_DESCRIPTION("DVB Core Driver");
1126 MODULE_AUTHOR("Marcus Metzler, Ralph Metzler, Holger Waechtler");
1127 MODULE_LICENSE("GPL");