bttv-driver.c 107 KB

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