pwc-ctrl.c 28 KB

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