vino.c 96 KB

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