vino.c 105 KB

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