pwc-ctrl.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541
  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 Luc Saillard (luc@saillard.org)
  6. NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
  7. driver and thus may have bugs that are not present in the original version.
  8. Please send bug reports and support requests to <luc@saillard.org>.
  9. NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
  10. driver and thus may have bugs that are not present in the original version.
  11. Please send bug reports and support requests to <luc@saillard.org>.
  12. The decompression routines have been implemented by reverse-engineering the
  13. Nemosoft binary pwcx module. Caveat emptor.
  14. This program is free software; you can redistribute it and/or modify
  15. it under the terms of the GNU General Public License as published by
  16. the Free Software Foundation; either version 2 of the License, or
  17. (at your option) any later version.
  18. This program is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. GNU General Public License for more details.
  22. You should have received a copy of the GNU General Public License
  23. along with this program; if not, write to the Free Software
  24. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. */
  26. /*
  27. Changes
  28. 2001/08/03 Alvarado Added methods for changing white balance and
  29. red/green gains
  30. */
  31. /* Control functions for the cam; brightness, contrast, video mode, etc. */
  32. #ifdef __KERNEL__
  33. #include <asm/uaccess.h>
  34. #endif
  35. #include <asm/errno.h>
  36. #include "pwc.h"
  37. #include "pwc-ioctl.h"
  38. #include "pwc-uncompress.h"
  39. #include "pwc-kiara.h"
  40. #include "pwc-timon.h"
  41. /* Request types: video */
  42. #define SET_LUM_CTL 0x01
  43. #define GET_LUM_CTL 0x02
  44. #define SET_CHROM_CTL 0x03
  45. #define GET_CHROM_CTL 0x04
  46. #define SET_STATUS_CTL 0x05
  47. #define GET_STATUS_CTL 0x06
  48. #define SET_EP_STREAM_CTL 0x07
  49. #define GET_EP_STREAM_CTL 0x08
  50. #define SET_MPT_CTL 0x0D
  51. #define GET_MPT_CTL 0x0E
  52. /* Selectors for the Luminance controls [GS]ET_LUM_CTL */
  53. #define AGC_MODE_FORMATTER 0x2000
  54. #define PRESET_AGC_FORMATTER 0x2100
  55. #define SHUTTER_MODE_FORMATTER 0x2200
  56. #define PRESET_SHUTTER_FORMATTER 0x2300
  57. #define PRESET_CONTOUR_FORMATTER 0x2400
  58. #define AUTO_CONTOUR_FORMATTER 0x2500
  59. #define BACK_LIGHT_COMPENSATION_FORMATTER 0x2600
  60. #define CONTRAST_FORMATTER 0x2700
  61. #define DYNAMIC_NOISE_CONTROL_FORMATTER 0x2800
  62. #define FLICKERLESS_MODE_FORMATTER 0x2900
  63. #define AE_CONTROL_SPEED 0x2A00
  64. #define BRIGHTNESS_FORMATTER 0x2B00
  65. #define GAMMA_FORMATTER 0x2C00
  66. /* Selectors for the Chrominance controls [GS]ET_CHROM_CTL */
  67. #define WB_MODE_FORMATTER 0x1000
  68. #define AWB_CONTROL_SPEED_FORMATTER 0x1100
  69. #define AWB_CONTROL_DELAY_FORMATTER 0x1200
  70. #define PRESET_MANUAL_RED_GAIN_FORMATTER 0x1300
  71. #define PRESET_MANUAL_BLUE_GAIN_FORMATTER 0x1400
  72. #define COLOUR_MODE_FORMATTER 0x1500
  73. #define SATURATION_MODE_FORMATTER1 0x1600
  74. #define SATURATION_MODE_FORMATTER2 0x1700
  75. /* Selectors for the Status controls [GS]ET_STATUS_CTL */
  76. #define SAVE_USER_DEFAULTS_FORMATTER 0x0200
  77. #define RESTORE_USER_DEFAULTS_FORMATTER 0x0300
  78. #define RESTORE_FACTORY_DEFAULTS_FORMATTER 0x0400
  79. #define READ_AGC_FORMATTER 0x0500
  80. #define READ_SHUTTER_FORMATTER 0x0600
  81. #define READ_RED_GAIN_FORMATTER 0x0700
  82. #define READ_BLUE_GAIN_FORMATTER 0x0800
  83. #define SENSOR_TYPE_FORMATTER1 0x0C00
  84. #define READ_RAW_Y_MEAN_FORMATTER 0x3100
  85. #define SET_POWER_SAVE_MODE_FORMATTER 0x3200
  86. #define MIRROR_IMAGE_FORMATTER 0x3300
  87. #define LED_FORMATTER 0x3400
  88. #define SENSOR_TYPE_FORMATTER2 0x3700
  89. /* Formatters for the Video Endpoint controls [GS]ET_EP_STREAM_CTL */
  90. #define VIDEO_OUTPUT_CONTROL_FORMATTER 0x0100
  91. /* Formatters for the motorized pan & tilt [GS]ET_MPT_CTL */
  92. #define PT_RELATIVE_CONTROL_FORMATTER 0x01
  93. #define PT_RESET_CONTROL_FORMATTER 0x02
  94. #define PT_STATUS_FORMATTER 0x03
  95. static const char *size2name[PSZ_MAX] =
  96. {
  97. "subQCIF",
  98. "QSIF",
  99. "QCIF",
  100. "SIF",
  101. "CIF",
  102. "VGA",
  103. };
  104. /********/
  105. /* Entries for the Nala (645/646) camera; the Nala doesn't have compression
  106. preferences, so you either get compressed or non-compressed streams.
  107. An alternate value of 0 means this mode is not available at all.
  108. */
  109. struct Nala_table_entry {
  110. char alternate; /* USB alternate setting */
  111. int compressed; /* Compressed yes/no */
  112. unsigned char mode[3]; /* precomputed mode table */
  113. };
  114. static struct Nala_table_entry Nala_table[PSZ_MAX][8] =
  115. {
  116. #include "pwc-nala.h"
  117. };
  118. /****************************************************************************/
  119. #define SendControlMsg(request, value, buflen) \
  120. usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0), \
  121. request, \
  122. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, \
  123. value, \
  124. pdev->vcinterface, \
  125. &buf, buflen, 500)
  126. #define RecvControlMsg(request, value, buflen) \
  127. usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0), \
  128. request, \
  129. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, \
  130. value, \
  131. pdev->vcinterface, \
  132. &buf, buflen, 500)
  133. #if PWC_DEBUG
  134. void pwc_hexdump(void *p, int len)
  135. {
  136. int i;
  137. unsigned char *s;
  138. char buf[100], *d;
  139. s = (unsigned char *)p;
  140. d = buf;
  141. *d = '\0';
  142. Debug("Doing hexdump @ %p, %d bytes.\n", p, len);
  143. for (i = 0; i < len; i++) {
  144. d += sprintf(d, "%02X ", *s++);
  145. if ((i & 0xF) == 0xF) {
  146. Debug("%s\n", buf);
  147. d = buf;
  148. *d = '\0';
  149. }
  150. }
  151. if ((i & 0xF) != 0)
  152. Debug("%s\n", buf);
  153. }
  154. #endif
  155. static inline int send_video_command(struct usb_device *udev, int index, void *buf, int buflen)
  156. {
  157. return usb_control_msg(udev,
  158. usb_sndctrlpipe(udev, 0),
  159. SET_EP_STREAM_CTL,
  160. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  161. VIDEO_OUTPUT_CONTROL_FORMATTER,
  162. index,
  163. buf, buflen, 1000);
  164. }
  165. static inline int set_video_mode_Nala(struct pwc_device *pdev, int size, int frames)
  166. {
  167. unsigned char buf[3];
  168. int ret, fps;
  169. struct Nala_table_entry *pEntry;
  170. int frames2frames[31] =
  171. { /* closest match of framerate */
  172. 0, 0, 0, 0, 4, /* 0-4 */
  173. 5, 5, 7, 7, 10, /* 5-9 */
  174. 10, 10, 12, 12, 15, /* 10-14 */
  175. 15, 15, 15, 20, 20, /* 15-19 */
  176. 20, 20, 20, 24, 24, /* 20-24 */
  177. 24, 24, 24, 24, 24, /* 25-29 */
  178. 24 /* 30 */
  179. };
  180. int frames2table[31] =
  181. { 0, 0, 0, 0, 0, /* 0-4 */
  182. 1, 1, 1, 2, 2, /* 5-9 */
  183. 3, 3, 4, 4, 4, /* 10-14 */
  184. 5, 5, 5, 5, 5, /* 15-19 */
  185. 6, 6, 6, 6, 7, /* 20-24 */
  186. 7, 7, 7, 7, 7, /* 25-29 */
  187. 7 /* 30 */
  188. };
  189. if (size < 0 || size > PSZ_CIF || frames < 4 || frames > 25)
  190. return -EINVAL;
  191. frames = frames2frames[frames];
  192. fps = frames2table[frames];
  193. pEntry = &Nala_table[size][fps];
  194. if (pEntry->alternate == 0)
  195. return -EINVAL;
  196. if (pEntry->compressed)
  197. return -ENOENT; /* Not supported. */
  198. memcpy(buf, pEntry->mode, 3);
  199. ret = send_video_command(pdev->udev, pdev->vendpoint, buf, 3);
  200. if (ret < 0) {
  201. Debug("Failed to send video command... %d\n", ret);
  202. return ret;
  203. }
  204. if (pEntry->compressed && pdev->vpalette != VIDEO_PALETTE_RAW)
  205. {
  206. switch(pdev->type) {
  207. case 645:
  208. case 646:
  209. /* pwc_dec1_init(pdev->type, pdev->release, buf, pdev->decompress_data); */
  210. break;
  211. case 675:
  212. case 680:
  213. case 690:
  214. case 720:
  215. case 730:
  216. case 740:
  217. case 750:
  218. /* pwc_dec23_init(pdev->type, pdev->release, buf, pdev->decompress_data); */
  219. break;
  220. }
  221. }
  222. pdev->cmd_len = 3;
  223. memcpy(pdev->cmd_buf, buf, 3);
  224. /* Set various parameters */
  225. pdev->vframes = frames;
  226. pdev->vsize = size;
  227. pdev->valternate = pEntry->alternate;
  228. pdev->image = pwc_image_sizes[size];
  229. pdev->frame_size = (pdev->image.x * pdev->image.y * 3) / 2;
  230. if (pEntry->compressed) {
  231. if (pdev->release < 5) { /* 4 fold compression */
  232. pdev->vbandlength = 528;
  233. pdev->frame_size /= 4;
  234. }
  235. else {
  236. pdev->vbandlength = 704;
  237. pdev->frame_size /= 3;
  238. }
  239. }
  240. else
  241. pdev->vbandlength = 0;
  242. return 0;
  243. }
  244. static inline int set_video_mode_Timon(struct pwc_device *pdev, int size, int frames, int compression, int snapshot)
  245. {
  246. unsigned char buf[13];
  247. const struct Timon_table_entry *pChoose;
  248. int ret, fps;
  249. if (size >= PSZ_MAX || frames < 5 || frames > 30 || compression < 0 || compression > 3)
  250. return -EINVAL;
  251. if (size == PSZ_VGA && frames > 15)
  252. return -EINVAL;
  253. fps = (frames / 5) - 1;
  254. /* Find a supported framerate with progressively higher compression ratios
  255. if the preferred ratio is not available.
  256. */
  257. pChoose = NULL;
  258. while (compression <= 3) {
  259. pChoose = &Timon_table[size][fps][compression];
  260. if (pChoose->alternate != 0)
  261. break;
  262. compression++;
  263. }
  264. if (pChoose == NULL || pChoose->alternate == 0)
  265. return -ENOENT; /* Not supported. */
  266. memcpy(buf, pChoose->mode, 13);
  267. if (snapshot)
  268. buf[0] |= 0x80;
  269. ret = send_video_command(pdev->udev, pdev->vendpoint, buf, 13);
  270. if (ret < 0)
  271. return ret;
  272. /* if (pChoose->bandlength > 0 && pdev->vpalette != VIDEO_PALETTE_RAW)
  273. pwc_dec23_init(pdev->type, pdev->release, buf, pdev->decompress_data); */
  274. pdev->cmd_len = 13;
  275. memcpy(pdev->cmd_buf, buf, 13);
  276. /* Set various parameters */
  277. pdev->vframes = frames;
  278. pdev->vsize = size;
  279. pdev->vsnapshot = snapshot;
  280. pdev->valternate = pChoose->alternate;
  281. pdev->image = pwc_image_sizes[size];
  282. pdev->vbandlength = pChoose->bandlength;
  283. if (pChoose->bandlength > 0)
  284. pdev->frame_size = (pChoose->bandlength * pdev->image.y) / 4;
  285. else
  286. pdev->frame_size = (pdev->image.x * pdev->image.y * 12) / 8;
  287. return 0;
  288. }
  289. static inline int set_video_mode_Kiara(struct pwc_device *pdev, int size, int frames, int compression, int snapshot)
  290. {
  291. const struct Kiara_table_entry *pChoose = NULL;
  292. int fps, ret;
  293. unsigned char buf[12];
  294. struct Kiara_table_entry RawEntry = {6, 773, 1272, {0xAD, 0xF4, 0x10, 0x27, 0xB6, 0x24, 0x96, 0x02, 0x30, 0x05, 0x03, 0x80}};
  295. if (size >= PSZ_MAX || frames < 5 || frames > 30 || compression < 0 || compression > 3)
  296. return -EINVAL;
  297. if (size == PSZ_VGA && frames > 15)
  298. return -EINVAL;
  299. fps = (frames / 5) - 1;
  300. /* special case: VGA @ 5 fps and snapshot is raw bayer mode */
  301. if (size == PSZ_VGA && frames == 5 && snapshot)
  302. {
  303. /* Only available in case the raw palette is selected or
  304. we have the decompressor available. This mode is
  305. only available in compressed form
  306. */
  307. if (pdev->vpalette == VIDEO_PALETTE_RAW)
  308. {
  309. Info("Choosing VGA/5 BAYER mode (%d).\n", pdev->vpalette);
  310. pChoose = &RawEntry;
  311. }
  312. else
  313. {
  314. Info("VGA/5 BAYER mode _must_ have a decompressor available, or use RAW palette.\n");
  315. }
  316. }
  317. else
  318. {
  319. /* Find a supported framerate with progressively higher compression ratios
  320. if the preferred ratio is not available.
  321. Skip this step when using RAW modes.
  322. */
  323. while (compression <= 3) {
  324. pChoose = &Kiara_table[size][fps][compression];
  325. if (pChoose->alternate != 0)
  326. break;
  327. compression++;
  328. }
  329. }
  330. if (pChoose == NULL || pChoose->alternate == 0)
  331. return -ENOENT; /* Not supported. */
  332. Debug("Using alternate setting %d.\n", pChoose->alternate);
  333. /* usb_control_msg won't take staticly allocated arrays as argument?? */
  334. memcpy(buf, pChoose->mode, 12);
  335. if (snapshot)
  336. buf[0] |= 0x80;
  337. /* Firmware bug: video endpoint is 5, but commands are sent to endpoint 4 */
  338. ret = send_video_command(pdev->udev, 4 /* pdev->vendpoint */, buf, 12);
  339. if (ret < 0)
  340. return ret;
  341. /* if (pChoose->bandlength > 0 && pdev->vpalette != VIDEO_PALETTE_RAW)
  342. pwc_dec23_init(pdev->type, pdev->release, buf, pdev->decompress_data); */
  343. pdev->cmd_len = 12;
  344. memcpy(pdev->cmd_buf, buf, 12);
  345. /* All set and go */
  346. pdev->vframes = frames;
  347. pdev->vsize = size;
  348. pdev->vsnapshot = snapshot;
  349. pdev->valternate = pChoose->alternate;
  350. pdev->image = pwc_image_sizes[size];
  351. pdev->vbandlength = pChoose->bandlength;
  352. if (pdev->vbandlength > 0)
  353. pdev->frame_size = (pdev->vbandlength * pdev->image.y) / 4;
  354. else
  355. pdev->frame_size = (pdev->image.x * pdev->image.y * 12) / 8;
  356. return 0;
  357. }
  358. static void pwc_set_image_buffer_size(struct pwc_device *pdev)
  359. {
  360. int i, factor = 0, filler = 0;
  361. /* for PALETTE_YUV420P */
  362. switch(pdev->vpalette)
  363. {
  364. case VIDEO_PALETTE_YUV420P:
  365. factor = 6;
  366. filler = 128;
  367. break;
  368. case VIDEO_PALETTE_RAW:
  369. factor = 6; /* can be uncompressed YUV420P */
  370. filler = 0;
  371. break;
  372. }
  373. /* Set sizes in bytes */
  374. pdev->image.size = pdev->image.x * pdev->image.y * factor / 4;
  375. pdev->view.size = pdev->view.x * pdev->view.y * factor / 4;
  376. /* Align offset, or you'll get some very weird results in
  377. YUV420 mode... x must be multiple of 4 (to get the Y's in
  378. place), and y even (or you'll mixup U & V). This is less of a
  379. problem for YUV420P.
  380. */
  381. pdev->offset.x = ((pdev->view.x - pdev->image.x) / 2) & 0xFFFC;
  382. pdev->offset.y = ((pdev->view.y - pdev->image.y) / 2) & 0xFFFE;
  383. /* Fill buffers with gray or black */
  384. for (i = 0; i < MAX_IMAGES; i++) {
  385. if (pdev->image_ptr[i] != NULL)
  386. memset(pdev->image_ptr[i], filler, pdev->view.size);
  387. }
  388. }
  389. /**
  390. @pdev: device structure
  391. @width: viewport width
  392. @height: viewport height
  393. @frame: framerate, in fps
  394. @compression: preferred compression ratio
  395. @snapshot: snapshot mode or streaming
  396. */
  397. int pwc_set_video_mode(struct pwc_device *pdev, int width, int height, int frames, int compression, int snapshot)
  398. {
  399. int ret, size;
  400. Trace(TRACE_FLOW, "set_video_mode(%dx%d @ %d, palette %d).\n", width, height, frames, pdev->vpalette);
  401. size = pwc_decode_size(pdev, width, height);
  402. if (size < 0) {
  403. Debug("Could not find suitable size.\n");
  404. return -ERANGE;
  405. }
  406. Debug("decode_size = %d.\n", size);
  407. ret = -EINVAL;
  408. switch(pdev->type) {
  409. case 645:
  410. case 646:
  411. ret = set_video_mode_Nala(pdev, size, frames);
  412. break;
  413. case 675:
  414. case 680:
  415. case 690:
  416. ret = set_video_mode_Timon(pdev, size, frames, compression, snapshot);
  417. break;
  418. case 720:
  419. case 730:
  420. case 740:
  421. case 750:
  422. ret = set_video_mode_Kiara(pdev, size, frames, compression, snapshot);
  423. break;
  424. }
  425. if (ret < 0) {
  426. if (ret == -ENOENT)
  427. Info("Video mode %s@%d fps is only supported with the decompressor module (pwcx).\n", size2name[size], frames);
  428. else {
  429. Err("Failed to set video mode %s@%d fps; return code = %d\n", size2name[size], frames, ret);
  430. }
  431. return ret;
  432. }
  433. pdev->view.x = width;
  434. pdev->view.y = height;
  435. pdev->frame_total_size = pdev->frame_size + pdev->frame_header_size + pdev->frame_trailer_size;
  436. pwc_set_image_buffer_size(pdev);
  437. Trace(TRACE_SIZE, "Set viewport to %dx%d, image size is %dx%d.\n", width, height, pwc_image_sizes[size].x, pwc_image_sizes[size].y);
  438. return 0;
  439. }
  440. /* BRIGHTNESS */
  441. int pwc_get_brightness(struct pwc_device *pdev)
  442. {
  443. char buf;
  444. int ret;
  445. ret = RecvControlMsg(GET_LUM_CTL, BRIGHTNESS_FORMATTER, 1);
  446. if (ret < 0)
  447. return ret;
  448. return buf << 9;
  449. }
  450. int pwc_set_brightness(struct pwc_device *pdev, int value)
  451. {
  452. char buf;
  453. if (value < 0)
  454. value = 0;
  455. if (value > 0xffff)
  456. value = 0xffff;
  457. buf = (value >> 9) & 0x7f;
  458. return SendControlMsg(SET_LUM_CTL, BRIGHTNESS_FORMATTER, 1);
  459. }
  460. /* CONTRAST */
  461. int pwc_get_contrast(struct pwc_device *pdev)
  462. {
  463. char buf;
  464. int ret;
  465. ret = RecvControlMsg(GET_LUM_CTL, CONTRAST_FORMATTER, 1);
  466. if (ret < 0)
  467. return ret;
  468. return buf << 10;
  469. }
  470. int pwc_set_contrast(struct pwc_device *pdev, int value)
  471. {
  472. char buf;
  473. if (value < 0)
  474. value = 0;
  475. if (value > 0xffff)
  476. value = 0xffff;
  477. buf = (value >> 10) & 0x3f;
  478. return SendControlMsg(SET_LUM_CTL, CONTRAST_FORMATTER, 1);
  479. }
  480. /* GAMMA */
  481. int pwc_get_gamma(struct pwc_device *pdev)
  482. {
  483. char buf;
  484. int ret;
  485. ret = RecvControlMsg(GET_LUM_CTL, GAMMA_FORMATTER, 1);
  486. if (ret < 0)
  487. return ret;
  488. return buf << 11;
  489. }
  490. int pwc_set_gamma(struct pwc_device *pdev, int value)
  491. {
  492. char buf;
  493. if (value < 0)
  494. value = 0;
  495. if (value > 0xffff)
  496. value = 0xffff;
  497. buf = (value >> 11) & 0x1f;
  498. return SendControlMsg(SET_LUM_CTL, GAMMA_FORMATTER, 1);
  499. }
  500. /* SATURATION */
  501. int pwc_get_saturation(struct pwc_device *pdev)
  502. {
  503. char buf;
  504. int ret;
  505. if (pdev->type < 675)
  506. return -1;
  507. ret = RecvControlMsg(GET_CHROM_CTL, pdev->type < 730 ? SATURATION_MODE_FORMATTER2 : SATURATION_MODE_FORMATTER1, 1);
  508. if (ret < 0)
  509. return ret;
  510. return 32768 + buf * 327;
  511. }
  512. int pwc_set_saturation(struct pwc_device *pdev, int value)
  513. {
  514. char buf;
  515. if (pdev->type < 675)
  516. return -EINVAL;
  517. if (value < 0)
  518. value = 0;
  519. if (value > 0xffff)
  520. value = 0xffff;
  521. /* saturation ranges from -100 to +100 */
  522. buf = (value - 32768) / 327;
  523. return SendControlMsg(SET_CHROM_CTL, pdev->type < 730 ? SATURATION_MODE_FORMATTER2 : SATURATION_MODE_FORMATTER1, 1);
  524. }
  525. /* AGC */
  526. static inline int pwc_set_agc(struct pwc_device *pdev, int mode, int value)
  527. {
  528. char buf;
  529. int ret;
  530. if (mode)
  531. buf = 0x0; /* auto */
  532. else
  533. buf = 0xff; /* fixed */
  534. ret = SendControlMsg(SET_LUM_CTL, AGC_MODE_FORMATTER, 1);
  535. if (!mode && ret >= 0) {
  536. if (value < 0)
  537. value = 0;
  538. if (value > 0xffff)
  539. value = 0xffff;
  540. buf = (value >> 10) & 0x3F;
  541. ret = SendControlMsg(SET_LUM_CTL, PRESET_AGC_FORMATTER, 1);
  542. }
  543. if (ret < 0)
  544. return ret;
  545. return 0;
  546. }
  547. static inline int pwc_get_agc(struct pwc_device *pdev, int *value)
  548. {
  549. unsigned char buf;
  550. int ret;
  551. ret = RecvControlMsg(GET_LUM_CTL, AGC_MODE_FORMATTER, 1);
  552. if (ret < 0)
  553. return ret;
  554. if (buf != 0) { /* fixed */
  555. ret = RecvControlMsg(GET_LUM_CTL, PRESET_AGC_FORMATTER, 1);
  556. if (ret < 0)
  557. return ret;
  558. if (buf > 0x3F)
  559. buf = 0x3F;
  560. *value = (buf << 10);
  561. }
  562. else { /* auto */
  563. ret = RecvControlMsg(GET_STATUS_CTL, READ_AGC_FORMATTER, 1);
  564. if (ret < 0)
  565. return ret;
  566. /* Gah... this value ranges from 0x00 ... 0x9F */
  567. if (buf > 0x9F)
  568. buf = 0x9F;
  569. *value = -(48 + buf * 409);
  570. }
  571. return 0;
  572. }
  573. static inline int pwc_set_shutter_speed(struct pwc_device *pdev, int mode, int value)
  574. {
  575. char buf[2];
  576. int speed, ret;
  577. if (mode)
  578. buf[0] = 0x0; /* auto */
  579. else
  580. buf[0] = 0xff; /* fixed */
  581. ret = SendControlMsg(SET_LUM_CTL, SHUTTER_MODE_FORMATTER, 1);
  582. if (!mode && ret >= 0) {
  583. if (value < 0)
  584. value = 0;
  585. if (value > 0xffff)
  586. value = 0xffff;
  587. switch(pdev->type) {
  588. case 675:
  589. case 680:
  590. case 690:
  591. /* speed ranges from 0x0 to 0x290 (656) */
  592. speed = (value / 100);
  593. buf[1] = speed >> 8;
  594. buf[0] = speed & 0xff;
  595. break;
  596. case 720:
  597. case 730:
  598. case 740:
  599. case 750:
  600. /* speed seems to range from 0x0 to 0xff */
  601. buf[1] = 0;
  602. buf[0] = value >> 8;
  603. break;
  604. }
  605. ret = SendControlMsg(SET_LUM_CTL, PRESET_SHUTTER_FORMATTER, 2);
  606. }
  607. return ret;
  608. }
  609. /* POWER */
  610. int pwc_camera_power(struct pwc_device *pdev, int power)
  611. {
  612. char buf;
  613. if (pdev->type < 675 || (pdev->type < 730 && pdev->release < 6))
  614. return 0; /* Not supported by Nala or Timon < release 6 */
  615. if (power)
  616. buf = 0x00; /* active */
  617. else
  618. buf = 0xFF; /* power save */
  619. return SendControlMsg(SET_STATUS_CTL, SET_POWER_SAVE_MODE_FORMATTER, 1);
  620. }
  621. /* private calls */
  622. static inline int pwc_restore_user(struct pwc_device *pdev)
  623. {
  624. char buf; /* dummy */
  625. return SendControlMsg(SET_STATUS_CTL, RESTORE_USER_DEFAULTS_FORMATTER, 0);
  626. }
  627. static inline int pwc_save_user(struct pwc_device *pdev)
  628. {
  629. char buf; /* dummy */
  630. return SendControlMsg(SET_STATUS_CTL, SAVE_USER_DEFAULTS_FORMATTER, 0);
  631. }
  632. static inline int pwc_restore_factory(struct pwc_device *pdev)
  633. {
  634. char buf; /* dummy */
  635. return SendControlMsg(SET_STATUS_CTL, RESTORE_FACTORY_DEFAULTS_FORMATTER, 0);
  636. }
  637. /* ************************************************* */
  638. /* Patch by Alvarado: (not in the original version */
  639. /*
  640. * the camera recognizes modes from 0 to 4:
  641. *
  642. * 00: indoor (incandescant lighting)
  643. * 01: outdoor (sunlight)
  644. * 02: fluorescent lighting
  645. * 03: manual
  646. * 04: auto
  647. */
  648. static inline int pwc_set_awb(struct pwc_device *pdev, int mode)
  649. {
  650. char buf;
  651. int ret;
  652. if (mode < 0)
  653. mode = 0;
  654. if (mode > 4)
  655. mode = 4;
  656. buf = mode & 0x07; /* just the lowest three bits */
  657. ret = SendControlMsg(SET_CHROM_CTL, WB_MODE_FORMATTER, 1);
  658. if (ret < 0)
  659. return ret;
  660. return 0;
  661. }
  662. static inline int pwc_get_awb(struct pwc_device *pdev)
  663. {
  664. unsigned char buf;
  665. int ret;
  666. ret = RecvControlMsg(GET_CHROM_CTL, WB_MODE_FORMATTER, 1);
  667. if (ret < 0)
  668. return ret;
  669. return buf;
  670. }
  671. static inline int pwc_set_red_gain(struct pwc_device *pdev, int value)
  672. {
  673. unsigned char buf;
  674. if (value < 0)
  675. value = 0;
  676. if (value > 0xffff)
  677. value = 0xffff;
  678. /* only the msb is considered */
  679. buf = value >> 8;
  680. return SendControlMsg(SET_CHROM_CTL, PRESET_MANUAL_RED_GAIN_FORMATTER, 1);
  681. }
  682. static inline int pwc_get_red_gain(struct pwc_device *pdev, int *value)
  683. {
  684. unsigned char buf;
  685. int ret;
  686. ret = RecvControlMsg(GET_CHROM_CTL, PRESET_MANUAL_RED_GAIN_FORMATTER, 1);
  687. if (ret < 0)
  688. return ret;
  689. *value = buf << 8;
  690. return 0;
  691. }
  692. static inline int pwc_set_blue_gain(struct pwc_device *pdev, int value)
  693. {
  694. unsigned char buf;
  695. if (value < 0)
  696. value = 0;
  697. if (value > 0xffff)
  698. value = 0xffff;
  699. /* only the msb is considered */
  700. buf = value >> 8;
  701. return SendControlMsg(SET_CHROM_CTL, PRESET_MANUAL_BLUE_GAIN_FORMATTER, 1);
  702. }
  703. static inline int pwc_get_blue_gain(struct pwc_device *pdev, int *value)
  704. {
  705. unsigned char buf;
  706. int ret;
  707. ret = RecvControlMsg(GET_CHROM_CTL, PRESET_MANUAL_BLUE_GAIN_FORMATTER, 1);
  708. if (ret < 0)
  709. return ret;
  710. *value = buf << 8;
  711. return 0;
  712. }
  713. /* The following two functions are different, since they only read the
  714. internal red/blue gains, which may be different from the manual
  715. gains set or read above.
  716. */
  717. static inline int pwc_read_red_gain(struct pwc_device *pdev, int *value)
  718. {
  719. unsigned char buf;
  720. int ret;
  721. ret = RecvControlMsg(GET_STATUS_CTL, READ_RED_GAIN_FORMATTER, 1);
  722. if (ret < 0)
  723. return ret;
  724. *value = buf << 8;
  725. return 0;
  726. }
  727. static inline int pwc_read_blue_gain(struct pwc_device *pdev, int *value)
  728. {
  729. unsigned char buf;
  730. int ret;
  731. ret = RecvControlMsg(GET_STATUS_CTL, READ_BLUE_GAIN_FORMATTER, 1);
  732. if (ret < 0)
  733. return ret;
  734. *value = buf << 8;
  735. return 0;
  736. }
  737. static inline int pwc_set_wb_speed(struct pwc_device *pdev, int speed)
  738. {
  739. unsigned char buf;
  740. /* useful range is 0x01..0x20 */
  741. buf = speed / 0x7f0;
  742. return SendControlMsg(SET_CHROM_CTL, AWB_CONTROL_SPEED_FORMATTER, 1);
  743. }
  744. static inline int pwc_get_wb_speed(struct pwc_device *pdev, int *value)
  745. {
  746. unsigned char buf;
  747. int ret;
  748. ret = RecvControlMsg(GET_CHROM_CTL, AWB_CONTROL_SPEED_FORMATTER, 1);
  749. if (ret < 0)
  750. return ret;
  751. *value = buf * 0x7f0;
  752. return 0;
  753. }
  754. static inline int pwc_set_wb_delay(struct pwc_device *pdev, int delay)
  755. {
  756. unsigned char buf;
  757. /* useful range is 0x01..0x3F */
  758. buf = (delay >> 10);
  759. return SendControlMsg(SET_CHROM_CTL, AWB_CONTROL_DELAY_FORMATTER, 1);
  760. }
  761. static inline int pwc_get_wb_delay(struct pwc_device *pdev, int *value)
  762. {
  763. unsigned char buf;
  764. int ret;
  765. ret = RecvControlMsg(GET_CHROM_CTL, AWB_CONTROL_DELAY_FORMATTER, 1);
  766. if (ret < 0)
  767. return ret;
  768. *value = buf << 10;
  769. return 0;
  770. }
  771. int pwc_set_leds(struct pwc_device *pdev, int on_value, int off_value)
  772. {
  773. unsigned char buf[2];
  774. if (pdev->type < 730)
  775. return 0;
  776. on_value /= 100;
  777. off_value /= 100;
  778. if (on_value < 0)
  779. on_value = 0;
  780. if (on_value > 0xff)
  781. on_value = 0xff;
  782. if (off_value < 0)
  783. off_value = 0;
  784. if (off_value > 0xff)
  785. off_value = 0xff;
  786. buf[0] = on_value;
  787. buf[1] = off_value;
  788. return SendControlMsg(SET_STATUS_CTL, LED_FORMATTER, 2);
  789. }
  790. static int pwc_get_leds(struct pwc_device *pdev, int *on_value, int *off_value)
  791. {
  792. unsigned char buf[2];
  793. int ret;
  794. if (pdev->type < 730) {
  795. *on_value = -1;
  796. *off_value = -1;
  797. return 0;
  798. }
  799. ret = RecvControlMsg(GET_STATUS_CTL, LED_FORMATTER, 2);
  800. if (ret < 0)
  801. return ret;
  802. *on_value = buf[0] * 100;
  803. *off_value = buf[1] * 100;
  804. return 0;
  805. }
  806. static inline int pwc_set_contour(struct pwc_device *pdev, int contour)
  807. {
  808. unsigned char buf;
  809. int ret;
  810. if (contour < 0)
  811. buf = 0xff; /* auto contour on */
  812. else
  813. buf = 0x0; /* auto contour off */
  814. ret = SendControlMsg(SET_LUM_CTL, AUTO_CONTOUR_FORMATTER, 1);
  815. if (ret < 0)
  816. return ret;
  817. if (contour < 0)
  818. return 0;
  819. if (contour > 0xffff)
  820. contour = 0xffff;
  821. buf = (contour >> 10); /* contour preset is [0..3f] */
  822. ret = SendControlMsg(SET_LUM_CTL, PRESET_CONTOUR_FORMATTER, 1);
  823. if (ret < 0)
  824. return ret;
  825. return 0;
  826. }
  827. static inline int pwc_get_contour(struct pwc_device *pdev, int *contour)
  828. {
  829. unsigned char buf;
  830. int ret;
  831. ret = RecvControlMsg(GET_LUM_CTL, AUTO_CONTOUR_FORMATTER, 1);
  832. if (ret < 0)
  833. return ret;
  834. if (buf == 0) {
  835. /* auto mode off, query current preset value */
  836. ret = RecvControlMsg(GET_LUM_CTL, PRESET_CONTOUR_FORMATTER, 1);
  837. if (ret < 0)
  838. return ret;
  839. *contour = buf << 10;
  840. }
  841. else
  842. *contour = -1;
  843. return 0;
  844. }
  845. static inline int pwc_set_backlight(struct pwc_device *pdev, int backlight)
  846. {
  847. unsigned char buf;
  848. if (backlight)
  849. buf = 0xff;
  850. else
  851. buf = 0x0;
  852. return SendControlMsg(SET_LUM_CTL, BACK_LIGHT_COMPENSATION_FORMATTER, 1);
  853. }
  854. static inline int pwc_get_backlight(struct pwc_device *pdev, int *backlight)
  855. {
  856. int ret;
  857. unsigned char buf;
  858. ret = RecvControlMsg(GET_LUM_CTL, BACK_LIGHT_COMPENSATION_FORMATTER, 1);
  859. if (ret < 0)
  860. return ret;
  861. *backlight = buf;
  862. return 0;
  863. }
  864. static inline int pwc_set_flicker(struct pwc_device *pdev, int flicker)
  865. {
  866. unsigned char buf;
  867. if (flicker)
  868. buf = 0xff;
  869. else
  870. buf = 0x0;
  871. return SendControlMsg(SET_LUM_CTL, FLICKERLESS_MODE_FORMATTER, 1);
  872. }
  873. static inline int pwc_get_flicker(struct pwc_device *pdev, int *flicker)
  874. {
  875. int ret;
  876. unsigned char buf;
  877. ret = RecvControlMsg(GET_LUM_CTL, FLICKERLESS_MODE_FORMATTER, 1);
  878. if (ret < 0)
  879. return ret;
  880. *flicker = buf;
  881. return 0;
  882. }
  883. static inline int pwc_set_dynamic_noise(struct pwc_device *pdev, int noise)
  884. {
  885. unsigned char buf;
  886. if (noise < 0)
  887. noise = 0;
  888. if (noise > 3)
  889. noise = 3;
  890. buf = noise;
  891. return SendControlMsg(SET_LUM_CTL, DYNAMIC_NOISE_CONTROL_FORMATTER, 1);
  892. }
  893. static inline int pwc_get_dynamic_noise(struct pwc_device *pdev, int *noise)
  894. {
  895. int ret;
  896. unsigned char buf;
  897. ret = RecvControlMsg(GET_LUM_CTL, DYNAMIC_NOISE_CONTROL_FORMATTER, 1);
  898. if (ret < 0)
  899. return ret;
  900. *noise = buf;
  901. return 0;
  902. }
  903. static int pwc_mpt_reset(struct pwc_device *pdev, int flags)
  904. {
  905. unsigned char buf;
  906. buf = flags & 0x03; // only lower two bits are currently used
  907. return SendControlMsg(SET_MPT_CTL, PT_RESET_CONTROL_FORMATTER, 1);
  908. }
  909. static inline int pwc_mpt_set_angle(struct pwc_device *pdev, int pan, int tilt)
  910. {
  911. unsigned char buf[4];
  912. /* set new relative angle; angles are expressed in degrees * 100,
  913. but cam as .5 degree resolution, hence divide by 200. Also
  914. the angle must be multiplied by 64 before it's send to
  915. the cam (??)
  916. */
  917. pan = 64 * pan / 100;
  918. tilt = -64 * tilt / 100; /* positive tilt is down, which is not what the user would expect */
  919. buf[0] = pan & 0xFF;
  920. buf[1] = (pan >> 8) & 0xFF;
  921. buf[2] = tilt & 0xFF;
  922. buf[3] = (tilt >> 8) & 0xFF;
  923. return SendControlMsg(SET_MPT_CTL, PT_RELATIVE_CONTROL_FORMATTER, 4);
  924. }
  925. static inline int pwc_mpt_get_status(struct pwc_device *pdev, struct pwc_mpt_status *status)
  926. {
  927. int ret;
  928. unsigned char buf[5];
  929. ret = RecvControlMsg(GET_MPT_CTL, PT_STATUS_FORMATTER, 5);
  930. if (ret < 0)
  931. return ret;
  932. status->status = buf[0] & 0x7; // 3 bits are used for reporting
  933. status->time_pan = (buf[1] << 8) + buf[2];
  934. status->time_tilt = (buf[3] << 8) + buf[4];
  935. return 0;
  936. }
  937. int pwc_get_cmos_sensor(struct pwc_device *pdev, int *sensor)
  938. {
  939. unsigned char buf;
  940. int ret = -1, request;
  941. if (pdev->type < 675)
  942. request = SENSOR_TYPE_FORMATTER1;
  943. else if (pdev->type < 730)
  944. return -1; /* The Vesta series doesn't have this call */
  945. else
  946. request = SENSOR_TYPE_FORMATTER2;
  947. ret = RecvControlMsg(GET_STATUS_CTL, request, 1);
  948. if (ret < 0)
  949. return ret;
  950. if (pdev->type < 675)
  951. *sensor = buf | 0x100;
  952. else
  953. *sensor = buf;
  954. return 0;
  955. }
  956. /* End of Add-Ons */
  957. /* ************************************************* */
  958. int pwc_ioctl(struct pwc_device *pdev, unsigned int cmd, void *arg)
  959. {
  960. int ret = 0;
  961. switch(cmd) {
  962. case VIDIOCPWCRUSER:
  963. {
  964. if (pwc_restore_user(pdev))
  965. ret = -EINVAL;
  966. break;
  967. }
  968. case VIDIOCPWCSUSER:
  969. {
  970. if (pwc_save_user(pdev))
  971. ret = -EINVAL;
  972. break;
  973. }
  974. case VIDIOCPWCFACTORY:
  975. {
  976. if (pwc_restore_factory(pdev))
  977. ret = -EINVAL;
  978. break;
  979. }
  980. case VIDIOCPWCSCQUAL:
  981. {
  982. int *qual = arg;
  983. if (*qual < 0 || *qual > 3)
  984. ret = -EINVAL;
  985. else
  986. ret = pwc_try_video_mode(pdev, pdev->view.x, pdev->view.y, pdev->vframes, *qual, pdev->vsnapshot);
  987. if (ret >= 0)
  988. pdev->vcompression = *qual;
  989. break;
  990. }
  991. case VIDIOCPWCGCQUAL:
  992. {
  993. int *qual = arg;
  994. *qual = pdev->vcompression;
  995. break;
  996. }
  997. case VIDIOCPWCPROBE:
  998. {
  999. struct pwc_probe *probe = arg;
  1000. strcpy(probe->name, pdev->vdev->name);
  1001. probe->type = pdev->type;
  1002. break;
  1003. }
  1004. case VIDIOCPWCGSERIAL:
  1005. {
  1006. struct pwc_serial *serial = arg;
  1007. strcpy(serial->serial, pdev->serial);
  1008. break;
  1009. }
  1010. case VIDIOCPWCSAGC:
  1011. {
  1012. int *agc = arg;
  1013. if (pwc_set_agc(pdev, *agc < 0 ? 1 : 0, *agc))
  1014. ret = -EINVAL;
  1015. break;
  1016. }
  1017. case VIDIOCPWCGAGC:
  1018. {
  1019. int *agc = arg;
  1020. if (pwc_get_agc(pdev, agc))
  1021. ret = -EINVAL;
  1022. break;
  1023. }
  1024. case VIDIOCPWCSSHUTTER:
  1025. {
  1026. int *shutter_speed = arg;
  1027. ret = pwc_set_shutter_speed(pdev, *shutter_speed < 0 ? 1 : 0, *shutter_speed);
  1028. break;
  1029. }
  1030. case VIDIOCPWCSAWB:
  1031. {
  1032. struct pwc_whitebalance *wb = arg;
  1033. ret = pwc_set_awb(pdev, wb->mode);
  1034. if (ret >= 0 && wb->mode == PWC_WB_MANUAL) {
  1035. pwc_set_red_gain(pdev, wb->manual_red);
  1036. pwc_set_blue_gain(pdev, wb->manual_blue);
  1037. }
  1038. break;
  1039. }
  1040. case VIDIOCPWCGAWB:
  1041. {
  1042. struct pwc_whitebalance *wb = arg;
  1043. memset(wb, 0, sizeof(struct pwc_whitebalance));
  1044. wb->mode = pwc_get_awb(pdev);
  1045. if (wb->mode < 0)
  1046. ret = -EINVAL;
  1047. else {
  1048. if (wb->mode == PWC_WB_MANUAL) {
  1049. ret = pwc_get_red_gain(pdev, &wb->manual_red);
  1050. if (ret < 0)
  1051. break;
  1052. ret = pwc_get_blue_gain(pdev, &wb->manual_blue);
  1053. if (ret < 0)
  1054. break;
  1055. }
  1056. if (wb->mode == PWC_WB_AUTO) {
  1057. ret = pwc_read_red_gain(pdev, &wb->read_red);
  1058. if (ret < 0)
  1059. break;
  1060. ret = pwc_read_blue_gain(pdev, &wb->read_blue);
  1061. if (ret < 0)
  1062. break;
  1063. }
  1064. }
  1065. break;
  1066. }
  1067. case VIDIOCPWCSAWBSPEED:
  1068. {
  1069. struct pwc_wb_speed *wbs = arg;
  1070. if (wbs->control_speed > 0) {
  1071. ret = pwc_set_wb_speed(pdev, wbs->control_speed);
  1072. }
  1073. if (wbs->control_delay > 0) {
  1074. ret = pwc_set_wb_delay(pdev, wbs->control_delay);
  1075. }
  1076. break;
  1077. }
  1078. case VIDIOCPWCGAWBSPEED:
  1079. {
  1080. struct pwc_wb_speed *wbs = arg;
  1081. ret = pwc_get_wb_speed(pdev, &wbs->control_speed);
  1082. if (ret < 0)
  1083. break;
  1084. ret = pwc_get_wb_delay(pdev, &wbs->control_delay);
  1085. if (ret < 0)
  1086. break;
  1087. break;
  1088. }
  1089. case VIDIOCPWCSLED:
  1090. {
  1091. struct pwc_leds *leds = arg;
  1092. ret = pwc_set_leds(pdev, leds->led_on, leds->led_off);
  1093. break;
  1094. }
  1095. case VIDIOCPWCGLED:
  1096. {
  1097. struct pwc_leds *leds = arg;
  1098. ret = pwc_get_leds(pdev, &leds->led_on, &leds->led_off);
  1099. break;
  1100. }
  1101. case VIDIOCPWCSCONTOUR:
  1102. {
  1103. int *contour = arg;
  1104. ret = pwc_set_contour(pdev, *contour);
  1105. break;
  1106. }
  1107. case VIDIOCPWCGCONTOUR:
  1108. {
  1109. int *contour = arg;
  1110. ret = pwc_get_contour(pdev, contour);
  1111. break;
  1112. }
  1113. case VIDIOCPWCSBACKLIGHT:
  1114. {
  1115. int *backlight = arg;
  1116. ret = pwc_set_backlight(pdev, *backlight);
  1117. break;
  1118. }
  1119. case VIDIOCPWCGBACKLIGHT:
  1120. {
  1121. int *backlight = arg;
  1122. ret = pwc_get_backlight(pdev, backlight);
  1123. break;
  1124. }
  1125. case VIDIOCPWCSFLICKER:
  1126. {
  1127. int *flicker = arg;
  1128. ret = pwc_set_flicker(pdev, *flicker);
  1129. break;
  1130. }
  1131. case VIDIOCPWCGFLICKER:
  1132. {
  1133. int *flicker = arg;
  1134. ret = pwc_get_flicker(pdev, flicker);
  1135. break;
  1136. }
  1137. case VIDIOCPWCSDYNNOISE:
  1138. {
  1139. int *dynnoise = arg;
  1140. ret = pwc_set_dynamic_noise(pdev, *dynnoise);
  1141. break;
  1142. }
  1143. case VIDIOCPWCGDYNNOISE:
  1144. {
  1145. int *dynnoise = arg;
  1146. ret = pwc_get_dynamic_noise(pdev, dynnoise);
  1147. break;
  1148. }
  1149. case VIDIOCPWCGREALSIZE:
  1150. {
  1151. struct pwc_imagesize *size = arg;
  1152. size->width = pdev->image.x;
  1153. size->height = pdev->image.y;
  1154. break;
  1155. }
  1156. case VIDIOCPWCMPTRESET:
  1157. {
  1158. if (pdev->features & FEATURE_MOTOR_PANTILT)
  1159. {
  1160. int *flags = arg;
  1161. ret = pwc_mpt_reset(pdev, *flags);
  1162. if (ret >= 0)
  1163. {
  1164. pdev->pan_angle = 0;
  1165. pdev->tilt_angle = 0;
  1166. }
  1167. }
  1168. else
  1169. {
  1170. ret = -ENXIO;
  1171. }
  1172. break;
  1173. }
  1174. case VIDIOCPWCMPTGRANGE:
  1175. {
  1176. if (pdev->features & FEATURE_MOTOR_PANTILT)
  1177. {
  1178. struct pwc_mpt_range *range = arg;
  1179. *range = pdev->angle_range;
  1180. }
  1181. else
  1182. {
  1183. ret = -ENXIO;
  1184. }
  1185. break;
  1186. }
  1187. case VIDIOCPWCMPTSANGLE:
  1188. {
  1189. int new_pan, new_tilt;
  1190. if (pdev->features & FEATURE_MOTOR_PANTILT)
  1191. {
  1192. struct pwc_mpt_angles *angles = arg;
  1193. /* The camera can only set relative angles, so
  1194. do some calculations when getting an absolute angle .
  1195. */
  1196. if (angles->absolute)
  1197. {
  1198. new_pan = angles->pan;
  1199. new_tilt = angles->tilt;
  1200. }
  1201. else
  1202. {
  1203. new_pan = pdev->pan_angle + angles->pan;
  1204. new_tilt = pdev->tilt_angle + angles->tilt;
  1205. }
  1206. /* check absolute ranges */
  1207. if (new_pan < pdev->angle_range.pan_min ||
  1208. new_pan > pdev->angle_range.pan_max ||
  1209. new_tilt < pdev->angle_range.tilt_min ||
  1210. new_tilt > pdev->angle_range.tilt_max)
  1211. {
  1212. ret = -ERANGE;
  1213. }
  1214. else
  1215. {
  1216. /* go to relative range, check again */
  1217. new_pan -= pdev->pan_angle;
  1218. new_tilt -= pdev->tilt_angle;
  1219. /* angles are specified in degrees * 100, thus the limit = 36000 */
  1220. if (new_pan < -36000 || new_pan > 36000 || new_tilt < -36000 || new_tilt > 36000)
  1221. ret = -ERANGE;
  1222. }
  1223. if (ret == 0) /* no errors so far */
  1224. {
  1225. ret = pwc_mpt_set_angle(pdev, new_pan, new_tilt);
  1226. if (ret >= 0)
  1227. {
  1228. pdev->pan_angle += new_pan;
  1229. pdev->tilt_angle += new_tilt;
  1230. }
  1231. if (ret == -EPIPE) /* stall -> out of range */
  1232. ret = -ERANGE;
  1233. }
  1234. }
  1235. else
  1236. {
  1237. ret = -ENXIO;
  1238. }
  1239. break;
  1240. }
  1241. case VIDIOCPWCMPTGANGLE:
  1242. {
  1243. if (pdev->features & FEATURE_MOTOR_PANTILT)
  1244. {
  1245. struct pwc_mpt_angles *angles = arg;
  1246. angles->absolute = 1;
  1247. angles->pan = pdev->pan_angle;
  1248. angles->tilt = pdev->tilt_angle;
  1249. }
  1250. else
  1251. {
  1252. ret = -ENXIO;
  1253. }
  1254. break;
  1255. }
  1256. case VIDIOCPWCMPTSTATUS:
  1257. {
  1258. if (pdev->features & FEATURE_MOTOR_PANTILT)
  1259. {
  1260. struct pwc_mpt_status *status = arg;
  1261. ret = pwc_mpt_get_status(pdev, status);
  1262. }
  1263. else
  1264. {
  1265. ret = -ENXIO;
  1266. }
  1267. break;
  1268. }
  1269. case VIDIOCPWCGVIDCMD:
  1270. {
  1271. struct pwc_video_command *cmd = arg;
  1272. cmd->type = pdev->type;
  1273. cmd->release = pdev->release;
  1274. cmd->command_len = pdev->cmd_len;
  1275. memcpy(&cmd->command_buf, pdev->cmd_buf, pdev->cmd_len);
  1276. cmd->bandlength = pdev->vbandlength;
  1277. cmd->frame_size = pdev->frame_size;
  1278. break;
  1279. }
  1280. /*
  1281. case VIDIOCPWCGVIDTABLE:
  1282. {
  1283. struct pwc_table_init_buffer *table = arg;
  1284. table->len = pdev->cmd_len;
  1285. memcpy(&table->buffer, pdev->decompress_data, pdev->decompressor->table_size);
  1286. break;
  1287. }
  1288. */
  1289. default:
  1290. ret = -ENOIOCTLCMD;
  1291. break;
  1292. }
  1293. if (ret > 0)
  1294. return 0;
  1295. return ret;
  1296. }