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>
44 #include <linux/version.h>
47 #include "pwc-ioctl.h"
48 #include "pwc-uncompress.h"
49 #include "pwc-kiara.h"
50 #include "pwc-timon.h"
52 #include "pwc-dec23.h"
54 /* Request types: video */
55 #define SET_LUM_CTL 0x01
56 #define GET_LUM_CTL 0x02
57 #define SET_CHROM_CTL 0x03
58 #define GET_CHROM_CTL 0x04
59 #define SET_STATUS_CTL 0x05
60 #define GET_STATUS_CTL 0x06
61 #define SET_EP_STREAM_CTL 0x07
62 #define GET_EP_STREAM_CTL 0x08
63 #define SET_MPT_CTL 0x0D
64 #define GET_MPT_CTL 0x0E
66 /* Selectors for the Luminance controls [GS]ET_LUM_CTL */
67 #define AGC_MODE_FORMATTER 0x2000
68 #define PRESET_AGC_FORMATTER 0x2100
69 #define SHUTTER_MODE_FORMATTER 0x2200
70 #define PRESET_SHUTTER_FORMATTER 0x2300
71 #define PRESET_CONTOUR_FORMATTER 0x2400
72 #define AUTO_CONTOUR_FORMATTER 0x2500
73 #define BACK_LIGHT_COMPENSATION_FORMATTER 0x2600
74 #define CONTRAST_FORMATTER 0x2700
75 #define DYNAMIC_NOISE_CONTROL_FORMATTER 0x2800
76 #define FLICKERLESS_MODE_FORMATTER 0x2900
77 #define AE_CONTROL_SPEED 0x2A00
78 #define BRIGHTNESS_FORMATTER 0x2B00
79 #define GAMMA_FORMATTER 0x2C00
81 /* Selectors for the Chrominance controls [GS]ET_CHROM_CTL */
82 #define WB_MODE_FORMATTER 0x1000
83 #define AWB_CONTROL_SPEED_FORMATTER 0x1100
84 #define AWB_CONTROL_DELAY_FORMATTER 0x1200
85 #define PRESET_MANUAL_RED_GAIN_FORMATTER 0x1300
86 #define PRESET_MANUAL_BLUE_GAIN_FORMATTER 0x1400
87 #define COLOUR_MODE_FORMATTER 0x1500
88 #define SATURATION_MODE_FORMATTER1 0x1600
89 #define SATURATION_MODE_FORMATTER2 0x1700
91 /* Selectors for the Status controls [GS]ET_STATUS_CTL */
92 #define SAVE_USER_DEFAULTS_FORMATTER 0x0200
93 #define RESTORE_USER_DEFAULTS_FORMATTER 0x0300
94 #define RESTORE_FACTORY_DEFAULTS_FORMATTER 0x0400
95 #define READ_AGC_FORMATTER 0x0500
96 #define READ_SHUTTER_FORMATTER 0x0600
97 #define READ_RED_GAIN_FORMATTER 0x0700
98 #define READ_BLUE_GAIN_FORMATTER 0x0800
99 #define SENSOR_TYPE_FORMATTER1 0x0C00
100 #define READ_RAW_Y_MEAN_FORMATTER 0x3100
101 #define SET_POWER_SAVE_MODE_FORMATTER 0x3200
102 #define MIRROR_IMAGE_FORMATTER 0x3300
103 #define LED_FORMATTER 0x3400
104 #define SENSOR_TYPE_FORMATTER2 0x3700
106 /* Formatters for the Video Endpoint controls [GS]ET_EP_STREAM_CTL */
107 #define VIDEO_OUTPUT_CONTROL_FORMATTER 0x0100
109 /* Formatters for the motorized pan & tilt [GS]ET_MPT_CTL */
110 #define PT_RELATIVE_CONTROL_FORMATTER 0x01
111 #define PT_RESET_CONTROL_FORMATTER 0x02
112 #define PT_STATUS_FORMATTER 0x03
114 static char *size2name
[PSZ_MAX
] =
126 /* Entries for the Nala (645/646) camera; the Nala doesn't have compression
127 preferences, so you either get compressed or non-compressed streams.
129 An alternate value of 0 means this mode is not available at all.
132 struct Nala_table_entry
{
133 char alternate
; /* USB alternate setting */
134 int compressed
; /* Compressed yes/no */
136 unsigned char mode
[3]; /* precomputed mode table */
139 static struct Nala_table_entry Nala_table
[PSZ_MAX
][8] =
141 #include "pwc-nala.h"
145 /****************************************************************************/
148 #define SendControlMsg(request, value, buflen) \
149 usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0), \
151 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, \
156 #define RecvControlMsg(request, value, buflen) \
157 usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0), \
159 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, \
166 void pwc_hexdump(void *p
, int len
)
172 s
= (unsigned char *)p
;
175 Debug("Doing hexdump @ %p, %d bytes.\n", p
, len
);
176 for (i
= 0; i
< len
; i
++) {
177 d
+= sprintf(d
, "%02X ", *s
++);
178 if ((i
& 0xF) == 0xF) {
189 static inline int send_video_command(struct usb_device
*udev
, int index
, void *buf
, int buflen
)
191 return usb_control_msg(udev
,
192 usb_sndctrlpipe(udev
, 0),
194 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
195 VIDEO_OUTPUT_CONTROL_FORMATTER
,
202 static inline int set_video_mode_Nala(struct pwc_device
*pdev
, int size
, int frames
)
204 unsigned char buf
[3];
206 struct Nala_table_entry
*pEntry
;
207 int frames2frames
[31] =
208 { /* closest match of framerate */
209 0, 0, 0, 0, 4, /* 0-4 */
210 5, 5, 7, 7, 10, /* 5-9 */
211 10, 10, 12, 12, 15, /* 10-14 */
212 15, 15, 15, 20, 20, /* 15-19 */
213 20, 20, 20, 24, 24, /* 20-24 */
214 24, 24, 24, 24, 24, /* 25-29 */
217 int frames2table
[31] =
218 { 0, 0, 0, 0, 0, /* 0-4 */
219 1, 1, 1, 2, 2, /* 5-9 */
220 3, 3, 4, 4, 4, /* 10-14 */
221 5, 5, 5, 5, 5, /* 15-19 */
222 6, 6, 6, 6, 7, /* 20-24 */
223 7, 7, 7, 7, 7, /* 25-29 */
227 if (size
< 0 || size
> PSZ_CIF
|| frames
< 4 || frames
> 25)
229 frames
= frames2frames
[frames
];
230 fps
= frames2table
[frames
];
231 pEntry
= &Nala_table
[size
][fps
];
232 if (pEntry
->alternate
== 0)
235 if (pEntry
->compressed
)
236 return -ENOENT
; /* Not supported. */
238 memcpy(buf
, pEntry
->mode
, 3);
239 ret
= send_video_command(pdev
->udev
, pdev
->vendpoint
, buf
, 3);
241 Debug("Failed to send video command... %d\n", ret
);
244 if (pEntry
->compressed
&& pdev
->vpalette
!= VIDEO_PALETTE_RAW
)
249 pwc_dec1_init(pdev
->type
, pdev
->release
, buf
, pdev
->decompress_data
);
259 pwc_dec23_init(pdev
->type
, pdev
->release
, buf
, pdev
->decompress_data
);
265 memcpy(pdev
->cmd_buf
, buf
, 3);
267 /* Set various parameters */
268 pdev
->vframes
= frames
;
270 pdev
->valternate
= pEntry
->alternate
;
271 pdev
->image
= pwc_image_sizes
[size
];
272 pdev
->frame_size
= (pdev
->image
.x
* pdev
->image
.y
* 3) / 2;
273 if (pEntry
->compressed
) {
274 if (pdev
->release
< 5) { /* 4 fold compression */
275 pdev
->vbandlength
= 528;
276 pdev
->frame_size
/= 4;
279 pdev
->vbandlength
= 704;
280 pdev
->frame_size
/= 3;
284 pdev
->vbandlength
= 0;
289 static inline int set_video_mode_Timon(struct pwc_device
*pdev
, int size
, int frames
, int compression
, int snapshot
)
291 unsigned char buf
[13];
292 const struct Timon_table_entry
*pChoose
;
295 if (size
>= PSZ_MAX
|| frames
< 5 || frames
> 30 || compression
< 0 || compression
> 3)
297 if (size
== PSZ_VGA
&& frames
> 15)
299 fps
= (frames
/ 5) - 1;
301 /* Find a supported framerate with progressively higher compression ratios
302 if the preferred ratio is not available.
305 while (compression
<= 3) {
306 pChoose
= &Timon_table
[size
][fps
][compression
];
307 if (pChoose
->alternate
!= 0)
311 if (pChoose
== NULL
|| pChoose
->alternate
== 0)
312 return -ENOENT
; /* Not supported. */
314 memcpy(buf
, pChoose
->mode
, 13);
317 ret
= send_video_command(pdev
->udev
, pdev
->vendpoint
, buf
, 13);
321 if (pChoose
->bandlength
> 0 && pdev
->vpalette
!= VIDEO_PALETTE_RAW
)
322 pwc_dec23_init(pdev
->type
, pdev
->release
, buf
, pdev
->decompress_data
);
325 memcpy(pdev
->cmd_buf
, buf
, 13);
327 /* Set various parameters */
328 pdev
->vframes
= frames
;
330 pdev
->vsnapshot
= snapshot
;
331 pdev
->valternate
= pChoose
->alternate
;
332 pdev
->image
= pwc_image_sizes
[size
];
333 pdev
->vbandlength
= pChoose
->bandlength
;
334 if (pChoose
->bandlength
> 0)
335 pdev
->frame_size
= (pChoose
->bandlength
* pdev
->image
.y
) / 4;
337 pdev
->frame_size
= (pdev
->image
.x
* pdev
->image
.y
* 12) / 8;
342 static inline int set_video_mode_Kiara(struct pwc_device
*pdev
, int size
, int frames
, int compression
, int snapshot
)
344 const struct Kiara_table_entry
*pChoose
= NULL
;
346 unsigned char buf
[12];
347 struct Kiara_table_entry RawEntry
= {6, 773, 1272, {0xAD, 0xF4, 0x10, 0x27, 0xB6, 0x24, 0x96, 0x02, 0x30, 0x05, 0x03, 0x80}};
349 if (size
>= PSZ_MAX
|| frames
< 5 || frames
> 30 || compression
< 0 || compression
> 3)
351 if (size
== PSZ_VGA
&& frames
> 15)
353 fps
= (frames
/ 5) - 1;
355 /* special case: VGA @ 5 fps and snapshot is raw bayer mode */
356 if (size
== PSZ_VGA
&& frames
== 5 && snapshot
)
358 /* Only available in case the raw palette is selected or
359 we have the decompressor available. This mode is
360 only available in compressed form
362 if (pdev
->vpalette
== VIDEO_PALETTE_RAW
)
364 Info("Choosing VGA/5 BAYER mode (%d).\n", pdev
->vpalette
);
369 Info("VGA/5 BAYER mode _must_ have a decompressor available, or use RAW palette.\n");
374 /* Find a supported framerate with progressively higher compression ratios
375 if the preferred ratio is not available.
376 Skip this step when using RAW modes.
378 while (compression
<= 3) {
379 pChoose
= &Kiara_table
[size
][fps
][compression
];
380 if (pChoose
->alternate
!= 0)
385 if (pChoose
== NULL
|| pChoose
->alternate
== 0)
386 return -ENOENT
; /* Not supported. */
388 Debug("Using alternate setting %d.\n", pChoose
->alternate
);
390 /* usb_control_msg won't take staticly allocated arrays as argument?? */
391 memcpy(buf
, pChoose
->mode
, 12);
395 /* Firmware bug: video endpoint is 5, but commands are sent to endpoint 4 */
396 ret
= send_video_command(pdev
->udev
, 4 /* pdev->vendpoint */, buf
, 12);
400 if (pChoose
->bandlength
> 0 && pdev
->vpalette
!= VIDEO_PALETTE_RAW
)
401 pwc_dec23_init(pdev
->type
, pdev
->release
, buf
, pdev
->decompress_data
);
404 memcpy(pdev
->cmd_buf
, buf
, 12);
406 pdev
->vframes
= frames
;
408 pdev
->vsnapshot
= snapshot
;
409 pdev
->valternate
= pChoose
->alternate
;
410 pdev
->image
= pwc_image_sizes
[size
];
411 pdev
->vbandlength
= pChoose
->bandlength
;
412 if (pdev
->vbandlength
> 0)
413 pdev
->frame_size
= (pdev
->vbandlength
* pdev
->image
.y
) / 4;
415 pdev
->frame_size
= (pdev
->image
.x
* pdev
->image
.y
* 12) / 8;
421 static void pwc_set_image_buffer_size(struct pwc_device
*pdev
)
423 int i
, factor
= 0, filler
= 0;
425 /* for PALETTE_YUV420P */
426 switch(pdev
->vpalette
)
428 case VIDEO_PALETTE_YUV420P
:
432 case VIDEO_PALETTE_RAW
:
433 factor
= 6; /* can be uncompressed YUV420P */
438 /* Set sizes in bytes */
439 pdev
->image
.size
= pdev
->image
.x
* pdev
->image
.y
* factor
/ 4;
440 pdev
->view
.size
= pdev
->view
.x
* pdev
->view
.y
* factor
/ 4;
442 /* Align offset, or you'll get some very weird results in
443 YUV420 mode... x must be multiple of 4 (to get the Y's in
444 place), and y even (or you'll mixup U & V). This is less of a
447 pdev
->offset
.x
= ((pdev
->view
.x
- pdev
->image
.x
) / 2) & 0xFFFC;
448 pdev
->offset
.y
= ((pdev
->view
.y
- pdev
->image
.y
) / 2) & 0xFFFE;
450 /* Fill buffers with gray or black */
451 for (i
= 0; i
< MAX_IMAGES
; i
++) {
452 if (pdev
->image_ptr
[i
] != NULL
)
453 memset(pdev
->image_ptr
[i
], filler
, pdev
->view
.size
);
460 @pdev: device structure
461 @width: viewport width
462 @height: viewport height
463 @frame: framerate, in fps
464 @compression: preferred compression ratio
465 @snapshot: snapshot mode or streaming
467 int pwc_set_video_mode(struct pwc_device
*pdev
, int width
, int height
, int frames
, int compression
, int snapshot
)
471 Trace(TRACE_FLOW
, "set_video_mode(%dx%d @ %d, palette %d).\n", width
, height
, frames
, pdev
->vpalette
);
472 size
= pwc_decode_size(pdev
, width
, height
);
474 Debug("Could not find suitable size.\n");
477 Debug("decode_size = %d.\n", size
);
483 ret
= set_video_mode_Nala(pdev
, size
, frames
);
489 ret
= set_video_mode_Timon(pdev
, size
, frames
, compression
, snapshot
);
496 ret
= set_video_mode_Kiara(pdev
, size
, frames
, compression
, snapshot
);
501 Info("Video mode %s@%d fps is only supported with the decompressor module (pwcx).\n", size2name
[size
], frames
);
503 Err("Failed to set video mode %s@%d fps; return code = %d\n", size2name
[size
], frames
, ret
);
507 pdev
->view
.x
= width
;
508 pdev
->view
.y
= height
;
509 pdev
->frame_total_size
= pdev
->frame_size
+ pdev
->frame_header_size
+ pdev
->frame_trailer_size
;
510 pwc_set_image_buffer_size(pdev
);
511 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
);
518 int pwc_get_brightness(struct pwc_device
*pdev
)
523 ret
= RecvControlMsg(GET_LUM_CTL
, BRIGHTNESS_FORMATTER
, 1);
529 int pwc_set_brightness(struct pwc_device
*pdev
, int value
)
537 buf
= (value
>> 9) & 0x7f;
538 return SendControlMsg(SET_LUM_CTL
, BRIGHTNESS_FORMATTER
, 1);
543 int pwc_get_contrast(struct pwc_device
*pdev
)
548 ret
= RecvControlMsg(GET_LUM_CTL
, CONTRAST_FORMATTER
, 1);
554 int pwc_set_contrast(struct pwc_device
*pdev
, int value
)
562 buf
= (value
>> 10) & 0x3f;
563 return SendControlMsg(SET_LUM_CTL
, CONTRAST_FORMATTER
, 1);
568 int pwc_get_gamma(struct pwc_device
*pdev
)
573 ret
= RecvControlMsg(GET_LUM_CTL
, GAMMA_FORMATTER
, 1);
579 int pwc_set_gamma(struct pwc_device
*pdev
, int value
)
587 buf
= (value
>> 11) & 0x1f;
588 return SendControlMsg(SET_LUM_CTL
, GAMMA_FORMATTER
, 1);
594 int pwc_get_saturation(struct pwc_device
*pdev
)
599 if (pdev
->type
< 675)
601 ret
= RecvControlMsg(GET_CHROM_CTL
, pdev
->type
< 730 ? SATURATION_MODE_FORMATTER2
: SATURATION_MODE_FORMATTER1
, 1);
604 return 32768 + buf
* 327;
607 int pwc_set_saturation(struct pwc_device
*pdev
, int value
)
611 if (pdev
->type
< 675)
617 /* saturation ranges from -100 to +100 */
618 buf
= (value
- 32768) / 327;
619 return SendControlMsg(SET_CHROM_CTL
, pdev
->type
< 730 ? SATURATION_MODE_FORMATTER2
: SATURATION_MODE_FORMATTER1
, 1);
624 static inline int pwc_set_agc(struct pwc_device
*pdev
, int mode
, int value
)
630 buf
= 0x0; /* auto */
632 buf
= 0xff; /* fixed */
634 ret
= SendControlMsg(SET_LUM_CTL
, AGC_MODE_FORMATTER
, 1);
636 if (!mode
&& ret
>= 0) {
641 buf
= (value
>> 10) & 0x3F;
642 ret
= SendControlMsg(SET_LUM_CTL
, PRESET_AGC_FORMATTER
, 1);
649 static inline int pwc_get_agc(struct pwc_device
*pdev
, int *value
)
654 ret
= RecvControlMsg(GET_LUM_CTL
, AGC_MODE_FORMATTER
, 1);
658 if (buf
!= 0) { /* fixed */
659 ret
= RecvControlMsg(GET_LUM_CTL
, PRESET_AGC_FORMATTER
, 1);
664 *value
= (buf
<< 10);
667 ret
= RecvControlMsg(GET_STATUS_CTL
, READ_AGC_FORMATTER
, 1);
670 /* Gah... this value ranges from 0x00 ... 0x9F */
673 *value
= -(48 + buf
* 409);
679 static inline int pwc_set_shutter_speed(struct pwc_device
*pdev
, int mode
, int value
)
686 buf
[0] = 0x0; /* auto */
688 buf
[0] = 0xff; /* fixed */
690 ret
= SendControlMsg(SET_LUM_CTL
, SHUTTER_MODE_FORMATTER
, 1);
692 if (!mode
&& ret
>= 0) {
701 /* speed ranges from 0x0 to 0x290 (656) */
702 speed
= (value
/ 100);
704 buf
[0] = speed
& 0xff;
710 /* speed seems to range from 0x0 to 0xff */
716 ret
= SendControlMsg(SET_LUM_CTL
, PRESET_SHUTTER_FORMATTER
, 2);
724 int pwc_camera_power(struct pwc_device
*pdev
, int power
)
728 if (pdev
->type
< 675 || (pdev
->type
< 730 && pdev
->release
< 6))
729 return 0; /* Not supported by Nala or Timon < release 6 */
732 buf
= 0x00; /* active */
734 buf
= 0xFF; /* power save */
735 return SendControlMsg(SET_STATUS_CTL
, SET_POWER_SAVE_MODE_FORMATTER
, 1);
742 static inline int pwc_restore_user(struct pwc_device
*pdev
)
744 char buf
; /* dummy */
745 return SendControlMsg(SET_STATUS_CTL
, RESTORE_USER_DEFAULTS_FORMATTER
, 0);
748 static inline int pwc_save_user(struct pwc_device
*pdev
)
750 char buf
; /* dummy */
751 return SendControlMsg(SET_STATUS_CTL
, SAVE_USER_DEFAULTS_FORMATTER
, 0);
754 static inline int pwc_restore_factory(struct pwc_device
*pdev
)
756 char buf
; /* dummy */
757 return SendControlMsg(SET_STATUS_CTL
, RESTORE_FACTORY_DEFAULTS_FORMATTER
, 0);
760 /* ************************************************* */
761 /* Patch by Alvarado: (not in the original version */
764 * the camera recognizes modes from 0 to 4:
766 * 00: indoor (incandescant lighting)
767 * 01: outdoor (sunlight)
768 * 02: fluorescent lighting
772 static inline int pwc_set_awb(struct pwc_device
*pdev
, int mode
)
783 buf
= mode
& 0x07; /* just the lowest three bits */
785 ret
= SendControlMsg(SET_CHROM_CTL
, WB_MODE_FORMATTER
, 1);
792 static inline int pwc_get_awb(struct pwc_device
*pdev
)
797 ret
= RecvControlMsg(GET_CHROM_CTL
, WB_MODE_FORMATTER
, 1);
804 static inline int pwc_set_red_gain(struct pwc_device
*pdev
, int value
)
812 /* only the msb is considered */
814 return SendControlMsg(SET_CHROM_CTL
, PRESET_MANUAL_RED_GAIN_FORMATTER
, 1);
817 static inline int pwc_get_red_gain(struct pwc_device
*pdev
, int *value
)
822 ret
= RecvControlMsg(GET_CHROM_CTL
, PRESET_MANUAL_RED_GAIN_FORMATTER
, 1);
830 static inline int pwc_set_blue_gain(struct pwc_device
*pdev
, int value
)
838 /* only the msb is considered */
840 return SendControlMsg(SET_CHROM_CTL
, PRESET_MANUAL_BLUE_GAIN_FORMATTER
, 1);
843 static inline int pwc_get_blue_gain(struct pwc_device
*pdev
, int *value
)
848 ret
= RecvControlMsg(GET_CHROM_CTL
, PRESET_MANUAL_BLUE_GAIN_FORMATTER
, 1);
856 /* The following two functions are different, since they only read the
857 internal red/blue gains, which may be different from the manual
858 gains set or read above.
860 static inline int pwc_read_red_gain(struct pwc_device
*pdev
, int *value
)
865 ret
= RecvControlMsg(GET_STATUS_CTL
, READ_RED_GAIN_FORMATTER
, 1);
872 static inline int pwc_read_blue_gain(struct pwc_device
*pdev
, int *value
)
877 ret
= RecvControlMsg(GET_STATUS_CTL
, READ_BLUE_GAIN_FORMATTER
, 1);
885 static inline int pwc_set_wb_speed(struct pwc_device
*pdev
, int speed
)
889 /* useful range is 0x01..0x20 */
891 return SendControlMsg(SET_CHROM_CTL
, AWB_CONTROL_SPEED_FORMATTER
, 1);
894 static inline int pwc_get_wb_speed(struct pwc_device
*pdev
, int *value
)
899 ret
= RecvControlMsg(GET_CHROM_CTL
, AWB_CONTROL_SPEED_FORMATTER
, 1);
902 *value
= buf
* 0x7f0;
907 static inline int pwc_set_wb_delay(struct pwc_device
*pdev
, int delay
)
911 /* useful range is 0x01..0x3F */
913 return SendControlMsg(SET_CHROM_CTL
, AWB_CONTROL_DELAY_FORMATTER
, 1);
916 static inline int pwc_get_wb_delay(struct pwc_device
*pdev
, int *value
)
921 ret
= RecvControlMsg(GET_CHROM_CTL
, AWB_CONTROL_DELAY_FORMATTER
, 1);
929 int pwc_set_leds(struct pwc_device
*pdev
, int on_value
, int off_value
)
931 unsigned char buf
[2];
933 if (pdev
->type
< 730)
943 if (off_value
> 0xff)
949 return SendControlMsg(SET_STATUS_CTL
, LED_FORMATTER
, 2);
952 static int pwc_get_leds(struct pwc_device
*pdev
, int *on_value
, int *off_value
)
954 unsigned char buf
[2];
957 if (pdev
->type
< 730) {
963 ret
= RecvControlMsg(GET_STATUS_CTL
, LED_FORMATTER
, 2);
966 *on_value
= buf
[0] * 100;
967 *off_value
= buf
[1] * 100;
971 static inline int pwc_set_contour(struct pwc_device
*pdev
, int contour
)
977 buf
= 0xff; /* auto contour on */
979 buf
= 0x0; /* auto contour off */
980 ret
= SendControlMsg(SET_LUM_CTL
, AUTO_CONTOUR_FORMATTER
, 1);
986 if (contour
> 0xffff)
989 buf
= (contour
>> 10); /* contour preset is [0..3f] */
990 ret
= SendControlMsg(SET_LUM_CTL
, PRESET_CONTOUR_FORMATTER
, 1);
996 static inline int pwc_get_contour(struct pwc_device
*pdev
, int *contour
)
1001 ret
= RecvControlMsg(GET_LUM_CTL
, AUTO_CONTOUR_FORMATTER
, 1);
1006 /* auto mode off, query current preset value */
1007 ret
= RecvControlMsg(GET_LUM_CTL
, PRESET_CONTOUR_FORMATTER
, 1);
1010 *contour
= buf
<< 10;
1018 static inline int pwc_set_backlight(struct pwc_device
*pdev
, int backlight
)
1026 return SendControlMsg(SET_LUM_CTL
, BACK_LIGHT_COMPENSATION_FORMATTER
, 1);
1029 static inline int pwc_get_backlight(struct pwc_device
*pdev
, int *backlight
)
1034 ret
= RecvControlMsg(GET_LUM_CTL
, BACK_LIGHT_COMPENSATION_FORMATTER
, 1);
1042 static inline int pwc_set_flicker(struct pwc_device
*pdev
, int flicker
)
1050 return SendControlMsg(SET_LUM_CTL
, FLICKERLESS_MODE_FORMATTER
, 1);
1053 static inline int pwc_get_flicker(struct pwc_device
*pdev
, int *flicker
)
1058 ret
= RecvControlMsg(GET_LUM_CTL
, FLICKERLESS_MODE_FORMATTER
, 1);
1066 static inline int pwc_set_dynamic_noise(struct pwc_device
*pdev
, int noise
)
1075 return SendControlMsg(SET_LUM_CTL
, DYNAMIC_NOISE_CONTROL_FORMATTER
, 1);
1078 static inline int pwc_get_dynamic_noise(struct pwc_device
*pdev
, int *noise
)
1083 ret
= RecvControlMsg(GET_LUM_CTL
, DYNAMIC_NOISE_CONTROL_FORMATTER
, 1);
1090 static int pwc_mpt_reset(struct pwc_device
*pdev
, int flags
)
1094 buf
= flags
& 0x03; // only lower two bits are currently used
1095 return SendControlMsg(SET_MPT_CTL
, PT_RESET_CONTROL_FORMATTER
, 1);
1098 static inline int pwc_mpt_set_angle(struct pwc_device
*pdev
, int pan
, int tilt
)
1100 unsigned char buf
[4];
1102 /* set new relative angle; angles are expressed in degrees * 100,
1103 but cam as .5 degree resolution, hence divide by 200. Also
1104 the angle must be multiplied by 64 before it's send to
1107 pan
= 64 * pan
/ 100;
1108 tilt
= -64 * tilt
/ 100; /* positive tilt is down, which is not what the user would expect */
1109 buf
[0] = pan
& 0xFF;
1110 buf
[1] = (pan
>> 8) & 0xFF;
1111 buf
[2] = tilt
& 0xFF;
1112 buf
[3] = (tilt
>> 8) & 0xFF;
1113 return SendControlMsg(SET_MPT_CTL
, PT_RELATIVE_CONTROL_FORMATTER
, 4);
1116 static inline int pwc_mpt_get_status(struct pwc_device
*pdev
, struct pwc_mpt_status
*status
)
1119 unsigned char buf
[5];
1121 ret
= RecvControlMsg(GET_MPT_CTL
, PT_STATUS_FORMATTER
, 5);
1124 status
->status
= buf
[0] & 0x7; // 3 bits are used for reporting
1125 status
->time_pan
= (buf
[1] << 8) + buf
[2];
1126 status
->time_tilt
= (buf
[3] << 8) + buf
[4];
1131 int pwc_get_cmos_sensor(struct pwc_device
*pdev
, int *sensor
)
1134 int ret
= -1, request
;
1136 if (pdev
->type
< 675)
1137 request
= SENSOR_TYPE_FORMATTER1
;
1138 else if (pdev
->type
< 730)
1139 return -1; /* The Vesta series doesn't have this call */
1141 request
= SENSOR_TYPE_FORMATTER2
;
1143 ret
= RecvControlMsg(GET_STATUS_CTL
, request
, 1);
1146 if (pdev
->type
< 675)
1147 *sensor
= buf
| 0x100;
1154 /* End of Add-Ons */
1155 /* ************************************************* */
1157 /* Linux 2.5.something and 2.6 pass direct pointers to arguments of
1158 ioctl() calls. With 2.4, you have to do tedious copy_from_user()
1159 and copy_to_user() calls. With these macros we circumvent this,
1160 and let me maintain only one source file. The functionality is
1161 exactly the same otherwise.
1164 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
1166 /* define local variable for arg */
1167 #define ARG_DEF(ARG_type, ARG_name)\
1168 ARG_type *ARG_name = arg;
1169 /* copy arg to local variable */
1170 #define ARG_IN(ARG_name) /* nothing */
1171 /* argument itself (referenced) */
1172 #define ARGR(ARG_name) (*ARG_name)
1173 /* argument address */
1174 #define ARGA(ARG_name) ARG_name
1175 /* copy local variable to arg */
1176 #define ARG_OUT(ARG_name) /* nothing */
1180 #define ARG_DEF(ARG_type, ARG_name)\
1182 #define ARG_IN(ARG_name)\
1183 if (copy_from_user(&ARG_name, arg, sizeof(ARG_name))) {\
1187 #define ARGR(ARG_name) ARG_name
1188 #define ARGA(ARG_name) &ARG_name
1189 #define ARG_OUT(ARG_name)\
1190 if (copy_to_user(arg, &ARG_name, sizeof(ARG_name))) {\
1197 int pwc_ioctl(struct pwc_device
*pdev
, unsigned int cmd
, void *arg
)
1202 case VIDIOCPWCRUSER
:
1204 if (pwc_restore_user(pdev
))
1209 case VIDIOCPWCSUSER
:
1211 if (pwc_save_user(pdev
))
1216 case VIDIOCPWCFACTORY
:
1218 if (pwc_restore_factory(pdev
))
1223 case VIDIOCPWCSCQUAL
:
1228 if (ARGR(qual
) < 0 || ARGR(qual
) > 3)
1231 ret
= pwc_try_video_mode(pdev
, pdev
->view
.x
, pdev
->view
.y
, pdev
->vframes
, ARGR(qual
), pdev
->vsnapshot
);
1233 pdev
->vcompression
= ARGR(qual
);
1237 case VIDIOCPWCGCQUAL
:
1241 ARGR(qual
) = pdev
->vcompression
;
1246 case VIDIOCPWCPROBE
:
1248 ARG_DEF(struct pwc_probe
, probe
)
1250 strcpy(ARGR(probe
).name
, pdev
->vdev
->name
);
1251 ARGR(probe
).type
= pdev
->type
;
1256 case VIDIOCPWCGSERIAL
:
1258 ARG_DEF(struct pwc_serial
, serial
)
1260 strcpy(ARGR(serial
).serial
, pdev
->serial
);
1270 if (pwc_set_agc(pdev
, ARGR(agc
) < 0 ? 1 : 0, ARGR(agc
)))
1279 if (pwc_get_agc(pdev
, ARGA(agc
)))
1285 case VIDIOCPWCSSHUTTER
:
1287 ARG_DEF(int, shutter_speed
)
1289 ARG_IN(shutter_speed
)
1290 ret
= pwc_set_shutter_speed(pdev
, ARGR(shutter_speed
) < 0 ? 1 : 0, ARGR(shutter_speed
));
1296 ARG_DEF(struct pwc_whitebalance
, wb
)
1299 ret
= pwc_set_awb(pdev
, ARGR(wb
).mode
);
1300 if (ret
>= 0 && ARGR(wb
).mode
== PWC_WB_MANUAL
) {
1301 pwc_set_red_gain(pdev
, ARGR(wb
).manual_red
);
1302 pwc_set_blue_gain(pdev
, ARGR(wb
).manual_blue
);
1309 ARG_DEF(struct pwc_whitebalance
, wb
)
1311 memset(ARGA(wb
), 0, sizeof(struct pwc_whitebalance
));
1312 ARGR(wb
).mode
= pwc_get_awb(pdev
);
1313 if (ARGR(wb
).mode
< 0)
1316 if (ARGR(wb
).mode
== PWC_WB_MANUAL
) {
1317 ret
= pwc_get_red_gain(pdev
, &ARGR(wb
).manual_red
);
1320 ret
= pwc_get_blue_gain(pdev
, &ARGR(wb
).manual_blue
);
1324 if (ARGR(wb
).mode
== PWC_WB_AUTO
) {
1325 ret
= pwc_read_red_gain(pdev
, &ARGR(wb
).read_red
);
1328 ret
=pwc_read_blue_gain(pdev
, &ARGR(wb
).read_blue
);
1337 case VIDIOCPWCSAWBSPEED
:
1339 ARG_DEF(struct pwc_wb_speed
, wbs
)
1341 if (ARGR(wbs
).control_speed
> 0) {
1342 ret
= pwc_set_wb_speed(pdev
, ARGR(wbs
).control_speed
);
1344 if (ARGR(wbs
).control_delay
> 0) {
1345 ret
= pwc_set_wb_delay(pdev
, ARGR(wbs
).control_delay
);
1350 case VIDIOCPWCGAWBSPEED
:
1352 ARG_DEF(struct pwc_wb_speed
, wbs
)
1354 ret
= pwc_get_wb_speed(pdev
, &ARGR(wbs
).control_speed
);
1357 ret
= pwc_get_wb_delay(pdev
, &ARGR(wbs
).control_delay
);
1366 ARG_DEF(struct pwc_leds
, leds
)
1369 ret
= pwc_set_leds(pdev
, ARGR(leds
).led_on
, ARGR(leds
).led_off
);
1376 ARG_DEF(struct pwc_leds
, leds
)
1378 ret
= pwc_get_leds(pdev
, &ARGR(leds
).led_on
, &ARGR(leds
).led_off
);
1383 case VIDIOCPWCSCONTOUR
:
1385 ARG_DEF(int, contour
)
1388 ret
= pwc_set_contour(pdev
, ARGR(contour
));
1392 case VIDIOCPWCGCONTOUR
:
1394 ARG_DEF(int, contour
)
1396 ret
= pwc_get_contour(pdev
, ARGA(contour
));
1401 case VIDIOCPWCSBACKLIGHT
:
1403 ARG_DEF(int, backlight
)
1406 ret
= pwc_set_backlight(pdev
, ARGR(backlight
));
1410 case VIDIOCPWCGBACKLIGHT
:
1412 ARG_DEF(int, backlight
)
1414 ret
= pwc_get_backlight(pdev
, ARGA(backlight
));
1419 case VIDIOCPWCSFLICKER
:
1421 ARG_DEF(int, flicker
)
1424 ret
= pwc_set_flicker(pdev
, ARGR(flicker
));
1428 case VIDIOCPWCGFLICKER
:
1430 ARG_DEF(int, flicker
)
1432 ret
= pwc_get_flicker(pdev
, ARGA(flicker
));
1437 case VIDIOCPWCSDYNNOISE
:
1439 ARG_DEF(int, dynnoise
)
1442 ret
= pwc_set_dynamic_noise(pdev
, ARGR(dynnoise
));
1446 case VIDIOCPWCGDYNNOISE
:
1448 ARG_DEF(int, dynnoise
)
1450 ret
= pwc_get_dynamic_noise(pdev
, ARGA(dynnoise
));
1455 case VIDIOCPWCGREALSIZE
:
1457 ARG_DEF(struct pwc_imagesize
, size
)
1459 ARGR(size
).width
= pdev
->image
.x
;
1460 ARGR(size
).height
= pdev
->image
.y
;
1465 case VIDIOCPWCMPTRESET
:
1467 if (pdev
->features
& FEATURE_MOTOR_PANTILT
)
1472 ret
= pwc_mpt_reset(pdev
, ARGR(flags
));
1475 pdev
->pan_angle
= 0;
1476 pdev
->tilt_angle
= 0;
1486 case VIDIOCPWCMPTGRANGE
:
1488 if (pdev
->features
& FEATURE_MOTOR_PANTILT
)
1490 ARG_DEF(struct pwc_mpt_range
, range
)
1492 ARGR(range
) = pdev
->angle_range
;
1502 case VIDIOCPWCMPTSANGLE
:
1504 int new_pan
, new_tilt
;
1506 if (pdev
->features
& FEATURE_MOTOR_PANTILT
)
1508 ARG_DEF(struct pwc_mpt_angles
, angles
)
1511 /* The camera can only set relative angles, so
1512 do some calculations when getting an absolute angle .
1514 if (ARGR(angles
).absolute
)
1516 new_pan
= ARGR(angles
).pan
;
1517 new_tilt
= ARGR(angles
).tilt
;
1521 new_pan
= pdev
->pan_angle
+ ARGR(angles
).pan
;
1522 new_tilt
= pdev
->tilt_angle
+ ARGR(angles
).tilt
;
1524 /* check absolute ranges */
1525 if (new_pan
< pdev
->angle_range
.pan_min
||
1526 new_pan
> pdev
->angle_range
.pan_max
||
1527 new_tilt
< pdev
->angle_range
.tilt_min
||
1528 new_tilt
> pdev
->angle_range
.tilt_max
)
1534 /* go to relative range, check again */
1535 new_pan
-= pdev
->pan_angle
;
1536 new_tilt
-= pdev
->tilt_angle
;
1537 /* angles are specified in degrees * 100, thus the limit = 36000 */
1538 if (new_pan
< -36000 || new_pan
> 36000 || new_tilt
< -36000 || new_tilt
> 36000)
1541 if (ret
== 0) /* no errors so far */
1543 ret
= pwc_mpt_set_angle(pdev
, new_pan
, new_tilt
);
1546 pdev
->pan_angle
+= new_pan
;
1547 pdev
->tilt_angle
+= new_tilt
;
1549 if (ret
== -EPIPE
) /* stall -> out of range */
1560 case VIDIOCPWCMPTGANGLE
:
1563 if (pdev
->features
& FEATURE_MOTOR_PANTILT
)
1565 ARG_DEF(struct pwc_mpt_angles
, angles
)
1567 ARGR(angles
).absolute
= 1;
1568 ARGR(angles
).pan
= pdev
->pan_angle
;
1569 ARGR(angles
).tilt
= pdev
->tilt_angle
;
1579 case VIDIOCPWCMPTSTATUS
:
1581 if (pdev
->features
& FEATURE_MOTOR_PANTILT
)
1583 ARG_DEF(struct pwc_mpt_status
, status
)
1585 ret
= pwc_mpt_get_status(pdev
, ARGA(status
));
1595 case VIDIOCPWCGVIDCMD
:
1597 ARG_DEF(struct pwc_video_command
, cmd
);
1599 ARGR(cmd
).type
= pdev
->type
;
1600 ARGR(cmd
).release
= pdev
->release
;
1601 ARGR(cmd
).command_len
= pdev
->cmd_len
;
1602 memcpy(&ARGR(cmd
).command_buf
, pdev
->cmd_buf
, pdev
->cmd_len
);
1603 ARGR(cmd
).bandlength
= pdev
->vbandlength
;
1604 ARGR(cmd
).frame_size
= pdev
->frame_size
;
1609 case VIDIOCPWCGVIDTABLE:
1611 ARG_DEF(struct pwc_table_init_buffer, table);
1612 ARGR(table).len = pdev->cmd_len;
1613 memcpy(&ARGR(table).buffer, pdev->decompress_data, pdev->decompressor->table_size);