uvc_ctrl.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257
  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. /*
  1439. * Retrieve flags for a given control
  1440. */
  1441. static int uvc_ctrl_get_flags(struct uvc_device *dev,
  1442. const struct uvc_control *ctrl,
  1443. struct uvc_control_info *info)
  1444. {
  1445. u8 *data;
  1446. int ret;
  1447. data = kmalloc(1, GFP_KERNEL);
  1448. if (data == NULL)
  1449. return -ENOMEM;
  1450. ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id, dev->intfnum,
  1451. info->selector, data, 1);
  1452. if (!ret)
  1453. info->flags = UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX
  1454. | UVC_CTRL_FLAG_GET_RES | UVC_CTRL_FLAG_GET_DEF
  1455. | (data[0] & UVC_CONTROL_CAP_GET ?
  1456. UVC_CTRL_FLAG_GET_CUR : 0)
  1457. | (data[0] & UVC_CONTROL_CAP_SET ?
  1458. UVC_CTRL_FLAG_SET_CUR : 0)
  1459. | (data[0] & UVC_CONTROL_CAP_AUTOUPDATE ?
  1460. UVC_CTRL_FLAG_AUTO_UPDATE : 0);
  1461. kfree(data);
  1462. return ret;
  1463. }
  1464. static void uvc_ctrl_fixup_xu_info(struct uvc_device *dev,
  1465. const struct uvc_control *ctrl, struct uvc_control_info *info)
  1466. {
  1467. struct uvc_ctrl_fixup {
  1468. struct usb_device_id id;
  1469. u8 entity;
  1470. u8 selector;
  1471. u8 flags;
  1472. };
  1473. static const struct uvc_ctrl_fixup fixups[] = {
  1474. { { USB_DEVICE(0x046d, 0x08c2) }, 9, 1,
  1475. UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
  1476. UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
  1477. UVC_CTRL_FLAG_AUTO_UPDATE },
  1478. { { USB_DEVICE(0x046d, 0x08cc) }, 9, 1,
  1479. UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
  1480. UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
  1481. UVC_CTRL_FLAG_AUTO_UPDATE },
  1482. { { USB_DEVICE(0x046d, 0x0994) }, 9, 1,
  1483. UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
  1484. UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
  1485. UVC_CTRL_FLAG_AUTO_UPDATE },
  1486. };
  1487. unsigned int i;
  1488. for (i = 0; i < ARRAY_SIZE(fixups); ++i) {
  1489. if (!usb_match_one_id(dev->intf, &fixups[i].id))
  1490. continue;
  1491. if (fixups[i].entity == ctrl->entity->id &&
  1492. fixups[i].selector == info->selector) {
  1493. info->flags = fixups[i].flags;
  1494. return;
  1495. }
  1496. }
  1497. }
  1498. /*
  1499. * Query control information (size and flags) for XU controls.
  1500. */
  1501. static int uvc_ctrl_fill_xu_info(struct uvc_device *dev,
  1502. const struct uvc_control *ctrl, struct uvc_control_info *info)
  1503. {
  1504. u8 *data;
  1505. int ret;
  1506. data = kmalloc(2, GFP_KERNEL);
  1507. if (data == NULL)
  1508. return -ENOMEM;
  1509. memcpy(info->entity, ctrl->entity->extension.guidExtensionCode,
  1510. sizeof(info->entity));
  1511. info->index = ctrl->index;
  1512. info->selector = ctrl->index + 1;
  1513. /* Query and verify the control length (GET_LEN) */
  1514. ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id, dev->intfnum,
  1515. info->selector, data, 2);
  1516. if (ret < 0) {
  1517. uvc_trace(UVC_TRACE_CONTROL,
  1518. "GET_LEN failed on control %pUl/%u (%d).\n",
  1519. info->entity, info->selector, ret);
  1520. goto done;
  1521. }
  1522. info->size = le16_to_cpup((__le16 *)data);
  1523. ret = uvc_ctrl_get_flags(dev, ctrl, info);
  1524. if (ret < 0) {
  1525. uvc_trace(UVC_TRACE_CONTROL,
  1526. "Failed to get flags for control %pUl/%u (%d).\n",
  1527. info->entity, info->selector, ret);
  1528. goto done;
  1529. }
  1530. uvc_ctrl_fixup_xu_info(dev, ctrl, info);
  1531. uvc_trace(UVC_TRACE_CONTROL, "XU control %pUl/%u queried: len %u, "
  1532. "flags { get %u set %u auto %u }.\n",
  1533. info->entity, info->selector, info->size,
  1534. (info->flags & UVC_CTRL_FLAG_GET_CUR) ? 1 : 0,
  1535. (info->flags & UVC_CTRL_FLAG_SET_CUR) ? 1 : 0,
  1536. (info->flags & UVC_CTRL_FLAG_AUTO_UPDATE) ? 1 : 0);
  1537. done:
  1538. kfree(data);
  1539. return ret;
  1540. }
  1541. static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
  1542. const struct uvc_control_info *info);
  1543. static int uvc_ctrl_init_xu_ctrl(struct uvc_device *dev,
  1544. struct uvc_control *ctrl)
  1545. {
  1546. struct uvc_control_info info;
  1547. int ret;
  1548. if (ctrl->initialized)
  1549. return 0;
  1550. ret = uvc_ctrl_fill_xu_info(dev, ctrl, &info);
  1551. if (ret < 0)
  1552. return ret;
  1553. ret = uvc_ctrl_add_info(dev, ctrl, &info);
  1554. if (ret < 0)
  1555. uvc_trace(UVC_TRACE_CONTROL, "Failed to initialize control "
  1556. "%pUl/%u on device %s entity %u\n", info.entity,
  1557. info.selector, dev->udev->devpath, ctrl->entity->id);
  1558. return ret;
  1559. }
  1560. int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
  1561. struct uvc_xu_control_query *xqry)
  1562. {
  1563. struct uvc_entity *entity;
  1564. struct uvc_control *ctrl;
  1565. unsigned int i, found = 0;
  1566. u32 reqflags;
  1567. u16 size;
  1568. u8 *data = NULL;
  1569. int ret;
  1570. /* Find the extension unit. */
  1571. list_for_each_entry(entity, &chain->entities, chain) {
  1572. if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT &&
  1573. entity->id == xqry->unit)
  1574. break;
  1575. }
  1576. if (entity->id != xqry->unit) {
  1577. uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
  1578. xqry->unit);
  1579. return -ENOENT;
  1580. }
  1581. /* Find the control and perform delayed initialization if needed. */
  1582. for (i = 0; i < entity->ncontrols; ++i) {
  1583. ctrl = &entity->controls[i];
  1584. if (ctrl->index == xqry->selector - 1) {
  1585. found = 1;
  1586. break;
  1587. }
  1588. }
  1589. if (!found) {
  1590. uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u not found.\n",
  1591. entity->extension.guidExtensionCode, xqry->selector);
  1592. return -ENOENT;
  1593. }
  1594. if (mutex_lock_interruptible(&chain->ctrl_mutex))
  1595. return -ERESTARTSYS;
  1596. ret = uvc_ctrl_init_xu_ctrl(chain->dev, ctrl);
  1597. if (ret < 0) {
  1598. ret = -ENOENT;
  1599. goto done;
  1600. }
  1601. /* Validate the required buffer size and flags for the request */
  1602. reqflags = 0;
  1603. size = ctrl->info.size;
  1604. switch (xqry->query) {
  1605. case UVC_GET_CUR:
  1606. reqflags = UVC_CTRL_FLAG_GET_CUR;
  1607. break;
  1608. case UVC_GET_MIN:
  1609. reqflags = UVC_CTRL_FLAG_GET_MIN;
  1610. break;
  1611. case UVC_GET_MAX:
  1612. reqflags = UVC_CTRL_FLAG_GET_MAX;
  1613. break;
  1614. case UVC_GET_DEF:
  1615. reqflags = UVC_CTRL_FLAG_GET_DEF;
  1616. break;
  1617. case UVC_GET_RES:
  1618. reqflags = UVC_CTRL_FLAG_GET_RES;
  1619. break;
  1620. case UVC_SET_CUR:
  1621. reqflags = UVC_CTRL_FLAG_SET_CUR;
  1622. break;
  1623. case UVC_GET_LEN:
  1624. size = 2;
  1625. break;
  1626. case UVC_GET_INFO:
  1627. size = 1;
  1628. break;
  1629. default:
  1630. ret = -EINVAL;
  1631. goto done;
  1632. }
  1633. if (size != xqry->size) {
  1634. ret = -ENOBUFS;
  1635. goto done;
  1636. }
  1637. if (reqflags && !(ctrl->info.flags & reqflags)) {
  1638. ret = -EBADRQC;
  1639. goto done;
  1640. }
  1641. data = kmalloc(size, GFP_KERNEL);
  1642. if (data == NULL) {
  1643. ret = -ENOMEM;
  1644. goto done;
  1645. }
  1646. if (xqry->query == UVC_SET_CUR &&
  1647. copy_from_user(data, xqry->data, size)) {
  1648. ret = -EFAULT;
  1649. goto done;
  1650. }
  1651. ret = uvc_query_ctrl(chain->dev, xqry->query, xqry->unit,
  1652. chain->dev->intfnum, xqry->selector, data, size);
  1653. if (ret < 0)
  1654. goto done;
  1655. if (xqry->query != UVC_SET_CUR &&
  1656. copy_to_user(xqry->data, data, size))
  1657. ret = -EFAULT;
  1658. done:
  1659. kfree(data);
  1660. mutex_unlock(&chain->ctrl_mutex);
  1661. return ret;
  1662. }
  1663. /* --------------------------------------------------------------------------
  1664. * Suspend/resume
  1665. */
  1666. /*
  1667. * Restore control values after resume, skipping controls that haven't been
  1668. * changed.
  1669. *
  1670. * TODO
  1671. * - Don't restore modified controls that are back to their default value.
  1672. * - Handle restore order (Auto-Exposure Mode should be restored before
  1673. * Exposure Time).
  1674. */
  1675. int uvc_ctrl_restore_values(struct uvc_device *dev)
  1676. {
  1677. struct uvc_control *ctrl;
  1678. struct uvc_entity *entity;
  1679. unsigned int i;
  1680. int ret;
  1681. /* Walk the entities list and restore controls when possible. */
  1682. list_for_each_entry(entity, &dev->entities, list) {
  1683. for (i = 0; i < entity->ncontrols; ++i) {
  1684. ctrl = &entity->controls[i];
  1685. if (!ctrl->initialized || !ctrl->modified ||
  1686. (ctrl->info.flags & UVC_CTRL_FLAG_RESTORE) == 0)
  1687. continue;
  1688. printk(KERN_INFO "restoring control %pUl/%u/%u\n",
  1689. ctrl->info.entity, ctrl->info.index,
  1690. ctrl->info.selector);
  1691. ctrl->dirty = 1;
  1692. }
  1693. ret = uvc_ctrl_commit_entity(dev, entity, 0);
  1694. if (ret < 0)
  1695. return ret;
  1696. }
  1697. return 0;
  1698. }
  1699. /* --------------------------------------------------------------------------
  1700. * Control and mapping handling
  1701. */
  1702. /*
  1703. * Add control information to a given control.
  1704. */
  1705. static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
  1706. const struct uvc_control_info *info)
  1707. {
  1708. int ret = 0;
  1709. ctrl->info = *info;
  1710. INIT_LIST_HEAD(&ctrl->info.mappings);
  1711. /* Allocate an array to save control values (cur, def, max, etc.) */
  1712. ctrl->uvc_data = kzalloc(ctrl->info.size * UVC_CTRL_DATA_LAST + 1,
  1713. GFP_KERNEL);
  1714. if (ctrl->uvc_data == NULL) {
  1715. ret = -ENOMEM;
  1716. goto done;
  1717. }
  1718. /*
  1719. * Retrieve control flags from the device. Ignore errors and work with
  1720. * default flag values from the uvc_ctrl array when the device doesn't
  1721. * properly implement GET_INFO on standard controls.
  1722. */
  1723. uvc_ctrl_get_flags(dev, ctrl, &ctrl->info);
  1724. ctrl->initialized = 1;
  1725. uvc_trace(UVC_TRACE_CONTROL, "Added control %pUl/%u to device %s "
  1726. "entity %u\n", ctrl->info.entity, ctrl->info.selector,
  1727. dev->udev->devpath, ctrl->entity->id);
  1728. done:
  1729. if (ret < 0)
  1730. kfree(ctrl->uvc_data);
  1731. return ret;
  1732. }
  1733. /*
  1734. * Add a control mapping to a given control.
  1735. */
  1736. static int __uvc_ctrl_add_mapping(struct uvc_device *dev,
  1737. struct uvc_control *ctrl, const struct uvc_control_mapping *mapping)
  1738. {
  1739. struct uvc_control_mapping *map;
  1740. unsigned int size;
  1741. /* Most mappings come from static kernel data and need to be duplicated.
  1742. * Mappings that come from userspace will be unnecessarily duplicated,
  1743. * this could be optimized.
  1744. */
  1745. map = kmemdup(mapping, sizeof(*mapping), GFP_KERNEL);
  1746. if (map == NULL)
  1747. return -ENOMEM;
  1748. INIT_LIST_HEAD(&map->ev_subs);
  1749. size = sizeof(*mapping->menu_info) * mapping->menu_count;
  1750. map->menu_info = kmemdup(mapping->menu_info, size, GFP_KERNEL);
  1751. if (map->menu_info == NULL) {
  1752. kfree(map);
  1753. return -ENOMEM;
  1754. }
  1755. if (map->get == NULL)
  1756. map->get = uvc_get_le_value;
  1757. if (map->set == NULL)
  1758. map->set = uvc_set_le_value;
  1759. list_add_tail(&map->list, &ctrl->info.mappings);
  1760. uvc_trace(UVC_TRACE_CONTROL,
  1761. "Adding mapping '%s' to control %pUl/%u.\n",
  1762. map->name, ctrl->info.entity, ctrl->info.selector);
  1763. return 0;
  1764. }
  1765. int uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
  1766. const struct uvc_control_mapping *mapping)
  1767. {
  1768. struct uvc_device *dev = chain->dev;
  1769. struct uvc_control_mapping *map;
  1770. struct uvc_entity *entity;
  1771. struct uvc_control *ctrl;
  1772. int found = 0;
  1773. int ret;
  1774. if (mapping->id & ~V4L2_CTRL_ID_MASK) {
  1775. uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', control "
  1776. "id 0x%08x is invalid.\n", mapping->name,
  1777. mapping->id);
  1778. return -EINVAL;
  1779. }
  1780. /* Search for the matching (GUID/CS) control on the current chain */
  1781. list_for_each_entry(entity, &chain->entities, chain) {
  1782. unsigned int i;
  1783. if (UVC_ENTITY_TYPE(entity) != UVC_VC_EXTENSION_UNIT ||
  1784. !uvc_entity_match_guid(entity, mapping->entity))
  1785. continue;
  1786. for (i = 0; i < entity->ncontrols; ++i) {
  1787. ctrl = &entity->controls[i];
  1788. if (ctrl->index == mapping->selector - 1) {
  1789. found = 1;
  1790. break;
  1791. }
  1792. }
  1793. if (found)
  1794. break;
  1795. }
  1796. if (!found)
  1797. return -ENOENT;
  1798. if (mutex_lock_interruptible(&chain->ctrl_mutex))
  1799. return -ERESTARTSYS;
  1800. /* Perform delayed initialization of XU controls */
  1801. ret = uvc_ctrl_init_xu_ctrl(dev, ctrl);
  1802. if (ret < 0) {
  1803. ret = -ENOENT;
  1804. goto done;
  1805. }
  1806. /* Validate the user-provided bit-size and offset */
  1807. if (mapping->size > 32 ||
  1808. mapping->offset + mapping->size > ctrl->info.size * 8) {
  1809. ret = -EINVAL;
  1810. goto done;
  1811. }
  1812. list_for_each_entry(map, &ctrl->info.mappings, list) {
  1813. if (mapping->id == map->id) {
  1814. uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', "
  1815. "control id 0x%08x already exists.\n",
  1816. mapping->name, mapping->id);
  1817. ret = -EEXIST;
  1818. goto done;
  1819. }
  1820. }
  1821. /* Prevent excess memory consumption */
  1822. if (atomic_inc_return(&dev->nmappings) > UVC_MAX_CONTROL_MAPPINGS) {
  1823. atomic_dec(&dev->nmappings);
  1824. uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', maximum "
  1825. "mappings count (%u) exceeded.\n", mapping->name,
  1826. UVC_MAX_CONTROL_MAPPINGS);
  1827. ret = -ENOMEM;
  1828. goto done;
  1829. }
  1830. ret = __uvc_ctrl_add_mapping(dev, ctrl, mapping);
  1831. if (ret < 0)
  1832. atomic_dec(&dev->nmappings);
  1833. done:
  1834. mutex_unlock(&chain->ctrl_mutex);
  1835. return ret;
  1836. }
  1837. /*
  1838. * Prune an entity of its bogus controls using a blacklist. Bogus controls
  1839. * are currently the ones that crash the camera or unconditionally return an
  1840. * error when queried.
  1841. */
  1842. static void uvc_ctrl_prune_entity(struct uvc_device *dev,
  1843. struct uvc_entity *entity)
  1844. {
  1845. struct uvc_ctrl_blacklist {
  1846. struct usb_device_id id;
  1847. u8 index;
  1848. };
  1849. static const struct uvc_ctrl_blacklist processing_blacklist[] = {
  1850. { { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */
  1851. { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
  1852. { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
  1853. };
  1854. static const struct uvc_ctrl_blacklist camera_blacklist[] = {
  1855. { { USB_DEVICE(0x06f8, 0x3005) }, 9 }, /* Zoom, Absolute */
  1856. };
  1857. const struct uvc_ctrl_blacklist *blacklist;
  1858. unsigned int size;
  1859. unsigned int count;
  1860. unsigned int i;
  1861. u8 *controls;
  1862. switch (UVC_ENTITY_TYPE(entity)) {
  1863. case UVC_VC_PROCESSING_UNIT:
  1864. blacklist = processing_blacklist;
  1865. count = ARRAY_SIZE(processing_blacklist);
  1866. controls = entity->processing.bmControls;
  1867. size = entity->processing.bControlSize;
  1868. break;
  1869. case UVC_ITT_CAMERA:
  1870. blacklist = camera_blacklist;
  1871. count = ARRAY_SIZE(camera_blacklist);
  1872. controls = entity->camera.bmControls;
  1873. size = entity->camera.bControlSize;
  1874. break;
  1875. default:
  1876. return;
  1877. }
  1878. for (i = 0; i < count; ++i) {
  1879. if (!usb_match_one_id(dev->intf, &blacklist[i].id))
  1880. continue;
  1881. if (blacklist[i].index >= 8 * size ||
  1882. !uvc_test_bit(controls, blacklist[i].index))
  1883. continue;
  1884. uvc_trace(UVC_TRACE_CONTROL, "%u/%u control is black listed, "
  1885. "removing it.\n", entity->id, blacklist[i].index);
  1886. uvc_clear_bit(controls, blacklist[i].index);
  1887. }
  1888. }
  1889. /*
  1890. * Add control information and hardcoded stock control mappings to the given
  1891. * device.
  1892. */
  1893. static void uvc_ctrl_init_ctrl(struct uvc_device *dev, struct uvc_control *ctrl)
  1894. {
  1895. const struct uvc_control_info *info = uvc_ctrls;
  1896. const struct uvc_control_info *iend = info + ARRAY_SIZE(uvc_ctrls);
  1897. const struct uvc_control_mapping *mapping = uvc_ctrl_mappings;
  1898. const struct uvc_control_mapping *mend =
  1899. mapping + ARRAY_SIZE(uvc_ctrl_mappings);
  1900. /* XU controls initialization requires querying the device for control
  1901. * information. As some buggy UVC devices will crash when queried
  1902. * repeatedly in a tight loop, delay XU controls initialization until
  1903. * first use.
  1904. */
  1905. if (UVC_ENTITY_TYPE(ctrl->entity) == UVC_VC_EXTENSION_UNIT)
  1906. return;
  1907. for (; info < iend; ++info) {
  1908. if (uvc_entity_match_guid(ctrl->entity, info->entity) &&
  1909. ctrl->index == info->index) {
  1910. uvc_ctrl_add_info(dev, ctrl, info);
  1911. break;
  1912. }
  1913. }
  1914. if (!ctrl->initialized)
  1915. return;
  1916. for (; mapping < mend; ++mapping) {
  1917. if (uvc_entity_match_guid(ctrl->entity, mapping->entity) &&
  1918. ctrl->info.selector == mapping->selector)
  1919. __uvc_ctrl_add_mapping(dev, ctrl, mapping);
  1920. }
  1921. }
  1922. /*
  1923. * Initialize device controls.
  1924. */
  1925. int uvc_ctrl_init_device(struct uvc_device *dev)
  1926. {
  1927. struct uvc_entity *entity;
  1928. unsigned int i;
  1929. /* Walk the entities list and instantiate controls */
  1930. list_for_each_entry(entity, &dev->entities, list) {
  1931. struct uvc_control *ctrl;
  1932. unsigned int bControlSize = 0, ncontrols;
  1933. u8 *bmControls = NULL;
  1934. if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
  1935. bmControls = entity->extension.bmControls;
  1936. bControlSize = entity->extension.bControlSize;
  1937. } else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) {
  1938. bmControls = entity->processing.bmControls;
  1939. bControlSize = entity->processing.bControlSize;
  1940. } else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
  1941. bmControls = entity->camera.bmControls;
  1942. bControlSize = entity->camera.bControlSize;
  1943. }
  1944. /* Remove bogus/blacklisted controls */
  1945. uvc_ctrl_prune_entity(dev, entity);
  1946. /* Count supported controls and allocate the controls array */
  1947. ncontrols = memweight(bmControls, bControlSize);
  1948. if (ncontrols == 0)
  1949. continue;
  1950. entity->controls = kcalloc(ncontrols, sizeof(*ctrl),
  1951. GFP_KERNEL);
  1952. if (entity->controls == NULL)
  1953. return -ENOMEM;
  1954. entity->ncontrols = ncontrols;
  1955. /* Initialize all supported controls */
  1956. ctrl = entity->controls;
  1957. for (i = 0; i < bControlSize * 8; ++i) {
  1958. if (uvc_test_bit(bmControls, i) == 0)
  1959. continue;
  1960. ctrl->entity = entity;
  1961. ctrl->index = i;
  1962. uvc_ctrl_init_ctrl(dev, ctrl);
  1963. ctrl++;
  1964. }
  1965. }
  1966. return 0;
  1967. }
  1968. /*
  1969. * Cleanup device controls.
  1970. */
  1971. static void uvc_ctrl_cleanup_mappings(struct uvc_device *dev,
  1972. struct uvc_control *ctrl)
  1973. {
  1974. struct uvc_control_mapping *mapping, *nm;
  1975. list_for_each_entry_safe(mapping, nm, &ctrl->info.mappings, list) {
  1976. list_del(&mapping->list);
  1977. kfree(mapping->menu_info);
  1978. kfree(mapping);
  1979. }
  1980. }
  1981. void uvc_ctrl_cleanup_device(struct uvc_device *dev)
  1982. {
  1983. struct uvc_entity *entity;
  1984. unsigned int i;
  1985. /* Free controls and control mappings for all entities. */
  1986. list_for_each_entry(entity, &dev->entities, list) {
  1987. for (i = 0; i < entity->ncontrols; ++i) {
  1988. struct uvc_control *ctrl = &entity->controls[i];
  1989. if (!ctrl->initialized)
  1990. continue;
  1991. uvc_ctrl_cleanup_mappings(dev, ctrl);
  1992. kfree(ctrl->uvc_data);
  1993. }
  1994. kfree(entity->controls);
  1995. }
  1996. }