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