uvc_ctrl.c 36 KB

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