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