uvc_ctrl.c 40 KB

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