Skip to contentSkip to conversation

add Sensoray 2255 v4l driver

PATCH In review Greg KH 5 participants 10 messages Jun 26, 2008 – Jun 29, 2008 Media (V4L/DVB)

lore
Share & tools
lore

Greg KH

author
#0 lore raw

PATCH 1/1 add Sensoray 2255 v4l driver

From: Dean Anderson <[email protected]>

This driver adds support for the Sensoray 2255 devices.

It was primarily developed by Dean Anderson with only a little bit of guidance and cleanup by Greg.

---

All of the previous review comments have been addressed in this version. Mauro, can you apply this to your tree if there are no other objections?

 drivers/media/video/Kconfig    |    9 
 drivers/media/video/Makefile   |    1 
 drivers/media/video/s2255drv.c | 2486 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 2496 insertions(+)
drivers/media/video/Kconfig +9 -0
@@ -897,6 +897,15 @@ config USB_STKWEBCAM
 	  To compile this driver as a module, choose M here: the
 	  module will be called stkwebcam.
 
+config USB_S2255
+	tristate "USB Sensoray 2255 video capture device"
+	depends on VIDEO_V4L2
+	select VIDEOBUF_VMALLOC
+	default n
+	help
+	  Say Y here if you want support for the Sensoray 2255 USB device.
+	  This driver can be compiled as a module, called s2255drv.
+
 endif # V4L_USB_DRIVERS
 
 config SOC_CAMERA
drivers/media/video/Makefile +1 -0
@@ -122,6 +122,7 @@ obj-$(CONFIG_USB_IBMCAM)        += usbvi
 obj-$(CONFIG_USB_KONICAWC)      += usbvideo/
 obj-$(CONFIG_USB_VICAM)         += usbvideo/
 obj-$(CONFIG_USB_QUICKCAM_MESSENGER)	+= usbvideo/
+obj-$(CONFIG_USB_S2255)		+= s2255drv.o
 
 obj-$(CONFIG_VIDEO_IVTV) += ivtv/
 obj-$(CONFIG_VIDEO_CX18) += cx18/
drivers/media/video/s2255drv.c new file +2486 -0

2,486 lines not on this page · raw message

#1 lore raw

Am Freitag 27 Juni 2008 01:15:51 schrieb Greg KH:

Quoted 15 lines

From: Dean Anderson <[email protected]>

This driver adds support for the Sensoray 2255 devices.

It was primarily developed by Dean Anderson with only a little bit of guidance and cleanup by Greg.

---

All of the previous review comments have been addressed in this version. Mauro, can you apply this to your tree if there are no other objections?

drivers/media/video/s2255drv.c line 470

Quoted 7 lines
drivers/media/video/s2255drv.c new file +7 -0
+/* kickstarts the firmware loading. from probe
+ */
+static void s2255_timer(unsigned long user_data)
+{
+	struct s2255_fw *data = (struct s2255_fw *)user_data;
+	dprintk(100, "s2255 timer\n");
+	if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {

In this error case the state is stuck at FW_NOTLOADED and the firmware can never be loaded as you release it. In principle you should deregister the device under these conditions.

Quoted 27 lines
drivers/media/video/s2255drv.c new file +27 -0
+		printk(KERN_ERR "s2255: can't submit urb\n");
+		if (data->fw) {
+			release_firmware(data->fw);
+			data->fw = NULL;
+		}
+		return;
+	}
+}
+
+/* called when DSP is up and running.  DSP is guaranteed to
+   be running after S2255_DSP_BOOTTIME */
+static void s2255_dsp_running(unsigned long user_data)
+{
+	struct s2255_fw *data = (struct s2255_fw *)user_data;
+	dprintk(1, "dsp running\n");
+	atomic_set(&data->fw_state, S2255_FW_SUCCESS);
+	wake_up(&data->wait_fw);
+	printk(KERN_INFO "s2255: firmware loaded successfully\n");
+	return;
+}
+
+
+/* this loads the firmware asynchronously.
+   Originally this was done synchroously in probe.
+   But it is better to load it asynchronously here than block
+   inside the probe function. Blocking inside probe affects boot time.
+   FW loading is triggered by the timer in the probe function

drivers/media/video/s2255drv.c line 507

Quoted 10 lines
drivers/media/video/s2255drv.c new file +10 -0
+*/
+static void s2255_fwchunk_complete(struct urb *urb)
+{
+	struct s2255_fw *data = urb->context;
+	struct usb_device *udev = urb->dev;
+	int len;
+	dprintk(100, "udev %p urb %p", udev, urb);
+
+	if (urb->status) {
+		dev_err(&udev->dev, "URB failed with status %d", urb->status);

You fail to reflect the change in status.

Quoted 47 lines
drivers/media/video/s2255drv.c new file +47 -0
+		return;
+	}
+	if (data->fw_urb == NULL) {
+		dev_err(&udev->dev, "early disconncect\n");
+		return;
+	}
+#define CHUNK_SIZE 512
+	/* all USB transfers must be done with continuous kernel memory.
+	   can't allocate more than 128k in current linux kernel, so
+	   upload the firmware in chunks
+	 */
+	if (data->fw_loaded < data->fw_size) {
+		len = (data->fw_loaded + CHUNK_SIZE) > data->fw_size ?
+		    data->fw_size % CHUNK_SIZE : CHUNK_SIZE;
+
+		if (len < CHUNK_SIZE)
+			memset(data->pfw_data, 0, CHUNK_SIZE);
+
+		dprintk(100, "completed len %d, loaded %d \n", len,
+			data->fw_loaded);
+
+		memcpy(data->pfw_data,
+		       (char *) data->fw->data + data->fw_loaded, len);
+
+		usb_fill_bulk_urb(data->fw_urb, udev, usb_sndbulkpipe(udev, 2),
+				  data->pfw_data, CHUNK_SIZE,
+				  s2255_fwchunk_complete, data);
+		if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) {
+			dev_err(&udev->dev, "failed submit URB\n");
+			atomic_set(&data->fw_state, S2255_FW_FAILED);
+			/* wake up anything waiting for the firmware */
+			wake_up(&data->wait_fw);
+			return;
+		}
+		data->fw_loaded += len;
+	} else {
+		init_timer(&data->dsp_wait);
+		data->dsp_wait.function = s2255_dsp_running;
+		data->dsp_wait.data = (unsigned long)data;
+		atomic_set(&data->fw_state, S2255_FW_LOADED_DSPWAIT);
+		mod_timer(&data->dsp_wait, msecs_to_jiffies(S2255_DSP_BOOTTIME)
+			  + jiffies);
+	}
+	dprintk(100, "2255 complete done\n");
+	return;
+
+}

[..]

Quoted 13 lines
drivers/media/video/s2255drv.c new file +13 -0
+static void s2255_destroy(struct kref *kref)
+{
+	struct s2255_dev *dev = to_s2255_dev(kref);
+	if (!dev) {
+		printk(KERN_ERR "s2255drv: kref problem\n");
+		return;
+	}
+	/* prevent s2255_disconnect from racing s2255_open */
+	mutex_lock(&dev->open_lock);
+	s2255_exit_v4l(dev);
+	/* device unregistered so no longer possible to open. open_mutex
+	   can be unlocked */
+	mutex_unlock(&dev->open_lock);

drivers/media/video/s2255drv.c line 1572

Quoted 10 lines
drivers/media/video/s2255drv.c new file +10 -0
+
+	/* board shutdown stops the read pipe if it is running */
+	s2255_board_shutdown(dev);
+
+	/* make sure firmware still not trying to load */
+	if (dev->fw_data->fw_urb) {
+		dprintk(2, "kill fw_urb\n");
+		usb_kill_urb(dev->fw_data->fw_urb);
+		usb_free_urb(dev->fw_data->fw_urb);
+		dev->fw_data->fw_urb = NULL;

You are potentially leaving both timers alive.

drivers/media/video/s2255drv.c line 1581

Quoted 9 lines
drivers/media/video/s2255drv.c new file +9 -0
+	}
+
+	/* make sure we aren't waiting for the DSP */
+	if (atomic_read(&dev->fw_data->fw_state) == S2255_FW_LOADED_DSPWAIT) {
+		/* if we are, wait for the wakeup for fw_success or timeout */
+		wait_event_timeout(dev->fw_data->wait_fw,
+				   (atomic_read(&dev->fw_data->fw_state)
+				   == S2255_FW_SUCCESS),
+				   msecs_to_jiffies(S2255_LOAD_TIMEOUT));

If a timeout happens you cannot simply ignore it.

drivers/media/video/s2255drv.c line 1589

Quoted 8 lines
drivers/media/video/s2255drv.c new file +8 -0
+	}
+
+	if (dev->fw_data) {
+		kfree(dev->fw_data->pfw_data);
+		kfree(dev->fw_data);
+	}
+
+	if (dev->fw_data->fw) {

Stone cold access after kfree()

drivers/media/video/s2255drv.c line 1597

Quoted 8 lines
drivers/media/video/s2255drv.c new file +8 -0
+		release_firmware(dev->fw_data->fw);
+		dev->fw_data->fw = NULL;
+	}
+
+	usb_put_dev(dev->udev);
+	dprintk(1, "%s", __func__);
+	kfree(dev);
+}

Regards
Oliver

#2 lore raw
In reply to Greg KH

Hi Greg and Dean,

Quoted 2 lines · On Thu, 26 Jun 2008 16:15:51 -0700 Greg KH <[email protected]> wrote:

All of the previous review comments have been addressed in this version. Mauro, can you apply this to your tree if there are no other objections?

Oliver pointed some issues that seems to fix some bugs at the driver, including one race condition (freeing with timers active). I would add a few other ones.

Even with those issues, I think we should commit the driver at my tree. Having the code there will allow more people to test and eventually discover more issues.

So, if it is ok for you, I'm committing the current patch. I'll be waiting for patches fixing the pointed issues.

Cheers,
Mauro

drivers/media/video/Kconfig config USB_STKWEBCAM · line 903

Quoted 4 lines
drivers/media/video/Kconfig +4 -0
+config USB_S2255
+	tristate "USB Sensoray 2255 video capture device"
+	depends on VIDEO_V4L2
+	select VIDEOBUF_VMALLOC

I think you need also to add:
select VIDEOBUF_GEN

since select doesn't do recursion, afaik.

Quoted 1 line

+#define SYS_FRAMES_MAXSIZE (720*288*2*2 + 4096)

CodingStyle: it should be:

#define SYS_FRAMES_MAXSIZE (720 * 288 * 2 * 2 + 4096)

drivers/media/video/s2255drv.c line 336

Quoted 3 lines
drivers/media/video/s2255drv.c new file +3 -0
+module_param(debug, int, 0);
+module_param(vid_limit, int, 0);
+module_param(video_nr, int, 0);

Why permissions are 0? IMO, it would be better to use 0x644.

Quoted 12 lines
drivers/media/video/s2255drv.c new file +12 -0
+/* converts 2255 planar format to yuyv or uyvy */
+static void planar422p_to_yuv_packed(const unsigned char *in,
+				     unsigned char *out,
+				     int width, int height,
+				     int fmt)
+{
+	unsigned char *pY;
+	unsigned char *pCb;
+	unsigned char *pCr;
+	unsigned long size = height * width;
+	unsigned int i;
+	pY = (unsigned char *)in;

drivers/media/video/s2255drv.c line 461

Quoted 10 lines
drivers/media/video/s2255drv.c new file +10 -0
+	pCr = (unsigned char *)in + height * width;
+	pCb = (unsigned char *)in + height * width + (height * width / 2);
+	for (i = 0; i < size * 2; i += 4) {
+		out[i] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCr++;
+		out[i + 1] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCr++ : *pY++;
+		out[i + 2] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCb++;
+		out[i + 3] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCb++ : *pY++;
+	}
+	return;
+}

Hmm... still keeping format conversion here? Ok for a first version, provided that you move this to firmware on a near version. Btw, we have an userspace library to handle such conversions right now. It allows its dynamic usage even with applications that don't explicitly call the library (even binary-only can use this). It is available at:
http://hansdegoede.livejournal.com/3636.html

IMO, the proper way is to remove this conversion (and add at the library if this conversion is not there yet).

drivers/media/video/s2255drv.c line 685

Quoted 11 lines
drivers/media/video/s2255drv.c new file +11 -0
+static int buffer_setup(struct videobuf_queue *vq, unsigned int *count,
+			unsigned int *size)
+{
+	struct s2255_fh *fh = vq->priv_data;
+
+	*size = fh->width * fh->height * (fh->fmt->depth >> 3);
+
+	if (0 == *count)
+		*count = S2255_DEF_BUFS;
+
+	while (*size * *count > vid_limit * 1024 * 1024)

CodingStyle: Better to use *count inside parenthesis.

drivers/media/video/s2255drv.c line 1429

Quoted 2 lines
drivers/media/video/s2255drv.c new file +2 -0
+static int s2255_open(struct inode *inode, struct file *file)
+{
	...

drivers/media/video/s2255drv.c line 1463

Quoted 7 lines
drivers/media/video/s2255drv.c new file +7 -0
+	dev->users[cur_channel]++;
+	if (dev->users[cur_channel] > S2255_MAX_USERS) {
+		dev->users[cur_channel]--;
+		mutex_unlock(&dev->open_lock);
+		printk(KERN_INFO "s2255drv: too many open handles!\n");
+		return -EBUSY;
+	}

Hmm... V4L2 API allows multiple opens by device. This is, in fact, used sometimes to allow a better control of the video device, like, for example using a different app to change the device controls (for example, qv4l2).

It is preferred to not limit the max users but, instead, to limit the driver to server more than one stream at a time.

---

I'm not seen any code for suspend/resume. I doubt that the state of the video will return back from S1/S3 with the current code. IMO, you'll need to implement explicit handlers for saving the current state and recovering video/audio streams after returning back from sleep.

A last word is about the usage of locks at the driver. I suspect you would need to lock on almost all ioctl handlers. However, as we're currently protected by Kernel big lock, the code is ok. There are some proposed changes to move away from KBL. When this happen, we may need to review the locking schema.

--
video4linux-list mailing list
Unsubscribe mailto:[email protected]?subject=unsubscribe
https://www.redhat.com/mailman/listinfo/video4linux-list

#3 lore raw

[snip]

Quoted 5 lines

A last word is about the usage of locks at the driver. I suspect you would need to lock on almost all ioctl handlers. However, as we're currently protected by Kernel big lock, the code is ok. There are some proposed changes to move away from KBL. When this happen, we may need to review the locking schema.

Given that most drivers rely on the KBL being held, we could address this issue in videodev driver rather than having to update all other drivers. Granted it may not be ideal to do so, but it would buy some time to update the drivers after the BLK is removed.

Regards,

David Ellingsworth

--
video4linux-list mailing list
Unsubscribe mailto:[email protected]?subject=unsubscribe
https://www.redhat.com/mailman/listinfo/video4linux-list

#4 lore raw
In reply to Greg KH
Quoted 7 lines · On Thu, 26 Jun 2008 16:15:51 -0700 Greg KH <[email protected]> wrote:

From: Dean Anderson <[email protected]>

This driver adds support for the Sensoray 2255 devices.

It was primarily developed by Dean Anderson with only a little bit of guidance and cleanup by Greg.

Hmm... there are still a few minor CodingStyle errors:

ERROR: return is not a function, parentheses are not required #881: FILE: drivers/media/video/s2255drv.c:792:
+ return (dev->resources[fh->channel]);

WARNING: line over 80 characters
#898: FILE: drivers/media/video/s2255drv.c:809:
+ strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));

ERROR: return is not a function, parentheses are not required #932: FILE: drivers/media/video/s2255drv.c:843:
+ return (0);

ERROR: return is not a function, parentheses are not required #1053: FILE: drivers/media/video/s2255drv.c:964:
+ return (ret);

ERROR: return is not a function, parentheses are not required #1428: FILE: drivers/media/video/s2255drv.c:1339:
+ return (0);

ERROR: return is not a function, parentheses are not required #1452: FILE: drivers/media/video/s2255drv.c:1363:
+ return (0);

ERROR: return is not a function, parentheses are not required #1467: FILE: drivers/media/video/s2255drv.c:1378:
+ return (0);

ERROR: return is not a function, parentheses are not required #1487: FILE: drivers/media/video/s2255drv.c:1398:
+ return (-ERANGE);

total: 7 errors, 1 warnings, 2508 lines checked

I'm applying the patch right now on my tree.

Dean,

When you have some time, please send me a patch fixing those.

heers,
Mauro

--
video4linux-list mailing list
Unsubscribe mailto:[email protected]?subject=unsubscribe
https://www.redhat.com/mailman/listinfo/video4linux-list

#5 lore raw

Am Samstag, den 28.06.2008, 08:31 -0300 schrieb Mauro Carvalho Chehab:

Quoted 47 lines
Quoted 7 lines · On Thu, 26 Jun 2008 16:15:51 -0700 Greg KH <[email protected]> wrote:

From: Dean Anderson <[email protected]>

This driver adds support for the Sensoray 2255 devices.

It was primarily developed by Dean Anderson with only a little bit of guidance and cleanup by Greg.

Hmm... there are still a few minor CodingStyle errors:

ERROR: return is not a function, parentheses are not required #881: FILE: drivers/media/video/s2255drv.c:792:
+ return (dev->resources[fh->channel]);

WARNING: line over 80 characters
#898: FILE: drivers/media/video/s2255drv.c:809:
+ strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));

ERROR: return is not a function, parentheses are not required #932: FILE: drivers/media/video/s2255drv.c:843:
+ return (0);

ERROR: return is not a function, parentheses are not required #1053: FILE: drivers/media/video/s2255drv.c:964:
+ return (ret);

ERROR: return is not a function, parentheses are not required #1428: FILE: drivers/media/video/s2255drv.c:1339:
+ return (0);

ERROR: return is not a function, parentheses are not required #1452: FILE: drivers/media/video/s2255drv.c:1363:
+ return (0);

ERROR: return is not a function, parentheses are not required #1467: FILE: drivers/media/video/s2255drv.c:1378:
+ return (0);

ERROR: return is not a function, parentheses are not required #1487: FILE: drivers/media/video/s2255drv.c:1398:
+ return (-ERANGE);

total: 7 errors, 1 warnings, 2508 lines checked

Quoted 9 lines

I'm applying the patch right now on my tree.

Dean,

When you have some time, please send me a patch fixing those.

heers,
Mauro

On an attempt to get recent drivers on a FC8 x86_64 machine to watch Glastonbury music festival on BBC HD DVB-S with the saa714 driver on an recently updated 2.6.25 FC kernel it also fails to compile on some strlcpy attempt. Same with a 2.6.24 on the mail machine here.

CC [M] /mnt/xfer/mercurial/v4l-dvb-head/v4l-dvb/v4l/quickcam_messenger.o
CC [M] /mnt/xfer/mercurial/v4l-dvb-head/v4l-dvb/v4l/s2255drv.o
/mnt/xfer/mercurial/v4l-dvb-head/v4l-dvb/v4l/s2255drv.c: In function 'vidioc_querycap':
/mnt/xfer/mercurial/v4l-dvb-head/v4l-dvb/v4l/s2255drv.c:809: error: implicit declaration of function 'dev_name' /mnt/xfer/mercurial/v4l-dvb-head/v4l-dvb/v4l/s2255drv.c:809: warning: passing argument 2 of 'strlcpy' makes pointer from integer without a cast make[3]: *** [/mnt/xfer/mercurial/v4l-dvb-head/v4l-dvb/v4l/s2255drv.o] Error 1
make[2]: *** [_module_/mnt/xfer/mercurial/v4l-dvb-head/v4l-dvb/v4l] Error 2
make[2]: Leaving directory `/usr/src/kernels/2.6.24.7-92.fc8-i686'
make[1]: *** [default] Fehler 2
make[1]: Leaving directory `/mnt/xfer/mercurial/v4l-dvb-head/v4l-dvb/v4l'
make: *** [all] Fehler 2

Cheers,
Hermann

--
video4linux-list mailing list
Unsubscribe mailto:[email protected]?subject=unsubscribe
https://www.redhat.com/mailman/listinfo/video4linux-list

#6 lore raw

Hi Hermann,

Quoted 10 lines · On Sun, 29 Jun 2008 00:08:34 +0200 hermann pitton <[email protected]>…

On an attempt to get recent drivers on a FC8 x86_64 machine to watch Glastonbury music festival on BBC HD DVB-S with the saa714 driver on an recently updated 2.6.25 FC kernel it also fails to compile on some strlcpy attempt. Same with a 2.6.24 on the mail machine here.

CC [M] /mnt/xfer/mercurial/v4l-dvb-head/v4l-dvb/v4l/quickcam_messenger.o
CC [M] /mnt/xfer/mercurial/v4l-dvb-head/v4l-dvb/v4l/s2255drv.o
/mnt/xfer/mercurial/v4l-dvb-head/v4l-dvb/v4l/s2255drv.c: In function 'vidioc_querycap':
/mnt/xfer/mercurial/v4l-dvb-head/v4l-dvb/v4l/s2255drv.c:809: error: implicit declaration of function 'dev_name' /mnt/xfer/mercurial/v4l-dvb-head/v4l-dvb/v4l/s2255drv.c:809: warning: passing argument 2 of 'strlcpy' makes pointer from integer without a cast

The code is not backward compatible. It works only with 2.6.27-rc.

I'll add the macro ballow at compat.h to allow its out of tree compilation:

#define dev_name(dev) ((dev)->bus_id)

This simple macro worked fine with 2.6.25.6.

Cheers,
Mauro

#8 lore raw

Am Samstag, den 28.06.2008, 21:48 -0300 schrieb Mauro Carvalho Chehab:

Quoted 10 lines
Quoted 1 line · On Sat, 28 Jun 2008 21:43:35 -0300 Mauro Carvalho Chehab…

The code is not backward compatible. It works only with 2.6.27-rc.

							  =========

Ops... typo: I mean: 2.6.26-rc8

Cheers,
Mauro

OK.

was out all day and just did hit it.

I stop such reports, if wanted ;)

Cheers,
Hermann

#9 lore raw
In reply to Mauro Carvalho Chehab

Am Samstag, den 28.06.2008, 21:43 -0300 schrieb Mauro Carvalho Chehab:

Quoted 19 lines

Hi Hermann,

Quoted 10 lines · On Sun, 29 Jun 2008 00:08:34 +0200 hermann pitton <[email protected]>…

On an attempt to get recent drivers on a FC8 x86_64 machine to watch Glastonbury music festival on BBC HD DVB-S with the saa714 driver on an recently updated 2.6.25 FC kernel it also fails to compile on some strlcpy attempt. Same with a 2.6.24 on the mail machine here.

CC [M] /mnt/xfer/mercurial/v4l-dvb-head/v4l-dvb/v4l/quickcam_messenger.o
CC [M] /mnt/xfer/mercurial/v4l-dvb-head/v4l-dvb/v4l/s2255drv.o
/mnt/xfer/mercurial/v4l-dvb-head/v4l-dvb/v4l/s2255drv.c: In function 'vidioc_querycap':
/mnt/xfer/mercurial/v4l-dvb-head/v4l-dvb/v4l/s2255drv.c:809: error: implicit declaration of function 'dev_name' /mnt/xfer/mercurial/v4l-dvb-head/v4l-dvb/v4l/s2255drv.c:809: warning: passing argument 2 of 'strlcpy' makes pointer from integer without a cast

The code is not backward compatible. It works only with 2.6.27-rc.

Quoted 8 lines

I'll add the macro ballow at compat.h to allow its out of tree compilation:

#define dev_name(dev) ((dev)->bus_id)

This simple macro worked fine with 2.6.25.6.

Cheers,
Mauro

Seems we had some crossed and delayed mails all over it.

I'll continue to cry, whenever it hurts.

Cheers,
Hermann