vino.c 104 KB

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