uvc_ctrl.c 32 KB

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