bttv-driver.c 116 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455
  1. /*
  2. bttv - Bt848 frame grabber driver
  3. Copyright (C) 1996,97,98 Ralph Metzler <rjkm@thp.uni-koeln.de>
  4. & Marcus Metzler <mocm@thp.uni-koeln.de>
  5. (c) 1999-2002 Gerd Knorr <kraxel@bytesex.org>
  6. some v4l2 code lines are taken from Justin's bttv2 driver which is
  7. (c) 2000 Justin Schoeman <justin@suntiger.ee.up.ac.za>
  8. V4L1 removal from:
  9. (c) 2005-2006 Nickolay V. Shmyrev <nshmyrev@yandex.ru>
  10. Fixes to be fully V4L2 compliant by
  11. (c) 2006 Mauro Carvalho Chehab <mchehab@infradead.org>
  12. Cropping and overscan support
  13. Copyright (C) 2005, 2006 Michael H. Schimek <mschimek@gmx.at>
  14. Sponsored by OPQ Systems AB
  15. This program is free software; you can redistribute it and/or modify
  16. it under the terms of the GNU General Public License as published by
  17. the Free Software Foundation; either version 2 of the License, or
  18. (at your option) any later version.
  19. This program is distributed in the hope that it will be useful,
  20. but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. GNU General Public License for more details.
  23. You should have received a copy of the GNU General Public License
  24. along with this program; if not, write to the Free Software
  25. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  26. */
  27. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  28. #include <linux/init.h>
  29. #include <linux/module.h>
  30. #include <linux/delay.h>
  31. #include <linux/slab.h>
  32. #include <linux/errno.h>
  33. #include <linux/fs.h>
  34. #include <linux/kernel.h>
  35. #include <linux/sched.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/kdev_t.h>
  38. #include "bttvp.h"
  39. #include <media/v4l2-common.h>
  40. #include <media/v4l2-ioctl.h>
  41. #include <media/v4l2-event.h>
  42. #include <media/i2c/tvaudio.h>
  43. #include <media/drv-intf/msp3400.h>
  44. #include <linux/dma-mapping.h>
  45. #include <asm/io.h>
  46. #include <asm/byteorder.h>
  47. #include <media/i2c/saa6588.h>
  48. #define BTTV_VERSION "0.9.19"
  49. unsigned int bttv_num; /* number of Bt848s in use */
  50. struct bttv *bttvs[BTTV_MAX];
  51. unsigned int bttv_debug;
  52. unsigned int bttv_verbose = 1;
  53. unsigned int bttv_gpio;
  54. /* config variables */
  55. #ifdef __BIG_ENDIAN
  56. static unsigned int bigendian=1;
  57. #else
  58. static unsigned int bigendian;
  59. #endif
  60. static unsigned int radio[BTTV_MAX];
  61. static unsigned int irq_debug;
  62. static unsigned int gbuffers = 8;
  63. static unsigned int gbufsize = 0x208000;
  64. static unsigned int reset_crop = 1;
  65. static int video_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
  66. static int radio_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
  67. static int vbi_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
  68. static int debug_latency;
  69. static int disable_ir;
  70. static unsigned int fdsr;
  71. /* options */
  72. static unsigned int combfilter;
  73. static unsigned int lumafilter;
  74. static unsigned int automute = 1;
  75. static unsigned int chroma_agc;
  76. static unsigned int agc_crush = 1;
  77. static unsigned int whitecrush_upper = 0xCF;
  78. static unsigned int whitecrush_lower = 0x7F;
  79. static unsigned int vcr_hack;
  80. static unsigned int irq_iswitch;
  81. static unsigned int uv_ratio = 50;
  82. static unsigned int full_luma_range;
  83. static unsigned int coring;
  84. /* API features (turn on/off stuff for testing) */
  85. static unsigned int v4l2 = 1;
  86. /* insmod args */
  87. module_param(bttv_verbose, int, 0644);
  88. module_param(bttv_gpio, int, 0644);
  89. module_param(bttv_debug, int, 0644);
  90. module_param(irq_debug, int, 0644);
  91. module_param(debug_latency, int, 0644);
  92. module_param(disable_ir, int, 0444);
  93. module_param(fdsr, int, 0444);
  94. module_param(gbuffers, int, 0444);
  95. module_param(gbufsize, int, 0444);
  96. module_param(reset_crop, int, 0444);
  97. module_param(v4l2, int, 0644);
  98. module_param(bigendian, int, 0644);
  99. module_param(irq_iswitch, int, 0644);
  100. module_param(combfilter, int, 0444);
  101. module_param(lumafilter, int, 0444);
  102. module_param(automute, int, 0444);
  103. module_param(chroma_agc, int, 0444);
  104. module_param(agc_crush, int, 0444);
  105. module_param(whitecrush_upper, int, 0444);
  106. module_param(whitecrush_lower, int, 0444);
  107. module_param(vcr_hack, int, 0444);
  108. module_param(uv_ratio, int, 0444);
  109. module_param(full_luma_range, int, 0444);
  110. module_param(coring, int, 0444);
  111. module_param_array(radio, int, NULL, 0444);
  112. module_param_array(video_nr, int, NULL, 0444);
  113. module_param_array(radio_nr, int, NULL, 0444);
  114. module_param_array(vbi_nr, int, NULL, 0444);
  115. MODULE_PARM_DESC(radio, "The TV card supports radio, default is 0 (no)");
  116. MODULE_PARM_DESC(bigendian, "byte order of the framebuffer, default is native endian");
  117. MODULE_PARM_DESC(bttv_verbose, "verbose startup messages, default is 1 (yes)");
  118. MODULE_PARM_DESC(bttv_gpio, "log gpio changes, default is 0 (no)");
  119. MODULE_PARM_DESC(bttv_debug, "debug messages, default is 0 (no)");
  120. MODULE_PARM_DESC(irq_debug, "irq handler debug messages, default is 0 (no)");
  121. MODULE_PARM_DESC(disable_ir, "disable infrared remote support");
  122. MODULE_PARM_DESC(gbuffers, "number of capture buffers. range 2-32, default 8");
  123. MODULE_PARM_DESC(gbufsize, "size of the capture buffers, default is 0x208000");
  124. MODULE_PARM_DESC(reset_crop, "reset cropping parameters at open(), default "
  125. "is 1 (yes) for compatibility with older applications");
  126. MODULE_PARM_DESC(automute, "mute audio on bad/missing video signal, default is 1 (yes)");
  127. MODULE_PARM_DESC(chroma_agc, "enables the AGC of chroma signal, default is 0 (no)");
  128. MODULE_PARM_DESC(agc_crush, "enables the luminance AGC crush, default is 1 (yes)");
  129. MODULE_PARM_DESC(whitecrush_upper, "sets the white crush upper value, default is 207");
  130. MODULE_PARM_DESC(whitecrush_lower, "sets the white crush lower value, default is 127");
  131. MODULE_PARM_DESC(vcr_hack, "enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
  132. MODULE_PARM_DESC(irq_iswitch, "switch inputs in irq handler");
  133. MODULE_PARM_DESC(uv_ratio, "ratio between u and v gains, default is 50");
  134. MODULE_PARM_DESC(full_luma_range, "use the full luma range, default is 0 (no)");
  135. MODULE_PARM_DESC(coring, "set the luma coring level, default is 0 (no)");
  136. MODULE_PARM_DESC(video_nr, "video device numbers");
  137. MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
  138. MODULE_PARM_DESC(radio_nr, "radio device numbers");
  139. MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
  140. MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
  141. MODULE_LICENSE("GPL");
  142. MODULE_VERSION(BTTV_VERSION);
  143. #define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_USER_BTTV_BASE + 0)
  144. #define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_USER_BTTV_BASE + 1)
  145. #define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_USER_BTTV_BASE + 2)
  146. #define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_USER_BTTV_BASE + 3)
  147. #define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_USER_BTTV_BASE + 4)
  148. #define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_USER_BTTV_BASE + 5)
  149. #define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_USER_BTTV_BASE + 6)
  150. #define V4L2_CID_PRIVATE_UV_RATIO (V4L2_CID_USER_BTTV_BASE + 7)
  151. #define V4L2_CID_PRIVATE_FULL_LUMA_RANGE (V4L2_CID_USER_BTTV_BASE + 8)
  152. #define V4L2_CID_PRIVATE_CORING (V4L2_CID_USER_BTTV_BASE + 9)
  153. /* ----------------------------------------------------------------------- */
  154. /* sysfs */
  155. static ssize_t show_card(struct device *cd,
  156. struct device_attribute *attr, char *buf)
  157. {
  158. struct video_device *vfd = to_video_device(cd);
  159. struct bttv *btv = video_get_drvdata(vfd);
  160. return sprintf(buf, "%d\n", btv ? btv->c.type : UNSET);
  161. }
  162. static DEVICE_ATTR(card, S_IRUGO, show_card, NULL);
  163. /* ----------------------------------------------------------------------- */
  164. /* dvb auto-load setup */
  165. #if defined(CONFIG_MODULES) && defined(MODULE)
  166. static void request_module_async(struct work_struct *work)
  167. {
  168. request_module("dvb-bt8xx");
  169. }
  170. static void request_modules(struct bttv *dev)
  171. {
  172. INIT_WORK(&dev->request_module_wk, request_module_async);
  173. schedule_work(&dev->request_module_wk);
  174. }
  175. static void flush_request_modules(struct bttv *dev)
  176. {
  177. flush_work(&dev->request_module_wk);
  178. }
  179. #else
  180. #define request_modules(dev)
  181. #define flush_request_modules(dev) do {} while(0)
  182. #endif /* CONFIG_MODULES */
  183. /* ----------------------------------------------------------------------- */
  184. /* static data */
  185. /* special timing tables from conexant... */
  186. static u8 SRAM_Table[][60] =
  187. {
  188. /* PAL digital input over GPIO[7:0] */
  189. {
  190. 45, // 45 bytes following
  191. 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
  192. 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
  193. 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
  194. 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
  195. 0x37,0x00,0xAF,0x21,0x00
  196. },
  197. /* NTSC digital input over GPIO[7:0] */
  198. {
  199. 51, // 51 bytes following
  200. 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
  201. 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
  202. 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
  203. 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
  204. 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
  205. 0x00,
  206. },
  207. // TGB_NTSC392 // quartzsight
  208. // This table has been modified to be used for Fusion Rev D
  209. {
  210. 0x2A, // size of table = 42
  211. 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
  212. 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
  213. 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
  214. 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
  215. 0x20, 0x00
  216. }
  217. };
  218. /* minhdelayx1 first video pixel we can capture on a line and
  219. hdelayx1 start of active video, both relative to rising edge of
  220. /HRESET pulse (0H) in 1 / fCLKx1.
  221. swidth width of active video and
  222. totalwidth total line width, both in 1 / fCLKx1.
  223. sqwidth total line width in square pixels.
  224. vdelay start of active video in 2 * field lines relative to
  225. trailing edge of /VRESET pulse (VDELAY register).
  226. sheight height of active video in 2 * field lines.
  227. extraheight Added to sheight for cropcap.bounds.height only
  228. videostart0 ITU-R frame line number of the line corresponding
  229. to vdelay in the first field. */
  230. #define CROPCAP(minhdelayx1, hdelayx1, swidth, totalwidth, sqwidth, \
  231. vdelay, sheight, extraheight, videostart0) \
  232. .cropcap.bounds.left = minhdelayx1, \
  233. /* * 2 because vertically we count field lines times two, */ \
  234. /* e.g. 23 * 2 to 23 * 2 + 576 in PAL-BGHI defrect. */ \
  235. .cropcap.bounds.top = (videostart0) * 2 - (vdelay) + MIN_VDELAY, \
  236. /* 4 is a safety margin at the end of the line. */ \
  237. .cropcap.bounds.width = (totalwidth) - (minhdelayx1) - 4, \
  238. .cropcap.bounds.height = (sheight) + (extraheight) + (vdelay) - \
  239. MIN_VDELAY, \
  240. .cropcap.defrect.left = hdelayx1, \
  241. .cropcap.defrect.top = (videostart0) * 2, \
  242. .cropcap.defrect.width = swidth, \
  243. .cropcap.defrect.height = sheight, \
  244. .cropcap.pixelaspect.numerator = totalwidth, \
  245. .cropcap.pixelaspect.denominator = sqwidth,
  246. const struct bttv_tvnorm bttv_tvnorms[] = {
  247. /* PAL-BDGHI */
  248. /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
  249. /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
  250. {
  251. .v4l2_id = V4L2_STD_PAL,
  252. .name = "PAL",
  253. .Fsc = 35468950,
  254. .swidth = 924,
  255. .sheight = 576,
  256. .totalwidth = 1135,
  257. .adelay = 0x7f,
  258. .bdelay = 0x72,
  259. .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
  260. .scaledtwidth = 1135,
  261. .hdelayx1 = 186,
  262. .hactivex1 = 924,
  263. .vdelay = 0x20,
  264. .vbipack = 255, /* min (2048 / 4, 0x1ff) & 0xff */
  265. .sram = 0,
  266. /* ITU-R frame line number of the first VBI line
  267. we can capture, of the first and second field.
  268. The last line is determined by cropcap.bounds. */
  269. .vbistart = { 7, 320 },
  270. CROPCAP(/* minhdelayx1 */ 68,
  271. /* hdelayx1 */ 186,
  272. /* Should be (768 * 1135 + 944 / 2) / 944.
  273. cropcap.defrect is used for image width
  274. checks, so we keep the old value 924. */
  275. /* swidth */ 924,
  276. /* totalwidth */ 1135,
  277. /* sqwidth */ 944,
  278. /* vdelay */ 0x20,
  279. /* sheight */ 576,
  280. /* bt878 (and bt848?) can capture another
  281. line below active video. */
  282. /* extraheight */ 2,
  283. /* videostart0 */ 23)
  284. },{
  285. .v4l2_id = V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_KR,
  286. .name = "NTSC",
  287. .Fsc = 28636363,
  288. .swidth = 768,
  289. .sheight = 480,
  290. .totalwidth = 910,
  291. .adelay = 0x68,
  292. .bdelay = 0x5d,
  293. .iform = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
  294. .scaledtwidth = 910,
  295. .hdelayx1 = 128,
  296. .hactivex1 = 910,
  297. .vdelay = 0x1a,
  298. .vbipack = 144, /* min (1600 / 4, 0x1ff) & 0xff */
  299. .sram = 1,
  300. .vbistart = { 10, 273 },
  301. CROPCAP(/* minhdelayx1 */ 68,
  302. /* hdelayx1 */ 128,
  303. /* Should be (640 * 910 + 780 / 2) / 780? */
  304. /* swidth */ 768,
  305. /* totalwidth */ 910,
  306. /* sqwidth */ 780,
  307. /* vdelay */ 0x1a,
  308. /* sheight */ 480,
  309. /* extraheight */ 0,
  310. /* videostart0 */ 23)
  311. },{
  312. .v4l2_id = V4L2_STD_SECAM,
  313. .name = "SECAM",
  314. .Fsc = 35468950,
  315. .swidth = 924,
  316. .sheight = 576,
  317. .totalwidth = 1135,
  318. .adelay = 0x7f,
  319. .bdelay = 0xb0,
  320. .iform = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
  321. .scaledtwidth = 1135,
  322. .hdelayx1 = 186,
  323. .hactivex1 = 922,
  324. .vdelay = 0x20,
  325. .vbipack = 255,
  326. .sram = 0, /* like PAL, correct? */
  327. .vbistart = { 7, 320 },
  328. CROPCAP(/* minhdelayx1 */ 68,
  329. /* hdelayx1 */ 186,
  330. /* swidth */ 924,
  331. /* totalwidth */ 1135,
  332. /* sqwidth */ 944,
  333. /* vdelay */ 0x20,
  334. /* sheight */ 576,
  335. /* extraheight */ 0,
  336. /* videostart0 */ 23)
  337. },{
  338. .v4l2_id = V4L2_STD_PAL_Nc,
  339. .name = "PAL-Nc",
  340. .Fsc = 28636363,
  341. .swidth = 640,
  342. .sheight = 576,
  343. .totalwidth = 910,
  344. .adelay = 0x68,
  345. .bdelay = 0x5d,
  346. .iform = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
  347. .scaledtwidth = 780,
  348. .hdelayx1 = 130,
  349. .hactivex1 = 734,
  350. .vdelay = 0x1a,
  351. .vbipack = 144,
  352. .sram = -1,
  353. .vbistart = { 7, 320 },
  354. CROPCAP(/* minhdelayx1 */ 68,
  355. /* hdelayx1 */ 130,
  356. /* swidth */ (640 * 910 + 780 / 2) / 780,
  357. /* totalwidth */ 910,
  358. /* sqwidth */ 780,
  359. /* vdelay */ 0x1a,
  360. /* sheight */ 576,
  361. /* extraheight */ 0,
  362. /* videostart0 */ 23)
  363. },{
  364. .v4l2_id = V4L2_STD_PAL_M,
  365. .name = "PAL-M",
  366. .Fsc = 28636363,
  367. .swidth = 640,
  368. .sheight = 480,
  369. .totalwidth = 910,
  370. .adelay = 0x68,
  371. .bdelay = 0x5d,
  372. .iform = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
  373. .scaledtwidth = 780,
  374. .hdelayx1 = 135,
  375. .hactivex1 = 754,
  376. .vdelay = 0x1a,
  377. .vbipack = 144,
  378. .sram = -1,
  379. .vbistart = { 10, 273 },
  380. CROPCAP(/* minhdelayx1 */ 68,
  381. /* hdelayx1 */ 135,
  382. /* swidth */ (640 * 910 + 780 / 2) / 780,
  383. /* totalwidth */ 910,
  384. /* sqwidth */ 780,
  385. /* vdelay */ 0x1a,
  386. /* sheight */ 480,
  387. /* extraheight */ 0,
  388. /* videostart0 */ 23)
  389. },{
  390. .v4l2_id = V4L2_STD_PAL_N,
  391. .name = "PAL-N",
  392. .Fsc = 35468950,
  393. .swidth = 768,
  394. .sheight = 576,
  395. .totalwidth = 1135,
  396. .adelay = 0x7f,
  397. .bdelay = 0x72,
  398. .iform = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
  399. .scaledtwidth = 944,
  400. .hdelayx1 = 186,
  401. .hactivex1 = 922,
  402. .vdelay = 0x20,
  403. .vbipack = 144,
  404. .sram = -1,
  405. .vbistart = { 7, 320 },
  406. CROPCAP(/* minhdelayx1 */ 68,
  407. /* hdelayx1 */ 186,
  408. /* swidth */ (768 * 1135 + 944 / 2) / 944,
  409. /* totalwidth */ 1135,
  410. /* sqwidth */ 944,
  411. /* vdelay */ 0x20,
  412. /* sheight */ 576,
  413. /* extraheight */ 0,
  414. /* videostart0 */ 23)
  415. },{
  416. .v4l2_id = V4L2_STD_NTSC_M_JP,
  417. .name = "NTSC-JP",
  418. .Fsc = 28636363,
  419. .swidth = 640,
  420. .sheight = 480,
  421. .totalwidth = 910,
  422. .adelay = 0x68,
  423. .bdelay = 0x5d,
  424. .iform = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
  425. .scaledtwidth = 780,
  426. .hdelayx1 = 135,
  427. .hactivex1 = 754,
  428. .vdelay = 0x16,
  429. .vbipack = 144,
  430. .sram = -1,
  431. .vbistart = { 10, 273 },
  432. CROPCAP(/* minhdelayx1 */ 68,
  433. /* hdelayx1 */ 135,
  434. /* swidth */ (640 * 910 + 780 / 2) / 780,
  435. /* totalwidth */ 910,
  436. /* sqwidth */ 780,
  437. /* vdelay */ 0x16,
  438. /* sheight */ 480,
  439. /* extraheight */ 0,
  440. /* videostart0 */ 23)
  441. },{
  442. /* that one hopefully works with the strange timing
  443. * which video recorders produce when playing a NTSC
  444. * tape on a PAL TV ... */
  445. .v4l2_id = V4L2_STD_PAL_60,
  446. .name = "PAL-60",
  447. .Fsc = 35468950,
  448. .swidth = 924,
  449. .sheight = 480,
  450. .totalwidth = 1135,
  451. .adelay = 0x7f,
  452. .bdelay = 0x72,
  453. .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
  454. .scaledtwidth = 1135,
  455. .hdelayx1 = 186,
  456. .hactivex1 = 924,
  457. .vdelay = 0x1a,
  458. .vbipack = 255,
  459. .vtotal = 524,
  460. .sram = -1,
  461. .vbistart = { 10, 273 },
  462. CROPCAP(/* minhdelayx1 */ 68,
  463. /* hdelayx1 */ 186,
  464. /* swidth */ 924,
  465. /* totalwidth */ 1135,
  466. /* sqwidth */ 944,
  467. /* vdelay */ 0x1a,
  468. /* sheight */ 480,
  469. /* extraheight */ 0,
  470. /* videostart0 */ 23)
  471. }
  472. };
  473. static const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms);
  474. /* ----------------------------------------------------------------------- */
  475. /* bttv format list
  476. packed pixel formats must come first */
  477. static const struct bttv_format formats[] = {
  478. {
  479. .name = "8 bpp, gray",
  480. .fourcc = V4L2_PIX_FMT_GREY,
  481. .btformat = BT848_COLOR_FMT_Y8,
  482. .depth = 8,
  483. .flags = FORMAT_FLAGS_PACKED,
  484. },{
  485. .name = "8 bpp, dithered color",
  486. .fourcc = V4L2_PIX_FMT_HI240,
  487. .btformat = BT848_COLOR_FMT_RGB8,
  488. .depth = 8,
  489. .flags = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER,
  490. },{
  491. .name = "15 bpp RGB, le",
  492. .fourcc = V4L2_PIX_FMT_RGB555,
  493. .btformat = BT848_COLOR_FMT_RGB15,
  494. .depth = 16,
  495. .flags = FORMAT_FLAGS_PACKED,
  496. },{
  497. .name = "15 bpp RGB, be",
  498. .fourcc = V4L2_PIX_FMT_RGB555X,
  499. .btformat = BT848_COLOR_FMT_RGB15,
  500. .btswap = 0x03, /* byteswap */
  501. .depth = 16,
  502. .flags = FORMAT_FLAGS_PACKED,
  503. },{
  504. .name = "16 bpp RGB, le",
  505. .fourcc = V4L2_PIX_FMT_RGB565,
  506. .btformat = BT848_COLOR_FMT_RGB16,
  507. .depth = 16,
  508. .flags = FORMAT_FLAGS_PACKED,
  509. },{
  510. .name = "16 bpp RGB, be",
  511. .fourcc = V4L2_PIX_FMT_RGB565X,
  512. .btformat = BT848_COLOR_FMT_RGB16,
  513. .btswap = 0x03, /* byteswap */
  514. .depth = 16,
  515. .flags = FORMAT_FLAGS_PACKED,
  516. },{
  517. .name = "24 bpp RGB, le",
  518. .fourcc = V4L2_PIX_FMT_BGR24,
  519. .btformat = BT848_COLOR_FMT_RGB24,
  520. .depth = 24,
  521. .flags = FORMAT_FLAGS_PACKED,
  522. },{
  523. .name = "32 bpp RGB, le",
  524. .fourcc = V4L2_PIX_FMT_BGR32,
  525. .btformat = BT848_COLOR_FMT_RGB32,
  526. .depth = 32,
  527. .flags = FORMAT_FLAGS_PACKED,
  528. },{
  529. .name = "32 bpp RGB, be",
  530. .fourcc = V4L2_PIX_FMT_RGB32,
  531. .btformat = BT848_COLOR_FMT_RGB32,
  532. .btswap = 0x0f, /* byte+word swap */
  533. .depth = 32,
  534. .flags = FORMAT_FLAGS_PACKED,
  535. },{
  536. .name = "4:2:2, packed, YUYV",
  537. .fourcc = V4L2_PIX_FMT_YUYV,
  538. .btformat = BT848_COLOR_FMT_YUY2,
  539. .depth = 16,
  540. .flags = FORMAT_FLAGS_PACKED,
  541. },{
  542. .name = "4:2:2, packed, UYVY",
  543. .fourcc = V4L2_PIX_FMT_UYVY,
  544. .btformat = BT848_COLOR_FMT_YUY2,
  545. .btswap = 0x03, /* byteswap */
  546. .depth = 16,
  547. .flags = FORMAT_FLAGS_PACKED,
  548. },{
  549. .name = "4:2:2, planar, Y-Cb-Cr",
  550. .fourcc = V4L2_PIX_FMT_YUV422P,
  551. .btformat = BT848_COLOR_FMT_YCrCb422,
  552. .depth = 16,
  553. .flags = FORMAT_FLAGS_PLANAR,
  554. .hshift = 1,
  555. .vshift = 0,
  556. },{
  557. .name = "4:2:0, planar, Y-Cb-Cr",
  558. .fourcc = V4L2_PIX_FMT_YUV420,
  559. .btformat = BT848_COLOR_FMT_YCrCb422,
  560. .depth = 12,
  561. .flags = FORMAT_FLAGS_PLANAR,
  562. .hshift = 1,
  563. .vshift = 1,
  564. },{
  565. .name = "4:2:0, planar, Y-Cr-Cb",
  566. .fourcc = V4L2_PIX_FMT_YVU420,
  567. .btformat = BT848_COLOR_FMT_YCrCb422,
  568. .depth = 12,
  569. .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
  570. .hshift = 1,
  571. .vshift = 1,
  572. },{
  573. .name = "4:1:1, planar, Y-Cb-Cr",
  574. .fourcc = V4L2_PIX_FMT_YUV411P,
  575. .btformat = BT848_COLOR_FMT_YCrCb411,
  576. .depth = 12,
  577. .flags = FORMAT_FLAGS_PLANAR,
  578. .hshift = 2,
  579. .vshift = 0,
  580. },{
  581. .name = "4:1:0, planar, Y-Cb-Cr",
  582. .fourcc = V4L2_PIX_FMT_YUV410,
  583. .btformat = BT848_COLOR_FMT_YCrCb411,
  584. .depth = 9,
  585. .flags = FORMAT_FLAGS_PLANAR,
  586. .hshift = 2,
  587. .vshift = 2,
  588. },{
  589. .name = "4:1:0, planar, Y-Cr-Cb",
  590. .fourcc = V4L2_PIX_FMT_YVU410,
  591. .btformat = BT848_COLOR_FMT_YCrCb411,
  592. .depth = 9,
  593. .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
  594. .hshift = 2,
  595. .vshift = 2,
  596. },{
  597. .name = "raw scanlines",
  598. .fourcc = -1,
  599. .btformat = BT848_COLOR_FMT_RAW,
  600. .depth = 8,
  601. .flags = FORMAT_FLAGS_RAW,
  602. }
  603. };
  604. static const unsigned int FORMATS = ARRAY_SIZE(formats);
  605. /* ----------------------------------------------------------------------- */
  606. /* resource management */
  607. /*
  608. RESOURCE_ allocated by freed by
  609. VIDEO_READ bttv_read 1) bttv_read 2)
  610. VIDEO_STREAM VIDIOC_STREAMON VIDIOC_STREAMOFF
  611. VIDIOC_QBUF 1) bttv_release
  612. VIDIOCMCAPTURE 1)
  613. OVERLAY VIDIOCCAPTURE on VIDIOCCAPTURE off
  614. VIDIOC_OVERLAY on VIDIOC_OVERLAY off
  615. 3) bttv_release
  616. VBI VIDIOC_STREAMON VIDIOC_STREAMOFF
  617. VIDIOC_QBUF 1) bttv_release
  618. bttv_read, bttv_poll 1) 4)
  619. 1) The resource must be allocated when we enter buffer prepare functions
  620. and remain allocated while buffers are in the DMA queue.
  621. 2) This is a single frame read.
  622. 3) VIDIOC_S_FBUF and VIDIOC_S_FMT (OVERLAY) still work when
  623. RESOURCE_OVERLAY is allocated.
  624. 4) This is a continuous read, implies VIDIOC_STREAMON.
  625. Note this driver permits video input and standard changes regardless if
  626. resources are allocated.
  627. */
  628. #define VBI_RESOURCES (RESOURCE_VBI)
  629. #define VIDEO_RESOURCES (RESOURCE_VIDEO_READ | \
  630. RESOURCE_VIDEO_STREAM | \
  631. RESOURCE_OVERLAY)
  632. static
  633. int check_alloc_btres_lock(struct bttv *btv, struct bttv_fh *fh, int bit)
  634. {
  635. int xbits; /* mutual exclusive resources */
  636. if (fh->resources & bit)
  637. /* have it already allocated */
  638. return 1;
  639. xbits = bit;
  640. if (bit & (RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM))
  641. xbits |= RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM;
  642. /* is it free? */
  643. if (btv->resources & xbits) {
  644. /* no, someone else uses it */
  645. goto fail;
  646. }
  647. if ((bit & VIDEO_RESOURCES)
  648. && 0 == (btv->resources & VIDEO_RESOURCES)) {
  649. /* Do crop - use current, don't - use default parameters. */
  650. __s32 top = btv->crop[!!fh->do_crop].rect.top;
  651. if (btv->vbi_end > top)
  652. goto fail;
  653. /* We cannot capture the same line as video and VBI data.
  654. Claim scan lines crop[].rect.top to bottom. */
  655. btv->crop_start = top;
  656. } else if (bit & VBI_RESOURCES) {
  657. __s32 end = fh->vbi_fmt.end;
  658. if (end > btv->crop_start)
  659. goto fail;
  660. /* Claim scan lines above fh->vbi_fmt.end. */
  661. btv->vbi_end = end;
  662. }
  663. /* it's free, grab it */
  664. fh->resources |= bit;
  665. btv->resources |= bit;
  666. return 1;
  667. fail:
  668. return 0;
  669. }
  670. static
  671. int check_btres(struct bttv_fh *fh, int bit)
  672. {
  673. return (fh->resources & bit);
  674. }
  675. static
  676. int locked_btres(struct bttv *btv, int bit)
  677. {
  678. return (btv->resources & bit);
  679. }
  680. /* Call with btv->lock down. */
  681. static void
  682. disclaim_vbi_lines(struct bttv *btv)
  683. {
  684. btv->vbi_end = 0;
  685. }
  686. /* Call with btv->lock down. */
  687. static void
  688. disclaim_video_lines(struct bttv *btv)
  689. {
  690. const struct bttv_tvnorm *tvnorm;
  691. u8 crop;
  692. tvnorm = &bttv_tvnorms[btv->tvnorm];
  693. btv->crop_start = tvnorm->cropcap.bounds.top
  694. + tvnorm->cropcap.bounds.height;
  695. /* VBI capturing ends at VDELAY, start of video capturing, no
  696. matter how many lines the VBI RISC program expects. When video
  697. capturing is off, it shall no longer "preempt" VBI capturing,
  698. so we set VDELAY to maximum. */
  699. crop = btread(BT848_E_CROP) | 0xc0;
  700. btwrite(crop, BT848_E_CROP);
  701. btwrite(0xfe, BT848_E_VDELAY_LO);
  702. btwrite(crop, BT848_O_CROP);
  703. btwrite(0xfe, BT848_O_VDELAY_LO);
  704. }
  705. static
  706. void free_btres_lock(struct bttv *btv, struct bttv_fh *fh, int bits)
  707. {
  708. if ((fh->resources & bits) != bits) {
  709. /* trying to free resources not allocated by us ... */
  710. pr_err("BUG! (btres)\n");
  711. }
  712. fh->resources &= ~bits;
  713. btv->resources &= ~bits;
  714. bits = btv->resources;
  715. if (0 == (bits & VIDEO_RESOURCES))
  716. disclaim_video_lines(btv);
  717. if (0 == (bits & VBI_RESOURCES))
  718. disclaim_vbi_lines(btv);
  719. }
  720. /* ----------------------------------------------------------------------- */
  721. /* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
  722. /* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
  723. PLL_X = Reference pre-divider (0=1, 1=2)
  724. PLL_C = Post divider (0=6, 1=4)
  725. PLL_I = Integer input
  726. PLL_F = Fractional input
  727. F_input = 28.636363 MHz:
  728. PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
  729. */
  730. static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
  731. {
  732. unsigned char fl, fh, fi;
  733. /* prevent overflows */
  734. fin/=4;
  735. fout/=4;
  736. fout*=12;
  737. fi=fout/fin;
  738. fout=(fout%fin)*256;
  739. fh=fout/fin;
  740. fout=(fout%fin)*256;
  741. fl=fout/fin;
  742. btwrite(fl, BT848_PLL_F_LO);
  743. btwrite(fh, BT848_PLL_F_HI);
  744. btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
  745. }
  746. static void set_pll(struct bttv *btv)
  747. {
  748. int i;
  749. if (!btv->pll.pll_crystal)
  750. return;
  751. if (btv->pll.pll_ofreq == btv->pll.pll_current) {
  752. dprintk("%d: PLL: no change required\n", btv->c.nr);
  753. return;
  754. }
  755. if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
  756. /* no PLL needed */
  757. if (btv->pll.pll_current == 0)
  758. return;
  759. if (bttv_verbose)
  760. pr_info("%d: PLL can sleep, using XTAL (%d)\n",
  761. btv->c.nr, btv->pll.pll_ifreq);
  762. btwrite(0x00,BT848_TGCTRL);
  763. btwrite(0x00,BT848_PLL_XCI);
  764. btv->pll.pll_current = 0;
  765. return;
  766. }
  767. if (bttv_verbose)
  768. pr_info("%d: Setting PLL: %d => %d (needs up to 100ms)\n",
  769. btv->c.nr,
  770. btv->pll.pll_ifreq, btv->pll.pll_ofreq);
  771. set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
  772. for (i=0; i<10; i++) {
  773. /* Let other people run while the PLL stabilizes */
  774. msleep(10);
  775. if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
  776. btwrite(0,BT848_DSTATUS);
  777. } else {
  778. btwrite(0x08,BT848_TGCTRL);
  779. btv->pll.pll_current = btv->pll.pll_ofreq;
  780. if (bttv_verbose)
  781. pr_info("PLL set ok\n");
  782. return;
  783. }
  784. }
  785. btv->pll.pll_current = -1;
  786. if (bttv_verbose)
  787. pr_info("Setting PLL failed\n");
  788. return;
  789. }
  790. /* used to switch between the bt848's analog/digital video capture modes */
  791. static void bt848A_set_timing(struct bttv *btv)
  792. {
  793. int i, len;
  794. int table_idx = bttv_tvnorms[btv->tvnorm].sram;
  795. int fsc = bttv_tvnorms[btv->tvnorm].Fsc;
  796. if (btv->input == btv->dig) {
  797. dprintk("%d: load digital timing table (table_idx=%d)\n",
  798. btv->c.nr,table_idx);
  799. /* timing change...reset timing generator address */
  800. btwrite(0x00, BT848_TGCTRL);
  801. btwrite(0x02, BT848_TGCTRL);
  802. btwrite(0x00, BT848_TGCTRL);
  803. len=SRAM_Table[table_idx][0];
  804. for(i = 1; i <= len; i++)
  805. btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
  806. btv->pll.pll_ofreq = 27000000;
  807. set_pll(btv);
  808. btwrite(0x11, BT848_TGCTRL);
  809. btwrite(0x41, BT848_DVSIF);
  810. } else {
  811. btv->pll.pll_ofreq = fsc;
  812. set_pll(btv);
  813. btwrite(0x0, BT848_DVSIF);
  814. }
  815. }
  816. /* ----------------------------------------------------------------------- */
  817. static void bt848_bright(struct bttv *btv, int bright)
  818. {
  819. int value;
  820. // printk("set bright: %d\n", bright); // DEBUG
  821. btv->bright = bright;
  822. /* We want -128 to 127 we get 0-65535 */
  823. value = (bright >> 8) - 128;
  824. btwrite(value & 0xff, BT848_BRIGHT);
  825. }
  826. static void bt848_hue(struct bttv *btv, int hue)
  827. {
  828. int value;
  829. btv->hue = hue;
  830. /* -128 to 127 */
  831. value = (hue >> 8) - 128;
  832. btwrite(value & 0xff, BT848_HUE);
  833. }
  834. static void bt848_contrast(struct bttv *btv, int cont)
  835. {
  836. int value,hibit;
  837. btv->contrast = cont;
  838. /* 0-511 */
  839. value = (cont >> 7);
  840. hibit = (value >> 6) & 4;
  841. btwrite(value & 0xff, BT848_CONTRAST_LO);
  842. btaor(hibit, ~4, BT848_E_CONTROL);
  843. btaor(hibit, ~4, BT848_O_CONTROL);
  844. }
  845. static void bt848_sat(struct bttv *btv, int color)
  846. {
  847. int val_u,val_v,hibits;
  848. btv->saturation = color;
  849. /* 0-511 for the color */
  850. val_u = ((color * btv->opt_uv_ratio) / 50) >> 7;
  851. val_v = (((color * (100 - btv->opt_uv_ratio) / 50) >>7)*180L)/254;
  852. hibits = (val_u >> 7) & 2;
  853. hibits |= (val_v >> 8) & 1;
  854. btwrite(val_u & 0xff, BT848_SAT_U_LO);
  855. btwrite(val_v & 0xff, BT848_SAT_V_LO);
  856. btaor(hibits, ~3, BT848_E_CONTROL);
  857. btaor(hibits, ~3, BT848_O_CONTROL);
  858. }
  859. /* ----------------------------------------------------------------------- */
  860. static int
  861. video_mux(struct bttv *btv, unsigned int input)
  862. {
  863. int mux,mask2;
  864. if (input >= bttv_tvcards[btv->c.type].video_inputs)
  865. return -EINVAL;
  866. /* needed by RemoteVideo MX */
  867. mask2 = bttv_tvcards[btv->c.type].gpiomask2;
  868. if (mask2)
  869. gpio_inout(mask2,mask2);
  870. if (input == btv->svhs) {
  871. btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
  872. btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
  873. } else {
  874. btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
  875. btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
  876. }
  877. mux = bttv_muxsel(btv, input);
  878. btaor(mux<<5, ~(3<<5), BT848_IFORM);
  879. dprintk("%d: video mux: input=%d mux=%d\n", btv->c.nr, input, mux);
  880. /* card specific hook */
  881. if(bttv_tvcards[btv->c.type].muxsel_hook)
  882. bttv_tvcards[btv->c.type].muxsel_hook (btv, input);
  883. return 0;
  884. }
  885. static char *audio_modes[] = {
  886. "audio: tuner", "audio: radio", "audio: extern",
  887. "audio: intern", "audio: mute"
  888. };
  889. static void
  890. audio_mux_gpio(struct bttv *btv, int input, int mute)
  891. {
  892. int gpio_val, signal, mute_gpio;
  893. gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
  894. bttv_tvcards[btv->c.type].gpiomask);
  895. signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;
  896. /* automute */
  897. mute_gpio = mute || (btv->opt_automute && (!signal || !btv->users)
  898. && !btv->has_radio_tuner);
  899. if (mute_gpio)
  900. gpio_val = bttv_tvcards[btv->c.type].gpiomute;
  901. else
  902. gpio_val = bttv_tvcards[btv->c.type].gpiomux[input];
  903. switch (btv->c.type) {
  904. case BTTV_BOARD_VOODOOTV_FM:
  905. case BTTV_BOARD_VOODOOTV_200:
  906. gpio_val = bttv_tda9880_setnorm(btv, gpio_val);
  907. break;
  908. default:
  909. gpio_bits(bttv_tvcards[btv->c.type].gpiomask, gpio_val);
  910. }
  911. if (bttv_gpio)
  912. bttv_gpio_tracking(btv, audio_modes[mute_gpio ? 4 : input]);
  913. }
  914. static int
  915. audio_mute(struct bttv *btv, int mute)
  916. {
  917. struct v4l2_ctrl *ctrl;
  918. audio_mux_gpio(btv, btv->audio_input, mute);
  919. if (btv->sd_msp34xx) {
  920. ctrl = v4l2_ctrl_find(btv->sd_msp34xx->ctrl_handler, V4L2_CID_AUDIO_MUTE);
  921. if (ctrl)
  922. v4l2_ctrl_s_ctrl(ctrl, mute);
  923. }
  924. if (btv->sd_tvaudio) {
  925. ctrl = v4l2_ctrl_find(btv->sd_tvaudio->ctrl_handler, V4L2_CID_AUDIO_MUTE);
  926. if (ctrl)
  927. v4l2_ctrl_s_ctrl(ctrl, mute);
  928. }
  929. if (btv->sd_tda7432) {
  930. ctrl = v4l2_ctrl_find(btv->sd_tda7432->ctrl_handler, V4L2_CID_AUDIO_MUTE);
  931. if (ctrl)
  932. v4l2_ctrl_s_ctrl(ctrl, mute);
  933. }
  934. return 0;
  935. }
  936. static int
  937. audio_input(struct bttv *btv, int input)
  938. {
  939. audio_mux_gpio(btv, input, btv->mute);
  940. if (btv->sd_msp34xx) {
  941. u32 in;
  942. /* Note: the inputs tuner/radio/extern/intern are translated
  943. to msp routings. This assumes common behavior for all msp3400
  944. based TV cards. When this assumption fails, then the
  945. specific MSP routing must be added to the card table.
  946. For now this is sufficient. */
  947. switch (input) {
  948. case TVAUDIO_INPUT_RADIO:
  949. /* Some boards need the msp do to the radio demod */
  950. if (btv->radio_uses_msp_demodulator) {
  951. in = MSP_INPUT_DEFAULT;
  952. break;
  953. }
  954. in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
  955. MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
  956. break;
  957. case TVAUDIO_INPUT_EXTERN:
  958. in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER1,
  959. MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
  960. break;
  961. case TVAUDIO_INPUT_INTERN:
  962. /* Yes, this is the same input as for RADIO. I doubt
  963. if this is ever used. The only board with an INTERN
  964. input is the BTTV_BOARD_AVERMEDIA98. I wonder how
  965. that was tested. My guess is that the whole INTERN
  966. input does not work. */
  967. in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
  968. MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
  969. break;
  970. case TVAUDIO_INPUT_TUNER:
  971. default:
  972. /* This is the only card that uses TUNER2, and afaik,
  973. is the only difference between the VOODOOTV_FM
  974. and VOODOOTV_200 */
  975. if (btv->c.type == BTTV_BOARD_VOODOOTV_200)
  976. in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER2, \
  977. MSP_DSP_IN_TUNER, MSP_DSP_IN_TUNER);
  978. else
  979. in = MSP_INPUT_DEFAULT;
  980. break;
  981. }
  982. v4l2_subdev_call(btv->sd_msp34xx, audio, s_routing,
  983. in, MSP_OUTPUT_DEFAULT, 0);
  984. }
  985. if (btv->sd_tvaudio) {
  986. v4l2_subdev_call(btv->sd_tvaudio, audio, s_routing,
  987. input, 0, 0);
  988. }
  989. return 0;
  990. }
  991. static void
  992. bttv_crop_calc_limits(struct bttv_crop *c)
  993. {
  994. /* Scale factor min. 1:1, max. 16:1. Min. image size
  995. 48 x 32. Scaled width must be a multiple of 4. */
  996. if (1) {
  997. /* For bug compatibility with VIDIOCGCAP and image
  998. size checks in earlier driver versions. */
  999. c->min_scaled_width = 48;
  1000. c->min_scaled_height = 32;
  1001. } else {
  1002. c->min_scaled_width =
  1003. (max_t(unsigned int, 48, c->rect.width >> 4) + 3) & ~3;
  1004. c->min_scaled_height =
  1005. max_t(unsigned int, 32, c->rect.height >> 4);
  1006. }
  1007. c->max_scaled_width = c->rect.width & ~3;
  1008. c->max_scaled_height = c->rect.height;
  1009. }
  1010. static void
  1011. bttv_crop_reset(struct bttv_crop *c, unsigned int norm)
  1012. {
  1013. c->rect = bttv_tvnorms[norm].cropcap.defrect;
  1014. bttv_crop_calc_limits(c);
  1015. }
  1016. /* Call with btv->lock down. */
  1017. static int
  1018. set_tvnorm(struct bttv *btv, unsigned int norm)
  1019. {
  1020. const struct bttv_tvnorm *tvnorm;
  1021. v4l2_std_id id;
  1022. BUG_ON(norm >= BTTV_TVNORMS);
  1023. BUG_ON(btv->tvnorm >= BTTV_TVNORMS);
  1024. tvnorm = &bttv_tvnorms[norm];
  1025. if (memcmp(&bttv_tvnorms[btv->tvnorm].cropcap, &tvnorm->cropcap,
  1026. sizeof (tvnorm->cropcap))) {
  1027. bttv_crop_reset(&btv->crop[0], norm);
  1028. btv->crop[1] = btv->crop[0]; /* current = default */
  1029. if (0 == (btv->resources & VIDEO_RESOURCES)) {
  1030. btv->crop_start = tvnorm->cropcap.bounds.top
  1031. + tvnorm->cropcap.bounds.height;
  1032. }
  1033. }
  1034. btv->tvnorm = norm;
  1035. btwrite(tvnorm->adelay, BT848_ADELAY);
  1036. btwrite(tvnorm->bdelay, BT848_BDELAY);
  1037. btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH),
  1038. BT848_IFORM);
  1039. btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE);
  1040. btwrite(1, BT848_VBI_PACK_DEL);
  1041. bt848A_set_timing(btv);
  1042. switch (btv->c.type) {
  1043. case BTTV_BOARD_VOODOOTV_FM:
  1044. case BTTV_BOARD_VOODOOTV_200:
  1045. bttv_tda9880_setnorm(btv, gpio_read());
  1046. break;
  1047. }
  1048. id = tvnorm->v4l2_id;
  1049. bttv_call_all(btv, video, s_std, id);
  1050. return 0;
  1051. }
  1052. /* Call with btv->lock down. */
  1053. static void
  1054. set_input(struct bttv *btv, unsigned int input, unsigned int norm)
  1055. {
  1056. unsigned long flags;
  1057. btv->input = input;
  1058. if (irq_iswitch) {
  1059. spin_lock_irqsave(&btv->s_lock,flags);
  1060. if (btv->curr.frame_irq) {
  1061. /* active capture -> delayed input switch */
  1062. btv->new_input = input;
  1063. } else {
  1064. video_mux(btv,input);
  1065. }
  1066. spin_unlock_irqrestore(&btv->s_lock,flags);
  1067. } else {
  1068. video_mux(btv,input);
  1069. }
  1070. btv->audio_input = (btv->tuner_type != TUNER_ABSENT && input == 0) ?
  1071. TVAUDIO_INPUT_TUNER : TVAUDIO_INPUT_EXTERN;
  1072. audio_input(btv, btv->audio_input);
  1073. set_tvnorm(btv, norm);
  1074. }
  1075. static void init_irqreg(struct bttv *btv)
  1076. {
  1077. /* clear status */
  1078. btwrite(0xfffffUL, BT848_INT_STAT);
  1079. if (bttv_tvcards[btv->c.type].no_video) {
  1080. /* i2c only */
  1081. btwrite(BT848_INT_I2CDONE,
  1082. BT848_INT_MASK);
  1083. } else {
  1084. /* full video */
  1085. btwrite((btv->triton1) |
  1086. (btv->gpioirq ? BT848_INT_GPINT : 0) |
  1087. BT848_INT_SCERR |
  1088. (fdsr ? BT848_INT_FDSR : 0) |
  1089. BT848_INT_RISCI | BT848_INT_OCERR |
  1090. BT848_INT_FMTCHG|BT848_INT_HLOCK|
  1091. BT848_INT_I2CDONE,
  1092. BT848_INT_MASK);
  1093. }
  1094. }
  1095. static void init_bt848(struct bttv *btv)
  1096. {
  1097. if (bttv_tvcards[btv->c.type].no_video) {
  1098. /* very basic init only */
  1099. init_irqreg(btv);
  1100. return;
  1101. }
  1102. btwrite(0x00, BT848_CAP_CTL);
  1103. btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
  1104. btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);
  1105. /* set planar and packed mode trigger points and */
  1106. /* set rising edge of inverted GPINTR pin as irq trigger */
  1107. btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
  1108. BT848_GPIO_DMA_CTL_PLTP1_16|
  1109. BT848_GPIO_DMA_CTL_PLTP23_16|
  1110. BT848_GPIO_DMA_CTL_GPINTC|
  1111. BT848_GPIO_DMA_CTL_GPINTI,
  1112. BT848_GPIO_DMA_CTL);
  1113. btwrite(0x20, BT848_E_VSCALE_HI);
  1114. btwrite(0x20, BT848_O_VSCALE_HI);
  1115. v4l2_ctrl_handler_setup(&btv->ctrl_handler);
  1116. /* interrupt */
  1117. init_irqreg(btv);
  1118. }
  1119. static void bttv_reinit_bt848(struct bttv *btv)
  1120. {
  1121. unsigned long flags;
  1122. if (bttv_verbose)
  1123. pr_info("%d: reset, reinitialize\n", btv->c.nr);
  1124. spin_lock_irqsave(&btv->s_lock,flags);
  1125. btv->errors=0;
  1126. bttv_set_dma(btv,0);
  1127. spin_unlock_irqrestore(&btv->s_lock,flags);
  1128. init_bt848(btv);
  1129. btv->pll.pll_current = -1;
  1130. set_input(btv, btv->input, btv->tvnorm);
  1131. }
  1132. static int bttv_s_ctrl(struct v4l2_ctrl *c)
  1133. {
  1134. struct bttv *btv = container_of(c->handler, struct bttv, ctrl_handler);
  1135. int val;
  1136. switch (c->id) {
  1137. case V4L2_CID_BRIGHTNESS:
  1138. bt848_bright(btv, c->val);
  1139. break;
  1140. case V4L2_CID_HUE:
  1141. bt848_hue(btv, c->val);
  1142. break;
  1143. case V4L2_CID_CONTRAST:
  1144. bt848_contrast(btv, c->val);
  1145. break;
  1146. case V4L2_CID_SATURATION:
  1147. bt848_sat(btv, c->val);
  1148. break;
  1149. case V4L2_CID_COLOR_KILLER:
  1150. if (c->val) {
  1151. btor(BT848_SCLOOP_CKILL, BT848_E_SCLOOP);
  1152. btor(BT848_SCLOOP_CKILL, BT848_O_SCLOOP);
  1153. } else {
  1154. btand(~BT848_SCLOOP_CKILL, BT848_E_SCLOOP);
  1155. btand(~BT848_SCLOOP_CKILL, BT848_O_SCLOOP);
  1156. }
  1157. break;
  1158. case V4L2_CID_AUDIO_MUTE:
  1159. audio_mute(btv, c->val);
  1160. btv->mute = c->val;
  1161. break;
  1162. case V4L2_CID_AUDIO_VOLUME:
  1163. btv->volume_gpio(btv, c->val);
  1164. break;
  1165. case V4L2_CID_CHROMA_AGC:
  1166. val = c->val ? BT848_SCLOOP_CAGC : 0;
  1167. btwrite(val, BT848_E_SCLOOP);
  1168. btwrite(val, BT848_O_SCLOOP);
  1169. break;
  1170. case V4L2_CID_PRIVATE_COMBFILTER:
  1171. btv->opt_combfilter = c->val;
  1172. break;
  1173. case V4L2_CID_PRIVATE_LUMAFILTER:
  1174. if (c->val) {
  1175. btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
  1176. btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
  1177. } else {
  1178. btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
  1179. btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
  1180. }
  1181. break;
  1182. case V4L2_CID_PRIVATE_AUTOMUTE:
  1183. btv->opt_automute = c->val;
  1184. break;
  1185. case V4L2_CID_PRIVATE_AGC_CRUSH:
  1186. btwrite(BT848_ADC_RESERVED |
  1187. (c->val ? BT848_ADC_CRUSH : 0),
  1188. BT848_ADC);
  1189. break;
  1190. case V4L2_CID_PRIVATE_VCR_HACK:
  1191. btv->opt_vcr_hack = c->val;
  1192. break;
  1193. case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
  1194. btwrite(c->val, BT848_WC_UP);
  1195. break;
  1196. case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
  1197. btwrite(c->val, BT848_WC_DOWN);
  1198. break;
  1199. case V4L2_CID_PRIVATE_UV_RATIO:
  1200. btv->opt_uv_ratio = c->val;
  1201. bt848_sat(btv, btv->saturation);
  1202. break;
  1203. case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
  1204. btaor((c->val << 7), ~BT848_OFORM_RANGE, BT848_OFORM);
  1205. break;
  1206. case V4L2_CID_PRIVATE_CORING:
  1207. btaor((c->val << 5), ~BT848_OFORM_CORE32, BT848_OFORM);
  1208. break;
  1209. default:
  1210. return -EINVAL;
  1211. }
  1212. return 0;
  1213. }
  1214. /* ----------------------------------------------------------------------- */
  1215. static const struct v4l2_ctrl_ops bttv_ctrl_ops = {
  1216. .s_ctrl = bttv_s_ctrl,
  1217. };
  1218. static struct v4l2_ctrl_config bttv_ctrl_combfilter = {
  1219. .ops = &bttv_ctrl_ops,
  1220. .id = V4L2_CID_PRIVATE_COMBFILTER,
  1221. .name = "Comb Filter",
  1222. .type = V4L2_CTRL_TYPE_BOOLEAN,
  1223. .min = 0,
  1224. .max = 1,
  1225. .step = 1,
  1226. .def = 1,
  1227. };
  1228. static struct v4l2_ctrl_config bttv_ctrl_automute = {
  1229. .ops = &bttv_ctrl_ops,
  1230. .id = V4L2_CID_PRIVATE_AUTOMUTE,
  1231. .name = "Auto Mute",
  1232. .type = V4L2_CTRL_TYPE_BOOLEAN,
  1233. .min = 0,
  1234. .max = 1,
  1235. .step = 1,
  1236. .def = 1,
  1237. };
  1238. static struct v4l2_ctrl_config bttv_ctrl_lumafilter = {
  1239. .ops = &bttv_ctrl_ops,
  1240. .id = V4L2_CID_PRIVATE_LUMAFILTER,
  1241. .name = "Luma Decimation Filter",
  1242. .type = V4L2_CTRL_TYPE_BOOLEAN,
  1243. .min = 0,
  1244. .max = 1,
  1245. .step = 1,
  1246. .def = 1,
  1247. };
  1248. static struct v4l2_ctrl_config bttv_ctrl_agc_crush = {
  1249. .ops = &bttv_ctrl_ops,
  1250. .id = V4L2_CID_PRIVATE_AGC_CRUSH,
  1251. .name = "AGC Crush",
  1252. .type = V4L2_CTRL_TYPE_BOOLEAN,
  1253. .min = 0,
  1254. .max = 1,
  1255. .step = 1,
  1256. .def = 1,
  1257. };
  1258. static struct v4l2_ctrl_config bttv_ctrl_vcr_hack = {
  1259. .ops = &bttv_ctrl_ops,
  1260. .id = V4L2_CID_PRIVATE_VCR_HACK,
  1261. .name = "VCR Hack",
  1262. .type = V4L2_CTRL_TYPE_BOOLEAN,
  1263. .min = 0,
  1264. .max = 1,
  1265. .step = 1,
  1266. .def = 1,
  1267. };
  1268. static struct v4l2_ctrl_config bttv_ctrl_whitecrush_lower = {
  1269. .ops = &bttv_ctrl_ops,
  1270. .id = V4L2_CID_PRIVATE_WHITECRUSH_LOWER,
  1271. .name = "Whitecrush Lower",
  1272. .type = V4L2_CTRL_TYPE_INTEGER,
  1273. .min = 0,
  1274. .max = 255,
  1275. .step = 1,
  1276. .def = 0x7f,
  1277. };
  1278. static struct v4l2_ctrl_config bttv_ctrl_whitecrush_upper = {
  1279. .ops = &bttv_ctrl_ops,
  1280. .id = V4L2_CID_PRIVATE_WHITECRUSH_UPPER,
  1281. .name = "Whitecrush Upper",
  1282. .type = V4L2_CTRL_TYPE_INTEGER,
  1283. .min = 0,
  1284. .max = 255,
  1285. .step = 1,
  1286. .def = 0xcf,
  1287. };
  1288. static struct v4l2_ctrl_config bttv_ctrl_uv_ratio = {
  1289. .ops = &bttv_ctrl_ops,
  1290. .id = V4L2_CID_PRIVATE_UV_RATIO,
  1291. .name = "UV Ratio",
  1292. .type = V4L2_CTRL_TYPE_INTEGER,
  1293. .min = 0,
  1294. .max = 100,
  1295. .step = 1,
  1296. .def = 50,
  1297. };
  1298. static struct v4l2_ctrl_config bttv_ctrl_full_luma = {
  1299. .ops = &bttv_ctrl_ops,
  1300. .id = V4L2_CID_PRIVATE_FULL_LUMA_RANGE,
  1301. .name = "Full Luma Range",
  1302. .type = V4L2_CTRL_TYPE_BOOLEAN,
  1303. .min = 0,
  1304. .max = 1,
  1305. .step = 1,
  1306. };
  1307. static struct v4l2_ctrl_config bttv_ctrl_coring = {
  1308. .ops = &bttv_ctrl_ops,
  1309. .id = V4L2_CID_PRIVATE_CORING,
  1310. .name = "Coring",
  1311. .type = V4L2_CTRL_TYPE_INTEGER,
  1312. .min = 0,
  1313. .max = 3,
  1314. .step = 1,
  1315. };
  1316. /* ----------------------------------------------------------------------- */
  1317. void bttv_gpio_tracking(struct bttv *btv, char *comment)
  1318. {
  1319. unsigned int outbits, data;
  1320. outbits = btread(BT848_GPIO_OUT_EN);
  1321. data = btread(BT848_GPIO_DATA);
  1322. pr_debug("%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
  1323. btv->c.nr, outbits, data & outbits, data & ~outbits, comment);
  1324. }
  1325. static void bttv_field_count(struct bttv *btv)
  1326. {
  1327. int need_count = 0;
  1328. if (btv->users)
  1329. need_count++;
  1330. if (need_count) {
  1331. /* start field counter */
  1332. btor(BT848_INT_VSYNC,BT848_INT_MASK);
  1333. } else {
  1334. /* stop field counter */
  1335. btand(~BT848_INT_VSYNC,BT848_INT_MASK);
  1336. btv->field_count = 0;
  1337. }
  1338. }
  1339. static const struct bttv_format*
  1340. format_by_fourcc(int fourcc)
  1341. {
  1342. unsigned int i;
  1343. for (i = 0; i < FORMATS; i++) {
  1344. if (-1 == formats[i].fourcc)
  1345. continue;
  1346. if (formats[i].fourcc == fourcc)
  1347. return formats+i;
  1348. }
  1349. return NULL;
  1350. }
  1351. /* ----------------------------------------------------------------------- */
  1352. /* misc helpers */
  1353. static int
  1354. bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
  1355. struct bttv_buffer *new)
  1356. {
  1357. struct bttv_buffer *old;
  1358. unsigned long flags;
  1359. dprintk("switch_overlay: enter [new=%p]\n", new);
  1360. if (new)
  1361. new->vb.state = VIDEOBUF_DONE;
  1362. spin_lock_irqsave(&btv->s_lock,flags);
  1363. old = btv->screen;
  1364. btv->screen = new;
  1365. btv->loop_irq |= 1;
  1366. bttv_set_dma(btv, 0x03);
  1367. spin_unlock_irqrestore(&btv->s_lock,flags);
  1368. if (NULL != old) {
  1369. dprintk("switch_overlay: old=%p state is %d\n",
  1370. old, old->vb.state);
  1371. bttv_dma_free(&fh->cap,btv, old);
  1372. kfree(old);
  1373. }
  1374. if (NULL == new)
  1375. free_btres_lock(btv,fh,RESOURCE_OVERLAY);
  1376. dprintk("switch_overlay: done\n");
  1377. return 0;
  1378. }
  1379. /* ----------------------------------------------------------------------- */
  1380. /* video4linux (1) interface */
  1381. static int bttv_prepare_buffer(struct videobuf_queue *q,struct bttv *btv,
  1382. struct bttv_buffer *buf,
  1383. const struct bttv_format *fmt,
  1384. unsigned int width, unsigned int height,
  1385. enum v4l2_field field)
  1386. {
  1387. struct bttv_fh *fh = q->priv_data;
  1388. int redo_dma_risc = 0;
  1389. struct bttv_crop c;
  1390. int norm;
  1391. int rc;
  1392. /* check settings */
  1393. if (NULL == fmt)
  1394. return -EINVAL;
  1395. if (fmt->btformat == BT848_COLOR_FMT_RAW) {
  1396. width = RAW_BPL;
  1397. height = RAW_LINES*2;
  1398. if (width*height > buf->vb.bsize)
  1399. return -EINVAL;
  1400. buf->vb.size = buf->vb.bsize;
  1401. /* Make sure tvnorm and vbi_end remain consistent
  1402. until we're done. */
  1403. norm = btv->tvnorm;
  1404. /* In this mode capturing always starts at defrect.top
  1405. (default VDELAY), ignoring cropping parameters. */
  1406. if (btv->vbi_end > bttv_tvnorms[norm].cropcap.defrect.top) {
  1407. return -EINVAL;
  1408. }
  1409. c.rect = bttv_tvnorms[norm].cropcap.defrect;
  1410. } else {
  1411. norm = btv->tvnorm;
  1412. c = btv->crop[!!fh->do_crop];
  1413. if (width < c.min_scaled_width ||
  1414. width > c.max_scaled_width ||
  1415. height < c.min_scaled_height)
  1416. return -EINVAL;
  1417. switch (field) {
  1418. case V4L2_FIELD_TOP:
  1419. case V4L2_FIELD_BOTTOM:
  1420. case V4L2_FIELD_ALTERNATE:
  1421. /* btv->crop counts frame lines. Max. scale
  1422. factor is 16:1 for frames, 8:1 for fields. */
  1423. if (height * 2 > c.max_scaled_height)
  1424. return -EINVAL;
  1425. break;
  1426. default:
  1427. if (height > c.max_scaled_height)
  1428. return -EINVAL;
  1429. break;
  1430. }
  1431. buf->vb.size = (width * height * fmt->depth) >> 3;
  1432. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  1433. return -EINVAL;
  1434. }
  1435. /* alloc + fill struct bttv_buffer (if changed) */
  1436. if (buf->vb.width != width || buf->vb.height != height ||
  1437. buf->vb.field != field ||
  1438. buf->tvnorm != norm || buf->fmt != fmt ||
  1439. buf->crop.top != c.rect.top ||
  1440. buf->crop.left != c.rect.left ||
  1441. buf->crop.width != c.rect.width ||
  1442. buf->crop.height != c.rect.height) {
  1443. buf->vb.width = width;
  1444. buf->vb.height = height;
  1445. buf->vb.field = field;
  1446. buf->tvnorm = norm;
  1447. buf->fmt = fmt;
  1448. buf->crop = c.rect;
  1449. redo_dma_risc = 1;
  1450. }
  1451. /* alloc risc memory */
  1452. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  1453. redo_dma_risc = 1;
  1454. if (0 != (rc = videobuf_iolock(q,&buf->vb,&btv->fbuf)))
  1455. goto fail;
  1456. }
  1457. if (redo_dma_risc)
  1458. if (0 != (rc = bttv_buffer_risc(btv,buf)))
  1459. goto fail;
  1460. buf->vb.state = VIDEOBUF_PREPARED;
  1461. return 0;
  1462. fail:
  1463. bttv_dma_free(q,btv,buf);
  1464. return rc;
  1465. }
  1466. static int
  1467. buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
  1468. {
  1469. struct bttv_fh *fh = q->priv_data;
  1470. *size = fh->fmt->depth*fh->width*fh->height >> 3;
  1471. if (0 == *count)
  1472. *count = gbuffers;
  1473. if (*size * *count > gbuffers * gbufsize)
  1474. *count = (gbuffers * gbufsize) / *size;
  1475. return 0;
  1476. }
  1477. static int
  1478. buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
  1479. enum v4l2_field field)
  1480. {
  1481. struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
  1482. struct bttv_fh *fh = q->priv_data;
  1483. return bttv_prepare_buffer(q,fh->btv, buf, fh->fmt,
  1484. fh->width, fh->height, field);
  1485. }
  1486. static void
  1487. buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
  1488. {
  1489. struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
  1490. struct bttv_fh *fh = q->priv_data;
  1491. struct bttv *btv = fh->btv;
  1492. buf->vb.state = VIDEOBUF_QUEUED;
  1493. list_add_tail(&buf->vb.queue,&btv->capture);
  1494. if (!btv->curr.frame_irq) {
  1495. btv->loop_irq |= 1;
  1496. bttv_set_dma(btv, 0x03);
  1497. }
  1498. }
  1499. static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
  1500. {
  1501. struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
  1502. struct bttv_fh *fh = q->priv_data;
  1503. bttv_dma_free(q,fh->btv,buf);
  1504. }
  1505. static struct videobuf_queue_ops bttv_video_qops = {
  1506. .buf_setup = buffer_setup,
  1507. .buf_prepare = buffer_prepare,
  1508. .buf_queue = buffer_queue,
  1509. .buf_release = buffer_release,
  1510. };
  1511. static void radio_enable(struct bttv *btv)
  1512. {
  1513. /* Switch to the radio tuner */
  1514. if (!btv->has_radio_tuner) {
  1515. btv->has_radio_tuner = 1;
  1516. bttv_call_all(btv, tuner, s_radio);
  1517. btv->audio_input = TVAUDIO_INPUT_RADIO;
  1518. audio_input(btv, btv->audio_input);
  1519. }
  1520. }
  1521. static int bttv_s_std(struct file *file, void *priv, v4l2_std_id id)
  1522. {
  1523. struct bttv_fh *fh = priv;
  1524. struct bttv *btv = fh->btv;
  1525. unsigned int i;
  1526. for (i = 0; i < BTTV_TVNORMS; i++)
  1527. if (id & bttv_tvnorms[i].v4l2_id)
  1528. break;
  1529. if (i == BTTV_TVNORMS)
  1530. return -EINVAL;
  1531. btv->std = id;
  1532. set_tvnorm(btv, i);
  1533. return 0;
  1534. }
  1535. static int bttv_g_std(struct file *file, void *priv, v4l2_std_id *id)
  1536. {
  1537. struct bttv_fh *fh = priv;
  1538. struct bttv *btv = fh->btv;
  1539. *id = btv->std;
  1540. return 0;
  1541. }
  1542. static int bttv_querystd(struct file *file, void *f, v4l2_std_id *id)
  1543. {
  1544. struct bttv_fh *fh = f;
  1545. struct bttv *btv = fh->btv;
  1546. if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
  1547. *id &= V4L2_STD_625_50;
  1548. else
  1549. *id &= V4L2_STD_525_60;
  1550. return 0;
  1551. }
  1552. static int bttv_enum_input(struct file *file, void *priv,
  1553. struct v4l2_input *i)
  1554. {
  1555. struct bttv_fh *fh = priv;
  1556. struct bttv *btv = fh->btv;
  1557. if (i->index >= bttv_tvcards[btv->c.type].video_inputs)
  1558. return -EINVAL;
  1559. i->type = V4L2_INPUT_TYPE_CAMERA;
  1560. i->audioset = 0;
  1561. if (btv->tuner_type != TUNER_ABSENT && i->index == 0) {
  1562. sprintf(i->name, "Television");
  1563. i->type = V4L2_INPUT_TYPE_TUNER;
  1564. i->tuner = 0;
  1565. } else if (i->index == btv->svhs) {
  1566. sprintf(i->name, "S-Video");
  1567. } else {
  1568. sprintf(i->name, "Composite%d", i->index);
  1569. }
  1570. if (i->index == btv->input) {
  1571. __u32 dstatus = btread(BT848_DSTATUS);
  1572. if (0 == (dstatus & BT848_DSTATUS_PRES))
  1573. i->status |= V4L2_IN_ST_NO_SIGNAL;
  1574. if (0 == (dstatus & BT848_DSTATUS_HLOC))
  1575. i->status |= V4L2_IN_ST_NO_H_LOCK;
  1576. }
  1577. i->std = BTTV_NORMS;
  1578. return 0;
  1579. }
  1580. static int bttv_g_input(struct file *file, void *priv, unsigned int *i)
  1581. {
  1582. struct bttv_fh *fh = priv;
  1583. struct bttv *btv = fh->btv;
  1584. *i = btv->input;
  1585. return 0;
  1586. }
  1587. static int bttv_s_input(struct file *file, void *priv, unsigned int i)
  1588. {
  1589. struct bttv_fh *fh = priv;
  1590. struct bttv *btv = fh->btv;
  1591. if (i >= bttv_tvcards[btv->c.type].video_inputs)
  1592. return -EINVAL;
  1593. set_input(btv, i, btv->tvnorm);
  1594. return 0;
  1595. }
  1596. static int bttv_s_tuner(struct file *file, void *priv,
  1597. const struct v4l2_tuner *t)
  1598. {
  1599. struct bttv_fh *fh = priv;
  1600. struct bttv *btv = fh->btv;
  1601. if (t->index)
  1602. return -EINVAL;
  1603. bttv_call_all(btv, tuner, s_tuner, t);
  1604. if (btv->audio_mode_gpio) {
  1605. struct v4l2_tuner copy = *t;
  1606. btv->audio_mode_gpio(btv, &copy, 1);
  1607. }
  1608. return 0;
  1609. }
  1610. static int bttv_g_frequency(struct file *file, void *priv,
  1611. struct v4l2_frequency *f)
  1612. {
  1613. struct bttv_fh *fh = priv;
  1614. struct bttv *btv = fh->btv;
  1615. if (f->tuner)
  1616. return -EINVAL;
  1617. if (f->type == V4L2_TUNER_RADIO)
  1618. radio_enable(btv);
  1619. f->frequency = f->type == V4L2_TUNER_RADIO ?
  1620. btv->radio_freq : btv->tv_freq;
  1621. return 0;
  1622. }
  1623. static void bttv_set_frequency(struct bttv *btv, const struct v4l2_frequency *f)
  1624. {
  1625. struct v4l2_frequency new_freq = *f;
  1626. bttv_call_all(btv, tuner, s_frequency, f);
  1627. /* s_frequency may clamp the frequency, so get the actual
  1628. frequency before assigning radio/tv_freq. */
  1629. bttv_call_all(btv, tuner, g_frequency, &new_freq);
  1630. if (new_freq.type == V4L2_TUNER_RADIO) {
  1631. radio_enable(btv);
  1632. btv->radio_freq = new_freq.frequency;
  1633. if (btv->has_tea575x) {
  1634. btv->tea.freq = btv->radio_freq;
  1635. snd_tea575x_set_freq(&btv->tea);
  1636. }
  1637. } else {
  1638. btv->tv_freq = new_freq.frequency;
  1639. }
  1640. }
  1641. static int bttv_s_frequency(struct file *file, void *priv,
  1642. const struct v4l2_frequency *f)
  1643. {
  1644. struct bttv_fh *fh = priv;
  1645. struct bttv *btv = fh->btv;
  1646. if (f->tuner)
  1647. return -EINVAL;
  1648. bttv_set_frequency(btv, f);
  1649. return 0;
  1650. }
  1651. static int bttv_log_status(struct file *file, void *f)
  1652. {
  1653. struct video_device *vdev = video_devdata(file);
  1654. struct bttv_fh *fh = f;
  1655. struct bttv *btv = fh->btv;
  1656. v4l2_ctrl_handler_log_status(vdev->ctrl_handler, btv->c.v4l2_dev.name);
  1657. bttv_call_all(btv, core, log_status);
  1658. return 0;
  1659. }
  1660. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1661. static int bttv_g_register(struct file *file, void *f,
  1662. struct v4l2_dbg_register *reg)
  1663. {
  1664. struct bttv_fh *fh = f;
  1665. struct bttv *btv = fh->btv;
  1666. /* bt848 has a 12-bit register space */
  1667. reg->reg &= 0xfff;
  1668. reg->val = btread(reg->reg);
  1669. reg->size = 1;
  1670. return 0;
  1671. }
  1672. static int bttv_s_register(struct file *file, void *f,
  1673. const struct v4l2_dbg_register *reg)
  1674. {
  1675. struct bttv_fh *fh = f;
  1676. struct bttv *btv = fh->btv;
  1677. /* bt848 has a 12-bit register space */
  1678. btwrite(reg->val, reg->reg & 0xfff);
  1679. return 0;
  1680. }
  1681. #endif
  1682. /* Given cropping boundaries b and the scaled width and height of a
  1683. single field or frame, which must not exceed hardware limits, this
  1684. function adjusts the cropping parameters c. */
  1685. static void
  1686. bttv_crop_adjust (struct bttv_crop * c,
  1687. const struct v4l2_rect * b,
  1688. __s32 width,
  1689. __s32 height,
  1690. enum v4l2_field field)
  1691. {
  1692. __s32 frame_height = height << !V4L2_FIELD_HAS_BOTH(field);
  1693. __s32 max_left;
  1694. __s32 max_top;
  1695. if (width < c->min_scaled_width) {
  1696. /* Max. hor. scale factor 16:1. */
  1697. c->rect.width = width * 16;
  1698. } else if (width > c->max_scaled_width) {
  1699. /* Min. hor. scale factor 1:1. */
  1700. c->rect.width = width;
  1701. max_left = b->left + b->width - width;
  1702. max_left = min(max_left, (__s32) MAX_HDELAY);
  1703. if (c->rect.left > max_left)
  1704. c->rect.left = max_left;
  1705. }
  1706. if (height < c->min_scaled_height) {
  1707. /* Max. vert. scale factor 16:1, single fields 8:1. */
  1708. c->rect.height = height * 16;
  1709. } else if (frame_height > c->max_scaled_height) {
  1710. /* Min. vert. scale factor 1:1.
  1711. Top and height count field lines times two. */
  1712. c->rect.height = (frame_height + 1) & ~1;
  1713. max_top = b->top + b->height - c->rect.height;
  1714. if (c->rect.top > max_top)
  1715. c->rect.top = max_top;
  1716. }
  1717. bttv_crop_calc_limits(c);
  1718. }
  1719. /* Returns an error if scaling to a frame or single field with the given
  1720. width and height is not possible with the current cropping parameters
  1721. and width aligned according to width_mask. If adjust_size is TRUE the
  1722. function may adjust the width and/or height instead, rounding width
  1723. to (width + width_bias) & width_mask. If adjust_crop is TRUE it may
  1724. also adjust the current cropping parameters to get closer to the
  1725. desired image size. */
  1726. static int
  1727. limit_scaled_size_lock (struct bttv_fh * fh,
  1728. __s32 * width,
  1729. __s32 * height,
  1730. enum v4l2_field field,
  1731. unsigned int width_mask,
  1732. unsigned int width_bias,
  1733. int adjust_size,
  1734. int adjust_crop)
  1735. {
  1736. struct bttv *btv = fh->btv;
  1737. const struct v4l2_rect *b;
  1738. struct bttv_crop *c;
  1739. __s32 min_width;
  1740. __s32 min_height;
  1741. __s32 max_width;
  1742. __s32 max_height;
  1743. int rc;
  1744. BUG_ON((int) width_mask >= 0 ||
  1745. width_bias >= (unsigned int) -width_mask);
  1746. /* Make sure tvnorm, vbi_end and the current cropping parameters
  1747. remain consistent until we're done. */
  1748. b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
  1749. /* Do crop - use current, don't - use default parameters. */
  1750. c = &btv->crop[!!fh->do_crop];
  1751. if (fh->do_crop
  1752. && adjust_size
  1753. && adjust_crop
  1754. && !locked_btres(btv, VIDEO_RESOURCES)) {
  1755. min_width = 48;
  1756. min_height = 32;
  1757. /* We cannot scale up. When the scaled image is larger
  1758. than crop.rect we adjust the crop.rect as required
  1759. by the V4L2 spec, hence cropcap.bounds are our limit. */
  1760. max_width = min_t(unsigned int, b->width, MAX_HACTIVE);
  1761. max_height = b->height;
  1762. /* We cannot capture the same line as video and VBI data.
  1763. Note btv->vbi_end is really a minimum, see
  1764. bttv_vbi_try_fmt(). */
  1765. if (btv->vbi_end > b->top) {
  1766. max_height -= btv->vbi_end - b->top;
  1767. rc = -EBUSY;
  1768. if (min_height > max_height)
  1769. goto fail;
  1770. }
  1771. } else {
  1772. rc = -EBUSY;
  1773. if (btv->vbi_end > c->rect.top)
  1774. goto fail;
  1775. min_width = c->min_scaled_width;
  1776. min_height = c->min_scaled_height;
  1777. max_width = c->max_scaled_width;
  1778. max_height = c->max_scaled_height;
  1779. adjust_crop = 0;
  1780. }
  1781. min_width = (min_width - width_mask - 1) & width_mask;
  1782. max_width = max_width & width_mask;
  1783. /* Max. scale factor is 16:1 for frames, 8:1 for fields. */
  1784. min_height = min_height;
  1785. /* Min. scale factor is 1:1. */
  1786. max_height >>= !V4L2_FIELD_HAS_BOTH(field);
  1787. if (adjust_size) {
  1788. *width = clamp(*width, min_width, max_width);
  1789. *height = clamp(*height, min_height, max_height);
  1790. /* Round after clamping to avoid overflow. */
  1791. *width = (*width + width_bias) & width_mask;
  1792. if (adjust_crop) {
  1793. bttv_crop_adjust(c, b, *width, *height, field);
  1794. if (btv->vbi_end > c->rect.top) {
  1795. /* Move the crop window out of the way. */
  1796. c->rect.top = btv->vbi_end;
  1797. }
  1798. }
  1799. } else {
  1800. rc = -EINVAL;
  1801. if (*width < min_width ||
  1802. *height < min_height ||
  1803. *width > max_width ||
  1804. *height > max_height ||
  1805. 0 != (*width & ~width_mask))
  1806. goto fail;
  1807. }
  1808. rc = 0; /* success */
  1809. fail:
  1810. return rc;
  1811. }
  1812. /* Returns an error if the given overlay window dimensions are not
  1813. possible with the current cropping parameters. If adjust_size is
  1814. TRUE the function may adjust the window width and/or height
  1815. instead, however it always rounds the horizontal position and
  1816. width as btcx_align() does. If adjust_crop is TRUE the function
  1817. may also adjust the current cropping parameters to get closer
  1818. to the desired window size. */
  1819. static int
  1820. verify_window_lock(struct bttv_fh *fh, struct v4l2_window *win,
  1821. int adjust_size, int adjust_crop)
  1822. {
  1823. enum v4l2_field field;
  1824. unsigned int width_mask;
  1825. int rc;
  1826. if (win->w.width < 48)
  1827. win->w.width = 48;
  1828. if (win->w.height < 32)
  1829. win->w.height = 32;
  1830. if (win->clipcount > 2048)
  1831. win->clipcount = 2048;
  1832. win->chromakey = 0;
  1833. win->global_alpha = 0;
  1834. field = win->field;
  1835. switch (field) {
  1836. case V4L2_FIELD_TOP:
  1837. case V4L2_FIELD_BOTTOM:
  1838. case V4L2_FIELD_INTERLACED:
  1839. break;
  1840. default:
  1841. field = V4L2_FIELD_ANY;
  1842. break;
  1843. }
  1844. if (V4L2_FIELD_ANY == field) {
  1845. __s32 height2;
  1846. height2 = fh->btv->crop[!!fh->do_crop].rect.height >> 1;
  1847. field = (win->w.height > height2)
  1848. ? V4L2_FIELD_INTERLACED
  1849. : V4L2_FIELD_TOP;
  1850. }
  1851. win->field = field;
  1852. if (NULL == fh->ovfmt)
  1853. return -EINVAL;
  1854. /* 4-byte alignment. */
  1855. width_mask = ~0;
  1856. switch (fh->ovfmt->depth) {
  1857. case 8:
  1858. case 24:
  1859. width_mask = ~3;
  1860. break;
  1861. case 16:
  1862. width_mask = ~1;
  1863. break;
  1864. case 32:
  1865. break;
  1866. default:
  1867. BUG();
  1868. }
  1869. win->w.width -= win->w.left & ~width_mask;
  1870. win->w.left = (win->w.left - width_mask - 1) & width_mask;
  1871. rc = limit_scaled_size_lock(fh, &win->w.width, &win->w.height,
  1872. field, width_mask,
  1873. /* width_bias: round down */ 0,
  1874. adjust_size, adjust_crop);
  1875. if (0 != rc)
  1876. return rc;
  1877. return 0;
  1878. }
  1879. static int setup_window_lock(struct bttv_fh *fh, struct bttv *btv,
  1880. struct v4l2_window *win, int fixup)
  1881. {
  1882. struct v4l2_clip *clips = NULL;
  1883. int n,size,retval = 0;
  1884. if (NULL == fh->ovfmt)
  1885. return -EINVAL;
  1886. if (!(fh->ovfmt->flags & FORMAT_FLAGS_PACKED))
  1887. return -EINVAL;
  1888. retval = verify_window_lock(fh, win,
  1889. /* adjust_size */ fixup,
  1890. /* adjust_crop */ fixup);
  1891. if (0 != retval)
  1892. return retval;
  1893. /* copy clips -- luckily v4l1 + v4l2 are binary
  1894. compatible here ...*/
  1895. n = win->clipcount;
  1896. size = sizeof(*clips)*(n+4);
  1897. clips = kmalloc(size,GFP_KERNEL);
  1898. if (NULL == clips)
  1899. return -ENOMEM;
  1900. if (n > 0) {
  1901. if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
  1902. kfree(clips);
  1903. return -EFAULT;
  1904. }
  1905. }
  1906. /* clip against screen */
  1907. if (NULL != btv->fbuf.base)
  1908. n = btcx_screen_clips(btv->fbuf.fmt.width, btv->fbuf.fmt.height,
  1909. &win->w, clips, n);
  1910. btcx_sort_clips(clips,n);
  1911. /* 4-byte alignments */
  1912. switch (fh->ovfmt->depth) {
  1913. case 8:
  1914. case 24:
  1915. btcx_align(&win->w, clips, n, 3);
  1916. break;
  1917. case 16:
  1918. btcx_align(&win->w, clips, n, 1);
  1919. break;
  1920. case 32:
  1921. /* no alignment fixups needed */
  1922. break;
  1923. default:
  1924. BUG();
  1925. }
  1926. kfree(fh->ov.clips);
  1927. fh->ov.clips = clips;
  1928. fh->ov.nclips = n;
  1929. fh->ov.w = win->w;
  1930. fh->ov.field = win->field;
  1931. fh->ov.setup_ok = 1;
  1932. btv->init.ov.w.width = win->w.width;
  1933. btv->init.ov.w.height = win->w.height;
  1934. btv->init.ov.field = win->field;
  1935. /* update overlay if needed */
  1936. retval = 0;
  1937. if (check_btres(fh, RESOURCE_OVERLAY)) {
  1938. struct bttv_buffer *new;
  1939. new = videobuf_sg_alloc(sizeof(*new));
  1940. new->crop = btv->crop[!!fh->do_crop].rect;
  1941. bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
  1942. retval = bttv_switch_overlay(btv,fh,new);
  1943. }
  1944. return retval;
  1945. }
  1946. /* ----------------------------------------------------------------------- */
  1947. static struct videobuf_queue* bttv_queue(struct bttv_fh *fh)
  1948. {
  1949. struct videobuf_queue* q = NULL;
  1950. switch (fh->type) {
  1951. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  1952. q = &fh->cap;
  1953. break;
  1954. case V4L2_BUF_TYPE_VBI_CAPTURE:
  1955. q = &fh->vbi;
  1956. break;
  1957. default:
  1958. BUG();
  1959. }
  1960. return q;
  1961. }
  1962. static int bttv_resource(struct bttv_fh *fh)
  1963. {
  1964. int res = 0;
  1965. switch (fh->type) {
  1966. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  1967. res = RESOURCE_VIDEO_STREAM;
  1968. break;
  1969. case V4L2_BUF_TYPE_VBI_CAPTURE:
  1970. res = RESOURCE_VBI;
  1971. break;
  1972. default:
  1973. BUG();
  1974. }
  1975. return res;
  1976. }
  1977. static int bttv_switch_type(struct bttv_fh *fh, enum v4l2_buf_type type)
  1978. {
  1979. struct videobuf_queue *q = bttv_queue(fh);
  1980. int res = bttv_resource(fh);
  1981. if (check_btres(fh,res))
  1982. return -EBUSY;
  1983. if (videobuf_queue_is_busy(q))
  1984. return -EBUSY;
  1985. fh->type = type;
  1986. return 0;
  1987. }
  1988. static void
  1989. pix_format_set_size (struct v4l2_pix_format * f,
  1990. const struct bttv_format * fmt,
  1991. unsigned int width,
  1992. unsigned int height)
  1993. {
  1994. f->width = width;
  1995. f->height = height;
  1996. if (fmt->flags & FORMAT_FLAGS_PLANAR) {
  1997. f->bytesperline = width; /* Y plane */
  1998. f->sizeimage = (width * height * fmt->depth) >> 3;
  1999. } else {
  2000. f->bytesperline = (width * fmt->depth) >> 3;
  2001. f->sizeimage = height * f->bytesperline;
  2002. }
  2003. }
  2004. static int bttv_g_fmt_vid_cap(struct file *file, void *priv,
  2005. struct v4l2_format *f)
  2006. {
  2007. struct bttv_fh *fh = priv;
  2008. pix_format_set_size(&f->fmt.pix, fh->fmt,
  2009. fh->width, fh->height);
  2010. f->fmt.pix.field = fh->cap.field;
  2011. f->fmt.pix.pixelformat = fh->fmt->fourcc;
  2012. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  2013. return 0;
  2014. }
  2015. static int bttv_g_fmt_vid_overlay(struct file *file, void *priv,
  2016. struct v4l2_format *f)
  2017. {
  2018. struct bttv_fh *fh = priv;
  2019. f->fmt.win.w = fh->ov.w;
  2020. f->fmt.win.field = fh->ov.field;
  2021. return 0;
  2022. }
  2023. static void bttv_get_width_mask_vid_cap(const struct bttv_format *fmt,
  2024. unsigned int *width_mask,
  2025. unsigned int *width_bias)
  2026. {
  2027. if (fmt->flags & FORMAT_FLAGS_PLANAR) {
  2028. *width_mask = ~15; /* width must be a multiple of 16 pixels */
  2029. *width_bias = 8; /* nearest */
  2030. } else {
  2031. *width_mask = ~3; /* width must be a multiple of 4 pixels */
  2032. *width_bias = 2; /* nearest */
  2033. }
  2034. }
  2035. static int bttv_try_fmt_vid_cap(struct file *file, void *priv,
  2036. struct v4l2_format *f)
  2037. {
  2038. const struct bttv_format *fmt;
  2039. struct bttv_fh *fh = priv;
  2040. struct bttv *btv = fh->btv;
  2041. enum v4l2_field field;
  2042. __s32 width, height;
  2043. __s32 height2;
  2044. unsigned int width_mask, width_bias;
  2045. int rc;
  2046. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  2047. if (NULL == fmt)
  2048. return -EINVAL;
  2049. field = f->fmt.pix.field;
  2050. switch (field) {
  2051. case V4L2_FIELD_TOP:
  2052. case V4L2_FIELD_BOTTOM:
  2053. case V4L2_FIELD_ALTERNATE:
  2054. case V4L2_FIELD_INTERLACED:
  2055. break;
  2056. case V4L2_FIELD_SEQ_BT:
  2057. case V4L2_FIELD_SEQ_TB:
  2058. if (!(fmt->flags & FORMAT_FLAGS_PLANAR)) {
  2059. field = V4L2_FIELD_SEQ_TB;
  2060. break;
  2061. }
  2062. /* fall through */
  2063. default: /* FIELD_ANY case */
  2064. height2 = btv->crop[!!fh->do_crop].rect.height >> 1;
  2065. field = (f->fmt.pix.height > height2)
  2066. ? V4L2_FIELD_INTERLACED
  2067. : V4L2_FIELD_BOTTOM;
  2068. break;
  2069. }
  2070. width = f->fmt.pix.width;
  2071. height = f->fmt.pix.height;
  2072. bttv_get_width_mask_vid_cap(fmt, &width_mask, &width_bias);
  2073. rc = limit_scaled_size_lock(fh, &width, &height, field,
  2074. width_mask, width_bias,
  2075. /* adjust_size */ 1,
  2076. /* adjust_crop */ 0);
  2077. if (0 != rc)
  2078. return rc;
  2079. /* update data for the application */
  2080. f->fmt.pix.field = field;
  2081. pix_format_set_size(&f->fmt.pix, fmt, width, height);
  2082. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  2083. return 0;
  2084. }
  2085. static int bttv_try_fmt_vid_overlay(struct file *file, void *priv,
  2086. struct v4l2_format *f)
  2087. {
  2088. struct bttv_fh *fh = priv;
  2089. verify_window_lock(fh, &f->fmt.win,
  2090. /* adjust_size */ 1,
  2091. /* adjust_crop */ 0);
  2092. return 0;
  2093. }
  2094. static int bttv_s_fmt_vid_cap(struct file *file, void *priv,
  2095. struct v4l2_format *f)
  2096. {
  2097. int retval;
  2098. const struct bttv_format *fmt;
  2099. struct bttv_fh *fh = priv;
  2100. struct bttv *btv = fh->btv;
  2101. __s32 width, height;
  2102. unsigned int width_mask, width_bias;
  2103. enum v4l2_field field;
  2104. retval = bttv_switch_type(fh, f->type);
  2105. if (0 != retval)
  2106. return retval;
  2107. retval = bttv_try_fmt_vid_cap(file, priv, f);
  2108. if (0 != retval)
  2109. return retval;
  2110. width = f->fmt.pix.width;
  2111. height = f->fmt.pix.height;
  2112. field = f->fmt.pix.field;
  2113. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  2114. bttv_get_width_mask_vid_cap(fmt, &width_mask, &width_bias);
  2115. retval = limit_scaled_size_lock(fh, &width, &height, f->fmt.pix.field,
  2116. width_mask, width_bias,
  2117. /* adjust_size */ 1,
  2118. /* adjust_crop */ 1);
  2119. if (0 != retval)
  2120. return retval;
  2121. f->fmt.pix.field = field;
  2122. /* update our state informations */
  2123. fh->fmt = fmt;
  2124. fh->cap.field = f->fmt.pix.field;
  2125. fh->cap.last = V4L2_FIELD_NONE;
  2126. fh->width = f->fmt.pix.width;
  2127. fh->height = f->fmt.pix.height;
  2128. btv->init.fmt = fmt;
  2129. btv->init.width = f->fmt.pix.width;
  2130. btv->init.height = f->fmt.pix.height;
  2131. return 0;
  2132. }
  2133. static int bttv_s_fmt_vid_overlay(struct file *file, void *priv,
  2134. struct v4l2_format *f)
  2135. {
  2136. struct bttv_fh *fh = priv;
  2137. struct bttv *btv = fh->btv;
  2138. if (no_overlay > 0) {
  2139. pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
  2140. return -EINVAL;
  2141. }
  2142. return setup_window_lock(fh, btv, &f->fmt.win, 1);
  2143. }
  2144. static int bttv_querycap(struct file *file, void *priv,
  2145. struct v4l2_capability *cap)
  2146. {
  2147. struct video_device *vdev = video_devdata(file);
  2148. struct bttv_fh *fh = priv;
  2149. struct bttv *btv = fh->btv;
  2150. if (0 == v4l2)
  2151. return -EINVAL;
  2152. strlcpy(cap->driver, "bttv", sizeof(cap->driver));
  2153. strlcpy(cap->card, btv->video_dev.name, sizeof(cap->card));
  2154. snprintf(cap->bus_info, sizeof(cap->bus_info),
  2155. "PCI:%s", pci_name(btv->c.pci));
  2156. cap->capabilities =
  2157. V4L2_CAP_VIDEO_CAPTURE |
  2158. V4L2_CAP_READWRITE |
  2159. V4L2_CAP_STREAMING |
  2160. V4L2_CAP_DEVICE_CAPS;
  2161. if (no_overlay <= 0)
  2162. cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
  2163. if (video_is_registered(&btv->vbi_dev))
  2164. cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
  2165. if (video_is_registered(&btv->radio_dev))
  2166. cap->capabilities |= V4L2_CAP_RADIO;
  2167. /*
  2168. * No need to lock here: those vars are initialized during board
  2169. * probe and remains untouched during the rest of the driver lifecycle
  2170. */
  2171. if (btv->has_saa6588)
  2172. cap->capabilities |= V4L2_CAP_RDS_CAPTURE;
  2173. if (btv->tuner_type != TUNER_ABSENT)
  2174. cap->capabilities |= V4L2_CAP_TUNER;
  2175. if (vdev->vfl_type == VFL_TYPE_GRABBER)
  2176. cap->device_caps = cap->capabilities &
  2177. (V4L2_CAP_VIDEO_CAPTURE |
  2178. V4L2_CAP_READWRITE |
  2179. V4L2_CAP_STREAMING |
  2180. V4L2_CAP_VIDEO_OVERLAY |
  2181. V4L2_CAP_TUNER);
  2182. else if (vdev->vfl_type == VFL_TYPE_VBI)
  2183. cap->device_caps = cap->capabilities &
  2184. (V4L2_CAP_VBI_CAPTURE |
  2185. V4L2_CAP_READWRITE |
  2186. V4L2_CAP_STREAMING |
  2187. V4L2_CAP_TUNER);
  2188. else {
  2189. cap->device_caps = V4L2_CAP_RADIO | V4L2_CAP_TUNER;
  2190. if (btv->has_saa6588)
  2191. cap->device_caps |= V4L2_CAP_READWRITE |
  2192. V4L2_CAP_RDS_CAPTURE;
  2193. if (btv->has_tea575x)
  2194. cap->device_caps |= V4L2_CAP_HW_FREQ_SEEK;
  2195. }
  2196. return 0;
  2197. }
  2198. static int bttv_enum_fmt_cap_ovr(struct v4l2_fmtdesc *f)
  2199. {
  2200. int index = -1, i;
  2201. for (i = 0; i < FORMATS; i++) {
  2202. if (formats[i].fourcc != -1)
  2203. index++;
  2204. if ((unsigned int)index == f->index)
  2205. break;
  2206. }
  2207. if (FORMATS == i)
  2208. return -EINVAL;
  2209. f->pixelformat = formats[i].fourcc;
  2210. strlcpy(f->description, formats[i].name, sizeof(f->description));
  2211. return i;
  2212. }
  2213. static int bttv_enum_fmt_vid_cap(struct file *file, void *priv,
  2214. struct v4l2_fmtdesc *f)
  2215. {
  2216. int rc = bttv_enum_fmt_cap_ovr(f);
  2217. if (rc < 0)
  2218. return rc;
  2219. return 0;
  2220. }
  2221. static int bttv_enum_fmt_vid_overlay(struct file *file, void *priv,
  2222. struct v4l2_fmtdesc *f)
  2223. {
  2224. int rc;
  2225. if (no_overlay > 0) {
  2226. pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
  2227. return -EINVAL;
  2228. }
  2229. rc = bttv_enum_fmt_cap_ovr(f);
  2230. if (rc < 0)
  2231. return rc;
  2232. if (!(formats[rc].flags & FORMAT_FLAGS_PACKED))
  2233. return -EINVAL;
  2234. return 0;
  2235. }
  2236. static int bttv_g_fbuf(struct file *file, void *f,
  2237. struct v4l2_framebuffer *fb)
  2238. {
  2239. struct bttv_fh *fh = f;
  2240. struct bttv *btv = fh->btv;
  2241. *fb = btv->fbuf;
  2242. fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
  2243. fb->flags = V4L2_FBUF_FLAG_PRIMARY;
  2244. if (fh->ovfmt)
  2245. fb->fmt.pixelformat = fh->ovfmt->fourcc;
  2246. return 0;
  2247. }
  2248. static int bttv_overlay(struct file *file, void *f, unsigned int on)
  2249. {
  2250. struct bttv_fh *fh = f;
  2251. struct bttv *btv = fh->btv;
  2252. struct bttv_buffer *new;
  2253. int retval = 0;
  2254. if (on) {
  2255. /* verify args */
  2256. if (unlikely(!btv->fbuf.base)) {
  2257. return -EINVAL;
  2258. }
  2259. if (unlikely(!fh->ov.setup_ok)) {
  2260. dprintk("%d: overlay: !setup_ok\n", btv->c.nr);
  2261. retval = -EINVAL;
  2262. }
  2263. if (retval)
  2264. return retval;
  2265. }
  2266. if (!check_alloc_btres_lock(btv, fh, RESOURCE_OVERLAY))
  2267. return -EBUSY;
  2268. if (on) {
  2269. fh->ov.tvnorm = btv->tvnorm;
  2270. new = videobuf_sg_alloc(sizeof(*new));
  2271. new->crop = btv->crop[!!fh->do_crop].rect;
  2272. bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
  2273. } else {
  2274. new = NULL;
  2275. }
  2276. /* switch over */
  2277. retval = bttv_switch_overlay(btv, fh, new);
  2278. return retval;
  2279. }
  2280. static int bttv_s_fbuf(struct file *file, void *f,
  2281. const struct v4l2_framebuffer *fb)
  2282. {
  2283. struct bttv_fh *fh = f;
  2284. struct bttv *btv = fh->btv;
  2285. const struct bttv_format *fmt;
  2286. int retval;
  2287. if (!capable(CAP_SYS_ADMIN) &&
  2288. !capable(CAP_SYS_RAWIO))
  2289. return -EPERM;
  2290. /* check args */
  2291. fmt = format_by_fourcc(fb->fmt.pixelformat);
  2292. if (NULL == fmt)
  2293. return -EINVAL;
  2294. if (0 == (fmt->flags & FORMAT_FLAGS_PACKED))
  2295. return -EINVAL;
  2296. retval = -EINVAL;
  2297. if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
  2298. __s32 width = fb->fmt.width;
  2299. __s32 height = fb->fmt.height;
  2300. retval = limit_scaled_size_lock(fh, &width, &height,
  2301. V4L2_FIELD_INTERLACED,
  2302. /* width_mask */ ~3,
  2303. /* width_bias */ 2,
  2304. /* adjust_size */ 0,
  2305. /* adjust_crop */ 0);
  2306. if (0 != retval)
  2307. return retval;
  2308. }
  2309. /* ok, accept it */
  2310. btv->fbuf.base = fb->base;
  2311. btv->fbuf.fmt.width = fb->fmt.width;
  2312. btv->fbuf.fmt.height = fb->fmt.height;
  2313. if (0 != fb->fmt.bytesperline)
  2314. btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline;
  2315. else
  2316. btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8;
  2317. retval = 0;
  2318. fh->ovfmt = fmt;
  2319. btv->init.ovfmt = fmt;
  2320. if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
  2321. fh->ov.w.left = 0;
  2322. fh->ov.w.top = 0;
  2323. fh->ov.w.width = fb->fmt.width;
  2324. fh->ov.w.height = fb->fmt.height;
  2325. btv->init.ov.w.width = fb->fmt.width;
  2326. btv->init.ov.w.height = fb->fmt.height;
  2327. kfree(fh->ov.clips);
  2328. fh->ov.clips = NULL;
  2329. fh->ov.nclips = 0;
  2330. if (check_btres(fh, RESOURCE_OVERLAY)) {
  2331. struct bttv_buffer *new;
  2332. new = videobuf_sg_alloc(sizeof(*new));
  2333. new->crop = btv->crop[!!fh->do_crop].rect;
  2334. bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
  2335. retval = bttv_switch_overlay(btv, fh, new);
  2336. }
  2337. }
  2338. return retval;
  2339. }
  2340. static int bttv_reqbufs(struct file *file, void *priv,
  2341. struct v4l2_requestbuffers *p)
  2342. {
  2343. struct bttv_fh *fh = priv;
  2344. return videobuf_reqbufs(bttv_queue(fh), p);
  2345. }
  2346. static int bttv_querybuf(struct file *file, void *priv,
  2347. struct v4l2_buffer *b)
  2348. {
  2349. struct bttv_fh *fh = priv;
  2350. return videobuf_querybuf(bttv_queue(fh), b);
  2351. }
  2352. static int bttv_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  2353. {
  2354. struct bttv_fh *fh = priv;
  2355. struct bttv *btv = fh->btv;
  2356. int res = bttv_resource(fh);
  2357. if (!check_alloc_btres_lock(btv, fh, res))
  2358. return -EBUSY;
  2359. return videobuf_qbuf(bttv_queue(fh), b);
  2360. }
  2361. static int bttv_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  2362. {
  2363. struct bttv_fh *fh = priv;
  2364. return videobuf_dqbuf(bttv_queue(fh), b,
  2365. file->f_flags & O_NONBLOCK);
  2366. }
  2367. static int bttv_streamon(struct file *file, void *priv,
  2368. enum v4l2_buf_type type)
  2369. {
  2370. struct bttv_fh *fh = priv;
  2371. struct bttv *btv = fh->btv;
  2372. int res = bttv_resource(fh);
  2373. if (!check_alloc_btres_lock(btv, fh, res))
  2374. return -EBUSY;
  2375. return videobuf_streamon(bttv_queue(fh));
  2376. }
  2377. static int bttv_streamoff(struct file *file, void *priv,
  2378. enum v4l2_buf_type type)
  2379. {
  2380. struct bttv_fh *fh = priv;
  2381. struct bttv *btv = fh->btv;
  2382. int retval;
  2383. int res = bttv_resource(fh);
  2384. retval = videobuf_streamoff(bttv_queue(fh));
  2385. if (retval < 0)
  2386. return retval;
  2387. free_btres_lock(btv, fh, res);
  2388. return 0;
  2389. }
  2390. static int bttv_g_parm(struct file *file, void *f,
  2391. struct v4l2_streamparm *parm)
  2392. {
  2393. struct bttv_fh *fh = f;
  2394. struct bttv *btv = fh->btv;
  2395. if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  2396. return -EINVAL;
  2397. parm->parm.capture.readbuffers = gbuffers;
  2398. v4l2_video_std_frame_period(bttv_tvnorms[btv->tvnorm].v4l2_id,
  2399. &parm->parm.capture.timeperframe);
  2400. return 0;
  2401. }
  2402. static int bttv_g_tuner(struct file *file, void *priv,
  2403. struct v4l2_tuner *t)
  2404. {
  2405. struct bttv_fh *fh = priv;
  2406. struct bttv *btv = fh->btv;
  2407. if (0 != t->index)
  2408. return -EINVAL;
  2409. t->rxsubchans = V4L2_TUNER_SUB_MONO;
  2410. t->capability = V4L2_TUNER_CAP_NORM;
  2411. bttv_call_all(btv, tuner, g_tuner, t);
  2412. strcpy(t->name, "Television");
  2413. t->type = V4L2_TUNER_ANALOG_TV;
  2414. if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
  2415. t->signal = 0xffff;
  2416. if (btv->audio_mode_gpio)
  2417. btv->audio_mode_gpio(btv, t, 0);
  2418. return 0;
  2419. }
  2420. static int bttv_cropcap(struct file *file, void *priv,
  2421. struct v4l2_cropcap *cap)
  2422. {
  2423. struct bttv_fh *fh = priv;
  2424. struct bttv *btv = fh->btv;
  2425. if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  2426. cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
  2427. return -EINVAL;
  2428. /* defrect and bounds are set via g_selection */
  2429. cap->pixelaspect = bttv_tvnorms[btv->tvnorm].cropcap.pixelaspect;
  2430. return 0;
  2431. }
  2432. static int bttv_g_selection(struct file *file, void *f, struct v4l2_selection *sel)
  2433. {
  2434. struct bttv_fh *fh = f;
  2435. struct bttv *btv = fh->btv;
  2436. if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  2437. sel->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
  2438. return -EINVAL;
  2439. switch (sel->target) {
  2440. case V4L2_SEL_TGT_CROP:
  2441. /*
  2442. * No fh->do_crop = 1; because btv->crop[1] may be
  2443. * inconsistent with fh->width or fh->height and apps
  2444. * do not expect a change here.
  2445. */
  2446. sel->r = btv->crop[!!fh->do_crop].rect;
  2447. break;
  2448. case V4L2_SEL_TGT_CROP_DEFAULT:
  2449. sel->r = bttv_tvnorms[btv->tvnorm].cropcap.defrect;
  2450. break;
  2451. case V4L2_SEL_TGT_CROP_BOUNDS:
  2452. sel->r = bttv_tvnorms[btv->tvnorm].cropcap.bounds;
  2453. break;
  2454. default:
  2455. return -EINVAL;
  2456. }
  2457. return 0;
  2458. }
  2459. static int bttv_s_selection(struct file *file, void *f, struct v4l2_selection *sel)
  2460. {
  2461. struct bttv_fh *fh = f;
  2462. struct bttv *btv = fh->btv;
  2463. const struct v4l2_rect *b;
  2464. int retval;
  2465. struct bttv_crop c;
  2466. __s32 b_left;
  2467. __s32 b_top;
  2468. __s32 b_right;
  2469. __s32 b_bottom;
  2470. if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  2471. sel->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
  2472. return -EINVAL;
  2473. if (sel->target != V4L2_SEL_TGT_CROP)
  2474. return -EINVAL;
  2475. /* Make sure tvnorm, vbi_end and the current cropping
  2476. parameters remain consistent until we're done. Note
  2477. read() may change vbi_end in check_alloc_btres_lock(). */
  2478. retval = -EBUSY;
  2479. if (locked_btres(fh->btv, VIDEO_RESOURCES)) {
  2480. return retval;
  2481. }
  2482. b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
  2483. b_left = b->left;
  2484. b_right = b_left + b->width;
  2485. b_bottom = b->top + b->height;
  2486. b_top = max(b->top, btv->vbi_end);
  2487. if (b_top + 32 >= b_bottom) {
  2488. return retval;
  2489. }
  2490. /* Min. scaled size 48 x 32. */
  2491. c.rect.left = clamp_t(s32, sel->r.left, b_left, b_right - 48);
  2492. c.rect.left = min(c.rect.left, (__s32) MAX_HDELAY);
  2493. c.rect.width = clamp_t(s32, sel->r.width,
  2494. 48, b_right - c.rect.left);
  2495. c.rect.top = clamp_t(s32, sel->r.top, b_top, b_bottom - 32);
  2496. /* Top and height must be a multiple of two. */
  2497. c.rect.top = (c.rect.top + 1) & ~1;
  2498. c.rect.height = clamp_t(s32, sel->r.height,
  2499. 32, b_bottom - c.rect.top);
  2500. c.rect.height = (c.rect.height + 1) & ~1;
  2501. bttv_crop_calc_limits(&c);
  2502. sel->r = c.rect;
  2503. btv->crop[1] = c;
  2504. fh->do_crop = 1;
  2505. if (fh->width < c.min_scaled_width) {
  2506. fh->width = c.min_scaled_width;
  2507. btv->init.width = c.min_scaled_width;
  2508. } else if (fh->width > c.max_scaled_width) {
  2509. fh->width = c.max_scaled_width;
  2510. btv->init.width = c.max_scaled_width;
  2511. }
  2512. if (fh->height < c.min_scaled_height) {
  2513. fh->height = c.min_scaled_height;
  2514. btv->init.height = c.min_scaled_height;
  2515. } else if (fh->height > c.max_scaled_height) {
  2516. fh->height = c.max_scaled_height;
  2517. btv->init.height = c.max_scaled_height;
  2518. }
  2519. return 0;
  2520. }
  2521. static ssize_t bttv_read(struct file *file, char __user *data,
  2522. size_t count, loff_t *ppos)
  2523. {
  2524. struct bttv_fh *fh = file->private_data;
  2525. int retval = 0;
  2526. if (fh->btv->errors)
  2527. bttv_reinit_bt848(fh->btv);
  2528. dprintk("%d: read count=%d type=%s\n",
  2529. fh->btv->c.nr, (int)count, v4l2_type_names[fh->type]);
  2530. switch (fh->type) {
  2531. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  2532. if (!check_alloc_btres_lock(fh->btv, fh, RESOURCE_VIDEO_READ)) {
  2533. /* VIDEO_READ in use by another fh,
  2534. or VIDEO_STREAM by any fh. */
  2535. return -EBUSY;
  2536. }
  2537. retval = videobuf_read_one(&fh->cap, data, count, ppos,
  2538. file->f_flags & O_NONBLOCK);
  2539. free_btres_lock(fh->btv, fh, RESOURCE_VIDEO_READ);
  2540. break;
  2541. case V4L2_BUF_TYPE_VBI_CAPTURE:
  2542. if (!check_alloc_btres_lock(fh->btv,fh,RESOURCE_VBI))
  2543. return -EBUSY;
  2544. retval = videobuf_read_stream(&fh->vbi, data, count, ppos, 1,
  2545. file->f_flags & O_NONBLOCK);
  2546. break;
  2547. default:
  2548. BUG();
  2549. }
  2550. return retval;
  2551. }
  2552. static unsigned int bttv_poll(struct file *file, poll_table *wait)
  2553. {
  2554. struct bttv_fh *fh = file->private_data;
  2555. struct bttv_buffer *buf;
  2556. enum v4l2_field field;
  2557. unsigned int rc = 0;
  2558. unsigned long req_events = poll_requested_events(wait);
  2559. if (v4l2_event_pending(&fh->fh))
  2560. rc = POLLPRI;
  2561. else if (req_events & POLLPRI)
  2562. poll_wait(file, &fh->fh.wait, wait);
  2563. if (!(req_events & (POLLIN | POLLRDNORM)))
  2564. return rc;
  2565. if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
  2566. if (!check_alloc_btres_lock(fh->btv,fh,RESOURCE_VBI))
  2567. return rc | POLLERR;
  2568. return rc | videobuf_poll_stream(file, &fh->vbi, wait);
  2569. }
  2570. if (check_btres(fh,RESOURCE_VIDEO_STREAM)) {
  2571. /* streaming capture */
  2572. if (list_empty(&fh->cap.stream))
  2573. return rc | POLLERR;
  2574. buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream);
  2575. } else {
  2576. /* read() capture */
  2577. if (NULL == fh->cap.read_buf) {
  2578. /* need to capture a new frame */
  2579. if (locked_btres(fh->btv,RESOURCE_VIDEO_STREAM))
  2580. return rc | POLLERR;
  2581. fh->cap.read_buf = videobuf_sg_alloc(fh->cap.msize);
  2582. if (NULL == fh->cap.read_buf)
  2583. return rc | POLLERR;
  2584. fh->cap.read_buf->memory = V4L2_MEMORY_USERPTR;
  2585. field = videobuf_next_field(&fh->cap);
  2586. if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,field)) {
  2587. kfree (fh->cap.read_buf);
  2588. fh->cap.read_buf = NULL;
  2589. return rc | POLLERR;
  2590. }
  2591. fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
  2592. fh->cap.read_off = 0;
  2593. }
  2594. buf = (struct bttv_buffer*)fh->cap.read_buf;
  2595. }
  2596. poll_wait(file, &buf->vb.done, wait);
  2597. if (buf->vb.state == VIDEOBUF_DONE ||
  2598. buf->vb.state == VIDEOBUF_ERROR)
  2599. rc = rc | POLLIN|POLLRDNORM;
  2600. return rc;
  2601. }
  2602. static int bttv_open(struct file *file)
  2603. {
  2604. struct video_device *vdev = video_devdata(file);
  2605. struct bttv *btv = video_drvdata(file);
  2606. struct bttv_fh *fh;
  2607. enum v4l2_buf_type type = 0;
  2608. dprintk("open dev=%s\n", video_device_node_name(vdev));
  2609. if (vdev->vfl_type == VFL_TYPE_GRABBER) {
  2610. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  2611. } else if (vdev->vfl_type == VFL_TYPE_VBI) {
  2612. type = V4L2_BUF_TYPE_VBI_CAPTURE;
  2613. } else {
  2614. WARN_ON(1);
  2615. return -ENODEV;
  2616. }
  2617. dprintk("%d: open called (type=%s)\n",
  2618. btv->c.nr, v4l2_type_names[type]);
  2619. /* allocate per filehandle data */
  2620. fh = kmalloc(sizeof(*fh), GFP_KERNEL);
  2621. if (unlikely(!fh))
  2622. return -ENOMEM;
  2623. btv->users++;
  2624. file->private_data = fh;
  2625. *fh = btv->init;
  2626. v4l2_fh_init(&fh->fh, vdev);
  2627. fh->type = type;
  2628. fh->ov.setup_ok = 0;
  2629. videobuf_queue_sg_init(&fh->cap, &bttv_video_qops,
  2630. &btv->c.pci->dev, &btv->s_lock,
  2631. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  2632. V4L2_FIELD_INTERLACED,
  2633. sizeof(struct bttv_buffer),
  2634. fh, &btv->lock);
  2635. videobuf_queue_sg_init(&fh->vbi, &bttv_vbi_qops,
  2636. &btv->c.pci->dev, &btv->s_lock,
  2637. V4L2_BUF_TYPE_VBI_CAPTURE,
  2638. V4L2_FIELD_SEQ_TB,
  2639. sizeof(struct bttv_buffer),
  2640. fh, &btv->lock);
  2641. set_tvnorm(btv,btv->tvnorm);
  2642. set_input(btv, btv->input, btv->tvnorm);
  2643. audio_mute(btv, btv->mute);
  2644. /* The V4L2 spec requires one global set of cropping parameters
  2645. which only change on request. These are stored in btv->crop[1].
  2646. However for compatibility with V4L apps and cropping unaware
  2647. V4L2 apps we now reset the cropping parameters as seen through
  2648. this fh, which is to say VIDIOC_G_SELECTION and scaling limit checks
  2649. will use btv->crop[0], the default cropping parameters for the
  2650. current video standard, and VIDIOC_S_FMT will not implicitely
  2651. change the cropping parameters until VIDIOC_S_SELECTION has been
  2652. called. */
  2653. fh->do_crop = !reset_crop; /* module parameter */
  2654. /* Likewise there should be one global set of VBI capture
  2655. parameters, but for compatibility with V4L apps and earlier
  2656. driver versions each fh has its own parameters. */
  2657. bttv_vbi_fmt_reset(&fh->vbi_fmt, btv->tvnorm);
  2658. bttv_field_count(btv);
  2659. v4l2_fh_add(&fh->fh);
  2660. return 0;
  2661. }
  2662. static int bttv_release(struct file *file)
  2663. {
  2664. struct bttv_fh *fh = file->private_data;
  2665. struct bttv *btv = fh->btv;
  2666. /* turn off overlay */
  2667. if (check_btres(fh, RESOURCE_OVERLAY))
  2668. bttv_switch_overlay(btv,fh,NULL);
  2669. /* stop video capture */
  2670. if (check_btres(fh, RESOURCE_VIDEO_STREAM)) {
  2671. videobuf_streamoff(&fh->cap);
  2672. free_btres_lock(btv,fh,RESOURCE_VIDEO_STREAM);
  2673. }
  2674. if (fh->cap.read_buf) {
  2675. buffer_release(&fh->cap,fh->cap.read_buf);
  2676. kfree(fh->cap.read_buf);
  2677. }
  2678. if (check_btres(fh, RESOURCE_VIDEO_READ)) {
  2679. free_btres_lock(btv, fh, RESOURCE_VIDEO_READ);
  2680. }
  2681. /* stop vbi capture */
  2682. if (check_btres(fh, RESOURCE_VBI)) {
  2683. videobuf_stop(&fh->vbi);
  2684. free_btres_lock(btv,fh,RESOURCE_VBI);
  2685. }
  2686. /* free stuff */
  2687. videobuf_mmap_free(&fh->cap);
  2688. videobuf_mmap_free(&fh->vbi);
  2689. file->private_data = NULL;
  2690. btv->users--;
  2691. bttv_field_count(btv);
  2692. if (!btv->users)
  2693. audio_mute(btv, btv->mute);
  2694. v4l2_fh_del(&fh->fh);
  2695. v4l2_fh_exit(&fh->fh);
  2696. kfree(fh);
  2697. return 0;
  2698. }
  2699. static int
  2700. bttv_mmap(struct file *file, struct vm_area_struct *vma)
  2701. {
  2702. struct bttv_fh *fh = file->private_data;
  2703. dprintk("%d: mmap type=%s 0x%lx+%ld\n",
  2704. fh->btv->c.nr, v4l2_type_names[fh->type],
  2705. vma->vm_start, vma->vm_end - vma->vm_start);
  2706. return videobuf_mmap_mapper(bttv_queue(fh),vma);
  2707. }
  2708. static const struct v4l2_file_operations bttv_fops =
  2709. {
  2710. .owner = THIS_MODULE,
  2711. .open = bttv_open,
  2712. .release = bttv_release,
  2713. .unlocked_ioctl = video_ioctl2,
  2714. .read = bttv_read,
  2715. .mmap = bttv_mmap,
  2716. .poll = bttv_poll,
  2717. };
  2718. static const struct v4l2_ioctl_ops bttv_ioctl_ops = {
  2719. .vidioc_querycap = bttv_querycap,
  2720. .vidioc_enum_fmt_vid_cap = bttv_enum_fmt_vid_cap,
  2721. .vidioc_g_fmt_vid_cap = bttv_g_fmt_vid_cap,
  2722. .vidioc_try_fmt_vid_cap = bttv_try_fmt_vid_cap,
  2723. .vidioc_s_fmt_vid_cap = bttv_s_fmt_vid_cap,
  2724. .vidioc_enum_fmt_vid_overlay = bttv_enum_fmt_vid_overlay,
  2725. .vidioc_g_fmt_vid_overlay = bttv_g_fmt_vid_overlay,
  2726. .vidioc_try_fmt_vid_overlay = bttv_try_fmt_vid_overlay,
  2727. .vidioc_s_fmt_vid_overlay = bttv_s_fmt_vid_overlay,
  2728. .vidioc_g_fmt_vbi_cap = bttv_g_fmt_vbi_cap,
  2729. .vidioc_try_fmt_vbi_cap = bttv_try_fmt_vbi_cap,
  2730. .vidioc_s_fmt_vbi_cap = bttv_s_fmt_vbi_cap,
  2731. .vidioc_cropcap = bttv_cropcap,
  2732. .vidioc_reqbufs = bttv_reqbufs,
  2733. .vidioc_querybuf = bttv_querybuf,
  2734. .vidioc_qbuf = bttv_qbuf,
  2735. .vidioc_dqbuf = bttv_dqbuf,
  2736. .vidioc_s_std = bttv_s_std,
  2737. .vidioc_g_std = bttv_g_std,
  2738. .vidioc_enum_input = bttv_enum_input,
  2739. .vidioc_g_input = bttv_g_input,
  2740. .vidioc_s_input = bttv_s_input,
  2741. .vidioc_streamon = bttv_streamon,
  2742. .vidioc_streamoff = bttv_streamoff,
  2743. .vidioc_g_tuner = bttv_g_tuner,
  2744. .vidioc_s_tuner = bttv_s_tuner,
  2745. .vidioc_g_selection = bttv_g_selection,
  2746. .vidioc_s_selection = bttv_s_selection,
  2747. .vidioc_g_fbuf = bttv_g_fbuf,
  2748. .vidioc_s_fbuf = bttv_s_fbuf,
  2749. .vidioc_overlay = bttv_overlay,
  2750. .vidioc_g_parm = bttv_g_parm,
  2751. .vidioc_g_frequency = bttv_g_frequency,
  2752. .vidioc_s_frequency = bttv_s_frequency,
  2753. .vidioc_log_status = bttv_log_status,
  2754. .vidioc_querystd = bttv_querystd,
  2755. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  2756. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  2757. #ifdef CONFIG_VIDEO_ADV_DEBUG
  2758. .vidioc_g_register = bttv_g_register,
  2759. .vidioc_s_register = bttv_s_register,
  2760. #endif
  2761. };
  2762. static struct video_device bttv_video_template = {
  2763. .fops = &bttv_fops,
  2764. .ioctl_ops = &bttv_ioctl_ops,
  2765. .tvnorms = BTTV_NORMS,
  2766. };
  2767. /* ----------------------------------------------------------------------- */
  2768. /* radio interface */
  2769. static int radio_open(struct file *file)
  2770. {
  2771. struct video_device *vdev = video_devdata(file);
  2772. struct bttv *btv = video_drvdata(file);
  2773. struct bttv_fh *fh;
  2774. dprintk("open dev=%s\n", video_device_node_name(vdev));
  2775. dprintk("%d: open called (radio)\n", btv->c.nr);
  2776. /* allocate per filehandle data */
  2777. fh = kmalloc(sizeof(*fh), GFP_KERNEL);
  2778. if (unlikely(!fh))
  2779. return -ENOMEM;
  2780. file->private_data = fh;
  2781. *fh = btv->init;
  2782. v4l2_fh_init(&fh->fh, vdev);
  2783. btv->radio_user++;
  2784. audio_mute(btv, btv->mute);
  2785. v4l2_fh_add(&fh->fh);
  2786. return 0;
  2787. }
  2788. static int radio_release(struct file *file)
  2789. {
  2790. struct bttv_fh *fh = file->private_data;
  2791. struct bttv *btv = fh->btv;
  2792. struct saa6588_command cmd;
  2793. file->private_data = NULL;
  2794. v4l2_fh_del(&fh->fh);
  2795. v4l2_fh_exit(&fh->fh);
  2796. kfree(fh);
  2797. btv->radio_user--;
  2798. bttv_call_all(btv, core, ioctl, SAA6588_CMD_CLOSE, &cmd);
  2799. if (btv->radio_user == 0)
  2800. btv->has_radio_tuner = 0;
  2801. return 0;
  2802. }
  2803. static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
  2804. {
  2805. struct bttv_fh *fh = priv;
  2806. struct bttv *btv = fh->btv;
  2807. if (0 != t->index)
  2808. return -EINVAL;
  2809. strcpy(t->name, "Radio");
  2810. t->type = V4L2_TUNER_RADIO;
  2811. radio_enable(btv);
  2812. bttv_call_all(btv, tuner, g_tuner, t);
  2813. if (btv->audio_mode_gpio)
  2814. btv->audio_mode_gpio(btv, t, 0);
  2815. if (btv->has_tea575x)
  2816. return snd_tea575x_g_tuner(&btv->tea, t);
  2817. return 0;
  2818. }
  2819. static int radio_s_tuner(struct file *file, void *priv,
  2820. const struct v4l2_tuner *t)
  2821. {
  2822. struct bttv_fh *fh = priv;
  2823. struct bttv *btv = fh->btv;
  2824. if (0 != t->index)
  2825. return -EINVAL;
  2826. radio_enable(btv);
  2827. bttv_call_all(btv, tuner, s_tuner, t);
  2828. return 0;
  2829. }
  2830. static int radio_s_hw_freq_seek(struct file *file, void *priv,
  2831. const struct v4l2_hw_freq_seek *a)
  2832. {
  2833. struct bttv_fh *fh = priv;
  2834. struct bttv *btv = fh->btv;
  2835. if (btv->has_tea575x)
  2836. return snd_tea575x_s_hw_freq_seek(file, &btv->tea, a);
  2837. return -ENOTTY;
  2838. }
  2839. static int radio_enum_freq_bands(struct file *file, void *priv,
  2840. struct v4l2_frequency_band *band)
  2841. {
  2842. struct bttv_fh *fh = priv;
  2843. struct bttv *btv = fh->btv;
  2844. if (btv->has_tea575x)
  2845. return snd_tea575x_enum_freq_bands(&btv->tea, band);
  2846. return -ENOTTY;
  2847. }
  2848. static ssize_t radio_read(struct file *file, char __user *data,
  2849. size_t count, loff_t *ppos)
  2850. {
  2851. struct bttv_fh *fh = file->private_data;
  2852. struct bttv *btv = fh->btv;
  2853. struct saa6588_command cmd;
  2854. cmd.block_count = count / 3;
  2855. cmd.nonblocking = file->f_flags & O_NONBLOCK;
  2856. cmd.buffer = data;
  2857. cmd.instance = file;
  2858. cmd.result = -ENODEV;
  2859. radio_enable(btv);
  2860. bttv_call_all(btv, core, ioctl, SAA6588_CMD_READ, &cmd);
  2861. return cmd.result;
  2862. }
  2863. static unsigned int radio_poll(struct file *file, poll_table *wait)
  2864. {
  2865. struct bttv_fh *fh = file->private_data;
  2866. struct bttv *btv = fh->btv;
  2867. unsigned long req_events = poll_requested_events(wait);
  2868. struct saa6588_command cmd;
  2869. unsigned int res = 0;
  2870. if (v4l2_event_pending(&fh->fh))
  2871. res = POLLPRI;
  2872. else if (req_events & POLLPRI)
  2873. poll_wait(file, &fh->fh.wait, wait);
  2874. radio_enable(btv);
  2875. cmd.instance = file;
  2876. cmd.event_list = wait;
  2877. cmd.result = res;
  2878. bttv_call_all(btv, core, ioctl, SAA6588_CMD_POLL, &cmd);
  2879. return cmd.result;
  2880. }
  2881. static const struct v4l2_file_operations radio_fops =
  2882. {
  2883. .owner = THIS_MODULE,
  2884. .open = radio_open,
  2885. .read = radio_read,
  2886. .release = radio_release,
  2887. .unlocked_ioctl = video_ioctl2,
  2888. .poll = radio_poll,
  2889. };
  2890. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  2891. .vidioc_querycap = bttv_querycap,
  2892. .vidioc_log_status = bttv_log_status,
  2893. .vidioc_g_tuner = radio_g_tuner,
  2894. .vidioc_s_tuner = radio_s_tuner,
  2895. .vidioc_g_frequency = bttv_g_frequency,
  2896. .vidioc_s_frequency = bttv_s_frequency,
  2897. .vidioc_s_hw_freq_seek = radio_s_hw_freq_seek,
  2898. .vidioc_enum_freq_bands = radio_enum_freq_bands,
  2899. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  2900. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  2901. };
  2902. static struct video_device radio_template = {
  2903. .fops = &radio_fops,
  2904. .ioctl_ops = &radio_ioctl_ops,
  2905. };
  2906. /* ----------------------------------------------------------------------- */
  2907. /* some debug code */
  2908. static int bttv_risc_decode(u32 risc)
  2909. {
  2910. static char *instr[16] = {
  2911. [ BT848_RISC_WRITE >> 28 ] = "write",
  2912. [ BT848_RISC_SKIP >> 28 ] = "skip",
  2913. [ BT848_RISC_WRITEC >> 28 ] = "writec",
  2914. [ BT848_RISC_JUMP >> 28 ] = "jump",
  2915. [ BT848_RISC_SYNC >> 28 ] = "sync",
  2916. [ BT848_RISC_WRITE123 >> 28 ] = "write123",
  2917. [ BT848_RISC_SKIP123 >> 28 ] = "skip123",
  2918. [ BT848_RISC_WRITE1S23 >> 28 ] = "write1s23",
  2919. };
  2920. static int incr[16] = {
  2921. [ BT848_RISC_WRITE >> 28 ] = 2,
  2922. [ BT848_RISC_JUMP >> 28 ] = 2,
  2923. [ BT848_RISC_SYNC >> 28 ] = 2,
  2924. [ BT848_RISC_WRITE123 >> 28 ] = 5,
  2925. [ BT848_RISC_SKIP123 >> 28 ] = 2,
  2926. [ BT848_RISC_WRITE1S23 >> 28 ] = 3,
  2927. };
  2928. static char *bits[] = {
  2929. "be0", "be1", "be2", "be3/resync",
  2930. "set0", "set1", "set2", "set3",
  2931. "clr0", "clr1", "clr2", "clr3",
  2932. "irq", "res", "eol", "sol",
  2933. };
  2934. int i;
  2935. pr_cont("0x%08x [ %s", risc,
  2936. instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
  2937. for (i = ARRAY_SIZE(bits)-1; i >= 0; i--)
  2938. if (risc & (1 << (i + 12)))
  2939. pr_cont(" %s", bits[i]);
  2940. pr_cont(" count=%d ]\n", risc & 0xfff);
  2941. return incr[risc >> 28] ? incr[risc >> 28] : 1;
  2942. }
  2943. static void bttv_risc_disasm(struct bttv *btv,
  2944. struct btcx_riscmem *risc)
  2945. {
  2946. unsigned int i,j,n;
  2947. pr_info("%s: risc disasm: %p [dma=0x%08lx]\n",
  2948. btv->c.v4l2_dev.name, risc->cpu, (unsigned long)risc->dma);
  2949. for (i = 0; i < (risc->size >> 2); i += n) {
  2950. pr_info("%s: 0x%lx: ",
  2951. btv->c.v4l2_dev.name,
  2952. (unsigned long)(risc->dma + (i<<2)));
  2953. n = bttv_risc_decode(le32_to_cpu(risc->cpu[i]));
  2954. for (j = 1; j < n; j++)
  2955. pr_info("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
  2956. btv->c.v4l2_dev.name,
  2957. (unsigned long)(risc->dma + ((i+j)<<2)),
  2958. risc->cpu[i+j], j);
  2959. if (0 == risc->cpu[i])
  2960. break;
  2961. }
  2962. }
  2963. static void bttv_print_riscaddr(struct bttv *btv)
  2964. {
  2965. pr_info(" main: %08llx\n", (unsigned long long)btv->main.dma);
  2966. pr_info(" vbi : o=%08llx e=%08llx\n",
  2967. btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
  2968. btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0);
  2969. pr_info(" cap : o=%08llx e=%08llx\n",
  2970. btv->curr.top
  2971. ? (unsigned long long)btv->curr.top->top.dma : 0,
  2972. btv->curr.bottom
  2973. ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
  2974. pr_info(" scr : o=%08llx e=%08llx\n",
  2975. btv->screen ? (unsigned long long)btv->screen->top.dma : 0,
  2976. btv->screen ? (unsigned long long)btv->screen->bottom.dma : 0);
  2977. bttv_risc_disasm(btv, &btv->main);
  2978. }
  2979. /* ----------------------------------------------------------------------- */
  2980. /* irq handler */
  2981. static char *irq_name[] = {
  2982. "FMTCHG", // format change detected (525 vs. 625)
  2983. "VSYNC", // vertical sync (new field)
  2984. "HSYNC", // horizontal sync
  2985. "OFLOW", // chroma/luma AGC overflow
  2986. "HLOCK", // horizontal lock changed
  2987. "VPRES", // video presence changed
  2988. "6", "7",
  2989. "I2CDONE", // hw irc operation finished
  2990. "GPINT", // gpio port triggered irq
  2991. "10",
  2992. "RISCI", // risc instruction triggered irq
  2993. "FBUS", // pixel data fifo dropped data (high pci bus latencies)
  2994. "FTRGT", // pixel data fifo overrun
  2995. "FDSR", // fifo data stream resyncronisation
  2996. "PPERR", // parity error (data transfer)
  2997. "RIPERR", // parity error (read risc instructions)
  2998. "PABORT", // pci abort
  2999. "OCERR", // risc instruction error
  3000. "SCERR", // syncronisation error
  3001. };
  3002. static void bttv_print_irqbits(u32 print, u32 mark)
  3003. {
  3004. unsigned int i;
  3005. pr_cont("bits:");
  3006. for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
  3007. if (print & (1 << i))
  3008. pr_cont(" %s", irq_name[i]);
  3009. if (mark & (1 << i))
  3010. pr_cont("*");
  3011. }
  3012. }
  3013. static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc)
  3014. {
  3015. pr_warn("%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
  3016. btv->c.nr,
  3017. (unsigned long)btv->main.dma,
  3018. (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_VBI+1]),
  3019. (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_FIELD+1]),
  3020. (unsigned long)rc);
  3021. if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) {
  3022. pr_notice("%d: Oh, there (temporarily?) is no input signal. "
  3023. "Ok, then this is harmless, don't worry ;)\n",
  3024. btv->c.nr);
  3025. return;
  3026. }
  3027. pr_notice("%d: Uhm. Looks like we have unusual high IRQ latencies\n",
  3028. btv->c.nr);
  3029. pr_notice("%d: Lets try to catch the culpit red-handed ...\n",
  3030. btv->c.nr);
  3031. dump_stack();
  3032. }
  3033. static int
  3034. bttv_irq_next_video(struct bttv *btv, struct bttv_buffer_set *set)
  3035. {
  3036. struct bttv_buffer *item;
  3037. memset(set,0,sizeof(*set));
  3038. /* capture request ? */
  3039. if (!list_empty(&btv->capture)) {
  3040. set->frame_irq = 1;
  3041. item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
  3042. if (V4L2_FIELD_HAS_TOP(item->vb.field))
  3043. set->top = item;
  3044. if (V4L2_FIELD_HAS_BOTTOM(item->vb.field))
  3045. set->bottom = item;
  3046. /* capture request for other field ? */
  3047. if (!V4L2_FIELD_HAS_BOTH(item->vb.field) &&
  3048. (item->vb.queue.next != &btv->capture)) {
  3049. item = list_entry(item->vb.queue.next, struct bttv_buffer, vb.queue);
  3050. /* Mike Isely <isely@pobox.com> - Only check
  3051. * and set up the bottom field in the logic
  3052. * below. Don't ever do the top field. This
  3053. * of course means that if we set up the
  3054. * bottom field in the above code that we'll
  3055. * actually skip a field. But that's OK.
  3056. * Having processed only a single buffer this
  3057. * time, then the next time around the first
  3058. * available buffer should be for a top field.
  3059. * That will then cause us here to set up a
  3060. * top then a bottom field in the normal way.
  3061. * The alternative to this understanding is
  3062. * that we set up the second available buffer
  3063. * as a top field, but that's out of order
  3064. * since this driver always processes the top
  3065. * field first - the effect will be the two
  3066. * buffers being returned in the wrong order,
  3067. * with the second buffer also being delayed
  3068. * by one field time (owing to the fifo nature
  3069. * of videobuf). Worse still, we'll be stuck
  3070. * doing fields out of order now every time
  3071. * until something else causes a field to be
  3072. * dropped. By effectively forcing a field to
  3073. * drop this way then we always get back into
  3074. * sync within a single frame time. (Out of
  3075. * order fields can screw up deinterlacing
  3076. * algorithms.) */
  3077. if (!V4L2_FIELD_HAS_BOTH(item->vb.field)) {
  3078. if (NULL == set->bottom &&
  3079. V4L2_FIELD_BOTTOM == item->vb.field) {
  3080. set->bottom = item;
  3081. }
  3082. if (NULL != set->top && NULL != set->bottom)
  3083. set->top_irq = 2;
  3084. }
  3085. }
  3086. }
  3087. /* screen overlay ? */
  3088. if (NULL != btv->screen) {
  3089. if (V4L2_FIELD_HAS_BOTH(btv->screen->vb.field)) {
  3090. if (NULL == set->top && NULL == set->bottom) {
  3091. set->top = btv->screen;
  3092. set->bottom = btv->screen;
  3093. }
  3094. } else {
  3095. if (V4L2_FIELD_TOP == btv->screen->vb.field &&
  3096. NULL == set->top) {
  3097. set->top = btv->screen;
  3098. }
  3099. if (V4L2_FIELD_BOTTOM == btv->screen->vb.field &&
  3100. NULL == set->bottom) {
  3101. set->bottom = btv->screen;
  3102. }
  3103. }
  3104. }
  3105. dprintk("%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
  3106. btv->c.nr, set->top, set->bottom,
  3107. btv->screen, set->frame_irq, set->top_irq);
  3108. return 0;
  3109. }
  3110. static void
  3111. bttv_irq_wakeup_video(struct bttv *btv, struct bttv_buffer_set *wakeup,
  3112. struct bttv_buffer_set *curr, unsigned int state)
  3113. {
  3114. struct timeval ts;
  3115. v4l2_get_timestamp(&ts);
  3116. if (wakeup->top == wakeup->bottom) {
  3117. if (NULL != wakeup->top && curr->top != wakeup->top) {
  3118. if (irq_debug > 1)
  3119. pr_debug("%d: wakeup: both=%p\n",
  3120. btv->c.nr, wakeup->top);
  3121. wakeup->top->vb.ts = ts;
  3122. wakeup->top->vb.field_count = btv->field_count;
  3123. wakeup->top->vb.state = state;
  3124. wake_up(&wakeup->top->vb.done);
  3125. }
  3126. } else {
  3127. if (NULL != wakeup->top && curr->top != wakeup->top) {
  3128. if (irq_debug > 1)
  3129. pr_debug("%d: wakeup: top=%p\n",
  3130. btv->c.nr, wakeup->top);
  3131. wakeup->top->vb.ts = ts;
  3132. wakeup->top->vb.field_count = btv->field_count;
  3133. wakeup->top->vb.state = state;
  3134. wake_up(&wakeup->top->vb.done);
  3135. }
  3136. if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
  3137. if (irq_debug > 1)
  3138. pr_debug("%d: wakeup: bottom=%p\n",
  3139. btv->c.nr, wakeup->bottom);
  3140. wakeup->bottom->vb.ts = ts;
  3141. wakeup->bottom->vb.field_count = btv->field_count;
  3142. wakeup->bottom->vb.state = state;
  3143. wake_up(&wakeup->bottom->vb.done);
  3144. }
  3145. }
  3146. }
  3147. static void
  3148. bttv_irq_wakeup_vbi(struct bttv *btv, struct bttv_buffer *wakeup,
  3149. unsigned int state)
  3150. {
  3151. if (NULL == wakeup)
  3152. return;
  3153. v4l2_get_timestamp(&wakeup->vb.ts);
  3154. wakeup->vb.field_count = btv->field_count;
  3155. wakeup->vb.state = state;
  3156. wake_up(&wakeup->vb.done);
  3157. }
  3158. static void bttv_irq_timeout(unsigned long data)
  3159. {
  3160. struct bttv *btv = (struct bttv *)data;
  3161. struct bttv_buffer_set old,new;
  3162. struct bttv_buffer *ovbi;
  3163. struct bttv_buffer *item;
  3164. unsigned long flags;
  3165. if (bttv_verbose) {
  3166. pr_info("%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
  3167. btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total,
  3168. btread(BT848_RISC_COUNT));
  3169. bttv_print_irqbits(btread(BT848_INT_STAT),0);
  3170. pr_cont("\n");
  3171. }
  3172. spin_lock_irqsave(&btv->s_lock,flags);
  3173. /* deactivate stuff */
  3174. memset(&new,0,sizeof(new));
  3175. old = btv->curr;
  3176. ovbi = btv->cvbi;
  3177. btv->curr = new;
  3178. btv->cvbi = NULL;
  3179. btv->loop_irq = 0;
  3180. bttv_buffer_activate_video(btv, &new);
  3181. bttv_buffer_activate_vbi(btv, NULL);
  3182. bttv_set_dma(btv, 0);
  3183. /* wake up */
  3184. bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_ERROR);
  3185. bttv_irq_wakeup_vbi(btv, ovbi, VIDEOBUF_ERROR);
  3186. /* cancel all outstanding capture / vbi requests */
  3187. while (!list_empty(&btv->capture)) {
  3188. item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
  3189. list_del(&item->vb.queue);
  3190. item->vb.state = VIDEOBUF_ERROR;
  3191. wake_up(&item->vb.done);
  3192. }
  3193. while (!list_empty(&btv->vcapture)) {
  3194. item = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
  3195. list_del(&item->vb.queue);
  3196. item->vb.state = VIDEOBUF_ERROR;
  3197. wake_up(&item->vb.done);
  3198. }
  3199. btv->errors++;
  3200. spin_unlock_irqrestore(&btv->s_lock,flags);
  3201. }
  3202. static void
  3203. bttv_irq_wakeup_top(struct bttv *btv)
  3204. {
  3205. struct bttv_buffer *wakeup = btv->curr.top;
  3206. if (NULL == wakeup)
  3207. return;
  3208. spin_lock(&btv->s_lock);
  3209. btv->curr.top_irq = 0;
  3210. btv->curr.top = NULL;
  3211. bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
  3212. v4l2_get_timestamp(&wakeup->vb.ts);
  3213. wakeup->vb.field_count = btv->field_count;
  3214. wakeup->vb.state = VIDEOBUF_DONE;
  3215. wake_up(&wakeup->vb.done);
  3216. spin_unlock(&btv->s_lock);
  3217. }
  3218. static inline int is_active(struct btcx_riscmem *risc, u32 rc)
  3219. {
  3220. if (rc < risc->dma)
  3221. return 0;
  3222. if (rc > risc->dma + risc->size)
  3223. return 0;
  3224. return 1;
  3225. }
  3226. static void
  3227. bttv_irq_switch_video(struct bttv *btv)
  3228. {
  3229. struct bttv_buffer_set new;
  3230. struct bttv_buffer_set old;
  3231. dma_addr_t rc;
  3232. spin_lock(&btv->s_lock);
  3233. /* new buffer set */
  3234. bttv_irq_next_video(btv, &new);
  3235. rc = btread(BT848_RISC_COUNT);
  3236. if ((btv->curr.top && is_active(&btv->curr.top->top, rc)) ||
  3237. (btv->curr.bottom && is_active(&btv->curr.bottom->bottom, rc))) {
  3238. btv->framedrop++;
  3239. if (debug_latency)
  3240. bttv_irq_debug_low_latency(btv, rc);
  3241. spin_unlock(&btv->s_lock);
  3242. return;
  3243. }
  3244. /* switch over */
  3245. old = btv->curr;
  3246. btv->curr = new;
  3247. btv->loop_irq &= ~1;
  3248. bttv_buffer_activate_video(btv, &new);
  3249. bttv_set_dma(btv, 0);
  3250. /* switch input */
  3251. if (UNSET != btv->new_input) {
  3252. video_mux(btv,btv->new_input);
  3253. btv->new_input = UNSET;
  3254. }
  3255. /* wake up finished buffers */
  3256. bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_DONE);
  3257. spin_unlock(&btv->s_lock);
  3258. }
  3259. static void
  3260. bttv_irq_switch_vbi(struct bttv *btv)
  3261. {
  3262. struct bttv_buffer *new = NULL;
  3263. struct bttv_buffer *old;
  3264. u32 rc;
  3265. spin_lock(&btv->s_lock);
  3266. if (!list_empty(&btv->vcapture))
  3267. new = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
  3268. old = btv->cvbi;
  3269. rc = btread(BT848_RISC_COUNT);
  3270. if (NULL != old && (is_active(&old->top, rc) ||
  3271. is_active(&old->bottom, rc))) {
  3272. btv->framedrop++;
  3273. if (debug_latency)
  3274. bttv_irq_debug_low_latency(btv, rc);
  3275. spin_unlock(&btv->s_lock);
  3276. return;
  3277. }
  3278. /* switch */
  3279. btv->cvbi = new;
  3280. btv->loop_irq &= ~4;
  3281. bttv_buffer_activate_vbi(btv, new);
  3282. bttv_set_dma(btv, 0);
  3283. bttv_irq_wakeup_vbi(btv, old, VIDEOBUF_DONE);
  3284. spin_unlock(&btv->s_lock);
  3285. }
  3286. static irqreturn_t bttv_irq(int irq, void *dev_id)
  3287. {
  3288. u32 stat,astat;
  3289. u32 dstat;
  3290. int count;
  3291. struct bttv *btv;
  3292. int handled = 0;
  3293. btv=(struct bttv *)dev_id;
  3294. count=0;
  3295. while (1) {
  3296. /* get/clear interrupt status bits */
  3297. stat=btread(BT848_INT_STAT);
  3298. astat=stat&btread(BT848_INT_MASK);
  3299. if (!astat)
  3300. break;
  3301. handled = 1;
  3302. btwrite(stat,BT848_INT_STAT);
  3303. /* get device status bits */
  3304. dstat=btread(BT848_DSTATUS);
  3305. if (irq_debug) {
  3306. pr_debug("%d: irq loop=%d fc=%d riscs=%x, riscc=%08x, ",
  3307. btv->c.nr, count, btv->field_count,
  3308. stat>>28, btread(BT848_RISC_COUNT));
  3309. bttv_print_irqbits(stat,astat);
  3310. if (stat & BT848_INT_HLOCK)
  3311. pr_cont(" HLOC => %s",
  3312. dstat & BT848_DSTATUS_HLOC
  3313. ? "yes" : "no");
  3314. if (stat & BT848_INT_VPRES)
  3315. pr_cont(" PRES => %s",
  3316. dstat & BT848_DSTATUS_PRES
  3317. ? "yes" : "no");
  3318. if (stat & BT848_INT_FMTCHG)
  3319. pr_cont(" NUML => %s",
  3320. dstat & BT848_DSTATUS_NUML
  3321. ? "625" : "525");
  3322. pr_cont("\n");
  3323. }
  3324. if (astat&BT848_INT_VSYNC)
  3325. btv->field_count++;
  3326. if ((astat & BT848_INT_GPINT) && btv->remote) {
  3327. bttv_input_irq(btv);
  3328. }
  3329. if (astat & BT848_INT_I2CDONE) {
  3330. btv->i2c_done = stat;
  3331. wake_up(&btv->i2c_queue);
  3332. }
  3333. if ((astat & BT848_INT_RISCI) && (stat & (4<<28)))
  3334. bttv_irq_switch_vbi(btv);
  3335. if ((astat & BT848_INT_RISCI) && (stat & (2<<28)))
  3336. bttv_irq_wakeup_top(btv);
  3337. if ((astat & BT848_INT_RISCI) && (stat & (1<<28)))
  3338. bttv_irq_switch_video(btv);
  3339. if ((astat & BT848_INT_HLOCK) && btv->opt_automute)
  3340. /* trigger automute */
  3341. audio_mux_gpio(btv, btv->audio_input, btv->mute);
  3342. if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
  3343. pr_info("%d: %s%s @ %08x,",
  3344. btv->c.nr,
  3345. (astat & BT848_INT_SCERR) ? "SCERR" : "",
  3346. (astat & BT848_INT_OCERR) ? "OCERR" : "",
  3347. btread(BT848_RISC_COUNT));
  3348. bttv_print_irqbits(stat,astat);
  3349. pr_cont("\n");
  3350. if (bttv_debug)
  3351. bttv_print_riscaddr(btv);
  3352. }
  3353. if (fdsr && astat & BT848_INT_FDSR) {
  3354. pr_info("%d: FDSR @ %08x\n",
  3355. btv->c.nr, btread(BT848_RISC_COUNT));
  3356. if (bttv_debug)
  3357. bttv_print_riscaddr(btv);
  3358. }
  3359. count++;
  3360. if (count > 4) {
  3361. if (count > 8 || !(astat & BT848_INT_GPINT)) {
  3362. btwrite(0, BT848_INT_MASK);
  3363. pr_err("%d: IRQ lockup, cleared int mask [",
  3364. btv->c.nr);
  3365. } else {
  3366. pr_err("%d: IRQ lockup, clearing GPINT from int mask [",
  3367. btv->c.nr);
  3368. btwrite(btread(BT848_INT_MASK) & (-1 ^ BT848_INT_GPINT),
  3369. BT848_INT_MASK);
  3370. }
  3371. bttv_print_irqbits(stat,astat);
  3372. pr_cont("]\n");
  3373. }
  3374. }
  3375. btv->irq_total++;
  3376. if (handled)
  3377. btv->irq_me++;
  3378. return IRQ_RETVAL(handled);
  3379. }
  3380. /* ----------------------------------------------------------------------- */
  3381. /* initialization */
  3382. static void vdev_init(struct bttv *btv,
  3383. struct video_device *vfd,
  3384. const struct video_device *template,
  3385. const char *type_name)
  3386. {
  3387. *vfd = *template;
  3388. vfd->v4l2_dev = &btv->c.v4l2_dev;
  3389. vfd->release = video_device_release_empty;
  3390. video_set_drvdata(vfd, btv);
  3391. snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
  3392. btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
  3393. type_name, bttv_tvcards[btv->c.type].name);
  3394. if (btv->tuner_type == TUNER_ABSENT) {
  3395. v4l2_disable_ioctl(vfd, VIDIOC_G_FREQUENCY);
  3396. v4l2_disable_ioctl(vfd, VIDIOC_S_FREQUENCY);
  3397. v4l2_disable_ioctl(vfd, VIDIOC_G_TUNER);
  3398. v4l2_disable_ioctl(vfd, VIDIOC_S_TUNER);
  3399. }
  3400. }
  3401. static void bttv_unregister_video(struct bttv *btv)
  3402. {
  3403. video_unregister_device(&btv->video_dev);
  3404. video_unregister_device(&btv->vbi_dev);
  3405. video_unregister_device(&btv->radio_dev);
  3406. }
  3407. /* register video4linux devices */
  3408. static int bttv_register_video(struct bttv *btv)
  3409. {
  3410. if (no_overlay > 0)
  3411. pr_notice("Overlay support disabled\n");
  3412. /* video */
  3413. vdev_init(btv, &btv->video_dev, &bttv_video_template, "video");
  3414. if (video_register_device(&btv->video_dev, VFL_TYPE_GRABBER,
  3415. video_nr[btv->c.nr]) < 0)
  3416. goto err;
  3417. pr_info("%d: registered device %s\n",
  3418. btv->c.nr, video_device_node_name(&btv->video_dev));
  3419. if (device_create_file(&btv->video_dev.dev,
  3420. &dev_attr_card)<0) {
  3421. pr_err("%d: device_create_file 'card' failed\n", btv->c.nr);
  3422. goto err;
  3423. }
  3424. /* vbi */
  3425. vdev_init(btv, &btv->vbi_dev, &bttv_video_template, "vbi");
  3426. if (video_register_device(&btv->vbi_dev, VFL_TYPE_VBI,
  3427. vbi_nr[btv->c.nr]) < 0)
  3428. goto err;
  3429. pr_info("%d: registered device %s\n",
  3430. btv->c.nr, video_device_node_name(&btv->vbi_dev));
  3431. if (!btv->has_radio)
  3432. return 0;
  3433. /* radio */
  3434. vdev_init(btv, &btv->radio_dev, &radio_template, "radio");
  3435. btv->radio_dev.ctrl_handler = &btv->radio_ctrl_handler;
  3436. if (video_register_device(&btv->radio_dev, VFL_TYPE_RADIO,
  3437. radio_nr[btv->c.nr]) < 0)
  3438. goto err;
  3439. pr_info("%d: registered device %s\n",
  3440. btv->c.nr, video_device_node_name(&btv->radio_dev));
  3441. /* all done */
  3442. return 0;
  3443. err:
  3444. bttv_unregister_video(btv);
  3445. return -1;
  3446. }
  3447. /* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
  3448. /* response on cards with no firmware is not enabled by OF */
  3449. static void pci_set_command(struct pci_dev *dev)
  3450. {
  3451. #if defined(__powerpc__)
  3452. unsigned int cmd;
  3453. pci_read_config_dword(dev, PCI_COMMAND, &cmd);
  3454. cmd = (cmd | PCI_COMMAND_MEMORY );
  3455. pci_write_config_dword(dev, PCI_COMMAND, cmd);
  3456. #endif
  3457. }
  3458. static int bttv_probe(struct pci_dev *dev, const struct pci_device_id *pci_id)
  3459. {
  3460. struct v4l2_frequency init_freq = {
  3461. .tuner = 0,
  3462. .type = V4L2_TUNER_ANALOG_TV,
  3463. .frequency = 980,
  3464. };
  3465. int result;
  3466. unsigned char lat;
  3467. struct bttv *btv;
  3468. struct v4l2_ctrl_handler *hdl;
  3469. if (bttv_num == BTTV_MAX)
  3470. return -ENOMEM;
  3471. pr_info("Bt8xx card found (%d)\n", bttv_num);
  3472. bttvs[bttv_num] = btv = kzalloc(sizeof(*btv), GFP_KERNEL);
  3473. if (btv == NULL) {
  3474. pr_err("out of memory\n");
  3475. return -ENOMEM;
  3476. }
  3477. btv->c.nr = bttv_num;
  3478. snprintf(btv->c.v4l2_dev.name, sizeof(btv->c.v4l2_dev.name),
  3479. "bttv%d", btv->c.nr);
  3480. /* initialize structs / fill in defaults */
  3481. mutex_init(&btv->lock);
  3482. spin_lock_init(&btv->s_lock);
  3483. spin_lock_init(&btv->gpio_lock);
  3484. init_waitqueue_head(&btv->i2c_queue);
  3485. INIT_LIST_HEAD(&btv->c.subs);
  3486. INIT_LIST_HEAD(&btv->capture);
  3487. INIT_LIST_HEAD(&btv->vcapture);
  3488. init_timer(&btv->timeout);
  3489. btv->timeout.function = bttv_irq_timeout;
  3490. btv->timeout.data = (unsigned long)btv;
  3491. btv->i2c_rc = -1;
  3492. btv->tuner_type = UNSET;
  3493. btv->new_input = UNSET;
  3494. btv->has_radio=radio[btv->c.nr];
  3495. /* pci stuff (init, get irq/mmio, ... */
  3496. btv->c.pci = dev;
  3497. btv->id = dev->device;
  3498. if (pci_enable_device(dev)) {
  3499. pr_warn("%d: Can't enable device\n", btv->c.nr);
  3500. return -EIO;
  3501. }
  3502. if (pci_set_dma_mask(dev, DMA_BIT_MASK(32))) {
  3503. pr_warn("%d: No suitable DMA available\n", btv->c.nr);
  3504. return -EIO;
  3505. }
  3506. if (!request_mem_region(pci_resource_start(dev,0),
  3507. pci_resource_len(dev,0),
  3508. btv->c.v4l2_dev.name)) {
  3509. pr_warn("%d: can't request iomem (0x%llx)\n",
  3510. btv->c.nr,
  3511. (unsigned long long)pci_resource_start(dev, 0));
  3512. return -EBUSY;
  3513. }
  3514. pci_set_master(dev);
  3515. pci_set_command(dev);
  3516. result = v4l2_device_register(&dev->dev, &btv->c.v4l2_dev);
  3517. if (result < 0) {
  3518. pr_warn("%d: v4l2_device_register() failed\n", btv->c.nr);
  3519. goto fail0;
  3520. }
  3521. hdl = &btv->ctrl_handler;
  3522. v4l2_ctrl_handler_init(hdl, 20);
  3523. btv->c.v4l2_dev.ctrl_handler = hdl;
  3524. v4l2_ctrl_handler_init(&btv->radio_ctrl_handler, 6);
  3525. btv->revision = dev->revision;
  3526. pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
  3527. pr_info("%d: Bt%d (rev %d) at %s, irq: %d, latency: %d, mmio: 0x%llx\n",
  3528. bttv_num, btv->id, btv->revision, pci_name(dev),
  3529. btv->c.pci->irq, lat,
  3530. (unsigned long long)pci_resource_start(dev, 0));
  3531. schedule();
  3532. btv->bt848_mmio = ioremap(pci_resource_start(dev, 0), 0x1000);
  3533. if (NULL == btv->bt848_mmio) {
  3534. pr_err("%d: ioremap() failed\n", btv->c.nr);
  3535. result = -EIO;
  3536. goto fail1;
  3537. }
  3538. /* identify card */
  3539. bttv_idcard(btv);
  3540. /* disable irqs, register irq handler */
  3541. btwrite(0, BT848_INT_MASK);
  3542. result = request_irq(btv->c.pci->irq, bttv_irq,
  3543. IRQF_SHARED, btv->c.v4l2_dev.name, (void *)btv);
  3544. if (result < 0) {
  3545. pr_err("%d: can't get IRQ %d\n",
  3546. bttv_num, btv->c.pci->irq);
  3547. goto fail1;
  3548. }
  3549. if (0 != bttv_handle_chipset(btv)) {
  3550. result = -EIO;
  3551. goto fail2;
  3552. }
  3553. /* init options from insmod args */
  3554. btv->opt_combfilter = combfilter;
  3555. bttv_ctrl_combfilter.def = combfilter;
  3556. bttv_ctrl_lumafilter.def = lumafilter;
  3557. btv->opt_automute = automute;
  3558. bttv_ctrl_automute.def = automute;
  3559. bttv_ctrl_agc_crush.def = agc_crush;
  3560. btv->opt_vcr_hack = vcr_hack;
  3561. bttv_ctrl_vcr_hack.def = vcr_hack;
  3562. bttv_ctrl_whitecrush_upper.def = whitecrush_upper;
  3563. bttv_ctrl_whitecrush_lower.def = whitecrush_lower;
  3564. btv->opt_uv_ratio = uv_ratio;
  3565. bttv_ctrl_uv_ratio.def = uv_ratio;
  3566. bttv_ctrl_full_luma.def = full_luma_range;
  3567. bttv_ctrl_coring.def = coring;
  3568. /* fill struct bttv with some useful defaults */
  3569. btv->init.btv = btv;
  3570. btv->init.ov.w.width = 320;
  3571. btv->init.ov.w.height = 240;
  3572. btv->init.fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
  3573. btv->init.width = 320;
  3574. btv->init.height = 240;
  3575. btv->init.ov.w.width = 320;
  3576. btv->init.ov.w.height = 240;
  3577. btv->init.ov.field = V4L2_FIELD_INTERLACED;
  3578. btv->input = 0;
  3579. v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
  3580. V4L2_CID_BRIGHTNESS, 0, 0xff00, 0x100, 32768);
  3581. v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
  3582. V4L2_CID_CONTRAST, 0, 0xff80, 0x80, 0x6c00);
  3583. v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
  3584. V4L2_CID_SATURATION, 0, 0xff80, 0x80, 32768);
  3585. v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
  3586. V4L2_CID_COLOR_KILLER, 0, 1, 1, 0);
  3587. v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
  3588. V4L2_CID_HUE, 0, 0xff00, 0x100, 32768);
  3589. v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
  3590. V4L2_CID_CHROMA_AGC, 0, 1, 1, !!chroma_agc);
  3591. v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
  3592. V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
  3593. if (btv->volume_gpio)
  3594. v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
  3595. V4L2_CID_AUDIO_VOLUME, 0, 0xff00, 0x100, 0xff00);
  3596. v4l2_ctrl_new_custom(hdl, &bttv_ctrl_combfilter, NULL);
  3597. v4l2_ctrl_new_custom(hdl, &bttv_ctrl_automute, NULL);
  3598. v4l2_ctrl_new_custom(hdl, &bttv_ctrl_lumafilter, NULL);
  3599. v4l2_ctrl_new_custom(hdl, &bttv_ctrl_agc_crush, NULL);
  3600. v4l2_ctrl_new_custom(hdl, &bttv_ctrl_vcr_hack, NULL);
  3601. v4l2_ctrl_new_custom(hdl, &bttv_ctrl_whitecrush_lower, NULL);
  3602. v4l2_ctrl_new_custom(hdl, &bttv_ctrl_whitecrush_upper, NULL);
  3603. v4l2_ctrl_new_custom(hdl, &bttv_ctrl_uv_ratio, NULL);
  3604. v4l2_ctrl_new_custom(hdl, &bttv_ctrl_full_luma, NULL);
  3605. v4l2_ctrl_new_custom(hdl, &bttv_ctrl_coring, NULL);
  3606. /* initialize hardware */
  3607. if (bttv_gpio)
  3608. bttv_gpio_tracking(btv,"pre-init");
  3609. bttv_risc_init_main(btv);
  3610. init_bt848(btv);
  3611. /* gpio */
  3612. btwrite(0x00, BT848_GPIO_REG_INP);
  3613. btwrite(0x00, BT848_GPIO_OUT_EN);
  3614. if (bttv_verbose)
  3615. bttv_gpio_tracking(btv,"init");
  3616. /* needs to be done before i2c is registered */
  3617. bttv_init_card1(btv);
  3618. /* register i2c + gpio */
  3619. init_bttv_i2c(btv);
  3620. /* some card-specific stuff (needs working i2c) */
  3621. bttv_init_card2(btv);
  3622. bttv_init_tuner(btv);
  3623. if (btv->tuner_type != TUNER_ABSENT) {
  3624. bttv_set_frequency(btv, &init_freq);
  3625. btv->radio_freq = 90500 * 16; /* 90.5Mhz default */
  3626. }
  3627. btv->std = V4L2_STD_PAL;
  3628. init_irqreg(btv);
  3629. if (!bttv_tvcards[btv->c.type].no_video)
  3630. v4l2_ctrl_handler_setup(hdl);
  3631. if (hdl->error) {
  3632. result = hdl->error;
  3633. goto fail2;
  3634. }
  3635. /* mute device */
  3636. audio_mute(btv, 1);
  3637. /* register video4linux + input */
  3638. if (!bttv_tvcards[btv->c.type].no_video) {
  3639. v4l2_ctrl_add_handler(&btv->radio_ctrl_handler, hdl,
  3640. v4l2_ctrl_radio_filter);
  3641. if (btv->radio_ctrl_handler.error) {
  3642. result = btv->radio_ctrl_handler.error;
  3643. goto fail2;
  3644. }
  3645. set_input(btv, 0, btv->tvnorm);
  3646. bttv_crop_reset(&btv->crop[0], btv->tvnorm);
  3647. btv->crop[1] = btv->crop[0]; /* current = default */
  3648. disclaim_vbi_lines(btv);
  3649. disclaim_video_lines(btv);
  3650. bttv_register_video(btv);
  3651. }
  3652. /* add subdevices and autoload dvb-bt8xx if needed */
  3653. if (bttv_tvcards[btv->c.type].has_dvb) {
  3654. bttv_sub_add_device(&btv->c, "dvb");
  3655. request_modules(btv);
  3656. }
  3657. if (!disable_ir) {
  3658. init_bttv_i2c_ir(btv);
  3659. bttv_input_init(btv);
  3660. }
  3661. /* everything is fine */
  3662. bttv_num++;
  3663. return 0;
  3664. fail2:
  3665. free_irq(btv->c.pci->irq,btv);
  3666. fail1:
  3667. v4l2_ctrl_handler_free(&btv->ctrl_handler);
  3668. v4l2_ctrl_handler_free(&btv->radio_ctrl_handler);
  3669. v4l2_device_unregister(&btv->c.v4l2_dev);
  3670. fail0:
  3671. if (btv->bt848_mmio)
  3672. iounmap(btv->bt848_mmio);
  3673. release_mem_region(pci_resource_start(btv->c.pci,0),
  3674. pci_resource_len(btv->c.pci,0));
  3675. pci_disable_device(btv->c.pci);
  3676. return result;
  3677. }
  3678. static void bttv_remove(struct pci_dev *pci_dev)
  3679. {
  3680. struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
  3681. struct bttv *btv = to_bttv(v4l2_dev);
  3682. if (bttv_verbose)
  3683. pr_info("%d: unloading\n", btv->c.nr);
  3684. if (bttv_tvcards[btv->c.type].has_dvb)
  3685. flush_request_modules(btv);
  3686. /* shutdown everything (DMA+IRQs) */
  3687. btand(~15, BT848_GPIO_DMA_CTL);
  3688. btwrite(0, BT848_INT_MASK);
  3689. btwrite(~0x0, BT848_INT_STAT);
  3690. btwrite(0x0, BT848_GPIO_OUT_EN);
  3691. if (bttv_gpio)
  3692. bttv_gpio_tracking(btv,"cleanup");
  3693. /* tell gpio modules we are leaving ... */
  3694. btv->shutdown=1;
  3695. bttv_input_fini(btv);
  3696. bttv_sub_del_devices(&btv->c);
  3697. /* unregister i2c_bus + input */
  3698. fini_bttv_i2c(btv);
  3699. /* unregister video4linux */
  3700. bttv_unregister_video(btv);
  3701. /* free allocated memory */
  3702. v4l2_ctrl_handler_free(&btv->ctrl_handler);
  3703. v4l2_ctrl_handler_free(&btv->radio_ctrl_handler);
  3704. btcx_riscmem_free(btv->c.pci,&btv->main);
  3705. /* free resources */
  3706. free_irq(btv->c.pci->irq,btv);
  3707. iounmap(btv->bt848_mmio);
  3708. release_mem_region(pci_resource_start(btv->c.pci,0),
  3709. pci_resource_len(btv->c.pci,0));
  3710. pci_disable_device(btv->c.pci);
  3711. v4l2_device_unregister(&btv->c.v4l2_dev);
  3712. bttvs[btv->c.nr] = NULL;
  3713. kfree(btv);
  3714. return;
  3715. }
  3716. #ifdef CONFIG_PM
  3717. static int bttv_suspend(struct pci_dev *pci_dev, pm_message_t state)
  3718. {
  3719. struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
  3720. struct bttv *btv = to_bttv(v4l2_dev);
  3721. struct bttv_buffer_set idle;
  3722. unsigned long flags;
  3723. dprintk("%d: suspend %d\n", btv->c.nr, state.event);
  3724. /* stop dma + irqs */
  3725. spin_lock_irqsave(&btv->s_lock,flags);
  3726. memset(&idle, 0, sizeof(idle));
  3727. btv->state.video = btv->curr;
  3728. btv->state.vbi = btv->cvbi;
  3729. btv->state.loop_irq = btv->loop_irq;
  3730. btv->curr = idle;
  3731. btv->loop_irq = 0;
  3732. bttv_buffer_activate_video(btv, &idle);
  3733. bttv_buffer_activate_vbi(btv, NULL);
  3734. bttv_set_dma(btv, 0);
  3735. btwrite(0, BT848_INT_MASK);
  3736. spin_unlock_irqrestore(&btv->s_lock,flags);
  3737. /* save bt878 state */
  3738. btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN);
  3739. btv->state.gpio_data = gpio_read();
  3740. /* save pci state */
  3741. pci_save_state(pci_dev);
  3742. if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
  3743. pci_disable_device(pci_dev);
  3744. btv->state.disabled = 1;
  3745. }
  3746. return 0;
  3747. }
  3748. static int bttv_resume(struct pci_dev *pci_dev)
  3749. {
  3750. struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
  3751. struct bttv *btv = to_bttv(v4l2_dev);
  3752. unsigned long flags;
  3753. int err;
  3754. dprintk("%d: resume\n", btv->c.nr);
  3755. /* restore pci state */
  3756. if (btv->state.disabled) {
  3757. err=pci_enable_device(pci_dev);
  3758. if (err) {
  3759. pr_warn("%d: Can't enable device\n", btv->c.nr);
  3760. return err;
  3761. }
  3762. btv->state.disabled = 0;
  3763. }
  3764. err=pci_set_power_state(pci_dev, PCI_D0);
  3765. if (err) {
  3766. pci_disable_device(pci_dev);
  3767. pr_warn("%d: Can't enable device\n", btv->c.nr);
  3768. btv->state.disabled = 1;
  3769. return err;
  3770. }
  3771. pci_restore_state(pci_dev);
  3772. /* restore bt878 state */
  3773. bttv_reinit_bt848(btv);
  3774. gpio_inout(0xffffff, btv->state.gpio_enable);
  3775. gpio_write(btv->state.gpio_data);
  3776. /* restart dma */
  3777. spin_lock_irqsave(&btv->s_lock,flags);
  3778. btv->curr = btv->state.video;
  3779. btv->cvbi = btv->state.vbi;
  3780. btv->loop_irq = btv->state.loop_irq;
  3781. bttv_buffer_activate_video(btv, &btv->curr);
  3782. bttv_buffer_activate_vbi(btv, btv->cvbi);
  3783. bttv_set_dma(btv, 0);
  3784. spin_unlock_irqrestore(&btv->s_lock,flags);
  3785. return 0;
  3786. }
  3787. #endif
  3788. static struct pci_device_id bttv_pci_tbl[] = {
  3789. {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT848), 0},
  3790. {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT849), 0},
  3791. {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT878), 0},
  3792. {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT879), 0},
  3793. {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_FUSION879), 0},
  3794. {0,}
  3795. };
  3796. MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
  3797. static struct pci_driver bttv_pci_driver = {
  3798. .name = "bttv",
  3799. .id_table = bttv_pci_tbl,
  3800. .probe = bttv_probe,
  3801. .remove = bttv_remove,
  3802. #ifdef CONFIG_PM
  3803. .suspend = bttv_suspend,
  3804. .resume = bttv_resume,
  3805. #endif
  3806. };
  3807. static int __init bttv_init_module(void)
  3808. {
  3809. int ret;
  3810. bttv_num = 0;
  3811. pr_info("driver version %s loaded\n", BTTV_VERSION);
  3812. if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
  3813. gbuffers = 2;
  3814. if (gbufsize > BTTV_MAX_FBUF)
  3815. gbufsize = BTTV_MAX_FBUF;
  3816. gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
  3817. if (bttv_verbose)
  3818. pr_info("using %d buffers with %dk (%d pages) each for capture\n",
  3819. gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
  3820. bttv_check_chipset();
  3821. ret = bus_register(&bttv_sub_bus_type);
  3822. if (ret < 0) {
  3823. pr_warn("bus_register error: %d\n", ret);
  3824. return ret;
  3825. }
  3826. ret = pci_register_driver(&bttv_pci_driver);
  3827. if (ret < 0)
  3828. bus_unregister(&bttv_sub_bus_type);
  3829. return ret;
  3830. }
  3831. static void __exit bttv_cleanup_module(void)
  3832. {
  3833. pci_unregister_driver(&bttv_pci_driver);
  3834. bus_unregister(&bttv_sub_bus_type);
  3835. }
  3836. module_init(bttv_init_module);
  3837. module_exit(bttv_cleanup_module);