usbvision-core.c 175 KB

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