pwc-ctrl.c 14 KB

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