diff options
author | Mauro Carvalho Chehab <mchehab@redhat.com> | 2012-08-13 23:13:22 -0400 |
---|---|---|
committer | Mauro Carvalho Chehab <mchehab@redhat.com> | 2012-08-15 15:25:07 -0400 |
commit | 0c0d06cac63ee327ceaab4b5ffe2206574ab86bd (patch) | |
tree | e759f0dc3185d97f2a0c6b5cd5e32ea6faa74d40 /drivers/media/usb/stk1160/stk1160-v4l.c | |
parent | 84cfe9e79bd5ac11c963f4841158454fefa872f6 (diff) |
[media] rename most media/video usb drivers to media/usb
Rename all USB drivers with their own directory under
drivers/media/video into drivers/media/usb and update the
building system.
Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
Diffstat (limited to 'drivers/media/usb/stk1160/stk1160-v4l.c')
-rw-r--r-- | drivers/media/usb/stk1160/stk1160-v4l.c | 738 |
1 files changed, 738 insertions, 0 deletions
diff --git a/drivers/media/usb/stk1160/stk1160-v4l.c b/drivers/media/usb/stk1160/stk1160-v4l.c new file mode 100644 index 000000000000..360bdbee4270 --- /dev/null +++ b/drivers/media/usb/stk1160/stk1160-v4l.c | |||
@@ -0,0 +1,738 @@ | |||
1 | /* | ||
2 | * STK1160 driver | ||
3 | * | ||
4 | * Copyright (C) 2012 Ezequiel Garcia | ||
5 | * <elezegarcia--a.t--gmail.com> | ||
6 | * | ||
7 | * Based on Easycap driver by R.M. Thomas | ||
8 | * Copyright (C) 2010 R.M. Thomas | ||
9 | * <rmthomas--a.t--sciolus.org> | ||
10 | * | ||
11 | * This program is free software; you can redistribute it and/or modify | ||
12 | * it under the terms of the GNU General Public License as published by | ||
13 | * the Free Software Foundation; either version 2 of the License, or | ||
14 | * (at your option) any later version. | ||
15 | * | ||
16 | * This program is distributed in the hope that it will be useful, | ||
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
19 | * GNU General Public License for more details. | ||
20 | * | ||
21 | */ | ||
22 | |||
23 | #include <linux/module.h> | ||
24 | #include <linux/usb.h> | ||
25 | #include <linux/mm.h> | ||
26 | #include <linux/slab.h> | ||
27 | |||
28 | #include <linux/videodev2.h> | ||
29 | #include <media/v4l2-device.h> | ||
30 | #include <media/v4l2-common.h> | ||
31 | #include <media/v4l2-ioctl.h> | ||
32 | #include <media/v4l2-fh.h> | ||
33 | #include <media/v4l2-event.h> | ||
34 | #include <media/v4l2-chip-ident.h> | ||
35 | #include <media/videobuf2-vmalloc.h> | ||
36 | |||
37 | #include <media/saa7115.h> | ||
38 | |||
39 | #include "stk1160.h" | ||
40 | #include "stk1160-reg.h" | ||
41 | |||
42 | static unsigned int vidioc_debug; | ||
43 | module_param(vidioc_debug, int, 0644); | ||
44 | MODULE_PARM_DESC(vidioc_debug, "enable debug messages [vidioc]"); | ||
45 | |||
46 | static bool keep_buffers; | ||
47 | module_param(keep_buffers, bool, 0644); | ||
48 | MODULE_PARM_DESC(keep_buffers, "don't release buffers upon stop streaming"); | ||
49 | |||
50 | /* supported video standards */ | ||
51 | static struct stk1160_fmt format[] = { | ||
52 | { | ||
53 | .name = "16 bpp YUY2, 4:2:2, packed", | ||
54 | .fourcc = V4L2_PIX_FMT_UYVY, | ||
55 | .depth = 16, | ||
56 | } | ||
57 | }; | ||
58 | |||
59 | static void stk1160_set_std(struct stk1160 *dev) | ||
60 | { | ||
61 | int i; | ||
62 | |||
63 | static struct regval std525[] = { | ||
64 | |||
65 | /* 720x480 */ | ||
66 | |||
67 | /* Frame start */ | ||
68 | {STK116_CFSPO_STX_L, 0x0000}, | ||
69 | {STK116_CFSPO_STX_H, 0x0000}, | ||
70 | {STK116_CFSPO_STY_L, 0x0003}, | ||
71 | {STK116_CFSPO_STY_H, 0x0000}, | ||
72 | |||
73 | /* Frame end */ | ||
74 | {STK116_CFEPO_ENX_L, 0x05a0}, | ||
75 | {STK116_CFEPO_ENX_H, 0x0005}, | ||
76 | {STK116_CFEPO_ENY_L, 0x00f3}, | ||
77 | {STK116_CFEPO_ENY_H, 0x0000}, | ||
78 | |||
79 | {0xffff, 0xffff} | ||
80 | }; | ||
81 | |||
82 | static struct regval std625[] = { | ||
83 | |||
84 | /* 720x576 */ | ||
85 | |||
86 | /* TODO: Each line of frame has some junk at the end */ | ||
87 | /* Frame start */ | ||
88 | {STK116_CFSPO, 0x0000}, | ||
89 | {STK116_CFSPO+1, 0x0000}, | ||
90 | {STK116_CFSPO+2, 0x0001}, | ||
91 | {STK116_CFSPO+3, 0x0000}, | ||
92 | |||
93 | /* Frame end */ | ||
94 | {STK116_CFEPO, 0x05a0}, | ||
95 | {STK116_CFEPO+1, 0x0005}, | ||
96 | {STK116_CFEPO+2, 0x0121}, | ||
97 | {STK116_CFEPO+3, 0x0001}, | ||
98 | |||
99 | {0xffff, 0xffff} | ||
100 | }; | ||
101 | |||
102 | if (dev->norm & V4L2_STD_525_60) { | ||
103 | stk1160_dbg("registers to NTSC like standard\n"); | ||
104 | for (i = 0; std525[i].reg != 0xffff; i++) | ||
105 | stk1160_write_reg(dev, std525[i].reg, std525[i].val); | ||
106 | } else { | ||
107 | stk1160_dbg("registers to PAL like standard\n"); | ||
108 | for (i = 0; std625[i].reg != 0xffff; i++) | ||
109 | stk1160_write_reg(dev, std625[i].reg, std625[i].val); | ||
110 | } | ||
111 | |||
112 | } | ||
113 | |||
114 | /* | ||
115 | * Set a new alternate setting. | ||
116 | * Returns true is dev->max_pkt_size has changed, false otherwise. | ||
117 | */ | ||
118 | static bool stk1160_set_alternate(struct stk1160 *dev) | ||
119 | { | ||
120 | int i, prev_alt = dev->alt; | ||
121 | unsigned int min_pkt_size; | ||
122 | bool new_pkt_size; | ||
123 | |||
124 | /* | ||
125 | * If we don't set right alternate, | ||
126 | * then we will get a green screen with junk. | ||
127 | */ | ||
128 | min_pkt_size = STK1160_MIN_PKT_SIZE; | ||
129 | |||
130 | for (i = 0; i < dev->num_alt; i++) { | ||
131 | /* stop when the selected alt setting offers enough bandwidth */ | ||
132 | if (dev->alt_max_pkt_size[i] >= min_pkt_size) { | ||
133 | dev->alt = i; | ||
134 | break; | ||
135 | /* | ||
136 | * otherwise make sure that we end up with the maximum bandwidth | ||
137 | * because the min_pkt_size equation might be wrong... | ||
138 | */ | ||
139 | } else if (dev->alt_max_pkt_size[i] > | ||
140 | dev->alt_max_pkt_size[dev->alt]) | ||
141 | dev->alt = i; | ||
142 | } | ||
143 | |||
144 | stk1160_info("setting alternate %d\n", dev->alt); | ||
145 | |||
146 | if (dev->alt != prev_alt) { | ||
147 | stk1160_dbg("minimum isoc packet size: %u (alt=%d)\n", | ||
148 | min_pkt_size, dev->alt); | ||
149 | stk1160_dbg("setting alt %d with wMaxPacketSize=%u\n", | ||
150 | dev->alt, dev->alt_max_pkt_size[dev->alt]); | ||
151 | usb_set_interface(dev->udev, 0, dev->alt); | ||
152 | } | ||
153 | |||
154 | new_pkt_size = dev->max_pkt_size != dev->alt_max_pkt_size[dev->alt]; | ||
155 | dev->max_pkt_size = dev->alt_max_pkt_size[dev->alt]; | ||
156 | |||
157 | return new_pkt_size; | ||
158 | } | ||
159 | |||
160 | static int stk1160_start_streaming(struct stk1160 *dev) | ||
161 | { | ||
162 | int i, rc; | ||
163 | bool new_pkt_size; | ||
164 | |||
165 | /* Check device presence */ | ||
166 | if (!dev->udev) | ||
167 | return -ENODEV; | ||
168 | |||
169 | if (mutex_lock_interruptible(&dev->v4l_lock)) | ||
170 | return -ERESTARTSYS; | ||
171 | /* | ||
172 | * For some reason it is mandatory to set alternate *first* | ||
173 | * and only *then* initialize isoc urbs. | ||
174 | * Someone please explain me why ;) | ||
175 | */ | ||
176 | new_pkt_size = stk1160_set_alternate(dev); | ||
177 | |||
178 | /* | ||
179 | * We (re)allocate isoc urbs if: | ||
180 | * there is no allocated isoc urbs, OR | ||
181 | * a new dev->max_pkt_size is detected | ||
182 | */ | ||
183 | if (!dev->isoc_ctl.num_bufs || new_pkt_size) { | ||
184 | rc = stk1160_alloc_isoc(dev); | ||
185 | if (rc < 0) | ||
186 | return rc; | ||
187 | } | ||
188 | |||
189 | /* submit urbs and enables IRQ */ | ||
190 | for (i = 0; i < dev->isoc_ctl.num_bufs; i++) { | ||
191 | rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_KERNEL); | ||
192 | if (rc) { | ||
193 | stk1160_err("cannot submit urb[%d] (%d)\n", i, rc); | ||
194 | stk1160_uninit_isoc(dev); | ||
195 | return rc; | ||
196 | } | ||
197 | } | ||
198 | |||
199 | /* Start saa711x */ | ||
200 | v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 1); | ||
201 | |||
202 | /* Start stk1160 */ | ||
203 | stk1160_write_reg(dev, STK1160_DCTRL, 0xb3); | ||
204 | stk1160_write_reg(dev, STK1160_DCTRL+3, 0x00); | ||
205 | |||
206 | stk1160_dbg("streaming started\n"); | ||
207 | |||
208 | mutex_unlock(&dev->v4l_lock); | ||
209 | |||
210 | return 0; | ||
211 | } | ||
212 | |||
213 | /* Must be called with v4l_lock hold */ | ||
214 | static void stk1160_stop_hw(struct stk1160 *dev) | ||
215 | { | ||
216 | /* If the device is not physically present, there is nothing to do */ | ||
217 | if (!dev->udev) | ||
218 | return; | ||
219 | |||
220 | /* set alternate 0 */ | ||
221 | dev->alt = 0; | ||
222 | stk1160_info("setting alternate %d\n", dev->alt); | ||
223 | usb_set_interface(dev->udev, 0, 0); | ||
224 | |||
225 | /* Stop stk1160 */ | ||
226 | stk1160_write_reg(dev, STK1160_DCTRL, 0x00); | ||
227 | stk1160_write_reg(dev, STK1160_DCTRL+3, 0x00); | ||
228 | |||
229 | /* Stop saa711x */ | ||
230 | v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0); | ||
231 | } | ||
232 | |||
233 | static int stk1160_stop_streaming(struct stk1160 *dev) | ||
234 | { | ||
235 | if (mutex_lock_interruptible(&dev->v4l_lock)) | ||
236 | return -ERESTARTSYS; | ||
237 | |||
238 | stk1160_cancel_isoc(dev); | ||
239 | |||
240 | /* | ||
241 | * It is possible to keep buffers around using a module parameter. | ||
242 | * This is intended to avoid memory fragmentation. | ||
243 | */ | ||
244 | if (!keep_buffers) | ||
245 | stk1160_free_isoc(dev); | ||
246 | |||
247 | stk1160_stop_hw(dev); | ||
248 | |||
249 | stk1160_clear_queue(dev); | ||
250 | |||
251 | stk1160_dbg("streaming stopped\n"); | ||
252 | |||
253 | mutex_unlock(&dev->v4l_lock); | ||
254 | |||
255 | return 0; | ||
256 | } | ||
257 | |||
258 | static struct v4l2_file_operations stk1160_fops = { | ||
259 | .owner = THIS_MODULE, | ||
260 | .open = v4l2_fh_open, | ||
261 | .release = vb2_fop_release, | ||
262 | .read = vb2_fop_read, | ||
263 | .poll = vb2_fop_poll, | ||
264 | .mmap = vb2_fop_mmap, | ||
265 | .unlocked_ioctl = video_ioctl2, | ||
266 | }; | ||
267 | |||
268 | /* | ||
269 | * vidioc ioctls | ||
270 | */ | ||
271 | static int vidioc_querycap(struct file *file, | ||
272 | void *priv, struct v4l2_capability *cap) | ||
273 | { | ||
274 | struct stk1160 *dev = video_drvdata(file); | ||
275 | |||
276 | strcpy(cap->driver, "stk1160"); | ||
277 | strcpy(cap->card, "stk1160"); | ||
278 | usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info)); | ||
279 | cap->device_caps = | ||
280 | V4L2_CAP_VIDEO_CAPTURE | | ||
281 | V4L2_CAP_STREAMING | | ||
282 | V4L2_CAP_READWRITE; | ||
283 | cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS; | ||
284 | return 0; | ||
285 | } | ||
286 | |||
287 | static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv, | ||
288 | struct v4l2_fmtdesc *f) | ||
289 | { | ||
290 | if (f->index != 0) | ||
291 | return -EINVAL; | ||
292 | |||
293 | strlcpy(f->description, format[f->index].name, sizeof(f->description)); | ||
294 | f->pixelformat = format[f->index].fourcc; | ||
295 | return 0; | ||
296 | } | ||
297 | |||
298 | static int vidioc_g_fmt_vid_cap(struct file *file, void *priv, | ||
299 | struct v4l2_format *f) | ||
300 | { | ||
301 | struct stk1160 *dev = video_drvdata(file); | ||
302 | |||
303 | f->fmt.pix.width = dev->width; | ||
304 | f->fmt.pix.height = dev->height; | ||
305 | f->fmt.pix.field = V4L2_FIELD_INTERLACED; | ||
306 | f->fmt.pix.pixelformat = dev->fmt->fourcc; | ||
307 | f->fmt.pix.bytesperline = dev->width * 2; | ||
308 | f->fmt.pix.sizeimage = dev->height * f->fmt.pix.bytesperline; | ||
309 | f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; | ||
310 | |||
311 | return 0; | ||
312 | } | ||
313 | |||
314 | static int vidioc_try_fmt_vid_cap(struct file *file, void *priv, | ||
315 | struct v4l2_format *f) | ||
316 | { | ||
317 | struct stk1160 *dev = video_drvdata(file); | ||
318 | |||
319 | if (f->fmt.pix.pixelformat != format[0].fourcc) { | ||
320 | stk1160_err("fourcc format 0x%08x invalid\n", | ||
321 | f->fmt.pix.pixelformat); | ||
322 | return -EINVAL; | ||
323 | } | ||
324 | |||
325 | /* | ||
326 | * User can't choose size at his own will, | ||
327 | * so we just return him the current size chosen | ||
328 | * at standard selection. | ||
329 | * TODO: Implement frame scaling? | ||
330 | */ | ||
331 | |||
332 | f->fmt.pix.width = dev->width; | ||
333 | f->fmt.pix.height = dev->height; | ||
334 | f->fmt.pix.field = V4L2_FIELD_INTERLACED; | ||
335 | f->fmt.pix.bytesperline = dev->width * 2; | ||
336 | f->fmt.pix.sizeimage = dev->height * f->fmt.pix.bytesperline; | ||
337 | f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; | ||
338 | |||
339 | return 0; | ||
340 | } | ||
341 | |||
342 | static int vidioc_s_fmt_vid_cap(struct file *file, void *priv, | ||
343 | struct v4l2_format *f) | ||
344 | { | ||
345 | struct stk1160 *dev = video_drvdata(file); | ||
346 | struct vb2_queue *q = &dev->vb_vidq; | ||
347 | int rc; | ||
348 | |||
349 | if (vb2_is_busy(q)) | ||
350 | return -EBUSY; | ||
351 | |||
352 | rc = vidioc_try_fmt_vid_cap(file, priv, f); | ||
353 | if (rc < 0) | ||
354 | return rc; | ||
355 | |||
356 | /* We don't support any format changes */ | ||
357 | |||
358 | return 0; | ||
359 | } | ||
360 | |||
361 | static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm) | ||
362 | { | ||
363 | struct stk1160 *dev = video_drvdata(file); | ||
364 | v4l2_device_call_all(&dev->v4l2_dev, 0, video, querystd, norm); | ||
365 | return 0; | ||
366 | } | ||
367 | |||
368 | static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm) | ||
369 | { | ||
370 | struct stk1160 *dev = video_drvdata(file); | ||
371 | |||
372 | *norm = dev->norm; | ||
373 | return 0; | ||
374 | } | ||
375 | |||
376 | static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm) | ||
377 | { | ||
378 | struct stk1160 *dev = video_drvdata(file); | ||
379 | struct vb2_queue *q = &dev->vb_vidq; | ||
380 | |||
381 | if (vb2_is_busy(q)) | ||
382 | return -EBUSY; | ||
383 | |||
384 | /* Check device presence */ | ||
385 | if (!dev->udev) | ||
386 | return -ENODEV; | ||
387 | |||
388 | /* We need to set this now, before we call stk1160_set_std */ | ||
389 | dev->norm = *norm; | ||
390 | |||
391 | /* This is taken from saa7115 video decoder */ | ||
392 | if (dev->norm & V4L2_STD_525_60) { | ||
393 | dev->width = 720; | ||
394 | dev->height = 480; | ||
395 | } else if (dev->norm & V4L2_STD_625_50) { | ||
396 | dev->width = 720; | ||
397 | dev->height = 576; | ||
398 | } else { | ||
399 | stk1160_err("invalid standard\n"); | ||
400 | return -EINVAL; | ||
401 | } | ||
402 | |||
403 | stk1160_set_std(dev); | ||
404 | |||
405 | v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, | ||
406 | dev->norm); | ||
407 | |||
408 | return 0; | ||
409 | } | ||
410 | |||
411 | |||
412 | static int vidioc_enum_input(struct file *file, void *priv, | ||
413 | struct v4l2_input *i) | ||
414 | { | ||
415 | struct stk1160 *dev = video_drvdata(file); | ||
416 | |||
417 | if (i->index > STK1160_MAX_INPUT) | ||
418 | return -EINVAL; | ||
419 | |||
420 | sprintf(i->name, "Composite%d", i->index); | ||
421 | i->type = V4L2_INPUT_TYPE_CAMERA; | ||
422 | i->std = dev->vdev.tvnorms; | ||
423 | return 0; | ||
424 | } | ||
425 | |||
426 | static int vidioc_g_input(struct file *file, void *priv, unsigned int *i) | ||
427 | { | ||
428 | struct stk1160 *dev = video_drvdata(file); | ||
429 | *i = dev->ctl_input; | ||
430 | return 0; | ||
431 | } | ||
432 | |||
433 | static int vidioc_s_input(struct file *file, void *priv, unsigned int i) | ||
434 | { | ||
435 | struct stk1160 *dev = video_drvdata(file); | ||
436 | |||
437 | if (vb2_is_busy(&dev->vb_vidq)) | ||
438 | return -EBUSY; | ||
439 | |||
440 | if (i > STK1160_MAX_INPUT) | ||
441 | return -EINVAL; | ||
442 | |||
443 | dev->ctl_input = i; | ||
444 | |||
445 | stk1160_select_input(dev); | ||
446 | |||
447 | return 0; | ||
448 | } | ||
449 | |||
450 | static int vidioc_g_chip_ident(struct file *file, void *priv, | ||
451 | struct v4l2_dbg_chip_ident *chip) | ||
452 | { | ||
453 | switch (chip->match.type) { | ||
454 | case V4L2_CHIP_MATCH_HOST: | ||
455 | chip->ident = V4L2_IDENT_NONE; | ||
456 | chip->revision = 0; | ||
457 | return 0; | ||
458 | default: | ||
459 | return -EINVAL; | ||
460 | } | ||
461 | } | ||
462 | |||
463 | #ifdef CONFIG_VIDEO_ADV_DEBUG | ||
464 | static int vidioc_g_register(struct file *file, void *priv, | ||
465 | struct v4l2_dbg_register *reg) | ||
466 | { | ||
467 | struct stk1160 *dev = video_drvdata(file); | ||
468 | int rc; | ||
469 | u8 val; | ||
470 | |||
471 | switch (reg->match.type) { | ||
472 | case V4L2_CHIP_MATCH_AC97: | ||
473 | /* TODO: Support me please :-( */ | ||
474 | return -EINVAL; | ||
475 | case V4L2_CHIP_MATCH_I2C_DRIVER: | ||
476 | v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg); | ||
477 | return 0; | ||
478 | case V4L2_CHIP_MATCH_I2C_ADDR: | ||
479 | /* TODO: is this correct? */ | ||
480 | v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg); | ||
481 | return 0; | ||
482 | default: | ||
483 | if (!v4l2_chip_match_host(®->match)) | ||
484 | return -EINVAL; | ||
485 | } | ||
486 | |||
487 | /* Match host */ | ||
488 | rc = stk1160_read_reg(dev, reg->reg, &val); | ||
489 | reg->val = val; | ||
490 | reg->size = 1; | ||
491 | |||
492 | return rc; | ||
493 | } | ||
494 | |||
495 | static int vidioc_s_register(struct file *file, void *priv, | ||
496 | struct v4l2_dbg_register *reg) | ||
497 | { | ||
498 | struct stk1160 *dev = video_drvdata(file); | ||
499 | |||
500 | switch (reg->match.type) { | ||
501 | case V4L2_CHIP_MATCH_AC97: | ||
502 | return -EINVAL; | ||
503 | case V4L2_CHIP_MATCH_I2C_DRIVER: | ||
504 | v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg); | ||
505 | return 0; | ||
506 | case V4L2_CHIP_MATCH_I2C_ADDR: | ||
507 | /* TODO: is this correct? */ | ||
508 | v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg); | ||
509 | return 0; | ||
510 | default: | ||
511 | if (!v4l2_chip_match_host(®->match)) | ||
512 | return -EINVAL; | ||
513 | } | ||
514 | |||
515 | /* Match host */ | ||
516 | return stk1160_write_reg(dev, reg->reg, cpu_to_le16(reg->val)); | ||
517 | } | ||
518 | #endif | ||
519 | |||
520 | static const struct v4l2_ioctl_ops stk1160_ioctl_ops = { | ||
521 | .vidioc_querycap = vidioc_querycap, | ||
522 | .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap, | ||
523 | .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap, | ||
524 | .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap, | ||
525 | .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap, | ||
526 | .vidioc_querystd = vidioc_querystd, | ||
527 | .vidioc_g_std = vidioc_g_std, | ||
528 | .vidioc_s_std = vidioc_s_std, | ||
529 | .vidioc_enum_input = vidioc_enum_input, | ||
530 | .vidioc_g_input = vidioc_g_input, | ||
531 | .vidioc_s_input = vidioc_s_input, | ||
532 | |||
533 | /* vb2 takes care of these */ | ||
534 | .vidioc_reqbufs = vb2_ioctl_reqbufs, | ||
535 | .vidioc_querybuf = vb2_ioctl_querybuf, | ||
536 | .vidioc_qbuf = vb2_ioctl_qbuf, | ||
537 | .vidioc_dqbuf = vb2_ioctl_dqbuf, | ||
538 | .vidioc_streamon = vb2_ioctl_streamon, | ||
539 | .vidioc_streamoff = vb2_ioctl_streamoff, | ||
540 | |||
541 | .vidioc_log_status = v4l2_ctrl_log_status, | ||
542 | .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, | ||
543 | .vidioc_unsubscribe_event = v4l2_event_unsubscribe, | ||
544 | .vidioc_g_chip_ident = vidioc_g_chip_ident, | ||
545 | |||
546 | #ifdef CONFIG_VIDEO_ADV_DEBUG | ||
547 | .vidioc_g_register = vidioc_g_register, | ||
548 | .vidioc_s_register = vidioc_s_register, | ||
549 | #endif | ||
550 | }; | ||
551 | |||
552 | /********************************************************************/ | ||
553 | |||
554 | /* | ||
555 | * Videobuf2 operations | ||
556 | */ | ||
557 | static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *v4l_fmt, | ||
558 | unsigned int *nbuffers, unsigned int *nplanes, | ||
559 | unsigned int sizes[], void *alloc_ctxs[]) | ||
560 | { | ||
561 | struct stk1160 *dev = vb2_get_drv_priv(vq); | ||
562 | unsigned long size; | ||
563 | |||
564 | size = dev->width * dev->height * 2; | ||
565 | |||
566 | /* | ||
567 | * Here we can change the number of buffers being requested. | ||
568 | * So, we set a minimum and a maximum like this: | ||
569 | */ | ||
570 | *nbuffers = clamp_t(unsigned int, *nbuffers, | ||
571 | STK1160_MIN_VIDEO_BUFFERS, STK1160_MAX_VIDEO_BUFFERS); | ||
572 | |||
573 | /* This means a packed colorformat */ | ||
574 | *nplanes = 1; | ||
575 | |||
576 | sizes[0] = size; | ||
577 | |||
578 | stk1160_info("%s: buffer count %d, each %ld bytes\n", | ||
579 | __func__, *nbuffers, size); | ||
580 | |||
581 | return 0; | ||
582 | } | ||
583 | |||
584 | static void buffer_queue(struct vb2_buffer *vb) | ||
585 | { | ||
586 | unsigned long flags; | ||
587 | struct stk1160 *dev = vb2_get_drv_priv(vb->vb2_queue); | ||
588 | struct stk1160_buffer *buf = | ||
589 | container_of(vb, struct stk1160_buffer, vb); | ||
590 | |||
591 | spin_lock_irqsave(&dev->buf_lock, flags); | ||
592 | if (!dev->udev) { | ||
593 | /* | ||
594 | * If the device is disconnected return the buffer to userspace | ||
595 | * directly. The next QBUF call will fail with -ENODEV. | ||
596 | */ | ||
597 | vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR); | ||
598 | } else { | ||
599 | |||
600 | buf->mem = vb2_plane_vaddr(vb, 0); | ||
601 | buf->length = vb2_plane_size(vb, 0); | ||
602 | buf->bytesused = 0; | ||
603 | buf->pos = 0; | ||
604 | |||
605 | /* | ||
606 | * If buffer length is less from expected then we return | ||
607 | * the buffer to userspace directly. | ||
608 | */ | ||
609 | if (buf->length < dev->width * dev->height * 2) | ||
610 | vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR); | ||
611 | else | ||
612 | list_add_tail(&buf->list, &dev->avail_bufs); | ||
613 | |||
614 | } | ||
615 | spin_unlock_irqrestore(&dev->buf_lock, flags); | ||
616 | } | ||
617 | |||
618 | static int start_streaming(struct vb2_queue *vq, unsigned int count) | ||
619 | { | ||
620 | struct stk1160 *dev = vb2_get_drv_priv(vq); | ||
621 | return stk1160_start_streaming(dev); | ||
622 | } | ||
623 | |||
624 | /* abort streaming and wait for last buffer */ | ||
625 | static int stop_streaming(struct vb2_queue *vq) | ||
626 | { | ||
627 | struct stk1160 *dev = vb2_get_drv_priv(vq); | ||
628 | return stk1160_stop_streaming(dev); | ||
629 | } | ||
630 | |||
631 | static struct vb2_ops stk1160_video_qops = { | ||
632 | .queue_setup = queue_setup, | ||
633 | .buf_queue = buffer_queue, | ||
634 | .start_streaming = start_streaming, | ||
635 | .stop_streaming = stop_streaming, | ||
636 | .wait_prepare = vb2_ops_wait_prepare, | ||
637 | .wait_finish = vb2_ops_wait_finish, | ||
638 | }; | ||
639 | |||
640 | static struct video_device v4l_template = { | ||
641 | .name = "stk1160", | ||
642 | .tvnorms = V4L2_STD_525_60 | V4L2_STD_625_50, | ||
643 | .fops = &stk1160_fops, | ||
644 | .ioctl_ops = &stk1160_ioctl_ops, | ||
645 | .release = video_device_release_empty, | ||
646 | }; | ||
647 | |||
648 | /********************************************************************/ | ||
649 | |||
650 | /* Must be called with both v4l_lock and vb_queue_lock hold */ | ||
651 | void stk1160_clear_queue(struct stk1160 *dev) | ||
652 | { | ||
653 | struct stk1160_buffer *buf; | ||
654 | unsigned long flags; | ||
655 | |||
656 | /* Release all active buffers */ | ||
657 | spin_lock_irqsave(&dev->buf_lock, flags); | ||
658 | while (!list_empty(&dev->avail_bufs)) { | ||
659 | buf = list_first_entry(&dev->avail_bufs, | ||
660 | struct stk1160_buffer, list); | ||
661 | list_del(&buf->list); | ||
662 | vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR); | ||
663 | stk1160_info("buffer [%p/%d] aborted\n", | ||
664 | buf, buf->vb.v4l2_buf.index); | ||
665 | } | ||
666 | /* It's important to clear current buffer */ | ||
667 | dev->isoc_ctl.buf = NULL; | ||
668 | spin_unlock_irqrestore(&dev->buf_lock, flags); | ||
669 | } | ||
670 | |||
671 | int stk1160_vb2_setup(struct stk1160 *dev) | ||
672 | { | ||
673 | int rc; | ||
674 | struct vb2_queue *q; | ||
675 | |||
676 | q = &dev->vb_vidq; | ||
677 | memset(q, 0, sizeof(dev->vb_vidq)); | ||
678 | q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | ||
679 | q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR; | ||
680 | q->drv_priv = dev; | ||
681 | q->buf_struct_size = sizeof(struct stk1160_buffer); | ||
682 | q->ops = &stk1160_video_qops; | ||
683 | q->mem_ops = &vb2_vmalloc_memops; | ||
684 | |||
685 | rc = vb2_queue_init(q); | ||
686 | if (rc < 0) | ||
687 | return rc; | ||
688 | |||
689 | /* initialize video dma queue */ | ||
690 | INIT_LIST_HEAD(&dev->avail_bufs); | ||
691 | |||
692 | return 0; | ||
693 | } | ||
694 | |||
695 | int stk1160_video_register(struct stk1160 *dev) | ||
696 | { | ||
697 | int rc; | ||
698 | |||
699 | /* Initialize video_device with a template structure */ | ||
700 | dev->vdev = v4l_template; | ||
701 | dev->vdev.debug = vidioc_debug; | ||
702 | dev->vdev.queue = &dev->vb_vidq; | ||
703 | |||
704 | /* | ||
705 | * Provide mutexes for v4l2 core and for videobuf2 queue. | ||
706 | * It will be used to protect *only* v4l2 ioctls. | ||
707 | */ | ||
708 | dev->vdev.lock = &dev->v4l_lock; | ||
709 | dev->vdev.queue->lock = &dev->vb_queue_lock; | ||
710 | |||
711 | /* This will be used to set video_device parent */ | ||
712 | dev->vdev.v4l2_dev = &dev->v4l2_dev; | ||
713 | set_bit(V4L2_FL_USE_FH_PRIO, &dev->vdev.flags); | ||
714 | |||
715 | /* NTSC is default */ | ||
716 | dev->norm = V4L2_STD_NTSC_M; | ||
717 | dev->width = 720; | ||
718 | dev->height = 480; | ||
719 | |||
720 | /* set default format */ | ||
721 | dev->fmt = &format[0]; | ||
722 | stk1160_set_std(dev); | ||
723 | |||
724 | v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, | ||
725 | dev->norm); | ||
726 | |||
727 | video_set_drvdata(&dev->vdev, dev); | ||
728 | rc = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1); | ||
729 | if (rc < 0) { | ||
730 | stk1160_err("video_register_device failed (%d)\n", rc); | ||
731 | return rc; | ||
732 | } | ||
733 | |||
734 | v4l2_info(&dev->v4l2_dev, "V4L2 device registered as %s\n", | ||
735 | video_device_node_name(&dev->vdev)); | ||
736 | |||
737 | return 0; | ||
738 | } | ||