bttv-driver.c 107 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236
  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. bttv_printk(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. bttv_printk(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. bttv_printk(".");
  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. bttv_printk(" ok\n");
  730. return;
  731. }
  732. }
  733. btv->pll.pll_current = -1;
  734. bttv_printk("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_BOARD_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_BOARD_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. case VIDIOC_LOG_STATUS:
  1671. {
  1672. bttv_call_i2c_clients(btv, VIDIOC_LOG_STATUS, 0);
  1673. return 0;
  1674. }
  1675. default:
  1676. return -ENOIOCTLCMD;
  1677. }
  1678. return 0;
  1679. }
  1680. static int verify_window(const struct bttv_tvnorm *tvn,
  1681. struct v4l2_window *win, int fixup)
  1682. {
  1683. enum v4l2_field field;
  1684. int maxw, maxh;
  1685. if (win->w.width < 48 || win->w.height < 32)
  1686. return -EINVAL;
  1687. if (win->clipcount > 2048)
  1688. return -EINVAL;
  1689. field = win->field;
  1690. maxw = tvn->swidth;
  1691. maxh = tvn->sheight;
  1692. if (V4L2_FIELD_ANY == field) {
  1693. field = (win->w.height > maxh/2)
  1694. ? V4L2_FIELD_INTERLACED
  1695. : V4L2_FIELD_TOP;
  1696. }
  1697. switch (field) {
  1698. case V4L2_FIELD_TOP:
  1699. case V4L2_FIELD_BOTTOM:
  1700. maxh = maxh / 2;
  1701. break;
  1702. case V4L2_FIELD_INTERLACED:
  1703. break;
  1704. default:
  1705. return -EINVAL;
  1706. }
  1707. if (!fixup && (win->w.width > maxw || win->w.height > maxh))
  1708. return -EINVAL;
  1709. if (win->w.width > maxw)
  1710. win->w.width = maxw;
  1711. if (win->w.height > maxh)
  1712. win->w.height = maxh;
  1713. win->field = field;
  1714. return 0;
  1715. }
  1716. static int setup_window(struct bttv_fh *fh, struct bttv *btv,
  1717. struct v4l2_window *win, int fixup)
  1718. {
  1719. struct v4l2_clip *clips = NULL;
  1720. int n,size,retval = 0;
  1721. if (NULL == fh->ovfmt)
  1722. return -EINVAL;
  1723. if (!(fh->ovfmt->flags & FORMAT_FLAGS_PACKED))
  1724. return -EINVAL;
  1725. retval = verify_window(&bttv_tvnorms[btv->tvnorm],win,fixup);
  1726. if (0 != retval)
  1727. return retval;
  1728. /* copy clips -- luckily v4l1 + v4l2 are binary
  1729. compatible here ...*/
  1730. n = win->clipcount;
  1731. size = sizeof(*clips)*(n+4);
  1732. clips = kmalloc(size,GFP_KERNEL);
  1733. if (NULL == clips)
  1734. return -ENOMEM;
  1735. if (n > 0) {
  1736. if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
  1737. kfree(clips);
  1738. return -EFAULT;
  1739. }
  1740. }
  1741. /* clip against screen */
  1742. if (NULL != btv->fbuf.base)
  1743. n = btcx_screen_clips(btv->fbuf.fmt.width, btv->fbuf.fmt.height,
  1744. &win->w, clips, n);
  1745. btcx_sort_clips(clips,n);
  1746. /* 4-byte alignments */
  1747. switch (fh->ovfmt->depth) {
  1748. case 8:
  1749. case 24:
  1750. btcx_align(&win->w, clips, n, 3);
  1751. break;
  1752. case 16:
  1753. btcx_align(&win->w, clips, n, 1);
  1754. break;
  1755. case 32:
  1756. /* no alignment fixups needed */
  1757. break;
  1758. default:
  1759. BUG();
  1760. }
  1761. down(&fh->cap.lock);
  1762. kfree(fh->ov.clips);
  1763. fh->ov.clips = clips;
  1764. fh->ov.nclips = n;
  1765. fh->ov.w = win->w;
  1766. fh->ov.field = win->field;
  1767. fh->ov.setup_ok = 1;
  1768. btv->init.ov.w.width = win->w.width;
  1769. btv->init.ov.w.height = win->w.height;
  1770. btv->init.ov.field = win->field;
  1771. /* update overlay if needed */
  1772. retval = 0;
  1773. if (check_btres(fh, RESOURCE_OVERLAY)) {
  1774. struct bttv_buffer *new;
  1775. new = videobuf_alloc(sizeof(*new));
  1776. bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
  1777. retval = bttv_switch_overlay(btv,fh,new);
  1778. }
  1779. up(&fh->cap.lock);
  1780. return retval;
  1781. }
  1782. /* ----------------------------------------------------------------------- */
  1783. static struct videobuf_queue* bttv_queue(struct bttv_fh *fh)
  1784. {
  1785. struct videobuf_queue* q = NULL;
  1786. switch (fh->type) {
  1787. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  1788. q = &fh->cap;
  1789. break;
  1790. case V4L2_BUF_TYPE_VBI_CAPTURE:
  1791. q = &fh->vbi;
  1792. break;
  1793. default:
  1794. BUG();
  1795. }
  1796. return q;
  1797. }
  1798. static int bttv_resource(struct bttv_fh *fh)
  1799. {
  1800. int res = 0;
  1801. switch (fh->type) {
  1802. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  1803. res = RESOURCE_VIDEO;
  1804. break;
  1805. case V4L2_BUF_TYPE_VBI_CAPTURE:
  1806. res = RESOURCE_VBI;
  1807. break;
  1808. default:
  1809. BUG();
  1810. }
  1811. return res;
  1812. }
  1813. static int bttv_switch_type(struct bttv_fh *fh, enum v4l2_buf_type type)
  1814. {
  1815. struct videobuf_queue *q = bttv_queue(fh);
  1816. int res = bttv_resource(fh);
  1817. if (check_btres(fh,res))
  1818. return -EBUSY;
  1819. if (videobuf_queue_is_busy(q))
  1820. return -EBUSY;
  1821. fh->type = type;
  1822. return 0;
  1823. }
  1824. static int bttv_g_fmt(struct bttv_fh *fh, struct v4l2_format *f)
  1825. {
  1826. switch (f->type) {
  1827. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  1828. memset(&f->fmt.pix,0,sizeof(struct v4l2_pix_format));
  1829. f->fmt.pix.width = fh->width;
  1830. f->fmt.pix.height = fh->height;
  1831. f->fmt.pix.field = fh->cap.field;
  1832. f->fmt.pix.pixelformat = fh->fmt->fourcc;
  1833. f->fmt.pix.bytesperline =
  1834. (f->fmt.pix.width * fh->fmt->depth) >> 3;
  1835. f->fmt.pix.sizeimage =
  1836. f->fmt.pix.height * f->fmt.pix.bytesperline;
  1837. return 0;
  1838. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  1839. memset(&f->fmt.win,0,sizeof(struct v4l2_window));
  1840. f->fmt.win.w = fh->ov.w;
  1841. f->fmt.win.field = fh->ov.field;
  1842. return 0;
  1843. case V4L2_BUF_TYPE_VBI_CAPTURE:
  1844. bttv_vbi_get_fmt(fh,f);
  1845. return 0;
  1846. default:
  1847. return -EINVAL;
  1848. }
  1849. }
  1850. static int bttv_try_fmt(struct bttv_fh *fh, struct bttv *btv,
  1851. struct v4l2_format *f)
  1852. {
  1853. switch (f->type) {
  1854. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  1855. {
  1856. const struct bttv_format *fmt;
  1857. enum v4l2_field field;
  1858. unsigned int maxw,maxh;
  1859. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  1860. if (NULL == fmt)
  1861. return -EINVAL;
  1862. /* fixup format */
  1863. maxw = bttv_tvnorms[btv->tvnorm].swidth;
  1864. maxh = bttv_tvnorms[btv->tvnorm].sheight;
  1865. field = f->fmt.pix.field;
  1866. if (V4L2_FIELD_ANY == field)
  1867. field = (f->fmt.pix.height > maxh/2)
  1868. ? V4L2_FIELD_INTERLACED
  1869. : V4L2_FIELD_BOTTOM;
  1870. if (V4L2_FIELD_SEQ_BT == field)
  1871. field = V4L2_FIELD_SEQ_TB;
  1872. switch (field) {
  1873. case V4L2_FIELD_TOP:
  1874. case V4L2_FIELD_BOTTOM:
  1875. case V4L2_FIELD_ALTERNATE:
  1876. maxh = maxh/2;
  1877. break;
  1878. case V4L2_FIELD_INTERLACED:
  1879. break;
  1880. case V4L2_FIELD_SEQ_TB:
  1881. if (fmt->flags & FORMAT_FLAGS_PLANAR)
  1882. return -EINVAL;
  1883. break;
  1884. default:
  1885. return -EINVAL;
  1886. }
  1887. /* update data for the application */
  1888. f->fmt.pix.field = field;
  1889. if (f->fmt.pix.width < 48)
  1890. f->fmt.pix.width = 48;
  1891. if (f->fmt.pix.height < 32)
  1892. f->fmt.pix.height = 32;
  1893. if (f->fmt.pix.width > maxw)
  1894. f->fmt.pix.width = maxw;
  1895. if (f->fmt.pix.height > maxh)
  1896. f->fmt.pix.height = maxh;
  1897. f->fmt.pix.width &= ~0x03;
  1898. f->fmt.pix.bytesperline =
  1899. (f->fmt.pix.width * fmt->depth) >> 3;
  1900. f->fmt.pix.sizeimage =
  1901. f->fmt.pix.height * f->fmt.pix.bytesperline;
  1902. return 0;
  1903. }
  1904. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  1905. return verify_window(&bttv_tvnorms[btv->tvnorm],
  1906. &f->fmt.win, 1);
  1907. case V4L2_BUF_TYPE_VBI_CAPTURE:
  1908. bttv_vbi_try_fmt(fh,f);
  1909. return 0;
  1910. default:
  1911. return -EINVAL;
  1912. }
  1913. }
  1914. static int bttv_s_fmt(struct bttv_fh *fh, struct bttv *btv,
  1915. struct v4l2_format *f)
  1916. {
  1917. int retval;
  1918. switch (f->type) {
  1919. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  1920. {
  1921. const struct bttv_format *fmt;
  1922. retval = bttv_switch_type(fh,f->type);
  1923. if (0 != retval)
  1924. return retval;
  1925. retval = bttv_try_fmt(fh,btv,f);
  1926. if (0 != retval)
  1927. return retval;
  1928. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  1929. /* update our state informations */
  1930. down(&fh->cap.lock);
  1931. fh->fmt = fmt;
  1932. fh->cap.field = f->fmt.pix.field;
  1933. fh->cap.last = V4L2_FIELD_NONE;
  1934. fh->width = f->fmt.pix.width;
  1935. fh->height = f->fmt.pix.height;
  1936. btv->init.fmt = fmt;
  1937. btv->init.width = f->fmt.pix.width;
  1938. btv->init.height = f->fmt.pix.height;
  1939. up(&fh->cap.lock);
  1940. return 0;
  1941. }
  1942. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  1943. if (no_overlay > 0) {
  1944. printk ("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
  1945. return -EINVAL;
  1946. }
  1947. return setup_window(fh, btv, &f->fmt.win, 1);
  1948. case V4L2_BUF_TYPE_VBI_CAPTURE:
  1949. retval = bttv_switch_type(fh,f->type);
  1950. if (0 != retval)
  1951. return retval;
  1952. if (locked_btres(fh->btv, RESOURCE_VBI))
  1953. return -EBUSY;
  1954. bttv_vbi_try_fmt(fh,f);
  1955. bttv_vbi_setlines(fh,btv,f->fmt.vbi.count[0]);
  1956. bttv_vbi_get_fmt(fh,f);
  1957. return 0;
  1958. default:
  1959. return -EINVAL;
  1960. }
  1961. }
  1962. static int bttv_do_ioctl(struct inode *inode, struct file *file,
  1963. unsigned int cmd, void *arg)
  1964. {
  1965. struct bttv_fh *fh = file->private_data;
  1966. struct bttv *btv = fh->btv;
  1967. unsigned long flags;
  1968. int retval = 0;
  1969. if (bttv_debug > 1) {
  1970. switch (_IOC_TYPE(cmd)) {
  1971. case 'v':
  1972. printk("bttv%d: ioctl 0x%x (v4l1, VIDIOC%s)\n",
  1973. btv->c.nr, cmd, (_IOC_NR(cmd) < V4L1_IOCTLS) ?
  1974. v4l1_ioctls[_IOC_NR(cmd)] : "???");
  1975. break;
  1976. case 'V':
  1977. printk("bttv%d: ioctl 0x%x (v4l2, %s)\n",
  1978. btv->c.nr, cmd, v4l2_ioctl_names[_IOC_NR(cmd)]);
  1979. break;
  1980. default:
  1981. printk("bttv%d: ioctl 0x%x (???)\n",
  1982. btv->c.nr, cmd);
  1983. }
  1984. }
  1985. if (btv->errors)
  1986. bttv_reinit_bt848(btv);
  1987. switch (cmd) {
  1988. case VIDIOCSFREQ:
  1989. case VIDIOCSTUNER:
  1990. case VIDIOCSCHAN:
  1991. case VIDIOC_S_CTRL:
  1992. case VIDIOC_S_STD:
  1993. case VIDIOC_S_INPUT:
  1994. case VIDIOC_S_TUNER:
  1995. case VIDIOC_S_FREQUENCY:
  1996. retval = v4l2_prio_check(&btv->prio,&fh->prio);
  1997. if (0 != retval)
  1998. return retval;
  1999. };
  2000. switch (cmd) {
  2001. /* *** v4l1 *** ************************************************ */
  2002. case VIDIOCGCAP:
  2003. {
  2004. struct video_capability *cap = arg;
  2005. memset(cap,0,sizeof(*cap));
  2006. strcpy(cap->name,btv->video_dev->name);
  2007. if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
  2008. /* vbi */
  2009. cap->type = VID_TYPE_TUNER|VID_TYPE_TELETEXT;
  2010. } else {
  2011. /* others */
  2012. cap->type = VID_TYPE_CAPTURE|
  2013. VID_TYPE_TUNER|
  2014. VID_TYPE_CLIPPING|
  2015. VID_TYPE_SCALES;
  2016. if (no_overlay <= 0)
  2017. cap->type |= VID_TYPE_OVERLAY;
  2018. cap->maxwidth = bttv_tvnorms[btv->tvnorm].swidth;
  2019. cap->maxheight = bttv_tvnorms[btv->tvnorm].sheight;
  2020. cap->minwidth = 48;
  2021. cap->minheight = 32;
  2022. }
  2023. cap->channels = bttv_tvcards[btv->c.type].video_inputs;
  2024. cap->audios = bttv_tvcards[btv->c.type].audio_inputs;
  2025. return 0;
  2026. }
  2027. case VIDIOCGPICT:
  2028. {
  2029. struct video_picture *pic = arg;
  2030. memset(pic,0,sizeof(*pic));
  2031. pic->brightness = btv->bright;
  2032. pic->contrast = btv->contrast;
  2033. pic->hue = btv->hue;
  2034. pic->colour = btv->saturation;
  2035. if (fh->fmt) {
  2036. pic->depth = fh->fmt->depth;
  2037. pic->palette = fh->fmt->palette;
  2038. }
  2039. return 0;
  2040. }
  2041. case VIDIOCSPICT:
  2042. {
  2043. struct video_picture *pic = arg;
  2044. const struct bttv_format *fmt;
  2045. fmt = format_by_palette(pic->palette);
  2046. if (NULL == fmt)
  2047. return -EINVAL;
  2048. down(&fh->cap.lock);
  2049. if (fmt->depth != pic->depth) {
  2050. retval = -EINVAL;
  2051. goto fh_unlock_and_return;
  2052. }
  2053. fh->ovfmt = fmt;
  2054. fh->fmt = fmt;
  2055. btv->init.ovfmt = fmt;
  2056. btv->init.fmt = fmt;
  2057. if (bigendian) {
  2058. /* dirty hack time: swap bytes for overlay if the
  2059. display adaptor is big endian (insmod option) */
  2060. if (fmt->palette == VIDEO_PALETTE_RGB555 ||
  2061. fmt->palette == VIDEO_PALETTE_RGB565 ||
  2062. fmt->palette == VIDEO_PALETTE_RGB32) {
  2063. fh->ovfmt = fmt+1;
  2064. }
  2065. }
  2066. bt848_bright(btv,pic->brightness);
  2067. bt848_contrast(btv,pic->contrast);
  2068. bt848_hue(btv,pic->hue);
  2069. bt848_sat(btv,pic->colour);
  2070. up(&fh->cap.lock);
  2071. return 0;
  2072. }
  2073. case VIDIOCGWIN:
  2074. {
  2075. struct video_window *win = arg;
  2076. memset(win,0,sizeof(*win));
  2077. win->x = fh->ov.w.left;
  2078. win->y = fh->ov.w.top;
  2079. win->width = fh->ov.w.width;
  2080. win->height = fh->ov.w.height;
  2081. return 0;
  2082. }
  2083. case VIDIOCSWIN:
  2084. {
  2085. struct video_window *win = arg;
  2086. struct v4l2_window w2;
  2087. if (no_overlay > 0) {
  2088. printk ("VIDIOCSWIN: no_overlay\n");
  2089. return -EINVAL;
  2090. }
  2091. w2.field = V4L2_FIELD_ANY;
  2092. w2.w.left = win->x;
  2093. w2.w.top = win->y;
  2094. w2.w.width = win->width;
  2095. w2.w.height = win->height;
  2096. w2.clipcount = win->clipcount;
  2097. w2.clips = (struct v4l2_clip __user *)win->clips;
  2098. retval = setup_window(fh, btv, &w2, 0);
  2099. if (0 == retval) {
  2100. /* on v4l1 this ioctl affects the read() size too */
  2101. fh->width = fh->ov.w.width;
  2102. fh->height = fh->ov.w.height;
  2103. btv->init.width = fh->ov.w.width;
  2104. btv->init.height = fh->ov.w.height;
  2105. }
  2106. return retval;
  2107. }
  2108. case VIDIOCGFBUF:
  2109. {
  2110. struct video_buffer *fbuf = arg;
  2111. fbuf->base = btv->fbuf.base;
  2112. fbuf->width = btv->fbuf.fmt.width;
  2113. fbuf->height = btv->fbuf.fmt.height;
  2114. fbuf->bytesperline = btv->fbuf.fmt.bytesperline;
  2115. if (fh->ovfmt)
  2116. fbuf->depth = fh->ovfmt->depth;
  2117. return 0;
  2118. }
  2119. case VIDIOCSFBUF:
  2120. {
  2121. struct video_buffer *fbuf = arg;
  2122. const struct bttv_format *fmt;
  2123. unsigned long end;
  2124. if(!capable(CAP_SYS_ADMIN) &&
  2125. !capable(CAP_SYS_RAWIO))
  2126. return -EPERM;
  2127. end = (unsigned long)fbuf->base +
  2128. fbuf->height * fbuf->bytesperline;
  2129. down(&fh->cap.lock);
  2130. retval = -EINVAL;
  2131. switch (fbuf->depth) {
  2132. case 8:
  2133. fmt = format_by_palette(VIDEO_PALETTE_HI240);
  2134. break;
  2135. case 16:
  2136. fmt = format_by_palette(VIDEO_PALETTE_RGB565);
  2137. break;
  2138. case 24:
  2139. fmt = format_by_palette(VIDEO_PALETTE_RGB24);
  2140. break;
  2141. case 32:
  2142. fmt = format_by_palette(VIDEO_PALETTE_RGB32);
  2143. break;
  2144. case 15:
  2145. fbuf->depth = 16;
  2146. fmt = format_by_palette(VIDEO_PALETTE_RGB555);
  2147. break;
  2148. default:
  2149. fmt = NULL;
  2150. break;
  2151. }
  2152. if (NULL == fmt)
  2153. goto fh_unlock_and_return;
  2154. fh->ovfmt = fmt;
  2155. fh->fmt = fmt;
  2156. btv->init.ovfmt = fmt;
  2157. btv->init.fmt = fmt;
  2158. btv->fbuf.base = fbuf->base;
  2159. btv->fbuf.fmt.width = fbuf->width;
  2160. btv->fbuf.fmt.height = fbuf->height;
  2161. if (fbuf->bytesperline)
  2162. btv->fbuf.fmt.bytesperline = fbuf->bytesperline;
  2163. else
  2164. btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fbuf->depth/8;
  2165. up(&fh->cap.lock);
  2166. return 0;
  2167. }
  2168. case VIDIOCCAPTURE:
  2169. case VIDIOC_OVERLAY:
  2170. {
  2171. struct bttv_buffer *new;
  2172. int *on = arg;
  2173. if (*on) {
  2174. /* verify args */
  2175. if (NULL == btv->fbuf.base)
  2176. return -EINVAL;
  2177. if (!fh->ov.setup_ok) {
  2178. dprintk("bttv%d: overlay: !setup_ok\n",btv->c.nr);
  2179. return -EINVAL;
  2180. }
  2181. }
  2182. if (!check_alloc_btres(btv,fh,RESOURCE_OVERLAY))
  2183. return -EBUSY;
  2184. down(&fh->cap.lock);
  2185. if (*on) {
  2186. fh->ov.tvnorm = btv->tvnorm;
  2187. new = videobuf_alloc(sizeof(*new));
  2188. bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
  2189. } else {
  2190. new = NULL;
  2191. }
  2192. /* switch over */
  2193. retval = bttv_switch_overlay(btv,fh,new);
  2194. up(&fh->cap.lock);
  2195. return retval;
  2196. }
  2197. case VIDIOCGMBUF:
  2198. {
  2199. struct video_mbuf *mbuf = arg;
  2200. unsigned int i;
  2201. down(&fh->cap.lock);
  2202. retval = videobuf_mmap_setup(&fh->cap,gbuffers,gbufsize,
  2203. V4L2_MEMORY_MMAP);
  2204. if (retval < 0)
  2205. goto fh_unlock_and_return;
  2206. memset(mbuf,0,sizeof(*mbuf));
  2207. mbuf->frames = gbuffers;
  2208. mbuf->size = gbuffers * gbufsize;
  2209. for (i = 0; i < gbuffers; i++)
  2210. mbuf->offsets[i] = i * gbufsize;
  2211. up(&fh->cap.lock);
  2212. return 0;
  2213. }
  2214. case VIDIOCMCAPTURE:
  2215. {
  2216. struct video_mmap *vm = arg;
  2217. struct bttv_buffer *buf;
  2218. enum v4l2_field field;
  2219. if (vm->frame >= VIDEO_MAX_FRAME)
  2220. return -EINVAL;
  2221. down(&fh->cap.lock);
  2222. retval = -EINVAL;
  2223. buf = (struct bttv_buffer *)fh->cap.bufs[vm->frame];
  2224. if (NULL == buf)
  2225. goto fh_unlock_and_return;
  2226. if (0 == buf->vb.baddr)
  2227. goto fh_unlock_and_return;
  2228. if (buf->vb.state == STATE_QUEUED ||
  2229. buf->vb.state == STATE_ACTIVE)
  2230. goto fh_unlock_and_return;
  2231. field = (vm->height > bttv_tvnorms[btv->tvnorm].sheight/2)
  2232. ? V4L2_FIELD_INTERLACED
  2233. : V4L2_FIELD_BOTTOM;
  2234. retval = bttv_prepare_buffer(btv,buf,
  2235. format_by_palette(vm->format),
  2236. vm->width,vm->height,field);
  2237. if (0 != retval)
  2238. goto fh_unlock_and_return;
  2239. spin_lock_irqsave(&btv->s_lock,flags);
  2240. buffer_queue(&fh->cap,&buf->vb);
  2241. spin_unlock_irqrestore(&btv->s_lock,flags);
  2242. up(&fh->cap.lock);
  2243. return 0;
  2244. }
  2245. case VIDIOCSYNC:
  2246. {
  2247. int *frame = arg;
  2248. struct bttv_buffer *buf;
  2249. if (*frame >= VIDEO_MAX_FRAME)
  2250. return -EINVAL;
  2251. down(&fh->cap.lock);
  2252. retval = -EINVAL;
  2253. buf = (struct bttv_buffer *)fh->cap.bufs[*frame];
  2254. if (NULL == buf)
  2255. goto fh_unlock_and_return;
  2256. retval = videobuf_waiton(&buf->vb,0,1);
  2257. if (0 != retval)
  2258. goto fh_unlock_and_return;
  2259. switch (buf->vb.state) {
  2260. case STATE_ERROR:
  2261. retval = -EIO;
  2262. /* fall through */
  2263. case STATE_DONE:
  2264. videobuf_dma_pci_sync(btv->c.pci,&buf->vb.dma);
  2265. bttv_dma_free(btv,buf);
  2266. break;
  2267. default:
  2268. retval = -EINVAL;
  2269. break;
  2270. }
  2271. up(&fh->cap.lock);
  2272. return retval;
  2273. }
  2274. case VIDIOCGVBIFMT:
  2275. {
  2276. struct vbi_format *fmt = (void *) arg;
  2277. struct v4l2_format fmt2;
  2278. if (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE) {
  2279. retval = bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
  2280. if (0 != retval)
  2281. return retval;
  2282. }
  2283. bttv_vbi_get_fmt(fh, &fmt2);
  2284. memset(fmt,0,sizeof(*fmt));
  2285. fmt->sampling_rate = fmt2.fmt.vbi.sampling_rate;
  2286. fmt->samples_per_line = fmt2.fmt.vbi.samples_per_line;
  2287. fmt->sample_format = VIDEO_PALETTE_RAW;
  2288. fmt->start[0] = fmt2.fmt.vbi.start[0];
  2289. fmt->count[0] = fmt2.fmt.vbi.count[0];
  2290. fmt->start[1] = fmt2.fmt.vbi.start[1];
  2291. fmt->count[1] = fmt2.fmt.vbi.count[1];
  2292. if (fmt2.fmt.vbi.flags & VBI_UNSYNC)
  2293. fmt->flags |= V4L2_VBI_UNSYNC;
  2294. if (fmt2.fmt.vbi.flags & VBI_INTERLACED)
  2295. fmt->flags |= V4L2_VBI_INTERLACED;
  2296. return 0;
  2297. }
  2298. case VIDIOCSVBIFMT:
  2299. {
  2300. struct vbi_format *fmt = (void *) arg;
  2301. struct v4l2_format fmt2;
  2302. retval = bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
  2303. if (0 != retval)
  2304. return retval;
  2305. bttv_vbi_get_fmt(fh, &fmt2);
  2306. if (fmt->sampling_rate != fmt2.fmt.vbi.sampling_rate ||
  2307. fmt->samples_per_line != fmt2.fmt.vbi.samples_per_line ||
  2308. fmt->sample_format != VIDEO_PALETTE_RAW ||
  2309. fmt->start[0] != fmt2.fmt.vbi.start[0] ||
  2310. fmt->start[1] != fmt2.fmt.vbi.start[1] ||
  2311. fmt->count[0] != fmt->count[1] ||
  2312. fmt->count[0] < 1 ||
  2313. fmt->count[0] > 32 /* VBI_MAXLINES */)
  2314. return -EINVAL;
  2315. bttv_vbi_setlines(fh,btv,fmt->count[0]);
  2316. return 0;
  2317. }
  2318. case BTTV_VERSION:
  2319. case VIDIOCGFREQ:
  2320. case VIDIOCSFREQ:
  2321. case VIDIOCGTUNER:
  2322. case VIDIOCSTUNER:
  2323. case VIDIOCGCHAN:
  2324. case VIDIOCSCHAN:
  2325. case VIDIOCGAUDIO:
  2326. case VIDIOCSAUDIO:
  2327. return bttv_common_ioctls(btv,cmd,arg);
  2328. /* *** v4l2 *** ************************************************ */
  2329. case VIDIOC_QUERYCAP:
  2330. {
  2331. struct v4l2_capability *cap = arg;
  2332. if (0 == v4l2)
  2333. return -EINVAL;
  2334. strcpy(cap->driver,"bttv");
  2335. strlcpy(cap->card,btv->video_dev->name,sizeof(cap->card));
  2336. sprintf(cap->bus_info,"PCI:%s",pci_name(btv->c.pci));
  2337. cap->version = BTTV_VERSION_CODE;
  2338. cap->capabilities =
  2339. V4L2_CAP_VIDEO_CAPTURE |
  2340. V4L2_CAP_VBI_CAPTURE |
  2341. V4L2_CAP_READWRITE |
  2342. V4L2_CAP_STREAMING;
  2343. if (no_overlay <= 0)
  2344. cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
  2345. if (bttv_tvcards[btv->c.type].tuner != UNSET &&
  2346. bttv_tvcards[btv->c.type].tuner != TUNER_ABSENT)
  2347. cap->capabilities |= V4L2_CAP_TUNER;
  2348. return 0;
  2349. }
  2350. case VIDIOC_ENUM_FMT:
  2351. {
  2352. struct v4l2_fmtdesc *f = arg;
  2353. enum v4l2_buf_type type;
  2354. unsigned int i;
  2355. int index;
  2356. type = f->type;
  2357. if (V4L2_BUF_TYPE_VBI_CAPTURE == type) {
  2358. /* vbi */
  2359. index = f->index;
  2360. if (0 != index)
  2361. return -EINVAL;
  2362. memset(f,0,sizeof(*f));
  2363. f->index = index;
  2364. f->type = type;
  2365. f->pixelformat = V4L2_PIX_FMT_GREY;
  2366. strcpy(f->description,"vbi data");
  2367. return 0;
  2368. }
  2369. /* video capture + overlay */
  2370. index = -1;
  2371. for (i = 0; i < BTTV_FORMATS; i++) {
  2372. if (bttv_formats[i].fourcc != -1)
  2373. index++;
  2374. if ((unsigned int)index == f->index)
  2375. break;
  2376. }
  2377. if (BTTV_FORMATS == i)
  2378. return -EINVAL;
  2379. switch (f->type) {
  2380. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  2381. break;
  2382. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2383. if (!(bttv_formats[i].flags & FORMAT_FLAGS_PACKED))
  2384. return -EINVAL;
  2385. break;
  2386. default:
  2387. return -EINVAL;
  2388. }
  2389. memset(f,0,sizeof(*f));
  2390. f->index = index;
  2391. f->type = type;
  2392. f->pixelformat = bttv_formats[i].fourcc;
  2393. strlcpy(f->description,bttv_formats[i].name,sizeof(f->description));
  2394. return 0;
  2395. }
  2396. case VIDIOC_TRY_FMT:
  2397. {
  2398. struct v4l2_format *f = arg;
  2399. return bttv_try_fmt(fh,btv,f);
  2400. }
  2401. case VIDIOC_G_FMT:
  2402. {
  2403. struct v4l2_format *f = arg;
  2404. return bttv_g_fmt(fh,f);
  2405. }
  2406. case VIDIOC_S_FMT:
  2407. {
  2408. struct v4l2_format *f = arg;
  2409. return bttv_s_fmt(fh,btv,f);
  2410. }
  2411. case VIDIOC_G_FBUF:
  2412. {
  2413. struct v4l2_framebuffer *fb = arg;
  2414. *fb = btv->fbuf;
  2415. fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
  2416. if (fh->ovfmt)
  2417. fb->fmt.pixelformat = fh->ovfmt->fourcc;
  2418. return 0;
  2419. }
  2420. case VIDIOC_S_FBUF:
  2421. {
  2422. struct v4l2_framebuffer *fb = arg;
  2423. const struct bttv_format *fmt;
  2424. if(!capable(CAP_SYS_ADMIN) &&
  2425. !capable(CAP_SYS_RAWIO))
  2426. return -EPERM;
  2427. /* check args */
  2428. fmt = format_by_fourcc(fb->fmt.pixelformat);
  2429. if (NULL == fmt)
  2430. return -EINVAL;
  2431. if (0 == (fmt->flags & FORMAT_FLAGS_PACKED))
  2432. return -EINVAL;
  2433. down(&fh->cap.lock);
  2434. retval = -EINVAL;
  2435. if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
  2436. if (fb->fmt.width > bttv_tvnorms[btv->tvnorm].swidth)
  2437. goto fh_unlock_and_return;
  2438. if (fb->fmt.height > bttv_tvnorms[btv->tvnorm].sheight)
  2439. goto fh_unlock_and_return;
  2440. }
  2441. /* ok, accept it */
  2442. btv->fbuf.base = fb->base;
  2443. btv->fbuf.fmt.width = fb->fmt.width;
  2444. btv->fbuf.fmt.height = fb->fmt.height;
  2445. if (0 != fb->fmt.bytesperline)
  2446. btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline;
  2447. else
  2448. btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8;
  2449. retval = 0;
  2450. fh->ovfmt = fmt;
  2451. btv->init.ovfmt = fmt;
  2452. if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
  2453. fh->ov.w.left = 0;
  2454. fh->ov.w.top = 0;
  2455. fh->ov.w.width = fb->fmt.width;
  2456. fh->ov.w.height = fb->fmt.height;
  2457. btv->init.ov.w.width = fb->fmt.width;
  2458. btv->init.ov.w.height = fb->fmt.height;
  2459. kfree(fh->ov.clips);
  2460. fh->ov.clips = NULL;
  2461. fh->ov.nclips = 0;
  2462. if (check_btres(fh, RESOURCE_OVERLAY)) {
  2463. struct bttv_buffer *new;
  2464. new = videobuf_alloc(sizeof(*new));
  2465. bttv_overlay_risc(btv,&fh->ov,fh->ovfmt,new);
  2466. retval = bttv_switch_overlay(btv,fh,new);
  2467. }
  2468. }
  2469. up(&fh->cap.lock);
  2470. return retval;
  2471. }
  2472. case VIDIOC_REQBUFS:
  2473. return videobuf_reqbufs(bttv_queue(fh),arg);
  2474. case VIDIOC_QUERYBUF:
  2475. return videobuf_querybuf(bttv_queue(fh),arg);
  2476. case VIDIOC_QBUF:
  2477. return videobuf_qbuf(bttv_queue(fh),arg);
  2478. case VIDIOC_DQBUF:
  2479. return videobuf_dqbuf(bttv_queue(fh),arg,
  2480. file->f_flags & O_NONBLOCK);
  2481. case VIDIOC_STREAMON:
  2482. {
  2483. int res = bttv_resource(fh);
  2484. if (!check_alloc_btres(btv,fh,res))
  2485. return -EBUSY;
  2486. return videobuf_streamon(bttv_queue(fh));
  2487. }
  2488. case VIDIOC_STREAMOFF:
  2489. {
  2490. int res = bttv_resource(fh);
  2491. retval = videobuf_streamoff(bttv_queue(fh));
  2492. if (retval < 0)
  2493. return retval;
  2494. free_btres(btv,fh,res);
  2495. return 0;
  2496. }
  2497. case VIDIOC_QUERYCTRL:
  2498. {
  2499. struct v4l2_queryctrl *c = arg;
  2500. int i;
  2501. if ((c->id < V4L2_CID_BASE ||
  2502. c->id >= V4L2_CID_LASTP1) &&
  2503. (c->id < V4L2_CID_PRIVATE_BASE ||
  2504. c->id >= V4L2_CID_PRIVATE_LASTP1))
  2505. return -EINVAL;
  2506. for (i = 0; i < BTTV_CTLS; i++)
  2507. if (bttv_ctls[i].id == c->id)
  2508. break;
  2509. if (i == BTTV_CTLS) {
  2510. *c = no_ctl;
  2511. return 0;
  2512. }
  2513. *c = bttv_ctls[i];
  2514. if (i >= 4 && i <= 8) {
  2515. struct video_audio va;
  2516. memset(&va,0,sizeof(va));
  2517. bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
  2518. if (btv->audio_hook)
  2519. btv->audio_hook(btv,&va,0);
  2520. switch (bttv_ctls[i].id) {
  2521. case V4L2_CID_AUDIO_VOLUME:
  2522. if (!(va.flags & VIDEO_AUDIO_VOLUME))
  2523. *c = no_ctl;
  2524. break;
  2525. case V4L2_CID_AUDIO_BALANCE:
  2526. if (!(va.flags & VIDEO_AUDIO_BALANCE))
  2527. *c = no_ctl;
  2528. break;
  2529. case V4L2_CID_AUDIO_BASS:
  2530. if (!(va.flags & VIDEO_AUDIO_BASS))
  2531. *c = no_ctl;
  2532. break;
  2533. case V4L2_CID_AUDIO_TREBLE:
  2534. if (!(va.flags & VIDEO_AUDIO_TREBLE))
  2535. *c = no_ctl;
  2536. break;
  2537. }
  2538. }
  2539. return 0;
  2540. }
  2541. case VIDIOC_G_CTRL:
  2542. return get_control(btv,arg);
  2543. case VIDIOC_S_CTRL:
  2544. return set_control(btv,arg);
  2545. case VIDIOC_G_PARM:
  2546. {
  2547. struct v4l2_streamparm *parm = arg;
  2548. struct v4l2_standard s;
  2549. if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  2550. return -EINVAL;
  2551. memset(parm,0,sizeof(*parm));
  2552. v4l2_video_std_construct(&s, bttv_tvnorms[btv->tvnorm].v4l2_id,
  2553. bttv_tvnorms[btv->tvnorm].name);
  2554. parm->parm.capture.timeperframe = s.frameperiod;
  2555. return 0;
  2556. }
  2557. case VIDIOC_G_PRIORITY:
  2558. {
  2559. enum v4l2_priority *p = arg;
  2560. *p = v4l2_prio_max(&btv->prio);
  2561. return 0;
  2562. }
  2563. case VIDIOC_S_PRIORITY:
  2564. {
  2565. enum v4l2_priority *prio = arg;
  2566. return v4l2_prio_change(&btv->prio, &fh->prio, *prio);
  2567. }
  2568. case VIDIOC_ENUMSTD:
  2569. case VIDIOC_G_STD:
  2570. case VIDIOC_S_STD:
  2571. case VIDIOC_ENUMINPUT:
  2572. case VIDIOC_G_INPUT:
  2573. case VIDIOC_S_INPUT:
  2574. case VIDIOC_G_TUNER:
  2575. case VIDIOC_S_TUNER:
  2576. case VIDIOC_G_FREQUENCY:
  2577. case VIDIOC_S_FREQUENCY:
  2578. case VIDIOC_LOG_STATUS:
  2579. return bttv_common_ioctls(btv,cmd,arg);
  2580. default:
  2581. return -ENOIOCTLCMD;
  2582. }
  2583. return 0;
  2584. fh_unlock_and_return:
  2585. up(&fh->cap.lock);
  2586. return retval;
  2587. }
  2588. static int bttv_ioctl(struct inode *inode, struct file *file,
  2589. unsigned int cmd, unsigned long arg)
  2590. {
  2591. struct bttv_fh *fh = file->private_data;
  2592. switch (cmd) {
  2593. case BTTV_VBISIZE:
  2594. bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
  2595. return fh->lines * 2 * 2048;
  2596. default:
  2597. return video_usercopy(inode, file, cmd, arg, bttv_do_ioctl);
  2598. }
  2599. }
  2600. static ssize_t bttv_read(struct file *file, char __user *data,
  2601. size_t count, loff_t *ppos)
  2602. {
  2603. struct bttv_fh *fh = file->private_data;
  2604. int retval = 0;
  2605. if (fh->btv->errors)
  2606. bttv_reinit_bt848(fh->btv);
  2607. dprintk("bttv%d: read count=%d type=%s\n",
  2608. fh->btv->c.nr,(int)count,v4l2_type_names[fh->type]);
  2609. switch (fh->type) {
  2610. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  2611. if (locked_btres(fh->btv,RESOURCE_VIDEO))
  2612. return -EBUSY;
  2613. retval = videobuf_read_one(&fh->cap, data, count, ppos,
  2614. file->f_flags & O_NONBLOCK);
  2615. break;
  2616. case V4L2_BUF_TYPE_VBI_CAPTURE:
  2617. if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
  2618. return -EBUSY;
  2619. retval = videobuf_read_stream(&fh->vbi, data, count, ppos, 1,
  2620. file->f_flags & O_NONBLOCK);
  2621. break;
  2622. default:
  2623. BUG();
  2624. }
  2625. return retval;
  2626. }
  2627. static unsigned int bttv_poll(struct file *file, poll_table *wait)
  2628. {
  2629. struct bttv_fh *fh = file->private_data;
  2630. struct bttv_buffer *buf;
  2631. enum v4l2_field field;
  2632. if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
  2633. if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
  2634. return POLLERR;
  2635. return videobuf_poll_stream(file, &fh->vbi, wait);
  2636. }
  2637. if (check_btres(fh,RESOURCE_VIDEO)) {
  2638. /* streaming capture */
  2639. if (list_empty(&fh->cap.stream))
  2640. return POLLERR;
  2641. buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream);
  2642. } else {
  2643. /* read() capture */
  2644. down(&fh->cap.lock);
  2645. if (NULL == fh->cap.read_buf) {
  2646. /* need to capture a new frame */
  2647. if (locked_btres(fh->btv,RESOURCE_VIDEO)) {
  2648. up(&fh->cap.lock);
  2649. return POLLERR;
  2650. }
  2651. fh->cap.read_buf = videobuf_alloc(fh->cap.msize);
  2652. if (NULL == fh->cap.read_buf) {
  2653. up(&fh->cap.lock);
  2654. return POLLERR;
  2655. }
  2656. fh->cap.read_buf->memory = V4L2_MEMORY_USERPTR;
  2657. field = videobuf_next_field(&fh->cap);
  2658. if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,field)) {
  2659. up(&fh->cap.lock);
  2660. return POLLERR;
  2661. }
  2662. fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
  2663. fh->cap.read_off = 0;
  2664. }
  2665. up(&fh->cap.lock);
  2666. buf = (struct bttv_buffer*)fh->cap.read_buf;
  2667. }
  2668. poll_wait(file, &buf->vb.done, wait);
  2669. if (buf->vb.state == STATE_DONE ||
  2670. buf->vb.state == STATE_ERROR)
  2671. return POLLIN|POLLRDNORM;
  2672. return 0;
  2673. }
  2674. static int bttv_open(struct inode *inode, struct file *file)
  2675. {
  2676. int minor = iminor(inode);
  2677. struct bttv *btv = NULL;
  2678. struct bttv_fh *fh;
  2679. enum v4l2_buf_type type = 0;
  2680. unsigned int i;
  2681. dprintk(KERN_DEBUG "bttv: open minor=%d\n",minor);
  2682. for (i = 0; i < bttv_num; i++) {
  2683. if (bttvs[i].video_dev &&
  2684. bttvs[i].video_dev->minor == minor) {
  2685. btv = &bttvs[i];
  2686. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  2687. break;
  2688. }
  2689. if (bttvs[i].vbi_dev &&
  2690. bttvs[i].vbi_dev->minor == minor) {
  2691. btv = &bttvs[i];
  2692. type = V4L2_BUF_TYPE_VBI_CAPTURE;
  2693. break;
  2694. }
  2695. }
  2696. if (NULL == btv)
  2697. return -ENODEV;
  2698. dprintk(KERN_DEBUG "bttv%d: open called (type=%s)\n",
  2699. btv->c.nr,v4l2_type_names[type]);
  2700. /* allocate per filehandle data */
  2701. fh = kmalloc(sizeof(*fh),GFP_KERNEL);
  2702. if (NULL == fh)
  2703. return -ENOMEM;
  2704. file->private_data = fh;
  2705. *fh = btv->init;
  2706. fh->type = type;
  2707. fh->ov.setup_ok = 0;
  2708. v4l2_prio_open(&btv->prio,&fh->prio);
  2709. videobuf_queue_init(&fh->cap, &bttv_video_qops,
  2710. btv->c.pci, &btv->s_lock,
  2711. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  2712. V4L2_FIELD_INTERLACED,
  2713. sizeof(struct bttv_buffer),
  2714. fh);
  2715. videobuf_queue_init(&fh->vbi, &bttv_vbi_qops,
  2716. btv->c.pci, &btv->s_lock,
  2717. V4L2_BUF_TYPE_VBI_CAPTURE,
  2718. V4L2_FIELD_SEQ_TB,
  2719. sizeof(struct bttv_buffer),
  2720. fh);
  2721. i2c_vidiocschan(btv);
  2722. btv->users++;
  2723. if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)
  2724. bttv_vbi_setlines(fh,btv,16);
  2725. bttv_field_count(btv);
  2726. return 0;
  2727. }
  2728. static int bttv_release(struct inode *inode, struct file *file)
  2729. {
  2730. struct bttv_fh *fh = file->private_data;
  2731. struct bttv *btv = fh->btv;
  2732. /* turn off overlay */
  2733. if (check_btres(fh, RESOURCE_OVERLAY))
  2734. bttv_switch_overlay(btv,fh,NULL);
  2735. /* stop video capture */
  2736. if (check_btres(fh, RESOURCE_VIDEO)) {
  2737. videobuf_streamoff(&fh->cap);
  2738. free_btres(btv,fh,RESOURCE_VIDEO);
  2739. }
  2740. if (fh->cap.read_buf) {
  2741. buffer_release(&fh->cap,fh->cap.read_buf);
  2742. kfree(fh->cap.read_buf);
  2743. }
  2744. /* stop vbi capture */
  2745. if (check_btres(fh, RESOURCE_VBI)) {
  2746. if (fh->vbi.streaming)
  2747. videobuf_streamoff(&fh->vbi);
  2748. if (fh->vbi.reading)
  2749. videobuf_read_stop(&fh->vbi);
  2750. free_btres(btv,fh,RESOURCE_VBI);
  2751. }
  2752. /* free stuff */
  2753. videobuf_mmap_free(&fh->cap);
  2754. videobuf_mmap_free(&fh->vbi);
  2755. v4l2_prio_close(&btv->prio,&fh->prio);
  2756. file->private_data = NULL;
  2757. kfree(fh);
  2758. btv->users--;
  2759. bttv_field_count(btv);
  2760. return 0;
  2761. }
  2762. static int
  2763. bttv_mmap(struct file *file, struct vm_area_struct *vma)
  2764. {
  2765. struct bttv_fh *fh = file->private_data;
  2766. dprintk("bttv%d: mmap type=%s 0x%lx+%ld\n",
  2767. fh->btv->c.nr, v4l2_type_names[fh->type],
  2768. vma->vm_start, vma->vm_end - vma->vm_start);
  2769. return videobuf_mmap_mapper(bttv_queue(fh),vma);
  2770. }
  2771. static struct file_operations bttv_fops =
  2772. {
  2773. .owner = THIS_MODULE,
  2774. .open = bttv_open,
  2775. .release = bttv_release,
  2776. .ioctl = bttv_ioctl,
  2777. .llseek = no_llseek,
  2778. .read = bttv_read,
  2779. .mmap = bttv_mmap,
  2780. .poll = bttv_poll,
  2781. };
  2782. static struct video_device bttv_video_template =
  2783. {
  2784. .name = "UNSET",
  2785. .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|
  2786. VID_TYPE_CLIPPING|VID_TYPE_SCALES,
  2787. .hardware = VID_HARDWARE_BT848,
  2788. .fops = &bttv_fops,
  2789. .minor = -1,
  2790. };
  2791. static struct video_device bttv_vbi_template =
  2792. {
  2793. .name = "bt848/878 vbi",
  2794. .type = VID_TYPE_TUNER|VID_TYPE_TELETEXT,
  2795. .hardware = VID_HARDWARE_BT848,
  2796. .fops = &bttv_fops,
  2797. .minor = -1,
  2798. };
  2799. /* ----------------------------------------------------------------------- */
  2800. /* radio interface */
  2801. static int radio_open(struct inode *inode, struct file *file)
  2802. {
  2803. int minor = iminor(inode);
  2804. struct bttv *btv = NULL;
  2805. unsigned int i;
  2806. dprintk("bttv: open minor=%d\n",minor);
  2807. for (i = 0; i < bttv_num; i++) {
  2808. if (bttvs[i].radio_dev->minor == minor) {
  2809. btv = &bttvs[i];
  2810. break;
  2811. }
  2812. }
  2813. if (NULL == btv)
  2814. return -ENODEV;
  2815. dprintk("bttv%d: open called (radio)\n",btv->c.nr);
  2816. down(&btv->lock);
  2817. btv->radio_user++;
  2818. file->private_data = btv;
  2819. bttv_call_i2c_clients(btv,AUDC_SET_RADIO,&btv->tuner_type);
  2820. audio_mux(btv,AUDIO_RADIO);
  2821. up(&btv->lock);
  2822. return 0;
  2823. }
  2824. static int radio_release(struct inode *inode, struct file *file)
  2825. {
  2826. struct bttv *btv = file->private_data;
  2827. struct rds_command cmd;
  2828. btv->radio_user--;
  2829. bttv_call_i2c_clients(btv, RDS_CMD_CLOSE, &cmd);
  2830. return 0;
  2831. }
  2832. static int radio_do_ioctl(struct inode *inode, struct file *file,
  2833. unsigned int cmd, void *arg)
  2834. {
  2835. struct bttv *btv = file->private_data;
  2836. switch (cmd) {
  2837. case VIDIOCGCAP:
  2838. {
  2839. struct video_capability *cap = arg;
  2840. memset(cap,0,sizeof(*cap));
  2841. strcpy(cap->name,btv->radio_dev->name);
  2842. cap->type = VID_TYPE_TUNER;
  2843. cap->channels = 1;
  2844. cap->audios = 1;
  2845. return 0;
  2846. }
  2847. case VIDIOCGTUNER:
  2848. {
  2849. struct video_tuner *v = arg;
  2850. if(v->tuner)
  2851. return -EINVAL;
  2852. memset(v,0,sizeof(*v));
  2853. strcpy(v->name, "Radio");
  2854. bttv_call_i2c_clients(btv,cmd,v);
  2855. return 0;
  2856. }
  2857. case VIDIOCSTUNER:
  2858. /* nothing to do */
  2859. return 0;
  2860. case BTTV_VERSION:
  2861. case VIDIOCGFREQ:
  2862. case VIDIOCSFREQ:
  2863. case VIDIOCGAUDIO:
  2864. case VIDIOCSAUDIO:
  2865. return bttv_common_ioctls(btv,cmd,arg);
  2866. default:
  2867. return -ENOIOCTLCMD;
  2868. }
  2869. return 0;
  2870. }
  2871. static int radio_ioctl(struct inode *inode, struct file *file,
  2872. unsigned int cmd, unsigned long arg)
  2873. {
  2874. return video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
  2875. }
  2876. static ssize_t radio_read(struct file *file, char __user *data,
  2877. size_t count, loff_t *ppos)
  2878. {
  2879. struct bttv *btv = file->private_data;
  2880. struct rds_command cmd;
  2881. cmd.block_count = count/3;
  2882. cmd.buffer = data;
  2883. cmd.instance = file;
  2884. cmd.result = -ENODEV;
  2885. bttv_call_i2c_clients(btv, RDS_CMD_READ, &cmd);
  2886. return cmd.result;
  2887. }
  2888. static unsigned int radio_poll(struct file *file, poll_table *wait)
  2889. {
  2890. struct bttv *btv = file->private_data;
  2891. struct rds_command cmd;
  2892. cmd.instance = file;
  2893. cmd.event_list = wait;
  2894. cmd.result = -ENODEV;
  2895. bttv_call_i2c_clients(btv, RDS_CMD_POLL, &cmd);
  2896. return cmd.result;
  2897. }
  2898. static struct file_operations radio_fops =
  2899. {
  2900. .owner = THIS_MODULE,
  2901. .open = radio_open,
  2902. .read = radio_read,
  2903. .release = radio_release,
  2904. .ioctl = radio_ioctl,
  2905. .llseek = no_llseek,
  2906. .poll = radio_poll,
  2907. };
  2908. static struct video_device radio_template =
  2909. {
  2910. .name = "bt848/878 radio",
  2911. .type = VID_TYPE_TUNER,
  2912. .hardware = VID_HARDWARE_BT848,
  2913. .fops = &radio_fops,
  2914. .minor = -1,
  2915. };
  2916. /* ----------------------------------------------------------------------- */
  2917. /* some debug code */
  2918. static int bttv_risc_decode(u32 risc)
  2919. {
  2920. static char *instr[16] = {
  2921. [ BT848_RISC_WRITE >> 28 ] = "write",
  2922. [ BT848_RISC_SKIP >> 28 ] = "skip",
  2923. [ BT848_RISC_WRITEC >> 28 ] = "writec",
  2924. [ BT848_RISC_JUMP >> 28 ] = "jump",
  2925. [ BT848_RISC_SYNC >> 28 ] = "sync",
  2926. [ BT848_RISC_WRITE123 >> 28 ] = "write123",
  2927. [ BT848_RISC_SKIP123 >> 28 ] = "skip123",
  2928. [ BT848_RISC_WRITE1S23 >> 28 ] = "write1s23",
  2929. };
  2930. static int incr[16] = {
  2931. [ BT848_RISC_WRITE >> 28 ] = 2,
  2932. [ BT848_RISC_JUMP >> 28 ] = 2,
  2933. [ BT848_RISC_SYNC >> 28 ] = 2,
  2934. [ BT848_RISC_WRITE123 >> 28 ] = 5,
  2935. [ BT848_RISC_SKIP123 >> 28 ] = 2,
  2936. [ BT848_RISC_WRITE1S23 >> 28 ] = 3,
  2937. };
  2938. static char *bits[] = {
  2939. "be0", "be1", "be2", "be3/resync",
  2940. "set0", "set1", "set2", "set3",
  2941. "clr0", "clr1", "clr2", "clr3",
  2942. "irq", "res", "eol", "sol",
  2943. };
  2944. int i;
  2945. printk("0x%08x [ %s", risc,
  2946. instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
  2947. for (i = ARRAY_SIZE(bits)-1; i >= 0; i--)
  2948. if (risc & (1 << (i + 12)))
  2949. printk(" %s",bits[i]);
  2950. printk(" count=%d ]\n", risc & 0xfff);
  2951. return incr[risc >> 28] ? incr[risc >> 28] : 1;
  2952. }
  2953. static void bttv_risc_disasm(struct bttv *btv,
  2954. struct btcx_riscmem *risc)
  2955. {
  2956. unsigned int i,j,n;
  2957. printk("%s: risc disasm: %p [dma=0x%08lx]\n",
  2958. btv->c.name, risc->cpu, (unsigned long)risc->dma);
  2959. for (i = 0; i < (risc->size >> 2); i += n) {
  2960. printk("%s: 0x%lx: ", btv->c.name,
  2961. (unsigned long)(risc->dma + (i<<2)));
  2962. n = bttv_risc_decode(risc->cpu[i]);
  2963. for (j = 1; j < n; j++)
  2964. printk("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
  2965. btv->c.name, (unsigned long)(risc->dma + ((i+j)<<2)),
  2966. risc->cpu[i+j], j);
  2967. if (0 == risc->cpu[i])
  2968. break;
  2969. }
  2970. }
  2971. static void bttv_print_riscaddr(struct bttv *btv)
  2972. {
  2973. printk(" main: %08Lx\n",
  2974. (unsigned long long)btv->main.dma);
  2975. printk(" vbi : o=%08Lx e=%08Lx\n",
  2976. btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
  2977. btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0);
  2978. printk(" cap : o=%08Lx e=%08Lx\n",
  2979. btv->curr.top ? (unsigned long long)btv->curr.top->top.dma : 0,
  2980. btv->curr.bottom ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
  2981. printk(" scr : o=%08Lx e=%08Lx\n",
  2982. btv->screen ? (unsigned long long)btv->screen->top.dma : 0,
  2983. btv->screen ? (unsigned long long)btv->screen->bottom.dma : 0);
  2984. bttv_risc_disasm(btv, &btv->main);
  2985. }
  2986. /* ----------------------------------------------------------------------- */
  2987. /* irq handler */
  2988. static char *irq_name[] = {
  2989. "FMTCHG", // format change detected (525 vs. 625)
  2990. "VSYNC", // vertical sync (new field)
  2991. "HSYNC", // horizontal sync
  2992. "OFLOW", // chroma/luma AGC overflow
  2993. "HLOCK", // horizontal lock changed
  2994. "VPRES", // video presence changed
  2995. "6", "7",
  2996. "I2CDONE", // hw irc operation finished
  2997. "GPINT", // gpio port triggered irq
  2998. "10",
  2999. "RISCI", // risc instruction triggered irq
  3000. "FBUS", // pixel data fifo dropped data (high pci bus latencies)
  3001. "FTRGT", // pixel data fifo overrun
  3002. "FDSR", // fifo data stream resyncronisation
  3003. "PPERR", // parity error (data transfer)
  3004. "RIPERR", // parity error (read risc instructions)
  3005. "PABORT", // pci abort
  3006. "OCERR", // risc instruction error
  3007. "SCERR", // syncronisation error
  3008. };
  3009. static void bttv_print_irqbits(u32 print, u32 mark)
  3010. {
  3011. unsigned int i;
  3012. printk("bits:");
  3013. for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
  3014. if (print & (1 << i))
  3015. printk(" %s",irq_name[i]);
  3016. if (mark & (1 << i))
  3017. printk("*");
  3018. }
  3019. }
  3020. static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc)
  3021. {
  3022. printk("bttv%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
  3023. btv->c.nr,
  3024. (unsigned long)btv->main.dma,
  3025. (unsigned long)btv->main.cpu[RISC_SLOT_O_VBI+1],
  3026. (unsigned long)btv->main.cpu[RISC_SLOT_O_FIELD+1],
  3027. (unsigned long)rc);
  3028. if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) {
  3029. printk("bttv%d: Oh, there (temporarely?) is no input signal. "
  3030. "Ok, then this is harmless, don't worry ;)\n",
  3031. btv->c.nr);
  3032. return;
  3033. }
  3034. printk("bttv%d: Uhm. Looks like we have unusual high IRQ latencies.\n",
  3035. btv->c.nr);
  3036. printk("bttv%d: Lets try to catch the culpit red-handed ...\n",
  3037. btv->c.nr);
  3038. dump_stack();
  3039. }
  3040. static int
  3041. bttv_irq_next_video(struct bttv *btv, struct bttv_buffer_set *set)
  3042. {
  3043. struct bttv_buffer *item;
  3044. memset(set,0,sizeof(*set));
  3045. /* capture request ? */
  3046. if (!list_empty(&btv->capture)) {
  3047. set->frame_irq = 1;
  3048. item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
  3049. if (V4L2_FIELD_HAS_TOP(item->vb.field))
  3050. set->top = item;
  3051. if (V4L2_FIELD_HAS_BOTTOM(item->vb.field))
  3052. set->bottom = item;
  3053. /* capture request for other field ? */
  3054. if (!V4L2_FIELD_HAS_BOTH(item->vb.field) &&
  3055. (item->vb.queue.next != &btv->capture)) {
  3056. item = list_entry(item->vb.queue.next, struct bttv_buffer, vb.queue);
  3057. if (!V4L2_FIELD_HAS_BOTH(item->vb.field)) {
  3058. if (NULL == set->top &&
  3059. V4L2_FIELD_TOP == item->vb.field) {
  3060. set->top = item;
  3061. }
  3062. if (NULL == set->bottom &&
  3063. V4L2_FIELD_BOTTOM == item->vb.field) {
  3064. set->bottom = item;
  3065. }
  3066. if (NULL != set->top && NULL != set->bottom)
  3067. set->top_irq = 2;
  3068. }
  3069. }
  3070. }
  3071. /* screen overlay ? */
  3072. if (NULL != btv->screen) {
  3073. if (V4L2_FIELD_HAS_BOTH(btv->screen->vb.field)) {
  3074. if (NULL == set->top && NULL == set->bottom) {
  3075. set->top = btv->screen;
  3076. set->bottom = btv->screen;
  3077. }
  3078. } else {
  3079. if (V4L2_FIELD_TOP == btv->screen->vb.field &&
  3080. NULL == set->top) {
  3081. set->top = btv->screen;
  3082. }
  3083. if (V4L2_FIELD_BOTTOM == btv->screen->vb.field &&
  3084. NULL == set->bottom) {
  3085. set->bottom = btv->screen;
  3086. }
  3087. }
  3088. }
  3089. dprintk("bttv%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
  3090. btv->c.nr,set->top, set->bottom,
  3091. btv->screen,set->frame_irq,set->top_irq);
  3092. return 0;
  3093. }
  3094. static void
  3095. bttv_irq_wakeup_video(struct bttv *btv, struct bttv_buffer_set *wakeup,
  3096. struct bttv_buffer_set *curr, unsigned int state)
  3097. {
  3098. struct timeval ts;
  3099. do_gettimeofday(&ts);
  3100. if (wakeup->top == wakeup->bottom) {
  3101. if (NULL != wakeup->top && curr->top != wakeup->top) {
  3102. if (irq_debug > 1)
  3103. printk("bttv%d: wakeup: both=%p\n",btv->c.nr,wakeup->top);
  3104. wakeup->top->vb.ts = ts;
  3105. wakeup->top->vb.field_count = btv->field_count;
  3106. wakeup->top->vb.state = state;
  3107. wake_up(&wakeup->top->vb.done);
  3108. }
  3109. } else {
  3110. if (NULL != wakeup->top && curr->top != wakeup->top) {
  3111. if (irq_debug > 1)
  3112. printk("bttv%d: wakeup: top=%p\n",btv->c.nr,wakeup->top);
  3113. wakeup->top->vb.ts = ts;
  3114. wakeup->top->vb.field_count = btv->field_count;
  3115. wakeup->top->vb.state = state;
  3116. wake_up(&wakeup->top->vb.done);
  3117. }
  3118. if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
  3119. if (irq_debug > 1)
  3120. printk("bttv%d: wakeup: bottom=%p\n",btv->c.nr,wakeup->bottom);
  3121. wakeup->bottom->vb.ts = ts;
  3122. wakeup->bottom->vb.field_count = btv->field_count;
  3123. wakeup->bottom->vb.state = state;
  3124. wake_up(&wakeup->bottom->vb.done);
  3125. }
  3126. }
  3127. }
  3128. static void
  3129. bttv_irq_wakeup_vbi(struct bttv *btv, struct bttv_buffer *wakeup,
  3130. unsigned int state)
  3131. {
  3132. struct timeval ts;
  3133. if (NULL == wakeup)
  3134. return;
  3135. do_gettimeofday(&ts);
  3136. wakeup->vb.ts = ts;
  3137. wakeup->vb.field_count = btv->field_count;
  3138. wakeup->vb.state = state;
  3139. wake_up(&wakeup->vb.done);
  3140. }
  3141. static void bttv_irq_timeout(unsigned long data)
  3142. {
  3143. struct bttv *btv = (struct bttv *)data;
  3144. struct bttv_buffer_set old,new;
  3145. struct bttv_buffer *ovbi;
  3146. struct bttv_buffer *item;
  3147. unsigned long flags;
  3148. if (bttv_verbose) {
  3149. printk(KERN_INFO "bttv%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
  3150. btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total,
  3151. btread(BT848_RISC_COUNT));
  3152. bttv_print_irqbits(btread(BT848_INT_STAT),0);
  3153. printk("\n");
  3154. }
  3155. spin_lock_irqsave(&btv->s_lock,flags);
  3156. /* deactivate stuff */
  3157. memset(&new,0,sizeof(new));
  3158. old = btv->curr;
  3159. ovbi = btv->cvbi;
  3160. btv->curr = new;
  3161. btv->cvbi = NULL;
  3162. btv->loop_irq = 0;
  3163. bttv_buffer_activate_video(btv, &new);
  3164. bttv_buffer_activate_vbi(btv, NULL);
  3165. bttv_set_dma(btv, 0);
  3166. /* wake up */
  3167. bttv_irq_wakeup_video(btv, &old, &new, STATE_ERROR);
  3168. bttv_irq_wakeup_vbi(btv, ovbi, STATE_ERROR);
  3169. /* cancel all outstanding capture / vbi requests */
  3170. while (!list_empty(&btv->capture)) {
  3171. item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
  3172. list_del(&item->vb.queue);
  3173. item->vb.state = STATE_ERROR;
  3174. wake_up(&item->vb.done);
  3175. }
  3176. while (!list_empty(&btv->vcapture)) {
  3177. item = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
  3178. list_del(&item->vb.queue);
  3179. item->vb.state = STATE_ERROR;
  3180. wake_up(&item->vb.done);
  3181. }
  3182. btv->errors++;
  3183. spin_unlock_irqrestore(&btv->s_lock,flags);
  3184. }
  3185. static void
  3186. bttv_irq_wakeup_top(struct bttv *btv)
  3187. {
  3188. struct bttv_buffer *wakeup = btv->curr.top;
  3189. if (NULL == wakeup)
  3190. return;
  3191. spin_lock(&btv->s_lock);
  3192. btv->curr.top_irq = 0;
  3193. btv->curr.top = NULL;
  3194. bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
  3195. do_gettimeofday(&wakeup->vb.ts);
  3196. wakeup->vb.field_count = btv->field_count;
  3197. wakeup->vb.state = STATE_DONE;
  3198. wake_up(&wakeup->vb.done);
  3199. spin_unlock(&btv->s_lock);
  3200. }
  3201. static inline int is_active(struct btcx_riscmem *risc, u32 rc)
  3202. {
  3203. if (rc < risc->dma)
  3204. return 0;
  3205. if (rc > risc->dma + risc->size)
  3206. return 0;
  3207. return 1;
  3208. }
  3209. static void
  3210. bttv_irq_switch_video(struct bttv *btv)
  3211. {
  3212. struct bttv_buffer_set new;
  3213. struct bttv_buffer_set old;
  3214. dma_addr_t rc;
  3215. spin_lock(&btv->s_lock);
  3216. /* new buffer set */
  3217. bttv_irq_next_video(btv, &new);
  3218. rc = btread(BT848_RISC_COUNT);
  3219. if ((btv->curr.top && is_active(&btv->curr.top->top, rc)) ||
  3220. (btv->curr.bottom && is_active(&btv->curr.bottom->bottom, rc))) {
  3221. btv->framedrop++;
  3222. if (debug_latency)
  3223. bttv_irq_debug_low_latency(btv, rc);
  3224. spin_unlock(&btv->s_lock);
  3225. return;
  3226. }
  3227. /* switch over */
  3228. old = btv->curr;
  3229. btv->curr = new;
  3230. btv->loop_irq &= ~1;
  3231. bttv_buffer_activate_video(btv, &new);
  3232. bttv_set_dma(btv, 0);
  3233. /* switch input */
  3234. if (UNSET != btv->new_input) {
  3235. video_mux(btv,btv->new_input);
  3236. btv->new_input = UNSET;
  3237. }
  3238. /* wake up finished buffers */
  3239. bttv_irq_wakeup_video(btv, &old, &new, STATE_DONE);
  3240. spin_unlock(&btv->s_lock);
  3241. }
  3242. static void
  3243. bttv_irq_switch_vbi(struct bttv *btv)
  3244. {
  3245. struct bttv_buffer *new = NULL;
  3246. struct bttv_buffer *old;
  3247. u32 rc;
  3248. spin_lock(&btv->s_lock);
  3249. if (!list_empty(&btv->vcapture))
  3250. new = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
  3251. old = btv->cvbi;
  3252. rc = btread(BT848_RISC_COUNT);
  3253. if (NULL != old && (is_active(&old->top, rc) ||
  3254. is_active(&old->bottom, rc))) {
  3255. btv->framedrop++;
  3256. if (debug_latency)
  3257. bttv_irq_debug_low_latency(btv, rc);
  3258. spin_unlock(&btv->s_lock);
  3259. return;
  3260. }
  3261. /* switch */
  3262. btv->cvbi = new;
  3263. btv->loop_irq &= ~4;
  3264. bttv_buffer_activate_vbi(btv, new);
  3265. bttv_set_dma(btv, 0);
  3266. bttv_irq_wakeup_vbi(btv, old, STATE_DONE);
  3267. spin_unlock(&btv->s_lock);
  3268. }
  3269. static irqreturn_t bttv_irq(int irq, void *dev_id, struct pt_regs * regs)
  3270. {
  3271. u32 stat,astat;
  3272. u32 dstat;
  3273. int count;
  3274. struct bttv *btv;
  3275. int handled = 0;
  3276. btv=(struct bttv *)dev_id;
  3277. count=0;
  3278. while (1) {
  3279. /* get/clear interrupt status bits */
  3280. stat=btread(BT848_INT_STAT);
  3281. astat=stat&btread(BT848_INT_MASK);
  3282. if (!astat)
  3283. break;
  3284. handled = 1;
  3285. btwrite(stat,BT848_INT_STAT);
  3286. /* get device status bits */
  3287. dstat=btread(BT848_DSTATUS);
  3288. if (irq_debug) {
  3289. printk(KERN_DEBUG "bttv%d: irq loop=%d fc=%d "
  3290. "riscs=%x, riscc=%08x, ",
  3291. btv->c.nr, count, btv->field_count,
  3292. stat>>28, btread(BT848_RISC_COUNT));
  3293. bttv_print_irqbits(stat,astat);
  3294. if (stat & BT848_INT_HLOCK)
  3295. printk(" HLOC => %s", (dstat & BT848_DSTATUS_HLOC)
  3296. ? "yes" : "no");
  3297. if (stat & BT848_INT_VPRES)
  3298. printk(" PRES => %s", (dstat & BT848_DSTATUS_PRES)
  3299. ? "yes" : "no");
  3300. if (stat & BT848_INT_FMTCHG)
  3301. printk(" NUML => %s", (dstat & BT848_DSTATUS_NUML)
  3302. ? "625" : "525");
  3303. printk("\n");
  3304. }
  3305. if (astat&BT848_INT_VSYNC)
  3306. btv->field_count++;
  3307. if (astat & BT848_INT_GPINT) {
  3308. wake_up(&btv->gpioq);
  3309. bttv_gpio_irq(&btv->c);
  3310. }
  3311. if (astat & BT848_INT_I2CDONE) {
  3312. btv->i2c_done = stat;
  3313. wake_up(&btv->i2c_queue);
  3314. }
  3315. if ((astat & BT848_INT_RISCI) && (stat & (4<<28)))
  3316. bttv_irq_switch_vbi(btv);
  3317. if ((astat & BT848_INT_RISCI) && (stat & (2<<28)))
  3318. bttv_irq_wakeup_top(btv);
  3319. if ((astat & BT848_INT_RISCI) && (stat & (1<<28)))
  3320. bttv_irq_switch_video(btv);
  3321. if ((astat & BT848_INT_HLOCK) && btv->opt_automute)
  3322. audio_mux(btv, -1);
  3323. if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
  3324. printk(KERN_INFO "bttv%d: %s%s @ %08x,",btv->c.nr,
  3325. (astat & BT848_INT_SCERR) ? "SCERR" : "",
  3326. (astat & BT848_INT_OCERR) ? "OCERR" : "",
  3327. btread(BT848_RISC_COUNT));
  3328. bttv_print_irqbits(stat,astat);
  3329. printk("\n");
  3330. if (bttv_debug)
  3331. bttv_print_riscaddr(btv);
  3332. }
  3333. if (fdsr && astat & BT848_INT_FDSR) {
  3334. printk(KERN_INFO "bttv%d: FDSR @ %08x\n",
  3335. btv->c.nr,btread(BT848_RISC_COUNT));
  3336. if (bttv_debug)
  3337. bttv_print_riscaddr(btv);
  3338. }
  3339. count++;
  3340. if (count > 4) {
  3341. if (count > 8 || !(astat & BT848_INT_GPINT)) {
  3342. btwrite(0, BT848_INT_MASK);
  3343. printk(KERN_ERR
  3344. "bttv%d: IRQ lockup, cleared int mask [", btv->c.nr);
  3345. } else {
  3346. printk(KERN_ERR
  3347. "bttv%d: IRQ lockup, clearing GPINT from int mask [", btv->c.nr);
  3348. btwrite(btread(BT848_INT_MASK) & (-1 ^ BT848_INT_GPINT),
  3349. BT848_INT_MASK);
  3350. };
  3351. bttv_print_irqbits(stat,astat);
  3352. printk("]\n");
  3353. }
  3354. }
  3355. btv->irq_total++;
  3356. if (handled)
  3357. btv->irq_me++;
  3358. return IRQ_RETVAL(handled);
  3359. }
  3360. /* ----------------------------------------------------------------------- */
  3361. /* initialitation */
  3362. static struct video_device *vdev_init(struct bttv *btv,
  3363. struct video_device *template,
  3364. char *type)
  3365. {
  3366. struct video_device *vfd;
  3367. vfd = video_device_alloc();
  3368. if (NULL == vfd)
  3369. return NULL;
  3370. *vfd = *template;
  3371. vfd->minor = -1;
  3372. vfd->dev = &btv->c.pci->dev;
  3373. vfd->release = video_device_release;
  3374. snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
  3375. btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
  3376. type, bttv_tvcards[btv->c.type].name);
  3377. return vfd;
  3378. }
  3379. static void bttv_unregister_video(struct bttv *btv)
  3380. {
  3381. if (btv->video_dev) {
  3382. if (-1 != btv->video_dev->minor)
  3383. video_unregister_device(btv->video_dev);
  3384. else
  3385. video_device_release(btv->video_dev);
  3386. btv->video_dev = NULL;
  3387. }
  3388. if (btv->vbi_dev) {
  3389. if (-1 != btv->vbi_dev->minor)
  3390. video_unregister_device(btv->vbi_dev);
  3391. else
  3392. video_device_release(btv->vbi_dev);
  3393. btv->vbi_dev = NULL;
  3394. }
  3395. if (btv->radio_dev) {
  3396. if (-1 != btv->radio_dev->minor)
  3397. video_unregister_device(btv->radio_dev);
  3398. else
  3399. video_device_release(btv->radio_dev);
  3400. btv->radio_dev = NULL;
  3401. }
  3402. }
  3403. /* register video4linux devices */
  3404. static int __devinit bttv_register_video(struct bttv *btv)
  3405. {
  3406. if (no_overlay <= 0) {
  3407. bttv_video_template.type |= VID_TYPE_OVERLAY;
  3408. } else {
  3409. printk("bttv: Overlay support disabled.\n");
  3410. }
  3411. /* video */
  3412. btv->video_dev = vdev_init(btv, &bttv_video_template, "video");
  3413. if (NULL == btv->video_dev)
  3414. goto err;
  3415. if (video_register_device(btv->video_dev,VFL_TYPE_GRABBER,video_nr)<0)
  3416. goto err;
  3417. printk(KERN_INFO "bttv%d: registered device video%d\n",
  3418. btv->c.nr,btv->video_dev->minor & 0x1f);
  3419. video_device_create_file(btv->video_dev, &class_device_attr_card);
  3420. /* vbi */
  3421. btv->vbi_dev = vdev_init(btv, &bttv_vbi_template, "vbi");
  3422. if (NULL == btv->vbi_dev)
  3423. goto err;
  3424. if (video_register_device(btv->vbi_dev,VFL_TYPE_VBI,vbi_nr)<0)
  3425. goto err;
  3426. printk(KERN_INFO "bttv%d: registered device vbi%d\n",
  3427. btv->c.nr,btv->vbi_dev->minor & 0x1f);
  3428. if (!btv->has_radio)
  3429. return 0;
  3430. /* radio */
  3431. btv->radio_dev = vdev_init(btv, &radio_template, "radio");
  3432. if (NULL == btv->radio_dev)
  3433. goto err;
  3434. if (video_register_device(btv->radio_dev, VFL_TYPE_RADIO,radio_nr)<0)
  3435. goto err;
  3436. printk(KERN_INFO "bttv%d: registered device radio%d\n",
  3437. btv->c.nr,btv->radio_dev->minor & 0x1f);
  3438. /* all done */
  3439. return 0;
  3440. err:
  3441. bttv_unregister_video(btv);
  3442. return -1;
  3443. }
  3444. /* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
  3445. /* response on cards with no firmware is not enabled by OF */
  3446. static void pci_set_command(struct pci_dev *dev)
  3447. {
  3448. #if defined(__powerpc__)
  3449. unsigned int cmd;
  3450. pci_read_config_dword(dev, PCI_COMMAND, &cmd);
  3451. cmd = (cmd | PCI_COMMAND_MEMORY );
  3452. pci_write_config_dword(dev, PCI_COMMAND, cmd);
  3453. #endif
  3454. }
  3455. static int __devinit bttv_probe(struct pci_dev *dev,
  3456. const struct pci_device_id *pci_id)
  3457. {
  3458. int result;
  3459. unsigned char lat;
  3460. struct bttv *btv;
  3461. if (bttv_num == BTTV_MAX)
  3462. return -ENOMEM;
  3463. printk(KERN_INFO "bttv: Bt8xx card found (%d).\n", bttv_num);
  3464. btv=&bttvs[bttv_num];
  3465. memset(btv,0,sizeof(*btv));
  3466. btv->c.nr = bttv_num;
  3467. sprintf(btv->c.name,"bttv%d",btv->c.nr);
  3468. /* initialize structs / fill in defaults */
  3469. init_MUTEX(&btv->lock);
  3470. init_MUTEX(&btv->reslock);
  3471. spin_lock_init(&btv->s_lock);
  3472. spin_lock_init(&btv->gpio_lock);
  3473. init_waitqueue_head(&btv->gpioq);
  3474. init_waitqueue_head(&btv->i2c_queue);
  3475. INIT_LIST_HEAD(&btv->c.subs);
  3476. INIT_LIST_HEAD(&btv->capture);
  3477. INIT_LIST_HEAD(&btv->vcapture);
  3478. v4l2_prio_init(&btv->prio);
  3479. init_timer(&btv->timeout);
  3480. btv->timeout.function = bttv_irq_timeout;
  3481. btv->timeout.data = (unsigned long)btv;
  3482. btv->i2c_rc = -1;
  3483. btv->tuner_type = UNSET;
  3484. btv->pinnacle_id = UNSET;
  3485. btv->new_input = UNSET;
  3486. btv->has_radio=radio[btv->c.nr];
  3487. /* pci stuff (init, get irq/mmio, ... */
  3488. btv->c.pci = dev;
  3489. btv->id = dev->device;
  3490. if (pci_enable_device(dev)) {
  3491. printk(KERN_WARNING "bttv%d: Can't enable device.\n",
  3492. btv->c.nr);
  3493. return -EIO;
  3494. }
  3495. if (pci_set_dma_mask(dev, DMA_32BIT_MASK)) {
  3496. printk(KERN_WARNING "bttv%d: No suitable DMA available.\n",
  3497. btv->c.nr);
  3498. return -EIO;
  3499. }
  3500. if (!request_mem_region(pci_resource_start(dev,0),
  3501. pci_resource_len(dev,0),
  3502. btv->c.name)) {
  3503. printk(KERN_WARNING "bttv%d: can't request iomem (0x%lx).\n",
  3504. btv->c.nr, pci_resource_start(dev,0));
  3505. return -EBUSY;
  3506. }
  3507. pci_set_master(dev);
  3508. pci_set_command(dev);
  3509. pci_set_drvdata(dev,btv);
  3510. pci_read_config_byte(dev, PCI_CLASS_REVISION, &btv->revision);
  3511. pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
  3512. printk(KERN_INFO "bttv%d: Bt%d (rev %d) at %s, ",
  3513. bttv_num,btv->id, btv->revision, pci_name(dev));
  3514. printk("irq: %d, latency: %d, mmio: 0x%lx\n",
  3515. btv->c.pci->irq, lat, pci_resource_start(dev,0));
  3516. schedule();
  3517. btv->bt848_mmio=ioremap(pci_resource_start(dev,0), 0x1000);
  3518. if (NULL == ioremap(pci_resource_start(dev,0), 0x1000)) {
  3519. printk("bttv%d: ioremap() failed\n", btv->c.nr);
  3520. result = -EIO;
  3521. goto fail1;
  3522. }
  3523. /* identify card */
  3524. bttv_idcard(btv);
  3525. /* disable irqs, register irq handler */
  3526. btwrite(0, BT848_INT_MASK);
  3527. result = request_irq(btv->c.pci->irq, bttv_irq,
  3528. SA_SHIRQ | SA_INTERRUPT,btv->c.name,(void *)btv);
  3529. if (result < 0) {
  3530. printk(KERN_ERR "bttv%d: can't get IRQ %d\n",
  3531. bttv_num,btv->c.pci->irq);
  3532. goto fail1;
  3533. }
  3534. if (0 != bttv_handle_chipset(btv)) {
  3535. result = -EIO;
  3536. goto fail2;
  3537. }
  3538. /* init options from insmod args */
  3539. btv->opt_combfilter = combfilter;
  3540. btv->opt_lumafilter = lumafilter;
  3541. btv->opt_automute = automute;
  3542. btv->opt_chroma_agc = chroma_agc;
  3543. btv->opt_adc_crush = adc_crush;
  3544. btv->opt_vcr_hack = vcr_hack;
  3545. btv->opt_whitecrush_upper = whitecrush_upper;
  3546. btv->opt_whitecrush_lower = whitecrush_lower;
  3547. btv->opt_uv_ratio = uv_ratio;
  3548. btv->opt_full_luma_range = full_luma_range;
  3549. btv->opt_coring = coring;
  3550. /* fill struct bttv with some useful defaults */
  3551. btv->init.btv = btv;
  3552. btv->init.ov.w.width = 320;
  3553. btv->init.ov.w.height = 240;
  3554. btv->init.fmt = format_by_palette(VIDEO_PALETTE_RGB24);
  3555. btv->init.width = 320;
  3556. btv->init.height = 240;
  3557. btv->init.lines = 16;
  3558. btv->input = 0;
  3559. /* initialize hardware */
  3560. if (bttv_gpio)
  3561. bttv_gpio_tracking(btv,"pre-init");
  3562. bttv_risc_init_main(btv);
  3563. init_bt848(btv);
  3564. /* gpio */
  3565. btwrite(0x00, BT848_GPIO_REG_INP);
  3566. btwrite(0x00, BT848_GPIO_OUT_EN);
  3567. if (bttv_verbose)
  3568. bttv_gpio_tracking(btv,"init");
  3569. /* needs to be done before i2c is registered */
  3570. bttv_init_card1(btv);
  3571. /* register i2c + gpio */
  3572. init_bttv_i2c(btv);
  3573. /* some card-specific stuff (needs working i2c) */
  3574. bttv_init_card2(btv);
  3575. init_irqreg(btv);
  3576. /* register video4linux + input */
  3577. if (!bttv_tvcards[btv->c.type].no_video) {
  3578. bttv_register_video(btv);
  3579. bt848_bright(btv,32768);
  3580. bt848_contrast(btv,32768);
  3581. bt848_hue(btv,32768);
  3582. bt848_sat(btv,32768);
  3583. audio_mux(btv,AUDIO_MUTE);
  3584. set_input(btv,0);
  3585. }
  3586. /* add subdevices */
  3587. if (btv->has_remote)
  3588. bttv_sub_add_device(&btv->c, "remote");
  3589. if (bttv_tvcards[btv->c.type].has_dvb)
  3590. bttv_sub_add_device(&btv->c, "dvb");
  3591. /* everything is fine */
  3592. bttv_num++;
  3593. return 0;
  3594. fail2:
  3595. free_irq(btv->c.pci->irq,btv);
  3596. fail1:
  3597. if (btv->bt848_mmio)
  3598. iounmap(btv->bt848_mmio);
  3599. release_mem_region(pci_resource_start(btv->c.pci,0),
  3600. pci_resource_len(btv->c.pci,0));
  3601. pci_set_drvdata(dev,NULL);
  3602. return result;
  3603. }
  3604. static void __devexit bttv_remove(struct pci_dev *pci_dev)
  3605. {
  3606. struct bttv *btv = pci_get_drvdata(pci_dev);
  3607. if (bttv_verbose)
  3608. printk("bttv%d: unloading\n",btv->c.nr);
  3609. /* shutdown everything (DMA+IRQs) */
  3610. btand(~15, BT848_GPIO_DMA_CTL);
  3611. btwrite(0, BT848_INT_MASK);
  3612. btwrite(~0x0, BT848_INT_STAT);
  3613. btwrite(0x0, BT848_GPIO_OUT_EN);
  3614. if (bttv_gpio)
  3615. bttv_gpio_tracking(btv,"cleanup");
  3616. /* tell gpio modules we are leaving ... */
  3617. btv->shutdown=1;
  3618. wake_up(&btv->gpioq);
  3619. bttv_sub_del_devices(&btv->c);
  3620. /* unregister i2c_bus + input */
  3621. fini_bttv_i2c(btv);
  3622. /* unregister video4linux */
  3623. bttv_unregister_video(btv);
  3624. /* free allocated memory */
  3625. btcx_riscmem_free(btv->c.pci,&btv->main);
  3626. /* free ressources */
  3627. free_irq(btv->c.pci->irq,btv);
  3628. iounmap(btv->bt848_mmio);
  3629. release_mem_region(pci_resource_start(btv->c.pci,0),
  3630. pci_resource_len(btv->c.pci,0));
  3631. pci_set_drvdata(pci_dev, NULL);
  3632. return;
  3633. }
  3634. static int bttv_suspend(struct pci_dev *pci_dev, pm_message_t state)
  3635. {
  3636. struct bttv *btv = pci_get_drvdata(pci_dev);
  3637. struct bttv_buffer_set idle;
  3638. unsigned long flags;
  3639. dprintk("bttv%d: suspend %d\n", btv->c.nr, state.event);
  3640. /* stop dma + irqs */
  3641. spin_lock_irqsave(&btv->s_lock,flags);
  3642. memset(&idle, 0, sizeof(idle));
  3643. btv->state.video = btv->curr;
  3644. btv->state.vbi = btv->cvbi;
  3645. btv->state.loop_irq = btv->loop_irq;
  3646. btv->curr = idle;
  3647. btv->loop_irq = 0;
  3648. bttv_buffer_activate_video(btv, &idle);
  3649. bttv_buffer_activate_vbi(btv, NULL);
  3650. bttv_set_dma(btv, 0);
  3651. btwrite(0, BT848_INT_MASK);
  3652. spin_unlock_irqrestore(&btv->s_lock,flags);
  3653. /* save bt878 state */
  3654. btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN);
  3655. btv->state.gpio_data = gpio_read();
  3656. /* save pci state */
  3657. pci_save_state(pci_dev);
  3658. if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
  3659. pci_disable_device(pci_dev);
  3660. btv->state.disabled = 1;
  3661. }
  3662. return 0;
  3663. }
  3664. static int bttv_resume(struct pci_dev *pci_dev)
  3665. {
  3666. struct bttv *btv = pci_get_drvdata(pci_dev);
  3667. unsigned long flags;
  3668. int err;
  3669. dprintk("bttv%d: resume\n", btv->c.nr);
  3670. /* restore pci state */
  3671. if (btv->state.disabled) {
  3672. err=pci_enable_device(pci_dev);
  3673. if (err) {
  3674. printk(KERN_WARNING "bttv%d: Can't enable device.\n",
  3675. btv->c.nr);
  3676. return err;
  3677. }
  3678. btv->state.disabled = 0;
  3679. }
  3680. err=pci_set_power_state(pci_dev, PCI_D0);
  3681. if (err) {
  3682. pci_disable_device(pci_dev);
  3683. printk(KERN_WARNING "bttv%d: Can't enable device.\n",
  3684. btv->c.nr);
  3685. btv->state.disabled = 1;
  3686. return err;
  3687. }
  3688. pci_restore_state(pci_dev);
  3689. /* restore bt878 state */
  3690. bttv_reinit_bt848(btv);
  3691. gpio_inout(0xffffff, btv->state.gpio_enable);
  3692. gpio_write(btv->state.gpio_data);
  3693. /* restart dma */
  3694. spin_lock_irqsave(&btv->s_lock,flags);
  3695. btv->curr = btv->state.video;
  3696. btv->cvbi = btv->state.vbi;
  3697. btv->loop_irq = btv->state.loop_irq;
  3698. bttv_buffer_activate_video(btv, &btv->curr);
  3699. bttv_buffer_activate_vbi(btv, btv->cvbi);
  3700. bttv_set_dma(btv, 0);
  3701. spin_unlock_irqrestore(&btv->s_lock,flags);
  3702. return 0;
  3703. }
  3704. static struct pci_device_id bttv_pci_tbl[] = {
  3705. {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT848,
  3706. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  3707. {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT849,
  3708. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  3709. {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT878,
  3710. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  3711. {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT879,
  3712. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  3713. {0,}
  3714. };
  3715. MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
  3716. static struct pci_driver bttv_pci_driver = {
  3717. .name = "bttv",
  3718. .id_table = bttv_pci_tbl,
  3719. .probe = bttv_probe,
  3720. .remove = __devexit_p(bttv_remove),
  3721. .suspend = bttv_suspend,
  3722. .resume = bttv_resume,
  3723. };
  3724. static int bttv_init_module(void)
  3725. {
  3726. bttv_num = 0;
  3727. printk(KERN_INFO "bttv: driver version %d.%d.%d loaded\n",
  3728. (BTTV_VERSION_CODE >> 16) & 0xff,
  3729. (BTTV_VERSION_CODE >> 8) & 0xff,
  3730. BTTV_VERSION_CODE & 0xff);
  3731. #ifdef SNAPSHOT
  3732. printk(KERN_INFO "bttv: snapshot date %04d-%02d-%02d\n",
  3733. SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
  3734. #endif
  3735. if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
  3736. gbuffers = 2;
  3737. if (gbufsize < 0 || gbufsize > BTTV_MAX_FBUF)
  3738. gbufsize = BTTV_MAX_FBUF;
  3739. gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
  3740. if (bttv_verbose)
  3741. printk(KERN_INFO "bttv: using %d buffers with %dk (%d pages) each for capture\n",
  3742. gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
  3743. bttv_check_chipset();
  3744. bus_register(&bttv_sub_bus_type);
  3745. return pci_module_init(&bttv_pci_driver);
  3746. }
  3747. static void bttv_cleanup_module(void)
  3748. {
  3749. pci_unregister_driver(&bttv_pci_driver);
  3750. bus_unregister(&bttv_sub_bus_type);
  3751. return;
  3752. }
  3753. module_init(bttv_init_module);
  3754. module_exit(bttv_cleanup_module);
  3755. /*
  3756. * Local variables:
  3757. * c-basic-offset: 8
  3758. * End:
  3759. */