uvc_ctrl.c 40 KB

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