bttv-driver.c 106 KB

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