1 /* Driver for Philips webcam
2 Functions that send various control messages to the webcam, including
4 (C) 1999-2003 Nemosoft Unv. (webcam@smcc.demon.nl)
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 2001/08/03 Alvarado Added methods for changing white balance and
27 /* Control functions for the cam; brightness, contrast, video mode, etc. */
30 #include <asm/uaccess.h>
32 #include <asm/errno.h>
33 #include <linux/version.h>
36 #include "pwc-ioctl.h"
37 #include "pwc-uncompress.h"
39 /* Request types: video */
40 #define SET_LUM_CTL 0x01
41 #define GET_LUM_CTL 0x02
42 #define SET_CHROM_CTL 0x03
43 #define GET_CHROM_CTL 0x04
44 #define SET_STATUS_CTL 0x05
45 #define GET_STATUS_CTL 0x06
46 #define SET_EP_STREAM_CTL 0x07
47 #define GET_EP_STREAM_CTL 0x08
48 #define SET_MPT_CTL 0x0D
49 #define GET_MPT_CTL 0x0E
51 /* Selectors for the Luminance controls [GS]ET_LUM_CTL */
52 #define AGC_MODE_FORMATTER 0x2000
53 #define PRESET_AGC_FORMATTER 0x2100
54 #define SHUTTER_MODE_FORMATTER 0x2200
55 #define PRESET_SHUTTER_FORMATTER 0x2300
56 #define PRESET_CONTOUR_FORMATTER 0x2400
57 #define AUTO_CONTOUR_FORMATTER 0x2500
58 #define BACK_LIGHT_COMPENSATION_FORMATTER 0x2600
59 #define CONTRAST_FORMATTER 0x2700
60 #define DYNAMIC_NOISE_CONTROL_FORMATTER 0x2800
61 #define FLICKERLESS_MODE_FORMATTER 0x2900
62 #define AE_CONTROL_SPEED 0x2A00
63 #define BRIGHTNESS_FORMATTER 0x2B00
64 #define GAMMA_FORMATTER 0x2C00
66 /* Selectors for the Chrominance controls [GS]ET_CHROM_CTL */
67 #define WB_MODE_FORMATTER 0x1000
68 #define AWB_CONTROL_SPEED_FORMATTER 0x1100
69 #define AWB_CONTROL_DELAY_FORMATTER 0x1200
70 #define PRESET_MANUAL_RED_GAIN_FORMATTER 0x1300
71 #define PRESET_MANUAL_BLUE_GAIN_FORMATTER 0x1400
72 #define COLOUR_MODE_FORMATTER 0x1500
73 #define SATURATION_MODE_FORMATTER1 0x1600
74 #define SATURATION_MODE_FORMATTER2 0x1700
76 /* Selectors for the Status controls [GS]ET_STATUS_CTL */
77 #define SAVE_USER_DEFAULTS_FORMATTER 0x0200
78 #define RESTORE_USER_DEFAULTS_FORMATTER 0x0300
79 #define RESTORE_FACTORY_DEFAULTS_FORMATTER 0x0400
80 #define READ_AGC_FORMATTER 0x0500
81 #define READ_SHUTTER_FORMATTER 0x0600
82 #define READ_RED_GAIN_FORMATTER 0x0700
83 #define READ_BLUE_GAIN_FORMATTER 0x0800
84 #define SENSOR_TYPE_FORMATTER1 0x0C00
85 #define READ_RAW_Y_MEAN_FORMATTER 0x3100
86 #define SET_POWER_SAVE_MODE_FORMATTER 0x3200
87 #define MIRROR_IMAGE_FORMATTER 0x3300
88 #define LED_FORMATTER 0x3400
89 #define SENSOR_TYPE_FORMATTER2 0x3700
91 /* Formatters for the Video Endpoint controls [GS]ET_EP_STREAM_CTL */
92 #define VIDEO_OUTPUT_CONTROL_FORMATTER 0x0100
94 /* Formatters for the motorized pan & tilt [GS]ET_MPT_CTL */
95 #define PT_RELATIVE_CONTROL_FORMATTER 0x01
96 #define PT_RESET_CONTROL_FORMATTER 0x02
97 #define PT_STATUS_FORMATTER 0x03
99 static char *size2name[PSZ_MAX] =
111 /* Entries for the Nala (645/646) camera; the Nala doesn't have compression
112 preferences, so you either get compressed or non-compressed streams.
114 An alternate value of 0 means this mode is not available at all.
117 struct Nala_table_entry {
118 char alternate; /* USB alternate setting */
119 int compressed; /* Compressed yes/no */
121 unsigned char mode[3]; /* precomputed mode table */
124 static struct Nala_table_entry Nala_table[PSZ_MAX][8] =
126 #include "pwc_nala.h"
129 /* This tables contains entries for the 675/680/690 (Timon) camera, with
130 4 different qualities (no compression, low, medium, high).
131 It lists the bandwidth requirements for said mode by its alternate interface
132 number. An alternate of 0 means that the mode is unavailable.
134 There are 6 * 4 * 4 entries:
135 6 different resolutions subqcif, qsif, qcif, sif, cif, vga
136 6 framerates: 5, 10, 15, 20, 25, 30
137 4 compression modi: none, low, medium, high
139 When an uncompressed mode is not available, the next available compressed mode
140 will be chosen (unless the decompressor is absent). Sometimes there are only
141 1 or 2 compressed modes available; in that case entries are duplicated.
143 struct Timon_table_entry
145 char alternate; /* USB alternate interface */
146 unsigned short packetsize; /* Normal packet size */
147 unsigned short bandlength; /* Bandlength when decompressing */
148 unsigned char mode[13]; /* precomputed mode settings for cam */
151 static struct Timon_table_entry Timon_table[PSZ_MAX][6][4] =
153 #include "pwc_timon.h"
156 /* Entries for the Kiara (730/740/750) camera */
158 struct Kiara_table_entry
160 char alternate; /* USB alternate interface */
161 unsigned short packetsize; /* Normal packet size */
162 unsigned short bandlength; /* Bandlength when decompressing */
163 unsigned char mode[12]; /* precomputed mode settings for cam */
166 static struct Kiara_table_entry Kiara_table[PSZ_MAX][6][4] =
168 #include "pwc_kiara.h"
172 /****************************************************************************/
175 #define SendControlMsg(request, value, buflen) \
176 usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0), \
178 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, \
181 &buf, buflen, HZ / 2)
183 #define RecvControlMsg(request, value, buflen) \
184 usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0), \
186 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, \
189 &buf, buflen, HZ / 2)
193 void pwc_hexdump(void *p, int len)
199 s = (unsigned char *)p;
202 Debug("Doing hexdump @ %p, %d bytes.\n", p, len);
203 for (i = 0; i < len; i++) {
204 d += sprintf(d, "%02X ", *s++);
205 if ((i & 0xF) == 0xF) {
216 static inline int send_video_command(struct usb_device *udev, int index, void *buf, int buflen)
218 return usb_control_msg(udev,
219 usb_sndctrlpipe(udev, 0),
221 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
222 VIDEO_OUTPUT_CONTROL_FORMATTER,
229 static inline int set_video_mode_Nala(struct pwc_device *pdev, int size, int frames)
231 unsigned char buf[3];
233 struct Nala_table_entry *pEntry;
234 int frames2frames[31] =
235 { /* closest match of framerate */
236 0, 0, 0, 0, 4, /* 0-4 */
237 5, 5, 7, 7, 10, /* 5-9 */
238 10, 10, 12, 12, 15, /* 10-14 */
239 15, 15, 15, 20, 20, /* 15-19 */
240 20, 20, 20, 24, 24, /* 20-24 */
241 24, 24, 24, 24, 24, /* 25-29 */
244 int frames2table[31] =
245 { 0, 0, 0, 0, 0, /* 0-4 */
246 1, 1, 1, 2, 2, /* 5-9 */
247 3, 3, 4, 4, 4, /* 10-14 */
248 5, 5, 5, 5, 5, /* 15-19 */
249 6, 6, 6, 6, 7, /* 20-24 */
250 7, 7, 7, 7, 7, /* 25-29 */
254 if (size < 0 || size > PSZ_CIF || frames < 4 || frames > 25)
256 frames = frames2frames[frames];
257 fps = frames2table[frames];
258 pEntry = &Nala_table[size][fps];
259 if (pEntry->alternate == 0)
262 if (pEntry->compressed && pdev->decompressor == NULL)
263 return -ENOENT; /* Not supported. */
265 memcpy(buf, pEntry->mode, 3);
266 ret = send_video_command(pdev->udev, pdev->vendpoint, buf, 3);
268 Debug("Failed to send video command... %d\n", ret);
271 if (pEntry->compressed && pdev->decompressor != 0 && pdev->vpalette != VIDEO_PALETTE_RAW)
272 pdev->decompressor->init(pdev->type, pdev->release, buf, pdev->decompress_data);
275 memcpy(pdev->cmd_buf, buf, 3);
277 /* Set various parameters */
278 pdev->vframes = frames;
280 pdev->valternate = pEntry->alternate;
281 pdev->image = pwc_image_sizes[size];
282 pdev->frame_size = (pdev->image.x * pdev->image.y * 3) / 2;
283 if (pEntry->compressed) {
284 if (pdev->release < 5) { /* 4 fold compression */
285 pdev->vbandlength = 528;
286 pdev->frame_size /= 4;
289 pdev->vbandlength = 704;
290 pdev->frame_size /= 3;
294 pdev->vbandlength = 0;
299 static inline int set_video_mode_Timon(struct pwc_device *pdev, int size, int frames, int compression, int snapshot)
301 unsigned char buf[13];
302 struct Timon_table_entry *pChoose;
305 if (size >= PSZ_MAX || frames < 5 || frames > 30 || compression < 0 || compression > 3)
307 if (size == PSZ_VGA && frames > 15)
309 fps = (frames / 5) - 1;
311 /* Find a supported framerate with progressively higher compression ratios
312 if the preferred ratio is not available.
315 if (pdev->decompressor == NULL) {
317 Debug("Trying to find uncompressed mode.\n");
319 pChoose = &Timon_table[size][fps][0];
322 while (compression <= 3) {
323 pChoose = &Timon_table[size][fps][compression];
324 if (pChoose->alternate != 0)
329 if (pChoose == NULL || pChoose->alternate == 0)
330 return -ENOENT; /* Not supported. */
332 memcpy(buf, pChoose->mode, 13);
335 ret = send_video_command(pdev->udev, pdev->vendpoint, buf, 13);
339 if (pChoose->bandlength > 0 && pdev->decompressor != 0 && pdev->vpalette != VIDEO_PALETTE_RAW)
340 pdev->decompressor->init(pdev->type, pdev->release, buf, pdev->decompress_data);
343 memcpy(pdev->cmd_buf, buf, 13);
345 /* Set various parameters */
346 pdev->vframes = frames;
348 pdev->vsnapshot = snapshot;
349 pdev->valternate = pChoose->alternate;
350 pdev->image = pwc_image_sizes[size];
351 pdev->vbandlength = pChoose->bandlength;
352 if (pChoose->bandlength > 0)
353 pdev->frame_size = (pChoose->bandlength * pdev->image.y) / 4;
355 pdev->frame_size = (pdev->image.x * pdev->image.y * 12) / 8;
360 static inline int set_video_mode_Kiara(struct pwc_device *pdev, int size, int frames, int compression, int snapshot)
362 struct Kiara_table_entry *pChoose = NULL;
364 unsigned char buf[12];
365 struct Kiara_table_entry RawEntry = {6, 773, 1272, {0xAD, 0xF4, 0x10, 0x27, 0xB6, 0x24, 0x96, 0x02, 0x30, 0x05, 0x03, 0x80}};
367 if (size >= PSZ_MAX || frames < 5 || frames > 30 || compression < 0 || compression > 3)
369 if (size == PSZ_VGA && frames > 15)
371 fps = (frames / 5) - 1;
373 /* special case: VGA @ 5 fps and snapshot is raw bayer mode */
374 if (size == PSZ_VGA && frames == 5 && snapshot)
376 /* Only available in case the raw palette is selected or
377 we have the decompressor available. This mode is
378 only available in compressed form
380 if (pdev->vpalette == VIDEO_PALETTE_RAW || pdev->decompressor != NULL)
382 Info("Choosing VGA/5 BAYER mode (%d).\n", pdev->vpalette);
387 Info("VGA/5 BAYER mode _must_ have a decompressor available, or use RAW palette.\n");
392 /* Find a supported framerate with progressively higher compression ratios
393 if the preferred ratio is not available.
394 Skip this step when using RAW modes.
396 if (pdev->decompressor == NULL && pdev->vpalette != VIDEO_PALETTE_RAW) {
398 Debug("Trying to find uncompressed mode.\n");
400 pChoose = &Kiara_table[size][fps][0];
403 while (compression <= 3) {
404 pChoose = &Kiara_table[size][fps][compression];
405 if (pChoose->alternate != 0)
411 if (pChoose == NULL || pChoose->alternate == 0)
412 return -ENOENT; /* Not supported. */
414 /* usb_control_msg won't take staticly allocated arrays as argument?? */
415 memcpy(buf, pChoose->mode, 12);
419 /* Firmware bug: video endpoint is 5, but commands are sent to endpoint 4 */
420 ret = send_video_command(pdev->udev, 4 /* pdev->vendpoint */, buf, 12);
424 if (pChoose->bandlength > 0 && pdev->decompressor != 0 && pdev->vpalette != VIDEO_PALETTE_RAW)
425 pdev->decompressor->init(pdev->type, pdev->release, buf, pdev->decompress_data);
428 memcpy(pdev->cmd_buf, buf, 12);
430 pdev->vframes = frames;
432 pdev->vsnapshot = snapshot;
433 pdev->valternate = pChoose->alternate;
434 pdev->image = pwc_image_sizes[size];
435 pdev->vbandlength = pChoose->bandlength;
436 if (pdev->vbandlength > 0)
437 pdev->frame_size = (pdev->vbandlength * pdev->image.y) / 4;
439 pdev->frame_size = (pdev->image.x * pdev->image.y * 12) / 8;
446 @pdev: device structure
447 @width: viewport width
448 @height: viewport height
449 @frame: framerate, in fps
450 @compression: preferred compression ratio
451 @snapshot: snapshot mode or streaming
453 int pwc_set_video_mode(struct pwc_device *pdev, int width, int height, int frames, int compression, int snapshot)
457 Trace(TRACE_FLOW, "set_video_mode(%dx%d @ %d, palette %d).\n", width, height, frames, pdev->vpalette);
458 size = pwc_decode_size(pdev, width, height);
460 Debug("Could not find suitable size.\n");
463 Debug("decode_size = %d.\n", size);
469 ret = set_video_mode_Nala(pdev, size, frames);
475 ret = set_video_mode_Timon(pdev, size, frames, compression, snapshot);
482 ret = set_video_mode_Kiara(pdev, size, frames, compression, snapshot);
487 Info("Video mode %s@%d fps is only supported with the decompressor module (pwcx).\n", size2name[size], frames);
489 Err("Failed to set video mode %s@%d fps; return code = %d\n", size2name[size], frames, ret);
493 pdev->view.x = width;
494 pdev->view.y = height;
495 pdev->frame_total_size = pdev->frame_size + pdev->frame_header_size + pdev->frame_trailer_size;
496 pwc_set_image_buffer_size(pdev);
497 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);
502 void pwc_set_image_buffer_size(struct pwc_device *pdev)
504 int i, factor = 0, filler = 0;
506 /* for PALETTE_YUV420P */
507 switch(pdev->vpalette)
509 case VIDEO_PALETTE_YUV420P:
513 case VIDEO_PALETTE_RAW:
514 factor = 6; /* can be uncompressed YUV420P */
519 /* Set sizes in bytes */
520 pdev->image.size = pdev->image.x * pdev->image.y * factor / 4;
521 pdev->view.size = pdev->view.x * pdev->view.y * factor / 4;
523 /* Align offset, or you'll get some very weird results in
524 YUV420 mode... x must be multiple of 4 (to get the Y's in
525 place), and y even (or you'll mixup U & V). This is less of a
528 pdev->offset.x = ((pdev->view.x - pdev->image.x) / 2) & 0xFFFC;
529 pdev->offset.y = ((pdev->view.y - pdev->image.y) / 2) & 0xFFFE;
531 /* Fill buffers with gray or black */
532 for (i = 0; i < MAX_IMAGES; i++) {
533 if (pdev->image_ptr[i] != NULL)
534 memset(pdev->image_ptr[i], filler, pdev->view.size);
542 int pwc_get_brightness(struct pwc_device *pdev)
547 ret = RecvControlMsg(GET_LUM_CTL, BRIGHTNESS_FORMATTER, 1);
553 int pwc_set_brightness(struct pwc_device *pdev, int value)
561 buf = (value >> 9) & 0x7f;
562 return SendControlMsg(SET_LUM_CTL, BRIGHTNESS_FORMATTER, 1);
567 int pwc_get_contrast(struct pwc_device *pdev)
572 ret = RecvControlMsg(GET_LUM_CTL, CONTRAST_FORMATTER, 1);
578 int pwc_set_contrast(struct pwc_device *pdev, int value)
586 buf = (value >> 10) & 0x3f;
587 return SendControlMsg(SET_LUM_CTL, CONTRAST_FORMATTER, 1);
592 int pwc_get_gamma(struct pwc_device *pdev)
597 ret = RecvControlMsg(GET_LUM_CTL, GAMMA_FORMATTER, 1);
603 int pwc_set_gamma(struct pwc_device *pdev, int value)
611 buf = (value >> 11) & 0x1f;
612 return SendControlMsg(SET_LUM_CTL, GAMMA_FORMATTER, 1);
618 int pwc_get_saturation(struct pwc_device *pdev)
623 if (pdev->type < 675)
625 ret = RecvControlMsg(GET_CHROM_CTL, pdev->type < 730 ? SATURATION_MODE_FORMATTER2 : SATURATION_MODE_FORMATTER1, 1);
628 return 32768 + buf * 327;
631 int pwc_set_saturation(struct pwc_device *pdev, int value)
635 if (pdev->type < 675)
641 /* saturation ranges from -100 to +100 */
642 buf = (value - 32768) / 327;
643 return SendControlMsg(SET_CHROM_CTL, pdev->type < 730 ? SATURATION_MODE_FORMATTER2 : SATURATION_MODE_FORMATTER1, 1);
648 static inline int pwc_set_agc(struct pwc_device *pdev, int mode, int value)
654 buf = 0x0; /* auto */
656 buf = 0xff; /* fixed */
658 ret = SendControlMsg(SET_LUM_CTL, AGC_MODE_FORMATTER, 1);
660 if (!mode && ret >= 0) {
665 buf = (value >> 10) & 0x3F;
666 ret = SendControlMsg(SET_LUM_CTL, PRESET_AGC_FORMATTER, 1);
673 static inline int pwc_get_agc(struct pwc_device *pdev, int *value)
678 ret = RecvControlMsg(GET_LUM_CTL, AGC_MODE_FORMATTER, 1);
682 if (buf != 0) { /* fixed */
683 ret = RecvControlMsg(GET_LUM_CTL, PRESET_AGC_FORMATTER, 1);
688 *value = (buf << 10);
691 ret = RecvControlMsg(GET_STATUS_CTL, READ_AGC_FORMATTER, 1);
694 /* Gah... this value ranges from 0x00 ... 0x9F */
697 *value = -(48 + buf * 409);
703 static inline int pwc_set_shutter_speed(struct pwc_device *pdev, int mode, int value)
710 buf[0] = 0x0; /* auto */
712 buf[0] = 0xff; /* fixed */
714 ret = SendControlMsg(SET_LUM_CTL, SHUTTER_MODE_FORMATTER, 1);
716 if (!mode && ret >= 0) {
725 /* speed ranges from 0x0 to 0x290 (656) */
726 speed = (value / 100);
728 buf[0] = speed & 0xff;
734 /* speed seems to range from 0x0 to 0xff */
740 ret = SendControlMsg(SET_LUM_CTL, PRESET_SHUTTER_FORMATTER, 2);
748 int pwc_camera_power(struct pwc_device *pdev, int power)
752 if (pdev->type < 675 || (pdev->type < 730 && pdev->release < 6))
753 return 0; /* Not supported by Nala or Timon < release 6 */
756 buf = 0x00; /* active */
758 buf = 0xFF; /* power save */
759 return SendControlMsg(SET_STATUS_CTL, SET_POWER_SAVE_MODE_FORMATTER, 1);
766 static inline int pwc_restore_user(struct pwc_device *pdev)
768 char buf; /* dummy */
769 return SendControlMsg(SET_STATUS_CTL, RESTORE_USER_DEFAULTS_FORMATTER, 0);
772 static inline int pwc_save_user(struct pwc_device *pdev)
774 char buf; /* dummy */
775 return SendControlMsg(SET_STATUS_CTL, SAVE_USER_DEFAULTS_FORMATTER, 0);
778 static inline int pwc_restore_factory(struct pwc_device *pdev)
780 char buf; /* dummy */
781 return SendControlMsg(SET_STATUS_CTL, RESTORE_FACTORY_DEFAULTS_FORMATTER, 0);
784 /* ************************************************* */
785 /* Patch by Alvarado: (not in the original version */
788 * the camera recognizes modes from 0 to 4:
790 * 00: indoor (incandescant lighting)
791 * 01: outdoor (sunlight)
792 * 02: fluorescent lighting
796 static inline int pwc_set_awb(struct pwc_device *pdev, int mode)
807 buf = mode & 0x07; /* just the lowest three bits */
809 ret = SendControlMsg(SET_CHROM_CTL, WB_MODE_FORMATTER, 1);
816 static inline int pwc_get_awb(struct pwc_device *pdev)
821 ret = RecvControlMsg(GET_CHROM_CTL, WB_MODE_FORMATTER, 1);
828 static inline int pwc_set_red_gain(struct pwc_device *pdev, int value)
836 /* only the msb is considered */
838 return SendControlMsg(SET_CHROM_CTL, PRESET_MANUAL_RED_GAIN_FORMATTER, 1);
841 static inline int pwc_get_red_gain(struct pwc_device *pdev, int *value)
846 ret = RecvControlMsg(GET_CHROM_CTL, PRESET_MANUAL_RED_GAIN_FORMATTER, 1);
854 static inline int pwc_set_blue_gain(struct pwc_device *pdev, int value)
862 /* only the msb is considered */
864 return SendControlMsg(SET_CHROM_CTL, PRESET_MANUAL_BLUE_GAIN_FORMATTER, 1);
867 static inline int pwc_get_blue_gain(struct pwc_device *pdev, int *value)
872 ret = RecvControlMsg(GET_CHROM_CTL, PRESET_MANUAL_BLUE_GAIN_FORMATTER, 1);
880 /* The following two functions are different, since they only read the
881 internal red/blue gains, which may be different from the manual
882 gains set or read above.
884 static inline int pwc_read_red_gain(struct pwc_device *pdev, int *value)
889 ret = RecvControlMsg(GET_STATUS_CTL, READ_RED_GAIN_FORMATTER, 1);
896 static inline int pwc_read_blue_gain(struct pwc_device *pdev, int *value)
901 ret = RecvControlMsg(GET_STATUS_CTL, READ_BLUE_GAIN_FORMATTER, 1);
909 static inline int pwc_set_wb_speed(struct pwc_device *pdev, int speed)
913 /* useful range is 0x01..0x20 */
915 return SendControlMsg(SET_CHROM_CTL, AWB_CONTROL_SPEED_FORMATTER, 1);
918 static inline int pwc_get_wb_speed(struct pwc_device *pdev, int *value)
923 ret = RecvControlMsg(GET_CHROM_CTL, AWB_CONTROL_SPEED_FORMATTER, 1);
926 *value = buf * 0x7f0;
931 static inline int pwc_set_wb_delay(struct pwc_device *pdev, int delay)
935 /* useful range is 0x01..0x3F */
937 return SendControlMsg(SET_CHROM_CTL, AWB_CONTROL_DELAY_FORMATTER, 1);
940 static inline int pwc_get_wb_delay(struct pwc_device *pdev, int *value)
945 ret = RecvControlMsg(GET_CHROM_CTL, AWB_CONTROL_DELAY_FORMATTER, 1);
953 int pwc_set_leds(struct pwc_device *pdev, int on_value, int off_value)
955 unsigned char buf[2];
957 if (pdev->type < 730)
967 if (off_value > 0xff)
973 return SendControlMsg(SET_STATUS_CTL, LED_FORMATTER, 2);
976 int pwc_get_leds(struct pwc_device *pdev, int *on_value, int *off_value)
978 unsigned char buf[2];
981 if (pdev->type < 730) {
987 ret = RecvControlMsg(GET_STATUS_CTL, LED_FORMATTER, 2);
990 *on_value = buf[0] * 100;
991 *off_value = buf[1] * 100;
995 static inline int pwc_set_contour(struct pwc_device *pdev, int contour)
1001 buf = 0xff; /* auto contour on */
1003 buf = 0x0; /* auto contour off */
1004 ret = SendControlMsg(SET_LUM_CTL, AUTO_CONTOUR_FORMATTER, 1);
1010 if (contour > 0xffff)
1013 buf = (contour >> 10); /* contour preset is [0..3f] */
1014 ret = SendControlMsg(SET_LUM_CTL, PRESET_CONTOUR_FORMATTER, 1);
1020 static inline int pwc_get_contour(struct pwc_device *pdev, int *contour)
1025 ret = RecvControlMsg(GET_LUM_CTL, AUTO_CONTOUR_FORMATTER, 1);
1030 /* auto mode off, query current preset value */
1031 ret = RecvControlMsg(GET_LUM_CTL, PRESET_CONTOUR_FORMATTER, 1);
1034 *contour = buf << 10;
1042 static inline int pwc_set_backlight(struct pwc_device *pdev, int backlight)
1050 return SendControlMsg(SET_LUM_CTL, BACK_LIGHT_COMPENSATION_FORMATTER, 1);
1053 static inline int pwc_get_backlight(struct pwc_device *pdev, int *backlight)
1058 ret = RecvControlMsg(GET_LUM_CTL, BACK_LIGHT_COMPENSATION_FORMATTER, 1);
1066 static inline int pwc_set_flicker(struct pwc_device *pdev, int flicker)
1074 return SendControlMsg(SET_LUM_CTL, FLICKERLESS_MODE_FORMATTER, 1);
1077 static inline int pwc_get_flicker(struct pwc_device *pdev, int *flicker)
1082 ret = RecvControlMsg(GET_LUM_CTL, FLICKERLESS_MODE_FORMATTER, 1);
1090 static inline int pwc_set_dynamic_noise(struct pwc_device *pdev, int noise)
1099 return SendControlMsg(SET_LUM_CTL, DYNAMIC_NOISE_CONTROL_FORMATTER, 1);
1102 static inline int pwc_get_dynamic_noise(struct pwc_device *pdev, int *noise)
1107 ret = RecvControlMsg(GET_LUM_CTL, DYNAMIC_NOISE_CONTROL_FORMATTER, 1);
1114 int pwc_mpt_reset(struct pwc_device *pdev, int flags)
1118 buf = flags & 0x03; // only lower two bits are currently used
1119 return SendControlMsg(SET_MPT_CTL, PT_RESET_CONTROL_FORMATTER, 1);
1122 static inline int pwc_mpt_set_angle(struct pwc_device *pdev, int pan, int tilt)
1124 unsigned char buf[4];
1126 /* set new relative angle; angles are expressed in degrees * 100,
1127 but cam as .5 degree resolution, hence devide by 200. Also
1128 the angle must be multiplied by 64 before it's send to
1131 pan = 64 * pan / 100;
1132 tilt = -64 * tilt / 100; /* positive tilt is down, which is not what the user would expect */
1133 buf[0] = pan & 0xFF;
1134 buf[1] = (pan >> 8) & 0xFF;
1135 buf[2] = tilt & 0xFF;
1136 buf[3] = (tilt >> 8) & 0xFF;
1137 return SendControlMsg(SET_MPT_CTL, PT_RELATIVE_CONTROL_FORMATTER, 4);
1140 static inline int pwc_mpt_get_status(struct pwc_device *pdev, struct pwc_mpt_status *status)
1143 unsigned char buf[5];
1145 ret = RecvControlMsg(GET_MPT_CTL, PT_STATUS_FORMATTER, 5);
1148 status->status = buf[0] & 0x7; // 3 bits are used for reporting
1149 status->time_pan = (buf[1] << 8) + buf[2];
1150 status->time_tilt = (buf[3] << 8) + buf[4];
1155 int pwc_get_cmos_sensor(struct pwc_device *pdev, int *sensor)
1158 int ret = -1, request;
1160 if (pdev->type < 675)
1161 request = SENSOR_TYPE_FORMATTER1;
1162 else if (pdev->type < 730)
1163 return -1; /* The Vesta series doesn't have this call */
1165 request = SENSOR_TYPE_FORMATTER2;
1167 ret = RecvControlMsg(GET_STATUS_CTL, request, 1);
1170 if (pdev->type < 675)
1171 *sensor = buf | 0x100;
1178 /* End of Add-Ons */
1179 /* ************************************************* */
1181 /* Linux 2.5.something and 2.6 pass direct pointers to arguments of
1182 ioctl() calls. With 2.4, you have to do tedious copy_from_user()
1183 and copy_to_user() calls. With these macros we circumvent this,
1184 and let me maintain only one source file. The functionality is
1185 exactly the same otherwise.
1188 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
1190 /* define local variable for arg */
1191 #define ARG_DEF(ARG_type, ARG_name)\
1192 ARG_type *ARG_name = arg;
1193 /* copy arg to local variable */
1194 #define ARG_IN(ARG_name) /* nothing */
1195 /* argument itself (referenced) */
1196 #define ARGR(ARG_name) (*ARG_name)
1197 /* argument address */
1198 #define ARGA(ARG_name) ARG_name
1199 /* copy local variable to arg */
1200 #define ARG_OUT(ARG_name) /* nothing */
1204 #define ARG_DEF(ARG_type, ARG_name)\
1206 #define ARG_IN(ARG_name)\
1207 if (copy_from_user(&ARG_name, arg, sizeof(ARG_name))) {\
1211 #define ARGR(ARG_name) ARG_name
1212 #define ARGA(ARG_name) &ARG_name
1213 #define ARG_OUT(ARG_name)\
1214 if (copy_to_user(arg, &ARG_name, sizeof(ARG_name))) {\
1221 int pwc_ioctl(struct pwc_device *pdev, unsigned int cmd, void *arg)
1226 case VIDIOCPWCRUSER:
1228 if (pwc_restore_user(pdev))
1233 case VIDIOCPWCSUSER:
1235 if (pwc_save_user(pdev))
1240 case VIDIOCPWCFACTORY:
1242 if (pwc_restore_factory(pdev))
1247 case VIDIOCPWCSCQUAL:
1252 if (ARGR(qual) < 0 || ARGR(qual) > 3)
1255 ret = pwc_try_video_mode(pdev, pdev->view.x, pdev->view.y, pdev->vframes, ARGR(qual), pdev->vsnapshot);
1257 pdev->vcompression = ARGR(qual);
1261 case VIDIOCPWCGCQUAL:
1265 ARGR(qual) = pdev->vcompression;
1270 case VIDIOCPWCPROBE:
1272 ARG_DEF(struct pwc_probe, probe)
1274 strcpy(ARGR(probe).name, pdev->vdev->name);
1275 ARGR(probe).type = pdev->type;
1280 case VIDIOCPWCGSERIAL:
1282 ARG_DEF(struct pwc_serial, serial)
1284 strcpy(ARGR(serial).serial, pdev->serial);
1294 if (pwc_set_agc(pdev, ARGR(agc) < 0 ? 1 : 0, ARGR(agc)))
1303 if (pwc_get_agc(pdev, ARGA(agc)))
1309 case VIDIOCPWCSSHUTTER:
1311 ARG_DEF(int, shutter_speed)
1313 ARG_IN(shutter_speed)
1314 ret = pwc_set_shutter_speed(pdev, ARGR(shutter_speed) < 0 ? 1 : 0, ARGR(shutter_speed));
1320 ARG_DEF(struct pwc_whitebalance, wb)
1323 ret = pwc_set_awb(pdev, ARGR(wb).mode);
1324 if (ret >= 0 && ARGR(wb).mode == PWC_WB_MANUAL) {
1325 pwc_set_red_gain(pdev, ARGR(wb).manual_red);
1326 pwc_set_blue_gain(pdev, ARGR(wb).manual_blue);
1333 ARG_DEF(struct pwc_whitebalance, wb)
1335 memset(ARGA(wb), 0, sizeof(struct pwc_whitebalance));
1336 ARGR(wb).mode = pwc_get_awb(pdev);
1337 if (ARGR(wb).mode < 0)
1340 if (ARGR(wb).mode == PWC_WB_MANUAL) {
1341 ret = pwc_get_red_gain(pdev, &ARGR(wb).manual_red);
1344 ret = pwc_get_blue_gain(pdev, &ARGR(wb).manual_blue);
1348 if (ARGR(wb).mode == PWC_WB_AUTO) {
1349 ret = pwc_read_red_gain(pdev, &ARGR(wb).read_red);
1352 ret =pwc_read_blue_gain(pdev, &ARGR(wb).read_blue);
1361 case VIDIOCPWCSAWBSPEED:
1363 ARG_DEF(struct pwc_wb_speed, wbs)
1365 if (ARGR(wbs).control_speed > 0) {
1366 ret = pwc_set_wb_speed(pdev, ARGR(wbs).control_speed);
1368 if (ARGR(wbs).control_delay > 0) {
1369 ret = pwc_set_wb_delay(pdev, ARGR(wbs).control_delay);
1374 case VIDIOCPWCGAWBSPEED:
1376 ARG_DEF(struct pwc_wb_speed, wbs)
1378 ret = pwc_get_wb_speed(pdev, &ARGR(wbs).control_speed);
1381 ret = pwc_get_wb_delay(pdev, &ARGR(wbs).control_delay);
1390 ARG_DEF(struct pwc_leds, leds)
1393 ret = pwc_set_leds(pdev, ARGR(leds).led_on, ARGR(leds).led_off);
1400 ARG_DEF(struct pwc_leds, leds)
1402 ret = pwc_get_leds(pdev, &ARGR(leds).led_on, &ARGR(leds).led_off);
1407 case VIDIOCPWCSCONTOUR:
1409 ARG_DEF(int, contour)
1412 ret = pwc_set_contour(pdev, ARGR(contour));
1416 case VIDIOCPWCGCONTOUR:
1418 ARG_DEF(int, contour)
1420 ret = pwc_get_contour(pdev, ARGA(contour));
1425 case VIDIOCPWCSBACKLIGHT:
1427 ARG_DEF(int, backlight)
1430 ret = pwc_set_backlight(pdev, ARGR(backlight));
1434 case VIDIOCPWCGBACKLIGHT:
1436 ARG_DEF(int, backlight)
1438 ret = pwc_get_backlight(pdev, ARGA(backlight));
1443 case VIDIOCPWCSFLICKER:
1445 ARG_DEF(int, flicker)
1448 ret = pwc_set_flicker(pdev, ARGR(flicker));
1452 case VIDIOCPWCGFLICKER:
1454 ARG_DEF(int, flicker)
1456 ret = pwc_get_flicker(pdev, ARGA(flicker));
1461 case VIDIOCPWCSDYNNOISE:
1463 ARG_DEF(int, dynnoise)
1466 ret = pwc_set_dynamic_noise(pdev, ARGR(dynnoise));
1470 case VIDIOCPWCGDYNNOISE:
1472 ARG_DEF(int, dynnoise)
1474 ret = pwc_get_dynamic_noise(pdev, ARGA(dynnoise));
1479 case VIDIOCPWCGREALSIZE:
1481 ARG_DEF(struct pwc_imagesize, size)
1483 ARGR(size).width = pdev->image.x;
1484 ARGR(size).height = pdev->image.y;
1489 case VIDIOCPWCMPTRESET:
1491 if (pdev->features & FEATURE_MOTOR_PANTILT)
1496 ret = pwc_mpt_reset(pdev, ARGR(flags));
1499 pdev->pan_angle = 0;
1500 pdev->tilt_angle = 0;
1510 case VIDIOCPWCMPTGRANGE:
1512 if (pdev->features & FEATURE_MOTOR_PANTILT)
1514 ARG_DEF(struct pwc_mpt_range, range)
1516 ARGR(range) = pdev->angle_range;
1526 case VIDIOCPWCMPTSANGLE:
1528 int new_pan, new_tilt;
1530 if (pdev->features & FEATURE_MOTOR_PANTILT)
1532 ARG_DEF(struct pwc_mpt_angles, angles)
1535 /* The camera can only set relative angles, so
1536 do some calculations when getting an absolute angle .
1538 if (ARGR(angles).absolute)
1540 new_pan = ARGR(angles).pan;
1541 new_tilt = ARGR(angles).tilt;
1545 new_pan = pdev->pan_angle + ARGR(angles).pan;
1546 new_tilt = pdev->tilt_angle + ARGR(angles).tilt;
1548 /* check absolute ranges */
1549 if (new_pan < pdev->angle_range.pan_min ||
1550 new_pan > pdev->angle_range.pan_max ||
1551 new_tilt < pdev->angle_range.tilt_min ||
1552 new_tilt > pdev->angle_range.tilt_max)
1558 /* go to relative range, check again */
1559 new_pan -= pdev->pan_angle;
1560 new_tilt -= pdev->tilt_angle;
1561 /* angles are specified in degrees * 100, thus the limit = 36000 */
1562 if (new_pan < -36000 || new_pan > 36000 || new_tilt < -36000 || new_tilt > 36000)
1565 if (ret == 0) /* no errors so far */
1567 ret = pwc_mpt_set_angle(pdev, new_pan, new_tilt);
1570 pdev->pan_angle += new_pan;
1571 pdev->tilt_angle += new_tilt;
1573 if (ret == -EPIPE) /* stall -> out of range */
1584 case VIDIOCPWCMPTGANGLE:
1587 if (pdev->features & FEATURE_MOTOR_PANTILT)
1589 ARG_DEF(struct pwc_mpt_angles, angles)
1591 ARGR(angles).absolute = 1;
1592 ARGR(angles).pan = pdev->pan_angle;
1593 ARGR(angles).tilt = pdev->tilt_angle;
1603 case VIDIOCPWCMPTSTATUS:
1605 if (pdev->features & FEATURE_MOTOR_PANTILT)
1607 ARG_DEF(struct pwc_mpt_status, status)
1609 ret = pwc_mpt_get_status(pdev, ARGA(status));
1619 case VIDIOCPWCGVIDCMD:
1621 ARG_DEF(struct pwc_video_command, cmd);
1623 ARGR(cmd).type = pdev->type;
1624 ARGR(cmd).release = pdev->release;
1625 ARGR(cmd).command_len = pdev->cmd_len;
1626 memcpy(&ARGR(cmd).command_buf, pdev->cmd_buf, pdev->cmd_len);
1627 ARGR(cmd).bandlength = pdev->vbandlength;
1628 ARGR(cmd).frame_size = pdev->frame_size;