pwc-ctrl.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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 _send_control_msg(struct pwc_device *pdev,
  86. u8 request, u16 value, int index, void *buf, int buflen)
  87. {
  88. int rc;
  89. void *kbuf = NULL;
  90. if (buflen) {
  91. kbuf = kmemdup(buf, buflen, GFP_KERNEL); /* not allowed on stack */
  92. if (kbuf == NULL)
  93. return -ENOMEM;
  94. }
  95. rc = usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
  96. request,
  97. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  98. value,
  99. index,
  100. kbuf, buflen, USB_CTRL_SET_TIMEOUT);
  101. kfree(kbuf);
  102. return rc;
  103. }
  104. static int recv_control_msg(struct pwc_device *pdev,
  105. u8 request, u16 value, void *buf, int buflen)
  106. {
  107. int rc;
  108. void *kbuf = kmalloc(buflen, GFP_KERNEL); /* not allowed on stack */
  109. if (kbuf == NULL)
  110. return -ENOMEM;
  111. rc = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
  112. request,
  113. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  114. value,
  115. pdev->vcinterface,
  116. kbuf, buflen, USB_CTRL_GET_TIMEOUT);
  117. memcpy(buf, kbuf, buflen);
  118. kfree(kbuf);
  119. if (rc < 0)
  120. PWC_ERROR("recv_control_msg error %d req %02x val %04x\n",
  121. rc, request, value);
  122. return rc;
  123. }
  124. static inline int send_video_command(struct pwc_device *pdev,
  125. int index, void *buf, int buflen)
  126. {
  127. return _send_control_msg(pdev,
  128. SET_EP_STREAM_CTL,
  129. VIDEO_OUTPUT_CONTROL_FORMATTER,
  130. index,
  131. buf, buflen);
  132. }
  133. int send_control_msg(struct pwc_device *pdev,
  134. u8 request, u16 value, void *buf, int buflen)
  135. {
  136. return _send_control_msg(pdev,
  137. request, value, pdev->vcinterface, buf, buflen);
  138. }
  139. static int set_video_mode_Nala(struct pwc_device *pdev, int size, int pixfmt,
  140. int frames, int *compression, int send_to_cam)
  141. {
  142. unsigned char buf[3];
  143. int fps, ret = 0;
  144. struct Nala_table_entry *pEntry;
  145. int frames2frames[31] =
  146. { /* closest match of framerate */
  147. 0, 0, 0, 0, 4, /* 0-4 */
  148. 5, 5, 7, 7, 10, /* 5-9 */
  149. 10, 10, 12, 12, 15, /* 10-14 */
  150. 15, 15, 15, 20, 20, /* 15-19 */
  151. 20, 20, 20, 24, 24, /* 20-24 */
  152. 24, 24, 24, 24, 24, /* 25-29 */
  153. 24 /* 30 */
  154. };
  155. int frames2table[31] =
  156. { 0, 0, 0, 0, 0, /* 0-4 */
  157. 1, 1, 1, 2, 2, /* 5-9 */
  158. 3, 3, 4, 4, 4, /* 10-14 */
  159. 5, 5, 5, 5, 5, /* 15-19 */
  160. 6, 6, 6, 6, 7, /* 20-24 */
  161. 7, 7, 7, 7, 7, /* 25-29 */
  162. 7 /* 30 */
  163. };
  164. if (size < 0 || size > PSZ_CIF)
  165. return -EINVAL;
  166. if (frames < 4)
  167. frames = 4;
  168. else if (frames > 25)
  169. frames = 25;
  170. frames = frames2frames[frames];
  171. fps = frames2table[frames];
  172. pEntry = &Nala_table[size][fps];
  173. if (pEntry->alternate == 0)
  174. return -EINVAL;
  175. memcpy(buf, pEntry->mode, 3);
  176. if (send_to_cam)
  177. ret = send_video_command(pdev, pdev->vendpoint, buf, 3);
  178. if (ret < 0) {
  179. PWC_DEBUG_MODULE("Failed to send video command... %d\n", ret);
  180. return ret;
  181. }
  182. if (pEntry->compressed && pixfmt == V4L2_PIX_FMT_YUV420)
  183. pwc_dec1_init(pdev, buf);
  184. pdev->cmd_len = 3;
  185. memcpy(pdev->cmd_buf, buf, 3);
  186. /* Set various parameters */
  187. pdev->pixfmt = pixfmt;
  188. pdev->vframes = frames;
  189. pdev->valternate = pEntry->alternate;
  190. pdev->width = pwc_image_sizes[size][0];
  191. pdev->height = pwc_image_sizes[size][1];
  192. pdev->frame_size = (pdev->width * pdev->height * 3) / 2;
  193. if (pEntry->compressed) {
  194. if (pdev->release < 5) { /* 4 fold compression */
  195. pdev->vbandlength = 528;
  196. pdev->frame_size /= 4;
  197. }
  198. else {
  199. pdev->vbandlength = 704;
  200. pdev->frame_size /= 3;
  201. }
  202. }
  203. else
  204. pdev->vbandlength = 0;
  205. /* Let pwc-if.c:isoc_init know we don't support higher compression */
  206. *compression = 3;
  207. return 0;
  208. }
  209. static int set_video_mode_Timon(struct pwc_device *pdev, int size, int pixfmt,
  210. int frames, int *compression, int send_to_cam)
  211. {
  212. unsigned char buf[13];
  213. const struct Timon_table_entry *pChoose;
  214. int fps, ret = 0;
  215. if (size >= PSZ_MAX || *compression < 0 || *compression > 3)
  216. return -EINVAL;
  217. if (frames < 5)
  218. frames = 5;
  219. else if (size == PSZ_VGA && frames > 15)
  220. frames = 15;
  221. else if (frames > 30)
  222. frames = 30;
  223. fps = (frames / 5) - 1;
  224. /* Find a supported framerate with progressively higher compression */
  225. pChoose = NULL;
  226. while (*compression <= 3) {
  227. pChoose = &Timon_table[size][fps][*compression];
  228. if (pChoose->alternate != 0)
  229. break;
  230. (*compression)++;
  231. }
  232. if (pChoose == NULL || pChoose->alternate == 0)
  233. return -ENOENT; /* Not supported. */
  234. memcpy(buf, pChoose->mode, 13);
  235. if (send_to_cam)
  236. ret = send_video_command(pdev, pdev->vendpoint, buf, 13);
  237. if (ret < 0)
  238. return ret;
  239. if (pChoose->bandlength > 0 && pixfmt == V4L2_PIX_FMT_YUV420)
  240. pwc_dec23_init(pdev, buf);
  241. pdev->cmd_len = 13;
  242. memcpy(pdev->cmd_buf, buf, 13);
  243. /* Set various parameters */
  244. pdev->pixfmt = pixfmt;
  245. pdev->vframes = (fps + 1) * 5;
  246. pdev->valternate = pChoose->alternate;
  247. pdev->width = pwc_image_sizes[size][0];
  248. pdev->height = pwc_image_sizes[size][1];
  249. pdev->vbandlength = pChoose->bandlength;
  250. if (pChoose->bandlength > 0)
  251. pdev->frame_size = (pChoose->bandlength * pdev->height) / 4;
  252. else
  253. pdev->frame_size = (pdev->width * pdev->height * 12) / 8;
  254. return 0;
  255. }
  256. static int set_video_mode_Kiara(struct pwc_device *pdev, int size, int pixfmt,
  257. int frames, int *compression, int send_to_cam)
  258. {
  259. const struct Kiara_table_entry *pChoose = NULL;
  260. int fps, ret = 0;
  261. unsigned char buf[12];
  262. if (size >= PSZ_MAX || *compression < 0 || *compression > 3)
  263. return -EINVAL;
  264. if (frames < 5)
  265. frames = 5;
  266. else if (size == PSZ_VGA && frames > 15)
  267. frames = 15;
  268. else if (frames > 30)
  269. frames = 30;
  270. fps = (frames / 5) - 1;
  271. /* Find a supported framerate with progressively higher compression */
  272. while (*compression <= 3) {
  273. pChoose = &Kiara_table[size][fps][*compression];
  274. if (pChoose->alternate != 0)
  275. break;
  276. (*compression)++;
  277. }
  278. if (pChoose == NULL || pChoose->alternate == 0)
  279. return -ENOENT; /* Not supported. */
  280. PWC_TRACE("Using alternate setting %d.\n", pChoose->alternate);
  281. /* usb_control_msg won't take staticly allocated arrays as argument?? */
  282. memcpy(buf, pChoose->mode, 12);
  283. /* Firmware bug: video endpoint is 5, but commands are sent to endpoint 4 */
  284. if (send_to_cam)
  285. ret = send_video_command(pdev, 4, buf, 12);
  286. if (ret < 0)
  287. return ret;
  288. if (pChoose->bandlength > 0 && pixfmt == V4L2_PIX_FMT_YUV420)
  289. pwc_dec23_init(pdev, buf);
  290. pdev->cmd_len = 12;
  291. memcpy(pdev->cmd_buf, buf, 12);
  292. /* All set and go */
  293. pdev->pixfmt = pixfmt;
  294. pdev->vframes = (fps + 1) * 5;
  295. pdev->valternate = pChoose->alternate;
  296. pdev->width = pwc_image_sizes[size][0];
  297. pdev->height = pwc_image_sizes[size][1];
  298. pdev->vbandlength = pChoose->bandlength;
  299. if (pdev->vbandlength > 0)
  300. pdev->frame_size = (pdev->vbandlength * pdev->height) / 4;
  301. else
  302. pdev->frame_size = (pdev->width * pdev->height * 12) / 8;
  303. PWC_TRACE("frame_size=%d, vframes=%d, vsize=%d, vbandlength=%d\n",
  304. pdev->frame_size, pdev->vframes, size, pdev->vbandlength);
  305. return 0;
  306. }
  307. int pwc_set_video_mode(struct pwc_device *pdev, int width, int height,
  308. int pixfmt, int frames, int *compression, int send_to_cam)
  309. {
  310. int ret, size;
  311. PWC_DEBUG_FLOW("set_video_mode(%dx%d @ %d, pixfmt %08x).\n",
  312. width, height, frames, pixfmt);
  313. size = pwc_get_size(pdev, width, height);
  314. PWC_TRACE("decode_size = %d.\n", size);
  315. if (DEVICE_USE_CODEC1(pdev->type)) {
  316. ret = set_video_mode_Nala(pdev, size, pixfmt, frames,
  317. compression, send_to_cam);
  318. } else if (DEVICE_USE_CODEC3(pdev->type)) {
  319. ret = set_video_mode_Kiara(pdev, size, pixfmt, frames,
  320. compression, send_to_cam);
  321. } else {
  322. ret = set_video_mode_Timon(pdev, size, pixfmt, frames,
  323. compression, send_to_cam);
  324. }
  325. if (ret < 0) {
  326. PWC_ERROR("Failed to set video mode %s@%d fps; return code = %d\n", size2name[size], frames, ret);
  327. return ret;
  328. }
  329. pdev->frame_total_size = pdev->frame_size + pdev->frame_header_size + pdev->frame_trailer_size;
  330. PWC_DEBUG_SIZE("Set resolution to %dx%d\n", pdev->width, pdev->height);
  331. return 0;
  332. }
  333. static unsigned int pwc_get_fps_Nala(struct pwc_device *pdev, unsigned int index, unsigned int size)
  334. {
  335. unsigned int i;
  336. for (i = 0; i < PWC_FPS_MAX_NALA; i++) {
  337. if (Nala_table[size][i].alternate) {
  338. if (index--==0) return Nala_fps_vector[i];
  339. }
  340. }
  341. return 0;
  342. }
  343. static unsigned int pwc_get_fps_Kiara(struct pwc_device *pdev, unsigned int index, unsigned int size)
  344. {
  345. unsigned int i;
  346. for (i = 0; i < PWC_FPS_MAX_KIARA; i++) {
  347. if (Kiara_table[size][i][3].alternate) {
  348. if (index--==0) return Kiara_fps_vector[i];
  349. }
  350. }
  351. return 0;
  352. }
  353. static unsigned int pwc_get_fps_Timon(struct pwc_device *pdev, unsigned int index, unsigned int size)
  354. {
  355. unsigned int i;
  356. for (i=0; i < PWC_FPS_MAX_TIMON; i++) {
  357. if (Timon_table[size][i][3].alternate) {
  358. if (index--==0) return Timon_fps_vector[i];
  359. }
  360. }
  361. return 0;
  362. }
  363. unsigned int pwc_get_fps(struct pwc_device *pdev, unsigned int index, unsigned int size)
  364. {
  365. unsigned int ret;
  366. if (DEVICE_USE_CODEC1(pdev->type)) {
  367. ret = pwc_get_fps_Nala(pdev, index, size);
  368. } else if (DEVICE_USE_CODEC3(pdev->type)) {
  369. ret = pwc_get_fps_Kiara(pdev, index, size);
  370. } else {
  371. ret = pwc_get_fps_Timon(pdev, index, size);
  372. }
  373. return ret;
  374. }
  375. int pwc_get_u8_ctrl(struct pwc_device *pdev, u8 request, u16 value, int *data)
  376. {
  377. int ret;
  378. u8 buf;
  379. ret = recv_control_msg(pdev, request, value, &buf, sizeof(buf));
  380. if (ret < 0)
  381. return ret;
  382. *data = buf;
  383. return 0;
  384. }
  385. int pwc_set_u8_ctrl(struct pwc_device *pdev, u8 request, u16 value, u8 data)
  386. {
  387. int ret;
  388. ret = send_control_msg(pdev, request, value, &data, sizeof(data));
  389. if (ret < 0)
  390. return ret;
  391. return 0;
  392. }
  393. int pwc_get_s8_ctrl(struct pwc_device *pdev, u8 request, u16 value, int *data)
  394. {
  395. int ret;
  396. s8 buf;
  397. ret = recv_control_msg(pdev, request, value, &buf, sizeof(buf));
  398. if (ret < 0)
  399. return ret;
  400. *data = buf;
  401. return 0;
  402. }
  403. int pwc_get_u16_ctrl(struct pwc_device *pdev, u8 request, u16 value, int *data)
  404. {
  405. int ret;
  406. u8 buf[2];
  407. ret = recv_control_msg(pdev, request, value, buf, sizeof(buf));
  408. if (ret < 0)
  409. return ret;
  410. *data = (buf[1] << 8) | buf[0];
  411. return 0;
  412. }
  413. int pwc_set_u16_ctrl(struct pwc_device *pdev, u8 request, u16 value, u16 data)
  414. {
  415. int ret;
  416. u8 buf[2];
  417. buf[0] = data & 0xff;
  418. buf[1] = data >> 8;
  419. ret = send_control_msg(pdev, request, value, buf, sizeof(buf));
  420. if (ret < 0)
  421. return ret;
  422. return 0;
  423. }
  424. int pwc_button_ctrl(struct pwc_device *pdev, u16 value)
  425. {
  426. int ret;
  427. ret = send_control_msg(pdev, SET_STATUS_CTL, value, NULL, 0);
  428. if (ret < 0)
  429. return ret;
  430. return 0;
  431. }
  432. /* POWER */
  433. void pwc_camera_power(struct pwc_device *pdev, int power)
  434. {
  435. char buf;
  436. int r;
  437. if (!pdev->power_save)
  438. return;
  439. if (pdev->type < 675 || (pdev->type < 730 && pdev->release < 6))
  440. return; /* Not supported by Nala or Timon < release 6 */
  441. if (power)
  442. buf = 0x00; /* active */
  443. else
  444. buf = 0xFF; /* power save */
  445. r = send_control_msg(pdev,
  446. SET_STATUS_CTL, SET_POWER_SAVE_MODE_FORMATTER,
  447. &buf, sizeof(buf));
  448. if (r < 0)
  449. PWC_ERROR("Failed to power %s camera (%d)\n",
  450. power ? "on" : "off", r);
  451. }
  452. int pwc_set_leds(struct pwc_device *pdev, int on_value, int off_value)
  453. {
  454. unsigned char buf[2];
  455. int r;
  456. if (pdev->type < 730)
  457. return 0;
  458. on_value /= 100;
  459. off_value /= 100;
  460. if (on_value < 0)
  461. on_value = 0;
  462. if (on_value > 0xff)
  463. on_value = 0xff;
  464. if (off_value < 0)
  465. off_value = 0;
  466. if (off_value > 0xff)
  467. off_value = 0xff;
  468. buf[0] = on_value;
  469. buf[1] = off_value;
  470. r = send_control_msg(pdev,
  471. SET_STATUS_CTL, LED_FORMATTER, &buf, sizeof(buf));
  472. if (r < 0)
  473. PWC_ERROR("Failed to set LED on/off time (%d)\n", r);
  474. return r;
  475. }
  476. #ifdef CONFIG_USB_PWC_DEBUG
  477. int pwc_get_cmos_sensor(struct pwc_device *pdev, int *sensor)
  478. {
  479. unsigned char buf;
  480. int ret = -1, request;
  481. if (pdev->type < 675)
  482. request = SENSOR_TYPE_FORMATTER1;
  483. else if (pdev->type < 730)
  484. return -1; /* The Vesta series doesn't have this call */
  485. else
  486. request = SENSOR_TYPE_FORMATTER2;
  487. ret = recv_control_msg(pdev,
  488. GET_STATUS_CTL, request, &buf, sizeof(buf));
  489. if (ret < 0)
  490. return ret;
  491. if (pdev->type < 675)
  492. *sensor = buf | 0x100;
  493. else
  494. *sensor = buf;
  495. return 0;
  496. }
  497. #endif