VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / usb / media / pwc-ctrl.c
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. (webcam@smcc.demon.nl)
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 /*
22    Changes
23    2001/08/03  Alvarado   Added methods for changing white balance and 
24                           red/green gains
25  */
26
27 /* Control functions for the cam; brightness, contrast, video mode, etc. */
28
29 #ifdef __KERNEL__
30 #include <asm/uaccess.h> 
31 #endif
32 #include <asm/errno.h>
33 #include <linux/version.h>
34  
35 #include "pwc.h"
36 #include "pwc-ioctl.h"
37 #include "pwc-uncompress.h"
38
39 /* Request types: video */
40 #define SET_LUM_CTL                     0x01
41 #define GET_LUM_CTL                     0x02
42 #define SET_CHROM_CTL                   0x03
43 #define GET_CHROM_CTL                   0x04
44 #define SET_STATUS_CTL                  0x05
45 #define GET_STATUS_CTL                  0x06
46 #define SET_EP_STREAM_CTL               0x07
47 #define GET_EP_STREAM_CTL               0x08
48 #define SET_MPT_CTL                     0x0D
49 #define GET_MPT_CTL                     0x0E
50
51 /* Selectors for the Luminance controls [GS]ET_LUM_CTL */
52 #define AGC_MODE_FORMATTER                      0x2000
53 #define PRESET_AGC_FORMATTER                    0x2100
54 #define SHUTTER_MODE_FORMATTER                  0x2200
55 #define PRESET_SHUTTER_FORMATTER                0x2300
56 #define PRESET_CONTOUR_FORMATTER                0x2400
57 #define AUTO_CONTOUR_FORMATTER                  0x2500
58 #define BACK_LIGHT_COMPENSATION_FORMATTER       0x2600
59 #define CONTRAST_FORMATTER                      0x2700
60 #define DYNAMIC_NOISE_CONTROL_FORMATTER         0x2800
61 #define FLICKERLESS_MODE_FORMATTER              0x2900
62 #define AE_CONTROL_SPEED                        0x2A00
63 #define BRIGHTNESS_FORMATTER                    0x2B00
64 #define GAMMA_FORMATTER                         0x2C00
65
66 /* Selectors for the Chrominance controls [GS]ET_CHROM_CTL */
67 #define WB_MODE_FORMATTER                       0x1000
68 #define AWB_CONTROL_SPEED_FORMATTER             0x1100
69 #define AWB_CONTROL_DELAY_FORMATTER             0x1200
70 #define PRESET_MANUAL_RED_GAIN_FORMATTER        0x1300
71 #define PRESET_MANUAL_BLUE_GAIN_FORMATTER       0x1400
72 #define COLOUR_MODE_FORMATTER                   0x1500
73 #define SATURATION_MODE_FORMATTER1              0x1600
74 #define SATURATION_MODE_FORMATTER2              0x1700
75
76 /* Selectors for the Status controls [GS]ET_STATUS_CTL */
77 #define SAVE_USER_DEFAULTS_FORMATTER            0x0200
78 #define RESTORE_USER_DEFAULTS_FORMATTER         0x0300
79 #define RESTORE_FACTORY_DEFAULTS_FORMATTER      0x0400
80 #define READ_AGC_FORMATTER                      0x0500
81 #define READ_SHUTTER_FORMATTER                  0x0600
82 #define READ_RED_GAIN_FORMATTER                 0x0700
83 #define READ_BLUE_GAIN_FORMATTER                0x0800
84 #define SENSOR_TYPE_FORMATTER1                  0x0C00
85 #define READ_RAW_Y_MEAN_FORMATTER               0x3100
86 #define SET_POWER_SAVE_MODE_FORMATTER           0x3200
87 #define MIRROR_IMAGE_FORMATTER                  0x3300
88 #define LED_FORMATTER                           0x3400
89 #define SENSOR_TYPE_FORMATTER2                  0x3700
90
91 /* Formatters for the Video Endpoint controls [GS]ET_EP_STREAM_CTL */
92 #define VIDEO_OUTPUT_CONTROL_FORMATTER          0x0100
93
94 /* Formatters for the motorized pan & tilt [GS]ET_MPT_CTL */
95 #define PT_RELATIVE_CONTROL_FORMATTER           0x01
96 #define PT_RESET_CONTROL_FORMATTER              0x02
97 #define PT_STATUS_FORMATTER                     0x03
98
99 static char *size2name[PSZ_MAX] =
100 {
101         "subQCIF",
102         "QSIF",
103         "QCIF",
104         "SIF",
105         "CIF",
106         "VGA",
107 };  
108
109 /********/
110
111 /* Entries for the Nala (645/646) camera; the Nala doesn't have compression 
112    preferences, so you either get compressed or non-compressed streams.
113    
114    An alternate value of 0 means this mode is not available at all.
115  */
116
117 struct Nala_table_entry {
118         char alternate;                 /* USB alternate setting */
119         int compressed;                 /* Compressed yes/no */
120
121         unsigned char mode[3];          /* precomputed mode table */
122 };
123
124 static struct Nala_table_entry Nala_table[PSZ_MAX][8] =
125 {
126 #include "pwc_nala.h"
127 };
128
129 /* This tables contains entries for the 675/680/690 (Timon) camera, with
130    4 different qualities (no compression, low, medium, high).
131    It lists the bandwidth requirements for said mode by its alternate interface
132    number. An alternate of 0 means that the mode is unavailable.
133
134    There are 6 * 4 * 4 entries:
135      6 different resolutions subqcif, qsif, qcif, sif, cif, vga
136      6 framerates: 5, 10, 15, 20, 25, 30
137      4 compression modi: none, low, medium, high
138
139    When an uncompressed mode is not available, the next available compressed mode
140    will be chosen (unless the decompressor is absent). Sometimes there are only
141    1 or 2 compressed modes available; in that case entries are duplicated.
142 */
143 struct Timon_table_entry
144 {
145         char alternate;                 /* USB alternate interface */
146         unsigned short packetsize;      /* Normal packet size */
147         unsigned short bandlength;      /* Bandlength when decompressing */
148         unsigned char mode[13];         /* precomputed mode settings for cam */
149 };
150
151 static struct Timon_table_entry Timon_table[PSZ_MAX][6][4] =
152 {
153 #include "pwc_timon.h"
154 };
155
156 /* Entries for the Kiara (730/740/750) camera */
157
158 struct Kiara_table_entry
159 {
160         char alternate;                 /* USB alternate interface */
161         unsigned short packetsize;      /* Normal packet size */
162         unsigned short bandlength;      /* Bandlength when decompressing */
163         unsigned char mode[12];         /* precomputed mode settings for cam */
164 };
165
166 static struct Kiara_table_entry Kiara_table[PSZ_MAX][6][4] =
167 {
168 #include "pwc_kiara.h"
169 };
170
171
172 /****************************************************************************/
173
174
175 #define SendControlMsg(request, value, buflen) \
176         usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0), \
177                 request, \
178                 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, \
179                 value, \
180                 pdev->vcinterface, \
181                 &buf, buflen, HZ / 2)
182
183 #define RecvControlMsg(request, value, buflen) \
184         usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0), \
185                 request, \
186                 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, \
187                 value, \
188                 pdev->vcinterface, \
189                 &buf, buflen, HZ / 2)
190
191
192 #if PWC_DEBUG
193 void pwc_hexdump(void *p, int len)
194 {
195         int i;
196         unsigned char *s;
197         char buf[100], *d;
198
199         s = (unsigned char *)p;
200         d = buf;
201         *d = '\0';
202         Debug("Doing hexdump @ %p, %d bytes.\n", p, len);
203         for (i = 0; i < len; i++) {
204                 d += sprintf(d, "%02X ", *s++);
205                 if ((i & 0xF) == 0xF) {
206                         Debug("%s\n", buf);
207                         d = buf;
208                         *d = '\0';
209                 }
210         }
211         if ((i & 0xF) != 0)
212                 Debug("%s\n", buf);
213 }
214 #endif
215
216 static inline int send_video_command(struct usb_device *udev, int index, void *buf, int buflen)
217 {
218         return usb_control_msg(udev,
219                 usb_sndctrlpipe(udev, 0),
220                 SET_EP_STREAM_CTL,
221                 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
222                 VIDEO_OUTPUT_CONTROL_FORMATTER,
223                 index,
224                 buf, buflen, HZ);
225 }
226
227
228
229 static inline int set_video_mode_Nala(struct pwc_device *pdev, int size, int frames)
230 {
231         unsigned char buf[3];
232         int ret, fps;
233         struct Nala_table_entry *pEntry;
234         int frames2frames[31] =
235         { /* closest match of framerate */
236            0,  0,  0,  0,  4,  /*  0-4  */
237            5,  5,  7,  7, 10,  /*  5-9  */
238           10, 10, 12, 12, 15,  /* 10-14 */
239           15, 15, 15, 20, 20,  /* 15-19 */
240           20, 20, 20, 24, 24,  /* 20-24 */
241           24, 24, 24, 24, 24,  /* 25-29 */
242           24                   /* 30    */
243         };
244         int frames2table[31] = 
245         { 0, 0, 0, 0, 0, /*  0-4  */
246           1, 1, 1, 2, 2, /*  5-9  */
247           3, 3, 4, 4, 4, /* 10-14 */
248           5, 5, 5, 5, 5, /* 15-19 */
249           6, 6, 6, 6, 7, /* 20-24 */
250           7, 7, 7, 7, 7, /* 25-29 */
251           7              /* 30    */
252         };
253         
254         if (size < 0 || size > PSZ_CIF || frames < 4 || frames > 25)
255                 return -EINVAL;
256         frames = frames2frames[frames];
257         fps = frames2table[frames];
258         pEntry = &Nala_table[size][fps];
259         if (pEntry->alternate == 0)
260                 return -EINVAL;
261
262         if (pEntry->compressed && pdev->decompressor == NULL)
263                 return -ENOENT; /* Not supported. */
264
265         memcpy(buf, pEntry->mode, 3);   
266         ret = send_video_command(pdev->udev, pdev->vendpoint, buf, 3);
267         if (ret < 0) {
268                 Debug("Failed to send video command... %d\n", ret);
269                 return ret;
270         }
271         if (pEntry->compressed && pdev->decompressor != 0 && pdev->vpalette != VIDEO_PALETTE_RAW)
272                 pdev->decompressor->init(pdev->type, pdev->release, buf, pdev->decompress_data);
273
274         pdev->cmd_len = 3;
275         memcpy(pdev->cmd_buf, buf, 3);
276
277         /* Set various parameters */
278         pdev->vframes = frames;
279         pdev->vsize = size;
280         pdev->valternate = pEntry->alternate;
281         pdev->image = pwc_image_sizes[size];
282         pdev->frame_size = (pdev->image.x * pdev->image.y * 3) / 2;
283         if (pEntry->compressed) {
284                 if (pdev->release < 5) { /* 4 fold compression */
285                         pdev->vbandlength = 528;
286                         pdev->frame_size /= 4;
287                 }
288                 else {
289                         pdev->vbandlength = 704;
290                         pdev->frame_size /= 3;
291                 }
292         }
293         else
294                 pdev->vbandlength = 0;
295         return 0;
296 }
297
298
299 static inline int set_video_mode_Timon(struct pwc_device *pdev, int size, int frames, int compression, int snapshot)
300 {
301         unsigned char buf[13];
302         struct Timon_table_entry *pChoose;
303         int ret, fps;
304
305         if (size >= PSZ_MAX || frames < 5 || frames > 30 || compression < 0 || compression > 3)
306                 return -EINVAL;
307         if (size == PSZ_VGA && frames > 15)
308                 return -EINVAL;
309         fps = (frames / 5) - 1;
310
311         /* Find a supported framerate with progressively higher compression ratios
312            if the preferred ratio is not available.
313         */
314         pChoose = NULL;
315         if (pdev->decompressor == NULL) {
316 #if PWC_DEBUG
317                 Debug("Trying to find uncompressed mode.\n");
318 #endif
319                 pChoose = &Timon_table[size][fps][0];
320         }
321         else {
322                 while (compression <= 3) {
323                         pChoose = &Timon_table[size][fps][compression];
324                         if (pChoose->alternate != 0)
325                                 break;
326                         compression++;
327                 }
328         }
329         if (pChoose == NULL || pChoose->alternate == 0)
330                 return -ENOENT; /* Not supported. */
331
332         memcpy(buf, pChoose->mode, 13);
333         if (snapshot)
334                 buf[0] |= 0x80;
335         ret = send_video_command(pdev->udev, pdev->vendpoint, buf, 13);
336         if (ret < 0)
337                 return ret;
338
339         if (pChoose->bandlength > 0 && pdev->decompressor != 0 && pdev->vpalette != VIDEO_PALETTE_RAW)
340                 pdev->decompressor->init(pdev->type, pdev->release, buf, pdev->decompress_data);
341
342         pdev->cmd_len = 13;
343         memcpy(pdev->cmd_buf, buf, 13);
344
345         /* Set various parameters */
346         pdev->vframes = frames;
347         pdev->vsize = size;
348         pdev->vsnapshot = snapshot;
349         pdev->valternate = pChoose->alternate;
350         pdev->image = pwc_image_sizes[size];
351         pdev->vbandlength = pChoose->bandlength;
352         if (pChoose->bandlength > 0)
353                 pdev->frame_size = (pChoose->bandlength * pdev->image.y) / 4;
354         else
355                 pdev->frame_size = (pdev->image.x * pdev->image.y * 12) / 8;
356         return 0;
357 }
358
359
360 static inline int set_video_mode_Kiara(struct pwc_device *pdev, int size, int frames, int compression, int snapshot)
361 {
362         struct Kiara_table_entry *pChoose = NULL;
363         int fps, ret;
364         unsigned char buf[12];
365         struct Kiara_table_entry RawEntry = {6, 773, 1272, {0xAD, 0xF4, 0x10, 0x27, 0xB6, 0x24, 0x96, 0x02, 0x30, 0x05, 0x03, 0x80}};
366
367         if (size >= PSZ_MAX || frames < 5 || frames > 30 || compression < 0 || compression > 3)
368                 return -EINVAL;
369         if (size == PSZ_VGA && frames > 15)
370                 return -EINVAL;
371         fps = (frames / 5) - 1;
372
373         /* special case: VGA @ 5 fps and snapshot is raw bayer mode */
374         if (size == PSZ_VGA && frames == 5 && snapshot)
375         {
376                 /* Only available in case the raw palette is selected or 
377                    we have the decompressor available. This mode is 
378                    only available in compressed form 
379                 */
380                 if (pdev->vpalette == VIDEO_PALETTE_RAW || pdev->decompressor != NULL)
381                 {
382                         Info("Choosing VGA/5 BAYER mode (%d).\n", pdev->vpalette);
383                         pChoose = &RawEntry;
384                 }
385                 else
386                 {
387                         Info("VGA/5 BAYER mode _must_ have a decompressor available, or use RAW palette.\n");
388                 }
389         }
390         else
391         {
392                 /* Find a supported framerate with progressively higher compression ratios
393                    if the preferred ratio is not available.
394                    Skip this step when using RAW modes.
395                 */
396                 if (pdev->decompressor == NULL && pdev->vpalette != VIDEO_PALETTE_RAW) {
397 #if PWC_DEBUG
398                         Debug("Trying to find uncompressed mode.\n");
399 #endif
400                         pChoose = &Kiara_table[size][fps][0];
401                 }
402                 else {
403                         while (compression <= 3) {
404                                 pChoose = &Kiara_table[size][fps][compression];
405                                 if (pChoose->alternate != 0)
406                                         break;
407                                 compression++;
408                         }
409                 }
410         }
411         if (pChoose == NULL || pChoose->alternate == 0)
412                 return -ENOENT; /* Not supported. */
413
414         /* usb_control_msg won't take staticly allocated arrays as argument?? */
415         memcpy(buf, pChoose->mode, 12);
416         if (snapshot)
417                 buf[0] |= 0x80;
418
419         /* Firmware bug: video endpoint is 5, but commands are sent to endpoint 4 */
420         ret = send_video_command(pdev->udev, 4 /* pdev->vendpoint */, buf, 12);
421         if (ret < 0)
422                 return ret;
423
424         if (pChoose->bandlength > 0 && pdev->decompressor != 0 && pdev->vpalette != VIDEO_PALETTE_RAW)
425                 pdev->decompressor->init(pdev->type, pdev->release, buf, pdev->decompress_data);
426
427         pdev->cmd_len = 12;
428         memcpy(pdev->cmd_buf, buf, 12);
429         /* All set and go */
430         pdev->vframes = frames;
431         pdev->vsize = size;
432         pdev->vsnapshot = snapshot;
433         pdev->valternate = pChoose->alternate;
434         pdev->image = pwc_image_sizes[size];
435         pdev->vbandlength = pChoose->bandlength;
436         if (pdev->vbandlength > 0)
437                 pdev->frame_size = (pdev->vbandlength * pdev->image.y) / 4;
438         else
439                 pdev->frame_size = (pdev->image.x * pdev->image.y * 12) / 8;
440         return 0;
441 }
442
443
444
445 /**
446    @pdev: device structure
447    @width: viewport width
448    @height: viewport height
449    @frame: framerate, in fps
450    @compression: preferred compression ratio
451    @snapshot: snapshot mode or streaming
452  */
453 int pwc_set_video_mode(struct pwc_device *pdev, int width, int height, int frames, int compression, int snapshot)
454 {
455         int ret, size;
456
457         Trace(TRACE_FLOW, "set_video_mode(%dx%d @ %d, palette %d).\n", width, height, frames, pdev->vpalette);
458         size = pwc_decode_size(pdev, width, height);
459         if (size < 0) {
460                 Debug("Could not find suitable size.\n");
461                 return -ERANGE;
462         }
463         Debug("decode_size = %d.\n", size);
464
465         ret = -EINVAL;
466         switch(pdev->type) {
467         case 645:
468         case 646:
469                 ret = set_video_mode_Nala(pdev, size, frames);
470                 break;
471
472         case 675:
473         case 680:
474         case 690:
475                 ret = set_video_mode_Timon(pdev, size, frames, compression, snapshot);
476                 break;
477         
478         case 720:
479         case 730:
480         case 740:
481         case 750:
482                 ret = set_video_mode_Kiara(pdev, size, frames, compression, snapshot);
483                 break;
484         }
485         if (ret < 0) {
486                 if (ret == -ENOENT)
487                         Info("Video mode %s@%d fps is only supported with the decompressor module (pwcx).\n", size2name[size], frames);
488                 else {
489                         Err("Failed to set video mode %s@%d fps; return code = %d\n", size2name[size], frames, ret);
490                 }
491                 return ret;
492         }
493         pdev->view.x = width;
494         pdev->view.y = height;
495         pdev->frame_total_size = pdev->frame_size + pdev->frame_header_size + pdev->frame_trailer_size;
496         pwc_set_image_buffer_size(pdev);
497         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);
498         return 0;
499 }
500
501
502 void pwc_set_image_buffer_size(struct pwc_device *pdev)
503 {
504         int i, factor = 0, filler = 0;
505
506         /* for PALETTE_YUV420P */
507         switch(pdev->vpalette)
508         {
509         case VIDEO_PALETTE_YUV420P:
510                 factor = 6;
511                 filler = 128;
512                 break;
513         case VIDEO_PALETTE_RAW:
514                 factor = 6; /* can be uncompressed YUV420P */
515                 filler = 0;
516                 break;
517         }
518
519         /* Set sizes in bytes */
520         pdev->image.size = pdev->image.x * pdev->image.y * factor / 4;
521         pdev->view.size  = pdev->view.x  * pdev->view.y  * factor / 4;
522
523         /* Align offset, or you'll get some very weird results in
524            YUV420 mode... x must be multiple of 4 (to get the Y's in
525            place), and y even (or you'll mixup U & V). This is less of a
526            problem for YUV420P.
527          */
528         pdev->offset.x = ((pdev->view.x - pdev->image.x) / 2) & 0xFFFC;
529         pdev->offset.y = ((pdev->view.y - pdev->image.y) / 2) & 0xFFFE;
530
531         /* Fill buffers with gray or black */
532         for (i = 0; i < MAX_IMAGES; i++) {
533                 if (pdev->image_ptr[i] != NULL)
534                         memset(pdev->image_ptr[i], filler, pdev->view.size);
535         }
536 }
537
538
539
540 /* BRIGHTNESS */
541
542 int pwc_get_brightness(struct pwc_device *pdev)
543 {
544         char buf;
545         int ret;
546
547         ret = RecvControlMsg(GET_LUM_CTL, BRIGHTNESS_FORMATTER, 1);     
548         if (ret < 0)
549                 return ret;
550         return buf << 9;
551 }
552
553 int pwc_set_brightness(struct pwc_device *pdev, int value)
554 {
555         char buf;
556
557         if (value < 0)
558                 value = 0;
559         if (value > 0xffff)
560                 value = 0xffff;
561         buf = (value >> 9) & 0x7f;
562         return SendControlMsg(SET_LUM_CTL, BRIGHTNESS_FORMATTER, 1);
563 }
564
565 /* CONTRAST */
566
567 int pwc_get_contrast(struct pwc_device *pdev)
568 {
569         char buf;
570         int ret;
571
572         ret = RecvControlMsg(GET_LUM_CTL, CONTRAST_FORMATTER, 1);
573         if (ret < 0)
574                 return ret;
575         return buf << 10;
576 }
577
578 int pwc_set_contrast(struct pwc_device *pdev, int value)
579 {
580         char buf;
581
582         if (value < 0)
583                 value = 0;
584         if (value > 0xffff)
585                 value = 0xffff;
586         buf = (value >> 10) & 0x3f;
587         return SendControlMsg(SET_LUM_CTL, CONTRAST_FORMATTER, 1);
588 }
589
590 /* GAMMA */
591
592 int pwc_get_gamma(struct pwc_device *pdev)
593 {
594         char buf;
595         int ret;
596         
597         ret = RecvControlMsg(GET_LUM_CTL, GAMMA_FORMATTER, 1);
598         if (ret < 0)
599                 return ret;
600         return buf << 11;
601 }
602
603 int pwc_set_gamma(struct pwc_device *pdev, int value)
604 {
605         char buf;
606
607         if (value < 0)
608                 value = 0;
609         if (value > 0xffff)
610                 value = 0xffff;
611         buf = (value >> 11) & 0x1f;
612         return SendControlMsg(SET_LUM_CTL, GAMMA_FORMATTER, 1);
613 }
614
615
616 /* SATURATION */
617
618 int pwc_get_saturation(struct pwc_device *pdev)
619 {
620         char buf;
621         int ret;
622
623         if (pdev->type < 675)
624                 return -1;
625         ret = RecvControlMsg(GET_CHROM_CTL, pdev->type < 730 ? SATURATION_MODE_FORMATTER2 : SATURATION_MODE_FORMATTER1, 1);
626         if (ret < 0)
627                 return ret;
628         return 32768 + buf * 327;
629 }
630
631 int pwc_set_saturation(struct pwc_device *pdev, int value)
632 {
633         char buf;
634
635         if (pdev->type < 675)
636                 return -EINVAL;
637         if (value < 0)
638                 value = 0;
639         if (value > 0xffff)
640                 value = 0xffff;
641         /* saturation ranges from -100 to +100 */
642         buf = (value - 32768) / 327;
643         return SendControlMsg(SET_CHROM_CTL, pdev->type < 730 ? SATURATION_MODE_FORMATTER2 : SATURATION_MODE_FORMATTER1, 1);
644 }
645
646 /* AGC */
647
648 static inline int pwc_set_agc(struct pwc_device *pdev, int mode, int value)
649 {
650         char buf;
651         int ret;
652         
653         if (mode)
654                 buf = 0x0; /* auto */
655         else
656                 buf = 0xff; /* fixed */
657
658         ret = SendControlMsg(SET_LUM_CTL, AGC_MODE_FORMATTER, 1);
659         
660         if (!mode && ret >= 0) {
661                 if (value < 0)
662                         value = 0;
663                 if (value > 0xffff)
664                         value = 0xffff;
665                 buf = (value >> 10) & 0x3F;
666                 ret = SendControlMsg(SET_LUM_CTL, PRESET_AGC_FORMATTER, 1);
667         }
668         if (ret < 0)
669                 return ret;
670         return 0;
671 }
672
673 static inline int pwc_get_agc(struct pwc_device *pdev, int *value)
674 {
675         unsigned char buf;
676         int ret;
677         
678         ret = RecvControlMsg(GET_LUM_CTL, AGC_MODE_FORMATTER, 1);
679         if (ret < 0)
680                 return ret;
681
682         if (buf != 0) { /* fixed */
683                 ret = RecvControlMsg(GET_LUM_CTL, PRESET_AGC_FORMATTER, 1);
684                 if (ret < 0)
685                         return ret;
686                 if (buf > 0x3F)
687                         buf = 0x3F;
688                 *value = (buf << 10);           
689         }
690         else { /* auto */
691                 ret = RecvControlMsg(GET_STATUS_CTL, READ_AGC_FORMATTER, 1);
692                 if (ret < 0)
693                         return ret;
694                 /* Gah... this value ranges from 0x00 ... 0x9F */
695                 if (buf > 0x9F)
696                         buf = 0x9F;
697                 *value = -(48 + buf * 409);
698         }
699
700         return 0;
701 }
702
703 static inline int pwc_set_shutter_speed(struct pwc_device *pdev, int mode, int value)
704 {
705         char buf[2];
706         int speed, ret;
707
708
709         if (mode)
710                 buf[0] = 0x0;   /* auto */
711         else
712                 buf[0] = 0xff; /* fixed */
713         
714         ret = SendControlMsg(SET_LUM_CTL, SHUTTER_MODE_FORMATTER, 1);
715
716         if (!mode && ret >= 0) {
717                 if (value < 0)
718                         value = 0;
719                 if (value > 0xffff)
720                         value = 0xffff;
721                 switch(pdev->type) {
722                 case 675:
723                 case 680:
724                 case 690:
725                         /* speed ranges from 0x0 to 0x290 (656) */
726                         speed = (value / 100);
727                         buf[1] = speed >> 8;
728                         buf[0] = speed & 0xff;
729                         break;
730                 case 720:
731                 case 730:
732                 case 740:
733                 case 750:
734                         /* speed seems to range from 0x0 to 0xff */
735                         buf[1] = 0;
736                         buf[0] = value >> 8;
737                         break;
738                 }
739
740                 ret = SendControlMsg(SET_LUM_CTL, PRESET_SHUTTER_FORMATTER, 2);
741         }
742         return ret;
743 }       
744
745
746 /* POWER */
747
748 int pwc_camera_power(struct pwc_device *pdev, int power)
749 {
750         char buf;
751
752         if (pdev->type < 675 || (pdev->type < 730 && pdev->release < 6))
753                 return 0;       /* Not supported by Nala or Timon < release 6 */
754
755         if (power)
756                 buf = 0x00; /* active */
757         else
758                 buf = 0xFF; /* power save */
759         return SendControlMsg(SET_STATUS_CTL, SET_POWER_SAVE_MODE_FORMATTER, 1);
760 }
761
762
763
764 /* private calls */
765
766 static inline int pwc_restore_user(struct pwc_device *pdev)
767 {
768         char buf; /* dummy */
769         return SendControlMsg(SET_STATUS_CTL, RESTORE_USER_DEFAULTS_FORMATTER, 0);
770 }
771
772 static inline int pwc_save_user(struct pwc_device *pdev)
773 {
774         char buf; /* dummy */
775         return SendControlMsg(SET_STATUS_CTL, SAVE_USER_DEFAULTS_FORMATTER, 0);
776 }
777
778 static inline int pwc_restore_factory(struct pwc_device *pdev)
779 {
780         char buf; /* dummy */
781         return SendControlMsg(SET_STATUS_CTL, RESTORE_FACTORY_DEFAULTS_FORMATTER, 0);
782 }
783
784  /* ************************************************* */
785  /* Patch by Alvarado: (not in the original version   */
786
787  /*
788   * the camera recognizes modes from 0 to 4:
789   *
790   * 00: indoor (incandescant lighting)
791   * 01: outdoor (sunlight)
792   * 02: fluorescent lighting
793   * 03: manual
794   * 04: auto
795   */ 
796 static inline int pwc_set_awb(struct pwc_device *pdev, int mode)
797 {
798         char buf;
799         int ret;
800         
801         if (mode < 0)
802             mode = 0;
803         
804         if (mode > 4)
805             mode = 4;
806         
807         buf = mode & 0x07; /* just the lowest three bits */
808         
809         ret = SendControlMsg(SET_CHROM_CTL, WB_MODE_FORMATTER, 1);
810         
811         if (ret < 0)
812                 return ret;
813         return 0;
814 }
815
816 static inline int pwc_get_awb(struct pwc_device *pdev)
817 {
818         unsigned char buf;
819         int ret;
820         
821         ret = RecvControlMsg(GET_CHROM_CTL, WB_MODE_FORMATTER, 1);
822
823         if (ret < 0) 
824                 return ret;
825         return buf;
826 }
827
828 static inline int pwc_set_red_gain(struct pwc_device *pdev, int value)
829 {
830         unsigned char buf;
831
832         if (value < 0)
833                 value = 0;
834         if (value > 0xffff)
835                 value = 0xffff;
836         /* only the msb is considered */
837         buf = value >> 8;
838         return SendControlMsg(SET_CHROM_CTL, PRESET_MANUAL_RED_GAIN_FORMATTER, 1);
839 }
840
841 static inline int pwc_get_red_gain(struct pwc_device *pdev, int *value)
842 {
843         unsigned char buf;
844         int ret;
845         
846         ret = RecvControlMsg(GET_CHROM_CTL, PRESET_MANUAL_RED_GAIN_FORMATTER, 1);
847         if (ret < 0)
848             return ret;
849         *value = buf << 8;
850         return 0;
851 }
852
853
854 static inline int pwc_set_blue_gain(struct pwc_device *pdev, int value)
855 {
856         unsigned char buf;
857
858         if (value < 0)
859                 value = 0;
860         if (value > 0xffff)
861                 value = 0xffff;
862         /* only the msb is considered */
863         buf = value >> 8;
864         return SendControlMsg(SET_CHROM_CTL, PRESET_MANUAL_BLUE_GAIN_FORMATTER, 1);
865 }
866
867 static inline int pwc_get_blue_gain(struct pwc_device *pdev, int *value)
868 {
869         unsigned char buf;
870         int ret;
871         
872         ret = RecvControlMsg(GET_CHROM_CTL, PRESET_MANUAL_BLUE_GAIN_FORMATTER, 1);
873         if (ret < 0)
874             return ret;
875         *value = buf << 8;
876         return 0;
877 }
878
879
880 /* The following two functions are different, since they only read the
881    internal red/blue gains, which may be different from the manual 
882    gains set or read above.
883  */   
884 static inline int pwc_read_red_gain(struct pwc_device *pdev, int *value)
885 {
886         unsigned char buf;
887         int ret;
888         
889         ret = RecvControlMsg(GET_STATUS_CTL, READ_RED_GAIN_FORMATTER, 1);
890         if (ret < 0)
891                 return ret;
892         *value = buf << 8;
893         return 0;
894 }
895
896 static inline int pwc_read_blue_gain(struct pwc_device *pdev, int *value)
897 {
898         unsigned char buf;
899         int ret;
900         
901         ret = RecvControlMsg(GET_STATUS_CTL, READ_BLUE_GAIN_FORMATTER, 1);
902         if (ret < 0)
903                 return ret;
904         *value = buf << 8;
905         return 0;
906 }
907
908
909 static inline int pwc_set_wb_speed(struct pwc_device *pdev, int speed)
910 {
911         unsigned char buf;
912         
913         /* useful range is 0x01..0x20 */
914         buf = speed / 0x7f0;
915         return SendControlMsg(SET_CHROM_CTL, AWB_CONTROL_SPEED_FORMATTER, 1);
916 }
917
918 static inline int pwc_get_wb_speed(struct pwc_device *pdev, int *value)
919 {
920         unsigned char buf;
921         int ret;
922         
923         ret = RecvControlMsg(GET_CHROM_CTL, AWB_CONTROL_SPEED_FORMATTER, 1);
924         if (ret < 0)
925                 return ret;
926         *value = buf * 0x7f0;
927         return 0;
928 }
929
930
931 static inline int pwc_set_wb_delay(struct pwc_device *pdev, int delay)
932 {
933         unsigned char buf;
934         
935         /* useful range is 0x01..0x3F */
936         buf = (delay >> 10);
937         return SendControlMsg(SET_CHROM_CTL, AWB_CONTROL_DELAY_FORMATTER, 1);
938 }
939
940 static inline int pwc_get_wb_delay(struct pwc_device *pdev, int *value)
941 {
942         unsigned char buf;
943         int ret;
944         
945         ret = RecvControlMsg(GET_CHROM_CTL, AWB_CONTROL_DELAY_FORMATTER, 1);
946         if (ret < 0)
947                 return ret;
948         *value = buf << 10;
949         return 0;
950 }
951
952
953 int pwc_set_leds(struct pwc_device *pdev, int on_value, int off_value)
954 {
955         unsigned char buf[2];
956
957         if (pdev->type < 730)
958                 return 0;
959         on_value /= 100;
960         off_value /= 100;
961         if (on_value < 0)
962                 on_value = 0;
963         if (on_value > 0xff)
964                 on_value = 0xff;
965         if (off_value < 0)
966                 off_value = 0;
967         if (off_value > 0xff)
968                 off_value = 0xff;
969
970         buf[0] = on_value;
971         buf[1] = off_value;
972
973         return SendControlMsg(SET_STATUS_CTL, LED_FORMATTER, 2);
974 }
975
976 int pwc_get_leds(struct pwc_device *pdev, int *on_value, int *off_value)
977 {
978         unsigned char buf[2];
979         int ret;
980         
981         if (pdev->type < 730) {
982                 *on_value = -1;
983                 *off_value = -1;
984                 return 0;
985         }
986
987         ret = RecvControlMsg(GET_STATUS_CTL, LED_FORMATTER, 2);
988         if (ret < 0)
989                 return ret;
990         *on_value = buf[0] * 100;
991         *off_value = buf[1] * 100;
992         return 0;
993 }
994
995 static inline int pwc_set_contour(struct pwc_device *pdev, int contour)
996 {
997         unsigned char buf;
998         int ret;
999         
1000         if (contour < 0)
1001                 buf = 0xff; /* auto contour on */
1002         else
1003                 buf = 0x0; /* auto contour off */
1004         ret = SendControlMsg(SET_LUM_CTL, AUTO_CONTOUR_FORMATTER, 1);
1005         if (ret < 0)
1006                 return ret;
1007         
1008         if (contour < 0)
1009                 return 0;
1010         if (contour > 0xffff)
1011                 contour = 0xffff;
1012         
1013         buf = (contour >> 10); /* contour preset is [0..3f] */
1014         ret = SendControlMsg(SET_LUM_CTL, PRESET_CONTOUR_FORMATTER, 1);
1015         if (ret < 0)    
1016                 return ret;     
1017         return 0;
1018 }
1019
1020 static inline int pwc_get_contour(struct pwc_device *pdev, int *contour)
1021 {
1022         unsigned char buf;
1023         int ret;
1024         
1025         ret = RecvControlMsg(GET_LUM_CTL, AUTO_CONTOUR_FORMATTER, 1);
1026         if (ret < 0)
1027                 return ret;
1028
1029         if (buf == 0) {
1030                 /* auto mode off, query current preset value */
1031                 ret = RecvControlMsg(GET_LUM_CTL, PRESET_CONTOUR_FORMATTER, 1);
1032                 if (ret < 0)    
1033                         return ret;
1034                 *contour = buf << 10;
1035         }
1036         else
1037                 *contour = -1;
1038         return 0;
1039 }
1040
1041
1042 static inline int pwc_set_backlight(struct pwc_device *pdev, int backlight)
1043 {
1044         unsigned char buf;
1045         
1046         if (backlight)
1047                 buf = 0xff;
1048         else
1049                 buf = 0x0;
1050         return SendControlMsg(SET_LUM_CTL, BACK_LIGHT_COMPENSATION_FORMATTER, 1);
1051 }
1052
1053 static inline int pwc_get_backlight(struct pwc_device *pdev, int *backlight)
1054 {
1055         int ret;
1056         unsigned char buf;
1057         
1058         ret = RecvControlMsg(GET_LUM_CTL, BACK_LIGHT_COMPENSATION_FORMATTER, 1);
1059         if (ret < 0)
1060                 return ret;
1061         *backlight = buf;
1062         return 0;
1063 }
1064
1065
1066 static inline int pwc_set_flicker(struct pwc_device *pdev, int flicker)
1067 {
1068         unsigned char buf;
1069         
1070         if (flicker)
1071                 buf = 0xff;
1072         else
1073                 buf = 0x0;
1074         return SendControlMsg(SET_LUM_CTL, FLICKERLESS_MODE_FORMATTER, 1);
1075 }
1076
1077 static inline int pwc_get_flicker(struct pwc_device *pdev, int *flicker)
1078 {
1079         int ret;
1080         unsigned char buf;
1081         
1082         ret = RecvControlMsg(GET_LUM_CTL, FLICKERLESS_MODE_FORMATTER, 1);
1083         if (ret < 0)
1084                 return ret;
1085         *flicker = buf;
1086         return 0;
1087 }
1088
1089
1090 static inline int pwc_set_dynamic_noise(struct pwc_device *pdev, int noise)
1091 {
1092         unsigned char buf;
1093
1094         if (noise < 0)
1095                 noise = 0;
1096         if (noise > 3)
1097                 noise = 3;
1098         buf = noise;
1099         return SendControlMsg(SET_LUM_CTL, DYNAMIC_NOISE_CONTROL_FORMATTER, 1);
1100 }
1101
1102 static inline int pwc_get_dynamic_noise(struct pwc_device *pdev, int *noise)
1103 {
1104         int ret;
1105         unsigned char buf;
1106         
1107         ret = RecvControlMsg(GET_LUM_CTL, DYNAMIC_NOISE_CONTROL_FORMATTER, 1);
1108         if (ret < 0)
1109                 return ret;
1110         *noise = buf;
1111         return 0;
1112 }
1113
1114 int pwc_mpt_reset(struct pwc_device *pdev, int flags)
1115 {
1116         unsigned char buf;
1117         
1118         buf = flags & 0x03; // only lower two bits are currently used
1119         return SendControlMsg(SET_MPT_CTL, PT_RESET_CONTROL_FORMATTER, 1);
1120 }
1121
1122 static inline int pwc_mpt_set_angle(struct pwc_device *pdev, int pan, int tilt)
1123 {
1124         unsigned char buf[4];
1125         
1126         /* set new relative angle; angles are expressed in degrees * 100,
1127            but cam as .5 degree resolution, hence devide by 200. Also
1128            the angle must be multiplied by 64 before it's send to
1129            the cam (??)
1130          */
1131         pan  =  64 * pan  / 100;
1132         tilt = -64 * tilt / 100; /* positive tilt is down, which is not what the user would expect */
1133         buf[0] = pan & 0xFF;
1134         buf[1] = (pan >> 8) & 0xFF;
1135         buf[2] = tilt & 0xFF;
1136         buf[3] = (tilt >> 8) & 0xFF;
1137         return SendControlMsg(SET_MPT_CTL, PT_RELATIVE_CONTROL_FORMATTER, 4);
1138 }
1139
1140 static inline int pwc_mpt_get_status(struct pwc_device *pdev, struct pwc_mpt_status *status)
1141 {
1142         int ret;
1143         unsigned char buf[5];
1144         
1145         ret = RecvControlMsg(GET_MPT_CTL, PT_STATUS_FORMATTER, 5);
1146         if (ret < 0)
1147                 return ret;
1148         status->status = buf[0] & 0x7; // 3 bits are used for reporting
1149         status->time_pan = (buf[1] << 8) + buf[2];
1150         status->time_tilt = (buf[3] << 8) + buf[4];
1151         return 0;
1152 }
1153
1154
1155 int pwc_get_cmos_sensor(struct pwc_device *pdev, int *sensor)
1156 {
1157         unsigned char buf;
1158         int ret = -1, request;
1159         
1160         if (pdev->type < 675)
1161                 request = SENSOR_TYPE_FORMATTER1;
1162         else if (pdev->type < 730)
1163                 return -1; /* The Vesta series doesn't have this call */
1164         else
1165                 request = SENSOR_TYPE_FORMATTER2;
1166         
1167         ret = RecvControlMsg(GET_STATUS_CTL, request, 1);
1168         if (ret < 0)
1169                 return ret;
1170         if (pdev->type < 675)
1171                 *sensor = buf | 0x100;
1172         else
1173                 *sensor = buf;
1174         return 0;
1175 }
1176
1177
1178  /* End of Add-Ons                                    */
1179  /* ************************************************* */
1180
1181 /* Linux 2.5.something and 2.6 pass direct pointers to arguments of
1182    ioctl() calls. With 2.4, you have to do tedious copy_from_user()
1183    and copy_to_user() calls. With these macros we circumvent this,
1184    and let me maintain only one source file. The functionality is
1185    exactly the same otherwise.
1186  */   
1187
1188 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
1189
1190 /* define local variable for arg */
1191 #define ARG_DEF(ARG_type, ARG_name)\
1192         ARG_type *ARG_name = arg;
1193 /* copy arg to local variable */        
1194 #define ARG_IN(ARG_name) /* nothing */
1195 /* argument itself (referenced) */
1196 #define ARGR(ARG_name) (*ARG_name)
1197 /* argument address */
1198 #define ARGA(ARG_name) ARG_name
1199 /* copy local variable to arg */
1200 #define ARG_OUT(ARG_name) /* nothing */
1201
1202 #else
1203
1204 #define ARG_DEF(ARG_type, ARG_name)\
1205         ARG_type ARG_name;
1206 #define ARG_IN(ARG_name)\
1207         if (copy_from_user(&ARG_name, arg, sizeof(ARG_name))) {\
1208                 ret = -EFAULT;\
1209                 break;\
1210         }
1211 #define ARGR(ARG_name) ARG_name
1212 #define ARGA(ARG_name) &ARG_name
1213 #define ARG_OUT(ARG_name)\
1214         if (copy_to_user(arg, &ARG_name, sizeof(ARG_name))) {\
1215                 ret = -EFAULT;\
1216                 break;\
1217         }
1218
1219 #endif
1220
1221 int pwc_ioctl(struct pwc_device *pdev, unsigned int cmd, void *arg)
1222 {
1223         int ret = 0;
1224
1225         switch(cmd) {
1226         case VIDIOCPWCRUSER:
1227         {
1228                 if (pwc_restore_user(pdev))
1229                         ret = -EINVAL;
1230                 break;
1231         }
1232         
1233         case VIDIOCPWCSUSER:
1234         {
1235                 if (pwc_save_user(pdev))
1236                         ret = -EINVAL;
1237                 break;
1238         }
1239                 
1240         case VIDIOCPWCFACTORY:
1241         {
1242                 if (pwc_restore_factory(pdev))
1243                         ret = -EINVAL;
1244                 break;
1245         }
1246         
1247         case VIDIOCPWCSCQUAL:
1248         {       
1249                 ARG_DEF(int, qual)
1250
1251                 ARG_IN(qual)
1252                 if (ARGR(qual) < 0 || ARGR(qual) > 3)
1253                         ret = -EINVAL;
1254                 else
1255                         ret = pwc_try_video_mode(pdev, pdev->view.x, pdev->view.y, pdev->vframes, ARGR(qual), pdev->vsnapshot);
1256                 if (ret >= 0)
1257                         pdev->vcompression = ARGR(qual);
1258                 break;
1259         }
1260         
1261         case VIDIOCPWCGCQUAL:
1262         {
1263                 ARG_DEF(int, qual)
1264                 
1265                 ARGR(qual) = pdev->vcompression;
1266                 ARG_OUT(qual)
1267                 break;
1268         }
1269         
1270         case VIDIOCPWCPROBE:
1271         {
1272                 ARG_DEF(struct pwc_probe, probe)
1273                 
1274                 strcpy(ARGR(probe).name, pdev->vdev->name);
1275                 ARGR(probe).type = pdev->type;
1276                 ARG_OUT(probe)
1277                 break;
1278         }
1279
1280         case VIDIOCPWCGSERIAL:
1281         {
1282                 ARG_DEF(struct pwc_serial, serial)
1283                 
1284                 strcpy(ARGR(serial).serial, pdev->serial);
1285                 ARG_OUT(serial)
1286                 break;
1287         }
1288
1289         case VIDIOCPWCSAGC:
1290         {
1291                 ARG_DEF(int, agc)
1292
1293                 ARG_IN(agc)
1294                 if (pwc_set_agc(pdev, ARGR(agc) < 0 ? 1 : 0, ARGR(agc)))
1295                         ret = -EINVAL;
1296                 break;
1297         }
1298         
1299         case VIDIOCPWCGAGC:
1300         {
1301                 ARG_DEF(int, agc)
1302                 
1303                 if (pwc_get_agc(pdev, ARGA(agc)))
1304                         ret = -EINVAL;
1305                 ARG_OUT(agc)
1306                 break;
1307         }
1308         
1309         case VIDIOCPWCSSHUTTER:
1310         {
1311                 ARG_DEF(int, shutter_speed)
1312
1313                 ARG_IN(shutter_speed)
1314                 ret = pwc_set_shutter_speed(pdev, ARGR(shutter_speed) < 0 ? 1 : 0, ARGR(shutter_speed));
1315                 break;
1316         }
1317         
1318         case VIDIOCPWCSAWB:
1319         {
1320                 ARG_DEF(struct pwc_whitebalance, wb)
1321                 
1322                 ARG_IN(wb)
1323                 ret = pwc_set_awb(pdev, ARGR(wb).mode);
1324                 if (ret >= 0 && ARGR(wb).mode == PWC_WB_MANUAL) {
1325                         pwc_set_red_gain(pdev, ARGR(wb).manual_red);
1326                         pwc_set_blue_gain(pdev, ARGR(wb).manual_blue);
1327                 }
1328                 break;
1329         }
1330
1331         case VIDIOCPWCGAWB:
1332         {
1333                 ARG_DEF(struct pwc_whitebalance, wb)
1334
1335                 memset(ARGA(wb), 0, sizeof(struct pwc_whitebalance));
1336                 ARGR(wb).mode = pwc_get_awb(pdev);
1337                 if (ARGR(wb).mode < 0)
1338                         ret = -EINVAL;
1339                 else {
1340                         if (ARGR(wb).mode == PWC_WB_MANUAL) {
1341                                 ret = pwc_get_red_gain(pdev, &ARGR(wb).manual_red);
1342                                 if (ret < 0)
1343                                         break;
1344                                 ret = pwc_get_blue_gain(pdev, &ARGR(wb).manual_blue);
1345                                 if (ret < 0)
1346                                         break;
1347                         }
1348                         if (ARGR(wb).mode == PWC_WB_AUTO) {
1349                                 ret = pwc_read_red_gain(pdev, &ARGR(wb).read_red);
1350                                 if (ret < 0)
1351                                         break;
1352                                 ret =pwc_read_blue_gain(pdev, &ARGR(wb).read_blue);
1353                                 if (ret < 0)
1354                                         break;
1355                         }
1356                 }
1357                 ARG_OUT(wb)
1358                 break;
1359         }
1360         
1361         case VIDIOCPWCSAWBSPEED:
1362         {
1363                 ARG_DEF(struct pwc_wb_speed, wbs)
1364                 
1365                 if (ARGR(wbs).control_speed > 0) {
1366                         ret = pwc_set_wb_speed(pdev, ARGR(wbs).control_speed);
1367                 }
1368                 if (ARGR(wbs).control_delay > 0) {
1369                         ret = pwc_set_wb_delay(pdev, ARGR(wbs).control_delay);
1370                 }
1371                 break;
1372         }
1373         
1374         case VIDIOCPWCGAWBSPEED:
1375         {
1376                 ARG_DEF(struct pwc_wb_speed, wbs)
1377                 
1378                 ret = pwc_get_wb_speed(pdev, &ARGR(wbs).control_speed);
1379                 if (ret < 0)
1380                         break;
1381                 ret = pwc_get_wb_delay(pdev, &ARGR(wbs).control_delay);
1382                 if (ret < 0)
1383                         break;
1384                 ARG_OUT(wbs)
1385                 break;
1386         }
1387
1388         case VIDIOCPWCSLED:
1389         {
1390                 ARG_DEF(struct pwc_leds, leds)
1391
1392                 ARG_IN(leds)
1393                 ret = pwc_set_leds(pdev, ARGR(leds).led_on, ARGR(leds).led_off);
1394                 break;
1395         }
1396
1397
1398         case VIDIOCPWCGLED:
1399         {
1400                 ARG_DEF(struct pwc_leds, leds)
1401                 
1402                 ret = pwc_get_leds(pdev, &ARGR(leds).led_on, &ARGR(leds).led_off);
1403                 ARG_OUT(leds)
1404                 break;
1405         }
1406
1407         case VIDIOCPWCSCONTOUR:
1408         {
1409                 ARG_DEF(int, contour)
1410
1411                 ARG_IN(contour)
1412                 ret = pwc_set_contour(pdev, ARGR(contour));
1413                 break;
1414         }
1415                         
1416         case VIDIOCPWCGCONTOUR:
1417         {
1418                 ARG_DEF(int, contour)
1419                 
1420                 ret = pwc_get_contour(pdev, ARGA(contour));
1421                 ARG_OUT(contour)
1422                 break;
1423         }
1424         
1425         case VIDIOCPWCSBACKLIGHT:
1426         {
1427                 ARG_DEF(int, backlight)
1428                 
1429                 ARG_IN(backlight)
1430                 ret = pwc_set_backlight(pdev, ARGR(backlight));
1431                 break;
1432         }
1433
1434         case VIDIOCPWCGBACKLIGHT:
1435         {
1436                 ARG_DEF(int, backlight)
1437                 
1438                 ret = pwc_get_backlight(pdev, ARGA(backlight));
1439                 ARG_OUT(backlight)
1440                 break;
1441         }
1442         
1443         case VIDIOCPWCSFLICKER:
1444         {
1445                 ARG_DEF(int, flicker)
1446                 
1447                 ARG_IN(flicker)
1448                 ret = pwc_set_flicker(pdev, ARGR(flicker));
1449                 break;
1450         }
1451
1452         case VIDIOCPWCGFLICKER:
1453         {
1454                 ARG_DEF(int, flicker)
1455                 
1456                 ret = pwc_get_flicker(pdev, ARGA(flicker));
1457                 ARG_OUT(flicker)
1458                 break;
1459         }
1460         
1461         case VIDIOCPWCSDYNNOISE:
1462         {
1463                 ARG_DEF(int, dynnoise)
1464                 
1465                 ARG_IN(dynnoise)
1466                 ret = pwc_set_dynamic_noise(pdev, ARGR(dynnoise));
1467                 break;
1468         }
1469         
1470         case VIDIOCPWCGDYNNOISE:
1471         {
1472                 ARG_DEF(int, dynnoise)
1473
1474                 ret = pwc_get_dynamic_noise(pdev, ARGA(dynnoise));
1475                 ARG_OUT(dynnoise);
1476                 break;
1477         }
1478
1479         case VIDIOCPWCGREALSIZE:
1480         {
1481                 ARG_DEF(struct pwc_imagesize, size)
1482                 
1483                 ARGR(size).width = pdev->image.x;
1484                 ARGR(size).height = pdev->image.y;
1485                 ARG_OUT(size)
1486                 break;
1487         }
1488         
1489         case VIDIOCPWCMPTRESET:
1490         {
1491                 if (pdev->features & FEATURE_MOTOR_PANTILT)
1492                 {
1493                         ARG_DEF(int, flags)
1494
1495                         ARG_IN(flags)
1496                         ret = pwc_mpt_reset(pdev, ARGR(flags));
1497                         if (ret >= 0)
1498                         {
1499                                 pdev->pan_angle = 0;
1500                                 pdev->tilt_angle = 0;
1501                         }
1502                 }
1503                 else
1504                 {
1505                         ret = -ENXIO;
1506                 }
1507                 break;          
1508         }
1509         
1510         case VIDIOCPWCMPTGRANGE:
1511         {
1512                 if (pdev->features & FEATURE_MOTOR_PANTILT)
1513                 {
1514                         ARG_DEF(struct pwc_mpt_range, range)
1515                         
1516                         ARGR(range) = pdev->angle_range;
1517                         ARG_OUT(range)
1518                 }
1519                 else
1520                 {       
1521                         ret = -ENXIO;
1522                 }
1523                 break;
1524         }
1525         
1526         case VIDIOCPWCMPTSANGLE:
1527         {
1528                 int new_pan, new_tilt;
1529                 
1530                 if (pdev->features & FEATURE_MOTOR_PANTILT)
1531                 {
1532                         ARG_DEF(struct pwc_mpt_angles, angles)
1533
1534                         ARG_IN(angles)
1535                         /* The camera can only set relative angles, so
1536                            do some calculations when getting an absolute angle .
1537                          */
1538                         if (ARGR(angles).absolute)
1539                         {
1540                                 new_pan  = ARGR(angles).pan; 
1541                                 new_tilt = ARGR(angles).tilt;
1542                         }
1543                         else
1544                         {
1545                                 new_pan  = pdev->pan_angle  + ARGR(angles).pan;
1546                                 new_tilt = pdev->tilt_angle + ARGR(angles).tilt;
1547                         }
1548                         /* check absolute ranges */
1549                         if (new_pan  < pdev->angle_range.pan_min  ||
1550                             new_pan  > pdev->angle_range.pan_max  ||
1551                             new_tilt < pdev->angle_range.tilt_min ||
1552                             new_tilt > pdev->angle_range.tilt_max)
1553                         {
1554                                 ret = -ERANGE;
1555                         }
1556                         else
1557                         {
1558                                 /* go to relative range, check again */
1559                                 new_pan  -= pdev->pan_angle;
1560                                 new_tilt -= pdev->tilt_angle;
1561                                 /* angles are specified in degrees * 100, thus the limit = 36000 */
1562                                 if (new_pan < -36000 || new_pan > 36000 || new_tilt < -36000 || new_tilt > 36000)
1563                                         ret = -ERANGE;
1564                         }
1565                         if (ret == 0) /* no errors so far */
1566                         {
1567                                 ret = pwc_mpt_set_angle(pdev, new_pan, new_tilt);
1568                                 if (ret >= 0)
1569                                 {
1570                                         pdev->pan_angle  += new_pan;
1571                                         pdev->tilt_angle += new_tilt;
1572                                 }
1573                                 if (ret == -EPIPE) /* stall -> out of range */
1574                                         ret = -ERANGE;                          
1575                         }
1576                 }
1577                 else
1578                 {
1579                         ret = -ENXIO;
1580                 }
1581                 break;
1582         }
1583
1584         case VIDIOCPWCMPTGANGLE:
1585         {
1586                 
1587                 if (pdev->features & FEATURE_MOTOR_PANTILT)
1588                 {
1589                         ARG_DEF(struct pwc_mpt_angles, angles)
1590
1591                         ARGR(angles).absolute = 1;
1592                         ARGR(angles).pan  = pdev->pan_angle;
1593                         ARGR(angles).tilt = pdev->tilt_angle;
1594                         ARG_OUT(angles)
1595                 }
1596                 else
1597                 {
1598                         ret = -ENXIO;
1599                 }
1600                 break;
1601         }
1602  
1603         case VIDIOCPWCMPTSTATUS:
1604         {
1605                 if (pdev->features & FEATURE_MOTOR_PANTILT)
1606                 {
1607                         ARG_DEF(struct pwc_mpt_status, status)
1608                         
1609                         ret = pwc_mpt_get_status(pdev, ARGA(status));
1610                         ARG_OUT(status)
1611                 }
1612                 else
1613                 {
1614                         ret = -ENXIO;
1615                 }
1616                 break;
1617         }
1618
1619         case VIDIOCPWCGVIDCMD:
1620         {
1621                 ARG_DEF(struct pwc_video_command, cmd);
1622                 
1623                 ARGR(cmd).type = pdev->type;
1624                 ARGR(cmd).release = pdev->release;
1625                 ARGR(cmd).command_len = pdev->cmd_len;
1626                 memcpy(&ARGR(cmd).command_buf, pdev->cmd_buf, pdev->cmd_len);
1627                 ARGR(cmd).bandlength = pdev->vbandlength;
1628                 ARGR(cmd).frame_size = pdev->frame_size;
1629                 ARG_OUT(cmd)
1630                 break;
1631         }
1632
1633         default:
1634                 ret = -ENOIOCTLCMD;
1635                 break;
1636         }
1637         
1638         if (ret > 0)
1639                 return 0;
1640         return ret;
1641 }
1642
1643
1644