vino.c 100 KB

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