bttv-driver.c 106 KB

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