vino.c 99 KB

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