pwc-ctrl.c 15 KB

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