vino.c 104 KB

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