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