uvc_ctrl.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523
  1. /*
  2. * uvc_ctrl.c -- USB Video Class driver - Controls
  3. *
  4. * Copyright (C) 2005-2008
  5. * Laurent Pinchart (laurent.pinchart@skynet.be)
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/version.h>
  15. #include <linux/list.h>
  16. #include <linux/module.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/usb.h>
  19. #include <linux/videodev2.h>
  20. #include <linux/vmalloc.h>
  21. #include <linux/wait.h>
  22. #include <asm/atomic.h>
  23. #include "uvcvideo.h"
  24. #define UVC_CTRL_NDATA 2
  25. #define UVC_CTRL_DATA_CURRENT 0
  26. #define UVC_CTRL_DATA_BACKUP 1
  27. /* ------------------------------------------------------------------------
  28. * Control, formats, ...
  29. */
  30. static struct uvc_control_info uvc_ctrls[] = {
  31. {
  32. .entity = UVC_GUID_UVC_PROCESSING,
  33. .selector = PU_BRIGHTNESS_CONTROL,
  34. .index = 0,
  35. .size = 2,
  36. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  37. | UVC_CONTROL_RESTORE,
  38. },
  39. {
  40. .entity = UVC_GUID_UVC_PROCESSING,
  41. .selector = PU_CONTRAST_CONTROL,
  42. .index = 1,
  43. .size = 2,
  44. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  45. | UVC_CONTROL_RESTORE,
  46. },
  47. {
  48. .entity = UVC_GUID_UVC_PROCESSING,
  49. .selector = PU_HUE_CONTROL,
  50. .index = 2,
  51. .size = 2,
  52. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  53. | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
  54. },
  55. {
  56. .entity = UVC_GUID_UVC_PROCESSING,
  57. .selector = PU_SATURATION_CONTROL,
  58. .index = 3,
  59. .size = 2,
  60. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  61. | UVC_CONTROL_RESTORE,
  62. },
  63. {
  64. .entity = UVC_GUID_UVC_PROCESSING,
  65. .selector = PU_SHARPNESS_CONTROL,
  66. .index = 4,
  67. .size = 2,
  68. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  69. | UVC_CONTROL_RESTORE,
  70. },
  71. {
  72. .entity = UVC_GUID_UVC_PROCESSING,
  73. .selector = PU_GAMMA_CONTROL,
  74. .index = 5,
  75. .size = 2,
  76. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  77. | UVC_CONTROL_RESTORE,
  78. },
  79. {
  80. .entity = UVC_GUID_UVC_PROCESSING,
  81. .selector = PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
  82. .index = 6,
  83. .size = 2,
  84. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  85. | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
  86. },
  87. {
  88. .entity = UVC_GUID_UVC_PROCESSING,
  89. .selector = PU_WHITE_BALANCE_COMPONENT_CONTROL,
  90. .index = 7,
  91. .size = 4,
  92. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  93. | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
  94. },
  95. {
  96. .entity = UVC_GUID_UVC_PROCESSING,
  97. .selector = PU_BACKLIGHT_COMPENSATION_CONTROL,
  98. .index = 8,
  99. .size = 2,
  100. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  101. | UVC_CONTROL_RESTORE,
  102. },
  103. {
  104. .entity = UVC_GUID_UVC_PROCESSING,
  105. .selector = PU_GAIN_CONTROL,
  106. .index = 9,
  107. .size = 2,
  108. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  109. | UVC_CONTROL_RESTORE,
  110. },
  111. {
  112. .entity = UVC_GUID_UVC_PROCESSING,
  113. .selector = PU_POWER_LINE_FREQUENCY_CONTROL,
  114. .index = 10,
  115. .size = 1,
  116. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  117. | UVC_CONTROL_RESTORE,
  118. },
  119. {
  120. .entity = UVC_GUID_UVC_PROCESSING,
  121. .selector = PU_HUE_AUTO_CONTROL,
  122. .index = 11,
  123. .size = 1,
  124. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
  125. | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
  126. },
  127. {
  128. .entity = UVC_GUID_UVC_PROCESSING,
  129. .selector = PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
  130. .index = 12,
  131. .size = 1,
  132. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
  133. | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
  134. },
  135. {
  136. .entity = UVC_GUID_UVC_PROCESSING,
  137. .selector = PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
  138. .index = 13,
  139. .size = 1,
  140. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
  141. | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
  142. },
  143. {
  144. .entity = UVC_GUID_UVC_PROCESSING,
  145. .selector = PU_DIGITAL_MULTIPLIER_CONTROL,
  146. .index = 14,
  147. .size = 2,
  148. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  149. | UVC_CONTROL_RESTORE,
  150. },
  151. {
  152. .entity = UVC_GUID_UVC_PROCESSING,
  153. .selector = PU_DIGITAL_MULTIPLIER_LIMIT_CONTROL,
  154. .index = 15,
  155. .size = 2,
  156. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  157. | UVC_CONTROL_RESTORE,
  158. },
  159. {
  160. .entity = UVC_GUID_UVC_PROCESSING,
  161. .selector = PU_ANALOG_VIDEO_STANDARD_CONTROL,
  162. .index = 16,
  163. .size = 1,
  164. .flags = UVC_CONTROL_GET_CUR,
  165. },
  166. {
  167. .entity = UVC_GUID_UVC_PROCESSING,
  168. .selector = PU_ANALOG_LOCK_STATUS_CONTROL,
  169. .index = 17,
  170. .size = 1,
  171. .flags = UVC_CONTROL_GET_CUR,
  172. },
  173. {
  174. .entity = UVC_GUID_UVC_CAMERA,
  175. .selector = CT_SCANNING_MODE_CONTROL,
  176. .index = 0,
  177. .size = 1,
  178. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
  179. | UVC_CONTROL_RESTORE,
  180. },
  181. {
  182. .entity = UVC_GUID_UVC_CAMERA,
  183. .selector = CT_AE_MODE_CONTROL,
  184. .index = 1,
  185. .size = 1,
  186. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
  187. | UVC_CONTROL_GET_DEF | UVC_CONTROL_GET_RES
  188. | UVC_CONTROL_RESTORE,
  189. },
  190. {
  191. .entity = UVC_GUID_UVC_CAMERA,
  192. .selector = CT_AE_PRIORITY_CONTROL,
  193. .index = 2,
  194. .size = 1,
  195. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
  196. | UVC_CONTROL_RESTORE,
  197. },
  198. {
  199. .entity = UVC_GUID_UVC_CAMERA,
  200. .selector = CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
  201. .index = 3,
  202. .size = 4,
  203. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  204. | UVC_CONTROL_RESTORE,
  205. },
  206. {
  207. .entity = UVC_GUID_UVC_CAMERA,
  208. .selector = CT_EXPOSURE_TIME_RELATIVE_CONTROL,
  209. .index = 4,
  210. .size = 1,
  211. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
  212. | UVC_CONTROL_RESTORE,
  213. },
  214. {
  215. .entity = UVC_GUID_UVC_CAMERA,
  216. .selector = CT_FOCUS_ABSOLUTE_CONTROL,
  217. .index = 5,
  218. .size = 2,
  219. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  220. | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
  221. },
  222. {
  223. .entity = UVC_GUID_UVC_CAMERA,
  224. .selector = CT_FOCUS_RELATIVE_CONTROL,
  225. .index = 6,
  226. .size = 2,
  227. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  228. | UVC_CONTROL_AUTO_UPDATE,
  229. },
  230. {
  231. .entity = UVC_GUID_UVC_CAMERA,
  232. .selector = CT_IRIS_ABSOLUTE_CONTROL,
  233. .index = 7,
  234. .size = 2,
  235. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  236. | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
  237. },
  238. {
  239. .entity = UVC_GUID_UVC_CAMERA,
  240. .selector = CT_IRIS_RELATIVE_CONTROL,
  241. .index = 8,
  242. .size = 1,
  243. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
  244. | UVC_CONTROL_AUTO_UPDATE,
  245. },
  246. {
  247. .entity = UVC_GUID_UVC_CAMERA,
  248. .selector = CT_ZOOM_ABSOLUTE_CONTROL,
  249. .index = 9,
  250. .size = 2,
  251. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  252. | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
  253. },
  254. {
  255. .entity = UVC_GUID_UVC_CAMERA,
  256. .selector = CT_ZOOM_RELATIVE_CONTROL,
  257. .index = 10,
  258. .size = 3,
  259. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  260. | UVC_CONTROL_AUTO_UPDATE,
  261. },
  262. {
  263. .entity = UVC_GUID_UVC_CAMERA,
  264. .selector = CT_PANTILT_ABSOLUTE_CONTROL,
  265. .index = 11,
  266. .size = 8,
  267. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  268. | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
  269. },
  270. {
  271. .entity = UVC_GUID_UVC_CAMERA,
  272. .selector = CT_PANTILT_RELATIVE_CONTROL,
  273. .index = 12,
  274. .size = 4,
  275. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  276. | UVC_CONTROL_AUTO_UPDATE,
  277. },
  278. {
  279. .entity = UVC_GUID_UVC_CAMERA,
  280. .selector = CT_ROLL_ABSOLUTE_CONTROL,
  281. .index = 13,
  282. .size = 2,
  283. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  284. | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
  285. },
  286. {
  287. .entity = UVC_GUID_UVC_CAMERA,
  288. .selector = CT_ROLL_RELATIVE_CONTROL,
  289. .index = 14,
  290. .size = 2,
  291. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  292. | UVC_CONTROL_AUTO_UPDATE,
  293. },
  294. {
  295. .entity = UVC_GUID_UVC_CAMERA,
  296. .selector = CT_FOCUS_AUTO_CONTROL,
  297. .index = 17,
  298. .size = 1,
  299. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
  300. | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
  301. },
  302. {
  303. .entity = UVC_GUID_UVC_CAMERA,
  304. .selector = CT_PRIVACY_CONTROL,
  305. .index = 18,
  306. .size = 1,
  307. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
  308. | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
  309. },
  310. };
  311. static struct uvc_menu_info power_line_frequency_controls[] = {
  312. { 0, "Disabled" },
  313. { 1, "50 Hz" },
  314. { 2, "60 Hz" },
  315. };
  316. static struct uvc_menu_info exposure_auto_controls[] = {
  317. { 2, "Auto Mode" },
  318. { 1, "Manual Mode" },
  319. { 4, "Shutter Priority Mode" },
  320. { 8, "Aperture Priority Mode" },
  321. };
  322. static __s32 uvc_ctrl_get_zoom(struct uvc_control_mapping *mapping,
  323. __u8 query, const __u8 *data)
  324. {
  325. __s8 zoom = (__s8)data[0];
  326. switch (query) {
  327. case GET_CUR:
  328. return (zoom == 0) ? 0 : (zoom > 0 ? data[2] : -data[2]);
  329. case GET_MIN:
  330. case GET_MAX:
  331. case GET_RES:
  332. case GET_DEF:
  333. default:
  334. return data[2];
  335. }
  336. }
  337. static void uvc_ctrl_set_zoom(struct uvc_control_mapping *mapping,
  338. __s32 value, __u8 *data)
  339. {
  340. data[0] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
  341. data[2] = min(abs(value), 0xff);
  342. }
  343. static struct uvc_control_mapping uvc_ctrl_mappings[] = {
  344. {
  345. .id = V4L2_CID_BRIGHTNESS,
  346. .name = "Brightness",
  347. .entity = UVC_GUID_UVC_PROCESSING,
  348. .selector = PU_BRIGHTNESS_CONTROL,
  349. .size = 16,
  350. .offset = 0,
  351. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  352. .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
  353. },
  354. {
  355. .id = V4L2_CID_CONTRAST,
  356. .name = "Contrast",
  357. .entity = UVC_GUID_UVC_PROCESSING,
  358. .selector = PU_CONTRAST_CONTROL,
  359. .size = 16,
  360. .offset = 0,
  361. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  362. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  363. },
  364. {
  365. .id = V4L2_CID_HUE,
  366. .name = "Hue",
  367. .entity = UVC_GUID_UVC_PROCESSING,
  368. .selector = PU_HUE_CONTROL,
  369. .size = 16,
  370. .offset = 0,
  371. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  372. .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
  373. },
  374. {
  375. .id = V4L2_CID_SATURATION,
  376. .name = "Saturation",
  377. .entity = UVC_GUID_UVC_PROCESSING,
  378. .selector = PU_SATURATION_CONTROL,
  379. .size = 16,
  380. .offset = 0,
  381. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  382. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  383. },
  384. {
  385. .id = V4L2_CID_SHARPNESS,
  386. .name = "Sharpness",
  387. .entity = UVC_GUID_UVC_PROCESSING,
  388. .selector = PU_SHARPNESS_CONTROL,
  389. .size = 16,
  390. .offset = 0,
  391. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  392. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  393. },
  394. {
  395. .id = V4L2_CID_GAMMA,
  396. .name = "Gamma",
  397. .entity = UVC_GUID_UVC_PROCESSING,
  398. .selector = PU_GAMMA_CONTROL,
  399. .size = 16,
  400. .offset = 0,
  401. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  402. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  403. },
  404. {
  405. .id = V4L2_CID_BACKLIGHT_COMPENSATION,
  406. .name = "Backlight Compensation",
  407. .entity = UVC_GUID_UVC_PROCESSING,
  408. .selector = PU_BACKLIGHT_COMPENSATION_CONTROL,
  409. .size = 16,
  410. .offset = 0,
  411. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  412. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  413. },
  414. {
  415. .id = V4L2_CID_GAIN,
  416. .name = "Gain",
  417. .entity = UVC_GUID_UVC_PROCESSING,
  418. .selector = PU_GAIN_CONTROL,
  419. .size = 16,
  420. .offset = 0,
  421. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  422. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  423. },
  424. {
  425. .id = V4L2_CID_POWER_LINE_FREQUENCY,
  426. .name = "Power Line Frequency",
  427. .entity = UVC_GUID_UVC_PROCESSING,
  428. .selector = PU_POWER_LINE_FREQUENCY_CONTROL,
  429. .size = 2,
  430. .offset = 0,
  431. .v4l2_type = V4L2_CTRL_TYPE_MENU,
  432. .data_type = UVC_CTRL_DATA_TYPE_ENUM,
  433. .menu_info = power_line_frequency_controls,
  434. .menu_count = ARRAY_SIZE(power_line_frequency_controls),
  435. },
  436. {
  437. .id = V4L2_CID_HUE_AUTO,
  438. .name = "Hue, Auto",
  439. .entity = UVC_GUID_UVC_PROCESSING,
  440. .selector = PU_HUE_AUTO_CONTROL,
  441. .size = 1,
  442. .offset = 0,
  443. .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
  444. .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
  445. },
  446. {
  447. .id = V4L2_CID_EXPOSURE_AUTO,
  448. .name = "Exposure, Auto",
  449. .entity = UVC_GUID_UVC_CAMERA,
  450. .selector = CT_AE_MODE_CONTROL,
  451. .size = 4,
  452. .offset = 0,
  453. .v4l2_type = V4L2_CTRL_TYPE_MENU,
  454. .data_type = UVC_CTRL_DATA_TYPE_BITMASK,
  455. .menu_info = exposure_auto_controls,
  456. .menu_count = ARRAY_SIZE(exposure_auto_controls),
  457. },
  458. {
  459. .id = V4L2_CID_EXPOSURE_AUTO_PRIORITY,
  460. .name = "Exposure, Auto Priority",
  461. .entity = UVC_GUID_UVC_CAMERA,
  462. .selector = CT_AE_PRIORITY_CONTROL,
  463. .size = 1,
  464. .offset = 0,
  465. .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
  466. .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
  467. },
  468. {
  469. .id = V4L2_CID_EXPOSURE_ABSOLUTE,
  470. .name = "Exposure (Absolute)",
  471. .entity = UVC_GUID_UVC_CAMERA,
  472. .selector = CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
  473. .size = 32,
  474. .offset = 0,
  475. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  476. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  477. },
  478. {
  479. .id = V4L2_CID_AUTO_WHITE_BALANCE,
  480. .name = "White Balance Temperature, Auto",
  481. .entity = UVC_GUID_UVC_PROCESSING,
  482. .selector = PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
  483. .size = 1,
  484. .offset = 0,
  485. .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
  486. .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
  487. },
  488. {
  489. .id = V4L2_CID_WHITE_BALANCE_TEMPERATURE,
  490. .name = "White Balance Temperature",
  491. .entity = UVC_GUID_UVC_PROCESSING,
  492. .selector = PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
  493. .size = 16,
  494. .offset = 0,
  495. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  496. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  497. },
  498. {
  499. .id = V4L2_CID_AUTO_WHITE_BALANCE,
  500. .name = "White Balance Component, Auto",
  501. .entity = UVC_GUID_UVC_PROCESSING,
  502. .selector = PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
  503. .size = 1,
  504. .offset = 0,
  505. .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
  506. .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
  507. },
  508. {
  509. .id = V4L2_CID_BLUE_BALANCE,
  510. .name = "White Balance Blue Component",
  511. .entity = UVC_GUID_UVC_PROCESSING,
  512. .selector = PU_WHITE_BALANCE_COMPONENT_CONTROL,
  513. .size = 16,
  514. .offset = 0,
  515. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  516. .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
  517. },
  518. {
  519. .id = V4L2_CID_RED_BALANCE,
  520. .name = "White Balance Red Component",
  521. .entity = UVC_GUID_UVC_PROCESSING,
  522. .selector = PU_WHITE_BALANCE_COMPONENT_CONTROL,
  523. .size = 16,
  524. .offset = 16,
  525. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  526. .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
  527. },
  528. {
  529. .id = V4L2_CID_FOCUS_ABSOLUTE,
  530. .name = "Focus (absolute)",
  531. .entity = UVC_GUID_UVC_CAMERA,
  532. .selector = CT_FOCUS_ABSOLUTE_CONTROL,
  533. .size = 16,
  534. .offset = 0,
  535. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  536. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  537. },
  538. {
  539. .id = V4L2_CID_FOCUS_AUTO,
  540. .name = "Focus, Auto",
  541. .entity = UVC_GUID_UVC_CAMERA,
  542. .selector = CT_FOCUS_AUTO_CONTROL,
  543. .size = 1,
  544. .offset = 0,
  545. .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
  546. .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
  547. },
  548. {
  549. .id = V4L2_CID_ZOOM_ABSOLUTE,
  550. .name = "Zoom, Absolute",
  551. .entity = UVC_GUID_UVC_CAMERA,
  552. .selector = CT_ZOOM_ABSOLUTE_CONTROL,
  553. .size = 16,
  554. .offset = 0,
  555. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  556. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  557. },
  558. {
  559. .id = V4L2_CID_ZOOM_CONTINUOUS,
  560. .name = "Zoom, Continuous",
  561. .entity = UVC_GUID_UVC_CAMERA,
  562. .selector = CT_ZOOM_RELATIVE_CONTROL,
  563. .size = 0,
  564. .offset = 0,
  565. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  566. .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
  567. .get = uvc_ctrl_get_zoom,
  568. .set = uvc_ctrl_set_zoom,
  569. },
  570. {
  571. .id = V4L2_CID_PRIVACY,
  572. .name = "Privacy",
  573. .entity = UVC_GUID_UVC_CAMERA,
  574. .selector = CT_PRIVACY_CONTROL,
  575. .size = 1,
  576. .offset = 0,
  577. .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
  578. .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
  579. },
  580. };
  581. /* ------------------------------------------------------------------------
  582. * Utility functions
  583. */
  584. static inline __u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id)
  585. {
  586. return ctrl->data + id * ctrl->info->size;
  587. }
  588. static inline int uvc_test_bit(const __u8 *data, int bit)
  589. {
  590. return (data[bit >> 3] >> (bit & 7)) & 1;
  591. }
  592. static inline void uvc_clear_bit(__u8 *data, int bit)
  593. {
  594. data[bit >> 3] &= ~(1 << (bit & 7));
  595. }
  596. /* Extract the bit string specified by mapping->offset and mapping->size
  597. * from the little-endian data stored at 'data' and return the result as
  598. * a signed 32bit integer. Sign extension will be performed if the mapping
  599. * references a signed data type.
  600. */
  601. static __s32 uvc_get_le_value(struct uvc_control_mapping *mapping,
  602. __u8 query, const __u8 *data)
  603. {
  604. int bits = mapping->size;
  605. int offset = mapping->offset;
  606. __s32 value = 0;
  607. __u8 mask;
  608. data += offset / 8;
  609. offset &= 7;
  610. mask = ((1LL << bits) - 1) << offset;
  611. for (; bits > 0; data++) {
  612. __u8 byte = *data & mask;
  613. value |= offset > 0 ? (byte >> offset) : (byte << (-offset));
  614. bits -= 8 - (offset > 0 ? offset : 0);
  615. offset -= 8;
  616. mask = (1 << bits) - 1;
  617. }
  618. /* Sign-extend the value if needed */
  619. if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
  620. value |= -(value & (1 << (mapping->size - 1)));
  621. return value;
  622. }
  623. /* Set the bit string specified by mapping->offset and mapping->size
  624. * in the little-endian data stored at 'data' to the value 'value'.
  625. */
  626. static void uvc_set_le_value(struct uvc_control_mapping *mapping,
  627. __s32 value, __u8 *data)
  628. {
  629. int bits = mapping->size;
  630. int offset = mapping->offset;
  631. __u8 mask;
  632. data += offset / 8;
  633. offset &= 7;
  634. for (; bits > 0; data++) {
  635. mask = ((1LL << bits) - 1) << offset;
  636. *data = (*data & ~mask) | ((value << offset) & mask);
  637. value >>= offset ? offset : 8;
  638. bits -= 8 - offset;
  639. offset = 0;
  640. }
  641. }
  642. /* ------------------------------------------------------------------------
  643. * Terminal and unit management
  644. */
  645. static const __u8 uvc_processing_guid[16] = UVC_GUID_UVC_PROCESSING;
  646. static const __u8 uvc_camera_guid[16] = UVC_GUID_UVC_CAMERA;
  647. static const __u8 uvc_media_transport_input_guid[16] =
  648. UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT;
  649. static int uvc_entity_match_guid(struct uvc_entity *entity, __u8 guid[16])
  650. {
  651. switch (UVC_ENTITY_TYPE(entity)) {
  652. case ITT_CAMERA:
  653. return memcmp(uvc_camera_guid, guid, 16) == 0;
  654. case ITT_MEDIA_TRANSPORT_INPUT:
  655. return memcmp(uvc_media_transport_input_guid, guid, 16) == 0;
  656. case VC_PROCESSING_UNIT:
  657. return memcmp(uvc_processing_guid, guid, 16) == 0;
  658. case VC_EXTENSION_UNIT:
  659. return memcmp(entity->extension.guidExtensionCode,
  660. guid, 16) == 0;
  661. default:
  662. return 0;
  663. }
  664. }
  665. /* ------------------------------------------------------------------------
  666. * UVC Controls
  667. */
  668. static void __uvc_find_control(struct uvc_entity *entity, __u32 v4l2_id,
  669. struct uvc_control_mapping **mapping, struct uvc_control **control,
  670. int next)
  671. {
  672. struct uvc_control *ctrl;
  673. struct uvc_control_mapping *map;
  674. unsigned int i;
  675. if (entity == NULL)
  676. return;
  677. for (i = 0; i < entity->ncontrols; ++i) {
  678. ctrl = &entity->controls[i];
  679. if (ctrl->info == NULL)
  680. continue;
  681. list_for_each_entry(map, &ctrl->info->mappings, list) {
  682. if ((map->id == v4l2_id) && !next) {
  683. *control = ctrl;
  684. *mapping = map;
  685. return;
  686. }
  687. if ((*mapping == NULL || (*mapping)->id > map->id) &&
  688. (map->id > v4l2_id) && next) {
  689. *control = ctrl;
  690. *mapping = map;
  691. }
  692. }
  693. }
  694. }
  695. struct uvc_control *uvc_find_control(struct uvc_video_device *video,
  696. __u32 v4l2_id, struct uvc_control_mapping **mapping)
  697. {
  698. struct uvc_control *ctrl = NULL;
  699. struct uvc_entity *entity;
  700. int next = v4l2_id & V4L2_CTRL_FLAG_NEXT_CTRL;
  701. *mapping = NULL;
  702. /* Mask the query flags. */
  703. v4l2_id &= V4L2_CTRL_ID_MASK;
  704. /* Find the control. */
  705. __uvc_find_control(video->processing, v4l2_id, mapping, &ctrl, next);
  706. if (ctrl && !next)
  707. return ctrl;
  708. list_for_each_entry(entity, &video->iterms, chain) {
  709. __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next);
  710. if (ctrl && !next)
  711. return ctrl;
  712. }
  713. list_for_each_entry(entity, &video->extensions, chain) {
  714. __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next);
  715. if (ctrl && !next)
  716. return ctrl;
  717. }
  718. if (ctrl == NULL && !next)
  719. uvc_trace(UVC_TRACE_CONTROL, "Control 0x%08x not found.\n",
  720. v4l2_id);
  721. return ctrl;
  722. }
  723. int uvc_query_v4l2_ctrl(struct uvc_video_device *video,
  724. struct v4l2_queryctrl *v4l2_ctrl)
  725. {
  726. struct uvc_control *ctrl;
  727. struct uvc_control_mapping *mapping;
  728. struct uvc_menu_info *menu;
  729. unsigned int i;
  730. __u8 *data;
  731. int ret;
  732. ctrl = uvc_find_control(video, v4l2_ctrl->id, &mapping);
  733. if (ctrl == NULL)
  734. return -EINVAL;
  735. data = kmalloc(ctrl->info->size, GFP_KERNEL);
  736. if (data == NULL)
  737. return -ENOMEM;
  738. memset(v4l2_ctrl, 0, sizeof *v4l2_ctrl);
  739. v4l2_ctrl->id = mapping->id;
  740. v4l2_ctrl->type = mapping->v4l2_type;
  741. strncpy(v4l2_ctrl->name, mapping->name, sizeof v4l2_ctrl->name);
  742. v4l2_ctrl->flags = 0;
  743. if (!(ctrl->info->flags & UVC_CONTROL_SET_CUR))
  744. v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  745. if (ctrl->info->flags & UVC_CONTROL_GET_DEF) {
  746. if ((ret = uvc_query_ctrl(video->dev, GET_DEF, ctrl->entity->id,
  747. video->dev->intfnum, ctrl->info->selector,
  748. data, ctrl->info->size)) < 0)
  749. goto out;
  750. v4l2_ctrl->default_value = mapping->get(mapping, GET_DEF, data);
  751. }
  752. switch (mapping->v4l2_type) {
  753. case V4L2_CTRL_TYPE_MENU:
  754. v4l2_ctrl->minimum = 0;
  755. v4l2_ctrl->maximum = mapping->menu_count - 1;
  756. v4l2_ctrl->step = 1;
  757. menu = mapping->menu_info;
  758. for (i = 0; i < mapping->menu_count; ++i, ++menu) {
  759. if (menu->value == v4l2_ctrl->default_value) {
  760. v4l2_ctrl->default_value = i;
  761. break;
  762. }
  763. }
  764. ret = 0;
  765. goto out;
  766. case V4L2_CTRL_TYPE_BOOLEAN:
  767. v4l2_ctrl->minimum = 0;
  768. v4l2_ctrl->maximum = 1;
  769. v4l2_ctrl->step = 1;
  770. ret = 0;
  771. goto out;
  772. default:
  773. break;
  774. }
  775. if (ctrl->info->flags & UVC_CONTROL_GET_MIN) {
  776. if ((ret = uvc_query_ctrl(video->dev, GET_MIN, ctrl->entity->id,
  777. video->dev->intfnum, ctrl->info->selector,
  778. data, ctrl->info->size)) < 0)
  779. goto out;
  780. v4l2_ctrl->minimum = mapping->get(mapping, GET_MIN, data);
  781. }
  782. if (ctrl->info->flags & UVC_CONTROL_GET_MAX) {
  783. if ((ret = uvc_query_ctrl(video->dev, GET_MAX, ctrl->entity->id,
  784. video->dev->intfnum, ctrl->info->selector,
  785. data, ctrl->info->size)) < 0)
  786. goto out;
  787. v4l2_ctrl->maximum = mapping->get(mapping, GET_MAX, data);
  788. }
  789. if (ctrl->info->flags & UVC_CONTROL_GET_RES) {
  790. if ((ret = uvc_query_ctrl(video->dev, GET_RES, ctrl->entity->id,
  791. video->dev->intfnum, ctrl->info->selector,
  792. data, ctrl->info->size)) < 0)
  793. goto out;
  794. v4l2_ctrl->step = mapping->get(mapping, GET_RES, data);
  795. }
  796. ret = 0;
  797. out:
  798. kfree(data);
  799. return ret;
  800. }
  801. /* --------------------------------------------------------------------------
  802. * Control transactions
  803. *
  804. * To make extended set operations as atomic as the hardware allows, controls
  805. * are handled using begin/commit/rollback operations.
  806. *
  807. * At the beginning of a set request, uvc_ctrl_begin should be called to
  808. * initialize the request. This function acquires the control lock.
  809. *
  810. * When setting a control, the new value is stored in the control data field
  811. * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
  812. * later processing. If the UVC and V4L2 control sizes differ, the current
  813. * value is loaded from the hardware before storing the new value in the data
  814. * field.
  815. *
  816. * After processing all controls in the transaction, uvc_ctrl_commit or
  817. * uvc_ctrl_rollback must be called to apply the pending changes to the
  818. * hardware or revert them. When applying changes, all controls marked as
  819. * dirty will be modified in the UVC device, and the dirty flag will be
  820. * cleared. When reverting controls, the control data field
  821. * UVC_CTRL_DATA_CURRENT is reverted to its previous value
  822. * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
  823. * control lock.
  824. */
  825. int uvc_ctrl_begin(struct uvc_video_device *video)
  826. {
  827. return mutex_lock_interruptible(&video->ctrl_mutex) ? -ERESTARTSYS : 0;
  828. }
  829. static int uvc_ctrl_commit_entity(struct uvc_device *dev,
  830. struct uvc_entity *entity, int rollback)
  831. {
  832. struct uvc_control *ctrl;
  833. unsigned int i;
  834. int ret;
  835. if (entity == NULL)
  836. return 0;
  837. for (i = 0; i < entity->ncontrols; ++i) {
  838. ctrl = &entity->controls[i];
  839. if (ctrl->info == NULL)
  840. continue;
  841. /* Reset the loaded flag for auto-update controls that were
  842. * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
  843. * uvc_ctrl_get from using the cached value.
  844. */
  845. if (ctrl->info->flags & UVC_CONTROL_AUTO_UPDATE)
  846. ctrl->loaded = 0;
  847. if (!ctrl->dirty)
  848. continue;
  849. if (!rollback)
  850. ret = uvc_query_ctrl(dev, SET_CUR, ctrl->entity->id,
  851. dev->intfnum, ctrl->info->selector,
  852. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  853. ctrl->info->size);
  854. else
  855. ret = 0;
  856. if (rollback || ret < 0)
  857. memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  858. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
  859. ctrl->info->size);
  860. ctrl->dirty = 0;
  861. if (ret < 0)
  862. return ret;
  863. }
  864. return 0;
  865. }
  866. int __uvc_ctrl_commit(struct uvc_video_device *video, int rollback)
  867. {
  868. struct uvc_entity *entity;
  869. int ret = 0;
  870. /* Find the control. */
  871. ret = uvc_ctrl_commit_entity(video->dev, video->processing, rollback);
  872. if (ret < 0)
  873. goto done;
  874. list_for_each_entry(entity, &video->iterms, chain) {
  875. ret = uvc_ctrl_commit_entity(video->dev, entity, rollback);
  876. if (ret < 0)
  877. goto done;
  878. }
  879. list_for_each_entry(entity, &video->extensions, chain) {
  880. ret = uvc_ctrl_commit_entity(video->dev, entity, rollback);
  881. if (ret < 0)
  882. goto done;
  883. }
  884. done:
  885. mutex_unlock(&video->ctrl_mutex);
  886. return ret;
  887. }
  888. int uvc_ctrl_get(struct uvc_video_device *video,
  889. struct v4l2_ext_control *xctrl)
  890. {
  891. struct uvc_control *ctrl;
  892. struct uvc_control_mapping *mapping;
  893. struct uvc_menu_info *menu;
  894. unsigned int i;
  895. int ret;
  896. ctrl = uvc_find_control(video, xctrl->id, &mapping);
  897. if (ctrl == NULL || (ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0)
  898. return -EINVAL;
  899. if (!ctrl->loaded) {
  900. ret = uvc_query_ctrl(video->dev, GET_CUR, ctrl->entity->id,
  901. video->dev->intfnum, ctrl->info->selector,
  902. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  903. ctrl->info->size);
  904. if (ret < 0)
  905. return ret;
  906. ctrl->loaded = 1;
  907. }
  908. xctrl->value = mapping->get(mapping, GET_CUR,
  909. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
  910. if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
  911. menu = mapping->menu_info;
  912. for (i = 0; i < mapping->menu_count; ++i, ++menu) {
  913. if (menu->value == xctrl->value) {
  914. xctrl->value = i;
  915. break;
  916. }
  917. }
  918. }
  919. return 0;
  920. }
  921. int uvc_ctrl_set(struct uvc_video_device *video,
  922. struct v4l2_ext_control *xctrl)
  923. {
  924. struct uvc_control *ctrl;
  925. struct uvc_control_mapping *mapping;
  926. s32 value = xctrl->value;
  927. int ret;
  928. ctrl = uvc_find_control(video, xctrl->id, &mapping);
  929. if (ctrl == NULL || (ctrl->info->flags & UVC_CONTROL_SET_CUR) == 0)
  930. return -EINVAL;
  931. if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
  932. if (value < 0 || value >= mapping->menu_count)
  933. return -EINVAL;
  934. value = mapping->menu_info[value].value;
  935. }
  936. if (!ctrl->loaded && (ctrl->info->size * 8) != mapping->size) {
  937. if ((ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0) {
  938. memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  939. 0, ctrl->info->size);
  940. } else {
  941. ret = uvc_query_ctrl(video->dev, GET_CUR,
  942. ctrl->entity->id, video->dev->intfnum,
  943. ctrl->info->selector,
  944. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  945. ctrl->info->size);
  946. if (ret < 0)
  947. return ret;
  948. }
  949. ctrl->loaded = 1;
  950. }
  951. if (!ctrl->dirty) {
  952. memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
  953. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  954. ctrl->info->size);
  955. }
  956. mapping->set(mapping, value,
  957. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
  958. ctrl->dirty = 1;
  959. ctrl->modified = 1;
  960. return 0;
  961. }
  962. /* --------------------------------------------------------------------------
  963. * Dynamic controls
  964. */
  965. int uvc_xu_ctrl_query(struct uvc_video_device *video,
  966. struct uvc_xu_control *xctrl, int set)
  967. {
  968. struct uvc_entity *entity;
  969. struct uvc_control *ctrl = NULL;
  970. unsigned int i, found = 0;
  971. __u8 *data;
  972. int ret;
  973. /* Find the extension unit. */
  974. list_for_each_entry(entity, &video->extensions, chain) {
  975. if (entity->id == xctrl->unit)
  976. break;
  977. }
  978. if (entity->id != xctrl->unit) {
  979. uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
  980. xctrl->unit);
  981. return -EINVAL;
  982. }
  983. /* Find the control. */
  984. for (i = 0; i < entity->ncontrols; ++i) {
  985. ctrl = &entity->controls[i];
  986. if (ctrl->info == NULL)
  987. continue;
  988. if (ctrl->info->selector == xctrl->selector) {
  989. found = 1;
  990. break;
  991. }
  992. }
  993. if (!found) {
  994. uvc_trace(UVC_TRACE_CONTROL,
  995. "Control " UVC_GUID_FORMAT "/%u not found.\n",
  996. UVC_GUID_ARGS(entity->extension.guidExtensionCode),
  997. xctrl->selector);
  998. return -EINVAL;
  999. }
  1000. /* Validate control data size. */
  1001. if (ctrl->info->size != xctrl->size)
  1002. return -EINVAL;
  1003. if ((set && !(ctrl->info->flags & UVC_CONTROL_SET_CUR)) ||
  1004. (!set && !(ctrl->info->flags & UVC_CONTROL_GET_CUR)))
  1005. return -EINVAL;
  1006. if (mutex_lock_interruptible(&video->ctrl_mutex))
  1007. return -ERESTARTSYS;
  1008. memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
  1009. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  1010. xctrl->size);
  1011. data = uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT);
  1012. if (set && copy_from_user(data, xctrl->data, xctrl->size)) {
  1013. ret = -EFAULT;
  1014. goto out;
  1015. }
  1016. ret = uvc_query_ctrl(video->dev, set ? SET_CUR : GET_CUR, xctrl->unit,
  1017. video->dev->intfnum, xctrl->selector, data,
  1018. xctrl->size);
  1019. if (ret < 0)
  1020. goto out;
  1021. if (!set && copy_to_user(xctrl->data, data, xctrl->size)) {
  1022. ret = -EFAULT;
  1023. goto out;
  1024. }
  1025. out:
  1026. if (ret)
  1027. memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  1028. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
  1029. xctrl->size);
  1030. mutex_unlock(&video->ctrl_mutex);
  1031. return ret;
  1032. }
  1033. /* --------------------------------------------------------------------------
  1034. * Suspend/resume
  1035. */
  1036. /*
  1037. * Restore control values after resume, skipping controls that haven't been
  1038. * changed.
  1039. *
  1040. * TODO
  1041. * - Don't restore modified controls that are back to their default value.
  1042. * - Handle restore order (Auto-Exposure Mode should be restored before
  1043. * Exposure Time).
  1044. */
  1045. int uvc_ctrl_resume_device(struct uvc_device *dev)
  1046. {
  1047. struct uvc_control *ctrl;
  1048. struct uvc_entity *entity;
  1049. unsigned int i;
  1050. int ret;
  1051. /* Walk the entities list and restore controls when possible. */
  1052. list_for_each_entry(entity, &dev->entities, list) {
  1053. for (i = 0; i < entity->ncontrols; ++i) {
  1054. ctrl = &entity->controls[i];
  1055. if (ctrl->info == NULL || !ctrl->modified ||
  1056. (ctrl->info->flags & UVC_CONTROL_RESTORE) == 0)
  1057. continue;
  1058. printk(KERN_INFO "restoring control " UVC_GUID_FORMAT
  1059. "/%u/%u\n", UVC_GUID_ARGS(ctrl->info->entity),
  1060. ctrl->info->index, ctrl->info->selector);
  1061. ctrl->dirty = 1;
  1062. }
  1063. ret = uvc_ctrl_commit_entity(dev, entity, 0);
  1064. if (ret < 0)
  1065. return ret;
  1066. }
  1067. return 0;
  1068. }
  1069. /* --------------------------------------------------------------------------
  1070. * Control and mapping handling
  1071. */
  1072. static void uvc_ctrl_add_ctrl(struct uvc_device *dev,
  1073. struct uvc_control_info *info)
  1074. {
  1075. struct uvc_entity *entity;
  1076. struct uvc_control *ctrl = NULL;
  1077. int ret, found = 0;
  1078. unsigned int i;
  1079. list_for_each_entry(entity, &dev->entities, list) {
  1080. if (!uvc_entity_match_guid(entity, info->entity))
  1081. continue;
  1082. for (i = 0; i < entity->ncontrols; ++i) {
  1083. ctrl = &entity->controls[i];
  1084. if (ctrl->index == info->index) {
  1085. found = 1;
  1086. break;
  1087. }
  1088. }
  1089. if (found)
  1090. break;
  1091. }
  1092. if (!found)
  1093. return;
  1094. if (UVC_ENTITY_TYPE(entity) == VC_EXTENSION_UNIT) {
  1095. /* Check if the device control information and length match
  1096. * the user supplied information.
  1097. */
  1098. __u32 flags;
  1099. __le16 size;
  1100. __u8 inf;
  1101. if ((ret = uvc_query_ctrl(dev, GET_LEN, ctrl->entity->id,
  1102. dev->intfnum, info->selector, (__u8 *)&size, 2)) < 0) {
  1103. uvc_trace(UVC_TRACE_CONTROL, "GET_LEN failed on "
  1104. "control " UVC_GUID_FORMAT "/%u (%d).\n",
  1105. UVC_GUID_ARGS(info->entity), info->selector,
  1106. ret);
  1107. return;
  1108. }
  1109. if (info->size != le16_to_cpu(size)) {
  1110. uvc_trace(UVC_TRACE_CONTROL, "Control " UVC_GUID_FORMAT
  1111. "/%u size doesn't match user supplied "
  1112. "value.\n", UVC_GUID_ARGS(info->entity),
  1113. info->selector);
  1114. return;
  1115. }
  1116. if ((ret = uvc_query_ctrl(dev, GET_INFO, ctrl->entity->id,
  1117. dev->intfnum, info->selector, &inf, 1)) < 0) {
  1118. uvc_trace(UVC_TRACE_CONTROL, "GET_INFO failed on "
  1119. "control " UVC_GUID_FORMAT "/%u (%d).\n",
  1120. UVC_GUID_ARGS(info->entity), info->selector,
  1121. ret);
  1122. return;
  1123. }
  1124. flags = info->flags;
  1125. if (((flags & UVC_CONTROL_GET_CUR) && !(inf & (1 << 0))) ||
  1126. ((flags & UVC_CONTROL_SET_CUR) && !(inf & (1 << 1)))) {
  1127. uvc_trace(UVC_TRACE_CONTROL, "Control "
  1128. UVC_GUID_FORMAT "/%u flags don't match "
  1129. "supported operations.\n",
  1130. UVC_GUID_ARGS(info->entity), info->selector);
  1131. return;
  1132. }
  1133. }
  1134. ctrl->info = info;
  1135. ctrl->data = kmalloc(ctrl->info->size * UVC_CTRL_NDATA, GFP_KERNEL);
  1136. uvc_trace(UVC_TRACE_CONTROL, "Added control " UVC_GUID_FORMAT "/%u "
  1137. "to device %s entity %u\n", UVC_GUID_ARGS(ctrl->info->entity),
  1138. ctrl->info->selector, dev->udev->devpath, entity->id);
  1139. }
  1140. /*
  1141. * Add an item to the UVC control information list, and instantiate a control
  1142. * structure for each device that supports the control.
  1143. */
  1144. int uvc_ctrl_add_info(struct uvc_control_info *info)
  1145. {
  1146. struct uvc_control_info *ctrl;
  1147. struct uvc_device *dev;
  1148. int ret = 0;
  1149. /* Find matching controls by walking the devices, entities and
  1150. * controls list.
  1151. */
  1152. mutex_lock(&uvc_driver.ctrl_mutex);
  1153. /* First check if the list contains a control matching the new one.
  1154. * Bail out if it does.
  1155. */
  1156. list_for_each_entry(ctrl, &uvc_driver.controls, list) {
  1157. if (memcmp(ctrl->entity, info->entity, 16))
  1158. continue;
  1159. if (ctrl->selector == info->selector) {
  1160. uvc_trace(UVC_TRACE_CONTROL, "Control "
  1161. UVC_GUID_FORMAT "/%u is already defined.\n",
  1162. UVC_GUID_ARGS(info->entity), info->selector);
  1163. ret = -EEXIST;
  1164. goto end;
  1165. }
  1166. if (ctrl->index == info->index) {
  1167. uvc_trace(UVC_TRACE_CONTROL, "Control "
  1168. UVC_GUID_FORMAT "/%u would overwrite index "
  1169. "%d.\n", UVC_GUID_ARGS(info->entity),
  1170. info->selector, info->index);
  1171. ret = -EEXIST;
  1172. goto end;
  1173. }
  1174. }
  1175. list_for_each_entry(dev, &uvc_driver.devices, list)
  1176. uvc_ctrl_add_ctrl(dev, info);
  1177. INIT_LIST_HEAD(&info->mappings);
  1178. list_add_tail(&info->list, &uvc_driver.controls);
  1179. end:
  1180. mutex_unlock(&uvc_driver.ctrl_mutex);
  1181. return ret;
  1182. }
  1183. int uvc_ctrl_add_mapping(struct uvc_control_mapping *mapping)
  1184. {
  1185. struct uvc_control_info *info;
  1186. struct uvc_control_mapping *map;
  1187. int ret = -EINVAL;
  1188. if (mapping->get == NULL)
  1189. mapping->get = uvc_get_le_value;
  1190. if (mapping->set == NULL)
  1191. mapping->set = uvc_set_le_value;
  1192. if (mapping->id & ~V4L2_CTRL_ID_MASK) {
  1193. uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s' with "
  1194. "invalid control id 0x%08x\n", mapping->name,
  1195. mapping->id);
  1196. return -EINVAL;
  1197. }
  1198. mutex_lock(&uvc_driver.ctrl_mutex);
  1199. list_for_each_entry(info, &uvc_driver.controls, list) {
  1200. if (memcmp(info->entity, mapping->entity, 16) ||
  1201. info->selector != mapping->selector)
  1202. continue;
  1203. if (info->size * 8 < mapping->size + mapping->offset) {
  1204. uvc_trace(UVC_TRACE_CONTROL, "Mapping '%s' would "
  1205. "overflow control " UVC_GUID_FORMAT "/%u\n",
  1206. mapping->name, UVC_GUID_ARGS(info->entity),
  1207. info->selector);
  1208. ret = -EOVERFLOW;
  1209. goto end;
  1210. }
  1211. /* Check if the list contains a mapping matching the new one.
  1212. * Bail out if it does.
  1213. */
  1214. list_for_each_entry(map, &info->mappings, list) {
  1215. if (map->id == mapping->id) {
  1216. uvc_trace(UVC_TRACE_CONTROL, "Mapping '%s' is "
  1217. "already defined.\n", mapping->name);
  1218. ret = -EEXIST;
  1219. goto end;
  1220. }
  1221. }
  1222. mapping->ctrl = info;
  1223. list_add_tail(&mapping->list, &info->mappings);
  1224. uvc_trace(UVC_TRACE_CONTROL, "Adding mapping %s to control "
  1225. UVC_GUID_FORMAT "/%u.\n", mapping->name,
  1226. UVC_GUID_ARGS(info->entity), info->selector);
  1227. ret = 0;
  1228. break;
  1229. }
  1230. end:
  1231. mutex_unlock(&uvc_driver.ctrl_mutex);
  1232. return ret;
  1233. }
  1234. /*
  1235. * Prune an entity of its bogus controls. This currently includes processing
  1236. * unit auto controls for which no corresponding manual control is available.
  1237. * Such auto controls make little sense if any, and are known to crash at
  1238. * least the SiGma Micro webcam.
  1239. */
  1240. static void
  1241. uvc_ctrl_prune_entity(struct uvc_entity *entity)
  1242. {
  1243. static const struct {
  1244. u8 idx_manual;
  1245. u8 idx_auto;
  1246. } blacklist[] = {
  1247. { 2, 11 }, /* Hue */
  1248. { 6, 12 }, /* White Balance Temperature */
  1249. { 7, 13 }, /* White Balance Component */
  1250. };
  1251. u8 *controls;
  1252. unsigned int size;
  1253. unsigned int i;
  1254. if (UVC_ENTITY_TYPE(entity) != VC_PROCESSING_UNIT)
  1255. return;
  1256. controls = entity->processing.bmControls;
  1257. size = entity->processing.bControlSize;
  1258. for (i = 0; i < ARRAY_SIZE(blacklist); ++i) {
  1259. if (blacklist[i].idx_auto >= 8 * size ||
  1260. blacklist[i].idx_manual >= 8 * size)
  1261. continue;
  1262. if (!uvc_test_bit(controls, blacklist[i].idx_auto) ||
  1263. uvc_test_bit(controls, blacklist[i].idx_manual))
  1264. continue;
  1265. uvc_trace(UVC_TRACE_CONTROL, "Auto control %u/%u has no "
  1266. "matching manual control, removing it.\n", entity->id,
  1267. blacklist[i].idx_auto);
  1268. uvc_clear_bit(controls, blacklist[i].idx_auto);
  1269. }
  1270. }
  1271. /*
  1272. * Initialize device controls.
  1273. */
  1274. int uvc_ctrl_init_device(struct uvc_device *dev)
  1275. {
  1276. struct uvc_control_info *info;
  1277. struct uvc_control *ctrl;
  1278. struct uvc_entity *entity;
  1279. unsigned int i;
  1280. /* Walk the entities list and instantiate controls */
  1281. list_for_each_entry(entity, &dev->entities, list) {
  1282. unsigned int bControlSize = 0, ncontrols = 0;
  1283. __u8 *bmControls = NULL;
  1284. if (UVC_ENTITY_TYPE(entity) == VC_EXTENSION_UNIT) {
  1285. bmControls = entity->extension.bmControls;
  1286. bControlSize = entity->extension.bControlSize;
  1287. } else if (UVC_ENTITY_TYPE(entity) == VC_PROCESSING_UNIT) {
  1288. bmControls = entity->processing.bmControls;
  1289. bControlSize = entity->processing.bControlSize;
  1290. } else if (UVC_ENTITY_TYPE(entity) == ITT_CAMERA) {
  1291. bmControls = entity->camera.bmControls;
  1292. bControlSize = entity->camera.bControlSize;
  1293. }
  1294. if (dev->quirks & UVC_QUIRK_PRUNE_CONTROLS)
  1295. uvc_ctrl_prune_entity(entity);
  1296. for (i = 0; i < bControlSize; ++i)
  1297. ncontrols += hweight8(bmControls[i]);
  1298. if (ncontrols == 0)
  1299. continue;
  1300. entity->controls = kzalloc(ncontrols*sizeof *ctrl, GFP_KERNEL);
  1301. if (entity->controls == NULL)
  1302. return -ENOMEM;
  1303. entity->ncontrols = ncontrols;
  1304. ctrl = entity->controls;
  1305. for (i = 0; i < bControlSize * 8; ++i) {
  1306. if (uvc_test_bit(bmControls, i) == 0)
  1307. continue;
  1308. ctrl->entity = entity;
  1309. ctrl->index = i;
  1310. ctrl++;
  1311. }
  1312. }
  1313. /* Walk the controls info list and associate them with the device
  1314. * controls, then add the device to the global device list. This has
  1315. * to be done while holding the controls lock, to make sure
  1316. * uvc_ctrl_add_info() will not get called in-between.
  1317. */
  1318. mutex_lock(&uvc_driver.ctrl_mutex);
  1319. list_for_each_entry(info, &uvc_driver.controls, list)
  1320. uvc_ctrl_add_ctrl(dev, info);
  1321. list_add_tail(&dev->list, &uvc_driver.devices);
  1322. mutex_unlock(&uvc_driver.ctrl_mutex);
  1323. return 0;
  1324. }
  1325. /*
  1326. * Cleanup device controls.
  1327. */
  1328. void uvc_ctrl_cleanup_device(struct uvc_device *dev)
  1329. {
  1330. struct uvc_entity *entity;
  1331. unsigned int i;
  1332. /* Remove the device from the global devices list */
  1333. mutex_lock(&uvc_driver.ctrl_mutex);
  1334. if (dev->list.next != NULL)
  1335. list_del(&dev->list);
  1336. mutex_unlock(&uvc_driver.ctrl_mutex);
  1337. list_for_each_entry(entity, &dev->entities, list) {
  1338. for (i = 0; i < entity->ncontrols; ++i)
  1339. kfree(entity->controls[i].data);
  1340. kfree(entity->controls);
  1341. }
  1342. }
  1343. void uvc_ctrl_init(void)
  1344. {
  1345. struct uvc_control_info *ctrl = uvc_ctrls;
  1346. struct uvc_control_info *cend = ctrl + ARRAY_SIZE(uvc_ctrls);
  1347. struct uvc_control_mapping *mapping = uvc_ctrl_mappings;
  1348. struct uvc_control_mapping *mend =
  1349. mapping + ARRAY_SIZE(uvc_ctrl_mappings);
  1350. for (; ctrl < cend; ++ctrl)
  1351. uvc_ctrl_add_info(ctrl);
  1352. for (; mapping < mend; ++mapping)
  1353. uvc_ctrl_add_mapping(mapping);
  1354. }