uvc_ctrl.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938
  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 "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. if (UVC_ENTITY_TYPE(ctrl->entity) !=
  836. UVC_VC_EXTENSION_UNIT)
  837. return ret;
  838. /* GET_RES is mandatory for XU controls, but some
  839. * cameras still choke on it. Ignore errors and set the
  840. * resolution value to zero.
  841. */
  842. uvc_warn_once(chain->dev, UVC_WARN_XU_GET_RES,
  843. "UVC non compliance - GET_RES failed on "
  844. "an XU control. Enabling workaround.\n");
  845. memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES), 0,
  846. ctrl->info.size);
  847. }
  848. }
  849. ctrl->cached = 1;
  850. return 0;
  851. }
  852. int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
  853. struct v4l2_queryctrl *v4l2_ctrl)
  854. {
  855. struct uvc_control *ctrl;
  856. struct uvc_control_mapping *mapping;
  857. struct uvc_menu_info *menu;
  858. unsigned int i;
  859. int ret;
  860. ret = mutex_lock_interruptible(&chain->ctrl_mutex);
  861. if (ret < 0)
  862. return -ERESTARTSYS;
  863. ctrl = uvc_find_control(chain, v4l2_ctrl->id, &mapping);
  864. if (ctrl == NULL) {
  865. ret = -EINVAL;
  866. goto done;
  867. }
  868. memset(v4l2_ctrl, 0, sizeof *v4l2_ctrl);
  869. v4l2_ctrl->id = mapping->id;
  870. v4l2_ctrl->type = mapping->v4l2_type;
  871. strlcpy(v4l2_ctrl->name, mapping->name, sizeof v4l2_ctrl->name);
  872. v4l2_ctrl->flags = 0;
  873. if (!(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
  874. v4l2_ctrl->flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
  875. if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
  876. v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  877. if (!ctrl->cached) {
  878. ret = uvc_ctrl_populate_cache(chain, ctrl);
  879. if (ret < 0)
  880. goto done;
  881. }
  882. if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
  883. v4l2_ctrl->default_value = mapping->get(mapping, UVC_GET_DEF,
  884. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF));
  885. }
  886. switch (mapping->v4l2_type) {
  887. case V4L2_CTRL_TYPE_MENU:
  888. v4l2_ctrl->minimum = 0;
  889. v4l2_ctrl->maximum = mapping->menu_count - 1;
  890. v4l2_ctrl->step = 1;
  891. menu = mapping->menu_info;
  892. for (i = 0; i < mapping->menu_count; ++i, ++menu) {
  893. if (menu->value == v4l2_ctrl->default_value) {
  894. v4l2_ctrl->default_value = i;
  895. break;
  896. }
  897. }
  898. goto done;
  899. case V4L2_CTRL_TYPE_BOOLEAN:
  900. v4l2_ctrl->minimum = 0;
  901. v4l2_ctrl->maximum = 1;
  902. v4l2_ctrl->step = 1;
  903. goto done;
  904. case V4L2_CTRL_TYPE_BUTTON:
  905. v4l2_ctrl->minimum = 0;
  906. v4l2_ctrl->maximum = 0;
  907. v4l2_ctrl->step = 0;
  908. goto done;
  909. default:
  910. break;
  911. }
  912. if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN)
  913. v4l2_ctrl->minimum = mapping->get(mapping, UVC_GET_MIN,
  914. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
  915. if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX)
  916. v4l2_ctrl->maximum = mapping->get(mapping, UVC_GET_MAX,
  917. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
  918. if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)
  919. v4l2_ctrl->step = mapping->get(mapping, UVC_GET_RES,
  920. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
  921. done:
  922. mutex_unlock(&chain->ctrl_mutex);
  923. return ret;
  924. }
  925. /*
  926. * Mapping V4L2 controls to UVC controls can be straighforward if done well.
  927. * Most of the UVC controls exist in V4L2, and can be mapped directly. Some
  928. * must be grouped (for instance the Red Balance, Blue Balance and Do White
  929. * Balance V4L2 controls use the White Balance Component UVC control) or
  930. * otherwise translated. The approach we take here is to use a translation
  931. * table for the controls that can be mapped directly, and handle the others
  932. * manually.
  933. */
  934. int uvc_query_v4l2_menu(struct uvc_video_chain *chain,
  935. struct v4l2_querymenu *query_menu)
  936. {
  937. struct uvc_menu_info *menu_info;
  938. struct uvc_control_mapping *mapping;
  939. struct uvc_control *ctrl;
  940. u32 index = query_menu->index;
  941. u32 id = query_menu->id;
  942. int ret;
  943. memset(query_menu, 0, sizeof(*query_menu));
  944. query_menu->id = id;
  945. query_menu->index = index;
  946. ret = mutex_lock_interruptible(&chain->ctrl_mutex);
  947. if (ret < 0)
  948. return -ERESTARTSYS;
  949. ctrl = uvc_find_control(chain, query_menu->id, &mapping);
  950. if (ctrl == NULL || mapping->v4l2_type != V4L2_CTRL_TYPE_MENU) {
  951. ret = -EINVAL;
  952. goto done;
  953. }
  954. if (query_menu->index >= mapping->menu_count) {
  955. ret = -EINVAL;
  956. goto done;
  957. }
  958. menu_info = &mapping->menu_info[query_menu->index];
  959. if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK &&
  960. (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)) {
  961. s32 bitmap;
  962. if (!ctrl->cached) {
  963. ret = uvc_ctrl_populate_cache(chain, ctrl);
  964. if (ret < 0)
  965. goto done;
  966. }
  967. bitmap = mapping->get(mapping, UVC_GET_RES,
  968. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
  969. if (!(bitmap & menu_info->value)) {
  970. ret = -EINVAL;
  971. goto done;
  972. }
  973. }
  974. strlcpy(query_menu->name, menu_info->name, sizeof query_menu->name);
  975. done:
  976. mutex_unlock(&chain->ctrl_mutex);
  977. return ret;
  978. }
  979. /* --------------------------------------------------------------------------
  980. * Control transactions
  981. *
  982. * To make extended set operations as atomic as the hardware allows, controls
  983. * are handled using begin/commit/rollback operations.
  984. *
  985. * At the beginning of a set request, uvc_ctrl_begin should be called to
  986. * initialize the request. This function acquires the control lock.
  987. *
  988. * When setting a control, the new value is stored in the control data field
  989. * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
  990. * later processing. If the UVC and V4L2 control sizes differ, the current
  991. * value is loaded from the hardware before storing the new value in the data
  992. * field.
  993. *
  994. * After processing all controls in the transaction, uvc_ctrl_commit or
  995. * uvc_ctrl_rollback must be called to apply the pending changes to the
  996. * hardware or revert them. When applying changes, all controls marked as
  997. * dirty will be modified in the UVC device, and the dirty flag will be
  998. * cleared. When reverting controls, the control data field
  999. * UVC_CTRL_DATA_CURRENT is reverted to its previous value
  1000. * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
  1001. * control lock.
  1002. */
  1003. int uvc_ctrl_begin(struct uvc_video_chain *chain)
  1004. {
  1005. return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0;
  1006. }
  1007. static int uvc_ctrl_commit_entity(struct uvc_device *dev,
  1008. struct uvc_entity *entity, int rollback)
  1009. {
  1010. struct uvc_control *ctrl;
  1011. unsigned int i;
  1012. int ret;
  1013. if (entity == NULL)
  1014. return 0;
  1015. for (i = 0; i < entity->ncontrols; ++i) {
  1016. ctrl = &entity->controls[i];
  1017. if (!ctrl->initialized)
  1018. continue;
  1019. /* Reset the loaded flag for auto-update controls that were
  1020. * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
  1021. * uvc_ctrl_get from using the cached value.
  1022. */
  1023. if (ctrl->info.flags & UVC_CTRL_FLAG_AUTO_UPDATE)
  1024. ctrl->loaded = 0;
  1025. if (!ctrl->dirty)
  1026. continue;
  1027. if (!rollback)
  1028. ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id,
  1029. dev->intfnum, ctrl->info.selector,
  1030. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  1031. ctrl->info.size);
  1032. else
  1033. ret = 0;
  1034. if (rollback || ret < 0)
  1035. memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  1036. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
  1037. ctrl->info.size);
  1038. ctrl->dirty = 0;
  1039. if (ret < 0)
  1040. return ret;
  1041. }
  1042. return 0;
  1043. }
  1044. int __uvc_ctrl_commit(struct uvc_video_chain *chain, int rollback)
  1045. {
  1046. struct uvc_entity *entity;
  1047. int ret = 0;
  1048. /* Find the control. */
  1049. list_for_each_entry(entity, &chain->entities, chain) {
  1050. ret = uvc_ctrl_commit_entity(chain->dev, entity, rollback);
  1051. if (ret < 0)
  1052. goto done;
  1053. }
  1054. done:
  1055. mutex_unlock(&chain->ctrl_mutex);
  1056. return ret;
  1057. }
  1058. int uvc_ctrl_get(struct uvc_video_chain *chain,
  1059. struct v4l2_ext_control *xctrl)
  1060. {
  1061. struct uvc_control *ctrl;
  1062. struct uvc_control_mapping *mapping;
  1063. struct uvc_menu_info *menu;
  1064. unsigned int i;
  1065. int ret;
  1066. ctrl = uvc_find_control(chain, xctrl->id, &mapping);
  1067. if (ctrl == NULL || (ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0)
  1068. return -EINVAL;
  1069. if (!ctrl->loaded) {
  1070. ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, ctrl->entity->id,
  1071. chain->dev->intfnum, ctrl->info.selector,
  1072. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  1073. ctrl->info.size);
  1074. if (ret < 0)
  1075. return ret;
  1076. ctrl->loaded = 1;
  1077. }
  1078. xctrl->value = mapping->get(mapping, UVC_GET_CUR,
  1079. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
  1080. if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
  1081. menu = mapping->menu_info;
  1082. for (i = 0; i < mapping->menu_count; ++i, ++menu) {
  1083. if (menu->value == xctrl->value) {
  1084. xctrl->value = i;
  1085. break;
  1086. }
  1087. }
  1088. }
  1089. return 0;
  1090. }
  1091. int uvc_ctrl_set(struct uvc_video_chain *chain,
  1092. struct v4l2_ext_control *xctrl)
  1093. {
  1094. struct uvc_control *ctrl;
  1095. struct uvc_control_mapping *mapping;
  1096. s32 value;
  1097. u32 step;
  1098. s32 min;
  1099. s32 max;
  1100. int ret;
  1101. ctrl = uvc_find_control(chain, xctrl->id, &mapping);
  1102. if (ctrl == NULL || (ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR) == 0)
  1103. return -EINVAL;
  1104. /* Clamp out of range values. */
  1105. switch (mapping->v4l2_type) {
  1106. case V4L2_CTRL_TYPE_INTEGER:
  1107. if (!ctrl->cached) {
  1108. ret = uvc_ctrl_populate_cache(chain, ctrl);
  1109. if (ret < 0)
  1110. return ret;
  1111. }
  1112. min = mapping->get(mapping, UVC_GET_MIN,
  1113. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
  1114. max = mapping->get(mapping, UVC_GET_MAX,
  1115. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
  1116. step = mapping->get(mapping, UVC_GET_RES,
  1117. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
  1118. if (step == 0)
  1119. step = 1;
  1120. xctrl->value = min + (xctrl->value - min + step/2) / step * step;
  1121. xctrl->value = clamp(xctrl->value, min, max);
  1122. value = xctrl->value;
  1123. break;
  1124. case V4L2_CTRL_TYPE_BOOLEAN:
  1125. xctrl->value = clamp(xctrl->value, 0, 1);
  1126. value = xctrl->value;
  1127. break;
  1128. case V4L2_CTRL_TYPE_MENU:
  1129. if (xctrl->value < 0 || xctrl->value >= mapping->menu_count)
  1130. return -ERANGE;
  1131. value = mapping->menu_info[xctrl->value].value;
  1132. /* Valid menu indices are reported by the GET_RES request for
  1133. * UVC controls that support it.
  1134. */
  1135. if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK &&
  1136. (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)) {
  1137. if (!ctrl->cached) {
  1138. ret = uvc_ctrl_populate_cache(chain, ctrl);
  1139. if (ret < 0)
  1140. return ret;
  1141. }
  1142. step = mapping->get(mapping, UVC_GET_RES,
  1143. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
  1144. if (!(step & value))
  1145. return -ERANGE;
  1146. }
  1147. break;
  1148. default:
  1149. value = xctrl->value;
  1150. break;
  1151. }
  1152. /* If the mapping doesn't span the whole UVC control, the current value
  1153. * needs to be loaded from the device to perform the read-modify-write
  1154. * operation.
  1155. */
  1156. if (!ctrl->loaded && (ctrl->info.size * 8) != mapping->size) {
  1157. if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0) {
  1158. memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  1159. 0, ctrl->info.size);
  1160. } else {
  1161. ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
  1162. ctrl->entity->id, chain->dev->intfnum,
  1163. ctrl->info.selector,
  1164. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  1165. ctrl->info.size);
  1166. if (ret < 0)
  1167. return ret;
  1168. }
  1169. ctrl->loaded = 1;
  1170. }
  1171. /* Backup the current value in case we need to rollback later. */
  1172. if (!ctrl->dirty) {
  1173. memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
  1174. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  1175. ctrl->info.size);
  1176. }
  1177. mapping->set(mapping, value,
  1178. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
  1179. ctrl->dirty = 1;
  1180. ctrl->modified = 1;
  1181. return 0;
  1182. }
  1183. /* --------------------------------------------------------------------------
  1184. * Dynamic controls
  1185. */
  1186. static void uvc_ctrl_fixup_xu_info(struct uvc_device *dev,
  1187. const struct uvc_control *ctrl, struct uvc_control_info *info)
  1188. {
  1189. struct uvc_ctrl_fixup {
  1190. struct usb_device_id id;
  1191. u8 entity;
  1192. u8 selector;
  1193. u8 flags;
  1194. };
  1195. static const struct uvc_ctrl_fixup fixups[] = {
  1196. { { USB_DEVICE(0x046d, 0x08c2) }, 9, 1,
  1197. UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
  1198. UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
  1199. UVC_CTRL_FLAG_AUTO_UPDATE },
  1200. { { USB_DEVICE(0x046d, 0x08cc) }, 9, 1,
  1201. UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
  1202. UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
  1203. UVC_CTRL_FLAG_AUTO_UPDATE },
  1204. { { USB_DEVICE(0x046d, 0x0994) }, 9, 1,
  1205. UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
  1206. UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
  1207. UVC_CTRL_FLAG_AUTO_UPDATE },
  1208. };
  1209. unsigned int i;
  1210. for (i = 0; i < ARRAY_SIZE(fixups); ++i) {
  1211. if (!usb_match_one_id(dev->intf, &fixups[i].id))
  1212. continue;
  1213. if (fixups[i].entity == ctrl->entity->id &&
  1214. fixups[i].selector == info->selector) {
  1215. info->flags = fixups[i].flags;
  1216. return;
  1217. }
  1218. }
  1219. }
  1220. /*
  1221. * Query control information (size and flags) for XU controls.
  1222. */
  1223. static int uvc_ctrl_fill_xu_info(struct uvc_device *dev,
  1224. const struct uvc_control *ctrl, struct uvc_control_info *info)
  1225. {
  1226. u8 *data;
  1227. int ret;
  1228. data = kmalloc(2, GFP_KERNEL);
  1229. if (data == NULL)
  1230. return -ENOMEM;
  1231. memcpy(info->entity, ctrl->entity->extension.guidExtensionCode,
  1232. sizeof(info->entity));
  1233. info->index = ctrl->index;
  1234. info->selector = ctrl->index + 1;
  1235. /* Query and verify the control length (GET_LEN) */
  1236. ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id, dev->intfnum,
  1237. info->selector, data, 2);
  1238. if (ret < 0) {
  1239. uvc_trace(UVC_TRACE_CONTROL,
  1240. "GET_LEN failed on control %pUl/%u (%d).\n",
  1241. info->entity, info->selector, ret);
  1242. goto done;
  1243. }
  1244. info->size = le16_to_cpup((__le16 *)data);
  1245. /* Query the control information (GET_INFO) */
  1246. ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id, dev->intfnum,
  1247. info->selector, data, 1);
  1248. if (ret < 0) {
  1249. uvc_trace(UVC_TRACE_CONTROL,
  1250. "GET_INFO failed on control %pUl/%u (%d).\n",
  1251. info->entity, info->selector, ret);
  1252. goto done;
  1253. }
  1254. info->flags = UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX
  1255. | UVC_CTRL_FLAG_GET_RES | UVC_CTRL_FLAG_GET_DEF
  1256. | (data[0] & UVC_CONTROL_CAP_GET ?
  1257. UVC_CTRL_FLAG_GET_CUR : 0)
  1258. | (data[0] & UVC_CONTROL_CAP_SET ?
  1259. UVC_CTRL_FLAG_SET_CUR : 0)
  1260. | (data[0] & UVC_CONTROL_CAP_AUTOUPDATE ?
  1261. UVC_CTRL_FLAG_AUTO_UPDATE : 0);
  1262. uvc_ctrl_fixup_xu_info(dev, ctrl, info);
  1263. uvc_trace(UVC_TRACE_CONTROL, "XU control %pUl/%u queried: len %u, "
  1264. "flags { get %u set %u auto %u }.\n",
  1265. info->entity, info->selector, info->size,
  1266. (info->flags & UVC_CTRL_FLAG_GET_CUR) ? 1 : 0,
  1267. (info->flags & UVC_CTRL_FLAG_SET_CUR) ? 1 : 0,
  1268. (info->flags & UVC_CTRL_FLAG_AUTO_UPDATE) ? 1 : 0);
  1269. done:
  1270. kfree(data);
  1271. return ret;
  1272. }
  1273. static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
  1274. const struct uvc_control_info *info);
  1275. static int uvc_ctrl_init_xu_ctrl(struct uvc_device *dev,
  1276. struct uvc_control *ctrl)
  1277. {
  1278. struct uvc_control_info info;
  1279. int ret;
  1280. if (ctrl->initialized)
  1281. return 0;
  1282. ret = uvc_ctrl_fill_xu_info(dev, ctrl, &info);
  1283. if (ret < 0)
  1284. return ret;
  1285. ret = uvc_ctrl_add_info(dev, ctrl, &info);
  1286. if (ret < 0)
  1287. uvc_trace(UVC_TRACE_CONTROL, "Failed to initialize control "
  1288. "%pUl/%u on device %s entity %u\n", info.entity,
  1289. info.selector, dev->udev->devpath, ctrl->entity->id);
  1290. return ret;
  1291. }
  1292. int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
  1293. struct uvc_xu_control_query *xqry)
  1294. {
  1295. struct uvc_entity *entity;
  1296. struct uvc_control *ctrl;
  1297. unsigned int i, found = 0;
  1298. __u32 reqflags;
  1299. __u16 size;
  1300. __u8 *data = NULL;
  1301. int ret;
  1302. /* Find the extension unit. */
  1303. list_for_each_entry(entity, &chain->entities, chain) {
  1304. if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT &&
  1305. entity->id == xqry->unit)
  1306. break;
  1307. }
  1308. if (entity->id != xqry->unit) {
  1309. uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
  1310. xqry->unit);
  1311. return -ENOENT;
  1312. }
  1313. /* Find the control and perform delayed initialization if needed. */
  1314. for (i = 0; i < entity->ncontrols; ++i) {
  1315. ctrl = &entity->controls[i];
  1316. if (ctrl->index == xqry->selector - 1) {
  1317. found = 1;
  1318. break;
  1319. }
  1320. }
  1321. if (!found) {
  1322. uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u not found.\n",
  1323. entity->extension.guidExtensionCode, xqry->selector);
  1324. return -ENOENT;
  1325. }
  1326. if (mutex_lock_interruptible(&chain->ctrl_mutex))
  1327. return -ERESTARTSYS;
  1328. ret = uvc_ctrl_init_xu_ctrl(chain->dev, ctrl);
  1329. if (ret < 0) {
  1330. ret = -ENOENT;
  1331. goto done;
  1332. }
  1333. /* Validate the required buffer size and flags for the request */
  1334. reqflags = 0;
  1335. size = ctrl->info.size;
  1336. switch (xqry->query) {
  1337. case UVC_GET_CUR:
  1338. reqflags = UVC_CTRL_FLAG_GET_CUR;
  1339. break;
  1340. case UVC_GET_MIN:
  1341. reqflags = UVC_CTRL_FLAG_GET_MIN;
  1342. break;
  1343. case UVC_GET_MAX:
  1344. reqflags = UVC_CTRL_FLAG_GET_MAX;
  1345. break;
  1346. case UVC_GET_DEF:
  1347. reqflags = UVC_CTRL_FLAG_GET_DEF;
  1348. break;
  1349. case UVC_GET_RES:
  1350. reqflags = UVC_CTRL_FLAG_GET_RES;
  1351. break;
  1352. case UVC_SET_CUR:
  1353. reqflags = UVC_CTRL_FLAG_SET_CUR;
  1354. break;
  1355. case UVC_GET_LEN:
  1356. size = 2;
  1357. break;
  1358. case UVC_GET_INFO:
  1359. size = 1;
  1360. break;
  1361. default:
  1362. ret = -EINVAL;
  1363. goto done;
  1364. }
  1365. if (size != xqry->size) {
  1366. ret = -ENOBUFS;
  1367. goto done;
  1368. }
  1369. if (reqflags && !(ctrl->info.flags & reqflags)) {
  1370. ret = -EBADRQC;
  1371. goto done;
  1372. }
  1373. data = kmalloc(size, GFP_KERNEL);
  1374. if (data == NULL) {
  1375. ret = -ENOMEM;
  1376. goto done;
  1377. }
  1378. if (xqry->query == UVC_SET_CUR &&
  1379. copy_from_user(data, xqry->data, size)) {
  1380. ret = -EFAULT;
  1381. goto done;
  1382. }
  1383. ret = uvc_query_ctrl(chain->dev, xqry->query, xqry->unit,
  1384. chain->dev->intfnum, xqry->selector, data, size);
  1385. if (ret < 0)
  1386. goto done;
  1387. if (xqry->query != UVC_SET_CUR &&
  1388. copy_to_user(xqry->data, data, size))
  1389. ret = -EFAULT;
  1390. done:
  1391. kfree(data);
  1392. mutex_unlock(&chain->ctrl_mutex);
  1393. return ret;
  1394. }
  1395. /* --------------------------------------------------------------------------
  1396. * Suspend/resume
  1397. */
  1398. /*
  1399. * Restore control values after resume, skipping controls that haven't been
  1400. * changed.
  1401. *
  1402. * TODO
  1403. * - Don't restore modified controls that are back to their default value.
  1404. * - Handle restore order (Auto-Exposure Mode should be restored before
  1405. * Exposure Time).
  1406. */
  1407. int uvc_ctrl_resume_device(struct uvc_device *dev)
  1408. {
  1409. struct uvc_control *ctrl;
  1410. struct uvc_entity *entity;
  1411. unsigned int i;
  1412. int ret;
  1413. /* Walk the entities list and restore controls when possible. */
  1414. list_for_each_entry(entity, &dev->entities, list) {
  1415. for (i = 0; i < entity->ncontrols; ++i) {
  1416. ctrl = &entity->controls[i];
  1417. if (!ctrl->initialized || !ctrl->modified ||
  1418. (ctrl->info.flags & UVC_CTRL_FLAG_RESTORE) == 0)
  1419. continue;
  1420. printk(KERN_INFO "restoring control %pUl/%u/%u\n",
  1421. ctrl->info.entity, ctrl->info.index,
  1422. ctrl->info.selector);
  1423. ctrl->dirty = 1;
  1424. }
  1425. ret = uvc_ctrl_commit_entity(dev, entity, 0);
  1426. if (ret < 0)
  1427. return ret;
  1428. }
  1429. return 0;
  1430. }
  1431. /* --------------------------------------------------------------------------
  1432. * Control and mapping handling
  1433. */
  1434. /*
  1435. * Add control information to a given control.
  1436. */
  1437. static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
  1438. const struct uvc_control_info *info)
  1439. {
  1440. int ret = 0;
  1441. memcpy(&ctrl->info, info, sizeof(*info));
  1442. INIT_LIST_HEAD(&ctrl->info.mappings);
  1443. /* Allocate an array to save control values (cur, def, max, etc.) */
  1444. ctrl->uvc_data = kzalloc(ctrl->info.size * UVC_CTRL_DATA_LAST + 1,
  1445. GFP_KERNEL);
  1446. if (ctrl->uvc_data == NULL) {
  1447. ret = -ENOMEM;
  1448. goto done;
  1449. }
  1450. ctrl->initialized = 1;
  1451. uvc_trace(UVC_TRACE_CONTROL, "Added control %pUl/%u to device %s "
  1452. "entity %u\n", ctrl->info.entity, ctrl->info.selector,
  1453. dev->udev->devpath, ctrl->entity->id);
  1454. done:
  1455. if (ret < 0)
  1456. kfree(ctrl->uvc_data);
  1457. return ret;
  1458. }
  1459. /*
  1460. * Add a control mapping to a given control.
  1461. */
  1462. static int __uvc_ctrl_add_mapping(struct uvc_device *dev,
  1463. struct uvc_control *ctrl, const struct uvc_control_mapping *mapping)
  1464. {
  1465. struct uvc_control_mapping *map;
  1466. unsigned int size;
  1467. /* Most mappings come from static kernel data and need to be duplicated.
  1468. * Mappings that come from userspace will be unnecessarily duplicated,
  1469. * this could be optimized.
  1470. */
  1471. map = kmemdup(mapping, sizeof(*mapping), GFP_KERNEL);
  1472. if (map == NULL)
  1473. return -ENOMEM;
  1474. size = sizeof(*mapping->menu_info) * mapping->menu_count;
  1475. map->menu_info = kmemdup(mapping->menu_info, size, GFP_KERNEL);
  1476. if (map->menu_info == NULL) {
  1477. kfree(map);
  1478. return -ENOMEM;
  1479. }
  1480. if (map->get == NULL)
  1481. map->get = uvc_get_le_value;
  1482. if (map->set == NULL)
  1483. map->set = uvc_set_le_value;
  1484. map->ctrl = &ctrl->info;
  1485. list_add_tail(&map->list, &ctrl->info.mappings);
  1486. uvc_trace(UVC_TRACE_CONTROL,
  1487. "Adding mapping '%s' to control %pUl/%u.\n",
  1488. map->name, ctrl->info.entity, ctrl->info.selector);
  1489. return 0;
  1490. }
  1491. int uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
  1492. const struct uvc_control_mapping *mapping)
  1493. {
  1494. struct uvc_device *dev = chain->dev;
  1495. struct uvc_control_mapping *map;
  1496. struct uvc_entity *entity;
  1497. struct uvc_control *ctrl;
  1498. int found = 0;
  1499. int ret;
  1500. if (mapping->id & ~V4L2_CTRL_ID_MASK) {
  1501. uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', control "
  1502. "id 0x%08x is invalid.\n", mapping->name,
  1503. mapping->id);
  1504. return -EINVAL;
  1505. }
  1506. /* Search for the matching (GUID/CS) control on the current chain */
  1507. list_for_each_entry(entity, &chain->entities, chain) {
  1508. unsigned int i;
  1509. if (UVC_ENTITY_TYPE(entity) != UVC_VC_EXTENSION_UNIT ||
  1510. !uvc_entity_match_guid(entity, mapping->entity))
  1511. continue;
  1512. for (i = 0; i < entity->ncontrols; ++i) {
  1513. ctrl = &entity->controls[i];
  1514. if (ctrl->index == mapping->selector - 1) {
  1515. found = 1;
  1516. break;
  1517. }
  1518. }
  1519. if (found)
  1520. break;
  1521. }
  1522. if (!found)
  1523. return -ENOENT;
  1524. if (ctrl->info.size < mapping->size)
  1525. return -EINVAL;
  1526. if (mutex_lock_interruptible(&chain->ctrl_mutex))
  1527. return -ERESTARTSYS;
  1528. /* Perform delayed initialization of XU controls */
  1529. ret = uvc_ctrl_init_xu_ctrl(dev, ctrl);
  1530. if (ret < 0) {
  1531. ret = -ENOENT;
  1532. goto done;
  1533. }
  1534. list_for_each_entry(map, &ctrl->info.mappings, list) {
  1535. if (mapping->id == map->id) {
  1536. uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', "
  1537. "control id 0x%08x already exists.\n",
  1538. mapping->name, mapping->id);
  1539. ret = -EEXIST;
  1540. goto done;
  1541. }
  1542. }
  1543. /* Prevent excess memory consumption */
  1544. if (atomic_inc_return(&dev->nmappings) > UVC_MAX_CONTROL_MAPPINGS) {
  1545. atomic_dec(&dev->nmappings);
  1546. uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', maximum "
  1547. "mappings count (%u) exceeded.\n", mapping->name,
  1548. UVC_MAX_CONTROL_MAPPINGS);
  1549. ret = -ENOMEM;
  1550. goto done;
  1551. }
  1552. ret = __uvc_ctrl_add_mapping(dev, ctrl, mapping);
  1553. if (ret < 0)
  1554. atomic_dec(&dev->nmappings);
  1555. done:
  1556. mutex_unlock(&chain->ctrl_mutex);
  1557. return ret;
  1558. }
  1559. /*
  1560. * Prune an entity of its bogus controls using a blacklist. Bogus controls
  1561. * are currently the ones that crash the camera or unconditionally return an
  1562. * error when queried.
  1563. */
  1564. static void uvc_ctrl_prune_entity(struct uvc_device *dev,
  1565. struct uvc_entity *entity)
  1566. {
  1567. struct uvc_ctrl_blacklist {
  1568. struct usb_device_id id;
  1569. u8 index;
  1570. };
  1571. static const struct uvc_ctrl_blacklist processing_blacklist[] = {
  1572. { { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */
  1573. { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
  1574. { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
  1575. };
  1576. static const struct uvc_ctrl_blacklist camera_blacklist[] = {
  1577. { { USB_DEVICE(0x06f8, 0x3005) }, 9 }, /* Zoom, Absolute */
  1578. };
  1579. const struct uvc_ctrl_blacklist *blacklist;
  1580. unsigned int size;
  1581. unsigned int count;
  1582. unsigned int i;
  1583. u8 *controls;
  1584. switch (UVC_ENTITY_TYPE(entity)) {
  1585. case UVC_VC_PROCESSING_UNIT:
  1586. blacklist = processing_blacklist;
  1587. count = ARRAY_SIZE(processing_blacklist);
  1588. controls = entity->processing.bmControls;
  1589. size = entity->processing.bControlSize;
  1590. break;
  1591. case UVC_ITT_CAMERA:
  1592. blacklist = camera_blacklist;
  1593. count = ARRAY_SIZE(camera_blacklist);
  1594. controls = entity->camera.bmControls;
  1595. size = entity->camera.bControlSize;
  1596. break;
  1597. default:
  1598. return;
  1599. }
  1600. for (i = 0; i < count; ++i) {
  1601. if (!usb_match_one_id(dev->intf, &blacklist[i].id))
  1602. continue;
  1603. if (blacklist[i].index >= 8 * size ||
  1604. !uvc_test_bit(controls, blacklist[i].index))
  1605. continue;
  1606. uvc_trace(UVC_TRACE_CONTROL, "%u/%u control is black listed, "
  1607. "removing it.\n", entity->id, blacklist[i].index);
  1608. uvc_clear_bit(controls, blacklist[i].index);
  1609. }
  1610. }
  1611. /*
  1612. * Add control information and hardcoded stock control mappings to the given
  1613. * device.
  1614. */
  1615. static void uvc_ctrl_init_ctrl(struct uvc_device *dev, struct uvc_control *ctrl)
  1616. {
  1617. const struct uvc_control_info *info = uvc_ctrls;
  1618. const struct uvc_control_info *iend = info + ARRAY_SIZE(uvc_ctrls);
  1619. const struct uvc_control_mapping *mapping = uvc_ctrl_mappings;
  1620. const struct uvc_control_mapping *mend =
  1621. mapping + ARRAY_SIZE(uvc_ctrl_mappings);
  1622. /* XU controls initialization requires querying the device for control
  1623. * information. As some buggy UVC devices will crash when queried
  1624. * repeatedly in a tight loop, delay XU controls initialization until
  1625. * first use.
  1626. */
  1627. if (UVC_ENTITY_TYPE(ctrl->entity) == UVC_VC_EXTENSION_UNIT)
  1628. return;
  1629. for (; info < iend; ++info) {
  1630. if (uvc_entity_match_guid(ctrl->entity, info->entity) &&
  1631. ctrl->index == info->index) {
  1632. uvc_ctrl_add_info(dev, ctrl, info);
  1633. break;
  1634. }
  1635. }
  1636. if (!ctrl->initialized)
  1637. return;
  1638. for (; mapping < mend; ++mapping) {
  1639. if (uvc_entity_match_guid(ctrl->entity, mapping->entity) &&
  1640. ctrl->info.selector == mapping->selector)
  1641. __uvc_ctrl_add_mapping(dev, ctrl, mapping);
  1642. }
  1643. }
  1644. /*
  1645. * Initialize device controls.
  1646. */
  1647. int uvc_ctrl_init_device(struct uvc_device *dev)
  1648. {
  1649. struct uvc_entity *entity;
  1650. unsigned int i;
  1651. /* Walk the entities list and instantiate controls */
  1652. list_for_each_entry(entity, &dev->entities, list) {
  1653. struct uvc_control *ctrl;
  1654. unsigned int bControlSize = 0, ncontrols = 0;
  1655. __u8 *bmControls = NULL;
  1656. if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
  1657. bmControls = entity->extension.bmControls;
  1658. bControlSize = entity->extension.bControlSize;
  1659. } else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) {
  1660. bmControls = entity->processing.bmControls;
  1661. bControlSize = entity->processing.bControlSize;
  1662. } else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
  1663. bmControls = entity->camera.bmControls;
  1664. bControlSize = entity->camera.bControlSize;
  1665. }
  1666. /* Remove bogus/blacklisted controls */
  1667. uvc_ctrl_prune_entity(dev, entity);
  1668. /* Count supported controls and allocate the controls array */
  1669. for (i = 0; i < bControlSize; ++i)
  1670. ncontrols += hweight8(bmControls[i]);
  1671. if (ncontrols == 0)
  1672. continue;
  1673. entity->controls = kcalloc(ncontrols, sizeof(*ctrl),
  1674. GFP_KERNEL);
  1675. if (entity->controls == NULL)
  1676. return -ENOMEM;
  1677. entity->ncontrols = ncontrols;
  1678. /* Initialize all supported controls */
  1679. ctrl = entity->controls;
  1680. for (i = 0; i < bControlSize * 8; ++i) {
  1681. if (uvc_test_bit(bmControls, i) == 0)
  1682. continue;
  1683. ctrl->entity = entity;
  1684. ctrl->index = i;
  1685. uvc_ctrl_init_ctrl(dev, ctrl);
  1686. ctrl++;
  1687. }
  1688. }
  1689. return 0;
  1690. }
  1691. /*
  1692. * Cleanup device controls.
  1693. */
  1694. static void uvc_ctrl_cleanup_mappings(struct uvc_device *dev,
  1695. struct uvc_control *ctrl)
  1696. {
  1697. struct uvc_control_mapping *mapping, *nm;
  1698. list_for_each_entry_safe(mapping, nm, &ctrl->info.mappings, list) {
  1699. list_del(&mapping->list);
  1700. kfree(mapping->menu_info);
  1701. kfree(mapping);
  1702. }
  1703. }
  1704. void uvc_ctrl_cleanup_device(struct uvc_device *dev)
  1705. {
  1706. struct uvc_entity *entity;
  1707. unsigned int i;
  1708. /* Free controls and control mappings for all entities. */
  1709. list_for_each_entry(entity, &dev->entities, list) {
  1710. for (i = 0; i < entity->ncontrols; ++i) {
  1711. struct uvc_control *ctrl = &entity->controls[i];
  1712. if (!ctrl->initialized)
  1713. continue;
  1714. uvc_ctrl_cleanup_mappings(dev, ctrl);
  1715. kfree(ctrl->uvc_data);
  1716. }
  1717. kfree(entity->controls);
  1718. }
  1719. }