vino.c 99 KB

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