v4l2-ctrls.c 100 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456
  1. /*
  2. V4L2 controls framework implementation.
  3. Copyright (C) 2010 Hans Verkuil <hverkuil@xs4all.nl>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. */
  16. #include <linux/ctype.h>
  17. #include <linux/slab.h>
  18. #include <linux/export.h>
  19. #include <media/v4l2-ioctl.h>
  20. #include <media/v4l2-device.h>
  21. #include <media/v4l2-ctrls.h>
  22. #include <media/v4l2-event.h>
  23. #include <media/v4l2-dev.h>
  24. #define has_op(master, op) \
  25. (master->ops && master->ops->op)
  26. #define call_op(master, op) \
  27. (has_op(master, op) ? master->ops->op(master) : 0)
  28. /* Internal temporary helper struct, one for each v4l2_ext_control */
  29. struct v4l2_ctrl_helper {
  30. /* Pointer to the control reference of the master control */
  31. struct v4l2_ctrl_ref *mref;
  32. /* The control corresponding to the v4l2_ext_control ID field. */
  33. struct v4l2_ctrl *ctrl;
  34. /* v4l2_ext_control index of the next control belonging to the
  35. same cluster, or 0 if there isn't any. */
  36. u32 next;
  37. };
  38. /* Small helper function to determine if the autocluster is set to manual
  39. mode. */
  40. static bool is_cur_manual(const struct v4l2_ctrl *master)
  41. {
  42. return master->is_auto && master->cur.val == master->manual_mode_value;
  43. }
  44. /* Same as above, but this checks the against the new value instead of the
  45. current value. */
  46. static bool is_new_manual(const struct v4l2_ctrl *master)
  47. {
  48. return master->is_auto && master->val == master->manual_mode_value;
  49. }
  50. /* Returns NULL or a character pointer array containing the menu for
  51. the given control ID. The pointer array ends with a NULL pointer.
  52. An empty string signifies a menu entry that is invalid. This allows
  53. drivers to disable certain options if it is not supported. */
  54. const char * const *v4l2_ctrl_get_menu(u32 id)
  55. {
  56. static const char * const mpeg_audio_sampling_freq[] = {
  57. "44.1 kHz",
  58. "48 kHz",
  59. "32 kHz",
  60. NULL
  61. };
  62. static const char * const mpeg_audio_encoding[] = {
  63. "MPEG-1/2 Layer I",
  64. "MPEG-1/2 Layer II",
  65. "MPEG-1/2 Layer III",
  66. "MPEG-2/4 AAC",
  67. "AC-3",
  68. NULL
  69. };
  70. static const char * const mpeg_audio_l1_bitrate[] = {
  71. "32 kbps",
  72. "64 kbps",
  73. "96 kbps",
  74. "128 kbps",
  75. "160 kbps",
  76. "192 kbps",
  77. "224 kbps",
  78. "256 kbps",
  79. "288 kbps",
  80. "320 kbps",
  81. "352 kbps",
  82. "384 kbps",
  83. "416 kbps",
  84. "448 kbps",
  85. NULL
  86. };
  87. static const char * const mpeg_audio_l2_bitrate[] = {
  88. "32 kbps",
  89. "48 kbps",
  90. "56 kbps",
  91. "64 kbps",
  92. "80 kbps",
  93. "96 kbps",
  94. "112 kbps",
  95. "128 kbps",
  96. "160 kbps",
  97. "192 kbps",
  98. "224 kbps",
  99. "256 kbps",
  100. "320 kbps",
  101. "384 kbps",
  102. NULL
  103. };
  104. static const char * const mpeg_audio_l3_bitrate[] = {
  105. "32 kbps",
  106. "40 kbps",
  107. "48 kbps",
  108. "56 kbps",
  109. "64 kbps",
  110. "80 kbps",
  111. "96 kbps",
  112. "112 kbps",
  113. "128 kbps",
  114. "160 kbps",
  115. "192 kbps",
  116. "224 kbps",
  117. "256 kbps",
  118. "320 kbps",
  119. NULL
  120. };
  121. static const char * const mpeg_audio_ac3_bitrate[] = {
  122. "32 kbps",
  123. "40 kbps",
  124. "48 kbps",
  125. "56 kbps",
  126. "64 kbps",
  127. "80 kbps",
  128. "96 kbps",
  129. "112 kbps",
  130. "128 kbps",
  131. "160 kbps",
  132. "192 kbps",
  133. "224 kbps",
  134. "256 kbps",
  135. "320 kbps",
  136. "384 kbps",
  137. "448 kbps",
  138. "512 kbps",
  139. "576 kbps",
  140. "640 kbps",
  141. NULL
  142. };
  143. static const char * const mpeg_audio_mode[] = {
  144. "Stereo",
  145. "Joint Stereo",
  146. "Dual",
  147. "Mono",
  148. NULL
  149. };
  150. static const char * const mpeg_audio_mode_extension[] = {
  151. "Bound 4",
  152. "Bound 8",
  153. "Bound 12",
  154. "Bound 16",
  155. NULL
  156. };
  157. static const char * const mpeg_audio_emphasis[] = {
  158. "No Emphasis",
  159. "50/15 us",
  160. "CCITT J17",
  161. NULL
  162. };
  163. static const char * const mpeg_audio_crc[] = {
  164. "No CRC",
  165. "16-bit CRC",
  166. NULL
  167. };
  168. static const char * const mpeg_audio_dec_playback[] = {
  169. "Auto",
  170. "Stereo",
  171. "Left",
  172. "Right",
  173. "Mono",
  174. "Swapped Stereo",
  175. NULL
  176. };
  177. static const char * const mpeg_video_encoding[] = {
  178. "MPEG-1",
  179. "MPEG-2",
  180. "MPEG-4 AVC",
  181. NULL
  182. };
  183. static const char * const mpeg_video_aspect[] = {
  184. "1x1",
  185. "4x3",
  186. "16x9",
  187. "2.21x1",
  188. NULL
  189. };
  190. static const char * const mpeg_video_bitrate_mode[] = {
  191. "Variable Bitrate",
  192. "Constant Bitrate",
  193. NULL
  194. };
  195. static const char * const mpeg_stream_type[] = {
  196. "MPEG-2 Program Stream",
  197. "MPEG-2 Transport Stream",
  198. "MPEG-1 System Stream",
  199. "MPEG-2 DVD-compatible Stream",
  200. "MPEG-1 VCD-compatible Stream",
  201. "MPEG-2 SVCD-compatible Stream",
  202. NULL
  203. };
  204. static const char * const mpeg_stream_vbi_fmt[] = {
  205. "No VBI",
  206. "Private Packet, IVTV Format",
  207. NULL
  208. };
  209. static const char * const camera_power_line_frequency[] = {
  210. "Disabled",
  211. "50 Hz",
  212. "60 Hz",
  213. "Auto",
  214. NULL
  215. };
  216. static const char * const camera_exposure_auto[] = {
  217. "Auto Mode",
  218. "Manual Mode",
  219. "Shutter Priority Mode",
  220. "Aperture Priority Mode",
  221. NULL
  222. };
  223. static const char * const camera_exposure_metering[] = {
  224. "Average",
  225. "Center Weighted",
  226. "Spot",
  227. "Matrix",
  228. NULL
  229. };
  230. static const char * const camera_auto_focus_range[] = {
  231. "Auto",
  232. "Normal",
  233. "Macro",
  234. "Infinity",
  235. NULL
  236. };
  237. static const char * const colorfx[] = {
  238. "None",
  239. "Black & White",
  240. "Sepia",
  241. "Negative",
  242. "Emboss",
  243. "Sketch",
  244. "Sky Blue",
  245. "Grass Green",
  246. "Skin Whiten",
  247. "Vivid",
  248. "Aqua",
  249. "Art Freeze",
  250. "Silhouette",
  251. "Solarization",
  252. "Antique",
  253. "Set Cb/Cr",
  254. NULL
  255. };
  256. static const char * const auto_n_preset_white_balance[] = {
  257. "Manual",
  258. "Auto",
  259. "Incandescent",
  260. "Fluorescent",
  261. "Fluorescent H",
  262. "Horizon",
  263. "Daylight",
  264. "Flash",
  265. "Cloudy",
  266. "Shade",
  267. NULL,
  268. };
  269. static const char * const camera_iso_sensitivity_auto[] = {
  270. "Manual",
  271. "Auto",
  272. NULL
  273. };
  274. static const char * const scene_mode[] = {
  275. "None",
  276. "Backlight",
  277. "Beach/Snow",
  278. "Candle Light",
  279. "Dusk/Dawn",
  280. "Fall Colors",
  281. "Fireworks",
  282. "Landscape",
  283. "Night",
  284. "Party/Indoor",
  285. "Portrait",
  286. "Sports",
  287. "Sunset",
  288. "Text",
  289. NULL
  290. };
  291. static const char * const tune_emphasis[] = {
  292. "None",
  293. "50 Microseconds",
  294. "75 Microseconds",
  295. NULL,
  296. };
  297. static const char * const header_mode[] = {
  298. "Separate Buffer",
  299. "Joined With 1st Frame",
  300. NULL,
  301. };
  302. static const char * const multi_slice[] = {
  303. "Single",
  304. "Max Macroblocks",
  305. "Max Bytes",
  306. NULL,
  307. };
  308. static const char * const entropy_mode[] = {
  309. "CAVLC",
  310. "CABAC",
  311. NULL,
  312. };
  313. static const char * const mpeg_h264_level[] = {
  314. "1",
  315. "1b",
  316. "1.1",
  317. "1.2",
  318. "1.3",
  319. "2",
  320. "2.1",
  321. "2.2",
  322. "3",
  323. "3.1",
  324. "3.2",
  325. "4",
  326. "4.1",
  327. "4.2",
  328. "5",
  329. "5.1",
  330. NULL,
  331. };
  332. static const char * const h264_loop_filter[] = {
  333. "Enabled",
  334. "Disabled",
  335. "Disabled at Slice Boundary",
  336. NULL,
  337. };
  338. static const char * const h264_profile[] = {
  339. "Baseline",
  340. "Constrained Baseline",
  341. "Main",
  342. "Extended",
  343. "High",
  344. "High 10",
  345. "High 422",
  346. "High 444 Predictive",
  347. "High 10 Intra",
  348. "High 422 Intra",
  349. "High 444 Intra",
  350. "CAVLC 444 Intra",
  351. "Scalable Baseline",
  352. "Scalable High",
  353. "Scalable High Intra",
  354. "Multiview High",
  355. NULL,
  356. };
  357. static const char * const vui_sar_idc[] = {
  358. "Unspecified",
  359. "1:1",
  360. "12:11",
  361. "10:11",
  362. "16:11",
  363. "40:33",
  364. "24:11",
  365. "20:11",
  366. "32:11",
  367. "80:33",
  368. "18:11",
  369. "15:11",
  370. "64:33",
  371. "160:99",
  372. "4:3",
  373. "3:2",
  374. "2:1",
  375. "Extended SAR",
  376. NULL,
  377. };
  378. static const char * const h264_fp_arrangement_type[] = {
  379. "Checkerboard",
  380. "Column",
  381. "Row",
  382. "Side by Side",
  383. "Top Bottom",
  384. "Temporal",
  385. NULL,
  386. };
  387. static const char * const h264_fmo_map_type[] = {
  388. "Interleaved Slices",
  389. "Scattered Slices",
  390. "Foreground with Leftover",
  391. "Box Out",
  392. "Raster Scan",
  393. "Wipe Scan",
  394. "Explicit",
  395. NULL,
  396. };
  397. static const char * const mpeg_mpeg4_level[] = {
  398. "0",
  399. "0b",
  400. "1",
  401. "2",
  402. "3",
  403. "3b",
  404. "4",
  405. "5",
  406. NULL,
  407. };
  408. static const char * const mpeg4_profile[] = {
  409. "Simple",
  410. "Advanced Simple",
  411. "Core",
  412. "Simple Scalable",
  413. "Advanced Coding Efficiency",
  414. NULL,
  415. };
  416. static const char * const vpx_golden_frame_sel[] = {
  417. "Use Previous Frame",
  418. "Use Previous Specific Frame",
  419. NULL,
  420. };
  421. static const char * const flash_led_mode[] = {
  422. "Off",
  423. "Flash",
  424. "Torch",
  425. NULL,
  426. };
  427. static const char * const flash_strobe_source[] = {
  428. "Software",
  429. "External",
  430. NULL,
  431. };
  432. static const char * const jpeg_chroma_subsampling[] = {
  433. "4:4:4",
  434. "4:2:2",
  435. "4:2:0",
  436. "4:1:1",
  437. "4:1:0",
  438. "Gray",
  439. NULL,
  440. };
  441. static const char * const dv_tx_mode[] = {
  442. "DVI-D",
  443. "HDMI",
  444. NULL,
  445. };
  446. static const char * const dv_rgb_range[] = {
  447. "Automatic",
  448. "RGB limited range (16-235)",
  449. "RGB full range (0-255)",
  450. NULL,
  451. };
  452. static const char * const detect_md_mode[] = {
  453. "Disabled",
  454. "Global",
  455. "Threshold Grid",
  456. "Region Grid",
  457. NULL,
  458. };
  459. switch (id) {
  460. case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
  461. return mpeg_audio_sampling_freq;
  462. case V4L2_CID_MPEG_AUDIO_ENCODING:
  463. return mpeg_audio_encoding;
  464. case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
  465. return mpeg_audio_l1_bitrate;
  466. case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
  467. return mpeg_audio_l2_bitrate;
  468. case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
  469. return mpeg_audio_l3_bitrate;
  470. case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
  471. return mpeg_audio_ac3_bitrate;
  472. case V4L2_CID_MPEG_AUDIO_MODE:
  473. return mpeg_audio_mode;
  474. case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
  475. return mpeg_audio_mode_extension;
  476. case V4L2_CID_MPEG_AUDIO_EMPHASIS:
  477. return mpeg_audio_emphasis;
  478. case V4L2_CID_MPEG_AUDIO_CRC:
  479. return mpeg_audio_crc;
  480. case V4L2_CID_MPEG_AUDIO_DEC_PLAYBACK:
  481. case V4L2_CID_MPEG_AUDIO_DEC_MULTILINGUAL_PLAYBACK:
  482. return mpeg_audio_dec_playback;
  483. case V4L2_CID_MPEG_VIDEO_ENCODING:
  484. return mpeg_video_encoding;
  485. case V4L2_CID_MPEG_VIDEO_ASPECT:
  486. return mpeg_video_aspect;
  487. case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
  488. return mpeg_video_bitrate_mode;
  489. case V4L2_CID_MPEG_STREAM_TYPE:
  490. return mpeg_stream_type;
  491. case V4L2_CID_MPEG_STREAM_VBI_FMT:
  492. return mpeg_stream_vbi_fmt;
  493. case V4L2_CID_POWER_LINE_FREQUENCY:
  494. return camera_power_line_frequency;
  495. case V4L2_CID_EXPOSURE_AUTO:
  496. return camera_exposure_auto;
  497. case V4L2_CID_EXPOSURE_METERING:
  498. return camera_exposure_metering;
  499. case V4L2_CID_AUTO_FOCUS_RANGE:
  500. return camera_auto_focus_range;
  501. case V4L2_CID_COLORFX:
  502. return colorfx;
  503. case V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE:
  504. return auto_n_preset_white_balance;
  505. case V4L2_CID_ISO_SENSITIVITY_AUTO:
  506. return camera_iso_sensitivity_auto;
  507. case V4L2_CID_SCENE_MODE:
  508. return scene_mode;
  509. case V4L2_CID_TUNE_PREEMPHASIS:
  510. return tune_emphasis;
  511. case V4L2_CID_TUNE_DEEMPHASIS:
  512. return tune_emphasis;
  513. case V4L2_CID_FLASH_LED_MODE:
  514. return flash_led_mode;
  515. case V4L2_CID_FLASH_STROBE_SOURCE:
  516. return flash_strobe_source;
  517. case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
  518. return header_mode;
  519. case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
  520. return multi_slice;
  521. case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
  522. return entropy_mode;
  523. case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
  524. return mpeg_h264_level;
  525. case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
  526. return h264_loop_filter;
  527. case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
  528. return h264_profile;
  529. case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC:
  530. return vui_sar_idc;
  531. case V4L2_CID_MPEG_VIDEO_H264_SEI_FP_ARRANGEMENT_TYPE:
  532. return h264_fp_arrangement_type;
  533. case V4L2_CID_MPEG_VIDEO_H264_FMO_MAP_TYPE:
  534. return h264_fmo_map_type;
  535. case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
  536. return mpeg_mpeg4_level;
  537. case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
  538. return mpeg4_profile;
  539. case V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_SEL:
  540. return vpx_golden_frame_sel;
  541. case V4L2_CID_JPEG_CHROMA_SUBSAMPLING:
  542. return jpeg_chroma_subsampling;
  543. case V4L2_CID_DV_TX_MODE:
  544. return dv_tx_mode;
  545. case V4L2_CID_DV_TX_RGB_RANGE:
  546. case V4L2_CID_DV_RX_RGB_RANGE:
  547. return dv_rgb_range;
  548. case V4L2_CID_DETECT_MD_MODE:
  549. return detect_md_mode;
  550. default:
  551. return NULL;
  552. }
  553. }
  554. EXPORT_SYMBOL(v4l2_ctrl_get_menu);
  555. #define __v4l2_qmenu_int_len(arr, len) ({ *(len) = ARRAY_SIZE(arr); arr; })
  556. /*
  557. * Returns NULL or an s64 type array containing the menu for given
  558. * control ID. The total number of the menu items is returned in @len.
  559. */
  560. const s64 *v4l2_ctrl_get_int_menu(u32 id, u32 *len)
  561. {
  562. static const s64 qmenu_int_vpx_num_partitions[] = {
  563. 1, 2, 4, 8,
  564. };
  565. static const s64 qmenu_int_vpx_num_ref_frames[] = {
  566. 1, 2, 3,
  567. };
  568. switch (id) {
  569. case V4L2_CID_MPEG_VIDEO_VPX_NUM_PARTITIONS:
  570. return __v4l2_qmenu_int_len(qmenu_int_vpx_num_partitions, len);
  571. case V4L2_CID_MPEG_VIDEO_VPX_NUM_REF_FRAMES:
  572. return __v4l2_qmenu_int_len(qmenu_int_vpx_num_ref_frames, len);
  573. default:
  574. *len = 0;
  575. return NULL;
  576. }
  577. }
  578. EXPORT_SYMBOL(v4l2_ctrl_get_int_menu);
  579. /* Return the control name. */
  580. const char *v4l2_ctrl_get_name(u32 id)
  581. {
  582. switch (id) {
  583. /* USER controls */
  584. /* Keep the order of the 'case's the same as in v4l2-controls.h! */
  585. case V4L2_CID_USER_CLASS: return "User Controls";
  586. case V4L2_CID_BRIGHTNESS: return "Brightness";
  587. case V4L2_CID_CONTRAST: return "Contrast";
  588. case V4L2_CID_SATURATION: return "Saturation";
  589. case V4L2_CID_HUE: return "Hue";
  590. case V4L2_CID_AUDIO_VOLUME: return "Volume";
  591. case V4L2_CID_AUDIO_BALANCE: return "Balance";
  592. case V4L2_CID_AUDIO_BASS: return "Bass";
  593. case V4L2_CID_AUDIO_TREBLE: return "Treble";
  594. case V4L2_CID_AUDIO_MUTE: return "Mute";
  595. case V4L2_CID_AUDIO_LOUDNESS: return "Loudness";
  596. case V4L2_CID_BLACK_LEVEL: return "Black Level";
  597. case V4L2_CID_AUTO_WHITE_BALANCE: return "White Balance, Automatic";
  598. case V4L2_CID_DO_WHITE_BALANCE: return "Do White Balance";
  599. case V4L2_CID_RED_BALANCE: return "Red Balance";
  600. case V4L2_CID_BLUE_BALANCE: return "Blue Balance";
  601. case V4L2_CID_GAMMA: return "Gamma";
  602. case V4L2_CID_EXPOSURE: return "Exposure";
  603. case V4L2_CID_AUTOGAIN: return "Gain, Automatic";
  604. case V4L2_CID_GAIN: return "Gain";
  605. case V4L2_CID_HFLIP: return "Horizontal Flip";
  606. case V4L2_CID_VFLIP: return "Vertical Flip";
  607. case V4L2_CID_POWER_LINE_FREQUENCY: return "Power Line Frequency";
  608. case V4L2_CID_HUE_AUTO: return "Hue, Automatic";
  609. case V4L2_CID_WHITE_BALANCE_TEMPERATURE: return "White Balance Temperature";
  610. case V4L2_CID_SHARPNESS: return "Sharpness";
  611. case V4L2_CID_BACKLIGHT_COMPENSATION: return "Backlight Compensation";
  612. case V4L2_CID_CHROMA_AGC: return "Chroma AGC";
  613. case V4L2_CID_COLOR_KILLER: return "Color Killer";
  614. case V4L2_CID_COLORFX: return "Color Effects";
  615. case V4L2_CID_AUTOBRIGHTNESS: return "Brightness, Automatic";
  616. case V4L2_CID_BAND_STOP_FILTER: return "Band-Stop Filter";
  617. case V4L2_CID_ROTATE: return "Rotate";
  618. case V4L2_CID_BG_COLOR: return "Background Color";
  619. case V4L2_CID_CHROMA_GAIN: return "Chroma Gain";
  620. case V4L2_CID_ILLUMINATORS_1: return "Illuminator 1";
  621. case V4L2_CID_ILLUMINATORS_2: return "Illuminator 2";
  622. case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE: return "Min Number of Capture Buffers";
  623. case V4L2_CID_MIN_BUFFERS_FOR_OUTPUT: return "Min Number of Output Buffers";
  624. case V4L2_CID_ALPHA_COMPONENT: return "Alpha Component";
  625. case V4L2_CID_COLORFX_CBCR: return "Color Effects, CbCr";
  626. /* Codec controls */
  627. /* The MPEG controls are applicable to all codec controls
  628. * and the 'MPEG' part of the define is historical */
  629. /* Keep the order of the 'case's the same as in videodev2.h! */
  630. case V4L2_CID_MPEG_CLASS: return "Codec Controls";
  631. case V4L2_CID_MPEG_STREAM_TYPE: return "Stream Type";
  632. case V4L2_CID_MPEG_STREAM_PID_PMT: return "Stream PMT Program ID";
  633. case V4L2_CID_MPEG_STREAM_PID_AUDIO: return "Stream Audio Program ID";
  634. case V4L2_CID_MPEG_STREAM_PID_VIDEO: return "Stream Video Program ID";
  635. case V4L2_CID_MPEG_STREAM_PID_PCR: return "Stream PCR Program ID";
  636. case V4L2_CID_MPEG_STREAM_PES_ID_AUDIO: return "Stream PES Audio ID";
  637. case V4L2_CID_MPEG_STREAM_PES_ID_VIDEO: return "Stream PES Video ID";
  638. case V4L2_CID_MPEG_STREAM_VBI_FMT: return "Stream VBI Format";
  639. case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ: return "Audio Sampling Frequency";
  640. case V4L2_CID_MPEG_AUDIO_ENCODING: return "Audio Encoding";
  641. case V4L2_CID_MPEG_AUDIO_L1_BITRATE: return "Audio Layer I Bitrate";
  642. case V4L2_CID_MPEG_AUDIO_L2_BITRATE: return "Audio Layer II Bitrate";
  643. case V4L2_CID_MPEG_AUDIO_L3_BITRATE: return "Audio Layer III Bitrate";
  644. case V4L2_CID_MPEG_AUDIO_MODE: return "Audio Stereo Mode";
  645. case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION: return "Audio Stereo Mode Extension";
  646. case V4L2_CID_MPEG_AUDIO_EMPHASIS: return "Audio Emphasis";
  647. case V4L2_CID_MPEG_AUDIO_CRC: return "Audio CRC";
  648. case V4L2_CID_MPEG_AUDIO_MUTE: return "Audio Mute";
  649. case V4L2_CID_MPEG_AUDIO_AAC_BITRATE: return "Audio AAC Bitrate";
  650. case V4L2_CID_MPEG_AUDIO_AC3_BITRATE: return "Audio AC-3 Bitrate";
  651. case V4L2_CID_MPEG_AUDIO_DEC_PLAYBACK: return "Audio Playback";
  652. case V4L2_CID_MPEG_AUDIO_DEC_MULTILINGUAL_PLAYBACK: return "Audio Multilingual Playback";
  653. case V4L2_CID_MPEG_VIDEO_ENCODING: return "Video Encoding";
  654. case V4L2_CID_MPEG_VIDEO_ASPECT: return "Video Aspect";
  655. case V4L2_CID_MPEG_VIDEO_B_FRAMES: return "Video B Frames";
  656. case V4L2_CID_MPEG_VIDEO_GOP_SIZE: return "Video GOP Size";
  657. case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE: return "Video GOP Closure";
  658. case V4L2_CID_MPEG_VIDEO_PULLDOWN: return "Video Pulldown";
  659. case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: return "Video Bitrate Mode";
  660. case V4L2_CID_MPEG_VIDEO_BITRATE: return "Video Bitrate";
  661. case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: return "Video Peak Bitrate";
  662. case V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION: return "Video Temporal Decimation";
  663. case V4L2_CID_MPEG_VIDEO_MUTE: return "Video Mute";
  664. case V4L2_CID_MPEG_VIDEO_MUTE_YUV: return "Video Mute YUV";
  665. case V4L2_CID_MPEG_VIDEO_DECODER_SLICE_INTERFACE: return "Decoder Slice Interface";
  666. case V4L2_CID_MPEG_VIDEO_DECODER_MPEG4_DEBLOCK_FILTER: return "MPEG4 Loop Filter Enable";
  667. case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB: return "Number of Intra Refresh MBs";
  668. case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE: return "Frame Level Rate Control Enable";
  669. case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE: return "H264 MB Level Rate Control";
  670. case V4L2_CID_MPEG_VIDEO_HEADER_MODE: return "Sequence Header Mode";
  671. case V4L2_CID_MPEG_VIDEO_MAX_REF_PIC: return "Max Number of Reference Pics";
  672. case V4L2_CID_MPEG_VIDEO_H263_I_FRAME_QP: return "H263 I-Frame QP Value";
  673. case V4L2_CID_MPEG_VIDEO_H263_P_FRAME_QP: return "H263 P-Frame QP Value";
  674. case V4L2_CID_MPEG_VIDEO_H263_B_FRAME_QP: return "H263 B-Frame QP Value";
  675. case V4L2_CID_MPEG_VIDEO_H263_MIN_QP: return "H263 Minimum QP Value";
  676. case V4L2_CID_MPEG_VIDEO_H263_MAX_QP: return "H263 Maximum QP Value";
  677. case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP: return "H264 I-Frame QP Value";
  678. case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP: return "H264 P-Frame QP Value";
  679. case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP: return "H264 B-Frame QP Value";
  680. case V4L2_CID_MPEG_VIDEO_H264_MAX_QP: return "H264 Maximum QP Value";
  681. case V4L2_CID_MPEG_VIDEO_H264_MIN_QP: return "H264 Minimum QP Value";
  682. case V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM: return "H264 8x8 Transform Enable";
  683. case V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE: return "H264 CPB Buffer Size";
  684. case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE: return "H264 Entropy Mode";
  685. case V4L2_CID_MPEG_VIDEO_H264_I_PERIOD: return "H264 I-Frame Period";
  686. case V4L2_CID_MPEG_VIDEO_H264_LEVEL: return "H264 Level";
  687. case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA: return "H264 Loop Filter Alpha Offset";
  688. case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA: return "H264 Loop Filter Beta Offset";
  689. case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE: return "H264 Loop Filter Mode";
  690. case V4L2_CID_MPEG_VIDEO_H264_PROFILE: return "H264 Profile";
  691. case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT: return "Vertical Size of SAR";
  692. case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH: return "Horizontal Size of SAR";
  693. case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE: return "Aspect Ratio VUI Enable";
  694. case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC: return "VUI Aspect Ratio IDC";
  695. case V4L2_CID_MPEG_VIDEO_H264_SEI_FRAME_PACKING: return "H264 Enable Frame Packing SEI";
  696. case V4L2_CID_MPEG_VIDEO_H264_SEI_FP_CURRENT_FRAME_0: return "H264 Set Curr. Frame as Frame0";
  697. case V4L2_CID_MPEG_VIDEO_H264_SEI_FP_ARRANGEMENT_TYPE: return "H264 FP Arrangement Type";
  698. case V4L2_CID_MPEG_VIDEO_H264_FMO: return "H264 Flexible MB Ordering";
  699. case V4L2_CID_MPEG_VIDEO_H264_FMO_MAP_TYPE: return "H264 Map Type for FMO";
  700. case V4L2_CID_MPEG_VIDEO_H264_FMO_SLICE_GROUP: return "H264 FMO Number of Slice Groups";
  701. case V4L2_CID_MPEG_VIDEO_H264_FMO_CHANGE_DIRECTION: return "H264 FMO Direction of Change";
  702. case V4L2_CID_MPEG_VIDEO_H264_FMO_CHANGE_RATE: return "H264 FMO Size of 1st Slice Grp";
  703. case V4L2_CID_MPEG_VIDEO_H264_FMO_RUN_LENGTH: return "H264 FMO No. of Consecutive MBs";
  704. case V4L2_CID_MPEG_VIDEO_H264_ASO: return "H264 Arbitrary Slice Ordering";
  705. case V4L2_CID_MPEG_VIDEO_H264_ASO_SLICE_ORDER: return "H264 ASO Slice Order";
  706. case V4L2_CID_MPEG_VIDEO_H264_HIERARCHICAL_CODING: return "Enable H264 Hierarchical Coding";
  707. case V4L2_CID_MPEG_VIDEO_H264_HIERARCHICAL_CODING_TYPE: return "H264 Hierarchical Coding Type";
  708. case V4L2_CID_MPEG_VIDEO_H264_HIERARCHICAL_CODING_LAYER:return "H264 Number of HC Layers";
  709. case V4L2_CID_MPEG_VIDEO_H264_HIERARCHICAL_CODING_LAYER_QP:
  710. return "H264 Set QP Value for HC Layers";
  711. case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP: return "MPEG4 I-Frame QP Value";
  712. case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP: return "MPEG4 P-Frame QP Value";
  713. case V4L2_CID_MPEG_VIDEO_MPEG4_B_FRAME_QP: return "MPEG4 B-Frame QP Value";
  714. case V4L2_CID_MPEG_VIDEO_MPEG4_MIN_QP: return "MPEG4 Minimum QP Value";
  715. case V4L2_CID_MPEG_VIDEO_MPEG4_MAX_QP: return "MPEG4 Maximum QP Value";
  716. case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL: return "MPEG4 Level";
  717. case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE: return "MPEG4 Profile";
  718. case V4L2_CID_MPEG_VIDEO_MPEG4_QPEL: return "Quarter Pixel Search Enable";
  719. case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES: return "Maximum Bytes in a Slice";
  720. case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB: return "Number of MBs in a Slice";
  721. case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE: return "Slice Partitioning Method";
  722. case V4L2_CID_MPEG_VIDEO_VBV_SIZE: return "VBV Buffer Size";
  723. case V4L2_CID_MPEG_VIDEO_DEC_PTS: return "Video Decoder PTS";
  724. case V4L2_CID_MPEG_VIDEO_DEC_FRAME: return "Video Decoder Frame Count";
  725. case V4L2_CID_MPEG_VIDEO_VBV_DELAY: return "Initial Delay for VBV Control";
  726. case V4L2_CID_MPEG_VIDEO_MV_H_SEARCH_RANGE: return "Horizontal MV Search Range";
  727. case V4L2_CID_MPEG_VIDEO_MV_V_SEARCH_RANGE: return "Vertical MV Search Range";
  728. case V4L2_CID_MPEG_VIDEO_REPEAT_SEQ_HEADER: return "Repeat Sequence Header";
  729. /* VPX controls */
  730. case V4L2_CID_MPEG_VIDEO_VPX_NUM_PARTITIONS: return "VPX Number of Partitions";
  731. case V4L2_CID_MPEG_VIDEO_VPX_IMD_DISABLE_4X4: return "VPX Intra Mode Decision Disable";
  732. case V4L2_CID_MPEG_VIDEO_VPX_NUM_REF_FRAMES: return "VPX No. of Refs for P Frame";
  733. case V4L2_CID_MPEG_VIDEO_VPX_FILTER_LEVEL: return "VPX Loop Filter Level Range";
  734. case V4L2_CID_MPEG_VIDEO_VPX_FILTER_SHARPNESS: return "VPX Deblocking Effect Control";
  735. case V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_REF_PERIOD: return "VPX Golden Frame Refresh Period";
  736. case V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_SEL: return "VPX Golden Frame Indicator";
  737. case V4L2_CID_MPEG_VIDEO_VPX_MIN_QP: return "VPX Minimum QP Value";
  738. case V4L2_CID_MPEG_VIDEO_VPX_MAX_QP: return "VPX Maximum QP Value";
  739. case V4L2_CID_MPEG_VIDEO_VPX_I_FRAME_QP: return "VPX I-Frame QP Value";
  740. case V4L2_CID_MPEG_VIDEO_VPX_P_FRAME_QP: return "VPX P-Frame QP Value";
  741. case V4L2_CID_MPEG_VIDEO_VPX_PROFILE: return "VPX Profile";
  742. /* CAMERA controls */
  743. /* Keep the order of the 'case's the same as in v4l2-controls.h! */
  744. case V4L2_CID_CAMERA_CLASS: return "Camera Controls";
  745. case V4L2_CID_EXPOSURE_AUTO: return "Auto Exposure";
  746. case V4L2_CID_EXPOSURE_ABSOLUTE: return "Exposure Time, Absolute";
  747. case V4L2_CID_EXPOSURE_AUTO_PRIORITY: return "Exposure, Dynamic Framerate";
  748. case V4L2_CID_PAN_RELATIVE: return "Pan, Relative";
  749. case V4L2_CID_TILT_RELATIVE: return "Tilt, Relative";
  750. case V4L2_CID_PAN_RESET: return "Pan, Reset";
  751. case V4L2_CID_TILT_RESET: return "Tilt, Reset";
  752. case V4L2_CID_PAN_ABSOLUTE: return "Pan, Absolute";
  753. case V4L2_CID_TILT_ABSOLUTE: return "Tilt, Absolute";
  754. case V4L2_CID_FOCUS_ABSOLUTE: return "Focus, Absolute";
  755. case V4L2_CID_FOCUS_RELATIVE: return "Focus, Relative";
  756. case V4L2_CID_FOCUS_AUTO: return "Focus, Automatic Continuous";
  757. case V4L2_CID_ZOOM_ABSOLUTE: return "Zoom, Absolute";
  758. case V4L2_CID_ZOOM_RELATIVE: return "Zoom, Relative";
  759. case V4L2_CID_ZOOM_CONTINUOUS: return "Zoom, Continuous";
  760. case V4L2_CID_PRIVACY: return "Privacy";
  761. case V4L2_CID_IRIS_ABSOLUTE: return "Iris, Absolute";
  762. case V4L2_CID_IRIS_RELATIVE: return "Iris, Relative";
  763. case V4L2_CID_AUTO_EXPOSURE_BIAS: return "Auto Exposure, Bias";
  764. case V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE: return "White Balance, Auto & Preset";
  765. case V4L2_CID_WIDE_DYNAMIC_RANGE: return "Wide Dynamic Range";
  766. case V4L2_CID_IMAGE_STABILIZATION: return "Image Stabilization";
  767. case V4L2_CID_ISO_SENSITIVITY: return "ISO Sensitivity";
  768. case V4L2_CID_ISO_SENSITIVITY_AUTO: return "ISO Sensitivity, Auto";
  769. case V4L2_CID_EXPOSURE_METERING: return "Exposure, Metering Mode";
  770. case V4L2_CID_SCENE_MODE: return "Scene Mode";
  771. case V4L2_CID_3A_LOCK: return "3A Lock";
  772. case V4L2_CID_AUTO_FOCUS_START: return "Auto Focus, Start";
  773. case V4L2_CID_AUTO_FOCUS_STOP: return "Auto Focus, Stop";
  774. case V4L2_CID_AUTO_FOCUS_STATUS: return "Auto Focus, Status";
  775. case V4L2_CID_AUTO_FOCUS_RANGE: return "Auto Focus, Range";
  776. case V4L2_CID_PAN_SPEED: return "Pan, Speed";
  777. case V4L2_CID_TILT_SPEED: return "Tilt, Speed";
  778. /* FM Radio Modulator controls */
  779. /* Keep the order of the 'case's the same as in v4l2-controls.h! */
  780. case V4L2_CID_FM_TX_CLASS: return "FM Radio Modulator Controls";
  781. case V4L2_CID_RDS_TX_DEVIATION: return "RDS Signal Deviation";
  782. case V4L2_CID_RDS_TX_PI: return "RDS Program ID";
  783. case V4L2_CID_RDS_TX_PTY: return "RDS Program Type";
  784. case V4L2_CID_RDS_TX_PS_NAME: return "RDS PS Name";
  785. case V4L2_CID_RDS_TX_RADIO_TEXT: return "RDS Radio Text";
  786. case V4L2_CID_RDS_TX_MONO_STEREO: return "RDS Stereo";
  787. case V4L2_CID_RDS_TX_ARTIFICIAL_HEAD: return "RDS Artificial Head";
  788. case V4L2_CID_RDS_TX_COMPRESSED: return "RDS Compressed";
  789. case V4L2_CID_RDS_TX_DYNAMIC_PTY: return "RDS Dynamic PTY";
  790. case V4L2_CID_RDS_TX_TRAFFIC_ANNOUNCEMENT: return "RDS Traffic Announcement";
  791. case V4L2_CID_RDS_TX_TRAFFIC_PROGRAM: return "RDS Traffic Program";
  792. case V4L2_CID_RDS_TX_MUSIC_SPEECH: return "RDS Music";
  793. case V4L2_CID_RDS_TX_ALT_FREQS_ENABLE: return "RDS Enable Alt Frequencies";
  794. case V4L2_CID_RDS_TX_ALT_FREQS: return "RDS Alternate Frequencies";
  795. case V4L2_CID_AUDIO_LIMITER_ENABLED: return "Audio Limiter Feature Enabled";
  796. case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME: return "Audio Limiter Release Time";
  797. case V4L2_CID_AUDIO_LIMITER_DEVIATION: return "Audio Limiter Deviation";
  798. case V4L2_CID_AUDIO_COMPRESSION_ENABLED: return "Audio Compression Enabled";
  799. case V4L2_CID_AUDIO_COMPRESSION_GAIN: return "Audio Compression Gain";
  800. case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD: return "Audio Compression Threshold";
  801. case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME: return "Audio Compression Attack Time";
  802. case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME: return "Audio Compression Release Time";
  803. case V4L2_CID_PILOT_TONE_ENABLED: return "Pilot Tone Feature Enabled";
  804. case V4L2_CID_PILOT_TONE_DEVIATION: return "Pilot Tone Deviation";
  805. case V4L2_CID_PILOT_TONE_FREQUENCY: return "Pilot Tone Frequency";
  806. case V4L2_CID_TUNE_PREEMPHASIS: return "Pre-Emphasis";
  807. case V4L2_CID_TUNE_POWER_LEVEL: return "Tune Power Level";
  808. case V4L2_CID_TUNE_ANTENNA_CAPACITOR: return "Tune Antenna Capacitor";
  809. /* Flash controls */
  810. /* Keep the order of the 'case's the same as in v4l2-controls.h! */
  811. case V4L2_CID_FLASH_CLASS: return "Flash Controls";
  812. case V4L2_CID_FLASH_LED_MODE: return "LED Mode";
  813. case V4L2_CID_FLASH_STROBE_SOURCE: return "Strobe Source";
  814. case V4L2_CID_FLASH_STROBE: return "Strobe";
  815. case V4L2_CID_FLASH_STROBE_STOP: return "Stop Strobe";
  816. case V4L2_CID_FLASH_STROBE_STATUS: return "Strobe Status";
  817. case V4L2_CID_FLASH_TIMEOUT: return "Strobe Timeout";
  818. case V4L2_CID_FLASH_INTENSITY: return "Intensity, Flash Mode";
  819. case V4L2_CID_FLASH_TORCH_INTENSITY: return "Intensity, Torch Mode";
  820. case V4L2_CID_FLASH_INDICATOR_INTENSITY: return "Intensity, Indicator";
  821. case V4L2_CID_FLASH_FAULT: return "Faults";
  822. case V4L2_CID_FLASH_CHARGE: return "Charge";
  823. case V4L2_CID_FLASH_READY: return "Ready to Strobe";
  824. /* JPEG encoder controls */
  825. /* Keep the order of the 'case's the same as in v4l2-controls.h! */
  826. case V4L2_CID_JPEG_CLASS: return "JPEG Compression Controls";
  827. case V4L2_CID_JPEG_CHROMA_SUBSAMPLING: return "Chroma Subsampling";
  828. case V4L2_CID_JPEG_RESTART_INTERVAL: return "Restart Interval";
  829. case V4L2_CID_JPEG_COMPRESSION_QUALITY: return "Compression Quality";
  830. case V4L2_CID_JPEG_ACTIVE_MARKER: return "Active Markers";
  831. /* Image source controls */
  832. /* Keep the order of the 'case's the same as in v4l2-controls.h! */
  833. case V4L2_CID_IMAGE_SOURCE_CLASS: return "Image Source Controls";
  834. case V4L2_CID_VBLANK: return "Vertical Blanking";
  835. case V4L2_CID_HBLANK: return "Horizontal Blanking";
  836. case V4L2_CID_ANALOGUE_GAIN: return "Analogue Gain";
  837. case V4L2_CID_TEST_PATTERN_RED: return "Red Pixel Value";
  838. case V4L2_CID_TEST_PATTERN_GREENR: return "Green (Red) Pixel Value";
  839. case V4L2_CID_TEST_PATTERN_BLUE: return "Blue Pixel Value";
  840. case V4L2_CID_TEST_PATTERN_GREENB: return "Green (Blue) Pixel Value";
  841. /* Image processing controls */
  842. /* Keep the order of the 'case's the same as in v4l2-controls.h! */
  843. case V4L2_CID_IMAGE_PROC_CLASS: return "Image Processing Controls";
  844. case V4L2_CID_LINK_FREQ: return "Link Frequency";
  845. case V4L2_CID_PIXEL_RATE: return "Pixel Rate";
  846. case V4L2_CID_TEST_PATTERN: return "Test Pattern";
  847. /* DV controls */
  848. /* Keep the order of the 'case's the same as in v4l2-controls.h! */
  849. case V4L2_CID_DV_CLASS: return "Digital Video Controls";
  850. case V4L2_CID_DV_TX_HOTPLUG: return "Hotplug Present";
  851. case V4L2_CID_DV_TX_RXSENSE: return "RxSense Present";
  852. case V4L2_CID_DV_TX_EDID_PRESENT: return "EDID Present";
  853. case V4L2_CID_DV_TX_MODE: return "Transmit Mode";
  854. case V4L2_CID_DV_TX_RGB_RANGE: return "Tx RGB Quantization Range";
  855. case V4L2_CID_DV_RX_POWER_PRESENT: return "Power Present";
  856. case V4L2_CID_DV_RX_RGB_RANGE: return "Rx RGB Quantization Range";
  857. case V4L2_CID_FM_RX_CLASS: return "FM Radio Receiver Controls";
  858. case V4L2_CID_TUNE_DEEMPHASIS: return "De-Emphasis";
  859. case V4L2_CID_RDS_RECEPTION: return "RDS Reception";
  860. case V4L2_CID_RF_TUNER_CLASS: return "RF Tuner Controls";
  861. case V4L2_CID_RF_TUNER_RF_GAIN: return "RF Gain";
  862. case V4L2_CID_RF_TUNER_LNA_GAIN_AUTO: return "LNA Gain, Auto";
  863. case V4L2_CID_RF_TUNER_LNA_GAIN: return "LNA Gain";
  864. case V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO: return "Mixer Gain, Auto";
  865. case V4L2_CID_RF_TUNER_MIXER_GAIN: return "Mixer Gain";
  866. case V4L2_CID_RF_TUNER_IF_GAIN_AUTO: return "IF Gain, Auto";
  867. case V4L2_CID_RF_TUNER_IF_GAIN: return "IF Gain";
  868. case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO: return "Bandwidth, Auto";
  869. case V4L2_CID_RF_TUNER_BANDWIDTH: return "Bandwidth";
  870. case V4L2_CID_RF_TUNER_PLL_LOCK: return "PLL Lock";
  871. case V4L2_CID_RDS_RX_PTY: return "RDS Program Type";
  872. case V4L2_CID_RDS_RX_PS_NAME: return "RDS PS Name";
  873. case V4L2_CID_RDS_RX_RADIO_TEXT: return "RDS Radio Text";
  874. case V4L2_CID_RDS_RX_TRAFFIC_ANNOUNCEMENT: return "RDS Traffic Announcement";
  875. case V4L2_CID_RDS_RX_TRAFFIC_PROGRAM: return "RDS Traffic Program";
  876. case V4L2_CID_RDS_RX_MUSIC_SPEECH: return "RDS Music";
  877. /* Detection controls */
  878. /* Keep the order of the 'case's the same as in v4l2-controls.h! */
  879. case V4L2_CID_DETECT_CLASS: return "Detection Controls";
  880. case V4L2_CID_DETECT_MD_MODE: return "Motion Detection Mode";
  881. case V4L2_CID_DETECT_MD_GLOBAL_THRESHOLD: return "MD Global Threshold";
  882. case V4L2_CID_DETECT_MD_THRESHOLD_GRID: return "MD Threshold Grid";
  883. case V4L2_CID_DETECT_MD_REGION_GRID: return "MD Region Grid";
  884. default:
  885. return NULL;
  886. }
  887. }
  888. EXPORT_SYMBOL(v4l2_ctrl_get_name);
  889. void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type,
  890. s64 *min, s64 *max, u64 *step, s64 *def, u32 *flags)
  891. {
  892. *name = v4l2_ctrl_get_name(id);
  893. *flags = 0;
  894. switch (id) {
  895. case V4L2_CID_AUDIO_MUTE:
  896. case V4L2_CID_AUDIO_LOUDNESS:
  897. case V4L2_CID_AUTO_WHITE_BALANCE:
  898. case V4L2_CID_AUTOGAIN:
  899. case V4L2_CID_HFLIP:
  900. case V4L2_CID_VFLIP:
  901. case V4L2_CID_HUE_AUTO:
  902. case V4L2_CID_CHROMA_AGC:
  903. case V4L2_CID_COLOR_KILLER:
  904. case V4L2_CID_AUTOBRIGHTNESS:
  905. case V4L2_CID_MPEG_AUDIO_MUTE:
  906. case V4L2_CID_MPEG_VIDEO_MUTE:
  907. case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
  908. case V4L2_CID_MPEG_VIDEO_PULLDOWN:
  909. case V4L2_CID_EXPOSURE_AUTO_PRIORITY:
  910. case V4L2_CID_FOCUS_AUTO:
  911. case V4L2_CID_PRIVACY:
  912. case V4L2_CID_AUDIO_LIMITER_ENABLED:
  913. case V4L2_CID_AUDIO_COMPRESSION_ENABLED:
  914. case V4L2_CID_PILOT_TONE_ENABLED:
  915. case V4L2_CID_ILLUMINATORS_1:
  916. case V4L2_CID_ILLUMINATORS_2:
  917. case V4L2_CID_FLASH_STROBE_STATUS:
  918. case V4L2_CID_FLASH_CHARGE:
  919. case V4L2_CID_FLASH_READY:
  920. case V4L2_CID_MPEG_VIDEO_DECODER_MPEG4_DEBLOCK_FILTER:
  921. case V4L2_CID_MPEG_VIDEO_DECODER_SLICE_INTERFACE:
  922. case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
  923. case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE:
  924. case V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM:
  925. case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE:
  926. case V4L2_CID_MPEG_VIDEO_MPEG4_QPEL:
  927. case V4L2_CID_MPEG_VIDEO_REPEAT_SEQ_HEADER:
  928. case V4L2_CID_WIDE_DYNAMIC_RANGE:
  929. case V4L2_CID_IMAGE_STABILIZATION:
  930. case V4L2_CID_RDS_RECEPTION:
  931. case V4L2_CID_RF_TUNER_LNA_GAIN_AUTO:
  932. case V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO:
  933. case V4L2_CID_RF_TUNER_IF_GAIN_AUTO:
  934. case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:
  935. case V4L2_CID_RF_TUNER_PLL_LOCK:
  936. case V4L2_CID_RDS_TX_MONO_STEREO:
  937. case V4L2_CID_RDS_TX_ARTIFICIAL_HEAD:
  938. case V4L2_CID_RDS_TX_COMPRESSED:
  939. case V4L2_CID_RDS_TX_DYNAMIC_PTY:
  940. case V4L2_CID_RDS_TX_TRAFFIC_ANNOUNCEMENT:
  941. case V4L2_CID_RDS_TX_TRAFFIC_PROGRAM:
  942. case V4L2_CID_RDS_TX_MUSIC_SPEECH:
  943. case V4L2_CID_RDS_TX_ALT_FREQS_ENABLE:
  944. case V4L2_CID_RDS_RX_TRAFFIC_ANNOUNCEMENT:
  945. case V4L2_CID_RDS_RX_TRAFFIC_PROGRAM:
  946. case V4L2_CID_RDS_RX_MUSIC_SPEECH:
  947. *type = V4L2_CTRL_TYPE_BOOLEAN;
  948. *min = 0;
  949. *max = *step = 1;
  950. break;
  951. case V4L2_CID_MPEG_VIDEO_MV_H_SEARCH_RANGE:
  952. case V4L2_CID_MPEG_VIDEO_MV_V_SEARCH_RANGE:
  953. *type = V4L2_CTRL_TYPE_INTEGER;
  954. break;
  955. case V4L2_CID_PAN_RESET:
  956. case V4L2_CID_TILT_RESET:
  957. case V4L2_CID_FLASH_STROBE:
  958. case V4L2_CID_FLASH_STROBE_STOP:
  959. case V4L2_CID_AUTO_FOCUS_START:
  960. case V4L2_CID_AUTO_FOCUS_STOP:
  961. *type = V4L2_CTRL_TYPE_BUTTON;
  962. *flags |= V4L2_CTRL_FLAG_WRITE_ONLY |
  963. V4L2_CTRL_FLAG_EXECUTE_ON_WRITE;
  964. *min = *max = *step = *def = 0;
  965. break;
  966. case V4L2_CID_POWER_LINE_FREQUENCY:
  967. case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
  968. case V4L2_CID_MPEG_AUDIO_ENCODING:
  969. case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
  970. case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
  971. case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
  972. case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
  973. case V4L2_CID_MPEG_AUDIO_MODE:
  974. case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
  975. case V4L2_CID_MPEG_AUDIO_EMPHASIS:
  976. case V4L2_CID_MPEG_AUDIO_CRC:
  977. case V4L2_CID_MPEG_AUDIO_DEC_PLAYBACK:
  978. case V4L2_CID_MPEG_AUDIO_DEC_MULTILINGUAL_PLAYBACK:
  979. case V4L2_CID_MPEG_VIDEO_ENCODING:
  980. case V4L2_CID_MPEG_VIDEO_ASPECT:
  981. case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
  982. case V4L2_CID_MPEG_STREAM_TYPE:
  983. case V4L2_CID_MPEG_STREAM_VBI_FMT:
  984. case V4L2_CID_EXPOSURE_AUTO:
  985. case V4L2_CID_AUTO_FOCUS_RANGE:
  986. case V4L2_CID_COLORFX:
  987. case V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE:
  988. case V4L2_CID_TUNE_PREEMPHASIS:
  989. case V4L2_CID_FLASH_LED_MODE:
  990. case V4L2_CID_FLASH_STROBE_SOURCE:
  991. case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
  992. case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
  993. case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
  994. case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
  995. case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
  996. case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
  997. case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC:
  998. case V4L2_CID_MPEG_VIDEO_H264_SEI_FP_ARRANGEMENT_TYPE:
  999. case V4L2_CID_MPEG_VIDEO_H264_FMO_MAP_TYPE:
  1000. case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
  1001. case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
  1002. case V4L2_CID_JPEG_CHROMA_SUBSAMPLING:
  1003. case V4L2_CID_ISO_SENSITIVITY_AUTO:
  1004. case V4L2_CID_EXPOSURE_METERING:
  1005. case V4L2_CID_SCENE_MODE:
  1006. case V4L2_CID_DV_TX_MODE:
  1007. case V4L2_CID_DV_TX_RGB_RANGE:
  1008. case V4L2_CID_DV_RX_RGB_RANGE:
  1009. case V4L2_CID_TEST_PATTERN:
  1010. case V4L2_CID_TUNE_DEEMPHASIS:
  1011. case V4L2_CID_MPEG_VIDEO_VPX_GOLDEN_FRAME_SEL:
  1012. case V4L2_CID_DETECT_MD_MODE:
  1013. *type = V4L2_CTRL_TYPE_MENU;
  1014. break;
  1015. case V4L2_CID_LINK_FREQ:
  1016. *type = V4L2_CTRL_TYPE_INTEGER_MENU;
  1017. break;
  1018. case V4L2_CID_RDS_TX_PS_NAME:
  1019. case V4L2_CID_RDS_TX_RADIO_TEXT:
  1020. case V4L2_CID_RDS_RX_PS_NAME:
  1021. case V4L2_CID_RDS_RX_RADIO_TEXT:
  1022. *type = V4L2_CTRL_TYPE_STRING;
  1023. break;
  1024. case V4L2_CID_ISO_SENSITIVITY:
  1025. case V4L2_CID_AUTO_EXPOSURE_BIAS:
  1026. case V4L2_CID_MPEG_VIDEO_VPX_NUM_PARTITIONS:
  1027. case V4L2_CID_MPEG_VIDEO_VPX_NUM_REF_FRAMES:
  1028. *type = V4L2_CTRL_TYPE_INTEGER_MENU;
  1029. break;
  1030. case V4L2_CID_USER_CLASS:
  1031. case V4L2_CID_CAMERA_CLASS:
  1032. case V4L2_CID_MPEG_CLASS:
  1033. case V4L2_CID_FM_TX_CLASS:
  1034. case V4L2_CID_FLASH_CLASS:
  1035. case V4L2_CID_JPEG_CLASS:
  1036. case V4L2_CID_IMAGE_SOURCE_CLASS:
  1037. case V4L2_CID_IMAGE_PROC_CLASS:
  1038. case V4L2_CID_DV_CLASS:
  1039. case V4L2_CID_FM_RX_CLASS:
  1040. case V4L2_CID_RF_TUNER_CLASS:
  1041. case V4L2_CID_DETECT_CLASS:
  1042. *type = V4L2_CTRL_TYPE_CTRL_CLASS;
  1043. /* You can neither read not write these */
  1044. *flags |= V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_WRITE_ONLY;
  1045. *min = *max = *step = *def = 0;
  1046. break;
  1047. case V4L2_CID_BG_COLOR:
  1048. *type = V4L2_CTRL_TYPE_INTEGER;
  1049. *step = 1;
  1050. *min = 0;
  1051. /* Max is calculated as RGB888 that is 2^24 */
  1052. *max = 0xFFFFFF;
  1053. break;
  1054. case V4L2_CID_FLASH_FAULT:
  1055. case V4L2_CID_JPEG_ACTIVE_MARKER:
  1056. case V4L2_CID_3A_LOCK:
  1057. case V4L2_CID_AUTO_FOCUS_STATUS:
  1058. case V4L2_CID_DV_TX_HOTPLUG:
  1059. case V4L2_CID_DV_TX_RXSENSE:
  1060. case V4L2_CID_DV_TX_EDID_PRESENT:
  1061. case V4L2_CID_DV_RX_POWER_PRESENT:
  1062. *type = V4L2_CTRL_TYPE_BITMASK;
  1063. break;
  1064. case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
  1065. case V4L2_CID_MIN_BUFFERS_FOR_OUTPUT:
  1066. *type = V4L2_CTRL_TYPE_INTEGER;
  1067. *flags |= V4L2_CTRL_FLAG_READ_ONLY;
  1068. break;
  1069. case V4L2_CID_MPEG_VIDEO_DEC_PTS:
  1070. *type = V4L2_CTRL_TYPE_INTEGER64;
  1071. *flags |= V4L2_CTRL_FLAG_VOLATILE | V4L2_CTRL_FLAG_READ_ONLY;
  1072. *min = *def = 0;
  1073. *max = 0x1ffffffffLL;
  1074. *step = 1;
  1075. break;
  1076. case V4L2_CID_MPEG_VIDEO_DEC_FRAME:
  1077. *type = V4L2_CTRL_TYPE_INTEGER64;
  1078. *flags |= V4L2_CTRL_FLAG_VOLATILE | V4L2_CTRL_FLAG_READ_ONLY;
  1079. *min = *def = 0;
  1080. *max = 0x7fffffffffffffffLL;
  1081. *step = 1;
  1082. break;
  1083. case V4L2_CID_PIXEL_RATE:
  1084. *type = V4L2_CTRL_TYPE_INTEGER64;
  1085. *flags |= V4L2_CTRL_FLAG_READ_ONLY;
  1086. break;
  1087. case V4L2_CID_DETECT_MD_REGION_GRID:
  1088. *type = V4L2_CTRL_TYPE_U8;
  1089. break;
  1090. case V4L2_CID_DETECT_MD_THRESHOLD_GRID:
  1091. *type = V4L2_CTRL_TYPE_U16;
  1092. break;
  1093. case V4L2_CID_RDS_TX_ALT_FREQS:
  1094. *type = V4L2_CTRL_TYPE_U32;
  1095. break;
  1096. default:
  1097. *type = V4L2_CTRL_TYPE_INTEGER;
  1098. break;
  1099. }
  1100. switch (id) {
  1101. case V4L2_CID_MPEG_AUDIO_ENCODING:
  1102. case V4L2_CID_MPEG_AUDIO_MODE:
  1103. case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
  1104. case V4L2_CID_MPEG_VIDEO_B_FRAMES:
  1105. case V4L2_CID_MPEG_STREAM_TYPE:
  1106. *flags |= V4L2_CTRL_FLAG_UPDATE;
  1107. break;
  1108. case V4L2_CID_AUDIO_VOLUME:
  1109. case V4L2_CID_AUDIO_BALANCE:
  1110. case V4L2_CID_AUDIO_BASS:
  1111. case V4L2_CID_AUDIO_TREBLE:
  1112. case V4L2_CID_BRIGHTNESS:
  1113. case V4L2_CID_CONTRAST:
  1114. case V4L2_CID_SATURATION:
  1115. case V4L2_CID_HUE:
  1116. case V4L2_CID_RED_BALANCE:
  1117. case V4L2_CID_BLUE_BALANCE:
  1118. case V4L2_CID_GAMMA:
  1119. case V4L2_CID_SHARPNESS:
  1120. case V4L2_CID_CHROMA_GAIN:
  1121. case V4L2_CID_RDS_TX_DEVIATION:
  1122. case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME:
  1123. case V4L2_CID_AUDIO_LIMITER_DEVIATION:
  1124. case V4L2_CID_AUDIO_COMPRESSION_GAIN:
  1125. case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD:
  1126. case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME:
  1127. case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME:
  1128. case V4L2_CID_PILOT_TONE_DEVIATION:
  1129. case V4L2_CID_PILOT_TONE_FREQUENCY:
  1130. case V4L2_CID_TUNE_POWER_LEVEL:
  1131. case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
  1132. case V4L2_CID_RF_TUNER_RF_GAIN:
  1133. case V4L2_CID_RF_TUNER_LNA_GAIN:
  1134. case V4L2_CID_RF_TUNER_MIXER_GAIN:
  1135. case V4L2_CID_RF_TUNER_IF_GAIN:
  1136. case V4L2_CID_RF_TUNER_BANDWIDTH:
  1137. case V4L2_CID_DETECT_MD_GLOBAL_THRESHOLD:
  1138. *flags |= V4L2_CTRL_FLAG_SLIDER;
  1139. break;
  1140. case V4L2_CID_PAN_RELATIVE:
  1141. case V4L2_CID_TILT_RELATIVE:
  1142. case V4L2_CID_FOCUS_RELATIVE:
  1143. case V4L2_CID_IRIS_RELATIVE:
  1144. case V4L2_CID_ZOOM_RELATIVE:
  1145. *flags |= V4L2_CTRL_FLAG_WRITE_ONLY |
  1146. V4L2_CTRL_FLAG_EXECUTE_ON_WRITE;
  1147. break;
  1148. case V4L2_CID_FLASH_STROBE_STATUS:
  1149. case V4L2_CID_AUTO_FOCUS_STATUS:
  1150. case V4L2_CID_FLASH_READY:
  1151. case V4L2_CID_DV_TX_HOTPLUG:
  1152. case V4L2_CID_DV_TX_RXSENSE:
  1153. case V4L2_CID_DV_TX_EDID_PRESENT:
  1154. case V4L2_CID_DV_RX_POWER_PRESENT:
  1155. case V4L2_CID_RDS_RX_PTY:
  1156. case V4L2_CID_RDS_RX_PS_NAME:
  1157. case V4L2_CID_RDS_RX_RADIO_TEXT:
  1158. case V4L2_CID_RDS_RX_TRAFFIC_ANNOUNCEMENT:
  1159. case V4L2_CID_RDS_RX_TRAFFIC_PROGRAM:
  1160. case V4L2_CID_RDS_RX_MUSIC_SPEECH:
  1161. *flags |= V4L2_CTRL_FLAG_READ_ONLY;
  1162. break;
  1163. case V4L2_CID_RF_TUNER_PLL_LOCK:
  1164. *flags |= V4L2_CTRL_FLAG_VOLATILE;
  1165. break;
  1166. }
  1167. }
  1168. EXPORT_SYMBOL(v4l2_ctrl_fill);
  1169. static u32 user_flags(const struct v4l2_ctrl *ctrl)
  1170. {
  1171. u32 flags = ctrl->flags;
  1172. if (ctrl->is_ptr)
  1173. flags |= V4L2_CTRL_FLAG_HAS_PAYLOAD;
  1174. return flags;
  1175. }
  1176. static void fill_event(struct v4l2_event *ev, struct v4l2_ctrl *ctrl, u32 changes)
  1177. {
  1178. memset(ev, 0, sizeof(*ev));
  1179. ev->type = V4L2_EVENT_CTRL;
  1180. ev->id = ctrl->id;
  1181. ev->u.ctrl.changes = changes;
  1182. ev->u.ctrl.type = ctrl->type;
  1183. ev->u.ctrl.flags = user_flags(ctrl);
  1184. if (ctrl->is_ptr)
  1185. ev->u.ctrl.value64 = 0;
  1186. else
  1187. ev->u.ctrl.value64 = *ctrl->p_cur.p_s64;
  1188. ev->u.ctrl.minimum = ctrl->minimum;
  1189. ev->u.ctrl.maximum = ctrl->maximum;
  1190. if (ctrl->type == V4L2_CTRL_TYPE_MENU
  1191. || ctrl->type == V4L2_CTRL_TYPE_INTEGER_MENU)
  1192. ev->u.ctrl.step = 1;
  1193. else
  1194. ev->u.ctrl.step = ctrl->step;
  1195. ev->u.ctrl.default_value = ctrl->default_value;
  1196. }
  1197. static void send_event(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, u32 changes)
  1198. {
  1199. struct v4l2_event ev;
  1200. struct v4l2_subscribed_event *sev;
  1201. if (list_empty(&ctrl->ev_subs))
  1202. return;
  1203. fill_event(&ev, ctrl, changes);
  1204. list_for_each_entry(sev, &ctrl->ev_subs, node)
  1205. if (sev->fh != fh ||
  1206. (sev->flags & V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK))
  1207. v4l2_event_queue_fh(sev->fh, &ev);
  1208. }
  1209. static bool std_equal(const struct v4l2_ctrl *ctrl, u32 idx,
  1210. union v4l2_ctrl_ptr ptr1,
  1211. union v4l2_ctrl_ptr ptr2)
  1212. {
  1213. switch (ctrl->type) {
  1214. case V4L2_CTRL_TYPE_BUTTON:
  1215. return false;
  1216. case V4L2_CTRL_TYPE_STRING:
  1217. idx *= ctrl->elem_size;
  1218. /* strings are always 0-terminated */
  1219. return !strcmp(ptr1.p_char + idx, ptr2.p_char + idx);
  1220. case V4L2_CTRL_TYPE_INTEGER64:
  1221. return ptr1.p_s64[idx] == ptr2.p_s64[idx];
  1222. case V4L2_CTRL_TYPE_U8:
  1223. return ptr1.p_u8[idx] == ptr2.p_u8[idx];
  1224. case V4L2_CTRL_TYPE_U16:
  1225. return ptr1.p_u16[idx] == ptr2.p_u16[idx];
  1226. case V4L2_CTRL_TYPE_U32:
  1227. return ptr1.p_u32[idx] == ptr2.p_u32[idx];
  1228. default:
  1229. if (ctrl->is_int)
  1230. return ptr1.p_s32[idx] == ptr2.p_s32[idx];
  1231. idx *= ctrl->elem_size;
  1232. return !memcmp(ptr1.p + idx, ptr2.p + idx, ctrl->elem_size);
  1233. }
  1234. }
  1235. static void std_init(const struct v4l2_ctrl *ctrl, u32 idx,
  1236. union v4l2_ctrl_ptr ptr)
  1237. {
  1238. switch (ctrl->type) {
  1239. case V4L2_CTRL_TYPE_STRING:
  1240. idx *= ctrl->elem_size;
  1241. memset(ptr.p_char + idx, ' ', ctrl->minimum);
  1242. ptr.p_char[idx + ctrl->minimum] = '\0';
  1243. break;
  1244. case V4L2_CTRL_TYPE_INTEGER64:
  1245. ptr.p_s64[idx] = ctrl->default_value;
  1246. break;
  1247. case V4L2_CTRL_TYPE_INTEGER:
  1248. case V4L2_CTRL_TYPE_INTEGER_MENU:
  1249. case V4L2_CTRL_TYPE_MENU:
  1250. case V4L2_CTRL_TYPE_BITMASK:
  1251. case V4L2_CTRL_TYPE_BOOLEAN:
  1252. ptr.p_s32[idx] = ctrl->default_value;
  1253. break;
  1254. case V4L2_CTRL_TYPE_U8:
  1255. ptr.p_u8[idx] = ctrl->default_value;
  1256. break;
  1257. case V4L2_CTRL_TYPE_U16:
  1258. ptr.p_u16[idx] = ctrl->default_value;
  1259. break;
  1260. case V4L2_CTRL_TYPE_U32:
  1261. ptr.p_u32[idx] = ctrl->default_value;
  1262. break;
  1263. default:
  1264. idx *= ctrl->elem_size;
  1265. memset(ptr.p + idx, 0, ctrl->elem_size);
  1266. break;
  1267. }
  1268. }
  1269. static void std_log(const struct v4l2_ctrl *ctrl)
  1270. {
  1271. union v4l2_ctrl_ptr ptr = ctrl->p_cur;
  1272. if (ctrl->is_array) {
  1273. unsigned i;
  1274. for (i = 0; i < ctrl->nr_of_dims; i++)
  1275. pr_cont("[%u]", ctrl->dims[i]);
  1276. pr_cont(" ");
  1277. }
  1278. switch (ctrl->type) {
  1279. case V4L2_CTRL_TYPE_INTEGER:
  1280. pr_cont("%d", *ptr.p_s32);
  1281. break;
  1282. case V4L2_CTRL_TYPE_BOOLEAN:
  1283. pr_cont("%s", *ptr.p_s32 ? "true" : "false");
  1284. break;
  1285. case V4L2_CTRL_TYPE_MENU:
  1286. pr_cont("%s", ctrl->qmenu[*ptr.p_s32]);
  1287. break;
  1288. case V4L2_CTRL_TYPE_INTEGER_MENU:
  1289. pr_cont("%lld", ctrl->qmenu_int[*ptr.p_s32]);
  1290. break;
  1291. case V4L2_CTRL_TYPE_BITMASK:
  1292. pr_cont("0x%08x", *ptr.p_s32);
  1293. break;
  1294. case V4L2_CTRL_TYPE_INTEGER64:
  1295. pr_cont("%lld", *ptr.p_s64);
  1296. break;
  1297. case V4L2_CTRL_TYPE_STRING:
  1298. pr_cont("%s", ptr.p_char);
  1299. break;
  1300. case V4L2_CTRL_TYPE_U8:
  1301. pr_cont("%u", (unsigned)*ptr.p_u8);
  1302. break;
  1303. case V4L2_CTRL_TYPE_U16:
  1304. pr_cont("%u", (unsigned)*ptr.p_u16);
  1305. break;
  1306. case V4L2_CTRL_TYPE_U32:
  1307. pr_cont("%u", (unsigned)*ptr.p_u32);
  1308. break;
  1309. default:
  1310. pr_cont("unknown type %d", ctrl->type);
  1311. break;
  1312. }
  1313. }
  1314. /*
  1315. * Round towards the closest legal value. Be careful when we are
  1316. * close to the maximum range of the control type to prevent
  1317. * wrap-arounds.
  1318. */
  1319. #define ROUND_TO_RANGE(val, offset_type, ctrl) \
  1320. ({ \
  1321. offset_type offset; \
  1322. if ((ctrl)->maximum >= 0 && \
  1323. val >= (ctrl)->maximum - (s32)((ctrl)->step / 2)) \
  1324. val = (ctrl)->maximum; \
  1325. else \
  1326. val += (s32)((ctrl)->step / 2); \
  1327. val = clamp_t(typeof(val), val, \
  1328. (ctrl)->minimum, (ctrl)->maximum); \
  1329. offset = (val) - (ctrl)->minimum; \
  1330. offset = (ctrl)->step * (offset / (u32)(ctrl)->step); \
  1331. val = (ctrl)->minimum + offset; \
  1332. 0; \
  1333. })
  1334. /* Validate a new control */
  1335. static int std_validate(const struct v4l2_ctrl *ctrl, u32 idx,
  1336. union v4l2_ctrl_ptr ptr)
  1337. {
  1338. size_t len;
  1339. u64 offset;
  1340. s64 val;
  1341. switch (ctrl->type) {
  1342. case V4L2_CTRL_TYPE_INTEGER:
  1343. return ROUND_TO_RANGE(ptr.p_s32[idx], u32, ctrl);
  1344. case V4L2_CTRL_TYPE_INTEGER64:
  1345. /*
  1346. * We can't use the ROUND_TO_RANGE define here due to
  1347. * the u64 divide that needs special care.
  1348. */
  1349. val = ptr.p_s64[idx];
  1350. if (ctrl->maximum >= 0 && val >= ctrl->maximum - (s64)(ctrl->step / 2))
  1351. val = ctrl->maximum;
  1352. else
  1353. val += (s64)(ctrl->step / 2);
  1354. val = clamp_t(s64, val, ctrl->minimum, ctrl->maximum);
  1355. offset = val - ctrl->minimum;
  1356. do_div(offset, ctrl->step);
  1357. ptr.p_s64[idx] = ctrl->minimum + offset * ctrl->step;
  1358. return 0;
  1359. case V4L2_CTRL_TYPE_U8:
  1360. return ROUND_TO_RANGE(ptr.p_u8[idx], u8, ctrl);
  1361. case V4L2_CTRL_TYPE_U16:
  1362. return ROUND_TO_RANGE(ptr.p_u16[idx], u16, ctrl);
  1363. case V4L2_CTRL_TYPE_U32:
  1364. return ROUND_TO_RANGE(ptr.p_u32[idx], u32, ctrl);
  1365. case V4L2_CTRL_TYPE_BOOLEAN:
  1366. ptr.p_s32[idx] = !!ptr.p_s32[idx];
  1367. return 0;
  1368. case V4L2_CTRL_TYPE_MENU:
  1369. case V4L2_CTRL_TYPE_INTEGER_MENU:
  1370. if (ptr.p_s32[idx] < ctrl->minimum || ptr.p_s32[idx] > ctrl->maximum)
  1371. return -ERANGE;
  1372. if (ctrl->menu_skip_mask & (1 << ptr.p_s32[idx]))
  1373. return -EINVAL;
  1374. if (ctrl->type == V4L2_CTRL_TYPE_MENU &&
  1375. ctrl->qmenu[ptr.p_s32[idx]][0] == '\0')
  1376. return -EINVAL;
  1377. return 0;
  1378. case V4L2_CTRL_TYPE_BITMASK:
  1379. ptr.p_s32[idx] &= ctrl->maximum;
  1380. return 0;
  1381. case V4L2_CTRL_TYPE_BUTTON:
  1382. case V4L2_CTRL_TYPE_CTRL_CLASS:
  1383. ptr.p_s32[idx] = 0;
  1384. return 0;
  1385. case V4L2_CTRL_TYPE_STRING:
  1386. idx *= ctrl->elem_size;
  1387. len = strlen(ptr.p_char + idx);
  1388. if (len < ctrl->minimum)
  1389. return -ERANGE;
  1390. if ((len - (u32)ctrl->minimum) % (u32)ctrl->step)
  1391. return -ERANGE;
  1392. return 0;
  1393. default:
  1394. return -EINVAL;
  1395. }
  1396. }
  1397. static const struct v4l2_ctrl_type_ops std_type_ops = {
  1398. .equal = std_equal,
  1399. .init = std_init,
  1400. .log = std_log,
  1401. .validate = std_validate,
  1402. };
  1403. /* Helper function: copy the given control value back to the caller */
  1404. static int ptr_to_user(struct v4l2_ext_control *c,
  1405. struct v4l2_ctrl *ctrl,
  1406. union v4l2_ctrl_ptr ptr)
  1407. {
  1408. u32 len;
  1409. if (ctrl->is_ptr && !ctrl->is_string)
  1410. return copy_to_user(c->ptr, ptr.p, c->size) ?
  1411. -EFAULT : 0;
  1412. switch (ctrl->type) {
  1413. case V4L2_CTRL_TYPE_STRING:
  1414. len = strlen(ptr.p_char);
  1415. if (c->size < len + 1) {
  1416. c->size = ctrl->elem_size;
  1417. return -ENOSPC;
  1418. }
  1419. return copy_to_user(c->string, ptr.p_char, len + 1) ?
  1420. -EFAULT : 0;
  1421. case V4L2_CTRL_TYPE_INTEGER64:
  1422. c->value64 = *ptr.p_s64;
  1423. break;
  1424. default:
  1425. c->value = *ptr.p_s32;
  1426. break;
  1427. }
  1428. return 0;
  1429. }
  1430. /* Helper function: copy the current control value back to the caller */
  1431. static int cur_to_user(struct v4l2_ext_control *c,
  1432. struct v4l2_ctrl *ctrl)
  1433. {
  1434. return ptr_to_user(c, ctrl, ctrl->p_cur);
  1435. }
  1436. /* Helper function: copy the new control value back to the caller */
  1437. static int new_to_user(struct v4l2_ext_control *c,
  1438. struct v4l2_ctrl *ctrl)
  1439. {
  1440. return ptr_to_user(c, ctrl, ctrl->p_new);
  1441. }
  1442. /* Helper function: copy the caller-provider value to the given control value */
  1443. static int user_to_ptr(struct v4l2_ext_control *c,
  1444. struct v4l2_ctrl *ctrl,
  1445. union v4l2_ctrl_ptr ptr)
  1446. {
  1447. int ret;
  1448. u32 size;
  1449. ctrl->is_new = 1;
  1450. if (ctrl->is_ptr && !ctrl->is_string) {
  1451. unsigned idx;
  1452. ret = copy_from_user(ptr.p, c->ptr, c->size) ? -EFAULT : 0;
  1453. if (ret || !ctrl->is_array)
  1454. return ret;
  1455. for (idx = c->size / ctrl->elem_size; idx < ctrl->elems; idx++)
  1456. ctrl->type_ops->init(ctrl, idx, ptr);
  1457. return 0;
  1458. }
  1459. switch (ctrl->type) {
  1460. case V4L2_CTRL_TYPE_INTEGER64:
  1461. *ptr.p_s64 = c->value64;
  1462. break;
  1463. case V4L2_CTRL_TYPE_STRING:
  1464. size = c->size;
  1465. if (size == 0)
  1466. return -ERANGE;
  1467. if (size > ctrl->maximum + 1)
  1468. size = ctrl->maximum + 1;
  1469. ret = copy_from_user(ptr.p_char, c->string, size) ? -EFAULT : 0;
  1470. if (!ret) {
  1471. char last = ptr.p_char[size - 1];
  1472. ptr.p_char[size - 1] = 0;
  1473. /* If the string was longer than ctrl->maximum,
  1474. then return an error. */
  1475. if (strlen(ptr.p_char) == ctrl->maximum && last)
  1476. return -ERANGE;
  1477. }
  1478. return ret;
  1479. default:
  1480. *ptr.p_s32 = c->value;
  1481. break;
  1482. }
  1483. return 0;
  1484. }
  1485. /* Helper function: copy the caller-provider value as the new control value */
  1486. static int user_to_new(struct v4l2_ext_control *c,
  1487. struct v4l2_ctrl *ctrl)
  1488. {
  1489. return user_to_ptr(c, ctrl, ctrl->p_new);
  1490. }
  1491. /* Copy the one value to another. */
  1492. static void ptr_to_ptr(struct v4l2_ctrl *ctrl,
  1493. union v4l2_ctrl_ptr from, union v4l2_ctrl_ptr to)
  1494. {
  1495. if (ctrl == NULL)
  1496. return;
  1497. memcpy(to.p, from.p, ctrl->elems * ctrl->elem_size);
  1498. }
  1499. /* Copy the new value to the current value. */
  1500. static void new_to_cur(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, u32 ch_flags)
  1501. {
  1502. bool changed;
  1503. if (ctrl == NULL)
  1504. return;
  1505. /* has_changed is set by cluster_changed */
  1506. changed = ctrl->has_changed;
  1507. if (changed)
  1508. ptr_to_ptr(ctrl, ctrl->p_new, ctrl->p_cur);
  1509. if (ch_flags & V4L2_EVENT_CTRL_CH_FLAGS) {
  1510. /* Note: CH_FLAGS is only set for auto clusters. */
  1511. ctrl->flags &=
  1512. ~(V4L2_CTRL_FLAG_INACTIVE | V4L2_CTRL_FLAG_VOLATILE);
  1513. if (!is_cur_manual(ctrl->cluster[0])) {
  1514. ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
  1515. if (ctrl->cluster[0]->has_volatiles)
  1516. ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
  1517. }
  1518. fh = NULL;
  1519. }
  1520. if (changed || ch_flags) {
  1521. /* If a control was changed that was not one of the controls
  1522. modified by the application, then send the event to all. */
  1523. if (!ctrl->is_new)
  1524. fh = NULL;
  1525. send_event(fh, ctrl,
  1526. (changed ? V4L2_EVENT_CTRL_CH_VALUE : 0) | ch_flags);
  1527. if (ctrl->call_notify && changed && ctrl->handler->notify)
  1528. ctrl->handler->notify(ctrl, ctrl->handler->notify_priv);
  1529. }
  1530. }
  1531. /* Copy the current value to the new value */
  1532. static void cur_to_new(struct v4l2_ctrl *ctrl)
  1533. {
  1534. if (ctrl == NULL)
  1535. return;
  1536. ptr_to_ptr(ctrl, ctrl->p_cur, ctrl->p_new);
  1537. }
  1538. /* Return non-zero if one or more of the controls in the cluster has a new
  1539. value that differs from the current value. */
  1540. static int cluster_changed(struct v4l2_ctrl *master)
  1541. {
  1542. bool changed = false;
  1543. unsigned idx;
  1544. int i;
  1545. for (i = 0; i < master->ncontrols; i++) {
  1546. struct v4l2_ctrl *ctrl = master->cluster[i];
  1547. bool ctrl_changed = false;
  1548. if (ctrl == NULL)
  1549. continue;
  1550. if (ctrl->flags & V4L2_CTRL_FLAG_EXECUTE_ON_WRITE)
  1551. changed = ctrl_changed = true;
  1552. /*
  1553. * Set has_changed to false to avoid generating
  1554. * the event V4L2_EVENT_CTRL_CH_VALUE
  1555. */
  1556. if (ctrl->flags & V4L2_CTRL_FLAG_VOLATILE) {
  1557. ctrl->has_changed = false;
  1558. continue;
  1559. }
  1560. for (idx = 0; !ctrl_changed && idx < ctrl->elems; idx++)
  1561. ctrl_changed = !ctrl->type_ops->equal(ctrl, idx,
  1562. ctrl->p_cur, ctrl->p_new);
  1563. ctrl->has_changed = ctrl_changed;
  1564. changed |= ctrl->has_changed;
  1565. }
  1566. return changed;
  1567. }
  1568. /* Control range checking */
  1569. static int check_range(enum v4l2_ctrl_type type,
  1570. s64 min, s64 max, u64 step, s64 def)
  1571. {
  1572. switch (type) {
  1573. case V4L2_CTRL_TYPE_BOOLEAN:
  1574. if (step != 1 || max > 1 || min < 0)
  1575. return -ERANGE;
  1576. /* fall through */
  1577. case V4L2_CTRL_TYPE_U8:
  1578. case V4L2_CTRL_TYPE_U16:
  1579. case V4L2_CTRL_TYPE_U32:
  1580. case V4L2_CTRL_TYPE_INTEGER:
  1581. case V4L2_CTRL_TYPE_INTEGER64:
  1582. if (step == 0 || min > max || def < min || def > max)
  1583. return -ERANGE;
  1584. return 0;
  1585. case V4L2_CTRL_TYPE_BITMASK:
  1586. if (step || min || !max || (def & ~max))
  1587. return -ERANGE;
  1588. return 0;
  1589. case V4L2_CTRL_TYPE_MENU:
  1590. case V4L2_CTRL_TYPE_INTEGER_MENU:
  1591. if (min > max || def < min || def > max)
  1592. return -ERANGE;
  1593. /* Note: step == menu_skip_mask for menu controls.
  1594. So here we check if the default value is masked out. */
  1595. if (step && ((1 << def) & step))
  1596. return -EINVAL;
  1597. return 0;
  1598. case V4L2_CTRL_TYPE_STRING:
  1599. if (min > max || min < 0 || step < 1 || def)
  1600. return -ERANGE;
  1601. return 0;
  1602. default:
  1603. return 0;
  1604. }
  1605. }
  1606. /* Validate a new control */
  1607. static int validate_new(const struct v4l2_ctrl *ctrl, union v4l2_ctrl_ptr p_new)
  1608. {
  1609. unsigned idx;
  1610. int err = 0;
  1611. for (idx = 0; !err && idx < ctrl->elems; idx++)
  1612. err = ctrl->type_ops->validate(ctrl, idx, p_new);
  1613. return err;
  1614. }
  1615. static inline u32 node2id(struct list_head *node)
  1616. {
  1617. return list_entry(node, struct v4l2_ctrl_ref, node)->ctrl->id;
  1618. }
  1619. /* Set the handler's error code if it wasn't set earlier already */
  1620. static inline int handler_set_err(struct v4l2_ctrl_handler *hdl, int err)
  1621. {
  1622. if (hdl->error == 0)
  1623. hdl->error = err;
  1624. return err;
  1625. }
  1626. /* Initialize the handler */
  1627. int v4l2_ctrl_handler_init_class(struct v4l2_ctrl_handler *hdl,
  1628. unsigned nr_of_controls_hint,
  1629. struct lock_class_key *key, const char *name)
  1630. {
  1631. hdl->lock = &hdl->_lock;
  1632. mutex_init(hdl->lock);
  1633. lockdep_set_class_and_name(hdl->lock, key, name);
  1634. INIT_LIST_HEAD(&hdl->ctrls);
  1635. INIT_LIST_HEAD(&hdl->ctrl_refs);
  1636. hdl->nr_of_buckets = 1 + nr_of_controls_hint / 8;
  1637. hdl->buckets = kcalloc(hdl->nr_of_buckets, sizeof(hdl->buckets[0]),
  1638. GFP_KERNEL);
  1639. hdl->error = hdl->buckets ? 0 : -ENOMEM;
  1640. return hdl->error;
  1641. }
  1642. EXPORT_SYMBOL(v4l2_ctrl_handler_init_class);
  1643. /* Free all controls and control refs */
  1644. void v4l2_ctrl_handler_free(struct v4l2_ctrl_handler *hdl)
  1645. {
  1646. struct v4l2_ctrl_ref *ref, *next_ref;
  1647. struct v4l2_ctrl *ctrl, *next_ctrl;
  1648. struct v4l2_subscribed_event *sev, *next_sev;
  1649. if (hdl == NULL || hdl->buckets == NULL)
  1650. return;
  1651. mutex_lock(hdl->lock);
  1652. /* Free all nodes */
  1653. list_for_each_entry_safe(ref, next_ref, &hdl->ctrl_refs, node) {
  1654. list_del(&ref->node);
  1655. kfree(ref);
  1656. }
  1657. /* Free all controls owned by the handler */
  1658. list_for_each_entry_safe(ctrl, next_ctrl, &hdl->ctrls, node) {
  1659. list_del(&ctrl->node);
  1660. list_for_each_entry_safe(sev, next_sev, &ctrl->ev_subs, node)
  1661. list_del(&sev->node);
  1662. kfree(ctrl);
  1663. }
  1664. kfree(hdl->buckets);
  1665. hdl->buckets = NULL;
  1666. hdl->cached = NULL;
  1667. hdl->error = 0;
  1668. mutex_unlock(hdl->lock);
  1669. }
  1670. EXPORT_SYMBOL(v4l2_ctrl_handler_free);
  1671. /* For backwards compatibility: V4L2_CID_PRIVATE_BASE should no longer
  1672. be used except in G_CTRL, S_CTRL, QUERYCTRL and QUERYMENU when dealing
  1673. with applications that do not use the NEXT_CTRL flag.
  1674. We just find the n-th private user control. It's O(N), but that should not
  1675. be an issue in this particular case. */
  1676. static struct v4l2_ctrl_ref *find_private_ref(
  1677. struct v4l2_ctrl_handler *hdl, u32 id)
  1678. {
  1679. struct v4l2_ctrl_ref *ref;
  1680. id -= V4L2_CID_PRIVATE_BASE;
  1681. list_for_each_entry(ref, &hdl->ctrl_refs, node) {
  1682. /* Search for private user controls that are compatible with
  1683. VIDIOC_G/S_CTRL. */
  1684. if (V4L2_CTRL_ID2CLASS(ref->ctrl->id) == V4L2_CTRL_CLASS_USER &&
  1685. V4L2_CTRL_DRIVER_PRIV(ref->ctrl->id)) {
  1686. if (!ref->ctrl->is_int)
  1687. continue;
  1688. if (id == 0)
  1689. return ref;
  1690. id--;
  1691. }
  1692. }
  1693. return NULL;
  1694. }
  1695. /* Find a control with the given ID. */
  1696. static struct v4l2_ctrl_ref *find_ref(struct v4l2_ctrl_handler *hdl, u32 id)
  1697. {
  1698. struct v4l2_ctrl_ref *ref;
  1699. int bucket;
  1700. id &= V4L2_CTRL_ID_MASK;
  1701. /* Old-style private controls need special handling */
  1702. if (id >= V4L2_CID_PRIVATE_BASE)
  1703. return find_private_ref(hdl, id);
  1704. bucket = id % hdl->nr_of_buckets;
  1705. /* Simple optimization: cache the last control found */
  1706. if (hdl->cached && hdl->cached->ctrl->id == id)
  1707. return hdl->cached;
  1708. /* Not in cache, search the hash */
  1709. ref = hdl->buckets ? hdl->buckets[bucket] : NULL;
  1710. while (ref && ref->ctrl->id != id)
  1711. ref = ref->next;
  1712. if (ref)
  1713. hdl->cached = ref; /* cache it! */
  1714. return ref;
  1715. }
  1716. /* Find a control with the given ID. Take the handler's lock first. */
  1717. static struct v4l2_ctrl_ref *find_ref_lock(
  1718. struct v4l2_ctrl_handler *hdl, u32 id)
  1719. {
  1720. struct v4l2_ctrl_ref *ref = NULL;
  1721. if (hdl) {
  1722. mutex_lock(hdl->lock);
  1723. ref = find_ref(hdl, id);
  1724. mutex_unlock(hdl->lock);
  1725. }
  1726. return ref;
  1727. }
  1728. /* Find a control with the given ID. */
  1729. struct v4l2_ctrl *v4l2_ctrl_find(struct v4l2_ctrl_handler *hdl, u32 id)
  1730. {
  1731. struct v4l2_ctrl_ref *ref = find_ref_lock(hdl, id);
  1732. return ref ? ref->ctrl : NULL;
  1733. }
  1734. EXPORT_SYMBOL(v4l2_ctrl_find);
  1735. /* Allocate a new v4l2_ctrl_ref and hook it into the handler. */
  1736. static int handler_new_ref(struct v4l2_ctrl_handler *hdl,
  1737. struct v4l2_ctrl *ctrl)
  1738. {
  1739. struct v4l2_ctrl_ref *ref;
  1740. struct v4l2_ctrl_ref *new_ref;
  1741. u32 id = ctrl->id;
  1742. u32 class_ctrl = V4L2_CTRL_ID2CLASS(id) | 1;
  1743. int bucket = id % hdl->nr_of_buckets; /* which bucket to use */
  1744. /*
  1745. * Automatically add the control class if it is not yet present and
  1746. * the new control is not a compound control.
  1747. */
  1748. if (ctrl->type < V4L2_CTRL_COMPOUND_TYPES &&
  1749. id != class_ctrl && find_ref_lock(hdl, class_ctrl) == NULL)
  1750. if (!v4l2_ctrl_new_std(hdl, NULL, class_ctrl, 0, 0, 0, 0))
  1751. return hdl->error;
  1752. if (hdl->error)
  1753. return hdl->error;
  1754. new_ref = kzalloc(sizeof(*new_ref), GFP_KERNEL);
  1755. if (!new_ref)
  1756. return handler_set_err(hdl, -ENOMEM);
  1757. new_ref->ctrl = ctrl;
  1758. if (ctrl->handler == hdl) {
  1759. /* By default each control starts in a cluster of its own.
  1760. new_ref->ctrl is basically a cluster array with one
  1761. element, so that's perfect to use as the cluster pointer.
  1762. But only do this for the handler that owns the control. */
  1763. ctrl->cluster = &new_ref->ctrl;
  1764. ctrl->ncontrols = 1;
  1765. }
  1766. INIT_LIST_HEAD(&new_ref->node);
  1767. mutex_lock(hdl->lock);
  1768. /* Add immediately at the end of the list if the list is empty, or if
  1769. the last element in the list has a lower ID.
  1770. This ensures that when elements are added in ascending order the
  1771. insertion is an O(1) operation. */
  1772. if (list_empty(&hdl->ctrl_refs) || id > node2id(hdl->ctrl_refs.prev)) {
  1773. list_add_tail(&new_ref->node, &hdl->ctrl_refs);
  1774. goto insert_in_hash;
  1775. }
  1776. /* Find insert position in sorted list */
  1777. list_for_each_entry(ref, &hdl->ctrl_refs, node) {
  1778. if (ref->ctrl->id < id)
  1779. continue;
  1780. /* Don't add duplicates */
  1781. if (ref->ctrl->id == id) {
  1782. kfree(new_ref);
  1783. goto unlock;
  1784. }
  1785. list_add(&new_ref->node, ref->node.prev);
  1786. break;
  1787. }
  1788. insert_in_hash:
  1789. /* Insert the control node in the hash */
  1790. new_ref->next = hdl->buckets[bucket];
  1791. hdl->buckets[bucket] = new_ref;
  1792. unlock:
  1793. mutex_unlock(hdl->lock);
  1794. return 0;
  1795. }
  1796. /* Add a new control */
  1797. static struct v4l2_ctrl *v4l2_ctrl_new(struct v4l2_ctrl_handler *hdl,
  1798. const struct v4l2_ctrl_ops *ops,
  1799. const struct v4l2_ctrl_type_ops *type_ops,
  1800. u32 id, const char *name, enum v4l2_ctrl_type type,
  1801. s64 min, s64 max, u64 step, s64 def,
  1802. const u32 dims[V4L2_CTRL_MAX_DIMS], u32 elem_size,
  1803. u32 flags, const char * const *qmenu,
  1804. const s64 *qmenu_int, void *priv)
  1805. {
  1806. struct v4l2_ctrl *ctrl;
  1807. unsigned sz_extra;
  1808. unsigned nr_of_dims = 0;
  1809. unsigned elems = 1;
  1810. bool is_array;
  1811. unsigned tot_ctrl_size;
  1812. unsigned idx;
  1813. void *data;
  1814. int err;
  1815. if (hdl->error)
  1816. return NULL;
  1817. while (dims && dims[nr_of_dims]) {
  1818. elems *= dims[nr_of_dims];
  1819. nr_of_dims++;
  1820. if (nr_of_dims == V4L2_CTRL_MAX_DIMS)
  1821. break;
  1822. }
  1823. is_array = nr_of_dims > 0;
  1824. /* Prefill elem_size for all types handled by std_type_ops */
  1825. switch (type) {
  1826. case V4L2_CTRL_TYPE_INTEGER64:
  1827. elem_size = sizeof(s64);
  1828. break;
  1829. case V4L2_CTRL_TYPE_STRING:
  1830. elem_size = max + 1;
  1831. break;
  1832. case V4L2_CTRL_TYPE_U8:
  1833. elem_size = sizeof(u8);
  1834. break;
  1835. case V4L2_CTRL_TYPE_U16:
  1836. elem_size = sizeof(u16);
  1837. break;
  1838. case V4L2_CTRL_TYPE_U32:
  1839. elem_size = sizeof(u32);
  1840. break;
  1841. default:
  1842. if (type < V4L2_CTRL_COMPOUND_TYPES)
  1843. elem_size = sizeof(s32);
  1844. break;
  1845. }
  1846. tot_ctrl_size = elem_size * elems;
  1847. /* Sanity checks */
  1848. if (id == 0 || name == NULL || !elem_size ||
  1849. id >= V4L2_CID_PRIVATE_BASE ||
  1850. (type == V4L2_CTRL_TYPE_MENU && qmenu == NULL) ||
  1851. (type == V4L2_CTRL_TYPE_INTEGER_MENU && qmenu_int == NULL)) {
  1852. handler_set_err(hdl, -ERANGE);
  1853. return NULL;
  1854. }
  1855. err = check_range(type, min, max, step, def);
  1856. if (err) {
  1857. handler_set_err(hdl, err);
  1858. return NULL;
  1859. }
  1860. if (type == V4L2_CTRL_TYPE_BITMASK && ((def & ~max) || min || step)) {
  1861. handler_set_err(hdl, -ERANGE);
  1862. return NULL;
  1863. }
  1864. if (is_array &&
  1865. (type == V4L2_CTRL_TYPE_BUTTON ||
  1866. type == V4L2_CTRL_TYPE_CTRL_CLASS)) {
  1867. handler_set_err(hdl, -EINVAL);
  1868. return NULL;
  1869. }
  1870. sz_extra = 0;
  1871. if (type == V4L2_CTRL_TYPE_BUTTON)
  1872. flags |= V4L2_CTRL_FLAG_WRITE_ONLY |
  1873. V4L2_CTRL_FLAG_EXECUTE_ON_WRITE;
  1874. else if (type == V4L2_CTRL_TYPE_CTRL_CLASS)
  1875. flags |= V4L2_CTRL_FLAG_READ_ONLY;
  1876. else if (type == V4L2_CTRL_TYPE_INTEGER64 ||
  1877. type == V4L2_CTRL_TYPE_STRING ||
  1878. type >= V4L2_CTRL_COMPOUND_TYPES ||
  1879. is_array)
  1880. sz_extra += 2 * tot_ctrl_size;
  1881. ctrl = kzalloc(sizeof(*ctrl) + sz_extra, GFP_KERNEL);
  1882. if (ctrl == NULL) {
  1883. handler_set_err(hdl, -ENOMEM);
  1884. return NULL;
  1885. }
  1886. INIT_LIST_HEAD(&ctrl->node);
  1887. INIT_LIST_HEAD(&ctrl->ev_subs);
  1888. ctrl->handler = hdl;
  1889. ctrl->ops = ops;
  1890. ctrl->type_ops = type_ops ? type_ops : &std_type_ops;
  1891. ctrl->id = id;
  1892. ctrl->name = name;
  1893. ctrl->type = type;
  1894. ctrl->flags = flags;
  1895. ctrl->minimum = min;
  1896. ctrl->maximum = max;
  1897. ctrl->step = step;
  1898. ctrl->default_value = def;
  1899. ctrl->is_string = !is_array && type == V4L2_CTRL_TYPE_STRING;
  1900. ctrl->is_ptr = is_array || type >= V4L2_CTRL_COMPOUND_TYPES || ctrl->is_string;
  1901. ctrl->is_int = !ctrl->is_ptr && type != V4L2_CTRL_TYPE_INTEGER64;
  1902. ctrl->is_array = is_array;
  1903. ctrl->elems = elems;
  1904. ctrl->nr_of_dims = nr_of_dims;
  1905. if (nr_of_dims)
  1906. memcpy(ctrl->dims, dims, nr_of_dims * sizeof(dims[0]));
  1907. ctrl->elem_size = elem_size;
  1908. if (type == V4L2_CTRL_TYPE_MENU)
  1909. ctrl->qmenu = qmenu;
  1910. else if (type == V4L2_CTRL_TYPE_INTEGER_MENU)
  1911. ctrl->qmenu_int = qmenu_int;
  1912. ctrl->priv = priv;
  1913. ctrl->cur.val = ctrl->val = def;
  1914. data = &ctrl[1];
  1915. if (!ctrl->is_int) {
  1916. ctrl->p_new.p = data;
  1917. ctrl->p_cur.p = data + tot_ctrl_size;
  1918. } else {
  1919. ctrl->p_new.p = &ctrl->val;
  1920. ctrl->p_cur.p = &ctrl->cur.val;
  1921. }
  1922. for (idx = 0; idx < elems; idx++) {
  1923. ctrl->type_ops->init(ctrl, idx, ctrl->p_cur);
  1924. ctrl->type_ops->init(ctrl, idx, ctrl->p_new);
  1925. }
  1926. if (handler_new_ref(hdl, ctrl)) {
  1927. kfree(ctrl);
  1928. return NULL;
  1929. }
  1930. mutex_lock(hdl->lock);
  1931. list_add_tail(&ctrl->node, &hdl->ctrls);
  1932. mutex_unlock(hdl->lock);
  1933. return ctrl;
  1934. }
  1935. struct v4l2_ctrl *v4l2_ctrl_new_custom(struct v4l2_ctrl_handler *hdl,
  1936. const struct v4l2_ctrl_config *cfg, void *priv)
  1937. {
  1938. bool is_menu;
  1939. struct v4l2_ctrl *ctrl;
  1940. const char *name = cfg->name;
  1941. const char * const *qmenu = cfg->qmenu;
  1942. const s64 *qmenu_int = cfg->qmenu_int;
  1943. enum v4l2_ctrl_type type = cfg->type;
  1944. u32 flags = cfg->flags;
  1945. s64 min = cfg->min;
  1946. s64 max = cfg->max;
  1947. u64 step = cfg->step;
  1948. s64 def = cfg->def;
  1949. if (name == NULL)
  1950. v4l2_ctrl_fill(cfg->id, &name, &type, &min, &max, &step,
  1951. &def, &flags);
  1952. is_menu = (cfg->type == V4L2_CTRL_TYPE_MENU ||
  1953. cfg->type == V4L2_CTRL_TYPE_INTEGER_MENU);
  1954. if (is_menu)
  1955. WARN_ON(step);
  1956. else
  1957. WARN_ON(cfg->menu_skip_mask);
  1958. if (cfg->type == V4L2_CTRL_TYPE_MENU && qmenu == NULL)
  1959. qmenu = v4l2_ctrl_get_menu(cfg->id);
  1960. else if (cfg->type == V4L2_CTRL_TYPE_INTEGER_MENU &&
  1961. qmenu_int == NULL) {
  1962. handler_set_err(hdl, -EINVAL);
  1963. return NULL;
  1964. }
  1965. ctrl = v4l2_ctrl_new(hdl, cfg->ops, cfg->type_ops, cfg->id, name,
  1966. type, min, max,
  1967. is_menu ? cfg->menu_skip_mask : step, def,
  1968. cfg->dims, cfg->elem_size,
  1969. flags, qmenu, qmenu_int, priv);
  1970. if (ctrl)
  1971. ctrl->is_private = cfg->is_private;
  1972. return ctrl;
  1973. }
  1974. EXPORT_SYMBOL(v4l2_ctrl_new_custom);
  1975. /* Helper function for standard non-menu controls */
  1976. struct v4l2_ctrl *v4l2_ctrl_new_std(struct v4l2_ctrl_handler *hdl,
  1977. const struct v4l2_ctrl_ops *ops,
  1978. u32 id, s64 min, s64 max, u64 step, s64 def)
  1979. {
  1980. const char *name;
  1981. enum v4l2_ctrl_type type;
  1982. u32 flags;
  1983. v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
  1984. if (type == V4L2_CTRL_TYPE_MENU ||
  1985. type == V4L2_CTRL_TYPE_INTEGER_MENU ||
  1986. type >= V4L2_CTRL_COMPOUND_TYPES) {
  1987. handler_set_err(hdl, -EINVAL);
  1988. return NULL;
  1989. }
  1990. return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
  1991. min, max, step, def, NULL, 0,
  1992. flags, NULL, NULL, NULL);
  1993. }
  1994. EXPORT_SYMBOL(v4l2_ctrl_new_std);
  1995. /* Helper function for standard menu controls */
  1996. struct v4l2_ctrl *v4l2_ctrl_new_std_menu(struct v4l2_ctrl_handler *hdl,
  1997. const struct v4l2_ctrl_ops *ops,
  1998. u32 id, u8 _max, u64 mask, u8 _def)
  1999. {
  2000. const char * const *qmenu = NULL;
  2001. const s64 *qmenu_int = NULL;
  2002. unsigned int qmenu_int_len = 0;
  2003. const char *name;
  2004. enum v4l2_ctrl_type type;
  2005. s64 min;
  2006. s64 max = _max;
  2007. s64 def = _def;
  2008. u64 step;
  2009. u32 flags;
  2010. v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
  2011. if (type == V4L2_CTRL_TYPE_MENU)
  2012. qmenu = v4l2_ctrl_get_menu(id);
  2013. else if (type == V4L2_CTRL_TYPE_INTEGER_MENU)
  2014. qmenu_int = v4l2_ctrl_get_int_menu(id, &qmenu_int_len);
  2015. if ((!qmenu && !qmenu_int) || (qmenu_int && max > qmenu_int_len)) {
  2016. handler_set_err(hdl, -EINVAL);
  2017. return NULL;
  2018. }
  2019. return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
  2020. 0, max, mask, def, NULL, 0,
  2021. flags, qmenu, qmenu_int, NULL);
  2022. }
  2023. EXPORT_SYMBOL(v4l2_ctrl_new_std_menu);
  2024. /* Helper function for standard menu controls with driver defined menu */
  2025. struct v4l2_ctrl *v4l2_ctrl_new_std_menu_items(struct v4l2_ctrl_handler *hdl,
  2026. const struct v4l2_ctrl_ops *ops, u32 id, u8 _max,
  2027. u64 mask, u8 _def, const char * const *qmenu)
  2028. {
  2029. enum v4l2_ctrl_type type;
  2030. const char *name;
  2031. u32 flags;
  2032. u64 step;
  2033. s64 min;
  2034. s64 max = _max;
  2035. s64 def = _def;
  2036. /* v4l2_ctrl_new_std_menu_items() should only be called for
  2037. * standard controls without a standard menu.
  2038. */
  2039. if (v4l2_ctrl_get_menu(id)) {
  2040. handler_set_err(hdl, -EINVAL);
  2041. return NULL;
  2042. }
  2043. v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
  2044. if (type != V4L2_CTRL_TYPE_MENU || qmenu == NULL) {
  2045. handler_set_err(hdl, -EINVAL);
  2046. return NULL;
  2047. }
  2048. return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
  2049. 0, max, mask, def, NULL, 0,
  2050. flags, qmenu, NULL, NULL);
  2051. }
  2052. EXPORT_SYMBOL(v4l2_ctrl_new_std_menu_items);
  2053. /* Helper function for standard integer menu controls */
  2054. struct v4l2_ctrl *v4l2_ctrl_new_int_menu(struct v4l2_ctrl_handler *hdl,
  2055. const struct v4l2_ctrl_ops *ops,
  2056. u32 id, u8 _max, u8 _def, const s64 *qmenu_int)
  2057. {
  2058. const char *name;
  2059. enum v4l2_ctrl_type type;
  2060. s64 min;
  2061. u64 step;
  2062. s64 max = _max;
  2063. s64 def = _def;
  2064. u32 flags;
  2065. v4l2_ctrl_fill(id, &name, &type, &min, &max, &step, &def, &flags);
  2066. if (type != V4L2_CTRL_TYPE_INTEGER_MENU) {
  2067. handler_set_err(hdl, -EINVAL);
  2068. return NULL;
  2069. }
  2070. return v4l2_ctrl_new(hdl, ops, NULL, id, name, type,
  2071. 0, max, 0, def, NULL, 0,
  2072. flags, NULL, qmenu_int, NULL);
  2073. }
  2074. EXPORT_SYMBOL(v4l2_ctrl_new_int_menu);
  2075. /* Add a control from another handler to this handler */
  2076. struct v4l2_ctrl *v4l2_ctrl_add_ctrl(struct v4l2_ctrl_handler *hdl,
  2077. struct v4l2_ctrl *ctrl)
  2078. {
  2079. if (hdl == NULL || hdl->error)
  2080. return NULL;
  2081. if (ctrl == NULL) {
  2082. handler_set_err(hdl, -EINVAL);
  2083. return NULL;
  2084. }
  2085. if (ctrl->handler == hdl)
  2086. return ctrl;
  2087. return handler_new_ref(hdl, ctrl) ? NULL : ctrl;
  2088. }
  2089. EXPORT_SYMBOL(v4l2_ctrl_add_ctrl);
  2090. /* Add the controls from another handler to our own. */
  2091. int v4l2_ctrl_add_handler(struct v4l2_ctrl_handler *hdl,
  2092. struct v4l2_ctrl_handler *add,
  2093. bool (*filter)(const struct v4l2_ctrl *ctrl))
  2094. {
  2095. struct v4l2_ctrl_ref *ref;
  2096. int ret = 0;
  2097. /* Do nothing if either handler is NULL or if they are the same */
  2098. if (!hdl || !add || hdl == add)
  2099. return 0;
  2100. if (hdl->error)
  2101. return hdl->error;
  2102. mutex_lock(add->lock);
  2103. list_for_each_entry(ref, &add->ctrl_refs, node) {
  2104. struct v4l2_ctrl *ctrl = ref->ctrl;
  2105. /* Skip handler-private controls. */
  2106. if (ctrl->is_private)
  2107. continue;
  2108. /* And control classes */
  2109. if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
  2110. continue;
  2111. /* Filter any unwanted controls */
  2112. if (filter && !filter(ctrl))
  2113. continue;
  2114. ret = handler_new_ref(hdl, ctrl);
  2115. if (ret)
  2116. break;
  2117. }
  2118. mutex_unlock(add->lock);
  2119. return ret;
  2120. }
  2121. EXPORT_SYMBOL(v4l2_ctrl_add_handler);
  2122. bool v4l2_ctrl_radio_filter(const struct v4l2_ctrl *ctrl)
  2123. {
  2124. if (V4L2_CTRL_ID2CLASS(ctrl->id) == V4L2_CTRL_CLASS_FM_TX)
  2125. return true;
  2126. if (V4L2_CTRL_ID2CLASS(ctrl->id) == V4L2_CTRL_CLASS_FM_RX)
  2127. return true;
  2128. switch (ctrl->id) {
  2129. case V4L2_CID_AUDIO_MUTE:
  2130. case V4L2_CID_AUDIO_VOLUME:
  2131. case V4L2_CID_AUDIO_BALANCE:
  2132. case V4L2_CID_AUDIO_BASS:
  2133. case V4L2_CID_AUDIO_TREBLE:
  2134. case V4L2_CID_AUDIO_LOUDNESS:
  2135. return true;
  2136. default:
  2137. break;
  2138. }
  2139. return false;
  2140. }
  2141. EXPORT_SYMBOL(v4l2_ctrl_radio_filter);
  2142. /* Cluster controls */
  2143. void v4l2_ctrl_cluster(unsigned ncontrols, struct v4l2_ctrl **controls)
  2144. {
  2145. bool has_volatiles = false;
  2146. int i;
  2147. /* The first control is the master control and it must not be NULL */
  2148. if (WARN_ON(ncontrols == 0 || controls[0] == NULL))
  2149. return;
  2150. for (i = 0; i < ncontrols; i++) {
  2151. if (controls[i]) {
  2152. controls[i]->cluster = controls;
  2153. controls[i]->ncontrols = ncontrols;
  2154. if (controls[i]->flags & V4L2_CTRL_FLAG_VOLATILE)
  2155. has_volatiles = true;
  2156. }
  2157. }
  2158. controls[0]->has_volatiles = has_volatiles;
  2159. }
  2160. EXPORT_SYMBOL(v4l2_ctrl_cluster);
  2161. void v4l2_ctrl_auto_cluster(unsigned ncontrols, struct v4l2_ctrl **controls,
  2162. u8 manual_val, bool set_volatile)
  2163. {
  2164. struct v4l2_ctrl *master = controls[0];
  2165. u32 flag = 0;
  2166. int i;
  2167. v4l2_ctrl_cluster(ncontrols, controls);
  2168. WARN_ON(ncontrols <= 1);
  2169. WARN_ON(manual_val < master->minimum || manual_val > master->maximum);
  2170. WARN_ON(set_volatile && !has_op(master, g_volatile_ctrl));
  2171. master->is_auto = true;
  2172. master->has_volatiles = set_volatile;
  2173. master->manual_mode_value = manual_val;
  2174. master->flags |= V4L2_CTRL_FLAG_UPDATE;
  2175. if (!is_cur_manual(master))
  2176. flag = V4L2_CTRL_FLAG_INACTIVE |
  2177. (set_volatile ? V4L2_CTRL_FLAG_VOLATILE : 0);
  2178. for (i = 1; i < ncontrols; i++)
  2179. if (controls[i])
  2180. controls[i]->flags |= flag;
  2181. }
  2182. EXPORT_SYMBOL(v4l2_ctrl_auto_cluster);
  2183. /* Activate/deactivate a control. */
  2184. void v4l2_ctrl_activate(struct v4l2_ctrl *ctrl, bool active)
  2185. {
  2186. /* invert since the actual flag is called 'inactive' */
  2187. bool inactive = !active;
  2188. bool old;
  2189. if (ctrl == NULL)
  2190. return;
  2191. if (inactive)
  2192. /* set V4L2_CTRL_FLAG_INACTIVE */
  2193. old = test_and_set_bit(4, &ctrl->flags);
  2194. else
  2195. /* clear V4L2_CTRL_FLAG_INACTIVE */
  2196. old = test_and_clear_bit(4, &ctrl->flags);
  2197. if (old != inactive)
  2198. send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS);
  2199. }
  2200. EXPORT_SYMBOL(v4l2_ctrl_activate);
  2201. /* Grab/ungrab a control.
  2202. Typically used when streaming starts and you want to grab controls,
  2203. preventing the user from changing them.
  2204. Just call this and the framework will block any attempts to change
  2205. these controls. */
  2206. void v4l2_ctrl_grab(struct v4l2_ctrl *ctrl, bool grabbed)
  2207. {
  2208. bool old;
  2209. if (ctrl == NULL)
  2210. return;
  2211. v4l2_ctrl_lock(ctrl);
  2212. if (grabbed)
  2213. /* set V4L2_CTRL_FLAG_GRABBED */
  2214. old = test_and_set_bit(1, &ctrl->flags);
  2215. else
  2216. /* clear V4L2_CTRL_FLAG_GRABBED */
  2217. old = test_and_clear_bit(1, &ctrl->flags);
  2218. if (old != grabbed)
  2219. send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_FLAGS);
  2220. v4l2_ctrl_unlock(ctrl);
  2221. }
  2222. EXPORT_SYMBOL(v4l2_ctrl_grab);
  2223. /* Log the control name and value */
  2224. static void log_ctrl(const struct v4l2_ctrl *ctrl,
  2225. const char *prefix, const char *colon)
  2226. {
  2227. if (ctrl->flags & (V4L2_CTRL_FLAG_DISABLED | V4L2_CTRL_FLAG_WRITE_ONLY))
  2228. return;
  2229. if (ctrl->type == V4L2_CTRL_TYPE_CTRL_CLASS)
  2230. return;
  2231. pr_info("%s%s%s: ", prefix, colon, ctrl->name);
  2232. ctrl->type_ops->log(ctrl);
  2233. if (ctrl->flags & (V4L2_CTRL_FLAG_INACTIVE |
  2234. V4L2_CTRL_FLAG_GRABBED |
  2235. V4L2_CTRL_FLAG_VOLATILE)) {
  2236. if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE)
  2237. pr_cont(" inactive");
  2238. if (ctrl->flags & V4L2_CTRL_FLAG_GRABBED)
  2239. pr_cont(" grabbed");
  2240. if (ctrl->flags & V4L2_CTRL_FLAG_VOLATILE)
  2241. pr_cont(" volatile");
  2242. }
  2243. pr_cont("\n");
  2244. }
  2245. /* Log all controls owned by the handler */
  2246. void v4l2_ctrl_handler_log_status(struct v4l2_ctrl_handler *hdl,
  2247. const char *prefix)
  2248. {
  2249. struct v4l2_ctrl *ctrl;
  2250. const char *colon = "";
  2251. int len;
  2252. if (hdl == NULL)
  2253. return;
  2254. if (prefix == NULL)
  2255. prefix = "";
  2256. len = strlen(prefix);
  2257. if (len && prefix[len - 1] != ' ')
  2258. colon = ": ";
  2259. mutex_lock(hdl->lock);
  2260. list_for_each_entry(ctrl, &hdl->ctrls, node)
  2261. if (!(ctrl->flags & V4L2_CTRL_FLAG_DISABLED))
  2262. log_ctrl(ctrl, prefix, colon);
  2263. mutex_unlock(hdl->lock);
  2264. }
  2265. EXPORT_SYMBOL(v4l2_ctrl_handler_log_status);
  2266. int v4l2_ctrl_subdev_log_status(struct v4l2_subdev *sd)
  2267. {
  2268. v4l2_ctrl_handler_log_status(sd->ctrl_handler, sd->name);
  2269. return 0;
  2270. }
  2271. EXPORT_SYMBOL(v4l2_ctrl_subdev_log_status);
  2272. /* Call s_ctrl for all controls owned by the handler */
  2273. int v4l2_ctrl_handler_setup(struct v4l2_ctrl_handler *hdl)
  2274. {
  2275. struct v4l2_ctrl *ctrl;
  2276. int ret = 0;
  2277. if (hdl == NULL)
  2278. return 0;
  2279. mutex_lock(hdl->lock);
  2280. list_for_each_entry(ctrl, &hdl->ctrls, node)
  2281. ctrl->done = false;
  2282. list_for_each_entry(ctrl, &hdl->ctrls, node) {
  2283. struct v4l2_ctrl *master = ctrl->cluster[0];
  2284. int i;
  2285. /* Skip if this control was already handled by a cluster. */
  2286. /* Skip button controls and read-only controls. */
  2287. if (ctrl->done || ctrl->type == V4L2_CTRL_TYPE_BUTTON ||
  2288. (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY))
  2289. continue;
  2290. for (i = 0; i < master->ncontrols; i++) {
  2291. if (master->cluster[i]) {
  2292. cur_to_new(master->cluster[i]);
  2293. master->cluster[i]->is_new = 1;
  2294. master->cluster[i]->done = true;
  2295. }
  2296. }
  2297. ret = call_op(master, s_ctrl);
  2298. if (ret)
  2299. break;
  2300. }
  2301. mutex_unlock(hdl->lock);
  2302. return ret;
  2303. }
  2304. EXPORT_SYMBOL(v4l2_ctrl_handler_setup);
  2305. /* Implement VIDIOC_QUERY_EXT_CTRL */
  2306. int v4l2_query_ext_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_query_ext_ctrl *qc)
  2307. {
  2308. const unsigned next_flags = V4L2_CTRL_FLAG_NEXT_CTRL | V4L2_CTRL_FLAG_NEXT_COMPOUND;
  2309. u32 id = qc->id & V4L2_CTRL_ID_MASK;
  2310. struct v4l2_ctrl_ref *ref;
  2311. struct v4l2_ctrl *ctrl;
  2312. if (hdl == NULL)
  2313. return -EINVAL;
  2314. mutex_lock(hdl->lock);
  2315. /* Try to find it */
  2316. ref = find_ref(hdl, id);
  2317. if ((qc->id & next_flags) && !list_empty(&hdl->ctrl_refs)) {
  2318. bool is_compound;
  2319. /* Match any control that is not hidden */
  2320. unsigned mask = 1;
  2321. bool match = false;
  2322. if ((qc->id & next_flags) == V4L2_CTRL_FLAG_NEXT_COMPOUND) {
  2323. /* Match any hidden control */
  2324. match = true;
  2325. } else if ((qc->id & next_flags) == next_flags) {
  2326. /* Match any control, compound or not */
  2327. mask = 0;
  2328. }
  2329. /* Find the next control with ID > qc->id */
  2330. /* Did we reach the end of the control list? */
  2331. if (id >= node2id(hdl->ctrl_refs.prev)) {
  2332. ref = NULL; /* Yes, so there is no next control */
  2333. } else if (ref) {
  2334. /* We found a control with the given ID, so just get
  2335. the next valid one in the list. */
  2336. list_for_each_entry_continue(ref, &hdl->ctrl_refs, node) {
  2337. is_compound = ref->ctrl->is_array ||
  2338. ref->ctrl->type >= V4L2_CTRL_COMPOUND_TYPES;
  2339. if (id < ref->ctrl->id &&
  2340. (is_compound & mask) == match)
  2341. break;
  2342. }
  2343. if (&ref->node == &hdl->ctrl_refs)
  2344. ref = NULL;
  2345. } else {
  2346. /* No control with the given ID exists, so start
  2347. searching for the next largest ID. We know there
  2348. is one, otherwise the first 'if' above would have
  2349. been true. */
  2350. list_for_each_entry(ref, &hdl->ctrl_refs, node) {
  2351. is_compound = ref->ctrl->is_array ||
  2352. ref->ctrl->type >= V4L2_CTRL_COMPOUND_TYPES;
  2353. if (id < ref->ctrl->id &&
  2354. (is_compound & mask) == match)
  2355. break;
  2356. }
  2357. if (&ref->node == &hdl->ctrl_refs)
  2358. ref = NULL;
  2359. }
  2360. }
  2361. mutex_unlock(hdl->lock);
  2362. if (!ref)
  2363. return -EINVAL;
  2364. ctrl = ref->ctrl;
  2365. memset(qc, 0, sizeof(*qc));
  2366. if (id >= V4L2_CID_PRIVATE_BASE)
  2367. qc->id = id;
  2368. else
  2369. qc->id = ctrl->id;
  2370. strlcpy(qc->name, ctrl->name, sizeof(qc->name));
  2371. qc->flags = user_flags(ctrl);
  2372. qc->type = ctrl->type;
  2373. qc->elem_size = ctrl->elem_size;
  2374. qc->elems = ctrl->elems;
  2375. qc->nr_of_dims = ctrl->nr_of_dims;
  2376. memcpy(qc->dims, ctrl->dims, qc->nr_of_dims * sizeof(qc->dims[0]));
  2377. qc->minimum = ctrl->minimum;
  2378. qc->maximum = ctrl->maximum;
  2379. qc->default_value = ctrl->default_value;
  2380. if (ctrl->type == V4L2_CTRL_TYPE_MENU
  2381. || ctrl->type == V4L2_CTRL_TYPE_INTEGER_MENU)
  2382. qc->step = 1;
  2383. else
  2384. qc->step = ctrl->step;
  2385. return 0;
  2386. }
  2387. EXPORT_SYMBOL(v4l2_query_ext_ctrl);
  2388. /* Implement VIDIOC_QUERYCTRL */
  2389. int v4l2_queryctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_queryctrl *qc)
  2390. {
  2391. struct v4l2_query_ext_ctrl qec = { qc->id };
  2392. int rc;
  2393. rc = v4l2_query_ext_ctrl(hdl, &qec);
  2394. if (rc)
  2395. return rc;
  2396. qc->id = qec.id;
  2397. qc->type = qec.type;
  2398. qc->flags = qec.flags;
  2399. strlcpy(qc->name, qec.name, sizeof(qc->name));
  2400. switch (qc->type) {
  2401. case V4L2_CTRL_TYPE_INTEGER:
  2402. case V4L2_CTRL_TYPE_BOOLEAN:
  2403. case V4L2_CTRL_TYPE_MENU:
  2404. case V4L2_CTRL_TYPE_INTEGER_MENU:
  2405. case V4L2_CTRL_TYPE_STRING:
  2406. case V4L2_CTRL_TYPE_BITMASK:
  2407. qc->minimum = qec.minimum;
  2408. qc->maximum = qec.maximum;
  2409. qc->step = qec.step;
  2410. qc->default_value = qec.default_value;
  2411. break;
  2412. default:
  2413. qc->minimum = 0;
  2414. qc->maximum = 0;
  2415. qc->step = 0;
  2416. qc->default_value = 0;
  2417. break;
  2418. }
  2419. return 0;
  2420. }
  2421. EXPORT_SYMBOL(v4l2_queryctrl);
  2422. int v4l2_subdev_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
  2423. {
  2424. if (qc->id & (V4L2_CTRL_FLAG_NEXT_CTRL | V4L2_CTRL_FLAG_NEXT_COMPOUND))
  2425. return -EINVAL;
  2426. return v4l2_queryctrl(sd->ctrl_handler, qc);
  2427. }
  2428. EXPORT_SYMBOL(v4l2_subdev_queryctrl);
  2429. /* Implement VIDIOC_QUERYMENU */
  2430. int v4l2_querymenu(struct v4l2_ctrl_handler *hdl, struct v4l2_querymenu *qm)
  2431. {
  2432. struct v4l2_ctrl *ctrl;
  2433. u32 i = qm->index;
  2434. ctrl = v4l2_ctrl_find(hdl, qm->id);
  2435. if (!ctrl)
  2436. return -EINVAL;
  2437. qm->reserved = 0;
  2438. /* Sanity checks */
  2439. switch (ctrl->type) {
  2440. case V4L2_CTRL_TYPE_MENU:
  2441. if (ctrl->qmenu == NULL)
  2442. return -EINVAL;
  2443. break;
  2444. case V4L2_CTRL_TYPE_INTEGER_MENU:
  2445. if (ctrl->qmenu_int == NULL)
  2446. return -EINVAL;
  2447. break;
  2448. default:
  2449. return -EINVAL;
  2450. }
  2451. if (i < ctrl->minimum || i > ctrl->maximum)
  2452. return -EINVAL;
  2453. /* Use mask to see if this menu item should be skipped */
  2454. if (ctrl->menu_skip_mask & (1 << i))
  2455. return -EINVAL;
  2456. /* Empty menu items should also be skipped */
  2457. if (ctrl->type == V4L2_CTRL_TYPE_MENU) {
  2458. if (ctrl->qmenu[i] == NULL || ctrl->qmenu[i][0] == '\0')
  2459. return -EINVAL;
  2460. strlcpy(qm->name, ctrl->qmenu[i], sizeof(qm->name));
  2461. } else {
  2462. qm->value = ctrl->qmenu_int[i];
  2463. }
  2464. return 0;
  2465. }
  2466. EXPORT_SYMBOL(v4l2_querymenu);
  2467. int v4l2_subdev_querymenu(struct v4l2_subdev *sd, struct v4l2_querymenu *qm)
  2468. {
  2469. return v4l2_querymenu(sd->ctrl_handler, qm);
  2470. }
  2471. EXPORT_SYMBOL(v4l2_subdev_querymenu);
  2472. /* Some general notes on the atomic requirements of VIDIOC_G/TRY/S_EXT_CTRLS:
  2473. It is not a fully atomic operation, just best-effort only. After all, if
  2474. multiple controls have to be set through multiple i2c writes (for example)
  2475. then some initial writes may succeed while others fail. Thus leaving the
  2476. system in an inconsistent state. The question is how much effort you are
  2477. willing to spend on trying to make something atomic that really isn't.
  2478. From the point of view of an application the main requirement is that
  2479. when you call VIDIOC_S_EXT_CTRLS and some values are invalid then an
  2480. error should be returned without actually affecting any controls.
  2481. If all the values are correct, then it is acceptable to just give up
  2482. in case of low-level errors.
  2483. It is important though that the application can tell when only a partial
  2484. configuration was done. The way we do that is through the error_idx field
  2485. of struct v4l2_ext_controls: if that is equal to the count field then no
  2486. controls were affected. Otherwise all controls before that index were
  2487. successful in performing their 'get' or 'set' operation, the control at
  2488. the given index failed, and you don't know what happened with the controls
  2489. after the failed one. Since if they were part of a control cluster they
  2490. could have been successfully processed (if a cluster member was encountered
  2491. at index < error_idx), they could have failed (if a cluster member was at
  2492. error_idx), or they may not have been processed yet (if the first cluster
  2493. member appeared after error_idx).
  2494. It is all fairly theoretical, though. In practice all you can do is to
  2495. bail out. If error_idx == count, then it is an application bug. If
  2496. error_idx < count then it is only an application bug if the error code was
  2497. EBUSY. That usually means that something started streaming just when you
  2498. tried to set the controls. In all other cases it is a driver/hardware
  2499. problem and all you can do is to retry or bail out.
  2500. Note that these rules do not apply to VIDIOC_TRY_EXT_CTRLS: since that
  2501. never modifies controls the error_idx is just set to whatever control
  2502. has an invalid value.
  2503. */
  2504. /* Prepare for the extended g/s/try functions.
  2505. Find the controls in the control array and do some basic checks. */
  2506. static int prepare_ext_ctrls(struct v4l2_ctrl_handler *hdl,
  2507. struct v4l2_ext_controls *cs,
  2508. struct v4l2_ctrl_helper *helpers,
  2509. bool get)
  2510. {
  2511. struct v4l2_ctrl_helper *h;
  2512. bool have_clusters = false;
  2513. u32 i;
  2514. for (i = 0, h = helpers; i < cs->count; i++, h++) {
  2515. struct v4l2_ext_control *c = &cs->controls[i];
  2516. struct v4l2_ctrl_ref *ref;
  2517. struct v4l2_ctrl *ctrl;
  2518. u32 id = c->id & V4L2_CTRL_ID_MASK;
  2519. cs->error_idx = i;
  2520. if (cs->ctrl_class && V4L2_CTRL_ID2CLASS(id) != cs->ctrl_class)
  2521. return -EINVAL;
  2522. /* Old-style private controls are not allowed for
  2523. extended controls */
  2524. if (id >= V4L2_CID_PRIVATE_BASE)
  2525. return -EINVAL;
  2526. ref = find_ref_lock(hdl, id);
  2527. if (ref == NULL)
  2528. return -EINVAL;
  2529. ctrl = ref->ctrl;
  2530. if (ctrl->flags & V4L2_CTRL_FLAG_DISABLED)
  2531. return -EINVAL;
  2532. if (ctrl->cluster[0]->ncontrols > 1)
  2533. have_clusters = true;
  2534. if (ctrl->cluster[0] != ctrl)
  2535. ref = find_ref_lock(hdl, ctrl->cluster[0]->id);
  2536. if (ctrl->is_ptr && !ctrl->is_string) {
  2537. unsigned tot_size = ctrl->elems * ctrl->elem_size;
  2538. if (c->size < tot_size) {
  2539. if (get) {
  2540. c->size = tot_size;
  2541. return -ENOSPC;
  2542. }
  2543. return -EFAULT;
  2544. }
  2545. c->size = tot_size;
  2546. }
  2547. /* Store the ref to the master control of the cluster */
  2548. h->mref = ref;
  2549. h->ctrl = ctrl;
  2550. /* Initially set next to 0, meaning that there is no other
  2551. control in this helper array belonging to the same
  2552. cluster */
  2553. h->next = 0;
  2554. }
  2555. /* We are done if there were no controls that belong to a multi-
  2556. control cluster. */
  2557. if (!have_clusters)
  2558. return 0;
  2559. /* The code below figures out in O(n) time which controls in the list
  2560. belong to the same cluster. */
  2561. /* This has to be done with the handler lock taken. */
  2562. mutex_lock(hdl->lock);
  2563. /* First zero the helper field in the master control references */
  2564. for (i = 0; i < cs->count; i++)
  2565. helpers[i].mref->helper = NULL;
  2566. for (i = 0, h = helpers; i < cs->count; i++, h++) {
  2567. struct v4l2_ctrl_ref *mref = h->mref;
  2568. /* If the mref->helper is set, then it points to an earlier
  2569. helper that belongs to the same cluster. */
  2570. if (mref->helper) {
  2571. /* Set the next field of mref->helper to the current
  2572. index: this means that that earlier helper now
  2573. points to the next helper in the same cluster. */
  2574. mref->helper->next = i;
  2575. /* mref should be set only for the first helper in the
  2576. cluster, clear the others. */
  2577. h->mref = NULL;
  2578. }
  2579. /* Point the mref helper to the current helper struct. */
  2580. mref->helper = h;
  2581. }
  2582. mutex_unlock(hdl->lock);
  2583. return 0;
  2584. }
  2585. /* Handles the corner case where cs->count == 0. It checks whether the
  2586. specified control class exists. If that class ID is 0, then it checks
  2587. whether there are any controls at all. */
  2588. static int class_check(struct v4l2_ctrl_handler *hdl, u32 ctrl_class)
  2589. {
  2590. if (ctrl_class == 0)
  2591. return list_empty(&hdl->ctrl_refs) ? -EINVAL : 0;
  2592. return find_ref_lock(hdl, ctrl_class | 1) ? 0 : -EINVAL;
  2593. }
  2594. /* Get extended controls. Allocates the helpers array if needed. */
  2595. int v4l2_g_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *cs)
  2596. {
  2597. struct v4l2_ctrl_helper helper[4];
  2598. struct v4l2_ctrl_helper *helpers = helper;
  2599. int ret;
  2600. int i, j;
  2601. cs->error_idx = cs->count;
  2602. cs->ctrl_class = V4L2_CTRL_ID2CLASS(cs->ctrl_class);
  2603. if (hdl == NULL)
  2604. return -EINVAL;
  2605. if (cs->count == 0)
  2606. return class_check(hdl, cs->ctrl_class);
  2607. if (cs->count > ARRAY_SIZE(helper)) {
  2608. helpers = kmalloc_array(cs->count, sizeof(helper[0]),
  2609. GFP_KERNEL);
  2610. if (helpers == NULL)
  2611. return -ENOMEM;
  2612. }
  2613. ret = prepare_ext_ctrls(hdl, cs, helpers, true);
  2614. cs->error_idx = cs->count;
  2615. for (i = 0; !ret && i < cs->count; i++)
  2616. if (helpers[i].ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY)
  2617. ret = -EACCES;
  2618. for (i = 0; !ret && i < cs->count; i++) {
  2619. int (*ctrl_to_user)(struct v4l2_ext_control *c,
  2620. struct v4l2_ctrl *ctrl) = cur_to_user;
  2621. struct v4l2_ctrl *master;
  2622. if (helpers[i].mref == NULL)
  2623. continue;
  2624. master = helpers[i].mref->ctrl;
  2625. cs->error_idx = i;
  2626. v4l2_ctrl_lock(master);
  2627. /* g_volatile_ctrl will update the new control values */
  2628. if ((master->flags & V4L2_CTRL_FLAG_VOLATILE) ||
  2629. (master->has_volatiles && !is_cur_manual(master))) {
  2630. for (j = 0; j < master->ncontrols; j++)
  2631. cur_to_new(master->cluster[j]);
  2632. ret = call_op(master, g_volatile_ctrl);
  2633. ctrl_to_user = new_to_user;
  2634. }
  2635. /* If OK, then copy the current (for non-volatile controls)
  2636. or the new (for volatile controls) control values to the
  2637. caller */
  2638. if (!ret) {
  2639. u32 idx = i;
  2640. do {
  2641. ret = ctrl_to_user(cs->controls + idx,
  2642. helpers[idx].ctrl);
  2643. idx = helpers[idx].next;
  2644. } while (!ret && idx);
  2645. }
  2646. v4l2_ctrl_unlock(master);
  2647. }
  2648. if (cs->count > ARRAY_SIZE(helper))
  2649. kfree(helpers);
  2650. return ret;
  2651. }
  2652. EXPORT_SYMBOL(v4l2_g_ext_ctrls);
  2653. int v4l2_subdev_g_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
  2654. {
  2655. return v4l2_g_ext_ctrls(sd->ctrl_handler, cs);
  2656. }
  2657. EXPORT_SYMBOL(v4l2_subdev_g_ext_ctrls);
  2658. /* Helper function to get a single control */
  2659. static int get_ctrl(struct v4l2_ctrl *ctrl, struct v4l2_ext_control *c)
  2660. {
  2661. struct v4l2_ctrl *master = ctrl->cluster[0];
  2662. int ret = 0;
  2663. int i;
  2664. /* Compound controls are not supported. The new_to_user() and
  2665. * cur_to_user() calls below would need to be modified not to access
  2666. * userspace memory when called from get_ctrl().
  2667. */
  2668. if (!ctrl->is_int && ctrl->type != V4L2_CTRL_TYPE_INTEGER64)
  2669. return -EINVAL;
  2670. if (ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY)
  2671. return -EACCES;
  2672. v4l2_ctrl_lock(master);
  2673. /* g_volatile_ctrl will update the current control values */
  2674. if (ctrl->flags & V4L2_CTRL_FLAG_VOLATILE) {
  2675. for (i = 0; i < master->ncontrols; i++)
  2676. cur_to_new(master->cluster[i]);
  2677. ret = call_op(master, g_volatile_ctrl);
  2678. new_to_user(c, ctrl);
  2679. } else {
  2680. cur_to_user(c, ctrl);
  2681. }
  2682. v4l2_ctrl_unlock(master);
  2683. return ret;
  2684. }
  2685. int v4l2_g_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_control *control)
  2686. {
  2687. struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
  2688. struct v4l2_ext_control c;
  2689. int ret;
  2690. if (ctrl == NULL || !ctrl->is_int)
  2691. return -EINVAL;
  2692. ret = get_ctrl(ctrl, &c);
  2693. control->value = c.value;
  2694. return ret;
  2695. }
  2696. EXPORT_SYMBOL(v4l2_g_ctrl);
  2697. int v4l2_subdev_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *control)
  2698. {
  2699. return v4l2_g_ctrl(sd->ctrl_handler, control);
  2700. }
  2701. EXPORT_SYMBOL(v4l2_subdev_g_ctrl);
  2702. s32 v4l2_ctrl_g_ctrl(struct v4l2_ctrl *ctrl)
  2703. {
  2704. struct v4l2_ext_control c;
  2705. /* It's a driver bug if this happens. */
  2706. WARN_ON(!ctrl->is_int);
  2707. c.value = 0;
  2708. get_ctrl(ctrl, &c);
  2709. return c.value;
  2710. }
  2711. EXPORT_SYMBOL(v4l2_ctrl_g_ctrl);
  2712. s64 v4l2_ctrl_g_ctrl_int64(struct v4l2_ctrl *ctrl)
  2713. {
  2714. struct v4l2_ext_control c;
  2715. /* It's a driver bug if this happens. */
  2716. WARN_ON(ctrl->is_ptr || ctrl->type != V4L2_CTRL_TYPE_INTEGER64);
  2717. c.value64 = 0;
  2718. get_ctrl(ctrl, &c);
  2719. return c.value64;
  2720. }
  2721. EXPORT_SYMBOL(v4l2_ctrl_g_ctrl_int64);
  2722. /* Core function that calls try/s_ctrl and ensures that the new value is
  2723. copied to the current value on a set.
  2724. Must be called with ctrl->handler->lock held. */
  2725. static int try_or_set_cluster(struct v4l2_fh *fh, struct v4l2_ctrl *master,
  2726. bool set, u32 ch_flags)
  2727. {
  2728. bool update_flag;
  2729. int ret;
  2730. int i;
  2731. /* Go through the cluster and either validate the new value or
  2732. (if no new value was set), copy the current value to the new
  2733. value, ensuring a consistent view for the control ops when
  2734. called. */
  2735. for (i = 0; i < master->ncontrols; i++) {
  2736. struct v4l2_ctrl *ctrl = master->cluster[i];
  2737. if (ctrl == NULL)
  2738. continue;
  2739. if (!ctrl->is_new) {
  2740. cur_to_new(ctrl);
  2741. continue;
  2742. }
  2743. /* Check again: it may have changed since the
  2744. previous check in try_or_set_ext_ctrls(). */
  2745. if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED))
  2746. return -EBUSY;
  2747. }
  2748. ret = call_op(master, try_ctrl);
  2749. /* Don't set if there is no change */
  2750. if (ret || !set || !cluster_changed(master))
  2751. return ret;
  2752. ret = call_op(master, s_ctrl);
  2753. if (ret)
  2754. return ret;
  2755. /* If OK, then make the new values permanent. */
  2756. update_flag = is_cur_manual(master) != is_new_manual(master);
  2757. for (i = 0; i < master->ncontrols; i++)
  2758. new_to_cur(fh, master->cluster[i], ch_flags |
  2759. ((update_flag && i > 0) ? V4L2_EVENT_CTRL_CH_FLAGS : 0));
  2760. return 0;
  2761. }
  2762. /* Validate controls. */
  2763. static int validate_ctrls(struct v4l2_ext_controls *cs,
  2764. struct v4l2_ctrl_helper *helpers, bool set)
  2765. {
  2766. unsigned i;
  2767. int ret = 0;
  2768. cs->error_idx = cs->count;
  2769. for (i = 0; i < cs->count; i++) {
  2770. struct v4l2_ctrl *ctrl = helpers[i].ctrl;
  2771. union v4l2_ctrl_ptr p_new;
  2772. cs->error_idx = i;
  2773. if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)
  2774. return -EACCES;
  2775. /* This test is also done in try_set_control_cluster() which
  2776. is called in atomic context, so that has the final say,
  2777. but it makes sense to do an up-front check as well. Once
  2778. an error occurs in try_set_control_cluster() some other
  2779. controls may have been set already and we want to do a
  2780. best-effort to avoid that. */
  2781. if (set && (ctrl->flags & V4L2_CTRL_FLAG_GRABBED))
  2782. return -EBUSY;
  2783. /*
  2784. * Skip validation for now if the payload needs to be copied
  2785. * from userspace into kernelspace. We'll validate those later.
  2786. */
  2787. if (ctrl->is_ptr)
  2788. continue;
  2789. if (ctrl->type == V4L2_CTRL_TYPE_INTEGER64)
  2790. p_new.p_s64 = &cs->controls[i].value64;
  2791. else
  2792. p_new.p_s32 = &cs->controls[i].value;
  2793. ret = validate_new(ctrl, p_new);
  2794. if (ret)
  2795. return ret;
  2796. }
  2797. return 0;
  2798. }
  2799. /* Obtain the current volatile values of an autocluster and mark them
  2800. as new. */
  2801. static void update_from_auto_cluster(struct v4l2_ctrl *master)
  2802. {
  2803. int i;
  2804. for (i = 1; i < master->ncontrols; i++)
  2805. cur_to_new(master->cluster[i]);
  2806. if (!call_op(master, g_volatile_ctrl))
  2807. for (i = 1; i < master->ncontrols; i++)
  2808. if (master->cluster[i])
  2809. master->cluster[i]->is_new = 1;
  2810. }
  2811. /* Try or try-and-set controls */
  2812. static int try_set_ext_ctrls(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
  2813. struct v4l2_ext_controls *cs,
  2814. bool set)
  2815. {
  2816. struct v4l2_ctrl_helper helper[4];
  2817. struct v4l2_ctrl_helper *helpers = helper;
  2818. unsigned i, j;
  2819. int ret;
  2820. cs->error_idx = cs->count;
  2821. cs->ctrl_class = V4L2_CTRL_ID2CLASS(cs->ctrl_class);
  2822. if (hdl == NULL)
  2823. return -EINVAL;
  2824. if (cs->count == 0)
  2825. return class_check(hdl, cs->ctrl_class);
  2826. if (cs->count > ARRAY_SIZE(helper)) {
  2827. helpers = kmalloc_array(cs->count, sizeof(helper[0]),
  2828. GFP_KERNEL);
  2829. if (!helpers)
  2830. return -ENOMEM;
  2831. }
  2832. ret = prepare_ext_ctrls(hdl, cs, helpers, false);
  2833. if (!ret)
  2834. ret = validate_ctrls(cs, helpers, set);
  2835. if (ret && set)
  2836. cs->error_idx = cs->count;
  2837. for (i = 0; !ret && i < cs->count; i++) {
  2838. struct v4l2_ctrl *master;
  2839. u32 idx = i;
  2840. if (helpers[i].mref == NULL)
  2841. continue;
  2842. cs->error_idx = i;
  2843. master = helpers[i].mref->ctrl;
  2844. v4l2_ctrl_lock(master);
  2845. /* Reset the 'is_new' flags of the cluster */
  2846. for (j = 0; j < master->ncontrols; j++)
  2847. if (master->cluster[j])
  2848. master->cluster[j]->is_new = 0;
  2849. /* For volatile autoclusters that are currently in auto mode
  2850. we need to discover if it will be set to manual mode.
  2851. If so, then we have to copy the current volatile values
  2852. first since those will become the new manual values (which
  2853. may be overwritten by explicit new values from this set
  2854. of controls). */
  2855. if (master->is_auto && master->has_volatiles &&
  2856. !is_cur_manual(master)) {
  2857. /* Pick an initial non-manual value */
  2858. s32 new_auto_val = master->manual_mode_value + 1;
  2859. u32 tmp_idx = idx;
  2860. do {
  2861. /* Check if the auto control is part of the
  2862. list, and remember the new value. */
  2863. if (helpers[tmp_idx].ctrl == master)
  2864. new_auto_val = cs->controls[tmp_idx].value;
  2865. tmp_idx = helpers[tmp_idx].next;
  2866. } while (tmp_idx);
  2867. /* If the new value == the manual value, then copy
  2868. the current volatile values. */
  2869. if (new_auto_val == master->manual_mode_value)
  2870. update_from_auto_cluster(master);
  2871. }
  2872. /* Copy the new caller-supplied control values.
  2873. user_to_new() sets 'is_new' to 1. */
  2874. do {
  2875. struct v4l2_ctrl *ctrl = helpers[idx].ctrl;
  2876. ret = user_to_new(cs->controls + idx, ctrl);
  2877. if (!ret && ctrl->is_ptr)
  2878. ret = validate_new(ctrl, ctrl->p_new);
  2879. idx = helpers[idx].next;
  2880. } while (!ret && idx);
  2881. if (!ret)
  2882. ret = try_or_set_cluster(fh, master, set, 0);
  2883. /* Copy the new values back to userspace. */
  2884. if (!ret) {
  2885. idx = i;
  2886. do {
  2887. ret = new_to_user(cs->controls + idx,
  2888. helpers[idx].ctrl);
  2889. idx = helpers[idx].next;
  2890. } while (!ret && idx);
  2891. }
  2892. v4l2_ctrl_unlock(master);
  2893. }
  2894. if (cs->count > ARRAY_SIZE(helper))
  2895. kfree(helpers);
  2896. return ret;
  2897. }
  2898. int v4l2_try_ext_ctrls(struct v4l2_ctrl_handler *hdl, struct v4l2_ext_controls *cs)
  2899. {
  2900. return try_set_ext_ctrls(NULL, hdl, cs, false);
  2901. }
  2902. EXPORT_SYMBOL(v4l2_try_ext_ctrls);
  2903. int v4l2_s_ext_ctrls(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
  2904. struct v4l2_ext_controls *cs)
  2905. {
  2906. return try_set_ext_ctrls(fh, hdl, cs, true);
  2907. }
  2908. EXPORT_SYMBOL(v4l2_s_ext_ctrls);
  2909. int v4l2_subdev_try_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
  2910. {
  2911. return try_set_ext_ctrls(NULL, sd->ctrl_handler, cs, false);
  2912. }
  2913. EXPORT_SYMBOL(v4l2_subdev_try_ext_ctrls);
  2914. int v4l2_subdev_s_ext_ctrls(struct v4l2_subdev *sd, struct v4l2_ext_controls *cs)
  2915. {
  2916. return try_set_ext_ctrls(NULL, sd->ctrl_handler, cs, true);
  2917. }
  2918. EXPORT_SYMBOL(v4l2_subdev_s_ext_ctrls);
  2919. /* Helper function for VIDIOC_S_CTRL compatibility */
  2920. static int set_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl, u32 ch_flags)
  2921. {
  2922. struct v4l2_ctrl *master = ctrl->cluster[0];
  2923. int ret;
  2924. int i;
  2925. /* Reset the 'is_new' flags of the cluster */
  2926. for (i = 0; i < master->ncontrols; i++)
  2927. if (master->cluster[i])
  2928. master->cluster[i]->is_new = 0;
  2929. ret = validate_new(ctrl, ctrl->p_new);
  2930. if (ret)
  2931. return ret;
  2932. /* For autoclusters with volatiles that are switched from auto to
  2933. manual mode we have to update the current volatile values since
  2934. those will become the initial manual values after such a switch. */
  2935. if (master->is_auto && master->has_volatiles && ctrl == master &&
  2936. !is_cur_manual(master) && ctrl->val == master->manual_mode_value)
  2937. update_from_auto_cluster(master);
  2938. ctrl->is_new = 1;
  2939. return try_or_set_cluster(fh, master, true, ch_flags);
  2940. }
  2941. /* Helper function for VIDIOC_S_CTRL compatibility */
  2942. static int set_ctrl_lock(struct v4l2_fh *fh, struct v4l2_ctrl *ctrl,
  2943. struct v4l2_ext_control *c)
  2944. {
  2945. int ret;
  2946. v4l2_ctrl_lock(ctrl);
  2947. user_to_new(c, ctrl);
  2948. ret = set_ctrl(fh, ctrl, 0);
  2949. if (!ret)
  2950. cur_to_user(c, ctrl);
  2951. v4l2_ctrl_unlock(ctrl);
  2952. return ret;
  2953. }
  2954. int v4l2_s_ctrl(struct v4l2_fh *fh, struct v4l2_ctrl_handler *hdl,
  2955. struct v4l2_control *control)
  2956. {
  2957. struct v4l2_ctrl *ctrl = v4l2_ctrl_find(hdl, control->id);
  2958. struct v4l2_ext_control c = { control->id };
  2959. int ret;
  2960. if (ctrl == NULL || !ctrl->is_int)
  2961. return -EINVAL;
  2962. if (ctrl->flags & V4L2_CTRL_FLAG_READ_ONLY)
  2963. return -EACCES;
  2964. c.value = control->value;
  2965. ret = set_ctrl_lock(fh, ctrl, &c);
  2966. control->value = c.value;
  2967. return ret;
  2968. }
  2969. EXPORT_SYMBOL(v4l2_s_ctrl);
  2970. int v4l2_subdev_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *control)
  2971. {
  2972. return v4l2_s_ctrl(NULL, sd->ctrl_handler, control);
  2973. }
  2974. EXPORT_SYMBOL(v4l2_subdev_s_ctrl);
  2975. int __v4l2_ctrl_s_ctrl(struct v4l2_ctrl *ctrl, s32 val)
  2976. {
  2977. lockdep_assert_held(ctrl->handler->lock);
  2978. /* It's a driver bug if this happens. */
  2979. WARN_ON(!ctrl->is_int);
  2980. ctrl->val = val;
  2981. return set_ctrl(NULL, ctrl, 0);
  2982. }
  2983. EXPORT_SYMBOL(__v4l2_ctrl_s_ctrl);
  2984. int __v4l2_ctrl_s_ctrl_int64(struct v4l2_ctrl *ctrl, s64 val)
  2985. {
  2986. lockdep_assert_held(ctrl->handler->lock);
  2987. /* It's a driver bug if this happens. */
  2988. WARN_ON(ctrl->is_ptr || ctrl->type != V4L2_CTRL_TYPE_INTEGER64);
  2989. *ctrl->p_new.p_s64 = val;
  2990. return set_ctrl(NULL, ctrl, 0);
  2991. }
  2992. EXPORT_SYMBOL(__v4l2_ctrl_s_ctrl_int64);
  2993. int __v4l2_ctrl_s_ctrl_string(struct v4l2_ctrl *ctrl, const char *s)
  2994. {
  2995. lockdep_assert_held(ctrl->handler->lock);
  2996. /* It's a driver bug if this happens. */
  2997. WARN_ON(ctrl->type != V4L2_CTRL_TYPE_STRING);
  2998. strlcpy(ctrl->p_new.p_char, s, ctrl->maximum + 1);
  2999. return set_ctrl(NULL, ctrl, 0);
  3000. }
  3001. EXPORT_SYMBOL(__v4l2_ctrl_s_ctrl_string);
  3002. void v4l2_ctrl_notify(struct v4l2_ctrl *ctrl, v4l2_ctrl_notify_fnc notify, void *priv)
  3003. {
  3004. if (ctrl == NULL)
  3005. return;
  3006. if (notify == NULL) {
  3007. ctrl->call_notify = 0;
  3008. return;
  3009. }
  3010. if (WARN_ON(ctrl->handler->notify && ctrl->handler->notify != notify))
  3011. return;
  3012. ctrl->handler->notify = notify;
  3013. ctrl->handler->notify_priv = priv;
  3014. ctrl->call_notify = 1;
  3015. }
  3016. EXPORT_SYMBOL(v4l2_ctrl_notify);
  3017. int __v4l2_ctrl_modify_range(struct v4l2_ctrl *ctrl,
  3018. s64 min, s64 max, u64 step, s64 def)
  3019. {
  3020. bool changed;
  3021. int ret;
  3022. lockdep_assert_held(ctrl->handler->lock);
  3023. switch (ctrl->type) {
  3024. case V4L2_CTRL_TYPE_INTEGER:
  3025. case V4L2_CTRL_TYPE_INTEGER64:
  3026. case V4L2_CTRL_TYPE_BOOLEAN:
  3027. case V4L2_CTRL_TYPE_MENU:
  3028. case V4L2_CTRL_TYPE_INTEGER_MENU:
  3029. case V4L2_CTRL_TYPE_BITMASK:
  3030. case V4L2_CTRL_TYPE_U8:
  3031. case V4L2_CTRL_TYPE_U16:
  3032. case V4L2_CTRL_TYPE_U32:
  3033. if (ctrl->is_array)
  3034. return -EINVAL;
  3035. ret = check_range(ctrl->type, min, max, step, def);
  3036. if (ret)
  3037. return ret;
  3038. break;
  3039. default:
  3040. return -EINVAL;
  3041. }
  3042. ctrl->minimum = min;
  3043. ctrl->maximum = max;
  3044. ctrl->step = step;
  3045. ctrl->default_value = def;
  3046. cur_to_new(ctrl);
  3047. if (validate_new(ctrl, ctrl->p_new)) {
  3048. if (ctrl->type == V4L2_CTRL_TYPE_INTEGER64)
  3049. *ctrl->p_new.p_s64 = def;
  3050. else
  3051. *ctrl->p_new.p_s32 = def;
  3052. }
  3053. if (ctrl->type == V4L2_CTRL_TYPE_INTEGER64)
  3054. changed = *ctrl->p_new.p_s64 != *ctrl->p_cur.p_s64;
  3055. else
  3056. changed = *ctrl->p_new.p_s32 != *ctrl->p_cur.p_s32;
  3057. if (changed)
  3058. ret = set_ctrl(NULL, ctrl, V4L2_EVENT_CTRL_CH_RANGE);
  3059. else
  3060. send_event(NULL, ctrl, V4L2_EVENT_CTRL_CH_RANGE);
  3061. return ret;
  3062. }
  3063. EXPORT_SYMBOL(__v4l2_ctrl_modify_range);
  3064. static int v4l2_ctrl_add_event(struct v4l2_subscribed_event *sev, unsigned elems)
  3065. {
  3066. struct v4l2_ctrl *ctrl = v4l2_ctrl_find(sev->fh->ctrl_handler, sev->id);
  3067. if (ctrl == NULL)
  3068. return -EINVAL;
  3069. v4l2_ctrl_lock(ctrl);
  3070. list_add_tail(&sev->node, &ctrl->ev_subs);
  3071. if (ctrl->type != V4L2_CTRL_TYPE_CTRL_CLASS &&
  3072. (sev->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL)) {
  3073. struct v4l2_event ev;
  3074. u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
  3075. if (!(ctrl->flags & V4L2_CTRL_FLAG_WRITE_ONLY))
  3076. changes |= V4L2_EVENT_CTRL_CH_VALUE;
  3077. fill_event(&ev, ctrl, changes);
  3078. /* Mark the queue as active, allowing this initial
  3079. event to be accepted. */
  3080. sev->elems = elems;
  3081. v4l2_event_queue_fh(sev->fh, &ev);
  3082. }
  3083. v4l2_ctrl_unlock(ctrl);
  3084. return 0;
  3085. }
  3086. static void v4l2_ctrl_del_event(struct v4l2_subscribed_event *sev)
  3087. {
  3088. struct v4l2_ctrl *ctrl = v4l2_ctrl_find(sev->fh->ctrl_handler, sev->id);
  3089. v4l2_ctrl_lock(ctrl);
  3090. list_del(&sev->node);
  3091. v4l2_ctrl_unlock(ctrl);
  3092. }
  3093. void v4l2_ctrl_replace(struct v4l2_event *old, const struct v4l2_event *new)
  3094. {
  3095. u32 old_changes = old->u.ctrl.changes;
  3096. old->u.ctrl = new->u.ctrl;
  3097. old->u.ctrl.changes |= old_changes;
  3098. }
  3099. EXPORT_SYMBOL(v4l2_ctrl_replace);
  3100. void v4l2_ctrl_merge(const struct v4l2_event *old, struct v4l2_event *new)
  3101. {
  3102. new->u.ctrl.changes |= old->u.ctrl.changes;
  3103. }
  3104. EXPORT_SYMBOL(v4l2_ctrl_merge);
  3105. const struct v4l2_subscribed_event_ops v4l2_ctrl_sub_ev_ops = {
  3106. .add = v4l2_ctrl_add_event,
  3107. .del = v4l2_ctrl_del_event,
  3108. .replace = v4l2_ctrl_replace,
  3109. .merge = v4l2_ctrl_merge,
  3110. };
  3111. EXPORT_SYMBOL(v4l2_ctrl_sub_ev_ops);
  3112. int v4l2_ctrl_log_status(struct file *file, void *fh)
  3113. {
  3114. struct video_device *vfd = video_devdata(file);
  3115. struct v4l2_fh *vfh = file->private_data;
  3116. if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) && vfd->v4l2_dev)
  3117. v4l2_ctrl_handler_log_status(vfh->ctrl_handler,
  3118. vfd->v4l2_dev->name);
  3119. return 0;
  3120. }
  3121. EXPORT_SYMBOL(v4l2_ctrl_log_status);
  3122. int v4l2_ctrl_subscribe_event(struct v4l2_fh *fh,
  3123. const struct v4l2_event_subscription *sub)
  3124. {
  3125. if (sub->type == V4L2_EVENT_CTRL)
  3126. return v4l2_event_subscribe(fh, sub, 0, &v4l2_ctrl_sub_ev_ops);
  3127. return -EINVAL;
  3128. }
  3129. EXPORT_SYMBOL(v4l2_ctrl_subscribe_event);
  3130. int v4l2_ctrl_subdev_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
  3131. struct v4l2_event_subscription *sub)
  3132. {
  3133. if (!sd->ctrl_handler)
  3134. return -EINVAL;
  3135. return v4l2_ctrl_subscribe_event(fh, sub);
  3136. }
  3137. EXPORT_SYMBOL(v4l2_ctrl_subdev_subscribe_event);
  3138. unsigned int v4l2_ctrl_poll(struct file *file, struct poll_table_struct *wait)
  3139. {
  3140. struct v4l2_fh *fh = file->private_data;
  3141. if (v4l2_event_pending(fh))
  3142. return POLLPRI;
  3143. poll_wait(file, &fh->wait, wait);
  3144. return 0;
  3145. }
  3146. EXPORT_SYMBOL(v4l2_ctrl_poll);