V4L/DVB (9133): v4l: disconnect kernel number from minor

The v4l core creates four different video devices (video, vbi, radio, vtx)
and each has its own range of minor numbers. However, modern devices keep
increasing the number of devices that they need so a maximum of 64 video
devices will not be enough in the future. In addition this scheme makes
it very hard to add new device types.

This patch disconnects the kernel number allocation (e.g. video0, video1,
etc.) from the actual minor number (just pick the first free minor).

This allows for much more flexibility in the future. However, it does
require the use of udev. For those who cannot use udev a new CONFIG option
was created that changes the allocation scheme back to the old behavior.

Thanks to Greg KH for suggesting this approach during the 2008 LPC.

In addition, several bugs were fixed in the ivtv and cx18 drivers: these
drivers try to allocate specific kernel numbers but that scheme contained
a bug which caused what should have been e.g. video17 to appear as e.g.
video2.

Signed-off-by: Hans Verkuil <hverkuil@xs4all.nl>
Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
diff --git a/drivers/media/video/cx18/cx18-streams.c b/drivers/media/video/cx18/cx18-streams.c
index c752a6a..0c8e754 100644
--- a/drivers/media/video/cx18/cx18-streams.c
+++ b/drivers/media/video/cx18/cx18-streams.c
@@ -57,7 +57,7 @@
 static struct {
 	const char *name;
 	int vfl_type;
-	int minor_offset;
+	int num_offset;
 	int dma;
 	enum v4l2_buf_type buf_type;
 	struct file_operations *fops;
@@ -144,8 +144,8 @@
 {
 	struct cx18_stream *s = &cx->streams[type];
 	u32 cap = cx->v4l2_cap;
-	int minor_offset = cx18_stream_info[type].minor_offset;
-	int minor;
+	int num_offset = cx18_stream_info[type].num_offset;
+	int num = cx->num + cx18_first_minor + num_offset;
 
 	/* These four fields are always initialized. If v4l2dev == NULL, then
 	   this stream is not in use. In that case no other fields but these
@@ -164,9 +164,6 @@
 	    !(cap & (V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_CAPTURE)))
 		return 0;
 
-	/* card number + user defined offset + device offset */
-	minor = cx->num + cx18_first_minor + minor_offset;
-
 	/* User explicitly selected 0 buffers for these streams, so don't
 	   create them. */
 	if (cx18_stream_info[type].dma != PCI_DMA_NONE &&
@@ -177,7 +174,7 @@
 
 	cx18_stream_init(cx, type);
 
-	if (minor_offset == -1)
+	if (num_offset == -1)
 		return 0;
 
 	/* allocate and initialize the v4l2 video device structure */
@@ -191,7 +188,7 @@
 	snprintf(s->v4l2dev->name, sizeof(s->v4l2dev->name), "cx18-%d",
 			cx->num);
 
-	s->v4l2dev->minor = minor;
+	s->v4l2dev->num = num;
 	s->v4l2dev->parent = &cx->dev->dev;
 	s->v4l2dev->fops = cx18_stream_info[type].fops;
 	s->v4l2dev->release = video_device_release;
@@ -227,7 +224,7 @@
 {
 	struct cx18_stream *s = &cx->streams[type];
 	int vfl_type = cx18_stream_info[type].vfl_type;
-	int minor;
+	int num;
 
 	/* TODO: Shouldn't this be a VFL_TYPE_TRANSPORT or something?
 	 * We need a VFL_TYPE_TS defined.
@@ -245,38 +242,44 @@
 	if (s->v4l2dev == NULL)
 		return 0;
 
-	minor = s->v4l2dev->minor;
+	num = s->v4l2dev->num;
+	/* card number + user defined offset + device offset */
+	if (type != CX18_ENC_STREAM_TYPE_MPG) {
+		struct cx18_stream *s_mpg = &cx->streams[CX18_ENC_STREAM_TYPE_MPG];
+
+		if (s_mpg->v4l2dev)
+			num = s_mpg->v4l2dev->num + cx18_stream_info[type].num_offset;
+	}
 
 	/* Register device. First try the desired minor, then any free one. */
-	if (video_register_device(s->v4l2dev, vfl_type, minor) &&
-			video_register_device(s->v4l2dev, vfl_type, -1)) {
-		CX18_ERR("Couldn't register v4l2 device for %s minor %d\n",
-			s->name, minor);
+	if (video_register_device(s->v4l2dev, vfl_type, num)) {
+		CX18_ERR("Couldn't register v4l2 device for %s kernel number %d\n",
+			s->name, num);
 		video_device_release(s->v4l2dev);
 		s->v4l2dev = NULL;
 		return -ENOMEM;
 	}
-	minor = s->v4l2dev->minor;
+	num = s->v4l2dev->num;
 
 	switch (vfl_type) {
 	case VFL_TYPE_GRABBER:
 		CX18_INFO("Registered device video%d for %s (%d MB)\n",
-			minor, s->name, cx->options.megabytes[type]);
+			num, s->name, cx->options.megabytes[type]);
 		break;
 
 	case VFL_TYPE_RADIO:
 		CX18_INFO("Registered device radio%d for %s\n",
-			minor - MINOR_VFL_TYPE_RADIO_MIN, s->name);
+			num, s->name);
 		break;
 
 	case VFL_TYPE_VBI:
 		if (cx->options.megabytes[type])
 			CX18_INFO("Registered device vbi%d for %s (%d MB)\n",
-				minor - MINOR_VFL_TYPE_VBI_MIN,
+				num,
 				s->name, cx->options.megabytes[type]);
 		else
 			CX18_INFO("Registered device vbi%d for %s\n",
-				minor - MINOR_VFL_TYPE_VBI_MIN, s->name);
+				num, s->name);
 		break;
 	}