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>
35 #include "pwc-ioctl.h"
36 #include "pwc-uncompress.h"
38 /* Request types: video */
39 #define SET_LUM_CTL 0x01
40 #define GET_LUM_CTL 0x02
41 #define SET_CHROM_CTL 0x03
42 #define GET_CHROM_CTL 0x04
43 #define SET_STATUS_CTL 0x05
44 #define GET_STATUS_CTL 0x06
45 #define SET_EP_STREAM_CTL 0x07
46 #define GET_EP_STREAM_CTL 0x08
48 /* Selectors for the Luminance controls [GS]ET_LUM_CTL */
49 #define AGC_MODE_FORMATTER 0x2000
50 #define PRESET_AGC_FORMATTER 0x2100
51 #define SHUTTER_MODE_FORMATTER 0x2200
52 #define PRESET_SHUTTER_FORMATTER 0x2300
53 #define PRESET_CONTOUR_FORMATTER 0x2400
54 #define AUTO_CONTOUR_FORMATTER 0x2500
55 #define BACK_LIGHT_COMPENSATION_FORMATTER 0x2600
56 #define CONTRAST_FORMATTER 0x2700
57 #define DYNAMIC_NOISE_CONTROL_FORMATTER 0x2800
58 #define FLICKERLESS_MODE_FORMATTER 0x2900
59 #define AE_CONTROL_SPEED 0x2A00
60 #define BRIGHTNESS_FORMATTER 0x2B00
61 #define GAMMA_FORMATTER 0x2C00
63 /* Selectors for the Chrominance controls [GS]ET_CHROM_CTL */
64 #define WB_MODE_FORMATTER 0x1000
65 #define AWB_CONTROL_SPEED_FORMATTER 0x1100
66 #define AWB_CONTROL_DELAY_FORMATTER 0x1200
67 #define PRESET_MANUAL_RED_GAIN_FORMATTER 0x1300
68 #define PRESET_MANUAL_BLUE_GAIN_FORMATTER 0x1400
69 #define COLOUR_MODE_FORMATTER 0x1500
70 #define SATURATION_MODE_FORMATTER1 0x1600
71 #define SATURATION_MODE_FORMATTER2 0x1700
73 /* Selectors for the Status controls [GS]ET_STATUS_CTL */
74 #define SAVE_USER_DEFAULTS_FORMATTER 0x0200
75 #define RESTORE_USER_DEFAULTS_FORMATTER 0x0300
76 #define RESTORE_FACTORY_DEFAULTS_FORMATTER 0x0400
77 #define READ_AGC_FORMATTER 0x0500
78 #define READ_SHUTTER_FORMATTER 0x0600
79 #define READ_RED_GAIN_FORMATTER 0x0700
80 #define READ_BLUE_GAIN_FORMATTER 0x0800
81 #define SENSOR_TYPE_FORMATTER1 0x0C00
82 #define READ_RAW_Y_MEAN_FORMATTER 0x3100
83 #define SET_POWER_SAVE_MODE_FORMATTER 0x3200
84 #define MIRROR_IMAGE_FORMATTER 0x3300
85 #define LED_FORMATTER 0x3400
86 #define SENSOR_TYPE_FORMATTER2 0x3700
88 /* Formatters for the Video Endpoint controls [GS]ET_EP_STREAM_CTL */
89 #define VIDEO_OUTPUT_CONTROL_FORMATTER 0x0100
91 static char *size2name[PSZ_MAX] =
103 /* Entries for the Nala (645/646) camera; the Nala doesn't have compression
104 preferences, so you either get compressed or non-compressed streams.
106 An alternate value of 0 means this mode is not available at all.
109 struct Nala_table_entry {
110 char alternate; /* USB alternate setting */
111 int compressed; /* Compressed yes/no */
113 unsigned char mode[3]; /* precomputed mode table */
116 static struct Nala_table_entry Nala_table[PSZ_MAX][8] =
118 #include "pwc_nala.h"
121 /* This tables contains entries for the 675/680/690 (Timon) camera, with
122 4 different qualities (no compression, low, medium, high).
123 It lists the bandwidth requirements for said mode by its alternate interface
124 number. An alternate of 0 means that the mode is unavailable.
126 There are 6 * 4 * 4 entries:
127 6 different resolutions subqcif, qsif, qcif, sif, cif, vga
128 6 framerates: 5, 10, 15, 20, 25, 30
129 4 compression modi: none, low, medium, high
131 When an uncompressed mode is not available, the next available compressed mode
132 will be choosen (unless the decompressor is absent). Sometimes there are only
133 1 or 2 compressed modes available; in that case entries are duplicated.
135 struct Timon_table_entry
137 char alternate; /* USB alternate interface */
138 unsigned short packetsize; /* Normal packet size */
139 unsigned short bandlength; /* Bandlength when decompressing */
140 unsigned char mode[13]; /* precomputed mode settings for cam */
143 static struct Timon_table_entry Timon_table[PSZ_MAX][6][4] =
145 #include "pwc_timon.h"
148 /* Entries for the Kiara (730/740/750) camera */
150 struct Kiara_table_entry
152 char alternate; /* USB alternate interface */
153 unsigned short packetsize; /* Normal packet size */
154 unsigned short bandlength; /* Bandlength when decompressing */
155 unsigned char mode[12]; /* precomputed mode settings for cam */
158 static struct Kiara_table_entry Kiara_table[PSZ_MAX][6][4] =
160 #include "pwc_kiara.h"
164 /****************************************************************************/
167 #define SendControlMsg(request, value, buflen) \
168 usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0), \
170 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, \
173 &buf, buflen, HZ / 2)
175 #define RecvControlMsg(request, value, buflen) \
176 usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0), \
178 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, \
181 &buf, buflen, HZ / 2)
185 void pwc_hexdump(void *p, int len)
191 s = (unsigned char *)p;
194 Debug("Doing hexdump @ %p, %d bytes.\n", p, len);
195 for (i = 0; i < len; i++) {
196 d += sprintf(d, "%02X ", *s++);
197 if ((i & 0xF) == 0xF) {
208 static inline int send_video_command(struct usb_device *udev, int index, void *buf, int buflen)
210 return usb_control_msg(udev,
211 usb_sndctrlpipe(udev, 0),
213 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
214 VIDEO_OUTPUT_CONTROL_FORMATTER,
221 static inline int set_video_mode_Nala(struct pwc_device *pdev, int size, int frames)
223 unsigned char buf[3];
225 struct Nala_table_entry *pEntry;
226 int frames2frames[31] =
227 { /* closest match of framerate */
228 0, 0, 0, 0, 4, /* 0-4 */
229 5, 5, 7, 7, 10, /* 5-9 */
230 10, 10, 12, 12, 15, /* 10-14 */
231 15, 15, 15, 20, 20, /* 15-19 */
232 20, 20, 20, 24, 24, /* 20-24 */
233 24, 24, 24, 24, 24, /* 25-29 */
236 int frames2table[31] =
237 { 0, 0, 0, 0, 0, /* 0-4 */
238 1, 1, 1, 2, 2, /* 5-9 */
239 3, 3, 4, 4, 4, /* 10-14 */
240 5, 5, 5, 5, 5, /* 15-19 */
241 6, 6, 6, 6, 7, /* 20-24 */
242 7, 7, 7, 7, 7, /* 25-29 */
246 if (size < 0 || size > PSZ_CIF || frames < 4 || frames > 25)
248 frames = frames2frames[frames];
249 fps = frames2table[frames];
250 pEntry = &Nala_table[size][fps];
251 if (pEntry->alternate == 0)
254 if (pEntry->compressed && pdev->decompressor == NULL)
255 return -ENOENT; /* Not supported. */
257 memcpy(buf, pEntry->mode, 3);
258 ret = send_video_command(pdev->udev, pdev->vendpoint, buf, 3);
260 Debug("Failed to send video command... %d\n", ret);
263 if (pEntry->compressed && pdev->decompressor != NULL)
264 pdev->decompressor->init(pdev->release, buf, pdev->decompress_data);
266 /* Set various parameters */
267 pdev->vframes = frames;
269 pdev->valternate = pEntry->alternate;
270 pdev->image = pwc_image_sizes[size];
271 pdev->frame_size = (pdev->image.x * pdev->image.y * 3) / 2;
272 if (pEntry->compressed) {
273 if (pdev->release < 5) { /* 4 fold compression */
274 pdev->vbandlength = 528;
275 pdev->frame_size /= 4;
278 pdev->vbandlength = 704;
279 pdev->frame_size /= 3;
283 pdev->vbandlength = 0;
288 static inline int set_video_mode_Timon(struct pwc_device *pdev, int size, int frames, int compression, int snapshot)
290 unsigned char buf[13];
291 struct Timon_table_entry *pChoose;
294 if (size >= PSZ_MAX || frames < 5 || frames > 30 || compression < 0 || compression > 3)
296 if (size == PSZ_VGA && frames > 15)
298 fps = (frames / 5) - 1;
300 /* Find a supported framerate with progressively higher compression ratios
301 if the preferred ratio is not available.
304 if (pdev->decompressor == NULL) {
306 Debug("Trying to find uncompressed mode.\n");
308 pChoose = &Timon_table[size][fps][0];
311 while (compression <= 3) {
312 pChoose = &Timon_table[size][fps][compression];
313 if (pChoose->alternate != 0)
318 if (pChoose == NULL || pChoose->alternate == 0)
319 return -ENOENT; /* Not supported. */
321 memcpy(buf, pChoose->mode, 13);
324 ret = send_video_command(pdev->udev, pdev->vendpoint, buf, 13);
328 if (pChoose->bandlength > 0)
329 pdev->decompressor->init(pdev->release, buf, pdev->decompress_data);
331 /* Set various parameters */
332 pdev->vframes = frames;
334 pdev->vsnapshot = snapshot;
335 pdev->valternate = pChoose->alternate;
336 pdev->image = pwc_image_sizes[size];
337 pdev->vbandlength = pChoose->bandlength;
338 if (pChoose->bandlength > 0)
339 pdev->frame_size = (pChoose->bandlength * pdev->image.y) / 4;
341 pdev->frame_size = (pdev->image.x * pdev->image.y * 12) / 8;
346 static inline int set_video_mode_Kiara(struct pwc_device *pdev, int size, int frames, int compression, int snapshot)
348 struct Kiara_table_entry *pChoose;
350 unsigned char buf[12];
352 if (size >= PSZ_MAX || frames < 5 || frames > 30 || compression < 0 || compression > 3)
354 if (size == PSZ_VGA && frames > 15)
356 fps = (frames / 5) - 1;
358 /* Find a supported framerate with progressively higher compression ratios
359 if the preferred ratio is not available.
362 if (pdev->decompressor == NULL) {
364 Debug("Trying to find uncompressed mode.\n");
366 pChoose = &Kiara_table[size][fps][0];
369 while (compression <= 3) {
370 pChoose = &Kiara_table[size][fps][compression];
371 if (pChoose->alternate != 0)
376 if (pChoose == NULL || pChoose->alternate == 0)
377 return -ENOENT; /* Not supported. */
379 /* usb_control_msg won't take staticly allocated arrays as argument?? */
380 memcpy(buf, pChoose->mode, 12);
384 /* Firmware bug: video endpoint is 5, but commands are sent to endpoint 4 */
385 ret = send_video_command(pdev->udev, 4 /* pdev->vendpoint */, buf, 12);
389 if (pChoose->bandlength > 0)
390 pdev->decompressor->init(pdev->release, buf, pdev->decompress_data);
393 pdev->vframes = frames;
395 pdev->vsnapshot = snapshot;
396 pdev->valternate = pChoose->alternate;
397 pdev->image = pwc_image_sizes[size];
398 pdev->vbandlength = pChoose->bandlength;
399 if (pChoose->bandlength > 0)
400 pdev->frame_size = (pChoose->bandlength * pdev->image.y) / 4;
402 pdev->frame_size = (pdev->image.x * pdev->image.y * 12) / 8;
403 pdev->frame_size += (pdev->frame_header_size + pdev->frame_trailer_size);
409 @pdev: device structure
410 @width: viewport width
411 @height: viewport height
412 @frame: framerate, in fps
413 @compression: preferred compression ratio
414 @snapshot: snapshot mode or streaming
416 int pwc_set_video_mode(struct pwc_device *pdev, int width, int height, int frames, int compression, int snapshot)
420 size = pwc_decode_size(pdev, width, height);
422 Debug("Could not find suitable size.\n");
429 ret = set_video_mode_Nala(pdev, size, frames);
435 ret = set_video_mode_Timon(pdev, size, frames, compression, snapshot);
441 ret = set_video_mode_Kiara(pdev, size, frames, compression, snapshot);
446 Info("Video mode %s@%d fps is only supported with the decompressor module (pwcx).\n", size2name[size], frames);
448 Err("Failed to set video mode %s@%d fps; return code = %d\n", size2name[size], frames, ret);
452 pdev->view.x = width;
453 pdev->view.y = height;
454 pwc_set_image_buffer_size(pdev);
455 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);
460 void pwc_set_image_buffer_size(struct pwc_device *pdev)
462 int factor, i, filler = 0;
464 /* for PALETTE_YUV420P */
468 /* Set sizes in bytes */
469 pdev->image.size = pdev->image.x * pdev->image.y * factor / 4;
470 pdev->view.size = pdev->view.x * pdev->view.y * factor / 4;
472 /* Align offset, or you'll get some very weird results in
473 YUV420 mode... x must be multiple of 4 (to get the Y's in
474 place), and y even (or you'll mixup U & V). This is less of a
477 pdev->offset.x = ((pdev->view.x - pdev->image.x) / 2) & 0xFFFC;
478 pdev->offset.y = ((pdev->view.y - pdev->image.y) / 2) & 0xFFFE;
480 /* Fill buffers with gray or black */
481 for (i = 0; i < MAX_IMAGES; i++) {
482 if (pdev->image_ptr[i] != NULL)
483 memset(pdev->image_ptr[i], filler, pdev->view.size);
491 int pwc_get_brightness(struct pwc_device *pdev)
496 ret = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
498 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
499 BRIGHTNESS_FORMATTER,
507 int pwc_set_brightness(struct pwc_device *pdev, int value)
515 buf = (value >> 9) & 0x7f;
516 return usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
518 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
519 BRIGHTNESS_FORMATTER,
526 int pwc_get_contrast(struct pwc_device *pdev)
531 ret = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
533 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
542 int pwc_set_contrast(struct pwc_device *pdev, int value)
550 buf = (value >> 10) & 0x3f;
551 return usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
553 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
561 int pwc_get_gamma(struct pwc_device *pdev)
566 ret = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
568 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
577 int pwc_set_gamma(struct pwc_device *pdev, int value)
585 buf = (value >> 11) & 0x1f;
586 return usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
588 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
597 int pwc_get_saturation(struct pwc_device *pdev)
602 if (pdev->type < 675)
604 ret = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
606 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
607 pdev->type < 730 ? SATURATION_MODE_FORMATTER2 : SATURATION_MODE_FORMATTER1,
612 return 32768 + buf * 327;
615 int pwc_set_saturation(struct pwc_device *pdev, int value)
619 if (pdev->type < 675)
625 /* saturation ranges from -100 to +100 */
626 buf = (value - 32768) / 327;
627 return usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
629 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
630 pdev->type < 730 ? SATURATION_MODE_FORMATTER2 : SATURATION_MODE_FORMATTER1,
637 static inline int pwc_set_agc(struct pwc_device *pdev, int mode, int value)
643 buf = 0x0; /* auto */
645 buf = 0xff; /* fixed */
647 ret = usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
649 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
654 if (!mode && ret >= 0) {
659 buf = (value >> 10) & 0x3F;
660 ret = usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
662 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
663 PRESET_AGC_FORMATTER,
672 static inline int pwc_get_agc(struct pwc_device *pdev, int *value)
677 ret = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
679 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
686 if (buf != 0) { /* fixed */
687 ret = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
689 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
690 PRESET_AGC_FORMATTER,
697 *value = (buf << 10);
700 ret = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
702 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
708 /* Gah... this value ranges from 0x00 ... 0x9F */
711 *value = -(48 + buf * 409);
717 static inline int pwc_set_shutter_speed(struct pwc_device *pdev, int mode, int value)
724 buf[0] = 0x0; /* auto */
726 buf[0] = 0xff; /* fixed */
728 ret = usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
730 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
731 SHUTTER_MODE_FORMATTER,
735 if (!mode && ret >= 0) {
744 /* speed ranges from 0x0 to 0x290 (656) */
745 speed = (value / 100);
747 buf[0] = speed & 0xff;
752 /* speed seems to range from 0x0 to 0xff */
758 ret = usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
760 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
761 PRESET_SHUTTER_FORMATTER,
771 int pwc_camera_power(struct pwc_device *pdev, int power)
775 if (pdev->type < 675 || (pdev->type < 730 && pdev->release < 6))
776 return 0; /* Not supported by Nala or Timon < release 6 */
779 buf = 0x00; /* active */
781 buf = 0xFF; /* power save */
782 return usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
784 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
785 SET_POWER_SAVE_MODE_FORMATTER,
794 static inline int pwc_restore_user(struct pwc_device *pdev)
796 return usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
798 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
799 RESTORE_USER_DEFAULTS_FORMATTER,
804 static inline int pwc_save_user(struct pwc_device *pdev)
806 return usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
808 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
809 SAVE_USER_DEFAULTS_FORMATTER,
814 static inline int pwc_restore_factory(struct pwc_device *pdev)
816 return usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
818 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
819 RESTORE_FACTORY_DEFAULTS_FORMATTER,
824 /* ************************************************* */
825 /* Patch by Alvarado: (not in the original version */
828 * the camera recognizes modes from 0 to 4:
830 * 00: indoor (incandescant lighting)
831 * 01: outdoor (sunlight)
832 * 02: fluorescent lighting
836 static inline int pwc_set_awb(struct pwc_device *pdev, int mode)
847 buf = mode & 0x07; /* just the lowest three bits */
849 ret = usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
851 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
861 static inline int pwc_get_awb(struct pwc_device *pdev)
866 ret = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
868 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
878 static inline int pwc_set_red_gain(struct pwc_device *pdev, int value)
887 /* only the msb are considered */
890 return usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
892 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
893 PRESET_MANUAL_RED_GAIN_FORMATTER,
898 static inline int pwc_get_red_gain(struct pwc_device *pdev)
903 ret = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
905 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
906 PRESET_MANUAL_RED_GAIN_FORMATTER,
917 static inline int pwc_set_blue_gain(struct pwc_device *pdev, int value)
926 /* linear mapping of 0..0xffff to -0x80..0x7f */
929 return usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
931 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
932 PRESET_MANUAL_BLUE_GAIN_FORMATTER,
937 static inline int pwc_get_blue_gain(struct pwc_device *pdev)
942 ret = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
944 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
945 PRESET_MANUAL_BLUE_GAIN_FORMATTER,
956 /* The following two functions are different, since they only read the
957 internal red/blue gains, which may be different from the manual
958 gains set or read above.
960 static inline int pwc_read_red_gain(struct pwc_device *pdev)
965 ret = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
967 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
968 READ_RED_GAIN_FORMATTER,
978 static inline int pwc_read_blue_gain(struct pwc_device *pdev)
983 ret = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
985 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
986 READ_BLUE_GAIN_FORMATTER,
997 static inline int pwc_set_wb_speed(struct pwc_device *pdev, int speed)
1001 /* useful range is 0x01..0x20 */
1002 buf = speed / 0x7f0;
1003 return usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
1005 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1006 AWB_CONTROL_SPEED_FORMATTER,
1011 static inline int pwc_get_wb_speed(struct pwc_device *pdev)
1016 ret = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
1018 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1019 AWB_CONTROL_SPEED_FORMATTER,
1024 return (buf * 0x7f0);
1028 static inline int pwc_set_wb_delay(struct pwc_device *pdev, int delay)
1032 /* useful range is 0x01..0x3F */
1033 buf = (delay >> 10);
1034 return usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
1036 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1037 AWB_CONTROL_DELAY_FORMATTER,
1042 static inline int pwc_get_wb_delay(struct pwc_device *pdev)
1047 ret = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
1049 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1050 AWB_CONTROL_DELAY_FORMATTER,
1059 int pwc_set_leds(struct pwc_device *pdev, int on_value, int off_value)
1061 unsigned char buf[2];
1063 if (pdev->type < 730)
1069 if (on_value > 0xff)
1073 if (off_value > 0xff)
1079 return SendControlMsg(SET_STATUS_CTL, LED_FORMATTER, 2);
1082 int pwc_get_leds(struct pwc_device *pdev, int *on_value, int *off_value)
1084 unsigned char buf[2];
1087 if (pdev->type < 730) {
1093 ret = RecvControlMsg(GET_STATUS_CTL, LED_FORMATTER, 2);
1096 *on_value = buf[0] * 100;
1097 *off_value = buf[1] * 100;
1101 static inline int pwc_set_contour(struct pwc_device *pdev, int contour)
1107 buf = 0xff; /* auto contour on */
1109 buf = 0x0; /* auto contour off */
1110 ret = usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
1112 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1113 AUTO_CONTOUR_FORMATTER,
1121 if (contour > 0xffff)
1124 buf = (contour >> 10); /* contour preset is [0..3f] */
1125 ret = usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
1127 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1128 PRESET_CONTOUR_FORMATTER,
1136 static inline int pwc_get_contour(struct pwc_device *pdev, int *contour)
1141 ret = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
1143 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1144 AUTO_CONTOUR_FORMATTER,
1151 /* auto mode off, query current preset value */
1152 ret = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
1154 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1155 PRESET_CONTOUR_FORMATTER,
1160 *contour = (buf << 10);
1168 static inline int pwc_set_backlight(struct pwc_device *pdev, int backlight)
1176 return usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
1178 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1179 BACK_LIGHT_COMPENSATION_FORMATTER,
1184 static inline int pwc_get_backlight(struct pwc_device *pdev)
1189 ret = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
1191 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1192 BACK_LIGHT_COMPENSATION_FORMATTER,
1201 static inline int pwc_set_flicker(struct pwc_device *pdev, int flicker)
1209 return SendControlMsg(SET_LUM_CTL, FLICKERLESS_MODE_FORMATTER, 1);
1212 static inline int pwc_get_flicker(struct pwc_device *pdev)
1217 ret = RecvControlMsg(GET_LUM_CTL, FLICKERLESS_MODE_FORMATTER, 1);
1224 static inline int pwc_set_dynamic_noise(struct pwc_device *pdev, int noise)
1233 return SendControlMsg(SET_LUM_CTL, DYNAMIC_NOISE_CONTROL_FORMATTER, 1);
1236 static inline int pwc_get_dynamic_noise(struct pwc_device *pdev)
1241 ret = RecvControlMsg(GET_LUM_CTL, DYNAMIC_NOISE_CONTROL_FORMATTER, 1);
1248 int pwc_get_cmos_sensor(struct pwc_device *pdev)
1251 int ret = -1, request;
1253 if (pdev->type < 675)
1254 request = SENSOR_TYPE_FORMATTER1;
1255 else if (pdev->type < 730)
1256 return -1; /* The Vesta series doesn't have this call */
1258 request = SENSOR_TYPE_FORMATTER2;
1260 ret = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
1262 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1268 if (pdev->type < 675)
1275 /* End of Add-Ons */
1276 /* ************************************************* */
1278 int pwc_ioctl(struct pwc_device *pdev, unsigned int cmd, void *arg)
1283 case VIDIOCPWCRUSER:
1285 if (pwc_restore_user(pdev))
1290 case VIDIOCPWCSUSER:
1292 if (pwc_save_user(pdev))
1297 case VIDIOCPWCFACTORY:
1299 if (pwc_restore_factory(pdev))
1304 case VIDIOCPWCSCQUAL:
1308 if (copy_from_user(&qual, arg, sizeof(int)))
1311 if (qual < 0 || qual > 3)
1314 ret = pwc_try_video_mode(pdev, pdev->view.x, pdev->view.y, pdev->vframes, qual, pdev->vsnapshot);
1316 pdev->vcompression = qual;
1321 case VIDIOCPWCGCQUAL:
1323 if (copy_to_user(arg, &pdev->vcompression, sizeof(int)))
1328 case VIDIOCPWCPROBE:
1330 struct pwc_probe probe;
1332 strcpy(probe.name, pdev->vdev.name);
1333 probe.type = pdev->type;
1334 if (copy_to_user(arg, &probe, sizeof(probe)))
1343 if (copy_from_user(&agc, arg, sizeof(agc)))
1346 if (pwc_set_agc(pdev, agc < 0 ? 1 : 0, agc))
1356 if (pwc_get_agc(pdev, &agc))
1359 if (copy_to_user(arg, &agc, sizeof(agc)))
1364 case VIDIOCPWCSSHUTTER:
1368 if (copy_from_user(&shutter_speed, arg, sizeof(shutter_speed)))
1371 ret = pwc_set_shutter_speed(pdev, shutter_speed < 0 ? 1 : 0, shutter_speed);
1377 struct pwc_whitebalance wb;
1379 if (copy_from_user(&wb, arg, sizeof(wb)))
1382 ret = pwc_set_awb(pdev, wb.mode);
1383 if (ret >= 0 && wb.mode == PWC_WB_MANUAL) {
1384 pwc_set_red_gain(pdev, wb.manual_red);
1385 pwc_set_blue_gain(pdev, wb.manual_blue);
1393 struct pwc_whitebalance wb;
1395 memset(&wb, 0, sizeof(wb));
1396 wb.mode = pwc_get_awb(pdev);
1400 if (wb.mode == PWC_WB_MANUAL) {
1401 wb.manual_red = pwc_get_red_gain(pdev);
1402 wb.manual_blue = pwc_get_blue_gain(pdev);
1404 if (wb.mode == PWC_WB_AUTO) {
1405 wb.read_red = pwc_read_red_gain(pdev);
1406 wb.read_blue = pwc_read_blue_gain(pdev);
1408 if (copy_to_user(arg, &wb, sizeof(wb)))
1414 case VIDIOCPWCSAWBSPEED:
1416 struct pwc_wb_speed wbs;
1418 if (copy_from_user(&wbs, arg, sizeof(wbs)))
1421 if (wbs.control_speed > 0) {
1422 ret = pwc_set_wb_speed(pdev, wbs.control_speed);
1424 if (wbs.control_delay > 0) {
1425 ret = pwc_set_wb_delay(pdev, wbs.control_delay);
1431 case VIDIOCPWCGAWBSPEED:
1433 struct pwc_wb_speed wbs;
1435 ret = pwc_get_wb_speed(pdev);
1438 wbs.control_speed = ret;
1439 ret = pwc_get_wb_delay(pdev);
1442 wbs.control_delay = ret;
1443 if (copy_to_user(arg, &wbs, sizeof(wbs)))
1450 struct pwc_leds leds;
1452 if (copy_from_user(&leds, arg, sizeof(leds)))
1455 ret = pwc_set_leds(pdev, leds.led_on, leds.led_off);
1462 struct pwc_leds leds;
1464 ret = pwc_get_leds(pdev, &leds.led_on, &leds.led_off);
1467 if (copy_to_user(arg, &leds, sizeof(leds)))
1472 case VIDIOCPWCSCONTOUR:
1476 if (copy_from_user(&contour, arg, sizeof(contour)))
1479 ret = pwc_set_contour(pdev, contour);
1483 case VIDIOCPWCGCONTOUR:
1487 ret = pwc_get_contour(pdev, &contour);
1491 if (copy_to_user(arg, &contour, sizeof(contour)))
1496 case VIDIOCPWCSBACKLIGHT:
1500 if (copy_from_user(&backlight, arg, sizeof(backlight)))
1503 ret = pwc_set_backlight(pdev, backlight);
1507 case VIDIOCPWCGBACKLIGHT:
1509 ret = pwc_get_backlight(pdev);
1512 if (copy_to_user(arg, &ret, sizeof(ret)))
1517 case VIDIOCPWCSFLICKER:
1521 if (copy_from_user(&flicker, arg, sizeof(flicker)))
1524 ret = pwc_set_flicker(pdev, flicker);
1528 case VIDIOCPWCGFLICKER:
1530 ret = pwc_get_flicker(pdev);
1533 if (copy_to_user(arg, &ret, sizeof(ret)))
1538 case VIDIOCPWCSDYNNOISE:
1542 if (copy_from_user(&dynnoise, arg, sizeof(dynnoise)))
1545 ret = pwc_set_dynamic_noise(pdev, dynnoise);
1549 case VIDIOCPWCGDYNNOISE:
1551 ret = pwc_get_dynamic_noise(pdev);
1554 if (copy_to_user(arg, &ret, sizeof(ret)))
1559 case VIDIOCPWCGREALSIZE:
1561 struct pwc_imagesize size;
1563 size.width = pdev->image.x;
1564 size.height = pdev->image.y;
1565 if (copy_to_user(arg, &size, sizeof(size)))