uvc_ctrl.c 60 KB

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