bttv-driver.c 104 KB

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