vino.c 105 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677
  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 <media/v4l2-common.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_del_adapter(&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. /* to ensure that schedule_timeout will return immediately
  1704. * if VINO interrupt was triggered meanwhile */
  1705. schedule_timeout_interruptible(msecs_to_jiffies(100));
  1706. if (signal_pending(current))
  1707. err = -EINTR;
  1708. remove_wait_queue(&vcs->fb_queue.frame_wait_queue, &wait);
  1709. dprintk("vino_wait_for_frame(): waiting for frame %s\n",
  1710. err ? "failed" : "ok");
  1711. return err;
  1712. }
  1713. /* the function assumes that PAGE_SIZE % 4 == 0 */
  1714. static void vino_convert_to_rgba(struct vino_framebuffer *fb) {
  1715. unsigned char *pageptr;
  1716. unsigned int page, i;
  1717. unsigned char a;
  1718. for (page = 0; page < fb->desc_table.page_count; page++) {
  1719. pageptr = (unsigned char *)fb->desc_table.virtual[page];
  1720. for (i = 0; i < PAGE_SIZE; i += 4) {
  1721. a = pageptr[0];
  1722. pageptr[0] = pageptr[3];
  1723. pageptr[1] = pageptr[2];
  1724. pageptr[2] = pageptr[1];
  1725. pageptr[3] = a;
  1726. pageptr += 4;
  1727. }
  1728. }
  1729. }
  1730. /* checks if the buffer is in correct state and syncs data */
  1731. static int vino_check_buffer(struct vino_channel_settings *vcs,
  1732. struct vino_framebuffer *fb)
  1733. {
  1734. int err = 0;
  1735. unsigned long flags;
  1736. dprintk("vino_check_buffer():\n");
  1737. spin_lock_irqsave(&fb->state_lock, flags);
  1738. switch (fb->state) {
  1739. case VINO_FRAMEBUFFER_IN_USE:
  1740. err = -EIO;
  1741. break;
  1742. case VINO_FRAMEBUFFER_READY:
  1743. vino_sync_buffer(fb);
  1744. fb->state = VINO_FRAMEBUFFER_UNUSED;
  1745. break;
  1746. default:
  1747. err = -EINVAL;
  1748. }
  1749. spin_unlock_irqrestore(&fb->state_lock, flags);
  1750. if (!err) {
  1751. if (vino_pixel_conversion
  1752. && (fb->data_format == VINO_DATA_FMT_RGB32)) {
  1753. vino_convert_to_rgba(fb);
  1754. }
  1755. } else if (err && (err != -EINVAL)) {
  1756. dprintk("vino_check_buffer(): buffer not ready\n");
  1757. spin_lock_irqsave(&vino_drvdata->vino_lock, flags);
  1758. vino_dma_stop(vcs);
  1759. vino_clear_interrupt(vcs);
  1760. spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags);
  1761. }
  1762. return err;
  1763. }
  1764. /* forcefully terminates capture */
  1765. static void vino_capture_stop(struct vino_channel_settings *vcs)
  1766. {
  1767. unsigned int incoming = 0, outgoing = 0, id;
  1768. unsigned long flags, flags2;
  1769. dprintk("vino_capture_stop():\n");
  1770. spin_lock_irqsave(&vcs->capture_lock, flags);
  1771. /* unset capturing to stop queue processing */
  1772. vcs->capturing = 0;
  1773. spin_lock_irqsave(&vino_drvdata->vino_lock, flags2);
  1774. vino_dma_stop(vcs);
  1775. vino_clear_interrupt(vcs);
  1776. spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags2);
  1777. /* remove all items from the queue */
  1778. if (vino_queue_get_incoming(&vcs->fb_queue, &incoming)) {
  1779. dprintk("vino_capture_stop(): "
  1780. "vino_queue_get_incoming() failed\n");
  1781. goto out;
  1782. }
  1783. while (incoming > 0) {
  1784. vino_queue_transfer(&vcs->fb_queue);
  1785. if (vino_queue_get_incoming(&vcs->fb_queue, &incoming)) {
  1786. dprintk("vino_capture_stop(): "
  1787. "vino_queue_get_incoming() failed\n");
  1788. goto out;
  1789. }
  1790. }
  1791. if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
  1792. dprintk("vino_capture_stop(): "
  1793. "vino_queue_get_outgoing() failed\n");
  1794. goto out;
  1795. }
  1796. while (outgoing > 0) {
  1797. vino_queue_remove(&vcs->fb_queue, &id);
  1798. if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
  1799. dprintk("vino_capture_stop(): "
  1800. "vino_queue_get_outgoing() failed\n");
  1801. goto out;
  1802. }
  1803. }
  1804. out:
  1805. spin_unlock_irqrestore(&vcs->capture_lock, flags);
  1806. }
  1807. #if 0
  1808. static int vino_capture_failed(struct vino_channel_settings *vcs)
  1809. {
  1810. struct vino_framebuffer *fb;
  1811. unsigned long flags;
  1812. unsigned int i;
  1813. int ret;
  1814. dprintk("vino_capture_failed():\n");
  1815. spin_lock_irqsave(&vino_drvdata->vino_lock, flags);
  1816. vino_dma_stop(vcs);
  1817. vino_clear_interrupt(vcs);
  1818. spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags);
  1819. ret = vino_queue_get_incoming(&vcs->fb_queue, &i);
  1820. if (ret == VINO_QUEUE_ERROR) {
  1821. dprintk("vino_queue_get_incoming() failed\n");
  1822. return -EINVAL;
  1823. }
  1824. if (i == 0) {
  1825. /* no buffers to process */
  1826. return 0;
  1827. }
  1828. fb = vino_queue_peek(&vcs->fb_queue, &i);
  1829. if (fb == NULL) {
  1830. dprintk("vino_queue_peek() failed\n");
  1831. return -EINVAL;
  1832. }
  1833. spin_lock_irqsave(&fb->state_lock, flags);
  1834. if (fb->state == VINO_FRAMEBUFFER_IN_USE) {
  1835. fb->state = VINO_FRAMEBUFFER_UNUSED;
  1836. vino_queue_transfer(&vcs->fb_queue);
  1837. vino_queue_remove(&vcs->fb_queue, &i);
  1838. /* we should actually discard the newest frame,
  1839. * but who cares ... */
  1840. }
  1841. spin_unlock_irqrestore(&fb->state_lock, flags);
  1842. return 0;
  1843. }
  1844. #endif
  1845. static void vino_skip_frame(struct vino_channel_settings *vcs)
  1846. {
  1847. struct vino_framebuffer *fb;
  1848. unsigned long flags;
  1849. unsigned int id;
  1850. spin_lock_irqsave(&vcs->capture_lock, flags);
  1851. fb = vino_queue_peek(&vcs->fb_queue, &id);
  1852. if (!fb) {
  1853. spin_unlock_irqrestore(&vcs->capture_lock, flags);
  1854. dprintk("vino_skip_frame(): vino_queue_peek() failed!\n");
  1855. return;
  1856. }
  1857. spin_unlock_irqrestore(&vcs->capture_lock, flags);
  1858. spin_lock_irqsave(&fb->state_lock, flags);
  1859. fb->state = VINO_FRAMEBUFFER_UNUSED;
  1860. spin_unlock_irqrestore(&fb->state_lock, flags);
  1861. vino_capture_next(vcs, 0);
  1862. }
  1863. static void vino_frame_done(struct vino_channel_settings *vcs)
  1864. {
  1865. struct vino_framebuffer *fb;
  1866. unsigned long flags;
  1867. spin_lock_irqsave(&vcs->capture_lock, flags);
  1868. fb = vino_queue_transfer(&vcs->fb_queue);
  1869. if (!fb) {
  1870. spin_unlock_irqrestore(&vcs->capture_lock, flags);
  1871. dprintk("vino_frame_done(): vino_queue_transfer() failed!\n");
  1872. return;
  1873. }
  1874. spin_unlock_irqrestore(&vcs->capture_lock, flags);
  1875. fb->frame_counter = vcs->int_data.frame_counter;
  1876. memcpy(&fb->timestamp, &vcs->int_data.timestamp,
  1877. sizeof(struct timeval));
  1878. spin_lock_irqsave(&fb->state_lock, flags);
  1879. if (fb->state == VINO_FRAMEBUFFER_IN_USE)
  1880. fb->state = VINO_FRAMEBUFFER_READY;
  1881. spin_unlock_irqrestore(&fb->state_lock, flags);
  1882. wake_up(&vcs->fb_queue.frame_wait_queue);
  1883. vino_capture_next(vcs, 0);
  1884. }
  1885. static void vino_capture_tasklet(unsigned long channel) {
  1886. struct vino_channel_settings *vcs;
  1887. vcs = (channel == VINO_CHANNEL_A)
  1888. ? &vino_drvdata->a : &vino_drvdata->b;
  1889. if (vcs->int_data.skip)
  1890. vcs->int_data.skip_count++;
  1891. if (vcs->int_data.skip && (vcs->int_data.skip_count
  1892. <= VINO_MAX_FRAME_SKIP_COUNT)) {
  1893. vino_skip_frame(vcs);
  1894. } else {
  1895. vcs->int_data.skip_count = 0;
  1896. vino_frame_done(vcs);
  1897. }
  1898. }
  1899. static irqreturn_t vino_interrupt(int irq, void *dev_id)
  1900. {
  1901. u32 ctrl, intr;
  1902. unsigned int fc_a, fc_b;
  1903. int handled_a = 0, skip_a = 0, done_a = 0;
  1904. int handled_b = 0, skip_b = 0, done_b = 0;
  1905. #ifdef VINO_DEBUG_INT
  1906. int loop = 0;
  1907. unsigned int line_count = vino->a.line_count,
  1908. page_index = vino->a.page_index,
  1909. field_counter = vino->a.field_counter,
  1910. start_desc_tbl = vino->a.start_desc_tbl,
  1911. next_4_desc = vino->a.next_4_desc;
  1912. unsigned int line_count_2,
  1913. page_index_2,
  1914. field_counter_2,
  1915. start_desc_tbl_2,
  1916. next_4_desc_2;
  1917. #endif
  1918. spin_lock(&vino_drvdata->vino_lock);
  1919. while ((intr = vino->intr_status)) {
  1920. fc_a = vino->a.field_counter >> 1;
  1921. fc_b = vino->b.field_counter >> 1;
  1922. /* handle error-interrupts in some special way ?
  1923. * --> skips frames */
  1924. if (intr & VINO_INTSTAT_A) {
  1925. if (intr & VINO_INTSTAT_A_EOF) {
  1926. vino_drvdata->a.field++;
  1927. if (vino_drvdata->a.field > 1) {
  1928. vino_dma_stop(&vino_drvdata->a);
  1929. vino_clear_interrupt(&vino_drvdata->a);
  1930. vino_drvdata->a.field = 0;
  1931. done_a = 1;
  1932. } else {
  1933. if (vino->a.page_index
  1934. != vino_drvdata->a.line_size) {
  1935. vino->a.line_count = 0;
  1936. vino->a.page_index =
  1937. vino_drvdata->
  1938. a.line_size;
  1939. vino->a.next_4_desc =
  1940. vino->a.start_desc_tbl;
  1941. }
  1942. }
  1943. dprintk("channel A end-of-field "
  1944. "interrupt: %04x\n", intr);
  1945. } else {
  1946. vino_dma_stop(&vino_drvdata->a);
  1947. vino_clear_interrupt(&vino_drvdata->a);
  1948. vino_drvdata->a.field = 0;
  1949. skip_a = 1;
  1950. dprintk("channel A error interrupt: %04x\n",
  1951. intr);
  1952. }
  1953. #ifdef VINO_DEBUG_INT
  1954. line_count_2 = vino->a.line_count;
  1955. page_index_2 = vino->a.page_index;
  1956. field_counter_2 = vino->a.field_counter;
  1957. start_desc_tbl_2 = vino->a.start_desc_tbl;
  1958. next_4_desc_2 = vino->a.next_4_desc;
  1959. printk("intr = %04x, loop = %d, field = %d\n",
  1960. intr, loop, vino_drvdata->a.field);
  1961. printk("1- line count = %04d, page index = %04d, "
  1962. "start = %08x, next = %08x\n"
  1963. " fieldc = %d, framec = %d\n",
  1964. line_count, page_index, start_desc_tbl,
  1965. next_4_desc, field_counter, fc_a);
  1966. printk("12-line count = %04d, page index = %04d, "
  1967. " start = %08x, next = %08x\n",
  1968. line_count_2, page_index_2, start_desc_tbl_2,
  1969. next_4_desc_2);
  1970. if (done_a)
  1971. printk("\n");
  1972. #endif
  1973. }
  1974. if (intr & VINO_INTSTAT_B) {
  1975. if (intr & VINO_INTSTAT_B_EOF) {
  1976. vino_drvdata->b.field++;
  1977. if (vino_drvdata->b.field > 1) {
  1978. vino_dma_stop(&vino_drvdata->b);
  1979. vino_clear_interrupt(&vino_drvdata->b);
  1980. vino_drvdata->b.field = 0;
  1981. done_b = 1;
  1982. }
  1983. dprintk("channel B end-of-field "
  1984. "interrupt: %04x\n", intr);
  1985. } else {
  1986. vino_dma_stop(&vino_drvdata->b);
  1987. vino_clear_interrupt(&vino_drvdata->b);
  1988. vino_drvdata->b.field = 0;
  1989. skip_b = 1;
  1990. dprintk("channel B error interrupt: %04x\n",
  1991. intr);
  1992. }
  1993. }
  1994. /* Always remember to clear interrupt status.
  1995. * Disable VINO interrupts while we do this. */
  1996. ctrl = vino->control;
  1997. vino->control = ctrl & ~(VINO_CTRL_A_INT | VINO_CTRL_B_INT);
  1998. vino->intr_status = ~intr;
  1999. vino->control = ctrl;
  2000. spin_unlock(&vino_drvdata->vino_lock);
  2001. if ((!handled_a) && (done_a || skip_a)) {
  2002. if (!skip_a) {
  2003. do_gettimeofday(&vino_drvdata->
  2004. a.int_data.timestamp);
  2005. vino_drvdata->a.int_data.frame_counter = fc_a;
  2006. }
  2007. vino_drvdata->a.int_data.skip = skip_a;
  2008. dprintk("channel A %s, interrupt: %d\n",
  2009. skip_a ? "skipping frame" : "frame done",
  2010. intr);
  2011. tasklet_hi_schedule(&vino_tasklet_a);
  2012. handled_a = 1;
  2013. }
  2014. if ((!handled_b) && (done_b || skip_b)) {
  2015. if (!skip_b) {
  2016. do_gettimeofday(&vino_drvdata->
  2017. b.int_data.timestamp);
  2018. vino_drvdata->b.int_data.frame_counter = fc_b;
  2019. }
  2020. vino_drvdata->b.int_data.skip = skip_b;
  2021. dprintk("channel B %s, interrupt: %d\n",
  2022. skip_b ? "skipping frame" : "frame done",
  2023. intr);
  2024. tasklet_hi_schedule(&vino_tasklet_b);
  2025. handled_b = 1;
  2026. }
  2027. #ifdef VINO_DEBUG_INT
  2028. loop++;
  2029. #endif
  2030. spin_lock(&vino_drvdata->vino_lock);
  2031. }
  2032. spin_unlock(&vino_drvdata->vino_lock);
  2033. return IRQ_HANDLED;
  2034. }
  2035. /* VINO video input management */
  2036. static int vino_get_saa7191_input(int input)
  2037. {
  2038. switch (input) {
  2039. case VINO_INPUT_COMPOSITE:
  2040. return SAA7191_INPUT_COMPOSITE;
  2041. case VINO_INPUT_SVIDEO:
  2042. return SAA7191_INPUT_SVIDEO;
  2043. default:
  2044. printk(KERN_ERR "VINO: vino_get_saa7191_input(): "
  2045. "invalid input!\n");
  2046. return -1;
  2047. }
  2048. }
  2049. static int vino_get_saa7191_norm(unsigned int data_norm)
  2050. {
  2051. switch (data_norm) {
  2052. case VINO_DATA_NORM_AUTO:
  2053. return SAA7191_NORM_AUTO;
  2054. case VINO_DATA_NORM_AUTO_EXT:
  2055. return SAA7191_NORM_AUTO_EXT;
  2056. case VINO_DATA_NORM_PAL:
  2057. return SAA7191_NORM_PAL;
  2058. case VINO_DATA_NORM_NTSC:
  2059. return SAA7191_NORM_NTSC;
  2060. case VINO_DATA_NORM_SECAM:
  2061. return SAA7191_NORM_SECAM;
  2062. default:
  2063. printk(KERN_ERR "VINO: vino_get_saa7191_norm(): "
  2064. "invalid norm!\n");
  2065. return -1;
  2066. }
  2067. }
  2068. static int vino_get_from_saa7191_norm(int saa7191_norm)
  2069. {
  2070. switch (saa7191_norm) {
  2071. case SAA7191_NORM_PAL:
  2072. return VINO_DATA_NORM_PAL;
  2073. case SAA7191_NORM_NTSC:
  2074. return VINO_DATA_NORM_NTSC;
  2075. case SAA7191_NORM_SECAM:
  2076. return VINO_DATA_NORM_SECAM;
  2077. default:
  2078. printk(KERN_ERR "VINO: vino_get_from_saa7191_norm(): "
  2079. "invalid norm!\n");
  2080. return VINO_DATA_NORM_NONE;
  2081. }
  2082. }
  2083. static int vino_saa7191_set_norm(unsigned int *data_norm)
  2084. {
  2085. int saa7191_norm, new_data_norm;
  2086. int err = 0;
  2087. saa7191_norm = vino_get_saa7191_norm(*data_norm);
  2088. err = i2c_decoder_command(DECODER_SAA7191_SET_NORM,
  2089. &saa7191_norm);
  2090. if (err)
  2091. goto out;
  2092. if ((*data_norm == VINO_DATA_NORM_AUTO)
  2093. || (*data_norm == VINO_DATA_NORM_AUTO_EXT)) {
  2094. struct saa7191_status status;
  2095. err = i2c_decoder_command(DECODER_SAA7191_GET_STATUS,
  2096. &status);
  2097. if (err)
  2098. goto out;
  2099. new_data_norm =
  2100. vino_get_from_saa7191_norm(status.norm);
  2101. if (new_data_norm == VINO_DATA_NORM_NONE) {
  2102. err = -EINVAL;
  2103. goto out;
  2104. }
  2105. *data_norm = (unsigned int)new_data_norm;
  2106. }
  2107. out:
  2108. return err;
  2109. }
  2110. /* execute with input_lock locked */
  2111. static int vino_is_input_owner(struct vino_channel_settings *vcs)
  2112. {
  2113. switch(vcs->input) {
  2114. case VINO_INPUT_COMPOSITE:
  2115. case VINO_INPUT_SVIDEO:
  2116. return (vino_drvdata->decoder.owner == vcs->channel);
  2117. case VINO_INPUT_D1:
  2118. return (vino_drvdata->camera.owner == vcs->channel);
  2119. default:
  2120. return 0;
  2121. }
  2122. }
  2123. static int vino_acquire_input(struct vino_channel_settings *vcs)
  2124. {
  2125. unsigned long flags;
  2126. int ret = 0;
  2127. dprintk("vino_acquire_input():\n");
  2128. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2129. /* First try D1 and then SAA7191 */
  2130. if (vino_drvdata->camera.driver
  2131. && (vino_drvdata->camera.owner == VINO_NO_CHANNEL)) {
  2132. if (i2c_use_client(vino_drvdata->camera.driver)) {
  2133. ret = -ENODEV;
  2134. goto out;
  2135. }
  2136. vino_drvdata->camera.owner = vcs->channel;
  2137. vcs->input = VINO_INPUT_D1;
  2138. vcs->data_norm = VINO_DATA_NORM_D1;
  2139. } else if (vino_drvdata->decoder.driver
  2140. && (vino_drvdata->decoder.owner == VINO_NO_CHANNEL)) {
  2141. int input, data_norm;
  2142. int saa7191_input;
  2143. if (i2c_use_client(vino_drvdata->decoder.driver)) {
  2144. ret = -ENODEV;
  2145. goto out;
  2146. }
  2147. input = VINO_INPUT_COMPOSITE;
  2148. saa7191_input = vino_get_saa7191_input(input);
  2149. ret = i2c_decoder_command(DECODER_SET_INPUT,
  2150. &saa7191_input);
  2151. if (ret) {
  2152. ret = -EINVAL;
  2153. goto out;
  2154. }
  2155. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2156. /* Don't hold spinlocks while auto-detecting norm
  2157. * as it may take a while... */
  2158. data_norm = VINO_DATA_NORM_AUTO_EXT;
  2159. ret = vino_saa7191_set_norm(&data_norm);
  2160. if ((ret == -EBUSY) || (ret == -EAGAIN)) {
  2161. data_norm = VINO_DATA_NORM_PAL;
  2162. ret = vino_saa7191_set_norm(&data_norm);
  2163. }
  2164. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2165. if (ret) {
  2166. ret = -EINVAL;
  2167. goto out;
  2168. }
  2169. vino_drvdata->decoder.owner = vcs->channel;
  2170. vcs->input = input;
  2171. vcs->data_norm = data_norm;
  2172. } else {
  2173. vcs->input = (vcs->channel == VINO_CHANNEL_A) ?
  2174. vino_drvdata->b.input : vino_drvdata->a.input;
  2175. vcs->data_norm = (vcs->channel == VINO_CHANNEL_A) ?
  2176. vino_drvdata->b.data_norm : vino_drvdata->a.data_norm;
  2177. }
  2178. if (vcs->input == VINO_INPUT_NONE) {
  2179. ret = -ENODEV;
  2180. goto out;
  2181. }
  2182. vino_set_default_clipping(vcs);
  2183. vino_set_default_scaling(vcs);
  2184. vino_set_default_framerate(vcs);
  2185. dprintk("vino_acquire_input(): %s\n", vino_inputs[vcs->input].name);
  2186. out:
  2187. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2188. return ret;
  2189. }
  2190. static int vino_set_input(struct vino_channel_settings *vcs, int input)
  2191. {
  2192. struct vino_channel_settings *vcs2 = (vcs->channel == VINO_CHANNEL_A) ?
  2193. &vino_drvdata->b : &vino_drvdata->a;
  2194. unsigned long flags;
  2195. int ret = 0;
  2196. dprintk("vino_set_input():\n");
  2197. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2198. if (vcs->input == input)
  2199. goto out;
  2200. switch (input) {
  2201. case VINO_INPUT_COMPOSITE:
  2202. case VINO_INPUT_SVIDEO:
  2203. if (!vino_drvdata->decoder.driver) {
  2204. ret = -EINVAL;
  2205. goto out;
  2206. }
  2207. if (vino_drvdata->decoder.owner == VINO_NO_CHANNEL) {
  2208. if (i2c_use_client(vino_drvdata->decoder.driver)) {
  2209. ret = -ENODEV;
  2210. goto out;
  2211. }
  2212. vino_drvdata->decoder.owner = vcs->channel;
  2213. }
  2214. if (vino_drvdata->decoder.owner == vcs->channel) {
  2215. int data_norm;
  2216. int saa7191_input;
  2217. saa7191_input = vino_get_saa7191_input(input);
  2218. ret = i2c_decoder_command(DECODER_SET_INPUT,
  2219. &saa7191_input);
  2220. if (ret) {
  2221. vino_drvdata->decoder.owner = VINO_NO_CHANNEL;
  2222. ret = -EINVAL;
  2223. goto out;
  2224. }
  2225. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2226. /* Don't hold spinlocks while auto-detecting norm
  2227. * as it may take a while... */
  2228. data_norm = VINO_DATA_NORM_AUTO_EXT;
  2229. ret = vino_saa7191_set_norm(&data_norm);
  2230. if ((ret == -EBUSY) || (ret == -EAGAIN)) {
  2231. data_norm = VINO_DATA_NORM_PAL;
  2232. ret = vino_saa7191_set_norm(&data_norm);
  2233. }
  2234. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2235. if (ret) {
  2236. vino_drvdata->decoder.owner = VINO_NO_CHANNEL;
  2237. ret = -EINVAL;
  2238. goto out;
  2239. }
  2240. vcs->input = input;
  2241. vcs->data_norm = data_norm;
  2242. } else {
  2243. if (input != vcs2->input) {
  2244. ret = -EBUSY;
  2245. goto out;
  2246. }
  2247. vcs->input = input;
  2248. vcs->data_norm = vcs2->data_norm;
  2249. }
  2250. if (vino_drvdata->camera.owner == vcs->channel) {
  2251. /* Transfer the ownership or release the input */
  2252. if (vcs2->input == VINO_INPUT_D1) {
  2253. vino_drvdata->camera.owner = vcs2->channel;
  2254. } else {
  2255. i2c_release_client(vino_drvdata->
  2256. camera.driver);
  2257. vino_drvdata->camera.owner = VINO_NO_CHANNEL;
  2258. }
  2259. }
  2260. break;
  2261. case VINO_INPUT_D1:
  2262. if (!vino_drvdata->camera.driver) {
  2263. ret = -EINVAL;
  2264. goto out;
  2265. }
  2266. if (vino_drvdata->camera.owner == VINO_NO_CHANNEL) {
  2267. if (i2c_use_client(vino_drvdata->camera.driver)) {
  2268. ret = -ENODEV;
  2269. goto out;
  2270. }
  2271. vino_drvdata->camera.owner = vcs->channel;
  2272. }
  2273. if (vino_drvdata->decoder.owner == vcs->channel) {
  2274. /* Transfer the ownership or release the input */
  2275. if ((vcs2->input == VINO_INPUT_COMPOSITE) ||
  2276. (vcs2->input == VINO_INPUT_SVIDEO)) {
  2277. vino_drvdata->decoder.owner = vcs2->channel;
  2278. } else {
  2279. i2c_release_client(vino_drvdata->
  2280. decoder.driver);
  2281. vino_drvdata->decoder.owner = VINO_NO_CHANNEL;
  2282. }
  2283. }
  2284. vcs->input = input;
  2285. vcs->data_norm = VINO_DATA_NORM_D1;
  2286. break;
  2287. default:
  2288. ret = -EINVAL;
  2289. goto out;
  2290. }
  2291. vino_set_default_clipping(vcs);
  2292. vino_set_default_scaling(vcs);
  2293. vino_set_default_framerate(vcs);
  2294. dprintk("vino_set_input(): %s\n", vino_inputs[vcs->input].name);
  2295. out:
  2296. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2297. return ret;
  2298. }
  2299. static void vino_release_input(struct vino_channel_settings *vcs)
  2300. {
  2301. struct vino_channel_settings *vcs2 = (vcs->channel == VINO_CHANNEL_A) ?
  2302. &vino_drvdata->b : &vino_drvdata->a;
  2303. unsigned long flags;
  2304. dprintk("vino_release_input():\n");
  2305. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2306. /* Release ownership of the channel
  2307. * and if the other channel takes input from
  2308. * the same source, transfer the ownership */
  2309. if (vino_drvdata->camera.owner == vcs->channel) {
  2310. if (vcs2->input == VINO_INPUT_D1) {
  2311. vino_drvdata->camera.owner = vcs2->channel;
  2312. } else {
  2313. i2c_release_client(vino_drvdata->camera.driver);
  2314. vino_drvdata->camera.owner = VINO_NO_CHANNEL;
  2315. }
  2316. } else if (vino_drvdata->decoder.owner == vcs->channel) {
  2317. if ((vcs2->input == VINO_INPUT_COMPOSITE) ||
  2318. (vcs2->input == VINO_INPUT_SVIDEO)) {
  2319. vino_drvdata->decoder.owner = vcs2->channel;
  2320. } else {
  2321. i2c_release_client(vino_drvdata->decoder.driver);
  2322. vino_drvdata->decoder.owner = VINO_NO_CHANNEL;
  2323. }
  2324. }
  2325. vcs->input = VINO_INPUT_NONE;
  2326. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2327. }
  2328. /* execute with input_lock locked */
  2329. static int vino_set_data_norm(struct vino_channel_settings *vcs,
  2330. unsigned int data_norm,
  2331. unsigned long *flags)
  2332. {
  2333. int err = 0;
  2334. if (data_norm == vcs->data_norm)
  2335. return 0;
  2336. switch (vcs->input) {
  2337. case VINO_INPUT_D1:
  2338. /* only one "norm" supported */
  2339. if ((data_norm != VINO_DATA_NORM_D1)
  2340. && (data_norm != VINO_DATA_NORM_AUTO)
  2341. && (data_norm != VINO_DATA_NORM_AUTO_EXT))
  2342. return -EINVAL;
  2343. break;
  2344. case VINO_INPUT_COMPOSITE:
  2345. case VINO_INPUT_SVIDEO: {
  2346. if ((data_norm != VINO_DATA_NORM_PAL)
  2347. && (data_norm != VINO_DATA_NORM_NTSC)
  2348. && (data_norm != VINO_DATA_NORM_SECAM)
  2349. && (data_norm != VINO_DATA_NORM_AUTO)
  2350. && (data_norm != VINO_DATA_NORM_AUTO_EXT))
  2351. return -EINVAL;
  2352. spin_unlock_irqrestore(&vino_drvdata->input_lock, *flags);
  2353. /* Don't hold spinlocks while setting norm
  2354. * as it may take a while... */
  2355. err = vino_saa7191_set_norm(&data_norm);
  2356. spin_lock_irqsave(&vino_drvdata->input_lock, *flags);
  2357. if (err)
  2358. goto out;
  2359. vcs->data_norm = data_norm;
  2360. vino_set_default_clipping(vcs);
  2361. vino_set_default_scaling(vcs);
  2362. vino_set_default_framerate(vcs);
  2363. break;
  2364. }
  2365. default:
  2366. return -EINVAL;
  2367. }
  2368. out:
  2369. return err;
  2370. }
  2371. /* V4L2 helper functions */
  2372. static int vino_find_data_format(__u32 pixelformat)
  2373. {
  2374. int i;
  2375. for (i = 0; i < VINO_DATA_FMT_COUNT; i++) {
  2376. if (vino_data_formats[i].pixelformat == pixelformat)
  2377. return i;
  2378. }
  2379. return VINO_DATA_FMT_NONE;
  2380. }
  2381. static int vino_enum_data_norm(struct vino_channel_settings *vcs, __u32 index)
  2382. {
  2383. int data_norm = VINO_DATA_NORM_NONE;
  2384. unsigned long flags;
  2385. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2386. switch(vcs->input) {
  2387. case VINO_INPUT_COMPOSITE:
  2388. case VINO_INPUT_SVIDEO:
  2389. if (index == 0) {
  2390. data_norm = VINO_DATA_NORM_PAL;
  2391. } else if (index == 1) {
  2392. data_norm = VINO_DATA_NORM_NTSC;
  2393. } else if (index == 2) {
  2394. data_norm = VINO_DATA_NORM_SECAM;
  2395. }
  2396. break;
  2397. case VINO_INPUT_D1:
  2398. if (index == 0) {
  2399. data_norm = VINO_DATA_NORM_D1;
  2400. }
  2401. break;
  2402. }
  2403. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2404. return data_norm;
  2405. }
  2406. static int vino_enum_input(struct vino_channel_settings *vcs, __u32 index)
  2407. {
  2408. int input = VINO_INPUT_NONE;
  2409. unsigned long flags;
  2410. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2411. if (vino_drvdata->decoder.driver && vino_drvdata->camera.driver) {
  2412. switch (index) {
  2413. case 0:
  2414. input = VINO_INPUT_COMPOSITE;
  2415. break;
  2416. case 1:
  2417. input = VINO_INPUT_SVIDEO;
  2418. break;
  2419. case 2:
  2420. input = VINO_INPUT_D1;
  2421. break;
  2422. }
  2423. } else if (vino_drvdata->decoder.driver) {
  2424. switch (index) {
  2425. case 0:
  2426. input = VINO_INPUT_COMPOSITE;
  2427. break;
  2428. case 1:
  2429. input = VINO_INPUT_SVIDEO;
  2430. break;
  2431. }
  2432. } else if (vino_drvdata->camera.driver) {
  2433. switch (index) {
  2434. case 0:
  2435. input = VINO_INPUT_D1;
  2436. break;
  2437. }
  2438. }
  2439. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2440. return input;
  2441. }
  2442. /* execute with input_lock locked */
  2443. static __u32 vino_find_input_index(struct vino_channel_settings *vcs)
  2444. {
  2445. __u32 index = 0;
  2446. // FIXME: detect when no inputs available
  2447. if (vino_drvdata->decoder.driver && vino_drvdata->camera.driver) {
  2448. switch (vcs->input) {
  2449. case VINO_INPUT_COMPOSITE:
  2450. index = 0;
  2451. break;
  2452. case VINO_INPUT_SVIDEO:
  2453. index = 1;
  2454. break;
  2455. case VINO_INPUT_D1:
  2456. index = 2;
  2457. break;
  2458. }
  2459. } else if (vino_drvdata->decoder.driver) {
  2460. switch (vcs->input) {
  2461. case VINO_INPUT_COMPOSITE:
  2462. index = 0;
  2463. break;
  2464. case VINO_INPUT_SVIDEO:
  2465. index = 1;
  2466. break;
  2467. }
  2468. } else if (vino_drvdata->camera.driver) {
  2469. switch (vcs->input) {
  2470. case VINO_INPUT_D1:
  2471. index = 0;
  2472. break;
  2473. }
  2474. }
  2475. return index;
  2476. }
  2477. /* V4L2 ioctls */
  2478. static void vino_v4l2_querycap(struct v4l2_capability *cap)
  2479. {
  2480. memset(cap, 0, sizeof(struct v4l2_capability));
  2481. strcpy(cap->driver, vino_driver_name);
  2482. strcpy(cap->card, vino_driver_description);
  2483. strcpy(cap->bus_info, vino_bus_name);
  2484. cap->version = VINO_VERSION_CODE;
  2485. cap->capabilities =
  2486. V4L2_CAP_VIDEO_CAPTURE |
  2487. V4L2_CAP_STREAMING;
  2488. // V4L2_CAP_OVERLAY, V4L2_CAP_READWRITE
  2489. }
  2490. static int vino_v4l2_enuminput(struct vino_channel_settings *vcs,
  2491. struct v4l2_input *i)
  2492. {
  2493. __u32 index = i->index;
  2494. int input;
  2495. dprintk("requested index = %d\n", index);
  2496. input = vino_enum_input(vcs, index);
  2497. if (input == VINO_INPUT_NONE)
  2498. return -EINVAL;
  2499. memset(i, 0, sizeof(struct v4l2_input));
  2500. i->index = index;
  2501. i->type = V4L2_INPUT_TYPE_CAMERA;
  2502. i->std = vino_inputs[input].std;
  2503. strcpy(i->name, vino_inputs[input].name);
  2504. if ((input == VINO_INPUT_COMPOSITE)
  2505. || (input == VINO_INPUT_SVIDEO)) {
  2506. struct saa7191_status status;
  2507. i2c_decoder_command(DECODER_SAA7191_GET_STATUS, &status);
  2508. i->status |= status.signal ? 0 : V4L2_IN_ST_NO_SIGNAL;
  2509. i->status |= status.color ? 0 : V4L2_IN_ST_NO_COLOR;
  2510. }
  2511. return 0;
  2512. }
  2513. static int vino_v4l2_g_input(struct vino_channel_settings *vcs,
  2514. unsigned int *i)
  2515. {
  2516. __u32 index;
  2517. int input;
  2518. unsigned long flags;
  2519. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2520. input = vcs->input;
  2521. index = vino_find_input_index(vcs);
  2522. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2523. dprintk("input = %d\n", input);
  2524. if (input == VINO_INPUT_NONE) {
  2525. return -EINVAL;
  2526. }
  2527. *i = index;
  2528. return 0;
  2529. }
  2530. static int vino_v4l2_s_input(struct vino_channel_settings *vcs,
  2531. unsigned int *i)
  2532. {
  2533. int input;
  2534. dprintk("requested input = %d\n", *i);
  2535. input = vino_enum_input(vcs, *i);
  2536. if (input == VINO_INPUT_NONE)
  2537. return -EINVAL;
  2538. return vino_set_input(vcs, input);
  2539. }
  2540. static int vino_v4l2_enumstd(struct vino_channel_settings *vcs,
  2541. struct v4l2_standard *s)
  2542. {
  2543. int index = s->index;
  2544. int data_norm;
  2545. data_norm = vino_enum_data_norm(vcs, index);
  2546. dprintk("standard index = %d\n", index);
  2547. if (data_norm == VINO_DATA_NORM_NONE)
  2548. return -EINVAL;
  2549. dprintk("standard name = %s\n",
  2550. vino_data_norms[data_norm].description);
  2551. memset(s, 0, sizeof(struct v4l2_standard));
  2552. s->index = index;
  2553. s->id = vino_data_norms[data_norm].std;
  2554. s->frameperiod.numerator = 1;
  2555. s->frameperiod.denominator =
  2556. vino_data_norms[data_norm].fps_max;
  2557. s->framelines =
  2558. vino_data_norms[data_norm].framelines;
  2559. strcpy(s->name,
  2560. vino_data_norms[data_norm].description);
  2561. return 0;
  2562. }
  2563. static int vino_v4l2_querystd(struct vino_channel_settings *vcs,
  2564. v4l2_std_id *std)
  2565. {
  2566. unsigned long flags;
  2567. int err = 0;
  2568. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2569. switch (vcs->input) {
  2570. case VINO_INPUT_D1:
  2571. *std = vino_inputs[vcs->input].std;
  2572. break;
  2573. case VINO_INPUT_COMPOSITE:
  2574. case VINO_INPUT_SVIDEO: {
  2575. struct saa7191_status status;
  2576. i2c_decoder_command(DECODER_SAA7191_GET_STATUS, &status);
  2577. if (status.signal) {
  2578. if (status.signal_60hz) {
  2579. *std = V4L2_STD_NTSC;
  2580. } else {
  2581. *std = V4L2_STD_PAL | V4L2_STD_SECAM;
  2582. }
  2583. } else {
  2584. *std = vino_inputs[vcs->input].std;
  2585. }
  2586. break;
  2587. }
  2588. default:
  2589. err = -EINVAL;
  2590. }
  2591. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2592. return err;
  2593. }
  2594. static int vino_v4l2_g_std(struct vino_channel_settings *vcs,
  2595. v4l2_std_id *std)
  2596. {
  2597. unsigned long flags;
  2598. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2599. *std = vino_data_norms[vcs->data_norm].std;
  2600. dprintk("current standard = %d\n", vcs->data_norm);
  2601. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2602. return 0;
  2603. }
  2604. static int vino_v4l2_s_std(struct vino_channel_settings *vcs,
  2605. v4l2_std_id *std)
  2606. {
  2607. unsigned long flags;
  2608. int ret = 0;
  2609. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2610. if (!vino_is_input_owner(vcs)) {
  2611. ret = -EBUSY;
  2612. goto out;
  2613. }
  2614. /* check if the standard is valid for the current input */
  2615. if ((*std) & vino_inputs[vcs->input].std) {
  2616. dprintk("standard accepted\n");
  2617. /* change the video norm for SAA7191
  2618. * and accept NTSC for D1 (do nothing) */
  2619. if (vcs->input == VINO_INPUT_D1)
  2620. goto out;
  2621. if (((*std) & V4L2_STD_PAL)
  2622. && ((*std) & V4L2_STD_NTSC)
  2623. && ((*std) & V4L2_STD_SECAM)) {
  2624. ret = vino_set_data_norm(vcs, VINO_DATA_NORM_AUTO_EXT,
  2625. &flags);
  2626. } else if ((*std) & V4L2_STD_PAL) {
  2627. ret = vino_set_data_norm(vcs, VINO_DATA_NORM_PAL,
  2628. &flags);
  2629. } else if ((*std) & V4L2_STD_NTSC) {
  2630. ret = vino_set_data_norm(vcs, VINO_DATA_NORM_NTSC,
  2631. &flags);
  2632. } else if ((*std) & V4L2_STD_SECAM) {
  2633. ret = vino_set_data_norm(vcs, VINO_DATA_NORM_SECAM,
  2634. &flags);
  2635. } else {
  2636. ret = -EINVAL;
  2637. }
  2638. if (ret) {
  2639. ret = -EINVAL;
  2640. }
  2641. } else {
  2642. ret = -EINVAL;
  2643. }
  2644. out:
  2645. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2646. return ret;
  2647. }
  2648. static int vino_v4l2_enum_fmt(struct vino_channel_settings *vcs,
  2649. struct v4l2_fmtdesc *fd)
  2650. {
  2651. enum v4l2_buf_type type = fd->type;
  2652. int index = fd->index;
  2653. dprintk("format index = %d\n", index);
  2654. switch (fd->type) {
  2655. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  2656. if ((fd->index < 0) ||
  2657. (fd->index >= VINO_DATA_FMT_COUNT))
  2658. return -EINVAL;
  2659. dprintk("format name = %s\n",
  2660. vino_data_formats[index].description);
  2661. memset(fd, 0, sizeof(struct v4l2_fmtdesc));
  2662. fd->index = index;
  2663. fd->type = type;
  2664. fd->pixelformat = vino_data_formats[index].pixelformat;
  2665. strcpy(fd->description, vino_data_formats[index].description);
  2666. break;
  2667. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2668. default:
  2669. return -EINVAL;
  2670. }
  2671. return 0;
  2672. }
  2673. static int vino_v4l2_try_fmt(struct vino_channel_settings *vcs,
  2674. struct v4l2_format *f)
  2675. {
  2676. struct vino_channel_settings tempvcs;
  2677. unsigned long flags;
  2678. switch (f->type) {
  2679. case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
  2680. struct v4l2_pix_format *pf = &f->fmt.pix;
  2681. dprintk("requested: w = %d, h = %d\n",
  2682. pf->width, pf->height);
  2683. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2684. memcpy(&tempvcs, vcs, sizeof(struct vino_channel_settings));
  2685. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2686. tempvcs.data_format = vino_find_data_format(pf->pixelformat);
  2687. if (tempvcs.data_format == VINO_DATA_FMT_NONE) {
  2688. tempvcs.data_format = VINO_DATA_FMT_GREY;
  2689. pf->pixelformat =
  2690. vino_data_formats[tempvcs.data_format].
  2691. pixelformat;
  2692. }
  2693. /* data format must be set before clipping/scaling */
  2694. vino_set_scaling(&tempvcs, pf->width, pf->height);
  2695. dprintk("data format = %s\n",
  2696. vino_data_formats[tempvcs.data_format].description);
  2697. pf->width = (tempvcs.clipping.right - tempvcs.clipping.left) /
  2698. tempvcs.decimation;
  2699. pf->height = (tempvcs.clipping.bottom - tempvcs.clipping.top) /
  2700. tempvcs.decimation;
  2701. pf->field = V4L2_FIELD_INTERLACED;
  2702. pf->bytesperline = tempvcs.line_size;
  2703. pf->sizeimage = tempvcs.line_size *
  2704. (tempvcs.clipping.bottom - tempvcs.clipping.top) /
  2705. tempvcs.decimation;
  2706. pf->colorspace =
  2707. vino_data_formats[tempvcs.data_format].colorspace;
  2708. pf->priv = 0;
  2709. break;
  2710. }
  2711. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2712. default:
  2713. return -EINVAL;
  2714. }
  2715. return 0;
  2716. }
  2717. static int vino_v4l2_g_fmt(struct vino_channel_settings *vcs,
  2718. struct v4l2_format *f)
  2719. {
  2720. unsigned long flags;
  2721. switch (f->type) {
  2722. case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
  2723. struct v4l2_pix_format *pf = &f->fmt.pix;
  2724. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2725. pf->width = (vcs->clipping.right - vcs->clipping.left) /
  2726. vcs->decimation;
  2727. pf->height = (vcs->clipping.bottom - vcs->clipping.top) /
  2728. vcs->decimation;
  2729. pf->pixelformat =
  2730. vino_data_formats[vcs->data_format].pixelformat;
  2731. pf->field = V4L2_FIELD_INTERLACED;
  2732. pf->bytesperline = vcs->line_size;
  2733. pf->sizeimage = vcs->line_size *
  2734. (vcs->clipping.bottom - vcs->clipping.top) /
  2735. vcs->decimation;
  2736. pf->colorspace =
  2737. vino_data_formats[vcs->data_format].colorspace;
  2738. pf->priv = 0;
  2739. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2740. break;
  2741. }
  2742. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2743. default:
  2744. return -EINVAL;
  2745. }
  2746. return 0;
  2747. }
  2748. static int vino_v4l2_s_fmt(struct vino_channel_settings *vcs,
  2749. struct v4l2_format *f)
  2750. {
  2751. int data_format;
  2752. unsigned long flags;
  2753. switch (f->type) {
  2754. case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
  2755. struct v4l2_pix_format *pf = &f->fmt.pix;
  2756. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2757. data_format = vino_find_data_format(pf->pixelformat);
  2758. if (data_format == VINO_DATA_FMT_NONE) {
  2759. vcs->data_format = VINO_DATA_FMT_GREY;
  2760. pf->pixelformat =
  2761. vino_data_formats[vcs->data_format].
  2762. pixelformat;
  2763. } else {
  2764. vcs->data_format = data_format;
  2765. }
  2766. /* data format must be set before clipping/scaling */
  2767. vino_set_scaling(vcs, pf->width, pf->height);
  2768. dprintk("data format = %s\n",
  2769. vino_data_formats[vcs->data_format].description);
  2770. pf->width = vcs->clipping.right - vcs->clipping.left;
  2771. pf->height = vcs->clipping.bottom - vcs->clipping.top;
  2772. pf->field = V4L2_FIELD_INTERLACED;
  2773. pf->bytesperline = vcs->line_size;
  2774. pf->sizeimage = vcs->line_size *
  2775. (vcs->clipping.bottom - vcs->clipping.top) /
  2776. vcs->decimation;
  2777. pf->colorspace =
  2778. vino_data_formats[vcs->data_format].colorspace;
  2779. pf->priv = 0;
  2780. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2781. break;
  2782. }
  2783. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2784. default:
  2785. return -EINVAL;
  2786. }
  2787. return 0;
  2788. }
  2789. static int vino_v4l2_cropcap(struct vino_channel_settings *vcs,
  2790. struct v4l2_cropcap *ccap)
  2791. {
  2792. const struct vino_data_norm *norm;
  2793. unsigned long flags;
  2794. switch (ccap->type) {
  2795. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  2796. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2797. norm = &vino_data_norms[vcs->data_norm];
  2798. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2799. ccap->bounds.left = 0;
  2800. ccap->bounds.top = 0;
  2801. ccap->bounds.width = norm->width;
  2802. ccap->bounds.height = norm->height;
  2803. memcpy(&ccap->defrect, &ccap->bounds,
  2804. sizeof(struct v4l2_rect));
  2805. ccap->pixelaspect.numerator = 1;
  2806. ccap->pixelaspect.denominator = 1;
  2807. break;
  2808. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2809. default:
  2810. return -EINVAL;
  2811. }
  2812. return 0;
  2813. }
  2814. static int vino_v4l2_g_crop(struct vino_channel_settings *vcs,
  2815. struct v4l2_crop *c)
  2816. {
  2817. unsigned long flags;
  2818. switch (c->type) {
  2819. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  2820. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2821. c->c.left = vcs->clipping.left;
  2822. c->c.top = vcs->clipping.top;
  2823. c->c.width = vcs->clipping.right - vcs->clipping.left;
  2824. c->c.height = vcs->clipping.bottom - vcs->clipping.top;
  2825. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2826. break;
  2827. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2828. default:
  2829. return -EINVAL;
  2830. }
  2831. return 0;
  2832. }
  2833. static int vino_v4l2_s_crop(struct vino_channel_settings *vcs,
  2834. struct v4l2_crop *c)
  2835. {
  2836. unsigned long flags;
  2837. switch (c->type) {
  2838. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  2839. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2840. vino_set_clipping(vcs, c->c.left, c->c.top,
  2841. c->c.width, c->c.height);
  2842. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2843. break;
  2844. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2845. default:
  2846. return -EINVAL;
  2847. }
  2848. return 0;
  2849. }
  2850. static int vino_v4l2_g_parm(struct vino_channel_settings *vcs,
  2851. struct v4l2_streamparm *sp)
  2852. {
  2853. unsigned long flags;
  2854. switch (sp->type) {
  2855. case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
  2856. struct v4l2_captureparm *cp = &sp->parm.capture;
  2857. memset(cp, 0, sizeof(struct v4l2_captureparm));
  2858. cp->capability = V4L2_CAP_TIMEPERFRAME;
  2859. cp->timeperframe.numerator = 1;
  2860. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2861. cp->timeperframe.denominator = vcs->fps;
  2862. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2863. // TODO: cp->readbuffers = xxx;
  2864. break;
  2865. }
  2866. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2867. default:
  2868. return -EINVAL;
  2869. }
  2870. return 0;
  2871. }
  2872. static int vino_v4l2_s_parm(struct vino_channel_settings *vcs,
  2873. struct v4l2_streamparm *sp)
  2874. {
  2875. unsigned long flags;
  2876. switch (sp->type) {
  2877. case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
  2878. struct v4l2_captureparm *cp = &sp->parm.capture;
  2879. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2880. if ((cp->timeperframe.numerator == 0) ||
  2881. (cp->timeperframe.denominator == 0)) {
  2882. /* reset framerate */
  2883. vino_set_default_framerate(vcs);
  2884. } else {
  2885. vino_set_framerate(vcs, cp->timeperframe.denominator /
  2886. cp->timeperframe.numerator);
  2887. }
  2888. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2889. // TODO: set buffers according to cp->readbuffers
  2890. break;
  2891. }
  2892. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2893. default:
  2894. return -EINVAL;
  2895. }
  2896. return 0;
  2897. }
  2898. static int vino_v4l2_reqbufs(struct vino_channel_settings *vcs,
  2899. struct v4l2_requestbuffers *rb)
  2900. {
  2901. if (vcs->reading)
  2902. return -EBUSY;
  2903. switch (rb->type) {
  2904. case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
  2905. // TODO: check queue type
  2906. if (rb->memory != V4L2_MEMORY_MMAP) {
  2907. dprintk("type not mmap\n");
  2908. return -EINVAL;
  2909. }
  2910. dprintk("count = %d\n", rb->count);
  2911. if (rb->count > 0) {
  2912. if (vino_is_capturing(vcs)) {
  2913. dprintk("busy, capturing\n");
  2914. return -EBUSY;
  2915. }
  2916. if (vino_queue_has_mapped_buffers(&vcs->fb_queue)) {
  2917. dprintk("busy, buffers still mapped\n");
  2918. return -EBUSY;
  2919. } else {
  2920. vcs->streaming = 0;
  2921. vino_queue_free(&vcs->fb_queue);
  2922. vino_queue_init(&vcs->fb_queue, &rb->count);
  2923. }
  2924. } else {
  2925. vcs->streaming = 0;
  2926. vino_capture_stop(vcs);
  2927. vino_queue_free(&vcs->fb_queue);
  2928. }
  2929. break;
  2930. }
  2931. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2932. default:
  2933. return -EINVAL;
  2934. }
  2935. return 0;
  2936. }
  2937. static void vino_v4l2_get_buffer_status(struct vino_channel_settings *vcs,
  2938. struct vino_framebuffer *fb,
  2939. struct v4l2_buffer *b)
  2940. {
  2941. if (vino_queue_outgoing_contains(&vcs->fb_queue,
  2942. fb->id)) {
  2943. b->flags &= ~V4L2_BUF_FLAG_QUEUED;
  2944. b->flags |= V4L2_BUF_FLAG_DONE;
  2945. } else if (vino_queue_incoming_contains(&vcs->fb_queue,
  2946. fb->id)) {
  2947. b->flags &= ~V4L2_BUF_FLAG_DONE;
  2948. b->flags |= V4L2_BUF_FLAG_QUEUED;
  2949. } else {
  2950. b->flags &= ~(V4L2_BUF_FLAG_DONE |
  2951. V4L2_BUF_FLAG_QUEUED);
  2952. }
  2953. b->flags &= ~(V4L2_BUF_FLAG_TIMECODE);
  2954. if (fb->map_count > 0)
  2955. b->flags |= V4L2_BUF_FLAG_MAPPED;
  2956. b->index = fb->id;
  2957. b->memory = (vcs->fb_queue.type == VINO_MEMORY_MMAP) ?
  2958. V4L2_MEMORY_MMAP : V4L2_MEMORY_USERPTR;
  2959. b->m.offset = fb->offset;
  2960. b->bytesused = fb->data_size;
  2961. b->length = fb->size;
  2962. b->field = V4L2_FIELD_INTERLACED;
  2963. b->sequence = fb->frame_counter;
  2964. memcpy(&b->timestamp, &fb->timestamp,
  2965. sizeof(struct timeval));
  2966. // b->input ?
  2967. dprintk("buffer %d: length = %d, bytesused = %d, offset = %d\n",
  2968. fb->id, fb->size, fb->data_size, fb->offset);
  2969. }
  2970. static int vino_v4l2_querybuf(struct vino_channel_settings *vcs,
  2971. struct v4l2_buffer *b)
  2972. {
  2973. if (vcs->reading)
  2974. return -EBUSY;
  2975. switch (b->type) {
  2976. case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
  2977. struct vino_framebuffer *fb;
  2978. // TODO: check queue type
  2979. if (b->index >= vino_queue_get_length(&vcs->fb_queue)) {
  2980. dprintk("invalid index = %d\n",
  2981. b->index);
  2982. return -EINVAL;
  2983. }
  2984. fb = vino_queue_get_buffer(&vcs->fb_queue,
  2985. b->index);
  2986. if (fb == NULL) {
  2987. dprintk("vino_queue_get_buffer() failed");
  2988. return -EINVAL;
  2989. }
  2990. vino_v4l2_get_buffer_status(vcs, fb, b);
  2991. break;
  2992. }
  2993. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2994. default:
  2995. return -EINVAL;
  2996. }
  2997. return 0;
  2998. }
  2999. static int vino_v4l2_qbuf(struct vino_channel_settings *vcs,
  3000. struct v4l2_buffer *b)
  3001. {
  3002. if (vcs->reading)
  3003. return -EBUSY;
  3004. switch (b->type) {
  3005. case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
  3006. struct vino_framebuffer *fb;
  3007. int ret;
  3008. // TODO: check queue type
  3009. if (b->memory != V4L2_MEMORY_MMAP) {
  3010. dprintk("type not mmap\n");
  3011. return -EINVAL;
  3012. }
  3013. fb = vino_capture_enqueue(vcs, b->index);
  3014. if (fb == NULL)
  3015. return -EINVAL;
  3016. vino_v4l2_get_buffer_status(vcs, fb, b);
  3017. if (vcs->streaming) {
  3018. ret = vino_capture_next(vcs, 1);
  3019. if (ret)
  3020. return ret;
  3021. }
  3022. break;
  3023. }
  3024. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  3025. default:
  3026. return -EINVAL;
  3027. }
  3028. return 0;
  3029. }
  3030. static int vino_v4l2_dqbuf(struct vino_channel_settings *vcs,
  3031. struct v4l2_buffer *b,
  3032. unsigned int nonblocking)
  3033. {
  3034. if (vcs->reading)
  3035. return -EBUSY;
  3036. switch (b->type) {
  3037. case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
  3038. struct vino_framebuffer *fb;
  3039. unsigned int incoming, outgoing;
  3040. int err;
  3041. // TODO: check queue type
  3042. err = vino_queue_get_incoming(&vcs->fb_queue, &incoming);
  3043. if (err) {
  3044. dprintk("vino_queue_get_incoming() failed\n");
  3045. return -EINVAL;
  3046. }
  3047. err = vino_queue_get_outgoing(&vcs->fb_queue, &outgoing);
  3048. if (err) {
  3049. dprintk("vino_queue_get_outgoing() failed\n");
  3050. return -EINVAL;
  3051. }
  3052. dprintk("incoming = %d, outgoing = %d\n", incoming, outgoing);
  3053. if (outgoing == 0) {
  3054. if (incoming == 0) {
  3055. dprintk("no incoming or outgoing buffers\n");
  3056. return -EINVAL;
  3057. }
  3058. if (nonblocking) {
  3059. dprintk("non-blocking I/O was selected and "
  3060. "there are no buffers to dequeue\n");
  3061. return -EAGAIN;
  3062. }
  3063. err = vino_wait_for_frame(vcs);
  3064. if (err) {
  3065. err = vino_wait_for_frame(vcs);
  3066. if (err) {
  3067. /* interrupted or
  3068. * no frames captured because
  3069. * of frame skipping */
  3070. // vino_capture_failed(vcs);
  3071. return -EIO;
  3072. }
  3073. }
  3074. }
  3075. fb = vino_queue_remove(&vcs->fb_queue, &b->index);
  3076. if (fb == NULL) {
  3077. dprintk("vino_queue_remove() failed\n");
  3078. return -EINVAL;
  3079. }
  3080. err = vino_check_buffer(vcs, fb);
  3081. vino_v4l2_get_buffer_status(vcs, fb, b);
  3082. if (err)
  3083. return -EIO;
  3084. break;
  3085. }
  3086. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  3087. default:
  3088. return -EINVAL;
  3089. }
  3090. return 0;
  3091. }
  3092. static int vino_v4l2_streamon(struct vino_channel_settings *vcs)
  3093. {
  3094. unsigned int incoming;
  3095. int ret;
  3096. if (vcs->reading)
  3097. return -EBUSY;
  3098. if (vcs->streaming)
  3099. return 0;
  3100. // TODO: check queue type
  3101. if (vino_queue_get_length(&vcs->fb_queue) < 1) {
  3102. dprintk("no buffers allocated\n");
  3103. return -EINVAL;
  3104. }
  3105. ret = vino_queue_get_incoming(&vcs->fb_queue, &incoming);
  3106. if (ret) {
  3107. dprintk("vino_queue_get_incoming() failed\n");
  3108. return -EINVAL;
  3109. }
  3110. vcs->streaming = 1;
  3111. if (incoming > 0) {
  3112. ret = vino_capture_next(vcs, 1);
  3113. if (ret) {
  3114. vcs->streaming = 0;
  3115. dprintk("couldn't start capture\n");
  3116. return -EINVAL;
  3117. }
  3118. }
  3119. return 0;
  3120. }
  3121. static int vino_v4l2_streamoff(struct vino_channel_settings *vcs)
  3122. {
  3123. if (vcs->reading)
  3124. return -EBUSY;
  3125. if (!vcs->streaming)
  3126. return 0;
  3127. vcs->streaming = 0;
  3128. vino_capture_stop(vcs);
  3129. return 0;
  3130. }
  3131. static int vino_v4l2_queryctrl(struct vino_channel_settings *vcs,
  3132. struct v4l2_queryctrl *queryctrl)
  3133. {
  3134. unsigned long flags;
  3135. int i;
  3136. int err = 0;
  3137. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  3138. switch (vcs->input) {
  3139. case VINO_INPUT_D1:
  3140. for (i = 0; i < VINO_INDYCAM_V4L2_CONTROL_COUNT; i++) {
  3141. if (vino_indycam_v4l2_controls[i].id ==
  3142. queryctrl->id) {
  3143. memcpy(queryctrl,
  3144. &vino_indycam_v4l2_controls[i],
  3145. sizeof(struct v4l2_queryctrl));
  3146. queryctrl->reserved[0] = 0;
  3147. goto found;
  3148. }
  3149. }
  3150. err = -EINVAL;
  3151. break;
  3152. case VINO_INPUT_COMPOSITE:
  3153. case VINO_INPUT_SVIDEO:
  3154. for (i = 0; i < VINO_SAA7191_V4L2_CONTROL_COUNT; i++) {
  3155. if (vino_saa7191_v4l2_controls[i].id ==
  3156. queryctrl->id) {
  3157. memcpy(queryctrl,
  3158. &vino_saa7191_v4l2_controls[i],
  3159. sizeof(struct v4l2_queryctrl));
  3160. queryctrl->reserved[0] = 0;
  3161. goto found;
  3162. }
  3163. }
  3164. err = -EINVAL;
  3165. break;
  3166. default:
  3167. err = -EINVAL;
  3168. }
  3169. found:
  3170. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  3171. return err;
  3172. }
  3173. static int vino_v4l2_g_ctrl(struct vino_channel_settings *vcs,
  3174. struct v4l2_control *control)
  3175. {
  3176. unsigned long flags;
  3177. int i;
  3178. int err = 0;
  3179. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  3180. switch (vcs->input) {
  3181. case VINO_INPUT_D1: {
  3182. struct indycam_control indycam_ctrl;
  3183. for (i = 0; i < VINO_INDYCAM_V4L2_CONTROL_COUNT; i++) {
  3184. if (vino_indycam_v4l2_controls[i].id ==
  3185. control->id) {
  3186. goto found1;
  3187. }
  3188. }
  3189. err = -EINVAL;
  3190. goto out;
  3191. found1:
  3192. indycam_ctrl.type = vino_indycam_v4l2_controls[i].reserved[0];
  3193. err = i2c_camera_command(DECODER_INDYCAM_GET_CONTROL,
  3194. &indycam_ctrl);
  3195. if (err) {
  3196. err = -EINVAL;
  3197. goto out;
  3198. }
  3199. control->value = indycam_ctrl.value;
  3200. break;
  3201. }
  3202. case VINO_INPUT_COMPOSITE:
  3203. case VINO_INPUT_SVIDEO: {
  3204. struct saa7191_control saa7191_ctrl;
  3205. for (i = 0; i < VINO_SAA7191_V4L2_CONTROL_COUNT; i++) {
  3206. if (vino_saa7191_v4l2_controls[i].id ==
  3207. control->id) {
  3208. goto found2;
  3209. }
  3210. }
  3211. err = -EINVAL;
  3212. goto out;
  3213. found2:
  3214. saa7191_ctrl.type = vino_saa7191_v4l2_controls[i].reserved[0];
  3215. err = i2c_decoder_command(DECODER_SAA7191_GET_CONTROL,
  3216. &saa7191_ctrl);
  3217. if (err) {
  3218. err = -EINVAL;
  3219. goto out;
  3220. }
  3221. control->value = saa7191_ctrl.value;
  3222. break;
  3223. }
  3224. default:
  3225. err = -EINVAL;
  3226. }
  3227. out:
  3228. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  3229. return err;
  3230. }
  3231. static int vino_v4l2_s_ctrl(struct vino_channel_settings *vcs,
  3232. struct v4l2_control *control)
  3233. {
  3234. unsigned long flags;
  3235. int i;
  3236. int err = 0;
  3237. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  3238. if (!vino_is_input_owner(vcs)) {
  3239. err = -EBUSY;
  3240. goto out;
  3241. }
  3242. switch (vcs->input) {
  3243. case VINO_INPUT_D1: {
  3244. struct indycam_control indycam_ctrl;
  3245. for (i = 0; i < VINO_INDYCAM_V4L2_CONTROL_COUNT; i++) {
  3246. if (vino_indycam_v4l2_controls[i].id ==
  3247. control->id) {
  3248. if ((control->value >=
  3249. vino_indycam_v4l2_controls[i].minimum)
  3250. && (control->value <=
  3251. vino_indycam_v4l2_controls[i].
  3252. maximum)) {
  3253. goto found1;
  3254. } else {
  3255. err = -ERANGE;
  3256. goto out;
  3257. }
  3258. }
  3259. }
  3260. err = -EINVAL;
  3261. goto out;
  3262. found1:
  3263. indycam_ctrl.type = vino_indycam_v4l2_controls[i].reserved[0];
  3264. indycam_ctrl.value = control->value;
  3265. err = i2c_camera_command(DECODER_INDYCAM_SET_CONTROL,
  3266. &indycam_ctrl);
  3267. if (err)
  3268. err = -EINVAL;
  3269. break;
  3270. }
  3271. case VINO_INPUT_COMPOSITE:
  3272. case VINO_INPUT_SVIDEO: {
  3273. struct saa7191_control saa7191_ctrl;
  3274. for (i = 0; i < VINO_SAA7191_V4L2_CONTROL_COUNT; i++) {
  3275. if (vino_saa7191_v4l2_controls[i].id ==
  3276. control->id) {
  3277. if ((control->value >=
  3278. vino_saa7191_v4l2_controls[i].minimum)
  3279. && (control->value <=
  3280. vino_saa7191_v4l2_controls[i].
  3281. maximum)) {
  3282. goto found2;
  3283. } else {
  3284. err = -ERANGE;
  3285. goto out;
  3286. }
  3287. }
  3288. }
  3289. err = -EINVAL;
  3290. goto out;
  3291. found2:
  3292. saa7191_ctrl.type = vino_saa7191_v4l2_controls[i].reserved[0];
  3293. saa7191_ctrl.value = control->value;
  3294. err = i2c_decoder_command(DECODER_SAA7191_SET_CONTROL,
  3295. &saa7191_ctrl);
  3296. if (err)
  3297. err = -EINVAL;
  3298. break;
  3299. }
  3300. default:
  3301. err = -EINVAL;
  3302. }
  3303. out:
  3304. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  3305. return err;
  3306. }
  3307. /* File operations */
  3308. static int vino_open(struct inode *inode, struct file *file)
  3309. {
  3310. struct video_device *dev = video_devdata(file);
  3311. struct vino_channel_settings *vcs = video_get_drvdata(dev);
  3312. int ret = 0;
  3313. dprintk("open(): channel = %c\n",
  3314. (vcs->channel == VINO_CHANNEL_A) ? 'A' : 'B');
  3315. mutex_lock(&vcs->mutex);
  3316. if (vcs->users) {
  3317. dprintk("open(): driver busy\n");
  3318. ret = -EBUSY;
  3319. goto out;
  3320. }
  3321. ret = vino_acquire_input(vcs);
  3322. if (ret) {
  3323. dprintk("open(): vino_acquire_input() failed\n");
  3324. goto out;
  3325. }
  3326. vcs->users++;
  3327. out:
  3328. mutex_unlock(&vcs->mutex);
  3329. dprintk("open(): %s!\n", ret ? "failed" : "complete");
  3330. return ret;
  3331. }
  3332. static int vino_close(struct inode *inode, struct file *file)
  3333. {
  3334. struct video_device *dev = video_devdata(file);
  3335. struct vino_channel_settings *vcs = video_get_drvdata(dev);
  3336. dprintk("close():\n");
  3337. mutex_lock(&vcs->mutex);
  3338. vcs->users--;
  3339. if (!vcs->users) {
  3340. vino_release_input(vcs);
  3341. /* stop DMA and free buffers */
  3342. vino_capture_stop(vcs);
  3343. vino_queue_free(&vcs->fb_queue);
  3344. }
  3345. mutex_unlock(&vcs->mutex);
  3346. return 0;
  3347. }
  3348. static void vino_vm_open(struct vm_area_struct *vma)
  3349. {
  3350. struct vino_framebuffer *fb = vma->vm_private_data;
  3351. fb->map_count++;
  3352. dprintk("vino_vm_open(): count = %d\n", fb->map_count);
  3353. }
  3354. static void vino_vm_close(struct vm_area_struct *vma)
  3355. {
  3356. struct vino_framebuffer *fb = vma->vm_private_data;
  3357. fb->map_count--;
  3358. dprintk("vino_vm_close(): count = %d\n", fb->map_count);
  3359. }
  3360. static struct vm_operations_struct vino_vm_ops = {
  3361. .open = vino_vm_open,
  3362. .close = vino_vm_close,
  3363. };
  3364. static int vino_mmap(struct file *file, struct vm_area_struct *vma)
  3365. {
  3366. struct video_device *dev = video_devdata(file);
  3367. struct vino_channel_settings *vcs = video_get_drvdata(dev);
  3368. unsigned long start = vma->vm_start;
  3369. unsigned long size = vma->vm_end - vma->vm_start;
  3370. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  3371. struct vino_framebuffer *fb = NULL;
  3372. unsigned int i, length;
  3373. int ret = 0;
  3374. dprintk("mmap():\n");
  3375. // TODO: reject mmap if already mapped
  3376. if (mutex_lock_interruptible(&vcs->mutex))
  3377. return -EINTR;
  3378. if (vcs->reading) {
  3379. ret = -EBUSY;
  3380. goto out;
  3381. }
  3382. // TODO: check queue type
  3383. if (!(vma->vm_flags & VM_WRITE)) {
  3384. dprintk("mmap(): app bug: PROT_WRITE please\n");
  3385. ret = -EINVAL;
  3386. goto out;
  3387. }
  3388. if (!(vma->vm_flags & VM_SHARED)) {
  3389. dprintk("mmap(): app bug: MAP_SHARED please\n");
  3390. ret = -EINVAL;
  3391. goto out;
  3392. }
  3393. /* find the correct buffer using offset */
  3394. length = vino_queue_get_length(&vcs->fb_queue);
  3395. if (length == 0) {
  3396. dprintk("mmap(): queue not initialized\n");
  3397. ret = -EINVAL;
  3398. goto out;
  3399. }
  3400. for (i = 0; i < length; i++) {
  3401. fb = vino_queue_get_buffer(&vcs->fb_queue, i);
  3402. if (fb == NULL) {
  3403. dprintk("mmap(): vino_queue_get_buffer() failed\n");
  3404. ret = -EINVAL;
  3405. goto out;
  3406. }
  3407. if (fb->offset == offset)
  3408. goto found;
  3409. }
  3410. dprintk("mmap(): invalid offset = %lu\n", offset);
  3411. ret = -EINVAL;
  3412. goto out;
  3413. found:
  3414. dprintk("mmap(): buffer = %d\n", i);
  3415. if (size > (fb->desc_table.page_count * PAGE_SIZE)) {
  3416. dprintk("mmap(): failed: size = %lu > %lu\n",
  3417. size, fb->desc_table.page_count * PAGE_SIZE);
  3418. ret = -EINVAL;
  3419. goto out;
  3420. }
  3421. for (i = 0; i < fb->desc_table.page_count; i++) {
  3422. unsigned long pfn =
  3423. virt_to_phys((void *)fb->desc_table.virtual[i]) >>
  3424. PAGE_SHIFT;
  3425. if (size < PAGE_SIZE)
  3426. break;
  3427. // protection was: PAGE_READONLY
  3428. if (remap_pfn_range(vma, start, pfn, PAGE_SIZE,
  3429. vma->vm_page_prot)) {
  3430. dprintk("mmap(): remap_pfn_range() failed\n");
  3431. ret = -EAGAIN;
  3432. goto out;
  3433. }
  3434. start += PAGE_SIZE;
  3435. size -= PAGE_SIZE;
  3436. }
  3437. fb->map_count = 1;
  3438. vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED;
  3439. vma->vm_flags &= ~VM_IO;
  3440. vma->vm_private_data = fb;
  3441. vma->vm_file = file;
  3442. vma->vm_ops = &vino_vm_ops;
  3443. out:
  3444. mutex_unlock(&vcs->mutex);
  3445. return ret;
  3446. }
  3447. static unsigned int vino_poll(struct file *file, poll_table *pt)
  3448. {
  3449. struct video_device *dev = video_devdata(file);
  3450. struct vino_channel_settings *vcs = video_get_drvdata(dev);
  3451. unsigned int outgoing;
  3452. unsigned int ret = 0;
  3453. // lock mutex (?)
  3454. // TODO: this has to be corrected for different read modes
  3455. dprintk("poll():\n");
  3456. if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
  3457. dprintk("poll(): vino_queue_get_outgoing() failed\n");
  3458. ret = POLLERR;
  3459. goto error;
  3460. }
  3461. if (outgoing > 0)
  3462. goto over;
  3463. poll_wait(file, &vcs->fb_queue.frame_wait_queue, pt);
  3464. if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
  3465. dprintk("poll(): vino_queue_get_outgoing() failed\n");
  3466. ret = POLLERR;
  3467. goto error;
  3468. }
  3469. over:
  3470. dprintk("poll(): data %savailable\n",
  3471. (outgoing > 0) ? "" : "not ");
  3472. if (outgoing > 0)
  3473. ret = POLLIN | POLLRDNORM;
  3474. error:
  3475. return ret;
  3476. }
  3477. static int vino_do_ioctl(struct inode *inode, struct file *file,
  3478. unsigned int cmd, void *arg)
  3479. {
  3480. struct video_device *dev = video_devdata(file);
  3481. struct vino_channel_settings *vcs = video_get_drvdata(dev);
  3482. #ifdef VINO_DEBUG
  3483. switch (_IOC_TYPE(cmd)) {
  3484. case 'v':
  3485. dprintk("ioctl(): V4L1 unsupported (0x%08x)\n", cmd);
  3486. break;
  3487. case 'V':
  3488. dprintk("ioctl(): V4L2 %s (0x%08x)\n",
  3489. v4l2_ioctl_names[_IOC_NR(cmd)], cmd);
  3490. break;
  3491. default:
  3492. dprintk("ioctl(): unsupported command 0x%08x\n", cmd);
  3493. }
  3494. #endif
  3495. switch (cmd) {
  3496. /* V4L2 interface */
  3497. case VIDIOC_QUERYCAP: {
  3498. vino_v4l2_querycap(arg);
  3499. break;
  3500. }
  3501. case VIDIOC_ENUMINPUT: {
  3502. return vino_v4l2_enuminput(vcs, arg);
  3503. }
  3504. case VIDIOC_G_INPUT: {
  3505. return vino_v4l2_g_input(vcs, arg);
  3506. }
  3507. case VIDIOC_S_INPUT: {
  3508. return vino_v4l2_s_input(vcs, arg);
  3509. }
  3510. case VIDIOC_ENUMSTD: {
  3511. return vino_v4l2_enumstd(vcs, arg);
  3512. }
  3513. case VIDIOC_QUERYSTD: {
  3514. return vino_v4l2_querystd(vcs, arg);
  3515. }
  3516. case VIDIOC_G_STD: {
  3517. return vino_v4l2_g_std(vcs, arg);
  3518. }
  3519. case VIDIOC_S_STD: {
  3520. return vino_v4l2_s_std(vcs, arg);
  3521. }
  3522. case VIDIOC_ENUM_FMT: {
  3523. return vino_v4l2_enum_fmt(vcs, arg);
  3524. }
  3525. case VIDIOC_TRY_FMT: {
  3526. return vino_v4l2_try_fmt(vcs, arg);
  3527. }
  3528. case VIDIOC_G_FMT: {
  3529. return vino_v4l2_g_fmt(vcs, arg);
  3530. }
  3531. case VIDIOC_S_FMT: {
  3532. return vino_v4l2_s_fmt(vcs, arg);
  3533. }
  3534. case VIDIOC_CROPCAP: {
  3535. return vino_v4l2_cropcap(vcs, arg);
  3536. }
  3537. case VIDIOC_G_CROP: {
  3538. return vino_v4l2_g_crop(vcs, arg);
  3539. }
  3540. case VIDIOC_S_CROP: {
  3541. return vino_v4l2_s_crop(vcs, arg);
  3542. }
  3543. case VIDIOC_G_PARM: {
  3544. return vino_v4l2_g_parm(vcs, arg);
  3545. }
  3546. case VIDIOC_S_PARM: {
  3547. return vino_v4l2_s_parm(vcs, arg);
  3548. }
  3549. case VIDIOC_REQBUFS: {
  3550. return vino_v4l2_reqbufs(vcs, arg);
  3551. }
  3552. case VIDIOC_QUERYBUF: {
  3553. return vino_v4l2_querybuf(vcs, arg);
  3554. }
  3555. case VIDIOC_QBUF: {
  3556. return vino_v4l2_qbuf(vcs, arg);
  3557. }
  3558. case VIDIOC_DQBUF: {
  3559. return vino_v4l2_dqbuf(vcs, arg, file->f_flags & O_NONBLOCK);
  3560. }
  3561. case VIDIOC_STREAMON: {
  3562. return vino_v4l2_streamon(vcs);
  3563. }
  3564. case VIDIOC_STREAMOFF: {
  3565. return vino_v4l2_streamoff(vcs);
  3566. }
  3567. case VIDIOC_QUERYCTRL: {
  3568. return vino_v4l2_queryctrl(vcs, arg);
  3569. }
  3570. case VIDIOC_G_CTRL: {
  3571. return vino_v4l2_g_ctrl(vcs, arg);
  3572. }
  3573. case VIDIOC_S_CTRL: {
  3574. return vino_v4l2_s_ctrl(vcs, arg);
  3575. }
  3576. default:
  3577. return -ENOIOCTLCMD;
  3578. }
  3579. return 0;
  3580. }
  3581. static int vino_ioctl(struct inode *inode, struct file *file,
  3582. unsigned int cmd, unsigned long arg)
  3583. {
  3584. struct video_device *dev = video_devdata(file);
  3585. struct vino_channel_settings *vcs = video_get_drvdata(dev);
  3586. int ret;
  3587. if (mutex_lock_interruptible(&vcs->mutex))
  3588. return -EINTR;
  3589. ret = video_usercopy(inode, file, cmd, arg, vino_do_ioctl);
  3590. mutex_unlock(&vcs->mutex);
  3591. return ret;
  3592. }
  3593. /* Initialization and cleanup */
  3594. // __initdata
  3595. static int vino_init_stage = 0;
  3596. static const struct file_operations vino_fops = {
  3597. .owner = THIS_MODULE,
  3598. .open = vino_open,
  3599. .release = vino_close,
  3600. .ioctl = vino_ioctl,
  3601. .mmap = vino_mmap,
  3602. .poll = vino_poll,
  3603. .llseek = no_llseek,
  3604. };
  3605. static struct video_device v4l_device_template = {
  3606. .name = "NOT SET",
  3607. //.type = VID_TYPE_CAPTURE | VID_TYPE_SUBCAPTURE |
  3608. // VID_TYPE_CLIPPING | VID_TYPE_SCALES, VID_TYPE_OVERLAY
  3609. .fops = &vino_fops,
  3610. .minor = -1,
  3611. };
  3612. static void vino_module_cleanup(int stage)
  3613. {
  3614. switch(stage) {
  3615. case 10:
  3616. video_unregister_device(vino_drvdata->b.v4l_device);
  3617. vino_drvdata->b.v4l_device = NULL;
  3618. case 9:
  3619. video_unregister_device(vino_drvdata->a.v4l_device);
  3620. vino_drvdata->a.v4l_device = NULL;
  3621. case 8:
  3622. vino_i2c_del_bus();
  3623. case 7:
  3624. free_irq(SGI_VINO_IRQ, NULL);
  3625. case 6:
  3626. if (vino_drvdata->b.v4l_device) {
  3627. video_device_release(vino_drvdata->b.v4l_device);
  3628. vino_drvdata->b.v4l_device = NULL;
  3629. }
  3630. case 5:
  3631. if (vino_drvdata->a.v4l_device) {
  3632. video_device_release(vino_drvdata->a.v4l_device);
  3633. vino_drvdata->a.v4l_device = NULL;
  3634. }
  3635. case 4:
  3636. /* all entries in dma_cpu dummy table have the same address */
  3637. dma_unmap_single(NULL,
  3638. vino_drvdata->dummy_desc_table.dma_cpu[0],
  3639. PAGE_SIZE, DMA_FROM_DEVICE);
  3640. dma_free_coherent(NULL, VINO_DUMMY_DESC_COUNT
  3641. * sizeof(dma_addr_t),
  3642. (void *)vino_drvdata->
  3643. dummy_desc_table.dma_cpu,
  3644. vino_drvdata->dummy_desc_table.dma);
  3645. case 3:
  3646. free_page(vino_drvdata->dummy_page);
  3647. case 2:
  3648. kfree(vino_drvdata);
  3649. case 1:
  3650. iounmap(vino);
  3651. case 0:
  3652. break;
  3653. default:
  3654. dprintk("vino_module_cleanup(): invalid cleanup stage = %d\n",
  3655. stage);
  3656. }
  3657. }
  3658. static int vino_probe(void)
  3659. {
  3660. unsigned long rev_id;
  3661. if (ip22_is_fullhouse()) {
  3662. printk(KERN_ERR "VINO doesn't exist in IP22 Fullhouse\n");
  3663. return -ENODEV;
  3664. }
  3665. if (!(sgimc->systemid & SGIMC_SYSID_EPRESENT)) {
  3666. printk(KERN_ERR "VINO is not found (EISA BUS not present)\n");
  3667. return -ENODEV;
  3668. }
  3669. vino = (struct sgi_vino *)ioremap(VINO_BASE, sizeof(struct sgi_vino));
  3670. if (!vino) {
  3671. printk(KERN_ERR "VINO: ioremap() failed\n");
  3672. return -EIO;
  3673. }
  3674. vino_init_stage++;
  3675. if (get_dbe(rev_id, &(vino->rev_id))) {
  3676. printk(KERN_ERR "Failed to read VINO revision register\n");
  3677. vino_module_cleanup(vino_init_stage);
  3678. return -ENODEV;
  3679. }
  3680. if (VINO_ID_VALUE(rev_id) != VINO_CHIP_ID) {
  3681. printk(KERN_ERR "Unknown VINO chip ID (Rev/ID: 0x%02lx)\n",
  3682. rev_id);
  3683. vino_module_cleanup(vino_init_stage);
  3684. return -ENODEV;
  3685. }
  3686. printk(KERN_INFO "VINO revision %ld found\n", VINO_REV_NUM(rev_id));
  3687. return 0;
  3688. }
  3689. static int vino_init(void)
  3690. {
  3691. dma_addr_t dma_dummy_address;
  3692. int i;
  3693. vino_drvdata = kzalloc(sizeof(struct vino_settings), GFP_KERNEL);
  3694. if (!vino_drvdata) {
  3695. vino_module_cleanup(vino_init_stage);
  3696. return -ENOMEM;
  3697. }
  3698. vino_init_stage++;
  3699. /* create a dummy dma descriptor */
  3700. vino_drvdata->dummy_page = get_zeroed_page(GFP_KERNEL | GFP_DMA);
  3701. if (!vino_drvdata->dummy_page) {
  3702. vino_module_cleanup(vino_init_stage);
  3703. return -ENOMEM;
  3704. }
  3705. vino_init_stage++;
  3706. // TODO: use page_count in dummy_desc_table
  3707. vino_drvdata->dummy_desc_table.dma_cpu =
  3708. dma_alloc_coherent(NULL,
  3709. VINO_DUMMY_DESC_COUNT * sizeof(dma_addr_t),
  3710. &vino_drvdata->dummy_desc_table.dma,
  3711. GFP_KERNEL | GFP_DMA);
  3712. if (!vino_drvdata->dummy_desc_table.dma_cpu) {
  3713. vino_module_cleanup(vino_init_stage);
  3714. return -ENOMEM;
  3715. }
  3716. vino_init_stage++;
  3717. dma_dummy_address = dma_map_single(NULL,
  3718. (void *)vino_drvdata->dummy_page,
  3719. PAGE_SIZE, DMA_FROM_DEVICE);
  3720. for (i = 0; i < VINO_DUMMY_DESC_COUNT; i++) {
  3721. vino_drvdata->dummy_desc_table.dma_cpu[i] = dma_dummy_address;
  3722. }
  3723. /* initialize VINO */
  3724. vino->control = 0;
  3725. vino->a.next_4_desc = vino_drvdata->dummy_desc_table.dma;
  3726. vino->b.next_4_desc = vino_drvdata->dummy_desc_table.dma;
  3727. udelay(VINO_DESC_FETCH_DELAY);
  3728. vino->intr_status = 0;
  3729. vino->a.fifo_thres = VINO_FIFO_THRESHOLD_DEFAULT;
  3730. vino->b.fifo_thres = VINO_FIFO_THRESHOLD_DEFAULT;
  3731. return 0;
  3732. }
  3733. static int vino_init_channel_settings(struct vino_channel_settings *vcs,
  3734. unsigned int channel, const char *name)
  3735. {
  3736. vcs->channel = channel;
  3737. vcs->input = VINO_INPUT_NONE;
  3738. vcs->alpha = 0;
  3739. vcs->users = 0;
  3740. vcs->data_format = VINO_DATA_FMT_GREY;
  3741. vcs->data_norm = VINO_DATA_NORM_NTSC;
  3742. vcs->decimation = 1;
  3743. vino_set_default_clipping(vcs);
  3744. vino_set_default_framerate(vcs);
  3745. vcs->capturing = 0;
  3746. mutex_init(&vcs->mutex);
  3747. spin_lock_init(&vcs->capture_lock);
  3748. mutex_init(&vcs->fb_queue.queue_mutex);
  3749. spin_lock_init(&vcs->fb_queue.queue_lock);
  3750. init_waitqueue_head(&vcs->fb_queue.frame_wait_queue);
  3751. vcs->v4l_device = video_device_alloc();
  3752. if (!vcs->v4l_device) {
  3753. vino_module_cleanup(vino_init_stage);
  3754. return -ENOMEM;
  3755. }
  3756. vino_init_stage++;
  3757. memcpy(vcs->v4l_device, &v4l_device_template,
  3758. sizeof(struct video_device));
  3759. strcpy(vcs->v4l_device->name, name);
  3760. vcs->v4l_device->release = video_device_release;
  3761. video_set_drvdata(vcs->v4l_device, vcs);
  3762. return 0;
  3763. }
  3764. static int __init vino_module_init(void)
  3765. {
  3766. int ret;
  3767. printk(KERN_INFO "SGI VINO driver version %s\n",
  3768. VINO_MODULE_VERSION);
  3769. ret = vino_probe();
  3770. if (ret)
  3771. return ret;
  3772. ret = vino_init();
  3773. if (ret)
  3774. return ret;
  3775. /* initialize data structures */
  3776. spin_lock_init(&vino_drvdata->vino_lock);
  3777. spin_lock_init(&vino_drvdata->input_lock);
  3778. ret = vino_init_channel_settings(&vino_drvdata->a, VINO_CHANNEL_A,
  3779. vino_v4l_device_name_a);
  3780. if (ret)
  3781. return ret;
  3782. ret = vino_init_channel_settings(&vino_drvdata->b, VINO_CHANNEL_B,
  3783. vino_v4l_device_name_b);
  3784. if (ret)
  3785. return ret;
  3786. /* initialize hardware and register V4L devices */
  3787. ret = request_irq(SGI_VINO_IRQ, vino_interrupt, 0,
  3788. vino_driver_description, NULL);
  3789. if (ret) {
  3790. printk(KERN_ERR "VINO: requesting IRQ %02d failed\n",
  3791. SGI_VINO_IRQ);
  3792. vino_module_cleanup(vino_init_stage);
  3793. return -EAGAIN;
  3794. }
  3795. vino_init_stage++;
  3796. ret = vino_i2c_add_bus();
  3797. if (ret) {
  3798. printk(KERN_ERR "VINO I2C bus registration failed\n");
  3799. vino_module_cleanup(vino_init_stage);
  3800. return ret;
  3801. }
  3802. vino_init_stage++;
  3803. ret = video_register_device(vino_drvdata->a.v4l_device,
  3804. VFL_TYPE_GRABBER, -1);
  3805. if (ret < 0) {
  3806. printk(KERN_ERR "VINO channel A Video4Linux-device "
  3807. "registration failed\n");
  3808. vino_module_cleanup(vino_init_stage);
  3809. return -EINVAL;
  3810. }
  3811. vino_init_stage++;
  3812. ret = video_register_device(vino_drvdata->b.v4l_device,
  3813. VFL_TYPE_GRABBER, -1);
  3814. if (ret < 0) {
  3815. printk(KERN_ERR "VINO channel B Video4Linux-device "
  3816. "registration failed\n");
  3817. vino_module_cleanup(vino_init_stage);
  3818. return -EINVAL;
  3819. }
  3820. vino_init_stage++;
  3821. #if defined(CONFIG_KMOD) && defined(MODULE)
  3822. request_module("saa7191");
  3823. request_module("indycam");
  3824. #endif
  3825. dprintk("init complete!\n");
  3826. return 0;
  3827. }
  3828. static void __exit vino_module_exit(void)
  3829. {
  3830. dprintk("exiting, stage = %d ...\n", vino_init_stage);
  3831. vino_module_cleanup(vino_init_stage);
  3832. dprintk("cleanup complete, exit!\n");
  3833. }
  3834. module_init(vino_module_init);
  3835. module_exit(vino_module_exit);