uvc_ctrl.c 57 KB

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