uvc_ctrl.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171
  1. /*
  2. * uvc_ctrl.c -- USB Video Class driver - Controls
  3. *
  4. * Copyright (C) 2005-2010
  5. * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  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/list.h>
  15. #include <linux/module.h>
  16. #include <linux/slab.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 <linux/atomic.h>
  23. #include <media/v4l2-ctrls.h>
  24. #include "uvcvideo.h"
  25. #define UVC_CTRL_DATA_CURRENT 0
  26. #define UVC_CTRL_DATA_BACKUP 1
  27. #define UVC_CTRL_DATA_MIN 2
  28. #define UVC_CTRL_DATA_MAX 3
  29. #define UVC_CTRL_DATA_RES 4
  30. #define UVC_CTRL_DATA_DEF 5
  31. #define UVC_CTRL_DATA_LAST 6
  32. /* ------------------------------------------------------------------------
  33. * Controls
  34. */
  35. static struct uvc_control_info uvc_ctrls[] = {
  36. {
  37. .entity = UVC_GUID_UVC_PROCESSING,
  38. .selector = UVC_PU_BRIGHTNESS_CONTROL,
  39. .index = 0,
  40. .size = 2,
  41. .flags = UVC_CTRL_FLAG_SET_CUR
  42. | UVC_CTRL_FLAG_GET_RANGE
  43. | UVC_CTRL_FLAG_RESTORE,
  44. },
  45. {
  46. .entity = UVC_GUID_UVC_PROCESSING,
  47. .selector = UVC_PU_CONTRAST_CONTROL,
  48. .index = 1,
  49. .size = 2,
  50. .flags = UVC_CTRL_FLAG_SET_CUR
  51. | UVC_CTRL_FLAG_GET_RANGE
  52. | UVC_CTRL_FLAG_RESTORE,
  53. },
  54. {
  55. .entity = UVC_GUID_UVC_PROCESSING,
  56. .selector = UVC_PU_HUE_CONTROL,
  57. .index = 2,
  58. .size = 2,
  59. .flags = UVC_CTRL_FLAG_SET_CUR
  60. | UVC_CTRL_FLAG_GET_RANGE
  61. | UVC_CTRL_FLAG_RESTORE
  62. | UVC_CTRL_FLAG_AUTO_UPDATE,
  63. },
  64. {
  65. .entity = UVC_GUID_UVC_PROCESSING,
  66. .selector = UVC_PU_SATURATION_CONTROL,
  67. .index = 3,
  68. .size = 2,
  69. .flags = UVC_CTRL_FLAG_SET_CUR
  70. | UVC_CTRL_FLAG_GET_RANGE
  71. | UVC_CTRL_FLAG_RESTORE,
  72. },
  73. {
  74. .entity = UVC_GUID_UVC_PROCESSING,
  75. .selector = UVC_PU_SHARPNESS_CONTROL,
  76. .index = 4,
  77. .size = 2,
  78. .flags = UVC_CTRL_FLAG_SET_CUR
  79. | UVC_CTRL_FLAG_GET_RANGE
  80. | UVC_CTRL_FLAG_RESTORE,
  81. },
  82. {
  83. .entity = UVC_GUID_UVC_PROCESSING,
  84. .selector = UVC_PU_GAMMA_CONTROL,
  85. .index = 5,
  86. .size = 2,
  87. .flags = UVC_CTRL_FLAG_SET_CUR
  88. | UVC_CTRL_FLAG_GET_RANGE
  89. | UVC_CTRL_FLAG_RESTORE,
  90. },
  91. {
  92. .entity = UVC_GUID_UVC_PROCESSING,
  93. .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
  94. .index = 6,
  95. .size = 2,
  96. .flags = UVC_CTRL_FLAG_SET_CUR
  97. | UVC_CTRL_FLAG_GET_RANGE
  98. | UVC_CTRL_FLAG_RESTORE
  99. | UVC_CTRL_FLAG_AUTO_UPDATE,
  100. },
  101. {
  102. .entity = UVC_GUID_UVC_PROCESSING,
  103. .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
  104. .index = 7,
  105. .size = 4,
  106. .flags = UVC_CTRL_FLAG_SET_CUR
  107. | UVC_CTRL_FLAG_GET_RANGE
  108. | UVC_CTRL_FLAG_RESTORE
  109. | UVC_CTRL_FLAG_AUTO_UPDATE,
  110. },
  111. {
  112. .entity = UVC_GUID_UVC_PROCESSING,
  113. .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
  114. .index = 8,
  115. .size = 2,
  116. .flags = UVC_CTRL_FLAG_SET_CUR
  117. | UVC_CTRL_FLAG_GET_RANGE
  118. | UVC_CTRL_FLAG_RESTORE,
  119. },
  120. {
  121. .entity = UVC_GUID_UVC_PROCESSING,
  122. .selector = UVC_PU_GAIN_CONTROL,
  123. .index = 9,
  124. .size = 2,
  125. .flags = UVC_CTRL_FLAG_SET_CUR
  126. | UVC_CTRL_FLAG_GET_RANGE
  127. | UVC_CTRL_FLAG_RESTORE,
  128. },
  129. {
  130. .entity = UVC_GUID_UVC_PROCESSING,
  131. .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
  132. .index = 10,
  133. .size = 1,
  134. .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
  135. | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
  136. },
  137. {
  138. .entity = UVC_GUID_UVC_PROCESSING,
  139. .selector = UVC_PU_HUE_AUTO_CONTROL,
  140. .index = 11,
  141. .size = 1,
  142. .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
  143. | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
  144. },
  145. {
  146. .entity = UVC_GUID_UVC_PROCESSING,
  147. .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
  148. .index = 12,
  149. .size = 1,
  150. .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
  151. | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
  152. },
  153. {
  154. .entity = UVC_GUID_UVC_PROCESSING,
  155. .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
  156. .index = 13,
  157. .size = 1,
  158. .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
  159. | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
  160. },
  161. {
  162. .entity = UVC_GUID_UVC_PROCESSING,
  163. .selector = UVC_PU_DIGITAL_MULTIPLIER_CONTROL,
  164. .index = 14,
  165. .size = 2,
  166. .flags = UVC_CTRL_FLAG_SET_CUR
  167. | UVC_CTRL_FLAG_GET_RANGE
  168. | UVC_CTRL_FLAG_RESTORE,
  169. },
  170. {
  171. .entity = UVC_GUID_UVC_PROCESSING,
  172. .selector = UVC_PU_DIGITAL_MULTIPLIER_LIMIT_CONTROL,
  173. .index = 15,
  174. .size = 2,
  175. .flags = UVC_CTRL_FLAG_SET_CUR
  176. | UVC_CTRL_FLAG_GET_RANGE
  177. | UVC_CTRL_FLAG_RESTORE,
  178. },
  179. {
  180. .entity = UVC_GUID_UVC_PROCESSING,
  181. .selector = UVC_PU_ANALOG_VIDEO_STANDARD_CONTROL,
  182. .index = 16,
  183. .size = 1,
  184. .flags = UVC_CTRL_FLAG_GET_CUR,
  185. },
  186. {
  187. .entity = UVC_GUID_UVC_PROCESSING,
  188. .selector = UVC_PU_ANALOG_LOCK_STATUS_CONTROL,
  189. .index = 17,
  190. .size = 1,
  191. .flags = UVC_CTRL_FLAG_GET_CUR,
  192. },
  193. {
  194. .entity = UVC_GUID_UVC_CAMERA,
  195. .selector = UVC_CT_SCANNING_MODE_CONTROL,
  196. .index = 0,
  197. .size = 1,
  198. .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
  199. | UVC_CTRL_FLAG_RESTORE,
  200. },
  201. {
  202. .entity = UVC_GUID_UVC_CAMERA,
  203. .selector = UVC_CT_AE_MODE_CONTROL,
  204. .index = 1,
  205. .size = 1,
  206. .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
  207. | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_GET_RES
  208. | UVC_CTRL_FLAG_RESTORE,
  209. },
  210. {
  211. .entity = UVC_GUID_UVC_CAMERA,
  212. .selector = UVC_CT_AE_PRIORITY_CONTROL,
  213. .index = 2,
  214. .size = 1,
  215. .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
  216. | UVC_CTRL_FLAG_RESTORE,
  217. },
  218. {
  219. .entity = UVC_GUID_UVC_CAMERA,
  220. .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
  221. .index = 3,
  222. .size = 4,
  223. .flags = UVC_CTRL_FLAG_SET_CUR
  224. | UVC_CTRL_FLAG_GET_RANGE
  225. | UVC_CTRL_FLAG_RESTORE,
  226. },
  227. {
  228. .entity = UVC_GUID_UVC_CAMERA,
  229. .selector = UVC_CT_EXPOSURE_TIME_RELATIVE_CONTROL,
  230. .index = 4,
  231. .size = 1,
  232. .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_RESTORE,
  233. },
  234. {
  235. .entity = UVC_GUID_UVC_CAMERA,
  236. .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
  237. .index = 5,
  238. .size = 2,
  239. .flags = UVC_CTRL_FLAG_SET_CUR
  240. | UVC_CTRL_FLAG_GET_RANGE
  241. | UVC_CTRL_FLAG_RESTORE
  242. | UVC_CTRL_FLAG_AUTO_UPDATE,
  243. },
  244. {
  245. .entity = UVC_GUID_UVC_CAMERA,
  246. .selector = UVC_CT_FOCUS_RELATIVE_CONTROL,
  247. .index = 6,
  248. .size = 2,
  249. .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
  250. | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
  251. | UVC_CTRL_FLAG_GET_DEF
  252. | UVC_CTRL_FLAG_AUTO_UPDATE,
  253. },
  254. {
  255. .entity = UVC_GUID_UVC_CAMERA,
  256. .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL,
  257. .index = 7,
  258. .size = 2,
  259. .flags = UVC_CTRL_FLAG_SET_CUR
  260. | UVC_CTRL_FLAG_GET_RANGE
  261. | UVC_CTRL_FLAG_RESTORE
  262. | UVC_CTRL_FLAG_AUTO_UPDATE,
  263. },
  264. {
  265. .entity = UVC_GUID_UVC_CAMERA,
  266. .selector = UVC_CT_IRIS_RELATIVE_CONTROL,
  267. .index = 8,
  268. .size = 1,
  269. .flags = UVC_CTRL_FLAG_SET_CUR
  270. | UVC_CTRL_FLAG_AUTO_UPDATE,
  271. },
  272. {
  273. .entity = UVC_GUID_UVC_CAMERA,
  274. .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
  275. .index = 9,
  276. .size = 2,
  277. .flags = UVC_CTRL_FLAG_SET_CUR
  278. | UVC_CTRL_FLAG_GET_RANGE
  279. | UVC_CTRL_FLAG_RESTORE
  280. | UVC_CTRL_FLAG_AUTO_UPDATE,
  281. },
  282. {
  283. .entity = UVC_GUID_UVC_CAMERA,
  284. .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
  285. .index = 10,
  286. .size = 3,
  287. .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
  288. | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
  289. | UVC_CTRL_FLAG_GET_DEF
  290. | UVC_CTRL_FLAG_AUTO_UPDATE,
  291. },
  292. {
  293. .entity = UVC_GUID_UVC_CAMERA,
  294. .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
  295. .index = 11,
  296. .size = 8,
  297. .flags = UVC_CTRL_FLAG_SET_CUR
  298. | UVC_CTRL_FLAG_GET_RANGE
  299. | UVC_CTRL_FLAG_RESTORE
  300. | UVC_CTRL_FLAG_AUTO_UPDATE,
  301. },
  302. {
  303. .entity = UVC_GUID_UVC_CAMERA,
  304. .selector = UVC_CT_PANTILT_RELATIVE_CONTROL,
  305. .index = 12,
  306. .size = 4,
  307. .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
  308. | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
  309. | UVC_CTRL_FLAG_GET_DEF
  310. | UVC_CTRL_FLAG_AUTO_UPDATE,
  311. },
  312. {
  313. .entity = UVC_GUID_UVC_CAMERA,
  314. .selector = UVC_CT_ROLL_ABSOLUTE_CONTROL,
  315. .index = 13,
  316. .size = 2,
  317. .flags = UVC_CTRL_FLAG_SET_CUR
  318. | UVC_CTRL_FLAG_GET_RANGE
  319. | UVC_CTRL_FLAG_RESTORE
  320. | UVC_CTRL_FLAG_AUTO_UPDATE,
  321. },
  322. {
  323. .entity = UVC_GUID_UVC_CAMERA,
  324. .selector = UVC_CT_ROLL_RELATIVE_CONTROL,
  325. .index = 14,
  326. .size = 2,
  327. .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
  328. | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
  329. | UVC_CTRL_FLAG_GET_DEF
  330. | UVC_CTRL_FLAG_AUTO_UPDATE,
  331. },
  332. {
  333. .entity = UVC_GUID_UVC_CAMERA,
  334. .selector = UVC_CT_FOCUS_AUTO_CONTROL,
  335. .index = 17,
  336. .size = 1,
  337. .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
  338. | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
  339. },
  340. {
  341. .entity = UVC_GUID_UVC_CAMERA,
  342. .selector = UVC_CT_PRIVACY_CONTROL,
  343. .index = 18,
  344. .size = 1,
  345. .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
  346. | UVC_CTRL_FLAG_RESTORE
  347. | UVC_CTRL_FLAG_AUTO_UPDATE,
  348. },
  349. };
  350. static struct uvc_menu_info power_line_frequency_controls[] = {
  351. { 0, "Disabled" },
  352. { 1, "50 Hz" },
  353. { 2, "60 Hz" },
  354. };
  355. static struct uvc_menu_info exposure_auto_controls[] = {
  356. { 2, "Auto Mode" },
  357. { 1, "Manual Mode" },
  358. { 4, "Shutter Priority Mode" },
  359. { 8, "Aperture Priority Mode" },
  360. };
  361. static __s32 uvc_ctrl_get_zoom(struct uvc_control_mapping *mapping,
  362. __u8 query, const __u8 *data)
  363. {
  364. __s8 zoom = (__s8)data[0];
  365. switch (query) {
  366. case UVC_GET_CUR:
  367. return (zoom == 0) ? 0 : (zoom > 0 ? data[2] : -data[2]);
  368. case UVC_GET_MIN:
  369. case UVC_GET_MAX:
  370. case UVC_GET_RES:
  371. case UVC_GET_DEF:
  372. default:
  373. return data[2];
  374. }
  375. }
  376. static void uvc_ctrl_set_zoom(struct uvc_control_mapping *mapping,
  377. __s32 value, __u8 *data)
  378. {
  379. data[0] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
  380. data[2] = min((int)abs(value), 0xff);
  381. }
  382. static struct uvc_control_mapping uvc_ctrl_mappings[] = {
  383. {
  384. .id = V4L2_CID_BRIGHTNESS,
  385. .name = "Brightness",
  386. .entity = UVC_GUID_UVC_PROCESSING,
  387. .selector = UVC_PU_BRIGHTNESS_CONTROL,
  388. .size = 16,
  389. .offset = 0,
  390. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  391. .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
  392. },
  393. {
  394. .id = V4L2_CID_CONTRAST,
  395. .name = "Contrast",
  396. .entity = UVC_GUID_UVC_PROCESSING,
  397. .selector = UVC_PU_CONTRAST_CONTROL,
  398. .size = 16,
  399. .offset = 0,
  400. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  401. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  402. },
  403. {
  404. .id = V4L2_CID_HUE,
  405. .name = "Hue",
  406. .entity = UVC_GUID_UVC_PROCESSING,
  407. .selector = UVC_PU_HUE_CONTROL,
  408. .size = 16,
  409. .offset = 0,
  410. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  411. .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
  412. .master_id = V4L2_CID_HUE_AUTO,
  413. .master_manual = 0,
  414. },
  415. {
  416. .id = V4L2_CID_SATURATION,
  417. .name = "Saturation",
  418. .entity = UVC_GUID_UVC_PROCESSING,
  419. .selector = UVC_PU_SATURATION_CONTROL,
  420. .size = 16,
  421. .offset = 0,
  422. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  423. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  424. },
  425. {
  426. .id = V4L2_CID_SHARPNESS,
  427. .name = "Sharpness",
  428. .entity = UVC_GUID_UVC_PROCESSING,
  429. .selector = UVC_PU_SHARPNESS_CONTROL,
  430. .size = 16,
  431. .offset = 0,
  432. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  433. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  434. },
  435. {
  436. .id = V4L2_CID_GAMMA,
  437. .name = "Gamma",
  438. .entity = UVC_GUID_UVC_PROCESSING,
  439. .selector = UVC_PU_GAMMA_CONTROL,
  440. .size = 16,
  441. .offset = 0,
  442. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  443. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  444. },
  445. {
  446. .id = V4L2_CID_BACKLIGHT_COMPENSATION,
  447. .name = "Backlight Compensation",
  448. .entity = UVC_GUID_UVC_PROCESSING,
  449. .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
  450. .size = 16,
  451. .offset = 0,
  452. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  453. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  454. },
  455. {
  456. .id = V4L2_CID_GAIN,
  457. .name = "Gain",
  458. .entity = UVC_GUID_UVC_PROCESSING,
  459. .selector = UVC_PU_GAIN_CONTROL,
  460. .size = 16,
  461. .offset = 0,
  462. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  463. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  464. },
  465. {
  466. .id = V4L2_CID_POWER_LINE_FREQUENCY,
  467. .name = "Power Line Frequency",
  468. .entity = UVC_GUID_UVC_PROCESSING,
  469. .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
  470. .size = 2,
  471. .offset = 0,
  472. .v4l2_type = V4L2_CTRL_TYPE_MENU,
  473. .data_type = UVC_CTRL_DATA_TYPE_ENUM,
  474. .menu_info = power_line_frequency_controls,
  475. .menu_count = ARRAY_SIZE(power_line_frequency_controls),
  476. },
  477. {
  478. .id = V4L2_CID_HUE_AUTO,
  479. .name = "Hue, Auto",
  480. .entity = UVC_GUID_UVC_PROCESSING,
  481. .selector = UVC_PU_HUE_AUTO_CONTROL,
  482. .size = 1,
  483. .offset = 0,
  484. .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
  485. .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
  486. .slave_ids = { V4L2_CID_HUE, },
  487. },
  488. {
  489. .id = V4L2_CID_EXPOSURE_AUTO,
  490. .name = "Exposure, Auto",
  491. .entity = UVC_GUID_UVC_CAMERA,
  492. .selector = UVC_CT_AE_MODE_CONTROL,
  493. .size = 4,
  494. .offset = 0,
  495. .v4l2_type = V4L2_CTRL_TYPE_MENU,
  496. .data_type = UVC_CTRL_DATA_TYPE_BITMASK,
  497. .menu_info = exposure_auto_controls,
  498. .menu_count = ARRAY_SIZE(exposure_auto_controls),
  499. .slave_ids = { V4L2_CID_EXPOSURE_ABSOLUTE, },
  500. },
  501. {
  502. .id = V4L2_CID_EXPOSURE_AUTO_PRIORITY,
  503. .name = "Exposure, Auto Priority",
  504. .entity = UVC_GUID_UVC_CAMERA,
  505. .selector = UVC_CT_AE_PRIORITY_CONTROL,
  506. .size = 1,
  507. .offset = 0,
  508. .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
  509. .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
  510. },
  511. {
  512. .id = V4L2_CID_EXPOSURE_ABSOLUTE,
  513. .name = "Exposure (Absolute)",
  514. .entity = UVC_GUID_UVC_CAMERA,
  515. .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
  516. .size = 32,
  517. .offset = 0,
  518. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  519. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  520. .master_id = V4L2_CID_EXPOSURE_AUTO,
  521. .master_manual = V4L2_EXPOSURE_MANUAL,
  522. },
  523. {
  524. .id = V4L2_CID_AUTO_WHITE_BALANCE,
  525. .name = "White Balance Temperature, Auto",
  526. .entity = UVC_GUID_UVC_PROCESSING,
  527. .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
  528. .size = 1,
  529. .offset = 0,
  530. .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
  531. .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
  532. .slave_ids = { V4L2_CID_WHITE_BALANCE_TEMPERATURE, },
  533. },
  534. {
  535. .id = V4L2_CID_WHITE_BALANCE_TEMPERATURE,
  536. .name = "White Balance Temperature",
  537. .entity = UVC_GUID_UVC_PROCESSING,
  538. .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
  539. .size = 16,
  540. .offset = 0,
  541. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  542. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  543. .master_id = V4L2_CID_AUTO_WHITE_BALANCE,
  544. .master_manual = 0,
  545. },
  546. {
  547. .id = V4L2_CID_AUTO_WHITE_BALANCE,
  548. .name = "White Balance Component, Auto",
  549. .entity = UVC_GUID_UVC_PROCESSING,
  550. .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
  551. .size = 1,
  552. .offset = 0,
  553. .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
  554. .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
  555. .slave_ids = { V4L2_CID_BLUE_BALANCE,
  556. V4L2_CID_RED_BALANCE },
  557. },
  558. {
  559. .id = V4L2_CID_BLUE_BALANCE,
  560. .name = "White Balance Blue Component",
  561. .entity = UVC_GUID_UVC_PROCESSING,
  562. .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
  563. .size = 16,
  564. .offset = 0,
  565. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  566. .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
  567. .master_id = V4L2_CID_AUTO_WHITE_BALANCE,
  568. .master_manual = 0,
  569. },
  570. {
  571. .id = V4L2_CID_RED_BALANCE,
  572. .name = "White Balance Red Component",
  573. .entity = UVC_GUID_UVC_PROCESSING,
  574. .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
  575. .size = 16,
  576. .offset = 16,
  577. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  578. .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
  579. .master_id = V4L2_CID_AUTO_WHITE_BALANCE,
  580. .master_manual = 0,
  581. },
  582. {
  583. .id = V4L2_CID_FOCUS_ABSOLUTE,
  584. .name = "Focus (absolute)",
  585. .entity = UVC_GUID_UVC_CAMERA,
  586. .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
  587. .size = 16,
  588. .offset = 0,
  589. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  590. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  591. .master_id = V4L2_CID_FOCUS_AUTO,
  592. .master_manual = 0,
  593. },
  594. {
  595. .id = V4L2_CID_FOCUS_AUTO,
  596. .name = "Focus, Auto",
  597. .entity = UVC_GUID_UVC_CAMERA,
  598. .selector = UVC_CT_FOCUS_AUTO_CONTROL,
  599. .size = 1,
  600. .offset = 0,
  601. .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
  602. .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
  603. .slave_ids = { V4L2_CID_FOCUS_ABSOLUTE, },
  604. },
  605. {
  606. .id = V4L2_CID_IRIS_ABSOLUTE,
  607. .name = "Iris, Absolute",
  608. .entity = UVC_GUID_UVC_CAMERA,
  609. .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL,
  610. .size = 16,
  611. .offset = 0,
  612. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  613. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  614. },
  615. {
  616. .id = V4L2_CID_IRIS_RELATIVE,
  617. .name = "Iris, Relative",
  618. .entity = UVC_GUID_UVC_CAMERA,
  619. .selector = UVC_CT_IRIS_RELATIVE_CONTROL,
  620. .size = 8,
  621. .offset = 0,
  622. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  623. .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
  624. },
  625. {
  626. .id = V4L2_CID_ZOOM_ABSOLUTE,
  627. .name = "Zoom, Absolute",
  628. .entity = UVC_GUID_UVC_CAMERA,
  629. .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
  630. .size = 16,
  631. .offset = 0,
  632. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  633. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  634. },
  635. {
  636. .id = V4L2_CID_ZOOM_CONTINUOUS,
  637. .name = "Zoom, Continuous",
  638. .entity = UVC_GUID_UVC_CAMERA,
  639. .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
  640. .size = 0,
  641. .offset = 0,
  642. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  643. .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
  644. .get = uvc_ctrl_get_zoom,
  645. .set = uvc_ctrl_set_zoom,
  646. },
  647. {
  648. .id = V4L2_CID_PAN_ABSOLUTE,
  649. .name = "Pan (Absolute)",
  650. .entity = UVC_GUID_UVC_CAMERA,
  651. .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
  652. .size = 32,
  653. .offset = 0,
  654. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  655. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  656. },
  657. {
  658. .id = V4L2_CID_TILT_ABSOLUTE,
  659. .name = "Tilt (Absolute)",
  660. .entity = UVC_GUID_UVC_CAMERA,
  661. .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
  662. .size = 32,
  663. .offset = 32,
  664. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  665. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  666. },
  667. {
  668. .id = V4L2_CID_PRIVACY,
  669. .name = "Privacy",
  670. .entity = UVC_GUID_UVC_CAMERA,
  671. .selector = UVC_CT_PRIVACY_CONTROL,
  672. .size = 1,
  673. .offset = 0,
  674. .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
  675. .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
  676. },
  677. };
  678. /* ------------------------------------------------------------------------
  679. * Utility functions
  680. */
  681. static inline __u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id)
  682. {
  683. return ctrl->uvc_data + id * ctrl->info.size;
  684. }
  685. static inline int uvc_test_bit(const __u8 *data, int bit)
  686. {
  687. return (data[bit >> 3] >> (bit & 7)) & 1;
  688. }
  689. static inline void uvc_clear_bit(__u8 *data, int bit)
  690. {
  691. data[bit >> 3] &= ~(1 << (bit & 7));
  692. }
  693. /* Extract the bit string specified by mapping->offset and mapping->size
  694. * from the little-endian data stored at 'data' and return the result as
  695. * a signed 32bit integer. Sign extension will be performed if the mapping
  696. * references a signed data type.
  697. */
  698. static __s32 uvc_get_le_value(struct uvc_control_mapping *mapping,
  699. __u8 query, const __u8 *data)
  700. {
  701. int bits = mapping->size;
  702. int offset = mapping->offset;
  703. __s32 value = 0;
  704. __u8 mask;
  705. data += offset / 8;
  706. offset &= 7;
  707. mask = ((1LL << bits) - 1) << offset;
  708. for (; bits > 0; data++) {
  709. __u8 byte = *data & mask;
  710. value |= offset > 0 ? (byte >> offset) : (byte << (-offset));
  711. bits -= 8 - (offset > 0 ? offset : 0);
  712. offset -= 8;
  713. mask = (1 << bits) - 1;
  714. }
  715. /* Sign-extend the value if needed. */
  716. if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
  717. value |= -(value & (1 << (mapping->size - 1)));
  718. return value;
  719. }
  720. /* Set the bit string specified by mapping->offset and mapping->size
  721. * in the little-endian data stored at 'data' to the value 'value'.
  722. */
  723. static void uvc_set_le_value(struct uvc_control_mapping *mapping,
  724. __s32 value, __u8 *data)
  725. {
  726. int bits = mapping->size;
  727. int offset = mapping->offset;
  728. __u8 mask;
  729. /* According to the v4l2 spec, writing any value to a button control
  730. * should result in the action belonging to the button control being
  731. * triggered. UVC devices however want to see a 1 written -> override
  732. * value.
  733. */
  734. if (mapping->v4l2_type == V4L2_CTRL_TYPE_BUTTON)
  735. value = -1;
  736. data += offset / 8;
  737. offset &= 7;
  738. for (; bits > 0; data++) {
  739. mask = ((1LL << bits) - 1) << offset;
  740. *data = (*data & ~mask) | ((value << offset) & mask);
  741. value >>= offset ? offset : 8;
  742. bits -= 8 - offset;
  743. offset = 0;
  744. }
  745. }
  746. /* ------------------------------------------------------------------------
  747. * Terminal and unit management
  748. */
  749. static const __u8 uvc_processing_guid[16] = UVC_GUID_UVC_PROCESSING;
  750. static const __u8 uvc_camera_guid[16] = UVC_GUID_UVC_CAMERA;
  751. static const __u8 uvc_media_transport_input_guid[16] =
  752. UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT;
  753. static int uvc_entity_match_guid(const struct uvc_entity *entity,
  754. const __u8 guid[16])
  755. {
  756. switch (UVC_ENTITY_TYPE(entity)) {
  757. case UVC_ITT_CAMERA:
  758. return memcmp(uvc_camera_guid, guid, 16) == 0;
  759. case UVC_ITT_MEDIA_TRANSPORT_INPUT:
  760. return memcmp(uvc_media_transport_input_guid, guid, 16) == 0;
  761. case UVC_VC_PROCESSING_UNIT:
  762. return memcmp(uvc_processing_guid, guid, 16) == 0;
  763. case UVC_VC_EXTENSION_UNIT:
  764. return memcmp(entity->extension.guidExtensionCode,
  765. guid, 16) == 0;
  766. default:
  767. return 0;
  768. }
  769. }
  770. /* ------------------------------------------------------------------------
  771. * UVC Controls
  772. */
  773. static void __uvc_find_control(struct uvc_entity *entity, __u32 v4l2_id,
  774. struct uvc_control_mapping **mapping, struct uvc_control **control,
  775. int next)
  776. {
  777. struct uvc_control *ctrl;
  778. struct uvc_control_mapping *map;
  779. unsigned int i;
  780. if (entity == NULL)
  781. return;
  782. for (i = 0; i < entity->ncontrols; ++i) {
  783. ctrl = &entity->controls[i];
  784. if (!ctrl->initialized)
  785. continue;
  786. list_for_each_entry(map, &ctrl->info.mappings, list) {
  787. if ((map->id == v4l2_id) && !next) {
  788. *control = ctrl;
  789. *mapping = map;
  790. return;
  791. }
  792. if ((*mapping == NULL || (*mapping)->id > map->id) &&
  793. (map->id > v4l2_id) && next) {
  794. *control = ctrl;
  795. *mapping = map;
  796. }
  797. }
  798. }
  799. }
  800. static struct uvc_control *uvc_find_control(struct uvc_video_chain *chain,
  801. __u32 v4l2_id, struct uvc_control_mapping **mapping)
  802. {
  803. struct uvc_control *ctrl = NULL;
  804. struct uvc_entity *entity;
  805. int next = v4l2_id & V4L2_CTRL_FLAG_NEXT_CTRL;
  806. *mapping = NULL;
  807. /* Mask the query flags. */
  808. v4l2_id &= V4L2_CTRL_ID_MASK;
  809. /* Find the control. */
  810. list_for_each_entry(entity, &chain->entities, chain) {
  811. __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next);
  812. if (ctrl && !next)
  813. return ctrl;
  814. }
  815. if (ctrl == NULL && !next)
  816. uvc_trace(UVC_TRACE_CONTROL, "Control 0x%08x not found.\n",
  817. v4l2_id);
  818. return ctrl;
  819. }
  820. static int uvc_ctrl_populate_cache(struct uvc_video_chain *chain,
  821. struct uvc_control *ctrl)
  822. {
  823. int ret;
  824. if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
  825. ret = uvc_query_ctrl(chain->dev, UVC_GET_DEF, ctrl->entity->id,
  826. chain->dev->intfnum, ctrl->info.selector,
  827. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF),
  828. ctrl->info.size);
  829. if (ret < 0)
  830. return ret;
  831. }
  832. if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN) {
  833. ret = uvc_query_ctrl(chain->dev, UVC_GET_MIN, ctrl->entity->id,
  834. chain->dev->intfnum, ctrl->info.selector,
  835. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN),
  836. ctrl->info.size);
  837. if (ret < 0)
  838. return ret;
  839. }
  840. if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX) {
  841. ret = uvc_query_ctrl(chain->dev, UVC_GET_MAX, ctrl->entity->id,
  842. chain->dev->intfnum, ctrl->info.selector,
  843. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX),
  844. ctrl->info.size);
  845. if (ret < 0)
  846. return ret;
  847. }
  848. if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES) {
  849. ret = uvc_query_ctrl(chain->dev, UVC_GET_RES, ctrl->entity->id,
  850. chain->dev->intfnum, ctrl->info.selector,
  851. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES),
  852. ctrl->info.size);
  853. if (ret < 0) {
  854. if (UVC_ENTITY_TYPE(ctrl->entity) !=
  855. UVC_VC_EXTENSION_UNIT)
  856. return ret;
  857. /* GET_RES is mandatory for XU controls, but some
  858. * cameras still choke on it. Ignore errors and set the
  859. * resolution value to zero.
  860. */
  861. uvc_warn_once(chain->dev, UVC_WARN_XU_GET_RES,
  862. "UVC non compliance - GET_RES failed on "
  863. "an XU control. Enabling workaround.\n");
  864. memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES), 0,
  865. ctrl->info.size);
  866. }
  867. }
  868. ctrl->cached = 1;
  869. return 0;
  870. }
  871. static int __uvc_ctrl_get(struct uvc_video_chain *chain,
  872. struct uvc_control *ctrl, struct uvc_control_mapping *mapping,
  873. s32 *value)
  874. {
  875. struct uvc_menu_info *menu;
  876. unsigned int i;
  877. int ret;
  878. if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0)
  879. return -EACCES;
  880. if (!ctrl->loaded) {
  881. ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, ctrl->entity->id,
  882. chain->dev->intfnum, ctrl->info.selector,
  883. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  884. ctrl->info.size);
  885. if (ret < 0)
  886. return ret;
  887. ctrl->loaded = 1;
  888. }
  889. *value = mapping->get(mapping, UVC_GET_CUR,
  890. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
  891. if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
  892. menu = mapping->menu_info;
  893. for (i = 0; i < mapping->menu_count; ++i, ++menu) {
  894. if (menu->value == *value) {
  895. *value = i;
  896. break;
  897. }
  898. }
  899. }
  900. return 0;
  901. }
  902. static int __uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
  903. struct uvc_control *ctrl,
  904. struct uvc_control_mapping *mapping,
  905. struct v4l2_queryctrl *v4l2_ctrl)
  906. {
  907. struct uvc_control_mapping *master_map = NULL;
  908. struct uvc_control *master_ctrl = NULL;
  909. struct uvc_menu_info *menu;
  910. unsigned int i;
  911. memset(v4l2_ctrl, 0, sizeof *v4l2_ctrl);
  912. v4l2_ctrl->id = mapping->id;
  913. v4l2_ctrl->type = mapping->v4l2_type;
  914. strlcpy(v4l2_ctrl->name, mapping->name, sizeof v4l2_ctrl->name);
  915. v4l2_ctrl->flags = 0;
  916. if (!(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
  917. v4l2_ctrl->flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
  918. if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
  919. v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  920. if (mapping->master_id)
  921. __uvc_find_control(ctrl->entity, mapping->master_id,
  922. &master_map, &master_ctrl, 0);
  923. if (master_ctrl && (master_ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR)) {
  924. s32 val;
  925. int ret = __uvc_ctrl_get(chain, master_ctrl, master_map, &val);
  926. if (ret < 0)
  927. return ret;
  928. if (val != mapping->master_manual)
  929. v4l2_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
  930. }
  931. if (!ctrl->cached) {
  932. int ret = uvc_ctrl_populate_cache(chain, ctrl);
  933. if (ret < 0)
  934. return ret;
  935. }
  936. if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
  937. v4l2_ctrl->default_value = mapping->get(mapping, UVC_GET_DEF,
  938. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF));
  939. }
  940. switch (mapping->v4l2_type) {
  941. case V4L2_CTRL_TYPE_MENU:
  942. v4l2_ctrl->minimum = 0;
  943. v4l2_ctrl->maximum = mapping->menu_count - 1;
  944. v4l2_ctrl->step = 1;
  945. menu = mapping->menu_info;
  946. for (i = 0; i < mapping->menu_count; ++i, ++menu) {
  947. if (menu->value == v4l2_ctrl->default_value) {
  948. v4l2_ctrl->default_value = i;
  949. break;
  950. }
  951. }
  952. return 0;
  953. case V4L2_CTRL_TYPE_BOOLEAN:
  954. v4l2_ctrl->minimum = 0;
  955. v4l2_ctrl->maximum = 1;
  956. v4l2_ctrl->step = 1;
  957. return 0;
  958. case V4L2_CTRL_TYPE_BUTTON:
  959. v4l2_ctrl->minimum = 0;
  960. v4l2_ctrl->maximum = 0;
  961. v4l2_ctrl->step = 0;
  962. return 0;
  963. default:
  964. break;
  965. }
  966. if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN)
  967. v4l2_ctrl->minimum = mapping->get(mapping, UVC_GET_MIN,
  968. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
  969. if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX)
  970. v4l2_ctrl->maximum = mapping->get(mapping, UVC_GET_MAX,
  971. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
  972. if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)
  973. v4l2_ctrl->step = mapping->get(mapping, UVC_GET_RES,
  974. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
  975. return 0;
  976. }
  977. int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
  978. struct v4l2_queryctrl *v4l2_ctrl)
  979. {
  980. struct uvc_control *ctrl;
  981. struct uvc_control_mapping *mapping;
  982. int ret;
  983. ret = mutex_lock_interruptible(&chain->ctrl_mutex);
  984. if (ret < 0)
  985. return -ERESTARTSYS;
  986. ctrl = uvc_find_control(chain, v4l2_ctrl->id, &mapping);
  987. if (ctrl == NULL) {
  988. ret = -EINVAL;
  989. goto done;
  990. }
  991. ret = __uvc_query_v4l2_ctrl(chain, ctrl, mapping, v4l2_ctrl);
  992. done:
  993. mutex_unlock(&chain->ctrl_mutex);
  994. return ret;
  995. }
  996. /*
  997. * Mapping V4L2 controls to UVC controls can be straighforward if done well.
  998. * Most of the UVC controls exist in V4L2, and can be mapped directly. Some
  999. * must be grouped (for instance the Red Balance, Blue Balance and Do White
  1000. * Balance V4L2 controls use the White Balance Component UVC control) or
  1001. * otherwise translated. The approach we take here is to use a translation
  1002. * table for the controls that can be mapped directly, and handle the others
  1003. * manually.
  1004. */
  1005. int uvc_query_v4l2_menu(struct uvc_video_chain *chain,
  1006. struct v4l2_querymenu *query_menu)
  1007. {
  1008. struct uvc_menu_info *menu_info;
  1009. struct uvc_control_mapping *mapping;
  1010. struct uvc_control *ctrl;
  1011. u32 index = query_menu->index;
  1012. u32 id = query_menu->id;
  1013. int ret;
  1014. memset(query_menu, 0, sizeof(*query_menu));
  1015. query_menu->id = id;
  1016. query_menu->index = index;
  1017. ret = mutex_lock_interruptible(&chain->ctrl_mutex);
  1018. if (ret < 0)
  1019. return -ERESTARTSYS;
  1020. ctrl = uvc_find_control(chain, query_menu->id, &mapping);
  1021. if (ctrl == NULL || mapping->v4l2_type != V4L2_CTRL_TYPE_MENU) {
  1022. ret = -EINVAL;
  1023. goto done;
  1024. }
  1025. if (query_menu->index >= mapping->menu_count) {
  1026. ret = -EINVAL;
  1027. goto done;
  1028. }
  1029. menu_info = &mapping->menu_info[query_menu->index];
  1030. if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK &&
  1031. (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)) {
  1032. s32 bitmap;
  1033. if (!ctrl->cached) {
  1034. ret = uvc_ctrl_populate_cache(chain, ctrl);
  1035. if (ret < 0)
  1036. goto done;
  1037. }
  1038. bitmap = mapping->get(mapping, UVC_GET_RES,
  1039. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
  1040. if (!(bitmap & menu_info->value)) {
  1041. ret = -EINVAL;
  1042. goto done;
  1043. }
  1044. }
  1045. strlcpy(query_menu->name, menu_info->name, sizeof query_menu->name);
  1046. done:
  1047. mutex_unlock(&chain->ctrl_mutex);
  1048. return ret;
  1049. }
  1050. /* --------------------------------------------------------------------------
  1051. * Ctrl event handling
  1052. */
  1053. static void uvc_ctrl_fill_event(struct uvc_video_chain *chain,
  1054. struct v4l2_event *ev,
  1055. struct uvc_control *ctrl,
  1056. struct uvc_control_mapping *mapping,
  1057. s32 value, u32 changes)
  1058. {
  1059. struct v4l2_queryctrl v4l2_ctrl;
  1060. __uvc_query_v4l2_ctrl(chain, ctrl, mapping, &v4l2_ctrl);
  1061. memset(ev->reserved, 0, sizeof(ev->reserved));
  1062. ev->type = V4L2_EVENT_CTRL;
  1063. ev->id = v4l2_ctrl.id;
  1064. ev->u.ctrl.value = value;
  1065. ev->u.ctrl.changes = changes;
  1066. ev->u.ctrl.type = v4l2_ctrl.type;
  1067. ev->u.ctrl.flags = v4l2_ctrl.flags;
  1068. ev->u.ctrl.minimum = v4l2_ctrl.minimum;
  1069. ev->u.ctrl.maximum = v4l2_ctrl.maximum;
  1070. ev->u.ctrl.step = v4l2_ctrl.step;
  1071. ev->u.ctrl.default_value = v4l2_ctrl.default_value;
  1072. }
  1073. static void uvc_ctrl_send_event(struct uvc_fh *handle,
  1074. struct uvc_control *ctrl, struct uvc_control_mapping *mapping,
  1075. s32 value, u32 changes)
  1076. {
  1077. struct v4l2_subscribed_event *sev;
  1078. struct v4l2_event ev;
  1079. if (list_empty(&mapping->ev_subs))
  1080. return;
  1081. uvc_ctrl_fill_event(handle->chain, &ev, ctrl, mapping, value, changes);
  1082. list_for_each_entry(sev, &mapping->ev_subs, node) {
  1083. if (sev->fh && (sev->fh != &handle->vfh ||
  1084. (sev->flags & V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK) ||
  1085. (changes & V4L2_EVENT_CTRL_CH_FLAGS)))
  1086. v4l2_event_queue_fh(sev->fh, &ev);
  1087. }
  1088. }
  1089. static void uvc_ctrl_send_slave_event(struct uvc_fh *handle,
  1090. struct uvc_control *master, u32 slave_id,
  1091. const struct v4l2_ext_control *xctrls, unsigned int xctrls_count)
  1092. {
  1093. struct uvc_control_mapping *mapping = NULL;
  1094. struct uvc_control *ctrl = NULL;
  1095. u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
  1096. unsigned int i;
  1097. s32 val = 0;
  1098. /*
  1099. * We can skip sending an event for the slave if the slave
  1100. * is being modified in the same transaction.
  1101. */
  1102. for (i = 0; i < xctrls_count; i++) {
  1103. if (xctrls[i].id == slave_id)
  1104. return;
  1105. }
  1106. __uvc_find_control(master->entity, slave_id, &mapping, &ctrl, 0);
  1107. if (ctrl == NULL)
  1108. return;
  1109. if (__uvc_ctrl_get(handle->chain, ctrl, mapping, &val) == 0)
  1110. changes |= V4L2_EVENT_CTRL_CH_VALUE;
  1111. uvc_ctrl_send_event(handle, ctrl, mapping, val, changes);
  1112. }
  1113. static void uvc_ctrl_send_events(struct uvc_fh *handle,
  1114. const struct v4l2_ext_control *xctrls, unsigned int xctrls_count)
  1115. {
  1116. struct uvc_control_mapping *mapping;
  1117. struct uvc_control *ctrl;
  1118. u32 changes = V4L2_EVENT_CTRL_CH_VALUE;
  1119. unsigned int i;
  1120. unsigned int j;
  1121. for (i = 0; i < xctrls_count; ++i) {
  1122. ctrl = uvc_find_control(handle->chain, xctrls[i].id, &mapping);
  1123. for (j = 0; j < ARRAY_SIZE(mapping->slave_ids); ++j) {
  1124. if (!mapping->slave_ids[j])
  1125. break;
  1126. uvc_ctrl_send_slave_event(handle, ctrl,
  1127. mapping->slave_ids[j],
  1128. xctrls, xctrls_count);
  1129. }
  1130. /*
  1131. * If the master is being modified in the same transaction
  1132. * flags may change too.
  1133. */
  1134. if (mapping->master_id) {
  1135. for (j = 0; j < xctrls_count; j++) {
  1136. if (xctrls[j].id == mapping->master_id) {
  1137. changes |= V4L2_EVENT_CTRL_CH_FLAGS;
  1138. break;
  1139. }
  1140. }
  1141. }
  1142. uvc_ctrl_send_event(handle, ctrl, mapping, xctrls[i].value,
  1143. changes);
  1144. }
  1145. }
  1146. static int uvc_ctrl_add_event(struct v4l2_subscribed_event *sev, unsigned elems)
  1147. {
  1148. struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);
  1149. struct uvc_control_mapping *mapping;
  1150. struct uvc_control *ctrl;
  1151. int ret;
  1152. ret = mutex_lock_interruptible(&handle->chain->ctrl_mutex);
  1153. if (ret < 0)
  1154. return -ERESTARTSYS;
  1155. ctrl = uvc_find_control(handle->chain, sev->id, &mapping);
  1156. if (ctrl == NULL) {
  1157. ret = -EINVAL;
  1158. goto done;
  1159. }
  1160. list_add_tail(&sev->node, &mapping->ev_subs);
  1161. if (sev->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL) {
  1162. struct v4l2_event ev;
  1163. u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
  1164. s32 val = 0;
  1165. if (__uvc_ctrl_get(handle->chain, ctrl, mapping, &val) == 0)
  1166. changes |= V4L2_EVENT_CTRL_CH_VALUE;
  1167. uvc_ctrl_fill_event(handle->chain, &ev, ctrl, mapping, val,
  1168. changes);
  1169. /* Mark the queue as active, allowing this initial
  1170. event to be accepted. */
  1171. sev->elems = elems;
  1172. v4l2_event_queue_fh(sev->fh, &ev);
  1173. }
  1174. done:
  1175. mutex_unlock(&handle->chain->ctrl_mutex);
  1176. return ret;
  1177. }
  1178. static void uvc_ctrl_del_event(struct v4l2_subscribed_event *sev)
  1179. {
  1180. struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);
  1181. mutex_lock(&handle->chain->ctrl_mutex);
  1182. list_del(&sev->node);
  1183. mutex_unlock(&handle->chain->ctrl_mutex);
  1184. }
  1185. const struct v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops = {
  1186. .add = uvc_ctrl_add_event,
  1187. .del = uvc_ctrl_del_event,
  1188. .replace = v4l2_ctrl_replace,
  1189. .merge = v4l2_ctrl_merge,
  1190. };
  1191. /* --------------------------------------------------------------------------
  1192. * Control transactions
  1193. *
  1194. * To make extended set operations as atomic as the hardware allows, controls
  1195. * are handled using begin/commit/rollback operations.
  1196. *
  1197. * At the beginning of a set request, uvc_ctrl_begin should be called to
  1198. * initialize the request. This function acquires the control lock.
  1199. *
  1200. * When setting a control, the new value is stored in the control data field
  1201. * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
  1202. * later processing. If the UVC and V4L2 control sizes differ, the current
  1203. * value is loaded from the hardware before storing the new value in the data
  1204. * field.
  1205. *
  1206. * After processing all controls in the transaction, uvc_ctrl_commit or
  1207. * uvc_ctrl_rollback must be called to apply the pending changes to the
  1208. * hardware or revert them. When applying changes, all controls marked as
  1209. * dirty will be modified in the UVC device, and the dirty flag will be
  1210. * cleared. When reverting controls, the control data field
  1211. * UVC_CTRL_DATA_CURRENT is reverted to its previous value
  1212. * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
  1213. * control lock.
  1214. */
  1215. int uvc_ctrl_begin(struct uvc_video_chain *chain)
  1216. {
  1217. return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0;
  1218. }
  1219. static int uvc_ctrl_commit_entity(struct uvc_device *dev,
  1220. struct uvc_entity *entity, int rollback)
  1221. {
  1222. struct uvc_control *ctrl;
  1223. unsigned int i;
  1224. int ret;
  1225. if (entity == NULL)
  1226. return 0;
  1227. for (i = 0; i < entity->ncontrols; ++i) {
  1228. ctrl = &entity->controls[i];
  1229. if (!ctrl->initialized)
  1230. continue;
  1231. /* Reset the loaded flag for auto-update controls that were
  1232. * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
  1233. * uvc_ctrl_get from using the cached value, and for write-only
  1234. * controls to prevent uvc_ctrl_set from setting bits not
  1235. * explicitly set by the user.
  1236. */
  1237. if (ctrl->info.flags & UVC_CTRL_FLAG_AUTO_UPDATE ||
  1238. !(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
  1239. ctrl->loaded = 0;
  1240. if (!ctrl->dirty)
  1241. continue;
  1242. if (!rollback)
  1243. ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id,
  1244. dev->intfnum, ctrl->info.selector,
  1245. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  1246. ctrl->info.size);
  1247. else
  1248. ret = 0;
  1249. if (rollback || ret < 0)
  1250. memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  1251. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
  1252. ctrl->info.size);
  1253. ctrl->dirty = 0;
  1254. if (ret < 0)
  1255. return ret;
  1256. }
  1257. return 0;
  1258. }
  1259. int __uvc_ctrl_commit(struct uvc_fh *handle, int rollback,
  1260. const struct v4l2_ext_control *xctrls,
  1261. unsigned int xctrls_count)
  1262. {
  1263. struct uvc_video_chain *chain = handle->chain;
  1264. struct uvc_entity *entity;
  1265. int ret = 0;
  1266. /* Find the control. */
  1267. list_for_each_entry(entity, &chain->entities, chain) {
  1268. ret = uvc_ctrl_commit_entity(chain->dev, entity, rollback);
  1269. if (ret < 0)
  1270. goto done;
  1271. }
  1272. if (!rollback)
  1273. uvc_ctrl_send_events(handle, xctrls, xctrls_count);
  1274. done:
  1275. mutex_unlock(&chain->ctrl_mutex);
  1276. return ret;
  1277. }
  1278. int uvc_ctrl_get(struct uvc_video_chain *chain,
  1279. struct v4l2_ext_control *xctrl)
  1280. {
  1281. struct uvc_control *ctrl;
  1282. struct uvc_control_mapping *mapping;
  1283. ctrl = uvc_find_control(chain, xctrl->id, &mapping);
  1284. if (ctrl == NULL)
  1285. return -EINVAL;
  1286. return __uvc_ctrl_get(chain, ctrl, mapping, &xctrl->value);
  1287. }
  1288. int uvc_ctrl_set(struct uvc_video_chain *chain,
  1289. struct v4l2_ext_control *xctrl)
  1290. {
  1291. struct uvc_control *ctrl;
  1292. struct uvc_control_mapping *mapping;
  1293. s32 value;
  1294. u32 step;
  1295. s32 min;
  1296. s32 max;
  1297. int ret;
  1298. ctrl = uvc_find_control(chain, xctrl->id, &mapping);
  1299. if (ctrl == NULL)
  1300. return -EINVAL;
  1301. if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
  1302. return -EACCES;
  1303. /* Clamp out of range values. */
  1304. switch (mapping->v4l2_type) {
  1305. case V4L2_CTRL_TYPE_INTEGER:
  1306. if (!ctrl->cached) {
  1307. ret = uvc_ctrl_populate_cache(chain, ctrl);
  1308. if (ret < 0)
  1309. return ret;
  1310. }
  1311. min = mapping->get(mapping, UVC_GET_MIN,
  1312. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
  1313. max = mapping->get(mapping, UVC_GET_MAX,
  1314. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
  1315. step = mapping->get(mapping, UVC_GET_RES,
  1316. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
  1317. if (step == 0)
  1318. step = 1;
  1319. xctrl->value = min + ((u32)(xctrl->value - min) + step / 2)
  1320. / step * step;
  1321. if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
  1322. xctrl->value = clamp(xctrl->value, min, max);
  1323. else
  1324. xctrl->value = clamp_t(u32, xctrl->value, min, max);
  1325. value = xctrl->value;
  1326. break;
  1327. case V4L2_CTRL_TYPE_BOOLEAN:
  1328. xctrl->value = clamp(xctrl->value, 0, 1);
  1329. value = xctrl->value;
  1330. break;
  1331. case V4L2_CTRL_TYPE_MENU:
  1332. if (xctrl->value < 0 || xctrl->value >= mapping->menu_count)
  1333. return -ERANGE;
  1334. value = mapping->menu_info[xctrl->value].value;
  1335. /* Valid menu indices are reported by the GET_RES request for
  1336. * UVC controls that support it.
  1337. */
  1338. if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK &&
  1339. (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)) {
  1340. if (!ctrl->cached) {
  1341. ret = uvc_ctrl_populate_cache(chain, ctrl);
  1342. if (ret < 0)
  1343. return ret;
  1344. }
  1345. step = mapping->get(mapping, UVC_GET_RES,
  1346. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
  1347. if (!(step & value))
  1348. return -ERANGE;
  1349. }
  1350. break;
  1351. default:
  1352. value = xctrl->value;
  1353. break;
  1354. }
  1355. /* If the mapping doesn't span the whole UVC control, the current value
  1356. * needs to be loaded from the device to perform the read-modify-write
  1357. * operation.
  1358. */
  1359. if (!ctrl->loaded && (ctrl->info.size * 8) != mapping->size) {
  1360. if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0) {
  1361. memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  1362. 0, ctrl->info.size);
  1363. } else {
  1364. ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
  1365. ctrl->entity->id, chain->dev->intfnum,
  1366. ctrl->info.selector,
  1367. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  1368. ctrl->info.size);
  1369. if (ret < 0)
  1370. return ret;
  1371. }
  1372. ctrl->loaded = 1;
  1373. }
  1374. /* Backup the current value in case we need to rollback later. */
  1375. if (!ctrl->dirty) {
  1376. memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
  1377. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  1378. ctrl->info.size);
  1379. }
  1380. mapping->set(mapping, value,
  1381. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
  1382. ctrl->dirty = 1;
  1383. ctrl->modified = 1;
  1384. return 0;
  1385. }
  1386. /* --------------------------------------------------------------------------
  1387. * Dynamic controls
  1388. */
  1389. static void uvc_ctrl_fixup_xu_info(struct uvc_device *dev,
  1390. const struct uvc_control *ctrl, struct uvc_control_info *info)
  1391. {
  1392. struct uvc_ctrl_fixup {
  1393. struct usb_device_id id;
  1394. u8 entity;
  1395. u8 selector;
  1396. u8 flags;
  1397. };
  1398. static const struct uvc_ctrl_fixup fixups[] = {
  1399. { { USB_DEVICE(0x046d, 0x08c2) }, 9, 1,
  1400. UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
  1401. UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
  1402. UVC_CTRL_FLAG_AUTO_UPDATE },
  1403. { { USB_DEVICE(0x046d, 0x08cc) }, 9, 1,
  1404. UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
  1405. UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
  1406. UVC_CTRL_FLAG_AUTO_UPDATE },
  1407. { { USB_DEVICE(0x046d, 0x0994) }, 9, 1,
  1408. UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
  1409. UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
  1410. UVC_CTRL_FLAG_AUTO_UPDATE },
  1411. };
  1412. unsigned int i;
  1413. for (i = 0; i < ARRAY_SIZE(fixups); ++i) {
  1414. if (!usb_match_one_id(dev->intf, &fixups[i].id))
  1415. continue;
  1416. if (fixups[i].entity == ctrl->entity->id &&
  1417. fixups[i].selector == info->selector) {
  1418. info->flags = fixups[i].flags;
  1419. return;
  1420. }
  1421. }
  1422. }
  1423. /*
  1424. * Query control information (size and flags) for XU controls.
  1425. */
  1426. static int uvc_ctrl_fill_xu_info(struct uvc_device *dev,
  1427. const struct uvc_control *ctrl, struct uvc_control_info *info)
  1428. {
  1429. u8 *data;
  1430. int ret;
  1431. data = kmalloc(2, GFP_KERNEL);
  1432. if (data == NULL)
  1433. return -ENOMEM;
  1434. memcpy(info->entity, ctrl->entity->extension.guidExtensionCode,
  1435. sizeof(info->entity));
  1436. info->index = ctrl->index;
  1437. info->selector = ctrl->index + 1;
  1438. /* Query and verify the control length (GET_LEN) */
  1439. ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id, dev->intfnum,
  1440. info->selector, data, 2);
  1441. if (ret < 0) {
  1442. uvc_trace(UVC_TRACE_CONTROL,
  1443. "GET_LEN failed on control %pUl/%u (%d).\n",
  1444. info->entity, info->selector, ret);
  1445. goto done;
  1446. }
  1447. info->size = le16_to_cpup((__le16 *)data);
  1448. /* Query the control information (GET_INFO) */
  1449. ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id, dev->intfnum,
  1450. info->selector, data, 1);
  1451. if (ret < 0) {
  1452. uvc_trace(UVC_TRACE_CONTROL,
  1453. "GET_INFO failed on control %pUl/%u (%d).\n",
  1454. info->entity, info->selector, ret);
  1455. goto done;
  1456. }
  1457. info->flags = UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX
  1458. | UVC_CTRL_FLAG_GET_RES | UVC_CTRL_FLAG_GET_DEF
  1459. | (data[0] & UVC_CONTROL_CAP_GET ?
  1460. UVC_CTRL_FLAG_GET_CUR : 0)
  1461. | (data[0] & UVC_CONTROL_CAP_SET ?
  1462. UVC_CTRL_FLAG_SET_CUR : 0)
  1463. | (data[0] & UVC_CONTROL_CAP_AUTOUPDATE ?
  1464. UVC_CTRL_FLAG_AUTO_UPDATE : 0);
  1465. uvc_ctrl_fixup_xu_info(dev, ctrl, info);
  1466. uvc_trace(UVC_TRACE_CONTROL, "XU control %pUl/%u queried: len %u, "
  1467. "flags { get %u set %u auto %u }.\n",
  1468. info->entity, info->selector, info->size,
  1469. (info->flags & UVC_CTRL_FLAG_GET_CUR) ? 1 : 0,
  1470. (info->flags & UVC_CTRL_FLAG_SET_CUR) ? 1 : 0,
  1471. (info->flags & UVC_CTRL_FLAG_AUTO_UPDATE) ? 1 : 0);
  1472. done:
  1473. kfree(data);
  1474. return ret;
  1475. }
  1476. static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
  1477. const struct uvc_control_info *info);
  1478. static int uvc_ctrl_init_xu_ctrl(struct uvc_device *dev,
  1479. struct uvc_control *ctrl)
  1480. {
  1481. struct uvc_control_info info;
  1482. int ret;
  1483. if (ctrl->initialized)
  1484. return 0;
  1485. ret = uvc_ctrl_fill_xu_info(dev, ctrl, &info);
  1486. if (ret < 0)
  1487. return ret;
  1488. ret = uvc_ctrl_add_info(dev, ctrl, &info);
  1489. if (ret < 0)
  1490. uvc_trace(UVC_TRACE_CONTROL, "Failed to initialize control "
  1491. "%pUl/%u on device %s entity %u\n", info.entity,
  1492. info.selector, dev->udev->devpath, ctrl->entity->id);
  1493. return ret;
  1494. }
  1495. int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
  1496. struct uvc_xu_control_query *xqry)
  1497. {
  1498. struct uvc_entity *entity;
  1499. struct uvc_control *ctrl;
  1500. unsigned int i, found = 0;
  1501. __u32 reqflags;
  1502. __u16 size;
  1503. __u8 *data = NULL;
  1504. int ret;
  1505. /* Find the extension unit. */
  1506. list_for_each_entry(entity, &chain->entities, chain) {
  1507. if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT &&
  1508. entity->id == xqry->unit)
  1509. break;
  1510. }
  1511. if (entity->id != xqry->unit) {
  1512. uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
  1513. xqry->unit);
  1514. return -ENOENT;
  1515. }
  1516. /* Find the control and perform delayed initialization if needed. */
  1517. for (i = 0; i < entity->ncontrols; ++i) {
  1518. ctrl = &entity->controls[i];
  1519. if (ctrl->index == xqry->selector - 1) {
  1520. found = 1;
  1521. break;
  1522. }
  1523. }
  1524. if (!found) {
  1525. uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u not found.\n",
  1526. entity->extension.guidExtensionCode, xqry->selector);
  1527. return -ENOENT;
  1528. }
  1529. if (mutex_lock_interruptible(&chain->ctrl_mutex))
  1530. return -ERESTARTSYS;
  1531. ret = uvc_ctrl_init_xu_ctrl(chain->dev, ctrl);
  1532. if (ret < 0) {
  1533. ret = -ENOENT;
  1534. goto done;
  1535. }
  1536. /* Validate the required buffer size and flags for the request */
  1537. reqflags = 0;
  1538. size = ctrl->info.size;
  1539. switch (xqry->query) {
  1540. case UVC_GET_CUR:
  1541. reqflags = UVC_CTRL_FLAG_GET_CUR;
  1542. break;
  1543. case UVC_GET_MIN:
  1544. reqflags = UVC_CTRL_FLAG_GET_MIN;
  1545. break;
  1546. case UVC_GET_MAX:
  1547. reqflags = UVC_CTRL_FLAG_GET_MAX;
  1548. break;
  1549. case UVC_GET_DEF:
  1550. reqflags = UVC_CTRL_FLAG_GET_DEF;
  1551. break;
  1552. case UVC_GET_RES:
  1553. reqflags = UVC_CTRL_FLAG_GET_RES;
  1554. break;
  1555. case UVC_SET_CUR:
  1556. reqflags = UVC_CTRL_FLAG_SET_CUR;
  1557. break;
  1558. case UVC_GET_LEN:
  1559. size = 2;
  1560. break;
  1561. case UVC_GET_INFO:
  1562. size = 1;
  1563. break;
  1564. default:
  1565. ret = -EINVAL;
  1566. goto done;
  1567. }
  1568. if (size != xqry->size) {
  1569. ret = -ENOBUFS;
  1570. goto done;
  1571. }
  1572. if (reqflags && !(ctrl->info.flags & reqflags)) {
  1573. ret = -EBADRQC;
  1574. goto done;
  1575. }
  1576. data = kmalloc(size, GFP_KERNEL);
  1577. if (data == NULL) {
  1578. ret = -ENOMEM;
  1579. goto done;
  1580. }
  1581. if (xqry->query == UVC_SET_CUR &&
  1582. copy_from_user(data, xqry->data, size)) {
  1583. ret = -EFAULT;
  1584. goto done;
  1585. }
  1586. ret = uvc_query_ctrl(chain->dev, xqry->query, xqry->unit,
  1587. chain->dev->intfnum, xqry->selector, data, size);
  1588. if (ret < 0)
  1589. goto done;
  1590. if (xqry->query != UVC_SET_CUR &&
  1591. copy_to_user(xqry->data, data, size))
  1592. ret = -EFAULT;
  1593. done:
  1594. kfree(data);
  1595. mutex_unlock(&chain->ctrl_mutex);
  1596. return ret;
  1597. }
  1598. /* --------------------------------------------------------------------------
  1599. * Suspend/resume
  1600. */
  1601. /*
  1602. * Restore control values after resume, skipping controls that haven't been
  1603. * changed.
  1604. *
  1605. * TODO
  1606. * - Don't restore modified controls that are back to their default value.
  1607. * - Handle restore order (Auto-Exposure Mode should be restored before
  1608. * Exposure Time).
  1609. */
  1610. int uvc_ctrl_resume_device(struct uvc_device *dev)
  1611. {
  1612. struct uvc_control *ctrl;
  1613. struct uvc_entity *entity;
  1614. unsigned int i;
  1615. int ret;
  1616. /* Walk the entities list and restore controls when possible. */
  1617. list_for_each_entry(entity, &dev->entities, list) {
  1618. for (i = 0; i < entity->ncontrols; ++i) {
  1619. ctrl = &entity->controls[i];
  1620. if (!ctrl->initialized || !ctrl->modified ||
  1621. (ctrl->info.flags & UVC_CTRL_FLAG_RESTORE) == 0)
  1622. continue;
  1623. printk(KERN_INFO "restoring control %pUl/%u/%u\n",
  1624. ctrl->info.entity, ctrl->info.index,
  1625. ctrl->info.selector);
  1626. ctrl->dirty = 1;
  1627. }
  1628. ret = uvc_ctrl_commit_entity(dev, entity, 0);
  1629. if (ret < 0)
  1630. return ret;
  1631. }
  1632. return 0;
  1633. }
  1634. /* --------------------------------------------------------------------------
  1635. * Control and mapping handling
  1636. */
  1637. /*
  1638. * Add control information to a given control.
  1639. */
  1640. static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
  1641. const struct uvc_control_info *info)
  1642. {
  1643. int ret = 0;
  1644. ctrl->info = *info;
  1645. INIT_LIST_HEAD(&ctrl->info.mappings);
  1646. /* Allocate an array to save control values (cur, def, max, etc.) */
  1647. ctrl->uvc_data = kzalloc(ctrl->info.size * UVC_CTRL_DATA_LAST + 1,
  1648. GFP_KERNEL);
  1649. if (ctrl->uvc_data == NULL) {
  1650. ret = -ENOMEM;
  1651. goto done;
  1652. }
  1653. ctrl->initialized = 1;
  1654. uvc_trace(UVC_TRACE_CONTROL, "Added control %pUl/%u to device %s "
  1655. "entity %u\n", ctrl->info.entity, ctrl->info.selector,
  1656. dev->udev->devpath, ctrl->entity->id);
  1657. done:
  1658. if (ret < 0)
  1659. kfree(ctrl->uvc_data);
  1660. return ret;
  1661. }
  1662. /*
  1663. * Add a control mapping to a given control.
  1664. */
  1665. static int __uvc_ctrl_add_mapping(struct uvc_device *dev,
  1666. struct uvc_control *ctrl, const struct uvc_control_mapping *mapping)
  1667. {
  1668. struct uvc_control_mapping *map;
  1669. unsigned int size;
  1670. /* Most mappings come from static kernel data and need to be duplicated.
  1671. * Mappings that come from userspace will be unnecessarily duplicated,
  1672. * this could be optimized.
  1673. */
  1674. map = kmemdup(mapping, sizeof(*mapping), GFP_KERNEL);
  1675. if (map == NULL)
  1676. return -ENOMEM;
  1677. INIT_LIST_HEAD(&map->ev_subs);
  1678. size = sizeof(*mapping->menu_info) * mapping->menu_count;
  1679. map->menu_info = kmemdup(mapping->menu_info, size, GFP_KERNEL);
  1680. if (map->menu_info == NULL) {
  1681. kfree(map);
  1682. return -ENOMEM;
  1683. }
  1684. if (map->get == NULL)
  1685. map->get = uvc_get_le_value;
  1686. if (map->set == NULL)
  1687. map->set = uvc_set_le_value;
  1688. list_add_tail(&map->list, &ctrl->info.mappings);
  1689. uvc_trace(UVC_TRACE_CONTROL,
  1690. "Adding mapping '%s' to control %pUl/%u.\n",
  1691. map->name, ctrl->info.entity, ctrl->info.selector);
  1692. return 0;
  1693. }
  1694. int uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
  1695. const struct uvc_control_mapping *mapping)
  1696. {
  1697. struct uvc_device *dev = chain->dev;
  1698. struct uvc_control_mapping *map;
  1699. struct uvc_entity *entity;
  1700. struct uvc_control *ctrl;
  1701. int found = 0;
  1702. int ret;
  1703. if (mapping->id & ~V4L2_CTRL_ID_MASK) {
  1704. uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', control "
  1705. "id 0x%08x is invalid.\n", mapping->name,
  1706. mapping->id);
  1707. return -EINVAL;
  1708. }
  1709. /* Search for the matching (GUID/CS) control on the current chain */
  1710. list_for_each_entry(entity, &chain->entities, chain) {
  1711. unsigned int i;
  1712. if (UVC_ENTITY_TYPE(entity) != UVC_VC_EXTENSION_UNIT ||
  1713. !uvc_entity_match_guid(entity, mapping->entity))
  1714. continue;
  1715. for (i = 0; i < entity->ncontrols; ++i) {
  1716. ctrl = &entity->controls[i];
  1717. if (ctrl->index == mapping->selector - 1) {
  1718. found = 1;
  1719. break;
  1720. }
  1721. }
  1722. if (found)
  1723. break;
  1724. }
  1725. if (!found)
  1726. return -ENOENT;
  1727. if (mutex_lock_interruptible(&chain->ctrl_mutex))
  1728. return -ERESTARTSYS;
  1729. /* Perform delayed initialization of XU controls */
  1730. ret = uvc_ctrl_init_xu_ctrl(dev, ctrl);
  1731. if (ret < 0) {
  1732. ret = -ENOENT;
  1733. goto done;
  1734. }
  1735. list_for_each_entry(map, &ctrl->info.mappings, list) {
  1736. if (mapping->id == map->id) {
  1737. uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', "
  1738. "control id 0x%08x already exists.\n",
  1739. mapping->name, mapping->id);
  1740. ret = -EEXIST;
  1741. goto done;
  1742. }
  1743. }
  1744. /* Prevent excess memory consumption */
  1745. if (atomic_inc_return(&dev->nmappings) > UVC_MAX_CONTROL_MAPPINGS) {
  1746. atomic_dec(&dev->nmappings);
  1747. uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', maximum "
  1748. "mappings count (%u) exceeded.\n", mapping->name,
  1749. UVC_MAX_CONTROL_MAPPINGS);
  1750. ret = -ENOMEM;
  1751. goto done;
  1752. }
  1753. ret = __uvc_ctrl_add_mapping(dev, ctrl, mapping);
  1754. if (ret < 0)
  1755. atomic_dec(&dev->nmappings);
  1756. done:
  1757. mutex_unlock(&chain->ctrl_mutex);
  1758. return ret;
  1759. }
  1760. /*
  1761. * Prune an entity of its bogus controls using a blacklist. Bogus controls
  1762. * are currently the ones that crash the camera or unconditionally return an
  1763. * error when queried.
  1764. */
  1765. static void uvc_ctrl_prune_entity(struct uvc_device *dev,
  1766. struct uvc_entity *entity)
  1767. {
  1768. struct uvc_ctrl_blacklist {
  1769. struct usb_device_id id;
  1770. u8 index;
  1771. };
  1772. static const struct uvc_ctrl_blacklist processing_blacklist[] = {
  1773. { { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */
  1774. { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
  1775. { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
  1776. };
  1777. static const struct uvc_ctrl_blacklist camera_blacklist[] = {
  1778. { { USB_DEVICE(0x06f8, 0x3005) }, 9 }, /* Zoom, Absolute */
  1779. };
  1780. const struct uvc_ctrl_blacklist *blacklist;
  1781. unsigned int size;
  1782. unsigned int count;
  1783. unsigned int i;
  1784. u8 *controls;
  1785. switch (UVC_ENTITY_TYPE(entity)) {
  1786. case UVC_VC_PROCESSING_UNIT:
  1787. blacklist = processing_blacklist;
  1788. count = ARRAY_SIZE(processing_blacklist);
  1789. controls = entity->processing.bmControls;
  1790. size = entity->processing.bControlSize;
  1791. break;
  1792. case UVC_ITT_CAMERA:
  1793. blacklist = camera_blacklist;
  1794. count = ARRAY_SIZE(camera_blacklist);
  1795. controls = entity->camera.bmControls;
  1796. size = entity->camera.bControlSize;
  1797. break;
  1798. default:
  1799. return;
  1800. }
  1801. for (i = 0; i < count; ++i) {
  1802. if (!usb_match_one_id(dev->intf, &blacklist[i].id))
  1803. continue;
  1804. if (blacklist[i].index >= 8 * size ||
  1805. !uvc_test_bit(controls, blacklist[i].index))
  1806. continue;
  1807. uvc_trace(UVC_TRACE_CONTROL, "%u/%u control is black listed, "
  1808. "removing it.\n", entity->id, blacklist[i].index);
  1809. uvc_clear_bit(controls, blacklist[i].index);
  1810. }
  1811. }
  1812. /*
  1813. * Add control information and hardcoded stock control mappings to the given
  1814. * device.
  1815. */
  1816. static void uvc_ctrl_init_ctrl(struct uvc_device *dev, struct uvc_control *ctrl)
  1817. {
  1818. const struct uvc_control_info *info = uvc_ctrls;
  1819. const struct uvc_control_info *iend = info + ARRAY_SIZE(uvc_ctrls);
  1820. const struct uvc_control_mapping *mapping = uvc_ctrl_mappings;
  1821. const struct uvc_control_mapping *mend =
  1822. mapping + ARRAY_SIZE(uvc_ctrl_mappings);
  1823. /* XU controls initialization requires querying the device for control
  1824. * information. As some buggy UVC devices will crash when queried
  1825. * repeatedly in a tight loop, delay XU controls initialization until
  1826. * first use.
  1827. */
  1828. if (UVC_ENTITY_TYPE(ctrl->entity) == UVC_VC_EXTENSION_UNIT)
  1829. return;
  1830. for (; info < iend; ++info) {
  1831. if (uvc_entity_match_guid(ctrl->entity, info->entity) &&
  1832. ctrl->index == info->index) {
  1833. uvc_ctrl_add_info(dev, ctrl, info);
  1834. break;
  1835. }
  1836. }
  1837. if (!ctrl->initialized)
  1838. return;
  1839. for (; mapping < mend; ++mapping) {
  1840. if (uvc_entity_match_guid(ctrl->entity, mapping->entity) &&
  1841. ctrl->info.selector == mapping->selector)
  1842. __uvc_ctrl_add_mapping(dev, ctrl, mapping);
  1843. }
  1844. }
  1845. /*
  1846. * Initialize device controls.
  1847. */
  1848. int uvc_ctrl_init_device(struct uvc_device *dev)
  1849. {
  1850. struct uvc_entity *entity;
  1851. unsigned int i;
  1852. /* Walk the entities list and instantiate controls */
  1853. list_for_each_entry(entity, &dev->entities, list) {
  1854. struct uvc_control *ctrl;
  1855. unsigned int bControlSize = 0, ncontrols;
  1856. __u8 *bmControls = NULL;
  1857. if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
  1858. bmControls = entity->extension.bmControls;
  1859. bControlSize = entity->extension.bControlSize;
  1860. } else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) {
  1861. bmControls = entity->processing.bmControls;
  1862. bControlSize = entity->processing.bControlSize;
  1863. } else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
  1864. bmControls = entity->camera.bmControls;
  1865. bControlSize = entity->camera.bControlSize;
  1866. }
  1867. /* Remove bogus/blacklisted controls */
  1868. uvc_ctrl_prune_entity(dev, entity);
  1869. /* Count supported controls and allocate the controls array */
  1870. ncontrols = memweight(bmControls, bControlSize);
  1871. if (ncontrols == 0)
  1872. continue;
  1873. entity->controls = kcalloc(ncontrols, sizeof(*ctrl),
  1874. GFP_KERNEL);
  1875. if (entity->controls == NULL)
  1876. return -ENOMEM;
  1877. entity->ncontrols = ncontrols;
  1878. /* Initialize all supported controls */
  1879. ctrl = entity->controls;
  1880. for (i = 0; i < bControlSize * 8; ++i) {
  1881. if (uvc_test_bit(bmControls, i) == 0)
  1882. continue;
  1883. ctrl->entity = entity;
  1884. ctrl->index = i;
  1885. uvc_ctrl_init_ctrl(dev, ctrl);
  1886. ctrl++;
  1887. }
  1888. }
  1889. return 0;
  1890. }
  1891. /*
  1892. * Cleanup device controls.
  1893. */
  1894. static void uvc_ctrl_cleanup_mappings(struct uvc_device *dev,
  1895. struct uvc_control *ctrl)
  1896. {
  1897. struct uvc_control_mapping *mapping, *nm;
  1898. list_for_each_entry_safe(mapping, nm, &ctrl->info.mappings, list) {
  1899. list_del(&mapping->list);
  1900. kfree(mapping->menu_info);
  1901. kfree(mapping);
  1902. }
  1903. }
  1904. void uvc_ctrl_cleanup_device(struct uvc_device *dev)
  1905. {
  1906. struct uvc_entity *entity;
  1907. unsigned int i;
  1908. /* Free controls and control mappings for all entities. */
  1909. list_for_each_entry(entity, &dev->entities, list) {
  1910. for (i = 0; i < entity->ncontrols; ++i) {
  1911. struct uvc_control *ctrl = &entity->controls[i];
  1912. if (!ctrl->initialized)
  1913. continue;
  1914. uvc_ctrl_cleanup_mappings(dev, ctrl);
  1915. kfree(ctrl->uvc_data);
  1916. }
  1917. kfree(entity->controls);
  1918. }
  1919. }