uvc_ctrl.c 45 KB

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