uvc_ctrl.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277
  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;
  548. int ret;
  549. ctrl = uvc_find_control(video, v4l2_ctrl->id, &mapping);
  550. if (ctrl == NULL)
  551. return -EINVAL;
  552. data = kmalloc(ctrl->info->size, GFP_KERNEL);
  553. if (data == NULL)
  554. return -ENOMEM;
  555. memset(v4l2_ctrl, 0, sizeof *v4l2_ctrl);
  556. v4l2_ctrl->id = mapping->id;
  557. v4l2_ctrl->type = mapping->v4l2_type;
  558. strncpy(v4l2_ctrl->name, mapping->name, sizeof v4l2_ctrl->name);
  559. v4l2_ctrl->flags = 0;
  560. if (!(ctrl->info->flags & UVC_CONTROL_SET_CUR))
  561. v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  562. if (ctrl->info->flags & UVC_CONTROL_GET_DEF) {
  563. if ((ret = uvc_query_ctrl(video->dev, GET_DEF, ctrl->entity->id,
  564. video->dev->intfnum, ctrl->info->selector,
  565. data, ctrl->info->size)) < 0)
  566. goto out;
  567. v4l2_ctrl->default_value = uvc_get_le_value(data, mapping);
  568. }
  569. switch (mapping->v4l2_type) {
  570. case V4L2_CTRL_TYPE_MENU:
  571. v4l2_ctrl->minimum = 0;
  572. v4l2_ctrl->maximum = mapping->menu_count - 1;
  573. v4l2_ctrl->step = 1;
  574. menu = mapping->menu_info;
  575. for (i = 0; i < mapping->menu_count; ++i, ++menu) {
  576. if (menu->value == v4l2_ctrl->default_value) {
  577. v4l2_ctrl->default_value = i;
  578. break;
  579. }
  580. }
  581. ret = 0;
  582. goto out;
  583. case V4L2_CTRL_TYPE_BOOLEAN:
  584. v4l2_ctrl->minimum = 0;
  585. v4l2_ctrl->maximum = 1;
  586. v4l2_ctrl->step = 1;
  587. ret = 0;
  588. goto out;
  589. default:
  590. break;
  591. }
  592. if (ctrl->info->flags & UVC_CONTROL_GET_MIN) {
  593. if ((ret = uvc_query_ctrl(video->dev, GET_MIN, ctrl->entity->id,
  594. video->dev->intfnum, ctrl->info->selector,
  595. data, ctrl->info->size)) < 0)
  596. goto out;
  597. v4l2_ctrl->minimum = uvc_get_le_value(data, mapping);
  598. }
  599. if (ctrl->info->flags & UVC_CONTROL_GET_MAX) {
  600. if ((ret = uvc_query_ctrl(video->dev, GET_MAX, ctrl->entity->id,
  601. video->dev->intfnum, ctrl->info->selector,
  602. data, ctrl->info->size)) < 0)
  603. goto out;
  604. v4l2_ctrl->maximum = uvc_get_le_value(data, mapping);
  605. }
  606. if (ctrl->info->flags & UVC_CONTROL_GET_RES) {
  607. if ((ret = uvc_query_ctrl(video->dev, GET_RES, ctrl->entity->id,
  608. video->dev->intfnum, ctrl->info->selector,
  609. data, ctrl->info->size)) < 0)
  610. goto out;
  611. v4l2_ctrl->step = uvc_get_le_value(data, mapping);
  612. }
  613. ret = 0;
  614. out:
  615. kfree(data);
  616. return ret;
  617. }
  618. /* --------------------------------------------------------------------------
  619. * Control transactions
  620. *
  621. * To make extended set operations as atomic as the hardware allows, controls
  622. * are handled using begin/commit/rollback operations.
  623. *
  624. * At the beginning of a set request, uvc_ctrl_begin should be called to
  625. * initialize the request. This function acquires the control lock.
  626. *
  627. * When setting a control, the new value is stored in the control data field
  628. * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
  629. * later processing. If the UVC and V4L2 control sizes differ, the current
  630. * value is loaded from the hardware before storing the new value in the data
  631. * field.
  632. *
  633. * After processing all controls in the transaction, uvc_ctrl_commit or
  634. * uvc_ctrl_rollback must be called to apply the pending changes to the
  635. * hardware or revert them. When applying changes, all controls marked as
  636. * dirty will be modified in the UVC device, and the dirty flag will be
  637. * cleared. When reverting controls, the control data field
  638. * UVC_CTRL_DATA_CURRENT is reverted to its previous value
  639. * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
  640. * control lock.
  641. */
  642. int uvc_ctrl_begin(struct uvc_video_device *video)
  643. {
  644. return mutex_lock_interruptible(&video->ctrl_mutex) ? -ERESTARTSYS : 0;
  645. }
  646. static int uvc_ctrl_commit_entity(struct uvc_device *dev,
  647. struct uvc_entity *entity, int rollback)
  648. {
  649. struct uvc_control *ctrl;
  650. unsigned int i;
  651. int ret;
  652. if (entity == NULL)
  653. return 0;
  654. for (i = 0; i < entity->ncontrols; ++i) {
  655. ctrl = &entity->controls[i];
  656. if (ctrl->info == NULL || !ctrl->dirty)
  657. continue;
  658. if (!rollback)
  659. ret = uvc_query_ctrl(dev, SET_CUR, ctrl->entity->id,
  660. dev->intfnum, ctrl->info->selector,
  661. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  662. ctrl->info->size);
  663. else
  664. ret = 0;
  665. if (rollback || ret < 0)
  666. memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  667. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
  668. ctrl->info->size);
  669. if ((ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0)
  670. ctrl->loaded = 0;
  671. ctrl->dirty = 0;
  672. if (ret < 0)
  673. return ret;
  674. }
  675. return 0;
  676. }
  677. int __uvc_ctrl_commit(struct uvc_video_device *video, int rollback)
  678. {
  679. struct uvc_entity *entity;
  680. int ret = 0;
  681. /* Find the control. */
  682. ret = uvc_ctrl_commit_entity(video->dev, video->processing, rollback);
  683. if (ret < 0)
  684. goto done;
  685. list_for_each_entry(entity, &video->iterms, chain) {
  686. ret = uvc_ctrl_commit_entity(video->dev, entity, rollback);
  687. if (ret < 0)
  688. goto done;
  689. }
  690. list_for_each_entry(entity, &video->extensions, chain) {
  691. ret = uvc_ctrl_commit_entity(video->dev, entity, rollback);
  692. if (ret < 0)
  693. goto done;
  694. }
  695. done:
  696. mutex_unlock(&video->ctrl_mutex);
  697. return ret;
  698. }
  699. int uvc_ctrl_get(struct uvc_video_device *video,
  700. struct v4l2_ext_control *xctrl)
  701. {
  702. struct uvc_control *ctrl;
  703. struct uvc_control_mapping *mapping;
  704. struct uvc_menu_info *menu;
  705. unsigned int i;
  706. int ret;
  707. ctrl = uvc_find_control(video, xctrl->id, &mapping);
  708. if (ctrl == NULL || (ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0)
  709. return -EINVAL;
  710. if (!ctrl->loaded) {
  711. ret = uvc_query_ctrl(video->dev, GET_CUR, ctrl->entity->id,
  712. video->dev->intfnum, ctrl->info->selector,
  713. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  714. ctrl->info->size);
  715. if (ret < 0)
  716. return ret;
  717. if ((ctrl->info->flags & UVC_CONTROL_AUTO_UPDATE) == 0)
  718. ctrl->loaded = 1;
  719. }
  720. xctrl->value = uvc_get_le_value(
  721. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT), mapping);
  722. if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
  723. menu = mapping->menu_info;
  724. for (i = 0; i < mapping->menu_count; ++i, ++menu) {
  725. if (menu->value == xctrl->value) {
  726. xctrl->value = i;
  727. break;
  728. }
  729. }
  730. }
  731. return 0;
  732. }
  733. int uvc_ctrl_set(struct uvc_video_device *video,
  734. struct v4l2_ext_control *xctrl)
  735. {
  736. struct uvc_control *ctrl;
  737. struct uvc_control_mapping *mapping;
  738. s32 value = xctrl->value;
  739. int ret;
  740. ctrl = uvc_find_control(video, xctrl->id, &mapping);
  741. if (ctrl == NULL || (ctrl->info->flags & UVC_CONTROL_SET_CUR) == 0)
  742. return -EINVAL;
  743. if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
  744. if (value < 0 || value >= mapping->menu_count)
  745. return -EINVAL;
  746. value = mapping->menu_info[value].value;
  747. }
  748. if (!ctrl->loaded && (ctrl->info->size * 8) != mapping->size) {
  749. if ((ctrl->info->flags & UVC_CONTROL_GET_CUR) == 0) {
  750. memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  751. 0, ctrl->info->size);
  752. } else {
  753. ret = uvc_query_ctrl(video->dev, GET_CUR,
  754. ctrl->entity->id, video->dev->intfnum,
  755. ctrl->info->selector,
  756. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  757. ctrl->info->size);
  758. if (ret < 0)
  759. return ret;
  760. }
  761. if ((ctrl->info->flags & UVC_CONTROL_AUTO_UPDATE) == 0)
  762. ctrl->loaded = 1;
  763. }
  764. if (!ctrl->dirty) {
  765. memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
  766. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  767. ctrl->info->size);
  768. }
  769. uvc_set_le_value(value,
  770. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT), mapping);
  771. ctrl->dirty = 1;
  772. ctrl->modified = 1;
  773. return 0;
  774. }
  775. /* --------------------------------------------------------------------------
  776. * Dynamic controls
  777. */
  778. int uvc_xu_ctrl_query(struct uvc_video_device *video,
  779. struct uvc_xu_control *xctrl, int set)
  780. {
  781. struct uvc_entity *entity;
  782. struct uvc_control *ctrl = NULL;
  783. unsigned int i, found = 0;
  784. __u8 *data;
  785. int ret;
  786. /* Find the extension unit. */
  787. list_for_each_entry(entity, &video->extensions, chain) {
  788. if (entity->id == xctrl->unit)
  789. break;
  790. }
  791. if (entity->id != xctrl->unit) {
  792. uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
  793. xctrl->unit);
  794. return -EINVAL;
  795. }
  796. /* Find the control. */
  797. for (i = 0; i < entity->ncontrols; ++i) {
  798. ctrl = &entity->controls[i];
  799. if (ctrl->info == NULL)
  800. continue;
  801. if (ctrl->info->selector == xctrl->selector) {
  802. found = 1;
  803. break;
  804. }
  805. }
  806. if (!found) {
  807. uvc_trace(UVC_TRACE_CONTROL,
  808. "Control " UVC_GUID_FORMAT "/%u not found.\n",
  809. UVC_GUID_ARGS(entity->extension.guidExtensionCode),
  810. xctrl->selector);
  811. return -EINVAL;
  812. }
  813. /* Validate control data size. */
  814. if (ctrl->info->size != xctrl->size)
  815. return -EINVAL;
  816. if ((set && !(ctrl->info->flags & UVC_CONTROL_SET_CUR)) ||
  817. (!set && !(ctrl->info->flags & UVC_CONTROL_GET_CUR)))
  818. return -EINVAL;
  819. if (mutex_lock_interruptible(&video->ctrl_mutex))
  820. return -ERESTARTSYS;
  821. memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
  822. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  823. xctrl->size);
  824. data = uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT);
  825. if (set && copy_from_user(data, xctrl->data, xctrl->size)) {
  826. ret = -EFAULT;
  827. goto out;
  828. }
  829. ret = uvc_query_ctrl(video->dev, set ? SET_CUR : GET_CUR, xctrl->unit,
  830. video->dev->intfnum, xctrl->selector, data,
  831. xctrl->size);
  832. if (ret < 0)
  833. goto out;
  834. if (!set && copy_to_user(xctrl->data, data, xctrl->size)) {
  835. ret = -EFAULT;
  836. goto out;
  837. }
  838. out:
  839. if (ret)
  840. memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
  841. uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
  842. xctrl->size);
  843. mutex_unlock(&video->ctrl_mutex);
  844. return ret;
  845. }
  846. /* --------------------------------------------------------------------------
  847. * Suspend/resume
  848. */
  849. /*
  850. * Restore control values after resume, skipping controls that haven't been
  851. * changed.
  852. *
  853. * TODO
  854. * - Don't restore modified controls that are back to their default value.
  855. * - Handle restore order (Auto-Exposure Mode should be restored before
  856. * Exposure Time).
  857. */
  858. int uvc_ctrl_resume_device(struct uvc_device *dev)
  859. {
  860. struct uvc_control *ctrl;
  861. struct uvc_entity *entity;
  862. unsigned int i;
  863. int ret;
  864. /* Walk the entities list and restore controls when possible. */
  865. list_for_each_entry(entity, &dev->entities, list) {
  866. for (i = 0; i < entity->ncontrols; ++i) {
  867. ctrl = &entity->controls[i];
  868. if (ctrl->info == NULL || !ctrl->modified ||
  869. (ctrl->info->flags & UVC_CONTROL_RESTORE) == 0)
  870. continue;
  871. printk(KERN_INFO "restoring control " UVC_GUID_FORMAT
  872. "/%u/%u\n", UVC_GUID_ARGS(ctrl->info->entity),
  873. ctrl->info->index, ctrl->info->selector);
  874. ctrl->dirty = 1;
  875. }
  876. ret = uvc_ctrl_commit_entity(dev, entity, 0);
  877. if (ret < 0)
  878. return ret;
  879. }
  880. return 0;
  881. }
  882. /* --------------------------------------------------------------------------
  883. * Control and mapping handling
  884. */
  885. static void uvc_ctrl_add_ctrl(struct uvc_device *dev,
  886. struct uvc_control_info *info)
  887. {
  888. struct uvc_entity *entity;
  889. struct uvc_control *ctrl = NULL;
  890. int ret, found = 0;
  891. unsigned int i;
  892. list_for_each_entry(entity, &dev->entities, list) {
  893. if (!uvc_entity_match_guid(entity, info->entity))
  894. continue;
  895. for (i = 0; i < entity->ncontrols; ++i) {
  896. ctrl = &entity->controls[i];
  897. if (ctrl->index == info->index) {
  898. found = 1;
  899. break;
  900. }
  901. }
  902. if (found)
  903. break;
  904. }
  905. if (!found)
  906. return;
  907. if (UVC_ENTITY_TYPE(entity) == VC_EXTENSION_UNIT) {
  908. /* Check if the device control information and length match
  909. * the user supplied information.
  910. */
  911. __u32 flags;
  912. __le16 size;
  913. __u8 inf;
  914. if ((ret = uvc_query_ctrl(dev, GET_LEN, ctrl->entity->id,
  915. dev->intfnum, info->selector, (__u8 *)&size, 2)) < 0) {
  916. uvc_trace(UVC_TRACE_CONTROL, "GET_LEN failed on "
  917. "control " UVC_GUID_FORMAT "/%u (%d).\n",
  918. UVC_GUID_ARGS(info->entity), info->selector,
  919. ret);
  920. return;
  921. }
  922. if (info->size != le16_to_cpu(size)) {
  923. uvc_trace(UVC_TRACE_CONTROL, "Control " UVC_GUID_FORMAT
  924. "/%u size doesn't match user supplied "
  925. "value.\n", UVC_GUID_ARGS(info->entity),
  926. info->selector);
  927. return;
  928. }
  929. if ((ret = uvc_query_ctrl(dev, GET_INFO, ctrl->entity->id,
  930. dev->intfnum, info->selector, &inf, 1)) < 0) {
  931. uvc_trace(UVC_TRACE_CONTROL, "GET_INFO failed on "
  932. "control " UVC_GUID_FORMAT "/%u (%d).\n",
  933. UVC_GUID_ARGS(info->entity), info->selector,
  934. ret);
  935. return;
  936. }
  937. flags = info->flags;
  938. if (((flags & UVC_CONTROL_GET_CUR) && !(inf & (1 << 0))) ||
  939. ((flags & UVC_CONTROL_SET_CUR) && !(inf & (1 << 1)))) {
  940. uvc_trace(UVC_TRACE_CONTROL, "Control "
  941. UVC_GUID_FORMAT "/%u flags don't match "
  942. "supported operations.\n",
  943. UVC_GUID_ARGS(info->entity), info->selector);
  944. return;
  945. }
  946. }
  947. ctrl->info = info;
  948. ctrl->data = kmalloc(ctrl->info->size * UVC_CTRL_NDATA, GFP_KERNEL);
  949. uvc_trace(UVC_TRACE_CONTROL, "Added control " UVC_GUID_FORMAT "/%u "
  950. "to device %s entity %u\n", UVC_GUID_ARGS(ctrl->info->entity),
  951. ctrl->info->selector, dev->udev->devpath, entity->id);
  952. }
  953. /*
  954. * Add an item to the UVC control information list, and instantiate a control
  955. * structure for each device that supports the control.
  956. */
  957. int uvc_ctrl_add_info(struct uvc_control_info *info)
  958. {
  959. struct uvc_control_info *ctrl;
  960. struct uvc_device *dev;
  961. int ret = 0;
  962. /* Find matching controls by walking the devices, entities and
  963. * controls list.
  964. */
  965. mutex_lock(&uvc_driver.ctrl_mutex);
  966. /* First check if the list contains a control matching the new one.
  967. * Bail out if it does.
  968. */
  969. list_for_each_entry(ctrl, &uvc_driver.controls, list) {
  970. if (memcmp(ctrl->entity, info->entity, 16))
  971. continue;
  972. if (ctrl->selector == info->selector) {
  973. uvc_trace(UVC_TRACE_CONTROL, "Control "
  974. UVC_GUID_FORMAT "/%u is already defined.\n",
  975. UVC_GUID_ARGS(info->entity), info->selector);
  976. ret = -EEXIST;
  977. goto end;
  978. }
  979. if (ctrl->index == info->index) {
  980. uvc_trace(UVC_TRACE_CONTROL, "Control "
  981. UVC_GUID_FORMAT "/%u would overwrite index "
  982. "%d.\n", UVC_GUID_ARGS(info->entity),
  983. info->selector, info->index);
  984. ret = -EEXIST;
  985. goto end;
  986. }
  987. }
  988. list_for_each_entry(dev, &uvc_driver.devices, list)
  989. uvc_ctrl_add_ctrl(dev, info);
  990. INIT_LIST_HEAD(&info->mappings);
  991. list_add_tail(&info->list, &uvc_driver.controls);
  992. end:
  993. mutex_unlock(&uvc_driver.ctrl_mutex);
  994. return ret;
  995. }
  996. int uvc_ctrl_add_mapping(struct uvc_control_mapping *mapping)
  997. {
  998. struct uvc_control_info *info;
  999. struct uvc_control_mapping *map;
  1000. int ret = -EINVAL;
  1001. if (mapping->id & ~V4L2_CTRL_ID_MASK) {
  1002. uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s' with "
  1003. "invalid control id 0x%08x\n", mapping->name,
  1004. mapping->id);
  1005. return -EINVAL;
  1006. }
  1007. mutex_lock(&uvc_driver.ctrl_mutex);
  1008. list_for_each_entry(info, &uvc_driver.controls, list) {
  1009. if (memcmp(info->entity, mapping->entity, 16) ||
  1010. info->selector != mapping->selector)
  1011. continue;
  1012. if (info->size * 8 < mapping->size + mapping->offset) {
  1013. uvc_trace(UVC_TRACE_CONTROL, "Mapping '%s' would "
  1014. "overflow control " UVC_GUID_FORMAT "/%u\n",
  1015. mapping->name, UVC_GUID_ARGS(info->entity),
  1016. info->selector);
  1017. ret = -EOVERFLOW;
  1018. goto end;
  1019. }
  1020. /* Check if the list contains a mapping matching the new one.
  1021. * Bail out if it does.
  1022. */
  1023. list_for_each_entry(map, &info->mappings, list) {
  1024. if (map->id == mapping->id) {
  1025. uvc_trace(UVC_TRACE_CONTROL, "Mapping '%s' is "
  1026. "already defined.\n", mapping->name);
  1027. ret = -EEXIST;
  1028. goto end;
  1029. }
  1030. }
  1031. mapping->ctrl = info;
  1032. list_add_tail(&mapping->list, &info->mappings);
  1033. uvc_trace(UVC_TRACE_CONTROL, "Adding mapping %s to control "
  1034. UVC_GUID_FORMAT "/%u.\n", mapping->name,
  1035. UVC_GUID_ARGS(info->entity), info->selector);
  1036. ret = 0;
  1037. break;
  1038. }
  1039. end:
  1040. mutex_unlock(&uvc_driver.ctrl_mutex);
  1041. return ret;
  1042. }
  1043. /*
  1044. * Initialize device controls.
  1045. */
  1046. int uvc_ctrl_init_device(struct uvc_device *dev)
  1047. {
  1048. struct uvc_control_info *info;
  1049. struct uvc_control *ctrl;
  1050. struct uvc_entity *entity;
  1051. unsigned int i;
  1052. /* Walk the entities list and instantiate controls */
  1053. list_for_each_entry(entity, &dev->entities, list) {
  1054. unsigned int bControlSize = 0, ncontrols = 0;
  1055. __u8 *bmControls = NULL;
  1056. if (UVC_ENTITY_TYPE(entity) == VC_EXTENSION_UNIT) {
  1057. bmControls = entity->extension.bmControls;
  1058. bControlSize = entity->extension.bControlSize;
  1059. } else if (UVC_ENTITY_TYPE(entity) == VC_PROCESSING_UNIT) {
  1060. bmControls = entity->processing.bmControls;
  1061. bControlSize = entity->processing.bControlSize;
  1062. } else if (UVC_ENTITY_TYPE(entity) == ITT_CAMERA) {
  1063. bmControls = entity->camera.bmControls;
  1064. bControlSize = entity->camera.bControlSize;
  1065. }
  1066. for (i = 0; i < bControlSize; ++i)
  1067. ncontrols += hweight8(bmControls[i]);
  1068. if (ncontrols == 0)
  1069. continue;
  1070. entity->controls = kzalloc(ncontrols*sizeof *ctrl, GFP_KERNEL);
  1071. if (entity->controls == NULL)
  1072. return -ENOMEM;
  1073. entity->ncontrols = ncontrols;
  1074. ctrl = entity->controls;
  1075. for (i = 0; i < bControlSize * 8; ++i) {
  1076. if (uvc_get_bit(bmControls, i) == 0)
  1077. continue;
  1078. ctrl->entity = entity;
  1079. ctrl->index = i;
  1080. ctrl++;
  1081. }
  1082. }
  1083. /* Walk the controls info list and associate them with the device
  1084. * controls, then add the device to the global device list. This has
  1085. * to be done while holding the controls lock, to make sure
  1086. * uvc_ctrl_add_info() will not get called in-between.
  1087. */
  1088. mutex_lock(&uvc_driver.ctrl_mutex);
  1089. list_for_each_entry(info, &uvc_driver.controls, list)
  1090. uvc_ctrl_add_ctrl(dev, info);
  1091. list_add_tail(&dev->list, &uvc_driver.devices);
  1092. mutex_unlock(&uvc_driver.ctrl_mutex);
  1093. return 0;
  1094. }
  1095. /*
  1096. * Cleanup device controls.
  1097. */
  1098. void uvc_ctrl_cleanup_device(struct uvc_device *dev)
  1099. {
  1100. struct uvc_entity *entity;
  1101. unsigned int i;
  1102. /* Remove the device from the global devices list */
  1103. mutex_lock(&uvc_driver.ctrl_mutex);
  1104. if (dev->list.next != NULL)
  1105. list_del(&dev->list);
  1106. mutex_unlock(&uvc_driver.ctrl_mutex);
  1107. list_for_each_entry(entity, &dev->entities, list) {
  1108. for (i = 0; i < entity->ncontrols; ++i)
  1109. kfree(entity->controls[i].data);
  1110. kfree(entity->controls);
  1111. }
  1112. }
  1113. void uvc_ctrl_init(void)
  1114. {
  1115. struct uvc_control_info *ctrl = uvc_ctrls;
  1116. struct uvc_control_info *cend = ctrl + ARRAY_SIZE(uvc_ctrls);
  1117. struct uvc_control_mapping *mapping = uvc_ctrl_mappings;
  1118. struct uvc_control_mapping *mend =
  1119. mapping + ARRAY_SIZE(uvc_ctrl_mappings);
  1120. for (; ctrl < cend; ++ctrl)
  1121. uvc_ctrl_add_info(ctrl);
  1122. for (; mapping < mend; ++mapping)
  1123. uvc_ctrl_add_mapping(mapping);
  1124. }