vino.c 104 KB

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