uvc_ctrl.c 38 KB

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