blob: 315c55927f5c508df94c895f1107b1cdb3c4d67d [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/* Driver for Philips webcam
3 Functions that send various control messages to the webcam, including
4 video modes.
5 (C) 1999-2003 Nemosoft Unv.
Luc Saillard2b455db2006-04-24 10:29:46 -03006 (C) 2004-2006 Luc Saillard (luc@saillard.org)
Hans de Goede6c9cac82011-06-26 12:52:01 -03007 (C) 2011 Hans de Goede <hdegoede@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008
9 NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
10 driver and thus may have bugs that are not present in the original version.
11 Please send bug reports and support requests to <luc@saillard.org>.
12
13 NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
14 driver and thus may have bugs that are not present in the original version.
15 Please send bug reports and support requests to <luc@saillard.org>.
16 The decompression routines have been implemented by reverse-engineering the
17 Nemosoft binary pwcx module. Caveat emptor.
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019*/
20
21/*
22 Changes
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030023 2001/08/03 Alvarado Added methods for changing white balance and
24 red/green gains
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 */
26
27/* Control functions for the cam; brightness, contrast, video mode, etc. */
28
29#ifdef __KERNEL__
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080030#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#endif
32#include <asm/errno.h>
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "pwc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "pwc-kiara.h"
36#include "pwc-timon.h"
Luc Saillard2b455db2006-04-24 10:29:46 -030037#include "pwc-dec1.h"
38#include "pwc-dec23.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Hans de Goede6c9cac82011-06-26 12:52:01 -030040/* Selectors for status controls used only in this file */
Luc Saillard2b455db2006-04-24 10:29:46 -030041#define GET_STATUS_B00 0x0B00
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#define SENSOR_TYPE_FORMATTER1 0x0C00
Luc Saillard2b455db2006-04-24 10:29:46 -030043#define GET_STATUS_3000 0x3000
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#define READ_RAW_Y_MEAN_FORMATTER 0x3100
45#define SET_POWER_SAVE_MODE_FORMATTER 0x3200
46#define MIRROR_IMAGE_FORMATTER 0x3300
47#define LED_FORMATTER 0x3400
Luc Saillard2b455db2006-04-24 10:29:46 -030048#define LOWLIGHT 0x3500
49#define GET_STATUS_3600 0x3600
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define SENSOR_TYPE_FORMATTER2 0x3700
Luc Saillard2b455db2006-04-24 10:29:46 -030051#define GET_STATUS_3800 0x3800
52#define GET_STATUS_4000 0x4000
53#define GET_STATUS_4100 0x4100 /* Get */
54#define CTL_STATUS_4200 0x4200 /* [GS] 1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56/* Formatters for the Video Endpoint controls [GS]ET_EP_STREAM_CTL */
57#define VIDEO_OUTPUT_CONTROL_FORMATTER 0x0100
58
Arjan van de Ven4c4c9432005-11-29 09:43:42 +010059static const char *size2name[PSZ_MAX] =
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
61 "subQCIF",
62 "QSIF",
63 "QCIF",
64 "SIF",
65 "CIF",
66 "VGA",
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030067};
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69/********/
70
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030071/* Entries for the Nala (645/646) camera; the Nala doesn't have compression
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 preferences, so you either get compressed or non-compressed streams.
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030073
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 An alternate value of 0 means this mode is not available at all.
75 */
76
Luc Saillard9ee6d782007-04-22 23:54:36 -030077#define PWC_FPS_MAX_NALA 8
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079struct Nala_table_entry {
80 char alternate; /* USB alternate setting */
81 int compressed; /* Compressed yes/no */
82
83 unsigned char mode[3]; /* precomputed mode table */
84};
85
Luc Saillard9ee6d782007-04-22 23:54:36 -030086static unsigned int Nala_fps_vector[PWC_FPS_MAX_NALA] = { 4, 5, 7, 10, 12, 15, 20, 24 };
87
88static struct Nala_table_entry Nala_table[PSZ_MAX][PWC_FPS_MAX_NALA] =
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
90#include "pwc-nala.h"
91};
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093/****************************************************************************/
94
Martin Fuzzey6b35ca02009-04-21 21:48:09 +020095static int recv_control_msg(struct pwc_device *pdev,
Hans de Goede24be6892012-01-10 17:02:04 -030096 u8 request, u16 value, int recv_count)
Martin Fuzzey6b35ca02009-04-21 21:48:09 +020097{
98 int rc;
Martin Fuzzey6b35ca02009-04-21 21:48:09 +020099
100 rc = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
101 request,
102 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
Hans de Goede24be6892012-01-10 17:02:04 -0300103 value, pdev->vcinterface,
104 pdev->ctrl_buf, recv_count, USB_CTRL_GET_TIMEOUT);
Hans de Goede6c9cac82011-06-26 12:52:01 -0300105 if (rc < 0)
106 PWC_ERROR("recv_control_msg error %d req %02x val %04x\n",
107 rc, request, value);
Martin Fuzzey6b35ca02009-04-21 21:48:09 +0200108 return rc;
109}
110
111static inline int send_video_command(struct pwc_device *pdev,
Hans de Goede24be6892012-01-10 17:02:04 -0300112 int index, const unsigned char *buf, int buflen)
Martin Fuzzey6b35ca02009-04-21 21:48:09 +0200113{
Hans de Goede24be6892012-01-10 17:02:04 -0300114 int rc;
115
116 memcpy(pdev->ctrl_buf, buf, buflen);
117
118 rc = usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
119 SET_EP_STREAM_CTL,
120 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
121 VIDEO_OUTPUT_CONTROL_FORMATTER, index,
122 pdev->ctrl_buf, buflen, USB_CTRL_SET_TIMEOUT);
123 if (rc >= 0)
124 memcpy(pdev->cmd_buf, buf, buflen);
125 else
126 PWC_ERROR("send_video_command error %d\n", rc);
127
128 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
Hans de Goede294e2892011-07-03 12:23:24 -0300131int send_control_msg(struct pwc_device *pdev,
Martin Fuzzey6b35ca02009-04-21 21:48:09 +0200132 u8 request, u16 value, void *buf, int buflen)
133{
Hans de Goede24be6892012-01-10 17:02:04 -0300134 return usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
135 request,
136 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
137 value, pdev->vcinterface,
138 buf, buflen, USB_CTRL_SET_TIMEOUT);
Martin Fuzzey6b35ca02009-04-21 21:48:09 +0200139}
140
Hans de Goeded167a852012-01-10 13:01:41 -0300141static int set_video_mode_Nala(struct pwc_device *pdev, int size, int pixfmt,
Hans de Goede938d5b92012-01-10 13:14:46 -0300142 int frames, int *compression, int send_to_cam)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Hans de Goede938d5b92012-01-10 13:14:46 -0300144 int fps, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 struct Nala_table_entry *pEntry;
146 int frames2frames[31] =
147 { /* closest match of framerate */
148 0, 0, 0, 0, 4, /* 0-4 */
149 5, 5, 7, 7, 10, /* 5-9 */
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300150 10, 10, 12, 12, 15, /* 10-14 */
151 15, 15, 15, 20, 20, /* 15-19 */
152 20, 20, 20, 24, 24, /* 20-24 */
153 24, 24, 24, 24, 24, /* 25-29 */
154 24 /* 30 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 };
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300156 int frames2table[31] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 { 0, 0, 0, 0, 0, /* 0-4 */
158 1, 1, 1, 2, 2, /* 5-9 */
159 3, 3, 4, 4, 4, /* 10-14 */
160 5, 5, 5, 5, 5, /* 15-19 */
161 6, 6, 6, 6, 7, /* 20-24 */
162 7, 7, 7, 7, 7, /* 25-29 */
163 7 /* 30 */
164 };
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300165
Hans de Goede54d3fb32012-01-08 07:19:29 -0300166 if (size < 0 || size > PSZ_CIF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return -EINVAL;
Hans de Goede54d3fb32012-01-08 07:19:29 -0300168 if (frames < 4)
169 frames = 4;
Hans de Goedebb969702012-10-08 06:05:24 -0300170 else if (size > PSZ_QCIF && frames > 15)
171 frames = 15;
Hans de Goede54d3fb32012-01-08 07:19:29 -0300172 else if (frames > 25)
173 frames = 25;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 frames = frames2frames[frames];
175 fps = frames2table[frames];
176 pEntry = &Nala_table[size][fps];
177 if (pEntry->alternate == 0)
178 return -EINVAL;
179
Hans de Goede938d5b92012-01-10 13:14:46 -0300180 if (send_to_cam)
Hans de Goede24be6892012-01-10 17:02:04 -0300181 ret = send_video_command(pdev, pdev->vendpoint,
182 pEntry->mode, 3);
183 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return ret;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300185
Hans de Goede24be6892012-01-10 17:02:04 -0300186 if (pEntry->compressed && pixfmt == V4L2_PIX_FMT_YUV420)
187 pwc_dec1_init(pdev, pEntry->mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 /* Set various parameters */
Hans de Goeded167a852012-01-10 13:01:41 -0300190 pdev->pixfmt = pixfmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 pdev->vframes = frames;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 pdev->valternate = pEntry->alternate;
Hans de Goede795e6eb2012-01-04 16:58:44 -0300193 pdev->width = pwc_image_sizes[size][0];
194 pdev->height = pwc_image_sizes[size][1];
195 pdev->frame_size = (pdev->width * pdev->height * 3) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 if (pEntry->compressed) {
197 if (pdev->release < 5) { /* 4 fold compression */
198 pdev->vbandlength = 528;
199 pdev->frame_size /= 4;
200 }
201 else {
202 pdev->vbandlength = 704;
203 pdev->frame_size /= 3;
204 }
205 }
206 else
207 pdev->vbandlength = 0;
Hans de Goede5bbe18d2012-01-04 18:48:05 -0300208
209 /* Let pwc-if.c:isoc_init know we don't support higher compression */
210 *compression = 3;
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return 0;
213}
214
215
Hans de Goeded167a852012-01-10 13:01:41 -0300216static int set_video_mode_Timon(struct pwc_device *pdev, int size, int pixfmt,
Hans de Goede938d5b92012-01-10 13:14:46 -0300217 int frames, int *compression, int send_to_cam)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 const struct Timon_table_entry *pChoose;
Hans de Goede938d5b92012-01-10 13:14:46 -0300220 int fps, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Hans de Goede54d3fb32012-01-08 07:19:29 -0300222 if (size >= PSZ_MAX || *compression < 0 || *compression > 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return -EINVAL;
Hans de Goede54d3fb32012-01-08 07:19:29 -0300224 if (frames < 5)
225 frames = 5;
226 else if (size == PSZ_VGA && frames > 15)
227 frames = 15;
228 else if (frames > 30)
229 frames = 30;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 fps = (frames / 5) - 1;
231
Hans de Goede5bbe18d2012-01-04 18:48:05 -0300232 /* Find a supported framerate with progressively higher compression */
Mauro Carvalho Chehab4f777d02019-03-25 16:20:47 -0400233 do {
Hans de Goede5bbe18d2012-01-04 18:48:05 -0300234 pChoose = &Timon_table[size][fps][*compression];
235 if (pChoose->alternate != 0)
236 break;
237 (*compression)++;
Mauro Carvalho Chehab4f777d02019-03-25 16:20:47 -0400238 } while (*compression <= 3);
239
240 if (pChoose->alternate == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return -ENOENT; /* Not supported. */
242
Hans de Goede938d5b92012-01-10 13:14:46 -0300243 if (send_to_cam)
Hans de Goede24be6892012-01-10 17:02:04 -0300244 ret = send_video_command(pdev, pdev->vendpoint,
245 pChoose->mode, 13);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (ret < 0)
247 return ret;
248
Hans de Goeded167a852012-01-10 13:01:41 -0300249 if (pChoose->bandlength > 0 && pixfmt == V4L2_PIX_FMT_YUV420)
Hans de Goede24be6892012-01-10 17:02:04 -0300250 pwc_dec23_init(pdev, pChoose->mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 /* Set various parameters */
Hans de Goeded167a852012-01-10 13:01:41 -0300253 pdev->pixfmt = pixfmt;
Hans de Goede54d3fb32012-01-08 07:19:29 -0300254 pdev->vframes = (fps + 1) * 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 pdev->valternate = pChoose->alternate;
Hans de Goede795e6eb2012-01-04 16:58:44 -0300256 pdev->width = pwc_image_sizes[size][0];
257 pdev->height = pwc_image_sizes[size][1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 pdev->vbandlength = pChoose->bandlength;
259 if (pChoose->bandlength > 0)
Hans de Goede795e6eb2012-01-04 16:58:44 -0300260 pdev->frame_size = (pChoose->bandlength * pdev->height) / 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 else
Hans de Goede795e6eb2012-01-04 16:58:44 -0300262 pdev->frame_size = (pdev->width * pdev->height * 12) / 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 return 0;
264}
265
266
Hans de Goeded167a852012-01-10 13:01:41 -0300267static int set_video_mode_Kiara(struct pwc_device *pdev, int size, int pixfmt,
Hans de Goede938d5b92012-01-10 13:14:46 -0300268 int frames, int *compression, int send_to_cam)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
Mauro Carvalho Chehab4f777d02019-03-25 16:20:47 -0400270 const struct Kiara_table_entry *pChoose;
Hans de Goede938d5b92012-01-10 13:14:46 -0300271 int fps, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Hans de Goede54d3fb32012-01-08 07:19:29 -0300273 if (size >= PSZ_MAX || *compression < 0 || *compression > 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return -EINVAL;
Hans de Goede54d3fb32012-01-08 07:19:29 -0300275 if (frames < 5)
276 frames = 5;
277 else if (size == PSZ_VGA && frames > 15)
278 frames = 15;
279 else if (frames > 30)
280 frames = 30;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 fps = (frames / 5) - 1;
282
Hans de Goede5bbe18d2012-01-04 18:48:05 -0300283 /* Find a supported framerate with progressively higher compression */
Mauro Carvalho Chehab4f777d02019-03-25 16:20:47 -0400284 do {
Hans de Goede5bbe18d2012-01-04 18:48:05 -0300285 pChoose = &Kiara_table[size][fps][*compression];
Hans de Goededc8a7e82011-12-31 10:52:02 -0300286 if (pChoose->alternate != 0)
287 break;
Hans de Goede5bbe18d2012-01-04 18:48:05 -0300288 (*compression)++;
Mauro Carvalho Chehab4f777d02019-03-25 16:20:47 -0400289 } while (*compression <= 3);
290
291 if (pChoose->alternate == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 return -ENOENT; /* Not supported. */
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 /* Firmware bug: video endpoint is 5, but commands are sent to endpoint 4 */
Hans de Goede938d5b92012-01-10 13:14:46 -0300295 if (send_to_cam)
Hans de Goede24be6892012-01-10 17:02:04 -0300296 ret = send_video_command(pdev, 4, pChoose->mode, 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 if (ret < 0)
298 return ret;
299
Hans de Goeded167a852012-01-10 13:01:41 -0300300 if (pChoose->bandlength > 0 && pixfmt == V4L2_PIX_FMT_YUV420)
Hans de Goede24be6892012-01-10 17:02:04 -0300301 pwc_dec23_init(pdev, pChoose->mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 /* All set and go */
Hans de Goeded167a852012-01-10 13:01:41 -0300304 pdev->pixfmt = pixfmt;
Hans de Goede54d3fb32012-01-08 07:19:29 -0300305 pdev->vframes = (fps + 1) * 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 pdev->valternate = pChoose->alternate;
Hans de Goede795e6eb2012-01-04 16:58:44 -0300307 pdev->width = pwc_image_sizes[size][0];
308 pdev->height = pwc_image_sizes[size][1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 pdev->vbandlength = pChoose->bandlength;
310 if (pdev->vbandlength > 0)
Hans de Goede795e6eb2012-01-04 16:58:44 -0300311 pdev->frame_size = (pdev->vbandlength * pdev->height) / 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 else
Hans de Goede795e6eb2012-01-04 16:58:44 -0300313 pdev->frame_size = (pdev->width * pdev->height * 12) / 8;
Hans de Goededc8a7e82011-12-31 10:52:02 -0300314 PWC_TRACE("frame_size=%d, vframes=%d, vsize=%d, vbandlength=%d\n",
315 pdev->frame_size, pdev->vframes, size, pdev->vbandlength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return 0;
317}
318
Hans de Goededc8a7e82011-12-31 10:52:02 -0300319int pwc_set_video_mode(struct pwc_device *pdev, int width, int height,
Hans de Goede938d5b92012-01-10 13:14:46 -0300320 int pixfmt, int frames, int *compression, int send_to_cam)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300322 int ret, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Hans de Goede56ae24a2012-01-08 11:29:19 -0300324 PWC_DEBUG_FLOW("set_video_mode(%dx%d @ %d, pixfmt %08x).\n",
Hans de Goeded167a852012-01-10 13:01:41 -0300325 width, height, frames, pixfmt);
Hans de Goede795e6eb2012-01-04 16:58:44 -0300326 size = pwc_get_size(pdev, width, height);
Luc Saillard2b455db2006-04-24 10:29:46 -0300327 PWC_TRACE("decode_size = %d.\n", size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Luc Saillard2b455db2006-04-24 10:29:46 -0300329 if (DEVICE_USE_CODEC1(pdev->type)) {
Hans de Goeded167a852012-01-10 13:01:41 -0300330 ret = set_video_mode_Nala(pdev, size, pixfmt, frames,
Hans de Goede938d5b92012-01-10 13:14:46 -0300331 compression, send_to_cam);
Luc Saillard2b455db2006-04-24 10:29:46 -0300332 } else if (DEVICE_USE_CODEC3(pdev->type)) {
Hans de Goeded167a852012-01-10 13:01:41 -0300333 ret = set_video_mode_Kiara(pdev, size, pixfmt, frames,
Hans de Goede938d5b92012-01-10 13:14:46 -0300334 compression, send_to_cam);
Luc Saillard2b455db2006-04-24 10:29:46 -0300335 } else {
Hans de Goeded167a852012-01-10 13:01:41 -0300336 ret = set_video_mode_Timon(pdev, size, pixfmt, frames,
Hans de Goede938d5b92012-01-10 13:14:46 -0300337 compression, send_to_cam);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
339 if (ret < 0) {
Luc Saillard2b455db2006-04-24 10:29:46 -0300340 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 -0700341 return ret;
342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 pdev->frame_total_size = pdev->frame_size + pdev->frame_header_size + pdev->frame_trailer_size;
Hans de Goede795e6eb2012-01-04 16:58:44 -0300344 PWC_DEBUG_SIZE("Set resolution to %dx%d\n", pdev->width, pdev->height);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return 0;
346}
347
Luc Saillard9ee6d782007-04-22 23:54:36 -0300348static unsigned int pwc_get_fps_Nala(struct pwc_device *pdev, unsigned int index, unsigned int size)
349{
350 unsigned int i;
351
352 for (i = 0; i < PWC_FPS_MAX_NALA; i++) {
353 if (Nala_table[size][i].alternate) {
354 if (index--==0) return Nala_fps_vector[i];
355 }
356 }
357 return 0;
358}
359
360static unsigned int pwc_get_fps_Kiara(struct pwc_device *pdev, unsigned int index, unsigned int size)
361{
362 unsigned int i;
363
364 for (i = 0; i < PWC_FPS_MAX_KIARA; i++) {
365 if (Kiara_table[size][i][3].alternate) {
366 if (index--==0) return Kiara_fps_vector[i];
367 }
368 }
369 return 0;
370}
371
372static unsigned int pwc_get_fps_Timon(struct pwc_device *pdev, unsigned int index, unsigned int size)
373{
374 unsigned int i;
375
376 for (i=0; i < PWC_FPS_MAX_TIMON; i++) {
377 if (Timon_table[size][i][3].alternate) {
378 if (index--==0) return Timon_fps_vector[i];
379 }
380 }
381 return 0;
382}
383
384unsigned int pwc_get_fps(struct pwc_device *pdev, unsigned int index, unsigned int size)
385{
386 unsigned int ret;
387
388 if (DEVICE_USE_CODEC1(pdev->type)) {
389 ret = pwc_get_fps_Nala(pdev, index, size);
390
391 } else if (DEVICE_USE_CODEC3(pdev->type)) {
392 ret = pwc_get_fps_Kiara(pdev, index, size);
393
394 } else {
395 ret = pwc_get_fps_Timon(pdev, index, size);
396 }
397
398 return ret;
399}
400
Hans de Goede6c9cac82011-06-26 12:52:01 -0300401int pwc_get_u8_ctrl(struct pwc_device *pdev, u8 request, u16 value, int *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 int ret;
404
Hans de Goede24be6892012-01-10 17:02:04 -0300405 ret = recv_control_msg(pdev, request, value, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if (ret < 0)
407 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Hans de Goede24be6892012-01-10 17:02:04 -0300409 *data = pdev->ctrl_buf[0];
Luc Saillard2b455db2006-04-24 10:29:46 -0300410 return 0;
411}
412
Hans de Goede6c9cac82011-06-26 12:52:01 -0300413int pwc_set_u8_ctrl(struct pwc_device *pdev, u8 request, u16 value, u8 data)
Luc Saillard2b455db2006-04-24 10:29:46 -0300414{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 int ret;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300416
Hans de Goede24be6892012-01-10 17:02:04 -0300417 pdev->ctrl_buf[0] = data;
418 ret = send_control_msg(pdev, request, value, pdev->ctrl_buf, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (ret < 0)
420 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 return 0;
423}
424
Hans de Goede6c9cac82011-06-26 12:52:01 -0300425int pwc_get_s8_ctrl(struct pwc_device *pdev, u8 request, u16 value, int *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Luc Saillard2b455db2006-04-24 10:29:46 -0300427 int ret;
428
Hans de Goede24be6892012-01-10 17:02:04 -0300429 ret = recv_control_msg(pdev, request, value, 1);
Luc Saillard2b455db2006-04-24 10:29:46 -0300430 if (ret < 0)
431 return ret;
Hans de Goede6c9cac82011-06-26 12:52:01 -0300432
Hans de Goede24be6892012-01-10 17:02:04 -0300433 *data = ((s8 *)pdev->ctrl_buf)[0];
Hans de Goede6c9cac82011-06-26 12:52:01 -0300434 return 0;
435}
436
437int pwc_get_u16_ctrl(struct pwc_device *pdev, u8 request, u16 value, int *data)
438{
439 int ret;
Hans de Goede6c9cac82011-06-26 12:52:01 -0300440
Hans de Goede24be6892012-01-10 17:02:04 -0300441 ret = recv_control_msg(pdev, request, value, 2);
Hans de Goede6c9cac82011-06-26 12:52:01 -0300442 if (ret < 0)
443 return ret;
444
Hans de Goede24be6892012-01-10 17:02:04 -0300445 *data = (pdev->ctrl_buf[1] << 8) | pdev->ctrl_buf[0];
Hans de Goede6c9cac82011-06-26 12:52:01 -0300446 return 0;
447}
448
449int pwc_set_u16_ctrl(struct pwc_device *pdev, u8 request, u16 value, u16 data)
450{
451 int ret;
Hans de Goede6c9cac82011-06-26 12:52:01 -0300452
Hans de Goede24be6892012-01-10 17:02:04 -0300453 pdev->ctrl_buf[0] = data & 0xff;
454 pdev->ctrl_buf[1] = data >> 8;
455 ret = send_control_msg(pdev, request, value, pdev->ctrl_buf, 2);
Hans de Goede6c9cac82011-06-26 12:52:01 -0300456 if (ret < 0)
457 return ret;
458
459 return 0;
460}
461
462int pwc_button_ctrl(struct pwc_device *pdev, u16 value)
463{
464 int ret;
465
466 ret = send_control_msg(pdev, SET_STATUS_CTL, value, NULL, 0);
467 if (ret < 0)
468 return ret;
469
Luc Saillard2b455db2006-04-24 10:29:46 -0300470 return 0;
471}
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473/* POWER */
Hans de Goede3b4d0ec2011-06-26 03:51:19 -0300474void pwc_camera_power(struct pwc_device *pdev, int power)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Hans de Goede3b4d0ec2011-06-26 03:51:19 -0300476 int r;
477
478 if (!pdev->power_save)
479 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 if (pdev->type < 675 || (pdev->type < 730 && pdev->release < 6))
Hans de Goede3b4d0ec2011-06-26 03:51:19 -0300482 return; /* Not supported by Nala or Timon < release 6 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 if (power)
Hans de Goede24be6892012-01-10 17:02:04 -0300485 pdev->ctrl_buf[0] = 0x00; /* active */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 else
Hans de Goede24be6892012-01-10 17:02:04 -0300487 pdev->ctrl_buf[0] = 0xFF; /* power save */
488 r = send_control_msg(pdev, SET_STATUS_CTL,
489 SET_POWER_SAVE_MODE_FORMATTER, pdev->ctrl_buf, 1);
Hans de Goede3b4d0ec2011-06-26 03:51:19 -0300490 if (r < 0)
491 PWC_ERROR("Failed to power %s camera (%d)\n",
492 power ? "on" : "off", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495int pwc_set_leds(struct pwc_device *pdev, int on_value, int off_value)
496{
Hans de Goede3b4d0ec2011-06-26 03:51:19 -0300497 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 if (pdev->type < 730)
500 return 0;
501 on_value /= 100;
502 off_value /= 100;
503 if (on_value < 0)
504 on_value = 0;
505 if (on_value > 0xff)
506 on_value = 0xff;
507 if (off_value < 0)
508 off_value = 0;
509 if (off_value > 0xff)
510 off_value = 0xff;
511
Hans de Goede24be6892012-01-10 17:02:04 -0300512 pdev->ctrl_buf[0] = on_value;
513 pdev->ctrl_buf[1] = off_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Hans de Goede3b4d0ec2011-06-26 03:51:19 -0300515 r = send_control_msg(pdev,
Hans de Goede24be6892012-01-10 17:02:04 -0300516 SET_STATUS_CTL, LED_FORMATTER, pdev->ctrl_buf, 2);
Hans de Goede3b4d0ec2011-06-26 03:51:19 -0300517 if (r < 0)
518 PWC_ERROR("Failed to set LED on/off time (%d)\n", r);
519
520 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
Hans de Goede6eba9352011-06-26 06:49:59 -0300523#ifdef CONFIG_USB_PWC_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524int pwc_get_cmos_sensor(struct pwc_device *pdev, int *sensor)
525{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 int ret = -1, request;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if (pdev->type < 675)
529 request = SENSOR_TYPE_FORMATTER1;
530 else if (pdev->type < 730)
531 return -1; /* The Vesta series doesn't have this call */
532 else
533 request = SENSOR_TYPE_FORMATTER2;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300534
Hans de Goede24be6892012-01-10 17:02:04 -0300535 ret = recv_control_msg(pdev, GET_STATUS_CTL, request, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (ret < 0)
537 return ret;
538 if (pdev->type < 675)
Hans de Goede24be6892012-01-10 17:02:04 -0300539 *sensor = pdev->ctrl_buf[0] | 0x100;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 else
Hans de Goede24be6892012-01-10 17:02:04 -0300541 *sensor = pdev->ctrl_buf[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 return 0;
543}
Hans de Goede6eba9352011-06-26 06:49:59 -0300544#endif