usbvision-core.c 175 KB

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