1 /* Driver for Philips webcam
2 Functions that send various control messages to the webcam, including
4 (C) 1999-2003 Nemosoft Unv.
5 (C) 2004-2006 Luc Saillard (luc@saillard.org)
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>.
11 NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
12 driver and thus may have bugs that are not present in the original version.
13 Please send bug reports and support requests to <luc@saillard.org>.
14 The decompression routines have been implemented by reverse-engineering the
15 Nemosoft binary pwcx module. Caveat emptor.
17 This program is free software; you can redistribute it and/or modify
18 it under the terms of the GNU General Public License as published by
19 the Free Software Foundation; either version 2 of the License, or
20 (at your option) any later version.
22 This program is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 2001/08/03 Alvarado Added methods for changing white balance and
38 /* Control functions for the cam; brightness, contrast, video mode, etc. */
41 #include <asm/uaccess.h>
43 #include <asm/errno.h>
46 #include "pwc-uncompress.h"
47 #include "pwc-kiara.h"
48 #include "pwc-timon.h"
50 #include "pwc-dec23.h"
52 /* Request types: video */
53 #define SET_LUM_CTL 0x01
54 #define GET_LUM_CTL 0x02
55 #define SET_CHROM_CTL 0x03
56 #define GET_CHROM_CTL 0x04
57 #define SET_STATUS_CTL 0x05
58 #define GET_STATUS_CTL 0x06
59 #define SET_EP_STREAM_CTL 0x07
60 #define GET_EP_STREAM_CTL 0x08
61 #define GET_XX_CTL 0x09
62 #define SET_XX_CTL 0x0A
63 #define GET_XY_CTL 0x0B
64 #define SET_XY_CTL 0x0C
65 #define SET_MPT_CTL 0x0D
66 #define GET_MPT_CTL 0x0E
68 /* Selectors for the Luminance controls [GS]ET_LUM_CTL */
69 #define AGC_MODE_FORMATTER 0x2000
70 #define PRESET_AGC_FORMATTER 0x2100
71 #define SHUTTER_MODE_FORMATTER 0x2200
72 #define PRESET_SHUTTER_FORMATTER 0x2300
73 #define PRESET_CONTOUR_FORMATTER 0x2400
74 #define AUTO_CONTOUR_FORMATTER 0x2500
75 #define BACK_LIGHT_COMPENSATION_FORMATTER 0x2600
76 #define CONTRAST_FORMATTER 0x2700
77 #define DYNAMIC_NOISE_CONTROL_FORMATTER 0x2800
78 #define FLICKERLESS_MODE_FORMATTER 0x2900
79 #define AE_CONTROL_SPEED 0x2A00
80 #define BRIGHTNESS_FORMATTER 0x2B00
81 #define GAMMA_FORMATTER 0x2C00
83 /* Selectors for the Chrominance controls [GS]ET_CHROM_CTL */
84 #define WB_MODE_FORMATTER 0x1000
85 #define AWB_CONTROL_SPEED_FORMATTER 0x1100
86 #define AWB_CONTROL_DELAY_FORMATTER 0x1200
87 #define PRESET_MANUAL_RED_GAIN_FORMATTER 0x1300
88 #define PRESET_MANUAL_BLUE_GAIN_FORMATTER 0x1400
89 #define COLOUR_MODE_FORMATTER 0x1500
90 #define SATURATION_MODE_FORMATTER1 0x1600
91 #define SATURATION_MODE_FORMATTER2 0x1700
93 /* Selectors for the Status controls [GS]ET_STATUS_CTL */
94 #define SAVE_USER_DEFAULTS_FORMATTER 0x0200
95 #define RESTORE_USER_DEFAULTS_FORMATTER 0x0300
96 #define RESTORE_FACTORY_DEFAULTS_FORMATTER 0x0400
97 #define READ_AGC_FORMATTER 0x0500
98 #define READ_SHUTTER_FORMATTER 0x0600
99 #define READ_RED_GAIN_FORMATTER 0x0700
100 #define READ_BLUE_GAIN_FORMATTER 0x0800
101 #define GET_STATUS_B00 0x0B00
102 #define SENSOR_TYPE_FORMATTER1 0x0C00
103 #define GET_STATUS_3000 0x3000
104 #define READ_RAW_Y_MEAN_FORMATTER 0x3100
105 #define SET_POWER_SAVE_MODE_FORMATTER 0x3200
106 #define MIRROR_IMAGE_FORMATTER 0x3300
107 #define LED_FORMATTER 0x3400
108 #define LOWLIGHT 0x3500
109 #define GET_STATUS_3600 0x3600
110 #define SENSOR_TYPE_FORMATTER2 0x3700
111 #define GET_STATUS_3800 0x3800
112 #define GET_STATUS_4000 0x4000
113 #define GET_STATUS_4100 0x4100 /* Get */
114 #define CTL_STATUS_4200 0x4200 /* [GS] 1 */
116 /* Formatters for the Video Endpoint controls [GS]ET_EP_STREAM_CTL */
117 #define VIDEO_OUTPUT_CONTROL_FORMATTER 0x0100
119 /* Formatters for the motorized pan & tilt [GS]ET_MPT_CTL */
120 #define PT_RELATIVE_CONTROL_FORMATTER 0x01
121 #define PT_RESET_CONTROL_FORMATTER 0x02
122 #define PT_STATUS_FORMATTER 0x03
124 static const char *size2name[PSZ_MAX] =
136 /* Entries for the Nala (645/646) camera; the Nala doesn't have compression
137 preferences, so you either get compressed or non-compressed streams.
139 An alternate value of 0 means this mode is not available at all.
142 #define PWC_FPS_MAX_NALA 8
144 struct Nala_table_entry {
145 char alternate; /* USB alternate setting */
146 int compressed; /* Compressed yes/no */
148 unsigned char mode[3]; /* precomputed mode table */
151 static unsigned int Nala_fps_vector[PWC_FPS_MAX_NALA] = { 4, 5, 7, 10, 12, 15, 20, 24 };
153 static struct Nala_table_entry Nala_table[PSZ_MAX][PWC_FPS_MAX_NALA] =
155 #include "pwc-nala.h"
158 static void pwc_set_image_buffer_size(struct pwc_device *pdev);
160 /****************************************************************************/
163 #define SendControlMsg(request, value, buflen) \
164 usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0), \
166 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, \
171 #define RecvControlMsg(request, value, buflen) \
172 usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0), \
174 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, \
180 static int send_video_command(struct usb_device *udev, int index, void *buf, int buflen)
182 return usb_control_msg(udev,
183 usb_sndctrlpipe(udev, 0),
185 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
186 VIDEO_OUTPUT_CONTROL_FORMATTER,
193 static int set_video_mode_Nala(struct pwc_device *pdev, int size, int frames)
195 unsigned char buf[3];
197 struct Nala_table_entry *pEntry;
198 int frames2frames[31] =
199 { /* closest match of framerate */
200 0, 0, 0, 0, 4, /* 0-4 */
201 5, 5, 7, 7, 10, /* 5-9 */
202 10, 10, 12, 12, 15, /* 10-14 */
203 15, 15, 15, 20, 20, /* 15-19 */
204 20, 20, 20, 24, 24, /* 20-24 */
205 24, 24, 24, 24, 24, /* 25-29 */
208 int frames2table[31] =
209 { 0, 0, 0, 0, 0, /* 0-4 */
210 1, 1, 1, 2, 2, /* 5-9 */
211 3, 3, 4, 4, 4, /* 10-14 */
212 5, 5, 5, 5, 5, /* 15-19 */
213 6, 6, 6, 6, 7, /* 20-24 */
214 7, 7, 7, 7, 7, /* 25-29 */
218 if (size < 0 || size > PSZ_CIF || frames < 4 || frames > 25)
220 frames = frames2frames[frames];
221 fps = frames2table[frames];
222 pEntry = &Nala_table[size][fps];
223 if (pEntry->alternate == 0)
226 memcpy(buf, pEntry->mode, 3);
227 ret = send_video_command(pdev->udev, pdev->vendpoint, buf, 3);
229 PWC_DEBUG_MODULE("Failed to send video command... %d\n", ret);
232 if (pEntry->compressed && pdev->vpalette != VIDEO_PALETTE_RAW)
233 pwc_dec1_init(pdev->type, pdev->release, buf, pdev->decompress_data);
236 memcpy(pdev->cmd_buf, buf, 3);
238 /* Set various parameters */
239 pdev->vframes = frames;
241 pdev->valternate = pEntry->alternate;
242 pdev->image = pwc_image_sizes[size];
243 pdev->frame_size = (pdev->image.x * pdev->image.y * 3) / 2;
244 if (pEntry->compressed) {
245 if (pdev->release < 5) { /* 4 fold compression */
246 pdev->vbandlength = 528;
247 pdev->frame_size /= 4;
250 pdev->vbandlength = 704;
251 pdev->frame_size /= 3;
255 pdev->vbandlength = 0;
260 static int set_video_mode_Timon(struct pwc_device *pdev, int size, int frames, int compression, int snapshot)
262 unsigned char buf[13];
263 const struct Timon_table_entry *pChoose;
266 if (size >= PSZ_MAX || frames < 5 || frames > 30 || compression < 0 || compression > 3)
268 if (size == PSZ_VGA && frames > 15)
270 fps = (frames / 5) - 1;
272 /* Find a supported framerate with progressively higher compression ratios
273 if the preferred ratio is not available.
276 while (compression <= 3) {
277 pChoose = &Timon_table[size][fps][compression];
278 if (pChoose->alternate != 0)
282 if (pChoose == NULL || pChoose->alternate == 0)
283 return -ENOENT; /* Not supported. */
285 memcpy(buf, pChoose->mode, 13);
288 ret = send_video_command(pdev->udev, pdev->vendpoint, buf, 13);
292 if (pChoose->bandlength > 0 && pdev->vpalette != VIDEO_PALETTE_RAW)
293 pwc_dec23_init(pdev, pdev->type, buf);
296 memcpy(pdev->cmd_buf, buf, 13);
298 /* Set various parameters */
299 pdev->vframes = frames;
301 pdev->vsnapshot = snapshot;
302 pdev->valternate = pChoose->alternate;
303 pdev->image = pwc_image_sizes[size];
304 pdev->vbandlength = pChoose->bandlength;
305 if (pChoose->bandlength > 0)
306 pdev->frame_size = (pChoose->bandlength * pdev->image.y) / 4;
308 pdev->frame_size = (pdev->image.x * pdev->image.y * 12) / 8;
313 static int set_video_mode_Kiara(struct pwc_device *pdev, int size, int frames, int compression, int snapshot)
315 const struct Kiara_table_entry *pChoose = NULL;
317 unsigned char buf[12];
318 struct Kiara_table_entry RawEntry = {6, 773, 1272, {0xAD, 0xF4, 0x10, 0x27, 0xB6, 0x24, 0x96, 0x02, 0x30, 0x05, 0x03, 0x80}};
320 if (size >= PSZ_MAX || frames < 5 || frames > 30 || compression < 0 || compression > 3)
322 if (size == PSZ_VGA && frames > 15)
324 fps = (frames / 5) - 1;
326 /* special case: VGA @ 5 fps and snapshot is raw bayer mode */
327 if (size == PSZ_VGA && frames == 5 && snapshot && pdev->vpalette == VIDEO_PALETTE_RAW)
329 /* Only available in case the raw palette is selected or
330 we have the decompressor available. This mode is
331 only available in compressed form
333 PWC_DEBUG_SIZE("Choosing VGA/5 BAYER mode.\n");
338 /* Find a supported framerate with progressively higher compression ratios
339 if the preferred ratio is not available.
340 Skip this step when using RAW modes.
343 while (compression <= 3) {
344 pChoose = &Kiara_table[size][fps][compression];
345 if (pChoose->alternate != 0)
350 if (pChoose == NULL || pChoose->alternate == 0)
351 return -ENOENT; /* Not supported. */
353 PWC_TRACE("Using alternate setting %d.\n", pChoose->alternate);
355 /* usb_control_msg won't take staticly allocated arrays as argument?? */
356 memcpy(buf, pChoose->mode, 12);
360 /* Firmware bug: video endpoint is 5, but commands are sent to endpoint 4 */
361 ret = send_video_command(pdev->udev, 4 /* pdev->vendpoint */, buf, 12);
365 if (pChoose->bandlength > 0 && pdev->vpalette != VIDEO_PALETTE_RAW)
366 pwc_dec23_init(pdev, pdev->type, buf);
369 memcpy(pdev->cmd_buf, buf, 12);
371 pdev->vframes = frames;
373 pdev->vsnapshot = snapshot;
374 pdev->valternate = pChoose->alternate;
375 pdev->image = pwc_image_sizes[size];
376 pdev->vbandlength = pChoose->bandlength;
377 if (pdev->vbandlength > 0)
378 pdev->frame_size = (pdev->vbandlength * pdev->image.y) / 4;
380 pdev->frame_size = (pdev->image.x * pdev->image.y * 12) / 8;
381 PWC_TRACE("frame_size=%d, vframes=%d, vsize=%d, vsnapshot=%d, vbandlength=%d\n",
382 pdev->frame_size,pdev->vframes,pdev->vsize,pdev->vsnapshot,pdev->vbandlength);
389 @pdev: device structure
390 @width: viewport width
391 @height: viewport height
392 @frame: framerate, in fps
393 @compression: preferred compression ratio
394 @snapshot: snapshot mode or streaming
396 int pwc_set_video_mode(struct pwc_device *pdev, int width, int height, int frames, int compression, int snapshot)
400 PWC_DEBUG_FLOW("set_video_mode(%dx%d @ %d, palette %d).\n", width, height, frames, pdev->vpalette);
401 size = pwc_decode_size(pdev, width, height);
403 PWC_DEBUG_MODULE("Could not find suitable size.\n");
406 PWC_TRACE("decode_size = %d.\n", size);
408 if (DEVICE_USE_CODEC1(pdev->type)) {
409 ret = set_video_mode_Nala(pdev, size, frames);
411 } else if (DEVICE_USE_CODEC3(pdev->type)) {
412 ret = set_video_mode_Kiara(pdev, size, frames, compression, snapshot);
415 ret = set_video_mode_Timon(pdev, size, frames, compression, snapshot);
418 PWC_ERROR("Failed to set video mode %s@%d fps; return code = %d\n", size2name[size], frames, ret);
421 pdev->view.x = width;
422 pdev->view.y = height;
423 pdev->frame_total_size = pdev->frame_size + pdev->frame_header_size + pdev->frame_trailer_size;
424 pwc_set_image_buffer_size(pdev);
425 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);
429 static unsigned int pwc_get_fps_Nala(struct pwc_device *pdev, unsigned int index, unsigned int size)
433 for (i = 0; i < PWC_FPS_MAX_NALA; i++) {
434 if (Nala_table[size][i].alternate) {
435 if (index--==0) return Nala_fps_vector[i];
441 static unsigned int pwc_get_fps_Kiara(struct pwc_device *pdev, unsigned int index, unsigned int size)
445 for (i = 0; i < PWC_FPS_MAX_KIARA; i++) {
446 if (Kiara_table[size][i][3].alternate) {
447 if (index--==0) return Kiara_fps_vector[i];
453 static unsigned int pwc_get_fps_Timon(struct pwc_device *pdev, unsigned int index, unsigned int size)
457 for (i=0; i < PWC_FPS_MAX_TIMON; i++) {
458 if (Timon_table[size][i][3].alternate) {
459 if (index--==0) return Timon_fps_vector[i];
465 unsigned int pwc_get_fps(struct pwc_device *pdev, unsigned int index, unsigned int size)
469 if (DEVICE_USE_CODEC1(pdev->type)) {
470 ret = pwc_get_fps_Nala(pdev, index, size);
472 } else if (DEVICE_USE_CODEC3(pdev->type)) {
473 ret = pwc_get_fps_Kiara(pdev, index, size);
476 ret = pwc_get_fps_Timon(pdev, index, size);
486 static void pwc_set_image_buffer_size(struct pwc_device *pdev)
490 /* for PALETTE_YUV420P */
491 switch(pdev->vpalette)
493 case VIDEO_PALETTE_YUV420P:
496 case VIDEO_PALETTE_RAW:
497 factor = 6; /* can be uncompressed YUV420P */
501 /* Set sizes in bytes */
502 pdev->image.size = pdev->image.x * pdev->image.y * factor / 4;
503 pdev->view.size = pdev->view.x * pdev->view.y * factor / 4;
505 /* Align offset, or you'll get some very weird results in
506 YUV420 mode... x must be multiple of 4 (to get the Y's in
507 place), and y even (or you'll mixup U & V). This is less of a
510 pdev->offset.x = ((pdev->view.x - pdev->image.x) / 2) & 0xFFFC;
511 pdev->offset.y = ((pdev->view.y - pdev->image.y) / 2) & 0xFFFE;
513 /* Fill buffers with black colors */
514 for (i = 0; i < pwc_mbufs; i++) {
515 unsigned char *p = pdev->image_data + pdev->images[i].offset;
516 memset(p, BLACK_Y, pdev->view.x * pdev->view.y);
517 p += pdev->view.x * pdev->view.y;
518 memset(p, BLACK_U, pdev->view.x * pdev->view.y/4);
519 p += pdev->view.x * pdev->view.y/4;
520 memset(p, BLACK_V, pdev->view.x * pdev->view.y/4);
528 int pwc_get_brightness(struct pwc_device *pdev)
533 ret = RecvControlMsg(GET_LUM_CTL, BRIGHTNESS_FORMATTER, 1);
539 int pwc_set_brightness(struct pwc_device *pdev, int value)
547 buf = (value >> 9) & 0x7f;
548 return SendControlMsg(SET_LUM_CTL, BRIGHTNESS_FORMATTER, 1);
553 int pwc_get_contrast(struct pwc_device *pdev)
558 ret = RecvControlMsg(GET_LUM_CTL, CONTRAST_FORMATTER, 1);
564 int pwc_set_contrast(struct pwc_device *pdev, int value)
572 buf = (value >> 10) & 0x3f;
573 return SendControlMsg(SET_LUM_CTL, CONTRAST_FORMATTER, 1);
578 int pwc_get_gamma(struct pwc_device *pdev)
583 ret = RecvControlMsg(GET_LUM_CTL, GAMMA_FORMATTER, 1);
589 int pwc_set_gamma(struct pwc_device *pdev, int value)
597 buf = (value >> 11) & 0x1f;
598 return SendControlMsg(SET_LUM_CTL, GAMMA_FORMATTER, 1);
604 /* return a value between [-100 , 100] */
605 int pwc_get_saturation(struct pwc_device *pdev, int *value)
608 int ret, saturation_register;
610 if (pdev->type < 675)
612 if (pdev->type < 730)
613 saturation_register = SATURATION_MODE_FORMATTER2;
615 saturation_register = SATURATION_MODE_FORMATTER1;
616 ret = RecvControlMsg(GET_CHROM_CTL, saturation_register, 1);
619 *value = (signed)buf;
623 /* @param value saturation color between [-100 , 100] */
624 int pwc_set_saturation(struct pwc_device *pdev, int value)
627 int saturation_register;
629 if (pdev->type < 675)
635 if (pdev->type < 730)
636 saturation_register = SATURATION_MODE_FORMATTER2;
638 saturation_register = SATURATION_MODE_FORMATTER1;
639 return SendControlMsg(SET_CHROM_CTL, saturation_register, 1);
644 int pwc_set_agc(struct pwc_device *pdev, int mode, int value)
650 buf = 0x0; /* auto */
652 buf = 0xff; /* fixed */
654 ret = SendControlMsg(SET_LUM_CTL, AGC_MODE_FORMATTER, 1);
656 if (!mode && ret >= 0) {
661 buf = (value >> 10) & 0x3F;
662 ret = SendControlMsg(SET_LUM_CTL, PRESET_AGC_FORMATTER, 1);
669 int pwc_get_agc(struct pwc_device *pdev, int *value)
674 ret = RecvControlMsg(GET_LUM_CTL, AGC_MODE_FORMATTER, 1);
678 if (buf != 0) { /* fixed */
679 ret = RecvControlMsg(GET_LUM_CTL, PRESET_AGC_FORMATTER, 1);
684 *value = (buf << 10);
687 ret = RecvControlMsg(GET_STATUS_CTL, READ_AGC_FORMATTER, 1);
690 /* Gah... this value ranges from 0x00 ... 0x9F */
693 *value = -(48 + buf * 409);
699 int pwc_set_shutter_speed(struct pwc_device *pdev, int mode, int value)
706 buf[0] = 0x0; /* auto */
708 buf[0] = 0xff; /* fixed */
710 ret = SendControlMsg(SET_LUM_CTL, SHUTTER_MODE_FORMATTER, 1);
712 if (!mode && ret >= 0) {
718 if (DEVICE_USE_CODEC2(pdev->type)) {
719 /* speed ranges from 0x0 to 0x290 (656) */
720 speed = (value / 100);
722 buf[0] = speed & 0xff;
723 } else if (DEVICE_USE_CODEC3(pdev->type)) {
724 /* speed seems to range from 0x0 to 0xff */
729 ret = SendControlMsg(SET_LUM_CTL, PRESET_SHUTTER_FORMATTER, 2);
734 /* This function is not exported to v4l1, so output values between 0 -> 256 */
735 int pwc_get_shutter_speed(struct pwc_device *pdev, int *value)
737 unsigned char buf[2];
740 ret = RecvControlMsg(GET_STATUS_CTL, READ_SHUTTER_FORMATTER, 2);
743 *value = buf[0] + (buf[1] << 8);
744 if (DEVICE_USE_CODEC2(pdev->type)) {
745 /* speed ranges from 0x0 to 0x290 (656) */
747 } else if (DEVICE_USE_CODEC3(pdev->type)) {
748 /* speed seems to range from 0x0 to 0xff */
756 int pwc_camera_power(struct pwc_device *pdev, int power)
760 if (pdev->type < 675 || (pdev->type < 730 && pdev->release < 6))
761 return 0; /* Not supported by Nala or Timon < release 6 */
764 buf = 0x00; /* active */
766 buf = 0xFF; /* power save */
767 return SendControlMsg(SET_STATUS_CTL, SET_POWER_SAVE_MODE_FORMATTER, 1);
774 int pwc_restore_user(struct pwc_device *pdev)
776 char buf; /* dummy */
777 return SendControlMsg(SET_STATUS_CTL, RESTORE_USER_DEFAULTS_FORMATTER, 0);
780 int pwc_save_user(struct pwc_device *pdev)
782 char buf; /* dummy */
783 return SendControlMsg(SET_STATUS_CTL, SAVE_USER_DEFAULTS_FORMATTER, 0);
786 int pwc_restore_factory(struct pwc_device *pdev)
788 char buf; /* dummy */
789 return SendControlMsg(SET_STATUS_CTL, RESTORE_FACTORY_DEFAULTS_FORMATTER, 0);
792 /* ************************************************* */
793 /* Patch by Alvarado: (not in the original version */
796 * the camera recognizes modes from 0 to 4:
798 * 00: indoor (incandescant lighting)
799 * 01: outdoor (sunlight)
800 * 02: fluorescent lighting
804 int pwc_set_awb(struct pwc_device *pdev, int mode)
815 buf = mode & 0x07; /* just the lowest three bits */
817 ret = SendControlMsg(SET_CHROM_CTL, WB_MODE_FORMATTER, 1);
824 int pwc_get_awb(struct pwc_device *pdev)
829 ret = RecvControlMsg(GET_CHROM_CTL, WB_MODE_FORMATTER, 1);
836 int pwc_set_red_gain(struct pwc_device *pdev, int value)
844 /* only the msb is considered */
846 return SendControlMsg(SET_CHROM_CTL, PRESET_MANUAL_RED_GAIN_FORMATTER, 1);
849 int pwc_get_red_gain(struct pwc_device *pdev, int *value)
854 ret = RecvControlMsg(GET_CHROM_CTL, PRESET_MANUAL_RED_GAIN_FORMATTER, 1);
862 int pwc_set_blue_gain(struct pwc_device *pdev, int value)
870 /* only the msb is considered */
872 return SendControlMsg(SET_CHROM_CTL, PRESET_MANUAL_BLUE_GAIN_FORMATTER, 1);
875 int pwc_get_blue_gain(struct pwc_device *pdev, int *value)
880 ret = RecvControlMsg(GET_CHROM_CTL, PRESET_MANUAL_BLUE_GAIN_FORMATTER, 1);
888 /* The following two functions are different, since they only read the
889 internal red/blue gains, which may be different from the manual
890 gains set or read above.
892 static int pwc_read_red_gain(struct pwc_device *pdev, int *value)
897 ret = RecvControlMsg(GET_STATUS_CTL, READ_RED_GAIN_FORMATTER, 1);
904 static int pwc_read_blue_gain(struct pwc_device *pdev, int *value)
909 ret = RecvControlMsg(GET_STATUS_CTL, READ_BLUE_GAIN_FORMATTER, 1);
917 static int pwc_set_wb_speed(struct pwc_device *pdev, int speed)
921 /* useful range is 0x01..0x20 */
923 return SendControlMsg(SET_CHROM_CTL, AWB_CONTROL_SPEED_FORMATTER, 1);
926 static int pwc_get_wb_speed(struct pwc_device *pdev, int *value)
931 ret = RecvControlMsg(GET_CHROM_CTL, AWB_CONTROL_SPEED_FORMATTER, 1);
934 *value = buf * 0x7f0;
939 static int pwc_set_wb_delay(struct pwc_device *pdev, int delay)
943 /* useful range is 0x01..0x3F */
945 return SendControlMsg(SET_CHROM_CTL, AWB_CONTROL_DELAY_FORMATTER, 1);
948 static int pwc_get_wb_delay(struct pwc_device *pdev, int *value)
953 ret = RecvControlMsg(GET_CHROM_CTL, AWB_CONTROL_DELAY_FORMATTER, 1);
961 int pwc_set_leds(struct pwc_device *pdev, int on_value, int off_value)
963 unsigned char buf[2];
965 if (pdev->type < 730)
975 if (off_value > 0xff)
981 return SendControlMsg(SET_STATUS_CTL, LED_FORMATTER, 2);
984 static int pwc_get_leds(struct pwc_device *pdev, int *on_value, int *off_value)
986 unsigned char buf[2];
989 if (pdev->type < 730) {
995 ret = RecvControlMsg(GET_STATUS_CTL, LED_FORMATTER, 2);
998 *on_value = buf[0] * 100;
999 *off_value = buf[1] * 100;
1003 int pwc_set_contour(struct pwc_device *pdev, int contour)
1009 buf = 0xff; /* auto contour on */
1011 buf = 0x0; /* auto contour off */
1012 ret = SendControlMsg(SET_LUM_CTL, AUTO_CONTOUR_FORMATTER, 1);
1018 if (contour > 0xffff)
1021 buf = (contour >> 10); /* contour preset is [0..3f] */
1022 ret = SendControlMsg(SET_LUM_CTL, PRESET_CONTOUR_FORMATTER, 1);
1028 int pwc_get_contour(struct pwc_device *pdev, int *contour)
1033 ret = RecvControlMsg(GET_LUM_CTL, AUTO_CONTOUR_FORMATTER, 1);
1038 /* auto mode off, query current preset value */
1039 ret = RecvControlMsg(GET_LUM_CTL, PRESET_CONTOUR_FORMATTER, 1);
1042 *contour = buf << 10;
1050 int pwc_set_backlight(struct pwc_device *pdev, int backlight)
1058 return SendControlMsg(SET_LUM_CTL, BACK_LIGHT_COMPENSATION_FORMATTER, 1);
1061 int pwc_get_backlight(struct pwc_device *pdev, int *backlight)
1066 ret = RecvControlMsg(GET_LUM_CTL, BACK_LIGHT_COMPENSATION_FORMATTER, 1);
1073 int pwc_set_colour_mode(struct pwc_device *pdev, int colour)
1081 return SendControlMsg(SET_CHROM_CTL, COLOUR_MODE_FORMATTER, 1);
1084 int pwc_get_colour_mode(struct pwc_device *pdev, int *colour)
1089 ret = RecvControlMsg(GET_CHROM_CTL, COLOUR_MODE_FORMATTER, 1);
1097 int pwc_set_flicker(struct pwc_device *pdev, int flicker)
1105 return SendControlMsg(SET_LUM_CTL, FLICKERLESS_MODE_FORMATTER, 1);
1108 int pwc_get_flicker(struct pwc_device *pdev, int *flicker)
1113 ret = RecvControlMsg(GET_LUM_CTL, FLICKERLESS_MODE_FORMATTER, 1);
1120 int pwc_set_dynamic_noise(struct pwc_device *pdev, int noise)
1129 return SendControlMsg(SET_LUM_CTL, DYNAMIC_NOISE_CONTROL_FORMATTER, 1);
1132 int pwc_get_dynamic_noise(struct pwc_device *pdev, int *noise)
1137 ret = RecvControlMsg(GET_LUM_CTL, DYNAMIC_NOISE_CONTROL_FORMATTER, 1);
1144 static int _pwc_mpt_reset(struct pwc_device *pdev, int flags)
1148 buf = flags & 0x03; // only lower two bits are currently used
1149 return SendControlMsg(SET_MPT_CTL, PT_RESET_CONTROL_FORMATTER, 1);
1152 int pwc_mpt_reset(struct pwc_device *pdev, int flags)
1155 ret = _pwc_mpt_reset(pdev, flags);
1157 pdev->pan_angle = 0;
1158 pdev->tilt_angle = 0;
1163 static int _pwc_mpt_set_angle(struct pwc_device *pdev, int pan, int tilt)
1165 unsigned char buf[4];
1167 /* set new relative angle; angles are expressed in degrees * 100,
1168 but cam as .5 degree resolution, hence divide by 200. Also
1169 the angle must be multiplied by 64 before it's send to
1172 pan = 64 * pan / 100;
1173 tilt = -64 * tilt / 100; /* positive tilt is down, which is not what the user would expect */
1174 buf[0] = pan & 0xFF;
1175 buf[1] = (pan >> 8) & 0xFF;
1176 buf[2] = tilt & 0xFF;
1177 buf[3] = (tilt >> 8) & 0xFF;
1178 return SendControlMsg(SET_MPT_CTL, PT_RELATIVE_CONTROL_FORMATTER, 4);
1181 int pwc_mpt_set_angle(struct pwc_device *pdev, int pan, int tilt)
1185 /* check absolute ranges */
1186 if (pan < pdev->angle_range.pan_min ||
1187 pan > pdev->angle_range.pan_max ||
1188 tilt < pdev->angle_range.tilt_min ||
1189 tilt > pdev->angle_range.tilt_max)
1192 /* go to relative range, check again */
1193 pan -= pdev->pan_angle;
1194 tilt -= pdev->tilt_angle;
1195 /* angles are specified in degrees * 100, thus the limit = 36000 */
1196 if (pan < -36000 || pan > 36000 || tilt < -36000 || tilt > 36000)
1199 ret = _pwc_mpt_set_angle(pdev, pan, tilt);
1201 pdev->pan_angle += pan;
1202 pdev->tilt_angle += tilt;
1204 if (ret == -EPIPE) /* stall -> out of range */
1209 static int pwc_mpt_get_status(struct pwc_device *pdev, struct pwc_mpt_status *status)
1212 unsigned char buf[5];
1214 ret = RecvControlMsg(GET_MPT_CTL, PT_STATUS_FORMATTER, 5);
1217 status->status = buf[0] & 0x7; // 3 bits are used for reporting
1218 status->time_pan = (buf[1] << 8) + buf[2];
1219 status->time_tilt = (buf[3] << 8) + buf[4];
1224 int pwc_get_cmos_sensor(struct pwc_device *pdev, int *sensor)
1227 int ret = -1, request;
1229 if (pdev->type < 675)
1230 request = SENSOR_TYPE_FORMATTER1;
1231 else if (pdev->type < 730)
1232 return -1; /* The Vesta series doesn't have this call */
1234 request = SENSOR_TYPE_FORMATTER2;
1236 ret = RecvControlMsg(GET_STATUS_CTL, request, 1);
1239 if (pdev->type < 675)
1240 *sensor = buf | 0x100;
1247 /* End of Add-Ons */
1248 /* ************************************************* */
1250 /* Linux 2.5.something and 2.6 pass direct pointers to arguments of
1251 ioctl() calls. With 2.4, you have to do tedious copy_from_user()
1252 and copy_to_user() calls. With these macros we circumvent this,
1253 and let me maintain only one source file. The functionality is
1254 exactly the same otherwise.
1257 /* define local variable for arg */
1258 #define ARG_DEF(ARG_type, ARG_name)\
1259 ARG_type *ARG_name = arg;
1260 /* copy arg to local variable */
1261 #define ARG_IN(ARG_name) /* nothing */
1262 /* argument itself (referenced) */
1263 #define ARGR(ARG_name) (*ARG_name)
1264 /* argument address */
1265 #define ARGA(ARG_name) ARG_name
1266 /* copy local variable to arg */
1267 #define ARG_OUT(ARG_name) /* nothing */
1269 long pwc_ioctl(struct pwc_device *pdev, unsigned int cmd, void *arg)
1274 case VIDIOCPWCRUSER:
1276 if (pwc_restore_user(pdev))
1281 case VIDIOCPWCSUSER:
1283 if (pwc_save_user(pdev))
1288 case VIDIOCPWCFACTORY:
1290 if (pwc_restore_factory(pdev))
1295 case VIDIOCPWCSCQUAL:
1300 if (ARGR(qual) < 0 || ARGR(qual) > 3)
1303 ret = pwc_try_video_mode(pdev, pdev->view.x, pdev->view.y, pdev->vframes, ARGR(qual), pdev->vsnapshot);
1305 pdev->vcompression = ARGR(qual);
1309 case VIDIOCPWCGCQUAL:
1313 ARGR(qual) = pdev->vcompression;
1318 case VIDIOCPWCPROBE:
1320 ARG_DEF(struct pwc_probe, probe)
1322 strcpy(ARGR(probe).name, pdev->vdev->name);
1323 ARGR(probe).type = pdev->type;
1328 case VIDIOCPWCGSERIAL:
1330 ARG_DEF(struct pwc_serial, serial)
1332 strcpy(ARGR(serial).serial, pdev->serial);
1342 if (pwc_set_agc(pdev, ARGR(agc) < 0 ? 1 : 0, ARGR(agc)))
1351 if (pwc_get_agc(pdev, ARGA(agc)))
1357 case VIDIOCPWCSSHUTTER:
1359 ARG_DEF(int, shutter_speed)
1361 ARG_IN(shutter_speed)
1362 ret = pwc_set_shutter_speed(pdev, ARGR(shutter_speed) < 0 ? 1 : 0, ARGR(shutter_speed));
1368 ARG_DEF(struct pwc_whitebalance, wb)
1371 ret = pwc_set_awb(pdev, ARGR(wb).mode);
1372 if (ret >= 0 && ARGR(wb).mode == PWC_WB_MANUAL) {
1373 pwc_set_red_gain(pdev, ARGR(wb).manual_red);
1374 pwc_set_blue_gain(pdev, ARGR(wb).manual_blue);
1381 ARG_DEF(struct pwc_whitebalance, wb)
1383 memset(ARGA(wb), 0, sizeof(struct pwc_whitebalance));
1384 ARGR(wb).mode = pwc_get_awb(pdev);
1385 if (ARGR(wb).mode < 0)
1388 if (ARGR(wb).mode == PWC_WB_MANUAL) {
1389 ret = pwc_get_red_gain(pdev, &ARGR(wb).manual_red);
1392 ret = pwc_get_blue_gain(pdev, &ARGR(wb).manual_blue);
1396 if (ARGR(wb).mode == PWC_WB_AUTO) {
1397 ret = pwc_read_red_gain(pdev, &ARGR(wb).read_red);
1400 ret = pwc_read_blue_gain(pdev, &ARGR(wb).read_blue);
1409 case VIDIOCPWCSAWBSPEED:
1411 ARG_DEF(struct pwc_wb_speed, wbs)
1413 if (ARGR(wbs).control_speed > 0) {
1414 ret = pwc_set_wb_speed(pdev, ARGR(wbs).control_speed);
1416 if (ARGR(wbs).control_delay > 0) {
1417 ret = pwc_set_wb_delay(pdev, ARGR(wbs).control_delay);
1422 case VIDIOCPWCGAWBSPEED:
1424 ARG_DEF(struct pwc_wb_speed, wbs)
1426 ret = pwc_get_wb_speed(pdev, &ARGR(wbs).control_speed);
1429 ret = pwc_get_wb_delay(pdev, &ARGR(wbs).control_delay);
1438 ARG_DEF(struct pwc_leds, leds)
1441 ret = pwc_set_leds(pdev, ARGR(leds).led_on, ARGR(leds).led_off);
1448 ARG_DEF(struct pwc_leds, leds)
1450 ret = pwc_get_leds(pdev, &ARGR(leds).led_on, &ARGR(leds).led_off);
1455 case VIDIOCPWCSCONTOUR:
1457 ARG_DEF(int, contour)
1460 ret = pwc_set_contour(pdev, ARGR(contour));
1464 case VIDIOCPWCGCONTOUR:
1466 ARG_DEF(int, contour)
1468 ret = pwc_get_contour(pdev, ARGA(contour));
1473 case VIDIOCPWCSBACKLIGHT:
1475 ARG_DEF(int, backlight)
1478 ret = pwc_set_backlight(pdev, ARGR(backlight));
1482 case VIDIOCPWCGBACKLIGHT:
1484 ARG_DEF(int, backlight)
1486 ret = pwc_get_backlight(pdev, ARGA(backlight));
1491 case VIDIOCPWCSFLICKER:
1493 ARG_DEF(int, flicker)
1496 ret = pwc_set_flicker(pdev, ARGR(flicker));
1500 case VIDIOCPWCGFLICKER:
1502 ARG_DEF(int, flicker)
1504 ret = pwc_get_flicker(pdev, ARGA(flicker));
1509 case VIDIOCPWCSDYNNOISE:
1511 ARG_DEF(int, dynnoise)
1514 ret = pwc_set_dynamic_noise(pdev, ARGR(dynnoise));
1518 case VIDIOCPWCGDYNNOISE:
1520 ARG_DEF(int, dynnoise)
1522 ret = pwc_get_dynamic_noise(pdev, ARGA(dynnoise));
1527 case VIDIOCPWCGREALSIZE:
1529 ARG_DEF(struct pwc_imagesize, size)
1531 ARGR(size).width = pdev->image.x;
1532 ARGR(size).height = pdev->image.y;
1537 case VIDIOCPWCMPTRESET:
1539 if (pdev->features & FEATURE_MOTOR_PANTILT)
1544 ret = pwc_mpt_reset(pdev, ARGR(flags));
1553 case VIDIOCPWCMPTGRANGE:
1555 if (pdev->features & FEATURE_MOTOR_PANTILT)
1557 ARG_DEF(struct pwc_mpt_range, range)
1559 ARGR(range) = pdev->angle_range;
1569 case VIDIOCPWCMPTSANGLE:
1571 int new_pan, new_tilt;
1573 if (pdev->features & FEATURE_MOTOR_PANTILT)
1575 ARG_DEF(struct pwc_mpt_angles, angles)
1578 /* The camera can only set relative angles, so
1579 do some calculations when getting an absolute angle .
1581 if (ARGR(angles).absolute)
1583 new_pan = ARGR(angles).pan;
1584 new_tilt = ARGR(angles).tilt;
1588 new_pan = pdev->pan_angle + ARGR(angles).pan;
1589 new_tilt = pdev->tilt_angle + ARGR(angles).tilt;
1591 ret = pwc_mpt_set_angle(pdev, new_pan, new_tilt);
1600 case VIDIOCPWCMPTGANGLE:
1603 if (pdev->features & FEATURE_MOTOR_PANTILT)
1605 ARG_DEF(struct pwc_mpt_angles, angles)
1607 ARGR(angles).absolute = 1;
1608 ARGR(angles).pan = pdev->pan_angle;
1609 ARGR(angles).tilt = pdev->tilt_angle;
1619 case VIDIOCPWCMPTSTATUS:
1621 if (pdev->features & FEATURE_MOTOR_PANTILT)
1623 ARG_DEF(struct pwc_mpt_status, status)
1625 ret = pwc_mpt_get_status(pdev, ARGA(status));
1635 case VIDIOCPWCGVIDCMD:
1637 ARG_DEF(struct pwc_video_command, vcmd);
1639 ARGR(vcmd).type = pdev->type;
1640 ARGR(vcmd).release = pdev->release;
1641 ARGR(vcmd).command_len = pdev->cmd_len;
1642 memcpy(&ARGR(vcmd).command_buf, pdev->cmd_buf, pdev->cmd_len);
1643 ARGR(vcmd).bandlength = pdev->vbandlength;
1644 ARGR(vcmd).frame_size = pdev->frame_size;
1649 case VIDIOCPWCGVIDTABLE:
1651 ARG_DEF(struct pwc_table_init_buffer, table);
1652 ARGR(table).len = pdev->cmd_len;
1653 memcpy(&ARGR(table).buffer, pdev->decompress_data, pdev->decompressor->table_size);
1670 /* vim: set cinoptions= formatoptions=croql cindent shiftwidth=8 tabstop=8: */