vino.c 105 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676
  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/time.h>
  30. #include <linux/version.h>
  31. #ifdef CONFIG_KMOD
  32. #include <linux/kmod.h>
  33. #endif
  34. #include <linux/i2c.h>
  35. #include <linux/i2c-algo-sgi.h>
  36. #include <linux/videodev.h>
  37. #include <media/v4l2-common.h>
  38. #include <linux/video_decoder.h>
  39. #include <linux/mutex.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 mutex queue_mutex;
  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 mutex mutex;
  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_del_adapter(&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. mutex_lock(&q->queue_mutex);
  965. vino_queue_free_with_count(q, q->length);
  966. mutex_unlock(&q->queue_mutex);
  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. mutex_lock(&q->queue_mutex);
  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. mutex_unlock(&q->queue_mutex);
  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. /* to ensure that schedule_timeout will return immediately
  1703. * if VINO interrupt was triggered meanwhile */
  1704. schedule_timeout_interruptible(msecs_to_jiffies(100));
  1705. if (signal_pending(current))
  1706. err = -EINTR;
  1707. remove_wait_queue(&vcs->fb_queue.frame_wait_queue, &wait);
  1708. dprintk("vino_wait_for_frame(): waiting for frame %s\n",
  1709. err ? "failed" : "ok");
  1710. return err;
  1711. }
  1712. /* the function assumes that PAGE_SIZE % 4 == 0 */
  1713. static void vino_convert_to_rgba(struct vino_framebuffer *fb) {
  1714. unsigned char *pageptr;
  1715. unsigned int page, i;
  1716. unsigned char a;
  1717. for (page = 0; page < fb->desc_table.page_count; page++) {
  1718. pageptr = (unsigned char *)fb->desc_table.virtual[page];
  1719. for (i = 0; i < PAGE_SIZE; i += 4) {
  1720. a = pageptr[0];
  1721. pageptr[0] = pageptr[3];
  1722. pageptr[1] = pageptr[2];
  1723. pageptr[2] = pageptr[1];
  1724. pageptr[3] = a;
  1725. pageptr += 4;
  1726. }
  1727. }
  1728. }
  1729. /* checks if the buffer is in correct state and syncs data */
  1730. static int vino_check_buffer(struct vino_channel_settings *vcs,
  1731. struct vino_framebuffer *fb)
  1732. {
  1733. int err = 0;
  1734. unsigned long flags;
  1735. dprintk("vino_check_buffer():\n");
  1736. spin_lock_irqsave(&fb->state_lock, flags);
  1737. switch (fb->state) {
  1738. case VINO_FRAMEBUFFER_IN_USE:
  1739. err = -EIO;
  1740. break;
  1741. case VINO_FRAMEBUFFER_READY:
  1742. vino_sync_buffer(fb);
  1743. fb->state = VINO_FRAMEBUFFER_UNUSED;
  1744. break;
  1745. default:
  1746. err = -EINVAL;
  1747. }
  1748. spin_unlock_irqrestore(&fb->state_lock, flags);
  1749. if (!err) {
  1750. if (vino_pixel_conversion
  1751. && (fb->data_format == VINO_DATA_FMT_RGB32)) {
  1752. vino_convert_to_rgba(fb);
  1753. }
  1754. } else if (err && (err != -EINVAL)) {
  1755. dprintk("vino_check_buffer(): buffer not ready\n");
  1756. spin_lock_irqsave(&vino_drvdata->vino_lock, flags);
  1757. vino_dma_stop(vcs);
  1758. vino_clear_interrupt(vcs);
  1759. spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags);
  1760. }
  1761. return err;
  1762. }
  1763. /* forcefully terminates capture */
  1764. static void vino_capture_stop(struct vino_channel_settings *vcs)
  1765. {
  1766. unsigned int incoming = 0, outgoing = 0, id;
  1767. unsigned long flags, flags2;
  1768. dprintk("vino_capture_stop():\n");
  1769. spin_lock_irqsave(&vcs->capture_lock, flags);
  1770. /* unset capturing to stop queue processing */
  1771. vcs->capturing = 0;
  1772. spin_lock_irqsave(&vino_drvdata->vino_lock, flags2);
  1773. vino_dma_stop(vcs);
  1774. vino_clear_interrupt(vcs);
  1775. spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags2);
  1776. /* remove all items from the queue */
  1777. if (vino_queue_get_incoming(&vcs->fb_queue, &incoming)) {
  1778. dprintk("vino_capture_stop(): "
  1779. "vino_queue_get_incoming() failed\n");
  1780. goto out;
  1781. }
  1782. while (incoming > 0) {
  1783. vino_queue_transfer(&vcs->fb_queue);
  1784. if (vino_queue_get_incoming(&vcs->fb_queue, &incoming)) {
  1785. dprintk("vino_capture_stop(): "
  1786. "vino_queue_get_incoming() failed\n");
  1787. goto out;
  1788. }
  1789. }
  1790. if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
  1791. dprintk("vino_capture_stop(): "
  1792. "vino_queue_get_outgoing() failed\n");
  1793. goto out;
  1794. }
  1795. while (outgoing > 0) {
  1796. vino_queue_remove(&vcs->fb_queue, &id);
  1797. if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
  1798. dprintk("vino_capture_stop(): "
  1799. "vino_queue_get_outgoing() failed\n");
  1800. goto out;
  1801. }
  1802. }
  1803. out:
  1804. spin_unlock_irqrestore(&vcs->capture_lock, flags);
  1805. }
  1806. #if 0
  1807. static int vino_capture_failed(struct vino_channel_settings *vcs)
  1808. {
  1809. struct vino_framebuffer *fb;
  1810. unsigned long flags;
  1811. unsigned int i;
  1812. int ret;
  1813. dprintk("vino_capture_failed():\n");
  1814. spin_lock_irqsave(&vino_drvdata->vino_lock, flags);
  1815. vino_dma_stop(vcs);
  1816. vino_clear_interrupt(vcs);
  1817. spin_unlock_irqrestore(&vino_drvdata->vino_lock, flags);
  1818. ret = vino_queue_get_incoming(&vcs->fb_queue, &i);
  1819. if (ret == VINO_QUEUE_ERROR) {
  1820. dprintk("vino_queue_get_incoming() failed\n");
  1821. return -EINVAL;
  1822. }
  1823. if (i == 0) {
  1824. /* no buffers to process */
  1825. return 0;
  1826. }
  1827. fb = vino_queue_peek(&vcs->fb_queue, &i);
  1828. if (fb == NULL) {
  1829. dprintk("vino_queue_peek() failed\n");
  1830. return -EINVAL;
  1831. }
  1832. spin_lock_irqsave(&fb->state_lock, flags);
  1833. if (fb->state == VINO_FRAMEBUFFER_IN_USE) {
  1834. fb->state = VINO_FRAMEBUFFER_UNUSED;
  1835. vino_queue_transfer(&vcs->fb_queue);
  1836. vino_queue_remove(&vcs->fb_queue, &i);
  1837. /* we should actually discard the newest frame,
  1838. * but who cares ... */
  1839. }
  1840. spin_unlock_irqrestore(&fb->state_lock, flags);
  1841. return 0;
  1842. }
  1843. #endif
  1844. static void vino_skip_frame(struct vino_channel_settings *vcs)
  1845. {
  1846. struct vino_framebuffer *fb;
  1847. unsigned long flags;
  1848. unsigned int id;
  1849. spin_lock_irqsave(&vcs->capture_lock, flags);
  1850. fb = vino_queue_peek(&vcs->fb_queue, &id);
  1851. if (!fb) {
  1852. spin_unlock_irqrestore(&vcs->capture_lock, flags);
  1853. dprintk("vino_skip_frame(): vino_queue_peek() failed!\n");
  1854. return;
  1855. }
  1856. spin_unlock_irqrestore(&vcs->capture_lock, flags);
  1857. spin_lock_irqsave(&fb->state_lock, flags);
  1858. fb->state = VINO_FRAMEBUFFER_UNUSED;
  1859. spin_unlock_irqrestore(&fb->state_lock, flags);
  1860. vino_capture_next(vcs, 0);
  1861. }
  1862. static void vino_frame_done(struct vino_channel_settings *vcs)
  1863. {
  1864. struct vino_framebuffer *fb;
  1865. unsigned long flags;
  1866. spin_lock_irqsave(&vcs->capture_lock, flags);
  1867. fb = vino_queue_transfer(&vcs->fb_queue);
  1868. if (!fb) {
  1869. spin_unlock_irqrestore(&vcs->capture_lock, flags);
  1870. dprintk("vino_frame_done(): vino_queue_transfer() failed!\n");
  1871. return;
  1872. }
  1873. spin_unlock_irqrestore(&vcs->capture_lock, flags);
  1874. fb->frame_counter = vcs->int_data.frame_counter;
  1875. memcpy(&fb->timestamp, &vcs->int_data.timestamp,
  1876. sizeof(struct timeval));
  1877. spin_lock_irqsave(&fb->state_lock, flags);
  1878. if (fb->state == VINO_FRAMEBUFFER_IN_USE)
  1879. fb->state = VINO_FRAMEBUFFER_READY;
  1880. spin_unlock_irqrestore(&fb->state_lock, flags);
  1881. wake_up(&vcs->fb_queue.frame_wait_queue);
  1882. vino_capture_next(vcs, 0);
  1883. }
  1884. static void vino_capture_tasklet(unsigned long channel) {
  1885. struct vino_channel_settings *vcs;
  1886. vcs = (channel == VINO_CHANNEL_A)
  1887. ? &vino_drvdata->a : &vino_drvdata->b;
  1888. if (vcs->int_data.skip)
  1889. vcs->int_data.skip_count++;
  1890. if (vcs->int_data.skip && (vcs->int_data.skip_count
  1891. <= VINO_MAX_FRAME_SKIP_COUNT)) {
  1892. vino_skip_frame(vcs);
  1893. } else {
  1894. vcs->int_data.skip_count = 0;
  1895. vino_frame_done(vcs);
  1896. }
  1897. }
  1898. static irqreturn_t vino_interrupt(int irq, void *dev_id)
  1899. {
  1900. u32 ctrl, intr;
  1901. unsigned int fc_a, fc_b;
  1902. int handled_a = 0, skip_a = 0, done_a = 0;
  1903. int handled_b = 0, skip_b = 0, done_b = 0;
  1904. #ifdef VINO_DEBUG_INT
  1905. int loop = 0;
  1906. unsigned int line_count = vino->a.line_count,
  1907. page_index = vino->a.page_index,
  1908. field_counter = vino->a.field_counter,
  1909. start_desc_tbl = vino->a.start_desc_tbl,
  1910. next_4_desc = vino->a.next_4_desc;
  1911. unsigned int line_count_2,
  1912. page_index_2,
  1913. field_counter_2,
  1914. start_desc_tbl_2,
  1915. next_4_desc_2;
  1916. #endif
  1917. spin_lock(&vino_drvdata->vino_lock);
  1918. while ((intr = vino->intr_status)) {
  1919. fc_a = vino->a.field_counter >> 1;
  1920. fc_b = vino->b.field_counter >> 1;
  1921. /* handle error-interrupts in some special way ?
  1922. * --> skips frames */
  1923. if (intr & VINO_INTSTAT_A) {
  1924. if (intr & VINO_INTSTAT_A_EOF) {
  1925. vino_drvdata->a.field++;
  1926. if (vino_drvdata->a.field > 1) {
  1927. vino_dma_stop(&vino_drvdata->a);
  1928. vino_clear_interrupt(&vino_drvdata->a);
  1929. vino_drvdata->a.field = 0;
  1930. done_a = 1;
  1931. } else {
  1932. if (vino->a.page_index
  1933. != vino_drvdata->a.line_size) {
  1934. vino->a.line_count = 0;
  1935. vino->a.page_index =
  1936. vino_drvdata->
  1937. a.line_size;
  1938. vino->a.next_4_desc =
  1939. vino->a.start_desc_tbl;
  1940. }
  1941. }
  1942. dprintk("channel A end-of-field "
  1943. "interrupt: %04x\n", intr);
  1944. } else {
  1945. vino_dma_stop(&vino_drvdata->a);
  1946. vino_clear_interrupt(&vino_drvdata->a);
  1947. vino_drvdata->a.field = 0;
  1948. skip_a = 1;
  1949. dprintk("channel A error interrupt: %04x\n",
  1950. intr);
  1951. }
  1952. #ifdef VINO_DEBUG_INT
  1953. line_count_2 = vino->a.line_count;
  1954. page_index_2 = vino->a.page_index;
  1955. field_counter_2 = vino->a.field_counter;
  1956. start_desc_tbl_2 = vino->a.start_desc_tbl;
  1957. next_4_desc_2 = vino->a.next_4_desc;
  1958. printk("intr = %04x, loop = %d, field = %d\n",
  1959. intr, loop, vino_drvdata->a.field);
  1960. printk("1- line count = %04d, page index = %04d, "
  1961. "start = %08x, next = %08x\n"
  1962. " fieldc = %d, framec = %d\n",
  1963. line_count, page_index, start_desc_tbl,
  1964. next_4_desc, field_counter, fc_a);
  1965. printk("12-line count = %04d, page index = %04d, "
  1966. " start = %08x, next = %08x\n",
  1967. line_count_2, page_index_2, start_desc_tbl_2,
  1968. next_4_desc_2);
  1969. if (done_a)
  1970. printk("\n");
  1971. #endif
  1972. }
  1973. if (intr & VINO_INTSTAT_B) {
  1974. if (intr & VINO_INTSTAT_B_EOF) {
  1975. vino_drvdata->b.field++;
  1976. if (vino_drvdata->b.field > 1) {
  1977. vino_dma_stop(&vino_drvdata->b);
  1978. vino_clear_interrupt(&vino_drvdata->b);
  1979. vino_drvdata->b.field = 0;
  1980. done_b = 1;
  1981. }
  1982. dprintk("channel B end-of-field "
  1983. "interrupt: %04x\n", intr);
  1984. } else {
  1985. vino_dma_stop(&vino_drvdata->b);
  1986. vino_clear_interrupt(&vino_drvdata->b);
  1987. vino_drvdata->b.field = 0;
  1988. skip_b = 1;
  1989. dprintk("channel B error interrupt: %04x\n",
  1990. intr);
  1991. }
  1992. }
  1993. /* Always remember to clear interrupt status.
  1994. * Disable VINO interrupts while we do this. */
  1995. ctrl = vino->control;
  1996. vino->control = ctrl & ~(VINO_CTRL_A_INT | VINO_CTRL_B_INT);
  1997. vino->intr_status = ~intr;
  1998. vino->control = ctrl;
  1999. spin_unlock(&vino_drvdata->vino_lock);
  2000. if ((!handled_a) && (done_a || skip_a)) {
  2001. if (!skip_a) {
  2002. do_gettimeofday(&vino_drvdata->
  2003. a.int_data.timestamp);
  2004. vino_drvdata->a.int_data.frame_counter = fc_a;
  2005. }
  2006. vino_drvdata->a.int_data.skip = skip_a;
  2007. dprintk("channel A %s, interrupt: %d\n",
  2008. skip_a ? "skipping frame" : "frame done",
  2009. intr);
  2010. tasklet_hi_schedule(&vino_tasklet_a);
  2011. handled_a = 1;
  2012. }
  2013. if ((!handled_b) && (done_b || skip_b)) {
  2014. if (!skip_b) {
  2015. do_gettimeofday(&vino_drvdata->
  2016. b.int_data.timestamp);
  2017. vino_drvdata->b.int_data.frame_counter = fc_b;
  2018. }
  2019. vino_drvdata->b.int_data.skip = skip_b;
  2020. dprintk("channel B %s, interrupt: %d\n",
  2021. skip_b ? "skipping frame" : "frame done",
  2022. intr);
  2023. tasklet_hi_schedule(&vino_tasklet_b);
  2024. handled_b = 1;
  2025. }
  2026. #ifdef VINO_DEBUG_INT
  2027. loop++;
  2028. #endif
  2029. spin_lock(&vino_drvdata->vino_lock);
  2030. }
  2031. spin_unlock(&vino_drvdata->vino_lock);
  2032. return IRQ_HANDLED;
  2033. }
  2034. /* VINO video input management */
  2035. static int vino_get_saa7191_input(int input)
  2036. {
  2037. switch (input) {
  2038. case VINO_INPUT_COMPOSITE:
  2039. return SAA7191_INPUT_COMPOSITE;
  2040. case VINO_INPUT_SVIDEO:
  2041. return SAA7191_INPUT_SVIDEO;
  2042. default:
  2043. printk(KERN_ERR "VINO: vino_get_saa7191_input(): "
  2044. "invalid input!\n");
  2045. return -1;
  2046. }
  2047. }
  2048. static int vino_get_saa7191_norm(unsigned int data_norm)
  2049. {
  2050. switch (data_norm) {
  2051. case VINO_DATA_NORM_AUTO:
  2052. return SAA7191_NORM_AUTO;
  2053. case VINO_DATA_NORM_AUTO_EXT:
  2054. return SAA7191_NORM_AUTO_EXT;
  2055. case VINO_DATA_NORM_PAL:
  2056. return SAA7191_NORM_PAL;
  2057. case VINO_DATA_NORM_NTSC:
  2058. return SAA7191_NORM_NTSC;
  2059. case VINO_DATA_NORM_SECAM:
  2060. return SAA7191_NORM_SECAM;
  2061. default:
  2062. printk(KERN_ERR "VINO: vino_get_saa7191_norm(): "
  2063. "invalid norm!\n");
  2064. return -1;
  2065. }
  2066. }
  2067. static int vino_get_from_saa7191_norm(int saa7191_norm)
  2068. {
  2069. switch (saa7191_norm) {
  2070. case SAA7191_NORM_PAL:
  2071. return VINO_DATA_NORM_PAL;
  2072. case SAA7191_NORM_NTSC:
  2073. return VINO_DATA_NORM_NTSC;
  2074. case SAA7191_NORM_SECAM:
  2075. return VINO_DATA_NORM_SECAM;
  2076. default:
  2077. printk(KERN_ERR "VINO: vino_get_from_saa7191_norm(): "
  2078. "invalid norm!\n");
  2079. return VINO_DATA_NORM_NONE;
  2080. }
  2081. }
  2082. static int vino_saa7191_set_norm(unsigned int *data_norm)
  2083. {
  2084. int saa7191_norm, new_data_norm;
  2085. int err = 0;
  2086. saa7191_norm = vino_get_saa7191_norm(*data_norm);
  2087. err = i2c_decoder_command(DECODER_SAA7191_SET_NORM,
  2088. &saa7191_norm);
  2089. if (err)
  2090. goto out;
  2091. if ((*data_norm == VINO_DATA_NORM_AUTO)
  2092. || (*data_norm == VINO_DATA_NORM_AUTO_EXT)) {
  2093. struct saa7191_status status;
  2094. err = i2c_decoder_command(DECODER_SAA7191_GET_STATUS,
  2095. &status);
  2096. if (err)
  2097. goto out;
  2098. new_data_norm =
  2099. vino_get_from_saa7191_norm(status.norm);
  2100. if (new_data_norm == VINO_DATA_NORM_NONE) {
  2101. err = -EINVAL;
  2102. goto out;
  2103. }
  2104. *data_norm = (unsigned int)new_data_norm;
  2105. }
  2106. out:
  2107. return err;
  2108. }
  2109. /* execute with input_lock locked */
  2110. static int vino_is_input_owner(struct vino_channel_settings *vcs)
  2111. {
  2112. switch(vcs->input) {
  2113. case VINO_INPUT_COMPOSITE:
  2114. case VINO_INPUT_SVIDEO:
  2115. return (vino_drvdata->decoder.owner == vcs->channel);
  2116. case VINO_INPUT_D1:
  2117. return (vino_drvdata->camera.owner == vcs->channel);
  2118. default:
  2119. return 0;
  2120. }
  2121. }
  2122. static int vino_acquire_input(struct vino_channel_settings *vcs)
  2123. {
  2124. unsigned long flags;
  2125. int ret = 0;
  2126. dprintk("vino_acquire_input():\n");
  2127. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2128. /* First try D1 and then SAA7191 */
  2129. if (vino_drvdata->camera.driver
  2130. && (vino_drvdata->camera.owner == VINO_NO_CHANNEL)) {
  2131. if (i2c_use_client(vino_drvdata->camera.driver)) {
  2132. ret = -ENODEV;
  2133. goto out;
  2134. }
  2135. vino_drvdata->camera.owner = vcs->channel;
  2136. vcs->input = VINO_INPUT_D1;
  2137. vcs->data_norm = VINO_DATA_NORM_D1;
  2138. } else if (vino_drvdata->decoder.driver
  2139. && (vino_drvdata->decoder.owner == VINO_NO_CHANNEL)) {
  2140. int input, data_norm;
  2141. int saa7191_input;
  2142. if (i2c_use_client(vino_drvdata->decoder.driver)) {
  2143. ret = -ENODEV;
  2144. goto out;
  2145. }
  2146. input = VINO_INPUT_COMPOSITE;
  2147. saa7191_input = vino_get_saa7191_input(input);
  2148. ret = i2c_decoder_command(DECODER_SET_INPUT,
  2149. &saa7191_input);
  2150. if (ret) {
  2151. ret = -EINVAL;
  2152. goto out;
  2153. }
  2154. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2155. /* Don't hold spinlocks while auto-detecting norm
  2156. * as it may take a while... */
  2157. data_norm = VINO_DATA_NORM_AUTO_EXT;
  2158. ret = vino_saa7191_set_norm(&data_norm);
  2159. if ((ret == -EBUSY) || (ret == -EAGAIN)) {
  2160. data_norm = VINO_DATA_NORM_PAL;
  2161. ret = vino_saa7191_set_norm(&data_norm);
  2162. }
  2163. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2164. if (ret) {
  2165. ret = -EINVAL;
  2166. goto out;
  2167. }
  2168. vino_drvdata->decoder.owner = vcs->channel;
  2169. vcs->input = input;
  2170. vcs->data_norm = data_norm;
  2171. } else {
  2172. vcs->input = (vcs->channel == VINO_CHANNEL_A) ?
  2173. vino_drvdata->b.input : vino_drvdata->a.input;
  2174. vcs->data_norm = (vcs->channel == VINO_CHANNEL_A) ?
  2175. vino_drvdata->b.data_norm : vino_drvdata->a.data_norm;
  2176. }
  2177. if (vcs->input == VINO_INPUT_NONE) {
  2178. ret = -ENODEV;
  2179. goto out;
  2180. }
  2181. vino_set_default_clipping(vcs);
  2182. vino_set_default_scaling(vcs);
  2183. vino_set_default_framerate(vcs);
  2184. dprintk("vino_acquire_input(): %s\n", vino_inputs[vcs->input].name);
  2185. out:
  2186. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2187. return ret;
  2188. }
  2189. static int vino_set_input(struct vino_channel_settings *vcs, int input)
  2190. {
  2191. struct vino_channel_settings *vcs2 = (vcs->channel == VINO_CHANNEL_A) ?
  2192. &vino_drvdata->b : &vino_drvdata->a;
  2193. unsigned long flags;
  2194. int ret = 0;
  2195. dprintk("vino_set_input():\n");
  2196. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2197. if (vcs->input == input)
  2198. goto out;
  2199. switch (input) {
  2200. case VINO_INPUT_COMPOSITE:
  2201. case VINO_INPUT_SVIDEO:
  2202. if (!vino_drvdata->decoder.driver) {
  2203. ret = -EINVAL;
  2204. goto out;
  2205. }
  2206. if (vino_drvdata->decoder.owner == VINO_NO_CHANNEL) {
  2207. if (i2c_use_client(vino_drvdata->decoder.driver)) {
  2208. ret = -ENODEV;
  2209. goto out;
  2210. }
  2211. vino_drvdata->decoder.owner = vcs->channel;
  2212. }
  2213. if (vino_drvdata->decoder.owner == vcs->channel) {
  2214. int data_norm;
  2215. int saa7191_input;
  2216. saa7191_input = vino_get_saa7191_input(input);
  2217. ret = i2c_decoder_command(DECODER_SET_INPUT,
  2218. &saa7191_input);
  2219. if (ret) {
  2220. vino_drvdata->decoder.owner = VINO_NO_CHANNEL;
  2221. ret = -EINVAL;
  2222. goto out;
  2223. }
  2224. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2225. /* Don't hold spinlocks while auto-detecting norm
  2226. * as it may take a while... */
  2227. data_norm = VINO_DATA_NORM_AUTO_EXT;
  2228. ret = vino_saa7191_set_norm(&data_norm);
  2229. if ((ret == -EBUSY) || (ret == -EAGAIN)) {
  2230. data_norm = VINO_DATA_NORM_PAL;
  2231. ret = vino_saa7191_set_norm(&data_norm);
  2232. }
  2233. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2234. if (ret) {
  2235. vino_drvdata->decoder.owner = VINO_NO_CHANNEL;
  2236. ret = -EINVAL;
  2237. goto out;
  2238. }
  2239. vcs->input = input;
  2240. vcs->data_norm = data_norm;
  2241. } else {
  2242. if (input != vcs2->input) {
  2243. ret = -EBUSY;
  2244. goto out;
  2245. }
  2246. vcs->input = input;
  2247. vcs->data_norm = vcs2->data_norm;
  2248. }
  2249. if (vino_drvdata->camera.owner == vcs->channel) {
  2250. /* Transfer the ownership or release the input */
  2251. if (vcs2->input == VINO_INPUT_D1) {
  2252. vino_drvdata->camera.owner = vcs2->channel;
  2253. } else {
  2254. i2c_release_client(vino_drvdata->
  2255. camera.driver);
  2256. vino_drvdata->camera.owner = VINO_NO_CHANNEL;
  2257. }
  2258. }
  2259. break;
  2260. case VINO_INPUT_D1:
  2261. if (!vino_drvdata->camera.driver) {
  2262. ret = -EINVAL;
  2263. goto out;
  2264. }
  2265. if (vino_drvdata->camera.owner == VINO_NO_CHANNEL) {
  2266. if (i2c_use_client(vino_drvdata->camera.driver)) {
  2267. ret = -ENODEV;
  2268. goto out;
  2269. }
  2270. vino_drvdata->camera.owner = vcs->channel;
  2271. }
  2272. if (vino_drvdata->decoder.owner == vcs->channel) {
  2273. /* Transfer the ownership or release the input */
  2274. if ((vcs2->input == VINO_INPUT_COMPOSITE) ||
  2275. (vcs2->input == VINO_INPUT_SVIDEO)) {
  2276. vino_drvdata->decoder.owner = vcs2->channel;
  2277. } else {
  2278. i2c_release_client(vino_drvdata->
  2279. decoder.driver);
  2280. vino_drvdata->decoder.owner = VINO_NO_CHANNEL;
  2281. }
  2282. }
  2283. vcs->input = input;
  2284. vcs->data_norm = VINO_DATA_NORM_D1;
  2285. break;
  2286. default:
  2287. ret = -EINVAL;
  2288. goto out;
  2289. }
  2290. vino_set_default_clipping(vcs);
  2291. vino_set_default_scaling(vcs);
  2292. vino_set_default_framerate(vcs);
  2293. dprintk("vino_set_input(): %s\n", vino_inputs[vcs->input].name);
  2294. out:
  2295. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2296. return ret;
  2297. }
  2298. static void vino_release_input(struct vino_channel_settings *vcs)
  2299. {
  2300. struct vino_channel_settings *vcs2 = (vcs->channel == VINO_CHANNEL_A) ?
  2301. &vino_drvdata->b : &vino_drvdata->a;
  2302. unsigned long flags;
  2303. dprintk("vino_release_input():\n");
  2304. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2305. /* Release ownership of the channel
  2306. * and if the other channel takes input from
  2307. * the same source, transfer the ownership */
  2308. if (vino_drvdata->camera.owner == vcs->channel) {
  2309. if (vcs2->input == VINO_INPUT_D1) {
  2310. vino_drvdata->camera.owner = vcs2->channel;
  2311. } else {
  2312. i2c_release_client(vino_drvdata->camera.driver);
  2313. vino_drvdata->camera.owner = VINO_NO_CHANNEL;
  2314. }
  2315. } else if (vino_drvdata->decoder.owner == vcs->channel) {
  2316. if ((vcs2->input == VINO_INPUT_COMPOSITE) ||
  2317. (vcs2->input == VINO_INPUT_SVIDEO)) {
  2318. vino_drvdata->decoder.owner = vcs2->channel;
  2319. } else {
  2320. i2c_release_client(vino_drvdata->decoder.driver);
  2321. vino_drvdata->decoder.owner = VINO_NO_CHANNEL;
  2322. }
  2323. }
  2324. vcs->input = VINO_INPUT_NONE;
  2325. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2326. }
  2327. /* execute with input_lock locked */
  2328. static int vino_set_data_norm(struct vino_channel_settings *vcs,
  2329. unsigned int data_norm,
  2330. unsigned long *flags)
  2331. {
  2332. int err = 0;
  2333. if (data_norm == vcs->data_norm)
  2334. return 0;
  2335. switch (vcs->input) {
  2336. case VINO_INPUT_D1:
  2337. /* only one "norm" supported */
  2338. if ((data_norm != VINO_DATA_NORM_D1)
  2339. && (data_norm != VINO_DATA_NORM_AUTO)
  2340. && (data_norm != VINO_DATA_NORM_AUTO_EXT))
  2341. return -EINVAL;
  2342. break;
  2343. case VINO_INPUT_COMPOSITE:
  2344. case VINO_INPUT_SVIDEO: {
  2345. if ((data_norm != VINO_DATA_NORM_PAL)
  2346. && (data_norm != VINO_DATA_NORM_NTSC)
  2347. && (data_norm != VINO_DATA_NORM_SECAM)
  2348. && (data_norm != VINO_DATA_NORM_AUTO)
  2349. && (data_norm != VINO_DATA_NORM_AUTO_EXT))
  2350. return -EINVAL;
  2351. spin_unlock_irqrestore(&vino_drvdata->input_lock, *flags);
  2352. /* Don't hold spinlocks while setting norm
  2353. * as it may take a while... */
  2354. err = vino_saa7191_set_norm(&data_norm);
  2355. spin_lock_irqsave(&vino_drvdata->input_lock, *flags);
  2356. if (err)
  2357. goto out;
  2358. vcs->data_norm = data_norm;
  2359. vino_set_default_clipping(vcs);
  2360. vino_set_default_scaling(vcs);
  2361. vino_set_default_framerate(vcs);
  2362. break;
  2363. }
  2364. default:
  2365. return -EINVAL;
  2366. }
  2367. out:
  2368. return err;
  2369. }
  2370. /* V4L2 helper functions */
  2371. static int vino_find_data_format(__u32 pixelformat)
  2372. {
  2373. int i;
  2374. for (i = 0; i < VINO_DATA_FMT_COUNT; i++) {
  2375. if (vino_data_formats[i].pixelformat == pixelformat)
  2376. return i;
  2377. }
  2378. return VINO_DATA_FMT_NONE;
  2379. }
  2380. static int vino_enum_data_norm(struct vino_channel_settings *vcs, __u32 index)
  2381. {
  2382. int data_norm = VINO_DATA_NORM_NONE;
  2383. unsigned long flags;
  2384. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2385. switch(vcs->input) {
  2386. case VINO_INPUT_COMPOSITE:
  2387. case VINO_INPUT_SVIDEO:
  2388. if (index == 0) {
  2389. data_norm = VINO_DATA_NORM_PAL;
  2390. } else if (index == 1) {
  2391. data_norm = VINO_DATA_NORM_NTSC;
  2392. } else if (index == 2) {
  2393. data_norm = VINO_DATA_NORM_SECAM;
  2394. }
  2395. break;
  2396. case VINO_INPUT_D1:
  2397. if (index == 0) {
  2398. data_norm = VINO_DATA_NORM_D1;
  2399. }
  2400. break;
  2401. }
  2402. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2403. return data_norm;
  2404. }
  2405. static int vino_enum_input(struct vino_channel_settings *vcs, __u32 index)
  2406. {
  2407. int input = VINO_INPUT_NONE;
  2408. unsigned long flags;
  2409. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2410. if (vino_drvdata->decoder.driver && vino_drvdata->camera.driver) {
  2411. switch (index) {
  2412. case 0:
  2413. input = VINO_INPUT_COMPOSITE;
  2414. break;
  2415. case 1:
  2416. input = VINO_INPUT_SVIDEO;
  2417. break;
  2418. case 2:
  2419. input = VINO_INPUT_D1;
  2420. break;
  2421. }
  2422. } else if (vino_drvdata->decoder.driver) {
  2423. switch (index) {
  2424. case 0:
  2425. input = VINO_INPUT_COMPOSITE;
  2426. break;
  2427. case 1:
  2428. input = VINO_INPUT_SVIDEO;
  2429. break;
  2430. }
  2431. } else if (vino_drvdata->camera.driver) {
  2432. switch (index) {
  2433. case 0:
  2434. input = VINO_INPUT_D1;
  2435. break;
  2436. }
  2437. }
  2438. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2439. return input;
  2440. }
  2441. /* execute with input_lock locked */
  2442. static __u32 vino_find_input_index(struct vino_channel_settings *vcs)
  2443. {
  2444. __u32 index = 0;
  2445. // FIXME: detect when no inputs available
  2446. if (vino_drvdata->decoder.driver && vino_drvdata->camera.driver) {
  2447. switch (vcs->input) {
  2448. case VINO_INPUT_COMPOSITE:
  2449. index = 0;
  2450. break;
  2451. case VINO_INPUT_SVIDEO:
  2452. index = 1;
  2453. break;
  2454. case VINO_INPUT_D1:
  2455. index = 2;
  2456. break;
  2457. }
  2458. } else if (vino_drvdata->decoder.driver) {
  2459. switch (vcs->input) {
  2460. case VINO_INPUT_COMPOSITE:
  2461. index = 0;
  2462. break;
  2463. case VINO_INPUT_SVIDEO:
  2464. index = 1;
  2465. break;
  2466. }
  2467. } else if (vino_drvdata->camera.driver) {
  2468. switch (vcs->input) {
  2469. case VINO_INPUT_D1:
  2470. index = 0;
  2471. break;
  2472. }
  2473. }
  2474. return index;
  2475. }
  2476. /* V4L2 ioctls */
  2477. static void vino_v4l2_querycap(struct v4l2_capability *cap)
  2478. {
  2479. memset(cap, 0, sizeof(struct v4l2_capability));
  2480. strcpy(cap->driver, vino_driver_name);
  2481. strcpy(cap->card, vino_driver_description);
  2482. strcpy(cap->bus_info, vino_bus_name);
  2483. cap->version = VINO_VERSION_CODE;
  2484. cap->capabilities =
  2485. V4L2_CAP_VIDEO_CAPTURE |
  2486. V4L2_CAP_STREAMING;
  2487. // V4L2_CAP_OVERLAY, V4L2_CAP_READWRITE
  2488. }
  2489. static int vino_v4l2_enuminput(struct vino_channel_settings *vcs,
  2490. struct v4l2_input *i)
  2491. {
  2492. __u32 index = i->index;
  2493. int input;
  2494. dprintk("requested index = %d\n", index);
  2495. input = vino_enum_input(vcs, index);
  2496. if (input == VINO_INPUT_NONE)
  2497. return -EINVAL;
  2498. memset(i, 0, sizeof(struct v4l2_input));
  2499. i->index = index;
  2500. i->type = V4L2_INPUT_TYPE_CAMERA;
  2501. i->std = vino_inputs[input].std;
  2502. strcpy(i->name, vino_inputs[input].name);
  2503. if ((input == VINO_INPUT_COMPOSITE)
  2504. || (input == VINO_INPUT_SVIDEO)) {
  2505. struct saa7191_status status;
  2506. i2c_decoder_command(DECODER_SAA7191_GET_STATUS, &status);
  2507. i->status |= status.signal ? 0 : V4L2_IN_ST_NO_SIGNAL;
  2508. i->status |= status.color ? 0 : V4L2_IN_ST_NO_COLOR;
  2509. }
  2510. return 0;
  2511. }
  2512. static int vino_v4l2_g_input(struct vino_channel_settings *vcs,
  2513. unsigned int *i)
  2514. {
  2515. __u32 index;
  2516. int input;
  2517. unsigned long flags;
  2518. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2519. input = vcs->input;
  2520. index = vino_find_input_index(vcs);
  2521. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2522. dprintk("input = %d\n", input);
  2523. if (input == VINO_INPUT_NONE) {
  2524. return -EINVAL;
  2525. }
  2526. *i = index;
  2527. return 0;
  2528. }
  2529. static int vino_v4l2_s_input(struct vino_channel_settings *vcs,
  2530. unsigned int *i)
  2531. {
  2532. int input;
  2533. dprintk("requested input = %d\n", *i);
  2534. input = vino_enum_input(vcs, *i);
  2535. if (input == VINO_INPUT_NONE)
  2536. return -EINVAL;
  2537. return vino_set_input(vcs, input);
  2538. }
  2539. static int vino_v4l2_enumstd(struct vino_channel_settings *vcs,
  2540. struct v4l2_standard *s)
  2541. {
  2542. int index = s->index;
  2543. int data_norm;
  2544. data_norm = vino_enum_data_norm(vcs, index);
  2545. dprintk("standard index = %d\n", index);
  2546. if (data_norm == VINO_DATA_NORM_NONE)
  2547. return -EINVAL;
  2548. dprintk("standard name = %s\n",
  2549. vino_data_norms[data_norm].description);
  2550. memset(s, 0, sizeof(struct v4l2_standard));
  2551. s->index = index;
  2552. s->id = vino_data_norms[data_norm].std;
  2553. s->frameperiod.numerator = 1;
  2554. s->frameperiod.denominator =
  2555. vino_data_norms[data_norm].fps_max;
  2556. s->framelines =
  2557. vino_data_norms[data_norm].framelines;
  2558. strcpy(s->name,
  2559. vino_data_norms[data_norm].description);
  2560. return 0;
  2561. }
  2562. static int vino_v4l2_querystd(struct vino_channel_settings *vcs,
  2563. v4l2_std_id *std)
  2564. {
  2565. unsigned long flags;
  2566. int err = 0;
  2567. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2568. switch (vcs->input) {
  2569. case VINO_INPUT_D1:
  2570. *std = vino_inputs[vcs->input].std;
  2571. break;
  2572. case VINO_INPUT_COMPOSITE:
  2573. case VINO_INPUT_SVIDEO: {
  2574. struct saa7191_status status;
  2575. i2c_decoder_command(DECODER_SAA7191_GET_STATUS, &status);
  2576. if (status.signal) {
  2577. if (status.signal_60hz) {
  2578. *std = V4L2_STD_NTSC;
  2579. } else {
  2580. *std = V4L2_STD_PAL | V4L2_STD_SECAM;
  2581. }
  2582. } else {
  2583. *std = vino_inputs[vcs->input].std;
  2584. }
  2585. break;
  2586. }
  2587. default:
  2588. err = -EINVAL;
  2589. }
  2590. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2591. return err;
  2592. }
  2593. static int vino_v4l2_g_std(struct vino_channel_settings *vcs,
  2594. v4l2_std_id *std)
  2595. {
  2596. unsigned long flags;
  2597. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2598. *std = vino_data_norms[vcs->data_norm].std;
  2599. dprintk("current standard = %d\n", vcs->data_norm);
  2600. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2601. return 0;
  2602. }
  2603. static int vino_v4l2_s_std(struct vino_channel_settings *vcs,
  2604. v4l2_std_id *std)
  2605. {
  2606. unsigned long flags;
  2607. int ret = 0;
  2608. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2609. if (!vino_is_input_owner(vcs)) {
  2610. ret = -EBUSY;
  2611. goto out;
  2612. }
  2613. /* check if the standard is valid for the current input */
  2614. if ((*std) & vino_inputs[vcs->input].std) {
  2615. dprintk("standard accepted\n");
  2616. /* change the video norm for SAA7191
  2617. * and accept NTSC for D1 (do nothing) */
  2618. if (vcs->input == VINO_INPUT_D1)
  2619. goto out;
  2620. if (((*std) & V4L2_STD_PAL)
  2621. && ((*std) & V4L2_STD_NTSC)
  2622. && ((*std) & V4L2_STD_SECAM)) {
  2623. ret = vino_set_data_norm(vcs, VINO_DATA_NORM_AUTO_EXT,
  2624. &flags);
  2625. } else if ((*std) & V4L2_STD_PAL) {
  2626. ret = vino_set_data_norm(vcs, VINO_DATA_NORM_PAL,
  2627. &flags);
  2628. } else if ((*std) & V4L2_STD_NTSC) {
  2629. ret = vino_set_data_norm(vcs, VINO_DATA_NORM_NTSC,
  2630. &flags);
  2631. } else if ((*std) & V4L2_STD_SECAM) {
  2632. ret = vino_set_data_norm(vcs, VINO_DATA_NORM_SECAM,
  2633. &flags);
  2634. } else {
  2635. ret = -EINVAL;
  2636. }
  2637. if (ret) {
  2638. ret = -EINVAL;
  2639. }
  2640. } else {
  2641. ret = -EINVAL;
  2642. }
  2643. out:
  2644. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2645. return ret;
  2646. }
  2647. static int vino_v4l2_enum_fmt(struct vino_channel_settings *vcs,
  2648. struct v4l2_fmtdesc *fd)
  2649. {
  2650. enum v4l2_buf_type type = fd->type;
  2651. int index = fd->index;
  2652. dprintk("format index = %d\n", index);
  2653. switch (fd->type) {
  2654. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  2655. if ((fd->index < 0) ||
  2656. (fd->index >= VINO_DATA_FMT_COUNT))
  2657. return -EINVAL;
  2658. dprintk("format name = %s\n",
  2659. vino_data_formats[index].description);
  2660. memset(fd, 0, sizeof(struct v4l2_fmtdesc));
  2661. fd->index = index;
  2662. fd->type = type;
  2663. fd->pixelformat = vino_data_formats[index].pixelformat;
  2664. strcpy(fd->description, vino_data_formats[index].description);
  2665. break;
  2666. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2667. default:
  2668. return -EINVAL;
  2669. }
  2670. return 0;
  2671. }
  2672. static int vino_v4l2_try_fmt(struct vino_channel_settings *vcs,
  2673. struct v4l2_format *f)
  2674. {
  2675. struct vino_channel_settings tempvcs;
  2676. unsigned long flags;
  2677. switch (f->type) {
  2678. case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
  2679. struct v4l2_pix_format *pf = &f->fmt.pix;
  2680. dprintk("requested: w = %d, h = %d\n",
  2681. pf->width, pf->height);
  2682. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2683. memcpy(&tempvcs, vcs, sizeof(struct vino_channel_settings));
  2684. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2685. tempvcs.data_format = vino_find_data_format(pf->pixelformat);
  2686. if (tempvcs.data_format == VINO_DATA_FMT_NONE) {
  2687. tempvcs.data_format = VINO_DATA_FMT_GREY;
  2688. pf->pixelformat =
  2689. vino_data_formats[tempvcs.data_format].
  2690. pixelformat;
  2691. }
  2692. /* data format must be set before clipping/scaling */
  2693. vino_set_scaling(&tempvcs, pf->width, pf->height);
  2694. dprintk("data format = %s\n",
  2695. vino_data_formats[tempvcs.data_format].description);
  2696. pf->width = (tempvcs.clipping.right - tempvcs.clipping.left) /
  2697. tempvcs.decimation;
  2698. pf->height = (tempvcs.clipping.bottom - tempvcs.clipping.top) /
  2699. tempvcs.decimation;
  2700. pf->field = V4L2_FIELD_INTERLACED;
  2701. pf->bytesperline = tempvcs.line_size;
  2702. pf->sizeimage = tempvcs.line_size *
  2703. (tempvcs.clipping.bottom - tempvcs.clipping.top) /
  2704. tempvcs.decimation;
  2705. pf->colorspace =
  2706. vino_data_formats[tempvcs.data_format].colorspace;
  2707. pf->priv = 0;
  2708. break;
  2709. }
  2710. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2711. default:
  2712. return -EINVAL;
  2713. }
  2714. return 0;
  2715. }
  2716. static int vino_v4l2_g_fmt(struct vino_channel_settings *vcs,
  2717. struct v4l2_format *f)
  2718. {
  2719. unsigned long flags;
  2720. switch (f->type) {
  2721. case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
  2722. struct v4l2_pix_format *pf = &f->fmt.pix;
  2723. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2724. pf->width = (vcs->clipping.right - vcs->clipping.left) /
  2725. vcs->decimation;
  2726. pf->height = (vcs->clipping.bottom - vcs->clipping.top) /
  2727. vcs->decimation;
  2728. pf->pixelformat =
  2729. vino_data_formats[vcs->data_format].pixelformat;
  2730. pf->field = V4L2_FIELD_INTERLACED;
  2731. pf->bytesperline = vcs->line_size;
  2732. pf->sizeimage = vcs->line_size *
  2733. (vcs->clipping.bottom - vcs->clipping.top) /
  2734. vcs->decimation;
  2735. pf->colorspace =
  2736. vino_data_formats[vcs->data_format].colorspace;
  2737. pf->priv = 0;
  2738. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2739. break;
  2740. }
  2741. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2742. default:
  2743. return -EINVAL;
  2744. }
  2745. return 0;
  2746. }
  2747. static int vino_v4l2_s_fmt(struct vino_channel_settings *vcs,
  2748. struct v4l2_format *f)
  2749. {
  2750. int data_format;
  2751. unsigned long flags;
  2752. switch (f->type) {
  2753. case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
  2754. struct v4l2_pix_format *pf = &f->fmt.pix;
  2755. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2756. data_format = vino_find_data_format(pf->pixelformat);
  2757. if (data_format == VINO_DATA_FMT_NONE) {
  2758. vcs->data_format = VINO_DATA_FMT_GREY;
  2759. pf->pixelformat =
  2760. vino_data_formats[vcs->data_format].
  2761. pixelformat;
  2762. } else {
  2763. vcs->data_format = data_format;
  2764. }
  2765. /* data format must be set before clipping/scaling */
  2766. vino_set_scaling(vcs, pf->width, pf->height);
  2767. dprintk("data format = %s\n",
  2768. vino_data_formats[vcs->data_format].description);
  2769. pf->width = vcs->clipping.right - vcs->clipping.left;
  2770. pf->height = vcs->clipping.bottom - vcs->clipping.top;
  2771. pf->field = V4L2_FIELD_INTERLACED;
  2772. pf->bytesperline = vcs->line_size;
  2773. pf->sizeimage = vcs->line_size *
  2774. (vcs->clipping.bottom - vcs->clipping.top) /
  2775. vcs->decimation;
  2776. pf->colorspace =
  2777. vino_data_formats[vcs->data_format].colorspace;
  2778. pf->priv = 0;
  2779. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2780. break;
  2781. }
  2782. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2783. default:
  2784. return -EINVAL;
  2785. }
  2786. return 0;
  2787. }
  2788. static int vino_v4l2_cropcap(struct vino_channel_settings *vcs,
  2789. struct v4l2_cropcap *ccap)
  2790. {
  2791. const struct vino_data_norm *norm;
  2792. unsigned long flags;
  2793. switch (ccap->type) {
  2794. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  2795. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2796. norm = &vino_data_norms[vcs->data_norm];
  2797. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2798. ccap->bounds.left = 0;
  2799. ccap->bounds.top = 0;
  2800. ccap->bounds.width = norm->width;
  2801. ccap->bounds.height = norm->height;
  2802. memcpy(&ccap->defrect, &ccap->bounds,
  2803. sizeof(struct v4l2_rect));
  2804. ccap->pixelaspect.numerator = 1;
  2805. ccap->pixelaspect.denominator = 1;
  2806. break;
  2807. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2808. default:
  2809. return -EINVAL;
  2810. }
  2811. return 0;
  2812. }
  2813. static int vino_v4l2_g_crop(struct vino_channel_settings *vcs,
  2814. struct v4l2_crop *c)
  2815. {
  2816. unsigned long flags;
  2817. switch (c->type) {
  2818. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  2819. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2820. c->c.left = vcs->clipping.left;
  2821. c->c.top = vcs->clipping.top;
  2822. c->c.width = vcs->clipping.right - vcs->clipping.left;
  2823. c->c.height = vcs->clipping.bottom - vcs->clipping.top;
  2824. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2825. break;
  2826. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2827. default:
  2828. return -EINVAL;
  2829. }
  2830. return 0;
  2831. }
  2832. static int vino_v4l2_s_crop(struct vino_channel_settings *vcs,
  2833. struct v4l2_crop *c)
  2834. {
  2835. unsigned long flags;
  2836. switch (c->type) {
  2837. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  2838. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2839. vino_set_clipping(vcs, c->c.left, c->c.top,
  2840. c->c.width, c->c.height);
  2841. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2842. break;
  2843. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2844. default:
  2845. return -EINVAL;
  2846. }
  2847. return 0;
  2848. }
  2849. static int vino_v4l2_g_parm(struct vino_channel_settings *vcs,
  2850. struct v4l2_streamparm *sp)
  2851. {
  2852. unsigned long flags;
  2853. switch (sp->type) {
  2854. case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
  2855. struct v4l2_captureparm *cp = &sp->parm.capture;
  2856. memset(cp, 0, sizeof(struct v4l2_captureparm));
  2857. cp->capability = V4L2_CAP_TIMEPERFRAME;
  2858. cp->timeperframe.numerator = 1;
  2859. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2860. cp->timeperframe.denominator = vcs->fps;
  2861. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2862. // TODO: cp->readbuffers = xxx;
  2863. break;
  2864. }
  2865. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2866. default:
  2867. return -EINVAL;
  2868. }
  2869. return 0;
  2870. }
  2871. static int vino_v4l2_s_parm(struct vino_channel_settings *vcs,
  2872. struct v4l2_streamparm *sp)
  2873. {
  2874. unsigned long flags;
  2875. switch (sp->type) {
  2876. case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
  2877. struct v4l2_captureparm *cp = &sp->parm.capture;
  2878. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  2879. if ((cp->timeperframe.numerator == 0) ||
  2880. (cp->timeperframe.denominator == 0)) {
  2881. /* reset framerate */
  2882. vino_set_default_framerate(vcs);
  2883. } else {
  2884. vino_set_framerate(vcs, cp->timeperframe.denominator /
  2885. cp->timeperframe.numerator);
  2886. }
  2887. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  2888. // TODO: set buffers according to cp->readbuffers
  2889. break;
  2890. }
  2891. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2892. default:
  2893. return -EINVAL;
  2894. }
  2895. return 0;
  2896. }
  2897. static int vino_v4l2_reqbufs(struct vino_channel_settings *vcs,
  2898. struct v4l2_requestbuffers *rb)
  2899. {
  2900. if (vcs->reading)
  2901. return -EBUSY;
  2902. switch (rb->type) {
  2903. case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
  2904. // TODO: check queue type
  2905. if (rb->memory != V4L2_MEMORY_MMAP) {
  2906. dprintk("type not mmap\n");
  2907. return -EINVAL;
  2908. }
  2909. dprintk("count = %d\n", rb->count);
  2910. if (rb->count > 0) {
  2911. if (vino_is_capturing(vcs)) {
  2912. dprintk("busy, capturing\n");
  2913. return -EBUSY;
  2914. }
  2915. if (vino_queue_has_mapped_buffers(&vcs->fb_queue)) {
  2916. dprintk("busy, buffers still mapped\n");
  2917. return -EBUSY;
  2918. } else {
  2919. vcs->streaming = 0;
  2920. vino_queue_free(&vcs->fb_queue);
  2921. vino_queue_init(&vcs->fb_queue, &rb->count);
  2922. }
  2923. } else {
  2924. vcs->streaming = 0;
  2925. vino_capture_stop(vcs);
  2926. vino_queue_free(&vcs->fb_queue);
  2927. }
  2928. break;
  2929. }
  2930. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2931. default:
  2932. return -EINVAL;
  2933. }
  2934. return 0;
  2935. }
  2936. static void vino_v4l2_get_buffer_status(struct vino_channel_settings *vcs,
  2937. struct vino_framebuffer *fb,
  2938. struct v4l2_buffer *b)
  2939. {
  2940. if (vino_queue_outgoing_contains(&vcs->fb_queue,
  2941. fb->id)) {
  2942. b->flags &= ~V4L2_BUF_FLAG_QUEUED;
  2943. b->flags |= V4L2_BUF_FLAG_DONE;
  2944. } else if (vino_queue_incoming_contains(&vcs->fb_queue,
  2945. fb->id)) {
  2946. b->flags &= ~V4L2_BUF_FLAG_DONE;
  2947. b->flags |= V4L2_BUF_FLAG_QUEUED;
  2948. } else {
  2949. b->flags &= ~(V4L2_BUF_FLAG_DONE |
  2950. V4L2_BUF_FLAG_QUEUED);
  2951. }
  2952. b->flags &= ~(V4L2_BUF_FLAG_TIMECODE);
  2953. if (fb->map_count > 0)
  2954. b->flags |= V4L2_BUF_FLAG_MAPPED;
  2955. b->index = fb->id;
  2956. b->memory = (vcs->fb_queue.type == VINO_MEMORY_MMAP) ?
  2957. V4L2_MEMORY_MMAP : V4L2_MEMORY_USERPTR;
  2958. b->m.offset = fb->offset;
  2959. b->bytesused = fb->data_size;
  2960. b->length = fb->size;
  2961. b->field = V4L2_FIELD_INTERLACED;
  2962. b->sequence = fb->frame_counter;
  2963. memcpy(&b->timestamp, &fb->timestamp,
  2964. sizeof(struct timeval));
  2965. // b->input ?
  2966. dprintk("buffer %d: length = %d, bytesused = %d, offset = %d\n",
  2967. fb->id, fb->size, fb->data_size, fb->offset);
  2968. }
  2969. static int vino_v4l2_querybuf(struct vino_channel_settings *vcs,
  2970. struct v4l2_buffer *b)
  2971. {
  2972. if (vcs->reading)
  2973. return -EBUSY;
  2974. switch (b->type) {
  2975. case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
  2976. struct vino_framebuffer *fb;
  2977. // TODO: check queue type
  2978. if (b->index >= vino_queue_get_length(&vcs->fb_queue)) {
  2979. dprintk("invalid index = %d\n",
  2980. b->index);
  2981. return -EINVAL;
  2982. }
  2983. fb = vino_queue_get_buffer(&vcs->fb_queue,
  2984. b->index);
  2985. if (fb == NULL) {
  2986. dprintk("vino_queue_get_buffer() failed");
  2987. return -EINVAL;
  2988. }
  2989. vino_v4l2_get_buffer_status(vcs, fb, b);
  2990. break;
  2991. }
  2992. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2993. default:
  2994. return -EINVAL;
  2995. }
  2996. return 0;
  2997. }
  2998. static int vino_v4l2_qbuf(struct vino_channel_settings *vcs,
  2999. struct v4l2_buffer *b)
  3000. {
  3001. if (vcs->reading)
  3002. return -EBUSY;
  3003. switch (b->type) {
  3004. case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
  3005. struct vino_framebuffer *fb;
  3006. int ret;
  3007. // TODO: check queue type
  3008. if (b->memory != V4L2_MEMORY_MMAP) {
  3009. dprintk("type not mmap\n");
  3010. return -EINVAL;
  3011. }
  3012. fb = vino_capture_enqueue(vcs, b->index);
  3013. if (fb == NULL)
  3014. return -EINVAL;
  3015. vino_v4l2_get_buffer_status(vcs, fb, b);
  3016. if (vcs->streaming) {
  3017. ret = vino_capture_next(vcs, 1);
  3018. if (ret)
  3019. return ret;
  3020. }
  3021. break;
  3022. }
  3023. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  3024. default:
  3025. return -EINVAL;
  3026. }
  3027. return 0;
  3028. }
  3029. static int vino_v4l2_dqbuf(struct vino_channel_settings *vcs,
  3030. struct v4l2_buffer *b,
  3031. unsigned int nonblocking)
  3032. {
  3033. if (vcs->reading)
  3034. return -EBUSY;
  3035. switch (b->type) {
  3036. case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
  3037. struct vino_framebuffer *fb;
  3038. unsigned int incoming, outgoing;
  3039. int err;
  3040. // TODO: check queue type
  3041. err = vino_queue_get_incoming(&vcs->fb_queue, &incoming);
  3042. if (err) {
  3043. dprintk("vino_queue_get_incoming() failed\n");
  3044. return -EINVAL;
  3045. }
  3046. err = vino_queue_get_outgoing(&vcs->fb_queue, &outgoing);
  3047. if (err) {
  3048. dprintk("vino_queue_get_outgoing() failed\n");
  3049. return -EINVAL;
  3050. }
  3051. dprintk("incoming = %d, outgoing = %d\n", incoming, outgoing);
  3052. if (outgoing == 0) {
  3053. if (incoming == 0) {
  3054. dprintk("no incoming or outgoing buffers\n");
  3055. return -EINVAL;
  3056. }
  3057. if (nonblocking) {
  3058. dprintk("non-blocking I/O was selected and "
  3059. "there are no buffers to dequeue\n");
  3060. return -EAGAIN;
  3061. }
  3062. err = vino_wait_for_frame(vcs);
  3063. if (err) {
  3064. err = vino_wait_for_frame(vcs);
  3065. if (err) {
  3066. /* interrupted or
  3067. * no frames captured because
  3068. * of frame skipping */
  3069. // vino_capture_failed(vcs);
  3070. return -EIO;
  3071. }
  3072. }
  3073. }
  3074. fb = vino_queue_remove(&vcs->fb_queue, &b->index);
  3075. if (fb == NULL) {
  3076. dprintk("vino_queue_remove() failed\n");
  3077. return -EINVAL;
  3078. }
  3079. err = vino_check_buffer(vcs, fb);
  3080. vino_v4l2_get_buffer_status(vcs, fb, b);
  3081. if (err)
  3082. return -EIO;
  3083. break;
  3084. }
  3085. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  3086. default:
  3087. return -EINVAL;
  3088. }
  3089. return 0;
  3090. }
  3091. static int vino_v4l2_streamon(struct vino_channel_settings *vcs)
  3092. {
  3093. unsigned int incoming;
  3094. int ret;
  3095. if (vcs->reading)
  3096. return -EBUSY;
  3097. if (vcs->streaming)
  3098. return 0;
  3099. // TODO: check queue type
  3100. if (vino_queue_get_length(&vcs->fb_queue) < 1) {
  3101. dprintk("no buffers allocated\n");
  3102. return -EINVAL;
  3103. }
  3104. ret = vino_queue_get_incoming(&vcs->fb_queue, &incoming);
  3105. if (ret) {
  3106. dprintk("vino_queue_get_incoming() failed\n");
  3107. return -EINVAL;
  3108. }
  3109. vcs->streaming = 1;
  3110. if (incoming > 0) {
  3111. ret = vino_capture_next(vcs, 1);
  3112. if (ret) {
  3113. vcs->streaming = 0;
  3114. dprintk("couldn't start capture\n");
  3115. return -EINVAL;
  3116. }
  3117. }
  3118. return 0;
  3119. }
  3120. static int vino_v4l2_streamoff(struct vino_channel_settings *vcs)
  3121. {
  3122. if (vcs->reading)
  3123. return -EBUSY;
  3124. if (!vcs->streaming)
  3125. return 0;
  3126. vcs->streaming = 0;
  3127. vino_capture_stop(vcs);
  3128. return 0;
  3129. }
  3130. static int vino_v4l2_queryctrl(struct vino_channel_settings *vcs,
  3131. struct v4l2_queryctrl *queryctrl)
  3132. {
  3133. unsigned long flags;
  3134. int i;
  3135. int err = 0;
  3136. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  3137. switch (vcs->input) {
  3138. case VINO_INPUT_D1:
  3139. for (i = 0; i < VINO_INDYCAM_V4L2_CONTROL_COUNT; i++) {
  3140. if (vino_indycam_v4l2_controls[i].id ==
  3141. queryctrl->id) {
  3142. memcpy(queryctrl,
  3143. &vino_indycam_v4l2_controls[i],
  3144. sizeof(struct v4l2_queryctrl));
  3145. queryctrl->reserved[0] = 0;
  3146. goto found;
  3147. }
  3148. }
  3149. err = -EINVAL;
  3150. break;
  3151. case VINO_INPUT_COMPOSITE:
  3152. case VINO_INPUT_SVIDEO:
  3153. for (i = 0; i < VINO_SAA7191_V4L2_CONTROL_COUNT; i++) {
  3154. if (vino_saa7191_v4l2_controls[i].id ==
  3155. queryctrl->id) {
  3156. memcpy(queryctrl,
  3157. &vino_saa7191_v4l2_controls[i],
  3158. sizeof(struct v4l2_queryctrl));
  3159. queryctrl->reserved[0] = 0;
  3160. goto found;
  3161. }
  3162. }
  3163. err = -EINVAL;
  3164. break;
  3165. default:
  3166. err = -EINVAL;
  3167. }
  3168. found:
  3169. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  3170. return err;
  3171. }
  3172. static int vino_v4l2_g_ctrl(struct vino_channel_settings *vcs,
  3173. struct v4l2_control *control)
  3174. {
  3175. unsigned long flags;
  3176. int i;
  3177. int err = 0;
  3178. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  3179. switch (vcs->input) {
  3180. case VINO_INPUT_D1: {
  3181. struct indycam_control indycam_ctrl;
  3182. for (i = 0; i < VINO_INDYCAM_V4L2_CONTROL_COUNT; i++) {
  3183. if (vino_indycam_v4l2_controls[i].id ==
  3184. control->id) {
  3185. goto found1;
  3186. }
  3187. }
  3188. err = -EINVAL;
  3189. goto out;
  3190. found1:
  3191. indycam_ctrl.type = vino_indycam_v4l2_controls[i].reserved[0];
  3192. err = i2c_camera_command(DECODER_INDYCAM_GET_CONTROL,
  3193. &indycam_ctrl);
  3194. if (err) {
  3195. err = -EINVAL;
  3196. goto out;
  3197. }
  3198. control->value = indycam_ctrl.value;
  3199. break;
  3200. }
  3201. case VINO_INPUT_COMPOSITE:
  3202. case VINO_INPUT_SVIDEO: {
  3203. struct saa7191_control saa7191_ctrl;
  3204. for (i = 0; i < VINO_SAA7191_V4L2_CONTROL_COUNT; i++) {
  3205. if (vino_saa7191_v4l2_controls[i].id ==
  3206. control->id) {
  3207. goto found2;
  3208. }
  3209. }
  3210. err = -EINVAL;
  3211. goto out;
  3212. found2:
  3213. saa7191_ctrl.type = vino_saa7191_v4l2_controls[i].reserved[0];
  3214. err = i2c_decoder_command(DECODER_SAA7191_GET_CONTROL,
  3215. &saa7191_ctrl);
  3216. if (err) {
  3217. err = -EINVAL;
  3218. goto out;
  3219. }
  3220. control->value = saa7191_ctrl.value;
  3221. break;
  3222. }
  3223. default:
  3224. err = -EINVAL;
  3225. }
  3226. out:
  3227. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  3228. return err;
  3229. }
  3230. static int vino_v4l2_s_ctrl(struct vino_channel_settings *vcs,
  3231. struct v4l2_control *control)
  3232. {
  3233. unsigned long flags;
  3234. int i;
  3235. int err = 0;
  3236. spin_lock_irqsave(&vino_drvdata->input_lock, flags);
  3237. if (!vino_is_input_owner(vcs)) {
  3238. err = -EBUSY;
  3239. goto out;
  3240. }
  3241. switch (vcs->input) {
  3242. case VINO_INPUT_D1: {
  3243. struct indycam_control indycam_ctrl;
  3244. for (i = 0; i < VINO_INDYCAM_V4L2_CONTROL_COUNT; i++) {
  3245. if (vino_indycam_v4l2_controls[i].id ==
  3246. control->id) {
  3247. if ((control->value >=
  3248. vino_indycam_v4l2_controls[i].minimum)
  3249. && (control->value <=
  3250. vino_indycam_v4l2_controls[i].
  3251. maximum)) {
  3252. goto found1;
  3253. } else {
  3254. err = -ERANGE;
  3255. goto out;
  3256. }
  3257. }
  3258. }
  3259. err = -EINVAL;
  3260. goto out;
  3261. found1:
  3262. indycam_ctrl.type = vino_indycam_v4l2_controls[i].reserved[0];
  3263. indycam_ctrl.value = control->value;
  3264. err = i2c_camera_command(DECODER_INDYCAM_SET_CONTROL,
  3265. &indycam_ctrl);
  3266. if (err)
  3267. err = -EINVAL;
  3268. break;
  3269. }
  3270. case VINO_INPUT_COMPOSITE:
  3271. case VINO_INPUT_SVIDEO: {
  3272. struct saa7191_control saa7191_ctrl;
  3273. for (i = 0; i < VINO_SAA7191_V4L2_CONTROL_COUNT; i++) {
  3274. if (vino_saa7191_v4l2_controls[i].id ==
  3275. control->id) {
  3276. if ((control->value >=
  3277. vino_saa7191_v4l2_controls[i].minimum)
  3278. && (control->value <=
  3279. vino_saa7191_v4l2_controls[i].
  3280. maximum)) {
  3281. goto found2;
  3282. } else {
  3283. err = -ERANGE;
  3284. goto out;
  3285. }
  3286. }
  3287. }
  3288. err = -EINVAL;
  3289. goto out;
  3290. found2:
  3291. saa7191_ctrl.type = vino_saa7191_v4l2_controls[i].reserved[0];
  3292. saa7191_ctrl.value = control->value;
  3293. err = i2c_decoder_command(DECODER_SAA7191_SET_CONTROL,
  3294. &saa7191_ctrl);
  3295. if (err)
  3296. err = -EINVAL;
  3297. break;
  3298. }
  3299. default:
  3300. err = -EINVAL;
  3301. }
  3302. out:
  3303. spin_unlock_irqrestore(&vino_drvdata->input_lock, flags);
  3304. return err;
  3305. }
  3306. /* File operations */
  3307. static int vino_open(struct inode *inode, struct file *file)
  3308. {
  3309. struct video_device *dev = video_devdata(file);
  3310. struct vino_channel_settings *vcs = video_get_drvdata(dev);
  3311. int ret = 0;
  3312. dprintk("open(): channel = %c\n",
  3313. (vcs->channel == VINO_CHANNEL_A) ? 'A' : 'B');
  3314. mutex_lock(&vcs->mutex);
  3315. if (vcs->users) {
  3316. dprintk("open(): driver busy\n");
  3317. ret = -EBUSY;
  3318. goto out;
  3319. }
  3320. ret = vino_acquire_input(vcs);
  3321. if (ret) {
  3322. dprintk("open(): vino_acquire_input() failed\n");
  3323. goto out;
  3324. }
  3325. vcs->users++;
  3326. out:
  3327. mutex_unlock(&vcs->mutex);
  3328. dprintk("open(): %s!\n", ret ? "failed" : "complete");
  3329. return ret;
  3330. }
  3331. static int vino_close(struct inode *inode, struct file *file)
  3332. {
  3333. struct video_device *dev = video_devdata(file);
  3334. struct vino_channel_settings *vcs = video_get_drvdata(dev);
  3335. dprintk("close():\n");
  3336. mutex_lock(&vcs->mutex);
  3337. vcs->users--;
  3338. if (!vcs->users) {
  3339. vino_release_input(vcs);
  3340. /* stop DMA and free buffers */
  3341. vino_capture_stop(vcs);
  3342. vino_queue_free(&vcs->fb_queue);
  3343. }
  3344. mutex_unlock(&vcs->mutex);
  3345. return 0;
  3346. }
  3347. static void vino_vm_open(struct vm_area_struct *vma)
  3348. {
  3349. struct vino_framebuffer *fb = vma->vm_private_data;
  3350. fb->map_count++;
  3351. dprintk("vino_vm_open(): count = %d\n", fb->map_count);
  3352. }
  3353. static void vino_vm_close(struct vm_area_struct *vma)
  3354. {
  3355. struct vino_framebuffer *fb = vma->vm_private_data;
  3356. fb->map_count--;
  3357. dprintk("vino_vm_close(): count = %d\n", fb->map_count);
  3358. }
  3359. static struct vm_operations_struct vino_vm_ops = {
  3360. .open = vino_vm_open,
  3361. .close = vino_vm_close,
  3362. };
  3363. static int vino_mmap(struct file *file, struct vm_area_struct *vma)
  3364. {
  3365. struct video_device *dev = video_devdata(file);
  3366. struct vino_channel_settings *vcs = video_get_drvdata(dev);
  3367. unsigned long start = vma->vm_start;
  3368. unsigned long size = vma->vm_end - vma->vm_start;
  3369. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  3370. struct vino_framebuffer *fb = NULL;
  3371. unsigned int i, length;
  3372. int ret = 0;
  3373. dprintk("mmap():\n");
  3374. // TODO: reject mmap if already mapped
  3375. if (mutex_lock_interruptible(&vcs->mutex))
  3376. return -EINTR;
  3377. if (vcs->reading) {
  3378. ret = -EBUSY;
  3379. goto out;
  3380. }
  3381. // TODO: check queue type
  3382. if (!(vma->vm_flags & VM_WRITE)) {
  3383. dprintk("mmap(): app bug: PROT_WRITE please\n");
  3384. ret = -EINVAL;
  3385. goto out;
  3386. }
  3387. if (!(vma->vm_flags & VM_SHARED)) {
  3388. dprintk("mmap(): app bug: MAP_SHARED please\n");
  3389. ret = -EINVAL;
  3390. goto out;
  3391. }
  3392. /* find the correct buffer using offset */
  3393. length = vino_queue_get_length(&vcs->fb_queue);
  3394. if (length == 0) {
  3395. dprintk("mmap(): queue not initialized\n");
  3396. ret = -EINVAL;
  3397. goto out;
  3398. }
  3399. for (i = 0; i < length; i++) {
  3400. fb = vino_queue_get_buffer(&vcs->fb_queue, i);
  3401. if (fb == NULL) {
  3402. dprintk("mmap(): vino_queue_get_buffer() failed\n");
  3403. ret = -EINVAL;
  3404. goto out;
  3405. }
  3406. if (fb->offset == offset)
  3407. goto found;
  3408. }
  3409. dprintk("mmap(): invalid offset = %lu\n", offset);
  3410. ret = -EINVAL;
  3411. goto out;
  3412. found:
  3413. dprintk("mmap(): buffer = %d\n", i);
  3414. if (size > (fb->desc_table.page_count * PAGE_SIZE)) {
  3415. dprintk("mmap(): failed: size = %lu > %lu\n",
  3416. size, fb->desc_table.page_count * PAGE_SIZE);
  3417. ret = -EINVAL;
  3418. goto out;
  3419. }
  3420. for (i = 0; i < fb->desc_table.page_count; i++) {
  3421. unsigned long pfn =
  3422. virt_to_phys((void *)fb->desc_table.virtual[i]) >>
  3423. PAGE_SHIFT;
  3424. if (size < PAGE_SIZE)
  3425. break;
  3426. // protection was: PAGE_READONLY
  3427. if (remap_pfn_range(vma, start, pfn, PAGE_SIZE,
  3428. vma->vm_page_prot)) {
  3429. dprintk("mmap(): remap_pfn_range() failed\n");
  3430. ret = -EAGAIN;
  3431. goto out;
  3432. }
  3433. start += PAGE_SIZE;
  3434. size -= PAGE_SIZE;
  3435. }
  3436. fb->map_count = 1;
  3437. vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED;
  3438. vma->vm_flags &= ~VM_IO;
  3439. vma->vm_private_data = fb;
  3440. vma->vm_file = file;
  3441. vma->vm_ops = &vino_vm_ops;
  3442. out:
  3443. mutex_unlock(&vcs->mutex);
  3444. return ret;
  3445. }
  3446. static unsigned int vino_poll(struct file *file, poll_table *pt)
  3447. {
  3448. struct video_device *dev = video_devdata(file);
  3449. struct vino_channel_settings *vcs = video_get_drvdata(dev);
  3450. unsigned int outgoing;
  3451. unsigned int ret = 0;
  3452. // lock mutex (?)
  3453. // TODO: this has to be corrected for different read modes
  3454. dprintk("poll():\n");
  3455. if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
  3456. dprintk("poll(): vino_queue_get_outgoing() failed\n");
  3457. ret = POLLERR;
  3458. goto error;
  3459. }
  3460. if (outgoing > 0)
  3461. goto over;
  3462. poll_wait(file, &vcs->fb_queue.frame_wait_queue, pt);
  3463. if (vino_queue_get_outgoing(&vcs->fb_queue, &outgoing)) {
  3464. dprintk("poll(): vino_queue_get_outgoing() failed\n");
  3465. ret = POLLERR;
  3466. goto error;
  3467. }
  3468. over:
  3469. dprintk("poll(): data %savailable\n",
  3470. (outgoing > 0) ? "" : "not ");
  3471. if (outgoing > 0)
  3472. ret = POLLIN | POLLRDNORM;
  3473. error:
  3474. return ret;
  3475. }
  3476. static int vino_do_ioctl(struct inode *inode, struct file *file,
  3477. unsigned int cmd, void *arg)
  3478. {
  3479. struct video_device *dev = video_devdata(file);
  3480. struct vino_channel_settings *vcs = video_get_drvdata(dev);
  3481. #ifdef VINO_DEBUG
  3482. switch (_IOC_TYPE(cmd)) {
  3483. case 'v':
  3484. dprintk("ioctl(): V4L1 unsupported (0x%08x)\n", cmd);
  3485. break;
  3486. case 'V':
  3487. dprintk("ioctl(): V4L2 %s (0x%08x)\n",
  3488. v4l2_ioctl_names[_IOC_NR(cmd)], cmd);
  3489. break;
  3490. default:
  3491. dprintk("ioctl(): unsupported command 0x%08x\n", cmd);
  3492. }
  3493. #endif
  3494. switch (cmd) {
  3495. /* V4L2 interface */
  3496. case VIDIOC_QUERYCAP: {
  3497. vino_v4l2_querycap(arg);
  3498. break;
  3499. }
  3500. case VIDIOC_ENUMINPUT: {
  3501. return vino_v4l2_enuminput(vcs, arg);
  3502. }
  3503. case VIDIOC_G_INPUT: {
  3504. return vino_v4l2_g_input(vcs, arg);
  3505. }
  3506. case VIDIOC_S_INPUT: {
  3507. return vino_v4l2_s_input(vcs, arg);
  3508. }
  3509. case VIDIOC_ENUMSTD: {
  3510. return vino_v4l2_enumstd(vcs, arg);
  3511. }
  3512. case VIDIOC_QUERYSTD: {
  3513. return vino_v4l2_querystd(vcs, arg);
  3514. }
  3515. case VIDIOC_G_STD: {
  3516. return vino_v4l2_g_std(vcs, arg);
  3517. }
  3518. case VIDIOC_S_STD: {
  3519. return vino_v4l2_s_std(vcs, arg);
  3520. }
  3521. case VIDIOC_ENUM_FMT: {
  3522. return vino_v4l2_enum_fmt(vcs, arg);
  3523. }
  3524. case VIDIOC_TRY_FMT: {
  3525. return vino_v4l2_try_fmt(vcs, arg);
  3526. }
  3527. case VIDIOC_G_FMT: {
  3528. return vino_v4l2_g_fmt(vcs, arg);
  3529. }
  3530. case VIDIOC_S_FMT: {
  3531. return vino_v4l2_s_fmt(vcs, arg);
  3532. }
  3533. case VIDIOC_CROPCAP: {
  3534. return vino_v4l2_cropcap(vcs, arg);
  3535. }
  3536. case VIDIOC_G_CROP: {
  3537. return vino_v4l2_g_crop(vcs, arg);
  3538. }
  3539. case VIDIOC_S_CROP: {
  3540. return vino_v4l2_s_crop(vcs, arg);
  3541. }
  3542. case VIDIOC_G_PARM: {
  3543. return vino_v4l2_g_parm(vcs, arg);
  3544. }
  3545. case VIDIOC_S_PARM: {
  3546. return vino_v4l2_s_parm(vcs, arg);
  3547. }
  3548. case VIDIOC_REQBUFS: {
  3549. return vino_v4l2_reqbufs(vcs, arg);
  3550. }
  3551. case VIDIOC_QUERYBUF: {
  3552. return vino_v4l2_querybuf(vcs, arg);
  3553. }
  3554. case VIDIOC_QBUF: {
  3555. return vino_v4l2_qbuf(vcs, arg);
  3556. }
  3557. case VIDIOC_DQBUF: {
  3558. return vino_v4l2_dqbuf(vcs, arg, file->f_flags & O_NONBLOCK);
  3559. }
  3560. case VIDIOC_STREAMON: {
  3561. return vino_v4l2_streamon(vcs);
  3562. }
  3563. case VIDIOC_STREAMOFF: {
  3564. return vino_v4l2_streamoff(vcs);
  3565. }
  3566. case VIDIOC_QUERYCTRL: {
  3567. return vino_v4l2_queryctrl(vcs, arg);
  3568. }
  3569. case VIDIOC_G_CTRL: {
  3570. return vino_v4l2_g_ctrl(vcs, arg);
  3571. }
  3572. case VIDIOC_S_CTRL: {
  3573. return vino_v4l2_s_ctrl(vcs, arg);
  3574. }
  3575. default:
  3576. return -ENOIOCTLCMD;
  3577. }
  3578. return 0;
  3579. }
  3580. static int vino_ioctl(struct inode *inode, struct file *file,
  3581. unsigned int cmd, unsigned long arg)
  3582. {
  3583. struct video_device *dev = video_devdata(file);
  3584. struct vino_channel_settings *vcs = video_get_drvdata(dev);
  3585. int ret;
  3586. if (mutex_lock_interruptible(&vcs->mutex))
  3587. return -EINTR;
  3588. ret = video_usercopy(inode, file, cmd, arg, vino_do_ioctl);
  3589. mutex_unlock(&vcs->mutex);
  3590. return ret;
  3591. }
  3592. /* Initialization and cleanup */
  3593. // __initdata
  3594. static int vino_init_stage = 0;
  3595. static const struct file_operations vino_fops = {
  3596. .owner = THIS_MODULE,
  3597. .open = vino_open,
  3598. .release = vino_close,
  3599. .ioctl = vino_ioctl,
  3600. .mmap = vino_mmap,
  3601. .poll = vino_poll,
  3602. .llseek = no_llseek,
  3603. };
  3604. static struct video_device v4l_device_template = {
  3605. .name = "NOT SET",
  3606. //.type = VID_TYPE_CAPTURE | VID_TYPE_SUBCAPTURE |
  3607. // VID_TYPE_CLIPPING | VID_TYPE_SCALES, VID_TYPE_OVERLAY
  3608. .fops = &vino_fops,
  3609. .minor = -1,
  3610. };
  3611. static void vino_module_cleanup(int stage)
  3612. {
  3613. switch(stage) {
  3614. case 10:
  3615. video_unregister_device(vino_drvdata->b.v4l_device);
  3616. vino_drvdata->b.v4l_device = NULL;
  3617. case 9:
  3618. video_unregister_device(vino_drvdata->a.v4l_device);
  3619. vino_drvdata->a.v4l_device = NULL;
  3620. case 8:
  3621. vino_i2c_del_bus();
  3622. case 7:
  3623. free_irq(SGI_VINO_IRQ, NULL);
  3624. case 6:
  3625. if (vino_drvdata->b.v4l_device) {
  3626. video_device_release(vino_drvdata->b.v4l_device);
  3627. vino_drvdata->b.v4l_device = NULL;
  3628. }
  3629. case 5:
  3630. if (vino_drvdata->a.v4l_device) {
  3631. video_device_release(vino_drvdata->a.v4l_device);
  3632. vino_drvdata->a.v4l_device = NULL;
  3633. }
  3634. case 4:
  3635. /* all entries in dma_cpu dummy table have the same address */
  3636. dma_unmap_single(NULL,
  3637. vino_drvdata->dummy_desc_table.dma_cpu[0],
  3638. PAGE_SIZE, DMA_FROM_DEVICE);
  3639. dma_free_coherent(NULL, VINO_DUMMY_DESC_COUNT
  3640. * sizeof(dma_addr_t),
  3641. (void *)vino_drvdata->
  3642. dummy_desc_table.dma_cpu,
  3643. vino_drvdata->dummy_desc_table.dma);
  3644. case 3:
  3645. free_page(vino_drvdata->dummy_page);
  3646. case 2:
  3647. kfree(vino_drvdata);
  3648. case 1:
  3649. iounmap(vino);
  3650. case 0:
  3651. break;
  3652. default:
  3653. dprintk("vino_module_cleanup(): invalid cleanup stage = %d\n",
  3654. stage);
  3655. }
  3656. }
  3657. static int vino_probe(void)
  3658. {
  3659. unsigned long rev_id;
  3660. if (ip22_is_fullhouse()) {
  3661. printk(KERN_ERR "VINO doesn't exist in IP22 Fullhouse\n");
  3662. return -ENODEV;
  3663. }
  3664. if (!(sgimc->systemid & SGIMC_SYSID_EPRESENT)) {
  3665. printk(KERN_ERR "VINO is not found (EISA BUS not present)\n");
  3666. return -ENODEV;
  3667. }
  3668. vino = (struct sgi_vino *)ioremap(VINO_BASE, sizeof(struct sgi_vino));
  3669. if (!vino) {
  3670. printk(KERN_ERR "VINO: ioremap() failed\n");
  3671. return -EIO;
  3672. }
  3673. vino_init_stage++;
  3674. if (get_dbe(rev_id, &(vino->rev_id))) {
  3675. printk(KERN_ERR "Failed to read VINO revision register\n");
  3676. vino_module_cleanup(vino_init_stage);
  3677. return -ENODEV;
  3678. }
  3679. if (VINO_ID_VALUE(rev_id) != VINO_CHIP_ID) {
  3680. printk(KERN_ERR "Unknown VINO chip ID (Rev/ID: 0x%02lx)\n",
  3681. rev_id);
  3682. vino_module_cleanup(vino_init_stage);
  3683. return -ENODEV;
  3684. }
  3685. printk(KERN_INFO "VINO revision %ld found\n", VINO_REV_NUM(rev_id));
  3686. return 0;
  3687. }
  3688. static int vino_init(void)
  3689. {
  3690. dma_addr_t dma_dummy_address;
  3691. int i;
  3692. vino_drvdata = kzalloc(sizeof(struct vino_settings), GFP_KERNEL);
  3693. if (!vino_drvdata) {
  3694. vino_module_cleanup(vino_init_stage);
  3695. return -ENOMEM;
  3696. }
  3697. vino_init_stage++;
  3698. /* create a dummy dma descriptor */
  3699. vino_drvdata->dummy_page = get_zeroed_page(GFP_KERNEL | GFP_DMA);
  3700. if (!vino_drvdata->dummy_page) {
  3701. vino_module_cleanup(vino_init_stage);
  3702. return -ENOMEM;
  3703. }
  3704. vino_init_stage++;
  3705. // TODO: use page_count in dummy_desc_table
  3706. vino_drvdata->dummy_desc_table.dma_cpu =
  3707. dma_alloc_coherent(NULL,
  3708. VINO_DUMMY_DESC_COUNT * sizeof(dma_addr_t),
  3709. &vino_drvdata->dummy_desc_table.dma,
  3710. GFP_KERNEL | GFP_DMA);
  3711. if (!vino_drvdata->dummy_desc_table.dma_cpu) {
  3712. vino_module_cleanup(vino_init_stage);
  3713. return -ENOMEM;
  3714. }
  3715. vino_init_stage++;
  3716. dma_dummy_address = dma_map_single(NULL,
  3717. (void *)vino_drvdata->dummy_page,
  3718. PAGE_SIZE, DMA_FROM_DEVICE);
  3719. for (i = 0; i < VINO_DUMMY_DESC_COUNT; i++) {
  3720. vino_drvdata->dummy_desc_table.dma_cpu[i] = dma_dummy_address;
  3721. }
  3722. /* initialize VINO */
  3723. vino->control = 0;
  3724. vino->a.next_4_desc = vino_drvdata->dummy_desc_table.dma;
  3725. vino->b.next_4_desc = vino_drvdata->dummy_desc_table.dma;
  3726. udelay(VINO_DESC_FETCH_DELAY);
  3727. vino->intr_status = 0;
  3728. vino->a.fifo_thres = VINO_FIFO_THRESHOLD_DEFAULT;
  3729. vino->b.fifo_thres = VINO_FIFO_THRESHOLD_DEFAULT;
  3730. return 0;
  3731. }
  3732. static int vino_init_channel_settings(struct vino_channel_settings *vcs,
  3733. unsigned int channel, const char *name)
  3734. {
  3735. vcs->channel = channel;
  3736. vcs->input = VINO_INPUT_NONE;
  3737. vcs->alpha = 0;
  3738. vcs->users = 0;
  3739. vcs->data_format = VINO_DATA_FMT_GREY;
  3740. vcs->data_norm = VINO_DATA_NORM_NTSC;
  3741. vcs->decimation = 1;
  3742. vino_set_default_clipping(vcs);
  3743. vino_set_default_framerate(vcs);
  3744. vcs->capturing = 0;
  3745. mutex_init(&vcs->mutex);
  3746. spin_lock_init(&vcs->capture_lock);
  3747. mutex_init(&vcs->fb_queue.queue_mutex);
  3748. spin_lock_init(&vcs->fb_queue.queue_lock);
  3749. init_waitqueue_head(&vcs->fb_queue.frame_wait_queue);
  3750. vcs->v4l_device = video_device_alloc();
  3751. if (!vcs->v4l_device) {
  3752. vino_module_cleanup(vino_init_stage);
  3753. return -ENOMEM;
  3754. }
  3755. vino_init_stage++;
  3756. memcpy(vcs->v4l_device, &v4l_device_template,
  3757. sizeof(struct video_device));
  3758. strcpy(vcs->v4l_device->name, name);
  3759. vcs->v4l_device->release = video_device_release;
  3760. video_set_drvdata(vcs->v4l_device, vcs);
  3761. return 0;
  3762. }
  3763. static int __init vino_module_init(void)
  3764. {
  3765. int ret;
  3766. printk(KERN_INFO "SGI VINO driver version %s\n",
  3767. VINO_MODULE_VERSION);
  3768. ret = vino_probe();
  3769. if (ret)
  3770. return ret;
  3771. ret = vino_init();
  3772. if (ret)
  3773. return ret;
  3774. /* initialize data structures */
  3775. spin_lock_init(&vino_drvdata->vino_lock);
  3776. spin_lock_init(&vino_drvdata->input_lock);
  3777. ret = vino_init_channel_settings(&vino_drvdata->a, VINO_CHANNEL_A,
  3778. vino_v4l_device_name_a);
  3779. if (ret)
  3780. return ret;
  3781. ret = vino_init_channel_settings(&vino_drvdata->b, VINO_CHANNEL_B,
  3782. vino_v4l_device_name_b);
  3783. if (ret)
  3784. return ret;
  3785. /* initialize hardware and register V4L devices */
  3786. ret = request_irq(SGI_VINO_IRQ, vino_interrupt, 0,
  3787. vino_driver_description, NULL);
  3788. if (ret) {
  3789. printk(KERN_ERR "VINO: requesting IRQ %02d failed\n",
  3790. SGI_VINO_IRQ);
  3791. vino_module_cleanup(vino_init_stage);
  3792. return -EAGAIN;
  3793. }
  3794. vino_init_stage++;
  3795. ret = vino_i2c_add_bus();
  3796. if (ret) {
  3797. printk(KERN_ERR "VINO I2C bus registration failed\n");
  3798. vino_module_cleanup(vino_init_stage);
  3799. return ret;
  3800. }
  3801. vino_init_stage++;
  3802. ret = video_register_device(vino_drvdata->a.v4l_device,
  3803. VFL_TYPE_GRABBER, -1);
  3804. if (ret < 0) {
  3805. printk(KERN_ERR "VINO channel A Video4Linux-device "
  3806. "registration failed\n");
  3807. vino_module_cleanup(vino_init_stage);
  3808. return -EINVAL;
  3809. }
  3810. vino_init_stage++;
  3811. ret = video_register_device(vino_drvdata->b.v4l_device,
  3812. VFL_TYPE_GRABBER, -1);
  3813. if (ret < 0) {
  3814. printk(KERN_ERR "VINO channel B Video4Linux-device "
  3815. "registration failed\n");
  3816. vino_module_cleanup(vino_init_stage);
  3817. return -EINVAL;
  3818. }
  3819. vino_init_stage++;
  3820. #if defined(CONFIG_KMOD) && defined(MODULE)
  3821. request_module("saa7191");
  3822. request_module("indycam");
  3823. #endif
  3824. dprintk("init complete!\n");
  3825. return 0;
  3826. }
  3827. static void __exit vino_module_exit(void)
  3828. {
  3829. dprintk("exiting, stage = %d ...\n", vino_init_stage);
  3830. vino_module_cleanup(vino_init_stage);
  3831. dprintk("cleanup complete, exit!\n");
  3832. }
  3833. module_init(vino_module_init);
  3834. module_exit(vino_module_exit);