Commit d43fa32f authored by Jean-Francois Moine's avatar Jean-Francois Moine Committed by Mauro Carvalho Chehab

V4L/DVB (8156): Many bug fixes, zc3xx added.

Signed-off-by: default avatarJean-Francois Moine <moinejf@free.fr>
Signed-off-by: default avatarMauro Carvalho Chehab <mchehab@infradead.org>
parent 4aa0d037
obj-$(CONFIG_GSPCA) += gspca_main.o gspca_pac207.o gspca_stk014.o obj-$(CONFIG_GSPCA) += gspca_main.o \
gspca_pac207.o gspca_stk014.o gspca_zc3xx.o
gspca_main-objs := gspca.o gspca_main-objs := gspca.o
gspca_pac207-objs := pac207.o gspca_pac207-objs := pac207.o
gspca_stk014-objs := stk014.o gspca_stk014-objs := stk014.o
gspca_zc3xx-objs := zc3xx.o
...@@ -35,12 +35,18 @@ ...@@ -35,12 +35,18 @@
#include "gspca.h" #include "gspca.h"
/* option */
#define GSPCA_HLP 0
/* global values */
#define DEF_NURBS 2 /* default number of URBs (mmap) */
MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>"); MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
MODULE_DESCRIPTION("GSPCA USB Camera Driver"); MODULE_DESCRIPTION("GSPCA USB Camera Driver");
MODULE_LICENSE("GPL"); MODULE_LICENSE("GPL");
#define DRIVER_VERSION_NUMBER KERNEL_VERSION(0, 1, 1) #define DRIVER_VERSION_NUMBER KERNEL_VERSION(0, 2, 15)
static const char version[] = "0.1.1"; static const char version[] = "0.2.15";
static int video_nr = -1; static int video_nr = -1;
...@@ -71,6 +77,229 @@ static void PDEBUG_MODE(char *txt, __u32 pixfmt, int w, int h) ...@@ -71,6 +77,229 @@ static void PDEBUG_MODE(char *txt, __u32 pixfmt, int w, int h)
#define PDEBUG_MODE(txt, pixfmt, w, h) #define PDEBUG_MODE(txt, pixfmt, w, h)
#endif #endif
/* specific memory types - !! should different from V4L2_MEMORY_xxx */
#define GSPCA_MEMORY_NO 0 /* V4L2_MEMORY_xxx starts from 1 */
#define GSPCA_MEMORY_READ 7
#ifndef GSPCA_HLP
#define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)
#else
#define GSPCA_BUF_FLAG_DECODE 0x1000 /* internal buffer flag */
#define BUF_ALL_FLAGS (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE \
| GSPCA_BUF_FLAG_DECODE)
static int autostart = 4;
module_param(autostart, int, 0644);
MODULE_PARM_DESC(autostart,
"Automatically start the helper process");
/* try to start the helper process */
static void start_hlp(void)
{
int ret;
static char *argv[] = {"gspca_hlp", NULL};
static char *env[] = {NULL};
if (autostart <= 0) {
if (autostart < 0)
PDEBUG(D_ERR|D_PROBE, "Too many helper restart");
return;
}
autostart--;
if (autostart == 0)
autostart = -1;
ret = call_usermodehelper("/sbin/gspca_hlp", argv, env,
UMH_WAIT_EXEC);
if (ret != 0)
PDEBUG(D_ERR|D_PROBE,
"/sbin/gspca_hlp start failed %d", ret);
}
/* /dev/gspca_hlp stuff */
#include <linux/miscdevice.h>
#include "gspca_hlp.h"
/* !! possible decodings defined in decoder.c */
static __u32 bayer_to_tb[] = {
V4L2_PIX_FMT_SBGGR8,
V4L2_PIX_FMT_YUYV,
V4L2_PIX_FMT_YUV420,
V4L2_PIX_FMT_RGB24,
V4L2_PIX_FMT_BGR24,
V4L2_PIX_FMT_RGB565,
};
static __u32 jpeg_to_tb[] = {
V4L2_PIX_FMT_JPEG,
V4L2_PIX_FMT_YUYV,
V4L2_PIX_FMT_YUV420,
V4L2_PIX_FMT_RGB24,
V4L2_PIX_FMT_BGR24,
V4L2_PIX_FMT_RGB565,
};
/* /dev/gspca_hlp device */
struct hlp_dev {
struct gspca_dev *gspca_dev; /* associated device */
struct gspca_frame *frame; /* frame being decoded */
__u32 pixfmt; /* webcam pixel format */
atomic_t nevent; /* nb of frames ready to decode */
wait_queue_head_t wq; /* wait queue */
char fr_d; /* next frame to decode */
} *hlp;
static int hlp_open(struct inode *inode, struct file *file)
{
struct hlp_dev *hlp_dev;
PDEBUG(D_CONF, "hlp open");
if (hlp != 0)
return -EBUSY;
hlp_dev = kzalloc(sizeof *hlp_dev, GFP_KERNEL);
if (hlp_dev == NULL) {
err("couldn't kzalloc hlp struct");
return -EIO;
}
init_waitqueue_head(&hlp_dev->wq);
file->private_data = hlp_dev;
hlp = hlp_dev;
return 0;
}
static int hlp_close(struct inode *inode, struct file *file)
{
struct gspca_dev *gspca_dev;
int mode;
PDEBUG(D_CONF, "hlp close");
file->private_data = NULL;
/* stop decoding */
gspca_dev = hlp->gspca_dev;
if (gspca_dev != 0) {
mode = gspca_dev->curr_mode;
gspca_dev->pixfmt = gspca_dev->cam.cam_mode[mode].pixfmt;
}
/* destroy the helper structure */
kfree(hlp);
hlp = 0;
/* try to restart the helper process */
start_hlp();
return 0;
}
static ssize_t hlp_read(struct file *file, char __user *buf,
size_t cnt, loff_t *ppos)
{
struct hlp_dev *hlp_dev = file->private_data;
struct gspca_dev *gspca_dev;
struct gspca_frame *frame;
struct gspca_hlp_read_hd head;
int i, j, len, ret;
PDEBUG(D_FRAM, "hlp read (%d)", cnt);
/* check / wait till a frame is ready */
for (;;) {
gspca_dev = hlp_dev->gspca_dev;
if (gspca_dev != 0 && gspca_dev->streaming) {
i = hlp_dev->fr_d; /* frame to decode */
j = gspca_dev->fr_queue[i];
frame = &gspca_dev->frame[j];
if (frame->v4l2_buf.flags & GSPCA_BUF_FLAG_DECODE)
break;
}
ret = wait_event_interruptible(hlp_dev->wq,
atomic_read(&hlp_dev->nevent) > 0);
if (ret < 0) { /* helper process is killed */
autostart = 0; /* don't restart it */
return ret;
}
}
atomic_dec(&hlp_dev->nevent);
hlp_dev->fr_d = (i + 1) % gspca_dev->nframes;
PDEBUG(D_FRAM, "hlp read q:%d i:%d d:%d o:%d",
gspca_dev->fr_q,
gspca_dev->fr_i,
hlp_dev->fr_d,
gspca_dev->fr_o);
hlp_dev->frame = frame; /* memorize the current frame */
len = frame->v4l2_buf.bytesused;
if (cnt < sizeof head - sizeof head.data + len)
/*fixme: special errno?*/
return -EINVAL;
head.pixfmt_out = gspca_dev->pixfmt;
head.pixfmt_in = hlp_dev->pixfmt;
head.width = gspca_dev->width;
head.height = gspca_dev->height;
copy_to_user(buf, &head, sizeof head);
copy_to_user(buf + sizeof head - sizeof head.data,
frame->data, len);
return sizeof head - sizeof head.data + len;
}
static ssize_t hlp_write(struct file *file,
const char __user *buf,
size_t cnt, loff_t *ppos)
{
struct hlp_dev *hlp_dev = file->private_data;
struct gspca_dev *gspca_dev;
struct gspca_frame *frame;
PDEBUG(D_FRAM, "hlp write (%d)", cnt);
gspca_dev = hlp_dev->gspca_dev;
if (gspca_dev == 0)
return cnt;
if (mutex_lock_interruptible(&gspca_dev->queue_lock))
return -ERESTARTSYS;
if (!gspca_dev->streaming)
goto out;
frame = hlp_dev->frame;
hlp_dev->frame = 0;
if (frame == 0)
goto out;
if (cnt > frame->v4l2_buf.length) {
PDEBUG(D_ERR|D_FRAM, "bad frame size %d - %d",
cnt, frame->v4l2_buf.length);
cnt = -EINVAL;
goto out;
}
copy_from_user(frame->data, buf, cnt);
frame->v4l2_buf.bytesused = cnt;
frame->v4l2_buf.flags &= ~(V4L2_BUF_FLAG_QUEUED
| GSPCA_BUF_FLAG_DECODE);
frame->v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
mutex_unlock(&gspca_dev->queue_lock);
atomic_inc(&gspca_dev->nevent);
wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
PDEBUG(D_FRAM, "hlp write q:%d i:%d d:%d o:%d",
gspca_dev->fr_q,
gspca_dev->fr_i,
hlp_dev->fr_d,
gspca_dev->fr_o);
return cnt;
out:
mutex_unlock(&gspca_dev->queue_lock);
return cnt;
}
static struct file_operations hlp_fops = {
.owner = THIS_MODULE,
.open = hlp_open,
.release = hlp_close,
.read = hlp_read,
.write = hlp_write,
.llseek = no_llseek
};
static struct miscdevice hlp_device = {
.minor = MISC_DYNAMIC_MINOR,
.name = "gspca_hlp",
.fops = &hlp_fops,
};
#endif
/* /*
* VMA operations. * VMA operations.
*/ */
...@@ -96,22 +325,16 @@ static struct vm_operations_struct gspca_vm_ops = { ...@@ -96,22 +325,16 @@ static struct vm_operations_struct gspca_vm_ops = {
}; };
/* /*
* ISOC message interrupt from the USB device * fill a video frame from an URB and resubmit
*
* Analyse each packet and call the subdriver for doing the copy
* to the frame buffer.
*/ */
static void isoc_irq(struct urb *urb) static void fill_frame(struct gspca_dev *gspca_dev,
struct urb *urb)
{ {
struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
struct gspca_frame *frame; struct gspca_frame *frame;
unsigned char *data; /* address of data in the iso message */ unsigned char *data; /* address of data in the iso message */
int i, j, len, st; int i, j, len, st;
cam_pkt_op pkt_scan; cam_pkt_op pkt_scan;
PDEBUG(D_PACK, "isoc irq");
if (!gspca_dev->streaming)
return;
pkt_scan = gspca_dev->sd_desc->pkt_scan; pkt_scan = gspca_dev->sd_desc->pkt_scan;
for (i = 0; i < urb->number_of_packets; i++) { for (i = 0; i < urb->number_of_packets; i++) {
...@@ -119,8 +342,7 @@ static void isoc_irq(struct urb *urb) ...@@ -119,8 +342,7 @@ static void isoc_irq(struct urb *urb)
j = gspca_dev->fr_i; j = gspca_dev->fr_i;
j = gspca_dev->fr_queue[j]; j = gspca_dev->fr_queue[j];
frame = &gspca_dev->frame[j]; frame = &gspca_dev->frame[j];
if ((frame->v4l2_buf.flags if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
& (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE))
!= V4L2_BUF_FLAG_QUEUED) { != V4L2_BUF_FLAG_QUEUED) {
gspca_dev->last_packet_type = DISCARD_PACKET; gspca_dev->last_packet_type = DISCARD_PACKET;
break; break;
...@@ -147,16 +369,86 @@ static void isoc_irq(struct urb *urb) ...@@ -147,16 +369,86 @@ static void isoc_irq(struct urb *urb)
} }
/* resubmit the URB */ /* resubmit the URB */
/*fixme: don't do that when userptr and too many URBs sent*/
urb->status = 0; urb->status = 0;
st = usb_submit_urb(urb, GFP_ATOMIC); st = usb_submit_urb(urb, GFP_ATOMIC);
if (st < 0) if (st < 0)
PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st); PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st);
} }
/*
* ISOC message interrupt from the USB device
*
* Analyse each packet and call the subdriver for copy
* to the frame buffer.
*
* There are 2 functions:
* - the first one (isoc_irq_mmap) is used when the application
* buffers are mapped. The frame detection and copy is done
* at interrupt level.
* - the second one (isoc_irq_user) is used when the application
* buffers are in user space (userptr). The frame detection
* and copy is done by the application.
*/
static void isoc_irq_mmap(struct urb *urb)
{
struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
PDEBUG(D_PACK, "isoc irq mmap");
if (!gspca_dev->streaming)
return;
fill_frame(gspca_dev, urb);
}
static void isoc_irq_user(struct urb *urb)
{
struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
int i;
PDEBUG(D_PACK, "isoc irq user");
if (!gspca_dev->streaming)
return;
i = gspca_dev->urb_in % gspca_dev->nurbs;
if (urb != gspca_dev->urb[i]) {
PDEBUG(D_ERR|D_PACK, "urb out of sequence");
return; /* should never occur */
}
gspca_dev->urb_in++;
atomic_inc(&gspca_dev->nevent); /* new event */
wake_up_interruptible(&gspca_dev->wq);
/*fixme: submit a new URBs until urb_in == urb_out (% nurbs)*/
}
/*
* treat the isoc messages
*
* This routine is called by the application (case userptr).
*/
static void isoc_transfer(struct gspca_dev *gspca_dev)
{
struct urb *urb;
int i;
for (;;) {
i = gspca_dev->urb_out;
PDEBUG(D_PACK, "isoc transf i:%d o:%d", gspca_dev->urb_in, i);
if (i == gspca_dev->urb_in) /* isoc message to read */
break; /* no (more) message */
atomic_dec(&gspca_dev->nevent);
/*PDEBUG(D_PACK, "isoc_trf nevent: %d", atomic_read(&gspca_dev->nevent));*/
gspca_dev->urb_out = i + 1; /* message treated */
urb = gspca_dev->urb[i % gspca_dev->nurbs];
fill_frame(gspca_dev, urb);
}
}
/* /*
* add data to the current frame * add data to the current frame
* *
* This function is called by the subdrivers at interrupt level. * This function is called by the subdrivers at interrupt level
* or user level.
* To build a frame, these ones must add * To build a frame, these ones must add
* - one FIRST_PACKET * - one FIRST_PACKET
* - 0 or many INTER_PACKETs * - 0 or many INTER_PACKETs
...@@ -177,9 +469,8 @@ struct gspca_frame *gspca_frame_add(struct gspca_dev *gspca_dev, ...@@ -177,9 +469,8 @@ struct gspca_frame *gspca_frame_add(struct gspca_dev *gspca_dev,
/* when start of a new frame, if the current frame buffer /* when start of a new frame, if the current frame buffer
* is not queued, discard the whole frame */ * is not queued, discard the whole frame */
if (packet_type == FIRST_PACKET) { if (packet_type == FIRST_PACKET) {
if ((frame->v4l2_buf.flags if ((frame->v4l2_buf.flags & BUF_ALL_FLAGS)
& (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)) != V4L2_BUF_FLAG_QUEUED) {
!= V4L2_BUF_FLAG_QUEUED) {
gspca_dev->last_packet_type = DISCARD_PACKET; gspca_dev->last_packet_type = DISCARD_PACKET;
return frame; return frame;
} }
...@@ -187,10 +478,11 @@ struct gspca_frame *gspca_frame_add(struct gspca_dev *gspca_dev, ...@@ -187,10 +478,11 @@ struct gspca_frame *gspca_frame_add(struct gspca_dev *gspca_dev,
jiffies_to_timeval(get_jiffies_64(), jiffies_to_timeval(get_jiffies_64(),
&frame->v4l2_buf.timestamp); &frame->v4l2_buf.timestamp);
frame->v4l2_buf.sequence = ++gspca_dev->sequence; frame->v4l2_buf.sequence = ++gspca_dev->sequence;
} else if (gspca_dev->last_packet_type == DISCARD_PACKET) } else if (gspca_dev->last_packet_type == DISCARD_PACKET) {
return frame; return frame;
}
/* append the packet in the frame buffer */ /* append the packet to the frame buffer */
if (len > 0) { if (len > 0) {
if (frame->data_end - frame->data + len if (frame->data_end - frame->data + len
> frame->v4l2_buf.length) { > frame->v4l2_buf.length) {
...@@ -211,12 +503,25 @@ struct gspca_frame *gspca_frame_add(struct gspca_dev *gspca_dev, ...@@ -211,12 +503,25 @@ struct gspca_frame *gspca_frame_add(struct gspca_dev *gspca_dev,
/* if last packet, wake the application and advance in the queue */ /* if last packet, wake the application and advance in the queue */
if (packet_type == LAST_PACKET) { if (packet_type == LAST_PACKET) {
frame->v4l2_buf.bytesused = frame->data_end - frame->data; frame->v4l2_buf.bytesused = frame->data_end - frame->data;
#ifndef GSPCA_HLP
frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED; frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
frame->v4l2_buf.flags |= V4L2_BUF_FLAG_DONE; frame->v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
atomic_inc(&gspca_dev->nevent); atomic_inc(&gspca_dev->nevent);
wake_up_interruptible(&gspca_dev->wq); /* event = new frame */ wake_up_interruptible(&gspca_dev->wq); /* event = new frame */
i = gspca_dev->fr_i; #else /*GSPCA_HLP*/
i = (i + 1) % gspca_dev->nframes; if (hlp != 0 && hlp->gspca_dev == gspca_dev) {
frame->v4l2_buf.flags |= GSPCA_BUF_FLAG_DECODE;
atomic_inc(&hlp->nevent);
wake_up_interruptible(&hlp->wq);
} else {
frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
frame->v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
atomic_inc(&gspca_dev->nevent);
wake_up_interruptible(&gspca_dev->wq); /* new frame */
}
#endif /*GSPCA_HLP*/
i = (gspca_dev->fr_i + 1) % gspca_dev->nframes;
gspca_dev->fr_i = i;
PDEBUG(D_FRAM, "frame complete len:%d q:%d i:%d o:%d", PDEBUG(D_FRAM, "frame complete len:%d q:%d i:%d o:%d",
frame->v4l2_buf.bytesused, frame->v4l2_buf.bytesused,
gspca_dev->fr_q, gspca_dev->fr_q,
...@@ -224,7 +529,6 @@ struct gspca_frame *gspca_frame_add(struct gspca_dev *gspca_dev, ...@@ -224,7 +529,6 @@ struct gspca_frame *gspca_frame_add(struct gspca_dev *gspca_dev,
gspca_dev->fr_o); gspca_dev->fr_o);
j = gspca_dev->fr_queue[i]; j = gspca_dev->fr_queue[i];
frame = &gspca_dev->frame[j]; frame = &gspca_dev->frame[j];
gspca_dev->fr_i = i;
} }
return frame; return frame;
} }
...@@ -245,7 +549,7 @@ static void *rvmalloc(unsigned long size) ...@@ -245,7 +549,7 @@ static void *rvmalloc(unsigned long size)
void *mem; void *mem;
unsigned long adr; unsigned long adr;
size = PAGE_ALIGN(size); /* size = PAGE_ALIGN(size); (already done) */
mem = vmalloc_32(size); mem = vmalloc_32(size);
if (mem != 0) { if (mem != 0) {
memset(mem, 0, size); memset(mem, 0, size);
...@@ -274,27 +578,65 @@ static void rvfree(void *mem, unsigned long size) ...@@ -274,27 +578,65 @@ static void rvfree(void *mem, unsigned long size)
vfree(mem); vfree(mem);
} }
static __u32 get_v4l2_depth(__u32 pixfmt)
{
switch (pixfmt) {
case V4L2_PIX_FMT_BGR32:
case V4L2_PIX_FMT_RGB32:
return 32;
case V4L2_PIX_FMT_RGB24: /* 'RGB3' */
case V4L2_PIX_FMT_BGR24:
return 24;
case V4L2_PIX_FMT_RGB565: /* 'RGBP' */
case V4L2_PIX_FMT_YUYV: /* 'YUYV' packed 4.2.2 */
case V4L2_PIX_FMT_YYUV: /* 'YYUV' */
return 16;
case V4L2_PIX_FMT_YUV420: /* 'YU12' planar 4.2.0 */
return 12;
case V4L2_PIX_FMT_MJPEG:
case V4L2_PIX_FMT_JPEG:
case V4L2_PIX_FMT_SBGGR8: /* 'BA81' Bayer */
return 8;
}
PDEBUG(D_ERR|D_CONF, "Unknown pixel format %c%c%c%c",
pixfmt & 0xff,
(pixfmt >> 8) & 0xff,
(pixfmt >> 16) & 0xff,
pixfmt >> 24);
return -EINVAL;
}
static int gspca_get_buff_size(struct gspca_dev *gspca_dev)
{
unsigned int size;
size = gspca_dev->width * gspca_dev->height
* get_v4l2_depth(gspca_dev->pixfmt) / 8;
if (!size)
return -ENOMEM;
return size;
}
static int frame_alloc(struct gspca_dev *gspca_dev, static int frame_alloc(struct gspca_dev *gspca_dev,
unsigned int count, unsigned int count)
unsigned int frsz,
enum v4l2_memory memory)
{ {
int i, ret = 0; struct gspca_frame *frame;
unsigned int frsz;
int i;
frsz = gspca_get_buff_size(gspca_dev);
if (frsz < 0)
return frsz;
PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz); PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz);
if (gspca_dev->nframes != 0) {
PDEBUG(D_ERR|D_STREAM, "alloc frame already done");
return -EBUSY;
}
if (count > GSPCA_MAX_FRAMES) if (count > GSPCA_MAX_FRAMES)
count = GSPCA_MAX_FRAMES; count = GSPCA_MAX_FRAMES;
/* if compressed, reduce the buffer size */ /* if compressed (JPEG), reduce the buffer size */
if (gspca_is_compressed(gspca_dev->pixfmt)) if (gspca_is_compressed(gspca_dev->pixfmt))
frsz = (frsz * comp_fac) / 100; frsz = (frsz * comp_fac) / 100 + 600; /* plus JPEG header */
frsz = PAGE_ALIGN(frsz); frsz = PAGE_ALIGN(frsz);
PDEBUG(D_STREAM, "new fr_sz: %d", frsz); PDEBUG(D_STREAM, "new fr_sz: %d", frsz);
gspca_dev->frsz = frsz; gspca_dev->frsz = frsz;
if (memory == V4L2_MEMORY_MMAP) { if (gspca_dev->memory == V4L2_MEMORY_MMAP) {
gspca_dev->frbuf = rvmalloc(frsz * count); gspca_dev->frbuf = rvmalloc(frsz * count);
if (!gspca_dev->frbuf) { if (!gspca_dev->frbuf) {
err("frame alloc failed"); err("frame alloc failed");
...@@ -303,25 +645,36 @@ static int frame_alloc(struct gspca_dev *gspca_dev, ...@@ -303,25 +645,36 @@ static int frame_alloc(struct gspca_dev *gspca_dev,
} }
gspca_dev->nframes = count; gspca_dev->nframes = count;
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
gspca_dev->frame[i].v4l2_buf.index = i; frame = &gspca_dev->frame[i];
gspca_dev->frame[i].v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; frame->v4l2_buf.index = i;
gspca_dev->frame[i].v4l2_buf.flags = 0; frame->v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
gspca_dev->frame[i].v4l2_buf.field = V4L2_FIELD_NONE; frame->v4l2_buf.flags = 0;
gspca_dev->frame[i].v4l2_buf.length = frsz; frame->v4l2_buf.field = V4L2_FIELD_NONE;
gspca_dev->frame[i].v4l2_buf.memory = memory; frame->v4l2_buf.length = frsz;
if (memory == V4L2_MEMORY_MMAP) { frame->v4l2_buf.memory = gspca_dev->memory;
gspca_dev->frame[i].data frame->v4l2_buf.sequence = 0;
= gspca_dev->frame[i].data_end if (gspca_dev->memory == V4L2_MEMORY_MMAP) {
= gspca_dev->frbuf + i * frsz; frame->data = frame->data_end =
gspca_dev->frame[i].v4l2_buf.m.offset = i * frsz; gspca_dev->frbuf + i * frsz;
frame->v4l2_buf.m.offset = i * frsz;
} }
gspca_dev->frame[i].v4l2_buf.flags = 0; /* buf in app space */
} }
gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0; gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0;
#ifdef GSPCA_HLP
{
struct hlp_dev *hlp_dev;
hlp_dev = hlp;
if (hlp != 0 && hlp_dev->gspca_dev == gspca_dev) {
hlp_dev->fr_d = 0;
atomic_set(&hlp_dev->nevent, 0);
}
}
#endif /*GSPCA_HLP*/
gspca_dev->last_packet_type = DISCARD_PACKET; gspca_dev->last_packet_type = DISCARD_PACKET;
gspca_dev->sequence = 0; gspca_dev->sequence = 0;
atomic_set(&gspca_dev->nevent, 0); atomic_set(&gspca_dev->nevent, 0);
return ret; return 0;
} }
static void frame_free(struct gspca_dev *gspca_dev) static void frame_free(struct gspca_dev *gspca_dev)
...@@ -339,16 +692,16 @@ static void frame_free(struct gspca_dev *gspca_dev) ...@@ -339,16 +692,16 @@ static void frame_free(struct gspca_dev *gspca_dev)
gspca_dev->nframes = 0; gspca_dev->nframes = 0;
} }
static int gspca_kill_transfer(struct gspca_dev *gspca_dev) static void destroy_urbs(struct gspca_dev *gspca_dev)
{ {
struct urb *urb; struct urb *urb;
unsigned int i; unsigned int i;
PDEBUG(D_STREAM, "kill transfer"); PDEBUG(D_STREAM, "kill transfer");
for (i = 0; i < NURBS; ++i) { for (i = 0; i < MAX_NURBS; ++i) {
urb = gspca_dev->urb[i]; urb = gspca_dev->urb[i];
if (urb == NULL) if (urb == NULL)
continue; break;
gspca_dev->urb[i] = NULL; gspca_dev->urb[i] = NULL;
usb_kill_urb(urb); usb_kill_urb(urb);
...@@ -359,7 +712,6 @@ static int gspca_kill_transfer(struct gspca_dev *gspca_dev) ...@@ -359,7 +712,6 @@ static int gspca_kill_transfer(struct gspca_dev *gspca_dev)
urb->transfer_dma); urb->transfer_dma);
usb_free_urb(urb); usb_free_urb(urb);
} }
return 0;
} }
/* /*
...@@ -417,7 +769,7 @@ struct usb_host_endpoint *get_isoc_ep(struct gspca_dev *gspca_dev) ...@@ -417,7 +769,7 @@ struct usb_host_endpoint *get_isoc_ep(struct gspca_dev *gspca_dev)
err("set interface err %d", ret); err("set interface err %d", ret);
return NULL; return NULL;
} }
gspca_dev->alt = i; gspca_dev->alt = i; /* memorize the current alt setting */
return ep; return ep;
} }
...@@ -428,28 +780,28 @@ static int create_urbs(struct gspca_dev *gspca_dev, ...@@ -428,28 +780,28 @@ static int create_urbs(struct gspca_dev *gspca_dev,
struct usb_host_endpoint *ep) struct usb_host_endpoint *ep)
{ {
struct urb *urb; struct urb *urb;
int n, i, psize, npkt, bsize; int n, nurbs, i, psize, npkt, bsize;
usb_complete_t usb_complete;
/* calculate the packet size and the number of packets */ /* calculate the packet size and the number of packets */
/* the URB buffer size must be a power of 2 */
psize = le16_to_cpu(ep->desc.wMaxPacketSize); psize = le16_to_cpu(ep->desc.wMaxPacketSize);
/* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */ /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */
psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3)); psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
npkt = ISO_MAX_SIZE / psize; npkt = ISO_MAX_SIZE / psize;
if (npkt > ISO_MAX_PKT) if (npkt > ISO_MAX_PKT)
npkt = ISO_MAX_PKT; npkt = ISO_MAX_PKT;
bsize = psize * npkt; bsize = psize * npkt;
for (n = ISO_MAX_SIZE; n > 0; n >>= 1) {
if (n & bsize) /* !! assume ISO_MAX_SIZE is a power of 2 */
break;
}
if (n != 0) {
npkt = n / psize;
bsize = psize * npkt;
}
PDEBUG(D_STREAM, PDEBUG(D_STREAM,
"isoc %d pkts size %d (bsize:%d)", npkt, psize, bsize); "isoc %d pkts size %d (bsize:%d)", npkt, psize, bsize);
for (n = 0; n < NURBS; n++) { /*fixme:change for userptr*/
/*fixme:don't submit all URBs when userptr*/
gspca_dev->nurbs = nurbs = DEF_NURBS;
if (gspca_dev->memory == V4L2_MEMORY_MMAP)
usb_complete = isoc_irq_mmap;
else
usb_complete = isoc_irq_user;
for (n = 0; n < nurbs; n++) {
urb = usb_alloc_urb(npkt, GFP_KERNEL); urb = usb_alloc_urb(npkt, GFP_KERNEL);
if (!urb) { if (!urb) {
err("usb_alloc_urb failed"); err("usb_alloc_urb failed");
...@@ -462,7 +814,7 @@ static int create_urbs(struct gspca_dev *gspca_dev, ...@@ -462,7 +814,7 @@ static int create_urbs(struct gspca_dev *gspca_dev,
if (urb->transfer_buffer == NULL) { if (urb->transfer_buffer == NULL) {
usb_free_urb(urb); usb_free_urb(urb);
gspca_kill_transfer(gspca_dev); destroy_urbs(gspca_dev);
err("usb_buffer_urb failed"); err("usb_buffer_urb failed");
return -ENOMEM; return -ENOMEM;
} }
...@@ -474,7 +826,7 @@ static int create_urbs(struct gspca_dev *gspca_dev, ...@@ -474,7 +826,7 @@ static int create_urbs(struct gspca_dev *gspca_dev,
urb->transfer_flags = URB_ISO_ASAP urb->transfer_flags = URB_ISO_ASAP
| URB_NO_TRANSFER_DMA_MAP; | URB_NO_TRANSFER_DMA_MAP;
urb->interval = ep->desc.bInterval; urb->interval = ep->desc.bInterval;
urb->complete = isoc_irq; urb->complete = usb_complete;
urb->number_of_packets = npkt; urb->number_of_packets = npkt;
urb->transfer_buffer_length = bsize; urb->transfer_buffer_length = bsize;
for (i = 0; i < npkt; i++) { for (i = 0; i < npkt; i++) {
...@@ -482,6 +834,7 @@ static int create_urbs(struct gspca_dev *gspca_dev, ...@@ -482,6 +834,7 @@ static int create_urbs(struct gspca_dev *gspca_dev,
urb->iso_frame_desc[i].offset = psize * i; urb->iso_frame_desc[i].offset = psize * i;
} }
} }
gspca_dev->urb_in = gspca_dev->urb_out = 0;
return 0; return 0;
} }
...@@ -490,18 +843,17 @@ static int create_urbs(struct gspca_dev *gspca_dev, ...@@ -490,18 +843,17 @@ static int create_urbs(struct gspca_dev *gspca_dev,
*/ */
static int gspca_init_transfer(struct gspca_dev *gspca_dev) static int gspca_init_transfer(struct gspca_dev *gspca_dev)
{ {
struct usb_interface *intf;
struct usb_host_endpoint *ep; struct usb_host_endpoint *ep;
int n, ret; int n, ret;
if (mutex_lock_interruptible(&gspca_dev->usb_lock)) if (mutex_lock_interruptible(&gspca_dev->usb_lock))
return -ERESTARTSYS; return -ERESTARTSYS;
/* set the max alternate setting and loop until urb submit succeeds */ /* set the higher alternate setting and
intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface); * loop until urb submit succeeds */
gspca_dev->alt = intf->num_altsetting; gspca_dev->alt = gspca_dev->nbalt;
for (;;) { for (;;) {
PDEBUG(D_STREAM, "init transfer nbalt %d", gspca_dev->alt); PDEBUG(D_STREAM, "init transfer alt %d", gspca_dev->alt);
ep = get_isoc_ep(gspca_dev); ep = get_isoc_ep(gspca_dev);
if (ep == NULL) { if (ep == NULL) {
ret = -EIO; ret = -EIO;
...@@ -517,13 +869,13 @@ static int gspca_init_transfer(struct gspca_dev *gspca_dev) ...@@ -517,13 +869,13 @@ static int gspca_init_transfer(struct gspca_dev *gspca_dev)
atomic_set(&gspca_dev->nevent, 0); atomic_set(&gspca_dev->nevent, 0);
/* submit the URBs */ /* submit the URBs */
for (n = 0; n < NURBS; n++) { for (n = 0; n < gspca_dev->nurbs; n++) {
ret = usb_submit_urb(gspca_dev->urb[n], GFP_KERNEL); ret = usb_submit_urb(gspca_dev->urb[n], GFP_KERNEL);
if (ret < 0) { if (ret < 0) {
PDEBUG(D_ERR|D_STREAM, PDEBUG(D_ERR|D_STREAM,
"usb_submit_urb [%d] err %d", n, ret); "usb_submit_urb [%d] err %d", n, ret);
gspca_dev->streaming = 0; gspca_dev->streaming = 0;
gspca_kill_transfer(gspca_dev); destroy_urbs(gspca_dev);
if (ret == -ENOSPC) if (ret == -ENOSPC)
break; /* try the previous alt */ break; /* try the previous alt */
goto out; goto out;
...@@ -551,21 +903,32 @@ static int gspca_set_alt0(struct gspca_dev *gspca_dev) ...@@ -551,21 +903,32 @@ static int gspca_set_alt0(struct gspca_dev *gspca_dev)
static void gspca_stream_off(struct gspca_dev *gspca_dev) static void gspca_stream_off(struct gspca_dev *gspca_dev)
{ {
gspca_dev->streaming = 0; gspca_dev->streaming = 0;
atomic_set(&gspca_dev->nevent, 0);
#ifdef GSPCA_HLP
{
struct hlp_dev *hlp_dev;
hlp_dev = hlp;
if (hlp_dev != 0
&& hlp_dev->gspca_dev == gspca_dev)
atomic_set(&hlp_dev->nevent, 0);
}
#endif
if (gspca_dev->present) { if (gspca_dev->present) {
gspca_dev->sd_desc->stopN(gspca_dev); gspca_dev->sd_desc->stopN(gspca_dev);
gspca_kill_transfer(gspca_dev); destroy_urbs(gspca_dev);
gspca_set_alt0(gspca_dev); gspca_set_alt0(gspca_dev);
gspca_dev->sd_desc->stop0(gspca_dev); gspca_dev->sd_desc->stop0(gspca_dev);
PDEBUG(D_STREAM, "stream off OK"); PDEBUG(D_STREAM, "stream off OK");
} else { } else {
gspca_kill_transfer(gspca_dev); destroy_urbs(gspca_dev);
atomic_inc(&gspca_dev->nevent); atomic_inc(&gspca_dev->nevent);
wake_up_interruptible(&gspca_dev->wq); wake_up_interruptible(&gspca_dev->wq);
PDEBUG(D_ERR|D_STREAM, "stream off no device ??"); PDEBUG(D_ERR|D_STREAM, "stream off no device ??");
} }
} }
static int gspca_set_default_mode(struct gspca_dev *gspca_dev) static void gspca_set_default_mode(struct gspca_dev *gspca_dev)
{ {
int i; int i;
...@@ -574,7 +937,6 @@ static int gspca_set_default_mode(struct gspca_dev *gspca_dev) ...@@ -574,7 +937,6 @@ static int gspca_set_default_mode(struct gspca_dev *gspca_dev)
gspca_dev->width = gspca_dev->cam.cam_mode[i].width; gspca_dev->width = gspca_dev->cam.cam_mode[i].width;
gspca_dev->height = gspca_dev->cam.cam_mode[i].height; gspca_dev->height = gspca_dev->cam.cam_mode[i].height;
gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixfmt; gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixfmt;
return 0;
} }
static int wxh_to_mode(struct gspca_dev *gspca_dev, static int wxh_to_mode(struct gspca_dev *gspca_dev,
...@@ -582,46 +944,14 @@ static int wxh_to_mode(struct gspca_dev *gspca_dev, ...@@ -582,46 +944,14 @@ static int wxh_to_mode(struct gspca_dev *gspca_dev,
{ {
int i; int i;
for (i = gspca_dev->cam.nmodes - 1; --i >= 0; ) { for (i = gspca_dev->cam.nmodes; --i > 0; ) {
if (width > gspca_dev->cam.cam_mode[i].width) if (width >= gspca_dev->cam.cam_mode[i].width
&& height >= gspca_dev->cam.cam_mode[i].height)
break; break;
} }
i++;
while (i < gspca_dev->cam.nmodes - 1
&& width == gspca_dev->cam.cam_mode[i + 1].width
&& height < gspca_dev->cam.cam_mode[i + 1].height)
i++;
return i; return i;
} }
static __u32 get_v4l2_depth(__u32 pixfmt)
{
switch (pixfmt) {
case V4L2_PIX_FMT_BGR32:
case V4L2_PIX_FMT_RGB32:
return 32;
case V4L2_PIX_FMT_RGB24:
case V4L2_PIX_FMT_BGR24:
return 24;
case V4L2_PIX_FMT_RGB565:
case V4L2_PIX_FMT_YUYV: /* packed 4.2.2 */
case V4L2_PIX_FMT_YYUV:
return 16;
case V4L2_PIX_FMT_YUV420: /* planar 4.2.0 */
return 12;
case V4L2_PIX_FMT_MJPEG:
case V4L2_PIX_FMT_JPEG:
case V4L2_PIX_FMT_SBGGR8: /* Bayer */
return 8;
}
PDEBUG(D_ERR|D_CONF, "Unknown pixel format %c%c%c%c",
pixfmt & 0xff,
(pixfmt >> 8) & 0xff,
(pixfmt >> 16) & 0xff,
pixfmt >> 24);
return -EINVAL;
}
/* /*
* search a mode with the right pixel format * search a mode with the right pixel format
*/ */
...@@ -649,11 +979,15 @@ static int vidioc_enum_fmt_cap(struct file *file, void *priv, ...@@ -649,11 +979,15 @@ static int vidioc_enum_fmt_cap(struct file *file, void *priv,
struct v4l2_fmtdesc *fmtdesc) struct v4l2_fmtdesc *fmtdesc)
{ {
struct gspca_dev *gspca_dev = priv; struct gspca_dev *gspca_dev = priv;
int i, j, index; int i;
#ifndef GSPCA_HLP
int j, index;
__u32 fmt_tb[8]; __u32 fmt_tb[8];
#endif
PDEBUG(D_CONF, "enum fmt cap"); PDEBUG(D_CONF, "enum fmt cap");
#ifndef GSPCA_HLP
/* give an index to each format */ /* give an index to each format */
index = 0; index = 0;
j = 0; j = 0;
...@@ -676,10 +1010,40 @@ static int vidioc_enum_fmt_cap(struct file *file, void *priv, ...@@ -676,10 +1010,40 @@ static int vidioc_enum_fmt_cap(struct file *file, void *priv,
if (i < 0) if (i < 0)
return -EINVAL; /* no more format */ return -EINVAL; /* no more format */
fmtdesc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
fmtdesc->pixelformat = fmt_tb[index]; fmtdesc->pixelformat = fmt_tb[index];
if (gspca_is_compressed(fmt_tb[index])) if (gspca_is_compressed(fmt_tb[index]))
fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED; fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
#else /*GSPCA_HLP*/
/* !! code tied to the decoding functions in decoder.c */
i = gspca_dev->cam.nmodes - 1;
if (fmtdesc->index == 0) { /* (assume one format per subdriver) */
fmtdesc->pixelformat = gspca_dev->cam.cam_mode[i].pixfmt;
if (gspca_is_compressed(fmtdesc->pixelformat))
fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
} else {
if (hlp == 0
|| (hlp->gspca_dev != 0
&& hlp->gspca_dev != gspca_dev))
return -EINVAL;
switch (gspca_dev->cam.cam_mode[i].pixfmt) {
case V4L2_PIX_FMT_JPEG:
if (fmtdesc->index >= sizeof jpeg_to_tb
/ sizeof jpeg_to_tb[0])
return -EINVAL;
fmtdesc->pixelformat = jpeg_to_tb[fmtdesc->index];
break;
case V4L2_PIX_FMT_SBGGR8:
if (fmtdesc->index >= sizeof bayer_to_tb
/ sizeof bayer_to_tb[0])
return -EINVAL;
fmtdesc->pixelformat = bayer_to_tb[fmtdesc->index];
break;
default:
return -EINVAL;
}
}
#endif /*GSPCA_HLP*/
fmtdesc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
fmtdesc->description[0] = fmtdesc->pixelformat & 0xff; fmtdesc->description[0] = fmtdesc->pixelformat & 0xff;
fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff; fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff;
fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff; fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff;
...@@ -688,22 +1052,26 @@ static int vidioc_enum_fmt_cap(struct file *file, void *priv, ...@@ -688,22 +1052,26 @@ static int vidioc_enum_fmt_cap(struct file *file, void *priv,
return 0; return 0;
} }
static int gspca_get_buff_size(struct gspca_dev *gspca_dev)
{
unsigned int size;
size = gspca_dev->width * gspca_dev->height
* get_v4l2_depth(gspca_dev->pixfmt) / 8;
if (!size)
return -ENOMEM;
return size;
}
static int vidioc_g_fmt_cap(struct file *file, void *priv, static int vidioc_g_fmt_cap(struct file *file, void *priv,
struct v4l2_format *fmt) struct v4l2_format *fmt)
{ {
struct gspca_dev *gspca_dev = priv; struct gspca_dev *gspca_dev = priv;
#ifdef GSPCA_HLP
int i;
/* if the pixel format is not the one of the device and
* if the helper is inactive or busy, restore */
i = gspca_dev->curr_mode;
if (gspca_dev->pixfmt != gspca_dev->cam.cam_mode[i].pixfmt) {
struct hlp_dev *hlp_dev;
hlp_dev = hlp;
if (hlp_dev == 0 || hlp_dev->gspca_dev != gspca_dev)
gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixfmt;
}
#endif /*GSPCA_HLP*/
fmt->fmt.pix.width = gspca_dev->width; fmt->fmt.pix.width = gspca_dev->width;
fmt->fmt.pix.height = gspca_dev->height; fmt->fmt.pix.height = gspca_dev->height;
fmt->fmt.pix.pixelformat = gspca_dev->pixfmt; fmt->fmt.pix.pixelformat = gspca_dev->pixfmt;
...@@ -733,6 +1101,10 @@ static int try_fmt_cap(struct gspca_dev *gspca_dev, ...@@ -733,6 +1101,10 @@ static int try_fmt_cap(struct gspca_dev *gspca_dev,
w = fmt->fmt.pix.width; w = fmt->fmt.pix.width;
h = fmt->fmt.pix.height; h = fmt->fmt.pix.height;
/* (luvcview problem) */
if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_JPEG;
#ifdef GSPCA_DEBUG #ifdef GSPCA_DEBUG
if (gspca_debug & D_CONF) if (gspca_debug & D_CONF)
PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h); PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h);
...@@ -746,13 +1118,46 @@ static int try_fmt_cap(struct gspca_dev *gspca_dev, ...@@ -746,13 +1118,46 @@ static int try_fmt_cap(struct gspca_dev *gspca_dev,
/* else, search the closest mode with the same pixel format */ /* else, search the closest mode with the same pixel format */
mode2 = gspca_get_mode(gspca_dev, mode, mode2 = gspca_get_mode(gspca_dev, mode,
fmt->fmt.pix.pixelformat); fmt->fmt.pix.pixelformat);
if (mode2 >= 0) if (mode2 >= 0) {
mode = mode2; mode = mode2;
else { } else {
__u32 pixfmt;
pixfmt = gspca_dev->cam.cam_mode[mode].pixfmt;
#ifndef GSPCA_HLP
/* no chance, return this mode */ /* no chance, return this mode */
fmt->fmt.pix.pixelformat fmt->fmt.pix.pixelformat = pixfmt;
= gspca_dev->cam.cam_mode[mode].pixfmt; #else /*GSPCA_HLP*/
if (hlp != 0
&& (hlp->gspca_dev == 0
|| hlp->gspca_dev == gspca_dev)
/* decoding works for JPEG and Bayer only */
&& (pixfmt == V4L2_PIX_FMT_JPEG
|| pixfmt == V4L2_PIX_FMT_SBGGR8)) {
switch (fmt->fmt.pix.pixelformat) {
case V4L2_PIX_FMT_YUYV: /* 'YUYV' */
case V4L2_PIX_FMT_BGR24: /* 'BGR3' */
case V4L2_PIX_FMT_RGB24: /* 'RGB3' */
case V4L2_PIX_FMT_YUV420: /* 'YU12' */
case V4L2_PIX_FMT_RGB565: /* 'RGBP' */
break;
default: {
/* return any of the supported fmt's */
__u8 u;
u = get_jiffies_64();
u %= sizeof bayer_to_tb
/ sizeof bayer_to_tb[0] - 1;
fmt->fmt.pix.pixelformat =
bayer_to_tb[u + 1];
break;
}
}
} else {
fmt->fmt.pix.pixelformat = pixfmt;
}
#endif /*GSPCA_HLP*/
#ifdef GSPCA_DEBUG #ifdef GSPCA_DEBUG
if (gspca_debug & D_CONF) { if (gspca_debug & D_CONF) {
PDEBUG_MODE("new format", PDEBUG_MODE("new format",
...@@ -791,7 +1196,7 @@ static int vidioc_s_fmt_cap(struct file *file, void *priv, ...@@ -791,7 +1196,7 @@ static int vidioc_s_fmt_cap(struct file *file, void *priv,
struct v4l2_format *fmt) struct v4l2_format *fmt)
{ {
struct gspca_dev *gspca_dev = priv; struct gspca_dev *gspca_dev = priv;
int ret, was_streaming; int ret;
#ifdef GSPCA_DEBUG #ifdef GSPCA_DEBUG
if (gspca_debug & D_CONF) { if (gspca_debug & D_CONF) {
...@@ -802,32 +1207,56 @@ static int vidioc_s_fmt_cap(struct file *file, void *priv, ...@@ -802,32 +1207,56 @@ static int vidioc_s_fmt_cap(struct file *file, void *priv,
#endif #endif
if (mutex_lock_interruptible(&gspca_dev->queue_lock)) if (mutex_lock_interruptible(&gspca_dev->queue_lock))
return -ERESTARTSYS; return -ERESTARTSYS;
ret = try_fmt_cap(gspca_dev, fmt); ret = try_fmt_cap(gspca_dev, fmt);
if (ret < 0) if (ret < 0)
goto out; goto out;
if (gspca_dev->nframes != 0
&& fmt->fmt.pix.sizeimage > gspca_dev->frsz) {
ret = -EINVAL;
goto out;
}
#ifndef GSPCA_HLP
if (ret == gspca_dev->curr_mode) if (ret == gspca_dev->curr_mode)
goto out; /* same mode */ goto out; /* same mode */
was_streaming = gspca_dev->streaming; #else /*GSPCA_HLP*/
if (was_streaming) { if (ret == gspca_dev->curr_mode
if (gspca_dev->capt_file != 0 && gspca_dev->pixfmt == fmt->fmt.pix.pixelformat)
&& gspca_dev->capt_file != file) { goto out; /* same mode */
ret = -EBUSY; #endif /*GSPCA_HLP*/
goto out;
} if (gspca_dev->streaming) {
if (mutex_lock_interruptible(&gspca_dev->usb_lock)) { ret = -EBUSY;
ret = -ERESTARTSYS; goto out;
goto out;
}
gspca_stream_off(gspca_dev);
mutex_unlock(&gspca_dev->usb_lock);
} }
gspca_dev->width = fmt->fmt.pix.width; gspca_dev->width = fmt->fmt.pix.width;
gspca_dev->height = fmt->fmt.pix.height; gspca_dev->height = fmt->fmt.pix.height;
gspca_dev->pixfmt = fmt->fmt.pix.pixelformat; gspca_dev->pixfmt = fmt->fmt.pix.pixelformat;
gspca_dev->curr_mode = ret; gspca_dev->curr_mode = ret;
if (was_streaming)
ret = gspca_init_transfer(gspca_dev); #ifdef GSPCA_HLP
/* if frame decoding is required */
if (gspca_dev->pixfmt != gspca_dev->cam.cam_mode[ret].pixfmt) {
struct hlp_dev *hlp_dev;
hlp_dev = hlp;
if (hlp_dev == 0
|| (hlp_dev->gspca_dev != 0
&& hlp_dev->gspca_dev != gspca_dev)) { /* helper busy */
fmt->fmt.pix.pixelformat =
gspca_dev->pixfmt =
gspca_dev->cam.cam_mode[ret].pixfmt;
} else { /* helper active */
hlp_dev->gspca_dev = gspca_dev;
hlp_dev->pixfmt = gspca_dev->cam.cam_mode[ret].pixfmt;
hlp_dev->fr_d = gspca_dev->fr_i;
}
} else if (hlp != 0 && hlp->gspca_dev == gspca_dev)
hlp->gspca_dev = 0;
#endif /*GSPCA_HLP*/
ret = 0;
out: out:
mutex_unlock(&gspca_dev->queue_lock); mutex_unlock(&gspca_dev->queue_lock);
return ret; return ret;
...@@ -838,7 +1267,7 @@ static int dev_open(struct inode *inode, struct file *file) ...@@ -838,7 +1267,7 @@ static int dev_open(struct inode *inode, struct file *file)
struct gspca_dev *gspca_dev; struct gspca_dev *gspca_dev;
int ret; int ret;
PDEBUG(D_STREAM, "opening"); PDEBUG(D_STREAM, "%s open", current->comm);
gspca_dev = (struct gspca_dev *) video_devdata(file); gspca_dev = (struct gspca_dev *) video_devdata(file);
if (mutex_lock_interruptible(&gspca_dev->queue_lock)) if (mutex_lock_interruptible(&gspca_dev->queue_lock))
return -ERESTARTSYS; return -ERESTARTSYS;
...@@ -867,7 +1296,7 @@ static int dev_open(struct inode *inode, struct file *file) ...@@ -867,7 +1296,7 @@ static int dev_open(struct inode *inode, struct file *file)
file->private_data = gspca_dev; file->private_data = gspca_dev;
#ifdef GSPCA_DEBUG #ifdef GSPCA_DEBUG
/* activate the v4l2 debug */ /* activate the v4l2 debug */
if (gspca_debug & D_CONF) if (gspca_debug & D_V4L2)
gspca_dev->vdev.debug |= 3; gspca_dev->vdev.debug |= 3;
else else
gspca_dev->vdev.debug &= ~3; gspca_dev->vdev.debug &= ~3;
...@@ -877,7 +1306,7 @@ static int dev_open(struct inode *inode, struct file *file) ...@@ -877,7 +1306,7 @@ static int dev_open(struct inode *inode, struct file *file)
if (ret != 0) if (ret != 0)
PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret); PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret);
else else
PDEBUG(D_STREAM, "open OK"); PDEBUG(D_STREAM, "open done");
return ret; return ret;
} }
...@@ -885,7 +1314,7 @@ static int dev_close(struct inode *inode, struct file *file) ...@@ -885,7 +1314,7 @@ static int dev_close(struct inode *inode, struct file *file)
{ {
struct gspca_dev *gspca_dev = file->private_data; struct gspca_dev *gspca_dev = file->private_data;
PDEBUG(D_STREAM, "closing"); PDEBUG(D_STREAM, "%s close", current->comm);
if (mutex_lock_interruptible(&gspca_dev->queue_lock)) if (mutex_lock_interruptible(&gspca_dev->queue_lock))
return -ERESTARTSYS; return -ERESTARTSYS;
gspca_dev->users--; gspca_dev->users--;
...@@ -898,11 +1327,28 @@ static int dev_close(struct inode *inode, struct file *file) ...@@ -898,11 +1327,28 @@ static int dev_close(struct inode *inode, struct file *file)
gspca_dev->sd_desc->close(gspca_dev); gspca_dev->sd_desc->close(gspca_dev);
mutex_unlock(&gspca_dev->usb_lock); mutex_unlock(&gspca_dev->usb_lock);
frame_free(gspca_dev); frame_free(gspca_dev);
file->private_data = NULL;
gspca_dev->capt_file = 0; gspca_dev->capt_file = 0;
gspca_dev->memory = GSPCA_MEMORY_NO;
#ifdef GSPCA_HLP
{
struct hlp_dev *hlp_dev;
int mode;
hlp_dev = hlp;
if (hlp_dev != 0
&& hlp_dev->gspca_dev == gspca_dev) {
hlp_dev->gspca_dev = 0;
hlp_dev->frame = 0;
mode = gspca_dev->curr_mode;
gspca_dev->pixfmt =
gspca_dev->cam.cam_mode[mode].pixfmt;
}
}
#endif /*GSPCA_HLP*/
} }
file->private_data = NULL;
mutex_unlock(&gspca_dev->queue_lock); mutex_unlock(&gspca_dev->queue_lock);
PDEBUG(D_STREAM, "closed"); PDEBUG(D_STREAM, "close done");
return 0; return 0;
} }
...@@ -1036,32 +1482,50 @@ static int vidioc_reqbufs(struct file *file, void *priv, ...@@ -1036,32 +1482,50 @@ static int vidioc_reqbufs(struct file *file, void *priv,
struct v4l2_requestbuffers *rb) struct v4l2_requestbuffers *rb)
{ {
struct gspca_dev *gspca_dev = priv; struct gspca_dev *gspca_dev = priv;
int frsz, ret; int i, ret = 0;
PDEBUG(D_STREAM, "reqbufs %d", rb->count); PDEBUG(D_STREAM, "reqbufs %d", rb->count);
if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
return -EINVAL; return -EINVAL;
if (rb->memory != V4L2_MEMORY_MMAP switch (rb->memory) {
&& rb->memory != V4L2_MEMORY_USERPTR) case V4L2_MEMORY_MMAP:
break;
case V4L2_MEMORY_USERPTR:
#ifdef GSPCA_HLP
if (hlp == 0 || hlp->gspca_dev != gspca_dev)
break;
#endif
return -EINVAL; return -EINVAL;
if (rb->count == 0) default:
return -EINVAL; return -EINVAL;
frsz = gspca_get_buff_size(gspca_dev); }
if (frsz < 0)
return frsz;
if (mutex_lock_interruptible(&gspca_dev->queue_lock)) if (mutex_lock_interruptible(&gspca_dev->queue_lock))
return -ERESTARTSYS; return -ERESTARTSYS;
if (gspca_dev->capt_file != 0) { /* only one file may do capture */
for (i = 0; i < gspca_dev->nframes; i++) {
if (gspca_dev->frame[i].vma_use_count) {
ret = -EBUSY;
goto out;
}
}
/* only one file may do capture */
if ((gspca_dev->capt_file != 0 && gspca_dev->capt_file != file)
|| gspca_dev->streaming) {
ret = -EBUSY; ret = -EBUSY;
goto out; goto out;
} }
ret = frame_alloc(gspca_dev,
rb->count, if (rb->count == 0) { /* unrequest? */
(unsigned int) frsz, frame_free(gspca_dev);
rb->memory); gspca_dev->capt_file = 0;
if (ret == 0) { } else {
rb->count = gspca_dev->nframes; gspca_dev->memory = rb->memory;
gspca_dev->capt_file = file; ret = frame_alloc(gspca_dev, rb->count);
if (ret == 0) {
rb->count = gspca_dev->nframes;
gspca_dev->capt_file = file;
}
} }
out: out:
mutex_unlock(&gspca_dev->queue_lock); mutex_unlock(&gspca_dev->queue_lock);
...@@ -1224,12 +1688,25 @@ static int vidiocgmbuf(struct file *file, void *priv, ...@@ -1224,12 +1688,25 @@ static int vidiocgmbuf(struct file *file, void *priv,
if (gspca_dev->nframes == 0) { if (gspca_dev->nframes == 0) {
struct v4l2_requestbuffers rb; struct v4l2_requestbuffers rb;
int ret; int ret;
__u32 pixfmt;
short width, height;
/* as the final format is not yet defined, allocate
buffers with the max size */
pixfmt = gspca_dev->pixfmt;
width = gspca_dev->width;
height = gspca_dev->height;
gspca_dev->pixfmt = V4L2_PIX_FMT_BGR32;
gspca_dev->width = 640;
gspca_dev->height = 480;
memset(&rb, 0, sizeof rb); memset(&rb, 0, sizeof rb);
rb.count = 4; rb.count = 4;
rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
rb.memory = V4L2_MEMORY_MMAP; rb.memory = V4L2_MEMORY_MMAP;
ret = vidioc_reqbufs(file, priv, &rb); ret = vidioc_reqbufs(file, priv, &rb);
gspca_dev->pixfmt = pixfmt;
gspca_dev->width = width;
gspca_dev->height = height;
if (ret != 0) if (ret != 0)
return ret; return ret;
} }
...@@ -1328,43 +1805,22 @@ static int dev_mmap(struct file *file, struct vm_area_struct *vma) ...@@ -1328,43 +1805,22 @@ static int dev_mmap(struct file *file, struct vm_area_struct *vma)
return ret; return ret;
} }
static unsigned int dev_poll(struct file *file, poll_table * wait)
{
struct gspca_dev *gspca_dev = file->private_data;
int i, ret;
PDEBUG(D_FRAM, "poll");
poll_wait(file, &gspca_dev->wq, wait);
if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
return POLLERR;
if (gspca_dev->dev == 0
|| !gspca_dev->streaming) /* if not streaming */
ret = POLLERR;
else {
i = gspca_dev->fr_o;
i = gspca_dev->fr_queue[i];
if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
ret = POLLIN | POLLRDNORM; /* something to read */
else
ret = 0;
}
mutex_unlock(&gspca_dev->queue_lock);
return ret;
}
/* /*
* wait for a video frame * wait for a video frame
* *
* If a frame is ready, its index is returned. * If a frame is ready, its index is returned.
*/ */
static int gspca_frame_wait(struct gspca_dev *gspca_dev, static int frame_wait(struct gspca_dev *gspca_dev,
int nonblock_ing) int nonblock_ing)
{ {
struct gspca_frame *frame; struct gspca_frame *frame;
int i, j, ret; int i, j, ret;
/* if userptr, treat the awaiting URBs */
if (gspca_dev->memory == V4L2_MEMORY_USERPTR)
isoc_transfer(gspca_dev);
/* check if a frame is ready */
i = gspca_dev->fr_o; i = gspca_dev->fr_o;
j = gspca_dev->fr_queue[i]; j = gspca_dev->fr_queue[i];
frame = &gspca_dev->frame[j]; frame = &gspca_dev->frame[j];
...@@ -1385,13 +1841,14 @@ static int gspca_frame_wait(struct gspca_dev *gspca_dev, ...@@ -1385,13 +1841,14 @@ static int gspca_frame_wait(struct gspca_dev *gspca_dev,
} }
if (!gspca_dev->streaming || !gspca_dev->present) if (!gspca_dev->streaming || !gspca_dev->present)
return -EIO; return -EIO;
if (gspca_dev->memory == V4L2_MEMORY_USERPTR)
isoc_transfer(gspca_dev);
i = gspca_dev->fr_o; i = gspca_dev->fr_o;
j = gspca_dev->fr_queue[i]; j = gspca_dev->fr_queue[i];
frame = &gspca_dev->frame[j]; frame = &gspca_dev->frame[j];
if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
break; break;
} }
ok: ok:
atomic_dec(&gspca_dev->nevent); atomic_dec(&gspca_dev->nevent);
gspca_dev->fr_o = (i + 1) % gspca_dev->nframes; gspca_dev->fr_o = (i + 1) % gspca_dev->nframes;
...@@ -1434,7 +1891,7 @@ static int vidioc_dqbuf(struct file *file, void *priv, ...@@ -1434,7 +1891,7 @@ static int vidioc_dqbuf(struct file *file, void *priv,
if (mutex_lock_interruptible(&gspca_dev->read_lock)) if (mutex_lock_interruptible(&gspca_dev->read_lock))
return -ERESTARTSYS; return -ERESTARTSYS;
ret = gspca_frame_wait(gspca_dev, file->f_flags & O_NONBLOCK); ret = frame_wait(gspca_dev, file->f_flags & O_NONBLOCK);
if (ret < 0) if (ret < 0)
goto out; goto out;
i = ret; /* frame index */ i = ret; /* frame index */
...@@ -1467,14 +1924,14 @@ static int vidioc_qbuf(struct file *file, void *priv, ...@@ -1467,14 +1924,14 @@ static int vidioc_qbuf(struct file *file, void *priv,
index = v4l2_buf->index; index = v4l2_buf->index;
if ((unsigned) index >= gspca_dev->nframes) { if ((unsigned) index >= gspca_dev->nframes) {
PDEBUG(D_STREAM, PDEBUG(D_FRAM,
"qbuf idx %d >= %d", index, gspca_dev->nframes); "qbuf idx %d >= %d", index, gspca_dev->nframes);
return -EINVAL; return -EINVAL;
} }
frame = &gspca_dev->frame[index]; frame = &gspca_dev->frame[index];
if (v4l2_buf->memory != frame->v4l2_buf.memory) { if (v4l2_buf->memory != frame->v4l2_buf.memory) {
PDEBUG(D_STREAM, "qbuf bad memory type"); PDEBUG(D_FRAM, "qbuf bad memory type");
return -EINVAL; return -EINVAL;
} }
if (gspca_dev->capt_file != file) if (gspca_dev->capt_file != file)
...@@ -1483,17 +1940,16 @@ static int vidioc_qbuf(struct file *file, void *priv, ...@@ -1483,17 +1940,16 @@ static int vidioc_qbuf(struct file *file, void *priv,
if (mutex_lock_interruptible(&gspca_dev->queue_lock)) if (mutex_lock_interruptible(&gspca_dev->queue_lock))
return -ERESTARTSYS; return -ERESTARTSYS;
if (frame->v4l2_buf.flags if (frame->v4l2_buf.flags & BUF_ALL_FLAGS) {
& (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)) { PDEBUG(D_FRAM, "qbuf bad state");
PDEBUG(D_STREAM, "qbuf bad state");
ret = -EINVAL; ret = -EINVAL;
goto out; goto out;
} }
frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED; frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE; /* frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE; */
if (v4l2_buf->memory == V4L2_MEMORY_USERPTR) { if (frame->v4l2_buf.memory == V4L2_MEMORY_USERPTR) {
frame->data = frame->data_end = frame->data = frame->data_end =
(unsigned char *) v4l2_buf->m.userptr; (unsigned char *) v4l2_buf->m.userptr;
frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr; frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr;
...@@ -1517,16 +1973,16 @@ static int vidioc_qbuf(struct file *file, void *priv, ...@@ -1517,16 +1973,16 @@ static int vidioc_qbuf(struct file *file, void *priv,
return ret; return ret;
} }
static ssize_t dev_read(struct file *file, char __user *data, /*
size_t count, loff_t *ppos) * allocate the resources for read()
*/
static int read_alloc(struct gspca_dev *gspca_dev,
struct file *file)
{ {
struct gspca_dev *gspca_dev = file->private_data;
struct gspca_frame *frame;
struct v4l2_buffer v4l2_buf; struct v4l2_buffer v4l2_buf;
struct timeval timestamp; int i, ret;
int i, ret, ret2;
PDEBUG(D_FRAM, "read (%p, %d)", data, count); PDEBUG(D_STREAM, "read alloc");
if (gspca_dev->nframes == 0) { if (gspca_dev->nframes == 0) {
struct v4l2_requestbuffers rb; struct v4l2_requestbuffers rb;
...@@ -1536,7 +1992,7 @@ static ssize_t dev_read(struct file *file, char __user *data, ...@@ -1536,7 +1992,7 @@ static ssize_t dev_read(struct file *file, char __user *data,
rb.memory = V4L2_MEMORY_MMAP; rb.memory = V4L2_MEMORY_MMAP;
ret = vidioc_reqbufs(file, gspca_dev, &rb); ret = vidioc_reqbufs(file, gspca_dev, &rb);
if (ret != 0) { if (ret != 0) {
PDEBUG(D_STREAM, "read reqbuf err: %d", ret); PDEBUG(D_STREAM, "read reqbuf err %d", ret);
return ret; return ret;
} }
memset(&v4l2_buf, 0, sizeof v4l2_buf); memset(&v4l2_buf, 0, sizeof v4l2_buf);
...@@ -1553,18 +2009,89 @@ static ssize_t dev_read(struct file *file, char __user *data, ...@@ -1553,18 +2009,89 @@ static ssize_t dev_read(struct file *file, char __user *data,
return ret; return ret;
} }
} }
} else if (gspca_dev->capt_file != file) gspca_dev->memory = GSPCA_MEMORY_READ;
return -EINVAL; }
/* start streaming */
ret = vidioc_streamon(file, gspca_dev, V4L2_BUF_TYPE_VIDEO_CAPTURE);
if (ret != 0)
PDEBUG(D_STREAM, "read streamon err %d", ret);
return ret;
}
static unsigned int dev_poll(struct file *file, poll_table *wait)
{
struct gspca_dev *gspca_dev = file->private_data;
int i, ret;
PDEBUG(D_FRAM, "poll");
poll_wait(file, &gspca_dev->wq, wait);
if (!gspca_dev->present)
return POLLERR;
/* if not streaming, the user would use read() */
if (!gspca_dev->streaming) { if (!gspca_dev->streaming) {
ret = vidioc_streamon(file, gspca_dev, if (gspca_dev->memory != GSPCA_MEMORY_NO) {
V4L2_BUF_TYPE_VIDEO_CAPTURE); ret = POLLERR; /* not the 1st time */
goto out;
}
ret = read_alloc(gspca_dev, file);
if (ret != 0) { if (ret != 0) {
PDEBUG(D_STREAM, "read streamon err %d", ret); ret = POLLERR;
return ret; goto out;
} }
} }
if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0)
return POLLERR;
if (!gspca_dev->present) {
ret = POLLERR;
goto out;
}
/* if not mmap, treat the awaiting URBs */
if (gspca_dev->memory == V4L2_MEMORY_USERPTR
&& gspca_dev->capt_file == file)
isoc_transfer(gspca_dev);
i = gspca_dev->fr_o;
i = gspca_dev->fr_queue[i];
if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
ret = POLLIN | POLLRDNORM; /* something to read */
else
ret = 0;
out:
mutex_unlock(&gspca_dev->queue_lock);
return ret;
}
static ssize_t dev_read(struct file *file, char __user *data,
size_t count, loff_t *ppos)
{
struct gspca_dev *gspca_dev = file->private_data;
struct gspca_frame *frame;
struct v4l2_buffer v4l2_buf;
struct timeval timestamp;
int i, ret, ret2;
PDEBUG(D_FRAM, "read (%d)", count);
if (!gspca_dev->present)
return -ENODEV;
switch (gspca_dev->memory) {
case GSPCA_MEMORY_NO: /* first time */
ret = read_alloc(gspca_dev, file);
if (ret != 0)
return ret;
break;
case GSPCA_MEMORY_READ:
if (gspca_dev->capt_file != file)
return -EINVAL;
break;
default:
return -EINVAL;
}
/* get a frame */ /* get a frame */
jiffies_to_timeval(get_jiffies_64(), &timestamp); jiffies_to_timeval(get_jiffies_64(), &timestamp);
timestamp.tv_sec--; timestamp.tv_sec--;
...@@ -1615,7 +2142,7 @@ static ssize_t dev_read(struct file *file, char __user *data, ...@@ -1615,7 +2142,7 @@ static ssize_t dev_read(struct file *file, char __user *data,
return ret; return ret;
} }
static void gspca_dev_release(struct video_device *vfd) static void dev_release(struct video_device *vfd)
{ {
/* nothing */ /* nothing */
} }
...@@ -1635,7 +2162,7 @@ static struct video_device gspca_template = { ...@@ -1635,7 +2162,7 @@ static struct video_device gspca_template = {
.name = "gspca main driver", .name = "gspca main driver",
.type = VID_TYPE_CAPTURE, .type = VID_TYPE_CAPTURE,
.fops = &dev_fops, .fops = &dev_fops,
.release = gspca_dev_release, /* mandatory */ .release = dev_release, /* mandatory */
.minor = -1, .minor = -1,
.vidioc_querycap = vidioc_querycap, .vidioc_querycap = vidioc_querycap,
.vidioc_dqbuf = vidioc_dqbuf, .vidioc_dqbuf = vidioc_dqbuf,
...@@ -1673,7 +2200,8 @@ static struct video_device gspca_template = { ...@@ -1673,7 +2200,8 @@ static struct video_device gspca_template = {
int gspca_dev_probe(struct usb_interface *intf, int gspca_dev_probe(struct usb_interface *intf,
const struct usb_device_id *id, const struct usb_device_id *id,
const struct sd_desc *sd_desc, const struct sd_desc *sd_desc,
int dev_size) int dev_size,
struct module *module)
{ {
struct usb_interface_descriptor *interface; struct usb_interface_descriptor *interface;
struct gspca_dev *gspca_dev; struct gspca_dev *gspca_dev;
...@@ -1682,8 +2210,8 @@ int gspca_dev_probe(struct usb_interface *intf, ...@@ -1682,8 +2210,8 @@ int gspca_dev_probe(struct usb_interface *intf,
__u16 vendor; __u16 vendor;
__u16 product; __u16 product;
vendor = le16_to_cpu(dev->descriptor.idVendor); vendor = id->idVendor;
product = le16_to_cpu(dev->descriptor.idProduct); product = id->idProduct;
PDEBUG(D_PROBE, "probing %04x:%04x", vendor, product); PDEBUG(D_PROBE, "probing %04x:%04x", vendor, product);
/* we don't handle multi-config cameras */ /* we don't handle multi-config cameras */
...@@ -1703,6 +2231,7 @@ int gspca_dev_probe(struct usb_interface *intf, ...@@ -1703,6 +2231,7 @@ int gspca_dev_probe(struct usb_interface *intf,
} }
gspca_dev->dev = dev; gspca_dev->dev = dev;
gspca_dev->iface = interface->bInterfaceNumber; gspca_dev->iface = interface->bInterfaceNumber;
gspca_dev->nbalt = intf->num_altsetting;
gspca_dev->sd_desc = sd_desc; gspca_dev->sd_desc = sd_desc;
/* gspca_dev->users = 0; (done by kzalloc) */ /* gspca_dev->users = 0; (done by kzalloc) */
gspca_dev->nbufread = 2; gspca_dev->nbufread = 2;
...@@ -1724,6 +2253,9 @@ int gspca_dev_probe(struct usb_interface *intf, ...@@ -1724,6 +2253,9 @@ int gspca_dev_probe(struct usb_interface *intf,
/* init video stuff */ /* init video stuff */
memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template); memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template);
gspca_dev->vdev.dev = &dev->dev; gspca_dev->vdev.dev = &dev->dev;
memcpy(&gspca_dev->fops, &dev_fops, sizeof gspca_dev->fops);
gspca_dev->vdev.fops = &gspca_dev->fops;
gspca_dev->fops.owner = module; /* module protection */
ret = video_register_device(&gspca_dev->vdev, ret = video_register_device(&gspca_dev->vdev,
VFL_TYPE_GRABBER, VFL_TYPE_GRABBER,
video_nr); video_nr);
...@@ -1758,7 +2290,7 @@ void gspca_disconnect(struct usb_interface *intf) ...@@ -1758,7 +2290,7 @@ void gspca_disconnect(struct usb_interface *intf)
mutex_lock(&gspca_dev->queue_lock); mutex_lock(&gspca_dev->queue_lock);
mutex_lock(&gspca_dev->usb_lock); mutex_lock(&gspca_dev->usb_lock);
gspca_dev->streaming = 0; gspca_dev->streaming = 0;
gspca_kill_transfer(gspca_dev); destroy_urbs(gspca_dev);
mutex_unlock(&gspca_dev->usb_lock); mutex_unlock(&gspca_dev->usb_lock);
mutex_unlock(&gspca_dev->queue_lock); mutex_unlock(&gspca_dev->queue_lock);
while (gspca_dev->users != 0) { /* wait until fully closed */ while (gspca_dev->users != 0) { /* wait until fully closed */
...@@ -1777,11 +2309,23 @@ EXPORT_SYMBOL(gspca_disconnect); ...@@ -1777,11 +2309,23 @@ EXPORT_SYMBOL(gspca_disconnect);
/* -- module insert / remove -- */ /* -- module insert / remove -- */
static int __init gspca_init(void) static int __init gspca_init(void)
{ {
#ifdef GSPCA_HLP
int ret;
/* create /dev/gspca_hlp */
ret = misc_register(&hlp_device);
if (ret < 0)
err("misc_register err %d", ret);
start_hlp(); /* try to start the helper process */
#endif
info("main v%s registered", version); info("main v%s registered", version);
return 0; return 0;
} }
static void __exit gspca_exit(void) static void __exit gspca_exit(void)
{ {
#ifdef GSPCA_HLP
misc_deregister(&hlp_device);
#endif
info("main deregistered"); info("main deregistered");
} }
...@@ -1791,7 +2335,8 @@ module_exit(gspca_exit); ...@@ -1791,7 +2335,8 @@ module_exit(gspca_exit);
module_param_named(debug, gspca_debug, int, 0644); module_param_named(debug, gspca_debug, int, 0644);
MODULE_PARM_DESC(debug, MODULE_PARM_DESC(debug,
"Debug (bit) 0x01:error 0x02:probe 0x04:config" "Debug (bit) 0x01:error 0x02:probe 0x04:config"
" 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"); " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"
" 0x0100: v4l2");
module_param(comp_fac, int, 0644); module_param(comp_fac, int, 0644);
MODULE_PARM_DESC(comp_fac, MODULE_PARM_DESC(comp_fac,
......
...@@ -25,6 +25,7 @@ extern int gspca_debug; ...@@ -25,6 +25,7 @@ extern int gspca_debug;
#define D_PACK 0x20 #define D_PACK 0x20
#define D_USBI 0x40 #define D_USBI 0x40
#define D_USBO 0x80 #define D_USBO 0x80
#define D_V4L2 0x0100
#else #else
#define PDEBUG(level, fmt, args...) #define PDEBUG(level, fmt, args...)
#endif #endif
...@@ -46,9 +47,9 @@ extern int gspca_debug; ...@@ -46,9 +47,9 @@ extern int gspca_debug;
#define GSPCA_MAX_FRAMES 16 /* maximum number of video frame buffers */ #define GSPCA_MAX_FRAMES 16 /* maximum number of video frame buffers */
/* ISOC transfers */ /* ISOC transfers */
#define NURBS 4 /* number of URBs */ #define MAX_NURBS 32 /* max number of URBs (read & userptr) */
#define ISO_MAX_PKT 32 /* max number of packets in an ISOC transfer */ #define ISO_MAX_PKT 32 /* max number of packets in an ISOC transfer */
#define ISO_MAX_SIZE 0x10000 /* max size of one URB buffer (64 Kb) */ #define ISO_MAX_SIZE 0x8000 /* max size of one URB buffer (32 Kb) */
/* device information - set at probe time */ /* device information - set at probe time */
struct cam_mode { struct cam_mode {
...@@ -123,13 +124,14 @@ struct gspca_frame { ...@@ -123,13 +124,14 @@ struct gspca_frame {
struct gspca_dev { struct gspca_dev {
struct video_device vdev; /* !! must be the first item */ struct video_device vdev; /* !! must be the first item */
struct file_operations fops;
struct usb_device *dev; struct usb_device *dev;
struct file *capt_file; /* file doing video capture */ struct file *capt_file; /* file doing video capture */
struct cam cam; /* device information */ struct cam cam; /* device information */
const struct sd_desc *sd_desc; /* subdriver description */ const struct sd_desc *sd_desc; /* subdriver description */
struct urb *urb[NURBS]; struct urb *urb[MAX_NURBS];
__u8 *frbuf; /* buffer for nframes */ __u8 *frbuf; /* buffer for nframes */
struct gspca_frame frame[GSPCA_MAX_FRAMES]; struct gspca_frame frame[GSPCA_MAX_FRAMES];
...@@ -155,15 +157,21 @@ struct gspca_dev { ...@@ -155,15 +157,21 @@ struct gspca_dev {
struct mutex queue_lock; /* ISOC queue protection */ struct mutex queue_lock; /* ISOC queue protection */
__u32 sequence; /* frame sequence number */ __u32 sequence; /* frame sequence number */
char streaming; char streaming;
char users; /* # open */ char users; /* number of opens */
char present; /* device connected */ char present; /* device connected */
char nbufread; /* number of buffers for read() */ char nbufread; /* number of buffers for read() */
char nurbs; /* number of allocated URBs */
char memory; /* memory type (V4L2_MEMORY_xxx) */
__u8 urb_in; /* URB pointers - used when !mmap */
__u8 urb_out;
__u8 nbalt; /* number of USB alternate settings */
}; };
int gspca_dev_probe(struct usb_interface *intf, int gspca_dev_probe(struct usb_interface *intf,
const struct usb_device_id *id, const struct usb_device_id *id,
const struct sd_desc *sd_desc, const struct sd_desc *sd_desc,
int dev_size); int dev_size,
struct module *module);
void gspca_disconnect(struct usb_interface *intf); void gspca_disconnect(struct usb_interface *intf);
struct gspca_frame *gspca_frame_add(struct gspca_dev *gspca_dev, struct gspca_frame *gspca_frame_add(struct gspca_dev *gspca_dev,
int packet_type, int packet_type,
......
...@@ -265,7 +265,7 @@ static unsigned char eoh[] = { ...@@ -265,7 +265,7 @@ static unsigned char eoh[] = {
0x02, 0x11, 0x01, /* samples CbCr - quant CbCr */ 0x02, 0x11, 0x01, /* samples CbCr - quant CbCr */
0x03, 0x11, 0x01, 0x03, 0x11, 0x01,
0xff, 0xda, 0x00, 0x0c, /* SOS (start of scan */ 0xff, 0xda, 0x00, 0x0c, /* SOS (start of scan) */
0x03, 0x01, 0x00, 0x02, 0x11, 0x03, 0x11, 0x00, 0x3f, 0x00 0x03, 0x01, 0x00, 0x02, 0x11, 0x03, 0x11, 0x00, 0x3f, 0x00
}; };
#endif #endif
......
...@@ -27,8 +27,8 @@ ...@@ -27,8 +27,8 @@
#include "gspca.h" #include "gspca.h"
#define DRIVER_VERSION_NUMBER KERNEL_VERSION(0, 1, 1) #define DRIVER_VERSION_NUMBER KERNEL_VERSION(0, 2, 15)
static const char version[] = "0.1.1"; static const char version[] = "0.2.15";
MODULE_AUTHOR("Hans de Goede <j.w.r.degoede@hhs.nl>"); MODULE_AUTHOR("Hans de Goede <j.w.r.degoede@hhs.nl>");
MODULE_DESCRIPTION("Pixart PAC207"); MODULE_DESCRIPTION("Pixart PAC207");
...@@ -188,7 +188,8 @@ static const __u8 pac207_sensor_init[][8] = { ...@@ -188,7 +188,8 @@ static const __u8 pac207_sensor_init[][8] = {
/* 48 reg_72 Rate Control end BalSize_4a =0x36 */ /* 48 reg_72 Rate Control end BalSize_4a =0x36 */
static const __u8 PacReg72[] = { 0x00, 0x00, 0x36, 0x00 }; static const __u8 PacReg72[] = { 0x00, 0x00, 0x36, 0x00 };
static const char pac207_sof_marker[5] = { 0xff, 0xff, 0x00, 0xff, 0x96 }; static const unsigned char pac207_sof_marker[5] =
{ 0xff, 0xff, 0x00, 0xff, 0x96 };
int pac207_write_regs(struct gspca_dev *gspca_dev, u16 index, int pac207_write_regs(struct gspca_dev *gspca_dev, u16 index,
const u8 *buffer, u16 length) const u8 *buffer, u16 length)
...@@ -327,11 +328,12 @@ static void sd_start(struct gspca_dev *gspca_dev) ...@@ -327,11 +328,12 @@ static void sd_start(struct gspca_dev *gspca_dev)
pac207_write_reg(gspca_dev, 0x02, sd->exposure); /* PXCK = 12MHz /n */ pac207_write_reg(gspca_dev, 0x02, sd->exposure); /* PXCK = 12MHz /n */
mode = 0x02; /* Image Format (Bit 0), LED (1), Compr. test mode (2) */ mode = 0x02; /* Image Format (Bit 0), LED (1), Compr. test mode (2) */
if (gspca_dev->width == 176) { /* 176x144 */ if (gspca_dev->width == 176) { /* 176x144 */
mode |= 0x01; mode |= 0x01;
PDEBUG(D_STREAM, "pac207_start mode 176x144"); PDEBUG(D_STREAM, "pac207_start mode 176x144");
} else/* 352x288 */ } else { /* 352x288 */
PDEBUG(D_STREAM, "pac207_start mode 352x288"); PDEBUG(D_STREAM, "pac207_start mode 352x288");
}
pac207_write_reg(gspca_dev, 0x41, mode); pac207_write_reg(gspca_dev, 0x41, mode);
pac207_write_reg(gspca_dev, 0x13, 0x01); /* Bit 0, auto clear */ pac207_write_reg(gspca_dev, 0x13, 0x01); /* Bit 0, auto clear */
...@@ -425,7 +427,7 @@ void init_pixart_decoder(void) ...@@ -425,7 +427,7 @@ void init_pixart_decoder(void)
} }
/* auto gain and exposure algorithm based on the knee algorithm described here: /* auto gain and exposure algorithm based on the knee algorithm described here:
http://ytse.tricolour.net/docs/LowLightOptimization.html */ * <http://ytse.tricolour.net/docs/LowLightOptimization.html> */
static void pac207_do_auto_gain(struct gspca_dev *gspca_dev) static void pac207_do_auto_gain(struct gspca_dev *gspca_dev)
{ {
struct sd *sd = (struct sd *) gspca_dev; struct sd *sd = (struct sd *) gspca_dev;
...@@ -508,8 +510,9 @@ static unsigned char *pac207_find_sof(struct gspca_dev *gspca_dev, ...@@ -508,8 +510,9 @@ static unsigned char *pac207_find_sof(struct gspca_dev *gspca_dev,
sd->sof_read = 0; sd->sof_read = 0;
return m + i + 1; return m + i + 1;
} }
} else } else {
sd->sof_read = 0; sd->sof_read = 0;
}
} }
return NULL; return NULL;
...@@ -556,9 +559,9 @@ static int pac207_decompress_row(struct gspca_dev *gspca_dev, ...@@ -556,9 +559,9 @@ static int pac207_decompress_row(struct gspca_dev *gspca_dev,
decoder_state->line_read++; decoder_state->line_read++;
decoder_state->get_abs = 0; decoder_state->get_abs = 0;
} else { } else {
if (table[code].is_abs) if (table[code].is_abs) {
decoder_state->get_abs = 1; decoder_state->get_abs = 1;
else { } else {
/* relative to left pixel */ /* relative to left pixel */
val = outp[-2] + val = outp[-2] +
table[code].val; table[code].val;
...@@ -894,13 +897,13 @@ static struct sd_desc sd_desc = { ...@@ -894,13 +897,13 @@ static struct sd_desc sd_desc = {
#define DVNM(name) .driver_info = (kernel_ulong_t) name #define DVNM(name) .driver_info = (kernel_ulong_t) name
static __devinitdata struct usb_device_id device_table[] = { static __devinitdata struct usb_device_id device_table[] = {
{USB_DEVICE(0x041e, 0x4028), DVNM("Creative Webcam Vista Plus")}, {USB_DEVICE(0x041e, 0x4028), DVNM("Creative Webcam Vista Plus")},
{USB_DEVICE(0x093a, 0x2460), DVNM("PAC207 Qtec Webcam 100")}, {USB_DEVICE(0x093a, 0x2460), DVNM("Q-Tec Webcam 100")},
{USB_DEVICE(0x093a, 0x2463), DVNM("Philips spc200nc pac207")}, {USB_DEVICE(0x093a, 0x2463), DVNM("Philips spc200nc pac207")},
{USB_DEVICE(0x093a, 0x2464), DVNM("Labtec Webcam 1200")}, {USB_DEVICE(0x093a, 0x2464), DVNM("Labtec Webcam 1200")},
{USB_DEVICE(0x093a, 0x2468), DVNM("PAC207")}, {USB_DEVICE(0x093a, 0x2468), DVNM("PAC207")},
{USB_DEVICE(0x093a, 0x2470), DVNM("Genius GF112")}, {USB_DEVICE(0x093a, 0x2470), DVNM("Genius GF112")},
{USB_DEVICE(0x093a, 0x2471), DVNM("PAC207 Genius VideoCam ge111")}, {USB_DEVICE(0x093a, 0x2471), DVNM("Genius VideoCam GE111")},
{USB_DEVICE(0x093a, 0x2472), DVNM("PAC207 Genius VideoCam ge110")}, {USB_DEVICE(0x093a, 0x2472), DVNM("Genius VideoCam GE110")},
{USB_DEVICE(0x2001, 0xf115), DVNM("D-Link DSB-C120")}, {USB_DEVICE(0x2001, 0xf115), DVNM("D-Link DSB-C120")},
{} {}
}; };
...@@ -910,8 +913,8 @@ MODULE_DEVICE_TABLE(usb, device_table); ...@@ -910,8 +913,8 @@ MODULE_DEVICE_TABLE(usb, device_table);
static int sd_probe(struct usb_interface *intf, static int sd_probe(struct usb_interface *intf,
const struct usb_device_id *id) const struct usb_device_id *id)
{ {
PDEBUG(D_PROBE, "camera probe"); return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd)); THIS_MODULE);
} }
static struct usb_driver sd_driver = { static struct usb_driver sd_driver = {
......
/* /*
* Syntek DV4000 (STK014) subdriver * Syntek DV4000 (STK014) subdriver
* *
* Copyright (C) Jean-Francois Moine (http://moinejf.free.fr) * Copyright (C) 2008 Jean-Francois Moine (http://moinejf.free.fr)
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/ */
#define MODULE_NAME "stk014" #define MODULE_NAME "stk014"
...@@ -24,8 +23,8 @@ ...@@ -24,8 +23,8 @@
#include "gspca.h" #include "gspca.h"
#include "jpeg.h" #include "jpeg.h"
#define DRIVER_VERSION_NUMBER KERNEL_VERSION(0, 1, 0) #define DRIVER_VERSION_NUMBER KERNEL_VERSION(0, 2, 7)
static const char version[] = "0.1.0"; static const char version[] = "0.2.7";
MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>"); MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
MODULE_DESCRIPTION("Syntek DV4000 (STK014) USB Camera Driver"); MODULE_DESCRIPTION("Syntek DV4000 (STK014) USB Camera Driver");
...@@ -389,64 +388,32 @@ static void sd_pkt_scan(struct gspca_dev *gspca_dev, ...@@ -389,64 +388,32 @@ static void sd_pkt_scan(struct gspca_dev *gspca_dev,
unsigned char *data, /* isoc packet */ unsigned char *data, /* isoc packet */
int len) /* iso packet length */ int len) /* iso packet length */
{ {
int l;
static unsigned char ffd9[] = {0xff, 0xd9}; static unsigned char ffd9[] = {0xff, 0xd9};
/* a frame starts with: /* a frame starts with:
* - 0xff 0xfe * - 0xff 0xfe
* - 0x08 0x00 // length (little endian ?!) * - 0x08 0x00 - length (little endian ?!)
* - 4 bytes = size of whole frame (big endian - including header) * - 4 bytes = size of whole frame (BE - including header)
* - 0x00 0x0c * - 0x00 0x0c
* - 0xff 0xd8 * - 0xff 0xd8
* - .. JPEG image with escape sequences (ff 00) * - .. JPEG image with escape sequences (ff 00)
* (without ending - ff d9)
*/ */
if (data[0] == 0xff && data[1] == 0xfe) { if (data[0] == 0xff && data[1] == 0xfe) {
if (gspca_dev->last_packet_type == INTER_PACKET) { frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
PDEBUG(D_ERR|D_FRAM, "sof actual l: %d init l: %d", ffd9, 2);
frame->data_end - frame->data,
frame->v4l2_buf.bytesused);
}
/* put the JPEG headaer */ /* put the JPEG 411 header */
jpeg_put_header(gspca_dev, frame, sd_quant, 0x22); jpeg_put_header(gspca_dev, frame, sd_quant, 0x22);
/* beginning of the frame */ /* beginning of the frame */
#define STKHDRSZ 12 #define STKHDRSZ 12
l = (data[4] << 24) /* frame size */
+ (data[5] << 16)
+ (data[6] << 8)
+ data[7]
- STKHDRSZ
+ (frame->data_end - frame->data)
+ 2; /* EOF (ff d9) */
gspca_frame_add(gspca_dev, INTER_PACKET, frame, gspca_frame_add(gspca_dev, INTER_PACKET, frame,
data + STKHDRSZ, len - STKHDRSZ); data + STKHDRSZ, len - STKHDRSZ);
#undef STKHDRSZ #undef STKHDRSZ
frame->v4l2_buf.bytesused = l;
return;
}
if (gspca_dev->last_packet_type != INTER_PACKET) {
if (gspca_dev->last_packet_type == LAST_PACKET) {
PDEBUG(D_ERR|D_PACK, "mof actual l: %d init l: %d",
frame->data_end - frame->data,
frame->v4l2_buf.bytesused);
}
return;
}
/* intermediate packet */
l = frame->data_end - frame->data;
if (len < frame->v4l2_buf.bytesused - 2 - l) {
gspca_frame_add(gspca_dev, INTER_PACKET, frame,
data, len);
return; return;
} }
/* last packet */
if (len > frame->v4l2_buf.bytesused - 2 - l)
len = frame->v4l2_buf.bytesused - 2 - l;
gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len); gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
gspca_frame_add(gspca_dev, LAST_PACKET, frame, ffd9, 2);
} }
static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val) static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
...@@ -529,8 +496,8 @@ MODULE_DEVICE_TABLE(usb, device_table); ...@@ -529,8 +496,8 @@ MODULE_DEVICE_TABLE(usb, device_table);
static int sd_probe(struct usb_interface *intf, static int sd_probe(struct usb_interface *intf,
const struct usb_device_id *id) const struct usb_device_id *id)
{ {
PDEBUG(D_PROBE, "camera probe"); return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd)); THIS_MODULE);
} }
static struct usb_driver sd_driver = { static struct usb_driver sd_driver = {
......
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment