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 /* 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 SET_MPT_CTL                     0x0D
 
  62 #define GET_MPT_CTL                     0x0E
 
  64 /* Selectors for the Luminance controls [GS]ET_LUM_CTL */
 
  65 #define AGC_MODE_FORMATTER                      0x2000
 
  66 #define PRESET_AGC_FORMATTER                    0x2100
 
  67 #define SHUTTER_MODE_FORMATTER                  0x2200
 
  68 #define PRESET_SHUTTER_FORMATTER                0x2300
 
  69 #define PRESET_CONTOUR_FORMATTER                0x2400
 
  70 #define AUTO_CONTOUR_FORMATTER                  0x2500
 
  71 #define BACK_LIGHT_COMPENSATION_FORMATTER       0x2600
 
  72 #define CONTRAST_FORMATTER                      0x2700
 
  73 #define DYNAMIC_NOISE_CONTROL_FORMATTER         0x2800
 
  74 #define FLICKERLESS_MODE_FORMATTER              0x2900
 
  75 #define AE_CONTROL_SPEED                        0x2A00
 
  76 #define BRIGHTNESS_FORMATTER                    0x2B00
 
  77 #define GAMMA_FORMATTER                         0x2C00
 
  79 /* Selectors for the Chrominance controls [GS]ET_CHROM_CTL */
 
  80 #define WB_MODE_FORMATTER                       0x1000
 
  81 #define AWB_CONTROL_SPEED_FORMATTER             0x1100
 
  82 #define AWB_CONTROL_DELAY_FORMATTER             0x1200
 
  83 #define PRESET_MANUAL_RED_GAIN_FORMATTER        0x1300
 
  84 #define PRESET_MANUAL_BLUE_GAIN_FORMATTER       0x1400
 
  85 #define COLOUR_MODE_FORMATTER                   0x1500
 
  86 #define SATURATION_MODE_FORMATTER1              0x1600
 
  87 #define SATURATION_MODE_FORMATTER2              0x1700
 
  89 /* Selectors for the Status controls [GS]ET_STATUS_CTL */
 
  90 #define SAVE_USER_DEFAULTS_FORMATTER            0x0200
 
  91 #define RESTORE_USER_DEFAULTS_FORMATTER         0x0300
 
  92 #define RESTORE_FACTORY_DEFAULTS_FORMATTER      0x0400
 
  93 #define READ_AGC_FORMATTER                      0x0500
 
  94 #define READ_SHUTTER_FORMATTER                  0x0600
 
  95 #define READ_RED_GAIN_FORMATTER                 0x0700
 
  96 #define READ_BLUE_GAIN_FORMATTER                0x0800
 
  97 #define SENSOR_TYPE_FORMATTER1                  0x0C00
 
  98 #define READ_RAW_Y_MEAN_FORMATTER               0x3100
 
  99 #define SET_POWER_SAVE_MODE_FORMATTER           0x3200
 
 100 #define MIRROR_IMAGE_FORMATTER                  0x3300
 
 101 #define LED_FORMATTER                           0x3400
 
 102 #define SENSOR_TYPE_FORMATTER2                  0x3700
 
 104 /* Formatters for the Video Endpoint controls [GS]ET_EP_STREAM_CTL */
 
 105 #define VIDEO_OUTPUT_CONTROL_FORMATTER          0x0100
 
 107 /* Formatters for the motorized pan & tilt [GS]ET_MPT_CTL */
 
 108 #define PT_RELATIVE_CONTROL_FORMATTER           0x01
 
 109 #define PT_RESET_CONTROL_FORMATTER              0x02
 
 110 #define PT_STATUS_FORMATTER                     0x03
 
 112 static char *size2name[PSZ_MAX] =
 
 124 /* Entries for the Nala (645/646) camera; the Nala doesn't have compression 
 
 125    preferences, so you either get compressed or non-compressed streams.
 
 127    An alternate value of 0 means this mode is not available at all.
 
 130 struct Nala_table_entry {
 
 131         char alternate;                 /* USB alternate setting */
 
 132         int compressed;                 /* Compressed yes/no */
 
 134         unsigned char mode[3];          /* precomputed mode table */
 
 137 static struct Nala_table_entry Nala_table[PSZ_MAX][8] =
 
 139 #include "pwc-nala.h"
 
 143 /****************************************************************************/
 
 146 #define SendControlMsg(request, value, buflen) \
 
 147         usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0), \
 
 149                 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, \
 
 154 #define RecvControlMsg(request, value, buflen) \
 
 155         usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0), \
 
 157                 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, \
 
 164 void pwc_hexdump(void *p, int len)
 
 170         s = (unsigned char *)p;
 
 173         Debug("Doing hexdump @ %p, %d bytes.\n", p, len);
 
 174         for (i = 0; i < len; i++) {
 
 175                 d += sprintf(d, "%02X ", *s++);
 
 176                 if ((i & 0xF) == 0xF) {
 
 187 static inline int send_video_command(struct usb_device *udev, int index, void *buf, int buflen)
 
 189         return usb_control_msg(udev,
 
 190                 usb_sndctrlpipe(udev, 0),
 
 192                 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
 
 193                 VIDEO_OUTPUT_CONTROL_FORMATTER,
 
 200 static inline int set_video_mode_Nala(struct pwc_device *pdev, int size, int frames)
 
 202         unsigned char buf[3];
 
 204         struct Nala_table_entry *pEntry;
 
 205         int frames2frames[31] =
 
 206         { /* closest match of framerate */
 
 207            0,  0,  0,  0,  4,  /*  0-4  */
 
 208            5,  5,  7,  7, 10,  /*  5-9  */
 
 209           10, 10, 12, 12, 15,  /* 10-14 */
 
 210           15, 15, 15, 20, 20,  /* 15-19 */
 
 211           20, 20, 20, 24, 24,  /* 20-24 */
 
 212           24, 24, 24, 24, 24,  /* 25-29 */
 
 215         int frames2table[31] = 
 
 216         { 0, 0, 0, 0, 0, /*  0-4  */
 
 217           1, 1, 1, 2, 2, /*  5-9  */
 
 218           3, 3, 4, 4, 4, /* 10-14 */
 
 219           5, 5, 5, 5, 5, /* 15-19 */
 
 220           6, 6, 6, 6, 7, /* 20-24 */
 
 221           7, 7, 7, 7, 7, /* 25-29 */
 
 225         if (size < 0 || size > PSZ_CIF || frames < 4 || frames > 25)
 
 227         frames = frames2frames[frames];
 
 228         fps = frames2table[frames];
 
 229         pEntry = &Nala_table[size][fps];
 
 230         if (pEntry->alternate == 0)
 
 233         if (pEntry->compressed)
 
 234                 return -ENOENT; /* Not supported. */
 
 236         memcpy(buf, pEntry->mode, 3);   
 
 237         ret = send_video_command(pdev->udev, pdev->vendpoint, buf, 3);
 
 239                 Debug("Failed to send video command... %d\n", ret);
 
 242         if (pEntry->compressed && pdev->vpalette != VIDEO_PALETTE_RAW)
 
 247 /*             pwc_dec1_init(pdev->type, pdev->release, buf, pdev->decompress_data); */
 
 257 /*             pwc_dec23_init(pdev->type, pdev->release, buf, pdev->decompress_data); */
 
 263         memcpy(pdev->cmd_buf, buf, 3);
 
 265         /* Set various parameters */
 
 266         pdev->vframes = frames;
 
 268         pdev->valternate = pEntry->alternate;
 
 269         pdev->image = pwc_image_sizes[size];
 
 270         pdev->frame_size = (pdev->image.x * pdev->image.y * 3) / 2;
 
 271         if (pEntry->compressed) {
 
 272                 if (pdev->release < 5) { /* 4 fold compression */
 
 273                         pdev->vbandlength = 528;
 
 274                         pdev->frame_size /= 4;
 
 277                         pdev->vbandlength = 704;
 
 278                         pdev->frame_size /= 3;
 
 282                 pdev->vbandlength = 0;
 
 287 static inline int set_video_mode_Timon(struct pwc_device *pdev, int size, int frames, int compression, int snapshot)
 
 289         unsigned char buf[13];
 
 290         const struct Timon_table_entry *pChoose;
 
 293         if (size >= PSZ_MAX || frames < 5 || frames > 30 || compression < 0 || compression > 3)
 
 295         if (size == PSZ_VGA && frames > 15)
 
 297         fps = (frames / 5) - 1;
 
 299         /* Find a supported framerate with progressively higher compression ratios
 
 300            if the preferred ratio is not available.
 
 303         while (compression <= 3) {
 
 304            pChoose = &Timon_table[size][fps][compression];
 
 305            if (pChoose->alternate != 0)
 
 309         if (pChoose == NULL || pChoose->alternate == 0)
 
 310                 return -ENOENT; /* Not supported. */
 
 312         memcpy(buf, pChoose->mode, 13);
 
 315         ret = send_video_command(pdev->udev, pdev->vendpoint, buf, 13);
 
 319 /*      if (pChoose->bandlength > 0 && pdev->vpalette != VIDEO_PALETTE_RAW)
 
 320            pwc_dec23_init(pdev->type, pdev->release, buf, pdev->decompress_data); */
 
 323         memcpy(pdev->cmd_buf, buf, 13);
 
 325         /* Set various parameters */
 
 326         pdev->vframes = frames;
 
 328         pdev->vsnapshot = snapshot;
 
 329         pdev->valternate = pChoose->alternate;
 
 330         pdev->image = pwc_image_sizes[size];
 
 331         pdev->vbandlength = pChoose->bandlength;
 
 332         if (pChoose->bandlength > 0)
 
 333                 pdev->frame_size = (pChoose->bandlength * pdev->image.y) / 4;
 
 335                 pdev->frame_size = (pdev->image.x * pdev->image.y * 12) / 8;
 
 340 static inline int set_video_mode_Kiara(struct pwc_device *pdev, int size, int frames, int compression, int snapshot)
 
 342         const struct Kiara_table_entry *pChoose = NULL;
 
 344         unsigned char buf[12];
 
 345         struct Kiara_table_entry RawEntry = {6, 773, 1272, {0xAD, 0xF4, 0x10, 0x27, 0xB6, 0x24, 0x96, 0x02, 0x30, 0x05, 0x03, 0x80}};
 
 347         if (size >= PSZ_MAX || frames < 5 || frames > 30 || compression < 0 || compression > 3)
 
 349         if (size == PSZ_VGA && frames > 15)
 
 351         fps = (frames / 5) - 1;
 
 353         /* special case: VGA @ 5 fps and snapshot is raw bayer mode */
 
 354         if (size == PSZ_VGA && frames == 5 && snapshot)
 
 356                 /* Only available in case the raw palette is selected or 
 
 357                    we have the decompressor available. This mode is 
 
 358                    only available in compressed form 
 
 360                 if (pdev->vpalette == VIDEO_PALETTE_RAW)
 
 362                         Info("Choosing VGA/5 BAYER mode (%d).\n", pdev->vpalette);
 
 367                         Info("VGA/5 BAYER mode _must_ have a decompressor available, or use RAW palette.\n");
 
 372                 /* Find a supported framerate with progressively higher compression ratios
 
 373                    if the preferred ratio is not available.
 
 374                    Skip this step when using RAW modes.
 
 376                 while (compression <= 3) {
 
 377                         pChoose = &Kiara_table[size][fps][compression];
 
 378                         if (pChoose->alternate != 0)
 
 383         if (pChoose == NULL || pChoose->alternate == 0)
 
 384                 return -ENOENT; /* Not supported. */
 
 386         Debug("Using alternate setting %d.\n", pChoose->alternate);
 
 388         /* usb_control_msg won't take staticly allocated arrays as argument?? */
 
 389         memcpy(buf, pChoose->mode, 12);
 
 393         /* Firmware bug: video endpoint is 5, but commands are sent to endpoint 4 */
 
 394         ret = send_video_command(pdev->udev, 4 /* pdev->vendpoint */, buf, 12);
 
 398 /*      if (pChoose->bandlength > 0 && pdev->vpalette != VIDEO_PALETTE_RAW)
 
 399           pwc_dec23_init(pdev->type, pdev->release, buf, pdev->decompress_data); */
 
 402         memcpy(pdev->cmd_buf, buf, 12);
 
 404         pdev->vframes = frames;
 
 406         pdev->vsnapshot = snapshot;
 
 407         pdev->valternate = pChoose->alternate;
 
 408         pdev->image = pwc_image_sizes[size];
 
 409         pdev->vbandlength = pChoose->bandlength;
 
 410         if (pdev->vbandlength > 0)
 
 411                 pdev->frame_size = (pdev->vbandlength * pdev->image.y) / 4;
 
 413                 pdev->frame_size = (pdev->image.x * pdev->image.y * 12) / 8;
 
 419 static void pwc_set_image_buffer_size(struct pwc_device *pdev)
 
 421         int i, factor = 0, filler = 0;
 
 423         /* for PALETTE_YUV420P */
 
 424         switch(pdev->vpalette)
 
 426         case VIDEO_PALETTE_YUV420P:
 
 430         case VIDEO_PALETTE_RAW:
 
 431                 factor = 6; /* can be uncompressed YUV420P */
 
 436         /* Set sizes in bytes */
 
 437         pdev->image.size = pdev->image.x * pdev->image.y * factor / 4;
 
 438         pdev->view.size  = pdev->view.x  * pdev->view.y  * factor / 4;
 
 440         /* Align offset, or you'll get some very weird results in
 
 441            YUV420 mode... x must be multiple of 4 (to get the Y's in
 
 442            place), and y even (or you'll mixup U & V). This is less of a
 
 445         pdev->offset.x = ((pdev->view.x - pdev->image.x) / 2) & 0xFFFC;
 
 446         pdev->offset.y = ((pdev->view.y - pdev->image.y) / 2) & 0xFFFE;
 
 448         /* Fill buffers with gray or black */
 
 449         for (i = 0; i < MAX_IMAGES; i++) {
 
 450                 if (pdev->image_ptr[i] != NULL)
 
 451                         memset(pdev->image_ptr[i], filler, pdev->view.size);
 
 458    @pdev: device structure
 
 459    @width: viewport width
 
 460    @height: viewport height
 
 461    @frame: framerate, in fps
 
 462    @compression: preferred compression ratio
 
 463    @snapshot: snapshot mode or streaming
 
 465 int pwc_set_video_mode(struct pwc_device *pdev, int width, int height, int frames, int compression, int snapshot)
 
 469         Trace(TRACE_FLOW, "set_video_mode(%dx%d @ %d, palette %d).\n", width, height, frames, pdev->vpalette);
 
 470         size = pwc_decode_size(pdev, width, height);
 
 472                 Debug("Could not find suitable size.\n");
 
 475         Debug("decode_size = %d.\n", size);
 
 481                 ret = set_video_mode_Nala(pdev, size, frames);
 
 487                 ret = set_video_mode_Timon(pdev, size, frames, compression, snapshot);
 
 494                 ret = set_video_mode_Kiara(pdev, size, frames, compression, snapshot);
 
 499                         Info("Video mode %s@%d fps is only supported with the decompressor module (pwcx).\n", size2name[size], frames);
 
 501                         Err("Failed to set video mode %s@%d fps; return code = %d\n", size2name[size], frames, ret);
 
 505         pdev->view.x = width;
 
 506         pdev->view.y = height;
 
 507         pdev->frame_total_size = pdev->frame_size + pdev->frame_header_size + pdev->frame_trailer_size;
 
 508         pwc_set_image_buffer_size(pdev);
 
 509         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);
 
 516 int pwc_get_brightness(struct pwc_device *pdev)
 
 521         ret = RecvControlMsg(GET_LUM_CTL, BRIGHTNESS_FORMATTER, 1);     
 
 527 int pwc_set_brightness(struct pwc_device *pdev, int value)
 
 535         buf = (value >> 9) & 0x7f;
 
 536         return SendControlMsg(SET_LUM_CTL, BRIGHTNESS_FORMATTER, 1);
 
 541 int pwc_get_contrast(struct pwc_device *pdev)
 
 546         ret = RecvControlMsg(GET_LUM_CTL, CONTRAST_FORMATTER, 1);
 
 552 int pwc_set_contrast(struct pwc_device *pdev, int value)
 
 560         buf = (value >> 10) & 0x3f;
 
 561         return SendControlMsg(SET_LUM_CTL, CONTRAST_FORMATTER, 1);
 
 566 int pwc_get_gamma(struct pwc_device *pdev)
 
 571         ret = RecvControlMsg(GET_LUM_CTL, GAMMA_FORMATTER, 1);
 
 577 int pwc_set_gamma(struct pwc_device *pdev, int value)
 
 585         buf = (value >> 11) & 0x1f;
 
 586         return SendControlMsg(SET_LUM_CTL, GAMMA_FORMATTER, 1);
 
 592 int pwc_get_saturation(struct pwc_device *pdev)
 
 597         if (pdev->type < 675)
 
 599         ret = RecvControlMsg(GET_CHROM_CTL, pdev->type < 730 ? SATURATION_MODE_FORMATTER2 : SATURATION_MODE_FORMATTER1, 1);
 
 602         return 32768 + buf * 327;
 
 605 int pwc_set_saturation(struct pwc_device *pdev, int value)
 
 609         if (pdev->type < 675)
 
 615         /* saturation ranges from -100 to +100 */
 
 616         buf = (value - 32768) / 327;
 
 617         return SendControlMsg(SET_CHROM_CTL, pdev->type < 730 ? SATURATION_MODE_FORMATTER2 : SATURATION_MODE_FORMATTER1, 1);
 
 622 static inline int pwc_set_agc(struct pwc_device *pdev, int mode, int value)
 
 628                 buf = 0x0; /* auto */
 
 630                 buf = 0xff; /* fixed */
 
 632         ret = SendControlMsg(SET_LUM_CTL, AGC_MODE_FORMATTER, 1);
 
 634         if (!mode && ret >= 0) {
 
 639                 buf = (value >> 10) & 0x3F;
 
 640                 ret = SendControlMsg(SET_LUM_CTL, PRESET_AGC_FORMATTER, 1);
 
 647 static inline int pwc_get_agc(struct pwc_device *pdev, int *value)
 
 652         ret = RecvControlMsg(GET_LUM_CTL, AGC_MODE_FORMATTER, 1);
 
 656         if (buf != 0) { /* fixed */
 
 657                 ret = RecvControlMsg(GET_LUM_CTL, PRESET_AGC_FORMATTER, 1);
 
 662                 *value = (buf << 10);           
 
 665                 ret = RecvControlMsg(GET_STATUS_CTL, READ_AGC_FORMATTER, 1);
 
 668                 /* Gah... this value ranges from 0x00 ... 0x9F */
 
 671                 *value = -(48 + buf * 409);
 
 677 static inline int pwc_set_shutter_speed(struct pwc_device *pdev, int mode, int value)
 
 684                 buf[0] = 0x0;   /* auto */
 
 686                 buf[0] = 0xff; /* fixed */
 
 688         ret = SendControlMsg(SET_LUM_CTL, SHUTTER_MODE_FORMATTER, 1);
 
 690         if (!mode && ret >= 0) {
 
 699                         /* speed ranges from 0x0 to 0x290 (656) */
 
 700                         speed = (value / 100);
 
 702                         buf[0] = speed & 0xff;
 
 708                         /* speed seems to range from 0x0 to 0xff */
 
 714                 ret = SendControlMsg(SET_LUM_CTL, PRESET_SHUTTER_FORMATTER, 2);
 
 722 int pwc_camera_power(struct pwc_device *pdev, int power)
 
 726         if (pdev->type < 675 || (pdev->type < 730 && pdev->release < 6))
 
 727                 return 0;       /* Not supported by Nala or Timon < release 6 */
 
 730                 buf = 0x00; /* active */
 
 732                 buf = 0xFF; /* power save */
 
 733         return SendControlMsg(SET_STATUS_CTL, SET_POWER_SAVE_MODE_FORMATTER, 1);
 
 740 static inline int pwc_restore_user(struct pwc_device *pdev)
 
 742         char buf; /* dummy */
 
 743         return SendControlMsg(SET_STATUS_CTL, RESTORE_USER_DEFAULTS_FORMATTER, 0);
 
 746 static inline int pwc_save_user(struct pwc_device *pdev)
 
 748         char buf; /* dummy */
 
 749         return SendControlMsg(SET_STATUS_CTL, SAVE_USER_DEFAULTS_FORMATTER, 0);
 
 752 static inline int pwc_restore_factory(struct pwc_device *pdev)
 
 754         char buf; /* dummy */
 
 755         return SendControlMsg(SET_STATUS_CTL, RESTORE_FACTORY_DEFAULTS_FORMATTER, 0);
 
 758  /* ************************************************* */
 
 759  /* Patch by Alvarado: (not in the original version   */
 
 762   * the camera recognizes modes from 0 to 4:
 
 764   * 00: indoor (incandescant lighting)
 
 765   * 01: outdoor (sunlight)
 
 766   * 02: fluorescent lighting
 
 770 static inline int pwc_set_awb(struct pwc_device *pdev, int mode)
 
 781         buf = mode & 0x07; /* just the lowest three bits */
 
 783         ret = SendControlMsg(SET_CHROM_CTL, WB_MODE_FORMATTER, 1);
 
 790 static inline int pwc_get_awb(struct pwc_device *pdev)
 
 795         ret = RecvControlMsg(GET_CHROM_CTL, WB_MODE_FORMATTER, 1);
 
 802 static inline int pwc_set_red_gain(struct pwc_device *pdev, int value)
 
 810         /* only the msb is considered */
 
 812         return SendControlMsg(SET_CHROM_CTL, PRESET_MANUAL_RED_GAIN_FORMATTER, 1);
 
 815 static inline int pwc_get_red_gain(struct pwc_device *pdev, int *value)
 
 820         ret = RecvControlMsg(GET_CHROM_CTL, PRESET_MANUAL_RED_GAIN_FORMATTER, 1);
 
 828 static inline int pwc_set_blue_gain(struct pwc_device *pdev, int value)
 
 836         /* only the msb is considered */
 
 838         return SendControlMsg(SET_CHROM_CTL, PRESET_MANUAL_BLUE_GAIN_FORMATTER, 1);
 
 841 static inline int pwc_get_blue_gain(struct pwc_device *pdev, int *value)
 
 846         ret = RecvControlMsg(GET_CHROM_CTL, PRESET_MANUAL_BLUE_GAIN_FORMATTER, 1);
 
 854 /* The following two functions are different, since they only read the
 
 855    internal red/blue gains, which may be different from the manual 
 
 856    gains set or read above.
 
 858 static inline int pwc_read_red_gain(struct pwc_device *pdev, int *value)
 
 863         ret = RecvControlMsg(GET_STATUS_CTL, READ_RED_GAIN_FORMATTER, 1);
 
 870 static inline int pwc_read_blue_gain(struct pwc_device *pdev, int *value)
 
 875         ret = RecvControlMsg(GET_STATUS_CTL, READ_BLUE_GAIN_FORMATTER, 1);
 
 883 static inline int pwc_set_wb_speed(struct pwc_device *pdev, int speed)
 
 887         /* useful range is 0x01..0x20 */
 
 889         return SendControlMsg(SET_CHROM_CTL, AWB_CONTROL_SPEED_FORMATTER, 1);
 
 892 static inline int pwc_get_wb_speed(struct pwc_device *pdev, int *value)
 
 897         ret = RecvControlMsg(GET_CHROM_CTL, AWB_CONTROL_SPEED_FORMATTER, 1);
 
 900         *value = buf * 0x7f0;
 
 905 static inline int pwc_set_wb_delay(struct pwc_device *pdev, int delay)
 
 909         /* useful range is 0x01..0x3F */
 
 911         return SendControlMsg(SET_CHROM_CTL, AWB_CONTROL_DELAY_FORMATTER, 1);
 
 914 static inline int pwc_get_wb_delay(struct pwc_device *pdev, int *value)
 
 919         ret = RecvControlMsg(GET_CHROM_CTL, AWB_CONTROL_DELAY_FORMATTER, 1);
 
 927 int pwc_set_leds(struct pwc_device *pdev, int on_value, int off_value)
 
 929         unsigned char buf[2];
 
 931         if (pdev->type < 730)
 
 941         if (off_value > 0xff)
 
 947         return SendControlMsg(SET_STATUS_CTL, LED_FORMATTER, 2);
 
 950 static int pwc_get_leds(struct pwc_device *pdev, int *on_value, int *off_value)
 
 952         unsigned char buf[2];
 
 955         if (pdev->type < 730) {
 
 961         ret = RecvControlMsg(GET_STATUS_CTL, LED_FORMATTER, 2);
 
 964         *on_value = buf[0] * 100;
 
 965         *off_value = buf[1] * 100;
 
 969 static inline int pwc_set_contour(struct pwc_device *pdev, int contour)
 
 975                 buf = 0xff; /* auto contour on */
 
 977                 buf = 0x0; /* auto contour off */
 
 978         ret = SendControlMsg(SET_LUM_CTL, AUTO_CONTOUR_FORMATTER, 1);
 
 984         if (contour > 0xffff)
 
 987         buf = (contour >> 10); /* contour preset is [0..3f] */
 
 988         ret = SendControlMsg(SET_LUM_CTL, PRESET_CONTOUR_FORMATTER, 1);
 
 994 static inline int pwc_get_contour(struct pwc_device *pdev, int *contour)
 
 999         ret = RecvControlMsg(GET_LUM_CTL, AUTO_CONTOUR_FORMATTER, 1);
 
1004                 /* auto mode off, query current preset value */
 
1005                 ret = RecvControlMsg(GET_LUM_CTL, PRESET_CONTOUR_FORMATTER, 1);
 
1008                 *contour = buf << 10;
 
1016 static inline int pwc_set_backlight(struct pwc_device *pdev, int backlight)
 
1024         return SendControlMsg(SET_LUM_CTL, BACK_LIGHT_COMPENSATION_FORMATTER, 1);
 
1027 static inline int pwc_get_backlight(struct pwc_device *pdev, int *backlight)
 
1032         ret = RecvControlMsg(GET_LUM_CTL, BACK_LIGHT_COMPENSATION_FORMATTER, 1);
 
1040 static inline int pwc_set_flicker(struct pwc_device *pdev, int flicker)
 
1048         return SendControlMsg(SET_LUM_CTL, FLICKERLESS_MODE_FORMATTER, 1);
 
1051 static inline int pwc_get_flicker(struct pwc_device *pdev, int *flicker)
 
1056         ret = RecvControlMsg(GET_LUM_CTL, FLICKERLESS_MODE_FORMATTER, 1);
 
1064 static inline int pwc_set_dynamic_noise(struct pwc_device *pdev, int noise)
 
1073         return SendControlMsg(SET_LUM_CTL, DYNAMIC_NOISE_CONTROL_FORMATTER, 1);
 
1076 static inline int pwc_get_dynamic_noise(struct pwc_device *pdev, int *noise)
 
1081         ret = RecvControlMsg(GET_LUM_CTL, DYNAMIC_NOISE_CONTROL_FORMATTER, 1);
 
1088 static int pwc_mpt_reset(struct pwc_device *pdev, int flags)
 
1092         buf = flags & 0x03; // only lower two bits are currently used
 
1093         return SendControlMsg(SET_MPT_CTL, PT_RESET_CONTROL_FORMATTER, 1);
 
1096 static inline int pwc_mpt_set_angle(struct pwc_device *pdev, int pan, int tilt)
 
1098         unsigned char buf[4];
 
1100         /* set new relative angle; angles are expressed in degrees * 100,
 
1101            but cam as .5 degree resolution, hence divide by 200. Also
 
1102            the angle must be multiplied by 64 before it's send to
 
1105         pan  =  64 * pan  / 100;
 
1106         tilt = -64 * tilt / 100; /* positive tilt is down, which is not what the user would expect */
 
1107         buf[0] = pan & 0xFF;
 
1108         buf[1] = (pan >> 8) & 0xFF;
 
1109         buf[2] = tilt & 0xFF;
 
1110         buf[3] = (tilt >> 8) & 0xFF;
 
1111         return SendControlMsg(SET_MPT_CTL, PT_RELATIVE_CONTROL_FORMATTER, 4);
 
1114 static inline int pwc_mpt_get_status(struct pwc_device *pdev, struct pwc_mpt_status *status)
 
1117         unsigned char buf[5];
 
1119         ret = RecvControlMsg(GET_MPT_CTL, PT_STATUS_FORMATTER, 5);
 
1122         status->status = buf[0] & 0x7; // 3 bits are used for reporting
 
1123         status->time_pan = (buf[1] << 8) + buf[2];
 
1124         status->time_tilt = (buf[3] << 8) + buf[4];
 
1129 int pwc_get_cmos_sensor(struct pwc_device *pdev, int *sensor)
 
1132         int ret = -1, request;
 
1134         if (pdev->type < 675)
 
1135                 request = SENSOR_TYPE_FORMATTER1;
 
1136         else if (pdev->type < 730)
 
1137                 return -1; /* The Vesta series doesn't have this call */
 
1139                 request = SENSOR_TYPE_FORMATTER2;
 
1141         ret = RecvControlMsg(GET_STATUS_CTL, request, 1);
 
1144         if (pdev->type < 675)
 
1145                 *sensor = buf | 0x100;
 
1152  /* End of Add-Ons                                    */
 
1153  /* ************************************************* */
 
1155 /* Linux 2.5.something and 2.6 pass direct pointers to arguments of
 
1156    ioctl() calls. With 2.4, you have to do tedious copy_from_user()
 
1157    and copy_to_user() calls. With these macros we circumvent this,
 
1158    and let me maintain only one source file. The functionality is
 
1159    exactly the same otherwise.
 
1162 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
 
1164 /* define local variable for arg */
 
1165 #define ARG_DEF(ARG_type, ARG_name)\
 
1166         ARG_type *ARG_name = arg;
 
1167 /* copy arg to local variable */        
 
1168 #define ARG_IN(ARG_name) /* nothing */
 
1169 /* argument itself (referenced) */
 
1170 #define ARGR(ARG_name) (*ARG_name)
 
1171 /* argument address */
 
1172 #define ARGA(ARG_name) ARG_name
 
1173 /* copy local variable to arg */
 
1174 #define ARG_OUT(ARG_name) /* nothing */
 
1178 #define ARG_DEF(ARG_type, ARG_name)\
 
1180 #define ARG_IN(ARG_name)\
 
1181         if (copy_from_user(&ARG_name, arg, sizeof(ARG_name))) {\
 
1185 #define ARGR(ARG_name) ARG_name
 
1186 #define ARGA(ARG_name) &ARG_name
 
1187 #define ARG_OUT(ARG_name)\
 
1188         if (copy_to_user(arg, &ARG_name, sizeof(ARG_name))) {\
 
1195 int pwc_ioctl(struct pwc_device *pdev, unsigned int cmd, void *arg)
 
1200         case VIDIOCPWCRUSER:
 
1202                 if (pwc_restore_user(pdev))
 
1207         case VIDIOCPWCSUSER:
 
1209                 if (pwc_save_user(pdev))
 
1214         case VIDIOCPWCFACTORY:
 
1216                 if (pwc_restore_factory(pdev))
 
1221         case VIDIOCPWCSCQUAL:
 
1226                 if (ARGR(qual) < 0 || ARGR(qual) > 3)
 
1229                         ret = pwc_try_video_mode(pdev, pdev->view.x, pdev->view.y, pdev->vframes, ARGR(qual), pdev->vsnapshot);
 
1231                         pdev->vcompression = ARGR(qual);
 
1235         case VIDIOCPWCGCQUAL:
 
1239                 ARGR(qual) = pdev->vcompression;
 
1244         case VIDIOCPWCPROBE:
 
1246                 ARG_DEF(struct pwc_probe, probe)
 
1248                 strcpy(ARGR(probe).name, pdev->vdev->name);
 
1249                 ARGR(probe).type = pdev->type;
 
1254         case VIDIOCPWCGSERIAL:
 
1256                 ARG_DEF(struct pwc_serial, serial)
 
1258                 strcpy(ARGR(serial).serial, pdev->serial);
 
1268                 if (pwc_set_agc(pdev, ARGR(agc) < 0 ? 1 : 0, ARGR(agc)))
 
1277                 if (pwc_get_agc(pdev, ARGA(agc)))
 
1283         case VIDIOCPWCSSHUTTER:
 
1285                 ARG_DEF(int, shutter_speed)
 
1287                 ARG_IN(shutter_speed)
 
1288                 ret = pwc_set_shutter_speed(pdev, ARGR(shutter_speed) < 0 ? 1 : 0, ARGR(shutter_speed));
 
1294                 ARG_DEF(struct pwc_whitebalance, wb)
 
1297                 ret = pwc_set_awb(pdev, ARGR(wb).mode);
 
1298                 if (ret >= 0 && ARGR(wb).mode == PWC_WB_MANUAL) {
 
1299                         pwc_set_red_gain(pdev, ARGR(wb).manual_red);
 
1300                         pwc_set_blue_gain(pdev, ARGR(wb).manual_blue);
 
1307                 ARG_DEF(struct pwc_whitebalance, wb)
 
1309                 memset(ARGA(wb), 0, sizeof(struct pwc_whitebalance));
 
1310                 ARGR(wb).mode = pwc_get_awb(pdev);
 
1311                 if (ARGR(wb).mode < 0)
 
1314                         if (ARGR(wb).mode == PWC_WB_MANUAL) {
 
1315                                 ret = pwc_get_red_gain(pdev, &ARGR(wb).manual_red);
 
1318                                 ret = pwc_get_blue_gain(pdev, &ARGR(wb).manual_blue);
 
1322                         if (ARGR(wb).mode == PWC_WB_AUTO) {
 
1323                                 ret = pwc_read_red_gain(pdev, &ARGR(wb).read_red);
 
1326                                 ret =pwc_read_blue_gain(pdev, &ARGR(wb).read_blue);
 
1335         case VIDIOCPWCSAWBSPEED:
 
1337                 ARG_DEF(struct pwc_wb_speed, wbs)
 
1339                 if (ARGR(wbs).control_speed > 0) {
 
1340                         ret = pwc_set_wb_speed(pdev, ARGR(wbs).control_speed);
 
1342                 if (ARGR(wbs).control_delay > 0) {
 
1343                         ret = pwc_set_wb_delay(pdev, ARGR(wbs).control_delay);
 
1348         case VIDIOCPWCGAWBSPEED:
 
1350                 ARG_DEF(struct pwc_wb_speed, wbs)
 
1352                 ret = pwc_get_wb_speed(pdev, &ARGR(wbs).control_speed);
 
1355                 ret = pwc_get_wb_delay(pdev, &ARGR(wbs).control_delay);
 
1364                 ARG_DEF(struct pwc_leds, leds)
 
1367                 ret = pwc_set_leds(pdev, ARGR(leds).led_on, ARGR(leds).led_off);
 
1374                 ARG_DEF(struct pwc_leds, leds)
 
1376                 ret = pwc_get_leds(pdev, &ARGR(leds).led_on, &ARGR(leds).led_off);
 
1381         case VIDIOCPWCSCONTOUR:
 
1383                 ARG_DEF(int, contour)
 
1386                 ret = pwc_set_contour(pdev, ARGR(contour));
 
1390         case VIDIOCPWCGCONTOUR:
 
1392                 ARG_DEF(int, contour)
 
1394                 ret = pwc_get_contour(pdev, ARGA(contour));
 
1399         case VIDIOCPWCSBACKLIGHT:
 
1401                 ARG_DEF(int, backlight)
 
1404                 ret = pwc_set_backlight(pdev, ARGR(backlight));
 
1408         case VIDIOCPWCGBACKLIGHT:
 
1410                 ARG_DEF(int, backlight)
 
1412                 ret = pwc_get_backlight(pdev, ARGA(backlight));
 
1417         case VIDIOCPWCSFLICKER:
 
1419                 ARG_DEF(int, flicker)
 
1422                 ret = pwc_set_flicker(pdev, ARGR(flicker));
 
1426         case VIDIOCPWCGFLICKER:
 
1428                 ARG_DEF(int, flicker)
 
1430                 ret = pwc_get_flicker(pdev, ARGA(flicker));
 
1435         case VIDIOCPWCSDYNNOISE:
 
1437                 ARG_DEF(int, dynnoise)
 
1440                 ret = pwc_set_dynamic_noise(pdev, ARGR(dynnoise));
 
1444         case VIDIOCPWCGDYNNOISE:
 
1446                 ARG_DEF(int, dynnoise)
 
1448                 ret = pwc_get_dynamic_noise(pdev, ARGA(dynnoise));
 
1453         case VIDIOCPWCGREALSIZE:
 
1455                 ARG_DEF(struct pwc_imagesize, size)
 
1457                 ARGR(size).width = pdev->image.x;
 
1458                 ARGR(size).height = pdev->image.y;
 
1463         case VIDIOCPWCMPTRESET:
 
1465                 if (pdev->features & FEATURE_MOTOR_PANTILT)
 
1470                         ret = pwc_mpt_reset(pdev, ARGR(flags));
 
1473                                 pdev->pan_angle = 0;
 
1474                                 pdev->tilt_angle = 0;
 
1484         case VIDIOCPWCMPTGRANGE:
 
1486                 if (pdev->features & FEATURE_MOTOR_PANTILT)
 
1488                         ARG_DEF(struct pwc_mpt_range, range)
 
1490                         ARGR(range) = pdev->angle_range;
 
1500         case VIDIOCPWCMPTSANGLE:
 
1502                 int new_pan, new_tilt;
 
1504                 if (pdev->features & FEATURE_MOTOR_PANTILT)
 
1506                         ARG_DEF(struct pwc_mpt_angles, angles)
 
1509                         /* The camera can only set relative angles, so
 
1510                            do some calculations when getting an absolute angle .
 
1512                         if (ARGR(angles).absolute)
 
1514                                 new_pan  = ARGR(angles).pan; 
 
1515                                 new_tilt = ARGR(angles).tilt;
 
1519                                 new_pan  = pdev->pan_angle  + ARGR(angles).pan;
 
1520                                 new_tilt = pdev->tilt_angle + ARGR(angles).tilt;
 
1522                         /* check absolute ranges */
 
1523                         if (new_pan  < pdev->angle_range.pan_min  ||
 
1524                             new_pan  > pdev->angle_range.pan_max  ||
 
1525                             new_tilt < pdev->angle_range.tilt_min ||
 
1526                             new_tilt > pdev->angle_range.tilt_max)
 
1532                                 /* go to relative range, check again */
 
1533                                 new_pan  -= pdev->pan_angle;
 
1534                                 new_tilt -= pdev->tilt_angle;
 
1535                                 /* angles are specified in degrees * 100, thus the limit = 36000 */
 
1536                                 if (new_pan < -36000 || new_pan > 36000 || new_tilt < -36000 || new_tilt > 36000)
 
1539                         if (ret == 0) /* no errors so far */
 
1541                                 ret = pwc_mpt_set_angle(pdev, new_pan, new_tilt);
 
1544                                         pdev->pan_angle  += new_pan;
 
1545                                         pdev->tilt_angle += new_tilt;
 
1547                                 if (ret == -EPIPE) /* stall -> out of range */
 
1558         case VIDIOCPWCMPTGANGLE:
 
1561                 if (pdev->features & FEATURE_MOTOR_PANTILT)
 
1563                         ARG_DEF(struct pwc_mpt_angles, angles)
 
1565                         ARGR(angles).absolute = 1;
 
1566                         ARGR(angles).pan  = pdev->pan_angle;
 
1567                         ARGR(angles).tilt = pdev->tilt_angle;
 
1577         case VIDIOCPWCMPTSTATUS:
 
1579                 if (pdev->features & FEATURE_MOTOR_PANTILT)
 
1581                         ARG_DEF(struct pwc_mpt_status, status)
 
1583                         ret = pwc_mpt_get_status(pdev, ARGA(status));
 
1593         case VIDIOCPWCGVIDCMD:
 
1595                 ARG_DEF(struct pwc_video_command, cmd);
 
1597                 ARGR(cmd).type = pdev->type;
 
1598                 ARGR(cmd).release = pdev->release;
 
1599                 ARGR(cmd).command_len = pdev->cmd_len;
 
1600                 memcpy(&ARGR(cmd).command_buf, pdev->cmd_buf, pdev->cmd_len);
 
1601                 ARGR(cmd).bandlength = pdev->vbandlength;
 
1602                 ARGR(cmd).frame_size = pdev->frame_size;
 
1607         case VIDIOCPWCGVIDTABLE:
 
1609                 ARG_DEF(struct pwc_table_init_buffer, table);
 
1610                 ARGR(table).len = pdev->cmd_len;
 
1611                 memcpy(&ARGR(table).buffer, pdev->decompress_data, pdev->decompressor->table_size);