25 #include <linux/module.h>
27 #include <linux/kernel.h>
28 #include <linux/errno.h>
29 #include <linux/slab.h>
34 #include <linux/videodev2.h>
63 { USB_DEVICE_AND_INTERFACE_INFO(0x174f, 0xa311, 0xff, 0xff, 0xff) },
64 { USB_DEVICE_AND_INTERFACE_INFO(0x05e1, 0x0501, 0xff, 0xff, 0xff) },
114 int value_116, value_117;
119 STK_ERROR(
"FIXME: Buffers are not allocated\n");
125 STK_ERROR(
"usb_set_interface failed !\n");
152 static int stk_stop_stream(
struct stk_camera *dev)
168 STK_ERROR(
"usb_set_interface failed !\n");
170 STK_ERROR(
"error suspending the sensor\n");
183 static struct regval stk1125_initvals[] = {
213 static int stk_initialise(
struct stk_camera *dev)
221 rv = stk1125_initvals;
222 while (rv->
reg != 0xffff) {
241 static void stk_isoc_handler(
struct urb *
urb)
249 unsigned char *iso_buf =
NULL;
257 STK_ERROR(
"isoc_handler called with NULL device !\n");
269 if (urb->status != -
EINPROGRESS && urb->status != 0) {
270 STK_ERROR(
"isoc_handler: urb->status == %d\n", urb->status);
276 (
void) (printk_ratelimit() &&
277 STK_ERROR(
"isoc_handler without available buffer!\n"));
284 for (i = 0; i < urb->number_of_packets; i++) {
285 if (urb->iso_frame_desc[i].status != 0) {
286 if (urb->iso_frame_desc[i].status != -
EXDEV)
288 urb->iso_frame_desc[i].status);
291 framelen = urb->iso_frame_desc[
i].actual_length;
292 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[
i].offset;
306 if (*iso_buf & 0x80) {
310 if (fb->
v4lbuf.bytesused != 0
312 (
void) (printk_ratelimit() &&
314 "bytesused=%d, skipping\n",
315 i, fb->
v4lbuf.bytesused));
340 (
void) (printk_ratelimit() &&
341 STK_ERROR(
"Frame buffer overflow, lost sync\n"));
345 spin_unlock_irqrestore(&dev->
spinlock, flags);
346 memcpy(fill, iso_buf, framelen);
351 fb->
v4lbuf.bytesused += framelen;
355 spin_unlock_irqrestore(&dev->
spinlock, flags);
356 urb->dev = dev->
udev;
359 STK_ERROR(
"Error (%d) re-submitting urb in stk_isoc_handler.\n",
366 static int stk_prepare_iso(
struct stk_camera *dev)
371 struct usb_device *
udev;
378 STK_ERROR(
"isobufs already allocated. Bad\n");
383 STK_ERROR(
"Unable to allocate iso buffers\n");
390 STK_ERROR(
"Failed to allocate iso buffer %d\n",
396 STK_ERROR(
"isobuf data already allocated\n");
400 STK_ERROR(
"Failed to allocate URB %d\n", i);
411 urb->pipe = usb_rcvisocpipe(udev, dev->
isoc_ep);
412 urb->transfer_flags = URB_ISO_ASAP;
413 urb->transfer_buffer = dev->
isobufs[
i].data;
415 urb->complete = stk_isoc_handler;
417 urb->start_frame = 0;
429 for (i = 0; i < MAX_ISO_BUFS && dev->
isobufs[
i].data; i++)
431 for (i = 0; i < MAX_ISO_BUFS && dev->
isobufs[
i].urb; i++)
438 static void stk_clean_iso(
struct stk_camera *dev)
464 INIT_LIST_HEAD(&buf->
list);
479 static int stk_free_sio_buffers(
struct stk_camera *dev)
489 for (i = 0; i < dev->
n_sbufs; i++) {
501 spin_unlock_irqrestore(&dev->
spinlock, flags);
502 for (i = 0; i < nbufs; i++) {
511 static int stk_prepare_sio_buffers(
struct stk_camera *dev,
unsigned n_sbufs)
515 STK_ERROR(
"sio_bufs already allocated\n");
521 for (i = 0; i < n_sbufs; i++) {
522 if (stk_setup_siobuf(dev, i))
530 static int stk_allocate_buffers(
struct stk_camera *dev,
unsigned n_sbufs)
533 err = stk_prepare_iso(dev);
538 err = stk_prepare_sio_buffers(dev, n_sbufs);
540 stk_free_sio_buffers(dev);
546 static void stk_free_buffers(
struct stk_camera *dev)
549 stk_free_sio_buffers(dev);
555 static int v4l_stk_open(
struct file *
fp)
572 usb_autopm_get_interface(dev->
interface);
577 static int v4l_stk_release(
struct file *fp)
581 if (dev->
owner == fp) {
582 stk_stop_stream(dev);
583 stk_free_buffers(dev);
590 usb_autopm_put_interface(dev->
interface);
595 static ssize_t v4l_stk_read(
struct file *fp,
char __user *buf,
596 size_t count, loff_t *f_pos)
610 if (stk_initialise(dev)
611 || stk_allocate_buffers(dev, 3)
612 || stk_start_stream(dev))
615 for (i = 0; i < dev->
n_sbufs; i++) {
619 spin_unlock_irqrestore(&dev->
spinlock, flags);
635 spin_unlock_irqrestore(&dev->
spinlock, flags);
640 spin_unlock_irqrestore(&dev->
spinlock, flags);
651 spin_unlock_irqrestore(&dev->
spinlock, flags);
684 static const struct vm_operations_struct stk_v4l_vm_ops = {
685 .open = stk_v4l_vm_open,
686 .close = stk_v4l_vm_close
700 for (i = 0; i < dev->
n_sbufs; i++) {
701 if (dev->
sio_bufs[i].v4lbuf.m.offset == offset) {
713 vma->
vm_ops = &stk_v4l_vm_ops;
715 stk_v4l_vm_open(vma);
721 static int stk_vidioc_querycap(
struct file *filp,
733 static int stk_vidioc_enum_input(
struct file *filp,
736 if (input->
index != 0)
745 static int stk_vidioc_g_input(
struct file *filp,
void *priv,
unsigned int *i)
751 static int stk_vidioc_s_input(
struct file *filp,
void *priv,
unsigned int i)
770 .name =
"Brightness",
774 .default_value = 0x6000,
779 .name =
"Horizontal Flip",
788 .name =
"Vertical Flip",
796 static int stk_vidioc_queryctrl(
struct file *filp,
803 for (i = 0; i < nbr; i++) {
804 if (stk_controls[i].
id == c->
id) {
805 memcpy(c, &stk_controls[i],
813 static int stk_vidioc_g_ctrl(
struct file *filp,
833 static int stk_vidioc_s_ctrl(
struct file *filp,
854 static int stk_vidioc_enum_fmt_vid_cap(
struct file *filp,
857 switch (fmtd->
index) {
884 static struct stk_size {
889 { .w = 1280, .h = 1024, .m =
MODE_SXGA, },
890 { .w = 640, .h = 480, .m =
MODE_VGA, },
891 { .w = 352, .h = 288, .m =
MODE_CIF, },
896 static int stk_vidioc_g_fmt_vid_cap(
struct file *filp,
910 pix_format->
width = stk_sizes[
i].w;
911 pix_format->
height = stk_sizes[
i].h;
924 static int stk_vidioc_try_fmt_vid_cap(
struct file *filp,
928 switch (fmtd->
fmt.
pix.pixelformat) {
939 if (fmtd->
fmt.
pix.width > stk_sizes[i].w)
943 || (
abs(fmtd->
fmt.
pix.width - stk_sizes[i-1].w)
944 <
abs(fmtd->
fmt.
pix.width - stk_sizes[i].w))) {
945 fmtd->
fmt.
pix.height = stk_sizes[i-1].h;
946 fmtd->
fmt.
pix.width = stk_sizes[i-1].w;
947 fmtd->
fmt.
pix.priv = i - 1;
949 fmtd->
fmt.
pix.height = stk_sizes[
i].h;
950 fmtd->
fmt.
pix.width = stk_sizes[
i].w;
965 static int stk_setup_format(
struct stk_camera *dev)
977 STK_ERROR(
"Something is broken in %s\n", __func__);
991 ((stk_sizes[i].
w * depth) >> 8) & 0xff);
993 (stk_sizes[i].
w * depth) & 0xff);
995 (stk_sizes[i].
h >> 8) & 0xff);
997 stk_sizes[i].
h & 0xff);
1001 static int stk_vidioc_s_fmt_vid_cap(
struct file *filp,
1015 ret = stk_vidioc_try_fmt_vid_cap(filp, priv, fmtd);
1021 stk_free_buffers(dev);
1025 stk_initialise(dev);
1026 return stk_setup_format(dev);
1029 static int stk_vidioc_reqbufs(
struct file *filp,
1047 else if (rb->
count > 5)
1050 stk_allocate_buffers(dev, rb->
count);
1055 static int stk_vidioc_querybuf(
struct file *filp,
1068 static int stk_vidioc_qbuf(
struct file *filp,
1073 unsigned long flags;
1088 spin_unlock_irqrestore(&dev->
spinlock, flags);
1092 static int stk_vidioc_dqbuf(
struct file *filp,
1097 unsigned long flags;
1114 list_del_init(&sbuf->
list);
1115 spin_unlock_irqrestore(&dev->
spinlock, flags);
1125 static int stk_vidioc_streamon(
struct file *filp,
1134 return stk_start_stream(dev);
1137 static int stk_vidioc_streamoff(
struct file *filp,
1141 unsigned long flags;
1143 stk_stop_stream(dev);
1147 for (i = 0; i < dev->
n_sbufs; i++) {
1148 INIT_LIST_HEAD(&dev->
sio_bufs[i].list);
1151 spin_unlock_irqrestore(&dev->
spinlock, flags);
1156 static int stk_vidioc_g_parm(
struct file *filp,
1166 static int stk_vidioc_enum_framesizes(
struct file *filp,
1187 .open = v4l_stk_open,
1188 .release = v4l_stk_release,
1189 .read = v4l_stk_read,
1190 .poll = v4l_stk_poll,
1191 .mmap = v4l_stk_mmap,
1196 .vidioc_querycap = stk_vidioc_querycap,
1197 .vidioc_enum_fmt_vid_cap = stk_vidioc_enum_fmt_vid_cap,
1198 .vidioc_try_fmt_vid_cap = stk_vidioc_try_fmt_vid_cap,
1199 .vidioc_s_fmt_vid_cap = stk_vidioc_s_fmt_vid_cap,
1200 .vidioc_g_fmt_vid_cap = stk_vidioc_g_fmt_vid_cap,
1201 .vidioc_enum_input = stk_vidioc_enum_input,
1202 .vidioc_s_input = stk_vidioc_s_input,
1203 .vidioc_g_input = stk_vidioc_g_input,
1204 .vidioc_s_std = stk_vidioc_s_std,
1205 .vidioc_reqbufs = stk_vidioc_reqbufs,
1206 .vidioc_querybuf = stk_vidioc_querybuf,
1207 .vidioc_qbuf = stk_vidioc_qbuf,
1208 .vidioc_dqbuf = stk_vidioc_dqbuf,
1209 .vidioc_streamon = stk_vidioc_streamon,
1210 .vidioc_streamoff = stk_vidioc_streamoff,
1211 .vidioc_queryctrl = stk_vidioc_queryctrl,
1212 .vidioc_g_ctrl = stk_vidioc_g_ctrl,
1213 .vidioc_s_ctrl = stk_vidioc_s_ctrl,
1214 .vidioc_g_parm = stk_vidioc_g_parm,
1215 .vidioc_enum_framesizes = stk_vidioc_enum_framesizes,
1218 static void stk_v4l_dev_release(
struct video_device *vd)
1229 .name =
"stkwebcam",
1232 .fops = &v4l_stk_fops,
1233 .ioctl_ops = &v4l_stk_ioctl_ops,
1234 .release = stk_v4l_dev_release,
1238 static int stk_register_video_device(
struct stk_camera *dev)
1242 dev->
vdev = stk_v4l_data;
1249 STK_INFO(
"Syntek USB2.0 Camera is now controlling device %s\n",
1250 video_device_node_name(&dev->
vdev));
1264 struct usb_device *udev = interface_to_usbdev(interface);
1265 struct usb_host_interface *iface_desc;
1289 iface_desc = interface->cur_altsetting;
1291 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++
i) {
1292 endpoint = &iface_desc->endpoint[
i].desc;
1295 && usb_endpoint_is_isoc_in(endpoint)) {
1297 dev->
isoc_ep = usb_endpoint_num(endpoint);
1302 STK_ERROR(
"Could not find isoc-in endpoint");
1314 usb_set_intfdata(interface, dev);
1316 err = stk_register_video_device(dev);
1327 static void stk_camera_disconnect(
struct usb_interface *interface)
1329 struct stk_camera *dev = usb_get_intfdata(interface);
1331 usb_set_intfdata(interface,
NULL);
1336 STK_INFO(
"Syntek USB2.0 Camera release resources device %s\n",
1337 video_device_node_name(&dev->
vdev));
1345 struct stk_camera *dev = usb_get_intfdata(intf);
1347 stk_stop_stream(dev);
1356 struct stk_camera *dev = usb_get_intfdata(intf);
1360 stk_initialise(dev);
1362 stk_setup_format(dev);
1364 stk_start_stream(dev);
1369 static struct usb_driver stk_camera_driver = {
1370 .name =
"stkwebcam",
1371 .probe = stk_camera_probe,
1372 .disconnect = stk_camera_disconnect,
1373 .id_table = stkwebcam_table,
1375 .suspend = stk_camera_suspend,
1376 .resume = stk_camera_resume,