blob: 46feece3885258494a0cabb6371d89a3524d9558 [file] [log] [blame]
Luc Saillard2b455db2006-04-24 10:29:46 -03001/* 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)
Hans de Goede6c9cac82011-06-26 12:52:01 -03005 (C) 2011 Hans de Goede <hdegoede@redhat.com>
Luc Saillard2b455db2006-04-24 10:29:46 -03006
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>
Luc Saillard2b455db2006-04-24 10:29:46 -030034#include <linux/vmalloc.h>
Hans de Goede6c9cac82011-06-26 12:52:01 -030035#include <linux/jiffies.h>
Luc Saillard2b455db2006-04-24 10:29:46 -030036#include <asm/io.h>
37
38#include "pwc.h"
39
Hans de Goede6c9cac82011-06-26 12:52:01 -030040#define PWC_CID_CUSTOM(ctrl) ((V4L2_CID_USER_BASE | 0xf000) + custom_ ## ctrl)
41
42static int pwc_g_volatile_ctrl(struct v4l2_ctrl *ctrl);
43static int pwc_s_ctrl(struct v4l2_ctrl *ctrl);
44
45static const struct v4l2_ctrl_ops pwc_ctrl_ops = {
46 .g_volatile_ctrl = pwc_g_volatile_ctrl,
47 .s_ctrl = pwc_s_ctrl,
Luc Saillard2b455db2006-04-24 10:29:46 -030048};
49
Hans de Goede6c9cac82011-06-26 12:52:01 -030050enum { awb_indoor, awb_outdoor, awb_fl, awb_manual, awb_auto };
51enum { custom_autocontour, custom_contour, custom_noise_reduction,
Hans de Goedef4af65952011-10-09 09:56:23 -030052 custom_awb_speed, custom_awb_delay,
Hans de Goede6c9cac82011-06-26 12:52:01 -030053 custom_save_user, custom_restore_user, custom_restore_factory };
54
55const 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
64static 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
72static 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
82static 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",
Hans Verkuil43d23f32011-08-26 08:40:43 -030087 .flags = V4L2_CTRL_FLAG_SLIDER,
Hans de Goede6c9cac82011-06-26 12:52:01 -030088 .min = 0,
89 .max = 63,
90 .step = 1,
91};
92
93static 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,
100};
101
102static 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
111static 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
121static 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
128static 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
135static 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
Hans de Goedef4af65952011-10-09 09:56:23 -0300142static 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
152static 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
Hans de Goede6c9cac82011-06-26 12:52:01 -0300162int 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
Hans Verkuil43d23f32011-08-26 08:40:43 -0300231 v4l2_ctrl_auto_cluster(3, &pdev->auto_white_balance, awb_manual, true);
Hans de Goede6c9cac82011-06-26 12:52:01 -0300232
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)
Hans Verkuil43d23f32011-08-26 08:40:43 -0300355 pdev->restore_user->flags |= V4L2_CTRL_FLAG_UPDATE;
Hans de Goede6c9cac82011-06-26 12:52:01 -0300356 pdev->restore_factory = v4l2_ctrl_new_custom(hdl,
357 &pwc_restore_factory_cfg,
358 NULL);
359 if (pdev->restore_factory)
Hans Verkuil43d23f32011-08-26 08:40:43 -0300360 pdev->restore_factory->flags |= V4L2_CTRL_FLAG_UPDATE;
Hans de Goede6c9cac82011-06-26 12:52:01 -0300361
Hans de Goedef4af65952011-10-09 09:56:23 -0300362 /* 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
Dan Carpenter4264a8b2011-07-23 15:53:03 -0300379 if (!(pdev->features & FEATURE_MOTOR_PANTILT))
Hans de Goede294e2892011-07-03 12:23:24 -0300380 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
Hans de Goede6c9cac82011-06-26 12:52:01 -0300395 return hdl->error;
396}
Luc Saillard2b455db2006-04-24 10:29:46 -0300397
Hans de Goede31e582e2012-01-04 19:46:53 -0300398static void pwc_vidioc_fill_fmt(struct v4l2_format *f,
399 int width, int height, u32 pixfmt)
Luc Saillard2b455db2006-04-24 10:29:46 -0300400{
401 memset(&f->fmt.pix, 0, sizeof(struct v4l2_pix_format));
Hans de Goede31e582e2012-01-04 19:46:53 -0300402 f->fmt.pix.width = width;
403 f->fmt.pix.height = height;
Luc Saillard2b455db2006-04-24 10:29:46 -0300404 f->fmt.pix.field = V4L2_FIELD_NONE;
Hans de Goede31e582e2012-01-04 19:46:53 -0300405 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;
Luc Saillard2b455db2006-04-24 10:29:46 -0300408 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) */
421static int pwc_vidioc_try_fmt(struct pwc_device *pdev, struct v4l2_format *f)
422{
Hans de Goede795e6eb2012-01-04 16:58:44 -0300423 int size;
424
Luc Saillard2b455db2006-04-24 10:29:46 -0300425 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
Hans de Goede795e6eb2012-01-04 16:58:44 -0300451 size = pwc_get_size(pdev, f->fmt.pix.width, f->fmt.pix.height);
Hans de Goede31e582e2012-01-04 19:46:53 -0300452 pwc_vidioc_fill_fmt(f,
453 pwc_image_sizes[size][0],
454 pwc_image_sizes[size][1],
455 f->fmt.pix.pixelformat);
Luc Saillard2b455db2006-04-24 10:29:46 -0300456
457 return 0;
458}
459
460/* ioctl(VIDIOC_SET_FMT) */
Hans de Goede4fba4712011-06-26 12:13:44 -0300461
462static int pwc_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
Luc Saillard2b455db2006-04-24 10:29:46 -0300463{
Hans de Goede4fba4712011-06-26 12:13:44 -0300464 struct pwc_device *pdev = video_drvdata(file);
Hans de Goede5bbe18d2012-01-04 18:48:05 -0300465 int ret, pixelformat, compression = 0;
Luc Saillard2b455db2006-04-24 10:29:46 -0300466
Hans de Goedec20d78c2011-10-09 09:16:46 -0300467 if (pwc_test_n_set_capt_file(pdev, file))
Hans de Goede4fba4712011-06-26 12:13:44 -0300468 return -EBUSY;
469
Luc Saillard2b455db2006-04-24 10:29:46 -0300470 ret = pwc_vidioc_try_fmt(pdev, f);
Hans de Goedec20d78c2011-10-09 09:16:46 -0300471 if (ret < 0)
Luc Saillard2b455db2006-04-24 10:29:46 -0300472 return ret;
473
474 pixelformat = f->fmt.pix.pixelformat;
Luc Saillard2b455db2006-04-24 10:29:46 -0300475
Hans de Goedec20d78c2011-10-09 09:16:46 -0300476 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 }
Hans de Goede3751e282010-11-16 11:39:25 -0300486
487 PWC_DEBUG_IOCTL("Trying to set format to: width=%d height=%d fps=%d "
Hans de Goede5bbe18d2012-01-04 18:48:05 -0300488 "format=%c%c%c%c\n",
Hans de Goede115f4182011-12-31 08:47:07 -0300489 f->fmt.pix.width, f->fmt.pix.height, pdev->vframes,
Luc Saillard2b455db2006-04-24 10:29:46 -0300490 (pixelformat)&255,
491 (pixelformat>>8)&255,
492 (pixelformat>>16)&255,
493 (pixelformat>>24)&255);
494
Hans de Goede5bbe18d2012-01-04 18:48:05 -0300495 ret = pwc_set_video_mode(pdev, f->fmt.pix.width, f->fmt.pix.height,
Hans de Goede938d5b92012-01-10 13:14:46 -0300496 pixelformat, 30, &compression, 0);
Luc Saillard2b455db2006-04-24 10:29:46 -0300497
Hans de Goede3751e282010-11-16 11:39:25 -0300498 PWC_DEBUG_IOCTL("pwc_set_video_mode(), return=%d\n", ret);
Luc Saillard2b455db2006-04-24 10:29:46 -0300499
Hans de Goeded167a852012-01-10 13:01:41 -0300500 pwc_vidioc_fill_fmt(f, pdev->width, pdev->height, pdev->pixfmt);
Hans de Goedec20d78c2011-10-09 09:16:46 -0300501leave:
502 mutex_unlock(&pdev->udevlock);
503 return ret;
Luc Saillard2b455db2006-04-24 10:29:46 -0300504}
505
Hans Verkuilafa38522011-01-22 06:34:55 -0300506static int pwc_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
Luc Saillard2b455db2006-04-24 10:29:46 -0300507{
Hans Verkuilafa38522011-01-22 06:34:55 -0300508 struct pwc_device *pdev = video_drvdata(file);
Luc Saillard2b455db2006-04-24 10:29:46 -0300509
Hans de Goedeb824bb42011-06-25 17:39:19 -0300510 if (!pdev->udev)
511 return -ENODEV;
512
Hans Verkuilafa38522011-01-22 06:34:55 -0300513 strcpy(cap->driver, PWC_NAME);
Hans de Goede6c9cac82011-06-26 12:52:01 -0300514 strlcpy(cap->card, pdev->vdev.name, sizeof(cap->card));
Hans Verkuilafa38522011-01-22 06:34:55 -0300515 usb_make_path(pdev->udev, cap->bus_info, sizeof(cap->bus_info));
Hans Verkuilafa38522011-01-22 06:34:55 -0300516 cap->capabilities =
517 V4L2_CAP_VIDEO_CAPTURE |
518 V4L2_CAP_STREAMING |
519 V4L2_CAP_READWRITE;
Luc Saillard2b455db2006-04-24 10:29:46 -0300520 return 0;
521}
522
Hans Verkuilafa38522011-01-22 06:34:55 -0300523static int pwc_enum_input(struct file *file, void *fh, struct v4l2_input *i)
524{
525 if (i->index) /* Only one INPUT is supported */
526 return -EINVAL;
527
528 strcpy(i->name, "usb");
529 return 0;
530}
531
532static int pwc_g_input(struct file *file, void *fh, unsigned int *i)
533{
534 *i = 0;
535 return 0;
536}
537
538static int pwc_s_input(struct file *file, void *fh, unsigned int i)
539{
540 return i ? -EINVAL : 0;
541}
542
Hans de Goede6807cfc2011-10-09 17:22:00 -0300543static int pwc_g_volatile_ctrl_unlocked(struct v4l2_ctrl *ctrl)
Hans Verkuilafa38522011-01-22 06:34:55 -0300544{
Hans de Goede6c9cac82011-06-26 12:52:01 -0300545 struct pwc_device *pdev =
546 container_of(ctrl->handler, struct pwc_device, ctrl_handler);
547 int ret = 0;
Hans Verkuilafa38522011-01-22 06:34:55 -0300548
Hans de Goede6807cfc2011-10-09 17:22:00 -0300549 if (!pdev->udev)
550 return -ENODEV;
Hans de Goedeb824bb42011-06-25 17:39:19 -0300551
Hans de Goede6c9cac82011-06-26 12:52:01 -0300552 switch (ctrl->id) {
Hans Verkuilafa38522011-01-22 06:34:55 -0300553 case V4L2_CID_AUTO_WHITE_BALANCE:
Hans Verkuil43d23f32011-08-26 08:40:43 -0300554 if (pdev->color_bal_valid &&
555 (pdev->auto_white_balance->val != awb_auto ||
556 time_before(jiffies,
557 pdev->last_color_bal_update + HZ / 4))) {
Hans de Goede6c9cac82011-06-26 12:52:01 -0300558 pdev->red_balance->val = pdev->last_red_balance;
559 pdev->blue_balance->val = pdev->last_blue_balance;
560 break;
561 }
562 ret = pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
563 READ_RED_GAIN_FORMATTER,
564 &pdev->red_balance->val);
565 if (ret)
566 break;
567 ret = pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
568 READ_BLUE_GAIN_FORMATTER,
569 &pdev->blue_balance->val);
570 if (ret)
571 break;
572 pdev->last_red_balance = pdev->red_balance->val;
573 pdev->last_blue_balance = pdev->blue_balance->val;
574 pdev->last_color_bal_update = jiffies;
575 pdev->color_bal_valid = true;
576 break;
Hans Verkuilafa38522011-01-22 06:34:55 -0300577 case V4L2_CID_AUTOGAIN:
Hans de Goede6c9cac82011-06-26 12:52:01 -0300578 if (pdev->gain_valid && time_before(jiffies,
579 pdev->last_gain_update + HZ / 4)) {
580 pdev->gain->val = pdev->last_gain;
581 break;
582 }
583 ret = pwc_get_u8_ctrl(pdev, GET_STATUS_CTL,
584 READ_AGC_FORMATTER, &pdev->gain->val);
585 if (ret)
586 break;
587 pdev->last_gain = pdev->gain->val;
588 pdev->last_gain_update = jiffies;
589 pdev->gain_valid = true;
590 if (!DEVICE_USE_CODEC3(pdev->type))
591 break;
592 /* Fall through for CODEC3 where autogain also controls expo */
593 case V4L2_CID_EXPOSURE_AUTO:
594 if (pdev->exposure_valid && time_before(jiffies,
595 pdev->last_exposure_update + HZ / 4)) {
596 pdev->exposure->val = pdev->last_exposure;
597 break;
598 }
599 ret = pwc_get_u16_ctrl(pdev, GET_STATUS_CTL,
600 READ_SHUTTER_FORMATTER,
601 &pdev->exposure->val);
602 if (ret)
603 break;
604 pdev->last_exposure = pdev->exposure->val;
605 pdev->last_exposure_update = jiffies;
606 pdev->exposure_valid = true;
607 break;
608 default:
609 ret = -EINVAL;
Hans Verkuilafa38522011-01-22 06:34:55 -0300610 }
Hans de Goede6c9cac82011-06-26 12:52:01 -0300611
612 if (ret)
613 PWC_ERROR("g_ctrl %s error %d\n", ctrl->name, ret);
614
Hans de Goede6807cfc2011-10-09 17:22:00 -0300615 return ret;
616}
617
618static int pwc_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
619{
620 struct pwc_device *pdev =
621 container_of(ctrl->handler, struct pwc_device, ctrl_handler);
622 int ret;
623
624 mutex_lock(&pdev->udevlock);
625 ret = pwc_g_volatile_ctrl_unlocked(ctrl);
Hans de Goedec1127132011-07-03 11:50:51 -0300626 mutex_unlock(&pdev->udevlock);
Hans de Goede6c9cac82011-06-26 12:52:01 -0300627 return ret;
Hans Verkuilafa38522011-01-22 06:34:55 -0300628}
629
Hans de Goede6c9cac82011-06-26 12:52:01 -0300630static int pwc_set_awb(struct pwc_device *pdev)
Hans Verkuilafa38522011-01-22 06:34:55 -0300631{
Hans Verkuil43d23f32011-08-26 08:40:43 -0300632 int ret;
Hans de Goede6c9cac82011-06-26 12:52:01 -0300633
634 if (pdev->auto_white_balance->is_new) {
635 ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
636 WB_MODE_FORMATTER,
637 pdev->auto_white_balance->val);
638 if (ret)
639 return ret;
640
Hans de Goede6c9cac82011-06-26 12:52:01 -0300641 if (pdev->auto_white_balance->val != awb_manual)
Hans Verkuil43d23f32011-08-26 08:40:43 -0300642 pdev->color_bal_valid = false; /* Force cache update */
Hans de Goede6807cfc2011-10-09 17:22:00 -0300643
644 /*
645 * If this is a preset, update our red / blue balance values
646 * so that events get generated for the new preset values
647 */
648 if (pdev->auto_white_balance->val == awb_indoor ||
649 pdev->auto_white_balance->val == awb_outdoor ||
650 pdev->auto_white_balance->val == awb_fl)
651 pwc_g_volatile_ctrl_unlocked(pdev->auto_white_balance);
Hans Verkuil43d23f32011-08-26 08:40:43 -0300652 }
653 if (pdev->auto_white_balance->val != awb_manual)
654 return 0;
655
656 if (pdev->red_balance->is_new) {
Hans de Goede6c9cac82011-06-26 12:52:01 -0300657 ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
658 PRESET_MANUAL_RED_GAIN_FORMATTER,
659 pdev->red_balance->val);
Hans Verkuil43d23f32011-08-26 08:40:43 -0300660 if (ret)
661 return ret;
Hans de Goede6c9cac82011-06-26 12:52:01 -0300662 }
663
Hans Verkuil43d23f32011-08-26 08:40:43 -0300664 if (pdev->blue_balance->is_new) {
Hans de Goede6c9cac82011-06-26 12:52:01 -0300665 ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
666 PRESET_MANUAL_BLUE_GAIN_FORMATTER,
667 pdev->blue_balance->val);
Hans Verkuil43d23f32011-08-26 08:40:43 -0300668 if (ret)
669 return ret;
Hans de Goede6c9cac82011-06-26 12:52:01 -0300670 }
Hans Verkuil43d23f32011-08-26 08:40:43 -0300671 return 0;
Hans de Goede6c9cac82011-06-26 12:52:01 -0300672}
673
674/* For CODEC2 models which have separate autogain and auto exposure */
675static int pwc_set_autogain(struct pwc_device *pdev)
676{
Hans Verkuil43d23f32011-08-26 08:40:43 -0300677 int ret;
Hans de Goede6c9cac82011-06-26 12:52:01 -0300678
679 if (pdev->autogain->is_new) {
680 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
681 AGC_MODE_FORMATTER,
682 pdev->autogain->val ? 0 : 0xff);
683 if (ret)
684 return ret;
Hans Verkuil43d23f32011-08-26 08:40:43 -0300685
Hans de Goede6c9cac82011-06-26 12:52:01 -0300686 if (pdev->autogain->val)
687 pdev->gain_valid = false; /* Force cache update */
Hans de Goede6c9cac82011-06-26 12:52:01 -0300688 }
Hans Verkuil43d23f32011-08-26 08:40:43 -0300689
690 if (pdev->autogain->val)
691 return 0;
692
693 if (pdev->gain->is_new) {
Hans de Goede6c9cac82011-06-26 12:52:01 -0300694 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
695 PRESET_AGC_FORMATTER,
696 pdev->gain->val);
Hans Verkuil43d23f32011-08-26 08:40:43 -0300697 if (ret)
698 return ret;
Hans de Goede6c9cac82011-06-26 12:52:01 -0300699 }
Hans Verkuil43d23f32011-08-26 08:40:43 -0300700 return 0;
Hans de Goede6c9cac82011-06-26 12:52:01 -0300701}
702
703/* For CODEC2 models which have separate autogain and auto exposure */
704static int pwc_set_exposure_auto(struct pwc_device *pdev)
705{
Hans Verkuil43d23f32011-08-26 08:40:43 -0300706 int ret;
Hans de Goede6c9cac82011-06-26 12:52:01 -0300707 int is_auto = pdev->exposure_auto->val == V4L2_EXPOSURE_AUTO;
708
709 if (pdev->exposure_auto->is_new) {
710 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
711 SHUTTER_MODE_FORMATTER,
712 is_auto ? 0 : 0xff);
713 if (ret)
714 return ret;
Hans Verkuil43d23f32011-08-26 08:40:43 -0300715
Hans de Goede6c9cac82011-06-26 12:52:01 -0300716 if (is_auto)
717 pdev->exposure_valid = false; /* Force cache update */
Hans de Goede6c9cac82011-06-26 12:52:01 -0300718 }
Hans Verkuil43d23f32011-08-26 08:40:43 -0300719
720 if (is_auto)
721 return 0;
722
723 if (pdev->exposure->is_new) {
Hans de Goede6c9cac82011-06-26 12:52:01 -0300724 ret = pwc_set_u16_ctrl(pdev, SET_LUM_CTL,
725 PRESET_SHUTTER_FORMATTER,
726 pdev->exposure->val);
Hans Verkuil43d23f32011-08-26 08:40:43 -0300727 if (ret)
728 return ret;
Hans de Goede6c9cac82011-06-26 12:52:01 -0300729 }
Hans Verkuil43d23f32011-08-26 08:40:43 -0300730 return 0;
Hans de Goede6c9cac82011-06-26 12:52:01 -0300731}
732
733/* For CODEC3 models which have autogain controlling both gain and exposure */
734static int pwc_set_autogain_expo(struct pwc_device *pdev)
735{
Hans Verkuil43d23f32011-08-26 08:40:43 -0300736 int ret;
Hans de Goede6c9cac82011-06-26 12:52:01 -0300737
738 if (pdev->autogain->is_new) {
739 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
740 AGC_MODE_FORMATTER,
741 pdev->autogain->val ? 0 : 0xff);
742 if (ret)
743 return ret;
Hans Verkuil43d23f32011-08-26 08:40:43 -0300744
Hans de Goede6c9cac82011-06-26 12:52:01 -0300745 if (pdev->autogain->val) {
746 pdev->gain_valid = false; /* Force cache update */
747 pdev->exposure_valid = false; /* Force cache update */
Hans de Goede6c9cac82011-06-26 12:52:01 -0300748 }
749 }
Hans Verkuil43d23f32011-08-26 08:40:43 -0300750
751 if (pdev->autogain->val)
752 return 0;
753
754 if (pdev->gain->is_new) {
Hans de Goede6c9cac82011-06-26 12:52:01 -0300755 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
756 PRESET_AGC_FORMATTER,
757 pdev->gain->val);
Hans Verkuil43d23f32011-08-26 08:40:43 -0300758 if (ret)
759 return ret;
Hans de Goede6c9cac82011-06-26 12:52:01 -0300760 }
Hans Verkuil43d23f32011-08-26 08:40:43 -0300761
762 if (pdev->exposure->is_new) {
Hans de Goede6c9cac82011-06-26 12:52:01 -0300763 ret = pwc_set_u16_ctrl(pdev, SET_LUM_CTL,
764 PRESET_SHUTTER_FORMATTER,
765 pdev->exposure->val);
Hans Verkuil43d23f32011-08-26 08:40:43 -0300766 if (ret)
767 return ret;
Hans de Goede6c9cac82011-06-26 12:52:01 -0300768 }
Hans Verkuil43d23f32011-08-26 08:40:43 -0300769 return 0;
Hans de Goede6c9cac82011-06-26 12:52:01 -0300770}
771
Hans de Goede294e2892011-07-03 12:23:24 -0300772static int pwc_set_motor(struct pwc_device *pdev)
773{
774 int ret;
775 u8 buf[4];
776
777 buf[0] = 0;
778 if (pdev->motor_pan_reset->is_new)
779 buf[0] |= 0x01;
780 if (pdev->motor_tilt_reset->is_new)
781 buf[0] |= 0x02;
782 if (pdev->motor_pan_reset->is_new || pdev->motor_tilt_reset->is_new) {
783 ret = send_control_msg(pdev, SET_MPT_CTL,
784 PT_RESET_CONTROL_FORMATTER, buf, 1);
785 if (ret < 0)
786 return ret;
787 }
788
789 memset(buf, 0, sizeof(buf));
790 if (pdev->motor_pan->is_new) {
791 buf[0] = pdev->motor_pan->val & 0xFF;
792 buf[1] = (pdev->motor_pan->val >> 8);
793 }
794 if (pdev->motor_tilt->is_new) {
795 buf[2] = pdev->motor_tilt->val & 0xFF;
796 buf[3] = (pdev->motor_tilt->val >> 8);
797 }
798 if (pdev->motor_pan->is_new || pdev->motor_tilt->is_new) {
799 ret = send_control_msg(pdev, SET_MPT_CTL,
800 PT_RELATIVE_CONTROL_FORMATTER,
801 buf, sizeof(buf));
802 if (ret < 0)
803 return ret;
804 }
805
806 return 0;
807}
808
Hans de Goede6c9cac82011-06-26 12:52:01 -0300809static int pwc_s_ctrl(struct v4l2_ctrl *ctrl)
810{
811 struct pwc_device *pdev =
812 container_of(ctrl->handler, struct pwc_device, ctrl_handler);
813 int ret = 0;
Hans Verkuilafa38522011-01-22 06:34:55 -0300814
Hans de Goedec1127132011-07-03 11:50:51 -0300815 mutex_lock(&pdev->udevlock);
816
817 if (!pdev->udev) {
818 ret = -ENODEV;
819 goto leave;
820 }
Hans de Goedeb824bb42011-06-25 17:39:19 -0300821
Hans de Goede6c9cac82011-06-26 12:52:01 -0300822 switch (ctrl->id) {
Hans Verkuilafa38522011-01-22 06:34:55 -0300823 case V4L2_CID_BRIGHTNESS:
Hans de Goede6c9cac82011-06-26 12:52:01 -0300824 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
825 BRIGHTNESS_FORMATTER, ctrl->val);
826 break;
Hans Verkuilafa38522011-01-22 06:34:55 -0300827 case V4L2_CID_CONTRAST:
Hans de Goede6c9cac82011-06-26 12:52:01 -0300828 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
829 CONTRAST_FORMATTER, ctrl->val);
830 break;
Hans Verkuilafa38522011-01-22 06:34:55 -0300831 case V4L2_CID_SATURATION:
Hans de Goede6c9cac82011-06-26 12:52:01 -0300832 ret = pwc_set_s8_ctrl(pdev, SET_CHROM_CTL,
833 pdev->saturation_fmt, ctrl->val);
834 break;
Hans Verkuilafa38522011-01-22 06:34:55 -0300835 case V4L2_CID_GAMMA:
Hans de Goede6c9cac82011-06-26 12:52:01 -0300836 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
837 GAMMA_FORMATTER, ctrl->val);
838 break;
Hans Verkuilafa38522011-01-22 06:34:55 -0300839 case V4L2_CID_AUTO_WHITE_BALANCE:
Hans de Goede6c9cac82011-06-26 12:52:01 -0300840 ret = pwc_set_awb(pdev);
841 break;
Hans Verkuilafa38522011-01-22 06:34:55 -0300842 case V4L2_CID_AUTOGAIN:
Hans de Goede6c9cac82011-06-26 12:52:01 -0300843 if (DEVICE_USE_CODEC2(pdev->type))
844 ret = pwc_set_autogain(pdev);
845 else if (DEVICE_USE_CODEC3(pdev->type))
846 ret = pwc_set_autogain_expo(pdev);
847 else
848 ret = -EINVAL;
849 break;
850 case V4L2_CID_EXPOSURE_AUTO:
851 if (DEVICE_USE_CODEC2(pdev->type))
852 ret = pwc_set_exposure_auto(pdev);
853 else
854 ret = -EINVAL;
855 break;
856 case V4L2_CID_COLORFX:
857 ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
858 COLOUR_MODE_FORMATTER,
859 ctrl->val ? 0 : 0xff);
860 break;
861 case PWC_CID_CUSTOM(autocontour):
862 if (pdev->autocontour->is_new) {
863 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
864 AUTO_CONTOUR_FORMATTER,
865 pdev->autocontour->val ? 0 : 0xff);
866 }
867 if (ret == 0 && pdev->contour->is_new) {
Hans de Goede6c9cac82011-06-26 12:52:01 -0300868 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
869 PRESET_CONTOUR_FORMATTER,
870 pdev->contour->val);
871 }
872 break;
873 case V4L2_CID_BACKLIGHT_COMPENSATION:
874 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
875 BACK_LIGHT_COMPENSATION_FORMATTER,
876 ctrl->val ? 0 : 0xff);
877 break;
878 case V4L2_CID_BAND_STOP_FILTER:
879 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
880 FLICKERLESS_MODE_FORMATTER,
881 ctrl->val ? 0 : 0xff);
882 break;
883 case PWC_CID_CUSTOM(noise_reduction):
884 ret = pwc_set_u8_ctrl(pdev, SET_LUM_CTL,
885 DYNAMIC_NOISE_CONTROL_FORMATTER,
886 ctrl->val);
887 break;
888 case PWC_CID_CUSTOM(save_user):
889 ret = pwc_button_ctrl(pdev, SAVE_USER_DEFAULTS_FORMATTER);
890 break;
891 case PWC_CID_CUSTOM(restore_user):
892 ret = pwc_button_ctrl(pdev, RESTORE_USER_DEFAULTS_FORMATTER);
893 break;
894 case PWC_CID_CUSTOM(restore_factory):
895 ret = pwc_button_ctrl(pdev,
896 RESTORE_FACTORY_DEFAULTS_FORMATTER);
897 break;
Hans de Goedef4af65952011-10-09 09:56:23 -0300898 case PWC_CID_CUSTOM(awb_speed):
899 ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
900 AWB_CONTROL_SPEED_FORMATTER,
901 ctrl->val);
902 break;
903 case PWC_CID_CUSTOM(awb_delay):
904 ret = pwc_set_u8_ctrl(pdev, SET_CHROM_CTL,
905 AWB_CONTROL_DELAY_FORMATTER,
906 ctrl->val);
907 break;
Hans de Goede294e2892011-07-03 12:23:24 -0300908 case V4L2_CID_PAN_RELATIVE:
909 ret = pwc_set_motor(pdev);
910 break;
Hans de Goede6c9cac82011-06-26 12:52:01 -0300911 default:
912 ret = -EINVAL;
Hans Verkuilafa38522011-01-22 06:34:55 -0300913 }
Hans de Goede6c9cac82011-06-26 12:52:01 -0300914
915 if (ret)
916 PWC_ERROR("s_ctrl %s error %d\n", ctrl->name, ret);
917
Hans de Goedec1127132011-07-03 11:50:51 -0300918leave:
919 mutex_unlock(&pdev->udevlock);
Hans de Goede6c9cac82011-06-26 12:52:01 -0300920 return ret;
Hans Verkuilafa38522011-01-22 06:34:55 -0300921}
922
923static int pwc_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *f)
924{
925 struct pwc_device *pdev = video_drvdata(file);
926
927 /* We only support two format: the raw format, and YUV */
928 switch (f->index) {
929 case 0:
930 /* RAW format */
931 f->pixelformat = pdev->type <= 646 ? V4L2_PIX_FMT_PWC1 : V4L2_PIX_FMT_PWC2;
932 f->flags = V4L2_FMT_FLAG_COMPRESSED;
933 strlcpy(f->description, "Raw Philips Webcam", sizeof(f->description));
934 break;
935 case 1:
936 f->pixelformat = V4L2_PIX_FMT_YUV420;
937 strlcpy(f->description, "4:2:0, planar, Y-Cb-Cr", sizeof(f->description));
938 break;
939 default:
940 return -EINVAL;
941 }
942 return 0;
943}
944
945static int pwc_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
946{
947 struct pwc_device *pdev = video_drvdata(file);
948
Hans de Goede31e582e2012-01-04 19:46:53 -0300949 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
950 return -EINVAL;
951
Hans de Goedec20d78c2011-10-09 09:16:46 -0300952 mutex_lock(&pdev->udevlock); /* To avoid race with s_fmt */
Hans Verkuilafa38522011-01-22 06:34:55 -0300953 PWC_DEBUG_IOCTL("ioctl(VIDIOC_G_FMT) return size %dx%d\n",
Hans de Goede795e6eb2012-01-04 16:58:44 -0300954 pdev->width, pdev->height);
Hans de Goede31e582e2012-01-04 19:46:53 -0300955 pwc_vidioc_fill_fmt(f, pdev->width, pdev->height, pdev->pixfmt);
Hans de Goedec20d78c2011-10-09 09:16:46 -0300956 mutex_unlock(&pdev->udevlock);
Hans Verkuilafa38522011-01-22 06:34:55 -0300957 return 0;
958}
959
960static int pwc_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f)
961{
962 struct pwc_device *pdev = video_drvdata(file);
963
964 return pwc_vidioc_try_fmt(pdev, f);
965}
966
Hans de Goede885fe182011-06-06 15:33:44 -0300967static int pwc_reqbufs(struct file *file, void *fh,
968 struct v4l2_requestbuffers *rb)
Hans Verkuilafa38522011-01-22 06:34:55 -0300969{
Hans de Goede885fe182011-06-06 15:33:44 -0300970 struct pwc_device *pdev = video_drvdata(file);
Hans Verkuilafa38522011-01-22 06:34:55 -0300971
Hans de Goedec20d78c2011-10-09 09:16:46 -0300972 if (pwc_test_n_set_capt_file(pdev, file))
Hans de Goede4fba4712011-06-26 12:13:44 -0300973 return -EBUSY;
974
Hans de Goede885fe182011-06-06 15:33:44 -0300975 return vb2_reqbufs(&pdev->vb_queue, rb);
Hans Verkuilafa38522011-01-22 06:34:55 -0300976}
977
978static int pwc_querybuf(struct file *file, void *fh, struct v4l2_buffer *buf)
979{
980 struct pwc_device *pdev = video_drvdata(file);
Hans Verkuilafa38522011-01-22 06:34:55 -0300981
Hans de Goede885fe182011-06-06 15:33:44 -0300982 return vb2_querybuf(&pdev->vb_queue, buf);
Hans Verkuilafa38522011-01-22 06:34:55 -0300983}
984
985static int pwc_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
986{
Hans de Goede885fe182011-06-06 15:33:44 -0300987 struct pwc_device *pdev = video_drvdata(file);
Hans Verkuilafa38522011-01-22 06:34:55 -0300988
Hans de Goedeb824bb42011-06-25 17:39:19 -0300989 if (!pdev->udev)
990 return -ENODEV;
991
Hans de Goede4fba4712011-06-26 12:13:44 -0300992 if (pdev->capt_file != file)
993 return -EBUSY;
994
Hans de Goede885fe182011-06-06 15:33:44 -0300995 return vb2_qbuf(&pdev->vb_queue, buf);
Hans Verkuilafa38522011-01-22 06:34:55 -0300996}
997
998static int pwc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
999{
Hans Verkuilafa38522011-01-22 06:34:55 -03001000 struct pwc_device *pdev = video_drvdata(file);
Hans Verkuilafa38522011-01-22 06:34:55 -03001001
Hans de Goedeb824bb42011-06-25 17:39:19 -03001002 if (!pdev->udev)
1003 return -ENODEV;
1004
Hans de Goede4fba4712011-06-26 12:13:44 -03001005 if (pdev->capt_file != file)
1006 return -EBUSY;
1007
Hans de Goede885fe182011-06-06 15:33:44 -03001008 return vb2_dqbuf(&pdev->vb_queue, buf, file->f_flags & O_NONBLOCK);
Hans Verkuilafa38522011-01-22 06:34:55 -03001009}
1010
1011static int pwc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
1012{
1013 struct pwc_device *pdev = video_drvdata(file);
1014
Hans de Goedeb824bb42011-06-25 17:39:19 -03001015 if (!pdev->udev)
1016 return -ENODEV;
1017
Hans de Goede4fba4712011-06-26 12:13:44 -03001018 if (pdev->capt_file != file)
1019 return -EBUSY;
1020
Hans de Goede885fe182011-06-06 15:33:44 -03001021 return vb2_streamon(&pdev->vb_queue, i);
Hans Verkuilafa38522011-01-22 06:34:55 -03001022}
1023
1024static int pwc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
1025{
1026 struct pwc_device *pdev = video_drvdata(file);
1027
Hans de Goedeb824bb42011-06-25 17:39:19 -03001028 if (!pdev->udev)
1029 return -ENODEV;
1030
Hans de Goede4fba4712011-06-26 12:13:44 -03001031 if (pdev->capt_file != file)
1032 return -EBUSY;
1033
Hans de Goede885fe182011-06-06 15:33:44 -03001034 return vb2_streamoff(&pdev->vb_queue, i);
Hans Verkuilafa38522011-01-22 06:34:55 -03001035}
1036
1037static int pwc_enum_framesizes(struct file *file, void *fh,
1038 struct v4l2_frmsizeenum *fsize)
1039{
1040 struct pwc_device *pdev = video_drvdata(file);
1041 unsigned int i = 0, index = fsize->index;
1042
Hans de Goede795e6eb2012-01-04 16:58:44 -03001043 if (fsize->pixel_format == V4L2_PIX_FMT_YUV420 ||
1044 (fsize->pixel_format == V4L2_PIX_FMT_PWC1 &&
1045 DEVICE_USE_CODEC1(pdev->type)) ||
1046 (fsize->pixel_format == V4L2_PIX_FMT_PWC2 &&
1047 DEVICE_USE_CODEC23(pdev->type))) {
Hans Verkuilafa38522011-01-22 06:34:55 -03001048 for (i = 0; i < PSZ_MAX; i++) {
Hans de Goede795e6eb2012-01-04 16:58:44 -03001049 if (!(pdev->image_mask & (1UL << i)))
1050 continue;
1051 if (!index--) {
1052 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1053 fsize->discrete.width = pwc_image_sizes[i][0];
1054 fsize->discrete.height = pwc_image_sizes[i][1];
1055 return 0;
Hans Verkuilafa38522011-01-22 06:34:55 -03001056 }
1057 }
Hans Verkuilafa38522011-01-22 06:34:55 -03001058 }
1059 return -EINVAL;
1060}
1061
1062static int pwc_enum_frameintervals(struct file *file, void *fh,
1063 struct v4l2_frmivalenum *fival)
1064{
1065 struct pwc_device *pdev = video_drvdata(file);
1066 int size = -1;
1067 unsigned int i;
1068
1069 for (i = 0; i < PSZ_MAX; i++) {
Hans de Goede795e6eb2012-01-04 16:58:44 -03001070 if (pwc_image_sizes[i][0] == fival->width &&
1071 pwc_image_sizes[i][1] == fival->height) {
Hans Verkuilafa38522011-01-22 06:34:55 -03001072 size = i;
1073 break;
1074 }
1075 }
1076
1077 /* TODO: Support raw format */
1078 if (size < 0 || fival->pixel_format != V4L2_PIX_FMT_YUV420)
1079 return -EINVAL;
1080
1081 i = pwc_get_fps(pdev, fival->index, size);
1082 if (!i)
1083 return -EINVAL;
1084
1085 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1086 fival->discrete.numerator = 1;
1087 fival->discrete.denominator = i;
1088
1089 return 0;
1090}
1091
Hans de Goede54d3fb32012-01-08 07:19:29 -03001092static int pwc_g_parm(struct file *file, void *fh,
1093 struct v4l2_streamparm *parm)
1094{
1095 struct pwc_device *pdev = video_drvdata(file);
1096
1097 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1098 return -EINVAL;
1099
1100 memset(parm, 0, sizeof(*parm));
1101
1102 parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1103 parm->parm.capture.readbuffers = MIN_FRAMES;
1104 parm->parm.capture.capability |= V4L2_CAP_TIMEPERFRAME;
1105 parm->parm.capture.timeperframe.denominator = pdev->vframes;
1106 parm->parm.capture.timeperframe.numerator = 1;
1107
1108 return 0;
1109}
1110
1111static int pwc_s_parm(struct file *file, void *fh,
1112 struct v4l2_streamparm *parm)
1113{
1114 struct pwc_device *pdev = video_drvdata(file);
1115 int compression = 0;
1116 int ret, fps;
1117
1118 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1119 parm->parm.capture.timeperframe.numerator == 0)
1120 return -EINVAL;
1121
1122 if (pwc_test_n_set_capt_file(pdev, file))
1123 return -EBUSY;
1124
1125 fps = parm->parm.capture.timeperframe.denominator /
1126 parm->parm.capture.timeperframe.numerator;
1127
1128 mutex_lock(&pdev->udevlock);
1129 if (!pdev->udev) {
1130 ret = -ENODEV;
1131 goto leave;
1132 }
1133
1134 if (pdev->iso_init) {
1135 ret = -EBUSY;
1136 goto leave;
1137 }
1138
Hans de Goeded167a852012-01-10 13:01:41 -03001139 ret = pwc_set_video_mode(pdev, pdev->width, pdev->height, pdev->pixfmt,
Hans de Goede938d5b92012-01-10 13:14:46 -03001140 fps, &compression, 0);
Hans de Goede54d3fb32012-01-08 07:19:29 -03001141
1142 pwc_g_parm(file, fh, parm);
1143
1144leave:
1145 mutex_unlock(&pdev->udevlock);
1146 return ret;
1147}
1148
Hans Verkuil34ebdc92011-08-26 08:46:13 -03001149static int pwc_log_status(struct file *file, void *priv)
1150{
1151 struct pwc_device *pdev = video_drvdata(file);
1152
1153 v4l2_ctrl_handler_log_status(&pdev->ctrl_handler, PWC_NAME);
1154 return 0;
1155}
1156
Hans Verkuilafa38522011-01-22 06:34:55 -03001157const struct v4l2_ioctl_ops pwc_ioctl_ops = {
1158 .vidioc_querycap = pwc_querycap,
1159 .vidioc_enum_input = pwc_enum_input,
1160 .vidioc_g_input = pwc_g_input,
1161 .vidioc_s_input = pwc_s_input,
1162 .vidioc_enum_fmt_vid_cap = pwc_enum_fmt_vid_cap,
1163 .vidioc_g_fmt_vid_cap = pwc_g_fmt_vid_cap,
1164 .vidioc_s_fmt_vid_cap = pwc_s_fmt_vid_cap,
1165 .vidioc_try_fmt_vid_cap = pwc_try_fmt_vid_cap,
Hans Verkuilafa38522011-01-22 06:34:55 -03001166 .vidioc_reqbufs = pwc_reqbufs,
1167 .vidioc_querybuf = pwc_querybuf,
1168 .vidioc_qbuf = pwc_qbuf,
1169 .vidioc_dqbuf = pwc_dqbuf,
1170 .vidioc_streamon = pwc_streamon,
1171 .vidioc_streamoff = pwc_streamoff,
Hans Verkuil34ebdc92011-08-26 08:46:13 -03001172 .vidioc_log_status = pwc_log_status,
Hans Verkuilafa38522011-01-22 06:34:55 -03001173 .vidioc_enum_framesizes = pwc_enum_framesizes,
1174 .vidioc_enum_frameintervals = pwc_enum_frameintervals,
Hans de Goede54d3fb32012-01-08 07:19:29 -03001175 .vidioc_g_parm = pwc_g_parm,
1176 .vidioc_s_parm = pwc_s_parm,
Hans Verkuilafa38522011-01-22 06:34:55 -03001177};