uvc_ctrl.c 39 KB

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