vino.c 105 KB

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