vino.c 105 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680
  1. /*
  2. * Driver for the VINO (Video In No Out) system found in SGI Indys.
  3. *
  4. * This file is subject to the terms and conditions of the GNU General Public
  5. * License version 2 as published by the Free Software Foundation.
  6. *
  7. * Copyright (C) 2004,2005 Mikael Nousiainen <tmnousia@cc.hut.fi>
  8. *
  9. * Based on the previous version of the driver for 2.4 kernels by:
  10. * Copyright (C) 2003 Ladislav Michl <ladis@linux-mips.org>
  11. */
  12. /*
  13. * TODO:
  14. * - remove "mark pages reserved-hacks" from memory allocation code
  15. * and implement nopage()
  16. * - check decimation, calculating and reporting image size when
  17. * using decimation
  18. * - implement read(), user mode buffers and overlay (?)
  19. */
  20. #include <linux/init.h>
  21. #include <linux/module.h>
  22. #include <linux/delay.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/errno.h>
  25. #include <linux/fs.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/kernel.h>
  28. #include <linux/mm.h>
  29. #include <linux/moduleparam.h>
  30. #include <linux/time.h>
  31. #include <linux/version.h>
  32. #ifdef CONFIG_KMOD
  33. #include <linux/kmod.h>
  34. #endif
  35. #include <linux/i2c.h>
  36. #include <linux/i2c-algo-sgi.h>
  37. #include <linux/videodev.h>
  38. #include <linux/videodev2.h>
  39. #include <linux/video_decoder.h>
  40. #include <linux/mutex.h>
  41. #include <asm/paccess.h>
  42. #include <asm/io.h>
  43. #include <asm/sgi/ip22.h>
  44. #include <asm/sgi/mc.h>
  45. #include "vino.h"
  46. #include "saa7191.h"
  47. #include "indycam.h"
  48. /* Uncomment the following line to get lots and lots of (mostly useless)
  49. * debug info.
  50. * Note that the debug output also slows down the driver significantly */
  51. // #define VINO_DEBUG
  52. // #define VINO_DEBUG_INT
  53. #define VINO_MODULE_VERSION "0.0.5"
  54. #define VINO_VERSION_CODE KERNEL_VERSION(0, 0, 5)
  55. MODULE_DESCRIPTION("SGI VINO Video4Linux2 driver");
  56. MODULE_VERSION(VINO_MODULE_VERSION);
  57. MODULE_AUTHOR("Mikael Nousiainen <tmnousia@cc.hut.fi>");
  58. MODULE_LICENSE("GPL");
  59. #ifdef VINO_DEBUG
  60. #define dprintk(x...) printk("VINO: " x);
  61. #else
  62. #define dprintk(x...)
  63. #endif
  64. #define VINO_NO_CHANNEL 0
  65. #define VINO_CHANNEL_A 1
  66. #define VINO_CHANNEL_B 2
  67. #define VINO_PAL_WIDTH 768
  68. #define VINO_PAL_HEIGHT 576
  69. #define VINO_NTSC_WIDTH 640
  70. #define VINO_NTSC_HEIGHT 480
  71. #define VINO_MIN_WIDTH 32
  72. #define VINO_MIN_HEIGHT 32
  73. #define VINO_CLIPPING_START_ODD_D1 1
  74. #define VINO_CLIPPING_START_ODD_PAL 15
  75. #define VINO_CLIPPING_START_ODD_NTSC 12
  76. #define VINO_CLIPPING_START_EVEN_D1 2
  77. #define VINO_CLIPPING_START_EVEN_PAL 15
  78. #define VINO_CLIPPING_START_EVEN_NTSC 12
  79. #define VINO_INPUT_CHANNEL_COUNT 3
  80. /* the number is the index for vino_inputs */
  81. #define VINO_INPUT_NONE -1
  82. #define VINO_INPUT_COMPOSITE 0
  83. #define VINO_INPUT_SVIDEO 1
  84. #define VINO_INPUT_D1 2
  85. #define VINO_PAGE_RATIO (PAGE_SIZE / VINO_PAGE_SIZE)
  86. #define VINO_FIFO_THRESHOLD_DEFAULT 16
  87. #define VINO_FRAMEBUFFER_SIZE ((VINO_PAL_WIDTH \
  88. * VINO_PAL_HEIGHT * 4 \
  89. + 3 * PAGE_SIZE) & ~(PAGE_SIZE - 1))
  90. #define VINO_FRAMEBUFFER_COUNT_MAX 8
  91. #define VINO_FRAMEBUFFER_UNUSED 0
  92. #define VINO_FRAMEBUFFER_IN_USE 1
  93. #define VINO_FRAMEBUFFER_READY 2
  94. #define VINO_QUEUE_ERROR -1
  95. #define VINO_QUEUE_MAGIC 0x20050125
  96. #define VINO_MEMORY_NONE 0
  97. #define VINO_MEMORY_MMAP 1
  98. #define VINO_MEMORY_USERPTR 2
  99. #define VINO_DUMMY_DESC_COUNT 4
  100. #define VINO_DESC_FETCH_DELAY 5 /* microseconds */
  101. #define VINO_MAX_FRAME_SKIP_COUNT 128
  102. /* the number is the index for vino_data_formats */
  103. #define VINO_DATA_FMT_NONE -1
  104. #define VINO_DATA_FMT_GREY 0
  105. #define VINO_DATA_FMT_RGB332 1
  106. #define VINO_DATA_FMT_RGB32 2
  107. #define VINO_DATA_FMT_YUV 3
  108. #define VINO_DATA_FMT_COUNT 4
  109. /* the number is the index for vino_data_norms */
  110. #define VINO_DATA_NORM_NONE -1
  111. #define VINO_DATA_NORM_NTSC 0
  112. #define VINO_DATA_NORM_PAL 1
  113. #define VINO_DATA_NORM_SECAM 2
  114. #define VINO_DATA_NORM_D1 3
  115. /* The following are special entries that can be used to
  116. * autodetect the norm. */
  117. #define VINO_DATA_NORM_AUTO 0xfe
  118. #define VINO_DATA_NORM_AUTO_EXT 0xff
  119. #define VINO_DATA_NORM_COUNT 4
  120. /* Internal data structure definitions */
  121. struct vino_input {
  122. char *name;
  123. v4l2_std_id std;
  124. };
  125. struct vino_clipping {
  126. unsigned int left, right, top, bottom;
  127. };
  128. struct vino_data_format {
  129. /* the description */
  130. char *description;
  131. /* bytes per pixel */
  132. unsigned int bpp;
  133. /* V4L2 fourcc code */
  134. __u32 pixelformat;
  135. /* V4L2 colorspace (duh!) */
  136. enum v4l2_colorspace colorspace;
  137. };
  138. struct vino_data_norm {
  139. char *description;
  140. unsigned int width, height;
  141. struct vino_clipping odd;
  142. struct vino_clipping even;
  143. v4l2_std_id std;
  144. unsigned int fps_min, fps_max;
  145. __u32 framelines;
  146. };
  147. struct vino_descriptor_table {
  148. /* the number of PAGE_SIZE sized pages in the buffer */
  149. unsigned int page_count;
  150. /* virtual (kmalloc'd) pointers to the actual data
  151. * (in PAGE_SIZE chunks, used with mmap streaming) */
  152. unsigned long *virtual;
  153. /* cpu address for the VINO descriptor table
  154. * (contains DMA addresses, VINO_PAGE_SIZE chunks) */
  155. unsigned long *dma_cpu;
  156. /* dma address for the VINO descriptor table
  157. * (contains DMA addresses, VINO_PAGE_SIZE chunks) */
  158. dma_addr_t dma;
  159. };
  160. struct vino_framebuffer {
  161. /* identifier nubmer */
  162. unsigned int id;
  163. /* the length of the whole buffer */
  164. unsigned int size;
  165. /* the length of actual data in buffer */
  166. unsigned int data_size;
  167. /* the data format */
  168. unsigned int data_format;
  169. /* the state of buffer data */
  170. unsigned int state;
  171. /* is the buffer mapped in user space? */
  172. unsigned int map_count;
  173. /* memory offset for mmap() */
  174. unsigned int offset;
  175. /* frame counter */
  176. unsigned int frame_counter;
  177. /* timestamp (written when image capture finishes) */
  178. struct timeval timestamp;
  179. struct vino_descriptor_table desc_table;
  180. spinlock_t state_lock;
  181. };
  182. struct vino_framebuffer_fifo {
  183. unsigned int length;
  184. unsigned int used;
  185. unsigned int head;
  186. unsigned int tail;
  187. unsigned int data[VINO_FRAMEBUFFER_COUNT_MAX];
  188. };
  189. struct vino_framebuffer_queue {
  190. unsigned int magic;
  191. /* VINO_MEMORY_NONE, VINO_MEMORY_MMAP or VINO_MEMORY_USERPTR */
  192. unsigned int type;
  193. unsigned int length;
  194. /* data field of in and out contain index numbers for buffer */
  195. struct vino_framebuffer_fifo in;
  196. struct vino_framebuffer_fifo out;
  197. struct vino_framebuffer *buffer[VINO_FRAMEBUFFER_COUNT_MAX];
  198. spinlock_t queue_lock;
  199. struct mutex queue_mutex;
  200. wait_queue_head_t frame_wait_queue;
  201. };
  202. struct vino_interrupt_data {
  203. struct timeval timestamp;
  204. unsigned int frame_counter;
  205. unsigned int skip_count;
  206. unsigned int skip;
  207. };
  208. struct vino_channel_settings {
  209. unsigned int channel;
  210. int input;
  211. unsigned int data_format;
  212. unsigned int data_norm;
  213. struct vino_clipping clipping;
  214. unsigned int decimation;
  215. unsigned int line_size;
  216. unsigned int alpha;
  217. unsigned int fps;
  218. unsigned int framert_reg;
  219. unsigned int fifo_threshold;
  220. struct vino_framebuffer_queue fb_queue;
  221. /* number of the current field */
  222. unsigned int field;
  223. /* read in progress */
  224. int reading;
  225. /* streaming is active */
  226. int streaming;
  227. /* the driver is currently processing the queue */
  228. int capturing;
  229. struct mutex mutex;
  230. spinlock_t capture_lock;
  231. unsigned int users;
  232. struct vino_interrupt_data int_data;
  233. /* V4L support */
  234. struct video_device *v4l_device;
  235. };
  236. struct vino_client {
  237. /* the channel which owns this client:
  238. * VINO_NO_CHANNEL, VINO_CHANNEL_A or VINO_CHANNEL_B */
  239. unsigned int owner;
  240. struct i2c_client *driver;
  241. };
  242. struct vino_settings {
  243. struct vino_channel_settings a;
  244. struct vino_channel_settings b;
  245. struct vino_client decoder;
  246. struct vino_client camera;
  247. /* a lock for vino register access */
  248. spinlock_t vino_lock;
  249. /* a lock for channel input changes */
  250. spinlock_t input_lock;
  251. unsigned long dummy_page;
  252. struct vino_descriptor_table dummy_desc_table;
  253. };
  254. /* Module parameters */
  255. /*
  256. * Using vino_pixel_conversion the ABGR32-format pixels supplied
  257. * by the VINO chip can be converted to more common formats
  258. * like RGBA32 (or probably RGB24 in the future). This way we
  259. * can give out data that can be specified correctly with
  260. * the V4L2-definitions.
  261. *
  262. * The pixel format is specified as RGBA32 when no conversion
  263. * is used.
  264. *
  265. * Note that this only affects the 32-bit bit depth.
  266. *
  267. * Use non-zero value to enable conversion.
  268. */
  269. static int vino_pixel_conversion = 0;
  270. module_param_named(pixelconv, vino_pixel_conversion, int, 0);
  271. MODULE_PARM_DESC(pixelconv,
  272. "enable pixel conversion (non-zero value enables)");
  273. /* Internal data structures */
  274. static struct sgi_vino *vino;
  275. static struct vino_settings *vino_drvdata;
  276. static const char *vino_driver_name = "vino";
  277. static const char *vino_driver_description = "SGI VINO";
  278. static const char *vino_bus_name = "GIO64 bus";
  279. static const char *vino_v4l_device_name_a = "SGI VINO Channel A";
  280. static const char *vino_v4l_device_name_b = "SGI VINO Channel B";
  281. static void vino_capture_tasklet(unsigned long channel);
  282. DECLARE_TASKLET(vino_tasklet_a, vino_capture_tasklet, VINO_CHANNEL_A);
  283. DECLARE_TASKLET(vino_tasklet_b, vino_capture_tasklet, VINO_CHANNEL_B);
  284. static const struct vino_input vino_inputs[] = {
  285. {
  286. .name = "Composite",
  287. .std = V4L2_STD_NTSC | V4L2_STD_PAL
  288. | V4L2_STD_SECAM,
  289. },{
  290. .name = "S-Video",
  291. .std = V4L2_STD_NTSC | V4L2_STD_PAL
  292. | V4L2_STD_SECAM,
  293. },{
  294. .name = "D1/IndyCam",
  295. .std = V4L2_STD_NTSC,
  296. }
  297. };
  298. static const struct vino_data_format vino_data_formats[] = {
  299. {
  300. .description = "8-bit greyscale",
  301. .bpp = 1,
  302. .pixelformat = V4L2_PIX_FMT_GREY,
  303. .colorspace = V4L2_COLORSPACE_SMPTE170M,
  304. },{
  305. .description = "8-bit dithered RGB 3-3-2",
  306. .bpp = 1,
  307. .pixelformat = V4L2_PIX_FMT_RGB332,
  308. .colorspace = V4L2_COLORSPACE_SRGB,
  309. },{
  310. .description = "32-bit RGB",
  311. .bpp = 4,
  312. .pixelformat = V4L2_PIX_FMT_RGB32,
  313. .colorspace = V4L2_COLORSPACE_SRGB,
  314. },{
  315. .description = "YUV 4:2:2",
  316. .bpp = 2,
  317. .pixelformat = V4L2_PIX_FMT_YUYV, // XXX: swapped?
  318. .colorspace = V4L2_COLORSPACE_SMPTE170M,
  319. }
  320. };
  321. static const struct vino_data_norm vino_data_norms[] = {
  322. {
  323. .description = "NTSC",
  324. .std = V4L2_STD_NTSC,
  325. .fps_min = 6,
  326. .fps_max = 30,
  327. .framelines = 525,
  328. .width = VINO_NTSC_WIDTH,
  329. .height = VINO_NTSC_HEIGHT,
  330. .odd = {
  331. .top = VINO_CLIPPING_START_ODD_NTSC,
  332. .left = 0,
  333. .bottom = VINO_CLIPPING_START_ODD_NTSC
  334. + VINO_NTSC_HEIGHT / 2 - 1,
  335. .right = VINO_NTSC_WIDTH,
  336. },
  337. .even = {
  338. .top = VINO_CLIPPING_START_EVEN_NTSC,
  339. .left = 0,
  340. .bottom = VINO_CLIPPING_START_EVEN_NTSC
  341. + VINO_NTSC_HEIGHT / 2 - 1,
  342. .right = VINO_NTSC_WIDTH,
  343. },
  344. },{
  345. .description = "PAL",
  346. .std = V4L2_STD_PAL,
  347. .fps_min = 5,
  348. .fps_max = 25,
  349. .framelines = 625,
  350. .width = VINO_PAL_WIDTH,
  351. .height = VINO_PAL_HEIGHT,
  352. .odd = {
  353. .top = VINO_CLIPPING_START_ODD_PAL,
  354. .left = 0,
  355. .bottom = VINO_CLIPPING_START_ODD_PAL
  356. + VINO_PAL_HEIGHT / 2 - 1,
  357. .right = VINO_PAL_WIDTH,
  358. },
  359. .even = {
  360. .top = VINO_CLIPPING_START_EVEN_PAL,
  361. .left = 0,
  362. .bottom = VINO_CLIPPING_START_EVEN_PAL
  363. + VINO_PAL_HEIGHT / 2 - 1,
  364. .right = VINO_PAL_WIDTH,
  365. },
  366. },{
  367. .description = "SECAM",
  368. .std = V4L2_STD_SECAM,
  369. .fps_min = 5,
  370. .fps_max = 25,
  371. .framelines = 625,
  372. .width = VINO_PAL_WIDTH,
  373. .height = VINO_PAL_HEIGHT,
  374. .odd = {
  375. .top = VINO_CLIPPING_START_ODD_PAL,
  376. .left = 0,
  377. .bottom = VINO_CLIPPING_START_ODD_PAL
  378. + VINO_PAL_HEIGHT / 2 - 1,
  379. .right = VINO_PAL_WIDTH,
  380. },
  381. .even = {
  382. .top = VINO_CLIPPING_START_EVEN_PAL,
  383. .left = 0,
  384. .bottom = VINO_CLIPPING_START_EVEN_PAL
  385. + VINO_PAL_HEIGHT / 2 - 1,
  386. .right = VINO_PAL_WIDTH,
  387. },
  388. },{
  389. .description = "NTSC/D1",
  390. .std = V4L2_STD_NTSC,
  391. .fps_min = 6,
  392. .fps_max = 30,
  393. .framelines = 525,
  394. .width = VINO_NTSC_WIDTH,
  395. .height = VINO_NTSC_HEIGHT,
  396. .odd = {
  397. .top = VINO_CLIPPING_START_ODD_D1,
  398. .left = 0,
  399. .bottom = VINO_CLIPPING_START_ODD_D1
  400. + VINO_NTSC_HEIGHT / 2 - 1,
  401. .right = VINO_NTSC_WIDTH,
  402. },
  403. .even = {
  404. .top = VINO_CLIPPING_START_EVEN_D1,
  405. .left = 0,
  406. .bottom = VINO_CLIPPING_START_EVEN_D1
  407. + VINO_NTSC_HEIGHT / 2 - 1,
  408. .right = VINO_NTSC_WIDTH,
  409. },
  410. }
  411. };
  412. #define VINO_INDYCAM_V4L2_CONTROL_COUNT 9
  413. struct v4l2_queryctrl vino_indycam_v4l2_controls[] = {
  414. {
  415. .id = V4L2_CID_AUTOGAIN,
  416. .type = V4L2_CTRL_TYPE_BOOLEAN,
  417. .name = "Automatic Gain Control",
  418. .minimum = 0,
  419. .maximum = 1,
  420. .step = 1,
  421. .default_value = INDYCAM_AGC_DEFAULT,
  422. .flags = 0,
  423. .reserved = { INDYCAM_CONTROL_AGC, 0 },
  424. },{
  425. .id = V4L2_CID_AUTO_WHITE_BALANCE,
  426. .type = V4L2_CTRL_TYPE_BOOLEAN,
  427. .name = "Automatic White Balance",
  428. .minimum = 0,
  429. .maximum = 1,
  430. .step = 1,
  431. .default_value = INDYCAM_AWB_DEFAULT,
  432. .flags = 0,
  433. .reserved = { INDYCAM_CONTROL_AWB, 0 },
  434. },{
  435. .id = V4L2_CID_GAIN,
  436. .type = V4L2_CTRL_TYPE_INTEGER,
  437. .name = "Gain",
  438. .minimum = INDYCAM_GAIN_MIN,
  439. .maximum = INDYCAM_GAIN_MAX,
  440. .step = 1,
  441. .default_value = INDYCAM_GAIN_DEFAULT,
  442. .flags = 0,
  443. .reserved = { INDYCAM_CONTROL_GAIN, 0 },
  444. },{
  445. .id = V4L2_CID_PRIVATE_BASE,
  446. .type = V4L2_CTRL_TYPE_INTEGER,
  447. .name = "Red Saturation",
  448. .minimum = INDYCAM_RED_SATURATION_MIN,
  449. .maximum = INDYCAM_RED_SATURATION_MAX,
  450. .step = 1,
  451. .default_value = INDYCAM_RED_SATURATION_DEFAULT,
  452. .flags = 0,
  453. .reserved = { INDYCAM_CONTROL_RED_SATURATION, 0 },
  454. },{
  455. .id = V4L2_CID_PRIVATE_BASE + 1,
  456. .type = V4L2_CTRL_TYPE_INTEGER,
  457. .name = "Blue Saturation",
  458. .minimum = INDYCAM_BLUE_SATURATION_MIN,
  459. .maximum = INDYCAM_BLUE_SATURATION_MAX,
  460. .step = 1,
  461. .default_value = INDYCAM_BLUE_SATURATION_DEFAULT,
  462. .flags = 0,
  463. .reserved = { INDYCAM_CONTROL_BLUE_SATURATION, 0 },
  464. },{
  465. .id = V4L2_CID_RED_BALANCE,
  466. .type = V4L2_CTRL_TYPE_INTEGER,
  467. .name = "Red Balance",
  468. .minimum = INDYCAM_RED_BALANCE_MIN,
  469. .maximum = INDYCAM_RED_BALANCE_MAX,
  470. .step = 1,
  471. .default_value = INDYCAM_RED_BALANCE_DEFAULT,
  472. .flags = 0,
  473. .reserved = { INDYCAM_CONTROL_RED_BALANCE, 0 },
  474. },{
  475. .id = V4L2_CID_BLUE_BALANCE,
  476. .type = V4L2_CTRL_TYPE_INTEGER,
  477. .name = "Blue Balance",
  478. .minimum = INDYCAM_BLUE_BALANCE_MIN,
  479. .maximum = INDYCAM_BLUE_BALANCE_MAX,
  480. .step = 1,
  481. .default_value = INDYCAM_BLUE_BALANCE_DEFAULT,
  482. .flags = 0,
  483. .reserved = { INDYCAM_CONTROL_BLUE_BALANCE, 0 },
  484. },{
  485. .id = V4L2_CID_EXPOSURE,
  486. .type = V4L2_CTRL_TYPE_INTEGER,
  487. .name = "Shutter Control",
  488. .minimum = INDYCAM_SHUTTER_MIN,
  489. .maximum = INDYCAM_SHUTTER_MAX,
  490. .step = 1,
  491. .default_value = INDYCAM_SHUTTER_DEFAULT,
  492. .flags = 0,
  493. .reserved = { INDYCAM_CONTROL_SHUTTER, 0 },
  494. },{
  495. .id = V4L2_CID_GAMMA,
  496. .type = V4L2_CTRL_TYPE_INTEGER,
  497. .name = "Gamma",
  498. .minimum = INDYCAM_GAMMA_MIN,
  499. .maximum = INDYCAM_GAMMA_MAX,
  500. .step = 1,
  501. .default_value = INDYCAM_GAMMA_DEFAULT,
  502. .flags = 0,
  503. .reserved = { INDYCAM_CONTROL_GAMMA, 0 },
  504. }
  505. };
  506. #define VINO_SAA7191_V4L2_CONTROL_COUNT 9
  507. struct v4l2_queryctrl vino_saa7191_v4l2_controls[] = {
  508. {
  509. .id = V4L2_CID_HUE,
  510. .type = V4L2_CTRL_TYPE_INTEGER,
  511. .name = "Hue",
  512. .minimum = SAA7191_HUE_MIN,
  513. .maximum = SAA7191_HUE_MAX,
  514. .step = 1,
  515. .default_value = SAA7191_HUE_DEFAULT,
  516. .flags = 0,
  517. .reserved = { SAA7191_CONTROL_HUE, 0 },
  518. },{
  519. .id = V4L2_CID_PRIVATE_BASE,
  520. .type = V4L2_CTRL_TYPE_INTEGER,
  521. .name = "Luminance Bandpass",
  522. .minimum = SAA7191_BANDPASS_MIN,
  523. .maximum = SAA7191_BANDPASS_MAX,
  524. .step = 1,
  525. .default_value = SAA7191_BANDPASS_DEFAULT,
  526. .flags = 0,
  527. .reserved = { SAA7191_CONTROL_BANDPASS, 0 },
  528. },{
  529. .id = V4L2_CID_PRIVATE_BASE + 1,
  530. .type = V4L2_CTRL_TYPE_INTEGER,
  531. .name = "Luminance Bandpass Weight",
  532. .minimum = SAA7191_BANDPASS_WEIGHT_MIN,
  533. .maximum = SAA7191_BANDPASS_WEIGHT_MAX,
  534. .step = 1,
  535. .default_value = SAA7191_BANDPASS_WEIGHT_DEFAULT,
  536. .flags = 0,
  537. .reserved = { SAA7191_CONTROL_BANDPASS_WEIGHT, 0 },
  538. },{
  539. .id = V4L2_CID_PRIVATE_BASE + 2,
  540. .type = V4L2_CTRL_TYPE_INTEGER,
  541. .name = "HF Luminance Coring",
  542. .minimum = SAA7191_CORING_MIN,
  543. .maximum = SAA7191_CORING_MAX,
  544. .step = 1,
  545. .default_value = SAA7191_CORING_DEFAULT,
  546. .flags = 0,
  547. .reserved = { SAA7191_CONTROL_CORING, 0 },
  548. },{
  549. .id = V4L2_CID_PRIVATE_BASE + 3,
  550. .type = V4L2_CTRL_TYPE_BOOLEAN,
  551. .name = "Force Colour",
  552. .minimum = SAA7191_FORCE_COLOUR_MIN,
  553. .maximum = SAA7191_FORCE_COLOUR_MAX,
  554. .step = 1,
  555. .default_value = SAA7191_FORCE_COLOUR_DEFAULT,
  556. .flags = 0,
  557. .reserved = { SAA7191_CONTROL_FORCE_COLOUR, 0 },
  558. },{
  559. .id = V4L2_CID_PRIVATE_BASE + 4,
  560. .type = V4L2_CTRL_TYPE_INTEGER,
  561. .name = "Chrominance Gain Control",
  562. .minimum = SAA7191_CHROMA_GAIN_MIN,
  563. .maximum = SAA7191_CHROMA_GAIN_MAX,
  564. .step = 1,
  565. .default_value = SAA7191_CHROMA_GAIN_DEFAULT,
  566. .flags = 0,
  567. .reserved = { SAA7191_CONTROL_CHROMA_GAIN, 0 },
  568. },{
  569. .id = V4L2_CID_PRIVATE_BASE + 5,
  570. .type = V4L2_CTRL_TYPE_BOOLEAN,
  571. .name = "VTR Time Constant",
  572. .minimum = SAA7191_VTRC_MIN,
  573. .maximum = SAA7191_VTRC_MAX,
  574. .step = 1,
  575. .default_value = SAA7191_VTRC_DEFAULT,
  576. .flags = 0,
  577. .reserved = { SAA7191_CONTROL_VTRC, 0 },
  578. },{
  579. .id = V4L2_CID_PRIVATE_BASE + 6,
  580. .type = V4L2_CTRL_TYPE_INTEGER,
  581. .name = "Luminance Delay Compensation",
  582. .minimum = SAA7191_LUMA_DELAY_MIN,
  583. .maximum = SAA7191_LUMA_DELAY_MAX,
  584. .step = 1,
  585. .default_value = SAA7191_LUMA_DELAY_DEFAULT,
  586. .flags = 0,
  587. .reserved = { SAA7191_CONTROL_LUMA_DELAY, 0 },
  588. },{
  589. .id = V4L2_CID_PRIVATE_BASE + 7,
  590. .type = V4L2_CTRL_TYPE_INTEGER,
  591. .name = "Vertical Noise Reduction",
  592. .minimum = SAA7191_VNR_MIN,
  593. .maximum = SAA7191_VNR_MAX,
  594. .step = 1,
  595. .default_value = SAA7191_VNR_DEFAULT,
  596. .flags = 0,
  597. .reserved = { SAA7191_CONTROL_VNR, 0 },
  598. }
  599. };
  600. /* VINO I2C bus functions */
  601. unsigned i2c_vino_getctrl(void *data)
  602. {
  603. return vino->i2c_control;
  604. }
  605. void i2c_vino_setctrl(void *data, unsigned val)
  606. {
  607. vino->i2c_control = val;
  608. }
  609. unsigned i2c_vino_rdata(void *data)
  610. {
  611. return vino->i2c_data;
  612. }
  613. void i2c_vino_wdata(void *data, unsigned val)
  614. {
  615. vino->i2c_data = val;
  616. }
  617. static struct i2c_algo_sgi_data i2c_sgi_vino_data =
  618. {
  619. .getctrl = &i2c_vino_getctrl,
  620. .setctrl = &i2c_vino_setctrl,
  621. .rdata = &i2c_vino_rdata,
  622. .wdata = &i2c_vino_wdata,
  623. .xfer_timeout = 200,
  624. .ack_timeout = 1000,
  625. };
  626. /*
  627. * There are two possible clients on VINO I2C bus, so we limit usage only
  628. * to them.
  629. */
  630. static int i2c_vino_client_reg(struct i2c_client *client)
  631. {
  632. unsigned long flags;
  633. int ret = 0;
  634. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  635. switch (client->driver->id) {
  636. case I2C_DRIVERID_SAA7191:
  637. if (vino_drvdata->decoder.driver)
  638. ret = -EBUSY;
  639. else
  640. vino_drvdata->decoder.driver = client;
  641. break;
  642. case I2C_DRIVERID_INDYCAM:
  643. if (vino_drvdata->camera.driver)
  644. ret = -EBUSY;
  645. else
  646. vino_drvdata->camera.driver = client;
  647. break;
  648. default:
  649. ret = -ENODEV;
  650. }
  651. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  652. return ret;
  653. }
  654. static int i2c_vino_client_unreg(struct i2c_client *client)
  655. {
  656. unsigned long flags;
  657. int ret = 0;
  658. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  659. if (client == vino_drvdata->decoder.driver) {
  660. if (vino_drvdata->decoder.owner != VINO_NO_CHANNEL)
  661. ret = -EBUSY;
  662. else
  663. vino_drvdata->decoder.driver = NULL;
  664. } else if (client == vino_drvdata->camera.driver) {
  665. if (vino_drvdata->camera.owner != VINO_NO_CHANNEL)
  666. ret = -EBUSY;
  667. else
  668. vino_drvdata->camera.driver = NULL;
  669. }
  670. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  671. return ret;
  672. }
  673. static struct i2c_adapter vino_i2c_adapter =
  674. {
  675. .name = "VINO I2C bus",
  676. .id = I2C_HW_SGI_VINO,
  677. .algo_data = &i2c_sgi_vino_data,
  678. .client_register = &i2c_vino_client_reg,
  679. .client_unregister = &i2c_vino_client_unreg,
  680. };
  681. static int vino_i2c_add_bus(void)
  682. {
  683. return i2c_sgi_add_bus(&vino_i2c_adapter);
  684. }
  685. static int vino_i2c_del_bus(void)
  686. {
  687. return i2c_sgi_del_bus(&vino_i2c_adapter);
  688. }
  689. static int i2c_camera_command(unsigned int cmd, void *arg)
  690. {
  691. return vino_drvdata->camera.driver->
  692. driver->command(vino_drvdata->camera.driver,
  693. cmd, arg);
  694. }
  695. static int i2c_decoder_command(unsigned int cmd, void *arg)
  696. {
  697. return vino_drvdata->decoder.driver->
  698. driver->command(vino_drvdata->decoder.driver,
  699. cmd, arg);
  700. }
  701. /* VINO framebuffer/DMA descriptor management */
  702. static void vino_free_buffer_with_count(struct vino_framebuffer *fb,
  703. unsigned int count)
  704. {
  705. unsigned int i;
  706. dprintk("vino_free_buffer_with_count(): count = %d\n", count);
  707. for (i = 0; i < count; i++) {
  708. ClearPageReserved(virt_to_page(fb->desc_table.virtual[i]));
  709. dma_unmap_single(NULL,
  710. fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i],
  711. PAGE_SIZE, DMA_FROM_DEVICE);
  712. free_page(fb->desc_table.virtual[i]);
  713. }
  714. dma_free_coherent(NULL,
  715. VINO_PAGE_RATIO * (fb->desc_table.page_count + 4) *
  716. sizeof(dma_addr_t), (void *)fb->desc_table.dma_cpu,
  717. fb->desc_table.dma);
  718. kfree(fb->desc_table.virtual);
  719. memset(fb, 0, sizeof(struct vino_framebuffer));
  720. }
  721. static void vino_free_buffer(struct vino_framebuffer *fb)
  722. {
  723. vino_free_buffer_with_count(fb, fb->desc_table.page_count);
  724. }
  725. static int vino_allocate_buffer(struct vino_framebuffer *fb,
  726. unsigned int size)
  727. {
  728. unsigned int count, i, j;
  729. int ret = 0;
  730. dprintk("vino_allocate_buffer():\n");
  731. if (size < 1)
  732. return -EINVAL;
  733. memset(fb, 0, sizeof(struct vino_framebuffer));
  734. count = ((size / PAGE_SIZE) + 4) & ~3;
  735. dprintk("vino_allocate_buffer(): size = %d, count = %d\n",
  736. size, count);
  737. /* allocate memory for table with virtual (page) addresses */
  738. fb->desc_table.virtual = (unsigned long *)
  739. kmalloc(count * sizeof(unsigned long), GFP_KERNEL);
  740. if (!fb->desc_table.virtual)
  741. return -ENOMEM;
  742. /* allocate memory for table with dma addresses
  743. * (has space for four extra descriptors) */
  744. fb->desc_table.dma_cpu =
  745. dma_alloc_coherent(NULL, VINO_PAGE_RATIO * (count + 4) *
  746. sizeof(dma_addr_t), &fb->desc_table.dma,
  747. GFP_KERNEL | GFP_DMA);
  748. if (!fb->desc_table.dma_cpu) {
  749. ret = -ENOMEM;
  750. goto out_free_virtual;
  751. }
  752. /* allocate pages for the buffer and acquire the according
  753. * dma addresses */
  754. for (i = 0; i < count; i++) {
  755. dma_addr_t dma_data_addr;
  756. fb->desc_table.virtual[i] =
  757. get_zeroed_page(GFP_KERNEL | GFP_DMA);
  758. if (!fb->desc_table.virtual[i]) {
  759. ret = -ENOBUFS;
  760. break;
  761. }
  762. dma_data_addr =
  763. dma_map_single(NULL,
  764. (void *)fb->desc_table.virtual[i],
  765. PAGE_SIZE, DMA_FROM_DEVICE);
  766. for (j = 0; j < VINO_PAGE_RATIO; j++) {
  767. fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i + j] =
  768. dma_data_addr + VINO_PAGE_SIZE * j;
  769. }
  770. SetPageReserved(virt_to_page(fb->desc_table.virtual[i]));
  771. }
  772. /* page_count needs to be set anyway, because the descriptor table has
  773. * been allocated according to this number */
  774. fb->desc_table.page_count = count;
  775. if (ret) {
  776. /* the descriptor with index i doesn't contain
  777. * a valid address yet */
  778. vino_free_buffer_with_count(fb, i);
  779. return ret;
  780. }
  781. //fb->size = size;
  782. fb->size = count * PAGE_SIZE;
  783. fb->data_format = VINO_DATA_FMT_NONE;
  784. /* set the dma stop-bit for the last (count+1)th descriptor */
  785. fb->desc_table.dma_cpu[VINO_PAGE_RATIO * count] = VINO_DESC_STOP;
  786. return 0;
  787. out_free_virtual:
  788. kfree(fb->desc_table.virtual);
  789. return ret;
  790. }
  791. #if 0
  792. /* user buffers not fully implemented yet */
  793. static int vino_prepare_user_buffer(struct vino_framebuffer *fb,
  794. void *user,
  795. unsigned int size)
  796. {
  797. unsigned int count, i, j;
  798. int ret = 0;
  799. dprintk("vino_prepare_user_buffer():\n");
  800. if (size < 1)
  801. return -EINVAL;
  802. memset(fb, 0, sizeof(struct vino_framebuffer));
  803. count = ((size / PAGE_SIZE)) & ~3;
  804. dprintk("vino_prepare_user_buffer(): size = %d, count = %d\n",
  805. size, count);
  806. /* allocate memory for table with virtual (page) addresses */
  807. fb->desc_table.virtual = (unsigned long *)
  808. kmalloc(count * sizeof(unsigned long), GFP_KERNEL);
  809. if (!fb->desc_table.virtual)
  810. return -ENOMEM;
  811. /* allocate memory for table with dma addresses
  812. * (has space for four extra descriptors) */
  813. fb->desc_table.dma_cpu =
  814. dma_alloc_coherent(NULL, VINO_PAGE_RATIO * (count + 4) *
  815. sizeof(dma_addr_t), &fb->desc_table.dma,
  816. GFP_KERNEL | GFP_DMA);
  817. if (!fb->desc_table.dma_cpu) {
  818. ret = -ENOMEM;
  819. goto out_free_virtual;
  820. }
  821. /* allocate pages for the buffer and acquire the according
  822. * dma addresses */
  823. for (i = 0; i < count; i++) {
  824. dma_addr_t dma_data_addr;
  825. fb->desc_table.virtual[i] =
  826. get_zeroed_page(GFP_KERNEL | GFP_DMA);
  827. if (!fb->desc_table.virtual[i]) {
  828. ret = -ENOBUFS;
  829. break;
  830. }
  831. dma_data_addr =
  832. dma_map_single(NULL,
  833. (void *)fb->desc_table.virtual[i],
  834. PAGE_SIZE, DMA_FROM_DEVICE);
  835. for (j = 0; j < VINO_PAGE_RATIO; j++) {
  836. fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i + j] =
  837. dma_data_addr + VINO_PAGE_SIZE * j;
  838. }
  839. SetPageReserved(virt_to_page(fb->desc_table.virtual[i]));
  840. }
  841. /* page_count needs to be set anyway, because the descriptor table has
  842. * been allocated according to this number */
  843. fb->desc_table.page_count = count;
  844. if (ret) {
  845. /* the descriptor with index i doesn't contain
  846. * a valid address yet */
  847. vino_free_buffer_with_count(fb, i);
  848. return ret;
  849. }
  850. //fb->size = size;
  851. fb->size = count * PAGE_SIZE;
  852. /* set the dma stop-bit for the last (count+1)th descriptor */
  853. fb->desc_table.dma_cpu[VINO_PAGE_RATIO * count] = VINO_DESC_STOP;
  854. return 0;
  855. out_free_virtual:
  856. kfree(fb->desc_table.virtual);
  857. return ret;
  858. }
  859. #endif
  860. static void vino_sync_buffer(struct vino_framebuffer *fb)
  861. {
  862. int i;
  863. dprintk("vino_sync_buffer():\n");
  864. for (i = 0; i < fb->desc_table.page_count; i++)
  865. dma_sync_single(NULL,
  866. fb->desc_table.dma_cpu[VINO_PAGE_RATIO * i],
  867. PAGE_SIZE, DMA_FROM_DEVICE);
  868. }
  869. /* Framebuffer fifo functions (need to be locked externally) */
  870. static inline void vino_fifo_init(struct vino_framebuffer_fifo *f,
  871. unsigned int length)
  872. {
  873. f->length = 0;
  874. f->used = 0;
  875. f->head = 0;
  876. f->tail = 0;
  877. if (length > VINO_FRAMEBUFFER_COUNT_MAX)
  878. length = VINO_FRAMEBUFFER_COUNT_MAX;
  879. f->length = length;
  880. }
  881. /* returns true/false */
  882. static inline int vino_fifo_has_id(struct vino_framebuffer_fifo *f,
  883. unsigned int id)
  884. {
  885. unsigned int i;
  886. for (i = f->head; i == (f->tail - 1); i = (i + 1) % f->length) {
  887. if (f->data[i] == id)
  888. return 1;
  889. }
  890. return 0;
  891. }
  892. #if 0
  893. /* returns true/false */
  894. static inline int vino_fifo_full(struct vino_framebuffer_fifo *f)
  895. {
  896. return (f->used == f->length);
  897. }
  898. #endif
  899. static inline unsigned int vino_fifo_get_used(struct vino_framebuffer_fifo *f)
  900. {
  901. return f->used;
  902. }
  903. static int vino_fifo_enqueue(struct vino_framebuffer_fifo *f, unsigned int id)
  904. {
  905. if (id >= f->length) {
  906. return VINO_QUEUE_ERROR;
  907. }
  908. if (vino_fifo_has_id(f, id)) {
  909. return VINO_QUEUE_ERROR;
  910. }
  911. if (f->used < f->length) {
  912. f->data[f->tail] = id;
  913. f->tail = (f->tail + 1) % f->length;
  914. f->used++;
  915. } else {
  916. return VINO_QUEUE_ERROR;
  917. }
  918. return 0;
  919. }
  920. static int vino_fifo_peek(struct vino_framebuffer_fifo *f, unsigned int *id)
  921. {
  922. if (f->used > 0) {
  923. *id = f->data[f->head];
  924. } else {
  925. return VINO_QUEUE_ERROR;
  926. }
  927. return 0;
  928. }
  929. static int vino_fifo_dequeue(struct vino_framebuffer_fifo *f, unsigned int *id)
  930. {
  931. if (f->used > 0) {
  932. *id = f->data[f->head];
  933. f->head = (f->head + 1) % f->length;
  934. f->used--;
  935. } else {
  936. return VINO_QUEUE_ERROR;
  937. }
  938. return 0;
  939. }
  940. /* Framebuffer queue functions */
  941. /* execute with queue_lock locked */
  942. static void vino_queue_free_with_count(struct vino_framebuffer_queue *q,
  943. unsigned int length)
  944. {
  945. unsigned int i;
  946. q->length = 0;
  947. memset(&q->in, 0, sizeof(struct vino_framebuffer_fifo));
  948. memset(&q->out, 0, sizeof(struct vino_framebuffer_fifo));
  949. for (i = 0; i < length; i++) {
  950. dprintk("vino_queue_free_with_count(): freeing buffer %d\n",
  951. i);
  952. vino_free_buffer(q->buffer[i]);
  953. kfree(q->buffer[i]);
  954. }
  955. q->type = VINO_MEMORY_NONE;
  956. q->magic = 0;
  957. }
  958. static void vino_queue_free(struct vino_framebuffer_queue *q)
  959. {
  960. dprintk("vino_queue_free():\n");
  961. if (q->magic != VINO_QUEUE_MAGIC)
  962. return;
  963. if (q->type != VINO_MEMORY_MMAP)
  964. return;
  965. mutex_lock(&q->queue_mutex);
  966. vino_queue_free_with_count(q, q->length);
  967. mutex_unlock(&q->queue_mutex);
  968. }
  969. static int vino_queue_init(struct vino_framebuffer_queue *q,
  970. unsigned int *length)
  971. {
  972. unsigned int i;
  973. int ret = 0;
  974. dprintk("vino_queue_init(): length = %d\n", *length);
  975. if (q->magic == VINO_QUEUE_MAGIC) {
  976. dprintk("vino_queue_init(): queue already initialized!\n");
  977. return -EINVAL;
  978. }
  979. if (q->type != VINO_MEMORY_NONE) {
  980. dprintk("vino_queue_init(): queue already initialized!\n");
  981. return -EINVAL;
  982. }
  983. if (*length < 1)
  984. return -EINVAL;
  985. mutex_lock(&q->queue_mutex);
  986. if (*length > VINO_FRAMEBUFFER_COUNT_MAX)
  987. *length = VINO_FRAMEBUFFER_COUNT_MAX;
  988. q->length = 0;
  989. for (i = 0; i < *length; i++) {
  990. dprintk("vino_queue_init(): allocating buffer %d\n", i);
  991. q->buffer[i] = kmalloc(sizeof(struct vino_framebuffer),
  992. GFP_KERNEL);
  993. if (!q->buffer[i]) {
  994. dprintk("vino_queue_init(): kmalloc() failed\n");
  995. ret = -ENOMEM;
  996. break;
  997. }
  998. ret = vino_allocate_buffer(q->buffer[i],
  999. VINO_FRAMEBUFFER_SIZE);
  1000. if (ret) {
  1001. kfree(q->buffer[i]);
  1002. dprintk("vino_queue_init(): "
  1003. "vino_allocate_buffer() failed\n");
  1004. break;
  1005. }
  1006. q->buffer[i]->id = i;
  1007. if (i > 0) {
  1008. q->buffer[i]->offset = q->buffer[i - 1]->offset +
  1009. q->buffer[i - 1]->size;
  1010. } else {
  1011. q->buffer[i]->offset = 0;
  1012. }
  1013. spin_lock_init(&q->buffer[i]->state_lock);
  1014. dprintk("vino_queue_init(): buffer = %d, offset = %d, "
  1015. "size = %d\n", i, q->buffer[i]->offset,
  1016. q->buffer[i]->size);
  1017. }
  1018. if (ret) {
  1019. vino_queue_free_with_count(q, i);
  1020. *length = 0;
  1021. } else {
  1022. q->length = *length;
  1023. vino_fifo_init(&q->in, q->length);
  1024. vino_fifo_init(&q->out, q->length);
  1025. q->type = VINO_MEMORY_MMAP;
  1026. q->magic = VINO_QUEUE_MAGIC;
  1027. }
  1028. mutex_unlock(&q->queue_mutex);
  1029. return ret;
  1030. }
  1031. static struct vino_framebuffer *vino_queue_add(struct
  1032. vino_framebuffer_queue *q,
  1033. unsigned int id)
  1034. {
  1035. struct vino_framebuffer *ret = NULL;
  1036. unsigned int total;
  1037. unsigned long flags;
  1038. dprintk("vino_queue_add(): id = %d\n", id);
  1039. if (q->magic != VINO_QUEUE_MAGIC) {
  1040. return ret;
  1041. }
  1042. spin_lock_irqsave(&q->queue_lock, flags);
  1043. if (q->length == 0)
  1044. goto out;
  1045. if (id >= q->length)
  1046. goto out;
  1047. /* not needed?: if (vino_fifo_full(&q->out)) {
  1048. goto out;
  1049. }*/
  1050. /* check that outgoing queue isn't already full
  1051. * (or that it won't become full) */
  1052. total = vino_fifo_get_used(&q->in) +
  1053. vino_fifo_get_used(&q->out);
  1054. if (total >= q->length)
  1055. goto out;
  1056. if (vino_fifo_enqueue(&q->in, id))
  1057. goto out;
  1058. ret = q->buffer[id];
  1059. out:
  1060. spin_unlock_irqrestore(&q->queue_lock, flags);
  1061. return ret;
  1062. }
  1063. static struct vino_framebuffer *vino_queue_transfer(struct
  1064. vino_framebuffer_queue *q)
  1065. {
  1066. struct vino_framebuffer *ret = NULL;
  1067. struct vino_framebuffer *fb;
  1068. int id;
  1069. unsigned long flags;
  1070. dprintk("vino_queue_transfer():\n");
  1071. if (q->magic != VINO_QUEUE_MAGIC) {
  1072. return ret;
  1073. }
  1074. spin_lock_irqsave(&q->queue_lock, flags);
  1075. if (q->length == 0)
  1076. goto out;
  1077. // now this actually removes an entry from the incoming queue
  1078. if (vino_fifo_dequeue(&q->in, &id)) {
  1079. goto out;
  1080. }
  1081. dprintk("vino_queue_transfer(): id = %d\n", id);
  1082. fb = q->buffer[id];
  1083. // we have already checked that the outgoing queue is not full, but...
  1084. if (vino_fifo_enqueue(&q->out, id)) {
  1085. printk(KERN_ERR "vino_queue_transfer(): "
  1086. "outgoing queue is full, this shouldn't happen!\n");
  1087. goto out;
  1088. }
  1089. ret = fb;
  1090. out:
  1091. spin_unlock_irqrestore(&q->queue_lock, flags);
  1092. return ret;
  1093. }
  1094. /* returns true/false */
  1095. static int vino_queue_incoming_contains(struct vino_framebuffer_queue *q,
  1096. unsigned int id)
  1097. {
  1098. int ret = 0;
  1099. unsigned long flags;
  1100. if (q->magic != VINO_QUEUE_MAGIC) {
  1101. return ret;
  1102. }
  1103. spin_lock_irqsave(&q->queue_lock, flags);
  1104. if (q->length == 0)
  1105. goto out;
  1106. ret = vino_fifo_has_id(&q->in, id);
  1107. out:
  1108. spin_unlock_irqrestore(&q->queue_lock, flags);
  1109. return ret;
  1110. }
  1111. /* returns true/false */
  1112. static int vino_queue_outgoing_contains(struct vino_framebuffer_queue *q,
  1113. unsigned int id)
  1114. {
  1115. int ret = 0;
  1116. unsigned long flags;
  1117. if (q->magic != VINO_QUEUE_MAGIC) {
  1118. return ret;
  1119. }
  1120. spin_lock_irqsave(&q->queue_lock, flags);
  1121. if (q->length == 0)
  1122. goto out;
  1123. ret = vino_fifo_has_id(&q->out, id);
  1124. out:
  1125. spin_unlock_irqrestore(&q->queue_lock, flags);
  1126. return ret;
  1127. }
  1128. static int vino_queue_get_incoming(struct vino_framebuffer_queue *q,
  1129. unsigned int *used)
  1130. {
  1131. int ret = 0;
  1132. unsigned long flags;
  1133. if (q->magic != VINO_QUEUE_MAGIC) {
  1134. return VINO_QUEUE_ERROR;
  1135. }
  1136. spin_lock_irqsave(&q->queue_lock, flags);
  1137. if (q->length == 0) {
  1138. ret = VINO_QUEUE_ERROR;
  1139. goto out;
  1140. }
  1141. *used = vino_fifo_get_used(&q->in);
  1142. out:
  1143. spin_unlock_irqrestore(&q->queue_lock, flags);
  1144. return ret;
  1145. }
  1146. static int vino_queue_get_outgoing(struct vino_framebuffer_queue *q,
  1147. unsigned int *used)
  1148. {
  1149. int ret = 0;
  1150. unsigned long flags;
  1151. if (q->magic != VINO_QUEUE_MAGIC) {
  1152. return VINO_QUEUE_ERROR;
  1153. }
  1154. spin_lock_irqsave(&q->queue_lock, flags);
  1155. if (q->length == 0) {
  1156. ret = VINO_QUEUE_ERROR;
  1157. goto out;
  1158. }
  1159. *used = vino_fifo_get_used(&q->out);
  1160. out:
  1161. spin_unlock_irqrestore(&q->queue_lock, flags);
  1162. return ret;
  1163. }
  1164. #if 0
  1165. static int vino_queue_get_total(struct vino_framebuffer_queue *q,
  1166. unsigned int *total)
  1167. {
  1168. int ret = 0;
  1169. unsigned long flags;
  1170. if (q->magic != VINO_QUEUE_MAGIC) {
  1171. return VINO_QUEUE_ERROR;
  1172. }
  1173. spin_lock_irqsave(&q->queue_lock, flags);
  1174. if (q->length == 0) {
  1175. ret = VINO_QUEUE_ERROR;
  1176. goto out;
  1177. }
  1178. *total = vino_fifo_get_used(&q->in) +
  1179. vino_fifo_get_used(&q->out);
  1180. out:
  1181. spin_unlock_irqrestore(&q->queue_lock, flags);
  1182. return ret;
  1183. }
  1184. #endif
  1185. static struct vino_framebuffer *vino_queue_peek(struct
  1186. vino_framebuffer_queue *q,
  1187. unsigned int *id)
  1188. {
  1189. struct vino_framebuffer *ret = NULL;
  1190. unsigned long flags;
  1191. if (q->magic != VINO_QUEUE_MAGIC) {
  1192. return ret;
  1193. }
  1194. spin_lock_irqsave(&q->queue_lock, flags);
  1195. if (q->length == 0)
  1196. goto out;
  1197. if (vino_fifo_peek(&q->in, id)) {
  1198. goto out;
  1199. }
  1200. ret = q->buffer[*id];
  1201. out:
  1202. spin_unlock_irqrestore(&q->queue_lock, flags);
  1203. return ret;
  1204. }
  1205. static struct vino_framebuffer *vino_queue_remove(struct
  1206. vino_framebuffer_queue *q,
  1207. unsigned int *id)
  1208. {
  1209. struct vino_framebuffer *ret = NULL;
  1210. unsigned long flags;
  1211. dprintk("vino_queue_remove():\n");
  1212. if (q->magic != VINO_QUEUE_MAGIC) {
  1213. return ret;
  1214. }
  1215. spin_lock_irqsave(&q->queue_lock, flags);
  1216. if (q->length == 0)
  1217. goto out;
  1218. if (vino_fifo_dequeue(&q->out, id)) {
  1219. goto out;
  1220. }
  1221. dprintk("vino_queue_remove(): id = %d\n", *id);
  1222. ret = q->buffer[*id];
  1223. out:
  1224. spin_unlock_irqrestore(&q->queue_lock, flags);
  1225. return ret;
  1226. }
  1227. static struct
  1228. vino_framebuffer *vino_queue_get_buffer(struct vino_framebuffer_queue *q,
  1229. unsigned int id)
  1230. {
  1231. struct vino_framebuffer *ret = NULL;
  1232. unsigned long flags;
  1233. if (q->magic != VINO_QUEUE_MAGIC) {
  1234. return ret;
  1235. }
  1236. spin_lock_irqsave(&q->queue_lock, flags);
  1237. if (q->length == 0)
  1238. goto out;
  1239. if (id >= q->length)
  1240. goto out;
  1241. ret = q->buffer[id];
  1242. out:
  1243. spin_unlock_irqrestore(&q->queue_lock, flags);
  1244. return ret;
  1245. }
  1246. static unsigned int vino_queue_get_length(struct vino_framebuffer_queue *q)
  1247. {
  1248. unsigned int length = 0;
  1249. unsigned long flags;
  1250. if (q->magic != VINO_QUEUE_MAGIC) {
  1251. return length;
  1252. }
  1253. spin_lock_irqsave(&q->queue_lock, flags);
  1254. length = q->length;
  1255. spin_unlock_irqrestore(&q->queue_lock, flags);
  1256. return length;
  1257. }
  1258. static int vino_queue_has_mapped_buffers(struct vino_framebuffer_queue *q)
  1259. {
  1260. unsigned int i;
  1261. int ret = 0;
  1262. unsigned long flags;
  1263. if (q->magic != VINO_QUEUE_MAGIC) {
  1264. return ret;
  1265. }
  1266. spin_lock_irqsave(&q->queue_lock, flags);
  1267. for (i = 0; i < q->length; i++) {
  1268. if (q->buffer[i]->map_count > 0) {
  1269. ret = 1;
  1270. break;
  1271. }
  1272. }
  1273. spin_unlock_irqrestore(&q->queue_lock, flags);
  1274. return ret;
  1275. }
  1276. /* VINO functions */
  1277. /* execute with input_lock locked */
  1278. static void vino_update_line_size(struct vino_channel_settings *vcs)
  1279. {
  1280. unsigned int w = vcs->clipping.right - vcs->clipping.left;
  1281. unsigned int d = vcs->decimation;
  1282. unsigned int bpp = vino_data_formats[vcs->data_format].bpp;
  1283. unsigned int lsize;
  1284. dprintk("update_line_size(): before: w = %d, d = %d, "
  1285. "line_size = %d\n", w, d, vcs->line_size);
  1286. /* line size must be multiple of 8 bytes */
  1287. lsize = (bpp * (w / d)) & ~7;
  1288. w = (lsize / bpp) * d;
  1289. vcs->clipping.right = vcs->clipping.left + w;
  1290. vcs->line_size = lsize;
  1291. dprintk("update_line_size(): after: w = %d, d = %d, "
  1292. "line_size = %d\n", w, d, vcs->line_size);
  1293. }
  1294. /* execute with input_lock locked */
  1295. static void vino_set_clipping(struct vino_channel_settings *vcs,
  1296. unsigned int x, unsigned int y,
  1297. unsigned int w, unsigned int h)
  1298. {
  1299. unsigned int maxwidth, maxheight;
  1300. unsigned int d;
  1301. maxwidth = vino_data_norms[vcs->data_norm].width;
  1302. maxheight = vino_data_norms[vcs->data_norm].height;
  1303. d = vcs->decimation;
  1304. y &= ~1; /* odd/even fields */
  1305. if (x > maxwidth) {
  1306. x = 0;
  1307. }
  1308. if (y > maxheight) {
  1309. y = 0;
  1310. }
  1311. if (((w / d) < VINO_MIN_WIDTH)
  1312. || ((h / d) < VINO_MIN_HEIGHT)) {
  1313. w = VINO_MIN_WIDTH * d;
  1314. h = VINO_MIN_HEIGHT * d;
  1315. }
  1316. if ((x + w) > maxwidth) {
  1317. w = maxwidth - x;
  1318. if ((w / d) < VINO_MIN_WIDTH)
  1319. x = maxwidth - VINO_MIN_WIDTH * d;
  1320. }
  1321. if ((y + h) > maxheight) {
  1322. h = maxheight - y;
  1323. if ((h / d) < VINO_MIN_HEIGHT)
  1324. y = maxheight - VINO_MIN_HEIGHT * d;
  1325. }
  1326. vcs->clipping.left = x;
  1327. vcs->clipping.top = y;
  1328. vcs->clipping.right = x + w;
  1329. vcs->clipping.bottom = y + h;
  1330. vino_update_line_size(vcs);
  1331. dprintk("clipping %d, %d, %d, %d / %d - %d\n",
  1332. vcs->clipping.left, vcs->clipping.top, vcs->clipping.right,
  1333. vcs->clipping.bottom, vcs->decimation, vcs->line_size);
  1334. }
  1335. /* execute with input_lock locked */
  1336. static inline void vino_set_default_clipping(struct vino_channel_settings *vcs)
  1337. {
  1338. vino_set_clipping(vcs, 0, 0, vino_data_norms[vcs->data_norm].width,
  1339. vino_data_norms[vcs->data_norm].height);
  1340. }
  1341. /* execute with input_lock locked */
  1342. static void vino_set_scaling(struct vino_channel_settings *vcs,
  1343. unsigned int w, unsigned int h)
  1344. {
  1345. unsigned int x, y, curw, curh, d;
  1346. x = vcs->clipping.left;
  1347. y = vcs->clipping.top;
  1348. curw = vcs->clipping.right - vcs->clipping.left;
  1349. curh = vcs->clipping.bottom - vcs->clipping.top;
  1350. d = max(curw / w, curh / h);
  1351. dprintk("scaling w: %d, h: %d, curw: %d, curh: %d, d: %d\n",
  1352. w, h, curw, curh, d);
  1353. if (d < 1) {
  1354. d = 1;
  1355. } else if (d > 8) {
  1356. d = 8;
  1357. }
  1358. vcs->decimation = d;
  1359. vino_set_clipping(vcs, x, y, w * d, h * d);
  1360. dprintk("scaling %d, %d, %d, %d / %d - %d\n", vcs->clipping.left,
  1361. vcs->clipping.top, vcs->clipping.right, vcs->clipping.bottom,
  1362. vcs->decimation, vcs->line_size);
  1363. }
  1364. /* execute with input_lock locked */
  1365. static inline void vino_set_default_scaling(struct vino_channel_settings *vcs)
  1366. {
  1367. vino_set_scaling(vcs, vcs->clipping.right - vcs->clipping.left,
  1368. vcs->clipping.bottom - vcs->clipping.top);
  1369. }
  1370. /* execute with input_lock locked */
  1371. static void vino_set_framerate(struct vino_channel_settings *vcs,
  1372. unsigned int fps)
  1373. {
  1374. unsigned int mask;
  1375. switch (vcs->data_norm) {
  1376. case VINO_DATA_NORM_NTSC:
  1377. case VINO_DATA_NORM_D1:
  1378. fps = (unsigned int)(fps / 6) * 6; // FIXME: round!
  1379. if (fps < vino_data_norms[vcs->data_norm].fps_min)
  1380. fps = vino_data_norms[vcs->data_norm].fps_min;
  1381. if (fps > vino_data_norms[vcs->data_norm].fps_max)
  1382. fps = vino_data_norms[vcs->data_norm].fps_max;
  1383. switch (fps) {
  1384. case 6:
  1385. mask = 0x003;
  1386. break;
  1387. case 12:
  1388. mask = 0x0c3;
  1389. break;
  1390. case 18:
  1391. mask = 0x333;
  1392. break;
  1393. case 24:
  1394. mask = 0x3ff;
  1395. break;
  1396. case 30:
  1397. mask = 0xfff;
  1398. break;
  1399. default:
  1400. mask = VINO_FRAMERT_FULL;
  1401. }
  1402. vcs->framert_reg = VINO_FRAMERT_RT(mask);
  1403. break;
  1404. case VINO_DATA_NORM_PAL:
  1405. case VINO_DATA_NORM_SECAM:
  1406. fps = (unsigned int)(fps / 5) * 5; // FIXME: round!
  1407. if (fps < vino_data_norms[vcs->data_norm].fps_min)
  1408. fps = vino_data_norms[vcs->data_norm].fps_min;
  1409. if (fps > vino_data_norms[vcs->data_norm].fps_max)
  1410. fps = vino_data_norms[vcs->data_norm].fps_max;
  1411. switch (fps) {
  1412. case 5:
  1413. mask = 0x003;
  1414. break;
  1415. case 10:
  1416. mask = 0x0c3;
  1417. break;
  1418. case 15:
  1419. mask = 0x333;
  1420. break;
  1421. case 20:
  1422. mask = 0x0ff;
  1423. break;
  1424. case 25:
  1425. mask = 0x3ff;
  1426. break;
  1427. default:
  1428. mask = VINO_FRAMERT_FULL;
  1429. }
  1430. vcs->framert_reg = VINO_FRAMERT_RT(mask) | VINO_FRAMERT_PAL;
  1431. break;
  1432. }
  1433. vcs->fps = fps;
  1434. }
  1435. /* execute with input_lock locked */
  1436. static inline void vino_set_default_framerate(struct
  1437. vino_channel_settings *vcs)
  1438. {
  1439. vino_set_framerate(vcs, vino_data_norms[vcs->data_norm].fps_max);
  1440. }
  1441. /*
  1442. * Prepare VINO for DMA transfer...
  1443. * (execute only with vino_lock and input_lock locked)
  1444. */
  1445. static int vino_dma_setup(struct vino_channel_settings *vcs,
  1446. struct vino_framebuffer *fb)
  1447. {
  1448. u32 ctrl, intr;
  1449. struct sgi_vino_channel *ch;
  1450. const struct vino_data_norm *norm;
  1451. dprintk("vino_dma_setup():\n");
  1452. vcs->field = 0;
  1453. fb->frame_counter = 0;
  1454. ch = (vcs->channel == VINO_CHANNEL_A) ? &vino->a : &vino->b;
  1455. norm = &vino_data_norms[vcs->data_norm];
  1456. ch->page_index = 0;
  1457. ch->line_count = 0;
  1458. /* VINO line size register is set 8 bytes less than actual */
  1459. ch->line_size = vcs->line_size - 8;
  1460. /* let VINO know where to transfer data */
  1461. ch->start_desc_tbl = fb->desc_table.dma;
  1462. ch->next_4_desc = fb->desc_table.dma;
  1463. /* give vino time to fetch the first four descriptors, 5 usec
  1464. * should be more than enough time */
  1465. udelay(VINO_DESC_FETCH_DELAY);
  1466. dprintk("vino_dma_setup(): start desc = %08x, next 4 desc = %08x\n",
  1467. ch->start_desc_tbl, ch->next_4_desc);
  1468. /* set the alpha register */
  1469. ch->alpha = vcs->alpha;
  1470. /* set clipping registers */
  1471. ch->clip_start = VINO_CLIP_ODD(norm->odd.top + vcs->clipping.top / 2) |
  1472. VINO_CLIP_EVEN(norm->even.top +
  1473. vcs->clipping.top / 2) |
  1474. VINO_CLIP_X(vcs->clipping.left);
  1475. ch->clip_end = VINO_CLIP_ODD(norm->odd.top +
  1476. vcs->clipping.bottom / 2 - 1) |
  1477. VINO_CLIP_EVEN(norm->even.top +
  1478. vcs->clipping.bottom / 2 - 1) |
  1479. VINO_CLIP_X(vcs->clipping.right);
  1480. /* set the size of actual content in the buffer (DECIMATION !) */
  1481. fb->data_size = ((vcs->clipping.right - vcs->clipping.left) /
  1482. vcs->decimation) *
  1483. ((vcs->clipping.bottom - vcs->clipping.top) /
  1484. vcs->decimation) *
  1485. vino_data_formats[vcs->data_format].bpp;
  1486. ch->frame_rate = vcs->framert_reg;
  1487. ctrl = vino->control;
  1488. intr = vino->intr_status;
  1489. if (vcs->channel == VINO_CHANNEL_A) {
  1490. /* All interrupt conditions for this channel was cleared
  1491. * so clear the interrupt status register and enable
  1492. * interrupts */
  1493. intr &= ~VINO_INTSTAT_A;
  1494. ctrl |= VINO_CTRL_A_INT;
  1495. /* enable synchronization */
  1496. ctrl |= VINO_CTRL_A_SYNC_ENBL;
  1497. /* enable frame assembly */
  1498. ctrl |= VINO_CTRL_A_INTERLEAVE_ENBL;
  1499. /* set decimation used */
  1500. if (vcs->decimation < 2)
  1501. ctrl &= ~VINO_CTRL_A_DEC_ENBL;
  1502. else {
  1503. ctrl |= VINO_CTRL_A_DEC_ENBL;
  1504. ctrl &= ~VINO_CTRL_A_DEC_SCALE_MASK;
  1505. ctrl |= (vcs->decimation - 1) <<
  1506. VINO_CTRL_A_DEC_SCALE_SHIFT;
  1507. }
  1508. /* select input interface */
  1509. if (vcs->input == VINO_INPUT_D1)
  1510. ctrl |= VINO_CTRL_A_SELECT;
  1511. else
  1512. ctrl &= ~VINO_CTRL_A_SELECT;
  1513. /* palette */
  1514. ctrl &= ~(VINO_CTRL_A_LUMA_ONLY | VINO_CTRL_A_RGB |
  1515. VINO_CTRL_A_DITHER);
  1516. } else {
  1517. intr &= ~VINO_INTSTAT_B;
  1518. ctrl |= VINO_CTRL_B_INT;
  1519. ctrl |= VINO_CTRL_B_SYNC_ENBL;
  1520. ctrl |= VINO_CTRL_B_INTERLEAVE_ENBL;
  1521. if (vcs->decimation < 2)
  1522. ctrl &= ~VINO_CTRL_B_DEC_ENBL;
  1523. else {
  1524. ctrl |= VINO_CTRL_B_DEC_ENBL;
  1525. ctrl &= ~VINO_CTRL_B_DEC_SCALE_MASK;
  1526. ctrl |= (vcs->decimation - 1) <<
  1527. VINO_CTRL_B_DEC_SCALE_SHIFT;
  1528. }
  1529. if (vcs->input == VINO_INPUT_D1)
  1530. ctrl |= VINO_CTRL_B_SELECT;
  1531. else
  1532. ctrl &= ~VINO_CTRL_B_SELECT;
  1533. ctrl &= ~(VINO_CTRL_B_LUMA_ONLY | VINO_CTRL_B_RGB |
  1534. VINO_CTRL_B_DITHER);
  1535. }
  1536. /* set palette */
  1537. fb->data_format = vcs->data_format;
  1538. switch (vcs->data_format) {
  1539. case VINO_DATA_FMT_GREY:
  1540. ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
  1541. VINO_CTRL_A_LUMA_ONLY : VINO_CTRL_B_LUMA_ONLY;
  1542. break;
  1543. case VINO_DATA_FMT_RGB32:
  1544. ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
  1545. VINO_CTRL_A_RGB : VINO_CTRL_B_RGB;
  1546. break;
  1547. case VINO_DATA_FMT_YUV:
  1548. /* nothing needs to be done */
  1549. break;
  1550. case VINO_DATA_FMT_RGB332:
  1551. ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
  1552. VINO_CTRL_A_RGB | VINO_CTRL_A_DITHER :
  1553. VINO_CTRL_B_RGB | VINO_CTRL_B_DITHER;
  1554. break;
  1555. }
  1556. vino->intr_status = intr;
  1557. vino->control = ctrl;
  1558. return 0;
  1559. }
  1560. /* (execute only with vino_lock locked) */
  1561. static inline void vino_dma_start(struct vino_channel_settings *vcs)
  1562. {
  1563. u32 ctrl = vino->control;
  1564. dprintk("vino_dma_start():\n");
  1565. ctrl |= (vcs->channel == VINO_CHANNEL_A) ?
  1566. VINO_CTRL_A_DMA_ENBL : VINO_CTRL_B_DMA_ENBL;
  1567. vino->control = ctrl;
  1568. }
  1569. /* (execute only with vino_lock locked) */
  1570. static inline void vino_dma_stop(struct vino_channel_settings *vcs)
  1571. {
  1572. u32 ctrl = vino->control;
  1573. ctrl &= (vcs->channel == VINO_CHANNEL_A) ?
  1574. ~VINO_CTRL_A_DMA_ENBL : ~VINO_CTRL_B_DMA_ENBL;
  1575. ctrl &= (vcs->channel == VINO_CHANNEL_A) ?
  1576. ~VINO_CTRL_A_INT : ~VINO_CTRL_B_INT;
  1577. vino->control = ctrl;
  1578. dprintk("vino_dma_stop():\n");
  1579. }
  1580. /*
  1581. * Load dummy page to descriptor registers. This prevents generating of
  1582. * spurious interrupts. (execute only with vino_lock locked)
  1583. */
  1584. static void vino_clear_interrupt(struct vino_channel_settings *vcs)
  1585. {
  1586. struct sgi_vino_channel *ch;
  1587. ch = (vcs->channel == VINO_CHANNEL_A) ? &vino->a : &vino->b;
  1588. ch->page_index = 0;
  1589. ch->line_count = 0;
  1590. ch->start_desc_tbl = vino_drvdata->dummy_desc_table.dma;
  1591. ch->next_4_desc = vino_drvdata->dummy_desc_table.dma;
  1592. udelay(VINO_DESC_FETCH_DELAY);
  1593. dprintk("channel %c clear interrupt condition\n",
  1594. (vcs->channel == VINO_CHANNEL_A) ? 'A':'B');
  1595. }
  1596. static int vino_capture(struct vino_channel_settings *vcs,
  1597. struct vino_framebuffer *fb)
  1598. {
  1599. int err = 0;
  1600. unsigned long flags, flags2;
  1601. spin_lock_irqsave(&fb->state_lock, flags);
  1602. if (fb->state == VINO_FRAMEBUFFER_IN_USE)
  1603. err = -EBUSY;
  1604. fb->state = VINO_FRAMEBUFFER_IN_USE;
  1605. spin_unlock_irqrestore(&fb->state_lock, flags);
  1606. if (err)
  1607. return err;
  1608. spin_lock_irqsave(&vino_drvdata->vino_lock, flags);
  1609. spin_lock_irqsave(&vino_drvdata->input_lock, flags2);
  1610. vino_dma_setup(vcs, fb);
  1611. vino_dma_start(vcs);
  1612. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags2);
  1613. spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags);
  1614. return err;
  1615. }
  1616. static
  1617. struct vino_framebuffer *vino_capture_enqueue(struct
  1618. vino_channel_settings *vcs,
  1619. unsigned int index)
  1620. {
  1621. struct vino_framebuffer *fb;
  1622. unsigned long flags;
  1623. dprintk("vino_capture_enqueue():\n");
  1624. spin_lock_irqsave(&vcs->capture_lock, flags);
  1625. fb = vino_queue_add(&vcs->fb_queue, index);
  1626. if (fb == NULL) {
  1627. dprintk("vino_capture_enqueue(): vino_queue_add() failed, "
  1628. "queue full?\n");
  1629. goto out;
  1630. }
  1631. out:
  1632. spin_unlock_irqrestore(&vcs->capture_lock, flags);
  1633. return fb;
  1634. }
  1635. static int vino_capture_next(struct vino_channel_settings *vcs, int start)
  1636. {
  1637. struct vino_framebuffer *fb;
  1638. unsigned int incoming, id;
  1639. int err = 0;
  1640. unsigned long flags;
  1641. dprintk("vino_capture_next():\n");
  1642. spin_lock_irqsave(&vcs->capture_lock, flags);
  1643. if (start) {
  1644. /* start capture only if capture isn't in progress already */
  1645. if (vcs->capturing) {
  1646. spin_unlock_irqrestore(&vcs->capture_lock, flags);
  1647. return 0;
  1648. }
  1649. } else {
  1650. /* capture next frame:
  1651. * stop capture if capturing is not set */
  1652. if (!vcs->capturing) {
  1653. spin_unlock_irqrestore(&vcs->capture_lock, flags);
  1654. return 0;
  1655. }
  1656. }
  1657. err = vino_queue_get_incoming(&vcs->fb_queue, &incoming);
  1658. if (err) {
  1659. dprintk("vino_capture_next(): vino_queue_get_incoming() "
  1660. "failed\n");
  1661. err = -EINVAL;
  1662. goto out;
  1663. }
  1664. if (incoming == 0) {
  1665. dprintk("vino_capture_next(): no buffers available\n");
  1666. goto out;
  1667. }
  1668. fb = vino_queue_peek(&vcs->fb_queue, &id);
  1669. if (fb == NULL) {
  1670. dprintk("vino_capture_next(): vino_queue_peek() failed\n");
  1671. err = -EINVAL;
  1672. goto out;
  1673. }
  1674. if (start) {
  1675. vcs->capturing = 1;
  1676. }
  1677. spin_unlock_irqrestore(&vcs->capture_lock, flags);
  1678. err = vino_capture(vcs, fb);
  1679. return err;
  1680. out:
  1681. vcs->capturing = 0;
  1682. spin_unlock_irqrestore(&vcs->capture_lock, flags);
  1683. return err;
  1684. }
  1685. static inline int vino_is_capturing(struct vino_channel_settings *vcs)
  1686. {
  1687. int ret;
  1688. unsigned long flags;
  1689. spin_lock_irqsave(&vcs->capture_lock, flags);
  1690. ret = vcs->capturing;
  1691. spin_unlock_irqrestore(&vcs->capture_lock, flags);
  1692. return ret;
  1693. }
  1694. /* waits until a frame is captured */
  1695. static int vino_wait_for_frame(struct vino_channel_settings *vcs)
  1696. {
  1697. wait_queue_t wait;
  1698. int err = 0;
  1699. dprintk("vino_wait_for_frame():\n");
  1700. init_waitqueue_entry(&wait, current);
  1701. /* add ourselves into wait queue */
  1702. add_wait_queue(&vcs->fb_queue.frame_wait_queue, &wait);
  1703. /* and set current state */
  1704. set_current_state(TASK_INTERRUPTIBLE);
  1705. /* to ensure that schedule_timeout will return immediately
  1706. * if VINO interrupt was triggred meanwhile */
  1707. schedule_timeout(HZ / 10);
  1708. if (signal_pending(current))
  1709. err = -EINTR;
  1710. remove_wait_queue(&vcs->fb_queue.frame_wait_queue, &wait);
  1711. dprintk("vino_wait_for_frame(): waiting for frame %s\n",
  1712. err ? "failed" : "ok");
  1713. return err;
  1714. }
  1715. /* the function assumes that PAGE_SIZE % 4 == 0 */
  1716. static void vino_convert_to_rgba(struct vino_framebuffer *fb) {
  1717. unsigned char *pageptr;
  1718. unsigned int page, i;
  1719. unsigned char a;
  1720. for (page = 0; page < fb->desc_table.page_count; page++) {
  1721. pageptr = (unsigned char *)fb->desc_table.virtual[page];
  1722. for (i = 0; i < PAGE_SIZE; i += 4) {
  1723. a = pageptr[0];
  1724. pageptr[0] = pageptr[3];
  1725. pageptr[1] = pageptr[2];
  1726. pageptr[2] = pageptr[1];
  1727. pageptr[3] = a;
  1728. pageptr += 4;
  1729. }
  1730. }
  1731. }
  1732. /* checks if the buffer is in correct state and syncs data */
  1733. static int vino_check_buffer(struct vino_channel_settings *vcs,
  1734. struct vino_framebuffer *fb)
  1735. {
  1736. int err = 0;
  1737. unsigned long flags;
  1738. dprintk("vino_check_buffer():\n");
  1739. spin_lock_irqsave(&fb->state_lock, flags);
  1740. switch (fb->state) {
  1741. case VINO_FRAMEBUFFER_IN_USE:
  1742. err = -EIO;
  1743. break;
  1744. case VINO_FRAMEBUFFER_READY:
  1745. vino_sync_buffer(fb);
  1746. fb->state = VINO_FRAMEBUFFER_UNUSED;
  1747. break;
  1748. default:
  1749. err = -EINVAL;
  1750. }
  1751. spin_unlock_irqrestore(&fb->state_lock, flags);
  1752. if (!err) {
  1753. if (vino_pixel_conversion
  1754. && (fb->data_format == VINO_DATA_FMT_RGB32)) {
  1755. vino_convert_to_rgba(fb);
  1756. }
  1757. } else if (err && (err != -EINVAL)) {
  1758. dprintk("vino_check_buffer(): buffer not ready\n");
  1759. spin_lock_irqsave(&vino_drvdata->vino_lock, flags);
  1760. vino_dma_stop(vcs);
  1761. vino_clear_interrupt(vcs);
  1762. spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags);
  1763. }
  1764. return err;
  1765. }
  1766. /* forcefully terminates capture */
  1767. static void vino_capture_stop(struct vino_channel_settings *vcs)
  1768. {
  1769. unsigned int incoming = 0, outgoing = 0, id;
  1770. unsigned long flags, flags2;
  1771. dprintk("vino_capture_stop():\n");
  1772. spin_lock_irqsave(&vcs->capture_lock, flags);
  1773. /* unset capturing to stop queue processing */
  1774. vcs->capturing = 0;
  1775. spin_lock_irqsave(&vino_drvdata->vino_lock, flags2);
  1776. vino_dma_stop(vcs);
  1777. vino_clear_interrupt(vcs);
  1778. spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags2);
  1779. /* remove all items from the queue */
  1780. if (vino_queue_get_incoming(&vcs->fb_queue, &incoming)) {
  1781. dprintk("vino_capture_stop(): "
  1782. "vino_queue_get_incoming() failed\n");
  1783. goto out;
  1784. }
  1785. while (incoming > 0) {
  1786. vino_queue_transfer(&vcs->fb_queue);
  1787. if (vino_queue_get_incoming(&vcs->fb_queue, &incoming)) {
  1788. dprintk("vino_capture_stop(): "
  1789. "vino_queue_get_incoming() failed\n");
  1790. goto out;
  1791. }
  1792. }
  1793. if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
  1794. dprintk("vino_capture_stop(): "
  1795. "vino_queue_get_outgoing() failed\n");
  1796. goto out;
  1797. }
  1798. while (outgoing > 0) {
  1799. vino_queue_remove(&vcs->fb_queue, &id);
  1800. if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
  1801. dprintk("vino_capture_stop(): "
  1802. "vino_queue_get_outgoing() failed\n");
  1803. goto out;
  1804. }
  1805. }
  1806. out:
  1807. spin_unlock_irqrestore(&vcs->capture_lock, flags);
  1808. }
  1809. #if 0
  1810. static int vino_capture_failed(struct vino_channel_settings *vcs)
  1811. {
  1812. struct vino_framebuffer *fb;
  1813. unsigned long flags;
  1814. unsigned int i;
  1815. int ret;
  1816. dprintk("vino_capture_failed():\n");
  1817. spin_lock_irqsave(&vino_drvdata->vino_lock, flags);
  1818. vino_dma_stop(vcs);
  1819. vino_clear_interrupt(vcs);
  1820. spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags);
  1821. ret = vino_queue_get_incoming(&vcs->fb_queue, &i);
  1822. if (ret == VINO_QUEUE_ERROR) {
  1823. dprintk("vino_queue_get_incoming() failed\n");
  1824. return -EINVAL;
  1825. }
  1826. if (i == 0) {
  1827. /* no buffers to process */
  1828. return 0;
  1829. }
  1830. fb = vino_queue_peek(&vcs->fb_queue, &i);
  1831. if (fb == NULL) {
  1832. dprintk("vino_queue_peek() failed\n");
  1833. return -EINVAL;
  1834. }
  1835. spin_lock_irqsave(&fb->state_lock, flags);
  1836. if (fb->state == VINO_FRAMEBUFFER_IN_USE) {
  1837. fb->state = VINO_FRAMEBUFFER_UNUSED;
  1838. vino_queue_transfer(&vcs->fb_queue);
  1839. vino_queue_remove(&vcs->fb_queue, &i);
  1840. /* we should actually discard the newest frame,
  1841. * but who cares ... */
  1842. }
  1843. spin_unlock_irqrestore(&fb->state_lock, flags);
  1844. return 0;
  1845. }
  1846. #endif
  1847. static void vino_skip_frame(struct vino_channel_settings *vcs)
  1848. {
  1849. struct vino_framebuffer *fb;
  1850. unsigned long flags;
  1851. unsigned int id;
  1852. spin_lock_irqsave(&vcs->capture_lock, flags);
  1853. fb = vino_queue_peek(&vcs->fb_queue, &id);
  1854. if (!fb) {
  1855. spin_unlock_irqrestore(&vcs->capture_lock, flags);
  1856. dprintk("vino_skip_frame(): vino_queue_peek() failed!\n");
  1857. return;
  1858. }
  1859. spin_unlock_irqrestore(&vcs->capture_lock, flags);
  1860. spin_lock_irqsave(&fb->state_lock, flags);
  1861. fb->state = VINO_FRAMEBUFFER_UNUSED;
  1862. spin_unlock_irqrestore(&fb->state_lock, flags);
  1863. vino_capture_next(vcs, 0);
  1864. }
  1865. static void vino_frame_done(struct vino_channel_settings *vcs)
  1866. {
  1867. struct vino_framebuffer *fb;
  1868. unsigned long flags;
  1869. spin_lock_irqsave(&vcs->capture_lock, flags);
  1870. fb = vino_queue_transfer(&vcs->fb_queue);
  1871. if (!fb) {
  1872. spin_unlock_irqrestore(&vcs->capture_lock, flags);
  1873. dprintk("vino_frame_done(): vino_queue_transfer() failed!\n");
  1874. return;
  1875. }
  1876. spin_unlock_irqrestore(&vcs->capture_lock, flags);
  1877. fb->frame_counter = vcs->int_data.frame_counter;
  1878. memcpy(&fb->timestamp, &vcs->int_data.timestamp,
  1879. sizeof(struct timeval));
  1880. spin_lock_irqsave(&fb->state_lock, flags);
  1881. if (fb->state == VINO_FRAMEBUFFER_IN_USE)
  1882. fb->state = VINO_FRAMEBUFFER_READY;
  1883. spin_unlock_irqrestore(&fb->state_lock, flags);
  1884. wake_up(&vcs->fb_queue.frame_wait_queue);
  1885. vino_capture_next(vcs, 0);
  1886. }
  1887. static void vino_capture_tasklet(unsigned long channel) {
  1888. struct vino_channel_settings *vcs;
  1889. vcs = (channel == VINO_CHANNEL_A)
  1890. ? &vino_drvdata->a : &vino_drvdata->b;
  1891. if (vcs->int_data.skip)
  1892. vcs->int_data.skip_count++;
  1893. if (vcs->int_data.skip && (vcs->int_data.skip_count
  1894. <= VINO_MAX_FRAME_SKIP_COUNT)) {
  1895. vino_skip_frame(vcs);
  1896. } else {
  1897. vcs->int_data.skip_count = 0;
  1898. vino_frame_done(vcs);
  1899. }
  1900. }
  1901. static irqreturn_t vino_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  1902. {
  1903. u32 ctrl, intr;
  1904. unsigned int fc_a, fc_b;
  1905. int handled_a = 0, skip_a = 0, done_a = 0;
  1906. int handled_b = 0, skip_b = 0, done_b = 0;
  1907. #ifdef VINO_DEBUG_INT
  1908. int loop = 0;
  1909. unsigned int line_count = vino->a.line_count,
  1910. page_index = vino->a.page_index,
  1911. field_counter = vino->a.field_counter,
  1912. start_desc_tbl = vino->a.start_desc_tbl,
  1913. next_4_desc = vino->a.next_4_desc;
  1914. unsigned int line_count_2,
  1915. page_index_2,
  1916. field_counter_2,
  1917. start_desc_tbl_2,
  1918. next_4_desc_2;
  1919. #endif
  1920. spin_lock(&vino_drvdata->vino_lock);
  1921. while ((intr = vino->intr_status)) {
  1922. fc_a = vino->a.field_counter >> 1;
  1923. fc_b = vino->b.field_counter >> 1;
  1924. /* handle error-interrupts in some special way ?
  1925. * --> skips frames */
  1926. if (intr & VINO_INTSTAT_A) {
  1927. if (intr & VINO_INTSTAT_A_EOF) {
  1928. vino_drvdata->a.field++;
  1929. if (vino_drvdata->a.field > 1) {
  1930. vino_dma_stop(&vino_drvdata->a);
  1931. vino_clear_interrupt(&vino_drvdata->a);
  1932. vino_drvdata->a.field = 0;
  1933. done_a = 1;
  1934. } else {
  1935. if (vino->a.page_index
  1936. != vino_drvdata->a.line_size) {
  1937. vino->a.line_count = 0;
  1938. vino->a.page_index =
  1939. vino_drvdata->
  1940. a.line_size;
  1941. vino->a.next_4_desc =
  1942. vino->a.start_desc_tbl;
  1943. }
  1944. }
  1945. dprintk("channel A end-of-field "
  1946. "interrupt: %04x\n", intr);
  1947. } else {
  1948. vino_dma_stop(&vino_drvdata->a);
  1949. vino_clear_interrupt(&vino_drvdata->a);
  1950. vino_drvdata->a.field = 0;
  1951. skip_a = 1;
  1952. dprintk("channel A error interrupt: %04x\n",
  1953. intr);
  1954. }
  1955. #ifdef VINO_DEBUG_INT
  1956. line_count_2 = vino->a.line_count;
  1957. page_index_2 = vino->a.page_index;
  1958. field_counter_2 = vino->a.field_counter;
  1959. start_desc_tbl_2 = vino->a.start_desc_tbl;
  1960. next_4_desc_2 = vino->a.next_4_desc;
  1961. printk("intr = %04x, loop = %d, field = %d\n",
  1962. intr, loop, vino_drvdata->a.field);
  1963. printk("1- line count = %04d, page index = %04d, "
  1964. "start = %08x, next = %08x\n"
  1965. " fieldc = %d, framec = %d\n",
  1966. line_count, page_index, start_desc_tbl,
  1967. next_4_desc, field_counter, fc_a);
  1968. printk("12-line count = %04d, page index = %04d, "
  1969. " start = %08x, next = %08x\n",
  1970. line_count_2, page_index_2, start_desc_tbl_2,
  1971. next_4_desc_2);
  1972. if (done_a)
  1973. printk("\n");
  1974. #endif
  1975. }
  1976. if (intr & VINO_INTSTAT_B) {
  1977. if (intr & VINO_INTSTAT_B_EOF) {
  1978. vino_drvdata->b.field++;
  1979. if (vino_drvdata->b.field > 1) {
  1980. vino_dma_stop(&vino_drvdata->b);
  1981. vino_clear_interrupt(&vino_drvdata->b);
  1982. vino_drvdata->b.field = 0;
  1983. done_b = 1;
  1984. }
  1985. dprintk("channel B end-of-field "
  1986. "interrupt: %04x\n", intr);
  1987. } else {
  1988. vino_dma_stop(&vino_drvdata->b);
  1989. vino_clear_interrupt(&vino_drvdata->b);
  1990. vino_drvdata->b.field = 0;
  1991. skip_b = 1;
  1992. dprintk("channel B error interrupt: %04x\n",
  1993. intr);
  1994. }
  1995. }
  1996. /* Always remember to clear interrupt status.
  1997. * Disable VINO interrupts while we do this. */
  1998. ctrl = vino->control;
  1999. vino->control = ctrl & ~(VINO_CTRL_A_INT | VINO_CTRL_B_INT);
  2000. vino->intr_status = ~intr;
  2001. vino->control = ctrl;
  2002. spin_unlock(&vino_drvdata->vino_lock);
  2003. if ((!handled_a) && (done_a || skip_a)) {
  2004. if (!skip_a) {
  2005. do_gettimeofday(&vino_drvdata->
  2006. a.int_data.timestamp);
  2007. vino_drvdata->a.int_data.frame_counter = fc_a;
  2008. }
  2009. vino_drvdata->a.int_data.skip = skip_a;
  2010. dprintk("channel A %s, interrupt: %d\n",
  2011. skip_a ? "skipping frame" : "frame done",
  2012. intr);
  2013. tasklet_hi_schedule(&vino_tasklet_a);
  2014. handled_a = 1;
  2015. }
  2016. if ((!handled_b) && (done_b || skip_b)) {
  2017. if (!skip_b) {
  2018. do_gettimeofday(&vino_drvdata->
  2019. b.int_data.timestamp);
  2020. vino_drvdata->b.int_data.frame_counter = fc_b;
  2021. }
  2022. vino_drvdata->b.int_data.skip = skip_b;
  2023. dprintk("channel B %s, interrupt: %d\n",
  2024. skip_b ? "skipping frame" : "frame done",
  2025. intr);
  2026. tasklet_hi_schedule(&vino_tasklet_b);
  2027. handled_b = 1;
  2028. }
  2029. #ifdef VINO_DEBUG_INT
  2030. loop++;
  2031. #endif
  2032. spin_lock(&vino_drvdata->vino_lock);
  2033. }
  2034. spin_unlock(&vino_drvdata->vino_lock);
  2035. return IRQ_HANDLED;
  2036. }
  2037. /* VINO video input management */
  2038. static int vino_get_saa7191_input(int input)
  2039. {
  2040. switch (input) {
  2041. case VINO_INPUT_COMPOSITE:
  2042. return SAA7191_INPUT_COMPOSITE;
  2043. case VINO_INPUT_SVIDEO:
  2044. return SAA7191_INPUT_SVIDEO;
  2045. default:
  2046. printk(KERN_ERR "VINO: vino_get_saa7191_input(): "
  2047. "invalid input!\n");
  2048. return -1;
  2049. }
  2050. }
  2051. static int vino_get_saa7191_norm(unsigned int data_norm)
  2052. {
  2053. switch (data_norm) {
  2054. case VINO_DATA_NORM_AUTO:
  2055. return SAA7191_NORM_AUTO;
  2056. case VINO_DATA_NORM_AUTO_EXT:
  2057. return SAA7191_NORM_AUTO_EXT;
  2058. case VINO_DATA_NORM_PAL:
  2059. return SAA7191_NORM_PAL;
  2060. case VINO_DATA_NORM_NTSC:
  2061. return SAA7191_NORM_NTSC;
  2062. case VINO_DATA_NORM_SECAM:
  2063. return SAA7191_NORM_SECAM;
  2064. default:
  2065. printk(KERN_ERR "VINO: vino_get_saa7191_norm(): "
  2066. "invalid norm!\n");
  2067. return -1;
  2068. }
  2069. }
  2070. static int vino_get_from_saa7191_norm(int saa7191_norm)
  2071. {
  2072. switch (saa7191_norm) {
  2073. case SAA7191_NORM_PAL:
  2074. return VINO_DATA_NORM_PAL;
  2075. case SAA7191_NORM_NTSC:
  2076. return VINO_DATA_NORM_NTSC;
  2077. case SAA7191_NORM_SECAM:
  2078. return VINO_DATA_NORM_SECAM;
  2079. default:
  2080. printk(KERN_ERR "VINO: vino_get_from_saa7191_norm(): "
  2081. "invalid norm!\n");
  2082. return VINO_DATA_NORM_NONE;
  2083. }
  2084. }
  2085. static int vino_saa7191_set_norm(unsigned int *data_norm)
  2086. {
  2087. int saa7191_norm, new_data_norm;
  2088. int err = 0;
  2089. saa7191_norm = vino_get_saa7191_norm(*data_norm);
  2090. err = i2c_decoder_command(DECODER_SAA7191_SET_NORM,
  2091. &saa7191_norm);
  2092. if (err)
  2093. goto out;
  2094. if ((*data_norm == VINO_DATA_NORM_AUTO)
  2095. || (*data_norm == VINO_DATA_NORM_AUTO_EXT)) {
  2096. struct saa7191_status status;
  2097. err = i2c_decoder_command(DECODER_SAA7191_GET_STATUS,
  2098. &status);
  2099. if (err)
  2100. goto out;
  2101. new_data_norm =
  2102. vino_get_from_saa7191_norm(status.norm);
  2103. if (new_data_norm == VINO_DATA_NORM_NONE) {
  2104. err = -EINVAL;
  2105. goto out;
  2106. }
  2107. *data_norm = (unsigned int)new_data_norm;
  2108. }
  2109. out:
  2110. return err;
  2111. }
  2112. /* execute with input_lock locked */
  2113. static int vino_is_input_owner(struct vino_channel_settings *vcs)
  2114. {
  2115. switch(vcs->input) {
  2116. case VINO_INPUT_COMPOSITE:
  2117. case VINO_INPUT_SVIDEO:
  2118. return (vino_drvdata->decoder.owner == vcs->channel);
  2119. case VINO_INPUT_D1:
  2120. return (vino_drvdata->camera.owner == vcs->channel);
  2121. default:
  2122. return 0;
  2123. }
  2124. }
  2125. static int vino_acquire_input(struct vino_channel_settings *vcs)
  2126. {
  2127. unsigned long flags;
  2128. int ret = 0;
  2129. dprintk("vino_acquire_input():\n");
  2130. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2131. /* First try D1 and then SAA7191 */
  2132. if (vino_drvdata->camera.driver
  2133. && (vino_drvdata->camera.owner == VINO_NO_CHANNEL)) {
  2134. if (i2c_use_client(vino_drvdata->camera.driver)) {
  2135. ret = -ENODEV;
  2136. goto out;
  2137. }
  2138. vino_drvdata->camera.owner = vcs->channel;
  2139. vcs->input = VINO_INPUT_D1;
  2140. vcs->data_norm = VINO_DATA_NORM_D1;
  2141. } else if (vino_drvdata->decoder.driver
  2142. && (vino_drvdata->decoder.owner == VINO_NO_CHANNEL)) {
  2143. int input, data_norm;
  2144. int saa7191_input;
  2145. if (i2c_use_client(vino_drvdata->decoder.driver)) {
  2146. ret = -ENODEV;
  2147. goto out;
  2148. }
  2149. input = VINO_INPUT_COMPOSITE;
  2150. saa7191_input = vino_get_saa7191_input(input);
  2151. ret = i2c_decoder_command(DECODER_SET_INPUT,
  2152. &saa7191_input);
  2153. if (ret) {
  2154. ret = -EINVAL;
  2155. goto out;
  2156. }
  2157. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2158. /* Don't hold spinlocks while auto-detecting norm
  2159. * as it may take a while... */
  2160. data_norm = VINO_DATA_NORM_AUTO_EXT;
  2161. ret = vino_saa7191_set_norm(&data_norm);
  2162. if ((ret == -EBUSY) || (ret == -EAGAIN)) {
  2163. data_norm = VINO_DATA_NORM_PAL;
  2164. ret = vino_saa7191_set_norm(&data_norm);
  2165. }
  2166. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2167. if (ret) {
  2168. ret = -EINVAL;
  2169. goto out;
  2170. }
  2171. vino_drvdata->decoder.owner = vcs->channel;
  2172. vcs->input = input;
  2173. vcs->data_norm = data_norm;
  2174. } else {
  2175. vcs->input = (vcs->channel == VINO_CHANNEL_A) ?
  2176. vino_drvdata->b.input : vino_drvdata->a.input;
  2177. vcs->data_norm = (vcs->channel == VINO_CHANNEL_A) ?
  2178. vino_drvdata->b.data_norm : vino_drvdata->a.data_norm;
  2179. }
  2180. if (vcs->input == VINO_INPUT_NONE) {
  2181. ret = -ENODEV;
  2182. goto out;
  2183. }
  2184. vino_set_default_clipping(vcs);
  2185. vino_set_default_scaling(vcs);
  2186. vino_set_default_framerate(vcs);
  2187. dprintk("vino_acquire_input(): %s\n", vino_inputs[vcs->input].name);
  2188. out:
  2189. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2190. return ret;
  2191. }
  2192. static int vino_set_input(struct vino_channel_settings *vcs, int input)
  2193. {
  2194. struct vino_channel_settings *vcs2 = (vcs->channel == VINO_CHANNEL_A) ?
  2195. &vino_drvdata->b : &vino_drvdata->a;
  2196. unsigned long flags;
  2197. int ret = 0;
  2198. dprintk("vino_set_input():\n");
  2199. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2200. if (vcs->input == input)
  2201. goto out;
  2202. switch (input) {
  2203. case VINO_INPUT_COMPOSITE:
  2204. case VINO_INPUT_SVIDEO:
  2205. if (!vino_drvdata->decoder.driver) {
  2206. ret = -EINVAL;
  2207. goto out;
  2208. }
  2209. if (vino_drvdata->decoder.owner == VINO_NO_CHANNEL) {
  2210. if (i2c_use_client(vino_drvdata->decoder.driver)) {
  2211. ret = -ENODEV;
  2212. goto out;
  2213. }
  2214. vino_drvdata->decoder.owner = vcs->channel;
  2215. }
  2216. if (vino_drvdata->decoder.owner == vcs->channel) {
  2217. int data_norm;
  2218. int saa7191_input;
  2219. saa7191_input = vino_get_saa7191_input(input);
  2220. ret = i2c_decoder_command(DECODER_SET_INPUT,
  2221. &saa7191_input);
  2222. if (ret) {
  2223. vino_drvdata->decoder.owner = VINO_NO_CHANNEL;
  2224. ret = -EINVAL;
  2225. goto out;
  2226. }
  2227. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2228. /* Don't hold spinlocks while auto-detecting norm
  2229. * as it may take a while... */
  2230. data_norm = VINO_DATA_NORM_AUTO_EXT;
  2231. ret = vino_saa7191_set_norm(&data_norm);
  2232. if ((ret == -EBUSY) || (ret == -EAGAIN)) {
  2233. data_norm = VINO_DATA_NORM_PAL;
  2234. ret = vino_saa7191_set_norm(&data_norm);
  2235. }
  2236. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2237. if (ret) {
  2238. vino_drvdata->decoder.owner = VINO_NO_CHANNEL;
  2239. ret = -EINVAL;
  2240. goto out;
  2241. }
  2242. vcs->input = input;
  2243. vcs->data_norm = data_norm;
  2244. } else {
  2245. if (input != vcs2->input) {
  2246. ret = -EBUSY;
  2247. goto out;
  2248. }
  2249. vcs->input = input;
  2250. vcs->data_norm = vcs2->data_norm;
  2251. }
  2252. if (vino_drvdata->camera.owner == vcs->channel) {
  2253. /* Transfer the ownership or release the input */
  2254. if (vcs2->input == VINO_INPUT_D1) {
  2255. vino_drvdata->camera.owner = vcs2->channel;
  2256. } else {
  2257. i2c_release_client(vino_drvdata->
  2258. camera.driver);
  2259. vino_drvdata->camera.owner = VINO_NO_CHANNEL;
  2260. }
  2261. }
  2262. break;
  2263. case VINO_INPUT_D1:
  2264. if (!vino_drvdata->camera.driver) {
  2265. ret = -EINVAL;
  2266. goto out;
  2267. }
  2268. if (vino_drvdata->camera.owner == VINO_NO_CHANNEL) {
  2269. if (i2c_use_client(vino_drvdata->camera.driver)) {
  2270. ret = -ENODEV;
  2271. goto out;
  2272. }
  2273. vino_drvdata->camera.owner = vcs->channel;
  2274. }
  2275. if (vino_drvdata->decoder.owner == vcs->channel) {
  2276. /* Transfer the ownership or release the input */
  2277. if ((vcs2->input == VINO_INPUT_COMPOSITE) ||
  2278. (vcs2->input == VINO_INPUT_SVIDEO)) {
  2279. vino_drvdata->decoder.owner = vcs2->channel;
  2280. } else {
  2281. i2c_release_client(vino_drvdata->
  2282. decoder.driver);
  2283. vino_drvdata->decoder.owner = VINO_NO_CHANNEL;
  2284. }
  2285. }
  2286. vcs->input = input;
  2287. vcs->data_norm = VINO_DATA_NORM_D1;
  2288. break;
  2289. default:
  2290. ret = -EINVAL;
  2291. goto out;
  2292. }
  2293. vino_set_default_clipping(vcs);
  2294. vino_set_default_scaling(vcs);
  2295. vino_set_default_framerate(vcs);
  2296. dprintk("vino_set_input(): %s\n", vino_inputs[vcs->input].name);
  2297. out:
  2298. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2299. return ret;
  2300. }
  2301. static void vino_release_input(struct vino_channel_settings *vcs)
  2302. {
  2303. struct vino_channel_settings *vcs2 = (vcs->channel == VINO_CHANNEL_A) ?
  2304. &vino_drvdata->b : &vino_drvdata->a;
  2305. unsigned long flags;
  2306. dprintk("vino_release_input():\n");
  2307. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2308. /* Release ownership of the channel
  2309. * and if the other channel takes input from
  2310. * the same source, transfer the ownership */
  2311. if (vino_drvdata->camera.owner == vcs->channel) {
  2312. if (vcs2->input == VINO_INPUT_D1) {
  2313. vino_drvdata->camera.owner = vcs2->channel;
  2314. } else {
  2315. i2c_release_client(vino_drvdata->camera.driver);
  2316. vino_drvdata->camera.owner = VINO_NO_CHANNEL;
  2317. }
  2318. } else if (vino_drvdata->decoder.owner == vcs->channel) {
  2319. if ((vcs2->input == VINO_INPUT_COMPOSITE) ||
  2320. (vcs2->input == VINO_INPUT_SVIDEO)) {
  2321. vino_drvdata->decoder.owner = vcs2->channel;
  2322. } else {
  2323. i2c_release_client(vino_drvdata->decoder.driver);
  2324. vino_drvdata->decoder.owner = VINO_NO_CHANNEL;
  2325. }
  2326. }
  2327. vcs->input = VINO_INPUT_NONE;
  2328. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2329. }
  2330. /* execute with input_lock locked */
  2331. static int vino_set_data_norm(struct vino_channel_settings *vcs,
  2332. unsigned int data_norm,
  2333. unsigned long *flags)
  2334. {
  2335. int err = 0;
  2336. if (data_norm == vcs->data_norm)
  2337. return 0;
  2338. switch (vcs->input) {
  2339. case VINO_INPUT_D1:
  2340. /* only one "norm" supported */
  2341. if ((data_norm != VINO_DATA_NORM_D1)
  2342. && (data_norm != VINO_DATA_NORM_AUTO)
  2343. && (data_norm != VINO_DATA_NORM_AUTO_EXT))
  2344. return -EINVAL;
  2345. break;
  2346. case VINO_INPUT_COMPOSITE:
  2347. case VINO_INPUT_SVIDEO: {
  2348. if ((data_norm != VINO_DATA_NORM_PAL)
  2349. && (data_norm != VINO_DATA_NORM_NTSC)
  2350. && (data_norm != VINO_DATA_NORM_SECAM)
  2351. && (data_norm != VINO_DATA_NORM_AUTO)
  2352. && (data_norm != VINO_DATA_NORM_AUTO_EXT))
  2353. return -EINVAL;
  2354. spin_unlock_irqrestore(&vino_drvdata->input_lock, *flags);
  2355. /* Don't hold spinlocks while setting norm
  2356. * as it may take a while... */
  2357. err = vino_saa7191_set_norm(&data_norm);
  2358. spin_lock_irqsave(&vino_drvdata->input_lock, *flags);
  2359. if (err)
  2360. goto out;
  2361. vcs->data_norm = data_norm;
  2362. vino_set_default_clipping(vcs);
  2363. vino_set_default_scaling(vcs);
  2364. vino_set_default_framerate(vcs);
  2365. break;
  2366. }
  2367. default:
  2368. return -EINVAL;
  2369. }
  2370. out:
  2371. return err;
  2372. }
  2373. /* V4L2 helper functions */
  2374. static int vino_find_data_format(__u32 pixelformat)
  2375. {
  2376. int i;
  2377. for (i = 0; i < VINO_DATA_FMT_COUNT; i++) {
  2378. if (vino_data_formats[i].pixelformat == pixelformat)
  2379. return i;
  2380. }
  2381. return VINO_DATA_FMT_NONE;
  2382. }
  2383. static int vino_enum_data_norm(struct vino_channel_settings *vcs, __u32 index)
  2384. {
  2385. int data_norm = VINO_DATA_NORM_NONE;
  2386. unsigned long flags;
  2387. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2388. switch(vcs->input) {
  2389. case VINO_INPUT_COMPOSITE:
  2390. case VINO_INPUT_SVIDEO:
  2391. if (index == 0) {
  2392. data_norm = VINO_DATA_NORM_PAL;
  2393. } else if (index == 1) {
  2394. data_norm = VINO_DATA_NORM_NTSC;
  2395. } else if (index == 2) {
  2396. data_norm = VINO_DATA_NORM_SECAM;
  2397. }
  2398. break;
  2399. case VINO_INPUT_D1:
  2400. if (index == 0) {
  2401. data_norm = VINO_DATA_NORM_D1;
  2402. }
  2403. break;
  2404. }
  2405. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2406. return data_norm;
  2407. }
  2408. static int vino_enum_input(struct vino_channel_settings *vcs, __u32 index)
  2409. {
  2410. int input = VINO_INPUT_NONE;
  2411. unsigned long flags;
  2412. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2413. if (vino_drvdata->decoder.driver && vino_drvdata->camera.driver) {
  2414. switch (index) {
  2415. case 0:
  2416. input = VINO_INPUT_COMPOSITE;
  2417. break;
  2418. case 1:
  2419. input = VINO_INPUT_SVIDEO;
  2420. break;
  2421. case 2:
  2422. input = VINO_INPUT_D1;
  2423. break;
  2424. }
  2425. } else if (vino_drvdata->decoder.driver) {
  2426. switch (index) {
  2427. case 0:
  2428. input = VINO_INPUT_COMPOSITE;
  2429. break;
  2430. case 1:
  2431. input = VINO_INPUT_SVIDEO;
  2432. break;
  2433. }
  2434. } else if (vino_drvdata->camera.driver) {
  2435. switch (index) {
  2436. case 0:
  2437. input = VINO_INPUT_D1;
  2438. break;
  2439. }
  2440. }
  2441. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2442. return input;
  2443. }
  2444. /* execute with input_lock locked */
  2445. static __u32 vino_find_input_index(struct vino_channel_settings *vcs)
  2446. {
  2447. __u32 index = 0;
  2448. // FIXME: detect when no inputs available
  2449. if (vino_drvdata->decoder.driver && vino_drvdata->camera.driver) {
  2450. switch (vcs->input) {
  2451. case VINO_INPUT_COMPOSITE:
  2452. index = 0;
  2453. break;
  2454. case VINO_INPUT_SVIDEO:
  2455. index = 1;
  2456. break;
  2457. case VINO_INPUT_D1:
  2458. index = 2;
  2459. break;
  2460. }
  2461. } else if (vino_drvdata->decoder.driver) {
  2462. switch (vcs->input) {
  2463. case VINO_INPUT_COMPOSITE:
  2464. index = 0;
  2465. break;
  2466. case VINO_INPUT_SVIDEO:
  2467. index = 1;
  2468. break;
  2469. }
  2470. } else if (vino_drvdata->camera.driver) {
  2471. switch (vcs->input) {
  2472. case VINO_INPUT_D1:
  2473. index = 0;
  2474. break;
  2475. }
  2476. }
  2477. return index;
  2478. }
  2479. /* V4L2 ioctls */
  2480. static void vino_v4l2_querycap(struct v4l2_capability *cap)
  2481. {
  2482. memset(cap, 0, sizeof(struct v4l2_capability));
  2483. strcpy(cap->driver, vino_driver_name);
  2484. strcpy(cap->card, vino_driver_description);
  2485. strcpy(cap->bus_info, vino_bus_name);
  2486. cap->version = VINO_VERSION_CODE;
  2487. cap->capabilities =
  2488. V4L2_CAP_VIDEO_CAPTURE |
  2489. V4L2_CAP_STREAMING;
  2490. // V4L2_CAP_OVERLAY, V4L2_CAP_READWRITE
  2491. }
  2492. static int vino_v4l2_enuminput(struct vino_channel_settings *vcs,
  2493. struct v4l2_input *i)
  2494. {
  2495. __u32 index = i->index;
  2496. int input;
  2497. dprintk("requested index = %d\n", index);
  2498. input = vino_enum_input(vcs, index);
  2499. if (input == VINO_INPUT_NONE)
  2500. return -EINVAL;
  2501. memset(i, 0, sizeof(struct v4l2_input));
  2502. i->index = index;
  2503. i->type = V4L2_INPUT_TYPE_CAMERA;
  2504. i->std = vino_inputs[input].std;
  2505. strcpy(i->name, vino_inputs[input].name);
  2506. if ((input == VINO_INPUT_COMPOSITE)
  2507. || (input == VINO_INPUT_SVIDEO)) {
  2508. struct saa7191_status status;
  2509. i2c_decoder_command(DECODER_SAA7191_GET_STATUS, &status);
  2510. i->status |= status.signal ? 0 : V4L2_IN_ST_NO_SIGNAL;
  2511. i->status |= status.color ? 0 : V4L2_IN_ST_NO_COLOR;
  2512. }
  2513. return 0;
  2514. }
  2515. static int vino_v4l2_g_input(struct vino_channel_settings *vcs,
  2516. unsigned int *i)
  2517. {
  2518. __u32 index;
  2519. int input;
  2520. unsigned long flags;
  2521. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2522. input = vcs->input;
  2523. index = vino_find_input_index(vcs);
  2524. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2525. dprintk("input = %d\n", input);
  2526. if (input == VINO_INPUT_NONE) {
  2527. return -EINVAL;
  2528. }
  2529. *i = index;
  2530. return 0;
  2531. }
  2532. static int vino_v4l2_s_input(struct vino_channel_settings *vcs,
  2533. unsigned int *i)
  2534. {
  2535. int input;
  2536. dprintk("requested input = %d\n", *i);
  2537. input = vino_enum_input(vcs, *i);
  2538. if (input == VINO_INPUT_NONE)
  2539. return -EINVAL;
  2540. return vino_set_input(vcs, input);
  2541. }
  2542. static int vino_v4l2_enumstd(struct vino_channel_settings *vcs,
  2543. struct v4l2_standard *s)
  2544. {
  2545. int index = s->index;
  2546. int data_norm;
  2547. data_norm = vino_enum_data_norm(vcs, index);
  2548. dprintk("standard index = %d\n", index);
  2549. if (data_norm == VINO_DATA_NORM_NONE)
  2550. return -EINVAL;
  2551. dprintk("standard name = %s\n",
  2552. vino_data_norms[data_norm].description);
  2553. memset(s, 0, sizeof(struct v4l2_standard));
  2554. s->index = index;
  2555. s->id = vino_data_norms[data_norm].std;
  2556. s->frameperiod.numerator = 1;
  2557. s->frameperiod.denominator =
  2558. vino_data_norms[data_norm].fps_max;
  2559. s->framelines =
  2560. vino_data_norms[data_norm].framelines;
  2561. strcpy(s->name,
  2562. vino_data_norms[data_norm].description);
  2563. return 0;
  2564. }
  2565. static int vino_v4l2_querystd(struct vino_channel_settings *vcs,
  2566. v4l2_std_id *std)
  2567. {
  2568. unsigned long flags;
  2569. int err = 0;
  2570. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2571. switch (vcs->input) {
  2572. case VINO_INPUT_D1:
  2573. *std = vino_inputs[vcs->input].std;
  2574. break;
  2575. case VINO_INPUT_COMPOSITE:
  2576. case VINO_INPUT_SVIDEO: {
  2577. struct saa7191_status status;
  2578. i2c_decoder_command(DECODER_SAA7191_GET_STATUS, &status);
  2579. if (status.signal) {
  2580. if (status.signal_60hz) {
  2581. *std = V4L2_STD_NTSC;
  2582. } else {
  2583. *std = V4L2_STD_PAL | V4L2_STD_SECAM;
  2584. }
  2585. } else {
  2586. *std = vino_inputs[vcs->input].std;
  2587. }
  2588. break;
  2589. }
  2590. default:
  2591. err = -EINVAL;
  2592. }
  2593. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2594. return err;
  2595. }
  2596. static int vino_v4l2_g_std(struct vino_channel_settings *vcs,
  2597. v4l2_std_id *std)
  2598. {
  2599. unsigned long flags;
  2600. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2601. *std = vino_data_norms[vcs->data_norm].std;
  2602. dprintk("current standard = %d\n", vcs->data_norm);
  2603. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2604. return 0;
  2605. }
  2606. static int vino_v4l2_s_std(struct vino_channel_settings *vcs,
  2607. v4l2_std_id *std)
  2608. {
  2609. unsigned long flags;
  2610. int ret = 0;
  2611. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2612. if (!vino_is_input_owner(vcs)) {
  2613. ret = -EBUSY;
  2614. goto out;
  2615. }
  2616. /* check if the standard is valid for the current input */
  2617. if ((*std) & vino_inputs[vcs->input].std) {
  2618. dprintk("standard accepted\n");
  2619. /* change the video norm for SAA7191
  2620. * and accept NTSC for D1 (do nothing) */
  2621. if (vcs->input == VINO_INPUT_D1)
  2622. goto out;
  2623. if (((*std) & V4L2_STD_PAL)
  2624. && ((*std) & V4L2_STD_NTSC)
  2625. && ((*std) & V4L2_STD_SECAM)) {
  2626. ret = vino_set_data_norm(vcs, VINO_DATA_NORM_AUTO_EXT,
  2627. &flags);
  2628. } else if ((*std) & V4L2_STD_PAL) {
  2629. ret = vino_set_data_norm(vcs, VINO_DATA_NORM_PAL,
  2630. &flags);
  2631. } else if ((*std) & V4L2_STD_NTSC) {
  2632. ret = vino_set_data_norm(vcs, VINO_DATA_NORM_NTSC,
  2633. &flags);
  2634. } else if ((*std) & V4L2_STD_SECAM) {
  2635. ret = vino_set_data_norm(vcs, VINO_DATA_NORM_SECAM,
  2636. &flags);
  2637. } else {
  2638. ret = -EINVAL;
  2639. }
  2640. if (ret) {
  2641. ret = -EINVAL;
  2642. }
  2643. } else {
  2644. ret = -EINVAL;
  2645. }
  2646. out:
  2647. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2648. return ret;
  2649. }
  2650. static int vino_v4l2_enum_fmt(struct vino_channel_settings *vcs,
  2651. struct v4l2_fmtdesc *fd)
  2652. {
  2653. enum v4l2_buf_type type = fd->type;
  2654. int index = fd->index;
  2655. dprintk("format index = %d\n", index);
  2656. switch (fd->type) {
  2657. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  2658. if ((fd->index < 0) ||
  2659. (fd->index >= VINO_DATA_FMT_COUNT))
  2660. return -EINVAL;
  2661. dprintk("format name = %s\n",
  2662. vino_data_formats[index].description);
  2663. memset(fd, 0, sizeof(struct v4l2_fmtdesc));
  2664. fd->index = index;
  2665. fd->type = type;
  2666. fd->pixelformat = vino_data_formats[index].pixelformat;
  2667. strcpy(fd->description, vino_data_formats[index].description);
  2668. break;
  2669. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2670. default:
  2671. return -EINVAL;
  2672. }
  2673. return 0;
  2674. }
  2675. static int vino_v4l2_try_fmt(struct vino_channel_settings *vcs,
  2676. struct v4l2_format *f)
  2677. {
  2678. struct vino_channel_settings tempvcs;
  2679. unsigned long flags;
  2680. switch (f->type) {
  2681. case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
  2682. struct v4l2_pix_format *pf = &f->fmt.pix;
  2683. dprintk("requested: w = %d, h = %d\n",
  2684. pf->width, pf->height);
  2685. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2686. memcpy(&tempvcs, vcs, sizeof(struct vino_channel_settings));
  2687. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2688. tempvcs.data_format = vino_find_data_format(pf->pixelformat);
  2689. if (tempvcs.data_format == VINO_DATA_FMT_NONE) {
  2690. tempvcs.data_format = VINO_DATA_FMT_GREY;
  2691. pf->pixelformat =
  2692. vino_data_formats[tempvcs.data_format].
  2693. pixelformat;
  2694. }
  2695. /* data format must be set before clipping/scaling */
  2696. vino_set_scaling(&tempvcs, pf->width, pf->height);
  2697. dprintk("data format = %s\n",
  2698. vino_data_formats[tempvcs.data_format].description);
  2699. pf->width = (tempvcs.clipping.right - tempvcs.clipping.left) /
  2700. tempvcs.decimation;
  2701. pf->height = (tempvcs.clipping.bottom - tempvcs.clipping.top) /
  2702. tempvcs.decimation;
  2703. pf->field = V4L2_FIELD_INTERLACED;
  2704. pf->bytesperline = tempvcs.line_size;
  2705. pf->sizeimage = tempvcs.line_size *
  2706. (tempvcs.clipping.bottom - tempvcs.clipping.top) /
  2707. tempvcs.decimation;
  2708. pf->colorspace =
  2709. vino_data_formats[tempvcs.data_format].colorspace;
  2710. pf->priv = 0;
  2711. break;
  2712. }
  2713. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2714. default:
  2715. return -EINVAL;
  2716. }
  2717. return 0;
  2718. }
  2719. static int vino_v4l2_g_fmt(struct vino_channel_settings *vcs,
  2720. struct v4l2_format *f)
  2721. {
  2722. unsigned long flags;
  2723. switch (f->type) {
  2724. case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
  2725. struct v4l2_pix_format *pf = &f->fmt.pix;
  2726. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2727. pf->width = (vcs->clipping.right - vcs->clipping.left) /
  2728. vcs->decimation;
  2729. pf->height = (vcs->clipping.bottom - vcs->clipping.top) /
  2730. vcs->decimation;
  2731. pf->pixelformat =
  2732. vino_data_formats[vcs->data_format].pixelformat;
  2733. pf->field = V4L2_FIELD_INTERLACED;
  2734. pf->bytesperline = vcs->line_size;
  2735. pf->sizeimage = vcs->line_size *
  2736. (vcs->clipping.bottom - vcs->clipping.top) /
  2737. vcs->decimation;
  2738. pf->colorspace =
  2739. vino_data_formats[vcs->data_format].colorspace;
  2740. pf->priv = 0;
  2741. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2742. break;
  2743. }
  2744. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2745. default:
  2746. return -EINVAL;
  2747. }
  2748. return 0;
  2749. }
  2750. static int vino_v4l2_s_fmt(struct vino_channel_settings *vcs,
  2751. struct v4l2_format *f)
  2752. {
  2753. int data_format;
  2754. unsigned long flags;
  2755. switch (f->type) {
  2756. case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
  2757. struct v4l2_pix_format *pf = &f->fmt.pix;
  2758. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2759. data_format = vino_find_data_format(pf->pixelformat);
  2760. if (data_format == VINO_DATA_FMT_NONE) {
  2761. vcs->data_format = VINO_DATA_FMT_GREY;
  2762. pf->pixelformat =
  2763. vino_data_formats[vcs->data_format].
  2764. pixelformat;
  2765. } else {
  2766. vcs->data_format = data_format;
  2767. }
  2768. /* data format must be set before clipping/scaling */
  2769. vino_set_scaling(vcs, pf->width, pf->height);
  2770. dprintk("data format = %s\n",
  2771. vino_data_formats[vcs->data_format].description);
  2772. pf->width = vcs->clipping.right - vcs->clipping.left;
  2773. pf->height = vcs->clipping.bottom - vcs->clipping.top;
  2774. pf->field = V4L2_FIELD_INTERLACED;
  2775. pf->bytesperline = vcs->line_size;
  2776. pf->sizeimage = vcs->line_size *
  2777. (vcs->clipping.bottom - vcs->clipping.top) /
  2778. vcs->decimation;
  2779. pf->colorspace =
  2780. vino_data_formats[vcs->data_format].colorspace;
  2781. pf->priv = 0;
  2782. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2783. break;
  2784. }
  2785. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2786. default:
  2787. return -EINVAL;
  2788. }
  2789. return 0;
  2790. }
  2791. static int vino_v4l2_cropcap(struct vino_channel_settings *vcs,
  2792. struct v4l2_cropcap *ccap)
  2793. {
  2794. const struct vino_data_norm *norm;
  2795. unsigned long flags;
  2796. switch (ccap->type) {
  2797. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  2798. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2799. norm = &vino_data_norms[vcs->data_norm];
  2800. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2801. ccap->bounds.left = 0;
  2802. ccap->bounds.top = 0;
  2803. ccap->bounds.width = norm->width;
  2804. ccap->bounds.height = norm->height;
  2805. memcpy(&ccap->defrect, &ccap->bounds,
  2806. sizeof(struct v4l2_rect));
  2807. ccap->pixelaspect.numerator = 1;
  2808. ccap->pixelaspect.denominator = 1;
  2809. break;
  2810. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2811. default:
  2812. return -EINVAL;
  2813. }
  2814. return 0;
  2815. }
  2816. static int vino_v4l2_g_crop(struct vino_channel_settings *vcs,
  2817. struct v4l2_crop *c)
  2818. {
  2819. unsigned long flags;
  2820. switch (c->type) {
  2821. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  2822. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2823. c->c.left = vcs->clipping.left;
  2824. c->c.top = vcs->clipping.top;
  2825. c->c.width = vcs->clipping.right - vcs->clipping.left;
  2826. c->c.height = vcs->clipping.bottom - vcs->clipping.top;
  2827. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2828. break;
  2829. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2830. default:
  2831. return -EINVAL;
  2832. }
  2833. return 0;
  2834. }
  2835. static int vino_v4l2_s_crop(struct vino_channel_settings *vcs,
  2836. struct v4l2_crop *c)
  2837. {
  2838. unsigned long flags;
  2839. switch (c->type) {
  2840. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  2841. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2842. vino_set_clipping(vcs, c->c.left, c->c.top,
  2843. c->c.width, c->c.height);
  2844. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2845. break;
  2846. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2847. default:
  2848. return -EINVAL;
  2849. }
  2850. return 0;
  2851. }
  2852. static int vino_v4l2_g_parm(struct vino_channel_settings *vcs,
  2853. struct v4l2_streamparm *sp)
  2854. {
  2855. unsigned long flags;
  2856. switch (sp->type) {
  2857. case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
  2858. struct v4l2_captureparm *cp = &sp->parm.capture;
  2859. memset(cp, 0, sizeof(struct v4l2_captureparm));
  2860. cp->capability = V4L2_CAP_TIMEPERFRAME;
  2861. cp->timeperframe.numerator = 1;
  2862. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2863. cp->timeperframe.denominator = vcs->fps;
  2864. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2865. // TODO: cp->readbuffers = xxx;
  2866. break;
  2867. }
  2868. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2869. default:
  2870. return -EINVAL;
  2871. }
  2872. return 0;
  2873. }
  2874. static int vino_v4l2_s_parm(struct vino_channel_settings *vcs,
  2875. struct v4l2_streamparm *sp)
  2876. {
  2877. unsigned long flags;
  2878. switch (sp->type) {
  2879. case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
  2880. struct v4l2_captureparm *cp = &sp->parm.capture;
  2881. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2882. if ((cp->timeperframe.numerator == 0) ||
  2883. (cp->timeperframe.denominator == 0)) {
  2884. /* reset framerate */
  2885. vino_set_default_framerate(vcs);
  2886. } else {
  2887. vino_set_framerate(vcs, cp->timeperframe.denominator /
  2888. cp->timeperframe.numerator);
  2889. }
  2890. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2891. // TODO: set buffers according to cp->readbuffers
  2892. break;
  2893. }
  2894. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2895. default:
  2896. return -EINVAL;
  2897. }
  2898. return 0;
  2899. }
  2900. static int vino_v4l2_reqbufs(struct vino_channel_settings *vcs,
  2901. struct v4l2_requestbuffers *rb)
  2902. {
  2903. if (vcs->reading)
  2904. return -EBUSY;
  2905. switch (rb->type) {
  2906. case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
  2907. // TODO: check queue type
  2908. if (rb->memory != V4L2_MEMORY_MMAP) {
  2909. dprintk("type not mmap\n");
  2910. return -EINVAL;
  2911. }
  2912. dprintk("count = %d\n", rb->count);
  2913. if (rb->count > 0) {
  2914. if (vino_is_capturing(vcs)) {
  2915. dprintk("busy, capturing\n");
  2916. return -EBUSY;
  2917. }
  2918. if (vino_queue_has_mapped_buffers(&vcs->fb_queue)) {
  2919. dprintk("busy, buffers still mapped\n");
  2920. return -EBUSY;
  2921. } else {
  2922. vcs->streaming = 0;
  2923. vino_queue_free(&vcs->fb_queue);
  2924. vino_queue_init(&vcs->fb_queue, &rb->count);
  2925. }
  2926. } else {
  2927. vcs->streaming = 0;
  2928. vino_capture_stop(vcs);
  2929. vino_queue_free(&vcs->fb_queue);
  2930. }
  2931. break;
  2932. }
  2933. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2934. default:
  2935. return -EINVAL;
  2936. }
  2937. return 0;
  2938. }
  2939. static void vino_v4l2_get_buffer_status(struct vino_channel_settings *vcs,
  2940. struct vino_framebuffer *fb,
  2941. struct v4l2_buffer *b)
  2942. {
  2943. if (vino_queue_outgoing_contains(&vcs->fb_queue,
  2944. fb->id)) {
  2945. b->flags &= ~V4L2_BUF_FLAG_QUEUED;
  2946. b->flags |= V4L2_BUF_FLAG_DONE;
  2947. } else if (vino_queue_incoming_contains(&vcs->fb_queue,
  2948. fb->id)) {
  2949. b->flags &= ~V4L2_BUF_FLAG_DONE;
  2950. b->flags |= V4L2_BUF_FLAG_QUEUED;
  2951. } else {
  2952. b->flags &= ~(V4L2_BUF_FLAG_DONE |
  2953. V4L2_BUF_FLAG_QUEUED);
  2954. }
  2955. b->flags &= ~(V4L2_BUF_FLAG_TIMECODE);
  2956. if (fb->map_count > 0)
  2957. b->flags |= V4L2_BUF_FLAG_MAPPED;
  2958. b->index = fb->id;
  2959. b->memory = (vcs->fb_queue.type == VINO_MEMORY_MMAP) ?
  2960. V4L2_MEMORY_MMAP : V4L2_MEMORY_USERPTR;
  2961. b->m.offset = fb->offset;
  2962. b->bytesused = fb->data_size;
  2963. b->length = fb->size;
  2964. b->field = V4L2_FIELD_INTERLACED;
  2965. b->sequence = fb->frame_counter;
  2966. memcpy(&b->timestamp, &fb->timestamp,
  2967. sizeof(struct timeval));
  2968. // b->input ?
  2969. dprintk("buffer %d: length = %d, bytesused = %d, offset = %d\n",
  2970. fb->id, fb->size, fb->data_size, fb->offset);
  2971. }
  2972. static int vino_v4l2_querybuf(struct vino_channel_settings *vcs,
  2973. struct v4l2_buffer *b)
  2974. {
  2975. if (vcs->reading)
  2976. return -EBUSY;
  2977. switch (b->type) {
  2978. case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
  2979. struct vino_framebuffer *fb;
  2980. // TODO: check queue type
  2981. if (b->index >= vino_queue_get_length(&vcs->fb_queue)) {
  2982. dprintk("invalid index = %d\n",
  2983. b->index);
  2984. return -EINVAL;
  2985. }
  2986. fb = vino_queue_get_buffer(&vcs->fb_queue,
  2987. b->index);
  2988. if (fb == NULL) {
  2989. dprintk("vino_queue_get_buffer() failed");
  2990. return -EINVAL;
  2991. }
  2992. vino_v4l2_get_buffer_status(vcs, fb, b);
  2993. break;
  2994. }
  2995. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2996. default:
  2997. return -EINVAL;
  2998. }
  2999. return 0;
  3000. }
  3001. static int vino_v4l2_qbuf(struct vino_channel_settings *vcs,
  3002. struct v4l2_buffer *b)
  3003. {
  3004. if (vcs->reading)
  3005. return -EBUSY;
  3006. switch (b->type) {
  3007. case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
  3008. struct vino_framebuffer *fb;
  3009. int ret;
  3010. // TODO: check queue type
  3011. if (b->memory != V4L2_MEMORY_MMAP) {
  3012. dprintk("type not mmap\n");
  3013. return -EINVAL;
  3014. }
  3015. fb = vino_capture_enqueue(vcs, b->index);
  3016. if (fb == NULL)
  3017. return -EINVAL;
  3018. vino_v4l2_get_buffer_status(vcs, fb, b);
  3019. if (vcs->streaming) {
  3020. ret = vino_capture_next(vcs, 1);
  3021. if (ret)
  3022. return ret;
  3023. }
  3024. break;
  3025. }
  3026. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  3027. default:
  3028. return -EINVAL;
  3029. }
  3030. return 0;
  3031. }
  3032. static int vino_v4l2_dqbuf(struct vino_channel_settings *vcs,
  3033. struct v4l2_buffer *b,
  3034. unsigned int nonblocking)
  3035. {
  3036. if (vcs->reading)
  3037. return -EBUSY;
  3038. switch (b->type) {
  3039. case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
  3040. struct vino_framebuffer *fb;
  3041. unsigned int incoming, outgoing;
  3042. int err;
  3043. // TODO: check queue type
  3044. err = vino_queue_get_incoming(&vcs->fb_queue, &incoming);
  3045. if (err) {
  3046. dprintk("vino_queue_get_incoming() failed\n");
  3047. return -EINVAL;
  3048. }
  3049. err = vino_queue_get_outgoing(&vcs->fb_queue, &outgoing);
  3050. if (err) {
  3051. dprintk("vino_queue_get_outgoing() failed\n");
  3052. return -EINVAL;
  3053. }
  3054. dprintk("incoming = %d, outgoing = %d\n", incoming, outgoing);
  3055. if (outgoing == 0) {
  3056. if (incoming == 0) {
  3057. dprintk("no incoming or outgoing buffers\n");
  3058. return -EINVAL;
  3059. }
  3060. if (nonblocking) {
  3061. dprintk("non-blocking I/O was selected and "
  3062. "there are no buffers to dequeue\n");
  3063. return -EAGAIN;
  3064. }
  3065. err = vino_wait_for_frame(vcs);
  3066. if (err) {
  3067. err = vino_wait_for_frame(vcs);
  3068. if (err) {
  3069. /* interrupted or
  3070. * no frames captured because
  3071. * of frame skipping */
  3072. // vino_capture_failed(vcs);
  3073. return -EIO;
  3074. }
  3075. }
  3076. }
  3077. fb = vino_queue_remove(&vcs->fb_queue, &b->index);
  3078. if (fb == NULL) {
  3079. dprintk("vino_queue_remove() failed\n");
  3080. return -EINVAL;
  3081. }
  3082. err = vino_check_buffer(vcs, fb);
  3083. vino_v4l2_get_buffer_status(vcs, fb, b);
  3084. if (err)
  3085. return -EIO;
  3086. break;
  3087. }
  3088. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  3089. default:
  3090. return -EINVAL;
  3091. }
  3092. return 0;
  3093. }
  3094. static int vino_v4l2_streamon(struct vino_channel_settings *vcs)
  3095. {
  3096. unsigned int incoming;
  3097. int ret;
  3098. if (vcs->reading)
  3099. return -EBUSY;
  3100. if (vcs->streaming)
  3101. return 0;
  3102. // TODO: check queue type
  3103. if (vino_queue_get_length(&vcs->fb_queue) < 1) {
  3104. dprintk("no buffers allocated\n");
  3105. return -EINVAL;
  3106. }
  3107. ret = vino_queue_get_incoming(&vcs->fb_queue, &incoming);
  3108. if (ret) {
  3109. dprintk("vino_queue_get_incoming() failed\n");
  3110. return -EINVAL;
  3111. }
  3112. vcs->streaming = 1;
  3113. if (incoming > 0) {
  3114. ret = vino_capture_next(vcs, 1);
  3115. if (ret) {
  3116. vcs->streaming = 0;
  3117. dprintk("couldn't start capture\n");
  3118. return -EINVAL;
  3119. }
  3120. }
  3121. return 0;
  3122. }
  3123. static int vino_v4l2_streamoff(struct vino_channel_settings *vcs)
  3124. {
  3125. if (vcs->reading)
  3126. return -EBUSY;
  3127. if (!vcs->streaming)
  3128. return 0;
  3129. vcs->streaming = 0;
  3130. vino_capture_stop(vcs);
  3131. return 0;
  3132. }
  3133. static int vino_v4l2_queryctrl(struct vino_channel_settings *vcs,
  3134. struct v4l2_queryctrl *queryctrl)
  3135. {
  3136. unsigned long flags;
  3137. int i;
  3138. int err = 0;
  3139. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  3140. switch (vcs->input) {
  3141. case VINO_INPUT_D1:
  3142. for (i = 0; i < VINO_INDYCAM_V4L2_CONTROL_COUNT; i++) {
  3143. if (vino_indycam_v4l2_controls[i].id ==
  3144. queryctrl->id) {
  3145. memcpy(queryctrl,
  3146. &vino_indycam_v4l2_controls[i],
  3147. sizeof(struct v4l2_queryctrl));
  3148. queryctrl->reserved[0] = 0;
  3149. goto found;
  3150. }
  3151. }
  3152. err = -EINVAL;
  3153. break;
  3154. case VINO_INPUT_COMPOSITE:
  3155. case VINO_INPUT_SVIDEO:
  3156. for (i = 0; i < VINO_SAA7191_V4L2_CONTROL_COUNT; i++) {
  3157. if (vino_saa7191_v4l2_controls[i].id ==
  3158. queryctrl->id) {
  3159. memcpy(queryctrl,
  3160. &vino_saa7191_v4l2_controls[i],
  3161. sizeof(struct v4l2_queryctrl));
  3162. queryctrl->reserved[0] = 0;
  3163. goto found;
  3164. }
  3165. }
  3166. err = -EINVAL;
  3167. break;
  3168. default:
  3169. err = -EINVAL;
  3170. }
  3171. found:
  3172. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  3173. return err;
  3174. }
  3175. static int vino_v4l2_g_ctrl(struct vino_channel_settings *vcs,
  3176. struct v4l2_control *control)
  3177. {
  3178. unsigned long flags;
  3179. int i;
  3180. int err = 0;
  3181. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  3182. switch (vcs->input) {
  3183. case VINO_INPUT_D1: {
  3184. struct indycam_control indycam_ctrl;
  3185. for (i = 0; i < VINO_INDYCAM_V4L2_CONTROL_COUNT; i++) {
  3186. if (vino_indycam_v4l2_controls[i].id ==
  3187. control->id) {
  3188. goto found1;
  3189. }
  3190. }
  3191. err = -EINVAL;
  3192. goto out;
  3193. found1:
  3194. indycam_ctrl.type = vino_indycam_v4l2_controls[i].reserved[0];
  3195. err = i2c_camera_command(DECODER_INDYCAM_GET_CONTROL,
  3196. &indycam_ctrl);
  3197. if (err) {
  3198. err = -EINVAL;
  3199. goto out;
  3200. }
  3201. control->value = indycam_ctrl.value;
  3202. break;
  3203. }
  3204. case VINO_INPUT_COMPOSITE:
  3205. case VINO_INPUT_SVIDEO: {
  3206. struct saa7191_control saa7191_ctrl;
  3207. for (i = 0; i < VINO_SAA7191_V4L2_CONTROL_COUNT; i++) {
  3208. if (vino_saa7191_v4l2_controls[i].id ==
  3209. control->id) {
  3210. goto found2;
  3211. }
  3212. }
  3213. err = -EINVAL;
  3214. goto out;
  3215. found2:
  3216. saa7191_ctrl.type = vino_saa7191_v4l2_controls[i].reserved[0];
  3217. err = i2c_decoder_command(DECODER_SAA7191_GET_CONTROL,
  3218. &saa7191_ctrl);
  3219. if (err) {
  3220. err = -EINVAL;
  3221. goto out;
  3222. }
  3223. control->value = saa7191_ctrl.value;
  3224. break;
  3225. }
  3226. default:
  3227. err = -EINVAL;
  3228. }
  3229. out:
  3230. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  3231. return err;
  3232. }
  3233. static int vino_v4l2_s_ctrl(struct vino_channel_settings *vcs,
  3234. struct v4l2_control *control)
  3235. {
  3236. unsigned long flags;
  3237. int i;
  3238. int err = 0;
  3239. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  3240. if (!vino_is_input_owner(vcs)) {
  3241. err = -EBUSY;
  3242. goto out;
  3243. }
  3244. switch (vcs->input) {
  3245. case VINO_INPUT_D1: {
  3246. struct indycam_control indycam_ctrl;
  3247. for (i = 0; i < VINO_INDYCAM_V4L2_CONTROL_COUNT; i++) {
  3248. if (vino_indycam_v4l2_controls[i].id ==
  3249. control->id) {
  3250. if ((control->value >=
  3251. vino_indycam_v4l2_controls[i].minimum)
  3252. && (control->value <=
  3253. vino_indycam_v4l2_controls[i].
  3254. maximum)) {
  3255. goto found1;
  3256. } else {
  3257. err = -ERANGE;
  3258. goto out;
  3259. }
  3260. }
  3261. }
  3262. err = -EINVAL;
  3263. goto out;
  3264. found1:
  3265. indycam_ctrl.type = vino_indycam_v4l2_controls[i].reserved[0];
  3266. indycam_ctrl.value = control->value;
  3267. err = i2c_camera_command(DECODER_INDYCAM_SET_CONTROL,
  3268. &indycam_ctrl);
  3269. if (err)
  3270. err = -EINVAL;
  3271. break;
  3272. }
  3273. case VINO_INPUT_COMPOSITE:
  3274. case VINO_INPUT_SVIDEO: {
  3275. struct saa7191_control saa7191_ctrl;
  3276. for (i = 0; i < VINO_SAA7191_V4L2_CONTROL_COUNT; i++) {
  3277. if (vino_saa7191_v4l2_controls[i].id ==
  3278. control->id) {
  3279. if ((control->value >=
  3280. vino_saa7191_v4l2_controls[i].minimum)
  3281. && (control->value <=
  3282. vino_saa7191_v4l2_controls[i].
  3283. maximum)) {
  3284. goto found2;
  3285. } else {
  3286. err = -ERANGE;
  3287. goto out;
  3288. }
  3289. }
  3290. }
  3291. err = -EINVAL;
  3292. goto out;
  3293. found2:
  3294. saa7191_ctrl.type = vino_saa7191_v4l2_controls[i].reserved[0];
  3295. saa7191_ctrl.value = control->value;
  3296. err = i2c_decoder_command(DECODER_SAA7191_SET_CONTROL,
  3297. &saa7191_ctrl);
  3298. if (err)
  3299. err = -EINVAL;
  3300. break;
  3301. }
  3302. default:
  3303. err = -EINVAL;
  3304. }
  3305. out:
  3306. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  3307. return err;
  3308. }
  3309. /* File operations */
  3310. static int vino_open(struct inode *inode, struct file *file)
  3311. {
  3312. struct video_device *dev = video_devdata(file);
  3313. struct vino_channel_settings *vcs = video_get_drvdata(dev);
  3314. int ret = 0;
  3315. dprintk("open(): channel = %c\n",
  3316. (vcs->channel == VINO_CHANNEL_A) ? 'A' : 'B');
  3317. mutex_lock(&vcs->mutex);
  3318. if (vcs->users) {
  3319. dprintk("open(): driver busy\n");
  3320. ret = -EBUSY;
  3321. goto out;
  3322. }
  3323. ret = vino_acquire_input(vcs);
  3324. if (ret) {
  3325. dprintk("open(): vino_acquire_input() failed\n");
  3326. goto out;
  3327. }
  3328. vcs->users++;
  3329. out:
  3330. mutex_unlock(&vcs->mutex);
  3331. dprintk("open(): %s!\n", ret ? "failed" : "complete");
  3332. return ret;
  3333. }
  3334. static int vino_close(struct inode *inode, struct file *file)
  3335. {
  3336. struct video_device *dev = video_devdata(file);
  3337. struct vino_channel_settings *vcs = video_get_drvdata(dev);
  3338. dprintk("close():\n");
  3339. mutex_lock(&vcs->mutex);
  3340. vcs->users--;
  3341. if (!vcs->users) {
  3342. vino_release_input(vcs);
  3343. /* stop DMA and free buffers */
  3344. vino_capture_stop(vcs);
  3345. vino_queue_free(&vcs->fb_queue);
  3346. }
  3347. mutex_unlock(&vcs->mutex);
  3348. return 0;
  3349. }
  3350. static void vino_vm_open(struct vm_area_struct *vma)
  3351. {
  3352. struct vino_framebuffer *fb = vma->vm_private_data;
  3353. fb->map_count++;
  3354. dprintk("vino_vm_open(): count = %d\n", fb->map_count);
  3355. }
  3356. static void vino_vm_close(struct vm_area_struct *vma)
  3357. {
  3358. struct vino_framebuffer *fb = vma->vm_private_data;
  3359. fb->map_count--;
  3360. dprintk("vino_vm_close(): count = %d\n", fb->map_count);
  3361. }
  3362. static struct vm_operations_struct vino_vm_ops = {
  3363. .open = vino_vm_open,
  3364. .close = vino_vm_close,
  3365. };
  3366. static int vino_mmap(struct file *file, struct vm_area_struct *vma)
  3367. {
  3368. struct video_device *dev = video_devdata(file);
  3369. struct vino_channel_settings *vcs = video_get_drvdata(dev);
  3370. unsigned long start = vma->vm_start;
  3371. unsigned long size = vma->vm_end - vma->vm_start;
  3372. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  3373. struct vino_framebuffer *fb = NULL;
  3374. unsigned int i, length;
  3375. int ret = 0;
  3376. dprintk("mmap():\n");
  3377. // TODO: reject mmap if already mapped
  3378. if (mutex_lock_interruptible(&vcs->mutex))
  3379. return -EINTR;
  3380. if (vcs->reading) {
  3381. ret = -EBUSY;
  3382. goto out;
  3383. }
  3384. // TODO: check queue type
  3385. if (!(vma->vm_flags & VM_WRITE)) {
  3386. dprintk("mmap(): app bug: PROT_WRITE please\n");
  3387. ret = -EINVAL;
  3388. goto out;
  3389. }
  3390. if (!(vma->vm_flags & VM_SHARED)) {
  3391. dprintk("mmap(): app bug: MAP_SHARED please\n");
  3392. ret = -EINVAL;
  3393. goto out;
  3394. }
  3395. /* find the correct buffer using offset */
  3396. length = vino_queue_get_length(&vcs->fb_queue);
  3397. if (length == 0) {
  3398. dprintk("mmap(): queue not initialized\n");
  3399. ret = -EINVAL;
  3400. goto out;
  3401. }
  3402. for (i = 0; i < length; i++) {
  3403. fb = vino_queue_get_buffer(&vcs->fb_queue, i);
  3404. if (fb == NULL) {
  3405. dprintk("mmap(): vino_queue_get_buffer() failed\n");
  3406. ret = -EINVAL;
  3407. goto out;
  3408. }
  3409. if (fb->offset == offset)
  3410. goto found;
  3411. }
  3412. dprintk("mmap(): invalid offset = %lu\n", offset);
  3413. ret = -EINVAL;
  3414. goto out;
  3415. found:
  3416. dprintk("mmap(): buffer = %d\n", i);
  3417. if (size > (fb->desc_table.page_count * PAGE_SIZE)) {
  3418. dprintk("mmap(): failed: size = %lu > %lu\n",
  3419. size, fb->desc_table.page_count * PAGE_SIZE);
  3420. ret = -EINVAL;
  3421. goto out;
  3422. }
  3423. for (i = 0; i < fb->desc_table.page_count; i++) {
  3424. unsigned long pfn =
  3425. virt_to_phys((void *)fb->desc_table.virtual[i]) >>
  3426. PAGE_SHIFT;
  3427. if (size < PAGE_SIZE)
  3428. break;
  3429. // protection was: PAGE_READONLY
  3430. if (remap_pfn_range(vma, start, pfn, PAGE_SIZE,
  3431. vma->vm_page_prot)) {
  3432. dprintk("mmap(): remap_pfn_range() failed\n");
  3433. ret = -EAGAIN;
  3434. goto out;
  3435. }
  3436. start += PAGE_SIZE;
  3437. size -= PAGE_SIZE;
  3438. }
  3439. fb->map_count = 1;
  3440. vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED;
  3441. vma->vm_flags &= ~VM_IO;
  3442. vma->vm_private_data = fb;
  3443. vma->vm_file = file;
  3444. vma->vm_ops = &vino_vm_ops;
  3445. out:
  3446. mutex_unlock(&vcs->mutex);
  3447. return ret;
  3448. }
  3449. static unsigned int vino_poll(struct file *file, poll_table *pt)
  3450. {
  3451. struct video_device *dev = video_devdata(file);
  3452. struct vino_channel_settings *vcs = video_get_drvdata(dev);
  3453. unsigned int outgoing;
  3454. unsigned int ret = 0;
  3455. // lock mutex (?)
  3456. // TODO: this has to be corrected for different read modes
  3457. dprintk("poll():\n");
  3458. if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
  3459. dprintk("poll(): vino_queue_get_outgoing() failed\n");
  3460. ret = POLLERR;
  3461. goto error;
  3462. }
  3463. if (outgoing > 0)
  3464. goto over;
  3465. poll_wait(file, &vcs->fb_queue.frame_wait_queue, pt);
  3466. if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
  3467. dprintk("poll(): vino_queue_get_outgoing() failed\n");
  3468. ret = POLLERR;
  3469. goto error;
  3470. }
  3471. over:
  3472. dprintk("poll(): data %savailable\n",
  3473. (outgoing > 0) ? "" : "not ");
  3474. if (outgoing > 0)
  3475. ret = POLLIN | POLLRDNORM;
  3476. error:
  3477. return ret;
  3478. }
  3479. static int vino_do_ioctl(struct inode *inode, struct file *file,
  3480. unsigned int cmd, void *arg)
  3481. {
  3482. struct video_device *dev = video_devdata(file);
  3483. struct vino_channel_settings *vcs = video_get_drvdata(dev);
  3484. #ifdef VINO_DEBUG
  3485. switch (_IOC_TYPE(cmd)) {
  3486. case 'v':
  3487. dprintk("ioctl(): V4L1 unsupported (0x%08x)\n", cmd);
  3488. break;
  3489. case 'V':
  3490. dprintk("ioctl(): V4L2 %s (0x%08x)\n",
  3491. v4l2_ioctl_names[_IOC_NR(cmd)], cmd);
  3492. break;
  3493. default:
  3494. dprintk("ioctl(): unsupported command 0x%08x\n", cmd);
  3495. }
  3496. #endif
  3497. switch (cmd) {
  3498. /* V4L2 interface */
  3499. case VIDIOC_QUERYCAP: {
  3500. vino_v4l2_querycap(arg);
  3501. break;
  3502. }
  3503. case VIDIOC_ENUMINPUT: {
  3504. return vino_v4l2_enuminput(vcs, arg);
  3505. }
  3506. case VIDIOC_G_INPUT: {
  3507. return vino_v4l2_g_input(vcs, arg);
  3508. }
  3509. case VIDIOC_S_INPUT: {
  3510. return vino_v4l2_s_input(vcs, arg);
  3511. }
  3512. case VIDIOC_ENUMSTD: {
  3513. return vino_v4l2_enumstd(vcs, arg);
  3514. }
  3515. case VIDIOC_QUERYSTD: {
  3516. return vino_v4l2_querystd(vcs, arg);
  3517. }
  3518. case VIDIOC_G_STD: {
  3519. return vino_v4l2_g_std(vcs, arg);
  3520. }
  3521. case VIDIOC_S_STD: {
  3522. return vino_v4l2_s_std(vcs, arg);
  3523. }
  3524. case VIDIOC_ENUM_FMT: {
  3525. return vino_v4l2_enum_fmt(vcs, arg);
  3526. }
  3527. case VIDIOC_TRY_FMT: {
  3528. return vino_v4l2_try_fmt(vcs, arg);
  3529. }
  3530. case VIDIOC_G_FMT: {
  3531. return vino_v4l2_g_fmt(vcs, arg);
  3532. }
  3533. case VIDIOC_S_FMT: {
  3534. return vino_v4l2_s_fmt(vcs, arg);
  3535. }
  3536. case VIDIOC_CROPCAP: {
  3537. return vino_v4l2_cropcap(vcs, arg);
  3538. }
  3539. case VIDIOC_G_CROP: {
  3540. return vino_v4l2_g_crop(vcs, arg);
  3541. }
  3542. case VIDIOC_S_CROP: {
  3543. return vino_v4l2_s_crop(vcs, arg);
  3544. }
  3545. case VIDIOC_G_PARM: {
  3546. return vino_v4l2_g_parm(vcs, arg);
  3547. }
  3548. case VIDIOC_S_PARM: {
  3549. return vino_v4l2_s_parm(vcs, arg);
  3550. }
  3551. case VIDIOC_REQBUFS: {
  3552. return vino_v4l2_reqbufs(vcs, arg);
  3553. }
  3554. case VIDIOC_QUERYBUF: {
  3555. return vino_v4l2_querybuf(vcs, arg);
  3556. }
  3557. case VIDIOC_QBUF: {
  3558. return vino_v4l2_qbuf(vcs, arg);
  3559. }
  3560. case VIDIOC_DQBUF: {
  3561. return vino_v4l2_dqbuf(vcs, arg, file->f_flags & O_NONBLOCK);
  3562. }
  3563. case VIDIOC_STREAMON: {
  3564. return vino_v4l2_streamon(vcs);
  3565. }
  3566. case VIDIOC_STREAMOFF: {
  3567. return vino_v4l2_streamoff(vcs);
  3568. }
  3569. case VIDIOC_QUERYCTRL: {
  3570. return vino_v4l2_queryctrl(vcs, arg);
  3571. }
  3572. case VIDIOC_G_CTRL: {
  3573. return vino_v4l2_g_ctrl(vcs, arg);
  3574. }
  3575. case VIDIOC_S_CTRL: {
  3576. return vino_v4l2_s_ctrl(vcs, arg);
  3577. }
  3578. default:
  3579. return -ENOIOCTLCMD;
  3580. }
  3581. return 0;
  3582. }
  3583. static int vino_ioctl(struct inode *inode, struct file *file,
  3584. unsigned int cmd, unsigned long arg)
  3585. {
  3586. struct video_device *dev = video_devdata(file);
  3587. struct vino_channel_settings *vcs = video_get_drvdata(dev);
  3588. int ret;
  3589. if (mutex_lock_interruptible(&vcs->mutex))
  3590. return -EINTR;
  3591. ret = video_usercopy(inode, file, cmd, arg, vino_do_ioctl);
  3592. mutex_unlock(&vcs->mutex);
  3593. return ret;
  3594. }
  3595. /* Initialization and cleanup */
  3596. // __initdata
  3597. static int vino_init_stage = 0;
  3598. static struct file_operations vino_fops = {
  3599. .owner = THIS_MODULE,
  3600. .open = vino_open,
  3601. .release = vino_close,
  3602. .ioctl = vino_ioctl,
  3603. .mmap = vino_mmap,
  3604. .poll = vino_poll,
  3605. .llseek = no_llseek,
  3606. };
  3607. static struct video_device v4l_device_template = {
  3608. .name = "NOT SET",
  3609. //.type = VID_TYPE_CAPTURE | VID_TYPE_SUBCAPTURE |
  3610. // VID_TYPE_CLIPPING | VID_TYPE_SCALES, VID_TYPE_OVERLAY
  3611. .hardware = VID_HARDWARE_VINO,
  3612. .fops = &vino_fops,
  3613. .minor = -1,
  3614. };
  3615. static void vino_module_cleanup(int stage)
  3616. {
  3617. switch(stage) {
  3618. case 10:
  3619. video_unregister_device(vino_drvdata->b.v4l_device);
  3620. vino_drvdata->b.v4l_device = NULL;
  3621. case 9:
  3622. video_unregister_device(vino_drvdata->a.v4l_device);
  3623. vino_drvdata->a.v4l_device = NULL;
  3624. case 8:
  3625. vino_i2c_del_bus();
  3626. case 7:
  3627. free_irq(SGI_VINO_IRQ, NULL);
  3628. case 6:
  3629. if (vino_drvdata->b.v4l_device) {
  3630. video_device_release(vino_drvdata->b.v4l_device);
  3631. vino_drvdata->b.v4l_device = NULL;
  3632. }
  3633. case 5:
  3634. if (vino_drvdata->a.v4l_device) {
  3635. video_device_release(vino_drvdata->a.v4l_device);
  3636. vino_drvdata->a.v4l_device = NULL;
  3637. }
  3638. case 4:
  3639. /* all entries in dma_cpu dummy table have the same address */
  3640. dma_unmap_single(NULL,
  3641. vino_drvdata->dummy_desc_table.dma_cpu[0],
  3642. PAGE_SIZE, DMA_FROM_DEVICE);
  3643. dma_free_coherent(NULL, VINO_DUMMY_DESC_COUNT
  3644. * sizeof(dma_addr_t),
  3645. (void *)vino_drvdata->
  3646. dummy_desc_table.dma_cpu,
  3647. vino_drvdata->dummy_desc_table.dma);
  3648. case 3:
  3649. free_page(vino_drvdata->dummy_page);
  3650. case 2:
  3651. kfree(vino_drvdata);
  3652. case 1:
  3653. iounmap(vino);
  3654. case 0:
  3655. break;
  3656. default:
  3657. dprintk("vino_module_cleanup(): invalid cleanup stage = %d\n",
  3658. stage);
  3659. }
  3660. }
  3661. static int vino_probe(void)
  3662. {
  3663. unsigned long rev_id;
  3664. if (ip22_is_fullhouse()) {
  3665. printk(KERN_ERR "VINO doesn't exist in IP22 Fullhouse\n");
  3666. return -ENODEV;
  3667. }
  3668. if (!(sgimc->systemid & SGIMC_SYSID_EPRESENT)) {
  3669. printk(KERN_ERR "VINO is not found (EISA BUS not present)\n");
  3670. return -ENODEV;
  3671. }
  3672. vino = (struct sgi_vino *)ioremap(VINO_BASE, sizeof(struct sgi_vino));
  3673. if (!vino) {
  3674. printk(KERN_ERR "VINO: ioremap() failed\n");
  3675. return -EIO;
  3676. }
  3677. vino_init_stage++;
  3678. if (get_dbe(rev_id, &(vino->rev_id))) {
  3679. printk(KERN_ERR "Failed to read VINO revision register\n");
  3680. vino_module_cleanup(vino_init_stage);
  3681. return -ENODEV;
  3682. }
  3683. if (VINO_ID_VALUE(rev_id) != VINO_CHIP_ID) {
  3684. printk(KERN_ERR "Unknown VINO chip ID (Rev/ID: 0x%02lx)\n",
  3685. rev_id);
  3686. vino_module_cleanup(vino_init_stage);
  3687. return -ENODEV;
  3688. }
  3689. printk(KERN_INFO "VINO revision %ld found\n", VINO_REV_NUM(rev_id));
  3690. return 0;
  3691. }
  3692. static int vino_init(void)
  3693. {
  3694. dma_addr_t dma_dummy_address;
  3695. int i;
  3696. vino_drvdata = kzalloc(sizeof(struct vino_settings), GFP_KERNEL);
  3697. if (!vino_drvdata) {
  3698. vino_module_cleanup(vino_init_stage);
  3699. return -ENOMEM;
  3700. }
  3701. vino_init_stage++;
  3702. /* create a dummy dma descriptor */
  3703. vino_drvdata->dummy_page = get_zeroed_page(GFP_KERNEL | GFP_DMA);
  3704. if (!vino_drvdata->dummy_page) {
  3705. vino_module_cleanup(vino_init_stage);
  3706. return -ENOMEM;
  3707. }
  3708. vino_init_stage++;
  3709. // TODO: use page_count in dummy_desc_table
  3710. vino_drvdata->dummy_desc_table.dma_cpu =
  3711. dma_alloc_coherent(NULL,
  3712. VINO_DUMMY_DESC_COUNT * sizeof(dma_addr_t),
  3713. &vino_drvdata->dummy_desc_table.dma,
  3714. GFP_KERNEL | GFP_DMA);
  3715. if (!vino_drvdata->dummy_desc_table.dma_cpu) {
  3716. vino_module_cleanup(vino_init_stage);
  3717. return -ENOMEM;
  3718. }
  3719. vino_init_stage++;
  3720. dma_dummy_address = dma_map_single(NULL,
  3721. (void *)vino_drvdata->dummy_page,
  3722. PAGE_SIZE, DMA_FROM_DEVICE);
  3723. for (i = 0; i < VINO_DUMMY_DESC_COUNT; i++) {
  3724. vino_drvdata->dummy_desc_table.dma_cpu[i] = dma_dummy_address;
  3725. }
  3726. /* initialize VINO */
  3727. vino->control = 0;
  3728. vino->a.next_4_desc = vino_drvdata->dummy_desc_table.dma;
  3729. vino->b.next_4_desc = vino_drvdata->dummy_desc_table.dma;
  3730. udelay(VINO_DESC_FETCH_DELAY);
  3731. vino->intr_status = 0;
  3732. vino->a.fifo_thres = VINO_FIFO_THRESHOLD_DEFAULT;
  3733. vino->b.fifo_thres = VINO_FIFO_THRESHOLD_DEFAULT;
  3734. return 0;
  3735. }
  3736. static int vino_init_channel_settings(struct vino_channel_settings *vcs,
  3737. unsigned int channel, const char *name)
  3738. {
  3739. vcs->channel = channel;
  3740. vcs->input = VINO_INPUT_NONE;
  3741. vcs->alpha = 0;
  3742. vcs->users = 0;
  3743. vcs->data_format = VINO_DATA_FMT_GREY;
  3744. vcs->data_norm = VINO_DATA_NORM_NTSC;
  3745. vcs->decimation = 1;
  3746. vino_set_default_clipping(vcs);
  3747. vino_set_default_framerate(vcs);
  3748. vcs->capturing = 0;
  3749. mutex_init(&vcs->mutex);
  3750. spin_lock_init(&vcs->capture_lock);
  3751. mutex_init(&vcs->fb_queue.queue_mutex);
  3752. spin_lock_init(&vcs->fb_queue.queue_lock);
  3753. init_waitqueue_head(&vcs->fb_queue.frame_wait_queue);
  3754. vcs->v4l_device = video_device_alloc();
  3755. if (!vcs->v4l_device) {
  3756. vino_module_cleanup(vino_init_stage);
  3757. return -ENOMEM;
  3758. }
  3759. vino_init_stage++;
  3760. memcpy(vcs->v4l_device, &v4l_device_template,
  3761. sizeof(struct video_device));
  3762. strcpy(vcs->v4l_device->name, name);
  3763. vcs->v4l_device->release = video_device_release;
  3764. video_set_drvdata(vcs->v4l_device, vcs);
  3765. return 0;
  3766. }
  3767. static int __init vino_module_init(void)
  3768. {
  3769. int ret;
  3770. printk(KERN_INFO "SGI VINO driver version %s\n",
  3771. VINO_MODULE_VERSION);
  3772. ret = vino_probe();
  3773. if (ret)
  3774. return ret;
  3775. ret = vino_init();
  3776. if (ret)
  3777. return ret;
  3778. /* initialize data structures */
  3779. spin_lock_init(&vino_drvdata->vino_lock);
  3780. spin_lock_init(&vino_drvdata->input_lock);
  3781. ret = vino_init_channel_settings(&vino_drvdata->a, VINO_CHANNEL_A,
  3782. vino_v4l_device_name_a);
  3783. if (ret)
  3784. return ret;
  3785. ret = vino_init_channel_settings(&vino_drvdata->b, VINO_CHANNEL_B,
  3786. vino_v4l_device_name_b);
  3787. if (ret)
  3788. return ret;
  3789. /* initialize hardware and register V4L devices */
  3790. ret = request_irq(SGI_VINO_IRQ, vino_interrupt, 0,
  3791. vino_driver_description, NULL);
  3792. if (ret) {
  3793. printk(KERN_ERR "VINO: requesting IRQ %02d failed\n",
  3794. SGI_VINO_IRQ);
  3795. vino_module_cleanup(vino_init_stage);
  3796. return -EAGAIN;
  3797. }
  3798. vino_init_stage++;
  3799. ret = vino_i2c_add_bus();
  3800. if (ret) {
  3801. printk(KERN_ERR "VINO I2C bus registration failed\n");
  3802. vino_module_cleanup(vino_init_stage);
  3803. return ret;
  3804. }
  3805. vino_init_stage++;
  3806. ret = video_register_device(vino_drvdata->a.v4l_device,
  3807. VFL_TYPE_GRABBER, -1);
  3808. if (ret < 0) {
  3809. printk(KERN_ERR "VINO channel A Video4Linux-device "
  3810. "registration failed\n");
  3811. vino_module_cleanup(vino_init_stage);
  3812. return -EINVAL;
  3813. }
  3814. vino_init_stage++;
  3815. ret = video_register_device(vino_drvdata->b.v4l_device,
  3816. VFL_TYPE_GRABBER, -1);
  3817. if (ret < 0) {
  3818. printk(KERN_ERR "VINO channel B Video4Linux-device "
  3819. "registration failed\n");
  3820. vino_module_cleanup(vino_init_stage);
  3821. return -EINVAL;
  3822. }
  3823. vino_init_stage++;
  3824. #if defined(CONFIG_KMOD) && defined(MODULE)
  3825. request_module("saa7191");
  3826. request_module("indycam");
  3827. #endif
  3828. dprintk("init complete!\n");
  3829. return 0;
  3830. }
  3831. static void __exit vino_module_exit(void)
  3832. {
  3833. dprintk("exiting, stage = %d ...\n", vino_init_stage);
  3834. vino_module_cleanup(vino_init_stage);
  3835. dprintk("cleanup complete, exit!\n");
  3836. }
  3837. module_init(vino_module_init);
  3838. module_exit(vino_module_exit);