uvc_ctrl.c 36 KB

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