vino.c 96 KB

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