uvc_ctrl.c 52 KB

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