uvc_ctrl.c 56 KB

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