bttv-driver.c 106 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174
  1. /*
  2. $Id: bttv-driver.c,v 1.52 2005/08/04 00:55:16 mchehab Exp $
  3. bttv - Bt848 frame grabber driver
  4. Copyright (C) 1996,97,98 Ralph Metzler <rjkm@thp.uni-koeln.de>
  5. & Marcus Metzler <mocm@thp.uni-koeln.de>
  6. (c) 1999-2002 Gerd Knorr <kraxel@bytesex.org>
  7. some v4l2 code lines are taken from Justin's bttv2 driver which is
  8. (c) 2000 Justin Schoeman <justin@suntiger.ee.up.ac.za>
  9. This program is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2 of the License, or
  12. (at your option) any later version.
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <linux/init.h>
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/delay.h>
  25. #include <linux/errno.h>
  26. #include <linux/fs.h>
  27. #include <linux/kernel.h>
  28. #include <linux/sched.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/kdev_t.h>
  31. #include <linux/dma-mapping.h>
  32. #include <asm/io.h>
  33. #include <asm/byteorder.h>
  34. #include "bttvp.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. vprintk(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. vprintk(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. vprintk(".");
  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. vprintk(" ok\n");
  730. return;
  731. }
  732. }
  733. btv->pll.pll_current = -1;
  734. vprintk("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_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_VOODOOTV_FM:
  897. bttv_tda9880_setnorm(btv,norm);
  898. break;
  899. }
  900. return 0;
  901. }
  902. static void
  903. set_input(struct bttv *btv, unsigned int input)
  904. {
  905. unsigned long flags;
  906. btv->input = input;
  907. if (irq_iswitch) {
  908. spin_lock_irqsave(&btv->s_lock,flags);
  909. if (btv->curr.frame_irq) {
  910. /* active capture -> delayed input switch */
  911. btv->new_input = input;
  912. } else {
  913. video_mux(btv,input);
  914. }
  915. spin_unlock_irqrestore(&btv->s_lock,flags);
  916. } else {
  917. video_mux(btv,input);
  918. }
  919. audio_mux(btv,(input == bttv_tvcards[btv->c.type].tuner ?
  920. AUDIO_TUNER : AUDIO_EXTERN));
  921. set_tvnorm(btv,btv->tvnorm);
  922. i2c_vidiocschan(btv);
  923. }
  924. static void init_irqreg(struct bttv *btv)
  925. {
  926. /* clear status */
  927. btwrite(0xfffffUL, BT848_INT_STAT);
  928. if (bttv_tvcards[btv->c.type].no_video) {
  929. /* i2c only */
  930. btwrite(BT848_INT_I2CDONE,
  931. BT848_INT_MASK);
  932. } else {
  933. /* full video */
  934. btwrite((btv->triton1) |
  935. (btv->gpioirq ? BT848_INT_GPINT : 0) |
  936. BT848_INT_SCERR |
  937. (fdsr ? BT848_INT_FDSR : 0) |
  938. BT848_INT_RISCI|BT848_INT_OCERR|BT848_INT_VPRES|
  939. BT848_INT_FMTCHG|BT848_INT_HLOCK|
  940. BT848_INT_I2CDONE,
  941. BT848_INT_MASK);
  942. }
  943. }
  944. static void init_bt848(struct bttv *btv)
  945. {
  946. int val;
  947. if (bttv_tvcards[btv->c.type].no_video) {
  948. /* very basic init only */
  949. init_irqreg(btv);
  950. return;
  951. }
  952. btwrite(0x00, BT848_CAP_CTL);
  953. btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
  954. btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);
  955. /* set planar and packed mode trigger points and */
  956. /* set rising edge of inverted GPINTR pin as irq trigger */
  957. btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
  958. BT848_GPIO_DMA_CTL_PLTP1_16|
  959. BT848_GPIO_DMA_CTL_PLTP23_16|
  960. BT848_GPIO_DMA_CTL_GPINTC|
  961. BT848_GPIO_DMA_CTL_GPINTI,
  962. BT848_GPIO_DMA_CTL);
  963. val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
  964. btwrite(val, BT848_E_SCLOOP);
  965. btwrite(val, BT848_O_SCLOOP);
  966. btwrite(0x20, BT848_E_VSCALE_HI);
  967. btwrite(0x20, BT848_O_VSCALE_HI);
  968. btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
  969. BT848_ADC);
  970. btwrite(whitecrush_upper, BT848_WC_UP);
  971. btwrite(whitecrush_lower, BT848_WC_DOWN);
  972. if (btv->opt_lumafilter) {
  973. btwrite(0, BT848_E_CONTROL);
  974. btwrite(0, BT848_O_CONTROL);
  975. } else {
  976. btwrite(BT848_CONTROL_LDEC, BT848_E_CONTROL);
  977. btwrite(BT848_CONTROL_LDEC, BT848_O_CONTROL);
  978. }
  979. bt848_bright(btv, btv->bright);
  980. bt848_hue(btv, btv->hue);
  981. bt848_contrast(btv, btv->contrast);
  982. bt848_sat(btv, btv->saturation);
  983. /* interrupt */
  984. init_irqreg(btv);
  985. }
  986. static void bttv_reinit_bt848(struct bttv *btv)
  987. {
  988. unsigned long flags;
  989. if (bttv_verbose)
  990. printk(KERN_INFO "bttv%d: reset, reinitialize\n",btv->c.nr);
  991. spin_lock_irqsave(&btv->s_lock,flags);
  992. btv->errors=0;
  993. bttv_set_dma(btv,0);
  994. spin_unlock_irqrestore(&btv->s_lock,flags);
  995. init_bt848(btv);
  996. btv->pll.pll_current = -1;
  997. set_input(btv,btv->input);
  998. }
  999. static int get_control(struct bttv *btv, struct v4l2_control *c)
  1000. {
  1001. struct video_audio va;
  1002. int i;
  1003. for (i = 0; i < BTTV_CTLS; i++)
  1004. if (bttv_ctls[i].id == c->id)
  1005. break;
  1006. if (i == BTTV_CTLS)
  1007. return -EINVAL;
  1008. if (i >= 4 && i <= 8) {
  1009. memset(&va,0,sizeof(va));
  1010. bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
  1011. if (btv->audio_hook)
  1012. btv->audio_hook(btv,&va,0);
  1013. }
  1014. switch (c->id) {
  1015. case V4L2_CID_BRIGHTNESS:
  1016. c->value = btv->bright;
  1017. break;
  1018. case V4L2_CID_HUE:
  1019. c->value = btv->hue;
  1020. break;
  1021. case V4L2_CID_CONTRAST:
  1022. c->value = btv->contrast;
  1023. break;
  1024. case V4L2_CID_SATURATION:
  1025. c->value = btv->saturation;
  1026. break;
  1027. case V4L2_CID_AUDIO_MUTE:
  1028. c->value = (VIDEO_AUDIO_MUTE & va.flags) ? 1 : 0;
  1029. break;
  1030. case V4L2_CID_AUDIO_VOLUME:
  1031. c->value = va.volume;
  1032. break;
  1033. case V4L2_CID_AUDIO_BALANCE:
  1034. c->value = va.balance;
  1035. break;
  1036. case V4L2_CID_AUDIO_BASS:
  1037. c->value = va.bass;
  1038. break;
  1039. case V4L2_CID_AUDIO_TREBLE:
  1040. c->value = va.treble;
  1041. break;
  1042. case V4L2_CID_PRIVATE_CHROMA_AGC:
  1043. c->value = btv->opt_chroma_agc;
  1044. break;
  1045. case V4L2_CID_PRIVATE_COMBFILTER:
  1046. c->value = btv->opt_combfilter;
  1047. break;
  1048. case V4L2_CID_PRIVATE_LUMAFILTER:
  1049. c->value = btv->opt_lumafilter;
  1050. break;
  1051. case V4L2_CID_PRIVATE_AUTOMUTE:
  1052. c->value = btv->opt_automute;
  1053. break;
  1054. case V4L2_CID_PRIVATE_AGC_CRUSH:
  1055. c->value = btv->opt_adc_crush;
  1056. break;
  1057. case V4L2_CID_PRIVATE_VCR_HACK:
  1058. c->value = btv->opt_vcr_hack;
  1059. break;
  1060. case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
  1061. c->value = btv->opt_whitecrush_upper;
  1062. break;
  1063. case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
  1064. c->value = btv->opt_whitecrush_lower;
  1065. break;
  1066. case V4L2_CID_PRIVATE_UV_RATIO:
  1067. c->value = btv->opt_uv_ratio;
  1068. break;
  1069. case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
  1070. c->value = btv->opt_full_luma_range;
  1071. break;
  1072. case V4L2_CID_PRIVATE_CORING:
  1073. c->value = btv->opt_coring;
  1074. break;
  1075. default:
  1076. return -EINVAL;
  1077. }
  1078. return 0;
  1079. }
  1080. static int set_control(struct bttv *btv, struct v4l2_control *c)
  1081. {
  1082. struct video_audio va;
  1083. int i,val;
  1084. for (i = 0; i < BTTV_CTLS; i++)
  1085. if (bttv_ctls[i].id == c->id)
  1086. break;
  1087. if (i == BTTV_CTLS)
  1088. return -EINVAL;
  1089. if (i >= 4 && i <= 8) {
  1090. memset(&va,0,sizeof(va));
  1091. bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
  1092. if (btv->audio_hook)
  1093. btv->audio_hook(btv,&va,0);
  1094. }
  1095. switch (c->id) {
  1096. case V4L2_CID_BRIGHTNESS:
  1097. bt848_bright(btv,c->value);
  1098. break;
  1099. case V4L2_CID_HUE:
  1100. bt848_hue(btv,c->value);
  1101. break;
  1102. case V4L2_CID_CONTRAST:
  1103. bt848_contrast(btv,c->value);
  1104. break;
  1105. case V4L2_CID_SATURATION:
  1106. bt848_sat(btv,c->value);
  1107. break;
  1108. case V4L2_CID_AUDIO_MUTE:
  1109. if (c->value) {
  1110. va.flags |= VIDEO_AUDIO_MUTE;
  1111. audio_mux(btv, AUDIO_MUTE);
  1112. } else {
  1113. va.flags &= ~VIDEO_AUDIO_MUTE;
  1114. audio_mux(btv, AUDIO_UNMUTE);
  1115. }
  1116. break;
  1117. case V4L2_CID_AUDIO_VOLUME:
  1118. va.volume = c->value;
  1119. break;
  1120. case V4L2_CID_AUDIO_BALANCE:
  1121. va.balance = c->value;
  1122. break;
  1123. case V4L2_CID_AUDIO_BASS:
  1124. va.bass = c->value;
  1125. break;
  1126. case V4L2_CID_AUDIO_TREBLE:
  1127. va.treble = c->value;
  1128. break;
  1129. case V4L2_CID_PRIVATE_CHROMA_AGC:
  1130. btv->opt_chroma_agc = c->value;
  1131. val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
  1132. btwrite(val, BT848_E_SCLOOP);
  1133. btwrite(val, BT848_O_SCLOOP);
  1134. break;
  1135. case V4L2_CID_PRIVATE_COMBFILTER:
  1136. btv->opt_combfilter = c->value;
  1137. break;
  1138. case V4L2_CID_PRIVATE_LUMAFILTER:
  1139. btv->opt_lumafilter = c->value;
  1140. if (btv->opt_lumafilter) {
  1141. btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
  1142. btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
  1143. } else {
  1144. btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
  1145. btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
  1146. }
  1147. break;
  1148. case V4L2_CID_PRIVATE_AUTOMUTE:
  1149. btv->opt_automute = c->value;
  1150. break;
  1151. case V4L2_CID_PRIVATE_AGC_CRUSH:
  1152. btv->opt_adc_crush = c->value;
  1153. btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
  1154. BT848_ADC);
  1155. break;
  1156. case V4L2_CID_PRIVATE_VCR_HACK:
  1157. btv->opt_vcr_hack = c->value;
  1158. break;
  1159. case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
  1160. btv->opt_whitecrush_upper = c->value;
  1161. btwrite(c->value, BT848_WC_UP);
  1162. break;
  1163. case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
  1164. btv->opt_whitecrush_lower = c->value;
  1165. btwrite(c->value, BT848_WC_DOWN);
  1166. break;
  1167. case V4L2_CID_PRIVATE_UV_RATIO:
  1168. btv->opt_uv_ratio = c->value;
  1169. bt848_sat(btv, btv->saturation);
  1170. break;
  1171. case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
  1172. btv->opt_full_luma_range = c->value;
  1173. btaor((c->value<<7), ~BT848_OFORM_RANGE, BT848_OFORM);
  1174. break;
  1175. case V4L2_CID_PRIVATE_CORING:
  1176. btv->opt_coring = c->value;
  1177. btaor((c->value<<5), ~BT848_OFORM_CORE32, BT848_OFORM);
  1178. break;
  1179. default:
  1180. return -EINVAL;
  1181. }
  1182. if (i >= 4 && i <= 8) {
  1183. bttv_call_i2c_clients(btv, VIDIOCSAUDIO, &va);
  1184. if (btv->audio_hook)
  1185. btv->audio_hook(btv,&va,1);
  1186. }
  1187. return 0;
  1188. }
  1189. /* ----------------------------------------------------------------------- */
  1190. void bttv_gpio_tracking(struct bttv *btv, char *comment)
  1191. {
  1192. unsigned int outbits, data;
  1193. outbits = btread(BT848_GPIO_OUT_EN);
  1194. data = btread(BT848_GPIO_DATA);
  1195. printk(KERN_DEBUG "bttv%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
  1196. btv->c.nr,outbits,data & outbits, data & ~outbits, comment);
  1197. }
  1198. static void bttv_field_count(struct bttv *btv)
  1199. {
  1200. int need_count = 0;
  1201. if (btv->users)
  1202. need_count++;
  1203. if (need_count) {
  1204. /* start field counter */
  1205. btor(BT848_INT_VSYNC,BT848_INT_MASK);
  1206. } else {
  1207. /* stop field counter */
  1208. btand(~BT848_INT_VSYNC,BT848_INT_MASK);
  1209. btv->field_count = 0;
  1210. }
  1211. }
  1212. static const struct bttv_format*
  1213. format_by_palette(int palette)
  1214. {
  1215. unsigned int i;
  1216. for (i = 0; i < BTTV_FORMATS; i++) {
  1217. if (-1 == bttv_formats[i].palette)
  1218. continue;
  1219. if (bttv_formats[i].palette == palette)
  1220. return bttv_formats+i;
  1221. }
  1222. return NULL;
  1223. }
  1224. static const struct bttv_format*
  1225. format_by_fourcc(int fourcc)
  1226. {
  1227. unsigned int i;
  1228. for (i = 0; i < BTTV_FORMATS; i++) {
  1229. if (-1 == bttv_formats[i].fourcc)
  1230. continue;
  1231. if (bttv_formats[i].fourcc == fourcc)
  1232. return bttv_formats+i;
  1233. }
  1234. return NULL;
  1235. }
  1236. /* ----------------------------------------------------------------------- */
  1237. /* misc helpers */
  1238. static int
  1239. bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
  1240. struct bttv_buffer *new)
  1241. {
  1242. struct bttv_buffer *old;
  1243. unsigned long flags;
  1244. int retval = 0;
  1245. dprintk("switch_overlay: enter [new=%p]\n",new);
  1246. if (new)
  1247. new->vb.state = STATE_DONE;
  1248. spin_lock_irqsave(&btv->s_lock,flags);
  1249. old = btv->screen;
  1250. btv->screen = new;
  1251. btv->loop_irq |= 1;
  1252. bttv_set_dma(btv, 0x03);
  1253. spin_unlock_irqrestore(&btv->s_lock,flags);
  1254. if (NULL == new)
  1255. free_btres(btv,fh,RESOURCE_OVERLAY);
  1256. if (NULL != old) {
  1257. dprintk("switch_overlay: old=%p state is %d\n",old,old->vb.state);
  1258. bttv_dma_free(btv, old);
  1259. kfree(old);
  1260. }
  1261. dprintk("switch_overlay: done\n");
  1262. return retval;
  1263. }
  1264. /* ----------------------------------------------------------------------- */
  1265. /* video4linux (1) interface */
  1266. static int bttv_prepare_buffer(struct bttv *btv, struct bttv_buffer *buf,
  1267. const struct bttv_format *fmt,
  1268. unsigned int width, unsigned int height,
  1269. enum v4l2_field field)
  1270. {
  1271. int redo_dma_risc = 0;
  1272. int rc;
  1273. /* check settings */
  1274. if (NULL == fmt)
  1275. return -EINVAL;
  1276. if (fmt->btformat == BT848_COLOR_FMT_RAW) {
  1277. width = RAW_BPL;
  1278. height = RAW_LINES*2;
  1279. if (width*height > buf->vb.bsize)
  1280. return -EINVAL;
  1281. buf->vb.size = buf->vb.bsize;
  1282. } else {
  1283. if (width < 48 ||
  1284. height < 32 ||
  1285. width > bttv_tvnorms[btv->tvnorm].swidth ||
  1286. height > bttv_tvnorms[btv->tvnorm].sheight)
  1287. return -EINVAL;
  1288. buf->vb.size = (width * height * fmt->depth) >> 3;
  1289. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  1290. return -EINVAL;
  1291. }
  1292. /* alloc + fill struct bttv_buffer (if changed) */
  1293. if (buf->vb.width != width || buf->vb.height != height ||
  1294. buf->vb.field != field ||
  1295. buf->tvnorm != btv->tvnorm || buf->fmt != fmt) {
  1296. buf->vb.width = width;
  1297. buf->vb.height = height;
  1298. buf->vb.field = field;
  1299. buf->tvnorm = btv->tvnorm;
  1300. buf->fmt = fmt;
  1301. redo_dma_risc = 1;
  1302. }
  1303. /* alloc risc memory */
  1304. if (STATE_NEEDS_INIT == buf->vb.state) {
  1305. redo_dma_risc = 1;
  1306. if (0 != (rc = videobuf_iolock(btv->c.pci,&buf->vb,&btv->fbuf)))
  1307. goto fail;
  1308. }
  1309. if (redo_dma_risc)
  1310. if (0 != (rc = bttv_buffer_risc(btv,buf)))
  1311. goto fail;
  1312. buf->vb.state = STATE_PREPARED;
  1313. return 0;
  1314. fail:
  1315. bttv_dma_free(btv,buf);
  1316. return rc;
  1317. }
  1318. static int
  1319. buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
  1320. {
  1321. struct bttv_fh *fh = q->priv_data;
  1322. *size = fh->fmt->depth*fh->width*fh->height >> 3;
  1323. if (0 == *count)
  1324. *count = gbuffers;
  1325. while (*size * *count > gbuffers * gbufsize)
  1326. (*count)--;
  1327. return 0;
  1328. }
  1329. static int
  1330. buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
  1331. enum v4l2_field field)
  1332. {
  1333. struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
  1334. struct bttv_fh *fh = q->priv_data;
  1335. return bttv_prepare_buffer(fh->btv, buf, fh->fmt,
  1336. fh->width, fh->height, field);
  1337. }
  1338. static void
  1339. buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
  1340. {
  1341. struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
  1342. struct bttv_fh *fh = q->priv_data;
  1343. struct bttv *btv = fh->btv;
  1344. buf->vb.state = STATE_QUEUED;
  1345. list_add_tail(&buf->vb.queue,&btv->capture);
  1346. if (!btv->curr.frame_irq) {
  1347. btv->loop_irq |= 1;
  1348. bttv_set_dma(btv, 0x03);
  1349. }
  1350. }
  1351. static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
  1352. {
  1353. struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
  1354. struct bttv_fh *fh = q->priv_data;
  1355. bttv_dma_free(fh->btv,buf);
  1356. }
  1357. static struct videobuf_queue_ops bttv_video_qops = {
  1358. .buf_setup = buffer_setup,
  1359. .buf_prepare = buffer_prepare,
  1360. .buf_queue = buffer_queue,
  1361. .buf_release = buffer_release,
  1362. };
  1363. static const char *v4l1_ioctls[] = {
  1364. "?", "CGAP", "GCHAN", "SCHAN", "GTUNER", "STUNER", "GPICT", "SPICT",
  1365. "CCAPTURE", "GWIN", "SWIN", "GFBUF", "SFBUF", "KEY", "GFREQ",
  1366. "SFREQ", "GAUDIO", "SAUDIO", "SYNC", "MCAPTURE", "GMBUF", "GUNIT",
  1367. "GCAPTURE", "SCAPTURE", "SPLAYMODE", "SWRITEMODE", "GPLAYINFO",
  1368. "SMICROCODE", "GVBIFMT", "SVBIFMT" };
  1369. #define V4L1_IOCTLS ARRAY_SIZE(v4l1_ioctls)
  1370. static int bttv_common_ioctls(struct bttv *btv, unsigned int cmd, void *arg)
  1371. {
  1372. switch (cmd) {
  1373. case BTTV_VERSION:
  1374. return BTTV_VERSION_CODE;
  1375. /* *** v4l1 *** ************************************************ */
  1376. case VIDIOCGFREQ:
  1377. {
  1378. unsigned long *freq = arg;
  1379. *freq = btv->freq;
  1380. return 0;
  1381. }
  1382. case VIDIOCSFREQ:
  1383. {
  1384. unsigned long *freq = arg;
  1385. down(&btv->lock);
  1386. btv->freq=*freq;
  1387. bttv_call_i2c_clients(btv,VIDIOCSFREQ,freq);
  1388. if (btv->has_matchbox && btv->radio_user)
  1389. tea5757_set_freq(btv,*freq);
  1390. up(&btv->lock);
  1391. return 0;
  1392. }
  1393. case VIDIOCGTUNER:
  1394. {
  1395. struct video_tuner *v = arg;
  1396. if (UNSET == bttv_tvcards[btv->c.type].tuner)
  1397. return -EINVAL;
  1398. if (v->tuner) /* Only tuner 0 */
  1399. return -EINVAL;
  1400. strcpy(v->name, "Television");
  1401. v->rangelow = 0;
  1402. v->rangehigh = 0x7FFFFFFF;
  1403. v->flags = VIDEO_TUNER_PAL|VIDEO_TUNER_NTSC|VIDEO_TUNER_SECAM;
  1404. v->mode = btv->tvnorm;
  1405. v->signal = (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC) ? 0xFFFF : 0;
  1406. bttv_call_i2c_clients(btv,cmd,v);
  1407. return 0;
  1408. }
  1409. case VIDIOCSTUNER:
  1410. {
  1411. struct video_tuner *v = arg;
  1412. if (v->tuner) /* Only tuner 0 */
  1413. return -EINVAL;
  1414. if (v->mode >= BTTV_TVNORMS)
  1415. return -EINVAL;
  1416. down(&btv->lock);
  1417. set_tvnorm(btv,v->mode);
  1418. bttv_call_i2c_clients(btv,cmd,v);
  1419. up(&btv->lock);
  1420. return 0;
  1421. }
  1422. case VIDIOCGCHAN:
  1423. {
  1424. struct video_channel *v = arg;
  1425. unsigned int channel = v->channel;
  1426. if (channel >= bttv_tvcards[btv->c.type].video_inputs)
  1427. return -EINVAL;
  1428. v->tuners=0;
  1429. v->flags = VIDEO_VC_AUDIO;
  1430. v->type = VIDEO_TYPE_CAMERA;
  1431. v->norm = btv->tvnorm;
  1432. if (channel == bttv_tvcards[btv->c.type].tuner) {
  1433. strcpy(v->name,"Television");
  1434. v->flags|=VIDEO_VC_TUNER;
  1435. v->type=VIDEO_TYPE_TV;
  1436. v->tuners=1;
  1437. } else if (channel == btv->svhs) {
  1438. strcpy(v->name,"S-Video");
  1439. } else {
  1440. sprintf(v->name,"Composite%d",channel);
  1441. }
  1442. return 0;
  1443. }
  1444. case VIDIOCSCHAN:
  1445. {
  1446. struct video_channel *v = arg;
  1447. unsigned int channel = v->channel;
  1448. if (channel >= bttv_tvcards[btv->c.type].video_inputs)
  1449. return -EINVAL;
  1450. if (v->norm >= BTTV_TVNORMS)
  1451. return -EINVAL;
  1452. down(&btv->lock);
  1453. if (channel == btv->input &&
  1454. v->norm == btv->tvnorm) {
  1455. /* nothing to do */
  1456. up(&btv->lock);
  1457. return 0;
  1458. }
  1459. btv->tvnorm = v->norm;
  1460. set_input(btv,v->channel);
  1461. up(&btv->lock);
  1462. return 0;
  1463. }
  1464. case VIDIOCGAUDIO:
  1465. {
  1466. struct video_audio *v = arg;
  1467. memset(v,0,sizeof(*v));
  1468. strcpy(v->name,"Television");
  1469. v->flags |= VIDEO_AUDIO_MUTABLE;
  1470. v->mode = VIDEO_SOUND_MONO;
  1471. down(&btv->lock);
  1472. bttv_call_i2c_clients(btv,cmd,v);
  1473. /* card specific hooks */
  1474. if (btv->audio_hook)
  1475. btv->audio_hook(btv,v,0);
  1476. up(&btv->lock);
  1477. return 0;
  1478. }
  1479. case VIDIOCSAUDIO:
  1480. {
  1481. struct video_audio *v = arg;
  1482. unsigned int audio = v->audio;
  1483. if (audio >= bttv_tvcards[btv->c.type].audio_inputs)
  1484. return -EINVAL;
  1485. down(&btv->lock);
  1486. audio_mux(btv, (v->flags&VIDEO_AUDIO_MUTE) ? AUDIO_MUTE : AUDIO_UNMUTE);
  1487. bttv_call_i2c_clients(btv,cmd,v);
  1488. /* card specific hooks */
  1489. if (btv->audio_hook)
  1490. btv->audio_hook(btv,v,1);
  1491. up(&btv->lock);
  1492. return 0;
  1493. }
  1494. /* *** v4l2 *** ************************************************ */
  1495. case VIDIOC_ENUMSTD:
  1496. {
  1497. struct v4l2_standard *e = arg;
  1498. unsigned int index = e->index;
  1499. if (index >= BTTV_TVNORMS)
  1500. return -EINVAL;
  1501. v4l2_video_std_construct(e, bttv_tvnorms[e->index].v4l2_id,
  1502. bttv_tvnorms[e->index].name);
  1503. e->index = index;
  1504. return 0;
  1505. }
  1506. case VIDIOC_G_STD:
  1507. {
  1508. v4l2_std_id *id = arg;
  1509. *id = bttv_tvnorms[btv->tvnorm].v4l2_id;
  1510. return 0;
  1511. }
  1512. case VIDIOC_S_STD:
  1513. {
  1514. v4l2_std_id *id = arg;
  1515. unsigned int i;
  1516. for (i = 0; i < BTTV_TVNORMS; i++)
  1517. if (*id & bttv_tvnorms[i].v4l2_id)
  1518. break;
  1519. if (i == BTTV_TVNORMS)
  1520. return -EINVAL;
  1521. down(&btv->lock);
  1522. set_tvnorm(btv,i);
  1523. i2c_vidiocschan(btv);
  1524. up(&btv->lock);
  1525. return 0;
  1526. }
  1527. case VIDIOC_QUERYSTD:
  1528. {
  1529. v4l2_std_id *id = arg;
  1530. if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
  1531. *id = V4L2_STD_625_50;
  1532. else
  1533. *id = V4L2_STD_525_60;
  1534. return 0;
  1535. }
  1536. case VIDIOC_ENUMINPUT:
  1537. {
  1538. struct v4l2_input *i = arg;
  1539. unsigned int n;
  1540. n = i->index;
  1541. if (n >= bttv_tvcards[btv->c.type].video_inputs)
  1542. return -EINVAL;
  1543. memset(i,0,sizeof(*i));
  1544. i->index = n;
  1545. i->type = V4L2_INPUT_TYPE_CAMERA;
  1546. i->audioset = 1;
  1547. if (i->index == bttv_tvcards[btv->c.type].tuner) {
  1548. sprintf(i->name, "Television");
  1549. i->type = V4L2_INPUT_TYPE_TUNER;
  1550. i->tuner = 0;
  1551. } else if (i->index == btv->svhs) {
  1552. sprintf(i->name, "S-Video");
  1553. } else {
  1554. sprintf(i->name,"Composite%d",i->index);
  1555. }
  1556. if (i->index == btv->input) {
  1557. __u32 dstatus = btread(BT848_DSTATUS);
  1558. if (0 == (dstatus & BT848_DSTATUS_PRES))
  1559. i->status |= V4L2_IN_ST_NO_SIGNAL;
  1560. if (0 == (dstatus & BT848_DSTATUS_HLOC))
  1561. i->status |= V4L2_IN_ST_NO_H_LOCK;
  1562. }
  1563. for (n = 0; n < BTTV_TVNORMS; n++)
  1564. i->std |= bttv_tvnorms[n].v4l2_id;
  1565. return 0;
  1566. }
  1567. case VIDIOC_G_INPUT:
  1568. {
  1569. int *i = arg;
  1570. *i = btv->input;
  1571. return 0;
  1572. }
  1573. case VIDIOC_S_INPUT:
  1574. {
  1575. unsigned int *i = arg;
  1576. if (*i > bttv_tvcards[btv->c.type].video_inputs)
  1577. return -EINVAL;
  1578. down(&btv->lock);
  1579. set_input(btv,*i);
  1580. up(&btv->lock);
  1581. return 0;
  1582. }
  1583. case VIDIOC_G_TUNER:
  1584. {
  1585. struct v4l2_tuner *t = arg;
  1586. if (UNSET == bttv_tvcards[btv->c.type].tuner)
  1587. return -EINVAL;
  1588. if (0 != t->index)
  1589. return -EINVAL;
  1590. down(&btv->lock);
  1591. memset(t,0,sizeof(*t));
  1592. strcpy(t->name, "Television");
  1593. t->type = V4L2_TUNER_ANALOG_TV;
  1594. t->rangehigh = 0xffffffffUL;
  1595. t->capability = V4L2_TUNER_CAP_NORM;
  1596. t->rxsubchans = V4L2_TUNER_SUB_MONO;
  1597. if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
  1598. t->signal = 0xffff;
  1599. {
  1600. /* Hmmm ... */
  1601. struct video_audio va;
  1602. memset(&va, 0, sizeof(struct video_audio));
  1603. bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
  1604. if (btv->audio_hook)
  1605. btv->audio_hook(btv,&va,0);
  1606. if(va.mode & VIDEO_SOUND_STEREO) {
  1607. t->audmode = V4L2_TUNER_MODE_STEREO;
  1608. t->rxsubchans |= V4L2_TUNER_SUB_STEREO;
  1609. }
  1610. if(va.mode & VIDEO_SOUND_LANG1) {
  1611. t->audmode = V4L2_TUNER_MODE_LANG1;
  1612. t->rxsubchans = V4L2_TUNER_SUB_LANG1
  1613. | V4L2_TUNER_SUB_LANG2;
  1614. }
  1615. }
  1616. /* FIXME: fill capability+audmode */
  1617. up(&btv->lock);
  1618. return 0;
  1619. }
  1620. case VIDIOC_S_TUNER:
  1621. {
  1622. struct v4l2_tuner *t = arg;
  1623. if (UNSET == bttv_tvcards[btv->c.type].tuner)
  1624. return -EINVAL;
  1625. if (0 != t->index)
  1626. return -EINVAL;
  1627. down(&btv->lock);
  1628. {
  1629. struct video_audio va;
  1630. memset(&va, 0, sizeof(struct video_audio));
  1631. bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
  1632. if (t->audmode == V4L2_TUNER_MODE_MONO)
  1633. va.mode = VIDEO_SOUND_MONO;
  1634. else if (t->audmode == V4L2_TUNER_MODE_STEREO)
  1635. va.mode = VIDEO_SOUND_STEREO;
  1636. else if (t->audmode == V4L2_TUNER_MODE_LANG1)
  1637. va.mode = VIDEO_SOUND_LANG1;
  1638. else if (t->audmode == V4L2_TUNER_MODE_LANG2)
  1639. va.mode = VIDEO_SOUND_LANG2;
  1640. bttv_call_i2c_clients(btv, VIDIOCSAUDIO, &va);
  1641. if (btv->audio_hook)
  1642. btv->audio_hook(btv,&va,1);
  1643. }
  1644. up(&btv->lock);
  1645. return 0;
  1646. }
  1647. case VIDIOC_G_FREQUENCY:
  1648. {
  1649. struct v4l2_frequency *f = arg;
  1650. memset(f,0,sizeof(*f));
  1651. f->type = V4L2_TUNER_ANALOG_TV;
  1652. f->frequency = btv->freq;
  1653. return 0;
  1654. }
  1655. case VIDIOC_S_FREQUENCY:
  1656. {
  1657. struct v4l2_frequency *f = arg;
  1658. if (unlikely(f->tuner != 0))
  1659. return -EINVAL;
  1660. if (unlikely (f->type != V4L2_TUNER_ANALOG_TV))
  1661. return -EINVAL;
  1662. down(&btv->lock);
  1663. btv->freq = f->frequency;
  1664. bttv_call_i2c_clients(btv,VIDIOCSFREQ,&btv->freq);
  1665. if (btv->has_matchbox && btv->radio_user)
  1666. tea5757_set_freq(btv,btv->freq);
  1667. up(&btv->lock);
  1668. return 0;
  1669. }
  1670. default:
  1671. return -ENOIOCTLCMD;
  1672. }
  1673. return 0;
  1674. }
  1675. static int verify_window(const struct bttv_tvnorm *tvn,
  1676. struct v4l2_window *win, int fixup)
  1677. {
  1678. enum v4l2_field field;
  1679. int maxw, maxh;
  1680. if (win->w.width < 48 || win->w.height < 32)
  1681. return -EINVAL;
  1682. if (win->clipcount > 2048)
  1683. return -EINVAL;
  1684. field = win->field;
  1685. maxw = tvn->swidth;
  1686. maxh = tvn->sheight;
  1687. if (V4L2_FIELD_ANY == field) {
  1688. field = (win->w.height > maxh/2)
  1689. ? V4L2_FIELD_INTERLACED
  1690. : V4L2_FIELD_TOP;
  1691. }
  1692. switch (field) {
  1693. case V4L2_FIELD_TOP:
  1694. case V4L2_FIELD_BOTTOM:
  1695. maxh = maxh / 2;
  1696. break;
  1697. case V4L2_FIELD_INTERLACED:
  1698. break;
  1699. default:
  1700. return -EINVAL;
  1701. }
  1702. if (!fixup && (win->w.width > maxw || win->w.height > maxh))
  1703. return -EINVAL;
  1704. if (win->w.width > maxw)
  1705. win->w.width = maxw;
  1706. if (win->w.height > maxh)
  1707. win->w.height = maxh;
  1708. win->field = field;
  1709. return 0;
  1710. }
  1711. static int setup_window(struct bttv_fh *fh, struct bttv *btv,
  1712. struct v4l2_window *win, int fixup)
  1713. {
  1714. struct v4l2_clip *clips = NULL;
  1715. int n,size,retval = 0;
  1716. if (NULL == fh->ovfmt)
  1717. return -EINVAL;
  1718. if (!(fh->ovfmt->flags & FORMAT_FLAGS_PACKED))
  1719. return -EINVAL;
  1720. retval = verify_window(&bttv_tvnorms[btv->tvnorm],win,fixup);
  1721. if (0 != retval)
  1722. return retval;
  1723. /* copy clips -- luckily v4l1 + v4l2 are binary
  1724. compatible here ...*/
  1725. n = win->clipcount;
  1726. size = sizeof(*clips)*(n+4);
  1727. clips = kmalloc(size,GFP_KERNEL);
  1728. if (NULL == clips)
  1729. return -ENOMEM;
  1730. if (n > 0) {
  1731. if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
  1732. kfree(clips);
  1733. return -EFAULT;
  1734. }
  1735. }
  1736. /* clip against screen */
  1737. if (NULL != btv->fbuf.base)
  1738. n = btcx_screen_clips(btv->fbuf.fmt.width, btv->fbuf.fmt.height,
  1739. &win->w, clips, n);
  1740. btcx_sort_clips(clips,n);
  1741. /* 4-byte alignments */
  1742. switch (fh->ovfmt->depth) {
  1743. case 8:
  1744. case 24:
  1745. btcx_align(&win->w, clips, n, 3);
  1746. break;
  1747. case 16:
  1748. btcx_align(&win->w, clips, n, 1);
  1749. break;
  1750. case 32:
  1751. /* no alignment fixups needed */
  1752. break;
  1753. default:
  1754. BUG();
  1755. }
  1756. down(&fh->cap.lock);
  1757. if (fh->ov.clips)
  1758. kfree(fh->ov.clips);
  1759. fh->ov.clips = clips;
  1760. fh->ov.nclips = n;
  1761. fh->ov.w = win->w;
  1762. fh->ov.field = win->field;
  1763. fh->ov.setup_ok = 1;
  1764. btv->init.ov.w.width = win->w.width;
  1765. btv->init.ov.w.height = win->w.height;
  1766. btv->init.ov.field = win->field;
  1767. /* update overlay if needed */
  1768. retval = 0;
  1769. if (check_btres(fh, RESOURCE_OVERLAY)) {
  1770. struct bttv_buffer *new;
  1771. new = videobuf_alloc(sizeof(*new));
  1772. bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
  1773. retval = bttv_switch_overlay(btv,fh,new);
  1774. }
  1775. up(&fh->cap.lock);
  1776. return retval;
  1777. }
  1778. /* ----------------------------------------------------------------------- */
  1779. static struct videobuf_queue* bttv_queue(struct bttv_fh *fh)
  1780. {
  1781. struct videobuf_queue* q = NULL;
  1782. switch (fh->type) {
  1783. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  1784. q = &fh->cap;
  1785. break;
  1786. case V4L2_BUF_TYPE_VBI_CAPTURE:
  1787. q = &fh->vbi;
  1788. break;
  1789. default:
  1790. BUG();
  1791. }
  1792. return q;
  1793. }
  1794. static int bttv_resource(struct bttv_fh *fh)
  1795. {
  1796. int res = 0;
  1797. switch (fh->type) {
  1798. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  1799. res = RESOURCE_VIDEO;
  1800. break;
  1801. case V4L2_BUF_TYPE_VBI_CAPTURE:
  1802. res = RESOURCE_VBI;
  1803. break;
  1804. default:
  1805. BUG();
  1806. }
  1807. return res;
  1808. }
  1809. static int bttv_switch_type(struct bttv_fh *fh, enum v4l2_buf_type type)
  1810. {
  1811. struct videobuf_queue *q = bttv_queue(fh);
  1812. int res = bttv_resource(fh);
  1813. if (check_btres(fh,res))
  1814. return -EBUSY;
  1815. if (videobuf_queue_is_busy(q))
  1816. return -EBUSY;
  1817. fh->type = type;
  1818. return 0;
  1819. }
  1820. static int bttv_g_fmt(struct bttv_fh *fh, struct v4l2_format *f)
  1821. {
  1822. switch (f->type) {
  1823. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  1824. memset(&f->fmt.pix,0,sizeof(struct v4l2_pix_format));
  1825. f->fmt.pix.width = fh->width;
  1826. f->fmt.pix.height = fh->height;
  1827. f->fmt.pix.field = fh->cap.field;
  1828. f->fmt.pix.pixelformat = fh->fmt->fourcc;
  1829. f->fmt.pix.bytesperline =
  1830. (f->fmt.pix.width * fh->fmt->depth) >> 3;
  1831. f->fmt.pix.sizeimage =
  1832. f->fmt.pix.height * f->fmt.pix.bytesperline;
  1833. return 0;
  1834. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  1835. memset(&f->fmt.win,0,sizeof(struct v4l2_window));
  1836. f->fmt.win.w = fh->ov.w;
  1837. f->fmt.win.field = fh->ov.field;
  1838. return 0;
  1839. case V4L2_BUF_TYPE_VBI_CAPTURE:
  1840. bttv_vbi_get_fmt(fh,f);
  1841. return 0;
  1842. default:
  1843. return -EINVAL;
  1844. }
  1845. }
  1846. static int bttv_try_fmt(struct bttv_fh *fh, struct bttv *btv,
  1847. struct v4l2_format *f)
  1848. {
  1849. switch (f->type) {
  1850. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  1851. {
  1852. const struct bttv_format *fmt;
  1853. enum v4l2_field field;
  1854. unsigned int maxw,maxh;
  1855. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  1856. if (NULL == fmt)
  1857. return -EINVAL;
  1858. /* fixup format */
  1859. maxw = bttv_tvnorms[btv->tvnorm].swidth;
  1860. maxh = bttv_tvnorms[btv->tvnorm].sheight;
  1861. field = f->fmt.pix.field;
  1862. if (V4L2_FIELD_ANY == field)
  1863. field = (f->fmt.pix.height > maxh/2)
  1864. ? V4L2_FIELD_INTERLACED
  1865. : V4L2_FIELD_BOTTOM;
  1866. if (V4L2_FIELD_SEQ_BT == field)
  1867. field = V4L2_FIELD_SEQ_TB;
  1868. switch (field) {
  1869. case V4L2_FIELD_TOP:
  1870. case V4L2_FIELD_BOTTOM:
  1871. case V4L2_FIELD_ALTERNATE:
  1872. maxh = maxh/2;
  1873. break;
  1874. case V4L2_FIELD_INTERLACED:
  1875. break;
  1876. case V4L2_FIELD_SEQ_TB:
  1877. if (fmt->flags & FORMAT_FLAGS_PLANAR)
  1878. return -EINVAL;
  1879. break;
  1880. default:
  1881. return -EINVAL;
  1882. }
  1883. /* update data for the application */
  1884. f->fmt.pix.field = field;
  1885. if (f->fmt.pix.width < 48)
  1886. f->fmt.pix.width = 48;
  1887. if (f->fmt.pix.height < 32)
  1888. f->fmt.pix.height = 32;
  1889. if (f->fmt.pix.width > maxw)
  1890. f->fmt.pix.width = maxw;
  1891. if (f->fmt.pix.height > maxh)
  1892. f->fmt.pix.height = maxh;
  1893. f->fmt.pix.width &= ~0x03;
  1894. f->fmt.pix.bytesperline =
  1895. (f->fmt.pix.width * fmt->depth) >> 3;
  1896. f->fmt.pix.sizeimage =
  1897. f->fmt.pix.height * f->fmt.pix.bytesperline;
  1898. return 0;
  1899. }
  1900. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  1901. return verify_window(&bttv_tvnorms[btv->tvnorm],
  1902. &f->fmt.win, 1);
  1903. case V4L2_BUF_TYPE_VBI_CAPTURE:
  1904. bttv_vbi_try_fmt(fh,f);
  1905. return 0;
  1906. default:
  1907. return -EINVAL;
  1908. }
  1909. }
  1910. static int bttv_s_fmt(struct bttv_fh *fh, struct bttv *btv,
  1911. struct v4l2_format *f)
  1912. {
  1913. int retval;
  1914. switch (f->type) {
  1915. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  1916. {
  1917. const struct bttv_format *fmt;
  1918. retval = bttv_switch_type(fh,f->type);
  1919. if (0 != retval)
  1920. return retval;
  1921. retval = bttv_try_fmt(fh,btv,f);
  1922. if (0 != retval)
  1923. return retval;
  1924. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  1925. /* update our state informations */
  1926. down(&fh->cap.lock);
  1927. fh->fmt = fmt;
  1928. fh->cap.field = f->fmt.pix.field;
  1929. fh->cap.last = V4L2_FIELD_NONE;
  1930. fh->width = f->fmt.pix.width;
  1931. fh->height = f->fmt.pix.height;
  1932. btv->init.fmt = fmt;
  1933. btv->init.width = f->fmt.pix.width;
  1934. btv->init.height = f->fmt.pix.height;
  1935. up(&fh->cap.lock);
  1936. return 0;
  1937. }
  1938. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  1939. if (no_overlay > 0) {
  1940. printk ("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
  1941. return -EINVAL;
  1942. }
  1943. return setup_window(fh, btv, &f->fmt.win, 1);
  1944. case V4L2_BUF_TYPE_VBI_CAPTURE:
  1945. retval = bttv_switch_type(fh,f->type);
  1946. if (0 != retval)
  1947. return retval;
  1948. if (locked_btres(fh->btv, RESOURCE_VBI))
  1949. return -EBUSY;
  1950. bttv_vbi_try_fmt(fh,f);
  1951. bttv_vbi_setlines(fh,btv,f->fmt.vbi.count[0]);
  1952. bttv_vbi_get_fmt(fh,f);
  1953. return 0;
  1954. default:
  1955. return -EINVAL;
  1956. }
  1957. }
  1958. static int bttv_do_ioctl(struct inode *inode, struct file *file,
  1959. unsigned int cmd, void *arg)
  1960. {
  1961. struct bttv_fh *fh = file->private_data;
  1962. struct bttv *btv = fh->btv;
  1963. unsigned long flags;
  1964. int retval = 0;
  1965. if (bttv_debug > 1) {
  1966. switch (_IOC_TYPE(cmd)) {
  1967. case 'v':
  1968. printk("bttv%d: ioctl 0x%x (v4l1, VIDIOC%s)\n",
  1969. btv->c.nr, cmd, (_IOC_NR(cmd) < V4L1_IOCTLS) ?
  1970. v4l1_ioctls[_IOC_NR(cmd)] : "???");
  1971. break;
  1972. case 'V':
  1973. printk("bttv%d: ioctl 0x%x (v4l2, %s)\n",
  1974. btv->c.nr, cmd, v4l2_ioctl_names[_IOC_NR(cmd)]);
  1975. break;
  1976. default:
  1977. printk("bttv%d: ioctl 0x%x (???)\n",
  1978. btv->c.nr, cmd);
  1979. }
  1980. }
  1981. if (btv->errors)
  1982. bttv_reinit_bt848(btv);
  1983. switch (cmd) {
  1984. case VIDIOCSFREQ:
  1985. case VIDIOCSTUNER:
  1986. case VIDIOCSCHAN:
  1987. case VIDIOC_S_CTRL:
  1988. case VIDIOC_S_STD:
  1989. case VIDIOC_S_INPUT:
  1990. case VIDIOC_S_TUNER:
  1991. case VIDIOC_S_FREQUENCY:
  1992. retval = v4l2_prio_check(&btv->prio,&fh->prio);
  1993. if (0 != retval)
  1994. return retval;
  1995. };
  1996. switch (cmd) {
  1997. /* *** v4l1 *** ************************************************ */
  1998. case VIDIOCGCAP:
  1999. {
  2000. struct video_capability *cap = arg;
  2001. memset(cap,0,sizeof(*cap));
  2002. strcpy(cap->name,btv->video_dev->name);
  2003. if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
  2004. /* vbi */
  2005. cap->type = VID_TYPE_TUNER|VID_TYPE_TELETEXT;
  2006. } else {
  2007. /* others */
  2008. cap->type = VID_TYPE_CAPTURE|
  2009. VID_TYPE_TUNER|
  2010. VID_TYPE_CLIPPING|
  2011. VID_TYPE_SCALES;
  2012. if (no_overlay <= 0)
  2013. cap->type |= VID_TYPE_OVERLAY;
  2014. cap->maxwidth = bttv_tvnorms[btv->tvnorm].swidth;
  2015. cap->maxheight = bttv_tvnorms[btv->tvnorm].sheight;
  2016. cap->minwidth = 48;
  2017. cap->minheight = 32;
  2018. }
  2019. cap->channels = bttv_tvcards[btv->c.type].video_inputs;
  2020. cap->audios = bttv_tvcards[btv->c.type].audio_inputs;
  2021. return 0;
  2022. }
  2023. case VIDIOCGPICT:
  2024. {
  2025. struct video_picture *pic = arg;
  2026. memset(pic,0,sizeof(*pic));
  2027. pic->brightness = btv->bright;
  2028. pic->contrast = btv->contrast;
  2029. pic->hue = btv->hue;
  2030. pic->colour = btv->saturation;
  2031. if (fh->fmt) {
  2032. pic->depth = fh->fmt->depth;
  2033. pic->palette = fh->fmt->palette;
  2034. }
  2035. return 0;
  2036. }
  2037. case VIDIOCSPICT:
  2038. {
  2039. struct video_picture *pic = arg;
  2040. const struct bttv_format *fmt;
  2041. fmt = format_by_palette(pic->palette);
  2042. if (NULL == fmt)
  2043. return -EINVAL;
  2044. down(&fh->cap.lock);
  2045. if (fmt->depth != pic->depth) {
  2046. retval = -EINVAL;
  2047. goto fh_unlock_and_return;
  2048. }
  2049. fh->ovfmt = fmt;
  2050. fh->fmt = fmt;
  2051. btv->init.ovfmt = fmt;
  2052. btv->init.fmt = fmt;
  2053. if (bigendian) {
  2054. /* dirty hack time: swap bytes for overlay if the
  2055. display adaptor is big endian (insmod option) */
  2056. if (fmt->palette == VIDEO_PALETTE_RGB555 ||
  2057. fmt->palette == VIDEO_PALETTE_RGB565 ||
  2058. fmt->palette == VIDEO_PALETTE_RGB32) {
  2059. fh->ovfmt = fmt+1;
  2060. }
  2061. }
  2062. bt848_bright(btv,pic->brightness);
  2063. bt848_contrast(btv,pic->contrast);
  2064. bt848_hue(btv,pic->hue);
  2065. bt848_sat(btv,pic->colour);
  2066. up(&fh->cap.lock);
  2067. return 0;
  2068. }
  2069. case VIDIOCGWIN:
  2070. {
  2071. struct video_window *win = arg;
  2072. memset(win,0,sizeof(*win));
  2073. win->x = fh->ov.w.left;
  2074. win->y = fh->ov.w.top;
  2075. win->width = fh->ov.w.width;
  2076. win->height = fh->ov.w.height;
  2077. return 0;
  2078. }
  2079. case VIDIOCSWIN:
  2080. {
  2081. struct video_window *win = arg;
  2082. struct v4l2_window w2;
  2083. if (no_overlay > 0) {
  2084. printk ("VIDIOCSWIN: no_overlay\n");
  2085. return -EINVAL;
  2086. }
  2087. w2.field = V4L2_FIELD_ANY;
  2088. w2.w.left = win->x;
  2089. w2.w.top = win->y;
  2090. w2.w.width = win->width;
  2091. w2.w.height = win->height;
  2092. w2.clipcount = win->clipcount;
  2093. w2.clips = (struct v4l2_clip __user *)win->clips;
  2094. retval = setup_window(fh, btv, &w2, 0);
  2095. if (0 == retval) {
  2096. /* on v4l1 this ioctl affects the read() size too */
  2097. fh->width = fh->ov.w.width;
  2098. fh->height = fh->ov.w.height;
  2099. btv->init.width = fh->ov.w.width;
  2100. btv->init.height = fh->ov.w.height;
  2101. }
  2102. return retval;
  2103. }
  2104. case VIDIOCGFBUF:
  2105. {
  2106. struct video_buffer *fbuf = arg;
  2107. fbuf->base = btv->fbuf.base;
  2108. fbuf->width = btv->fbuf.fmt.width;
  2109. fbuf->height = btv->fbuf.fmt.height;
  2110. fbuf->bytesperline = btv->fbuf.fmt.bytesperline;
  2111. if (fh->ovfmt)
  2112. fbuf->depth = fh->ovfmt->depth;
  2113. return 0;
  2114. }
  2115. case VIDIOCSFBUF:
  2116. {
  2117. struct video_buffer *fbuf = arg;
  2118. const struct bttv_format *fmt;
  2119. unsigned long end;
  2120. if(!capable(CAP_SYS_ADMIN) &&
  2121. !capable(CAP_SYS_RAWIO))
  2122. return -EPERM;
  2123. end = (unsigned long)fbuf->base +
  2124. fbuf->height * fbuf->bytesperline;
  2125. down(&fh->cap.lock);
  2126. retval = -EINVAL;
  2127. switch (fbuf->depth) {
  2128. case 8:
  2129. fmt = format_by_palette(VIDEO_PALETTE_HI240);
  2130. break;
  2131. case 16:
  2132. fmt = format_by_palette(VIDEO_PALETTE_RGB565);
  2133. break;
  2134. case 24:
  2135. fmt = format_by_palette(VIDEO_PALETTE_RGB24);
  2136. break;
  2137. case 32:
  2138. fmt = format_by_palette(VIDEO_PALETTE_RGB32);
  2139. break;
  2140. case 15:
  2141. fbuf->depth = 16;
  2142. fmt = format_by_palette(VIDEO_PALETTE_RGB555);
  2143. break;
  2144. default:
  2145. fmt = NULL;
  2146. break;
  2147. }
  2148. if (NULL == fmt)
  2149. goto fh_unlock_and_return;
  2150. fh->ovfmt = fmt;
  2151. fh->fmt = fmt;
  2152. btv->init.ovfmt = fmt;
  2153. btv->init.fmt = fmt;
  2154. btv->fbuf.base = fbuf->base;
  2155. btv->fbuf.fmt.width = fbuf->width;
  2156. btv->fbuf.fmt.height = fbuf->height;
  2157. if (fbuf->bytesperline)
  2158. btv->fbuf.fmt.bytesperline = fbuf->bytesperline;
  2159. else
  2160. btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fbuf->depth/8;
  2161. up(&fh->cap.lock);
  2162. return 0;
  2163. }
  2164. case VIDIOCCAPTURE:
  2165. case VIDIOC_OVERLAY:
  2166. {
  2167. struct bttv_buffer *new;
  2168. int *on = arg;
  2169. if (*on) {
  2170. /* verify args */
  2171. if (NULL == btv->fbuf.base)
  2172. return -EINVAL;
  2173. if (!fh->ov.setup_ok) {
  2174. dprintk("bttv%d: overlay: !setup_ok\n",btv->c.nr);
  2175. return -EINVAL;
  2176. }
  2177. }
  2178. if (!check_alloc_btres(btv,fh,RESOURCE_OVERLAY))
  2179. return -EBUSY;
  2180. down(&fh->cap.lock);
  2181. if (*on) {
  2182. fh->ov.tvnorm = btv->tvnorm;
  2183. new = videobuf_alloc(sizeof(*new));
  2184. bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
  2185. } else {
  2186. new = NULL;
  2187. }
  2188. /* switch over */
  2189. retval = bttv_switch_overlay(btv,fh,new);
  2190. up(&fh->cap.lock);
  2191. return retval;
  2192. }
  2193. case VIDIOCGMBUF:
  2194. {
  2195. struct video_mbuf *mbuf = arg;
  2196. unsigned int i;
  2197. down(&fh->cap.lock);
  2198. retval = videobuf_mmap_setup(&fh->cap,gbuffers,gbufsize,
  2199. V4L2_MEMORY_MMAP);
  2200. if (retval < 0)
  2201. goto fh_unlock_and_return;
  2202. memset(mbuf,0,sizeof(*mbuf));
  2203. mbuf->frames = gbuffers;
  2204. mbuf->size = gbuffers * gbufsize;
  2205. for (i = 0; i < gbuffers; i++)
  2206. mbuf->offsets[i] = i * gbufsize;
  2207. up(&fh->cap.lock);
  2208. return 0;
  2209. }
  2210. case VIDIOCMCAPTURE:
  2211. {
  2212. struct video_mmap *vm = arg;
  2213. struct bttv_buffer *buf;
  2214. enum v4l2_field field;
  2215. if (vm->frame >= VIDEO_MAX_FRAME)
  2216. return -EINVAL;
  2217. down(&fh->cap.lock);
  2218. retval = -EINVAL;
  2219. buf = (struct bttv_buffer *)fh->cap.bufs[vm->frame];
  2220. if (NULL == buf)
  2221. goto fh_unlock_and_return;
  2222. if (0 == buf->vb.baddr)
  2223. goto fh_unlock_and_return;
  2224. if (buf->vb.state == STATE_QUEUED ||
  2225. buf->vb.state == STATE_ACTIVE)
  2226. goto fh_unlock_and_return;
  2227. field = (vm->height > bttv_tvnorms[btv->tvnorm].sheight/2)
  2228. ? V4L2_FIELD_INTERLACED
  2229. : V4L2_FIELD_BOTTOM;
  2230. retval = bttv_prepare_buffer(btv,buf,
  2231. format_by_palette(vm->format),
  2232. vm->width,vm->height,field);
  2233. if (0 != retval)
  2234. goto fh_unlock_and_return;
  2235. spin_lock_irqsave(&btv->s_lock,flags);
  2236. buffer_queue(&fh->cap,&buf->vb);
  2237. spin_unlock_irqrestore(&btv->s_lock,flags);
  2238. up(&fh->cap.lock);
  2239. return 0;
  2240. }
  2241. case VIDIOCSYNC:
  2242. {
  2243. int *frame = arg;
  2244. struct bttv_buffer *buf;
  2245. if (*frame >= VIDEO_MAX_FRAME)
  2246. return -EINVAL;
  2247. down(&fh->cap.lock);
  2248. retval = -EINVAL;
  2249. buf = (struct bttv_buffer *)fh->cap.bufs[*frame];
  2250. if (NULL == buf)
  2251. goto fh_unlock_and_return;
  2252. retval = videobuf_waiton(&buf->vb,0,1);
  2253. if (0 != retval)
  2254. goto fh_unlock_and_return;
  2255. switch (buf->vb.state) {
  2256. case STATE_ERROR:
  2257. retval = -EIO;
  2258. /* fall through */
  2259. case STATE_DONE:
  2260. videobuf_dma_pci_sync(btv->c.pci,&buf->vb.dma);
  2261. bttv_dma_free(btv,buf);
  2262. break;
  2263. default:
  2264. retval = -EINVAL;
  2265. break;
  2266. }
  2267. up(&fh->cap.lock);
  2268. return retval;
  2269. }
  2270. case VIDIOCGVBIFMT:
  2271. {
  2272. struct vbi_format *fmt = (void *) arg;
  2273. struct v4l2_format fmt2;
  2274. if (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE) {
  2275. retval = bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
  2276. if (0 != retval)
  2277. return retval;
  2278. }
  2279. bttv_vbi_get_fmt(fh, &fmt2);
  2280. memset(fmt,0,sizeof(*fmt));
  2281. fmt->sampling_rate = fmt2.fmt.vbi.sampling_rate;
  2282. fmt->samples_per_line = fmt2.fmt.vbi.samples_per_line;
  2283. fmt->sample_format = VIDEO_PALETTE_RAW;
  2284. fmt->start[0] = fmt2.fmt.vbi.start[0];
  2285. fmt->count[0] = fmt2.fmt.vbi.count[0];
  2286. fmt->start[1] = fmt2.fmt.vbi.start[1];
  2287. fmt->count[1] = fmt2.fmt.vbi.count[1];
  2288. if (fmt2.fmt.vbi.flags & VBI_UNSYNC)
  2289. fmt->flags |= V4L2_VBI_UNSYNC;
  2290. if (fmt2.fmt.vbi.flags & VBI_INTERLACED)
  2291. fmt->flags |= V4L2_VBI_INTERLACED;
  2292. return 0;
  2293. }
  2294. case VIDIOCSVBIFMT:
  2295. {
  2296. struct vbi_format *fmt = (void *) arg;
  2297. struct v4l2_format fmt2;
  2298. retval = bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
  2299. if (0 != retval)
  2300. return retval;
  2301. bttv_vbi_get_fmt(fh, &fmt2);
  2302. if (fmt->sampling_rate != fmt2.fmt.vbi.sampling_rate ||
  2303. fmt->samples_per_line != fmt2.fmt.vbi.samples_per_line ||
  2304. fmt->sample_format != VIDEO_PALETTE_RAW ||
  2305. fmt->start[0] != fmt2.fmt.vbi.start[0] ||
  2306. fmt->start[1] != fmt2.fmt.vbi.start[1] ||
  2307. fmt->count[0] != fmt->count[1] ||
  2308. fmt->count[0] < 1 ||
  2309. fmt->count[0] > 32 /* VBI_MAXLINES */)
  2310. return -EINVAL;
  2311. bttv_vbi_setlines(fh,btv,fmt->count[0]);
  2312. return 0;
  2313. }
  2314. case BTTV_VERSION:
  2315. case VIDIOCGFREQ:
  2316. case VIDIOCSFREQ:
  2317. case VIDIOCGTUNER:
  2318. case VIDIOCSTUNER:
  2319. case VIDIOCGCHAN:
  2320. case VIDIOCSCHAN:
  2321. case VIDIOCGAUDIO:
  2322. case VIDIOCSAUDIO:
  2323. return bttv_common_ioctls(btv,cmd,arg);
  2324. /* *** v4l2 *** ************************************************ */
  2325. case VIDIOC_QUERYCAP:
  2326. {
  2327. struct v4l2_capability *cap = arg;
  2328. if (0 == v4l2)
  2329. return -EINVAL;
  2330. strcpy(cap->driver,"bttv");
  2331. strlcpy(cap->card,btv->video_dev->name,sizeof(cap->card));
  2332. sprintf(cap->bus_info,"PCI:%s",pci_name(btv->c.pci));
  2333. cap->version = BTTV_VERSION_CODE;
  2334. cap->capabilities =
  2335. V4L2_CAP_VIDEO_CAPTURE |
  2336. V4L2_CAP_VBI_CAPTURE |
  2337. V4L2_CAP_READWRITE |
  2338. V4L2_CAP_STREAMING;
  2339. if (no_overlay <= 0)
  2340. cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
  2341. if (bttv_tvcards[btv->c.type].tuner != UNSET &&
  2342. bttv_tvcards[btv->c.type].tuner != TUNER_ABSENT)
  2343. cap->capabilities |= V4L2_CAP_TUNER;
  2344. return 0;
  2345. }
  2346. case VIDIOC_ENUM_FMT:
  2347. {
  2348. struct v4l2_fmtdesc *f = arg;
  2349. enum v4l2_buf_type type;
  2350. unsigned int i;
  2351. int index;
  2352. type = f->type;
  2353. if (V4L2_BUF_TYPE_VBI_CAPTURE == type) {
  2354. /* vbi */
  2355. index = f->index;
  2356. if (0 != index)
  2357. return -EINVAL;
  2358. memset(f,0,sizeof(*f));
  2359. f->index = index;
  2360. f->type = type;
  2361. f->pixelformat = V4L2_PIX_FMT_GREY;
  2362. strcpy(f->description,"vbi data");
  2363. return 0;
  2364. }
  2365. /* video capture + overlay */
  2366. index = -1;
  2367. for (i = 0; i < BTTV_FORMATS; i++) {
  2368. if (bttv_formats[i].fourcc != -1)
  2369. index++;
  2370. if ((unsigned int)index == f->index)
  2371. break;
  2372. }
  2373. if (BTTV_FORMATS == i)
  2374. return -EINVAL;
  2375. switch (f->type) {
  2376. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  2377. break;
  2378. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2379. if (!(bttv_formats[i].flags & FORMAT_FLAGS_PACKED))
  2380. return -EINVAL;
  2381. break;
  2382. default:
  2383. return -EINVAL;
  2384. }
  2385. memset(f,0,sizeof(*f));
  2386. f->index = index;
  2387. f->type = type;
  2388. f->pixelformat = bttv_formats[i].fourcc;
  2389. strlcpy(f->description,bttv_formats[i].name,sizeof(f->description));
  2390. return 0;
  2391. }
  2392. case VIDIOC_TRY_FMT:
  2393. {
  2394. struct v4l2_format *f = arg;
  2395. return bttv_try_fmt(fh,btv,f);
  2396. }
  2397. case VIDIOC_G_FMT:
  2398. {
  2399. struct v4l2_format *f = arg;
  2400. return bttv_g_fmt(fh,f);
  2401. }
  2402. case VIDIOC_S_FMT:
  2403. {
  2404. struct v4l2_format *f = arg;
  2405. return bttv_s_fmt(fh,btv,f);
  2406. }
  2407. case VIDIOC_G_FBUF:
  2408. {
  2409. struct v4l2_framebuffer *fb = arg;
  2410. *fb = btv->fbuf;
  2411. fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
  2412. if (fh->ovfmt)
  2413. fb->fmt.pixelformat = fh->ovfmt->fourcc;
  2414. return 0;
  2415. }
  2416. case VIDIOC_S_FBUF:
  2417. {
  2418. struct v4l2_framebuffer *fb = arg;
  2419. const struct bttv_format *fmt;
  2420. if(!capable(CAP_SYS_ADMIN) &&
  2421. !capable(CAP_SYS_RAWIO))
  2422. return -EPERM;
  2423. /* check args */
  2424. fmt = format_by_fourcc(fb->fmt.pixelformat);
  2425. if (NULL == fmt)
  2426. return -EINVAL;
  2427. if (0 == (fmt->flags & FORMAT_FLAGS_PACKED))
  2428. return -EINVAL;
  2429. down(&fh->cap.lock);
  2430. retval = -EINVAL;
  2431. if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
  2432. if (fb->fmt.width > bttv_tvnorms[btv->tvnorm].swidth)
  2433. goto fh_unlock_and_return;
  2434. if (fb->fmt.height > bttv_tvnorms[btv->tvnorm].sheight)
  2435. goto fh_unlock_and_return;
  2436. }
  2437. /* ok, accept it */
  2438. btv->fbuf.base = fb->base;
  2439. btv->fbuf.fmt.width = fb->fmt.width;
  2440. btv->fbuf.fmt.height = fb->fmt.height;
  2441. if (0 != fb->fmt.bytesperline)
  2442. btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline;
  2443. else
  2444. btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8;
  2445. retval = 0;
  2446. fh->ovfmt = fmt;
  2447. btv->init.ovfmt = fmt;
  2448. if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
  2449. fh->ov.w.left = 0;
  2450. fh->ov.w.top = 0;
  2451. fh->ov.w.width = fb->fmt.width;
  2452. fh->ov.w.height = fb->fmt.height;
  2453. btv->init.ov.w.width = fb->fmt.width;
  2454. btv->init.ov.w.height = fb->fmt.height;
  2455. if (fh->ov.clips)
  2456. kfree(fh->ov.clips);
  2457. fh->ov.clips = NULL;
  2458. fh->ov.nclips = 0;
  2459. if (check_btres(fh, RESOURCE_OVERLAY)) {
  2460. struct bttv_buffer *new;
  2461. new = videobuf_alloc(sizeof(*new));
  2462. bttv_overlay_risc(btv,&fh->ov,fh->ovfmt,new);
  2463. retval = bttv_switch_overlay(btv,fh,new);
  2464. }
  2465. }
  2466. up(&fh->cap.lock);
  2467. return retval;
  2468. }
  2469. case VIDIOC_REQBUFS:
  2470. return videobuf_reqbufs(bttv_queue(fh),arg);
  2471. case VIDIOC_QUERYBUF:
  2472. return videobuf_querybuf(bttv_queue(fh),arg);
  2473. case VIDIOC_QBUF:
  2474. return videobuf_qbuf(bttv_queue(fh),arg);
  2475. case VIDIOC_DQBUF:
  2476. return videobuf_dqbuf(bttv_queue(fh),arg,
  2477. file->f_flags & O_NONBLOCK);
  2478. case VIDIOC_STREAMON:
  2479. {
  2480. int res = bttv_resource(fh);
  2481. if (!check_alloc_btres(btv,fh,res))
  2482. return -EBUSY;
  2483. return videobuf_streamon(bttv_queue(fh));
  2484. }
  2485. case VIDIOC_STREAMOFF:
  2486. {
  2487. int res = bttv_resource(fh);
  2488. retval = videobuf_streamoff(bttv_queue(fh));
  2489. if (retval < 0)
  2490. return retval;
  2491. free_btres(btv,fh,res);
  2492. return 0;
  2493. }
  2494. case VIDIOC_QUERYCTRL:
  2495. {
  2496. struct v4l2_queryctrl *c = arg;
  2497. int i;
  2498. if ((c->id < V4L2_CID_BASE ||
  2499. c->id >= V4L2_CID_LASTP1) &&
  2500. (c->id < V4L2_CID_PRIVATE_BASE ||
  2501. c->id >= V4L2_CID_PRIVATE_LASTP1))
  2502. return -EINVAL;
  2503. for (i = 0; i < BTTV_CTLS; i++)
  2504. if (bttv_ctls[i].id == c->id)
  2505. break;
  2506. if (i == BTTV_CTLS) {
  2507. *c = no_ctl;
  2508. return 0;
  2509. }
  2510. *c = bttv_ctls[i];
  2511. if (i >= 4 && i <= 8) {
  2512. struct video_audio va;
  2513. memset(&va,0,sizeof(va));
  2514. bttv_call_i2c_clients(btv, VIDIOCGAUDIO, &va);
  2515. if (btv->audio_hook)
  2516. btv->audio_hook(btv,&va,0);
  2517. switch (bttv_ctls[i].id) {
  2518. case V4L2_CID_AUDIO_VOLUME:
  2519. if (!(va.flags & VIDEO_AUDIO_VOLUME))
  2520. *c = no_ctl;
  2521. break;
  2522. case V4L2_CID_AUDIO_BALANCE:
  2523. if (!(va.flags & VIDEO_AUDIO_BALANCE))
  2524. *c = no_ctl;
  2525. break;
  2526. case V4L2_CID_AUDIO_BASS:
  2527. if (!(va.flags & VIDEO_AUDIO_BASS))
  2528. *c = no_ctl;
  2529. break;
  2530. case V4L2_CID_AUDIO_TREBLE:
  2531. if (!(va.flags & VIDEO_AUDIO_TREBLE))
  2532. *c = no_ctl;
  2533. break;
  2534. }
  2535. }
  2536. return 0;
  2537. }
  2538. case VIDIOC_G_CTRL:
  2539. return get_control(btv,arg);
  2540. case VIDIOC_S_CTRL:
  2541. return set_control(btv,arg);
  2542. case VIDIOC_G_PARM:
  2543. {
  2544. struct v4l2_streamparm *parm = arg;
  2545. struct v4l2_standard s;
  2546. if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  2547. return -EINVAL;
  2548. memset(parm,0,sizeof(*parm));
  2549. v4l2_video_std_construct(&s, bttv_tvnorms[btv->tvnorm].v4l2_id,
  2550. bttv_tvnorms[btv->tvnorm].name);
  2551. parm->parm.capture.timeperframe = s.frameperiod;
  2552. return 0;
  2553. }
  2554. case VIDIOC_G_PRIORITY:
  2555. {
  2556. enum v4l2_priority *p = arg;
  2557. *p = v4l2_prio_max(&btv->prio);
  2558. return 0;
  2559. }
  2560. case VIDIOC_S_PRIORITY:
  2561. {
  2562. enum v4l2_priority *prio = arg;
  2563. return v4l2_prio_change(&btv->prio, &fh->prio, *prio);
  2564. }
  2565. case VIDIOC_ENUMSTD:
  2566. case VIDIOC_G_STD:
  2567. case VIDIOC_S_STD:
  2568. case VIDIOC_ENUMINPUT:
  2569. case VIDIOC_G_INPUT:
  2570. case VIDIOC_S_INPUT:
  2571. case VIDIOC_G_TUNER:
  2572. case VIDIOC_S_TUNER:
  2573. case VIDIOC_G_FREQUENCY:
  2574. case VIDIOC_S_FREQUENCY:
  2575. return bttv_common_ioctls(btv,cmd,arg);
  2576. default:
  2577. return -ENOIOCTLCMD;
  2578. }
  2579. return 0;
  2580. fh_unlock_and_return:
  2581. up(&fh->cap.lock);
  2582. return retval;
  2583. }
  2584. static int bttv_ioctl(struct inode *inode, struct file *file,
  2585. unsigned int cmd, unsigned long arg)
  2586. {
  2587. struct bttv_fh *fh = file->private_data;
  2588. switch (cmd) {
  2589. case BTTV_VBISIZE:
  2590. bttv_switch_type(fh,V4L2_BUF_TYPE_VBI_CAPTURE);
  2591. return fh->lines * 2 * 2048;
  2592. default:
  2593. return video_usercopy(inode, file, cmd, arg, bttv_do_ioctl);
  2594. }
  2595. }
  2596. static ssize_t bttv_read(struct file *file, char __user *data,
  2597. size_t count, loff_t *ppos)
  2598. {
  2599. struct bttv_fh *fh = file->private_data;
  2600. int retval = 0;
  2601. if (fh->btv->errors)
  2602. bttv_reinit_bt848(fh->btv);
  2603. dprintk("bttv%d: read count=%d type=%s\n",
  2604. fh->btv->c.nr,(int)count,v4l2_type_names[fh->type]);
  2605. switch (fh->type) {
  2606. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  2607. if (locked_btres(fh->btv,RESOURCE_VIDEO))
  2608. return -EBUSY;
  2609. retval = videobuf_read_one(&fh->cap, data, count, ppos,
  2610. file->f_flags & O_NONBLOCK);
  2611. break;
  2612. case V4L2_BUF_TYPE_VBI_CAPTURE:
  2613. if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
  2614. return -EBUSY;
  2615. retval = videobuf_read_stream(&fh->vbi, data, count, ppos, 1,
  2616. file->f_flags & O_NONBLOCK);
  2617. break;
  2618. default:
  2619. BUG();
  2620. }
  2621. return retval;
  2622. }
  2623. static unsigned int bttv_poll(struct file *file, poll_table *wait)
  2624. {
  2625. struct bttv_fh *fh = file->private_data;
  2626. struct bttv_buffer *buf;
  2627. enum v4l2_field field;
  2628. if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
  2629. if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
  2630. return POLLERR;
  2631. return videobuf_poll_stream(file, &fh->vbi, wait);
  2632. }
  2633. if (check_btres(fh,RESOURCE_VIDEO)) {
  2634. /* streaming capture */
  2635. if (list_empty(&fh->cap.stream))
  2636. return POLLERR;
  2637. buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream);
  2638. } else {
  2639. /* read() capture */
  2640. down(&fh->cap.lock);
  2641. if (NULL == fh->cap.read_buf) {
  2642. /* need to capture a new frame */
  2643. if (locked_btres(fh->btv,RESOURCE_VIDEO)) {
  2644. up(&fh->cap.lock);
  2645. return POLLERR;
  2646. }
  2647. fh->cap.read_buf = videobuf_alloc(fh->cap.msize);
  2648. if (NULL == fh->cap.read_buf) {
  2649. up(&fh->cap.lock);
  2650. return POLLERR;
  2651. }
  2652. fh->cap.read_buf->memory = V4L2_MEMORY_USERPTR;
  2653. field = videobuf_next_field(&fh->cap);
  2654. if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,field)) {
  2655. up(&fh->cap.lock);
  2656. return POLLERR;
  2657. }
  2658. fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
  2659. fh->cap.read_off = 0;
  2660. }
  2661. up(&fh->cap.lock);
  2662. buf = (struct bttv_buffer*)fh->cap.read_buf;
  2663. }
  2664. poll_wait(file, &buf->vb.done, wait);
  2665. if (buf->vb.state == STATE_DONE ||
  2666. buf->vb.state == STATE_ERROR)
  2667. return POLLIN|POLLRDNORM;
  2668. return 0;
  2669. }
  2670. static int bttv_open(struct inode *inode, struct file *file)
  2671. {
  2672. int minor = iminor(inode);
  2673. struct bttv *btv = NULL;
  2674. struct bttv_fh *fh;
  2675. enum v4l2_buf_type type = 0;
  2676. unsigned int i;
  2677. dprintk(KERN_DEBUG "bttv: open minor=%d\n",minor);
  2678. for (i = 0; i < bttv_num; i++) {
  2679. if (bttvs[i].video_dev &&
  2680. bttvs[i].video_dev->minor == minor) {
  2681. btv = &bttvs[i];
  2682. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  2683. break;
  2684. }
  2685. if (bttvs[i].vbi_dev &&
  2686. bttvs[i].vbi_dev->minor == minor) {
  2687. btv = &bttvs[i];
  2688. type = V4L2_BUF_TYPE_VBI_CAPTURE;
  2689. break;
  2690. }
  2691. }
  2692. if (NULL == btv)
  2693. return -ENODEV;
  2694. dprintk(KERN_DEBUG "bttv%d: open called (type=%s)\n",
  2695. btv->c.nr,v4l2_type_names[type]);
  2696. /* allocate per filehandle data */
  2697. fh = kmalloc(sizeof(*fh),GFP_KERNEL);
  2698. if (NULL == fh)
  2699. return -ENOMEM;
  2700. file->private_data = fh;
  2701. *fh = btv->init;
  2702. fh->type = type;
  2703. fh->ov.setup_ok = 0;
  2704. v4l2_prio_open(&btv->prio,&fh->prio);
  2705. videobuf_queue_init(&fh->cap, &bttv_video_qops,
  2706. btv->c.pci, &btv->s_lock,
  2707. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  2708. V4L2_FIELD_INTERLACED,
  2709. sizeof(struct bttv_buffer),
  2710. fh);
  2711. videobuf_queue_init(&fh->vbi, &bttv_vbi_qops,
  2712. btv->c.pci, &btv->s_lock,
  2713. V4L2_BUF_TYPE_VBI_CAPTURE,
  2714. V4L2_FIELD_SEQ_TB,
  2715. sizeof(struct bttv_buffer),
  2716. fh);
  2717. i2c_vidiocschan(btv);
  2718. btv->users++;
  2719. if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)
  2720. bttv_vbi_setlines(fh,btv,16);
  2721. bttv_field_count(btv);
  2722. return 0;
  2723. }
  2724. static int bttv_release(struct inode *inode, struct file *file)
  2725. {
  2726. struct bttv_fh *fh = file->private_data;
  2727. struct bttv *btv = fh->btv;
  2728. /* turn off overlay */
  2729. if (check_btres(fh, RESOURCE_OVERLAY))
  2730. bttv_switch_overlay(btv,fh,NULL);
  2731. /* stop video capture */
  2732. if (check_btres(fh, RESOURCE_VIDEO)) {
  2733. videobuf_streamoff(&fh->cap);
  2734. free_btres(btv,fh,RESOURCE_VIDEO);
  2735. }
  2736. if (fh->cap.read_buf) {
  2737. buffer_release(&fh->cap,fh->cap.read_buf);
  2738. kfree(fh->cap.read_buf);
  2739. }
  2740. /* stop vbi capture */
  2741. if (check_btres(fh, RESOURCE_VBI)) {
  2742. if (fh->vbi.streaming)
  2743. videobuf_streamoff(&fh->vbi);
  2744. if (fh->vbi.reading)
  2745. videobuf_read_stop(&fh->vbi);
  2746. free_btres(btv,fh,RESOURCE_VBI);
  2747. }
  2748. /* free stuff */
  2749. videobuf_mmap_free(&fh->cap);
  2750. videobuf_mmap_free(&fh->vbi);
  2751. v4l2_prio_close(&btv->prio,&fh->prio);
  2752. file->private_data = NULL;
  2753. kfree(fh);
  2754. btv->users--;
  2755. bttv_field_count(btv);
  2756. return 0;
  2757. }
  2758. static int
  2759. bttv_mmap(struct file *file, struct vm_area_struct *vma)
  2760. {
  2761. struct bttv_fh *fh = file->private_data;
  2762. dprintk("bttv%d: mmap type=%s 0x%lx+%ld\n",
  2763. fh->btv->c.nr, v4l2_type_names[fh->type],
  2764. vma->vm_start, vma->vm_end - vma->vm_start);
  2765. return videobuf_mmap_mapper(bttv_queue(fh),vma);
  2766. }
  2767. static struct file_operations bttv_fops =
  2768. {
  2769. .owner = THIS_MODULE,
  2770. .open = bttv_open,
  2771. .release = bttv_release,
  2772. .ioctl = bttv_ioctl,
  2773. .llseek = no_llseek,
  2774. .read = bttv_read,
  2775. .mmap = bttv_mmap,
  2776. .poll = bttv_poll,
  2777. };
  2778. static struct video_device bttv_video_template =
  2779. {
  2780. .name = "UNSET",
  2781. .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|
  2782. VID_TYPE_CLIPPING|VID_TYPE_SCALES,
  2783. .hardware = VID_HARDWARE_BT848,
  2784. .fops = &bttv_fops,
  2785. .minor = -1,
  2786. };
  2787. static struct video_device bttv_vbi_template =
  2788. {
  2789. .name = "bt848/878 vbi",
  2790. .type = VID_TYPE_TUNER|VID_TYPE_TELETEXT,
  2791. .hardware = VID_HARDWARE_BT848,
  2792. .fops = &bttv_fops,
  2793. .minor = -1,
  2794. };
  2795. /* ----------------------------------------------------------------------- */
  2796. /* radio interface */
  2797. static int radio_open(struct inode *inode, struct file *file)
  2798. {
  2799. int minor = iminor(inode);
  2800. struct bttv *btv = NULL;
  2801. unsigned int i;
  2802. dprintk("bttv: open minor=%d\n",minor);
  2803. for (i = 0; i < bttv_num; i++) {
  2804. if (bttvs[i].radio_dev->minor == minor) {
  2805. btv = &bttvs[i];
  2806. break;
  2807. }
  2808. }
  2809. if (NULL == btv)
  2810. return -ENODEV;
  2811. dprintk("bttv%d: open called (radio)\n",btv->c.nr);
  2812. down(&btv->lock);
  2813. if (btv->radio_user) {
  2814. up(&btv->lock);
  2815. return -EBUSY;
  2816. }
  2817. btv->radio_user++;
  2818. file->private_data = btv;
  2819. i2c_vidiocschan(btv);
  2820. bttv_call_i2c_clients(btv,AUDC_SET_RADIO,&btv->tuner_type);
  2821. audio_mux(btv,AUDIO_RADIO);
  2822. up(&btv->lock);
  2823. return 0;
  2824. }
  2825. static int radio_release(struct inode *inode, struct file *file)
  2826. {
  2827. struct bttv *btv = file->private_data;
  2828. btv->radio_user--;
  2829. return 0;
  2830. }
  2831. static int radio_do_ioctl(struct inode *inode, struct file *file,
  2832. unsigned int cmd, void *arg)
  2833. {
  2834. struct bttv *btv = file->private_data;
  2835. switch (cmd) {
  2836. case VIDIOCGCAP:
  2837. {
  2838. struct video_capability *cap = arg;
  2839. memset(cap,0,sizeof(*cap));
  2840. strcpy(cap->name,btv->radio_dev->name);
  2841. cap->type = VID_TYPE_TUNER;
  2842. cap->channels = 1;
  2843. cap->audios = 1;
  2844. return 0;
  2845. }
  2846. case VIDIOCGTUNER:
  2847. {
  2848. struct video_tuner *v = arg;
  2849. if(v->tuner)
  2850. return -EINVAL;
  2851. memset(v,0,sizeof(*v));
  2852. strcpy(v->name, "Radio");
  2853. bttv_call_i2c_clients(btv,cmd,v);
  2854. return 0;
  2855. }
  2856. case VIDIOCSTUNER:
  2857. /* nothing to do */
  2858. return 0;
  2859. case BTTV_VERSION:
  2860. case VIDIOCGFREQ:
  2861. case VIDIOCSFREQ:
  2862. case VIDIOCGAUDIO:
  2863. case VIDIOCSAUDIO:
  2864. return bttv_common_ioctls(btv,cmd,arg);
  2865. default:
  2866. return -ENOIOCTLCMD;
  2867. }
  2868. return 0;
  2869. }
  2870. static int radio_ioctl(struct inode *inode, struct file *file,
  2871. unsigned int cmd, unsigned long arg)
  2872. {
  2873. return video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
  2874. }
  2875. static struct file_operations radio_fops =
  2876. {
  2877. .owner = THIS_MODULE,
  2878. .open = radio_open,
  2879. .release = radio_release,
  2880. .ioctl = radio_ioctl,
  2881. .llseek = no_llseek,
  2882. };
  2883. static struct video_device radio_template =
  2884. {
  2885. .name = "bt848/878 radio",
  2886. .type = VID_TYPE_TUNER,
  2887. .hardware = VID_HARDWARE_BT848,
  2888. .fops = &radio_fops,
  2889. .minor = -1,
  2890. };
  2891. /* ----------------------------------------------------------------------- */
  2892. /* some debug code */
  2893. static int bttv_risc_decode(u32 risc)
  2894. {
  2895. static char *instr[16] = {
  2896. [ BT848_RISC_WRITE >> 28 ] = "write",
  2897. [ BT848_RISC_SKIP >> 28 ] = "skip",
  2898. [ BT848_RISC_WRITEC >> 28 ] = "writec",
  2899. [ BT848_RISC_JUMP >> 28 ] = "jump",
  2900. [ BT848_RISC_SYNC >> 28 ] = "sync",
  2901. [ BT848_RISC_WRITE123 >> 28 ] = "write123",
  2902. [ BT848_RISC_SKIP123 >> 28 ] = "skip123",
  2903. [ BT848_RISC_WRITE1S23 >> 28 ] = "write1s23",
  2904. };
  2905. static int incr[16] = {
  2906. [ BT848_RISC_WRITE >> 28 ] = 2,
  2907. [ BT848_RISC_JUMP >> 28 ] = 2,
  2908. [ BT848_RISC_SYNC >> 28 ] = 2,
  2909. [ BT848_RISC_WRITE123 >> 28 ] = 5,
  2910. [ BT848_RISC_SKIP123 >> 28 ] = 2,
  2911. [ BT848_RISC_WRITE1S23 >> 28 ] = 3,
  2912. };
  2913. static char *bits[] = {
  2914. "be0", "be1", "be2", "be3/resync",
  2915. "set0", "set1", "set2", "set3",
  2916. "clr0", "clr1", "clr2", "clr3",
  2917. "irq", "res", "eol", "sol",
  2918. };
  2919. int i;
  2920. printk("0x%08x [ %s", risc,
  2921. instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
  2922. for (i = ARRAY_SIZE(bits)-1; i >= 0; i--)
  2923. if (risc & (1 << (i + 12)))
  2924. printk(" %s",bits[i]);
  2925. printk(" count=%d ]\n", risc & 0xfff);
  2926. return incr[risc >> 28] ? incr[risc >> 28] : 1;
  2927. }
  2928. static void bttv_risc_disasm(struct bttv *btv,
  2929. struct btcx_riscmem *risc)
  2930. {
  2931. unsigned int i,j,n;
  2932. printk("%s: risc disasm: %p [dma=0x%08lx]\n",
  2933. btv->c.name, risc->cpu, (unsigned long)risc->dma);
  2934. for (i = 0; i < (risc->size >> 2); i += n) {
  2935. printk("%s: 0x%lx: ", btv->c.name,
  2936. (unsigned long)(risc->dma + (i<<2)));
  2937. n = bttv_risc_decode(risc->cpu[i]);
  2938. for (j = 1; j < n; j++)
  2939. printk("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
  2940. btv->c.name, (unsigned long)(risc->dma + ((i+j)<<2)),
  2941. risc->cpu[i+j], j);
  2942. if (0 == risc->cpu[i])
  2943. break;
  2944. }
  2945. }
  2946. static void bttv_print_riscaddr(struct bttv *btv)
  2947. {
  2948. printk(" main: %08Lx\n",
  2949. (unsigned long long)btv->main.dma);
  2950. printk(" vbi : o=%08Lx e=%08Lx\n",
  2951. btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
  2952. btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0);
  2953. printk(" cap : o=%08Lx e=%08Lx\n",
  2954. btv->curr.top ? (unsigned long long)btv->curr.top->top.dma : 0,
  2955. btv->curr.bottom ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
  2956. printk(" scr : o=%08Lx e=%08Lx\n",
  2957. btv->screen ? (unsigned long long)btv->screen->top.dma : 0,
  2958. btv->screen ? (unsigned long long)btv->screen->bottom.dma : 0);
  2959. bttv_risc_disasm(btv, &btv->main);
  2960. }
  2961. /* ----------------------------------------------------------------------- */
  2962. /* irq handler */
  2963. static char *irq_name[] = {
  2964. "FMTCHG", // format change detected (525 vs. 625)
  2965. "VSYNC", // vertical sync (new field)
  2966. "HSYNC", // horizontal sync
  2967. "OFLOW", // chroma/luma AGC overflow
  2968. "HLOCK", // horizontal lock changed
  2969. "VPRES", // video presence changed
  2970. "6", "7",
  2971. "I2CDONE", // hw irc operation finished
  2972. "GPINT", // gpio port triggered irq
  2973. "10",
  2974. "RISCI", // risc instruction triggered irq
  2975. "FBUS", // pixel data fifo dropped data (high pci bus latencies)
  2976. "FTRGT", // pixel data fifo overrun
  2977. "FDSR", // fifo data stream resyncronisation
  2978. "PPERR", // parity error (data transfer)
  2979. "RIPERR", // parity error (read risc instructions)
  2980. "PABORT", // pci abort
  2981. "OCERR", // risc instruction error
  2982. "SCERR", // syncronisation error
  2983. };
  2984. static void bttv_print_irqbits(u32 print, u32 mark)
  2985. {
  2986. unsigned int i;
  2987. printk("bits:");
  2988. for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
  2989. if (print & (1 << i))
  2990. printk(" %s",irq_name[i]);
  2991. if (mark & (1 << i))
  2992. printk("*");
  2993. }
  2994. }
  2995. static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc)
  2996. {
  2997. printk("bttv%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
  2998. btv->c.nr,
  2999. (unsigned long)btv->main.dma,
  3000. (unsigned long)btv->main.cpu[RISC_SLOT_O_VBI+1],
  3001. (unsigned long)btv->main.cpu[RISC_SLOT_O_FIELD+1],
  3002. (unsigned long)rc);
  3003. if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) {
  3004. printk("bttv%d: Oh, there (temporarely?) is no input signal. "
  3005. "Ok, then this is harmless, don't worry ;)\n",
  3006. btv->c.nr);
  3007. return;
  3008. }
  3009. printk("bttv%d: Uhm. Looks like we have unusual high IRQ latencies.\n",
  3010. btv->c.nr);
  3011. printk("bttv%d: Lets try to catch the culpit red-handed ...\n",
  3012. btv->c.nr);
  3013. dump_stack();
  3014. }
  3015. static int
  3016. bttv_irq_next_video(struct bttv *btv, struct bttv_buffer_set *set)
  3017. {
  3018. struct bttv_buffer *item;
  3019. memset(set,0,sizeof(*set));
  3020. /* capture request ? */
  3021. if (!list_empty(&btv->capture)) {
  3022. set->frame_irq = 1;
  3023. item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
  3024. if (V4L2_FIELD_HAS_TOP(item->vb.field))
  3025. set->top = item;
  3026. if (V4L2_FIELD_HAS_BOTTOM(item->vb.field))
  3027. set->bottom = item;
  3028. /* capture request for other field ? */
  3029. if (!V4L2_FIELD_HAS_BOTH(item->vb.field) &&
  3030. (item->vb.queue.next != &btv->capture)) {
  3031. item = list_entry(item->vb.queue.next, struct bttv_buffer, vb.queue);
  3032. if (!V4L2_FIELD_HAS_BOTH(item->vb.field)) {
  3033. if (NULL == set->top &&
  3034. V4L2_FIELD_TOP == item->vb.field) {
  3035. set->top = item;
  3036. }
  3037. if (NULL == set->bottom &&
  3038. V4L2_FIELD_BOTTOM == item->vb.field) {
  3039. set->bottom = item;
  3040. }
  3041. if (NULL != set->top && NULL != set->bottom)
  3042. set->top_irq = 2;
  3043. }
  3044. }
  3045. }
  3046. /* screen overlay ? */
  3047. if (NULL != btv->screen) {
  3048. if (V4L2_FIELD_HAS_BOTH(btv->screen->vb.field)) {
  3049. if (NULL == set->top && NULL == set->bottom) {
  3050. set->top = btv->screen;
  3051. set->bottom = btv->screen;
  3052. }
  3053. } else {
  3054. if (V4L2_FIELD_TOP == btv->screen->vb.field &&
  3055. NULL == set->top) {
  3056. set->top = btv->screen;
  3057. }
  3058. if (V4L2_FIELD_BOTTOM == btv->screen->vb.field &&
  3059. NULL == set->bottom) {
  3060. set->bottom = btv->screen;
  3061. }
  3062. }
  3063. }
  3064. dprintk("bttv%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
  3065. btv->c.nr,set->top, set->bottom,
  3066. btv->screen,set->frame_irq,set->top_irq);
  3067. return 0;
  3068. }
  3069. static void
  3070. bttv_irq_wakeup_video(struct bttv *btv, struct bttv_buffer_set *wakeup,
  3071. struct bttv_buffer_set *curr, unsigned int state)
  3072. {
  3073. struct timeval ts;
  3074. do_gettimeofday(&ts);
  3075. if (wakeup->top == wakeup->bottom) {
  3076. if (NULL != wakeup->top && curr->top != wakeup->top) {
  3077. if (irq_debug > 1)
  3078. printk("bttv%d: wakeup: both=%p\n",btv->c.nr,wakeup->top);
  3079. wakeup->top->vb.ts = ts;
  3080. wakeup->top->vb.field_count = btv->field_count;
  3081. wakeup->top->vb.state = state;
  3082. wake_up(&wakeup->top->vb.done);
  3083. }
  3084. } else {
  3085. if (NULL != wakeup->top && curr->top != wakeup->top) {
  3086. if (irq_debug > 1)
  3087. printk("bttv%d: wakeup: top=%p\n",btv->c.nr,wakeup->top);
  3088. wakeup->top->vb.ts = ts;
  3089. wakeup->top->vb.field_count = btv->field_count;
  3090. wakeup->top->vb.state = state;
  3091. wake_up(&wakeup->top->vb.done);
  3092. }
  3093. if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
  3094. if (irq_debug > 1)
  3095. printk("bttv%d: wakeup: bottom=%p\n",btv->c.nr,wakeup->bottom);
  3096. wakeup->bottom->vb.ts = ts;
  3097. wakeup->bottom->vb.field_count = btv->field_count;
  3098. wakeup->bottom->vb.state = state;
  3099. wake_up(&wakeup->bottom->vb.done);
  3100. }
  3101. }
  3102. }
  3103. static void
  3104. bttv_irq_wakeup_vbi(struct bttv *btv, struct bttv_buffer *wakeup,
  3105. unsigned int state)
  3106. {
  3107. struct timeval ts;
  3108. if (NULL == wakeup)
  3109. return;
  3110. do_gettimeofday(&ts);
  3111. wakeup->vb.ts = ts;
  3112. wakeup->vb.field_count = btv->field_count;
  3113. wakeup->vb.state = state;
  3114. wake_up(&wakeup->vb.done);
  3115. }
  3116. static void bttv_irq_timeout(unsigned long data)
  3117. {
  3118. struct bttv *btv = (struct bttv *)data;
  3119. struct bttv_buffer_set old,new;
  3120. struct bttv_buffer *ovbi;
  3121. struct bttv_buffer *item;
  3122. unsigned long flags;
  3123. if (bttv_verbose) {
  3124. printk(KERN_INFO "bttv%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
  3125. btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total,
  3126. btread(BT848_RISC_COUNT));
  3127. bttv_print_irqbits(btread(BT848_INT_STAT),0);
  3128. printk("\n");
  3129. }
  3130. spin_lock_irqsave(&btv->s_lock,flags);
  3131. /* deactivate stuff */
  3132. memset(&new,0,sizeof(new));
  3133. old = btv->curr;
  3134. ovbi = btv->cvbi;
  3135. btv->curr = new;
  3136. btv->cvbi = NULL;
  3137. btv->loop_irq = 0;
  3138. bttv_buffer_activate_video(btv, &new);
  3139. bttv_buffer_activate_vbi(btv, NULL);
  3140. bttv_set_dma(btv, 0);
  3141. /* wake up */
  3142. bttv_irq_wakeup_video(btv, &old, &new, STATE_ERROR);
  3143. bttv_irq_wakeup_vbi(btv, ovbi, STATE_ERROR);
  3144. /* cancel all outstanding capture / vbi requests */
  3145. while (!list_empty(&btv->capture)) {
  3146. item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
  3147. list_del(&item->vb.queue);
  3148. item->vb.state = STATE_ERROR;
  3149. wake_up(&item->vb.done);
  3150. }
  3151. while (!list_empty(&btv->vcapture)) {
  3152. item = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
  3153. list_del(&item->vb.queue);
  3154. item->vb.state = STATE_ERROR;
  3155. wake_up(&item->vb.done);
  3156. }
  3157. btv->errors++;
  3158. spin_unlock_irqrestore(&btv->s_lock,flags);
  3159. }
  3160. static void
  3161. bttv_irq_wakeup_top(struct bttv *btv)
  3162. {
  3163. struct bttv_buffer *wakeup = btv->curr.top;
  3164. if (NULL == wakeup)
  3165. return;
  3166. spin_lock(&btv->s_lock);
  3167. btv->curr.top_irq = 0;
  3168. btv->curr.top = NULL;
  3169. bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
  3170. do_gettimeofday(&wakeup->vb.ts);
  3171. wakeup->vb.field_count = btv->field_count;
  3172. wakeup->vb.state = STATE_DONE;
  3173. wake_up(&wakeup->vb.done);
  3174. spin_unlock(&btv->s_lock);
  3175. }
  3176. static inline int is_active(struct btcx_riscmem *risc, u32 rc)
  3177. {
  3178. if (rc < risc->dma)
  3179. return 0;
  3180. if (rc > risc->dma + risc->size)
  3181. return 0;
  3182. return 1;
  3183. }
  3184. static void
  3185. bttv_irq_switch_video(struct bttv *btv)
  3186. {
  3187. struct bttv_buffer_set new;
  3188. struct bttv_buffer_set old;
  3189. dma_addr_t rc;
  3190. spin_lock(&btv->s_lock);
  3191. /* new buffer set */
  3192. bttv_irq_next_video(btv, &new);
  3193. rc = btread(BT848_RISC_COUNT);
  3194. if ((btv->curr.top && is_active(&btv->curr.top->top, rc)) ||
  3195. (btv->curr.bottom && is_active(&btv->curr.bottom->bottom, rc))) {
  3196. btv->framedrop++;
  3197. if (debug_latency)
  3198. bttv_irq_debug_low_latency(btv, rc);
  3199. spin_unlock(&btv->s_lock);
  3200. return;
  3201. }
  3202. /* switch over */
  3203. old = btv->curr;
  3204. btv->curr = new;
  3205. btv->loop_irq &= ~1;
  3206. bttv_buffer_activate_video(btv, &new);
  3207. bttv_set_dma(btv, 0);
  3208. /* switch input */
  3209. if (UNSET != btv->new_input) {
  3210. video_mux(btv,btv->new_input);
  3211. btv->new_input = UNSET;
  3212. }
  3213. /* wake up finished buffers */
  3214. bttv_irq_wakeup_video(btv, &old, &new, STATE_DONE);
  3215. spin_unlock(&btv->s_lock);
  3216. }
  3217. static void
  3218. bttv_irq_switch_vbi(struct bttv *btv)
  3219. {
  3220. struct bttv_buffer *new = NULL;
  3221. struct bttv_buffer *old;
  3222. u32 rc;
  3223. spin_lock(&btv->s_lock);
  3224. if (!list_empty(&btv->vcapture))
  3225. new = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
  3226. old = btv->cvbi;
  3227. rc = btread(BT848_RISC_COUNT);
  3228. if (NULL != old && (is_active(&old->top, rc) ||
  3229. is_active(&old->bottom, rc))) {
  3230. btv->framedrop++;
  3231. if (debug_latency)
  3232. bttv_irq_debug_low_latency(btv, rc);
  3233. spin_unlock(&btv->s_lock);
  3234. return;
  3235. }
  3236. /* switch */
  3237. btv->cvbi = new;
  3238. btv->loop_irq &= ~4;
  3239. bttv_buffer_activate_vbi(btv, new);
  3240. bttv_set_dma(btv, 0);
  3241. bttv_irq_wakeup_vbi(btv, old, STATE_DONE);
  3242. spin_unlock(&btv->s_lock);
  3243. }
  3244. static irqreturn_t bttv_irq(int irq, void *dev_id, struct pt_regs * regs)
  3245. {
  3246. u32 stat,astat;
  3247. u32 dstat;
  3248. int count;
  3249. struct bttv *btv;
  3250. int handled = 0;
  3251. btv=(struct bttv *)dev_id;
  3252. count=0;
  3253. while (1) {
  3254. /* get/clear interrupt status bits */
  3255. stat=btread(BT848_INT_STAT);
  3256. astat=stat&btread(BT848_INT_MASK);
  3257. if (!astat)
  3258. break;
  3259. handled = 1;
  3260. btwrite(stat,BT848_INT_STAT);
  3261. /* get device status bits */
  3262. dstat=btread(BT848_DSTATUS);
  3263. if (irq_debug) {
  3264. printk(KERN_DEBUG "bttv%d: irq loop=%d fc=%d "
  3265. "riscs=%x, riscc=%08x, ",
  3266. btv->c.nr, count, btv->field_count,
  3267. stat>>28, btread(BT848_RISC_COUNT));
  3268. bttv_print_irqbits(stat,astat);
  3269. if (stat & BT848_INT_HLOCK)
  3270. printk(" HLOC => %s", (dstat & BT848_DSTATUS_HLOC)
  3271. ? "yes" : "no");
  3272. if (stat & BT848_INT_VPRES)
  3273. printk(" PRES => %s", (dstat & BT848_DSTATUS_PRES)
  3274. ? "yes" : "no");
  3275. if (stat & BT848_INT_FMTCHG)
  3276. printk(" NUML => %s", (dstat & BT848_DSTATUS_NUML)
  3277. ? "625" : "525");
  3278. printk("\n");
  3279. }
  3280. if (astat&BT848_INT_VSYNC)
  3281. btv->field_count++;
  3282. if (astat & BT848_INT_GPINT) {
  3283. wake_up(&btv->gpioq);
  3284. bttv_gpio_irq(&btv->c);
  3285. }
  3286. if (astat & BT848_INT_I2CDONE) {
  3287. btv->i2c_done = stat;
  3288. wake_up(&btv->i2c_queue);
  3289. }
  3290. if ((astat & BT848_INT_RISCI) && (stat & (4<<28)))
  3291. bttv_irq_switch_vbi(btv);
  3292. if ((astat & BT848_INT_RISCI) && (stat & (2<<28)))
  3293. bttv_irq_wakeup_top(btv);
  3294. if ((astat & BT848_INT_RISCI) && (stat & (1<<28)))
  3295. bttv_irq_switch_video(btv);
  3296. if ((astat & BT848_INT_HLOCK) && btv->opt_automute)
  3297. audio_mux(btv, -1);
  3298. if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
  3299. printk(KERN_INFO "bttv%d: %s%s @ %08x,",btv->c.nr,
  3300. (astat & BT848_INT_SCERR) ? "SCERR" : "",
  3301. (astat & BT848_INT_OCERR) ? "OCERR" : "",
  3302. btread(BT848_RISC_COUNT));
  3303. bttv_print_irqbits(stat,astat);
  3304. printk("\n");
  3305. if (bttv_debug)
  3306. bttv_print_riscaddr(btv);
  3307. }
  3308. if (fdsr && astat & BT848_INT_FDSR) {
  3309. printk(KERN_INFO "bttv%d: FDSR @ %08x\n",
  3310. btv->c.nr,btread(BT848_RISC_COUNT));
  3311. if (bttv_debug)
  3312. bttv_print_riscaddr(btv);
  3313. }
  3314. count++;
  3315. if (count > 4) {
  3316. btwrite(0, BT848_INT_MASK);
  3317. printk(KERN_ERR
  3318. "bttv%d: IRQ lockup, cleared int mask [", btv->c.nr);
  3319. bttv_print_irqbits(stat,astat);
  3320. printk("]\n");
  3321. }
  3322. }
  3323. btv->irq_total++;
  3324. if (handled)
  3325. btv->irq_me++;
  3326. return IRQ_RETVAL(handled);
  3327. }
  3328. /* ----------------------------------------------------------------------- */
  3329. /* initialitation */
  3330. static struct video_device *vdev_init(struct bttv *btv,
  3331. struct video_device *template,
  3332. char *type)
  3333. {
  3334. struct video_device *vfd;
  3335. vfd = video_device_alloc();
  3336. if (NULL == vfd)
  3337. return NULL;
  3338. *vfd = *template;
  3339. vfd->minor = -1;
  3340. vfd->dev = &btv->c.pci->dev;
  3341. vfd->release = video_device_release;
  3342. snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
  3343. btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
  3344. type, bttv_tvcards[btv->c.type].name);
  3345. return vfd;
  3346. }
  3347. static void bttv_unregister_video(struct bttv *btv)
  3348. {
  3349. if (btv->video_dev) {
  3350. if (-1 != btv->video_dev->minor)
  3351. video_unregister_device(btv->video_dev);
  3352. else
  3353. video_device_release(btv->video_dev);
  3354. btv->video_dev = NULL;
  3355. }
  3356. if (btv->vbi_dev) {
  3357. if (-1 != btv->vbi_dev->minor)
  3358. video_unregister_device(btv->vbi_dev);
  3359. else
  3360. video_device_release(btv->vbi_dev);
  3361. btv->vbi_dev = NULL;
  3362. }
  3363. if (btv->radio_dev) {
  3364. if (-1 != btv->radio_dev->minor)
  3365. video_unregister_device(btv->radio_dev);
  3366. else
  3367. video_device_release(btv->radio_dev);
  3368. btv->radio_dev = NULL;
  3369. }
  3370. }
  3371. /* register video4linux devices */
  3372. static int __devinit bttv_register_video(struct bttv *btv)
  3373. {
  3374. if (no_overlay <= 0) {
  3375. bttv_video_template.type |= VID_TYPE_OVERLAY;
  3376. } else {
  3377. printk("bttv: Overlay support disabled.\n");
  3378. }
  3379. /* video */
  3380. btv->video_dev = vdev_init(btv, &bttv_video_template, "video");
  3381. if (NULL == btv->video_dev)
  3382. goto err;
  3383. if (video_register_device(btv->video_dev,VFL_TYPE_GRABBER,video_nr)<0)
  3384. goto err;
  3385. printk(KERN_INFO "bttv%d: registered device video%d\n",
  3386. btv->c.nr,btv->video_dev->minor & 0x1f);
  3387. video_device_create_file(btv->video_dev, &class_device_attr_card);
  3388. /* vbi */
  3389. btv->vbi_dev = vdev_init(btv, &bttv_vbi_template, "vbi");
  3390. if (NULL == btv->vbi_dev)
  3391. goto err;
  3392. if (video_register_device(btv->vbi_dev,VFL_TYPE_VBI,vbi_nr)<0)
  3393. goto err;
  3394. printk(KERN_INFO "bttv%d: registered device vbi%d\n",
  3395. btv->c.nr,btv->vbi_dev->minor & 0x1f);
  3396. if (!btv->has_radio)
  3397. return 0;
  3398. /* radio */
  3399. btv->radio_dev = vdev_init(btv, &radio_template, "radio");
  3400. if (NULL == btv->radio_dev)
  3401. goto err;
  3402. if (video_register_device(btv->radio_dev, VFL_TYPE_RADIO,radio_nr)<0)
  3403. goto err;
  3404. printk(KERN_INFO "bttv%d: registered device radio%d\n",
  3405. btv->c.nr,btv->radio_dev->minor & 0x1f);
  3406. /* all done */
  3407. return 0;
  3408. err:
  3409. bttv_unregister_video(btv);
  3410. return -1;
  3411. }
  3412. /* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
  3413. /* response on cards with no firmware is not enabled by OF */
  3414. static void pci_set_command(struct pci_dev *dev)
  3415. {
  3416. #if defined(__powerpc__)
  3417. unsigned int cmd;
  3418. pci_read_config_dword(dev, PCI_COMMAND, &cmd);
  3419. cmd = (cmd | PCI_COMMAND_MEMORY );
  3420. pci_write_config_dword(dev, PCI_COMMAND, cmd);
  3421. #endif
  3422. }
  3423. static int __devinit bttv_probe(struct pci_dev *dev,
  3424. const struct pci_device_id *pci_id)
  3425. {
  3426. int result;
  3427. unsigned char lat;
  3428. struct bttv *btv;
  3429. if (bttv_num == BTTV_MAX)
  3430. return -ENOMEM;
  3431. printk(KERN_INFO "bttv: Bt8xx card found (%d).\n", bttv_num);
  3432. btv=&bttvs[bttv_num];
  3433. memset(btv,0,sizeof(*btv));
  3434. btv->c.nr = bttv_num;
  3435. sprintf(btv->c.name,"bttv%d",btv->c.nr);
  3436. /* initialize structs / fill in defaults */
  3437. init_MUTEX(&btv->lock);
  3438. init_MUTEX(&btv->reslock);
  3439. spin_lock_init(&btv->s_lock);
  3440. spin_lock_init(&btv->gpio_lock);
  3441. init_waitqueue_head(&btv->gpioq);
  3442. init_waitqueue_head(&btv->i2c_queue);
  3443. INIT_LIST_HEAD(&btv->c.subs);
  3444. INIT_LIST_HEAD(&btv->capture);
  3445. INIT_LIST_HEAD(&btv->vcapture);
  3446. v4l2_prio_init(&btv->prio);
  3447. init_timer(&btv->timeout);
  3448. btv->timeout.function = bttv_irq_timeout;
  3449. btv->timeout.data = (unsigned long)btv;
  3450. btv->i2c_rc = -1;
  3451. btv->tuner_type = UNSET;
  3452. btv->pinnacle_id = UNSET;
  3453. btv->new_input = UNSET;
  3454. btv->gpioirq = 1;
  3455. btv->has_radio=radio[btv->c.nr];
  3456. /* pci stuff (init, get irq/mmio, ... */
  3457. btv->c.pci = dev;
  3458. btv->id = dev->device;
  3459. if (pci_enable_device(dev)) {
  3460. printk(KERN_WARNING "bttv%d: Can't enable device.\n",
  3461. btv->c.nr);
  3462. return -EIO;
  3463. }
  3464. if (pci_set_dma_mask(dev, DMA_32BIT_MASK)) {
  3465. printk(KERN_WARNING "bttv%d: No suitable DMA available.\n",
  3466. btv->c.nr);
  3467. return -EIO;
  3468. }
  3469. if (!request_mem_region(pci_resource_start(dev,0),
  3470. pci_resource_len(dev,0),
  3471. btv->c.name)) {
  3472. printk(KERN_WARNING "bttv%d: can't request iomem (0x%lx).\n",
  3473. btv->c.nr, pci_resource_start(dev,0));
  3474. return -EBUSY;
  3475. }
  3476. pci_set_master(dev);
  3477. pci_set_command(dev);
  3478. pci_set_drvdata(dev,btv);
  3479. pci_read_config_byte(dev, PCI_CLASS_REVISION, &btv->revision);
  3480. pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
  3481. printk(KERN_INFO "bttv%d: Bt%d (rev %d) at %s, ",
  3482. bttv_num,btv->id, btv->revision, pci_name(dev));
  3483. printk("irq: %d, latency: %d, mmio: 0x%lx\n",
  3484. btv->c.pci->irq, lat, pci_resource_start(dev,0));
  3485. schedule();
  3486. btv->bt848_mmio=ioremap(pci_resource_start(dev,0), 0x1000);
  3487. if (NULL == ioremap(pci_resource_start(dev,0), 0x1000)) {
  3488. printk("bttv%d: ioremap() failed\n", btv->c.nr);
  3489. result = -EIO;
  3490. goto fail1;
  3491. }
  3492. /* identify card */
  3493. bttv_idcard(btv);
  3494. /* disable irqs, register irq handler */
  3495. btwrite(0, BT848_INT_MASK);
  3496. result = request_irq(btv->c.pci->irq, bttv_irq,
  3497. SA_SHIRQ | SA_INTERRUPT,btv->c.name,(void *)btv);
  3498. if (result < 0) {
  3499. printk(KERN_ERR "bttv%d: can't get IRQ %d\n",
  3500. bttv_num,btv->c.pci->irq);
  3501. goto fail1;
  3502. }
  3503. if (0 != bttv_handle_chipset(btv)) {
  3504. result = -EIO;
  3505. goto fail2;
  3506. }
  3507. /* init options from insmod args */
  3508. btv->opt_combfilter = combfilter;
  3509. btv->opt_lumafilter = lumafilter;
  3510. btv->opt_automute = automute;
  3511. btv->opt_chroma_agc = chroma_agc;
  3512. btv->opt_adc_crush = adc_crush;
  3513. btv->opt_vcr_hack = vcr_hack;
  3514. btv->opt_whitecrush_upper = whitecrush_upper;
  3515. btv->opt_whitecrush_lower = whitecrush_lower;
  3516. btv->opt_uv_ratio = uv_ratio;
  3517. btv->opt_full_luma_range = full_luma_range;
  3518. btv->opt_coring = coring;
  3519. /* fill struct bttv with some useful defaults */
  3520. btv->init.btv = btv;
  3521. btv->init.ov.w.width = 320;
  3522. btv->init.ov.w.height = 240;
  3523. btv->init.fmt = format_by_palette(VIDEO_PALETTE_RGB24);
  3524. btv->init.width = 320;
  3525. btv->init.height = 240;
  3526. btv->init.lines = 16;
  3527. btv->input = 0;
  3528. /* initialize hardware */
  3529. if (bttv_gpio)
  3530. bttv_gpio_tracking(btv,"pre-init");
  3531. bttv_risc_init_main(btv);
  3532. init_bt848(btv);
  3533. /* gpio */
  3534. btwrite(0x00, BT848_GPIO_REG_INP);
  3535. btwrite(0x00, BT848_GPIO_OUT_EN);
  3536. if (bttv_verbose)
  3537. bttv_gpio_tracking(btv,"init");
  3538. /* needs to be done before i2c is registered */
  3539. bttv_init_card1(btv);
  3540. /* register i2c + gpio */
  3541. init_bttv_i2c(btv);
  3542. /* some card-specific stuff (needs working i2c) */
  3543. bttv_init_card2(btv);
  3544. init_irqreg(btv);
  3545. /* register video4linux + input */
  3546. if (!bttv_tvcards[btv->c.type].no_video) {
  3547. bttv_register_video(btv);
  3548. bt848_bright(btv,32768);
  3549. bt848_contrast(btv,32768);
  3550. bt848_hue(btv,32768);
  3551. bt848_sat(btv,32768);
  3552. audio_mux(btv,AUDIO_MUTE);
  3553. set_input(btv,0);
  3554. }
  3555. /* add subdevices */
  3556. if (btv->has_remote)
  3557. bttv_sub_add_device(&btv->c, "remote");
  3558. if (bttv_tvcards[btv->c.type].has_dvb)
  3559. bttv_sub_add_device(&btv->c, "dvb");
  3560. /* everything is fine */
  3561. bttv_num++;
  3562. return 0;
  3563. fail2:
  3564. free_irq(btv->c.pci->irq,btv);
  3565. fail1:
  3566. if (btv->bt848_mmio)
  3567. iounmap(btv->bt848_mmio);
  3568. release_mem_region(pci_resource_start(btv->c.pci,0),
  3569. pci_resource_len(btv->c.pci,0));
  3570. pci_set_drvdata(dev,NULL);
  3571. return result;
  3572. }
  3573. static void __devexit bttv_remove(struct pci_dev *pci_dev)
  3574. {
  3575. struct bttv *btv = pci_get_drvdata(pci_dev);
  3576. if (bttv_verbose)
  3577. printk("bttv%d: unloading\n",btv->c.nr);
  3578. /* shutdown everything (DMA+IRQs) */
  3579. btand(~15, BT848_GPIO_DMA_CTL);
  3580. btwrite(0, BT848_INT_MASK);
  3581. btwrite(~0x0, BT848_INT_STAT);
  3582. btwrite(0x0, BT848_GPIO_OUT_EN);
  3583. if (bttv_gpio)
  3584. bttv_gpio_tracking(btv,"cleanup");
  3585. /* tell gpio modules we are leaving ... */
  3586. btv->shutdown=1;
  3587. wake_up(&btv->gpioq);
  3588. bttv_sub_del_devices(&btv->c);
  3589. /* unregister i2c_bus + input */
  3590. fini_bttv_i2c(btv);
  3591. /* unregister video4linux */
  3592. bttv_unregister_video(btv);
  3593. /* free allocated memory */
  3594. btcx_riscmem_free(btv->c.pci,&btv->main);
  3595. /* free ressources */
  3596. free_irq(btv->c.pci->irq,btv);
  3597. iounmap(btv->bt848_mmio);
  3598. release_mem_region(pci_resource_start(btv->c.pci,0),
  3599. pci_resource_len(btv->c.pci,0));
  3600. pci_set_drvdata(pci_dev, NULL);
  3601. return;
  3602. }
  3603. static int bttv_suspend(struct pci_dev *pci_dev, pm_message_t state)
  3604. {
  3605. struct bttv *btv = pci_get_drvdata(pci_dev);
  3606. struct bttv_buffer_set idle;
  3607. unsigned long flags;
  3608. /* stop dma + irqs */
  3609. spin_lock_irqsave(&btv->s_lock,flags);
  3610. memset(&idle, 0, sizeof(idle));
  3611. btv->state.video = btv->curr;
  3612. btv->state.vbi = btv->cvbi;
  3613. btv->state.loop_irq = btv->loop_irq;
  3614. btv->curr = idle;
  3615. btv->loop_irq = 0;
  3616. bttv_buffer_activate_video(btv, &idle);
  3617. bttv_buffer_activate_vbi(btv, NULL);
  3618. bttv_set_dma(btv, 0);
  3619. btwrite(0, BT848_INT_MASK);
  3620. spin_unlock_irqrestore(&btv->s_lock,flags);
  3621. /* save bt878 state */
  3622. btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN);
  3623. btv->state.gpio_data = gpio_read();
  3624. /* save pci state */
  3625. pci_save_state(pci_dev);
  3626. if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
  3627. pci_disable_device(pci_dev);
  3628. btv->state.disabled = 1;
  3629. }
  3630. return 0;
  3631. }
  3632. static int bttv_resume(struct pci_dev *pci_dev)
  3633. {
  3634. struct bttv *btv = pci_get_drvdata(pci_dev);
  3635. unsigned long flags;
  3636. dprintk("bttv%d: resume\n", btv->c.nr);
  3637. /* restore pci state */
  3638. if (btv->state.disabled) {
  3639. pci_enable_device(pci_dev);
  3640. btv->state.disabled = 0;
  3641. }
  3642. pci_set_power_state(pci_dev, PCI_D0);
  3643. pci_restore_state(pci_dev);
  3644. /* restore bt878 state */
  3645. bttv_reinit_bt848(btv);
  3646. gpio_inout(0xffffff, btv->state.gpio_enable);
  3647. gpio_write(btv->state.gpio_data);
  3648. /* restart dma */
  3649. spin_lock_irqsave(&btv->s_lock,flags);
  3650. btv->curr = btv->state.video;
  3651. btv->cvbi = btv->state.vbi;
  3652. btv->loop_irq = btv->state.loop_irq;
  3653. bttv_buffer_activate_video(btv, &btv->curr);
  3654. bttv_buffer_activate_vbi(btv, btv->cvbi);
  3655. bttv_set_dma(btv, 0);
  3656. spin_unlock_irqrestore(&btv->s_lock,flags);
  3657. return 0;
  3658. }
  3659. static struct pci_device_id bttv_pci_tbl[] = {
  3660. {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT848,
  3661. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  3662. {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT849,
  3663. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  3664. {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT878,
  3665. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  3666. {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT879,
  3667. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  3668. {0,}
  3669. };
  3670. MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
  3671. static struct pci_driver bttv_pci_driver = {
  3672. .name = "bttv",
  3673. .id_table = bttv_pci_tbl,
  3674. .probe = bttv_probe,
  3675. .remove = __devexit_p(bttv_remove),
  3676. .suspend = bttv_suspend,
  3677. .resume = bttv_resume,
  3678. };
  3679. static int bttv_init_module(void)
  3680. {
  3681. bttv_num = 0;
  3682. printk(KERN_INFO "bttv: driver version %d.%d.%d loaded\n",
  3683. (BTTV_VERSION_CODE >> 16) & 0xff,
  3684. (BTTV_VERSION_CODE >> 8) & 0xff,
  3685. BTTV_VERSION_CODE & 0xff);
  3686. #ifdef SNAPSHOT
  3687. printk(KERN_INFO "bttv: snapshot date %04d-%02d-%02d\n",
  3688. SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
  3689. #endif
  3690. if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
  3691. gbuffers = 2;
  3692. if (gbufsize < 0 || gbufsize > BTTV_MAX_FBUF)
  3693. gbufsize = BTTV_MAX_FBUF;
  3694. gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
  3695. if (bttv_verbose)
  3696. printk(KERN_INFO "bttv: using %d buffers with %dk (%d pages) each for capture\n",
  3697. gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
  3698. bttv_check_chipset();
  3699. bus_register(&bttv_sub_bus_type);
  3700. return pci_module_init(&bttv_pci_driver);
  3701. }
  3702. static void bttv_cleanup_module(void)
  3703. {
  3704. pci_unregister_driver(&bttv_pci_driver);
  3705. bus_unregister(&bttv_sub_bus_type);
  3706. return;
  3707. }
  3708. module_init(bttv_init_module);
  3709. module_exit(bttv_cleanup_module);
  3710. /*
  3711. * Local variables:
  3712. * c-basic-offset: 8
  3713. * End:
  3714. */