bttv-driver.c 105 KB

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