uvc_ctrl.c 39 KB

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