12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372 |
- /*
- * uvc_ctrl.c -- USB Video Class driver - Controls
- *
- * Copyright (C) 2005-2010
- * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- */
- #include <linux/kernel.h>
- #include <linux/list.h>
- #include <linux/module.h>
- #include <linux/slab.h>
- #include <linux/uaccess.h>
- #include <linux/usb.h>
- #include <linux/videodev2.h>
- #include <linux/vmalloc.h>
- #include <linux/wait.h>
- #include <linux/workqueue.h>
- #include <linux/atomic.h>
- #include <media/v4l2-ctrls.h>
- #include "uvcvideo.h"
- #define UVC_CTRL_DATA_CURRENT 0
- #define UVC_CTRL_DATA_BACKUP 1
- #define UVC_CTRL_DATA_MIN 2
- #define UVC_CTRL_DATA_MAX 3
- #define UVC_CTRL_DATA_RES 4
- #define UVC_CTRL_DATA_DEF 5
- #define UVC_CTRL_DATA_LAST 6
- /* ------------------------------------------------------------------------
- * Controls
- */
- static struct uvc_control_info uvc_ctrls[] = {
- {
- .entity = UVC_GUID_UVC_PROCESSING,
- .selector = UVC_PU_BRIGHTNESS_CONTROL,
- .index = 0,
- .size = 2,
- .flags = UVC_CTRL_FLAG_SET_CUR
- | UVC_CTRL_FLAG_GET_RANGE
- | UVC_CTRL_FLAG_RESTORE,
- },
- {
- .entity = UVC_GUID_UVC_PROCESSING,
- .selector = UVC_PU_CONTRAST_CONTROL,
- .index = 1,
- .size = 2,
- .flags = UVC_CTRL_FLAG_SET_CUR
- | UVC_CTRL_FLAG_GET_RANGE
- | UVC_CTRL_FLAG_RESTORE,
- },
- {
- .entity = UVC_GUID_UVC_PROCESSING,
- .selector = UVC_PU_HUE_CONTROL,
- .index = 2,
- .size = 2,
- .flags = UVC_CTRL_FLAG_SET_CUR
- | UVC_CTRL_FLAG_GET_RANGE
- | UVC_CTRL_FLAG_RESTORE
- | UVC_CTRL_FLAG_AUTO_UPDATE,
- },
- {
- .entity = UVC_GUID_UVC_PROCESSING,
- .selector = UVC_PU_SATURATION_CONTROL,
- .index = 3,
- .size = 2,
- .flags = UVC_CTRL_FLAG_SET_CUR
- | UVC_CTRL_FLAG_GET_RANGE
- | UVC_CTRL_FLAG_RESTORE,
- },
- {
- .entity = UVC_GUID_UVC_PROCESSING,
- .selector = UVC_PU_SHARPNESS_CONTROL,
- .index = 4,
- .size = 2,
- .flags = UVC_CTRL_FLAG_SET_CUR
- | UVC_CTRL_FLAG_GET_RANGE
- | UVC_CTRL_FLAG_RESTORE,
- },
- {
- .entity = UVC_GUID_UVC_PROCESSING,
- .selector = UVC_PU_GAMMA_CONTROL,
- .index = 5,
- .size = 2,
- .flags = UVC_CTRL_FLAG_SET_CUR
- | UVC_CTRL_FLAG_GET_RANGE
- | UVC_CTRL_FLAG_RESTORE,
- },
- {
- .entity = UVC_GUID_UVC_PROCESSING,
- .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
- .index = 6,
- .size = 2,
- .flags = UVC_CTRL_FLAG_SET_CUR
- | UVC_CTRL_FLAG_GET_RANGE
- | UVC_CTRL_FLAG_RESTORE
- | UVC_CTRL_FLAG_AUTO_UPDATE,
- },
- {
- .entity = UVC_GUID_UVC_PROCESSING,
- .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
- .index = 7,
- .size = 4,
- .flags = UVC_CTRL_FLAG_SET_CUR
- | UVC_CTRL_FLAG_GET_RANGE
- | UVC_CTRL_FLAG_RESTORE
- | UVC_CTRL_FLAG_AUTO_UPDATE,
- },
- {
- .entity = UVC_GUID_UVC_PROCESSING,
- .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
- .index = 8,
- .size = 2,
- .flags = UVC_CTRL_FLAG_SET_CUR
- | UVC_CTRL_FLAG_GET_RANGE
- | UVC_CTRL_FLAG_RESTORE,
- },
- {
- .entity = UVC_GUID_UVC_PROCESSING,
- .selector = UVC_PU_GAIN_CONTROL,
- .index = 9,
- .size = 2,
- .flags = UVC_CTRL_FLAG_SET_CUR
- | UVC_CTRL_FLAG_GET_RANGE
- | UVC_CTRL_FLAG_RESTORE,
- },
- {
- .entity = UVC_GUID_UVC_PROCESSING,
- .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
- .index = 10,
- .size = 1,
- .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
- | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
- },
- {
- .entity = UVC_GUID_UVC_PROCESSING,
- .selector = UVC_PU_HUE_AUTO_CONTROL,
- .index = 11,
- .size = 1,
- .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
- | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
- },
- {
- .entity = UVC_GUID_UVC_PROCESSING,
- .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
- .index = 12,
- .size = 1,
- .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
- | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
- },
- {
- .entity = UVC_GUID_UVC_PROCESSING,
- .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
- .index = 13,
- .size = 1,
- .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
- | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
- },
- {
- .entity = UVC_GUID_UVC_PROCESSING,
- .selector = UVC_PU_DIGITAL_MULTIPLIER_CONTROL,
- .index = 14,
- .size = 2,
- .flags = UVC_CTRL_FLAG_SET_CUR
- | UVC_CTRL_FLAG_GET_RANGE
- | UVC_CTRL_FLAG_RESTORE,
- },
- {
- .entity = UVC_GUID_UVC_PROCESSING,
- .selector = UVC_PU_DIGITAL_MULTIPLIER_LIMIT_CONTROL,
- .index = 15,
- .size = 2,
- .flags = UVC_CTRL_FLAG_SET_CUR
- | UVC_CTRL_FLAG_GET_RANGE
- | UVC_CTRL_FLAG_RESTORE,
- },
- {
- .entity = UVC_GUID_UVC_PROCESSING,
- .selector = UVC_PU_ANALOG_VIDEO_STANDARD_CONTROL,
- .index = 16,
- .size = 1,
- .flags = UVC_CTRL_FLAG_GET_CUR,
- },
- {
- .entity = UVC_GUID_UVC_PROCESSING,
- .selector = UVC_PU_ANALOG_LOCK_STATUS_CONTROL,
- .index = 17,
- .size = 1,
- .flags = UVC_CTRL_FLAG_GET_CUR,
- },
- {
- .entity = UVC_GUID_UVC_CAMERA,
- .selector = UVC_CT_SCANNING_MODE_CONTROL,
- .index = 0,
- .size = 1,
- .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
- | UVC_CTRL_FLAG_RESTORE,
- },
- {
- .entity = UVC_GUID_UVC_CAMERA,
- .selector = UVC_CT_AE_MODE_CONTROL,
- .index = 1,
- .size = 1,
- .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
- | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_GET_RES
- | UVC_CTRL_FLAG_RESTORE,
- },
- {
- .entity = UVC_GUID_UVC_CAMERA,
- .selector = UVC_CT_AE_PRIORITY_CONTROL,
- .index = 2,
- .size = 1,
- .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
- | UVC_CTRL_FLAG_RESTORE,
- },
- {
- .entity = UVC_GUID_UVC_CAMERA,
- .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
- .index = 3,
- .size = 4,
- .flags = UVC_CTRL_FLAG_SET_CUR
- | UVC_CTRL_FLAG_GET_RANGE
- | UVC_CTRL_FLAG_RESTORE
- | UVC_CTRL_FLAG_AUTO_UPDATE,
- },
- {
- .entity = UVC_GUID_UVC_CAMERA,
- .selector = UVC_CT_EXPOSURE_TIME_RELATIVE_CONTROL,
- .index = 4,
- .size = 1,
- .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_RESTORE,
- },
- {
- .entity = UVC_GUID_UVC_CAMERA,
- .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
- .index = 5,
- .size = 2,
- .flags = UVC_CTRL_FLAG_SET_CUR
- | UVC_CTRL_FLAG_GET_RANGE
- | UVC_CTRL_FLAG_RESTORE
- | UVC_CTRL_FLAG_AUTO_UPDATE,
- },
- {
- .entity = UVC_GUID_UVC_CAMERA,
- .selector = UVC_CT_FOCUS_RELATIVE_CONTROL,
- .index = 6,
- .size = 2,
- .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
- | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
- | UVC_CTRL_FLAG_GET_DEF
- | UVC_CTRL_FLAG_AUTO_UPDATE,
- },
- {
- .entity = UVC_GUID_UVC_CAMERA,
- .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL,
- .index = 7,
- .size = 2,
- .flags = UVC_CTRL_FLAG_SET_CUR
- | UVC_CTRL_FLAG_GET_RANGE
- | UVC_CTRL_FLAG_RESTORE
- | UVC_CTRL_FLAG_AUTO_UPDATE,
- },
- {
- .entity = UVC_GUID_UVC_CAMERA,
- .selector = UVC_CT_IRIS_RELATIVE_CONTROL,
- .index = 8,
- .size = 1,
- .flags = UVC_CTRL_FLAG_SET_CUR
- | UVC_CTRL_FLAG_AUTO_UPDATE,
- },
- {
- .entity = UVC_GUID_UVC_CAMERA,
- .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
- .index = 9,
- .size = 2,
- .flags = UVC_CTRL_FLAG_SET_CUR
- | UVC_CTRL_FLAG_GET_RANGE
- | UVC_CTRL_FLAG_RESTORE
- | UVC_CTRL_FLAG_AUTO_UPDATE,
- },
- {
- .entity = UVC_GUID_UVC_CAMERA,
- .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
- .index = 10,
- .size = 3,
- .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
- | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
- | UVC_CTRL_FLAG_GET_DEF
- | UVC_CTRL_FLAG_AUTO_UPDATE,
- },
- {
- .entity = UVC_GUID_UVC_CAMERA,
- .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
- .index = 11,
- .size = 8,
- .flags = UVC_CTRL_FLAG_SET_CUR
- | UVC_CTRL_FLAG_GET_RANGE
- | UVC_CTRL_FLAG_RESTORE
- | UVC_CTRL_FLAG_AUTO_UPDATE,
- },
- {
- .entity = UVC_GUID_UVC_CAMERA,
- .selector = UVC_CT_PANTILT_RELATIVE_CONTROL,
- .index = 12,
- .size = 4,
- .flags = UVC_CTRL_FLAG_SET_CUR
- | UVC_CTRL_FLAG_GET_RANGE
- | UVC_CTRL_FLAG_AUTO_UPDATE,
- },
- {
- .entity = UVC_GUID_UVC_CAMERA,
- .selector = UVC_CT_ROLL_ABSOLUTE_CONTROL,
- .index = 13,
- .size = 2,
- .flags = UVC_CTRL_FLAG_SET_CUR
- | UVC_CTRL_FLAG_GET_RANGE
- | UVC_CTRL_FLAG_RESTORE
- | UVC_CTRL_FLAG_AUTO_UPDATE,
- },
- {
- .entity = UVC_GUID_UVC_CAMERA,
- .selector = UVC_CT_ROLL_RELATIVE_CONTROL,
- .index = 14,
- .size = 2,
- .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_MIN
- | UVC_CTRL_FLAG_GET_MAX | UVC_CTRL_FLAG_GET_RES
- | UVC_CTRL_FLAG_GET_DEF
- | UVC_CTRL_FLAG_AUTO_UPDATE,
- },
- {
- .entity = UVC_GUID_UVC_CAMERA,
- .selector = UVC_CT_FOCUS_AUTO_CONTROL,
- .index = 17,
- .size = 1,
- .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
- | UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_RESTORE,
- },
- {
- .entity = UVC_GUID_UVC_CAMERA,
- .selector = UVC_CT_PRIVACY_CONTROL,
- .index = 18,
- .size = 1,
- .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR
- | UVC_CTRL_FLAG_RESTORE
- | UVC_CTRL_FLAG_AUTO_UPDATE,
- },
- };
- static struct uvc_menu_info power_line_frequency_controls[] = {
- { 0, "Disabled" },
- { 1, "50 Hz" },
- { 2, "60 Hz" },
- };
- static struct uvc_menu_info exposure_auto_controls[] = {
- { 2, "Auto Mode" },
- { 1, "Manual Mode" },
- { 4, "Shutter Priority Mode" },
- { 8, "Aperture Priority Mode" },
- };
- static s32 uvc_ctrl_get_zoom(struct uvc_control_mapping *mapping,
- u8 query, const u8 *data)
- {
- s8 zoom = (s8)data[0];
- switch (query) {
- case UVC_GET_CUR:
- return (zoom == 0) ? 0 : (zoom > 0 ? data[2] : -data[2]);
- case UVC_GET_MIN:
- case UVC_GET_MAX:
- case UVC_GET_RES:
- case UVC_GET_DEF:
- default:
- return data[2];
- }
- }
- static void uvc_ctrl_set_zoom(struct uvc_control_mapping *mapping,
- s32 value, u8 *data)
- {
- data[0] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
- data[2] = min((int)abs(value), 0xff);
- }
- static s32 uvc_ctrl_get_rel_speed(struct uvc_control_mapping *mapping,
- u8 query, const u8 *data)
- {
- unsigned int first = mapping->offset / 8;
- s8 rel = (s8)data[first];
- switch (query) {
- case UVC_GET_CUR:
- return (rel == 0) ? 0 : (rel > 0 ? data[first+1]
- : -data[first+1]);
- case UVC_GET_MIN:
- return -data[first+1];
- case UVC_GET_MAX:
- case UVC_GET_RES:
- case UVC_GET_DEF:
- default:
- return data[first+1];
- }
- }
- static void uvc_ctrl_set_rel_speed(struct uvc_control_mapping *mapping,
- s32 value, u8 *data)
- {
- unsigned int first = mapping->offset / 8;
- data[first] = value == 0 ? 0 : (value > 0) ? 1 : 0xff;
- data[first+1] = min_t(int, abs(value), 0xff);
- }
- static struct uvc_control_mapping uvc_ctrl_mappings[] = {
- {
- .id = V4L2_CID_BRIGHTNESS,
- .name = "Brightness",
- .entity = UVC_GUID_UVC_PROCESSING,
- .selector = UVC_PU_BRIGHTNESS_CONTROL,
- .size = 16,
- .offset = 0,
- .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
- .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
- },
- {
- .id = V4L2_CID_CONTRAST,
- .name = "Contrast",
- .entity = UVC_GUID_UVC_PROCESSING,
- .selector = UVC_PU_CONTRAST_CONTROL,
- .size = 16,
- .offset = 0,
- .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
- .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
- },
- {
- .id = V4L2_CID_HUE,
- .name = "Hue",
- .entity = UVC_GUID_UVC_PROCESSING,
- .selector = UVC_PU_HUE_CONTROL,
- .size = 16,
- .offset = 0,
- .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
- .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
- .master_id = V4L2_CID_HUE_AUTO,
- .master_manual = 0,
- },
- {
- .id = V4L2_CID_SATURATION,
- .name = "Saturation",
- .entity = UVC_GUID_UVC_PROCESSING,
- .selector = UVC_PU_SATURATION_CONTROL,
- .size = 16,
- .offset = 0,
- .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
- .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
- },
- {
- .id = V4L2_CID_SHARPNESS,
- .name = "Sharpness",
- .entity = UVC_GUID_UVC_PROCESSING,
- .selector = UVC_PU_SHARPNESS_CONTROL,
- .size = 16,
- .offset = 0,
- .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
- .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
- },
- {
- .id = V4L2_CID_GAMMA,
- .name = "Gamma",
- .entity = UVC_GUID_UVC_PROCESSING,
- .selector = UVC_PU_GAMMA_CONTROL,
- .size = 16,
- .offset = 0,
- .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
- .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
- },
- {
- .id = V4L2_CID_BACKLIGHT_COMPENSATION,
- .name = "Backlight Compensation",
- .entity = UVC_GUID_UVC_PROCESSING,
- .selector = UVC_PU_BACKLIGHT_COMPENSATION_CONTROL,
- .size = 16,
- .offset = 0,
- .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
- .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
- },
- {
- .id = V4L2_CID_GAIN,
- .name = "Gain",
- .entity = UVC_GUID_UVC_PROCESSING,
- .selector = UVC_PU_GAIN_CONTROL,
- .size = 16,
- .offset = 0,
- .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
- .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
- },
- {
- .id = V4L2_CID_POWER_LINE_FREQUENCY,
- .name = "Power Line Frequency",
- .entity = UVC_GUID_UVC_PROCESSING,
- .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL,
- .size = 2,
- .offset = 0,
- .v4l2_type = V4L2_CTRL_TYPE_MENU,
- .data_type = UVC_CTRL_DATA_TYPE_ENUM,
- .menu_info = power_line_frequency_controls,
- .menu_count = ARRAY_SIZE(power_line_frequency_controls),
- },
- {
- .id = V4L2_CID_HUE_AUTO,
- .name = "Hue, Auto",
- .entity = UVC_GUID_UVC_PROCESSING,
- .selector = UVC_PU_HUE_AUTO_CONTROL,
- .size = 1,
- .offset = 0,
- .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
- .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
- .slave_ids = { V4L2_CID_HUE, },
- },
- {
- .id = V4L2_CID_EXPOSURE_AUTO,
- .name = "Exposure, Auto",
- .entity = UVC_GUID_UVC_CAMERA,
- .selector = UVC_CT_AE_MODE_CONTROL,
- .size = 4,
- .offset = 0,
- .v4l2_type = V4L2_CTRL_TYPE_MENU,
- .data_type = UVC_CTRL_DATA_TYPE_BITMASK,
- .menu_info = exposure_auto_controls,
- .menu_count = ARRAY_SIZE(exposure_auto_controls),
- .slave_ids = { V4L2_CID_EXPOSURE_ABSOLUTE, },
- },
- {
- .id = V4L2_CID_EXPOSURE_AUTO_PRIORITY,
- .name = "Exposure, Auto Priority",
- .entity = UVC_GUID_UVC_CAMERA,
- .selector = UVC_CT_AE_PRIORITY_CONTROL,
- .size = 1,
- .offset = 0,
- .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
- .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
- },
- {
- .id = V4L2_CID_EXPOSURE_ABSOLUTE,
- .name = "Exposure (Absolute)",
- .entity = UVC_GUID_UVC_CAMERA,
- .selector = UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL,
- .size = 32,
- .offset = 0,
- .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
- .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
- .master_id = V4L2_CID_EXPOSURE_AUTO,
- .master_manual = V4L2_EXPOSURE_MANUAL,
- },
- {
- .id = V4L2_CID_AUTO_WHITE_BALANCE,
- .name = "White Balance Temperature, Auto",
- .entity = UVC_GUID_UVC_PROCESSING,
- .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL,
- .size = 1,
- .offset = 0,
- .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
- .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
- .slave_ids = { V4L2_CID_WHITE_BALANCE_TEMPERATURE, },
- },
- {
- .id = V4L2_CID_WHITE_BALANCE_TEMPERATURE,
- .name = "White Balance Temperature",
- .entity = UVC_GUID_UVC_PROCESSING,
- .selector = UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL,
- .size = 16,
- .offset = 0,
- .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
- .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
- .master_id = V4L2_CID_AUTO_WHITE_BALANCE,
- .master_manual = 0,
- },
- {
- .id = V4L2_CID_AUTO_WHITE_BALANCE,
- .name = "White Balance Component, Auto",
- .entity = UVC_GUID_UVC_PROCESSING,
- .selector = UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL,
- .size = 1,
- .offset = 0,
- .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
- .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
- .slave_ids = { V4L2_CID_BLUE_BALANCE,
- V4L2_CID_RED_BALANCE },
- },
- {
- .id = V4L2_CID_BLUE_BALANCE,
- .name = "White Balance Blue Component",
- .entity = UVC_GUID_UVC_PROCESSING,
- .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
- .size = 16,
- .offset = 0,
- .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
- .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
- .master_id = V4L2_CID_AUTO_WHITE_BALANCE,
- .master_manual = 0,
- },
- {
- .id = V4L2_CID_RED_BALANCE,
- .name = "White Balance Red Component",
- .entity = UVC_GUID_UVC_PROCESSING,
- .selector = UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL,
- .size = 16,
- .offset = 16,
- .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
- .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
- .master_id = V4L2_CID_AUTO_WHITE_BALANCE,
- .master_manual = 0,
- },
- {
- .id = V4L2_CID_FOCUS_ABSOLUTE,
- .name = "Focus (absolute)",
- .entity = UVC_GUID_UVC_CAMERA,
- .selector = UVC_CT_FOCUS_ABSOLUTE_CONTROL,
- .size = 16,
- .offset = 0,
- .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
- .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
- .master_id = V4L2_CID_FOCUS_AUTO,
- .master_manual = 0,
- },
- {
- .id = V4L2_CID_FOCUS_AUTO,
- .name = "Focus, Auto",
- .entity = UVC_GUID_UVC_CAMERA,
- .selector = UVC_CT_FOCUS_AUTO_CONTROL,
- .size = 1,
- .offset = 0,
- .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
- .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
- .slave_ids = { V4L2_CID_FOCUS_ABSOLUTE, },
- },
- {
- .id = V4L2_CID_IRIS_ABSOLUTE,
- .name = "Iris, Absolute",
- .entity = UVC_GUID_UVC_CAMERA,
- .selector = UVC_CT_IRIS_ABSOLUTE_CONTROL,
- .size = 16,
- .offset = 0,
- .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
- .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
- },
- {
- .id = V4L2_CID_IRIS_RELATIVE,
- .name = "Iris, Relative",
- .entity = UVC_GUID_UVC_CAMERA,
- .selector = UVC_CT_IRIS_RELATIVE_CONTROL,
- .size = 8,
- .offset = 0,
- .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
- .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
- },
- {
- .id = V4L2_CID_ZOOM_ABSOLUTE,
- .name = "Zoom, Absolute",
- .entity = UVC_GUID_UVC_CAMERA,
- .selector = UVC_CT_ZOOM_ABSOLUTE_CONTROL,
- .size = 16,
- .offset = 0,
- .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
- .data_type = UVC_CTRL_DATA_TYPE_UNSIGNED,
- },
- {
- .id = V4L2_CID_ZOOM_CONTINUOUS,
- .name = "Zoom, Continuous",
- .entity = UVC_GUID_UVC_CAMERA,
- .selector = UVC_CT_ZOOM_RELATIVE_CONTROL,
- .size = 0,
- .offset = 0,
- .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
- .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
- .get = uvc_ctrl_get_zoom,
- .set = uvc_ctrl_set_zoom,
- },
- {
- .id = V4L2_CID_PAN_ABSOLUTE,
- .name = "Pan (Absolute)",
- .entity = UVC_GUID_UVC_CAMERA,
- .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
- .size = 32,
- .offset = 0,
- .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
- .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
- },
- {
- .id = V4L2_CID_TILT_ABSOLUTE,
- .name = "Tilt (Absolute)",
- .entity = UVC_GUID_UVC_CAMERA,
- .selector = UVC_CT_PANTILT_ABSOLUTE_CONTROL,
- .size = 32,
- .offset = 32,
- .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
- .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
- },
- {
- .id = V4L2_CID_PAN_SPEED,
- .name = "Pan (Speed)",
- .entity = UVC_GUID_UVC_CAMERA,
- .selector = UVC_CT_PANTILT_RELATIVE_CONTROL,
- .size = 16,
- .offset = 0,
- .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
- .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
- .get = uvc_ctrl_get_rel_speed,
- .set = uvc_ctrl_set_rel_speed,
- },
- {
- .id = V4L2_CID_TILT_SPEED,
- .name = "Tilt (Speed)",
- .entity = UVC_GUID_UVC_CAMERA,
- .selector = UVC_CT_PANTILT_RELATIVE_CONTROL,
- .size = 16,
- .offset = 16,
- .v4l2_type = V4L2_CTRL_TYPE_INTEGER,
- .data_type = UVC_CTRL_DATA_TYPE_SIGNED,
- .get = uvc_ctrl_get_rel_speed,
- .set = uvc_ctrl_set_rel_speed,
- },
- {
- .id = V4L2_CID_PRIVACY,
- .name = "Privacy",
- .entity = UVC_GUID_UVC_CAMERA,
- .selector = UVC_CT_PRIVACY_CONTROL,
- .size = 1,
- .offset = 0,
- .v4l2_type = V4L2_CTRL_TYPE_BOOLEAN,
- .data_type = UVC_CTRL_DATA_TYPE_BOOLEAN,
- },
- };
- /* ------------------------------------------------------------------------
- * Utility functions
- */
- static inline u8 *uvc_ctrl_data(struct uvc_control *ctrl, int id)
- {
- return ctrl->uvc_data + id * ctrl->info.size;
- }
- static inline int uvc_test_bit(const u8 *data, int bit)
- {
- return (data[bit >> 3] >> (bit & 7)) & 1;
- }
- static inline void uvc_clear_bit(u8 *data, int bit)
- {
- data[bit >> 3] &= ~(1 << (bit & 7));
- }
- /* Extract the bit string specified by mapping->offset and mapping->size
- * from the little-endian data stored at 'data' and return the result as
- * a signed 32bit integer. Sign extension will be performed if the mapping
- * references a signed data type.
- */
- static s32 uvc_get_le_value(struct uvc_control_mapping *mapping,
- u8 query, const u8 *data)
- {
- int bits = mapping->size;
- int offset = mapping->offset;
- s32 value = 0;
- u8 mask;
- data += offset / 8;
- offset &= 7;
- mask = ((1LL << bits) - 1) << offset;
- for (; bits > 0; data++) {
- u8 byte = *data & mask;
- value |= offset > 0 ? (byte >> offset) : (byte << (-offset));
- bits -= 8 - (offset > 0 ? offset : 0);
- offset -= 8;
- mask = (1 << bits) - 1;
- }
- /* Sign-extend the value if needed. */
- if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
- value |= -(value & (1 << (mapping->size - 1)));
- return value;
- }
- /* Set the bit string specified by mapping->offset and mapping->size
- * in the little-endian data stored at 'data' to the value 'value'.
- */
- static void uvc_set_le_value(struct uvc_control_mapping *mapping,
- s32 value, u8 *data)
- {
- int bits = mapping->size;
- int offset = mapping->offset;
- u8 mask;
- /* According to the v4l2 spec, writing any value to a button control
- * should result in the action belonging to the button control being
- * triggered. UVC devices however want to see a 1 written -> override
- * value.
- */
- if (mapping->v4l2_type == V4L2_CTRL_TYPE_BUTTON)
- value = -1;
- data += offset / 8;
- offset &= 7;
- for (; bits > 0; data++) {
- mask = ((1LL << bits) - 1) << offset;
- *data = (*data & ~mask) | ((value << offset) & mask);
- value >>= offset ? offset : 8;
- bits -= 8 - offset;
- offset = 0;
- }
- }
- /* ------------------------------------------------------------------------
- * Terminal and unit management
- */
- static const u8 uvc_processing_guid[16] = UVC_GUID_UVC_PROCESSING;
- static const u8 uvc_camera_guid[16] = UVC_GUID_UVC_CAMERA;
- static const u8 uvc_media_transport_input_guid[16] =
- UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT;
- static int uvc_entity_match_guid(const struct uvc_entity *entity,
- const u8 guid[16])
- {
- switch (UVC_ENTITY_TYPE(entity)) {
- case UVC_ITT_CAMERA:
- return memcmp(uvc_camera_guid, guid, 16) == 0;
- case UVC_ITT_MEDIA_TRANSPORT_INPUT:
- return memcmp(uvc_media_transport_input_guid, guid, 16) == 0;
- case UVC_VC_PROCESSING_UNIT:
- return memcmp(uvc_processing_guid, guid, 16) == 0;
- case UVC_VC_EXTENSION_UNIT:
- return memcmp(entity->extension.guidExtensionCode,
- guid, 16) == 0;
- default:
- return 0;
- }
- }
- /* ------------------------------------------------------------------------
- * UVC Controls
- */
- static void __uvc_find_control(struct uvc_entity *entity, u32 v4l2_id,
- struct uvc_control_mapping **mapping, struct uvc_control **control,
- int next)
- {
- struct uvc_control *ctrl;
- struct uvc_control_mapping *map;
- unsigned int i;
- if (entity == NULL)
- return;
- for (i = 0; i < entity->ncontrols; ++i) {
- ctrl = &entity->controls[i];
- if (!ctrl->initialized)
- continue;
- list_for_each_entry(map, &ctrl->info.mappings, list) {
- if ((map->id == v4l2_id) && !next) {
- *control = ctrl;
- *mapping = map;
- return;
- }
- if ((*mapping == NULL || (*mapping)->id > map->id) &&
- (map->id > v4l2_id) && next) {
- *control = ctrl;
- *mapping = map;
- }
- }
- }
- }
- static struct uvc_control *uvc_find_control(struct uvc_video_chain *chain,
- u32 v4l2_id, struct uvc_control_mapping **mapping)
- {
- struct uvc_control *ctrl = NULL;
- struct uvc_entity *entity;
- int next = v4l2_id & V4L2_CTRL_FLAG_NEXT_CTRL;
- *mapping = NULL;
- /* Mask the query flags. */
- v4l2_id &= V4L2_CTRL_ID_MASK;
- /* Find the control. */
- list_for_each_entry(entity, &chain->entities, chain) {
- __uvc_find_control(entity, v4l2_id, mapping, &ctrl, next);
- if (ctrl && !next)
- return ctrl;
- }
- if (ctrl == NULL && !next)
- uvc_trace(UVC_TRACE_CONTROL, "Control 0x%08x not found.\n",
- v4l2_id);
- return ctrl;
- }
- static int uvc_ctrl_populate_cache(struct uvc_video_chain *chain,
- struct uvc_control *ctrl)
- {
- int ret;
- if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
- ret = uvc_query_ctrl(chain->dev, UVC_GET_DEF, ctrl->entity->id,
- chain->dev->intfnum, ctrl->info.selector,
- uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF),
- ctrl->info.size);
- if (ret < 0)
- return ret;
- }
- if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN) {
- ret = uvc_query_ctrl(chain->dev, UVC_GET_MIN, ctrl->entity->id,
- chain->dev->intfnum, ctrl->info.selector,
- uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN),
- ctrl->info.size);
- if (ret < 0)
- return ret;
- }
- if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX) {
- ret = uvc_query_ctrl(chain->dev, UVC_GET_MAX, ctrl->entity->id,
- chain->dev->intfnum, ctrl->info.selector,
- uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX),
- ctrl->info.size);
- if (ret < 0)
- return ret;
- }
- if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES) {
- ret = uvc_query_ctrl(chain->dev, UVC_GET_RES, ctrl->entity->id,
- chain->dev->intfnum, ctrl->info.selector,
- uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES),
- ctrl->info.size);
- if (ret < 0) {
- if (UVC_ENTITY_TYPE(ctrl->entity) !=
- UVC_VC_EXTENSION_UNIT)
- return ret;
- /* GET_RES is mandatory for XU controls, but some
- * cameras still choke on it. Ignore errors and set the
- * resolution value to zero.
- */
- uvc_warn_once(chain->dev, UVC_WARN_XU_GET_RES,
- "UVC non compliance - GET_RES failed on "
- "an XU control. Enabling workaround.\n");
- memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES), 0,
- ctrl->info.size);
- }
- }
- ctrl->cached = 1;
- return 0;
- }
- static s32 __uvc_ctrl_get_value(struct uvc_control_mapping *mapping,
- const u8 *data)
- {
- s32 value = mapping->get(mapping, UVC_GET_CUR, data);
- if (mapping->v4l2_type == V4L2_CTRL_TYPE_MENU) {
- struct uvc_menu_info *menu = mapping->menu_info;
- unsigned int i;
- for (i = 0; i < mapping->menu_count; ++i, ++menu) {
- if (menu->value == value) {
- value = i;
- break;
- }
- }
- }
- return value;
- }
- static int __uvc_ctrl_get(struct uvc_video_chain *chain,
- struct uvc_control *ctrl, struct uvc_control_mapping *mapping,
- s32 *value)
- {
- int ret;
- if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0)
- return -EACCES;
- if (!ctrl->loaded) {
- ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR, ctrl->entity->id,
- chain->dev->intfnum, ctrl->info.selector,
- uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
- ctrl->info.size);
- if (ret < 0)
- return ret;
- ctrl->loaded = 1;
- }
- *value = __uvc_ctrl_get_value(mapping,
- uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
- return 0;
- }
- static int __uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
- struct uvc_control *ctrl,
- struct uvc_control_mapping *mapping,
- struct v4l2_queryctrl *v4l2_ctrl)
- {
- struct uvc_control_mapping *master_map = NULL;
- struct uvc_control *master_ctrl = NULL;
- struct uvc_menu_info *menu;
- unsigned int i;
- memset(v4l2_ctrl, 0, sizeof(*v4l2_ctrl));
- v4l2_ctrl->id = mapping->id;
- v4l2_ctrl->type = mapping->v4l2_type;
- strlcpy(v4l2_ctrl->name, mapping->name, sizeof(v4l2_ctrl->name));
- v4l2_ctrl->flags = 0;
- if (!(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
- v4l2_ctrl->flags |= V4L2_CTRL_FLAG_WRITE_ONLY;
- if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
- v4l2_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
- if (mapping->master_id)
- __uvc_find_control(ctrl->entity, mapping->master_id,
- &master_map, &master_ctrl, 0);
- if (master_ctrl && (master_ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR)) {
- s32 val;
- int ret = __uvc_ctrl_get(chain, master_ctrl, master_map, &val);
- if (ret < 0)
- return ret;
- if (val != mapping->master_manual)
- v4l2_ctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
- }
- if (!ctrl->cached) {
- int ret = uvc_ctrl_populate_cache(chain, ctrl);
- if (ret < 0)
- return ret;
- }
- if (ctrl->info.flags & UVC_CTRL_FLAG_GET_DEF) {
- v4l2_ctrl->default_value = mapping->get(mapping, UVC_GET_DEF,
- uvc_ctrl_data(ctrl, UVC_CTRL_DATA_DEF));
- }
- switch (mapping->v4l2_type) {
- case V4L2_CTRL_TYPE_MENU:
- v4l2_ctrl->minimum = 0;
- v4l2_ctrl->maximum = mapping->menu_count - 1;
- v4l2_ctrl->step = 1;
- menu = mapping->menu_info;
- for (i = 0; i < mapping->menu_count; ++i, ++menu) {
- if (menu->value == v4l2_ctrl->default_value) {
- v4l2_ctrl->default_value = i;
- break;
- }
- }
- return 0;
- case V4L2_CTRL_TYPE_BOOLEAN:
- v4l2_ctrl->minimum = 0;
- v4l2_ctrl->maximum = 1;
- v4l2_ctrl->step = 1;
- return 0;
- case V4L2_CTRL_TYPE_BUTTON:
- v4l2_ctrl->minimum = 0;
- v4l2_ctrl->maximum = 0;
- v4l2_ctrl->step = 0;
- return 0;
- default:
- break;
- }
- if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MIN)
- v4l2_ctrl->minimum = mapping->get(mapping, UVC_GET_MIN,
- uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
- if (ctrl->info.flags & UVC_CTRL_FLAG_GET_MAX)
- v4l2_ctrl->maximum = mapping->get(mapping, UVC_GET_MAX,
- uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
- if (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)
- v4l2_ctrl->step = mapping->get(mapping, UVC_GET_RES,
- uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
- return 0;
- }
- int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
- struct v4l2_queryctrl *v4l2_ctrl)
- {
- struct uvc_control *ctrl;
- struct uvc_control_mapping *mapping;
- int ret;
- ret = mutex_lock_interruptible(&chain->ctrl_mutex);
- if (ret < 0)
- return -ERESTARTSYS;
- ctrl = uvc_find_control(chain, v4l2_ctrl->id, &mapping);
- if (ctrl == NULL) {
- ret = -EINVAL;
- goto done;
- }
- ret = __uvc_query_v4l2_ctrl(chain, ctrl, mapping, v4l2_ctrl);
- done:
- mutex_unlock(&chain->ctrl_mutex);
- return ret;
- }
- /*
- * Mapping V4L2 controls to UVC controls can be straightforward if done well.
- * Most of the UVC controls exist in V4L2, and can be mapped directly. Some
- * must be grouped (for instance the Red Balance, Blue Balance and Do White
- * Balance V4L2 controls use the White Balance Component UVC control) or
- * otherwise translated. The approach we take here is to use a translation
- * table for the controls that can be mapped directly, and handle the others
- * manually.
- */
- int uvc_query_v4l2_menu(struct uvc_video_chain *chain,
- struct v4l2_querymenu *query_menu)
- {
- struct uvc_menu_info *menu_info;
- struct uvc_control_mapping *mapping;
- struct uvc_control *ctrl;
- u32 index = query_menu->index;
- u32 id = query_menu->id;
- int ret;
- memset(query_menu, 0, sizeof(*query_menu));
- query_menu->id = id;
- query_menu->index = index;
- ret = mutex_lock_interruptible(&chain->ctrl_mutex);
- if (ret < 0)
- return -ERESTARTSYS;
- ctrl = uvc_find_control(chain, query_menu->id, &mapping);
- if (ctrl == NULL || mapping->v4l2_type != V4L2_CTRL_TYPE_MENU) {
- ret = -EINVAL;
- goto done;
- }
- if (query_menu->index >= mapping->menu_count) {
- ret = -EINVAL;
- goto done;
- }
- menu_info = &mapping->menu_info[query_menu->index];
- if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK &&
- (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)) {
- s32 bitmap;
- if (!ctrl->cached) {
- ret = uvc_ctrl_populate_cache(chain, ctrl);
- if (ret < 0)
- goto done;
- }
- bitmap = mapping->get(mapping, UVC_GET_RES,
- uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
- if (!(bitmap & menu_info->value)) {
- ret = -EINVAL;
- goto done;
- }
- }
- strlcpy(query_menu->name, menu_info->name, sizeof(query_menu->name));
- done:
- mutex_unlock(&chain->ctrl_mutex);
- return ret;
- }
- /* --------------------------------------------------------------------------
- * Ctrl event handling
- */
- static void uvc_ctrl_fill_event(struct uvc_video_chain *chain,
- struct v4l2_event *ev,
- struct uvc_control *ctrl,
- struct uvc_control_mapping *mapping,
- s32 value, u32 changes)
- {
- struct v4l2_queryctrl v4l2_ctrl;
- __uvc_query_v4l2_ctrl(chain, ctrl, mapping, &v4l2_ctrl);
- memset(ev, 0, sizeof(*ev));
- ev->type = V4L2_EVENT_CTRL;
- ev->id = v4l2_ctrl.id;
- ev->u.ctrl.value = value;
- ev->u.ctrl.changes = changes;
- ev->u.ctrl.type = v4l2_ctrl.type;
- ev->u.ctrl.flags = v4l2_ctrl.flags;
- ev->u.ctrl.minimum = v4l2_ctrl.minimum;
- ev->u.ctrl.maximum = v4l2_ctrl.maximum;
- ev->u.ctrl.step = v4l2_ctrl.step;
- ev->u.ctrl.default_value = v4l2_ctrl.default_value;
- }
- /*
- * Send control change events to all subscribers for the @ctrl control. By
- * default the subscriber that generated the event, as identified by @handle,
- * is not notified unless it has set the V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK flag.
- * @handle can be NULL for asynchronous events related to auto-update controls,
- * in which case all subscribers are notified.
- */
- static void uvc_ctrl_send_event(struct uvc_video_chain *chain,
- struct uvc_fh *handle, struct uvc_control *ctrl,
- struct uvc_control_mapping *mapping, s32 value, u32 changes)
- {
- struct v4l2_fh *originator = handle ? &handle->vfh : NULL;
- struct v4l2_subscribed_event *sev;
- struct v4l2_event ev;
- if (list_empty(&mapping->ev_subs))
- return;
- uvc_ctrl_fill_event(chain, &ev, ctrl, mapping, value, changes);
- list_for_each_entry(sev, &mapping->ev_subs, node) {
- if (sev->fh != originator ||
- (sev->flags & V4L2_EVENT_SUB_FL_ALLOW_FEEDBACK) ||
- (changes & V4L2_EVENT_CTRL_CH_FLAGS))
- v4l2_event_queue_fh(sev->fh, &ev);
- }
- }
- /*
- * Send control change events for the slave of the @master control identified
- * by the V4L2 ID @slave_id. The @handle identifies the event subscriber that
- * generated the event and may be NULL for auto-update events.
- */
- static void uvc_ctrl_send_slave_event(struct uvc_video_chain *chain,
- struct uvc_fh *handle, struct uvc_control *master, u32 slave_id)
- {
- struct uvc_control_mapping *mapping = NULL;
- struct uvc_control *ctrl = NULL;
- u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
- s32 val = 0;
- __uvc_find_control(master->entity, slave_id, &mapping, &ctrl, 0);
- if (ctrl == NULL)
- return;
- if (__uvc_ctrl_get(chain, ctrl, mapping, &val) == 0)
- changes |= V4L2_EVENT_CTRL_CH_VALUE;
- uvc_ctrl_send_event(chain, handle, ctrl, mapping, val, changes);
- }
- static void uvc_ctrl_status_event_work(struct work_struct *work)
- {
- struct uvc_device *dev = container_of(work, struct uvc_device,
- async_ctrl.work);
- struct uvc_ctrl_work *w = &dev->async_ctrl;
- struct uvc_video_chain *chain = w->chain;
- struct uvc_control_mapping *mapping;
- struct uvc_control *ctrl = w->ctrl;
- struct uvc_fh *handle;
- unsigned int i;
- int ret;
- mutex_lock(&chain->ctrl_mutex);
- handle = ctrl->handle;
- ctrl->handle = NULL;
- list_for_each_entry(mapping, &ctrl->info.mappings, list) {
- s32 value = __uvc_ctrl_get_value(mapping, w->data);
- /*
- * handle may be NULL here if the device sends auto-update
- * events without a prior related control set from userspace.
- */
- for (i = 0; i < ARRAY_SIZE(mapping->slave_ids); ++i) {
- if (!mapping->slave_ids[i])
- break;
- uvc_ctrl_send_slave_event(chain, handle, ctrl,
- mapping->slave_ids[i]);
- }
- uvc_ctrl_send_event(chain, handle, ctrl, mapping, value,
- V4L2_EVENT_CTRL_CH_VALUE);
- }
- mutex_unlock(&chain->ctrl_mutex);
- /* Resubmit the URB. */
- w->urb->interval = dev->int_ep->desc.bInterval;
- ret = usb_submit_urb(w->urb, GFP_KERNEL);
- if (ret < 0)
- uvc_printk(KERN_ERR, "Failed to resubmit status URB (%d).\n",
- ret);
- }
- bool uvc_ctrl_status_event(struct urb *urb, struct uvc_video_chain *chain,
- struct uvc_control *ctrl, const u8 *data)
- {
- struct uvc_device *dev = chain->dev;
- struct uvc_ctrl_work *w = &dev->async_ctrl;
- if (list_empty(&ctrl->info.mappings)) {
- ctrl->handle = NULL;
- return false;
- }
- w->data = data;
- w->urb = urb;
- w->chain = chain;
- w->ctrl = ctrl;
- schedule_work(&w->work);
- return true;
- }
- static bool uvc_ctrl_xctrls_has_control(const struct v4l2_ext_control *xctrls,
- unsigned int xctrls_count, u32 id)
- {
- unsigned int i;
- for (i = 0; i < xctrls_count; ++i) {
- if (xctrls[i].id == id)
- return true;
- }
- return false;
- }
- static void uvc_ctrl_send_events(struct uvc_fh *handle,
- const struct v4l2_ext_control *xctrls, unsigned int xctrls_count)
- {
- struct uvc_control_mapping *mapping;
- struct uvc_control *ctrl;
- u32 changes = V4L2_EVENT_CTRL_CH_VALUE;
- unsigned int i;
- unsigned int j;
- for (i = 0; i < xctrls_count; ++i) {
- ctrl = uvc_find_control(handle->chain, xctrls[i].id, &mapping);
- if (ctrl->info.flags & UVC_CTRL_FLAG_ASYNCHRONOUS)
- /* Notification will be sent from an Interrupt event. */
- continue;
- for (j = 0; j < ARRAY_SIZE(mapping->slave_ids); ++j) {
- u32 slave_id = mapping->slave_ids[j];
- if (!slave_id)
- break;
- /*
- * We can skip sending an event for the slave if the
- * slave is being modified in the same transaction.
- */
- if (uvc_ctrl_xctrls_has_control(xctrls, xctrls_count,
- slave_id))
- continue;
- uvc_ctrl_send_slave_event(handle->chain, handle, ctrl,
- slave_id);
- }
- /*
- * If the master is being modified in the same transaction
- * flags may change too.
- */
- if (mapping->master_id &&
- uvc_ctrl_xctrls_has_control(xctrls, xctrls_count,
- mapping->master_id))
- changes |= V4L2_EVENT_CTRL_CH_FLAGS;
- uvc_ctrl_send_event(handle->chain, handle, ctrl, mapping,
- xctrls[i].value, changes);
- }
- }
- static int uvc_ctrl_add_event(struct v4l2_subscribed_event *sev, unsigned elems)
- {
- struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);
- struct uvc_control_mapping *mapping;
- struct uvc_control *ctrl;
- int ret;
- ret = mutex_lock_interruptible(&handle->chain->ctrl_mutex);
- if (ret < 0)
- return -ERESTARTSYS;
- ctrl = uvc_find_control(handle->chain, sev->id, &mapping);
- if (ctrl == NULL) {
- ret = -EINVAL;
- goto done;
- }
- list_add_tail(&sev->node, &mapping->ev_subs);
- if (sev->flags & V4L2_EVENT_SUB_FL_SEND_INITIAL) {
- struct v4l2_event ev;
- u32 changes = V4L2_EVENT_CTRL_CH_FLAGS;
- s32 val = 0;
- if (__uvc_ctrl_get(handle->chain, ctrl, mapping, &val) == 0)
- changes |= V4L2_EVENT_CTRL_CH_VALUE;
- uvc_ctrl_fill_event(handle->chain, &ev, ctrl, mapping, val,
- changes);
- /* Mark the queue as active, allowing this initial
- event to be accepted. */
- sev->elems = elems;
- v4l2_event_queue_fh(sev->fh, &ev);
- }
- done:
- mutex_unlock(&handle->chain->ctrl_mutex);
- return ret;
- }
- static void uvc_ctrl_del_event(struct v4l2_subscribed_event *sev)
- {
- struct uvc_fh *handle = container_of(sev->fh, struct uvc_fh, vfh);
- mutex_lock(&handle->chain->ctrl_mutex);
- list_del(&sev->node);
- mutex_unlock(&handle->chain->ctrl_mutex);
- }
- const struct v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops = {
- .add = uvc_ctrl_add_event,
- .del = uvc_ctrl_del_event,
- .replace = v4l2_ctrl_replace,
- .merge = v4l2_ctrl_merge,
- };
- /* --------------------------------------------------------------------------
- * Control transactions
- *
- * To make extended set operations as atomic as the hardware allows, controls
- * are handled using begin/commit/rollback operations.
- *
- * At the beginning of a set request, uvc_ctrl_begin should be called to
- * initialize the request. This function acquires the control lock.
- *
- * When setting a control, the new value is stored in the control data field
- * at position UVC_CTRL_DATA_CURRENT. The control is then marked as dirty for
- * later processing. If the UVC and V4L2 control sizes differ, the current
- * value is loaded from the hardware before storing the new value in the data
- * field.
- *
- * After processing all controls in the transaction, uvc_ctrl_commit or
- * uvc_ctrl_rollback must be called to apply the pending changes to the
- * hardware or revert them. When applying changes, all controls marked as
- * dirty will be modified in the UVC device, and the dirty flag will be
- * cleared. When reverting controls, the control data field
- * UVC_CTRL_DATA_CURRENT is reverted to its previous value
- * (UVC_CTRL_DATA_BACKUP) for all dirty controls. Both functions release the
- * control lock.
- */
- int uvc_ctrl_begin(struct uvc_video_chain *chain)
- {
- return mutex_lock_interruptible(&chain->ctrl_mutex) ? -ERESTARTSYS : 0;
- }
- static int uvc_ctrl_commit_entity(struct uvc_device *dev,
- struct uvc_entity *entity, int rollback)
- {
- struct uvc_control *ctrl;
- unsigned int i;
- int ret;
- if (entity == NULL)
- return 0;
- for (i = 0; i < entity->ncontrols; ++i) {
- ctrl = &entity->controls[i];
- if (!ctrl->initialized)
- continue;
- /* Reset the loaded flag for auto-update controls that were
- * marked as loaded in uvc_ctrl_get/uvc_ctrl_set to prevent
- * uvc_ctrl_get from using the cached value, and for write-only
- * controls to prevent uvc_ctrl_set from setting bits not
- * explicitly set by the user.
- */
- if (ctrl->info.flags & UVC_CTRL_FLAG_AUTO_UPDATE ||
- !(ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR))
- ctrl->loaded = 0;
- if (!ctrl->dirty)
- continue;
- if (!rollback)
- ret = uvc_query_ctrl(dev, UVC_SET_CUR, ctrl->entity->id,
- dev->intfnum, ctrl->info.selector,
- uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
- ctrl->info.size);
- else
- ret = 0;
- if (rollback || ret < 0)
- memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
- uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
- ctrl->info.size);
- ctrl->dirty = 0;
- if (ret < 0)
- return ret;
- }
- return 0;
- }
- int __uvc_ctrl_commit(struct uvc_fh *handle, int rollback,
- const struct v4l2_ext_control *xctrls,
- unsigned int xctrls_count)
- {
- struct uvc_video_chain *chain = handle->chain;
- struct uvc_entity *entity;
- int ret = 0;
- /* Find the control. */
- list_for_each_entry(entity, &chain->entities, chain) {
- ret = uvc_ctrl_commit_entity(chain->dev, entity, rollback);
- if (ret < 0)
- goto done;
- }
- if (!rollback)
- uvc_ctrl_send_events(handle, xctrls, xctrls_count);
- done:
- mutex_unlock(&chain->ctrl_mutex);
- return ret;
- }
- int uvc_ctrl_get(struct uvc_video_chain *chain,
- struct v4l2_ext_control *xctrl)
- {
- struct uvc_control *ctrl;
- struct uvc_control_mapping *mapping;
- ctrl = uvc_find_control(chain, xctrl->id, &mapping);
- if (ctrl == NULL)
- return -EINVAL;
- return __uvc_ctrl_get(chain, ctrl, mapping, &xctrl->value);
- }
- int uvc_ctrl_set(struct uvc_fh *handle,
- struct v4l2_ext_control *xctrl)
- {
- struct uvc_video_chain *chain = handle->chain;
- struct uvc_control *ctrl;
- struct uvc_control_mapping *mapping;
- s32 value;
- u32 step;
- s32 min;
- s32 max;
- int ret;
- ctrl = uvc_find_control(chain, xctrl->id, &mapping);
- if (ctrl == NULL)
- return -EINVAL;
- if (!(ctrl->info.flags & UVC_CTRL_FLAG_SET_CUR))
- return -EACCES;
- /* Clamp out of range values. */
- switch (mapping->v4l2_type) {
- case V4L2_CTRL_TYPE_INTEGER:
- if (!ctrl->cached) {
- ret = uvc_ctrl_populate_cache(chain, ctrl);
- if (ret < 0)
- return ret;
- }
- min = mapping->get(mapping, UVC_GET_MIN,
- uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MIN));
- max = mapping->get(mapping, UVC_GET_MAX,
- uvc_ctrl_data(ctrl, UVC_CTRL_DATA_MAX));
- step = mapping->get(mapping, UVC_GET_RES,
- uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
- if (step == 0)
- step = 1;
- xctrl->value = min + ((u32)(xctrl->value - min) + step / 2)
- / step * step;
- if (mapping->data_type == UVC_CTRL_DATA_TYPE_SIGNED)
- xctrl->value = clamp(xctrl->value, min, max);
- else
- xctrl->value = clamp_t(u32, xctrl->value, min, max);
- value = xctrl->value;
- break;
- case V4L2_CTRL_TYPE_BOOLEAN:
- xctrl->value = clamp(xctrl->value, 0, 1);
- value = xctrl->value;
- break;
- case V4L2_CTRL_TYPE_MENU:
- if (xctrl->value < 0 || xctrl->value >= mapping->menu_count)
- return -ERANGE;
- value = mapping->menu_info[xctrl->value].value;
- /* Valid menu indices are reported by the GET_RES request for
- * UVC controls that support it.
- */
- if (mapping->data_type == UVC_CTRL_DATA_TYPE_BITMASK &&
- (ctrl->info.flags & UVC_CTRL_FLAG_GET_RES)) {
- if (!ctrl->cached) {
- ret = uvc_ctrl_populate_cache(chain, ctrl);
- if (ret < 0)
- return ret;
- }
- step = mapping->get(mapping, UVC_GET_RES,
- uvc_ctrl_data(ctrl, UVC_CTRL_DATA_RES));
- if (!(step & value))
- return -EINVAL;
- }
- break;
- default:
- value = xctrl->value;
- break;
- }
- /* If the mapping doesn't span the whole UVC control, the current value
- * needs to be loaded from the device to perform the read-modify-write
- * operation.
- */
- if (!ctrl->loaded && (ctrl->info.size * 8) != mapping->size) {
- if ((ctrl->info.flags & UVC_CTRL_FLAG_GET_CUR) == 0) {
- memset(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
- 0, ctrl->info.size);
- } else {
- ret = uvc_query_ctrl(chain->dev, UVC_GET_CUR,
- ctrl->entity->id, chain->dev->intfnum,
- ctrl->info.selector,
- uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
- ctrl->info.size);
- if (ret < 0)
- return ret;
- }
- ctrl->loaded = 1;
- }
- /* Backup the current value in case we need to rollback later. */
- if (!ctrl->dirty) {
- memcpy(uvc_ctrl_data(ctrl, UVC_CTRL_DATA_BACKUP),
- uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
- ctrl->info.size);
- }
- mapping->set(mapping, value,
- uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT));
- if (ctrl->info.flags & UVC_CTRL_FLAG_ASYNCHRONOUS)
- ctrl->handle = handle;
- ctrl->dirty = 1;
- ctrl->modified = 1;
- return 0;
- }
- /* --------------------------------------------------------------------------
- * Dynamic controls
- */
- /*
- * Retrieve flags for a given control
- */
- static int uvc_ctrl_get_flags(struct uvc_device *dev,
- const struct uvc_control *ctrl,
- struct uvc_control_info *info)
- {
- u8 *data;
- int ret;
- data = kmalloc(1, GFP_KERNEL);
- if (data == NULL)
- return -ENOMEM;
- ret = uvc_query_ctrl(dev, UVC_GET_INFO, ctrl->entity->id, dev->intfnum,
- info->selector, data, 1);
- if (!ret)
- info->flags |= (data[0] & UVC_CONTROL_CAP_GET ?
- UVC_CTRL_FLAG_GET_CUR : 0)
- | (data[0] & UVC_CONTROL_CAP_SET ?
- UVC_CTRL_FLAG_SET_CUR : 0)
- | (data[0] & UVC_CONTROL_CAP_AUTOUPDATE ?
- UVC_CTRL_FLAG_AUTO_UPDATE : 0)
- | (data[0] & UVC_CONTROL_CAP_ASYNCHRONOUS ?
- UVC_CTRL_FLAG_ASYNCHRONOUS : 0);
- kfree(data);
- return ret;
- }
- static void uvc_ctrl_fixup_xu_info(struct uvc_device *dev,
- const struct uvc_control *ctrl, struct uvc_control_info *info)
- {
- struct uvc_ctrl_fixup {
- struct usb_device_id id;
- u8 entity;
- u8 selector;
- u8 flags;
- };
- static const struct uvc_ctrl_fixup fixups[] = {
- { { USB_DEVICE(0x046d, 0x08c2) }, 9, 1,
- UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
- UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
- UVC_CTRL_FLAG_AUTO_UPDATE },
- { { USB_DEVICE(0x046d, 0x08cc) }, 9, 1,
- UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
- UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
- UVC_CTRL_FLAG_AUTO_UPDATE },
- { { USB_DEVICE(0x046d, 0x0994) }, 9, 1,
- UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX |
- UVC_CTRL_FLAG_GET_DEF | UVC_CTRL_FLAG_SET_CUR |
- UVC_CTRL_FLAG_AUTO_UPDATE },
- };
- unsigned int i;
- for (i = 0; i < ARRAY_SIZE(fixups); ++i) {
- if (!usb_match_one_id(dev->intf, &fixups[i].id))
- continue;
- if (fixups[i].entity == ctrl->entity->id &&
- fixups[i].selector == info->selector) {
- info->flags = fixups[i].flags;
- return;
- }
- }
- }
- /*
- * Query control information (size and flags) for XU controls.
- */
- static int uvc_ctrl_fill_xu_info(struct uvc_device *dev,
- const struct uvc_control *ctrl, struct uvc_control_info *info)
- {
- u8 *data;
- int ret;
- data = kmalloc(2, GFP_KERNEL);
- if (data == NULL)
- return -ENOMEM;
- memcpy(info->entity, ctrl->entity->extension.guidExtensionCode,
- sizeof(info->entity));
- info->index = ctrl->index;
- info->selector = ctrl->index + 1;
- /* Query and verify the control length (GET_LEN) */
- ret = uvc_query_ctrl(dev, UVC_GET_LEN, ctrl->entity->id, dev->intfnum,
- info->selector, data, 2);
- if (ret < 0) {
- uvc_trace(UVC_TRACE_CONTROL,
- "GET_LEN failed on control %pUl/%u (%d).\n",
- info->entity, info->selector, ret);
- goto done;
- }
- info->size = le16_to_cpup((__le16 *)data);
- info->flags = UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX
- | UVC_CTRL_FLAG_GET_RES | UVC_CTRL_FLAG_GET_DEF;
- ret = uvc_ctrl_get_flags(dev, ctrl, info);
- if (ret < 0) {
- uvc_trace(UVC_TRACE_CONTROL,
- "Failed to get flags for control %pUl/%u (%d).\n",
- info->entity, info->selector, ret);
- goto done;
- }
- uvc_ctrl_fixup_xu_info(dev, ctrl, info);
- uvc_trace(UVC_TRACE_CONTROL, "XU control %pUl/%u queried: len %u, "
- "flags { get %u set %u auto %u }.\n",
- info->entity, info->selector, info->size,
- (info->flags & UVC_CTRL_FLAG_GET_CUR) ? 1 : 0,
- (info->flags & UVC_CTRL_FLAG_SET_CUR) ? 1 : 0,
- (info->flags & UVC_CTRL_FLAG_AUTO_UPDATE) ? 1 : 0);
- done:
- kfree(data);
- return ret;
- }
- static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
- const struct uvc_control_info *info);
- static int uvc_ctrl_init_xu_ctrl(struct uvc_device *dev,
- struct uvc_control *ctrl)
- {
- struct uvc_control_info info;
- int ret;
- if (ctrl->initialized)
- return 0;
- ret = uvc_ctrl_fill_xu_info(dev, ctrl, &info);
- if (ret < 0)
- return ret;
- ret = uvc_ctrl_add_info(dev, ctrl, &info);
- if (ret < 0)
- uvc_trace(UVC_TRACE_CONTROL, "Failed to initialize control "
- "%pUl/%u on device %s entity %u\n", info.entity,
- info.selector, dev->udev->devpath, ctrl->entity->id);
- return ret;
- }
- int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
- struct uvc_xu_control_query *xqry)
- {
- struct uvc_entity *entity;
- struct uvc_control *ctrl;
- unsigned int i, found = 0;
- u32 reqflags;
- u16 size;
- u8 *data = NULL;
- int ret;
- /* Find the extension unit. */
- list_for_each_entry(entity, &chain->entities, chain) {
- if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT &&
- entity->id == xqry->unit)
- break;
- }
- if (entity->id != xqry->unit) {
- uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
- xqry->unit);
- return -ENOENT;
- }
- /* Find the control and perform delayed initialization if needed. */
- for (i = 0; i < entity->ncontrols; ++i) {
- ctrl = &entity->controls[i];
- if (ctrl->index == xqry->selector - 1) {
- found = 1;
- break;
- }
- }
- if (!found) {
- uvc_trace(UVC_TRACE_CONTROL, "Control %pUl/%u not found.\n",
- entity->extension.guidExtensionCode, xqry->selector);
- return -ENOENT;
- }
- if (mutex_lock_interruptible(&chain->ctrl_mutex))
- return -ERESTARTSYS;
- ret = uvc_ctrl_init_xu_ctrl(chain->dev, ctrl);
- if (ret < 0) {
- ret = -ENOENT;
- goto done;
- }
- /* Validate the required buffer size and flags for the request */
- reqflags = 0;
- size = ctrl->info.size;
- switch (xqry->query) {
- case UVC_GET_CUR:
- reqflags = UVC_CTRL_FLAG_GET_CUR;
- break;
- case UVC_GET_MIN:
- reqflags = UVC_CTRL_FLAG_GET_MIN;
- break;
- case UVC_GET_MAX:
- reqflags = UVC_CTRL_FLAG_GET_MAX;
- break;
- case UVC_GET_DEF:
- reqflags = UVC_CTRL_FLAG_GET_DEF;
- break;
- case UVC_GET_RES:
- reqflags = UVC_CTRL_FLAG_GET_RES;
- break;
- case UVC_SET_CUR:
- reqflags = UVC_CTRL_FLAG_SET_CUR;
- break;
- case UVC_GET_LEN:
- size = 2;
- break;
- case UVC_GET_INFO:
- size = 1;
- break;
- default:
- ret = -EINVAL;
- goto done;
- }
- if (size != xqry->size) {
- ret = -ENOBUFS;
- goto done;
- }
- if (reqflags && !(ctrl->info.flags & reqflags)) {
- ret = -EBADRQC;
- goto done;
- }
- data = kmalloc(size, GFP_KERNEL);
- if (data == NULL) {
- ret = -ENOMEM;
- goto done;
- }
- if (xqry->query == UVC_SET_CUR &&
- copy_from_user(data, xqry->data, size)) {
- ret = -EFAULT;
- goto done;
- }
- ret = uvc_query_ctrl(chain->dev, xqry->query, xqry->unit,
- chain->dev->intfnum, xqry->selector, data, size);
- if (ret < 0)
- goto done;
- if (xqry->query != UVC_SET_CUR &&
- copy_to_user(xqry->data, data, size))
- ret = -EFAULT;
- done:
- kfree(data);
- mutex_unlock(&chain->ctrl_mutex);
- return ret;
- }
- /* --------------------------------------------------------------------------
- * Suspend/resume
- */
- /*
- * Restore control values after resume, skipping controls that haven't been
- * changed.
- *
- * TODO
- * - Don't restore modified controls that are back to their default value.
- * - Handle restore order (Auto-Exposure Mode should be restored before
- * Exposure Time).
- */
- int uvc_ctrl_restore_values(struct uvc_device *dev)
- {
- struct uvc_control *ctrl;
- struct uvc_entity *entity;
- unsigned int i;
- int ret;
- /* Walk the entities list and restore controls when possible. */
- list_for_each_entry(entity, &dev->entities, list) {
- for (i = 0; i < entity->ncontrols; ++i) {
- ctrl = &entity->controls[i];
- if (!ctrl->initialized || !ctrl->modified ||
- (ctrl->info.flags & UVC_CTRL_FLAG_RESTORE) == 0)
- continue;
- printk(KERN_INFO "restoring control %pUl/%u/%u\n",
- ctrl->info.entity, ctrl->info.index,
- ctrl->info.selector);
- ctrl->dirty = 1;
- }
- ret = uvc_ctrl_commit_entity(dev, entity, 0);
- if (ret < 0)
- return ret;
- }
- return 0;
- }
- /* --------------------------------------------------------------------------
- * Control and mapping handling
- */
- /*
- * Add control information to a given control.
- */
- static int uvc_ctrl_add_info(struct uvc_device *dev, struct uvc_control *ctrl,
- const struct uvc_control_info *info)
- {
- int ret = 0;
- ctrl->info = *info;
- INIT_LIST_HEAD(&ctrl->info.mappings);
- /* Allocate an array to save control values (cur, def, max, etc.) */
- ctrl->uvc_data = kzalloc(ctrl->info.size * UVC_CTRL_DATA_LAST + 1,
- GFP_KERNEL);
- if (ctrl->uvc_data == NULL) {
- ret = -ENOMEM;
- goto done;
- }
- /*
- * Retrieve control flags from the device. Ignore errors and work with
- * default flag values from the uvc_ctrl array when the device doesn't
- * properly implement GET_INFO on standard controls.
- */
- uvc_ctrl_get_flags(dev, ctrl, &ctrl->info);
- ctrl->initialized = 1;
- uvc_trace(UVC_TRACE_CONTROL, "Added control %pUl/%u to device %s "
- "entity %u\n", ctrl->info.entity, ctrl->info.selector,
- dev->udev->devpath, ctrl->entity->id);
- done:
- if (ret < 0)
- kfree(ctrl->uvc_data);
- return ret;
- }
- /*
- * Add a control mapping to a given control.
- */
- static int __uvc_ctrl_add_mapping(struct uvc_device *dev,
- struct uvc_control *ctrl, const struct uvc_control_mapping *mapping)
- {
- struct uvc_control_mapping *map;
- unsigned int size;
- /* Most mappings come from static kernel data and need to be duplicated.
- * Mappings that come from userspace will be unnecessarily duplicated,
- * this could be optimized.
- */
- map = kmemdup(mapping, sizeof(*mapping), GFP_KERNEL);
- if (map == NULL)
- return -ENOMEM;
- INIT_LIST_HEAD(&map->ev_subs);
- size = sizeof(*mapping->menu_info) * mapping->menu_count;
- map->menu_info = kmemdup(mapping->menu_info, size, GFP_KERNEL);
- if (map->menu_info == NULL) {
- kfree(map);
- return -ENOMEM;
- }
- if (map->get == NULL)
- map->get = uvc_get_le_value;
- if (map->set == NULL)
- map->set = uvc_set_le_value;
- list_add_tail(&map->list, &ctrl->info.mappings);
- uvc_trace(UVC_TRACE_CONTROL,
- "Adding mapping '%s' to control %pUl/%u.\n",
- map->name, ctrl->info.entity, ctrl->info.selector);
- return 0;
- }
- int uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
- const struct uvc_control_mapping *mapping)
- {
- struct uvc_device *dev = chain->dev;
- struct uvc_control_mapping *map;
- struct uvc_entity *entity;
- struct uvc_control *ctrl;
- int found = 0;
- int ret;
- if (mapping->id & ~V4L2_CTRL_ID_MASK) {
- uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', control "
- "id 0x%08x is invalid.\n", mapping->name,
- mapping->id);
- return -EINVAL;
- }
- /* Search for the matching (GUID/CS) control on the current chain */
- list_for_each_entry(entity, &chain->entities, chain) {
- unsigned int i;
- if (UVC_ENTITY_TYPE(entity) != UVC_VC_EXTENSION_UNIT ||
- !uvc_entity_match_guid(entity, mapping->entity))
- continue;
- for (i = 0; i < entity->ncontrols; ++i) {
- ctrl = &entity->controls[i];
- if (ctrl->index == mapping->selector - 1) {
- found = 1;
- break;
- }
- }
- if (found)
- break;
- }
- if (!found)
- return -ENOENT;
- if (mutex_lock_interruptible(&chain->ctrl_mutex))
- return -ERESTARTSYS;
- /* Perform delayed initialization of XU controls */
- ret = uvc_ctrl_init_xu_ctrl(dev, ctrl);
- if (ret < 0) {
- ret = -ENOENT;
- goto done;
- }
- /* Validate the user-provided bit-size and offset */
- if (mapping->size > 32 ||
- mapping->offset + mapping->size > ctrl->info.size * 8) {
- ret = -EINVAL;
- goto done;
- }
- list_for_each_entry(map, &ctrl->info.mappings, list) {
- if (mapping->id == map->id) {
- uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', "
- "control id 0x%08x already exists.\n",
- mapping->name, mapping->id);
- ret = -EEXIST;
- goto done;
- }
- }
- /* Prevent excess memory consumption */
- if (atomic_inc_return(&dev->nmappings) > UVC_MAX_CONTROL_MAPPINGS) {
- atomic_dec(&dev->nmappings);
- uvc_trace(UVC_TRACE_CONTROL, "Can't add mapping '%s', maximum "
- "mappings count (%u) exceeded.\n", mapping->name,
- UVC_MAX_CONTROL_MAPPINGS);
- ret = -ENOMEM;
- goto done;
- }
- ret = __uvc_ctrl_add_mapping(dev, ctrl, mapping);
- if (ret < 0)
- atomic_dec(&dev->nmappings);
- done:
- mutex_unlock(&chain->ctrl_mutex);
- return ret;
- }
- /*
- * Prune an entity of its bogus controls using a blacklist. Bogus controls
- * are currently the ones that crash the camera or unconditionally return an
- * error when queried.
- */
- static void uvc_ctrl_prune_entity(struct uvc_device *dev,
- struct uvc_entity *entity)
- {
- struct uvc_ctrl_blacklist {
- struct usb_device_id id;
- u8 index;
- };
- static const struct uvc_ctrl_blacklist processing_blacklist[] = {
- { { USB_DEVICE(0x13d3, 0x509b) }, 9 }, /* Gain */
- { { USB_DEVICE(0x1c4f, 0x3000) }, 6 }, /* WB Temperature */
- { { USB_DEVICE(0x5986, 0x0241) }, 2 }, /* Hue */
- };
- static const struct uvc_ctrl_blacklist camera_blacklist[] = {
- { { USB_DEVICE(0x06f8, 0x3005) }, 9 }, /* Zoom, Absolute */
- };
- const struct uvc_ctrl_blacklist *blacklist;
- unsigned int size;
- unsigned int count;
- unsigned int i;
- u8 *controls;
- switch (UVC_ENTITY_TYPE(entity)) {
- case UVC_VC_PROCESSING_UNIT:
- blacklist = processing_blacklist;
- count = ARRAY_SIZE(processing_blacklist);
- controls = entity->processing.bmControls;
- size = entity->processing.bControlSize;
- break;
- case UVC_ITT_CAMERA:
- blacklist = camera_blacklist;
- count = ARRAY_SIZE(camera_blacklist);
- controls = entity->camera.bmControls;
- size = entity->camera.bControlSize;
- break;
- default:
- return;
- }
- for (i = 0; i < count; ++i) {
- if (!usb_match_one_id(dev->intf, &blacklist[i].id))
- continue;
- if (blacklist[i].index >= 8 * size ||
- !uvc_test_bit(controls, blacklist[i].index))
- continue;
- uvc_trace(UVC_TRACE_CONTROL, "%u/%u control is black listed, "
- "removing it.\n", entity->id, blacklist[i].index);
- uvc_clear_bit(controls, blacklist[i].index);
- }
- }
- /*
- * Add control information and hardcoded stock control mappings to the given
- * device.
- */
- static void uvc_ctrl_init_ctrl(struct uvc_device *dev, struct uvc_control *ctrl)
- {
- const struct uvc_control_info *info = uvc_ctrls;
- const struct uvc_control_info *iend = info + ARRAY_SIZE(uvc_ctrls);
- const struct uvc_control_mapping *mapping = uvc_ctrl_mappings;
- const struct uvc_control_mapping *mend =
- mapping + ARRAY_SIZE(uvc_ctrl_mappings);
- /* XU controls initialization requires querying the device for control
- * information. As some buggy UVC devices will crash when queried
- * repeatedly in a tight loop, delay XU controls initialization until
- * first use.
- */
- if (UVC_ENTITY_TYPE(ctrl->entity) == UVC_VC_EXTENSION_UNIT)
- return;
- for (; info < iend; ++info) {
- if (uvc_entity_match_guid(ctrl->entity, info->entity) &&
- ctrl->index == info->index) {
- uvc_ctrl_add_info(dev, ctrl, info);
- break;
- }
- }
- if (!ctrl->initialized)
- return;
- for (; mapping < mend; ++mapping) {
- if (uvc_entity_match_guid(ctrl->entity, mapping->entity) &&
- ctrl->info.selector == mapping->selector)
- __uvc_ctrl_add_mapping(dev, ctrl, mapping);
- }
- }
- /*
- * Initialize device controls.
- */
- int uvc_ctrl_init_device(struct uvc_device *dev)
- {
- struct uvc_entity *entity;
- unsigned int i;
- INIT_WORK(&dev->async_ctrl.work, uvc_ctrl_status_event_work);
- /* Walk the entities list and instantiate controls */
- list_for_each_entry(entity, &dev->entities, list) {
- struct uvc_control *ctrl;
- unsigned int bControlSize = 0, ncontrols;
- u8 *bmControls = NULL;
- if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT) {
- bmControls = entity->extension.bmControls;
- bControlSize = entity->extension.bControlSize;
- } else if (UVC_ENTITY_TYPE(entity) == UVC_VC_PROCESSING_UNIT) {
- bmControls = entity->processing.bmControls;
- bControlSize = entity->processing.bControlSize;
- } else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
- bmControls = entity->camera.bmControls;
- bControlSize = entity->camera.bControlSize;
- }
- /* Remove bogus/blacklisted controls */
- uvc_ctrl_prune_entity(dev, entity);
- /* Count supported controls and allocate the controls array */
- ncontrols = memweight(bmControls, bControlSize);
- if (ncontrols == 0)
- continue;
- entity->controls = kcalloc(ncontrols, sizeof(*ctrl),
- GFP_KERNEL);
- if (entity->controls == NULL)
- return -ENOMEM;
- entity->ncontrols = ncontrols;
- /* Initialize all supported controls */
- ctrl = entity->controls;
- for (i = 0; i < bControlSize * 8; ++i) {
- if (uvc_test_bit(bmControls, i) == 0)
- continue;
- ctrl->entity = entity;
- ctrl->index = i;
- uvc_ctrl_init_ctrl(dev, ctrl);
- ctrl++;
- }
- }
- return 0;
- }
- /*
- * Cleanup device controls.
- */
- static void uvc_ctrl_cleanup_mappings(struct uvc_device *dev,
- struct uvc_control *ctrl)
- {
- struct uvc_control_mapping *mapping, *nm;
- list_for_each_entry_safe(mapping, nm, &ctrl->info.mappings, list) {
- list_del(&mapping->list);
- kfree(mapping->menu_info);
- kfree(mapping);
- }
- }
- void uvc_ctrl_cleanup_device(struct uvc_device *dev)
- {
- struct uvc_entity *entity;
- unsigned int i;
- /* Can be uninitialized if we are aborting on probe error. */
- if (dev->async_ctrl.work.func)
- cancel_work_sync(&dev->async_ctrl.work);
- /* Free controls and control mappings for all entities. */
- list_for_each_entry(entity, &dev->entities, list) {
- for (i = 0; i < entity->ncontrols; ++i) {
- struct uvc_control *ctrl = &entity->controls[i];
- if (!ctrl->initialized)
- continue;
- uvc_ctrl_cleanup_mappings(dev, ctrl);
- kfree(ctrl->uvc_data);
- }
- kfree(entity->controls);
- }
- }
|