vino.c 104 KB

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