uvc_ctrl.c 60 KB

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