uvc_ctrl.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569
  1. /*
  2. * uvc_ctrl.c -- USB Video Class driver - Controls
  3. *
  4. * Copyright (C) 2005-2009
  5. * Laurent Pinchart (laurent.pinchart@skynet.be)
  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_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  41. | UVC_CONTROL_RESTORE,
  42. },
  43. {
  44. .entity = UVC_GUID_UVC_PROCESSING,
  45. .selector = UVC_PU_CONTRAST_CONTROL,
  46. .index = 1,
  47. .size = 2,
  48. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  49. | UVC_CONTROL_RESTORE,
  50. },
  51. {
  52. .entity = UVC_GUID_UVC_PROCESSING,
  53. .selector = UVC_PU_HUE_CONTROL,
  54. .index = 2,
  55. .size = 2,
  56. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  57. | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
  58. },
  59. {
  60. .entity = UVC_GUID_UVC_PROCESSING,
  61. .selector = UVC_PU_SATURATION_CONTROL,
  62. .index = 3,
  63. .size = 2,
  64. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  65. | UVC_CONTROL_RESTORE,
  66. },
  67. {
  68. .entity = UVC_GUID_UVC_PROCESSING,
  69. .selector = UVC_PU_SHARPNESS_CONTROL,
  70. .index = 4,
  71. .size = 2,
  72. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  73. | UVC_CONTROL_RESTORE,
  74. },
  75. {
  76. .entity = UVC_GUID_UVC_PROCESSING,
  77. .selector = UVC_PU_GAMMA_CONTROL,
  78. .index = 5,
  79. .size = 2,
  80. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  81. | UVC_CONTROL_RESTORE,
  82. },
  83. {
  84. .entity = UVC_GUID_UVC_PROCESSING,
  85. .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
  86. .index = 6,
  87. .size = 2,
  88. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  89. | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
  90. },
  91. {
  92. .entity = UVC_GUID_UVC_PROCESSING,
  93. .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
  94. .index = 7,
  95. .size = 4,
  96. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  97. | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
  98. },
  99. {
  100. .entity = UVC_GUID_UVC_PROCESSING,
  101. .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
  102. .index = 8,
  103. .size = 2,
  104. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  105. | UVC_CONTROL_RESTORE,
  106. },
  107. {
  108. .entity = UVC_GUID_UVC_PROCESSING,
  109. .selector = UVC_PU_GAIN_CONTROL,
  110. .index = 9,
  111. .size = 2,
  112. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  113. | UVC_CONTROL_RESTORE,
  114. },
  115. {
  116. .entity = UVC_GUID_UVC_PROCESSING,
  117. .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
  118. .index = 10,
  119. .size = 1,
  120. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  121. | UVC_CONTROL_RESTORE,
  122. },
  123. {
  124. .entity = UVC_GUID_UVC_PROCESSING,
  125. .selector = UVC_PU_HUE_AUTO_CONTROL,
  126. .index = 11,
  127. .size = 1,
  128. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
  129. | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
  130. },
  131. {
  132. .entity = UVC_GUID_UVC_PROCESSING,
  133. .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
  134. .index = 12,
  135. .size = 1,
  136. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
  137. | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
  138. },
  139. {
  140. .entity = UVC_GUID_UVC_PROCESSING,
  141. .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
  142. .index = 13,
  143. .size = 1,
  144. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
  145. | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
  146. },
  147. {
  148. .entity = UVC_GUID_UVC_PROCESSING,
  149. .selector = UVC_PU_DIGITAL_MULTIPLIER_CONTROL,
  150. .index = 14,
  151. .size = 2,
  152. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  153. | UVC_CONTROL_RESTORE,
  154. },
  155. {
  156. .entity = UVC_GUID_UVC_PROCESSING,
  157. .selector = UVC_PU_DIGITAL_MULTIPLIER_LIMIT_CONTROL,
  158. .index = 15,
  159. .size = 2,
  160. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  161. | UVC_CONTROL_RESTORE,
  162. },
  163. {
  164. .entity = UVC_GUID_UVC_PROCESSING,
  165. .selector = UVC_PU_ANALOG_VIDEO_STANDARD_CONTROL,
  166. .index = 16,
  167. .size = 1,
  168. .flags = UVC_CONTROL_GET_CUR,
  169. },
  170. {
  171. .entity = UVC_GUID_UVC_PROCESSING,
  172. .selector = UVC_PU_ANALOG_LOCK_STATUS_CONTROL,
  173. .index = 17,
  174. .size = 1,
  175. .flags = UVC_CONTROL_GET_CUR,
  176. },
  177. {
  178. .entity = UVC_GUID_UVC_CAMERA,
  179. .selector = UVC_CT_SCANNING_MODE_CONTROL,
  180. .index = 0,
  181. .size = 1,
  182. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
  183. | UVC_CONTROL_RESTORE,
  184. },
  185. {
  186. .entity = UVC_GUID_UVC_CAMERA,
  187. .selector = UVC_CT_AE_MODE_CONTROL,
  188. .index = 1,
  189. .size = 1,
  190. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
  191. | UVC_CONTROL_GET_DEF | UVC_CONTROL_GET_RES
  192. | UVC_CONTROL_RESTORE,
  193. },
  194. {
  195. .entity = UVC_GUID_UVC_CAMERA,
  196. .selector = UVC_CT_AE_PRIORITY_CONTROL,
  197. .index = 2,
  198. .size = 1,
  199. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
  200. | UVC_CONTROL_RESTORE,
  201. },
  202. {
  203. .entity = UVC_GUID_UVC_CAMERA,
  204. .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
  205. .index = 3,
  206. .size = 4,
  207. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  208. | UVC_CONTROL_RESTORE,
  209. },
  210. {
  211. .entity = UVC_GUID_UVC_CAMERA,
  212. .selector = UVC_CT_EXPOSURE_TIME_RELATIVE_CONTROL,
  213. .index = 4,
  214. .size = 1,
  215. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
  216. | UVC_CONTROL_RESTORE,
  217. },
  218. {
  219. .entity = UVC_GUID_UVC_CAMERA,
  220. .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
  221. .index = 5,
  222. .size = 2,
  223. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  224. | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
  225. },
  226. {
  227. .entity = UVC_GUID_UVC_CAMERA,
  228. .selector = UVC_CT_FOCUS_RELATIVE_CONTROL,
  229. .index = 6,
  230. .size = 2,
  231. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  232. | UVC_CONTROL_AUTO_UPDATE,
  233. },
  234. {
  235. .entity = UVC_GUID_UVC_CAMERA,
  236. .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL,
  237. .index = 7,
  238. .size = 2,
  239. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  240. | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
  241. },
  242. {
  243. .entity = UVC_GUID_UVC_CAMERA,
  244. .selector = UVC_CT_IRIS_RELATIVE_CONTROL,
  245. .index = 8,
  246. .size = 1,
  247. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
  248. | UVC_CONTROL_AUTO_UPDATE,
  249. },
  250. {
  251. .entity = UVC_GUID_UVC_CAMERA,
  252. .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
  253. .index = 9,
  254. .size = 2,
  255. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  256. | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
  257. },
  258. {
  259. .entity = UVC_GUID_UVC_CAMERA,
  260. .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
  261. .index = 10,
  262. .size = 3,
  263. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  264. | UVC_CONTROL_AUTO_UPDATE,
  265. },
  266. {
  267. .entity = UVC_GUID_UVC_CAMERA,
  268. .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
  269. .index = 11,
  270. .size = 8,
  271. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  272. | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
  273. },
  274. {
  275. .entity = UVC_GUID_UVC_CAMERA,
  276. .selector = UVC_CT_PANTILT_RELATIVE_CONTROL,
  277. .index = 12,
  278. .size = 4,
  279. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  280. | UVC_CONTROL_AUTO_UPDATE,
  281. },
  282. {
  283. .entity = UVC_GUID_UVC_CAMERA,
  284. .selector = UVC_CT_ROLL_ABSOLUTE_CONTROL,
  285. .index = 13,
  286. .size = 2,
  287. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  288. | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
  289. },
  290. {
  291. .entity = UVC_GUID_UVC_CAMERA,
  292. .selector = UVC_CT_ROLL_RELATIVE_CONTROL,
  293. .index = 14,
  294. .size = 2,
  295. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_RANGE
  296. | UVC_CONTROL_AUTO_UPDATE,
  297. },
  298. {
  299. .entity = UVC_GUID_UVC_CAMERA,
  300. .selector = UVC_CT_FOCUS_AUTO_CONTROL,
  301. .index = 17,
  302. .size = 1,
  303. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
  304. | UVC_CONTROL_GET_DEF | UVC_CONTROL_RESTORE,
  305. },
  306. {
  307. .entity = UVC_GUID_UVC_CAMERA,
  308. .selector = UVC_CT_PRIVACY_CONTROL,
  309. .index = 18,
  310. .size = 1,
  311. .flags = UVC_CONTROL_SET_CUR | UVC_CONTROL_GET_CUR
  312. | UVC_CONTROL_RESTORE | UVC_CONTROL_AUTO_UPDATE,
  313. },
  314. };
  315. static struct uvc_menu_info power_line_frequency_controls[] = {
  316. { 0, "Disabled" },
  317. { 1, "50 Hz" },
  318. { 2, "60 Hz" },
  319. };
  320. static struct uvc_menu_info exposure_auto_controls[] = {
  321. { 2, "Auto Mode" },
  322. { 1, "Manual Mode" },
  323. { 4, "Shutter Priority Mode" },
  324. { 8, "Aperture Priority Mode" },
  325. };
  326. static __s32 uvc_ctrl_get_zoom(struct uvc_control_mapping *mapping,
  327. __u8 query, const __u8 *data)
  328. {
  329. __s8 zoom = (__s8)data[0];
  330. switch (query) {
  331. case UVC_GET_CUR:
  332. return (zoom == 0) ? 0 : (zoom > 0 ? data[2] : -data[2]);
  333. case UVC_GET_MIN:
  334. case UVC_GET_MAX:
  335. case UVC_GET_RES:
  336. case UVC_GET_DEF:
  337. default:
  338. return data[2];
  339. }
  340. }
  341. static void uvc_ctrl_set_zoom(struct uvc_control_mapping *mapping,
  342. __s32 value, __u8 *data)
  343. {
  344. data[0] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
  345. data[2] = min((int)abs(value), 0xff);
  346. }
  347. static struct uvc_control_mapping uvc_ctrl_mappings[] = {
  348. {
  349. .id = V4L2_CID_BRIGHTNESS,
  350. .name = "Brightness",
  351. .entity = UVC_GUID_UVC_PROCESSING,
  352. .selector = UVC_PU_BRIGHTNESS_CONTROL,
  353. .size = 16,
  354. .offset = 0,
  355. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  356. .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
  357. },
  358. {
  359. .id = V4L2_CID_CONTRAST,
  360. .name = "Contrast",
  361. .entity = UVC_GUID_UVC_PROCESSING,
  362. .selector = UVC_PU_CONTRAST_CONTROL,
  363. .size = 16,
  364. .offset = 0,
  365. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  366. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  367. },
  368. {
  369. .id = V4L2_CID_HUE,
  370. .name = "Hue",
  371. .entity = UVC_GUID_UVC_PROCESSING,
  372. .selector = UVC_PU_HUE_CONTROL,
  373. .size = 16,
  374. .offset = 0,
  375. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  376. .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
  377. },
  378. {
  379. .id = V4L2_CID_SATURATION,
  380. .name = "Saturation",
  381. .entity = UVC_GUID_UVC_PROCESSING,
  382. .selector = UVC_PU_SATURATION_CONTROL,
  383. .size = 16,
  384. .offset = 0,
  385. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  386. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  387. },
  388. {
  389. .id = V4L2_CID_SHARPNESS,
  390. .name = "Sharpness",
  391. .entity = UVC_GUID_UVC_PROCESSING,
  392. .selector = UVC_PU_SHARPNESS_CONTROL,
  393. .size = 16,
  394. .offset = 0,
  395. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  396. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  397. },
  398. {
  399. .id = V4L2_CID_GAMMA,
  400. .name = "Gamma",
  401. .entity = UVC_GUID_UVC_PROCESSING,
  402. .selector = UVC_PU_GAMMA_CONTROL,
  403. .size = 16,
  404. .offset = 0,
  405. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  406. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  407. },
  408. {
  409. .id = V4L2_CID_BACKLIGHT_COMPENSATION,
  410. .name = "Backlight Compensation",
  411. .entity = UVC_GUID_UVC_PROCESSING,
  412. .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
  413. .size = 16,
  414. .offset = 0,
  415. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  416. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  417. },
  418. {
  419. .id = V4L2_CID_GAIN,
  420. .name = "Gain",
  421. .entity = UVC_GUID_UVC_PROCESSING,
  422. .selector = UVC_PU_GAIN_CONTROL,
  423. .size = 16,
  424. .offset = 0,
  425. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  426. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  427. },
  428. {
  429. .id = V4L2_CID_POWER_LINE_FREQUENCY,
  430. .name = "Power Line Frequency",
  431. .entity = UVC_GUID_UVC_PROCESSING,
  432. .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
  433. .size = 2,
  434. .offset = 0,
  435. .v4l2_type = V4L2_CTRL_TYPE_MENU,
  436. .data_type = UVC_CTRL_DATA_TYPE_ENUM,
  437. .menu_info = power_line_frequency_controls,
  438. .menu_count = ARRAY_SIZE(power_line_frequency_controls),
  439. },
  440. {
  441. .id = V4L2_CID_HUE_AUTO,
  442. .name = "Hue, Auto",
  443. .entity = UVC_GUID_UVC_PROCESSING,
  444. .selector = UVC_PU_HUE_AUTO_CONTROL,
  445. .size = 1,
  446. .offset = 0,
  447. .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
  448. .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
  449. },
  450. {
  451. .id = V4L2_CID_EXPOSURE_AUTO,
  452. .name = "Exposure, Auto",
  453. .entity = UVC_GUID_UVC_CAMERA,
  454. .selector = UVC_CT_AE_MODE_CONTROL,
  455. .size = 4,
  456. .offset = 0,
  457. .v4l2_type = V4L2_CTRL_TYPE_MENU,
  458. .data_type = UVC_CTRL_DATA_TYPE_BITMASK,
  459. .menu_info = exposure_auto_controls,
  460. .menu_count = ARRAY_SIZE(exposure_auto_controls),
  461. },
  462. {
  463. .id = V4L2_CID_EXPOSURE_AUTO_PRIORITY,
  464. .name = "Exposure, Auto Priority",
  465. .entity = UVC_GUID_UVC_CAMERA,
  466. .selector = UVC_CT_AE_PRIORITY_CONTROL,
  467. .size = 1,
  468. .offset = 0,
  469. .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
  470. .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
  471. },
  472. {
  473. .id = V4L2_CID_EXPOSURE_ABSOLUTE,
  474. .name = "Exposure (Absolute)",
  475. .entity = UVC_GUID_UVC_CAMERA,
  476. .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
  477. .size = 32,
  478. .offset = 0,
  479. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  480. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  481. },
  482. {
  483. .id = V4L2_CID_AUTO_WHITE_BALANCE,
  484. .name = "White Balance Temperature, Auto",
  485. .entity = UVC_GUID_UVC_PROCESSING,
  486. .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
  487. .size = 1,
  488. .offset = 0,
  489. .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
  490. .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
  491. },
  492. {
  493. .id = V4L2_CID_WHITE_BALANCE_TEMPERATURE,
  494. .name = "White Balance Temperature",
  495. .entity = UVC_GUID_UVC_PROCESSING,
  496. .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
  497. .size = 16,
  498. .offset = 0,
  499. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  500. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  501. },
  502. {
  503. .id = V4L2_CID_AUTO_WHITE_BALANCE,
  504. .name = "White Balance Component, Auto",
  505. .entity = UVC_GUID_UVC_PROCESSING,
  506. .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
  507. .size = 1,
  508. .offset = 0,
  509. .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
  510. .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
  511. },
  512. {
  513. .id = V4L2_CID_BLUE_BALANCE,
  514. .name = "White Balance Blue Component",
  515. .entity = UVC_GUID_UVC_PROCESSING,
  516. .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
  517. .size = 16,
  518. .offset = 0,
  519. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  520. .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
  521. },
  522. {
  523. .id = V4L2_CID_RED_BALANCE,
  524. .name = "White Balance Red Component",
  525. .entity = UVC_GUID_UVC_PROCESSING,
  526. .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
  527. .size = 16,
  528. .offset = 16,
  529. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  530. .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
  531. },
  532. {
  533. .id = V4L2_CID_FOCUS_ABSOLUTE,
  534. .name = "Focus (absolute)",
  535. .entity = UVC_GUID_UVC_CAMERA,
  536. .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
  537. .size = 16,
  538. .offset = 0,
  539. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  540. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  541. },
  542. {
  543. .id = V4L2_CID_FOCUS_AUTO,
  544. .name = "Focus, Auto",
  545. .entity = UVC_GUID_UVC_CAMERA,
  546. .selector = UVC_CT_FOCUS_AUTO_CONTROL,
  547. .size = 1,
  548. .offset = 0,
  549. .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
  550. .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
  551. },
  552. {
  553. .id = V4L2_CID_ZOOM_ABSOLUTE,
  554. .name = "Zoom, Absolute",
  555. .entity = UVC_GUID_UVC_CAMERA,
  556. .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
  557. .size = 16,
  558. .offset = 0,
  559. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  560. .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
  561. },
  562. {
  563. .id = V4L2_CID_ZOOM_CONTINUOUS,
  564. .name = "Zoom, Continuous",
  565. .entity = UVC_GUID_UVC_CAMERA,
  566. .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
  567. .size = 0,
  568. .offset = 0,
  569. .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
  570. .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
  571. .get = uvc_ctrl_get_zoom,
  572. .set = uvc_ctrl_set_zoom,
  573. },
  574. {
  575. .id = V4L2_CID_PRIVACY,
  576. .name = "Privacy",
  577. .entity = UVC_GUID_UVC_CAMERA,
  578. .selector = UVC_CT_PRIVACY_CONTROL,
  579. .size = 1,
  580. .offset = 0,
  581. .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
  582. .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
  583. },
  584. };
  585. /* ------------------------------------------------------------------------
  586. * Utility functions
  587. */
  588. static inline __u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id)
  589. {
  590. return ctrl->data + id * ctrl->info->size;
  591. }
  592. static inline int uvc_test_bit(const __u8 *data, int bit)
  593. {
  594. return (data[bit >> 3] >> (bit & 7)) & 1;
  595. }
  596. static inline void uvc_clear_bit(__u8 *data, int bit)
  597. {
  598. data[bit >> 3] &= ~(1 << (bit & 7));
  599. }
  600. /* Extract the bit string specified by mapping->offset and mapping->size
  601. * from the little-endian data stored at 'data' and return the result as
  602. * a signed 32bit integer. Sign extension will be performed if the mapping
  603. * references a signed data type.
  604. */
  605. static __s32 uvc_get_le_value(struct uvc_control_mapping *mapping,
  606. __u8 query, const __u8 *data)
  607. {
  608. int bits = mapping->size;
  609. int offset = mapping->offset;
  610. __s32 value = 0;
  611. __u8 mask;
  612. data += offset / 8;
  613. offset &= 7;
  614. mask = ((1LL << bits) - 1) << offset;
  615. for (; bits > 0; data++) {
  616. __u8 byte = *data & mask;
  617. value |= offset > 0 ? (byte >> offset) : (byte << (-offset));
  618. bits -= 8 - (offset > 0 ? offset : 0);
  619. offset -= 8;
  620. mask = (1 << bits) - 1;
  621. }
  622. /* Sign-extend the value if needed. */
  623. if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
  624. value |= -(value & (1 << (mapping->size - 1)));
  625. return value;
  626. }
  627. /* Set the bit string specified by mapping->offset and mapping->size
  628. * in the little-endian data stored at 'data' to the value 'value'.
  629. */
  630. static void uvc_set_le_value(struct uvc_control_mapping *mapping,
  631. __s32 value, __u8 *data)
  632. {
  633. int bits = mapping->size;
  634. int offset = mapping->offset;
  635. __u8 mask;
  636. data += offset / 8;
  637. offset &= 7;
  638. for (; bits > 0; data++) {
  639. mask = ((1LL << bits) - 1) << offset;
  640. *data = (*data & ~mask) | ((value << offset) & mask);
  641. value >>= offset ? offset : 8;
  642. bits -= 8 - offset;
  643. offset = 0;
  644. }
  645. }
  646. /* ------------------------------------------------------------------------
  647. * Terminal and unit management
  648. */
  649. static const __u8 uvc_processing_guid[16] = UVC_GUID_UVC_PROCESSING;
  650. static const __u8 uvc_camera_guid[16] = UVC_GUID_UVC_CAMERA;
  651. static const __u8 uvc_media_transport_input_guid[16] =
  652. UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT;
  653. static int uvc_entity_match_guid(struct uvc_entity *entity, __u8 guid[16])
  654. {
  655. switch (UVC_ENTITY_TYPE(entity)) {
  656. case UVC_ITT_CAMERA:
  657. return memcmp(uvc_camera_guid, guid, 16) == 0;
  658. case UVC_ITT_MEDIA_TRANSPORT_INPUT:
  659. return memcmp(uvc_media_transport_input_guid, guid, 16) == 0;
  660. case UVC_VC_PROCESSING_UNIT:
  661. return memcmp(uvc_processing_guid, guid, 16) == 0;
  662. case UVC_VC_EXTENSION_UNIT:
  663. return memcmp(entity->extension.guidExtensionCode,
  664. guid, 16) == 0;
  665. default:
  666. return 0;
  667. }
  668. }
  669. /* ------------------------------------------------------------------------
  670. * UVC Controls
  671. */
  672. static void __uvc_find_control(struct uvc_entity *entity, __u32 v4l2_id,
  673. struct uvc_control_mapping **mapping, struct uvc_control **control,
  674. int next)
  675. {
  676. struct uvc_control *ctrl;
  677. struct uvc_control_mapping *map;
  678. unsigned int i;
  679. if (entity == NULL)
  680. return;
  681. for (i = 0; i < entity->ncontrols; ++i) {
  682. ctrl = &entity->controls[i];
  683. if (ctrl->info == NULL)
  684. continue;
  685. list_for_each_entry(map, &ctrl->info->mappings, list) {
  686. if ((map->id == v4l2_id) && !next) {
  687. *control = ctrl;
  688. *mapping = map;
  689. return;
  690. }
  691. if ((*mapping == NULL || (*mapping)->id > map->id) &&
  692. (map->id > v4l2_id) && next) {
  693. *control = ctrl;
  694. *mapping = map;
  695. }
  696. }
  697. }
  698. }
  699. struct uvc_control *uvc_find_control(struct uvc_video_chain *chain,
  700. __u32 v4l2_id, struct uvc_control_mapping **mapping)
  701. {
  702. struct uvc_control *ctrl = NULL;
  703. struct uvc_entity *entity;
  704. int next = v4l2_id & V4L2_CTRL_FLAG_NEXT_CTRL;
  705. *mapping = NULL;
  706. /* Mask the query flags. */
  707. v4l2_id &= V4L2_CTRL_ID_MASK;
  708. /* Find the control. */
  709. list_for_each_entry(entity, &chain->entities, chain) {
  710. __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next);
  711. if (ctrl && !next)
  712. return ctrl;
  713. }
  714. if (ctrl == NULL && !next)
  715. uvc_trace(UVC_TRACE_CONTROL, "Control 0x%08x not found.\n",
  716. v4l2_id);
  717. return ctrl;
  718. }
  719. static int uvc_ctrl_populate_cache(struct uvc_video_chain *chain,
  720. struct uvc_control *ctrl)
  721. {
  722. int ret;
  723. if (ctrl->info->flags & UVC_CONTROL_GET_DEF) {
  724. ret = uvc_query_ctrl(chain->dev, UVC_GET_DEF, ctrl->entity->id,
  725. chain->dev->intfnum, ctrl->info->selector,
  726. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF),
  727. ctrl->info->size);
  728. if (ret < 0)
  729. return ret;
  730. }
  731. if (ctrl->info->flags & UVC_CONTROL_GET_MIN) {
  732. ret = uvc_query_ctrl(chain->dev, UVC_GET_MIN, ctrl->entity->id,
  733. chain->dev->intfnum, ctrl->info->selector,
  734. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN),
  735. ctrl->info->size);
  736. if (ret < 0)
  737. return ret;
  738. }
  739. if (ctrl->info->flags & UVC_CONTROL_GET_MAX) {
  740. ret = uvc_query_ctrl(chain->dev, UVC_GET_MAX, ctrl->entity->id,
  741. chain->dev->intfnum, ctrl->info->selector,
  742. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX),
  743. ctrl->info->size);
  744. if (ret < 0)
  745. return ret;
  746. }
  747. if (ctrl->info->flags & UVC_CONTROL_GET_RES) {
  748. ret = uvc_query_ctrl(chain->dev, UVC_GET_RES, ctrl->entity->id,
  749. chain->dev->intfnum, ctrl->info->selector,
  750. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES),
  751. ctrl->info->size);
  752. if (ret < 0)
  753. return ret;
  754. }
  755. ctrl->cached = 1;
  756. return 0;
  757. }
  758. int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
  759. struct v4l2_queryctrl *v4l2_ctrl)
  760. {
  761. struct uvc_control *ctrl;
  762. struct uvc_control_mapping *mapping;
  763. struct uvc_menu_info *menu;
  764. unsigned int i;
  765. int ret;
  766. ctrl = uvc_find_control(chain, v4l2_ctrl->id, &mapping);
  767. if (ctrl == NULL)
  768. return -EINVAL;
  769. memset(v4l2_ctrl, 0, sizeof *v4l2_ctrl);
  770. v4l2_ctrl->id = mapping->id;
  771. v4l2_ctrl->type = mapping->v4l2_type;
  772. strlcpy(v4l2_ctrl->name, mapping->name, sizeof v4l2_ctrl->name);
  773. v4l2_ctrl->flags = 0;
  774. if (!(ctrl->info->flags & UVC_CONTROL_SET_CUR))
  775. v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  776. if (!ctrl->cached) {
  777. ret = uvc_ctrl_populate_cache(chain, ctrl);
  778. if (ret < 0)
  779. return ret;
  780. }
  781. if (ctrl->info->flags & UVC_CONTROL_GET_DEF) {
  782. v4l2_ctrl->default_value = mapping->get(mapping, UVC_GET_DEF,
  783. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF));
  784. }
  785. switch (mapping->v4l2_type) {
  786. case V4L2_CTRL_TYPE_MENU:
  787. v4l2_ctrl->minimum = 0;
  788. v4l2_ctrl->maximum = mapping->menu_count - 1;
  789. v4l2_ctrl->step = 1;
  790. menu = mapping->menu_info;
  791. for (i = 0; i < mapping->menu_count; ++i, ++menu) {
  792. if (menu->value == v4l2_ctrl->default_value) {
  793. v4l2_ctrl->default_value = i;
  794. break;
  795. }
  796. }
  797. return 0;
  798. case V4L2_CTRL_TYPE_BOOLEAN:
  799. v4l2_ctrl->minimum = 0;
  800. v4l2_ctrl->maximum = 1;
  801. v4l2_ctrl->step = 1;
  802. return 0;
  803. case V4L2_CTRL_TYPE_BUTTON:
  804. v4l2_ctrl->minimum = 0;
  805. v4l2_ctrl->maximum = 0;
  806. v4l2_ctrl->step = 0;
  807. return 0;
  808. default:
  809. break;
  810. }
  811. if (ctrl->info->flags & UVC_CONTROL_GET_MIN)
  812. v4l2_ctrl->minimum = mapping->get(mapping, UVC_GET_MIN,
  813. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
  814. if (ctrl->info->flags & UVC_CONTROL_GET_MAX)
  815. v4l2_ctrl->maximum = mapping->get(mapping, UVC_GET_MAX,
  816. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
  817. if (ctrl->info->flags & UVC_CONTROL_GET_RES)
  818. v4l2_ctrl->step = mapping->get(mapping, UVC_GET_RES,
  819. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
  820. return 0;
  821. }
  822. /* --------------------------------------------------------------------------
  823. * Control transactions
  824. *
  825. * To make extended set operations as atomic as the hardware allows, controls
  826. * are handled using begin/commit/rollback operations.
  827. *
  828. * At the beginning of a set request, uvc_ctrl_begin should be called to
  829. * initialize the request. This function acquires the control lock.
  830. *
  831. * When setting a control, the new value is stored in the control data field
  832. * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
  833. * later processing. If the UVC and V4L2 control sizes differ, the current
  834. * value is loaded from the hardware before storing the new value in the data
  835. * field.
  836. *
  837. * After processing all controls in the transaction, uvc_ctrl_commit or
  838. * uvc_ctrl_rollback must be called to apply the pending changes to the
  839. * hardware or revert them. When applying changes, all controls marked as
  840. * dirty will be modified in the UVC device, and the dirty flag will be
  841. * cleared. When reverting controls, the control data field
  842. * UVC_CTRL_DATA_CURRENT is reverted to its previous value
  843. * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
  844. * control lock.
  845. */
  846. int uvc_ctrl_begin(struct uvc_video_chain *chain)
  847. {
  848. return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0;
  849. }
  850. static int uvc_ctrl_commit_entity(struct uvc_device *dev,
  851. struct uvc_entity *entity, int rollback)
  852. {
  853. struct uvc_control *ctrl;
  854. unsigned int i;
  855. int ret;
  856. if (entity == NULL)
  857. return 0;
  858. for (i = 0; i < entity->ncontrols; ++i) {
  859. ctrl = &entity->controls[i];
  860. if (ctrl->info == NULL)
  861. continue;
  862. /* Reset the loaded flag for auto-update controls that were
  863. * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
  864. * uvc_ctrl_get from using the cached value.
  865. */
  866. if (ctrl->info->flags & UVC_CONTROL_AUTO_UPDATE)
  867. ctrl->loaded = 0;
  868. if (!ctrl->dirty)
  869. continue;
  870. if (!rollback)
  871. ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id,
  872. dev->intfnum, ctrl->info->selector,
  873. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  874. ctrl->info->size);
  875. else
  876. ret = 0;
  877. if (rollback || ret < 0)
  878. memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  879. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
  880. ctrl->info->size);
  881. ctrl->dirty = 0;
  882. if (ret < 0)
  883. return ret;
  884. }
  885. return 0;
  886. }
  887. int __uvc_ctrl_commit(struct uvc_video_chain *chain, int rollback)
  888. {
  889. struct uvc_entity *entity;
  890. int ret = 0;
  891. /* Find the control. */
  892. list_for_each_entry(entity, &chain->entities, chain) {
  893. ret = uvc_ctrl_commit_entity(chain->dev, entity, rollback);
  894. if (ret < 0)
  895. goto done;
  896. }
  897. done:
  898. mutex_unlock(&chain->ctrl_mutex);
  899. return ret;
  900. }
  901. int uvc_ctrl_get(struct uvc_video_chain *chain,
  902. struct v4l2_ext_control *xctrl)
  903. {
  904. struct uvc_control *ctrl;
  905. struct uvc_control_mapping *mapping;
  906. struct uvc_menu_info *menu;
  907. unsigned int i;
  908. int ret;
  909. ctrl = uvc_find_control(chain, xctrl->id, &mapping);
  910. if (ctrl == NULL || (ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0)
  911. return -EINVAL;
  912. if (!ctrl->loaded) {
  913. ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, ctrl->entity->id,
  914. chain->dev->intfnum, ctrl->info->selector,
  915. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  916. ctrl->info->size);
  917. if (ret < 0)
  918. return ret;
  919. ctrl->loaded = 1;
  920. }
  921. xctrl->value = mapping->get(mapping, UVC_GET_CUR,
  922. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
  923. if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
  924. menu = mapping->menu_info;
  925. for (i = 0; i < mapping->menu_count; ++i, ++menu) {
  926. if (menu->value == xctrl->value) {
  927. xctrl->value = i;
  928. break;
  929. }
  930. }
  931. }
  932. return 0;
  933. }
  934. int uvc_ctrl_set(struct uvc_video_chain *chain,
  935. struct v4l2_ext_control *xctrl)
  936. {
  937. struct uvc_control *ctrl;
  938. struct uvc_control_mapping *mapping;
  939. s32 value;
  940. u32 step;
  941. s32 min;
  942. s32 max;
  943. int ret;
  944. ctrl = uvc_find_control(chain, xctrl->id, &mapping);
  945. if (ctrl == NULL || (ctrl->info->flags & UVC_CONTROL_SET_CUR) == 0)
  946. return -EINVAL;
  947. /* Clamp out of range values. */
  948. switch (mapping->v4l2_type) {
  949. case V4L2_CTRL_TYPE_INTEGER:
  950. if (!ctrl->cached) {
  951. ret = uvc_ctrl_populate_cache(chain, ctrl);
  952. if (ret < 0)
  953. return ret;
  954. }
  955. min = mapping->get(mapping, UVC_GET_MIN,
  956. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
  957. max = mapping->get(mapping, UVC_GET_MAX,
  958. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
  959. step = mapping->get(mapping, UVC_GET_RES,
  960. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
  961. xctrl->value = min + (xctrl->value - min + step/2) / step * step;
  962. xctrl->value = clamp(xctrl->value, min, max);
  963. value = xctrl->value;
  964. break;
  965. case V4L2_CTRL_TYPE_BOOLEAN:
  966. xctrl->value = clamp(xctrl->value, 0, 1);
  967. value = xctrl->value;
  968. break;
  969. case V4L2_CTRL_TYPE_MENU:
  970. if (xctrl->value < 0 || xctrl->value >= mapping->menu_count)
  971. return -ERANGE;
  972. value = mapping->menu_info[xctrl->value].value;
  973. break;
  974. default:
  975. value = xctrl->value;
  976. break;
  977. }
  978. /* If the mapping doesn't span the whole UVC control, the current value
  979. * needs to be loaded from the device to perform the read-modify-write
  980. * operation.
  981. */
  982. if (!ctrl->loaded && (ctrl->info->size * 8) != mapping->size) {
  983. if ((ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0) {
  984. memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  985. 0, ctrl->info->size);
  986. } else {
  987. ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
  988. ctrl->entity->id, chain->dev->intfnum,
  989. ctrl->info->selector,
  990. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  991. ctrl->info->size);
  992. if (ret < 0)
  993. return ret;
  994. }
  995. ctrl->loaded = 1;
  996. }
  997. /* Backup the current value in case we need to rollback later. */
  998. if (!ctrl->dirty) {
  999. memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
  1000. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  1001. ctrl->info->size);
  1002. }
  1003. mapping->set(mapping, value,
  1004. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
  1005. ctrl->dirty = 1;
  1006. ctrl->modified = 1;
  1007. return 0;
  1008. }
  1009. /* --------------------------------------------------------------------------
  1010. * Dynamic controls
  1011. */
  1012. int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
  1013. struct uvc_xu_control *xctrl, int set)
  1014. {
  1015. struct uvc_entity *entity;
  1016. struct uvc_control *ctrl = NULL;
  1017. unsigned int i, found = 0;
  1018. __u8 *data;
  1019. int ret;
  1020. /* Find the extension unit. */
  1021. list_for_each_entry(entity, &chain->entities, chain) {
  1022. if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT &&
  1023. entity->id == xctrl->unit)
  1024. break;
  1025. }
  1026. if (entity->id != xctrl->unit) {
  1027. uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
  1028. xctrl->unit);
  1029. return -EINVAL;
  1030. }
  1031. /* Find the control. */
  1032. for (i = 0; i < entity->ncontrols; ++i) {
  1033. ctrl = &entity->controls[i];
  1034. if (ctrl->info == NULL)
  1035. continue;
  1036. if (ctrl->info->selector == xctrl->selector) {
  1037. found = 1;
  1038. break;
  1039. }
  1040. }
  1041. if (!found) {
  1042. uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u not found.\n",
  1043. entity->extension.guidExtensionCode, xctrl->selector);
  1044. return -EINVAL;
  1045. }
  1046. /* Validate control data size. */
  1047. if (ctrl->info->size != xctrl->size)
  1048. return -EINVAL;
  1049. if ((set && !(ctrl->info->flags & UVC_CONTROL_SET_CUR)) ||
  1050. (!set && !(ctrl->info->flags & UVC_CONTROL_GET_CUR)))
  1051. return -EINVAL;
  1052. if (mutex_lock_interruptible(&chain->ctrl_mutex))
  1053. return -ERESTARTSYS;
  1054. memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
  1055. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  1056. xctrl->size);
  1057. data = uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT);
  1058. if (set && copy_from_user(data, xctrl->data, xctrl->size)) {
  1059. ret = -EFAULT;
  1060. goto out;
  1061. }
  1062. ret = uvc_query_ctrl(chain->dev, set ? UVC_SET_CUR : UVC_GET_CUR,
  1063. xctrl->unit, chain->dev->intfnum, xctrl->selector,
  1064. data, xctrl->size);
  1065. if (ret < 0)
  1066. goto out;
  1067. if (!set && copy_to_user(xctrl->data, data, xctrl->size)) {
  1068. ret = -EFAULT;
  1069. goto out;
  1070. }
  1071. out:
  1072. if (ret)
  1073. memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  1074. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
  1075. xctrl->size);
  1076. mutex_unlock(&chain->ctrl_mutex);
  1077. return ret;
  1078. }
  1079. /* --------------------------------------------------------------------------
  1080. * Suspend/resume
  1081. */
  1082. /*
  1083. * Restore control values after resume, skipping controls that haven't been
  1084. * changed.
  1085. *
  1086. * TODO
  1087. * - Don't restore modified controls that are back to their default value.
  1088. * - Handle restore order (Auto-Exposure Mode should be restored before
  1089. * Exposure Time).
  1090. */
  1091. int uvc_ctrl_resume_device(struct uvc_device *dev)
  1092. {
  1093. struct uvc_control *ctrl;
  1094. struct uvc_entity *entity;
  1095. unsigned int i;
  1096. int ret;
  1097. /* Walk the entities list and restore controls when possible. */
  1098. list_for_each_entry(entity, &dev->entities, list) {
  1099. for (i = 0; i < entity->ncontrols; ++i) {
  1100. ctrl = &entity->controls[i];
  1101. if (ctrl->info == NULL || !ctrl->modified ||
  1102. (ctrl->info->flags & UVC_CONTROL_RESTORE) == 0)
  1103. continue;
  1104. printk(KERN_INFO "restoring control %pUl/%u/%u\n",
  1105. ctrl->info->entity, ctrl->info->index,
  1106. ctrl->info->selector);
  1107. ctrl->dirty = 1;
  1108. }
  1109. ret = uvc_ctrl_commit_entity(dev, entity, 0);
  1110. if (ret < 0)
  1111. return ret;
  1112. }
  1113. return 0;
  1114. }
  1115. /* --------------------------------------------------------------------------
  1116. * Control and mapping handling
  1117. */
  1118. static void uvc_ctrl_add_ctrl(struct uvc_device *dev,
  1119. struct uvc_control_info *info)
  1120. {
  1121. struct uvc_entity *entity;
  1122. struct uvc_control *ctrl = NULL;
  1123. int ret, found = 0;
  1124. unsigned int i;
  1125. list_for_each_entry(entity, &dev->entities, list) {
  1126. if (!uvc_entity_match_guid(entity, info->entity))
  1127. continue;
  1128. for (i = 0; i < entity->ncontrols; ++i) {
  1129. ctrl = &entity->controls[i];
  1130. if (ctrl->index == info->index) {
  1131. found = 1;
  1132. break;
  1133. }
  1134. }
  1135. if (found)
  1136. break;
  1137. }
  1138. if (!found)
  1139. return;
  1140. if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
  1141. /* Check if the device control information and length match
  1142. * the user supplied information.
  1143. */
  1144. __u32 flags;
  1145. __le16 size;
  1146. __u8 inf;
  1147. ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id,
  1148. dev->intfnum, info->selector, (__u8 *)&size, 2);
  1149. if (ret < 0) {
  1150. uvc_trace(UVC_TRACE_CONTROL,
  1151. "GET_LEN failed on control %pUl/%u (%d).\n",
  1152. info->entity, info->selector, ret);
  1153. return;
  1154. }
  1155. if (info->size != le16_to_cpu(size)) {
  1156. uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u size "
  1157. "doesn't match user supplied value.\n",
  1158. info->entity, info->selector);
  1159. return;
  1160. }
  1161. ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id,
  1162. dev->intfnum, info->selector, &inf, 1);
  1163. if (ret < 0) {
  1164. uvc_trace(UVC_TRACE_CONTROL,
  1165. "GET_INFO failed on control %pUl/%u (%d).\n",
  1166. info->entity, info->selector, ret);
  1167. return;
  1168. }
  1169. flags = info->flags;
  1170. if (((flags & UVC_CONTROL_GET_CUR) && !(inf & (1 << 0))) ||
  1171. ((flags & UVC_CONTROL_SET_CUR) && !(inf & (1 << 1)))) {
  1172. uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u flags "
  1173. "don't match supported operations.\n",
  1174. info->entity, info->selector);
  1175. return;
  1176. }
  1177. }
  1178. ctrl->info = info;
  1179. ctrl->data = kmalloc(ctrl->info->size * UVC_CTRL_DATA_LAST, GFP_KERNEL);
  1180. uvc_trace(UVC_TRACE_CONTROL, "Added control %pUl/%u to device %s "
  1181. "entity %u\n", ctrl->info->entity, ctrl->info->selector,
  1182. dev->udev->devpath, entity->id);
  1183. }
  1184. /*
  1185. * Add an item to the UVC control information list, and instantiate a control
  1186. * structure for each device that supports the control.
  1187. */
  1188. int uvc_ctrl_add_info(struct uvc_control_info *info)
  1189. {
  1190. struct uvc_control_info *ctrl;
  1191. struct uvc_device *dev;
  1192. int ret = 0;
  1193. /* Find matching controls by walking the devices, entities and
  1194. * controls list.
  1195. */
  1196. mutex_lock(&uvc_driver.ctrl_mutex);
  1197. /* First check if the list contains a control matching the new one.
  1198. * Bail out if it does.
  1199. */
  1200. list_for_each_entry(ctrl, &uvc_driver.controls, list) {
  1201. if (memcmp(ctrl->entity, info->entity, 16))
  1202. continue;
  1203. if (ctrl->selector == info->selector) {
  1204. uvc_trace(UVC_TRACE_CONTROL,
  1205. "Control %pUl/%u is already defined.\n",
  1206. info->entity, info->selector);
  1207. ret = -EEXIST;
  1208. goto end;
  1209. }
  1210. if (ctrl->index == info->index) {
  1211. uvc_trace(UVC_TRACE_CONTROL,
  1212. "Control %pUl/%u would overwrite index %d.\n",
  1213. info->entity, info->selector, info->index);
  1214. ret = -EEXIST;
  1215. goto end;
  1216. }
  1217. }
  1218. list_for_each_entry(dev, &uvc_driver.devices, list)
  1219. uvc_ctrl_add_ctrl(dev, info);
  1220. INIT_LIST_HEAD(&info->mappings);
  1221. list_add_tail(&info->list, &uvc_driver.controls);
  1222. end:
  1223. mutex_unlock(&uvc_driver.ctrl_mutex);
  1224. return ret;
  1225. }
  1226. int uvc_ctrl_add_mapping(struct uvc_control_mapping *mapping)
  1227. {
  1228. struct uvc_control_info *info;
  1229. struct uvc_control_mapping *map;
  1230. int ret = -EINVAL;
  1231. if (mapping->get == NULL)
  1232. mapping->get = uvc_get_le_value;
  1233. if (mapping->set == NULL)
  1234. mapping->set = uvc_set_le_value;
  1235. if (mapping->id & ~V4L2_CTRL_ID_MASK) {
  1236. uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s' with "
  1237. "invalid control id 0x%08x\n", mapping->name,
  1238. mapping->id);
  1239. return -EINVAL;
  1240. }
  1241. mutex_lock(&uvc_driver.ctrl_mutex);
  1242. list_for_each_entry(info, &uvc_driver.controls, list) {
  1243. if (memcmp(info->entity, mapping->entity, 16) ||
  1244. info->selector != mapping->selector)
  1245. continue;
  1246. if (info->size * 8 < mapping->size + mapping->offset) {
  1247. uvc_trace(UVC_TRACE_CONTROL,
  1248. "Mapping '%s' would overflow control %pUl/%u\n",
  1249. mapping->name, info->entity, info->selector);
  1250. ret = -EOVERFLOW;
  1251. goto end;
  1252. }
  1253. /* Check if the list contains a mapping matching the new one.
  1254. * Bail out if it does.
  1255. */
  1256. list_for_each_entry(map, &info->mappings, list) {
  1257. if (map->id == mapping->id) {
  1258. uvc_trace(UVC_TRACE_CONTROL, "Mapping '%s' is "
  1259. "already defined.\n", mapping->name);
  1260. ret = -EEXIST;
  1261. goto end;
  1262. }
  1263. }
  1264. mapping->ctrl = info;
  1265. list_add_tail(&mapping->list, &info->mappings);
  1266. uvc_trace(UVC_TRACE_CONTROL,
  1267. "Adding mapping %s to control %pUl/%u.\n",
  1268. mapping->name, info->entity, info->selector);
  1269. ret = 0;
  1270. break;
  1271. }
  1272. end:
  1273. mutex_unlock(&uvc_driver.ctrl_mutex);
  1274. return ret;
  1275. }
  1276. /*
  1277. * Prune an entity of its bogus controls using a blacklist. Bogus controls
  1278. * are currently the ones that crash the camera or unconditionally return an
  1279. * error when queried.
  1280. */
  1281. static void
  1282. uvc_ctrl_prune_entity(struct uvc_device *dev, struct uvc_entity *entity)
  1283. {
  1284. static const struct {
  1285. struct usb_device_id id;
  1286. u8 index;
  1287. } blacklist[] = {
  1288. { { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */
  1289. { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
  1290. { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
  1291. };
  1292. u8 *controls;
  1293. unsigned int size;
  1294. unsigned int i;
  1295. if (UVC_ENTITY_TYPE(entity) != UVC_VC_PROCESSING_UNIT)
  1296. return;
  1297. controls = entity->processing.bmControls;
  1298. size = entity->processing.bControlSize;
  1299. for (i = 0; i < ARRAY_SIZE(blacklist); ++i) {
  1300. if (!usb_match_one_id(dev->intf, &blacklist[i].id))
  1301. continue;
  1302. if (blacklist[i].index >= 8 * size ||
  1303. !uvc_test_bit(controls, blacklist[i].index))
  1304. continue;
  1305. uvc_trace(UVC_TRACE_CONTROL, "%u/%u control is black listed, "
  1306. "removing it.\n", entity->id, blacklist[i].index);
  1307. uvc_clear_bit(controls, blacklist[i].index);
  1308. }
  1309. }
  1310. /*
  1311. * Initialize device controls.
  1312. */
  1313. int uvc_ctrl_init_device(struct uvc_device *dev)
  1314. {
  1315. struct uvc_control_info *info;
  1316. struct uvc_control *ctrl;
  1317. struct uvc_entity *entity;
  1318. unsigned int i;
  1319. /* Walk the entities list and instantiate controls */
  1320. list_for_each_entry(entity, &dev->entities, list) {
  1321. unsigned int bControlSize = 0, ncontrols = 0;
  1322. __u8 *bmControls = NULL;
  1323. if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
  1324. bmControls = entity->extension.bmControls;
  1325. bControlSize = entity->extension.bControlSize;
  1326. } else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) {
  1327. bmControls = entity->processing.bmControls;
  1328. bControlSize = entity->processing.bControlSize;
  1329. } else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
  1330. bmControls = entity->camera.bmControls;
  1331. bControlSize = entity->camera.bControlSize;
  1332. }
  1333. uvc_ctrl_prune_entity(dev, entity);
  1334. for (i = 0; i < bControlSize; ++i)
  1335. ncontrols += hweight8(bmControls[i]);
  1336. if (ncontrols == 0)
  1337. continue;
  1338. entity->controls = kzalloc(ncontrols*sizeof *ctrl, GFP_KERNEL);
  1339. if (entity->controls == NULL)
  1340. return -ENOMEM;
  1341. entity->ncontrols = ncontrols;
  1342. ctrl = entity->controls;
  1343. for (i = 0; i < bControlSize * 8; ++i) {
  1344. if (uvc_test_bit(bmControls, i) == 0)
  1345. continue;
  1346. ctrl->entity = entity;
  1347. ctrl->index = i;
  1348. ctrl++;
  1349. }
  1350. }
  1351. /* Walk the controls info list and associate them with the device
  1352. * controls, then add the device to the global device list. This has
  1353. * to be done while holding the controls lock, to make sure
  1354. * uvc_ctrl_add_info() will not get called in-between.
  1355. */
  1356. mutex_lock(&uvc_driver.ctrl_mutex);
  1357. list_for_each_entry(info, &uvc_driver.controls, list)
  1358. uvc_ctrl_add_ctrl(dev, info);
  1359. list_add_tail(&dev->list, &uvc_driver.devices);
  1360. mutex_unlock(&uvc_driver.ctrl_mutex);
  1361. return 0;
  1362. }
  1363. /*
  1364. * Cleanup device controls.
  1365. */
  1366. void uvc_ctrl_cleanup_device(struct uvc_device *dev)
  1367. {
  1368. struct uvc_entity *entity;
  1369. unsigned int i;
  1370. /* Remove the device from the global devices list */
  1371. mutex_lock(&uvc_driver.ctrl_mutex);
  1372. if (dev->list.next != NULL)
  1373. list_del(&dev->list);
  1374. mutex_unlock(&uvc_driver.ctrl_mutex);
  1375. list_for_each_entry(entity, &dev->entities, list) {
  1376. for (i = 0; i < entity->ncontrols; ++i)
  1377. kfree(entity->controls[i].data);
  1378. kfree(entity->controls);
  1379. }
  1380. }
  1381. void uvc_ctrl_init(void)
  1382. {
  1383. struct uvc_control_info *ctrl = uvc_ctrls;
  1384. struct uvc_control_info *cend = ctrl + ARRAY_SIZE(uvc_ctrls);
  1385. struct uvc_control_mapping *mapping = uvc_ctrl_mappings;
  1386. struct uvc_control_mapping *mend =
  1387. mapping + ARRAY_SIZE(uvc_ctrl_mappings);
  1388. for (; ctrl < cend; ++ctrl)
  1389. uvc_ctrl_add_info(ctrl);
  1390. for (; mapping < mend; ++mapping)
  1391. uvc_ctrl_add_mapping(mapping);
  1392. }