uvc_ctrl.c 56 KB

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