usbvision-core.c 173 KB

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