vino.c 104 KB

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