usbvision-core.c 174 KB

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