vino.c 104 KB

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