Merge remote-tracking branch 'moduleh/module.h-split'
[linux-2.6/next.git] / drivers / media / video / pwc / pwc-ctrl.c
blob3977addf3ba8553d586bfb0a3b8521e146435e7e
1 /* Driver for Philips webcam
2 Functions that send various control messages to the webcam, including
3 video modes.
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
34 Changes
35 2001/08/03 Alvarado Added methods for changing white balance and
36 red/green gains
39 /* Control functions for the cam; brightness, contrast, video mode, etc. */
41 #ifdef __KERNEL__
42 #include <asm/uaccess.h>
43 #endif
44 #include <asm/errno.h>
46 #include "pwc.h"
47 #include "pwc-kiara.h"
48 #include "pwc-timon.h"
49 #include "pwc-dec1.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] =
73 "subQCIF",
74 "QSIF",
75 "QCIF",
76 "SIF",
77 "CIF",
78 "VGA",
81 /********/
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)
112 int rc;
113 void *kbuf = NULL;
115 if (buflen) {
116 kbuf = kmalloc(buflen, GFP_KERNEL); /* not allowed on stack */
117 if (kbuf == NULL)
118 return -ENOMEM;
119 memcpy(kbuf, buf, buflen);
122 rc = usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
123 request,
124 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
125 value,
126 index,
127 kbuf, buflen, USB_CTRL_SET_TIMEOUT);
129 kfree(kbuf);
130 return rc;
133 static int recv_control_msg(struct pwc_device *pdev,
134 u8 request, u16 value, void *buf, int buflen)
136 int rc;
137 void *kbuf = kmalloc(buflen, GFP_KERNEL); /* not allowed on stack */
139 if (kbuf == NULL)
140 return -ENOMEM;
142 rc = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
143 request,
144 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
145 value,
146 pdev->vcinterface,
147 kbuf, buflen, USB_CTRL_GET_TIMEOUT);
148 memcpy(buf, kbuf, buflen);
149 kfree(kbuf);
151 if (rc < 0)
152 PWC_ERROR("recv_control_msg error %d req %02x val %04x\n",
153 rc, request, value);
154 return rc;
157 static inline int send_video_command(struct pwc_device *pdev,
158 int index, void *buf, int buflen)
160 return _send_control_msg(pdev,
161 SET_EP_STREAM_CTL,
162 VIDEO_OUTPUT_CONTROL_FORMATTER,
163 index,
164 buf, buflen);
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];
177 int ret, fps;
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 */
187 24 /* 30 */
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 */
196 7 /* 30 */
199 if (size < 0 || size > PSZ_CIF || frames < 4 || frames > 25)
200 return -EINVAL;
201 frames = frames2frames[frames];
202 fps = frames2table[frames];
203 pEntry = &Nala_table[size][fps];
204 if (pEntry->alternate == 0)
205 return -EINVAL;
207 memcpy(buf, pEntry->mode, 3);
208 ret = send_video_command(pdev, pdev->vendpoint, buf, 3);
209 if (ret < 0) {
210 PWC_DEBUG_MODULE("Failed to send video command... %d\n", ret);
211 return ret;
213 if (pEntry->compressed && pdev->pixfmt == V4L2_PIX_FMT_YUV420) {
214 ret = pwc_dec1_init(pdev, pdev->type, pdev->release, buf);
215 if (ret < 0)
216 return ret;
219 pdev->cmd_len = 3;
220 memcpy(pdev->cmd_buf, buf, 3);
222 /* Set various parameters */
223 pdev->vframes = frames;
224 pdev->vsize = size;
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;
233 else {
234 pdev->vbandlength = 704;
235 pdev->frame_size /= 3;
238 else
239 pdev->vbandlength = 0;
240 return 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;
248 int ret, fps;
250 if (size >= PSZ_MAX || frames < 5 || frames > 30 || compression < 0 || compression > 3)
251 return -EINVAL;
252 if (size == PSZ_VGA && frames > 15)
253 return -EINVAL;
254 fps = (frames / 5) - 1;
256 /* Find a supported framerate with progressively higher compression ratios
257 if the preferred ratio is not available.
259 pChoose = NULL;
260 while (compression <= 3) {
261 pChoose = &Timon_table[size][fps][compression];
262 if (pChoose->alternate != 0)
263 break;
264 compression++;
266 if (pChoose == NULL || pChoose->alternate == 0)
267 return -ENOENT; /* Not supported. */
269 memcpy(buf, pChoose->mode, 13);
270 if (snapshot)
271 buf[0] |= 0x80;
272 ret = send_video_command(pdev, pdev->vendpoint, buf, 13);
273 if (ret < 0)
274 return ret;
276 if (pChoose->bandlength > 0 && pdev->pixfmt == V4L2_PIX_FMT_YUV420) {
277 ret = pwc_dec23_init(pdev, pdev->type, buf);
278 if (ret < 0)
279 return ret;
282 pdev->cmd_len = 13;
283 memcpy(pdev->cmd_buf, buf, 13);
285 /* Set various parameters */
286 pdev->vframes = frames;
287 pdev->vsize = size;
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;
294 else
295 pdev->frame_size = (pdev->image.x * pdev->image.y * 12) / 8;
296 return 0;
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;
303 int fps, ret;
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)
308 return -EINVAL;
309 if (size == PSZ_VGA && frames > 15)
310 return -EINVAL;
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");
321 pChoose = &RawEntry;
323 else
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.
329 snapshot = 0;
330 while (compression <= 3) {
331 pChoose = &Kiara_table[size][fps][compression];
332 if (pChoose->alternate != 0)
333 break;
334 compression++;
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);
344 if (snapshot)
345 buf[0] |= 0x80;
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);
349 if (ret < 0)
350 return ret;
352 if (pChoose->bandlength > 0 && pdev->pixfmt == V4L2_PIX_FMT_YUV420) {
353 ret = pwc_dec23_init(pdev, pdev->type, buf);
354 if (ret < 0)
355 return ret;
358 pdev->cmd_len = 12;
359 memcpy(pdev->cmd_buf, buf, 12);
360 /* All set and go */
361 pdev->vframes = frames;
362 pdev->vsize = size;
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;
369 else
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);
373 return 0;
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)
388 int ret, size;
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);
392 if (size < 0) {
393 PWC_DEBUG_MODULE("Could not find suitable size.\n");
394 return -ERANGE;
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);
404 } else {
405 ret = set_video_mode_Timon(pdev, size, frames, compression, snapshot);
407 if (ret < 0) {
408 PWC_ERROR("Failed to set video mode %s@%d fps; return code = %d\n", size2name[size], frames, ret);
409 return 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);
417 return 0;
420 static unsigned int pwc_get_fps_Nala(struct pwc_device *pdev, unsigned int index, unsigned int size)
422 unsigned int i;
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];
429 return 0;
432 static unsigned int pwc_get_fps_Kiara(struct pwc_device *pdev, unsigned int index, unsigned int size)
434 unsigned int i;
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];
441 return 0;
444 static unsigned int pwc_get_fps_Timon(struct pwc_device *pdev, unsigned int index, unsigned int size)
446 unsigned int i;
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];
453 return 0;
456 unsigned int pwc_get_fps(struct pwc_device *pdev, unsigned int index, unsigned int size)
458 unsigned int ret;
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);
466 } else {
467 ret = pwc_get_fps_Timon(pdev, index, size);
470 return ret;
473 static void pwc_set_image_buffer_size(struct pwc_device *pdev)
475 int factor = 0;
477 /* for V4L2_PIX_FMT_YUV420 */
478 switch (pdev->pixfmt) {
479 case V4L2_PIX_FMT_YUV420:
480 factor = 6;
481 break;
482 case V4L2_PIX_FMT_PWC1:
483 case V4L2_PIX_FMT_PWC2:
484 factor = 6; /* can be uncompressed YUV420P */
485 break;
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
495 problem for YUV420P.
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)
503 int ret;
504 u8 buf;
506 ret = recv_control_msg(pdev, request, value, &buf, sizeof(buf));
507 if (ret < 0)
508 return ret;
510 *data = buf;
511 return 0;
514 int pwc_set_u8_ctrl(struct pwc_device *pdev, u8 request, u16 value, u8 data)
516 int ret;
518 ret = send_control_msg(pdev, request, value, &data, sizeof(data));
519 if (ret < 0)
520 return ret;
522 return 0;
525 int pwc_get_s8_ctrl(struct pwc_device *pdev, u8 request, u16 value, int *data)
527 int ret;
528 s8 buf;
530 ret = recv_control_msg(pdev, request, value, &buf, sizeof(buf));
531 if (ret < 0)
532 return ret;
534 *data = buf;
535 return 0;
538 int pwc_get_u16_ctrl(struct pwc_device *pdev, u8 request, u16 value, int *data)
540 int ret;
541 u8 buf[2];
543 ret = recv_control_msg(pdev, request, value, buf, sizeof(buf));
544 if (ret < 0)
545 return ret;
547 *data = (buf[1] << 8) | buf[0];
548 return 0;
551 int pwc_set_u16_ctrl(struct pwc_device *pdev, u8 request, u16 value, u16 data)
553 int ret;
554 u8 buf[2];
556 buf[0] = data & 0xff;
557 buf[1] = data >> 8;
558 ret = send_control_msg(pdev, request, value, buf, sizeof(buf));
559 if (ret < 0)
560 return ret;
562 return 0;
565 int pwc_button_ctrl(struct pwc_device *pdev, u16 value)
567 int ret;
569 ret = send_control_msg(pdev, SET_STATUS_CTL, value, NULL, 0);
570 if (ret < 0)
571 return ret;
573 return 0;
576 /* POWER */
577 void pwc_camera_power(struct pwc_device *pdev, int power)
579 char buf;
580 int r;
582 if (!pdev->power_save)
583 return;
585 if (pdev->type < 675 || (pdev->type < 730 && pdev->release < 6))
586 return; /* Not supported by Nala or Timon < release 6 */
588 if (power)
589 buf = 0x00; /* active */
590 else
591 buf = 0xFF; /* power save */
592 r = send_control_msg(pdev,
593 SET_STATUS_CTL, SET_POWER_SAVE_MODE_FORMATTER,
594 &buf, sizeof(buf));
596 if (r < 0)
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)
603 unsigned char buf;
605 /* useful range is 0x01..0x20 */
606 buf = speed / 0x7f0;
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)
613 unsigned char buf;
614 int ret;
616 ret = recv_control_msg(pdev,
617 GET_CHROM_CTL, AWB_CONTROL_SPEED_FORMATTER, &buf, sizeof(buf));
618 if (ret < 0)
619 return ret;
620 *value = buf * 0x7f0;
621 return 0;
625 static int pwc_set_wb_delay(struct pwc_device *pdev, int delay)
627 unsigned char buf;
629 /* useful range is 0x01..0x3F */
630 buf = (delay >> 10);
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)
637 unsigned char buf;
638 int ret;
640 ret = recv_control_msg(pdev,
641 GET_CHROM_CTL, AWB_CONTROL_DELAY_FORMATTER, &buf, sizeof(buf));
642 if (ret < 0)
643 return ret;
644 *value = buf << 10;
645 return 0;
649 int pwc_set_leds(struct pwc_device *pdev, int on_value, int off_value)
651 unsigned char buf[2];
652 int r;
654 if (pdev->type < 730)
655 return 0;
656 on_value /= 100;
657 off_value /= 100;
658 if (on_value < 0)
659 on_value = 0;
660 if (on_value > 0xff)
661 on_value = 0xff;
662 if (off_value < 0)
663 off_value = 0;
664 if (off_value > 0xff)
665 off_value = 0xff;
667 buf[0] = on_value;
668 buf[1] = off_value;
670 r = send_control_msg(pdev,
671 SET_STATUS_CTL, LED_FORMATTER, &buf, sizeof(buf));
672 if (r < 0)
673 PWC_ERROR("Failed to set LED on/off time (%d)\n", r);
675 return r;
678 static int pwc_get_leds(struct pwc_device *pdev, int *on_value, int *off_value)
680 unsigned char buf[2];
681 int ret;
683 if (pdev->type < 730) {
684 *on_value = -1;
685 *off_value = -1;
686 return 0;
689 ret = recv_control_msg(pdev,
690 GET_STATUS_CTL, LED_FORMATTER, &buf, sizeof(buf));
691 if (ret < 0)
692 return ret;
693 *on_value = buf[0] * 100;
694 *off_value = buf[1] * 100;
695 return 0;
698 static int _pwc_mpt_reset(struct pwc_device *pdev, int flags)
700 unsigned char buf;
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)
709 int ret;
710 ret = _pwc_mpt_reset(pdev, flags);
711 if (ret >= 0) {
712 pdev->pan_angle = 0;
713 pdev->tilt_angle = 0;
715 return ret;
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
725 the cam (??)
727 pan = 64 * pan / 100;
728 tilt = -64 * tilt / 100; /* positive tilt is down, which is not what the user would expect */
729 buf[0] = pan & 0xFF;
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)
739 int ret;
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)
746 return -ERANGE;
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)
753 return -ERANGE;
755 ret = _pwc_mpt_set_angle(pdev, pan, tilt);
756 if (ret >= 0) {
757 pdev->pan_angle += pan;
758 pdev->tilt_angle += tilt;
760 if (ret == -EPIPE) /* stall -> out of range */
761 ret = -ERANGE;
762 return ret;
765 static int pwc_mpt_get_status(struct pwc_device *pdev, struct pwc_mpt_status *status)
767 int ret;
768 unsigned char buf[5];
770 ret = recv_control_msg(pdev,
771 GET_MPT_CTL, PT_STATUS_FORMATTER, &buf, sizeof(buf));
772 if (ret < 0)
773 return ret;
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];
777 return 0;
780 #ifdef CONFIG_USB_PWC_DEBUG
781 int pwc_get_cmos_sensor(struct pwc_device *pdev, int *sensor)
783 unsigned char buf;
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 */
790 else
791 request = SENSOR_TYPE_FORMATTER2;
793 ret = recv_control_msg(pdev,
794 GET_STATUS_CTL, request, &buf, sizeof(buf));
795 if (ret < 0)
796 return ret;
797 if (pdev->type < 675)
798 *sensor = buf | 0x100;
799 else
800 *sensor = buf;
801 return 0;
803 #endif
805 /* End of Add-Ons */
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)
842 long ret = 0;
844 switch(cmd) {
845 case VIDIOCPWCRUSER:
846 ret = pwc_button_ctrl(pdev, RESTORE_USER_DEFAULTS_FORMATTER);
847 break;
849 case VIDIOCPWCSUSER:
850 ret = pwc_button_ctrl(pdev, SAVE_USER_DEFAULTS_FORMATTER);
851 break;
853 case VIDIOCPWCFACTORY:
854 ret = pwc_button_ctrl(pdev, RESTORE_FACTORY_DEFAULTS_FORMATTER);
855 break;
857 case VIDIOCPWCSCQUAL:
859 ARG_DEF(int, qual)
861 if (vb2_is_streaming(&pdev->vb_queue)) {
862 ret = -EBUSY;
863 break;
866 ARG_IN(qual)
867 if (ARGR(qual) < 0 || ARGR(qual) > 3)
868 ret = -EINVAL;
869 else
870 ret = pwc_set_video_mode(pdev, pdev->view.x, pdev->view.y, pdev->vframes, ARGR(qual), pdev->vsnapshot);
871 break;
874 case VIDIOCPWCGCQUAL:
876 ARG_DEF(int, qual)
878 ARGR(qual) = pdev->vcompression;
879 ARG_OUT(qual)
880 break;
883 case VIDIOCPWCPROBE:
885 ARG_DEF(struct pwc_probe, probe)
887 strcpy(ARGR(probe).name, pdev->vdev.name);
888 ARGR(probe).type = pdev->type;
889 ARG_OUT(probe)
890 break;
893 case VIDIOCPWCGSERIAL:
895 ARG_DEF(struct pwc_serial, serial)
897 strcpy(ARGR(serial).serial, pdev->serial);
898 ARG_OUT(serial)
899 break;
902 case VIDIOCPWCSAGC:
904 ARG_DEF(int, agc)
905 ARG_IN(agc)
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));
909 break;
912 case VIDIOCPWCGAGC:
914 ARG_DEF(int, agc)
915 if (v4l2_ctrl_g_ctrl(pdev->autogain))
916 ARGR(agc) = -1;
917 else
918 ARGR(agc) = pwc_ioctl_g_ctrl(pdev->gain);
919 ARG_OUT(agc)
920 break;
923 case VIDIOCPWCSSHUTTER:
925 ARG_DEF(int, shutter)
926 ARG_IN(shutter)
927 ret = v4l2_ctrl_s_ctrl(pdev->exposure_auto,
928 /* Menu idx 0 = auto, idx 1 = manual */
929 ARGR(shutter) >= 0);
930 if (ret == 0 && ARGR(shutter) >= 0)
931 ret = pwc_ioctl_s_ctrl(pdev->exposure, ARGR(shutter));
932 break;
935 case VIDIOCPWCSAWB:
937 ARG_DEF(struct pwc_whitebalance, wb)
938 ARG_IN(wb)
939 ret = v4l2_ctrl_s_ctrl(pdev->auto_white_balance,
940 ARGR(wb).mode);
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);
947 break;
950 case VIDIOCPWCGAWB:
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);
958 ARG_OUT(wb)
959 break;
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);
972 break;
975 case VIDIOCPWCGAWBSPEED:
977 ARG_DEF(struct pwc_wb_speed, wbs)
979 ret = pwc_get_wb_speed(pdev, &ARGR(wbs).control_speed);
980 if (ret < 0)
981 break;
982 ret = pwc_get_wb_delay(pdev, &ARGR(wbs).control_delay);
983 if (ret < 0)
984 break;
985 ARG_OUT(wbs)
986 break;
989 case VIDIOCPWCSLED:
991 ARG_DEF(struct pwc_leds, leds)
993 ARG_IN(leds)
994 ret = pwc_set_leds(pdev, ARGR(leds).led_on, ARGR(leds).led_off);
995 break;
999 case VIDIOCPWCGLED:
1001 ARG_DEF(struct pwc_leds, leds)
1003 ret = pwc_get_leds(pdev, &ARGR(leds).led_on, &ARGR(leds).led_off);
1004 ARG_OUT(leds)
1005 break;
1008 case VIDIOCPWCSCONTOUR:
1010 ARG_DEF(int, contour)
1011 ARG_IN(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));
1015 break;
1018 case VIDIOCPWCGCONTOUR:
1020 ARG_DEF(int, contour)
1021 if (v4l2_ctrl_g_ctrl(pdev->autocontour))
1022 ARGR(contour) = -1;
1023 else
1024 ARGR(contour) = pwc_ioctl_g_ctrl(pdev->contour);
1025 ARG_OUT(contour)
1026 break;
1029 case VIDIOCPWCSBACKLIGHT:
1031 ARG_DEF(int, backlight)
1032 ARG_IN(backlight)
1033 ret = v4l2_ctrl_s_ctrl(pdev->backlight, ARGR(backlight));
1034 break;
1037 case VIDIOCPWCGBACKLIGHT:
1039 ARG_DEF(int, backlight)
1040 ARGR(backlight) = v4l2_ctrl_g_ctrl(pdev->backlight);
1041 ARG_OUT(backlight)
1042 break;
1045 case VIDIOCPWCSFLICKER:
1047 ARG_DEF(int, flicker)
1048 ARG_IN(flicker)
1049 ret = v4l2_ctrl_s_ctrl(pdev->flicker, ARGR(flicker));
1050 break;
1053 case VIDIOCPWCGFLICKER:
1055 ARG_DEF(int, flicker)
1056 ARGR(flicker) = v4l2_ctrl_g_ctrl(pdev->flicker);
1057 ARG_OUT(flicker)
1058 break;
1061 case VIDIOCPWCSDYNNOISE:
1063 ARG_DEF(int, dynnoise)
1064 ARG_IN(dynnoise)
1065 ret = v4l2_ctrl_s_ctrl(pdev->noise_reduction, ARGR(dynnoise));
1066 break;
1069 case VIDIOCPWCGDYNNOISE:
1071 ARG_DEF(int, dynnoise)
1072 ARGR(dynnoise) = v4l2_ctrl_g_ctrl(pdev->noise_reduction);
1073 ARG_OUT(dynnoise);
1074 break;
1077 case VIDIOCPWCGREALSIZE:
1079 ARG_DEF(struct pwc_imagesize, size)
1081 ARGR(size).width = pdev->image.x;
1082 ARGR(size).height = pdev->image.y;
1083 ARG_OUT(size)
1084 break;
1087 case VIDIOCPWCMPTRESET:
1089 if (pdev->features & FEATURE_MOTOR_PANTILT)
1091 ARG_DEF(int, flags)
1093 ARG_IN(flags)
1094 ret = pwc_mpt_reset(pdev, ARGR(flags));
1096 else
1098 ret = -ENXIO;
1100 break;
1103 case VIDIOCPWCMPTGRANGE:
1105 if (pdev->features & FEATURE_MOTOR_PANTILT)
1107 ARG_DEF(struct pwc_mpt_range, range)
1109 ARGR(range) = pdev->angle_range;
1110 ARG_OUT(range)
1112 else
1114 ret = -ENXIO;
1116 break;
1119 case VIDIOCPWCMPTSANGLE:
1121 int new_pan, new_tilt;
1123 if (pdev->features & FEATURE_MOTOR_PANTILT)
1125 ARG_DEF(struct pwc_mpt_angles, angles)
1127 ARG_IN(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;
1136 else
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);
1143 else
1145 ret = -ENXIO;
1147 break;
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;
1160 ARG_OUT(angles)
1162 else
1164 ret = -ENXIO;
1166 break;
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));
1176 ARG_OUT(status)
1178 else
1180 ret = -ENXIO;
1182 break;
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;
1195 ARG_OUT(vcmd)
1196 break;
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);
1204 ARG_OUT(table)
1205 break;
1209 default:
1210 ret = -ENOIOCTLCMD;
1211 break;
1214 if (ret > 0)
1215 return 0;
1216 return ret;
1220 /* vim: set cinoptions= formatoptions=croql cindent shiftwidth=8 tabstop=8: */