bttv-driver.c 116 KB

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