uvc_ctrl.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884
  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. static const u32 uvc_control_classes[] = {
  358. V4L2_CID_CAMERA_CLASS,
  359. V4L2_CID_USER_CLASS,
  360. };
  361. static const int exposure_auto_mapping[] = { 2, 1, 4, 8 };
  362. /*
  363. * This function translates the V4L2 menu index @idx, as exposed to userspace as
  364. * the V4L2 control value, to the corresponding UVC control value used by the
  365. * device. The custom menu_mapping in the control @mapping is used when
  366. * available, otherwise the function assumes that the V4L2 and UVC values are
  367. * identical.
  368. *
  369. * For controls of type UVC_CTRL_DATA_TYPE_BITMASK, the UVC control value is
  370. * expressed as a bitmask and is thus guaranteed to have a single bit set.
  371. *
  372. * The function returns -EINVAL if the V4L2 menu index @idx isn't valid for the
  373. * control, which includes all controls whose type isn't UVC_CTRL_DATA_TYPE_ENUM
  374. * or UVC_CTRL_DATA_TYPE_BITMASK.
  375. */
  376. static int uvc_mapping_get_menu_value(const struct uvc_control_mapping *mapping,
  377. u32 idx)
  378. {
  379. if (!test_bit(idx, &mapping->menu_mask))
  380. return -EINVAL;
  381. if (mapping->menu_mapping)
  382. return mapping->menu_mapping[idx];
  383. return idx;
  384. }
  385. static const char *
  386. uvc_mapping_get_menu_name(const struct uvc_control_mapping *mapping, u32 idx)
  387. {
  388. if (!test_bit(idx, &mapping->menu_mask))
  389. return NULL;
  390. if (mapping->menu_names)
  391. return mapping->menu_names[idx];
  392. return v4l2_ctrl_get_menu(mapping->id)[idx];
  393. }
  394. static s32 uvc_ctrl_get_zoom(struct uvc_control_mapping *mapping,
  395. u8 query, const u8 *data)
  396. {
  397. s8 zoom = (s8)data[0];
  398. switch (query) {
  399. case UVC_GET_CUR:
  400. return (zoom == 0) ? 0 : (zoom > 0 ? data[2] : -data[2]);
  401. case UVC_GET_MIN:
  402. case UVC_GET_MAX:
  403. case UVC_GET_RES:
  404. case UVC_GET_DEF:
  405. default:
  406. return data[2];
  407. }
  408. }
  409. static void uvc_ctrl_set_zoom(struct uvc_control_mapping *mapping,
  410. s32 value, u8 *data)
  411. {
  412. data[0] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
  413. data[2] = min((int)abs(value), 0xff);
  414. }
  415. static s32 uvc_ctrl_get_rel_speed(struct uvc_control_mapping *mapping,
  416. u8 query, const u8 *data)
  417. {
  418. unsigned int first = mapping->offset / 8;
  419. s8 rel = (s8)data[first];
  420. switch (query) {
  421. case UVC_GET_CUR:
  422. return (rel == 0) ? 0 : (rel > 0 ? data[first+1]
  423. : -data[first+1]);
  424. case UVC_GET_MIN:
  425. return -data[first+1];
  426. case UVC_GET_MAX:
  427. case UVC_GET_RES:
  428. case UVC_GET_DEF:
  429. default:
  430. return data[first+1];
  431. }
  432. }
  433. static void uvc_ctrl_set_rel_speed(struct uvc_control_mapping *mapping,
  434. s32 value, u8 *data)
  435. {
  436. unsigned int first = mapping->offset / 8;
  437. data[first] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
  438. data[first+1] = min_t(int, abs(value), 0xff);
  439. }
  440. static const struct uvc_control_mapping uvc_ctrl_power_line_mapping_limited = {
  441. .id = V4L2_CID_POWER_LINE_FREQUENCY,
  442. .entity = UVC_GUID_UVC_PROCESSING,
  443. .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
  444. .size = 2,
  445. .offset = 0,
  446. .v4l2_type = V4L2_CTRL_TYPE_MENU,
  447. .data_type = UVC_CTRL_DATA_TYPE_ENUM,
  448. .menu_mask = GENMASK(V4L2_CID_POWER_LINE_FREQUENCY_60HZ,
  449. V4L2_CID_POWER_LINE_FREQUENCY_50HZ),
  450. };
  451. static const struct uvc_control_mapping uvc_ctrl_power_line_mapping_uvc11 = {
  452. .id = V4L2_CID_POWER_LINE_FREQUENCY,
  453. .entity = UVC_GUID_UVC_PROCESSING,
  454. .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
  455. .size = 2,
  456. .offset = 0,
  457. .v4l2_type = V4L2_CTRL_TYPE_MENU,
  458. .data_type = UVC_CTRL_DATA_TYPE_ENUM,
  459. .menu_mask = GENMASK(V4L2_CID_POWER_LINE_FREQUENCY_60HZ,
  460. V4L2_CID_POWER_LINE_FREQUENCY_DISABLED),
  461. };
  462. static const struct uvc_control_mapping uvc_ctrl_power_line_mapping_uvc15 = {
  463. .id = V4L2_CID_POWER_LINE_FREQUENCY,
  464. .entity = UVC_GUID_UVC_PROCESSING,
  465. .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
  466. .size = 2,
  467. .offset = 0,
  468. .v4l2_type = V4L2_CTRL_TYPE_MENU,
  469. .data_type = UVC_CTRL_DATA_TYPE_ENUM,
  470. .menu_mask = GENMASK(V4L2_CID_POWER_LINE_FREQUENCY_AUTO,
  471. V4L2_CID_POWER_LINE_FREQUENCY_DISABLED),
  472. };
  473. static const struct uvc_control_mapping *uvc_ctrl_filter_plf_mapping(
  474. struct uvc_video_chain *chain, struct uvc_control *ctrl)
  475. {
  476. const struct uvc_control_mapping *out_mapping =
  477. &uvc_ctrl_power_line_mapping_uvc11;
  478. u8 *buf __free(kfree) = NULL;
  479. u8 init_val;
  480. int ret;
  481. buf = kmalloc(sizeof(*buf), GFP_KERNEL);
  482. if (!buf)
  483. return NULL;
  484. /* Save the current PLF value, so we can restore it. */
  485. ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, ctrl->entity->id,
  486. chain->dev->intfnum, ctrl->info.selector,
  487. buf, sizeof(*buf));
  488. /* If we cannot read the control skip it. */
  489. if (ret)
  490. return NULL;
  491. init_val = *buf;
  492. /* If PLF value cannot be set to off, it is limited. */
  493. *buf = V4L2_CID_POWER_LINE_FREQUENCY_DISABLED;
  494. ret = uvc_query_ctrl(chain->dev, UVC_SET_CUR, ctrl->entity->id,
  495. chain->dev->intfnum, ctrl->info.selector,
  496. buf, sizeof(*buf));
  497. if (ret)
  498. return &uvc_ctrl_power_line_mapping_limited;
  499. /* UVC 1.1 does not define auto, we can exit. */
  500. if (chain->dev->uvc_version < 0x150)
  501. goto end;
  502. /* Check if the device supports auto. */
  503. *buf = V4L2_CID_POWER_LINE_FREQUENCY_AUTO;
  504. ret = uvc_query_ctrl(chain->dev, UVC_SET_CUR, ctrl->entity->id,
  505. chain->dev->intfnum, ctrl->info.selector,
  506. buf, sizeof(*buf));
  507. if (!ret)
  508. out_mapping = &uvc_ctrl_power_line_mapping_uvc15;
  509. end:
  510. /* Restore initial value and add mapping. */
  511. *buf = init_val;
  512. uvc_query_ctrl(chain->dev, UVC_SET_CUR, ctrl->entity->id,
  513. chain->dev->intfnum, ctrl->info.selector,
  514. buf, sizeof(*buf));
  515. return out_mapping;
  516. }
  517. static const struct uvc_control_mapping uvc_ctrl_mappings[] = {
  518. {
  519. .id = V4L2_CID_BRIGHTNESS,
  520. .entity = UVC_GUID_UVC_PROCESSING,
  521. .selector = UVC_PU_BRIGHTNESS_CONTROL,
  522. .size = 16,
  523. .offset = 0,
  524. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  525. .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
  526. },
  527. {
  528. .id = V4L2_CID_CONTRAST,
  529. .entity = UVC_GUID_UVC_PROCESSING,
  530. .selector = UVC_PU_CONTRAST_CONTROL,
  531. .size = 16,
  532. .offset = 0,
  533. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  534. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  535. },
  536. {
  537. .id = V4L2_CID_HUE,
  538. .entity = UVC_GUID_UVC_PROCESSING,
  539. .selector = UVC_PU_HUE_CONTROL,
  540. .size = 16,
  541. .offset = 0,
  542. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  543. .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
  544. .master_id = V4L2_CID_HUE_AUTO,
  545. .master_manual = 0,
  546. },
  547. {
  548. .id = V4L2_CID_SATURATION,
  549. .entity = UVC_GUID_UVC_PROCESSING,
  550. .selector = UVC_PU_SATURATION_CONTROL,
  551. .size = 16,
  552. .offset = 0,
  553. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  554. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  555. },
  556. {
  557. .id = V4L2_CID_SHARPNESS,
  558. .entity = UVC_GUID_UVC_PROCESSING,
  559. .selector = UVC_PU_SHARPNESS_CONTROL,
  560. .size = 16,
  561. .offset = 0,
  562. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  563. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  564. },
  565. {
  566. .id = V4L2_CID_GAMMA,
  567. .entity = UVC_GUID_UVC_PROCESSING,
  568. .selector = UVC_PU_GAMMA_CONTROL,
  569. .size = 16,
  570. .offset = 0,
  571. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  572. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  573. },
  574. {
  575. .id = V4L2_CID_BACKLIGHT_COMPENSATION,
  576. .entity = UVC_GUID_UVC_PROCESSING,
  577. .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
  578. .size = 16,
  579. .offset = 0,
  580. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  581. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  582. },
  583. {
  584. .id = V4L2_CID_GAIN,
  585. .entity = UVC_GUID_UVC_PROCESSING,
  586. .selector = UVC_PU_GAIN_CONTROL,
  587. .size = 16,
  588. .offset = 0,
  589. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  590. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  591. },
  592. {
  593. .id = V4L2_CID_HUE_AUTO,
  594. .entity = UVC_GUID_UVC_PROCESSING,
  595. .selector = UVC_PU_HUE_AUTO_CONTROL,
  596. .size = 1,
  597. .offset = 0,
  598. .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
  599. .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
  600. .slave_ids = { V4L2_CID_HUE, },
  601. },
  602. {
  603. .id = V4L2_CID_EXPOSURE_AUTO,
  604. .entity = UVC_GUID_UVC_CAMERA,
  605. .selector = UVC_CT_AE_MODE_CONTROL,
  606. .size = 4,
  607. .offset = 0,
  608. .v4l2_type = V4L2_CTRL_TYPE_MENU,
  609. .data_type = UVC_CTRL_DATA_TYPE_BITMASK,
  610. .menu_mapping = exposure_auto_mapping,
  611. .menu_mask = GENMASK(V4L2_EXPOSURE_APERTURE_PRIORITY,
  612. V4L2_EXPOSURE_AUTO),
  613. .slave_ids = { V4L2_CID_EXPOSURE_ABSOLUTE, },
  614. },
  615. {
  616. .id = V4L2_CID_EXPOSURE_AUTO_PRIORITY,
  617. .entity = UVC_GUID_UVC_CAMERA,
  618. .selector = UVC_CT_AE_PRIORITY_CONTROL,
  619. .size = 1,
  620. .offset = 0,
  621. .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
  622. .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
  623. },
  624. {
  625. .id = V4L2_CID_EXPOSURE_ABSOLUTE,
  626. .entity = UVC_GUID_UVC_CAMERA,
  627. .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
  628. .size = 32,
  629. .offset = 0,
  630. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  631. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  632. .master_id = V4L2_CID_EXPOSURE_AUTO,
  633. .master_manual = V4L2_EXPOSURE_MANUAL,
  634. },
  635. {
  636. .id = V4L2_CID_AUTO_WHITE_BALANCE,
  637. .entity = UVC_GUID_UVC_PROCESSING,
  638. .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
  639. .size = 1,
  640. .offset = 0,
  641. .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
  642. .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
  643. .slave_ids = { V4L2_CID_WHITE_BALANCE_TEMPERATURE, },
  644. },
  645. {
  646. .id = V4L2_CID_WHITE_BALANCE_TEMPERATURE,
  647. .entity = UVC_GUID_UVC_PROCESSING,
  648. .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
  649. .size = 16,
  650. .offset = 0,
  651. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  652. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  653. .master_id = V4L2_CID_AUTO_WHITE_BALANCE,
  654. .master_manual = 0,
  655. },
  656. {
  657. .id = V4L2_CID_AUTO_WHITE_BALANCE,
  658. .entity = UVC_GUID_UVC_PROCESSING,
  659. .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
  660. .size = 1,
  661. .offset = 0,
  662. .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
  663. .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
  664. .slave_ids = { V4L2_CID_BLUE_BALANCE,
  665. V4L2_CID_RED_BALANCE },
  666. },
  667. {
  668. .id = V4L2_CID_BLUE_BALANCE,
  669. .entity = UVC_GUID_UVC_PROCESSING,
  670. .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
  671. .size = 16,
  672. .offset = 0,
  673. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  674. .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
  675. .master_id = V4L2_CID_AUTO_WHITE_BALANCE,
  676. .master_manual = 0,
  677. },
  678. {
  679. .id = V4L2_CID_RED_BALANCE,
  680. .entity = UVC_GUID_UVC_PROCESSING,
  681. .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
  682. .size = 16,
  683. .offset = 16,
  684. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  685. .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
  686. .master_id = V4L2_CID_AUTO_WHITE_BALANCE,
  687. .master_manual = 0,
  688. },
  689. {
  690. .id = V4L2_CID_FOCUS_ABSOLUTE,
  691. .entity = UVC_GUID_UVC_CAMERA,
  692. .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
  693. .size = 16,
  694. .offset = 0,
  695. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  696. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  697. .master_id = V4L2_CID_FOCUS_AUTO,
  698. .master_manual = 0,
  699. },
  700. {
  701. .id = V4L2_CID_FOCUS_AUTO,
  702. .entity = UVC_GUID_UVC_CAMERA,
  703. .selector = UVC_CT_FOCUS_AUTO_CONTROL,
  704. .size = 1,
  705. .offset = 0,
  706. .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
  707. .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
  708. .slave_ids = { V4L2_CID_FOCUS_ABSOLUTE, },
  709. },
  710. {
  711. .id = V4L2_CID_IRIS_ABSOLUTE,
  712. .entity = UVC_GUID_UVC_CAMERA,
  713. .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL,
  714. .size = 16,
  715. .offset = 0,
  716. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  717. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  718. },
  719. {
  720. .id = V4L2_CID_IRIS_RELATIVE,
  721. .entity = UVC_GUID_UVC_CAMERA,
  722. .selector = UVC_CT_IRIS_RELATIVE_CONTROL,
  723. .size = 8,
  724. .offset = 0,
  725. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  726. .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
  727. },
  728. {
  729. .id = V4L2_CID_ZOOM_ABSOLUTE,
  730. .entity = UVC_GUID_UVC_CAMERA,
  731. .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
  732. .size = 16,
  733. .offset = 0,
  734. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  735. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  736. },
  737. {
  738. .id = V4L2_CID_ZOOM_CONTINUOUS,
  739. .entity = UVC_GUID_UVC_CAMERA,
  740. .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
  741. .size = 0,
  742. .offset = 0,
  743. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  744. .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
  745. .get = uvc_ctrl_get_zoom,
  746. .set = uvc_ctrl_set_zoom,
  747. },
  748. {
  749. .id = V4L2_CID_PAN_ABSOLUTE,
  750. .entity = UVC_GUID_UVC_CAMERA,
  751. .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
  752. .size = 32,
  753. .offset = 0,
  754. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  755. .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
  756. },
  757. {
  758. .id = V4L2_CID_TILT_ABSOLUTE,
  759. .entity = UVC_GUID_UVC_CAMERA,
  760. .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
  761. .size = 32,
  762. .offset = 32,
  763. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  764. .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
  765. },
  766. {
  767. .id = V4L2_CID_PAN_SPEED,
  768. .entity = UVC_GUID_UVC_CAMERA,
  769. .selector = UVC_CT_PANTILT_RELATIVE_CONTROL,
  770. .size = 16,
  771. .offset = 0,
  772. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  773. .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
  774. .get = uvc_ctrl_get_rel_speed,
  775. .set = uvc_ctrl_set_rel_speed,
  776. },
  777. {
  778. .id = V4L2_CID_TILT_SPEED,
  779. .entity = UVC_GUID_UVC_CAMERA,
  780. .selector = UVC_CT_PANTILT_RELATIVE_CONTROL,
  781. .size = 16,
  782. .offset = 16,
  783. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  784. .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
  785. .get = uvc_ctrl_get_rel_speed,
  786. .set = uvc_ctrl_set_rel_speed,
  787. },
  788. {
  789. .id = V4L2_CID_PRIVACY,
  790. .entity = UVC_GUID_UVC_CAMERA,
  791. .selector = UVC_CT_PRIVACY_CONTROL,
  792. .size = 1,
  793. .offset = 0,
  794. .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
  795. .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
  796. },
  797. {
  798. .id = V4L2_CID_PRIVACY,
  799. .entity = UVC_GUID_EXT_GPIO_CONTROLLER,
  800. .selector = UVC_CT_PRIVACY_CONTROL,
  801. .size = 1,
  802. .offset = 0,
  803. .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
  804. .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
  805. },
  806. {
  807. .entity = UVC_GUID_UVC_PROCESSING,
  808. .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
  809. .filter_mapping = uvc_ctrl_filter_plf_mapping,
  810. },
  811. };
  812. /* ------------------------------------------------------------------------
  813. * Utility functions
  814. */
  815. static inline u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id)
  816. {
  817. return ctrl->uvc_data + id * ctrl->info.size;
  818. }
  819. static inline int uvc_test_bit(const u8 *data, int bit)
  820. {
  821. return (data[bit >> 3] >> (bit & 7)) & 1;
  822. }
  823. static inline void uvc_clear_bit(u8 *data, int bit)
  824. {
  825. data[bit >> 3] &= ~(1 << (bit & 7));
  826. }
  827. static s32 uvc_menu_to_v4l2_menu(struct uvc_control_mapping *mapping, s32 val)
  828. {
  829. unsigned int i;
  830. for (i = 0; BIT(i) <= mapping->menu_mask; ++i) {
  831. u32 menu_value;
  832. if (!test_bit(i, &mapping->menu_mask))
  833. continue;
  834. menu_value = uvc_mapping_get_menu_value(mapping, i);
  835. if (menu_value == val)
  836. return i;
  837. }
  838. return val;
  839. }
  840. /*
  841. * Extract the bit string specified by mapping->offset and mapping->size
  842. * from the little-endian data stored at 'data' and return the result as
  843. * a signed 32bit integer. Sign extension will be performed if the mapping
  844. * references a signed data type.
  845. */
  846. static s32 uvc_get_le_value(struct uvc_control_mapping *mapping,
  847. u8 query, const u8 *data)
  848. {
  849. int bits = mapping->size;
  850. int offset = mapping->offset;
  851. s32 value = 0;
  852. u8 mask;
  853. data += offset / 8;
  854. offset &= 7;
  855. mask = ((1LL << bits) - 1) << offset;
  856. while (1) {
  857. u8 byte = *data & mask;
  858. value |= offset > 0 ? (byte >> offset) : (byte << (-offset));
  859. bits -= 8 - max(offset, 0);
  860. if (bits <= 0)
  861. break;
  862. offset -= 8;
  863. mask = (1 << bits) - 1;
  864. data++;
  865. }
  866. /* Sign-extend the value if needed. */
  867. if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
  868. value |= -(value & (1 << (mapping->size - 1)));
  869. /* If it is a menu, convert from uvc to v4l2. */
  870. if (mapping->v4l2_type != V4L2_CTRL_TYPE_MENU)
  871. return value;
  872. switch (query) {
  873. case UVC_GET_CUR:
  874. case UVC_GET_DEF:
  875. return uvc_menu_to_v4l2_menu(mapping, value);
  876. }
  877. return value;
  878. }
  879. /*
  880. * Set the bit string specified by mapping->offset and mapping->size
  881. * in the little-endian data stored at 'data' to the value 'value'.
  882. */
  883. static void uvc_set_le_value(struct uvc_control_mapping *mapping,
  884. s32 value, u8 *data)
  885. {
  886. int bits = mapping->size;
  887. int offset = mapping->offset;
  888. u8 mask;
  889. /*
  890. * According to the v4l2 spec, writing any value to a button control
  891. * should result in the action belonging to the button control being
  892. * triggered. UVC devices however want to see a 1 written -> override
  893. * value.
  894. */
  895. if (mapping->v4l2_type == V4L2_CTRL_TYPE_BUTTON)
  896. value = -1;
  897. data += offset / 8;
  898. offset &= 7;
  899. for (; bits > 0; data++) {
  900. mask = ((1LL << bits) - 1) << offset;
  901. *data = (*data & ~mask) | ((value << offset) & mask);
  902. value >>= offset ? offset : 8;
  903. bits -= 8 - offset;
  904. offset = 0;
  905. }
  906. }
  907. /* ------------------------------------------------------------------------
  908. * Terminal and unit management
  909. */
  910. static int uvc_entity_match_guid(const struct uvc_entity *entity,
  911. const u8 guid[16])
  912. {
  913. return memcmp(entity->guid, guid, sizeof(entity->guid)) == 0;
  914. }
  915. /* ------------------------------------------------------------------------
  916. * UVC Controls
  917. */
  918. static void __uvc_find_control(struct uvc_entity *entity, u32 v4l2_id,
  919. struct uvc_control_mapping **mapping, struct uvc_control **control,
  920. int next)
  921. {
  922. struct uvc_control *ctrl;
  923. struct uvc_control_mapping *map;
  924. unsigned int i;
  925. if (entity == NULL)
  926. return;
  927. for (i = 0; i < entity->ncontrols; ++i) {
  928. ctrl = &entity->controls[i];
  929. if (!ctrl->initialized)
  930. continue;
  931. list_for_each_entry(map, &ctrl->info.mappings, list) {
  932. if ((map->id == v4l2_id) && !next) {
  933. *control = ctrl;
  934. *mapping = map;
  935. return;
  936. }
  937. if ((*mapping == NULL || (*mapping)->id > map->id) &&
  938. (map->id > v4l2_id) && next) {
  939. *control = ctrl;
  940. *mapping = map;
  941. }
  942. }
  943. }
  944. }
  945. static struct uvc_control *uvc_find_control(struct uvc_video_chain *chain,
  946. u32 v4l2_id, struct uvc_control_mapping **mapping)
  947. {
  948. struct uvc_control *ctrl = NULL;
  949. struct uvc_entity *entity;
  950. int next = v4l2_id & V4L2_CTRL_FLAG_NEXT_CTRL;
  951. *mapping = NULL;
  952. /* Mask the query flags. */
  953. v4l2_id &= V4L2_CTRL_ID_MASK;
  954. /* Find the control. */
  955. list_for_each_entry(entity, &chain->entities, chain) {
  956. __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next);
  957. if (ctrl && !next)
  958. return ctrl;
  959. }
  960. if (ctrl == NULL && !next)
  961. uvc_dbg(chain->dev, CONTROL, "Control 0x%08x not found\n",
  962. v4l2_id);
  963. return ctrl;
  964. }
  965. static int uvc_ctrl_populate_cache(struct uvc_video_chain *chain,
  966. struct uvc_control *ctrl)
  967. {
  968. int ret;
  969. if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
  970. ret = uvc_query_ctrl(chain->dev, UVC_GET_DEF, ctrl->entity->id,
  971. chain->dev->intfnum, ctrl->info.selector,
  972. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF),
  973. ctrl->info.size);
  974. if (ret < 0)
  975. return ret;
  976. }
  977. if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN) {
  978. ret = uvc_query_ctrl(chain->dev, UVC_GET_MIN, ctrl->entity->id,
  979. chain->dev->intfnum, ctrl->info.selector,
  980. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN),
  981. ctrl->info.size);
  982. if (ret < 0)
  983. return ret;
  984. }
  985. if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX) {
  986. ret = uvc_query_ctrl(chain->dev, UVC_GET_MAX, ctrl->entity->id,
  987. chain->dev->intfnum, ctrl->info.selector,
  988. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX),
  989. ctrl->info.size);
  990. if (ret < 0)
  991. return ret;
  992. }
  993. if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES) {
  994. ret = uvc_query_ctrl(chain->dev, UVC_GET_RES, ctrl->entity->id,
  995. chain->dev->intfnum, ctrl->info.selector,
  996. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES),
  997. ctrl->info.size);
  998. if (ret < 0) {
  999. if (UVC_ENTITY_TYPE(ctrl->entity) !=
  1000. UVC_VC_EXTENSION_UNIT)
  1001. return ret;
  1002. /*
  1003. * GET_RES is mandatory for XU controls, but some
  1004. * cameras still choke on it. Ignore errors and set the
  1005. * resolution value to zero.
  1006. */
  1007. uvc_warn_once(chain->dev, UVC_WARN_XU_GET_RES,
  1008. "UVC non compliance - GET_RES failed on "
  1009. "an XU control. Enabling workaround.\n");
  1010. memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES), 0,
  1011. ctrl->info.size);
  1012. }
  1013. }
  1014. ctrl->cached = 1;
  1015. return 0;
  1016. }
  1017. static int __uvc_ctrl_load_cur(struct uvc_video_chain *chain,
  1018. struct uvc_control *ctrl)
  1019. {
  1020. u8 *data;
  1021. int ret;
  1022. if (ctrl->loaded)
  1023. return 0;
  1024. data = uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT);
  1025. if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0) {
  1026. memset(data, 0, ctrl->info.size);
  1027. ctrl->loaded = 1;
  1028. return 0;
  1029. }
  1030. if (ctrl->entity->get_cur)
  1031. ret = ctrl->entity->get_cur(chain->dev, ctrl->entity,
  1032. ctrl->info.selector, data,
  1033. ctrl->info.size);
  1034. else
  1035. ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
  1036. ctrl->entity->id, chain->dev->intfnum,
  1037. ctrl->info.selector, data,
  1038. ctrl->info.size);
  1039. if (ret < 0)
  1040. return ret;
  1041. ctrl->loaded = 1;
  1042. return ret;
  1043. }
  1044. static int __uvc_ctrl_get(struct uvc_video_chain *chain,
  1045. struct uvc_control *ctrl,
  1046. struct uvc_control_mapping *mapping,
  1047. s32 *value)
  1048. {
  1049. int ret;
  1050. if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0)
  1051. return -EACCES;
  1052. ret = __uvc_ctrl_load_cur(chain, ctrl);
  1053. if (ret < 0)
  1054. return ret;
  1055. *value = mapping->get(mapping, UVC_GET_CUR,
  1056. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
  1057. return 0;
  1058. }
  1059. static int __uvc_query_v4l2_class(struct uvc_video_chain *chain, u32 req_id,
  1060. u32 found_id)
  1061. {
  1062. bool find_next = req_id & V4L2_CTRL_FLAG_NEXT_CTRL;
  1063. unsigned int i;
  1064. req_id &= V4L2_CTRL_ID_MASK;
  1065. for (i = 0; i < ARRAY_SIZE(uvc_control_classes); i++) {
  1066. if (!(chain->ctrl_class_bitmap & BIT(i)))
  1067. continue;
  1068. if (!find_next) {
  1069. if (uvc_control_classes[i] == req_id)
  1070. return i;
  1071. continue;
  1072. }
  1073. if (uvc_control_classes[i] > req_id &&
  1074. uvc_control_classes[i] < found_id)
  1075. return i;
  1076. }
  1077. return -ENODEV;
  1078. }
  1079. static int uvc_query_v4l2_class(struct uvc_video_chain *chain, u32 req_id,
  1080. u32 found_id, struct v4l2_queryctrl *v4l2_ctrl)
  1081. {
  1082. int idx;
  1083. idx = __uvc_query_v4l2_class(chain, req_id, found_id);
  1084. if (idx < 0)
  1085. return -ENODEV;
  1086. memset(v4l2_ctrl, 0, sizeof(*v4l2_ctrl));
  1087. v4l2_ctrl->id = uvc_control_classes[idx];
  1088. strscpy(v4l2_ctrl->name, v4l2_ctrl_get_name(v4l2_ctrl->id),
  1089. sizeof(v4l2_ctrl->name));
  1090. v4l2_ctrl->type = V4L2_CTRL_TYPE_CTRL_CLASS;
  1091. v4l2_ctrl->flags = V4L2_CTRL_FLAG_WRITE_ONLY
  1092. | V4L2_CTRL_FLAG_READ_ONLY;
  1093. return 0;
  1094. }
  1095. /*
  1096. * Check if control @v4l2_id can be accessed by the given control @ioctl
  1097. * (VIDIOC_G_EXT_CTRLS, VIDIOC_TRY_EXT_CTRLS or VIDIOC_S_EXT_CTRLS).
  1098. *
  1099. * For set operations on slave controls, check if the master's value is set to
  1100. * manual, either in the others controls set in the same ioctl call, or from
  1101. * the master's current value. This catches VIDIOC_S_EXT_CTRLS calls that set
  1102. * both the master and slave control, such as for instance setting
  1103. * auto_exposure=1, exposure_time_absolute=251.
  1104. */
  1105. int uvc_ctrl_is_accessible(struct uvc_video_chain *chain, u32 v4l2_id,
  1106. const struct v4l2_ext_controls *ctrls,
  1107. unsigned long ioctl)
  1108. {
  1109. struct uvc_control_mapping *master_map = NULL;
  1110. struct uvc_control *master_ctrl = NULL;
  1111. struct uvc_control_mapping *mapping;
  1112. struct uvc_control *ctrl;
  1113. bool read = ioctl == VIDIOC_G_EXT_CTRLS;
  1114. s32 val;
  1115. int ret;
  1116. int i;
  1117. if (__uvc_query_v4l2_class(chain, v4l2_id, 0) >= 0)
  1118. return -EACCES;
  1119. ctrl = uvc_find_control(chain, v4l2_id, &mapping);
  1120. if (!ctrl)
  1121. return -EINVAL;
  1122. if (!(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) && read)
  1123. return -EACCES;
  1124. if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR) && !read)
  1125. return -EACCES;
  1126. if (ioctl != VIDIOC_S_EXT_CTRLS || !mapping->master_id)
  1127. return 0;
  1128. /*
  1129. * Iterate backwards in cases where the master control is accessed
  1130. * multiple times in the same ioctl. We want the last value.
  1131. */
  1132. for (i = ctrls->count - 1; i >= 0; i--) {
  1133. if (ctrls->controls[i].id == mapping->master_id)
  1134. return ctrls->controls[i].value ==
  1135. mapping->master_manual ? 0 : -EACCES;
  1136. }
  1137. __uvc_find_control(ctrl->entity, mapping->master_id, &master_map,
  1138. &master_ctrl, 0);
  1139. if (!master_ctrl || !(master_ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
  1140. return 0;
  1141. ret = __uvc_ctrl_get(chain, master_ctrl, master_map, &val);
  1142. if (ret >= 0 && val != mapping->master_manual)
  1143. return -EACCES;
  1144. return 0;
  1145. }
  1146. static const char *uvc_map_get_name(const struct uvc_control_mapping *map)
  1147. {
  1148. const char *name;
  1149. if (map->name)
  1150. return map->name;
  1151. name = v4l2_ctrl_get_name(map->id);
  1152. if (name)
  1153. return name;
  1154. return "Unknown Control";
  1155. }
  1156. static u32 uvc_get_ctrl_bitmap(struct uvc_control *ctrl,
  1157. struct uvc_control_mapping *mapping)
  1158. {
  1159. /*
  1160. * Some controls, like CT_AE_MODE_CONTROL, use GET_RES to represent
  1161. * the number of bits supported. Those controls do not list GET_MAX
  1162. * as supported.
  1163. */
  1164. if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)
  1165. return mapping->get(mapping, UVC_GET_RES,
  1166. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
  1167. if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX)
  1168. return mapping->get(mapping, UVC_GET_MAX,
  1169. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
  1170. return ~0;
  1171. }
  1172. static int __uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
  1173. struct uvc_control *ctrl,
  1174. struct uvc_control_mapping *mapping,
  1175. struct v4l2_queryctrl *v4l2_ctrl)
  1176. {
  1177. struct uvc_control_mapping *master_map = NULL;
  1178. struct uvc_control *master_ctrl = NULL;
  1179. memset(v4l2_ctrl, 0, sizeof(*v4l2_ctrl));
  1180. v4l2_ctrl->id = mapping->id;
  1181. v4l2_ctrl->type = mapping->v4l2_type;
  1182. strscpy(v4l2_ctrl->name, uvc_map_get_name(mapping),
  1183. sizeof(v4l2_ctrl->name));
  1184. v4l2_ctrl->flags = 0;
  1185. if (!(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
  1186. v4l2_ctrl->flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
  1187. if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
  1188. v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  1189. if (mapping->master_id)
  1190. __uvc_find_control(ctrl->entity, mapping->master_id,
  1191. &master_map, &master_ctrl, 0);
  1192. if (master_ctrl && (master_ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR)) {
  1193. s32 val;
  1194. int ret = __uvc_ctrl_get(chain, master_ctrl, master_map, &val);
  1195. if (ret < 0)
  1196. return ret;
  1197. if (val != mapping->master_manual)
  1198. v4l2_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
  1199. }
  1200. if (!ctrl->cached) {
  1201. int ret = uvc_ctrl_populate_cache(chain, ctrl);
  1202. if (ret < 0)
  1203. return ret;
  1204. }
  1205. if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
  1206. v4l2_ctrl->default_value = mapping->get(mapping, UVC_GET_DEF,
  1207. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF));
  1208. }
  1209. switch (mapping->v4l2_type) {
  1210. case V4L2_CTRL_TYPE_MENU:
  1211. v4l2_ctrl->minimum = ffs(mapping->menu_mask) - 1;
  1212. v4l2_ctrl->maximum = fls(mapping->menu_mask) - 1;
  1213. v4l2_ctrl->step = 1;
  1214. return 0;
  1215. case V4L2_CTRL_TYPE_BOOLEAN:
  1216. v4l2_ctrl->minimum = 0;
  1217. v4l2_ctrl->maximum = 1;
  1218. v4l2_ctrl->step = 1;
  1219. return 0;
  1220. case V4L2_CTRL_TYPE_BUTTON:
  1221. v4l2_ctrl->minimum = 0;
  1222. v4l2_ctrl->maximum = 0;
  1223. v4l2_ctrl->step = 0;
  1224. return 0;
  1225. case V4L2_CTRL_TYPE_BITMASK:
  1226. v4l2_ctrl->minimum = 0;
  1227. v4l2_ctrl->maximum = uvc_get_ctrl_bitmap(ctrl, mapping);
  1228. v4l2_ctrl->step = 0;
  1229. return 0;
  1230. default:
  1231. break;
  1232. }
  1233. if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN)
  1234. v4l2_ctrl->minimum = mapping->get(mapping, UVC_GET_MIN,
  1235. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
  1236. if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX)
  1237. v4l2_ctrl->maximum = mapping->get(mapping, UVC_GET_MAX,
  1238. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
  1239. if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)
  1240. v4l2_ctrl->step = mapping->get(mapping, UVC_GET_RES,
  1241. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
  1242. return 0;
  1243. }
  1244. int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
  1245. struct v4l2_queryctrl *v4l2_ctrl)
  1246. {
  1247. struct uvc_control *ctrl;
  1248. struct uvc_control_mapping *mapping;
  1249. int ret;
  1250. ret = mutex_lock_interruptible(&chain->ctrl_mutex);
  1251. if (ret < 0)
  1252. return -ERESTARTSYS;
  1253. /* Check if the ctrl is a know class */
  1254. if (!(v4l2_ctrl->id & V4L2_CTRL_FLAG_NEXT_CTRL)) {
  1255. ret = uvc_query_v4l2_class(chain, v4l2_ctrl->id, 0, v4l2_ctrl);
  1256. if (!ret)
  1257. goto done;
  1258. }
  1259. ctrl = uvc_find_control(chain, v4l2_ctrl->id, &mapping);
  1260. if (ctrl == NULL) {
  1261. ret = -EINVAL;
  1262. goto done;
  1263. }
  1264. /*
  1265. * If we're enumerating control with V4L2_CTRL_FLAG_NEXT_CTRL, check if
  1266. * a class should be inserted between the previous control and the one
  1267. * we have just found.
  1268. */
  1269. if (v4l2_ctrl->id & V4L2_CTRL_FLAG_NEXT_CTRL) {
  1270. ret = uvc_query_v4l2_class(chain, v4l2_ctrl->id, mapping->id,
  1271. v4l2_ctrl);
  1272. if (!ret)
  1273. goto done;
  1274. }
  1275. ret = __uvc_query_v4l2_ctrl(chain, ctrl, mapping, v4l2_ctrl);
  1276. done:
  1277. mutex_unlock(&chain->ctrl_mutex);
  1278. return ret;
  1279. }
  1280. /*
  1281. * Mapping V4L2 controls to UVC controls can be straightforward if done well.
  1282. * Most of the UVC controls exist in V4L2, and can be mapped directly. Some
  1283. * must be grouped (for instance the Red Balance, Blue Balance and Do White
  1284. * Balance V4L2 controls use the White Balance Component UVC control) or
  1285. * otherwise translated. The approach we take here is to use a translation
  1286. * table for the controls that can be mapped directly, and handle the others
  1287. * manually.
  1288. */
  1289. int uvc_query_v4l2_menu(struct uvc_video_chain *chain,
  1290. struct v4l2_querymenu *query_menu)
  1291. {
  1292. struct uvc_control_mapping *mapping;
  1293. struct uvc_control *ctrl;
  1294. u32 index = query_menu->index;
  1295. u32 id = query_menu->id;
  1296. const char *name;
  1297. int ret;
  1298. memset(query_menu, 0, sizeof(*query_menu));
  1299. query_menu->id = id;
  1300. query_menu->index = index;
  1301. if (index >= BITS_PER_TYPE(mapping->menu_mask))
  1302. return -EINVAL;
  1303. ret = mutex_lock_interruptible(&chain->ctrl_mutex);
  1304. if (ret < 0)
  1305. return -ERESTARTSYS;
  1306. ctrl = uvc_find_control(chain, query_menu->id, &mapping);
  1307. if (ctrl == NULL || mapping->v4l2_type != V4L2_CTRL_TYPE_MENU) {
  1308. ret = -EINVAL;
  1309. goto done;
  1310. }
  1311. if (!test_bit(query_menu->index, &mapping->menu_mask)) {
  1312. ret = -EINVAL;
  1313. goto done;
  1314. }
  1315. if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK) {
  1316. int mask;
  1317. if (!ctrl->cached) {
  1318. ret = uvc_ctrl_populate_cache(chain, ctrl);
  1319. if (ret < 0)
  1320. goto done;
  1321. }
  1322. mask = uvc_mapping_get_menu_value(mapping, query_menu->index);
  1323. if (mask < 0) {
  1324. ret = mask;
  1325. goto done;
  1326. }
  1327. if (!(uvc_get_ctrl_bitmap(ctrl, mapping) & mask)) {
  1328. ret = -EINVAL;
  1329. goto done;
  1330. }
  1331. }
  1332. name = uvc_mapping_get_menu_name(mapping, query_menu->index);
  1333. if (!name) {
  1334. ret = -EINVAL;
  1335. goto done;
  1336. }
  1337. strscpy(query_menu->name, name, sizeof(query_menu->name));
  1338. done:
  1339. mutex_unlock(&chain->ctrl_mutex);
  1340. return ret;
  1341. }
  1342. /* --------------------------------------------------------------------------
  1343. * Ctrl event handling
  1344. */
  1345. static void uvc_ctrl_fill_event(struct uvc_video_chain *chain,
  1346. struct v4l2_event *ev,
  1347. struct uvc_control *ctrl,
  1348. struct uvc_control_mapping *mapping,
  1349. s32 value, u32 changes)
  1350. {
  1351. struct v4l2_queryctrl v4l2_ctrl;
  1352. __uvc_query_v4l2_ctrl(chain, ctrl, mapping, &v4l2_ctrl);
  1353. memset(ev, 0, sizeof(*ev));
  1354. ev->type = V4L2_EVENT_CTRL;
  1355. ev->id = v4l2_ctrl.id;
  1356. ev->u.ctrl.value = value;
  1357. ev->u.ctrl.changes = changes;
  1358. ev->u.ctrl.type = v4l2_ctrl.type;
  1359. ev->u.ctrl.flags = v4l2_ctrl.flags;
  1360. ev->u.ctrl.minimum = v4l2_ctrl.minimum;
  1361. ev->u.ctrl.maximum = v4l2_ctrl.maximum;
  1362. ev->u.ctrl.step = v4l2_ctrl.step;
  1363. ev->u.ctrl.default_value = v4l2_ctrl.default_value;
  1364. }
  1365. /*
  1366. * Send control change events to all subscribers for the @ctrl control. By
  1367. * default the subscriber that generated the event, as identified by @handle,
  1368. * is not notified unless it has set the V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK flag.
  1369. * @handle can be NULL for asynchronous events related to auto-update controls,
  1370. * in which case all subscribers are notified.
  1371. */
  1372. static void uvc_ctrl_send_event(struct uvc_video_chain *chain,
  1373. struct uvc_fh *handle, struct uvc_control *ctrl,
  1374. struct uvc_control_mapping *mapping, s32 value, u32 changes)
  1375. {
  1376. struct v4l2_fh *originator = handle ? &handle->vfh : NULL;
  1377. struct v4l2_subscribed_event *sev;
  1378. struct v4l2_event ev;
  1379. if (list_empty(&mapping->ev_subs))
  1380. return;
  1381. uvc_ctrl_fill_event(chain, &ev, ctrl, mapping, value, changes);
  1382. list_for_each_entry(sev, &mapping->ev_subs, node) {
  1383. if (sev->fh != originator ||
  1384. (sev->flags & V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK) ||
  1385. (changes & V4L2_EVENT_CTRL_CH_FLAGS))
  1386. v4l2_event_queue_fh(sev->fh, &ev);
  1387. }
  1388. }
  1389. /*
  1390. * Send control change events for the slave of the @master control identified
  1391. * by the V4L2 ID @slave_id. The @handle identifies the event subscriber that
  1392. * generated the event and may be NULL for auto-update events.
  1393. */
  1394. static void uvc_ctrl_send_slave_event(struct uvc_video_chain *chain,
  1395. struct uvc_fh *handle, struct uvc_control *master, u32 slave_id)
  1396. {
  1397. struct uvc_control_mapping *mapping = NULL;
  1398. struct uvc_control *ctrl = NULL;
  1399. u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
  1400. s32 val = 0;
  1401. __uvc_find_control(master->entity, slave_id, &mapping, &ctrl, 0);
  1402. if (ctrl == NULL)
  1403. return;
  1404. if (__uvc_ctrl_get(chain, ctrl, mapping, &val) == 0)
  1405. changes |= V4L2_EVENT_CTRL_CH_VALUE;
  1406. uvc_ctrl_send_event(chain, handle, ctrl, mapping, val, changes);
  1407. }
  1408. static void uvc_ctrl_set_handle(struct uvc_fh *handle, struct uvc_control *ctrl,
  1409. struct uvc_fh *new_handle)
  1410. {
  1411. lockdep_assert_held(&handle->chain->ctrl_mutex);
  1412. if (new_handle) {
  1413. if (ctrl->handle)
  1414. dev_warn_ratelimited(&handle->stream->dev->udev->dev,
  1415. "UVC non compliance: Setting an async control with a pending operation.");
  1416. if (new_handle == ctrl->handle)
  1417. return;
  1418. if (ctrl->handle) {
  1419. WARN_ON(!ctrl->handle->pending_async_ctrls);
  1420. if (ctrl->handle->pending_async_ctrls)
  1421. ctrl->handle->pending_async_ctrls--;
  1422. }
  1423. ctrl->handle = new_handle;
  1424. handle->pending_async_ctrls++;
  1425. return;
  1426. }
  1427. /* Cannot clear the handle for a control not owned by us.*/
  1428. if (WARN_ON(ctrl->handle != handle))
  1429. return;
  1430. ctrl->handle = NULL;
  1431. if (WARN_ON(!handle->pending_async_ctrls))
  1432. return;
  1433. handle->pending_async_ctrls--;
  1434. }
  1435. void uvc_ctrl_status_event(struct uvc_video_chain *chain,
  1436. struct uvc_control *ctrl, const u8 *data)
  1437. {
  1438. struct uvc_control_mapping *mapping;
  1439. struct uvc_fh *handle;
  1440. unsigned int i;
  1441. mutex_lock(&chain->ctrl_mutex);
  1442. handle = ctrl->handle;
  1443. if (handle)
  1444. uvc_ctrl_set_handle(handle, ctrl, NULL);
  1445. list_for_each_entry(mapping, &ctrl->info.mappings, list) {
  1446. s32 value = mapping->get(mapping, UVC_GET_CUR, data);
  1447. /*
  1448. * handle may be NULL here if the device sends auto-update
  1449. * events without a prior related control set from userspace.
  1450. */
  1451. for (i = 0; i < ARRAY_SIZE(mapping->slave_ids); ++i) {
  1452. if (!mapping->slave_ids[i])
  1453. break;
  1454. uvc_ctrl_send_slave_event(chain, handle, ctrl,
  1455. mapping->slave_ids[i]);
  1456. }
  1457. uvc_ctrl_send_event(chain, handle, ctrl, mapping, value,
  1458. V4L2_EVENT_CTRL_CH_VALUE);
  1459. }
  1460. mutex_unlock(&chain->ctrl_mutex);
  1461. }
  1462. static void uvc_ctrl_status_event_work(struct work_struct *work)
  1463. {
  1464. struct uvc_device *dev = container_of(work, struct uvc_device,
  1465. async_ctrl.work);
  1466. struct uvc_ctrl_work *w = &dev->async_ctrl;
  1467. int ret;
  1468. uvc_ctrl_status_event(w->chain, w->ctrl, w->data);
  1469. /* The barrier is needed to synchronize with uvc_status_stop(). */
  1470. if (smp_load_acquire(&dev->flush_status))
  1471. return;
  1472. /* Resubmit the URB. */
  1473. w->urb->interval = dev->int_ep->desc.bInterval;
  1474. ret = usb_submit_urb(w->urb, GFP_KERNEL);
  1475. if (ret < 0)
  1476. dev_err(&dev->udev->dev,
  1477. "Failed to resubmit status URB (%d).\n", ret);
  1478. }
  1479. bool uvc_ctrl_status_event_async(struct urb *urb, struct uvc_video_chain *chain,
  1480. struct uvc_control *ctrl, const u8 *data)
  1481. {
  1482. struct uvc_device *dev = chain->dev;
  1483. struct uvc_ctrl_work *w = &dev->async_ctrl;
  1484. if (list_empty(&ctrl->info.mappings))
  1485. return false;
  1486. w->data = data;
  1487. w->urb = urb;
  1488. w->chain = chain;
  1489. w->ctrl = ctrl;
  1490. schedule_work(&w->work);
  1491. return true;
  1492. }
  1493. static bool uvc_ctrl_xctrls_has_control(const struct v4l2_ext_control *xctrls,
  1494. unsigned int xctrls_count, u32 id)
  1495. {
  1496. unsigned int i;
  1497. for (i = 0; i < xctrls_count; ++i) {
  1498. if (xctrls[i].id == id)
  1499. return true;
  1500. }
  1501. return false;
  1502. }
  1503. static void uvc_ctrl_send_events(struct uvc_fh *handle,
  1504. struct uvc_entity *entity,
  1505. const struct v4l2_ext_control *xctrls,
  1506. unsigned int xctrls_count)
  1507. {
  1508. struct uvc_control_mapping *mapping;
  1509. struct uvc_control *ctrl;
  1510. unsigned int i;
  1511. unsigned int j;
  1512. for (i = 0; i < xctrls_count; ++i) {
  1513. u32 changes = V4L2_EVENT_CTRL_CH_VALUE;
  1514. ctrl = uvc_find_control(handle->chain, xctrls[i].id, &mapping);
  1515. if (ctrl->entity != entity)
  1516. continue;
  1517. if (ctrl->info.flags & UVC_CTRL_FLAG_ASYNCHRONOUS)
  1518. /* Notification will be sent from an Interrupt event. */
  1519. continue;
  1520. for (j = 0; j < ARRAY_SIZE(mapping->slave_ids); ++j) {
  1521. u32 slave_id = mapping->slave_ids[j];
  1522. if (!slave_id)
  1523. break;
  1524. /*
  1525. * We can skip sending an event for the slave if the
  1526. * slave is being modified in the same transaction.
  1527. */
  1528. if (uvc_ctrl_xctrls_has_control(xctrls, xctrls_count,
  1529. slave_id))
  1530. continue;
  1531. uvc_ctrl_send_slave_event(handle->chain, handle, ctrl,
  1532. slave_id);
  1533. }
  1534. /*
  1535. * If the master is being modified in the same transaction
  1536. * flags may change too.
  1537. */
  1538. if (mapping->master_id &&
  1539. uvc_ctrl_xctrls_has_control(xctrls, xctrls_count,
  1540. mapping->master_id))
  1541. changes |= V4L2_EVENT_CTRL_CH_FLAGS;
  1542. uvc_ctrl_send_event(handle->chain, handle, ctrl, mapping,
  1543. xctrls[i].value, changes);
  1544. }
  1545. }
  1546. static int uvc_ctrl_add_event(struct v4l2_subscribed_event *sev, unsigned elems)
  1547. {
  1548. struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);
  1549. struct uvc_control_mapping *mapping;
  1550. struct uvc_control *ctrl;
  1551. int ret;
  1552. ret = mutex_lock_interruptible(&handle->chain->ctrl_mutex);
  1553. if (ret < 0)
  1554. return -ERESTARTSYS;
  1555. if (__uvc_query_v4l2_class(handle->chain, sev->id, 0) >= 0) {
  1556. ret = 0;
  1557. goto done;
  1558. }
  1559. ctrl = uvc_find_control(handle->chain, sev->id, &mapping);
  1560. if (ctrl == NULL) {
  1561. ret = -EINVAL;
  1562. goto done;
  1563. }
  1564. list_add_tail(&sev->node, &mapping->ev_subs);
  1565. if (sev->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL) {
  1566. struct v4l2_event ev;
  1567. u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
  1568. s32 val = 0;
  1569. if (__uvc_ctrl_get(handle->chain, ctrl, mapping, &val) == 0)
  1570. changes |= V4L2_EVENT_CTRL_CH_VALUE;
  1571. uvc_ctrl_fill_event(handle->chain, &ev, ctrl, mapping, val,
  1572. changes);
  1573. /*
  1574. * Mark the queue as active, allowing this initial event to be
  1575. * accepted.
  1576. */
  1577. sev->elems = elems;
  1578. v4l2_event_queue_fh(sev->fh, &ev);
  1579. }
  1580. done:
  1581. mutex_unlock(&handle->chain->ctrl_mutex);
  1582. return ret;
  1583. }
  1584. static void uvc_ctrl_del_event(struct v4l2_subscribed_event *sev)
  1585. {
  1586. struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);
  1587. mutex_lock(&handle->chain->ctrl_mutex);
  1588. if (__uvc_query_v4l2_class(handle->chain, sev->id, 0) >= 0)
  1589. goto done;
  1590. list_del(&sev->node);
  1591. done:
  1592. mutex_unlock(&handle->chain->ctrl_mutex);
  1593. }
  1594. const struct v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops = {
  1595. .add = uvc_ctrl_add_event,
  1596. .del = uvc_ctrl_del_event,
  1597. .replace = v4l2_ctrl_replace,
  1598. .merge = v4l2_ctrl_merge,
  1599. };
  1600. /* --------------------------------------------------------------------------
  1601. * Control transactions
  1602. *
  1603. * To make extended set operations as atomic as the hardware allows, controls
  1604. * are handled using begin/commit/rollback operations.
  1605. *
  1606. * At the beginning of a set request, uvc_ctrl_begin should be called to
  1607. * initialize the request. This function acquires the control lock.
  1608. *
  1609. * When setting a control, the new value is stored in the control data field
  1610. * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
  1611. * later processing. If the UVC and V4L2 control sizes differ, the current
  1612. * value is loaded from the hardware before storing the new value in the data
  1613. * field.
  1614. *
  1615. * After processing all controls in the transaction, uvc_ctrl_commit or
  1616. * uvc_ctrl_rollback must be called to apply the pending changes to the
  1617. * hardware or revert them. When applying changes, all controls marked as
  1618. * dirty will be modified in the UVC device, and the dirty flag will be
  1619. * cleared. When reverting controls, the control data field
  1620. * UVC_CTRL_DATA_CURRENT is reverted to its previous value
  1621. * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
  1622. * control lock.
  1623. */
  1624. int uvc_ctrl_begin(struct uvc_video_chain *chain)
  1625. {
  1626. return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0;
  1627. }
  1628. /*
  1629. * Returns the number of uvc controls that have been correctly set, or a
  1630. * negative number if there has been an error.
  1631. */
  1632. static int uvc_ctrl_commit_entity(struct uvc_device *dev,
  1633. struct uvc_fh *handle,
  1634. struct uvc_entity *entity,
  1635. int rollback,
  1636. struct uvc_control **err_ctrl)
  1637. {
  1638. unsigned int processed_ctrls = 0;
  1639. struct uvc_control *ctrl;
  1640. unsigned int i;
  1641. int ret = 0;
  1642. if (entity == NULL)
  1643. return 0;
  1644. for (i = 0; i < entity->ncontrols; ++i) {
  1645. ctrl = &entity->controls[i];
  1646. if (!ctrl->initialized)
  1647. continue;
  1648. /*
  1649. * Reset the loaded flag for auto-update controls that were
  1650. * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
  1651. * uvc_ctrl_get from using the cached value, and for write-only
  1652. * controls to prevent uvc_ctrl_set from setting bits not
  1653. * explicitly set by the user.
  1654. */
  1655. if (ctrl->info.flags & UVC_CTRL_FLAG_AUTO_UPDATE ||
  1656. !(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
  1657. ctrl->loaded = 0;
  1658. if (!ctrl->dirty)
  1659. continue;
  1660. if (!rollback)
  1661. ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id,
  1662. dev->intfnum, ctrl->info.selector,
  1663. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  1664. ctrl->info.size);
  1665. if (!ret)
  1666. processed_ctrls++;
  1667. if (rollback || ret < 0)
  1668. memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  1669. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
  1670. ctrl->info.size);
  1671. ctrl->dirty = 0;
  1672. if (ret < 0 && !rollback) {
  1673. if (err_ctrl)
  1674. *err_ctrl = ctrl;
  1675. /*
  1676. * If we fail to set a control, we need to rollback
  1677. * the next ones.
  1678. */
  1679. rollback = 1;
  1680. }
  1681. if (!rollback && handle && !ret &&
  1682. ctrl->info.flags & UVC_CTRL_FLAG_ASYNCHRONOUS)
  1683. uvc_ctrl_set_handle(handle, ctrl, handle);
  1684. }
  1685. if (ret)
  1686. return ret;
  1687. return processed_ctrls;
  1688. }
  1689. static int uvc_ctrl_find_ctrl_idx(struct uvc_entity *entity,
  1690. struct v4l2_ext_controls *ctrls,
  1691. struct uvc_control *uvc_control)
  1692. {
  1693. struct uvc_control_mapping *mapping = NULL;
  1694. struct uvc_control *ctrl_found = NULL;
  1695. unsigned int i;
  1696. if (!entity)
  1697. return ctrls->count;
  1698. for (i = 0; i < ctrls->count; i++) {
  1699. __uvc_find_control(entity, ctrls->controls[i].id, &mapping,
  1700. &ctrl_found, 0);
  1701. if (uvc_control == ctrl_found)
  1702. return i;
  1703. }
  1704. return ctrls->count;
  1705. }
  1706. int __uvc_ctrl_commit(struct uvc_fh *handle, int rollback,
  1707. struct v4l2_ext_controls *ctrls)
  1708. {
  1709. struct uvc_video_chain *chain = handle->chain;
  1710. struct uvc_control *err_ctrl;
  1711. struct uvc_entity *entity;
  1712. int ret_out = 0;
  1713. int ret;
  1714. /* Find the control. */
  1715. list_for_each_entry(entity, &chain->entities, chain) {
  1716. ret = uvc_ctrl_commit_entity(chain->dev, handle, entity,
  1717. rollback, &err_ctrl);
  1718. if (ret < 0) {
  1719. if (ctrls)
  1720. ctrls->error_idx =
  1721. uvc_ctrl_find_ctrl_idx(entity, ctrls,
  1722. err_ctrl);
  1723. /*
  1724. * When we fail to commit an entity, we need to
  1725. * restore the UVC_CTRL_DATA_BACKUP for all the
  1726. * controls in the other entities, otherwise our cache
  1727. * and the hardware will be out of sync.
  1728. */
  1729. rollback = 1;
  1730. ret_out = ret;
  1731. } else if (ret > 0 && !rollback) {
  1732. uvc_ctrl_send_events(handle, entity,
  1733. ctrls->controls, ctrls->count);
  1734. }
  1735. }
  1736. mutex_unlock(&chain->ctrl_mutex);
  1737. return ret_out;
  1738. }
  1739. int uvc_ctrl_get(struct uvc_video_chain *chain,
  1740. struct v4l2_ext_control *xctrl)
  1741. {
  1742. struct uvc_control *ctrl;
  1743. struct uvc_control_mapping *mapping;
  1744. if (__uvc_query_v4l2_class(chain, xctrl->id, 0) >= 0)
  1745. return -EACCES;
  1746. ctrl = uvc_find_control(chain, xctrl->id, &mapping);
  1747. if (ctrl == NULL)
  1748. return -EINVAL;
  1749. return __uvc_ctrl_get(chain, ctrl, mapping, &xctrl->value);
  1750. }
  1751. int uvc_ctrl_set(struct uvc_fh *handle,
  1752. struct v4l2_ext_control *xctrl)
  1753. {
  1754. struct uvc_video_chain *chain = handle->chain;
  1755. struct uvc_control *ctrl;
  1756. struct uvc_control_mapping *mapping;
  1757. s32 value;
  1758. u32 step;
  1759. s32 min;
  1760. s32 max;
  1761. int ret;
  1762. if (__uvc_query_v4l2_class(chain, xctrl->id, 0) >= 0)
  1763. return -EACCES;
  1764. ctrl = uvc_find_control(chain, xctrl->id, &mapping);
  1765. if (ctrl == NULL)
  1766. return -EINVAL;
  1767. if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
  1768. return -EACCES;
  1769. /* Clamp out of range values. */
  1770. switch (mapping->v4l2_type) {
  1771. case V4L2_CTRL_TYPE_INTEGER:
  1772. if (!ctrl->cached) {
  1773. ret = uvc_ctrl_populate_cache(chain, ctrl);
  1774. if (ret < 0)
  1775. return ret;
  1776. }
  1777. min = mapping->get(mapping, UVC_GET_MIN,
  1778. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
  1779. max = mapping->get(mapping, UVC_GET_MAX,
  1780. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
  1781. step = mapping->get(mapping, UVC_GET_RES,
  1782. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
  1783. if (step == 0)
  1784. step = 1;
  1785. xctrl->value = min + DIV_ROUND_CLOSEST((u32)(xctrl->value - min),
  1786. step) * step;
  1787. if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
  1788. xctrl->value = clamp(xctrl->value, min, max);
  1789. else
  1790. xctrl->value = clamp_t(u32, xctrl->value, min, max);
  1791. value = xctrl->value;
  1792. break;
  1793. case V4L2_CTRL_TYPE_BITMASK:
  1794. if (!ctrl->cached) {
  1795. ret = uvc_ctrl_populate_cache(chain, ctrl);
  1796. if (ret < 0)
  1797. return ret;
  1798. }
  1799. xctrl->value &= uvc_get_ctrl_bitmap(ctrl, mapping);
  1800. value = xctrl->value;
  1801. break;
  1802. case V4L2_CTRL_TYPE_BOOLEAN:
  1803. xctrl->value = clamp(xctrl->value, 0, 1);
  1804. value = xctrl->value;
  1805. break;
  1806. case V4L2_CTRL_TYPE_MENU:
  1807. if (xctrl->value < (ffs(mapping->menu_mask) - 1) ||
  1808. xctrl->value > (fls(mapping->menu_mask) - 1))
  1809. return -ERANGE;
  1810. if (!test_bit(xctrl->value, &mapping->menu_mask))
  1811. return -EINVAL;
  1812. value = uvc_mapping_get_menu_value(mapping, xctrl->value);
  1813. /*
  1814. * Valid menu indices are reported by the GET_RES request for
  1815. * UVC controls that support it.
  1816. */
  1817. if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK) {
  1818. if (!ctrl->cached) {
  1819. ret = uvc_ctrl_populate_cache(chain, ctrl);
  1820. if (ret < 0)
  1821. return ret;
  1822. }
  1823. if (!(uvc_get_ctrl_bitmap(ctrl, mapping) & value))
  1824. return -EINVAL;
  1825. }
  1826. break;
  1827. default:
  1828. value = xctrl->value;
  1829. break;
  1830. }
  1831. /*
  1832. * If the mapping doesn't span the whole UVC control, the current value
  1833. * needs to be loaded from the device to perform the read-modify-write
  1834. * operation.
  1835. */
  1836. if ((ctrl->info.size * 8) != mapping->size) {
  1837. ret = __uvc_ctrl_load_cur(chain, ctrl);
  1838. if (ret < 0)
  1839. return ret;
  1840. }
  1841. /* Backup the current value in case we need to rollback later. */
  1842. if (!ctrl->dirty) {
  1843. memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
  1844. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  1845. ctrl->info.size);
  1846. }
  1847. mapping->set(mapping, value,
  1848. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
  1849. ctrl->dirty = 1;
  1850. ctrl->modified = 1;
  1851. return 0;
  1852. }
  1853. /* --------------------------------------------------------------------------
  1854. * Dynamic controls
  1855. */
  1856. /*
  1857. * Retrieve flags for a given control
  1858. */
  1859. static int uvc_ctrl_get_flags(struct uvc_device *dev,
  1860. const struct uvc_control *ctrl,
  1861. struct uvc_control_info *info)
  1862. {
  1863. u8 *data;
  1864. int ret;
  1865. data = kmalloc(1, GFP_KERNEL);
  1866. if (data == NULL)
  1867. return -ENOMEM;
  1868. if (ctrl->entity->get_info)
  1869. ret = ctrl->entity->get_info(dev, ctrl->entity,
  1870. ctrl->info.selector, data);
  1871. else
  1872. ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id,
  1873. dev->intfnum, info->selector, data, 1);
  1874. if (!ret) {
  1875. info->flags &= ~(UVC_CTRL_FLAG_GET_CUR |
  1876. UVC_CTRL_FLAG_SET_CUR |
  1877. UVC_CTRL_FLAG_AUTO_UPDATE |
  1878. UVC_CTRL_FLAG_ASYNCHRONOUS);
  1879. info->flags |= (data[0] & UVC_CONTROL_CAP_GET ?
  1880. UVC_CTRL_FLAG_GET_CUR : 0)
  1881. | (data[0] & UVC_CONTROL_CAP_SET ?
  1882. UVC_CTRL_FLAG_SET_CUR : 0)
  1883. | (data[0] & UVC_CONTROL_CAP_AUTOUPDATE ?
  1884. UVC_CTRL_FLAG_AUTO_UPDATE : 0)
  1885. | (data[0] & UVC_CONTROL_CAP_ASYNCHRONOUS ?
  1886. UVC_CTRL_FLAG_ASYNCHRONOUS : 0);
  1887. }
  1888. kfree(data);
  1889. return ret;
  1890. }
  1891. static void uvc_ctrl_fixup_xu_info(struct uvc_device *dev,
  1892. const struct uvc_control *ctrl, struct uvc_control_info *info)
  1893. {
  1894. struct uvc_ctrl_fixup {
  1895. struct usb_device_id id;
  1896. u8 entity;
  1897. u8 selector;
  1898. u8 flags;
  1899. };
  1900. static const struct uvc_ctrl_fixup fixups[] = {
  1901. { { USB_DEVICE(0x046d, 0x08c2) }, 9, 1,
  1902. UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
  1903. UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
  1904. UVC_CTRL_FLAG_AUTO_UPDATE },
  1905. { { USB_DEVICE(0x046d, 0x08cc) }, 9, 1,
  1906. UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
  1907. UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
  1908. UVC_CTRL_FLAG_AUTO_UPDATE },
  1909. { { USB_DEVICE(0x046d, 0x0994) }, 9, 1,
  1910. UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
  1911. UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
  1912. UVC_CTRL_FLAG_AUTO_UPDATE },
  1913. };
  1914. unsigned int i;
  1915. for (i = 0; i < ARRAY_SIZE(fixups); ++i) {
  1916. if (!usb_match_one_id(dev->intf, &fixups[i].id))
  1917. continue;
  1918. if (fixups[i].entity == ctrl->entity->id &&
  1919. fixups[i].selector == info->selector) {
  1920. info->flags = fixups[i].flags;
  1921. return;
  1922. }
  1923. }
  1924. }
  1925. /*
  1926. * Query control information (size and flags) for XU controls.
  1927. */
  1928. static int uvc_ctrl_fill_xu_info(struct uvc_device *dev,
  1929. const struct uvc_control *ctrl, struct uvc_control_info *info)
  1930. {
  1931. u8 *data;
  1932. int ret;
  1933. data = kmalloc(2, GFP_KERNEL);
  1934. if (data == NULL)
  1935. return -ENOMEM;
  1936. memcpy(info->entity, ctrl->entity->guid, sizeof(info->entity));
  1937. info->index = ctrl->index;
  1938. info->selector = ctrl->index + 1;
  1939. /* Query and verify the control length (GET_LEN) */
  1940. ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id, dev->intfnum,
  1941. info->selector, data, 2);
  1942. if (ret < 0) {
  1943. uvc_dbg(dev, CONTROL,
  1944. "GET_LEN failed on control %pUl/%u (%d)\n",
  1945. info->entity, info->selector, ret);
  1946. goto done;
  1947. }
  1948. info->size = le16_to_cpup((__le16 *)data);
  1949. info->flags = UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX
  1950. | UVC_CTRL_FLAG_GET_RES | UVC_CTRL_FLAG_GET_DEF;
  1951. ret = uvc_ctrl_get_flags(dev, ctrl, info);
  1952. if (ret < 0) {
  1953. uvc_dbg(dev, CONTROL,
  1954. "Failed to get flags for control %pUl/%u (%d)\n",
  1955. info->entity, info->selector, ret);
  1956. goto done;
  1957. }
  1958. uvc_ctrl_fixup_xu_info(dev, ctrl, info);
  1959. uvc_dbg(dev, CONTROL,
  1960. "XU control %pUl/%u queried: len %u, flags { get %u set %u auto %u }\n",
  1961. info->entity, info->selector, info->size,
  1962. (info->flags & UVC_CTRL_FLAG_GET_CUR) ? 1 : 0,
  1963. (info->flags & UVC_CTRL_FLAG_SET_CUR) ? 1 : 0,
  1964. (info->flags & UVC_CTRL_FLAG_AUTO_UPDATE) ? 1 : 0);
  1965. done:
  1966. kfree(data);
  1967. return ret;
  1968. }
  1969. static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
  1970. const struct uvc_control_info *info);
  1971. static int uvc_ctrl_init_xu_ctrl(struct uvc_device *dev,
  1972. struct uvc_control *ctrl)
  1973. {
  1974. struct uvc_control_info info;
  1975. int ret;
  1976. if (ctrl->initialized)
  1977. return 0;
  1978. ret = uvc_ctrl_fill_xu_info(dev, ctrl, &info);
  1979. if (ret < 0)
  1980. return ret;
  1981. ret = uvc_ctrl_add_info(dev, ctrl, &info);
  1982. if (ret < 0)
  1983. uvc_dbg(dev, CONTROL,
  1984. "Failed to initialize control %pUl/%u on device %s entity %u\n",
  1985. info.entity, info.selector, dev->udev->devpath,
  1986. ctrl->entity->id);
  1987. return ret;
  1988. }
  1989. int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
  1990. struct uvc_xu_control_query *xqry)
  1991. {
  1992. struct uvc_entity *entity, *iter;
  1993. struct uvc_control *ctrl;
  1994. unsigned int i;
  1995. bool found;
  1996. u32 reqflags;
  1997. u16 size;
  1998. u8 *data = NULL;
  1999. int ret;
  2000. /* Find the extension unit. */
  2001. entity = NULL;
  2002. list_for_each_entry(iter, &chain->entities, chain) {
  2003. if (UVC_ENTITY_TYPE(iter) == UVC_VC_EXTENSION_UNIT &&
  2004. iter->id == xqry->unit) {
  2005. entity = iter;
  2006. break;
  2007. }
  2008. }
  2009. if (!entity) {
  2010. uvc_dbg(chain->dev, CONTROL, "Extension unit %u not found\n",
  2011. xqry->unit);
  2012. return -ENOENT;
  2013. }
  2014. /* Find the control and perform delayed initialization if needed. */
  2015. found = false;
  2016. for (i = 0; i < entity->ncontrols; ++i) {
  2017. ctrl = &entity->controls[i];
  2018. if (ctrl->index == xqry->selector - 1) {
  2019. found = true;
  2020. break;
  2021. }
  2022. }
  2023. if (!found) {
  2024. uvc_dbg(chain->dev, CONTROL, "Control %pUl/%u not found\n",
  2025. entity->guid, xqry->selector);
  2026. return -ENOENT;
  2027. }
  2028. if (mutex_lock_interruptible(&chain->ctrl_mutex))
  2029. return -ERESTARTSYS;
  2030. ret = uvc_ctrl_init_xu_ctrl(chain->dev, ctrl);
  2031. if (ret < 0) {
  2032. ret = -ENOENT;
  2033. goto done;
  2034. }
  2035. /* Validate the required buffer size and flags for the request */
  2036. reqflags = 0;
  2037. size = ctrl->info.size;
  2038. switch (xqry->query) {
  2039. case UVC_GET_CUR:
  2040. reqflags = UVC_CTRL_FLAG_GET_CUR;
  2041. break;
  2042. case UVC_GET_MIN:
  2043. reqflags = UVC_CTRL_FLAG_GET_MIN;
  2044. break;
  2045. case UVC_GET_MAX:
  2046. reqflags = UVC_CTRL_FLAG_GET_MAX;
  2047. break;
  2048. case UVC_GET_DEF:
  2049. reqflags = UVC_CTRL_FLAG_GET_DEF;
  2050. break;
  2051. case UVC_GET_RES:
  2052. reqflags = UVC_CTRL_FLAG_GET_RES;
  2053. break;
  2054. case UVC_SET_CUR:
  2055. reqflags = UVC_CTRL_FLAG_SET_CUR;
  2056. break;
  2057. case UVC_GET_LEN:
  2058. size = 2;
  2059. break;
  2060. case UVC_GET_INFO:
  2061. size = 1;
  2062. break;
  2063. default:
  2064. ret = -EINVAL;
  2065. goto done;
  2066. }
  2067. if (size != xqry->size) {
  2068. ret = -ENOBUFS;
  2069. goto done;
  2070. }
  2071. if (reqflags && !(ctrl->info.flags & reqflags)) {
  2072. ret = -EBADRQC;
  2073. goto done;
  2074. }
  2075. data = kmalloc(size, GFP_KERNEL);
  2076. if (data == NULL) {
  2077. ret = -ENOMEM;
  2078. goto done;
  2079. }
  2080. if (xqry->query == UVC_SET_CUR &&
  2081. copy_from_user(data, xqry->data, size)) {
  2082. ret = -EFAULT;
  2083. goto done;
  2084. }
  2085. ret = uvc_query_ctrl(chain->dev, xqry->query, xqry->unit,
  2086. chain->dev->intfnum, xqry->selector, data, size);
  2087. if (ret < 0)
  2088. goto done;
  2089. if (xqry->query != UVC_SET_CUR &&
  2090. copy_to_user(xqry->data, data, size))
  2091. ret = -EFAULT;
  2092. done:
  2093. kfree(data);
  2094. mutex_unlock(&chain->ctrl_mutex);
  2095. return ret;
  2096. }
  2097. /* --------------------------------------------------------------------------
  2098. * Suspend/resume
  2099. */
  2100. /*
  2101. * Restore control values after resume, skipping controls that haven't been
  2102. * changed.
  2103. *
  2104. * TODO
  2105. * - Don't restore modified controls that are back to their default value.
  2106. * - Handle restore order (Auto-Exposure Mode should be restored before
  2107. * Exposure Time).
  2108. */
  2109. int uvc_ctrl_restore_values(struct uvc_device *dev)
  2110. {
  2111. struct uvc_control *ctrl;
  2112. struct uvc_entity *entity;
  2113. unsigned int i;
  2114. int ret;
  2115. /* Walk the entities list and restore controls when possible. */
  2116. list_for_each_entry(entity, &dev->entities, list) {
  2117. for (i = 0; i < entity->ncontrols; ++i) {
  2118. ctrl = &entity->controls[i];
  2119. if (!ctrl->initialized || !ctrl->modified ||
  2120. (ctrl->info.flags & UVC_CTRL_FLAG_RESTORE) == 0)
  2121. continue;
  2122. dev_dbg(&dev->udev->dev,
  2123. "restoring control %pUl/%u/%u\n",
  2124. ctrl->info.entity, ctrl->info.index,
  2125. ctrl->info.selector);
  2126. ctrl->dirty = 1;
  2127. }
  2128. ret = uvc_ctrl_commit_entity(dev, NULL, entity, 0, NULL);
  2129. if (ret < 0)
  2130. return ret;
  2131. }
  2132. return 0;
  2133. }
  2134. /* --------------------------------------------------------------------------
  2135. * Control and mapping handling
  2136. */
  2137. /*
  2138. * Add control information to a given control.
  2139. */
  2140. static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
  2141. const struct uvc_control_info *info)
  2142. {
  2143. ctrl->info = *info;
  2144. INIT_LIST_HEAD(&ctrl->info.mappings);
  2145. /* Allocate an array to save control values (cur, def, max, etc.) */
  2146. ctrl->uvc_data = kzalloc(ctrl->info.size * UVC_CTRL_DATA_LAST + 1,
  2147. GFP_KERNEL);
  2148. if (!ctrl->uvc_data)
  2149. return -ENOMEM;
  2150. ctrl->initialized = 1;
  2151. uvc_dbg(dev, CONTROL, "Added control %pUl/%u to device %s entity %u\n",
  2152. ctrl->info.entity, ctrl->info.selector, dev->udev->devpath,
  2153. ctrl->entity->id);
  2154. return 0;
  2155. }
  2156. /*
  2157. * Add a control mapping to a given control.
  2158. */
  2159. static int __uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
  2160. struct uvc_control *ctrl, const struct uvc_control_mapping *mapping)
  2161. {
  2162. struct uvc_control_mapping *map;
  2163. unsigned int size;
  2164. unsigned int i;
  2165. /*
  2166. * Most mappings come from static kernel data, and need to be duplicated.
  2167. * Mappings that come from userspace will be unnecessarily duplicated,
  2168. * this could be optimized.
  2169. */
  2170. map = kmemdup(mapping, sizeof(*mapping), GFP_KERNEL);
  2171. if (!map)
  2172. return -ENOMEM;
  2173. map->name = NULL;
  2174. map->menu_names = NULL;
  2175. map->menu_mapping = NULL;
  2176. /* For UVCIOC_CTRL_MAP custom control */
  2177. if (mapping->name) {
  2178. map->name = kstrdup(mapping->name, GFP_KERNEL);
  2179. if (!map->name)
  2180. goto err_nomem;
  2181. }
  2182. INIT_LIST_HEAD(&map->ev_subs);
  2183. if (mapping->menu_mapping && mapping->menu_mask) {
  2184. size = sizeof(mapping->menu_mapping[0])
  2185. * fls(mapping->menu_mask);
  2186. map->menu_mapping = kmemdup(mapping->menu_mapping, size,
  2187. GFP_KERNEL);
  2188. if (!map->menu_mapping)
  2189. goto err_nomem;
  2190. }
  2191. if (mapping->menu_names && mapping->menu_mask) {
  2192. size = sizeof(mapping->menu_names[0])
  2193. * fls(mapping->menu_mask);
  2194. map->menu_names = kmemdup(mapping->menu_names, size,
  2195. GFP_KERNEL);
  2196. if (!map->menu_names)
  2197. goto err_nomem;
  2198. }
  2199. if (map->get == NULL)
  2200. map->get = uvc_get_le_value;
  2201. if (map->set == NULL)
  2202. map->set = uvc_set_le_value;
  2203. for (i = 0; i < ARRAY_SIZE(uvc_control_classes); i++) {
  2204. if (V4L2_CTRL_ID2WHICH(uvc_control_classes[i]) ==
  2205. V4L2_CTRL_ID2WHICH(map->id)) {
  2206. chain->ctrl_class_bitmap |= BIT(i);
  2207. break;
  2208. }
  2209. }
  2210. list_add_tail(&map->list, &ctrl->info.mappings);
  2211. uvc_dbg(chain->dev, CONTROL, "Adding mapping '%s' to control %pUl/%u\n",
  2212. uvc_map_get_name(map), ctrl->info.entity,
  2213. ctrl->info.selector);
  2214. return 0;
  2215. err_nomem:
  2216. kfree(map->menu_names);
  2217. kfree(map->menu_mapping);
  2218. kfree(map->name);
  2219. kfree(map);
  2220. return -ENOMEM;
  2221. }
  2222. int uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
  2223. const struct uvc_control_mapping *mapping)
  2224. {
  2225. struct uvc_device *dev = chain->dev;
  2226. struct uvc_control_mapping *map;
  2227. struct uvc_entity *entity;
  2228. struct uvc_control *ctrl;
  2229. int found = 0;
  2230. int ret;
  2231. if (mapping->id & ~V4L2_CTRL_ID_MASK) {
  2232. uvc_dbg(dev, CONTROL,
  2233. "Can't add mapping '%s', control id 0x%08x is invalid\n",
  2234. uvc_map_get_name(mapping), mapping->id);
  2235. return -EINVAL;
  2236. }
  2237. /* Search for the matching (GUID/CS) control on the current chain */
  2238. list_for_each_entry(entity, &chain->entities, chain) {
  2239. unsigned int i;
  2240. if (UVC_ENTITY_TYPE(entity) != UVC_VC_EXTENSION_UNIT ||
  2241. !uvc_entity_match_guid(entity, mapping->entity))
  2242. continue;
  2243. for (i = 0; i < entity->ncontrols; ++i) {
  2244. ctrl = &entity->controls[i];
  2245. if (ctrl->index == mapping->selector - 1) {
  2246. found = 1;
  2247. break;
  2248. }
  2249. }
  2250. if (found)
  2251. break;
  2252. }
  2253. if (!found)
  2254. return -ENOENT;
  2255. if (mutex_lock_interruptible(&chain->ctrl_mutex))
  2256. return -ERESTARTSYS;
  2257. /* Perform delayed initialization of XU controls */
  2258. ret = uvc_ctrl_init_xu_ctrl(dev, ctrl);
  2259. if (ret < 0) {
  2260. ret = -ENOENT;
  2261. goto done;
  2262. }
  2263. /* Validate the user-provided bit-size and offset */
  2264. if (mapping->size > 32 ||
  2265. mapping->offset + mapping->size > ctrl->info.size * 8) {
  2266. ret = -EINVAL;
  2267. goto done;
  2268. }
  2269. list_for_each_entry(map, &ctrl->info.mappings, list) {
  2270. if (mapping->id == map->id) {
  2271. uvc_dbg(dev, CONTROL,
  2272. "Can't add mapping '%s', control id 0x%08x already exists\n",
  2273. uvc_map_get_name(mapping), mapping->id);
  2274. ret = -EEXIST;
  2275. goto done;
  2276. }
  2277. }
  2278. /* Prevent excess memory consumption */
  2279. if (atomic_inc_return(&dev->nmappings) > UVC_MAX_CONTROL_MAPPINGS) {
  2280. atomic_dec(&dev->nmappings);
  2281. uvc_dbg(dev, CONTROL,
  2282. "Can't add mapping '%s', maximum mappings count (%u) exceeded\n",
  2283. uvc_map_get_name(mapping), UVC_MAX_CONTROL_MAPPINGS);
  2284. ret = -ENOMEM;
  2285. goto done;
  2286. }
  2287. ret = __uvc_ctrl_add_mapping(chain, ctrl, mapping);
  2288. if (ret < 0)
  2289. atomic_dec(&dev->nmappings);
  2290. done:
  2291. mutex_unlock(&chain->ctrl_mutex);
  2292. return ret;
  2293. }
  2294. /*
  2295. * Prune an entity of its bogus controls using a blacklist. Bogus controls
  2296. * are currently the ones that crash the camera or unconditionally return an
  2297. * error when queried.
  2298. */
  2299. static void uvc_ctrl_prune_entity(struct uvc_device *dev,
  2300. struct uvc_entity *entity)
  2301. {
  2302. struct uvc_ctrl_blacklist {
  2303. struct usb_device_id id;
  2304. u8 index;
  2305. };
  2306. static const struct uvc_ctrl_blacklist processing_blacklist[] = {
  2307. { { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */
  2308. { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
  2309. { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
  2310. };
  2311. static const struct uvc_ctrl_blacklist camera_blacklist[] = {
  2312. { { USB_DEVICE(0x06f8, 0x3005) }, 9 }, /* Zoom, Absolute */
  2313. };
  2314. const struct uvc_ctrl_blacklist *blacklist;
  2315. unsigned int size;
  2316. unsigned int count;
  2317. unsigned int i;
  2318. u8 *controls;
  2319. switch (UVC_ENTITY_TYPE(entity)) {
  2320. case UVC_VC_PROCESSING_UNIT:
  2321. blacklist = processing_blacklist;
  2322. count = ARRAY_SIZE(processing_blacklist);
  2323. controls = entity->processing.bmControls;
  2324. size = entity->processing.bControlSize;
  2325. break;
  2326. case UVC_ITT_CAMERA:
  2327. blacklist = camera_blacklist;
  2328. count = ARRAY_SIZE(camera_blacklist);
  2329. controls = entity->camera.bmControls;
  2330. size = entity->camera.bControlSize;
  2331. break;
  2332. default:
  2333. return;
  2334. }
  2335. for (i = 0; i < count; ++i) {
  2336. if (!usb_match_one_id(dev->intf, &blacklist[i].id))
  2337. continue;
  2338. if (blacklist[i].index >= 8 * size ||
  2339. !uvc_test_bit(controls, blacklist[i].index))
  2340. continue;
  2341. uvc_dbg(dev, CONTROL,
  2342. "%u/%u control is black listed, removing it\n",
  2343. entity->id, blacklist[i].index);
  2344. uvc_clear_bit(controls, blacklist[i].index);
  2345. }
  2346. }
  2347. /*
  2348. * Add control information and hardcoded stock control mappings to the given
  2349. * device.
  2350. */
  2351. static void uvc_ctrl_init_ctrl(struct uvc_video_chain *chain,
  2352. struct uvc_control *ctrl)
  2353. {
  2354. unsigned int i;
  2355. /*
  2356. * XU controls initialization requires querying the device for control
  2357. * information. As some buggy UVC devices will crash when queried
  2358. * repeatedly in a tight loop, delay XU controls initialization until
  2359. * first use.
  2360. */
  2361. if (UVC_ENTITY_TYPE(ctrl->entity) == UVC_VC_EXTENSION_UNIT)
  2362. return;
  2363. for (i = 0; i < ARRAY_SIZE(uvc_ctrls); ++i) {
  2364. const struct uvc_control_info *info = &uvc_ctrls[i];
  2365. if (uvc_entity_match_guid(ctrl->entity, info->entity) &&
  2366. ctrl->index == info->index) {
  2367. uvc_ctrl_add_info(chain->dev, ctrl, info);
  2368. /*
  2369. * Retrieve control flags from the device. Ignore errors
  2370. * and work with default flag values from the uvc_ctrl
  2371. * array when the device doesn't properly implement
  2372. * GET_INFO on standard controls.
  2373. */
  2374. uvc_ctrl_get_flags(chain->dev, ctrl, &ctrl->info);
  2375. break;
  2376. }
  2377. }
  2378. if (!ctrl->initialized)
  2379. return;
  2380. /* Process common mappings. */
  2381. for (i = 0; i < ARRAY_SIZE(uvc_ctrl_mappings); ++i) {
  2382. const struct uvc_control_mapping *mapping = &uvc_ctrl_mappings[i];
  2383. if (!uvc_entity_match_guid(ctrl->entity, mapping->entity) ||
  2384. ctrl->info.selector != mapping->selector)
  2385. continue;
  2386. /* Let the device provide a custom mapping. */
  2387. if (mapping->filter_mapping) {
  2388. mapping = mapping->filter_mapping(chain, ctrl);
  2389. if (!mapping)
  2390. continue;
  2391. }
  2392. __uvc_ctrl_add_mapping(chain, ctrl, mapping);
  2393. }
  2394. }
  2395. /*
  2396. * Initialize device controls.
  2397. */
  2398. static int uvc_ctrl_init_chain(struct uvc_video_chain *chain)
  2399. {
  2400. struct uvc_entity *entity;
  2401. unsigned int i;
  2402. /* Walk the entities list and instantiate controls */
  2403. list_for_each_entry(entity, &chain->entities, chain) {
  2404. struct uvc_control *ctrl;
  2405. unsigned int bControlSize = 0, ncontrols;
  2406. u8 *bmControls = NULL;
  2407. if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
  2408. bmControls = entity->extension.bmControls;
  2409. bControlSize = entity->extension.bControlSize;
  2410. } else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) {
  2411. bmControls = entity->processing.bmControls;
  2412. bControlSize = entity->processing.bControlSize;
  2413. } else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
  2414. bmControls = entity->camera.bmControls;
  2415. bControlSize = entity->camera.bControlSize;
  2416. } else if (UVC_ENTITY_TYPE(entity) == UVC_EXT_GPIO_UNIT) {
  2417. bmControls = entity->gpio.bmControls;
  2418. bControlSize = entity->gpio.bControlSize;
  2419. }
  2420. /* Remove bogus/blacklisted controls */
  2421. uvc_ctrl_prune_entity(chain->dev, entity);
  2422. /* Count supported controls and allocate the controls array */
  2423. ncontrols = memweight(bmControls, bControlSize);
  2424. if (ncontrols == 0)
  2425. continue;
  2426. entity->controls = kcalloc(ncontrols, sizeof(*ctrl),
  2427. GFP_KERNEL);
  2428. if (entity->controls == NULL)
  2429. return -ENOMEM;
  2430. entity->ncontrols = ncontrols;
  2431. /* Initialize all supported controls */
  2432. ctrl = entity->controls;
  2433. for (i = 0; i < bControlSize * 8; ++i) {
  2434. if (uvc_test_bit(bmControls, i) == 0)
  2435. continue;
  2436. ctrl->entity = entity;
  2437. ctrl->index = i;
  2438. uvc_ctrl_init_ctrl(chain, ctrl);
  2439. ctrl++;
  2440. }
  2441. }
  2442. return 0;
  2443. }
  2444. int uvc_ctrl_init_device(struct uvc_device *dev)
  2445. {
  2446. struct uvc_video_chain *chain;
  2447. int ret;
  2448. INIT_WORK(&dev->async_ctrl.work, uvc_ctrl_status_event_work);
  2449. list_for_each_entry(chain, &dev->chains, list) {
  2450. ret = uvc_ctrl_init_chain(chain);
  2451. if (ret)
  2452. return ret;
  2453. }
  2454. return 0;
  2455. }
  2456. void uvc_ctrl_cleanup_fh(struct uvc_fh *handle)
  2457. {
  2458. struct uvc_entity *entity;
  2459. guard(mutex)(&handle->chain->ctrl_mutex);
  2460. if (!handle->pending_async_ctrls)
  2461. return;
  2462. list_for_each_entry(entity, &handle->chain->dev->entities, list) {
  2463. for (unsigned int i = 0; i < entity->ncontrols; ++i) {
  2464. if (entity->controls[i].handle != handle)
  2465. continue;
  2466. uvc_ctrl_set_handle(handle, &entity->controls[i], NULL);
  2467. }
  2468. }
  2469. WARN_ON(handle->pending_async_ctrls);
  2470. }
  2471. /*
  2472. * Cleanup device controls.
  2473. */
  2474. static void uvc_ctrl_cleanup_mappings(struct uvc_device *dev,
  2475. struct uvc_control *ctrl)
  2476. {
  2477. struct uvc_control_mapping *mapping, *nm;
  2478. list_for_each_entry_safe(mapping, nm, &ctrl->info.mappings, list) {
  2479. list_del(&mapping->list);
  2480. kfree(mapping->menu_names);
  2481. kfree(mapping->menu_mapping);
  2482. kfree(mapping->name);
  2483. kfree(mapping);
  2484. }
  2485. }
  2486. void uvc_ctrl_cleanup_device(struct uvc_device *dev)
  2487. {
  2488. struct uvc_entity *entity;
  2489. unsigned int i;
  2490. /* Can be uninitialized if we are aborting on probe error. */
  2491. if (dev->async_ctrl.work.func)
  2492. cancel_work_sync(&dev->async_ctrl.work);
  2493. /* Free controls and control mappings for all entities. */
  2494. list_for_each_entry(entity, &dev->entities, list) {
  2495. for (i = 0; i < entity->ncontrols; ++i) {
  2496. struct uvc_control *ctrl = &entity->controls[i];
  2497. if (!ctrl->initialized)
  2498. continue;
  2499. uvc_ctrl_cleanup_mappings(dev, ctrl);
  2500. kfree(ctrl->uvc_data);
  2501. }
  2502. kfree(entity->controls);
  2503. }
  2504. }