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
47 #define SET_MPT_CTL 0x0D
48 #define GET_MPT_CTL 0x0E
50 /* Selectors for the Luminance controls [GS]ET_LUM_CTL */
51 #define AGC_MODE_FORMATTER 0x2000
52 #define PRESET_AGC_FORMATTER 0x2100
53 #define SHUTTER_MODE_FORMATTER 0x2200
54 #define PRESET_SHUTTER_FORMATTER 0x2300
55 #define PRESET_CONTOUR_FORMATTER 0x2400
56 #define AUTO_CONTOUR_FORMATTER 0x2500
57 #define BACK_LIGHT_COMPENSATION_FORMATTER 0x2600
58 #define CONTRAST_FORMATTER 0x2700
59 #define DYNAMIC_NOISE_CONTROL_FORMATTER 0x2800
60 #define FLICKERLESS_MODE_FORMATTER 0x2900
61 #define AE_CONTROL_SPEED 0x2A00
62 #define BRIGHTNESS_FORMATTER 0x2B00
63 #define GAMMA_FORMATTER 0x2C00
65 /* Selectors for the Chrominance controls [GS]ET_CHROM_CTL */
66 #define WB_MODE_FORMATTER 0x1000
67 #define AWB_CONTROL_SPEED_FORMATTER 0x1100
68 #define AWB_CONTROL_DELAY_FORMATTER 0x1200
69 #define PRESET_MANUAL_RED_GAIN_FORMATTER 0x1300
70 #define PRESET_MANUAL_BLUE_GAIN_FORMATTER 0x1400
71 #define COLOUR_MODE_FORMATTER 0x1500
72 #define SATURATION_MODE_FORMATTER1 0x1600
73 #define SATURATION_MODE_FORMATTER2 0x1700
75 /* Selectors for the Status controls [GS]ET_STATUS_CTL */
76 #define SAVE_USER_DEFAULTS_FORMATTER 0x0200
77 #define RESTORE_USER_DEFAULTS_FORMATTER 0x0300
78 #define RESTORE_FACTORY_DEFAULTS_FORMATTER 0x0400
79 #define READ_AGC_FORMATTER 0x0500
80 #define READ_SHUTTER_FORMATTER 0x0600
81 #define READ_RED_GAIN_FORMATTER 0x0700
82 #define READ_BLUE_GAIN_FORMATTER 0x0800
83 #define SENSOR_TYPE_FORMATTER1 0x0C00
84 #define READ_RAW_Y_MEAN_FORMATTER 0x3100
85 #define SET_POWER_SAVE_MODE_FORMATTER 0x3200
86 #define MIRROR_IMAGE_FORMATTER 0x3300
87 #define LED_FORMATTER 0x3400
88 #define SENSOR_TYPE_FORMATTER2 0x3700
90 /* Formatters for the Video Endpoint controls [GS]ET_EP_STREAM_CTL */
91 #define VIDEO_OUTPUT_CONTROL_FORMATTER 0x0100
93 /* Formatters for the motorized pan & tilt [GS]ET_MPT_CTL */
94 #define PT_RELATIVE_CONTROL_FORMATTER 0x01
95 #define PT_RESET_CONTROL_FORMATTER 0x02
96 #define PT_STATUS_FORMATTER 0x03
98 static char *size2name[PSZ_MAX] =
110 /* Entries for the Nala (645/646) camera; the Nala doesn't have compression
111 preferences, so you either get compressed or non-compressed streams.
113 An alternate value of 0 means this mode is not available at all.
116 struct Nala_table_entry {
117 char alternate; /* USB alternate setting */
118 int compressed; /* Compressed yes/no */
120 unsigned char mode[3]; /* precomputed mode table */
123 static struct Nala_table_entry Nala_table[PSZ_MAX][8] =
125 #include "pwc_nala.h"
128 /* This tables contains entries for the 675/680/690 (Timon) camera, with
129 4 different qualities (no compression, low, medium, high).
130 It lists the bandwidth requirements for said mode by its alternate interface
131 number. An alternate of 0 means that the mode is unavailable.
133 There are 6 * 4 * 4 entries:
134 6 different resolutions subqcif, qsif, qcif, sif, cif, vga
135 6 framerates: 5, 10, 15, 20, 25, 30
136 4 compression modi: none, low, medium, high
138 When an uncompressed mode is not available, the next available compressed mode
139 will be chosen (unless the decompressor is absent). Sometimes there are only
140 1 or 2 compressed modes available; in that case entries are duplicated.
142 struct Timon_table_entry
144 char alternate; /* USB alternate interface */
145 unsigned short packetsize; /* Normal packet size */
146 unsigned short bandlength; /* Bandlength when decompressing */
147 unsigned char mode[13]; /* precomputed mode settings for cam */
150 static struct Timon_table_entry Timon_table[PSZ_MAX][6][4] =
152 #include "pwc_timon.h"
155 /* Entries for the Kiara (730/740/750) camera */
157 struct Kiara_table_entry
159 char alternate; /* USB alternate interface */
160 unsigned short packetsize; /* Normal packet size */
161 unsigned short bandlength; /* Bandlength when decompressing */
162 unsigned char mode[12]; /* precomputed mode settings for cam */
165 static struct Kiara_table_entry Kiara_table[PSZ_MAX][6][4] =
167 #include "pwc_kiara.h"
171 /****************************************************************************/
174 #define SendControlMsg(request, value, buflen) \
175 usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0), \
177 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, \
180 &buf, buflen, HZ / 2)
182 #define RecvControlMsg(request, value, buflen) \
183 usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0), \
185 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, \
188 &buf, buflen, HZ / 2)
192 void pwc_hexdump(void *p, int len)
198 s = (unsigned char *)p;
201 Debug("Doing hexdump @ %p, %d bytes.\n", p, len);
202 for (i = 0; i < len; i++) {
203 d += sprintf(d, "%02X ", *s++);
204 if ((i & 0xF) == 0xF) {
215 static inline int send_video_command(struct usb_device *udev, int index, void *buf, int buflen)
217 return usb_control_msg(udev,
218 usb_sndctrlpipe(udev, 0),
220 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
221 VIDEO_OUTPUT_CONTROL_FORMATTER,
228 static inline int set_video_mode_Nala(struct pwc_device *pdev, int size, int frames)
230 unsigned char buf[3];
232 struct Nala_table_entry *pEntry;
233 int frames2frames[31] =
234 { /* closest match of framerate */
235 0, 0, 0, 0, 4, /* 0-4 */
236 5, 5, 7, 7, 10, /* 5-9 */
237 10, 10, 12, 12, 15, /* 10-14 */
238 15, 15, 15, 20, 20, /* 15-19 */
239 20, 20, 20, 24, 24, /* 20-24 */
240 24, 24, 24, 24, 24, /* 25-29 */
243 int frames2table[31] =
244 { 0, 0, 0, 0, 0, /* 0-4 */
245 1, 1, 1, 2, 2, /* 5-9 */
246 3, 3, 4, 4, 4, /* 10-14 */
247 5, 5, 5, 5, 5, /* 15-19 */
248 6, 6, 6, 6, 7, /* 20-24 */
249 7, 7, 7, 7, 7, /* 25-29 */
253 if (size < 0 || size > PSZ_CIF || frames < 4 || frames > 25)
255 frames = frames2frames[frames];
256 fps = frames2table[frames];
257 pEntry = &Nala_table[size][fps];
258 if (pEntry->alternate == 0)
261 if (pEntry->compressed && pdev->decompressor == NULL)
262 return -ENOENT; /* Not supported. */
264 memcpy(buf, pEntry->mode, 3);
265 ret = send_video_command(pdev->udev, pdev->vendpoint, buf, 3);
267 Debug("Failed to send video command... %d\n", ret);
270 if (pEntry->compressed && pdev->decompressor != NULL)
271 pdev->decompressor->init(pdev->release, buf, pdev->decompress_data);
273 /* Set various parameters */
274 pdev->vframes = frames;
276 pdev->valternate = pEntry->alternate;
277 pdev->image = pwc_image_sizes[size];
278 pdev->frame_size = (pdev->image.x * pdev->image.y * 3) / 2;
279 if (pEntry->compressed) {
280 if (pdev->release < 5) { /* 4 fold compression */
281 pdev->vbandlength = 528;
282 pdev->frame_size /= 4;
285 pdev->vbandlength = 704;
286 pdev->frame_size /= 3;
290 pdev->vbandlength = 0;
295 static inline int set_video_mode_Timon(struct pwc_device *pdev, int size, int frames, int compression, int snapshot)
297 unsigned char buf[13];
298 struct Timon_table_entry *pChoose;
301 if (size >= PSZ_MAX || frames < 5 || frames > 30 || compression < 0 || compression > 3)
303 if (size == PSZ_VGA && frames > 15)
305 fps = (frames / 5) - 1;
307 /* Find a supported framerate with progressively higher compression ratios
308 if the preferred ratio is not available.
311 if (pdev->decompressor == NULL) {
313 Debug("Trying to find uncompressed mode.\n");
315 pChoose = &Timon_table[size][fps][0];
318 while (compression <= 3) {
319 pChoose = &Timon_table[size][fps][compression];
320 if (pChoose->alternate != 0)
325 if (pChoose == NULL || pChoose->alternate == 0)
326 return -ENOENT; /* Not supported. */
328 memcpy(buf, pChoose->mode, 13);
331 ret = send_video_command(pdev->udev, pdev->vendpoint, buf, 13);
335 if (pChoose->bandlength > 0)
336 pdev->decompressor->init(pdev->release, buf, pdev->decompress_data);
338 /* Set various parameters */
339 pdev->vframes = frames;
341 pdev->vsnapshot = snapshot;
342 pdev->valternate = pChoose->alternate;
343 pdev->image = pwc_image_sizes[size];
344 pdev->vbandlength = pChoose->bandlength;
345 if (pChoose->bandlength > 0)
346 pdev->frame_size = (pChoose->bandlength * pdev->image.y) / 4;
348 pdev->frame_size = (pdev->image.x * pdev->image.y * 12) / 8;
353 static inline int set_video_mode_Kiara(struct pwc_device *pdev, int size, int frames, int compression, int snapshot)
355 struct Kiara_table_entry *pChoose;
357 unsigned char buf[12];
359 if (size >= PSZ_MAX || frames < 5 || frames > 30 || compression < 0 || compression > 3)
361 if (size == PSZ_VGA && frames > 15)
363 fps = (frames / 5) - 1;
365 /* Find a supported framerate with progressively higher compression ratios
366 if the preferred ratio is not available.
369 if (pdev->decompressor == NULL) {
371 Debug("Trying to find uncompressed mode.\n");
373 pChoose = &Kiara_table[size][fps][0];
376 while (compression <= 3) {
377 pChoose = &Kiara_table[size][fps][compression];
378 if (pChoose->alternate != 0)
383 if (pChoose == NULL || pChoose->alternate == 0)
384 return -ENOENT; /* Not supported. */
386 /* usb_control_msg won't take staticly allocated arrays as argument?? */
387 memcpy(buf, pChoose->mode, 12);
391 /* Firmware bug: video endpoint is 5, but commands are sent to endpoint 4 */
392 ret = send_video_command(pdev->udev, 4 /* pdev->vendpoint */, buf, 12);
396 if (pChoose->bandlength > 0)
397 pdev->decompressor->init(pdev->release, buf, pdev->decompress_data);
400 pdev->vframes = frames;
402 pdev->vsnapshot = snapshot;
403 pdev->valternate = pChoose->alternate;
404 pdev->image = pwc_image_sizes[size];
405 pdev->vbandlength = pChoose->bandlength;
406 if (pChoose->bandlength > 0)
407 pdev->frame_size = (pChoose->bandlength * pdev->image.y) / 4;
409 pdev->frame_size = (pdev->image.x * pdev->image.y * 12) / 8;
410 pdev->frame_size += (pdev->frame_header_size + pdev->frame_trailer_size);
416 @pdev: device structure
417 @width: viewport width
418 @height: viewport height
419 @frame: framerate, in fps
420 @compression: preferred compression ratio
421 @snapshot: snapshot mode or streaming
423 int pwc_set_video_mode(struct pwc_device *pdev, int width, int height, int frames, int compression, int snapshot)
427 size = pwc_decode_size(pdev, width, height);
429 Debug("Could not find suitable size.\n");
436 ret = set_video_mode_Nala(pdev, size, frames);
442 ret = set_video_mode_Timon(pdev, size, frames, compression, snapshot);
449 ret = set_video_mode_Kiara(pdev, size, frames, compression, snapshot);
454 Info("Video mode %s@%d fps is only supported with the decompressor module (pwcx).\n", size2name[size], frames);
456 Err("Failed to set video mode %s@%d fps; return code = %d\n", size2name[size], frames, ret);
460 pdev->view.x = width;
461 pdev->view.y = height;
462 pwc_set_image_buffer_size(pdev);
463 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);
468 void pwc_set_image_buffer_size(struct pwc_device *pdev)
470 int factor, i, filler = 0;
475 /* Set sizes in bytes */
476 pdev->image.size = pdev->image.x * pdev->image.y * factor / 4;
477 pdev->view.size = pdev->view.x * pdev->view.y * factor / 4;
479 /* Align offset, or you'll get some very weird results in
480 YUV420 mode... x must be multiple of 4 (to get the Y's in
481 place), and y even (or you'll mixup U & V). This is less of a
484 pdev->offset.x = ((pdev->view.x - pdev->image.x) / 2) & 0xFFFC;
485 pdev->offset.y = ((pdev->view.y - pdev->image.y) / 2) & 0xFFFE;
487 /* Fill buffers with gray or black */
488 for (i = 0; i < MAX_IMAGES; i++) {
489 if (pdev->image_ptr[i] != NULL)
490 memset(pdev->image_ptr[i], filler, pdev->view.size);
498 int pwc_get_brightness(struct pwc_device *pdev)
503 ret = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
505 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
506 BRIGHTNESS_FORMATTER,
514 int pwc_set_brightness(struct pwc_device *pdev, int value)
522 buf = (value >> 9) & 0x7f;
523 return usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
525 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
526 BRIGHTNESS_FORMATTER,
533 int pwc_get_contrast(struct pwc_device *pdev)
538 ret = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
540 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
549 int pwc_set_contrast(struct pwc_device *pdev, int value)
557 buf = (value >> 10) & 0x3f;
558 return usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
560 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
568 int pwc_get_gamma(struct pwc_device *pdev)
573 ret = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
575 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
584 int pwc_set_gamma(struct pwc_device *pdev, int value)
592 buf = (value >> 11) & 0x1f;
593 return usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
595 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
604 int pwc_get_saturation(struct pwc_device *pdev)
609 if (pdev->type < 675)
611 ret = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
613 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
614 pdev->type < 730 ? SATURATION_MODE_FORMATTER2 : SATURATION_MODE_FORMATTER1,
619 return 32768 + buf * 327;
622 int pwc_set_saturation(struct pwc_device *pdev, int value)
626 if (pdev->type < 675)
632 /* saturation ranges from -100 to +100 */
633 buf = (value - 32768) / 327;
634 return usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
636 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
637 pdev->type < 730 ? SATURATION_MODE_FORMATTER2 : SATURATION_MODE_FORMATTER1,
644 static inline int pwc_set_agc(struct pwc_device *pdev, int mode, int value)
650 buf = 0x0; /* auto */
652 buf = 0xff; /* fixed */
654 ret = usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
656 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
661 if (!mode && ret >= 0) {
666 buf = (value >> 10) & 0x3F;
667 ret = usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
669 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
670 PRESET_AGC_FORMATTER,
679 static inline int pwc_get_agc(struct pwc_device *pdev, int *value)
684 ret = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
686 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
693 if (buf != 0) { /* fixed */
694 ret = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
696 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
697 PRESET_AGC_FORMATTER,
704 *value = (buf << 10);
707 ret = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
709 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
715 /* Gah... this value ranges from 0x00 ... 0x9F */
718 *value = -(48 + buf * 409);
724 static inline int pwc_set_shutter_speed(struct pwc_device *pdev, int mode, int value)
731 buf[0] = 0x0; /* auto */
733 buf[0] = 0xff; /* fixed */
735 ret = usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
737 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
738 SHUTTER_MODE_FORMATTER,
742 if (!mode && ret >= 0) {
751 /* speed ranges from 0x0 to 0x290 (656) */
752 speed = (value / 100);
754 buf[0] = speed & 0xff;
760 /* speed seems to range from 0x0 to 0xff */
766 ret = usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
768 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
769 PRESET_SHUTTER_FORMATTER,
779 int pwc_camera_power(struct pwc_device *pdev, int power)
783 if (pdev->type < 675 || (pdev->type < 730 && pdev->release < 6))
784 return 0; /* Not supported by Nala or Timon < release 6 */
787 buf = 0x00; /* active */
789 buf = 0xFF; /* power save */
790 return usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
792 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
793 SET_POWER_SAVE_MODE_FORMATTER,
802 static inline int pwc_restore_user(struct pwc_device *pdev)
804 return usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
806 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
807 RESTORE_USER_DEFAULTS_FORMATTER,
812 static inline int pwc_save_user(struct pwc_device *pdev)
814 return usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
816 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
817 SAVE_USER_DEFAULTS_FORMATTER,
822 static inline int pwc_restore_factory(struct pwc_device *pdev)
824 return usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
826 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
827 RESTORE_FACTORY_DEFAULTS_FORMATTER,
832 /* ************************************************* */
833 /* Patch by Alvarado: (not in the original version */
836 * the camera recognizes modes from 0 to 4:
838 * 00: indoor (incandescant lighting)
839 * 01: outdoor (sunlight)
840 * 02: fluorescent lighting
844 static inline int pwc_set_awb(struct pwc_device *pdev, int mode)
855 buf = mode & 0x07; /* just the lowest three bits */
857 ret = usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
859 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
869 static inline int pwc_get_awb(struct pwc_device *pdev)
874 ret = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
876 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
886 static inline int pwc_set_red_gain(struct pwc_device *pdev, int value)
895 /* only the msb are considered */
898 return usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
900 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
901 PRESET_MANUAL_RED_GAIN_FORMATTER,
906 static inline int pwc_get_red_gain(struct pwc_device *pdev)
911 ret = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
913 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
914 PRESET_MANUAL_RED_GAIN_FORMATTER,
925 static inline int pwc_set_blue_gain(struct pwc_device *pdev, int value)
934 /* linear mapping of 0..0xffff to -0x80..0x7f */
937 return usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
939 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
940 PRESET_MANUAL_BLUE_GAIN_FORMATTER,
945 static inline int pwc_get_blue_gain(struct pwc_device *pdev)
950 ret = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
952 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
953 PRESET_MANUAL_BLUE_GAIN_FORMATTER,
964 /* The following two functions are different, since they only read the
965 internal red/blue gains, which may be different from the manual
966 gains set or read above.
968 static inline int pwc_read_red_gain(struct pwc_device *pdev)
973 ret = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
975 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
976 READ_RED_GAIN_FORMATTER,
986 static inline int pwc_read_blue_gain(struct pwc_device *pdev)
991 ret = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
993 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
994 READ_BLUE_GAIN_FORMATTER,
1005 static inline int pwc_set_wb_speed(struct pwc_device *pdev, int speed)
1009 /* useful range is 0x01..0x20 */
1010 buf = speed / 0x7f0;
1011 return usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
1013 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1014 AWB_CONTROL_SPEED_FORMATTER,
1019 static inline int pwc_get_wb_speed(struct pwc_device *pdev)
1024 ret = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
1026 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1027 AWB_CONTROL_SPEED_FORMATTER,
1032 return (buf * 0x7f0);
1036 static inline int pwc_set_wb_delay(struct pwc_device *pdev, int delay)
1040 /* useful range is 0x01..0x3F */
1041 buf = (delay >> 10);
1042 return usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
1044 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1045 AWB_CONTROL_DELAY_FORMATTER,
1050 static inline int pwc_get_wb_delay(struct pwc_device *pdev)
1055 ret = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
1057 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1058 AWB_CONTROL_DELAY_FORMATTER,
1067 int pwc_set_leds(struct pwc_device *pdev, int on_value, int off_value)
1069 unsigned char buf[2];
1071 if (pdev->type < 730)
1077 if (on_value > 0xff)
1081 if (off_value > 0xff)
1087 return SendControlMsg(SET_STATUS_CTL, LED_FORMATTER, 2);
1090 int pwc_get_leds(struct pwc_device *pdev, int *on_value, int *off_value)
1092 unsigned char buf[2];
1095 if (pdev->type < 730) {
1101 ret = RecvControlMsg(GET_STATUS_CTL, LED_FORMATTER, 2);
1104 *on_value = buf[0] * 100;
1105 *off_value = buf[1] * 100;
1109 static inline int pwc_set_contour(struct pwc_device *pdev, int contour)
1115 buf = 0xff; /* auto contour on */
1117 buf = 0x0; /* auto contour off */
1118 ret = usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
1120 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1121 AUTO_CONTOUR_FORMATTER,
1129 if (contour > 0xffff)
1132 buf = (contour >> 10); /* contour preset is [0..3f] */
1133 ret = usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
1135 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1136 PRESET_CONTOUR_FORMATTER,
1144 static inline int pwc_get_contour(struct pwc_device *pdev, int *contour)
1149 ret = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
1151 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1152 AUTO_CONTOUR_FORMATTER,
1159 /* auto mode off, query current preset value */
1160 ret = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
1162 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1163 PRESET_CONTOUR_FORMATTER,
1168 *contour = (buf << 10);
1176 static inline int pwc_set_backlight(struct pwc_device *pdev, int backlight)
1184 return usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
1186 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1187 BACK_LIGHT_COMPENSATION_FORMATTER,
1192 static inline int pwc_get_backlight(struct pwc_device *pdev)
1197 ret = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
1199 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1200 BACK_LIGHT_COMPENSATION_FORMATTER,
1209 static inline int pwc_set_flicker(struct pwc_device *pdev, int flicker)
1217 return SendControlMsg(SET_LUM_CTL, FLICKERLESS_MODE_FORMATTER, 1);
1220 static inline int pwc_get_flicker(struct pwc_device *pdev)
1225 ret = RecvControlMsg(GET_LUM_CTL, FLICKERLESS_MODE_FORMATTER, 1);
1232 static inline int pwc_set_dynamic_noise(struct pwc_device *pdev, int noise)
1241 return SendControlMsg(SET_LUM_CTL, DYNAMIC_NOISE_CONTROL_FORMATTER, 1);
1244 static inline int pwc_get_dynamic_noise(struct pwc_device *pdev)
1249 ret = RecvControlMsg(GET_LUM_CTL, DYNAMIC_NOISE_CONTROL_FORMATTER, 1);
1255 int pwc_mpt_reset(struct pwc_device *pdev, int flags)
1259 buf = flags & 0x03; // only lower two bits are currently used
1260 return SendControlMsg(SET_MPT_CTL, PT_RESET_CONTROL_FORMATTER, 1);
1263 static inline int pwc_mpt_set_angle(struct pwc_device *pdev, int pan, int tilt)
1265 unsigned char buf[4];
1267 /* set new relative angle; angles are expressed in degrees * 100,
1268 but cam as .5 degree resolution, hence devide by 200. Also
1269 the angle must be multiplied by 64 before it's send to
1272 pan = 64 * pan / 100;
1273 tilt = -64 * tilt / 100; /* positive tilt is down, which is not what the user would expect */
1274 buf[0] = pan & 0xFF;
1275 buf[1] = (pan >> 8) & 0xFF;
1276 buf[2] = tilt & 0xFF;
1277 buf[3] = (tilt >> 8) & 0xFF;
1278 return SendControlMsg(SET_MPT_CTL, PT_RELATIVE_CONTROL_FORMATTER, 4);
1281 static inline int pwc_mpt_get_status(struct pwc_device *pdev, struct pwc_mpt_status *status)
1284 unsigned char buf[5];
1286 ret = RecvControlMsg(GET_MPT_CTL, PT_STATUS_FORMATTER, 5);
1289 status->status = buf[0] & 0x7; // 3 bits are used for reporting
1290 status->time_pan = (buf[1] << 8) + buf[2];
1291 status->time_tilt = (buf[3] << 8) + buf[4];
1296 int pwc_get_cmos_sensor(struct pwc_device *pdev)
1299 int ret = -1, request;
1301 if (pdev->type < 675)
1302 request = SENSOR_TYPE_FORMATTER1;
1303 else if (pdev->type < 730)
1304 return -1; /* The Vesta series doesn't have this call */
1306 request = SENSOR_TYPE_FORMATTER2;
1308 ret = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
1310 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1316 if (pdev->type < 675)
1323 /* End of Add-Ons */
1324 /* ************************************************* */
1326 int pwc_ioctl(struct pwc_device *pdev, unsigned int cmd, void *arg)
1331 case VIDIOCPWCRUSER:
1333 if (pwc_restore_user(pdev))
1338 case VIDIOCPWCSUSER:
1340 if (pwc_save_user(pdev))
1345 case VIDIOCPWCFACTORY:
1347 if (pwc_restore_factory(pdev))
1352 case VIDIOCPWCSCQUAL:
1356 if (*qual < 0 || *qual > 3)
1359 ret = pwc_try_video_mode(pdev, pdev->view.x, pdev->view.y, pdev->vframes, *qual, pdev->vsnapshot);
1361 pdev->vcompression = *qual;
1365 case VIDIOCPWCGCQUAL:
1369 *qual = pdev->vcompression;
1373 case VIDIOCPWCPROBE:
1375 struct pwc_probe *probe = arg;
1377 strcpy(probe->name, pdev->vdev.name);
1378 probe->type = pdev->type;
1386 if (pwc_set_agc(pdev, *agc < 0 ? 1 : 0, *agc))
1395 if (pwc_get_agc(pdev, agc))
1400 case VIDIOCPWCSSHUTTER:
1402 int *shutter_speed = arg;
1404 ret = pwc_set_shutter_speed(pdev, *shutter_speed < 0 ? 1 : 0, *shutter_speed);
1410 struct pwc_whitebalance *wb = arg;
1412 ret = pwc_set_awb(pdev, wb->mode);
1413 if (ret >= 0 && wb->mode == PWC_WB_MANUAL) {
1414 pwc_set_red_gain(pdev, wb->manual_red);
1415 pwc_set_blue_gain(pdev, wb->manual_blue);
1422 struct pwc_whitebalance *wb = arg;
1424 memset(wb, 0, sizeof(*wb));
1425 wb->mode = pwc_get_awb(pdev);
1429 if (wb->mode == PWC_WB_MANUAL) {
1430 wb->manual_red = pwc_get_red_gain(pdev);
1431 wb->manual_blue = pwc_get_blue_gain(pdev);
1433 if (wb->mode == PWC_WB_AUTO) {
1434 wb->read_red = pwc_read_red_gain(pdev);
1435 wb->read_blue = pwc_read_blue_gain(pdev);
1441 case VIDIOCPWCSAWBSPEED:
1443 struct pwc_wb_speed *wbs = arg;
1445 if (wbs->control_speed > 0) {
1446 ret = pwc_set_wb_speed(pdev, wbs->control_speed);
1448 if (wbs->control_delay > 0) {
1449 ret = pwc_set_wb_delay(pdev, wbs->control_delay);
1454 case VIDIOCPWCGAWBSPEED:
1456 struct pwc_wb_speed *wbs = arg;
1458 ret = pwc_get_wb_speed(pdev);
1461 wbs->control_speed = ret;
1462 ret = pwc_get_wb_delay(pdev);
1465 wbs->control_delay = ret;
1471 struct pwc_leds *leds = arg;
1473 ret = pwc_set_leds(pdev, leds->led_on, leds->led_off);
1480 struct pwc_leds *leds = arg;
1482 ret = pwc_get_leds(pdev, &leds->led_on, &leds->led_off);
1486 case VIDIOCPWCSCONTOUR:
1490 ret = pwc_set_contour(pdev, *contour);
1494 case VIDIOCPWCGCONTOUR:
1498 ret = pwc_get_contour(pdev, contour);
1502 case VIDIOCPWCSBACKLIGHT:
1504 int *backlight = arg;
1506 ret = pwc_set_backlight(pdev, *backlight);
1510 case VIDIOCPWCGBACKLIGHT:
1512 int *backlight = arg;
1514 ret = pwc_get_backlight(pdev);
1520 case VIDIOCPWCSFLICKER:
1524 ret = pwc_set_flicker(pdev, *flicker);
1528 case VIDIOCPWCGFLICKER:
1532 ret = pwc_get_flicker(pdev);
1538 case VIDIOCPWCSDYNNOISE:
1540 int *dynnoise = arg;
1542 ret = pwc_set_dynamic_noise(pdev, *dynnoise);
1546 case VIDIOCPWCGDYNNOISE:
1548 int *dynnoise = arg;
1550 ret = pwc_get_dynamic_noise(pdev);
1557 case VIDIOCPWCGREALSIZE:
1559 struct pwc_imagesize *size = arg;
1561 size->width = pdev->image.x;
1562 size->height = pdev->image.y;
1566 case VIDIOCPWCMPTRESET:
1570 if (pdev->features & FEATURE_MOTOR_PANTILT)
1572 ret = pwc_mpt_reset(pdev, *flags);
1575 pdev->pan_angle = 0;
1576 pdev->tilt_angle = 0;
1585 case VIDIOCPWCMPTGRANGE:
1587 if (pdev->features & FEATURE_MOTOR_PANTILT)
1589 memcpy(arg, &pdev->angle_range, sizeof(struct pwc_mpt_range));
1598 case VIDIOCPWCMPTSANGLE:
1600 struct pwc_mpt_angles *angles = arg;
1601 int new_pan, new_tilt;
1603 if (pdev->features & FEATURE_MOTOR_PANTILT)
1605 /* The camera can only set relative angles, so
1606 do some calculations when getting an absolute angle .
1608 if (angles->absolute)
1610 new_pan = angles->pan;
1611 new_tilt = angles->tilt;
1615 new_pan = pdev->pan_angle + angles->pan;
1616 new_tilt = pdev->tilt_angle + angles->tilt;
1618 /* check absolute ranges */
1619 if (new_pan < pdev->angle_range.pan_min ||
1620 new_pan > pdev->angle_range.pan_max ||
1621 new_tilt < pdev->angle_range.tilt_min ||
1622 new_tilt > pdev->angle_range.tilt_max)
1628 /* go to relative range, check again */
1629 new_pan -= pdev->pan_angle;
1630 new_tilt -= pdev->tilt_angle;
1631 /* angles are specified in degrees * 100, thus the limit = 36000 */
1632 if (new_pan < -36000 || new_pan > 36000 || new_tilt < -36000 || new_tilt > 36000)
1635 if (ret == 0) /* no errors so far */
1637 ret = pwc_mpt_set_angle(pdev, new_pan, new_tilt);
1640 pdev->pan_angle += new_pan;
1641 pdev->tilt_angle += new_tilt;
1643 if (ret == -EPIPE) /* stall -> out of range */
1654 case VIDIOCPWCMPTGANGLE:
1656 struct pwc_mpt_angles *angles = arg;
1658 if (pdev->features & FEATURE_MOTOR_PANTILT)
1660 angles->absolute = 1;
1661 angles->pan = pdev->pan_angle;
1662 angles->tilt = pdev->tilt_angle;
1671 case VIDIOCPWCMPTSTATUS:
1673 struct pwc_mpt_status *status = arg;
1675 if (pdev->features & FEATURE_MOTOR_PANTILT)
1677 ret = pwc_mpt_get_status(pdev, status);