usbvision-core.c 174 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880
  1. /*
  2. * USB USBVISION Video device driver 0.9.8.3cvs (For Kernel 2.4.19-2.4.32 + 2.6.0-2.6.16)
  3. *
  4. *
  5. *
  6. * Copyright (c) 1999-2005 Joerg Heckenbach <joerg@heckenbach-aw.de>
  7. *
  8. * This module is part of usbvision driver project.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. *
  24. * Let's call the version 0.... until compression decoding is completely
  25. * implemented.
  26. *
  27. * This driver is written by Jose Ignacio Gijon and Joerg Heckenbach.
  28. * It was based on USB CPiA driver written by Peter Pregler,
  29. * Scott J. Bertin and Johannes Erdfelt
  30. * Ideas are taken from bttv driver by Ralph Metzler, Marcus Metzler &
  31. * Gerd Knorr and zoran 36120/36125 driver by Pauline Middelink
  32. * Updates to driver completed by Dwaine P. Garden
  33. *
  34. * History:
  35. *
  36. * Mar. 2000 - 15.12.2000: (0.0.0 - 0.2.0)
  37. * Several alpha drivers and the first beta.
  38. *
  39. * Since Dec. 2000: (0.2.1) or (v2.1)
  40. * Code changes or updates by Dwaine Garden and every other person.
  41. *
  42. * Added: New Hauppauge TV device Vendor ID: 0x0573
  43. * Product ID: 0x4D01
  44. * (Thanks to Giovanni Garberoglio)
  45. *
  46. * Added: UK Hauppauge WinTV-USB Vendor ID: 0x0573
  47. * Product ID: 0x4D02
  48. * (Thanks to Derek Freeman-Jones)
  49. *
  50. * Feb, 2001 - Apr 08, 2001: (0.3.0)
  51. * - Some fixes. Driver is now more stable.
  52. * - Scratch is organized as ring-buffer now for better performance
  53. * - DGA (overlay) is now supported.
  54. * !!!!Danger!!!! Clipping is not yet implemented. Your system will
  55. * crash if your video window leaves the screen!!!
  56. * - Max. Framesize is set to 320x240. There isn't more memory on the
  57. * nt1003 video device for the FIFO.
  58. * - Supported video palettes: RGB565, RGB555, RGB24, RGB32
  59. *
  60. *
  61. * Apr 15, 2001: (0.3.1-test...)
  62. * - Clipping is implemented
  63. * - NTSC is now coloured (Thanks to Dwaine Garden)
  64. * - Added SECAM colour detection in saa7111-new
  65. * - Added: French Hauppauge WinTV USB Vendor ID: 0x0573
  66. * Product ID: 0x4D03
  67. * (Thanks to Julius Hrivnac)
  68. * - Added: US Hauppauge WINTV USB Vendor ID: 0x0573
  69. * Product ID: 0x4D00
  70. * (Thanks to Derrick J Brashear)
  71. * - Changes for adding new devices. There's now a table in usbvision.h.
  72. * Adding your devices data to the usbvision_device_data table is all
  73. * you need to add a new device.
  74. *
  75. * May 11, 2001: (0.3.2-test...) (Thanks to Derek Freeman-Jones)
  76. * - Support YUV422 raw format for people with hardware scaling.
  77. * - Only power on the device when opened (use option PowerOnAtOpen=0 to disable it).
  78. * - Turn off audio so we can listen to Line In.
  79. *
  80. * July 5, 2001 - (Patch the driver to run with Kernel 2.4.6)
  81. * - Fixed a problem with the number of parameters passed to video_register_device.
  82. *
  83. * July 6, 2001 - Added: HAUPPAUGE WINTV-USB FM USA Vendor ID: 0x0573
  84. * Product ID: 0x4D10
  85. * (Thanks to Braddock Gaskill)
  86. * Added: USBGear USBG-V1 resp. HAMA USB
  87. * Vendor ID: 0x0573
  88. * Product ID: 0x0003
  89. * (Thanks to Bradley A. Singletary and Juergen Weigert)
  90. *
  91. * Jan 24, 2002 - (0.3.3-test...)
  92. * - Moved all global variables that are device specific the usb_usbvision struct
  93. * - Fixed the 64x48 unchangable image in xawtv when starting it with overlay
  94. * - Add VideoNorm and TunerType to the usb_device_data table
  95. * - Checked the audio channels and mute for HAUPPAUGE WinTV USB FM
  96. * - Implemented the power on when opening the device. But some software opens
  97. * the device several times when starting. So the i2c parts are just registered
  98. * by an open, when they become deregistered by the next close. You can speed
  99. * up tuner detection, when adding "options tuner addr=your_addr" to /etc/modules.conf
  100. * - Begin to resize the frame in width and height. So it will be possible to watch i.e.
  101. * 384x288 pixels at 23 fps.
  102. *
  103. * Feb 10, 2002
  104. * - Added radio device
  105. *
  106. *
  107. * Jul 30, 2002 - (Thanks Cameron Maxwell)
  108. * - Changes to usbvision.h --fixed usbvision device data structure, incorrectly had (0x0573, 0x4d21) for WinTV-USB II, should be 0x4d20.
  109. * - Changes for device WinTV-USB II (0x0573. 0x4D21). It does not have a FM tuner.
  110. * - Added the real device HAUPPAUGE WINTV-USB II (PAL) to the device structure in usbvision.h.
  111. * - Changes to saa7113-new, the video is 8 bit data for the Phillips SAA7113 not 16bit like SAA7111.
  112. * - Tuned lots of setup registers for the Phillips SAA7113 video chipset.
  113. * - Changes to the supplied makefile. (Dwaine Garden) Still needs to be fixed so it will compile modules on different distrubutions.
  114. *
  115. *
  116. * Aug 10, 2002 - (Thanks Mike Klinke)
  117. * - Changes to usbvision.txt -- Fixed instructions on the location to copy the contents of the tgz file.
  118. * - Added device WinTV-USB FM Model 621 (0x0573. 0x4D30). There is another device which carries the same name. Kept that device in the device structure.
  119. *
  120. * Aug 12, 2002 - Dwaine Garden
  121. * - Added the ability to read the NT100x chip for the MaxISOPacketLength and USB Bandwidth
  122. * Setting of the video device.
  123. * - Adjustments to the SAA7113H code for proper video output.
  124. * - Changes to usbvision.h, so all the devices with FM tuners are working.
  125. *
  126. * Feb 10, 2003 - Joerg Heckenbach
  127. * - fixed endian bug for Motorola PPC
  128. *
  129. * Feb 13, 2003 - Joerg Heckenbach
  130. * - fixed Vin_Reg setting and presetting from usbvision_device_data()
  131. *
  132. * Apr 19, 2003 - Dwaine Garden
  133. * - Fixed compiling errors under RedHat v9.0. from uvirt_to_kva and usbvision_mmap. (Thanks Cameron Maxwell)
  134. * - Changed pte_offset to pte_offset_kernel.
  135. * - Changed remap_page_range and added additional parameter to function.
  136. * - Change setup parameters for the D-Link V100 USB device
  137. * - Added a new device to the usbvision driver. Pinnacle Studio PCTV USB (PAL) 0x2304 0x0110
  138. * - Screwed up the sourceforge.net cvs respository! 8*)
  139. *
  140. * Apr 22, 2002 - Dwaine Garden
  141. * - Added a new device to the usbvision driver. Dazzle DVC-80 (PAL) 0x07d0 0x0004. (Thanks Carl Anderson)
  142. * - Changes to some of the comments.
  143. *
  144. * June 06, 2002 - Ivan, Dwaine Garden
  145. * - Ivan updates for fine tuning device parameters without driver recompiling. (Ivan)
  146. * - Changes to some of the comments. (Dwaine Garden)
  147. * - Changes to the makefile - Better CPU settings. (Ivan)
  148. * - Changes to device Hauppauge WinTv-USB III (PAL) FM Model 568 - Fine tuning parameters (Ivan)
  149. *
  150. *
  151. * Oct 16, 2003 - 0.9.0 - Joerg Heckenbach
  152. * - Implementation of the first part of the decompression algorithm for intra frames.
  153. * The resolution has to be 320x240. A dynamic adaption of compression deepth is
  154. * missing yet.
  155. *
  156. * Oct 22, 2003 - 0.9.1 - Joerg Heckenbach
  157. * - Implementation of the decompression algorithm for inter frames.
  158. * The resolution still has to be 320x240.
  159. *
  160. * Nov 2003 - Feb 2004 - 0.9.2 to 0.9.3 - Joerg Heckenbach
  161. * - Implement last unknown compressed block type. But color is still noisy.
  162. * - Finding criteria for adaptive compression adjustment.
  163. * - Porting to 2.6 kernels, but still working under 2.4
  164. *
  165. * Feb 04, 2004 - 0.9.4 Joerg Heckenbach
  166. * - Found bug in color decompression. Color is OK now.
  167. *
  168. * Feb 09, 2004 - 0.9.5 Joerg Heckenbach
  169. * - Add auto-recognition of chip type NT1003 or NT1004.
  170. * - Add adaptive compression adjustment.
  171. * - Patched saa7113 multiplexer switching (Thanks to Orlando F.S. Bordoni)
  172. *
  173. * Feb 24, 2004 - 0.9.6 Joerg Heckenbach
  174. * - Add a timer to wait before poweroff at close, to save start time in
  175. * some video applications
  176. *
  177. * Mar 4, 2004 - 0.9.6 Dwaine Garden
  178. * - Added device Global Village GV-007 (NTSC) to usbvision.h (Thanks to Abe Skolnik)
  179. * - Forgot to add this device to the driver. 8*)
  180. *
  181. * June 2, 2004 - 0.9.6 Dwaine Garden
  182. * - Fixed sourceforge.net cvs repository.
  183. * - Added #if LINUX_VERSION_CODE > KERNEL_VERSION(2,4,26) for .owner to help compiling under kernels 2.4.x which do not have the i2c v2.8.x updates.
  184. * - Added device Hauppauge WinTv-USB III (PAL) FM Model 597 to usbvision.h
  185. *
  186. * July 1, 2004 -0.9.6 Dwaine Garden
  187. * - Patch was submitted by Hal Finkel to fix the problem with the tuner not working under kernel 2.6.7.
  188. * - Thanks Hal.....
  189. *
  190. * July 30, 2004 - 0.9.6 Dwaine Garden
  191. * - Patch was submitted by Tobias Diaz to fix Model ID mismatch in usbvision.h.
  192. * - Thanks.....
  193. *
  194. * August 12, 2004 - 0.9.6 Dwaine Garden
  195. * - Updated the readme file so people could install the driver under the configuration file for kernel 2.6.x recompiles. Now people can use make xconfig!
  196. * - Added new device "Camtel Technology Corp TVB330-USB FM".
  197. * - Sourceforge.net CVS has been updated with all the changes.
  198. *
  199. * August 20, 2004 - 0.9.7 Dwaine Garden
  200. * - Added Device "Hauppauge USB Live Model 600"
  201. * - Fixed up all the devices which did not have a default tuner type in usbvision.h. It's best guess, at least until someone with the device tells me otherwise.
  202. * - Sourceforge.net CVS has been updated with all the changes.
  203. * - Clean up the driver.
  204. *
  205. * September 13, 2004 - 0.9.8 Dwaine Garden
  206. * - Changed usbvision_muxsel to address the problem with black & white s-video output for NT1004 devices with saa7114 video decoder. Thanks to Emmanuel for the patch and testing.
  207. * - Fixed up SECAM devices which could not properly output video. Changes to usbmuxsel. Thanks to Emmanuel for the patch and everyone with a SECAM device which help test.
  208. * - Removed some commented out code. Clean up.
  209. * - Tried to fix up the annoying empty directories in the sourceforge.net cvs. Fuck it up again. 8*(
  210. *
  211. * November 15, 2004 - 0.9.8 Dwaine Garden
  212. * - Release new tar - 0.9.8 on sourceforge.net
  213. * - Added some new devices to usbvision.h WinTV USB Model 602 40201 Rev B282, Hauppague WinTV USB Model 602 40201 Rev B285
  214. * - Added better compatibility for 2.6.x kernels.
  215. * - Hardware full screen scaling in grabdisplay mode.
  216. * - Better support for sysfs. More code to follow for both video device and radio device. Device information is located at /sys/class/video4linux/video0
  217. * - Added module_param so loaded module parameters are displayed in sysfs. Driver parameters should show up in /sys/module/usbvision
  218. * - Adjusted the SAA7111 registers to match the 2.6.x kernel SAA7111 code. Thanks to the person which helped test.
  219. * - Changed to wait_event_interruptible. For all the people running Fedora 2.
  220. * - Added some screenshots of actual video captures on sourceforge.net.
  221. *
  222. * November 24, 2004 - 0.9.8.1cvs Dwaine Garden
  223. * - Added patch to check for palette and format in VIDIOCSPICT. Helix Producer should work fine with the driver now. Thanks Jason Simpson
  224. * - Two device description changes and two additions for the maintainer of usb.ids.
  225. *
  226. * December 2, 2004 - 0.9.8.1cvs Dwaine Garden
  227. * - Added patch for YUV420P and YUV422P video output. Thanks to Alex Smith.
  228. * - Better support for mythtv.
  229. *
  230. * January 2, 2005 - 0.9.8.1cvs Dwaine Garden
  231. * - Setup that you can specify which device is used for video. Default is auto detect next available device number eg. /dev/videoX
  232. * - Setup that you can specify which device is used for radio. Default is auto detect next available device number eg. /dev/radioX
  233. * - usb_unlink_urb() is deprecated for synchronous unlinks. Using usb_kill_urb instead.
  234. * - usbvision_kvirt_to_pa is deprecated. Removed.
  235. * - Changes are related to kernel changes for 2.6.10. (Fedora 4)
  236. *
  237. * February 2, 2005 - 0.9.8.1cvs Dwaine Garden
  238. * - Added a new device to usbvision.h Dazzle DVC 50. Thanks to Luiz S.
  239. *
  240. * March 29, 2005 - 0.9.8.1cvs Dwaine Garden
  241. * - Fixed compile error with saa7113 under kernels 2.6.11+
  242. * - Added module parameter to help people with Black and White output with using s-video input. Some cables and input device are wired differently.
  243. * - Removed the .id from the i2c usbvision template. There was a change to the i2c with kernels 2.6.11+.
  244. *
  245. * April 9, 2005 - 0.9.8.1cvs Dwaine Garden
  246. * - Added in the 2.4 and 2.6 readme files the SwitchSVideoInput parameter information. This will help people setup the right values for the parameter.
  247. * If your device experiences Black and White images with the S-Video Input. Set this parameter to 1 when loading the module.
  248. * - Replaced the wrong 2.6 readme file. I lost the right version. Someone sent me the right version by e-mail. Thanks.
  249. * - Released new module version on sourceforge.net. So everyone can enjoy all the fixes and additional device support.
  250. *
  251. * April 20, 2005 - 0.9.8.2cvs Dwaine Garden
  252. * - Release lock in usbvision_v4l_read_done. -Thanks to nplanel for the patch.
  253. * - Additional comments to the driver.
  254. * - Fixed some spelling mistakes. 8*)
  255. *
  256. * April 23, 2005 - 0.9.8.2cvs Joerg Heckenbach
  257. * - Found bug in usbvision line counting. Now there should be no spurious lines in the image any longer.
  258. * - Swapped usbvision_register_video and usbvision_configure_video to fix problem with PowerOnAtOpen=0.
  259. * Thanks to Erwan Velu
  260. *
  261. * April 26, 2005 - 0.9.8.2cvs Joerg Heckenbach
  262. * - Fixed problem with rmmod module and oppses. Replaced vfree(usbvision->overlay_base) with iounmap(usbvision->overlay_base).
  263. * - Added function usb_get_dev(dev) and ; To help with unloading the module multiple times without crashing.
  264. * (Keep the reference count in kobjects correct)
  265. *
  266. * June 14, 2005 - 0.9.8.2cvs Dwaine
  267. * - Missed a change in saa7113.c for checking kernel version. Added conditional if's.
  268. *
  269. * June 15, 2005 - 0.9.8.2cvs Dwaine
  270. * - Added new device WinTV device VendorId 0573 and ProductId 4d29.
  271. * - Hacked some support for newer NT1005 devices. This devices only seem to have one configuration, not multiple configurations like the NT1004.
  272. *
  273. * June 29, 2005 - 0.9.8.2cvs Dwaine
  274. * - Added new device WinTV device VendorId 0573 and ProductId 4d37.
  275. * - Because of the first empty entry in usbvision_table, modutils failed to create the necessary entries for modules.usbmap.
  276. * This means hotplug won't work for usbvision. Thanks to Gary Ng.
  277. * - Sent an e-mail to the maintainer of usb.ids. New devices identified need to be added.
  278. * - Fixed compile error with saa7113 under kernel 2.6.12.
  279. *
  280. * July 6, 2005 - 0.9.8.2cvs Dwaine
  281. * - Patch submitted by Gary Ng for two additional procfs entries. Device Input and Frequency setting.
  282. *
  283. * July 12, 2005 - 0.9.8.2cvs Dwaine
  284. * - New tuner identified for some devices it's called TCL_MFPE05. This tuner uses the same API as tuner 38 in tuner.c.
  285. * - Thanks to lynx31 for contacting Hauppage and asking them.
  286. * - I have no clue as to which devices use this new tuner, so people will have to contact me and tell me.
  287. *
  288. * July 21, 2005 - 0.9.8.2cvs Dwaine
  289. * - Patched usbvision.c with missing ifdef kernversion statement so the module will compile with older kernels and v4l.
  290. * - Thanks to cipe007......
  291. *
  292. * May 19, 2006 - 0.9.8.3cvs Dwaine
  293. * - Patched usbvision.c and i2c-algo.c so they will compile with kernel 2.6.16
  294. * - Adjust device "Pinnacle Studio PCTV USB (PAL) FM" values in usbvision.h
  295. *
  296. * May 24, 2006 - 0.9.8.3cvs Dwaine
  297. * -Pinnacle Studio PCTV USB (NTSC) FM uses saa7111, not saa7113 like first thought.
  298. * -Updated usbvision.h
  299. *
  300. * Aug 15, 2006 - 0.9.8.3cvs Dwaine
  301. * -Added saa711x module into cvs, since the newer saa7115 module in newer kernels is v4l2. The usbvision driver is only v4l.
  302. * -Updated makefile to put compiled modules into correct location.
  303. *
  304. * Aug 21, 2006 - 0.9.8.3cvs Dwaine
  305. * -Changed number of bytes for i2c write to 4 as per the NT100X spec sheet. Thanks to Merlum for finding it.
  306. * -Remove the radio option for device Hauppauge WinTV USB device Model 40219 Rev E189. This device does not have a FM radio. Thanks to Shadwell.
  307. * -Added radio option for device Hauppauge WinTV USB device Model 40219 Rev E189 again. Just got an e-mail indicating their device has one. 8*)
  308. *
  309. * Aug 27, 2006 - 0.9.8.3cvs Dwaine
  310. * -Changed ifdef statement so the usbvision driver will compile with kernels at 2.6.12.
  311. * -Updated readme files for new updated tuner list for v4l devices.
  312. *
  313. *
  314. *
  315. * TODO:
  316. * - use submit_urb for all setup packets
  317. * - Fix memory settings for nt1004. It is 4 times as big as the
  318. * nt1003 memory.
  319. * - Add audio on endpoint 3 for nt1004 chip. Seems impossible, needs a codec interface. Which one?
  320. * - Clean up the driver.
  321. * - optimization for performance.
  322. * - Add Videotext capability (VBI). Working on it.....
  323. * - Check audio for other devices
  324. * - Add v4l2 interface
  325. *
  326. */
  327. #include <linux/kernel.h>
  328. #include <linux/sched.h>
  329. #include <linux/list.h>
  330. #include <linux/timer.h>
  331. #include <linux/slab.h>
  332. #include <linux/mm.h>
  333. #include <linux/utsname.h>
  334. #include <linux/highmem.h>
  335. #include <linux/smp_lock.h>
  336. #include <linux/videodev.h>
  337. #include <linux/vmalloc.h>
  338. #include <linux/module.h>
  339. #include <linux/init.h>
  340. #include <linux/spinlock.h>
  341. #include <linux/usb.h>
  342. #include <asm/io.h>
  343. #include <linux/videodev2.h>
  344. #include <linux/video_decoder.h>
  345. #include <linux/i2c.h>
  346. #include "usbvision-i2c.h"
  347. #define USBVISION_DRIVER_VERSION_MAJOR 0
  348. #define USBVISION_DRIVER_VERSION_MINOR 8
  349. #define USBVISION_DRIVER_VERSION_PATCHLEVEL 0
  350. #define USBVISION_VERSION __stringify(USBVISION_DRIVER_VERSION_MAJOR) "." __stringify(USBVISION_DRIVER_VERSION_MINOR) "." __stringify(USBVISION_DRIVER_VERSION_PATCHLEVEL) " " USBVISION_DRIVER_VERSION_COMMENT
  351. #define USBVISION_DRIVER_VERSION KERNEL_VERSION(USBVISION_DRIVER_VERSION_MAJOR,USBVISION_DRIVER_VERSION_MINOR,USBVISION_DRIVER_VERSION_PATCHLEVEL)
  352. #include <media/saa7115.h>
  353. #include <media/v4l2-common.h>
  354. #include <media/tuner.h>
  355. #include <media/audiochip.h>
  356. #include <linux/moduleparam.h>
  357. #include <linux/workqueue.h>
  358. #ifdef CONFIG_KMOD
  359. #include <linux/kmod.h>
  360. #endif
  361. #include "usbvision.h"
  362. #include "usbvision_ioctl.h"
  363. #define DRIVER_VERSION "0.9.8.3cvs for Linux kernels 2.4.19-2.4.32 + 2.6.0-2.6.17, compiled at "__DATE__", "__TIME__
  364. #define EMAIL "joerg@heckenbach-aw.de"
  365. #define DRIVER_AUTHOR "Joerg Heckenbach <joerg@heckenbach-aw.de>, Dwaine Garden <DwaineGarden@rogers.com>"
  366. #define DRIVER_DESC "USBVision USB Video Device Driver for Linux"
  367. #define DRIVER_LICENSE "GPL"
  368. #define DRIVER_ALIAS "USBVision"
  369. #define ENABLE_HEXDUMP 0 /* Enable if you need it */
  370. #define USBVISION_DEBUG /* Turn on debug messages */
  371. #ifdef USBVISION_DEBUG
  372. #define PDEBUG(level, fmt, args...) \
  373. if (debug & (level)) info("[%s:%d] " fmt, __PRETTY_FUNCTION__, __LINE__ , ## args)
  374. #else
  375. #define PDEBUG(level, fmt, args...) do {} while(0)
  376. #endif
  377. #define DBG_IOCTL 1<<3
  378. #define DBG_IO 1<<4
  379. #define DBG_RIO 1<<5
  380. #define DBG_HEADER 1<<7
  381. #define DBG_PROBE 1<<8
  382. #define DBG_IRQ 1<<9
  383. #define DBG_ISOC 1<<10
  384. #define DBG_PARSE 1<<11
  385. #define DBG_SCRATCH 1<<12
  386. #define DBG_FUNC 1<<13
  387. #define DBG_I2C 1<<14
  388. #define DEBUG(x...) /* General Debug */
  389. #define IODEBUG(x...) /* Debug IO */
  390. #define OVDEBUG(x...) /* Debug overlay */
  391. #define MDEBUG(x...) /* Debug memory management */
  392. //String operations
  393. #define rmspace(str) while(*str==' ') str++;
  394. #define goto2next(str) while(*str!=' ') str++; while(*str==' ') str++;
  395. static int usbvision_nr = 0; // sequential number of usbvision device
  396. static const int max_imgwidth = MAX_FRAME_WIDTH;
  397. static const int max_imgheight = MAX_FRAME_HEIGHT;
  398. static const int min_imgwidth = MIN_FRAME_WIDTH;
  399. static const int min_imgheight = MIN_FRAME_HEIGHT;
  400. #define FRAMERATE_MIN 0
  401. #define FRAMERATE_MAX 31
  402. enum {
  403. ISOC_MODE_YUV422 = 0x03,
  404. ISOC_MODE_YUV420 = 0x14,
  405. ISOC_MODE_COMPRESS = 0x60,
  406. };
  407. /*
  408. * The value of 'scratch_buf_size' affects quality of the picture
  409. * in many ways. Shorter buffers may cause loss of data when client
  410. * is too slow. Larger buffers are memory-consuming and take longer
  411. * to work with. This setting can be adjusted, but the default value
  412. * should be OK for most desktop users.
  413. */
  414. #define DEFAULT_SCRATCH_BUF_SIZE (0x20000) // 128kB memory scratch buffer
  415. static const int scratch_buf_size = DEFAULT_SCRATCH_BUF_SIZE;
  416. static int init_brightness = 128; // Initalize the brightness of the video device
  417. static int init_contrast = 192; // Initalize the contrast of the video device
  418. static int init_saturation = 128; // Initalize the staturation mode of the video device
  419. static int init_hue = 128; // Initalize the Hue settings of the video device
  420. // Function prototypes
  421. static int usbvision_restart_isoc(struct usb_usbvision *usbvision);
  422. static int usbvision_begin_streaming(struct usb_usbvision *usbvision);
  423. static int usbvision_muxsel(struct usb_usbvision *usbvision, int channel, int norm);
  424. static int usbvision_i2c_write(void *data, unsigned char addr, char *buf, short len);
  425. static int usbvision_i2c_read(void *data, unsigned char addr, char *buf, short len);
  426. static int usbvision_read_reg(struct usb_usbvision *usbvision, unsigned char reg);
  427. static int usbvision_write_reg(struct usb_usbvision *usbvision, unsigned char reg, unsigned char value);
  428. static int usbvision_request_intra (struct usb_usbvision *usbvision);
  429. static int usbvision_unrequest_intra (struct usb_usbvision *usbvision);
  430. static int usbvision_adjust_compression (struct usb_usbvision *usbvision);
  431. static int usbvision_measure_bandwidth (struct usb_usbvision *usbvision);
  432. static void usbvision_release(struct usb_usbvision *usbvision);
  433. static int usbvision_set_input(struct usb_usbvision *usbvision);
  434. static int usbvision_set_output(struct usb_usbvision *usbvision, int width, int height);
  435. static void call_i2c_clients(struct usb_usbvision *usbvision, unsigned int cmd, void *arg);
  436. // Bit flags (options)
  437. #define FLAGS_RETRY_VIDIOCSYNC (1 << 0)
  438. #define FLAGS_MONOCHROME (1 << 1)
  439. #define FLAGS_DISPLAY_HINTS (1 << 2)
  440. #define FLAGS_OSD_STATS (1 << 3)
  441. #define FLAGS_FORCE_TESTPATTERN (1 << 4)
  442. #define FLAGS_SEPARATE_FRAMES (1 << 5)
  443. #define FLAGS_CLEAN_FRAMES (1 << 6)
  444. // Default initalization of device driver parameters
  445. static int flags = 0; // Set the default Overlay Display mode of the device driver
  446. static int debug = 0; // Set the default Debug Mode of the device driver
  447. static int isocMode = ISOC_MODE_COMPRESS; // Set the default format for ISOC endpoint
  448. static int adjustCompression = 1; // Set the compression to be adaptive
  449. static int dga = 1; // Set the default Direct Graphic Access
  450. static int PowerOnAtOpen = 1; // Set the default device to power on at startup
  451. static int SwitchSVideoInput = 0; // To help people with Black and White output with using s-video input. Some cables and input device are wired differently.
  452. static int video_nr = -1; // Sequential Number of Video Device
  453. static int radio_nr = -1; // Sequential Number of Radio Device
  454. static int vbi_nr = -1; // Sequential Number of VBI Device
  455. static char *CustomDevice=NULL; // Set as nothing....
  456. // Grab parameters for the device driver
  457. #if defined(module_param) // Showing parameters under SYSFS
  458. module_param(flags, int, 0444);
  459. module_param(debug, int, 0444);
  460. module_param(isocMode, int, 0444);
  461. module_param(adjustCompression, int, 0444);
  462. module_param(dga, int, 0444);
  463. module_param(PowerOnAtOpen, int, 0444);
  464. module_param(SwitchSVideoInput, int, 0444);
  465. module_param(video_nr, int, 0444);
  466. module_param(radio_nr, int, 0444);
  467. module_param(vbi_nr, int, 0444);
  468. module_param(CustomDevice, charp, 0444);
  469. #else // Old Style
  470. MODULE_PARM(flags, "i"); // Grab the Overlay Display mode of the device driver
  471. MODULE_PARM(debug, "i"); // Grab the Debug Mode of the device driver
  472. MODULE_PARM(isocMode, "i"); // Grab the video format of the video device
  473. MODULE_PARM(adjustCompression, "i"); // Grab the compression to be adaptive
  474. MODULE_PARM(dga, "i"); // Grab the Direct Graphic Access
  475. MODULE_PARM(PowerOnAtOpen, "i"); // Grab the device to power on at startup
  476. MODULE_PARM(SwitchSVideoInput, "i"); // To help people with Black and White output with using s-video input. Some cables and input device are wired differently.
  477. MODULE_PARM(video_nr, "i"); // video_nr option allows to specify a certain /dev/videoX device (like /dev/video0 or /dev/video1 ...)
  478. MODULE_PARM(radio_nr, "i"); // radio_nr option allows to specify a certain /dev/radioX device (like /dev/radio0 or /dev/radio1 ...)
  479. MODULE_PARM(vbi_nr, "i"); // vbi_nr option allows to specify a certain /dev/vbiX device (like /dev/vbi0 or /dev/vbi1 ...)
  480. MODULE_PARM(CustomDevice, "s"); // .... CustomDevice
  481. #endif
  482. MODULE_PARM_DESC(flags, " Set the default Overlay Display mode of the device driver. Default: 0 (Off)");
  483. MODULE_PARM_DESC(debug, " Set the default Debug Mode of the device driver. Default: 0 (Off)");
  484. MODULE_PARM_DESC(isocMode, " Set the default format for ISOC endpoint. Default: 0x60 (Compression On)");
  485. MODULE_PARM_DESC(adjustCompression, " Set the ADPCM compression for the device. Default: 1 (On)");
  486. MODULE_PARM_DESC(dga, " Set the Direct Graphic Access for the device. Default: 1 (On)");
  487. MODULE_PARM_DESC(PowerOnAtOpen, " Set the default device to power on when device is opened. Default: 1 (On)");
  488. MODULE_PARM_DESC(SwitchSVideoInput, " Set the S-Video input. Some cables and input device are wired differently. Default: 0 (Off)");
  489. MODULE_PARM_DESC(video_nr, "Set video device number (/dev/videoX). Default: -1 (autodetect)");
  490. MODULE_PARM_DESC(radio_nr, "Set radio device number (/dev/radioX). Default: -1 (autodetect)");
  491. MODULE_PARM_DESC(vbi_nr, "Set vbi device number (/dev/vbiX). Default: -1 (autodetect)");
  492. MODULE_PARM_DESC(CustomDevice, " Define the fine tuning parameters for the device. Default: null");
  493. // Misc stuff
  494. MODULE_AUTHOR(DRIVER_AUTHOR);
  495. MODULE_DESCRIPTION(DRIVER_DESC);
  496. MODULE_LICENSE(DRIVER_LICENSE);
  497. MODULE_VERSION(DRIVER_VERSION);
  498. MODULE_ALIAS(DRIVER_ALIAS);
  499. #ifdef MODULE
  500. static unsigned int autoload = 1;
  501. #else
  502. static unsigned int autoload = 0;
  503. #endif
  504. /****************************************************************************************/
  505. /* SYSFS Code - Copied from the stv680.c usb module. */
  506. /* Device information is located at /sys/class/video4linux/video0 */
  507. /* Device parameters information is located at /sys/module/usbvision */
  508. /* Device USB Information is located at /sys/bus/usb/drivers/USBVision Video Grabber */
  509. /****************************************************************************************/
  510. #define YES_NO(x) ((x) ? "Yes" : "No")
  511. static inline struct usb_usbvision *cd_to_usbvision(struct class_device *cd)
  512. {
  513. struct video_device *vdev = to_video_device(cd);
  514. return video_get_drvdata(vdev);
  515. }
  516. static ssize_t show_version(struct class_device *cd, char *buf)
  517. {
  518. return sprintf(buf, "%s\n", DRIVER_VERSION);
  519. }
  520. static CLASS_DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
  521. static ssize_t show_model(struct class_device *class_dev, char *buf)
  522. {
  523. struct video_device *vdev = to_video_device(class_dev);
  524. struct usb_usbvision *usbvision = video_get_drvdata(vdev);
  525. return sprintf(buf, "%s\n", usbvision_device_data[usbvision->DevModel].ModelString);
  526. }
  527. static CLASS_DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
  528. static ssize_t show_hue(struct class_device *class_dev, char *buf)
  529. {
  530. struct video_device *vdev = to_video_device(class_dev);
  531. struct usb_usbvision *usbvision = video_get_drvdata(vdev);
  532. return sprintf(buf, "%d\n", usbvision->hue >> 8);
  533. }
  534. static CLASS_DEVICE_ATTR(hue, S_IRUGO, show_hue, NULL);
  535. static ssize_t show_contrast(struct class_device *class_dev, char *buf)
  536. {
  537. struct video_device *vdev = to_video_device(class_dev);
  538. struct usb_usbvision *usbvision = video_get_drvdata(vdev);
  539. return sprintf(buf, "%d\n", usbvision->contrast >> 8);
  540. }
  541. static CLASS_DEVICE_ATTR(contrast, S_IRUGO, show_contrast, NULL);
  542. static ssize_t show_brightness(struct class_device *class_dev, char *buf)
  543. {
  544. struct video_device *vdev = to_video_device(class_dev);
  545. struct usb_usbvision *usbvision = video_get_drvdata(vdev);
  546. return sprintf(buf, "%d\n", usbvision->brightness >> 8);
  547. }
  548. static CLASS_DEVICE_ATTR(brightness, S_IRUGO, show_brightness, NULL);
  549. static ssize_t show_saturation(struct class_device *class_dev, char *buf)
  550. {
  551. struct video_device *vdev = to_video_device(class_dev);
  552. struct usb_usbvision *usbvision = video_get_drvdata(vdev);
  553. return sprintf(buf, "%d\n", usbvision->saturation >> 8);
  554. }
  555. static CLASS_DEVICE_ATTR(saturation, S_IRUGO, show_saturation, NULL);
  556. static ssize_t show_streaming(struct class_device *class_dev, char *buf)
  557. {
  558. struct video_device *vdev = to_video_device(class_dev);
  559. struct usb_usbvision *usbvision = video_get_drvdata(vdev);
  560. return sprintf(buf, "%s\n", YES_NO(usbvision->streaming));
  561. }
  562. static CLASS_DEVICE_ATTR(streaming, S_IRUGO, show_streaming, NULL);
  563. static ssize_t show_overlay(struct class_device *class_dev, char *buf)
  564. {
  565. struct video_device *vdev = to_video_device(class_dev);
  566. struct usb_usbvision *usbvision = video_get_drvdata(vdev);
  567. return sprintf(buf, "%s\n", YES_NO(usbvision->overlay));
  568. }
  569. static CLASS_DEVICE_ATTR(overlay, S_IRUGO, show_overlay, NULL);
  570. static ssize_t show_compression(struct class_device *class_dev, char *buf)
  571. {
  572. struct video_device *vdev = to_video_device(class_dev);
  573. struct usb_usbvision *usbvision = video_get_drvdata(vdev);
  574. return sprintf(buf, "%s\n", YES_NO(usbvision->isocMode==ISOC_MODE_COMPRESS));
  575. }
  576. static CLASS_DEVICE_ATTR(compression, S_IRUGO, show_compression, NULL);
  577. static ssize_t show_device_bridge(struct class_device *class_dev, char *buf)
  578. {
  579. struct video_device *vdev = to_video_device(class_dev);
  580. struct usb_usbvision *usbvision = video_get_drvdata(vdev);
  581. return sprintf(buf, "%d\n", usbvision->bridgeType);
  582. }
  583. static CLASS_DEVICE_ATTR(bridge, S_IRUGO, show_device_bridge, NULL);
  584. static void usbvision_create_sysfs(struct video_device *vdev)
  585. {
  586. if (vdev) {
  587. video_device_create_file(vdev, &class_device_attr_version);
  588. video_device_create_file(vdev, &class_device_attr_model);
  589. video_device_create_file(vdev, &class_device_attr_hue);
  590. video_device_create_file(vdev, &class_device_attr_contrast);
  591. video_device_create_file(vdev, &class_device_attr_brightness);
  592. video_device_create_file(vdev, &class_device_attr_saturation);
  593. video_device_create_file(vdev, &class_device_attr_streaming);
  594. video_device_create_file(vdev, &class_device_attr_overlay);
  595. video_device_create_file(vdev, &class_device_attr_compression);
  596. video_device_create_file(vdev, &class_device_attr_bridge);
  597. }
  598. }
  599. static void usbvision_remove_sysfs(struct video_device *vdev)
  600. {
  601. if (vdev) {
  602. video_device_remove_file(vdev, &class_device_attr_version);
  603. video_device_remove_file(vdev, &class_device_attr_model);
  604. video_device_remove_file(vdev, &class_device_attr_hue);
  605. video_device_remove_file(vdev, &class_device_attr_contrast);
  606. video_device_remove_file(vdev, &class_device_attr_brightness);
  607. video_device_remove_file(vdev, &class_device_attr_saturation);
  608. video_device_remove_file(vdev, &class_device_attr_streaming);
  609. video_device_remove_file(vdev, &class_device_attr_overlay);
  610. video_device_remove_file(vdev, &class_device_attr_compression);
  611. video_device_remove_file(vdev, &class_device_attr_bridge);
  612. }
  613. }
  614. /*******************************/
  615. /* Memory management functions */
  616. /*******************************/
  617. /*
  618. * Here we want the physical address of the memory.
  619. * This is used when initializing the contents of the area.
  620. */
  621. void *usbvision_rvmalloc(unsigned long size)
  622. {
  623. void *mem;
  624. unsigned long adr;
  625. size = PAGE_ALIGN(size);
  626. mem = vmalloc_32(size);
  627. if (!mem)
  628. return NULL;
  629. memset(mem, 0, size); /* Clear the ram out, no junk to the user */
  630. adr = (unsigned long) mem;
  631. while (size > 0) {
  632. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
  633. mem_map_reserve(vmalloc_to_page((void *)adr));
  634. #else
  635. SetPageReserved(vmalloc_to_page((void *)adr));
  636. #endif
  637. adr += PAGE_SIZE;
  638. size -= PAGE_SIZE;
  639. }
  640. return mem;
  641. }
  642. void usbvision_rvfree(void *mem, unsigned long size)
  643. {
  644. unsigned long adr;
  645. if (!mem)
  646. return;
  647. adr = (unsigned long) mem;
  648. while ((long) size > 0) {
  649. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
  650. mem_map_unreserve(vmalloc_to_page((void *)adr));
  651. #else
  652. ClearPageReserved(vmalloc_to_page((void *)adr));
  653. #endif
  654. adr += PAGE_SIZE;
  655. size -= PAGE_SIZE;
  656. }
  657. vfree(mem);
  658. }
  659. #if ENABLE_HEXDUMP
  660. static void usbvision_hexdump(const unsigned char *data, int len)
  661. {
  662. char tmp[80];
  663. int i, k;
  664. for (i = k = 0; len > 0; i++, len--) {
  665. if (i > 0 && (i % 16 == 0)) {
  666. printk("%s\n", tmp);
  667. k = 0;
  668. }
  669. k += sprintf(&tmp[k], "%02x ", data[i]);
  670. }
  671. if (k > 0)
  672. printk("%s\n", tmp);
  673. }
  674. #endif
  675. /* These procedures handle the scratch ring buffer */
  676. int scratch_len(struct usb_usbvision *usbvision) /*This returns the amount of data actually in the buffer */
  677. {
  678. int len = usbvision->scratch_write_ptr - usbvision->scratch_read_ptr;
  679. if (len < 0) {
  680. len += scratch_buf_size;
  681. }
  682. PDEBUG(DBG_SCRATCH, "scratch_len() = %d\n", len);
  683. return len;
  684. }
  685. /* This returns the free space left in the buffer */
  686. int scratch_free(struct usb_usbvision *usbvision)
  687. {
  688. int free = usbvision->scratch_read_ptr - usbvision->scratch_write_ptr;
  689. if (free <= 0) {
  690. free += scratch_buf_size;
  691. }
  692. if (free) {
  693. free -= 1; /* at least one byte in the buffer must */
  694. /* left blank, otherwise there is no chance to differ between full and empty */
  695. }
  696. PDEBUG(DBG_SCRATCH, "return %d\n", free);
  697. return free;
  698. }
  699. void *debug_memcpy(void *dest, void *src, size_t len)
  700. {
  701. printk(KERN_DEBUG "memcpy(%p, %p, %d);\n", dest, src, len);
  702. return memcpy(dest, src, len);
  703. }
  704. /* This puts data into the buffer */
  705. int scratch_put(struct usb_usbvision *usbvision, unsigned char *data, int len)
  706. {
  707. int len_part;
  708. if (usbvision->scratch_write_ptr + len < scratch_buf_size) {
  709. memcpy(usbvision->scratch + usbvision->scratch_write_ptr, data, len);
  710. usbvision->scratch_write_ptr += len;
  711. }
  712. else {
  713. len_part = scratch_buf_size - usbvision->scratch_write_ptr;
  714. memcpy(usbvision->scratch + usbvision->scratch_write_ptr, data, len_part);
  715. if (len == len_part) {
  716. usbvision->scratch_write_ptr = 0; /* just set write_ptr to zero */
  717. }
  718. else {
  719. memcpy(usbvision->scratch, data + len_part, len - len_part);
  720. usbvision->scratch_write_ptr = len - len_part;
  721. }
  722. }
  723. PDEBUG(DBG_SCRATCH, "len=%d, new write_ptr=%d\n", len, usbvision->scratch_write_ptr);
  724. return len;
  725. }
  726. /* This marks the write_ptr as position of new frame header */
  727. void scratch_mark_header(struct usb_usbvision *usbvision)
  728. {
  729. PDEBUG(DBG_SCRATCH, "header at write_ptr=%d\n", usbvision->scratch_headermarker_write_ptr);
  730. usbvision->scratch_headermarker[usbvision->scratch_headermarker_write_ptr] =
  731. usbvision->scratch_write_ptr;
  732. usbvision->scratch_headermarker_write_ptr += 1;
  733. usbvision->scratch_headermarker_write_ptr %= USBVISION_NUM_HEADERMARKER;
  734. }
  735. /* This gets data from the buffer at the given "ptr" position */
  736. int scratch_get_extra(struct usb_usbvision *usbvision, unsigned char *data, int *ptr, int len)
  737. {
  738. int len_part;
  739. if (*ptr + len < scratch_buf_size) {
  740. memcpy(data, usbvision->scratch + *ptr, len);
  741. *ptr += len;
  742. }
  743. else {
  744. len_part = scratch_buf_size - *ptr;
  745. memcpy(data, usbvision->scratch + *ptr, len_part);
  746. if (len == len_part) {
  747. *ptr = 0; /* just set the y_ptr to zero */
  748. }
  749. else {
  750. memcpy(data + len_part, usbvision->scratch, len - len_part);
  751. *ptr = len - len_part;
  752. }
  753. }
  754. PDEBUG(DBG_SCRATCH, "len=%d, new ptr=%d\n", len, *ptr);
  755. return len;
  756. }
  757. /* This sets the scratch extra read pointer */
  758. void scratch_set_extra_ptr(struct usb_usbvision *usbvision, int *ptr, int len)
  759. {
  760. *ptr = (usbvision->scratch_read_ptr + len)%scratch_buf_size;
  761. PDEBUG(DBG_SCRATCH, "ptr=%d\n", *ptr);
  762. }
  763. /*This increments the scratch extra read pointer */
  764. void scratch_inc_extra_ptr(int *ptr, int len)
  765. {
  766. *ptr = (*ptr + len) % scratch_buf_size;
  767. PDEBUG(DBG_SCRATCH, "ptr=%d\n", *ptr);
  768. }
  769. /* This gets data from the buffer */
  770. int scratch_get(struct usb_usbvision *usbvision, unsigned char *data, int len)
  771. {
  772. int len_part;
  773. if (usbvision->scratch_read_ptr + len < scratch_buf_size) {
  774. memcpy(data, usbvision->scratch + usbvision->scratch_read_ptr, len);
  775. usbvision->scratch_read_ptr += len;
  776. }
  777. else {
  778. len_part = scratch_buf_size - usbvision->scratch_read_ptr;
  779. memcpy(data, usbvision->scratch + usbvision->scratch_read_ptr, len_part);
  780. if (len == len_part) {
  781. usbvision->scratch_read_ptr = 0; /* just set the read_ptr to zero */
  782. }
  783. else {
  784. memcpy(data + len_part, usbvision->scratch, len - len_part);
  785. usbvision->scratch_read_ptr = len - len_part;
  786. }
  787. }
  788. PDEBUG(DBG_SCRATCH, "len=%d, new read_ptr=%d\n", len, usbvision->scratch_read_ptr);
  789. return len;
  790. }
  791. /* This sets read pointer to next header and returns it */
  792. int scratch_get_header(struct usb_usbvision *usbvision,struct usbvision_frame_header *header)
  793. {
  794. int errCode = 0;
  795. PDEBUG(DBG_SCRATCH, "from read_ptr=%d", usbvision->scratch_headermarker_read_ptr);
  796. while (usbvision->scratch_headermarker_write_ptr -
  797. usbvision->scratch_headermarker_read_ptr != 0) {
  798. usbvision->scratch_read_ptr =
  799. usbvision->scratch_headermarker[usbvision->scratch_headermarker_read_ptr];
  800. usbvision->scratch_headermarker_read_ptr += 1;
  801. usbvision->scratch_headermarker_read_ptr %= USBVISION_NUM_HEADERMARKER;
  802. scratch_get(usbvision, (unsigned char *)header, USBVISION_HEADER_LENGTH);
  803. if ((header->magic_1 == USBVISION_MAGIC_1)
  804. && (header->magic_2 == USBVISION_MAGIC_2)
  805. && (header->headerLength == USBVISION_HEADER_LENGTH)) {
  806. errCode = USBVISION_HEADER_LENGTH;
  807. header->frameWidth = header->frameWidthLo + (header->frameWidthHi << 8);
  808. header->frameHeight = header->frameHeightLo + (header->frameHeightHi << 8);
  809. break;
  810. }
  811. }
  812. return errCode;
  813. }
  814. /*This removes len bytes of old data from the buffer */
  815. void scratch_rm_old(struct usb_usbvision *usbvision, int len)
  816. {
  817. usbvision->scratch_read_ptr += len;
  818. usbvision->scratch_read_ptr %= scratch_buf_size;
  819. PDEBUG(DBG_SCRATCH, "read_ptr is now %d\n", usbvision->scratch_read_ptr);
  820. }
  821. /*This resets the buffer - kills all data in it too */
  822. void scratch_reset(struct usb_usbvision *usbvision)
  823. {
  824. PDEBUG(DBG_SCRATCH, "\n");
  825. usbvision->scratch_read_ptr = 0;
  826. usbvision->scratch_write_ptr = 0;
  827. usbvision->scratch_headermarker_read_ptr = 0;
  828. usbvision->scratch_headermarker_write_ptr = 0;
  829. usbvision->isocstate = IsocState_NoFrame;
  830. }
  831. /* Here comes the OVERLAY stuff */
  832. /* Tell the interrupt handler what to to. */
  833. static
  834. void usbvision_cap(struct usb_usbvision* usbvision, int on)
  835. {
  836. DEBUG(printk(KERN_DEBUG "usbvision_cap: overlay was %d, set it to %d\n", usbvision->overlay, on);)
  837. if (on) {
  838. usbvision->overlay = 1;
  839. }
  840. else {
  841. usbvision->overlay = 0;
  842. }
  843. }
  844. /* append a new clipregion to the vector of video_clips */
  845. static
  846. void usbvision_new_clip(struct v4l2_format* vf, struct v4l2_clip* vcp, int x, int y, int w, int h)
  847. {
  848. vcp[vf->fmt.win.clipcount].c.left = x;
  849. vcp[vf->fmt.win.clipcount].c.top = y;
  850. vcp[vf->fmt.win.clipcount].c.width = w;
  851. vcp[vf->fmt.win.clipcount].c.height = h;
  852. vf->fmt.win.clipcount++;
  853. }
  854. #define mark_pixel(x,y) usbvision->clipmask[((x) + (y) * MAX_FRAME_WIDTH)/32] |= 0x00000001<<((x)%32)
  855. #define clipped_pixel(index) usbvision->clipmask[(index)/32] & (0x00000001<<((index)%32))
  856. static
  857. void usbvision_built_overlay(struct usb_usbvision* usbvision, int count, struct v4l2_clip *vcp)
  858. {
  859. usbvision->overlay_win = usbvision->overlay_base +
  860. (signed int)usbvision->vid_win.fmt.win.w.left * usbvision->depth / 8 +
  861. (signed int)usbvision->vid_win.fmt.win.w.top * usbvision->vid_buf.fmt.bytesperline;
  862. IODEBUG(printk(KERN_DEBUG "built_overlay base=%p, win=%p, bpl=%d, clips=%d, size=%dx%d\n",
  863. usbvision->overlay_base, usbvision->overlay_win,
  864. usbvision->vid_buf.fmt.bytesperline, count,
  865. usbvision->vid_win.fmt.win.w.width, usbvision->vid_win.fmt.win.w.height);)
  866. /* Add here generation of clipping mask */
  867. {
  868. int x_start, x_end, y_start, y_end;
  869. int clip_index, x, y;
  870. memset(usbvision->clipmask, 0, USBVISION_CLIPMASK_SIZE);
  871. OVDEBUG(printk(KERN_DEBUG "clips = %d\n", count);)
  872. for(clip_index = 0; clip_index < count; clip_index++) {
  873. OVDEBUG(printk(KERN_DEBUG "clip: %d,%d,%d,%d\n", vcp[clip_index].x,
  874. vcp[clip_index].y,
  875. vcp[clip_index].width,
  876. vcp[clip_index].height);)
  877. x_start = vcp[clip_index].c.left;
  878. if(x_start >= (int)usbvision->vid_win.fmt.win.w.width) {
  879. OVDEBUG(printk(KERN_DEBUG "x_start=%d\n", x_start);)
  880. continue; //clipping window is right of overlay window
  881. }
  882. x_end = x_start + vcp[clip_index].c.width;
  883. if(x_end <= 0) {
  884. OVDEBUG(printk(KERN_DEBUG "x_end=%d\n", x_end);)
  885. continue; //clipping window is left of overlay window
  886. }
  887. y_start = vcp[clip_index].c.top;
  888. if(y_start >= (int)usbvision->vid_win.fmt.win.w.height) {
  889. OVDEBUG(printk(KERN_DEBUG "y_start=%d\n", y_start);)
  890. continue; //clipping window is below overlay window
  891. }
  892. y_end = y_start + vcp[clip_index].c.height;
  893. if(y_end <= 0) {
  894. OVDEBUG(printk(KERN_DEBUG "y_end=%d\n", y_end);)
  895. continue; //clipping window is above overlay window
  896. }
  897. //clip the clipping window
  898. if (x_start < 0) {
  899. x_start = 0;
  900. }
  901. if (x_end > (int)usbvision->vid_win.fmt.win.w.width) {
  902. x_end = (int)usbvision->vid_win.fmt.win.w.width;
  903. }
  904. if (y_start < 0) {
  905. y_start = 0;
  906. }
  907. if (y_end > (int)usbvision->vid_win.fmt.win.w.height) {
  908. y_end = (int)usbvision->vid_win.fmt.win.w.height;
  909. }
  910. OVDEBUG(printk(KERN_DEBUG "clip_o: %d,%d,%d,%d\n", x_start, y_start, x_end, y_end);)
  911. for(y = y_start; y < y_end; y++) {
  912. for(x = x_start; x < x_end; x++) {
  913. mark_pixel(x,y);
  914. }
  915. }
  916. }
  917. }
  918. }
  919. void usbvision_osd_char(struct usb_usbvision *usbvision,
  920. struct usbvision_frame *frame, int x, int y, int ch)
  921. {
  922. static const unsigned short digits[16] = {
  923. 0xF6DE, /* 0 */
  924. 0x2492, /* 1 */
  925. 0xE7CE, /* 2 */
  926. 0xE79E, /* 3 */
  927. 0xB792, /* 4 */
  928. 0xF39E, /* 5 */
  929. 0xF3DE, /* 6 */
  930. 0xF492, /* 7 */
  931. 0xF7DE, /* 8 */
  932. 0xF79E, /* 9 */
  933. 0x77DA, /* a */
  934. 0xD75C, /* b */
  935. 0xF24E, /* c */
  936. 0xD6DC, /* d */
  937. 0xF34E, /* e */
  938. 0xF348 /* f */
  939. };
  940. unsigned short digit;
  941. int ix, iy;
  942. if ((usbvision == NULL) || (frame == NULL))
  943. return;
  944. if (ch >= '0' && ch <= '9')
  945. ch -= '0';
  946. else if (ch >= 'A' && ch <= 'F')
  947. ch = 10 + (ch - 'A');
  948. else if (ch >= 'a' && ch <= 'f')
  949. ch = 10 + (ch - 'a');
  950. else
  951. return;
  952. digit = digits[ch];
  953. for (iy = 0; iy < 5; iy++) {
  954. for (ix = 0; ix < 3; ix++) {
  955. if (digit & 0x8000) {
  956. // USBVISION_PUTPIXEL(frame, x + ix, y + iy,
  957. // 0xFF, 0xFF, 0xFF);
  958. }
  959. digit = digit << 1;
  960. }
  961. }
  962. }
  963. void usbvision_osd_string(struct usb_usbvision *usbvision,
  964. struct usbvision_frame *frame,
  965. int x, int y, const char *str)
  966. {
  967. while (*str) {
  968. usbvision_osd_char(usbvision, frame, x, y, *str);
  969. str++;
  970. x += 4; /* 3 pixels character + 1 space */
  971. }
  972. }
  973. /*
  974. * usb_usbvision_osd_stats()
  975. *
  976. * On screen display of important debugging information.
  977. *
  978. */
  979. void usbvision_osd_stats(struct usb_usbvision *usbvision,
  980. struct usbvision_frame *frame)
  981. {
  982. const int y_diff = 8;
  983. char tmp[16];
  984. int x = 10;
  985. int y = 10;
  986. sprintf(tmp, "%8x", usbvision->frame_num);
  987. usbvision_osd_string(usbvision, frame, x, y, tmp);
  988. y += y_diff;
  989. sprintf(tmp, "%8lx", usbvision->isocUrbCount);
  990. usbvision_osd_string(usbvision, frame, x, y, tmp);
  991. y += y_diff;
  992. sprintf(tmp, "%8lx", usbvision->urb_length);
  993. usbvision_osd_string(usbvision, frame, x, y, tmp);
  994. y += y_diff;
  995. sprintf(tmp, "%8lx", usbvision->isocDataCount);
  996. usbvision_osd_string(usbvision, frame, x, y, tmp);
  997. y += y_diff;
  998. sprintf(tmp, "%8lx", usbvision->header_count);
  999. usbvision_osd_string(usbvision, frame, x, y, tmp);
  1000. y += y_diff;
  1001. sprintf(tmp, "%8lx", usbvision->scratch_ovf_count);
  1002. usbvision_osd_string(usbvision, frame, x, y, tmp);
  1003. y += y_diff;
  1004. sprintf(tmp, "%8lx", usbvision->isocSkipCount);
  1005. usbvision_osd_string(usbvision, frame, x, y, tmp);
  1006. y += y_diff;
  1007. sprintf(tmp, "%8lx", usbvision->isocErrCount);
  1008. usbvision_osd_string(usbvision, frame, x, y, tmp);
  1009. y += y_diff;
  1010. sprintf(tmp, "%8x", usbvision->saturation);
  1011. usbvision_osd_string(usbvision, frame, x, y, tmp);
  1012. y += y_diff;
  1013. sprintf(tmp, "%8x", usbvision->hue);
  1014. usbvision_osd_string(usbvision, frame, x, y, tmp);
  1015. y += y_diff;
  1016. sprintf(tmp, "%8x", usbvision->brightness >> 8);
  1017. usbvision_osd_string(usbvision, frame, x, y, tmp);
  1018. y += y_diff;
  1019. sprintf(tmp, "%8x", usbvision->contrast >> 12);
  1020. usbvision_osd_string(usbvision, frame, x, y, tmp);
  1021. y += y_diff;
  1022. }
  1023. /*
  1024. * usbvision_testpattern()
  1025. *
  1026. * Procedure forms a test pattern (yellow grid on blue background).
  1027. *
  1028. * Parameters:
  1029. * fullframe: if TRUE then entire frame is filled, otherwise the procedure
  1030. * continues from the current scanline.
  1031. * pmode 0: fill the frame with solid blue color (like on VCR or TV)
  1032. * 1: Draw a colored grid
  1033. *
  1034. */
  1035. void usbvision_testpattern(struct usb_usbvision *usbvision, int fullframe,
  1036. int pmode)
  1037. {
  1038. static const char proc[] = "usbvision_testpattern";
  1039. struct usbvision_frame *frame;
  1040. unsigned char *f;
  1041. int num_cell = 0;
  1042. int scan_length = 0;
  1043. static int num_pass = 0;
  1044. if (usbvision == NULL) {
  1045. printk(KERN_ERR "%s: usbvision == NULL\n", proc);
  1046. return;
  1047. }
  1048. if ((usbvision->curFrameNum < 0)
  1049. || (usbvision->curFrameNum >= USBVISION_NUMFRAMES)) {
  1050. printk(KERN_ERR "%s: usbvision->curFrameNum=%d.\n", proc,
  1051. usbvision->curFrameNum);
  1052. return;
  1053. }
  1054. /* Grab the current frame */
  1055. frame = &usbvision->frame[usbvision->curFrameNum];
  1056. /* Optionally start at the beginning */
  1057. if (fullframe) {
  1058. frame->curline = 0;
  1059. frame->scanlength = 0;
  1060. }
  1061. /* Form every scan line */
  1062. for (; frame->curline < frame->frmheight; frame->curline++) {
  1063. int i;
  1064. f = frame->data + (usbvision->curwidth * 3 * frame->curline);
  1065. for (i = 0; i < usbvision->curwidth; i++) {
  1066. unsigned char cb = 0x80;
  1067. unsigned char cg = 0;
  1068. unsigned char cr = 0;
  1069. if (pmode == 1) {
  1070. if (frame->curline % 32 == 0)
  1071. cb = 0, cg = cr = 0xFF;
  1072. else if (i % 32 == 0) {
  1073. if (frame->curline % 32 == 1)
  1074. num_cell++;
  1075. cb = 0, cg = cr = 0xFF;
  1076. } else {
  1077. cb =
  1078. ((num_cell * 7) +
  1079. num_pass) & 0xFF;
  1080. cg =
  1081. ((num_cell * 5) +
  1082. num_pass * 2) & 0xFF;
  1083. cr =
  1084. ((num_cell * 3) +
  1085. num_pass * 3) & 0xFF;
  1086. }
  1087. } else {
  1088. /* Just the blue screen */
  1089. }
  1090. *f++ = cb;
  1091. *f++ = cg;
  1092. *f++ = cr;
  1093. scan_length += 3;
  1094. }
  1095. }
  1096. frame->grabstate = FrameState_Done;
  1097. frame->scanlength += scan_length;
  1098. ++num_pass;
  1099. /* We do this unconditionally, regardless of FLAGS_OSD_STATS */
  1100. usbvision_osd_stats(usbvision, frame);
  1101. }
  1102. /*
  1103. * Here comes the data parsing stuff that is run as interrupt
  1104. */
  1105. /*
  1106. * usbvision_find_header()
  1107. *
  1108. * Locate one of supported header markers in the scratch buffer.
  1109. */
  1110. static enum ParseState usbvision_find_header(struct usb_usbvision *usbvision)
  1111. {
  1112. struct usbvision_frame *frame;
  1113. int foundHeader = 0;
  1114. if (usbvision->overlay) {
  1115. frame = &usbvision->overlay_frame;
  1116. }
  1117. else {
  1118. frame = &usbvision->frame[usbvision->curFrameNum];
  1119. }
  1120. while (scratch_get_header(usbvision, &frame->isocHeader) == USBVISION_HEADER_LENGTH) {
  1121. // found header in scratch
  1122. PDEBUG(DBG_HEADER, "found header: 0x%02x%02x %d %d %d %d %#x 0x%02x %u %u",
  1123. frame->isocHeader.magic_2,
  1124. frame->isocHeader.magic_1,
  1125. frame->isocHeader.headerLength,
  1126. frame->isocHeader.frameNum,
  1127. frame->isocHeader.framePhase,
  1128. frame->isocHeader.frameLatency,
  1129. frame->isocHeader.dataFormat,
  1130. frame->isocHeader.formatParam,
  1131. frame->isocHeader.frameWidth,
  1132. frame->isocHeader.frameHeight);
  1133. if (usbvision->requestIntra) {
  1134. if (frame->isocHeader.formatParam & 0x80) {
  1135. foundHeader = 1;
  1136. usbvision->lastIsocFrameNum = -1; // do not check for lost frames this time
  1137. usbvision_unrequest_intra(usbvision);
  1138. break;
  1139. }
  1140. }
  1141. else {
  1142. foundHeader = 1;
  1143. break;
  1144. }
  1145. }
  1146. if (foundHeader) {
  1147. frame->frmwidth = frame->isocHeader.frameWidth * usbvision->stretch_width;
  1148. frame->frmheight = frame->isocHeader.frameHeight * usbvision->stretch_height;
  1149. frame->v4l2_linesize = (frame->frmwidth * frame->v4l2_format.depth)>> 3;
  1150. usbvision->curFrame = frame;
  1151. }
  1152. else { // no header found
  1153. PDEBUG(DBG_HEADER, "skipping scratch data, no header");
  1154. scratch_reset(usbvision);
  1155. return ParseState_EndParse;
  1156. }
  1157. // found header
  1158. if (frame->isocHeader.dataFormat==ISOC_MODE_COMPRESS) {
  1159. //check isocHeader.frameNum for lost frames
  1160. if (usbvision->lastIsocFrameNum >= 0) {
  1161. if (((usbvision->lastIsocFrameNum + 1) % 32) != frame->isocHeader.frameNum) {
  1162. // unexpected frame drop: need to request new intra frame
  1163. PDEBUG(DBG_HEADER, "Lost frame before %d on USB", frame->isocHeader.frameNum);
  1164. usbvision_request_intra(usbvision);
  1165. return ParseState_NextFrame;
  1166. }
  1167. }
  1168. usbvision->lastIsocFrameNum = frame->isocHeader.frameNum;
  1169. }
  1170. usbvision->header_count++;
  1171. frame->scanstate = ScanState_Lines;
  1172. frame->curline = 0;
  1173. if (flags & FLAGS_FORCE_TESTPATTERN) {
  1174. usbvision_testpattern(usbvision, 1, 1);
  1175. return ParseState_NextFrame;
  1176. }
  1177. return ParseState_Continue;
  1178. }
  1179. static enum ParseState usbvision_parse_lines_422(struct usb_usbvision *usbvision,
  1180. long *pcopylen)
  1181. {
  1182. volatile struct usbvision_frame *frame;
  1183. unsigned char *f;
  1184. int len;
  1185. int i;
  1186. unsigned char yuyv[4]={180, 128, 10, 128}; // YUV components
  1187. unsigned char rv, gv, bv; // RGB components
  1188. int clipmask_index, bytes_per_pixel;
  1189. int overlay = usbvision->overlay;
  1190. int stretch_bytes, clipmask_add;
  1191. if (overlay) {
  1192. frame = &usbvision->overlay_frame;
  1193. if (usbvision->overlay_base == NULL) {
  1194. //video_buffer is not set yet
  1195. return ParseState_NextFrame;
  1196. }
  1197. f = usbvision->overlay_win + frame->curline *
  1198. usbvision->vid_buf.fmt.bytesperline;
  1199. }
  1200. else {
  1201. frame = &usbvision->frame[usbvision->curFrameNum];
  1202. f = frame->data + (frame->v4l2_linesize * frame->curline);
  1203. }
  1204. /* Make sure there's enough data for the entire line */
  1205. len = (frame->isocHeader.frameWidth * 2)+5;
  1206. if (scratch_len(usbvision) < len) {
  1207. PDEBUG(DBG_PARSE, "out of data in line %d, need %u.\n", frame->curline, len);
  1208. return ParseState_Out;
  1209. }
  1210. if ((frame->curline + 1) >= frame->frmheight) {
  1211. return ParseState_NextFrame;
  1212. }
  1213. bytes_per_pixel = frame->v4l2_format.bytes_per_pixel;
  1214. stretch_bytes = (usbvision->stretch_width - 1) * bytes_per_pixel;
  1215. clipmask_index = frame->curline * MAX_FRAME_WIDTH;
  1216. clipmask_add = usbvision->stretch_width;
  1217. for (i = 0; i < frame->frmwidth; i+=(2 * usbvision->stretch_width)) {
  1218. scratch_get(usbvision, &yuyv[0], 4);
  1219. if((overlay) && (clipped_pixel(clipmask_index))) {
  1220. f += bytes_per_pixel;
  1221. }
  1222. else if (frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
  1223. *f++ = yuyv[0]; // Y
  1224. *f++ = yuyv[3]; // U
  1225. }
  1226. else {
  1227. YUV_TO_RGB_BY_THE_BOOK(yuyv[0], yuyv[1], yuyv[3], rv, gv, bv);
  1228. switch (frame->v4l2_format.format) {
  1229. case V4L2_PIX_FMT_RGB565:
  1230. *f++ = (0x1F & (bv >> 3)) | (0xE0 & (gv << 3));
  1231. *f++ = (0x07 & (gv >> 5)) | (0xF8 & rv);
  1232. break;
  1233. case V4L2_PIX_FMT_RGB24:
  1234. *f++ = bv;
  1235. *f++ = gv;
  1236. *f++ = rv;
  1237. break;
  1238. case V4L2_PIX_FMT_RGB32:
  1239. *f++ = bv;
  1240. *f++ = gv;
  1241. *f++ = rv;
  1242. f++;
  1243. break;
  1244. case V4L2_PIX_FMT_RGB555:
  1245. *f++ = (0x1F & (bv >> 3)) | (0xE0 & (gv << 2));
  1246. *f++ = (0x03 & (gv >> 6)) | (0x7C & (rv >> 1));
  1247. break;
  1248. }
  1249. }
  1250. clipmask_index += clipmask_add;
  1251. f += stretch_bytes;
  1252. if((overlay) && (clipped_pixel(clipmask_index))) {
  1253. f += bytes_per_pixel;
  1254. }
  1255. else if (frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
  1256. *f++ = yuyv[2]; // Y
  1257. *f++ = yuyv[1]; // V
  1258. }
  1259. else {
  1260. YUV_TO_RGB_BY_THE_BOOK(yuyv[2], yuyv[1], yuyv[3], rv, gv, bv);
  1261. switch (frame->v4l2_format.format) {
  1262. case V4L2_PIX_FMT_RGB565:
  1263. *f++ = (0x1F & (bv >> 3)) | (0xE0 & (gv << 3));
  1264. *f++ = (0x07 & (gv >> 5)) | (0xF8 & rv);
  1265. break;
  1266. case V4L2_PIX_FMT_RGB24:
  1267. *f++ = bv;
  1268. *f++ = gv;
  1269. *f++ = rv;
  1270. break;
  1271. case V4L2_PIX_FMT_RGB32:
  1272. *f++ = bv;
  1273. *f++ = gv;
  1274. *f++ = rv;
  1275. f++;
  1276. break;
  1277. case V4L2_PIX_FMT_RGB555:
  1278. *f++ = (0x1F & (bv >> 3)) | (0xE0 & (gv << 2));
  1279. *f++ = (0x03 & (gv >> 6)) | (0x7C & (rv >> 1));
  1280. break;
  1281. }
  1282. }
  1283. clipmask_index += clipmask_add;
  1284. f += stretch_bytes;
  1285. }
  1286. frame->curline += usbvision->stretch_height;
  1287. *pcopylen += frame->v4l2_linesize * usbvision->stretch_height;
  1288. if (frame->curline >= frame->frmheight) {
  1289. return ParseState_NextFrame;
  1290. }
  1291. else {
  1292. return ParseState_Continue;
  1293. }
  1294. }
  1295. static int usbvision_decompress(struct usb_usbvision *usbvision,unsigned char *Compressed,
  1296. unsigned char *Decompressed, int *StartPos,
  1297. int *BlockTypeStartPos, int Len)
  1298. {
  1299. int RestPixel, Idx, MaxPos, Pos, ExtraPos, BlockLen, BlockTypePos, BlockTypeLen;
  1300. unsigned char BlockByte, BlockCode, BlockType, BlockTypeByte, Integrator;
  1301. Integrator = 0;
  1302. Pos = *StartPos;
  1303. BlockTypePos = *BlockTypeStartPos;
  1304. MaxPos = 396; //Pos + Len;
  1305. ExtraPos = Pos;
  1306. BlockLen = 0;
  1307. BlockByte = 0;
  1308. BlockCode = 0;
  1309. BlockType = 0;
  1310. BlockTypeByte = 0;
  1311. BlockTypeLen = 0;
  1312. RestPixel = Len;
  1313. for (Idx = 0; Idx < Len; Idx++) {
  1314. if (BlockLen == 0) {
  1315. if (BlockTypeLen==0) {
  1316. BlockTypeByte = Compressed[BlockTypePos];
  1317. BlockTypePos++;
  1318. BlockTypeLen = 4;
  1319. }
  1320. BlockType = (BlockTypeByte & 0xC0) >> 6;
  1321. //statistic:
  1322. usbvision->ComprBlockTypes[BlockType]++;
  1323. Pos = ExtraPos;
  1324. if (BlockType == 0) {
  1325. if(RestPixel >= 24) {
  1326. Idx += 23;
  1327. RestPixel -= 24;
  1328. Integrator = Decompressed[Idx];
  1329. } else {
  1330. Idx += RestPixel - 1;
  1331. RestPixel = 0;
  1332. }
  1333. } else {
  1334. BlockCode = Compressed[Pos];
  1335. Pos++;
  1336. if (RestPixel >= 24) {
  1337. BlockLen = 24;
  1338. } else {
  1339. BlockLen = RestPixel;
  1340. }
  1341. RestPixel -= BlockLen;
  1342. ExtraPos = Pos + (BlockLen / 4);
  1343. }
  1344. BlockTypeByte <<= 2;
  1345. BlockTypeLen -= 1;
  1346. }
  1347. if (BlockLen > 0) {
  1348. if ((BlockLen%4) == 0) {
  1349. BlockByte = Compressed[Pos];
  1350. Pos++;
  1351. }
  1352. if (BlockType == 1) { //inter Block
  1353. Integrator = Decompressed[Idx];
  1354. }
  1355. switch (BlockByte & 0xC0) {
  1356. case 0x03<<6:
  1357. Integrator += Compressed[ExtraPos];
  1358. ExtraPos++;
  1359. break;
  1360. case 0x02<<6:
  1361. Integrator += BlockCode;
  1362. break;
  1363. case 0x00:
  1364. Integrator -= BlockCode;
  1365. break;
  1366. }
  1367. Decompressed[Idx] = Integrator;
  1368. BlockByte <<= 2;
  1369. BlockLen -= 1;
  1370. }
  1371. }
  1372. *StartPos = ExtraPos;
  1373. *BlockTypeStartPos = BlockTypePos;
  1374. return Idx;
  1375. }
  1376. /*
  1377. * usbvision_parse_compress()
  1378. *
  1379. * Parse compressed frame from the scratch buffer, put
  1380. * decoded RGB value into the current frame buffer and add the written
  1381. * number of bytes (RGB) to the *pcopylen.
  1382. *
  1383. */
  1384. static enum ParseState usbvision_parse_compress(struct usb_usbvision *usbvision,
  1385. long *pcopylen)
  1386. {
  1387. #define USBVISION_STRIP_MAGIC 0x5A
  1388. #define USBVISION_STRIP_LEN_MAX 400
  1389. #define USBVISION_STRIP_HEADER_LEN 3
  1390. struct usbvision_frame *frame;
  1391. unsigned char *f,*u = NULL ,*v = NULL;
  1392. unsigned char StripData[USBVISION_STRIP_LEN_MAX];
  1393. unsigned char StripHeader[USBVISION_STRIP_HEADER_LEN];
  1394. int Idx, IdxEnd, StripLen, StripPtr, StartBlockPos, BlockPos, BlockTypePos;
  1395. int clipmask_index, bytes_per_pixel, rc;
  1396. int overlay = usbvision->overlay;
  1397. int imageSize;
  1398. unsigned char rv, gv, bv;
  1399. static unsigned char *Y, *U, *V;
  1400. if (overlay) {
  1401. frame = &usbvision->overlay_frame;
  1402. imageSize = frame->frmwidth * frame->frmheight;
  1403. if (usbvision->overlay_base == NULL) {
  1404. //video_buffer is not set yet
  1405. return ParseState_NextFrame;
  1406. }
  1407. f = usbvision->overlay_win + frame->curline *
  1408. usbvision->vid_buf.fmt.bytesperline;
  1409. }
  1410. else {
  1411. frame = &usbvision->frame[usbvision->curFrameNum];
  1412. imageSize = frame->frmwidth * frame->frmheight;
  1413. if ( (frame->v4l2_format.format == V4L2_PIX_FMT_YUV422P) ||
  1414. (frame->v4l2_format.format == V4L2_PIX_FMT_YVU420) )
  1415. { // this is a planar format
  1416. //... v4l2_linesize not used here.
  1417. f = frame->data + (frame->width * frame->curline);
  1418. } else
  1419. f = frame->data + (frame->v4l2_linesize * frame->curline);
  1420. if (frame->v4l2_format.format == V4L2_PIX_FMT_YUYV){ //initialise u and v pointers
  1421. // get base of u and b planes add halfoffset
  1422. u = frame->data
  1423. + imageSize
  1424. + (frame->frmwidth >>1) * frame->curline ;
  1425. v = u + (imageSize >>1 );
  1426. } else if (frame->v4l2_format.format == V4L2_PIX_FMT_YVU420){
  1427. v = frame->data + imageSize + ((frame->curline* (frame->width))>>2) ;
  1428. u = v + (imageSize >>2) ;
  1429. }
  1430. }
  1431. if (frame->curline == 0) {
  1432. usbvision_adjust_compression(usbvision);
  1433. }
  1434. if (scratch_len(usbvision) < USBVISION_STRIP_HEADER_LEN) {
  1435. return ParseState_Out;
  1436. }
  1437. //get strip header without changing the scratch_read_ptr
  1438. scratch_set_extra_ptr(usbvision, &StripPtr, 0);
  1439. scratch_get_extra(usbvision, &StripHeader[0], &StripPtr,
  1440. USBVISION_STRIP_HEADER_LEN);
  1441. if (StripHeader[0] != USBVISION_STRIP_MAGIC) {
  1442. // wrong strip magic
  1443. usbvision->stripMagicErrors++;
  1444. return ParseState_NextFrame;
  1445. }
  1446. if (frame->curline != (int)StripHeader[2]) {
  1447. //line number missmatch error
  1448. usbvision->stripLineNumberErrors++;
  1449. }
  1450. StripLen = 2 * (unsigned int)StripHeader[1];
  1451. if (StripLen > USBVISION_STRIP_LEN_MAX) {
  1452. // strip overrun
  1453. // I think this never happens
  1454. usbvision_request_intra(usbvision);
  1455. }
  1456. if (scratch_len(usbvision) < StripLen) {
  1457. //there is not enough data for the strip
  1458. return ParseState_Out;
  1459. }
  1460. if (usbvision->IntraFrameBuffer) {
  1461. Y = usbvision->IntraFrameBuffer + frame->frmwidth * frame->curline;
  1462. U = usbvision->IntraFrameBuffer + imageSize + (frame->frmwidth / 2) * (frame->curline / 2);
  1463. V = usbvision->IntraFrameBuffer + imageSize / 4 * 5 + (frame->frmwidth / 2) * (frame->curline / 2);
  1464. }
  1465. else {
  1466. return ParseState_NextFrame;
  1467. }
  1468. bytes_per_pixel = frame->v4l2_format.bytes_per_pixel;
  1469. clipmask_index = frame->curline * MAX_FRAME_WIDTH;
  1470. scratch_get(usbvision, StripData, StripLen);
  1471. IdxEnd = frame->frmwidth;
  1472. BlockTypePos = USBVISION_STRIP_HEADER_LEN;
  1473. StartBlockPos = BlockTypePos + (IdxEnd - 1) / 96 + (IdxEnd / 2 - 1) / 96 + 2;
  1474. BlockPos = StartBlockPos;
  1475. usbvision->BlockPos = BlockPos;
  1476. if ((rc = usbvision_decompress(usbvision, StripData, Y, &BlockPos, &BlockTypePos, IdxEnd)) != IdxEnd) {
  1477. //return ParseState_Continue;
  1478. }
  1479. if (StripLen > usbvision->maxStripLen) {
  1480. usbvision->maxStripLen = StripLen;
  1481. }
  1482. if (frame->curline%2) {
  1483. if ((rc = usbvision_decompress(usbvision, StripData, V, &BlockPos, &BlockTypePos, IdxEnd/2)) != IdxEnd/2) {
  1484. //return ParseState_Continue;
  1485. }
  1486. }
  1487. else {
  1488. if ((rc = usbvision_decompress(usbvision, StripData, U, &BlockPos, &BlockTypePos, IdxEnd/2)) != IdxEnd/2) {
  1489. //return ParseState_Continue;
  1490. }
  1491. }
  1492. if (BlockPos > usbvision->comprBlockPos) {
  1493. usbvision->comprBlockPos = BlockPos;
  1494. }
  1495. if (BlockPos > StripLen) {
  1496. usbvision->stripLenErrors++;
  1497. }
  1498. for (Idx = 0; Idx < IdxEnd; Idx++) {
  1499. if((overlay) && (clipped_pixel(clipmask_index))) {
  1500. f += bytes_per_pixel;
  1501. }
  1502. else if(frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
  1503. *f++ = Y[Idx];
  1504. *f++ = Idx & 0x01 ? U[Idx/2] : V[Idx/2];
  1505. }
  1506. else if(frame->v4l2_format.format == V4L2_PIX_FMT_YUV422P) {
  1507. *f++ = Y[Idx];
  1508. if ( Idx & 0x01)
  1509. *u++ = U[Idx>>1] ;
  1510. else
  1511. *v++ = V[Idx>>1];
  1512. }
  1513. else if (frame->v4l2_format.format == V4L2_PIX_FMT_YVU420) {
  1514. *f++ = Y [Idx];
  1515. if ( !(( Idx & 0x01 ) | ( frame->curline & 0x01 )) ){
  1516. /* only need do this for 1 in 4 pixels */
  1517. /* intraframe buffer is YUV420 format */
  1518. *u++ = U[Idx >>1];
  1519. *v++ = V[Idx >>1];
  1520. }
  1521. }
  1522. else {
  1523. YUV_TO_RGB_BY_THE_BOOK(Y[Idx], U[Idx/2], V[Idx/2], rv, gv, bv);
  1524. switch (frame->v4l2_format.format) {
  1525. case V4L2_PIX_FMT_GREY:
  1526. *f++ = Y[Idx];
  1527. break;
  1528. case V4L2_PIX_FMT_RGB555:
  1529. *f++ = (0x1F & (bv >> 3)) | (0xE0 & (gv << 2));
  1530. *f++ = (0x03 & (gv >> 6)) | (0x7C & (rv >> 1));
  1531. break;
  1532. case V4L2_PIX_FMT_RGB565:
  1533. *f++ = (0x1F & (bv >> 3)) | (0xE0 & (gv << 3));
  1534. *f++ = (0x07 & (gv >> 5)) | (0xF8 & rv);
  1535. break;
  1536. case V4L2_PIX_FMT_RGB24:
  1537. *f++ = bv;
  1538. *f++ = gv;
  1539. *f++ = rv;
  1540. break;
  1541. case V4L2_PIX_FMT_RGB32:
  1542. *f++ = bv;
  1543. *f++ = gv;
  1544. *f++ = rv;
  1545. f++;
  1546. break;
  1547. }
  1548. }
  1549. clipmask_index++;
  1550. }
  1551. /* Deal with non-integer no. of bytes for YUV420P */
  1552. if (frame->v4l2_format.format != V4L2_PIX_FMT_YVU420 )
  1553. *pcopylen += frame->v4l2_linesize;
  1554. else
  1555. *pcopylen += frame->curline & 0x01 ? frame->v4l2_linesize : frame->v4l2_linesize << 1;
  1556. frame->curline += 1;
  1557. if (frame->curline >= frame->frmheight) {
  1558. return ParseState_NextFrame;
  1559. }
  1560. else {
  1561. return ParseState_Continue;
  1562. }
  1563. }
  1564. /*
  1565. * usbvision_parse_lines_420()
  1566. *
  1567. * Parse two lines from the scratch buffer, put
  1568. * decoded RGB value into the current frame buffer and add the written
  1569. * number of bytes (RGB) to the *pcopylen.
  1570. *
  1571. */
  1572. static enum ParseState usbvision_parse_lines_420(struct usb_usbvision *usbvision,
  1573. long *pcopylen)
  1574. {
  1575. struct usbvision_frame *frame;
  1576. unsigned char *f_even = NULL, *f_odd = NULL;
  1577. unsigned int pixel_per_line, block;
  1578. int pixel, block_split;
  1579. int y_ptr, u_ptr, v_ptr, y_odd_offset;
  1580. const int y_block_size = 128;
  1581. const int uv_block_size = 64;
  1582. const int sub_block_size = 32;
  1583. const int y_step[] = { 0, 0, 0, 2 }, y_step_size = 4;
  1584. const int uv_step[]= { 0, 0, 0, 4 }, uv_step_size = 4;
  1585. unsigned char y[2], u, v; /* YUV components */
  1586. int y_, u_, v_, vb, uvg, ur;
  1587. int r_, g_, b_; /* RGB components */
  1588. unsigned char g;
  1589. int clipmask_even_index, clipmask_odd_index, bytes_per_pixel;
  1590. int clipmask_add, stretch_bytes;
  1591. int overlay = usbvision->overlay;
  1592. if (overlay) {
  1593. frame = &usbvision->overlay_frame;
  1594. if (usbvision->overlay_base == NULL) {
  1595. //video_buffer is not set yet
  1596. return ParseState_NextFrame;
  1597. }
  1598. f_even = usbvision->overlay_win + frame->curline *
  1599. usbvision->vid_buf.fmt.bytesperline;
  1600. f_odd = f_even + usbvision->vid_buf.fmt.bytesperline * usbvision->stretch_height;
  1601. }
  1602. else {
  1603. frame = &usbvision->frame[usbvision->curFrameNum];
  1604. f_even = frame->data + (frame->v4l2_linesize * frame->curline);
  1605. f_odd = f_even + frame->v4l2_linesize * usbvision->stretch_height;
  1606. }
  1607. /* Make sure there's enough data for the entire line */
  1608. /* In this mode usbvision transfer 3 bytes for every 2 pixels */
  1609. /* I need two lines to decode the color */
  1610. bytes_per_pixel = frame->v4l2_format.bytes_per_pixel;
  1611. stretch_bytes = (usbvision->stretch_width - 1) * bytes_per_pixel;
  1612. clipmask_even_index = frame->curline * MAX_FRAME_WIDTH;
  1613. clipmask_odd_index = clipmask_even_index + MAX_FRAME_WIDTH;
  1614. clipmask_add = usbvision->stretch_width;
  1615. pixel_per_line = frame->isocHeader.frameWidth;
  1616. if (scratch_len(usbvision) < (int)pixel_per_line * 3) {
  1617. //printk(KERN_DEBUG "out of data, need %d\n", len);
  1618. return ParseState_Out;
  1619. }
  1620. if ((frame->curline + 1) >= frame->frmheight) {
  1621. return ParseState_NextFrame;
  1622. }
  1623. block_split = (pixel_per_line%y_block_size) ? 1 : 0; //are some blocks splitted into different lines?
  1624. y_odd_offset = (pixel_per_line / y_block_size) * (y_block_size + uv_block_size)
  1625. + block_split * uv_block_size;
  1626. scratch_set_extra_ptr(usbvision, &y_ptr, y_odd_offset);
  1627. scratch_set_extra_ptr(usbvision, &u_ptr, y_block_size);
  1628. scratch_set_extra_ptr(usbvision, &v_ptr, y_odd_offset
  1629. + (4 - block_split) * sub_block_size);
  1630. for (block = 0; block < (pixel_per_line / sub_block_size);
  1631. block++) {
  1632. for (pixel = 0; pixel < sub_block_size; pixel +=2) {
  1633. scratch_get(usbvision, &y[0], 2);
  1634. scratch_get_extra(usbvision, &u, &u_ptr, 1);
  1635. scratch_get_extra(usbvision, &v, &v_ptr, 1);
  1636. //I don't use the YUV_TO_RGB macro for better performance
  1637. v_ = v - 128;
  1638. u_ = u - 128;
  1639. vb = 132252 * v_;
  1640. uvg= -53281 * u_ - 25625 * v_;
  1641. ur = 104595 * u_;
  1642. if((overlay) && (clipped_pixel(clipmask_even_index))) {
  1643. f_even += bytes_per_pixel;
  1644. }
  1645. else if(frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
  1646. *f_even++ = y[0];
  1647. *f_even++ = v;
  1648. }
  1649. else {
  1650. y_ = 76284 * (y[0] - 16);
  1651. b_ = (y_ + vb) >> 16;
  1652. g_ = (y_ + uvg)>> 16;
  1653. r_ = (y_ + ur) >> 16;
  1654. switch (frame->v4l2_format.format) {
  1655. case V4L2_PIX_FMT_RGB565:
  1656. g = LIMIT_RGB(g_);
  1657. *f_even++ = (0x1F & (LIMIT_RGB(b_) >> 3)) | (0xE0 & (g << 3));
  1658. *f_even++ = (0x07 & ( g >> 5)) | (0xF8 & LIMIT_RGB(r_));
  1659. break;
  1660. case V4L2_PIX_FMT_RGB24:
  1661. *f_even++ = LIMIT_RGB(b_);
  1662. *f_even++ = LIMIT_RGB(g_);
  1663. *f_even++ = LIMIT_RGB(r_);
  1664. break;
  1665. case V4L2_PIX_FMT_RGB32:
  1666. *f_even++ = LIMIT_RGB(b_);
  1667. *f_even++ = LIMIT_RGB(g_);
  1668. *f_even++ = LIMIT_RGB(r_);
  1669. f_even++;
  1670. break;
  1671. case V4L2_PIX_FMT_RGB555:
  1672. g = LIMIT_RGB(g_);
  1673. *f_even++ = (0x1F & (LIMIT_RGB(b_) >> 3)) | (0xE0 & (g << 2));
  1674. *f_even++ = (0x03 & ( g >> 6)) |
  1675. (0x7C & (LIMIT_RGB(r_) >> 1));
  1676. break;
  1677. }
  1678. }
  1679. clipmask_even_index += clipmask_add;
  1680. f_even += stretch_bytes;
  1681. if((overlay) && (clipped_pixel(clipmask_even_index))) {
  1682. f_even += bytes_per_pixel;
  1683. }
  1684. else if(frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
  1685. *f_even++ = y[1];
  1686. *f_even++ = u;
  1687. }
  1688. else {
  1689. y_ = 76284 * (y[1] - 16);
  1690. b_ = (y_ + vb) >> 16;
  1691. g_ = (y_ + uvg)>> 16;
  1692. r_ = (y_ + ur) >> 16;
  1693. switch (frame->v4l2_format.format) {
  1694. case V4L2_PIX_FMT_RGB565:
  1695. g = LIMIT_RGB(g_);
  1696. *f_even++ = (0x1F & (LIMIT_RGB(b_) >> 3)) | (0xE0 & (g << 3));
  1697. *f_even++ = (0x07 & ( g >> 5)) | (0xF8 & LIMIT_RGB(r_));
  1698. break;
  1699. case V4L2_PIX_FMT_RGB24:
  1700. *f_even++ = LIMIT_RGB(b_);
  1701. *f_even++ = LIMIT_RGB(g_);
  1702. *f_even++ = LIMIT_RGB(r_);
  1703. break;
  1704. case V4L2_PIX_FMT_RGB32:
  1705. *f_even++ = LIMIT_RGB(b_);
  1706. *f_even++ = LIMIT_RGB(g_);
  1707. *f_even++ = LIMIT_RGB(r_);
  1708. f_even++;
  1709. break;
  1710. case V4L2_PIX_FMT_RGB555:
  1711. g = LIMIT_RGB(g_);
  1712. *f_even++ = (0x1F & (LIMIT_RGB(b_) >> 3)) | (0xE0 & (g << 2));
  1713. *f_even++ = (0x03 & ( g >> 6)) |
  1714. (0x7C & (LIMIT_RGB(r_) >> 1));
  1715. break;
  1716. }
  1717. }
  1718. clipmask_even_index += clipmask_add;
  1719. f_even += stretch_bytes;
  1720. scratch_get_extra(usbvision, &y[0], &y_ptr, 2);
  1721. if ((overlay) && (clipped_pixel(clipmask_odd_index))) {
  1722. f_odd += bytes_per_pixel;
  1723. }
  1724. else if(frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
  1725. *f_odd++ = y[0];
  1726. *f_odd++ = v;
  1727. }
  1728. else {
  1729. y_ = 76284 * (y[0] - 16);
  1730. b_ = (y_ + vb) >> 16;
  1731. g_ = (y_ + uvg)>> 16;
  1732. r_ = (y_ + ur) >> 16;
  1733. switch (frame->v4l2_format.format) {
  1734. case V4L2_PIX_FMT_RGB565:
  1735. g = LIMIT_RGB(g_);
  1736. *f_odd++ = (0x1F & (LIMIT_RGB(b_) >> 3)) | (0xE0 & (g << 3));
  1737. *f_odd++ = (0x07 & ( g >> 5)) | (0xF8 & LIMIT_RGB(r_));
  1738. break;
  1739. case V4L2_PIX_FMT_RGB24:
  1740. *f_odd++ = LIMIT_RGB(b_);
  1741. *f_odd++ = LIMIT_RGB(g_);
  1742. *f_odd++ = LIMIT_RGB(r_);
  1743. break;
  1744. case V4L2_PIX_FMT_RGB32:
  1745. *f_odd++ = LIMIT_RGB(b_);
  1746. *f_odd++ = LIMIT_RGB(g_);
  1747. *f_odd++ = LIMIT_RGB(r_);
  1748. f_odd++;
  1749. break;
  1750. case V4L2_PIX_FMT_RGB555:
  1751. g = LIMIT_RGB(g_);
  1752. *f_odd++ = (0x1F & (LIMIT_RGB(b_) >> 3)) | (0xE0 & (g << 2));
  1753. *f_odd++ = (0x03 & ( g >> 6)) |
  1754. (0x7C & (LIMIT_RGB(r_) >> 1));
  1755. break;
  1756. }
  1757. }
  1758. clipmask_odd_index += clipmask_add;
  1759. f_odd += stretch_bytes;
  1760. if((overlay) && (clipped_pixel(clipmask_odd_index))) {
  1761. f_odd += bytes_per_pixel;
  1762. }
  1763. else if(frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
  1764. *f_odd++ = y[1];
  1765. *f_odd++ = u;
  1766. }
  1767. else {
  1768. y_ = 76284 * (y[1] - 16);
  1769. b_ = (y_ + vb) >> 16;
  1770. g_ = (y_ + uvg)>> 16;
  1771. r_ = (y_ + ur) >> 16;
  1772. switch (frame->v4l2_format.format) {
  1773. case V4L2_PIX_FMT_RGB565:
  1774. g = LIMIT_RGB(g_);
  1775. *f_odd++ = (0x1F & (LIMIT_RGB(b_) >> 3)) | (0xE0 & (g << 3));
  1776. *f_odd++ = (0x07 & ( g >> 5)) | (0xF8 & LIMIT_RGB(r_));
  1777. break;
  1778. case V4L2_PIX_FMT_RGB24:
  1779. *f_odd++ = LIMIT_RGB(b_);
  1780. *f_odd++ = LIMIT_RGB(g_);
  1781. *f_odd++ = LIMIT_RGB(r_);
  1782. break;
  1783. case V4L2_PIX_FMT_RGB32:
  1784. *f_odd++ = LIMIT_RGB(b_);
  1785. *f_odd++ = LIMIT_RGB(g_);
  1786. *f_odd++ = LIMIT_RGB(r_);
  1787. f_odd++;
  1788. break;
  1789. case V4L2_PIX_FMT_RGB555:
  1790. g = LIMIT_RGB(g_);
  1791. *f_odd++ = (0x1F & (LIMIT_RGB(b_) >> 3)) | (0xE0 & (g << 2));
  1792. *f_odd++ = (0x03 & ( g >> 6)) |
  1793. (0x7C & (LIMIT_RGB(r_) >> 1));
  1794. break;
  1795. }
  1796. }
  1797. clipmask_odd_index += clipmask_add;
  1798. f_odd += stretch_bytes;
  1799. }
  1800. scratch_rm_old(usbvision,y_step[block % y_step_size] * sub_block_size);
  1801. scratch_inc_extra_ptr(&y_ptr, y_step[(block + 2 * block_split) % y_step_size]
  1802. * sub_block_size);
  1803. scratch_inc_extra_ptr(&u_ptr, uv_step[block % uv_step_size]
  1804. * sub_block_size);
  1805. scratch_inc_extra_ptr(&v_ptr, uv_step[(block + 2 * block_split) % uv_step_size]
  1806. * sub_block_size);
  1807. }
  1808. scratch_rm_old(usbvision, pixel_per_line * 3 / 2
  1809. + block_split * sub_block_size);
  1810. frame->curline += 2 * usbvision->stretch_height;
  1811. *pcopylen += frame->v4l2_linesize * 2 * usbvision->stretch_height;
  1812. if (frame->curline >= frame->frmheight)
  1813. return ParseState_NextFrame;
  1814. else
  1815. return ParseState_Continue;
  1816. }
  1817. /*
  1818. * usbvision_parse_data()
  1819. *
  1820. * Generic routine to parse the scratch buffer. It employs either
  1821. * usbvision_find_header() or usbvision_parse_lines() to do most
  1822. * of work.
  1823. *
  1824. */
  1825. static void usbvision_parse_data(struct usb_usbvision *usbvision)
  1826. {
  1827. struct usbvision_frame *frame;
  1828. enum ParseState newstate;
  1829. long copylen = 0;
  1830. if (usbvision->overlay) {
  1831. frame = &usbvision->overlay_frame;
  1832. }
  1833. else {
  1834. frame = &usbvision->frame[usbvision->curFrameNum];
  1835. }
  1836. PDEBUG(DBG_PARSE, "parsing len=%d\n", scratch_len(usbvision));
  1837. while (1) {
  1838. newstate = ParseState_Out;
  1839. if (scratch_len(usbvision)) {
  1840. if (frame->scanstate == ScanState_Scanning) {
  1841. newstate = usbvision_find_header(usbvision);
  1842. }
  1843. else if (frame->scanstate == ScanState_Lines) {
  1844. if (usbvision->isocMode == ISOC_MODE_YUV420) {
  1845. newstate = usbvision_parse_lines_420(usbvision, &copylen);
  1846. }
  1847. else if (usbvision->isocMode == ISOC_MODE_YUV422) {
  1848. newstate = usbvision_parse_lines_422(usbvision, &copylen);
  1849. }
  1850. else if (usbvision->isocMode == ISOC_MODE_COMPRESS) {
  1851. newstate = usbvision_parse_compress(usbvision, &copylen);
  1852. }
  1853. }
  1854. }
  1855. if (newstate == ParseState_Continue) {
  1856. continue;
  1857. }
  1858. else if ((newstate == ParseState_NextFrame) || (newstate == ParseState_Out)) {
  1859. break;
  1860. }
  1861. else {
  1862. return; /* ParseState_EndParse */
  1863. }
  1864. }
  1865. if (newstate == ParseState_NextFrame) {
  1866. frame->grabstate = FrameState_Done;
  1867. do_gettimeofday(&(frame->timestamp));
  1868. frame->sequence = usbvision->frame_num;
  1869. if (usbvision->overlay) {
  1870. frame->grabstate = FrameState_Grabbing;
  1871. frame->scanstate = ScanState_Scanning;
  1872. frame->scanlength = 0;
  1873. copylen = 0;
  1874. }
  1875. else {
  1876. usbvision->curFrameNum = -1;
  1877. }
  1878. usbvision->frame_num++;
  1879. /* Optionally display statistics on the screen */
  1880. if (flags & FLAGS_OSD_STATS)
  1881. usbvision_osd_stats(usbvision, frame);
  1882. /* This will cause the process to request another frame. */
  1883. if (waitqueue_active(&frame->wq)) {
  1884. wake_up_interruptible(&frame->wq);
  1885. }
  1886. }
  1887. /* Update the frame's uncompressed length. */
  1888. frame->scanlength += copylen;
  1889. }
  1890. /*
  1891. * Make all of the blocks of data contiguous
  1892. */
  1893. static int usbvision_compress_isochronous(struct usb_usbvision *usbvision,
  1894. struct urb *urb)
  1895. {
  1896. unsigned char *packet_data;
  1897. int i, totlen = 0;
  1898. for (i = 0; i < urb->number_of_packets; i++) {
  1899. int packet_len = urb->iso_frame_desc[i].actual_length;
  1900. int packet_stat = urb->iso_frame_desc[i].status;
  1901. packet_data = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  1902. /* Detect and ignore errored packets */
  1903. if (packet_stat) { // packet_stat != 0 ?????????????
  1904. PDEBUG(DBG_ISOC, "data error: [%d] len=%d, status=%X", i, packet_len, packet_stat);
  1905. usbvision->isocErrCount++;
  1906. continue;
  1907. }
  1908. /* Detect and ignore empty packets */
  1909. if (packet_len < 0) {
  1910. PDEBUG(DBG_ISOC, "error packet [%d]", i);
  1911. usbvision->isocSkipCount++;
  1912. continue;
  1913. }
  1914. else if (packet_len == 0) { /* Frame end ????? */
  1915. PDEBUG(DBG_ISOC, "null packet [%d]", i);
  1916. usbvision->isocstate=IsocState_NoFrame;
  1917. usbvision->isocSkipCount++;
  1918. continue;
  1919. }
  1920. else if (packet_len > usbvision->isocPacketSize) {
  1921. PDEBUG(DBG_ISOC, "packet[%d] > isocPacketSize", i);
  1922. usbvision->isocSkipCount++;
  1923. continue;
  1924. }
  1925. PDEBUG(DBG_ISOC, "packet ok [%d] len=%d", i, packet_len);
  1926. if (usbvision->isocstate==IsocState_NoFrame) { //new frame begins
  1927. usbvision->isocstate=IsocState_InFrame;
  1928. scratch_mark_header(usbvision);
  1929. usbvision_measure_bandwidth(usbvision);
  1930. PDEBUG(DBG_ISOC, "packet with header");
  1931. }
  1932. /*
  1933. * If usbvision continues to feed us with data but there is no
  1934. * consumption (if, for example, V4L client fell asleep) we
  1935. * may overflow the buffer. We have to move old data over to
  1936. * free room for new data. This is bad for old data. If we
  1937. * just drop new data then it's bad for new data... choose
  1938. * your favorite evil here.
  1939. */
  1940. if (scratch_free(usbvision) < packet_len) {
  1941. usbvision->scratch_ovf_count++;
  1942. PDEBUG(DBG_ISOC, "scratch buf overflow! scr_len: %d, n: %d",
  1943. scratch_len(usbvision), packet_len);
  1944. scratch_rm_old(usbvision, packet_len - scratch_free(usbvision));
  1945. }
  1946. /* Now we know that there is enough room in scratch buffer */
  1947. scratch_put(usbvision, packet_data, packet_len);
  1948. totlen += packet_len;
  1949. usbvision->isocDataCount += packet_len;
  1950. usbvision->isocPacketCount++;
  1951. }
  1952. #if ENABLE_HEXDUMP
  1953. if (totlen > 0) {
  1954. static int foo = 0;
  1955. if (foo < 1) {
  1956. printk(KERN_DEBUG "+%d.\n", usbvision->scratchlen);
  1957. usbvision_hexdump(data0, (totlen > 64) ? 64 : totlen);
  1958. ++foo;
  1959. }
  1960. }
  1961. #endif
  1962. return totlen;
  1963. }
  1964. static void usbvision_isocIrq(struct urb *urb, struct pt_regs *regs)
  1965. {
  1966. int errCode = 0;
  1967. int len;
  1968. struct usb_usbvision *usbvision = urb->context;
  1969. int i;
  1970. unsigned long startTime = jiffies;
  1971. /* We don't want to do anything if we are about to be removed! */
  1972. if (!USBVISION_IS_OPERATIONAL(usbvision))
  1973. return;
  1974. if (!usbvision->streaming) {
  1975. PDEBUG(DBG_IRQ, "oops, not streaming, but interrupt");
  1976. return;
  1977. }
  1978. /* Copy the data received into our scratch buffer */
  1979. len = usbvision_compress_isochronous(usbvision, urb);
  1980. usbvision->isocUrbCount++;
  1981. usbvision->urb_length = len;
  1982. for (i = 0; i < USBVISION_URB_FRAMES; i++) {
  1983. urb->iso_frame_desc[i].status = 0;
  1984. urb->iso_frame_desc[i].actual_length = 0;
  1985. }
  1986. urb->status = 0;
  1987. urb->dev = usbvision->dev;
  1988. errCode = usb_submit_urb (urb, GFP_ATOMIC);
  1989. /* Disable this warning. By design of the driver. */
  1990. // if(errCode) {
  1991. // err("%s: usb_submit_urb failed: error %d", __FUNCTION__, errCode);
  1992. // }
  1993. /* If we collected enough data let's parse! */
  1994. if (scratch_len(usbvision) > USBVISION_HEADER_LENGTH) { /* 12 == header_length */
  1995. /*If we don't have a frame we're current working on, complain */
  1996. if ((usbvision->curFrameNum >= 0) || (usbvision->overlay))
  1997. usbvision_parse_data(usbvision);
  1998. else {
  1999. PDEBUG(DBG_IRQ, "received data, but no one needs it");
  2000. scratch_reset(usbvision);
  2001. }
  2002. }
  2003. usbvision->timeInIrq += jiffies - startTime;
  2004. return;
  2005. }
  2006. /*************************************/
  2007. /* Low level usbvision access functions */
  2008. /*************************************/
  2009. /*
  2010. * usbvision_read_reg()
  2011. *
  2012. * return < 0 -> Error
  2013. * >= 0 -> Data
  2014. */
  2015. static int usbvision_read_reg(struct usb_usbvision *usbvision, unsigned char reg)
  2016. {
  2017. int errCode = 0;
  2018. unsigned char buffer[1];
  2019. if (!USBVISION_IS_OPERATIONAL(usbvision))
  2020. return -1;
  2021. errCode = usb_control_msg(usbvision->dev, usb_rcvctrlpipe(usbvision->dev, 1),
  2022. USBVISION_OP_CODE,
  2023. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT,
  2024. 0, (__u16) reg, buffer, 1, HZ);
  2025. if (errCode < 0) {
  2026. err("%s: failed: error %d", __FUNCTION__, errCode);
  2027. return errCode;
  2028. }
  2029. return buffer[0];
  2030. }
  2031. /*
  2032. * usbvision_write_reg()
  2033. *
  2034. * return 1 -> Reg written
  2035. * 0 -> usbvision is not yet ready
  2036. * -1 -> Something went wrong
  2037. */
  2038. static int usbvision_write_reg(struct usb_usbvision *usbvision, unsigned char reg,
  2039. unsigned char value)
  2040. {
  2041. int errCode = 0;
  2042. if (!USBVISION_IS_OPERATIONAL(usbvision))
  2043. return 0;
  2044. errCode = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1),
  2045. USBVISION_OP_CODE,
  2046. USB_DIR_OUT | USB_TYPE_VENDOR |
  2047. USB_RECIP_ENDPOINT, 0, (__u16) reg, &value, 1, HZ);
  2048. if (errCode < 0) {
  2049. err("%s: failed: error %d", __FUNCTION__, errCode);
  2050. }
  2051. return errCode;
  2052. }
  2053. static void usbvision_ctrlUrb_complete(struct urb *urb, struct pt_regs *regs)
  2054. {
  2055. struct usb_usbvision *usbvision = (struct usb_usbvision *)urb->context;
  2056. PDEBUG(DBG_IRQ, "");
  2057. usbvision->ctrlUrbBusy = 0;
  2058. if (waitqueue_active(&usbvision->ctrlUrb_wq)) {
  2059. wake_up_interruptible(&usbvision->ctrlUrb_wq);
  2060. }
  2061. }
  2062. static int usbvision_write_reg_irq(struct usb_usbvision *usbvision,int address,
  2063. unsigned char *data, int len)
  2064. {
  2065. int errCode = 0;
  2066. PDEBUG(DBG_IRQ, "");
  2067. if (len > 8) {
  2068. return -EFAULT;
  2069. }
  2070. // down(&usbvision->ctrlUrbLock);
  2071. if (usbvision->ctrlUrbBusy) {
  2072. // up(&usbvision->ctrlUrbLock);
  2073. return -EBUSY;
  2074. }
  2075. usbvision->ctrlUrbBusy = 1;
  2076. // up(&usbvision->ctrlUrbLock);
  2077. usbvision->ctrlUrbSetup.bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT;
  2078. usbvision->ctrlUrbSetup.bRequest = USBVISION_OP_CODE;
  2079. usbvision->ctrlUrbSetup.wValue = 0;
  2080. usbvision->ctrlUrbSetup.wIndex = cpu_to_le16(address);
  2081. usbvision->ctrlUrbSetup.wLength = cpu_to_le16(len);
  2082. usb_fill_control_urb (usbvision->ctrlUrb, usbvision->dev,
  2083. usb_sndctrlpipe(usbvision->dev, 1),
  2084. (unsigned char *)&usbvision->ctrlUrbSetup,
  2085. (void *)usbvision->ctrlUrbBuffer, len,
  2086. usbvision_ctrlUrb_complete,
  2087. (void *)usbvision);
  2088. memcpy(usbvision->ctrlUrbBuffer, data, len);
  2089. errCode = usb_submit_urb(usbvision->ctrlUrb, GFP_ATOMIC);
  2090. if (errCode < 0) {
  2091. // error in usb_submit_urb()
  2092. usbvision->ctrlUrbBusy = 0;
  2093. }
  2094. PDEBUG(DBG_IRQ, "submit %d byte: error %d", len, errCode);
  2095. return errCode;
  2096. }
  2097. static int usbvision_init_compression(struct usb_usbvision *usbvision)
  2098. {
  2099. int errCode = 0;
  2100. usbvision->lastIsocFrameNum = -1;
  2101. usbvision->isocDataCount = 0;
  2102. usbvision->isocPacketCount = 0;
  2103. usbvision->isocSkipCount = 0;
  2104. usbvision->comprLevel = 50;
  2105. usbvision->lastComprLevel = -1;
  2106. usbvision->isocUrbCount = 0;
  2107. usbvision->requestIntra = 1;
  2108. usbvision->isocMeasureBandwidthCount = 0;
  2109. return errCode;
  2110. }
  2111. /* this function measures the used bandwidth since last call
  2112. * return: 0 : no error
  2113. * sets usedBandwidth to 1-100 : 1-100% of full bandwidth resp. to isocPacketSize
  2114. */
  2115. static int usbvision_measure_bandwidth (struct usb_usbvision *usbvision)
  2116. {
  2117. int errCode = 0;
  2118. if (usbvision->isocMeasureBandwidthCount < 2) { // this gives an average bandwidth of 3 frames
  2119. usbvision->isocMeasureBandwidthCount++;
  2120. return errCode;
  2121. }
  2122. if ((usbvision->isocPacketSize > 0) && (usbvision->isocPacketCount > 0)) {
  2123. usbvision->usedBandwidth = usbvision->isocDataCount /
  2124. (usbvision->isocPacketCount + usbvision->isocSkipCount) *
  2125. 100 / usbvision->isocPacketSize;
  2126. }
  2127. usbvision->isocMeasureBandwidthCount = 0;
  2128. usbvision->isocDataCount = 0;
  2129. usbvision->isocPacketCount = 0;
  2130. usbvision->isocSkipCount = 0;
  2131. return errCode;
  2132. }
  2133. static int usbvision_adjust_compression (struct usb_usbvision *usbvision)
  2134. {
  2135. int errCode = 0;
  2136. unsigned char buffer[6];
  2137. PDEBUG(DBG_IRQ, "");
  2138. if ((adjustCompression) && (usbvision->usedBandwidth > 0)) {
  2139. usbvision->comprLevel += (usbvision->usedBandwidth - 90) / 2;
  2140. RESTRICT_TO_RANGE(usbvision->comprLevel, 0, 100);
  2141. if (usbvision->comprLevel != usbvision->lastComprLevel) {
  2142. int distorsion;
  2143. if (usbvision->bridgeType == BRIDGE_NT1004 || usbvision->bridgeType == BRIDGE_NT1005) {
  2144. buffer[0] = (unsigned char)(4 + 16 * usbvision->comprLevel / 100); // PCM Threshold 1
  2145. buffer[1] = (unsigned char)(4 + 8 * usbvision->comprLevel / 100); // PCM Threshold 2
  2146. distorsion = 7 + 248 * usbvision->comprLevel / 100;
  2147. buffer[2] = (unsigned char)(distorsion & 0xFF); // Average distorsion Threshold (inter)
  2148. buffer[3] = (unsigned char)(distorsion & 0xFF); // Average distorsion Threshold (intra)
  2149. distorsion = 1 + 42 * usbvision->comprLevel / 100;
  2150. buffer[4] = (unsigned char)(distorsion & 0xFF); // Maximum distorsion Threshold (inter)
  2151. buffer[5] = (unsigned char)(distorsion & 0xFF); // Maximum distorsion Threshold (intra)
  2152. }
  2153. else { //BRIDGE_NT1003
  2154. buffer[0] = (unsigned char)(4 + 16 * usbvision->comprLevel / 100); // PCM threshold 1
  2155. buffer[1] = (unsigned char)(4 + 8 * usbvision->comprLevel / 100); // PCM threshold 2
  2156. distorsion = 2 + 253 * usbvision->comprLevel / 100;
  2157. buffer[2] = (unsigned char)(distorsion & 0xFF); // distorsion threshold bit0-7
  2158. buffer[3] = 0; //(unsigned char)((distorsion >> 8) & 0x0F); // distorsion threshold bit 8-11
  2159. distorsion = 0 + 43 * usbvision->comprLevel / 100;
  2160. buffer[4] = (unsigned char)(distorsion & 0xFF); // maximum distorsion bit0-7
  2161. buffer[5] = 0; //(unsigned char)((distorsion >> 8) & 0x01); // maximum distorsion bit 8
  2162. }
  2163. errCode = usbvision_write_reg_irq(usbvision, USBVISION_PCM_THR1, buffer, 6);
  2164. if (errCode == 0){
  2165. PDEBUG(DBG_IRQ, "new compr params %#02x %#02x %#02x %#02x %#02x %#02x", buffer[0],
  2166. buffer[1], buffer[2], buffer[3], buffer[4], buffer[5]);
  2167. usbvision->lastComprLevel = usbvision->comprLevel;
  2168. }
  2169. }
  2170. }
  2171. return errCode;
  2172. }
  2173. static int usbvision_request_intra (struct usb_usbvision *usbvision)
  2174. {
  2175. int errCode = 0;
  2176. unsigned char buffer[1];
  2177. PDEBUG(DBG_IRQ, "");
  2178. usbvision->requestIntra = 1;
  2179. buffer[0] = 1;
  2180. usbvision_write_reg_irq(usbvision, USBVISION_FORCE_INTRA, buffer, 1);
  2181. return errCode;
  2182. }
  2183. static int usbvision_unrequest_intra (struct usb_usbvision *usbvision)
  2184. {
  2185. int errCode = 0;
  2186. unsigned char buffer[1];
  2187. PDEBUG(DBG_IRQ, "");
  2188. usbvision->requestIntra = 0;
  2189. buffer[0] = 0;
  2190. usbvision_write_reg_irq(usbvision, USBVISION_FORCE_INTRA, buffer, 1);
  2191. return errCode;
  2192. }
  2193. /* ----------------------------------------------------------------------- */
  2194. /* I2C functions */
  2195. /* ----------------------------------------------------------------------- */
  2196. static void call_i2c_clients(struct usb_usbvision *usbvision, unsigned int cmd,
  2197. void *arg)
  2198. {
  2199. int i;
  2200. for (i = 0; i < USBVISION_I2C_CLIENTS_MAX; i++) {
  2201. if (NULL == usbvision->i2c_clients[i])
  2202. continue;
  2203. if (NULL == usbvision->i2c_clients[i]->driver->command)
  2204. continue;
  2205. usbvision->i2c_clients[i]->driver->command(usbvision->i2c_clients[i], cmd, arg);
  2206. }
  2207. }
  2208. static int attach_inform(struct i2c_client *client)
  2209. {
  2210. struct usb_usbvision *usbvision;
  2211. struct tuner_setup tun_addr;
  2212. int i;
  2213. v4l2_std_id stdId;
  2214. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
  2215. usbvision = (struct usb_usbvision *)client->adapter->data;
  2216. #else
  2217. usbvision = (struct usb_usbvision *)i2c_get_adapdata(client->adapter);
  2218. #endif
  2219. for (i = 0; i < USBVISION_I2C_CLIENTS_MAX; i++) {
  2220. if (usbvision->i2c_clients[i] == NULL ||
  2221. usbvision->i2c_clients[i]->driver->id ==
  2222. client->driver->id) {
  2223. usbvision->i2c_clients[i] = client;
  2224. break;
  2225. }
  2226. }
  2227. if ((usbvision->have_tuner) && (usbvision->tuner_type != -1)) {
  2228. tun_addr.mode_mask = T_ANALOG_TV;
  2229. tun_addr.type = usbvision->tuner_type;
  2230. tun_addr.addr = ADDR_UNSET;
  2231. client->driver->command(client,TUNER_SET_TYPE_ADDR, &tun_addr);
  2232. call_i2c_clients(usbvision, VIDIOC_S_INPUT, &usbvision->input.index);
  2233. }
  2234. // FIXME : need to add a call VIDIOC_S_CTRL for each control
  2235. /* call_i2c_clients(usbvision, DECODER_SET_PICTURE, &usbvision->vpic); */
  2236. stdId = usbvision->input.std;
  2237. call_i2c_clients(usbvision, VIDIOC_S_STD, &stdId);
  2238. PDEBUG(DBG_I2C, "usbvision[%d] attaches %s", usbvision->nr, client->name);
  2239. return 0;
  2240. }
  2241. static int detach_inform(struct i2c_client *client)
  2242. {
  2243. struct usb_usbvision *usbvision;
  2244. int i;
  2245. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
  2246. usbvision = (struct usb_usbvision *)client->adapter->data;
  2247. #else
  2248. usbvision = (struct usb_usbvision *)i2c_get_adapdata(client->adapter);
  2249. #endif
  2250. PDEBUG(DBG_I2C, "usbvision[%d] detaches %s", usbvision->nr, client->name);
  2251. for (i = 0; i < USBVISION_I2C_CLIENTS_MAX; i++) {
  2252. if (NULL != usbvision->i2c_clients[i] &&
  2253. usbvision->i2c_clients[i]->driver->id ==
  2254. client->driver->id) {
  2255. usbvision->i2c_clients[i] = NULL;
  2256. break;
  2257. }
  2258. }
  2259. return 0;
  2260. }
  2261. static int
  2262. usbvision_i2c_read_max4(struct usb_usbvision *usbvision, unsigned char addr,
  2263. char *buf, short len)
  2264. {
  2265. int rc, retries;
  2266. for (retries = 5;;) {
  2267. rc = usbvision_write_reg(usbvision, USBVISION_SER_ADRS, addr);
  2268. if (rc < 0)
  2269. return rc;
  2270. /* Initiate byte read cycle */
  2271. /* USBVISION_SER_CONT <- d0-d2 n. of bytes to r/w */
  2272. /* d3 0=Wr 1=Rd */
  2273. rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT,
  2274. (len & 0x07) | 0x18);
  2275. if (rc < 0)
  2276. return rc;
  2277. /* Test for Busy and ACK */
  2278. do {
  2279. /* USBVISION_SER_CONT -> d4 == 0 busy */
  2280. rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT);
  2281. } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */
  2282. if (rc < 0)
  2283. return rc;
  2284. /* USBVISION_SER_CONT -> d5 == 1 Not ack */
  2285. if ((rc & 0x20) == 0) /* Ack? */
  2286. break;
  2287. /* I2C abort */
  2288. rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00);
  2289. if (rc < 0)
  2290. return rc;
  2291. if (--retries < 0)
  2292. return -1;
  2293. }
  2294. switch (len) {
  2295. case 4:
  2296. buf[3] = usbvision_read_reg(usbvision, USBVISION_SER_DAT4);
  2297. case 3:
  2298. buf[2] = usbvision_read_reg(usbvision, USBVISION_SER_DAT3);
  2299. case 2:
  2300. buf[1] = usbvision_read_reg(usbvision, USBVISION_SER_DAT2);
  2301. case 1:
  2302. buf[0] = usbvision_read_reg(usbvision, USBVISION_SER_DAT1);
  2303. break;
  2304. default:
  2305. printk(KERN_ERR
  2306. "usbvision_i2c_read_max4: buffer length > 4\n");
  2307. }
  2308. if (debug & DBG_I2C) {
  2309. int idx;
  2310. for (idx = 0; idx < len; idx++) {
  2311. PDEBUG(DBG_I2C, "read %x from address %x", (unsigned char)buf[idx], addr);
  2312. }
  2313. }
  2314. return len;
  2315. }
  2316. static int usbvision_i2c_write_max4(struct usb_usbvision *usbvision,
  2317. unsigned char addr, const char *buf,
  2318. short len)
  2319. {
  2320. int rc, retries;
  2321. int i;
  2322. unsigned char value[6];
  2323. unsigned char ser_cont;
  2324. ser_cont = (len & 0x07) | 0x10;
  2325. value[0] = addr;
  2326. value[1] = ser_cont;
  2327. for (i = 0; i < len; i++)
  2328. value[i + 2] = buf[i];
  2329. for (retries = 5;;) {
  2330. rc = usb_control_msg(usbvision->dev,
  2331. usb_sndctrlpipe(usbvision->dev, 1),
  2332. USBVISION_OP_CODE,
  2333. USB_DIR_OUT | USB_TYPE_VENDOR |
  2334. USB_RECIP_ENDPOINT, 0,
  2335. (__u16) USBVISION_SER_ADRS, value,
  2336. len + 2, HZ);
  2337. if (rc < 0)
  2338. return rc;
  2339. rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT,
  2340. (len & 0x07) | 0x10);
  2341. if (rc < 0)
  2342. return rc;
  2343. /* Test for Busy and ACK */
  2344. do {
  2345. rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT);
  2346. } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */
  2347. if (rc < 0)
  2348. return rc;
  2349. if ((rc & 0x20) == 0) /* Ack? */
  2350. break;
  2351. /* I2C abort */
  2352. usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00);
  2353. if (--retries < 0)
  2354. return -1;
  2355. }
  2356. if (debug & DBG_I2C) {
  2357. int idx;
  2358. for (idx = 0; idx < len; idx++) {
  2359. PDEBUG(DBG_I2C, "wrote %x at address %x", (unsigned char)buf[idx], addr);
  2360. }
  2361. }
  2362. return len;
  2363. }
  2364. static int usbvision_i2c_write(void *data, unsigned char addr, char *buf,
  2365. short len)
  2366. {
  2367. char *bufPtr = buf;
  2368. int retval;
  2369. int wrcount = 0;
  2370. int count;
  2371. int maxLen = 4;
  2372. struct usb_usbvision *usbvision = (struct usb_usbvision *) data;
  2373. while (len > 0) {
  2374. count = (len > maxLen) ? maxLen : len;
  2375. retval = usbvision_i2c_write_max4(usbvision, addr, bufPtr, count);
  2376. if (retval > 0) {
  2377. len -= count;
  2378. bufPtr += count;
  2379. wrcount += count;
  2380. } else
  2381. return (retval < 0) ? retval : -EFAULT;
  2382. }
  2383. return wrcount;
  2384. }
  2385. static int usbvision_i2c_read(void *data, unsigned char addr, char *buf,
  2386. short len)
  2387. {
  2388. char temp[4];
  2389. int retval, i;
  2390. int rdcount = 0;
  2391. int count;
  2392. struct usb_usbvision *usbvision = (struct usb_usbvision *) data;
  2393. while (len > 0) {
  2394. count = (len > 3) ? 4 : len;
  2395. retval = usbvision_i2c_read_max4(usbvision, addr, temp, count);
  2396. if (retval > 0) {
  2397. for (i = 0; i < len; i++)
  2398. buf[rdcount + i] = temp[i];
  2399. len -= count;
  2400. rdcount += count;
  2401. } else
  2402. return (retval < 0) ? retval : -EFAULT;
  2403. }
  2404. return rdcount;
  2405. }
  2406. static struct i2c_algo_usb_data i2c_algo_template = {
  2407. .data = NULL,
  2408. .inb = usbvision_i2c_read,
  2409. .outb = usbvision_i2c_write,
  2410. .udelay = 10,
  2411. .mdelay = 10,
  2412. .timeout = 100,
  2413. };
  2414. static struct i2c_adapter i2c_adap_template = {
  2415. .owner = THIS_MODULE,
  2416. .name = "usbvision",
  2417. .id = I2C_HW_B_BT848, /* FIXME */
  2418. .algo = NULL,
  2419. .algo_data = NULL,
  2420. .client_register = attach_inform,
  2421. .client_unregister = detach_inform,
  2422. #if defined (I2C_ADAP_CLASS_TV_ANALOG)
  2423. .class = I2C_ADAP_CLASS_TV_ANALOG,
  2424. #elif defined (I2C_CLASS_TV_ANALOG)
  2425. .class = I2C_CLASS_TV_ANALOG,
  2426. #endif
  2427. };
  2428. static struct i2c_client i2c_client_template = {
  2429. .name = "usbvision internal",
  2430. .flags = 0,
  2431. .addr = 0,
  2432. .adapter = NULL,
  2433. .driver = NULL,
  2434. };
  2435. static int usbvision_init_i2c(struct usb_usbvision *usbvision)
  2436. {
  2437. memcpy(&usbvision->i2c_adap, &i2c_adap_template,
  2438. sizeof(struct i2c_adapter));
  2439. memcpy(&usbvision->i2c_algo, &i2c_algo_template,
  2440. sizeof(struct i2c_algo_usb_data));
  2441. memcpy(&usbvision->i2c_client, &i2c_client_template,
  2442. sizeof(struct i2c_client));
  2443. sprintf(usbvision->i2c_adap.name + strlen(usbvision->i2c_adap.name),
  2444. " #%d", usbvision->vdev->minor & 0x1f);
  2445. PDEBUG(DBG_I2C, "Adaptername: %s", usbvision->i2c_adap.name);
  2446. i2c_set_adapdata(&usbvision->i2c_adap, usbvision);
  2447. i2c_set_clientdata(&usbvision->i2c_client, usbvision);
  2448. i2c_set_algo_usb_data(&usbvision->i2c_algo, usbvision);
  2449. usbvision->i2c_adap.algo_data = &usbvision->i2c_algo;
  2450. usbvision->i2c_client.adapter = &usbvision->i2c_adap;
  2451. if (usbvision_write_reg(usbvision, USBVISION_SER_MODE, USBVISION_IIC_LRNACK) < 0) {
  2452. printk(KERN_ERR "usbvision_init_i2c: can't wirte reg\n");
  2453. return -EBUSY;
  2454. }
  2455. #ifdef CONFIG_KMOD
  2456. /* Request the load of the i2c modules we need */
  2457. if (autoload) {
  2458. switch (usbvision_device_data[usbvision->DevModel].Codec) {
  2459. case CODEC_SAA7113:
  2460. request_module("saa7115");
  2461. break;
  2462. case CODEC_SAA7111:
  2463. request_module("saa7115");
  2464. break;
  2465. }
  2466. if (usbvision_device_data[usbvision->DevModel].Tuner == 1) {
  2467. request_module("tuner");
  2468. }
  2469. }
  2470. #endif
  2471. usbvision->i2c_ok = usbvision_i2c_usb_add_bus(&usbvision->i2c_adap);
  2472. return usbvision->i2c_ok;
  2473. }
  2474. /****************************/
  2475. /* usbvision utility functions */
  2476. /****************************/
  2477. static int usbvision_power_off(struct usb_usbvision *usbvision)
  2478. {
  2479. int errCode = 0;
  2480. PDEBUG(DBG_FUNC, "");
  2481. errCode = usbvision_write_reg(usbvision, USBVISION_PWR_REG, USBVISION_SSPND_EN);
  2482. if (errCode == 1) {
  2483. usbvision->power = 0;
  2484. }
  2485. PDEBUG(DBG_FUNC, "%s: errCode %d", (errCode!=1)?"ERROR":"power is off", errCode);
  2486. return errCode;
  2487. }
  2488. // to call usbvision_power_off from task queue
  2489. static void call_usbvision_power_off(void *_usbvision)
  2490. {
  2491. struct usb_usbvision *usbvision = _usbvision;
  2492. PDEBUG(DBG_FUNC, "");
  2493. down_interruptible(&usbvision->lock);
  2494. if(usbvision->user == 0) {
  2495. usbvision_i2c_usb_del_bus(&usbvision->i2c_adap);
  2496. usbvision_power_off(usbvision);
  2497. usbvision->initialized = 0;
  2498. }
  2499. up(&usbvision->lock);
  2500. }
  2501. /*
  2502. * usbvision_set_video_format()
  2503. *
  2504. */
  2505. static int usbvision_set_video_format(struct usb_usbvision *usbvision, int format)
  2506. {
  2507. static const char proc[] = "usbvision_set_video_format";
  2508. int rc;
  2509. unsigned char value[2];
  2510. if (!USBVISION_IS_OPERATIONAL(usbvision))
  2511. return 0;
  2512. PDEBUG(DBG_FUNC, "isocMode %#02x", format);
  2513. if ((format != ISOC_MODE_YUV422)
  2514. && (format != ISOC_MODE_YUV420)
  2515. && (format != ISOC_MODE_COMPRESS)) {
  2516. printk(KERN_ERR "usbvision: unknown video format %02x, using default YUV420",
  2517. format);
  2518. format = ISOC_MODE_YUV420;
  2519. }
  2520. value[0] = 0x0A; //TODO: See the effect of the filter
  2521. value[1] = format;
  2522. rc = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1),
  2523. USBVISION_OP_CODE,
  2524. USB_DIR_OUT | USB_TYPE_VENDOR |
  2525. USB_RECIP_ENDPOINT, 0,
  2526. (__u16) USBVISION_FILT_CONT, value, 2, HZ);
  2527. if (rc < 0) {
  2528. printk(KERN_ERR "%s: ERROR=%d. USBVISION stopped - "
  2529. "reconnect or reload driver.\n", proc, rc);
  2530. }
  2531. usbvision->isocMode = format;
  2532. return rc;
  2533. }
  2534. /*
  2535. * usbvision_set_output()
  2536. *
  2537. */
  2538. static int usbvision_set_output(struct usb_usbvision *usbvision, int width,
  2539. int height)
  2540. {
  2541. int errCode = 0;
  2542. int UsbWidth, UsbHeight;
  2543. unsigned int frameRate=0, frameDrop=0;
  2544. unsigned char value[4];
  2545. if (!USBVISION_IS_OPERATIONAL(usbvision)) {
  2546. return 0;
  2547. }
  2548. if (width > MAX_USB_WIDTH) {
  2549. UsbWidth = width / 2;
  2550. usbvision->stretch_width = 2;
  2551. }
  2552. else {
  2553. UsbWidth = width;
  2554. usbvision->stretch_width = 1;
  2555. }
  2556. if (height > MAX_USB_HEIGHT) {
  2557. UsbHeight = height / 2;
  2558. usbvision->stretch_height = 2;
  2559. }
  2560. else {
  2561. UsbHeight = height;
  2562. usbvision->stretch_height = 1;
  2563. }
  2564. RESTRICT_TO_RANGE(UsbWidth, MIN_FRAME_WIDTH, MAX_USB_WIDTH);
  2565. UsbWidth &= ~(MIN_FRAME_WIDTH-1);
  2566. RESTRICT_TO_RANGE(UsbHeight, MIN_FRAME_HEIGHT, MAX_USB_HEIGHT);
  2567. UsbHeight &= ~(1);
  2568. PDEBUG(DBG_FUNC, "usb %dx%d; screen %dx%d; stretch %dx%d",
  2569. UsbWidth, UsbHeight, width, height,
  2570. usbvision->stretch_width, usbvision->stretch_height);
  2571. /* I'll not rewrite the same values */
  2572. if ((UsbWidth != usbvision->curwidth) || (UsbHeight != usbvision->curheight)) {
  2573. value[0] = UsbWidth & 0xff; //LSB
  2574. value[1] = (UsbWidth >> 8) & 0x03; //MSB
  2575. value[2] = UsbHeight & 0xff; //LSB
  2576. value[3] = (UsbHeight >> 8) & 0x03; //MSB
  2577. errCode = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1),
  2578. USBVISION_OP_CODE,
  2579. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT,
  2580. 0, (__u16) USBVISION_LXSIZE_O, value, 4, HZ);
  2581. if (errCode < 0) {
  2582. err("%s failed: error %d", __FUNCTION__, errCode);
  2583. return errCode;
  2584. }
  2585. usbvision->curwidth = usbvision->stretch_width * UsbWidth;
  2586. usbvision->curheight = usbvision->stretch_height * UsbHeight;
  2587. }
  2588. if (usbvision->isocMode == ISOC_MODE_YUV422) {
  2589. frameRate = (usbvision->isocPacketSize * 1000) / (UsbWidth * UsbHeight * 2);
  2590. }
  2591. else if (usbvision->isocMode == ISOC_MODE_YUV420) {
  2592. frameRate = (usbvision->isocPacketSize * 1000) / ((UsbWidth * UsbHeight * 12) / 8);
  2593. }
  2594. else {
  2595. frameRate = FRAMERATE_MAX;
  2596. }
  2597. if (usbvision->input.std & V4L2_STD_625_50) {
  2598. frameDrop = frameRate * 32 / 25 - 1;
  2599. }
  2600. else if (usbvision->input.std & V4L2_STD_525_60) {
  2601. frameDrop = frameRate * 32 / 30 - 1;
  2602. }
  2603. RESTRICT_TO_RANGE(frameDrop, FRAMERATE_MIN, FRAMERATE_MAX);
  2604. PDEBUG(DBG_FUNC, "frameRate %d fps, frameDrop %d", frameRate, frameDrop);
  2605. frameDrop = FRAMERATE_MAX; // We can allow the maximum here, because dropping is controlled
  2606. /* frameDrop = 7; => framePhase = 1, 5, 9, 13, 17, 21, 25, 0, 4, 8, ...
  2607. => frameSkip = 4;
  2608. => frameRate = (7 + 1) * 25 / 32 = 200 / 32 = 6.25;
  2609. frameDrop = 9; => framePhase = 1, 5, 8, 11, 14, 17, 21, 24, 27, 1, 4, 8, ...
  2610. => frameSkip = 4, 3, 3, 3, 3, 4, 3, 3, 3, 3, 4, ...
  2611. => frameRate = (9 + 1) * 25 / 32 = 250 / 32 = 7.8125;
  2612. */
  2613. errCode = usbvision_write_reg(usbvision, USBVISION_FRM_RATE, frameDrop);
  2614. return errCode;
  2615. }
  2616. /*
  2617. * usbvision_set_compress_params()
  2618. *
  2619. */
  2620. static int usbvision_set_compress_params(struct usb_usbvision *usbvision)
  2621. {
  2622. static const char proc[] = "usbvision_set_compresion_params: ";
  2623. int rc;
  2624. unsigned char value[6];
  2625. value[0] = 0x0F; // Intra-Compression cycle
  2626. value[1] = 0x01; // Reg.45 one line per strip
  2627. value[2] = 0x00; // Reg.46 Force intra mode on all new frames
  2628. value[3] = 0x00; // Reg.47 FORCE_UP <- 0 normal operation (not force)
  2629. value[4] = 0xA2; // Reg.48 BUF_THR I'm not sure if this does something in not compressed mode.
  2630. value[5] = 0x00; // Reg.49 DVI_YUV This has nothing to do with compression
  2631. //catched values for NT1004
  2632. // value[0] = 0xFF; // Never apply intra mode automatically
  2633. // value[1] = 0xF1; // Use full frame height for virtual strip width; One line per strip
  2634. // value[2] = 0x01; // Force intra mode on all new frames
  2635. // value[3] = 0x00; // Strip size 400 Bytes; do not force up
  2636. // value[4] = 0xA2; //
  2637. if (!USBVISION_IS_OPERATIONAL(usbvision))
  2638. return 0;
  2639. rc = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1),
  2640. USBVISION_OP_CODE,
  2641. USB_DIR_OUT | USB_TYPE_VENDOR |
  2642. USB_RECIP_ENDPOINT, 0,
  2643. (__u16) USBVISION_INTRA_CYC, value, 5, HZ);
  2644. if (rc < 0) {
  2645. printk(KERN_ERR "%sERROR=%d. USBVISION stopped - "
  2646. "reconnect or reload driver.\n", proc, rc);
  2647. return rc;
  2648. }
  2649. if (usbvision->bridgeType == BRIDGE_NT1004) {
  2650. value[0] = 20; // PCM Threshold 1
  2651. value[1] = 12; // PCM Threshold 2
  2652. value[2] = 255; // Distorsion Threshold inter
  2653. value[3] = 255; // Distorsion Threshold intra
  2654. value[4] = 43; // Max Distorsion inter
  2655. value[5] = 43; // Max Distorsion intra
  2656. }
  2657. else {
  2658. value[0] = 20; // PCM Threshold 1
  2659. value[1] = 12; // PCM Threshold 2
  2660. value[2] = 255; // Distorsion Threshold d7-d0
  2661. value[3] = 0; // Distorsion Threshold d11-d8
  2662. value[4] = 43; // Max Distorsion d7-d0
  2663. value[5] = 0; // Max Distorsion d8
  2664. }
  2665. if (!USBVISION_IS_OPERATIONAL(usbvision))
  2666. return 0;
  2667. rc = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1),
  2668. USBVISION_OP_CODE,
  2669. USB_DIR_OUT | USB_TYPE_VENDOR |
  2670. USB_RECIP_ENDPOINT, 0,
  2671. (__u16) USBVISION_PCM_THR1, value, 6, HZ);
  2672. if (rc < 0) {
  2673. printk(KERN_ERR "%sERROR=%d. USBVISION stopped - "
  2674. "reconnect or reload driver.\n", proc, rc);
  2675. return rc;
  2676. }
  2677. return rc;
  2678. }
  2679. /*
  2680. * usbvision_set_input()
  2681. *
  2682. * Set the input (saa711x, ...) size x y and other misc input params
  2683. * I've no idea if this parameters are right
  2684. *
  2685. */
  2686. static int usbvision_set_input(struct usb_usbvision *usbvision)
  2687. {
  2688. static const char proc[] = "usbvision_set_input: ";
  2689. int rc;
  2690. unsigned char value[8];
  2691. unsigned char dvi_yuv_value;
  2692. if (!USBVISION_IS_OPERATIONAL(usbvision))
  2693. return 0;
  2694. /* Set input format expected from decoder*/
  2695. if (usbvision_device_data[usbvision->DevModel].Vin_Reg1 >= 0) {
  2696. value[0] = usbvision_device_data[usbvision->DevModel].Vin_Reg1 & 0xff;
  2697. } else if(usbvision_device_data[usbvision->DevModel].Codec == CODEC_SAA7113) {
  2698. /* SAA7113 uses 8 bit output */
  2699. value[0] = USBVISION_8_422_SYNC;
  2700. } else {
  2701. /* I'm sure only about d2-d0 [010] 16 bit 4:2:2 usin sync pulses
  2702. * as that is how saa7111 is configured */
  2703. value[0] = USBVISION_16_422_SYNC;
  2704. /* | USBVISION_VSNC_POL | USBVISION_VCLK_POL);*/
  2705. }
  2706. rc = usbvision_write_reg(usbvision, USBVISION_VIN_REG1, value[0]);
  2707. if (rc < 0) {
  2708. printk(KERN_ERR "%sERROR=%d. USBVISION stopped - "
  2709. "reconnect or reload driver.\n", proc, rc);
  2710. return rc;
  2711. }
  2712. if (usbvision->input.std & V4L2_STD_PAL) {
  2713. value[0] = 0xC0;
  2714. value[1] = 0x02; //0x02C0 -> 704 Input video line length
  2715. value[2] = 0x20;
  2716. value[3] = 0x01; //0x0120 -> 288 Input video n. of lines
  2717. value[4] = 0x60;
  2718. value[5] = 0x00; //0x0060 -> 96 Input video h offset
  2719. value[6] = 0x16;
  2720. value[7] = 0x00; //0x0016 -> 22 Input video v offset
  2721. } else if (usbvision->input.std & V4L2_STD_SECAM) {
  2722. value[0] = 0xC0;
  2723. value[1] = 0x02; //0x02C0 -> 704 Input video line length
  2724. value[2] = 0x20;
  2725. value[3] = 0x01; //0x0120 -> 288 Input video n. of lines
  2726. value[4] = 0x01;
  2727. value[5] = 0x00; //0x0001 -> 01 Input video h offset
  2728. value[6] = 0x01;
  2729. value[7] = 0x00; //0x0001 -> 01 Input video v offset
  2730. } else { /* V4L2_STD_NTSC */
  2731. value[0] = 0xD0;
  2732. value[1] = 0x02; //0x02D0 -> 720 Input video line length
  2733. value[2] = 0xF0;
  2734. value[3] = 0x00; //0x00F0 -> 240 Input video number of lines
  2735. value[4] = 0x50;
  2736. value[5] = 0x00; //0x0050 -> 80 Input video h offset
  2737. value[6] = 0x10;
  2738. value[7] = 0x00; //0x0010 -> 16 Input video v offset
  2739. }
  2740. if (usbvision_device_data[usbvision->DevModel].X_Offset >= 0) {
  2741. value[4]=usbvision_device_data[usbvision->DevModel].X_Offset & 0xff;
  2742. value[5]=(usbvision_device_data[usbvision->DevModel].X_Offset & 0x0300) >> 8;
  2743. }
  2744. if (usbvision_device_data[usbvision->DevModel].Y_Offset >= 0) {
  2745. value[6]=usbvision_device_data[usbvision->DevModel].Y_Offset & 0xff;
  2746. value[7]=(usbvision_device_data[usbvision->DevModel].Y_Offset & 0x0300) >> 8;
  2747. }
  2748. rc = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1),
  2749. USBVISION_OP_CODE, /* USBVISION specific code */
  2750. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT, 0,
  2751. (__u16) USBVISION_LXSIZE_I, value, 8, HZ);
  2752. if (rc < 0) {
  2753. printk(KERN_ERR "%sERROR=%d. USBVISION stopped - "
  2754. "reconnect or reload driver.\n", proc, rc);
  2755. return rc;
  2756. }
  2757. dvi_yuv_value = 0x00; /* U comes after V, Ya comes after U/V, Yb comes after Yb */
  2758. if(usbvision_device_data[usbvision->DevModel].Dvi_yuv >= 0){
  2759. dvi_yuv_value = usbvision_device_data[usbvision->DevModel].Dvi_yuv & 0xff;
  2760. }
  2761. else if(usbvision_device_data[usbvision->DevModel].Codec == CODEC_SAA7113) {
  2762. /* This changes as the fine sync control changes. Further investigation necessary */
  2763. dvi_yuv_value = 0x06;
  2764. }
  2765. return (usbvision_write_reg(usbvision, USBVISION_DVI_YUV, dvi_yuv_value));
  2766. }
  2767. /*
  2768. * usbvision_set_dram_settings()
  2769. *
  2770. * Set the buffer address needed by the usbvision dram to operate
  2771. * This values has been taken with usbsnoop.
  2772. *
  2773. */
  2774. static int usbvision_set_dram_settings(struct usb_usbvision *usbvision)
  2775. {
  2776. int rc;
  2777. unsigned char value[8];
  2778. if (usbvision->isocMode == ISOC_MODE_COMPRESS) {
  2779. value[0] = 0x42;
  2780. value[1] = 0x71;
  2781. value[2] = 0xff;
  2782. value[3] = 0x00;
  2783. value[4] = 0x98;
  2784. value[5] = 0xe0;
  2785. value[6] = 0x71;
  2786. value[7] = 0xff;
  2787. // UR: 0x0E200-0x3FFFF = 204288 Words (1 Word = 2 Byte)
  2788. // FDL: 0x00000-0x0E099 = 57498 Words
  2789. // VDW: 0x0E3FF-0x3FFFF
  2790. }
  2791. else {
  2792. value[0] = 0x42;
  2793. value[1] = 0x00;
  2794. value[2] = 0xff;
  2795. value[3] = 0x00;
  2796. value[4] = 0x00;
  2797. value[5] = 0x00;
  2798. value[6] = 0x00;
  2799. value[7] = 0xff;
  2800. }
  2801. /* These are the values of the address of the video buffer,
  2802. * they have to be loaded into the USBVISION_DRM_PRM1-8
  2803. *
  2804. * Start address of video output buffer for read: drm_prm1-2 -> 0x00000
  2805. * End address of video output buffer for read: drm_prm1-3 -> 0x1ffff
  2806. * Start address of video frame delay buffer: drm_prm1-4 -> 0x20000
  2807. * Only used in compressed mode
  2808. * End address of video frame delay buffer: drm_prm1-5-6 -> 0x3ffff
  2809. * Only used in compressed mode
  2810. * Start address of video output buffer for write: drm_prm1-7 -> 0x00000
  2811. * End address of video output buffer for write: drm_prm1-8 -> 0x1ffff
  2812. */
  2813. if (!USBVISION_IS_OPERATIONAL(usbvision))
  2814. return 0;
  2815. rc = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1),
  2816. USBVISION_OP_CODE, /* USBVISION specific code */
  2817. USB_DIR_OUT | USB_TYPE_VENDOR |
  2818. USB_RECIP_ENDPOINT, 0,
  2819. (__u16) USBVISION_DRM_PRM1, value, 8, HZ);
  2820. if (rc < 0) {
  2821. err("%sERROR=%d", __FUNCTION__, rc);
  2822. return rc;
  2823. }
  2824. /* Restart the video buffer logic */
  2825. if ((rc = usbvision_write_reg(usbvision, USBVISION_DRM_CONT, USBVISION_RES_UR |
  2826. USBVISION_RES_FDL | USBVISION_RES_VDW)) < 0)
  2827. return rc;
  2828. rc = usbvision_write_reg(usbvision, USBVISION_DRM_CONT, 0x00);
  2829. return rc;
  2830. }
  2831. /*
  2832. * ()
  2833. *
  2834. * Power on the device, enables suspend-resume logic
  2835. * & reset the isoc End-Point
  2836. *
  2837. */
  2838. static int usbvision_power_on(struct usb_usbvision *usbvision)
  2839. {
  2840. int errCode = 0;
  2841. PDEBUG(DBG_FUNC, "");
  2842. usbvision_write_reg(usbvision, USBVISION_PWR_REG, USBVISION_SSPND_EN);
  2843. usbvision_write_reg(usbvision, USBVISION_PWR_REG,
  2844. USBVISION_SSPND_EN | USBVISION_RES2);
  2845. usbvision_write_reg(usbvision, USBVISION_PWR_REG,
  2846. USBVISION_SSPND_EN | USBVISION_PWR_VID);
  2847. errCode = usbvision_write_reg(usbvision, USBVISION_PWR_REG,
  2848. USBVISION_SSPND_EN | USBVISION_PWR_VID | USBVISION_RES2);
  2849. if (errCode == 1) {
  2850. usbvision->power = 1;
  2851. }
  2852. PDEBUG(DBG_FUNC, "%s: errCode %d", (errCode<0)?"ERROR":"power is on", errCode);
  2853. return errCode;
  2854. }
  2855. static void usbvision_powerOffTimer(unsigned long data)
  2856. {
  2857. struct usb_usbvision *usbvision = (void *) data;
  2858. PDEBUG(DBG_FUNC, "");
  2859. del_timer(&usbvision->powerOffTimer);
  2860. INIT_WORK(&usbvision->powerOffWork, call_usbvision_power_off, usbvision);
  2861. (void) schedule_work(&usbvision->powerOffWork);
  2862. }
  2863. /*
  2864. * usbvision_begin_streaming()
  2865. * Sure you have to put bit 7 to 0, if not incoming frames are droped, but no
  2866. * idea about the rest
  2867. */
  2868. static int usbvision_begin_streaming(struct usb_usbvision *usbvision)
  2869. {
  2870. int errCode = 0;
  2871. if (usbvision->isocMode == ISOC_MODE_COMPRESS) {
  2872. usbvision_init_compression(usbvision);
  2873. }
  2874. errCode = usbvision_write_reg(usbvision, USBVISION_VIN_REG2, USBVISION_NOHVALID |
  2875. usbvision->Vin_Reg2_Preset);
  2876. return errCode;
  2877. }
  2878. /*
  2879. * usbvision_restart_isoc()
  2880. * Not sure yet if touching here PWR_REG make loose the config
  2881. */
  2882. static int usbvision_restart_isoc(struct usb_usbvision *usbvision)
  2883. {
  2884. int ret;
  2885. if (
  2886. (ret =
  2887. usbvision_write_reg(usbvision, USBVISION_PWR_REG,
  2888. USBVISION_SSPND_EN | USBVISION_PWR_VID)) < 0)
  2889. return ret;
  2890. if (
  2891. (ret =
  2892. usbvision_write_reg(usbvision, USBVISION_PWR_REG,
  2893. USBVISION_SSPND_EN | USBVISION_PWR_VID |
  2894. USBVISION_RES2)) < 0)
  2895. return ret;
  2896. if (
  2897. (ret =
  2898. usbvision_write_reg(usbvision, USBVISION_VIN_REG2,
  2899. USBVISION_KEEP_BLANK | USBVISION_NOHVALID |
  2900. usbvision->Vin_Reg2_Preset)) < 0) return ret;
  2901. /* TODO: schedule timeout */
  2902. while ((usbvision_read_reg(usbvision, USBVISION_STATUS_REG) && 0x01) != 1);
  2903. return 0;
  2904. }
  2905. static int usbvision_audio_on(struct usb_usbvision *usbvision)
  2906. {
  2907. if (usbvision_write_reg(usbvision, USBVISION_IOPIN_REG, usbvision->AudioChannel) < 0) {
  2908. printk(KERN_ERR "usbvision_audio_on: can't wirte reg\n");
  2909. return -1;
  2910. }
  2911. DEBUG(printk(KERN_DEBUG "usbvision_audio_on: channel %d\n", usbvision->AudioChannel));
  2912. usbvision->AudioMute = 0;
  2913. return 0;
  2914. }
  2915. static int usbvision_audio_mute(struct usb_usbvision *usbvision)
  2916. {
  2917. if (usbvision_write_reg(usbvision, USBVISION_IOPIN_REG, 0x03) < 0) {
  2918. printk(KERN_ERR "usbvision_audio_mute: can't wirte reg\n");
  2919. return -1;
  2920. }
  2921. DEBUG(printk(KERN_DEBUG "usbvision_audio_mute: audio mute\n"));
  2922. usbvision->AudioMute = 1;
  2923. return 0;
  2924. }
  2925. static int usbvision_audio_off(struct usb_usbvision *usbvision)
  2926. {
  2927. if (usbvision_write_reg(usbvision, USBVISION_IOPIN_REG, USBVISION_AUDIO_MUTE) < 0) {
  2928. printk(KERN_ERR "usbvision_audio_off: can't wirte reg\n");
  2929. return -1;
  2930. }
  2931. DEBUG(printk(KERN_DEBUG "usbvision_audio_off: audio off\n"));
  2932. usbvision->AudioMute = 0;
  2933. usbvision->AudioChannel = USBVISION_AUDIO_MUTE;
  2934. return 0;
  2935. }
  2936. static int usbvision_set_audio(struct usb_usbvision *usbvision, int AudioChannel)
  2937. {
  2938. if (!usbvision->AudioMute) {
  2939. if (usbvision_write_reg(usbvision, USBVISION_IOPIN_REG, AudioChannel) < 0) {
  2940. printk(KERN_ERR "usbvision_set_audio: can't write iopin register for audio switching\n");
  2941. return -1;
  2942. }
  2943. }
  2944. DEBUG(printk(KERN_DEBUG "usbvision_set_audio: channel %d\n", AudioChannel));
  2945. usbvision->AudioChannel = AudioChannel;
  2946. return 0;
  2947. }
  2948. static int usbvision_setup(struct usb_usbvision *usbvision)
  2949. {
  2950. usbvision_set_video_format(usbvision, isocMode);
  2951. usbvision_set_dram_settings(usbvision);
  2952. usbvision_set_compress_params(usbvision);
  2953. usbvision_set_input(usbvision);
  2954. usbvision_set_output(usbvision, MAX_USB_WIDTH, MAX_USB_HEIGHT);
  2955. usbvision_restart_isoc(usbvision);
  2956. /* cosas del PCM */
  2957. return USBVISION_IS_OPERATIONAL(usbvision);
  2958. }
  2959. /*
  2960. * usbvision_init_isoc()
  2961. *
  2962. */
  2963. static int usbvision_init_isoc(struct usb_usbvision *usbvision)
  2964. {
  2965. struct usb_device *dev = usbvision->dev;
  2966. int bufIdx, errCode, regValue;
  2967. if (!USBVISION_IS_OPERATIONAL(usbvision))
  2968. return -EFAULT;
  2969. usbvision->curFrameNum = -1;
  2970. scratch_reset(usbvision);
  2971. /* Alternate interface 1 is is the biggest frame size */
  2972. errCode = usb_set_interface(dev, usbvision->iface, usbvision->ifaceAltActive);
  2973. if (errCode < 0) {
  2974. usbvision->last_error = errCode;
  2975. return -EBUSY;
  2976. }
  2977. regValue = (16 - usbvision_read_reg(usbvision, USBVISION_ALTER_REG)) & 0x0F;
  2978. usbvision->isocPacketSize = (regValue == 0) ? 0 : (regValue * 64) - 1;
  2979. PDEBUG(DBG_ISOC, "ISO Packet Length:%d", usbvision->isocPacketSize);
  2980. usbvision->usb_bandwidth = regValue >> 1;
  2981. PDEBUG(DBG_ISOC, "USB Bandwidth Usage: %dMbit/Sec", usbvision->usb_bandwidth);
  2982. /* We double buffer the Iso lists */
  2983. for (bufIdx = 0; bufIdx < USBVISION_NUMSBUF; bufIdx++) {
  2984. int j, k;
  2985. struct urb *urb;
  2986. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
  2987. urb = usb_alloc_urb(USBVISION_URB_FRAMES);
  2988. #else
  2989. urb = usb_alloc_urb(USBVISION_URB_FRAMES, GFP_KERNEL);
  2990. #endif
  2991. if (urb == NULL) {
  2992. err("%s: usb_alloc_urb() failed", __FUNCTION__);
  2993. return -ENOMEM;
  2994. }
  2995. usbvision->sbuf[bufIdx].urb = urb;
  2996. urb->dev = dev;
  2997. urb->context = usbvision;
  2998. urb->pipe = usb_rcvisocpipe(dev, usbvision->video_endp);
  2999. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
  3000. urb->transfer_flags = USB_ISO_ASAP;
  3001. #else
  3002. urb->transfer_flags = URB_ISO_ASAP;
  3003. urb->interval = 1;
  3004. #endif
  3005. urb->transfer_buffer = usbvision->sbuf[bufIdx].data;
  3006. urb->complete = usbvision_isocIrq;
  3007. urb->number_of_packets = USBVISION_URB_FRAMES;
  3008. urb->transfer_buffer_length =
  3009. usbvision->isocPacketSize * USBVISION_URB_FRAMES;
  3010. for (j = k = 0; j < USBVISION_URB_FRAMES; j++,
  3011. k += usbvision->isocPacketSize) {
  3012. urb->iso_frame_desc[j].offset = k;
  3013. urb->iso_frame_desc[j].length = usbvision->isocPacketSize;
  3014. }
  3015. }
  3016. /* Submit all URBs */
  3017. for (bufIdx = 0; bufIdx < USBVISION_NUMSBUF; bufIdx++) {
  3018. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
  3019. errCode = usb_submit_urb(usbvision->sbuf[bufIdx].urb);
  3020. #else
  3021. errCode = usb_submit_urb(usbvision->sbuf[bufIdx].urb, GFP_KERNEL);
  3022. #endif
  3023. if (errCode) {
  3024. err("%s: usb_submit_urb(%d) failed: error %d", __FUNCTION__, bufIdx, errCode);
  3025. }
  3026. }
  3027. usbvision->streaming = 1;
  3028. PDEBUG(DBG_ISOC, "%s: streaming=1 usbvision->video_endp=$%02x", __FUNCTION__, usbvision->video_endp);
  3029. return 0;
  3030. }
  3031. /*
  3032. * usbvision_stop_isoc()
  3033. *
  3034. * This procedure stops streaming and deallocates URBs. Then it
  3035. * activates zero-bandwidth alt. setting of the video interface.
  3036. *
  3037. */
  3038. static void usbvision_stop_isoc(struct usb_usbvision *usbvision)
  3039. {
  3040. int bufIdx, errCode, regValue;
  3041. if (!usbvision->streaming || (usbvision->dev == NULL))
  3042. return;
  3043. /* Unschedule all of the iso td's */
  3044. for (bufIdx = 0; bufIdx < USBVISION_NUMSBUF; bufIdx++) {
  3045. usb_kill_urb(usbvision->sbuf[bufIdx].urb);
  3046. usb_free_urb(usbvision->sbuf[bufIdx].urb);
  3047. usbvision->sbuf[bufIdx].urb = NULL;
  3048. }
  3049. PDEBUG(DBG_ISOC, "%s: streaming=0\n", __FUNCTION__);
  3050. usbvision->streaming = 0;
  3051. if (!usbvision->remove_pending) {
  3052. /* Set packet size to 0 */
  3053. errCode = usb_set_interface(usbvision->dev, usbvision->iface,
  3054. usbvision->ifaceAltInactive);
  3055. if (errCode < 0) {
  3056. err("%s: usb_set_interface() failed: error %d", __FUNCTION__, errCode);
  3057. usbvision->last_error = errCode;
  3058. }
  3059. regValue = (16 - usbvision_read_reg(usbvision, USBVISION_ALTER_REG)) & 0x0F;
  3060. usbvision->isocPacketSize = (regValue == 0) ? 0 : (regValue * 64) - 1;
  3061. PDEBUG(DBG_ISOC, "ISO Packet Length:%d", usbvision->isocPacketSize);
  3062. usbvision->usb_bandwidth = regValue >> 1;
  3063. PDEBUG(DBG_ISOC, "USB Bandwidth Usage: %dMbit/Sec", usbvision->usb_bandwidth);
  3064. }
  3065. }
  3066. /*
  3067. * usbvision_new_frame()
  3068. *
  3069. */
  3070. static int usbvision_new_frame(struct usb_usbvision *usbvision, int framenum)
  3071. {
  3072. struct usbvision_frame *frame;
  3073. int n; //byhec , width, height;
  3074. /* If we're not grabbing a frame right now and the other frame is */
  3075. /* ready to be grabbed into, then use it instead */
  3076. if (usbvision->curFrameNum != -1)
  3077. return 0;
  3078. n = (framenum - 1 + USBVISION_NUMFRAMES) % USBVISION_NUMFRAMES;
  3079. if (usbvision->frame[n].grabstate == FrameState_Ready)
  3080. framenum = n;
  3081. frame = &usbvision->frame[framenum];
  3082. frame->grabstate = FrameState_Grabbing;
  3083. frame->scanstate = ScanState_Scanning;
  3084. frame->scanlength = 0; /* Accumulated in usbvision_parse_data() */
  3085. usbvision->curFrameNum = framenum;
  3086. /*
  3087. * Normally we would want to copy previous frame into the current one
  3088. * before we even start filling it with data; this allows us to stop
  3089. * filling at any moment; top portion of the frame will be new and
  3090. * bottom portion will stay as it was in previous frame. If we don't
  3091. * do that then missing chunks of video stream will result in flickering
  3092. * portions of old data whatever it was before.
  3093. *
  3094. * If we choose not to copy previous frame (to, for example, save few
  3095. * bus cycles - the frame can be pretty large!) then we have an option
  3096. * to clear the frame before using. If we experience losses in this
  3097. * mode then missing picture will be black (flickering).
  3098. *
  3099. * Finally, if user chooses not to clean the current frame before
  3100. * filling it with data then the old data will be visible if we fail
  3101. * to refill entire frame with new data.
  3102. */
  3103. if (!(flags & FLAGS_SEPARATE_FRAMES)) {
  3104. /* This copies previous frame into this one to mask losses */
  3105. memmove(frame->data, usbvision->frame[1 - framenum].data,
  3106. MAX_FRAME_SIZE);
  3107. } else {
  3108. if (flags & FLAGS_CLEAN_FRAMES) {
  3109. /*This provides a "clean" frame but slows things down */
  3110. memset(frame->data, 0, MAX_FRAME_SIZE);
  3111. }
  3112. }
  3113. return 0;
  3114. }
  3115. static int usbvision_muxsel(struct usb_usbvision *usbvision, int channel, int norm)
  3116. {
  3117. int mode[4];
  3118. int audio[]= {1, 0, 0, 0};
  3119. struct v4l2_routing route;
  3120. //channel 0 is TV with audiochannel 1 (tuner mono)
  3121. //channel 1 is Composite with audio channel 0 (line in)
  3122. //channel 2 is S-Video with audio channel 0 (line in)
  3123. //channel 3 is additional video inputs to the device with audio channel 0 (line in)
  3124. RESTRICT_TO_RANGE(channel, 0, usbvision->video_inputs);
  3125. /* set the new video norm */
  3126. if (usbvision->input.std != norm) {
  3127. v4l2_std_id video_command = norm;
  3128. route.input = SAA7115_COMPOSITE1;
  3129. call_i2c_clients(usbvision, VIDIOC_INT_S_VIDEO_ROUTING,&route);
  3130. call_i2c_clients(usbvision, VIDIOC_S_STD, &video_command);
  3131. usbvision->input.std = norm;
  3132. call_i2c_clients(usbvision, VIDIOC_S_INPUT, &usbvision->input.index); //set norm in tuner
  3133. }
  3134. // set the new channel
  3135. // Regular USB TV Tuners -> channel: 0 = Television, 1 = Composite, 2 = S-Video
  3136. // Four video input devices -> channel: 0 = Chan White, 1 = Chan Green, 2 = Chan Yellow, 3 = Chan Red
  3137. switch (usbvision_device_data[usbvision->DevModel].Codec) {
  3138. case CODEC_SAA7113:
  3139. if (SwitchSVideoInput) { // To handle problems with S-Video Input for some devices. Use SwitchSVideoInput parameter when loading the module.
  3140. mode[2] = 1;
  3141. }
  3142. else {
  3143. mode[2] = 7;
  3144. }
  3145. if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
  3146. mode[0] = 0; mode[1] = 2; mode[3] = 3; // Special for four input devices
  3147. }
  3148. else {
  3149. mode[0] = 0; mode[1] = 2; //modes for regular saa7113 devices
  3150. }
  3151. break;
  3152. case CODEC_SAA7111:
  3153. mode[0] = 0; mode[1] = 1; mode[2] = 7; //modes for saa7111
  3154. break;
  3155. default:
  3156. mode[0] = 0; mode[1] = 1; mode[2] = 7; //default modes
  3157. }
  3158. route.input = mode[channel];
  3159. call_i2c_clients(usbvision, VIDIOC_INT_S_VIDEO_ROUTING,&route);
  3160. usbvision->channel = channel;
  3161. usbvision_set_audio(usbvision, audio[channel]);
  3162. return 0;
  3163. }
  3164. /*
  3165. * usbvision_open()
  3166. *
  3167. * This is part of Video 4 Linux API. The driver can be opened by one
  3168. * client only (checks internal counter 'usbvision->user'). The procedure
  3169. * then allocates buffers needed for video processing.
  3170. *
  3171. */
  3172. static int usbvision_v4l2_open(struct inode *inode, struct file *file)
  3173. {
  3174. struct video_device *dev = video_devdata(file);
  3175. struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
  3176. const int sb_size = USBVISION_URB_FRAMES * USBVISION_MAX_ISOC_PACKET_SIZE;
  3177. int i, errCode = 0;
  3178. PDEBUG(DBG_IO, "open");
  3179. if (timer_pending(&usbvision->powerOffTimer)) {
  3180. del_timer(&usbvision->powerOffTimer);
  3181. }
  3182. if (usbvision->user)
  3183. errCode = -EBUSY;
  3184. else {
  3185. /* Clean pointers so we know if we allocated something */
  3186. for (i = 0; i < USBVISION_NUMSBUF; i++)
  3187. usbvision->sbuf[i].data = NULL;
  3188. /* Allocate memory for the frame buffers */
  3189. usbvision->max_frame_size = MAX_FRAME_SIZE;
  3190. usbvision->fbuf_size = USBVISION_NUMFRAMES * usbvision->max_frame_size;
  3191. usbvision->fbuf = usbvision_rvmalloc(usbvision->fbuf_size);
  3192. usbvision->scratch = vmalloc(scratch_buf_size);
  3193. scratch_reset(usbvision);
  3194. if ((usbvision->fbuf == NULL) || (usbvision->scratch == NULL)) {
  3195. err("%s: unable to allocate %d bytes for fbuf and %d bytes for scratch",
  3196. __FUNCTION__, usbvision->fbuf_size, scratch_buf_size);
  3197. errCode = -ENOMEM;
  3198. }
  3199. else {
  3200. /* Allocate all buffers */
  3201. for (i = 0; i < USBVISION_NUMFRAMES; i++) {
  3202. usbvision->frame[i].grabstate = FrameState_Unused;
  3203. usbvision->frame[i].data = usbvision->fbuf +
  3204. i * MAX_FRAME_SIZE;
  3205. /*
  3206. * Set default sizes in case IOCTL
  3207. * (VIDIOCMCAPTURE)
  3208. * is not used (using read() instead).
  3209. */
  3210. usbvision->stretch_width = 1;
  3211. usbvision->stretch_height = 1;
  3212. usbvision->frame[i].width = usbvision->curwidth;
  3213. usbvision->frame[i].height = usbvision->curheight;
  3214. usbvision->frame[i].bytes_read = 0;
  3215. }
  3216. if (dga) { //set default for DGA
  3217. usbvision->overlay_frame.grabstate = FrameState_Unused;
  3218. usbvision->overlay_frame.scanstate = ScanState_Scanning;
  3219. usbvision->overlay_frame.data = NULL;
  3220. usbvision->overlay_frame.width = usbvision->curwidth;
  3221. usbvision->overlay_frame.height = usbvision->curheight;
  3222. usbvision->overlay_frame.bytes_read = 0;
  3223. }
  3224. for (i = 0; i < USBVISION_NUMSBUF; i++) {
  3225. usbvision->sbuf[i].data = kzalloc(sb_size, GFP_KERNEL);
  3226. if (usbvision->sbuf[i].data == NULL) {
  3227. err("%s: unable to allocate %d bytes for sbuf", __FUNCTION__, sb_size);
  3228. errCode = -ENOMEM;
  3229. break;
  3230. }
  3231. }
  3232. }
  3233. if ((!errCode) && (usbvision->isocMode==ISOC_MODE_COMPRESS)) {
  3234. int IFB_size = MAX_FRAME_WIDTH * MAX_FRAME_HEIGHT * 3 / 2;
  3235. usbvision->IntraFrameBuffer = vmalloc(IFB_size);
  3236. if (usbvision->IntraFrameBuffer == NULL) {
  3237. err("%s: unable to allocate %d for compr. frame buffer", __FUNCTION__, IFB_size);
  3238. errCode = -ENOMEM;
  3239. }
  3240. }
  3241. if (errCode) {
  3242. /* Have to free all that memory */
  3243. if (usbvision->fbuf != NULL) {
  3244. usbvision_rvfree(usbvision->fbuf, usbvision->fbuf_size);
  3245. usbvision->fbuf = NULL;
  3246. }
  3247. if (usbvision->scratch != NULL) {
  3248. vfree(usbvision->scratch);
  3249. usbvision->scratch = NULL;
  3250. }
  3251. for (i = 0; i < USBVISION_NUMSBUF; i++) {
  3252. if (usbvision->sbuf[i].data != NULL) {
  3253. kfree(usbvision->sbuf[i].data);
  3254. usbvision->sbuf[i].data = NULL;
  3255. }
  3256. }
  3257. if (usbvision->IntraFrameBuffer != NULL) {
  3258. vfree(usbvision->IntraFrameBuffer);
  3259. usbvision->IntraFrameBuffer = NULL;
  3260. }
  3261. }
  3262. }
  3263. /* If so far no errors then we shall start the camera */
  3264. if (!errCode) {
  3265. down(&usbvision->lock);
  3266. if (usbvision->power == 0) {
  3267. usbvision_power_on(usbvision);
  3268. usbvision_init_i2c(usbvision);
  3269. }
  3270. /* Send init sequence only once, it's large! */
  3271. if (!usbvision->initialized) {
  3272. int setup_ok = 0;
  3273. setup_ok = usbvision_setup(usbvision);
  3274. if (setup_ok)
  3275. usbvision->initialized = 1;
  3276. else
  3277. errCode = -EBUSY;
  3278. }
  3279. if (!errCode) {
  3280. usbvision_begin_streaming(usbvision);
  3281. errCode = usbvision_init_isoc(usbvision);
  3282. usbvision->user++;
  3283. }
  3284. else {
  3285. if (PowerOnAtOpen) {
  3286. usbvision_i2c_usb_del_bus(&usbvision->i2c_adap);
  3287. usbvision_power_off(usbvision);
  3288. usbvision->initialized = 0;
  3289. }
  3290. }
  3291. up(&usbvision->lock);
  3292. }
  3293. if (errCode) {
  3294. }
  3295. PDEBUG(DBG_IO, "success");
  3296. return errCode;
  3297. }
  3298. /*
  3299. * usbvision_v4l2_close()
  3300. *
  3301. * This is part of Video 4 Linux API. The procedure
  3302. * stops streaming and deallocates all buffers that were earlier
  3303. * allocated in usbvision_v4l2_open().
  3304. *
  3305. */
  3306. static int usbvision_v4l2_close(struct inode *inode, struct file *file)
  3307. {
  3308. struct video_device *dev = video_devdata(file);
  3309. struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
  3310. int i;
  3311. PDEBUG(DBG_IO, "close");
  3312. down(&usbvision->lock);
  3313. usbvision_audio_off(usbvision);
  3314. usbvision_restart_isoc(usbvision);
  3315. usbvision_stop_isoc(usbvision);
  3316. if (usbvision->IntraFrameBuffer != NULL) {
  3317. vfree(usbvision->IntraFrameBuffer);
  3318. usbvision->IntraFrameBuffer = NULL;
  3319. }
  3320. usbvision_rvfree(usbvision->fbuf, usbvision->fbuf_size);
  3321. vfree(usbvision->scratch);
  3322. for (i = 0; i < USBVISION_NUMSBUF; i++)
  3323. kfree(usbvision->sbuf[i].data);
  3324. usbvision->user--;
  3325. if (PowerOnAtOpen) {
  3326. mod_timer(&usbvision->powerOffTimer, jiffies + USBVISION_POWEROFF_TIME);
  3327. usbvision->initialized = 0;
  3328. }
  3329. up(&usbvision->lock);
  3330. if (usbvision->remove_pending) {
  3331. info("%s: Final disconnect", __FUNCTION__);
  3332. usbvision_release(usbvision);
  3333. }
  3334. PDEBUG(DBG_IO, "success");
  3335. return 0;
  3336. }
  3337. /*
  3338. * usbvision_ioctl()
  3339. *
  3340. * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
  3341. *
  3342. */
  3343. static int usbvision_v4l2_do_ioctl(struct inode *inode, struct file *file,
  3344. unsigned int cmd, void *arg)
  3345. {
  3346. struct video_device *dev = video_devdata(file);
  3347. struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
  3348. if (!USBVISION_IS_OPERATIONAL(usbvision))
  3349. return -EFAULT;
  3350. switch (cmd) {
  3351. case UVIOCSREG:
  3352. {
  3353. struct usbvision_reg *usbvision_reg = arg;
  3354. int errCode;
  3355. errCode = usbvision_write_reg(usbvision, usbvision_reg->addr, usbvision_reg->value);
  3356. if (errCode < 0) {
  3357. err("%s: UVIOCSREG failed: error %d", __FUNCTION__, errCode);
  3358. }
  3359. else {
  3360. PDEBUG(DBG_IOCTL, "UVIOCSREG addr=0x%02X, value=0x%02X",
  3361. usbvision_reg->addr, usbvision_reg->value);
  3362. errCode = 0;
  3363. }
  3364. return errCode;
  3365. }
  3366. case UVIOCGREG:
  3367. {
  3368. struct usbvision_reg *usbvision_reg = arg;
  3369. int errCode;
  3370. errCode = usbvision_read_reg(usbvision, usbvision_reg->addr);
  3371. if (errCode < 0) {
  3372. err("%s: UVIOCGREG failed: error %d", __FUNCTION__, errCode);
  3373. }
  3374. else {
  3375. usbvision_reg->value=(unsigned char)errCode;
  3376. PDEBUG(DBG_IOCTL, "UVIOCGREG addr=0x%02X, value=0x%02X",
  3377. usbvision_reg->addr, usbvision_reg->value);
  3378. errCode = 0; // No error
  3379. }
  3380. return errCode;
  3381. }
  3382. case VIDIOC_QUERYCAP:
  3383. {
  3384. struct v4l2_capability *vc=arg;
  3385. *vc = usbvision->vcap;
  3386. PDEBUG(DBG_IOCTL, "VIDIOC_QUERYCAP");
  3387. return 0;
  3388. }
  3389. case VIDIOC_ENUMINPUT:
  3390. {
  3391. struct v4l2_input *vi = arg;
  3392. int chan;
  3393. if ((vi->index >= usbvision->video_inputs) || (vi->index < 0) )
  3394. return -EINVAL;
  3395. if (usbvision->have_tuner) {
  3396. chan = vi->index;
  3397. }
  3398. else {
  3399. chan = vi->index + 1; //skip Television string
  3400. }
  3401. switch(chan) {
  3402. case 0:
  3403. if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
  3404. strcpy(vi->name, "White Video Input");
  3405. }
  3406. else {
  3407. strcpy(vi->name, "Television");
  3408. vi->type = V4L2_INPUT_TYPE_TUNER;
  3409. vi->audioset = 1;
  3410. vi->tuner = chan;
  3411. vi->std = V4L2_STD_PAL | V4L2_STD_NTSC | V4L2_STD_SECAM;
  3412. }
  3413. break;
  3414. case 1:
  3415. vi->type = V4L2_INPUT_TYPE_CAMERA;
  3416. if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
  3417. strcpy(vi->name, "Green Video Input");
  3418. }
  3419. else {
  3420. strcpy(vi->name, "Composite Video Input");
  3421. }
  3422. vi->std = V4L2_STD_PAL;
  3423. break;
  3424. case 2:
  3425. vi->type = V4L2_INPUT_TYPE_CAMERA;
  3426. if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
  3427. strcpy(vi->name, "Yellow Video Input");
  3428. }
  3429. else {
  3430. strcpy(vi->name, "S-Video Input");
  3431. }
  3432. vi->std = V4L2_STD_PAL;
  3433. break;
  3434. case 3:
  3435. vi->type = V4L2_INPUT_TYPE_CAMERA;
  3436. strcpy(vi->name, "Red Video Input");
  3437. vi->std = V4L2_STD_PAL;
  3438. break;
  3439. }
  3440. PDEBUG(DBG_IOCTL, "VIDIOC_ENUMINPUT name=%s:%d tuners=%d type=%d norm=%x", vi->name, vi->index, vi->tuner,vi->type,(int)vi->std);
  3441. return 0;
  3442. }
  3443. case VIDIOC_ENUMSTD:
  3444. {
  3445. struct v4l2_standard *vs = arg;
  3446. switch(vs->index) {
  3447. case 0:
  3448. vs->id = V4L2_STD_PAL;
  3449. strcpy(vs->name,"PAL");
  3450. vs->frameperiod.numerator = 1;
  3451. vs->frameperiod.denominator = 25;
  3452. vs->framelines = 625;
  3453. break;
  3454. case 1:
  3455. vs->id = V4L2_STD_NTSC;
  3456. strcpy(vs->name,"NTSC");
  3457. vs->frameperiod.numerator = 1001;
  3458. vs->frameperiod.denominator = 30000;
  3459. vs->framelines = 525;
  3460. break;
  3461. case 2:
  3462. vs->id = V4L2_STD_SECAM;
  3463. strcpy(vs->name,"SECAM");
  3464. vs->frameperiod.numerator = 1;
  3465. vs->frameperiod.denominator = 25;
  3466. vs->framelines = 625;
  3467. break;
  3468. default:
  3469. return -EINVAL;
  3470. }
  3471. return 0;
  3472. }
  3473. case VIDIOC_G_INPUT:
  3474. {
  3475. int *input = arg;
  3476. *input = usbvision->input.index;
  3477. return 0;
  3478. }
  3479. case VIDIOC_S_INPUT:
  3480. {
  3481. int *input = arg;
  3482. if ((*input >= usbvision->video_inputs) || (*input < 0) )
  3483. return -EINVAL;
  3484. usbvision->input.index = *input;
  3485. down(&usbvision->lock);
  3486. usbvision_muxsel(usbvision, usbvision->input.index, usbvision->input.std);
  3487. usbvision_set_input(usbvision);
  3488. usbvision_set_output(usbvision, usbvision->curwidth, usbvision->curheight);
  3489. up(&usbvision->lock);
  3490. return 0;
  3491. }
  3492. case VIDIOC_G_STD:
  3493. {
  3494. v4l2_std_id *std = arg;
  3495. *std = usbvision->input.std;
  3496. PDEBUG(DBG_IOCTL, "VIDIOC_G_STD std_id=%x", (unsigned)*std);
  3497. return 0;
  3498. }
  3499. case VIDIOC_S_STD:
  3500. {
  3501. v4l2_std_id *std = arg;
  3502. down(&usbvision->lock);
  3503. usbvision_muxsel(usbvision, usbvision->input.index, *std);
  3504. usbvision_set_input(usbvision);
  3505. usbvision_set_output(usbvision, usbvision->curwidth, usbvision->curheight);
  3506. up(&usbvision->lock);
  3507. usbvision->input.std = *std;
  3508. PDEBUG(DBG_IOCTL, "VIDIOC_S_STD std_id=%x", (unsigned)*std);
  3509. return 0;
  3510. }
  3511. case VIDIOC_G_TUNER:
  3512. {
  3513. struct v4l2_tuner *vt = arg;
  3514. struct v4l2_tuner status;
  3515. if (!usbvision->have_tuner || vt->index) // Only tuner 0
  3516. return -EINVAL;
  3517. strcpy(vt->name, "Television");
  3518. vt->type = V4L2_TUNER_ANALOG_TV;
  3519. vt->capability = V4L2_TUNER_CAP_NORM;
  3520. vt->rangelow = 0;
  3521. vt->rangehigh = ~0;
  3522. vt->audmode = V4L2_TUNER_MODE_MONO;
  3523. vt->rxsubchans = V4L2_TUNER_SUB_MONO;
  3524. call_i2c_clients(usbvision,VIDIOC_G_TUNER,&status);
  3525. vt->signal = status.signal;
  3526. PDEBUG(DBG_IOCTL, "VIDIOC_G_TUNER");
  3527. return 0;
  3528. }
  3529. case VIDIOC_S_TUNER:
  3530. {
  3531. struct v4l2_tuner *vt = arg;
  3532. // Only no or one tuner for now
  3533. if (!usbvision->have_tuner || vt->index)
  3534. return -EINVAL;
  3535. // FIXME vt->audmode Radio mode (STEREO/MONO/...)
  3536. // vt->reserved Radio freq
  3537. // usbvision_muxsel(usbvision, vt->index, vt->mode);
  3538. PDEBUG(DBG_IOCTL, "VIDIOC_S_TUNER");
  3539. return 0;
  3540. }
  3541. case VIDIOC_G_FREQUENCY:
  3542. {
  3543. struct v4l2_frequency *freq = arg;
  3544. freq->tuner = 0; // Only one tuner
  3545. freq->type = V4L2_TUNER_ANALOG_TV;
  3546. freq->frequency = usbvision->freq;
  3547. PDEBUG(DBG_IOCTL, "VIDIOC_G_FREQUENCY freq=0x%X", (unsigned)freq->frequency);
  3548. return 0;
  3549. }
  3550. case VIDIOC_S_FREQUENCY:
  3551. {
  3552. struct v4l2_frequency *freq = arg;
  3553. usbvision->freq = freq->frequency;
  3554. call_i2c_clients(usbvision, cmd, freq);
  3555. PDEBUG(DBG_IOCTL, "VIDIOC_S_FREQUENCY freq=0x%X", (unsigned)freq->frequency);
  3556. return 0;
  3557. }
  3558. case VIDIOC_G_AUDIO:
  3559. {
  3560. struct v4l2_audio *v = arg;
  3561. memset(v,0, sizeof(v));
  3562. strcpy(v->name, "TV");
  3563. PDEBUG(DBG_IOCTL, "VIDIOC_G_AUDIO");
  3564. // FIXME: no more processings ???
  3565. return 0;
  3566. }
  3567. case VIDIOC_S_AUDIO:
  3568. {
  3569. struct v4l2_audio *v = arg;
  3570. if(v->index) {
  3571. return -EINVAL;
  3572. }
  3573. PDEBUG(DBG_IOCTL, "VIDIOC_S_AUDIO");
  3574. // FIXME: void function ???
  3575. return 0;
  3576. }
  3577. case VIDIOC_QUERYCTRL:
  3578. {
  3579. struct v4l2_queryctrl *qc = arg;
  3580. switch(qc->id) {
  3581. case V4L2_CID_BRIGHTNESS:
  3582. case V4L2_CID_HUE:
  3583. case V4L2_CID_SATURATION:
  3584. case V4L2_CID_CONTRAST:
  3585. case V4L2_CID_AUDIO_VOLUME:
  3586. case V4L2_CID_AUDIO_MUTE:
  3587. return v4l2_ctrl_query_fill_std(qc);
  3588. break;
  3589. default:
  3590. return -EINVAL;
  3591. }
  3592. return 0;
  3593. }
  3594. case VIDIOC_G_CTRL:
  3595. {
  3596. struct v4l2_control *ctrl = arg;
  3597. switch (ctrl->id) {
  3598. case V4L2_CID_BRIGHTNESS:
  3599. ctrl->value = usbvision->brightness;
  3600. break;
  3601. case V4L2_CID_CONTRAST:
  3602. ctrl->value = usbvision->contrast;
  3603. break;
  3604. case V4L2_CID_SATURATION:
  3605. ctrl->value = usbvision->saturation;
  3606. break;
  3607. case V4L2_CID_HUE:
  3608. ctrl->value = usbvision->hue;
  3609. break;
  3610. case V4L2_CID_AUDIO_VOLUME:
  3611. /* ctrl->value = usbvision->volume; */
  3612. break;
  3613. case V4L2_CID_AUDIO_MUTE:
  3614. ctrl->value = usbvision->AudioMute;
  3615. break;
  3616. default:
  3617. return -EINVAL;
  3618. }
  3619. PDEBUG(DBG_IOCTL, "VIDIOC_G_CTRL id=%x value=%x",ctrl->id,ctrl->value);
  3620. return 0;
  3621. }
  3622. case VIDIOC_S_CTRL:
  3623. {
  3624. struct v4l2_control *ctrl = arg;
  3625. PDEBUG(DBG_IOCTL, "VIDIOC_S_CTRL id=%x value=%x",ctrl->id,ctrl->value);
  3626. call_i2c_clients(usbvision, VIDIOC_S_CTRL, ctrl);
  3627. return 0;
  3628. }
  3629. case VIDIOC_REQBUFS:
  3630. {
  3631. struct v4l2_requestbuffers *vr = arg;
  3632. // FIXME : normally we allocate the requested number of buffers.
  3633. // this driver allocates statically the buffers.
  3634. vr->count = 2;
  3635. if(vr->memory != V4L2_MEMORY_MMAP)
  3636. return -EINVAL;
  3637. return 0;
  3638. }
  3639. case VIDIOC_QUERYBUF:
  3640. {
  3641. struct v4l2_buffer *vb = arg;
  3642. struct usbvision_frame *frame;
  3643. // FIXME : works only on VIDEO_CAPTURE MODE, MMAP.
  3644. if(vb->type != V4L2_CAP_VIDEO_CAPTURE) {
  3645. return -EINVAL;
  3646. }
  3647. if(vb->index>1) {
  3648. return -EINVAL;
  3649. }
  3650. vb->flags = 0;
  3651. frame = &usbvision->frame[vb->index];
  3652. if(frame->grabstate == FrameState_Grabbing)
  3653. vb->flags |= V4L2_BUF_FLAG_QUEUED;
  3654. if(frame->grabstate >= FrameState_Done)
  3655. vb->flags |= V4L2_BUF_FLAG_DONE;
  3656. if(frame->grabstate == FrameState_Unused)
  3657. vb->flags |= V4L2_BUF_FLAG_MAPPED;
  3658. vb->memory = V4L2_MEMORY_MMAP;
  3659. if(vb->index == 0) {
  3660. vb->m.offset = 0;
  3661. }
  3662. else {
  3663. vb->m.offset = MAX_FRAME_SIZE;
  3664. }
  3665. vb->length = MAX_FRAME_SIZE;
  3666. vb->timestamp = usbvision->frame[vb->index].timestamp;
  3667. vb->sequence = usbvision->frame_num;
  3668. return 0;
  3669. }
  3670. case VIDIOC_QBUF: // VIDIOCMCAPTURE + VIDIOCSYNC
  3671. {
  3672. struct v4l2_buffer *vb = arg;
  3673. struct usbvision_frame *frame;
  3674. // FIXME : works only on VIDEO_CAPTURE MODE, MMAP.
  3675. if(vb->type != V4L2_CAP_VIDEO_CAPTURE) {
  3676. return -EINVAL;
  3677. }
  3678. if(vb->index>1) {
  3679. return -EINVAL;
  3680. }
  3681. frame = &usbvision->frame[vb->index];
  3682. if (frame->grabstate == FrameState_Grabbing) {
  3683. return -EBUSY;
  3684. }
  3685. usbvision_set_output(usbvision, usbvision->curwidth, usbvision->curheight);
  3686. /* Mark it as ready */
  3687. frame->grabstate = FrameState_Ready;
  3688. vb->flags &= ~V4L2_BUF_FLAG_DONE;
  3689. /* set v4l2_format index */
  3690. frame->v4l2_format = usbvision->palette;
  3691. return usbvision_new_frame(usbvision, vb->index);
  3692. }
  3693. case VIDIOC_DQBUF:
  3694. {
  3695. struct v4l2_buffer *vb = arg;
  3696. int errCode = 0;
  3697. DECLARE_WAITQUEUE(wait, current);
  3698. // FIXME : not the proper way to get the last filled frame
  3699. vb->index=-1;
  3700. if(usbvision->curFrameNum != -1) vb->index=usbvision->curFrameNum;
  3701. else {
  3702. if(usbvision->frame[1].grabstate >= FrameState_Done)
  3703. vb->index = 1;
  3704. else if(usbvision->frame[0].grabstate >= FrameState_Done)
  3705. vb->index = 0;
  3706. // If no FRAME_DONE, look for a FRAME_GRABBING state.
  3707. // See if a frame is in process (grabbing), then use it.
  3708. if (vb->index == -1) {
  3709. if (usbvision->frame[1].grabstate == FrameState_Grabbing)
  3710. vb->index = 1;
  3711. else if (usbvision->frame[0].grabstate == FrameState_Grabbing)
  3712. vb->index = 0;
  3713. }
  3714. }
  3715. if (vb->index == -1)
  3716. return -EINVAL;
  3717. PDEBUG(DBG_IOCTL, "VIDIOC_DQBUF frame=%d, grabstate=%d",
  3718. vb->index, usbvision->frame[vb->index].grabstate);
  3719. switch (usbvision->frame[vb->index].grabstate) {
  3720. case FrameState_Unused:
  3721. errCode = -EINVAL;
  3722. break;
  3723. case FrameState_Grabbing:
  3724. add_wait_queue(&usbvision->frame[vb->index].wq, &wait);
  3725. current->state = TASK_INTERRUPTIBLE;
  3726. while (usbvision->frame[vb->index].grabstate == FrameState_Grabbing) {
  3727. schedule();
  3728. if (signal_pending(current)) {
  3729. remove_wait_queue(&usbvision->frame[vb->index].wq, &wait);
  3730. current->state = TASK_RUNNING;
  3731. return -EINTR;
  3732. }
  3733. }
  3734. remove_wait_queue(&usbvision->frame[vb->index].wq, &wait);
  3735. current->state = TASK_RUNNING;
  3736. case FrameState_Ready:
  3737. case FrameState_Error:
  3738. case FrameState_Done:
  3739. errCode = (usbvision->frame[vb->index].grabstate == FrameState_Error) ? -EIO : 0;
  3740. vb->memory = V4L2_MEMORY_MMAP;
  3741. vb->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE;
  3742. vb->sequence = usbvision->frame[vb->index].sequence;
  3743. usbvision->frame[vb->index].grabstate = FrameState_Unused;
  3744. break;
  3745. }
  3746. usbvision->frame[vb->index].grabstate = FrameState_Unused;
  3747. return errCode;
  3748. }
  3749. case VIDIOC_STREAMON:
  3750. {
  3751. int b=V4L2_BUF_TYPE_VIDEO_CAPTURE;
  3752. call_i2c_clients(usbvision,VIDIOC_STREAMON , &b);
  3753. return 0;
  3754. }
  3755. case VIDIOC_STREAMOFF:
  3756. {
  3757. int b=V4L2_BUF_TYPE_VIDEO_CAPTURE;
  3758. down(&usbvision->lock);
  3759. // Stop all video streamings
  3760. call_i2c_clients(usbvision,VIDIOC_STREAMOFF , &b);
  3761. usbvision->frame_num = -1;
  3762. usbvision->frame[0].grabstate = FrameState_Unused;
  3763. usbvision->frame[1].grabstate = FrameState_Unused;
  3764. up(&usbvision->lock);
  3765. return 0;
  3766. }
  3767. case VIDIOC_G_FBUF:
  3768. {
  3769. struct v4l2_framebuffer *vb = arg;
  3770. if (dga) {
  3771. *vb = usbvision->vid_buf;
  3772. }
  3773. else {
  3774. memset(vb, 0, sizeof(vb)); //dga not supported, not used
  3775. }
  3776. PDEBUG(DBG_IOCTL, "VIDIOC_G_FBUF base=%p, width=%d, height=%d, pixelformat=%d, bpl=%d",
  3777. vb->base, vb->fmt.width, vb->fmt.height, vb->fmt.pixelformat,vb->fmt.bytesperline);
  3778. return 0;
  3779. }
  3780. case VIDIOC_S_FBUF:
  3781. {
  3782. struct v4l2_framebuffer *vb = arg;
  3783. int formatIdx;
  3784. if (dga == 0) {
  3785. return -EINVAL;
  3786. }
  3787. if(!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_ADMIN)) {
  3788. return -EPERM;
  3789. }
  3790. PDEBUG(DBG_IOCTL, "VIDIOC_S_FBUF base=%p, width=%d, height=%d, pixelformat=%d, bpl=%d",
  3791. vb->base, vb->fmt.width, vb->fmt.height, vb->fmt.pixelformat,vb->fmt.bytesperline);
  3792. for (formatIdx=0; formatIdx <= USBVISION_SUPPORTED_PALETTES; formatIdx++) {
  3793. if (formatIdx == USBVISION_SUPPORTED_PALETTES) {
  3794. return -EINVAL; // no matching video_format
  3795. }
  3796. if ((vb->fmt.pixelformat == usbvision_v4l2_format[formatIdx].format) &&
  3797. (usbvision_v4l2_format[formatIdx].supported)) {
  3798. break; //found matching video_format
  3799. }
  3800. }
  3801. if (vb->fmt.bytesperline<1) {
  3802. return -EINVAL;
  3803. }
  3804. if (usbvision->overlay) {
  3805. return -EBUSY;
  3806. }
  3807. down(&usbvision->lock);
  3808. if (usbvision->overlay_base) {
  3809. iounmap(usbvision->overlay_base);
  3810. usbvision->vid_buf_valid = 0;
  3811. }
  3812. usbvision->overlay_base = ioremap((ulong)vb->base, vb->fmt.height * vb->fmt.bytesperline);
  3813. if (usbvision->overlay_base) {
  3814. usbvision->vid_buf_valid = 1;
  3815. }
  3816. usbvision->vid_buf = *vb;
  3817. usbvision->overlay_frame.v4l2_format = usbvision_v4l2_format[formatIdx];
  3818. up(&usbvision->lock);
  3819. return 0;
  3820. }
  3821. case VIDIOC_ENUM_FMT:
  3822. {
  3823. struct v4l2_fmtdesc *vfd = arg;
  3824. if ( (dga == 0) &&
  3825. (vfd->type == V4L2_BUF_TYPE_VIDEO_OVERLAY) &&
  3826. (usbvision->palette.format != V4L2_PIX_FMT_YVU420) &&
  3827. (usbvision->palette.format != V4L2_PIX_FMT_YUV422P) ) {
  3828. return -EINVAL;
  3829. }
  3830. if(vfd->index>=USBVISION_SUPPORTED_PALETTES-1) {
  3831. return -EINVAL;
  3832. }
  3833. vfd->flags = 0;
  3834. strcpy(vfd->description,usbvision_v4l2_format[vfd->index].desc);
  3835. vfd->pixelformat = usbvision_v4l2_format[vfd->index].format;
  3836. return 0;
  3837. }
  3838. case VIDIOC_G_FMT:
  3839. {
  3840. struct v4l2_format *vf = arg;
  3841. if ( (dga == 0) &&
  3842. (vf->type == V4L2_BUF_TYPE_VIDEO_OVERLAY) &&
  3843. (usbvision->palette.format != V4L2_PIX_FMT_YVU420) &&
  3844. (usbvision->palette.format != V4L2_PIX_FMT_YUV422P) ) {
  3845. return -EINVAL;
  3846. }
  3847. down(&usbvision->lock);
  3848. *vf = usbvision->vid_win;
  3849. up(&usbvision->lock);
  3850. PDEBUG(DBG_IOCTL, "VIDIOC_G_FMT x=%d, y=%d, w=%d, h=%d, chroma=%x, clips=%d",
  3851. vf->fmt.win.w.left, vf->fmt.win.w.top, vf->fmt.win.w.width, vf->fmt.win.w.height, vf->fmt.win.chromakey, vf->fmt.win.clipcount);
  3852. return 0;
  3853. }
  3854. case VIDIOC_S_FMT:
  3855. {
  3856. struct v4l2_format *vf = arg;
  3857. struct v4l2_clip *vc=NULL;
  3858. int on,formatIdx;
  3859. if ( (dga == 0) &&
  3860. (vf->type == V4L2_BUF_TYPE_VIDEO_OVERLAY) &&
  3861. (usbvision->palette.format != V4L2_PIX_FMT_YVU420) &&
  3862. (usbvision->palette.format != V4L2_PIX_FMT_YUV422P) ) {
  3863. return -EINVAL;
  3864. }
  3865. if(vf->type == V4L2_BUF_TYPE_VIDEO_OVERLAY) {
  3866. if (vf->fmt.win.clipcount>256) {
  3867. return -EDOM; /* Too many clips! */
  3868. }
  3869. // Do every clips.
  3870. vc = vmalloc(sizeof(struct v4l2_clip)*(vf->fmt.win.clipcount+4));
  3871. if (vc == NULL) {
  3872. return -ENOMEM;
  3873. }
  3874. if (vf->fmt.win.clipcount && copy_from_user(vc,vf->fmt.win.clips,sizeof(struct v4l2_clip)*vf->fmt.win.clipcount)) {
  3875. return -EFAULT;
  3876. }
  3877. on = usbvision->overlay; // Save overlay state
  3878. if (on) {
  3879. usbvision_cap(usbvision, 0);
  3880. }
  3881. // strange, it seems xawtv sometimes calls us with 0
  3882. // width and/or height. Ignore these values
  3883. if (vf->fmt.win.w.left == 0) {
  3884. vf->fmt.win.w.left = usbvision->vid_win.fmt.win.w.left;
  3885. }
  3886. if (vf->fmt.win.w.top == 0) {
  3887. vf->fmt.win.w.top = usbvision->vid_win.fmt.win.w.top;
  3888. }
  3889. // by now we are committed to the new data...
  3890. down(&usbvision->lock);
  3891. RESTRICT_TO_RANGE(vf->fmt.win.w.width, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
  3892. RESTRICT_TO_RANGE(vf->fmt.win.w.height, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);
  3893. usbvision->vid_win = *vf;
  3894. usbvision->overlay_frame.width = vf->fmt.win.w.width;
  3895. usbvision->overlay_frame.height = vf->fmt.win.w.height;
  3896. usbvision_set_output(usbvision, vf->fmt.win.w.width, vf->fmt.win.w.height);
  3897. up(&usbvision->lock);
  3898. // Impose display clips
  3899. if (vf->fmt.win.w.left+vf->fmt.win.w.width > (unsigned int)usbvision->vid_buf.fmt.width) {
  3900. usbvision_new_clip(vf, vc, usbvision->vid_buf.fmt.width-vf->fmt.win.w.left, 0, vf->fmt.win.w.width-1, vf->fmt.win.w.height-1);
  3901. }
  3902. if (vf->fmt.win.w.top+vf->fmt.win.w.height > (unsigned int)usbvision->vid_buf.fmt.height) {
  3903. usbvision_new_clip(vf, vc, 0, usbvision->vid_buf.fmt.height-vf->fmt.win.w.top, vf->fmt.win.w.width-1, vf->fmt.win.w.height-1);
  3904. }
  3905. // built the requested clipping zones
  3906. usbvision_built_overlay(usbvision, vf->fmt.win.clipcount, vc);
  3907. vfree(vc);
  3908. // restore overlay state
  3909. if (on) {
  3910. usbvision_cap(usbvision, 1);
  3911. }
  3912. usbvision->vid_win_valid = 1;
  3913. PDEBUG(DBG_IOCTL, "VIDIOC_S_FMT overlay x=%d, y=%d, w=%d, h=%d, chroma=%x, clips=%d",
  3914. vf->fmt.win.w.left, vf->fmt.win.w.top, vf->fmt.win.w.width, vf->fmt.win.w.height, vf->fmt.win.chromakey, vf->fmt.win.clipcount);
  3915. }
  3916. else {
  3917. /* Find requested format in available ones */
  3918. for(formatIdx=0;formatIdx<USBVISION_SUPPORTED_PALETTES;formatIdx++) {
  3919. if(vf->fmt.pix.pixelformat == usbvision_v4l2_format[formatIdx].format) {
  3920. usbvision->palette = usbvision_v4l2_format[formatIdx];
  3921. break;
  3922. }
  3923. }
  3924. /* robustness */
  3925. if(formatIdx == USBVISION_SUPPORTED_PALETTES) {
  3926. return -EINVAL;
  3927. }
  3928. usbvision->vid_win.fmt.pix.pixelformat = vf->fmt.pix.pixelformat;
  3929. usbvision->vid_win.fmt.pix.width = vf->fmt.pix.width; //Image width in pixels.
  3930. usbvision->vid_win.fmt.pix.height = vf->fmt.pix.height; // Image height in pixels.
  3931. // by now we are committed to the new data...
  3932. down(&usbvision->lock);
  3933. RESTRICT_TO_RANGE(vf->fmt.pix.width, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
  3934. RESTRICT_TO_RANGE(vf->fmt.pix.height, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);
  3935. usbvision->vid_win = *vf;
  3936. usbvision_set_output(usbvision, vf->fmt.pix.width, vf->fmt.pix.height);
  3937. up(&usbvision->lock);
  3938. usbvision->vid_win_valid = 1;
  3939. PDEBUG(DBG_IOCTL, "VIDIOC_S_FMT grabdisplay w=%d, h=%d, ",
  3940. vf->fmt.pix.width, vf->fmt.pix.height);
  3941. }
  3942. return 0;
  3943. }
  3944. case VIDIOC_OVERLAY:
  3945. {
  3946. int *v = arg;
  3947. if ( (dga == 0) &&
  3948. (usbvision->palette.format != V4L2_PIX_FMT_YVU420) &&
  3949. (usbvision->palette.format != V4L2_PIX_FMT_YUV422P) ) {
  3950. PDEBUG(DBG_IOCTL, "VIDIOC_OVERLAY DGA disabled");
  3951. return -EINVAL;
  3952. }
  3953. if (*v == 0) {
  3954. usbvision_cap(usbvision, 0);
  3955. }
  3956. else {
  3957. // are VIDIOCSFBUF and VIDIOCSWIN done?
  3958. if ((usbvision->vid_buf_valid == 0) || (usbvision->vid_win_valid == 0)) {
  3959. PDEBUG(DBG_IOCTL, "VIDIOC_OVERLAY vid_buf_valid %d; vid_win_valid %d",
  3960. usbvision->vid_buf_valid, usbvision->vid_win_valid);
  3961. return -EINVAL;
  3962. }
  3963. usbvision_cap(usbvision, 1);
  3964. }
  3965. PDEBUG(DBG_IOCTL, "VIDIOC_OVERLAY %s", (*v)?"on":"off");
  3966. return 0;
  3967. }
  3968. default:
  3969. return -ENOIOCTLCMD;
  3970. }
  3971. return 0;
  3972. }
  3973. static int usbvision_v4l2_ioctl(struct inode *inode, struct file *file,
  3974. unsigned int cmd, unsigned long arg)
  3975. {
  3976. return video_usercopy(inode, file, cmd, arg, usbvision_v4l2_do_ioctl);
  3977. }
  3978. static ssize_t usbvision_v4l2_read(struct file *file, char *buf,
  3979. size_t count, loff_t *ppos)
  3980. {
  3981. struct video_device *dev = video_devdata(file);
  3982. struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
  3983. int noblock = file->f_flags & O_NONBLOCK;
  3984. int frmx = -1;
  3985. int rc = 0;
  3986. struct usbvision_frame *frame;
  3987. PDEBUG(DBG_IO, "%s: %ld bytes, noblock=%d", __FUNCTION__, (unsigned long)count, noblock);
  3988. if (!USBVISION_IS_OPERATIONAL(usbvision) || (buf == NULL))
  3989. return -EFAULT;
  3990. down(&usbvision->lock);
  3991. //code for testing compression
  3992. if (usbvision->isocMode == ISOC_MODE_COMPRESS) {
  3993. usbvision->frame[0].v4l2_format = usbvision_v4l2_format[0]; //V4L2_PIX_FMT_GREY;
  3994. usbvision->frame[1].v4l2_format = usbvision_v4l2_format[0]; // V4L2_PIX_FMT_GREY;
  3995. }
  3996. // See if a frame is completed, then use it.
  3997. if (usbvision->frame[0].grabstate >= FrameState_Done) // _Done or _Error
  3998. frmx = 0;
  3999. else if (usbvision->frame[1].grabstate >= FrameState_Done)// _Done or _Error
  4000. frmx = 1;
  4001. if (noblock && (frmx == -1)) {
  4002. count = -EAGAIN;
  4003. goto usbvision_v4l2_read_done;
  4004. }
  4005. // If no FRAME_DONE, look for a FRAME_GRABBING state.
  4006. // See if a frame is in process (grabbing), then use it.
  4007. if (frmx == -1) {
  4008. if (usbvision->frame[0].grabstate == FrameState_Grabbing)
  4009. frmx = 0;
  4010. else if (usbvision->frame[1].grabstate == FrameState_Grabbing)
  4011. frmx = 1;
  4012. }
  4013. // If no frame is active, start one.
  4014. if (frmx == -1)
  4015. usbvision_new_frame(usbvision, frmx = 0);
  4016. frame = &usbvision->frame[frmx];
  4017. restart:
  4018. if (!USBVISION_IS_OPERATIONAL(usbvision)) {
  4019. count = -EIO;
  4020. goto usbvision_v4l2_read_done;
  4021. }
  4022. PDEBUG(DBG_IO, "Waiting frame grabbing");
  4023. rc = wait_event_interruptible(frame->wq, (frame->grabstate == FrameState_Done) ||
  4024. (frame->grabstate == FrameState_Error));
  4025. if (rc) {
  4026. goto usbvision_v4l2_read_done;
  4027. }
  4028. if (frame->grabstate == FrameState_Error) {
  4029. frame->bytes_read = 0;
  4030. if (usbvision_new_frame(usbvision, frmx)) {
  4031. err("%s: usbvision_new_frame() failed", __FUNCTION__);
  4032. }
  4033. goto restart;
  4034. }
  4035. PDEBUG(DBG_IO, "%s: frmx=%d, bytes_read=%ld, scanlength=%ld", __FUNCTION__,
  4036. frmx, frame->bytes_read, frame->scanlength);
  4037. /* copy bytes to user space; we allow for partials reads */
  4038. if ((count + frame->bytes_read) > (unsigned long)frame->scanlength)
  4039. count = frame->scanlength - frame->bytes_read;
  4040. if (copy_to_user(buf, frame->data + frame->bytes_read, count)) {
  4041. count = -EFAULT;
  4042. goto usbvision_v4l2_read_done;
  4043. }
  4044. frame->bytes_read += count;
  4045. PDEBUG(DBG_IO, "%s: {copy} count used=%ld, new bytes_read=%ld", __FUNCTION__,
  4046. (unsigned long)count, frame->bytes_read);
  4047. if (frame->bytes_read >= frame->scanlength) {// All data has been read
  4048. frame->bytes_read = 0;
  4049. /* Mark it as available to be used again. */
  4050. usbvision->frame[frmx].grabstate = FrameState_Unused;
  4051. if (usbvision_new_frame(usbvision, frmx ? 0 : 1))
  4052. err("%s: usbvision_new_frame() failed", __FUNCTION__);
  4053. }
  4054. usbvision_v4l2_read_done:
  4055. up(&usbvision->lock);
  4056. return count;
  4057. }
  4058. static int usbvision_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
  4059. {
  4060. struct video_device *dev = video_devdata(file);
  4061. struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
  4062. unsigned long start = vma->vm_start;
  4063. unsigned long size = vma->vm_end-vma->vm_start;
  4064. unsigned long page, pos;
  4065. if (!USBVISION_IS_OPERATIONAL(usbvision))
  4066. return -EFAULT;
  4067. if (size > (((USBVISION_NUMFRAMES * usbvision->max_frame_size) + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1)))
  4068. return -EINVAL;
  4069. pos = (unsigned long) usbvision->fbuf;
  4070. while (size > 0) {
  4071. // Really ugly....
  4072. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10) //Compatibility for 2.6.10+ kernels
  4073. page = vmalloc_to_pfn((void *)pos);
  4074. if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) {
  4075. return -EAGAIN;
  4076. }
  4077. #else //Compatibility for 2.6.0 - 2.6.9 kernels
  4078. page = usbvision_kvirt_to_pa(pos);
  4079. if (remap_page_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) {
  4080. return -EAGAIN;
  4081. }
  4082. #endif
  4083. start += PAGE_SIZE;
  4084. pos += PAGE_SIZE;
  4085. if (size > PAGE_SIZE)
  4086. size -= PAGE_SIZE;
  4087. else
  4088. size = 0;
  4089. }
  4090. return 0;
  4091. }
  4092. /*
  4093. * Here comes the stuff for radio on usbvision based devices
  4094. *
  4095. */
  4096. static int usbvision_radio_open(struct inode *inode, struct file *file)
  4097. {
  4098. struct video_device *dev = video_devdata(file);
  4099. struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
  4100. struct v4l2_frequency freq;
  4101. int errCode = 0;
  4102. PDEBUG(DBG_RIO, "%s:", __FUNCTION__);
  4103. down(&usbvision->lock);
  4104. if (usbvision->user) {
  4105. err("%s: Someone tried to open an already opened USBVision Radio!", __FUNCTION__);
  4106. errCode = -EBUSY;
  4107. }
  4108. else {
  4109. if(PowerOnAtOpen) {
  4110. if (timer_pending(&usbvision->powerOffTimer)) {
  4111. del_timer(&usbvision->powerOffTimer);
  4112. }
  4113. if (usbvision->power == 0) {
  4114. usbvision_power_on(usbvision);
  4115. usbvision_init_i2c(usbvision);
  4116. }
  4117. }
  4118. // If so far no errors then we shall start the radio
  4119. usbvision->radio = 1;
  4120. call_i2c_clients(usbvision,AUDC_SET_RADIO,&usbvision->tuner_type);
  4121. freq.frequency = 1517; //SWR3 @ 94.8MHz
  4122. call_i2c_clients(usbvision, VIDIOC_S_FREQUENCY, &freq);
  4123. usbvision_set_audio(usbvision, USBVISION_AUDIO_RADIO);
  4124. usbvision->user++;
  4125. }
  4126. if (errCode) {
  4127. if (PowerOnAtOpen) {
  4128. usbvision_i2c_usb_del_bus(&usbvision->i2c_adap);
  4129. usbvision_power_off(usbvision);
  4130. usbvision->initialized = 0;
  4131. }
  4132. }
  4133. up(&usbvision->lock);
  4134. return errCode;
  4135. }
  4136. static int usbvision_radio_close(struct inode *inode, struct file *file)
  4137. {
  4138. struct video_device *dev = video_devdata(file);
  4139. struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
  4140. int errCode = 0;
  4141. PDEBUG(DBG_RIO, "");
  4142. down(&usbvision->lock);
  4143. usbvision_audio_off(usbvision);
  4144. usbvision->radio=0;
  4145. usbvision->user--;
  4146. if (PowerOnAtOpen) {
  4147. mod_timer(&usbvision->powerOffTimer, jiffies + USBVISION_POWEROFF_TIME);
  4148. usbvision->initialized = 0;
  4149. }
  4150. up(&usbvision->lock);
  4151. if (usbvision->remove_pending) {
  4152. info("%s: Final disconnect", __FUNCTION__);
  4153. usbvision_release(usbvision);
  4154. }
  4155. PDEBUG(DBG_RIO, "success");
  4156. return errCode;
  4157. }
  4158. static int usbvision_do_radio_ioctl(struct inode *inode, struct file *file,
  4159. unsigned int cmd, void *arg)
  4160. {
  4161. struct video_device *dev = video_devdata(file);
  4162. struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
  4163. if (!USBVISION_IS_OPERATIONAL(usbvision))
  4164. return -EIO;
  4165. switch (cmd) {
  4166. /***************************
  4167. * V4L2 IOCTLs *
  4168. ***************************/
  4169. case VIDIOC_QUERYCAP:
  4170. {
  4171. struct v4l2_capability *vc=arg;
  4172. memset(vc, 0, sizeof(struct v4l2_capability));
  4173. strcpy(vc->driver,"usbvision radio");
  4174. strcpy(vc->card,usbvision->vcap.card);
  4175. strcpy(vc->bus_info,"usb");
  4176. vc->version = USBVISION_DRIVER_VERSION; /* version */
  4177. vc->capabilities = V4L2_CAP_TUNER; /* capabilities */
  4178. PDEBUG(DBG_RIO, "%s: VIDIOC_QUERYCAP", __FUNCTION__);
  4179. return 0;
  4180. }
  4181. case VIDIOC_QUERYCTRL:
  4182. {
  4183. struct v4l2_queryctrl *qc = arg;
  4184. switch(qc->id)
  4185. {
  4186. case V4L2_CID_AUDIO_VOLUME:
  4187. case V4L2_CID_AUDIO_MUTE:
  4188. return v4l2_ctrl_query_fill_std(qc);
  4189. break;
  4190. default:
  4191. return -EINVAL;
  4192. }
  4193. return 0;
  4194. }
  4195. case VIDIOC_G_CTRL:
  4196. {
  4197. struct v4l2_control *ctrl = arg;
  4198. PDEBUG(DBG_IOCTL, "VIDIOC_G_CTRL id=%x value=%x",ctrl->id,ctrl->value);
  4199. switch(ctrl->id) {
  4200. case V4L2_CID_AUDIO_VOLUME:
  4201. /* ctrl->value = usbvision->volume; */
  4202. break;
  4203. case V4L2_CID_AUDIO_MUTE:
  4204. ctrl->value = usbvision->AudioMute;
  4205. break;
  4206. default:
  4207. return -EINVAL;
  4208. }
  4209. return 0;
  4210. }
  4211. case VIDIOC_S_CTRL:
  4212. {
  4213. struct v4l2_control *ctrl = arg;
  4214. call_i2c_clients(usbvision, VIDIOC_S_CTRL, ctrl);
  4215. PDEBUG(DBG_RIO, "%s: VIDIOC_S_CTRL id=%x value=%x", __FUNCTION__,ctrl->id,ctrl->value);
  4216. return 0;
  4217. }
  4218. case VIDIOC_G_TUNER:
  4219. {
  4220. struct v4l2_tuner *vt = arg;
  4221. if (!usbvision->have_tuner || vt->index) // Only tuner 0
  4222. return -EINVAL;
  4223. strcpy(vt->name, "Radio");
  4224. vt->type = V4L2_TUNER_RADIO;
  4225. vt->capability = V4L2_TUNER_CAP_STEREO;
  4226. // japan: 76.0 MHz - 89.9 MHz
  4227. // western europe: 87.5 MHz - 108.0 MHz
  4228. // russia: 65.0 MHz - 108.0 MHz
  4229. vt->rangelow = (int)(65*16);;
  4230. vt->rangehigh = (int)(108*16);
  4231. vt->audmode = V4L2_TUNER_MODE_STEREO;
  4232. vt->rxsubchans = V4L2_TUNER_SUB_STEREO;
  4233. call_i2c_clients(usbvision,VIDIOC_G_TUNER,&vt);
  4234. PDEBUG(DBG_RIO, "%s: VIDIOC_G_TUNER signal=%d", __FUNCTION__, vt->signal);
  4235. return 0;
  4236. }
  4237. case VIDIOC_S_TUNER:
  4238. {
  4239. struct v4l2_tuner *vt = arg;
  4240. // Only channel 0 has a tuner
  4241. if((vt->index) || (usbvision->channel)) {
  4242. return -EINVAL;
  4243. }
  4244. PDEBUG(DBG_RIO, "%s: VIDIOC_S_TUNER", __FUNCTION__);
  4245. return 0;
  4246. }
  4247. case VIDIOC_G_AUDIO:
  4248. {
  4249. struct v4l2_audio *va = arg;
  4250. memset(va,0, sizeof(va));
  4251. va->capability = V4L2_AUDCAP_STEREO;
  4252. strcpy(va->name, "Radio");
  4253. PDEBUG(DBG_IOCTL, "VIDIOC_G_AUDIO");
  4254. return 0;
  4255. }
  4256. case VIDIOC_S_AUDIO:
  4257. {
  4258. struct v4l2_audio *v = arg;
  4259. if(v->index) {
  4260. return -EINVAL;
  4261. }
  4262. PDEBUG(DBG_IOCTL, "VIDIOC_S_AUDIO");
  4263. // FIXME: void function ???
  4264. return 0;
  4265. }
  4266. case VIDIOC_G_FREQUENCY:
  4267. {
  4268. struct v4l2_frequency *freq = arg;
  4269. freq->tuner = 0; // Only one tuner
  4270. freq->type = V4L2_TUNER_RADIO;
  4271. freq->frequency = usbvision->freq;
  4272. PDEBUG(DBG_RIO, "VIDIOC_G_FREQUENCY freq=0x%X", (unsigned)freq->frequency);
  4273. return 0;
  4274. }
  4275. case VIDIOC_S_FREQUENCY:
  4276. {
  4277. struct v4l2_frequency *freq = arg;
  4278. usbvision->freq = freq->frequency;
  4279. call_i2c_clients(usbvision, cmd, freq);
  4280. PDEBUG(DBG_RIO, "VIDIOC_S_FREQUENCY freq=0x%X", (unsigned)freq->frequency);
  4281. return 0;
  4282. }
  4283. /***************************
  4284. * V4L1 IOCTLs *
  4285. ***************************/
  4286. case VIDIOCGCAP:
  4287. {
  4288. struct video_capability *vc = arg;
  4289. memset(vc, 0, sizeof(struct video_capability));
  4290. strcpy(vc->name,usbvision->vcap.card);
  4291. vc->type = VID_TYPE_TUNER;
  4292. vc->channels = 1;
  4293. vc->audios = 1;
  4294. PDEBUG(DBG_RIO, "%s: VIDIOCGCAP", __FUNCTION__);
  4295. return 0;
  4296. }
  4297. case VIDIOCGTUNER:
  4298. {
  4299. struct video_tuner *vt = arg;
  4300. if((vt->tuner) || (usbvision->channel)) { /* Only tuner 0 */
  4301. return -EINVAL;
  4302. }
  4303. strcpy(vt->name, "Radio");
  4304. // japan: 76.0 MHz - 89.9 MHz
  4305. // western europe: 87.5 MHz - 108.0 MHz
  4306. // russia: 65.0 MHz - 108.0 MHz
  4307. vt->rangelow=(int)(65*16);
  4308. vt->rangehigh=(int)(108*16);
  4309. vt->flags= 0;
  4310. vt->mode = 0;
  4311. call_i2c_clients(usbvision,cmd,vt);
  4312. PDEBUG(DBG_RIO, "%s: VIDIOCGTUNER signal=%d", __FUNCTION__, vt->signal);
  4313. return 0;
  4314. }
  4315. case VIDIOCSTUNER:
  4316. {
  4317. struct video_tuner *vt = arg;
  4318. // Only channel 0 has a tuner
  4319. if((vt->tuner) || (usbvision->channel)) {
  4320. return -EINVAL;
  4321. }
  4322. PDEBUG(DBG_RIO, "%s: VIDIOCSTUNER", __FUNCTION__);
  4323. return 0;
  4324. }
  4325. case VIDIOCGAUDIO:
  4326. {
  4327. struct video_audio *va = arg;
  4328. memset(va,0, sizeof(struct video_audio));
  4329. call_i2c_clients(usbvision, cmd, va);
  4330. va->flags|=VIDEO_AUDIO_MUTABLE;
  4331. va->volume=1;
  4332. va->step=1;
  4333. strcpy(va->name, "Radio");
  4334. PDEBUG(DBG_RIO, "%s: VIDIOCGAUDIO", __FUNCTION__);
  4335. return 0;
  4336. }
  4337. case VIDIOCSAUDIO:
  4338. {
  4339. struct video_audio *va = arg;
  4340. if(va->audio) {
  4341. return -EINVAL;
  4342. }
  4343. if(va->flags & VIDEO_AUDIO_MUTE) {
  4344. if (usbvision_audio_mute(usbvision)) {
  4345. return -EFAULT;
  4346. }
  4347. }
  4348. else {
  4349. if (usbvision_audio_on(usbvision)) {
  4350. return -EFAULT;
  4351. }
  4352. }
  4353. PDEBUG(DBG_RIO, "%s: VIDIOCSAUDIO flags=0x%x)", __FUNCTION__, va->flags);
  4354. return 0;
  4355. }
  4356. case VIDIOCGFREQ:
  4357. {
  4358. unsigned long *freq = arg;
  4359. *freq = usbvision->freq;
  4360. PDEBUG(DBG_RIO, "%s: VIDIOCGFREQ freq = %ld00 kHz", __FUNCTION__, (*freq * 10)>>4);
  4361. return 0;
  4362. }
  4363. case VIDIOCSFREQ:
  4364. {
  4365. unsigned long *freq = arg;
  4366. usbvision->freq = *freq;
  4367. call_i2c_clients(usbvision, cmd, freq);
  4368. PDEBUG(DBG_RIO, "%s: VIDIOCSFREQ freq = %ld00 kHz", __FUNCTION__, (*freq * 10)>>4);
  4369. return 0;
  4370. }
  4371. default:
  4372. {
  4373. PDEBUG(DBG_RIO, "%s: Unknown command %x", __FUNCTION__, cmd);
  4374. return -ENOIOCTLCMD;
  4375. }
  4376. }
  4377. return 0;
  4378. }
  4379. static int usbvision_radio_ioctl(struct inode *inode, struct file *file,
  4380. unsigned int cmd, unsigned long arg)
  4381. {
  4382. return video_usercopy(inode, file, cmd, arg, usbvision_do_radio_ioctl);
  4383. }
  4384. /*
  4385. * Here comes the stuff for vbi on usbvision based devices
  4386. *
  4387. */
  4388. static int usbvision_vbi_open(struct inode *inode, struct file *file)
  4389. {
  4390. struct video_device *dev = video_devdata(file);
  4391. struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
  4392. unsigned long freq;
  4393. int errCode = 0;
  4394. PDEBUG(DBG_RIO, "%s:", __FUNCTION__);
  4395. down(&usbvision->lock);
  4396. if (usbvision->user) {
  4397. err("%s: Someone tried to open an already opened USBVision VBI!", __FUNCTION__);
  4398. errCode = -EBUSY;
  4399. }
  4400. else {
  4401. if(PowerOnAtOpen) {
  4402. if (timer_pending(&usbvision->powerOffTimer)) {
  4403. del_timer(&usbvision->powerOffTimer);
  4404. }
  4405. if (usbvision->power == 0) {
  4406. usbvision_power_on(usbvision);
  4407. usbvision_init_i2c(usbvision);
  4408. }
  4409. }
  4410. // If so far no errors then we shall start the vbi device
  4411. //usbvision->vbi = 1;
  4412. call_i2c_clients(usbvision,AUDC_SET_RADIO,&usbvision->tuner_type);
  4413. freq = 1517; //SWR3 @ 94.8MHz
  4414. call_i2c_clients(usbvision, VIDIOCSFREQ, &freq);
  4415. usbvision_set_audio(usbvision, USBVISION_AUDIO_RADIO);
  4416. usbvision->user++;
  4417. }
  4418. if (errCode) {
  4419. if (PowerOnAtOpen) {
  4420. usbvision_i2c_usb_del_bus(&usbvision->i2c_adap);
  4421. usbvision_power_off(usbvision);
  4422. usbvision->initialized = 0;
  4423. }
  4424. }
  4425. up(&usbvision->lock);
  4426. return errCode;
  4427. }
  4428. static int usbvision_vbi_close(struct inode *inode, struct file *file)
  4429. {
  4430. struct video_device *dev = video_devdata(file);
  4431. struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
  4432. int errCode = 0;
  4433. PDEBUG(DBG_RIO, "");
  4434. down(&usbvision->lock);
  4435. usbvision_audio_off(usbvision);
  4436. usbvision->vbi=0;
  4437. usbvision->user--;
  4438. if (PowerOnAtOpen) {
  4439. mod_timer(&usbvision->powerOffTimer, jiffies + USBVISION_POWEROFF_TIME);
  4440. usbvision->initialized = 0;
  4441. }
  4442. up(&usbvision->lock);
  4443. if (usbvision->remove_pending) {
  4444. info("%s: Final disconnect", __FUNCTION__);
  4445. usbvision_release(usbvision);
  4446. }
  4447. PDEBUG(DBG_RIO, "success");
  4448. return errCode;
  4449. }
  4450. static int usbvision_do_vbi_ioctl(struct inode *inode, struct file *file,
  4451. unsigned int cmd, void *arg)
  4452. {
  4453. struct video_device *dev = video_devdata(file);
  4454. struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
  4455. if (!USBVISION_IS_OPERATIONAL(usbvision))
  4456. return -EIO;
  4457. switch (cmd) {
  4458. case VIDIOC_QUERYCAP:
  4459. {
  4460. struct v4l2_capability *vc=arg;
  4461. memset(vc, 0, sizeof(struct v4l2_capability));
  4462. strcpy(vc->driver,"usbvision vbi");
  4463. strcpy(vc->card,usbvision->vcap.card);
  4464. strcpy(vc->bus_info,"usb");
  4465. vc->version = USBVISION_DRIVER_VERSION; /* version */
  4466. vc->capabilities = V4L2_CAP_VBI_CAPTURE; /* capabilities */
  4467. PDEBUG(DBG_RIO, "%s: VIDIOC_QUERYCAP", __FUNCTION__);
  4468. return 0;
  4469. }
  4470. case VIDIOCGTUNER:
  4471. {
  4472. struct video_tuner *vt = arg;
  4473. if((vt->tuner) || (usbvision->channel)) { /* Only tuner 0 */
  4474. return -EINVAL;
  4475. }
  4476. strcpy(vt->name, "vbi");
  4477. // japan: 76.0 MHz - 89.9 MHz
  4478. // western europe: 87.5 MHz - 108.0 MHz
  4479. // russia: 65.0 MHz - 108.0 MHz
  4480. vt->rangelow=(int)(65*16);
  4481. vt->rangehigh=(int)(108*16);
  4482. vt->flags= 0;
  4483. vt->mode = 0;
  4484. call_i2c_clients(usbvision,cmd,vt);
  4485. PDEBUG(DBG_RIO, "%s: VIDIOCGTUNER signal=%d", __FUNCTION__, vt->signal);
  4486. return 0;
  4487. }
  4488. case VIDIOCSTUNER:
  4489. {
  4490. struct video_tuner *vt = arg;
  4491. // Only channel 0 has a tuner
  4492. if((vt->tuner) || (usbvision->channel)) {
  4493. return -EINVAL;
  4494. }
  4495. PDEBUG(DBG_RIO, "%s: VIDIOCSTUNER", __FUNCTION__);
  4496. return 0;
  4497. }
  4498. case VIDIOCGAUDIO:
  4499. {
  4500. struct video_audio *va = arg;
  4501. memset(va,0, sizeof(struct video_audio));
  4502. call_i2c_clients(usbvision, cmd, va);
  4503. va->flags|=VIDEO_AUDIO_MUTABLE;
  4504. va->volume=1;
  4505. va->step=1;
  4506. strcpy(va->name, "vbi");
  4507. PDEBUG(DBG_RIO, "%s: VIDIOCGAUDIO", __FUNCTION__);
  4508. return 0;
  4509. }
  4510. case VIDIOCSAUDIO:
  4511. {
  4512. struct video_audio *va = arg;
  4513. if(va->audio) {
  4514. return -EINVAL;
  4515. }
  4516. if(va->flags & VIDEO_AUDIO_MUTE) {
  4517. if (usbvision_audio_mute(usbvision)) {
  4518. return -EFAULT;
  4519. }
  4520. }
  4521. else {
  4522. if (usbvision_audio_on(usbvision)) {
  4523. return -EFAULT;
  4524. }
  4525. }
  4526. PDEBUG(DBG_RIO, "%s: VIDIOCSAUDIO flags=0x%x)", __FUNCTION__, va->flags);
  4527. return 0;
  4528. }
  4529. case VIDIOCGFREQ:
  4530. {
  4531. unsigned long *freq = arg;
  4532. *freq = usbvision->freq;
  4533. PDEBUG(DBG_RIO, "%s: VIDIOCGFREQ freq = %ld00 kHz", __FUNCTION__, (*freq * 10)>>4);
  4534. return 0;
  4535. }
  4536. case VIDIOCSFREQ:
  4537. {
  4538. unsigned long *freq = arg;
  4539. usbvision->freq = *freq;
  4540. call_i2c_clients(usbvision, cmd, freq);
  4541. PDEBUG(DBG_RIO, "%s: VIDIOCSFREQ freq = %ld00 kHz", __FUNCTION__, (*freq * 10)>>4);
  4542. return 0;
  4543. }
  4544. default:
  4545. {
  4546. PDEBUG(DBG_RIO, "%s: Unknown command %d", __FUNCTION__, cmd);
  4547. return -ENOIOCTLCMD;
  4548. }
  4549. }
  4550. return 0;
  4551. }
  4552. static int usbvision_vbi_ioctl(struct inode *inode, struct file *file,
  4553. unsigned int cmd, unsigned long arg)
  4554. {
  4555. return video_usercopy(inode, file, cmd, arg, usbvision_do_vbi_ioctl);
  4556. }
  4557. static void usbvision_configure_video(struct usb_usbvision *usbvision)
  4558. {
  4559. int model;
  4560. if (usbvision == NULL)
  4561. return;
  4562. model = usbvision->DevModel;
  4563. RESTRICT_TO_RANGE(init_brightness, 0, 255);
  4564. RESTRICT_TO_RANGE(init_contrast, 0, 255);
  4565. RESTRICT_TO_RANGE(init_saturation, 0, 255);
  4566. RESTRICT_TO_RANGE(init_hue, 0, 255);
  4567. usbvision->saturation = init_saturation << 8;
  4568. usbvision->hue = init_hue << 8;
  4569. usbvision->brightness = init_brightness << 8;
  4570. usbvision->contrast = init_contrast << 8;
  4571. usbvision->depth = 24;
  4572. usbvision->palette = usbvision_v4l2_format[2]; // V4L2_PIX_FMT_RGB24;
  4573. if (usbvision_device_data[usbvision->DevModel].Vin_Reg2 >= 0) {
  4574. usbvision->Vin_Reg2_Preset = usbvision_device_data[usbvision->DevModel].Vin_Reg2 & 0xff;
  4575. } else {
  4576. usbvision->Vin_Reg2_Preset = 0;
  4577. }
  4578. memset(&usbvision->vcap, 0, sizeof(usbvision->vcap));
  4579. strcpy(usbvision->vcap.driver, "USBVision");
  4580. usbvision->vcap.capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
  4581. (dga ? (V4L2_FBUF_CAP_LIST_CLIPPING | V4L2_CAP_VIDEO_OVERLAY) : 0) |
  4582. (usbvision->have_tuner ? V4L2_CAP_TUNER : 0);
  4583. usbvision->vcap.version = USBVISION_DRIVER_VERSION; /* version */
  4584. usbvision->video_inputs = usbvision_device_data[model].VideoChannels;
  4585. memset(&usbvision->input, 0, sizeof(usbvision->input));
  4586. usbvision->input.tuner = 0;
  4587. usbvision->input.type = V4L2_INPUT_TYPE_TUNER|VIDEO_TYPE_CAMERA;
  4588. usbvision->input.std = usbvision_device_data[model].VideoNorm;
  4589. /* This should be here to make i2c clients to be able to register */
  4590. usbvision_audio_off(usbvision); //first switch off audio
  4591. if (!PowerOnAtOpen) {
  4592. usbvision_power_on(usbvision); //and then power up the noisy tuner
  4593. usbvision_init_i2c(usbvision);
  4594. }
  4595. }
  4596. //
  4597. // Video registration stuff
  4598. //
  4599. // Video template
  4600. static struct file_operations usbvision_fops = {
  4601. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,4,31)
  4602. .owner = THIS_MODULE,
  4603. #endif
  4604. .open = usbvision_v4l2_open,
  4605. .release = usbvision_v4l2_close,
  4606. .read = usbvision_v4l2_read,
  4607. .mmap = usbvision_v4l2_mmap,
  4608. .ioctl = usbvision_v4l2_ioctl,
  4609. .llseek = no_llseek,
  4610. };
  4611. static struct video_device usbvision_video_template = {
  4612. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,4,31)
  4613. .owner = THIS_MODULE,
  4614. #endif
  4615. .type = VID_TYPE_TUNER | VID_TYPE_CAPTURE,
  4616. .hardware = VID_HARDWARE_USBVISION,
  4617. .fops = &usbvision_fops,
  4618. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
  4619. .name = "usbvision-video",
  4620. .release = video_device_release,
  4621. #endif
  4622. .minor = -1,
  4623. };
  4624. // Radio template
  4625. static struct file_operations usbvision_radio_fops = {
  4626. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,4,31)
  4627. .owner = THIS_MODULE,
  4628. #endif
  4629. .open = usbvision_radio_open,
  4630. .release = usbvision_radio_close,
  4631. .ioctl = usbvision_radio_ioctl,
  4632. .llseek = no_llseek,
  4633. };
  4634. static struct video_device usbvision_radio_template=
  4635. {
  4636. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,4,31)
  4637. .owner = THIS_MODULE,
  4638. #endif
  4639. .type = VID_TYPE_TUNER,
  4640. .hardware = VID_HARDWARE_USBVISION,
  4641. .fops = &usbvision_radio_fops,
  4642. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
  4643. .release = video_device_release,
  4644. .name = "usbvision-radio",
  4645. #endif
  4646. .minor = -1,
  4647. };
  4648. // vbi template
  4649. static struct file_operations usbvision_vbi_fops = {
  4650. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,4,31)
  4651. .owner = THIS_MODULE,
  4652. #endif
  4653. .open = usbvision_vbi_open,
  4654. .release = usbvision_vbi_close,
  4655. .ioctl = usbvision_vbi_ioctl,
  4656. .llseek = no_llseek,
  4657. };
  4658. static struct video_device usbvision_vbi_template=
  4659. {
  4660. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,4,31)
  4661. .owner = THIS_MODULE,
  4662. #endif
  4663. .type = VID_TYPE_TUNER,
  4664. .hardware = VID_HARDWARE_USBVISION,
  4665. .fops = &usbvision_vbi_fops,
  4666. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
  4667. .release = video_device_release,
  4668. .name = "usbvision-vbi",
  4669. #endif
  4670. .minor = -1,
  4671. };
  4672. static struct video_device *usbvision_vdev_init(struct usb_usbvision *usbvision,
  4673. struct video_device *vdev_template,
  4674. char *name)
  4675. {
  4676. struct usb_device *usb_dev = usbvision->dev;
  4677. struct video_device *vdev;
  4678. if (usb_dev == NULL) {
  4679. err("%s: usbvision->dev is not set", __FUNCTION__);
  4680. return NULL;
  4681. }
  4682. vdev = video_device_alloc();
  4683. if (NULL == vdev) {
  4684. return NULL;
  4685. }
  4686. *vdev = *vdev_template;
  4687. // vdev->minor = -1;
  4688. vdev->dev = &usb_dev->dev;
  4689. snprintf(vdev->name, sizeof(vdev->name), "%s", name);
  4690. video_set_drvdata(vdev, usbvision);
  4691. return vdev;
  4692. }
  4693. // unregister video4linux devices
  4694. static void usbvision_unregister_video(struct usb_usbvision *usbvision)
  4695. {
  4696. // vbi Device:
  4697. if (usbvision->vbi) {
  4698. PDEBUG(DBG_PROBE, "unregister /dev/vbi%d [v4l2]", usbvision->vbi->minor & 0x1f);
  4699. if (usbvision->vbi->minor != -1) {
  4700. video_unregister_device(usbvision->vbi);
  4701. }
  4702. else {
  4703. video_device_release(usbvision->vbi);
  4704. }
  4705. usbvision->vbi = NULL;
  4706. }
  4707. // Radio Device:
  4708. if (usbvision->rdev) {
  4709. PDEBUG(DBG_PROBE, "unregister /dev/radio%d [v4l2]", usbvision->rdev->minor & 0x1f);
  4710. if (usbvision->rdev->minor != -1) {
  4711. video_unregister_device(usbvision->rdev);
  4712. }
  4713. else {
  4714. video_device_release(usbvision->rdev);
  4715. }
  4716. usbvision->rdev = NULL;
  4717. }
  4718. // Video Device:
  4719. if (usbvision->vdev) {
  4720. PDEBUG(DBG_PROBE, "unregister /dev/video%d [v4l2]", usbvision->vdev->minor & 0x1f);
  4721. if (usbvision->vdev->minor != -1) {
  4722. video_unregister_device(usbvision->vdev);
  4723. }
  4724. else {
  4725. video_device_release(usbvision->vdev);
  4726. }
  4727. usbvision->vdev = NULL;
  4728. }
  4729. }
  4730. // register video4linux devices
  4731. static int __devinit usbvision_register_video(struct usb_usbvision *usbvision)
  4732. {
  4733. // Video Device:
  4734. usbvision->vdev = usbvision_vdev_init(usbvision, &usbvision_video_template, "USBVision Video");
  4735. if (usbvision->vdev == NULL) {
  4736. goto err_exit;
  4737. }
  4738. if (video_register_device(usbvision->vdev, VFL_TYPE_GRABBER, video_nr)<0) {
  4739. goto err_exit;
  4740. }
  4741. info("USBVision[%d]: registered USBVision Video device /dev/video%d [v4l2]", usbvision->nr,usbvision->vdev->minor & 0x1f);
  4742. // Radio Device:
  4743. if (usbvision_device_data[usbvision->DevModel].Radio) {
  4744. // usbvision has radio
  4745. usbvision->rdev = usbvision_vdev_init(usbvision, &usbvision_radio_template, "USBVision Radio");
  4746. if (usbvision->rdev == NULL) {
  4747. goto err_exit;
  4748. }
  4749. if (video_register_device(usbvision->rdev, VFL_TYPE_RADIO, radio_nr)<0) {
  4750. goto err_exit;
  4751. }
  4752. info("USBVision[%d]: registered USBVision Radio device /dev/radio%d [v4l2]", usbvision->nr, usbvision->rdev->minor & 0x1f);
  4753. }
  4754. // vbi Device:
  4755. if (usbvision_device_data[usbvision->DevModel].vbi) {
  4756. usbvision->vbi = usbvision_vdev_init(usbvision, &usbvision_vbi_template, "USBVision VBI");
  4757. if (usbvision->vdev == NULL) {
  4758. goto err_exit;
  4759. }
  4760. if (video_register_device(usbvision->vbi, VFL_TYPE_VBI, vbi_nr)<0) {
  4761. goto err_exit;
  4762. }
  4763. info("USBVision[%d]: registered USBVision VBI device /dev/vbi%d [v4l2] (Not Working Yet!)", usbvision->nr,usbvision->vbi->minor & 0x1f);
  4764. }
  4765. // all done
  4766. return 0;
  4767. err_exit:
  4768. err("USBVision[%d]: video_register_device() failed", usbvision->nr);
  4769. usbvision_unregister_video(usbvision);
  4770. return -1;
  4771. }
  4772. /*
  4773. * usbvision_alloc()
  4774. *
  4775. * This code allocates the struct usb_usbvision. It is filled with default values.
  4776. *
  4777. * Returns NULL on error, a pointer to usb_usbvision else.
  4778. *
  4779. */
  4780. static struct usb_usbvision *usbvision_alloc(struct usb_device *dev)
  4781. {
  4782. struct usb_usbvision *usbvision;
  4783. int FrameIdx;
  4784. if ((usbvision = kzalloc(sizeof(struct usb_usbvision), GFP_KERNEL)) == NULL) {
  4785. goto err_exit;
  4786. }
  4787. usbvision->dev = dev;
  4788. for (FrameIdx = 0; FrameIdx < USBVISION_NUMFRAMES; FrameIdx++) {
  4789. init_waitqueue_head(&usbvision->frame[FrameIdx].wq);
  4790. }
  4791. init_waitqueue_head(&usbvision->overlay_frame.wq);
  4792. init_MUTEX(&usbvision->lock); /* to 1 == available */
  4793. // prepare control urb for control messages during interrupts
  4794. usbvision->ctrlUrb = usb_alloc_urb(USBVISION_URB_FRAMES, GFP_KERNEL);
  4795. if (usbvision->ctrlUrb == NULL) {
  4796. goto err_exit;
  4797. }
  4798. init_waitqueue_head(&usbvision->ctrlUrb_wq);
  4799. init_MUTEX(&usbvision->ctrlUrbLock);
  4800. init_timer(&usbvision->powerOffTimer);
  4801. usbvision->powerOffTimer.data = (long) usbvision;
  4802. usbvision->powerOffTimer.function = usbvision_powerOffTimer;
  4803. return usbvision;
  4804. err_exit:
  4805. if (usbvision && usbvision->ctrlUrb) {
  4806. usb_free_urb(usbvision->ctrlUrb);
  4807. }
  4808. if (usbvision) {
  4809. kfree(usbvision);
  4810. }
  4811. return NULL;
  4812. }
  4813. /*
  4814. * usbvision_release()
  4815. *
  4816. * This code does final release of struct usb_usbvision. This happens
  4817. * after the device is disconnected -and- all clients closed their files.
  4818. *
  4819. */
  4820. static void usbvision_release(struct usb_usbvision *usbvision)
  4821. {
  4822. PDEBUG(DBG_PROBE, "");
  4823. down(&usbvision->lock);
  4824. if (timer_pending(&usbvision->powerOffTimer)) {
  4825. del_timer(&usbvision->powerOffTimer);
  4826. }
  4827. usbvision->usbvision_used = 0;
  4828. usbvision->initialized = 0;
  4829. up(&usbvision->lock);
  4830. usbvision_remove_sysfs(usbvision->vdev);
  4831. usbvision_unregister_video(usbvision);
  4832. if(dga) {
  4833. if (usbvision->overlay_base) {
  4834. iounmap(usbvision->overlay_base);
  4835. }
  4836. }
  4837. if (usbvision->ctrlUrb) {
  4838. usb_free_urb(usbvision->ctrlUrb);
  4839. }
  4840. kfree(usbvision);
  4841. PDEBUG(DBG_PROBE, "success");
  4842. }
  4843. /*
  4844. * usbvision_probe()
  4845. *
  4846. * This procedure queries device descriptor and accepts the interface
  4847. * if it looks like USBVISION video device
  4848. *
  4849. */
  4850. static int __devinit usbvision_probe(struct usb_interface *intf, const struct usb_device_id *devid)
  4851. {
  4852. struct usb_device *dev = interface_to_usbdev(intf);
  4853. __u8 ifnum = intf->altsetting->desc.bInterfaceNumber;
  4854. const struct usb_host_interface *interface;
  4855. struct usb_usbvision *usbvision = NULL;
  4856. const struct usb_endpoint_descriptor *endpoint;
  4857. int model;
  4858. PDEBUG(DBG_PROBE, "VID=%#04x, PID=%#04x, ifnum=%u",
  4859. dev->descriptor.idVendor, dev->descriptor.idProduct, ifnum);
  4860. /* Is it an USBVISION video dev? */
  4861. model = 0;
  4862. for(model = 0; usbvision_device_data[model].idVendor; model++) {
  4863. if (le16_to_cpu(dev->descriptor.idVendor) != usbvision_device_data[model].idVendor) {
  4864. continue;
  4865. }
  4866. if (le16_to_cpu(dev->descriptor.idProduct) != usbvision_device_data[model].idProduct) {
  4867. continue;
  4868. }
  4869. info("%s: %s found", __FUNCTION__, usbvision_device_data[model].ModelString);
  4870. break;
  4871. }
  4872. if (usbvision_device_data[model].idVendor == 0) {
  4873. return -ENODEV; //no matching device
  4874. }
  4875. if (usbvision_device_data[model].Interface >= 0) {
  4876. interface = &dev->actconfig->interface[usbvision_device_data[model].Interface]->altsetting[0];
  4877. }
  4878. else {
  4879. interface = &dev->actconfig->interface[ifnum]->altsetting[0];
  4880. }
  4881. endpoint = &interface->endpoint[1].desc;
  4882. if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC) {
  4883. err("%s: interface %d. has non-ISO endpoint!", __FUNCTION__, ifnum);
  4884. err("%s: Endpoint attribures %d", __FUNCTION__, endpoint->bmAttributes);
  4885. return -ENODEV;
  4886. }
  4887. if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
  4888. err("%s: interface %d. has ISO OUT endpoint!", __FUNCTION__, ifnum);
  4889. return -ENODEV;
  4890. }
  4891. usb_get_dev(dev);
  4892. if ((usbvision = usbvision_alloc(dev)) == NULL) {
  4893. err("%s: couldn't allocate USBVision struct", __FUNCTION__);
  4894. return -ENOMEM;
  4895. }
  4896. if (dev->descriptor.bNumConfigurations > 1) {
  4897. usbvision->bridgeType = BRIDGE_NT1004;
  4898. }
  4899. else if (usbvision_device_data[model].ModelString == "Dazzle Fusion Model DVC-90 Rev 1 (SECAM)") {
  4900. usbvision->bridgeType = BRIDGE_NT1005;
  4901. }
  4902. else {
  4903. usbvision->bridgeType = BRIDGE_NT1003;
  4904. }
  4905. PDEBUG(DBG_PROBE, "bridgeType %d", usbvision->bridgeType);
  4906. down(&usbvision->lock);
  4907. usbvision->nr = usbvision_nr++;
  4908. usbvision->have_tuner = usbvision_device_data[model].Tuner;
  4909. if (usbvision->have_tuner) {
  4910. usbvision->tuner_type = usbvision_device_data[model].TunerType;
  4911. }
  4912. usbvision->DevModel = model;
  4913. usbvision->remove_pending = 0;
  4914. usbvision->last_error = 0;
  4915. usbvision->iface = ifnum;
  4916. usbvision->ifaceAltInactive = 0;
  4917. usbvision->ifaceAltActive = 1;
  4918. usbvision->video_endp = endpoint->bEndpointAddress;
  4919. usbvision->isocPacketSize = 0;
  4920. usbvision->usb_bandwidth = 0;
  4921. usbvision->user = 0;
  4922. usbvision_register_video(usbvision);
  4923. usbvision_configure_video(usbvision);
  4924. up(&usbvision->lock);
  4925. usb_set_intfdata (intf, usbvision);
  4926. usbvision_create_sysfs(usbvision->vdev);
  4927. PDEBUG(DBG_PROBE, "success");
  4928. return 0;
  4929. }
  4930. /*
  4931. * usbvision_disconnect()
  4932. *
  4933. * This procedure stops all driver activity, deallocates interface-private
  4934. * structure (pointed by 'ptr') and after that driver should be removable
  4935. * with no ill consequences.
  4936. *
  4937. */
  4938. static void __devexit usbvision_disconnect(struct usb_interface *intf)
  4939. {
  4940. struct usb_usbvision *usbvision = usb_get_intfdata(intf);
  4941. PDEBUG(DBG_PROBE, "");
  4942. if (usbvision == NULL) {
  4943. err("%s: usb_get_intfdata() failed", __FUNCTION__);
  4944. return;
  4945. }
  4946. usb_set_intfdata (intf, NULL);
  4947. down(&usbvision->lock);
  4948. // At this time we ask to cancel outstanding URBs
  4949. usbvision_stop_isoc(usbvision);
  4950. if (usbvision->power) {
  4951. usbvision_i2c_usb_del_bus(&usbvision->i2c_adap);
  4952. usbvision_power_off(usbvision);
  4953. }
  4954. usbvision->remove_pending = 1; // Now all ISO data will be ignored
  4955. usb_put_dev(usbvision->dev);
  4956. usbvision->dev = NULL; // USB device is no more
  4957. up(&usbvision->lock);
  4958. if (usbvision->user) {
  4959. info("%s: In use, disconnect pending", __FUNCTION__);
  4960. }
  4961. else {
  4962. usbvision_release(usbvision);
  4963. }
  4964. PDEBUG(DBG_PROBE, "success");
  4965. }
  4966. MODULE_DEVICE_TABLE (usb, usbvision_table);
  4967. static struct usb_driver usbvision_driver = {
  4968. .name = "usbvision",
  4969. .id_table = usbvision_table,
  4970. .probe = usbvision_probe,
  4971. .disconnect = usbvision_disconnect
  4972. };
  4973. /*
  4974. * customdevice_process()
  4975. *
  4976. * This procedure preprocesses CustomDevice parameter if any
  4977. *
  4978. */
  4979. void customdevice_process(void)
  4980. {
  4981. usbvision_device_data[0]=usbvision_device_data[1];
  4982. usbvision_table[0]=usbvision_table[1];
  4983. if(CustomDevice)
  4984. {
  4985. char *parse=CustomDevice;
  4986. PDEBUG(DBG_PROBE, "CustomDevide=%s", CustomDevice);
  4987. /*format is CustomDevice="0x0573 0x4D31 0 7113 3 PAL 1 1 1 5 -1 -1 -1 -1 -1"
  4988. usbvision_device_data[0].idVendor;
  4989. usbvision_device_data[0].idProduct;
  4990. usbvision_device_data[0].Interface;
  4991. usbvision_device_data[0].Codec;
  4992. usbvision_device_data[0].VideoChannels;
  4993. usbvision_device_data[0].VideoNorm;
  4994. usbvision_device_data[0].AudioChannels;
  4995. usbvision_device_data[0].Radio;
  4996. usbvision_device_data[0].Tuner;
  4997. usbvision_device_data[0].TunerType;
  4998. usbvision_device_data[0].Vin_Reg1;
  4999. usbvision_device_data[0].Vin_Reg2;
  5000. usbvision_device_data[0].X_Offset;
  5001. usbvision_device_data[0].Y_Offset;
  5002. usbvision_device_data[0].Dvi_yuv;
  5003. usbvision_device_data[0].ModelString;
  5004. */
  5005. rmspace(parse);
  5006. usbvision_device_data[0].ModelString="USBVISION Custom Device";
  5007. parse+=2;
  5008. sscanf(parse,"%x",&usbvision_device_data[0].idVendor);
  5009. goto2next(parse);
  5010. PDEBUG(DBG_PROBE, "idVendor=0x%.4X", usbvision_device_data[0].idVendor);
  5011. parse+=2;
  5012. sscanf(parse,"%x",&usbvision_device_data[0].idProduct);
  5013. goto2next(parse);
  5014. PDEBUG(DBG_PROBE, "idProduct=0x%.4X", usbvision_device_data[0].idProduct);
  5015. sscanf(parse,"%d",&usbvision_device_data[0].Interface);
  5016. goto2next(parse);
  5017. PDEBUG(DBG_PROBE, "Interface=%d", usbvision_device_data[0].Interface);
  5018. sscanf(parse,"%d",&usbvision_device_data[0].Codec);
  5019. goto2next(parse);
  5020. PDEBUG(DBG_PROBE, "Codec=%d", usbvision_device_data[0].Codec);
  5021. sscanf(parse,"%d",&usbvision_device_data[0].VideoChannels);
  5022. goto2next(parse);
  5023. PDEBUG(DBG_PROBE, "VideoChannels=%d", usbvision_device_data[0].VideoChannels);
  5024. switch(*parse)
  5025. {
  5026. case 'P':
  5027. PDEBUG(DBG_PROBE, "VideoNorm=PAL");
  5028. usbvision_device_data[0].VideoNorm=VIDEO_MODE_PAL;
  5029. break;
  5030. case 'S':
  5031. PDEBUG(DBG_PROBE, "VideoNorm=SECAM");
  5032. usbvision_device_data[0].VideoNorm=VIDEO_MODE_SECAM;
  5033. break;
  5034. case 'N':
  5035. PDEBUG(DBG_PROBE, "VideoNorm=NTSC");
  5036. usbvision_device_data[0].VideoNorm=VIDEO_MODE_NTSC;
  5037. break;
  5038. default:
  5039. PDEBUG(DBG_PROBE, "VideoNorm=PAL (by default)");
  5040. usbvision_device_data[0].VideoNorm=VIDEO_MODE_PAL;
  5041. break;
  5042. }
  5043. goto2next(parse);
  5044. sscanf(parse,"%d",&usbvision_device_data[0].AudioChannels);
  5045. goto2next(parse);
  5046. PDEBUG(DBG_PROBE, "AudioChannels=%d", usbvision_device_data[0].AudioChannels);
  5047. sscanf(parse,"%d",&usbvision_device_data[0].Radio);
  5048. goto2next(parse);
  5049. PDEBUG(DBG_PROBE, "Radio=%d", usbvision_device_data[0].Radio);
  5050. sscanf(parse,"%d",&usbvision_device_data[0].Tuner);
  5051. goto2next(parse);
  5052. PDEBUG(DBG_PROBE, "Tuner=%d", usbvision_device_data[0].Tuner);
  5053. sscanf(parse,"%d",&usbvision_device_data[0].TunerType);
  5054. goto2next(parse);
  5055. PDEBUG(DBG_PROBE, "TunerType=%d", usbvision_device_data[0].TunerType);
  5056. sscanf(parse,"%d",&usbvision_device_data[0].Vin_Reg1);
  5057. goto2next(parse);
  5058. PDEBUG(DBG_PROBE, "Vin_Reg1=%d", usbvision_device_data[0].Vin_Reg1);
  5059. sscanf(parse,"%d",&usbvision_device_data[0].Vin_Reg2);
  5060. goto2next(parse);
  5061. PDEBUG(DBG_PROBE, "Vin_Reg2=%d", usbvision_device_data[0].Vin_Reg2);
  5062. sscanf(parse,"%d",&usbvision_device_data[0].X_Offset);
  5063. goto2next(parse);
  5064. PDEBUG(DBG_PROBE, "X_Offset=%d", usbvision_device_data[0].X_Offset);
  5065. sscanf(parse,"%d",&usbvision_device_data[0].Y_Offset);
  5066. goto2next(parse);
  5067. PDEBUG(DBG_PROBE, "Y_Offset=%d", usbvision_device_data[0].Y_Offset);
  5068. sscanf(parse,"%d",&usbvision_device_data[0].Dvi_yuv);
  5069. PDEBUG(DBG_PROBE, "Dvi_yuv=%d", usbvision_device_data[0].Dvi_yuv);
  5070. //add to usbvision_table also
  5071. usbvision_table[0].match_flags=USB_DEVICE_ID_MATCH_DEVICE;
  5072. usbvision_table[0].idVendor=usbvision_device_data[0].idVendor;
  5073. usbvision_table[0].idProduct=usbvision_device_data[0].idProduct;
  5074. }
  5075. }
  5076. /*
  5077. * usbvision_init()
  5078. *
  5079. * This code is run to initialize the driver.
  5080. *
  5081. */
  5082. static int __init usbvision_init(void)
  5083. {
  5084. int errCode;
  5085. PDEBUG(DBG_PROBE, "");
  5086. PDEBUG(DBG_IOCTL, "IOCTL debugging is enabled");
  5087. PDEBUG(DBG_IO, "IO debugging is enabled");
  5088. PDEBUG(DBG_RIO, "RIO debugging is enabled");
  5089. PDEBUG(DBG_HEADER, "HEADER debugging is enabled");
  5090. PDEBUG(DBG_PROBE, "PROBE debugging is enabled");
  5091. PDEBUG(DBG_IRQ, "IRQ debugging is enabled");
  5092. PDEBUG(DBG_ISOC, "ISOC debugging is enabled");
  5093. PDEBUG(DBG_PARSE, "PARSE debugging is enabled");
  5094. PDEBUG(DBG_SCRATCH, "SCRATCH debugging is enabled");
  5095. PDEBUG(DBG_FUNC, "FUNC debugging is enabled");
  5096. PDEBUG(DBG_I2C, "I2C debugging is enabled");
  5097. /* disable planar mode support unless compression enabled */
  5098. if (isocMode != ISOC_MODE_COMPRESS ) {
  5099. // FIXME : not the right way to set supported flag
  5100. usbvision_v4l2_format[6].supported = 0; // V4L2_PIX_FMT_YVU420
  5101. usbvision_v4l2_format[7].supported = 0; // V4L2_PIX_FMT_YUV422P
  5102. }
  5103. customdevice_process();
  5104. errCode = usb_register(&usbvision_driver);
  5105. if (errCode == 0) {
  5106. info(DRIVER_DESC " : " DRIVER_VERSION);
  5107. PDEBUG(DBG_PROBE, "success");
  5108. }
  5109. return errCode;
  5110. }
  5111. static void __exit usbvision_exit(void)
  5112. {
  5113. PDEBUG(DBG_PROBE, "");
  5114. usb_deregister(&usbvision_driver);
  5115. PDEBUG(DBG_PROBE, "success");
  5116. }
  5117. module_init(usbvision_init);
  5118. module_exit(usbvision_exit);
  5119. /*
  5120. * Overrides for Emacs so that we follow Linus's tabbing style.
  5121. * ---------------------------------------------------------------------------
  5122. * Local variables:
  5123. * c-basic-offset: 8
  5124. * End:
  5125. */