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 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-ioctl.h"
47 #include "pwc-uncompress.h"
48 #include "pwc-kiara.h"
49 #include "pwc-timon.h"
51 /* Request types: video */
52 #define SET_LUM_CTL 0x01
53 #define GET_LUM_CTL 0x02
54 #define SET_CHROM_CTL 0x03
55 #define GET_CHROM_CTL 0x04
56 #define SET_STATUS_CTL 0x05
57 #define GET_STATUS_CTL 0x06
58 #define SET_EP_STREAM_CTL 0x07
59 #define GET_EP_STREAM_CTL 0x08
60 #define SET_MPT_CTL 0x0D
61 #define GET_MPT_CTL 0x0E
63 /* Selectors for the Luminance controls [GS]ET_LUM_CTL */
64 #define AGC_MODE_FORMATTER 0x2000
65 #define PRESET_AGC_FORMATTER 0x2100
66 #define SHUTTER_MODE_FORMATTER 0x2200
67 #define PRESET_SHUTTER_FORMATTER 0x2300
68 #define PRESET_CONTOUR_FORMATTER 0x2400
69 #define AUTO_CONTOUR_FORMATTER 0x2500
70 #define BACK_LIGHT_COMPENSATION_FORMATTER 0x2600
71 #define CONTRAST_FORMATTER 0x2700
72 #define DYNAMIC_NOISE_CONTROL_FORMATTER 0x2800
73 #define FLICKERLESS_MODE_FORMATTER 0x2900
74 #define AE_CONTROL_SPEED 0x2A00
75 #define BRIGHTNESS_FORMATTER 0x2B00
76 #define GAMMA_FORMATTER 0x2C00
78 /* Selectors for the Chrominance controls [GS]ET_CHROM_CTL */
79 #define WB_MODE_FORMATTER 0x1000
80 #define AWB_CONTROL_SPEED_FORMATTER 0x1100
81 #define AWB_CONTROL_DELAY_FORMATTER 0x1200
82 #define PRESET_MANUAL_RED_GAIN_FORMATTER 0x1300
83 #define PRESET_MANUAL_BLUE_GAIN_FORMATTER 0x1400
84 #define COLOUR_MODE_FORMATTER 0x1500
85 #define SATURATION_MODE_FORMATTER1 0x1600
86 #define SATURATION_MODE_FORMATTER2 0x1700
88 /* Selectors for the Status controls [GS]ET_STATUS_CTL */
89 #define SAVE_USER_DEFAULTS_FORMATTER 0x0200
90 #define RESTORE_USER_DEFAULTS_FORMATTER 0x0300
91 #define RESTORE_FACTORY_DEFAULTS_FORMATTER 0x0400
92 #define READ_AGC_FORMATTER 0x0500
93 #define READ_SHUTTER_FORMATTER 0x0600
94 #define READ_RED_GAIN_FORMATTER 0x0700
95 #define READ_BLUE_GAIN_FORMATTER 0x0800
96 #define SENSOR_TYPE_FORMATTER1 0x0C00
97 #define READ_RAW_Y_MEAN_FORMATTER 0x3100
98 #define SET_POWER_SAVE_MODE_FORMATTER 0x3200
99 #define MIRROR_IMAGE_FORMATTER 0x3300
100 #define LED_FORMATTER 0x3400
101 #define SENSOR_TYPE_FORMATTER2 0x3700
103 /* Formatters for the Video Endpoint controls [GS]ET_EP_STREAM_CTL */
104 #define VIDEO_OUTPUT_CONTROL_FORMATTER 0x0100
106 /* Formatters for the motorized pan & tilt [GS]ET_MPT_CTL */
107 #define PT_RELATIVE_CONTROL_FORMATTER 0x01
108 #define PT_RESET_CONTROL_FORMATTER 0x02
109 #define PT_STATUS_FORMATTER 0x03
111 static const char *size2name[PSZ_MAX] =
123 /* Entries for the Nala (645/646) camera; the Nala doesn't have compression
124 preferences, so you either get compressed or non-compressed streams.
126 An alternate value of 0 means this mode is not available at all.
129 struct Nala_table_entry {
130 char alternate; /* USB alternate setting */
131 int compressed; /* Compressed yes/no */
133 unsigned char mode[3]; /* precomputed mode table */
136 static struct Nala_table_entry Nala_table[PSZ_MAX][8] =
138 #include "pwc-nala.h"
142 /****************************************************************************/
145 #define SendControlMsg(request, value, buflen) \
146 usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0), \
148 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, \
153 #define RecvControlMsg(request, value, buflen) \
154 usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0), \
156 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, \
163 void pwc_hexdump(void *p, int len)
169 s = (unsigned char *)p;
172 Debug("Doing hexdump @ %p, %d bytes.\n", p, len);
173 for (i = 0; i < len; i++) {
174 d += sprintf(d, "%02X ", *s++);
175 if ((i & 0xF) == 0xF) {
186 static inline int send_video_command(struct usb_device *udev, int index, void *buf, int buflen)
188 return usb_control_msg(udev,
189 usb_sndctrlpipe(udev, 0),
191 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
192 VIDEO_OUTPUT_CONTROL_FORMATTER,
199 static inline int set_video_mode_Nala(struct pwc_device *pdev, int size, int frames)
201 unsigned char buf[3];
203 struct Nala_table_entry *pEntry;
204 int frames2frames[31] =
205 { /* closest match of framerate */
206 0, 0, 0, 0, 4, /* 0-4 */
207 5, 5, 7, 7, 10, /* 5-9 */
208 10, 10, 12, 12, 15, /* 10-14 */
209 15, 15, 15, 20, 20, /* 15-19 */
210 20, 20, 20, 24, 24, /* 20-24 */
211 24, 24, 24, 24, 24, /* 25-29 */
214 int frames2table[31] =
215 { 0, 0, 0, 0, 0, /* 0-4 */
216 1, 1, 1, 2, 2, /* 5-9 */
217 3, 3, 4, 4, 4, /* 10-14 */
218 5, 5, 5, 5, 5, /* 15-19 */
219 6, 6, 6, 6, 7, /* 20-24 */
220 7, 7, 7, 7, 7, /* 25-29 */
224 if (size < 0 || size > PSZ_CIF || frames < 4 || frames > 25)
226 frames = frames2frames[frames];
227 fps = frames2table[frames];
228 pEntry = &Nala_table[size][fps];
229 if (pEntry->alternate == 0)
232 if (pEntry->compressed)
233 return -ENOENT; /* Not supported. */
235 memcpy(buf, pEntry->mode, 3);
236 ret = send_video_command(pdev->udev, pdev->vendpoint, buf, 3);
238 Debug("Failed to send video command... %d\n", ret);
241 if (pEntry->compressed && pdev->vpalette != VIDEO_PALETTE_RAW)
246 /* pwc_dec1_init(pdev->type, pdev->release, buf, pdev->decompress_data); */
256 /* pwc_dec23_init(pdev->type, pdev->release, buf, pdev->decompress_data); */
262 memcpy(pdev->cmd_buf, buf, 3);
264 /* Set various parameters */
265 pdev->vframes = frames;
267 pdev->valternate = pEntry->alternate;
268 pdev->image = pwc_image_sizes[size];
269 pdev->frame_size = (pdev->image.x * pdev->image.y * 3) / 2;
270 if (pEntry->compressed) {
271 if (pdev->release < 5) { /* 4 fold compression */
272 pdev->vbandlength = 528;
273 pdev->frame_size /= 4;
276 pdev->vbandlength = 704;
277 pdev->frame_size /= 3;
281 pdev->vbandlength = 0;
286 static inline int set_video_mode_Timon(struct pwc_device *pdev, int size, int frames, int compression, int snapshot)
288 unsigned char buf[13];
289 const struct Timon_table_entry *pChoose;
292 if (size >= PSZ_MAX || frames < 5 || frames > 30 || compression < 0 || compression > 3)
294 if (size == PSZ_VGA && frames > 15)
296 fps = (frames / 5) - 1;
298 /* Find a supported framerate with progressively higher compression ratios
299 if the preferred ratio is not available.
302 while (compression <= 3) {
303 pChoose = &Timon_table[size][fps][compression];
304 if (pChoose->alternate != 0)
308 if (pChoose == NULL || pChoose->alternate == 0)
309 return -ENOENT; /* Not supported. */
311 memcpy(buf, pChoose->mode, 13);
314 ret = send_video_command(pdev->udev, pdev->vendpoint, buf, 13);
318 /* if (pChoose->bandlength > 0 && pdev->vpalette != VIDEO_PALETTE_RAW)
319 pwc_dec23_init(pdev->type, pdev->release, buf, pdev->decompress_data); */
322 memcpy(pdev->cmd_buf, buf, 13);
324 /* Set various parameters */
325 pdev->vframes = frames;
327 pdev->vsnapshot = snapshot;
328 pdev->valternate = pChoose->alternate;
329 pdev->image = pwc_image_sizes[size];
330 pdev->vbandlength = pChoose->bandlength;
331 if (pChoose->bandlength > 0)
332 pdev->frame_size = (pChoose->bandlength * pdev->image.y) / 4;
334 pdev->frame_size = (pdev->image.x * pdev->image.y * 12) / 8;
339 static inline int set_video_mode_Kiara(struct pwc_device *pdev, int size, int frames, int compression, int snapshot)
341 const struct Kiara_table_entry *pChoose = NULL;
343 unsigned char buf[12];
344 struct Kiara_table_entry RawEntry = {6, 773, 1272, {0xAD, 0xF4, 0x10, 0x27, 0xB6, 0x24, 0x96, 0x02, 0x30, 0x05, 0x03, 0x80}};
346 if (size >= PSZ_MAX || frames < 5 || frames > 30 || compression < 0 || compression > 3)
348 if (size == PSZ_VGA && frames > 15)
350 fps = (frames / 5) - 1;
352 /* special case: VGA @ 5 fps and snapshot is raw bayer mode */
353 if (size == PSZ_VGA && frames == 5 && snapshot)
355 /* Only available in case the raw palette is selected or
356 we have the decompressor available. This mode is
357 only available in compressed form
359 if (pdev->vpalette == VIDEO_PALETTE_RAW)
361 Info("Choosing VGA/5 BAYER mode (%d).\n", pdev->vpalette);
366 Info("VGA/5 BAYER mode _must_ have a decompressor available, or use RAW palette.\n");
371 /* Find a supported framerate with progressively higher compression ratios
372 if the preferred ratio is not available.
373 Skip this step when using RAW modes.
375 while (compression <= 3) {
376 pChoose = &Kiara_table[size][fps][compression];
377 if (pChoose->alternate != 0)
382 if (pChoose == NULL || pChoose->alternate == 0)
383 return -ENOENT; /* Not supported. */
385 Debug("Using alternate setting %d.\n", pChoose->alternate);
387 /* usb_control_msg won't take staticly allocated arrays as argument?? */
388 memcpy(buf, pChoose->mode, 12);
392 /* Firmware bug: video endpoint is 5, but commands are sent to endpoint 4 */
393 ret = send_video_command(pdev->udev, 4 /* pdev->vendpoint */, buf, 12);
397 /* if (pChoose->bandlength > 0 && pdev->vpalette != VIDEO_PALETTE_RAW)
398 pwc_dec23_init(pdev->type, pdev->release, buf, pdev->decompress_data); */
401 memcpy(pdev->cmd_buf, buf, 12);
403 pdev->vframes = frames;
405 pdev->vsnapshot = snapshot;
406 pdev->valternate = pChoose->alternate;
407 pdev->image = pwc_image_sizes[size];
408 pdev->vbandlength = pChoose->bandlength;
409 if (pdev->vbandlength > 0)
410 pdev->frame_size = (pdev->vbandlength * pdev->image.y) / 4;
412 pdev->frame_size = (pdev->image.x * pdev->image.y * 12) / 8;
418 static void pwc_set_image_buffer_size(struct pwc_device *pdev)
420 int i, factor = 0, filler = 0;
422 /* for PALETTE_YUV420P */
423 switch(pdev->vpalette)
425 case VIDEO_PALETTE_YUV420P:
429 case VIDEO_PALETTE_RAW:
430 factor = 6; /* can be uncompressed YUV420P */
435 /* Set sizes in bytes */
436 pdev->image.size = pdev->image.x * pdev->image.y * factor / 4;
437 pdev->view.size = pdev->view.x * pdev->view.y * factor / 4;
439 /* Align offset, or you'll get some very weird results in
440 YUV420 mode... x must be multiple of 4 (to get the Y's in
441 place), and y even (or you'll mixup U & V). This is less of a
444 pdev->offset.x = ((pdev->view.x - pdev->image.x) / 2) & 0xFFFC;
445 pdev->offset.y = ((pdev->view.y - pdev->image.y) / 2) & 0xFFFE;
447 /* Fill buffers with gray or black */
448 for (i = 0; i < MAX_IMAGES; i++) {
449 if (pdev->image_ptr[i] != NULL)
450 memset(pdev->image_ptr[i], filler, pdev->view.size);
457 @pdev: device structure
458 @width: viewport width
459 @height: viewport height
460 @frame: framerate, in fps
461 @compression: preferred compression ratio
462 @snapshot: snapshot mode or streaming
464 int pwc_set_video_mode(struct pwc_device *pdev, int width, int height, int frames, int compression, int snapshot)
468 Trace(TRACE_FLOW, "set_video_mode(%dx%d @ %d, palette %d).\n", width, height, frames, pdev->vpalette);
469 size = pwc_decode_size(pdev, width, height);
471 Debug("Could not find suitable size.\n");
474 Debug("decode_size = %d.\n", size);
480 ret = set_video_mode_Nala(pdev, size, frames);
486 ret = set_video_mode_Timon(pdev, size, frames, compression, snapshot);
493 ret = set_video_mode_Kiara(pdev, size, frames, compression, snapshot);
498 Info("Video mode %s@%d fps is only supported with the decompressor module (pwcx).\n", size2name[size], frames);
500 Err("Failed to set video mode %s@%d fps; return code = %d\n", size2name[size], frames, ret);
504 pdev->view.x = width;
505 pdev->view.y = height;
506 pdev->frame_total_size = pdev->frame_size + pdev->frame_header_size + pdev->frame_trailer_size;
507 pwc_set_image_buffer_size(pdev);
508 Trace(TRACE_SIZE, "Set viewport to %dx%d, image size is %dx%d.\n", width, height, pwc_image_sizes[size].x, pwc_image_sizes[size].y);
515 int pwc_get_brightness(struct pwc_device *pdev)
520 ret = RecvControlMsg(GET_LUM_CTL, BRIGHTNESS_FORMATTER, 1);
526 int pwc_set_brightness(struct pwc_device *pdev, int value)
534 buf = (value >> 9) & 0x7f;
535 return SendControlMsg(SET_LUM_CTL, BRIGHTNESS_FORMATTER, 1);
540 int pwc_get_contrast(struct pwc_device *pdev)
545 ret = RecvControlMsg(GET_LUM_CTL, CONTRAST_FORMATTER, 1);
551 int pwc_set_contrast(struct pwc_device *pdev, int value)
559 buf = (value >> 10) & 0x3f;
560 return SendControlMsg(SET_LUM_CTL, CONTRAST_FORMATTER, 1);
565 int pwc_get_gamma(struct pwc_device *pdev)
570 ret = RecvControlMsg(GET_LUM_CTL, GAMMA_FORMATTER, 1);
576 int pwc_set_gamma(struct pwc_device *pdev, int value)
584 buf = (value >> 11) & 0x1f;
585 return SendControlMsg(SET_LUM_CTL, GAMMA_FORMATTER, 1);
591 int pwc_get_saturation(struct pwc_device *pdev)
596 if (pdev->type < 675)
598 ret = RecvControlMsg(GET_CHROM_CTL, pdev->type < 730 ? SATURATION_MODE_FORMATTER2 : SATURATION_MODE_FORMATTER1, 1);
601 return 32768 + buf * 327;
604 int pwc_set_saturation(struct pwc_device *pdev, int value)
608 if (pdev->type < 675)
614 /* saturation ranges from -100 to +100 */
615 buf = (value - 32768) / 327;
616 return SendControlMsg(SET_CHROM_CTL, pdev->type < 730 ? SATURATION_MODE_FORMATTER2 : SATURATION_MODE_FORMATTER1, 1);
621 static inline int pwc_set_agc(struct pwc_device *pdev, int mode, int value)
627 buf = 0x0; /* auto */
629 buf = 0xff; /* fixed */
631 ret = SendControlMsg(SET_LUM_CTL, AGC_MODE_FORMATTER, 1);
633 if (!mode && ret >= 0) {
638 buf = (value >> 10) & 0x3F;
639 ret = SendControlMsg(SET_LUM_CTL, PRESET_AGC_FORMATTER, 1);
646 static inline int pwc_get_agc(struct pwc_device *pdev, int *value)
651 ret = RecvControlMsg(GET_LUM_CTL, AGC_MODE_FORMATTER, 1);
655 if (buf != 0) { /* fixed */
656 ret = RecvControlMsg(GET_LUM_CTL, PRESET_AGC_FORMATTER, 1);
661 *value = (buf << 10);
664 ret = RecvControlMsg(GET_STATUS_CTL, READ_AGC_FORMATTER, 1);
667 /* Gah... this value ranges from 0x00 ... 0x9F */
670 *value = -(48 + buf * 409);
676 static inline int pwc_set_shutter_speed(struct pwc_device *pdev, int mode, int value)
683 buf[0] = 0x0; /* auto */
685 buf[0] = 0xff; /* fixed */
687 ret = SendControlMsg(SET_LUM_CTL, SHUTTER_MODE_FORMATTER, 1);
689 if (!mode && ret >= 0) {
698 /* speed ranges from 0x0 to 0x290 (656) */
699 speed = (value / 100);
701 buf[0] = speed & 0xff;
707 /* speed seems to range from 0x0 to 0xff */
713 ret = SendControlMsg(SET_LUM_CTL, PRESET_SHUTTER_FORMATTER, 2);
721 int pwc_camera_power(struct pwc_device *pdev, int power)
725 if (pdev->type < 675 || (pdev->type < 730 && pdev->release < 6))
726 return 0; /* Not supported by Nala or Timon < release 6 */
729 buf = 0x00; /* active */
731 buf = 0xFF; /* power save */
732 return SendControlMsg(SET_STATUS_CTL, SET_POWER_SAVE_MODE_FORMATTER, 1);
739 static inline int pwc_restore_user(struct pwc_device *pdev)
741 char buf; /* dummy */
742 return SendControlMsg(SET_STATUS_CTL, RESTORE_USER_DEFAULTS_FORMATTER, 0);
745 static inline int pwc_save_user(struct pwc_device *pdev)
747 char buf; /* dummy */
748 return SendControlMsg(SET_STATUS_CTL, SAVE_USER_DEFAULTS_FORMATTER, 0);
751 static inline int pwc_restore_factory(struct pwc_device *pdev)
753 char buf; /* dummy */
754 return SendControlMsg(SET_STATUS_CTL, RESTORE_FACTORY_DEFAULTS_FORMATTER, 0);
757 /* ************************************************* */
758 /* Patch by Alvarado: (not in the original version */
761 * the camera recognizes modes from 0 to 4:
763 * 00: indoor (incandescant lighting)
764 * 01: outdoor (sunlight)
765 * 02: fluorescent lighting
769 static inline int pwc_set_awb(struct pwc_device *pdev, int mode)
780 buf = mode & 0x07; /* just the lowest three bits */
782 ret = SendControlMsg(SET_CHROM_CTL, WB_MODE_FORMATTER, 1);
789 static inline int pwc_get_awb(struct pwc_device *pdev)
794 ret = RecvControlMsg(GET_CHROM_CTL, WB_MODE_FORMATTER, 1);
801 static inline int pwc_set_red_gain(struct pwc_device *pdev, int value)
809 /* only the msb is considered */
811 return SendControlMsg(SET_CHROM_CTL, PRESET_MANUAL_RED_GAIN_FORMATTER, 1);
814 static inline int pwc_get_red_gain(struct pwc_device *pdev, int *value)
819 ret = RecvControlMsg(GET_CHROM_CTL, PRESET_MANUAL_RED_GAIN_FORMATTER, 1);
827 static inline int pwc_set_blue_gain(struct pwc_device *pdev, int value)
835 /* only the msb is considered */
837 return SendControlMsg(SET_CHROM_CTL, PRESET_MANUAL_BLUE_GAIN_FORMATTER, 1);
840 static inline int pwc_get_blue_gain(struct pwc_device *pdev, int *value)
845 ret = RecvControlMsg(GET_CHROM_CTL, PRESET_MANUAL_BLUE_GAIN_FORMATTER, 1);
853 /* The following two functions are different, since they only read the
854 internal red/blue gains, which may be different from the manual
855 gains set or read above.
857 static inline int pwc_read_red_gain(struct pwc_device *pdev, int *value)
862 ret = RecvControlMsg(GET_STATUS_CTL, READ_RED_GAIN_FORMATTER, 1);
869 static inline int pwc_read_blue_gain(struct pwc_device *pdev, int *value)
874 ret = RecvControlMsg(GET_STATUS_CTL, READ_BLUE_GAIN_FORMATTER, 1);
882 static inline int pwc_set_wb_speed(struct pwc_device *pdev, int speed)
886 /* useful range is 0x01..0x20 */
888 return SendControlMsg(SET_CHROM_CTL, AWB_CONTROL_SPEED_FORMATTER, 1);
891 static inline int pwc_get_wb_speed(struct pwc_device *pdev, int *value)
896 ret = RecvControlMsg(GET_CHROM_CTL, AWB_CONTROL_SPEED_FORMATTER, 1);
899 *value = buf * 0x7f0;
904 static inline int pwc_set_wb_delay(struct pwc_device *pdev, int delay)
908 /* useful range is 0x01..0x3F */
910 return SendControlMsg(SET_CHROM_CTL, AWB_CONTROL_DELAY_FORMATTER, 1);
913 static inline int pwc_get_wb_delay(struct pwc_device *pdev, int *value)
918 ret = RecvControlMsg(GET_CHROM_CTL, AWB_CONTROL_DELAY_FORMATTER, 1);
926 int pwc_set_leds(struct pwc_device *pdev, int on_value, int off_value)
928 unsigned char buf[2];
930 if (pdev->type < 730)
940 if (off_value > 0xff)
946 return SendControlMsg(SET_STATUS_CTL, LED_FORMATTER, 2);
949 static int pwc_get_leds(struct pwc_device *pdev, int *on_value, int *off_value)
951 unsigned char buf[2];
954 if (pdev->type < 730) {
960 ret = RecvControlMsg(GET_STATUS_CTL, LED_FORMATTER, 2);
963 *on_value = buf[0] * 100;
964 *off_value = buf[1] * 100;
968 static inline int pwc_set_contour(struct pwc_device *pdev, int contour)
974 buf = 0xff; /* auto contour on */
976 buf = 0x0; /* auto contour off */
977 ret = SendControlMsg(SET_LUM_CTL, AUTO_CONTOUR_FORMATTER, 1);
983 if (contour > 0xffff)
986 buf = (contour >> 10); /* contour preset is [0..3f] */
987 ret = SendControlMsg(SET_LUM_CTL, PRESET_CONTOUR_FORMATTER, 1);
993 static inline int pwc_get_contour(struct pwc_device *pdev, int *contour)
998 ret = RecvControlMsg(GET_LUM_CTL, AUTO_CONTOUR_FORMATTER, 1);
1003 /* auto mode off, query current preset value */
1004 ret = RecvControlMsg(GET_LUM_CTL, PRESET_CONTOUR_FORMATTER, 1);
1007 *contour = buf << 10;
1015 static inline int pwc_set_backlight(struct pwc_device *pdev, int backlight)
1023 return SendControlMsg(SET_LUM_CTL, BACK_LIGHT_COMPENSATION_FORMATTER, 1);
1026 static inline int pwc_get_backlight(struct pwc_device *pdev, int *backlight)
1031 ret = RecvControlMsg(GET_LUM_CTL, BACK_LIGHT_COMPENSATION_FORMATTER, 1);
1039 static inline int pwc_set_flicker(struct pwc_device *pdev, int flicker)
1047 return SendControlMsg(SET_LUM_CTL, FLICKERLESS_MODE_FORMATTER, 1);
1050 static inline int pwc_get_flicker(struct pwc_device *pdev, int *flicker)
1055 ret = RecvControlMsg(GET_LUM_CTL, FLICKERLESS_MODE_FORMATTER, 1);
1063 static inline int pwc_set_dynamic_noise(struct pwc_device *pdev, int noise)
1072 return SendControlMsg(SET_LUM_CTL, DYNAMIC_NOISE_CONTROL_FORMATTER, 1);
1075 static inline int pwc_get_dynamic_noise(struct pwc_device *pdev, int *noise)
1080 ret = RecvControlMsg(GET_LUM_CTL, DYNAMIC_NOISE_CONTROL_FORMATTER, 1);
1087 static int pwc_mpt_reset(struct pwc_device *pdev, int flags)
1091 buf = flags & 0x03; // only lower two bits are currently used
1092 return SendControlMsg(SET_MPT_CTL, PT_RESET_CONTROL_FORMATTER, 1);
1095 static inline int pwc_mpt_set_angle(struct pwc_device *pdev, int pan, int tilt)
1097 unsigned char buf[4];
1099 /* set new relative angle; angles are expressed in degrees * 100,
1100 but cam as .5 degree resolution, hence divide by 200. Also
1101 the angle must be multiplied by 64 before it's send to
1104 pan = 64 * pan / 100;
1105 tilt = -64 * tilt / 100; /* positive tilt is down, which is not what the user would expect */
1106 buf[0] = pan & 0xFF;
1107 buf[1] = (pan >> 8) & 0xFF;
1108 buf[2] = tilt & 0xFF;
1109 buf[3] = (tilt >> 8) & 0xFF;
1110 return SendControlMsg(SET_MPT_CTL, PT_RELATIVE_CONTROL_FORMATTER, 4);
1113 static inline int pwc_mpt_get_status(struct pwc_device *pdev, struct pwc_mpt_status *status)
1116 unsigned char buf[5];
1118 ret = RecvControlMsg(GET_MPT_CTL, PT_STATUS_FORMATTER, 5);
1121 status->status = buf[0] & 0x7; // 3 bits are used for reporting
1122 status->time_pan = (buf[1] << 8) + buf[2];
1123 status->time_tilt = (buf[3] << 8) + buf[4];
1128 int pwc_get_cmos_sensor(struct pwc_device *pdev, int *sensor)
1131 int ret = -1, request;
1133 if (pdev->type < 675)
1134 request = SENSOR_TYPE_FORMATTER1;
1135 else if (pdev->type < 730)
1136 return -1; /* The Vesta series doesn't have this call */
1138 request = SENSOR_TYPE_FORMATTER2;
1140 ret = RecvControlMsg(GET_STATUS_CTL, request, 1);
1143 if (pdev->type < 675)
1144 *sensor = buf | 0x100;
1151 /* End of Add-Ons */
1152 /* ************************************************* */
1155 int pwc_ioctl(struct pwc_device *pdev, unsigned int cmd, void *arg)
1160 case VIDIOCPWCRUSER:
1162 if (pwc_restore_user(pdev))
1167 case VIDIOCPWCSUSER:
1169 if (pwc_save_user(pdev))
1174 case VIDIOCPWCFACTORY:
1176 if (pwc_restore_factory(pdev))
1181 case VIDIOCPWCSCQUAL:
1185 if (*qual < 0 || *qual > 3)
1188 ret = pwc_try_video_mode(pdev, pdev->view.x, pdev->view.y, pdev->vframes, *qual, pdev->vsnapshot);
1190 pdev->vcompression = *qual;
1194 case VIDIOCPWCGCQUAL:
1197 *qual = pdev->vcompression;
1201 case VIDIOCPWCPROBE:
1203 struct pwc_probe *probe = arg;
1204 strcpy(probe->name, pdev->vdev->name);
1205 probe->type = pdev->type;
1209 case VIDIOCPWCGSERIAL:
1211 struct pwc_serial *serial = arg;
1212 strcpy(serial->serial, pdev->serial);
1219 if (pwc_set_agc(pdev, *agc < 0 ? 1 : 0, *agc))
1228 if (pwc_get_agc(pdev, agc))
1233 case VIDIOCPWCSSHUTTER:
1235 int *shutter_speed = arg;
1236 ret = pwc_set_shutter_speed(pdev, *shutter_speed < 0 ? 1 : 0, *shutter_speed);
1242 struct pwc_whitebalance *wb = arg;
1244 ret = pwc_set_awb(pdev, wb->mode);
1245 if (ret >= 0 && wb->mode == PWC_WB_MANUAL) {
1246 pwc_set_red_gain(pdev, wb->manual_red);
1247 pwc_set_blue_gain(pdev, wb->manual_blue);
1254 struct pwc_whitebalance *wb = arg;
1256 memset(wb, 0, sizeof(struct pwc_whitebalance));
1257 wb->mode = pwc_get_awb(pdev);
1261 if (wb->mode == PWC_WB_MANUAL) {
1262 ret = pwc_get_red_gain(pdev, &wb->manual_red);
1265 ret = pwc_get_blue_gain(pdev, &wb->manual_blue);
1269 if (wb->mode == PWC_WB_AUTO) {
1270 ret = pwc_read_red_gain(pdev, &wb->read_red);
1273 ret = pwc_read_blue_gain(pdev, &wb->read_blue);
1281 case VIDIOCPWCSAWBSPEED:
1283 struct pwc_wb_speed *wbs = arg;
1285 if (wbs->control_speed > 0) {
1286 ret = pwc_set_wb_speed(pdev, wbs->control_speed);
1288 if (wbs->control_delay > 0) {
1289 ret = pwc_set_wb_delay(pdev, wbs->control_delay);
1294 case VIDIOCPWCGAWBSPEED:
1296 struct pwc_wb_speed *wbs = arg;
1298 ret = pwc_get_wb_speed(pdev, &wbs->control_speed);
1301 ret = pwc_get_wb_delay(pdev, &wbs->control_delay);
1309 struct pwc_leds *leds = arg;
1310 ret = pwc_set_leds(pdev, leds->led_on, leds->led_off);
1317 struct pwc_leds *leds = arg;
1318 ret = pwc_get_leds(pdev, &leds->led_on, &leds->led_off);
1322 case VIDIOCPWCSCONTOUR:
1325 ret = pwc_set_contour(pdev, *contour);
1329 case VIDIOCPWCGCONTOUR:
1332 ret = pwc_get_contour(pdev, contour);
1336 case VIDIOCPWCSBACKLIGHT:
1338 int *backlight = arg;
1339 ret = pwc_set_backlight(pdev, *backlight);
1343 case VIDIOCPWCGBACKLIGHT:
1345 int *backlight = arg;
1346 ret = pwc_get_backlight(pdev, backlight);
1350 case VIDIOCPWCSFLICKER:
1353 ret = pwc_set_flicker(pdev, *flicker);
1357 case VIDIOCPWCGFLICKER:
1360 ret = pwc_get_flicker(pdev, flicker);
1364 case VIDIOCPWCSDYNNOISE:
1366 int *dynnoise = arg;
1367 ret = pwc_set_dynamic_noise(pdev, *dynnoise);
1371 case VIDIOCPWCGDYNNOISE:
1373 int *dynnoise = arg;
1374 ret = pwc_get_dynamic_noise(pdev, dynnoise);
1378 case VIDIOCPWCGREALSIZE:
1380 struct pwc_imagesize *size = arg;
1381 size->width = pdev->image.x;
1382 size->height = pdev->image.y;
1386 case VIDIOCPWCMPTRESET:
1388 if (pdev->features & FEATURE_MOTOR_PANTILT)
1392 ret = pwc_mpt_reset(pdev, *flags);
1395 pdev->pan_angle = 0;
1396 pdev->tilt_angle = 0;
1406 case VIDIOCPWCMPTGRANGE:
1408 if (pdev->features & FEATURE_MOTOR_PANTILT)
1410 struct pwc_mpt_range *range = arg;
1411 *range = pdev->angle_range;
1420 case VIDIOCPWCMPTSANGLE:
1422 int new_pan, new_tilt;
1424 if (pdev->features & FEATURE_MOTOR_PANTILT)
1426 struct pwc_mpt_angles *angles = arg;
1427 /* The camera can only set relative angles, so
1428 do some calculations when getting an absolute angle .
1430 if (angles->absolute)
1432 new_pan = angles->pan;
1433 new_tilt = angles->tilt;
1437 new_pan = pdev->pan_angle + angles->pan;
1438 new_tilt = pdev->tilt_angle + angles->tilt;
1440 /* check absolute ranges */
1441 if (new_pan < pdev->angle_range.pan_min ||
1442 new_pan > pdev->angle_range.pan_max ||
1443 new_tilt < pdev->angle_range.tilt_min ||
1444 new_tilt > pdev->angle_range.tilt_max)
1450 /* go to relative range, check again */
1451 new_pan -= pdev->pan_angle;
1452 new_tilt -= pdev->tilt_angle;
1453 /* angles are specified in degrees * 100, thus the limit = 36000 */
1454 if (new_pan < -36000 || new_pan > 36000 || new_tilt < -36000 || new_tilt > 36000)
1457 if (ret == 0) /* no errors so far */
1459 ret = pwc_mpt_set_angle(pdev, new_pan, new_tilt);
1462 pdev->pan_angle += new_pan;
1463 pdev->tilt_angle += new_tilt;
1465 if (ret == -EPIPE) /* stall -> out of range */
1476 case VIDIOCPWCMPTGANGLE:
1479 if (pdev->features & FEATURE_MOTOR_PANTILT)
1481 struct pwc_mpt_angles *angles = arg;
1483 angles->absolute = 1;
1484 angles->pan = pdev->pan_angle;
1485 angles->tilt = pdev->tilt_angle;
1494 case VIDIOCPWCMPTSTATUS:
1496 if (pdev->features & FEATURE_MOTOR_PANTILT)
1498 struct pwc_mpt_status *status = arg;
1499 ret = pwc_mpt_get_status(pdev, status);
1508 case VIDIOCPWCGVIDCMD:
1510 struct pwc_video_command *cmd = arg;
1512 cmd->type = pdev->type;
1513 cmd->release = pdev->release;
1514 cmd->command_len = pdev->cmd_len;
1515 memcpy(&cmd->command_buf, pdev->cmd_buf, pdev->cmd_len);
1516 cmd->bandlength = pdev->vbandlength;
1517 cmd->frame_size = pdev->frame_size;
1521 case VIDIOCPWCGVIDTABLE:
1523 struct pwc_table_init_buffer *table = arg;
1524 table->len = pdev->cmd_len;
1525 memcpy(&table->buffer, pdev->decompress_data, pdev->decompressor->table_size);