vino.c 99 KB

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