uvc_ctrl.c 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * uvc_ctrl.c -- USB Video Class driver - Controls
  4. *
  5. * Copyright (C) 2005-2010
  6. * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  7. */
  8. #include <asm/barrier.h>
  9. #include <linux/bitops.h>
  10. #include <linux/kernel.h>
  11. #include <linux/list.h>
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/usb.h>
  16. #include <linux/usb/uvc.h>
  17. #include <linux/videodev2.h>
  18. #include <linux/vmalloc.h>
  19. #include <linux/wait.h>
  20. #include <linux/workqueue.h>
  21. #include <linux/atomic.h>
  22. #include <media/v4l2-ctrls.h>
  23. #include "uvcvideo.h"
  24. #define UVC_CTRL_DATA_CURRENT 0
  25. #define UVC_CTRL_DATA_BACKUP 1
  26. #define UVC_CTRL_DATA_MIN 2
  27. #define UVC_CTRL_DATA_MAX 3
  28. #define UVC_CTRL_DATA_RES 4
  29. #define UVC_CTRL_DATA_DEF 5
  30. #define UVC_CTRL_DATA_LAST 6
  31. /* ------------------------------------------------------------------------
  32. * Controls
  33. */
  34. static const struct uvc_control_info uvc_ctrls[] = {
  35. {
  36. .entity = UVC_GUID_UVC_PROCESSING,
  37. .selector = UVC_PU_BRIGHTNESS_CONTROL,
  38. .index = 0,
  39. .size = 2,
  40. .flags = UVC_CTRL_FLAG_SET_CUR
  41. | UVC_CTRL_FLAG_GET_RANGE
  42. | UVC_CTRL_FLAG_RESTORE,
  43. },
  44. {
  45. .entity = UVC_GUID_UVC_PROCESSING,
  46. .selector = UVC_PU_CONTRAST_CONTROL,
  47. .index = 1,
  48. .size = 2,
  49. .flags = UVC_CTRL_FLAG_SET_CUR
  50. | UVC_CTRL_FLAG_GET_RANGE
  51. | UVC_CTRL_FLAG_RESTORE,
  52. },
  53. {
  54. .entity = UVC_GUID_UVC_PROCESSING,
  55. .selector = UVC_PU_HUE_CONTROL,
  56. .index = 2,
  57. .size = 2,
  58. .flags = UVC_CTRL_FLAG_SET_CUR
  59. | UVC_CTRL_FLAG_GET_RANGE
  60. | UVC_CTRL_FLAG_RESTORE
  61. | UVC_CTRL_FLAG_AUTO_UPDATE,
  62. },
  63. {
  64. .entity = UVC_GUID_UVC_PROCESSING,
  65. .selector = UVC_PU_SATURATION_CONTROL,
  66. .index = 3,
  67. .size = 2,
  68. .flags = UVC_CTRL_FLAG_SET_CUR
  69. | UVC_CTRL_FLAG_GET_RANGE
  70. | UVC_CTRL_FLAG_RESTORE,
  71. },
  72. {
  73. .entity = UVC_GUID_UVC_PROCESSING,
  74. .selector = UVC_PU_SHARPNESS_CONTROL,
  75. .index = 4,
  76. .size = 2,
  77. .flags = UVC_CTRL_FLAG_SET_CUR
  78. | UVC_CTRL_FLAG_GET_RANGE
  79. | UVC_CTRL_FLAG_RESTORE,
  80. },
  81. {
  82. .entity = UVC_GUID_UVC_PROCESSING,
  83. .selector = UVC_PU_GAMMA_CONTROL,
  84. .index = 5,
  85. .size = 2,
  86. .flags = UVC_CTRL_FLAG_SET_CUR
  87. | UVC_CTRL_FLAG_GET_RANGE
  88. | UVC_CTRL_FLAG_RESTORE,
  89. },
  90. {
  91. .entity = UVC_GUID_UVC_PROCESSING,
  92. .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
  93. .index = 6,
  94. .size = 2,
  95. .flags = UVC_CTRL_FLAG_SET_CUR
  96. | UVC_CTRL_FLAG_GET_RANGE
  97. | UVC_CTRL_FLAG_RESTORE
  98. | UVC_CTRL_FLAG_AUTO_UPDATE,
  99. },
  100. {
  101. .entity = UVC_GUID_UVC_PROCESSING,
  102. .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
  103. .index = 7,
  104. .size = 4,
  105. .flags = UVC_CTRL_FLAG_SET_CUR
  106. | UVC_CTRL_FLAG_GET_RANGE
  107. | UVC_CTRL_FLAG_RESTORE
  108. | UVC_CTRL_FLAG_AUTO_UPDATE,
  109. },
  110. {
  111. .entity = UVC_GUID_UVC_PROCESSING,
  112. .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
  113. .index = 8,
  114. .size = 2,
  115. .flags = UVC_CTRL_FLAG_SET_CUR
  116. | UVC_CTRL_FLAG_GET_RANGE
  117. | UVC_CTRL_FLAG_RESTORE,
  118. },
  119. {
  120. .entity = UVC_GUID_UVC_PROCESSING,
  121. .selector = UVC_PU_GAIN_CONTROL,
  122. .index = 9,
  123. .size = 2,
  124. .flags = UVC_CTRL_FLAG_SET_CUR
  125. | UVC_CTRL_FLAG_GET_RANGE
  126. | UVC_CTRL_FLAG_RESTORE,
  127. },
  128. {
  129. .entity = UVC_GUID_UVC_PROCESSING,
  130. .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
  131. .index = 10,
  132. .size = 1,
  133. .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
  134. | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
  135. },
  136. {
  137. .entity = UVC_GUID_UVC_PROCESSING,
  138. .selector = UVC_PU_HUE_AUTO_CONTROL,
  139. .index = 11,
  140. .size = 1,
  141. .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
  142. | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
  143. },
  144. {
  145. .entity = UVC_GUID_UVC_PROCESSING,
  146. .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
  147. .index = 12,
  148. .size = 1,
  149. .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
  150. | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
  151. },
  152. {
  153. .entity = UVC_GUID_UVC_PROCESSING,
  154. .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
  155. .index = 13,
  156. .size = 1,
  157. .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
  158. | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
  159. },
  160. {
  161. .entity = UVC_GUID_UVC_PROCESSING,
  162. .selector = UVC_PU_DIGITAL_MULTIPLIER_CONTROL,
  163. .index = 14,
  164. .size = 2,
  165. .flags = UVC_CTRL_FLAG_SET_CUR
  166. | UVC_CTRL_FLAG_GET_RANGE
  167. | UVC_CTRL_FLAG_RESTORE,
  168. },
  169. {
  170. .entity = UVC_GUID_UVC_PROCESSING,
  171. .selector = UVC_PU_DIGITAL_MULTIPLIER_LIMIT_CONTROL,
  172. .index = 15,
  173. .size = 2,
  174. .flags = UVC_CTRL_FLAG_SET_CUR
  175. | UVC_CTRL_FLAG_GET_RANGE
  176. | UVC_CTRL_FLAG_RESTORE,
  177. },
  178. {
  179. .entity = UVC_GUID_UVC_PROCESSING,
  180. .selector = UVC_PU_ANALOG_VIDEO_STANDARD_CONTROL,
  181. .index = 16,
  182. .size = 1,
  183. .flags = UVC_CTRL_FLAG_GET_CUR,
  184. },
  185. {
  186. .entity = UVC_GUID_UVC_PROCESSING,
  187. .selector = UVC_PU_ANALOG_LOCK_STATUS_CONTROL,
  188. .index = 17,
  189. .size = 1,
  190. .flags = UVC_CTRL_FLAG_GET_CUR,
  191. },
  192. {
  193. .entity = UVC_GUID_UVC_CAMERA,
  194. .selector = UVC_CT_SCANNING_MODE_CONTROL,
  195. .index = 0,
  196. .size = 1,
  197. .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
  198. | UVC_CTRL_FLAG_RESTORE,
  199. },
  200. {
  201. .entity = UVC_GUID_UVC_CAMERA,
  202. .selector = UVC_CT_AE_MODE_CONTROL,
  203. .index = 1,
  204. .size = 1,
  205. .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
  206. | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_GET_RES
  207. | UVC_CTRL_FLAG_RESTORE,
  208. },
  209. {
  210. .entity = UVC_GUID_UVC_CAMERA,
  211. .selector = UVC_CT_AE_PRIORITY_CONTROL,
  212. .index = 2,
  213. .size = 1,
  214. .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
  215. | UVC_CTRL_FLAG_RESTORE,
  216. },
  217. {
  218. .entity = UVC_GUID_UVC_CAMERA,
  219. .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
  220. .index = 3,
  221. .size = 4,
  222. .flags = UVC_CTRL_FLAG_SET_CUR
  223. | UVC_CTRL_FLAG_GET_RANGE
  224. | UVC_CTRL_FLAG_RESTORE
  225. | UVC_CTRL_FLAG_AUTO_UPDATE,
  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
  308. | UVC_CTRL_FLAG_GET_RANGE
  309. | UVC_CTRL_FLAG_AUTO_UPDATE,
  310. },
  311. {
  312. .entity = UVC_GUID_UVC_CAMERA,
  313. .selector = UVC_CT_ROLL_ABSOLUTE_CONTROL,
  314. .index = 13,
  315. .size = 2,
  316. .flags = UVC_CTRL_FLAG_SET_CUR
  317. | UVC_CTRL_FLAG_GET_RANGE
  318. | UVC_CTRL_FLAG_RESTORE
  319. | UVC_CTRL_FLAG_AUTO_UPDATE,
  320. },
  321. {
  322. .entity = UVC_GUID_UVC_CAMERA,
  323. .selector = UVC_CT_ROLL_RELATIVE_CONTROL,
  324. .index = 14,
  325. .size = 2,
  326. .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
  327. | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
  328. | UVC_CTRL_FLAG_GET_DEF
  329. | UVC_CTRL_FLAG_AUTO_UPDATE,
  330. },
  331. {
  332. .entity = UVC_GUID_UVC_CAMERA,
  333. .selector = UVC_CT_FOCUS_AUTO_CONTROL,
  334. .index = 17,
  335. .size = 1,
  336. .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
  337. | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
  338. },
  339. {
  340. .entity = UVC_GUID_UVC_CAMERA,
  341. .selector = UVC_CT_PRIVACY_CONTROL,
  342. .index = 18,
  343. .size = 1,
  344. .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
  345. | UVC_CTRL_FLAG_RESTORE
  346. | UVC_CTRL_FLAG_AUTO_UPDATE,
  347. },
  348. {
  349. .entity = UVC_GUID_EXT_GPIO_CONTROLLER,
  350. .selector = UVC_CT_PRIVACY_CONTROL,
  351. .index = 0,
  352. .size = 1,
  353. .flags = UVC_CTRL_FLAG_GET_CUR
  354. | UVC_CTRL_FLAG_AUTO_UPDATE,
  355. },
  356. /*
  357. * UVC_CTRL_FLAG_AUTO_UPDATE is needed because the RoI may get updated
  358. * by sensors.
  359. * "This RoI should be the same as specified in most recent SET_CUR
  360. * except in the case where the ‘Auto Detect and Track’ and/or
  361. * ‘Image Stabilization’ bit have been set."
  362. * 4.2.2.1.20 Digital Region of Interest (ROI) Control
  363. */
  364. {
  365. .entity = UVC_GUID_UVC_CAMERA,
  366. .selector = UVC_CT_REGION_OF_INTEREST_CONTROL,
  367. .index = 21,
  368. .size = 10,
  369. .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
  370. | UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX
  371. | UVC_CTRL_FLAG_GET_DEF
  372. | UVC_CTRL_FLAG_AUTO_UPDATE,
  373. },
  374. {
  375. .entity = UVC_GUID_CHROMEOS_XU,
  376. .selector = UVC_CROSXU_CONTROL_IQ_PROFILE,
  377. .index = 3,
  378. .size = 1,
  379. .flags = UVC_CTRL_FLAG_SET_CUR
  380. | UVC_CTRL_FLAG_GET_RANGE
  381. | UVC_CTRL_FLAG_RESTORE,
  382. },
  383. };
  384. static const u32 uvc_control_classes[] = {
  385. V4L2_CID_CAMERA_CLASS,
  386. V4L2_CID_USER_CLASS,
  387. };
  388. static const int exposure_auto_mapping[] = { 2, 1, 4, 8 };
  389. static const int cros_colorfx_mapping[] = {
  390. 1, /* V4L2_COLORFX_NONE */
  391. -1, /* V4L2_COLORFX_BW */
  392. -1, /* V4L2_COLORFX_SEPIA */
  393. -1, /* V4L2_COLORFX_NEGATIVE */
  394. -1, /* V4L2_COLORFX_EMBOSS */
  395. -1, /* V4L2_COLORFX_SKETCH */
  396. -1, /* V4L2_COLORFX_SKY_BLUE */
  397. -1, /* V4L2_COLORFX_GRASS_GREEN */
  398. -1, /* V4L2_COLORFX_SKIN_WHITEN */
  399. 0, /* V4L2_COLORFX_VIVID */
  400. };
  401. static bool uvc_ctrl_mapping_is_compound(struct uvc_control_mapping *mapping)
  402. {
  403. return mapping->v4l2_type >= V4L2_CTRL_COMPOUND_TYPES;
  404. }
  405. static s32 uvc_mapping_get_s32(struct uvc_control_mapping *mapping,
  406. u8 query, const void *data_in)
  407. {
  408. s32 data_out = 0;
  409. mapping->get(mapping, query, data_in, sizeof(data_out), &data_out);
  410. return data_out;
  411. }
  412. static void uvc_mapping_set_s32(struct uvc_control_mapping *mapping,
  413. s32 data_in, void *data_out)
  414. {
  415. mapping->set(mapping, sizeof(data_in), &data_in, data_out);
  416. }
  417. /*
  418. * This function translates the V4L2 menu index @idx, as exposed to userspace as
  419. * the V4L2 control value, to the corresponding UVC control value used by the
  420. * device. The custom menu_mapping in the control @mapping is used when
  421. * available, otherwise the function assumes that the V4L2 and UVC values are
  422. * identical.
  423. *
  424. * For controls of type UVC_CTRL_DATA_TYPE_BITMASK, the UVC control value is
  425. * expressed as a bitmask and is thus guaranteed to have a single bit set.
  426. *
  427. * The function returns -EINVAL if the V4L2 menu index @idx isn't valid for the
  428. * control, which includes all controls whose type isn't UVC_CTRL_DATA_TYPE_ENUM
  429. * or UVC_CTRL_DATA_TYPE_BITMASK.
  430. */
  431. static int uvc_mapping_get_menu_value(const struct uvc_control_mapping *mapping,
  432. u32 idx)
  433. {
  434. if (!test_bit(idx, &mapping->menu_mask))
  435. return -EINVAL;
  436. if (mapping->menu_mapping)
  437. return mapping->menu_mapping[idx];
  438. return idx;
  439. }
  440. static const char *
  441. uvc_mapping_get_menu_name(const struct uvc_control_mapping *mapping, u32 idx)
  442. {
  443. if (!test_bit(idx, &mapping->menu_mask))
  444. return NULL;
  445. if (mapping->menu_names)
  446. return mapping->menu_names[idx];
  447. return v4l2_ctrl_get_menu(mapping->id)[idx];
  448. }
  449. static int uvc_ctrl_get_zoom(struct uvc_control_mapping *mapping, u8 query,
  450. const void *uvc_in, size_t v4l2_size,
  451. void *v4l2_out)
  452. {
  453. u8 value = ((u8 *)uvc_in)[2];
  454. s8 sign = ((s8 *)uvc_in)[0];
  455. s32 *out = v4l2_out;
  456. if (WARN_ON(v4l2_size != sizeof(s32)))
  457. return -EINVAL;
  458. switch (query) {
  459. case UVC_GET_CUR:
  460. *out = (sign == 0) ? 0 : (sign > 0 ? value : -value);
  461. return 0;
  462. case UVC_GET_MIN:
  463. /* Not used, we use -UVC_GET_MAX */
  464. case UVC_GET_MAX:
  465. case UVC_GET_RES:
  466. case UVC_GET_DEF:
  467. default:
  468. *out = value;
  469. return 0;
  470. }
  471. }
  472. static int uvc_ctrl_set_zoom(struct uvc_control_mapping *mapping,
  473. size_t v4l2_size, const void *v4l2_in,
  474. void *uvc_out)
  475. {
  476. u8 *out = uvc_out;
  477. s32 value;
  478. if (WARN_ON(v4l2_size != sizeof(s32)))
  479. return -EINVAL;
  480. value = *(u32 *)v4l2_in;
  481. out[0] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
  482. out[2] = min_t(int, abs(value), 0xff);
  483. return 0;
  484. }
  485. static int uvc_ctrl_get_rel_speed(struct uvc_control_mapping *mapping,
  486. u8 query, const void *uvc_in,
  487. size_t v4l2_size, void *v4l2_out)
  488. {
  489. unsigned int first = mapping->offset / 8;
  490. u8 value = ((u8 *)uvc_in)[first + 1];
  491. s8 sign = ((s8 *)uvc_in)[first];
  492. s32 *out = v4l2_out;
  493. if (WARN_ON(v4l2_size != sizeof(s32)))
  494. return -EINVAL;
  495. switch (query) {
  496. case UVC_GET_CUR:
  497. *out = (sign == 0) ? 0 : (sign > 0 ? value : -value);
  498. return 0;
  499. case UVC_GET_MIN:
  500. /* Not used, we use -UVC_GET_MAX */
  501. case UVC_GET_MAX:
  502. case UVC_GET_RES:
  503. case UVC_GET_DEF:
  504. default:
  505. *out = value;
  506. return 0;
  507. }
  508. }
  509. static int uvc_ctrl_set_rel_speed(struct uvc_control_mapping *mapping,
  510. size_t v4l2_size, const void *v4l2_in,
  511. void *uvc_out)
  512. {
  513. unsigned int first = mapping->offset / 8;
  514. u8 *out = uvc_out;
  515. s32 value;
  516. if (WARN_ON(v4l2_size != sizeof(s32)))
  517. return -EINVAL;
  518. value = *(u32 *)v4l2_in;
  519. out[first] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
  520. out[first + 1] = min_t(int, abs(value), 0xff);
  521. return 0;
  522. }
  523. static const struct uvc_control_mapping uvc_ctrl_power_line_mapping_limited = {
  524. .id = V4L2_CID_POWER_LINE_FREQUENCY,
  525. .entity = UVC_GUID_UVC_PROCESSING,
  526. .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
  527. .size = 2,
  528. .offset = 0,
  529. .v4l2_type = V4L2_CTRL_TYPE_MENU,
  530. .data_type = UVC_CTRL_DATA_TYPE_ENUM,
  531. .menu_mask = GENMASK(V4L2_CID_POWER_LINE_FREQUENCY_60HZ,
  532. V4L2_CID_POWER_LINE_FREQUENCY_50HZ),
  533. };
  534. static const struct uvc_control_mapping uvc_ctrl_power_line_mapping_uvc11 = {
  535. .id = V4L2_CID_POWER_LINE_FREQUENCY,
  536. .entity = UVC_GUID_UVC_PROCESSING,
  537. .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
  538. .size = 2,
  539. .offset = 0,
  540. .v4l2_type = V4L2_CTRL_TYPE_MENU,
  541. .data_type = UVC_CTRL_DATA_TYPE_ENUM,
  542. .menu_mask = GENMASK(V4L2_CID_POWER_LINE_FREQUENCY_60HZ,
  543. V4L2_CID_POWER_LINE_FREQUENCY_DISABLED),
  544. };
  545. static const struct uvc_control_mapping uvc_ctrl_power_line_mapping_uvc15 = {
  546. .id = V4L2_CID_POWER_LINE_FREQUENCY,
  547. .entity = UVC_GUID_UVC_PROCESSING,
  548. .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
  549. .size = 2,
  550. .offset = 0,
  551. .v4l2_type = V4L2_CTRL_TYPE_MENU,
  552. .data_type = UVC_CTRL_DATA_TYPE_ENUM,
  553. .menu_mask = GENMASK(V4L2_CID_POWER_LINE_FREQUENCY_AUTO,
  554. V4L2_CID_POWER_LINE_FREQUENCY_DISABLED),
  555. };
  556. static const struct uvc_control_mapping *uvc_ctrl_filter_plf_mapping(
  557. struct uvc_video_chain *chain, struct uvc_control *ctrl)
  558. {
  559. const struct uvc_control_mapping *out_mapping =
  560. &uvc_ctrl_power_line_mapping_uvc11;
  561. u8 *buf __free(kfree) = NULL;
  562. u8 init_val;
  563. int ret;
  564. buf = kmalloc_obj(*buf);
  565. if (!buf)
  566. return NULL;
  567. /* Save the current PLF value, so we can restore it. */
  568. ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, ctrl->entity->id,
  569. chain->dev->intfnum, ctrl->info.selector,
  570. buf, sizeof(*buf));
  571. /* If we cannot read the control skip it. */
  572. if (ret)
  573. return NULL;
  574. init_val = *buf;
  575. /* If PLF value cannot be set to off, it is limited. */
  576. *buf = V4L2_CID_POWER_LINE_FREQUENCY_DISABLED;
  577. ret = uvc_query_ctrl(chain->dev, UVC_SET_CUR, ctrl->entity->id,
  578. chain->dev->intfnum, ctrl->info.selector,
  579. buf, sizeof(*buf));
  580. if (ret)
  581. return &uvc_ctrl_power_line_mapping_limited;
  582. /* UVC 1.1 does not define auto, we can exit. */
  583. if (chain->dev->uvc_version < 0x150)
  584. goto end;
  585. /* Check if the device supports auto. */
  586. *buf = V4L2_CID_POWER_LINE_FREQUENCY_AUTO;
  587. ret = uvc_query_ctrl(chain->dev, UVC_SET_CUR, ctrl->entity->id,
  588. chain->dev->intfnum, ctrl->info.selector,
  589. buf, sizeof(*buf));
  590. if (!ret)
  591. out_mapping = &uvc_ctrl_power_line_mapping_uvc15;
  592. end:
  593. /* Restore initial value and add mapping. */
  594. *buf = init_val;
  595. uvc_query_ctrl(chain->dev, UVC_SET_CUR, ctrl->entity->id,
  596. chain->dev->intfnum, ctrl->info.selector,
  597. buf, sizeof(*buf));
  598. return out_mapping;
  599. }
  600. static int uvc_get_rect(struct uvc_control_mapping *mapping, u8 query,
  601. const void *uvc_in, size_t v4l2_size, void *v4l2_out)
  602. {
  603. const struct uvc_rect *uvc_rect = uvc_in;
  604. struct v4l2_rect *v4l2_rect = v4l2_out;
  605. if (WARN_ON(v4l2_size != sizeof(struct v4l2_rect)))
  606. return -EINVAL;
  607. if (uvc_rect->left > uvc_rect->right ||
  608. uvc_rect->top > uvc_rect->bottom)
  609. return -EIO;
  610. v4l2_rect->top = uvc_rect->top;
  611. v4l2_rect->left = uvc_rect->left;
  612. v4l2_rect->height = uvc_rect->bottom - uvc_rect->top + 1;
  613. v4l2_rect->width = uvc_rect->right - uvc_rect->left + 1;
  614. return 0;
  615. }
  616. static int uvc_set_rect(struct uvc_control_mapping *mapping, size_t v4l2_size,
  617. const void *v4l2_in, void *uvc_out)
  618. {
  619. struct uvc_rect *uvc_rect = uvc_out;
  620. const struct v4l2_rect *v4l2_rect = v4l2_in;
  621. if (WARN_ON(v4l2_size != sizeof(struct v4l2_rect)))
  622. return -EINVAL;
  623. uvc_rect->top = min(0xffff, v4l2_rect->top);
  624. uvc_rect->left = min(0xffff, v4l2_rect->left);
  625. uvc_rect->bottom = min(0xffff, v4l2_rect->top + v4l2_rect->height - 1);
  626. uvc_rect->right = min(0xffff, v4l2_rect->left + v4l2_rect->width - 1);
  627. return 0;
  628. }
  629. static const struct uvc_control_mapping uvc_ctrl_mappings[] = {
  630. {
  631. .id = V4L2_CID_BRIGHTNESS,
  632. .entity = UVC_GUID_UVC_PROCESSING,
  633. .selector = UVC_PU_BRIGHTNESS_CONTROL,
  634. .size = 16,
  635. .offset = 0,
  636. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  637. .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
  638. },
  639. {
  640. .id = V4L2_CID_CONTRAST,
  641. .entity = UVC_GUID_UVC_PROCESSING,
  642. .selector = UVC_PU_CONTRAST_CONTROL,
  643. .size = 16,
  644. .offset = 0,
  645. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  646. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  647. },
  648. {
  649. .id = V4L2_CID_HUE,
  650. .entity = UVC_GUID_UVC_PROCESSING,
  651. .selector = UVC_PU_HUE_CONTROL,
  652. .size = 16,
  653. .offset = 0,
  654. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  655. .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
  656. .master_id = V4L2_CID_HUE_AUTO,
  657. .master_manual = 0,
  658. },
  659. {
  660. .id = V4L2_CID_SATURATION,
  661. .entity = UVC_GUID_UVC_PROCESSING,
  662. .selector = UVC_PU_SATURATION_CONTROL,
  663. .size = 16,
  664. .offset = 0,
  665. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  666. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  667. },
  668. {
  669. .id = V4L2_CID_SHARPNESS,
  670. .entity = UVC_GUID_UVC_PROCESSING,
  671. .selector = UVC_PU_SHARPNESS_CONTROL,
  672. .size = 16,
  673. .offset = 0,
  674. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  675. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  676. },
  677. {
  678. .id = V4L2_CID_GAMMA,
  679. .entity = UVC_GUID_UVC_PROCESSING,
  680. .selector = UVC_PU_GAMMA_CONTROL,
  681. .size = 16,
  682. .offset = 0,
  683. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  684. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  685. },
  686. {
  687. .id = V4L2_CID_BACKLIGHT_COMPENSATION,
  688. .entity = UVC_GUID_UVC_PROCESSING,
  689. .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
  690. .size = 16,
  691. .offset = 0,
  692. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  693. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  694. },
  695. {
  696. .id = V4L2_CID_GAIN,
  697. .entity = UVC_GUID_UVC_PROCESSING,
  698. .selector = UVC_PU_GAIN_CONTROL,
  699. .size = 16,
  700. .offset = 0,
  701. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  702. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  703. },
  704. {
  705. .id = V4L2_CID_HUE_AUTO,
  706. .entity = UVC_GUID_UVC_PROCESSING,
  707. .selector = UVC_PU_HUE_AUTO_CONTROL,
  708. .size = 1,
  709. .offset = 0,
  710. .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
  711. .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
  712. .slave_ids = { V4L2_CID_HUE, },
  713. },
  714. {
  715. .id = V4L2_CID_EXPOSURE_AUTO,
  716. .entity = UVC_GUID_UVC_CAMERA,
  717. .selector = UVC_CT_AE_MODE_CONTROL,
  718. .size = 4,
  719. .offset = 0,
  720. .v4l2_type = V4L2_CTRL_TYPE_MENU,
  721. .data_type = UVC_CTRL_DATA_TYPE_BITMASK,
  722. .menu_mapping = exposure_auto_mapping,
  723. .menu_mask = GENMASK(V4L2_EXPOSURE_APERTURE_PRIORITY,
  724. V4L2_EXPOSURE_AUTO),
  725. .slave_ids = { V4L2_CID_EXPOSURE_ABSOLUTE, },
  726. },
  727. {
  728. .id = V4L2_CID_EXPOSURE_AUTO_PRIORITY,
  729. .entity = UVC_GUID_UVC_CAMERA,
  730. .selector = UVC_CT_AE_PRIORITY_CONTROL,
  731. .size = 1,
  732. .offset = 0,
  733. .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
  734. .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
  735. },
  736. {
  737. .id = V4L2_CID_EXPOSURE_ABSOLUTE,
  738. .entity = UVC_GUID_UVC_CAMERA,
  739. .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
  740. .size = 32,
  741. .offset = 0,
  742. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  743. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  744. .master_id = V4L2_CID_EXPOSURE_AUTO,
  745. .master_manual = V4L2_EXPOSURE_MANUAL,
  746. },
  747. {
  748. .id = V4L2_CID_AUTO_WHITE_BALANCE,
  749. .entity = UVC_GUID_UVC_PROCESSING,
  750. .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
  751. .size = 1,
  752. .offset = 0,
  753. .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
  754. .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
  755. .slave_ids = { V4L2_CID_WHITE_BALANCE_TEMPERATURE, },
  756. },
  757. {
  758. .id = V4L2_CID_WHITE_BALANCE_TEMPERATURE,
  759. .entity = UVC_GUID_UVC_PROCESSING,
  760. .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
  761. .size = 16,
  762. .offset = 0,
  763. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  764. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  765. .master_id = V4L2_CID_AUTO_WHITE_BALANCE,
  766. .master_manual = 0,
  767. },
  768. {
  769. .id = V4L2_CID_AUTO_WHITE_BALANCE,
  770. .entity = UVC_GUID_UVC_PROCESSING,
  771. .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
  772. .size = 1,
  773. .offset = 0,
  774. .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
  775. .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
  776. .slave_ids = { V4L2_CID_BLUE_BALANCE,
  777. V4L2_CID_RED_BALANCE },
  778. },
  779. {
  780. .id = V4L2_CID_BLUE_BALANCE,
  781. .entity = UVC_GUID_UVC_PROCESSING,
  782. .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
  783. .size = 16,
  784. .offset = 0,
  785. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  786. .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
  787. .master_id = V4L2_CID_AUTO_WHITE_BALANCE,
  788. .master_manual = 0,
  789. },
  790. {
  791. .id = V4L2_CID_RED_BALANCE,
  792. .entity = UVC_GUID_UVC_PROCESSING,
  793. .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
  794. .size = 16,
  795. .offset = 16,
  796. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  797. .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
  798. .master_id = V4L2_CID_AUTO_WHITE_BALANCE,
  799. .master_manual = 0,
  800. },
  801. {
  802. .id = V4L2_CID_FOCUS_ABSOLUTE,
  803. .entity = UVC_GUID_UVC_CAMERA,
  804. .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
  805. .size = 16,
  806. .offset = 0,
  807. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  808. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  809. .master_id = V4L2_CID_FOCUS_AUTO,
  810. .master_manual = 0,
  811. },
  812. {
  813. .id = V4L2_CID_FOCUS_AUTO,
  814. .entity = UVC_GUID_UVC_CAMERA,
  815. .selector = UVC_CT_FOCUS_AUTO_CONTROL,
  816. .size = 1,
  817. .offset = 0,
  818. .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
  819. .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
  820. .slave_ids = { V4L2_CID_FOCUS_ABSOLUTE, },
  821. },
  822. {
  823. .id = V4L2_CID_IRIS_ABSOLUTE,
  824. .entity = UVC_GUID_UVC_CAMERA,
  825. .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL,
  826. .size = 16,
  827. .offset = 0,
  828. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  829. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  830. },
  831. {
  832. .id = V4L2_CID_IRIS_RELATIVE,
  833. .entity = UVC_GUID_UVC_CAMERA,
  834. .selector = UVC_CT_IRIS_RELATIVE_CONTROL,
  835. .size = 8,
  836. .offset = 0,
  837. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  838. .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
  839. },
  840. {
  841. .id = V4L2_CID_ZOOM_ABSOLUTE,
  842. .entity = UVC_GUID_UVC_CAMERA,
  843. .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
  844. .size = 16,
  845. .offset = 0,
  846. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  847. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  848. },
  849. {
  850. .id = V4L2_CID_ZOOM_CONTINUOUS,
  851. .entity = UVC_GUID_UVC_CAMERA,
  852. .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
  853. .size = 0,
  854. .offset = 0,
  855. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  856. .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
  857. .get = uvc_ctrl_get_zoom,
  858. .set = uvc_ctrl_set_zoom,
  859. },
  860. {
  861. .id = V4L2_CID_PAN_ABSOLUTE,
  862. .entity = UVC_GUID_UVC_CAMERA,
  863. .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
  864. .size = 32,
  865. .offset = 0,
  866. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  867. .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
  868. },
  869. {
  870. .id = V4L2_CID_TILT_ABSOLUTE,
  871. .entity = UVC_GUID_UVC_CAMERA,
  872. .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
  873. .size = 32,
  874. .offset = 32,
  875. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  876. .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
  877. },
  878. {
  879. .id = V4L2_CID_PAN_SPEED,
  880. .entity = UVC_GUID_UVC_CAMERA,
  881. .selector = UVC_CT_PANTILT_RELATIVE_CONTROL,
  882. .size = 16,
  883. .offset = 0,
  884. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  885. .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
  886. .get = uvc_ctrl_get_rel_speed,
  887. .set = uvc_ctrl_set_rel_speed,
  888. },
  889. {
  890. .id = V4L2_CID_TILT_SPEED,
  891. .entity = UVC_GUID_UVC_CAMERA,
  892. .selector = UVC_CT_PANTILT_RELATIVE_CONTROL,
  893. .size = 16,
  894. .offset = 16,
  895. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  896. .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
  897. .get = uvc_ctrl_get_rel_speed,
  898. .set = uvc_ctrl_set_rel_speed,
  899. },
  900. {
  901. .id = V4L2_CID_PRIVACY,
  902. .entity = UVC_GUID_UVC_CAMERA,
  903. .selector = UVC_CT_PRIVACY_CONTROL,
  904. .size = 1,
  905. .offset = 0,
  906. .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
  907. .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
  908. },
  909. {
  910. .id = V4L2_CID_PRIVACY,
  911. .entity = UVC_GUID_EXT_GPIO_CONTROLLER,
  912. .selector = UVC_CT_PRIVACY_CONTROL,
  913. .size = 1,
  914. .offset = 0,
  915. .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
  916. .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
  917. },
  918. {
  919. .entity = UVC_GUID_UVC_PROCESSING,
  920. .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
  921. .filter_mapping = uvc_ctrl_filter_plf_mapping,
  922. },
  923. {
  924. .id = V4L2_CID_UVC_REGION_OF_INTEREST_RECT,
  925. .entity = UVC_GUID_UVC_CAMERA,
  926. .selector = UVC_CT_REGION_OF_INTEREST_CONTROL,
  927. .size = sizeof(struct uvc_rect) * 8,
  928. .offset = 0,
  929. .v4l2_type = V4L2_CTRL_TYPE_RECT,
  930. .data_type = UVC_CTRL_DATA_TYPE_RECT,
  931. .get = uvc_get_rect,
  932. .set = uvc_set_rect,
  933. .name = "Region of Interest Rectangle",
  934. },
  935. {
  936. .id = V4L2_CID_UVC_REGION_OF_INTEREST_AUTO,
  937. .entity = UVC_GUID_UVC_CAMERA,
  938. .selector = UVC_CT_REGION_OF_INTEREST_CONTROL,
  939. .size = 16,
  940. .offset = 64,
  941. .v4l2_type = V4L2_CTRL_TYPE_BITMASK,
  942. .data_type = UVC_CTRL_DATA_TYPE_BITMASK,
  943. .name = "Region of Interest Auto Ctrls",
  944. },
  945. {
  946. .id = V4L2_CID_COLORFX,
  947. .entity = UVC_GUID_CHROMEOS_XU,
  948. .selector = UVC_CROSXU_CONTROL_IQ_PROFILE,
  949. .size = 8,
  950. .offset = 0,
  951. .v4l2_type = V4L2_CTRL_TYPE_MENU,
  952. .data_type = UVC_CTRL_DATA_TYPE_ENUM,
  953. .menu_mapping = cros_colorfx_mapping,
  954. .menu_mask = BIT(V4L2_COLORFX_VIVID) |
  955. BIT(V4L2_COLORFX_NONE),
  956. },
  957. };
  958. /* ------------------------------------------------------------------------
  959. * Utility functions
  960. */
  961. static inline u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id)
  962. {
  963. return ctrl->uvc_data + id * ctrl->info.size;
  964. }
  965. static inline int uvc_test_bit(const u8 *data, int bit)
  966. {
  967. return (data[bit >> 3] >> (bit & 7)) & 1;
  968. }
  969. static inline void uvc_clear_bit(u8 *data, int bit)
  970. {
  971. data[bit >> 3] &= ~(1 << (bit & 7));
  972. }
  973. static s32 uvc_menu_to_v4l2_menu(struct uvc_control_mapping *mapping, s32 val)
  974. {
  975. unsigned int i;
  976. for (i = 0; BIT(i) <= mapping->menu_mask; ++i) {
  977. u32 menu_value;
  978. if (!test_bit(i, &mapping->menu_mask))
  979. continue;
  980. menu_value = uvc_mapping_get_menu_value(mapping, i);
  981. if (menu_value == val)
  982. return i;
  983. }
  984. return val;
  985. }
  986. /*
  987. * Extract the bit string specified by mapping->offset and mapping->size
  988. * from the little-endian data stored at 'data' and return the result as
  989. * a signed 32bit integer. Sign extension will be performed if the mapping
  990. * references a signed data type.
  991. */
  992. static int uvc_get_le_value(struct uvc_control_mapping *mapping,
  993. u8 query, const void *uvc_in, size_t v4l2_size,
  994. void *v4l2_out)
  995. {
  996. int offset = mapping->offset;
  997. int bits = mapping->size;
  998. const u8 *data = uvc_in;
  999. s32 *out = v4l2_out;
  1000. s32 value = 0;
  1001. u8 mask;
  1002. if (WARN_ON(v4l2_size != sizeof(s32)))
  1003. return -EINVAL;
  1004. data += offset / 8;
  1005. offset &= 7;
  1006. mask = ((1LL << bits) - 1) << offset;
  1007. while (1) {
  1008. u8 byte = *data & mask;
  1009. value |= offset > 0 ? (byte >> offset) : (byte << (-offset));
  1010. bits -= 8 - max(offset, 0);
  1011. if (bits <= 0)
  1012. break;
  1013. offset -= 8;
  1014. mask = (1 << bits) - 1;
  1015. data++;
  1016. }
  1017. /* Sign-extend the value if needed. */
  1018. if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
  1019. value |= -(value & (1 << (mapping->size - 1)));
  1020. /* If it is a menu, convert from uvc to v4l2. */
  1021. if (mapping->v4l2_type != V4L2_CTRL_TYPE_MENU) {
  1022. *out = value;
  1023. return 0;
  1024. }
  1025. switch (query) {
  1026. case UVC_GET_CUR:
  1027. case UVC_GET_DEF:
  1028. *out = uvc_menu_to_v4l2_menu(mapping, value);
  1029. return 0;
  1030. }
  1031. *out = value;
  1032. return 0;
  1033. }
  1034. /*
  1035. * Set the bit string specified by mapping->offset and mapping->size
  1036. * in the little-endian data stored at 'data' to the value 'value'.
  1037. */
  1038. static int uvc_set_le_value(struct uvc_control_mapping *mapping,
  1039. size_t v4l2_size, const void *v4l2_in,
  1040. void *uvc_out)
  1041. {
  1042. int offset = mapping->offset;
  1043. int bits = mapping->size;
  1044. u8 *data = uvc_out;
  1045. s32 value;
  1046. u8 mask;
  1047. if (WARN_ON(v4l2_size != sizeof(s32)))
  1048. return -EINVAL;
  1049. value = *(s32 *)v4l2_in;
  1050. switch (mapping->v4l2_type) {
  1051. case V4L2_CTRL_TYPE_MENU:
  1052. value = uvc_mapping_get_menu_value(mapping, value);
  1053. break;
  1054. case V4L2_CTRL_TYPE_BUTTON:
  1055. /*
  1056. * According to the v4l2 spec, writing any value to a button
  1057. * control should result in the action belonging to the button
  1058. * control being triggered. UVC devices however want to see a 1
  1059. * written -> override value.
  1060. */
  1061. value = -1;
  1062. break;
  1063. default:
  1064. break;
  1065. }
  1066. data += offset / 8;
  1067. offset &= 7;
  1068. for (; bits > 0; data++) {
  1069. mask = ((1LL << bits) - 1) << offset;
  1070. *data = (*data & ~mask) | ((value << offset) & mask);
  1071. value >>= offset ? offset : 8;
  1072. bits -= 8 - offset;
  1073. offset = 0;
  1074. }
  1075. return 0;
  1076. }
  1077. /* ------------------------------------------------------------------------
  1078. * Terminal and unit management
  1079. */
  1080. static int uvc_entity_match_guid(const struct uvc_entity *entity,
  1081. const u8 guid[16])
  1082. {
  1083. return memcmp(entity->guid, guid, sizeof(entity->guid)) == 0;
  1084. }
  1085. /* ------------------------------------------------------------------------
  1086. * UVC Controls
  1087. */
  1088. static void __uvc_find_control(struct uvc_entity *entity, u32 v4l2_id,
  1089. struct uvc_control_mapping **mapping, struct uvc_control **control,
  1090. int next, int next_compound)
  1091. {
  1092. struct uvc_control *ctrl;
  1093. struct uvc_control_mapping *map;
  1094. unsigned int i;
  1095. if (entity == NULL)
  1096. return;
  1097. for (i = 0; i < entity->ncontrols; ++i) {
  1098. ctrl = &entity->controls[i];
  1099. if (!ctrl->initialized)
  1100. continue;
  1101. list_for_each_entry(map, &ctrl->info.mappings, list) {
  1102. if (map->id == v4l2_id && !next && !next_compound) {
  1103. *control = ctrl;
  1104. *mapping = map;
  1105. return;
  1106. }
  1107. if ((*mapping == NULL || (*mapping)->id > map->id) &&
  1108. (map->id > v4l2_id) &&
  1109. (uvc_ctrl_mapping_is_compound(map) ?
  1110. next_compound : next)) {
  1111. *control = ctrl;
  1112. *mapping = map;
  1113. }
  1114. }
  1115. }
  1116. }
  1117. static struct uvc_control *uvc_find_control(struct uvc_video_chain *chain,
  1118. u32 v4l2_id, struct uvc_control_mapping **mapping)
  1119. {
  1120. struct uvc_control *ctrl = NULL;
  1121. struct uvc_entity *entity;
  1122. int next = v4l2_id & V4L2_CTRL_FLAG_NEXT_CTRL;
  1123. int next_compound = v4l2_id & V4L2_CTRL_FLAG_NEXT_COMPOUND;
  1124. *mapping = NULL;
  1125. /* Mask the query flags. */
  1126. v4l2_id &= V4L2_CTRL_ID_MASK;
  1127. /* Find the control. */
  1128. list_for_each_entry(entity, &chain->entities, chain) {
  1129. __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next,
  1130. next_compound);
  1131. if (ctrl && !next && !next_compound)
  1132. return ctrl;
  1133. }
  1134. if (!ctrl && !next && !next_compound)
  1135. uvc_dbg(chain->dev, CONTROL, "Control 0x%08x not found\n",
  1136. v4l2_id);
  1137. return ctrl;
  1138. }
  1139. static int uvc_ctrl_populate_cache(struct uvc_video_chain *chain,
  1140. struct uvc_control *ctrl)
  1141. {
  1142. int ret;
  1143. if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
  1144. ret = uvc_query_ctrl(chain->dev, UVC_GET_DEF, ctrl->entity->id,
  1145. chain->dev->intfnum, ctrl->info.selector,
  1146. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF),
  1147. ctrl->info.size);
  1148. if (ret < 0)
  1149. return ret;
  1150. }
  1151. if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN) {
  1152. ret = uvc_query_ctrl(chain->dev, UVC_GET_MIN, ctrl->entity->id,
  1153. chain->dev->intfnum, ctrl->info.selector,
  1154. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN),
  1155. ctrl->info.size);
  1156. if (ret < 0)
  1157. return ret;
  1158. }
  1159. if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX) {
  1160. ret = uvc_query_ctrl(chain->dev, UVC_GET_MAX, ctrl->entity->id,
  1161. chain->dev->intfnum, ctrl->info.selector,
  1162. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX),
  1163. ctrl->info.size);
  1164. if (ret < 0)
  1165. return ret;
  1166. }
  1167. if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES) {
  1168. ret = uvc_query_ctrl(chain->dev, UVC_GET_RES, ctrl->entity->id,
  1169. chain->dev->intfnum, ctrl->info.selector,
  1170. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES),
  1171. ctrl->info.size);
  1172. if (ret < 0) {
  1173. if (UVC_ENTITY_TYPE(ctrl->entity) !=
  1174. UVC_VC_EXTENSION_UNIT)
  1175. return ret;
  1176. /*
  1177. * GET_RES is mandatory for XU controls, but some
  1178. * cameras still choke on it. Ignore errors and set the
  1179. * resolution value to zero.
  1180. */
  1181. uvc_warn_once(chain->dev, UVC_WARN_XU_GET_RES,
  1182. "UVC non compliance - GET_RES failed on "
  1183. "an XU control. Enabling workaround.\n");
  1184. memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES), 0,
  1185. ctrl->info.size);
  1186. }
  1187. }
  1188. ctrl->cached = 1;
  1189. return 0;
  1190. }
  1191. static int __uvc_ctrl_load_cur(struct uvc_video_chain *chain,
  1192. struct uvc_control *ctrl)
  1193. {
  1194. u8 *data;
  1195. int ret;
  1196. if (ctrl->loaded)
  1197. return 0;
  1198. data = uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT);
  1199. if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0) {
  1200. memset(data, 0, ctrl->info.size);
  1201. ctrl->loaded = 1;
  1202. return 0;
  1203. }
  1204. if (ctrl->entity->get_cur)
  1205. ret = ctrl->entity->get_cur(chain->dev, ctrl->entity,
  1206. ctrl->info.selector, data,
  1207. ctrl->info.size);
  1208. else
  1209. ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
  1210. ctrl->entity->id, chain->dev->intfnum,
  1211. ctrl->info.selector, data,
  1212. ctrl->info.size);
  1213. if (ret < 0)
  1214. return ret;
  1215. ctrl->loaded = 1;
  1216. return ret;
  1217. }
  1218. static int __uvc_ctrl_get(struct uvc_video_chain *chain,
  1219. struct uvc_control *ctrl,
  1220. struct uvc_control_mapping *mapping,
  1221. s32 *value)
  1222. {
  1223. int ret;
  1224. if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0)
  1225. return -EACCES;
  1226. ret = __uvc_ctrl_load_cur(chain, ctrl);
  1227. if (ret < 0)
  1228. return ret;
  1229. *value = uvc_mapping_get_s32(mapping, UVC_GET_CUR,
  1230. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
  1231. return 0;
  1232. }
  1233. static int __uvc_query_v4l2_class(struct uvc_video_chain *chain, u32 req_id,
  1234. u32 found_id)
  1235. {
  1236. bool find_next = req_id &
  1237. (V4L2_CTRL_FLAG_NEXT_CTRL | V4L2_CTRL_FLAG_NEXT_COMPOUND);
  1238. unsigned int i;
  1239. req_id &= V4L2_CTRL_ID_MASK;
  1240. for (i = 0; i < ARRAY_SIZE(uvc_control_classes); i++) {
  1241. if (!(chain->ctrl_class_bitmap & BIT(i)))
  1242. continue;
  1243. if (!find_next) {
  1244. if (uvc_control_classes[i] == req_id)
  1245. return i;
  1246. continue;
  1247. }
  1248. if (uvc_control_classes[i] > req_id &&
  1249. uvc_control_classes[i] < found_id)
  1250. return i;
  1251. }
  1252. return -ENODEV;
  1253. }
  1254. static int uvc_query_v4l2_class(struct uvc_video_chain *chain, u32 req_id,
  1255. u32 found_id,
  1256. struct v4l2_query_ext_ctrl *v4l2_ctrl)
  1257. {
  1258. int idx;
  1259. idx = __uvc_query_v4l2_class(chain, req_id, found_id);
  1260. if (idx < 0)
  1261. return -ENODEV;
  1262. memset(v4l2_ctrl, 0, sizeof(*v4l2_ctrl));
  1263. v4l2_ctrl->id = uvc_control_classes[idx];
  1264. strscpy(v4l2_ctrl->name, v4l2_ctrl_get_name(v4l2_ctrl->id),
  1265. sizeof(v4l2_ctrl->name));
  1266. v4l2_ctrl->type = V4L2_CTRL_TYPE_CTRL_CLASS;
  1267. v4l2_ctrl->flags = V4L2_CTRL_FLAG_WRITE_ONLY
  1268. | V4L2_CTRL_FLAG_READ_ONLY;
  1269. return 0;
  1270. }
  1271. static bool uvc_ctrl_is_readable(u32 which, struct uvc_control *ctrl,
  1272. struct uvc_control_mapping *mapping)
  1273. {
  1274. if (which == V4L2_CTRL_WHICH_CUR_VAL)
  1275. return !!(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR);
  1276. if (which == V4L2_CTRL_WHICH_DEF_VAL)
  1277. return !!(ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF);
  1278. /* Types with implicit boundaries. */
  1279. switch (mapping->v4l2_type) {
  1280. case V4L2_CTRL_TYPE_MENU:
  1281. case V4L2_CTRL_TYPE_BOOLEAN:
  1282. case V4L2_CTRL_TYPE_BUTTON:
  1283. return true;
  1284. case V4L2_CTRL_TYPE_BITMASK:
  1285. return (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES) ||
  1286. (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX);
  1287. default:
  1288. break;
  1289. }
  1290. if (which == V4L2_CTRL_WHICH_MIN_VAL)
  1291. return !!(ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN);
  1292. if (which == V4L2_CTRL_WHICH_MAX_VAL)
  1293. return !!(ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX);
  1294. return false;
  1295. }
  1296. /*
  1297. * Check if control @v4l2_id can be accessed by the given control @ioctl
  1298. * (VIDIOC_G_EXT_CTRLS, VIDIOC_TRY_EXT_CTRLS or VIDIOC_S_EXT_CTRLS).
  1299. *
  1300. * For set operations on slave controls, check if the master's value is set to
  1301. * manual, either in the others controls set in the same ioctl call, or from
  1302. * the master's current value. This catches VIDIOC_S_EXT_CTRLS calls that set
  1303. * both the master and slave control, such as for instance setting
  1304. * auto_exposure=1, exposure_time_absolute=251.
  1305. */
  1306. int uvc_ctrl_is_accessible(struct uvc_video_chain *chain, u32 v4l2_id,
  1307. const struct v4l2_ext_controls *ctrls, u32 which,
  1308. unsigned long ioctl)
  1309. {
  1310. struct uvc_control_mapping *master_map = NULL;
  1311. struct uvc_control *master_ctrl = NULL;
  1312. struct uvc_control_mapping *mapping;
  1313. struct uvc_control *ctrl;
  1314. s32 val;
  1315. int ret;
  1316. int i;
  1317. /*
  1318. * There is no need to check the ioctl, all the ioctls except
  1319. * VIDIOC_G_EXT_CTRLS use which=V4L2_CTRL_WHICH_CUR_VAL.
  1320. */
  1321. bool is_which_min_max = which == V4L2_CTRL_WHICH_MIN_VAL ||
  1322. which == V4L2_CTRL_WHICH_MAX_VAL;
  1323. if (__uvc_query_v4l2_class(chain, v4l2_id, 0) >= 0)
  1324. return is_which_min_max ? -EINVAL : -EACCES;
  1325. ctrl = uvc_find_control(chain, v4l2_id, &mapping);
  1326. if (!ctrl)
  1327. return -EINVAL;
  1328. if ((!(ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN) ||
  1329. !(ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX)) && is_which_min_max)
  1330. return -EINVAL;
  1331. if (ioctl == VIDIOC_G_EXT_CTRLS)
  1332. return uvc_ctrl_is_readable(ctrls->which, ctrl, mapping);
  1333. if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
  1334. return -EACCES;
  1335. if (ioctl != VIDIOC_S_EXT_CTRLS || !mapping->master_id)
  1336. return 0;
  1337. /*
  1338. * Iterate backwards in cases where the master control is accessed
  1339. * multiple times in the same ioctl. We want the last value.
  1340. */
  1341. for (i = ctrls->count - 1; i >= 0; i--) {
  1342. if (ctrls->controls[i].id == mapping->master_id)
  1343. return ctrls->controls[i].value ==
  1344. mapping->master_manual ? 0 : -EACCES;
  1345. }
  1346. __uvc_find_control(ctrl->entity, mapping->master_id, &master_map,
  1347. &master_ctrl, 0, 0);
  1348. if (!master_ctrl || !(master_ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
  1349. return 0;
  1350. if (WARN_ON(uvc_ctrl_mapping_is_compound(master_map)))
  1351. return -EIO;
  1352. ret = __uvc_ctrl_get(chain, master_ctrl, master_map, &val);
  1353. if (ret >= 0 && val != mapping->master_manual)
  1354. return -EACCES;
  1355. return 0;
  1356. }
  1357. static const char *uvc_map_get_name(const struct uvc_control_mapping *map)
  1358. {
  1359. const char *name;
  1360. if (map->name)
  1361. return map->name;
  1362. name = v4l2_ctrl_get_name(map->id);
  1363. if (name)
  1364. return name;
  1365. return "Unknown Control";
  1366. }
  1367. static u32 uvc_get_ctrl_bitmap(struct uvc_control *ctrl,
  1368. struct uvc_control_mapping *mapping)
  1369. {
  1370. /*
  1371. * Some controls, like CT_AE_MODE_CONTROL, use GET_RES to represent
  1372. * the number of bits supported. Those controls do not list GET_MAX
  1373. * as supported.
  1374. */
  1375. if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)
  1376. return uvc_mapping_get_s32(mapping, UVC_GET_RES,
  1377. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
  1378. if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX)
  1379. return uvc_mapping_get_s32(mapping, UVC_GET_MAX,
  1380. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
  1381. return ~0;
  1382. }
  1383. static bool uvc_ctrl_is_relative_ptz(__u32 ctrl_id)
  1384. {
  1385. switch (ctrl_id) {
  1386. case V4L2_CID_ZOOM_CONTINUOUS:
  1387. case V4L2_CID_PAN_SPEED:
  1388. case V4L2_CID_TILT_SPEED:
  1389. return true;
  1390. }
  1391. return false;
  1392. }
  1393. /*
  1394. * Maximum retry count to avoid spurious errors with controls. Increasing this
  1395. * value does no seem to produce better results in the tested hardware.
  1396. */
  1397. #define MAX_QUERY_RETRIES 2
  1398. static int __uvc_queryctrl_boundaries(struct uvc_video_chain *chain,
  1399. struct uvc_control *ctrl,
  1400. struct uvc_control_mapping *mapping,
  1401. struct v4l2_query_ext_ctrl *v4l2_ctrl)
  1402. {
  1403. if (!ctrl->cached) {
  1404. unsigned int retries;
  1405. int ret;
  1406. for (retries = 0; retries < MAX_QUERY_RETRIES; retries++) {
  1407. ret = uvc_ctrl_populate_cache(chain, ctrl);
  1408. if (ret != -EIO)
  1409. break;
  1410. }
  1411. if (ret)
  1412. return ret;
  1413. }
  1414. if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
  1415. v4l2_ctrl->default_value = uvc_mapping_get_s32(mapping,
  1416. UVC_GET_DEF, uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF));
  1417. }
  1418. switch (mapping->v4l2_type) {
  1419. case V4L2_CTRL_TYPE_MENU:
  1420. v4l2_ctrl->minimum = ffs(mapping->menu_mask) - 1;
  1421. v4l2_ctrl->maximum = fls(mapping->menu_mask) - 1;
  1422. v4l2_ctrl->step = 1;
  1423. return 0;
  1424. case V4L2_CTRL_TYPE_BOOLEAN:
  1425. v4l2_ctrl->minimum = 0;
  1426. v4l2_ctrl->maximum = 1;
  1427. v4l2_ctrl->step = 1;
  1428. return 0;
  1429. case V4L2_CTRL_TYPE_BUTTON:
  1430. v4l2_ctrl->minimum = 0;
  1431. v4l2_ctrl->maximum = 0;
  1432. v4l2_ctrl->step = 0;
  1433. return 0;
  1434. case V4L2_CTRL_TYPE_BITMASK:
  1435. v4l2_ctrl->minimum = 0;
  1436. v4l2_ctrl->maximum = uvc_get_ctrl_bitmap(ctrl, mapping);
  1437. v4l2_ctrl->step = 0;
  1438. return 0;
  1439. default:
  1440. break;
  1441. }
  1442. if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX)
  1443. v4l2_ctrl->maximum = uvc_mapping_get_s32(mapping, UVC_GET_MAX,
  1444. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
  1445. else
  1446. v4l2_ctrl->maximum = 0;
  1447. if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN) {
  1448. /*
  1449. * For relative PTZ controls, UVC_GET_MIN for
  1450. * b(Pan|Tilt|Zoom)Speed returns the minimum speed of the
  1451. * movement in direction specified in the sign field.
  1452. * See in USB Device Class Definition for Video Devices:
  1453. * 4.2.2.1.13 Zoom (Relative) Control
  1454. * 4.2.2.1.15 PanTilt (Relative) Control
  1455. *
  1456. * For minimum value, use maximum speed but in negative direction.
  1457. */
  1458. if (uvc_ctrl_is_relative_ptz(v4l2_ctrl->id))
  1459. v4l2_ctrl->minimum = -v4l2_ctrl->maximum;
  1460. else
  1461. v4l2_ctrl->minimum = uvc_mapping_get_s32(mapping,
  1462. UVC_GET_MIN, uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
  1463. } else {
  1464. v4l2_ctrl->minimum = 0;
  1465. }
  1466. if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)
  1467. v4l2_ctrl->step = uvc_mapping_get_s32(mapping, UVC_GET_RES,
  1468. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
  1469. else
  1470. v4l2_ctrl->step = 0;
  1471. return 0;
  1472. }
  1473. static size_t uvc_mapping_v4l2_size(struct uvc_control_mapping *mapping)
  1474. {
  1475. if (mapping->v4l2_type == V4L2_CTRL_TYPE_RECT)
  1476. return sizeof(struct v4l2_rect);
  1477. if (uvc_ctrl_mapping_is_compound(mapping))
  1478. return DIV_ROUND_UP(mapping->size, 8);
  1479. return sizeof(s32);
  1480. }
  1481. static int __uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
  1482. struct uvc_control *ctrl,
  1483. struct uvc_control_mapping *mapping,
  1484. struct v4l2_query_ext_ctrl *v4l2_ctrl)
  1485. {
  1486. struct uvc_control_mapping *master_map = NULL;
  1487. struct uvc_control *master_ctrl = NULL;
  1488. int ret;
  1489. memset(v4l2_ctrl, 0, sizeof(*v4l2_ctrl));
  1490. v4l2_ctrl->id = mapping->id;
  1491. v4l2_ctrl->type = mapping->v4l2_type;
  1492. strscpy(v4l2_ctrl->name, uvc_map_get_name(mapping),
  1493. sizeof(v4l2_ctrl->name));
  1494. v4l2_ctrl->flags = 0;
  1495. if (!(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
  1496. v4l2_ctrl->flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
  1497. if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
  1498. v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  1499. if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX) &&
  1500. (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN))
  1501. v4l2_ctrl->flags |= V4L2_CTRL_FLAG_HAS_WHICH_MIN_MAX;
  1502. if (mapping->master_id)
  1503. __uvc_find_control(ctrl->entity, mapping->master_id,
  1504. &master_map, &master_ctrl, 0, 0);
  1505. if (master_ctrl && (master_ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR)) {
  1506. unsigned int retries;
  1507. s32 val;
  1508. int ret;
  1509. if (WARN_ON(uvc_ctrl_mapping_is_compound(master_map)))
  1510. return -EIO;
  1511. for (retries = 0; retries < MAX_QUERY_RETRIES; retries++) {
  1512. ret = __uvc_ctrl_get(chain, master_ctrl, master_map,
  1513. &val);
  1514. if (!ret)
  1515. break;
  1516. if (ret < 0 && ret != -EIO)
  1517. return ret;
  1518. }
  1519. if (ret == -EIO) {
  1520. dev_warn_ratelimited(&chain->dev->intf->dev,
  1521. "UVC non compliance: Error %d querying master control %x (%s)\n",
  1522. ret, master_map->id,
  1523. uvc_map_get_name(master_map));
  1524. } else {
  1525. if (val != mapping->master_manual)
  1526. v4l2_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
  1527. }
  1528. }
  1529. v4l2_ctrl->elem_size = uvc_mapping_v4l2_size(mapping);
  1530. v4l2_ctrl->elems = 1;
  1531. if (v4l2_ctrl->type >= V4L2_CTRL_COMPOUND_TYPES) {
  1532. v4l2_ctrl->flags |= V4L2_CTRL_FLAG_HAS_PAYLOAD;
  1533. v4l2_ctrl->default_value = 0;
  1534. v4l2_ctrl->minimum = 0;
  1535. v4l2_ctrl->maximum = 0;
  1536. v4l2_ctrl->step = 0;
  1537. return 0;
  1538. }
  1539. ret = __uvc_queryctrl_boundaries(chain, ctrl, mapping, v4l2_ctrl);
  1540. if (ret && !mapping->disabled) {
  1541. dev_warn(&chain->dev->intf->dev,
  1542. "UVC non compliance: permanently disabling control %x (%s), due to error %d\n",
  1543. mapping->id, uvc_map_get_name(mapping), ret);
  1544. mapping->disabled = true;
  1545. }
  1546. if (mapping->disabled)
  1547. v4l2_ctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
  1548. return 0;
  1549. }
  1550. int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
  1551. struct v4l2_query_ext_ctrl *v4l2_ctrl)
  1552. {
  1553. struct uvc_control *ctrl;
  1554. struct uvc_control_mapping *mapping;
  1555. int ret;
  1556. ret = mutex_lock_interruptible(&chain->ctrl_mutex);
  1557. if (ret < 0)
  1558. return -ERESTARTSYS;
  1559. /* Check if the ctrl is a know class */
  1560. if (!(v4l2_ctrl->id & V4L2_CTRL_FLAG_NEXT_CTRL)) {
  1561. ret = uvc_query_v4l2_class(chain, v4l2_ctrl->id, 0, v4l2_ctrl);
  1562. if (!ret)
  1563. goto done;
  1564. }
  1565. ctrl = uvc_find_control(chain, v4l2_ctrl->id, &mapping);
  1566. if (ctrl == NULL) {
  1567. ret = -EINVAL;
  1568. goto done;
  1569. }
  1570. /*
  1571. * If we're enumerating control with V4L2_CTRL_FLAG_NEXT_CTRL, check if
  1572. * a class should be inserted between the previous control and the one
  1573. * we have just found.
  1574. */
  1575. if (v4l2_ctrl->id & V4L2_CTRL_FLAG_NEXT_CTRL) {
  1576. ret = uvc_query_v4l2_class(chain, v4l2_ctrl->id, mapping->id,
  1577. v4l2_ctrl);
  1578. if (!ret)
  1579. goto done;
  1580. }
  1581. ret = __uvc_query_v4l2_ctrl(chain, ctrl, mapping, v4l2_ctrl);
  1582. done:
  1583. mutex_unlock(&chain->ctrl_mutex);
  1584. return ret;
  1585. }
  1586. /*
  1587. * Mapping V4L2 controls to UVC controls can be straightforward if done well.
  1588. * Most of the UVC controls exist in V4L2, and can be mapped directly. Some
  1589. * must be grouped (for instance the Red Balance, Blue Balance and Do White
  1590. * Balance V4L2 controls use the White Balance Component UVC control) or
  1591. * otherwise translated. The approach we take here is to use a translation
  1592. * table for the controls that can be mapped directly, and handle the others
  1593. * manually.
  1594. */
  1595. int uvc_query_v4l2_menu(struct uvc_video_chain *chain,
  1596. struct v4l2_querymenu *query_menu)
  1597. {
  1598. struct uvc_control_mapping *mapping;
  1599. struct uvc_control *ctrl;
  1600. u32 index = query_menu->index;
  1601. u32 id = query_menu->id;
  1602. const char *name;
  1603. int ret;
  1604. memset(query_menu, 0, sizeof(*query_menu));
  1605. query_menu->id = id;
  1606. query_menu->index = index;
  1607. if (index >= BITS_PER_TYPE(mapping->menu_mask))
  1608. return -EINVAL;
  1609. ret = mutex_lock_interruptible(&chain->ctrl_mutex);
  1610. if (ret < 0)
  1611. return -ERESTARTSYS;
  1612. ctrl = uvc_find_control(chain, query_menu->id, &mapping);
  1613. if (ctrl == NULL || mapping->v4l2_type != V4L2_CTRL_TYPE_MENU) {
  1614. ret = -EINVAL;
  1615. goto done;
  1616. }
  1617. if (!test_bit(query_menu->index, &mapping->menu_mask)) {
  1618. ret = -EINVAL;
  1619. goto done;
  1620. }
  1621. if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK) {
  1622. int mask;
  1623. if (!ctrl->cached) {
  1624. ret = uvc_ctrl_populate_cache(chain, ctrl);
  1625. if (ret < 0)
  1626. goto done;
  1627. }
  1628. mask = uvc_mapping_get_menu_value(mapping, query_menu->index);
  1629. if (mask < 0) {
  1630. ret = mask;
  1631. goto done;
  1632. }
  1633. if (!(uvc_get_ctrl_bitmap(ctrl, mapping) & mask)) {
  1634. ret = -EINVAL;
  1635. goto done;
  1636. }
  1637. }
  1638. name = uvc_mapping_get_menu_name(mapping, query_menu->index);
  1639. if (!name) {
  1640. ret = -EINVAL;
  1641. goto done;
  1642. }
  1643. strscpy(query_menu->name, name, sizeof(query_menu->name));
  1644. done:
  1645. mutex_unlock(&chain->ctrl_mutex);
  1646. return ret;
  1647. }
  1648. /* --------------------------------------------------------------------------
  1649. * Ctrl event handling
  1650. */
  1651. static void uvc_ctrl_fill_event(struct uvc_video_chain *chain,
  1652. struct v4l2_event *ev,
  1653. struct uvc_control *ctrl,
  1654. struct uvc_control_mapping *mapping,
  1655. s32 value, u32 changes)
  1656. {
  1657. struct v4l2_query_ext_ctrl v4l2_ctrl;
  1658. __uvc_query_v4l2_ctrl(chain, ctrl, mapping, &v4l2_ctrl);
  1659. memset(ev, 0, sizeof(*ev));
  1660. ev->type = V4L2_EVENT_CTRL;
  1661. ev->id = v4l2_ctrl.id;
  1662. ev->u.ctrl.value = value;
  1663. ev->u.ctrl.changes = changes;
  1664. ev->u.ctrl.type = v4l2_ctrl.type;
  1665. ev->u.ctrl.flags = v4l2_ctrl.flags;
  1666. ev->u.ctrl.minimum = v4l2_ctrl.minimum;
  1667. ev->u.ctrl.maximum = v4l2_ctrl.maximum;
  1668. ev->u.ctrl.step = v4l2_ctrl.step;
  1669. ev->u.ctrl.default_value = v4l2_ctrl.default_value;
  1670. }
  1671. /*
  1672. * Send control change events to all subscribers for the @ctrl control. By
  1673. * default the subscriber that generated the event, as identified by @handle,
  1674. * is not notified unless it has set the V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK flag.
  1675. * @handle can be NULL for asynchronous events related to auto-update controls,
  1676. * in which case all subscribers are notified.
  1677. */
  1678. static void uvc_ctrl_send_event(struct uvc_video_chain *chain,
  1679. struct uvc_fh *handle, struct uvc_control *ctrl,
  1680. struct uvc_control_mapping *mapping, s32 value, u32 changes)
  1681. {
  1682. struct v4l2_fh *originator = handle ? &handle->vfh : NULL;
  1683. struct v4l2_subscribed_event *sev;
  1684. struct v4l2_event ev;
  1685. if (list_empty(&mapping->ev_subs))
  1686. return;
  1687. uvc_ctrl_fill_event(chain, &ev, ctrl, mapping, value, changes);
  1688. list_for_each_entry(sev, &mapping->ev_subs, node) {
  1689. if (sev->fh != originator ||
  1690. (sev->flags & V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK) ||
  1691. (changes & V4L2_EVENT_CTRL_CH_FLAGS))
  1692. v4l2_event_queue_fh(sev->fh, &ev);
  1693. }
  1694. }
  1695. /*
  1696. * Send control change events for the slave of the @master control identified
  1697. * by the V4L2 ID @slave_id. The @handle identifies the event subscriber that
  1698. * generated the event and may be NULL for auto-update events.
  1699. */
  1700. static void uvc_ctrl_send_slave_event(struct uvc_video_chain *chain,
  1701. struct uvc_fh *handle, struct uvc_control *master, u32 slave_id)
  1702. {
  1703. struct uvc_control_mapping *mapping = NULL;
  1704. struct uvc_control *ctrl = NULL;
  1705. u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
  1706. s32 val = 0;
  1707. __uvc_find_control(master->entity, slave_id, &mapping, &ctrl, 0, 0);
  1708. if (ctrl == NULL)
  1709. return;
  1710. if (uvc_ctrl_mapping_is_compound(mapping) ||
  1711. __uvc_ctrl_get(chain, ctrl, mapping, &val) == 0)
  1712. changes |= V4L2_EVENT_CTRL_CH_VALUE;
  1713. uvc_ctrl_send_event(chain, handle, ctrl, mapping, val, changes);
  1714. }
  1715. static int uvc_ctrl_set_handle(struct uvc_control *ctrl, struct uvc_fh *handle)
  1716. {
  1717. int ret;
  1718. lockdep_assert_held(&handle->chain->ctrl_mutex);
  1719. if (ctrl->handle) {
  1720. dev_warn_ratelimited(&handle->stream->dev->intf->dev,
  1721. "UVC non compliance: Setting an async control with a pending operation.");
  1722. if (ctrl->handle == handle)
  1723. return 0;
  1724. WARN_ON(!ctrl->handle->pending_async_ctrls);
  1725. if (ctrl->handle->pending_async_ctrls)
  1726. ctrl->handle->pending_async_ctrls--;
  1727. ctrl->handle = handle;
  1728. ctrl->handle->pending_async_ctrls++;
  1729. return 0;
  1730. }
  1731. ret = uvc_pm_get(handle->chain->dev);
  1732. if (ret)
  1733. return ret;
  1734. ctrl->handle = handle;
  1735. ctrl->handle->pending_async_ctrls++;
  1736. return 0;
  1737. }
  1738. static int uvc_ctrl_clear_handle(struct uvc_control *ctrl)
  1739. {
  1740. lockdep_assert_held(&ctrl->handle->chain->ctrl_mutex);
  1741. if (WARN_ON(!ctrl->handle->pending_async_ctrls)) {
  1742. ctrl->handle = NULL;
  1743. return -EINVAL;
  1744. }
  1745. ctrl->handle->pending_async_ctrls--;
  1746. uvc_pm_put(ctrl->handle->chain->dev);
  1747. ctrl->handle = NULL;
  1748. return 0;
  1749. }
  1750. void uvc_ctrl_status_event(struct uvc_video_chain *chain,
  1751. struct uvc_control *ctrl, const u8 *data)
  1752. {
  1753. struct uvc_control_mapping *mapping;
  1754. struct uvc_fh *handle;
  1755. unsigned int i;
  1756. mutex_lock(&chain->ctrl_mutex);
  1757. /* Flush the control cache, the data might have changed. */
  1758. ctrl->loaded = 0;
  1759. handle = ctrl->handle;
  1760. if (handle)
  1761. uvc_ctrl_clear_handle(ctrl);
  1762. list_for_each_entry(mapping, &ctrl->info.mappings, list) {
  1763. s32 value;
  1764. if (uvc_ctrl_mapping_is_compound(mapping))
  1765. value = 0;
  1766. else
  1767. value = uvc_mapping_get_s32(mapping, UVC_GET_CUR, data);
  1768. /*
  1769. * handle may be NULL here if the device sends auto-update
  1770. * events without a prior related control set from userspace.
  1771. */
  1772. for (i = 0; i < ARRAY_SIZE(mapping->slave_ids); ++i) {
  1773. if (!mapping->slave_ids[i])
  1774. break;
  1775. uvc_ctrl_send_slave_event(chain, handle, ctrl,
  1776. mapping->slave_ids[i]);
  1777. }
  1778. uvc_ctrl_send_event(chain, handle, ctrl, mapping, value,
  1779. V4L2_EVENT_CTRL_CH_VALUE);
  1780. }
  1781. mutex_unlock(&chain->ctrl_mutex);
  1782. }
  1783. static void uvc_ctrl_status_event_work(struct work_struct *work)
  1784. {
  1785. struct uvc_device *dev = container_of(work, struct uvc_device,
  1786. async_ctrl.work);
  1787. struct uvc_ctrl_work *w = &dev->async_ctrl;
  1788. int ret;
  1789. uvc_ctrl_status_event(w->chain, w->ctrl, w->data);
  1790. /* The barrier is needed to synchronize with uvc_status_stop(). */
  1791. if (smp_load_acquire(&dev->flush_status))
  1792. return;
  1793. /* Resubmit the URB. */
  1794. w->urb->interval = dev->int_ep->desc.bInterval;
  1795. ret = usb_submit_urb(w->urb, GFP_KERNEL);
  1796. if (ret < 0)
  1797. dev_err(&dev->intf->dev,
  1798. "Failed to resubmit status URB (%d).\n", ret);
  1799. }
  1800. bool uvc_ctrl_status_event_async(struct urb *urb, struct uvc_video_chain *chain,
  1801. struct uvc_control *ctrl, const u8 *data)
  1802. {
  1803. struct uvc_device *dev = chain->dev;
  1804. struct uvc_ctrl_work *w = &dev->async_ctrl;
  1805. if (list_empty(&ctrl->info.mappings))
  1806. return false;
  1807. w->data = data;
  1808. w->urb = urb;
  1809. w->chain = chain;
  1810. w->ctrl = ctrl;
  1811. schedule_work(&w->work);
  1812. return true;
  1813. }
  1814. static bool uvc_ctrl_xctrls_has_control(const struct v4l2_ext_control *xctrls,
  1815. unsigned int xctrls_count, u32 id)
  1816. {
  1817. unsigned int i;
  1818. for (i = 0; i < xctrls_count; ++i) {
  1819. if (xctrls[i].id == id)
  1820. return true;
  1821. }
  1822. return false;
  1823. }
  1824. static void uvc_ctrl_send_events(struct uvc_fh *handle,
  1825. struct uvc_entity *entity,
  1826. const struct v4l2_ext_control *xctrls,
  1827. unsigned int xctrls_count)
  1828. {
  1829. struct uvc_control_mapping *mapping;
  1830. struct uvc_control *ctrl;
  1831. unsigned int i;
  1832. unsigned int j;
  1833. for (i = 0; i < xctrls_count; ++i) {
  1834. u32 changes = V4L2_EVENT_CTRL_CH_VALUE;
  1835. s32 value;
  1836. ctrl = uvc_find_control(handle->chain, xctrls[i].id, &mapping);
  1837. if (ctrl->entity != entity)
  1838. continue;
  1839. if (ctrl->info.flags & UVC_CTRL_FLAG_ASYNCHRONOUS)
  1840. /* Notification will be sent from an Interrupt event. */
  1841. continue;
  1842. for (j = 0; j < ARRAY_SIZE(mapping->slave_ids); ++j) {
  1843. u32 slave_id = mapping->slave_ids[j];
  1844. if (!slave_id)
  1845. break;
  1846. /*
  1847. * We can skip sending an event for the slave if the
  1848. * slave is being modified in the same transaction.
  1849. */
  1850. if (uvc_ctrl_xctrls_has_control(xctrls, xctrls_count,
  1851. slave_id))
  1852. continue;
  1853. uvc_ctrl_send_slave_event(handle->chain, handle, ctrl,
  1854. slave_id);
  1855. }
  1856. if (uvc_ctrl_mapping_is_compound(mapping))
  1857. value = 0;
  1858. else
  1859. value = xctrls[i].value;
  1860. /*
  1861. * If the master is being modified in the same transaction
  1862. * flags may change too.
  1863. */
  1864. if (mapping->master_id &&
  1865. uvc_ctrl_xctrls_has_control(xctrls, xctrls_count,
  1866. mapping->master_id))
  1867. changes |= V4L2_EVENT_CTRL_CH_FLAGS;
  1868. uvc_ctrl_send_event(handle->chain, handle, ctrl, mapping,
  1869. value, changes);
  1870. }
  1871. }
  1872. static int uvc_ctrl_add_event(struct v4l2_subscribed_event *sev, unsigned elems)
  1873. {
  1874. struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);
  1875. struct uvc_control_mapping *mapping;
  1876. struct uvc_control *ctrl;
  1877. int ret;
  1878. ret = mutex_lock_interruptible(&handle->chain->ctrl_mutex);
  1879. if (ret < 0)
  1880. return -ERESTARTSYS;
  1881. if (__uvc_query_v4l2_class(handle->chain, sev->id, 0) >= 0) {
  1882. ret = 0;
  1883. goto done;
  1884. }
  1885. ctrl = uvc_find_control(handle->chain, sev->id, &mapping);
  1886. if (ctrl == NULL) {
  1887. ret = -EINVAL;
  1888. goto done;
  1889. }
  1890. if (sev->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL) {
  1891. struct v4l2_event ev;
  1892. u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
  1893. s32 val = 0;
  1894. ret = uvc_pm_get(handle->chain->dev);
  1895. if (ret)
  1896. goto done;
  1897. if (uvc_ctrl_mapping_is_compound(mapping) ||
  1898. __uvc_ctrl_get(handle->chain, ctrl, mapping, &val) == 0)
  1899. changes |= V4L2_EVENT_CTRL_CH_VALUE;
  1900. uvc_ctrl_fill_event(handle->chain, &ev, ctrl, mapping, val,
  1901. changes);
  1902. uvc_pm_put(handle->chain->dev);
  1903. /*
  1904. * Mark the queue as active, allowing this initial event to be
  1905. * accepted.
  1906. */
  1907. sev->elems = elems;
  1908. v4l2_event_queue_fh(sev->fh, &ev);
  1909. }
  1910. list_add_tail(&sev->node, &mapping->ev_subs);
  1911. done:
  1912. mutex_unlock(&handle->chain->ctrl_mutex);
  1913. return ret;
  1914. }
  1915. static void uvc_ctrl_del_event(struct v4l2_subscribed_event *sev)
  1916. {
  1917. struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);
  1918. mutex_lock(&handle->chain->ctrl_mutex);
  1919. if (__uvc_query_v4l2_class(handle->chain, sev->id, 0) >= 0)
  1920. goto done;
  1921. list_del(&sev->node);
  1922. done:
  1923. mutex_unlock(&handle->chain->ctrl_mutex);
  1924. }
  1925. const struct v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops = {
  1926. .add = uvc_ctrl_add_event,
  1927. .del = uvc_ctrl_del_event,
  1928. .replace = v4l2_ctrl_replace,
  1929. .merge = v4l2_ctrl_merge,
  1930. };
  1931. /* --------------------------------------------------------------------------
  1932. * Control transactions
  1933. *
  1934. * To make extended set operations as atomic as the hardware allows, controls
  1935. * are handled using begin/commit/rollback operations.
  1936. *
  1937. * At the beginning of a set request, uvc_ctrl_begin should be called to
  1938. * initialize the request. This function acquires the control lock.
  1939. *
  1940. * When setting a control, the new value is stored in the control data field
  1941. * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
  1942. * later processing. If the UVC and V4L2 control sizes differ, the current
  1943. * value is loaded from the hardware before storing the new value in the data
  1944. * field.
  1945. *
  1946. * After processing all controls in the transaction, uvc_ctrl_commit or
  1947. * uvc_ctrl_rollback must be called to apply the pending changes to the
  1948. * hardware or revert them. When applying changes, all controls marked as
  1949. * dirty will be modified in the UVC device, and the dirty flag will be
  1950. * cleared. When reverting controls, the control data field
  1951. * UVC_CTRL_DATA_CURRENT is reverted to its previous value
  1952. * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
  1953. * control lock.
  1954. */
  1955. int uvc_ctrl_begin(struct uvc_video_chain *chain)
  1956. {
  1957. return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0;
  1958. }
  1959. /*
  1960. * Returns the number of uvc controls that have been correctly set, or a
  1961. * negative number if there has been an error.
  1962. */
  1963. static int uvc_ctrl_commit_entity(struct uvc_device *dev,
  1964. struct uvc_fh *handle,
  1965. struct uvc_entity *entity,
  1966. int rollback,
  1967. struct uvc_control **err_ctrl)
  1968. {
  1969. unsigned int processed_ctrls = 0;
  1970. struct uvc_control *ctrl;
  1971. unsigned int i;
  1972. int ret = 0;
  1973. if (entity == NULL)
  1974. return 0;
  1975. for (i = 0; i < entity->ncontrols; ++i) {
  1976. ctrl = &entity->controls[i];
  1977. if (!ctrl->initialized)
  1978. continue;
  1979. /*
  1980. * Reset the loaded flag for auto-update controls that were
  1981. * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
  1982. * uvc_ctrl_get from using the cached value, and for write-only
  1983. * controls to prevent uvc_ctrl_set from setting bits not
  1984. * explicitly set by the user.
  1985. */
  1986. if (ctrl->info.flags & UVC_CTRL_FLAG_AUTO_UPDATE ||
  1987. !(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
  1988. ctrl->loaded = 0;
  1989. if (!ctrl->dirty)
  1990. continue;
  1991. if (!rollback)
  1992. ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id,
  1993. dev->intfnum, ctrl->info.selector,
  1994. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  1995. ctrl->info.size);
  1996. if (!ret)
  1997. processed_ctrls++;
  1998. if (rollback || ret < 0)
  1999. memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  2000. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
  2001. ctrl->info.size);
  2002. ctrl->dirty = 0;
  2003. if (!rollback && handle && !ret &&
  2004. ctrl->info.flags & UVC_CTRL_FLAG_ASYNCHRONOUS)
  2005. ret = uvc_ctrl_set_handle(ctrl, handle);
  2006. if (ret < 0 && !rollback) {
  2007. if (err_ctrl)
  2008. *err_ctrl = ctrl;
  2009. /*
  2010. * If we fail to set a control, we need to rollback
  2011. * the next ones.
  2012. */
  2013. rollback = 1;
  2014. }
  2015. }
  2016. if (ret)
  2017. return ret;
  2018. return processed_ctrls;
  2019. }
  2020. static int uvc_ctrl_find_ctrl_idx(struct uvc_entity *entity,
  2021. struct v4l2_ext_controls *ctrls,
  2022. struct uvc_control *uvc_control)
  2023. {
  2024. struct uvc_control_mapping *mapping = NULL;
  2025. struct uvc_control *ctrl_found = NULL;
  2026. unsigned int i;
  2027. if (!entity)
  2028. return ctrls->count;
  2029. for (i = 0; i < ctrls->count; i++) {
  2030. __uvc_find_control(entity, ctrls->controls[i].id, &mapping,
  2031. &ctrl_found, 0, 0);
  2032. if (uvc_control == ctrl_found)
  2033. return i;
  2034. }
  2035. return ctrls->count;
  2036. }
  2037. int __uvc_ctrl_commit(struct uvc_fh *handle, int rollback,
  2038. struct v4l2_ext_controls *ctrls)
  2039. {
  2040. struct uvc_video_chain *chain = handle->chain;
  2041. struct uvc_control *err_ctrl;
  2042. struct uvc_entity *entity;
  2043. int ret_out = 0;
  2044. int ret;
  2045. /* Find the control. */
  2046. list_for_each_entry(entity, &chain->entities, chain) {
  2047. ret = uvc_ctrl_commit_entity(chain->dev, handle, entity,
  2048. rollback, &err_ctrl);
  2049. if (ret < 0) {
  2050. if (ctrls)
  2051. ctrls->error_idx =
  2052. uvc_ctrl_find_ctrl_idx(entity, ctrls,
  2053. err_ctrl);
  2054. /*
  2055. * When we fail to commit an entity, we need to
  2056. * restore the UVC_CTRL_DATA_BACKUP for all the
  2057. * controls in the other entities, otherwise our cache
  2058. * and the hardware will be out of sync.
  2059. */
  2060. rollback = 1;
  2061. ret_out = ret;
  2062. } else if (ret > 0 && !rollback) {
  2063. uvc_ctrl_send_events(handle, entity,
  2064. ctrls->controls, ctrls->count);
  2065. }
  2066. }
  2067. mutex_unlock(&chain->ctrl_mutex);
  2068. return ret_out;
  2069. }
  2070. static int uvc_mapping_get_xctrl_compound(struct uvc_video_chain *chain,
  2071. struct uvc_control *ctrl,
  2072. struct uvc_control_mapping *mapping,
  2073. u32 which,
  2074. struct v4l2_ext_control *xctrl)
  2075. {
  2076. u8 *data __free(kfree) = NULL;
  2077. size_t size;
  2078. u8 query;
  2079. int ret;
  2080. int id;
  2081. switch (which) {
  2082. case V4L2_CTRL_WHICH_CUR_VAL:
  2083. id = UVC_CTRL_DATA_CURRENT;
  2084. query = UVC_GET_CUR;
  2085. break;
  2086. case V4L2_CTRL_WHICH_MIN_VAL:
  2087. id = UVC_CTRL_DATA_MIN;
  2088. query = UVC_GET_MIN;
  2089. break;
  2090. case V4L2_CTRL_WHICH_MAX_VAL:
  2091. id = UVC_CTRL_DATA_MAX;
  2092. query = UVC_GET_MAX;
  2093. break;
  2094. case V4L2_CTRL_WHICH_DEF_VAL:
  2095. id = UVC_CTRL_DATA_DEF;
  2096. query = UVC_GET_DEF;
  2097. break;
  2098. default:
  2099. return -EINVAL;
  2100. }
  2101. size = uvc_mapping_v4l2_size(mapping);
  2102. if (xctrl->size < size) {
  2103. xctrl->size = size;
  2104. return -ENOSPC;
  2105. }
  2106. data = kmalloc(size, GFP_KERNEL);
  2107. if (!data)
  2108. return -ENOMEM;
  2109. if (which == V4L2_CTRL_WHICH_CUR_VAL)
  2110. ret = __uvc_ctrl_load_cur(chain, ctrl);
  2111. else
  2112. ret = uvc_ctrl_populate_cache(chain, ctrl);
  2113. if (ret < 0)
  2114. return ret;
  2115. ret = mapping->get(mapping, query, uvc_ctrl_data(ctrl, id), size, data);
  2116. if (ret < 0)
  2117. return ret;
  2118. /*
  2119. * v4l2_ext_control does not have enough room to fit a compound control.
  2120. * Instead, the value is in the user memory at xctrl->ptr. The v4l2
  2121. * ioctl helper does not copy it for us.
  2122. */
  2123. return copy_to_user(xctrl->ptr, data, size) ? -EFAULT : 0;
  2124. }
  2125. static int uvc_mapping_get_xctrl_std(struct uvc_video_chain *chain,
  2126. struct uvc_control *ctrl,
  2127. struct uvc_control_mapping *mapping,
  2128. u32 which, struct v4l2_ext_control *xctrl)
  2129. {
  2130. struct v4l2_query_ext_ctrl qec;
  2131. int ret;
  2132. switch (which) {
  2133. case V4L2_CTRL_WHICH_CUR_VAL:
  2134. return __uvc_ctrl_get(chain, ctrl, mapping, &xctrl->value);
  2135. case V4L2_CTRL_WHICH_DEF_VAL:
  2136. case V4L2_CTRL_WHICH_MIN_VAL:
  2137. case V4L2_CTRL_WHICH_MAX_VAL:
  2138. break;
  2139. default:
  2140. return -EINVAL;
  2141. }
  2142. ret = __uvc_queryctrl_boundaries(chain, ctrl, mapping, &qec);
  2143. if (ret < 0)
  2144. return ret;
  2145. switch (which) {
  2146. case V4L2_CTRL_WHICH_DEF_VAL:
  2147. xctrl->value = qec.default_value;
  2148. break;
  2149. case V4L2_CTRL_WHICH_MIN_VAL:
  2150. xctrl->value = qec.minimum;
  2151. break;
  2152. case V4L2_CTRL_WHICH_MAX_VAL:
  2153. xctrl->value = qec.maximum;
  2154. break;
  2155. }
  2156. return 0;
  2157. }
  2158. static int uvc_mapping_get_xctrl(struct uvc_video_chain *chain,
  2159. struct uvc_control *ctrl,
  2160. struct uvc_control_mapping *mapping,
  2161. u32 which, struct v4l2_ext_control *xctrl)
  2162. {
  2163. if (uvc_ctrl_mapping_is_compound(mapping))
  2164. return uvc_mapping_get_xctrl_compound(chain, ctrl, mapping,
  2165. which, xctrl);
  2166. return uvc_mapping_get_xctrl_std(chain, ctrl, mapping, which, xctrl);
  2167. }
  2168. int uvc_ctrl_get(struct uvc_video_chain *chain, u32 which,
  2169. struct v4l2_ext_control *xctrl)
  2170. {
  2171. struct uvc_control *ctrl;
  2172. struct uvc_control_mapping *mapping;
  2173. if (__uvc_query_v4l2_class(chain, xctrl->id, 0) >= 0)
  2174. return -EACCES;
  2175. ctrl = uvc_find_control(chain, xctrl->id, &mapping);
  2176. if (!ctrl)
  2177. return -EINVAL;
  2178. return uvc_mapping_get_xctrl(chain, ctrl, mapping, which, xctrl);
  2179. }
  2180. static int uvc_ctrl_clamp(struct uvc_video_chain *chain,
  2181. struct uvc_control *ctrl,
  2182. u32 v4l2_id,
  2183. struct uvc_control_mapping *mapping,
  2184. s32 *value_in_out)
  2185. {
  2186. s32 value = *value_in_out;
  2187. u32 step;
  2188. s32 min;
  2189. s32 max;
  2190. int ret;
  2191. switch (mapping->v4l2_type) {
  2192. case V4L2_CTRL_TYPE_INTEGER:
  2193. if (!ctrl->cached) {
  2194. ret = uvc_ctrl_populate_cache(chain, ctrl);
  2195. if (ret < 0)
  2196. return ret;
  2197. }
  2198. max = uvc_mapping_get_s32(mapping, UVC_GET_MAX,
  2199. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
  2200. /*
  2201. * For relative PTZ controls, UVC_GET_MIN for
  2202. * b(Pan|Tilt|Zoom)Speed returns the minimum speed of the
  2203. * movement in direction specified in the sign field.
  2204. * See in USB Device Class Definition for Video Devices:
  2205. * 4.2.2.1.13 Zoom (Relative) Control
  2206. * 4.2.2.1.15 PanTilt (Relative) Control
  2207. *
  2208. * For minimum value, use maximum speed but in negative direction.
  2209. */
  2210. if (uvc_ctrl_is_relative_ptz(v4l2_id))
  2211. min = -max;
  2212. else
  2213. min = uvc_mapping_get_s32(mapping, UVC_GET_MIN,
  2214. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
  2215. step = uvc_mapping_get_s32(mapping, UVC_GET_RES,
  2216. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
  2217. if (step == 0)
  2218. step = 1;
  2219. value = min + DIV_ROUND_CLOSEST((u32)(value - min), step) * step;
  2220. if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
  2221. value = clamp(value, min, max);
  2222. else
  2223. value = clamp_t(u32, value, min, max);
  2224. *value_in_out = value;
  2225. return 0;
  2226. case V4L2_CTRL_TYPE_BITMASK:
  2227. if (!ctrl->cached) {
  2228. ret = uvc_ctrl_populate_cache(chain, ctrl);
  2229. if (ret < 0)
  2230. return ret;
  2231. }
  2232. value &= uvc_get_ctrl_bitmap(ctrl, mapping);
  2233. *value_in_out = value;
  2234. return 0;
  2235. case V4L2_CTRL_TYPE_BOOLEAN:
  2236. *value_in_out = clamp(value, 0, 1);
  2237. return 0;
  2238. case V4L2_CTRL_TYPE_MENU:
  2239. if (value < (ffs(mapping->menu_mask) - 1) ||
  2240. value > (fls(mapping->menu_mask) - 1))
  2241. return -ERANGE;
  2242. if (!test_bit(value, &mapping->menu_mask))
  2243. return -EINVAL;
  2244. /*
  2245. * Valid menu indices are reported by the GET_RES request for
  2246. * UVC controls that support it.
  2247. */
  2248. if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK) {
  2249. int val = uvc_mapping_get_menu_value(mapping, value);
  2250. if (!ctrl->cached) {
  2251. ret = uvc_ctrl_populate_cache(chain, ctrl);
  2252. if (ret < 0)
  2253. return ret;
  2254. }
  2255. if (!(uvc_get_ctrl_bitmap(ctrl, mapping) & val))
  2256. return -EINVAL;
  2257. }
  2258. return 0;
  2259. default:
  2260. return 0;
  2261. }
  2262. return 0;
  2263. }
  2264. static int uvc_mapping_set_xctrl_compound(struct uvc_control *ctrl,
  2265. struct uvc_control_mapping *mapping,
  2266. struct v4l2_ext_control *xctrl)
  2267. {
  2268. u8 *data __free(kfree) = NULL;
  2269. size_t size = uvc_mapping_v4l2_size(mapping);
  2270. if (xctrl->size != size)
  2271. return -EINVAL;
  2272. /*
  2273. * v4l2_ext_control does not have enough room to fit a compound control.
  2274. * Instead, the value is in the user memory at xctrl->ptr. The v4l2
  2275. * ioctl helper does not copy it for us.
  2276. */
  2277. data = memdup_user(xctrl->ptr, size);
  2278. if (IS_ERR(data))
  2279. return PTR_ERR(data);
  2280. return mapping->set(mapping, size, data,
  2281. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
  2282. }
  2283. static int uvc_mapping_set_xctrl(struct uvc_control *ctrl,
  2284. struct uvc_control_mapping *mapping,
  2285. struct v4l2_ext_control *xctrl)
  2286. {
  2287. if (uvc_ctrl_mapping_is_compound(mapping))
  2288. return uvc_mapping_set_xctrl_compound(ctrl, mapping, xctrl);
  2289. uvc_mapping_set_s32(mapping, xctrl->value,
  2290. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
  2291. return 0;
  2292. }
  2293. int uvc_ctrl_set(struct uvc_fh *handle, struct v4l2_ext_control *xctrl)
  2294. {
  2295. struct uvc_video_chain *chain = handle->chain;
  2296. struct uvc_control_mapping *mapping;
  2297. struct uvc_control *ctrl;
  2298. int ret;
  2299. lockdep_assert_held(&chain->ctrl_mutex);
  2300. if (__uvc_query_v4l2_class(chain, xctrl->id, 0) >= 0)
  2301. return -EACCES;
  2302. ctrl = uvc_find_control(chain, xctrl->id, &mapping);
  2303. if (!ctrl)
  2304. return -EINVAL;
  2305. if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
  2306. return -EACCES;
  2307. ret = uvc_ctrl_clamp(chain, ctrl, xctrl->id, mapping, &xctrl->value);
  2308. if (ret)
  2309. return ret;
  2310. /*
  2311. * If the mapping doesn't span the whole UVC control, the current value
  2312. * needs to be loaded from the device to perform the read-modify-write
  2313. * operation.
  2314. */
  2315. if ((ctrl->info.size * 8) != mapping->size) {
  2316. ret = __uvc_ctrl_load_cur(chain, ctrl);
  2317. if (ret < 0)
  2318. return ret;
  2319. }
  2320. /* Backup the current value in case we need to rollback later. */
  2321. if (!ctrl->dirty) {
  2322. memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
  2323. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  2324. ctrl->info.size);
  2325. }
  2326. ret = uvc_mapping_set_xctrl(ctrl, mapping, xctrl);
  2327. if (ret)
  2328. return ret;
  2329. ctrl->dirty = 1;
  2330. ctrl->modified = 1;
  2331. return 0;
  2332. }
  2333. /* --------------------------------------------------------------------------
  2334. * Dynamic controls
  2335. */
  2336. /*
  2337. * Retrieve flags for a given control
  2338. */
  2339. static int uvc_ctrl_get_flags(struct uvc_device *dev,
  2340. const struct uvc_control *ctrl,
  2341. struct uvc_control_info *info)
  2342. {
  2343. u8 *data;
  2344. int ret;
  2345. data = kmalloc(1, GFP_KERNEL);
  2346. if (data == NULL)
  2347. return -ENOMEM;
  2348. if (ctrl->entity->get_info)
  2349. ret = ctrl->entity->get_info(dev, ctrl->entity,
  2350. ctrl->info.selector, data);
  2351. else
  2352. ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id,
  2353. dev->intfnum, info->selector, data, 1);
  2354. if (!ret) {
  2355. info->flags &= ~(UVC_CTRL_FLAG_GET_CUR |
  2356. UVC_CTRL_FLAG_SET_CUR |
  2357. UVC_CTRL_FLAG_AUTO_UPDATE |
  2358. UVC_CTRL_FLAG_ASYNCHRONOUS);
  2359. info->flags |= (data[0] & UVC_CONTROL_CAP_GET ?
  2360. UVC_CTRL_FLAG_GET_CUR : 0)
  2361. | (data[0] & UVC_CONTROL_CAP_SET ?
  2362. UVC_CTRL_FLAG_SET_CUR : 0)
  2363. | (data[0] & UVC_CONTROL_CAP_AUTOUPDATE ?
  2364. UVC_CTRL_FLAG_AUTO_UPDATE : 0)
  2365. | (data[0] & UVC_CONTROL_CAP_ASYNCHRONOUS ?
  2366. UVC_CTRL_FLAG_ASYNCHRONOUS : 0);
  2367. }
  2368. kfree(data);
  2369. return ret;
  2370. }
  2371. static void uvc_ctrl_fixup_xu_info(struct uvc_device *dev,
  2372. const struct uvc_control *ctrl, struct uvc_control_info *info)
  2373. {
  2374. struct uvc_ctrl_fixup {
  2375. struct usb_device_id id;
  2376. u8 entity;
  2377. u8 selector;
  2378. u8 flags;
  2379. };
  2380. static const struct uvc_ctrl_fixup fixups[] = {
  2381. { { USB_DEVICE(0x046d, 0x08c2) }, 9, 1,
  2382. UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
  2383. UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
  2384. UVC_CTRL_FLAG_AUTO_UPDATE },
  2385. { { USB_DEVICE(0x046d, 0x08cc) }, 9, 1,
  2386. UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
  2387. UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
  2388. UVC_CTRL_FLAG_AUTO_UPDATE },
  2389. { { USB_DEVICE(0x046d, 0x0994) }, 9, 1,
  2390. UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
  2391. UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
  2392. UVC_CTRL_FLAG_AUTO_UPDATE },
  2393. };
  2394. unsigned int i;
  2395. for (i = 0; i < ARRAY_SIZE(fixups); ++i) {
  2396. if (!usb_match_one_id(dev->intf, &fixups[i].id))
  2397. continue;
  2398. if (fixups[i].entity == ctrl->entity->id &&
  2399. fixups[i].selector == info->selector) {
  2400. info->flags = fixups[i].flags;
  2401. return;
  2402. }
  2403. }
  2404. }
  2405. /*
  2406. * Query control information (size and flags) for XU controls.
  2407. */
  2408. static int uvc_ctrl_fill_xu_info(struct uvc_device *dev,
  2409. const struct uvc_control *ctrl, struct uvc_control_info *info)
  2410. {
  2411. u8 *data;
  2412. int ret;
  2413. data = kmalloc(2, GFP_KERNEL);
  2414. if (data == NULL)
  2415. return -ENOMEM;
  2416. memcpy(info->entity, ctrl->entity->guid, sizeof(info->entity));
  2417. info->index = ctrl->index;
  2418. info->selector = ctrl->index + 1;
  2419. /* Query and verify the control length (GET_LEN) */
  2420. ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id, dev->intfnum,
  2421. info->selector, data, 2);
  2422. if (ret < 0) {
  2423. uvc_dbg(dev, CONTROL,
  2424. "GET_LEN failed on control %pUl/%u (%d)\n",
  2425. info->entity, info->selector, ret);
  2426. goto done;
  2427. }
  2428. info->size = le16_to_cpup((__le16 *)data);
  2429. info->flags = UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX
  2430. | UVC_CTRL_FLAG_GET_RES | UVC_CTRL_FLAG_GET_DEF;
  2431. ret = uvc_ctrl_get_flags(dev, ctrl, info);
  2432. if (ret < 0) {
  2433. uvc_dbg(dev, CONTROL,
  2434. "Failed to get flags for control %pUl/%u (%d)\n",
  2435. info->entity, info->selector, ret);
  2436. goto done;
  2437. }
  2438. uvc_ctrl_fixup_xu_info(dev, ctrl, info);
  2439. uvc_dbg(dev, CONTROL,
  2440. "XU control %pUl/%u queried: len %u, flags { get %u set %u auto %u }\n",
  2441. info->entity, info->selector, info->size,
  2442. (info->flags & UVC_CTRL_FLAG_GET_CUR) ? 1 : 0,
  2443. (info->flags & UVC_CTRL_FLAG_SET_CUR) ? 1 : 0,
  2444. (info->flags & UVC_CTRL_FLAG_AUTO_UPDATE) ? 1 : 0);
  2445. done:
  2446. kfree(data);
  2447. return ret;
  2448. }
  2449. static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
  2450. const struct uvc_control_info *info);
  2451. static int uvc_ctrl_init_xu_ctrl(struct uvc_device *dev,
  2452. struct uvc_control *ctrl)
  2453. {
  2454. struct uvc_control_info info;
  2455. int ret;
  2456. if (ctrl->initialized)
  2457. return 0;
  2458. ret = uvc_ctrl_fill_xu_info(dev, ctrl, &info);
  2459. if (ret < 0)
  2460. return ret;
  2461. ret = uvc_ctrl_add_info(dev, ctrl, &info);
  2462. if (ret < 0)
  2463. uvc_dbg(dev, CONTROL,
  2464. "Failed to initialize control %pUl/%u on device %s entity %u\n",
  2465. info.entity, info.selector, dev->udev->devpath,
  2466. ctrl->entity->id);
  2467. return ret;
  2468. }
  2469. int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
  2470. struct uvc_xu_control_query *xqry)
  2471. {
  2472. struct uvc_entity *entity, *iter;
  2473. struct uvc_control *ctrl;
  2474. unsigned int i;
  2475. bool found;
  2476. u32 reqflags;
  2477. u16 size;
  2478. u8 *data = NULL;
  2479. int ret;
  2480. /* Find the extension unit. */
  2481. entity = NULL;
  2482. list_for_each_entry(iter, &chain->entities, chain) {
  2483. if (UVC_ENTITY_TYPE(iter) == UVC_VC_EXTENSION_UNIT &&
  2484. iter->id == xqry->unit) {
  2485. entity = iter;
  2486. break;
  2487. }
  2488. }
  2489. if (!entity) {
  2490. uvc_dbg(chain->dev, CONTROL, "Extension unit %u not found\n",
  2491. xqry->unit);
  2492. return -ENOENT;
  2493. }
  2494. /* Find the control and perform delayed initialization if needed. */
  2495. found = false;
  2496. for (i = 0; i < entity->ncontrols; ++i) {
  2497. ctrl = &entity->controls[i];
  2498. if (ctrl->index == xqry->selector - 1) {
  2499. found = true;
  2500. break;
  2501. }
  2502. }
  2503. if (!found) {
  2504. uvc_dbg(chain->dev, CONTROL, "Control %pUl/%u not found\n",
  2505. entity->guid, xqry->selector);
  2506. return -ENOENT;
  2507. }
  2508. if (mutex_lock_interruptible(&chain->ctrl_mutex))
  2509. return -ERESTARTSYS;
  2510. ret = uvc_ctrl_init_xu_ctrl(chain->dev, ctrl);
  2511. if (ret < 0) {
  2512. ret = -ENOENT;
  2513. goto done;
  2514. }
  2515. /* Validate the required buffer size and flags for the request */
  2516. reqflags = 0;
  2517. size = ctrl->info.size;
  2518. switch (xqry->query) {
  2519. case UVC_GET_CUR:
  2520. reqflags = UVC_CTRL_FLAG_GET_CUR;
  2521. break;
  2522. case UVC_GET_MIN:
  2523. reqflags = UVC_CTRL_FLAG_GET_MIN;
  2524. break;
  2525. case UVC_GET_MAX:
  2526. reqflags = UVC_CTRL_FLAG_GET_MAX;
  2527. break;
  2528. case UVC_GET_DEF:
  2529. reqflags = UVC_CTRL_FLAG_GET_DEF;
  2530. break;
  2531. case UVC_GET_RES:
  2532. reqflags = UVC_CTRL_FLAG_GET_RES;
  2533. break;
  2534. case UVC_SET_CUR:
  2535. reqflags = UVC_CTRL_FLAG_SET_CUR;
  2536. break;
  2537. case UVC_GET_LEN:
  2538. size = 2;
  2539. break;
  2540. case UVC_GET_INFO:
  2541. size = 1;
  2542. break;
  2543. default:
  2544. ret = -EINVAL;
  2545. goto done;
  2546. }
  2547. if (size != xqry->size) {
  2548. ret = -ENOBUFS;
  2549. goto done;
  2550. }
  2551. if (reqflags && !(ctrl->info.flags & reqflags)) {
  2552. ret = -EBADRQC;
  2553. goto done;
  2554. }
  2555. data = kmalloc(size, GFP_KERNEL);
  2556. if (data == NULL) {
  2557. ret = -ENOMEM;
  2558. goto done;
  2559. }
  2560. if (xqry->query == UVC_SET_CUR &&
  2561. copy_from_user(data, xqry->data, size)) {
  2562. ret = -EFAULT;
  2563. goto done;
  2564. }
  2565. ret = uvc_query_ctrl(chain->dev, xqry->query, xqry->unit,
  2566. chain->dev->intfnum, xqry->selector, data, size);
  2567. if (ret < 0)
  2568. goto done;
  2569. if (xqry->query != UVC_SET_CUR &&
  2570. copy_to_user(xqry->data, data, size))
  2571. ret = -EFAULT;
  2572. done:
  2573. kfree(data);
  2574. mutex_unlock(&chain->ctrl_mutex);
  2575. return ret;
  2576. }
  2577. /* --------------------------------------------------------------------------
  2578. * Suspend/resume
  2579. */
  2580. /*
  2581. * Restore control values after resume, skipping controls that haven't been
  2582. * changed.
  2583. *
  2584. * TODO
  2585. * - Don't restore modified controls that are back to their default value.
  2586. * - Handle restore order (Auto-Exposure Mode should be restored before
  2587. * Exposure Time).
  2588. */
  2589. int uvc_ctrl_restore_values(struct uvc_device *dev)
  2590. {
  2591. struct uvc_control *ctrl;
  2592. struct uvc_entity *entity;
  2593. unsigned int i;
  2594. int ret;
  2595. /* Walk the entities list and restore controls when possible. */
  2596. list_for_each_entry(entity, &dev->entities, list) {
  2597. for (i = 0; i < entity->ncontrols; ++i) {
  2598. ctrl = &entity->controls[i];
  2599. if (!ctrl->initialized || !ctrl->modified ||
  2600. (ctrl->info.flags & UVC_CTRL_FLAG_RESTORE) == 0)
  2601. continue;
  2602. uvc_dbg(dev, CONTROL, "restoring control %pUl/%u/%u\n",
  2603. ctrl->info.entity, ctrl->info.index,
  2604. ctrl->info.selector);
  2605. ctrl->dirty = 1;
  2606. }
  2607. ret = uvc_ctrl_commit_entity(dev, NULL, entity, 0, NULL);
  2608. if (ret < 0)
  2609. return ret;
  2610. }
  2611. return 0;
  2612. }
  2613. /* --------------------------------------------------------------------------
  2614. * Control and mapping handling
  2615. */
  2616. /*
  2617. * Add control information to a given control.
  2618. */
  2619. static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
  2620. const struct uvc_control_info *info)
  2621. {
  2622. ctrl->info = *info;
  2623. INIT_LIST_HEAD(&ctrl->info.mappings);
  2624. /* Allocate an array to save control values (cur, def, max, etc.) */
  2625. ctrl->uvc_data = kzalloc(ctrl->info.size * UVC_CTRL_DATA_LAST + 1,
  2626. GFP_KERNEL);
  2627. if (!ctrl->uvc_data)
  2628. return -ENOMEM;
  2629. ctrl->initialized = 1;
  2630. uvc_dbg(dev, CONTROL, "Added control %pUl/%u to device %s entity %u\n",
  2631. ctrl->info.entity, ctrl->info.selector, dev->udev->devpath,
  2632. ctrl->entity->id);
  2633. return 0;
  2634. }
  2635. /*
  2636. * Add a control mapping to a given control.
  2637. */
  2638. static int __uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
  2639. struct uvc_control *ctrl, const struct uvc_control_mapping *mapping)
  2640. {
  2641. struct uvc_control_mapping *map;
  2642. unsigned int size;
  2643. unsigned int i;
  2644. int ret;
  2645. /*
  2646. * Most mappings come from static kernel data, and need to be duplicated.
  2647. * Mappings that come from userspace will be unnecessarily duplicated,
  2648. * this could be optimized.
  2649. */
  2650. map = kmemdup(mapping, sizeof(*mapping), GFP_KERNEL);
  2651. if (!map)
  2652. return -ENOMEM;
  2653. map->name = NULL;
  2654. map->menu_names = NULL;
  2655. map->menu_mapping = NULL;
  2656. /* For UVCIOC_CTRL_MAP custom control */
  2657. if (mapping->name) {
  2658. map->name = kstrdup(mapping->name, GFP_KERNEL);
  2659. if (!map->name)
  2660. goto err_nomem;
  2661. }
  2662. INIT_LIST_HEAD(&map->ev_subs);
  2663. if (mapping->menu_mapping && mapping->menu_mask) {
  2664. size = sizeof(mapping->menu_mapping[0])
  2665. * fls(mapping->menu_mask);
  2666. map->menu_mapping = kmemdup(mapping->menu_mapping, size,
  2667. GFP_KERNEL);
  2668. if (!map->menu_mapping)
  2669. goto err_nomem;
  2670. }
  2671. if (mapping->menu_names && mapping->menu_mask) {
  2672. size = sizeof(mapping->menu_names[0])
  2673. * fls(mapping->menu_mask);
  2674. map->menu_names = kmemdup(mapping->menu_names, size,
  2675. GFP_KERNEL);
  2676. if (!map->menu_names)
  2677. goto err_nomem;
  2678. }
  2679. if (uvc_ctrl_mapping_is_compound(map))
  2680. if (WARN_ON(!map->set || !map->get)) {
  2681. ret = -EIO;
  2682. goto free_mem;
  2683. }
  2684. if (map->get == NULL)
  2685. map->get = uvc_get_le_value;
  2686. if (map->set == NULL)
  2687. map->set = uvc_set_le_value;
  2688. for (i = 0; i < ARRAY_SIZE(uvc_control_classes); i++) {
  2689. if (V4L2_CTRL_ID2WHICH(uvc_control_classes[i]) ==
  2690. V4L2_CTRL_ID2WHICH(map->id)) {
  2691. chain->ctrl_class_bitmap |= BIT(i);
  2692. break;
  2693. }
  2694. }
  2695. list_add_tail(&map->list, &ctrl->info.mappings);
  2696. uvc_dbg(chain->dev, CONTROL, "Adding mapping '%s' to control %pUl/%u\n",
  2697. uvc_map_get_name(map), ctrl->info.entity,
  2698. ctrl->info.selector);
  2699. return 0;
  2700. err_nomem:
  2701. ret = -ENOMEM;
  2702. free_mem:
  2703. kfree(map->menu_names);
  2704. kfree(map->menu_mapping);
  2705. kfree(map->name);
  2706. kfree(map);
  2707. return ret;
  2708. }
  2709. int uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
  2710. const struct uvc_control_mapping *mapping)
  2711. {
  2712. struct uvc_device *dev = chain->dev;
  2713. struct uvc_control_mapping *map;
  2714. struct uvc_entity *entity;
  2715. struct uvc_control *ctrl;
  2716. int found = 0;
  2717. int ret;
  2718. if (mapping->id & ~V4L2_CTRL_ID_MASK) {
  2719. uvc_dbg(dev, CONTROL,
  2720. "Can't add mapping '%s', control id 0x%08x is invalid\n",
  2721. uvc_map_get_name(mapping), mapping->id);
  2722. return -EINVAL;
  2723. }
  2724. /* Search for the matching (GUID/CS) control on the current chain */
  2725. list_for_each_entry(entity, &chain->entities, chain) {
  2726. unsigned int i;
  2727. if (UVC_ENTITY_TYPE(entity) != UVC_VC_EXTENSION_UNIT ||
  2728. !uvc_entity_match_guid(entity, mapping->entity))
  2729. continue;
  2730. for (i = 0; i < entity->ncontrols; ++i) {
  2731. ctrl = &entity->controls[i];
  2732. if (ctrl->index == mapping->selector - 1) {
  2733. found = 1;
  2734. break;
  2735. }
  2736. }
  2737. if (found)
  2738. break;
  2739. }
  2740. if (!found)
  2741. return -ENOENT;
  2742. if (mutex_lock_interruptible(&chain->ctrl_mutex))
  2743. return -ERESTARTSYS;
  2744. /* Perform delayed initialization of XU controls */
  2745. ret = uvc_ctrl_init_xu_ctrl(dev, ctrl);
  2746. if (ret < 0) {
  2747. ret = -ENOENT;
  2748. goto done;
  2749. }
  2750. /* Validate the user-provided bit-size and offset */
  2751. if (mapping->size > 32 ||
  2752. mapping->offset + mapping->size > ctrl->info.size * 8) {
  2753. ret = -EINVAL;
  2754. goto done;
  2755. }
  2756. list_for_each_entry(map, &ctrl->info.mappings, list) {
  2757. if (mapping->id == map->id) {
  2758. uvc_dbg(dev, CONTROL,
  2759. "Can't add mapping '%s', control id 0x%08x already exists\n",
  2760. uvc_map_get_name(mapping), mapping->id);
  2761. ret = -EEXIST;
  2762. goto done;
  2763. }
  2764. }
  2765. /* Prevent excess memory consumption */
  2766. if (atomic_inc_return(&dev->nmappings) > UVC_MAX_CONTROL_MAPPINGS) {
  2767. atomic_dec(&dev->nmappings);
  2768. uvc_dbg(dev, CONTROL,
  2769. "Can't add mapping '%s', maximum mappings count (%u) exceeded\n",
  2770. uvc_map_get_name(mapping), UVC_MAX_CONTROL_MAPPINGS);
  2771. ret = -ENOMEM;
  2772. goto done;
  2773. }
  2774. ret = __uvc_ctrl_add_mapping(chain, ctrl, mapping);
  2775. if (ret < 0)
  2776. atomic_dec(&dev->nmappings);
  2777. done:
  2778. mutex_unlock(&chain->ctrl_mutex);
  2779. return ret;
  2780. }
  2781. /*
  2782. * Prune an entity of its bogus controls using a blacklist. Bogus controls
  2783. * are currently the ones that crash the camera or unconditionally return an
  2784. * error when queried.
  2785. */
  2786. static void uvc_ctrl_prune_entity(struct uvc_device *dev,
  2787. struct uvc_entity *entity)
  2788. {
  2789. struct uvc_ctrl_blacklist {
  2790. struct usb_device_id id;
  2791. u8 index;
  2792. };
  2793. static const struct uvc_ctrl_blacklist processing_blacklist[] = {
  2794. { { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */
  2795. { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
  2796. { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
  2797. };
  2798. static const struct uvc_ctrl_blacklist camera_blacklist[] = {
  2799. { { USB_DEVICE(0x06f8, 0x3005) }, 9 }, /* Zoom, Absolute */
  2800. };
  2801. const struct uvc_ctrl_blacklist *blacklist;
  2802. unsigned int size;
  2803. unsigned int count;
  2804. unsigned int i;
  2805. u8 *controls;
  2806. switch (UVC_ENTITY_TYPE(entity)) {
  2807. case UVC_VC_PROCESSING_UNIT:
  2808. blacklist = processing_blacklist;
  2809. count = ARRAY_SIZE(processing_blacklist);
  2810. controls = entity->processing.bmControls;
  2811. size = entity->processing.bControlSize;
  2812. break;
  2813. case UVC_ITT_CAMERA:
  2814. blacklist = camera_blacklist;
  2815. count = ARRAY_SIZE(camera_blacklist);
  2816. controls = entity->camera.bmControls;
  2817. size = entity->camera.bControlSize;
  2818. break;
  2819. default:
  2820. return;
  2821. }
  2822. for (i = 0; i < count; ++i) {
  2823. if (!usb_match_one_id(dev->intf, &blacklist[i].id))
  2824. continue;
  2825. if (blacklist[i].index >= 8 * size ||
  2826. !uvc_test_bit(controls, blacklist[i].index))
  2827. continue;
  2828. uvc_dbg(dev, CONTROL,
  2829. "%u/%u control is black listed, removing it\n",
  2830. entity->id, blacklist[i].index);
  2831. uvc_clear_bit(controls, blacklist[i].index);
  2832. }
  2833. }
  2834. /*
  2835. * Add control information and hardcoded stock control mappings to the given
  2836. * device.
  2837. */
  2838. static void uvc_ctrl_init_ctrl(struct uvc_video_chain *chain,
  2839. struct uvc_control *ctrl)
  2840. {
  2841. unsigned int i;
  2842. for (i = 0; i < ARRAY_SIZE(uvc_ctrls); ++i) {
  2843. const struct uvc_control_info *info = &uvc_ctrls[i];
  2844. if (uvc_entity_match_guid(ctrl->entity, info->entity) &&
  2845. ctrl->index == info->index) {
  2846. uvc_ctrl_add_info(chain->dev, ctrl, info);
  2847. /*
  2848. * Retrieve control flags from the device. Ignore errors
  2849. * and work with default flag values from the uvc_ctrl
  2850. * array when the device doesn't properly implement
  2851. * GET_INFO on standard controls.
  2852. */
  2853. uvc_ctrl_get_flags(chain->dev, ctrl, &ctrl->info);
  2854. break;
  2855. }
  2856. }
  2857. if (!ctrl->initialized)
  2858. return;
  2859. /* Process common mappings. */
  2860. for (i = 0; i < ARRAY_SIZE(uvc_ctrl_mappings); ++i) {
  2861. const struct uvc_control_mapping *mapping = &uvc_ctrl_mappings[i];
  2862. if (!uvc_entity_match_guid(ctrl->entity, mapping->entity) ||
  2863. ctrl->info.selector != mapping->selector)
  2864. continue;
  2865. /* Let the device provide a custom mapping. */
  2866. if (mapping->filter_mapping) {
  2867. mapping = mapping->filter_mapping(chain, ctrl);
  2868. if (!mapping)
  2869. continue;
  2870. }
  2871. __uvc_ctrl_add_mapping(chain, ctrl, mapping);
  2872. }
  2873. }
  2874. /*
  2875. * Initialize device controls.
  2876. */
  2877. static int uvc_ctrl_init_chain(struct uvc_video_chain *chain)
  2878. {
  2879. struct uvc_entity *entity;
  2880. unsigned int i;
  2881. /* Walk the entities list and instantiate controls */
  2882. list_for_each_entry(entity, &chain->entities, chain) {
  2883. struct uvc_control *ctrl;
  2884. unsigned int bControlSize = 0, ncontrols;
  2885. u8 *bmControls = NULL;
  2886. if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
  2887. bmControls = entity->extension.bmControls;
  2888. bControlSize = entity->extension.bControlSize;
  2889. } else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) {
  2890. bmControls = entity->processing.bmControls;
  2891. bControlSize = entity->processing.bControlSize;
  2892. } else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
  2893. bmControls = entity->camera.bmControls;
  2894. bControlSize = entity->camera.bControlSize;
  2895. } else if (UVC_ENTITY_TYPE(entity) == UVC_EXT_GPIO_UNIT) {
  2896. bmControls = entity->gpio.bmControls;
  2897. bControlSize = entity->gpio.bControlSize;
  2898. }
  2899. /* Remove bogus/blacklisted controls */
  2900. uvc_ctrl_prune_entity(chain->dev, entity);
  2901. /* Count supported controls and allocate the controls array */
  2902. ncontrols = memweight(bmControls, bControlSize);
  2903. if (ncontrols == 0)
  2904. continue;
  2905. entity->controls = kzalloc_objs(*ctrl, ncontrols);
  2906. if (entity->controls == NULL)
  2907. return -ENOMEM;
  2908. entity->ncontrols = ncontrols;
  2909. /* Initialize all supported controls */
  2910. ctrl = entity->controls;
  2911. for (i = 0; i < bControlSize * 8; ++i) {
  2912. if (uvc_test_bit(bmControls, i) == 0)
  2913. continue;
  2914. ctrl->entity = entity;
  2915. ctrl->index = i;
  2916. uvc_ctrl_init_ctrl(chain, ctrl);
  2917. ctrl++;
  2918. }
  2919. }
  2920. return 0;
  2921. }
  2922. int uvc_ctrl_init_device(struct uvc_device *dev)
  2923. {
  2924. struct uvc_video_chain *chain;
  2925. int ret;
  2926. INIT_WORK(&dev->async_ctrl.work, uvc_ctrl_status_event_work);
  2927. list_for_each_entry(chain, &dev->chains, list) {
  2928. ret = uvc_ctrl_init_chain(chain);
  2929. if (ret)
  2930. return ret;
  2931. }
  2932. return 0;
  2933. }
  2934. void uvc_ctrl_cleanup_fh(struct uvc_fh *handle)
  2935. {
  2936. struct uvc_entity *entity;
  2937. guard(mutex)(&handle->chain->ctrl_mutex);
  2938. if (!handle->pending_async_ctrls)
  2939. return;
  2940. list_for_each_entry(entity, &handle->chain->dev->entities, list) {
  2941. for (unsigned int i = 0; i < entity->ncontrols; ++i) {
  2942. if (entity->controls[i].handle != handle)
  2943. continue;
  2944. uvc_ctrl_clear_handle(&entity->controls[i]);
  2945. }
  2946. }
  2947. if (!WARN_ON(handle->pending_async_ctrls))
  2948. return;
  2949. for (unsigned int i = 0; i < handle->pending_async_ctrls; i++)
  2950. uvc_pm_put(handle->stream->dev);
  2951. }
  2952. /*
  2953. * Cleanup device controls.
  2954. */
  2955. static void uvc_ctrl_cleanup_mappings(struct uvc_device *dev,
  2956. struct uvc_control *ctrl)
  2957. {
  2958. struct uvc_control_mapping *mapping, *nm;
  2959. list_for_each_entry_safe(mapping, nm, &ctrl->info.mappings, list) {
  2960. list_del(&mapping->list);
  2961. kfree(mapping->menu_names);
  2962. kfree(mapping->menu_mapping);
  2963. kfree(mapping->name);
  2964. kfree(mapping);
  2965. }
  2966. }
  2967. void uvc_ctrl_cleanup_device(struct uvc_device *dev)
  2968. {
  2969. struct uvc_entity *entity;
  2970. unsigned int i;
  2971. /* Can be uninitialized if we are aborting on probe error. */
  2972. if (dev->async_ctrl.work.func)
  2973. cancel_work_sync(&dev->async_ctrl.work);
  2974. /* Free controls and control mappings for all entities. */
  2975. list_for_each_entry(entity, &dev->entities, list) {
  2976. for (i = 0; i < entity->ncontrols; ++i) {
  2977. struct uvc_control *ctrl = &entity->controls[i];
  2978. if (!ctrl->initialized)
  2979. continue;
  2980. uvc_ctrl_cleanup_mappings(dev, ctrl);
  2981. kfree(ctrl->uvc_data);
  2982. }
  2983. kfree(entity->controls);
  2984. }
  2985. }