pwc-ctrl.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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.
  5. (C) 2004-2006 Luc Saillard (luc@saillard.org)
  6. (C) 2011 Hans de Goede <hdegoede@redhat.com>
  7. NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
  8. driver and thus may have bugs that are not present in the original version.
  9. Please send bug reports and support requests to <luc@saillard.org>.
  10. NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
  11. driver and thus may have bugs that are not present in the original version.
  12. Please send bug reports and support requests to <luc@saillard.org>.
  13. The decompression routines have been implemented by reverse-engineering the
  14. Nemosoft binary pwcx module. Caveat emptor.
  15. This program is free software; you can redistribute it and/or modify
  16. it under the terms of the GNU General Public License as published by
  17. the Free Software Foundation; either version 2 of the License, or
  18. (at your option) any later version.
  19. This program is distributed in the hope that it will be useful,
  20. but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. GNU General Public License for more details.
  23. You should have received a copy of the GNU General Public License
  24. along with this program; if not, write to the Free Software
  25. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. */
  27. /*
  28. Changes
  29. 2001/08/03 Alvarado Added methods for changing white balance and
  30. red/green gains
  31. */
  32. /* Control functions for the cam; brightness, contrast, video mode, etc. */
  33. #ifdef __KERNEL__
  34. #include <asm/uaccess.h>
  35. #endif
  36. #include <asm/errno.h>
  37. #include "pwc.h"
  38. #include "pwc-kiara.h"
  39. #include "pwc-timon.h"
  40. #include "pwc-dec1.h"
  41. #include "pwc-dec23.h"
  42. /* Selectors for status controls used only in this file */
  43. #define GET_STATUS_B00 0x0B00
  44. #define SENSOR_TYPE_FORMATTER1 0x0C00
  45. #define GET_STATUS_3000 0x3000
  46. #define READ_RAW_Y_MEAN_FORMATTER 0x3100
  47. #define SET_POWER_SAVE_MODE_FORMATTER 0x3200
  48. #define MIRROR_IMAGE_FORMATTER 0x3300
  49. #define LED_FORMATTER 0x3400
  50. #define LOWLIGHT 0x3500
  51. #define GET_STATUS_3600 0x3600
  52. #define SENSOR_TYPE_FORMATTER2 0x3700
  53. #define GET_STATUS_3800 0x3800
  54. #define GET_STATUS_4000 0x4000
  55. #define GET_STATUS_4100 0x4100 /* Get */
  56. #define CTL_STATUS_4200 0x4200 /* [GS] 1 */
  57. /* Formatters for the Video Endpoint controls [GS]ET_EP_STREAM_CTL */
  58. #define VIDEO_OUTPUT_CONTROL_FORMATTER 0x0100
  59. static const char *size2name[PSZ_MAX] =
  60. {
  61. "subQCIF",
  62. "QSIF",
  63. "QCIF",
  64. "SIF",
  65. "CIF",
  66. "VGA",
  67. };
  68. /********/
  69. /* Entries for the Nala (645/646) camera; the Nala doesn't have compression
  70. preferences, so you either get compressed or non-compressed streams.
  71. An alternate value of 0 means this mode is not available at all.
  72. */
  73. #define PWC_FPS_MAX_NALA 8
  74. struct Nala_table_entry {
  75. char alternate; /* USB alternate setting */
  76. int compressed; /* Compressed yes/no */
  77. unsigned char mode[3]; /* precomputed mode table */
  78. };
  79. static unsigned int Nala_fps_vector[PWC_FPS_MAX_NALA] = { 4, 5, 7, 10, 12, 15, 20, 24 };
  80. static struct Nala_table_entry Nala_table[PSZ_MAX][PWC_FPS_MAX_NALA] =
  81. {
  82. #include "pwc-nala.h"
  83. };
  84. /****************************************************************************/
  85. static int recv_control_msg(struct pwc_device *pdev,
  86. u8 request, u16 value, int recv_count)
  87. {
  88. int rc;
  89. rc = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
  90. request,
  91. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  92. value, pdev->vcinterface,
  93. pdev->ctrl_buf, recv_count, USB_CTRL_GET_TIMEOUT);
  94. if (rc < 0)
  95. PWC_ERROR("recv_control_msg error %d req %02x val %04x\n",
  96. rc, request, value);
  97. return rc;
  98. }
  99. static inline int send_video_command(struct pwc_device *pdev,
  100. int index, const unsigned char *buf, int buflen)
  101. {
  102. int rc;
  103. memcpy(pdev->ctrl_buf, buf, buflen);
  104. rc = usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
  105. SET_EP_STREAM_CTL,
  106. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  107. VIDEO_OUTPUT_CONTROL_FORMATTER, index,
  108. pdev->ctrl_buf, buflen, USB_CTRL_SET_TIMEOUT);
  109. if (rc >= 0)
  110. memcpy(pdev->cmd_buf, buf, buflen);
  111. else
  112. PWC_ERROR("send_video_command error %d\n", rc);
  113. return rc;
  114. }
  115. int send_control_msg(struct pwc_device *pdev,
  116. u8 request, u16 value, void *buf, int buflen)
  117. {
  118. return usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
  119. request,
  120. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  121. value, pdev->vcinterface,
  122. buf, buflen, USB_CTRL_SET_TIMEOUT);
  123. }
  124. static int set_video_mode_Nala(struct pwc_device *pdev, int size, int pixfmt,
  125. int frames, int *compression, int send_to_cam)
  126. {
  127. int fps, ret = 0;
  128. struct Nala_table_entry *pEntry;
  129. int frames2frames[31] =
  130. { /* closest match of framerate */
  131. 0, 0, 0, 0, 4, /* 0-4 */
  132. 5, 5, 7, 7, 10, /* 5-9 */
  133. 10, 10, 12, 12, 15, /* 10-14 */
  134. 15, 15, 15, 20, 20, /* 15-19 */
  135. 20, 20, 20, 24, 24, /* 20-24 */
  136. 24, 24, 24, 24, 24, /* 25-29 */
  137. 24 /* 30 */
  138. };
  139. int frames2table[31] =
  140. { 0, 0, 0, 0, 0, /* 0-4 */
  141. 1, 1, 1, 2, 2, /* 5-9 */
  142. 3, 3, 4, 4, 4, /* 10-14 */
  143. 5, 5, 5, 5, 5, /* 15-19 */
  144. 6, 6, 6, 6, 7, /* 20-24 */
  145. 7, 7, 7, 7, 7, /* 25-29 */
  146. 7 /* 30 */
  147. };
  148. if (size < 0 || size > PSZ_CIF)
  149. return -EINVAL;
  150. if (frames < 4)
  151. frames = 4;
  152. else if (frames > 25)
  153. frames = 25;
  154. frames = frames2frames[frames];
  155. fps = frames2table[frames];
  156. pEntry = &Nala_table[size][fps];
  157. if (pEntry->alternate == 0)
  158. return -EINVAL;
  159. if (send_to_cam)
  160. ret = send_video_command(pdev, pdev->vendpoint,
  161. pEntry->mode, 3);
  162. if (ret < 0)
  163. return ret;
  164. if (pEntry->compressed && pixfmt == V4L2_PIX_FMT_YUV420)
  165. pwc_dec1_init(pdev, pEntry->mode);
  166. /* Set various parameters */
  167. pdev->pixfmt = pixfmt;
  168. pdev->vframes = frames;
  169. pdev->valternate = pEntry->alternate;
  170. pdev->width = pwc_image_sizes[size][0];
  171. pdev->height = pwc_image_sizes[size][1];
  172. pdev->frame_size = (pdev->width * pdev->height * 3) / 2;
  173. if (pEntry->compressed) {
  174. if (pdev->release < 5) { /* 4 fold compression */
  175. pdev->vbandlength = 528;
  176. pdev->frame_size /= 4;
  177. }
  178. else {
  179. pdev->vbandlength = 704;
  180. pdev->frame_size /= 3;
  181. }
  182. }
  183. else
  184. pdev->vbandlength = 0;
  185. /* Let pwc-if.c:isoc_init know we don't support higher compression */
  186. *compression = 3;
  187. return 0;
  188. }
  189. static int set_video_mode_Timon(struct pwc_device *pdev, int size, int pixfmt,
  190. int frames, int *compression, int send_to_cam)
  191. {
  192. const struct Timon_table_entry *pChoose;
  193. int fps, ret = 0;
  194. if (size >= PSZ_MAX || *compression < 0 || *compression > 3)
  195. return -EINVAL;
  196. if (frames < 5)
  197. frames = 5;
  198. else if (size == PSZ_VGA && frames > 15)
  199. frames = 15;
  200. else if (frames > 30)
  201. frames = 30;
  202. fps = (frames / 5) - 1;
  203. /* Find a supported framerate with progressively higher compression */
  204. pChoose = NULL;
  205. while (*compression <= 3) {
  206. pChoose = &Timon_table[size][fps][*compression];
  207. if (pChoose->alternate != 0)
  208. break;
  209. (*compression)++;
  210. }
  211. if (pChoose == NULL || pChoose->alternate == 0)
  212. return -ENOENT; /* Not supported. */
  213. if (send_to_cam)
  214. ret = send_video_command(pdev, pdev->vendpoint,
  215. pChoose->mode, 13);
  216. if (ret < 0)
  217. return ret;
  218. if (pChoose->bandlength > 0 && pixfmt == V4L2_PIX_FMT_YUV420)
  219. pwc_dec23_init(pdev, pChoose->mode);
  220. /* Set various parameters */
  221. pdev->pixfmt = pixfmt;
  222. pdev->vframes = (fps + 1) * 5;
  223. pdev->valternate = pChoose->alternate;
  224. pdev->width = pwc_image_sizes[size][0];
  225. pdev->height = pwc_image_sizes[size][1];
  226. pdev->vbandlength = pChoose->bandlength;
  227. if (pChoose->bandlength > 0)
  228. pdev->frame_size = (pChoose->bandlength * pdev->height) / 4;
  229. else
  230. pdev->frame_size = (pdev->width * pdev->height * 12) / 8;
  231. return 0;
  232. }
  233. static int set_video_mode_Kiara(struct pwc_device *pdev, int size, int pixfmt,
  234. int frames, int *compression, int send_to_cam)
  235. {
  236. const struct Kiara_table_entry *pChoose = NULL;
  237. int fps, ret = 0;
  238. if (size >= PSZ_MAX || *compression < 0 || *compression > 3)
  239. return -EINVAL;
  240. if (frames < 5)
  241. frames = 5;
  242. else if (size == PSZ_VGA && frames > 15)
  243. frames = 15;
  244. else if (frames > 30)
  245. frames = 30;
  246. fps = (frames / 5) - 1;
  247. /* Find a supported framerate with progressively higher compression */
  248. while (*compression <= 3) {
  249. pChoose = &Kiara_table[size][fps][*compression];
  250. if (pChoose->alternate != 0)
  251. break;
  252. (*compression)++;
  253. }
  254. if (pChoose == NULL || pChoose->alternate == 0)
  255. return -ENOENT; /* Not supported. */
  256. /* Firmware bug: video endpoint is 5, but commands are sent to endpoint 4 */
  257. if (send_to_cam)
  258. ret = send_video_command(pdev, 4, pChoose->mode, 12);
  259. if (ret < 0)
  260. return ret;
  261. if (pChoose->bandlength > 0 && pixfmt == V4L2_PIX_FMT_YUV420)
  262. pwc_dec23_init(pdev, pChoose->mode);
  263. /* All set and go */
  264. pdev->pixfmt = pixfmt;
  265. pdev->vframes = (fps + 1) * 5;
  266. pdev->valternate = pChoose->alternate;
  267. pdev->width = pwc_image_sizes[size][0];
  268. pdev->height = pwc_image_sizes[size][1];
  269. pdev->vbandlength = pChoose->bandlength;
  270. if (pdev->vbandlength > 0)
  271. pdev->frame_size = (pdev->vbandlength * pdev->height) / 4;
  272. else
  273. pdev->frame_size = (pdev->width * pdev->height * 12) / 8;
  274. PWC_TRACE("frame_size=%d, vframes=%d, vsize=%d, vbandlength=%d\n",
  275. pdev->frame_size, pdev->vframes, size, pdev->vbandlength);
  276. return 0;
  277. }
  278. int pwc_set_video_mode(struct pwc_device *pdev, int width, int height,
  279. int pixfmt, int frames, int *compression, int send_to_cam)
  280. {
  281. int ret, size;
  282. PWC_DEBUG_FLOW("set_video_mode(%dx%d @ %d, pixfmt %08x).\n",
  283. width, height, frames, pixfmt);
  284. size = pwc_get_size(pdev, width, height);
  285. PWC_TRACE("decode_size = %d.\n", size);
  286. if (DEVICE_USE_CODEC1(pdev->type)) {
  287. ret = set_video_mode_Nala(pdev, size, pixfmt, frames,
  288. compression, send_to_cam);
  289. } else if (DEVICE_USE_CODEC3(pdev->type)) {
  290. ret = set_video_mode_Kiara(pdev, size, pixfmt, frames,
  291. compression, send_to_cam);
  292. } else {
  293. ret = set_video_mode_Timon(pdev, size, pixfmt, frames,
  294. compression, send_to_cam);
  295. }
  296. if (ret < 0) {
  297. PWC_ERROR("Failed to set video mode %s@%d fps; return code = %d\n", size2name[size], frames, ret);
  298. return ret;
  299. }
  300. pdev->frame_total_size = pdev->frame_size + pdev->frame_header_size + pdev->frame_trailer_size;
  301. PWC_DEBUG_SIZE("Set resolution to %dx%d\n", pdev->width, pdev->height);
  302. return 0;
  303. }
  304. static unsigned int pwc_get_fps_Nala(struct pwc_device *pdev, unsigned int index, unsigned int size)
  305. {
  306. unsigned int i;
  307. for (i = 0; i < PWC_FPS_MAX_NALA; i++) {
  308. if (Nala_table[size][i].alternate) {
  309. if (index--==0) return Nala_fps_vector[i];
  310. }
  311. }
  312. return 0;
  313. }
  314. static unsigned int pwc_get_fps_Kiara(struct pwc_device *pdev, unsigned int index, unsigned int size)
  315. {
  316. unsigned int i;
  317. for (i = 0; i < PWC_FPS_MAX_KIARA; i++) {
  318. if (Kiara_table[size][i][3].alternate) {
  319. if (index--==0) return Kiara_fps_vector[i];
  320. }
  321. }
  322. return 0;
  323. }
  324. static unsigned int pwc_get_fps_Timon(struct pwc_device *pdev, unsigned int index, unsigned int size)
  325. {
  326. unsigned int i;
  327. for (i=0; i < PWC_FPS_MAX_TIMON; i++) {
  328. if (Timon_table[size][i][3].alternate) {
  329. if (index--==0) return Timon_fps_vector[i];
  330. }
  331. }
  332. return 0;
  333. }
  334. unsigned int pwc_get_fps(struct pwc_device *pdev, unsigned int index, unsigned int size)
  335. {
  336. unsigned int ret;
  337. if (DEVICE_USE_CODEC1(pdev->type)) {
  338. ret = pwc_get_fps_Nala(pdev, index, size);
  339. } else if (DEVICE_USE_CODEC3(pdev->type)) {
  340. ret = pwc_get_fps_Kiara(pdev, index, size);
  341. } else {
  342. ret = pwc_get_fps_Timon(pdev, index, size);
  343. }
  344. return ret;
  345. }
  346. int pwc_get_u8_ctrl(struct pwc_device *pdev, u8 request, u16 value, int *data)
  347. {
  348. int ret;
  349. ret = recv_control_msg(pdev, request, value, 1);
  350. if (ret < 0)
  351. return ret;
  352. *data = pdev->ctrl_buf[0];
  353. return 0;
  354. }
  355. int pwc_set_u8_ctrl(struct pwc_device *pdev, u8 request, u16 value, u8 data)
  356. {
  357. int ret;
  358. pdev->ctrl_buf[0] = data;
  359. ret = send_control_msg(pdev, request, value, pdev->ctrl_buf, 1);
  360. if (ret < 0)
  361. return ret;
  362. return 0;
  363. }
  364. int pwc_get_s8_ctrl(struct pwc_device *pdev, u8 request, u16 value, int *data)
  365. {
  366. int ret;
  367. ret = recv_control_msg(pdev, request, value, 1);
  368. if (ret < 0)
  369. return ret;
  370. *data = ((s8 *)pdev->ctrl_buf)[0];
  371. return 0;
  372. }
  373. int pwc_get_u16_ctrl(struct pwc_device *pdev, u8 request, u16 value, int *data)
  374. {
  375. int ret;
  376. ret = recv_control_msg(pdev, request, value, 2);
  377. if (ret < 0)
  378. return ret;
  379. *data = (pdev->ctrl_buf[1] << 8) | pdev->ctrl_buf[0];
  380. return 0;
  381. }
  382. int pwc_set_u16_ctrl(struct pwc_device *pdev, u8 request, u16 value, u16 data)
  383. {
  384. int ret;
  385. pdev->ctrl_buf[0] = data & 0xff;
  386. pdev->ctrl_buf[1] = data >> 8;
  387. ret = send_control_msg(pdev, request, value, pdev->ctrl_buf, 2);
  388. if (ret < 0)
  389. return ret;
  390. return 0;
  391. }
  392. int pwc_button_ctrl(struct pwc_device *pdev, u16 value)
  393. {
  394. int ret;
  395. ret = send_control_msg(pdev, SET_STATUS_CTL, value, NULL, 0);
  396. if (ret < 0)
  397. return ret;
  398. return 0;
  399. }
  400. /* POWER */
  401. void pwc_camera_power(struct pwc_device *pdev, int power)
  402. {
  403. int r;
  404. if (!pdev->power_save)
  405. return;
  406. if (pdev->type < 675 || (pdev->type < 730 && pdev->release < 6))
  407. return; /* Not supported by Nala or Timon < release 6 */
  408. if (power)
  409. pdev->ctrl_buf[0] = 0x00; /* active */
  410. else
  411. pdev->ctrl_buf[0] = 0xFF; /* power save */
  412. r = send_control_msg(pdev, SET_STATUS_CTL,
  413. SET_POWER_SAVE_MODE_FORMATTER, pdev->ctrl_buf, 1);
  414. if (r < 0)
  415. PWC_ERROR("Failed to power %s camera (%d)\n",
  416. power ? "on" : "off", r);
  417. }
  418. int pwc_set_leds(struct pwc_device *pdev, int on_value, int off_value)
  419. {
  420. int r;
  421. if (pdev->type < 730)
  422. return 0;
  423. on_value /= 100;
  424. off_value /= 100;
  425. if (on_value < 0)
  426. on_value = 0;
  427. if (on_value > 0xff)
  428. on_value = 0xff;
  429. if (off_value < 0)
  430. off_value = 0;
  431. if (off_value > 0xff)
  432. off_value = 0xff;
  433. pdev->ctrl_buf[0] = on_value;
  434. pdev->ctrl_buf[1] = off_value;
  435. r = send_control_msg(pdev,
  436. SET_STATUS_CTL, LED_FORMATTER, pdev->ctrl_buf, 2);
  437. if (r < 0)
  438. PWC_ERROR("Failed to set LED on/off time (%d)\n", r);
  439. return r;
  440. }
  441. #ifdef CONFIG_USB_PWC_DEBUG
  442. int pwc_get_cmos_sensor(struct pwc_device *pdev, int *sensor)
  443. {
  444. int ret = -1, request;
  445. if (pdev->type < 675)
  446. request = SENSOR_TYPE_FORMATTER1;
  447. else if (pdev->type < 730)
  448. return -1; /* The Vesta series doesn't have this call */
  449. else
  450. request = SENSOR_TYPE_FORMATTER2;
  451. ret = recv_control_msg(pdev, GET_STATUS_CTL, request, 1);
  452. if (ret < 0)
  453. return ret;
  454. if (pdev->type < 675)
  455. *sensor = pdev->ctrl_buf[0] | 0x100;
  456. else
  457. *sensor = pdev->ctrl_buf[0];
  458. return 0;
  459. }
  460. #endif