blob: 5eddfab920eaaa5896b22b182a95b1264538c90e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Driver for Philips webcam
2 Functions that send various control messages to the webcam, including
3 video modes.
4 (C) 1999-2003 Nemosoft Unv.
Luc Saillard2b455db2006-04-24 10:29:46 -03005 (C) 2004-2006 Luc Saillard (luc@saillard.org)
Hans de Goede6c9cac82011-06-26 12:52:01 -03006 (C) 2011 Hans de Goede <hdegoede@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007
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
12 NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
13 driver and thus may have bugs that are not present in the original version.
14 Please send bug reports and support requests to <luc@saillard.org>.
15 The decompression routines have been implemented by reverse-engineering the
16 Nemosoft binary pwcx module. Caveat emptor.
17
18 This program is free software; you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation; either version 2 of the License, or
21 (at your option) any later version.
22
23 This program is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
27
28 You should have received a copy of the GNU General Public License
29 along with this program; if not, write to the Free Software
30 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31*/
32
33/*
34 Changes
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030035 2001/08/03 Alvarado Added methods for changing white balance and
36 red/green gains
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 */
38
39/* Control functions for the cam; brightness, contrast, video mode, etc. */
40
41#ifdef __KERNEL__
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030042#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#endif
44#include <asm/errno.h>
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include "pwc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include "pwc-kiara.h"
48#include "pwc-timon.h"
Luc Saillard2b455db2006-04-24 10:29:46 -030049#include "pwc-dec1.h"
50#include "pwc-dec23.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Hans de Goede6c9cac82011-06-26 12:52:01 -030052/* Selectors for status controls used only in this file */
Luc Saillard2b455db2006-04-24 10:29:46 -030053#define GET_STATUS_B00 0x0B00
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#define SENSOR_TYPE_FORMATTER1 0x0C00
Luc Saillard2b455db2006-04-24 10:29:46 -030055#define GET_STATUS_3000 0x3000
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#define READ_RAW_Y_MEAN_FORMATTER 0x3100
57#define SET_POWER_SAVE_MODE_FORMATTER 0x3200
58#define MIRROR_IMAGE_FORMATTER 0x3300
59#define LED_FORMATTER 0x3400
Luc Saillard2b455db2006-04-24 10:29:46 -030060#define LOWLIGHT 0x3500
61#define GET_STATUS_3600 0x3600
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#define SENSOR_TYPE_FORMATTER2 0x3700
Luc Saillard2b455db2006-04-24 10:29:46 -030063#define GET_STATUS_3800 0x3800
64#define GET_STATUS_4000 0x4000
65#define GET_STATUS_4100 0x4100 /* Get */
66#define CTL_STATUS_4200 0x4200 /* [GS] 1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68/* Formatters for the Video Endpoint controls [GS]ET_EP_STREAM_CTL */
69#define VIDEO_OUTPUT_CONTROL_FORMATTER 0x0100
70
Arjan van de Ven4c4c9432005-11-29 09:43:42 +010071static const char *size2name[PSZ_MAX] =
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
73 "subQCIF",
74 "QSIF",
75 "QCIF",
76 "SIF",
77 "CIF",
78 "VGA",
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030079};
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81/********/
82
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030083/* Entries for the Nala (645/646) camera; the Nala doesn't have compression
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 preferences, so you either get compressed or non-compressed streams.
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030085
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 An alternate value of 0 means this mode is not available at all.
87 */
88
Luc Saillard9ee6d782007-04-22 23:54:36 -030089#define PWC_FPS_MAX_NALA 8
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091struct Nala_table_entry {
92 char alternate; /* USB alternate setting */
93 int compressed; /* Compressed yes/no */
94
95 unsigned char mode[3]; /* precomputed mode table */
96};
97
Luc Saillard9ee6d782007-04-22 23:54:36 -030098static unsigned int Nala_fps_vector[PWC_FPS_MAX_NALA] = { 4, 5, 7, 10, 12, 15, 20, 24 };
99
100static struct Nala_table_entry Nala_table[PSZ_MAX][PWC_FPS_MAX_NALA] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
102#include "pwc-nala.h"
103};
104
Luc Saillard2b455db2006-04-24 10:29:46 -0300105static void pwc_set_image_buffer_size(struct pwc_device *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107/****************************************************************************/
108
Martin Fuzzey6b35ca02009-04-21 21:48:09 +0200109static int _send_control_msg(struct pwc_device *pdev,
Hans de Goede6c9cac82011-06-26 12:52:01 -0300110 u8 request, u16 value, int index, void *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Martin Fuzzey6b35ca02009-04-21 21:48:09 +0200112 int rc;
113 void *kbuf = NULL;
114
115 if (buflen) {
Thomas Meyer6d00c9a2011-11-17 18:43:40 -0300116 kbuf = kmemdup(buf, buflen, GFP_KERNEL); /* not allowed on stack */
Martin Fuzzey6b35ca02009-04-21 21:48:09 +0200117 if (kbuf == NULL)
118 return -ENOMEM;
Martin Fuzzey6b35ca02009-04-21 21:48:09 +0200119 }
120
121 rc = usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
122 request,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
Martin Fuzzey6b35ca02009-04-21 21:48:09 +0200124 value,
125 index,
Hans de Goede6c9cac82011-06-26 12:52:01 -0300126 kbuf, buflen, USB_CTRL_SET_TIMEOUT);
Martin Fuzzey6b35ca02009-04-21 21:48:09 +0200127
128 kfree(kbuf);
129 return rc;
130}
131
132static int recv_control_msg(struct pwc_device *pdev,
133 u8 request, u16 value, void *buf, int buflen)
134{
135 int rc;
136 void *kbuf = kmalloc(buflen, GFP_KERNEL); /* not allowed on stack */
137
138 if (kbuf == NULL)
139 return -ENOMEM;
140
141 rc = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
142 request,
143 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
144 value,
145 pdev->vcinterface,
Hans de Goede6c9cac82011-06-26 12:52:01 -0300146 kbuf, buflen, USB_CTRL_GET_TIMEOUT);
Martin Fuzzey6b35ca02009-04-21 21:48:09 +0200147 memcpy(buf, kbuf, buflen);
148 kfree(kbuf);
Hans de Goede6c9cac82011-06-26 12:52:01 -0300149
150 if (rc < 0)
151 PWC_ERROR("recv_control_msg error %d req %02x val %04x\n",
152 rc, request, value);
Martin Fuzzey6b35ca02009-04-21 21:48:09 +0200153 return rc;
154}
155
156static inline int send_video_command(struct pwc_device *pdev,
157 int index, void *buf, int buflen)
158{
159 return _send_control_msg(pdev,
160 SET_EP_STREAM_CTL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 VIDEO_OUTPUT_CONTROL_FORMATTER,
162 index,
Hans de Goede6c9cac82011-06-26 12:52:01 -0300163 buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
Hans de Goede294e2892011-07-03 12:23:24 -0300166int send_control_msg(struct pwc_device *pdev,
Martin Fuzzey6b35ca02009-04-21 21:48:09 +0200167 u8 request, u16 value, void *buf, int buflen)
168{
169 return _send_control_msg(pdev,
Hans de Goede6c9cac82011-06-26 12:52:01 -0300170 request, value, pdev->vcinterface, buf, buflen);
Martin Fuzzey6b35ca02009-04-21 21:48:09 +0200171}
172
Luc Saillard2b455db2006-04-24 10:29:46 -0300173static int set_video_mode_Nala(struct pwc_device *pdev, int size, int frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
175 unsigned char buf[3];
176 int ret, fps;
177 struct Nala_table_entry *pEntry;
178 int frames2frames[31] =
179 { /* closest match of framerate */
180 0, 0, 0, 0, 4, /* 0-4 */
181 5, 5, 7, 7, 10, /* 5-9 */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300182 10, 10, 12, 12, 15, /* 10-14 */
183 15, 15, 15, 20, 20, /* 15-19 */
184 20, 20, 20, 24, 24, /* 20-24 */
185 24, 24, 24, 24, 24, /* 25-29 */
186 24 /* 30 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 };
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300188 int frames2table[31] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 { 0, 0, 0, 0, 0, /* 0-4 */
190 1, 1, 1, 2, 2, /* 5-9 */
191 3, 3, 4, 4, 4, /* 10-14 */
192 5, 5, 5, 5, 5, /* 15-19 */
193 6, 6, 6, 6, 7, /* 20-24 */
194 7, 7, 7, 7, 7, /* 25-29 */
195 7 /* 30 */
196 };
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (size < 0 || size > PSZ_CIF || frames < 4 || frames > 25)
199 return -EINVAL;
200 frames = frames2frames[frames];
201 fps = frames2table[frames];
202 pEntry = &Nala_table[size][fps];
203 if (pEntry->alternate == 0)
204 return -EINVAL;
205
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300206 memcpy(buf, pEntry->mode, 3);
Martin Fuzzey6b35ca02009-04-21 21:48:09 +0200207 ret = send_video_command(pdev, pdev->vendpoint, buf, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (ret < 0) {
Luc Saillard2b455db2006-04-24 10:29:46 -0300209 PWC_DEBUG_MODULE("Failed to send video command... %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 return ret;
211 }
Hans de Goede6eba9352011-06-26 06:49:59 -0300212 if (pEntry->compressed && pdev->pixfmt == V4L2_PIX_FMT_YUV420) {
213 ret = pwc_dec1_init(pdev, pdev->type, pdev->release, buf);
214 if (ret < 0)
215 return ret;
216 }
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 pdev->cmd_len = 3;
219 memcpy(pdev->cmd_buf, buf, 3);
220
221 /* Set various parameters */
222 pdev->vframes = frames;
223 pdev->vsize = size;
224 pdev->valternate = pEntry->alternate;
225 pdev->image = pwc_image_sizes[size];
226 pdev->frame_size = (pdev->image.x * pdev->image.y * 3) / 2;
227 if (pEntry->compressed) {
228 if (pdev->release < 5) { /* 4 fold compression */
229 pdev->vbandlength = 528;
230 pdev->frame_size /= 4;
231 }
232 else {
233 pdev->vbandlength = 704;
234 pdev->frame_size /= 3;
235 }
236 }
237 else
238 pdev->vbandlength = 0;
239 return 0;
240}
241
242
Luc Saillard2b455db2006-04-24 10:29:46 -0300243static int set_video_mode_Timon(struct pwc_device *pdev, int size, int frames, int compression, int snapshot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 unsigned char buf[13];
246 const struct Timon_table_entry *pChoose;
247 int ret, fps;
248
249 if (size >= PSZ_MAX || frames < 5 || frames > 30 || compression < 0 || compression > 3)
250 return -EINVAL;
251 if (size == PSZ_VGA && frames > 15)
252 return -EINVAL;
253 fps = (frames / 5) - 1;
254
255 /* Find a supported framerate with progressively higher compression ratios
256 if the preferred ratio is not available.
257 */
258 pChoose = NULL;
259 while (compression <= 3) {
260 pChoose = &Timon_table[size][fps][compression];
261 if (pChoose->alternate != 0)
262 break;
263 compression++;
264 }
265 if (pChoose == NULL || pChoose->alternate == 0)
266 return -ENOENT; /* Not supported. */
267
268 memcpy(buf, pChoose->mode, 13);
269 if (snapshot)
270 buf[0] |= 0x80;
Martin Fuzzey6b35ca02009-04-21 21:48:09 +0200271 ret = send_video_command(pdev, pdev->vendpoint, buf, 13);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 if (ret < 0)
273 return ret;
274
Hans de Goede6eba9352011-06-26 06:49:59 -0300275 if (pChoose->bandlength > 0 && pdev->pixfmt == V4L2_PIX_FMT_YUV420) {
276 ret = pwc_dec23_init(pdev, pdev->type, buf);
277 if (ret < 0)
278 return ret;
279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 pdev->cmd_len = 13;
282 memcpy(pdev->cmd_buf, buf, 13);
283
284 /* Set various parameters */
285 pdev->vframes = frames;
286 pdev->vsize = size;
287 pdev->vsnapshot = snapshot;
288 pdev->valternate = pChoose->alternate;
289 pdev->image = pwc_image_sizes[size];
290 pdev->vbandlength = pChoose->bandlength;
291 if (pChoose->bandlength > 0)
292 pdev->frame_size = (pChoose->bandlength * pdev->image.y) / 4;
293 else
294 pdev->frame_size = (pdev->image.x * pdev->image.y * 12) / 8;
295 return 0;
296}
297
298
Luc Saillard2b455db2006-04-24 10:29:46 -0300299static int set_video_mode_Kiara(struct pwc_device *pdev, int size, int frames, int compression, int snapshot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
301 const struct Kiara_table_entry *pChoose = NULL;
302 int fps, ret;
303 unsigned char buf[12];
304 struct Kiara_table_entry RawEntry = {6, 773, 1272, {0xAD, 0xF4, 0x10, 0x27, 0xB6, 0x24, 0x96, 0x02, 0x30, 0x05, 0x03, 0x80}};
305
306 if (size >= PSZ_MAX || frames < 5 || frames > 30 || compression < 0 || compression > 3)
307 return -EINVAL;
308 if (size == PSZ_VGA && frames > 15)
309 return -EINVAL;
310 fps = (frames / 5) - 1;
311
312 /* special case: VGA @ 5 fps and snapshot is raw bayer mode */
Hans Verkuil479567c2010-09-12 17:05:11 -0300313 if (size == PSZ_VGA && frames == 5 && snapshot && pdev->pixfmt != V4L2_PIX_FMT_YUV420)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 {
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300315 /* Only available in case the raw palette is selected or
316 we have the decompressor available. This mode is
317 only available in compressed form
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 */
Luc Saillard2b455db2006-04-24 10:29:46 -0300319 PWC_DEBUG_SIZE("Choosing VGA/5 BAYER mode.\n");
320 pChoose = &RawEntry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
322 else
323 {
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300324 /* Find a supported framerate with progressively higher compression ratios
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 if the preferred ratio is not available.
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300326 Skip this step when using RAW modes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 */
Luc Saillard2b455db2006-04-24 10:29:46 -0300328 snapshot = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 while (compression <= 3) {
330 pChoose = &Kiara_table[size][fps][compression];
331 if (pChoose->alternate != 0)
332 break;
333 compression++;
334 }
335 }
336 if (pChoose == NULL || pChoose->alternate == 0)
337 return -ENOENT; /* Not supported. */
338
Luc Saillard2b455db2006-04-24 10:29:46 -0300339 PWC_TRACE("Using alternate setting %d.\n", pChoose->alternate);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 /* usb_control_msg won't take staticly allocated arrays as argument?? */
342 memcpy(buf, pChoose->mode, 12);
343 if (snapshot)
344 buf[0] |= 0x80;
345
346 /* Firmware bug: video endpoint is 5, but commands are sent to endpoint 4 */
Martin Fuzzey6b35ca02009-04-21 21:48:09 +0200347 ret = send_video_command(pdev, 4 /* pdev->vendpoint */, buf, 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (ret < 0)
349 return ret;
350
Hans de Goede6eba9352011-06-26 06:49:59 -0300351 if (pChoose->bandlength > 0 && pdev->pixfmt == V4L2_PIX_FMT_YUV420) {
352 ret = pwc_dec23_init(pdev, pdev->type, buf);
353 if (ret < 0)
354 return ret;
355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 pdev->cmd_len = 12;
358 memcpy(pdev->cmd_buf, buf, 12);
359 /* All set and go */
360 pdev->vframes = frames;
361 pdev->vsize = size;
362 pdev->vsnapshot = snapshot;
363 pdev->valternate = pChoose->alternate;
364 pdev->image = pwc_image_sizes[size];
365 pdev->vbandlength = pChoose->bandlength;
366 if (pdev->vbandlength > 0)
367 pdev->frame_size = (pdev->vbandlength * pdev->image.y) / 4;
368 else
369 pdev->frame_size = (pdev->image.x * pdev->image.y * 12) / 8;
Luc Saillard2b455db2006-04-24 10:29:46 -0300370 PWC_TRACE("frame_size=%d, vframes=%d, vsize=%d, vsnapshot=%d, vbandlength=%d\n",
371 pdev->frame_size,pdev->vframes,pdev->vsize,pdev->vsnapshot,pdev->vbandlength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return 0;
373}
374
375
376
377/**
378 @pdev: device structure
379 @width: viewport width
380 @height: viewport height
381 @frame: framerate, in fps
382 @compression: preferred compression ratio
383 @snapshot: snapshot mode or streaming
384 */
385int pwc_set_video_mode(struct pwc_device *pdev, int width, int height, int frames, int compression, int snapshot)
386{
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300387 int ret, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Hans Verkuil479567c2010-09-12 17:05:11 -0300389 PWC_DEBUG_FLOW("set_video_mode(%dx%d @ %d, pixfmt %08x).\n", width, height, frames, pdev->pixfmt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 size = pwc_decode_size(pdev, width, height);
391 if (size < 0) {
Luc Saillard2b455db2006-04-24 10:29:46 -0300392 PWC_DEBUG_MODULE("Could not find suitable size.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 return -ERANGE;
394 }
Luc Saillard2b455db2006-04-24 10:29:46 -0300395 PWC_TRACE("decode_size = %d.\n", size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Luc Saillard2b455db2006-04-24 10:29:46 -0300397 if (DEVICE_USE_CODEC1(pdev->type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 ret = set_video_mode_Nala(pdev, size, frames);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Luc Saillard2b455db2006-04-24 10:29:46 -0300400 } else if (DEVICE_USE_CODEC3(pdev->type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 ret = set_video_mode_Kiara(pdev, size, frames, compression, snapshot);
Luc Saillard2b455db2006-04-24 10:29:46 -0300402
403 } else {
404 ret = set_video_mode_Timon(pdev, size, frames, compression, snapshot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
406 if (ret < 0) {
Luc Saillard2b455db2006-04-24 10:29:46 -0300407 PWC_ERROR("Failed to set video mode %s@%d fps; return code = %d\n", size2name[size], frames, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return ret;
409 }
410 pdev->view.x = width;
411 pdev->view.y = height;
Hans de Goede6eba9352011-06-26 06:49:59 -0300412 pdev->vcompression = compression;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 pdev->frame_total_size = pdev->frame_size + pdev->frame_header_size + pdev->frame_trailer_size;
414 pwc_set_image_buffer_size(pdev);
Luc Saillard2b455db2006-04-24 10:29:46 -0300415 PWC_DEBUG_SIZE("Set viewport to %dx%d, image size is %dx%d.\n", width, height, pwc_image_sizes[size].x, pwc_image_sizes[size].y);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return 0;
417}
418
Luc Saillard9ee6d782007-04-22 23:54:36 -0300419static unsigned int pwc_get_fps_Nala(struct pwc_device *pdev, unsigned int index, unsigned int size)
420{
421 unsigned int i;
422
423 for (i = 0; i < PWC_FPS_MAX_NALA; i++) {
424 if (Nala_table[size][i].alternate) {
425 if (index--==0) return Nala_fps_vector[i];
426 }
427 }
428 return 0;
429}
430
431static unsigned int pwc_get_fps_Kiara(struct pwc_device *pdev, unsigned int index, unsigned int size)
432{
433 unsigned int i;
434
435 for (i = 0; i < PWC_FPS_MAX_KIARA; i++) {
436 if (Kiara_table[size][i][3].alternate) {
437 if (index--==0) return Kiara_fps_vector[i];
438 }
439 }
440 return 0;
441}
442
443static unsigned int pwc_get_fps_Timon(struct pwc_device *pdev, unsigned int index, unsigned int size)
444{
445 unsigned int i;
446
447 for (i=0; i < PWC_FPS_MAX_TIMON; i++) {
448 if (Timon_table[size][i][3].alternate) {
449 if (index--==0) return Timon_fps_vector[i];
450 }
451 }
452 return 0;
453}
454
455unsigned int pwc_get_fps(struct pwc_device *pdev, unsigned int index, unsigned int size)
456{
457 unsigned int ret;
458
459 if (DEVICE_USE_CODEC1(pdev->type)) {
460 ret = pwc_get_fps_Nala(pdev, index, size);
461
462 } else if (DEVICE_USE_CODEC3(pdev->type)) {
463 ret = pwc_get_fps_Kiara(pdev, index, size);
464
465 } else {
466 ret = pwc_get_fps_Timon(pdev, index, size);
467 }
468
469 return ret;
470}
471
Luc Saillard2b455db2006-04-24 10:29:46 -0300472static void pwc_set_image_buffer_size(struct pwc_device *pdev)
473{
Hans de Goede885fe182011-06-06 15:33:44 -0300474 int factor = 0;
Luc Saillard2b455db2006-04-24 10:29:46 -0300475
Hans Verkuil479567c2010-09-12 17:05:11 -0300476 /* for V4L2_PIX_FMT_YUV420 */
477 switch (pdev->pixfmt) {
478 case V4L2_PIX_FMT_YUV420:
Luc Saillard2b455db2006-04-24 10:29:46 -0300479 factor = 6;
480 break;
Hans Verkuil479567c2010-09-12 17:05:11 -0300481 case V4L2_PIX_FMT_PWC1:
482 case V4L2_PIX_FMT_PWC2:
Luc Saillard2b455db2006-04-24 10:29:46 -0300483 factor = 6; /* can be uncompressed YUV420P */
484 break;
485 }
486
487 /* Set sizes in bytes */
488 pdev->image.size = pdev->image.x * pdev->image.y * factor / 4;
489 pdev->view.size = pdev->view.x * pdev->view.y * factor / 4;
490
491 /* Align offset, or you'll get some very weird results in
492 YUV420 mode... x must be multiple of 4 (to get the Y's in
493 place), and y even (or you'll mixup U & V). This is less of a
494 problem for YUV420P.
495 */
496 pdev->offset.x = ((pdev->view.x - pdev->image.x) / 2) & 0xFFFC;
497 pdev->offset.y = ((pdev->view.y - pdev->image.y) / 2) & 0xFFFE;
Luc Saillard2b455db2006-04-24 10:29:46 -0300498}
499
Hans de Goede6c9cac82011-06-26 12:52:01 -0300500int pwc_get_u8_ctrl(struct pwc_device *pdev, u8 request, u16 value, int *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 int ret;
Hans de Goede6c9cac82011-06-26 12:52:01 -0300503 u8 buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Hans de Goede6c9cac82011-06-26 12:52:01 -0300505 ret = recv_control_msg(pdev, request, value, &buf, sizeof(buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if (ret < 0)
507 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Hans de Goede6c9cac82011-06-26 12:52:01 -0300509 *data = buf;
Luc Saillard2b455db2006-04-24 10:29:46 -0300510 return 0;
511}
512
Hans de Goede6c9cac82011-06-26 12:52:01 -0300513int pwc_set_u8_ctrl(struct pwc_device *pdev, u8 request, u16 value, u8 data)
Luc Saillard2b455db2006-04-24 10:29:46 -0300514{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 int ret;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300516
Hans de Goede6c9cac82011-06-26 12:52:01 -0300517 ret = send_control_msg(pdev, request, value, &data, sizeof(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 if (ret < 0)
519 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 return 0;
522}
523
Hans de Goede6c9cac82011-06-26 12:52:01 -0300524int pwc_get_s8_ctrl(struct pwc_device *pdev, u8 request, u16 value, int *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
Luc Saillard2b455db2006-04-24 10:29:46 -0300526 int ret;
Hans de Goede6c9cac82011-06-26 12:52:01 -0300527 s8 buf;
Luc Saillard2b455db2006-04-24 10:29:46 -0300528
Hans de Goede6c9cac82011-06-26 12:52:01 -0300529 ret = recv_control_msg(pdev, request, value, &buf, sizeof(buf));
Luc Saillard2b455db2006-04-24 10:29:46 -0300530 if (ret < 0)
531 return ret;
Hans de Goede6c9cac82011-06-26 12:52:01 -0300532
533 *data = buf;
534 return 0;
535}
536
537int pwc_get_u16_ctrl(struct pwc_device *pdev, u8 request, u16 value, int *data)
538{
539 int ret;
540 u8 buf[2];
541
542 ret = recv_control_msg(pdev, request, value, buf, sizeof(buf));
543 if (ret < 0)
544 return ret;
545
546 *data = (buf[1] << 8) | buf[0];
547 return 0;
548}
549
550int pwc_set_u16_ctrl(struct pwc_device *pdev, u8 request, u16 value, u16 data)
551{
552 int ret;
553 u8 buf[2];
554
555 buf[0] = data & 0xff;
556 buf[1] = data >> 8;
557 ret = send_control_msg(pdev, request, value, buf, sizeof(buf));
558 if (ret < 0)
559 return ret;
560
561 return 0;
562}
563
564int pwc_button_ctrl(struct pwc_device *pdev, u16 value)
565{
566 int ret;
567
568 ret = send_control_msg(pdev, SET_STATUS_CTL, value, NULL, 0);
569 if (ret < 0)
570 return ret;
571
Luc Saillard2b455db2006-04-24 10:29:46 -0300572 return 0;
573}
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575/* POWER */
Hans de Goede3b4d0ec2011-06-26 03:51:19 -0300576void pwc_camera_power(struct pwc_device *pdev, int power)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
578 char buf;
Hans de Goede3b4d0ec2011-06-26 03:51:19 -0300579 int r;
580
581 if (!pdev->power_save)
582 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 if (pdev->type < 675 || (pdev->type < 730 && pdev->release < 6))
Hans de Goede3b4d0ec2011-06-26 03:51:19 -0300585 return; /* Not supported by Nala or Timon < release 6 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 if (power)
588 buf = 0x00; /* active */
589 else
590 buf = 0xFF; /* power save */
Hans de Goede3b4d0ec2011-06-26 03:51:19 -0300591 r = send_control_msg(pdev,
Martin Fuzzey6b35ca02009-04-21 21:48:09 +0200592 SET_STATUS_CTL, SET_POWER_SAVE_MODE_FORMATTER,
593 &buf, sizeof(buf));
Hans de Goede3b4d0ec2011-06-26 03:51:19 -0300594
595 if (r < 0)
596 PWC_ERROR("Failed to power %s camera (%d)\n",
597 power ? "on" : "off", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598}
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600int pwc_set_leds(struct pwc_device *pdev, int on_value, int off_value)
601{
602 unsigned char buf[2];
Hans de Goede3b4d0ec2011-06-26 03:51:19 -0300603 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 if (pdev->type < 730)
606 return 0;
607 on_value /= 100;
608 off_value /= 100;
609 if (on_value < 0)
610 on_value = 0;
611 if (on_value > 0xff)
612 on_value = 0xff;
613 if (off_value < 0)
614 off_value = 0;
615 if (off_value > 0xff)
616 off_value = 0xff;
617
618 buf[0] = on_value;
619 buf[1] = off_value;
620
Hans de Goede3b4d0ec2011-06-26 03:51:19 -0300621 r = send_control_msg(pdev,
Martin Fuzzey6b35ca02009-04-21 21:48:09 +0200622 SET_STATUS_CTL, LED_FORMATTER, &buf, sizeof(buf));
Hans de Goede3b4d0ec2011-06-26 03:51:19 -0300623 if (r < 0)
624 PWC_ERROR("Failed to set LED on/off time (%d)\n", r);
625
626 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628
Adrian Bunkb20c3cf2006-06-23 06:49:34 -0300629static int pwc_get_leds(struct pwc_device *pdev, int *on_value, int *off_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
631 unsigned char buf[2];
632 int ret;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 if (pdev->type < 730) {
635 *on_value = -1;
636 *off_value = -1;
637 return 0;
638 }
639
Martin Fuzzey6b35ca02009-04-21 21:48:09 +0200640 ret = recv_control_msg(pdev,
641 GET_STATUS_CTL, LED_FORMATTER, &buf, sizeof(buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 if (ret < 0)
643 return ret;
644 *on_value = buf[0] * 100;
645 *off_value = buf[1] * 100;
646 return 0;
647}
648
Luc Saillard2b455db2006-04-24 10:29:46 -0300649static int _pwc_mpt_reset(struct pwc_device *pdev, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
651 unsigned char buf;
Hans de Goedec20d78c2011-10-09 09:16:46 -0300652 int r;
653
654 mutex_lock(&pdev->udevlock);
655 if (!pdev->udev) {
656 r = -ENODEV;
657 goto leave;
658 }
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 buf = flags & 0x03; // only lower two bits are currently used
Hans de Goedec20d78c2011-10-09 09:16:46 -0300661 r = send_control_msg(pdev,
Martin Fuzzey6b35ca02009-04-21 21:48:09 +0200662 SET_MPT_CTL, PT_RESET_CONTROL_FORMATTER, &buf, sizeof(buf));
Hans de Goedec20d78c2011-10-09 09:16:46 -0300663leave:
664 mutex_unlock(&pdev->udevlock);
665 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667
Luc Saillard2b455db2006-04-24 10:29:46 -0300668int pwc_mpt_reset(struct pwc_device *pdev, int flags)
669{
670 int ret;
671 ret = _pwc_mpt_reset(pdev, flags);
672 if (ret >= 0) {
673 pdev->pan_angle = 0;
674 pdev->tilt_angle = 0;
675 }
676 return ret;
677}
678
679static int _pwc_mpt_set_angle(struct pwc_device *pdev, int pan, int tilt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
681 unsigned char buf[4];
Hans de Goedec20d78c2011-10-09 09:16:46 -0300682 int r;
683
684 mutex_lock(&pdev->udevlock);
685 if (!pdev->udev) {
686 r = -ENODEV;
687 goto leave;
688 }
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 /* set new relative angle; angles are expressed in degrees * 100,
Steven Cole093cf722005-05-03 19:07:24 -0600691 but cam as .5 degree resolution, hence divide by 200. Also
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 the angle must be multiplied by 64 before it's send to
693 the cam (??)
694 */
695 pan = 64 * pan / 100;
696 tilt = -64 * tilt / 100; /* positive tilt is down, which is not what the user would expect */
697 buf[0] = pan & 0xFF;
698 buf[1] = (pan >> 8) & 0xFF;
699 buf[2] = tilt & 0xFF;
700 buf[3] = (tilt >> 8) & 0xFF;
Hans de Goedec20d78c2011-10-09 09:16:46 -0300701 r = send_control_msg(pdev,
Martin Fuzzey6b35ca02009-04-21 21:48:09 +0200702 SET_MPT_CTL, PT_RELATIVE_CONTROL_FORMATTER, &buf, sizeof(buf));
Hans de Goedec20d78c2011-10-09 09:16:46 -0300703leave:
704 mutex_unlock(&pdev->udevlock);
705 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
Luc Saillard2b455db2006-04-24 10:29:46 -0300708int pwc_mpt_set_angle(struct pwc_device *pdev, int pan, int tilt)
709{
710 int ret;
711
712 /* check absolute ranges */
713 if (pan < pdev->angle_range.pan_min ||
714 pan > pdev->angle_range.pan_max ||
715 tilt < pdev->angle_range.tilt_min ||
716 tilt > pdev->angle_range.tilt_max)
717 return -ERANGE;
718
719 /* go to relative range, check again */
720 pan -= pdev->pan_angle;
721 tilt -= pdev->tilt_angle;
722 /* angles are specified in degrees * 100, thus the limit = 36000 */
723 if (pan < -36000 || pan > 36000 || tilt < -36000 || tilt > 36000)
724 return -ERANGE;
725
726 ret = _pwc_mpt_set_angle(pdev, pan, tilt);
727 if (ret >= 0) {
728 pdev->pan_angle += pan;
729 pdev->tilt_angle += tilt;
730 }
731 if (ret == -EPIPE) /* stall -> out of range */
732 ret = -ERANGE;
733 return ret;
734}
735
736static int pwc_mpt_get_status(struct pwc_device *pdev, struct pwc_mpt_status *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
738 int ret;
739 unsigned char buf[5];
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300740
Hans de Goedec20d78c2011-10-09 09:16:46 -0300741 mutex_lock(&pdev->udevlock);
742 if (!pdev->udev) {
743 ret = -ENODEV;
744 goto leave;
745 }
746
Martin Fuzzey6b35ca02009-04-21 21:48:09 +0200747 ret = recv_control_msg(pdev,
748 GET_MPT_CTL, PT_STATUS_FORMATTER, &buf, sizeof(buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 if (ret < 0)
Hans de Goedec20d78c2011-10-09 09:16:46 -0300750 goto leave;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 status->status = buf[0] & 0x7; // 3 bits are used for reporting
752 status->time_pan = (buf[1] << 8) + buf[2];
753 status->time_tilt = (buf[3] << 8) + buf[4];
Hans de Goedec20d78c2011-10-09 09:16:46 -0300754leave:
755 mutex_unlock(&pdev->udevlock);
756 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
Hans de Goede6eba9352011-06-26 06:49:59 -0300759#ifdef CONFIG_USB_PWC_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760int pwc_get_cmos_sensor(struct pwc_device *pdev, int *sensor)
761{
762 unsigned char buf;
763 int ret = -1, request;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 if (pdev->type < 675)
766 request = SENSOR_TYPE_FORMATTER1;
767 else if (pdev->type < 730)
768 return -1; /* The Vesta series doesn't have this call */
769 else
770 request = SENSOR_TYPE_FORMATTER2;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300771
Martin Fuzzey6b35ca02009-04-21 21:48:09 +0200772 ret = recv_control_msg(pdev,
773 GET_STATUS_CTL, request, &buf, sizeof(buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 if (ret < 0)
775 return ret;
776 if (pdev->type < 675)
777 *sensor = buf | 0x100;
778 else
779 *sensor = buf;
780 return 0;
781}
Hans de Goede6eba9352011-06-26 06:49:59 -0300782#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 /* End of Add-Ons */
785 /* ************************************************* */
786
Luc Saillard2b455db2006-04-24 10:29:46 -0300787/* Linux 2.5.something and 2.6 pass direct pointers to arguments of
788 ioctl() calls. With 2.4, you have to do tedious copy_from_user()
789 and copy_to_user() calls. With these macros we circumvent this,
790 and let me maintain only one source file. The functionality is
791 exactly the same otherwise.
792 */
793
Luc Saillard2b455db2006-04-24 10:29:46 -0300794/* define local variable for arg */
795#define ARG_DEF(ARG_type, ARG_name)\
796 ARG_type *ARG_name = arg;
797/* copy arg to local variable */
798#define ARG_IN(ARG_name) /* nothing */
799/* argument itself (referenced) */
800#define ARGR(ARG_name) (*ARG_name)
801/* argument address */
802#define ARGA(ARG_name) ARG_name
803/* copy local variable to arg */
804#define ARG_OUT(ARG_name) /* nothing */
805
Hans de Goede6c9cac82011-06-26 12:52:01 -0300806/*
807 * Our ctrls use native values, but the old custom pwc ioctl interface expects
808 * values from 0 - 65535, define 2 helper functions to scale things. */
809static int pwc_ioctl_g_ctrl(struct v4l2_ctrl *ctrl)
810{
811 return v4l2_ctrl_g_ctrl(ctrl) * 65535 / ctrl->maximum;
812}
813
814static int pwc_ioctl_s_ctrl(struct v4l2_ctrl *ctrl, int val)
815{
816 return v4l2_ctrl_s_ctrl(ctrl, val * ctrl->maximum / 65535);
817}
818
Hans Verkuil069b7472008-12-30 07:04:34 -0300819long pwc_ioctl(struct pwc_device *pdev, unsigned int cmd, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
Hans Verkuil069b7472008-12-30 07:04:34 -0300821 long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 switch(cmd) {
824 case VIDIOCPWCRUSER:
Hans de Goedec20d78c2011-10-09 09:16:46 -0300825 ret = v4l2_ctrl_s_ctrl(pdev->restore_user, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 break;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 case VIDIOCPWCSUSER:
Hans de Goedec20d78c2011-10-09 09:16:46 -0300829 ret = v4l2_ctrl_s_ctrl(pdev->save_user, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 break;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 case VIDIOCPWCFACTORY:
Hans de Goedec20d78c2011-10-09 09:16:46 -0300833 ret = v4l2_ctrl_s_ctrl(pdev->restore_factory, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 break;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 case VIDIOCPWCSCQUAL:
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300837 {
Luc Saillard2b455db2006-04-24 10:29:46 -0300838 ARG_DEF(int, qual)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Hans de Goedec20d78c2011-10-09 09:16:46 -0300840 mutex_lock(&pdev->udevlock);
841 if (!pdev->udev) {
842 ret = -ENODEV;
843 goto leave;
844 }
845
846 if (pdev->iso_init) {
Hans de Goede3751e282010-11-16 11:39:25 -0300847 ret = -EBUSY;
Hans de Goedec20d78c2011-10-09 09:16:46 -0300848 goto leave;
Hans de Goede3751e282010-11-16 11:39:25 -0300849 }
850
Luc Saillard2b455db2006-04-24 10:29:46 -0300851 ARG_IN(qual)
852 if (ARGR(qual) < 0 || ARGR(qual) > 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 ret = -EINVAL;
854 else
Hans de Goede3751e282010-11-16 11:39:25 -0300855 ret = pwc_set_video_mode(pdev, pdev->view.x, pdev->view.y, pdev->vframes, ARGR(qual), pdev->vsnapshot);
Hans de Goedec20d78c2011-10-09 09:16:46 -0300856leave:
857 mutex_unlock(&pdev->udevlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 break;
859 }
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 case VIDIOCPWCGCQUAL:
862 {
Luc Saillard2b455db2006-04-24 10:29:46 -0300863 ARG_DEF(int, qual)
864
865 ARGR(qual) = pdev->vcompression;
866 ARG_OUT(qual)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 break;
868 }
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 case VIDIOCPWCPROBE:
871 {
Luc Saillard2b455db2006-04-24 10:29:46 -0300872 ARG_DEF(struct pwc_probe, probe)
873
Hans de Goede9a7b2d12011-06-06 14:43:39 -0300874 strcpy(ARGR(probe).name, pdev->vdev.name);
Luc Saillard2b455db2006-04-24 10:29:46 -0300875 ARGR(probe).type = pdev->type;
876 ARG_OUT(probe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 break;
878 }
879
880 case VIDIOCPWCGSERIAL:
881 {
Luc Saillard2b455db2006-04-24 10:29:46 -0300882 ARG_DEF(struct pwc_serial, serial)
883
884 strcpy(ARGR(serial).serial, pdev->serial);
885 ARG_OUT(serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 break;
887 }
888
889 case VIDIOCPWCSAGC:
890 {
Luc Saillard2b455db2006-04-24 10:29:46 -0300891 ARG_DEF(int, agc)
Luc Saillard2b455db2006-04-24 10:29:46 -0300892 ARG_IN(agc)
Hans de Goede6c9cac82011-06-26 12:52:01 -0300893 ret = v4l2_ctrl_s_ctrl(pdev->autogain, ARGR(agc) < 0);
894 if (ret == 0 && ARGR(agc) >= 0)
895 ret = pwc_ioctl_s_ctrl(pdev->gain, ARGR(agc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 break;
897 }
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 case VIDIOCPWCGAGC:
900 {
Luc Saillard2b455db2006-04-24 10:29:46 -0300901 ARG_DEF(int, agc)
Hans de Goede6c9cac82011-06-26 12:52:01 -0300902 if (v4l2_ctrl_g_ctrl(pdev->autogain))
903 ARGR(agc) = -1;
904 else
905 ARGR(agc) = pwc_ioctl_g_ctrl(pdev->gain);
Luc Saillard2b455db2006-04-24 10:29:46 -0300906 ARG_OUT(agc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 break;
908 }
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 case VIDIOCPWCSSHUTTER:
911 {
Hans de Goede6c9cac82011-06-26 12:52:01 -0300912 ARG_DEF(int, shutter)
913 ARG_IN(shutter)
914 ret = v4l2_ctrl_s_ctrl(pdev->exposure_auto,
915 /* Menu idx 0 = auto, idx 1 = manual */
916 ARGR(shutter) >= 0);
917 if (ret == 0 && ARGR(shutter) >= 0)
918 ret = pwc_ioctl_s_ctrl(pdev->exposure, ARGR(shutter));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 break;
920 }
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300921
922 case VIDIOCPWCSAWB:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 {
Luc Saillard2b455db2006-04-24 10:29:46 -0300924 ARG_DEF(struct pwc_whitebalance, wb)
Luc Saillard2b455db2006-04-24 10:29:46 -0300925 ARG_IN(wb)
Hans de Goede6c9cac82011-06-26 12:52:01 -0300926 ret = v4l2_ctrl_s_ctrl(pdev->auto_white_balance,
927 ARGR(wb).mode);
928 if (ret == 0 && ARGR(wb).mode == PWC_WB_MANUAL)
929 ret = pwc_ioctl_s_ctrl(pdev->red_balance,
930 ARGR(wb).manual_red);
931 if (ret == 0 && ARGR(wb).mode == PWC_WB_MANUAL)
932 ret = pwc_ioctl_s_ctrl(pdev->blue_balance,
933 ARGR(wb).manual_blue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 break;
935 }
936
937 case VIDIOCPWCGAWB:
938 {
Luc Saillard2b455db2006-04-24 10:29:46 -0300939 ARG_DEF(struct pwc_whitebalance, wb)
Hans de Goede6c9cac82011-06-26 12:52:01 -0300940 ARGR(wb).mode = v4l2_ctrl_g_ctrl(pdev->auto_white_balance);
941 ARGR(wb).manual_red = ARGR(wb).read_red =
942 pwc_ioctl_g_ctrl(pdev->red_balance);
943 ARGR(wb).manual_blue = ARGR(wb).read_blue =
944 pwc_ioctl_g_ctrl(pdev->blue_balance);
Luc Saillard2b455db2006-04-24 10:29:46 -0300945 ARG_OUT(wb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 break;
947 }
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 case VIDIOCPWCSAWBSPEED:
950 {
Luc Saillard2b455db2006-04-24 10:29:46 -0300951 ARG_DEF(struct pwc_wb_speed, wbs)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300952
Luc Saillard2b455db2006-04-24 10:29:46 -0300953 if (ARGR(wbs).control_speed > 0) {
Hans de Goedef4af65952011-10-09 09:56:23 -0300954 ret = pwc_ioctl_s_ctrl(pdev->awb_speed,
955 ARGR(wbs).control_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 }
Hans de Goedef4af65952011-10-09 09:56:23 -0300957 if (ret == 0 && ARGR(wbs).control_delay > 0) {
958 ret = pwc_ioctl_s_ctrl(pdev->awb_delay,
959 ARGR(wbs).control_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 }
961 break;
962 }
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 case VIDIOCPWCGAWBSPEED:
965 {
Luc Saillard2b455db2006-04-24 10:29:46 -0300966 ARG_DEF(struct pwc_wb_speed, wbs)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300967
Hans de Goedef4af65952011-10-09 09:56:23 -0300968 ARGR(wbs).control_speed = v4l2_ctrl_g_ctrl(pdev->awb_speed);
969 ARGR(wbs).control_delay = v4l2_ctrl_g_ctrl(pdev->awb_delay);
Luc Saillard2b455db2006-04-24 10:29:46 -0300970 ARG_OUT(wbs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 break;
972 }
973
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300974 case VIDIOCPWCSLED:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 {
Luc Saillard2b455db2006-04-24 10:29:46 -0300976 ARG_DEF(struct pwc_leds, leds)
977
Hans de Goedec20d78c2011-10-09 09:16:46 -0300978 mutex_lock(&pdev->udevlock);
979 if (!pdev->udev) {
980 ret = -ENODEV;
981 break;
982 }
983
Luc Saillard2b455db2006-04-24 10:29:46 -0300984 ARG_IN(leds)
985 ret = pwc_set_leds(pdev, ARGR(leds).led_on, ARGR(leds).led_off);
Hans de Goedec20d78c2011-10-09 09:16:46 -0300986
987 mutex_unlock(&pdev->udevlock);
Trent Piepho657de3c2006-06-20 00:30:57 -0300988 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 }
990
991
992 case VIDIOCPWCGLED:
993 {
Luc Saillard2b455db2006-04-24 10:29:46 -0300994 ARG_DEF(struct pwc_leds, leds)
995
Hans de Goedec20d78c2011-10-09 09:16:46 -0300996 mutex_lock(&pdev->udevlock);
997 if (!pdev->udev) {
998 ret = -ENODEV;
999 break;
1000 }
1001
Luc Saillard2b455db2006-04-24 10:29:46 -03001002 ret = pwc_get_leds(pdev, &ARGR(leds).led_on, &ARGR(leds).led_off);
1003 ARG_OUT(leds)
Hans de Goedec20d78c2011-10-09 09:16:46 -03001004
1005 mutex_unlock(&pdev->udevlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 break;
1007 }
1008
1009 case VIDIOCPWCSCONTOUR:
1010 {
Luc Saillard2b455db2006-04-24 10:29:46 -03001011 ARG_DEF(int, contour)
Luc Saillard2b455db2006-04-24 10:29:46 -03001012 ARG_IN(contour)
Hans de Goede6c9cac82011-06-26 12:52:01 -03001013 ret = v4l2_ctrl_s_ctrl(pdev->autocontour, ARGR(contour) < 0);
1014 if (ret == 0 && ARGR(contour) >= 0)
1015 ret = pwc_ioctl_s_ctrl(pdev->contour, ARGR(contour));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 break;
1017 }
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 case VIDIOCPWCGCONTOUR:
1020 {
Luc Saillard2b455db2006-04-24 10:29:46 -03001021 ARG_DEF(int, contour)
Hans de Goede6c9cac82011-06-26 12:52:01 -03001022 if (v4l2_ctrl_g_ctrl(pdev->autocontour))
1023 ARGR(contour) = -1;
1024 else
1025 ARGR(contour) = pwc_ioctl_g_ctrl(pdev->contour);
Luc Saillard2b455db2006-04-24 10:29:46 -03001026 ARG_OUT(contour)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 break;
1028 }
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 case VIDIOCPWCSBACKLIGHT:
1031 {
Luc Saillard2b455db2006-04-24 10:29:46 -03001032 ARG_DEF(int, backlight)
Luc Saillard2b455db2006-04-24 10:29:46 -03001033 ARG_IN(backlight)
Hans de Goede6c9cac82011-06-26 12:52:01 -03001034 ret = v4l2_ctrl_s_ctrl(pdev->backlight, ARGR(backlight));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 break;
1036 }
1037
1038 case VIDIOCPWCGBACKLIGHT:
1039 {
Luc Saillard2b455db2006-04-24 10:29:46 -03001040 ARG_DEF(int, backlight)
Hans de Goede6c9cac82011-06-26 12:52:01 -03001041 ARGR(backlight) = v4l2_ctrl_g_ctrl(pdev->backlight);
Luc Saillard2b455db2006-04-24 10:29:46 -03001042 ARG_OUT(backlight)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 break;
1044 }
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 case VIDIOCPWCSFLICKER:
1047 {
Luc Saillard2b455db2006-04-24 10:29:46 -03001048 ARG_DEF(int, flicker)
Luc Saillard2b455db2006-04-24 10:29:46 -03001049 ARG_IN(flicker)
Hans de Goede6c9cac82011-06-26 12:52:01 -03001050 ret = v4l2_ctrl_s_ctrl(pdev->flicker, ARGR(flicker));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 break;
1052 }
1053
1054 case VIDIOCPWCGFLICKER:
1055 {
Luc Saillard2b455db2006-04-24 10:29:46 -03001056 ARG_DEF(int, flicker)
Hans de Goede6c9cac82011-06-26 12:52:01 -03001057 ARGR(flicker) = v4l2_ctrl_g_ctrl(pdev->flicker);
Luc Saillard2b455db2006-04-24 10:29:46 -03001058 ARG_OUT(flicker)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 break;
1060 }
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 case VIDIOCPWCSDYNNOISE:
1063 {
Luc Saillard2b455db2006-04-24 10:29:46 -03001064 ARG_DEF(int, dynnoise)
Luc Saillard2b455db2006-04-24 10:29:46 -03001065 ARG_IN(dynnoise)
Hans de Goede6c9cac82011-06-26 12:52:01 -03001066 ret = v4l2_ctrl_s_ctrl(pdev->noise_reduction, ARGR(dynnoise));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 break;
1068 }
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 case VIDIOCPWCGDYNNOISE:
1071 {
Luc Saillard2b455db2006-04-24 10:29:46 -03001072 ARG_DEF(int, dynnoise)
Hans de Goede6c9cac82011-06-26 12:52:01 -03001073 ARGR(dynnoise) = v4l2_ctrl_g_ctrl(pdev->noise_reduction);
Luc Saillard2b455db2006-04-24 10:29:46 -03001074 ARG_OUT(dynnoise);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 break;
1076 }
1077
1078 case VIDIOCPWCGREALSIZE:
1079 {
Luc Saillard2b455db2006-04-24 10:29:46 -03001080 ARG_DEF(struct pwc_imagesize, size)
1081
1082 ARGR(size).width = pdev->image.x;
1083 ARGR(size).height = pdev->image.y;
1084 ARG_OUT(size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 break;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001086 }
1087
1088 case VIDIOCPWCMPTRESET:
1089 {
1090 if (pdev->features & FEATURE_MOTOR_PANTILT)
1091 {
Luc Saillard2b455db2006-04-24 10:29:46 -03001092 ARG_DEF(int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Luc Saillard2b455db2006-04-24 10:29:46 -03001094 ARG_IN(flags)
1095 ret = pwc_mpt_reset(pdev, ARGR(flags));
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001096 }
1097 else
1098 {
1099 ret = -ENXIO;
1100 }
1101 break;
1102 }
1103
1104 case VIDIOCPWCMPTGRANGE:
1105 {
1106 if (pdev->features & FEATURE_MOTOR_PANTILT)
1107 {
Luc Saillard2b455db2006-04-24 10:29:46 -03001108 ARG_DEF(struct pwc_mpt_range, range)
1109
1110 ARGR(range) = pdev->angle_range;
1111 ARG_OUT(range)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001112 }
1113 else
1114 {
1115 ret = -ENXIO;
1116 }
1117 break;
1118 }
1119
1120 case VIDIOCPWCMPTSANGLE:
1121 {
1122 int new_pan, new_tilt;
1123
1124 if (pdev->features & FEATURE_MOTOR_PANTILT)
1125 {
Luc Saillard2b455db2006-04-24 10:29:46 -03001126 ARG_DEF(struct pwc_mpt_angles, angles)
1127
1128 ARG_IN(angles)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 /* The camera can only set relative angles, so
1130 do some calculations when getting an absolute angle .
1131 */
Luc Saillard2b455db2006-04-24 10:29:46 -03001132 if (ARGR(angles).absolute)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 {
Luc Saillard2b455db2006-04-24 10:29:46 -03001134 new_pan = ARGR(angles).pan;
1135 new_tilt = ARGR(angles).tilt;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001136 }
1137 else
1138 {
Luc Saillard2b455db2006-04-24 10:29:46 -03001139 new_pan = pdev->pan_angle + ARGR(angles).pan;
1140 new_tilt = pdev->tilt_angle + ARGR(angles).tilt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 }
Luc Saillard2b455db2006-04-24 10:29:46 -03001142 ret = pwc_mpt_set_angle(pdev, new_pan, new_tilt);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001143 }
1144 else
1145 {
1146 ret = -ENXIO;
1147 }
1148 break;
1149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001151 case VIDIOCPWCMPTGANGLE:
1152 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001154 if (pdev->features & FEATURE_MOTOR_PANTILT)
1155 {
Luc Saillard2b455db2006-04-24 10:29:46 -03001156 ARG_DEF(struct pwc_mpt_angles, angles)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001157
Luc Saillard2b455db2006-04-24 10:29:46 -03001158 ARGR(angles).absolute = 1;
1159 ARGR(angles).pan = pdev->pan_angle;
1160 ARGR(angles).tilt = pdev->tilt_angle;
1161 ARG_OUT(angles)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001162 }
1163 else
1164 {
1165 ret = -ENXIO;
1166 }
1167 break;
1168 }
1169
1170 case VIDIOCPWCMPTSTATUS:
1171 {
1172 if (pdev->features & FEATURE_MOTOR_PANTILT)
1173 {
Luc Saillard2b455db2006-04-24 10:29:46 -03001174 ARG_DEF(struct pwc_mpt_status, status)
1175
1176 ret = pwc_mpt_get_status(pdev, ARGA(status));
1177 ARG_OUT(status)
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001178 }
1179 else
1180 {
1181 ret = -ENXIO;
1182 }
1183 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 }
1185
1186 case VIDIOCPWCGVIDCMD:
1187 {
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03001188 ARG_DEF(struct pwc_video_command, vcmd);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001189
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03001190 ARGR(vcmd).type = pdev->type;
1191 ARGR(vcmd).release = pdev->release;
1192 ARGR(vcmd).command_len = pdev->cmd_len;
1193 memcpy(&ARGR(vcmd).command_buf, pdev->cmd_buf, pdev->cmd_len);
1194 ARGR(vcmd).bandlength = pdev->vbandlength;
1195 ARGR(vcmd).frame_size = pdev->frame_size;
1196 ARG_OUT(vcmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 break;
1198 }
Michael Krufkyb930e1d2007-08-27 18:16:54 -03001199 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 case VIDIOCPWCGVIDTABLE:
1201 {
Luc Saillard2b455db2006-04-24 10:29:46 -03001202 ARG_DEF(struct pwc_table_init_buffer, table);
1203 ARGR(table).len = pdev->cmd_len;
1204 memcpy(&ARGR(table).buffer, pdev->decompress_data, pdev->decompressor->table_size);
1205 ARG_OUT(table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 break;
1207 }
1208 */
1209
1210 default:
1211 ret = -ENOIOCTLCMD;
1212 break;
1213 }
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 if (ret > 0)
1216 return 0;
1217 return ret;
1218}
1219
1220
Luc Saillard2b455db2006-04-24 10:29:46 -03001221/* vim: set cinoptions= formatoptions=croql cindent shiftwidth=8 tabstop=8: */