]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/media/video/stk-webcam.c
V4L/DVB (8613): v4l: move BKL down to the driver level.
[mirror_ubuntu-bionic-kernel.git] / drivers / media / video / stk-webcam.c
CommitLineData
ec16dae5
JVJ
1/*
2 * stk-webcam.c : Driver for Syntek 1125 USB webcam controller
3 *
4 * Copyright (C) 2006 Nicolas VIVIEN
5 * Copyright 2007-2008 Jaime Velasco Juan <jsagarribay@gmail.com>
6 *
7 * Some parts are inspired from cafe_ccic.c
8 * Copyright 2006-2007 Jonathan Corbet
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25#include <linux/module.h>
26#include <linux/init.h>
27#include <linux/kernel.h>
28#include <linux/errno.h>
29#include <linux/slab.h>
30#include <linux/kref.h>
31
32#include <linux/usb.h>
ef69c8e8 33#include <linux/mm.h>
387d4477 34#include <linux/vmalloc.h>
ec16dae5
JVJ
35#include <linux/videodev2.h>
36#include <media/v4l2-common.h>
35ea11ff 37#include <media/v4l2-ioctl.h>
ec16dae5
JVJ
38
39#include "stk-webcam.h"
40
41
42static int hflip = 1;
43module_param(hflip, bool, 0444);
44MODULE_PARM_DESC(hflip, "Horizontal image flip (mirror). Defaults to 1");
45
46static int vflip = 1;
47module_param(vflip, bool, 0444);
48MODULE_PARM_DESC(vflip, "Vertical image flip. Defaults to 1");
49
50static int debug;
51module_param(debug, int, 0444);
52MODULE_PARM_DESC(debug, "Debug v4l ioctls. Defaults to 0");
53
54MODULE_LICENSE("GPL");
55MODULE_AUTHOR("Jaime Velasco Juan <jsagarribay@gmail.com> and Nicolas VIVIEN");
56MODULE_DESCRIPTION("Syntek DC1125 webcam driver");
57
58
59
60/* Some cameras have audio interfaces, we aren't interested in those */
61static struct usb_device_id stkwebcam_table[] = {
62 { USB_DEVICE_AND_INTERFACE_INFO(0x174f, 0xa311, 0xff, 0xff, 0xff) },
63 { USB_DEVICE_AND_INTERFACE_INFO(0x05e1, 0x0501, 0xff, 0xff, 0xff) },
64 { }
65};
66MODULE_DEVICE_TABLE(usb, stkwebcam_table);
67
beb9e780 68static void stk_camera_cleanup(struct kref *kref)
ec16dae5
JVJ
69{
70 struct stk_camera *dev = to_stk_camera(kref);
71
72 STK_INFO("Syntek USB2.0 Camera release resources"
73 " video device /dev/video%d\n", dev->vdev.minor);
74 video_unregister_device(&dev->vdev);
75 dev->vdev.priv = NULL;
76
77 if (dev->sio_bufs != NULL || dev->isobufs != NULL)
78 STK_ERROR("We are leaking memory\n");
79 usb_put_intf(dev->interface);
80 kfree(dev);
81}
82
83
84/*
85 * Basic stuff
86 */
87int stk_camera_write_reg(struct stk_camera *dev, u16 index, u8 value)
88{
89 struct usb_device *udev = dev->udev;
90 int ret;
91
92 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
93 0x01,
94 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
95 value,
96 index,
97 NULL,
98 0,
99 500);
100 if (ret < 0)
101 return ret;
102 else
103 return 0;
104}
105
106int stk_camera_read_reg(struct stk_camera *dev, u16 index, int *value)
107{
108 struct usb_device *udev = dev->udev;
109 int ret;
110
111 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
112 0x00,
113 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
114 0x00,
115 index,
116 (u8 *) value,
117 sizeof(u8),
118 500);
119 if (ret < 0)
120 return ret;
121 else
122 return 0;
123}
124
125static int stk_start_stream(struct stk_camera *dev)
126{
127 int value;
128 int i, ret;
129 int value_116, value_117;
130
131 if (!is_present(dev))
132 return -ENODEV;
133 if (!is_memallocd(dev) || !is_initialised(dev)) {
134 STK_ERROR("FIXME: Buffers are not allocated\n");
135 return -EFAULT;
136 }
137 ret = usb_set_interface(dev->udev, 0, 5);
138
139 if (ret < 0)
140 STK_ERROR("usb_set_interface failed !\n");
141 if (stk_sensor_wakeup(dev))
142 STK_ERROR("error awaking the sensor\n");
143
144 stk_camera_read_reg(dev, 0x0116, &value_116);
145 stk_camera_read_reg(dev, 0x0117, &value_117);
146
147 stk_camera_write_reg(dev, 0x0116, 0x0000);
148 stk_camera_write_reg(dev, 0x0117, 0x0000);
149
150 stk_camera_read_reg(dev, 0x0100, &value);
151 stk_camera_write_reg(dev, 0x0100, value | 0x80);
152
153 stk_camera_write_reg(dev, 0x0116, value_116);
154 stk_camera_write_reg(dev, 0x0117, value_117);
155 for (i = 0; i < MAX_ISO_BUFS; i++) {
156 if (dev->isobufs[i].urb) {
157 ret = usb_submit_urb(dev->isobufs[i].urb, GFP_KERNEL);
158 atomic_inc(&dev->urbs_used);
159 if (ret)
160 return ret;
161 }
162 }
163 set_streaming(dev);
164 return 0;
165}
166
167static int stk_stop_stream(struct stk_camera *dev)
168{
169 int value;
170 int i;
171 if (is_present(dev)) {
172 stk_camera_read_reg(dev, 0x0100, &value);
173 stk_camera_write_reg(dev, 0x0100, value & ~0x80);
174 if (dev->isobufs != NULL) {
175 for (i = 0; i < MAX_ISO_BUFS; i++) {
176 if (dev->isobufs[i].urb)
177 usb_kill_urb(dev->isobufs[i].urb);
178 }
179 }
180 unset_streaming(dev);
181
182 if (usb_set_interface(dev->udev, 0, 0))
183 STK_ERROR("usb_set_interface failed !\n");
184 if (stk_sensor_sleep(dev))
185 STK_ERROR("error suspending the sensor\n");
186 }
187 return 0;
188}
189
190/*
191 * This seems to be the shortest init sequence we
192 * must do in order to find the sensor
193 * Bit 5 of reg. 0x0000 here is important, when reset to 0 the sensor
194 * is also reset. Maybe powers down it?
195 * Rest of values don't make a difference
196 */
197
198static struct regval stk1125_initvals[] = {
199 /*TODO: What means this sequence? */
200 {0x0000, 0x24},
201 {0x0100, 0x21},
202 {0x0002, 0x68},
203 {0x0003, 0x80},
204 {0x0005, 0x00},
205 {0x0007, 0x03},
206 {0x000d, 0x00},
207 {0x000f, 0x02},
208 {0x0300, 0x12},
209 {0x0350, 0x41},
210 {0x0351, 0x00},
211 {0x0352, 0x00},
212 {0x0353, 0x00},
213 {0x0018, 0x10},
214 {0x0019, 0x00},
215 {0x001b, 0x0e},
216 {0x001c, 0x46},
217 {0x0300, 0x80},
218 {0x001a, 0x04},
219 {0x0110, 0x00},
220 {0x0111, 0x00},
221 {0x0112, 0x00},
222 {0x0113, 0x00},
223
224 {0xffff, 0xff},
225};
226
227
228static int stk_initialise(struct stk_camera *dev)
229{
230 struct regval *rv;
231 int ret;
232 if (!is_present(dev))
233 return -ENODEV;
234 if (is_initialised(dev))
235 return 0;
236 rv = stk1125_initvals;
237 while (rv->reg != 0xffff) {
238 ret = stk_camera_write_reg(dev, rv->reg, rv->val);
239 if (ret)
240 return ret;
241 rv++;
242 }
243 if (stk_sensor_init(dev) == 0) {
244 set_initialised(dev);
245 return 0;
246 } else
247 return -1;
248}
249
ef69c8e8
MCC
250#ifdef CONFIG_VIDEO_V4L1_COMPAT
251
ec16dae5
JVJ
252/* sysfs functions */
253/*FIXME cleanup this */
254
255static ssize_t show_brightness(struct device *class,
256 struct device_attribute *attr, char *buf)
257{
258 struct video_device *vdev = to_video_device(class);
259 struct stk_camera *dev = vdev_to_camera(vdev);
260
261 return sprintf(buf, "%X\n", dev->vsettings.brightness);
262}
263
264static ssize_t store_brightness(struct device *class,
265 struct device_attribute *attr, const char *buf, size_t count)
266{
267 char *endp;
268 unsigned long value;
269 int ret;
270
271 struct video_device *vdev = to_video_device(class);
272 struct stk_camera *dev = vdev_to_camera(vdev);
273
274 value = simple_strtoul(buf, &endp, 16);
275
276 dev->vsettings.brightness = (int) value;
277
278 ret = stk_sensor_set_brightness(dev, value >> 8);
279 if (ret)
280 return ret;
281 else
282 return count;
283}
284
285static ssize_t show_hflip(struct device *class,
286 struct device_attribute *attr, char *buf)
287{
288 struct video_device *vdev = to_video_device(class);
289 struct stk_camera *dev = vdev_to_camera(vdev);
290
291 return sprintf(buf, "%d\n", dev->vsettings.hflip);
292}
293
294static ssize_t store_hflip(struct device *class,
295 struct device_attribute *attr, const char *buf, size_t count)
296{
297 struct video_device *vdev = to_video_device(class);
298 struct stk_camera *dev = vdev_to_camera(vdev);
299
300 if (strncmp(buf, "1", 1) == 0)
301 dev->vsettings.hflip = 1;
302 else if (strncmp(buf, "0", 1) == 0)
303 dev->vsettings.hflip = 0;
304 else
305 return -EINVAL;
306
307 return strlen(buf);
308}
309
310static ssize_t show_vflip(struct device *class,
311 struct device_attribute *attr, char *buf)
312{
313 struct video_device *vdev = to_video_device(class);
314 struct stk_camera *dev = vdev_to_camera(vdev);
315
316 return sprintf(buf, "%d\n", dev->vsettings.vflip);
317}
318
319static ssize_t store_vflip(struct device *class,
320 struct device_attribute *attr, const char *buf, size_t count)
321{
322 struct video_device *vdev = to_video_device(class);
323 struct stk_camera *dev = vdev_to_camera(vdev);
324
325 if (strncmp(buf, "1", 1) == 0)
326 dev->vsettings.vflip = 1;
327 else if (strncmp(buf, "0", 1) == 0)
328 dev->vsettings.vflip = 0;
329 else
330 return -EINVAL;
331
332 return strlen(buf);
333}
334
335static DEVICE_ATTR(brightness, S_IRUGO | S_IWUGO,
336 show_brightness, store_brightness);
337static DEVICE_ATTR(hflip, S_IRUGO | S_IWUGO, show_hflip, store_hflip);
338static DEVICE_ATTR(vflip, S_IRUGO | S_IWUGO, show_vflip, store_vflip);
339
340static int stk_create_sysfs_files(struct video_device *vdev)
341{
342 int ret;
343
f894dfd7
HV
344 ret = device_create_file(&vdev->dev, &dev_attr_brightness);
345 ret += device_create_file(&vdev->dev, &dev_attr_hflip);
346 ret += device_create_file(&vdev->dev, &dev_attr_vflip);
347 if (ret)
348 STK_WARNING("Could not create sysfs files\n");
ec16dae5
JVJ
349 return ret;
350}
351
352static void stk_remove_sysfs_files(struct video_device *vdev)
353{
f894dfd7
HV
354 device_remove_file(&vdev->dev, &dev_attr_brightness);
355 device_remove_file(&vdev->dev, &dev_attr_hflip);
356 device_remove_file(&vdev->dev, &dev_attr_vflip);
ec16dae5
JVJ
357}
358
ef69c8e8
MCC
359#else
360#define stk_create_sysfs_files(a)
361#define stk_remove_sysfs_files(a)
362#endif
ec16dae5
JVJ
363
364/* *********************************************** */
365/*
366 * This function is called as an URB transfert is complete (Isochronous pipe).
367 * So, the traitement is done in interrupt time, so it has be fast, not crash,
368 * and not stall. Neat.
369 */
370static void stk_isoc_handler(struct urb *urb)
371{
372 int i;
373 int ret;
374 int framelen;
375 unsigned long flags;
376
377 unsigned char *fill = NULL;
378 unsigned char *iso_buf = NULL;
379
380 struct stk_camera *dev;
381 struct stk_sio_buffer *fb;
382
383 dev = (struct stk_camera *) urb->context;
384
385 if (dev == NULL) {
386 STK_ERROR("isoc_handler called with NULL device !\n");
387 return;
388 }
389
390 if (urb->status == -ENOENT || urb->status == -ECONNRESET
391 || urb->status == -ESHUTDOWN) {
392 atomic_dec(&dev->urbs_used);
393 return;
394 }
395
396 spin_lock_irqsave(&dev->spinlock, flags);
397
398 if (urb->status != -EINPROGRESS && urb->status != 0) {
399 STK_ERROR("isoc_handler: urb->status == %d\n", urb->status);
400 goto resubmit;
401 }
402
403 if (list_empty(&dev->sio_avail)) {
404 /*FIXME Stop streaming after a while */
405 (void) (printk_ratelimit() &&
406 STK_ERROR("isoc_handler without available buffer!\n"));
407 goto resubmit;
408 }
409 fb = list_first_entry(&dev->sio_avail,
410 struct stk_sio_buffer, list);
411 fill = fb->buffer + fb->v4lbuf.bytesused;
412
413 for (i = 0; i < urb->number_of_packets; i++) {
414 if (urb->iso_frame_desc[i].status != 0) {
415 if (urb->iso_frame_desc[i].status != -EXDEV)
416 STK_ERROR("Frame %d has error %d\n", i,
417 urb->iso_frame_desc[i].status);
418 continue;
419 }
420 framelen = urb->iso_frame_desc[i].actual_length;
421 iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
422
423 if (framelen <= 4)
424 continue; /* no data */
425
426 /*
427 * we found something informational from there
428 * the isoc frames have to type of headers
429 * type1: 00 xx 00 00 or 20 xx 00 00
430 * type2: 80 xx 00 00 00 00 00 00 or a0 xx 00 00 00 00 00 00
431 * xx is a sequencer which has never been seen over 0x3f
432 * imho data written down looks like bayer, i see similarities
433 * after every 640 bytes
434 */
435 if (*iso_buf & 0x80) {
436 framelen -= 8;
437 iso_buf += 8;
438 /* This marks a new frame */
439 if (fb->v4lbuf.bytesused != 0
440 && fb->v4lbuf.bytesused != dev->frame_size) {
441 (void) (printk_ratelimit() &&
442 STK_ERROR("frame %d, "
443 "bytesused=%d, skipping\n",
444 i, fb->v4lbuf.bytesused));
445 fb->v4lbuf.bytesused = 0;
446 fill = fb->buffer;
447 } else if (fb->v4lbuf.bytesused == dev->frame_size) {
b1855907
JVJ
448 if (list_is_singular(&dev->sio_avail)) {
449 /* Always reuse the last buffer */
450 fb->v4lbuf.bytesused = 0;
451 fill = fb->buffer;
452 } else {
453 list_move_tail(dev->sio_avail.next,
454 &dev->sio_full);
455 wake_up(&dev->wait_frame);
456 fb = list_first_entry(&dev->sio_avail,
457 struct stk_sio_buffer, list);
458 fb->v4lbuf.bytesused = 0;
459 fill = fb->buffer;
ec16dae5 460 }
ec16dae5
JVJ
461 }
462 } else {
463 framelen -= 4;
464 iso_buf += 4;
465 }
466
467 /* Our buffer is full !!! */
468 if (framelen + fb->v4lbuf.bytesused > dev->frame_size) {
469 (void) (printk_ratelimit() &&
470 STK_ERROR("Frame buffer overflow, lost sync\n"));
471 /*FIXME Do something here? */
472 continue;
473 }
474 spin_unlock_irqrestore(&dev->spinlock, flags);
475 memcpy(fill, iso_buf, framelen);
476 spin_lock_irqsave(&dev->spinlock, flags);
477 fill += framelen;
478
479 /* New size of our buffer */
480 fb->v4lbuf.bytesused += framelen;
481 }
482
483resubmit:
484 spin_unlock_irqrestore(&dev->spinlock, flags);
485 urb->dev = dev->udev;
486 ret = usb_submit_urb(urb, GFP_ATOMIC);
487 if (ret != 0) {
488 STK_ERROR("Error (%d) re-submitting urb in stk_isoc_handler.\n",
489 ret);
490 }
491}
492
493/* -------------------------------------------- */
494
495static int stk_prepare_iso(struct stk_camera *dev)
496{
497 void *kbuf;
498 int i, j;
499 struct urb *urb;
500 struct usb_device *udev;
501
502 if (dev == NULL)
503 return -ENXIO;
504 udev = dev->udev;
505
506 if (dev->isobufs)
507 STK_ERROR("isobufs already allocated. Bad\n");
508 else
509 dev->isobufs = kzalloc(MAX_ISO_BUFS * sizeof(*dev->isobufs),
510 GFP_KERNEL);
511 if (dev->isobufs == NULL) {
512 STK_ERROR("Unable to allocate iso buffers\n");
513 return -ENOMEM;
514 }
515 for (i = 0; i < MAX_ISO_BUFS; i++) {
516 if (dev->isobufs[i].data == NULL) {
517 kbuf = kzalloc(ISO_BUFFER_SIZE, GFP_KERNEL);
518 if (kbuf == NULL) {
519 STK_ERROR("Failed to allocate iso buffer %d\n",
520 i);
521 goto isobufs_out;
522 }
523 dev->isobufs[i].data = kbuf;
524 } else
525 STK_ERROR("isobuf data already allocated\n");
526 if (dev->isobufs[i].urb == NULL) {
527 urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
528 if (urb == NULL) {
529 STK_ERROR("Failed to allocate URB %d\n", i);
530 goto isobufs_out;
531 }
532 dev->isobufs[i].urb = urb;
533 } else {
534 STK_ERROR("Killing URB\n");
535 usb_kill_urb(dev->isobufs[i].urb);
536 urb = dev->isobufs[i].urb;
537 }
538 urb->interval = 1;
539 urb->dev = udev;
540 urb->pipe = usb_rcvisocpipe(udev, dev->isoc_ep);
541 urb->transfer_flags = URB_ISO_ASAP;
542 urb->transfer_buffer = dev->isobufs[i].data;
543 urb->transfer_buffer_length = ISO_BUFFER_SIZE;
544 urb->complete = stk_isoc_handler;
545 urb->context = dev;
546 urb->start_frame = 0;
547 urb->number_of_packets = ISO_FRAMES_PER_DESC;
548
549 for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
550 urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
551 urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE;
552 }
553 }
554 set_memallocd(dev);
555 return 0;
556
557isobufs_out:
558 for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].data; i++)
559 kfree(dev->isobufs[i].data);
560 for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].urb; i++)
561 usb_free_urb(dev->isobufs[i].urb);
562 kfree(dev->isobufs);
563 dev->isobufs = NULL;
564 return -ENOMEM;
565}
566
567static void stk_clean_iso(struct stk_camera *dev)
568{
569 int i;
570
571 if (dev == NULL || dev->isobufs == NULL)
572 return;
573
574 for (i = 0; i < MAX_ISO_BUFS; i++) {
575 struct urb *urb;
576
577 urb = dev->isobufs[i].urb;
578 if (urb) {
579 if (atomic_read(&dev->urbs_used))
580 usb_kill_urb(urb);
581 usb_free_urb(urb);
582 }
583 kfree(dev->isobufs[i].data);
584 }
585 kfree(dev->isobufs);
586 dev->isobufs = NULL;
587 unset_memallocd(dev);
588}
589
590static int stk_setup_siobuf(struct stk_camera *dev, int index)
591{
592 struct stk_sio_buffer *buf = dev->sio_bufs + index;
593 INIT_LIST_HEAD(&buf->list);
594 buf->v4lbuf.length = PAGE_ALIGN(dev->frame_size);
595 buf->buffer = vmalloc_user(buf->v4lbuf.length);
596 if (buf->buffer == NULL)
597 return -ENOMEM;
598 buf->mapcount = 0;
599 buf->dev = dev;
600 buf->v4lbuf.index = index;
601 buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
602 buf->v4lbuf.field = V4L2_FIELD_NONE;
603 buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
604 buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
605 return 0;
606}
607
608static int stk_free_sio_buffers(struct stk_camera *dev)
609{
610 int i;
611 int nbufs;
612 unsigned long flags;
613 if (dev->n_sbufs == 0 || dev->sio_bufs == NULL)
614 return 0;
615 /*
616 * If any buffers are mapped, we cannot free them at all.
617 */
618 for (i = 0; i < dev->n_sbufs; i++) {
619 if (dev->sio_bufs[i].mapcount > 0)
620 return -EBUSY;
621 }
622 /*
623 * OK, let's do it.
624 */
625 spin_lock_irqsave(&dev->spinlock, flags);
626 INIT_LIST_HEAD(&dev->sio_avail);
627 INIT_LIST_HEAD(&dev->sio_full);
628 nbufs = dev->n_sbufs;
629 dev->n_sbufs = 0;
630 spin_unlock_irqrestore(&dev->spinlock, flags);
631 for (i = 0; i < nbufs; i++) {
632 if (dev->sio_bufs[i].buffer != NULL)
633 vfree(dev->sio_bufs[i].buffer);
634 }
635 kfree(dev->sio_bufs);
636 dev->sio_bufs = NULL;
637 return 0;
638}
639
640static int stk_prepare_sio_buffers(struct stk_camera *dev, unsigned n_sbufs)
641{
642 int i;
643 if (dev->sio_bufs != NULL)
644 STK_ERROR("sio_bufs already allocated\n");
645 else {
646 dev->sio_bufs = kzalloc(n_sbufs * sizeof(struct stk_sio_buffer),
647 GFP_KERNEL);
648 if (dev->sio_bufs == NULL)
649 return -ENOMEM;
650 for (i = 0; i < n_sbufs; i++) {
651 if (stk_setup_siobuf(dev, i))
652 return (dev->n_sbufs > 1)? 0 : -ENOMEM;
653 dev->n_sbufs = i+1;
654 }
655 }
656 return 0;
657}
658
659static int stk_allocate_buffers(struct stk_camera *dev, unsigned n_sbufs)
660{
661 int err;
662 err = stk_prepare_iso(dev);
663 if (err) {
664 stk_clean_iso(dev);
665 return err;
666 }
667 err = stk_prepare_sio_buffers(dev, n_sbufs);
668 if (err) {
669 stk_free_sio_buffers(dev);
670 return err;
671 }
672 return 0;
673}
674
675static void stk_free_buffers(struct stk_camera *dev)
676{
677 stk_clean_iso(dev);
678 stk_free_sio_buffers(dev);
679}
680/* -------------------------------------------- */
681
682/* v4l file operations */
683
684static int v4l_stk_open(struct inode *inode, struct file *fp)
685{
686 struct stk_camera *dev;
687 struct video_device *vdev;
688
689 vdev = video_devdata(fp);
690 dev = vdev_to_camera(vdev);
691
d56dc612
HV
692 lock_kernel();
693 if (dev == NULL || !is_present(dev)) {
694 unlock_kernel();
ec16dae5 695 return -ENXIO;
d56dc612 696 }
ec16dae5
JVJ
697 fp->private_data = vdev;
698 kref_get(&dev->kref);
1fdd61c0 699 usb_autopm_get_interface(dev->interface);
d56dc612 700 unlock_kernel();
ec16dae5
JVJ
701
702 return 0;
703}
704
705static int v4l_stk_release(struct inode *inode, struct file *fp)
706{
707 struct stk_camera *dev;
708 struct video_device *vdev;
709
710 vdev = video_devdata(fp);
711 if (vdev == NULL) {
712 STK_ERROR("v4l_release called w/o video devdata\n");
713 return -EFAULT;
714 }
715 dev = vdev_to_camera(vdev);
716 if (dev == NULL) {
717 STK_ERROR("v4l_release called on removed device\n");
718 return -ENODEV;
719 }
720
721 if (dev->owner != fp) {
1fdd61c0 722 usb_autopm_put_interface(dev->interface);
ec16dae5
JVJ
723 kref_put(&dev->kref, stk_camera_cleanup);
724 return 0;
725 }
726
727 stk_stop_stream(dev);
728
729 stk_free_buffers(dev);
730
731 dev->owner = NULL;
732
1fdd61c0 733 usb_autopm_put_interface(dev->interface);
ec16dae5
JVJ
734 kref_put(&dev->kref, stk_camera_cleanup);
735
736 return 0;
737}
738
739static ssize_t v4l_stk_read(struct file *fp, char __user *buf,
740 size_t count, loff_t *f_pos)
741{
742 int i;
743 int ret;
744 unsigned long flags;
745 struct stk_camera *dev;
746 struct video_device *vdev;
747 struct stk_sio_buffer *sbuf;
748
749 vdev = video_devdata(fp);
750 if (vdev == NULL)
751 return -EFAULT;
752 dev = vdev_to_camera(vdev);
753
754 if (dev == NULL)
755 return -EIO;
756
757 if (!is_present(dev))
758 return -EIO;
759 if (dev->owner && dev->owner != fp)
760 return -EBUSY;
761 dev->owner = fp;
762 if (!is_streaming(dev)) {
763 if (stk_initialise(dev)
764 || stk_allocate_buffers(dev, 3)
765 || stk_start_stream(dev))
766 return -ENOMEM;
767 spin_lock_irqsave(&dev->spinlock, flags);
768 for (i = 0; i < dev->n_sbufs; i++) {
769 list_add_tail(&dev->sio_bufs[i].list, &dev->sio_avail);
770 dev->sio_bufs[i].v4lbuf.flags = V4L2_BUF_FLAG_QUEUED;
771 }
772 spin_unlock_irqrestore(&dev->spinlock, flags);
773 }
774 if (*f_pos == 0) {
775 if (fp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
776 return -EWOULDBLOCK;
777 ret = wait_event_interruptible(dev->wait_frame,
778 !list_empty(&dev->sio_full) || !is_present(dev));
779 if (ret)
780 return ret;
781 if (!is_present(dev))
782 return -EIO;
783 }
784 if (count + *f_pos > dev->frame_size)
785 count = dev->frame_size - *f_pos;
786 spin_lock_irqsave(&dev->spinlock, flags);
787 if (list_empty(&dev->sio_full)) {
788 spin_unlock_irqrestore(&dev->spinlock, flags);
789 STK_ERROR("BUG: No siobufs ready\n");
790 return 0;
791 }
792 sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
793 spin_unlock_irqrestore(&dev->spinlock, flags);
794
795 if (copy_to_user(buf, sbuf->buffer + *f_pos, count))
796 return -EFAULT;
797
798 *f_pos += count;
799
800 if (*f_pos >= dev->frame_size) {
801 *f_pos = 0;
802 spin_lock_irqsave(&dev->spinlock, flags);
803 list_move_tail(&sbuf->list, &dev->sio_avail);
804 spin_unlock_irqrestore(&dev->spinlock, flags);
805 }
806 return count;
807}
808
809static unsigned int v4l_stk_poll(struct file *fp, poll_table *wait)
810{
811 struct stk_camera *dev;
812 struct video_device *vdev;
813
814 vdev = video_devdata(fp);
815
816 if (vdev == NULL)
817 return -EFAULT;
818
819 dev = vdev_to_camera(vdev);
820 if (dev == NULL)
821 return -ENODEV;
822
823 poll_wait(fp, &dev->wait_frame, wait);
824
825 if (!is_present(dev))
826 return POLLERR;
827
828 if (!list_empty(&dev->sio_full))
829 return (POLLIN | POLLRDNORM);
830
831 return 0;
832}
833
834
835static void stk_v4l_vm_open(struct vm_area_struct *vma)
836{
837 struct stk_sio_buffer *sbuf = vma->vm_private_data;
838 sbuf->mapcount++;
839}
840static void stk_v4l_vm_close(struct vm_area_struct *vma)
841{
842 struct stk_sio_buffer *sbuf = vma->vm_private_data;
843 sbuf->mapcount--;
844 if (sbuf->mapcount == 0)
845 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
846}
847static struct vm_operations_struct stk_v4l_vm_ops = {
848 .open = stk_v4l_vm_open,
849 .close = stk_v4l_vm_close
850};
851
852static int v4l_stk_mmap(struct file *fp, struct vm_area_struct *vma)
853{
854 unsigned int i;
855 int ret;
856 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
857 struct stk_camera *dev;
858 struct video_device *vdev;
859 struct stk_sio_buffer *sbuf = NULL;
860
861 if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED))
862 return -EINVAL;
863
864 vdev = video_devdata(fp);
865 dev = vdev_to_camera(vdev);
866
867 for (i = 0; i < dev->n_sbufs; i++) {
868 if (dev->sio_bufs[i].v4lbuf.m.offset == offset) {
869 sbuf = dev->sio_bufs + i;
870 break;
871 }
872 }
873 if (sbuf == NULL)
874 return -EINVAL;
875 ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
876 if (ret)
877 return ret;
878 vma->vm_flags |= VM_DONTEXPAND;
879 vma->vm_private_data = sbuf;
880 vma->vm_ops = &stk_v4l_vm_ops;
881 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
882 stk_v4l_vm_open(vma);
883 return 0;
884}
885
886/* v4l ioctl handlers */
887
888static int stk_vidioc_querycap(struct file *filp,
889 void *priv, struct v4l2_capability *cap)
890{
891 strcpy(cap->driver, "stk");
892 strcpy(cap->card, "stk");
893 cap->version = DRIVER_VERSION_NUM;
894
895 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
896 | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
897 return 0;
898}
899
900static int stk_vidioc_enum_input(struct file *filp,
901 void *priv, struct v4l2_input *input)
902{
903 if (input->index != 0)
904 return -EINVAL;
905
906 strcpy(input->name, "Syntek USB Camera");
907 input->type = V4L2_INPUT_TYPE_CAMERA;
908 return 0;
909}
910
911
912static int stk_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
913{
914 *i = 0;
915 return 0;
916}
917
918static int stk_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
919{
920 if (i != 0)
921 return -EINVAL;
922 else
923 return 0;
924}
925
926/* from vivi.c */
927static int stk_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
928{
929 return 0;
930}
931
932/* List of all V4Lv2 controls supported by the driver */
933static struct v4l2_queryctrl stk_controls[] = {
934 {
935 .id = V4L2_CID_BRIGHTNESS,
936 .type = V4L2_CTRL_TYPE_INTEGER,
937 .name = "Brightness",
938 .minimum = 0,
939 .maximum = 0xffff,
940 .step = 0x0100,
941 .default_value = 0x6000,
942 },
943 /*TODO: get more controls to work */
944};
945
946static int stk_vidioc_queryctrl(struct file *filp,
947 void *priv, struct v4l2_queryctrl *c)
948{
949 int i;
950 int nbr;
951 nbr = ARRAY_SIZE(stk_controls);
952
953 for (i = 0; i < nbr; i++) {
954 if (stk_controls[i].id == c->id) {
955 memcpy(c, &stk_controls[i],
956 sizeof(struct v4l2_queryctrl));
957 return 0;
958 }
959 }
960 return -EINVAL;
961}
962
963static int stk_vidioc_g_ctrl(struct file *filp,
964 void *priv, struct v4l2_control *c)
965{
966 struct stk_camera *dev = priv;
967 switch (c->id) {
968 case V4L2_CID_BRIGHTNESS:
969 c->value = dev->vsettings.brightness;
970 break;
971 default:
972 return -EINVAL;
973 }
974 return 0;
975}
976
977static int stk_vidioc_s_ctrl(struct file *filp,
978 void *priv, struct v4l2_control *c)
979{
980 struct stk_camera *dev = priv;
981 switch (c->id) {
982 case V4L2_CID_BRIGHTNESS:
983 dev->vsettings.brightness = c->value;
984 return stk_sensor_set_brightness(dev, c->value >> 8);
985 default:
986 return -EINVAL;
987 }
988 return 0;
989}
990
991
78b526a4 992static int stk_vidioc_enum_fmt_vid_cap(struct file *filp,
ec16dae5
JVJ
993 void *priv, struct v4l2_fmtdesc *fmtd)
994{
995 fmtd->flags = 0;
996
997 switch (fmtd->index) {
998 case 0:
999 fmtd->pixelformat = V4L2_PIX_FMT_RGB565;
1000 strcpy(fmtd->description, "r5g6b5");
1001 break;
1002 case 1:
1003 fmtd->pixelformat = V4L2_PIX_FMT_RGB565X;
1004 strcpy(fmtd->description, "r5g6b5BE");
1005 break;
1006 case 2:
1007 fmtd->pixelformat = V4L2_PIX_FMT_UYVY;
1008 strcpy(fmtd->description, "yuv4:2:2");
1009 break;
1010 case 3:
1011 fmtd->pixelformat = V4L2_PIX_FMT_SBGGR8;
1012 strcpy(fmtd->description, "Raw bayer");
1013 break;
1112fb68
JVJ
1014 case 4:
1015 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1016 strcpy(fmtd->description, "yuv4:2:2");
1017 break;
ec16dae5
JVJ
1018 default:
1019 return -EINVAL;
1020 }
1021 return 0;
1022}
1023
1024static struct stk_size {
1025 unsigned w;
1026 unsigned h;
1027 enum stk_mode m;
1028} stk_sizes[] = {
1029 { .w = 1280, .h = 1024, .m = MODE_SXGA, },
1030 { .w = 640, .h = 480, .m = MODE_VGA, },
1031 { .w = 352, .h = 288, .m = MODE_CIF, },
1032 { .w = 320, .h = 240, .m = MODE_QVGA, },
1033 { .w = 176, .h = 144, .m = MODE_QCIF, },
1034};
1035
78b526a4 1036static int stk_vidioc_g_fmt_vid_cap(struct file *filp,
ec16dae5
JVJ
1037 void *priv, struct v4l2_format *f)
1038{
1039 struct v4l2_pix_format *pix_format = &f->fmt.pix;
1040 struct stk_camera *dev = priv;
1041 int i;
1042
1043 for (i = 0; i < ARRAY_SIZE(stk_sizes)
1044 && stk_sizes[i].m != dev->vsettings.mode;
1045 i++);
1046 if (i == ARRAY_SIZE(stk_sizes)) {
1047 STK_ERROR("ERROR: mode invalid\n");
1048 return -EINVAL;
1049 }
1050 pix_format->width = stk_sizes[i].w;
1051 pix_format->height = stk_sizes[i].h;
1052 pix_format->field = V4L2_FIELD_NONE;
1053 pix_format->colorspace = V4L2_COLORSPACE_SRGB;
1054 pix_format->priv = 0;
1055 pix_format->pixelformat = dev->vsettings.palette;
1056 if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
1057 pix_format->bytesperline = pix_format->width;
1058 else
1059 pix_format->bytesperline = 2 * pix_format->width;
1060 pix_format->sizeimage = pix_format->bytesperline
1061 * pix_format->height;
1062 return 0;
1063}
1064
78b526a4 1065static int stk_vidioc_try_fmt_vid_cap(struct file *filp,
ec16dae5
JVJ
1066 void *priv, struct v4l2_format *fmtd)
1067{
1068 int i;
1069 switch (fmtd->fmt.pix.pixelformat) {
1070 case V4L2_PIX_FMT_RGB565:
1071 case V4L2_PIX_FMT_RGB565X:
1072 case V4L2_PIX_FMT_UYVY:
1112fb68 1073 case V4L2_PIX_FMT_YUYV:
ec16dae5
JVJ
1074 case V4L2_PIX_FMT_SBGGR8:
1075 break;
1076 default:
1077 return -EINVAL;
1078 }
1079 for (i = 1; i < ARRAY_SIZE(stk_sizes); i++) {
1080 if (fmtd->fmt.pix.width > stk_sizes[i].w)
1081 break;
1082 }
1083 if (i == ARRAY_SIZE(stk_sizes)
1084 || (abs(fmtd->fmt.pix.width - stk_sizes[i-1].w)
1085 < abs(fmtd->fmt.pix.width - stk_sizes[i].w))) {
1086 fmtd->fmt.pix.height = stk_sizes[i-1].h;
1087 fmtd->fmt.pix.width = stk_sizes[i-1].w;
1088 fmtd->fmt.pix.priv = i - 1;
1089 } else {
1090 fmtd->fmt.pix.height = stk_sizes[i].h;
1091 fmtd->fmt.pix.width = stk_sizes[i].w;
1092 fmtd->fmt.pix.priv = i;
1093 }
1094
1095 fmtd->fmt.pix.field = V4L2_FIELD_NONE;
1096 fmtd->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
1097 if (fmtd->fmt.pix.pixelformat == V4L2_PIX_FMT_SBGGR8)
1098 fmtd->fmt.pix.bytesperline = fmtd->fmt.pix.width;
1099 else
1100 fmtd->fmt.pix.bytesperline = 2 * fmtd->fmt.pix.width;
1101 fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.bytesperline
1102 * fmtd->fmt.pix.height;
1103 return 0;
1104}
1105
1fdd61c0
JVJ
1106static int stk_setup_format(struct stk_camera *dev)
1107{
1108 int i = 0;
1109 int depth;
1110 if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
1111 depth = 1;
1112 else
1113 depth = 2;
1114 while (stk_sizes[i].m != dev->vsettings.mode
1115 && i < ARRAY_SIZE(stk_sizes))
1116 i++;
1117 if (i == ARRAY_SIZE(stk_sizes)) {
7e28adb2 1118 STK_ERROR("Something is broken in %s\n", __func__);
1fdd61c0
JVJ
1119 return -EFAULT;
1120 }
1121 /* This registers controls some timings, not sure of what. */
1122 stk_camera_write_reg(dev, 0x001b, 0x0e);
1123 if (dev->vsettings.mode == MODE_SXGA)
1124 stk_camera_write_reg(dev, 0x001c, 0x0e);
1125 else
1126 stk_camera_write_reg(dev, 0x001c, 0x46);
1127 /*
1128 * Registers 0x0115 0x0114 are the size of each line (bytes),
1129 * regs 0x0117 0x0116 are the heigth of the image.
1130 */
1131 stk_camera_write_reg(dev, 0x0115,
1132 ((stk_sizes[i].w * depth) >> 8) & 0xff);
1133 stk_camera_write_reg(dev, 0x0114,
1134 (stk_sizes[i].w * depth) & 0xff);
1135 stk_camera_write_reg(dev, 0x0117,
1136 (stk_sizes[i].h >> 8) & 0xff);
1137 stk_camera_write_reg(dev, 0x0116,
1138 stk_sizes[i].h & 0xff);
1139 return stk_sensor_configure(dev);
1140}
1141
78b526a4 1142static int stk_vidioc_s_fmt_vid_cap(struct file *filp,
ec16dae5
JVJ
1143 void *priv, struct v4l2_format *fmtd)
1144{
1145 int ret;
1146 struct stk_camera *dev = priv;
1147
1148 if (dev == NULL)
1149 return -ENODEV;
1150 if (!is_present(dev))
1151 return -ENODEV;
1152 if (is_streaming(dev))
1153 return -EBUSY;
1154 if (dev->owner && dev->owner != filp)
1155 return -EBUSY;
78b526a4 1156 ret = stk_vidioc_try_fmt_vid_cap(filp, priv, fmtd);
ec16dae5
JVJ
1157 if (ret)
1158 return ret;
1fdd61c0 1159 dev->owner = filp;
ec16dae5
JVJ
1160
1161 dev->vsettings.palette = fmtd->fmt.pix.pixelformat;
1162 stk_free_buffers(dev);
1163 dev->frame_size = fmtd->fmt.pix.sizeimage;
1164 dev->vsettings.mode = stk_sizes[fmtd->fmt.pix.priv].m;
1165
1166 stk_initialise(dev);
1fdd61c0 1167 return stk_setup_format(dev);
ec16dae5
JVJ
1168}
1169
1170static int stk_vidioc_reqbufs(struct file *filp,
1171 void *priv, struct v4l2_requestbuffers *rb)
1172{
1173 struct stk_camera *dev = priv;
1174
1175 if (dev == NULL)
1176 return -ENODEV;
1177 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1178 return -EINVAL;
1179 if (rb->memory != V4L2_MEMORY_MMAP)
1180 return -EINVAL;
1181 if (is_streaming(dev)
1182 || (dev->owner && dev->owner != filp))
1183 return -EBUSY;
1184 dev->owner = filp;
1185
1186 /*FIXME If they ask for zero, we must stop streaming and free */
1187 if (rb->count < 3)
1188 rb->count = 3;
1189 /* Arbitrary limit */
1190 else if (rb->count > 5)
1191 rb->count = 5;
1192
1193 stk_allocate_buffers(dev, rb->count);
1194 rb->count = dev->n_sbufs;
1195 return 0;
1196}
1197
1198static int stk_vidioc_querybuf(struct file *filp,
1199 void *priv, struct v4l2_buffer *buf)
1200{
1201 int index;
1202 struct stk_camera *dev = priv;
1203 struct stk_sio_buffer *sbuf;
1204
1205 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1206 return -EINVAL;
1207
1208 index = buf->index;
1209
1210 if (index < 0 || index >= dev->n_sbufs)
1211 return -EINVAL;
1212 sbuf = dev->sio_bufs + buf->index;
1213 *buf = sbuf->v4lbuf;
1214 return 0;
1215}
1216
1217static int stk_vidioc_qbuf(struct file *filp,
1218 void *priv, struct v4l2_buffer *buf)
1219{
1220 struct stk_camera *dev = priv;
1221 struct stk_sio_buffer *sbuf;
1222 unsigned long flags;
1223 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1224 return -EINVAL;
1225
1226 if (buf->memory != V4L2_MEMORY_MMAP)
1227 return -EINVAL;
1228
1229 if (buf->index < 0 || buf->index >= dev->n_sbufs)
1230 return -EINVAL;
1231 sbuf = dev->sio_bufs + buf->index;
1232 if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED)
1233 return 0;
1234 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
1235 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
1236 spin_lock_irqsave(&dev->spinlock, flags);
1237 list_add_tail(&sbuf->list, &dev->sio_avail);
1238 *buf = sbuf->v4lbuf;
1239 spin_unlock_irqrestore(&dev->spinlock, flags);
1240 return 0;
1241}
1242
1243static int stk_vidioc_dqbuf(struct file *filp,
1244 void *priv, struct v4l2_buffer *buf)
1245{
1246 struct stk_camera *dev = priv;
1247 struct stk_sio_buffer *sbuf;
1248 unsigned long flags;
1249 int ret;
1250
1251 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1252 || !is_streaming(dev))
1253 return -EINVAL;
1254
1255 if (filp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
1256 return -EWOULDBLOCK;
1257 ret = wait_event_interruptible(dev->wait_frame,
1258 !list_empty(&dev->sio_full) || !is_present(dev));
1259 if (ret)
1260 return ret;
1261 if (!is_present(dev))
1262 return -EIO;
1263
1264 spin_lock_irqsave(&dev->spinlock, flags);
1265 sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
1266 list_del_init(&sbuf->list);
1267 spin_unlock_irqrestore(&dev->spinlock, flags);
1268 sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1269 sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
1270 sbuf->v4lbuf.sequence = ++dev->sequence;
1271 do_gettimeofday(&sbuf->v4lbuf.timestamp);
1272
1273 *buf = sbuf->v4lbuf;
1274 return 0;
1275}
1276
1277static int stk_vidioc_streamon(struct file *filp,
1278 void *priv, enum v4l2_buf_type type)
1279{
1280 struct stk_camera *dev = priv;
1281 if (is_streaming(dev))
1282 return 0;
1283 if (dev->sio_bufs == NULL)
1284 return -EINVAL;
1285 dev->sequence = 0;
1286 return stk_start_stream(dev);
1287}
1288
1289static int stk_vidioc_streamoff(struct file *filp,
1290 void *priv, enum v4l2_buf_type type)
1291{
1292 struct stk_camera *dev = priv;
1293 unsigned long flags;
1294 int i;
1295 stk_stop_stream(dev);
1296 spin_lock_irqsave(&dev->spinlock, flags);
1297 INIT_LIST_HEAD(&dev->sio_avail);
1298 INIT_LIST_HEAD(&dev->sio_full);
1299 for (i = 0; i < dev->n_sbufs; i++) {
1300 INIT_LIST_HEAD(&dev->sio_bufs[i].list);
1301 dev->sio_bufs[i].v4lbuf.flags = 0;
1302 }
1303 spin_unlock_irqrestore(&dev->spinlock, flags);
1304 return 0;
1305}
1306
1307
1308static int stk_vidioc_g_parm(struct file *filp,
1309 void *priv, struct v4l2_streamparm *sp)
1310{
1311 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1312 return -EINVAL;
1313
1314 sp->parm.capture.capability = 0;
1315 sp->parm.capture.capturemode = 0;
1316 /*FIXME This is not correct */
1317 sp->parm.capture.timeperframe.numerator = 1;
1318 sp->parm.capture.timeperframe.denominator = 30;
1319 sp->parm.capture.readbuffers = 2;
1320 sp->parm.capture.extendedmode = 0;
1321 return 0;
1322}
1323
1324static struct file_operations v4l_stk_fops = {
1325 .owner = THIS_MODULE,
1326 .open = v4l_stk_open,
1327 .release = v4l_stk_release,
1328 .read = v4l_stk_read,
1329 .poll = v4l_stk_poll,
1330 .mmap = v4l_stk_mmap,
1331 .ioctl = video_ioctl2,
2de3a5a5
JVJ
1332#ifdef CONFIG_COMPAT
1333 .compat_ioctl = v4l_compat_ioctl32,
1334#endif
ec16dae5
JVJ
1335 .llseek = no_llseek
1336};
1337
a399810c 1338static const struct v4l2_ioctl_ops v4l_stk_ioctl_ops = {
ec16dae5 1339 .vidioc_querycap = stk_vidioc_querycap,
78b526a4
HV
1340 .vidioc_enum_fmt_vid_cap = stk_vidioc_enum_fmt_vid_cap,
1341 .vidioc_try_fmt_vid_cap = stk_vidioc_try_fmt_vid_cap,
1342 .vidioc_s_fmt_vid_cap = stk_vidioc_s_fmt_vid_cap,
1343 .vidioc_g_fmt_vid_cap = stk_vidioc_g_fmt_vid_cap,
ec16dae5
JVJ
1344 .vidioc_enum_input = stk_vidioc_enum_input,
1345 .vidioc_s_input = stk_vidioc_s_input,
1346 .vidioc_g_input = stk_vidioc_g_input,
1347 .vidioc_s_std = stk_vidioc_s_std,
1348 .vidioc_reqbufs = stk_vidioc_reqbufs,
1349 .vidioc_querybuf = stk_vidioc_querybuf,
1350 .vidioc_qbuf = stk_vidioc_qbuf,
1351 .vidioc_dqbuf = stk_vidioc_dqbuf,
1352 .vidioc_streamon = stk_vidioc_streamon,
1353 .vidioc_streamoff = stk_vidioc_streamoff,
1354 .vidioc_queryctrl = stk_vidioc_queryctrl,
1355 .vidioc_g_ctrl = stk_vidioc_g_ctrl,
1356 .vidioc_s_ctrl = stk_vidioc_s_ctrl,
1357 .vidioc_g_parm = stk_vidioc_g_parm,
1358};
1359
a399810c
HV
1360static void stk_v4l_dev_release(struct video_device *vd)
1361{
1362}
1363
1364static struct video_device stk_v4l_data = {
1365 .name = "stkwebcam",
a399810c
HV
1366 .minor = -1,
1367 .tvnorms = V4L2_STD_UNKNOWN,
1368 .current_norm = V4L2_STD_UNKNOWN,
1369 .fops = &v4l_stk_fops,
1370 .ioctl_ops = &v4l_stk_ioctl_ops,
1371 .release = stk_v4l_dev_release,
1372};
1373
ec16dae5
JVJ
1374
1375static int stk_register_video_device(struct stk_camera *dev)
1376{
1377 int err;
1378
1379 dev->vdev = stk_v4l_data;
1380 dev->vdev.debug = debug;
5e85e732 1381 dev->vdev.parent = &dev->interface->dev;
ec16dae5
JVJ
1382 dev->vdev.priv = dev;
1383 err = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
1384 if (err)
1385 STK_ERROR("v4l registration failed\n");
1386 else
1387 STK_INFO("Syntek USB2.0 Camera is now controlling video device"
1388 " /dev/video%d\n", dev->vdev.minor);
1389 return err;
1390}
1391
1392
1393/* USB Stuff */
1394
1395static int stk_camera_probe(struct usb_interface *interface,
1396 const struct usb_device_id *id)
1397{
1398 int i;
1399 int err;
1400
1401 struct stk_camera *dev = NULL;
1402 struct usb_device *udev = interface_to_usbdev(interface);
1403 struct usb_host_interface *iface_desc;
1404 struct usb_endpoint_descriptor *endpoint;
1405
1406 dev = kzalloc(sizeof(struct stk_camera), GFP_KERNEL);
1407 if (dev == NULL) {
1408 STK_ERROR("Out of memory !\n");
1409 return -ENOMEM;
1410 }
1411
1412 kref_init(&dev->kref);
1413 spin_lock_init(&dev->spinlock);
1414 init_waitqueue_head(&dev->wait_frame);
1415
1416 dev->udev = udev;
1417 dev->interface = interface;
1418 usb_get_intf(interface);
1419
1420 dev->vsettings.vflip = vflip;
1421 dev->vsettings.hflip = hflip;
1422 dev->n_sbufs = 0;
1423 set_present(dev);
1424
1425 /* Set up the endpoint information
1426 * use only the first isoc-in endpoint
1427 * for the current alternate setting */
1428 iface_desc = interface->cur_altsetting;
1429
1430 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1431 endpoint = &iface_desc->endpoint[i].desc;
1432
1433 if (!dev->isoc_ep
1434 && ((endpoint->bEndpointAddress
1435 & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
1436 && ((endpoint->bmAttributes
1437 & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_ISOC)) {
1438 /* we found an isoc in endpoint */
1439 dev->isoc_ep = (endpoint->bEndpointAddress & 0xF);
1440 break;
1441 }
1442 }
1443 if (!dev->isoc_ep) {
1444 STK_ERROR("Could not find isoc-in endpoint");
1445 kref_put(&dev->kref, stk_camera_cleanup);
1446 return -ENODEV;
1447 }
1448 dev->vsettings.brightness = 0x7fff;
1449 dev->vsettings.palette = V4L2_PIX_FMT_RGB565;
1450 dev->vsettings.mode = MODE_VGA;
1112fb68 1451 dev->frame_size = 640 * 480 * 2;
ec16dae5
JVJ
1452
1453 INIT_LIST_HEAD(&dev->sio_avail);
1454 INIT_LIST_HEAD(&dev->sio_full);
1455
1456 usb_set_intfdata(interface, dev);
1457
1458 err = stk_register_video_device(dev);
1459 if (err) {
1460 kref_put(&dev->kref, stk_camera_cleanup);
1461 return err;
1462 }
1463
1464 stk_create_sysfs_files(&dev->vdev);
1fdd61c0 1465 usb_autopm_enable(dev->interface);
ec16dae5
JVJ
1466
1467 return 0;
1468}
1469
1470static void stk_camera_disconnect(struct usb_interface *interface)
1471{
1472 struct stk_camera *dev = usb_get_intfdata(interface);
1473
1474 usb_set_intfdata(interface, NULL);
1475 unset_present(dev);
1476
1477 wake_up_interruptible(&dev->wait_frame);
1478 stk_remove_sysfs_files(&dev->vdev);
1479
1480 kref_put(&dev->kref, stk_camera_cleanup);
1481}
1482
1fdd61c0 1483#ifdef CONFIG_PM
4d34dccd 1484static int stk_camera_suspend(struct usb_interface *intf, pm_message_t message)
1fdd61c0
JVJ
1485{
1486 struct stk_camera *dev = usb_get_intfdata(intf);
1487 if (is_streaming(dev)) {
1488 stk_stop_stream(dev);
1489 /* yes, this is ugly */
1490 set_streaming(dev);
1491 }
1492 return 0;
1493}
1494
4d34dccd 1495static int stk_camera_resume(struct usb_interface *intf)
1fdd61c0
JVJ
1496{
1497 struct stk_camera *dev = usb_get_intfdata(intf);
1498 if (!is_initialised(dev))
1499 return 0;
1500 unset_initialised(dev);
1501 stk_initialise(dev);
1502 stk_setup_format(dev);
1503 if (is_streaming(dev))
1504 stk_start_stream(dev);
1505 return 0;
1506}
1507#endif
1508
ec16dae5
JVJ
1509static struct usb_driver stk_camera_driver = {
1510 .name = "stkwebcam",
1511 .probe = stk_camera_probe,
1512 .disconnect = stk_camera_disconnect,
1513 .id_table = stkwebcam_table,
1fdd61c0
JVJ
1514#ifdef CONFIG_PM
1515 .suspend = stk_camera_suspend,
1516 .resume = stk_camera_resume,
1517#endif
ec16dae5
JVJ
1518};
1519
1520
1521static int __init stk_camera_init(void)
1522{
1523 int result;
1524
1525 result = usb_register(&stk_camera_driver);
1526 if (result)
1527 STK_ERROR("usb_register failed ! Error number %d\n", result);
1528
1529
1530 return result;
1531}
1532
1533static void __exit stk_camera_exit(void)
1534{
1535 usb_deregister(&stk_camera_driver);
1536}
1537
1538module_init(stk_camera_init);
1539module_exit(stk_camera_exit);
1540
1541