uvc_ctrl.c 32 KB

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