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-2006 Luc Saillard (luc@saillard.org)
7 NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
8 driver and thus may have bugs that are not present in the original version.
9 Please send bug reports and support requests to <luc@saillard.org>.
11 NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
12 driver and thus may have bugs that are not present in the original version.
13 Please send bug reports and support requests to <luc@saillard.org>.
14 The decompression routines have been implemented by reverse-engineering the
15 Nemosoft binary pwcx module. Caveat emptor.
17 This program is free software; you can redistribute it and/or modify
18 it under the terms of the GNU General Public License as published by
19 the Free Software Foundation; either version 2 of the License, or
20 (at your option) any later version.
22 This program is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 2001/08/03 Alvarado Added methods for changing white balance and
38 /* Control functions for the cam; brightness, contrast, video mode, etc. */
41 #include <asm/uaccess.h>
43 #include <asm/errno.h>
46 #include "pwc-uncompress.h"
47 #include "pwc-kiara.h"
48 #include "pwc-timon.h"
50 #include "pwc-dec23.h"
52 /* Request types: video */
53 #define SET_LUM_CTL 0x01
54 #define GET_LUM_CTL 0x02
55 #define SET_CHROM_CTL 0x03
56 #define GET_CHROM_CTL 0x04
57 #define SET_STATUS_CTL 0x05
58 #define GET_STATUS_CTL 0x06
59 #define SET_EP_STREAM_CTL 0x07
60 #define GET_EP_STREAM_CTL 0x08
61 #define GET_XX_CTL 0x09
62 #define SET_XX_CTL 0x0A
63 #define GET_XY_CTL 0x0B
64 #define SET_XY_CTL 0x0C
65 #define SET_MPT_CTL 0x0D
66 #define GET_MPT_CTL 0x0E
68 /* Selectors for the Luminance controls [GS]ET_LUM_CTL */
69 #define AGC_MODE_FORMATTER 0x2000
70 #define PRESET_AGC_FORMATTER 0x2100
71 #define SHUTTER_MODE_FORMATTER 0x2200
72 #define PRESET_SHUTTER_FORMATTER 0x2300
73 #define PRESET_CONTOUR_FORMATTER 0x2400
74 #define AUTO_CONTOUR_FORMATTER 0x2500
75 #define BACK_LIGHT_COMPENSATION_FORMATTER 0x2600
76 #define CONTRAST_FORMATTER 0x2700
77 #define DYNAMIC_NOISE_CONTROL_FORMATTER 0x2800
78 #define FLICKERLESS_MODE_FORMATTER 0x2900
79 #define AE_CONTROL_SPEED 0x2A00
80 #define BRIGHTNESS_FORMATTER 0x2B00
81 #define GAMMA_FORMATTER 0x2C00
83 /* Selectors for the Chrominance controls [GS]ET_CHROM_CTL */
84 #define WB_MODE_FORMATTER 0x1000
85 #define AWB_CONTROL_SPEED_FORMATTER 0x1100
86 #define AWB_CONTROL_DELAY_FORMATTER 0x1200
87 #define PRESET_MANUAL_RED_GAIN_FORMATTER 0x1300
88 #define PRESET_MANUAL_BLUE_GAIN_FORMATTER 0x1400
89 #define COLOUR_MODE_FORMATTER 0x1500
90 #define SATURATION_MODE_FORMATTER1 0x1600
91 #define SATURATION_MODE_FORMATTER2 0x1700
93 /* Selectors for the Status controls [GS]ET_STATUS_CTL */
94 #define SAVE_USER_DEFAULTS_FORMATTER 0x0200
95 #define RESTORE_USER_DEFAULTS_FORMATTER 0x0300
96 #define RESTORE_FACTORY_DEFAULTS_FORMATTER 0x0400
97 #define READ_AGC_FORMATTER 0x0500
98 #define READ_SHUTTER_FORMATTER 0x0600
99 #define READ_RED_GAIN_FORMATTER 0x0700
100 #define READ_BLUE_GAIN_FORMATTER 0x0800
101 #define GET_STATUS_B00 0x0B00
102 #define SENSOR_TYPE_FORMATTER1 0x0C00
103 #define GET_STATUS_3000 0x3000
104 #define READ_RAW_Y_MEAN_FORMATTER 0x3100
105 #define SET_POWER_SAVE_MODE_FORMATTER 0x3200
106 #define MIRROR_IMAGE_FORMATTER 0x3300
107 #define LED_FORMATTER 0x3400
108 #define LOWLIGHT 0x3500
109 #define GET_STATUS_3600 0x3600
110 #define SENSOR_TYPE_FORMATTER2 0x3700
111 #define GET_STATUS_3800 0x3800
112 #define GET_STATUS_4000 0x4000
113 #define GET_STATUS_4100 0x4100 /* Get */
114 #define CTL_STATUS_4200 0x4200 /* [GS] 1 */
116 /* Formatters for the Video Endpoint controls [GS]ET_EP_STREAM_CTL */
117 #define VIDEO_OUTPUT_CONTROL_FORMATTER 0x0100
119 /* Formatters for the motorized pan & tilt [GS]ET_MPT_CTL */
120 #define PT_RELATIVE_CONTROL_FORMATTER 0x01
121 #define PT_RESET_CONTROL_FORMATTER 0x02
122 #define PT_STATUS_FORMATTER 0x03
124 static const char *size2name
[PSZ_MAX
] =
136 /* Entries for the Nala (645/646) camera; the Nala doesn't have compression
137 preferences, so you either get compressed or non-compressed streams.
139 An alternate value of 0 means this mode is not available at all.
142 #define PWC_FPS_MAX_NALA 8
144 struct Nala_table_entry
{
145 char alternate
; /* USB alternate setting */
146 int compressed
; /* Compressed yes/no */
148 unsigned char mode
[3]; /* precomputed mode table */
151 static unsigned int Nala_fps_vector
[PWC_FPS_MAX_NALA
] = { 4, 5, 7, 10, 12, 15, 20, 24 };
153 static struct Nala_table_entry Nala_table
[PSZ_MAX
][PWC_FPS_MAX_NALA
] =
155 #include "pwc-nala.h"
158 static void pwc_set_image_buffer_size(struct pwc_device
*pdev
);
160 /****************************************************************************/
162 static int _send_control_msg(struct pwc_device
*pdev
,
163 u8 request
, u16 value
, int index
, void *buf
, int buflen
, int timeout
)
169 kbuf
= kmalloc(buflen
, GFP_KERNEL
); /* not allowed on stack */
172 memcpy(kbuf
, buf
, buflen
);
175 rc
= usb_control_msg(pdev
->udev
, usb_sndctrlpipe(pdev
->udev
, 0),
177 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
180 kbuf
, buflen
, timeout
);
186 static int recv_control_msg(struct pwc_device
*pdev
,
187 u8 request
, u16 value
, void *buf
, int buflen
)
190 void *kbuf
= kmalloc(buflen
, GFP_KERNEL
); /* not allowed on stack */
195 rc
= usb_control_msg(pdev
->udev
, usb_rcvctrlpipe(pdev
->udev
, 0),
197 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
201 memcpy(buf
, kbuf
, buflen
);
206 static inline int send_video_command(struct pwc_device
*pdev
,
207 int index
, void *buf
, int buflen
)
209 return _send_control_msg(pdev
,
211 VIDEO_OUTPUT_CONTROL_FORMATTER
,
216 static inline int send_control_msg(struct pwc_device
*pdev
,
217 u8 request
, u16 value
, void *buf
, int buflen
)
219 return _send_control_msg(pdev
,
220 request
, value
, pdev
->vcinterface
, buf
, buflen
, 500);
225 static int set_video_mode_Nala(struct pwc_device
*pdev
, int size
, int frames
)
227 unsigned char buf
[3];
229 struct Nala_table_entry
*pEntry
;
230 int frames2frames
[31] =
231 { /* closest match of framerate */
232 0, 0, 0, 0, 4, /* 0-4 */
233 5, 5, 7, 7, 10, /* 5-9 */
234 10, 10, 12, 12, 15, /* 10-14 */
235 15, 15, 15, 20, 20, /* 15-19 */
236 20, 20, 20, 24, 24, /* 20-24 */
237 24, 24, 24, 24, 24, /* 25-29 */
240 int frames2table
[31] =
241 { 0, 0, 0, 0, 0, /* 0-4 */
242 1, 1, 1, 2, 2, /* 5-9 */
243 3, 3, 4, 4, 4, /* 10-14 */
244 5, 5, 5, 5, 5, /* 15-19 */
245 6, 6, 6, 6, 7, /* 20-24 */
246 7, 7, 7, 7, 7, /* 25-29 */
250 if (size
< 0 || size
> PSZ_CIF
|| frames
< 4 || frames
> 25)
252 frames
= frames2frames
[frames
];
253 fps
= frames2table
[frames
];
254 pEntry
= &Nala_table
[size
][fps
];
255 if (pEntry
->alternate
== 0)
258 memcpy(buf
, pEntry
->mode
, 3);
259 ret
= send_video_command(pdev
, pdev
->vendpoint
, buf
, 3);
261 PWC_DEBUG_MODULE("Failed to send video command... %d\n", ret
);
264 if (pEntry
->compressed
&& pdev
->vpalette
!= VIDEO_PALETTE_RAW
)
265 pwc_dec1_init(pdev
->type
, pdev
->release
, buf
, pdev
->decompress_data
);
268 memcpy(pdev
->cmd_buf
, buf
, 3);
270 /* Set various parameters */
271 pdev
->vframes
= frames
;
273 pdev
->valternate
= pEntry
->alternate
;
274 pdev
->image
= pwc_image_sizes
[size
];
275 pdev
->frame_size
= (pdev
->image
.x
* pdev
->image
.y
* 3) / 2;
276 if (pEntry
->compressed
) {
277 if (pdev
->release
< 5) { /* 4 fold compression */
278 pdev
->vbandlength
= 528;
279 pdev
->frame_size
/= 4;
282 pdev
->vbandlength
= 704;
283 pdev
->frame_size
/= 3;
287 pdev
->vbandlength
= 0;
292 static int set_video_mode_Timon(struct pwc_device
*pdev
, int size
, int frames
, int compression
, int snapshot
)
294 unsigned char buf
[13];
295 const struct Timon_table_entry
*pChoose
;
298 if (size
>= PSZ_MAX
|| frames
< 5 || frames
> 30 || compression
< 0 || compression
> 3)
300 if (size
== PSZ_VGA
&& frames
> 15)
302 fps
= (frames
/ 5) - 1;
304 /* Find a supported framerate with progressively higher compression ratios
305 if the preferred ratio is not available.
308 while (compression
<= 3) {
309 pChoose
= &Timon_table
[size
][fps
][compression
];
310 if (pChoose
->alternate
!= 0)
314 if (pChoose
== NULL
|| pChoose
->alternate
== 0)
315 return -ENOENT
; /* Not supported. */
317 memcpy(buf
, pChoose
->mode
, 13);
320 ret
= send_video_command(pdev
, pdev
->vendpoint
, buf
, 13);
324 if (pChoose
->bandlength
> 0 && pdev
->vpalette
!= VIDEO_PALETTE_RAW
)
325 pwc_dec23_init(pdev
, pdev
->type
, buf
);
328 memcpy(pdev
->cmd_buf
, buf
, 13);
330 /* Set various parameters */
331 pdev
->vframes
= frames
;
333 pdev
->vsnapshot
= snapshot
;
334 pdev
->valternate
= pChoose
->alternate
;
335 pdev
->image
= pwc_image_sizes
[size
];
336 pdev
->vbandlength
= pChoose
->bandlength
;
337 if (pChoose
->bandlength
> 0)
338 pdev
->frame_size
= (pChoose
->bandlength
* pdev
->image
.y
) / 4;
340 pdev
->frame_size
= (pdev
->image
.x
* pdev
->image
.y
* 12) / 8;
345 static int set_video_mode_Kiara(struct pwc_device
*pdev
, int size
, int frames
, int compression
, int snapshot
)
347 const struct Kiara_table_entry
*pChoose
= NULL
;
349 unsigned char buf
[12];
350 struct Kiara_table_entry RawEntry
= {6, 773, 1272, {0xAD, 0xF4, 0x10, 0x27, 0xB6, 0x24, 0x96, 0x02, 0x30, 0x05, 0x03, 0x80}};
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 /* special case: VGA @ 5 fps and snapshot is raw bayer mode */
359 if (size
== PSZ_VGA
&& frames
== 5 && snapshot
&& pdev
->vpalette
== VIDEO_PALETTE_RAW
)
361 /* Only available in case the raw palette is selected or
362 we have the decompressor available. This mode is
363 only available in compressed form
365 PWC_DEBUG_SIZE("Choosing VGA/5 BAYER mode.\n");
370 /* Find a supported framerate with progressively higher compression ratios
371 if the preferred ratio is not available.
372 Skip this step when using RAW modes.
375 while (compression
<= 3) {
376 pChoose
= &Kiara_table
[size
][fps
][compression
];
377 if (pChoose
->alternate
!= 0)
382 if (pChoose
== NULL
|| pChoose
->alternate
== 0)
383 return -ENOENT
; /* Not supported. */
385 PWC_TRACE("Using alternate setting %d.\n", pChoose
->alternate
);
387 /* usb_control_msg won't take staticly allocated arrays as argument?? */
388 memcpy(buf
, pChoose
->mode
, 12);
392 /* Firmware bug: video endpoint is 5, but commands are sent to endpoint 4 */
393 ret
= send_video_command(pdev
, 4 /* pdev->vendpoint */, buf
, 12);
397 if (pChoose
->bandlength
> 0 && pdev
->vpalette
!= VIDEO_PALETTE_RAW
)
398 pwc_dec23_init(pdev
, pdev
->type
, buf
);
401 memcpy(pdev
->cmd_buf
, buf
, 12);
403 pdev
->vframes
= frames
;
405 pdev
->vsnapshot
= snapshot
;
406 pdev
->valternate
= pChoose
->alternate
;
407 pdev
->image
= pwc_image_sizes
[size
];
408 pdev
->vbandlength
= pChoose
->bandlength
;
409 if (pdev
->vbandlength
> 0)
410 pdev
->frame_size
= (pdev
->vbandlength
* pdev
->image
.y
) / 4;
412 pdev
->frame_size
= (pdev
->image
.x
* pdev
->image
.y
* 12) / 8;
413 PWC_TRACE("frame_size=%d, vframes=%d, vsize=%d, vsnapshot=%d, vbandlength=%d\n",
414 pdev
->frame_size
,pdev
->vframes
,pdev
->vsize
,pdev
->vsnapshot
,pdev
->vbandlength
);
421 @pdev: device structure
422 @width: viewport width
423 @height: viewport height
424 @frame: framerate, in fps
425 @compression: preferred compression ratio
426 @snapshot: snapshot mode or streaming
428 int pwc_set_video_mode(struct pwc_device
*pdev
, int width
, int height
, int frames
, int compression
, int snapshot
)
432 PWC_DEBUG_FLOW("set_video_mode(%dx%d @ %d, palette %d).\n", width
, height
, frames
, pdev
->vpalette
);
433 size
= pwc_decode_size(pdev
, width
, height
);
435 PWC_DEBUG_MODULE("Could not find suitable size.\n");
438 PWC_TRACE("decode_size = %d.\n", size
);
440 if (DEVICE_USE_CODEC1(pdev
->type
)) {
441 ret
= set_video_mode_Nala(pdev
, size
, frames
);
443 } else if (DEVICE_USE_CODEC3(pdev
->type
)) {
444 ret
= set_video_mode_Kiara(pdev
, size
, frames
, compression
, snapshot
);
447 ret
= set_video_mode_Timon(pdev
, size
, frames
, compression
, snapshot
);
450 PWC_ERROR("Failed to set video mode %s@%d fps; return code = %d\n", size2name
[size
], frames
, ret
);
453 pdev
->view
.x
= width
;
454 pdev
->view
.y
= height
;
455 pdev
->frame_total_size
= pdev
->frame_size
+ pdev
->frame_header_size
+ pdev
->frame_trailer_size
;
456 pwc_set_image_buffer_size(pdev
);
457 PWC_DEBUG_SIZE("Set viewport to %dx%d, image size is %dx%d.\n", width
, height
, pwc_image_sizes
[size
].x
, pwc_image_sizes
[size
].y
);
461 static unsigned int pwc_get_fps_Nala(struct pwc_device
*pdev
, unsigned int index
, unsigned int size
)
465 for (i
= 0; i
< PWC_FPS_MAX_NALA
; i
++) {
466 if (Nala_table
[size
][i
].alternate
) {
467 if (index
--==0) return Nala_fps_vector
[i
];
473 static unsigned int pwc_get_fps_Kiara(struct pwc_device
*pdev
, unsigned int index
, unsigned int size
)
477 for (i
= 0; i
< PWC_FPS_MAX_KIARA
; i
++) {
478 if (Kiara_table
[size
][i
][3].alternate
) {
479 if (index
--==0) return Kiara_fps_vector
[i
];
485 static unsigned int pwc_get_fps_Timon(struct pwc_device
*pdev
, unsigned int index
, unsigned int size
)
489 for (i
=0; i
< PWC_FPS_MAX_TIMON
; i
++) {
490 if (Timon_table
[size
][i
][3].alternate
) {
491 if (index
--==0) return Timon_fps_vector
[i
];
497 unsigned int pwc_get_fps(struct pwc_device
*pdev
, unsigned int index
, unsigned int size
)
501 if (DEVICE_USE_CODEC1(pdev
->type
)) {
502 ret
= pwc_get_fps_Nala(pdev
, index
, size
);
504 } else if (DEVICE_USE_CODEC3(pdev
->type
)) {
505 ret
= pwc_get_fps_Kiara(pdev
, index
, size
);
508 ret
= pwc_get_fps_Timon(pdev
, index
, size
);
518 static void pwc_set_image_buffer_size(struct pwc_device
*pdev
)
522 /* for PALETTE_YUV420P */
523 switch(pdev
->vpalette
)
525 case VIDEO_PALETTE_YUV420P
:
528 case VIDEO_PALETTE_RAW
:
529 factor
= 6; /* can be uncompressed YUV420P */
533 /* Set sizes in bytes */
534 pdev
->image
.size
= pdev
->image
.x
* pdev
->image
.y
* factor
/ 4;
535 pdev
->view
.size
= pdev
->view
.x
* pdev
->view
.y
* factor
/ 4;
537 /* Align offset, or you'll get some very weird results in
538 YUV420 mode... x must be multiple of 4 (to get the Y's in
539 place), and y even (or you'll mixup U & V). This is less of a
542 pdev
->offset
.x
= ((pdev
->view
.x
- pdev
->image
.x
) / 2) & 0xFFFC;
543 pdev
->offset
.y
= ((pdev
->view
.y
- pdev
->image
.y
) / 2) & 0xFFFE;
545 /* Fill buffers with black colors */
546 for (i
= 0; i
< pwc_mbufs
; i
++) {
547 unsigned char *p
= pdev
->image_data
+ pdev
->images
[i
].offset
;
548 memset(p
, BLACK_Y
, pdev
->view
.x
* pdev
->view
.y
);
549 p
+= pdev
->view
.x
* pdev
->view
.y
;
550 memset(p
, BLACK_U
, pdev
->view
.x
* pdev
->view
.y
/4);
551 p
+= pdev
->view
.x
* pdev
->view
.y
/4;
552 memset(p
, BLACK_V
, pdev
->view
.x
* pdev
->view
.y
/4);
560 int pwc_get_brightness(struct pwc_device
*pdev
)
565 ret
= recv_control_msg(pdev
,
566 GET_LUM_CTL
, BRIGHTNESS_FORMATTER
, &buf
, sizeof(buf
));
572 int pwc_set_brightness(struct pwc_device
*pdev
, int value
)
580 buf
= (value
>> 9) & 0x7f;
581 return send_control_msg(pdev
,
582 SET_LUM_CTL
, BRIGHTNESS_FORMATTER
, &buf
, sizeof(buf
));
587 int pwc_get_contrast(struct pwc_device
*pdev
)
592 ret
= recv_control_msg(pdev
,
593 GET_LUM_CTL
, CONTRAST_FORMATTER
, &buf
, sizeof(buf
));
599 int pwc_set_contrast(struct pwc_device
*pdev
, int value
)
607 buf
= (value
>> 10) & 0x3f;
608 return send_control_msg(pdev
,
609 SET_LUM_CTL
, CONTRAST_FORMATTER
, &buf
, sizeof(buf
));
614 int pwc_get_gamma(struct pwc_device
*pdev
)
619 ret
= recv_control_msg(pdev
,
620 GET_LUM_CTL
, GAMMA_FORMATTER
, &buf
, sizeof(buf
));
626 int pwc_set_gamma(struct pwc_device
*pdev
, int value
)
634 buf
= (value
>> 11) & 0x1f;
635 return send_control_msg(pdev
,
636 SET_LUM_CTL
, GAMMA_FORMATTER
, &buf
, sizeof(buf
));
642 /* return a value between [-100 , 100] */
643 int pwc_get_saturation(struct pwc_device
*pdev
, int *value
)
646 int ret
, saturation_register
;
648 if (pdev
->type
< 675)
650 if (pdev
->type
< 730)
651 saturation_register
= SATURATION_MODE_FORMATTER2
;
653 saturation_register
= SATURATION_MODE_FORMATTER1
;
654 ret
= recv_control_msg(pdev
,
655 GET_CHROM_CTL
, saturation_register
, &buf
, sizeof(buf
));
658 *value
= (signed)buf
;
662 /* @param value saturation color between [-100 , 100] */
663 int pwc_set_saturation(struct pwc_device
*pdev
, int value
)
666 int saturation_register
;
668 if (pdev
->type
< 675)
674 if (pdev
->type
< 730)
675 saturation_register
= SATURATION_MODE_FORMATTER2
;
677 saturation_register
= SATURATION_MODE_FORMATTER1
;
678 return send_control_msg(pdev
,
679 SET_CHROM_CTL
, saturation_register
, &buf
, sizeof(buf
));
684 int pwc_set_agc(struct pwc_device
*pdev
, int mode
, int value
)
690 buf
= 0x0; /* auto */
692 buf
= 0xff; /* fixed */
694 ret
= send_control_msg(pdev
,
695 SET_LUM_CTL
, AGC_MODE_FORMATTER
, &buf
, sizeof(buf
));
697 if (!mode
&& ret
>= 0) {
702 buf
= (value
>> 10) & 0x3F;
703 ret
= send_control_msg(pdev
,
704 SET_LUM_CTL
, PRESET_AGC_FORMATTER
, &buf
, sizeof(buf
));
711 int pwc_get_agc(struct pwc_device
*pdev
, int *value
)
716 ret
= recv_control_msg(pdev
,
717 GET_LUM_CTL
, AGC_MODE_FORMATTER
, &buf
, sizeof(buf
));
721 if (buf
!= 0) { /* fixed */
722 ret
= recv_control_msg(pdev
,
723 GET_LUM_CTL
, PRESET_AGC_FORMATTER
, &buf
, sizeof(buf
));
728 *value
= (buf
<< 10);
731 ret
= recv_control_msg(pdev
,
732 GET_STATUS_CTL
, READ_AGC_FORMATTER
, &buf
, sizeof(buf
));
735 /* Gah... this value ranges from 0x00 ... 0x9F */
738 *value
= -(48 + buf
* 409);
744 int pwc_set_shutter_speed(struct pwc_device
*pdev
, int mode
, int value
)
751 buf
[0] = 0x0; /* auto */
753 buf
[0] = 0xff; /* fixed */
755 ret
= send_control_msg(pdev
,
756 SET_LUM_CTL
, SHUTTER_MODE_FORMATTER
, &buf
, sizeof(buf
));
758 if (!mode
&& ret
>= 0) {
764 if (DEVICE_USE_CODEC2(pdev
->type
)) {
765 /* speed ranges from 0x0 to 0x290 (656) */
766 speed
= (value
/ 100);
768 buf
[0] = speed
& 0xff;
769 } else if (DEVICE_USE_CODEC3(pdev
->type
)) {
770 /* speed seems to range from 0x0 to 0xff */
775 ret
= send_control_msg(pdev
,
776 SET_LUM_CTL
, PRESET_SHUTTER_FORMATTER
,
782 /* This function is not exported to v4l1, so output values between 0 -> 256 */
783 int pwc_get_shutter_speed(struct pwc_device
*pdev
, int *value
)
785 unsigned char buf
[2];
788 ret
= recv_control_msg(pdev
,
789 GET_STATUS_CTL
, READ_SHUTTER_FORMATTER
, &buf
, sizeof(buf
));
792 *value
= buf
[0] + (buf
[1] << 8);
793 if (DEVICE_USE_CODEC2(pdev
->type
)) {
794 /* speed ranges from 0x0 to 0x290 (656) */
796 } else if (DEVICE_USE_CODEC3(pdev
->type
)) {
797 /* speed seems to range from 0x0 to 0xff */
805 int pwc_camera_power(struct pwc_device
*pdev
, int power
)
809 if (pdev
->type
< 675 || (pdev
->type
< 730 && pdev
->release
< 6))
810 return 0; /* Not supported by Nala or Timon < release 6 */
813 buf
= 0x00; /* active */
815 buf
= 0xFF; /* power save */
816 return send_control_msg(pdev
,
817 SET_STATUS_CTL
, SET_POWER_SAVE_MODE_FORMATTER
,
825 int pwc_restore_user(struct pwc_device
*pdev
)
827 return send_control_msg(pdev
,
828 SET_STATUS_CTL
, RESTORE_USER_DEFAULTS_FORMATTER
, NULL
, 0);
831 int pwc_save_user(struct pwc_device
*pdev
)
833 return send_control_msg(pdev
,
834 SET_STATUS_CTL
, SAVE_USER_DEFAULTS_FORMATTER
, NULL
, 0);
837 int pwc_restore_factory(struct pwc_device
*pdev
)
839 return send_control_msg(pdev
,
840 SET_STATUS_CTL
, RESTORE_FACTORY_DEFAULTS_FORMATTER
, NULL
, 0);
843 /* ************************************************* */
844 /* Patch by Alvarado: (not in the original version */
847 * the camera recognizes modes from 0 to 4:
849 * 00: indoor (incandescant lighting)
850 * 01: outdoor (sunlight)
851 * 02: fluorescent lighting
855 int pwc_set_awb(struct pwc_device
*pdev
, int mode
)
866 buf
= mode
& 0x07; /* just the lowest three bits */
868 ret
= send_control_msg(pdev
,
869 SET_CHROM_CTL
, WB_MODE_FORMATTER
, &buf
, sizeof(buf
));
876 int pwc_get_awb(struct pwc_device
*pdev
)
881 ret
= recv_control_msg(pdev
,
882 GET_CHROM_CTL
, WB_MODE_FORMATTER
, &buf
, sizeof(buf
));
889 int pwc_set_red_gain(struct pwc_device
*pdev
, int value
)
897 /* only the msb is considered */
899 return send_control_msg(pdev
,
900 SET_CHROM_CTL
, PRESET_MANUAL_RED_GAIN_FORMATTER
,
904 int pwc_get_red_gain(struct pwc_device
*pdev
, int *value
)
909 ret
= recv_control_msg(pdev
,
910 GET_CHROM_CTL
, PRESET_MANUAL_RED_GAIN_FORMATTER
,
919 int pwc_set_blue_gain(struct pwc_device
*pdev
, int value
)
927 /* only the msb is considered */
929 return send_control_msg(pdev
,
930 SET_CHROM_CTL
, PRESET_MANUAL_BLUE_GAIN_FORMATTER
,
934 int pwc_get_blue_gain(struct pwc_device
*pdev
, int *value
)
939 ret
= recv_control_msg(pdev
,
940 GET_CHROM_CTL
, PRESET_MANUAL_BLUE_GAIN_FORMATTER
,
949 /* The following two functions are different, since they only read the
950 internal red/blue gains, which may be different from the manual
951 gains set or read above.
953 static int pwc_read_red_gain(struct pwc_device
*pdev
, int *value
)
958 ret
= recv_control_msg(pdev
,
959 GET_STATUS_CTL
, READ_RED_GAIN_FORMATTER
, &buf
, sizeof(buf
));
966 static int pwc_read_blue_gain(struct pwc_device
*pdev
, int *value
)
971 ret
= recv_control_msg(pdev
,
972 GET_STATUS_CTL
, READ_BLUE_GAIN_FORMATTER
, &buf
, sizeof(buf
));
980 static int pwc_set_wb_speed(struct pwc_device
*pdev
, int speed
)
984 /* useful range is 0x01..0x20 */
986 return send_control_msg(pdev
,
987 SET_CHROM_CTL
, AWB_CONTROL_SPEED_FORMATTER
, &buf
, sizeof(buf
));
990 static int pwc_get_wb_speed(struct pwc_device
*pdev
, int *value
)
995 ret
= recv_control_msg(pdev
,
996 GET_CHROM_CTL
, AWB_CONTROL_SPEED_FORMATTER
, &buf
, sizeof(buf
));
999 *value
= buf
* 0x7f0;
1004 static int pwc_set_wb_delay(struct pwc_device
*pdev
, int delay
)
1008 /* useful range is 0x01..0x3F */
1009 buf
= (delay
>> 10);
1010 return send_control_msg(pdev
,
1011 SET_CHROM_CTL
, AWB_CONTROL_DELAY_FORMATTER
, &buf
, sizeof(buf
));
1014 static int pwc_get_wb_delay(struct pwc_device
*pdev
, int *value
)
1019 ret
= recv_control_msg(pdev
,
1020 GET_CHROM_CTL
, AWB_CONTROL_DELAY_FORMATTER
, &buf
, sizeof(buf
));
1028 int pwc_set_leds(struct pwc_device
*pdev
, int on_value
, int off_value
)
1030 unsigned char buf
[2];
1032 if (pdev
->type
< 730)
1038 if (on_value
> 0xff)
1042 if (off_value
> 0xff)
1048 return send_control_msg(pdev
,
1049 SET_STATUS_CTL
, LED_FORMATTER
, &buf
, sizeof(buf
));
1052 static int pwc_get_leds(struct pwc_device
*pdev
, int *on_value
, int *off_value
)
1054 unsigned char buf
[2];
1057 if (pdev
->type
< 730) {
1063 ret
= recv_control_msg(pdev
,
1064 GET_STATUS_CTL
, LED_FORMATTER
, &buf
, sizeof(buf
));
1067 *on_value
= buf
[0] * 100;
1068 *off_value
= buf
[1] * 100;
1072 int pwc_set_contour(struct pwc_device
*pdev
, int contour
)
1078 buf
= 0xff; /* auto contour on */
1080 buf
= 0x0; /* auto contour off */
1081 ret
= send_control_msg(pdev
,
1082 SET_LUM_CTL
, AUTO_CONTOUR_FORMATTER
, &buf
, sizeof(buf
));
1088 if (contour
> 0xffff)
1091 buf
= (contour
>> 10); /* contour preset is [0..3f] */
1092 ret
= send_control_msg(pdev
,
1093 SET_LUM_CTL
, PRESET_CONTOUR_FORMATTER
, &buf
, sizeof(buf
));
1099 int pwc_get_contour(struct pwc_device
*pdev
, int *contour
)
1104 ret
= recv_control_msg(pdev
,
1105 GET_LUM_CTL
, AUTO_CONTOUR_FORMATTER
, &buf
, sizeof(buf
));
1110 /* auto mode off, query current preset value */
1111 ret
= recv_control_msg(pdev
,
1112 GET_LUM_CTL
, PRESET_CONTOUR_FORMATTER
,
1116 *contour
= buf
<< 10;
1124 int pwc_set_backlight(struct pwc_device
*pdev
, int backlight
)
1132 return send_control_msg(pdev
,
1133 SET_LUM_CTL
, BACK_LIGHT_COMPENSATION_FORMATTER
,
1137 int pwc_get_backlight(struct pwc_device
*pdev
, int *backlight
)
1142 ret
= recv_control_msg(pdev
,
1143 GET_LUM_CTL
, BACK_LIGHT_COMPENSATION_FORMATTER
,
1151 int pwc_set_colour_mode(struct pwc_device
*pdev
, int colour
)
1159 return send_control_msg(pdev
,
1160 SET_CHROM_CTL
, COLOUR_MODE_FORMATTER
, &buf
, sizeof(buf
));
1163 int pwc_get_colour_mode(struct pwc_device
*pdev
, int *colour
)
1168 ret
= recv_control_msg(pdev
,
1169 GET_CHROM_CTL
, COLOUR_MODE_FORMATTER
, &buf
, sizeof(buf
));
1177 int pwc_set_flicker(struct pwc_device
*pdev
, int flicker
)
1185 return send_control_msg(pdev
,
1186 SET_LUM_CTL
, FLICKERLESS_MODE_FORMATTER
, &buf
, sizeof(buf
));
1189 int pwc_get_flicker(struct pwc_device
*pdev
, int *flicker
)
1194 ret
= recv_control_msg(pdev
,
1195 GET_LUM_CTL
, FLICKERLESS_MODE_FORMATTER
, &buf
, sizeof(buf
));
1202 int pwc_set_dynamic_noise(struct pwc_device
*pdev
, int noise
)
1211 return send_control_msg(pdev
,
1212 SET_LUM_CTL
, DYNAMIC_NOISE_CONTROL_FORMATTER
,
1216 int pwc_get_dynamic_noise(struct pwc_device
*pdev
, int *noise
)
1221 ret
= recv_control_msg(pdev
,
1222 GET_LUM_CTL
, DYNAMIC_NOISE_CONTROL_FORMATTER
,
1230 static int _pwc_mpt_reset(struct pwc_device
*pdev
, int flags
)
1234 buf
= flags
& 0x03; // only lower two bits are currently used
1235 return send_control_msg(pdev
,
1236 SET_MPT_CTL
, PT_RESET_CONTROL_FORMATTER
, &buf
, sizeof(buf
));
1239 int pwc_mpt_reset(struct pwc_device
*pdev
, int flags
)
1242 ret
= _pwc_mpt_reset(pdev
, flags
);
1244 pdev
->pan_angle
= 0;
1245 pdev
->tilt_angle
= 0;
1250 static int _pwc_mpt_set_angle(struct pwc_device
*pdev
, int pan
, int tilt
)
1252 unsigned char buf
[4];
1254 /* set new relative angle; angles are expressed in degrees * 100,
1255 but cam as .5 degree resolution, hence divide by 200. Also
1256 the angle must be multiplied by 64 before it's send to
1259 pan
= 64 * pan
/ 100;
1260 tilt
= -64 * tilt
/ 100; /* positive tilt is down, which is not what the user would expect */
1261 buf
[0] = pan
& 0xFF;
1262 buf
[1] = (pan
>> 8) & 0xFF;
1263 buf
[2] = tilt
& 0xFF;
1264 buf
[3] = (tilt
>> 8) & 0xFF;
1265 return send_control_msg(pdev
,
1266 SET_MPT_CTL
, PT_RELATIVE_CONTROL_FORMATTER
, &buf
, sizeof(buf
));
1269 int pwc_mpt_set_angle(struct pwc_device
*pdev
, int pan
, int tilt
)
1273 /* check absolute ranges */
1274 if (pan
< pdev
->angle_range
.pan_min
||
1275 pan
> pdev
->angle_range
.pan_max
||
1276 tilt
< pdev
->angle_range
.tilt_min
||
1277 tilt
> pdev
->angle_range
.tilt_max
)
1280 /* go to relative range, check again */
1281 pan
-= pdev
->pan_angle
;
1282 tilt
-= pdev
->tilt_angle
;
1283 /* angles are specified in degrees * 100, thus the limit = 36000 */
1284 if (pan
< -36000 || pan
> 36000 || tilt
< -36000 || tilt
> 36000)
1287 ret
= _pwc_mpt_set_angle(pdev
, pan
, tilt
);
1289 pdev
->pan_angle
+= pan
;
1290 pdev
->tilt_angle
+= tilt
;
1292 if (ret
== -EPIPE
) /* stall -> out of range */
1297 static int pwc_mpt_get_status(struct pwc_device
*pdev
, struct pwc_mpt_status
*status
)
1300 unsigned char buf
[5];
1302 ret
= recv_control_msg(pdev
,
1303 GET_MPT_CTL
, PT_STATUS_FORMATTER
, &buf
, sizeof(buf
));
1306 status
->status
= buf
[0] & 0x7; // 3 bits are used for reporting
1307 status
->time_pan
= (buf
[1] << 8) + buf
[2];
1308 status
->time_tilt
= (buf
[3] << 8) + buf
[4];
1313 int pwc_get_cmos_sensor(struct pwc_device
*pdev
, int *sensor
)
1316 int ret
= -1, request
;
1318 if (pdev
->type
< 675)
1319 request
= SENSOR_TYPE_FORMATTER1
;
1320 else if (pdev
->type
< 730)
1321 return -1; /* The Vesta series doesn't have this call */
1323 request
= SENSOR_TYPE_FORMATTER2
;
1325 ret
= recv_control_msg(pdev
,
1326 GET_STATUS_CTL
, request
, &buf
, sizeof(buf
));
1329 if (pdev
->type
< 675)
1330 *sensor
= buf
| 0x100;
1337 /* End of Add-Ons */
1338 /* ************************************************* */
1340 /* Linux 2.5.something and 2.6 pass direct pointers to arguments of
1341 ioctl() calls. With 2.4, you have to do tedious copy_from_user()
1342 and copy_to_user() calls. With these macros we circumvent this,
1343 and let me maintain only one source file. The functionality is
1344 exactly the same otherwise.
1347 /* define local variable for arg */
1348 #define ARG_DEF(ARG_type, ARG_name)\
1349 ARG_type *ARG_name = arg;
1350 /* copy arg to local variable */
1351 #define ARG_IN(ARG_name) /* nothing */
1352 /* argument itself (referenced) */
1353 #define ARGR(ARG_name) (*ARG_name)
1354 /* argument address */
1355 #define ARGA(ARG_name) ARG_name
1356 /* copy local variable to arg */
1357 #define ARG_OUT(ARG_name) /* nothing */
1359 long pwc_ioctl(struct pwc_device
*pdev
, unsigned int cmd
, void *arg
)
1364 case VIDIOCPWCRUSER
:
1366 if (pwc_restore_user(pdev
))
1371 case VIDIOCPWCSUSER
:
1373 if (pwc_save_user(pdev
))
1378 case VIDIOCPWCFACTORY
:
1380 if (pwc_restore_factory(pdev
))
1385 case VIDIOCPWCSCQUAL
:
1390 if (ARGR(qual
) < 0 || ARGR(qual
) > 3)
1393 ret
= pwc_try_video_mode(pdev
, pdev
->view
.x
, pdev
->view
.y
, pdev
->vframes
, ARGR(qual
), pdev
->vsnapshot
);
1395 pdev
->vcompression
= ARGR(qual
);
1399 case VIDIOCPWCGCQUAL
:
1403 ARGR(qual
) = pdev
->vcompression
;
1408 case VIDIOCPWCPROBE
:
1410 ARG_DEF(struct pwc_probe
, probe
)
1412 strcpy(ARGR(probe
).name
, pdev
->vdev
->name
);
1413 ARGR(probe
).type
= pdev
->type
;
1418 case VIDIOCPWCGSERIAL
:
1420 ARG_DEF(struct pwc_serial
, serial
)
1422 strcpy(ARGR(serial
).serial
, pdev
->serial
);
1432 if (pwc_set_agc(pdev
, ARGR(agc
) < 0 ? 1 : 0, ARGR(agc
)))
1441 if (pwc_get_agc(pdev
, ARGA(agc
)))
1447 case VIDIOCPWCSSHUTTER
:
1449 ARG_DEF(int, shutter_speed
)
1451 ARG_IN(shutter_speed
)
1452 ret
= pwc_set_shutter_speed(pdev
, ARGR(shutter_speed
) < 0 ? 1 : 0, ARGR(shutter_speed
));
1458 ARG_DEF(struct pwc_whitebalance
, wb
)
1461 ret
= pwc_set_awb(pdev
, ARGR(wb
).mode
);
1462 if (ret
>= 0 && ARGR(wb
).mode
== PWC_WB_MANUAL
) {
1463 pwc_set_red_gain(pdev
, ARGR(wb
).manual_red
);
1464 pwc_set_blue_gain(pdev
, ARGR(wb
).manual_blue
);
1471 ARG_DEF(struct pwc_whitebalance
, wb
)
1473 memset(ARGA(wb
), 0, sizeof(struct pwc_whitebalance
));
1474 ARGR(wb
).mode
= pwc_get_awb(pdev
);
1475 if (ARGR(wb
).mode
< 0)
1478 if (ARGR(wb
).mode
== PWC_WB_MANUAL
) {
1479 ret
= pwc_get_red_gain(pdev
, &ARGR(wb
).manual_red
);
1482 ret
= pwc_get_blue_gain(pdev
, &ARGR(wb
).manual_blue
);
1486 if (ARGR(wb
).mode
== PWC_WB_AUTO
) {
1487 ret
= pwc_read_red_gain(pdev
, &ARGR(wb
).read_red
);
1490 ret
= pwc_read_blue_gain(pdev
, &ARGR(wb
).read_blue
);
1499 case VIDIOCPWCSAWBSPEED
:
1501 ARG_DEF(struct pwc_wb_speed
, wbs
)
1503 if (ARGR(wbs
).control_speed
> 0) {
1504 ret
= pwc_set_wb_speed(pdev
, ARGR(wbs
).control_speed
);
1506 if (ARGR(wbs
).control_delay
> 0) {
1507 ret
= pwc_set_wb_delay(pdev
, ARGR(wbs
).control_delay
);
1512 case VIDIOCPWCGAWBSPEED
:
1514 ARG_DEF(struct pwc_wb_speed
, wbs
)
1516 ret
= pwc_get_wb_speed(pdev
, &ARGR(wbs
).control_speed
);
1519 ret
= pwc_get_wb_delay(pdev
, &ARGR(wbs
).control_delay
);
1528 ARG_DEF(struct pwc_leds
, leds
)
1531 ret
= pwc_set_leds(pdev
, ARGR(leds
).led_on
, ARGR(leds
).led_off
);
1538 ARG_DEF(struct pwc_leds
, leds
)
1540 ret
= pwc_get_leds(pdev
, &ARGR(leds
).led_on
, &ARGR(leds
).led_off
);
1545 case VIDIOCPWCSCONTOUR
:
1547 ARG_DEF(int, contour
)
1550 ret
= pwc_set_contour(pdev
, ARGR(contour
));
1554 case VIDIOCPWCGCONTOUR
:
1556 ARG_DEF(int, contour
)
1558 ret
= pwc_get_contour(pdev
, ARGA(contour
));
1563 case VIDIOCPWCSBACKLIGHT
:
1565 ARG_DEF(int, backlight
)
1568 ret
= pwc_set_backlight(pdev
, ARGR(backlight
));
1572 case VIDIOCPWCGBACKLIGHT
:
1574 ARG_DEF(int, backlight
)
1576 ret
= pwc_get_backlight(pdev
, ARGA(backlight
));
1581 case VIDIOCPWCSFLICKER
:
1583 ARG_DEF(int, flicker
)
1586 ret
= pwc_set_flicker(pdev
, ARGR(flicker
));
1590 case VIDIOCPWCGFLICKER
:
1592 ARG_DEF(int, flicker
)
1594 ret
= pwc_get_flicker(pdev
, ARGA(flicker
));
1599 case VIDIOCPWCSDYNNOISE
:
1601 ARG_DEF(int, dynnoise
)
1604 ret
= pwc_set_dynamic_noise(pdev
, ARGR(dynnoise
));
1608 case VIDIOCPWCGDYNNOISE
:
1610 ARG_DEF(int, dynnoise
)
1612 ret
= pwc_get_dynamic_noise(pdev
, ARGA(dynnoise
));
1617 case VIDIOCPWCGREALSIZE
:
1619 ARG_DEF(struct pwc_imagesize
, size
)
1621 ARGR(size
).width
= pdev
->image
.x
;
1622 ARGR(size
).height
= pdev
->image
.y
;
1627 case VIDIOCPWCMPTRESET
:
1629 if (pdev
->features
& FEATURE_MOTOR_PANTILT
)
1634 ret
= pwc_mpt_reset(pdev
, ARGR(flags
));
1643 case VIDIOCPWCMPTGRANGE
:
1645 if (pdev
->features
& FEATURE_MOTOR_PANTILT
)
1647 ARG_DEF(struct pwc_mpt_range
, range
)
1649 ARGR(range
) = pdev
->angle_range
;
1659 case VIDIOCPWCMPTSANGLE
:
1661 int new_pan
, new_tilt
;
1663 if (pdev
->features
& FEATURE_MOTOR_PANTILT
)
1665 ARG_DEF(struct pwc_mpt_angles
, angles
)
1668 /* The camera can only set relative angles, so
1669 do some calculations when getting an absolute angle .
1671 if (ARGR(angles
).absolute
)
1673 new_pan
= ARGR(angles
).pan
;
1674 new_tilt
= ARGR(angles
).tilt
;
1678 new_pan
= pdev
->pan_angle
+ ARGR(angles
).pan
;
1679 new_tilt
= pdev
->tilt_angle
+ ARGR(angles
).tilt
;
1681 ret
= pwc_mpt_set_angle(pdev
, new_pan
, new_tilt
);
1690 case VIDIOCPWCMPTGANGLE
:
1693 if (pdev
->features
& FEATURE_MOTOR_PANTILT
)
1695 ARG_DEF(struct pwc_mpt_angles
, angles
)
1697 ARGR(angles
).absolute
= 1;
1698 ARGR(angles
).pan
= pdev
->pan_angle
;
1699 ARGR(angles
).tilt
= pdev
->tilt_angle
;
1709 case VIDIOCPWCMPTSTATUS
:
1711 if (pdev
->features
& FEATURE_MOTOR_PANTILT
)
1713 ARG_DEF(struct pwc_mpt_status
, status
)
1715 ret
= pwc_mpt_get_status(pdev
, ARGA(status
));
1725 case VIDIOCPWCGVIDCMD
:
1727 ARG_DEF(struct pwc_video_command
, vcmd
);
1729 ARGR(vcmd
).type
= pdev
->type
;
1730 ARGR(vcmd
).release
= pdev
->release
;
1731 ARGR(vcmd
).command_len
= pdev
->cmd_len
;
1732 memcpy(&ARGR(vcmd
).command_buf
, pdev
->cmd_buf
, pdev
->cmd_len
);
1733 ARGR(vcmd
).bandlength
= pdev
->vbandlength
;
1734 ARGR(vcmd
).frame_size
= pdev
->frame_size
;
1739 case VIDIOCPWCGVIDTABLE:
1741 ARG_DEF(struct pwc_table_init_buffer, table);
1742 ARGR(table).len = pdev->cmd_len;
1743 memcpy(&ARGR(table).buffer, pdev->decompress_data, pdev->decompressor->table_size);
1760 /* vim: set cinoptions= formatoptions=croql cindent shiftwidth=8 tabstop=8: */