uvc_ctrl.c 47 KB

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