ov511.c 140 KB

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