usbvision-core.c 171 KB

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