uvc_ctrl.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934
  1. /*
  2. * uvc_ctrl.c -- USB Video Class driver - Controls
  3. *
  4. * Copyright (C) 2005-2010
  5. * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  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/slab.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/usb.h>
  19. #include <linux/videodev2.h>
  20. #include <linux/vmalloc.h>
  21. #include <linux/wait.h>
  22. #include <linux/atomic.h>
  23. #include "uvcvideo.h"
  24. #define UVC_CTRL_DATA_CURRENT 0
  25. #define UVC_CTRL_DATA_BACKUP 1
  26. #define UVC_CTRL_DATA_MIN 2
  27. #define UVC_CTRL_DATA_MAX 3
  28. #define UVC_CTRL_DATA_RES 4
  29. #define UVC_CTRL_DATA_DEF 5
  30. #define UVC_CTRL_DATA_LAST 6
  31. /* ------------------------------------------------------------------------
  32. * Controls
  33. */
  34. static struct uvc_control_info uvc_ctrls[] = {
  35. {
  36. .entity = UVC_GUID_UVC_PROCESSING,
  37. .selector = UVC_PU_BRIGHTNESS_CONTROL,
  38. .index = 0,
  39. .size = 2,
  40. .flags = UVC_CTRL_FLAG_SET_CUR
  41. | UVC_CTRL_FLAG_GET_RANGE
  42. | UVC_CTRL_FLAG_RESTORE,
  43. },
  44. {
  45. .entity = UVC_GUID_UVC_PROCESSING,
  46. .selector = UVC_PU_CONTRAST_CONTROL,
  47. .index = 1,
  48. .size = 2,
  49. .flags = UVC_CTRL_FLAG_SET_CUR
  50. | UVC_CTRL_FLAG_GET_RANGE
  51. | UVC_CTRL_FLAG_RESTORE,
  52. },
  53. {
  54. .entity = UVC_GUID_UVC_PROCESSING,
  55. .selector = UVC_PU_HUE_CONTROL,
  56. .index = 2,
  57. .size = 2,
  58. .flags = UVC_CTRL_FLAG_SET_CUR
  59. | UVC_CTRL_FLAG_GET_RANGE
  60. | UVC_CTRL_FLAG_RESTORE
  61. | UVC_CTRL_FLAG_AUTO_UPDATE,
  62. },
  63. {
  64. .entity = UVC_GUID_UVC_PROCESSING,
  65. .selector = UVC_PU_SATURATION_CONTROL,
  66. .index = 3,
  67. .size = 2,
  68. .flags = UVC_CTRL_FLAG_SET_CUR
  69. | UVC_CTRL_FLAG_GET_RANGE
  70. | UVC_CTRL_FLAG_RESTORE,
  71. },
  72. {
  73. .entity = UVC_GUID_UVC_PROCESSING,
  74. .selector = UVC_PU_SHARPNESS_CONTROL,
  75. .index = 4,
  76. .size = 2,
  77. .flags = UVC_CTRL_FLAG_SET_CUR
  78. | UVC_CTRL_FLAG_GET_RANGE
  79. | UVC_CTRL_FLAG_RESTORE,
  80. },
  81. {
  82. .entity = UVC_GUID_UVC_PROCESSING,
  83. .selector = UVC_PU_GAMMA_CONTROL,
  84. .index = 5,
  85. .size = 2,
  86. .flags = UVC_CTRL_FLAG_SET_CUR
  87. | UVC_CTRL_FLAG_GET_RANGE
  88. | UVC_CTRL_FLAG_RESTORE,
  89. },
  90. {
  91. .entity = UVC_GUID_UVC_PROCESSING,
  92. .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
  93. .index = 6,
  94. .size = 2,
  95. .flags = UVC_CTRL_FLAG_SET_CUR
  96. | UVC_CTRL_FLAG_GET_RANGE
  97. | UVC_CTRL_FLAG_RESTORE
  98. | UVC_CTRL_FLAG_AUTO_UPDATE,
  99. },
  100. {
  101. .entity = UVC_GUID_UVC_PROCESSING,
  102. .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
  103. .index = 7,
  104. .size = 4,
  105. .flags = UVC_CTRL_FLAG_SET_CUR
  106. | UVC_CTRL_FLAG_GET_RANGE
  107. | UVC_CTRL_FLAG_RESTORE
  108. | UVC_CTRL_FLAG_AUTO_UPDATE,
  109. },
  110. {
  111. .entity = UVC_GUID_UVC_PROCESSING,
  112. .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
  113. .index = 8,
  114. .size = 2,
  115. .flags = UVC_CTRL_FLAG_SET_CUR
  116. | UVC_CTRL_FLAG_GET_RANGE
  117. | UVC_CTRL_FLAG_RESTORE,
  118. },
  119. {
  120. .entity = UVC_GUID_UVC_PROCESSING,
  121. .selector = UVC_PU_GAIN_CONTROL,
  122. .index = 9,
  123. .size = 2,
  124. .flags = UVC_CTRL_FLAG_SET_CUR
  125. | UVC_CTRL_FLAG_GET_RANGE
  126. | UVC_CTRL_FLAG_RESTORE,
  127. },
  128. {
  129. .entity = UVC_GUID_UVC_PROCESSING,
  130. .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
  131. .index = 10,
  132. .size = 1,
  133. .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
  134. | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
  135. },
  136. {
  137. .entity = UVC_GUID_UVC_PROCESSING,
  138. .selector = UVC_PU_HUE_AUTO_CONTROL,
  139. .index = 11,
  140. .size = 1,
  141. .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
  142. | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
  143. },
  144. {
  145. .entity = UVC_GUID_UVC_PROCESSING,
  146. .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
  147. .index = 12,
  148. .size = 1,
  149. .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
  150. | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
  151. },
  152. {
  153. .entity = UVC_GUID_UVC_PROCESSING,
  154. .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
  155. .index = 13,
  156. .size = 1,
  157. .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
  158. | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
  159. },
  160. {
  161. .entity = UVC_GUID_UVC_PROCESSING,
  162. .selector = UVC_PU_DIGITAL_MULTIPLIER_CONTROL,
  163. .index = 14,
  164. .size = 2,
  165. .flags = UVC_CTRL_FLAG_SET_CUR
  166. | UVC_CTRL_FLAG_GET_RANGE
  167. | UVC_CTRL_FLAG_RESTORE,
  168. },
  169. {
  170. .entity = UVC_GUID_UVC_PROCESSING,
  171. .selector = UVC_PU_DIGITAL_MULTIPLIER_LIMIT_CONTROL,
  172. .index = 15,
  173. .size = 2,
  174. .flags = UVC_CTRL_FLAG_SET_CUR
  175. | UVC_CTRL_FLAG_GET_RANGE
  176. | UVC_CTRL_FLAG_RESTORE,
  177. },
  178. {
  179. .entity = UVC_GUID_UVC_PROCESSING,
  180. .selector = UVC_PU_ANALOG_VIDEO_STANDARD_CONTROL,
  181. .index = 16,
  182. .size = 1,
  183. .flags = UVC_CTRL_FLAG_GET_CUR,
  184. },
  185. {
  186. .entity = UVC_GUID_UVC_PROCESSING,
  187. .selector = UVC_PU_ANALOG_LOCK_STATUS_CONTROL,
  188. .index = 17,
  189. .size = 1,
  190. .flags = UVC_CTRL_FLAG_GET_CUR,
  191. },
  192. {
  193. .entity = UVC_GUID_UVC_CAMERA,
  194. .selector = UVC_CT_SCANNING_MODE_CONTROL,
  195. .index = 0,
  196. .size = 1,
  197. .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
  198. | UVC_CTRL_FLAG_RESTORE,
  199. },
  200. {
  201. .entity = UVC_GUID_UVC_CAMERA,
  202. .selector = UVC_CT_AE_MODE_CONTROL,
  203. .index = 1,
  204. .size = 1,
  205. .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
  206. | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_GET_RES
  207. | UVC_CTRL_FLAG_RESTORE,
  208. },
  209. {
  210. .entity = UVC_GUID_UVC_CAMERA,
  211. .selector = UVC_CT_AE_PRIORITY_CONTROL,
  212. .index = 2,
  213. .size = 1,
  214. .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
  215. | UVC_CTRL_FLAG_RESTORE,
  216. },
  217. {
  218. .entity = UVC_GUID_UVC_CAMERA,
  219. .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
  220. .index = 3,
  221. .size = 4,
  222. .flags = UVC_CTRL_FLAG_SET_CUR
  223. | UVC_CTRL_FLAG_GET_RANGE
  224. | UVC_CTRL_FLAG_RESTORE,
  225. },
  226. {
  227. .entity = UVC_GUID_UVC_CAMERA,
  228. .selector = UVC_CT_EXPOSURE_TIME_RELATIVE_CONTROL,
  229. .index = 4,
  230. .size = 1,
  231. .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_RESTORE,
  232. },
  233. {
  234. .entity = UVC_GUID_UVC_CAMERA,
  235. .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
  236. .index = 5,
  237. .size = 2,
  238. .flags = UVC_CTRL_FLAG_SET_CUR
  239. | UVC_CTRL_FLAG_GET_RANGE
  240. | UVC_CTRL_FLAG_RESTORE
  241. | UVC_CTRL_FLAG_AUTO_UPDATE,
  242. },
  243. {
  244. .entity = UVC_GUID_UVC_CAMERA,
  245. .selector = UVC_CT_FOCUS_RELATIVE_CONTROL,
  246. .index = 6,
  247. .size = 2,
  248. .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
  249. | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
  250. | UVC_CTRL_FLAG_GET_DEF
  251. | UVC_CTRL_FLAG_AUTO_UPDATE,
  252. },
  253. {
  254. .entity = UVC_GUID_UVC_CAMERA,
  255. .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL,
  256. .index = 7,
  257. .size = 2,
  258. .flags = UVC_CTRL_FLAG_SET_CUR
  259. | UVC_CTRL_FLAG_GET_RANGE
  260. | UVC_CTRL_FLAG_RESTORE
  261. | UVC_CTRL_FLAG_AUTO_UPDATE,
  262. },
  263. {
  264. .entity = UVC_GUID_UVC_CAMERA,
  265. .selector = UVC_CT_IRIS_RELATIVE_CONTROL,
  266. .index = 8,
  267. .size = 1,
  268. .flags = UVC_CTRL_FLAG_SET_CUR
  269. | UVC_CTRL_FLAG_AUTO_UPDATE,
  270. },
  271. {
  272. .entity = UVC_GUID_UVC_CAMERA,
  273. .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
  274. .index = 9,
  275. .size = 2,
  276. .flags = UVC_CTRL_FLAG_SET_CUR
  277. | UVC_CTRL_FLAG_GET_RANGE
  278. | UVC_CTRL_FLAG_RESTORE
  279. | UVC_CTRL_FLAG_AUTO_UPDATE,
  280. },
  281. {
  282. .entity = UVC_GUID_UVC_CAMERA,
  283. .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
  284. .index = 10,
  285. .size = 3,
  286. .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
  287. | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
  288. | UVC_CTRL_FLAG_GET_DEF
  289. | UVC_CTRL_FLAG_AUTO_UPDATE,
  290. },
  291. {
  292. .entity = UVC_GUID_UVC_CAMERA,
  293. .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
  294. .index = 11,
  295. .size = 8,
  296. .flags = UVC_CTRL_FLAG_SET_CUR
  297. | UVC_CTRL_FLAG_GET_RANGE
  298. | UVC_CTRL_FLAG_RESTORE
  299. | UVC_CTRL_FLAG_AUTO_UPDATE,
  300. },
  301. {
  302. .entity = UVC_GUID_UVC_CAMERA,
  303. .selector = UVC_CT_PANTILT_RELATIVE_CONTROL,
  304. .index = 12,
  305. .size = 4,
  306. .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
  307. | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
  308. | UVC_CTRL_FLAG_GET_DEF
  309. | UVC_CTRL_FLAG_AUTO_UPDATE,
  310. },
  311. {
  312. .entity = UVC_GUID_UVC_CAMERA,
  313. .selector = UVC_CT_ROLL_ABSOLUTE_CONTROL,
  314. .index = 13,
  315. .size = 2,
  316. .flags = UVC_CTRL_FLAG_SET_CUR
  317. | UVC_CTRL_FLAG_GET_RANGE
  318. | UVC_CTRL_FLAG_RESTORE
  319. | UVC_CTRL_FLAG_AUTO_UPDATE,
  320. },
  321. {
  322. .entity = UVC_GUID_UVC_CAMERA,
  323. .selector = UVC_CT_ROLL_RELATIVE_CONTROL,
  324. .index = 14,
  325. .size = 2,
  326. .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
  327. | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
  328. | UVC_CTRL_FLAG_GET_DEF
  329. | UVC_CTRL_FLAG_AUTO_UPDATE,
  330. },
  331. {
  332. .entity = UVC_GUID_UVC_CAMERA,
  333. .selector = UVC_CT_FOCUS_AUTO_CONTROL,
  334. .index = 17,
  335. .size = 1,
  336. .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
  337. | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
  338. },
  339. {
  340. .entity = UVC_GUID_UVC_CAMERA,
  341. .selector = UVC_CT_PRIVACY_CONTROL,
  342. .index = 18,
  343. .size = 1,
  344. .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
  345. | UVC_CTRL_FLAG_RESTORE
  346. | UVC_CTRL_FLAG_AUTO_UPDATE,
  347. },
  348. };
  349. static struct uvc_menu_info power_line_frequency_controls[] = {
  350. { 0, "Disabled" },
  351. { 1, "50 Hz" },
  352. { 2, "60 Hz" },
  353. };
  354. static struct uvc_menu_info exposure_auto_controls[] = {
  355. { 2, "Auto Mode" },
  356. { 1, "Manual Mode" },
  357. { 4, "Shutter Priority Mode" },
  358. { 8, "Aperture Priority Mode" },
  359. };
  360. static __s32 uvc_ctrl_get_zoom(struct uvc_control_mapping *mapping,
  361. __u8 query, const __u8 *data)
  362. {
  363. __s8 zoom = (__s8)data[0];
  364. switch (query) {
  365. case UVC_GET_CUR:
  366. return (zoom == 0) ? 0 : (zoom > 0 ? data[2] : -data[2]);
  367. case UVC_GET_MIN:
  368. case UVC_GET_MAX:
  369. case UVC_GET_RES:
  370. case UVC_GET_DEF:
  371. default:
  372. return data[2];
  373. }
  374. }
  375. static void uvc_ctrl_set_zoom(struct uvc_control_mapping *mapping,
  376. __s32 value, __u8 *data)
  377. {
  378. data[0] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
  379. data[2] = min((int)abs(value), 0xff);
  380. }
  381. static struct uvc_control_mapping uvc_ctrl_mappings[] = {
  382. {
  383. .id = V4L2_CID_BRIGHTNESS,
  384. .name = "Brightness",
  385. .entity = UVC_GUID_UVC_PROCESSING,
  386. .selector = UVC_PU_BRIGHTNESS_CONTROL,
  387. .size = 16,
  388. .offset = 0,
  389. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  390. .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
  391. },
  392. {
  393. .id = V4L2_CID_CONTRAST,
  394. .name = "Contrast",
  395. .entity = UVC_GUID_UVC_PROCESSING,
  396. .selector = UVC_PU_CONTRAST_CONTROL,
  397. .size = 16,
  398. .offset = 0,
  399. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  400. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  401. },
  402. {
  403. .id = V4L2_CID_HUE,
  404. .name = "Hue",
  405. .entity = UVC_GUID_UVC_PROCESSING,
  406. .selector = UVC_PU_HUE_CONTROL,
  407. .size = 16,
  408. .offset = 0,
  409. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  410. .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
  411. },
  412. {
  413. .id = V4L2_CID_SATURATION,
  414. .name = "Saturation",
  415. .entity = UVC_GUID_UVC_PROCESSING,
  416. .selector = UVC_PU_SATURATION_CONTROL,
  417. .size = 16,
  418. .offset = 0,
  419. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  420. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  421. },
  422. {
  423. .id = V4L2_CID_SHARPNESS,
  424. .name = "Sharpness",
  425. .entity = UVC_GUID_UVC_PROCESSING,
  426. .selector = UVC_PU_SHARPNESS_CONTROL,
  427. .size = 16,
  428. .offset = 0,
  429. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  430. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  431. },
  432. {
  433. .id = V4L2_CID_GAMMA,
  434. .name = "Gamma",
  435. .entity = UVC_GUID_UVC_PROCESSING,
  436. .selector = UVC_PU_GAMMA_CONTROL,
  437. .size = 16,
  438. .offset = 0,
  439. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  440. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  441. },
  442. {
  443. .id = V4L2_CID_BACKLIGHT_COMPENSATION,
  444. .name = "Backlight Compensation",
  445. .entity = UVC_GUID_UVC_PROCESSING,
  446. .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
  447. .size = 16,
  448. .offset = 0,
  449. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  450. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  451. },
  452. {
  453. .id = V4L2_CID_GAIN,
  454. .name = "Gain",
  455. .entity = UVC_GUID_UVC_PROCESSING,
  456. .selector = UVC_PU_GAIN_CONTROL,
  457. .size = 16,
  458. .offset = 0,
  459. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  460. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  461. },
  462. {
  463. .id = V4L2_CID_POWER_LINE_FREQUENCY,
  464. .name = "Power Line Frequency",
  465. .entity = UVC_GUID_UVC_PROCESSING,
  466. .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
  467. .size = 2,
  468. .offset = 0,
  469. .v4l2_type = V4L2_CTRL_TYPE_MENU,
  470. .data_type = UVC_CTRL_DATA_TYPE_ENUM,
  471. .menu_info = power_line_frequency_controls,
  472. .menu_count = ARRAY_SIZE(power_line_frequency_controls),
  473. },
  474. {
  475. .id = V4L2_CID_HUE_AUTO,
  476. .name = "Hue, Auto",
  477. .entity = UVC_GUID_UVC_PROCESSING,
  478. .selector = UVC_PU_HUE_AUTO_CONTROL,
  479. .size = 1,
  480. .offset = 0,
  481. .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
  482. .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
  483. },
  484. {
  485. .id = V4L2_CID_EXPOSURE_AUTO,
  486. .name = "Exposure, Auto",
  487. .entity = UVC_GUID_UVC_CAMERA,
  488. .selector = UVC_CT_AE_MODE_CONTROL,
  489. .size = 4,
  490. .offset = 0,
  491. .v4l2_type = V4L2_CTRL_TYPE_MENU,
  492. .data_type = UVC_CTRL_DATA_TYPE_BITMASK,
  493. .menu_info = exposure_auto_controls,
  494. .menu_count = ARRAY_SIZE(exposure_auto_controls),
  495. },
  496. {
  497. .id = V4L2_CID_EXPOSURE_AUTO_PRIORITY,
  498. .name = "Exposure, Auto Priority",
  499. .entity = UVC_GUID_UVC_CAMERA,
  500. .selector = UVC_CT_AE_PRIORITY_CONTROL,
  501. .size = 1,
  502. .offset = 0,
  503. .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
  504. .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
  505. },
  506. {
  507. .id = V4L2_CID_EXPOSURE_ABSOLUTE,
  508. .name = "Exposure (Absolute)",
  509. .entity = UVC_GUID_UVC_CAMERA,
  510. .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
  511. .size = 32,
  512. .offset = 0,
  513. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  514. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  515. },
  516. {
  517. .id = V4L2_CID_AUTO_WHITE_BALANCE,
  518. .name = "White Balance Temperature, Auto",
  519. .entity = UVC_GUID_UVC_PROCESSING,
  520. .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
  521. .size = 1,
  522. .offset = 0,
  523. .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
  524. .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
  525. },
  526. {
  527. .id = V4L2_CID_WHITE_BALANCE_TEMPERATURE,
  528. .name = "White Balance Temperature",
  529. .entity = UVC_GUID_UVC_PROCESSING,
  530. .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
  531. .size = 16,
  532. .offset = 0,
  533. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  534. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  535. },
  536. {
  537. .id = V4L2_CID_AUTO_WHITE_BALANCE,
  538. .name = "White Balance Component, Auto",
  539. .entity = UVC_GUID_UVC_PROCESSING,
  540. .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
  541. .size = 1,
  542. .offset = 0,
  543. .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
  544. .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
  545. },
  546. {
  547. .id = V4L2_CID_BLUE_BALANCE,
  548. .name = "White Balance Blue Component",
  549. .entity = UVC_GUID_UVC_PROCESSING,
  550. .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
  551. .size = 16,
  552. .offset = 0,
  553. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  554. .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
  555. },
  556. {
  557. .id = V4L2_CID_RED_BALANCE,
  558. .name = "White Balance Red Component",
  559. .entity = UVC_GUID_UVC_PROCESSING,
  560. .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
  561. .size = 16,
  562. .offset = 16,
  563. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  564. .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
  565. },
  566. {
  567. .id = V4L2_CID_FOCUS_ABSOLUTE,
  568. .name = "Focus (absolute)",
  569. .entity = UVC_GUID_UVC_CAMERA,
  570. .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
  571. .size = 16,
  572. .offset = 0,
  573. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  574. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  575. },
  576. {
  577. .id = V4L2_CID_FOCUS_AUTO,
  578. .name = "Focus, Auto",
  579. .entity = UVC_GUID_UVC_CAMERA,
  580. .selector = UVC_CT_FOCUS_AUTO_CONTROL,
  581. .size = 1,
  582. .offset = 0,
  583. .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
  584. .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
  585. },
  586. {
  587. .id = V4L2_CID_IRIS_ABSOLUTE,
  588. .name = "Iris, Absolute",
  589. .entity = UVC_GUID_UVC_CAMERA,
  590. .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL,
  591. .size = 16,
  592. .offset = 0,
  593. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  594. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  595. },
  596. {
  597. .id = V4L2_CID_IRIS_RELATIVE,
  598. .name = "Iris, Relative",
  599. .entity = UVC_GUID_UVC_CAMERA,
  600. .selector = UVC_CT_IRIS_RELATIVE_CONTROL,
  601. .size = 8,
  602. .offset = 0,
  603. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  604. .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
  605. },
  606. {
  607. .id = V4L2_CID_ZOOM_ABSOLUTE,
  608. .name = "Zoom, Absolute",
  609. .entity = UVC_GUID_UVC_CAMERA,
  610. .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
  611. .size = 16,
  612. .offset = 0,
  613. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  614. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  615. },
  616. {
  617. .id = V4L2_CID_ZOOM_CONTINUOUS,
  618. .name = "Zoom, Continuous",
  619. .entity = UVC_GUID_UVC_CAMERA,
  620. .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
  621. .size = 0,
  622. .offset = 0,
  623. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  624. .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
  625. .get = uvc_ctrl_get_zoom,
  626. .set = uvc_ctrl_set_zoom,
  627. },
  628. {
  629. .id = V4L2_CID_PAN_ABSOLUTE,
  630. .name = "Pan (Absolute)",
  631. .entity = UVC_GUID_UVC_CAMERA,
  632. .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
  633. .size = 32,
  634. .offset = 0,
  635. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  636. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  637. },
  638. {
  639. .id = V4L2_CID_TILT_ABSOLUTE,
  640. .name = "Tilt (Absolute)",
  641. .entity = UVC_GUID_UVC_CAMERA,
  642. .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
  643. .size = 32,
  644. .offset = 32,
  645. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  646. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  647. },
  648. {
  649. .id = V4L2_CID_PRIVACY,
  650. .name = "Privacy",
  651. .entity = UVC_GUID_UVC_CAMERA,
  652. .selector = UVC_CT_PRIVACY_CONTROL,
  653. .size = 1,
  654. .offset = 0,
  655. .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
  656. .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
  657. },
  658. };
  659. /* ------------------------------------------------------------------------
  660. * Utility functions
  661. */
  662. static inline __u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id)
  663. {
  664. return ctrl->uvc_data + id * ctrl->info.size;
  665. }
  666. static inline int uvc_test_bit(const __u8 *data, int bit)
  667. {
  668. return (data[bit >> 3] >> (bit & 7)) & 1;
  669. }
  670. static inline void uvc_clear_bit(__u8 *data, int bit)
  671. {
  672. data[bit >> 3] &= ~(1 << (bit & 7));
  673. }
  674. /* Extract the bit string specified by mapping->offset and mapping->size
  675. * from the little-endian data stored at 'data' and return the result as
  676. * a signed 32bit integer. Sign extension will be performed if the mapping
  677. * references a signed data type.
  678. */
  679. static __s32 uvc_get_le_value(struct uvc_control_mapping *mapping,
  680. __u8 query, const __u8 *data)
  681. {
  682. int bits = mapping->size;
  683. int offset = mapping->offset;
  684. __s32 value = 0;
  685. __u8 mask;
  686. data += offset / 8;
  687. offset &= 7;
  688. mask = ((1LL << bits) - 1) << offset;
  689. for (; bits > 0; data++) {
  690. __u8 byte = *data & mask;
  691. value |= offset > 0 ? (byte >> offset) : (byte << (-offset));
  692. bits -= 8 - (offset > 0 ? offset : 0);
  693. offset -= 8;
  694. mask = (1 << bits) - 1;
  695. }
  696. /* Sign-extend the value if needed. */
  697. if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
  698. value |= -(value & (1 << (mapping->size - 1)));
  699. return value;
  700. }
  701. /* Set the bit string specified by mapping->offset and mapping->size
  702. * in the little-endian data stored at 'data' to the value 'value'.
  703. */
  704. static void uvc_set_le_value(struct uvc_control_mapping *mapping,
  705. __s32 value, __u8 *data)
  706. {
  707. int bits = mapping->size;
  708. int offset = mapping->offset;
  709. __u8 mask;
  710. /* According to the v4l2 spec, writing any value to a button control
  711. * should result in the action belonging to the button control being
  712. * triggered. UVC devices however want to see a 1 written -> override
  713. * value.
  714. */
  715. if (mapping->v4l2_type == V4L2_CTRL_TYPE_BUTTON)
  716. value = -1;
  717. data += offset / 8;
  718. offset &= 7;
  719. for (; bits > 0; data++) {
  720. mask = ((1LL << bits) - 1) << offset;
  721. *data = (*data & ~mask) | ((value << offset) & mask);
  722. value >>= offset ? offset : 8;
  723. bits -= 8 - offset;
  724. offset = 0;
  725. }
  726. }
  727. /* ------------------------------------------------------------------------
  728. * Terminal and unit management
  729. */
  730. static const __u8 uvc_processing_guid[16] = UVC_GUID_UVC_PROCESSING;
  731. static const __u8 uvc_camera_guid[16] = UVC_GUID_UVC_CAMERA;
  732. static const __u8 uvc_media_transport_input_guid[16] =
  733. UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT;
  734. static int uvc_entity_match_guid(const struct uvc_entity *entity,
  735. const __u8 guid[16])
  736. {
  737. switch (UVC_ENTITY_TYPE(entity)) {
  738. case UVC_ITT_CAMERA:
  739. return memcmp(uvc_camera_guid, guid, 16) == 0;
  740. case UVC_ITT_MEDIA_TRANSPORT_INPUT:
  741. return memcmp(uvc_media_transport_input_guid, guid, 16) == 0;
  742. case UVC_VC_PROCESSING_UNIT:
  743. return memcmp(uvc_processing_guid, guid, 16) == 0;
  744. case UVC_VC_EXTENSION_UNIT:
  745. return memcmp(entity->extension.guidExtensionCode,
  746. guid, 16) == 0;
  747. default:
  748. return 0;
  749. }
  750. }
  751. /* ------------------------------------------------------------------------
  752. * UVC Controls
  753. */
  754. static void __uvc_find_control(struct uvc_entity *entity, __u32 v4l2_id,
  755. struct uvc_control_mapping **mapping, struct uvc_control **control,
  756. int next)
  757. {
  758. struct uvc_control *ctrl;
  759. struct uvc_control_mapping *map;
  760. unsigned int i;
  761. if (entity == NULL)
  762. return;
  763. for (i = 0; i < entity->ncontrols; ++i) {
  764. ctrl = &entity->controls[i];
  765. if (!ctrl->initialized)
  766. continue;
  767. list_for_each_entry(map, &ctrl->info.mappings, list) {
  768. if ((map->id == v4l2_id) && !next) {
  769. *control = ctrl;
  770. *mapping = map;
  771. return;
  772. }
  773. if ((*mapping == NULL || (*mapping)->id > map->id) &&
  774. (map->id > v4l2_id) && next) {
  775. *control = ctrl;
  776. *mapping = map;
  777. }
  778. }
  779. }
  780. }
  781. static struct uvc_control *uvc_find_control(struct uvc_video_chain *chain,
  782. __u32 v4l2_id, struct uvc_control_mapping **mapping)
  783. {
  784. struct uvc_control *ctrl = NULL;
  785. struct uvc_entity *entity;
  786. int next = v4l2_id & V4L2_CTRL_FLAG_NEXT_CTRL;
  787. *mapping = NULL;
  788. /* Mask the query flags. */
  789. v4l2_id &= V4L2_CTRL_ID_MASK;
  790. /* Find the control. */
  791. list_for_each_entry(entity, &chain->entities, chain) {
  792. __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next);
  793. if (ctrl && !next)
  794. return ctrl;
  795. }
  796. if (ctrl == NULL && !next)
  797. uvc_trace(UVC_TRACE_CONTROL, "Control 0x%08x not found.\n",
  798. v4l2_id);
  799. return ctrl;
  800. }
  801. static int uvc_ctrl_populate_cache(struct uvc_video_chain *chain,
  802. struct uvc_control *ctrl)
  803. {
  804. int ret;
  805. if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
  806. ret = uvc_query_ctrl(chain->dev, UVC_GET_DEF, ctrl->entity->id,
  807. chain->dev->intfnum, ctrl->info.selector,
  808. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF),
  809. ctrl->info.size);
  810. if (ret < 0)
  811. return ret;
  812. }
  813. if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN) {
  814. ret = uvc_query_ctrl(chain->dev, UVC_GET_MIN, ctrl->entity->id,
  815. chain->dev->intfnum, ctrl->info.selector,
  816. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN),
  817. ctrl->info.size);
  818. if (ret < 0)
  819. return ret;
  820. }
  821. if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX) {
  822. ret = uvc_query_ctrl(chain->dev, UVC_GET_MAX, ctrl->entity->id,
  823. chain->dev->intfnum, ctrl->info.selector,
  824. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX),
  825. ctrl->info.size);
  826. if (ret < 0)
  827. return ret;
  828. }
  829. if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES) {
  830. ret = uvc_query_ctrl(chain->dev, UVC_GET_RES, ctrl->entity->id,
  831. chain->dev->intfnum, ctrl->info.selector,
  832. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES),
  833. ctrl->info.size);
  834. if (ret < 0) {
  835. if (UVC_ENTITY_TYPE(ctrl->entity) !=
  836. UVC_VC_EXTENSION_UNIT)
  837. return ret;
  838. /* GET_RES is mandatory for XU controls, but some
  839. * cameras still choke on it. Ignore errors and set the
  840. * resolution value to zero.
  841. */
  842. uvc_warn_once(chain->dev, UVC_WARN_XU_GET_RES,
  843. "UVC non compliance - GET_RES failed on "
  844. "an XU control. Enabling workaround.\n");
  845. memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES), 0,
  846. ctrl->info.size);
  847. }
  848. }
  849. ctrl->cached = 1;
  850. return 0;
  851. }
  852. int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
  853. struct v4l2_queryctrl *v4l2_ctrl)
  854. {
  855. struct uvc_control *ctrl;
  856. struct uvc_control_mapping *mapping;
  857. struct uvc_menu_info *menu;
  858. unsigned int i;
  859. int ret;
  860. ret = mutex_lock_interruptible(&chain->ctrl_mutex);
  861. if (ret < 0)
  862. return -ERESTARTSYS;
  863. ctrl = uvc_find_control(chain, v4l2_ctrl->id, &mapping);
  864. if (ctrl == NULL) {
  865. ret = -EINVAL;
  866. goto done;
  867. }
  868. memset(v4l2_ctrl, 0, sizeof *v4l2_ctrl);
  869. v4l2_ctrl->id = mapping->id;
  870. v4l2_ctrl->type = mapping->v4l2_type;
  871. strlcpy(v4l2_ctrl->name, mapping->name, sizeof v4l2_ctrl->name);
  872. v4l2_ctrl->flags = 0;
  873. if (!(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
  874. v4l2_ctrl->flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
  875. if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
  876. v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  877. if (!ctrl->cached) {
  878. ret = uvc_ctrl_populate_cache(chain, ctrl);
  879. if (ret < 0)
  880. goto done;
  881. }
  882. if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
  883. v4l2_ctrl->default_value = mapping->get(mapping, UVC_GET_DEF,
  884. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF));
  885. }
  886. switch (mapping->v4l2_type) {
  887. case V4L2_CTRL_TYPE_MENU:
  888. v4l2_ctrl->minimum = 0;
  889. v4l2_ctrl->maximum = mapping->menu_count - 1;
  890. v4l2_ctrl->step = 1;
  891. menu = mapping->menu_info;
  892. for (i = 0; i < mapping->menu_count; ++i, ++menu) {
  893. if (menu->value == v4l2_ctrl->default_value) {
  894. v4l2_ctrl->default_value = i;
  895. break;
  896. }
  897. }
  898. goto done;
  899. case V4L2_CTRL_TYPE_BOOLEAN:
  900. v4l2_ctrl->minimum = 0;
  901. v4l2_ctrl->maximum = 1;
  902. v4l2_ctrl->step = 1;
  903. goto done;
  904. case V4L2_CTRL_TYPE_BUTTON:
  905. v4l2_ctrl->minimum = 0;
  906. v4l2_ctrl->maximum = 0;
  907. v4l2_ctrl->step = 0;
  908. goto done;
  909. default:
  910. break;
  911. }
  912. if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN)
  913. v4l2_ctrl->minimum = mapping->get(mapping, UVC_GET_MIN,
  914. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
  915. if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX)
  916. v4l2_ctrl->maximum = mapping->get(mapping, UVC_GET_MAX,
  917. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
  918. if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)
  919. v4l2_ctrl->step = mapping->get(mapping, UVC_GET_RES,
  920. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
  921. done:
  922. mutex_unlock(&chain->ctrl_mutex);
  923. return ret;
  924. }
  925. /*
  926. * Mapping V4L2 controls to UVC controls can be straighforward if done well.
  927. * Most of the UVC controls exist in V4L2, and can be mapped directly. Some
  928. * must be grouped (for instance the Red Balance, Blue Balance and Do White
  929. * Balance V4L2 controls use the White Balance Component UVC control) or
  930. * otherwise translated. The approach we take here is to use a translation
  931. * table for the controls that can be mapped directly, and handle the others
  932. * manually.
  933. */
  934. int uvc_query_v4l2_menu(struct uvc_video_chain *chain,
  935. struct v4l2_querymenu *query_menu)
  936. {
  937. struct uvc_menu_info *menu_info;
  938. struct uvc_control_mapping *mapping;
  939. struct uvc_control *ctrl;
  940. u32 index = query_menu->index;
  941. u32 id = query_menu->id;
  942. int ret;
  943. memset(query_menu, 0, sizeof(*query_menu));
  944. query_menu->id = id;
  945. query_menu->index = index;
  946. ret = mutex_lock_interruptible(&chain->ctrl_mutex);
  947. if (ret < 0)
  948. return -ERESTARTSYS;
  949. ctrl = uvc_find_control(chain, query_menu->id, &mapping);
  950. if (ctrl == NULL || mapping->v4l2_type != V4L2_CTRL_TYPE_MENU) {
  951. ret = -EINVAL;
  952. goto done;
  953. }
  954. if (query_menu->index >= mapping->menu_count) {
  955. ret = -EINVAL;
  956. goto done;
  957. }
  958. menu_info = &mapping->menu_info[query_menu->index];
  959. if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK &&
  960. (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)) {
  961. s32 bitmap;
  962. if (!ctrl->cached) {
  963. ret = uvc_ctrl_populate_cache(chain, ctrl);
  964. if (ret < 0)
  965. goto done;
  966. }
  967. bitmap = mapping->get(mapping, UVC_GET_RES,
  968. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
  969. if (!(bitmap & menu_info->value)) {
  970. ret = -EINVAL;
  971. goto done;
  972. }
  973. }
  974. strlcpy(query_menu->name, menu_info->name, sizeof query_menu->name);
  975. done:
  976. mutex_unlock(&chain->ctrl_mutex);
  977. return ret;
  978. }
  979. /* --------------------------------------------------------------------------
  980. * Control transactions
  981. *
  982. * To make extended set operations as atomic as the hardware allows, controls
  983. * are handled using begin/commit/rollback operations.
  984. *
  985. * At the beginning of a set request, uvc_ctrl_begin should be called to
  986. * initialize the request. This function acquires the control lock.
  987. *
  988. * When setting a control, the new value is stored in the control data field
  989. * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
  990. * later processing. If the UVC and V4L2 control sizes differ, the current
  991. * value is loaded from the hardware before storing the new value in the data
  992. * field.
  993. *
  994. * After processing all controls in the transaction, uvc_ctrl_commit or
  995. * uvc_ctrl_rollback must be called to apply the pending changes to the
  996. * hardware or revert them. When applying changes, all controls marked as
  997. * dirty will be modified in the UVC device, and the dirty flag will be
  998. * cleared. When reverting controls, the control data field
  999. * UVC_CTRL_DATA_CURRENT is reverted to its previous value
  1000. * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
  1001. * control lock.
  1002. */
  1003. int uvc_ctrl_begin(struct uvc_video_chain *chain)
  1004. {
  1005. return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0;
  1006. }
  1007. static int uvc_ctrl_commit_entity(struct uvc_device *dev,
  1008. struct uvc_entity *entity, int rollback)
  1009. {
  1010. struct uvc_control *ctrl;
  1011. unsigned int i;
  1012. int ret;
  1013. if (entity == NULL)
  1014. return 0;
  1015. for (i = 0; i < entity->ncontrols; ++i) {
  1016. ctrl = &entity->controls[i];
  1017. if (!ctrl->initialized)
  1018. continue;
  1019. /* Reset the loaded flag for auto-update controls that were
  1020. * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
  1021. * uvc_ctrl_get from using the cached value.
  1022. */
  1023. if (ctrl->info.flags & UVC_CTRL_FLAG_AUTO_UPDATE)
  1024. ctrl->loaded = 0;
  1025. if (!ctrl->dirty)
  1026. continue;
  1027. if (!rollback)
  1028. ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id,
  1029. dev->intfnum, ctrl->info.selector,
  1030. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  1031. ctrl->info.size);
  1032. else
  1033. ret = 0;
  1034. if (rollback || ret < 0)
  1035. memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  1036. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
  1037. ctrl->info.size);
  1038. ctrl->dirty = 0;
  1039. if (ret < 0)
  1040. return ret;
  1041. }
  1042. return 0;
  1043. }
  1044. int __uvc_ctrl_commit(struct uvc_video_chain *chain, int rollback)
  1045. {
  1046. struct uvc_entity *entity;
  1047. int ret = 0;
  1048. /* Find the control. */
  1049. list_for_each_entry(entity, &chain->entities, chain) {
  1050. ret = uvc_ctrl_commit_entity(chain->dev, entity, rollback);
  1051. if (ret < 0)
  1052. goto done;
  1053. }
  1054. done:
  1055. mutex_unlock(&chain->ctrl_mutex);
  1056. return ret;
  1057. }
  1058. int uvc_ctrl_get(struct uvc_video_chain *chain,
  1059. struct v4l2_ext_control *xctrl)
  1060. {
  1061. struct uvc_control *ctrl;
  1062. struct uvc_control_mapping *mapping;
  1063. struct uvc_menu_info *menu;
  1064. unsigned int i;
  1065. int ret;
  1066. ctrl = uvc_find_control(chain, xctrl->id, &mapping);
  1067. if (ctrl == NULL || (ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0)
  1068. return -EINVAL;
  1069. if (!ctrl->loaded) {
  1070. ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, ctrl->entity->id,
  1071. chain->dev->intfnum, ctrl->info.selector,
  1072. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  1073. ctrl->info.size);
  1074. if (ret < 0)
  1075. return ret;
  1076. ctrl->loaded = 1;
  1077. }
  1078. xctrl->value = mapping->get(mapping, UVC_GET_CUR,
  1079. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
  1080. if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
  1081. menu = mapping->menu_info;
  1082. for (i = 0; i < mapping->menu_count; ++i, ++menu) {
  1083. if (menu->value == xctrl->value) {
  1084. xctrl->value = i;
  1085. break;
  1086. }
  1087. }
  1088. }
  1089. return 0;
  1090. }
  1091. int uvc_ctrl_set(struct uvc_video_chain *chain,
  1092. struct v4l2_ext_control *xctrl)
  1093. {
  1094. struct uvc_control *ctrl;
  1095. struct uvc_control_mapping *mapping;
  1096. s32 value;
  1097. u32 step;
  1098. s32 min;
  1099. s32 max;
  1100. int ret;
  1101. ctrl = uvc_find_control(chain, xctrl->id, &mapping);
  1102. if (ctrl == NULL || (ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR) == 0)
  1103. return -EINVAL;
  1104. /* Clamp out of range values. */
  1105. switch (mapping->v4l2_type) {
  1106. case V4L2_CTRL_TYPE_INTEGER:
  1107. if (!ctrl->cached) {
  1108. ret = uvc_ctrl_populate_cache(chain, ctrl);
  1109. if (ret < 0)
  1110. return ret;
  1111. }
  1112. min = mapping->get(mapping, UVC_GET_MIN,
  1113. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
  1114. max = mapping->get(mapping, UVC_GET_MAX,
  1115. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
  1116. step = mapping->get(mapping, UVC_GET_RES,
  1117. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
  1118. if (step == 0)
  1119. step = 1;
  1120. xctrl->value = min + (xctrl->value - min + step/2) / step * step;
  1121. xctrl->value = clamp(xctrl->value, min, max);
  1122. value = xctrl->value;
  1123. break;
  1124. case V4L2_CTRL_TYPE_BOOLEAN:
  1125. xctrl->value = clamp(xctrl->value, 0, 1);
  1126. value = xctrl->value;
  1127. break;
  1128. case V4L2_CTRL_TYPE_MENU:
  1129. if (xctrl->value < 0 || xctrl->value >= mapping->menu_count)
  1130. return -ERANGE;
  1131. value = mapping->menu_info[xctrl->value].value;
  1132. /* Valid menu indices are reported by the GET_RES request for
  1133. * UVC controls that support it.
  1134. */
  1135. if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK &&
  1136. (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)) {
  1137. if (!ctrl->cached) {
  1138. ret = uvc_ctrl_populate_cache(chain, ctrl);
  1139. if (ret < 0)
  1140. return ret;
  1141. }
  1142. step = mapping->get(mapping, UVC_GET_RES,
  1143. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
  1144. if (!(step & value))
  1145. return -ERANGE;
  1146. }
  1147. break;
  1148. default:
  1149. value = xctrl->value;
  1150. break;
  1151. }
  1152. /* If the mapping doesn't span the whole UVC control, the current value
  1153. * needs to be loaded from the device to perform the read-modify-write
  1154. * operation.
  1155. */
  1156. if (!ctrl->loaded && (ctrl->info.size * 8) != mapping->size) {
  1157. if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0) {
  1158. memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  1159. 0, ctrl->info.size);
  1160. } else {
  1161. ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
  1162. ctrl->entity->id, chain->dev->intfnum,
  1163. ctrl->info.selector,
  1164. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  1165. ctrl->info.size);
  1166. if (ret < 0)
  1167. return ret;
  1168. }
  1169. ctrl->loaded = 1;
  1170. }
  1171. /* Backup the current value in case we need to rollback later. */
  1172. if (!ctrl->dirty) {
  1173. memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
  1174. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  1175. ctrl->info.size);
  1176. }
  1177. mapping->set(mapping, value,
  1178. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
  1179. ctrl->dirty = 1;
  1180. ctrl->modified = 1;
  1181. return 0;
  1182. }
  1183. /* --------------------------------------------------------------------------
  1184. * Dynamic controls
  1185. */
  1186. static void uvc_ctrl_fixup_xu_info(struct uvc_device *dev,
  1187. const struct uvc_control *ctrl, struct uvc_control_info *info)
  1188. {
  1189. struct uvc_ctrl_fixup {
  1190. struct usb_device_id id;
  1191. u8 entity;
  1192. u8 selector;
  1193. u8 flags;
  1194. };
  1195. static const struct uvc_ctrl_fixup fixups[] = {
  1196. { { USB_DEVICE(0x046d, 0x08c2) }, 9, 1,
  1197. UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
  1198. UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
  1199. UVC_CTRL_FLAG_AUTO_UPDATE },
  1200. { { USB_DEVICE(0x046d, 0x08cc) }, 9, 1,
  1201. UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
  1202. UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
  1203. UVC_CTRL_FLAG_AUTO_UPDATE },
  1204. { { USB_DEVICE(0x046d, 0x0994) }, 9, 1,
  1205. UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
  1206. UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
  1207. UVC_CTRL_FLAG_AUTO_UPDATE },
  1208. };
  1209. unsigned int i;
  1210. for (i = 0; i < ARRAY_SIZE(fixups); ++i) {
  1211. if (!usb_match_one_id(dev->intf, &fixups[i].id))
  1212. continue;
  1213. if (fixups[i].entity == ctrl->entity->id &&
  1214. fixups[i].selector == info->selector) {
  1215. info->flags = fixups[i].flags;
  1216. return;
  1217. }
  1218. }
  1219. }
  1220. /*
  1221. * Query control information (size and flags) for XU controls.
  1222. */
  1223. static int uvc_ctrl_fill_xu_info(struct uvc_device *dev,
  1224. const struct uvc_control *ctrl, struct uvc_control_info *info)
  1225. {
  1226. u8 *data;
  1227. int ret;
  1228. data = kmalloc(2, GFP_KERNEL);
  1229. if (data == NULL)
  1230. return -ENOMEM;
  1231. memcpy(info->entity, ctrl->entity->extension.guidExtensionCode,
  1232. sizeof(info->entity));
  1233. info->index = ctrl->index;
  1234. info->selector = ctrl->index + 1;
  1235. /* Query and verify the control length (GET_LEN) */
  1236. ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id, dev->intfnum,
  1237. info->selector, data, 2);
  1238. if (ret < 0) {
  1239. uvc_trace(UVC_TRACE_CONTROL,
  1240. "GET_LEN failed on control %pUl/%u (%d).\n",
  1241. info->entity, info->selector, ret);
  1242. goto done;
  1243. }
  1244. info->size = le16_to_cpup((__le16 *)data);
  1245. /* Query the control information (GET_INFO) */
  1246. ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id, dev->intfnum,
  1247. info->selector, data, 1);
  1248. if (ret < 0) {
  1249. uvc_trace(UVC_TRACE_CONTROL,
  1250. "GET_INFO failed on control %pUl/%u (%d).\n",
  1251. info->entity, info->selector, ret);
  1252. goto done;
  1253. }
  1254. info->flags = UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX
  1255. | UVC_CTRL_FLAG_GET_RES | UVC_CTRL_FLAG_GET_DEF
  1256. | (data[0] & UVC_CONTROL_CAP_GET ?
  1257. UVC_CTRL_FLAG_GET_CUR : 0)
  1258. | (data[0] & UVC_CONTROL_CAP_SET ?
  1259. UVC_CTRL_FLAG_SET_CUR : 0)
  1260. | (data[0] & UVC_CONTROL_CAP_AUTOUPDATE ?
  1261. UVC_CTRL_FLAG_AUTO_UPDATE : 0);
  1262. uvc_ctrl_fixup_xu_info(dev, ctrl, info);
  1263. uvc_trace(UVC_TRACE_CONTROL, "XU control %pUl/%u queried: len %u, "
  1264. "flags { get %u set %u auto %u }.\n",
  1265. info->entity, info->selector, info->size,
  1266. (info->flags & UVC_CTRL_FLAG_GET_CUR) ? 1 : 0,
  1267. (info->flags & UVC_CTRL_FLAG_SET_CUR) ? 1 : 0,
  1268. (info->flags & UVC_CTRL_FLAG_AUTO_UPDATE) ? 1 : 0);
  1269. done:
  1270. kfree(data);
  1271. return ret;
  1272. }
  1273. static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
  1274. const struct uvc_control_info *info);
  1275. static int uvc_ctrl_init_xu_ctrl(struct uvc_device *dev,
  1276. struct uvc_control *ctrl)
  1277. {
  1278. struct uvc_control_info info;
  1279. int ret;
  1280. if (ctrl->initialized)
  1281. return 0;
  1282. ret = uvc_ctrl_fill_xu_info(dev, ctrl, &info);
  1283. if (ret < 0)
  1284. return ret;
  1285. ret = uvc_ctrl_add_info(dev, ctrl, &info);
  1286. if (ret < 0)
  1287. uvc_trace(UVC_TRACE_CONTROL, "Failed to initialize control "
  1288. "%pUl/%u on device %s entity %u\n", info.entity,
  1289. info.selector, dev->udev->devpath, ctrl->entity->id);
  1290. return ret;
  1291. }
  1292. int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
  1293. struct uvc_xu_control_query *xqry)
  1294. {
  1295. struct uvc_entity *entity;
  1296. struct uvc_control *ctrl;
  1297. unsigned int i, found = 0;
  1298. __u32 reqflags;
  1299. __u16 size;
  1300. __u8 *data = NULL;
  1301. int ret;
  1302. /* Find the extension unit. */
  1303. list_for_each_entry(entity, &chain->entities, chain) {
  1304. if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT &&
  1305. entity->id == xqry->unit)
  1306. break;
  1307. }
  1308. if (entity->id != xqry->unit) {
  1309. uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
  1310. xqry->unit);
  1311. return -ENOENT;
  1312. }
  1313. /* Find the control and perform delayed initialization if needed. */
  1314. for (i = 0; i < entity->ncontrols; ++i) {
  1315. ctrl = &entity->controls[i];
  1316. if (ctrl->index == xqry->selector - 1) {
  1317. found = 1;
  1318. break;
  1319. }
  1320. }
  1321. if (!found) {
  1322. uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u not found.\n",
  1323. entity->extension.guidExtensionCode, xqry->selector);
  1324. return -ENOENT;
  1325. }
  1326. if (mutex_lock_interruptible(&chain->ctrl_mutex))
  1327. return -ERESTARTSYS;
  1328. ret = uvc_ctrl_init_xu_ctrl(chain->dev, ctrl);
  1329. if (ret < 0) {
  1330. ret = -ENOENT;
  1331. goto done;
  1332. }
  1333. /* Validate the required buffer size and flags for the request */
  1334. reqflags = 0;
  1335. size = ctrl->info.size;
  1336. switch (xqry->query) {
  1337. case UVC_GET_CUR:
  1338. reqflags = UVC_CTRL_FLAG_GET_CUR;
  1339. break;
  1340. case UVC_GET_MIN:
  1341. reqflags = UVC_CTRL_FLAG_GET_MIN;
  1342. break;
  1343. case UVC_GET_MAX:
  1344. reqflags = UVC_CTRL_FLAG_GET_MAX;
  1345. break;
  1346. case UVC_GET_DEF:
  1347. reqflags = UVC_CTRL_FLAG_GET_DEF;
  1348. break;
  1349. case UVC_GET_RES:
  1350. reqflags = UVC_CTRL_FLAG_GET_RES;
  1351. break;
  1352. case UVC_SET_CUR:
  1353. reqflags = UVC_CTRL_FLAG_SET_CUR;
  1354. break;
  1355. case UVC_GET_LEN:
  1356. size = 2;
  1357. break;
  1358. case UVC_GET_INFO:
  1359. size = 1;
  1360. break;
  1361. default:
  1362. ret = -EINVAL;
  1363. goto done;
  1364. }
  1365. if (size != xqry->size) {
  1366. ret = -ENOBUFS;
  1367. goto done;
  1368. }
  1369. if (reqflags && !(ctrl->info.flags & reqflags)) {
  1370. ret = -EBADRQC;
  1371. goto done;
  1372. }
  1373. data = kmalloc(size, GFP_KERNEL);
  1374. if (data == NULL) {
  1375. ret = -ENOMEM;
  1376. goto done;
  1377. }
  1378. if (xqry->query == UVC_SET_CUR &&
  1379. copy_from_user(data, xqry->data, size)) {
  1380. ret = -EFAULT;
  1381. goto done;
  1382. }
  1383. ret = uvc_query_ctrl(chain->dev, xqry->query, xqry->unit,
  1384. chain->dev->intfnum, xqry->selector, data, size);
  1385. if (ret < 0)
  1386. goto done;
  1387. if (xqry->query != UVC_SET_CUR &&
  1388. copy_to_user(xqry->data, data, size))
  1389. ret = -EFAULT;
  1390. done:
  1391. kfree(data);
  1392. mutex_unlock(&chain->ctrl_mutex);
  1393. return ret;
  1394. }
  1395. /* --------------------------------------------------------------------------
  1396. * Suspend/resume
  1397. */
  1398. /*
  1399. * Restore control values after resume, skipping controls that haven't been
  1400. * changed.
  1401. *
  1402. * TODO
  1403. * - Don't restore modified controls that are back to their default value.
  1404. * - Handle restore order (Auto-Exposure Mode should be restored before
  1405. * Exposure Time).
  1406. */
  1407. int uvc_ctrl_resume_device(struct uvc_device *dev)
  1408. {
  1409. struct uvc_control *ctrl;
  1410. struct uvc_entity *entity;
  1411. unsigned int i;
  1412. int ret;
  1413. /* Walk the entities list and restore controls when possible. */
  1414. list_for_each_entry(entity, &dev->entities, list) {
  1415. for (i = 0; i < entity->ncontrols; ++i) {
  1416. ctrl = &entity->controls[i];
  1417. if (!ctrl->initialized || !ctrl->modified ||
  1418. (ctrl->info.flags & UVC_CTRL_FLAG_RESTORE) == 0)
  1419. continue;
  1420. printk(KERN_INFO "restoring control %pUl/%u/%u\n",
  1421. ctrl->info.entity, ctrl->info.index,
  1422. ctrl->info.selector);
  1423. ctrl->dirty = 1;
  1424. }
  1425. ret = uvc_ctrl_commit_entity(dev, entity, 0);
  1426. if (ret < 0)
  1427. return ret;
  1428. }
  1429. return 0;
  1430. }
  1431. /* --------------------------------------------------------------------------
  1432. * Control and mapping handling
  1433. */
  1434. /*
  1435. * Add control information to a given control.
  1436. */
  1437. static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
  1438. const struct uvc_control_info *info)
  1439. {
  1440. int ret = 0;
  1441. memcpy(&ctrl->info, info, sizeof(*info));
  1442. INIT_LIST_HEAD(&ctrl->info.mappings);
  1443. /* Allocate an array to save control values (cur, def, max, etc.) */
  1444. ctrl->uvc_data = kzalloc(ctrl->info.size * UVC_CTRL_DATA_LAST + 1,
  1445. GFP_KERNEL);
  1446. if (ctrl->uvc_data == NULL) {
  1447. ret = -ENOMEM;
  1448. goto done;
  1449. }
  1450. ctrl->initialized = 1;
  1451. uvc_trace(UVC_TRACE_CONTROL, "Added control %pUl/%u to device %s "
  1452. "entity %u\n", ctrl->info.entity, ctrl->info.selector,
  1453. dev->udev->devpath, ctrl->entity->id);
  1454. done:
  1455. if (ret < 0)
  1456. kfree(ctrl->uvc_data);
  1457. return ret;
  1458. }
  1459. /*
  1460. * Add a control mapping to a given control.
  1461. */
  1462. static int __uvc_ctrl_add_mapping(struct uvc_device *dev,
  1463. struct uvc_control *ctrl, const struct uvc_control_mapping *mapping)
  1464. {
  1465. struct uvc_control_mapping *map;
  1466. unsigned int size;
  1467. /* Most mappings come from static kernel data and need to be duplicated.
  1468. * Mappings that come from userspace will be unnecessarily duplicated,
  1469. * this could be optimized.
  1470. */
  1471. map = kmemdup(mapping, sizeof(*mapping), GFP_KERNEL);
  1472. if (map == NULL)
  1473. return -ENOMEM;
  1474. size = sizeof(*mapping->menu_info) * mapping->menu_count;
  1475. map->menu_info = kmemdup(mapping->menu_info, size, GFP_KERNEL);
  1476. if (map->menu_info == NULL) {
  1477. kfree(map);
  1478. return -ENOMEM;
  1479. }
  1480. if (map->get == NULL)
  1481. map->get = uvc_get_le_value;
  1482. if (map->set == NULL)
  1483. map->set = uvc_set_le_value;
  1484. map->ctrl = &ctrl->info;
  1485. list_add_tail(&map->list, &ctrl->info.mappings);
  1486. uvc_trace(UVC_TRACE_CONTROL,
  1487. "Adding mapping '%s' to control %pUl/%u.\n",
  1488. map->name, ctrl->info.entity, ctrl->info.selector);
  1489. return 0;
  1490. }
  1491. int uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
  1492. const struct uvc_control_mapping *mapping)
  1493. {
  1494. struct uvc_device *dev = chain->dev;
  1495. struct uvc_control_mapping *map;
  1496. struct uvc_entity *entity;
  1497. struct uvc_control *ctrl;
  1498. int found = 0;
  1499. int ret;
  1500. if (mapping->id & ~V4L2_CTRL_ID_MASK) {
  1501. uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', control "
  1502. "id 0x%08x is invalid.\n", mapping->name,
  1503. mapping->id);
  1504. return -EINVAL;
  1505. }
  1506. /* Search for the matching (GUID/CS) control on the current chain */
  1507. list_for_each_entry(entity, &chain->entities, chain) {
  1508. unsigned int i;
  1509. if (UVC_ENTITY_TYPE(entity) != UVC_VC_EXTENSION_UNIT ||
  1510. !uvc_entity_match_guid(entity, mapping->entity))
  1511. continue;
  1512. for (i = 0; i < entity->ncontrols; ++i) {
  1513. ctrl = &entity->controls[i];
  1514. if (ctrl->index == mapping->selector - 1) {
  1515. found = 1;
  1516. break;
  1517. }
  1518. }
  1519. if (found)
  1520. break;
  1521. }
  1522. if (!found)
  1523. return -ENOENT;
  1524. if (mutex_lock_interruptible(&chain->ctrl_mutex))
  1525. return -ERESTARTSYS;
  1526. /* Perform delayed initialization of XU controls */
  1527. ret = uvc_ctrl_init_xu_ctrl(dev, ctrl);
  1528. if (ret < 0) {
  1529. ret = -ENOENT;
  1530. goto done;
  1531. }
  1532. list_for_each_entry(map, &ctrl->info.mappings, list) {
  1533. if (mapping->id == map->id) {
  1534. uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', "
  1535. "control id 0x%08x already exists.\n",
  1536. mapping->name, mapping->id);
  1537. ret = -EEXIST;
  1538. goto done;
  1539. }
  1540. }
  1541. /* Prevent excess memory consumption */
  1542. if (atomic_inc_return(&dev->nmappings) > UVC_MAX_CONTROL_MAPPINGS) {
  1543. atomic_dec(&dev->nmappings);
  1544. uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', maximum "
  1545. "mappings count (%u) exceeded.\n", mapping->name,
  1546. UVC_MAX_CONTROL_MAPPINGS);
  1547. ret = -ENOMEM;
  1548. goto done;
  1549. }
  1550. ret = __uvc_ctrl_add_mapping(dev, ctrl, mapping);
  1551. if (ret < 0)
  1552. atomic_dec(&dev->nmappings);
  1553. done:
  1554. mutex_unlock(&chain->ctrl_mutex);
  1555. return ret;
  1556. }
  1557. /*
  1558. * Prune an entity of its bogus controls using a blacklist. Bogus controls
  1559. * are currently the ones that crash the camera or unconditionally return an
  1560. * error when queried.
  1561. */
  1562. static void uvc_ctrl_prune_entity(struct uvc_device *dev,
  1563. struct uvc_entity *entity)
  1564. {
  1565. struct uvc_ctrl_blacklist {
  1566. struct usb_device_id id;
  1567. u8 index;
  1568. };
  1569. static const struct uvc_ctrl_blacklist processing_blacklist[] = {
  1570. { { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */
  1571. { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
  1572. { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
  1573. };
  1574. static const struct uvc_ctrl_blacklist camera_blacklist[] = {
  1575. { { USB_DEVICE(0x06f8, 0x3005) }, 9 }, /* Zoom, Absolute */
  1576. };
  1577. const struct uvc_ctrl_blacklist *blacklist;
  1578. unsigned int size;
  1579. unsigned int count;
  1580. unsigned int i;
  1581. u8 *controls;
  1582. switch (UVC_ENTITY_TYPE(entity)) {
  1583. case UVC_VC_PROCESSING_UNIT:
  1584. blacklist = processing_blacklist;
  1585. count = ARRAY_SIZE(processing_blacklist);
  1586. controls = entity->processing.bmControls;
  1587. size = entity->processing.bControlSize;
  1588. break;
  1589. case UVC_ITT_CAMERA:
  1590. blacklist = camera_blacklist;
  1591. count = ARRAY_SIZE(camera_blacklist);
  1592. controls = entity->camera.bmControls;
  1593. size = entity->camera.bControlSize;
  1594. break;
  1595. default:
  1596. return;
  1597. }
  1598. for (i = 0; i < count; ++i) {
  1599. if (!usb_match_one_id(dev->intf, &blacklist[i].id))
  1600. continue;
  1601. if (blacklist[i].index >= 8 * size ||
  1602. !uvc_test_bit(controls, blacklist[i].index))
  1603. continue;
  1604. uvc_trace(UVC_TRACE_CONTROL, "%u/%u control is black listed, "
  1605. "removing it.\n", entity->id, blacklist[i].index);
  1606. uvc_clear_bit(controls, blacklist[i].index);
  1607. }
  1608. }
  1609. /*
  1610. * Add control information and hardcoded stock control mappings to the given
  1611. * device.
  1612. */
  1613. static void uvc_ctrl_init_ctrl(struct uvc_device *dev, struct uvc_control *ctrl)
  1614. {
  1615. const struct uvc_control_info *info = uvc_ctrls;
  1616. const struct uvc_control_info *iend = info + ARRAY_SIZE(uvc_ctrls);
  1617. const struct uvc_control_mapping *mapping = uvc_ctrl_mappings;
  1618. const struct uvc_control_mapping *mend =
  1619. mapping + ARRAY_SIZE(uvc_ctrl_mappings);
  1620. /* XU controls initialization requires querying the device for control
  1621. * information. As some buggy UVC devices will crash when queried
  1622. * repeatedly in a tight loop, delay XU controls initialization until
  1623. * first use.
  1624. */
  1625. if (UVC_ENTITY_TYPE(ctrl->entity) == UVC_VC_EXTENSION_UNIT)
  1626. return;
  1627. for (; info < iend; ++info) {
  1628. if (uvc_entity_match_guid(ctrl->entity, info->entity) &&
  1629. ctrl->index == info->index) {
  1630. uvc_ctrl_add_info(dev, ctrl, info);
  1631. break;
  1632. }
  1633. }
  1634. if (!ctrl->initialized)
  1635. return;
  1636. for (; mapping < mend; ++mapping) {
  1637. if (uvc_entity_match_guid(ctrl->entity, mapping->entity) &&
  1638. ctrl->info.selector == mapping->selector)
  1639. __uvc_ctrl_add_mapping(dev, ctrl, mapping);
  1640. }
  1641. }
  1642. /*
  1643. * Initialize device controls.
  1644. */
  1645. int uvc_ctrl_init_device(struct uvc_device *dev)
  1646. {
  1647. struct uvc_entity *entity;
  1648. unsigned int i;
  1649. /* Walk the entities list and instantiate controls */
  1650. list_for_each_entry(entity, &dev->entities, list) {
  1651. struct uvc_control *ctrl;
  1652. unsigned int bControlSize = 0, ncontrols = 0;
  1653. __u8 *bmControls = NULL;
  1654. if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
  1655. bmControls = entity->extension.bmControls;
  1656. bControlSize = entity->extension.bControlSize;
  1657. } else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) {
  1658. bmControls = entity->processing.bmControls;
  1659. bControlSize = entity->processing.bControlSize;
  1660. } else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
  1661. bmControls = entity->camera.bmControls;
  1662. bControlSize = entity->camera.bControlSize;
  1663. }
  1664. /* Remove bogus/blacklisted controls */
  1665. uvc_ctrl_prune_entity(dev, entity);
  1666. /* Count supported controls and allocate the controls array */
  1667. for (i = 0; i < bControlSize; ++i)
  1668. ncontrols += hweight8(bmControls[i]);
  1669. if (ncontrols == 0)
  1670. continue;
  1671. entity->controls = kcalloc(ncontrols, sizeof(*ctrl),
  1672. GFP_KERNEL);
  1673. if (entity->controls == NULL)
  1674. return -ENOMEM;
  1675. entity->ncontrols = ncontrols;
  1676. /* Initialize all supported controls */
  1677. ctrl = entity->controls;
  1678. for (i = 0; i < bControlSize * 8; ++i) {
  1679. if (uvc_test_bit(bmControls, i) == 0)
  1680. continue;
  1681. ctrl->entity = entity;
  1682. ctrl->index = i;
  1683. uvc_ctrl_init_ctrl(dev, ctrl);
  1684. ctrl++;
  1685. }
  1686. }
  1687. return 0;
  1688. }
  1689. /*
  1690. * Cleanup device controls.
  1691. */
  1692. static void uvc_ctrl_cleanup_mappings(struct uvc_device *dev,
  1693. struct uvc_control *ctrl)
  1694. {
  1695. struct uvc_control_mapping *mapping, *nm;
  1696. list_for_each_entry_safe(mapping, nm, &ctrl->info.mappings, list) {
  1697. list_del(&mapping->list);
  1698. kfree(mapping->menu_info);
  1699. kfree(mapping);
  1700. }
  1701. }
  1702. void uvc_ctrl_cleanup_device(struct uvc_device *dev)
  1703. {
  1704. struct uvc_entity *entity;
  1705. unsigned int i;
  1706. /* Free controls and control mappings for all entities. */
  1707. list_for_each_entry(entity, &dev->entities, list) {
  1708. for (i = 0; i < entity->ncontrols; ++i) {
  1709. struct uvc_control *ctrl = &entity->controls[i];
  1710. if (!ctrl->initialized)
  1711. continue;
  1712. uvc_ctrl_cleanup_mappings(dev, ctrl);
  1713. kfree(ctrl->uvc_data);
  1714. }
  1715. kfree(entity->controls);
  1716. }
  1717. }