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