Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

V4L/DVB (8751): vivi: Fix some issues at vivi register routine

This patch fixes several small issues at vivi register routines:

1) minor and n_devs should be unsigned;
2) n_devs = 0 were not properly handled;
3) if n_devs specify a high number, the driver would just roll back and
won't register any device. The proper behaviour is to keep all succeded
devices registered;
4) both n_devs and minor were using 0 as permissions. Better to have
them with 0444.

With the current patch, if n_devs specify a very large value, it will
register all possible devices. For example, on a machine without any
other V4L drivers loaded, with this patch, we will have something like:

vivi: V4L2 device registered as /dev/video0
vivi: V4L2 device registered as /dev/video1
vivi: V4L2 device registered as /dev/video2
...
vivi: V4L2 device registered as /dev/video31
video_register_device_index: get_index failed
Video Technology Magazine Virtual Video Capture Board ver 0.5.0 successfully loaded.

5) The number of allocated devices on success is now kept at n_devs:

$ cat /sys/module/vivi/parameters/n_devs
32

Thanks to Henne <henne@nachtwindheim.de> for pointing that there were
some issues at vivi.

Cc: Henne <henne@nachtwindheim.de>
Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>

authored by

Mauro Carvalho Chehab and committed by
Mauro Carvalho Chehab
980d4f17 f3b9f50e

+36 -8
+36 -8
drivers/media/video/vivi.c
··· 1104 1104 Initialization and module stuff 1105 1105 ------------------------------------------------------------------*/ 1106 1106 1107 + /* This routine allocates from 1 to n_devs virtual drivers. 1108 + 1109 + The real maximum number of virtual drivers will depend on how many drivers 1110 + will succeed. This is limited to the maximum number of devices that 1111 + videodev supports. Since there are 64 minors for video grabbers, this is 1112 + currently the theoretical maximum limit. However, a further limit does 1113 + exist at videodev that forbids any driver to register more than 32 video 1114 + grabbers. 1115 + */ 1107 1116 static int __init vivi_init(void) 1108 1117 { 1109 1118 int ret = -ENOMEM, i; 1110 1119 struct vivi_dev *dev; 1111 1120 struct video_device *vfd; 1112 1121 1122 + if (n_devs <= 0) 1123 + n_devs = 1; 1124 + 1113 1125 for (i = 0; i < n_devs; i++) { 1114 1126 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 1115 - if (NULL == dev) 1127 + if (!dev) 1116 1128 break; 1117 - 1118 - list_add_tail(&dev->vivi_devlist, &vivi_devlist); 1119 1129 1120 1130 /* init video dma queues */ 1121 1131 INIT_LIST_HEAD(&dev->vidq.active); ··· 1136 1126 mutex_init(&dev->mutex); 1137 1127 1138 1128 vfd = video_device_alloc(); 1139 - if (NULL == vfd) 1129 + if (!vfd) { 1130 + kfree(dev); 1140 1131 break; 1132 + } 1141 1133 1142 1134 *vfd = vivi_template; 1143 1135 1144 1136 ret = video_register_device(vfd, VFL_TYPE_GRABBER, video_nr); 1145 - if (ret < 0) 1137 + if (ret < 0) { 1138 + video_device_release(vfd); 1139 + kfree(dev); 1140 + 1141 + /* If some registers succeeded, keep driver */ 1142 + if (i) 1143 + ret = 0; 1144 + 1146 1145 break; 1146 + } 1147 + 1148 + /* Now that everything is fine, let's add it to device list */ 1149 + list_add_tail(&dev->vivi_devlist, &vivi_devlist); 1147 1150 1148 1151 snprintf(vfd->name, sizeof(vfd->name), "%s (%i)", 1149 1152 vivi_template.name, vfd->minor); ··· 1172 1149 if (ret < 0) { 1173 1150 vivi_release(); 1174 1151 printk(KERN_INFO "Error %d while loading vivi driver\n", ret); 1175 - } else 1152 + } else { 1176 1153 printk(KERN_INFO "Video Technology Magazine Virtual Video " 1177 1154 "Capture Board ver %u.%u.%u successfully loaded.\n", 1178 1155 (VIVI_VERSION >> 16) & 0xFF, (VIVI_VERSION >> 8) & 0xFF, 1179 1156 VIVI_VERSION & 0xFF); 1157 + 1158 + /* n_devs will reflect the actual number of allocated devices */ 1159 + n_devs = i; 1160 + } 1161 + 1180 1162 return ret; 1181 1163 } 1182 1164 ··· 1197 1169 MODULE_AUTHOR("Mauro Carvalho Chehab, Ted Walther and John Sokol"); 1198 1170 MODULE_LICENSE("Dual BSD/GPL"); 1199 1171 1200 - module_param(video_nr, int, 0); 1172 + module_param(video_nr, uint, 0444); 1201 1173 MODULE_PARM_DESC(video_nr, "video iminor start number"); 1202 1174 1203 - module_param(n_devs, int, 0); 1175 + module_param(n_devs, uint, 0444); 1204 1176 MODULE_PARM_DESC(n_devs, "number of video devices to create"); 1205 1177 1206 1178 module_param_named(debug, vivi_template.debug, int, 0444);