uvc_ctrl.c 55 KB

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