blob: 3a1618580ed6c6a774713fb44e8c6fcab8d31c5c [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 recv_control_msg(struct pwc_device *pdev,
Hans de Goede24be6892012-01-10 17:02:04 -0300108 u8 request, u16 value, int recv_count)
Martin Fuzzey6b35ca02009-04-21 21:48:09 +0200109{
110 int rc;
Martin Fuzzey6b35ca02009-04-21 21:48:09 +0200111
112 rc = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
113 request,
114 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
Hans de Goede24be6892012-01-10 17:02:04 -0300115 value, pdev->vcinterface,
116 pdev->ctrl_buf, recv_count, USB_CTRL_GET_TIMEOUT);
Hans de Goede6c9cac82011-06-26 12:52:01 -0300117 if (rc < 0)
118 PWC_ERROR("recv_control_msg error %d req %02x val %04x\n",
119 rc, request, value);
Martin Fuzzey6b35ca02009-04-21 21:48:09 +0200120 return rc;
121}
122
123static inline int send_video_command(struct pwc_device *pdev,
Hans de Goede24be6892012-01-10 17:02:04 -0300124 int index, const unsigned char *buf, int buflen)
Martin Fuzzey6b35ca02009-04-21 21:48:09 +0200125{
Hans de Goede24be6892012-01-10 17:02:04 -0300126 int rc;
127
128 memcpy(pdev->ctrl_buf, buf, buflen);
129
130 rc = usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
131 SET_EP_STREAM_CTL,
132 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
133 VIDEO_OUTPUT_CONTROL_FORMATTER, index,
134 pdev->ctrl_buf, buflen, USB_CTRL_SET_TIMEOUT);
135 if (rc >= 0)
136 memcpy(pdev->cmd_buf, buf, buflen);
137 else
138 PWC_ERROR("send_video_command error %d\n", rc);
139
140 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
Hans de Goede294e2892011-07-03 12:23:24 -0300143int send_control_msg(struct pwc_device *pdev,
Martin Fuzzey6b35ca02009-04-21 21:48:09 +0200144 u8 request, u16 value, void *buf, int buflen)
145{
Hans de Goede24be6892012-01-10 17:02:04 -0300146 return usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
147 request,
148 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
149 value, pdev->vcinterface,
150 buf, buflen, USB_CTRL_SET_TIMEOUT);
Martin Fuzzey6b35ca02009-04-21 21:48:09 +0200151}
152
Hans de Goeded167a852012-01-10 13:01:41 -0300153static int set_video_mode_Nala(struct pwc_device *pdev, int size, int pixfmt,
Hans de Goede938d5b92012-01-10 13:14:46 -0300154 int frames, int *compression, int send_to_cam)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Hans de Goede938d5b92012-01-10 13:14:46 -0300156 int fps, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 struct Nala_table_entry *pEntry;
158 int frames2frames[31] =
159 { /* closest match of framerate */
160 0, 0, 0, 0, 4, /* 0-4 */
161 5, 5, 7, 7, 10, /* 5-9 */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300162 10, 10, 12, 12, 15, /* 10-14 */
163 15, 15, 15, 20, 20, /* 15-19 */
164 20, 20, 20, 24, 24, /* 20-24 */
165 24, 24, 24, 24, 24, /* 25-29 */
166 24 /* 30 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 };
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300168 int frames2table[31] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 { 0, 0, 0, 0, 0, /* 0-4 */
170 1, 1, 1, 2, 2, /* 5-9 */
171 3, 3, 4, 4, 4, /* 10-14 */
172 5, 5, 5, 5, 5, /* 15-19 */
173 6, 6, 6, 6, 7, /* 20-24 */
174 7, 7, 7, 7, 7, /* 25-29 */
175 7 /* 30 */
176 };
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300177
Hans de Goede54d3fb32012-01-08 07:19:29 -0300178 if (size < 0 || size > PSZ_CIF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return -EINVAL;
Hans de Goede54d3fb32012-01-08 07:19:29 -0300180 if (frames < 4)
181 frames = 4;
Hans de Goedebb969702012-10-08 06:05:24 -0300182 else if (size > PSZ_QCIF && frames > 15)
183 frames = 15;
Hans de Goede54d3fb32012-01-08 07:19:29 -0300184 else if (frames > 25)
185 frames = 25;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 frames = frames2frames[frames];
187 fps = frames2table[frames];
188 pEntry = &Nala_table[size][fps];
189 if (pEntry->alternate == 0)
190 return -EINVAL;
191
Hans de Goede938d5b92012-01-10 13:14:46 -0300192 if (send_to_cam)
Hans de Goede24be6892012-01-10 17:02:04 -0300193 ret = send_video_command(pdev, pdev->vendpoint,
194 pEntry->mode, 3);
195 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return ret;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300197
Hans de Goede24be6892012-01-10 17:02:04 -0300198 if (pEntry->compressed && pixfmt == V4L2_PIX_FMT_YUV420)
199 pwc_dec1_init(pdev, pEntry->mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 /* Set various parameters */
Hans de Goeded167a852012-01-10 13:01:41 -0300202 pdev->pixfmt = pixfmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 pdev->vframes = frames;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 pdev->valternate = pEntry->alternate;
Hans de Goede795e6eb2012-01-04 16:58:44 -0300205 pdev->width = pwc_image_sizes[size][0];
206 pdev->height = pwc_image_sizes[size][1];
207 pdev->frame_size = (pdev->width * pdev->height * 3) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (pEntry->compressed) {
209 if (pdev->release < 5) { /* 4 fold compression */
210 pdev->vbandlength = 528;
211 pdev->frame_size /= 4;
212 }
213 else {
214 pdev->vbandlength = 704;
215 pdev->frame_size /= 3;
216 }
217 }
218 else
219 pdev->vbandlength = 0;
Hans de Goede5bbe18d2012-01-04 18:48:05 -0300220
221 /* Let pwc-if.c:isoc_init know we don't support higher compression */
222 *compression = 3;
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return 0;
225}
226
227
Hans de Goeded167a852012-01-10 13:01:41 -0300228static int set_video_mode_Timon(struct pwc_device *pdev, int size, int pixfmt,
Hans de Goede938d5b92012-01-10 13:14:46 -0300229 int frames, int *compression, int send_to_cam)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 const struct Timon_table_entry *pChoose;
Hans de Goede938d5b92012-01-10 13:14:46 -0300232 int fps, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Hans de Goede54d3fb32012-01-08 07:19:29 -0300234 if (size >= PSZ_MAX || *compression < 0 || *compression > 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return -EINVAL;
Hans de Goede54d3fb32012-01-08 07:19:29 -0300236 if (frames < 5)
237 frames = 5;
238 else if (size == PSZ_VGA && frames > 15)
239 frames = 15;
240 else if (frames > 30)
241 frames = 30;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 fps = (frames / 5) - 1;
243
Hans de Goede5bbe18d2012-01-04 18:48:05 -0300244 /* Find a supported framerate with progressively higher compression */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 pChoose = NULL;
Hans de Goede5bbe18d2012-01-04 18:48:05 -0300246 while (*compression <= 3) {
247 pChoose = &Timon_table[size][fps][*compression];
248 if (pChoose->alternate != 0)
249 break;
250 (*compression)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
252 if (pChoose == NULL || pChoose->alternate == 0)
253 return -ENOENT; /* Not supported. */
254
Hans de Goede938d5b92012-01-10 13:14:46 -0300255 if (send_to_cam)
Hans de Goede24be6892012-01-10 17:02:04 -0300256 ret = send_video_command(pdev, pdev->vendpoint,
257 pChoose->mode, 13);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (ret < 0)
259 return ret;
260
Hans de Goeded167a852012-01-10 13:01:41 -0300261 if (pChoose->bandlength > 0 && pixfmt == V4L2_PIX_FMT_YUV420)
Hans de Goede24be6892012-01-10 17:02:04 -0300262 pwc_dec23_init(pdev, pChoose->mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 /* Set various parameters */
Hans de Goeded167a852012-01-10 13:01:41 -0300265 pdev->pixfmt = pixfmt;
Hans de Goede54d3fb32012-01-08 07:19:29 -0300266 pdev->vframes = (fps + 1) * 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 pdev->valternate = pChoose->alternate;
Hans de Goede795e6eb2012-01-04 16:58:44 -0300268 pdev->width = pwc_image_sizes[size][0];
269 pdev->height = pwc_image_sizes[size][1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 pdev->vbandlength = pChoose->bandlength;
271 if (pChoose->bandlength > 0)
Hans de Goede795e6eb2012-01-04 16:58:44 -0300272 pdev->frame_size = (pChoose->bandlength * pdev->height) / 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 else
Hans de Goede795e6eb2012-01-04 16:58:44 -0300274 pdev->frame_size = (pdev->width * pdev->height * 12) / 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return 0;
276}
277
278
Hans de Goeded167a852012-01-10 13:01:41 -0300279static int set_video_mode_Kiara(struct pwc_device *pdev, int size, int pixfmt,
Hans de Goede938d5b92012-01-10 13:14:46 -0300280 int frames, int *compression, int send_to_cam)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
282 const struct Kiara_table_entry *pChoose = NULL;
Hans de Goede938d5b92012-01-10 13:14:46 -0300283 int fps, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Hans de Goede54d3fb32012-01-08 07:19:29 -0300285 if (size >= PSZ_MAX || *compression < 0 || *compression > 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return -EINVAL;
Hans de Goede54d3fb32012-01-08 07:19:29 -0300287 if (frames < 5)
288 frames = 5;
289 else if (size == PSZ_VGA && frames > 15)
290 frames = 15;
291 else if (frames > 30)
292 frames = 30;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 fps = (frames / 5) - 1;
294
Hans de Goede5bbe18d2012-01-04 18:48:05 -0300295 /* Find a supported framerate with progressively higher compression */
296 while (*compression <= 3) {
297 pChoose = &Kiara_table[size][fps][*compression];
Hans de Goededc8a7e82011-12-31 10:52:02 -0300298 if (pChoose->alternate != 0)
299 break;
Hans de Goede5bbe18d2012-01-04 18:48:05 -0300300 (*compression)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302 if (pChoose == NULL || pChoose->alternate == 0)
303 return -ENOENT; /* Not supported. */
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 /* Firmware bug: video endpoint is 5, but commands are sent to endpoint 4 */
Hans de Goede938d5b92012-01-10 13:14:46 -0300306 if (send_to_cam)
Hans de Goede24be6892012-01-10 17:02:04 -0300307 ret = send_video_command(pdev, 4, pChoose->mode, 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 if (ret < 0)
309 return ret;
310
Hans de Goeded167a852012-01-10 13:01:41 -0300311 if (pChoose->bandlength > 0 && pixfmt == V4L2_PIX_FMT_YUV420)
Hans de Goede24be6892012-01-10 17:02:04 -0300312 pwc_dec23_init(pdev, pChoose->mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 /* All set and go */
Hans de Goeded167a852012-01-10 13:01:41 -0300315 pdev->pixfmt = pixfmt;
Hans de Goede54d3fb32012-01-08 07:19:29 -0300316 pdev->vframes = (fps + 1) * 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 pdev->valternate = pChoose->alternate;
Hans de Goede795e6eb2012-01-04 16:58:44 -0300318 pdev->width = pwc_image_sizes[size][0];
319 pdev->height = pwc_image_sizes[size][1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 pdev->vbandlength = pChoose->bandlength;
321 if (pdev->vbandlength > 0)
Hans de Goede795e6eb2012-01-04 16:58:44 -0300322 pdev->frame_size = (pdev->vbandlength * pdev->height) / 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 else
Hans de Goede795e6eb2012-01-04 16:58:44 -0300324 pdev->frame_size = (pdev->width * pdev->height * 12) / 8;
Hans de Goededc8a7e82011-12-31 10:52:02 -0300325 PWC_TRACE("frame_size=%d, vframes=%d, vsize=%d, vbandlength=%d\n",
326 pdev->frame_size, pdev->vframes, size, pdev->vbandlength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 return 0;
328}
329
Hans de Goededc8a7e82011-12-31 10:52:02 -0300330int pwc_set_video_mode(struct pwc_device *pdev, int width, int height,
Hans de Goede938d5b92012-01-10 13:14:46 -0300331 int pixfmt, int frames, int *compression, int send_to_cam)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300333 int ret, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Hans de Goede56ae24a2012-01-08 11:29:19 -0300335 PWC_DEBUG_FLOW("set_video_mode(%dx%d @ %d, pixfmt %08x).\n",
Hans de Goeded167a852012-01-10 13:01:41 -0300336 width, height, frames, pixfmt);
Hans de Goede795e6eb2012-01-04 16:58:44 -0300337 size = pwc_get_size(pdev, width, height);
Luc Saillard2b455db2006-04-24 10:29:46 -0300338 PWC_TRACE("decode_size = %d.\n", size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Luc Saillard2b455db2006-04-24 10:29:46 -0300340 if (DEVICE_USE_CODEC1(pdev->type)) {
Hans de Goeded167a852012-01-10 13:01:41 -0300341 ret = set_video_mode_Nala(pdev, size, pixfmt, frames,
Hans de Goede938d5b92012-01-10 13:14:46 -0300342 compression, send_to_cam);
Luc Saillard2b455db2006-04-24 10:29:46 -0300343 } else if (DEVICE_USE_CODEC3(pdev->type)) {
Hans de Goeded167a852012-01-10 13:01:41 -0300344 ret = set_video_mode_Kiara(pdev, size, pixfmt, frames,
Hans de Goede938d5b92012-01-10 13:14:46 -0300345 compression, send_to_cam);
Luc Saillard2b455db2006-04-24 10:29:46 -0300346 } else {
Hans de Goeded167a852012-01-10 13:01:41 -0300347 ret = set_video_mode_Timon(pdev, size, pixfmt, frames,
Hans de Goede938d5b92012-01-10 13:14:46 -0300348 compression, send_to_cam);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
350 if (ret < 0) {
Luc Saillard2b455db2006-04-24 10:29:46 -0300351 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 -0700352 return ret;
353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 pdev->frame_total_size = pdev->frame_size + pdev->frame_header_size + pdev->frame_trailer_size;
Hans de Goede795e6eb2012-01-04 16:58:44 -0300355 PWC_DEBUG_SIZE("Set resolution to %dx%d\n", pdev->width, pdev->height);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return 0;
357}
358
Luc Saillard9ee6d782007-04-22 23:54:36 -0300359static unsigned int pwc_get_fps_Nala(struct pwc_device *pdev, unsigned int index, unsigned int size)
360{
361 unsigned int i;
362
363 for (i = 0; i < PWC_FPS_MAX_NALA; i++) {
364 if (Nala_table[size][i].alternate) {
365 if (index--==0) return Nala_fps_vector[i];
366 }
367 }
368 return 0;
369}
370
371static unsigned int pwc_get_fps_Kiara(struct pwc_device *pdev, unsigned int index, unsigned int size)
372{
373 unsigned int i;
374
375 for (i = 0; i < PWC_FPS_MAX_KIARA; i++) {
376 if (Kiara_table[size][i][3].alternate) {
377 if (index--==0) return Kiara_fps_vector[i];
378 }
379 }
380 return 0;
381}
382
383static unsigned int pwc_get_fps_Timon(struct pwc_device *pdev, unsigned int index, unsigned int size)
384{
385 unsigned int i;
386
387 for (i=0; i < PWC_FPS_MAX_TIMON; i++) {
388 if (Timon_table[size][i][3].alternate) {
389 if (index--==0) return Timon_fps_vector[i];
390 }
391 }
392 return 0;
393}
394
395unsigned int pwc_get_fps(struct pwc_device *pdev, unsigned int index, unsigned int size)
396{
397 unsigned int ret;
398
399 if (DEVICE_USE_CODEC1(pdev->type)) {
400 ret = pwc_get_fps_Nala(pdev, index, size);
401
402 } else if (DEVICE_USE_CODEC3(pdev->type)) {
403 ret = pwc_get_fps_Kiara(pdev, index, size);
404
405 } else {
406 ret = pwc_get_fps_Timon(pdev, index, size);
407 }
408
409 return ret;
410}
411
Hans de Goede6c9cac82011-06-26 12:52:01 -0300412int pwc_get_u8_ctrl(struct pwc_device *pdev, u8 request, u16 value, int *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 int ret;
415
Hans de Goede24be6892012-01-10 17:02:04 -0300416 ret = recv_control_msg(pdev, request, value, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (ret < 0)
418 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Hans de Goede24be6892012-01-10 17:02:04 -0300420 *data = pdev->ctrl_buf[0];
Luc Saillard2b455db2006-04-24 10:29:46 -0300421 return 0;
422}
423
Hans de Goede6c9cac82011-06-26 12:52:01 -0300424int pwc_set_u8_ctrl(struct pwc_device *pdev, u8 request, u16 value, u8 data)
Luc Saillard2b455db2006-04-24 10:29:46 -0300425{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 int ret;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300427
Hans de Goede24be6892012-01-10 17:02:04 -0300428 pdev->ctrl_buf[0] = data;
429 ret = send_control_msg(pdev, request, value, pdev->ctrl_buf, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 if (ret < 0)
431 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 return 0;
434}
435
Hans de Goede6c9cac82011-06-26 12:52:01 -0300436int pwc_get_s8_ctrl(struct pwc_device *pdev, u8 request, u16 value, int *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Luc Saillard2b455db2006-04-24 10:29:46 -0300438 int ret;
439
Hans de Goede24be6892012-01-10 17:02:04 -0300440 ret = recv_control_msg(pdev, request, value, 1);
Luc Saillard2b455db2006-04-24 10:29:46 -0300441 if (ret < 0)
442 return ret;
Hans de Goede6c9cac82011-06-26 12:52:01 -0300443
Hans de Goede24be6892012-01-10 17:02:04 -0300444 *data = ((s8 *)pdev->ctrl_buf)[0];
Hans de Goede6c9cac82011-06-26 12:52:01 -0300445 return 0;
446}
447
448int pwc_get_u16_ctrl(struct pwc_device *pdev, u8 request, u16 value, int *data)
449{
450 int ret;
Hans de Goede6c9cac82011-06-26 12:52:01 -0300451
Hans de Goede24be6892012-01-10 17:02:04 -0300452 ret = recv_control_msg(pdev, request, value, 2);
Hans de Goede6c9cac82011-06-26 12:52:01 -0300453 if (ret < 0)
454 return ret;
455
Hans de Goede24be6892012-01-10 17:02:04 -0300456 *data = (pdev->ctrl_buf[1] << 8) | pdev->ctrl_buf[0];
Hans de Goede6c9cac82011-06-26 12:52:01 -0300457 return 0;
458}
459
460int pwc_set_u16_ctrl(struct pwc_device *pdev, u8 request, u16 value, u16 data)
461{
462 int ret;
Hans de Goede6c9cac82011-06-26 12:52:01 -0300463
Hans de Goede24be6892012-01-10 17:02:04 -0300464 pdev->ctrl_buf[0] = data & 0xff;
465 pdev->ctrl_buf[1] = data >> 8;
466 ret = send_control_msg(pdev, request, value, pdev->ctrl_buf, 2);
Hans de Goede6c9cac82011-06-26 12:52:01 -0300467 if (ret < 0)
468 return ret;
469
470 return 0;
471}
472
473int pwc_button_ctrl(struct pwc_device *pdev, u16 value)
474{
475 int ret;
476
477 ret = send_control_msg(pdev, SET_STATUS_CTL, value, NULL, 0);
478 if (ret < 0)
479 return ret;
480
Luc Saillard2b455db2006-04-24 10:29:46 -0300481 return 0;
482}
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484/* POWER */
Hans de Goede3b4d0ec2011-06-26 03:51:19 -0300485void pwc_camera_power(struct pwc_device *pdev, int power)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Hans de Goede3b4d0ec2011-06-26 03:51:19 -0300487 int r;
488
489 if (!pdev->power_save)
490 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 if (pdev->type < 675 || (pdev->type < 730 && pdev->release < 6))
Hans de Goede3b4d0ec2011-06-26 03:51:19 -0300493 return; /* Not supported by Nala or Timon < release 6 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 if (power)
Hans de Goede24be6892012-01-10 17:02:04 -0300496 pdev->ctrl_buf[0] = 0x00; /* active */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 else
Hans de Goede24be6892012-01-10 17:02:04 -0300498 pdev->ctrl_buf[0] = 0xFF; /* power save */
499 r = send_control_msg(pdev, SET_STATUS_CTL,
500 SET_POWER_SAVE_MODE_FORMATTER, pdev->ctrl_buf, 1);
Hans de Goede3b4d0ec2011-06-26 03:51:19 -0300501 if (r < 0)
502 PWC_ERROR("Failed to power %s camera (%d)\n",
503 power ? "on" : "off", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506int pwc_set_leds(struct pwc_device *pdev, int on_value, int off_value)
507{
Hans de Goede3b4d0ec2011-06-26 03:51:19 -0300508 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 if (pdev->type < 730)
511 return 0;
512 on_value /= 100;
513 off_value /= 100;
514 if (on_value < 0)
515 on_value = 0;
516 if (on_value > 0xff)
517 on_value = 0xff;
518 if (off_value < 0)
519 off_value = 0;
520 if (off_value > 0xff)
521 off_value = 0xff;
522
Hans de Goede24be6892012-01-10 17:02:04 -0300523 pdev->ctrl_buf[0] = on_value;
524 pdev->ctrl_buf[1] = off_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Hans de Goede3b4d0ec2011-06-26 03:51:19 -0300526 r = send_control_msg(pdev,
Hans de Goede24be6892012-01-10 17:02:04 -0300527 SET_STATUS_CTL, LED_FORMATTER, pdev->ctrl_buf, 2);
Hans de Goede3b4d0ec2011-06-26 03:51:19 -0300528 if (r < 0)
529 PWC_ERROR("Failed to set LED on/off time (%d)\n", r);
530
531 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
Hans de Goede6eba9352011-06-26 06:49:59 -0300534#ifdef CONFIG_USB_PWC_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535int pwc_get_cmos_sensor(struct pwc_device *pdev, int *sensor)
536{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 int ret = -1, request;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (pdev->type < 675)
540 request = SENSOR_TYPE_FORMATTER1;
541 else if (pdev->type < 730)
542 return -1; /* The Vesta series doesn't have this call */
543 else
544 request = SENSOR_TYPE_FORMATTER2;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300545
Hans de Goede24be6892012-01-10 17:02:04 -0300546 ret = recv_control_msg(pdev, GET_STATUS_CTL, request, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (ret < 0)
548 return ret;
549 if (pdev->type < 675)
Hans de Goede24be6892012-01-10 17:02:04 -0300550 *sensor = pdev->ctrl_buf[0] | 0x100;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 else
Hans de Goede24be6892012-01-10 17:02:04 -0300552 *sensor = pdev->ctrl_buf[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return 0;
554}
Hans de Goede6eba9352011-06-26 06:49:59 -0300555#endif