uvc_ctrl.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512
  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((int)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. list_for_each_entry(entity, &chain->entities, chain) {
  705. __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next);
  706. if (ctrl && !next)
  707. return ctrl;
  708. }
  709. if (ctrl == NULL && !next)
  710. uvc_trace(UVC_TRACE_CONTROL, "Control 0x%08x not found.\n",
  711. v4l2_id);
  712. return ctrl;
  713. }
  714. int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
  715. struct v4l2_queryctrl *v4l2_ctrl)
  716. {
  717. struct uvc_control *ctrl;
  718. struct uvc_control_mapping *mapping;
  719. struct uvc_menu_info *menu;
  720. unsigned int i;
  721. __u8 *data;
  722. int ret;
  723. ctrl = uvc_find_control(chain, v4l2_ctrl->id, &mapping);
  724. if (ctrl == NULL)
  725. return -EINVAL;
  726. data = kmalloc(ctrl->info->size, GFP_KERNEL);
  727. if (data == NULL)
  728. return -ENOMEM;
  729. memset(v4l2_ctrl, 0, sizeof *v4l2_ctrl);
  730. v4l2_ctrl->id = mapping->id;
  731. v4l2_ctrl->type = mapping->v4l2_type;
  732. strlcpy(v4l2_ctrl->name, mapping->name, sizeof v4l2_ctrl->name);
  733. v4l2_ctrl->flags = 0;
  734. if (!(ctrl->info->flags & UVC_CONTROL_SET_CUR))
  735. v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  736. if (ctrl->info->flags & UVC_CONTROL_GET_DEF) {
  737. ret = uvc_query_ctrl(chain->dev, UVC_GET_DEF, ctrl->entity->id,
  738. chain->dev->intfnum, ctrl->info->selector,
  739. data, ctrl->info->size);
  740. if (ret < 0)
  741. goto out;
  742. v4l2_ctrl->default_value =
  743. mapping->get(mapping, UVC_GET_DEF, data);
  744. }
  745. switch (mapping->v4l2_type) {
  746. case V4L2_CTRL_TYPE_MENU:
  747. v4l2_ctrl->minimum = 0;
  748. v4l2_ctrl->maximum = mapping->menu_count - 1;
  749. v4l2_ctrl->step = 1;
  750. menu = mapping->menu_info;
  751. for (i = 0; i < mapping->menu_count; ++i, ++menu) {
  752. if (menu->value == v4l2_ctrl->default_value) {
  753. v4l2_ctrl->default_value = i;
  754. break;
  755. }
  756. }
  757. ret = 0;
  758. goto out;
  759. case V4L2_CTRL_TYPE_BOOLEAN:
  760. v4l2_ctrl->minimum = 0;
  761. v4l2_ctrl->maximum = 1;
  762. v4l2_ctrl->step = 1;
  763. ret = 0;
  764. goto out;
  765. case V4L2_CTRL_TYPE_BUTTON:
  766. v4l2_ctrl->minimum = 0;
  767. v4l2_ctrl->maximum = 0;
  768. v4l2_ctrl->step = 0;
  769. ret = 0;
  770. goto out;
  771. default:
  772. break;
  773. }
  774. if (ctrl->info->flags & UVC_CONTROL_GET_MIN) {
  775. ret = uvc_query_ctrl(chain->dev, UVC_GET_MIN, ctrl->entity->id,
  776. chain->dev->intfnum, ctrl->info->selector,
  777. data, ctrl->info->size);
  778. if (ret < 0)
  779. goto out;
  780. v4l2_ctrl->minimum = mapping->get(mapping, UVC_GET_MIN, data);
  781. }
  782. if (ctrl->info->flags & UVC_CONTROL_GET_MAX) {
  783. ret = uvc_query_ctrl(chain->dev, UVC_GET_MAX, ctrl->entity->id,
  784. chain->dev->intfnum, ctrl->info->selector,
  785. data, ctrl->info->size);
  786. if (ret < 0)
  787. goto out;
  788. v4l2_ctrl->maximum = mapping->get(mapping, UVC_GET_MAX, data);
  789. }
  790. if (ctrl->info->flags & UVC_CONTROL_GET_RES) {
  791. ret = uvc_query_ctrl(chain->dev, UVC_GET_RES, ctrl->entity->id,
  792. chain->dev->intfnum, ctrl->info->selector,
  793. data, ctrl->info->size);
  794. if (ret < 0)
  795. goto out;
  796. v4l2_ctrl->step = mapping->get(mapping, UVC_GET_RES, data);
  797. }
  798. ret = 0;
  799. out:
  800. kfree(data);
  801. return ret;
  802. }
  803. /* --------------------------------------------------------------------------
  804. * Control transactions
  805. *
  806. * To make extended set operations as atomic as the hardware allows, controls
  807. * are handled using begin/commit/rollback operations.
  808. *
  809. * At the beginning of a set request, uvc_ctrl_begin should be called to
  810. * initialize the request. This function acquires the control lock.
  811. *
  812. * When setting a control, the new value is stored in the control data field
  813. * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
  814. * later processing. If the UVC and V4L2 control sizes differ, the current
  815. * value is loaded from the hardware before storing the new value in the data
  816. * field.
  817. *
  818. * After processing all controls in the transaction, uvc_ctrl_commit or
  819. * uvc_ctrl_rollback must be called to apply the pending changes to the
  820. * hardware or revert them. When applying changes, all controls marked as
  821. * dirty will be modified in the UVC device, and the dirty flag will be
  822. * cleared. When reverting controls, the control data field
  823. * UVC_CTRL_DATA_CURRENT is reverted to its previous value
  824. * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
  825. * control lock.
  826. */
  827. int uvc_ctrl_begin(struct uvc_video_chain *chain)
  828. {
  829. return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0;
  830. }
  831. static int uvc_ctrl_commit_entity(struct uvc_device *dev,
  832. struct uvc_entity *entity, int rollback)
  833. {
  834. struct uvc_control *ctrl;
  835. unsigned int i;
  836. int ret;
  837. if (entity == NULL)
  838. return 0;
  839. for (i = 0; i < entity->ncontrols; ++i) {
  840. ctrl = &entity->controls[i];
  841. if (ctrl->info == NULL)
  842. continue;
  843. /* Reset the loaded flag for auto-update controls that were
  844. * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
  845. * uvc_ctrl_get from using the cached value.
  846. */
  847. if (ctrl->info->flags & UVC_CONTROL_AUTO_UPDATE)
  848. ctrl->loaded = 0;
  849. if (!ctrl->dirty)
  850. continue;
  851. if (!rollback)
  852. ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id,
  853. dev->intfnum, ctrl->info->selector,
  854. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  855. ctrl->info->size);
  856. else
  857. ret = 0;
  858. if (rollback || ret < 0)
  859. memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  860. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
  861. ctrl->info->size);
  862. ctrl->dirty = 0;
  863. if (ret < 0)
  864. return ret;
  865. }
  866. return 0;
  867. }
  868. int __uvc_ctrl_commit(struct uvc_video_chain *chain, int rollback)
  869. {
  870. struct uvc_entity *entity;
  871. int ret = 0;
  872. /* Find the control. */
  873. list_for_each_entry(entity, &chain->entities, chain) {
  874. ret = uvc_ctrl_commit_entity(chain->dev, entity, rollback);
  875. if (ret < 0)
  876. goto done;
  877. }
  878. done:
  879. mutex_unlock(&chain->ctrl_mutex);
  880. return ret;
  881. }
  882. int uvc_ctrl_get(struct uvc_video_chain *chain,
  883. struct v4l2_ext_control *xctrl)
  884. {
  885. struct uvc_control *ctrl;
  886. struct uvc_control_mapping *mapping;
  887. struct uvc_menu_info *menu;
  888. unsigned int i;
  889. int ret;
  890. ctrl = uvc_find_control(chain, xctrl->id, &mapping);
  891. if (ctrl == NULL || (ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0)
  892. return -EINVAL;
  893. if (!ctrl->loaded) {
  894. ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, ctrl->entity->id,
  895. chain->dev->intfnum, ctrl->info->selector,
  896. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  897. ctrl->info->size);
  898. if (ret < 0)
  899. return ret;
  900. ctrl->loaded = 1;
  901. }
  902. xctrl->value = mapping->get(mapping, UVC_GET_CUR,
  903. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
  904. if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
  905. menu = mapping->menu_info;
  906. for (i = 0; i < mapping->menu_count; ++i, ++menu) {
  907. if (menu->value == xctrl->value) {
  908. xctrl->value = i;
  909. break;
  910. }
  911. }
  912. }
  913. return 0;
  914. }
  915. int uvc_ctrl_set(struct uvc_video_chain *chain,
  916. struct v4l2_ext_control *xctrl)
  917. {
  918. struct uvc_control *ctrl;
  919. struct uvc_control_mapping *mapping;
  920. s32 value = xctrl->value;
  921. int ret;
  922. ctrl = uvc_find_control(chain, xctrl->id, &mapping);
  923. if (ctrl == NULL || (ctrl->info->flags & UVC_CONTROL_SET_CUR) == 0)
  924. return -EINVAL;
  925. if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
  926. if (value < 0 || value >= mapping->menu_count)
  927. return -EINVAL;
  928. value = mapping->menu_info[value].value;
  929. }
  930. if (!ctrl->loaded && (ctrl->info->size * 8) != mapping->size) {
  931. if ((ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0) {
  932. memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  933. 0, ctrl->info->size);
  934. } else {
  935. ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
  936. ctrl->entity->id, chain->dev->intfnum,
  937. ctrl->info->selector,
  938. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  939. ctrl->info->size);
  940. if (ret < 0)
  941. return ret;
  942. }
  943. ctrl->loaded = 1;
  944. }
  945. if (!ctrl->dirty) {
  946. memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
  947. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  948. ctrl->info->size);
  949. }
  950. mapping->set(mapping, value,
  951. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
  952. ctrl->dirty = 1;
  953. ctrl->modified = 1;
  954. return 0;
  955. }
  956. /* --------------------------------------------------------------------------
  957. * Dynamic controls
  958. */
  959. int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
  960. struct uvc_xu_control *xctrl, int set)
  961. {
  962. struct uvc_entity *entity;
  963. struct uvc_control *ctrl = NULL;
  964. unsigned int i, found = 0;
  965. __u8 *data;
  966. int ret;
  967. /* Find the extension unit. */
  968. list_for_each_entry(entity, &chain->entities, chain) {
  969. if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT &&
  970. entity->id == xctrl->unit)
  971. break;
  972. }
  973. if (entity->id != xctrl->unit) {
  974. uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
  975. xctrl->unit);
  976. return -EINVAL;
  977. }
  978. /* Find the control. */
  979. for (i = 0; i < entity->ncontrols; ++i) {
  980. ctrl = &entity->controls[i];
  981. if (ctrl->info == NULL)
  982. continue;
  983. if (ctrl->info->selector == xctrl->selector) {
  984. found = 1;
  985. break;
  986. }
  987. }
  988. if (!found) {
  989. uvc_trace(UVC_TRACE_CONTROL,
  990. "Control " UVC_GUID_FORMAT "/%u not found.\n",
  991. UVC_GUID_ARGS(entity->extension.guidExtensionCode),
  992. xctrl->selector);
  993. return -EINVAL;
  994. }
  995. /* Validate control data size. */
  996. if (ctrl->info->size != xctrl->size)
  997. return -EINVAL;
  998. if ((set && !(ctrl->info->flags & UVC_CONTROL_SET_CUR)) ||
  999. (!set && !(ctrl->info->flags & UVC_CONTROL_GET_CUR)))
  1000. return -EINVAL;
  1001. if (mutex_lock_interruptible(&chain->ctrl_mutex))
  1002. return -ERESTARTSYS;
  1003. memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
  1004. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  1005. xctrl->size);
  1006. data = uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT);
  1007. if (set && copy_from_user(data, xctrl->data, xctrl->size)) {
  1008. ret = -EFAULT;
  1009. goto out;
  1010. }
  1011. ret = uvc_query_ctrl(chain->dev, set ? UVC_SET_CUR : UVC_GET_CUR,
  1012. xctrl->unit, chain->dev->intfnum, xctrl->selector,
  1013. data, xctrl->size);
  1014. if (ret < 0)
  1015. goto out;
  1016. if (!set && copy_to_user(xctrl->data, data, xctrl->size)) {
  1017. ret = -EFAULT;
  1018. goto out;
  1019. }
  1020. out:
  1021. if (ret)
  1022. memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  1023. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
  1024. xctrl->size);
  1025. mutex_unlock(&chain->ctrl_mutex);
  1026. return ret;
  1027. }
  1028. /* --------------------------------------------------------------------------
  1029. * Suspend/resume
  1030. */
  1031. /*
  1032. * Restore control values after resume, skipping controls that haven't been
  1033. * changed.
  1034. *
  1035. * TODO
  1036. * - Don't restore modified controls that are back to their default value.
  1037. * - Handle restore order (Auto-Exposure Mode should be restored before
  1038. * Exposure Time).
  1039. */
  1040. int uvc_ctrl_resume_device(struct uvc_device *dev)
  1041. {
  1042. struct uvc_control *ctrl;
  1043. struct uvc_entity *entity;
  1044. unsigned int i;
  1045. int ret;
  1046. /* Walk the entities list and restore controls when possible. */
  1047. list_for_each_entry(entity, &dev->entities, list) {
  1048. for (i = 0; i < entity->ncontrols; ++i) {
  1049. ctrl = &entity->controls[i];
  1050. if (ctrl->info == NULL || !ctrl->modified ||
  1051. (ctrl->info->flags & UVC_CONTROL_RESTORE) == 0)
  1052. continue;
  1053. printk(KERN_INFO "restoring control " UVC_GUID_FORMAT
  1054. "/%u/%u\n", UVC_GUID_ARGS(ctrl->info->entity),
  1055. ctrl->info->index, ctrl->info->selector);
  1056. ctrl->dirty = 1;
  1057. }
  1058. ret = uvc_ctrl_commit_entity(dev, entity, 0);
  1059. if (ret < 0)
  1060. return ret;
  1061. }
  1062. return 0;
  1063. }
  1064. /* --------------------------------------------------------------------------
  1065. * Control and mapping handling
  1066. */
  1067. static void uvc_ctrl_add_ctrl(struct uvc_device *dev,
  1068. struct uvc_control_info *info)
  1069. {
  1070. struct uvc_entity *entity;
  1071. struct uvc_control *ctrl = NULL;
  1072. int ret, found = 0;
  1073. unsigned int i;
  1074. list_for_each_entry(entity, &dev->entities, list) {
  1075. if (!uvc_entity_match_guid(entity, info->entity))
  1076. continue;
  1077. for (i = 0; i < entity->ncontrols; ++i) {
  1078. ctrl = &entity->controls[i];
  1079. if (ctrl->index == info->index) {
  1080. found = 1;
  1081. break;
  1082. }
  1083. }
  1084. if (found)
  1085. break;
  1086. }
  1087. if (!found)
  1088. return;
  1089. if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
  1090. /* Check if the device control information and length match
  1091. * the user supplied information.
  1092. */
  1093. __u32 flags;
  1094. __le16 size;
  1095. __u8 inf;
  1096. ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id,
  1097. dev->intfnum, info->selector, (__u8 *)&size, 2);
  1098. if (ret < 0) {
  1099. uvc_trace(UVC_TRACE_CONTROL, "GET_LEN failed on "
  1100. "control " UVC_GUID_FORMAT "/%u (%d).\n",
  1101. UVC_GUID_ARGS(info->entity), info->selector,
  1102. ret);
  1103. return;
  1104. }
  1105. if (info->size != le16_to_cpu(size)) {
  1106. uvc_trace(UVC_TRACE_CONTROL, "Control " UVC_GUID_FORMAT
  1107. "/%u size doesn't match user supplied "
  1108. "value.\n", UVC_GUID_ARGS(info->entity),
  1109. info->selector);
  1110. return;
  1111. }
  1112. ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id,
  1113. dev->intfnum, info->selector, &inf, 1);
  1114. if (ret < 0) {
  1115. uvc_trace(UVC_TRACE_CONTROL, "GET_INFO failed on "
  1116. "control " UVC_GUID_FORMAT "/%u (%d).\n",
  1117. UVC_GUID_ARGS(info->entity), info->selector,
  1118. ret);
  1119. return;
  1120. }
  1121. flags = info->flags;
  1122. if (((flags & UVC_CONTROL_GET_CUR) && !(inf & (1 << 0))) ||
  1123. ((flags & UVC_CONTROL_SET_CUR) && !(inf & (1 << 1)))) {
  1124. uvc_trace(UVC_TRACE_CONTROL, "Control "
  1125. UVC_GUID_FORMAT "/%u flags don't match "
  1126. "supported operations.\n",
  1127. UVC_GUID_ARGS(info->entity), info->selector);
  1128. return;
  1129. }
  1130. }
  1131. ctrl->info = info;
  1132. ctrl->data = kmalloc(ctrl->info->size * UVC_CTRL_NDATA, GFP_KERNEL);
  1133. uvc_trace(UVC_TRACE_CONTROL, "Added control " UVC_GUID_FORMAT "/%u "
  1134. "to device %s entity %u\n", UVC_GUID_ARGS(ctrl->info->entity),
  1135. ctrl->info->selector, dev->udev->devpath, entity->id);
  1136. }
  1137. /*
  1138. * Add an item to the UVC control information list, and instantiate a control
  1139. * structure for each device that supports the control.
  1140. */
  1141. int uvc_ctrl_add_info(struct uvc_control_info *info)
  1142. {
  1143. struct uvc_control_info *ctrl;
  1144. struct uvc_device *dev;
  1145. int ret = 0;
  1146. /* Find matching controls by walking the devices, entities and
  1147. * controls list.
  1148. */
  1149. mutex_lock(&uvc_driver.ctrl_mutex);
  1150. /* First check if the list contains a control matching the new one.
  1151. * Bail out if it does.
  1152. */
  1153. list_for_each_entry(ctrl, &uvc_driver.controls, list) {
  1154. if (memcmp(ctrl->entity, info->entity, 16))
  1155. continue;
  1156. if (ctrl->selector == info->selector) {
  1157. uvc_trace(UVC_TRACE_CONTROL, "Control "
  1158. UVC_GUID_FORMAT "/%u is already defined.\n",
  1159. UVC_GUID_ARGS(info->entity), info->selector);
  1160. ret = -EEXIST;
  1161. goto end;
  1162. }
  1163. if (ctrl->index == info->index) {
  1164. uvc_trace(UVC_TRACE_CONTROL, "Control "
  1165. UVC_GUID_FORMAT "/%u would overwrite index "
  1166. "%d.\n", UVC_GUID_ARGS(info->entity),
  1167. info->selector, info->index);
  1168. ret = -EEXIST;
  1169. goto end;
  1170. }
  1171. }
  1172. list_for_each_entry(dev, &uvc_driver.devices, list)
  1173. uvc_ctrl_add_ctrl(dev, info);
  1174. INIT_LIST_HEAD(&info->mappings);
  1175. list_add_tail(&info->list, &uvc_driver.controls);
  1176. end:
  1177. mutex_unlock(&uvc_driver.ctrl_mutex);
  1178. return ret;
  1179. }
  1180. int uvc_ctrl_add_mapping(struct uvc_control_mapping *mapping)
  1181. {
  1182. struct uvc_control_info *info;
  1183. struct uvc_control_mapping *map;
  1184. int ret = -EINVAL;
  1185. if (mapping->get == NULL)
  1186. mapping->get = uvc_get_le_value;
  1187. if (mapping->set == NULL)
  1188. mapping->set = uvc_set_le_value;
  1189. if (mapping->id & ~V4L2_CTRL_ID_MASK) {
  1190. uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s' with "
  1191. "invalid control id 0x%08x\n", mapping->name,
  1192. mapping->id);
  1193. return -EINVAL;
  1194. }
  1195. mutex_lock(&uvc_driver.ctrl_mutex);
  1196. list_for_each_entry(info, &uvc_driver.controls, list) {
  1197. if (memcmp(info->entity, mapping->entity, 16) ||
  1198. info->selector != mapping->selector)
  1199. continue;
  1200. if (info->size * 8 < mapping->size + mapping->offset) {
  1201. uvc_trace(UVC_TRACE_CONTROL, "Mapping '%s' would "
  1202. "overflow control " UVC_GUID_FORMAT "/%u\n",
  1203. mapping->name, UVC_GUID_ARGS(info->entity),
  1204. info->selector);
  1205. ret = -EOVERFLOW;
  1206. goto end;
  1207. }
  1208. /* Check if the list contains a mapping matching the new one.
  1209. * Bail out if it does.
  1210. */
  1211. list_for_each_entry(map, &info->mappings, list) {
  1212. if (map->id == mapping->id) {
  1213. uvc_trace(UVC_TRACE_CONTROL, "Mapping '%s' is "
  1214. "already defined.\n", mapping->name);
  1215. ret = -EEXIST;
  1216. goto end;
  1217. }
  1218. }
  1219. mapping->ctrl = info;
  1220. list_add_tail(&mapping->list, &info->mappings);
  1221. uvc_trace(UVC_TRACE_CONTROL, "Adding mapping %s to control "
  1222. UVC_GUID_FORMAT "/%u.\n", mapping->name,
  1223. UVC_GUID_ARGS(info->entity), info->selector);
  1224. ret = 0;
  1225. break;
  1226. }
  1227. end:
  1228. mutex_unlock(&uvc_driver.ctrl_mutex);
  1229. return ret;
  1230. }
  1231. /*
  1232. * Prune an entity of its bogus controls using a blacklist. Bogus controls
  1233. * are currently the ones that crash the camera or unconditionally return an
  1234. * error when queried.
  1235. */
  1236. static void
  1237. uvc_ctrl_prune_entity(struct uvc_device *dev, struct uvc_entity *entity)
  1238. {
  1239. static const struct {
  1240. struct usb_device_id id;
  1241. u8 index;
  1242. } blacklist[] = {
  1243. { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
  1244. { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
  1245. };
  1246. u8 *controls;
  1247. unsigned int size;
  1248. unsigned int i;
  1249. if (UVC_ENTITY_TYPE(entity) != UVC_VC_PROCESSING_UNIT)
  1250. return;
  1251. controls = entity->processing.bmControls;
  1252. size = entity->processing.bControlSize;
  1253. for (i = 0; i < ARRAY_SIZE(blacklist); ++i) {
  1254. if (!usb_match_id(dev->intf, &blacklist[i].id))
  1255. continue;
  1256. if (blacklist[i].index >= 8 * size ||
  1257. !uvc_test_bit(controls, blacklist[i].index))
  1258. continue;
  1259. uvc_trace(UVC_TRACE_CONTROL, "%u/%u control is black listed, "
  1260. "removing it.\n", entity->id, blacklist[i].index);
  1261. uvc_clear_bit(controls, blacklist[i].index);
  1262. }
  1263. }
  1264. /*
  1265. * Initialize device controls.
  1266. */
  1267. int uvc_ctrl_init_device(struct uvc_device *dev)
  1268. {
  1269. struct uvc_control_info *info;
  1270. struct uvc_control *ctrl;
  1271. struct uvc_entity *entity;
  1272. unsigned int i;
  1273. /* Walk the entities list and instantiate controls */
  1274. list_for_each_entry(entity, &dev->entities, list) {
  1275. unsigned int bControlSize = 0, ncontrols = 0;
  1276. __u8 *bmControls = NULL;
  1277. if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
  1278. bmControls = entity->extension.bmControls;
  1279. bControlSize = entity->extension.bControlSize;
  1280. } else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) {
  1281. bmControls = entity->processing.bmControls;
  1282. bControlSize = entity->processing.bControlSize;
  1283. } else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
  1284. bmControls = entity->camera.bmControls;
  1285. bControlSize = entity->camera.bControlSize;
  1286. }
  1287. uvc_ctrl_prune_entity(dev, entity);
  1288. for (i = 0; i < bControlSize; ++i)
  1289. ncontrols += hweight8(bmControls[i]);
  1290. if (ncontrols == 0)
  1291. continue;
  1292. entity->controls = kzalloc(ncontrols*sizeof *ctrl, GFP_KERNEL);
  1293. if (entity->controls == NULL)
  1294. return -ENOMEM;
  1295. entity->ncontrols = ncontrols;
  1296. ctrl = entity->controls;
  1297. for (i = 0; i < bControlSize * 8; ++i) {
  1298. if (uvc_test_bit(bmControls, i) == 0)
  1299. continue;
  1300. ctrl->entity = entity;
  1301. ctrl->index = i;
  1302. ctrl++;
  1303. }
  1304. }
  1305. /* Walk the controls info list and associate them with the device
  1306. * controls, then add the device to the global device list. This has
  1307. * to be done while holding the controls lock, to make sure
  1308. * uvc_ctrl_add_info() will not get called in-between.
  1309. */
  1310. mutex_lock(&uvc_driver.ctrl_mutex);
  1311. list_for_each_entry(info, &uvc_driver.controls, list)
  1312. uvc_ctrl_add_ctrl(dev, info);
  1313. list_add_tail(&dev->list, &uvc_driver.devices);
  1314. mutex_unlock(&uvc_driver.ctrl_mutex);
  1315. return 0;
  1316. }
  1317. /*
  1318. * Cleanup device controls.
  1319. */
  1320. void uvc_ctrl_cleanup_device(struct uvc_device *dev)
  1321. {
  1322. struct uvc_entity *entity;
  1323. unsigned int i;
  1324. /* Remove the device from the global devices list */
  1325. mutex_lock(&uvc_driver.ctrl_mutex);
  1326. if (dev->list.next != NULL)
  1327. list_del(&dev->list);
  1328. mutex_unlock(&uvc_driver.ctrl_mutex);
  1329. list_for_each_entry(entity, &dev->entities, list) {
  1330. for (i = 0; i < entity->ncontrols; ++i)
  1331. kfree(entity->controls[i].data);
  1332. kfree(entity->controls);
  1333. }
  1334. }
  1335. void uvc_ctrl_init(void)
  1336. {
  1337. struct uvc_control_info *ctrl = uvc_ctrls;
  1338. struct uvc_control_info *cend = ctrl + ARRAY_SIZE(uvc_ctrls);
  1339. struct uvc_control_mapping *mapping = uvc_ctrl_mappings;
  1340. struct uvc_control_mapping *mend =
  1341. mapping + ARRAY_SIZE(uvc_ctrl_mappings);
  1342. for (; ctrl < cend; ++ctrl)
  1343. uvc_ctrl_add_info(ctrl);
  1344. for (; mapping < mend; ++mapping)
  1345. uvc_ctrl_add_mapping(mapping);
  1346. }