uvc_ctrl.c 40 KB

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