bttv-driver.c 106 KB

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