pwc-ctrl.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  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-uncompress.h"
  39. #include "pwc-kiara.h"
  40. #include "pwc-timon.h"
  41. #include "pwc-dec1.h"
  42. #include "pwc-dec23.h"
  43. /* Selectors for status controls used only in this file */
  44. #define GET_STATUS_B00 0x0B00
  45. #define SENSOR_TYPE_FORMATTER1 0x0C00
  46. #define GET_STATUS_3000 0x3000
  47. #define READ_RAW_Y_MEAN_FORMATTER 0x3100
  48. #define SET_POWER_SAVE_MODE_FORMATTER 0x3200
  49. #define MIRROR_IMAGE_FORMATTER 0x3300
  50. #define LED_FORMATTER 0x3400
  51. #define LOWLIGHT 0x3500
  52. #define GET_STATUS_3600 0x3600
  53. #define SENSOR_TYPE_FORMATTER2 0x3700
  54. #define GET_STATUS_3800 0x3800
  55. #define GET_STATUS_4000 0x4000
  56. #define GET_STATUS_4100 0x4100 /* Get */
  57. #define CTL_STATUS_4200 0x4200 /* [GS] 1 */
  58. /* Formatters for the Video Endpoint controls [GS]ET_EP_STREAM_CTL */
  59. #define VIDEO_OUTPUT_CONTROL_FORMATTER 0x0100
  60. static const char *size2name[PSZ_MAX] =
  61. {
  62. "subQCIF",
  63. "QSIF",
  64. "QCIF",
  65. "SIF",
  66. "CIF",
  67. "VGA",
  68. };
  69. /********/
  70. /* Entries for the Nala (645/646) camera; the Nala doesn't have compression
  71. preferences, so you either get compressed or non-compressed streams.
  72. An alternate value of 0 means this mode is not available at all.
  73. */
  74. #define PWC_FPS_MAX_NALA 8
  75. struct Nala_table_entry {
  76. char alternate; /* USB alternate setting */
  77. int compressed; /* Compressed yes/no */
  78. unsigned char mode[3]; /* precomputed mode table */
  79. };
  80. static unsigned int Nala_fps_vector[PWC_FPS_MAX_NALA] = { 4, 5, 7, 10, 12, 15, 20, 24 };
  81. static struct Nala_table_entry Nala_table[PSZ_MAX][PWC_FPS_MAX_NALA] =
  82. {
  83. #include "pwc-nala.h"
  84. };
  85. static void pwc_set_image_buffer_size(struct pwc_device *pdev);
  86. /****************************************************************************/
  87. static int _send_control_msg(struct pwc_device *pdev,
  88. u8 request, u16 value, int index, void *buf, int buflen)
  89. {
  90. int rc;
  91. void *kbuf = NULL;
  92. if (buflen) {
  93. kbuf = kmalloc(buflen, GFP_KERNEL); /* not allowed on stack */
  94. if (kbuf == NULL)
  95. return -ENOMEM;
  96. memcpy(kbuf, buf, buflen);
  97. }
  98. rc = usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
  99. request,
  100. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  101. value,
  102. index,
  103. kbuf, buflen, USB_CTRL_SET_TIMEOUT);
  104. kfree(kbuf);
  105. return rc;
  106. }
  107. static int recv_control_msg(struct pwc_device *pdev,
  108. u8 request, u16 value, void *buf, int buflen)
  109. {
  110. int rc;
  111. void *kbuf = kmalloc(buflen, GFP_KERNEL); /* not allowed on stack */
  112. if (kbuf == NULL)
  113. return -ENOMEM;
  114. rc = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
  115. request,
  116. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  117. value,
  118. pdev->vcinterface,
  119. kbuf, buflen, USB_CTRL_GET_TIMEOUT);
  120. memcpy(buf, kbuf, buflen);
  121. kfree(kbuf);
  122. if (rc < 0)
  123. PWC_ERROR("recv_control_msg error %d req %02x val %04x\n",
  124. rc, request, value);
  125. return rc;
  126. }
  127. static inline int send_video_command(struct pwc_device *pdev,
  128. int index, void *buf, int buflen)
  129. {
  130. return _send_control_msg(pdev,
  131. SET_EP_STREAM_CTL,
  132. VIDEO_OUTPUT_CONTROL_FORMATTER,
  133. index,
  134. buf, buflen);
  135. }
  136. int send_control_msg(struct pwc_device *pdev,
  137. u8 request, u16 value, void *buf, int buflen)
  138. {
  139. return _send_control_msg(pdev,
  140. request, value, pdev->vcinterface, buf, buflen);
  141. }
  142. static int set_video_mode_Nala(struct pwc_device *pdev, int size, int frames)
  143. {
  144. unsigned char buf[3];
  145. int ret, fps;
  146. struct Nala_table_entry *pEntry;
  147. int frames2frames[31] =
  148. { /* closest match of framerate */
  149. 0, 0, 0, 0, 4, /* 0-4 */
  150. 5, 5, 7, 7, 10, /* 5-9 */
  151. 10, 10, 12, 12, 15, /* 10-14 */
  152. 15, 15, 15, 20, 20, /* 15-19 */
  153. 20, 20, 20, 24, 24, /* 20-24 */
  154. 24, 24, 24, 24, 24, /* 25-29 */
  155. 24 /* 30 */
  156. };
  157. int frames2table[31] =
  158. { 0, 0, 0, 0, 0, /* 0-4 */
  159. 1, 1, 1, 2, 2, /* 5-9 */
  160. 3, 3, 4, 4, 4, /* 10-14 */
  161. 5, 5, 5, 5, 5, /* 15-19 */
  162. 6, 6, 6, 6, 7, /* 20-24 */
  163. 7, 7, 7, 7, 7, /* 25-29 */
  164. 7 /* 30 */
  165. };
  166. if (size < 0 || size > PSZ_CIF || frames < 4 || frames > 25)
  167. return -EINVAL;
  168. frames = frames2frames[frames];
  169. fps = frames2table[frames];
  170. pEntry = &Nala_table[size][fps];
  171. if (pEntry->alternate == 0)
  172. return -EINVAL;
  173. memcpy(buf, pEntry->mode, 3);
  174. ret = send_video_command(pdev, pdev->vendpoint, buf, 3);
  175. if (ret < 0) {
  176. PWC_DEBUG_MODULE("Failed to send video command... %d\n", ret);
  177. return ret;
  178. }
  179. if (pEntry->compressed && pdev->pixfmt == V4L2_PIX_FMT_YUV420) {
  180. ret = pwc_dec1_init(pdev, pdev->type, pdev->release, buf);
  181. if (ret < 0)
  182. return ret;
  183. }
  184. pdev->cmd_len = 3;
  185. memcpy(pdev->cmd_buf, buf, 3);
  186. /* Set various parameters */
  187. pdev->vframes = frames;
  188. pdev->vsize = size;
  189. pdev->valternate = pEntry->alternate;
  190. pdev->image = pwc_image_sizes[size];
  191. pdev->frame_size = (pdev->image.x * pdev->image.y * 3) / 2;
  192. if (pEntry->compressed) {
  193. if (pdev->release < 5) { /* 4 fold compression */
  194. pdev->vbandlength = 528;
  195. pdev->frame_size /= 4;
  196. }
  197. else {
  198. pdev->vbandlength = 704;
  199. pdev->frame_size /= 3;
  200. }
  201. }
  202. else
  203. pdev->vbandlength = 0;
  204. return 0;
  205. }
  206. static int set_video_mode_Timon(struct pwc_device *pdev, int size, int frames, int compression, int snapshot)
  207. {
  208. unsigned char buf[13];
  209. const struct Timon_table_entry *pChoose;
  210. int ret, fps;
  211. if (size >= PSZ_MAX || frames < 5 || frames > 30 || compression < 0 || compression > 3)
  212. return -EINVAL;
  213. if (size == PSZ_VGA && frames > 15)
  214. return -EINVAL;
  215. fps = (frames / 5) - 1;
  216. /* Find a supported framerate with progressively higher compression ratios
  217. if the preferred ratio is not available.
  218. */
  219. pChoose = NULL;
  220. while (compression <= 3) {
  221. pChoose = &Timon_table[size][fps][compression];
  222. if (pChoose->alternate != 0)
  223. break;
  224. compression++;
  225. }
  226. if (pChoose == NULL || pChoose->alternate == 0)
  227. return -ENOENT; /* Not supported. */
  228. memcpy(buf, pChoose->mode, 13);
  229. if (snapshot)
  230. buf[0] |= 0x80;
  231. ret = send_video_command(pdev, pdev->vendpoint, buf, 13);
  232. if (ret < 0)
  233. return ret;
  234. if (pChoose->bandlength > 0 && pdev->pixfmt == V4L2_PIX_FMT_YUV420) {
  235. ret = pwc_dec23_init(pdev, pdev->type, buf);
  236. if (ret < 0)
  237. return ret;
  238. }
  239. pdev->cmd_len = 13;
  240. memcpy(pdev->cmd_buf, buf, 13);
  241. /* Set various parameters */
  242. pdev->vframes = frames;
  243. pdev->vsize = size;
  244. pdev->vsnapshot = snapshot;
  245. pdev->valternate = pChoose->alternate;
  246. pdev->image = pwc_image_sizes[size];
  247. pdev->vbandlength = pChoose->bandlength;
  248. if (pChoose->bandlength > 0)
  249. pdev->frame_size = (pChoose->bandlength * pdev->image.y) / 4;
  250. else
  251. pdev->frame_size = (pdev->image.x * pdev->image.y * 12) / 8;
  252. return 0;
  253. }
  254. static int set_video_mode_Kiara(struct pwc_device *pdev, int size, int frames, int compression, int snapshot)
  255. {
  256. const struct Kiara_table_entry *pChoose = NULL;
  257. int fps, ret;
  258. unsigned char buf[12];
  259. struct Kiara_table_entry RawEntry = {6, 773, 1272, {0xAD, 0xF4, 0x10, 0x27, 0xB6, 0x24, 0x96, 0x02, 0x30, 0x05, 0x03, 0x80}};
  260. if (size >= PSZ_MAX || frames < 5 || frames > 30 || compression < 0 || compression > 3)
  261. return -EINVAL;
  262. if (size == PSZ_VGA && frames > 15)
  263. return -EINVAL;
  264. fps = (frames / 5) - 1;
  265. /* special case: VGA @ 5 fps and snapshot is raw bayer mode */
  266. if (size == PSZ_VGA && frames == 5 && snapshot && pdev->pixfmt != V4L2_PIX_FMT_YUV420)
  267. {
  268. /* Only available in case the raw palette is selected or
  269. we have the decompressor available. This mode is
  270. only available in compressed form
  271. */
  272. PWC_DEBUG_SIZE("Choosing VGA/5 BAYER mode.\n");
  273. pChoose = &RawEntry;
  274. }
  275. else
  276. {
  277. /* Find a supported framerate with progressively higher compression ratios
  278. if the preferred ratio is not available.
  279. Skip this step when using RAW modes.
  280. */
  281. snapshot = 0;
  282. while (compression <= 3) {
  283. pChoose = &Kiara_table[size][fps][compression];
  284. if (pChoose->alternate != 0)
  285. break;
  286. compression++;
  287. }
  288. }
  289. if (pChoose == NULL || pChoose->alternate == 0)
  290. return -ENOENT; /* Not supported. */
  291. PWC_TRACE("Using alternate setting %d.\n", pChoose->alternate);
  292. /* usb_control_msg won't take staticly allocated arrays as argument?? */
  293. memcpy(buf, pChoose->mode, 12);
  294. if (snapshot)
  295. buf[0] |= 0x80;
  296. /* Firmware bug: video endpoint is 5, but commands are sent to endpoint 4 */
  297. ret = send_video_command(pdev, 4 /* pdev->vendpoint */, buf, 12);
  298. if (ret < 0)
  299. return ret;
  300. if (pChoose->bandlength > 0 && pdev->pixfmt == V4L2_PIX_FMT_YUV420) {
  301. ret = pwc_dec23_init(pdev, pdev->type, buf);
  302. if (ret < 0)
  303. return ret;
  304. }
  305. pdev->cmd_len = 12;
  306. memcpy(pdev->cmd_buf, buf, 12);
  307. /* All set and go */
  308. pdev->vframes = frames;
  309. pdev->vsize = size;
  310. pdev->vsnapshot = snapshot;
  311. pdev->valternate = pChoose->alternate;
  312. pdev->image = pwc_image_sizes[size];
  313. pdev->vbandlength = pChoose->bandlength;
  314. if (pdev->vbandlength > 0)
  315. pdev->frame_size = (pdev->vbandlength * pdev->image.y) / 4;
  316. else
  317. pdev->frame_size = (pdev->image.x * pdev->image.y * 12) / 8;
  318. PWC_TRACE("frame_size=%d, vframes=%d, vsize=%d, vsnapshot=%d, vbandlength=%d\n",
  319. pdev->frame_size,pdev->vframes,pdev->vsize,pdev->vsnapshot,pdev->vbandlength);
  320. return 0;
  321. }
  322. /**
  323. @pdev: device structure
  324. @width: viewport width
  325. @height: viewport height
  326. @frame: framerate, in fps
  327. @compression: preferred compression ratio
  328. @snapshot: snapshot mode or streaming
  329. */
  330. int pwc_set_video_mode(struct pwc_device *pdev, int width, int height, int frames, int compression, int snapshot)
  331. {
  332. int ret, size;
  333. PWC_DEBUG_FLOW("set_video_mode(%dx%d @ %d, pixfmt %08x).\n", width, height, frames, pdev->pixfmt);
  334. size = pwc_decode_size(pdev, width, height);
  335. if (size < 0) {
  336. PWC_DEBUG_MODULE("Could not find suitable size.\n");
  337. return -ERANGE;
  338. }
  339. PWC_TRACE("decode_size = %d.\n", size);
  340. if (DEVICE_USE_CODEC1(pdev->type)) {
  341. ret = set_video_mode_Nala(pdev, size, frames);
  342. } else if (DEVICE_USE_CODEC3(pdev->type)) {
  343. ret = set_video_mode_Kiara(pdev, size, frames, compression, snapshot);
  344. } else {
  345. ret = set_video_mode_Timon(pdev, size, frames, compression, snapshot);
  346. }
  347. if (ret < 0) {
  348. PWC_ERROR("Failed to set video mode %s@%d fps; return code = %d\n", size2name[size], frames, ret);
  349. return ret;
  350. }
  351. pdev->view.x = width;
  352. pdev->view.y = height;
  353. pdev->vcompression = compression;
  354. pdev->frame_total_size = pdev->frame_size + pdev->frame_header_size + pdev->frame_trailer_size;
  355. pwc_set_image_buffer_size(pdev);
  356. PWC_DEBUG_SIZE("Set viewport to %dx%d, image size is %dx%d.\n", width, height, pwc_image_sizes[size].x, pwc_image_sizes[size].y);
  357. return 0;
  358. }
  359. static unsigned int pwc_get_fps_Nala(struct pwc_device *pdev, unsigned int index, unsigned int size)
  360. {
  361. unsigned int i;
  362. for (i = 0; i < PWC_FPS_MAX_NALA; i++) {
  363. if (Nala_table[size][i].alternate) {
  364. if (index--==0) return Nala_fps_vector[i];
  365. }
  366. }
  367. return 0;
  368. }
  369. static unsigned int pwc_get_fps_Kiara(struct pwc_device *pdev, unsigned int index, unsigned int size)
  370. {
  371. unsigned int i;
  372. for (i = 0; i < PWC_FPS_MAX_KIARA; i++) {
  373. if (Kiara_table[size][i][3].alternate) {
  374. if (index--==0) return Kiara_fps_vector[i];
  375. }
  376. }
  377. return 0;
  378. }
  379. static unsigned int pwc_get_fps_Timon(struct pwc_device *pdev, unsigned int index, unsigned int size)
  380. {
  381. unsigned int i;
  382. for (i=0; i < PWC_FPS_MAX_TIMON; i++) {
  383. if (Timon_table[size][i][3].alternate) {
  384. if (index--==0) return Timon_fps_vector[i];
  385. }
  386. }
  387. return 0;
  388. }
  389. unsigned int pwc_get_fps(struct pwc_device *pdev, unsigned int index, unsigned int size)
  390. {
  391. unsigned int ret;
  392. if (DEVICE_USE_CODEC1(pdev->type)) {
  393. ret = pwc_get_fps_Nala(pdev, index, size);
  394. } else if (DEVICE_USE_CODEC3(pdev->type)) {
  395. ret = pwc_get_fps_Kiara(pdev, index, size);
  396. } else {
  397. ret = pwc_get_fps_Timon(pdev, index, size);
  398. }
  399. return ret;
  400. }
  401. static void pwc_set_image_buffer_size(struct pwc_device *pdev)
  402. {
  403. int factor = 0;
  404. /* for V4L2_PIX_FMT_YUV420 */
  405. switch (pdev->pixfmt) {
  406. case V4L2_PIX_FMT_YUV420:
  407. factor = 6;
  408. break;
  409. case V4L2_PIX_FMT_PWC1:
  410. case V4L2_PIX_FMT_PWC2:
  411. factor = 6; /* can be uncompressed YUV420P */
  412. break;
  413. }
  414. /* Set sizes in bytes */
  415. pdev->image.size = pdev->image.x * pdev->image.y * factor / 4;
  416. pdev->view.size = pdev->view.x * pdev->view.y * factor / 4;
  417. /* Align offset, or you'll get some very weird results in
  418. YUV420 mode... x must be multiple of 4 (to get the Y's in
  419. place), and y even (or you'll mixup U & V). This is less of a
  420. problem for YUV420P.
  421. */
  422. pdev->offset.x = ((pdev->view.x - pdev->image.x) / 2) & 0xFFFC;
  423. pdev->offset.y = ((pdev->view.y - pdev->image.y) / 2) & 0xFFFE;
  424. }
  425. int pwc_get_u8_ctrl(struct pwc_device *pdev, u8 request, u16 value, int *data)
  426. {
  427. int ret;
  428. u8 buf;
  429. ret = recv_control_msg(pdev, request, value, &buf, sizeof(buf));
  430. if (ret < 0)
  431. return ret;
  432. *data = buf;
  433. return 0;
  434. }
  435. int pwc_set_u8_ctrl(struct pwc_device *pdev, u8 request, u16 value, u8 data)
  436. {
  437. int ret;
  438. ret = send_control_msg(pdev, request, value, &data, sizeof(data));
  439. if (ret < 0)
  440. return ret;
  441. return 0;
  442. }
  443. int pwc_get_s8_ctrl(struct pwc_device *pdev, u8 request, u16 value, int *data)
  444. {
  445. int ret;
  446. s8 buf;
  447. ret = recv_control_msg(pdev, request, value, &buf, sizeof(buf));
  448. if (ret < 0)
  449. return ret;
  450. *data = buf;
  451. return 0;
  452. }
  453. int pwc_get_u16_ctrl(struct pwc_device *pdev, u8 request, u16 value, int *data)
  454. {
  455. int ret;
  456. u8 buf[2];
  457. ret = recv_control_msg(pdev, request, value, buf, sizeof(buf));
  458. if (ret < 0)
  459. return ret;
  460. *data = (buf[1] << 8) | buf[0];
  461. return 0;
  462. }
  463. int pwc_set_u16_ctrl(struct pwc_device *pdev, u8 request, u16 value, u16 data)
  464. {
  465. int ret;
  466. u8 buf[2];
  467. buf[0] = data & 0xff;
  468. buf[1] = data >> 8;
  469. ret = send_control_msg(pdev, request, value, buf, sizeof(buf));
  470. if (ret < 0)
  471. return ret;
  472. return 0;
  473. }
  474. int pwc_button_ctrl(struct pwc_device *pdev, u16 value)
  475. {
  476. int ret;
  477. ret = send_control_msg(pdev, SET_STATUS_CTL, value, NULL, 0);
  478. if (ret < 0)
  479. return ret;
  480. return 0;
  481. }
  482. /* POWER */
  483. void pwc_camera_power(struct pwc_device *pdev, int power)
  484. {
  485. char buf;
  486. int r;
  487. if (!pdev->power_save)
  488. return;
  489. if (pdev->type < 675 || (pdev->type < 730 && pdev->release < 6))
  490. return; /* Not supported by Nala or Timon < release 6 */
  491. if (power)
  492. buf = 0x00; /* active */
  493. else
  494. buf = 0xFF; /* power save */
  495. r = send_control_msg(pdev,
  496. SET_STATUS_CTL, SET_POWER_SAVE_MODE_FORMATTER,
  497. &buf, sizeof(buf));
  498. if (r < 0)
  499. PWC_ERROR("Failed to power %s camera (%d)\n",
  500. power ? "on" : "off", r);
  501. }
  502. static int pwc_set_wb_speed(struct pwc_device *pdev, int speed)
  503. {
  504. unsigned char buf;
  505. /* useful range is 0x01..0x20 */
  506. buf = speed / 0x7f0;
  507. return send_control_msg(pdev,
  508. SET_CHROM_CTL, AWB_CONTROL_SPEED_FORMATTER, &buf, sizeof(buf));
  509. }
  510. static int pwc_get_wb_speed(struct pwc_device *pdev, int *value)
  511. {
  512. unsigned char buf;
  513. int ret;
  514. ret = recv_control_msg(pdev,
  515. GET_CHROM_CTL, AWB_CONTROL_SPEED_FORMATTER, &buf, sizeof(buf));
  516. if (ret < 0)
  517. return ret;
  518. *value = buf * 0x7f0;
  519. return 0;
  520. }
  521. static int pwc_set_wb_delay(struct pwc_device *pdev, int delay)
  522. {
  523. unsigned char buf;
  524. /* useful range is 0x01..0x3F */
  525. buf = (delay >> 10);
  526. return send_control_msg(pdev,
  527. SET_CHROM_CTL, AWB_CONTROL_DELAY_FORMATTER, &buf, sizeof(buf));
  528. }
  529. static int pwc_get_wb_delay(struct pwc_device *pdev, int *value)
  530. {
  531. unsigned char buf;
  532. int ret;
  533. ret = recv_control_msg(pdev,
  534. GET_CHROM_CTL, AWB_CONTROL_DELAY_FORMATTER, &buf, sizeof(buf));
  535. if (ret < 0)
  536. return ret;
  537. *value = buf << 10;
  538. return 0;
  539. }
  540. int pwc_set_leds(struct pwc_device *pdev, int on_value, int off_value)
  541. {
  542. unsigned char buf[2];
  543. int r;
  544. if (pdev->type < 730)
  545. return 0;
  546. on_value /= 100;
  547. off_value /= 100;
  548. if (on_value < 0)
  549. on_value = 0;
  550. if (on_value > 0xff)
  551. on_value = 0xff;
  552. if (off_value < 0)
  553. off_value = 0;
  554. if (off_value > 0xff)
  555. off_value = 0xff;
  556. buf[0] = on_value;
  557. buf[1] = off_value;
  558. r = send_control_msg(pdev,
  559. SET_STATUS_CTL, LED_FORMATTER, &buf, sizeof(buf));
  560. if (r < 0)
  561. PWC_ERROR("Failed to set LED on/off time (%d)\n", r);
  562. return r;
  563. }
  564. static int pwc_get_leds(struct pwc_device *pdev, int *on_value, int *off_value)
  565. {
  566. unsigned char buf[2];
  567. int ret;
  568. if (pdev->type < 730) {
  569. *on_value = -1;
  570. *off_value = -1;
  571. return 0;
  572. }
  573. ret = recv_control_msg(pdev,
  574. GET_STATUS_CTL, LED_FORMATTER, &buf, sizeof(buf));
  575. if (ret < 0)
  576. return ret;
  577. *on_value = buf[0] * 100;
  578. *off_value = buf[1] * 100;
  579. return 0;
  580. }
  581. static int _pwc_mpt_reset(struct pwc_device *pdev, int flags)
  582. {
  583. unsigned char buf;
  584. buf = flags & 0x03; // only lower two bits are currently used
  585. return send_control_msg(pdev,
  586. SET_MPT_CTL, PT_RESET_CONTROL_FORMATTER, &buf, sizeof(buf));
  587. }
  588. int pwc_mpt_reset(struct pwc_device *pdev, int flags)
  589. {
  590. int ret;
  591. ret = _pwc_mpt_reset(pdev, flags);
  592. if (ret >= 0) {
  593. pdev->pan_angle = 0;
  594. pdev->tilt_angle = 0;
  595. }
  596. return ret;
  597. }
  598. static int _pwc_mpt_set_angle(struct pwc_device *pdev, int pan, int tilt)
  599. {
  600. unsigned char buf[4];
  601. /* set new relative angle; angles are expressed in degrees * 100,
  602. but cam as .5 degree resolution, hence divide by 200. Also
  603. the angle must be multiplied by 64 before it's send to
  604. the cam (??)
  605. */
  606. pan = 64 * pan / 100;
  607. tilt = -64 * tilt / 100; /* positive tilt is down, which is not what the user would expect */
  608. buf[0] = pan & 0xFF;
  609. buf[1] = (pan >> 8) & 0xFF;
  610. buf[2] = tilt & 0xFF;
  611. buf[3] = (tilt >> 8) & 0xFF;
  612. return send_control_msg(pdev,
  613. SET_MPT_CTL, PT_RELATIVE_CONTROL_FORMATTER, &buf, sizeof(buf));
  614. }
  615. int pwc_mpt_set_angle(struct pwc_device *pdev, int pan, int tilt)
  616. {
  617. int ret;
  618. /* check absolute ranges */
  619. if (pan < pdev->angle_range.pan_min ||
  620. pan > pdev->angle_range.pan_max ||
  621. tilt < pdev->angle_range.tilt_min ||
  622. tilt > pdev->angle_range.tilt_max)
  623. return -ERANGE;
  624. /* go to relative range, check again */
  625. pan -= pdev->pan_angle;
  626. tilt -= pdev->tilt_angle;
  627. /* angles are specified in degrees * 100, thus the limit = 36000 */
  628. if (pan < -36000 || pan > 36000 || tilt < -36000 || tilt > 36000)
  629. return -ERANGE;
  630. ret = _pwc_mpt_set_angle(pdev, pan, tilt);
  631. if (ret >= 0) {
  632. pdev->pan_angle += pan;
  633. pdev->tilt_angle += tilt;
  634. }
  635. if (ret == -EPIPE) /* stall -> out of range */
  636. ret = -ERANGE;
  637. return ret;
  638. }
  639. static int pwc_mpt_get_status(struct pwc_device *pdev, struct pwc_mpt_status *status)
  640. {
  641. int ret;
  642. unsigned char buf[5];
  643. ret = recv_control_msg(pdev,
  644. GET_MPT_CTL, PT_STATUS_FORMATTER, &buf, sizeof(buf));
  645. if (ret < 0)
  646. return ret;
  647. status->status = buf[0] & 0x7; // 3 bits are used for reporting
  648. status->time_pan = (buf[1] << 8) + buf[2];
  649. status->time_tilt = (buf[3] << 8) + buf[4];
  650. return 0;
  651. }
  652. #ifdef CONFIG_USB_PWC_DEBUG
  653. int pwc_get_cmos_sensor(struct pwc_device *pdev, int *sensor)
  654. {
  655. unsigned char buf;
  656. int ret = -1, request;
  657. if (pdev->type < 675)
  658. request = SENSOR_TYPE_FORMATTER1;
  659. else if (pdev->type < 730)
  660. return -1; /* The Vesta series doesn't have this call */
  661. else
  662. request = SENSOR_TYPE_FORMATTER2;
  663. ret = recv_control_msg(pdev,
  664. GET_STATUS_CTL, request, &buf, sizeof(buf));
  665. if (ret < 0)
  666. return ret;
  667. if (pdev->type < 675)
  668. *sensor = buf | 0x100;
  669. else
  670. *sensor = buf;
  671. return 0;
  672. }
  673. #endif
  674. /* End of Add-Ons */
  675. /* ************************************************* */
  676. /* Linux 2.5.something and 2.6 pass direct pointers to arguments of
  677. ioctl() calls. With 2.4, you have to do tedious copy_from_user()
  678. and copy_to_user() calls. With these macros we circumvent this,
  679. and let me maintain only one source file. The functionality is
  680. exactly the same otherwise.
  681. */
  682. /* define local variable for arg */
  683. #define ARG_DEF(ARG_type, ARG_name)\
  684. ARG_type *ARG_name = arg;
  685. /* copy arg to local variable */
  686. #define ARG_IN(ARG_name) /* nothing */
  687. /* argument itself (referenced) */
  688. #define ARGR(ARG_name) (*ARG_name)
  689. /* argument address */
  690. #define ARGA(ARG_name) ARG_name
  691. /* copy local variable to arg */
  692. #define ARG_OUT(ARG_name) /* nothing */
  693. /*
  694. * Our ctrls use native values, but the old custom pwc ioctl interface expects
  695. * values from 0 - 65535, define 2 helper functions to scale things. */
  696. static int pwc_ioctl_g_ctrl(struct v4l2_ctrl *ctrl)
  697. {
  698. return v4l2_ctrl_g_ctrl(ctrl) * 65535 / ctrl->maximum;
  699. }
  700. static int pwc_ioctl_s_ctrl(struct v4l2_ctrl *ctrl, int val)
  701. {
  702. return v4l2_ctrl_s_ctrl(ctrl, val * ctrl->maximum / 65535);
  703. }
  704. long pwc_ioctl(struct pwc_device *pdev, unsigned int cmd, void *arg)
  705. {
  706. long ret = 0;
  707. switch(cmd) {
  708. case VIDIOCPWCRUSER:
  709. ret = pwc_button_ctrl(pdev, RESTORE_USER_DEFAULTS_FORMATTER);
  710. break;
  711. case VIDIOCPWCSUSER:
  712. ret = pwc_button_ctrl(pdev, SAVE_USER_DEFAULTS_FORMATTER);
  713. break;
  714. case VIDIOCPWCFACTORY:
  715. ret = pwc_button_ctrl(pdev, RESTORE_FACTORY_DEFAULTS_FORMATTER);
  716. break;
  717. case VIDIOCPWCSCQUAL:
  718. {
  719. ARG_DEF(int, qual)
  720. if (vb2_is_streaming(&pdev->vb_queue)) {
  721. ret = -EBUSY;
  722. break;
  723. }
  724. ARG_IN(qual)
  725. if (ARGR(qual) < 0 || ARGR(qual) > 3)
  726. ret = -EINVAL;
  727. else
  728. ret = pwc_set_video_mode(pdev, pdev->view.x, pdev->view.y, pdev->vframes, ARGR(qual), pdev->vsnapshot);
  729. break;
  730. }
  731. case VIDIOCPWCGCQUAL:
  732. {
  733. ARG_DEF(int, qual)
  734. ARGR(qual) = pdev->vcompression;
  735. ARG_OUT(qual)
  736. break;
  737. }
  738. case VIDIOCPWCPROBE:
  739. {
  740. ARG_DEF(struct pwc_probe, probe)
  741. strcpy(ARGR(probe).name, pdev->vdev.name);
  742. ARGR(probe).type = pdev->type;
  743. ARG_OUT(probe)
  744. break;
  745. }
  746. case VIDIOCPWCGSERIAL:
  747. {
  748. ARG_DEF(struct pwc_serial, serial)
  749. strcpy(ARGR(serial).serial, pdev->serial);
  750. ARG_OUT(serial)
  751. break;
  752. }
  753. case VIDIOCPWCSAGC:
  754. {
  755. ARG_DEF(int, agc)
  756. ARG_IN(agc)
  757. ret = v4l2_ctrl_s_ctrl(pdev->autogain, ARGR(agc) < 0);
  758. if (ret == 0 && ARGR(agc) >= 0)
  759. ret = pwc_ioctl_s_ctrl(pdev->gain, ARGR(agc));
  760. break;
  761. }
  762. case VIDIOCPWCGAGC:
  763. {
  764. ARG_DEF(int, agc)
  765. if (v4l2_ctrl_g_ctrl(pdev->autogain))
  766. ARGR(agc) = -1;
  767. else
  768. ARGR(agc) = pwc_ioctl_g_ctrl(pdev->gain);
  769. ARG_OUT(agc)
  770. break;
  771. }
  772. case VIDIOCPWCSSHUTTER:
  773. {
  774. ARG_DEF(int, shutter)
  775. ARG_IN(shutter)
  776. ret = v4l2_ctrl_s_ctrl(pdev->exposure_auto,
  777. /* Menu idx 0 = auto, idx 1 = manual */
  778. ARGR(shutter) >= 0);
  779. if (ret == 0 && ARGR(shutter) >= 0)
  780. ret = pwc_ioctl_s_ctrl(pdev->exposure, ARGR(shutter));
  781. break;
  782. }
  783. case VIDIOCPWCSAWB:
  784. {
  785. ARG_DEF(struct pwc_whitebalance, wb)
  786. ARG_IN(wb)
  787. ret = v4l2_ctrl_s_ctrl(pdev->auto_white_balance,
  788. ARGR(wb).mode);
  789. if (ret == 0 && ARGR(wb).mode == PWC_WB_MANUAL)
  790. ret = pwc_ioctl_s_ctrl(pdev->red_balance,
  791. ARGR(wb).manual_red);
  792. if (ret == 0 && ARGR(wb).mode == PWC_WB_MANUAL)
  793. ret = pwc_ioctl_s_ctrl(pdev->blue_balance,
  794. ARGR(wb).manual_blue);
  795. break;
  796. }
  797. case VIDIOCPWCGAWB:
  798. {
  799. ARG_DEF(struct pwc_whitebalance, wb)
  800. ARGR(wb).mode = v4l2_ctrl_g_ctrl(pdev->auto_white_balance);
  801. ARGR(wb).manual_red = ARGR(wb).read_red =
  802. pwc_ioctl_g_ctrl(pdev->red_balance);
  803. ARGR(wb).manual_blue = ARGR(wb).read_blue =
  804. pwc_ioctl_g_ctrl(pdev->blue_balance);
  805. ARG_OUT(wb)
  806. break;
  807. }
  808. case VIDIOCPWCSAWBSPEED:
  809. {
  810. ARG_DEF(struct pwc_wb_speed, wbs)
  811. if (ARGR(wbs).control_speed > 0) {
  812. ret = pwc_set_wb_speed(pdev, ARGR(wbs).control_speed);
  813. }
  814. if (ARGR(wbs).control_delay > 0) {
  815. ret = pwc_set_wb_delay(pdev, ARGR(wbs).control_delay);
  816. }
  817. break;
  818. }
  819. case VIDIOCPWCGAWBSPEED:
  820. {
  821. ARG_DEF(struct pwc_wb_speed, wbs)
  822. ret = pwc_get_wb_speed(pdev, &ARGR(wbs).control_speed);
  823. if (ret < 0)
  824. break;
  825. ret = pwc_get_wb_delay(pdev, &ARGR(wbs).control_delay);
  826. if (ret < 0)
  827. break;
  828. ARG_OUT(wbs)
  829. break;
  830. }
  831. case VIDIOCPWCSLED:
  832. {
  833. ARG_DEF(struct pwc_leds, leds)
  834. ARG_IN(leds)
  835. ret = pwc_set_leds(pdev, ARGR(leds).led_on, ARGR(leds).led_off);
  836. break;
  837. }
  838. case VIDIOCPWCGLED:
  839. {
  840. ARG_DEF(struct pwc_leds, leds)
  841. ret = pwc_get_leds(pdev, &ARGR(leds).led_on, &ARGR(leds).led_off);
  842. ARG_OUT(leds)
  843. break;
  844. }
  845. case VIDIOCPWCSCONTOUR:
  846. {
  847. ARG_DEF(int, contour)
  848. ARG_IN(contour)
  849. ret = v4l2_ctrl_s_ctrl(pdev->autocontour, ARGR(contour) < 0);
  850. if (ret == 0 && ARGR(contour) >= 0)
  851. ret = pwc_ioctl_s_ctrl(pdev->contour, ARGR(contour));
  852. break;
  853. }
  854. case VIDIOCPWCGCONTOUR:
  855. {
  856. ARG_DEF(int, contour)
  857. if (v4l2_ctrl_g_ctrl(pdev->autocontour))
  858. ARGR(contour) = -1;
  859. else
  860. ARGR(contour) = pwc_ioctl_g_ctrl(pdev->contour);
  861. ARG_OUT(contour)
  862. break;
  863. }
  864. case VIDIOCPWCSBACKLIGHT:
  865. {
  866. ARG_DEF(int, backlight)
  867. ARG_IN(backlight)
  868. ret = v4l2_ctrl_s_ctrl(pdev->backlight, ARGR(backlight));
  869. break;
  870. }
  871. case VIDIOCPWCGBACKLIGHT:
  872. {
  873. ARG_DEF(int, backlight)
  874. ARGR(backlight) = v4l2_ctrl_g_ctrl(pdev->backlight);
  875. ARG_OUT(backlight)
  876. break;
  877. }
  878. case VIDIOCPWCSFLICKER:
  879. {
  880. ARG_DEF(int, flicker)
  881. ARG_IN(flicker)
  882. ret = v4l2_ctrl_s_ctrl(pdev->flicker, ARGR(flicker));
  883. break;
  884. }
  885. case VIDIOCPWCGFLICKER:
  886. {
  887. ARG_DEF(int, flicker)
  888. ARGR(flicker) = v4l2_ctrl_g_ctrl(pdev->flicker);
  889. ARG_OUT(flicker)
  890. break;
  891. }
  892. case VIDIOCPWCSDYNNOISE:
  893. {
  894. ARG_DEF(int, dynnoise)
  895. ARG_IN(dynnoise)
  896. ret = v4l2_ctrl_s_ctrl(pdev->noise_reduction, ARGR(dynnoise));
  897. break;
  898. }
  899. case VIDIOCPWCGDYNNOISE:
  900. {
  901. ARG_DEF(int, dynnoise)
  902. ARGR(dynnoise) = v4l2_ctrl_g_ctrl(pdev->noise_reduction);
  903. ARG_OUT(dynnoise);
  904. break;
  905. }
  906. case VIDIOCPWCGREALSIZE:
  907. {
  908. ARG_DEF(struct pwc_imagesize, size)
  909. ARGR(size).width = pdev->image.x;
  910. ARGR(size).height = pdev->image.y;
  911. ARG_OUT(size)
  912. break;
  913. }
  914. case VIDIOCPWCMPTRESET:
  915. {
  916. if (pdev->features & FEATURE_MOTOR_PANTILT)
  917. {
  918. ARG_DEF(int, flags)
  919. ARG_IN(flags)
  920. ret = pwc_mpt_reset(pdev, ARGR(flags));
  921. }
  922. else
  923. {
  924. ret = -ENXIO;
  925. }
  926. break;
  927. }
  928. case VIDIOCPWCMPTGRANGE:
  929. {
  930. if (pdev->features & FEATURE_MOTOR_PANTILT)
  931. {
  932. ARG_DEF(struct pwc_mpt_range, range)
  933. ARGR(range) = pdev->angle_range;
  934. ARG_OUT(range)
  935. }
  936. else
  937. {
  938. ret = -ENXIO;
  939. }
  940. break;
  941. }
  942. case VIDIOCPWCMPTSANGLE:
  943. {
  944. int new_pan, new_tilt;
  945. if (pdev->features & FEATURE_MOTOR_PANTILT)
  946. {
  947. ARG_DEF(struct pwc_mpt_angles, angles)
  948. ARG_IN(angles)
  949. /* The camera can only set relative angles, so
  950. do some calculations when getting an absolute angle .
  951. */
  952. if (ARGR(angles).absolute)
  953. {
  954. new_pan = ARGR(angles).pan;
  955. new_tilt = ARGR(angles).tilt;
  956. }
  957. else
  958. {
  959. new_pan = pdev->pan_angle + ARGR(angles).pan;
  960. new_tilt = pdev->tilt_angle + ARGR(angles).tilt;
  961. }
  962. ret = pwc_mpt_set_angle(pdev, new_pan, new_tilt);
  963. }
  964. else
  965. {
  966. ret = -ENXIO;
  967. }
  968. break;
  969. }
  970. case VIDIOCPWCMPTGANGLE:
  971. {
  972. if (pdev->features & FEATURE_MOTOR_PANTILT)
  973. {
  974. ARG_DEF(struct pwc_mpt_angles, angles)
  975. ARGR(angles).absolute = 1;
  976. ARGR(angles).pan = pdev->pan_angle;
  977. ARGR(angles).tilt = pdev->tilt_angle;
  978. ARG_OUT(angles)
  979. }
  980. else
  981. {
  982. ret = -ENXIO;
  983. }
  984. break;
  985. }
  986. case VIDIOCPWCMPTSTATUS:
  987. {
  988. if (pdev->features & FEATURE_MOTOR_PANTILT)
  989. {
  990. ARG_DEF(struct pwc_mpt_status, status)
  991. ret = pwc_mpt_get_status(pdev, ARGA(status));
  992. ARG_OUT(status)
  993. }
  994. else
  995. {
  996. ret = -ENXIO;
  997. }
  998. break;
  999. }
  1000. case VIDIOCPWCGVIDCMD:
  1001. {
  1002. ARG_DEF(struct pwc_video_command, vcmd);
  1003. ARGR(vcmd).type = pdev->type;
  1004. ARGR(vcmd).release = pdev->release;
  1005. ARGR(vcmd).command_len = pdev->cmd_len;
  1006. memcpy(&ARGR(vcmd).command_buf, pdev->cmd_buf, pdev->cmd_len);
  1007. ARGR(vcmd).bandlength = pdev->vbandlength;
  1008. ARGR(vcmd).frame_size = pdev->frame_size;
  1009. ARG_OUT(vcmd)
  1010. break;
  1011. }
  1012. /*
  1013. case VIDIOCPWCGVIDTABLE:
  1014. {
  1015. ARG_DEF(struct pwc_table_init_buffer, table);
  1016. ARGR(table).len = pdev->cmd_len;
  1017. memcpy(&ARGR(table).buffer, pdev->decompress_data, pdev->decompressor->table_size);
  1018. ARG_OUT(table)
  1019. break;
  1020. }
  1021. */
  1022. default:
  1023. ret = -ENOIOCTLCMD;
  1024. break;
  1025. }
  1026. if (ret > 0)
  1027. return 0;
  1028. return ret;
  1029. }
  1030. /* vim: set cinoptions= formatoptions=croql cindent shiftwidth=8 tabstop=8: */