vino.c 98 KB

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