vino.c 99 KB

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