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)
6 (C) 2011 Hans de Goede <hdegoede@redhat.com>
8 NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
9 driver and thus may have bugs that are not present in the original version.
10 Please send bug reports and support requests to <luc@saillard.org>.
12 NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
13 driver and thus may have bugs that are not present in the original version.
14 Please send bug reports and support requests to <luc@saillard.org>.
15 The decompression routines have been implemented by reverse-engineering the
16 Nemosoft binary pwcx module. Caveat emptor.
18 This program is free software; you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation; either version 2 of the License, or
21 (at your option) any later version.
23 This program is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
28 You should have received a copy of the GNU General Public License
29 along with this program; if not, write to the Free Software
30 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35 2001/08/03 Alvarado Added methods for changing white balance and
39 /* Control functions for the cam; brightness, contrast, video mode, etc. */
42 #include <asm/uaccess.h>
44 #include <asm/errno.h>
47 #include "pwc-kiara.h"
48 #include "pwc-timon.h"
50 #include "pwc-dec23.h"
52 /* Selectors for status controls used only in this file */
53 #define GET_STATUS_B00 0x0B00
54 #define SENSOR_TYPE_FORMATTER1 0x0C00
55 #define GET_STATUS_3000 0x3000
56 #define READ_RAW_Y_MEAN_FORMATTER 0x3100
57 #define SET_POWER_SAVE_MODE_FORMATTER 0x3200
58 #define MIRROR_IMAGE_FORMATTER 0x3300
59 #define LED_FORMATTER 0x3400
60 #define LOWLIGHT 0x3500
61 #define GET_STATUS_3600 0x3600
62 #define SENSOR_TYPE_FORMATTER2 0x3700
63 #define GET_STATUS_3800 0x3800
64 #define GET_STATUS_4000 0x4000
65 #define GET_STATUS_4100 0x4100 /* Get */
66 #define CTL_STATUS_4200 0x4200 /* [GS] 1 */
68 /* Formatters for the Video Endpoint controls [GS]ET_EP_STREAM_CTL */
69 #define VIDEO_OUTPUT_CONTROL_FORMATTER 0x0100
71 static const char *size2name
[PSZ_MAX
] =
83 /* Entries for the Nala (645/646) camera; the Nala doesn't have compression
84 preferences, so you either get compressed or non-compressed streams.
86 An alternate value of 0 means this mode is not available at all.
89 #define PWC_FPS_MAX_NALA 8
91 struct Nala_table_entry
{
92 char alternate
; /* USB alternate setting */
93 int compressed
; /* Compressed yes/no */
95 unsigned char mode
[3]; /* precomputed mode table */
98 static unsigned int Nala_fps_vector
[PWC_FPS_MAX_NALA
] = { 4, 5, 7, 10, 12, 15, 20, 24 };
100 static struct Nala_table_entry Nala_table
[PSZ_MAX
][PWC_FPS_MAX_NALA
] =
102 #include "pwc-nala.h"
105 static void pwc_set_image_buffer_size(struct pwc_device
*pdev
);
107 /****************************************************************************/
109 static int _send_control_msg(struct pwc_device
*pdev
,
110 u8 request
, u16 value
, int index
, void *buf
, int buflen
)
116 kbuf
= kmalloc(buflen
, GFP_KERNEL
); /* not allowed on stack */
119 memcpy(kbuf
, buf
, buflen
);
122 rc
= usb_control_msg(pdev
->udev
, usb_sndctrlpipe(pdev
->udev
, 0),
124 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
127 kbuf
, buflen
, USB_CTRL_SET_TIMEOUT
);
133 static int recv_control_msg(struct pwc_device
*pdev
,
134 u8 request
, u16 value
, void *buf
, int buflen
)
137 void *kbuf
= kmalloc(buflen
, GFP_KERNEL
); /* not allowed on stack */
142 rc
= usb_control_msg(pdev
->udev
, usb_rcvctrlpipe(pdev
->udev
, 0),
144 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
147 kbuf
, buflen
, USB_CTRL_GET_TIMEOUT
);
148 memcpy(buf
, kbuf
, buflen
);
152 PWC_ERROR("recv_control_msg error %d req %02x val %04x\n",
157 static inline int send_video_command(struct pwc_device
*pdev
,
158 int index
, void *buf
, int buflen
)
160 return _send_control_msg(pdev
,
162 VIDEO_OUTPUT_CONTROL_FORMATTER
,
167 int send_control_msg(struct pwc_device
*pdev
,
168 u8 request
, u16 value
, void *buf
, int buflen
)
170 return _send_control_msg(pdev
,
171 request
, value
, pdev
->vcinterface
, buf
, buflen
);
174 static int set_video_mode_Nala(struct pwc_device
*pdev
, int size
, int frames
)
176 unsigned char buf
[3];
178 struct Nala_table_entry
*pEntry
;
179 int frames2frames
[31] =
180 { /* closest match of framerate */
181 0, 0, 0, 0, 4, /* 0-4 */
182 5, 5, 7, 7, 10, /* 5-9 */
183 10, 10, 12, 12, 15, /* 10-14 */
184 15, 15, 15, 20, 20, /* 15-19 */
185 20, 20, 20, 24, 24, /* 20-24 */
186 24, 24, 24, 24, 24, /* 25-29 */
189 int frames2table
[31] =
190 { 0, 0, 0, 0, 0, /* 0-4 */
191 1, 1, 1, 2, 2, /* 5-9 */
192 3, 3, 4, 4, 4, /* 10-14 */
193 5, 5, 5, 5, 5, /* 15-19 */
194 6, 6, 6, 6, 7, /* 20-24 */
195 7, 7, 7, 7, 7, /* 25-29 */
199 if (size
< 0 || size
> PSZ_CIF
|| frames
< 4 || frames
> 25)
201 frames
= frames2frames
[frames
];
202 fps
= frames2table
[frames
];
203 pEntry
= &Nala_table
[size
][fps
];
204 if (pEntry
->alternate
== 0)
207 memcpy(buf
, pEntry
->mode
, 3);
208 ret
= send_video_command(pdev
, pdev
->vendpoint
, buf
, 3);
210 PWC_DEBUG_MODULE("Failed to send video command... %d\n", ret
);
213 if (pEntry
->compressed
&& pdev
->pixfmt
== V4L2_PIX_FMT_YUV420
) {
214 ret
= pwc_dec1_init(pdev
, pdev
->type
, pdev
->release
, buf
);
220 memcpy(pdev
->cmd_buf
, buf
, 3);
222 /* Set various parameters */
223 pdev
->vframes
= frames
;
225 pdev
->valternate
= pEntry
->alternate
;
226 pdev
->image
= pwc_image_sizes
[size
];
227 pdev
->frame_size
= (pdev
->image
.x
* pdev
->image
.y
* 3) / 2;
228 if (pEntry
->compressed
) {
229 if (pdev
->release
< 5) { /* 4 fold compression */
230 pdev
->vbandlength
= 528;
231 pdev
->frame_size
/= 4;
234 pdev
->vbandlength
= 704;
235 pdev
->frame_size
/= 3;
239 pdev
->vbandlength
= 0;
244 static int set_video_mode_Timon(struct pwc_device
*pdev
, int size
, int frames
, int compression
, int snapshot
)
246 unsigned char buf
[13];
247 const struct Timon_table_entry
*pChoose
;
250 if (size
>= PSZ_MAX
|| frames
< 5 || frames
> 30 || compression
< 0 || compression
> 3)
252 if (size
== PSZ_VGA
&& frames
> 15)
254 fps
= (frames
/ 5) - 1;
256 /* Find a supported framerate with progressively higher compression ratios
257 if the preferred ratio is not available.
260 while (compression
<= 3) {
261 pChoose
= &Timon_table
[size
][fps
][compression
];
262 if (pChoose
->alternate
!= 0)
266 if (pChoose
== NULL
|| pChoose
->alternate
== 0)
267 return -ENOENT
; /* Not supported. */
269 memcpy(buf
, pChoose
->mode
, 13);
272 ret
= send_video_command(pdev
, pdev
->vendpoint
, buf
, 13);
276 if (pChoose
->bandlength
> 0 && pdev
->pixfmt
== V4L2_PIX_FMT_YUV420
) {
277 ret
= pwc_dec23_init(pdev
, pdev
->type
, buf
);
283 memcpy(pdev
->cmd_buf
, buf
, 13);
285 /* Set various parameters */
286 pdev
->vframes
= frames
;
288 pdev
->vsnapshot
= snapshot
;
289 pdev
->valternate
= pChoose
->alternate
;
290 pdev
->image
= pwc_image_sizes
[size
];
291 pdev
->vbandlength
= pChoose
->bandlength
;
292 if (pChoose
->bandlength
> 0)
293 pdev
->frame_size
= (pChoose
->bandlength
* pdev
->image
.y
) / 4;
295 pdev
->frame_size
= (pdev
->image
.x
* pdev
->image
.y
* 12) / 8;
300 static int set_video_mode_Kiara(struct pwc_device
*pdev
, int size
, int frames
, int compression
, int snapshot
)
302 const struct Kiara_table_entry
*pChoose
= NULL
;
304 unsigned char buf
[12];
305 struct Kiara_table_entry RawEntry
= {6, 773, 1272, {0xAD, 0xF4, 0x10, 0x27, 0xB6, 0x24, 0x96, 0x02, 0x30, 0x05, 0x03, 0x80}};
307 if (size
>= PSZ_MAX
|| frames
< 5 || frames
> 30 || compression
< 0 || compression
> 3)
309 if (size
== PSZ_VGA
&& frames
> 15)
311 fps
= (frames
/ 5) - 1;
313 /* special case: VGA @ 5 fps and snapshot is raw bayer mode */
314 if (size
== PSZ_VGA
&& frames
== 5 && snapshot
&& pdev
->pixfmt
!= V4L2_PIX_FMT_YUV420
)
316 /* Only available in case the raw palette is selected or
317 we have the decompressor available. This mode is
318 only available in compressed form
320 PWC_DEBUG_SIZE("Choosing VGA/5 BAYER mode.\n");
325 /* Find a supported framerate with progressively higher compression ratios
326 if the preferred ratio is not available.
327 Skip this step when using RAW modes.
330 while (compression
<= 3) {
331 pChoose
= &Kiara_table
[size
][fps
][compression
];
332 if (pChoose
->alternate
!= 0)
337 if (pChoose
== NULL
|| pChoose
->alternate
== 0)
338 return -ENOENT
; /* Not supported. */
340 PWC_TRACE("Using alternate setting %d.\n", pChoose
->alternate
);
342 /* usb_control_msg won't take staticly allocated arrays as argument?? */
343 memcpy(buf
, pChoose
->mode
, 12);
347 /* Firmware bug: video endpoint is 5, but commands are sent to endpoint 4 */
348 ret
= send_video_command(pdev
, 4 /* pdev->vendpoint */, buf
, 12);
352 if (pChoose
->bandlength
> 0 && pdev
->pixfmt
== V4L2_PIX_FMT_YUV420
) {
353 ret
= pwc_dec23_init(pdev
, pdev
->type
, buf
);
359 memcpy(pdev
->cmd_buf
, buf
, 12);
361 pdev
->vframes
= frames
;
363 pdev
->vsnapshot
= snapshot
;
364 pdev
->valternate
= pChoose
->alternate
;
365 pdev
->image
= pwc_image_sizes
[size
];
366 pdev
->vbandlength
= pChoose
->bandlength
;
367 if (pdev
->vbandlength
> 0)
368 pdev
->frame_size
= (pdev
->vbandlength
* pdev
->image
.y
) / 4;
370 pdev
->frame_size
= (pdev
->image
.x
* pdev
->image
.y
* 12) / 8;
371 PWC_TRACE("frame_size=%d, vframes=%d, vsize=%d, vsnapshot=%d, vbandlength=%d\n",
372 pdev
->frame_size
,pdev
->vframes
,pdev
->vsize
,pdev
->vsnapshot
,pdev
->vbandlength
);
379 @pdev: device structure
380 @width: viewport width
381 @height: viewport height
382 @frame: framerate, in fps
383 @compression: preferred compression ratio
384 @snapshot: snapshot mode or streaming
386 int pwc_set_video_mode(struct pwc_device
*pdev
, int width
, int height
, int frames
, int compression
, int snapshot
)
390 PWC_DEBUG_FLOW("set_video_mode(%dx%d @ %d, pixfmt %08x).\n", width
, height
, frames
, pdev
->pixfmt
);
391 size
= pwc_decode_size(pdev
, width
, height
);
393 PWC_DEBUG_MODULE("Could not find suitable size.\n");
396 PWC_TRACE("decode_size = %d.\n", size
);
398 if (DEVICE_USE_CODEC1(pdev
->type
)) {
399 ret
= set_video_mode_Nala(pdev
, size
, frames
);
401 } else if (DEVICE_USE_CODEC3(pdev
->type
)) {
402 ret
= set_video_mode_Kiara(pdev
, size
, frames
, compression
, snapshot
);
405 ret
= set_video_mode_Timon(pdev
, size
, frames
, compression
, snapshot
);
408 PWC_ERROR("Failed to set video mode %s@%d fps; return code = %d\n", size2name
[size
], frames
, ret
);
411 pdev
->view
.x
= width
;
412 pdev
->view
.y
= height
;
413 pdev
->vcompression
= compression
;
414 pdev
->frame_total_size
= pdev
->frame_size
+ pdev
->frame_header_size
+ pdev
->frame_trailer_size
;
415 pwc_set_image_buffer_size(pdev
);
416 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
);
420 static unsigned int pwc_get_fps_Nala(struct pwc_device
*pdev
, unsigned int index
, unsigned int size
)
424 for (i
= 0; i
< PWC_FPS_MAX_NALA
; i
++) {
425 if (Nala_table
[size
][i
].alternate
) {
426 if (index
--==0) return Nala_fps_vector
[i
];
432 static unsigned int pwc_get_fps_Kiara(struct pwc_device
*pdev
, unsigned int index
, unsigned int size
)
436 for (i
= 0; i
< PWC_FPS_MAX_KIARA
; i
++) {
437 if (Kiara_table
[size
][i
][3].alternate
) {
438 if (index
--==0) return Kiara_fps_vector
[i
];
444 static unsigned int pwc_get_fps_Timon(struct pwc_device
*pdev
, unsigned int index
, unsigned int size
)
448 for (i
=0; i
< PWC_FPS_MAX_TIMON
; i
++) {
449 if (Timon_table
[size
][i
][3].alternate
) {
450 if (index
--==0) return Timon_fps_vector
[i
];
456 unsigned int pwc_get_fps(struct pwc_device
*pdev
, unsigned int index
, unsigned int size
)
460 if (DEVICE_USE_CODEC1(pdev
->type
)) {
461 ret
= pwc_get_fps_Nala(pdev
, index
, size
);
463 } else if (DEVICE_USE_CODEC3(pdev
->type
)) {
464 ret
= pwc_get_fps_Kiara(pdev
, index
, size
);
467 ret
= pwc_get_fps_Timon(pdev
, index
, size
);
473 static void pwc_set_image_buffer_size(struct pwc_device
*pdev
)
477 /* for V4L2_PIX_FMT_YUV420 */
478 switch (pdev
->pixfmt
) {
479 case V4L2_PIX_FMT_YUV420
:
482 case V4L2_PIX_FMT_PWC1
:
483 case V4L2_PIX_FMT_PWC2
:
484 factor
= 6; /* can be uncompressed YUV420P */
488 /* Set sizes in bytes */
489 pdev
->image
.size
= pdev
->image
.x
* pdev
->image
.y
* factor
/ 4;
490 pdev
->view
.size
= pdev
->view
.x
* pdev
->view
.y
* factor
/ 4;
492 /* Align offset, or you'll get some very weird results in
493 YUV420 mode... x must be multiple of 4 (to get the Y's in
494 place), and y even (or you'll mixup U & V). This is less of a
497 pdev
->offset
.x
= ((pdev
->view
.x
- pdev
->image
.x
) / 2) & 0xFFFC;
498 pdev
->offset
.y
= ((pdev
->view
.y
- pdev
->image
.y
) / 2) & 0xFFFE;
501 int pwc_get_u8_ctrl(struct pwc_device
*pdev
, u8 request
, u16 value
, int *data
)
506 ret
= recv_control_msg(pdev
, request
, value
, &buf
, sizeof(buf
));
514 int pwc_set_u8_ctrl(struct pwc_device
*pdev
, u8 request
, u16 value
, u8 data
)
518 ret
= send_control_msg(pdev
, request
, value
, &data
, sizeof(data
));
525 int pwc_get_s8_ctrl(struct pwc_device
*pdev
, u8 request
, u16 value
, int *data
)
530 ret
= recv_control_msg(pdev
, request
, value
, &buf
, sizeof(buf
));
538 int pwc_get_u16_ctrl(struct pwc_device
*pdev
, u8 request
, u16 value
, int *data
)
543 ret
= recv_control_msg(pdev
, request
, value
, buf
, sizeof(buf
));
547 *data
= (buf
[1] << 8) | buf
[0];
551 int pwc_set_u16_ctrl(struct pwc_device
*pdev
, u8 request
, u16 value
, u16 data
)
556 buf
[0] = data
& 0xff;
558 ret
= send_control_msg(pdev
, request
, value
, buf
, sizeof(buf
));
565 int pwc_button_ctrl(struct pwc_device
*pdev
, u16 value
)
569 ret
= send_control_msg(pdev
, SET_STATUS_CTL
, value
, NULL
, 0);
577 void pwc_camera_power(struct pwc_device
*pdev
, int power
)
582 if (!pdev
->power_save
)
585 if (pdev
->type
< 675 || (pdev
->type
< 730 && pdev
->release
< 6))
586 return; /* Not supported by Nala or Timon < release 6 */
589 buf
= 0x00; /* active */
591 buf
= 0xFF; /* power save */
592 r
= send_control_msg(pdev
,
593 SET_STATUS_CTL
, SET_POWER_SAVE_MODE_FORMATTER
,
597 PWC_ERROR("Failed to power %s camera (%d)\n",
598 power
? "on" : "off", r
);
601 static int pwc_set_wb_speed(struct pwc_device
*pdev
, int speed
)
605 /* useful range is 0x01..0x20 */
607 return send_control_msg(pdev
,
608 SET_CHROM_CTL
, AWB_CONTROL_SPEED_FORMATTER
, &buf
, sizeof(buf
));
611 static int pwc_get_wb_speed(struct pwc_device
*pdev
, int *value
)
616 ret
= recv_control_msg(pdev
,
617 GET_CHROM_CTL
, AWB_CONTROL_SPEED_FORMATTER
, &buf
, sizeof(buf
));
620 *value
= buf
* 0x7f0;
625 static int pwc_set_wb_delay(struct pwc_device
*pdev
, int delay
)
629 /* useful range is 0x01..0x3F */
631 return send_control_msg(pdev
,
632 SET_CHROM_CTL
, AWB_CONTROL_DELAY_FORMATTER
, &buf
, sizeof(buf
));
635 static int pwc_get_wb_delay(struct pwc_device
*pdev
, int *value
)
640 ret
= recv_control_msg(pdev
,
641 GET_CHROM_CTL
, AWB_CONTROL_DELAY_FORMATTER
, &buf
, sizeof(buf
));
649 int pwc_set_leds(struct pwc_device
*pdev
, int on_value
, int off_value
)
651 unsigned char buf
[2];
654 if (pdev
->type
< 730)
664 if (off_value
> 0xff)
670 r
= send_control_msg(pdev
,
671 SET_STATUS_CTL
, LED_FORMATTER
, &buf
, sizeof(buf
));
673 PWC_ERROR("Failed to set LED on/off time (%d)\n", r
);
678 static int pwc_get_leds(struct pwc_device
*pdev
, int *on_value
, int *off_value
)
680 unsigned char buf
[2];
683 if (pdev
->type
< 730) {
689 ret
= recv_control_msg(pdev
,
690 GET_STATUS_CTL
, LED_FORMATTER
, &buf
, sizeof(buf
));
693 *on_value
= buf
[0] * 100;
694 *off_value
= buf
[1] * 100;
698 static int _pwc_mpt_reset(struct pwc_device
*pdev
, int flags
)
702 buf
= flags
& 0x03; // only lower two bits are currently used
703 return send_control_msg(pdev
,
704 SET_MPT_CTL
, PT_RESET_CONTROL_FORMATTER
, &buf
, sizeof(buf
));
707 int pwc_mpt_reset(struct pwc_device
*pdev
, int flags
)
710 ret
= _pwc_mpt_reset(pdev
, flags
);
713 pdev
->tilt_angle
= 0;
718 static int _pwc_mpt_set_angle(struct pwc_device
*pdev
, int pan
, int tilt
)
720 unsigned char buf
[4];
722 /* set new relative angle; angles are expressed in degrees * 100,
723 but cam as .5 degree resolution, hence divide by 200. Also
724 the angle must be multiplied by 64 before it's send to
727 pan
= 64 * pan
/ 100;
728 tilt
= -64 * tilt
/ 100; /* positive tilt is down, which is not what the user would expect */
730 buf
[1] = (pan
>> 8) & 0xFF;
731 buf
[2] = tilt
& 0xFF;
732 buf
[3] = (tilt
>> 8) & 0xFF;
733 return send_control_msg(pdev
,
734 SET_MPT_CTL
, PT_RELATIVE_CONTROL_FORMATTER
, &buf
, sizeof(buf
));
737 int pwc_mpt_set_angle(struct pwc_device
*pdev
, int pan
, int tilt
)
741 /* check absolute ranges */
742 if (pan
< pdev
->angle_range
.pan_min
||
743 pan
> pdev
->angle_range
.pan_max
||
744 tilt
< pdev
->angle_range
.tilt_min
||
745 tilt
> pdev
->angle_range
.tilt_max
)
748 /* go to relative range, check again */
749 pan
-= pdev
->pan_angle
;
750 tilt
-= pdev
->tilt_angle
;
751 /* angles are specified in degrees * 100, thus the limit = 36000 */
752 if (pan
< -36000 || pan
> 36000 || tilt
< -36000 || tilt
> 36000)
755 ret
= _pwc_mpt_set_angle(pdev
, pan
, tilt
);
757 pdev
->pan_angle
+= pan
;
758 pdev
->tilt_angle
+= tilt
;
760 if (ret
== -EPIPE
) /* stall -> out of range */
765 static int pwc_mpt_get_status(struct pwc_device
*pdev
, struct pwc_mpt_status
*status
)
768 unsigned char buf
[5];
770 ret
= recv_control_msg(pdev
,
771 GET_MPT_CTL
, PT_STATUS_FORMATTER
, &buf
, sizeof(buf
));
774 status
->status
= buf
[0] & 0x7; // 3 bits are used for reporting
775 status
->time_pan
= (buf
[1] << 8) + buf
[2];
776 status
->time_tilt
= (buf
[3] << 8) + buf
[4];
780 #ifdef CONFIG_USB_PWC_DEBUG
781 int pwc_get_cmos_sensor(struct pwc_device
*pdev
, int *sensor
)
784 int ret
= -1, request
;
786 if (pdev
->type
< 675)
787 request
= SENSOR_TYPE_FORMATTER1
;
788 else if (pdev
->type
< 730)
789 return -1; /* The Vesta series doesn't have this call */
791 request
= SENSOR_TYPE_FORMATTER2
;
793 ret
= recv_control_msg(pdev
,
794 GET_STATUS_CTL
, request
, &buf
, sizeof(buf
));
797 if (pdev
->type
< 675)
798 *sensor
= buf
| 0x100;
806 /* ************************************************* */
808 /* Linux 2.5.something and 2.6 pass direct pointers to arguments of
809 ioctl() calls. With 2.4, you have to do tedious copy_from_user()
810 and copy_to_user() calls. With these macros we circumvent this,
811 and let me maintain only one source file. The functionality is
812 exactly the same otherwise.
815 /* define local variable for arg */
816 #define ARG_DEF(ARG_type, ARG_name)\
817 ARG_type *ARG_name = arg;
818 /* copy arg to local variable */
819 #define ARG_IN(ARG_name) /* nothing */
820 /* argument itself (referenced) */
821 #define ARGR(ARG_name) (*ARG_name)
822 /* argument address */
823 #define ARGA(ARG_name) ARG_name
824 /* copy local variable to arg */
825 #define ARG_OUT(ARG_name) /* nothing */
828 * Our ctrls use native values, but the old custom pwc ioctl interface expects
829 * values from 0 - 65535, define 2 helper functions to scale things. */
830 static int pwc_ioctl_g_ctrl(struct v4l2_ctrl
*ctrl
)
832 return v4l2_ctrl_g_ctrl(ctrl
) * 65535 / ctrl
->maximum
;
835 static int pwc_ioctl_s_ctrl(struct v4l2_ctrl
*ctrl
, int val
)
837 return v4l2_ctrl_s_ctrl(ctrl
, val
* ctrl
->maximum
/ 65535);
840 long pwc_ioctl(struct pwc_device
*pdev
, unsigned int cmd
, void *arg
)
846 ret
= pwc_button_ctrl(pdev
, RESTORE_USER_DEFAULTS_FORMATTER
);
850 ret
= pwc_button_ctrl(pdev
, SAVE_USER_DEFAULTS_FORMATTER
);
853 case VIDIOCPWCFACTORY
:
854 ret
= pwc_button_ctrl(pdev
, RESTORE_FACTORY_DEFAULTS_FORMATTER
);
857 case VIDIOCPWCSCQUAL
:
861 if (vb2_is_streaming(&pdev
->vb_queue
)) {
867 if (ARGR(qual
) < 0 || ARGR(qual
) > 3)
870 ret
= pwc_set_video_mode(pdev
, pdev
->view
.x
, pdev
->view
.y
, pdev
->vframes
, ARGR(qual
), pdev
->vsnapshot
);
874 case VIDIOCPWCGCQUAL
:
878 ARGR(qual
) = pdev
->vcompression
;
885 ARG_DEF(struct pwc_probe
, probe
)
887 strcpy(ARGR(probe
).name
, pdev
->vdev
.name
);
888 ARGR(probe
).type
= pdev
->type
;
893 case VIDIOCPWCGSERIAL
:
895 ARG_DEF(struct pwc_serial
, serial
)
897 strcpy(ARGR(serial
).serial
, pdev
->serial
);
906 ret
= v4l2_ctrl_s_ctrl(pdev
->autogain
, ARGR(agc
) < 0);
907 if (ret
== 0 && ARGR(agc
) >= 0)
908 ret
= pwc_ioctl_s_ctrl(pdev
->gain
, ARGR(agc
));
915 if (v4l2_ctrl_g_ctrl(pdev
->autogain
))
918 ARGR(agc
) = pwc_ioctl_g_ctrl(pdev
->gain
);
923 case VIDIOCPWCSSHUTTER
:
925 ARG_DEF(int, shutter
)
927 ret
= v4l2_ctrl_s_ctrl(pdev
->exposure_auto
,
928 /* Menu idx 0 = auto, idx 1 = manual */
930 if (ret
== 0 && ARGR(shutter
) >= 0)
931 ret
= pwc_ioctl_s_ctrl(pdev
->exposure
, ARGR(shutter
));
937 ARG_DEF(struct pwc_whitebalance
, wb
)
939 ret
= v4l2_ctrl_s_ctrl(pdev
->auto_white_balance
,
941 if (ret
== 0 && ARGR(wb
).mode
== PWC_WB_MANUAL
)
942 ret
= pwc_ioctl_s_ctrl(pdev
->red_balance
,
943 ARGR(wb
).manual_red
);
944 if (ret
== 0 && ARGR(wb
).mode
== PWC_WB_MANUAL
)
945 ret
= pwc_ioctl_s_ctrl(pdev
->blue_balance
,
946 ARGR(wb
).manual_blue
);
952 ARG_DEF(struct pwc_whitebalance
, wb
)
953 ARGR(wb
).mode
= v4l2_ctrl_g_ctrl(pdev
->auto_white_balance
);
954 ARGR(wb
).manual_red
= ARGR(wb
).read_red
=
955 pwc_ioctl_g_ctrl(pdev
->red_balance
);
956 ARGR(wb
).manual_blue
= ARGR(wb
).read_blue
=
957 pwc_ioctl_g_ctrl(pdev
->blue_balance
);
962 case VIDIOCPWCSAWBSPEED
:
964 ARG_DEF(struct pwc_wb_speed
, wbs
)
966 if (ARGR(wbs
).control_speed
> 0) {
967 ret
= pwc_set_wb_speed(pdev
, ARGR(wbs
).control_speed
);
969 if (ARGR(wbs
).control_delay
> 0) {
970 ret
= pwc_set_wb_delay(pdev
, ARGR(wbs
).control_delay
);
975 case VIDIOCPWCGAWBSPEED
:
977 ARG_DEF(struct pwc_wb_speed
, wbs
)
979 ret
= pwc_get_wb_speed(pdev
, &ARGR(wbs
).control_speed
);
982 ret
= pwc_get_wb_delay(pdev
, &ARGR(wbs
).control_delay
);
991 ARG_DEF(struct pwc_leds
, leds
)
994 ret
= pwc_set_leds(pdev
, ARGR(leds
).led_on
, ARGR(leds
).led_off
);
1001 ARG_DEF(struct pwc_leds
, leds
)
1003 ret
= pwc_get_leds(pdev
, &ARGR(leds
).led_on
, &ARGR(leds
).led_off
);
1008 case VIDIOCPWCSCONTOUR
:
1010 ARG_DEF(int, contour
)
1012 ret
= v4l2_ctrl_s_ctrl(pdev
->autocontour
, ARGR(contour
) < 0);
1013 if (ret
== 0 && ARGR(contour
) >= 0)
1014 ret
= pwc_ioctl_s_ctrl(pdev
->contour
, ARGR(contour
));
1018 case VIDIOCPWCGCONTOUR
:
1020 ARG_DEF(int, contour
)
1021 if (v4l2_ctrl_g_ctrl(pdev
->autocontour
))
1024 ARGR(contour
) = pwc_ioctl_g_ctrl(pdev
->contour
);
1029 case VIDIOCPWCSBACKLIGHT
:
1031 ARG_DEF(int, backlight
)
1033 ret
= v4l2_ctrl_s_ctrl(pdev
->backlight
, ARGR(backlight
));
1037 case VIDIOCPWCGBACKLIGHT
:
1039 ARG_DEF(int, backlight
)
1040 ARGR(backlight
) = v4l2_ctrl_g_ctrl(pdev
->backlight
);
1045 case VIDIOCPWCSFLICKER
:
1047 ARG_DEF(int, flicker
)
1049 ret
= v4l2_ctrl_s_ctrl(pdev
->flicker
, ARGR(flicker
));
1053 case VIDIOCPWCGFLICKER
:
1055 ARG_DEF(int, flicker
)
1056 ARGR(flicker
) = v4l2_ctrl_g_ctrl(pdev
->flicker
);
1061 case VIDIOCPWCSDYNNOISE
:
1063 ARG_DEF(int, dynnoise
)
1065 ret
= v4l2_ctrl_s_ctrl(pdev
->noise_reduction
, ARGR(dynnoise
));
1069 case VIDIOCPWCGDYNNOISE
:
1071 ARG_DEF(int, dynnoise
)
1072 ARGR(dynnoise
) = v4l2_ctrl_g_ctrl(pdev
->noise_reduction
);
1077 case VIDIOCPWCGREALSIZE
:
1079 ARG_DEF(struct pwc_imagesize
, size
)
1081 ARGR(size
).width
= pdev
->image
.x
;
1082 ARGR(size
).height
= pdev
->image
.y
;
1087 case VIDIOCPWCMPTRESET
:
1089 if (pdev
->features
& FEATURE_MOTOR_PANTILT
)
1094 ret
= pwc_mpt_reset(pdev
, ARGR(flags
));
1103 case VIDIOCPWCMPTGRANGE
:
1105 if (pdev
->features
& FEATURE_MOTOR_PANTILT
)
1107 ARG_DEF(struct pwc_mpt_range
, range
)
1109 ARGR(range
) = pdev
->angle_range
;
1119 case VIDIOCPWCMPTSANGLE
:
1121 int new_pan
, new_tilt
;
1123 if (pdev
->features
& FEATURE_MOTOR_PANTILT
)
1125 ARG_DEF(struct pwc_mpt_angles
, angles
)
1128 /* The camera can only set relative angles, so
1129 do some calculations when getting an absolute angle .
1131 if (ARGR(angles
).absolute
)
1133 new_pan
= ARGR(angles
).pan
;
1134 new_tilt
= ARGR(angles
).tilt
;
1138 new_pan
= pdev
->pan_angle
+ ARGR(angles
).pan
;
1139 new_tilt
= pdev
->tilt_angle
+ ARGR(angles
).tilt
;
1141 ret
= pwc_mpt_set_angle(pdev
, new_pan
, new_tilt
);
1150 case VIDIOCPWCMPTGANGLE
:
1153 if (pdev
->features
& FEATURE_MOTOR_PANTILT
)
1155 ARG_DEF(struct pwc_mpt_angles
, angles
)
1157 ARGR(angles
).absolute
= 1;
1158 ARGR(angles
).pan
= pdev
->pan_angle
;
1159 ARGR(angles
).tilt
= pdev
->tilt_angle
;
1169 case VIDIOCPWCMPTSTATUS
:
1171 if (pdev
->features
& FEATURE_MOTOR_PANTILT
)
1173 ARG_DEF(struct pwc_mpt_status
, status
)
1175 ret
= pwc_mpt_get_status(pdev
, ARGA(status
));
1185 case VIDIOCPWCGVIDCMD
:
1187 ARG_DEF(struct pwc_video_command
, vcmd
);
1189 ARGR(vcmd
).type
= pdev
->type
;
1190 ARGR(vcmd
).release
= pdev
->release
;
1191 ARGR(vcmd
).command_len
= pdev
->cmd_len
;
1192 memcpy(&ARGR(vcmd
).command_buf
, pdev
->cmd_buf
, pdev
->cmd_len
);
1193 ARGR(vcmd
).bandlength
= pdev
->vbandlength
;
1194 ARGR(vcmd
).frame_size
= pdev
->frame_size
;
1199 case VIDIOCPWCGVIDTABLE:
1201 ARG_DEF(struct pwc_table_init_buffer, table);
1202 ARGR(table).len = pdev->cmd_len;
1203 memcpy(&ARGR(table).buffer, pdev->decompress_data, pdev->decompressor->table_size);
1220 /* vim: set cinoptions= formatoptions=croql cindent shiftwidth=8 tabstop=8: */