usbvision-core.c 174 KB

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