bttv-driver.c 106 KB

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