vino.c 99 KB

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