uvc_ctrl.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522
  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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 GET_CUR:
  327. return (zoom == 0) ? 0 : (zoom > 0 ? data[2] : -data[2]);
  328. case GET_MIN:
  329. case GET_MAX:
  330. case GET_RES:
  331. case 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(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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 ITT_CAMERA:
  652. return memcmp(uvc_camera_guid, guid, 16) == 0;
  653. case ITT_MEDIA_TRANSPORT_INPUT:
  654. return memcmp(uvc_media_transport_input_guid, guid, 16) == 0;
  655. case VC_PROCESSING_UNIT:
  656. return memcmp(uvc_processing_guid, guid, 16) == 0;
  657. case 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_device *video,
  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(video->processing, v4l2_id, mapping, &ctrl, next);
  705. if (ctrl && !next)
  706. return ctrl;
  707. list_for_each_entry(entity, &video->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, &video->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_device *video,
  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(video, 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. if ((ret = uvc_query_ctrl(video->dev, GET_DEF, ctrl->entity->id,
  746. video->dev->intfnum, ctrl->info->selector,
  747. data, ctrl->info->size)) < 0)
  748. goto out;
  749. v4l2_ctrl->default_value = mapping->get(mapping, GET_DEF, data);
  750. }
  751. switch (mapping->v4l2_type) {
  752. case V4L2_CTRL_TYPE_MENU:
  753. v4l2_ctrl->minimum = 0;
  754. v4l2_ctrl->maximum = mapping->menu_count - 1;
  755. v4l2_ctrl->step = 1;
  756. menu = mapping->menu_info;
  757. for (i = 0; i < mapping->menu_count; ++i, ++menu) {
  758. if (menu->value == v4l2_ctrl->default_value) {
  759. v4l2_ctrl->default_value = i;
  760. break;
  761. }
  762. }
  763. ret = 0;
  764. goto out;
  765. case V4L2_CTRL_TYPE_BOOLEAN:
  766. v4l2_ctrl->minimum = 0;
  767. v4l2_ctrl->maximum = 1;
  768. v4l2_ctrl->step = 1;
  769. ret = 0;
  770. goto out;
  771. default:
  772. break;
  773. }
  774. if (ctrl->info->flags & UVC_CONTROL_GET_MIN) {
  775. if ((ret = uvc_query_ctrl(video->dev, GET_MIN, ctrl->entity->id,
  776. video->dev->intfnum, ctrl->info->selector,
  777. data, ctrl->info->size)) < 0)
  778. goto out;
  779. v4l2_ctrl->minimum = mapping->get(mapping, GET_MIN, data);
  780. }
  781. if (ctrl->info->flags & UVC_CONTROL_GET_MAX) {
  782. if ((ret = uvc_query_ctrl(video->dev, GET_MAX, ctrl->entity->id,
  783. video->dev->intfnum, ctrl->info->selector,
  784. data, ctrl->info->size)) < 0)
  785. goto out;
  786. v4l2_ctrl->maximum = mapping->get(mapping, GET_MAX, data);
  787. }
  788. if (ctrl->info->flags & UVC_CONTROL_GET_RES) {
  789. if ((ret = uvc_query_ctrl(video->dev, GET_RES, ctrl->entity->id,
  790. video->dev->intfnum, ctrl->info->selector,
  791. data, ctrl->info->size)) < 0)
  792. goto out;
  793. v4l2_ctrl->step = mapping->get(mapping, GET_RES, data);
  794. }
  795. ret = 0;
  796. out:
  797. kfree(data);
  798. return ret;
  799. }
  800. /* --------------------------------------------------------------------------
  801. * Control transactions
  802. *
  803. * To make extended set operations as atomic as the hardware allows, controls
  804. * are handled using begin/commit/rollback operations.
  805. *
  806. * At the beginning of a set request, uvc_ctrl_begin should be called to
  807. * initialize the request. This function acquires the control lock.
  808. *
  809. * When setting a control, the new value is stored in the control data field
  810. * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
  811. * later processing. If the UVC and V4L2 control sizes differ, the current
  812. * value is loaded from the hardware before storing the new value in the data
  813. * field.
  814. *
  815. * After processing all controls in the transaction, uvc_ctrl_commit or
  816. * uvc_ctrl_rollback must be called to apply the pending changes to the
  817. * hardware or revert them. When applying changes, all controls marked as
  818. * dirty will be modified in the UVC device, and the dirty flag will be
  819. * cleared. When reverting controls, the control data field
  820. * UVC_CTRL_DATA_CURRENT is reverted to its previous value
  821. * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
  822. * control lock.
  823. */
  824. int uvc_ctrl_begin(struct uvc_video_device *video)
  825. {
  826. return mutex_lock_interruptible(&video->ctrl_mutex) ? -ERESTARTSYS : 0;
  827. }
  828. static int uvc_ctrl_commit_entity(struct uvc_device *dev,
  829. struct uvc_entity *entity, int rollback)
  830. {
  831. struct uvc_control *ctrl;
  832. unsigned int i;
  833. int ret;
  834. if (entity == NULL)
  835. return 0;
  836. for (i = 0; i < entity->ncontrols; ++i) {
  837. ctrl = &entity->controls[i];
  838. if (ctrl->info == NULL)
  839. continue;
  840. /* Reset the loaded flag for auto-update controls that were
  841. * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
  842. * uvc_ctrl_get from using the cached value.
  843. */
  844. if (ctrl->info->flags & UVC_CONTROL_AUTO_UPDATE)
  845. ctrl->loaded = 0;
  846. if (!ctrl->dirty)
  847. continue;
  848. if (!rollback)
  849. ret = uvc_query_ctrl(dev, SET_CUR, ctrl->entity->id,
  850. dev->intfnum, ctrl->info->selector,
  851. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  852. ctrl->info->size);
  853. else
  854. ret = 0;
  855. if (rollback || ret < 0)
  856. memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  857. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
  858. ctrl->info->size);
  859. ctrl->dirty = 0;
  860. if (ret < 0)
  861. return ret;
  862. }
  863. return 0;
  864. }
  865. int __uvc_ctrl_commit(struct uvc_video_device *video, int rollback)
  866. {
  867. struct uvc_entity *entity;
  868. int ret = 0;
  869. /* Find the control. */
  870. ret = uvc_ctrl_commit_entity(video->dev, video->processing, rollback);
  871. if (ret < 0)
  872. goto done;
  873. list_for_each_entry(entity, &video->iterms, chain) {
  874. ret = uvc_ctrl_commit_entity(video->dev, entity, rollback);
  875. if (ret < 0)
  876. goto done;
  877. }
  878. list_for_each_entry(entity, &video->extensions, chain) {
  879. ret = uvc_ctrl_commit_entity(video->dev, entity, rollback);
  880. if (ret < 0)
  881. goto done;
  882. }
  883. done:
  884. mutex_unlock(&video->ctrl_mutex);
  885. return ret;
  886. }
  887. int uvc_ctrl_get(struct uvc_video_device *video,
  888. struct v4l2_ext_control *xctrl)
  889. {
  890. struct uvc_control *ctrl;
  891. struct uvc_control_mapping *mapping;
  892. struct uvc_menu_info *menu;
  893. unsigned int i;
  894. int ret;
  895. ctrl = uvc_find_control(video, xctrl->id, &mapping);
  896. if (ctrl == NULL || (ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0)
  897. return -EINVAL;
  898. if (!ctrl->loaded) {
  899. ret = uvc_query_ctrl(video->dev, GET_CUR, ctrl->entity->id,
  900. video->dev->intfnum, ctrl->info->selector,
  901. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  902. ctrl->info->size);
  903. if (ret < 0)
  904. return ret;
  905. ctrl->loaded = 1;
  906. }
  907. xctrl->value = mapping->get(mapping, GET_CUR,
  908. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
  909. if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
  910. menu = mapping->menu_info;
  911. for (i = 0; i < mapping->menu_count; ++i, ++menu) {
  912. if (menu->value == xctrl->value) {
  913. xctrl->value = i;
  914. break;
  915. }
  916. }
  917. }
  918. return 0;
  919. }
  920. int uvc_ctrl_set(struct uvc_video_device *video,
  921. struct v4l2_ext_control *xctrl)
  922. {
  923. struct uvc_control *ctrl;
  924. struct uvc_control_mapping *mapping;
  925. s32 value = xctrl->value;
  926. int ret;
  927. ctrl = uvc_find_control(video, xctrl->id, &mapping);
  928. if (ctrl == NULL || (ctrl->info->flags & UVC_CONTROL_SET_CUR) == 0)
  929. return -EINVAL;
  930. if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
  931. if (value < 0 || value >= mapping->menu_count)
  932. return -EINVAL;
  933. value = mapping->menu_info[value].value;
  934. }
  935. if (!ctrl->loaded && (ctrl->info->size * 8) != mapping->size) {
  936. if ((ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0) {
  937. memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  938. 0, ctrl->info->size);
  939. } else {
  940. ret = uvc_query_ctrl(video->dev, GET_CUR,
  941. ctrl->entity->id, video->dev->intfnum,
  942. ctrl->info->selector,
  943. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  944. ctrl->info->size);
  945. if (ret < 0)
  946. return ret;
  947. }
  948. ctrl->loaded = 1;
  949. }
  950. if (!ctrl->dirty) {
  951. memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
  952. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  953. ctrl->info->size);
  954. }
  955. mapping->set(mapping, value,
  956. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
  957. ctrl->dirty = 1;
  958. ctrl->modified = 1;
  959. return 0;
  960. }
  961. /* --------------------------------------------------------------------------
  962. * Dynamic controls
  963. */
  964. int uvc_xu_ctrl_query(struct uvc_video_device *video,
  965. struct uvc_xu_control *xctrl, int set)
  966. {
  967. struct uvc_entity *entity;
  968. struct uvc_control *ctrl = NULL;
  969. unsigned int i, found = 0;
  970. __u8 *data;
  971. int ret;
  972. /* Find the extension unit. */
  973. list_for_each_entry(entity, &video->extensions, chain) {
  974. if (entity->id == xctrl->unit)
  975. break;
  976. }
  977. if (entity->id != xctrl->unit) {
  978. uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
  979. xctrl->unit);
  980. return -EINVAL;
  981. }
  982. /* Find the control. */
  983. for (i = 0; i < entity->ncontrols; ++i) {
  984. ctrl = &entity->controls[i];
  985. if (ctrl->info == NULL)
  986. continue;
  987. if (ctrl->info->selector == xctrl->selector) {
  988. found = 1;
  989. break;
  990. }
  991. }
  992. if (!found) {
  993. uvc_trace(UVC_TRACE_CONTROL,
  994. "Control " UVC_GUID_FORMAT "/%u not found.\n",
  995. UVC_GUID_ARGS(entity->extension.guidExtensionCode),
  996. xctrl->selector);
  997. return -EINVAL;
  998. }
  999. /* Validate control data size. */
  1000. if (ctrl->info->size != xctrl->size)
  1001. return -EINVAL;
  1002. if ((set && !(ctrl->info->flags & UVC_CONTROL_SET_CUR)) ||
  1003. (!set && !(ctrl->info->flags & UVC_CONTROL_GET_CUR)))
  1004. return -EINVAL;
  1005. if (mutex_lock_interruptible(&video->ctrl_mutex))
  1006. return -ERESTARTSYS;
  1007. memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
  1008. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  1009. xctrl->size);
  1010. data = uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT);
  1011. if (set && copy_from_user(data, xctrl->data, xctrl->size)) {
  1012. ret = -EFAULT;
  1013. goto out;
  1014. }
  1015. ret = uvc_query_ctrl(video->dev, set ? SET_CUR : GET_CUR, xctrl->unit,
  1016. video->dev->intfnum, xctrl->selector, data,
  1017. xctrl->size);
  1018. if (ret < 0)
  1019. goto out;
  1020. if (!set && copy_to_user(xctrl->data, data, xctrl->size)) {
  1021. ret = -EFAULT;
  1022. goto out;
  1023. }
  1024. out:
  1025. if (ret)
  1026. memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  1027. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
  1028. xctrl->size);
  1029. mutex_unlock(&video->ctrl_mutex);
  1030. return ret;
  1031. }
  1032. /* --------------------------------------------------------------------------
  1033. * Suspend/resume
  1034. */
  1035. /*
  1036. * Restore control values after resume, skipping controls that haven't been
  1037. * changed.
  1038. *
  1039. * TODO
  1040. * - Don't restore modified controls that are back to their default value.
  1041. * - Handle restore order (Auto-Exposure Mode should be restored before
  1042. * Exposure Time).
  1043. */
  1044. int uvc_ctrl_resume_device(struct uvc_device *dev)
  1045. {
  1046. struct uvc_control *ctrl;
  1047. struct uvc_entity *entity;
  1048. unsigned int i;
  1049. int ret;
  1050. /* Walk the entities list and restore controls when possible. */
  1051. list_for_each_entry(entity, &dev->entities, list) {
  1052. for (i = 0; i < entity->ncontrols; ++i) {
  1053. ctrl = &entity->controls[i];
  1054. if (ctrl->info == NULL || !ctrl->modified ||
  1055. (ctrl->info->flags & UVC_CONTROL_RESTORE) == 0)
  1056. continue;
  1057. printk(KERN_INFO "restoring control " UVC_GUID_FORMAT
  1058. "/%u/%u\n", UVC_GUID_ARGS(ctrl->info->entity),
  1059. ctrl->info->index, ctrl->info->selector);
  1060. ctrl->dirty = 1;
  1061. }
  1062. ret = uvc_ctrl_commit_entity(dev, entity, 0);
  1063. if (ret < 0)
  1064. return ret;
  1065. }
  1066. return 0;
  1067. }
  1068. /* --------------------------------------------------------------------------
  1069. * Control and mapping handling
  1070. */
  1071. static void uvc_ctrl_add_ctrl(struct uvc_device *dev,
  1072. struct uvc_control_info *info)
  1073. {
  1074. struct uvc_entity *entity;
  1075. struct uvc_control *ctrl = NULL;
  1076. int ret, found = 0;
  1077. unsigned int i;
  1078. list_for_each_entry(entity, &dev->entities, list) {
  1079. if (!uvc_entity_match_guid(entity, info->entity))
  1080. continue;
  1081. for (i = 0; i < entity->ncontrols; ++i) {
  1082. ctrl = &entity->controls[i];
  1083. if (ctrl->index == info->index) {
  1084. found = 1;
  1085. break;
  1086. }
  1087. }
  1088. if (found)
  1089. break;
  1090. }
  1091. if (!found)
  1092. return;
  1093. if (UVC_ENTITY_TYPE(entity) == VC_EXTENSION_UNIT) {
  1094. /* Check if the device control information and length match
  1095. * the user supplied information.
  1096. */
  1097. __u32 flags;
  1098. __le16 size;
  1099. __u8 inf;
  1100. if ((ret = uvc_query_ctrl(dev, GET_LEN, ctrl->entity->id,
  1101. dev->intfnum, info->selector, (__u8 *)&size, 2)) < 0) {
  1102. uvc_trace(UVC_TRACE_CONTROL, "GET_LEN failed on "
  1103. "control " UVC_GUID_FORMAT "/%u (%d).\n",
  1104. UVC_GUID_ARGS(info->entity), info->selector,
  1105. ret);
  1106. return;
  1107. }
  1108. if (info->size != le16_to_cpu(size)) {
  1109. uvc_trace(UVC_TRACE_CONTROL, "Control " UVC_GUID_FORMAT
  1110. "/%u size doesn't match user supplied "
  1111. "value.\n", UVC_GUID_ARGS(info->entity),
  1112. info->selector);
  1113. return;
  1114. }
  1115. if ((ret = uvc_query_ctrl(dev, GET_INFO, ctrl->entity->id,
  1116. dev->intfnum, info->selector, &inf, 1)) < 0) {
  1117. uvc_trace(UVC_TRACE_CONTROL, "GET_INFO failed on "
  1118. "control " UVC_GUID_FORMAT "/%u (%d).\n",
  1119. UVC_GUID_ARGS(info->entity), info->selector,
  1120. ret);
  1121. return;
  1122. }
  1123. flags = info->flags;
  1124. if (((flags & UVC_CONTROL_GET_CUR) && !(inf & (1 << 0))) ||
  1125. ((flags & UVC_CONTROL_SET_CUR) && !(inf & (1 << 1)))) {
  1126. uvc_trace(UVC_TRACE_CONTROL, "Control "
  1127. UVC_GUID_FORMAT "/%u flags don't match "
  1128. "supported operations.\n",
  1129. UVC_GUID_ARGS(info->entity), info->selector);
  1130. return;
  1131. }
  1132. }
  1133. ctrl->info = info;
  1134. ctrl->data = kmalloc(ctrl->info->size * UVC_CTRL_NDATA, GFP_KERNEL);
  1135. uvc_trace(UVC_TRACE_CONTROL, "Added control " UVC_GUID_FORMAT "/%u "
  1136. "to device %s entity %u\n", UVC_GUID_ARGS(ctrl->info->entity),
  1137. ctrl->info->selector, dev->udev->devpath, entity->id);
  1138. }
  1139. /*
  1140. * Add an item to the UVC control information list, and instantiate a control
  1141. * structure for each device that supports the control.
  1142. */
  1143. int uvc_ctrl_add_info(struct uvc_control_info *info)
  1144. {
  1145. struct uvc_control_info *ctrl;
  1146. struct uvc_device *dev;
  1147. int ret = 0;
  1148. /* Find matching controls by walking the devices, entities and
  1149. * controls list.
  1150. */
  1151. mutex_lock(&uvc_driver.ctrl_mutex);
  1152. /* First check if the list contains a control matching the new one.
  1153. * Bail out if it does.
  1154. */
  1155. list_for_each_entry(ctrl, &uvc_driver.controls, list) {
  1156. if (memcmp(ctrl->entity, info->entity, 16))
  1157. continue;
  1158. if (ctrl->selector == info->selector) {
  1159. uvc_trace(UVC_TRACE_CONTROL, "Control "
  1160. UVC_GUID_FORMAT "/%u is already defined.\n",
  1161. UVC_GUID_ARGS(info->entity), info->selector);
  1162. ret = -EEXIST;
  1163. goto end;
  1164. }
  1165. if (ctrl->index == info->index) {
  1166. uvc_trace(UVC_TRACE_CONTROL, "Control "
  1167. UVC_GUID_FORMAT "/%u would overwrite index "
  1168. "%d.\n", UVC_GUID_ARGS(info->entity),
  1169. info->selector, info->index);
  1170. ret = -EEXIST;
  1171. goto end;
  1172. }
  1173. }
  1174. list_for_each_entry(dev, &uvc_driver.devices, list)
  1175. uvc_ctrl_add_ctrl(dev, info);
  1176. INIT_LIST_HEAD(&info->mappings);
  1177. list_add_tail(&info->list, &uvc_driver.controls);
  1178. end:
  1179. mutex_unlock(&uvc_driver.ctrl_mutex);
  1180. return ret;
  1181. }
  1182. int uvc_ctrl_add_mapping(struct uvc_control_mapping *mapping)
  1183. {
  1184. struct uvc_control_info *info;
  1185. struct uvc_control_mapping *map;
  1186. int ret = -EINVAL;
  1187. if (mapping->get == NULL)
  1188. mapping->get = uvc_get_le_value;
  1189. if (mapping->set == NULL)
  1190. mapping->set = uvc_set_le_value;
  1191. if (mapping->id & ~V4L2_CTRL_ID_MASK) {
  1192. uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s' with "
  1193. "invalid control id 0x%08x\n", mapping->name,
  1194. mapping->id);
  1195. return -EINVAL;
  1196. }
  1197. mutex_lock(&uvc_driver.ctrl_mutex);
  1198. list_for_each_entry(info, &uvc_driver.controls, list) {
  1199. if (memcmp(info->entity, mapping->entity, 16) ||
  1200. info->selector != mapping->selector)
  1201. continue;
  1202. if (info->size * 8 < mapping->size + mapping->offset) {
  1203. uvc_trace(UVC_TRACE_CONTROL, "Mapping '%s' would "
  1204. "overflow control " UVC_GUID_FORMAT "/%u\n",
  1205. mapping->name, UVC_GUID_ARGS(info->entity),
  1206. info->selector);
  1207. ret = -EOVERFLOW;
  1208. goto end;
  1209. }
  1210. /* Check if the list contains a mapping matching the new one.
  1211. * Bail out if it does.
  1212. */
  1213. list_for_each_entry(map, &info->mappings, list) {
  1214. if (map->id == mapping->id) {
  1215. uvc_trace(UVC_TRACE_CONTROL, "Mapping '%s' is "
  1216. "already defined.\n", mapping->name);
  1217. ret = -EEXIST;
  1218. goto end;
  1219. }
  1220. }
  1221. mapping->ctrl = info;
  1222. list_add_tail(&mapping->list, &info->mappings);
  1223. uvc_trace(UVC_TRACE_CONTROL, "Adding mapping %s to control "
  1224. UVC_GUID_FORMAT "/%u.\n", mapping->name,
  1225. UVC_GUID_ARGS(info->entity), info->selector);
  1226. ret = 0;
  1227. break;
  1228. }
  1229. end:
  1230. mutex_unlock(&uvc_driver.ctrl_mutex);
  1231. return ret;
  1232. }
  1233. /*
  1234. * Prune an entity of its bogus controls. This currently includes processing
  1235. * unit auto controls for which no corresponding manual control is available.
  1236. * Such auto controls make little sense if any, and are known to crash at
  1237. * least the SiGma Micro webcam.
  1238. */
  1239. static void
  1240. uvc_ctrl_prune_entity(struct uvc_entity *entity)
  1241. {
  1242. static const struct {
  1243. u8 idx_manual;
  1244. u8 idx_auto;
  1245. } blacklist[] = {
  1246. { 2, 11 }, /* Hue */
  1247. { 6, 12 }, /* White Balance Temperature */
  1248. { 7, 13 }, /* White Balance Component */
  1249. };
  1250. u8 *controls;
  1251. unsigned int size;
  1252. unsigned int i;
  1253. if (UVC_ENTITY_TYPE(entity) != VC_PROCESSING_UNIT)
  1254. return;
  1255. controls = entity->processing.bmControls;
  1256. size = entity->processing.bControlSize;
  1257. for (i = 0; i < ARRAY_SIZE(blacklist); ++i) {
  1258. if (blacklist[i].idx_auto >= 8 * size ||
  1259. blacklist[i].idx_manual >= 8 * size)
  1260. continue;
  1261. if (!uvc_test_bit(controls, blacklist[i].idx_auto) ||
  1262. uvc_test_bit(controls, blacklist[i].idx_manual))
  1263. continue;
  1264. uvc_trace(UVC_TRACE_CONTROL, "Auto control %u/%u has no "
  1265. "matching manual control, removing it.\n", entity->id,
  1266. blacklist[i].idx_auto);
  1267. uvc_clear_bit(controls, blacklist[i].idx_auto);
  1268. }
  1269. }
  1270. /*
  1271. * Initialize device controls.
  1272. */
  1273. int uvc_ctrl_init_device(struct uvc_device *dev)
  1274. {
  1275. struct uvc_control_info *info;
  1276. struct uvc_control *ctrl;
  1277. struct uvc_entity *entity;
  1278. unsigned int i;
  1279. /* Walk the entities list and instantiate controls */
  1280. list_for_each_entry(entity, &dev->entities, list) {
  1281. unsigned int bControlSize = 0, ncontrols = 0;
  1282. __u8 *bmControls = NULL;
  1283. if (UVC_ENTITY_TYPE(entity) == VC_EXTENSION_UNIT) {
  1284. bmControls = entity->extension.bmControls;
  1285. bControlSize = entity->extension.bControlSize;
  1286. } else if (UVC_ENTITY_TYPE(entity) == VC_PROCESSING_UNIT) {
  1287. bmControls = entity->processing.bmControls;
  1288. bControlSize = entity->processing.bControlSize;
  1289. } else if (UVC_ENTITY_TYPE(entity) == ITT_CAMERA) {
  1290. bmControls = entity->camera.bmControls;
  1291. bControlSize = entity->camera.bControlSize;
  1292. }
  1293. if (dev->quirks & UVC_QUIRK_PRUNE_CONTROLS)
  1294. uvc_ctrl_prune_entity(entity);
  1295. for (i = 0; i < bControlSize; ++i)
  1296. ncontrols += hweight8(bmControls[i]);
  1297. if (ncontrols == 0)
  1298. continue;
  1299. entity->controls = kzalloc(ncontrols*sizeof *ctrl, GFP_KERNEL);
  1300. if (entity->controls == NULL)
  1301. return -ENOMEM;
  1302. entity->ncontrols = ncontrols;
  1303. ctrl = entity->controls;
  1304. for (i = 0; i < bControlSize * 8; ++i) {
  1305. if (uvc_test_bit(bmControls, i) == 0)
  1306. continue;
  1307. ctrl->entity = entity;
  1308. ctrl->index = i;
  1309. ctrl++;
  1310. }
  1311. }
  1312. /* Walk the controls info list and associate them with the device
  1313. * controls, then add the device to the global device list. This has
  1314. * to be done while holding the controls lock, to make sure
  1315. * uvc_ctrl_add_info() will not get called in-between.
  1316. */
  1317. mutex_lock(&uvc_driver.ctrl_mutex);
  1318. list_for_each_entry(info, &uvc_driver.controls, list)
  1319. uvc_ctrl_add_ctrl(dev, info);
  1320. list_add_tail(&dev->list, &uvc_driver.devices);
  1321. mutex_unlock(&uvc_driver.ctrl_mutex);
  1322. return 0;
  1323. }
  1324. /*
  1325. * Cleanup device controls.
  1326. */
  1327. void uvc_ctrl_cleanup_device(struct uvc_device *dev)
  1328. {
  1329. struct uvc_entity *entity;
  1330. unsigned int i;
  1331. /* Remove the device from the global devices list */
  1332. mutex_lock(&uvc_driver.ctrl_mutex);
  1333. if (dev->list.next != NULL)
  1334. list_del(&dev->list);
  1335. mutex_unlock(&uvc_driver.ctrl_mutex);
  1336. list_for_each_entry(entity, &dev->entities, list) {
  1337. for (i = 0; i < entity->ncontrols; ++i)
  1338. kfree(entity->controls[i].data);
  1339. kfree(entity->controls);
  1340. }
  1341. }
  1342. void uvc_ctrl_init(void)
  1343. {
  1344. struct uvc_control_info *ctrl = uvc_ctrls;
  1345. struct uvc_control_info *cend = ctrl + ARRAY_SIZE(uvc_ctrls);
  1346. struct uvc_control_mapping *mapping = uvc_ctrl_mappings;
  1347. struct uvc_control_mapping *mend =
  1348. mapping + ARRAY_SIZE(uvc_ctrl_mappings);
  1349. for (; ctrl < cend; ++ctrl)
  1350. uvc_ctrl_add_info(ctrl);
  1351. for (; mapping < mend; ++mapping)
  1352. uvc_ctrl_add_mapping(mapping);
  1353. }