ov511.c 143 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000
  1. /*
  2. * OmniVision OV511 Camera-to-USB Bridge Driver
  3. *
  4. * Copyright (c) 1999-2003 Mark W. McClelland
  5. * Original decompression code Copyright 1998-2000 OmniVision Technologies
  6. * Many improvements by Bret Wallach <bwallac1@san.rr.com>
  7. * Color fixes by by Orion Sky Lawlor <olawlor@acm.org> (2/26/2000)
  8. * Snapshot code by Kevin Moore
  9. * OV7620 fixes by Charl P. Botha <cpbotha@ieee.org>
  10. * Changes by Claudio Matsuoka <claudio@conectiva.com>
  11. * Original SAA7111A code by Dave Perks <dperks@ibm.net>
  12. * URB error messages from pwc driver by Nemosoft
  13. * generic_ioctl() code from videodev.c by Gerd Knorr and Alan Cox
  14. * Memory management (rvmalloc) code from bttv driver, by Gerd Knorr and others
  15. *
  16. * Based on the Linux CPiA driver written by Peter Pregler,
  17. * Scott J. Bertin and Johannes Erdfelt.
  18. *
  19. * Please see the file: Documentation/usb/ov511.txt
  20. * and the website at: http://alpha.dyndns.org/ov511
  21. * for more info.
  22. *
  23. * This program is free software; you can redistribute it and/or modify it
  24. * under the terms of the GNU General Public License as published by the
  25. * Free Software Foundation; either version 2 of the License, or (at your
  26. * option) any later version.
  27. *
  28. * This program is distributed in the hope that it will be useful, but
  29. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  30. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  31. * for more details.
  32. *
  33. * You should have received a copy of the GNU General Public License
  34. * along with this program; if not, write to the Free Software Foundation,
  35. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  36. */
  37. #include <linux/module.h>
  38. #include <linux/init.h>
  39. #include <linux/vmalloc.h>
  40. #include <linux/slab.h>
  41. #include <linux/ctype.h>
  42. #include <linux/pagemap.h>
  43. #include <asm/processor.h>
  44. #include <linux/mm.h>
  45. #include <linux/device.h>
  46. #if defined (__i386__)
  47. #include <asm/cpufeature.h>
  48. #endif
  49. #include "ov511.h"
  50. /*
  51. * Version Information
  52. */
  53. #define DRIVER_VERSION "v1.64 for Linux 2.5"
  54. #define EMAIL "mark@alpha.dyndns.org"
  55. #define DRIVER_AUTHOR "Mark McClelland <mark@alpha.dyndns.org> & Bret Wallach \
  56. & Orion Sky Lawlor <olawlor@acm.org> & Kevin Moore & Charl P. Botha \
  57. <cpbotha@ieee.org> & Claudio Matsuoka <claudio@conectiva.com>"
  58. #define DRIVER_DESC "ov511 USB Camera Driver"
  59. #define OV511_I2C_RETRIES 3
  60. #define ENABLE_Y_QUANTABLE 1
  61. #define ENABLE_UV_QUANTABLE 1
  62. #define OV511_MAX_UNIT_VIDEO 16
  63. /* Pixel count * bytes per YUV420 pixel (1.5) */
  64. #define MAX_FRAME_SIZE(w, h) ((w) * (h) * 3 / 2)
  65. #define MAX_DATA_SIZE(w, h) (MAX_FRAME_SIZE(w, h) + sizeof(struct timeval))
  66. /* Max size * bytes per YUV420 pixel (1.5) + one extra isoc frame for safety */
  67. #define MAX_RAW_DATA_SIZE(w, h) ((w) * (h) * 3 / 2 + 1024)
  68. #define FATAL_ERROR(rc) ((rc) < 0 && (rc) != -EPERM)
  69. /**********************************************************************
  70. * Module Parameters
  71. * (See ov511.txt for detailed descriptions of these)
  72. **********************************************************************/
  73. /* These variables (and all static globals) default to zero */
  74. static int autobright = 1;
  75. static int autogain = 1;
  76. static int autoexp = 1;
  77. static int debug;
  78. static int snapshot;
  79. static int cams = 1;
  80. static int compress;
  81. static int testpat;
  82. static int dumppix;
  83. static int led = 1;
  84. static int dump_bridge;
  85. static int dump_sensor;
  86. static int printph;
  87. static int phy = 0x1f;
  88. static int phuv = 0x05;
  89. static int pvy = 0x06;
  90. static int pvuv = 0x06;
  91. static int qhy = 0x14;
  92. static int qhuv = 0x03;
  93. static int qvy = 0x04;
  94. static int qvuv = 0x04;
  95. static int lightfreq;
  96. static int bandingfilter;
  97. static int clockdiv = -1;
  98. static int packetsize = -1;
  99. static int framedrop = -1;
  100. static int fastset;
  101. static int force_palette;
  102. static int backlight;
  103. /* Bitmask marking allocated devices from 0 to OV511_MAX_UNIT_VIDEO */
  104. static unsigned long ov511_devused;
  105. static int unit_video[OV511_MAX_UNIT_VIDEO];
  106. static int remove_zeros;
  107. static int mirror;
  108. static int ov518_color;
  109. module_param(autobright, int, 0);
  110. MODULE_PARM_DESC(autobright, "Sensor automatically changes brightness");
  111. module_param(autogain, int, 0);
  112. MODULE_PARM_DESC(autogain, "Sensor automatically changes gain");
  113. module_param(autoexp, int, 0);
  114. MODULE_PARM_DESC(autoexp, "Sensor automatically changes exposure");
  115. module_param(debug, int, 0);
  116. MODULE_PARM_DESC(debug,
  117. "Debug level: 0=none, 1=inits, 2=warning, 3=config, 4=functions, 5=max");
  118. module_param(snapshot, int, 0);
  119. MODULE_PARM_DESC(snapshot, "Enable snapshot mode");
  120. module_param(cams, int, 0);
  121. MODULE_PARM_DESC(cams, "Number of simultaneous cameras");
  122. module_param(compress, int, 0);
  123. MODULE_PARM_DESC(compress, "Turn on compression");
  124. module_param(testpat, int, 0);
  125. MODULE_PARM_DESC(testpat,
  126. "Replace image with vertical bar testpattern (only partially working)");
  127. module_param(dumppix, int, 0);
  128. MODULE_PARM_DESC(dumppix, "Dump raw pixel data");
  129. module_param(led, int, 0);
  130. MODULE_PARM_DESC(led,
  131. "LED policy (OV511+ or later). 0=off, 1=on (default), 2=auto (on when open)");
  132. module_param(dump_bridge, int, 0);
  133. MODULE_PARM_DESC(dump_bridge, "Dump the bridge registers");
  134. module_param(dump_sensor, int, 0);
  135. MODULE_PARM_DESC(dump_sensor, "Dump the sensor registers");
  136. module_param(printph, int, 0);
  137. MODULE_PARM_DESC(printph, "Print frame start/end headers");
  138. module_param(phy, int, 0);
  139. MODULE_PARM_DESC(phy, "Prediction range (horiz. Y)");
  140. module_param(phuv, int, 0);
  141. MODULE_PARM_DESC(phuv, "Prediction range (horiz. UV)");
  142. module_param(pvy, int, 0);
  143. MODULE_PARM_DESC(pvy, "Prediction range (vert. Y)");
  144. module_param(pvuv, int, 0);
  145. MODULE_PARM_DESC(pvuv, "Prediction range (vert. UV)");
  146. module_param(qhy, int, 0);
  147. MODULE_PARM_DESC(qhy, "Quantization threshold (horiz. Y)");
  148. module_param(qhuv, int, 0);
  149. MODULE_PARM_DESC(qhuv, "Quantization threshold (horiz. UV)");
  150. module_param(qvy, int, 0);
  151. MODULE_PARM_DESC(qvy, "Quantization threshold (vert. Y)");
  152. module_param(qvuv, int, 0);
  153. MODULE_PARM_DESC(qvuv, "Quantization threshold (vert. UV)");
  154. module_param(lightfreq, int, 0);
  155. MODULE_PARM_DESC(lightfreq,
  156. "Light frequency. Set to 50 or 60 Hz, or zero for default settings");
  157. module_param(bandingfilter, int, 0);
  158. MODULE_PARM_DESC(bandingfilter,
  159. "Enable banding filter (to reduce effects of fluorescent lighting)");
  160. module_param(clockdiv, int, 0);
  161. MODULE_PARM_DESC(clockdiv, "Force pixel clock divisor to a specific value");
  162. module_param(packetsize, int, 0);
  163. MODULE_PARM_DESC(packetsize, "Force a specific isoc packet size");
  164. module_param(framedrop, int, 0);
  165. MODULE_PARM_DESC(framedrop, "Force a specific frame drop register setting");
  166. module_param(fastset, int, 0);
  167. MODULE_PARM_DESC(fastset, "Allows picture settings to take effect immediately");
  168. module_param(force_palette, int, 0);
  169. MODULE_PARM_DESC(force_palette, "Force the palette to a specific value");
  170. module_param(backlight, int, 0);
  171. MODULE_PARM_DESC(backlight, "For objects that are lit from behind");
  172. static unsigned int num_uv;
  173. module_param_array(unit_video, int, &num_uv, 0);
  174. MODULE_PARM_DESC(unit_video,
  175. "Force use of specific minor number(s). 0 is not allowed.");
  176. module_param(remove_zeros, int, 0);
  177. MODULE_PARM_DESC(remove_zeros,
  178. "Remove zero-padding from uncompressed incoming data");
  179. module_param(mirror, int, 0);
  180. MODULE_PARM_DESC(mirror, "Reverse image horizontally");
  181. module_param(ov518_color, int, 0);
  182. MODULE_PARM_DESC(ov518_color, "Enable OV518 color (experimental)");
  183. MODULE_AUTHOR(DRIVER_AUTHOR);
  184. MODULE_DESCRIPTION(DRIVER_DESC);
  185. MODULE_LICENSE("GPL");
  186. /**********************************************************************
  187. * Miscellaneous Globals
  188. **********************************************************************/
  189. static struct usb_driver ov511_driver;
  190. /* Number of times to retry a failed I2C transaction. Increase this if you
  191. * are getting "Failed to read sensor ID..." */
  192. static const int i2c_detect_tries = 5;
  193. static struct usb_device_id device_table [] = {
  194. { USB_DEVICE(VEND_OMNIVISION, PROD_OV511) },
  195. { USB_DEVICE(VEND_OMNIVISION, PROD_OV511PLUS) },
  196. { USB_DEVICE(VEND_MATTEL, PROD_ME2CAM) },
  197. { } /* Terminating entry */
  198. };
  199. MODULE_DEVICE_TABLE (usb, device_table);
  200. static unsigned char yQuanTable511[] = OV511_YQUANTABLE;
  201. static unsigned char uvQuanTable511[] = OV511_UVQUANTABLE;
  202. static unsigned char yQuanTable518[] = OV518_YQUANTABLE;
  203. static unsigned char uvQuanTable518[] = OV518_UVQUANTABLE;
  204. /**********************************************************************
  205. * Symbolic Names
  206. **********************************************************************/
  207. /* Known OV511-based cameras */
  208. static struct symbolic_list camlist[] = {
  209. { 0, "Generic Camera (no ID)" },
  210. { 1, "Mustek WCam 3X" },
  211. { 3, "D-Link DSB-C300" },
  212. { 4, "Generic OV511/OV7610" },
  213. { 5, "Puretek PT-6007" },
  214. { 6, "Lifeview USB Life TV (NTSC)" },
  215. { 21, "Creative Labs WebCam 3" },
  216. { 22, "Lifeview USB Life TV (PAL D/K+B/G)" },
  217. { 36, "Koala-Cam" },
  218. { 38, "Lifeview USB Life TV (PAL)" },
  219. { 41, "Samsung Anycam MPC-M10" },
  220. { 43, "Mtekvision Zeca MV402" },
  221. { 46, "Suma eON" },
  222. { 70, "Lifeview USB Life TV (PAL/SECAM)" },
  223. { 100, "Lifeview RoboCam" },
  224. { 102, "AverMedia InterCam Elite" },
  225. { 112, "MediaForte MV300" }, /* or OV7110 evaluation kit */
  226. { 134, "Ezonics EZCam II" },
  227. { 192, "Webeye 2000B" },
  228. { 253, "Alpha Vision Tech. AlphaCam SE" },
  229. { -1, NULL }
  230. };
  231. /* Video4Linux1 Palettes */
  232. static struct symbolic_list v4l1_plist[] = {
  233. { VIDEO_PALETTE_GREY, "GREY" },
  234. { VIDEO_PALETTE_HI240, "HI240" },
  235. { VIDEO_PALETTE_RGB565, "RGB565" },
  236. { VIDEO_PALETTE_RGB24, "RGB24" },
  237. { VIDEO_PALETTE_RGB32, "RGB32" },
  238. { VIDEO_PALETTE_RGB555, "RGB555" },
  239. { VIDEO_PALETTE_YUV422, "YUV422" },
  240. { VIDEO_PALETTE_YUYV, "YUYV" },
  241. { VIDEO_PALETTE_UYVY, "UYVY" },
  242. { VIDEO_PALETTE_YUV420, "YUV420" },
  243. { VIDEO_PALETTE_YUV411, "YUV411" },
  244. { VIDEO_PALETTE_RAW, "RAW" },
  245. { VIDEO_PALETTE_YUV422P,"YUV422P" },
  246. { VIDEO_PALETTE_YUV411P,"YUV411P" },
  247. { VIDEO_PALETTE_YUV420P,"YUV420P" },
  248. { VIDEO_PALETTE_YUV410P,"YUV410P" },
  249. { -1, NULL }
  250. };
  251. static struct symbolic_list brglist[] = {
  252. { BRG_OV511, "OV511" },
  253. { BRG_OV511PLUS, "OV511+" },
  254. { BRG_OV518, "OV518" },
  255. { BRG_OV518PLUS, "OV518+" },
  256. { -1, NULL }
  257. };
  258. static struct symbolic_list senlist[] = {
  259. { SEN_OV76BE, "OV76BE" },
  260. { SEN_OV7610, "OV7610" },
  261. { SEN_OV7620, "OV7620" },
  262. { SEN_OV7620AE, "OV7620AE" },
  263. { SEN_OV6620, "OV6620" },
  264. { SEN_OV6630, "OV6630" },
  265. { SEN_OV6630AE, "OV6630AE" },
  266. { SEN_OV6630AF, "OV6630AF" },
  267. { SEN_OV8600, "OV8600" },
  268. { SEN_KS0127, "KS0127" },
  269. { SEN_KS0127B, "KS0127B" },
  270. { SEN_SAA7111A, "SAA7111A" },
  271. { -1, NULL }
  272. };
  273. /* URB error codes: */
  274. static struct symbolic_list urb_errlist[] = {
  275. { -ENOSR, "Buffer error (overrun)" },
  276. { -EPIPE, "Stalled (device not responding)" },
  277. { -EOVERFLOW, "Babble (device sends too much data)" },
  278. { -EPROTO, "Bit-stuff error (bad cable?)" },
  279. { -EILSEQ, "CRC/Timeout (bad cable?)" },
  280. { -ETIME, "Device does not respond to token" },
  281. { -ETIMEDOUT, "Device does not respond to command" },
  282. { -1, NULL }
  283. };
  284. /**********************************************************************
  285. * Memory management
  286. **********************************************************************/
  287. static void *
  288. rvmalloc(unsigned long size)
  289. {
  290. void *mem;
  291. unsigned long adr;
  292. size = PAGE_ALIGN(size);
  293. mem = vmalloc_32(size);
  294. if (!mem)
  295. return NULL;
  296. memset(mem, 0, size); /* Clear the ram out, no junk to the user */
  297. adr = (unsigned long) mem;
  298. while (size > 0) {
  299. SetPageReserved(vmalloc_to_page((void *)adr));
  300. adr += PAGE_SIZE;
  301. size -= PAGE_SIZE;
  302. }
  303. return mem;
  304. }
  305. static void
  306. rvfree(void *mem, unsigned long size)
  307. {
  308. unsigned long adr;
  309. if (!mem)
  310. return;
  311. adr = (unsigned long) mem;
  312. while ((long) size > 0) {
  313. ClearPageReserved(vmalloc_to_page((void *)adr));
  314. adr += PAGE_SIZE;
  315. size -= PAGE_SIZE;
  316. }
  317. vfree(mem);
  318. }
  319. /**********************************************************************
  320. *
  321. * Register I/O
  322. *
  323. **********************************************************************/
  324. /* Write an OV51x register */
  325. static int
  326. reg_w(struct usb_ov511 *ov, unsigned char reg, unsigned char value)
  327. {
  328. int rc;
  329. PDEBUG(5, "0x%02X:0x%02X", reg, value);
  330. mutex_lock(&ov->cbuf_lock);
  331. ov->cbuf[0] = value;
  332. rc = usb_control_msg(ov->dev,
  333. usb_sndctrlpipe(ov->dev, 0),
  334. (ov->bclass == BCL_OV518)?1:2 /* REG_IO */,
  335. USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  336. 0, (__u16)reg, &ov->cbuf[0], 1, 1000);
  337. mutex_unlock(&ov->cbuf_lock);
  338. if (rc < 0)
  339. err("reg write: error %d: %s", rc, symbolic(urb_errlist, rc));
  340. return rc;
  341. }
  342. /* Read from an OV51x register */
  343. /* returns: negative is error, pos or zero is data */
  344. static int
  345. reg_r(struct usb_ov511 *ov, unsigned char reg)
  346. {
  347. int rc;
  348. mutex_lock(&ov->cbuf_lock);
  349. rc = usb_control_msg(ov->dev,
  350. usb_rcvctrlpipe(ov->dev, 0),
  351. (ov->bclass == BCL_OV518)?1:3 /* REG_IO */,
  352. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  353. 0, (__u16)reg, &ov->cbuf[0], 1, 1000);
  354. if (rc < 0) {
  355. err("reg read: error %d: %s", rc, symbolic(urb_errlist, rc));
  356. } else {
  357. rc = ov->cbuf[0];
  358. PDEBUG(5, "0x%02X:0x%02X", reg, ov->cbuf[0]);
  359. }
  360. mutex_unlock(&ov->cbuf_lock);
  361. return rc;
  362. }
  363. /*
  364. * Writes bits at positions specified by mask to an OV51x reg. Bits that are in
  365. * the same position as 1's in "mask" are cleared and set to "value". Bits
  366. * that are in the same position as 0's in "mask" are preserved, regardless
  367. * of their respective state in "value".
  368. */
  369. static int
  370. reg_w_mask(struct usb_ov511 *ov,
  371. unsigned char reg,
  372. unsigned char value,
  373. unsigned char mask)
  374. {
  375. int ret;
  376. unsigned char oldval, newval;
  377. ret = reg_r(ov, reg);
  378. if (ret < 0)
  379. return ret;
  380. oldval = (unsigned char) ret;
  381. oldval &= (~mask); /* Clear the masked bits */
  382. value &= mask; /* Enforce mask on value */
  383. newval = oldval | value; /* Set the desired bits */
  384. return (reg_w(ov, reg, newval));
  385. }
  386. /*
  387. * Writes multiple (n) byte value to a single register. Only valid with certain
  388. * registers (0x30 and 0xc4 - 0xce).
  389. */
  390. static int
  391. ov518_reg_w32(struct usb_ov511 *ov, unsigned char reg, u32 val, int n)
  392. {
  393. int rc;
  394. PDEBUG(5, "0x%02X:%7d, n=%d", reg, val, n);
  395. mutex_lock(&ov->cbuf_lock);
  396. *((__le32 *)ov->cbuf) = __cpu_to_le32(val);
  397. rc = usb_control_msg(ov->dev,
  398. usb_sndctrlpipe(ov->dev, 0),
  399. 1 /* REG_IO */,
  400. USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  401. 0, (__u16)reg, ov->cbuf, n, 1000);
  402. mutex_unlock(&ov->cbuf_lock);
  403. if (rc < 0)
  404. err("reg write multiple: error %d: %s", rc,
  405. symbolic(urb_errlist, rc));
  406. return rc;
  407. }
  408. static int
  409. ov511_upload_quan_tables(struct usb_ov511 *ov)
  410. {
  411. unsigned char *pYTable = yQuanTable511;
  412. unsigned char *pUVTable = uvQuanTable511;
  413. unsigned char val0, val1;
  414. int i, rc, reg = R511_COMP_LUT_BEGIN;
  415. PDEBUG(4, "Uploading quantization tables");
  416. for (i = 0; i < OV511_QUANTABLESIZE / 2; i++) {
  417. if (ENABLE_Y_QUANTABLE) {
  418. val0 = *pYTable++;
  419. val1 = *pYTable++;
  420. val0 &= 0x0f;
  421. val1 &= 0x0f;
  422. val0 |= val1 << 4;
  423. rc = reg_w(ov, reg, val0);
  424. if (rc < 0)
  425. return rc;
  426. }
  427. if (ENABLE_UV_QUANTABLE) {
  428. val0 = *pUVTable++;
  429. val1 = *pUVTable++;
  430. val0 &= 0x0f;
  431. val1 &= 0x0f;
  432. val0 |= val1 << 4;
  433. rc = reg_w(ov, reg + OV511_QUANTABLESIZE/2, val0);
  434. if (rc < 0)
  435. return rc;
  436. }
  437. reg++;
  438. }
  439. return 0;
  440. }
  441. /* OV518 quantization tables are 8x4 (instead of 8x8) */
  442. static int
  443. ov518_upload_quan_tables(struct usb_ov511 *ov)
  444. {
  445. unsigned char *pYTable = yQuanTable518;
  446. unsigned char *pUVTable = uvQuanTable518;
  447. unsigned char val0, val1;
  448. int i, rc, reg = R511_COMP_LUT_BEGIN;
  449. PDEBUG(4, "Uploading quantization tables");
  450. for (i = 0; i < OV518_QUANTABLESIZE / 2; i++) {
  451. if (ENABLE_Y_QUANTABLE) {
  452. val0 = *pYTable++;
  453. val1 = *pYTable++;
  454. val0 &= 0x0f;
  455. val1 &= 0x0f;
  456. val0 |= val1 << 4;
  457. rc = reg_w(ov, reg, val0);
  458. if (rc < 0)
  459. return rc;
  460. }
  461. if (ENABLE_UV_QUANTABLE) {
  462. val0 = *pUVTable++;
  463. val1 = *pUVTable++;
  464. val0 &= 0x0f;
  465. val1 &= 0x0f;
  466. val0 |= val1 << 4;
  467. rc = reg_w(ov, reg + OV518_QUANTABLESIZE/2, val0);
  468. if (rc < 0)
  469. return rc;
  470. }
  471. reg++;
  472. }
  473. return 0;
  474. }
  475. static int
  476. ov51x_reset(struct usb_ov511 *ov, unsigned char reset_type)
  477. {
  478. int rc;
  479. /* Setting bit 0 not allowed on 518/518Plus */
  480. if (ov->bclass == BCL_OV518)
  481. reset_type &= 0xfe;
  482. PDEBUG(4, "Reset: type=0x%02X", reset_type);
  483. rc = reg_w(ov, R51x_SYS_RESET, reset_type);
  484. rc = reg_w(ov, R51x_SYS_RESET, 0);
  485. if (rc < 0)
  486. err("reset: command failed");
  487. return rc;
  488. }
  489. /**********************************************************************
  490. *
  491. * Low-level I2C I/O functions
  492. *
  493. **********************************************************************/
  494. /* NOTE: Do not call this function directly!
  495. * The OV518 I2C I/O procedure is different, hence, this function.
  496. * This is normally only called from i2c_w(). Note that this function
  497. * always succeeds regardless of whether the sensor is present and working.
  498. */
  499. static int
  500. ov518_i2c_write_internal(struct usb_ov511 *ov,
  501. unsigned char reg,
  502. unsigned char value)
  503. {
  504. int rc;
  505. PDEBUG(5, "0x%02X:0x%02X", reg, value);
  506. /* Select camera register */
  507. rc = reg_w(ov, R51x_I2C_SADDR_3, reg);
  508. if (rc < 0)
  509. return rc;
  510. /* Write "value" to I2C data port of OV511 */
  511. rc = reg_w(ov, R51x_I2C_DATA, value);
  512. if (rc < 0)
  513. return rc;
  514. /* Initiate 3-byte write cycle */
  515. rc = reg_w(ov, R518_I2C_CTL, 0x01);
  516. if (rc < 0)
  517. return rc;
  518. return 0;
  519. }
  520. /* NOTE: Do not call this function directly! */
  521. static int
  522. ov511_i2c_write_internal(struct usb_ov511 *ov,
  523. unsigned char reg,
  524. unsigned char value)
  525. {
  526. int rc, retries;
  527. PDEBUG(5, "0x%02X:0x%02X", reg, value);
  528. /* Three byte write cycle */
  529. for (retries = OV511_I2C_RETRIES; ; ) {
  530. /* Select camera register */
  531. rc = reg_w(ov, R51x_I2C_SADDR_3, reg);
  532. if (rc < 0)
  533. break;
  534. /* Write "value" to I2C data port of OV511 */
  535. rc = reg_w(ov, R51x_I2C_DATA, value);
  536. if (rc < 0)
  537. break;
  538. /* Initiate 3-byte write cycle */
  539. rc = reg_w(ov, R511_I2C_CTL, 0x01);
  540. if (rc < 0)
  541. break;
  542. /* Retry until idle */
  543. do {
  544. rc = reg_r(ov, R511_I2C_CTL);
  545. } while (rc > 0 && ((rc&1) == 0));
  546. if (rc < 0)
  547. break;
  548. /* Ack? */
  549. if ((rc&2) == 0) {
  550. rc = 0;
  551. break;
  552. }
  553. #if 0
  554. /* I2C abort */
  555. reg_w(ov, R511_I2C_CTL, 0x10);
  556. #endif
  557. if (--retries < 0) {
  558. err("i2c write retries exhausted");
  559. rc = -1;
  560. break;
  561. }
  562. }
  563. return rc;
  564. }
  565. /* NOTE: Do not call this function directly!
  566. * The OV518 I2C I/O procedure is different, hence, this function.
  567. * This is normally only called from i2c_r(). Note that this function
  568. * always succeeds regardless of whether the sensor is present and working.
  569. */
  570. static int
  571. ov518_i2c_read_internal(struct usb_ov511 *ov, unsigned char reg)
  572. {
  573. int rc, value;
  574. /* Select camera register */
  575. rc = reg_w(ov, R51x_I2C_SADDR_2, reg);
  576. if (rc < 0)
  577. return rc;
  578. /* Initiate 2-byte write cycle */
  579. rc = reg_w(ov, R518_I2C_CTL, 0x03);
  580. if (rc < 0)
  581. return rc;
  582. /* Initiate 2-byte read cycle */
  583. rc = reg_w(ov, R518_I2C_CTL, 0x05);
  584. if (rc < 0)
  585. return rc;
  586. value = reg_r(ov, R51x_I2C_DATA);
  587. PDEBUG(5, "0x%02X:0x%02X", reg, value);
  588. return value;
  589. }
  590. /* NOTE: Do not call this function directly!
  591. * returns: negative is error, pos or zero is data */
  592. static int
  593. ov511_i2c_read_internal(struct usb_ov511 *ov, unsigned char reg)
  594. {
  595. int rc, value, retries;
  596. /* Two byte write cycle */
  597. for (retries = OV511_I2C_RETRIES; ; ) {
  598. /* Select camera register */
  599. rc = reg_w(ov, R51x_I2C_SADDR_2, reg);
  600. if (rc < 0)
  601. return rc;
  602. /* Initiate 2-byte write cycle */
  603. rc = reg_w(ov, R511_I2C_CTL, 0x03);
  604. if (rc < 0)
  605. return rc;
  606. /* Retry until idle */
  607. do {
  608. rc = reg_r(ov, R511_I2C_CTL);
  609. } while (rc > 0 && ((rc & 1) == 0));
  610. if (rc < 0)
  611. return rc;
  612. if ((rc&2) == 0) /* Ack? */
  613. break;
  614. /* I2C abort */
  615. reg_w(ov, R511_I2C_CTL, 0x10);
  616. if (--retries < 0) {
  617. err("i2c write retries exhausted");
  618. return -1;
  619. }
  620. }
  621. /* Two byte read cycle */
  622. for (retries = OV511_I2C_RETRIES; ; ) {
  623. /* Initiate 2-byte read cycle */
  624. rc = reg_w(ov, R511_I2C_CTL, 0x05);
  625. if (rc < 0)
  626. return rc;
  627. /* Retry until idle */
  628. do {
  629. rc = reg_r(ov, R511_I2C_CTL);
  630. } while (rc > 0 && ((rc&1) == 0));
  631. if (rc < 0)
  632. return rc;
  633. if ((rc&2) == 0) /* Ack? */
  634. break;
  635. /* I2C abort */
  636. rc = reg_w(ov, R511_I2C_CTL, 0x10);
  637. if (rc < 0)
  638. return rc;
  639. if (--retries < 0) {
  640. err("i2c read retries exhausted");
  641. return -1;
  642. }
  643. }
  644. value = reg_r(ov, R51x_I2C_DATA);
  645. PDEBUG(5, "0x%02X:0x%02X", reg, value);
  646. /* This is needed to make i2c_w() work */
  647. rc = reg_w(ov, R511_I2C_CTL, 0x05);
  648. if (rc < 0)
  649. return rc;
  650. return value;
  651. }
  652. /* returns: negative is error, pos or zero is data */
  653. static int
  654. i2c_r(struct usb_ov511 *ov, unsigned char reg)
  655. {
  656. int rc;
  657. mutex_lock(&ov->i2c_lock);
  658. if (ov->bclass == BCL_OV518)
  659. rc = ov518_i2c_read_internal(ov, reg);
  660. else
  661. rc = ov511_i2c_read_internal(ov, reg);
  662. mutex_unlock(&ov->i2c_lock);
  663. return rc;
  664. }
  665. static int
  666. i2c_w(struct usb_ov511 *ov, unsigned char reg, unsigned char value)
  667. {
  668. int rc;
  669. mutex_lock(&ov->i2c_lock);
  670. if (ov->bclass == BCL_OV518)
  671. rc = ov518_i2c_write_internal(ov, reg, value);
  672. else
  673. rc = ov511_i2c_write_internal(ov, reg, value);
  674. mutex_unlock(&ov->i2c_lock);
  675. return rc;
  676. }
  677. /* Do not call this function directly! */
  678. static int
  679. ov51x_i2c_write_mask_internal(struct usb_ov511 *ov,
  680. unsigned char reg,
  681. unsigned char value,
  682. unsigned char mask)
  683. {
  684. int rc;
  685. unsigned char oldval, newval;
  686. if (mask == 0xff) {
  687. newval = value;
  688. } else {
  689. if (ov->bclass == BCL_OV518)
  690. rc = ov518_i2c_read_internal(ov, reg);
  691. else
  692. rc = ov511_i2c_read_internal(ov, reg);
  693. if (rc < 0)
  694. return rc;
  695. oldval = (unsigned char) rc;
  696. oldval &= (~mask); /* Clear the masked bits */
  697. value &= mask; /* Enforce mask on value */
  698. newval = oldval | value; /* Set the desired bits */
  699. }
  700. if (ov->bclass == BCL_OV518)
  701. return (ov518_i2c_write_internal(ov, reg, newval));
  702. else
  703. return (ov511_i2c_write_internal(ov, reg, newval));
  704. }
  705. /* Writes bits at positions specified by mask to an I2C reg. Bits that are in
  706. * the same position as 1's in "mask" are cleared and set to "value". Bits
  707. * that are in the same position as 0's in "mask" are preserved, regardless
  708. * of their respective state in "value".
  709. */
  710. static int
  711. i2c_w_mask(struct usb_ov511 *ov,
  712. unsigned char reg,
  713. unsigned char value,
  714. unsigned char mask)
  715. {
  716. int rc;
  717. mutex_lock(&ov->i2c_lock);
  718. rc = ov51x_i2c_write_mask_internal(ov, reg, value, mask);
  719. mutex_unlock(&ov->i2c_lock);
  720. return rc;
  721. }
  722. /* Set the read and write slave IDs. The "slave" argument is the write slave,
  723. * and the read slave will be set to (slave + 1). ov->i2c_lock should be held
  724. * when calling this. This should not be called from outside the i2c I/O
  725. * functions.
  726. */
  727. static int
  728. i2c_set_slave_internal(struct usb_ov511 *ov, unsigned char slave)
  729. {
  730. int rc;
  731. rc = reg_w(ov, R51x_I2C_W_SID, slave);
  732. if (rc < 0)
  733. return rc;
  734. rc = reg_w(ov, R51x_I2C_R_SID, slave + 1);
  735. if (rc < 0)
  736. return rc;
  737. return 0;
  738. }
  739. /* Write to a specific I2C slave ID and register, using the specified mask */
  740. static int
  741. i2c_w_slave(struct usb_ov511 *ov,
  742. unsigned char slave,
  743. unsigned char reg,
  744. unsigned char value,
  745. unsigned char mask)
  746. {
  747. int rc = 0;
  748. mutex_lock(&ov->i2c_lock);
  749. /* Set new slave IDs */
  750. rc = i2c_set_slave_internal(ov, slave);
  751. if (rc < 0)
  752. goto out;
  753. rc = ov51x_i2c_write_mask_internal(ov, reg, value, mask);
  754. out:
  755. /* Restore primary IDs */
  756. if (i2c_set_slave_internal(ov, ov->primary_i2c_slave) < 0)
  757. err("Couldn't restore primary I2C slave");
  758. mutex_unlock(&ov->i2c_lock);
  759. return rc;
  760. }
  761. /* Read from a specific I2C slave ID and register */
  762. static int
  763. i2c_r_slave(struct usb_ov511 *ov,
  764. unsigned char slave,
  765. unsigned char reg)
  766. {
  767. int rc;
  768. mutex_lock(&ov->i2c_lock);
  769. /* Set new slave IDs */
  770. rc = i2c_set_slave_internal(ov, slave);
  771. if (rc < 0)
  772. goto out;
  773. if (ov->bclass == BCL_OV518)
  774. rc = ov518_i2c_read_internal(ov, reg);
  775. else
  776. rc = ov511_i2c_read_internal(ov, reg);
  777. out:
  778. /* Restore primary IDs */
  779. if (i2c_set_slave_internal(ov, ov->primary_i2c_slave) < 0)
  780. err("Couldn't restore primary I2C slave");
  781. mutex_unlock(&ov->i2c_lock);
  782. return rc;
  783. }
  784. /* Sets I2C read and write slave IDs. Returns <0 for error */
  785. static int
  786. ov51x_set_slave_ids(struct usb_ov511 *ov, unsigned char sid)
  787. {
  788. int rc;
  789. mutex_lock(&ov->i2c_lock);
  790. rc = i2c_set_slave_internal(ov, sid);
  791. if (rc < 0)
  792. goto out;
  793. // FIXME: Is this actually necessary?
  794. rc = ov51x_reset(ov, OV511_RESET_NOREGS);
  795. out:
  796. mutex_unlock(&ov->i2c_lock);
  797. return rc;
  798. }
  799. static int
  800. write_regvals(struct usb_ov511 *ov, struct ov511_regvals * pRegvals)
  801. {
  802. int rc;
  803. while (pRegvals->bus != OV511_DONE_BUS) {
  804. if (pRegvals->bus == OV511_REG_BUS) {
  805. if ((rc = reg_w(ov, pRegvals->reg, pRegvals->val)) < 0)
  806. return rc;
  807. } else if (pRegvals->bus == OV511_I2C_BUS) {
  808. if ((rc = i2c_w(ov, pRegvals->reg, pRegvals->val)) < 0)
  809. return rc;
  810. } else {
  811. err("Bad regval array");
  812. return -1;
  813. }
  814. pRegvals++;
  815. }
  816. return 0;
  817. }
  818. #ifdef OV511_DEBUG
  819. static void
  820. dump_i2c_range(struct usb_ov511 *ov, int reg1, int regn)
  821. {
  822. int i, rc;
  823. for (i = reg1; i <= regn; i++) {
  824. rc = i2c_r(ov, i);
  825. dev_info(&ov->dev->dev, "Sensor[0x%02X] = 0x%02X\n", i, rc);
  826. }
  827. }
  828. static void
  829. dump_i2c_regs(struct usb_ov511 *ov)
  830. {
  831. dev_info(&ov->dev->dev, "I2C REGS\n");
  832. dump_i2c_range(ov, 0x00, 0x7C);
  833. }
  834. static void
  835. dump_reg_range(struct usb_ov511 *ov, int reg1, int regn)
  836. {
  837. int i, rc;
  838. for (i = reg1; i <= regn; i++) {
  839. rc = reg_r(ov, i);
  840. dev_info(&ov->dev->dev, "OV511[0x%02X] = 0x%02X\n", i, rc);
  841. }
  842. }
  843. static void
  844. ov511_dump_regs(struct usb_ov511 *ov)
  845. {
  846. dev_info(&ov->dev->dev, "CAMERA INTERFACE REGS\n");
  847. dump_reg_range(ov, 0x10, 0x1f);
  848. dev_info(&ov->dev->dev, "DRAM INTERFACE REGS\n");
  849. dump_reg_range(ov, 0x20, 0x23);
  850. dev_info(&ov->dev->dev, "ISO FIFO REGS\n");
  851. dump_reg_range(ov, 0x30, 0x31);
  852. dev_info(&ov->dev->dev, "PIO REGS\n");
  853. dump_reg_range(ov, 0x38, 0x39);
  854. dump_reg_range(ov, 0x3e, 0x3e);
  855. dev_info(&ov->dev->dev, "I2C REGS\n");
  856. dump_reg_range(ov, 0x40, 0x49);
  857. dev_info(&ov->dev->dev, "SYSTEM CONTROL REGS\n");
  858. dump_reg_range(ov, 0x50, 0x55);
  859. dump_reg_range(ov, 0x5e, 0x5f);
  860. dev_info(&ov->dev->dev, "OmniCE REGS\n");
  861. dump_reg_range(ov, 0x70, 0x79);
  862. /* NOTE: Quantization tables are not readable. You will get the value
  863. * in reg. 0x79 for every table register */
  864. dump_reg_range(ov, 0x80, 0x9f);
  865. dump_reg_range(ov, 0xa0, 0xbf);
  866. }
  867. static void
  868. ov518_dump_regs(struct usb_ov511 *ov)
  869. {
  870. dev_info(&ov->dev->dev, "VIDEO MODE REGS\n");
  871. dump_reg_range(ov, 0x20, 0x2f);
  872. dev_info(&ov->dev->dev, "DATA PUMP AND SNAPSHOT REGS\n");
  873. dump_reg_range(ov, 0x30, 0x3f);
  874. dev_info(&ov->dev->dev, "I2C REGS\n");
  875. dump_reg_range(ov, 0x40, 0x4f);
  876. dev_info(&ov->dev->dev, "SYSTEM CONTROL AND VENDOR REGS\n");
  877. dump_reg_range(ov, 0x50, 0x5f);
  878. dev_info(&ov->dev->dev, "60 - 6F\n");
  879. dump_reg_range(ov, 0x60, 0x6f);
  880. dev_info(&ov->dev->dev, "70 - 7F\n");
  881. dump_reg_range(ov, 0x70, 0x7f);
  882. dev_info(&ov->dev->dev, "Y QUANTIZATION TABLE\n");
  883. dump_reg_range(ov, 0x80, 0x8f);
  884. dev_info(&ov->dev->dev, "UV QUANTIZATION TABLE\n");
  885. dump_reg_range(ov, 0x90, 0x9f);
  886. dev_info(&ov->dev->dev, "A0 - BF\n");
  887. dump_reg_range(ov, 0xa0, 0xbf);
  888. dev_info(&ov->dev->dev, "CBR\n");
  889. dump_reg_range(ov, 0xc0, 0xcf);
  890. }
  891. #endif
  892. /*****************************************************************************/
  893. /* Temporarily stops OV511 from functioning. Must do this before changing
  894. * registers while the camera is streaming */
  895. static inline int
  896. ov51x_stop(struct usb_ov511 *ov)
  897. {
  898. PDEBUG(4, "stopping");
  899. ov->stopped = 1;
  900. if (ov->bclass == BCL_OV518)
  901. return (reg_w_mask(ov, R51x_SYS_RESET, 0x3a, 0x3a));
  902. else
  903. return (reg_w(ov, R51x_SYS_RESET, 0x3d));
  904. }
  905. /* Restarts OV511 after ov511_stop() is called. Has no effect if it is not
  906. * actually stopped (for performance). */
  907. static inline int
  908. ov51x_restart(struct usb_ov511 *ov)
  909. {
  910. if (ov->stopped) {
  911. PDEBUG(4, "restarting");
  912. ov->stopped = 0;
  913. /* Reinitialize the stream */
  914. if (ov->bclass == BCL_OV518)
  915. reg_w(ov, 0x2f, 0x80);
  916. return (reg_w(ov, R51x_SYS_RESET, 0x00));
  917. }
  918. return 0;
  919. }
  920. /* Sleeps until no frames are active. Returns !0 if got signal */
  921. static int
  922. ov51x_wait_frames_inactive(struct usb_ov511 *ov)
  923. {
  924. return wait_event_interruptible(ov->wq, ov->curframe < 0);
  925. }
  926. /* Resets the hardware snapshot button */
  927. static void
  928. ov51x_clear_snapshot(struct usb_ov511 *ov)
  929. {
  930. if (ov->bclass == BCL_OV511) {
  931. reg_w(ov, R51x_SYS_SNAP, 0x00);
  932. reg_w(ov, R51x_SYS_SNAP, 0x02);
  933. reg_w(ov, R51x_SYS_SNAP, 0x00);
  934. } else if (ov->bclass == BCL_OV518) {
  935. dev_warn(&ov->dev->dev,
  936. "snapshot reset not supported yet on OV518(+)\n");
  937. } else {
  938. dev_err(&ov->dev->dev, "clear snap: invalid bridge type\n");
  939. }
  940. }
  941. #if 0
  942. /* Checks the status of the snapshot button. Returns 1 if it was pressed since
  943. * it was last cleared, and zero in all other cases (including errors) */
  944. static int
  945. ov51x_check_snapshot(struct usb_ov511 *ov)
  946. {
  947. int ret, status = 0;
  948. if (ov->bclass == BCL_OV511) {
  949. ret = reg_r(ov, R51x_SYS_SNAP);
  950. if (ret < 0) {
  951. dev_err(&ov->dev->dev,
  952. "Error checking snspshot status (%d)\n", ret);
  953. } else if (ret & 0x08) {
  954. status = 1;
  955. }
  956. } else if (ov->bclass == BCL_OV518) {
  957. dev_warn(&ov->dev->dev,
  958. "snapshot check not supported yet on OV518(+)\n");
  959. } else {
  960. dev_err(&ov->dev->dev, "clear snap: invalid bridge type\n");
  961. }
  962. return status;
  963. }
  964. #endif
  965. /* This does an initial reset of an OmniVision sensor and ensures that I2C
  966. * is synchronized. Returns <0 for failure.
  967. */
  968. static int
  969. init_ov_sensor(struct usb_ov511 *ov)
  970. {
  971. int i, success;
  972. /* Reset the sensor */
  973. if (i2c_w(ov, 0x12, 0x80) < 0)
  974. return -EIO;
  975. /* Wait for it to initialize */
  976. msleep(150);
  977. for (i = 0, success = 0; i < i2c_detect_tries && !success; i++) {
  978. if ((i2c_r(ov, OV7610_REG_ID_HIGH) == 0x7F) &&
  979. (i2c_r(ov, OV7610_REG_ID_LOW) == 0xA2)) {
  980. success = 1;
  981. continue;
  982. }
  983. /* Reset the sensor */
  984. if (i2c_w(ov, 0x12, 0x80) < 0)
  985. return -EIO;
  986. /* Wait for it to initialize */
  987. msleep(150);
  988. /* Dummy read to sync I2C */
  989. if (i2c_r(ov, 0x00) < 0)
  990. return -EIO;
  991. }
  992. if (!success)
  993. return -EIO;
  994. PDEBUG(1, "I2C synced in %d attempt(s)", i);
  995. return 0;
  996. }
  997. static int
  998. ov511_set_packet_size(struct usb_ov511 *ov, int size)
  999. {
  1000. int alt, mult;
  1001. if (ov51x_stop(ov) < 0)
  1002. return -EIO;
  1003. mult = size >> 5;
  1004. if (ov->bridge == BRG_OV511) {
  1005. if (size == 0)
  1006. alt = OV511_ALT_SIZE_0;
  1007. else if (size == 257)
  1008. alt = OV511_ALT_SIZE_257;
  1009. else if (size == 513)
  1010. alt = OV511_ALT_SIZE_513;
  1011. else if (size == 769)
  1012. alt = OV511_ALT_SIZE_769;
  1013. else if (size == 993)
  1014. alt = OV511_ALT_SIZE_993;
  1015. else {
  1016. err("Set packet size: invalid size (%d)", size);
  1017. return -EINVAL;
  1018. }
  1019. } else if (ov->bridge == BRG_OV511PLUS) {
  1020. if (size == 0)
  1021. alt = OV511PLUS_ALT_SIZE_0;
  1022. else if (size == 33)
  1023. alt = OV511PLUS_ALT_SIZE_33;
  1024. else if (size == 129)
  1025. alt = OV511PLUS_ALT_SIZE_129;
  1026. else if (size == 257)
  1027. alt = OV511PLUS_ALT_SIZE_257;
  1028. else if (size == 385)
  1029. alt = OV511PLUS_ALT_SIZE_385;
  1030. else if (size == 513)
  1031. alt = OV511PLUS_ALT_SIZE_513;
  1032. else if (size == 769)
  1033. alt = OV511PLUS_ALT_SIZE_769;
  1034. else if (size == 961)
  1035. alt = OV511PLUS_ALT_SIZE_961;
  1036. else {
  1037. err("Set packet size: invalid size (%d)", size);
  1038. return -EINVAL;
  1039. }
  1040. } else {
  1041. err("Set packet size: Invalid bridge type");
  1042. return -EINVAL;
  1043. }
  1044. PDEBUG(3, "%d, mult=%d, alt=%d", size, mult, alt);
  1045. if (reg_w(ov, R51x_FIFO_PSIZE, mult) < 0)
  1046. return -EIO;
  1047. if (usb_set_interface(ov->dev, ov->iface, alt) < 0) {
  1048. err("Set packet size: set interface error");
  1049. return -EBUSY;
  1050. }
  1051. if (ov51x_reset(ov, OV511_RESET_NOREGS) < 0)
  1052. return -EIO;
  1053. ov->packet_size = size;
  1054. if (ov51x_restart(ov) < 0)
  1055. return -EIO;
  1056. return 0;
  1057. }
  1058. /* Note: Unlike the OV511/OV511+, the size argument does NOT include the
  1059. * optional packet number byte. The actual size *is* stored in ov->packet_size,
  1060. * though. */
  1061. static int
  1062. ov518_set_packet_size(struct usb_ov511 *ov, int size)
  1063. {
  1064. int alt;
  1065. if (ov51x_stop(ov) < 0)
  1066. return -EIO;
  1067. if (ov->bclass == BCL_OV518) {
  1068. if (size == 0)
  1069. alt = OV518_ALT_SIZE_0;
  1070. else if (size == 128)
  1071. alt = OV518_ALT_SIZE_128;
  1072. else if (size == 256)
  1073. alt = OV518_ALT_SIZE_256;
  1074. else if (size == 384)
  1075. alt = OV518_ALT_SIZE_384;
  1076. else if (size == 512)
  1077. alt = OV518_ALT_SIZE_512;
  1078. else if (size == 640)
  1079. alt = OV518_ALT_SIZE_640;
  1080. else if (size == 768)
  1081. alt = OV518_ALT_SIZE_768;
  1082. else if (size == 896)
  1083. alt = OV518_ALT_SIZE_896;
  1084. else {
  1085. err("Set packet size: invalid size (%d)", size);
  1086. return -EINVAL;
  1087. }
  1088. } else {
  1089. err("Set packet size: Invalid bridge type");
  1090. return -EINVAL;
  1091. }
  1092. PDEBUG(3, "%d, alt=%d", size, alt);
  1093. ov->packet_size = size;
  1094. if (size > 0) {
  1095. /* Program ISO FIFO size reg (packet number isn't included) */
  1096. ov518_reg_w32(ov, 0x30, size, 2);
  1097. if (ov->packet_numbering)
  1098. ++ov->packet_size;
  1099. }
  1100. if (usb_set_interface(ov->dev, ov->iface, alt) < 0) {
  1101. err("Set packet size: set interface error");
  1102. return -EBUSY;
  1103. }
  1104. /* Initialize the stream */
  1105. if (reg_w(ov, 0x2f, 0x80) < 0)
  1106. return -EIO;
  1107. if (ov51x_restart(ov) < 0)
  1108. return -EIO;
  1109. if (ov51x_reset(ov, OV511_RESET_NOREGS) < 0)
  1110. return -EIO;
  1111. return 0;
  1112. }
  1113. /* Upload compression params and quantization tables. Returns 0 for success. */
  1114. static int
  1115. ov511_init_compression(struct usb_ov511 *ov)
  1116. {
  1117. int rc = 0;
  1118. if (!ov->compress_inited) {
  1119. reg_w(ov, 0x70, phy);
  1120. reg_w(ov, 0x71, phuv);
  1121. reg_w(ov, 0x72, pvy);
  1122. reg_w(ov, 0x73, pvuv);
  1123. reg_w(ov, 0x74, qhy);
  1124. reg_w(ov, 0x75, qhuv);
  1125. reg_w(ov, 0x76, qvy);
  1126. reg_w(ov, 0x77, qvuv);
  1127. if (ov511_upload_quan_tables(ov) < 0) {
  1128. err("Error uploading quantization tables");
  1129. rc = -EIO;
  1130. goto out;
  1131. }
  1132. }
  1133. ov->compress_inited = 1;
  1134. out:
  1135. return rc;
  1136. }
  1137. /* Upload compression params and quantization tables. Returns 0 for success. */
  1138. static int
  1139. ov518_init_compression(struct usb_ov511 *ov)
  1140. {
  1141. int rc = 0;
  1142. if (!ov->compress_inited) {
  1143. if (ov518_upload_quan_tables(ov) < 0) {
  1144. err("Error uploading quantization tables");
  1145. rc = -EIO;
  1146. goto out;
  1147. }
  1148. }
  1149. ov->compress_inited = 1;
  1150. out:
  1151. return rc;
  1152. }
  1153. /* -------------------------------------------------------------------------- */
  1154. /* Sets sensor's contrast setting to "val" */
  1155. static int
  1156. sensor_set_contrast(struct usb_ov511 *ov, unsigned short val)
  1157. {
  1158. int rc;
  1159. PDEBUG(3, "%d", val);
  1160. if (ov->stop_during_set)
  1161. if (ov51x_stop(ov) < 0)
  1162. return -EIO;
  1163. switch (ov->sensor) {
  1164. case SEN_OV7610:
  1165. case SEN_OV6620:
  1166. {
  1167. rc = i2c_w(ov, OV7610_REG_CNT, val >> 8);
  1168. if (rc < 0)
  1169. goto out;
  1170. break;
  1171. }
  1172. case SEN_OV6630:
  1173. {
  1174. rc = i2c_w_mask(ov, OV7610_REG_CNT, val >> 12, 0x0f);
  1175. if (rc < 0)
  1176. goto out;
  1177. break;
  1178. }
  1179. case SEN_OV7620:
  1180. {
  1181. unsigned char ctab[] = {
  1182. 0x01, 0x05, 0x09, 0x11, 0x15, 0x35, 0x37, 0x57,
  1183. 0x5b, 0xa5, 0xa7, 0xc7, 0xc9, 0xcf, 0xef, 0xff
  1184. };
  1185. /* Use Y gamma control instead. Bit 0 enables it. */
  1186. rc = i2c_w(ov, 0x64, ctab[val>>12]);
  1187. if (rc < 0)
  1188. goto out;
  1189. break;
  1190. }
  1191. case SEN_SAA7111A:
  1192. {
  1193. rc = i2c_w(ov, 0x0b, val >> 9);
  1194. if (rc < 0)
  1195. goto out;
  1196. break;
  1197. }
  1198. default:
  1199. {
  1200. PDEBUG(3, "Unsupported with this sensor");
  1201. rc = -EPERM;
  1202. goto out;
  1203. }
  1204. }
  1205. rc = 0; /* Success */
  1206. ov->contrast = val;
  1207. out:
  1208. if (ov51x_restart(ov) < 0)
  1209. return -EIO;
  1210. return rc;
  1211. }
  1212. /* Gets sensor's contrast setting */
  1213. static int
  1214. sensor_get_contrast(struct usb_ov511 *ov, unsigned short *val)
  1215. {
  1216. int rc;
  1217. switch (ov->sensor) {
  1218. case SEN_OV7610:
  1219. case SEN_OV6620:
  1220. rc = i2c_r(ov, OV7610_REG_CNT);
  1221. if (rc < 0)
  1222. return rc;
  1223. else
  1224. *val = rc << 8;
  1225. break;
  1226. case SEN_OV6630:
  1227. rc = i2c_r(ov, OV7610_REG_CNT);
  1228. if (rc < 0)
  1229. return rc;
  1230. else
  1231. *val = rc << 12;
  1232. break;
  1233. case SEN_OV7620:
  1234. /* Use Y gamma reg instead. Bit 0 is the enable bit. */
  1235. rc = i2c_r(ov, 0x64);
  1236. if (rc < 0)
  1237. return rc;
  1238. else
  1239. *val = (rc & 0xfe) << 8;
  1240. break;
  1241. case SEN_SAA7111A:
  1242. *val = ov->contrast;
  1243. break;
  1244. default:
  1245. PDEBUG(3, "Unsupported with this sensor");
  1246. return -EPERM;
  1247. }
  1248. PDEBUG(3, "%d", *val);
  1249. ov->contrast = *val;
  1250. return 0;
  1251. }
  1252. /* -------------------------------------------------------------------------- */
  1253. /* Sets sensor's brightness setting to "val" */
  1254. static int
  1255. sensor_set_brightness(struct usb_ov511 *ov, unsigned short val)
  1256. {
  1257. int rc;
  1258. PDEBUG(4, "%d", val);
  1259. if (ov->stop_during_set)
  1260. if (ov51x_stop(ov) < 0)
  1261. return -EIO;
  1262. switch (ov->sensor) {
  1263. case SEN_OV7610:
  1264. case SEN_OV76BE:
  1265. case SEN_OV6620:
  1266. case SEN_OV6630:
  1267. rc = i2c_w(ov, OV7610_REG_BRT, val >> 8);
  1268. if (rc < 0)
  1269. goto out;
  1270. break;
  1271. case SEN_OV7620:
  1272. /* 7620 doesn't like manual changes when in auto mode */
  1273. if (!ov->auto_brt) {
  1274. rc = i2c_w(ov, OV7610_REG_BRT, val >> 8);
  1275. if (rc < 0)
  1276. goto out;
  1277. }
  1278. break;
  1279. case SEN_SAA7111A:
  1280. rc = i2c_w(ov, 0x0a, val >> 8);
  1281. if (rc < 0)
  1282. goto out;
  1283. break;
  1284. default:
  1285. PDEBUG(3, "Unsupported with this sensor");
  1286. rc = -EPERM;
  1287. goto out;
  1288. }
  1289. rc = 0; /* Success */
  1290. ov->brightness = val;
  1291. out:
  1292. if (ov51x_restart(ov) < 0)
  1293. return -EIO;
  1294. return rc;
  1295. }
  1296. /* Gets sensor's brightness setting */
  1297. static int
  1298. sensor_get_brightness(struct usb_ov511 *ov, unsigned short *val)
  1299. {
  1300. int rc;
  1301. switch (ov->sensor) {
  1302. case SEN_OV7610:
  1303. case SEN_OV76BE:
  1304. case SEN_OV7620:
  1305. case SEN_OV6620:
  1306. case SEN_OV6630:
  1307. rc = i2c_r(ov, OV7610_REG_BRT);
  1308. if (rc < 0)
  1309. return rc;
  1310. else
  1311. *val = rc << 8;
  1312. break;
  1313. case SEN_SAA7111A:
  1314. *val = ov->brightness;
  1315. break;
  1316. default:
  1317. PDEBUG(3, "Unsupported with this sensor");
  1318. return -EPERM;
  1319. }
  1320. PDEBUG(3, "%d", *val);
  1321. ov->brightness = *val;
  1322. return 0;
  1323. }
  1324. /* -------------------------------------------------------------------------- */
  1325. /* Sets sensor's saturation (color intensity) setting to "val" */
  1326. static int
  1327. sensor_set_saturation(struct usb_ov511 *ov, unsigned short val)
  1328. {
  1329. int rc;
  1330. PDEBUG(3, "%d", val);
  1331. if (ov->stop_during_set)
  1332. if (ov51x_stop(ov) < 0)
  1333. return -EIO;
  1334. switch (ov->sensor) {
  1335. case SEN_OV7610:
  1336. case SEN_OV76BE:
  1337. case SEN_OV6620:
  1338. case SEN_OV6630:
  1339. rc = i2c_w(ov, OV7610_REG_SAT, val >> 8);
  1340. if (rc < 0)
  1341. goto out;
  1342. break;
  1343. case SEN_OV7620:
  1344. // /* Use UV gamma control instead. Bits 0 & 7 are reserved. */
  1345. // rc = ov_i2c_write(ov->dev, 0x62, (val >> 9) & 0x7e);
  1346. // if (rc < 0)
  1347. // goto out;
  1348. rc = i2c_w(ov, OV7610_REG_SAT, val >> 8);
  1349. if (rc < 0)
  1350. goto out;
  1351. break;
  1352. case SEN_SAA7111A:
  1353. rc = i2c_w(ov, 0x0c, val >> 9);
  1354. if (rc < 0)
  1355. goto out;
  1356. break;
  1357. default:
  1358. PDEBUG(3, "Unsupported with this sensor");
  1359. rc = -EPERM;
  1360. goto out;
  1361. }
  1362. rc = 0; /* Success */
  1363. ov->colour = val;
  1364. out:
  1365. if (ov51x_restart(ov) < 0)
  1366. return -EIO;
  1367. return rc;
  1368. }
  1369. /* Gets sensor's saturation (color intensity) setting */
  1370. static int
  1371. sensor_get_saturation(struct usb_ov511 *ov, unsigned short *val)
  1372. {
  1373. int rc;
  1374. switch (ov->sensor) {
  1375. case SEN_OV7610:
  1376. case SEN_OV76BE:
  1377. case SEN_OV6620:
  1378. case SEN_OV6630:
  1379. rc = i2c_r(ov, OV7610_REG_SAT);
  1380. if (rc < 0)
  1381. return rc;
  1382. else
  1383. *val = rc << 8;
  1384. break;
  1385. case SEN_OV7620:
  1386. // /* Use UV gamma reg instead. Bits 0 & 7 are reserved. */
  1387. // rc = i2c_r(ov, 0x62);
  1388. // if (rc < 0)
  1389. // return rc;
  1390. // else
  1391. // *val = (rc & 0x7e) << 9;
  1392. rc = i2c_r(ov, OV7610_REG_SAT);
  1393. if (rc < 0)
  1394. return rc;
  1395. else
  1396. *val = rc << 8;
  1397. break;
  1398. case SEN_SAA7111A:
  1399. *val = ov->colour;
  1400. break;
  1401. default:
  1402. PDEBUG(3, "Unsupported with this sensor");
  1403. return -EPERM;
  1404. }
  1405. PDEBUG(3, "%d", *val);
  1406. ov->colour = *val;
  1407. return 0;
  1408. }
  1409. /* -------------------------------------------------------------------------- */
  1410. /* Sets sensor's hue (red/blue balance) setting to "val" */
  1411. static int
  1412. sensor_set_hue(struct usb_ov511 *ov, unsigned short val)
  1413. {
  1414. int rc;
  1415. PDEBUG(3, "%d", val);
  1416. if (ov->stop_during_set)
  1417. if (ov51x_stop(ov) < 0)
  1418. return -EIO;
  1419. switch (ov->sensor) {
  1420. case SEN_OV7610:
  1421. case SEN_OV6620:
  1422. case SEN_OV6630:
  1423. rc = i2c_w(ov, OV7610_REG_RED, 0xFF - (val >> 8));
  1424. if (rc < 0)
  1425. goto out;
  1426. rc = i2c_w(ov, OV7610_REG_BLUE, val >> 8);
  1427. if (rc < 0)
  1428. goto out;
  1429. break;
  1430. case SEN_OV7620:
  1431. // Hue control is causing problems. I will enable it once it's fixed.
  1432. #if 0
  1433. rc = i2c_w(ov, 0x7a, (unsigned char)(val >> 8) + 0xb);
  1434. if (rc < 0)
  1435. goto out;
  1436. rc = i2c_w(ov, 0x79, (unsigned char)(val >> 8) + 0xb);
  1437. if (rc < 0)
  1438. goto out;
  1439. #endif
  1440. break;
  1441. case SEN_SAA7111A:
  1442. rc = i2c_w(ov, 0x0d, (val + 32768) >> 8);
  1443. if (rc < 0)
  1444. goto out;
  1445. break;
  1446. default:
  1447. PDEBUG(3, "Unsupported with this sensor");
  1448. rc = -EPERM;
  1449. goto out;
  1450. }
  1451. rc = 0; /* Success */
  1452. ov->hue = val;
  1453. out:
  1454. if (ov51x_restart(ov) < 0)
  1455. return -EIO;
  1456. return rc;
  1457. }
  1458. /* Gets sensor's hue (red/blue balance) setting */
  1459. static int
  1460. sensor_get_hue(struct usb_ov511 *ov, unsigned short *val)
  1461. {
  1462. int rc;
  1463. switch (ov->sensor) {
  1464. case SEN_OV7610:
  1465. case SEN_OV6620:
  1466. case SEN_OV6630:
  1467. rc = i2c_r(ov, OV7610_REG_BLUE);
  1468. if (rc < 0)
  1469. return rc;
  1470. else
  1471. *val = rc << 8;
  1472. break;
  1473. case SEN_OV7620:
  1474. rc = i2c_r(ov, 0x7a);
  1475. if (rc < 0)
  1476. return rc;
  1477. else
  1478. *val = rc << 8;
  1479. break;
  1480. case SEN_SAA7111A:
  1481. *val = ov->hue;
  1482. break;
  1483. default:
  1484. PDEBUG(3, "Unsupported with this sensor");
  1485. return -EPERM;
  1486. }
  1487. PDEBUG(3, "%d", *val);
  1488. ov->hue = *val;
  1489. return 0;
  1490. }
  1491. /* -------------------------------------------------------------------------- */
  1492. static int
  1493. sensor_set_picture(struct usb_ov511 *ov, struct video_picture *p)
  1494. {
  1495. int rc;
  1496. PDEBUG(4, "sensor_set_picture");
  1497. ov->whiteness = p->whiteness;
  1498. /* Don't return error if a setting is unsupported, or rest of settings
  1499. * will not be performed */
  1500. rc = sensor_set_contrast(ov, p->contrast);
  1501. if (FATAL_ERROR(rc))
  1502. return rc;
  1503. rc = sensor_set_brightness(ov, p->brightness);
  1504. if (FATAL_ERROR(rc))
  1505. return rc;
  1506. rc = sensor_set_saturation(ov, p->colour);
  1507. if (FATAL_ERROR(rc))
  1508. return rc;
  1509. rc = sensor_set_hue(ov, p->hue);
  1510. if (FATAL_ERROR(rc))
  1511. return rc;
  1512. return 0;
  1513. }
  1514. static int
  1515. sensor_get_picture(struct usb_ov511 *ov, struct video_picture *p)
  1516. {
  1517. int rc;
  1518. PDEBUG(4, "sensor_get_picture");
  1519. /* Don't return error if a setting is unsupported, or rest of settings
  1520. * will not be performed */
  1521. rc = sensor_get_contrast(ov, &(p->contrast));
  1522. if (FATAL_ERROR(rc))
  1523. return rc;
  1524. rc = sensor_get_brightness(ov, &(p->brightness));
  1525. if (FATAL_ERROR(rc))
  1526. return rc;
  1527. rc = sensor_get_saturation(ov, &(p->colour));
  1528. if (FATAL_ERROR(rc))
  1529. return rc;
  1530. rc = sensor_get_hue(ov, &(p->hue));
  1531. if (FATAL_ERROR(rc))
  1532. return rc;
  1533. p->whiteness = 105 << 8;
  1534. return 0;
  1535. }
  1536. #if 0
  1537. // FIXME: Exposure range is only 0x00-0x7f in interlace mode
  1538. /* Sets current exposure for sensor. This only has an effect if auto-exposure
  1539. * is off */
  1540. static inline int
  1541. sensor_set_exposure(struct usb_ov511 *ov, unsigned char val)
  1542. {
  1543. int rc;
  1544. PDEBUG(3, "%d", val);
  1545. if (ov->stop_during_set)
  1546. if (ov51x_stop(ov) < 0)
  1547. return -EIO;
  1548. switch (ov->sensor) {
  1549. case SEN_OV6620:
  1550. case SEN_OV6630:
  1551. case SEN_OV7610:
  1552. case SEN_OV7620:
  1553. case SEN_OV76BE:
  1554. case SEN_OV8600:
  1555. rc = i2c_w(ov, 0x10, val);
  1556. if (rc < 0)
  1557. goto out;
  1558. break;
  1559. case SEN_KS0127:
  1560. case SEN_KS0127B:
  1561. case SEN_SAA7111A:
  1562. PDEBUG(3, "Unsupported with this sensor");
  1563. return -EPERM;
  1564. default:
  1565. err("Sensor not supported for set_exposure");
  1566. return -EINVAL;
  1567. }
  1568. rc = 0; /* Success */
  1569. ov->exposure = val;
  1570. out:
  1571. if (ov51x_restart(ov) < 0)
  1572. return -EIO;
  1573. return rc;
  1574. }
  1575. #endif
  1576. /* Gets current exposure level from sensor, regardless of whether it is under
  1577. * manual control. */
  1578. static int
  1579. sensor_get_exposure(struct usb_ov511 *ov, unsigned char *val)
  1580. {
  1581. int rc;
  1582. switch (ov->sensor) {
  1583. case SEN_OV7610:
  1584. case SEN_OV6620:
  1585. case SEN_OV6630:
  1586. case SEN_OV7620:
  1587. case SEN_OV76BE:
  1588. case SEN_OV8600:
  1589. rc = i2c_r(ov, 0x10);
  1590. if (rc < 0)
  1591. return rc;
  1592. else
  1593. *val = rc;
  1594. break;
  1595. case SEN_KS0127:
  1596. case SEN_KS0127B:
  1597. case SEN_SAA7111A:
  1598. val = NULL;
  1599. PDEBUG(3, "Unsupported with this sensor");
  1600. return -EPERM;
  1601. default:
  1602. err("Sensor not supported for get_exposure");
  1603. return -EINVAL;
  1604. }
  1605. PDEBUG(3, "%d", *val);
  1606. ov->exposure = *val;
  1607. return 0;
  1608. }
  1609. /* Turns on or off the LED. Only has an effect with OV511+/OV518(+) */
  1610. static void
  1611. ov51x_led_control(struct usb_ov511 *ov, int enable)
  1612. {
  1613. PDEBUG(4, " (%s)", enable ? "turn on" : "turn off");
  1614. if (ov->bridge == BRG_OV511PLUS)
  1615. reg_w(ov, R511_SYS_LED_CTL, enable ? 1 : 0);
  1616. else if (ov->bclass == BCL_OV518)
  1617. reg_w_mask(ov, R518_GPIO_OUT, enable ? 0x02 : 0x00, 0x02);
  1618. return;
  1619. }
  1620. /* Matches the sensor's internal frame rate to the lighting frequency.
  1621. * Valid frequencies are:
  1622. * 50 - 50Hz, for European and Asian lighting
  1623. * 60 - 60Hz, for American lighting
  1624. *
  1625. * Tested with: OV7610, OV7620, OV76BE, OV6620
  1626. * Unsupported: KS0127, KS0127B, SAA7111A
  1627. * Returns: 0 for success
  1628. */
  1629. static int
  1630. sensor_set_light_freq(struct usb_ov511 *ov, int freq)
  1631. {
  1632. int sixty;
  1633. PDEBUG(4, "%d Hz", freq);
  1634. if (freq == 60)
  1635. sixty = 1;
  1636. else if (freq == 50)
  1637. sixty = 0;
  1638. else {
  1639. err("Invalid light freq (%d Hz)", freq);
  1640. return -EINVAL;
  1641. }
  1642. switch (ov->sensor) {
  1643. case SEN_OV7610:
  1644. i2c_w_mask(ov, 0x2a, sixty?0x00:0x80, 0x80);
  1645. i2c_w(ov, 0x2b, sixty?0x00:0xac);
  1646. i2c_w_mask(ov, 0x13, 0x10, 0x10);
  1647. i2c_w_mask(ov, 0x13, 0x00, 0x10);
  1648. break;
  1649. case SEN_OV7620:
  1650. case SEN_OV76BE:
  1651. case SEN_OV8600:
  1652. i2c_w_mask(ov, 0x2a, sixty?0x00:0x80, 0x80);
  1653. i2c_w(ov, 0x2b, sixty?0x00:0xac);
  1654. i2c_w_mask(ov, 0x76, 0x01, 0x01);
  1655. break;
  1656. case SEN_OV6620:
  1657. case SEN_OV6630:
  1658. i2c_w(ov, 0x2b, sixty?0xa8:0x28);
  1659. i2c_w(ov, 0x2a, sixty?0x84:0xa4);
  1660. break;
  1661. case SEN_KS0127:
  1662. case SEN_KS0127B:
  1663. case SEN_SAA7111A:
  1664. PDEBUG(5, "Unsupported with this sensor");
  1665. return -EPERM;
  1666. default:
  1667. err("Sensor not supported for set_light_freq");
  1668. return -EINVAL;
  1669. }
  1670. ov->lightfreq = freq;
  1671. return 0;
  1672. }
  1673. /* If enable is true, turn on the sensor's banding filter, otherwise turn it
  1674. * off. This filter tries to reduce the pattern of horizontal light/dark bands
  1675. * caused by some (usually fluorescent) lighting. The light frequency must be
  1676. * set either before or after enabling it with ov51x_set_light_freq().
  1677. *
  1678. * Tested with: OV7610, OV7620, OV76BE, OV6620.
  1679. * Unsupported: KS0127, KS0127B, SAA7111A
  1680. * Returns: 0 for success
  1681. */
  1682. static int
  1683. sensor_set_banding_filter(struct usb_ov511 *ov, int enable)
  1684. {
  1685. int rc;
  1686. PDEBUG(4, " (%s)", enable ? "turn on" : "turn off");
  1687. if (ov->sensor == SEN_KS0127 || ov->sensor == SEN_KS0127B
  1688. || ov->sensor == SEN_SAA7111A) {
  1689. PDEBUG(5, "Unsupported with this sensor");
  1690. return -EPERM;
  1691. }
  1692. rc = i2c_w_mask(ov, 0x2d, enable?0x04:0x00, 0x04);
  1693. if (rc < 0)
  1694. return rc;
  1695. ov->bandfilt = enable;
  1696. return 0;
  1697. }
  1698. /* If enable is true, turn on the sensor's auto brightness control, otherwise
  1699. * turn it off.
  1700. *
  1701. * Unsupported: KS0127, KS0127B, SAA7111A
  1702. * Returns: 0 for success
  1703. */
  1704. static int
  1705. sensor_set_auto_brightness(struct usb_ov511 *ov, int enable)
  1706. {
  1707. int rc;
  1708. PDEBUG(4, " (%s)", enable ? "turn on" : "turn off");
  1709. if (ov->sensor == SEN_KS0127 || ov->sensor == SEN_KS0127B
  1710. || ov->sensor == SEN_SAA7111A) {
  1711. PDEBUG(5, "Unsupported with this sensor");
  1712. return -EPERM;
  1713. }
  1714. rc = i2c_w_mask(ov, 0x2d, enable?0x10:0x00, 0x10);
  1715. if (rc < 0)
  1716. return rc;
  1717. ov->auto_brt = enable;
  1718. return 0;
  1719. }
  1720. /* If enable is true, turn on the sensor's auto exposure control, otherwise
  1721. * turn it off.
  1722. *
  1723. * Unsupported: KS0127, KS0127B, SAA7111A
  1724. * Returns: 0 for success
  1725. */
  1726. static int
  1727. sensor_set_auto_exposure(struct usb_ov511 *ov, int enable)
  1728. {
  1729. PDEBUG(4, " (%s)", enable ? "turn on" : "turn off");
  1730. switch (ov->sensor) {
  1731. case SEN_OV7610:
  1732. i2c_w_mask(ov, 0x29, enable?0x00:0x80, 0x80);
  1733. break;
  1734. case SEN_OV6620:
  1735. case SEN_OV7620:
  1736. case SEN_OV76BE:
  1737. case SEN_OV8600:
  1738. i2c_w_mask(ov, 0x13, enable?0x01:0x00, 0x01);
  1739. break;
  1740. case SEN_OV6630:
  1741. i2c_w_mask(ov, 0x28, enable?0x00:0x10, 0x10);
  1742. break;
  1743. case SEN_KS0127:
  1744. case SEN_KS0127B:
  1745. case SEN_SAA7111A:
  1746. PDEBUG(5, "Unsupported with this sensor");
  1747. return -EPERM;
  1748. default:
  1749. err("Sensor not supported for set_auto_exposure");
  1750. return -EINVAL;
  1751. }
  1752. ov->auto_exp = enable;
  1753. return 0;
  1754. }
  1755. /* Modifies the sensor's exposure algorithm to allow proper exposure of objects
  1756. * that are illuminated from behind.
  1757. *
  1758. * Tested with: OV6620, OV7620
  1759. * Unsupported: OV7610, OV76BE, KS0127, KS0127B, SAA7111A
  1760. * Returns: 0 for success
  1761. */
  1762. static int
  1763. sensor_set_backlight(struct usb_ov511 *ov, int enable)
  1764. {
  1765. PDEBUG(4, " (%s)", enable ? "turn on" : "turn off");
  1766. switch (ov->sensor) {
  1767. case SEN_OV7620:
  1768. case SEN_OV8600:
  1769. i2c_w_mask(ov, 0x68, enable?0xe0:0xc0, 0xe0);
  1770. i2c_w_mask(ov, 0x29, enable?0x08:0x00, 0x08);
  1771. i2c_w_mask(ov, 0x28, enable?0x02:0x00, 0x02);
  1772. break;
  1773. case SEN_OV6620:
  1774. i2c_w_mask(ov, 0x4e, enable?0xe0:0xc0, 0xe0);
  1775. i2c_w_mask(ov, 0x29, enable?0x08:0x00, 0x08);
  1776. i2c_w_mask(ov, 0x0e, enable?0x80:0x00, 0x80);
  1777. break;
  1778. case SEN_OV6630:
  1779. i2c_w_mask(ov, 0x4e, enable?0x80:0x60, 0xe0);
  1780. i2c_w_mask(ov, 0x29, enable?0x08:0x00, 0x08);
  1781. i2c_w_mask(ov, 0x28, enable?0x02:0x00, 0x02);
  1782. break;
  1783. case SEN_OV7610:
  1784. case SEN_OV76BE:
  1785. case SEN_KS0127:
  1786. case SEN_KS0127B:
  1787. case SEN_SAA7111A:
  1788. PDEBUG(5, "Unsupported with this sensor");
  1789. return -EPERM;
  1790. default:
  1791. err("Sensor not supported for set_backlight");
  1792. return -EINVAL;
  1793. }
  1794. ov->backlight = enable;
  1795. return 0;
  1796. }
  1797. static int
  1798. sensor_set_mirror(struct usb_ov511 *ov, int enable)
  1799. {
  1800. PDEBUG(4, " (%s)", enable ? "turn on" : "turn off");
  1801. switch (ov->sensor) {
  1802. case SEN_OV6620:
  1803. case SEN_OV6630:
  1804. case SEN_OV7610:
  1805. case SEN_OV7620:
  1806. case SEN_OV76BE:
  1807. case SEN_OV8600:
  1808. i2c_w_mask(ov, 0x12, enable?0x40:0x00, 0x40);
  1809. break;
  1810. case SEN_KS0127:
  1811. case SEN_KS0127B:
  1812. case SEN_SAA7111A:
  1813. PDEBUG(5, "Unsupported with this sensor");
  1814. return -EPERM;
  1815. default:
  1816. err("Sensor not supported for set_mirror");
  1817. return -EINVAL;
  1818. }
  1819. ov->mirror = enable;
  1820. return 0;
  1821. }
  1822. /* Returns number of bits per pixel (regardless of where they are located;
  1823. * planar or not), or zero for unsupported format.
  1824. */
  1825. static inline int
  1826. get_depth(int palette)
  1827. {
  1828. switch (palette) {
  1829. case VIDEO_PALETTE_GREY: return 8;
  1830. case VIDEO_PALETTE_YUV420: return 12;
  1831. case VIDEO_PALETTE_YUV420P: return 12; /* Planar */
  1832. default: return 0; /* Invalid format */
  1833. }
  1834. }
  1835. /* Bytes per frame. Used by read(). Return of 0 indicates error */
  1836. static inline long int
  1837. get_frame_length(struct ov511_frame *frame)
  1838. {
  1839. if (!frame)
  1840. return 0;
  1841. else
  1842. return ((frame->width * frame->height
  1843. * get_depth(frame->format)) >> 3);
  1844. }
  1845. static int
  1846. mode_init_ov_sensor_regs(struct usb_ov511 *ov, int width, int height,
  1847. int mode, int sub_flag, int qvga)
  1848. {
  1849. int clock;
  1850. /******** Mode (VGA/QVGA) and sensor specific regs ********/
  1851. switch (ov->sensor) {
  1852. case SEN_OV7610:
  1853. i2c_w(ov, 0x14, qvga?0x24:0x04);
  1854. // FIXME: Does this improve the image quality or frame rate?
  1855. #if 0
  1856. i2c_w_mask(ov, 0x28, qvga?0x00:0x20, 0x20);
  1857. i2c_w(ov, 0x24, 0x10);
  1858. i2c_w(ov, 0x25, qvga?0x40:0x8a);
  1859. i2c_w(ov, 0x2f, qvga?0x30:0xb0);
  1860. i2c_w(ov, 0x35, qvga?0x1c:0x9c);
  1861. #endif
  1862. break;
  1863. case SEN_OV7620:
  1864. // i2c_w(ov, 0x2b, 0x00);
  1865. i2c_w(ov, 0x14, qvga?0xa4:0x84);
  1866. i2c_w_mask(ov, 0x28, qvga?0x00:0x20, 0x20);
  1867. i2c_w(ov, 0x24, qvga?0x20:0x3a);
  1868. i2c_w(ov, 0x25, qvga?0x30:0x60);
  1869. i2c_w_mask(ov, 0x2d, qvga?0x40:0x00, 0x40);
  1870. i2c_w_mask(ov, 0x67, qvga?0xf0:0x90, 0xf0);
  1871. i2c_w_mask(ov, 0x74, qvga?0x20:0x00, 0x20);
  1872. break;
  1873. case SEN_OV76BE:
  1874. // i2c_w(ov, 0x2b, 0x00);
  1875. i2c_w(ov, 0x14, qvga?0xa4:0x84);
  1876. // FIXME: Enable this once 7620AE uses 7620 initial settings
  1877. #if 0
  1878. i2c_w_mask(ov, 0x28, qvga?0x00:0x20, 0x20);
  1879. i2c_w(ov, 0x24, qvga?0x20:0x3a);
  1880. i2c_w(ov, 0x25, qvga?0x30:0x60);
  1881. i2c_w_mask(ov, 0x2d, qvga?0x40:0x00, 0x40);
  1882. i2c_w_mask(ov, 0x67, qvga?0xb0:0x90, 0xf0);
  1883. i2c_w_mask(ov, 0x74, qvga?0x20:0x00, 0x20);
  1884. #endif
  1885. break;
  1886. case SEN_OV6620:
  1887. i2c_w(ov, 0x14, qvga?0x24:0x04);
  1888. break;
  1889. case SEN_OV6630:
  1890. i2c_w(ov, 0x14, qvga?0xa0:0x80);
  1891. break;
  1892. default:
  1893. err("Invalid sensor");
  1894. return -EINVAL;
  1895. }
  1896. /******** Palette-specific regs ********/
  1897. if (mode == VIDEO_PALETTE_GREY) {
  1898. if (ov->sensor == SEN_OV7610 || ov->sensor == SEN_OV76BE) {
  1899. /* these aren't valid on the OV6620/OV7620/6630? */
  1900. i2c_w_mask(ov, 0x0e, 0x40, 0x40);
  1901. }
  1902. if (ov->sensor == SEN_OV6630 && ov->bridge == BRG_OV518
  1903. && ov518_color) {
  1904. i2c_w_mask(ov, 0x12, 0x00, 0x10);
  1905. i2c_w_mask(ov, 0x13, 0x00, 0x20);
  1906. } else {
  1907. i2c_w_mask(ov, 0x13, 0x20, 0x20);
  1908. }
  1909. } else {
  1910. if (ov->sensor == SEN_OV7610 || ov->sensor == SEN_OV76BE) {
  1911. /* not valid on the OV6620/OV7620/6630? */
  1912. i2c_w_mask(ov, 0x0e, 0x00, 0x40);
  1913. }
  1914. /* The OV518 needs special treatment. Although both the OV518
  1915. * and the OV6630 support a 16-bit video bus, only the 8 bit Y
  1916. * bus is actually used. The UV bus is tied to ground.
  1917. * Therefore, the OV6630 needs to be in 8-bit multiplexed
  1918. * output mode */
  1919. if (ov->sensor == SEN_OV6630 && ov->bridge == BRG_OV518
  1920. && ov518_color) {
  1921. i2c_w_mask(ov, 0x12, 0x10, 0x10);
  1922. i2c_w_mask(ov, 0x13, 0x20, 0x20);
  1923. } else {
  1924. i2c_w_mask(ov, 0x13, 0x00, 0x20);
  1925. }
  1926. }
  1927. /******** Clock programming ********/
  1928. /* The OV6620 needs special handling. This prevents the
  1929. * severe banding that normally occurs */
  1930. if (ov->sensor == SEN_OV6620 || ov->sensor == SEN_OV6630)
  1931. {
  1932. /* Clock down */
  1933. i2c_w(ov, 0x2a, 0x04);
  1934. if (ov->compress) {
  1935. // clock = 0; /* This ensures the highest frame rate */
  1936. clock = 3;
  1937. } else if (clockdiv == -1) { /* If user didn't override it */
  1938. clock = 3; /* Gives better exposure time */
  1939. } else {
  1940. clock = clockdiv;
  1941. }
  1942. PDEBUG(4, "Setting clock divisor to %d", clock);
  1943. i2c_w(ov, 0x11, clock);
  1944. i2c_w(ov, 0x2a, 0x84);
  1945. /* This next setting is critical. It seems to improve
  1946. * the gain or the contrast. The "reserved" bits seem
  1947. * to have some effect in this case. */
  1948. i2c_w(ov, 0x2d, 0x85);
  1949. }
  1950. else
  1951. {
  1952. if (ov->compress) {
  1953. clock = 1; /* This ensures the highest frame rate */
  1954. } else if (clockdiv == -1) { /* If user didn't override it */
  1955. /* Calculate and set the clock divisor */
  1956. clock = ((sub_flag ? ov->subw * ov->subh
  1957. : width * height)
  1958. * (mode == VIDEO_PALETTE_GREY ? 2 : 3) / 2)
  1959. / 66000;
  1960. } else {
  1961. clock = clockdiv;
  1962. }
  1963. PDEBUG(4, "Setting clock divisor to %d", clock);
  1964. i2c_w(ov, 0x11, clock);
  1965. }
  1966. /******** Special Features ********/
  1967. if (framedrop >= 0)
  1968. i2c_w(ov, 0x16, framedrop);
  1969. /* Test Pattern */
  1970. i2c_w_mask(ov, 0x12, (testpat?0x02:0x00), 0x02);
  1971. /* Enable auto white balance */
  1972. i2c_w_mask(ov, 0x12, 0x04, 0x04);
  1973. // This will go away as soon as ov51x_mode_init_sensor_regs()
  1974. // is fully tested.
  1975. /* 7620/6620/6630? don't have register 0x35, so play it safe */
  1976. if (ov->sensor == SEN_OV7610 || ov->sensor == SEN_OV76BE) {
  1977. if (width == 640 && height == 480)
  1978. i2c_w(ov, 0x35, 0x9e);
  1979. else
  1980. i2c_w(ov, 0x35, 0x1e);
  1981. }
  1982. return 0;
  1983. }
  1984. static int
  1985. set_ov_sensor_window(struct usb_ov511 *ov, int width, int height, int mode,
  1986. int sub_flag)
  1987. {
  1988. int ret;
  1989. int hwsbase, hwebase, vwsbase, vwebase, hwsize, vwsize;
  1990. int hoffset, voffset, hwscale = 0, vwscale = 0;
  1991. /* The different sensor ICs handle setting up of window differently.
  1992. * IF YOU SET IT WRONG, YOU WILL GET ALL ZERO ISOC DATA FROM OV51x!!! */
  1993. switch (ov->sensor) {
  1994. case SEN_OV7610:
  1995. case SEN_OV76BE:
  1996. hwsbase = 0x38;
  1997. hwebase = 0x3a;
  1998. vwsbase = vwebase = 0x05;
  1999. break;
  2000. case SEN_OV6620:
  2001. case SEN_OV6630:
  2002. hwsbase = 0x38;
  2003. hwebase = 0x3a;
  2004. vwsbase = 0x05;
  2005. vwebase = 0x06;
  2006. break;
  2007. case SEN_OV7620:
  2008. hwsbase = 0x2f; /* From 7620.SET (spec is wrong) */
  2009. hwebase = 0x2f;
  2010. vwsbase = vwebase = 0x05;
  2011. break;
  2012. default:
  2013. err("Invalid sensor");
  2014. return -EINVAL;
  2015. }
  2016. if (ov->sensor == SEN_OV6620 || ov->sensor == SEN_OV6630) {
  2017. /* Note: OV518(+) does downsample on its own) */
  2018. if ((width > 176 && height > 144)
  2019. || ov->bclass == BCL_OV518) { /* CIF */
  2020. ret = mode_init_ov_sensor_regs(ov, width, height,
  2021. mode, sub_flag, 0);
  2022. if (ret < 0)
  2023. return ret;
  2024. hwscale = 1;
  2025. vwscale = 1; /* The datasheet says 0; it's wrong */
  2026. hwsize = 352;
  2027. vwsize = 288;
  2028. } else if (width > 176 || height > 144) {
  2029. err("Illegal dimensions");
  2030. return -EINVAL;
  2031. } else { /* QCIF */
  2032. ret = mode_init_ov_sensor_regs(ov, width, height,
  2033. mode, sub_flag, 1);
  2034. if (ret < 0)
  2035. return ret;
  2036. hwsize = 176;
  2037. vwsize = 144;
  2038. }
  2039. } else {
  2040. if (width > 320 && height > 240) { /* VGA */
  2041. ret = mode_init_ov_sensor_regs(ov, width, height,
  2042. mode, sub_flag, 0);
  2043. if (ret < 0)
  2044. return ret;
  2045. hwscale = 2;
  2046. vwscale = 1;
  2047. hwsize = 640;
  2048. vwsize = 480;
  2049. } else if (width > 320 || height > 240) {
  2050. err("Illegal dimensions");
  2051. return -EINVAL;
  2052. } else { /* QVGA */
  2053. ret = mode_init_ov_sensor_regs(ov, width, height,
  2054. mode, sub_flag, 1);
  2055. if (ret < 0)
  2056. return ret;
  2057. hwscale = 1;
  2058. hwsize = 320;
  2059. vwsize = 240;
  2060. }
  2061. }
  2062. /* Center the window */
  2063. hoffset = ((hwsize - width) / 2) >> hwscale;
  2064. voffset = ((vwsize - height) / 2) >> vwscale;
  2065. /* FIXME! - This needs to be changed to support 160x120 and 6620!!! */
  2066. if (sub_flag) {
  2067. i2c_w(ov, 0x17, hwsbase+(ov->subx>>hwscale));
  2068. i2c_w(ov, 0x18, hwebase+((ov->subx+ov->subw)>>hwscale));
  2069. i2c_w(ov, 0x19, vwsbase+(ov->suby>>vwscale));
  2070. i2c_w(ov, 0x1a, vwebase+((ov->suby+ov->subh)>>vwscale));
  2071. } else {
  2072. i2c_w(ov, 0x17, hwsbase + hoffset);
  2073. i2c_w(ov, 0x18, hwebase + hoffset + (hwsize>>hwscale));
  2074. i2c_w(ov, 0x19, vwsbase + voffset);
  2075. i2c_w(ov, 0x1a, vwebase + voffset + (vwsize>>vwscale));
  2076. }
  2077. #ifdef OV511_DEBUG
  2078. if (dump_sensor)
  2079. dump_i2c_regs(ov);
  2080. #endif
  2081. return 0;
  2082. }
  2083. /* Set up the OV511/OV511+ with the given image parameters.
  2084. *
  2085. * Do not put any sensor-specific code in here (including I2C I/O functions)
  2086. */
  2087. static int
  2088. ov511_mode_init_regs(struct usb_ov511 *ov,
  2089. int width, int height, int mode, int sub_flag)
  2090. {
  2091. int hsegs, vsegs;
  2092. if (sub_flag) {
  2093. width = ov->subw;
  2094. height = ov->subh;
  2095. }
  2096. PDEBUG(3, "width:%d, height:%d, mode:%d, sub:%d",
  2097. width, height, mode, sub_flag);
  2098. // FIXME: This should be moved to a 7111a-specific function once
  2099. // subcapture is dealt with properly
  2100. if (ov->sensor == SEN_SAA7111A) {
  2101. if (width == 320 && height == 240) {
  2102. /* No need to do anything special */
  2103. } else if (width == 640 && height == 480) {
  2104. /* Set the OV511 up as 320x480, but keep the
  2105. * V4L resolution as 640x480 */
  2106. width = 320;
  2107. } else {
  2108. err("SAA7111A only allows 320x240 or 640x480");
  2109. return -EINVAL;
  2110. }
  2111. }
  2112. /* Make sure width and height are a multiple of 8 */
  2113. if (width % 8 || height % 8) {
  2114. err("Invalid size (%d, %d) (mode = %d)", width, height, mode);
  2115. return -EINVAL;
  2116. }
  2117. if (width < ov->minwidth || height < ov->minheight) {
  2118. err("Requested dimensions are too small");
  2119. return -EINVAL;
  2120. }
  2121. if (ov51x_stop(ov) < 0)
  2122. return -EIO;
  2123. if (mode == VIDEO_PALETTE_GREY) {
  2124. reg_w(ov, R511_CAM_UV_EN, 0x00);
  2125. reg_w(ov, R511_SNAP_UV_EN, 0x00);
  2126. reg_w(ov, R511_SNAP_OPTS, 0x01);
  2127. } else {
  2128. reg_w(ov, R511_CAM_UV_EN, 0x01);
  2129. reg_w(ov, R511_SNAP_UV_EN, 0x01);
  2130. reg_w(ov, R511_SNAP_OPTS, 0x03);
  2131. }
  2132. /* Here I'm assuming that snapshot size == image size.
  2133. * I hope that's always true. --claudio
  2134. */
  2135. hsegs = (width >> 3) - 1;
  2136. vsegs = (height >> 3) - 1;
  2137. reg_w(ov, R511_CAM_PXCNT, hsegs);
  2138. reg_w(ov, R511_CAM_LNCNT, vsegs);
  2139. reg_w(ov, R511_CAM_PXDIV, 0x00);
  2140. reg_w(ov, R511_CAM_LNDIV, 0x00);
  2141. /* YUV420, low pass filter on */
  2142. reg_w(ov, R511_CAM_OPTS, 0x03);
  2143. /* Snapshot additions */
  2144. reg_w(ov, R511_SNAP_PXCNT, hsegs);
  2145. reg_w(ov, R511_SNAP_LNCNT, vsegs);
  2146. reg_w(ov, R511_SNAP_PXDIV, 0x00);
  2147. reg_w(ov, R511_SNAP_LNDIV, 0x00);
  2148. if (ov->compress) {
  2149. /* Enable Y and UV quantization and compression */
  2150. reg_w(ov, R511_COMP_EN, 0x07);
  2151. reg_w(ov, R511_COMP_LUT_EN, 0x03);
  2152. ov51x_reset(ov, OV511_RESET_OMNICE);
  2153. }
  2154. if (ov51x_restart(ov) < 0)
  2155. return -EIO;
  2156. return 0;
  2157. }
  2158. /* Sets up the OV518/OV518+ with the given image parameters
  2159. *
  2160. * OV518 needs a completely different approach, until we can figure out what
  2161. * the individual registers do. Also, only 15 FPS is supported now.
  2162. *
  2163. * Do not put any sensor-specific code in here (including I2C I/O functions)
  2164. */
  2165. static int
  2166. ov518_mode_init_regs(struct usb_ov511 *ov,
  2167. int width, int height, int mode, int sub_flag)
  2168. {
  2169. int hsegs, vsegs, hi_res;
  2170. if (sub_flag) {
  2171. width = ov->subw;
  2172. height = ov->subh;
  2173. }
  2174. PDEBUG(3, "width:%d, height:%d, mode:%d, sub:%d",
  2175. width, height, mode, sub_flag);
  2176. if (width % 16 || height % 8) {
  2177. err("Invalid size (%d, %d)", width, height);
  2178. return -EINVAL;
  2179. }
  2180. if (width < ov->minwidth || height < ov->minheight) {
  2181. err("Requested dimensions are too small");
  2182. return -EINVAL;
  2183. }
  2184. if (width >= 320 && height >= 240) {
  2185. hi_res = 1;
  2186. } else if (width >= 320 || height >= 240) {
  2187. err("Invalid width/height combination (%d, %d)", width, height);
  2188. return -EINVAL;
  2189. } else {
  2190. hi_res = 0;
  2191. }
  2192. if (ov51x_stop(ov) < 0)
  2193. return -EIO;
  2194. /******** Set the mode ********/
  2195. reg_w(ov, 0x2b, 0);
  2196. reg_w(ov, 0x2c, 0);
  2197. reg_w(ov, 0x2d, 0);
  2198. reg_w(ov, 0x2e, 0);
  2199. reg_w(ov, 0x3b, 0);
  2200. reg_w(ov, 0x3c, 0);
  2201. reg_w(ov, 0x3d, 0);
  2202. reg_w(ov, 0x3e, 0);
  2203. if (ov->bridge == BRG_OV518 && ov518_color) {
  2204. /* OV518 needs U and V swapped */
  2205. i2c_w_mask(ov, 0x15, 0x00, 0x01);
  2206. if (mode == VIDEO_PALETTE_GREY) {
  2207. /* Set 16-bit input format (UV data are ignored) */
  2208. reg_w_mask(ov, 0x20, 0x00, 0x08);
  2209. /* Set 8-bit (4:0:0) output format */
  2210. reg_w_mask(ov, 0x28, 0x00, 0xf0);
  2211. reg_w_mask(ov, 0x38, 0x00, 0xf0);
  2212. } else {
  2213. /* Set 8-bit (YVYU) input format */
  2214. reg_w_mask(ov, 0x20, 0x08, 0x08);
  2215. /* Set 12-bit (4:2:0) output format */
  2216. reg_w_mask(ov, 0x28, 0x80, 0xf0);
  2217. reg_w_mask(ov, 0x38, 0x80, 0xf0);
  2218. }
  2219. } else {
  2220. reg_w(ov, 0x28, (mode == VIDEO_PALETTE_GREY) ? 0x00:0x80);
  2221. reg_w(ov, 0x38, (mode == VIDEO_PALETTE_GREY) ? 0x00:0x80);
  2222. }
  2223. hsegs = width / 16;
  2224. vsegs = height / 4;
  2225. reg_w(ov, 0x29, hsegs);
  2226. reg_w(ov, 0x2a, vsegs);
  2227. reg_w(ov, 0x39, hsegs);
  2228. reg_w(ov, 0x3a, vsegs);
  2229. /* Windows driver does this here; who knows why */
  2230. reg_w(ov, 0x2f, 0x80);
  2231. /******** Set the framerate (to 15 FPS) ********/
  2232. /* Mode independent, but framerate dependent, regs */
  2233. reg_w(ov, 0x51, 0x02); /* Clock divider; lower==faster */
  2234. reg_w(ov, 0x22, 0x18);
  2235. reg_w(ov, 0x23, 0xff);
  2236. if (ov->bridge == BRG_OV518PLUS)
  2237. reg_w(ov, 0x21, 0x19);
  2238. else
  2239. reg_w(ov, 0x71, 0x19); /* Compression-related? */
  2240. // FIXME: Sensor-specific
  2241. /* Bit 5 is what matters here. Of course, it is "reserved" */
  2242. i2c_w(ov, 0x54, 0x23);
  2243. reg_w(ov, 0x2f, 0x80);
  2244. if (ov->bridge == BRG_OV518PLUS) {
  2245. reg_w(ov, 0x24, 0x94);
  2246. reg_w(ov, 0x25, 0x90);
  2247. ov518_reg_w32(ov, 0xc4, 400, 2); /* 190h */
  2248. ov518_reg_w32(ov, 0xc6, 540, 2); /* 21ch */
  2249. ov518_reg_w32(ov, 0xc7, 540, 2); /* 21ch */
  2250. ov518_reg_w32(ov, 0xc8, 108, 2); /* 6ch */
  2251. ov518_reg_w32(ov, 0xca, 131098, 3); /* 2001ah */
  2252. ov518_reg_w32(ov, 0xcb, 532, 2); /* 214h */
  2253. ov518_reg_w32(ov, 0xcc, 2400, 2); /* 960h */
  2254. ov518_reg_w32(ov, 0xcd, 32, 2); /* 20h */
  2255. ov518_reg_w32(ov, 0xce, 608, 2); /* 260h */
  2256. } else {
  2257. reg_w(ov, 0x24, 0x9f);
  2258. reg_w(ov, 0x25, 0x90);
  2259. ov518_reg_w32(ov, 0xc4, 400, 2); /* 190h */
  2260. ov518_reg_w32(ov, 0xc6, 500, 2); /* 1f4h */
  2261. ov518_reg_w32(ov, 0xc7, 500, 2); /* 1f4h */
  2262. ov518_reg_w32(ov, 0xc8, 142, 2); /* 8eh */
  2263. ov518_reg_w32(ov, 0xca, 131098, 3); /* 2001ah */
  2264. ov518_reg_w32(ov, 0xcb, 532, 2); /* 214h */
  2265. ov518_reg_w32(ov, 0xcc, 2000, 2); /* 7d0h */
  2266. ov518_reg_w32(ov, 0xcd, 32, 2); /* 20h */
  2267. ov518_reg_w32(ov, 0xce, 608, 2); /* 260h */
  2268. }
  2269. reg_w(ov, 0x2f, 0x80);
  2270. if (ov51x_restart(ov) < 0)
  2271. return -EIO;
  2272. /* Reset it just for good measure */
  2273. if (ov51x_reset(ov, OV511_RESET_NOREGS) < 0)
  2274. return -EIO;
  2275. return 0;
  2276. }
  2277. /* This is a wrapper around the OV511, OV518, and sensor specific functions */
  2278. static int
  2279. mode_init_regs(struct usb_ov511 *ov,
  2280. int width, int height, int mode, int sub_flag)
  2281. {
  2282. int rc = 0;
  2283. if (!ov || !ov->dev)
  2284. return -EFAULT;
  2285. if (ov->bclass == BCL_OV518) {
  2286. rc = ov518_mode_init_regs(ov, width, height, mode, sub_flag);
  2287. } else {
  2288. rc = ov511_mode_init_regs(ov, width, height, mode, sub_flag);
  2289. }
  2290. if (FATAL_ERROR(rc))
  2291. return rc;
  2292. switch (ov->sensor) {
  2293. case SEN_OV7610:
  2294. case SEN_OV7620:
  2295. case SEN_OV76BE:
  2296. case SEN_OV8600:
  2297. case SEN_OV6620:
  2298. case SEN_OV6630:
  2299. rc = set_ov_sensor_window(ov, width, height, mode, sub_flag);
  2300. break;
  2301. case SEN_KS0127:
  2302. case SEN_KS0127B:
  2303. err("KS0127-series decoders not supported yet");
  2304. rc = -EINVAL;
  2305. break;
  2306. case SEN_SAA7111A:
  2307. // rc = mode_init_saa_sensor_regs(ov, width, height, mode,
  2308. // sub_flag);
  2309. PDEBUG(1, "SAA status = 0x%02X", i2c_r(ov, 0x1f));
  2310. break;
  2311. default:
  2312. err("Unknown sensor");
  2313. rc = -EINVAL;
  2314. }
  2315. if (FATAL_ERROR(rc))
  2316. return rc;
  2317. /* Sensor-independent settings */
  2318. rc = sensor_set_auto_brightness(ov, ov->auto_brt);
  2319. if (FATAL_ERROR(rc))
  2320. return rc;
  2321. rc = sensor_set_auto_exposure(ov, ov->auto_exp);
  2322. if (FATAL_ERROR(rc))
  2323. return rc;
  2324. rc = sensor_set_banding_filter(ov, bandingfilter);
  2325. if (FATAL_ERROR(rc))
  2326. return rc;
  2327. if (ov->lightfreq) {
  2328. rc = sensor_set_light_freq(ov, lightfreq);
  2329. if (FATAL_ERROR(rc))
  2330. return rc;
  2331. }
  2332. rc = sensor_set_backlight(ov, ov->backlight);
  2333. if (FATAL_ERROR(rc))
  2334. return rc;
  2335. rc = sensor_set_mirror(ov, ov->mirror);
  2336. if (FATAL_ERROR(rc))
  2337. return rc;
  2338. return 0;
  2339. }
  2340. /* This sets the default image parameters. This is useful for apps that use
  2341. * read() and do not set these.
  2342. */
  2343. static int
  2344. ov51x_set_default_params(struct usb_ov511 *ov)
  2345. {
  2346. int i;
  2347. /* Set default sizes in case IOCTL (VIDIOCMCAPTURE) is not used
  2348. * (using read() instead). */
  2349. for (i = 0; i < OV511_NUMFRAMES; i++) {
  2350. ov->frame[i].width = ov->maxwidth;
  2351. ov->frame[i].height = ov->maxheight;
  2352. ov->frame[i].bytes_read = 0;
  2353. if (force_palette)
  2354. ov->frame[i].format = force_palette;
  2355. else
  2356. ov->frame[i].format = VIDEO_PALETTE_YUV420;
  2357. ov->frame[i].depth = get_depth(ov->frame[i].format);
  2358. }
  2359. PDEBUG(3, "%dx%d, %s", ov->maxwidth, ov->maxheight,
  2360. symbolic(v4l1_plist, ov->frame[0].format));
  2361. /* Initialize to max width/height, YUV420 or RGB24 (if supported) */
  2362. if (mode_init_regs(ov, ov->maxwidth, ov->maxheight,
  2363. ov->frame[0].format, 0) < 0)
  2364. return -EINVAL;
  2365. return 0;
  2366. }
  2367. /**********************************************************************
  2368. *
  2369. * Video decoder stuff
  2370. *
  2371. **********************************************************************/
  2372. /* Set analog input port of decoder */
  2373. static int
  2374. decoder_set_input(struct usb_ov511 *ov, int input)
  2375. {
  2376. PDEBUG(4, "port %d", input);
  2377. switch (ov->sensor) {
  2378. case SEN_SAA7111A:
  2379. {
  2380. /* Select mode */
  2381. i2c_w_mask(ov, 0x02, input, 0x07);
  2382. /* Bypass chrominance trap for modes 4..7 */
  2383. i2c_w_mask(ov, 0x09, (input > 3) ? 0x80:0x00, 0x80);
  2384. break;
  2385. }
  2386. default:
  2387. return -EINVAL;
  2388. }
  2389. return 0;
  2390. }
  2391. /* Get ASCII name of video input */
  2392. static int
  2393. decoder_get_input_name(struct usb_ov511 *ov, int input, char *name)
  2394. {
  2395. switch (ov->sensor) {
  2396. case SEN_SAA7111A:
  2397. {
  2398. if (input < 0 || input > 7)
  2399. return -EINVAL;
  2400. else if (input < 4)
  2401. sprintf(name, "CVBS-%d", input);
  2402. else // if (input < 8)
  2403. sprintf(name, "S-Video-%d", input - 4);
  2404. break;
  2405. }
  2406. default:
  2407. sprintf(name, "%s", "Camera");
  2408. }
  2409. return 0;
  2410. }
  2411. /* Set norm (NTSC, PAL, SECAM, AUTO) */
  2412. static int
  2413. decoder_set_norm(struct usb_ov511 *ov, int norm)
  2414. {
  2415. PDEBUG(4, "%d", norm);
  2416. switch (ov->sensor) {
  2417. case SEN_SAA7111A:
  2418. {
  2419. int reg_8, reg_e;
  2420. if (norm == VIDEO_MODE_NTSC) {
  2421. reg_8 = 0x40; /* 60 Hz */
  2422. reg_e = 0x00; /* NTSC M / PAL BGHI */
  2423. } else if (norm == VIDEO_MODE_PAL) {
  2424. reg_8 = 0x00; /* 50 Hz */
  2425. reg_e = 0x00; /* NTSC M / PAL BGHI */
  2426. } else if (norm == VIDEO_MODE_AUTO) {
  2427. reg_8 = 0x80; /* Auto field detect */
  2428. reg_e = 0x00; /* NTSC M / PAL BGHI */
  2429. } else if (norm == VIDEO_MODE_SECAM) {
  2430. reg_8 = 0x00; /* 50 Hz */
  2431. reg_e = 0x50; /* SECAM / PAL 4.43 */
  2432. } else {
  2433. return -EINVAL;
  2434. }
  2435. i2c_w_mask(ov, 0x08, reg_8, 0xc0);
  2436. i2c_w_mask(ov, 0x0e, reg_e, 0x70);
  2437. break;
  2438. }
  2439. default:
  2440. return -EINVAL;
  2441. }
  2442. return 0;
  2443. }
  2444. /**********************************************************************
  2445. *
  2446. * Raw data parsing
  2447. *
  2448. **********************************************************************/
  2449. /* Copies a 64-byte segment at pIn to an 8x8 block at pOut. The width of the
  2450. * image at pOut is specified by w.
  2451. */
  2452. static inline void
  2453. make_8x8(unsigned char *pIn, unsigned char *pOut, int w)
  2454. {
  2455. unsigned char *pOut1 = pOut;
  2456. int x, y;
  2457. for (y = 0; y < 8; y++) {
  2458. pOut1 = pOut;
  2459. for (x = 0; x < 8; x++) {
  2460. *pOut1++ = *pIn++;
  2461. }
  2462. pOut += w;
  2463. }
  2464. }
  2465. /*
  2466. * For RAW BW (YUV 4:0:0) images, data show up in 256 byte segments.
  2467. * The segments represent 4 squares of 8x8 pixels as follows:
  2468. *
  2469. * 0 1 ... 7 64 65 ... 71 ... 192 193 ... 199
  2470. * 8 9 ... 15 72 73 ... 79 200 201 ... 207
  2471. * ... ... ...
  2472. * 56 57 ... 63 120 121 ... 127 248 249 ... 255
  2473. *
  2474. */
  2475. static void
  2476. yuv400raw_to_yuv400p(struct ov511_frame *frame,
  2477. unsigned char *pIn0, unsigned char *pOut0)
  2478. {
  2479. int x, y;
  2480. unsigned char *pIn, *pOut, *pOutLine;
  2481. /* Copy Y */
  2482. pIn = pIn0;
  2483. pOutLine = pOut0;
  2484. for (y = 0; y < frame->rawheight - 1; y += 8) {
  2485. pOut = pOutLine;
  2486. for (x = 0; x < frame->rawwidth - 1; x += 8) {
  2487. make_8x8(pIn, pOut, frame->rawwidth);
  2488. pIn += 64;
  2489. pOut += 8;
  2490. }
  2491. pOutLine += 8 * frame->rawwidth;
  2492. }
  2493. }
  2494. /*
  2495. * For YUV 4:2:0 images, the data show up in 384 byte segments.
  2496. * The first 64 bytes of each segment are U, the next 64 are V. The U and
  2497. * V are arranged as follows:
  2498. *
  2499. * 0 1 ... 7
  2500. * 8 9 ... 15
  2501. * ...
  2502. * 56 57 ... 63
  2503. *
  2504. * U and V are shipped at half resolution (1 U,V sample -> one 2x2 block).
  2505. *
  2506. * The next 256 bytes are full resolution Y data and represent 4 squares
  2507. * of 8x8 pixels as follows:
  2508. *
  2509. * 0 1 ... 7 64 65 ... 71 ... 192 193 ... 199
  2510. * 8 9 ... 15 72 73 ... 79 200 201 ... 207
  2511. * ... ... ...
  2512. * 56 57 ... 63 120 121 ... 127 ... 248 249 ... 255
  2513. *
  2514. * Note that the U and V data in one segment represent a 16 x 16 pixel
  2515. * area, but the Y data represent a 32 x 8 pixel area. If the width is not an
  2516. * even multiple of 32, the extra 8x8 blocks within a 32x8 block belong to the
  2517. * next horizontal stripe.
  2518. *
  2519. * If dumppix module param is set, _parse_data just dumps the incoming segments,
  2520. * verbatim, in order, into the frame. When used with vidcat -f ppm -s 640x480
  2521. * this puts the data on the standard output and can be analyzed with the
  2522. * parseppm.c utility I wrote. That's a much faster way for figuring out how
  2523. * these data are scrambled.
  2524. */
  2525. /* Converts from raw, uncompressed segments at pIn0 to a YUV420P frame at pOut0.
  2526. *
  2527. * FIXME: Currently only handles width and height that are multiples of 16
  2528. */
  2529. static void
  2530. yuv420raw_to_yuv420p(struct ov511_frame *frame,
  2531. unsigned char *pIn0, unsigned char *pOut0)
  2532. {
  2533. int k, x, y;
  2534. unsigned char *pIn, *pOut, *pOutLine;
  2535. const unsigned int a = frame->rawwidth * frame->rawheight;
  2536. const unsigned int w = frame->rawwidth / 2;
  2537. /* Copy U and V */
  2538. pIn = pIn0;
  2539. pOutLine = pOut0 + a;
  2540. for (y = 0; y < frame->rawheight - 1; y += 16) {
  2541. pOut = pOutLine;
  2542. for (x = 0; x < frame->rawwidth - 1; x += 16) {
  2543. make_8x8(pIn, pOut, w);
  2544. make_8x8(pIn + 64, pOut + a/4, w);
  2545. pIn += 384;
  2546. pOut += 8;
  2547. }
  2548. pOutLine += 8 * w;
  2549. }
  2550. /* Copy Y */
  2551. pIn = pIn0 + 128;
  2552. pOutLine = pOut0;
  2553. k = 0;
  2554. for (y = 0; y < frame->rawheight - 1; y += 8) {
  2555. pOut = pOutLine;
  2556. for (x = 0; x < frame->rawwidth - 1; x += 8) {
  2557. make_8x8(pIn, pOut, frame->rawwidth);
  2558. pIn += 64;
  2559. pOut += 8;
  2560. if ((++k) > 3) {
  2561. k = 0;
  2562. pIn += 128;
  2563. }
  2564. }
  2565. pOutLine += 8 * frame->rawwidth;
  2566. }
  2567. }
  2568. /**********************************************************************
  2569. *
  2570. * Decompression
  2571. *
  2572. **********************************************************************/
  2573. static int
  2574. request_decompressor(struct usb_ov511 *ov)
  2575. {
  2576. if (ov->bclass == BCL_OV511 || ov->bclass == BCL_OV518) {
  2577. err("No decompressor available");
  2578. } else {
  2579. err("Unknown bridge");
  2580. }
  2581. return -ENOSYS;
  2582. }
  2583. static void
  2584. decompress(struct usb_ov511 *ov, struct ov511_frame *frame,
  2585. unsigned char *pIn0, unsigned char *pOut0)
  2586. {
  2587. if (!ov->decomp_ops)
  2588. if (request_decompressor(ov))
  2589. return;
  2590. }
  2591. /**********************************************************************
  2592. *
  2593. * Format conversion
  2594. *
  2595. **********************************************************************/
  2596. /* Fuses even and odd fields together, and doubles width.
  2597. * INPUT: an odd field followed by an even field at pIn0, in YUV planar format
  2598. * OUTPUT: a normal YUV planar image, with correct aspect ratio
  2599. */
  2600. static void
  2601. deinterlace(struct ov511_frame *frame, int rawformat,
  2602. unsigned char *pIn0, unsigned char *pOut0)
  2603. {
  2604. const int fieldheight = frame->rawheight / 2;
  2605. const int fieldpix = fieldheight * frame->rawwidth;
  2606. const int w = frame->width;
  2607. int x, y;
  2608. unsigned char *pInEven, *pInOdd, *pOut;
  2609. PDEBUG(5, "fieldheight=%d", fieldheight);
  2610. if (frame->rawheight != frame->height) {
  2611. err("invalid height");
  2612. return;
  2613. }
  2614. if ((frame->rawwidth * 2) != frame->width) {
  2615. err("invalid width");
  2616. return;
  2617. }
  2618. /* Y */
  2619. pInOdd = pIn0;
  2620. pInEven = pInOdd + fieldpix;
  2621. pOut = pOut0;
  2622. for (y = 0; y < fieldheight; y++) {
  2623. for (x = 0; x < frame->rawwidth; x++) {
  2624. *pOut = *pInEven;
  2625. *(pOut+1) = *pInEven++;
  2626. *(pOut+w) = *pInOdd;
  2627. *(pOut+w+1) = *pInOdd++;
  2628. pOut += 2;
  2629. }
  2630. pOut += w;
  2631. }
  2632. if (rawformat == RAWFMT_YUV420) {
  2633. /* U */
  2634. pInOdd = pIn0 + fieldpix * 2;
  2635. pInEven = pInOdd + fieldpix / 4;
  2636. for (y = 0; y < fieldheight / 2; y++) {
  2637. for (x = 0; x < frame->rawwidth / 2; x++) {
  2638. *pOut = *pInEven;
  2639. *(pOut+1) = *pInEven++;
  2640. *(pOut+w/2) = *pInOdd;
  2641. *(pOut+w/2+1) = *pInOdd++;
  2642. pOut += 2;
  2643. }
  2644. pOut += w/2;
  2645. }
  2646. /* V */
  2647. pInOdd = pIn0 + fieldpix * 2 + fieldpix / 2;
  2648. pInEven = pInOdd + fieldpix / 4;
  2649. for (y = 0; y < fieldheight / 2; y++) {
  2650. for (x = 0; x < frame->rawwidth / 2; x++) {
  2651. *pOut = *pInEven;
  2652. *(pOut+1) = *pInEven++;
  2653. *(pOut+w/2) = *pInOdd;
  2654. *(pOut+w/2+1) = *pInOdd++;
  2655. pOut += 2;
  2656. }
  2657. pOut += w/2;
  2658. }
  2659. }
  2660. }
  2661. static void
  2662. ov51x_postprocess_grey(struct usb_ov511 *ov, struct ov511_frame *frame)
  2663. {
  2664. /* Deinterlace frame, if necessary */
  2665. if (ov->sensor == SEN_SAA7111A && frame->rawheight >= 480) {
  2666. if (frame->compressed)
  2667. decompress(ov, frame, frame->rawdata,
  2668. frame->tempdata);
  2669. else
  2670. yuv400raw_to_yuv400p(frame, frame->rawdata,
  2671. frame->tempdata);
  2672. deinterlace(frame, RAWFMT_YUV400, frame->tempdata,
  2673. frame->data);
  2674. } else {
  2675. if (frame->compressed)
  2676. decompress(ov, frame, frame->rawdata,
  2677. frame->data);
  2678. else
  2679. yuv400raw_to_yuv400p(frame, frame->rawdata,
  2680. frame->data);
  2681. }
  2682. }
  2683. /* Process raw YUV420 data into standard YUV420P */
  2684. static void
  2685. ov51x_postprocess_yuv420(struct usb_ov511 *ov, struct ov511_frame *frame)
  2686. {
  2687. /* Deinterlace frame, if necessary */
  2688. if (ov->sensor == SEN_SAA7111A && frame->rawheight >= 480) {
  2689. if (frame->compressed)
  2690. decompress(ov, frame, frame->rawdata, frame->tempdata);
  2691. else
  2692. yuv420raw_to_yuv420p(frame, frame->rawdata,
  2693. frame->tempdata);
  2694. deinterlace(frame, RAWFMT_YUV420, frame->tempdata,
  2695. frame->data);
  2696. } else {
  2697. if (frame->compressed)
  2698. decompress(ov, frame, frame->rawdata, frame->data);
  2699. else
  2700. yuv420raw_to_yuv420p(frame, frame->rawdata,
  2701. frame->data);
  2702. }
  2703. }
  2704. /* Post-processes the specified frame. This consists of:
  2705. * 1. Decompress frame, if necessary
  2706. * 2. Deinterlace frame and scale to proper size, if necessary
  2707. * 3. Convert from YUV planar to destination format, if necessary
  2708. * 4. Fix the RGB offset, if necessary
  2709. */
  2710. static void
  2711. ov51x_postprocess(struct usb_ov511 *ov, struct ov511_frame *frame)
  2712. {
  2713. if (dumppix) {
  2714. memset(frame->data, 0,
  2715. MAX_DATA_SIZE(ov->maxwidth, ov->maxheight));
  2716. PDEBUG(4, "Dumping %d bytes", frame->bytes_recvd);
  2717. memcpy(frame->data, frame->rawdata, frame->bytes_recvd);
  2718. } else {
  2719. switch (frame->format) {
  2720. case VIDEO_PALETTE_GREY:
  2721. ov51x_postprocess_grey(ov, frame);
  2722. break;
  2723. case VIDEO_PALETTE_YUV420:
  2724. case VIDEO_PALETTE_YUV420P:
  2725. ov51x_postprocess_yuv420(ov, frame);
  2726. break;
  2727. default:
  2728. err("Cannot convert data to %s",
  2729. symbolic(v4l1_plist, frame->format));
  2730. }
  2731. }
  2732. }
  2733. /**********************************************************************
  2734. *
  2735. * OV51x data transfer, IRQ handler
  2736. *
  2737. **********************************************************************/
  2738. static inline void
  2739. ov511_move_data(struct usb_ov511 *ov, unsigned char *in, int n)
  2740. {
  2741. int num, offset;
  2742. int pnum = in[ov->packet_size - 1]; /* Get packet number */
  2743. int max_raw = MAX_RAW_DATA_SIZE(ov->maxwidth, ov->maxheight);
  2744. struct ov511_frame *frame = &ov->frame[ov->curframe];
  2745. struct timeval *ts;
  2746. /* SOF/EOF packets have 1st to 8th bytes zeroed and the 9th
  2747. * byte non-zero. The EOF packet has image width/height in the
  2748. * 10th and 11th bytes. The 9th byte is given as follows:
  2749. *
  2750. * bit 7: EOF
  2751. * 6: compression enabled
  2752. * 5: 422/420/400 modes
  2753. * 4: 422/420/400 modes
  2754. * 3: 1
  2755. * 2: snapshot button on
  2756. * 1: snapshot frame
  2757. * 0: even/odd field
  2758. */
  2759. if (printph) {
  2760. dev_info(&ov->dev->dev,
  2761. "ph(%3d): %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x\n",
  2762. pnum, in[0], in[1], in[2], in[3], in[4], in[5], in[6],
  2763. in[7], in[8], in[9], in[10], in[11]);
  2764. }
  2765. /* Check for SOF/EOF packet */
  2766. if ((in[0] | in[1] | in[2] | in[3] | in[4] | in[5] | in[6] | in[7]) ||
  2767. (~in[8] & 0x08))
  2768. goto check_middle;
  2769. /* Frame end */
  2770. if (in[8] & 0x80) {
  2771. ts = (struct timeval *)(frame->data
  2772. + MAX_FRAME_SIZE(ov->maxwidth, ov->maxheight));
  2773. do_gettimeofday(ts);
  2774. /* Get the actual frame size from the EOF header */
  2775. frame->rawwidth = ((int)(in[9]) + 1) * 8;
  2776. frame->rawheight = ((int)(in[10]) + 1) * 8;
  2777. PDEBUG(4, "Frame end, frame=%d, pnum=%d, w=%d, h=%d, recvd=%d",
  2778. ov->curframe, pnum, frame->rawwidth, frame->rawheight,
  2779. frame->bytes_recvd);
  2780. /* Validate the header data */
  2781. RESTRICT_TO_RANGE(frame->rawwidth, ov->minwidth, ov->maxwidth);
  2782. RESTRICT_TO_RANGE(frame->rawheight, ov->minheight,
  2783. ov->maxheight);
  2784. /* Don't allow byte count to exceed buffer size */
  2785. RESTRICT_TO_RANGE(frame->bytes_recvd, 8, max_raw);
  2786. if (frame->scanstate == STATE_LINES) {
  2787. int nextf;
  2788. frame->grabstate = FRAME_DONE;
  2789. wake_up_interruptible(&frame->wq);
  2790. /* If next frame is ready or grabbing,
  2791. * point to it */
  2792. nextf = (ov->curframe + 1) % OV511_NUMFRAMES;
  2793. if (ov->frame[nextf].grabstate == FRAME_READY
  2794. || ov->frame[nextf].grabstate == FRAME_GRABBING) {
  2795. ov->curframe = nextf;
  2796. ov->frame[nextf].scanstate = STATE_SCANNING;
  2797. } else {
  2798. if (frame->grabstate == FRAME_DONE) {
  2799. PDEBUG(4, "** Frame done **");
  2800. } else {
  2801. PDEBUG(4, "Frame not ready? state = %d",
  2802. ov->frame[nextf].grabstate);
  2803. }
  2804. ov->curframe = -1;
  2805. }
  2806. } else {
  2807. PDEBUG(5, "Frame done, but not scanning");
  2808. }
  2809. /* Image corruption caused by misplaced frame->segment = 0
  2810. * fixed by carlosf@conectiva.com.br
  2811. */
  2812. } else {
  2813. /* Frame start */
  2814. PDEBUG(4, "Frame start, framenum = %d", ov->curframe);
  2815. /* Check to see if it's a snapshot frame */
  2816. /* FIXME?? Should the snapshot reset go here? Performance? */
  2817. if (in[8] & 0x02) {
  2818. frame->snapshot = 1;
  2819. PDEBUG(3, "snapshot detected");
  2820. }
  2821. frame->scanstate = STATE_LINES;
  2822. frame->bytes_recvd = 0;
  2823. frame->compressed = in[8] & 0x40;
  2824. }
  2825. check_middle:
  2826. /* Are we in a frame? */
  2827. if (frame->scanstate != STATE_LINES) {
  2828. PDEBUG(5, "Not in a frame; packet skipped");
  2829. return;
  2830. }
  2831. /* If frame start, skip header */
  2832. if (frame->bytes_recvd == 0)
  2833. offset = 9;
  2834. else
  2835. offset = 0;
  2836. num = n - offset - 1;
  2837. /* Dump all data exactly as received */
  2838. if (dumppix == 2) {
  2839. frame->bytes_recvd += n - 1;
  2840. if (frame->bytes_recvd <= max_raw)
  2841. memcpy(frame->rawdata + frame->bytes_recvd - (n - 1),
  2842. in, n - 1);
  2843. else
  2844. PDEBUG(3, "Raw data buffer overrun!! (%d)",
  2845. frame->bytes_recvd - max_raw);
  2846. } else if (!frame->compressed && !remove_zeros) {
  2847. frame->bytes_recvd += num;
  2848. if (frame->bytes_recvd <= max_raw)
  2849. memcpy(frame->rawdata + frame->bytes_recvd - num,
  2850. in + offset, num);
  2851. else
  2852. PDEBUG(3, "Raw data buffer overrun!! (%d)",
  2853. frame->bytes_recvd - max_raw);
  2854. } else { /* Remove all-zero FIFO lines (aligned 32-byte blocks) */
  2855. int b, read = 0, allzero, copied = 0;
  2856. if (offset) {
  2857. frame->bytes_recvd += 32 - offset; // Bytes out
  2858. memcpy(frame->rawdata, in + offset, 32 - offset);
  2859. read += 32;
  2860. }
  2861. while (read < n - 1) {
  2862. allzero = 1;
  2863. for (b = 0; b < 32; b++) {
  2864. if (in[read + b]) {
  2865. allzero = 0;
  2866. break;
  2867. }
  2868. }
  2869. if (allzero) {
  2870. /* Don't copy it */
  2871. } else {
  2872. if (frame->bytes_recvd + copied + 32 <= max_raw)
  2873. {
  2874. memcpy(frame->rawdata
  2875. + frame->bytes_recvd + copied,
  2876. in + read, 32);
  2877. copied += 32;
  2878. } else {
  2879. PDEBUG(3, "Raw data buffer overrun!!");
  2880. }
  2881. }
  2882. read += 32;
  2883. }
  2884. frame->bytes_recvd += copied;
  2885. }
  2886. }
  2887. static inline void
  2888. ov518_move_data(struct usb_ov511 *ov, unsigned char *in, int n)
  2889. {
  2890. int max_raw = MAX_RAW_DATA_SIZE(ov->maxwidth, ov->maxheight);
  2891. struct ov511_frame *frame = &ov->frame[ov->curframe];
  2892. struct timeval *ts;
  2893. /* Don't copy the packet number byte */
  2894. if (ov->packet_numbering)
  2895. --n;
  2896. /* A false positive here is likely, until OVT gives me
  2897. * the definitive SOF/EOF format */
  2898. if ((!(in[0] | in[1] | in[2] | in[3] | in[5])) && in[6]) {
  2899. if (printph) {
  2900. dev_info(&ov->dev->dev,
  2901. "ph: %2x %2x %2x %2x %2x %2x %2x %2x\n",
  2902. in[0], in[1], in[2], in[3], in[4], in[5],
  2903. in[6], in[7]);
  2904. }
  2905. if (frame->scanstate == STATE_LINES) {
  2906. PDEBUG(4, "Detected frame end/start");
  2907. goto eof;
  2908. } else { //scanstate == STATE_SCANNING
  2909. /* Frame start */
  2910. PDEBUG(4, "Frame start, framenum = %d", ov->curframe);
  2911. goto sof;
  2912. }
  2913. } else {
  2914. goto check_middle;
  2915. }
  2916. eof:
  2917. ts = (struct timeval *)(frame->data
  2918. + MAX_FRAME_SIZE(ov->maxwidth, ov->maxheight));
  2919. do_gettimeofday(ts);
  2920. PDEBUG(4, "Frame end, curframe = %d, hw=%d, vw=%d, recvd=%d",
  2921. ov->curframe,
  2922. (int)(in[9]), (int)(in[10]), frame->bytes_recvd);
  2923. // FIXME: Since we don't know the header formats yet,
  2924. // there is no way to know what the actual image size is
  2925. frame->rawwidth = frame->width;
  2926. frame->rawheight = frame->height;
  2927. /* Validate the header data */
  2928. RESTRICT_TO_RANGE(frame->rawwidth, ov->minwidth, ov->maxwidth);
  2929. RESTRICT_TO_RANGE(frame->rawheight, ov->minheight, ov->maxheight);
  2930. /* Don't allow byte count to exceed buffer size */
  2931. RESTRICT_TO_RANGE(frame->bytes_recvd, 8, max_raw);
  2932. if (frame->scanstate == STATE_LINES) {
  2933. int nextf;
  2934. frame->grabstate = FRAME_DONE;
  2935. wake_up_interruptible(&frame->wq);
  2936. /* If next frame is ready or grabbing,
  2937. * point to it */
  2938. nextf = (ov->curframe + 1) % OV511_NUMFRAMES;
  2939. if (ov->frame[nextf].grabstate == FRAME_READY
  2940. || ov->frame[nextf].grabstate == FRAME_GRABBING) {
  2941. ov->curframe = nextf;
  2942. ov->frame[nextf].scanstate = STATE_SCANNING;
  2943. frame = &ov->frame[nextf];
  2944. } else {
  2945. if (frame->grabstate == FRAME_DONE) {
  2946. PDEBUG(4, "** Frame done **");
  2947. } else {
  2948. PDEBUG(4, "Frame not ready? state = %d",
  2949. ov->frame[nextf].grabstate);
  2950. }
  2951. ov->curframe = -1;
  2952. PDEBUG(4, "SOF dropped (no active frame)");
  2953. return; /* Nowhere to store this frame */
  2954. }
  2955. }
  2956. sof:
  2957. PDEBUG(4, "Starting capture on frame %d", frame->framenum);
  2958. // Snapshot not reverse-engineered yet.
  2959. #if 0
  2960. /* Check to see if it's a snapshot frame */
  2961. /* FIXME?? Should the snapshot reset go here? Performance? */
  2962. if (in[8] & 0x02) {
  2963. frame->snapshot = 1;
  2964. PDEBUG(3, "snapshot detected");
  2965. }
  2966. #endif
  2967. frame->scanstate = STATE_LINES;
  2968. frame->bytes_recvd = 0;
  2969. frame->compressed = 1;
  2970. check_middle:
  2971. /* Are we in a frame? */
  2972. if (frame->scanstate != STATE_LINES) {
  2973. PDEBUG(4, "scanstate: no SOF yet");
  2974. return;
  2975. }
  2976. /* Dump all data exactly as received */
  2977. if (dumppix == 2) {
  2978. frame->bytes_recvd += n;
  2979. if (frame->bytes_recvd <= max_raw)
  2980. memcpy(frame->rawdata + frame->bytes_recvd - n, in, n);
  2981. else
  2982. PDEBUG(3, "Raw data buffer overrun!! (%d)",
  2983. frame->bytes_recvd - max_raw);
  2984. } else {
  2985. /* All incoming data are divided into 8-byte segments. If the
  2986. * segment contains all zero bytes, it must be skipped. These
  2987. * zero-segments allow the OV518 to mainain a constant data rate
  2988. * regardless of the effectiveness of the compression. Segments
  2989. * are aligned relative to the beginning of each isochronous
  2990. * packet. The first segment in each image is a header (the
  2991. * decompressor skips it later).
  2992. */
  2993. int b, read = 0, allzero, copied = 0;
  2994. while (read < n) {
  2995. allzero = 1;
  2996. for (b = 0; b < 8; b++) {
  2997. if (in[read + b]) {
  2998. allzero = 0;
  2999. break;
  3000. }
  3001. }
  3002. if (allzero) {
  3003. /* Don't copy it */
  3004. } else {
  3005. if (frame->bytes_recvd + copied + 8 <= max_raw)
  3006. {
  3007. memcpy(frame->rawdata
  3008. + frame->bytes_recvd + copied,
  3009. in + read, 8);
  3010. copied += 8;
  3011. } else {
  3012. PDEBUG(3, "Raw data buffer overrun!!");
  3013. }
  3014. }
  3015. read += 8;
  3016. }
  3017. frame->bytes_recvd += copied;
  3018. }
  3019. }
  3020. static void
  3021. ov51x_isoc_irq(struct urb *urb)
  3022. {
  3023. int i;
  3024. struct usb_ov511 *ov;
  3025. struct ov511_sbuf *sbuf;
  3026. if (!urb->context) {
  3027. PDEBUG(4, "no context");
  3028. return;
  3029. }
  3030. sbuf = urb->context;
  3031. ov = sbuf->ov;
  3032. if (!ov || !ov->dev || !ov->user) {
  3033. PDEBUG(4, "no device, or not open");
  3034. return;
  3035. }
  3036. if (!ov->streaming) {
  3037. PDEBUG(4, "hmmm... not streaming, but got interrupt");
  3038. return;
  3039. }
  3040. if (urb->status == -ENOENT || urb->status == -ECONNRESET) {
  3041. PDEBUG(4, "URB unlinked");
  3042. return;
  3043. }
  3044. if (urb->status != -EINPROGRESS && urb->status != 0) {
  3045. err("ERROR: urb->status=%d: %s", urb->status,
  3046. symbolic(urb_errlist, urb->status));
  3047. }
  3048. /* Copy the data received into our frame buffer */
  3049. PDEBUG(5, "sbuf[%d]: Moving %d packets", sbuf->n,
  3050. urb->number_of_packets);
  3051. for (i = 0; i < urb->number_of_packets; i++) {
  3052. /* Warning: Don't call *_move_data() if no frame active! */
  3053. if (ov->curframe >= 0) {
  3054. int n = urb->iso_frame_desc[i].actual_length;
  3055. int st = urb->iso_frame_desc[i].status;
  3056. unsigned char *cdata;
  3057. urb->iso_frame_desc[i].actual_length = 0;
  3058. urb->iso_frame_desc[i].status = 0;
  3059. cdata = urb->transfer_buffer
  3060. + urb->iso_frame_desc[i].offset;
  3061. if (!n) {
  3062. PDEBUG(4, "Zero-length packet");
  3063. continue;
  3064. }
  3065. if (st)
  3066. PDEBUG(2, "data error: [%d] len=%d, status=%d",
  3067. i, n, st);
  3068. if (ov->bclass == BCL_OV511)
  3069. ov511_move_data(ov, cdata, n);
  3070. else if (ov->bclass == BCL_OV518)
  3071. ov518_move_data(ov, cdata, n);
  3072. else
  3073. err("Unknown bridge device (%d)", ov->bridge);
  3074. } else if (waitqueue_active(&ov->wq)) {
  3075. wake_up_interruptible(&ov->wq);
  3076. }
  3077. }
  3078. /* Resubmit this URB */
  3079. urb->dev = ov->dev;
  3080. if ((i = usb_submit_urb(urb, GFP_ATOMIC)) != 0)
  3081. err("usb_submit_urb() ret %d", i);
  3082. return;
  3083. }
  3084. /****************************************************************************
  3085. *
  3086. * Stream initialization and termination
  3087. *
  3088. ***************************************************************************/
  3089. static int
  3090. ov51x_init_isoc(struct usb_ov511 *ov)
  3091. {
  3092. struct urb *urb;
  3093. int fx, err, n, i, size;
  3094. PDEBUG(3, "*** Initializing capture ***");
  3095. ov->curframe = -1;
  3096. if (ov->bridge == BRG_OV511) {
  3097. if (cams == 1)
  3098. size = 993;
  3099. else if (cams == 2)
  3100. size = 513;
  3101. else if (cams == 3 || cams == 4)
  3102. size = 257;
  3103. else {
  3104. err("\"cams\" parameter too high!");
  3105. return -1;
  3106. }
  3107. } else if (ov->bridge == BRG_OV511PLUS) {
  3108. if (cams == 1)
  3109. size = 961;
  3110. else if (cams == 2)
  3111. size = 513;
  3112. else if (cams == 3 || cams == 4)
  3113. size = 257;
  3114. else if (cams >= 5 && cams <= 8)
  3115. size = 129;
  3116. else if (cams >= 9 && cams <= 31)
  3117. size = 33;
  3118. else {
  3119. err("\"cams\" parameter too high!");
  3120. return -1;
  3121. }
  3122. } else if (ov->bclass == BCL_OV518) {
  3123. if (cams == 1)
  3124. size = 896;
  3125. else if (cams == 2)
  3126. size = 512;
  3127. else if (cams == 3 || cams == 4)
  3128. size = 256;
  3129. else if (cams >= 5 && cams <= 8)
  3130. size = 128;
  3131. else {
  3132. err("\"cams\" parameter too high!");
  3133. return -1;
  3134. }
  3135. } else {
  3136. err("invalid bridge type");
  3137. return -1;
  3138. }
  3139. // FIXME: OV518 is hardcoded to 15 FPS (alternate 5) for now
  3140. if (ov->bclass == BCL_OV518) {
  3141. if (packetsize == -1) {
  3142. ov518_set_packet_size(ov, 640);
  3143. } else {
  3144. dev_info(&ov->dev->dev, "Forcing packet size to %d\n",
  3145. packetsize);
  3146. ov518_set_packet_size(ov, packetsize);
  3147. }
  3148. } else {
  3149. if (packetsize == -1) {
  3150. ov511_set_packet_size(ov, size);
  3151. } else {
  3152. dev_info(&ov->dev->dev, "Forcing packet size to %d\n",
  3153. packetsize);
  3154. ov511_set_packet_size(ov, packetsize);
  3155. }
  3156. }
  3157. for (n = 0; n < OV511_NUMSBUF; n++) {
  3158. urb = usb_alloc_urb(FRAMES_PER_DESC, GFP_KERNEL);
  3159. if (!urb) {
  3160. err("init isoc: usb_alloc_urb ret. NULL");
  3161. for (i = 0; i < n; i++)
  3162. usb_free_urb(ov->sbuf[i].urb);
  3163. return -ENOMEM;
  3164. }
  3165. ov->sbuf[n].urb = urb;
  3166. urb->dev = ov->dev;
  3167. urb->context = &ov->sbuf[n];
  3168. urb->pipe = usb_rcvisocpipe(ov->dev, OV511_ENDPOINT_ADDRESS);
  3169. urb->transfer_flags = URB_ISO_ASAP;
  3170. urb->transfer_buffer = ov->sbuf[n].data;
  3171. urb->complete = ov51x_isoc_irq;
  3172. urb->number_of_packets = FRAMES_PER_DESC;
  3173. urb->transfer_buffer_length = ov->packet_size * FRAMES_PER_DESC;
  3174. urb->interval = 1;
  3175. for (fx = 0; fx < FRAMES_PER_DESC; fx++) {
  3176. urb->iso_frame_desc[fx].offset = ov->packet_size * fx;
  3177. urb->iso_frame_desc[fx].length = ov->packet_size;
  3178. }
  3179. }
  3180. ov->streaming = 1;
  3181. for (n = 0; n < OV511_NUMSBUF; n++) {
  3182. ov->sbuf[n].urb->dev = ov->dev;
  3183. err = usb_submit_urb(ov->sbuf[n].urb, GFP_KERNEL);
  3184. if (err) {
  3185. err("init isoc: usb_submit_urb(%d) ret %d", n, err);
  3186. return err;
  3187. }
  3188. }
  3189. return 0;
  3190. }
  3191. static void
  3192. ov51x_unlink_isoc(struct usb_ov511 *ov)
  3193. {
  3194. int n;
  3195. /* Unschedule all of the iso td's */
  3196. for (n = OV511_NUMSBUF - 1; n >= 0; n--) {
  3197. if (ov->sbuf[n].urb) {
  3198. usb_kill_urb(ov->sbuf[n].urb);
  3199. usb_free_urb(ov->sbuf[n].urb);
  3200. ov->sbuf[n].urb = NULL;
  3201. }
  3202. }
  3203. }
  3204. static void
  3205. ov51x_stop_isoc(struct usb_ov511 *ov)
  3206. {
  3207. if (!ov->streaming || !ov->dev)
  3208. return;
  3209. PDEBUG(3, "*** Stopping capture ***");
  3210. if (ov->bclass == BCL_OV518)
  3211. ov518_set_packet_size(ov, 0);
  3212. else
  3213. ov511_set_packet_size(ov, 0);
  3214. ov->streaming = 0;
  3215. ov51x_unlink_isoc(ov);
  3216. }
  3217. static int
  3218. ov51x_new_frame(struct usb_ov511 *ov, int framenum)
  3219. {
  3220. struct ov511_frame *frame;
  3221. int newnum;
  3222. PDEBUG(4, "ov->curframe = %d, framenum = %d", ov->curframe, framenum);
  3223. if (!ov->dev)
  3224. return -1;
  3225. /* If we're not grabbing a frame right now and the other frame is */
  3226. /* ready to be grabbed into, then use it instead */
  3227. if (ov->curframe == -1) {
  3228. newnum = (framenum - 1 + OV511_NUMFRAMES) % OV511_NUMFRAMES;
  3229. if (ov->frame[newnum].grabstate == FRAME_READY)
  3230. framenum = newnum;
  3231. } else
  3232. return 0;
  3233. frame = &ov->frame[framenum];
  3234. PDEBUG(4, "framenum = %d, width = %d, height = %d", framenum,
  3235. frame->width, frame->height);
  3236. frame->grabstate = FRAME_GRABBING;
  3237. frame->scanstate = STATE_SCANNING;
  3238. frame->snapshot = 0;
  3239. ov->curframe = framenum;
  3240. /* Make sure it's not too big */
  3241. if (frame->width > ov->maxwidth)
  3242. frame->width = ov->maxwidth;
  3243. frame->width &= ~7L; /* Multiple of 8 */
  3244. if (frame->height > ov->maxheight)
  3245. frame->height = ov->maxheight;
  3246. frame->height &= ~3L; /* Multiple of 4 */
  3247. return 0;
  3248. }
  3249. /****************************************************************************
  3250. *
  3251. * Buffer management
  3252. *
  3253. ***************************************************************************/
  3254. /*
  3255. * - You must acquire buf_lock before entering this function.
  3256. * - Because this code will free any non-null pointer, you must be sure to null
  3257. * them if you explicitly free them somewhere else!
  3258. */
  3259. static void
  3260. ov51x_do_dealloc(struct usb_ov511 *ov)
  3261. {
  3262. int i;
  3263. PDEBUG(4, "entered");
  3264. if (ov->fbuf) {
  3265. rvfree(ov->fbuf, OV511_NUMFRAMES
  3266. * MAX_DATA_SIZE(ov->maxwidth, ov->maxheight));
  3267. ov->fbuf = NULL;
  3268. }
  3269. vfree(ov->rawfbuf);
  3270. ov->rawfbuf = NULL;
  3271. vfree(ov->tempfbuf);
  3272. ov->tempfbuf = NULL;
  3273. for (i = 0; i < OV511_NUMSBUF; i++) {
  3274. kfree(ov->sbuf[i].data);
  3275. ov->sbuf[i].data = NULL;
  3276. }
  3277. for (i = 0; i < OV511_NUMFRAMES; i++) {
  3278. ov->frame[i].data = NULL;
  3279. ov->frame[i].rawdata = NULL;
  3280. ov->frame[i].tempdata = NULL;
  3281. if (ov->frame[i].compbuf) {
  3282. free_page((unsigned long) ov->frame[i].compbuf);
  3283. ov->frame[i].compbuf = NULL;
  3284. }
  3285. }
  3286. PDEBUG(4, "buffer memory deallocated");
  3287. ov->buf_state = BUF_NOT_ALLOCATED;
  3288. PDEBUG(4, "leaving");
  3289. }
  3290. static int
  3291. ov51x_alloc(struct usb_ov511 *ov)
  3292. {
  3293. int i;
  3294. const int w = ov->maxwidth;
  3295. const int h = ov->maxheight;
  3296. const int data_bufsize = OV511_NUMFRAMES * MAX_DATA_SIZE(w, h);
  3297. const int raw_bufsize = OV511_NUMFRAMES * MAX_RAW_DATA_SIZE(w, h);
  3298. PDEBUG(4, "entered");
  3299. mutex_lock(&ov->buf_lock);
  3300. if (ov->buf_state == BUF_ALLOCATED)
  3301. goto out;
  3302. ov->fbuf = rvmalloc(data_bufsize);
  3303. if (!ov->fbuf)
  3304. goto error;
  3305. ov->rawfbuf = vmalloc(raw_bufsize);
  3306. if (!ov->rawfbuf)
  3307. goto error;
  3308. memset(ov->rawfbuf, 0, raw_bufsize);
  3309. ov->tempfbuf = vmalloc(raw_bufsize);
  3310. if (!ov->tempfbuf)
  3311. goto error;
  3312. memset(ov->tempfbuf, 0, raw_bufsize);
  3313. for (i = 0; i < OV511_NUMSBUF; i++) {
  3314. ov->sbuf[i].data = kmalloc(FRAMES_PER_DESC *
  3315. MAX_FRAME_SIZE_PER_DESC, GFP_KERNEL);
  3316. if (!ov->sbuf[i].data)
  3317. goto error;
  3318. PDEBUG(4, "sbuf[%d] @ %p", i, ov->sbuf[i].data);
  3319. }
  3320. for (i = 0; i < OV511_NUMFRAMES; i++) {
  3321. ov->frame[i].data = ov->fbuf + i * MAX_DATA_SIZE(w, h);
  3322. ov->frame[i].rawdata = ov->rawfbuf
  3323. + i * MAX_RAW_DATA_SIZE(w, h);
  3324. ov->frame[i].tempdata = ov->tempfbuf
  3325. + i * MAX_RAW_DATA_SIZE(w, h);
  3326. ov->frame[i].compbuf =
  3327. (unsigned char *) __get_free_page(GFP_KERNEL);
  3328. if (!ov->frame[i].compbuf)
  3329. goto error;
  3330. PDEBUG(4, "frame[%d] @ %p", i, ov->frame[i].data);
  3331. }
  3332. ov->buf_state = BUF_ALLOCATED;
  3333. out:
  3334. mutex_unlock(&ov->buf_lock);
  3335. PDEBUG(4, "leaving");
  3336. return 0;
  3337. error:
  3338. ov51x_do_dealloc(ov);
  3339. mutex_unlock(&ov->buf_lock);
  3340. PDEBUG(4, "errored");
  3341. return -ENOMEM;
  3342. }
  3343. static void
  3344. ov51x_dealloc(struct usb_ov511 *ov)
  3345. {
  3346. PDEBUG(4, "entered");
  3347. mutex_lock(&ov->buf_lock);
  3348. ov51x_do_dealloc(ov);
  3349. mutex_unlock(&ov->buf_lock);
  3350. PDEBUG(4, "leaving");
  3351. }
  3352. /****************************************************************************
  3353. *
  3354. * V4L 1 API
  3355. *
  3356. ***************************************************************************/
  3357. static int
  3358. ov51x_v4l1_open(struct file *file)
  3359. {
  3360. struct video_device *vdev = video_devdata(file);
  3361. struct usb_ov511 *ov = video_get_drvdata(vdev);
  3362. int err, i;
  3363. PDEBUG(4, "opening");
  3364. mutex_lock(&ov->lock);
  3365. err = -EBUSY;
  3366. if (ov->user)
  3367. goto out;
  3368. ov->sub_flag = 0;
  3369. /* In case app doesn't set them... */
  3370. err = ov51x_set_default_params(ov);
  3371. if (err < 0)
  3372. goto out;
  3373. /* Make sure frames are reset */
  3374. for (i = 0; i < OV511_NUMFRAMES; i++) {
  3375. ov->frame[i].grabstate = FRAME_UNUSED;
  3376. ov->frame[i].bytes_read = 0;
  3377. }
  3378. /* If compression is on, make sure now that a
  3379. * decompressor can be loaded */
  3380. if (ov->compress && !ov->decomp_ops) {
  3381. err = request_decompressor(ov);
  3382. if (err && !dumppix)
  3383. goto out;
  3384. }
  3385. err = ov51x_alloc(ov);
  3386. if (err < 0)
  3387. goto out;
  3388. err = ov51x_init_isoc(ov);
  3389. if (err) {
  3390. ov51x_dealloc(ov);
  3391. goto out;
  3392. }
  3393. ov->user++;
  3394. file->private_data = vdev;
  3395. if (ov->led_policy == LED_AUTO)
  3396. ov51x_led_control(ov, 1);
  3397. out:
  3398. mutex_unlock(&ov->lock);
  3399. return err;
  3400. }
  3401. static int
  3402. ov51x_v4l1_close(struct file *file)
  3403. {
  3404. struct video_device *vdev = file->private_data;
  3405. struct usb_ov511 *ov = video_get_drvdata(vdev);
  3406. PDEBUG(4, "ov511_close");
  3407. mutex_lock(&ov->lock);
  3408. ov->user--;
  3409. ov51x_stop_isoc(ov);
  3410. if (ov->led_policy == LED_AUTO)
  3411. ov51x_led_control(ov, 0);
  3412. if (ov->dev)
  3413. ov51x_dealloc(ov);
  3414. mutex_unlock(&ov->lock);
  3415. /* Device unplugged while open. Only a minimum of unregistration is done
  3416. * here; the disconnect callback already did the rest. */
  3417. if (!ov->dev) {
  3418. mutex_lock(&ov->cbuf_lock);
  3419. kfree(ov->cbuf);
  3420. ov->cbuf = NULL;
  3421. mutex_unlock(&ov->cbuf_lock);
  3422. ov51x_dealloc(ov);
  3423. kfree(ov);
  3424. ov = NULL;
  3425. }
  3426. file->private_data = NULL;
  3427. return 0;
  3428. }
  3429. /* Do not call this function directly! */
  3430. static long
  3431. ov51x_v4l1_ioctl_internal(struct file *file, unsigned int cmd, void *arg)
  3432. {
  3433. struct video_device *vdev = file->private_data;
  3434. struct usb_ov511 *ov = video_get_drvdata(vdev);
  3435. PDEBUG(5, "IOCtl: 0x%X", cmd);
  3436. if (!ov->dev)
  3437. return -EIO;
  3438. switch (cmd) {
  3439. case VIDIOCGCAP:
  3440. {
  3441. struct video_capability *b = arg;
  3442. PDEBUG(4, "VIDIOCGCAP");
  3443. memset(b, 0, sizeof(struct video_capability));
  3444. sprintf(b->name, "%s USB Camera",
  3445. symbolic(brglist, ov->bridge));
  3446. b->type = VID_TYPE_CAPTURE | VID_TYPE_SUBCAPTURE;
  3447. b->channels = ov->num_inputs;
  3448. b->audios = 0;
  3449. b->maxwidth = ov->maxwidth;
  3450. b->maxheight = ov->maxheight;
  3451. b->minwidth = ov->minwidth;
  3452. b->minheight = ov->minheight;
  3453. return 0;
  3454. }
  3455. case VIDIOCGCHAN:
  3456. {
  3457. struct video_channel *v = arg;
  3458. PDEBUG(4, "VIDIOCGCHAN");
  3459. if ((unsigned)(v->channel) >= ov->num_inputs) {
  3460. err("Invalid channel (%d)", v->channel);
  3461. return -EINVAL;
  3462. }
  3463. v->norm = ov->norm;
  3464. v->type = VIDEO_TYPE_CAMERA;
  3465. v->flags = 0;
  3466. // v->flags |= (ov->has_decoder) ? VIDEO_VC_NORM : 0;
  3467. v->tuners = 0;
  3468. decoder_get_input_name(ov, v->channel, v->name);
  3469. return 0;
  3470. }
  3471. case VIDIOCSCHAN:
  3472. {
  3473. struct video_channel *v = arg;
  3474. int err;
  3475. PDEBUG(4, "VIDIOCSCHAN");
  3476. /* Make sure it's not a camera */
  3477. if (!ov->has_decoder) {
  3478. if (v->channel == 0)
  3479. return 0;
  3480. else
  3481. return -EINVAL;
  3482. }
  3483. if (v->norm != VIDEO_MODE_PAL &&
  3484. v->norm != VIDEO_MODE_NTSC &&
  3485. v->norm != VIDEO_MODE_SECAM &&
  3486. v->norm != VIDEO_MODE_AUTO) {
  3487. err("Invalid norm (%d)", v->norm);
  3488. return -EINVAL;
  3489. }
  3490. if ((unsigned)(v->channel) >= ov->num_inputs) {
  3491. err("Invalid channel (%d)", v->channel);
  3492. return -EINVAL;
  3493. }
  3494. err = decoder_set_input(ov, v->channel);
  3495. if (err)
  3496. return err;
  3497. err = decoder_set_norm(ov, v->norm);
  3498. if (err)
  3499. return err;
  3500. return 0;
  3501. }
  3502. case VIDIOCGPICT:
  3503. {
  3504. struct video_picture *p = arg;
  3505. PDEBUG(4, "VIDIOCGPICT");
  3506. memset(p, 0, sizeof(struct video_picture));
  3507. if (sensor_get_picture(ov, p))
  3508. return -EIO;
  3509. /* Can we get these from frame[0]? -claudio? */
  3510. p->depth = ov->frame[0].depth;
  3511. p->palette = ov->frame[0].format;
  3512. return 0;
  3513. }
  3514. case VIDIOCSPICT:
  3515. {
  3516. struct video_picture *p = arg;
  3517. int i, rc;
  3518. PDEBUG(4, "VIDIOCSPICT");
  3519. if (!get_depth(p->palette))
  3520. return -EINVAL;
  3521. if (sensor_set_picture(ov, p))
  3522. return -EIO;
  3523. if (force_palette && p->palette != force_palette) {
  3524. dev_info(&ov->dev->dev, "Palette rejected (%s)\n",
  3525. symbolic(v4l1_plist, p->palette));
  3526. return -EINVAL;
  3527. }
  3528. // FIXME: Format should be independent of frames
  3529. if (p->palette != ov->frame[0].format) {
  3530. PDEBUG(4, "Detected format change");
  3531. rc = ov51x_wait_frames_inactive(ov);
  3532. if (rc)
  3533. return rc;
  3534. mode_init_regs(ov, ov->frame[0].width,
  3535. ov->frame[0].height, p->palette, ov->sub_flag);
  3536. }
  3537. PDEBUG(4, "Setting depth=%d, palette=%s",
  3538. p->depth, symbolic(v4l1_plist, p->palette));
  3539. for (i = 0; i < OV511_NUMFRAMES; i++) {
  3540. ov->frame[i].depth = p->depth;
  3541. ov->frame[i].format = p->palette;
  3542. }
  3543. return 0;
  3544. }
  3545. case VIDIOCGCAPTURE:
  3546. {
  3547. int *vf = arg;
  3548. PDEBUG(4, "VIDIOCGCAPTURE");
  3549. ov->sub_flag = *vf;
  3550. return 0;
  3551. }
  3552. case VIDIOCSCAPTURE:
  3553. {
  3554. struct video_capture *vc = arg;
  3555. PDEBUG(4, "VIDIOCSCAPTURE");
  3556. if (vc->flags)
  3557. return -EINVAL;
  3558. if (vc->decimation)
  3559. return -EINVAL;
  3560. vc->x &= ~3L;
  3561. vc->y &= ~1L;
  3562. vc->y &= ~31L;
  3563. if (vc->width == 0)
  3564. vc->width = 32;
  3565. vc->height /= 16;
  3566. vc->height *= 16;
  3567. if (vc->height == 0)
  3568. vc->height = 16;
  3569. ov->subx = vc->x;
  3570. ov->suby = vc->y;
  3571. ov->subw = vc->width;
  3572. ov->subh = vc->height;
  3573. return 0;
  3574. }
  3575. case VIDIOCSWIN:
  3576. {
  3577. struct video_window *vw = arg;
  3578. int i, rc;
  3579. PDEBUG(4, "VIDIOCSWIN: %dx%d", vw->width, vw->height);
  3580. #if 0
  3581. if (vw->flags)
  3582. return -EINVAL;
  3583. if (vw->clipcount)
  3584. return -EINVAL;
  3585. if (vw->height != ov->maxheight)
  3586. return -EINVAL;
  3587. if (vw->width != ov->maxwidth)
  3588. return -EINVAL;
  3589. #endif
  3590. rc = ov51x_wait_frames_inactive(ov);
  3591. if (rc)
  3592. return rc;
  3593. rc = mode_init_regs(ov, vw->width, vw->height,
  3594. ov->frame[0].format, ov->sub_flag);
  3595. if (rc < 0)
  3596. return rc;
  3597. for (i = 0; i < OV511_NUMFRAMES; i++) {
  3598. ov->frame[i].width = vw->width;
  3599. ov->frame[i].height = vw->height;
  3600. }
  3601. return 0;
  3602. }
  3603. case VIDIOCGWIN:
  3604. {
  3605. struct video_window *vw = arg;
  3606. memset(vw, 0, sizeof(struct video_window));
  3607. vw->x = 0; /* FIXME */
  3608. vw->y = 0;
  3609. vw->width = ov->frame[0].width;
  3610. vw->height = ov->frame[0].height;
  3611. vw->flags = 30;
  3612. PDEBUG(4, "VIDIOCGWIN: %dx%d", vw->width, vw->height);
  3613. return 0;
  3614. }
  3615. case VIDIOCGMBUF:
  3616. {
  3617. struct video_mbuf *vm = arg;
  3618. int i;
  3619. PDEBUG(4, "VIDIOCGMBUF");
  3620. memset(vm, 0, sizeof(struct video_mbuf));
  3621. vm->size = OV511_NUMFRAMES
  3622. * MAX_DATA_SIZE(ov->maxwidth, ov->maxheight);
  3623. vm->frames = OV511_NUMFRAMES;
  3624. vm->offsets[0] = 0;
  3625. for (i = 1; i < OV511_NUMFRAMES; i++) {
  3626. vm->offsets[i] = vm->offsets[i-1]
  3627. + MAX_DATA_SIZE(ov->maxwidth, ov->maxheight);
  3628. }
  3629. return 0;
  3630. }
  3631. case VIDIOCMCAPTURE:
  3632. {
  3633. struct video_mmap *vm = arg;
  3634. int rc, depth;
  3635. unsigned int f = vm->frame;
  3636. PDEBUG(4, "VIDIOCMCAPTURE: frame: %d, %dx%d, %s", f, vm->width,
  3637. vm->height, symbolic(v4l1_plist, vm->format));
  3638. depth = get_depth(vm->format);
  3639. if (!depth) {
  3640. PDEBUG(2, "VIDIOCMCAPTURE: invalid format (%s)",
  3641. symbolic(v4l1_plist, vm->format));
  3642. return -EINVAL;
  3643. }
  3644. if (f >= OV511_NUMFRAMES) {
  3645. err("VIDIOCMCAPTURE: invalid frame (%d)", f);
  3646. return -EINVAL;
  3647. }
  3648. if (vm->width > ov->maxwidth
  3649. || vm->height > ov->maxheight) {
  3650. err("VIDIOCMCAPTURE: requested dimensions too big");
  3651. return -EINVAL;
  3652. }
  3653. if (ov->frame[f].grabstate == FRAME_GRABBING) {
  3654. PDEBUG(4, "VIDIOCMCAPTURE: already grabbing");
  3655. return -EBUSY;
  3656. }
  3657. if (force_palette && (vm->format != force_palette)) {
  3658. PDEBUG(2, "palette rejected (%s)",
  3659. symbolic(v4l1_plist, vm->format));
  3660. return -EINVAL;
  3661. }
  3662. if ((ov->frame[f].width != vm->width) ||
  3663. (ov->frame[f].height != vm->height) ||
  3664. (ov->frame[f].format != vm->format) ||
  3665. (ov->frame[f].sub_flag != ov->sub_flag) ||
  3666. (ov->frame[f].depth != depth)) {
  3667. PDEBUG(4, "VIDIOCMCAPTURE: change in image parameters");
  3668. rc = ov51x_wait_frames_inactive(ov);
  3669. if (rc)
  3670. return rc;
  3671. rc = mode_init_regs(ov, vm->width, vm->height,
  3672. vm->format, ov->sub_flag);
  3673. #if 0
  3674. if (rc < 0) {
  3675. PDEBUG(1, "Got error while initializing regs ");
  3676. return ret;
  3677. }
  3678. #endif
  3679. ov->frame[f].width = vm->width;
  3680. ov->frame[f].height = vm->height;
  3681. ov->frame[f].format = vm->format;
  3682. ov->frame[f].sub_flag = ov->sub_flag;
  3683. ov->frame[f].depth = depth;
  3684. }
  3685. /* Mark it as ready */
  3686. ov->frame[f].grabstate = FRAME_READY;
  3687. PDEBUG(4, "VIDIOCMCAPTURE: renewing frame %d", f);
  3688. return ov51x_new_frame(ov, f);
  3689. }
  3690. case VIDIOCSYNC:
  3691. {
  3692. unsigned int fnum = *((unsigned int *) arg);
  3693. struct ov511_frame *frame;
  3694. int rc;
  3695. if (fnum >= OV511_NUMFRAMES) {
  3696. err("VIDIOCSYNC: invalid frame (%d)", fnum);
  3697. return -EINVAL;
  3698. }
  3699. frame = &ov->frame[fnum];
  3700. PDEBUG(4, "syncing to frame %d, grabstate = %d", fnum,
  3701. frame->grabstate);
  3702. switch (frame->grabstate) {
  3703. case FRAME_UNUSED:
  3704. return -EINVAL;
  3705. case FRAME_READY:
  3706. case FRAME_GRABBING:
  3707. case FRAME_ERROR:
  3708. redo:
  3709. if (!ov->dev)
  3710. return -EIO;
  3711. rc = wait_event_interruptible(frame->wq,
  3712. (frame->grabstate == FRAME_DONE)
  3713. || (frame->grabstate == FRAME_ERROR));
  3714. if (rc)
  3715. return rc;
  3716. if (frame->grabstate == FRAME_ERROR) {
  3717. if ((rc = ov51x_new_frame(ov, fnum)) < 0)
  3718. return rc;
  3719. goto redo;
  3720. }
  3721. /* Fall through */
  3722. case FRAME_DONE:
  3723. if (ov->snap_enabled && !frame->snapshot) {
  3724. if ((rc = ov51x_new_frame(ov, fnum)) < 0)
  3725. return rc;
  3726. goto redo;
  3727. }
  3728. frame->grabstate = FRAME_UNUSED;
  3729. /* Reset the hardware snapshot button */
  3730. /* FIXME - Is this the best place for this? */
  3731. if ((ov->snap_enabled) && (frame->snapshot)) {
  3732. frame->snapshot = 0;
  3733. ov51x_clear_snapshot(ov);
  3734. }
  3735. /* Decompression, format conversion, etc... */
  3736. ov51x_postprocess(ov, frame);
  3737. break;
  3738. } /* end switch */
  3739. return 0;
  3740. }
  3741. case VIDIOCGFBUF:
  3742. {
  3743. struct video_buffer *vb = arg;
  3744. PDEBUG(4, "VIDIOCGFBUF");
  3745. memset(vb, 0, sizeof(struct video_buffer));
  3746. return 0;
  3747. }
  3748. case VIDIOCGUNIT:
  3749. {
  3750. struct video_unit *vu = arg;
  3751. PDEBUG(4, "VIDIOCGUNIT");
  3752. memset(vu, 0, sizeof(struct video_unit));
  3753. vu->video = ov->vdev->minor;
  3754. vu->vbi = VIDEO_NO_UNIT;
  3755. vu->radio = VIDEO_NO_UNIT;
  3756. vu->audio = VIDEO_NO_UNIT;
  3757. vu->teletext = VIDEO_NO_UNIT;
  3758. return 0;
  3759. }
  3760. case OV511IOC_WI2C:
  3761. {
  3762. struct ov511_i2c_struct *w = arg;
  3763. return i2c_w_slave(ov, w->slave, w->reg, w->value, w->mask);
  3764. }
  3765. case OV511IOC_RI2C:
  3766. {
  3767. struct ov511_i2c_struct *r = arg;
  3768. int rc;
  3769. rc = i2c_r_slave(ov, r->slave, r->reg);
  3770. if (rc < 0)
  3771. return rc;
  3772. r->value = rc;
  3773. return 0;
  3774. }
  3775. default:
  3776. PDEBUG(3, "Unsupported IOCtl: 0x%X", cmd);
  3777. return -ENOIOCTLCMD;
  3778. } /* end switch */
  3779. return 0;
  3780. }
  3781. static long
  3782. ov51x_v4l1_ioctl(struct file *file,
  3783. unsigned int cmd, unsigned long arg)
  3784. {
  3785. struct video_device *vdev = file->private_data;
  3786. struct usb_ov511 *ov = video_get_drvdata(vdev);
  3787. int rc;
  3788. if (mutex_lock_interruptible(&ov->lock))
  3789. return -EINTR;
  3790. rc = video_usercopy(file, cmd, arg, ov51x_v4l1_ioctl_internal);
  3791. mutex_unlock(&ov->lock);
  3792. return rc;
  3793. }
  3794. static ssize_t
  3795. ov51x_v4l1_read(struct file *file, char __user *buf, size_t cnt, loff_t *ppos)
  3796. {
  3797. struct video_device *vdev = file->private_data;
  3798. int noblock = file->f_flags&O_NONBLOCK;
  3799. unsigned long count = cnt;
  3800. struct usb_ov511 *ov = video_get_drvdata(vdev);
  3801. int i, rc = 0, frmx = -1;
  3802. struct ov511_frame *frame;
  3803. if (mutex_lock_interruptible(&ov->lock))
  3804. return -EINTR;
  3805. PDEBUG(4, "%ld bytes, noblock=%d", count, noblock);
  3806. if (!vdev || !buf) {
  3807. rc = -EFAULT;
  3808. goto error;
  3809. }
  3810. if (!ov->dev) {
  3811. rc = -EIO;
  3812. goto error;
  3813. }
  3814. // FIXME: Only supports two frames
  3815. /* See if a frame is completed, then use it. */
  3816. if (ov->frame[0].grabstate >= FRAME_DONE) /* _DONE or _ERROR */
  3817. frmx = 0;
  3818. else if (ov->frame[1].grabstate >= FRAME_DONE)/* _DONE or _ERROR */
  3819. frmx = 1;
  3820. /* If nonblocking we return immediately */
  3821. if (noblock && (frmx == -1)) {
  3822. rc = -EAGAIN;
  3823. goto error;
  3824. }
  3825. /* If no FRAME_DONE, look for a FRAME_GRABBING state. */
  3826. /* See if a frame is in process (grabbing), then use it. */
  3827. if (frmx == -1) {
  3828. if (ov->frame[0].grabstate == FRAME_GRABBING)
  3829. frmx = 0;
  3830. else if (ov->frame[1].grabstate == FRAME_GRABBING)
  3831. frmx = 1;
  3832. }
  3833. /* If no frame is active, start one. */
  3834. if (frmx == -1) {
  3835. if ((rc = ov51x_new_frame(ov, frmx = 0))) {
  3836. err("read: ov51x_new_frame error");
  3837. goto error;
  3838. }
  3839. }
  3840. frame = &ov->frame[frmx];
  3841. restart:
  3842. if (!ov->dev) {
  3843. rc = -EIO;
  3844. goto error;
  3845. }
  3846. /* Wait while we're grabbing the image */
  3847. PDEBUG(4, "Waiting image grabbing");
  3848. rc = wait_event_interruptible(frame->wq,
  3849. (frame->grabstate == FRAME_DONE)
  3850. || (frame->grabstate == FRAME_ERROR));
  3851. if (rc)
  3852. goto error;
  3853. PDEBUG(4, "Got image, frame->grabstate = %d", frame->grabstate);
  3854. PDEBUG(4, "bytes_recvd = %d", frame->bytes_recvd);
  3855. if (frame->grabstate == FRAME_ERROR) {
  3856. frame->bytes_read = 0;
  3857. err("** ick! ** Errored frame %d", ov->curframe);
  3858. if (ov51x_new_frame(ov, frmx)) {
  3859. err("read: ov51x_new_frame error");
  3860. goto error;
  3861. }
  3862. goto restart;
  3863. }
  3864. /* Repeat until we get a snapshot frame */
  3865. if (ov->snap_enabled)
  3866. PDEBUG(4, "Waiting snapshot frame");
  3867. if (ov->snap_enabled && !frame->snapshot) {
  3868. frame->bytes_read = 0;
  3869. if ((rc = ov51x_new_frame(ov, frmx))) {
  3870. err("read: ov51x_new_frame error");
  3871. goto error;
  3872. }
  3873. goto restart;
  3874. }
  3875. /* Clear the snapshot */
  3876. if (ov->snap_enabled && frame->snapshot) {
  3877. frame->snapshot = 0;
  3878. ov51x_clear_snapshot(ov);
  3879. }
  3880. /* Decompression, format conversion, etc... */
  3881. ov51x_postprocess(ov, frame);
  3882. PDEBUG(4, "frmx=%d, bytes_read=%ld, length=%ld", frmx,
  3883. frame->bytes_read,
  3884. get_frame_length(frame));
  3885. /* copy bytes to user space; we allow for partials reads */
  3886. // if ((count + frame->bytes_read)
  3887. // > get_frame_length((struct ov511_frame *)frame))
  3888. // count = frame->scanlength - frame->bytes_read;
  3889. /* FIXME - count hardwired to be one frame... */
  3890. count = get_frame_length(frame);
  3891. PDEBUG(4, "Copy to user space: %ld bytes", count);
  3892. if ((i = copy_to_user(buf, frame->data + frame->bytes_read, count))) {
  3893. PDEBUG(4, "Copy failed! %d bytes not copied", i);
  3894. rc = -EFAULT;
  3895. goto error;
  3896. }
  3897. frame->bytes_read += count;
  3898. PDEBUG(4, "{copy} count used=%ld, new bytes_read=%ld",
  3899. count, frame->bytes_read);
  3900. /* If all data have been read... */
  3901. if (frame->bytes_read
  3902. >= get_frame_length(frame)) {
  3903. frame->bytes_read = 0;
  3904. // FIXME: Only supports two frames
  3905. /* Mark it as available to be used again. */
  3906. ov->frame[frmx].grabstate = FRAME_UNUSED;
  3907. if ((rc = ov51x_new_frame(ov, !frmx))) {
  3908. err("ov51x_new_frame returned error");
  3909. goto error;
  3910. }
  3911. }
  3912. PDEBUG(4, "read finished, returning %ld (sweet)", count);
  3913. mutex_unlock(&ov->lock);
  3914. return count;
  3915. error:
  3916. mutex_unlock(&ov->lock);
  3917. return rc;
  3918. }
  3919. static int
  3920. ov51x_v4l1_mmap(struct file *file, struct vm_area_struct *vma)
  3921. {
  3922. struct video_device *vdev = file->private_data;
  3923. unsigned long start = vma->vm_start;
  3924. unsigned long size = vma->vm_end - vma->vm_start;
  3925. struct usb_ov511 *ov = video_get_drvdata(vdev);
  3926. unsigned long page, pos;
  3927. if (ov->dev == NULL)
  3928. return -EIO;
  3929. PDEBUG(4, "mmap: %ld (%lX) bytes", size, size);
  3930. if (size > (((OV511_NUMFRAMES
  3931. * MAX_DATA_SIZE(ov->maxwidth, ov->maxheight)
  3932. + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1))))
  3933. return -EINVAL;
  3934. if (mutex_lock_interruptible(&ov->lock))
  3935. return -EINTR;
  3936. pos = (unsigned long)ov->fbuf;
  3937. while (size > 0) {
  3938. page = vmalloc_to_pfn((void *)pos);
  3939. if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) {
  3940. mutex_unlock(&ov->lock);
  3941. return -EAGAIN;
  3942. }
  3943. start += PAGE_SIZE;
  3944. pos += PAGE_SIZE;
  3945. if (size > PAGE_SIZE)
  3946. size -= PAGE_SIZE;
  3947. else
  3948. size = 0;
  3949. }
  3950. mutex_unlock(&ov->lock);
  3951. return 0;
  3952. }
  3953. static const struct v4l2_file_operations ov511_fops = {
  3954. .owner = THIS_MODULE,
  3955. .open = ov51x_v4l1_open,
  3956. .release = ov51x_v4l1_close,
  3957. .read = ov51x_v4l1_read,
  3958. .mmap = ov51x_v4l1_mmap,
  3959. .ioctl = ov51x_v4l1_ioctl,
  3960. };
  3961. static struct video_device vdev_template = {
  3962. .name = "OV511 USB Camera",
  3963. .fops = &ov511_fops,
  3964. .release = video_device_release,
  3965. };
  3966. /****************************************************************************
  3967. *
  3968. * OV511 and sensor configuration
  3969. *
  3970. ***************************************************************************/
  3971. /* This initializes the OV7610, OV7620, or OV76BE sensor. The OV76BE uses
  3972. * the same register settings as the OV7610, since they are very similar.
  3973. */
  3974. static int
  3975. ov7xx0_configure(struct usb_ov511 *ov)
  3976. {
  3977. int i, success;
  3978. int rc;
  3979. /* Lawrence Glaister <lg@jfm.bc.ca> reports:
  3980. *
  3981. * Register 0x0f in the 7610 has the following effects:
  3982. *
  3983. * 0x85 (AEC method 1): Best overall, good contrast range
  3984. * 0x45 (AEC method 2): Very overexposed
  3985. * 0xa5 (spec sheet default): Ok, but the black level is
  3986. * shifted resulting in loss of contrast
  3987. * 0x05 (old driver setting): very overexposed, too much
  3988. * contrast
  3989. */
  3990. static struct ov511_regvals aRegvalsNorm7610[] = {
  3991. { OV511_I2C_BUS, 0x10, 0xff },
  3992. { OV511_I2C_BUS, 0x16, 0x06 },
  3993. { OV511_I2C_BUS, 0x28, 0x24 },
  3994. { OV511_I2C_BUS, 0x2b, 0xac },
  3995. { OV511_I2C_BUS, 0x12, 0x00 },
  3996. { OV511_I2C_BUS, 0x38, 0x81 },
  3997. { OV511_I2C_BUS, 0x28, 0x24 }, /* 0c */
  3998. { OV511_I2C_BUS, 0x0f, 0x85 }, /* lg's setting */
  3999. { OV511_I2C_BUS, 0x15, 0x01 },
  4000. { OV511_I2C_BUS, 0x20, 0x1c },
  4001. { OV511_I2C_BUS, 0x23, 0x2a },
  4002. { OV511_I2C_BUS, 0x24, 0x10 },
  4003. { OV511_I2C_BUS, 0x25, 0x8a },
  4004. { OV511_I2C_BUS, 0x26, 0xa2 },
  4005. { OV511_I2C_BUS, 0x27, 0xc2 },
  4006. { OV511_I2C_BUS, 0x2a, 0x04 },
  4007. { OV511_I2C_BUS, 0x2c, 0xfe },
  4008. { OV511_I2C_BUS, 0x2d, 0x93 },
  4009. { OV511_I2C_BUS, 0x30, 0x71 },
  4010. { OV511_I2C_BUS, 0x31, 0x60 },
  4011. { OV511_I2C_BUS, 0x32, 0x26 },
  4012. { OV511_I2C_BUS, 0x33, 0x20 },
  4013. { OV511_I2C_BUS, 0x34, 0x48 },
  4014. { OV511_I2C_BUS, 0x12, 0x24 },
  4015. { OV511_I2C_BUS, 0x11, 0x01 },
  4016. { OV511_I2C_BUS, 0x0c, 0x24 },
  4017. { OV511_I2C_BUS, 0x0d, 0x24 },
  4018. { OV511_DONE_BUS, 0x0, 0x00 },
  4019. };
  4020. static struct ov511_regvals aRegvalsNorm7620[] = {
  4021. { OV511_I2C_BUS, 0x00, 0x00 },
  4022. { OV511_I2C_BUS, 0x01, 0x80 },
  4023. { OV511_I2C_BUS, 0x02, 0x80 },
  4024. { OV511_I2C_BUS, 0x03, 0xc0 },
  4025. { OV511_I2C_BUS, 0x06, 0x60 },
  4026. { OV511_I2C_BUS, 0x07, 0x00 },
  4027. { OV511_I2C_BUS, 0x0c, 0x24 },
  4028. { OV511_I2C_BUS, 0x0c, 0x24 },
  4029. { OV511_I2C_BUS, 0x0d, 0x24 },
  4030. { OV511_I2C_BUS, 0x11, 0x01 },
  4031. { OV511_I2C_BUS, 0x12, 0x24 },
  4032. { OV511_I2C_BUS, 0x13, 0x01 },
  4033. { OV511_I2C_BUS, 0x14, 0x84 },
  4034. { OV511_I2C_BUS, 0x15, 0x01 },
  4035. { OV511_I2C_BUS, 0x16, 0x03 },
  4036. { OV511_I2C_BUS, 0x17, 0x2f },
  4037. { OV511_I2C_BUS, 0x18, 0xcf },
  4038. { OV511_I2C_BUS, 0x19, 0x06 },
  4039. { OV511_I2C_BUS, 0x1a, 0xf5 },
  4040. { OV511_I2C_BUS, 0x1b, 0x00 },
  4041. { OV511_I2C_BUS, 0x20, 0x18 },
  4042. { OV511_I2C_BUS, 0x21, 0x80 },
  4043. { OV511_I2C_BUS, 0x22, 0x80 },
  4044. { OV511_I2C_BUS, 0x23, 0x00 },
  4045. { OV511_I2C_BUS, 0x26, 0xa2 },
  4046. { OV511_I2C_BUS, 0x27, 0xea },
  4047. { OV511_I2C_BUS, 0x28, 0x20 },
  4048. { OV511_I2C_BUS, 0x29, 0x00 },
  4049. { OV511_I2C_BUS, 0x2a, 0x10 },
  4050. { OV511_I2C_BUS, 0x2b, 0x00 },
  4051. { OV511_I2C_BUS, 0x2c, 0x88 },
  4052. { OV511_I2C_BUS, 0x2d, 0x91 },
  4053. { OV511_I2C_BUS, 0x2e, 0x80 },
  4054. { OV511_I2C_BUS, 0x2f, 0x44 },
  4055. { OV511_I2C_BUS, 0x60, 0x27 },
  4056. { OV511_I2C_BUS, 0x61, 0x02 },
  4057. { OV511_I2C_BUS, 0x62, 0x5f },
  4058. { OV511_I2C_BUS, 0x63, 0xd5 },
  4059. { OV511_I2C_BUS, 0x64, 0x57 },
  4060. { OV511_I2C_BUS, 0x65, 0x83 },
  4061. { OV511_I2C_BUS, 0x66, 0x55 },
  4062. { OV511_I2C_BUS, 0x67, 0x92 },
  4063. { OV511_I2C_BUS, 0x68, 0xcf },
  4064. { OV511_I2C_BUS, 0x69, 0x76 },
  4065. { OV511_I2C_BUS, 0x6a, 0x22 },
  4066. { OV511_I2C_BUS, 0x6b, 0x00 },
  4067. { OV511_I2C_BUS, 0x6c, 0x02 },
  4068. { OV511_I2C_BUS, 0x6d, 0x44 },
  4069. { OV511_I2C_BUS, 0x6e, 0x80 },
  4070. { OV511_I2C_BUS, 0x6f, 0x1d },
  4071. { OV511_I2C_BUS, 0x70, 0x8b },
  4072. { OV511_I2C_BUS, 0x71, 0x00 },
  4073. { OV511_I2C_BUS, 0x72, 0x14 },
  4074. { OV511_I2C_BUS, 0x73, 0x54 },
  4075. { OV511_I2C_BUS, 0x74, 0x00 },
  4076. { OV511_I2C_BUS, 0x75, 0x8e },
  4077. { OV511_I2C_BUS, 0x76, 0x00 },
  4078. { OV511_I2C_BUS, 0x77, 0xff },
  4079. { OV511_I2C_BUS, 0x78, 0x80 },
  4080. { OV511_I2C_BUS, 0x79, 0x80 },
  4081. { OV511_I2C_BUS, 0x7a, 0x80 },
  4082. { OV511_I2C_BUS, 0x7b, 0xe2 },
  4083. { OV511_I2C_BUS, 0x7c, 0x00 },
  4084. { OV511_DONE_BUS, 0x0, 0x00 },
  4085. };
  4086. PDEBUG(4, "starting configuration");
  4087. /* This looks redundant, but is necessary for WebCam 3 */
  4088. ov->primary_i2c_slave = OV7xx0_SID;
  4089. if (ov51x_set_slave_ids(ov, OV7xx0_SID) < 0)
  4090. return -1;
  4091. if (init_ov_sensor(ov) >= 0) {
  4092. PDEBUG(1, "OV7xx0 sensor initalized (method 1)");
  4093. } else {
  4094. /* Reset the 76xx */
  4095. if (i2c_w(ov, 0x12, 0x80) < 0)
  4096. return -1;
  4097. /* Wait for it to initialize */
  4098. msleep(150);
  4099. i = 0;
  4100. success = 0;
  4101. while (i <= i2c_detect_tries) {
  4102. if ((i2c_r(ov, OV7610_REG_ID_HIGH) == 0x7F) &&
  4103. (i2c_r(ov, OV7610_REG_ID_LOW) == 0xA2)) {
  4104. success = 1;
  4105. break;
  4106. } else {
  4107. i++;
  4108. }
  4109. }
  4110. // Was (i == i2c_detect_tries) previously. This obviously used to always report
  4111. // success. Whether anyone actually depended on that bug is unknown
  4112. if ((i >= i2c_detect_tries) && (success == 0)) {
  4113. err("Failed to read sensor ID. You might not have an");
  4114. err("OV7610/20, or it may be not responding. Report");
  4115. err("this to " EMAIL);
  4116. err("This is only a warning. You can attempt to use");
  4117. err("your camera anyway");
  4118. // Only issue a warning for now
  4119. // return -1;
  4120. } else {
  4121. PDEBUG(1, "OV7xx0 initialized (method 2, %dx)", i+1);
  4122. }
  4123. }
  4124. /* Detect sensor (sub)type */
  4125. rc = i2c_r(ov, OV7610_REG_COM_I);
  4126. if (rc < 0) {
  4127. err("Error detecting sensor type");
  4128. return -1;
  4129. } else if ((rc & 3) == 3) {
  4130. dev_info(&ov->dev->dev, "Sensor is an OV7610\n");
  4131. ov->sensor = SEN_OV7610;
  4132. } else if ((rc & 3) == 1) {
  4133. /* I don't know what's different about the 76BE yet. */
  4134. if (i2c_r(ov, 0x15) & 1)
  4135. dev_info(&ov->dev->dev, "Sensor is an OV7620AE\n");
  4136. else
  4137. dev_info(&ov->dev->dev, "Sensor is an OV76BE\n");
  4138. /* OV511+ will return all zero isoc data unless we
  4139. * configure the sensor as a 7620. Someone needs to
  4140. * find the exact reg. setting that causes this. */
  4141. if (ov->bridge == BRG_OV511PLUS) {
  4142. dev_info(&ov->dev->dev,
  4143. "Enabling 511+/7620AE workaround\n");
  4144. ov->sensor = SEN_OV7620;
  4145. } else {
  4146. ov->sensor = SEN_OV76BE;
  4147. }
  4148. } else if ((rc & 3) == 0) {
  4149. dev_info(&ov->dev->dev, "Sensor is an OV7620\n");
  4150. ov->sensor = SEN_OV7620;
  4151. } else {
  4152. err("Unknown image sensor version: %d", rc & 3);
  4153. return -1;
  4154. }
  4155. if (ov->sensor == SEN_OV7620) {
  4156. PDEBUG(4, "Writing 7620 registers");
  4157. if (write_regvals(ov, aRegvalsNorm7620))
  4158. return -1;
  4159. } else {
  4160. PDEBUG(4, "Writing 7610 registers");
  4161. if (write_regvals(ov, aRegvalsNorm7610))
  4162. return -1;
  4163. }
  4164. /* Set sensor-specific vars */
  4165. ov->maxwidth = 640;
  4166. ov->maxheight = 480;
  4167. ov->minwidth = 64;
  4168. ov->minheight = 48;
  4169. // FIXME: These do not match the actual settings yet
  4170. ov->brightness = 0x80 << 8;
  4171. ov->contrast = 0x80 << 8;
  4172. ov->colour = 0x80 << 8;
  4173. ov->hue = 0x80 << 8;
  4174. return 0;
  4175. }
  4176. /* This initializes the OV6620, OV6630, OV6630AE, or OV6630AF sensor. */
  4177. static int
  4178. ov6xx0_configure(struct usb_ov511 *ov)
  4179. {
  4180. int rc;
  4181. static struct ov511_regvals aRegvalsNorm6x20[] = {
  4182. { OV511_I2C_BUS, 0x12, 0x80 }, /* reset */
  4183. { OV511_I2C_BUS, 0x11, 0x01 },
  4184. { OV511_I2C_BUS, 0x03, 0x60 },
  4185. { OV511_I2C_BUS, 0x05, 0x7f }, /* For when autoadjust is off */
  4186. { OV511_I2C_BUS, 0x07, 0xa8 },
  4187. /* The ratio of 0x0c and 0x0d controls the white point */
  4188. { OV511_I2C_BUS, 0x0c, 0x24 },
  4189. { OV511_I2C_BUS, 0x0d, 0x24 },
  4190. { OV511_I2C_BUS, 0x0f, 0x15 }, /* COMS */
  4191. { OV511_I2C_BUS, 0x10, 0x75 }, /* AEC Exposure time */
  4192. { OV511_I2C_BUS, 0x12, 0x24 }, /* Enable AGC */
  4193. { OV511_I2C_BUS, 0x14, 0x04 },
  4194. /* 0x16: 0x06 helps frame stability with moving objects */
  4195. { OV511_I2C_BUS, 0x16, 0x06 },
  4196. // { OV511_I2C_BUS, 0x20, 0x30 }, /* Aperture correction enable */
  4197. { OV511_I2C_BUS, 0x26, 0xb2 }, /* BLC enable */
  4198. /* 0x28: 0x05 Selects RGB format if RGB on */
  4199. { OV511_I2C_BUS, 0x28, 0x05 },
  4200. { OV511_I2C_BUS, 0x2a, 0x04 }, /* Disable framerate adjust */
  4201. // { OV511_I2C_BUS, 0x2b, 0xac }, /* Framerate; Set 2a[7] first */
  4202. { OV511_I2C_BUS, 0x2d, 0x99 },
  4203. { OV511_I2C_BUS, 0x33, 0xa0 }, /* Color Processing Parameter */
  4204. { OV511_I2C_BUS, 0x34, 0xd2 }, /* Max A/D range */
  4205. { OV511_I2C_BUS, 0x38, 0x8b },
  4206. { OV511_I2C_BUS, 0x39, 0x40 },
  4207. { OV511_I2C_BUS, 0x3c, 0x39 }, /* Enable AEC mode changing */
  4208. { OV511_I2C_BUS, 0x3c, 0x3c }, /* Change AEC mode */
  4209. { OV511_I2C_BUS, 0x3c, 0x24 }, /* Disable AEC mode changing */
  4210. { OV511_I2C_BUS, 0x3d, 0x80 },
  4211. /* These next two registers (0x4a, 0x4b) are undocumented. They
  4212. * control the color balance */
  4213. { OV511_I2C_BUS, 0x4a, 0x80 },
  4214. { OV511_I2C_BUS, 0x4b, 0x80 },
  4215. { OV511_I2C_BUS, 0x4d, 0xd2 }, /* This reduces noise a bit */
  4216. { OV511_I2C_BUS, 0x4e, 0xc1 },
  4217. { OV511_I2C_BUS, 0x4f, 0x04 },
  4218. // Do 50-53 have any effect?
  4219. // Toggle 0x12[2] off and on here?
  4220. { OV511_DONE_BUS, 0x0, 0x00 }, /* END MARKER */
  4221. };
  4222. static struct ov511_regvals aRegvalsNorm6x30[] = {
  4223. /*OK*/ { OV511_I2C_BUS, 0x12, 0x80 }, /* reset */
  4224. { OV511_I2C_BUS, 0x11, 0x00 },
  4225. /*OK*/ { OV511_I2C_BUS, 0x03, 0x60 },
  4226. /*0A?*/ { OV511_I2C_BUS, 0x05, 0x7f }, /* For when autoadjust is off */
  4227. { OV511_I2C_BUS, 0x07, 0xa8 },
  4228. /* The ratio of 0x0c and 0x0d controls the white point */
  4229. /*OK*/ { OV511_I2C_BUS, 0x0c, 0x24 },
  4230. /*OK*/ { OV511_I2C_BUS, 0x0d, 0x24 },
  4231. /*A*/ { OV511_I2C_BUS, 0x0e, 0x20 },
  4232. // /*04?*/ { OV511_I2C_BUS, 0x14, 0x80 },
  4233. { OV511_I2C_BUS, 0x16, 0x03 },
  4234. // /*OK*/ { OV511_I2C_BUS, 0x20, 0x30 }, /* Aperture correction enable */
  4235. // 21 & 22? The suggested values look wrong. Go with default
  4236. /*A*/ { OV511_I2C_BUS, 0x23, 0xc0 },
  4237. /*A*/ { OV511_I2C_BUS, 0x25, 0x9a }, // Check this against default
  4238. // /*OK*/ { OV511_I2C_BUS, 0x26, 0xb2 }, /* BLC enable */
  4239. /* 0x28: 0x05 Selects RGB format if RGB on */
  4240. // /*04?*/ { OV511_I2C_BUS, 0x28, 0x05 },
  4241. // /*04?*/ { OV511_I2C_BUS, 0x28, 0x45 }, // DEBUG: Tristate UV bus
  4242. /*OK*/ { OV511_I2C_BUS, 0x2a, 0x04 }, /* Disable framerate adjust */
  4243. // /*OK*/ { OV511_I2C_BUS, 0x2b, 0xac }, /* Framerate; Set 2a[7] first */
  4244. { OV511_I2C_BUS, 0x2d, 0x99 },
  4245. // /*A*/ { OV511_I2C_BUS, 0x33, 0x26 }, // Reserved bits on 6620
  4246. // /*d2?*/ { OV511_I2C_BUS, 0x34, 0x03 }, /* Max A/D range */
  4247. // /*8b?*/ { OV511_I2C_BUS, 0x38, 0x83 },
  4248. // /*40?*/ { OV511_I2C_BUS, 0x39, 0xc0 }, // 6630 adds bit 7
  4249. // { OV511_I2C_BUS, 0x3c, 0x39 }, /* Enable AEC mode changing */
  4250. // { OV511_I2C_BUS, 0x3c, 0x3c }, /* Change AEC mode */
  4251. // { OV511_I2C_BUS, 0x3c, 0x24 }, /* Disable AEC mode changing */
  4252. { OV511_I2C_BUS, 0x3d, 0x80 },
  4253. // /*A*/ { OV511_I2C_BUS, 0x3f, 0x0e },
  4254. /* These next two registers (0x4a, 0x4b) are undocumented. They
  4255. * control the color balance */
  4256. // /*OK?*/ { OV511_I2C_BUS, 0x4a, 0x80 }, // Check these
  4257. // /*OK?*/ { OV511_I2C_BUS, 0x4b, 0x80 },
  4258. { OV511_I2C_BUS, 0x4d, 0x10 }, /* U = 0.563u, V = 0.714v */
  4259. /*c1?*/ { OV511_I2C_BUS, 0x4e, 0x40 },
  4260. /* UV average mode, color killer: strongest */
  4261. { OV511_I2C_BUS, 0x4f, 0x07 },
  4262. { OV511_I2C_BUS, 0x54, 0x23 }, /* Max AGC gain: 18dB */
  4263. { OV511_I2C_BUS, 0x57, 0x81 }, /* (default) */
  4264. { OV511_I2C_BUS, 0x59, 0x01 }, /* AGC dark current comp: +1 */
  4265. { OV511_I2C_BUS, 0x5a, 0x2c }, /* (undocumented) */
  4266. { OV511_I2C_BUS, 0x5b, 0x0f }, /* AWB chrominance levels */
  4267. // { OV511_I2C_BUS, 0x5c, 0x10 },
  4268. { OV511_DONE_BUS, 0x0, 0x00 }, /* END MARKER */
  4269. };
  4270. PDEBUG(4, "starting sensor configuration");
  4271. if (init_ov_sensor(ov) < 0) {
  4272. err("Failed to read sensor ID. You might not have an OV6xx0,");
  4273. err("or it may be not responding. Report this to " EMAIL);
  4274. return -1;
  4275. } else {
  4276. PDEBUG(1, "OV6xx0 sensor detected");
  4277. }
  4278. /* Detect sensor (sub)type */
  4279. rc = i2c_r(ov, OV7610_REG_COM_I);
  4280. if (rc < 0) {
  4281. err("Error detecting sensor type");
  4282. return -1;
  4283. }
  4284. if ((rc & 3) == 0) {
  4285. ov->sensor = SEN_OV6630;
  4286. dev_info(&ov->dev->dev, "Sensor is an OV6630\n");
  4287. } else if ((rc & 3) == 1) {
  4288. ov->sensor = SEN_OV6620;
  4289. dev_info(&ov->dev->dev, "Sensor is an OV6620\n");
  4290. } else if ((rc & 3) == 2) {
  4291. ov->sensor = SEN_OV6630;
  4292. dev_info(&ov->dev->dev, "Sensor is an OV6630AE\n");
  4293. } else if ((rc & 3) == 3) {
  4294. ov->sensor = SEN_OV6630;
  4295. dev_info(&ov->dev->dev, "Sensor is an OV6630AF\n");
  4296. }
  4297. /* Set sensor-specific vars */
  4298. ov->maxwidth = 352;
  4299. ov->maxheight = 288;
  4300. ov->minwidth = 64;
  4301. ov->minheight = 48;
  4302. // FIXME: These do not match the actual settings yet
  4303. ov->brightness = 0x80 << 8;
  4304. ov->contrast = 0x80 << 8;
  4305. ov->colour = 0x80 << 8;
  4306. ov->hue = 0x80 << 8;
  4307. if (ov->sensor == SEN_OV6620) {
  4308. PDEBUG(4, "Writing 6x20 registers");
  4309. if (write_regvals(ov, aRegvalsNorm6x20))
  4310. return -1;
  4311. } else {
  4312. PDEBUG(4, "Writing 6x30 registers");
  4313. if (write_regvals(ov, aRegvalsNorm6x30))
  4314. return -1;
  4315. }
  4316. return 0;
  4317. }
  4318. /* This initializes the KS0127 and KS0127B video decoders. */
  4319. static int
  4320. ks0127_configure(struct usb_ov511 *ov)
  4321. {
  4322. int rc;
  4323. // FIXME: I don't know how to sync or reset it yet
  4324. #if 0
  4325. if (ov51x_init_ks_sensor(ov) < 0) {
  4326. err("Failed to initialize the KS0127");
  4327. return -1;
  4328. } else {
  4329. PDEBUG(1, "KS012x(B) sensor detected");
  4330. }
  4331. #endif
  4332. /* Detect decoder subtype */
  4333. rc = i2c_r(ov, 0x00);
  4334. if (rc < 0) {
  4335. err("Error detecting sensor type");
  4336. return -1;
  4337. } else if (rc & 0x08) {
  4338. rc = i2c_r(ov, 0x3d);
  4339. if (rc < 0) {
  4340. err("Error detecting sensor type");
  4341. return -1;
  4342. } else if ((rc & 0x0f) == 0) {
  4343. dev_info(&ov->dev->dev, "Sensor is a KS0127\n");
  4344. ov->sensor = SEN_KS0127;
  4345. } else if ((rc & 0x0f) == 9) {
  4346. dev_info(&ov->dev->dev, "Sensor is a KS0127B Rev. A\n");
  4347. ov->sensor = SEN_KS0127B;
  4348. }
  4349. } else {
  4350. err("Error: Sensor is an unsupported KS0122");
  4351. return -1;
  4352. }
  4353. /* Set sensor-specific vars */
  4354. ov->maxwidth = 640;
  4355. ov->maxheight = 480;
  4356. ov->minwidth = 64;
  4357. ov->minheight = 48;
  4358. // FIXME: These do not match the actual settings yet
  4359. ov->brightness = 0x80 << 8;
  4360. ov->contrast = 0x80 << 8;
  4361. ov->colour = 0x80 << 8;
  4362. ov->hue = 0x80 << 8;
  4363. /* This device is not supported yet. Bail out now... */
  4364. err("This sensor is not supported yet.");
  4365. return -1;
  4366. return 0;
  4367. }
  4368. /* This initializes the SAA7111A video decoder. */
  4369. static int
  4370. saa7111a_configure(struct usb_ov511 *ov)
  4371. {
  4372. int rc;
  4373. /* Since there is no register reset command, all registers must be
  4374. * written, otherwise gives erratic results */
  4375. static struct ov511_regvals aRegvalsNormSAA7111A[] = {
  4376. { OV511_I2C_BUS, 0x06, 0xce },
  4377. { OV511_I2C_BUS, 0x07, 0x00 },
  4378. { OV511_I2C_BUS, 0x10, 0x44 }, /* YUV422, 240/286 lines */
  4379. { OV511_I2C_BUS, 0x0e, 0x01 }, /* NTSC M or PAL BGHI */
  4380. { OV511_I2C_BUS, 0x00, 0x00 },
  4381. { OV511_I2C_BUS, 0x01, 0x00 },
  4382. { OV511_I2C_BUS, 0x03, 0x23 },
  4383. { OV511_I2C_BUS, 0x04, 0x00 },
  4384. { OV511_I2C_BUS, 0x05, 0x00 },
  4385. { OV511_I2C_BUS, 0x08, 0xc8 }, /* Auto field freq */
  4386. { OV511_I2C_BUS, 0x09, 0x01 }, /* Chrom. trap off, APER=0.25 */
  4387. { OV511_I2C_BUS, 0x0a, 0x80 }, /* BRIG=128 */
  4388. { OV511_I2C_BUS, 0x0b, 0x40 }, /* CONT=1.0 */
  4389. { OV511_I2C_BUS, 0x0c, 0x40 }, /* SATN=1.0 */
  4390. { OV511_I2C_BUS, 0x0d, 0x00 }, /* HUE=0 */
  4391. { OV511_I2C_BUS, 0x0f, 0x00 },
  4392. { OV511_I2C_BUS, 0x11, 0x0c },
  4393. { OV511_I2C_BUS, 0x12, 0x00 },
  4394. { OV511_I2C_BUS, 0x13, 0x00 },
  4395. { OV511_I2C_BUS, 0x14, 0x00 },
  4396. { OV511_I2C_BUS, 0x15, 0x00 },
  4397. { OV511_I2C_BUS, 0x16, 0x00 },
  4398. { OV511_I2C_BUS, 0x17, 0x00 },
  4399. { OV511_I2C_BUS, 0x02, 0xc0 }, /* Composite input 0 */
  4400. { OV511_DONE_BUS, 0x0, 0x00 },
  4401. };
  4402. // FIXME: I don't know how to sync or reset it yet
  4403. #if 0
  4404. if (ov51x_init_saa_sensor(ov) < 0) {
  4405. err("Failed to initialize the SAA7111A");
  4406. return -1;
  4407. } else {
  4408. PDEBUG(1, "SAA7111A sensor detected");
  4409. }
  4410. #endif
  4411. /* 640x480 not supported with PAL */
  4412. if (ov->pal) {
  4413. ov->maxwidth = 320;
  4414. ov->maxheight = 240; /* Even field only */
  4415. } else {
  4416. ov->maxwidth = 640;
  4417. ov->maxheight = 480; /* Even/Odd fields */
  4418. }
  4419. ov->minwidth = 320;
  4420. ov->minheight = 240; /* Even field only */
  4421. ov->has_decoder = 1;
  4422. ov->num_inputs = 8;
  4423. ov->norm = VIDEO_MODE_AUTO;
  4424. ov->stop_during_set = 0; /* Decoder guarantees stable image */
  4425. /* Decoder doesn't change these values, so we use these instead of
  4426. * acutally reading the registers (which doesn't work) */
  4427. ov->brightness = 0x80 << 8;
  4428. ov->contrast = 0x40 << 9;
  4429. ov->colour = 0x40 << 9;
  4430. ov->hue = 32768;
  4431. PDEBUG(4, "Writing SAA7111A registers");
  4432. if (write_regvals(ov, aRegvalsNormSAA7111A))
  4433. return -1;
  4434. /* Detect version of decoder. This must be done after writing the
  4435. * initial regs or the decoder will lock up. */
  4436. rc = i2c_r(ov, 0x00);
  4437. if (rc < 0) {
  4438. err("Error detecting sensor version");
  4439. return -1;
  4440. } else {
  4441. dev_info(&ov->dev->dev,
  4442. "Sensor is an SAA7111A (version 0x%x)\n", rc);
  4443. ov->sensor = SEN_SAA7111A;
  4444. }
  4445. // FIXME: Fix this for OV518(+)
  4446. /* Latch to negative edge of clock. Otherwise, we get incorrect
  4447. * colors and jitter in the digital signal. */
  4448. if (ov->bclass == BCL_OV511)
  4449. reg_w(ov, 0x11, 0x00);
  4450. else
  4451. dev_warn(&ov->dev->dev,
  4452. "SAA7111A not yet supported with OV518/OV518+\n");
  4453. return 0;
  4454. }
  4455. /* This initializes the OV511/OV511+ and the sensor */
  4456. static int
  4457. ov511_configure(struct usb_ov511 *ov)
  4458. {
  4459. static struct ov511_regvals aRegvalsInit511[] = {
  4460. { OV511_REG_BUS, R51x_SYS_RESET, 0x7f },
  4461. { OV511_REG_BUS, R51x_SYS_INIT, 0x01 },
  4462. { OV511_REG_BUS, R51x_SYS_RESET, 0x7f },
  4463. { OV511_REG_BUS, R51x_SYS_INIT, 0x01 },
  4464. { OV511_REG_BUS, R51x_SYS_RESET, 0x3f },
  4465. { OV511_REG_BUS, R51x_SYS_INIT, 0x01 },
  4466. { OV511_REG_BUS, R51x_SYS_RESET, 0x3d },
  4467. { OV511_DONE_BUS, 0x0, 0x00},
  4468. };
  4469. static struct ov511_regvals aRegvalsNorm511[] = {
  4470. { OV511_REG_BUS, R511_DRAM_FLOW_CTL, 0x01 },
  4471. { OV511_REG_BUS, R51x_SYS_SNAP, 0x00 },
  4472. { OV511_REG_BUS, R51x_SYS_SNAP, 0x02 },
  4473. { OV511_REG_BUS, R51x_SYS_SNAP, 0x00 },
  4474. { OV511_REG_BUS, R511_FIFO_OPTS, 0x1f },
  4475. { OV511_REG_BUS, R511_COMP_EN, 0x00 },
  4476. { OV511_REG_BUS, R511_COMP_LUT_EN, 0x03 },
  4477. { OV511_DONE_BUS, 0x0, 0x00 },
  4478. };
  4479. static struct ov511_regvals aRegvalsNorm511Plus[] = {
  4480. { OV511_REG_BUS, R511_DRAM_FLOW_CTL, 0xff },
  4481. { OV511_REG_BUS, R51x_SYS_SNAP, 0x00 },
  4482. { OV511_REG_BUS, R51x_SYS_SNAP, 0x02 },
  4483. { OV511_REG_BUS, R51x_SYS_SNAP, 0x00 },
  4484. { OV511_REG_BUS, R511_FIFO_OPTS, 0xff },
  4485. { OV511_REG_BUS, R511_COMP_EN, 0x00 },
  4486. { OV511_REG_BUS, R511_COMP_LUT_EN, 0x03 },
  4487. { OV511_DONE_BUS, 0x0, 0x00 },
  4488. };
  4489. PDEBUG(4, "");
  4490. ov->customid = reg_r(ov, R511_SYS_CUST_ID);
  4491. if (ov->customid < 0) {
  4492. err("Unable to read camera bridge registers");
  4493. goto error;
  4494. }
  4495. PDEBUG (1, "CustomID = %d", ov->customid);
  4496. ov->desc = symbolic(camlist, ov->customid);
  4497. dev_info(&ov->dev->dev, "model: %s\n", ov->desc);
  4498. if (0 == strcmp(ov->desc, NOT_DEFINED_STR)) {
  4499. err("Camera type (%d) not recognized", ov->customid);
  4500. err("Please notify " EMAIL " of the name,");
  4501. err("manufacturer, model, and this number of your camera.");
  4502. err("Also include the output of the detection process.");
  4503. }
  4504. if (ov->customid == 70) /* USB Life TV (PAL/SECAM) */
  4505. ov->pal = 1;
  4506. if (write_regvals(ov, aRegvalsInit511))
  4507. goto error;
  4508. if (ov->led_policy == LED_OFF || ov->led_policy == LED_AUTO)
  4509. ov51x_led_control(ov, 0);
  4510. /* The OV511+ has undocumented bits in the flow control register.
  4511. * Setting it to 0xff fixes the corruption with moving objects. */
  4512. if (ov->bridge == BRG_OV511) {
  4513. if (write_regvals(ov, aRegvalsNorm511))
  4514. goto error;
  4515. } else if (ov->bridge == BRG_OV511PLUS) {
  4516. if (write_regvals(ov, aRegvalsNorm511Plus))
  4517. goto error;
  4518. } else {
  4519. err("Invalid bridge");
  4520. }
  4521. if (ov511_init_compression(ov))
  4522. goto error;
  4523. ov->packet_numbering = 1;
  4524. ov511_set_packet_size(ov, 0);
  4525. ov->snap_enabled = snapshot;
  4526. /* Test for 7xx0 */
  4527. PDEBUG(3, "Testing for 0V7xx0");
  4528. ov->primary_i2c_slave = OV7xx0_SID;
  4529. if (ov51x_set_slave_ids(ov, OV7xx0_SID) < 0)
  4530. goto error;
  4531. if (i2c_w(ov, 0x12, 0x80) < 0) {
  4532. /* Test for 6xx0 */
  4533. PDEBUG(3, "Testing for 0V6xx0");
  4534. ov->primary_i2c_slave = OV6xx0_SID;
  4535. if (ov51x_set_slave_ids(ov, OV6xx0_SID) < 0)
  4536. goto error;
  4537. if (i2c_w(ov, 0x12, 0x80) < 0) {
  4538. /* Test for 8xx0 */
  4539. PDEBUG(3, "Testing for 0V8xx0");
  4540. ov->primary_i2c_slave = OV8xx0_SID;
  4541. if (ov51x_set_slave_ids(ov, OV8xx0_SID) < 0)
  4542. goto error;
  4543. if (i2c_w(ov, 0x12, 0x80) < 0) {
  4544. /* Test for SAA7111A */
  4545. PDEBUG(3, "Testing for SAA7111A");
  4546. ov->primary_i2c_slave = SAA7111A_SID;
  4547. if (ov51x_set_slave_ids(ov, SAA7111A_SID) < 0)
  4548. goto error;
  4549. if (i2c_w(ov, 0x0d, 0x00) < 0) {
  4550. /* Test for KS0127 */
  4551. PDEBUG(3, "Testing for KS0127");
  4552. ov->primary_i2c_slave = KS0127_SID;
  4553. if (ov51x_set_slave_ids(ov, KS0127_SID) < 0)
  4554. goto error;
  4555. if (i2c_w(ov, 0x10, 0x00) < 0) {
  4556. err("Can't determine sensor slave IDs");
  4557. goto error;
  4558. } else {
  4559. if (ks0127_configure(ov) < 0) {
  4560. err("Failed to configure KS0127");
  4561. goto error;
  4562. }
  4563. }
  4564. } else {
  4565. if (saa7111a_configure(ov) < 0) {
  4566. err("Failed to configure SAA7111A");
  4567. goto error;
  4568. }
  4569. }
  4570. } else {
  4571. err("Detected unsupported OV8xx0 sensor");
  4572. goto error;
  4573. }
  4574. } else {
  4575. if (ov6xx0_configure(ov) < 0) {
  4576. err("Failed to configure OV6xx0");
  4577. goto error;
  4578. }
  4579. }
  4580. } else {
  4581. if (ov7xx0_configure(ov) < 0) {
  4582. err("Failed to configure OV7xx0");
  4583. goto error;
  4584. }
  4585. }
  4586. return 0;
  4587. error:
  4588. err("OV511 Config failed");
  4589. return -EBUSY;
  4590. }
  4591. /* This initializes the OV518/OV518+ and the sensor */
  4592. static int
  4593. ov518_configure(struct usb_ov511 *ov)
  4594. {
  4595. /* For 518 and 518+ */
  4596. static struct ov511_regvals aRegvalsInit518[] = {
  4597. { OV511_REG_BUS, R51x_SYS_RESET, 0x40 },
  4598. { OV511_REG_BUS, R51x_SYS_INIT, 0xe1 },
  4599. { OV511_REG_BUS, R51x_SYS_RESET, 0x3e },
  4600. { OV511_REG_BUS, R51x_SYS_INIT, 0xe1 },
  4601. { OV511_REG_BUS, R51x_SYS_RESET, 0x00 },
  4602. { OV511_REG_BUS, R51x_SYS_INIT, 0xe1 },
  4603. { OV511_REG_BUS, 0x46, 0x00 },
  4604. { OV511_REG_BUS, 0x5d, 0x03 },
  4605. { OV511_DONE_BUS, 0x0, 0x00},
  4606. };
  4607. static struct ov511_regvals aRegvalsNorm518[] = {
  4608. { OV511_REG_BUS, R51x_SYS_SNAP, 0x02 }, /* Reset */
  4609. { OV511_REG_BUS, R51x_SYS_SNAP, 0x01 }, /* Enable */
  4610. { OV511_REG_BUS, 0x31, 0x0f },
  4611. { OV511_REG_BUS, 0x5d, 0x03 },
  4612. { OV511_REG_BUS, 0x24, 0x9f },
  4613. { OV511_REG_BUS, 0x25, 0x90 },
  4614. { OV511_REG_BUS, 0x20, 0x00 },
  4615. { OV511_REG_BUS, 0x51, 0x04 },
  4616. { OV511_REG_BUS, 0x71, 0x19 },
  4617. { OV511_DONE_BUS, 0x0, 0x00 },
  4618. };
  4619. static struct ov511_regvals aRegvalsNorm518Plus[] = {
  4620. { OV511_REG_BUS, R51x_SYS_SNAP, 0x02 }, /* Reset */
  4621. { OV511_REG_BUS, R51x_SYS_SNAP, 0x01 }, /* Enable */
  4622. { OV511_REG_BUS, 0x31, 0x0f },
  4623. { OV511_REG_BUS, 0x5d, 0x03 },
  4624. { OV511_REG_BUS, 0x24, 0x9f },
  4625. { OV511_REG_BUS, 0x25, 0x90 },
  4626. { OV511_REG_BUS, 0x20, 0x60 },
  4627. { OV511_REG_BUS, 0x51, 0x02 },
  4628. { OV511_REG_BUS, 0x71, 0x19 },
  4629. { OV511_REG_BUS, 0x40, 0xff },
  4630. { OV511_REG_BUS, 0x41, 0x42 },
  4631. { OV511_REG_BUS, 0x46, 0x00 },
  4632. { OV511_REG_BUS, 0x33, 0x04 },
  4633. { OV511_REG_BUS, 0x21, 0x19 },
  4634. { OV511_REG_BUS, 0x3f, 0x10 },
  4635. { OV511_DONE_BUS, 0x0, 0x00 },
  4636. };
  4637. PDEBUG(4, "");
  4638. /* First 5 bits of custom ID reg are a revision ID on OV518 */
  4639. dev_info(&ov->dev->dev, "Device revision %d\n",
  4640. 0x1F & reg_r(ov, R511_SYS_CUST_ID));
  4641. /* Give it the default description */
  4642. ov->desc = symbolic(camlist, 0);
  4643. if (write_regvals(ov, aRegvalsInit518))
  4644. goto error;
  4645. /* Set LED GPIO pin to output mode */
  4646. if (reg_w_mask(ov, 0x57, 0x00, 0x02) < 0)
  4647. goto error;
  4648. /* LED is off by default with OV518; have to explicitly turn it on */
  4649. if (ov->led_policy == LED_OFF || ov->led_policy == LED_AUTO)
  4650. ov51x_led_control(ov, 0);
  4651. else
  4652. ov51x_led_control(ov, 1);
  4653. /* Don't require compression if dumppix is enabled; otherwise it's
  4654. * required. OV518 has no uncompressed mode, to save RAM. */
  4655. if (!dumppix && !ov->compress) {
  4656. ov->compress = 1;
  4657. dev_warn(&ov->dev->dev,
  4658. "Compression required with OV518...enabling\n");
  4659. }
  4660. if (ov->bridge == BRG_OV518) {
  4661. if (write_regvals(ov, aRegvalsNorm518))
  4662. goto error;
  4663. } else if (ov->bridge == BRG_OV518PLUS) {
  4664. if (write_regvals(ov, aRegvalsNorm518Plus))
  4665. goto error;
  4666. } else {
  4667. err("Invalid bridge");
  4668. }
  4669. if (reg_w(ov, 0x2f, 0x80) < 0)
  4670. goto error;
  4671. if (ov518_init_compression(ov))
  4672. goto error;
  4673. if (ov->bridge == BRG_OV518)
  4674. {
  4675. struct usb_interface *ifp;
  4676. struct usb_host_interface *alt;
  4677. __u16 mxps = 0;
  4678. ifp = usb_ifnum_to_if(ov->dev, 0);
  4679. if (ifp) {
  4680. alt = usb_altnum_to_altsetting(ifp, 7);
  4681. if (alt)
  4682. mxps = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize);
  4683. }
  4684. /* Some OV518s have packet numbering by default, some don't */
  4685. if (mxps == 897)
  4686. ov->packet_numbering = 1;
  4687. else
  4688. ov->packet_numbering = 0;
  4689. } else {
  4690. /* OV518+ has packet numbering turned on by default */
  4691. ov->packet_numbering = 1;
  4692. }
  4693. ov518_set_packet_size(ov, 0);
  4694. ov->snap_enabled = snapshot;
  4695. /* Test for 76xx */
  4696. ov->primary_i2c_slave = OV7xx0_SID;
  4697. if (ov51x_set_slave_ids(ov, OV7xx0_SID) < 0)
  4698. goto error;
  4699. /* The OV518 must be more aggressive about sensor detection since
  4700. * I2C write will never fail if the sensor is not present. We have
  4701. * to try to initialize the sensor to detect its presence */
  4702. if (init_ov_sensor(ov) < 0) {
  4703. /* Test for 6xx0 */
  4704. ov->primary_i2c_slave = OV6xx0_SID;
  4705. if (ov51x_set_slave_ids(ov, OV6xx0_SID) < 0)
  4706. goto error;
  4707. if (init_ov_sensor(ov) < 0) {
  4708. /* Test for 8xx0 */
  4709. ov->primary_i2c_slave = OV8xx0_SID;
  4710. if (ov51x_set_slave_ids(ov, OV8xx0_SID) < 0)
  4711. goto error;
  4712. if (init_ov_sensor(ov) < 0) {
  4713. err("Can't determine sensor slave IDs");
  4714. goto error;
  4715. } else {
  4716. err("Detected unsupported OV8xx0 sensor");
  4717. goto error;
  4718. }
  4719. } else {
  4720. if (ov6xx0_configure(ov) < 0) {
  4721. err("Failed to configure OV6xx0");
  4722. goto error;
  4723. }
  4724. }
  4725. } else {
  4726. if (ov7xx0_configure(ov) < 0) {
  4727. err("Failed to configure OV7xx0");
  4728. goto error;
  4729. }
  4730. }
  4731. ov->maxwidth = 352;
  4732. ov->maxheight = 288;
  4733. // The OV518 cannot go as low as the sensor can
  4734. ov->minwidth = 160;
  4735. ov->minheight = 120;
  4736. return 0;
  4737. error:
  4738. err("OV518 Config failed");
  4739. return -EBUSY;
  4740. }
  4741. /****************************************************************************
  4742. * sysfs
  4743. ***************************************************************************/
  4744. static inline struct usb_ov511 *cd_to_ov(struct device *cd)
  4745. {
  4746. struct video_device *vdev = to_video_device(cd);
  4747. return video_get_drvdata(vdev);
  4748. }
  4749. static ssize_t show_custom_id(struct device *cd,
  4750. struct device_attribute *attr, char *buf)
  4751. {
  4752. struct usb_ov511 *ov = cd_to_ov(cd);
  4753. return sprintf(buf, "%d\n", ov->customid);
  4754. }
  4755. static DEVICE_ATTR(custom_id, S_IRUGO, show_custom_id, NULL);
  4756. static ssize_t show_model(struct device *cd,
  4757. struct device_attribute *attr, char *buf)
  4758. {
  4759. struct usb_ov511 *ov = cd_to_ov(cd);
  4760. return sprintf(buf, "%s\n", ov->desc);
  4761. }
  4762. static DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
  4763. static ssize_t show_bridge(struct device *cd,
  4764. struct device_attribute *attr, char *buf)
  4765. {
  4766. struct usb_ov511 *ov = cd_to_ov(cd);
  4767. return sprintf(buf, "%s\n", symbolic(brglist, ov->bridge));
  4768. }
  4769. static DEVICE_ATTR(bridge, S_IRUGO, show_bridge, NULL);
  4770. static ssize_t show_sensor(struct device *cd,
  4771. struct device_attribute *attr, char *buf)
  4772. {
  4773. struct usb_ov511 *ov = cd_to_ov(cd);
  4774. return sprintf(buf, "%s\n", symbolic(senlist, ov->sensor));
  4775. }
  4776. static DEVICE_ATTR(sensor, S_IRUGO, show_sensor, NULL);
  4777. static ssize_t show_brightness(struct device *cd,
  4778. struct device_attribute *attr, char *buf)
  4779. {
  4780. struct usb_ov511 *ov = cd_to_ov(cd);
  4781. unsigned short x;
  4782. if (!ov->dev)
  4783. return -ENODEV;
  4784. sensor_get_brightness(ov, &x);
  4785. return sprintf(buf, "%d\n", x >> 8);
  4786. }
  4787. static DEVICE_ATTR(brightness, S_IRUGO, show_brightness, NULL);
  4788. static ssize_t show_saturation(struct device *cd,
  4789. struct device_attribute *attr, char *buf)
  4790. {
  4791. struct usb_ov511 *ov = cd_to_ov(cd);
  4792. unsigned short x;
  4793. if (!ov->dev)
  4794. return -ENODEV;
  4795. sensor_get_saturation(ov, &x);
  4796. return sprintf(buf, "%d\n", x >> 8);
  4797. }
  4798. static DEVICE_ATTR(saturation, S_IRUGO, show_saturation, NULL);
  4799. static ssize_t show_contrast(struct device *cd,
  4800. struct device_attribute *attr, char *buf)
  4801. {
  4802. struct usb_ov511 *ov = cd_to_ov(cd);
  4803. unsigned short x;
  4804. if (!ov->dev)
  4805. return -ENODEV;
  4806. sensor_get_contrast(ov, &x);
  4807. return sprintf(buf, "%d\n", x >> 8);
  4808. }
  4809. static DEVICE_ATTR(contrast, S_IRUGO, show_contrast, NULL);
  4810. static ssize_t show_hue(struct device *cd,
  4811. struct device_attribute *attr, char *buf)
  4812. {
  4813. struct usb_ov511 *ov = cd_to_ov(cd);
  4814. unsigned short x;
  4815. if (!ov->dev)
  4816. return -ENODEV;
  4817. sensor_get_hue(ov, &x);
  4818. return sprintf(buf, "%d\n", x >> 8);
  4819. }
  4820. static DEVICE_ATTR(hue, S_IRUGO, show_hue, NULL);
  4821. static ssize_t show_exposure(struct device *cd,
  4822. struct device_attribute *attr, char *buf)
  4823. {
  4824. struct usb_ov511 *ov = cd_to_ov(cd);
  4825. unsigned char exp = 0;
  4826. if (!ov->dev)
  4827. return -ENODEV;
  4828. sensor_get_exposure(ov, &exp);
  4829. return sprintf(buf, "%d\n", exp);
  4830. }
  4831. static DEVICE_ATTR(exposure, S_IRUGO, show_exposure, NULL);
  4832. static int ov_create_sysfs(struct video_device *vdev)
  4833. {
  4834. int rc;
  4835. rc = device_create_file(&vdev->dev, &dev_attr_custom_id);
  4836. if (rc) goto err;
  4837. rc = device_create_file(&vdev->dev, &dev_attr_model);
  4838. if (rc) goto err_id;
  4839. rc = device_create_file(&vdev->dev, &dev_attr_bridge);
  4840. if (rc) goto err_model;
  4841. rc = device_create_file(&vdev->dev, &dev_attr_sensor);
  4842. if (rc) goto err_bridge;
  4843. rc = device_create_file(&vdev->dev, &dev_attr_brightness);
  4844. if (rc) goto err_sensor;
  4845. rc = device_create_file(&vdev->dev, &dev_attr_saturation);
  4846. if (rc) goto err_bright;
  4847. rc = device_create_file(&vdev->dev, &dev_attr_contrast);
  4848. if (rc) goto err_sat;
  4849. rc = device_create_file(&vdev->dev, &dev_attr_hue);
  4850. if (rc) goto err_contrast;
  4851. rc = device_create_file(&vdev->dev, &dev_attr_exposure);
  4852. if (rc) goto err_hue;
  4853. return 0;
  4854. err_hue:
  4855. device_remove_file(&vdev->dev, &dev_attr_hue);
  4856. err_contrast:
  4857. device_remove_file(&vdev->dev, &dev_attr_contrast);
  4858. err_sat:
  4859. device_remove_file(&vdev->dev, &dev_attr_saturation);
  4860. err_bright:
  4861. device_remove_file(&vdev->dev, &dev_attr_brightness);
  4862. err_sensor:
  4863. device_remove_file(&vdev->dev, &dev_attr_sensor);
  4864. err_bridge:
  4865. device_remove_file(&vdev->dev, &dev_attr_bridge);
  4866. err_model:
  4867. device_remove_file(&vdev->dev, &dev_attr_model);
  4868. err_id:
  4869. device_remove_file(&vdev->dev, &dev_attr_custom_id);
  4870. err:
  4871. return rc;
  4872. }
  4873. /****************************************************************************
  4874. * USB routines
  4875. ***************************************************************************/
  4876. static int
  4877. ov51x_probe(struct usb_interface *intf, const struct usb_device_id *id)
  4878. {
  4879. struct usb_device *dev = interface_to_usbdev(intf);
  4880. struct usb_interface_descriptor *idesc;
  4881. struct usb_ov511 *ov;
  4882. int i, rc, nr;
  4883. PDEBUG(1, "probing for device...");
  4884. /* We don't handle multi-config cameras */
  4885. if (dev->descriptor.bNumConfigurations != 1)
  4886. return -ENODEV;
  4887. idesc = &intf->cur_altsetting->desc;
  4888. if (idesc->bInterfaceClass != 0xFF)
  4889. return -ENODEV;
  4890. if (idesc->bInterfaceSubClass != 0x00)
  4891. return -ENODEV;
  4892. if ((ov = kzalloc(sizeof(*ov), GFP_KERNEL)) == NULL) {
  4893. err("couldn't kmalloc ov struct");
  4894. goto error_out;
  4895. }
  4896. ov->dev = dev;
  4897. ov->iface = idesc->bInterfaceNumber;
  4898. ov->led_policy = led;
  4899. ov->compress = compress;
  4900. ov->lightfreq = lightfreq;
  4901. ov->num_inputs = 1; /* Video decoder init functs. change this */
  4902. ov->stop_during_set = !fastset;
  4903. ov->backlight = backlight;
  4904. ov->mirror = mirror;
  4905. ov->auto_brt = autobright;
  4906. ov->auto_gain = autogain;
  4907. ov->auto_exp = autoexp;
  4908. switch (le16_to_cpu(dev->descriptor.idProduct)) {
  4909. case PROD_OV511:
  4910. ov->bridge = BRG_OV511;
  4911. ov->bclass = BCL_OV511;
  4912. break;
  4913. case PROD_OV511PLUS:
  4914. ov->bridge = BRG_OV511PLUS;
  4915. ov->bclass = BCL_OV511;
  4916. break;
  4917. case PROD_OV518:
  4918. ov->bridge = BRG_OV518;
  4919. ov->bclass = BCL_OV518;
  4920. break;
  4921. case PROD_OV518PLUS:
  4922. ov->bridge = BRG_OV518PLUS;
  4923. ov->bclass = BCL_OV518;
  4924. break;
  4925. case PROD_ME2CAM:
  4926. if (le16_to_cpu(dev->descriptor.idVendor) != VEND_MATTEL)
  4927. goto error;
  4928. ov->bridge = BRG_OV511PLUS;
  4929. ov->bclass = BCL_OV511;
  4930. break;
  4931. default:
  4932. err("Unknown product ID 0x%04x", le16_to_cpu(dev->descriptor.idProduct));
  4933. goto error;
  4934. }
  4935. dev_info(&intf->dev, "USB %s video device found\n",
  4936. symbolic(brglist, ov->bridge));
  4937. init_waitqueue_head(&ov->wq);
  4938. mutex_init(&ov->lock); /* to 1 == available */
  4939. mutex_init(&ov->buf_lock);
  4940. mutex_init(&ov->i2c_lock);
  4941. mutex_init(&ov->cbuf_lock);
  4942. ov->buf_state = BUF_NOT_ALLOCATED;
  4943. if (usb_make_path(dev, ov->usb_path, OV511_USB_PATH_LEN) < 0) {
  4944. err("usb_make_path error");
  4945. goto error;
  4946. }
  4947. /* Allocate control transfer buffer. */
  4948. /* Must be kmalloc()'ed, for DMA compatibility */
  4949. ov->cbuf = kmalloc(OV511_CBUF_SIZE, GFP_KERNEL);
  4950. if (!ov->cbuf)
  4951. goto error;
  4952. if (ov->bclass == BCL_OV518) {
  4953. if (ov518_configure(ov) < 0)
  4954. goto error;
  4955. } else {
  4956. if (ov511_configure(ov) < 0)
  4957. goto error;
  4958. }
  4959. for (i = 0; i < OV511_NUMFRAMES; i++) {
  4960. ov->frame[i].framenum = i;
  4961. init_waitqueue_head(&ov->frame[i].wq);
  4962. }
  4963. for (i = 0; i < OV511_NUMSBUF; i++) {
  4964. ov->sbuf[i].ov = ov;
  4965. spin_lock_init(&ov->sbuf[i].lock);
  4966. ov->sbuf[i].n = i;
  4967. }
  4968. /* Unnecessary? (This is done on open(). Need to make sure variables
  4969. * are properly initialized without this before removing it, though). */
  4970. if (ov51x_set_default_params(ov) < 0)
  4971. goto error;
  4972. #ifdef OV511_DEBUG
  4973. if (dump_bridge) {
  4974. if (ov->bclass == BCL_OV511)
  4975. ov511_dump_regs(ov);
  4976. else
  4977. ov518_dump_regs(ov);
  4978. }
  4979. #endif
  4980. ov->vdev = video_device_alloc();
  4981. if (!ov->vdev)
  4982. goto error;
  4983. memcpy(ov->vdev, &vdev_template, sizeof(*ov->vdev));
  4984. ov->vdev->parent = &intf->dev;
  4985. video_set_drvdata(ov->vdev, ov);
  4986. mutex_lock(&ov->lock);
  4987. /* Check to see next free device and mark as used */
  4988. nr = find_first_zero_bit(&ov511_devused, OV511_MAX_UNIT_VIDEO);
  4989. /* Registers device */
  4990. if (unit_video[nr] != 0)
  4991. rc = video_register_device(ov->vdev, VFL_TYPE_GRABBER,
  4992. unit_video[nr]);
  4993. else
  4994. rc = video_register_device(ov->vdev, VFL_TYPE_GRABBER, -1);
  4995. if (rc < 0) {
  4996. err("video_register_device failed");
  4997. mutex_unlock(&ov->lock);
  4998. goto error;
  4999. }
  5000. /* Mark device as used */
  5001. ov511_devused |= 1 << nr;
  5002. ov->nr = nr;
  5003. dev_info(&intf->dev, "Device at %s registered to %s\n",
  5004. ov->usb_path, video_device_node_name(ov->vdev));
  5005. usb_set_intfdata(intf, ov);
  5006. if (ov_create_sysfs(ov->vdev)) {
  5007. err("ov_create_sysfs failed");
  5008. ov511_devused &= ~(1 << nr);
  5009. mutex_unlock(&ov->lock);
  5010. goto error;
  5011. }
  5012. mutex_unlock(&ov->lock);
  5013. return 0;
  5014. error:
  5015. if (ov->vdev) {
  5016. if (!video_is_registered(ov->vdev))
  5017. video_device_release(ov->vdev);
  5018. else
  5019. video_unregister_device(ov->vdev);
  5020. ov->vdev = NULL;
  5021. }
  5022. if (ov->cbuf) {
  5023. mutex_lock(&ov->cbuf_lock);
  5024. kfree(ov->cbuf);
  5025. ov->cbuf = NULL;
  5026. mutex_unlock(&ov->cbuf_lock);
  5027. }
  5028. kfree(ov);
  5029. ov = NULL;
  5030. error_out:
  5031. err("Camera initialization failed");
  5032. return -EIO;
  5033. }
  5034. static void
  5035. ov51x_disconnect(struct usb_interface *intf)
  5036. {
  5037. struct usb_ov511 *ov = usb_get_intfdata(intf);
  5038. int n;
  5039. PDEBUG(3, "");
  5040. mutex_lock(&ov->lock);
  5041. usb_set_intfdata (intf, NULL);
  5042. if (!ov) {
  5043. mutex_unlock(&ov->lock);
  5044. return;
  5045. }
  5046. /* Free device number */
  5047. ov511_devused &= ~(1 << ov->nr);
  5048. if (ov->vdev)
  5049. video_unregister_device(ov->vdev);
  5050. for (n = 0; n < OV511_NUMFRAMES; n++)
  5051. ov->frame[n].grabstate = FRAME_ERROR;
  5052. ov->curframe = -1;
  5053. /* This will cause the process to request another frame */
  5054. for (n = 0; n < OV511_NUMFRAMES; n++)
  5055. wake_up_interruptible(&ov->frame[n].wq);
  5056. wake_up_interruptible(&ov->wq);
  5057. ov->streaming = 0;
  5058. ov51x_unlink_isoc(ov);
  5059. mutex_unlock(&ov->lock);
  5060. ov->dev = NULL;
  5061. /* Free the memory */
  5062. if (ov && !ov->user) {
  5063. mutex_lock(&ov->cbuf_lock);
  5064. kfree(ov->cbuf);
  5065. ov->cbuf = NULL;
  5066. mutex_unlock(&ov->cbuf_lock);
  5067. ov51x_dealloc(ov);
  5068. kfree(ov);
  5069. ov = NULL;
  5070. }
  5071. PDEBUG(3, "Disconnect complete");
  5072. }
  5073. static struct usb_driver ov511_driver = {
  5074. .name = "ov511",
  5075. .id_table = device_table,
  5076. .probe = ov51x_probe,
  5077. .disconnect = ov51x_disconnect
  5078. };
  5079. /****************************************************************************
  5080. *
  5081. * Module routines
  5082. *
  5083. ***************************************************************************/
  5084. static int __init
  5085. usb_ov511_init(void)
  5086. {
  5087. int retval;
  5088. retval = usb_register(&ov511_driver);
  5089. if (retval)
  5090. goto out;
  5091. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  5092. DRIVER_DESC "\n");
  5093. out:
  5094. return retval;
  5095. }
  5096. static void __exit
  5097. usb_ov511_exit(void)
  5098. {
  5099. usb_deregister(&ov511_driver);
  5100. printk(KERN_INFO KBUILD_MODNAME ": driver deregistered\n");
  5101. }
  5102. module_init(usb_ov511_init);
  5103. module_exit(usb_ov511_exit);