Commit | Line | Data |
---|---|---|
1a59d1b8 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
2b455db6 LS |
2 | /* Linux driver for Philips webcam |
3 | USB and Video4Linux interface part. | |
4 | (C) 1999-2004 Nemosoft Unv. | |
5 | (C) 2004-2006 Luc Saillard (luc@saillard.org) | |
6c9cac89 | 6 | (C) 2011 Hans de Goede <hdegoede@redhat.com> |
2b455db6 LS |
7 | |
8 | NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx | |
9 | driver and thus may have bugs that are not present in the original version. | |
10 | Please send bug reports and support requests to <luc@saillard.org>. | |
11 | The decompression routines have been implemented by reverse-engineering the | |
12 | Nemosoft binary pwcx module. Caveat emptor. | |
13 | ||
2b455db6 LS |
14 | |
15 | */ | |
16 | ||
17 | #include <linux/errno.h> | |
18 | #include <linux/init.h> | |
19 | #include <linux/mm.h> | |
20 | #include <linux/module.h> | |
21 | #include <linux/poll.h> | |
2b455db6 | 22 | #include <linux/vmalloc.h> |
6c9cac89 | 23 | #include <linux/jiffies.h> |
2b455db6 LS |
24 | #include <asm/io.h> |
25 | ||
26 | #include "pwc.h" | |
27 | ||
6c9cac89 HG |
28 | #define PWC_CID_CUSTOM(ctrl) ((V4L2_CID_USER_BASE | 0xf000) + custom_ ## ctrl) |
29 | ||
30 | static int pwc_g_volatile_ctrl(struct v4l2_ctrl *ctrl); | |
31 | static int pwc_s_ctrl(struct v4l2_ctrl *ctrl); | |
32 | ||
33 | static const struct v4l2_ctrl_ops pwc_ctrl_ops = { | |
34 | .g_volatile_ctrl = pwc_g_volatile_ctrl, | |
35 | .s_ctrl = pwc_s_ctrl, | |
36 | }; | |
37 | ||
38 | enum { awb_indoor, awb_outdoor, awb_fl, awb_manual, awb_auto }; | |
39 | enum { custom_autocontour, custom_contour, custom_noise_reduction, | |
f4af6595 | 40 | custom_awb_speed, custom_awb_delay, |
6c9cac89 HG |
41 | custom_save_user, custom_restore_user, custom_restore_factory }; |
42 | ||
29541925 | 43 | static const char * const pwc_auto_whitebal_qmenu[] = { |
6c9cac89 HG |
44 | "Indoor (Incandescant Lighting) Mode", |
45 | "Outdoor (Sunlight) Mode", | |
46 | "Indoor (Fluorescent Lighting) Mode", | |
47 | "Manual Mode", | |
48 | "Auto Mode", | |
49 | NULL | |
50 | }; | |
51 | ||
52 | static const struct v4l2_ctrl_config pwc_auto_white_balance_cfg = { | |
53 | .ops = &pwc_ctrl_ops, | |
54 | .id = V4L2_CID_AUTO_WHITE_BALANCE, | |
55 | .type = V4L2_CTRL_TYPE_MENU, | |
56 | .max = awb_auto, | |
57 | .qmenu = pwc_auto_whitebal_qmenu, | |
58 | }; | |
59 | ||
60 | static const struct v4l2_ctrl_config pwc_autocontour_cfg = { | |
61 | .ops = &pwc_ctrl_ops, | |
62 | .id = PWC_CID_CUSTOM(autocontour), | |
63 | .type = V4L2_CTRL_TYPE_BOOLEAN, | |
64 | .name = "Auto contour", | |
65 | .min = 0, | |
66 | .max = 1, | |
67 | .step = 1, | |
68 | }; | |
69 | ||
70 | static const struct v4l2_ctrl_config pwc_contour_cfg = { | |
71 | .ops = &pwc_ctrl_ops, | |
72 | .id = PWC_CID_CUSTOM(contour), | |
73 | .type = V4L2_CTRL_TYPE_INTEGER, | |
74 | .name = "Contour", | |
43d23f36 | 75 | .flags = V4L2_CTRL_FLAG_SLIDER, |
6c9cac89 HG |
76 | .min = 0, |
77 | .max = 63, | |
78 | .step = 1, | |
79 | }; | |
80 | ||
81 | static const struct v4l2_ctrl_config pwc_backlight_cfg = { | |
82 | .ops = &pwc_ctrl_ops, | |
83 | .id = V4L2_CID_BACKLIGHT_COMPENSATION, | |
84 | .type = V4L2_CTRL_TYPE_BOOLEAN, | |
85 | .min = 0, | |
86 | .max = 1, | |
87 | .step = 1, | |
2b455db6 LS |
88 | }; |
89 | ||
6c9cac89 HG |
90 | static const struct v4l2_ctrl_config pwc_flicker_cfg = { |
91 | .ops = &pwc_ctrl_ops, | |
92 | .id = V4L2_CID_BAND_STOP_FILTER, | |
93 | .type = V4L2_CTRL_TYPE_BOOLEAN, | |
94 | .min = 0, | |
95 | .max = 1, | |
96 | .step = 1, | |
97 | }; | |
98 | ||
99 | static const struct v4l2_ctrl_config pwc_noise_reduction_cfg = { | |
100 | .ops = &pwc_ctrl_ops, | |
101 | .id = PWC_CID_CUSTOM(noise_reduction), | |
102 | .type = V4L2_CTRL_TYPE_INTEGER, | |
103 | .name = "Dynamic Noise Reduction", | |
104 | .min = 0, | |
105 | .max = 3, | |
106 | .step = 1, | |
107 | }; | |
108 | ||
109 | static const struct v4l2_ctrl_config pwc_save_user_cfg = { | |
110 | .ops = &pwc_ctrl_ops, | |
111 | .id = PWC_CID_CUSTOM(save_user), | |
112 | .type = V4L2_CTRL_TYPE_BUTTON, | |
113 | .name = "Save User Settings", | |
114 | }; | |
115 | ||
116 | static const struct v4l2_ctrl_config pwc_restore_user_cfg = { | |
117 | .ops = &pwc_ctrl_ops, | |
118 | .id = PWC_CID_CUSTOM(restore_user), | |
119 | .type = V4L2_CTRL_TYPE_BUTTON, | |
120 | .name = "Restore User Settings", | |
121 | }; | |
122 | ||
123 | static const struct v4l2_ctrl_config pwc_restore_factory_cfg = { | |
124 | .ops = &pwc_ctrl_ops, | |
125 | .id = PWC_CID_CUSTOM(restore_factory), | |
126 | .type = V4L2_CTRL_TYPE_BUTTON, | |
127 | .name = "Restore Factory Settings", | |
128 | }; | |
129 | ||
f4af6595 HG |
130 | static const struct v4l2_ctrl_config pwc_awb_speed_cfg = { |
131 | .ops = &pwc_ctrl_ops, | |
132 | .id = PWC_CID_CUSTOM(awb_speed), | |
133 | .type = V4L2_CTRL_TYPE_INTEGER, | |
134 | .name = "Auto White Balance Speed", | |
135 | .min = 1, | |
136 | .max = 32, | |
137 | .step = 1, | |
138 | }; | |
139 | ||
140 | static const struct v4l2_ctrl_config pwc_awb_delay_cfg = { | |
141 | .ops = &pwc_ctrl_ops, | |
142 | .id = PWC_CID_CUSTOM(awb_delay), | |
143 | .type = V4L2_CTRL_TYPE_INTEGER, | |
144 | .name = "Auto White Balance Delay", | |
145 | .min = 0, | |
146 | .max = 63, | |
147 | .step = 1, | |
148 | }; | |
149 | ||
6c9cac89 HG |
150 | int pwc_init_controls(struct pwc_device *pdev) |
151 | { | |
152 | struct v4l2_ctrl_handler *hdl; | |
153 | struct v4l2_ctrl_config cfg; | |
154 | int r, def; | |
155 | ||
156 | hdl = &pdev->ctrl_handler; | |
157 | r = v4l2_ctrl_handler_init(hdl, 20); | |
158 | if (r) | |
159 | return r; | |
160 | ||
161 | /* Brightness, contrast, saturation, gamma */ | |
162 | r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, BRIGHTNESS_FORMATTER, &def); | |
163 | if (r || def > 127) | |
164 | def = 63; | |
165 | pdev->brightness = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops, | |
166 | V4L2_CID_BRIGHTNESS, 0, 127, 1, def); | |
167 | ||
168 | r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, CONTRAST_FORMATTER, &def); | |
169 | if (r || def > 63) | |
170 | def = 31; | |
171 | pdev->contrast = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops, | |
172 | V4L2_CID_CONTRAST, 0, 63, 1, def); | |
173 | ||
174 | if (pdev->type >= 675) { | |
175 | if (pdev->type < 730) | |
176 | pdev->saturation_fmt = SATURATION_MODE_FORMATTER2; | |
177 | else | |
178 | pdev->saturation_fmt = SATURATION_MODE_FORMATTER1; | |
179 | r = pwc_get_s8_ctrl(pdev, GET_CHROM_CTL, pdev->saturation_fmt, | |
180 | &def); | |
181 | if (r || def < -100 || def > 100) | |
182 | def = 0; | |
183 | pdev->saturation = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops, | |
184 | V4L2_CID_SATURATION, -100, 100, 1, def); | |
185 | } | |
186 | ||
187 | r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, GAMMA_FORMATTER, &def); | |
188 | if (r || def > 31) | |
189 | def = 15; | |
190 | pdev->gamma = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops, | |
191 | V4L2_CID_GAMMA, 0, 31, 1, def); | |
192 | ||
193 | /* auto white balance, red gain, blue gain */ | |
194 | r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL, WB_MODE_FORMATTER, &def); | |
195 | if (r || def > awb_auto) | |
196 | def = awb_auto; | |
197 | cfg = pwc_auto_white_balance_cfg; | |
198 | cfg.name = v4l2_ctrl_get_name(cfg.id); | |
199 | cfg.def = def; | |
200 | pdev->auto_white_balance = v4l2_ctrl_new_custom(hdl, &cfg, NULL); | |
201 | /* check auto controls to avoid NULL deref in v4l2_ctrl_auto_cluster */ | |
202 | if (!pdev->auto_white_balance) | |
203 | return hdl->error; | |
204 | ||
205 | r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL, | |
206 | PRESET_MANUAL_RED_GAIN_FORMATTER, &def); | |
207 | if (r) | |
208 | def = 127; | |
209 | pdev->red_balance = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops, | |
210 | V4L2_CID_RED_BALANCE, 0, 255, 1, def); | |
211 | ||
212 | r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL, | |
213 | PRESET_MANUAL_BLUE_GAIN_FORMATTER, &def); | |
214 | if (r) | |
215 | def = 127; | |
216 | pdev->blue_balance = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops, | |
217 | V4L2_CID_BLUE_BALANCE, 0, 255, 1, def); | |
218 | ||
43d23f36 | 219 | v4l2_ctrl_auto_cluster(3, &pdev->auto_white_balance, awb_manual, true); |
6c9cac89 HG |
220 | |
221 | /* autogain, gain */ | |
222 | r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, AGC_MODE_FORMATTER, &def); | |
223 | if (r || (def != 0 && def != 0xff)) | |
224 | def = 0; | |
225 | /* Note a register value if 0 means auto gain is on */ | |
226 | pdev->autogain = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops, | |
227 | V4L2_CID_AUTOGAIN, 0, 1, 1, def == 0); | |
228 | if (!pdev->autogain) | |
229 | return hdl->error; | |
230 | ||
231 | r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, PRESET_AGC_FORMATTER, &def); | |
232 | if (r || def > 63) | |
233 | def = 31; | |
234 | pdev->gain = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops, | |
235 | V4L2_CID_GAIN, 0, 63, 1, def); | |
236 | ||
237 | /* auto exposure, exposure */ | |
238 | if (DEVICE_USE_CODEC2(pdev->type)) { | |
239 | r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, SHUTTER_MODE_FORMATTER, | |
240 | &def); | |
241 | if (r || (def != 0 && def != 0xff)) | |
242 | def = 0; | |
243 | /* | |
244 | * def = 0 auto, def = ff manual | |
245 | * menu idx 0 = auto, idx 1 = manual | |
246 | */ | |
247 | pdev->exposure_auto = v4l2_ctrl_new_std_menu(hdl, | |
248 | &pwc_ctrl_ops, | |
249 | V4L2_CID_EXPOSURE_AUTO, | |
250 | 1, 0, def != 0); | |
251 | if (!pdev->exposure_auto) | |
252 | return hdl->error; | |
253 | ||
254 | /* GET_LUM_CTL, PRESET_SHUTTER_FORMATTER is unreliable */ | |
255 | r = pwc_get_u16_ctrl(pdev, GET_STATUS_CTL, | |
256 | READ_SHUTTER_FORMATTER, &def); | |
257 | if (r || def > 655) | |
258 | def = 655; | |
259 | pdev->exposure = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops, | |
260 | V4L2_CID_EXPOSURE, 0, 655, 1, def); | |
261 | /* CODEC2: separate auto gain & auto exposure */ | |
262 | v4l2_ctrl_auto_cluster(2, &pdev->autogain, 0, true); | |
263 | v4l2_ctrl_auto_cluster(2, &pdev->exposure_auto, | |
264 | V4L2_EXPOSURE_MANUAL, true); | |
265 | } else if (DEVICE_USE_CODEC3(pdev->type)) { | |
266 | /* GET_LUM_CTL, PRESET_SHUTTER_FORMATTER is unreliable */ | |
267 | r = pwc_get_u16_ctrl(pdev, GET_STATUS_CTL, | |
268 | READ_SHUTTER_FORMATTER, &def); | |
269 | if (r || def > 255) | |
270 | def = 255; | |
271 | pdev->exposure = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops, | |
272 | V4L2_CID_EXPOSURE, 0, 255, 1, def); | |
273 | /* CODEC3: both gain and exposure controlled by autogain */ | |
274 | pdev->autogain_expo_cluster[0] = pdev->autogain; | |
275 | pdev->autogain_expo_cluster[1] = pdev->gain; | |
276 | pdev->autogain_expo_cluster[2] = pdev->exposure; | |
277 | v4l2_ctrl_auto_cluster(3, pdev->autogain_expo_cluster, | |
278 | 0, true); | |
279 | } | |
280 | ||
281 | /* color / bw setting */ | |
282 | r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL, COLOUR_MODE_FORMATTER, | |
283 | &def); | |
284 | if (r || (def != 0 && def != 0xff)) | |
285 | def = 0xff; | |
286 | /* def = 0 bw, def = ff color, menu idx 0 = color, idx 1 = bw */ | |
287 | pdev->colorfx = v4l2_ctrl_new_std_menu(hdl, &pwc_ctrl_ops, | |
288 | V4L2_CID_COLORFX, 1, 0, def == 0); | |
289 | ||
290 | /* autocontour, contour */ | |
291 | r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, AUTO_CONTOUR_FORMATTER, &def); | |
292 | if (r || (def != 0 && def != 0xff)) | |
293 | def = 0; | |
294 | cfg = pwc_autocontour_cfg; | |
295 | cfg.def = def == 0; | |
296 | pdev->autocontour = v4l2_ctrl_new_custom(hdl, &cfg, NULL); | |
297 | if (!pdev->autocontour) | |
298 | return hdl->error; | |
299 | ||
300 | r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, PRESET_CONTOUR_FORMATTER, &def); | |
301 | if (r || def > 63) | |
302 | def = 31; | |
303 | cfg = pwc_contour_cfg; | |
304 | cfg.def = def; | |
305 | pdev->contour = v4l2_ctrl_new_custom(hdl, &cfg, NULL); | |
306 | ||
307 | v4l2_ctrl_auto_cluster(2, &pdev->autocontour, 0, false); | |
308 | ||
309 | /* backlight */ | |
310 | r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, | |
311 | BACK_LIGHT_COMPENSATION_FORMATTER, &def); | |
312 | if (r || (def != 0 && def != 0xff)) | |
313 | def = 0; | |
314 | cfg = pwc_backlight_cfg; | |
315 | cfg.name = v4l2_ctrl_get_name(cfg.id); | |
316 | cfg.def = def == 0; | |
317 | pdev->backlight = v4l2_ctrl_new_custom(hdl, &cfg, NULL); | |
318 | ||
319 | /* flikker rediction */ | |
320 | r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, | |
321 | FLICKERLESS_MODE_FORMATTER, &def); | |
322 | if (r || (def != 0 && def != 0xff)) | |
323 | def = 0; | |
324 | cfg = pwc_flicker_cfg; | |
325 | cfg.name = v4l2_ctrl_get_name(cfg.id); | |
326 | cfg.def = def == 0; | |
327 | pdev->flicker = v4l2_ctrl_new_custom(hdl, &cfg, NULL); | |
328 | ||
329 | /* Dynamic noise reduction */ | |
330 | r = pwc_get_u8_ctrl(pdev, GET_LUM_CTL, | |
331 | DYNAMIC_NOISE_CONTROL_FORMATTER, &def); | |
332 | if (r || def > 3) | |
333 | def = 2; | |
334 | cfg = pwc_noise_reduction_cfg; | |
335 | cfg.def = def; | |
336 | pdev->noise_reduction = v4l2_ctrl_new_custom(hdl, &cfg, NULL); | |
337 | ||
338 | /* Save / Restore User / Factory Settings */ | |
339 | pdev->save_user = v4l2_ctrl_new_custom(hdl, &pwc_save_user_cfg, NULL); | |
340 | pdev->restore_user = v4l2_ctrl_new_custom(hdl, &pwc_restore_user_cfg, | |
341 | NULL); | |
342 | if (pdev->restore_user) | |
43d23f36 | 343 | pdev->restore_user->flags |= V4L2_CTRL_FLAG_UPDATE; |
6c9cac89 HG |
344 | pdev->restore_factory = v4l2_ctrl_new_custom(hdl, |
345 | &pwc_restore_factory_cfg, | |
346 | NULL); | |
347 | if (pdev->restore_factory) | |
43d23f36 | 348 | pdev->restore_factory->flags |= V4L2_CTRL_FLAG_UPDATE; |
6c9cac89 | 349 | |
f4af6595 HG |
350 | /* Auto White Balance speed & delay */ |
351 | r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL, | |
352 | AWB_CONTROL_SPEED_FORMATTER, &def); | |
353 | if (r || def < 1 || def > 32) | |
354 | def = 1; | |
355 | cfg = pwc_awb_speed_cfg; | |
356 | cfg.def = def; | |
357 | pdev->awb_speed = v4l2_ctrl_new_custom(hdl, &cfg, NULL); | |
358 | ||
359 | r = pwc_get_u8_ctrl(pdev, GET_CHROM_CTL, | |
360 | AWB_CONTROL_DELAY_FORMATTER, &def); | |
361 | if (r || def > 63) | |
362 | def = 0; | |
363 | cfg = pwc_awb_delay_cfg; | |
364 | cfg.def = def; | |
365 | pdev->awb_delay = v4l2_ctrl_new_custom(hdl, &cfg, NULL); | |
366 | ||
4264a8b6 | 367 | if (!(pdev->features & FEATURE_MOTOR_PANTILT)) |
294e2896 HG |
368 | return hdl->error; |
369 | ||
370 | /* Motor pan / tilt / reset */ | |
371 | pdev->motor_pan = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops, | |
372 | V4L2_CID_PAN_RELATIVE, -4480, 4480, 64, 0); | |
373 | if (!pdev->motor_pan) | |
374 | return hdl->error; | |
375 | pdev->motor_tilt = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops, | |
376 | V4L2_CID_TILT_RELATIVE, -1920, 1920, 64, 0); | |
377 | pdev->motor_pan_reset = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops, | |
378 | V4L2_CID_PAN_RESET, 0, 0, 0, 0); | |
379 | pdev->motor_tilt_reset = v4l2_ctrl_new_std(hdl, &pwc_ctrl_ops, | |
380 | V4L2_CID_TILT_RESET, 0, 0, 0, 0); | |
381 | v4l2_ctrl_cluster(4, &pdev->motor_pan); | |
382 | ||
6c9cac89 HG |
383 | return hdl->error; |
384 | } | |
2b455db6 | 385 | |
31e582e9 HG |
386 | static void pwc_vidioc_fill_fmt(struct v4l2_format *f, |
387 | int width, int height, u32 pixfmt) | |
2b455db6 LS |
388 | { |
389 | memset(&f->fmt.pix, 0, sizeof(struct v4l2_pix_format)); | |
31e582e9 HG |
390 | f->fmt.pix.width = width; |
391 | f->fmt.pix.height = height; | |
2b455db6 | 392 | f->fmt.pix.field = V4L2_FIELD_NONE; |
31e582e9 HG |
393 | f->fmt.pix.pixelformat = pixfmt; |
394 | f->fmt.pix.bytesperline = f->fmt.pix.width; | |
395 | f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.width * 3 / 2; | |
387c71b2 | 396 | f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB; |
c91e42f5 | 397 | PWC_DEBUG_IOCTL("pwc_vidioc_fill_fmt() width=%d, height=%d, bytesperline=%d, sizeimage=%d, pixelformat=%c%c%c%c\n", |
2b455db6 LS |
398 | f->fmt.pix.width, |
399 | f->fmt.pix.height, | |
400 | f->fmt.pix.bytesperline, | |
401 | f->fmt.pix.sizeimage, | |
402 | (f->fmt.pix.pixelformat)&255, | |
403 | (f->fmt.pix.pixelformat>>8)&255, | |
404 | (f->fmt.pix.pixelformat>>16)&255, | |
405 | (f->fmt.pix.pixelformat>>24)&255); | |
406 | } | |
407 | ||
408 | /* ioctl(VIDIOC_TRY_FMT) */ | |
409 | static int pwc_vidioc_try_fmt(struct pwc_device *pdev, struct v4l2_format *f) | |
410 | { | |
795e6eb3 HG |
411 | int size; |
412 | ||
2b455db6 LS |
413 | if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) { |
414 | PWC_DEBUG_IOCTL("Bad video type must be V4L2_BUF_TYPE_VIDEO_CAPTURE\n"); | |
415 | return -EINVAL; | |
416 | } | |
417 | ||
418 | switch (f->fmt.pix.pixelformat) { | |
419 | case V4L2_PIX_FMT_YUV420: | |
420 | break; | |
421 | case V4L2_PIX_FMT_PWC1: | |
422 | if (DEVICE_USE_CODEC23(pdev->type)) { | |
423 | PWC_DEBUG_IOCTL("codec1 is only supported for old pwc webcam\n"); | |
f1927479 | 424 | f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420; |
2b455db6 LS |
425 | } |
426 | break; | |
427 | case V4L2_PIX_FMT_PWC2: | |
428 | if (DEVICE_USE_CODEC1(pdev->type)) { | |
429 | PWC_DEBUG_IOCTL("codec23 is only supported for new pwc webcam\n"); | |
f1927479 | 430 | f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420; |
2b455db6 LS |
431 | } |
432 | break; | |
433 | default: | |
434 | PWC_DEBUG_IOCTL("Unsupported pixel format\n"); | |
f1927479 | 435 | f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420; |
2b455db6 LS |
436 | } |
437 | ||
795e6eb3 | 438 | size = pwc_get_size(pdev, f->fmt.pix.width, f->fmt.pix.height); |
31e582e9 HG |
439 | pwc_vidioc_fill_fmt(f, |
440 | pwc_image_sizes[size][0], | |
441 | pwc_image_sizes[size][1], | |
442 | f->fmt.pix.pixelformat); | |
2b455db6 LS |
443 | |
444 | return 0; | |
445 | } | |
446 | ||
447 | /* ioctl(VIDIOC_SET_FMT) */ | |
4fba471e HG |
448 | |
449 | static int pwc_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f) | |
2b455db6 | 450 | { |
4fba471e | 451 | struct pwc_device *pdev = video_drvdata(file); |
5bbe18d7 | 452 | int ret, pixelformat, compression = 0; |
2b455db6 LS |
453 | |
454 | ret = pwc_vidioc_try_fmt(pdev, f); | |
c20d78cd | 455 | if (ret < 0) |
2b455db6 LS |
456 | return ret; |
457 | ||
2e43dec0 HV |
458 | if (vb2_is_busy(&pdev->vb_queue)) |
459 | return -EBUSY; | |
3751e288 | 460 | |
ceede9fa HG |
461 | pixelformat = f->fmt.pix.pixelformat; |
462 | ||
c91e42f5 | 463 | PWC_DEBUG_IOCTL("Trying to set format to: width=%d height=%d fps=%d format=%c%c%c%c\n", |
115f418b | 464 | f->fmt.pix.width, f->fmt.pix.height, pdev->vframes, |
2b455db6 LS |
465 | (pixelformat)&255, |
466 | (pixelformat>>8)&255, | |
467 | (pixelformat>>16)&255, | |
468 | (pixelformat>>24)&255); | |
469 | ||
5bbe18d7 | 470 | ret = pwc_set_video_mode(pdev, f->fmt.pix.width, f->fmt.pix.height, |
938d5b9e | 471 | pixelformat, 30, &compression, 0); |
2b455db6 | 472 | |
3751e288 | 473 | PWC_DEBUG_IOCTL("pwc_set_video_mode(), return=%d\n", ret); |
2b455db6 | 474 | |
d167a85c | 475 | pwc_vidioc_fill_fmt(f, pdev->width, pdev->height, pdev->pixfmt); |
c20d78cd | 476 | return ret; |
2b455db6 LS |
477 | } |
478 | ||
afa38521 | 479 | static int pwc_querycap(struct file *file, void *fh, struct v4l2_capability *cap) |
2b455db6 | 480 | { |
afa38521 HV |
481 | struct pwc_device *pdev = video_drvdata(file); |
482 | ||
cc1e6315 | 483 | strscpy(cap->driver, PWC_NAME, sizeof(cap->driver)); |
c0decac1 | 484 | strscpy(cap->card, pdev->vdev.name, sizeof(cap->card)); |
afa38521 | 485 | usb_make_path(pdev->udev, cap->bus_info, sizeof(cap->bus_info)); |
387c71b2 HV |
486 | cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING | |
487 | V4L2_CAP_READWRITE; | |
488 | cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS; | |
afa38521 HV |
489 | return 0; |
490 | } | |
2b455db6 | 491 | |
afa38521 HV |
492 | static int pwc_enum_input(struct file *file, void *fh, struct v4l2_input *i) |
493 | { | |
494 | if (i->index) /* Only one INPUT is supported */ | |
495 | return -EINVAL; | |
2b455db6 | 496 | |
c0decac1 | 497 | strscpy(i->name, "Camera", sizeof(i->name)); |
387c71b2 | 498 | i->type = V4L2_INPUT_TYPE_CAMERA; |
afa38521 HV |
499 | return 0; |
500 | } | |
2b455db6 | 501 | |
afa38521 HV |
502 | static int pwc_g_input(struct file *file, void *fh, unsigned int *i) |
503 | { | |
504 | *i = 0; | |
505 | return 0; | |
506 | } | |
2b455db6 | 507 | |
afa38521 HV |
508 | static int pwc_s_input(struct file *file, void *fh, unsigned int i) |
509 | { | |
510 | return i ? -EINVAL : 0; | |
511 | } | |
2b455db6 | 512 | |
ceede9fa | 513 | static int pwc_g_volatile_ctrl(struct v4l2_ctrl *ctrl) |
afa38521 | 514 | { |
6c9cac89 HG |
515 | struct pwc_device *pdev = |
516 | container_of(ctrl->handler, struct pwc_device, ctrl_handler); | |
517 | int ret = 0; | |
518 | ||
6c9cac89 HG |
519 | switch (ctrl->id) { |
520 | case V4L2_CID_AUTO_WHITE_BALANCE: | |
43d23f36 HV |
521 | if (pdev->color_bal_valid && |
522 | (pdev->auto_white_balance->val != awb_auto || | |
523 | time_before(jiffies, | |
524 | pdev->last_color_bal_update + HZ / 4))) { | |
6c9cac89 HG |
525 | pdev->red_balance->val = pdev->last_red_balance; |
526 | pdev->blue_balance->val = pdev->last_blue_balance; | |
527 | break; | |
6588687a | 528 | } |
6c9cac89 HG |
529 | ret = pwc_get_u8_ctrl(pdev, GET_STATUS_CTL, |
530 | READ_RED_GAIN_FORMATTER, | |
531 | &pdev->red_balance->val); | |
532 | if (ret) | |
533 | break; | |
534 | ret = pwc_get_u8_ctrl(pdev, GET_STATUS_CTL, | |
535 | READ_BLUE_GAIN_FORMATTER, | |
536 | &pdev->blue_balance->val); | |
537 | if (ret) | |
538 | break; | |
539 | pdev->last_red_balance = pdev->red_balance->val; | |
540 | pdev->last_blue_balance = pdev->blue_balance->val; | |
541 | pdev->last_color_bal_update = jiffies; | |
542 | pdev->color_bal_valid = true; | |
543 | break; | |
544 | case V4L2_CID_AUTOGAIN: | |
545 | if (pdev->gain_valid && time_before(jiffies, | |
546 | pdev->last_gain_update + HZ / 4)) { | |
547 | pdev->gain->val = pdev->last_gain; | |
548 | break; | |
2b455db6 | 549 | } |
6c9cac89 HG |
550 | ret = pwc_get_u8_ctrl(pdev, GET_STATUS_CTL, |
551 | READ_AGC_FORMATTER, &pdev->gain->val); | |
552 | if (ret) | |
553 | break; | |
554 | pdev->last_gain = pdev->gain->val; | |
555 | pdev->last_gain_update = jiffies; | |
556 | pdev->gain_valid = true; | |
557 | if (!DEVICE_USE_CODEC3(pdev->type)) | |
558 | break; | |
06eeefe8 MCC |
559 | /* For CODEC3 where autogain also controls expo */ |
560 | /* fall through */ | |
6c9cac89 HG |
561 | case V4L2_CID_EXPOSURE_AUTO: |
562 | if (pdev->exposure_valid && time_before(jiffies, | |
563 | pdev->last_exposure_update + HZ / 4)) { | |
564 | pdev->exposure->val = pdev->last_exposure; | |
565 | break; | |
566 | } | |
567 | ret = pwc_get_u16_ctrl(pdev, GET_STATUS_CTL, | |
568 | READ_SHUTTER_FORMATTER, | |
569 | &pdev->exposure->val); | |
570 | if (ret) | |
571 | break; | |
572 | pdev->last_exposure = pdev->exposure->val; | |
573 | pdev->last_exposure_update = jiffies; | |
574 | pdev->exposure_valid = true; | |
575 | break; | |
576 | default: | |
577 | ret = -EINVAL; | |
afa38521 | 578 | } |
6c9cac89 HG |
579 | |
580 | if (ret) | |
581 | PWC_ERROR("g_ctrl %s error %d\n", ctrl->name, ret); | |
582 | ||
6807cfcb HG |
583 | return ret; |
584 | } | |
585 | ||
6c9cac89 | 586 | static int pwc_set_awb(struct pwc_device *pdev) |
afa38521 | 587 | { |
43d23f36 | 588 | int ret; |
6c9cac89 HG |
589 | |
590 | if (pdev->auto_white_balance->is_new) { | |
591 | ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL, | |
592 | WB_MODE_FORMATTER, | |
593 | pdev->auto_white_balance->val); | |
594 | if (ret) | |
595 | return ret; | |
596 | ||
43d23f36 | 597 | if (pdev->auto_white_balance->val != awb_manual) |
6c9cac89 | 598 | pdev->color_bal_valid = false; /* Force cache update */ |
6807cfcb HG |
599 | |
600 | /* | |
601 | * If this is a preset, update our red / blue balance values | |
602 | * so that events get generated for the new preset values | |
603 | */ | |
604 | if (pdev->auto_white_balance->val == awb_indoor || | |
605 | pdev->auto_white_balance->val == awb_outdoor || | |
606 | pdev->auto_white_balance->val == awb_fl) | |
ceede9fa | 607 | pwc_g_volatile_ctrl(pdev->auto_white_balance); |
6c9cac89 | 608 | } |
43d23f36 HV |
609 | if (pdev->auto_white_balance->val != awb_manual) |
610 | return 0; | |
2b455db6 | 611 | |
43d23f36 | 612 | if (pdev->red_balance->is_new) { |
6c9cac89 HG |
613 | ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL, |
614 | PRESET_MANUAL_RED_GAIN_FORMATTER, | |
615 | pdev->red_balance->val); | |
43d23f36 HV |
616 | if (ret) |
617 | return ret; | |
6c9cac89 | 618 | } |
b824bb4b | 619 | |
43d23f36 | 620 | if (pdev->blue_balance->is_new) { |
6c9cac89 HG |
621 | ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL, |
622 | PRESET_MANUAL_BLUE_GAIN_FORMATTER, | |
623 | pdev->blue_balance->val); | |
43d23f36 HV |
624 | if (ret) |
625 | return ret; | |
6c9cac89 | 626 | } |
43d23f36 | 627 | return 0; |
6c9cac89 | 628 | } |
2b455db6 | 629 | |
6c9cac89 HG |
630 | /* For CODEC2 models which have separate autogain and auto exposure */ |
631 | static int pwc_set_autogain(struct pwc_device *pdev) | |
632 | { | |
43d23f36 | 633 | int ret; |
6c9cac89 HG |
634 | |
635 | if (pdev->autogain->is_new) { | |
636 | ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL, | |
637 | AGC_MODE_FORMATTER, | |
638 | pdev->autogain->val ? 0 : 0xff); | |
639 | if (ret) | |
640 | return ret; | |
43d23f36 | 641 | |
6c9cac89 HG |
642 | if (pdev->autogain->val) |
643 | pdev->gain_valid = false; /* Force cache update */ | |
afa38521 | 644 | } |
43d23f36 HV |
645 | |
646 | if (pdev->autogain->val) | |
647 | return 0; | |
648 | ||
649 | if (pdev->gain->is_new) { | |
6c9cac89 HG |
650 | ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL, |
651 | PRESET_AGC_FORMATTER, | |
652 | pdev->gain->val); | |
43d23f36 HV |
653 | if (ret) |
654 | return ret; | |
6c9cac89 | 655 | } |
43d23f36 | 656 | return 0; |
afa38521 | 657 | } |
2b455db6 | 658 | |
6c9cac89 HG |
659 | /* For CODEC2 models which have separate autogain and auto exposure */ |
660 | static int pwc_set_exposure_auto(struct pwc_device *pdev) | |
afa38521 | 661 | { |
43d23f36 | 662 | int ret; |
6c9cac89 HG |
663 | int is_auto = pdev->exposure_auto->val == V4L2_EXPOSURE_AUTO; |
664 | ||
665 | if (pdev->exposure_auto->is_new) { | |
666 | ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL, | |
667 | SHUTTER_MODE_FORMATTER, | |
668 | is_auto ? 0 : 0xff); | |
669 | if (ret) | |
670 | return ret; | |
43d23f36 | 671 | |
6c9cac89 HG |
672 | if (is_auto) |
673 | pdev->exposure_valid = false; /* Force cache update */ | |
6c9cac89 | 674 | } |
43d23f36 HV |
675 | |
676 | if (is_auto) | |
677 | return 0; | |
678 | ||
679 | if (pdev->exposure->is_new) { | |
6c9cac89 HG |
680 | ret = pwc_set_u16_ctrl(pdev, SET_LUM_CTL, |
681 | PRESET_SHUTTER_FORMATTER, | |
682 | pdev->exposure->val); | |
43d23f36 HV |
683 | if (ret) |
684 | return ret; | |
6c9cac89 | 685 | } |
43d23f36 | 686 | return 0; |
6c9cac89 HG |
687 | } |
688 | ||
689 | /* For CODEC3 models which have autogain controlling both gain and exposure */ | |
690 | static int pwc_set_autogain_expo(struct pwc_device *pdev) | |
691 | { | |
43d23f36 | 692 | int ret; |
6c9cac89 HG |
693 | |
694 | if (pdev->autogain->is_new) { | |
695 | ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL, | |
696 | AGC_MODE_FORMATTER, | |
697 | pdev->autogain->val ? 0 : 0xff); | |
698 | if (ret) | |
699 | return ret; | |
43d23f36 | 700 | |
6c9cac89 HG |
701 | if (pdev->autogain->val) { |
702 | pdev->gain_valid = false; /* Force cache update */ | |
703 | pdev->exposure_valid = false; /* Force cache update */ | |
6c9cac89 HG |
704 | } |
705 | } | |
43d23f36 HV |
706 | |
707 | if (pdev->autogain->val) | |
708 | return 0; | |
709 | ||
710 | if (pdev->gain->is_new) { | |
6c9cac89 HG |
711 | ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL, |
712 | PRESET_AGC_FORMATTER, | |
713 | pdev->gain->val); | |
43d23f36 HV |
714 | if (ret) |
715 | return ret; | |
6c9cac89 | 716 | } |
43d23f36 HV |
717 | |
718 | if (pdev->exposure->is_new) { | |
6c9cac89 HG |
719 | ret = pwc_set_u16_ctrl(pdev, SET_LUM_CTL, |
720 | PRESET_SHUTTER_FORMATTER, | |
721 | pdev->exposure->val); | |
43d23f36 HV |
722 | if (ret) |
723 | return ret; | |
6c9cac89 | 724 | } |
43d23f36 | 725 | return 0; |
6c9cac89 HG |
726 | } |
727 | ||
294e2896 HG |
728 | static int pwc_set_motor(struct pwc_device *pdev) |
729 | { | |
730 | int ret; | |
294e2896 | 731 | |
24be689b | 732 | pdev->ctrl_buf[0] = 0; |
294e2896 | 733 | if (pdev->motor_pan_reset->is_new) |
24be689b | 734 | pdev->ctrl_buf[0] |= 0x01; |
294e2896 | 735 | if (pdev->motor_tilt_reset->is_new) |
24be689b | 736 | pdev->ctrl_buf[0] |= 0x02; |
294e2896 HG |
737 | if (pdev->motor_pan_reset->is_new || pdev->motor_tilt_reset->is_new) { |
738 | ret = send_control_msg(pdev, SET_MPT_CTL, | |
24be689b HG |
739 | PT_RESET_CONTROL_FORMATTER, |
740 | pdev->ctrl_buf, 1); | |
294e2896 HG |
741 | if (ret < 0) |
742 | return ret; | |
743 | } | |
744 | ||
24be689b | 745 | memset(pdev->ctrl_buf, 0, 4); |
294e2896 | 746 | if (pdev->motor_pan->is_new) { |
24be689b HG |
747 | pdev->ctrl_buf[0] = pdev->motor_pan->val & 0xFF; |
748 | pdev->ctrl_buf[1] = (pdev->motor_pan->val >> 8); | |
294e2896 HG |
749 | } |
750 | if (pdev->motor_tilt->is_new) { | |
24be689b HG |
751 | pdev->ctrl_buf[2] = pdev->motor_tilt->val & 0xFF; |
752 | pdev->ctrl_buf[3] = (pdev->motor_tilt->val >> 8); | |
294e2896 HG |
753 | } |
754 | if (pdev->motor_pan->is_new || pdev->motor_tilt->is_new) { | |
755 | ret = send_control_msg(pdev, SET_MPT_CTL, | |
756 | PT_RELATIVE_CONTROL_FORMATTER, | |
24be689b | 757 | pdev->ctrl_buf, 4); |
294e2896 HG |
758 | if (ret < 0) |
759 | return ret; | |
760 | } | |
761 | ||
762 | return 0; | |
763 | } | |
764 | ||
6c9cac89 HG |
765 | static int pwc_s_ctrl(struct v4l2_ctrl *ctrl) |
766 | { | |
767 | struct pwc_device *pdev = | |
768 | container_of(ctrl->handler, struct pwc_device, ctrl_handler); | |
769 | int ret = 0; | |
afa38521 | 770 | |
6c9cac89 | 771 | switch (ctrl->id) { |
afa38521 | 772 | case V4L2_CID_BRIGHTNESS: |
6c9cac89 HG |
773 | ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL, |
774 | BRIGHTNESS_FORMATTER, ctrl->val); | |
775 | break; | |
afa38521 | 776 | case V4L2_CID_CONTRAST: |
6c9cac89 HG |
777 | ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL, |
778 | CONTRAST_FORMATTER, ctrl->val); | |
779 | break; | |
afa38521 | 780 | case V4L2_CID_SATURATION: |
6c9cac89 HG |
781 | ret = pwc_set_s8_ctrl(pdev, SET_CHROM_CTL, |
782 | pdev->saturation_fmt, ctrl->val); | |
783 | break; | |
afa38521 | 784 | case V4L2_CID_GAMMA: |
6c9cac89 HG |
785 | ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL, |
786 | GAMMA_FORMATTER, ctrl->val); | |
787 | break; | |
afa38521 | 788 | case V4L2_CID_AUTO_WHITE_BALANCE: |
6c9cac89 HG |
789 | ret = pwc_set_awb(pdev); |
790 | break; | |
afa38521 | 791 | case V4L2_CID_AUTOGAIN: |
6c9cac89 HG |
792 | if (DEVICE_USE_CODEC2(pdev->type)) |
793 | ret = pwc_set_autogain(pdev); | |
794 | else if (DEVICE_USE_CODEC3(pdev->type)) | |
795 | ret = pwc_set_autogain_expo(pdev); | |
796 | else | |
797 | ret = -EINVAL; | |
798 | break; | |
799 | case V4L2_CID_EXPOSURE_AUTO: | |
800 | if (DEVICE_USE_CODEC2(pdev->type)) | |
801 | ret = pwc_set_exposure_auto(pdev); | |
802 | else | |
803 | ret = -EINVAL; | |
804 | break; | |
805 | case V4L2_CID_COLORFX: | |
806 | ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL, | |
807 | COLOUR_MODE_FORMATTER, | |
808 | ctrl->val ? 0 : 0xff); | |
809 | break; | |
810 | case PWC_CID_CUSTOM(autocontour): | |
811 | if (pdev->autocontour->is_new) { | |
812 | ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL, | |
813 | AUTO_CONTOUR_FORMATTER, | |
814 | pdev->autocontour->val ? 0 : 0xff); | |
815 | } | |
816 | if (ret == 0 && pdev->contour->is_new) { | |
6c9cac89 HG |
817 | ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL, |
818 | PRESET_CONTOUR_FORMATTER, | |
819 | pdev->contour->val); | |
820 | } | |
821 | break; | |
822 | case V4L2_CID_BACKLIGHT_COMPENSATION: | |
823 | ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL, | |
824 | BACK_LIGHT_COMPENSATION_FORMATTER, | |
825 | ctrl->val ? 0 : 0xff); | |
826 | break; | |
827 | case V4L2_CID_BAND_STOP_FILTER: | |
828 | ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL, | |
829 | FLICKERLESS_MODE_FORMATTER, | |
830 | ctrl->val ? 0 : 0xff); | |
831 | break; | |
832 | case PWC_CID_CUSTOM(noise_reduction): | |
833 | ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL, | |
834 | DYNAMIC_NOISE_CONTROL_FORMATTER, | |
835 | ctrl->val); | |
836 | break; | |
837 | case PWC_CID_CUSTOM(save_user): | |
838 | ret = pwc_button_ctrl(pdev, SAVE_USER_DEFAULTS_FORMATTER); | |
839 | break; | |
840 | case PWC_CID_CUSTOM(restore_user): | |
841 | ret = pwc_button_ctrl(pdev, RESTORE_USER_DEFAULTS_FORMATTER); | |
842 | break; | |
843 | case PWC_CID_CUSTOM(restore_factory): | |
844 | ret = pwc_button_ctrl(pdev, | |
845 | RESTORE_FACTORY_DEFAULTS_FORMATTER); | |
846 | break; | |
f4af6595 HG |
847 | case PWC_CID_CUSTOM(awb_speed): |
848 | ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL, | |
849 | AWB_CONTROL_SPEED_FORMATTER, | |
850 | ctrl->val); | |
851 | break; | |
852 | case PWC_CID_CUSTOM(awb_delay): | |
853 | ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL, | |
854 | AWB_CONTROL_DELAY_FORMATTER, | |
855 | ctrl->val); | |
856 | break; | |
294e2896 HG |
857 | case V4L2_CID_PAN_RELATIVE: |
858 | ret = pwc_set_motor(pdev); | |
859 | break; | |
6c9cac89 HG |
860 | default: |
861 | ret = -EINVAL; | |
afa38521 | 862 | } |
6c9cac89 HG |
863 | |
864 | if (ret) | |
865 | PWC_ERROR("s_ctrl %s error %d\n", ctrl->name, ret); | |
866 | ||
867 | return ret; | |
afa38521 | 868 | } |
2b455db6 | 869 | |
afa38521 HV |
870 | static int pwc_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *f) |
871 | { | |
872 | struct pwc_device *pdev = video_drvdata(file); | |
873 | ||
874 | /* We only support two format: the raw format, and YUV */ | |
875 | switch (f->index) { | |
876 | case 0: | |
877 | /* RAW format */ | |
878 | f->pixelformat = pdev->type <= 646 ? V4L2_PIX_FMT_PWC1 : V4L2_PIX_FMT_PWC2; | |
879 | f->flags = V4L2_FMT_FLAG_COMPRESSED; | |
c0decac1 MCC |
880 | strscpy(f->description, "Raw Philips Webcam", |
881 | sizeof(f->description)); | |
afa38521 HV |
882 | break; |
883 | case 1: | |
884 | f->pixelformat = V4L2_PIX_FMT_YUV420; | |
c0decac1 MCC |
885 | strscpy(f->description, "4:2:0, planar, Y-Cb-Cr", |
886 | sizeof(f->description)); | |
afa38521 HV |
887 | break; |
888 | default: | |
889 | return -EINVAL; | |
890 | } | |
891 | return 0; | |
892 | } | |
2b455db6 | 893 | |
afa38521 HV |
894 | static int pwc_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f) |
895 | { | |
896 | struct pwc_device *pdev = video_drvdata(file); | |
2b455db6 | 897 | |
31e582e9 HG |
898 | if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
899 | return -EINVAL; | |
900 | ||
afa38521 | 901 | PWC_DEBUG_IOCTL("ioctl(VIDIOC_G_FMT) return size %dx%d\n", |
795e6eb3 | 902 | pdev->width, pdev->height); |
31e582e9 | 903 | pwc_vidioc_fill_fmt(f, pdev->width, pdev->height, pdev->pixfmt); |
afa38521 HV |
904 | return 0; |
905 | } | |
2b455db6 | 906 | |
afa38521 HV |
907 | static int pwc_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f) |
908 | { | |
909 | struct pwc_device *pdev = video_drvdata(file); | |
2b455db6 | 910 | |
afa38521 HV |
911 | return pwc_vidioc_try_fmt(pdev, f); |
912 | } | |
2b455db6 | 913 | |
afa38521 HV |
914 | static int pwc_enum_framesizes(struct file *file, void *fh, |
915 | struct v4l2_frmsizeenum *fsize) | |
916 | { | |
917 | struct pwc_device *pdev = video_drvdata(file); | |
918 | unsigned int i = 0, index = fsize->index; | |
919 | ||
795e6eb3 HG |
920 | if (fsize->pixel_format == V4L2_PIX_FMT_YUV420 || |
921 | (fsize->pixel_format == V4L2_PIX_FMT_PWC1 && | |
922 | DEVICE_USE_CODEC1(pdev->type)) || | |
923 | (fsize->pixel_format == V4L2_PIX_FMT_PWC2 && | |
924 | DEVICE_USE_CODEC23(pdev->type))) { | |
afa38521 | 925 | for (i = 0; i < PSZ_MAX; i++) { |
795e6eb3 HG |
926 | if (!(pdev->image_mask & (1UL << i))) |
927 | continue; | |
928 | if (!index--) { | |
929 | fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE; | |
930 | fsize->discrete.width = pwc_image_sizes[i][0]; | |
931 | fsize->discrete.height = pwc_image_sizes[i][1]; | |
932 | return 0; | |
9ee6d78c | 933 | } |
afa38521 | 934 | } |
afa38521 HV |
935 | } |
936 | return -EINVAL; | |
937 | } | |
9ee6d78c | 938 | |
afa38521 HV |
939 | static int pwc_enum_frameintervals(struct file *file, void *fh, |
940 | struct v4l2_frmivalenum *fival) | |
941 | { | |
942 | struct pwc_device *pdev = video_drvdata(file); | |
943 | int size = -1; | |
944 | unsigned int i; | |
945 | ||
946 | for (i = 0; i < PSZ_MAX; i++) { | |
795e6eb3 HG |
947 | if (pwc_image_sizes[i][0] == fival->width && |
948 | pwc_image_sizes[i][1] == fival->height) { | |
afa38521 HV |
949 | size = i; |
950 | break; | |
951 | } | |
952 | } | |
9ee6d78c | 953 | |
afa38521 HV |
954 | /* TODO: Support raw format */ |
955 | if (size < 0 || fival->pixel_format != V4L2_PIX_FMT_YUV420) | |
956 | return -EINVAL; | |
9ee6d78c | 957 | |
afa38521 HV |
958 | i = pwc_get_fps(pdev, fival->index, size); |
959 | if (!i) | |
960 | return -EINVAL; | |
9ee6d78c | 961 | |
afa38521 HV |
962 | fival->type = V4L2_FRMIVAL_TYPE_DISCRETE; |
963 | fival->discrete.numerator = 1; | |
964 | fival->discrete.denominator = i; | |
9ee6d78c | 965 | |
2b455db6 LS |
966 | return 0; |
967 | } | |
968 | ||
54d3fb3b HG |
969 | static int pwc_g_parm(struct file *file, void *fh, |
970 | struct v4l2_streamparm *parm) | |
971 | { | |
972 | struct pwc_device *pdev = video_drvdata(file); | |
973 | ||
974 | if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) | |
975 | return -EINVAL; | |
976 | ||
977 | memset(parm, 0, sizeof(*parm)); | |
978 | ||
979 | parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
980 | parm->parm.capture.readbuffers = MIN_FRAMES; | |
981 | parm->parm.capture.capability |= V4L2_CAP_TIMEPERFRAME; | |
982 | parm->parm.capture.timeperframe.denominator = pdev->vframes; | |
983 | parm->parm.capture.timeperframe.numerator = 1; | |
984 | ||
985 | return 0; | |
986 | } | |
987 | ||
988 | static int pwc_s_parm(struct file *file, void *fh, | |
989 | struct v4l2_streamparm *parm) | |
990 | { | |
991 | struct pwc_device *pdev = video_drvdata(file); | |
992 | int compression = 0; | |
993 | int ret, fps; | |
994 | ||
387c71b2 | 995 | if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
54d3fb3b HG |
996 | return -EINVAL; |
997 | ||
387c71b2 HV |
998 | /* If timeperframe == 0, then reset the framerate to the nominal value. |
999 | We pick a high framerate here, and let pwc_set_video_mode() figure | |
1000 | out the best match. */ | |
1001 | if (parm->parm.capture.timeperframe.numerator == 0 || | |
1002 | parm->parm.capture.timeperframe.denominator == 0) | |
1003 | fps = 30; | |
1004 | else | |
1005 | fps = parm->parm.capture.timeperframe.denominator / | |
1006 | parm->parm.capture.timeperframe.numerator; | |
54d3fb3b | 1007 | |
2e43dec0 HV |
1008 | if (vb2_is_busy(&pdev->vb_queue)) |
1009 | return -EBUSY; | |
54d3fb3b | 1010 | |
d167a85c | 1011 | ret = pwc_set_video_mode(pdev, pdev->width, pdev->height, pdev->pixfmt, |
938d5b9e | 1012 | fps, &compression, 0); |
54d3fb3b HG |
1013 | |
1014 | pwc_g_parm(file, fh, parm); | |
1015 | ||
54d3fb3b HG |
1016 | return ret; |
1017 | } | |
1018 | ||
afa38521 HV |
1019 | const struct v4l2_ioctl_ops pwc_ioctl_ops = { |
1020 | .vidioc_querycap = pwc_querycap, | |
1021 | .vidioc_enum_input = pwc_enum_input, | |
1022 | .vidioc_g_input = pwc_g_input, | |
1023 | .vidioc_s_input = pwc_s_input, | |
1024 | .vidioc_enum_fmt_vid_cap = pwc_enum_fmt_vid_cap, | |
1025 | .vidioc_g_fmt_vid_cap = pwc_g_fmt_vid_cap, | |
1026 | .vidioc_s_fmt_vid_cap = pwc_s_fmt_vid_cap, | |
1027 | .vidioc_try_fmt_vid_cap = pwc_try_fmt_vid_cap, | |
2e43dec0 HV |
1028 | .vidioc_reqbufs = vb2_ioctl_reqbufs, |
1029 | .vidioc_querybuf = vb2_ioctl_querybuf, | |
1030 | .vidioc_qbuf = vb2_ioctl_qbuf, | |
1031 | .vidioc_dqbuf = vb2_ioctl_dqbuf, | |
1032 | .vidioc_streamon = vb2_ioctl_streamon, | |
1033 | .vidioc_streamoff = vb2_ioctl_streamoff, | |
e2ecb257 | 1034 | .vidioc_log_status = v4l2_ctrl_log_status, |
afa38521 HV |
1035 | .vidioc_enum_framesizes = pwc_enum_framesizes, |
1036 | .vidioc_enum_frameintervals = pwc_enum_frameintervals, | |
54d3fb3b HG |
1037 | .vidioc_g_parm = pwc_g_parm, |
1038 | .vidioc_s_parm = pwc_s_parm, | |
3f1bfef8 HG |
1039 | .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, |
1040 | .vidioc_unsubscribe_event = v4l2_event_unsubscribe, | |
afa38521 | 1041 | }; |