vino.c 99 KB

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