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