uvc_ctrl.c 47 KB

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