uvc_ctrl.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166
  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 -EINVAL;
  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 = -EINVAL;
  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 || mapping->v4l2_type != V4L2_CTRL_TYPE_MENU) {
  1022. ret = -EINVAL;
  1023. goto done;
  1024. }
  1025. if (query_menu->index >= mapping->menu_count) {
  1026. ret = -EINVAL;
  1027. goto done;
  1028. }
  1029. menu_info = &mapping->menu_info[query_menu->index];
  1030. if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK &&
  1031. (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)) {
  1032. s32 bitmap;
  1033. if (!ctrl->cached) {
  1034. ret = uvc_ctrl_populate_cache(chain, ctrl);
  1035. if (ret < 0)
  1036. goto done;
  1037. }
  1038. bitmap = mapping->get(mapping, UVC_GET_RES,
  1039. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
  1040. if (!(bitmap & menu_info->value)) {
  1041. ret = -EINVAL;
  1042. goto done;
  1043. }
  1044. }
  1045. strlcpy(query_menu->name, menu_info->name, sizeof query_menu->name);
  1046. done:
  1047. mutex_unlock(&chain->ctrl_mutex);
  1048. return ret;
  1049. }
  1050. /* --------------------------------------------------------------------------
  1051. * Ctrl event handling
  1052. */
  1053. static void uvc_ctrl_fill_event(struct uvc_video_chain *chain,
  1054. struct v4l2_event *ev,
  1055. struct uvc_control *ctrl,
  1056. struct uvc_control_mapping *mapping,
  1057. s32 value, u32 changes)
  1058. {
  1059. struct v4l2_queryctrl v4l2_ctrl;
  1060. __uvc_query_v4l2_ctrl(chain, ctrl, mapping, &v4l2_ctrl);
  1061. memset(ev->reserved, 0, sizeof(ev->reserved));
  1062. ev->type = V4L2_EVENT_CTRL;
  1063. ev->id = v4l2_ctrl.id;
  1064. ev->u.ctrl.value = value;
  1065. ev->u.ctrl.changes = changes;
  1066. ev->u.ctrl.type = v4l2_ctrl.type;
  1067. ev->u.ctrl.flags = v4l2_ctrl.flags;
  1068. ev->u.ctrl.minimum = v4l2_ctrl.minimum;
  1069. ev->u.ctrl.maximum = v4l2_ctrl.maximum;
  1070. ev->u.ctrl.step = v4l2_ctrl.step;
  1071. ev->u.ctrl.default_value = v4l2_ctrl.default_value;
  1072. }
  1073. static void uvc_ctrl_send_event(struct uvc_fh *handle,
  1074. struct uvc_control *ctrl, struct uvc_control_mapping *mapping,
  1075. s32 value, u32 changes)
  1076. {
  1077. struct v4l2_subscribed_event *sev;
  1078. struct v4l2_event ev;
  1079. if (list_empty(&mapping->ev_subs))
  1080. return;
  1081. uvc_ctrl_fill_event(handle->chain, &ev, ctrl, mapping, value, changes);
  1082. list_for_each_entry(sev, &mapping->ev_subs, node) {
  1083. if (sev->fh && (sev->fh != &handle->vfh ||
  1084. (sev->flags & V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK) ||
  1085. (changes & V4L2_EVENT_CTRL_CH_FLAGS)))
  1086. v4l2_event_queue_fh(sev->fh, &ev);
  1087. }
  1088. }
  1089. static void uvc_ctrl_send_slave_event(struct uvc_fh *handle,
  1090. struct uvc_control *master, u32 slave_id,
  1091. const struct v4l2_ext_control *xctrls, unsigned int xctrls_count)
  1092. {
  1093. struct uvc_control_mapping *mapping = NULL;
  1094. struct uvc_control *ctrl = NULL;
  1095. u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
  1096. unsigned int i;
  1097. s32 val = 0;
  1098. /*
  1099. * We can skip sending an event for the slave if the slave
  1100. * is being modified in the same transaction.
  1101. */
  1102. for (i = 0; i < xctrls_count; i++) {
  1103. if (xctrls[i].id == slave_id)
  1104. return;
  1105. }
  1106. __uvc_find_control(master->entity, slave_id, &mapping, &ctrl, 0);
  1107. if (ctrl == NULL)
  1108. return;
  1109. if (__uvc_ctrl_get(handle->chain, ctrl, mapping, &val) == 0)
  1110. changes |= V4L2_EVENT_CTRL_CH_VALUE;
  1111. uvc_ctrl_send_event(handle, ctrl, mapping, val, changes);
  1112. }
  1113. static void uvc_ctrl_send_events(struct uvc_fh *handle,
  1114. const struct v4l2_ext_control *xctrls, unsigned int xctrls_count)
  1115. {
  1116. struct uvc_control_mapping *mapping;
  1117. struct uvc_control *ctrl;
  1118. u32 changes = V4L2_EVENT_CTRL_CH_VALUE;
  1119. unsigned int i;
  1120. unsigned int j;
  1121. for (i = 0; i < xctrls_count; ++i) {
  1122. ctrl = uvc_find_control(handle->chain, xctrls[i].id, &mapping);
  1123. for (j = 0; j < ARRAY_SIZE(mapping->slave_ids); ++j) {
  1124. if (!mapping->slave_ids[j])
  1125. break;
  1126. uvc_ctrl_send_slave_event(handle, ctrl,
  1127. mapping->slave_ids[j],
  1128. xctrls, xctrls_count);
  1129. }
  1130. /*
  1131. * If the master is being modified in the same transaction
  1132. * flags may change too.
  1133. */
  1134. if (mapping->master_id) {
  1135. for (j = 0; j < xctrls_count; j++) {
  1136. if (xctrls[j].id == mapping->master_id) {
  1137. changes |= V4L2_EVENT_CTRL_CH_FLAGS;
  1138. break;
  1139. }
  1140. }
  1141. }
  1142. uvc_ctrl_send_event(handle, ctrl, mapping, xctrls[i].value,
  1143. changes);
  1144. }
  1145. }
  1146. static int uvc_ctrl_add_event(struct v4l2_subscribed_event *sev, unsigned elems)
  1147. {
  1148. struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);
  1149. struct uvc_control_mapping *mapping;
  1150. struct uvc_control *ctrl;
  1151. int ret;
  1152. ret = mutex_lock_interruptible(&handle->chain->ctrl_mutex);
  1153. if (ret < 0)
  1154. return -ERESTARTSYS;
  1155. ctrl = uvc_find_control(handle->chain, sev->id, &mapping);
  1156. if (ctrl == NULL) {
  1157. ret = -EINVAL;
  1158. goto done;
  1159. }
  1160. list_add_tail(&sev->node, &mapping->ev_subs);
  1161. if (sev->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL) {
  1162. struct v4l2_event ev;
  1163. u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
  1164. s32 val = 0;
  1165. if (__uvc_ctrl_get(handle->chain, ctrl, mapping, &val) == 0)
  1166. changes |= V4L2_EVENT_CTRL_CH_VALUE;
  1167. uvc_ctrl_fill_event(handle->chain, &ev, ctrl, mapping, val,
  1168. changes);
  1169. /* Mark the queue as active, allowing this initial
  1170. event to be accepted. */
  1171. sev->elems = elems;
  1172. v4l2_event_queue_fh(sev->fh, &ev);
  1173. }
  1174. done:
  1175. mutex_unlock(&handle->chain->ctrl_mutex);
  1176. return ret;
  1177. }
  1178. static void uvc_ctrl_del_event(struct v4l2_subscribed_event *sev)
  1179. {
  1180. struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);
  1181. mutex_lock(&handle->chain->ctrl_mutex);
  1182. list_del(&sev->node);
  1183. mutex_unlock(&handle->chain->ctrl_mutex);
  1184. }
  1185. const struct v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops = {
  1186. .add = uvc_ctrl_add_event,
  1187. .del = uvc_ctrl_del_event,
  1188. .replace = v4l2_ctrl_replace,
  1189. .merge = v4l2_ctrl_merge,
  1190. };
  1191. /* --------------------------------------------------------------------------
  1192. * Control transactions
  1193. *
  1194. * To make extended set operations as atomic as the hardware allows, controls
  1195. * are handled using begin/commit/rollback operations.
  1196. *
  1197. * At the beginning of a set request, uvc_ctrl_begin should be called to
  1198. * initialize the request. This function acquires the control lock.
  1199. *
  1200. * When setting a control, the new value is stored in the control data field
  1201. * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
  1202. * later processing. If the UVC and V4L2 control sizes differ, the current
  1203. * value is loaded from the hardware before storing the new value in the data
  1204. * field.
  1205. *
  1206. * After processing all controls in the transaction, uvc_ctrl_commit or
  1207. * uvc_ctrl_rollback must be called to apply the pending changes to the
  1208. * hardware or revert them. When applying changes, all controls marked as
  1209. * dirty will be modified in the UVC device, and the dirty flag will be
  1210. * cleared. When reverting controls, the control data field
  1211. * UVC_CTRL_DATA_CURRENT is reverted to its previous value
  1212. * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
  1213. * control lock.
  1214. */
  1215. int uvc_ctrl_begin(struct uvc_video_chain *chain)
  1216. {
  1217. return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0;
  1218. }
  1219. static int uvc_ctrl_commit_entity(struct uvc_device *dev,
  1220. struct uvc_entity *entity, int rollback)
  1221. {
  1222. struct uvc_control *ctrl;
  1223. unsigned int i;
  1224. int ret;
  1225. if (entity == NULL)
  1226. return 0;
  1227. for (i = 0; i < entity->ncontrols; ++i) {
  1228. ctrl = &entity->controls[i];
  1229. if (!ctrl->initialized)
  1230. continue;
  1231. /* Reset the loaded flag for auto-update controls that were
  1232. * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
  1233. * uvc_ctrl_get from using the cached value, and for write-only
  1234. * controls to prevent uvc_ctrl_set from setting bits not
  1235. * explicitly set by the user.
  1236. */
  1237. if (ctrl->info.flags & UVC_CTRL_FLAG_AUTO_UPDATE ||
  1238. !(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
  1239. ctrl->loaded = 0;
  1240. if (!ctrl->dirty)
  1241. continue;
  1242. if (!rollback)
  1243. ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id,
  1244. dev->intfnum, ctrl->info.selector,
  1245. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  1246. ctrl->info.size);
  1247. else
  1248. ret = 0;
  1249. if (rollback || ret < 0)
  1250. memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  1251. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
  1252. ctrl->info.size);
  1253. ctrl->dirty = 0;
  1254. if (ret < 0)
  1255. return ret;
  1256. }
  1257. return 0;
  1258. }
  1259. int __uvc_ctrl_commit(struct uvc_fh *handle, int rollback,
  1260. const struct v4l2_ext_control *xctrls,
  1261. unsigned int xctrls_count)
  1262. {
  1263. struct uvc_video_chain *chain = handle->chain;
  1264. struct uvc_entity *entity;
  1265. int ret = 0;
  1266. /* Find the control. */
  1267. list_for_each_entry(entity, &chain->entities, chain) {
  1268. ret = uvc_ctrl_commit_entity(chain->dev, entity, rollback);
  1269. if (ret < 0)
  1270. goto done;
  1271. }
  1272. if (!rollback)
  1273. uvc_ctrl_send_events(handle, xctrls, xctrls_count);
  1274. done:
  1275. mutex_unlock(&chain->ctrl_mutex);
  1276. return ret;
  1277. }
  1278. int uvc_ctrl_get(struct uvc_video_chain *chain,
  1279. struct v4l2_ext_control *xctrl)
  1280. {
  1281. struct uvc_control *ctrl;
  1282. struct uvc_control_mapping *mapping;
  1283. ctrl = uvc_find_control(chain, xctrl->id, &mapping);
  1284. if (ctrl == NULL)
  1285. return -EINVAL;
  1286. return __uvc_ctrl_get(chain, ctrl, mapping, &xctrl->value);
  1287. }
  1288. int uvc_ctrl_set(struct uvc_video_chain *chain,
  1289. struct v4l2_ext_control *xctrl)
  1290. {
  1291. struct uvc_control *ctrl;
  1292. struct uvc_control_mapping *mapping;
  1293. s32 value;
  1294. u32 step;
  1295. s32 min;
  1296. s32 max;
  1297. int ret;
  1298. ctrl = uvc_find_control(chain, xctrl->id, &mapping);
  1299. if (ctrl == NULL || (ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR) == 0)
  1300. return -EINVAL;
  1301. /* Clamp out of range values. */
  1302. switch (mapping->v4l2_type) {
  1303. case V4L2_CTRL_TYPE_INTEGER:
  1304. if (!ctrl->cached) {
  1305. ret = uvc_ctrl_populate_cache(chain, ctrl);
  1306. if (ret < 0)
  1307. return ret;
  1308. }
  1309. min = mapping->get(mapping, UVC_GET_MIN,
  1310. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
  1311. max = mapping->get(mapping, UVC_GET_MAX,
  1312. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
  1313. step = mapping->get(mapping, UVC_GET_RES,
  1314. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
  1315. if (step == 0)
  1316. step = 1;
  1317. xctrl->value = min + (xctrl->value - min + step/2) / step * step;
  1318. xctrl->value = clamp(xctrl->value, min, max);
  1319. value = xctrl->value;
  1320. break;
  1321. case V4L2_CTRL_TYPE_BOOLEAN:
  1322. xctrl->value = clamp(xctrl->value, 0, 1);
  1323. value = xctrl->value;
  1324. break;
  1325. case V4L2_CTRL_TYPE_MENU:
  1326. if (xctrl->value < 0 || xctrl->value >= mapping->menu_count)
  1327. return -ERANGE;
  1328. value = mapping->menu_info[xctrl->value].value;
  1329. /* Valid menu indices are reported by the GET_RES request for
  1330. * UVC controls that support it.
  1331. */
  1332. if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK &&
  1333. (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)) {
  1334. if (!ctrl->cached) {
  1335. ret = uvc_ctrl_populate_cache(chain, ctrl);
  1336. if (ret < 0)
  1337. return ret;
  1338. }
  1339. step = mapping->get(mapping, UVC_GET_RES,
  1340. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
  1341. if (!(step & value))
  1342. return -ERANGE;
  1343. }
  1344. break;
  1345. default:
  1346. value = xctrl->value;
  1347. break;
  1348. }
  1349. /* If the mapping doesn't span the whole UVC control, the current value
  1350. * needs to be loaded from the device to perform the read-modify-write
  1351. * operation.
  1352. */
  1353. if (!ctrl->loaded && (ctrl->info.size * 8) != mapping->size) {
  1354. if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0) {
  1355. memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  1356. 0, ctrl->info.size);
  1357. } else {
  1358. ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
  1359. ctrl->entity->id, chain->dev->intfnum,
  1360. ctrl->info.selector,
  1361. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  1362. ctrl->info.size);
  1363. if (ret < 0)
  1364. return ret;
  1365. }
  1366. ctrl->loaded = 1;
  1367. }
  1368. /* Backup the current value in case we need to rollback later. */
  1369. if (!ctrl->dirty) {
  1370. memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
  1371. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  1372. ctrl->info.size);
  1373. }
  1374. mapping->set(mapping, value,
  1375. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
  1376. ctrl->dirty = 1;
  1377. ctrl->modified = 1;
  1378. return 0;
  1379. }
  1380. /* --------------------------------------------------------------------------
  1381. * Dynamic controls
  1382. */
  1383. static void uvc_ctrl_fixup_xu_info(struct uvc_device *dev,
  1384. const struct uvc_control *ctrl, struct uvc_control_info *info)
  1385. {
  1386. struct uvc_ctrl_fixup {
  1387. struct usb_device_id id;
  1388. u8 entity;
  1389. u8 selector;
  1390. u8 flags;
  1391. };
  1392. static const struct uvc_ctrl_fixup fixups[] = {
  1393. { { USB_DEVICE(0x046d, 0x08c2) }, 9, 1,
  1394. UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
  1395. UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
  1396. UVC_CTRL_FLAG_AUTO_UPDATE },
  1397. { { USB_DEVICE(0x046d, 0x08cc) }, 9, 1,
  1398. UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
  1399. UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
  1400. UVC_CTRL_FLAG_AUTO_UPDATE },
  1401. { { USB_DEVICE(0x046d, 0x0994) }, 9, 1,
  1402. UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
  1403. UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
  1404. UVC_CTRL_FLAG_AUTO_UPDATE },
  1405. };
  1406. unsigned int i;
  1407. for (i = 0; i < ARRAY_SIZE(fixups); ++i) {
  1408. if (!usb_match_one_id(dev->intf, &fixups[i].id))
  1409. continue;
  1410. if (fixups[i].entity == ctrl->entity->id &&
  1411. fixups[i].selector == info->selector) {
  1412. info->flags = fixups[i].flags;
  1413. return;
  1414. }
  1415. }
  1416. }
  1417. /*
  1418. * Query control information (size and flags) for XU controls.
  1419. */
  1420. static int uvc_ctrl_fill_xu_info(struct uvc_device *dev,
  1421. const struct uvc_control *ctrl, struct uvc_control_info *info)
  1422. {
  1423. u8 *data;
  1424. int ret;
  1425. data = kmalloc(2, GFP_KERNEL);
  1426. if (data == NULL)
  1427. return -ENOMEM;
  1428. memcpy(info->entity, ctrl->entity->extension.guidExtensionCode,
  1429. sizeof(info->entity));
  1430. info->index = ctrl->index;
  1431. info->selector = ctrl->index + 1;
  1432. /* Query and verify the control length (GET_LEN) */
  1433. ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id, dev->intfnum,
  1434. info->selector, data, 2);
  1435. if (ret < 0) {
  1436. uvc_trace(UVC_TRACE_CONTROL,
  1437. "GET_LEN failed on control %pUl/%u (%d).\n",
  1438. info->entity, info->selector, ret);
  1439. goto done;
  1440. }
  1441. info->size = le16_to_cpup((__le16 *)data);
  1442. /* Query the control information (GET_INFO) */
  1443. ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id, dev->intfnum,
  1444. info->selector, data, 1);
  1445. if (ret < 0) {
  1446. uvc_trace(UVC_TRACE_CONTROL,
  1447. "GET_INFO failed on control %pUl/%u (%d).\n",
  1448. info->entity, info->selector, ret);
  1449. goto done;
  1450. }
  1451. info->flags = UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX
  1452. | UVC_CTRL_FLAG_GET_RES | UVC_CTRL_FLAG_GET_DEF
  1453. | (data[0] & UVC_CONTROL_CAP_GET ?
  1454. UVC_CTRL_FLAG_GET_CUR : 0)
  1455. | (data[0] & UVC_CONTROL_CAP_SET ?
  1456. UVC_CTRL_FLAG_SET_CUR : 0)
  1457. | (data[0] & UVC_CONTROL_CAP_AUTOUPDATE ?
  1458. UVC_CTRL_FLAG_AUTO_UPDATE : 0);
  1459. uvc_ctrl_fixup_xu_info(dev, ctrl, info);
  1460. uvc_trace(UVC_TRACE_CONTROL, "XU control %pUl/%u queried: len %u, "
  1461. "flags { get %u set %u auto %u }.\n",
  1462. info->entity, info->selector, info->size,
  1463. (info->flags & UVC_CTRL_FLAG_GET_CUR) ? 1 : 0,
  1464. (info->flags & UVC_CTRL_FLAG_SET_CUR) ? 1 : 0,
  1465. (info->flags & UVC_CTRL_FLAG_AUTO_UPDATE) ? 1 : 0);
  1466. done:
  1467. kfree(data);
  1468. return ret;
  1469. }
  1470. static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
  1471. const struct uvc_control_info *info);
  1472. static int uvc_ctrl_init_xu_ctrl(struct uvc_device *dev,
  1473. struct uvc_control *ctrl)
  1474. {
  1475. struct uvc_control_info info;
  1476. int ret;
  1477. if (ctrl->initialized)
  1478. return 0;
  1479. ret = uvc_ctrl_fill_xu_info(dev, ctrl, &info);
  1480. if (ret < 0)
  1481. return ret;
  1482. ret = uvc_ctrl_add_info(dev, ctrl, &info);
  1483. if (ret < 0)
  1484. uvc_trace(UVC_TRACE_CONTROL, "Failed to initialize control "
  1485. "%pUl/%u on device %s entity %u\n", info.entity,
  1486. info.selector, dev->udev->devpath, ctrl->entity->id);
  1487. return ret;
  1488. }
  1489. int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
  1490. struct uvc_xu_control_query *xqry)
  1491. {
  1492. struct uvc_entity *entity;
  1493. struct uvc_control *ctrl;
  1494. unsigned int i, found = 0;
  1495. __u32 reqflags;
  1496. __u16 size;
  1497. __u8 *data = NULL;
  1498. int ret;
  1499. /* Find the extension unit. */
  1500. list_for_each_entry(entity, &chain->entities, chain) {
  1501. if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT &&
  1502. entity->id == xqry->unit)
  1503. break;
  1504. }
  1505. if (entity->id != xqry->unit) {
  1506. uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
  1507. xqry->unit);
  1508. return -ENOENT;
  1509. }
  1510. /* Find the control and perform delayed initialization if needed. */
  1511. for (i = 0; i < entity->ncontrols; ++i) {
  1512. ctrl = &entity->controls[i];
  1513. if (ctrl->index == xqry->selector - 1) {
  1514. found = 1;
  1515. break;
  1516. }
  1517. }
  1518. if (!found) {
  1519. uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u not found.\n",
  1520. entity->extension.guidExtensionCode, xqry->selector);
  1521. return -ENOENT;
  1522. }
  1523. if (mutex_lock_interruptible(&chain->ctrl_mutex))
  1524. return -ERESTARTSYS;
  1525. ret = uvc_ctrl_init_xu_ctrl(chain->dev, ctrl);
  1526. if (ret < 0) {
  1527. ret = -ENOENT;
  1528. goto done;
  1529. }
  1530. /* Validate the required buffer size and flags for the request */
  1531. reqflags = 0;
  1532. size = ctrl->info.size;
  1533. switch (xqry->query) {
  1534. case UVC_GET_CUR:
  1535. reqflags = UVC_CTRL_FLAG_GET_CUR;
  1536. break;
  1537. case UVC_GET_MIN:
  1538. reqflags = UVC_CTRL_FLAG_GET_MIN;
  1539. break;
  1540. case UVC_GET_MAX:
  1541. reqflags = UVC_CTRL_FLAG_GET_MAX;
  1542. break;
  1543. case UVC_GET_DEF:
  1544. reqflags = UVC_CTRL_FLAG_GET_DEF;
  1545. break;
  1546. case UVC_GET_RES:
  1547. reqflags = UVC_CTRL_FLAG_GET_RES;
  1548. break;
  1549. case UVC_SET_CUR:
  1550. reqflags = UVC_CTRL_FLAG_SET_CUR;
  1551. break;
  1552. case UVC_GET_LEN:
  1553. size = 2;
  1554. break;
  1555. case UVC_GET_INFO:
  1556. size = 1;
  1557. break;
  1558. default:
  1559. ret = -EINVAL;
  1560. goto done;
  1561. }
  1562. if (size != xqry->size) {
  1563. ret = -ENOBUFS;
  1564. goto done;
  1565. }
  1566. if (reqflags && !(ctrl->info.flags & reqflags)) {
  1567. ret = -EBADRQC;
  1568. goto done;
  1569. }
  1570. data = kmalloc(size, GFP_KERNEL);
  1571. if (data == NULL) {
  1572. ret = -ENOMEM;
  1573. goto done;
  1574. }
  1575. if (xqry->query == UVC_SET_CUR &&
  1576. copy_from_user(data, xqry->data, size)) {
  1577. ret = -EFAULT;
  1578. goto done;
  1579. }
  1580. ret = uvc_query_ctrl(chain->dev, xqry->query, xqry->unit,
  1581. chain->dev->intfnum, xqry->selector, data, size);
  1582. if (ret < 0)
  1583. goto done;
  1584. if (xqry->query != UVC_SET_CUR &&
  1585. copy_to_user(xqry->data, data, size))
  1586. ret = -EFAULT;
  1587. done:
  1588. kfree(data);
  1589. mutex_unlock(&chain->ctrl_mutex);
  1590. return ret;
  1591. }
  1592. /* --------------------------------------------------------------------------
  1593. * Suspend/resume
  1594. */
  1595. /*
  1596. * Restore control values after resume, skipping controls that haven't been
  1597. * changed.
  1598. *
  1599. * TODO
  1600. * - Don't restore modified controls that are back to their default value.
  1601. * - Handle restore order (Auto-Exposure Mode should be restored before
  1602. * Exposure Time).
  1603. */
  1604. int uvc_ctrl_resume_device(struct uvc_device *dev)
  1605. {
  1606. struct uvc_control *ctrl;
  1607. struct uvc_entity *entity;
  1608. unsigned int i;
  1609. int ret;
  1610. /* Walk the entities list and restore controls when possible. */
  1611. list_for_each_entry(entity, &dev->entities, list) {
  1612. for (i = 0; i < entity->ncontrols; ++i) {
  1613. ctrl = &entity->controls[i];
  1614. if (!ctrl->initialized || !ctrl->modified ||
  1615. (ctrl->info.flags & UVC_CTRL_FLAG_RESTORE) == 0)
  1616. continue;
  1617. printk(KERN_INFO "restoring control %pUl/%u/%u\n",
  1618. ctrl->info.entity, ctrl->info.index,
  1619. ctrl->info.selector);
  1620. ctrl->dirty = 1;
  1621. }
  1622. ret = uvc_ctrl_commit_entity(dev, entity, 0);
  1623. if (ret < 0)
  1624. return ret;
  1625. }
  1626. return 0;
  1627. }
  1628. /* --------------------------------------------------------------------------
  1629. * Control and mapping handling
  1630. */
  1631. /*
  1632. * Add control information to a given control.
  1633. */
  1634. static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
  1635. const struct uvc_control_info *info)
  1636. {
  1637. int ret = 0;
  1638. memcpy(&ctrl->info, info, sizeof(*info));
  1639. INIT_LIST_HEAD(&ctrl->info.mappings);
  1640. /* Allocate an array to save control values (cur, def, max, etc.) */
  1641. ctrl->uvc_data = kzalloc(ctrl->info.size * UVC_CTRL_DATA_LAST + 1,
  1642. GFP_KERNEL);
  1643. if (ctrl->uvc_data == NULL) {
  1644. ret = -ENOMEM;
  1645. goto done;
  1646. }
  1647. ctrl->initialized = 1;
  1648. uvc_trace(UVC_TRACE_CONTROL, "Added control %pUl/%u to device %s "
  1649. "entity %u\n", ctrl->info.entity, ctrl->info.selector,
  1650. dev->udev->devpath, ctrl->entity->id);
  1651. done:
  1652. if (ret < 0)
  1653. kfree(ctrl->uvc_data);
  1654. return ret;
  1655. }
  1656. /*
  1657. * Add a control mapping to a given control.
  1658. */
  1659. static int __uvc_ctrl_add_mapping(struct uvc_device *dev,
  1660. struct uvc_control *ctrl, const struct uvc_control_mapping *mapping)
  1661. {
  1662. struct uvc_control_mapping *map;
  1663. unsigned int size;
  1664. /* Most mappings come from static kernel data and need to be duplicated.
  1665. * Mappings that come from userspace will be unnecessarily duplicated,
  1666. * this could be optimized.
  1667. */
  1668. map = kmemdup(mapping, sizeof(*mapping), GFP_KERNEL);
  1669. if (map == NULL)
  1670. return -ENOMEM;
  1671. INIT_LIST_HEAD(&map->ev_subs);
  1672. size = sizeof(*mapping->menu_info) * mapping->menu_count;
  1673. map->menu_info = kmemdup(mapping->menu_info, size, GFP_KERNEL);
  1674. if (map->menu_info == NULL) {
  1675. kfree(map);
  1676. return -ENOMEM;
  1677. }
  1678. if (map->get == NULL)
  1679. map->get = uvc_get_le_value;
  1680. if (map->set == NULL)
  1681. map->set = uvc_set_le_value;
  1682. list_add_tail(&map->list, &ctrl->info.mappings);
  1683. uvc_trace(UVC_TRACE_CONTROL,
  1684. "Adding mapping '%s' to control %pUl/%u.\n",
  1685. map->name, ctrl->info.entity, ctrl->info.selector);
  1686. return 0;
  1687. }
  1688. int uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
  1689. const struct uvc_control_mapping *mapping)
  1690. {
  1691. struct uvc_device *dev = chain->dev;
  1692. struct uvc_control_mapping *map;
  1693. struct uvc_entity *entity;
  1694. struct uvc_control *ctrl;
  1695. int found = 0;
  1696. int ret;
  1697. if (mapping->id & ~V4L2_CTRL_ID_MASK) {
  1698. uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', control "
  1699. "id 0x%08x is invalid.\n", mapping->name,
  1700. mapping->id);
  1701. return -EINVAL;
  1702. }
  1703. /* Search for the matching (GUID/CS) control on the current chain */
  1704. list_for_each_entry(entity, &chain->entities, chain) {
  1705. unsigned int i;
  1706. if (UVC_ENTITY_TYPE(entity) != UVC_VC_EXTENSION_UNIT ||
  1707. !uvc_entity_match_guid(entity, mapping->entity))
  1708. continue;
  1709. for (i = 0; i < entity->ncontrols; ++i) {
  1710. ctrl = &entity->controls[i];
  1711. if (ctrl->index == mapping->selector - 1) {
  1712. found = 1;
  1713. break;
  1714. }
  1715. }
  1716. if (found)
  1717. break;
  1718. }
  1719. if (!found)
  1720. return -ENOENT;
  1721. if (mutex_lock_interruptible(&chain->ctrl_mutex))
  1722. return -ERESTARTSYS;
  1723. /* Perform delayed initialization of XU controls */
  1724. ret = uvc_ctrl_init_xu_ctrl(dev, ctrl);
  1725. if (ret < 0) {
  1726. ret = -ENOENT;
  1727. goto done;
  1728. }
  1729. list_for_each_entry(map, &ctrl->info.mappings, list) {
  1730. if (mapping->id == map->id) {
  1731. uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', "
  1732. "control id 0x%08x already exists.\n",
  1733. mapping->name, mapping->id);
  1734. ret = -EEXIST;
  1735. goto done;
  1736. }
  1737. }
  1738. /* Prevent excess memory consumption */
  1739. if (atomic_inc_return(&dev->nmappings) > UVC_MAX_CONTROL_MAPPINGS) {
  1740. atomic_dec(&dev->nmappings);
  1741. uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', maximum "
  1742. "mappings count (%u) exceeded.\n", mapping->name,
  1743. UVC_MAX_CONTROL_MAPPINGS);
  1744. ret = -ENOMEM;
  1745. goto done;
  1746. }
  1747. ret = __uvc_ctrl_add_mapping(dev, ctrl, mapping);
  1748. if (ret < 0)
  1749. atomic_dec(&dev->nmappings);
  1750. done:
  1751. mutex_unlock(&chain->ctrl_mutex);
  1752. return ret;
  1753. }
  1754. /*
  1755. * Prune an entity of its bogus controls using a blacklist. Bogus controls
  1756. * are currently the ones that crash the camera or unconditionally return an
  1757. * error when queried.
  1758. */
  1759. static void uvc_ctrl_prune_entity(struct uvc_device *dev,
  1760. struct uvc_entity *entity)
  1761. {
  1762. struct uvc_ctrl_blacklist {
  1763. struct usb_device_id id;
  1764. u8 index;
  1765. };
  1766. static const struct uvc_ctrl_blacklist processing_blacklist[] = {
  1767. { { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */
  1768. { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
  1769. { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
  1770. };
  1771. static const struct uvc_ctrl_blacklist camera_blacklist[] = {
  1772. { { USB_DEVICE(0x06f8, 0x3005) }, 9 }, /* Zoom, Absolute */
  1773. };
  1774. const struct uvc_ctrl_blacklist *blacklist;
  1775. unsigned int size;
  1776. unsigned int count;
  1777. unsigned int i;
  1778. u8 *controls;
  1779. switch (UVC_ENTITY_TYPE(entity)) {
  1780. case UVC_VC_PROCESSING_UNIT:
  1781. blacklist = processing_blacklist;
  1782. count = ARRAY_SIZE(processing_blacklist);
  1783. controls = entity->processing.bmControls;
  1784. size = entity->processing.bControlSize;
  1785. break;
  1786. case UVC_ITT_CAMERA:
  1787. blacklist = camera_blacklist;
  1788. count = ARRAY_SIZE(camera_blacklist);
  1789. controls = entity->camera.bmControls;
  1790. size = entity->camera.bControlSize;
  1791. break;
  1792. default:
  1793. return;
  1794. }
  1795. for (i = 0; i < count; ++i) {
  1796. if (!usb_match_one_id(dev->intf, &blacklist[i].id))
  1797. continue;
  1798. if (blacklist[i].index >= 8 * size ||
  1799. !uvc_test_bit(controls, blacklist[i].index))
  1800. continue;
  1801. uvc_trace(UVC_TRACE_CONTROL, "%u/%u control is black listed, "
  1802. "removing it.\n", entity->id, blacklist[i].index);
  1803. uvc_clear_bit(controls, blacklist[i].index);
  1804. }
  1805. }
  1806. /*
  1807. * Add control information and hardcoded stock control mappings to the given
  1808. * device.
  1809. */
  1810. static void uvc_ctrl_init_ctrl(struct uvc_device *dev, struct uvc_control *ctrl)
  1811. {
  1812. const struct uvc_control_info *info = uvc_ctrls;
  1813. const struct uvc_control_info *iend = info + ARRAY_SIZE(uvc_ctrls);
  1814. const struct uvc_control_mapping *mapping = uvc_ctrl_mappings;
  1815. const struct uvc_control_mapping *mend =
  1816. mapping + ARRAY_SIZE(uvc_ctrl_mappings);
  1817. /* XU controls initialization requires querying the device for control
  1818. * information. As some buggy UVC devices will crash when queried
  1819. * repeatedly in a tight loop, delay XU controls initialization until
  1820. * first use.
  1821. */
  1822. if (UVC_ENTITY_TYPE(ctrl->entity) == UVC_VC_EXTENSION_UNIT)
  1823. return;
  1824. for (; info < iend; ++info) {
  1825. if (uvc_entity_match_guid(ctrl->entity, info->entity) &&
  1826. ctrl->index == info->index) {
  1827. uvc_ctrl_add_info(dev, ctrl, info);
  1828. break;
  1829. }
  1830. }
  1831. if (!ctrl->initialized)
  1832. return;
  1833. for (; mapping < mend; ++mapping) {
  1834. if (uvc_entity_match_guid(ctrl->entity, mapping->entity) &&
  1835. ctrl->info.selector == mapping->selector)
  1836. __uvc_ctrl_add_mapping(dev, ctrl, mapping);
  1837. }
  1838. }
  1839. /*
  1840. * Initialize device controls.
  1841. */
  1842. int uvc_ctrl_init_device(struct uvc_device *dev)
  1843. {
  1844. struct uvc_entity *entity;
  1845. unsigned int i;
  1846. /* Walk the entities list and instantiate controls */
  1847. list_for_each_entry(entity, &dev->entities, list) {
  1848. struct uvc_control *ctrl;
  1849. unsigned int bControlSize = 0, ncontrols = 0;
  1850. __u8 *bmControls = NULL;
  1851. if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
  1852. bmControls = entity->extension.bmControls;
  1853. bControlSize = entity->extension.bControlSize;
  1854. } else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) {
  1855. bmControls = entity->processing.bmControls;
  1856. bControlSize = entity->processing.bControlSize;
  1857. } else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
  1858. bmControls = entity->camera.bmControls;
  1859. bControlSize = entity->camera.bControlSize;
  1860. }
  1861. /* Remove bogus/blacklisted controls */
  1862. uvc_ctrl_prune_entity(dev, entity);
  1863. /* Count supported controls and allocate the controls array */
  1864. for (i = 0; i < bControlSize; ++i)
  1865. ncontrols += hweight8(bmControls[i]);
  1866. if (ncontrols == 0)
  1867. continue;
  1868. entity->controls = kcalloc(ncontrols, sizeof(*ctrl),
  1869. GFP_KERNEL);
  1870. if (entity->controls == NULL)
  1871. return -ENOMEM;
  1872. entity->ncontrols = ncontrols;
  1873. /* Initialize all supported controls */
  1874. ctrl = entity->controls;
  1875. for (i = 0; i < bControlSize * 8; ++i) {
  1876. if (uvc_test_bit(bmControls, i) == 0)
  1877. continue;
  1878. ctrl->entity = entity;
  1879. ctrl->index = i;
  1880. uvc_ctrl_init_ctrl(dev, ctrl);
  1881. ctrl++;
  1882. }
  1883. }
  1884. return 0;
  1885. }
  1886. /*
  1887. * Cleanup device controls.
  1888. */
  1889. static void uvc_ctrl_cleanup_mappings(struct uvc_device *dev,
  1890. struct uvc_control *ctrl)
  1891. {
  1892. struct uvc_control_mapping *mapping, *nm;
  1893. list_for_each_entry_safe(mapping, nm, &ctrl->info.mappings, list) {
  1894. list_del(&mapping->list);
  1895. kfree(mapping->menu_info);
  1896. kfree(mapping);
  1897. }
  1898. }
  1899. void uvc_ctrl_cleanup_device(struct uvc_device *dev)
  1900. {
  1901. struct uvc_entity *entity;
  1902. unsigned int i;
  1903. /* Free controls and control mappings for all entities. */
  1904. list_for_each_entry(entity, &dev->entities, list) {
  1905. for (i = 0; i < entity->ncontrols; ++i) {
  1906. struct uvc_control *ctrl = &entity->controls[i];
  1907. if (!ctrl->initialized)
  1908. continue;
  1909. uvc_ctrl_cleanup_mappings(dev, ctrl);
  1910. kfree(ctrl->uvc_data);
  1911. }
  1912. kfree(entity->controls);
  1913. }
  1914. }