zoran_driver.c 113 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699
  1. /*
  2. * Zoran zr36057/zr36067 PCI controller driver, for the
  3. * Pinnacle/Miro DC10/DC10+/DC30/DC30+, Iomega Buz, Linux
  4. * Media Labs LML33/LML33R10.
  5. *
  6. * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
  7. *
  8. * Changes for BUZ by Wolfgang Scherr <scherr@net4you.net>
  9. *
  10. * Changes for DC10/DC30 by Laurent Pinchart <laurent.pinchart@skynet.be>
  11. *
  12. * Changes for LML33R10 by Maxim Yevtyushkin <max@linuxmedialabs.com>
  13. *
  14. * Changes for videodev2/v4l2 by Ronald Bultje <rbultje@ronald.bitfreak.net>
  15. *
  16. * Based on
  17. *
  18. * Miro DC10 driver
  19. * Copyright (C) 1999 Wolfgang Scherr <scherr@net4you.net>
  20. *
  21. * Iomega Buz driver version 1.0
  22. * Copyright (C) 1999 Rainer Johanni <Rainer@Johanni.de>
  23. *
  24. * buz.0.0.3
  25. * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
  26. *
  27. * bttv - Bt848 frame grabber driver
  28. * Copyright (C) 1996,97,98 Ralph Metzler (rjkm@thp.uni-koeln.de)
  29. * & Marcus Metzler (mocm@thp.uni-koeln.de)
  30. *
  31. *
  32. * This program is free software; you can redistribute it and/or modify
  33. * it under the terms of the GNU General Public License as published by
  34. * the Free Software Foundation; either version 2 of the License, or
  35. * (at your option) any later version.
  36. *
  37. * This program is distributed in the hope that it will be useful,
  38. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  39. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  40. * GNU General Public License for more details.
  41. *
  42. * You should have received a copy of the GNU General Public License
  43. * along with this program; if not, write to the Free Software
  44. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  45. */
  46. #include <linux/config.h>
  47. #include <linux/version.h>
  48. #include <linux/init.h>
  49. #include <linux/module.h>
  50. #include <linux/delay.h>
  51. #include <linux/slab.h>
  52. #include <linux/pci.h>
  53. #include <linux/vmalloc.h>
  54. #include <linux/wait.h>
  55. #include <linux/byteorder/generic.h>
  56. #include <linux/interrupt.h>
  57. #include <linux/i2c.h>
  58. #include <linux/i2c-algo-bit.h>
  59. #include <linux/spinlock.h>
  60. #define MAP_NR(x) virt_to_page(x)
  61. #define ZORAN_HARDWARE VID_HARDWARE_ZR36067
  62. #define ZORAN_VID_TYPE ( \
  63. VID_TYPE_CAPTURE | \
  64. VID_TYPE_OVERLAY | \
  65. VID_TYPE_CLIPPING | \
  66. VID_TYPE_FRAMERAM | \
  67. VID_TYPE_SCALES | \
  68. VID_TYPE_MJPEG_DECODER | \
  69. VID_TYPE_MJPEG_ENCODER \
  70. )
  71. #include <linux/videodev.h>
  72. #include "videocodec.h"
  73. #include <asm/io.h>
  74. #include <asm/uaccess.h>
  75. #include <linux/proc_fs.h>
  76. #include <linux/video_decoder.h>
  77. #include <linux/video_encoder.h>
  78. #include "zoran.h"
  79. #include "zoran_device.h"
  80. #include "zoran_card.h"
  81. #ifdef HAVE_V4L2
  82. /* we declare some card type definitions here, they mean
  83. * the same as the v4l1 ZORAN_VID_TYPE above, except it's v4l2 */
  84. #define ZORAN_V4L2_VID_FLAGS ( \
  85. V4L2_CAP_STREAMING |\
  86. V4L2_CAP_VIDEO_CAPTURE |\
  87. V4L2_CAP_VIDEO_OUTPUT |\
  88. V4L2_CAP_VIDEO_OVERLAY \
  89. )
  90. #endif
  91. #include <asm/byteorder.h>
  92. const struct zoran_format zoran_formats[] = {
  93. {
  94. .name = "15-bit RGB",
  95. .palette = VIDEO_PALETTE_RGB555,
  96. #ifdef HAVE_V4L2
  97. #ifdef __LITTLE_ENDIAN
  98. .fourcc = V4L2_PIX_FMT_RGB555,
  99. #else
  100. .fourcc = V4L2_PIX_FMT_RGB555X,
  101. #endif
  102. .colorspace = V4L2_COLORSPACE_SRGB,
  103. #endif
  104. .depth = 15,
  105. .flags = ZORAN_FORMAT_CAPTURE |
  106. ZORAN_FORMAT_OVERLAY,
  107. }, {
  108. .name = "16-bit RGB",
  109. .palette = VIDEO_PALETTE_RGB565,
  110. #ifdef HAVE_V4L2
  111. #ifdef __LITTLE_ENDIAN
  112. .fourcc = V4L2_PIX_FMT_RGB565,
  113. #else
  114. .fourcc = V4L2_PIX_FMT_RGB565X,
  115. #endif
  116. .colorspace = V4L2_COLORSPACE_SRGB,
  117. #endif
  118. .depth = 16,
  119. .flags = ZORAN_FORMAT_CAPTURE |
  120. ZORAN_FORMAT_OVERLAY,
  121. }, {
  122. .name = "24-bit RGB",
  123. .palette = VIDEO_PALETTE_RGB24,
  124. #ifdef HAVE_V4L2
  125. #ifdef __LITTLE_ENDIAN
  126. .fourcc = V4L2_PIX_FMT_BGR24,
  127. #else
  128. .fourcc = V4L2_PIX_FMT_RGB24,
  129. #endif
  130. .colorspace = V4L2_COLORSPACE_SRGB,
  131. #endif
  132. .depth = 24,
  133. .flags = ZORAN_FORMAT_CAPTURE |
  134. ZORAN_FORMAT_OVERLAY,
  135. }, {
  136. .name = "32-bit RGB",
  137. .palette = VIDEO_PALETTE_RGB32,
  138. #ifdef HAVE_V4L2
  139. #ifdef __LITTLE_ENDIAN
  140. .fourcc = V4L2_PIX_FMT_BGR32,
  141. #else
  142. .fourcc = V4L2_PIX_FMT_RGB32,
  143. #endif
  144. .colorspace = V4L2_COLORSPACE_SRGB,
  145. #endif
  146. .depth = 32,
  147. .flags = ZORAN_FORMAT_CAPTURE |
  148. ZORAN_FORMAT_OVERLAY,
  149. }, {
  150. .name = "4:2:2, packed, YUYV",
  151. .palette = VIDEO_PALETTE_YUV422,
  152. #ifdef HAVE_V4L2
  153. .fourcc = V4L2_PIX_FMT_YUYV,
  154. .colorspace = V4L2_COLORSPACE_SMPTE170M,
  155. #endif
  156. .depth = 16,
  157. .flags = ZORAN_FORMAT_CAPTURE |
  158. ZORAN_FORMAT_OVERLAY,
  159. }, {
  160. .name = "Hardware-encoded Motion-JPEG",
  161. .palette = -1,
  162. #ifdef HAVE_V4L2
  163. .fourcc = V4L2_PIX_FMT_MJPEG,
  164. .colorspace = V4L2_COLORSPACE_SMPTE170M,
  165. #endif
  166. .depth = 0,
  167. .flags = ZORAN_FORMAT_CAPTURE |
  168. ZORAN_FORMAT_PLAYBACK |
  169. ZORAN_FORMAT_COMPRESSED,
  170. }
  171. };
  172. static const int zoran_num_formats =
  173. (sizeof(zoran_formats) / sizeof(struct zoran_format));
  174. // RJ: Test only - want to test BUZ_USE_HIMEM even when CONFIG_BIGPHYS_AREA is defined
  175. #if !defined(CONFIG_BIGPHYS_AREA)
  176. //#undef CONFIG_BIGPHYS_AREA
  177. #define BUZ_USE_HIMEM
  178. #endif
  179. #if defined(CONFIG_BIGPHYS_AREA)
  180. # include <linux/bigphysarea.h>
  181. #endif
  182. extern int *zr_debug;
  183. #define dprintk(num, format, args...) \
  184. do { \
  185. if (*zr_debug >= num) \
  186. printk(format, ##args); \
  187. } while (0)
  188. extern int v4l_nbufs;
  189. extern int v4l_bufsize;
  190. extern int jpg_nbufs;
  191. extern int jpg_bufsize;
  192. extern int pass_through;
  193. static int lock_norm = 0; /* 1=Don't change TV standard (norm) */
  194. module_param(lock_norm, int, 0);
  195. MODULE_PARM_DESC(lock_norm, "Users can't change norm");
  196. #ifdef HAVE_V4L2
  197. /* small helper function for calculating buffersizes for v4l2
  198. * we calculate the nearest higher power-of-two, which
  199. * will be the recommended buffersize */
  200. static __u32
  201. zoran_v4l2_calc_bufsize (struct zoran_jpg_settings *settings)
  202. {
  203. __u8 div = settings->VerDcm * settings->HorDcm * settings->TmpDcm;
  204. __u32 num = (1024 * 512) / (div);
  205. __u32 result = 2;
  206. num--;
  207. while (num) {
  208. num >>= 1;
  209. result <<= 1;
  210. }
  211. if (result > jpg_bufsize)
  212. return jpg_bufsize;
  213. if (result < 8192)
  214. return 8192;
  215. return result;
  216. }
  217. #endif
  218. /* forward references */
  219. static void v4l_fbuffer_free(struct file *file);
  220. static void jpg_fbuffer_free(struct file *file);
  221. /*
  222. * Allocate the V4L grab buffers
  223. *
  224. * These have to be pysically contiguous.
  225. * If v4l_bufsize <= MAX_KMALLOC_MEM we use kmalloc
  226. * else we try to allocate them with bigphysarea_alloc_pages
  227. * if the bigphysarea patch is present in the kernel,
  228. * else we try to use high memory (if the user has bootet
  229. * Linux with the necessary memory left over).
  230. */
  231. #if defined(BUZ_USE_HIMEM) && !defined(CONFIG_BIGPHYS_AREA)
  232. static unsigned long
  233. get_high_mem (unsigned long size)
  234. {
  235. /*
  236. * Check if there is usable memory at the end of Linux memory
  237. * of at least size. Return the physical address of this memory,
  238. * return 0 on failure.
  239. *
  240. * The idea is from Alexandro Rubini's book "Linux device drivers".
  241. * The driver from him which is downloadable from O'Reilly's
  242. * web site misses the "virt_to_phys(high_memory)" part
  243. * (and therefore doesn't work at all - at least with 2.2.x kernels).
  244. *
  245. * It should be unnecessary to mention that THIS IS DANGEROUS,
  246. * if more than one driver at a time has the idea to use this memory!!!!
  247. */
  248. volatile unsigned char __iomem *mem;
  249. unsigned char c;
  250. unsigned long hi_mem_ph;
  251. unsigned long i;
  252. /* Map the high memory to user space */
  253. hi_mem_ph = virt_to_phys(high_memory);
  254. mem = ioremap(hi_mem_ph, size);
  255. if (!mem) {
  256. dprintk(1,
  257. KERN_ERR "%s: get_high_mem() - ioremap failed\n",
  258. ZORAN_NAME);
  259. return 0;
  260. }
  261. for (i = 0; i < size; i++) {
  262. /* Check if it is memory */
  263. c = i & 0xff;
  264. writeb(c, mem + i);
  265. if (readb(mem + i) != c)
  266. break;
  267. c = 255 - c;
  268. writeb(c, mem + i);
  269. if (readb(mem + i) != c)
  270. break;
  271. writeb(0, mem + i); /* zero out memory */
  272. /* give the kernel air to breath */
  273. if ((i & 0x3ffff) == 0x3ffff)
  274. schedule();
  275. }
  276. iounmap(mem);
  277. if (i != size) {
  278. dprintk(1,
  279. KERN_ERR
  280. "%s: get_high_mem() - requested %lu, avail %lu\n",
  281. ZORAN_NAME, size, i);
  282. return 0;
  283. }
  284. return hi_mem_ph;
  285. }
  286. #endif
  287. static int
  288. v4l_fbuffer_alloc (struct file *file)
  289. {
  290. struct zoran_fh *fh = file->private_data;
  291. struct zoran *zr = fh->zr;
  292. int i, off;
  293. unsigned char *mem;
  294. #if defined(BUZ_USE_HIMEM) && !defined(CONFIG_BIGPHYS_AREA)
  295. unsigned long pmem = 0;
  296. #endif
  297. /* we might have old buffers lying around... */
  298. if (fh->v4l_buffers.ready_to_be_freed) {
  299. v4l_fbuffer_free(file);
  300. }
  301. for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
  302. if (fh->v4l_buffers.buffer[i].fbuffer)
  303. dprintk(2,
  304. KERN_WARNING
  305. "%s: v4l_fbuffer_alloc() - buffer %d allready allocated!?\n",
  306. ZR_DEVNAME(zr), i);
  307. //udelay(20);
  308. if (fh->v4l_buffers.buffer_size <= MAX_KMALLOC_MEM) {
  309. /* Use kmalloc */
  310. mem =
  311. (unsigned char *) kmalloc(fh->v4l_buffers.
  312. buffer_size,
  313. GFP_KERNEL);
  314. if (mem == 0) {
  315. dprintk(1,
  316. KERN_ERR
  317. "%s: v4l_fbuffer_alloc() - kmalloc for V4L buf %d failed\n",
  318. ZR_DEVNAME(zr), i);
  319. v4l_fbuffer_free(file);
  320. return -ENOBUFS;
  321. }
  322. fh->v4l_buffers.buffer[i].fbuffer = mem;
  323. fh->v4l_buffers.buffer[i].fbuffer_phys =
  324. virt_to_phys(mem);
  325. fh->v4l_buffers.buffer[i].fbuffer_bus =
  326. virt_to_bus(mem);
  327. for (off = 0; off < fh->v4l_buffers.buffer_size;
  328. off += PAGE_SIZE)
  329. SetPageReserved(MAP_NR(mem + off));
  330. dprintk(4,
  331. KERN_INFO
  332. "%s: v4l_fbuffer_alloc() - V4L frame %d mem 0x%lx (bus: 0x%lx)\n",
  333. ZR_DEVNAME(zr), i, (unsigned long) mem,
  334. virt_to_bus(mem));
  335. } else {
  336. #if defined(CONFIG_BIGPHYS_AREA)
  337. /* Use bigphysarea_alloc_pages */
  338. int n =
  339. (fh->v4l_buffers.buffer_size + PAGE_SIZE -
  340. 1) / PAGE_SIZE;
  341. mem =
  342. (unsigned char *) bigphysarea_alloc_pages(n, 0,
  343. GFP_KERNEL);
  344. if (mem == 0) {
  345. dprintk(1,
  346. KERN_ERR
  347. "%s: v4l_fbuffer_alloc() - bigphysarea_alloc_pages for V4L buf %d failed\n",
  348. ZR_DEVNAME(zr), i);
  349. v4l_fbuffer_free(file);
  350. return -ENOBUFS;
  351. }
  352. fh->v4l_buffers.buffer[i].fbuffer = mem;
  353. fh->v4l_buffers.buffer[i].fbuffer_phys =
  354. virt_to_phys(mem);
  355. fh->v4l_buffers.buffer[i].fbuffer_bus =
  356. virt_to_bus(mem);
  357. dprintk(4,
  358. KERN_INFO
  359. "%s: Bigphysarea frame %d mem 0x%x (bus: 0x%x)\n",
  360. ZR_DEVNAME(zr), i, (unsigned) mem,
  361. (unsigned) virt_to_bus(mem));
  362. /* Zero out the allocated memory */
  363. memset(fh->v4l_buffers.buffer[i].fbuffer, 0,
  364. fh->v4l_buffers.buffer_size);
  365. #elif defined(BUZ_USE_HIMEM)
  366. /* Use high memory which has been left at boot time */
  367. /* Ok., Ok. this is an evil hack - we make
  368. * the assumption that physical addresses are
  369. * the same as bus addresses (true at least
  370. * for Intel processors). The whole method of
  371. * obtaining and using this memory is not very
  372. * nice - but I hope it saves some poor users
  373. * from kernel hacking, which might have even
  374. * more evil results */
  375. if (i == 0) {
  376. int size =
  377. fh->v4l_buffers.num_buffers *
  378. fh->v4l_buffers.buffer_size;
  379. pmem = get_high_mem(size);
  380. if (pmem == 0) {
  381. dprintk(1,
  382. KERN_ERR
  383. "%s: v4l_fbuffer_alloc() - get_high_mem (size = %d KB) for V4L bufs failed\n",
  384. ZR_DEVNAME(zr), size >> 10);
  385. return -ENOBUFS;
  386. }
  387. fh->v4l_buffers.buffer[0].fbuffer = NULL;
  388. fh->v4l_buffers.buffer[0].fbuffer_phys = pmem;
  389. fh->v4l_buffers.buffer[0].fbuffer_bus = pmem;
  390. dprintk(4,
  391. KERN_INFO
  392. "%s: v4l_fbuffer_alloc() - using %d KB high memory\n",
  393. ZR_DEVNAME(zr), size >> 10);
  394. } else {
  395. fh->v4l_buffers.buffer[i].fbuffer = NULL;
  396. fh->v4l_buffers.buffer[i].fbuffer_phys =
  397. pmem + i * fh->v4l_buffers.buffer_size;
  398. fh->v4l_buffers.buffer[i].fbuffer_bus =
  399. pmem + i * fh->v4l_buffers.buffer_size;
  400. }
  401. #else
  402. /* No bigphysarea present, usage of high memory disabled,
  403. * but user wants buffers of more than MAX_KMALLOC_MEM */
  404. dprintk(1,
  405. KERN_ERR
  406. "%s: v4l_fbuffer_alloc() - no bigphysarea_patch present, usage of high memory disabled,\n",
  407. ZR_DEVNAME(zr));
  408. dprintk(1,
  409. KERN_ERR
  410. "%s: v4l_fbuffer_alloc() - sorry, could not allocate %d V4L buffers of size %d KB.\n",
  411. ZR_DEVNAME(zr), fh->v4l_buffers.num_buffers,
  412. fh->v4l_buffers.buffer_size >> 10);
  413. return -ENOBUFS;
  414. #endif
  415. }
  416. }
  417. fh->v4l_buffers.allocated = 1;
  418. return 0;
  419. }
  420. /* free the V4L grab buffers */
  421. static void
  422. v4l_fbuffer_free (struct file *file)
  423. {
  424. struct zoran_fh *fh = file->private_data;
  425. struct zoran *zr = fh->zr;
  426. int i, off;
  427. unsigned char *mem;
  428. dprintk(4, KERN_INFO "%s: v4l_fbuffer_free()\n", ZR_DEVNAME(zr));
  429. for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
  430. if (!fh->v4l_buffers.buffer[i].fbuffer)
  431. continue;
  432. if (fh->v4l_buffers.buffer_size <= MAX_KMALLOC_MEM) {
  433. mem = fh->v4l_buffers.buffer[i].fbuffer;
  434. for (off = 0; off < fh->v4l_buffers.buffer_size;
  435. off += PAGE_SIZE)
  436. ClearPageReserved(MAP_NR(mem + off));
  437. kfree((void *) fh->v4l_buffers.buffer[i].fbuffer);
  438. }
  439. #if defined(CONFIG_BIGPHYS_AREA)
  440. else
  441. bigphysarea_free_pages((void *) fh->v4l_buffers.
  442. buffer[i].fbuffer);
  443. #endif
  444. fh->v4l_buffers.buffer[i].fbuffer = NULL;
  445. }
  446. fh->v4l_buffers.allocated = 0;
  447. fh->v4l_buffers.ready_to_be_freed = 0;
  448. }
  449. /*
  450. * Allocate the MJPEG grab buffers.
  451. *
  452. * If the requested buffer size is smaller than MAX_KMALLOC_MEM,
  453. * kmalloc is used to request a physically contiguous area,
  454. * else we allocate the memory in framgents with get_zeroed_page.
  455. *
  456. * If a Natoma chipset is present and this is a revision 1 zr36057,
  457. * each MJPEG buffer needs to be physically contiguous.
  458. * (RJ: This statement is from Dave Perks' original driver,
  459. * I could never check it because I have a zr36067)
  460. * The driver cares about this because it reduces the buffer
  461. * size to MAX_KMALLOC_MEM in that case (which forces contiguous allocation).
  462. *
  463. * RJ: The contents grab buffers needs never be accessed in the driver.
  464. * Therefore there is no need to allocate them with vmalloc in order
  465. * to get a contiguous virtual memory space.
  466. * I don't understand why many other drivers first allocate them with
  467. * vmalloc (which uses internally also get_zeroed_page, but delivers you
  468. * virtual addresses) and then again have to make a lot of efforts
  469. * to get the physical address.
  470. *
  471. * Ben Capper:
  472. * On big-endian architectures (such as ppc) some extra steps
  473. * are needed. When reading and writing to the stat_com array
  474. * and fragment buffers, the device expects to see little-
  475. * endian values. The use of cpu_to_le32() and le32_to_cpu()
  476. * in this function (and one or two others in zoran_device.c)
  477. * ensure that these values are always stored in little-endian
  478. * form, regardless of architecture. The zr36057 does Very Bad
  479. * Things on big endian architectures if the stat_com array
  480. * and fragment buffers are not little-endian.
  481. */
  482. static int
  483. jpg_fbuffer_alloc (struct file *file)
  484. {
  485. struct zoran_fh *fh = file->private_data;
  486. struct zoran *zr = fh->zr;
  487. int i, j, off;
  488. unsigned long mem;
  489. /* we might have old buffers lying around */
  490. if (fh->jpg_buffers.ready_to_be_freed) {
  491. jpg_fbuffer_free(file);
  492. }
  493. for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
  494. if (fh->jpg_buffers.buffer[i].frag_tab)
  495. dprintk(2,
  496. KERN_WARNING
  497. "%s: jpg_fbuffer_alloc() - buffer %d allready allocated!?\n",
  498. ZR_DEVNAME(zr), i);
  499. /* Allocate fragment table for this buffer */
  500. mem = get_zeroed_page(GFP_KERNEL);
  501. if (mem == 0) {
  502. dprintk(1,
  503. KERN_ERR
  504. "%s: jpg_fbuffer_alloc() - get_zeroed_page (frag_tab) failed for buffer %d\n",
  505. ZR_DEVNAME(zr), i);
  506. jpg_fbuffer_free(file);
  507. return -ENOBUFS;
  508. }
  509. memset((void *) mem, 0, PAGE_SIZE);
  510. fh->jpg_buffers.buffer[i].frag_tab = (u32 *) mem;
  511. fh->jpg_buffers.buffer[i].frag_tab_bus =
  512. virt_to_bus((void *) mem);
  513. //if (alloc_contig) {
  514. if (fh->jpg_buffers.need_contiguous) {
  515. mem =
  516. (unsigned long) kmalloc(fh->jpg_buffers.
  517. buffer_size,
  518. GFP_KERNEL);
  519. if (mem == 0) {
  520. dprintk(1,
  521. KERN_ERR
  522. "%s: jpg_fbuffer_alloc() - kmalloc failed for buffer %d\n",
  523. ZR_DEVNAME(zr), i);
  524. jpg_fbuffer_free(file);
  525. return -ENOBUFS;
  526. }
  527. fh->jpg_buffers.buffer[i].frag_tab[0] =
  528. cpu_to_le32(virt_to_bus((void *) mem));
  529. fh->jpg_buffers.buffer[i].frag_tab[1] =
  530. cpu_to_le32(((fh->jpg_buffers.buffer_size / 4) << 1) | 1);
  531. for (off = 0; off < fh->jpg_buffers.buffer_size;
  532. off += PAGE_SIZE)
  533. SetPageReserved(MAP_NR(mem + off));
  534. } else {
  535. /* jpg_bufsize is allreay page aligned */
  536. for (j = 0;
  537. j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
  538. j++) {
  539. mem = get_zeroed_page(GFP_KERNEL);
  540. if (mem == 0) {
  541. dprintk(1,
  542. KERN_ERR
  543. "%s: jpg_fbuffer_alloc() - get_zeroed_page failed for buffer %d\n",
  544. ZR_DEVNAME(zr), i);
  545. jpg_fbuffer_free(file);
  546. return -ENOBUFS;
  547. }
  548. fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
  549. cpu_to_le32(virt_to_bus((void *) mem));
  550. fh->jpg_buffers.buffer[i].frag_tab[2 * j +
  551. 1] =
  552. cpu_to_le32((PAGE_SIZE / 4) << 1);
  553. SetPageReserved(MAP_NR(mem));
  554. }
  555. fh->jpg_buffers.buffer[i].frag_tab[2 * j - 1] |= cpu_to_le32(1);
  556. }
  557. }
  558. dprintk(4,
  559. KERN_DEBUG "%s: jpg_fbuffer_alloc() - %d KB allocated\n",
  560. ZR_DEVNAME(zr),
  561. (fh->jpg_buffers.num_buffers *
  562. fh->jpg_buffers.buffer_size) >> 10);
  563. fh->jpg_buffers.allocated = 1;
  564. return 0;
  565. }
  566. /* free the MJPEG grab buffers */
  567. static void
  568. jpg_fbuffer_free (struct file *file)
  569. {
  570. struct zoran_fh *fh = file->private_data;
  571. struct zoran *zr = fh->zr;
  572. int i, j, off;
  573. unsigned char *mem;
  574. dprintk(4, KERN_DEBUG "%s: jpg_fbuffer_free()\n", ZR_DEVNAME(zr));
  575. for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
  576. if (!fh->jpg_buffers.buffer[i].frag_tab)
  577. continue;
  578. //if (alloc_contig) {
  579. if (fh->jpg_buffers.need_contiguous) {
  580. if (fh->jpg_buffers.buffer[i].frag_tab[0]) {
  581. mem = (unsigned char *) bus_to_virt(le32_to_cpu(
  582. fh->jpg_buffers.buffer[i].frag_tab[0]));
  583. for (off = 0;
  584. off < fh->jpg_buffers.buffer_size;
  585. off += PAGE_SIZE)
  586. ClearPageReserved(MAP_NR
  587. (mem + off));
  588. kfree(mem);
  589. fh->jpg_buffers.buffer[i].frag_tab[0] = 0;
  590. fh->jpg_buffers.buffer[i].frag_tab[1] = 0;
  591. }
  592. } else {
  593. for (j = 0;
  594. j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
  595. j++) {
  596. if (!fh->jpg_buffers.buffer[i].
  597. frag_tab[2 * j])
  598. break;
  599. ClearPageReserved(MAP_NR
  600. (bus_to_virt
  601. (le32_to_cpu
  602. (fh->jpg_buffers.
  603. buffer[i].frag_tab[2 *
  604. j]))));
  605. free_page((unsigned long)
  606. bus_to_virt
  607. (le32_to_cpu
  608. (fh->jpg_buffers.
  609. buffer[i].
  610. frag_tab[2 * j])));
  611. fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
  612. 0;
  613. fh->jpg_buffers.buffer[i].frag_tab[2 * j +
  614. 1] = 0;
  615. }
  616. }
  617. free_page((unsigned long) fh->jpg_buffers.buffer[i].
  618. frag_tab);
  619. fh->jpg_buffers.buffer[i].frag_tab = NULL;
  620. }
  621. fh->jpg_buffers.allocated = 0;
  622. fh->jpg_buffers.ready_to_be_freed = 0;
  623. }
  624. /*
  625. * V4L Buffer grabbing
  626. */
  627. static int
  628. zoran_v4l_set_format (struct file *file,
  629. int width,
  630. int height,
  631. const struct zoran_format *format)
  632. {
  633. struct zoran_fh *fh = file->private_data;
  634. struct zoran *zr = fh->zr;
  635. int bpp;
  636. /* Check size and format of the grab wanted */
  637. if (height < BUZ_MIN_HEIGHT || width < BUZ_MIN_WIDTH ||
  638. height > BUZ_MAX_HEIGHT || width > BUZ_MAX_WIDTH) {
  639. dprintk(1,
  640. KERN_ERR
  641. "%s: v4l_set_format() - wrong frame size (%dx%d)\n",
  642. ZR_DEVNAME(zr), width, height);
  643. return -EINVAL;
  644. }
  645. bpp = (format->depth + 7) / 8;
  646. /* Check against available buffer size */
  647. if (height * width * bpp > fh->v4l_buffers.buffer_size) {
  648. dprintk(1,
  649. KERN_ERR
  650. "%s: v4l_set_format() - video buffer size (%d kB) is too small\n",
  651. ZR_DEVNAME(zr), fh->v4l_buffers.buffer_size >> 10);
  652. return -EINVAL;
  653. }
  654. /* The video front end needs 4-byte alinged line sizes */
  655. if ((bpp == 2 && (width & 1)) || (bpp == 3 && (width & 3))) {
  656. dprintk(1,
  657. KERN_ERR
  658. "%s: v4l_set_format() - wrong frame alingment\n",
  659. ZR_DEVNAME(zr));
  660. return -EINVAL;
  661. }
  662. fh->v4l_settings.width = width;
  663. fh->v4l_settings.height = height;
  664. fh->v4l_settings.format = format;
  665. fh->v4l_settings.bytesperline = bpp * fh->v4l_settings.width;
  666. return 0;
  667. }
  668. static int
  669. zoran_v4l_queue_frame (struct file *file,
  670. int num)
  671. {
  672. struct zoran_fh *fh = file->private_data;
  673. struct zoran *zr = fh->zr;
  674. unsigned long flags;
  675. int res = 0;
  676. if (!fh->v4l_buffers.allocated) {
  677. dprintk(1,
  678. KERN_ERR
  679. "%s: v4l_queue_frame() - buffers not yet allocated\n",
  680. ZR_DEVNAME(zr));
  681. res = -ENOMEM;
  682. }
  683. /* No grabbing outside the buffer range! */
  684. if (num >= fh->v4l_buffers.num_buffers || num < 0) {
  685. dprintk(1,
  686. KERN_ERR
  687. "%s: v4l_queue_frame() - buffer %d is out of range\n",
  688. ZR_DEVNAME(zr), num);
  689. res = -EINVAL;
  690. }
  691. spin_lock_irqsave(&zr->spinlock, flags);
  692. if (fh->v4l_buffers.active == ZORAN_FREE) {
  693. if (zr->v4l_buffers.active == ZORAN_FREE) {
  694. zr->v4l_buffers = fh->v4l_buffers;
  695. fh->v4l_buffers.active = ZORAN_ACTIVE;
  696. } else {
  697. dprintk(1,
  698. KERN_ERR
  699. "%s: v4l_queue_frame() - another session is already capturing\n",
  700. ZR_DEVNAME(zr));
  701. res = -EBUSY;
  702. }
  703. }
  704. /* make sure a grab isn't going on currently with this buffer */
  705. if (!res) {
  706. switch (zr->v4l_buffers.buffer[num].state) {
  707. default:
  708. case BUZ_STATE_PEND:
  709. if (zr->v4l_buffers.active == ZORAN_FREE) {
  710. fh->v4l_buffers.active = ZORAN_FREE;
  711. zr->v4l_buffers.allocated = 0;
  712. }
  713. res = -EBUSY; /* what are you doing? */
  714. break;
  715. case BUZ_STATE_DONE:
  716. dprintk(2,
  717. KERN_WARNING
  718. "%s: v4l_queue_frame() - queueing buffer %d in state DONE!?\n",
  719. ZR_DEVNAME(zr), num);
  720. case BUZ_STATE_USER:
  721. /* since there is at least one unused buffer there's room for at least
  722. * one more pend[] entry */
  723. zr->v4l_pend[zr->v4l_pend_head++ &
  724. V4L_MASK_FRAME] = num;
  725. zr->v4l_buffers.buffer[num].state = BUZ_STATE_PEND;
  726. zr->v4l_buffers.buffer[num].bs.length =
  727. fh->v4l_settings.bytesperline *
  728. zr->v4l_settings.height;
  729. fh->v4l_buffers.buffer[num] =
  730. zr->v4l_buffers.buffer[num];
  731. break;
  732. }
  733. }
  734. spin_unlock_irqrestore(&zr->spinlock, flags);
  735. if (!res && zr->v4l_buffers.active == ZORAN_FREE)
  736. zr->v4l_buffers.active = fh->v4l_buffers.active;
  737. return res;
  738. }
  739. static int
  740. v4l_grab (struct file *file,
  741. struct video_mmap *mp)
  742. {
  743. struct zoran_fh *fh = file->private_data;
  744. struct zoran *zr = fh->zr;
  745. int res = 0, i;
  746. for (i = 0; i < zoran_num_formats; i++) {
  747. if (zoran_formats[i].palette == mp->format &&
  748. zoran_formats[i].flags & ZORAN_FORMAT_CAPTURE &&
  749. !(zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED))
  750. break;
  751. }
  752. if (i == zoran_num_formats || zoran_formats[i].depth == 0) {
  753. dprintk(1,
  754. KERN_ERR
  755. "%s: v4l_grab() - wrong bytes-per-pixel format\n",
  756. ZR_DEVNAME(zr));
  757. return -EINVAL;
  758. }
  759. /*
  760. * To minimize the time spent in the IRQ routine, we avoid setting up
  761. * the video front end there.
  762. * If this grab has different parameters from a running streaming capture
  763. * we stop the streaming capture and start it over again.
  764. */
  765. if (zr->v4l_memgrab_active &&
  766. (zr->v4l_settings.width != mp->width ||
  767. zr->v4l_settings.height != mp->height ||
  768. zr->v4l_settings.format->palette != mp->format)) {
  769. res = wait_grab_pending(zr);
  770. if (res)
  771. return res;
  772. }
  773. if ((res = zoran_v4l_set_format(file,
  774. mp->width,
  775. mp->height,
  776. &zoran_formats[i])))
  777. return res;
  778. zr->v4l_settings = fh->v4l_settings;
  779. /* queue the frame in the pending queue */
  780. if ((res = zoran_v4l_queue_frame(file, mp->frame))) {
  781. fh->v4l_buffers.active = ZORAN_FREE;
  782. return res;
  783. }
  784. /* put the 36057 into frame grabbing mode */
  785. if (!res && !zr->v4l_memgrab_active)
  786. zr36057_set_memgrab(zr, 1);
  787. //dprintk(4, KERN_INFO "%s: Frame grab 3...\n", ZR_DEVNAME(zr));
  788. return res;
  789. }
  790. /*
  791. * Sync on a V4L buffer
  792. */
  793. static int
  794. v4l_sync (struct file *file,
  795. int frame)
  796. {
  797. struct zoran_fh *fh = file->private_data;
  798. struct zoran *zr = fh->zr;
  799. unsigned long flags;
  800. if (fh->v4l_buffers.active == ZORAN_FREE) {
  801. dprintk(1,
  802. KERN_ERR
  803. "%s: v4l_sync() - no grab active for this session\n",
  804. ZR_DEVNAME(zr));
  805. return -EINVAL;
  806. }
  807. /* check passed-in frame number */
  808. if (frame >= fh->v4l_buffers.num_buffers || frame < 0) {
  809. dprintk(1,
  810. KERN_ERR "%s: v4l_sync() - frame %d is invalid\n",
  811. ZR_DEVNAME(zr), frame);
  812. return -EINVAL;
  813. }
  814. /* Check if is buffer was queued at all */
  815. if (zr->v4l_buffers.buffer[frame].state == BUZ_STATE_USER) {
  816. dprintk(1,
  817. KERN_ERR
  818. "%s: v4l_sync() - attempt to sync on a buffer which was not queued?\n",
  819. ZR_DEVNAME(zr));
  820. return -EPROTO;
  821. }
  822. /* wait on this buffer to get ready */
  823. if (!wait_event_interruptible_timeout(zr->v4l_capq,
  824. (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_PEND),
  825. 10*HZ))
  826. return -ETIME;
  827. if (signal_pending(current))
  828. return -ERESTARTSYS;
  829. /* buffer should now be in BUZ_STATE_DONE */
  830. if (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_DONE)
  831. dprintk(2,
  832. KERN_ERR "%s: v4l_sync() - internal state error\n",
  833. ZR_DEVNAME(zr));
  834. zr->v4l_buffers.buffer[frame].state = BUZ_STATE_USER;
  835. fh->v4l_buffers.buffer[frame] = zr->v4l_buffers.buffer[frame];
  836. spin_lock_irqsave(&zr->spinlock, flags);
  837. /* Check if streaming capture has finished */
  838. if (zr->v4l_pend_tail == zr->v4l_pend_head) {
  839. zr36057_set_memgrab(zr, 0);
  840. if (zr->v4l_buffers.active == ZORAN_ACTIVE) {
  841. fh->v4l_buffers.active = zr->v4l_buffers.active =
  842. ZORAN_FREE;
  843. zr->v4l_buffers.allocated = 0;
  844. }
  845. }
  846. spin_unlock_irqrestore(&zr->spinlock, flags);
  847. return 0;
  848. }
  849. /*
  850. * Queue a MJPEG buffer for capture/playback
  851. */
  852. static int
  853. zoran_jpg_queue_frame (struct file *file,
  854. int num,
  855. enum zoran_codec_mode mode)
  856. {
  857. struct zoran_fh *fh = file->private_data;
  858. struct zoran *zr = fh->zr;
  859. unsigned long flags;
  860. int res = 0;
  861. /* Check if buffers are allocated */
  862. if (!fh->jpg_buffers.allocated) {
  863. dprintk(1,
  864. KERN_ERR
  865. "%s: jpg_queue_frame() - buffers not yet allocated\n",
  866. ZR_DEVNAME(zr));
  867. return -ENOMEM;
  868. }
  869. /* No grabbing outside the buffer range! */
  870. if (num >= fh->jpg_buffers.num_buffers || num < 0) {
  871. dprintk(1,
  872. KERN_ERR
  873. "%s: jpg_queue_frame() - buffer %d out of range\n",
  874. ZR_DEVNAME(zr), num);
  875. return -EINVAL;
  876. }
  877. /* what is the codec mode right now? */
  878. if (zr->codec_mode == BUZ_MODE_IDLE) {
  879. zr->jpg_settings = fh->jpg_settings;
  880. } else if (zr->codec_mode != mode) {
  881. /* wrong codec mode active - invalid */
  882. dprintk(1,
  883. KERN_ERR
  884. "%s: jpg_queue_frame() - codec in wrong mode\n",
  885. ZR_DEVNAME(zr));
  886. return -EINVAL;
  887. }
  888. if (fh->jpg_buffers.active == ZORAN_FREE) {
  889. if (zr->jpg_buffers.active == ZORAN_FREE) {
  890. zr->jpg_buffers = fh->jpg_buffers;
  891. fh->jpg_buffers.active = ZORAN_ACTIVE;
  892. } else {
  893. dprintk(1,
  894. KERN_ERR
  895. "%s: jpg_queue_frame() - another session is already capturing\n",
  896. ZR_DEVNAME(zr));
  897. res = -EBUSY;
  898. }
  899. }
  900. if (!res && zr->codec_mode == BUZ_MODE_IDLE) {
  901. /* Ok load up the jpeg codec */
  902. zr36057_enable_jpg(zr, mode);
  903. }
  904. spin_lock_irqsave(&zr->spinlock, flags);
  905. if (!res) {
  906. switch (zr->jpg_buffers.buffer[num].state) {
  907. case BUZ_STATE_DONE:
  908. dprintk(2,
  909. KERN_WARNING
  910. "%s: jpg_queue_frame() - queing frame in BUZ_STATE_DONE state!?\n",
  911. ZR_DEVNAME(zr));
  912. case BUZ_STATE_USER:
  913. /* since there is at least one unused buffer there's room for at
  914. *least one more pend[] entry */
  915. zr->jpg_pend[zr->jpg_que_head++ & BUZ_MASK_FRAME] =
  916. num;
  917. zr->jpg_buffers.buffer[num].state = BUZ_STATE_PEND;
  918. fh->jpg_buffers.buffer[num] =
  919. zr->jpg_buffers.buffer[num];
  920. zoran_feed_stat_com(zr);
  921. break;
  922. default:
  923. case BUZ_STATE_DMA:
  924. case BUZ_STATE_PEND:
  925. if (zr->jpg_buffers.active == ZORAN_FREE) {
  926. fh->jpg_buffers.active = ZORAN_FREE;
  927. zr->jpg_buffers.allocated = 0;
  928. }
  929. res = -EBUSY; /* what are you doing? */
  930. break;
  931. }
  932. }
  933. spin_unlock_irqrestore(&zr->spinlock, flags);
  934. if (!res && zr->jpg_buffers.active == ZORAN_FREE) {
  935. zr->jpg_buffers.active = fh->jpg_buffers.active;
  936. }
  937. return res;
  938. }
  939. static int
  940. jpg_qbuf (struct file *file,
  941. int frame,
  942. enum zoran_codec_mode mode)
  943. {
  944. struct zoran_fh *fh = file->private_data;
  945. struct zoran *zr = fh->zr;
  946. int res = 0;
  947. /* Does the user want to stop streaming? */
  948. if (frame < 0) {
  949. if (zr->codec_mode == mode) {
  950. if (fh->jpg_buffers.active == ZORAN_FREE) {
  951. dprintk(1,
  952. KERN_ERR
  953. "%s: jpg_qbuf(-1) - session not active\n",
  954. ZR_DEVNAME(zr));
  955. return -EINVAL;
  956. }
  957. fh->jpg_buffers.active = zr->jpg_buffers.active =
  958. ZORAN_FREE;
  959. zr->jpg_buffers.allocated = 0;
  960. zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
  961. return 0;
  962. } else {
  963. dprintk(1,
  964. KERN_ERR
  965. "%s: jpg_qbuf() - stop streaming but not in streaming mode\n",
  966. ZR_DEVNAME(zr));
  967. return -EINVAL;
  968. }
  969. }
  970. if ((res = zoran_jpg_queue_frame(file, frame, mode)))
  971. return res;
  972. /* Start the jpeg codec when the first frame is queued */
  973. if (!res && zr->jpg_que_head == 1)
  974. jpeg_start(zr);
  975. return res;
  976. }
  977. /*
  978. * Sync on a MJPEG buffer
  979. */
  980. static int
  981. jpg_sync (struct file *file,
  982. struct zoran_sync *bs)
  983. {
  984. struct zoran_fh *fh = file->private_data;
  985. struct zoran *zr = fh->zr;
  986. unsigned long flags;
  987. int frame;
  988. if (fh->jpg_buffers.active == ZORAN_FREE) {
  989. dprintk(1,
  990. KERN_ERR
  991. "%s: jpg_sync() - capture is not currently active\n",
  992. ZR_DEVNAME(zr));
  993. return -EINVAL;
  994. }
  995. if (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS &&
  996. zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) {
  997. dprintk(1,
  998. KERN_ERR
  999. "%s: jpg_sync() - codec not in streaming mode\n",
  1000. ZR_DEVNAME(zr));
  1001. return -EINVAL;
  1002. }
  1003. if (!wait_event_interruptible_timeout(zr->jpg_capq,
  1004. (zr->jpg_que_tail != zr->jpg_dma_tail ||
  1005. zr->jpg_dma_tail == zr->jpg_dma_head),
  1006. 10*HZ)) {
  1007. int isr;
  1008. btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
  1009. udelay(1);
  1010. zr->codec->control(zr->codec, CODEC_G_STATUS,
  1011. sizeof(isr), &isr);
  1012. dprintk(1,
  1013. KERN_ERR
  1014. "%s: jpg_sync() - timeout: codec isr=0x%02x\n",
  1015. ZR_DEVNAME(zr), isr);
  1016. return -ETIME;
  1017. }
  1018. if (signal_pending(current))
  1019. return -ERESTARTSYS;
  1020. spin_lock_irqsave(&zr->spinlock, flags);
  1021. if (zr->jpg_dma_tail != zr->jpg_dma_head)
  1022. frame = zr->jpg_pend[zr->jpg_que_tail++ & BUZ_MASK_FRAME];
  1023. else
  1024. frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
  1025. /* buffer should now be in BUZ_STATE_DONE */
  1026. if (*zr_debug > 0)
  1027. if (zr->jpg_buffers.buffer[frame].state != BUZ_STATE_DONE)
  1028. dprintk(2,
  1029. KERN_ERR
  1030. "%s: jpg_sync() - internal state error\n",
  1031. ZR_DEVNAME(zr));
  1032. *bs = zr->jpg_buffers.buffer[frame].bs;
  1033. bs->frame = frame;
  1034. zr->jpg_buffers.buffer[frame].state = BUZ_STATE_USER;
  1035. fh->jpg_buffers.buffer[frame] = zr->jpg_buffers.buffer[frame];
  1036. spin_unlock_irqrestore(&zr->spinlock, flags);
  1037. return 0;
  1038. }
  1039. static void
  1040. zoran_open_init_session (struct file *file)
  1041. {
  1042. int i;
  1043. struct zoran_fh *fh = file->private_data;
  1044. struct zoran *zr = fh->zr;
  1045. /* Per default, map the V4L Buffers */
  1046. fh->map_mode = ZORAN_MAP_MODE_RAW;
  1047. /* take over the card's current settings */
  1048. fh->overlay_settings = zr->overlay_settings;
  1049. fh->overlay_settings.is_set = 0;
  1050. fh->overlay_settings.format = zr->overlay_settings.format;
  1051. fh->overlay_active = ZORAN_FREE;
  1052. /* v4l settings */
  1053. fh->v4l_settings = zr->v4l_settings;
  1054. /* v4l_buffers */
  1055. memset(&fh->v4l_buffers, 0, sizeof(struct zoran_v4l_struct));
  1056. for (i = 0; i < VIDEO_MAX_FRAME; i++) {
  1057. fh->v4l_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
  1058. fh->v4l_buffers.buffer[i].bs.frame = i;
  1059. }
  1060. fh->v4l_buffers.allocated = 0;
  1061. fh->v4l_buffers.ready_to_be_freed = 0;
  1062. fh->v4l_buffers.active = ZORAN_FREE;
  1063. fh->v4l_buffers.buffer_size = v4l_bufsize;
  1064. fh->v4l_buffers.num_buffers = v4l_nbufs;
  1065. /* jpg settings */
  1066. fh->jpg_settings = zr->jpg_settings;
  1067. /* jpg_buffers */
  1068. memset(&fh->jpg_buffers, 0, sizeof(struct zoran_jpg_struct));
  1069. for (i = 0; i < BUZ_MAX_FRAME; i++) {
  1070. fh->jpg_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
  1071. fh->jpg_buffers.buffer[i].bs.frame = i;
  1072. }
  1073. fh->jpg_buffers.need_contiguous = zr->jpg_buffers.need_contiguous;
  1074. fh->jpg_buffers.allocated = 0;
  1075. fh->jpg_buffers.ready_to_be_freed = 0;
  1076. fh->jpg_buffers.active = ZORAN_FREE;
  1077. fh->jpg_buffers.buffer_size = jpg_bufsize;
  1078. fh->jpg_buffers.num_buffers = jpg_nbufs;
  1079. }
  1080. static void
  1081. zoran_close_end_session (struct file *file)
  1082. {
  1083. struct zoran_fh *fh = file->private_data;
  1084. struct zoran *zr = fh->zr;
  1085. /* overlay */
  1086. if (fh->overlay_active != ZORAN_FREE) {
  1087. fh->overlay_active = zr->overlay_active = ZORAN_FREE;
  1088. zr->v4l_overlay_active = 0;
  1089. if (!zr->v4l_memgrab_active)
  1090. zr36057_overlay(zr, 0);
  1091. zr->overlay_mask = NULL;
  1092. }
  1093. /* v4l capture */
  1094. if (fh->v4l_buffers.active != ZORAN_FREE) {
  1095. zr36057_set_memgrab(zr, 0);
  1096. zr->v4l_buffers.allocated = 0;
  1097. zr->v4l_buffers.active = fh->v4l_buffers.active =
  1098. ZORAN_FREE;
  1099. }
  1100. /* v4l buffers */
  1101. if (fh->v4l_buffers.allocated ||
  1102. fh->v4l_buffers.ready_to_be_freed) {
  1103. v4l_fbuffer_free(file);
  1104. }
  1105. /* jpg capture */
  1106. if (fh->jpg_buffers.active != ZORAN_FREE) {
  1107. zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
  1108. zr->jpg_buffers.allocated = 0;
  1109. zr->jpg_buffers.active = fh->jpg_buffers.active =
  1110. ZORAN_FREE;
  1111. }
  1112. /* jpg buffers */
  1113. if (fh->jpg_buffers.allocated ||
  1114. fh->jpg_buffers.ready_to_be_freed) {
  1115. jpg_fbuffer_free(file);
  1116. }
  1117. }
  1118. /*
  1119. * Open a zoran card. Right now the flags stuff is just playing
  1120. */
  1121. static int
  1122. zoran_open (struct inode *inode,
  1123. struct file *file)
  1124. {
  1125. unsigned int minor = iminor(inode);
  1126. struct zoran *zr = NULL;
  1127. struct zoran_fh *fh;
  1128. int i, res, first_open = 0, have_module_locks = 0;
  1129. /* find the device */
  1130. for (i = 0; i < zoran_num; i++) {
  1131. if (zoran[i].video_dev->minor == minor) {
  1132. zr = &zoran[i];
  1133. break;
  1134. }
  1135. }
  1136. if (!zr) {
  1137. dprintk(1, KERN_ERR "%s: device not found!\n", ZORAN_NAME);
  1138. res = -ENODEV;
  1139. goto open_unlock_and_return;
  1140. }
  1141. /* see fs/device.c - the kernel already locks during open(),
  1142. * so locking ourselves only causes deadlocks */
  1143. /*down(&zr->resource_lock);*/
  1144. if (!zr->decoder) {
  1145. dprintk(1,
  1146. KERN_ERR "%s: no TV decoder loaded for device!\n",
  1147. ZR_DEVNAME(zr));
  1148. res = -EIO;
  1149. goto open_unlock_and_return;
  1150. }
  1151. /* try to grab a module lock */
  1152. if (!try_module_get(THIS_MODULE)) {
  1153. dprintk(1,
  1154. KERN_ERR
  1155. "%s: failed to acquire my own lock! PANIC!\n",
  1156. ZR_DEVNAME(zr));
  1157. res = -ENODEV;
  1158. goto open_unlock_and_return;
  1159. }
  1160. if (!try_module_get(zr->decoder->driver->driver.owner)) {
  1161. dprintk(1,
  1162. KERN_ERR
  1163. "%s: failed to grab ownership of i2c decoder\n",
  1164. ZR_DEVNAME(zr));
  1165. res = -EIO;
  1166. module_put(THIS_MODULE);
  1167. goto open_unlock_and_return;
  1168. }
  1169. if (zr->encoder &&
  1170. !try_module_get(zr->encoder->driver->driver.owner)) {
  1171. dprintk(1,
  1172. KERN_ERR
  1173. "%s: failed to grab ownership of i2c encoder\n",
  1174. ZR_DEVNAME(zr));
  1175. res = -EIO;
  1176. module_put(zr->decoder->driver->driver.owner);
  1177. module_put(THIS_MODULE);
  1178. goto open_unlock_and_return;
  1179. }
  1180. have_module_locks = 1;
  1181. if (zr->user >= 2048) {
  1182. dprintk(1, KERN_ERR "%s: too many users (%d) on device\n",
  1183. ZR_DEVNAME(zr), zr->user);
  1184. res = -EBUSY;
  1185. goto open_unlock_and_return;
  1186. }
  1187. dprintk(1, KERN_INFO "%s: zoran_open(%s, pid=[%d]), users(-)=%d\n",
  1188. ZR_DEVNAME(zr), current->comm, current->pid, zr->user);
  1189. /* now, create the open()-specific file_ops struct */
  1190. fh = kzalloc(sizeof(struct zoran_fh), GFP_KERNEL);
  1191. if (!fh) {
  1192. dprintk(1,
  1193. KERN_ERR
  1194. "%s: zoran_open() - allocation of zoran_fh failed\n",
  1195. ZR_DEVNAME(zr));
  1196. res = -ENOMEM;
  1197. goto open_unlock_and_return;
  1198. }
  1199. /* used to be BUZ_MAX_WIDTH/HEIGHT, but that gives overflows
  1200. * on norm-change! */
  1201. fh->overlay_mask =
  1202. kmalloc(((768 + 31) / 32) * 576 * 4, GFP_KERNEL);
  1203. if (!fh->overlay_mask) {
  1204. dprintk(1,
  1205. KERN_ERR
  1206. "%s: zoran_open() - allocation of overlay_mask failed\n",
  1207. ZR_DEVNAME(zr));
  1208. kfree(fh);
  1209. res = -ENOMEM;
  1210. goto open_unlock_and_return;
  1211. }
  1212. if (zr->user++ == 0)
  1213. first_open = 1;
  1214. /*up(&zr->resource_lock);*/
  1215. /* default setup - TODO: look at flags */
  1216. if (first_open) { /* First device open */
  1217. zr36057_restart(zr);
  1218. zoran_open_init_params(zr);
  1219. zoran_init_hardware(zr);
  1220. btor(ZR36057_ICR_IntPinEn, ZR36057_ICR);
  1221. }
  1222. /* set file_ops stuff */
  1223. file->private_data = fh;
  1224. fh->zr = zr;
  1225. zoran_open_init_session(file);
  1226. return 0;
  1227. open_unlock_and_return:
  1228. /* if we grabbed locks, release them accordingly */
  1229. if (have_module_locks) {
  1230. module_put(zr->decoder->driver->driver.owner);
  1231. if (zr->encoder) {
  1232. module_put(zr->encoder->driver->driver.owner);
  1233. }
  1234. module_put(THIS_MODULE);
  1235. }
  1236. /* if there's no device found, we didn't obtain the lock either */
  1237. if (zr) {
  1238. /*up(&zr->resource_lock);*/
  1239. }
  1240. return res;
  1241. }
  1242. static int
  1243. zoran_close (struct inode *inode,
  1244. struct file *file)
  1245. {
  1246. struct zoran_fh *fh = file->private_data;
  1247. struct zoran *zr = fh->zr;
  1248. dprintk(1, KERN_INFO "%s: zoran_close(%s, pid=[%d]), users(+)=%d\n",
  1249. ZR_DEVNAME(zr), current->comm, current->pid, zr->user);
  1250. /* kernel locks (fs/device.c), so don't do that ourselves
  1251. * (prevents deadlocks) */
  1252. /*down(&zr->resource_lock);*/
  1253. zoran_close_end_session(file);
  1254. if (zr->user-- == 1) { /* Last process */
  1255. /* Clean up JPEG process */
  1256. wake_up_interruptible(&zr->jpg_capq);
  1257. zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
  1258. zr->jpg_buffers.allocated = 0;
  1259. zr->jpg_buffers.active = ZORAN_FREE;
  1260. /* disable interrupts */
  1261. btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
  1262. if (*zr_debug > 1)
  1263. print_interrupts(zr);
  1264. /* Overlay off */
  1265. zr->v4l_overlay_active = 0;
  1266. zr36057_overlay(zr, 0);
  1267. zr->overlay_mask = NULL;
  1268. /* capture off */
  1269. wake_up_interruptible(&zr->v4l_capq);
  1270. zr36057_set_memgrab(zr, 0);
  1271. zr->v4l_buffers.allocated = 0;
  1272. zr->v4l_buffers.active = ZORAN_FREE;
  1273. zoran_set_pci_master(zr, 0);
  1274. if (!pass_through) { /* Switch to color bar */
  1275. int zero = 0, two = 2;
  1276. decoder_command(zr, DECODER_ENABLE_OUTPUT, &zero);
  1277. encoder_command(zr, ENCODER_SET_INPUT, &two);
  1278. }
  1279. }
  1280. file->private_data = NULL;
  1281. kfree(fh->overlay_mask);
  1282. kfree(fh);
  1283. /* release locks on the i2c modules */
  1284. module_put(zr->decoder->driver->driver.owner);
  1285. if (zr->encoder) {
  1286. module_put(zr->encoder->driver->driver.owner);
  1287. }
  1288. module_put(THIS_MODULE);
  1289. /*up(&zr->resource_lock);*/
  1290. dprintk(4, KERN_INFO "%s: zoran_close() done\n", ZR_DEVNAME(zr));
  1291. return 0;
  1292. }
  1293. static ssize_t
  1294. zoran_read (struct file *file,
  1295. char __user *data,
  1296. size_t count,
  1297. loff_t *ppos)
  1298. {
  1299. /* we simply don't support read() (yet)... */
  1300. return -EINVAL;
  1301. }
  1302. static ssize_t
  1303. zoran_write (struct file *file,
  1304. const char __user *data,
  1305. size_t count,
  1306. loff_t *ppos)
  1307. {
  1308. /* ...and the same goes for write() */
  1309. return -EINVAL;
  1310. }
  1311. static int
  1312. setup_fbuffer (struct file *file,
  1313. void *base,
  1314. const struct zoran_format *fmt,
  1315. int width,
  1316. int height,
  1317. int bytesperline)
  1318. {
  1319. struct zoran_fh *fh = file->private_data;
  1320. struct zoran *zr = fh->zr;
  1321. /* (Ronald) v4l/v4l2 guidelines */
  1322. if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
  1323. return -EPERM;
  1324. /* we need a bytesperline value, even if not given */
  1325. if (!bytesperline)
  1326. bytesperline = width * ((fmt->depth + 7) & ~7) / 8;
  1327. #if 0
  1328. if (zr->overlay_active) {
  1329. /* dzjee... stupid users... don't even bother to turn off
  1330. * overlay before changing the memory location...
  1331. * normally, we would return errors here. However, one of
  1332. * the tools that does this is... xawtv! and since xawtv
  1333. * is used by +/- 99% of the users, we'd rather be user-
  1334. * friendly and silently do as if nothing went wrong */
  1335. dprintk(3,
  1336. KERN_ERR
  1337. "%s: setup_fbuffer() - forced overlay turnoff because framebuffer changed\n",
  1338. ZR_DEVNAME(zr));
  1339. zr36057_overlay(zr, 0);
  1340. }
  1341. #endif
  1342. if (!(fmt->flags & ZORAN_FORMAT_OVERLAY)) {
  1343. dprintk(1,
  1344. KERN_ERR
  1345. "%s: setup_fbuffer() - no valid overlay format given\n",
  1346. ZR_DEVNAME(zr));
  1347. return -EINVAL;
  1348. }
  1349. if (height <= 0 || width <= 0 || bytesperline <= 0) {
  1350. dprintk(1,
  1351. KERN_ERR
  1352. "%s: setup_fbuffer() - invalid height/width/bpl value (%d|%d|%d)\n",
  1353. ZR_DEVNAME(zr), width, height, bytesperline);
  1354. return -EINVAL;
  1355. }
  1356. if (bytesperline & 3) {
  1357. dprintk(1,
  1358. KERN_ERR
  1359. "%s: setup_fbuffer() - bytesperline (%d) must be 4-byte aligned\n",
  1360. ZR_DEVNAME(zr), bytesperline);
  1361. return -EINVAL;
  1362. }
  1363. zr->buffer.base = (void *) ((unsigned long) base & ~3);
  1364. zr->buffer.height = height;
  1365. zr->buffer.width = width;
  1366. zr->buffer.depth = fmt->depth;
  1367. zr->overlay_settings.format = fmt;
  1368. zr->buffer.bytesperline = bytesperline;
  1369. /* The user should set new window parameters */
  1370. zr->overlay_settings.is_set = 0;
  1371. return 0;
  1372. }
  1373. static int
  1374. setup_window (struct file *file,
  1375. int x,
  1376. int y,
  1377. int width,
  1378. int height,
  1379. struct video_clip __user *clips,
  1380. int clipcount,
  1381. void __user *bitmap)
  1382. {
  1383. struct zoran_fh *fh = file->private_data;
  1384. struct zoran *zr = fh->zr;
  1385. struct video_clip *vcp = NULL;
  1386. int on, end;
  1387. if (!zr->buffer.base) {
  1388. dprintk(1,
  1389. KERN_ERR
  1390. "%s: setup_window() - frame buffer has to be set first\n",
  1391. ZR_DEVNAME(zr));
  1392. return -EINVAL;
  1393. }
  1394. if (!fh->overlay_settings.format) {
  1395. dprintk(1,
  1396. KERN_ERR
  1397. "%s: setup_window() - no overlay format set\n",
  1398. ZR_DEVNAME(zr));
  1399. return -EINVAL;
  1400. }
  1401. /*
  1402. * The video front end needs 4-byte alinged line sizes, we correct that
  1403. * silently here if necessary
  1404. */
  1405. if (zr->buffer.depth == 15 || zr->buffer.depth == 16) {
  1406. end = (x + width) & ~1; /* round down */
  1407. x = (x + 1) & ~1; /* round up */
  1408. width = end - x;
  1409. }
  1410. if (zr->buffer.depth == 24) {
  1411. end = (x + width) & ~3; /* round down */
  1412. x = (x + 3) & ~3; /* round up */
  1413. width = end - x;
  1414. }
  1415. if (width > BUZ_MAX_WIDTH)
  1416. width = BUZ_MAX_WIDTH;
  1417. if (height > BUZ_MAX_HEIGHT)
  1418. height = BUZ_MAX_HEIGHT;
  1419. /* Check for vaild parameters */
  1420. if (width < BUZ_MIN_WIDTH || height < BUZ_MIN_HEIGHT ||
  1421. width > BUZ_MAX_WIDTH || height > BUZ_MAX_HEIGHT) {
  1422. dprintk(1,
  1423. KERN_ERR
  1424. "%s: setup_window() - width = %d or height = %d invalid\n",
  1425. ZR_DEVNAME(zr), width, height);
  1426. return -EINVAL;
  1427. }
  1428. fh->overlay_settings.x = x;
  1429. fh->overlay_settings.y = y;
  1430. fh->overlay_settings.width = width;
  1431. fh->overlay_settings.height = height;
  1432. fh->overlay_settings.clipcount = clipcount;
  1433. /*
  1434. * If an overlay is running, we have to switch it off
  1435. * and switch it on again in order to get the new settings in effect.
  1436. *
  1437. * We also want to avoid that the overlay mask is written
  1438. * when an overlay is running.
  1439. */
  1440. on = zr->v4l_overlay_active && !zr->v4l_memgrab_active &&
  1441. zr->overlay_active != ZORAN_FREE &&
  1442. fh->overlay_active != ZORAN_FREE;
  1443. if (on)
  1444. zr36057_overlay(zr, 0);
  1445. /*
  1446. * Write the overlay mask if clips are wanted.
  1447. * We prefer a bitmap.
  1448. */
  1449. if (bitmap) {
  1450. /* fake value - it just means we want clips */
  1451. fh->overlay_settings.clipcount = 1;
  1452. if (copy_from_user(fh->overlay_mask, bitmap,
  1453. (width * height + 7) / 8)) {
  1454. return -EFAULT;
  1455. }
  1456. } else if (clipcount > 0) {
  1457. /* write our own bitmap from the clips */
  1458. vcp = vmalloc(sizeof(struct video_clip) * (clipcount + 4));
  1459. if (vcp == NULL) {
  1460. dprintk(1,
  1461. KERN_ERR
  1462. "%s: setup_window() - Alloc of clip mask failed\n",
  1463. ZR_DEVNAME(zr));
  1464. return -ENOMEM;
  1465. }
  1466. if (copy_from_user
  1467. (vcp, clips, sizeof(struct video_clip) * clipcount)) {
  1468. vfree(vcp);
  1469. return -EFAULT;
  1470. }
  1471. write_overlay_mask(file, vcp, clipcount);
  1472. vfree(vcp);
  1473. }
  1474. fh->overlay_settings.is_set = 1;
  1475. if (fh->overlay_active != ZORAN_FREE &&
  1476. zr->overlay_active != ZORAN_FREE)
  1477. zr->overlay_settings = fh->overlay_settings;
  1478. if (on)
  1479. zr36057_overlay(zr, 1);
  1480. /* Make sure the changes come into effect */
  1481. return wait_grab_pending(zr);
  1482. }
  1483. static int
  1484. setup_overlay (struct file *file,
  1485. int on)
  1486. {
  1487. struct zoran_fh *fh = file->private_data;
  1488. struct zoran *zr = fh->zr;
  1489. /* If there is nothing to do, return immediatly */
  1490. if ((on && fh->overlay_active != ZORAN_FREE) ||
  1491. (!on && fh->overlay_active == ZORAN_FREE))
  1492. return 0;
  1493. /* check whether we're touching someone else's overlay */
  1494. if (on && zr->overlay_active != ZORAN_FREE &&
  1495. fh->overlay_active == ZORAN_FREE) {
  1496. dprintk(1,
  1497. KERN_ERR
  1498. "%s: setup_overlay() - overlay is already active for another session\n",
  1499. ZR_DEVNAME(zr));
  1500. return -EBUSY;
  1501. }
  1502. if (!on && zr->overlay_active != ZORAN_FREE &&
  1503. fh->overlay_active == ZORAN_FREE) {
  1504. dprintk(1,
  1505. KERN_ERR
  1506. "%s: setup_overlay() - you cannot cancel someone else's session\n",
  1507. ZR_DEVNAME(zr));
  1508. return -EPERM;
  1509. }
  1510. if (on == 0) {
  1511. zr->overlay_active = fh->overlay_active = ZORAN_FREE;
  1512. zr->v4l_overlay_active = 0;
  1513. /* When a grab is running, the video simply
  1514. * won't be switched on any more */
  1515. if (!zr->v4l_memgrab_active)
  1516. zr36057_overlay(zr, 0);
  1517. zr->overlay_mask = NULL;
  1518. } else {
  1519. if (!zr->buffer.base || !fh->overlay_settings.is_set) {
  1520. dprintk(1,
  1521. KERN_ERR
  1522. "%s: setup_overlay() - buffer or window not set\n",
  1523. ZR_DEVNAME(zr));
  1524. return -EINVAL;
  1525. }
  1526. if (!fh->overlay_settings.format) {
  1527. dprintk(1,
  1528. KERN_ERR
  1529. "%s: setup_overlay() - no overlay format set\n",
  1530. ZR_DEVNAME(zr));
  1531. return -EINVAL;
  1532. }
  1533. zr->overlay_active = fh->overlay_active = ZORAN_LOCKED;
  1534. zr->v4l_overlay_active = 1;
  1535. zr->overlay_mask = fh->overlay_mask;
  1536. zr->overlay_settings = fh->overlay_settings;
  1537. if (!zr->v4l_memgrab_active)
  1538. zr36057_overlay(zr, 1);
  1539. /* When a grab is running, the video will be
  1540. * switched on when grab is finished */
  1541. }
  1542. /* Make sure the changes come into effect */
  1543. return wait_grab_pending(zr);
  1544. }
  1545. #ifdef HAVE_V4L2
  1546. /* get the status of a buffer in the clients buffer queue */
  1547. static int
  1548. zoran_v4l2_buffer_status (struct file *file,
  1549. struct v4l2_buffer *buf,
  1550. int num)
  1551. {
  1552. struct zoran_fh *fh = file->private_data;
  1553. struct zoran *zr = fh->zr;
  1554. buf->flags = V4L2_BUF_FLAG_MAPPED;
  1555. switch (fh->map_mode) {
  1556. case ZORAN_MAP_MODE_RAW:
  1557. /* check range */
  1558. if (num < 0 || num >= fh->v4l_buffers.num_buffers ||
  1559. !fh->v4l_buffers.allocated) {
  1560. dprintk(1,
  1561. KERN_ERR
  1562. "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
  1563. ZR_DEVNAME(zr));
  1564. return -EINVAL;
  1565. }
  1566. buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1567. buf->length = fh->v4l_buffers.buffer_size;
  1568. /* get buffer */
  1569. buf->bytesused = fh->v4l_buffers.buffer[num].bs.length;
  1570. if (fh->v4l_buffers.buffer[num].state == BUZ_STATE_DONE ||
  1571. fh->v4l_buffers.buffer[num].state == BUZ_STATE_USER) {
  1572. buf->sequence = fh->v4l_buffers.buffer[num].bs.seq;
  1573. buf->flags |= V4L2_BUF_FLAG_DONE;
  1574. buf->timestamp =
  1575. fh->v4l_buffers.buffer[num].bs.timestamp;
  1576. } else {
  1577. buf->flags |= V4L2_BUF_FLAG_QUEUED;
  1578. }
  1579. if (fh->v4l_settings.height <= BUZ_MAX_HEIGHT / 2)
  1580. buf->field = V4L2_FIELD_TOP;
  1581. else
  1582. buf->field = V4L2_FIELD_INTERLACED;
  1583. break;
  1584. case ZORAN_MAP_MODE_JPG_REC:
  1585. case ZORAN_MAP_MODE_JPG_PLAY:
  1586. /* check range */
  1587. if (num < 0 || num >= fh->jpg_buffers.num_buffers ||
  1588. !fh->jpg_buffers.allocated) {
  1589. dprintk(1,
  1590. KERN_ERR
  1591. "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
  1592. ZR_DEVNAME(zr));
  1593. return -EINVAL;
  1594. }
  1595. buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
  1596. V4L2_BUF_TYPE_VIDEO_CAPTURE :
  1597. V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1598. buf->length = fh->jpg_buffers.buffer_size;
  1599. /* these variables are only written after frame has been captured */
  1600. if (fh->jpg_buffers.buffer[num].state == BUZ_STATE_DONE ||
  1601. fh->jpg_buffers.buffer[num].state == BUZ_STATE_USER) {
  1602. buf->sequence = fh->jpg_buffers.buffer[num].bs.seq;
  1603. buf->timestamp =
  1604. fh->jpg_buffers.buffer[num].bs.timestamp;
  1605. buf->bytesused =
  1606. fh->jpg_buffers.buffer[num].bs.length;
  1607. buf->flags |= V4L2_BUF_FLAG_DONE;
  1608. } else {
  1609. buf->flags |= V4L2_BUF_FLAG_QUEUED;
  1610. }
  1611. /* which fields are these? */
  1612. if (fh->jpg_settings.TmpDcm != 1)
  1613. buf->field =
  1614. fh->jpg_settings.
  1615. odd_even ? V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
  1616. else
  1617. buf->field =
  1618. fh->jpg_settings.
  1619. odd_even ? V4L2_FIELD_SEQ_TB :
  1620. V4L2_FIELD_SEQ_BT;
  1621. break;
  1622. default:
  1623. dprintk(5,
  1624. KERN_ERR
  1625. "%s: v4l2_buffer_status() - invalid buffer type|map_mode (%d|%d)\n",
  1626. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  1627. return -EINVAL;
  1628. }
  1629. buf->memory = V4L2_MEMORY_MMAP;
  1630. buf->index = num;
  1631. buf->m.offset = buf->length * num;
  1632. return 0;
  1633. }
  1634. #endif
  1635. static int
  1636. zoran_set_norm (struct zoran *zr,
  1637. int norm) /* VIDEO_MODE_* */
  1638. {
  1639. int norm_encoder, on;
  1640. if (zr->v4l_buffers.active != ZORAN_FREE ||
  1641. zr->jpg_buffers.active != ZORAN_FREE) {
  1642. dprintk(1,
  1643. KERN_WARNING
  1644. "%s: set_norm() called while in playback/capture mode\n",
  1645. ZR_DEVNAME(zr));
  1646. return -EBUSY;
  1647. }
  1648. if (lock_norm && norm != zr->norm) {
  1649. if (lock_norm > 1) {
  1650. dprintk(1,
  1651. KERN_WARNING
  1652. "%s: set_norm() - TV standard is locked, can not switch norm\n",
  1653. ZR_DEVNAME(zr));
  1654. return -EPERM;
  1655. } else {
  1656. dprintk(1,
  1657. KERN_WARNING
  1658. "%s: set_norm() - TV standard is locked, norm was not changed\n",
  1659. ZR_DEVNAME(zr));
  1660. norm = zr->norm;
  1661. }
  1662. }
  1663. if (norm != VIDEO_MODE_AUTO &&
  1664. (norm < 0 || norm >= zr->card.norms ||
  1665. !zr->card.tvn[norm])) {
  1666. dprintk(1,
  1667. KERN_ERR "%s: set_norm() - unsupported norm %d\n",
  1668. ZR_DEVNAME(zr), norm);
  1669. return -EINVAL;
  1670. }
  1671. if (norm == VIDEO_MODE_AUTO) {
  1672. int status;
  1673. /* if we have autodetect, ... */
  1674. struct video_decoder_capability caps;
  1675. decoder_command(zr, DECODER_GET_CAPABILITIES, &caps);
  1676. if (!(caps.flags & VIDEO_DECODER_AUTO)) {
  1677. dprintk(1, KERN_ERR "%s: norm=auto unsupported\n",
  1678. ZR_DEVNAME(zr));
  1679. return -EINVAL;
  1680. }
  1681. decoder_command(zr, DECODER_SET_NORM, &norm);
  1682. /* let changes come into effect */
  1683. ssleep(2);
  1684. decoder_command(zr, DECODER_GET_STATUS, &status);
  1685. if (!(status & DECODER_STATUS_GOOD)) {
  1686. dprintk(1,
  1687. KERN_ERR
  1688. "%s: set_norm() - no norm detected\n",
  1689. ZR_DEVNAME(zr));
  1690. /* reset norm */
  1691. decoder_command(zr, DECODER_SET_NORM, &zr->norm);
  1692. return -EIO;
  1693. }
  1694. if (status & DECODER_STATUS_NTSC)
  1695. norm = VIDEO_MODE_NTSC;
  1696. else if (status & DECODER_STATUS_SECAM)
  1697. norm = VIDEO_MODE_SECAM;
  1698. else
  1699. norm = VIDEO_MODE_PAL;
  1700. }
  1701. zr->timing = zr->card.tvn[norm];
  1702. norm_encoder = norm;
  1703. /* We switch overlay off and on since a change in the
  1704. * norm needs different VFE settings */
  1705. on = zr->overlay_active && !zr->v4l_memgrab_active;
  1706. if (on)
  1707. zr36057_overlay(zr, 0);
  1708. decoder_command(zr, DECODER_SET_NORM, &norm);
  1709. encoder_command(zr, ENCODER_SET_NORM, &norm_encoder);
  1710. if (on)
  1711. zr36057_overlay(zr, 1);
  1712. /* Make sure the changes come into effect */
  1713. zr->norm = norm;
  1714. return 0;
  1715. }
  1716. static int
  1717. zoran_set_input (struct zoran *zr,
  1718. int input)
  1719. {
  1720. int realinput;
  1721. if (input == zr->input) {
  1722. return 0;
  1723. }
  1724. if (zr->v4l_buffers.active != ZORAN_FREE ||
  1725. zr->jpg_buffers.active != ZORAN_FREE) {
  1726. dprintk(1,
  1727. KERN_WARNING
  1728. "%s: set_input() called while in playback/capture mode\n",
  1729. ZR_DEVNAME(zr));
  1730. return -EBUSY;
  1731. }
  1732. if (input < 0 || input >= zr->card.inputs) {
  1733. dprintk(1,
  1734. KERN_ERR
  1735. "%s: set_input() - unnsupported input %d\n",
  1736. ZR_DEVNAME(zr), input);
  1737. return -EINVAL;
  1738. }
  1739. realinput = zr->card.input[input].muxsel;
  1740. zr->input = input;
  1741. decoder_command(zr, DECODER_SET_INPUT, &realinput);
  1742. return 0;
  1743. }
  1744. /*
  1745. * ioctl routine
  1746. */
  1747. static int
  1748. zoran_do_ioctl (struct inode *inode,
  1749. struct file *file,
  1750. unsigned int cmd,
  1751. void *arg)
  1752. {
  1753. struct zoran_fh *fh = file->private_data;
  1754. struct zoran *zr = fh->zr;
  1755. /* CAREFUL: used in multiple places here */
  1756. struct zoran_jpg_settings settings;
  1757. /* we might have older buffers lying around... We don't want
  1758. * to wait, but we do want to try cleaning them up ASAP. So
  1759. * we try to obtain the lock and free them. If that fails, we
  1760. * don't do anything and wait for the next turn. In the end,
  1761. * zoran_close() or a new allocation will still free them...
  1762. * This is just a 'the sooner the better' extra 'feature'
  1763. *
  1764. * We don't free the buffers right on munmap() because that
  1765. * causes oopses (kfree() inside munmap() oopses for no
  1766. * apparent reason - it's also not reproduceable in any way,
  1767. * but moving the free code outside the munmap() handler fixes
  1768. * all this... If someone knows why, please explain me (Ronald)
  1769. */
  1770. if (!down_trylock(&zr->resource_lock)) {
  1771. /* we obtained it! Let's try to free some things */
  1772. if (fh->jpg_buffers.ready_to_be_freed)
  1773. jpg_fbuffer_free(file);
  1774. if (fh->v4l_buffers.ready_to_be_freed)
  1775. v4l_fbuffer_free(file);
  1776. up(&zr->resource_lock);
  1777. }
  1778. switch (cmd) {
  1779. case VIDIOCGCAP:
  1780. {
  1781. struct video_capability *vcap = arg;
  1782. dprintk(3, KERN_DEBUG "%s: VIDIOCGCAP\n", ZR_DEVNAME(zr));
  1783. memset(vcap, 0, sizeof(struct video_capability));
  1784. strncpy(vcap->name, ZR_DEVNAME(zr), sizeof(vcap->name));
  1785. vcap->type = ZORAN_VID_TYPE;
  1786. vcap->channels = zr->card.inputs;
  1787. vcap->audios = 0;
  1788. down(&zr->resource_lock);
  1789. vcap->maxwidth = BUZ_MAX_WIDTH;
  1790. vcap->maxheight = BUZ_MAX_HEIGHT;
  1791. vcap->minwidth = BUZ_MIN_WIDTH;
  1792. vcap->minheight = BUZ_MIN_HEIGHT;
  1793. up(&zr->resource_lock);
  1794. return 0;
  1795. }
  1796. break;
  1797. case VIDIOCGCHAN:
  1798. {
  1799. struct video_channel *vchan = arg;
  1800. int channel = vchan->channel;
  1801. dprintk(3, KERN_DEBUG "%s: VIDIOCGCHAN - channel=%d\n",
  1802. ZR_DEVNAME(zr), vchan->channel);
  1803. memset(vchan, 0, sizeof(struct video_channel));
  1804. if (channel > zr->card.inputs || channel < 0) {
  1805. dprintk(1,
  1806. KERN_ERR
  1807. "%s: VIDIOCGCHAN on not existing channel %d\n",
  1808. ZR_DEVNAME(zr), channel);
  1809. return -EINVAL;
  1810. }
  1811. strcpy(vchan->name, zr->card.input[channel].name);
  1812. vchan->tuners = 0;
  1813. vchan->flags = 0;
  1814. vchan->type = VIDEO_TYPE_CAMERA;
  1815. down(&zr->resource_lock);
  1816. vchan->norm = zr->norm;
  1817. up(&zr->resource_lock);
  1818. vchan->channel = channel;
  1819. return 0;
  1820. }
  1821. break;
  1822. /* RJ: the documentation at http://roadrunner.swansea.linux.org.uk/v4lapi.shtml says:
  1823. *
  1824. * * "The VIDIOCSCHAN ioctl takes an integer argument and switches the capture to this input."
  1825. * * ^^^^^^^
  1826. * * The famos BTTV driver has it implemented with a struct video_channel argument
  1827. * * and we follow it for compatibility reasons
  1828. * *
  1829. * * BTW: this is the only way the user can set the norm!
  1830. */
  1831. case VIDIOCSCHAN:
  1832. {
  1833. struct video_channel *vchan = arg;
  1834. int res;
  1835. dprintk(3,
  1836. KERN_DEBUG
  1837. "%s: VIDIOCSCHAN - channel=%d, norm=%d\n",
  1838. ZR_DEVNAME(zr), vchan->channel, vchan->norm);
  1839. down(&zr->resource_lock);
  1840. if ((res = zoran_set_input(zr, vchan->channel)))
  1841. goto schan_unlock_and_return;
  1842. if ((res = zoran_set_norm(zr, vchan->norm)))
  1843. goto schan_unlock_and_return;
  1844. /* Make sure the changes come into effect */
  1845. res = wait_grab_pending(zr);
  1846. schan_unlock_and_return:
  1847. up(&zr->resource_lock);
  1848. return res;
  1849. }
  1850. break;
  1851. case VIDIOCGPICT:
  1852. {
  1853. struct video_picture *vpict = arg;
  1854. dprintk(3, KERN_DEBUG "%s: VIDIOCGPICT\n", ZR_DEVNAME(zr));
  1855. memset(vpict, 0, sizeof(struct video_picture));
  1856. down(&zr->resource_lock);
  1857. vpict->hue = zr->hue;
  1858. vpict->brightness = zr->brightness;
  1859. vpict->contrast = zr->contrast;
  1860. vpict->colour = zr->saturation;
  1861. if (fh->overlay_settings.format) {
  1862. vpict->depth = fh->overlay_settings.format->depth;
  1863. vpict->palette = fh->overlay_settings.format->palette;
  1864. } else {
  1865. vpict->depth = 0;
  1866. }
  1867. up(&zr->resource_lock);
  1868. return 0;
  1869. }
  1870. break;
  1871. case VIDIOCSPICT:
  1872. {
  1873. struct video_picture *vpict = arg;
  1874. int i;
  1875. dprintk(3,
  1876. KERN_DEBUG
  1877. "%s: VIDIOCSPICT - bri=%d, hue=%d, col=%d, con=%d, dep=%d, pal=%d\n",
  1878. ZR_DEVNAME(zr), vpict->brightness, vpict->hue,
  1879. vpict->colour, vpict->contrast, vpict->depth,
  1880. vpict->palette);
  1881. for (i = 0; i < zoran_num_formats; i++) {
  1882. const struct zoran_format *fmt = &zoran_formats[i];
  1883. if (fmt->palette != -1 &&
  1884. fmt->flags & ZORAN_FORMAT_OVERLAY &&
  1885. fmt->palette == vpict->palette &&
  1886. fmt->depth == vpict->depth)
  1887. break;
  1888. }
  1889. if (i == zoran_num_formats) {
  1890. dprintk(1,
  1891. KERN_ERR
  1892. "%s: VIDIOCSPICT - Invalid palette %d\n",
  1893. ZR_DEVNAME(zr), vpict->palette);
  1894. return -EINVAL;
  1895. }
  1896. down(&zr->resource_lock);
  1897. decoder_command(zr, DECODER_SET_PICTURE, vpict);
  1898. zr->hue = vpict->hue;
  1899. zr->contrast = vpict->contrast;
  1900. zr->saturation = vpict->colour;
  1901. zr->brightness = vpict->brightness;
  1902. fh->overlay_settings.format = &zoran_formats[i];
  1903. up(&zr->resource_lock);
  1904. return 0;
  1905. }
  1906. break;
  1907. case VIDIOCCAPTURE:
  1908. {
  1909. int *on = arg, res;
  1910. dprintk(3, KERN_DEBUG "%s: VIDIOCCAPTURE - on=%d\n",
  1911. ZR_DEVNAME(zr), *on);
  1912. down(&zr->resource_lock);
  1913. res = setup_overlay(file, *on);
  1914. up(&zr->resource_lock);
  1915. return res;
  1916. }
  1917. break;
  1918. case VIDIOCGWIN:
  1919. {
  1920. struct video_window *vwin = arg;
  1921. dprintk(3, KERN_DEBUG "%s: VIDIOCGWIN\n", ZR_DEVNAME(zr));
  1922. memset(vwin, 0, sizeof(struct video_window));
  1923. down(&zr->resource_lock);
  1924. vwin->x = fh->overlay_settings.x;
  1925. vwin->y = fh->overlay_settings.y;
  1926. vwin->width = fh->overlay_settings.width;
  1927. vwin->height = fh->overlay_settings.height;
  1928. up(&zr->resource_lock);
  1929. vwin->clipcount = 0;
  1930. return 0;
  1931. }
  1932. break;
  1933. case VIDIOCSWIN:
  1934. {
  1935. struct video_window *vwin = arg;
  1936. int res;
  1937. dprintk(3,
  1938. KERN_DEBUG
  1939. "%s: VIDIOCSWIN - x=%d, y=%d, w=%d, h=%d, clipcount=%d\n",
  1940. ZR_DEVNAME(zr), vwin->x, vwin->y, vwin->width,
  1941. vwin->height, vwin->clipcount);
  1942. down(&zr->resource_lock);
  1943. res =
  1944. setup_window(file, vwin->x, vwin->y, vwin->width,
  1945. vwin->height, vwin->clips,
  1946. vwin->clipcount, NULL);
  1947. up(&zr->resource_lock);
  1948. return res;
  1949. }
  1950. break;
  1951. case VIDIOCGFBUF:
  1952. {
  1953. struct video_buffer *vbuf = arg;
  1954. dprintk(3, KERN_DEBUG "%s: VIDIOCGFBUF\n", ZR_DEVNAME(zr));
  1955. down(&zr->resource_lock);
  1956. *vbuf = zr->buffer;
  1957. up(&zr->resource_lock);
  1958. return 0;
  1959. }
  1960. break;
  1961. case VIDIOCSFBUF:
  1962. {
  1963. struct video_buffer *vbuf = arg;
  1964. int i, res = 0;
  1965. dprintk(3,
  1966. KERN_DEBUG
  1967. "%s: VIDIOCSFBUF - base=%p, w=%d, h=%d, depth=%d, bpl=%d\n",
  1968. ZR_DEVNAME(zr), vbuf->base, vbuf->width,
  1969. vbuf->height, vbuf->depth, vbuf->bytesperline);
  1970. for (i = 0; i < zoran_num_formats; i++)
  1971. if (zoran_formats[i].depth == vbuf->depth)
  1972. break;
  1973. if (i == zoran_num_formats) {
  1974. dprintk(1,
  1975. KERN_ERR
  1976. "%s: VIDIOCSFBUF - invalid fbuf depth %d\n",
  1977. ZR_DEVNAME(zr), vbuf->depth);
  1978. return -EINVAL;
  1979. }
  1980. down(&zr->resource_lock);
  1981. res =
  1982. setup_fbuffer(file, vbuf->base, &zoran_formats[i],
  1983. vbuf->width, vbuf->height,
  1984. vbuf->bytesperline);
  1985. up(&zr->resource_lock);
  1986. return res;
  1987. }
  1988. break;
  1989. case VIDIOCSYNC:
  1990. {
  1991. int *frame = arg, res;
  1992. dprintk(3, KERN_DEBUG "%s: VIDIOCSYNC - frame=%d\n",
  1993. ZR_DEVNAME(zr), *frame);
  1994. down(&zr->resource_lock);
  1995. res = v4l_sync(file, *frame);
  1996. up(&zr->resource_lock);
  1997. if (!res)
  1998. zr->v4l_sync_tail++;
  1999. return res;
  2000. }
  2001. break;
  2002. case VIDIOCMCAPTURE:
  2003. {
  2004. struct video_mmap *vmap = arg;
  2005. int res;
  2006. dprintk(3,
  2007. KERN_DEBUG
  2008. "%s: VIDIOCMCAPTURE - frame=%d, geom=%dx%d, fmt=%d\n",
  2009. ZR_DEVNAME(zr), vmap->frame, vmap->width, vmap->height,
  2010. vmap->format);
  2011. down(&zr->resource_lock);
  2012. res = v4l_grab(file, vmap);
  2013. up(&zr->resource_lock);
  2014. return res;
  2015. }
  2016. break;
  2017. case VIDIOCGMBUF:
  2018. {
  2019. struct video_mbuf *vmbuf = arg;
  2020. int i, res = 0;
  2021. dprintk(3, KERN_DEBUG "%s: VIDIOCGMBUF\n", ZR_DEVNAME(zr));
  2022. vmbuf->size =
  2023. fh->v4l_buffers.num_buffers *
  2024. fh->v4l_buffers.buffer_size;
  2025. vmbuf->frames = fh->v4l_buffers.num_buffers;
  2026. for (i = 0; i < vmbuf->frames; i++) {
  2027. vmbuf->offsets[i] =
  2028. i * fh->v4l_buffers.buffer_size;
  2029. }
  2030. down(&zr->resource_lock);
  2031. if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
  2032. dprintk(1,
  2033. KERN_ERR
  2034. "%s: VIDIOCGMBUF - buffers already allocated\n",
  2035. ZR_DEVNAME(zr));
  2036. res = -EINVAL;
  2037. goto v4l1reqbuf_unlock_and_return;
  2038. }
  2039. if (v4l_fbuffer_alloc(file)) {
  2040. res = -ENOMEM;
  2041. goto v4l1reqbuf_unlock_and_return;
  2042. }
  2043. /* The next mmap will map the V4L buffers */
  2044. fh->map_mode = ZORAN_MAP_MODE_RAW;
  2045. v4l1reqbuf_unlock_and_return:
  2046. up(&zr->resource_lock);
  2047. return res;
  2048. }
  2049. break;
  2050. case VIDIOCGUNIT:
  2051. {
  2052. struct video_unit *vunit = arg;
  2053. dprintk(3, KERN_DEBUG "%s: VIDIOCGUNIT\n", ZR_DEVNAME(zr));
  2054. vunit->video = zr->video_dev->minor;
  2055. vunit->vbi = VIDEO_NO_UNIT;
  2056. vunit->radio = VIDEO_NO_UNIT;
  2057. vunit->audio = VIDEO_NO_UNIT;
  2058. vunit->teletext = VIDEO_NO_UNIT;
  2059. return 0;
  2060. }
  2061. break;
  2062. /*
  2063. * RJ: In principal we could support subcaptures for V4L grabbing.
  2064. * Not even the famous BTTV driver has them, however.
  2065. * If there should be a strong demand, one could consider
  2066. * to implement them.
  2067. */
  2068. case VIDIOCGCAPTURE:
  2069. {
  2070. dprintk(3, KERN_ERR "%s: VIDIOCGCAPTURE not supported\n",
  2071. ZR_DEVNAME(zr));
  2072. return -EINVAL;
  2073. }
  2074. break;
  2075. case VIDIOCSCAPTURE:
  2076. {
  2077. dprintk(3, KERN_ERR "%s: VIDIOCSCAPTURE not supported\n",
  2078. ZR_DEVNAME(zr));
  2079. return -EINVAL;
  2080. }
  2081. break;
  2082. case BUZIOC_G_PARAMS:
  2083. {
  2084. struct zoran_params *bparams = arg;
  2085. dprintk(3, KERN_DEBUG "%s: BUZIOC_G_PARAMS\n", ZR_DEVNAME(zr));
  2086. memset(bparams, 0, sizeof(struct zoran_params));
  2087. bparams->major_version = MAJOR_VERSION;
  2088. bparams->minor_version = MINOR_VERSION;
  2089. down(&zr->resource_lock);
  2090. bparams->norm = zr->norm;
  2091. bparams->input = zr->input;
  2092. bparams->decimation = fh->jpg_settings.decimation;
  2093. bparams->HorDcm = fh->jpg_settings.HorDcm;
  2094. bparams->VerDcm = fh->jpg_settings.VerDcm;
  2095. bparams->TmpDcm = fh->jpg_settings.TmpDcm;
  2096. bparams->field_per_buff = fh->jpg_settings.field_per_buff;
  2097. bparams->img_x = fh->jpg_settings.img_x;
  2098. bparams->img_y = fh->jpg_settings.img_y;
  2099. bparams->img_width = fh->jpg_settings.img_width;
  2100. bparams->img_height = fh->jpg_settings.img_height;
  2101. bparams->odd_even = fh->jpg_settings.odd_even;
  2102. bparams->quality = fh->jpg_settings.jpg_comp.quality;
  2103. bparams->APPn = fh->jpg_settings.jpg_comp.APPn;
  2104. bparams->APP_len = fh->jpg_settings.jpg_comp.APP_len;
  2105. memcpy(bparams->APP_data,
  2106. fh->jpg_settings.jpg_comp.APP_data,
  2107. sizeof(bparams->APP_data));
  2108. bparams->COM_len = zr->jpg_settings.jpg_comp.COM_len;
  2109. memcpy(bparams->COM_data,
  2110. fh->jpg_settings.jpg_comp.COM_data,
  2111. sizeof(bparams->COM_data));
  2112. bparams->jpeg_markers =
  2113. fh->jpg_settings.jpg_comp.jpeg_markers;
  2114. up(&zr->resource_lock);
  2115. bparams->VFIFO_FB = 0;
  2116. return 0;
  2117. }
  2118. break;
  2119. case BUZIOC_S_PARAMS:
  2120. {
  2121. struct zoran_params *bparams = arg;
  2122. int res = 0;
  2123. dprintk(3, KERN_DEBUG "%s: BUZIOC_S_PARAMS\n", ZR_DEVNAME(zr));
  2124. settings.decimation = bparams->decimation;
  2125. settings.HorDcm = bparams->HorDcm;
  2126. settings.VerDcm = bparams->VerDcm;
  2127. settings.TmpDcm = bparams->TmpDcm;
  2128. settings.field_per_buff = bparams->field_per_buff;
  2129. settings.img_x = bparams->img_x;
  2130. settings.img_y = bparams->img_y;
  2131. settings.img_width = bparams->img_width;
  2132. settings.img_height = bparams->img_height;
  2133. settings.odd_even = bparams->odd_even;
  2134. settings.jpg_comp.quality = bparams->quality;
  2135. settings.jpg_comp.APPn = bparams->APPn;
  2136. settings.jpg_comp.APP_len = bparams->APP_len;
  2137. memcpy(settings.jpg_comp.APP_data, bparams->APP_data,
  2138. sizeof(bparams->APP_data));
  2139. settings.jpg_comp.COM_len = bparams->COM_len;
  2140. memcpy(settings.jpg_comp.COM_data, bparams->COM_data,
  2141. sizeof(bparams->COM_data));
  2142. settings.jpg_comp.jpeg_markers = bparams->jpeg_markers;
  2143. down(&zr->resource_lock);
  2144. if (zr->codec_mode != BUZ_MODE_IDLE) {
  2145. dprintk(1,
  2146. KERN_ERR
  2147. "%s: BUZIOC_S_PARAMS called, but Buz in capture/playback mode\n",
  2148. ZR_DEVNAME(zr));
  2149. res = -EINVAL;
  2150. goto sparams_unlock_and_return;
  2151. }
  2152. /* Check the params first before overwriting our
  2153. * nternal values */
  2154. if (zoran_check_jpg_settings(zr, &settings)) {
  2155. res = -EINVAL;
  2156. goto sparams_unlock_and_return;
  2157. }
  2158. fh->jpg_settings = settings;
  2159. sparams_unlock_and_return:
  2160. up(&zr->resource_lock);
  2161. return res;
  2162. }
  2163. break;
  2164. case BUZIOC_REQBUFS:
  2165. {
  2166. struct zoran_requestbuffers *breq = arg;
  2167. int res = 0;
  2168. dprintk(3,
  2169. KERN_DEBUG
  2170. "%s: BUZIOC_REQBUFS - count=%lu, size=%lu\n",
  2171. ZR_DEVNAME(zr), breq->count, breq->size);
  2172. /* Enforce reasonable lower and upper limits */
  2173. if (breq->count < 4)
  2174. breq->count = 4; /* Could be choosen smaller */
  2175. if (breq->count > jpg_nbufs)
  2176. breq->count = jpg_nbufs;
  2177. breq->size = PAGE_ALIGN(breq->size);
  2178. if (breq->size < 8192)
  2179. breq->size = 8192; /* Arbitrary */
  2180. /* breq->size is limited by 1 page for the stat_com
  2181. * tables to a Maximum of 2 MB */
  2182. if (breq->size > jpg_bufsize)
  2183. breq->size = jpg_bufsize;
  2184. if (fh->jpg_buffers.need_contiguous &&
  2185. breq->size > MAX_KMALLOC_MEM)
  2186. breq->size = MAX_KMALLOC_MEM;
  2187. down(&zr->resource_lock);
  2188. if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
  2189. dprintk(1,
  2190. KERN_ERR
  2191. "%s: BUZIOC_REQBUFS - buffers allready allocated\n",
  2192. ZR_DEVNAME(zr));
  2193. res = -EBUSY;
  2194. goto jpgreqbuf_unlock_and_return;
  2195. }
  2196. fh->jpg_buffers.num_buffers = breq->count;
  2197. fh->jpg_buffers.buffer_size = breq->size;
  2198. if (jpg_fbuffer_alloc(file)) {
  2199. res = -ENOMEM;
  2200. goto jpgreqbuf_unlock_and_return;
  2201. }
  2202. /* The next mmap will map the MJPEG buffers - could
  2203. * also be *_PLAY, but it doesn't matter here */
  2204. fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
  2205. jpgreqbuf_unlock_and_return:
  2206. up(&zr->resource_lock);
  2207. return res;
  2208. }
  2209. break;
  2210. case BUZIOC_QBUF_CAPT:
  2211. {
  2212. int *frame = arg, res;
  2213. dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_CAPT - frame=%d\n",
  2214. ZR_DEVNAME(zr), *frame);
  2215. down(&zr->resource_lock);
  2216. res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_COMPRESS);
  2217. up(&zr->resource_lock);
  2218. return res;
  2219. }
  2220. break;
  2221. case BUZIOC_QBUF_PLAY:
  2222. {
  2223. int *frame = arg, res;
  2224. dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_PLAY - frame=%d\n",
  2225. ZR_DEVNAME(zr), *frame);
  2226. down(&zr->resource_lock);
  2227. res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_DECOMPRESS);
  2228. up(&zr->resource_lock);
  2229. return res;
  2230. }
  2231. break;
  2232. case BUZIOC_SYNC:
  2233. {
  2234. struct zoran_sync *bsync = arg;
  2235. int res;
  2236. dprintk(3, KERN_DEBUG "%s: BUZIOC_SYNC\n", ZR_DEVNAME(zr));
  2237. down(&zr->resource_lock);
  2238. res = jpg_sync(file, bsync);
  2239. up(&zr->resource_lock);
  2240. return res;
  2241. }
  2242. break;
  2243. case BUZIOC_G_STATUS:
  2244. {
  2245. struct zoran_status *bstat = arg;
  2246. int norm, input, status, res = 0;
  2247. dprintk(3, KERN_DEBUG "%s: BUZIOC_G_STATUS\n", ZR_DEVNAME(zr));
  2248. if (zr->codec_mode != BUZ_MODE_IDLE) {
  2249. dprintk(1,
  2250. KERN_ERR
  2251. "%s: BUZIOC_G_STATUS called but Buz in capture/playback mode\n",
  2252. ZR_DEVNAME(zr));
  2253. return -EINVAL;
  2254. }
  2255. input = zr->card.input[bstat->input].muxsel;
  2256. norm = VIDEO_MODE_AUTO;
  2257. down(&zr->resource_lock);
  2258. if (zr->codec_mode != BUZ_MODE_IDLE) {
  2259. dprintk(1,
  2260. KERN_ERR
  2261. "%s: BUZIOC_G_STATUS called, but Buz in capture/playback mode\n",
  2262. ZR_DEVNAME(zr));
  2263. res = -EINVAL;
  2264. goto gstat_unlock_and_return;
  2265. }
  2266. decoder_command(zr, DECODER_SET_INPUT, &input);
  2267. decoder_command(zr, DECODER_SET_NORM, &norm);
  2268. /* sleep 1 second */
  2269. ssleep(1);
  2270. /* Get status of video decoder */
  2271. decoder_command(zr, DECODER_GET_STATUS, &status);
  2272. /* restore previous input and norm */
  2273. input = zr->card.input[zr->input].muxsel;
  2274. decoder_command(zr, DECODER_SET_INPUT, &input);
  2275. decoder_command(zr, DECODER_SET_NORM, &zr->norm);
  2276. gstat_unlock_and_return:
  2277. up(&zr->resource_lock);
  2278. if (!res) {
  2279. bstat->signal =
  2280. (status & DECODER_STATUS_GOOD) ? 1 : 0;
  2281. if (status & DECODER_STATUS_NTSC)
  2282. bstat->norm = VIDEO_MODE_NTSC;
  2283. else if (status & DECODER_STATUS_SECAM)
  2284. bstat->norm = VIDEO_MODE_SECAM;
  2285. else
  2286. bstat->norm = VIDEO_MODE_PAL;
  2287. bstat->color =
  2288. (status & DECODER_STATUS_COLOR) ? 1 : 0;
  2289. }
  2290. return res;
  2291. }
  2292. break;
  2293. #ifdef HAVE_V4L2
  2294. /* The new video4linux2 capture interface - much nicer than video4linux1, since
  2295. * it allows for integrating the JPEG capturing calls inside standard v4l2
  2296. */
  2297. case VIDIOC_QUERYCAP:
  2298. {
  2299. struct v4l2_capability *cap = arg;
  2300. dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCAP\n", ZR_DEVNAME(zr));
  2301. memset(cap, 0, sizeof(*cap));
  2302. strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card));
  2303. strncpy(cap->driver, "zoran", sizeof(cap->driver));
  2304. snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
  2305. pci_name(zr->pci_dev));
  2306. cap->version =
  2307. KERNEL_VERSION(MAJOR_VERSION, MINOR_VERSION,
  2308. RELEASE_VERSION);
  2309. cap->capabilities = ZORAN_V4L2_VID_FLAGS;
  2310. return 0;
  2311. }
  2312. break;
  2313. case VIDIOC_ENUM_FMT:
  2314. {
  2315. struct v4l2_fmtdesc *fmt = arg;
  2316. int index = fmt->index, num = -1, i, flag = 0, type =
  2317. fmt->type;
  2318. dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUM_FMT - index=%d\n",
  2319. ZR_DEVNAME(zr), fmt->index);
  2320. switch (fmt->type) {
  2321. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  2322. flag = ZORAN_FORMAT_CAPTURE;
  2323. break;
  2324. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  2325. flag = ZORAN_FORMAT_PLAYBACK;
  2326. break;
  2327. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2328. flag = ZORAN_FORMAT_OVERLAY;
  2329. break;
  2330. default:
  2331. dprintk(1,
  2332. KERN_ERR
  2333. "%s: VIDIOC_ENUM_FMT - unknown type %d\n",
  2334. ZR_DEVNAME(zr), fmt->type);
  2335. return -EINVAL;
  2336. }
  2337. for (i = 0; i < zoran_num_formats; i++) {
  2338. if (zoran_formats[i].flags & flag)
  2339. num++;
  2340. if (num == fmt->index)
  2341. break;
  2342. }
  2343. if (fmt->index < 0 /* late, but not too late */ ||
  2344. i == zoran_num_formats)
  2345. return -EINVAL;
  2346. memset(fmt, 0, sizeof(*fmt));
  2347. fmt->index = index;
  2348. fmt->type = type;
  2349. strncpy(fmt->description, zoran_formats[i].name, 31);
  2350. fmt->pixelformat = zoran_formats[i].fourcc;
  2351. if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
  2352. fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
  2353. return 0;
  2354. }
  2355. break;
  2356. case VIDIOC_G_FMT:
  2357. {
  2358. struct v4l2_format *fmt = arg;
  2359. int type = fmt->type;
  2360. dprintk(5, KERN_DEBUG "%s: VIDIOC_G_FMT\n", ZR_DEVNAME(zr));
  2361. memset(fmt, 0, sizeof(*fmt));
  2362. fmt->type = type;
  2363. switch (fmt->type) {
  2364. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2365. down(&zr->resource_lock);
  2366. fmt->fmt.win.w.left = fh->overlay_settings.x;
  2367. fmt->fmt.win.w.top = fh->overlay_settings.y;
  2368. fmt->fmt.win.w.width = fh->overlay_settings.width;
  2369. fmt->fmt.win.w.height =
  2370. fh->overlay_settings.height;
  2371. if (fh->overlay_settings.width * 2 >
  2372. BUZ_MAX_HEIGHT)
  2373. fmt->fmt.win.field = V4L2_FIELD_INTERLACED;
  2374. else
  2375. fmt->fmt.win.field = V4L2_FIELD_TOP;
  2376. up(&zr->resource_lock);
  2377. break;
  2378. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  2379. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  2380. down(&zr->resource_lock);
  2381. if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  2382. fh->map_mode == ZORAN_MAP_MODE_RAW) {
  2383. fmt->fmt.pix.width =
  2384. fh->v4l_settings.width;
  2385. fmt->fmt.pix.height =
  2386. fh->v4l_settings.height;
  2387. fmt->fmt.pix.sizeimage =
  2388. fh->v4l_buffers.buffer_size;
  2389. fmt->fmt.pix.pixelformat =
  2390. fh->v4l_settings.format->fourcc;
  2391. fmt->fmt.pix.colorspace =
  2392. fh->v4l_settings.format->colorspace;
  2393. fmt->fmt.pix.bytesperline = 0;
  2394. if (BUZ_MAX_HEIGHT <
  2395. (fh->v4l_settings.height * 2))
  2396. fmt->fmt.pix.field =
  2397. V4L2_FIELD_INTERLACED;
  2398. else
  2399. fmt->fmt.pix.field =
  2400. V4L2_FIELD_TOP;
  2401. } else {
  2402. fmt->fmt.pix.width =
  2403. fh->jpg_settings.img_width /
  2404. fh->jpg_settings.HorDcm;
  2405. fmt->fmt.pix.height =
  2406. fh->jpg_settings.img_height /
  2407. (fh->jpg_settings.VerDcm *
  2408. fh->jpg_settings.TmpDcm);
  2409. fmt->fmt.pix.sizeimage =
  2410. zoran_v4l2_calc_bufsize(&fh->
  2411. jpg_settings);
  2412. fmt->fmt.pix.pixelformat =
  2413. V4L2_PIX_FMT_MJPEG;
  2414. if (fh->jpg_settings.TmpDcm == 1)
  2415. fmt->fmt.pix.field =
  2416. (fh->jpg_settings.
  2417. odd_even ? V4L2_FIELD_SEQ_BT :
  2418. V4L2_FIELD_SEQ_BT);
  2419. else
  2420. fmt->fmt.pix.field =
  2421. (fh->jpg_settings.
  2422. odd_even ? V4L2_FIELD_TOP :
  2423. V4L2_FIELD_BOTTOM);
  2424. fmt->fmt.pix.bytesperline = 0;
  2425. fmt->fmt.pix.colorspace =
  2426. V4L2_COLORSPACE_SMPTE170M;
  2427. }
  2428. up(&zr->resource_lock);
  2429. break;
  2430. default:
  2431. dprintk(1,
  2432. KERN_ERR
  2433. "%s: VIDIOC_G_FMT - unsupported type %d\n",
  2434. ZR_DEVNAME(zr), fmt->type);
  2435. return -EINVAL;
  2436. }
  2437. return 0;
  2438. }
  2439. break;
  2440. case VIDIOC_S_FMT:
  2441. {
  2442. struct v4l2_format *fmt = arg;
  2443. int i, res = 0;
  2444. __u32 printformat;
  2445. dprintk(3, KERN_DEBUG "%s: VIDIOC_S_FMT - type=%d, ",
  2446. ZR_DEVNAME(zr), fmt->type);
  2447. switch (fmt->type) {
  2448. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2449. dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n",
  2450. fmt->fmt.win.w.left, fmt->fmt.win.w.top,
  2451. fmt->fmt.win.w.width,
  2452. fmt->fmt.win.w.height,
  2453. fmt->fmt.win.clipcount,
  2454. fmt->fmt.win.bitmap);
  2455. down(&zr->resource_lock);
  2456. res =
  2457. setup_window(file, fmt->fmt.win.w.left,
  2458. fmt->fmt.win.w.top,
  2459. fmt->fmt.win.w.width,
  2460. fmt->fmt.win.w.height,
  2461. (struct video_clip __user *)
  2462. fmt->fmt.win.clips,
  2463. fmt->fmt.win.clipcount,
  2464. fmt->fmt.win.bitmap);
  2465. up(&zr->resource_lock);
  2466. return res;
  2467. break;
  2468. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  2469. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  2470. printformat =
  2471. __cpu_to_le32(fmt->fmt.pix.pixelformat);
  2472. dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n",
  2473. fmt->fmt.pix.width, fmt->fmt.pix.height,
  2474. fmt->fmt.pix.pixelformat,
  2475. (char *) &printformat);
  2476. if (fmt->fmt.pix.bytesperline > 0) {
  2477. dprintk(5,
  2478. KERN_ERR "%s: bpl not supported\n",
  2479. ZR_DEVNAME(zr));
  2480. return -EINVAL;
  2481. }
  2482. /* we can be requested to do JPEG/raw playback/capture */
  2483. if (!
  2484. (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  2485. (fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  2486. fmt->fmt.pix.pixelformat ==
  2487. V4L2_PIX_FMT_MJPEG))) {
  2488. dprintk(1,
  2489. KERN_ERR
  2490. "%s: VIDIOC_S_FMT - unknown type %d/0x%x(%4.4s) combination\n",
  2491. ZR_DEVNAME(zr), fmt->type,
  2492. fmt->fmt.pix.pixelformat,
  2493. (char *) &printformat);
  2494. return -EINVAL;
  2495. }
  2496. if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
  2497. down(&zr->resource_lock);
  2498. settings = fh->jpg_settings;
  2499. if (fh->v4l_buffers.allocated ||
  2500. fh->jpg_buffers.allocated) {
  2501. dprintk(1,
  2502. KERN_ERR
  2503. "%s: VIDIOC_S_FMT - cannot change capture mode\n",
  2504. ZR_DEVNAME(zr));
  2505. res = -EBUSY;
  2506. goto sfmtjpg_unlock_and_return;
  2507. }
  2508. /* we actually need to set 'real' parameters now */
  2509. if ((fmt->fmt.pix.height * 2) >
  2510. BUZ_MAX_HEIGHT)
  2511. settings.TmpDcm = 1;
  2512. else
  2513. settings.TmpDcm = 2;
  2514. settings.decimation = 0;
  2515. if (fmt->fmt.pix.height <=
  2516. fh->jpg_settings.img_height / 2)
  2517. settings.VerDcm = 2;
  2518. else
  2519. settings.VerDcm = 1;
  2520. if (fmt->fmt.pix.width <=
  2521. fh->jpg_settings.img_width / 4)
  2522. settings.HorDcm = 4;
  2523. else if (fmt->fmt.pix.width <=
  2524. fh->jpg_settings.img_width / 2)
  2525. settings.HorDcm = 2;
  2526. else
  2527. settings.HorDcm = 1;
  2528. if (settings.TmpDcm == 1)
  2529. settings.field_per_buff = 2;
  2530. else
  2531. settings.field_per_buff = 1;
  2532. /* check */
  2533. if ((res =
  2534. zoran_check_jpg_settings(zr,
  2535. &settings)))
  2536. goto sfmtjpg_unlock_and_return;
  2537. /* it's ok, so set them */
  2538. fh->jpg_settings = settings;
  2539. /* tell the user what we actually did */
  2540. fmt->fmt.pix.width =
  2541. settings.img_width / settings.HorDcm;
  2542. fmt->fmt.pix.height =
  2543. settings.img_height * 2 /
  2544. (settings.TmpDcm * settings.VerDcm);
  2545. if (settings.TmpDcm == 1)
  2546. fmt->fmt.pix.field =
  2547. (fh->jpg_settings.
  2548. odd_even ? V4L2_FIELD_SEQ_TB :
  2549. V4L2_FIELD_SEQ_BT);
  2550. else
  2551. fmt->fmt.pix.field =
  2552. (fh->jpg_settings.
  2553. odd_even ? V4L2_FIELD_TOP :
  2554. V4L2_FIELD_BOTTOM);
  2555. fh->jpg_buffers.buffer_size =
  2556. zoran_v4l2_calc_bufsize(&fh->
  2557. jpg_settings);
  2558. fmt->fmt.pix.sizeimage =
  2559. fh->jpg_buffers.buffer_size;
  2560. /* we hereby abuse this variable to show that
  2561. * we're gonna do mjpeg capture */
  2562. fh->map_mode =
  2563. (fmt->type ==
  2564. V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
  2565. ZORAN_MAP_MODE_JPG_REC :
  2566. ZORAN_MAP_MODE_JPG_PLAY;
  2567. sfmtjpg_unlock_and_return:
  2568. up(&zr->resource_lock);
  2569. } else {
  2570. for (i = 0; i < zoran_num_formats; i++)
  2571. if (fmt->fmt.pix.pixelformat ==
  2572. zoran_formats[i].fourcc)
  2573. break;
  2574. if (i == zoran_num_formats) {
  2575. dprintk(1,
  2576. KERN_ERR
  2577. "%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x (%4.4s)\n",
  2578. ZR_DEVNAME(zr),
  2579. fmt->fmt.pix.pixelformat,
  2580. (char *) &printformat);
  2581. return -EINVAL;
  2582. }
  2583. down(&zr->resource_lock);
  2584. if (fh->jpg_buffers.allocated ||
  2585. (fh->v4l_buffers.allocated &&
  2586. fh->v4l_buffers.active !=
  2587. ZORAN_FREE)) {
  2588. dprintk(1,
  2589. KERN_ERR
  2590. "%s: VIDIOC_S_FMT - cannot change capture mode\n",
  2591. ZR_DEVNAME(zr));
  2592. res = -EBUSY;
  2593. goto sfmtv4l_unlock_and_return;
  2594. }
  2595. if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
  2596. fmt->fmt.pix.height =
  2597. BUZ_MAX_HEIGHT;
  2598. if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
  2599. fmt->fmt.pix.width = BUZ_MAX_WIDTH;
  2600. if ((res =
  2601. zoran_v4l_set_format(file,
  2602. fmt->fmt.pix.
  2603. width,
  2604. fmt->fmt.pix.
  2605. height,
  2606. &zoran_formats
  2607. [i])))
  2608. goto sfmtv4l_unlock_and_return;
  2609. /* tell the user the
  2610. * results/missing stuff */
  2611. fmt->fmt.pix.sizeimage = fh->v4l_buffers.buffer_size /*zr->gbpl * zr->gheight */
  2612. ;
  2613. if (BUZ_MAX_HEIGHT <
  2614. (fh->v4l_settings.height * 2))
  2615. fmt->fmt.pix.field =
  2616. V4L2_FIELD_INTERLACED;
  2617. else
  2618. fmt->fmt.pix.field =
  2619. V4L2_FIELD_TOP;
  2620. fh->map_mode = ZORAN_MAP_MODE_RAW;
  2621. sfmtv4l_unlock_and_return:
  2622. up(&zr->resource_lock);
  2623. }
  2624. break;
  2625. default:
  2626. dprintk(3, "unsupported\n");
  2627. dprintk(1,
  2628. KERN_ERR
  2629. "%s: VIDIOC_S_FMT - unsupported type %d\n",
  2630. ZR_DEVNAME(zr), fmt->type);
  2631. return -EINVAL;
  2632. }
  2633. return res;
  2634. }
  2635. break;
  2636. case VIDIOC_G_FBUF:
  2637. {
  2638. struct v4l2_framebuffer *fb = arg;
  2639. dprintk(3, KERN_DEBUG "%s: VIDIOC_G_FBUF\n", ZR_DEVNAME(zr));
  2640. memset(fb, 0, sizeof(*fb));
  2641. down(&zr->resource_lock);
  2642. fb->base = zr->buffer.base;
  2643. fb->fmt.width = zr->buffer.width;
  2644. fb->fmt.height = zr->buffer.height;
  2645. if (zr->overlay_settings.format) {
  2646. fb->fmt.pixelformat =
  2647. fh->overlay_settings.format->fourcc;
  2648. }
  2649. fb->fmt.bytesperline = zr->buffer.bytesperline;
  2650. up(&zr->resource_lock);
  2651. fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
  2652. fb->fmt.field = V4L2_FIELD_INTERLACED;
  2653. fb->flags = V4L2_FBUF_FLAG_OVERLAY;
  2654. fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
  2655. return 0;
  2656. }
  2657. break;
  2658. case VIDIOC_S_FBUF:
  2659. {
  2660. int i, res = 0;
  2661. struct v4l2_framebuffer *fb = arg;
  2662. __u32 printformat = __cpu_to_le32(fb->fmt.pixelformat);
  2663. dprintk(3,
  2664. KERN_DEBUG
  2665. "%s: VIDIOC_S_FBUF - base=0x%p, size=%dx%d, bpl=%d, fmt=0x%x (%4.4s)\n",
  2666. ZR_DEVNAME(zr), fb->base, fb->fmt.width, fb->fmt.height,
  2667. fb->fmt.bytesperline, fb->fmt.pixelformat,
  2668. (char *) &printformat);
  2669. for (i = 0; i < zoran_num_formats; i++)
  2670. if (zoran_formats[i].fourcc == fb->fmt.pixelformat)
  2671. break;
  2672. if (i == zoran_num_formats) {
  2673. dprintk(1,
  2674. KERN_ERR
  2675. "%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
  2676. ZR_DEVNAME(zr), fb->fmt.pixelformat,
  2677. (char *) &printformat);
  2678. return -EINVAL;
  2679. }
  2680. down(&zr->resource_lock);
  2681. res =
  2682. setup_fbuffer(file, fb->base, &zoran_formats[i],
  2683. fb->fmt.width, fb->fmt.height,
  2684. fb->fmt.bytesperline);
  2685. up(&zr->resource_lock);
  2686. return res;
  2687. }
  2688. break;
  2689. case VIDIOC_OVERLAY:
  2690. {
  2691. int *on = arg, res;
  2692. dprintk(3, KERN_DEBUG "%s: VIDIOC_PREVIEW - on=%d\n",
  2693. ZR_DEVNAME(zr), *on);
  2694. down(&zr->resource_lock);
  2695. res = setup_overlay(file, *on);
  2696. up(&zr->resource_lock);
  2697. return res;
  2698. }
  2699. break;
  2700. case VIDIOC_REQBUFS:
  2701. {
  2702. struct v4l2_requestbuffers *req = arg;
  2703. int res = 0;
  2704. dprintk(3, KERN_DEBUG "%s: VIDIOC_REQBUFS - type=%d\n",
  2705. ZR_DEVNAME(zr), req->type);
  2706. if (req->memory != V4L2_MEMORY_MMAP) {
  2707. dprintk(1,
  2708. KERN_ERR
  2709. "%s: only MEMORY_MMAP capture is supported, not %d\n",
  2710. ZR_DEVNAME(zr), req->memory);
  2711. return -EINVAL;
  2712. }
  2713. down(&zr->resource_lock);
  2714. if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
  2715. dprintk(1,
  2716. KERN_ERR
  2717. "%s: VIDIOC_REQBUFS - buffers allready allocated\n",
  2718. ZR_DEVNAME(zr));
  2719. res = -EBUSY;
  2720. goto v4l2reqbuf_unlock_and_return;
  2721. }
  2722. if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
  2723. req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  2724. /* control user input */
  2725. if (req->count < 2)
  2726. req->count = 2;
  2727. if (req->count > v4l_nbufs)
  2728. req->count = v4l_nbufs;
  2729. fh->v4l_buffers.num_buffers = req->count;
  2730. if (v4l_fbuffer_alloc(file)) {
  2731. res = -ENOMEM;
  2732. goto v4l2reqbuf_unlock_and_return;
  2733. }
  2734. /* The next mmap will map the V4L buffers */
  2735. fh->map_mode = ZORAN_MAP_MODE_RAW;
  2736. } else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
  2737. fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
  2738. /* we need to calculate size ourselves now */
  2739. if (req->count < 4)
  2740. req->count = 4;
  2741. if (req->count > jpg_nbufs)
  2742. req->count = jpg_nbufs;
  2743. fh->jpg_buffers.num_buffers = req->count;
  2744. fh->jpg_buffers.buffer_size =
  2745. zoran_v4l2_calc_bufsize(&fh->jpg_settings);
  2746. if (jpg_fbuffer_alloc(file)) {
  2747. res = -ENOMEM;
  2748. goto v4l2reqbuf_unlock_and_return;
  2749. }
  2750. /* The next mmap will map the MJPEG buffers */
  2751. if (req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  2752. fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
  2753. else
  2754. fh->map_mode = ZORAN_MAP_MODE_JPG_PLAY;
  2755. } else {
  2756. dprintk(1,
  2757. KERN_ERR
  2758. "%s: VIDIOC_REQBUFS - unknown type %d\n",
  2759. ZR_DEVNAME(zr), req->type);
  2760. res = -EINVAL;
  2761. goto v4l2reqbuf_unlock_and_return;
  2762. }
  2763. v4l2reqbuf_unlock_and_return:
  2764. up(&zr->resource_lock);
  2765. return 0;
  2766. }
  2767. break;
  2768. case VIDIOC_QUERYBUF:
  2769. {
  2770. struct v4l2_buffer *buf = arg;
  2771. __u32 type = buf->type;
  2772. int index = buf->index, res;
  2773. dprintk(3,
  2774. KERN_DEBUG
  2775. "%s: VIDIOC_QUERYBUF - index=%d, type=%d\n",
  2776. ZR_DEVNAME(zr), buf->index, buf->type);
  2777. memset(buf, 0, sizeof(buf));
  2778. buf->type = type;
  2779. buf->index = index;
  2780. down(&zr->resource_lock);
  2781. res = zoran_v4l2_buffer_status(file, buf, buf->index);
  2782. up(&zr->resource_lock);
  2783. return res;
  2784. }
  2785. break;
  2786. case VIDIOC_QBUF:
  2787. {
  2788. struct v4l2_buffer *buf = arg;
  2789. int res = 0, codec_mode, buf_type;
  2790. dprintk(3,
  2791. KERN_DEBUG "%s: VIDIOC_QBUF - type=%d, index=%d\n",
  2792. ZR_DEVNAME(zr), buf->type, buf->index);
  2793. down(&zr->resource_lock);
  2794. switch (fh->map_mode) {
  2795. case ZORAN_MAP_MODE_RAW:
  2796. if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  2797. dprintk(1,
  2798. KERN_ERR
  2799. "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
  2800. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  2801. res = -EINVAL;
  2802. goto qbuf_unlock_and_return;
  2803. }
  2804. res = zoran_v4l_queue_frame(file, buf->index);
  2805. if (res)
  2806. goto qbuf_unlock_and_return;
  2807. if (!zr->v4l_memgrab_active &&
  2808. fh->v4l_buffers.active == ZORAN_LOCKED)
  2809. zr36057_set_memgrab(zr, 1);
  2810. break;
  2811. case ZORAN_MAP_MODE_JPG_REC:
  2812. case ZORAN_MAP_MODE_JPG_PLAY:
  2813. if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
  2814. buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  2815. codec_mode = BUZ_MODE_MOTION_DECOMPRESS;
  2816. } else {
  2817. buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  2818. codec_mode = BUZ_MODE_MOTION_COMPRESS;
  2819. }
  2820. if (buf->type != buf_type) {
  2821. dprintk(1,
  2822. KERN_ERR
  2823. "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
  2824. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  2825. res = -EINVAL;
  2826. goto qbuf_unlock_and_return;
  2827. }
  2828. res =
  2829. zoran_jpg_queue_frame(file, buf->index,
  2830. codec_mode);
  2831. if (res != 0)
  2832. goto qbuf_unlock_and_return;
  2833. if (zr->codec_mode == BUZ_MODE_IDLE &&
  2834. fh->jpg_buffers.active == ZORAN_LOCKED) {
  2835. zr36057_enable_jpg(zr, codec_mode);
  2836. }
  2837. break;
  2838. default:
  2839. dprintk(1,
  2840. KERN_ERR
  2841. "%s: VIDIOC_QBUF - unsupported type %d\n",
  2842. ZR_DEVNAME(zr), buf->type);
  2843. res = -EINVAL;
  2844. goto qbuf_unlock_and_return;
  2845. }
  2846. qbuf_unlock_and_return:
  2847. up(&zr->resource_lock);
  2848. return res;
  2849. }
  2850. break;
  2851. case VIDIOC_DQBUF:
  2852. {
  2853. struct v4l2_buffer *buf = arg;
  2854. int res = 0, buf_type, num = -1; /* compiler borks here (?) */
  2855. dprintk(3, KERN_DEBUG "%s: VIDIOC_DQBUF - type=%d\n",
  2856. ZR_DEVNAME(zr), buf->type);
  2857. down(&zr->resource_lock);
  2858. switch (fh->map_mode) {
  2859. case ZORAN_MAP_MODE_RAW:
  2860. if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  2861. dprintk(1,
  2862. KERN_ERR
  2863. "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
  2864. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  2865. res = -EINVAL;
  2866. goto dqbuf_unlock_and_return;
  2867. }
  2868. num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
  2869. if (file->f_flags & O_NONBLOCK &&
  2870. zr->v4l_buffers.buffer[num].state !=
  2871. BUZ_STATE_DONE) {
  2872. res = -EAGAIN;
  2873. goto dqbuf_unlock_and_return;
  2874. }
  2875. res = v4l_sync(file, num);
  2876. if (res)
  2877. goto dqbuf_unlock_and_return;
  2878. else
  2879. zr->v4l_sync_tail++;
  2880. res = zoran_v4l2_buffer_status(file, buf, num);
  2881. break;
  2882. case ZORAN_MAP_MODE_JPG_REC:
  2883. case ZORAN_MAP_MODE_JPG_PLAY:
  2884. {
  2885. struct zoran_sync bs;
  2886. if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
  2887. buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  2888. else
  2889. buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  2890. if (buf->type != buf_type) {
  2891. dprintk(1,
  2892. KERN_ERR
  2893. "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
  2894. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  2895. res = -EINVAL;
  2896. goto dqbuf_unlock_and_return;
  2897. }
  2898. num =
  2899. zr->jpg_pend[zr->
  2900. jpg_que_tail & BUZ_MASK_FRAME];
  2901. if (file->f_flags & O_NONBLOCK &&
  2902. zr->jpg_buffers.buffer[num].state !=
  2903. BUZ_STATE_DONE) {
  2904. res = -EAGAIN;
  2905. goto dqbuf_unlock_and_return;
  2906. }
  2907. res = jpg_sync(file, &bs);
  2908. if (res)
  2909. goto dqbuf_unlock_and_return;
  2910. res =
  2911. zoran_v4l2_buffer_status(file, buf, bs.frame);
  2912. break;
  2913. }
  2914. default:
  2915. dprintk(1,
  2916. KERN_ERR
  2917. "%s: VIDIOC_DQBUF - unsupported type %d\n",
  2918. ZR_DEVNAME(zr), buf->type);
  2919. res = -EINVAL;
  2920. goto dqbuf_unlock_and_return;
  2921. }
  2922. dqbuf_unlock_and_return:
  2923. up(&zr->resource_lock);
  2924. return res;
  2925. }
  2926. break;
  2927. case VIDIOC_STREAMON:
  2928. {
  2929. int res = 0;
  2930. dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMON\n", ZR_DEVNAME(zr));
  2931. down(&zr->resource_lock);
  2932. switch (fh->map_mode) {
  2933. case ZORAN_MAP_MODE_RAW: /* raw capture */
  2934. if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
  2935. fh->v4l_buffers.active != ZORAN_ACTIVE) {
  2936. res = -EBUSY;
  2937. goto strmon_unlock_and_return;
  2938. }
  2939. zr->v4l_buffers.active = fh->v4l_buffers.active =
  2940. ZORAN_LOCKED;
  2941. zr->v4l_settings = fh->v4l_settings;
  2942. zr->v4l_sync_tail = zr->v4l_pend_tail;
  2943. if (!zr->v4l_memgrab_active &&
  2944. zr->v4l_pend_head != zr->v4l_pend_tail) {
  2945. zr36057_set_memgrab(zr, 1);
  2946. }
  2947. break;
  2948. case ZORAN_MAP_MODE_JPG_REC:
  2949. case ZORAN_MAP_MODE_JPG_PLAY:
  2950. /* what is the codec mode right now? */
  2951. if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
  2952. fh->jpg_buffers.active != ZORAN_ACTIVE) {
  2953. res = -EBUSY;
  2954. goto strmon_unlock_and_return;
  2955. }
  2956. zr->jpg_buffers.active = fh->jpg_buffers.active =
  2957. ZORAN_LOCKED;
  2958. if (zr->jpg_que_head != zr->jpg_que_tail) {
  2959. /* Start the jpeg codec when the first frame is queued */
  2960. jpeg_start(zr);
  2961. }
  2962. break;
  2963. default:
  2964. dprintk(1,
  2965. KERN_ERR
  2966. "%s: VIDIOC_STREAMON - invalid map mode %d\n",
  2967. ZR_DEVNAME(zr), fh->map_mode);
  2968. res = -EINVAL;
  2969. goto strmon_unlock_and_return;
  2970. }
  2971. strmon_unlock_and_return:
  2972. up(&zr->resource_lock);
  2973. return res;
  2974. }
  2975. break;
  2976. case VIDIOC_STREAMOFF:
  2977. {
  2978. int i, res = 0;
  2979. dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMOFF\n", ZR_DEVNAME(zr));
  2980. down(&zr->resource_lock);
  2981. switch (fh->map_mode) {
  2982. case ZORAN_MAP_MODE_RAW: /* raw capture */
  2983. if (fh->v4l_buffers.active == ZORAN_FREE &&
  2984. zr->v4l_buffers.active != ZORAN_FREE) {
  2985. res = -EPERM; /* stay off other's settings! */
  2986. goto strmoff_unlock_and_return;
  2987. }
  2988. if (zr->v4l_buffers.active == ZORAN_FREE)
  2989. goto strmoff_unlock_and_return;
  2990. /* unload capture */
  2991. if (zr->v4l_memgrab_active)
  2992. zr36057_set_memgrab(zr, 0);
  2993. for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
  2994. zr->v4l_buffers.buffer[i].state =
  2995. BUZ_STATE_USER;
  2996. fh->v4l_buffers = zr->v4l_buffers;
  2997. zr->v4l_buffers.active = fh->v4l_buffers.active =
  2998. ZORAN_FREE;
  2999. zr->v4l_grab_seq = 0;
  3000. zr->v4l_pend_head = zr->v4l_pend_tail = 0;
  3001. zr->v4l_sync_tail = 0;
  3002. break;
  3003. case ZORAN_MAP_MODE_JPG_REC:
  3004. case ZORAN_MAP_MODE_JPG_PLAY:
  3005. if (fh->jpg_buffers.active == ZORAN_FREE &&
  3006. zr->jpg_buffers.active != ZORAN_FREE) {
  3007. res = -EPERM; /* stay off other's settings! */
  3008. goto strmoff_unlock_and_return;
  3009. }
  3010. if (zr->jpg_buffers.active == ZORAN_FREE)
  3011. goto strmoff_unlock_and_return;
  3012. res =
  3013. jpg_qbuf(file, -1,
  3014. (fh->map_mode ==
  3015. ZORAN_MAP_MODE_JPG_REC) ?
  3016. BUZ_MODE_MOTION_COMPRESS :
  3017. BUZ_MODE_MOTION_DECOMPRESS);
  3018. if (res)
  3019. goto strmoff_unlock_and_return;
  3020. break;
  3021. default:
  3022. dprintk(1,
  3023. KERN_ERR
  3024. "%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
  3025. ZR_DEVNAME(zr), fh->map_mode);
  3026. res = -EINVAL;
  3027. goto strmoff_unlock_and_return;
  3028. }
  3029. strmoff_unlock_and_return:
  3030. up(&zr->resource_lock);
  3031. return res;
  3032. }
  3033. break;
  3034. case VIDIOC_QUERYCTRL:
  3035. {
  3036. struct v4l2_queryctrl *ctrl = arg;
  3037. dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCTRL - id=%d\n",
  3038. ZR_DEVNAME(zr), ctrl->id);
  3039. /* we only support hue/saturation/contrast/brightness */
  3040. if (ctrl->id < V4L2_CID_BRIGHTNESS ||
  3041. ctrl->id > V4L2_CID_HUE)
  3042. return -EINVAL;
  3043. else {
  3044. int id = ctrl->id;
  3045. memset(ctrl, 0, sizeof(*ctrl));
  3046. ctrl->id = id;
  3047. }
  3048. switch (ctrl->id) {
  3049. case V4L2_CID_BRIGHTNESS:
  3050. strncpy(ctrl->name, "Brightness", 31);
  3051. break;
  3052. case V4L2_CID_CONTRAST:
  3053. strncpy(ctrl->name, "Contrast", 31);
  3054. break;
  3055. case V4L2_CID_SATURATION:
  3056. strncpy(ctrl->name, "Saturation", 31);
  3057. break;
  3058. case V4L2_CID_HUE:
  3059. strncpy(ctrl->name, "Hue", 31);
  3060. break;
  3061. }
  3062. ctrl->minimum = 0;
  3063. ctrl->maximum = 65535;
  3064. ctrl->step = 1;
  3065. ctrl->default_value = 32768;
  3066. ctrl->type = V4L2_CTRL_TYPE_INTEGER;
  3067. return 0;
  3068. }
  3069. break;
  3070. case VIDIOC_G_CTRL:
  3071. {
  3072. struct v4l2_control *ctrl = arg;
  3073. dprintk(3, KERN_DEBUG "%s: VIDIOC_G_CTRL - id=%d\n",
  3074. ZR_DEVNAME(zr), ctrl->id);
  3075. /* we only support hue/saturation/contrast/brightness */
  3076. if (ctrl->id < V4L2_CID_BRIGHTNESS ||
  3077. ctrl->id > V4L2_CID_HUE)
  3078. return -EINVAL;
  3079. down(&zr->resource_lock);
  3080. switch (ctrl->id) {
  3081. case V4L2_CID_BRIGHTNESS:
  3082. ctrl->value = zr->brightness;
  3083. break;
  3084. case V4L2_CID_CONTRAST:
  3085. ctrl->value = zr->contrast;
  3086. break;
  3087. case V4L2_CID_SATURATION:
  3088. ctrl->value = zr->saturation;
  3089. break;
  3090. case V4L2_CID_HUE:
  3091. ctrl->value = zr->hue;
  3092. break;
  3093. }
  3094. up(&zr->resource_lock);
  3095. return 0;
  3096. }
  3097. break;
  3098. case VIDIOC_S_CTRL:
  3099. {
  3100. struct v4l2_control *ctrl = arg;
  3101. struct video_picture pict;
  3102. dprintk(3, KERN_DEBUG "%s: VIDIOC_S_CTRL - id=%d\n",
  3103. ZR_DEVNAME(zr), ctrl->id);
  3104. /* we only support hue/saturation/contrast/brightness */
  3105. if (ctrl->id < V4L2_CID_BRIGHTNESS ||
  3106. ctrl->id > V4L2_CID_HUE)
  3107. return -EINVAL;
  3108. if (ctrl->value < 0 || ctrl->value > 65535) {
  3109. dprintk(1,
  3110. KERN_ERR
  3111. "%s: VIDIOC_S_CTRL - invalid value %d for id=%d\n",
  3112. ZR_DEVNAME(zr), ctrl->value, ctrl->id);
  3113. return -EINVAL;
  3114. }
  3115. down(&zr->resource_lock);
  3116. switch (ctrl->id) {
  3117. case V4L2_CID_BRIGHTNESS:
  3118. zr->brightness = ctrl->value;
  3119. break;
  3120. case V4L2_CID_CONTRAST:
  3121. zr->contrast = ctrl->value;
  3122. break;
  3123. case V4L2_CID_SATURATION:
  3124. zr->saturation = ctrl->value;
  3125. break;
  3126. case V4L2_CID_HUE:
  3127. zr->hue = ctrl->value;
  3128. break;
  3129. }
  3130. pict.brightness = zr->brightness;
  3131. pict.contrast = zr->contrast;
  3132. pict.colour = zr->saturation;
  3133. pict.hue = zr->hue;
  3134. decoder_command(zr, DECODER_SET_PICTURE, &pict);
  3135. up(&zr->resource_lock);
  3136. return 0;
  3137. }
  3138. break;
  3139. case VIDIOC_ENUMSTD:
  3140. {
  3141. struct v4l2_standard *std = arg;
  3142. dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMSTD - index=%d\n",
  3143. ZR_DEVNAME(zr), std->index);
  3144. if (std->index < 0 || std->index >= (zr->card.norms + 1))
  3145. return -EINVAL;
  3146. else {
  3147. int id = std->index;
  3148. memset(std, 0, sizeof(*std));
  3149. std->index = id;
  3150. }
  3151. if (std->index == zr->card.norms) {
  3152. /* if we have autodetect, ... */
  3153. struct video_decoder_capability caps;
  3154. decoder_command(zr, DECODER_GET_CAPABILITIES,
  3155. &caps);
  3156. if (caps.flags & VIDEO_DECODER_AUTO) {
  3157. std->id = V4L2_STD_ALL;
  3158. strncpy(std->name, "Autodetect", 31);
  3159. return 0;
  3160. } else
  3161. return -EINVAL;
  3162. }
  3163. switch (std->index) {
  3164. case 0:
  3165. std->id = V4L2_STD_PAL;
  3166. strncpy(std->name, "PAL", 31);
  3167. std->frameperiod.numerator = 1;
  3168. std->frameperiod.denominator = 25;
  3169. std->framelines = zr->card.tvn[0]->Ht;
  3170. break;
  3171. case 1:
  3172. std->id = V4L2_STD_NTSC;
  3173. strncpy(std->name, "NTSC", 31);
  3174. std->frameperiod.numerator = 1001;
  3175. std->frameperiod.denominator = 30000;
  3176. std->framelines = zr->card.tvn[1]->Ht;
  3177. break;
  3178. case 2:
  3179. std->id = V4L2_STD_SECAM;
  3180. strncpy(std->name, "SECAM", 31);
  3181. std->frameperiod.numerator = 1;
  3182. std->frameperiod.denominator = 25;
  3183. std->framelines = zr->card.tvn[2]->Ht;
  3184. break;
  3185. }
  3186. return 0;
  3187. }
  3188. break;
  3189. case VIDIOC_G_STD:
  3190. {
  3191. v4l2_std_id *std = arg;
  3192. int norm;
  3193. dprintk(3, KERN_DEBUG "%s: VIDIOC_G_STD\n", ZR_DEVNAME(zr));
  3194. down(&zr->resource_lock);
  3195. norm = zr->norm;
  3196. up(&zr->resource_lock);
  3197. switch (norm) {
  3198. case VIDEO_MODE_PAL:
  3199. *std = V4L2_STD_PAL;
  3200. break;
  3201. case VIDEO_MODE_NTSC:
  3202. *std = V4L2_STD_NTSC;
  3203. break;
  3204. case VIDEO_MODE_SECAM:
  3205. *std = V4L2_STD_SECAM;
  3206. break;
  3207. }
  3208. return 0;
  3209. }
  3210. break;
  3211. case VIDIOC_S_STD:
  3212. {
  3213. int norm = -1, res = 0;
  3214. v4l2_std_id *std = arg;
  3215. dprintk(3, KERN_DEBUG "%s: VIDIOC_S_STD - norm=0x%llx\n",
  3216. ZR_DEVNAME(zr), (unsigned long long)*std);
  3217. if (*std == V4L2_STD_PAL)
  3218. norm = VIDEO_MODE_PAL;
  3219. else if (*std == V4L2_STD_NTSC)
  3220. norm = VIDEO_MODE_NTSC;
  3221. else if (*std == V4L2_STD_SECAM)
  3222. norm = VIDEO_MODE_SECAM;
  3223. else if (*std == V4L2_STD_ALL)
  3224. norm = VIDEO_MODE_AUTO;
  3225. else {
  3226. dprintk(1,
  3227. KERN_ERR
  3228. "%s: VIDIOC_S_STD - invalid norm 0x%llx\n",
  3229. ZR_DEVNAME(zr), (unsigned long long)*std);
  3230. return -EINVAL;
  3231. }
  3232. down(&zr->resource_lock);
  3233. if ((res = zoran_set_norm(zr, norm)))
  3234. goto sstd_unlock_and_return;
  3235. res = wait_grab_pending(zr);
  3236. sstd_unlock_and_return:
  3237. up(&zr->resource_lock);
  3238. return res;
  3239. }
  3240. break;
  3241. case VIDIOC_ENUMINPUT:
  3242. {
  3243. struct v4l2_input *inp = arg;
  3244. int status;
  3245. dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMINPUT - index=%d\n",
  3246. ZR_DEVNAME(zr), inp->index);
  3247. if (inp->index < 0 || inp->index >= zr->card.inputs)
  3248. return -EINVAL;
  3249. else {
  3250. int id = inp->index;
  3251. memset(inp, 0, sizeof(*inp));
  3252. inp->index = id;
  3253. }
  3254. strncpy(inp->name, zr->card.input[inp->index].name,
  3255. sizeof(inp->name) - 1);
  3256. inp->type = V4L2_INPUT_TYPE_CAMERA;
  3257. inp->std = V4L2_STD_ALL;
  3258. /* Get status of video decoder */
  3259. down(&zr->resource_lock);
  3260. decoder_command(zr, DECODER_GET_STATUS, &status);
  3261. up(&zr->resource_lock);
  3262. if (!(status & DECODER_STATUS_GOOD)) {
  3263. inp->status |= V4L2_IN_ST_NO_POWER;
  3264. inp->status |= V4L2_IN_ST_NO_SIGNAL;
  3265. }
  3266. if (!(status & DECODER_STATUS_COLOR))
  3267. inp->status |= V4L2_IN_ST_NO_COLOR;
  3268. return 0;
  3269. }
  3270. break;
  3271. case VIDIOC_G_INPUT:
  3272. {
  3273. int *input = arg;
  3274. dprintk(3, KERN_DEBUG "%s: VIDIOC_G_INPUT\n", ZR_DEVNAME(zr));
  3275. down(&zr->resource_lock);
  3276. *input = zr->input;
  3277. up(&zr->resource_lock);
  3278. return 0;
  3279. }
  3280. break;
  3281. case VIDIOC_S_INPUT:
  3282. {
  3283. int *input = arg, res = 0;
  3284. dprintk(3, KERN_DEBUG "%s: VIDIOC_S_INPUT - input=%d\n",
  3285. ZR_DEVNAME(zr), *input);
  3286. down(&zr->resource_lock);
  3287. if ((res = zoran_set_input(zr, *input)))
  3288. goto sinput_unlock_and_return;
  3289. /* Make sure the changes come into effect */
  3290. res = wait_grab_pending(zr);
  3291. sinput_unlock_and_return:
  3292. up(&zr->resource_lock);
  3293. return res;
  3294. }
  3295. break;
  3296. case VIDIOC_ENUMOUTPUT:
  3297. {
  3298. struct v4l2_output *outp = arg;
  3299. dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMOUTPUT - index=%d\n",
  3300. ZR_DEVNAME(zr), outp->index);
  3301. if (outp->index != 0)
  3302. return -EINVAL;
  3303. memset(outp, 0, sizeof(*outp));
  3304. outp->index = 0;
  3305. outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
  3306. strncpy(outp->name, "Autodetect", 31);
  3307. return 0;
  3308. }
  3309. break;
  3310. case VIDIOC_G_OUTPUT:
  3311. {
  3312. int *output = arg;
  3313. dprintk(3, KERN_DEBUG "%s: VIDIOC_G_OUTPUT\n", ZR_DEVNAME(zr));
  3314. *output = 0;
  3315. return 0;
  3316. }
  3317. break;
  3318. case VIDIOC_S_OUTPUT:
  3319. {
  3320. int *output = arg;
  3321. dprintk(3, KERN_DEBUG "%s: VIDIOC_S_OUTPUT - output=%d\n",
  3322. ZR_DEVNAME(zr), *output);
  3323. if (*output != 0)
  3324. return -EINVAL;
  3325. return 0;
  3326. }
  3327. break;
  3328. /* cropping (sub-frame capture) */
  3329. case VIDIOC_CROPCAP:
  3330. {
  3331. struct v4l2_cropcap *cropcap = arg;
  3332. int type = cropcap->type, res = 0;
  3333. dprintk(3, KERN_ERR "%s: VIDIOC_CROPCAP - type=%d\n",
  3334. ZR_DEVNAME(zr), cropcap->type);
  3335. memset(cropcap, 0, sizeof(*cropcap));
  3336. cropcap->type = type;
  3337. down(&zr->resource_lock);
  3338. if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  3339. (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  3340. fh->map_mode == ZORAN_MAP_MODE_RAW)) {
  3341. dprintk(1,
  3342. KERN_ERR
  3343. "%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
  3344. ZR_DEVNAME(zr));
  3345. res = -EINVAL;
  3346. goto cropcap_unlock_and_return;
  3347. }
  3348. cropcap->bounds.top = cropcap->bounds.left = 0;
  3349. cropcap->bounds.width = BUZ_MAX_WIDTH;
  3350. cropcap->bounds.height = BUZ_MAX_HEIGHT;
  3351. cropcap->defrect.top = cropcap->defrect.left = 0;
  3352. cropcap->defrect.width = BUZ_MIN_WIDTH;
  3353. cropcap->defrect.height = BUZ_MIN_HEIGHT;
  3354. cropcap_unlock_and_return:
  3355. up(&zr->resource_lock);
  3356. return res;
  3357. }
  3358. break;
  3359. case VIDIOC_G_CROP:
  3360. {
  3361. struct v4l2_crop *crop = arg;
  3362. int type = crop->type, res = 0;
  3363. dprintk(3, KERN_ERR "%s: VIDIOC_G_CROP - type=%d\n",
  3364. ZR_DEVNAME(zr), crop->type);
  3365. memset(crop, 0, sizeof(*crop));
  3366. crop->type = type;
  3367. down(&zr->resource_lock);
  3368. if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  3369. (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  3370. fh->map_mode == ZORAN_MAP_MODE_RAW)) {
  3371. dprintk(1,
  3372. KERN_ERR
  3373. "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
  3374. ZR_DEVNAME(zr));
  3375. res = -EINVAL;
  3376. goto gcrop_unlock_and_return;
  3377. }
  3378. crop->c.top = fh->jpg_settings.img_y;
  3379. crop->c.left = fh->jpg_settings.img_x;
  3380. crop->c.width = fh->jpg_settings.img_width;
  3381. crop->c.height = fh->jpg_settings.img_height;
  3382. gcrop_unlock_and_return:
  3383. up(&zr->resource_lock);
  3384. return res;
  3385. }
  3386. break;
  3387. case VIDIOC_S_CROP:
  3388. {
  3389. struct v4l2_crop *crop = arg;
  3390. int res = 0;
  3391. settings = fh->jpg_settings;
  3392. dprintk(3,
  3393. KERN_ERR
  3394. "%s: VIDIOC_S_CROP - type=%d, x=%d,y=%d,w=%d,h=%d\n",
  3395. ZR_DEVNAME(zr), crop->type, crop->c.left, crop->c.top,
  3396. crop->c.width, crop->c.height);
  3397. down(&zr->resource_lock);
  3398. if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
  3399. dprintk(1,
  3400. KERN_ERR
  3401. "%s: VIDIOC_S_CROP - cannot change settings while active\n",
  3402. ZR_DEVNAME(zr));
  3403. res = -EBUSY;
  3404. goto scrop_unlock_and_return;
  3405. }
  3406. if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  3407. (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  3408. fh->map_mode == ZORAN_MAP_MODE_RAW)) {
  3409. dprintk(1,
  3410. KERN_ERR
  3411. "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
  3412. ZR_DEVNAME(zr));
  3413. res = -EINVAL;
  3414. goto scrop_unlock_and_return;
  3415. }
  3416. /* move into a form that we understand */
  3417. settings.img_x = crop->c.left;
  3418. settings.img_y = crop->c.top;
  3419. settings.img_width = crop->c.width;
  3420. settings.img_height = crop->c.height;
  3421. /* check validity */
  3422. if ((res = zoran_check_jpg_settings(zr, &settings)))
  3423. goto scrop_unlock_and_return;
  3424. /* accept */
  3425. fh->jpg_settings = settings;
  3426. scrop_unlock_and_return:
  3427. up(&zr->resource_lock);
  3428. return res;
  3429. }
  3430. break;
  3431. case VIDIOC_G_JPEGCOMP:
  3432. {
  3433. struct v4l2_jpegcompression *params = arg;
  3434. dprintk(3, KERN_DEBUG "%s: VIDIOC_G_JPEGCOMP\n",
  3435. ZR_DEVNAME(zr));
  3436. memset(params, 0, sizeof(*params));
  3437. down(&zr->resource_lock);
  3438. params->quality = fh->jpg_settings.jpg_comp.quality;
  3439. params->APPn = fh->jpg_settings.jpg_comp.APPn;
  3440. memcpy(params->APP_data,
  3441. fh->jpg_settings.jpg_comp.APP_data,
  3442. fh->jpg_settings.jpg_comp.APP_len);
  3443. params->APP_len = fh->jpg_settings.jpg_comp.APP_len;
  3444. memcpy(params->COM_data,
  3445. fh->jpg_settings.jpg_comp.COM_data,
  3446. fh->jpg_settings.jpg_comp.COM_len);
  3447. params->COM_len = fh->jpg_settings.jpg_comp.COM_len;
  3448. params->jpeg_markers =
  3449. fh->jpg_settings.jpg_comp.jpeg_markers;
  3450. up(&zr->resource_lock);
  3451. return 0;
  3452. }
  3453. break;
  3454. case VIDIOC_S_JPEGCOMP:
  3455. {
  3456. struct v4l2_jpegcompression *params = arg;
  3457. int res = 0;
  3458. settings = fh->jpg_settings;
  3459. dprintk(3,
  3460. KERN_DEBUG
  3461. "%s: VIDIOC_S_JPEGCOMP - quality=%d, APPN=%d, APP_len=%d, COM_len=%d\n",
  3462. ZR_DEVNAME(zr), params->quality, params->APPn,
  3463. params->APP_len, params->COM_len);
  3464. settings.jpg_comp = *params;
  3465. down(&zr->resource_lock);
  3466. if (fh->v4l_buffers.active != ZORAN_FREE ||
  3467. fh->jpg_buffers.active != ZORAN_FREE) {
  3468. dprintk(1,
  3469. KERN_WARNING
  3470. "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
  3471. ZR_DEVNAME(zr));
  3472. res = -EBUSY;
  3473. goto sjpegc_unlock_and_return;
  3474. }
  3475. if ((res = zoran_check_jpg_settings(zr, &settings)))
  3476. goto sjpegc_unlock_and_return;
  3477. if (!fh->jpg_buffers.allocated)
  3478. fh->jpg_buffers.buffer_size =
  3479. zoran_v4l2_calc_bufsize(&fh->jpg_settings);
  3480. fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
  3481. sjpegc_unlock_and_return:
  3482. up(&zr->resource_lock);
  3483. return 0;
  3484. }
  3485. break;
  3486. case VIDIOC_QUERYSTD: /* why is this useful? */
  3487. {
  3488. v4l2_std_id *std = arg;
  3489. dprintk(3,
  3490. KERN_DEBUG "%s: VIDIOC_QUERY_STD - std=0x%llx\n",
  3491. ZR_DEVNAME(zr), (unsigned long long)*std);
  3492. if (*std == V4L2_STD_ALL || *std == V4L2_STD_NTSC ||
  3493. *std == V4L2_STD_PAL || (*std == V4L2_STD_SECAM &&
  3494. zr->card.norms == 3)) {
  3495. return 0;
  3496. }
  3497. return -EINVAL;
  3498. }
  3499. break;
  3500. case VIDIOC_TRY_FMT:
  3501. {
  3502. struct v4l2_format *fmt = arg;
  3503. int res = 0;
  3504. dprintk(3, KERN_DEBUG "%s: VIDIOC_TRY_FMT - type=%d\n",
  3505. ZR_DEVNAME(zr), fmt->type);
  3506. switch (fmt->type) {
  3507. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  3508. down(&zr->resource_lock);
  3509. if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH)
  3510. fmt->fmt.win.w.width = BUZ_MAX_WIDTH;
  3511. if (fmt->fmt.win.w.width < BUZ_MIN_WIDTH)
  3512. fmt->fmt.win.w.width = BUZ_MIN_WIDTH;
  3513. if (fmt->fmt.win.w.height > BUZ_MAX_HEIGHT)
  3514. fmt->fmt.win.w.height = BUZ_MAX_HEIGHT;
  3515. if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT)
  3516. fmt->fmt.win.w.height = BUZ_MIN_HEIGHT;
  3517. up(&zr->resource_lock);
  3518. break;
  3519. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  3520. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  3521. if (fmt->fmt.pix.bytesperline > 0)
  3522. return -EINVAL;
  3523. down(&zr->resource_lock);
  3524. if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
  3525. settings = fh->jpg_settings;
  3526. /* we actually need to set 'real' parameters now */
  3527. if ((fmt->fmt.pix.height * 2) >
  3528. BUZ_MAX_HEIGHT)
  3529. settings.TmpDcm = 1;
  3530. else
  3531. settings.TmpDcm = 2;
  3532. settings.decimation = 0;
  3533. if (fmt->fmt.pix.height <=
  3534. fh->jpg_settings.img_height / 2)
  3535. settings.VerDcm = 2;
  3536. else
  3537. settings.VerDcm = 1;
  3538. if (fmt->fmt.pix.width <=
  3539. fh->jpg_settings.img_width / 4)
  3540. settings.HorDcm = 4;
  3541. else if (fmt->fmt.pix.width <=
  3542. fh->jpg_settings.img_width / 2)
  3543. settings.HorDcm = 2;
  3544. else
  3545. settings.HorDcm = 1;
  3546. if (settings.TmpDcm == 1)
  3547. settings.field_per_buff = 2;
  3548. else
  3549. settings.field_per_buff = 1;
  3550. /* check */
  3551. if ((res =
  3552. zoran_check_jpg_settings(zr,
  3553. &settings)))
  3554. goto tryfmt_unlock_and_return;
  3555. /* tell the user what we actually did */
  3556. fmt->fmt.pix.width =
  3557. settings.img_width / settings.HorDcm;
  3558. fmt->fmt.pix.height =
  3559. settings.img_height * 2 /
  3560. (settings.TmpDcm * settings.VerDcm);
  3561. if (settings.TmpDcm == 1)
  3562. fmt->fmt.pix.field =
  3563. (fh->jpg_settings.
  3564. odd_even ? V4L2_FIELD_SEQ_TB :
  3565. V4L2_FIELD_SEQ_BT);
  3566. else
  3567. fmt->fmt.pix.field =
  3568. (fh->jpg_settings.
  3569. odd_even ? V4L2_FIELD_TOP :
  3570. V4L2_FIELD_BOTTOM);
  3571. fmt->fmt.pix.sizeimage =
  3572. zoran_v4l2_calc_bufsize(&settings);
  3573. } else if (fmt->type ==
  3574. V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  3575. int i;
  3576. for (i = 0; i < zoran_num_formats; i++)
  3577. if (zoran_formats[i].fourcc ==
  3578. fmt->fmt.pix.pixelformat)
  3579. break;
  3580. if (i == zoran_num_formats) {
  3581. res = -EINVAL;
  3582. goto tryfmt_unlock_and_return;
  3583. }
  3584. if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
  3585. fmt->fmt.pix.width = BUZ_MAX_WIDTH;
  3586. if (fmt->fmt.pix.width < BUZ_MIN_WIDTH)
  3587. fmt->fmt.pix.width = BUZ_MIN_WIDTH;
  3588. if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
  3589. fmt->fmt.pix.height =
  3590. BUZ_MAX_HEIGHT;
  3591. if (fmt->fmt.pix.height < BUZ_MIN_HEIGHT)
  3592. fmt->fmt.pix.height =
  3593. BUZ_MIN_HEIGHT;
  3594. } else {
  3595. res = -EINVAL;
  3596. goto tryfmt_unlock_and_return;
  3597. }
  3598. tryfmt_unlock_and_return:
  3599. up(&zr->resource_lock);
  3600. return res;
  3601. break;
  3602. default:
  3603. return -EINVAL;
  3604. }
  3605. return 0;
  3606. }
  3607. break;
  3608. #endif
  3609. default:
  3610. dprintk(1, KERN_DEBUG "%s: UNKNOWN ioctl cmd: 0x%x\n",
  3611. ZR_DEVNAME(zr), cmd);
  3612. return -ENOIOCTLCMD;
  3613. break;
  3614. }
  3615. return 0;
  3616. }
  3617. static int
  3618. zoran_ioctl (struct inode *inode,
  3619. struct file *file,
  3620. unsigned int cmd,
  3621. unsigned long arg)
  3622. {
  3623. return video_usercopy(inode, file, cmd, arg, zoran_do_ioctl);
  3624. }
  3625. static unsigned int
  3626. zoran_poll (struct file *file,
  3627. poll_table *wait)
  3628. {
  3629. struct zoran_fh *fh = file->private_data;
  3630. struct zoran *zr = fh->zr;
  3631. wait_queue_head_t *queue = NULL;
  3632. int res = 0, frame;
  3633. /* we should check whether buffers are ready to be synced on
  3634. * (w/o waits - O_NONBLOCK) here
  3635. * if ready for read (sync), return POLLIN|POLLRDNORM,
  3636. * if ready for write (sync), return POLLOUT|POLLWRNORM,
  3637. * if error, return POLLERR,
  3638. * if no buffers queued or so, return POLLNVAL
  3639. */
  3640. down(&zr->resource_lock);
  3641. switch (fh->map_mode) {
  3642. case ZORAN_MAP_MODE_RAW:
  3643. if (fh->v4l_buffers.active == ZORAN_FREE ||
  3644. zr->v4l_pend_head == zr->v4l_pend_tail) {
  3645. dprintk(1,
  3646. "%s: zoran_poll() - no buffers queued\n",
  3647. ZR_DEVNAME(zr));
  3648. res = POLLNVAL;
  3649. goto poll_unlock_and_return;
  3650. }
  3651. queue = &zr->v4l_capq;
  3652. frame = zr->v4l_pend[zr->v4l_pend_tail & V4L_MASK_FRAME];
  3653. poll_wait(file, queue, wait);
  3654. if (fh->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
  3655. res = POLLIN | POLLRDNORM;
  3656. break;
  3657. case ZORAN_MAP_MODE_JPG_REC:
  3658. case ZORAN_MAP_MODE_JPG_PLAY:
  3659. if (fh->jpg_buffers.active == ZORAN_FREE ||
  3660. zr->jpg_que_head == zr->jpg_que_tail) {
  3661. dprintk(1,
  3662. "%s: zoran_poll() - no buffers queued\n",
  3663. ZR_DEVNAME(zr));
  3664. res = POLLNVAL;
  3665. goto poll_unlock_and_return;
  3666. }
  3667. queue = &zr->jpg_capq;
  3668. frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
  3669. poll_wait(file, queue, wait);
  3670. if (fh->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
  3671. if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
  3672. res = POLLIN | POLLRDNORM;
  3673. else
  3674. res = POLLOUT | POLLWRNORM;
  3675. }
  3676. break;
  3677. default:
  3678. dprintk(1,
  3679. "%s: zoran_poll() - internal error, unknown map_mode=%d\n",
  3680. ZR_DEVNAME(zr), fh->map_mode);
  3681. res = POLLNVAL;
  3682. goto poll_unlock_and_return;
  3683. }
  3684. poll_unlock_and_return:
  3685. up(&zr->resource_lock);
  3686. return res;
  3687. }
  3688. /*
  3689. * This maps the buffers to user space.
  3690. *
  3691. * Depending on the state of fh->map_mode
  3692. * the V4L or the MJPEG buffers are mapped
  3693. * per buffer or all together
  3694. *
  3695. * Note that we need to connect to some
  3696. * unmap signal event to unmap the de-allocate
  3697. * the buffer accordingly (zoran_vm_close())
  3698. */
  3699. static void
  3700. zoran_vm_open (struct vm_area_struct *vma)
  3701. {
  3702. struct zoran_mapping *map = vma->vm_private_data;
  3703. map->count++;
  3704. }
  3705. static void
  3706. zoran_vm_close (struct vm_area_struct *vma)
  3707. {
  3708. struct zoran_mapping *map = vma->vm_private_data;
  3709. struct file *file = map->file;
  3710. struct zoran_fh *fh = file->private_data;
  3711. struct zoran *zr = fh->zr;
  3712. int i;
  3713. map->count--;
  3714. if (map->count == 0) {
  3715. switch (fh->map_mode) {
  3716. case ZORAN_MAP_MODE_JPG_REC:
  3717. case ZORAN_MAP_MODE_JPG_PLAY:
  3718. dprintk(3, KERN_INFO "%s: munmap(MJPEG)\n",
  3719. ZR_DEVNAME(zr));
  3720. for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
  3721. if (fh->jpg_buffers.buffer[i].map == map) {
  3722. fh->jpg_buffers.buffer[i].map =
  3723. NULL;
  3724. }
  3725. }
  3726. kfree(map);
  3727. for (i = 0; i < fh->jpg_buffers.num_buffers; i++)
  3728. if (fh->jpg_buffers.buffer[i].map)
  3729. break;
  3730. if (i == fh->jpg_buffers.num_buffers) {
  3731. down(&zr->resource_lock);
  3732. if (fh->jpg_buffers.active != ZORAN_FREE) {
  3733. jpg_qbuf(file, -1, zr->codec_mode);
  3734. zr->jpg_buffers.allocated = 0;
  3735. zr->jpg_buffers.active =
  3736. fh->jpg_buffers.active =
  3737. ZORAN_FREE;
  3738. }
  3739. //jpg_fbuffer_free(file);
  3740. fh->jpg_buffers.allocated = 0;
  3741. fh->jpg_buffers.ready_to_be_freed = 1;
  3742. up(&zr->resource_lock);
  3743. }
  3744. break;
  3745. case ZORAN_MAP_MODE_RAW:
  3746. dprintk(3, KERN_INFO "%s: munmap(V4L)\n",
  3747. ZR_DEVNAME(zr));
  3748. for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
  3749. if (fh->v4l_buffers.buffer[i].map == map) {
  3750. /* unqueue/unmap */
  3751. fh->v4l_buffers.buffer[i].map =
  3752. NULL;
  3753. }
  3754. }
  3755. kfree(map);
  3756. for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
  3757. if (fh->v4l_buffers.buffer[i].map)
  3758. break;
  3759. if (i == fh->v4l_buffers.num_buffers) {
  3760. down(&zr->resource_lock);
  3761. if (fh->v4l_buffers.active != ZORAN_FREE) {
  3762. zr36057_set_memgrab(zr, 0);
  3763. zr->v4l_buffers.allocated = 0;
  3764. zr->v4l_buffers.active =
  3765. fh->v4l_buffers.active =
  3766. ZORAN_FREE;
  3767. }
  3768. //v4l_fbuffer_free(file);
  3769. fh->v4l_buffers.allocated = 0;
  3770. fh->v4l_buffers.ready_to_be_freed = 1;
  3771. up(&zr->resource_lock);
  3772. }
  3773. break;
  3774. default:
  3775. printk(KERN_ERR
  3776. "%s: munmap() - internal error - unknown map mode %d\n",
  3777. ZR_DEVNAME(zr), fh->map_mode);
  3778. break;
  3779. }
  3780. }
  3781. }
  3782. static struct vm_operations_struct zoran_vm_ops = {
  3783. .open = zoran_vm_open,
  3784. .close = zoran_vm_close,
  3785. };
  3786. static int
  3787. zoran_mmap (struct file *file,
  3788. struct vm_area_struct *vma)
  3789. {
  3790. struct zoran_fh *fh = file->private_data;
  3791. struct zoran *zr = fh->zr;
  3792. unsigned long size = (vma->vm_end - vma->vm_start);
  3793. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  3794. int i, j;
  3795. unsigned long page, start = vma->vm_start, todo, pos, fraglen;
  3796. int first, last;
  3797. struct zoran_mapping *map;
  3798. int res = 0;
  3799. dprintk(3,
  3800. KERN_INFO "%s: mmap(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
  3801. ZR_DEVNAME(zr),
  3802. fh->map_mode == ZORAN_MAP_MODE_RAW ? "V4L" : "MJPEG",
  3803. vma->vm_start, vma->vm_end, size);
  3804. if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
  3805. !(vma->vm_flags & VM_WRITE)) {
  3806. dprintk(1,
  3807. KERN_ERR
  3808. "%s: mmap() - no MAP_SHARED/PROT_{READ,WRITE} given\n",
  3809. ZR_DEVNAME(zr));
  3810. return -EINVAL;
  3811. }
  3812. switch (fh->map_mode) {
  3813. case ZORAN_MAP_MODE_JPG_REC:
  3814. case ZORAN_MAP_MODE_JPG_PLAY:
  3815. /* lock */
  3816. down(&zr->resource_lock);
  3817. /* Map the MJPEG buffers */
  3818. if (!fh->jpg_buffers.allocated) {
  3819. dprintk(1,
  3820. KERN_ERR
  3821. "%s: zoran_mmap(MJPEG) - buffers not yet allocated\n",
  3822. ZR_DEVNAME(zr));
  3823. res = -ENOMEM;
  3824. goto jpg_mmap_unlock_and_return;
  3825. }
  3826. first = offset / fh->jpg_buffers.buffer_size;
  3827. last = first - 1 + size / fh->jpg_buffers.buffer_size;
  3828. if (offset % fh->jpg_buffers.buffer_size != 0 ||
  3829. size % fh->jpg_buffers.buffer_size != 0 || first < 0 ||
  3830. last < 0 || first >= fh->jpg_buffers.num_buffers ||
  3831. last >= fh->jpg_buffers.num_buffers) {
  3832. dprintk(1,
  3833. KERN_ERR
  3834. "%s: mmap(MJPEG) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
  3835. ZR_DEVNAME(zr), offset, size,
  3836. fh->jpg_buffers.buffer_size,
  3837. fh->jpg_buffers.num_buffers);
  3838. res = -EINVAL;
  3839. goto jpg_mmap_unlock_and_return;
  3840. }
  3841. for (i = first; i <= last; i++) {
  3842. if (fh->jpg_buffers.buffer[i].map) {
  3843. dprintk(1,
  3844. KERN_ERR
  3845. "%s: mmap(MJPEG) - buffer %d already mapped\n",
  3846. ZR_DEVNAME(zr), i);
  3847. res = -EBUSY;
  3848. goto jpg_mmap_unlock_and_return;
  3849. }
  3850. }
  3851. /* map these buffers (v4l_buffers[i]) */
  3852. map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
  3853. if (!map) {
  3854. res = -ENOMEM;
  3855. goto jpg_mmap_unlock_and_return;
  3856. }
  3857. map->file = file;
  3858. map->count = 1;
  3859. vma->vm_ops = &zoran_vm_ops;
  3860. vma->vm_flags |= VM_DONTEXPAND;
  3861. vma->vm_private_data = map;
  3862. for (i = first; i <= last; i++) {
  3863. for (j = 0;
  3864. j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
  3865. j++) {
  3866. fraglen =
  3867. (le32_to_cpu(fh->jpg_buffers.buffer[i].
  3868. frag_tab[2 * j + 1]) & ~1) << 1;
  3869. todo = size;
  3870. if (todo > fraglen)
  3871. todo = fraglen;
  3872. pos =
  3873. le32_to_cpu((unsigned long) fh->jpg_buffers.
  3874. buffer[i].frag_tab[2 * j]);
  3875. /* should just be pos on i386 */
  3876. page = virt_to_phys(bus_to_virt(pos))
  3877. >> PAGE_SHIFT;
  3878. if (remap_pfn_range(vma, start, page,
  3879. todo, PAGE_SHARED)) {
  3880. dprintk(1,
  3881. KERN_ERR
  3882. "%s: zoran_mmap(V4L) - remap_pfn_range failed\n",
  3883. ZR_DEVNAME(zr));
  3884. res = -EAGAIN;
  3885. goto jpg_mmap_unlock_and_return;
  3886. }
  3887. size -= todo;
  3888. start += todo;
  3889. if (size == 0)
  3890. break;
  3891. if (le32_to_cpu(fh->jpg_buffers.buffer[i].
  3892. frag_tab[2 * j + 1]) & 1)
  3893. break; /* was last fragment */
  3894. }
  3895. fh->jpg_buffers.buffer[i].map = map;
  3896. if (size == 0)
  3897. break;
  3898. }
  3899. jpg_mmap_unlock_and_return:
  3900. up(&zr->resource_lock);
  3901. break;
  3902. case ZORAN_MAP_MODE_RAW:
  3903. down(&zr->resource_lock);
  3904. /* Map the V4L buffers */
  3905. if (!fh->v4l_buffers.allocated) {
  3906. dprintk(1,
  3907. KERN_ERR
  3908. "%s: zoran_mmap(V4L) - buffers not yet allocated\n",
  3909. ZR_DEVNAME(zr));
  3910. res = -ENOMEM;
  3911. goto v4l_mmap_unlock_and_return;
  3912. }
  3913. first = offset / fh->v4l_buffers.buffer_size;
  3914. last = first - 1 + size / fh->v4l_buffers.buffer_size;
  3915. if (offset % fh->v4l_buffers.buffer_size != 0 ||
  3916. size % fh->v4l_buffers.buffer_size != 0 || first < 0 ||
  3917. last < 0 || first >= fh->v4l_buffers.num_buffers ||
  3918. last >= fh->v4l_buffers.buffer_size) {
  3919. dprintk(1,
  3920. KERN_ERR
  3921. "%s: mmap(V4L) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
  3922. ZR_DEVNAME(zr), offset, size,
  3923. fh->v4l_buffers.buffer_size,
  3924. fh->v4l_buffers.num_buffers);
  3925. res = -EINVAL;
  3926. goto v4l_mmap_unlock_and_return;
  3927. }
  3928. for (i = first; i <= last; i++) {
  3929. if (fh->v4l_buffers.buffer[i].map) {
  3930. dprintk(1,
  3931. KERN_ERR
  3932. "%s: mmap(V4L) - buffer %d already mapped\n",
  3933. ZR_DEVNAME(zr), i);
  3934. res = -EBUSY;
  3935. goto v4l_mmap_unlock_and_return;
  3936. }
  3937. }
  3938. /* map these buffers (v4l_buffers[i]) */
  3939. map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
  3940. if (!map) {
  3941. res = -ENOMEM;
  3942. goto v4l_mmap_unlock_and_return;
  3943. }
  3944. map->file = file;
  3945. map->count = 1;
  3946. vma->vm_ops = &zoran_vm_ops;
  3947. vma->vm_flags |= VM_DONTEXPAND;
  3948. vma->vm_private_data = map;
  3949. for (i = first; i <= last; i++) {
  3950. todo = size;
  3951. if (todo > fh->v4l_buffers.buffer_size)
  3952. todo = fh->v4l_buffers.buffer_size;
  3953. page = fh->v4l_buffers.buffer[i].fbuffer_phys;
  3954. if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
  3955. todo, PAGE_SHARED)) {
  3956. dprintk(1,
  3957. KERN_ERR
  3958. "%s: zoran_mmap(V4L)i - remap_pfn_range failed\n",
  3959. ZR_DEVNAME(zr));
  3960. res = -EAGAIN;
  3961. goto v4l_mmap_unlock_and_return;
  3962. }
  3963. size -= todo;
  3964. start += todo;
  3965. fh->v4l_buffers.buffer[i].map = map;
  3966. if (size == 0)
  3967. break;
  3968. }
  3969. v4l_mmap_unlock_and_return:
  3970. up(&zr->resource_lock);
  3971. break;
  3972. default:
  3973. dprintk(1,
  3974. KERN_ERR
  3975. "%s: zoran_mmap() - internal error - unknown map mode %d\n",
  3976. ZR_DEVNAME(zr), fh->map_mode);
  3977. break;
  3978. }
  3979. return 0;
  3980. }
  3981. static struct file_operations zoran_fops = {
  3982. .owner = THIS_MODULE,
  3983. .open = zoran_open,
  3984. .release = zoran_close,
  3985. .ioctl = zoran_ioctl,
  3986. .compat_ioctl = v4l_compat_ioctl32,
  3987. .llseek = no_llseek,
  3988. .read = zoran_read,
  3989. .write = zoran_write,
  3990. .mmap = zoran_mmap,
  3991. .poll = zoran_poll,
  3992. };
  3993. struct video_device zoran_template __devinitdata = {
  3994. .name = ZORAN_NAME,
  3995. .type = ZORAN_VID_TYPE,
  3996. #ifdef HAVE_V4L2
  3997. .type2 = ZORAN_V4L2_VID_FLAGS,
  3998. #endif
  3999. .hardware = ZORAN_HARDWARE,
  4000. .fops = &zoran_fops,
  4001. .release = &zoran_vdev_release,
  4002. .minor = -1
  4003. };