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