blob: 9c1fb3f07dee75630ba529542175fcfec1774738 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Driver for Philips webcam
2 Functions that send various control messages to the webcam, including
3 video modes.
4 (C) 1999-2003 Nemosoft Unv.
Luc Saillard2b455db2006-04-24 10:29:46 -03005 (C) 2004-2006 Luc Saillard (luc@saillard.org)
Hans de Goede6c9cac82011-06-26 12:52:01 -03006 (C) 2011 Hans de Goede <hdegoede@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007
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
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030035 2001/08/03 Alvarado Added methods for changing white balance and
36 red/green gains
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 */
38
39/* Control functions for the cam; brightness, contrast, video mode, etc. */
40
41#ifdef __KERNEL__
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030042#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#endif
44#include <asm/errno.h>
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include "pwc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include "pwc-kiara.h"
48#include "pwc-timon.h"
Luc Saillard2b455db2006-04-24 10:29:46 -030049#include "pwc-dec1.h"
50#include "pwc-dec23.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Hans de Goede6c9cac82011-06-26 12:52:01 -030052/* Selectors for status controls used only in this file */
Luc Saillard2b455db2006-04-24 10:29:46 -030053#define GET_STATUS_B00 0x0B00
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#define SENSOR_TYPE_FORMATTER1 0x0C00
Luc Saillard2b455db2006-04-24 10:29:46 -030055#define GET_STATUS_3000 0x3000
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#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
Luc Saillard2b455db2006-04-24 10:29:46 -030060#define LOWLIGHT 0x3500
61#define GET_STATUS_3600 0x3600
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#define SENSOR_TYPE_FORMATTER2 0x3700
Luc Saillard2b455db2006-04-24 10:29:46 -030063#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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68/* Formatters for the Video Endpoint controls [GS]ET_EP_STREAM_CTL */
69#define VIDEO_OUTPUT_CONTROL_FORMATTER 0x0100
70
Arjan van de Ven4c4c9432005-11-29 09:43:42 +010071static const char *size2name[PSZ_MAX] =
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
73 "subQCIF",
74 "QSIF",
75 "QCIF",
76 "SIF",
77 "CIF",
78 "VGA",
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030079};
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81/********/
82
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030083/* Entries for the Nala (645/646) camera; the Nala doesn't have compression
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 preferences, so you either get compressed or non-compressed streams.
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030085
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 An alternate value of 0 means this mode is not available at all.
87 */
88
Luc Saillard9ee6d782007-04-22 23:54:36 -030089#define PWC_FPS_MAX_NALA 8
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091struct 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
Luc Saillard9ee6d782007-04-22 23:54:36 -030098static unsigned int Nala_fps_vector[PWC_FPS_MAX_NALA] = { 4, 5, 7, 10, 12, 15, 20, 24 };
99
100static struct Nala_table_entry Nala_table[PSZ_MAX][PWC_FPS_MAX_NALA] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
102#include "pwc-nala.h"
103};
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105/****************************************************************************/
106
Martin Fuzzey6b35ca02009-04-21 21:48:09 +0200107static int _send_control_msg(struct pwc_device *pdev,
Hans de Goede6c9cac82011-06-26 12:52:01 -0300108 u8 request, u16 value, int index, void *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Martin Fuzzey6b35ca02009-04-21 21:48:09 +0200110 int rc;
111 void *kbuf = NULL;
112
113 if (buflen) {
Thomas Meyer6d00c9a2011-11-17 18:43:40 -0300114 kbuf = kmemdup(buf, buflen, GFP_KERNEL); /* not allowed on stack */
Martin Fuzzey6b35ca02009-04-21 21:48:09 +0200115 if (kbuf == NULL)
116 return -ENOMEM;
Martin Fuzzey6b35ca02009-04-21 21:48:09 +0200117 }
118
119 rc = usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
120 request,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
Martin Fuzzey6b35ca02009-04-21 21:48:09 +0200122 value,
123 index,
Hans de Goede6c9cac82011-06-26 12:52:01 -0300124 kbuf, buflen, USB_CTRL_SET_TIMEOUT);
Martin Fuzzey6b35ca02009-04-21 21:48:09 +0200125
126 kfree(kbuf);
127 return rc;
128}
129
130static 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,
Hans de Goede6c9cac82011-06-26 12:52:01 -0300144 kbuf, buflen, USB_CTRL_GET_TIMEOUT);
Martin Fuzzey6b35ca02009-04-21 21:48:09 +0200145 memcpy(buf, kbuf, buflen);
146 kfree(kbuf);
Hans de Goede6c9cac82011-06-26 12:52:01 -0300147
148 if (rc < 0)
149 PWC_ERROR("recv_control_msg error %d req %02x val %04x\n",
150 rc, request, value);
Martin Fuzzey6b35ca02009-04-21 21:48:09 +0200151 return rc;
152}
153
154static 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,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 VIDEO_OUTPUT_CONTROL_FORMATTER,
160 index,
Hans de Goede6c9cac82011-06-26 12:52:01 -0300161 buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
Hans de Goede294e2892011-07-03 12:23:24 -0300164int send_control_msg(struct pwc_device *pdev,
Martin Fuzzey6b35ca02009-04-21 21:48:09 +0200165 u8 request, u16 value, void *buf, int buflen)
166{
167 return _send_control_msg(pdev,
Hans de Goede6c9cac82011-06-26 12:52:01 -0300168 request, value, pdev->vcinterface, buf, buflen);
Martin Fuzzey6b35ca02009-04-21 21:48:09 +0200169}
170
Hans de Goeded167a852012-01-10 13:01:41 -0300171static int set_video_mode_Nala(struct pwc_device *pdev, int size, int pixfmt,
Hans de Goede938d5b92012-01-10 13:14:46 -0300172 int frames, int *compression, int send_to_cam)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
174 unsigned char buf[3];
Hans de Goede938d5b92012-01-10 13:14:46 -0300175 int fps, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 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 */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300181 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 };
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300187 int frames2table[31] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 { 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 };
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300196
Hans de Goede54d3fb32012-01-08 07:19:29 -0300197 if (size < 0 || size > PSZ_CIF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return -EINVAL;
Hans de Goede54d3fb32012-01-08 07:19:29 -0300199 if (frames < 4)
200 frames = 4;
201 else if (frames > 25)
202 frames = 25;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 frames = frames2frames[frames];
204 fps = frames2table[frames];
205 pEntry = &Nala_table[size][fps];
206 if (pEntry->alternate == 0)
207 return -EINVAL;
208
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300209 memcpy(buf, pEntry->mode, 3);
Hans de Goede938d5b92012-01-10 13:14:46 -0300210 if (send_to_cam)
211 ret = send_video_command(pdev, pdev->vendpoint, buf, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (ret < 0) {
Luc Saillard2b455db2006-04-24 10:29:46 -0300213 PWC_DEBUG_MODULE("Failed to send video command... %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return ret;
215 }
Hans de Goeded167a852012-01-10 13:01:41 -0300216 if (pEntry->compressed && pixfmt == V4L2_PIX_FMT_YUV420)
Hans de Goede56ae24a2012-01-08 11:29:19 -0300217 pwc_dec1_init(pdev, buf);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 pdev->cmd_len = 3;
220 memcpy(pdev->cmd_buf, buf, 3);
221
222 /* Set various parameters */
Hans de Goeded167a852012-01-10 13:01:41 -0300223 pdev->pixfmt = pixfmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 pdev->vframes = frames;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 pdev->valternate = pEntry->alternate;
Hans de Goede795e6eb2012-01-04 16:58:44 -0300226 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 if (pEntry->compressed) {
230 if (pdev->release < 5) { /* 4 fold compression */
231 pdev->vbandlength = 528;
232 pdev->frame_size /= 4;
233 }
234 else {
235 pdev->vbandlength = 704;
236 pdev->frame_size /= 3;
237 }
238 }
239 else
240 pdev->vbandlength = 0;
Hans de Goede5bbe18d2012-01-04 18:48:05 -0300241
242 /* Let pwc-if.c:isoc_init know we don't support higher compression */
243 *compression = 3;
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return 0;
246}
247
248
Hans de Goeded167a852012-01-10 13:01:41 -0300249static int set_video_mode_Timon(struct pwc_device *pdev, int size, int pixfmt,
Hans de Goede938d5b92012-01-10 13:14:46 -0300250 int frames, int *compression, int send_to_cam)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
252 unsigned char buf[13];
253 const struct Timon_table_entry *pChoose;
Hans de Goede938d5b92012-01-10 13:14:46 -0300254 int fps, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Hans de Goede54d3fb32012-01-08 07:19:29 -0300256 if (size >= PSZ_MAX || *compression < 0 || *compression > 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 return -EINVAL;
Hans de Goede54d3fb32012-01-08 07:19:29 -0300258 if (frames < 5)
259 frames = 5;
260 else if (size == PSZ_VGA && frames > 15)
261 frames = 15;
262 else if (frames > 30)
263 frames = 30;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 fps = (frames / 5) - 1;
265
Hans de Goede5bbe18d2012-01-04 18:48:05 -0300266 /* Find a supported framerate with progressively higher compression */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 pChoose = NULL;
Hans de Goede5bbe18d2012-01-04 18:48:05 -0300268 while (*compression <= 3) {
269 pChoose = &Timon_table[size][fps][*compression];
270 if (pChoose->alternate != 0)
271 break;
272 (*compression)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
274 if (pChoose == NULL || pChoose->alternate == 0)
275 return -ENOENT; /* Not supported. */
276
277 memcpy(buf, pChoose->mode, 13);
Hans de Goede938d5b92012-01-10 13:14:46 -0300278 if (send_to_cam)
279 ret = send_video_command(pdev, pdev->vendpoint, buf, 13);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (ret < 0)
281 return ret;
282
Hans de Goeded167a852012-01-10 13:01:41 -0300283 if (pChoose->bandlength > 0 && pixfmt == V4L2_PIX_FMT_YUV420)
Hans de Goede56ae24a2012-01-08 11:29:19 -0300284 pwc_dec23_init(pdev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 pdev->cmd_len = 13;
287 memcpy(pdev->cmd_buf, buf, 13);
288
289 /* Set various parameters */
Hans de Goeded167a852012-01-10 13:01:41 -0300290 pdev->pixfmt = pixfmt;
Hans de Goede54d3fb32012-01-08 07:19:29 -0300291 pdev->vframes = (fps + 1) * 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 pdev->valternate = pChoose->alternate;
Hans de Goede795e6eb2012-01-04 16:58:44 -0300293 pdev->width = pwc_image_sizes[size][0];
294 pdev->height = pwc_image_sizes[size][1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 pdev->vbandlength = pChoose->bandlength;
296 if (pChoose->bandlength > 0)
Hans de Goede795e6eb2012-01-04 16:58:44 -0300297 pdev->frame_size = (pChoose->bandlength * pdev->height) / 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 else
Hans de Goede795e6eb2012-01-04 16:58:44 -0300299 pdev->frame_size = (pdev->width * pdev->height * 12) / 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return 0;
301}
302
303
Hans de Goeded167a852012-01-10 13:01:41 -0300304static int set_video_mode_Kiara(struct pwc_device *pdev, int size, int pixfmt,
Hans de Goede938d5b92012-01-10 13:14:46 -0300305 int frames, int *compression, int send_to_cam)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307 const struct Kiara_table_entry *pChoose = NULL;
Hans de Goede938d5b92012-01-10 13:14:46 -0300308 int fps, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 unsigned char buf[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Hans de Goede54d3fb32012-01-08 07:19:29 -0300311 if (size >= PSZ_MAX || *compression < 0 || *compression > 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 return -EINVAL;
Hans de Goede54d3fb32012-01-08 07:19:29 -0300313 if (frames < 5)
314 frames = 5;
315 else if (size == PSZ_VGA && frames > 15)
316 frames = 15;
317 else if (frames > 30)
318 frames = 30;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 fps = (frames / 5) - 1;
320
Hans de Goede5bbe18d2012-01-04 18:48:05 -0300321 /* Find a supported framerate with progressively higher compression */
322 while (*compression <= 3) {
323 pChoose = &Kiara_table[size][fps][*compression];
Hans de Goededc8a7e82011-12-31 10:52:02 -0300324 if (pChoose->alternate != 0)
325 break;
Hans de Goede5bbe18d2012-01-04 18:48:05 -0300326 (*compression)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
328 if (pChoose == NULL || pChoose->alternate == 0)
329 return -ENOENT; /* Not supported. */
330
Luc Saillard2b455db2006-04-24 10:29:46 -0300331 PWC_TRACE("Using alternate setting %d.\n", pChoose->alternate);
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 /* usb_control_msg won't take staticly allocated arrays as argument?? */
334 memcpy(buf, pChoose->mode, 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 /* Firmware bug: video endpoint is 5, but commands are sent to endpoint 4 */
Hans de Goede938d5b92012-01-10 13:14:46 -0300337 if (send_to_cam)
338 ret = send_video_command(pdev, 4, buf, 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (ret < 0)
340 return ret;
341
Hans de Goeded167a852012-01-10 13:01:41 -0300342 if (pChoose->bandlength > 0 && pixfmt == V4L2_PIX_FMT_YUV420)
Hans de Goede56ae24a2012-01-08 11:29:19 -0300343 pwc_dec23_init(pdev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 pdev->cmd_len = 12;
346 memcpy(pdev->cmd_buf, buf, 12);
347 /* All set and go */
Hans de Goeded167a852012-01-10 13:01:41 -0300348 pdev->pixfmt = pixfmt;
Hans de Goede54d3fb32012-01-08 07:19:29 -0300349 pdev->vframes = (fps + 1) * 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 pdev->valternate = pChoose->alternate;
Hans de Goede795e6eb2012-01-04 16:58:44 -0300351 pdev->width = pwc_image_sizes[size][0];
352 pdev->height = pwc_image_sizes[size][1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 pdev->vbandlength = pChoose->bandlength;
354 if (pdev->vbandlength > 0)
Hans de Goede795e6eb2012-01-04 16:58:44 -0300355 pdev->frame_size = (pdev->vbandlength * pdev->height) / 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 else
Hans de Goede795e6eb2012-01-04 16:58:44 -0300357 pdev->frame_size = (pdev->width * pdev->height * 12) / 8;
Hans de Goededc8a7e82011-12-31 10:52:02 -0300358 PWC_TRACE("frame_size=%d, vframes=%d, vsize=%d, vbandlength=%d\n",
359 pdev->frame_size, pdev->vframes, size, pdev->vbandlength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return 0;
361}
362
Hans de Goededc8a7e82011-12-31 10:52:02 -0300363int pwc_set_video_mode(struct pwc_device *pdev, int width, int height,
Hans de Goede938d5b92012-01-10 13:14:46 -0300364 int pixfmt, int frames, int *compression, int send_to_cam)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300366 int ret, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Hans de Goede56ae24a2012-01-08 11:29:19 -0300368 PWC_DEBUG_FLOW("set_video_mode(%dx%d @ %d, pixfmt %08x).\n",
Hans de Goeded167a852012-01-10 13:01:41 -0300369 width, height, frames, pixfmt);
Hans de Goede795e6eb2012-01-04 16:58:44 -0300370 size = pwc_get_size(pdev, width, height);
Luc Saillard2b455db2006-04-24 10:29:46 -0300371 PWC_TRACE("decode_size = %d.\n", size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Luc Saillard2b455db2006-04-24 10:29:46 -0300373 if (DEVICE_USE_CODEC1(pdev->type)) {
Hans de Goeded167a852012-01-10 13:01:41 -0300374 ret = set_video_mode_Nala(pdev, size, pixfmt, frames,
Hans de Goede938d5b92012-01-10 13:14:46 -0300375 compression, send_to_cam);
Luc Saillard2b455db2006-04-24 10:29:46 -0300376 } else if (DEVICE_USE_CODEC3(pdev->type)) {
Hans de Goeded167a852012-01-10 13:01:41 -0300377 ret = set_video_mode_Kiara(pdev, size, pixfmt, frames,
Hans de Goede938d5b92012-01-10 13:14:46 -0300378 compression, send_to_cam);
Luc Saillard2b455db2006-04-24 10:29:46 -0300379 } else {
Hans de Goeded167a852012-01-10 13:01:41 -0300380 ret = set_video_mode_Timon(pdev, size, pixfmt, frames,
Hans de Goede938d5b92012-01-10 13:14:46 -0300381 compression, send_to_cam);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 }
383 if (ret < 0) {
Luc Saillard2b455db2006-04-24 10:29:46 -0300384 PWC_ERROR("Failed to set video mode %s@%d fps; return code = %d\n", size2name[size], frames, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return ret;
386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 pdev->frame_total_size = pdev->frame_size + pdev->frame_header_size + pdev->frame_trailer_size;
Hans de Goede795e6eb2012-01-04 16:58:44 -0300388 PWC_DEBUG_SIZE("Set resolution to %dx%d\n", pdev->width, pdev->height);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return 0;
390}
391
Luc Saillard9ee6d782007-04-22 23:54:36 -0300392static unsigned int pwc_get_fps_Nala(struct pwc_device *pdev, unsigned int index, unsigned int size)
393{
394 unsigned int i;
395
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];
399 }
400 }
401 return 0;
402}
403
404static unsigned int pwc_get_fps_Kiara(struct pwc_device *pdev, unsigned int index, unsigned int size)
405{
406 unsigned int i;
407
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];
411 }
412 }
413 return 0;
414}
415
416static unsigned int pwc_get_fps_Timon(struct pwc_device *pdev, unsigned int index, unsigned int size)
417{
418 unsigned int i;
419
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];
423 }
424 }
425 return 0;
426}
427
428unsigned int pwc_get_fps(struct pwc_device *pdev, unsigned int index, unsigned int size)
429{
430 unsigned int ret;
431
432 if (DEVICE_USE_CODEC1(pdev->type)) {
433 ret = pwc_get_fps_Nala(pdev, index, size);
434
435 } else if (DEVICE_USE_CODEC3(pdev->type)) {
436 ret = pwc_get_fps_Kiara(pdev, index, size);
437
438 } else {
439 ret = pwc_get_fps_Timon(pdev, index, size);
440 }
441
442 return ret;
443}
444
Hans de Goede6c9cac82011-06-26 12:52:01 -0300445int pwc_get_u8_ctrl(struct pwc_device *pdev, u8 request, u16 value, int *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 int ret;
Hans de Goede6c9cac82011-06-26 12:52:01 -0300448 u8 buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Hans de Goede6c9cac82011-06-26 12:52:01 -0300450 ret = recv_control_msg(pdev, request, value, &buf, sizeof(buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 if (ret < 0)
452 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Hans de Goede6c9cac82011-06-26 12:52:01 -0300454 *data = buf;
Luc Saillard2b455db2006-04-24 10:29:46 -0300455 return 0;
456}
457
Hans de Goede6c9cac82011-06-26 12:52:01 -0300458int pwc_set_u8_ctrl(struct pwc_device *pdev, u8 request, u16 value, u8 data)
Luc Saillard2b455db2006-04-24 10:29:46 -0300459{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 int ret;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300461
Hans de Goede6c9cac82011-06-26 12:52:01 -0300462 ret = send_control_msg(pdev, request, value, &data, sizeof(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (ret < 0)
464 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 return 0;
467}
468
Hans de Goede6c9cac82011-06-26 12:52:01 -0300469int pwc_get_s8_ctrl(struct pwc_device *pdev, u8 request, u16 value, int *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Luc Saillard2b455db2006-04-24 10:29:46 -0300471 int ret;
Hans de Goede6c9cac82011-06-26 12:52:01 -0300472 s8 buf;
Luc Saillard2b455db2006-04-24 10:29:46 -0300473
Hans de Goede6c9cac82011-06-26 12:52:01 -0300474 ret = recv_control_msg(pdev, request, value, &buf, sizeof(buf));
Luc Saillard2b455db2006-04-24 10:29:46 -0300475 if (ret < 0)
476 return ret;
Hans de Goede6c9cac82011-06-26 12:52:01 -0300477
478 *data = buf;
479 return 0;
480}
481
482int pwc_get_u16_ctrl(struct pwc_device *pdev, u8 request, u16 value, int *data)
483{
484 int ret;
485 u8 buf[2];
486
487 ret = recv_control_msg(pdev, request, value, buf, sizeof(buf));
488 if (ret < 0)
489 return ret;
490
491 *data = (buf[1] << 8) | buf[0];
492 return 0;
493}
494
495int pwc_set_u16_ctrl(struct pwc_device *pdev, u8 request, u16 value, u16 data)
496{
497 int ret;
498 u8 buf[2];
499
500 buf[0] = data & 0xff;
501 buf[1] = data >> 8;
502 ret = send_control_msg(pdev, request, value, buf, sizeof(buf));
503 if (ret < 0)
504 return ret;
505
506 return 0;
507}
508
509int pwc_button_ctrl(struct pwc_device *pdev, u16 value)
510{
511 int ret;
512
513 ret = send_control_msg(pdev, SET_STATUS_CTL, value, NULL, 0);
514 if (ret < 0)
515 return ret;
516
Luc Saillard2b455db2006-04-24 10:29:46 -0300517 return 0;
518}
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520/* POWER */
Hans de Goede3b4d0ec2011-06-26 03:51:19 -0300521void pwc_camera_power(struct pwc_device *pdev, int power)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
523 char buf;
Hans de Goede3b4d0ec2011-06-26 03:51:19 -0300524 int r;
525
526 if (!pdev->power_save)
527 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 if (pdev->type < 675 || (pdev->type < 730 && pdev->release < 6))
Hans de Goede3b4d0ec2011-06-26 03:51:19 -0300530 return; /* Not supported by Nala or Timon < release 6 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 if (power)
533 buf = 0x00; /* active */
534 else
535 buf = 0xFF; /* power save */
Hans de Goede3b4d0ec2011-06-26 03:51:19 -0300536 r = send_control_msg(pdev,
Martin Fuzzey6b35ca02009-04-21 21:48:09 +0200537 SET_STATUS_CTL, SET_POWER_SAVE_MODE_FORMATTER,
538 &buf, sizeof(buf));
Hans de Goede3b4d0ec2011-06-26 03:51:19 -0300539
540 if (r < 0)
541 PWC_ERROR("Failed to power %s camera (%d)\n",
542 power ? "on" : "off", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545int pwc_set_leds(struct pwc_device *pdev, int on_value, int off_value)
546{
547 unsigned char buf[2];
Hans de Goede3b4d0ec2011-06-26 03:51:19 -0300548 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 if (pdev->type < 730)
551 return 0;
552 on_value /= 100;
553 off_value /= 100;
554 if (on_value < 0)
555 on_value = 0;
556 if (on_value > 0xff)
557 on_value = 0xff;
558 if (off_value < 0)
559 off_value = 0;
560 if (off_value > 0xff)
561 off_value = 0xff;
562
563 buf[0] = on_value;
564 buf[1] = off_value;
565
Hans de Goede3b4d0ec2011-06-26 03:51:19 -0300566 r = send_control_msg(pdev,
Martin Fuzzey6b35ca02009-04-21 21:48:09 +0200567 SET_STATUS_CTL, LED_FORMATTER, &buf, sizeof(buf));
Hans de Goede3b4d0ec2011-06-26 03:51:19 -0300568 if (r < 0)
569 PWC_ERROR("Failed to set LED on/off time (%d)\n", r);
570
571 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
Hans de Goede6eba9352011-06-26 06:49:59 -0300574#ifdef CONFIG_USB_PWC_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575int pwc_get_cmos_sensor(struct pwc_device *pdev, int *sensor)
576{
577 unsigned char buf;
578 int ret = -1, request;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 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 */
584 else
585 request = SENSOR_TYPE_FORMATTER2;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300586
Martin Fuzzey6b35ca02009-04-21 21:48:09 +0200587 ret = recv_control_msg(pdev,
588 GET_STATUS_CTL, request, &buf, sizeof(buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (ret < 0)
590 return ret;
591 if (pdev->type < 675)
592 *sensor = buf | 0x100;
593 else
594 *sensor = buf;
595 return 0;
596}
Hans de Goede6eba9352011-06-26 06:49:59 -0300597#endif