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