vino.c 102 KB

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