]> git.karo-electronics.de Git - linux-beck.git/blob - drivers/media/video/pwc/pwc-ctrl.c
Merge tag 'scsi-misc' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi...
[linux-beck.git] / drivers / media / video / pwc / 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.
5    (C) 2004-2006 Luc Saillard (luc@saillard.org)
6    (C) 2011 Hans de Goede <hdegoede@redhat.com>
7
8    NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
9    driver and thus may have bugs that are not present in the original version.
10    Please send bug reports and support requests to <luc@saillard.org>.
11
12    NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
13    driver and thus may have bugs that are not present in the original version.
14    Please send bug reports and support requests to <luc@saillard.org>.
15    The decompression routines have been implemented by reverse-engineering the
16    Nemosoft binary pwcx module. Caveat emptor.
17
18    This program is free software; you can redistribute it and/or modify
19    it under the terms of the GNU General Public License as published by
20    the Free Software Foundation; either version 2 of the License, or
21    (at your option) any later version.
22
23    This program is distributed in the hope that it will be useful,
24    but WITHOUT ANY WARRANTY; without even the implied warranty of
25    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26    GNU General Public License for more details.
27
28    You should have received a copy of the GNU General Public License
29    along with this program; if not, write to the Free Software
30    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
31 */
32
33 /*
34    Changes
35    2001/08/03  Alvarado   Added methods for changing white balance and
36                           red/green gains
37  */
38
39 /* Control functions for the cam; brightness, contrast, video mode, etc. */
40
41 #ifdef __KERNEL__
42 #include <asm/uaccess.h>
43 #endif
44 #include <asm/errno.h>
45
46 #include "pwc.h"
47 #include "pwc-kiara.h"
48 #include "pwc-timon.h"
49 #include "pwc-dec1.h"
50 #include "pwc-dec23.h"
51
52 /* Selectors for status controls used only in this file */
53 #define GET_STATUS_B00                          0x0B00
54 #define SENSOR_TYPE_FORMATTER1                  0x0C00
55 #define GET_STATUS_3000                         0x3000
56 #define READ_RAW_Y_MEAN_FORMATTER               0x3100
57 #define SET_POWER_SAVE_MODE_FORMATTER           0x3200
58 #define MIRROR_IMAGE_FORMATTER                  0x3300
59 #define LED_FORMATTER                           0x3400
60 #define LOWLIGHT                                0x3500
61 #define GET_STATUS_3600                         0x3600
62 #define SENSOR_TYPE_FORMATTER2                  0x3700
63 #define GET_STATUS_3800                         0x3800
64 #define GET_STATUS_4000                         0x4000
65 #define GET_STATUS_4100                         0x4100  /* Get */
66 #define CTL_STATUS_4200                         0x4200  /* [GS] 1 */
67
68 /* Formatters for the Video Endpoint controls [GS]ET_EP_STREAM_CTL */
69 #define VIDEO_OUTPUT_CONTROL_FORMATTER          0x0100
70
71 static const char *size2name[PSZ_MAX] =
72 {
73         "subQCIF",
74         "QSIF",
75         "QCIF",
76         "SIF",
77         "CIF",
78         "VGA",
79 };
80
81 /********/
82
83 /* Entries for the Nala (645/646) camera; the Nala doesn't have compression
84    preferences, so you either get compressed or non-compressed streams.
85
86    An alternate value of 0 means this mode is not available at all.
87  */
88
89 #define PWC_FPS_MAX_NALA 8
90
91 struct Nala_table_entry {
92         char alternate;                 /* USB alternate setting */
93         int compressed;                 /* Compressed yes/no */
94
95         unsigned char mode[3];          /* precomputed mode table */
96 };
97
98 static unsigned int Nala_fps_vector[PWC_FPS_MAX_NALA] = { 4, 5, 7, 10, 12, 15, 20, 24 };
99
100 static struct Nala_table_entry Nala_table[PSZ_MAX][PWC_FPS_MAX_NALA] =
101 {
102 #include "pwc-nala.h"
103 };
104
105 /****************************************************************************/
106
107 static int _send_control_msg(struct pwc_device *pdev,
108         u8 request, u16 value, int index, void *buf, int buflen)
109 {
110         int rc;
111         void *kbuf = NULL;
112
113         if (buflen) {
114                 kbuf = kmemdup(buf, buflen, GFP_KERNEL); /* not allowed on stack */
115                 if (kbuf == NULL)
116                         return -ENOMEM;
117         }
118
119         rc = usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
120                 request,
121                 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
122                 value,
123                 index,
124                 kbuf, buflen, USB_CTRL_SET_TIMEOUT);
125
126         kfree(kbuf);
127         return rc;
128 }
129
130 static int recv_control_msg(struct pwc_device *pdev,
131         u8 request, u16 value, void *buf, int buflen)
132 {
133         int rc;
134         void *kbuf = kmalloc(buflen, GFP_KERNEL); /* not allowed on stack */
135
136         if (kbuf == NULL)
137                 return -ENOMEM;
138
139         rc = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
140                 request,
141                 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
142                 value,
143                 pdev->vcinterface,
144                 kbuf, buflen, USB_CTRL_GET_TIMEOUT);
145         memcpy(buf, kbuf, buflen);
146         kfree(kbuf);
147
148         if (rc < 0)
149                 PWC_ERROR("recv_control_msg error %d req %02x val %04x\n",
150                           rc, request, value);
151         return rc;
152 }
153
154 static inline int send_video_command(struct pwc_device *pdev,
155         int index, void *buf, int buflen)
156 {
157         return _send_control_msg(pdev,
158                 SET_EP_STREAM_CTL,
159                 VIDEO_OUTPUT_CONTROL_FORMATTER,
160                 index,
161                 buf, buflen);
162 }
163
164 int send_control_msg(struct pwc_device *pdev,
165         u8 request, u16 value, void *buf, int buflen)
166 {
167         return _send_control_msg(pdev,
168                 request, value, pdev->vcinterface, buf, buflen);
169 }
170
171 static int set_video_mode_Nala(struct pwc_device *pdev, int size, int frames,
172                                int *compression)
173 {
174         unsigned char buf[3];
175         int ret, fps;
176         struct Nala_table_entry *pEntry;
177         int frames2frames[31] =
178         { /* closest match of framerate */
179            0,  0,  0,  0,  4,  /*  0-4  */
180            5,  5,  7,  7, 10,  /*  5-9  */
181           10, 10, 12, 12, 15,  /* 10-14 */
182           15, 15, 15, 20, 20,  /* 15-19 */
183           20, 20, 20, 24, 24,  /* 20-24 */
184           24, 24, 24, 24, 24,  /* 25-29 */
185           24                   /* 30    */
186         };
187         int frames2table[31] =
188         { 0, 0, 0, 0, 0, /*  0-4  */
189           1, 1, 1, 2, 2, /*  5-9  */
190           3, 3, 4, 4, 4, /* 10-14 */
191           5, 5, 5, 5, 5, /* 15-19 */
192           6, 6, 6, 6, 7, /* 20-24 */
193           7, 7, 7, 7, 7, /* 25-29 */
194           7              /* 30    */
195         };
196
197         if (size < 0 || size > PSZ_CIF || frames < 4 || frames > 25)
198                 return -EINVAL;
199         frames = frames2frames[frames];
200         fps = frames2table[frames];
201         pEntry = &Nala_table[size][fps];
202         if (pEntry->alternate == 0)
203                 return -EINVAL;
204
205         memcpy(buf, pEntry->mode, 3);
206         ret = send_video_command(pdev, pdev->vendpoint, buf, 3);
207         if (ret < 0) {
208                 PWC_DEBUG_MODULE("Failed to send video command... %d\n", ret);
209                 return ret;
210         }
211         if (pEntry->compressed && pdev->pixfmt == V4L2_PIX_FMT_YUV420) {
212                 ret = pwc_dec1_init(pdev, pdev->type, pdev->release, buf);
213                 if (ret < 0)
214                         return ret;
215         }
216
217         pdev->cmd_len = 3;
218         memcpy(pdev->cmd_buf, buf, 3);
219
220         /* Set various parameters */
221         pdev->vframes = frames;
222         pdev->valternate = pEntry->alternate;
223         pdev->width  = pwc_image_sizes[size][0];
224         pdev->height = pwc_image_sizes[size][1];
225         pdev->frame_size = (pdev->width * pdev->height * 3) / 2;
226         if (pEntry->compressed) {
227                 if (pdev->release < 5) { /* 4 fold compression */
228                         pdev->vbandlength = 528;
229                         pdev->frame_size /= 4;
230                 }
231                 else {
232                         pdev->vbandlength = 704;
233                         pdev->frame_size /= 3;
234                 }
235         }
236         else
237                 pdev->vbandlength = 0;
238
239         /* Let pwc-if.c:isoc_init know we don't support higher compression */
240         *compression = 3;
241
242         return 0;
243 }
244
245
246 static int set_video_mode_Timon(struct pwc_device *pdev, int size, int frames,
247         int *compression)
248 {
249         unsigned char buf[13];
250         const struct Timon_table_entry *pChoose;
251         int ret, fps;
252
253         if (size >= PSZ_MAX || frames < 5 || frames > 30 ||
254             *compression < 0 || *compression > 3)
255                 return -EINVAL;
256         if (size == PSZ_VGA && frames > 15)
257                 return -EINVAL;
258         fps = (frames / 5) - 1;
259
260         /* Find a supported framerate with progressively higher compression */
261         pChoose = NULL;
262         while (*compression <= 3) {
263                 pChoose = &Timon_table[size][fps][*compression];
264                 if (pChoose->alternate != 0)
265                         break;
266                 (*compression)++;
267         }
268         if (pChoose == NULL || pChoose->alternate == 0)
269                 return -ENOENT; /* Not supported. */
270
271         memcpy(buf, pChoose->mode, 13);
272         ret = send_video_command(pdev, pdev->vendpoint, buf, 13);
273         if (ret < 0)
274                 return ret;
275
276         if (pChoose->bandlength > 0 && pdev->pixfmt == V4L2_PIX_FMT_YUV420) {
277                 ret = pwc_dec23_init(pdev, pdev->type, buf);
278                 if (ret < 0)
279                         return ret;
280         }
281
282         pdev->cmd_len = 13;
283         memcpy(pdev->cmd_buf, buf, 13);
284
285         /* Set various parameters */
286         pdev->vframes = frames;
287         pdev->valternate = pChoose->alternate;
288         pdev->width  = pwc_image_sizes[size][0];
289         pdev->height = pwc_image_sizes[size][1];
290         pdev->vbandlength = pChoose->bandlength;
291         if (pChoose->bandlength > 0)
292                 pdev->frame_size = (pChoose->bandlength * pdev->height) / 4;
293         else
294                 pdev->frame_size = (pdev->width * pdev->height * 12) / 8;
295         return 0;
296 }
297
298
299 static int set_video_mode_Kiara(struct pwc_device *pdev, int size, int frames,
300         int *compression)
301 {
302         const struct Kiara_table_entry *pChoose = NULL;
303         int fps, ret;
304         unsigned char buf[12];
305
306         if (size >= PSZ_MAX || frames < 5 || frames > 30 ||
307             *compression < 0 || *compression > 3)
308                 return -EINVAL;
309         if (size == PSZ_VGA && frames > 15)
310                 return -EINVAL;
311         fps = (frames / 5) - 1;
312
313         /* Find a supported framerate with progressively higher compression */
314         while (*compression <= 3) {
315                 pChoose = &Kiara_table[size][fps][*compression];
316                 if (pChoose->alternate != 0)
317                         break;
318                 (*compression)++;
319         }
320         if (pChoose == NULL || pChoose->alternate == 0)
321                 return -ENOENT; /* Not supported. */
322
323         PWC_TRACE("Using alternate setting %d.\n", pChoose->alternate);
324
325         /* usb_control_msg won't take staticly allocated arrays as argument?? */
326         memcpy(buf, pChoose->mode, 12);
327
328         /* Firmware bug: video endpoint is 5, but commands are sent to endpoint 4 */
329         ret = send_video_command(pdev, 4 /* pdev->vendpoint */, buf, 12);
330         if (ret < 0)
331                 return ret;
332
333         if (pChoose->bandlength > 0 && pdev->pixfmt == V4L2_PIX_FMT_YUV420) {
334                 ret = pwc_dec23_init(pdev, pdev->type, buf);
335                 if (ret < 0)
336                         return ret;
337         }
338
339         pdev->cmd_len = 12;
340         memcpy(pdev->cmd_buf, buf, 12);
341         /* All set and go */
342         pdev->vframes = frames;
343         pdev->valternate = pChoose->alternate;
344         pdev->width  = pwc_image_sizes[size][0];
345         pdev->height = pwc_image_sizes[size][1];
346         pdev->vbandlength = pChoose->bandlength;
347         if (pdev->vbandlength > 0)
348                 pdev->frame_size = (pdev->vbandlength * pdev->height) / 4;
349         else
350                 pdev->frame_size = (pdev->width * pdev->height * 12) / 8;
351         PWC_TRACE("frame_size=%d, vframes=%d, vsize=%d, vbandlength=%d\n",
352             pdev->frame_size, pdev->vframes, size, pdev->vbandlength);
353         return 0;
354 }
355
356 int pwc_set_video_mode(struct pwc_device *pdev, int width, int height,
357         int frames, int *compression)
358 {
359         int ret, size;
360
361         PWC_DEBUG_FLOW("set_video_mode(%dx%d @ %d, pixfmt %08x).\n", width, height, frames, pdev->pixfmt);
362         size = pwc_get_size(pdev, width, height);
363         PWC_TRACE("decode_size = %d.\n", size);
364
365         if (DEVICE_USE_CODEC1(pdev->type)) {
366                 ret = set_video_mode_Nala(pdev, size, frames, compression);
367
368         } else if (DEVICE_USE_CODEC3(pdev->type)) {
369                 ret = set_video_mode_Kiara(pdev, size, frames, compression);
370
371         } else {
372                 ret = set_video_mode_Timon(pdev, size, frames, compression);
373         }
374         if (ret < 0) {
375                 PWC_ERROR("Failed to set video mode %s@%d fps; return code = %d\n", size2name[size], frames, ret);
376                 return ret;
377         }
378         pdev->frame_total_size = pdev->frame_size + pdev->frame_header_size + pdev->frame_trailer_size;
379         PWC_DEBUG_SIZE("Set resolution to %dx%d\n", pdev->width, pdev->height);
380         return 0;
381 }
382
383 static unsigned int pwc_get_fps_Nala(struct pwc_device *pdev, unsigned int index, unsigned int size)
384 {
385         unsigned int i;
386
387         for (i = 0; i < PWC_FPS_MAX_NALA; i++) {
388                 if (Nala_table[size][i].alternate) {
389                         if (index--==0) return Nala_fps_vector[i];
390                 }
391         }
392         return 0;
393 }
394
395 static unsigned int pwc_get_fps_Kiara(struct pwc_device *pdev, unsigned int index, unsigned int size)
396 {
397         unsigned int i;
398
399         for (i = 0; i < PWC_FPS_MAX_KIARA; i++) {
400                 if (Kiara_table[size][i][3].alternate) {
401                         if (index--==0) return Kiara_fps_vector[i];
402                 }
403         }
404         return 0;
405 }
406
407 static unsigned int pwc_get_fps_Timon(struct pwc_device *pdev, unsigned int index, unsigned int size)
408 {
409         unsigned int i;
410
411         for (i=0; i < PWC_FPS_MAX_TIMON; i++) {
412                 if (Timon_table[size][i][3].alternate) {
413                         if (index--==0) return Timon_fps_vector[i];
414                 }
415         }
416         return 0;
417 }
418
419 unsigned int pwc_get_fps(struct pwc_device *pdev, unsigned int index, unsigned int size)
420 {
421         unsigned int ret;
422
423         if (DEVICE_USE_CODEC1(pdev->type)) {
424                 ret = pwc_get_fps_Nala(pdev, index, size);
425
426         } else if (DEVICE_USE_CODEC3(pdev->type)) {
427                 ret = pwc_get_fps_Kiara(pdev, index, size);
428
429         } else {
430                 ret = pwc_get_fps_Timon(pdev, index, size);
431         }
432
433         return ret;
434 }
435
436 int pwc_get_u8_ctrl(struct pwc_device *pdev, u8 request, u16 value, int *data)
437 {
438         int ret;
439         u8 buf;
440
441         ret = recv_control_msg(pdev, request, value, &buf, sizeof(buf));
442         if (ret < 0)
443                 return ret;
444
445         *data = buf;
446         return 0;
447 }
448
449 int pwc_set_u8_ctrl(struct pwc_device *pdev, u8 request, u16 value, u8 data)
450 {
451         int ret;
452
453         ret = send_control_msg(pdev, request, value, &data, sizeof(data));
454         if (ret < 0)
455                 return ret;
456
457         return 0;
458 }
459
460 int pwc_get_s8_ctrl(struct pwc_device *pdev, u8 request, u16 value, int *data)
461 {
462         int ret;
463         s8 buf;
464
465         ret = recv_control_msg(pdev, request, value, &buf, sizeof(buf));
466         if (ret < 0)
467                 return ret;
468
469         *data = buf;
470         return 0;
471 }
472
473 int pwc_get_u16_ctrl(struct pwc_device *pdev, u8 request, u16 value, int *data)
474 {
475         int ret;
476         u8 buf[2];
477
478         ret = recv_control_msg(pdev, request, value, buf, sizeof(buf));
479         if (ret < 0)
480                 return ret;
481
482         *data = (buf[1] << 8) | buf[0];
483         return 0;
484 }
485
486 int pwc_set_u16_ctrl(struct pwc_device *pdev, u8 request, u16 value, u16 data)
487 {
488         int ret;
489         u8 buf[2];
490
491         buf[0] = data & 0xff;
492         buf[1] = data >> 8;
493         ret = send_control_msg(pdev, request, value, buf, sizeof(buf));
494         if (ret < 0)
495                 return ret;
496
497         return 0;
498 }
499
500 int pwc_button_ctrl(struct pwc_device *pdev, u16 value)
501 {
502         int ret;
503
504         ret = send_control_msg(pdev, SET_STATUS_CTL, value, NULL, 0);
505         if (ret < 0)
506                 return ret;
507
508         return 0;
509 }
510
511 /* POWER */
512 void pwc_camera_power(struct pwc_device *pdev, int power)
513 {
514         char buf;
515         int r;
516
517         if (!pdev->power_save)
518                 return;
519
520         if (pdev->type < 675 || (pdev->type < 730 && pdev->release < 6))
521                 return; /* Not supported by Nala or Timon < release 6 */
522
523         if (power)
524                 buf = 0x00; /* active */
525         else
526                 buf = 0xFF; /* power save */
527         r = send_control_msg(pdev,
528                 SET_STATUS_CTL, SET_POWER_SAVE_MODE_FORMATTER,
529                 &buf, sizeof(buf));
530
531         if (r < 0)
532                 PWC_ERROR("Failed to power %s camera (%d)\n",
533                           power ? "on" : "off", r);
534 }
535
536 int pwc_set_leds(struct pwc_device *pdev, int on_value, int off_value)
537 {
538         unsigned char buf[2];
539         int r;
540
541         if (pdev->type < 730)
542                 return 0;
543         on_value /= 100;
544         off_value /= 100;
545         if (on_value < 0)
546                 on_value = 0;
547         if (on_value > 0xff)
548                 on_value = 0xff;
549         if (off_value < 0)
550                 off_value = 0;
551         if (off_value > 0xff)
552                 off_value = 0xff;
553
554         buf[0] = on_value;
555         buf[1] = off_value;
556
557         r = send_control_msg(pdev,
558                 SET_STATUS_CTL, LED_FORMATTER, &buf, sizeof(buf));
559         if (r < 0)
560                 PWC_ERROR("Failed to set LED on/off time (%d)\n", r);
561
562         return r;
563 }
564
565 #ifdef CONFIG_USB_PWC_DEBUG
566 int pwc_get_cmos_sensor(struct pwc_device *pdev, int *sensor)
567 {
568         unsigned char buf;
569         int ret = -1, request;
570
571         if (pdev->type < 675)
572                 request = SENSOR_TYPE_FORMATTER1;
573         else if (pdev->type < 730)
574                 return -1; /* The Vesta series doesn't have this call */
575         else
576                 request = SENSOR_TYPE_FORMATTER2;
577
578         ret = recv_control_msg(pdev,
579                 GET_STATUS_CTL, request, &buf, sizeof(buf));
580         if (ret < 0)
581                 return ret;
582         if (pdev->type < 675)
583                 *sensor = buf | 0x100;
584         else
585                 *sensor = buf;
586         return 0;
587 }
588 #endif