1 /* Driver for Philips webcam
2 Functions that send various control messages to the webcam, including
4 (C) 1999-2003 Nemosoft Unv.
5 (C) 2004-2006 Luc Saillard (luc@saillard.org)
6 (C) 2011 Hans de Goede <hdegoede@redhat.com>
8 NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
9 driver and thus may have bugs that are not present in the original version.
10 Please send bug reports and support requests to <luc@saillard.org>.
12 NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
13 driver and thus may have bugs that are not present in the original version.
14 Please send bug reports and support requests to <luc@saillard.org>.
15 The decompression routines have been implemented by reverse-engineering the
16 Nemosoft binary pwcx module. Caveat emptor.
18 This program is free software; you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation; either version 2 of the License, or
21 (at your option) any later version.
23 This program is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
28 You should have received a copy of the GNU General Public License
29 along with this program; if not, write to the Free Software
30 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35 2001/08/03 Alvarado Added methods for changing white balance and
39 /* Control functions for the cam; brightness, contrast, video mode, etc. */
42 #include <asm/uaccess.h>
44 #include <asm/errno.h>
47 #include "pwc-kiara.h"
48 #include "pwc-timon.h"
50 #include "pwc-dec23.h"
52 /* Selectors for status controls used only in this file */
53 #define GET_STATUS_B00 0x0B00
54 #define SENSOR_TYPE_FORMATTER1 0x0C00
55 #define GET_STATUS_3000 0x3000
56 #define READ_RAW_Y_MEAN_FORMATTER 0x3100
57 #define SET_POWER_SAVE_MODE_FORMATTER 0x3200
58 #define MIRROR_IMAGE_FORMATTER 0x3300
59 #define LED_FORMATTER 0x3400
60 #define LOWLIGHT 0x3500
61 #define GET_STATUS_3600 0x3600
62 #define SENSOR_TYPE_FORMATTER2 0x3700
63 #define GET_STATUS_3800 0x3800
64 #define GET_STATUS_4000 0x4000
65 #define GET_STATUS_4100 0x4100 /* Get */
66 #define CTL_STATUS_4200 0x4200 /* [GS] 1 */
68 /* Formatters for the Video Endpoint controls [GS]ET_EP_STREAM_CTL */
69 #define VIDEO_OUTPUT_CONTROL_FORMATTER 0x0100
71 static const char *size2name[PSZ_MAX] =
83 /* Entries for the Nala (645/646) camera; the Nala doesn't have compression
84 preferences, so you either get compressed or non-compressed streams.
86 An alternate value of 0 means this mode is not available at all.
89 #define PWC_FPS_MAX_NALA 8
91 struct Nala_table_entry {
92 char alternate; /* USB alternate setting */
93 int compressed; /* Compressed yes/no */
95 unsigned char mode[3]; /* precomputed mode table */
98 static unsigned int Nala_fps_vector[PWC_FPS_MAX_NALA] = { 4, 5, 7, 10, 12, 15, 20, 24 };
100 static struct Nala_table_entry Nala_table[PSZ_MAX][PWC_FPS_MAX_NALA] =
102 #include "pwc-nala.h"
105 /****************************************************************************/
107 static int _send_control_msg(struct pwc_device *pdev,
108 u8 request, u16 value, int index, void *buf, int buflen)
114 kbuf = kmemdup(buf, buflen, GFP_KERNEL); /* not allowed on stack */
119 rc = usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
121 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
124 kbuf, buflen, USB_CTRL_SET_TIMEOUT);
130 static int recv_control_msg(struct pwc_device *pdev,
131 u8 request, u16 value, void *buf, int buflen)
134 void *kbuf = kmalloc(buflen, GFP_KERNEL); /* not allowed on stack */
139 rc = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
141 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
144 kbuf, buflen, USB_CTRL_GET_TIMEOUT);
145 memcpy(buf, kbuf, buflen);
149 PWC_ERROR("recv_control_msg error %d req %02x val %04x\n",
154 static inline int send_video_command(struct pwc_device *pdev,
155 int index, void *buf, int buflen)
157 return _send_control_msg(pdev,
159 VIDEO_OUTPUT_CONTROL_FORMATTER,
164 int send_control_msg(struct pwc_device *pdev,
165 u8 request, u16 value, void *buf, int buflen)
167 return _send_control_msg(pdev,
168 request, value, pdev->vcinterface, buf, buflen);
171 static int set_video_mode_Nala(struct pwc_device *pdev, int size, int pixfmt,
172 int frames, int *compression, int send_to_cam)
174 unsigned char buf[3];
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 */
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 */
197 if (size < 0 || size > PSZ_CIF)
201 else if (frames > 25)
203 frames = frames2frames[frames];
204 fps = frames2table[frames];
205 pEntry = &Nala_table[size][fps];
206 if (pEntry->alternate == 0)
209 memcpy(buf, pEntry->mode, 3);
211 ret = send_video_command(pdev, pdev->vendpoint, buf, 3);
213 PWC_DEBUG_MODULE("Failed to send video command... %d\n", ret);
216 if (pEntry->compressed && pixfmt == V4L2_PIX_FMT_YUV420)
217 pwc_dec1_init(pdev, buf);
220 memcpy(pdev->cmd_buf, buf, 3);
222 /* Set various parameters */
223 pdev->pixfmt = pixfmt;
224 pdev->vframes = frames;
225 pdev->valternate = pEntry->alternate;
226 pdev->width = pwc_image_sizes[size][0];
227 pdev->height = pwc_image_sizes[size][1];
228 pdev->frame_size = (pdev->width * pdev->height * 3) / 2;
229 if (pEntry->compressed) {
230 if (pdev->release < 5) { /* 4 fold compression */
231 pdev->vbandlength = 528;
232 pdev->frame_size /= 4;
235 pdev->vbandlength = 704;
236 pdev->frame_size /= 3;
240 pdev->vbandlength = 0;
242 /* Let pwc-if.c:isoc_init know we don't support higher compression */
249 static int set_video_mode_Timon(struct pwc_device *pdev, int size, int pixfmt,
250 int frames, int *compression, int send_to_cam)
252 unsigned char buf[13];
253 const struct Timon_table_entry *pChoose;
256 if (size >= PSZ_MAX || *compression < 0 || *compression > 3)
260 else if (size == PSZ_VGA && frames > 15)
262 else if (frames > 30)
264 fps = (frames / 5) - 1;
266 /* Find a supported framerate with progressively higher compression */
268 while (*compression <= 3) {
269 pChoose = &Timon_table[size][fps][*compression];
270 if (pChoose->alternate != 0)
274 if (pChoose == NULL || pChoose->alternate == 0)
275 return -ENOENT; /* Not supported. */
277 memcpy(buf, pChoose->mode, 13);
279 ret = send_video_command(pdev, pdev->vendpoint, buf, 13);
283 if (pChoose->bandlength > 0 && pixfmt == V4L2_PIX_FMT_YUV420)
284 pwc_dec23_init(pdev, buf);
287 memcpy(pdev->cmd_buf, buf, 13);
289 /* Set various parameters */
290 pdev->pixfmt = pixfmt;
291 pdev->vframes = (fps + 1) * 5;
292 pdev->valternate = pChoose->alternate;
293 pdev->width = pwc_image_sizes[size][0];
294 pdev->height = pwc_image_sizes[size][1];
295 pdev->vbandlength = pChoose->bandlength;
296 if (pChoose->bandlength > 0)
297 pdev->frame_size = (pChoose->bandlength * pdev->height) / 4;
299 pdev->frame_size = (pdev->width * pdev->height * 12) / 8;
304 static int set_video_mode_Kiara(struct pwc_device *pdev, int size, int pixfmt,
305 int frames, int *compression, int send_to_cam)
307 const struct Kiara_table_entry *pChoose = NULL;
309 unsigned char buf[12];
311 if (size >= PSZ_MAX || *compression < 0 || *compression > 3)
315 else if (size == PSZ_VGA && frames > 15)
317 else if (frames > 30)
319 fps = (frames / 5) - 1;
321 /* Find a supported framerate with progressively higher compression */
322 while (*compression <= 3) {
323 pChoose = &Kiara_table[size][fps][*compression];
324 if (pChoose->alternate != 0)
328 if (pChoose == NULL || pChoose->alternate == 0)
329 return -ENOENT; /* Not supported. */
331 PWC_TRACE("Using alternate setting %d.\n", pChoose->alternate);
333 /* usb_control_msg won't take staticly allocated arrays as argument?? */
334 memcpy(buf, pChoose->mode, 12);
336 /* Firmware bug: video endpoint is 5, but commands are sent to endpoint 4 */
338 ret = send_video_command(pdev, 4, buf, 12);
342 if (pChoose->bandlength > 0 && pixfmt == V4L2_PIX_FMT_YUV420)
343 pwc_dec23_init(pdev, buf);
346 memcpy(pdev->cmd_buf, buf, 12);
348 pdev->pixfmt = pixfmt;
349 pdev->vframes = (fps + 1) * 5;
350 pdev->valternate = pChoose->alternate;
351 pdev->width = pwc_image_sizes[size][0];
352 pdev->height = pwc_image_sizes[size][1];
353 pdev->vbandlength = pChoose->bandlength;
354 if (pdev->vbandlength > 0)
355 pdev->frame_size = (pdev->vbandlength * pdev->height) / 4;
357 pdev->frame_size = (pdev->width * pdev->height * 12) / 8;
358 PWC_TRACE("frame_size=%d, vframes=%d, vsize=%d, vbandlength=%d\n",
359 pdev->frame_size, pdev->vframes, size, pdev->vbandlength);
363 int pwc_set_video_mode(struct pwc_device *pdev, int width, int height,
364 int pixfmt, int frames, int *compression, int send_to_cam)
368 PWC_DEBUG_FLOW("set_video_mode(%dx%d @ %d, pixfmt %08x).\n",
369 width, height, frames, pixfmt);
370 size = pwc_get_size(pdev, width, height);
371 PWC_TRACE("decode_size = %d.\n", size);
373 if (DEVICE_USE_CODEC1(pdev->type)) {
374 ret = set_video_mode_Nala(pdev, size, pixfmt, frames,
375 compression, send_to_cam);
376 } else if (DEVICE_USE_CODEC3(pdev->type)) {
377 ret = set_video_mode_Kiara(pdev, size, pixfmt, frames,
378 compression, send_to_cam);
380 ret = set_video_mode_Timon(pdev, size, pixfmt, frames,
381 compression, send_to_cam);
384 PWC_ERROR("Failed to set video mode %s@%d fps; return code = %d\n", size2name[size], frames, ret);
387 pdev->frame_total_size = pdev->frame_size + pdev->frame_header_size + pdev->frame_trailer_size;
388 PWC_DEBUG_SIZE("Set resolution to %dx%d\n", pdev->width, pdev->height);
392 static unsigned int pwc_get_fps_Nala(struct pwc_device *pdev, unsigned int index, unsigned int size)
396 for (i = 0; i < PWC_FPS_MAX_NALA; i++) {
397 if (Nala_table[size][i].alternate) {
398 if (index--==0) return Nala_fps_vector[i];
404 static unsigned int pwc_get_fps_Kiara(struct pwc_device *pdev, unsigned int index, unsigned int size)
408 for (i = 0; i < PWC_FPS_MAX_KIARA; i++) {
409 if (Kiara_table[size][i][3].alternate) {
410 if (index--==0) return Kiara_fps_vector[i];
416 static unsigned int pwc_get_fps_Timon(struct pwc_device *pdev, unsigned int index, unsigned int size)
420 for (i=0; i < PWC_FPS_MAX_TIMON; i++) {
421 if (Timon_table[size][i][3].alternate) {
422 if (index--==0) return Timon_fps_vector[i];
428 unsigned int pwc_get_fps(struct pwc_device *pdev, unsigned int index, unsigned int size)
432 if (DEVICE_USE_CODEC1(pdev->type)) {
433 ret = pwc_get_fps_Nala(pdev, index, size);
435 } else if (DEVICE_USE_CODEC3(pdev->type)) {
436 ret = pwc_get_fps_Kiara(pdev, index, size);
439 ret = pwc_get_fps_Timon(pdev, index, size);
445 int pwc_get_u8_ctrl(struct pwc_device *pdev, u8 request, u16 value, int *data)
450 ret = recv_control_msg(pdev, request, value, &buf, sizeof(buf));
458 int pwc_set_u8_ctrl(struct pwc_device *pdev, u8 request, u16 value, u8 data)
462 ret = send_control_msg(pdev, request, value, &data, sizeof(data));
469 int pwc_get_s8_ctrl(struct pwc_device *pdev, u8 request, u16 value, int *data)
474 ret = recv_control_msg(pdev, request, value, &buf, sizeof(buf));
482 int pwc_get_u16_ctrl(struct pwc_device *pdev, u8 request, u16 value, int *data)
487 ret = recv_control_msg(pdev, request, value, buf, sizeof(buf));
491 *data = (buf[1] << 8) | buf[0];
495 int pwc_set_u16_ctrl(struct pwc_device *pdev, u8 request, u16 value, u16 data)
500 buf[0] = data & 0xff;
502 ret = send_control_msg(pdev, request, value, buf, sizeof(buf));
509 int pwc_button_ctrl(struct pwc_device *pdev, u16 value)
513 ret = send_control_msg(pdev, SET_STATUS_CTL, value, NULL, 0);
521 void pwc_camera_power(struct pwc_device *pdev, int power)
526 if (!pdev->power_save)
529 if (pdev->type < 675 || (pdev->type < 730 && pdev->release < 6))
530 return; /* Not supported by Nala or Timon < release 6 */
533 buf = 0x00; /* active */
535 buf = 0xFF; /* power save */
536 r = send_control_msg(pdev,
537 SET_STATUS_CTL, SET_POWER_SAVE_MODE_FORMATTER,
541 PWC_ERROR("Failed to power %s camera (%d)\n",
542 power ? "on" : "off", r);
545 int pwc_set_leds(struct pwc_device *pdev, int on_value, int off_value)
547 unsigned char buf[2];
550 if (pdev->type < 730)
560 if (off_value > 0xff)
566 r = send_control_msg(pdev,
567 SET_STATUS_CTL, LED_FORMATTER, &buf, sizeof(buf));
569 PWC_ERROR("Failed to set LED on/off time (%d)\n", r);
574 #ifdef CONFIG_USB_PWC_DEBUG
575 int pwc_get_cmos_sensor(struct pwc_device *pdev, int *sensor)
578 int ret = -1, request;
580 if (pdev->type < 675)
581 request = SENSOR_TYPE_FORMATTER1;
582 else if (pdev->type < 730)
583 return -1; /* The Vesta series doesn't have this call */
585 request = SENSOR_TYPE_FORMATTER2;
587 ret = recv_control_msg(pdev,
588 GET_STATUS_CTL, request, &buf, sizeof(buf));
591 if (pdev->type < 675)
592 *sensor = buf | 0x100;