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((void *) 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. spin_lock_irqsave(&zr->spinlock, flags);
  889. if (fh->jpg_buffers.active == ZORAN_FREE) {
  890. if (zr->jpg_buffers.active == ZORAN_FREE) {
  891. zr->jpg_buffers = fh->jpg_buffers;
  892. fh->jpg_buffers.active = ZORAN_ACTIVE;
  893. } else {
  894. dprintk(1,
  895. KERN_ERR
  896. "%s: jpg_queue_frame() - another session is already capturing\n",
  897. ZR_DEVNAME(zr));
  898. res = -EBUSY;
  899. }
  900. }
  901. if (!res && zr->codec_mode == BUZ_MODE_IDLE) {
  902. /* Ok load up the jpeg codec */
  903. zr36057_enable_jpg(zr, mode);
  904. }
  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->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->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->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 = kmalloc(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. memset(fh, 0, sizeof(struct zoran_fh));
  1200. /* used to be BUZ_MAX_WIDTH/HEIGHT, but that gives overflows
  1201. * on norm-change! */
  1202. fh->overlay_mask =
  1203. kmalloc(((768 + 31) / 32) * 576 * 4, GFP_KERNEL);
  1204. if (!fh->overlay_mask) {
  1205. dprintk(1,
  1206. KERN_ERR
  1207. "%s: zoran_open() - allocation of overlay_mask failed\n",
  1208. ZR_DEVNAME(zr));
  1209. kfree(fh);
  1210. res = -ENOMEM;
  1211. goto open_unlock_and_return;
  1212. }
  1213. if (zr->user++ == 0)
  1214. first_open = 1;
  1215. /*up(&zr->resource_lock);*/
  1216. /* default setup - TODO: look at flags */
  1217. if (first_open) { /* First device open */
  1218. zr36057_restart(zr);
  1219. zoran_open_init_params(zr);
  1220. zoran_init_hardware(zr);
  1221. btor(ZR36057_ICR_IntPinEn, ZR36057_ICR);
  1222. }
  1223. /* set file_ops stuff */
  1224. file->private_data = fh;
  1225. fh->zr = zr;
  1226. zoran_open_init_session(file);
  1227. return 0;
  1228. open_unlock_and_return:
  1229. /* if we grabbed locks, release them accordingly */
  1230. if (have_module_locks) {
  1231. module_put(zr->decoder->driver->owner);
  1232. if (zr->encoder) {
  1233. module_put(zr->encoder->driver->owner);
  1234. }
  1235. module_put(THIS_MODULE);
  1236. }
  1237. /* if there's no device found, we didn't obtain the lock either */
  1238. if (zr) {
  1239. /*up(&zr->resource_lock);*/
  1240. }
  1241. return res;
  1242. }
  1243. static int
  1244. zoran_close (struct inode *inode,
  1245. struct file *file)
  1246. {
  1247. struct zoran_fh *fh = file->private_data;
  1248. struct zoran *zr = fh->zr;
  1249. dprintk(1, KERN_INFO "%s: zoran_close(%s, pid=[%d]), users(+)=%d\n",
  1250. ZR_DEVNAME(zr), current->comm, current->pid, zr->user);
  1251. /* kernel locks (fs/device.c), so don't do that ourselves
  1252. * (prevents deadlocks) */
  1253. /*down(&zr->resource_lock);*/
  1254. zoran_close_end_session(file);
  1255. if (zr->user-- == 1) { /* Last process */
  1256. /* Clean up JPEG process */
  1257. wake_up_interruptible(&zr->jpg_capq);
  1258. zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
  1259. zr->jpg_buffers.allocated = 0;
  1260. zr->jpg_buffers.active = ZORAN_FREE;
  1261. /* disable interrupts */
  1262. btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
  1263. if (*zr_debug > 1)
  1264. print_interrupts(zr);
  1265. /* Overlay off */
  1266. zr->v4l_overlay_active = 0;
  1267. zr36057_overlay(zr, 0);
  1268. zr->overlay_mask = NULL;
  1269. /* capture off */
  1270. wake_up_interruptible(&zr->v4l_capq);
  1271. zr36057_set_memgrab(zr, 0);
  1272. zr->v4l_buffers.allocated = 0;
  1273. zr->v4l_buffers.active = ZORAN_FREE;
  1274. zoran_set_pci_master(zr, 0);
  1275. if (!pass_through) { /* Switch to color bar */
  1276. int zero = 0, two = 2;
  1277. decoder_command(zr, DECODER_ENABLE_OUTPUT, &zero);
  1278. encoder_command(zr, ENCODER_SET_INPUT, &two);
  1279. }
  1280. }
  1281. file->private_data = NULL;
  1282. kfree(fh->overlay_mask);
  1283. kfree(fh);
  1284. /* release locks on the i2c modules */
  1285. module_put(zr->decoder->driver->owner);
  1286. if (zr->encoder) {
  1287. module_put(zr->encoder->driver->owner);
  1288. }
  1289. module_put(THIS_MODULE);
  1290. /*up(&zr->resource_lock);*/
  1291. dprintk(4, KERN_INFO "%s: zoran_close() done\n", ZR_DEVNAME(zr));
  1292. return 0;
  1293. }
  1294. static ssize_t
  1295. zoran_read (struct file *file,
  1296. char __user *data,
  1297. size_t count,
  1298. loff_t *ppos)
  1299. {
  1300. /* we simply don't support read() (yet)... */
  1301. return -EINVAL;
  1302. }
  1303. static ssize_t
  1304. zoran_write (struct file *file,
  1305. const char __user *data,
  1306. size_t count,
  1307. loff_t *ppos)
  1308. {
  1309. /* ...and the same goes for write() */
  1310. return -EINVAL;
  1311. }
  1312. static int
  1313. setup_fbuffer (struct file *file,
  1314. void *base,
  1315. const struct zoran_format *fmt,
  1316. int width,
  1317. int height,
  1318. int bytesperline)
  1319. {
  1320. struct zoran_fh *fh = file->private_data;
  1321. struct zoran *zr = fh->zr;
  1322. /* (Ronald) v4l/v4l2 guidelines */
  1323. if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
  1324. return -EPERM;
  1325. /* we need a bytesperline value, even if not given */
  1326. if (!bytesperline)
  1327. bytesperline = width * ((fmt->depth + 7) & ~7) / 8;
  1328. #if 0
  1329. if (zr->overlay_active) {
  1330. /* dzjee... stupid users... don't even bother to turn off
  1331. * overlay before changing the memory location...
  1332. * normally, we would return errors here. However, one of
  1333. * the tools that does this is... xawtv! and since xawtv
  1334. * is used by +/- 99% of the users, we'd rather be user-
  1335. * friendly and silently do as if nothing went wrong */
  1336. dprintk(3,
  1337. KERN_ERR
  1338. "%s: setup_fbuffer() - forced overlay turnoff because framebuffer changed\n",
  1339. ZR_DEVNAME(zr));
  1340. zr36057_overlay(zr, 0);
  1341. }
  1342. #endif
  1343. if (!(fmt->flags & ZORAN_FORMAT_OVERLAY)) {
  1344. dprintk(1,
  1345. KERN_ERR
  1346. "%s: setup_fbuffer() - no valid overlay format given\n",
  1347. ZR_DEVNAME(zr));
  1348. return -EINVAL;
  1349. }
  1350. if (height <= 0 || width <= 0 || bytesperline <= 0) {
  1351. dprintk(1,
  1352. KERN_ERR
  1353. "%s: setup_fbuffer() - invalid height/width/bpl value (%d|%d|%d)\n",
  1354. ZR_DEVNAME(zr), width, height, bytesperline);
  1355. return -EINVAL;
  1356. }
  1357. if (bytesperline & 3) {
  1358. dprintk(1,
  1359. KERN_ERR
  1360. "%s: setup_fbuffer() - bytesperline (%d) must be 4-byte aligned\n",
  1361. ZR_DEVNAME(zr), bytesperline);
  1362. return -EINVAL;
  1363. }
  1364. zr->buffer.base = (void *) ((unsigned long) base & ~3);
  1365. zr->buffer.height = height;
  1366. zr->buffer.width = width;
  1367. zr->buffer.depth = fmt->depth;
  1368. zr->overlay_settings.format = fmt;
  1369. zr->buffer.bytesperline = bytesperline;
  1370. /* The user should set new window parameters */
  1371. zr->overlay_settings.is_set = 0;
  1372. return 0;
  1373. }
  1374. static int
  1375. setup_window (struct file *file,
  1376. int x,
  1377. int y,
  1378. int width,
  1379. int height,
  1380. struct video_clip __user *clips,
  1381. int clipcount,
  1382. void __user *bitmap)
  1383. {
  1384. struct zoran_fh *fh = file->private_data;
  1385. struct zoran *zr = fh->zr;
  1386. struct video_clip *vcp = NULL;
  1387. int on, end;
  1388. if (!zr->buffer.base) {
  1389. dprintk(1,
  1390. KERN_ERR
  1391. "%s: setup_window() - frame buffer has to be set first\n",
  1392. ZR_DEVNAME(zr));
  1393. return -EINVAL;
  1394. }
  1395. if (!fh->overlay_settings.format) {
  1396. dprintk(1,
  1397. KERN_ERR
  1398. "%s: setup_window() - no overlay format set\n",
  1399. ZR_DEVNAME(zr));
  1400. return -EINVAL;
  1401. }
  1402. /*
  1403. * The video front end needs 4-byte alinged line sizes, we correct that
  1404. * silently here if necessary
  1405. */
  1406. if (zr->buffer.depth == 15 || zr->buffer.depth == 16) {
  1407. end = (x + width) & ~1; /* round down */
  1408. x = (x + 1) & ~1; /* round up */
  1409. width = end - x;
  1410. }
  1411. if (zr->buffer.depth == 24) {
  1412. end = (x + width) & ~3; /* round down */
  1413. x = (x + 3) & ~3; /* round up */
  1414. width = end - x;
  1415. }
  1416. if (width > BUZ_MAX_WIDTH)
  1417. width = BUZ_MAX_WIDTH;
  1418. if (height > BUZ_MAX_HEIGHT)
  1419. height = BUZ_MAX_HEIGHT;
  1420. /* Check for vaild parameters */
  1421. if (width < BUZ_MIN_WIDTH || height < BUZ_MIN_HEIGHT ||
  1422. width > BUZ_MAX_WIDTH || height > BUZ_MAX_HEIGHT) {
  1423. dprintk(1,
  1424. KERN_ERR
  1425. "%s: setup_window() - width = %d or height = %d invalid\n",
  1426. ZR_DEVNAME(zr), width, height);
  1427. return -EINVAL;
  1428. }
  1429. fh->overlay_settings.x = x;
  1430. fh->overlay_settings.y = y;
  1431. fh->overlay_settings.width = width;
  1432. fh->overlay_settings.height = height;
  1433. fh->overlay_settings.clipcount = clipcount;
  1434. /*
  1435. * If an overlay is running, we have to switch it off
  1436. * and switch it on again in order to get the new settings in effect.
  1437. *
  1438. * We also want to avoid that the overlay mask is written
  1439. * when an overlay is running.
  1440. */
  1441. on = zr->v4l_overlay_active && !zr->v4l_memgrab_active &&
  1442. zr->overlay_active != ZORAN_FREE &&
  1443. fh->overlay_active != ZORAN_FREE;
  1444. if (on)
  1445. zr36057_overlay(zr, 0);
  1446. /*
  1447. * Write the overlay mask if clips are wanted.
  1448. * We prefer a bitmap.
  1449. */
  1450. if (bitmap) {
  1451. /* fake value - it just means we want clips */
  1452. fh->overlay_settings.clipcount = 1;
  1453. if (copy_from_user(fh->overlay_mask, bitmap,
  1454. (width * height + 7) / 8)) {
  1455. return -EFAULT;
  1456. }
  1457. } else if (clipcount > 0) {
  1458. /* write our own bitmap from the clips */
  1459. vcp = vmalloc(sizeof(struct video_clip) * (clipcount + 4));
  1460. if (vcp == NULL) {
  1461. dprintk(1,
  1462. KERN_ERR
  1463. "%s: setup_window() - Alloc of clip mask failed\n",
  1464. ZR_DEVNAME(zr));
  1465. return -ENOMEM;
  1466. }
  1467. if (copy_from_user
  1468. (vcp, clips, sizeof(struct video_clip) * clipcount)) {
  1469. vfree(vcp);
  1470. return -EFAULT;
  1471. }
  1472. write_overlay_mask(file, vcp, clipcount);
  1473. vfree(vcp);
  1474. }
  1475. fh->overlay_settings.is_set = 1;
  1476. if (fh->overlay_active != ZORAN_FREE &&
  1477. zr->overlay_active != ZORAN_FREE)
  1478. zr->overlay_settings = fh->overlay_settings;
  1479. if (on)
  1480. zr36057_overlay(zr, 1);
  1481. /* Make sure the changes come into effect */
  1482. return wait_grab_pending(zr);
  1483. }
  1484. static int
  1485. setup_overlay (struct file *file,
  1486. int on)
  1487. {
  1488. struct zoran_fh *fh = file->private_data;
  1489. struct zoran *zr = fh->zr;
  1490. /* If there is nothing to do, return immediatly */
  1491. if ((on && fh->overlay_active != ZORAN_FREE) ||
  1492. (!on && fh->overlay_active == ZORAN_FREE))
  1493. return 0;
  1494. /* check whether we're touching someone else's overlay */
  1495. if (on && zr->overlay_active != ZORAN_FREE &&
  1496. fh->overlay_active == ZORAN_FREE) {
  1497. dprintk(1,
  1498. KERN_ERR
  1499. "%s: setup_overlay() - overlay is already active for another session\n",
  1500. ZR_DEVNAME(zr));
  1501. return -EBUSY;
  1502. }
  1503. if (!on && zr->overlay_active != ZORAN_FREE &&
  1504. fh->overlay_active == ZORAN_FREE) {
  1505. dprintk(1,
  1506. KERN_ERR
  1507. "%s: setup_overlay() - you cannot cancel someone else's session\n",
  1508. ZR_DEVNAME(zr));
  1509. return -EPERM;
  1510. }
  1511. if (on == 0) {
  1512. zr->overlay_active = fh->overlay_active = ZORAN_FREE;
  1513. zr->v4l_overlay_active = 0;
  1514. /* When a grab is running, the video simply
  1515. * won't be switched on any more */
  1516. if (!zr->v4l_memgrab_active)
  1517. zr36057_overlay(zr, 0);
  1518. zr->overlay_mask = NULL;
  1519. } else {
  1520. if (!zr->buffer.base || !fh->overlay_settings.is_set) {
  1521. dprintk(1,
  1522. KERN_ERR
  1523. "%s: setup_overlay() - buffer or window not set\n",
  1524. ZR_DEVNAME(zr));
  1525. return -EINVAL;
  1526. }
  1527. if (!fh->overlay_settings.format) {
  1528. dprintk(1,
  1529. KERN_ERR
  1530. "%s: setup_overlay() - no overlay format set\n",
  1531. ZR_DEVNAME(zr));
  1532. return -EINVAL;
  1533. }
  1534. zr->overlay_active = fh->overlay_active = ZORAN_LOCKED;
  1535. zr->v4l_overlay_active = 1;
  1536. zr->overlay_mask = fh->overlay_mask;
  1537. zr->overlay_settings = fh->overlay_settings;
  1538. if (!zr->v4l_memgrab_active)
  1539. zr36057_overlay(zr, 1);
  1540. /* When a grab is running, the video will be
  1541. * switched on when grab is finished */
  1542. }
  1543. /* Make sure the changes come into effect */
  1544. return wait_grab_pending(zr);
  1545. }
  1546. #ifdef HAVE_V4L2
  1547. /* get the status of a buffer in the clients buffer queue */
  1548. static int
  1549. zoran_v4l2_buffer_status (struct file *file,
  1550. struct v4l2_buffer *buf,
  1551. int num)
  1552. {
  1553. struct zoran_fh *fh = file->private_data;
  1554. struct zoran *zr = fh->zr;
  1555. buf->flags = V4L2_BUF_FLAG_MAPPED;
  1556. switch (fh->map_mode) {
  1557. case ZORAN_MAP_MODE_RAW:
  1558. /* check range */
  1559. if (num < 0 || num >= fh->v4l_buffers.num_buffers ||
  1560. !fh->v4l_buffers.allocated) {
  1561. dprintk(1,
  1562. KERN_ERR
  1563. "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
  1564. ZR_DEVNAME(zr));
  1565. return -EINVAL;
  1566. }
  1567. buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1568. buf->length = fh->v4l_buffers.buffer_size;
  1569. /* get buffer */
  1570. buf->bytesused = fh->v4l_buffers.buffer[num].bs.length;
  1571. if (fh->v4l_buffers.buffer[num].state == BUZ_STATE_DONE ||
  1572. fh->v4l_buffers.buffer[num].state == BUZ_STATE_USER) {
  1573. buf->sequence = fh->v4l_buffers.buffer[num].bs.seq;
  1574. buf->flags |= V4L2_BUF_FLAG_DONE;
  1575. buf->timestamp =
  1576. fh->v4l_buffers.buffer[num].bs.timestamp;
  1577. } else {
  1578. buf->flags |= V4L2_BUF_FLAG_QUEUED;
  1579. }
  1580. if (fh->v4l_settings.height <= BUZ_MAX_HEIGHT / 2)
  1581. buf->field = V4L2_FIELD_TOP;
  1582. else
  1583. buf->field = V4L2_FIELD_INTERLACED;
  1584. break;
  1585. case ZORAN_MAP_MODE_JPG_REC:
  1586. case ZORAN_MAP_MODE_JPG_PLAY:
  1587. /* check range */
  1588. if (num < 0 || num >= fh->jpg_buffers.num_buffers ||
  1589. !fh->jpg_buffers.allocated) {
  1590. dprintk(1,
  1591. KERN_ERR
  1592. "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
  1593. ZR_DEVNAME(zr));
  1594. return -EINVAL;
  1595. }
  1596. buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
  1597. V4L2_BUF_TYPE_VIDEO_CAPTURE :
  1598. V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1599. buf->length = fh->jpg_buffers.buffer_size;
  1600. /* these variables are only written after frame has been captured */
  1601. if (fh->jpg_buffers.buffer[num].state == BUZ_STATE_DONE ||
  1602. fh->jpg_buffers.buffer[num].state == BUZ_STATE_USER) {
  1603. buf->sequence = fh->jpg_buffers.buffer[num].bs.seq;
  1604. buf->timestamp =
  1605. fh->jpg_buffers.buffer[num].bs.timestamp;
  1606. buf->bytesused =
  1607. fh->jpg_buffers.buffer[num].bs.length;
  1608. buf->flags |= V4L2_BUF_FLAG_DONE;
  1609. } else {
  1610. buf->flags |= V4L2_BUF_FLAG_QUEUED;
  1611. }
  1612. /* which fields are these? */
  1613. if (fh->jpg_settings.TmpDcm != 1)
  1614. buf->field =
  1615. fh->jpg_settings.
  1616. odd_even ? V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
  1617. else
  1618. buf->field =
  1619. fh->jpg_settings.
  1620. odd_even ? V4L2_FIELD_SEQ_TB :
  1621. V4L2_FIELD_SEQ_BT;
  1622. break;
  1623. default:
  1624. dprintk(5,
  1625. KERN_ERR
  1626. "%s: v4l2_buffer_status() - invalid buffer type|map_mode (%d|%d)\n",
  1627. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  1628. return -EINVAL;
  1629. }
  1630. buf->memory = V4L2_MEMORY_MMAP;
  1631. buf->index = num;
  1632. buf->m.offset = buf->length * num;
  1633. return 0;
  1634. }
  1635. #endif
  1636. static int
  1637. zoran_set_norm (struct zoran *zr,
  1638. int norm) /* VIDEO_MODE_* */
  1639. {
  1640. int norm_encoder, on;
  1641. if (zr->v4l_buffers.active != ZORAN_FREE ||
  1642. zr->jpg_buffers.active != ZORAN_FREE) {
  1643. dprintk(1,
  1644. KERN_WARNING
  1645. "%s: set_norm() called while in playback/capture mode\n",
  1646. ZR_DEVNAME(zr));
  1647. return -EBUSY;
  1648. }
  1649. if (lock_norm && norm != zr->norm) {
  1650. if (lock_norm > 1) {
  1651. dprintk(1,
  1652. KERN_WARNING
  1653. "%s: set_norm() - TV standard is locked, can not switch norm\n",
  1654. ZR_DEVNAME(zr));
  1655. return -EPERM;
  1656. } else {
  1657. dprintk(1,
  1658. KERN_WARNING
  1659. "%s: set_norm() - TV standard is locked, norm was not changed\n",
  1660. ZR_DEVNAME(zr));
  1661. norm = zr->norm;
  1662. }
  1663. }
  1664. if (norm != VIDEO_MODE_AUTO &&
  1665. (norm < 0 || norm >= zr->card.norms ||
  1666. !zr->card.tvn[norm])) {
  1667. dprintk(1,
  1668. KERN_ERR "%s: set_norm() - unsupported norm %d\n",
  1669. ZR_DEVNAME(zr), norm);
  1670. return -EINVAL;
  1671. }
  1672. if (norm == VIDEO_MODE_AUTO) {
  1673. int status;
  1674. /* if we have autodetect, ... */
  1675. struct video_decoder_capability caps;
  1676. decoder_command(zr, DECODER_GET_CAPABILITIES, &caps);
  1677. if (!(caps.flags & VIDEO_DECODER_AUTO)) {
  1678. dprintk(1, KERN_ERR "%s: norm=auto unsupported\n",
  1679. ZR_DEVNAME(zr));
  1680. return -EINVAL;
  1681. }
  1682. decoder_command(zr, DECODER_SET_NORM, &norm);
  1683. /* let changes come into effect */
  1684. ssleep(2);
  1685. decoder_command(zr, DECODER_GET_STATUS, &status);
  1686. if (!(status & DECODER_STATUS_GOOD)) {
  1687. dprintk(1,
  1688. KERN_ERR
  1689. "%s: set_norm() - no norm detected\n",
  1690. ZR_DEVNAME(zr));
  1691. /* reset norm */
  1692. decoder_command(zr, DECODER_SET_NORM, &zr->norm);
  1693. return -EIO;
  1694. }
  1695. if (status & DECODER_STATUS_NTSC)
  1696. norm = VIDEO_MODE_NTSC;
  1697. else if (status & DECODER_STATUS_SECAM)
  1698. norm = VIDEO_MODE_SECAM;
  1699. else
  1700. norm = VIDEO_MODE_PAL;
  1701. }
  1702. zr->timing = zr->card.tvn[norm];
  1703. norm_encoder = norm;
  1704. /* We switch overlay off and on since a change in the
  1705. * norm needs different VFE settings */
  1706. on = zr->overlay_active && !zr->v4l_memgrab_active;
  1707. if (on)
  1708. zr36057_overlay(zr, 0);
  1709. decoder_command(zr, DECODER_SET_NORM, &norm);
  1710. encoder_command(zr, ENCODER_SET_NORM, &norm_encoder);
  1711. if (on)
  1712. zr36057_overlay(zr, 1);
  1713. /* Make sure the changes come into effect */
  1714. zr->norm = norm;
  1715. return 0;
  1716. }
  1717. static int
  1718. zoran_set_input (struct zoran *zr,
  1719. int input)
  1720. {
  1721. int realinput;
  1722. if (input == zr->input) {
  1723. return 0;
  1724. }
  1725. if (zr->v4l_buffers.active != ZORAN_FREE ||
  1726. zr->jpg_buffers.active != ZORAN_FREE) {
  1727. dprintk(1,
  1728. KERN_WARNING
  1729. "%s: set_input() called while in playback/capture mode\n",
  1730. ZR_DEVNAME(zr));
  1731. return -EBUSY;
  1732. }
  1733. if (input < 0 || input >= zr->card.inputs) {
  1734. dprintk(1,
  1735. KERN_ERR
  1736. "%s: set_input() - unnsupported input %d\n",
  1737. ZR_DEVNAME(zr), input);
  1738. return -EINVAL;
  1739. }
  1740. realinput = zr->card.input[input].muxsel;
  1741. zr->input = input;
  1742. decoder_command(zr, DECODER_SET_INPUT, &realinput);
  1743. return 0;
  1744. }
  1745. /*
  1746. * ioctl routine
  1747. */
  1748. static int
  1749. zoran_do_ioctl (struct inode *inode,
  1750. struct file *file,
  1751. unsigned int cmd,
  1752. void *arg)
  1753. {
  1754. struct zoran_fh *fh = file->private_data;
  1755. struct zoran *zr = fh->zr;
  1756. /* CAREFUL: used in multiple places here */
  1757. struct zoran_jpg_settings settings;
  1758. /* we might have older buffers lying around... We don't want
  1759. * to wait, but we do want to try cleaning them up ASAP. So
  1760. * we try to obtain the lock and free them. If that fails, we
  1761. * don't do anything and wait for the next turn. In the end,
  1762. * zoran_close() or a new allocation will still free them...
  1763. * This is just a 'the sooner the better' extra 'feature'
  1764. *
  1765. * We don't free the buffers right on munmap() because that
  1766. * causes oopses (kfree() inside munmap() oopses for no
  1767. * apparent reason - it's also not reproduceable in any way,
  1768. * but moving the free code outside the munmap() handler fixes
  1769. * all this... If someone knows why, please explain me (Ronald)
  1770. */
  1771. if (!down_trylock(&zr->resource_lock)) {
  1772. /* we obtained it! Let's try to free some things */
  1773. if (fh->jpg_buffers.ready_to_be_freed)
  1774. jpg_fbuffer_free(file);
  1775. if (fh->v4l_buffers.ready_to_be_freed)
  1776. v4l_fbuffer_free(file);
  1777. up(&zr->resource_lock);
  1778. }
  1779. switch (cmd) {
  1780. case VIDIOCGCAP:
  1781. {
  1782. struct video_capability *vcap = arg;
  1783. dprintk(3, KERN_DEBUG "%s: VIDIOCGCAP\n", ZR_DEVNAME(zr));
  1784. memset(vcap, 0, sizeof(struct video_capability));
  1785. strncpy(vcap->name, ZR_DEVNAME(zr), sizeof(vcap->name));
  1786. vcap->type = ZORAN_VID_TYPE;
  1787. vcap->channels = zr->card.inputs;
  1788. vcap->audios = 0;
  1789. down(&zr->resource_lock);
  1790. vcap->maxwidth = BUZ_MAX_WIDTH;
  1791. vcap->maxheight = BUZ_MAX_HEIGHT;
  1792. vcap->minwidth = BUZ_MIN_WIDTH;
  1793. vcap->minheight = BUZ_MIN_HEIGHT;
  1794. up(&zr->resource_lock);
  1795. return 0;
  1796. }
  1797. break;
  1798. case VIDIOCGCHAN:
  1799. {
  1800. struct video_channel *vchan = arg;
  1801. int channel = vchan->channel;
  1802. dprintk(3, KERN_DEBUG "%s: VIDIOCGCHAN - channel=%d\n",
  1803. ZR_DEVNAME(zr), vchan->channel);
  1804. memset(vchan, 0, sizeof(struct video_channel));
  1805. if (channel > zr->card.inputs || channel < 0) {
  1806. dprintk(1,
  1807. KERN_ERR
  1808. "%s: VIDIOCGCHAN on not existing channel %d\n",
  1809. ZR_DEVNAME(zr), channel);
  1810. return -EINVAL;
  1811. }
  1812. strcpy(vchan->name, zr->card.input[channel].name);
  1813. vchan->tuners = 0;
  1814. vchan->flags = 0;
  1815. vchan->type = VIDEO_TYPE_CAMERA;
  1816. down(&zr->resource_lock);
  1817. vchan->norm = zr->norm;
  1818. up(&zr->resource_lock);
  1819. vchan->channel = channel;
  1820. return 0;
  1821. }
  1822. break;
  1823. /* RJ: the documentation at http://roadrunner.swansea.linux.org.uk/v4lapi.shtml says:
  1824. *
  1825. * * "The VIDIOCSCHAN ioctl takes an integer argument and switches the capture to this input."
  1826. * * ^^^^^^^
  1827. * * The famos BTTV driver has it implemented with a struct video_channel argument
  1828. * * and we follow it for compatibility reasons
  1829. * *
  1830. * * BTW: this is the only way the user can set the norm!
  1831. */
  1832. case VIDIOCSCHAN:
  1833. {
  1834. struct video_channel *vchan = arg;
  1835. int res;
  1836. dprintk(3,
  1837. KERN_DEBUG
  1838. "%s: VIDIOCSCHAN - channel=%d, norm=%d\n",
  1839. ZR_DEVNAME(zr), vchan->channel, vchan->norm);
  1840. down(&zr->resource_lock);
  1841. if ((res = zoran_set_input(zr, vchan->channel)))
  1842. goto schan_unlock_and_return;
  1843. if ((res = zoran_set_norm(zr, vchan->norm)))
  1844. goto schan_unlock_and_return;
  1845. /* Make sure the changes come into effect */
  1846. res = wait_grab_pending(zr);
  1847. schan_unlock_and_return:
  1848. up(&zr->resource_lock);
  1849. return res;
  1850. }
  1851. break;
  1852. case VIDIOCGPICT:
  1853. {
  1854. struct video_picture *vpict = arg;
  1855. dprintk(3, KERN_DEBUG "%s: VIDIOCGPICT\n", ZR_DEVNAME(zr));
  1856. memset(vpict, 0, sizeof(struct video_picture));
  1857. down(&zr->resource_lock);
  1858. vpict->hue = zr->hue;
  1859. vpict->brightness = zr->brightness;
  1860. vpict->contrast = zr->contrast;
  1861. vpict->colour = zr->saturation;
  1862. if (fh->overlay_settings.format) {
  1863. vpict->depth = fh->overlay_settings.format->depth;
  1864. vpict->palette = fh->overlay_settings.format->palette;
  1865. } else {
  1866. vpict->depth = 0;
  1867. }
  1868. up(&zr->resource_lock);
  1869. return 0;
  1870. }
  1871. break;
  1872. case VIDIOCSPICT:
  1873. {
  1874. struct video_picture *vpict = arg;
  1875. int i;
  1876. dprintk(3,
  1877. KERN_DEBUG
  1878. "%s: VIDIOCSPICT - bri=%d, hue=%d, col=%d, con=%d, dep=%d, pal=%d\n",
  1879. ZR_DEVNAME(zr), vpict->brightness, vpict->hue,
  1880. vpict->colour, vpict->contrast, vpict->depth,
  1881. vpict->palette);
  1882. for (i = 0; i < zoran_num_formats; i++) {
  1883. const struct zoran_format *fmt = &zoran_formats[i];
  1884. if (fmt->palette != -1 &&
  1885. fmt->flags & ZORAN_FORMAT_OVERLAY &&
  1886. fmt->palette == vpict->palette &&
  1887. fmt->depth == vpict->depth)
  1888. break;
  1889. }
  1890. if (i == zoran_num_formats) {
  1891. dprintk(1,
  1892. KERN_ERR
  1893. "%s: VIDIOCSPICT - Invalid palette %d\n",
  1894. ZR_DEVNAME(zr), vpict->palette);
  1895. return -EINVAL;
  1896. }
  1897. down(&zr->resource_lock);
  1898. decoder_command(zr, DECODER_SET_PICTURE, vpict);
  1899. zr->hue = vpict->hue;
  1900. zr->contrast = vpict->contrast;
  1901. zr->saturation = vpict->colour;
  1902. zr->brightness = vpict->brightness;
  1903. fh->overlay_settings.format = &zoran_formats[i];
  1904. up(&zr->resource_lock);
  1905. return 0;
  1906. }
  1907. break;
  1908. case VIDIOCCAPTURE:
  1909. {
  1910. int *on = arg, res;
  1911. dprintk(3, KERN_DEBUG "%s: VIDIOCCAPTURE - on=%d\n",
  1912. ZR_DEVNAME(zr), *on);
  1913. down(&zr->resource_lock);
  1914. res = setup_overlay(file, *on);
  1915. up(&zr->resource_lock);
  1916. return res;
  1917. }
  1918. break;
  1919. case VIDIOCGWIN:
  1920. {
  1921. struct video_window *vwin = arg;
  1922. dprintk(3, KERN_DEBUG "%s: VIDIOCGWIN\n", ZR_DEVNAME(zr));
  1923. memset(vwin, 0, sizeof(struct video_window));
  1924. down(&zr->resource_lock);
  1925. vwin->x = fh->overlay_settings.x;
  1926. vwin->y = fh->overlay_settings.y;
  1927. vwin->width = fh->overlay_settings.width;
  1928. vwin->height = fh->overlay_settings.height;
  1929. up(&zr->resource_lock);
  1930. vwin->clipcount = 0;
  1931. return 0;
  1932. }
  1933. break;
  1934. case VIDIOCSWIN:
  1935. {
  1936. struct video_window *vwin = arg;
  1937. int res;
  1938. dprintk(3,
  1939. KERN_DEBUG
  1940. "%s: VIDIOCSWIN - x=%d, y=%d, w=%d, h=%d, clipcount=%d\n",
  1941. ZR_DEVNAME(zr), vwin->x, vwin->y, vwin->width,
  1942. vwin->height, vwin->clipcount);
  1943. down(&zr->resource_lock);
  1944. res =
  1945. setup_window(file, vwin->x, vwin->y, vwin->width,
  1946. vwin->height, vwin->clips,
  1947. vwin->clipcount, NULL);
  1948. up(&zr->resource_lock);
  1949. return res;
  1950. }
  1951. break;
  1952. case VIDIOCGFBUF:
  1953. {
  1954. struct video_buffer *vbuf = arg;
  1955. dprintk(3, KERN_DEBUG "%s: VIDIOCGFBUF\n", ZR_DEVNAME(zr));
  1956. down(&zr->resource_lock);
  1957. *vbuf = zr->buffer;
  1958. up(&zr->resource_lock);
  1959. return 0;
  1960. }
  1961. break;
  1962. case VIDIOCSFBUF:
  1963. {
  1964. struct video_buffer *vbuf = arg;
  1965. int i, res = 0;
  1966. dprintk(3,
  1967. KERN_DEBUG
  1968. "%s: VIDIOCSFBUF - base=%p, w=%d, h=%d, depth=%d, bpl=%d\n",
  1969. ZR_DEVNAME(zr), vbuf->base, vbuf->width,
  1970. vbuf->height, vbuf->depth, vbuf->bytesperline);
  1971. for (i = 0; i < zoran_num_formats; i++)
  1972. if (zoran_formats[i].depth == vbuf->depth)
  1973. break;
  1974. if (i == zoran_num_formats) {
  1975. dprintk(1,
  1976. KERN_ERR
  1977. "%s: VIDIOCSFBUF - invalid fbuf depth %d\n",
  1978. ZR_DEVNAME(zr), vbuf->depth);
  1979. return -EINVAL;
  1980. }
  1981. down(&zr->resource_lock);
  1982. res =
  1983. setup_fbuffer(file, vbuf->base, &zoran_formats[i],
  1984. vbuf->width, vbuf->height,
  1985. vbuf->bytesperline);
  1986. up(&zr->resource_lock);
  1987. return res;
  1988. }
  1989. break;
  1990. case VIDIOCSYNC:
  1991. {
  1992. int *frame = arg, res;
  1993. dprintk(3, KERN_DEBUG "%s: VIDIOCSYNC - frame=%d\n",
  1994. ZR_DEVNAME(zr), *frame);
  1995. down(&zr->resource_lock);
  1996. res = v4l_sync(file, *frame);
  1997. up(&zr->resource_lock);
  1998. if (!res)
  1999. zr->v4l_sync_tail++;
  2000. return res;
  2001. }
  2002. break;
  2003. case VIDIOCMCAPTURE:
  2004. {
  2005. struct video_mmap *vmap = arg;
  2006. int res;
  2007. dprintk(3,
  2008. KERN_DEBUG
  2009. "%s: VIDIOCMCAPTURE - frame=%d, geom=%dx%d, fmt=%d\n",
  2010. ZR_DEVNAME(zr), vmap->frame, vmap->width, vmap->height,
  2011. vmap->format);
  2012. down(&zr->resource_lock);
  2013. res = v4l_grab(file, vmap);
  2014. up(&zr->resource_lock);
  2015. return res;
  2016. }
  2017. break;
  2018. case VIDIOCGMBUF:
  2019. {
  2020. struct video_mbuf *vmbuf = arg;
  2021. int i, res = 0;
  2022. dprintk(3, KERN_DEBUG "%s: VIDIOCGMBUF\n", ZR_DEVNAME(zr));
  2023. vmbuf->size =
  2024. fh->v4l_buffers.num_buffers *
  2025. fh->v4l_buffers.buffer_size;
  2026. vmbuf->frames = fh->v4l_buffers.num_buffers;
  2027. for (i = 0; i < vmbuf->frames; i++) {
  2028. vmbuf->offsets[i] =
  2029. i * fh->v4l_buffers.buffer_size;
  2030. }
  2031. down(&zr->resource_lock);
  2032. if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
  2033. dprintk(1,
  2034. KERN_ERR
  2035. "%s: VIDIOCGMBUF - buffers already allocated\n",
  2036. ZR_DEVNAME(zr));
  2037. res = -EINVAL;
  2038. goto v4l1reqbuf_unlock_and_return;
  2039. }
  2040. if (v4l_fbuffer_alloc(file)) {
  2041. res = -ENOMEM;
  2042. goto v4l1reqbuf_unlock_and_return;
  2043. }
  2044. /* The next mmap will map the V4L buffers */
  2045. fh->map_mode = ZORAN_MAP_MODE_RAW;
  2046. v4l1reqbuf_unlock_and_return:
  2047. up(&zr->resource_lock);
  2048. return res;
  2049. }
  2050. break;
  2051. case VIDIOCGUNIT:
  2052. {
  2053. struct video_unit *vunit = arg;
  2054. dprintk(3, KERN_DEBUG "%s: VIDIOCGUNIT\n", ZR_DEVNAME(zr));
  2055. vunit->video = zr->video_dev->minor;
  2056. vunit->vbi = VIDEO_NO_UNIT;
  2057. vunit->radio = VIDEO_NO_UNIT;
  2058. vunit->audio = VIDEO_NO_UNIT;
  2059. vunit->teletext = VIDEO_NO_UNIT;
  2060. return 0;
  2061. }
  2062. break;
  2063. /*
  2064. * RJ: In principal we could support subcaptures for V4L grabbing.
  2065. * Not even the famous BTTV driver has them, however.
  2066. * If there should be a strong demand, one could consider
  2067. * to implement them.
  2068. */
  2069. case VIDIOCGCAPTURE:
  2070. {
  2071. dprintk(3, KERN_ERR "%s: VIDIOCGCAPTURE not supported\n",
  2072. ZR_DEVNAME(zr));
  2073. return -EINVAL;
  2074. }
  2075. break;
  2076. case VIDIOCSCAPTURE:
  2077. {
  2078. dprintk(3, KERN_ERR "%s: VIDIOCSCAPTURE not supported\n",
  2079. ZR_DEVNAME(zr));
  2080. return -EINVAL;
  2081. }
  2082. break;
  2083. case BUZIOC_G_PARAMS:
  2084. {
  2085. struct zoran_params *bparams = arg;
  2086. dprintk(3, KERN_DEBUG "%s: BUZIOC_G_PARAMS\n", ZR_DEVNAME(zr));
  2087. memset(bparams, 0, sizeof(struct zoran_params));
  2088. bparams->major_version = MAJOR_VERSION;
  2089. bparams->minor_version = MINOR_VERSION;
  2090. down(&zr->resource_lock);
  2091. bparams->norm = zr->norm;
  2092. bparams->input = zr->input;
  2093. bparams->decimation = fh->jpg_settings.decimation;
  2094. bparams->HorDcm = fh->jpg_settings.HorDcm;
  2095. bparams->VerDcm = fh->jpg_settings.VerDcm;
  2096. bparams->TmpDcm = fh->jpg_settings.TmpDcm;
  2097. bparams->field_per_buff = fh->jpg_settings.field_per_buff;
  2098. bparams->img_x = fh->jpg_settings.img_x;
  2099. bparams->img_y = fh->jpg_settings.img_y;
  2100. bparams->img_width = fh->jpg_settings.img_width;
  2101. bparams->img_height = fh->jpg_settings.img_height;
  2102. bparams->odd_even = fh->jpg_settings.odd_even;
  2103. bparams->quality = fh->jpg_settings.jpg_comp.quality;
  2104. bparams->APPn = fh->jpg_settings.jpg_comp.APPn;
  2105. bparams->APP_len = fh->jpg_settings.jpg_comp.APP_len;
  2106. memcpy(bparams->APP_data,
  2107. fh->jpg_settings.jpg_comp.APP_data,
  2108. sizeof(bparams->APP_data));
  2109. bparams->COM_len = zr->jpg_settings.jpg_comp.COM_len;
  2110. memcpy(bparams->COM_data,
  2111. fh->jpg_settings.jpg_comp.COM_data,
  2112. sizeof(bparams->COM_data));
  2113. bparams->jpeg_markers =
  2114. fh->jpg_settings.jpg_comp.jpeg_markers;
  2115. up(&zr->resource_lock);
  2116. bparams->VFIFO_FB = 0;
  2117. return 0;
  2118. }
  2119. break;
  2120. case BUZIOC_S_PARAMS:
  2121. {
  2122. struct zoran_params *bparams = arg;
  2123. int res = 0;
  2124. dprintk(3, KERN_DEBUG "%s: BUZIOC_S_PARAMS\n", ZR_DEVNAME(zr));
  2125. settings.decimation = bparams->decimation;
  2126. settings.HorDcm = bparams->HorDcm;
  2127. settings.VerDcm = bparams->VerDcm;
  2128. settings.TmpDcm = bparams->TmpDcm;
  2129. settings.field_per_buff = bparams->field_per_buff;
  2130. settings.img_x = bparams->img_x;
  2131. settings.img_y = bparams->img_y;
  2132. settings.img_width = bparams->img_width;
  2133. settings.img_height = bparams->img_height;
  2134. settings.odd_even = bparams->odd_even;
  2135. settings.jpg_comp.quality = bparams->quality;
  2136. settings.jpg_comp.APPn = bparams->APPn;
  2137. settings.jpg_comp.APP_len = bparams->APP_len;
  2138. memcpy(settings.jpg_comp.APP_data, bparams->APP_data,
  2139. sizeof(bparams->APP_data));
  2140. settings.jpg_comp.COM_len = bparams->COM_len;
  2141. memcpy(settings.jpg_comp.COM_data, bparams->COM_data,
  2142. sizeof(bparams->COM_data));
  2143. settings.jpg_comp.jpeg_markers = bparams->jpeg_markers;
  2144. down(&zr->resource_lock);
  2145. if (zr->codec_mode != BUZ_MODE_IDLE) {
  2146. dprintk(1,
  2147. KERN_ERR
  2148. "%s: BUZIOC_S_PARAMS called, but Buz in capture/playback mode\n",
  2149. ZR_DEVNAME(zr));
  2150. res = -EINVAL;
  2151. goto sparams_unlock_and_return;
  2152. }
  2153. /* Check the params first before overwriting our
  2154. * nternal values */
  2155. if (zoran_check_jpg_settings(zr, &settings)) {
  2156. res = -EINVAL;
  2157. goto sparams_unlock_and_return;
  2158. }
  2159. fh->jpg_settings = settings;
  2160. sparams_unlock_and_return:
  2161. up(&zr->resource_lock);
  2162. return res;
  2163. }
  2164. break;
  2165. case BUZIOC_REQBUFS:
  2166. {
  2167. struct zoran_requestbuffers *breq = arg;
  2168. int res = 0;
  2169. dprintk(3,
  2170. KERN_DEBUG
  2171. "%s: BUZIOC_REQBUFS - count=%lu, size=%lu\n",
  2172. ZR_DEVNAME(zr), breq->count, breq->size);
  2173. /* Enforce reasonable lower and upper limits */
  2174. if (breq->count < 4)
  2175. breq->count = 4; /* Could be choosen smaller */
  2176. if (breq->count > jpg_nbufs)
  2177. breq->count = jpg_nbufs;
  2178. breq->size = PAGE_ALIGN(breq->size);
  2179. if (breq->size < 8192)
  2180. breq->size = 8192; /* Arbitrary */
  2181. /* breq->size is limited by 1 page for the stat_com
  2182. * tables to a Maximum of 2 MB */
  2183. if (breq->size > jpg_bufsize)
  2184. breq->size = jpg_bufsize;
  2185. if (fh->jpg_buffers.need_contiguous &&
  2186. breq->size > MAX_KMALLOC_MEM)
  2187. breq->size = MAX_KMALLOC_MEM;
  2188. down(&zr->resource_lock);
  2189. if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
  2190. dprintk(1,
  2191. KERN_ERR
  2192. "%s: BUZIOC_REQBUFS - buffers allready allocated\n",
  2193. ZR_DEVNAME(zr));
  2194. res = -EBUSY;
  2195. goto jpgreqbuf_unlock_and_return;
  2196. }
  2197. fh->jpg_buffers.num_buffers = breq->count;
  2198. fh->jpg_buffers.buffer_size = breq->size;
  2199. if (jpg_fbuffer_alloc(file)) {
  2200. res = -ENOMEM;
  2201. goto jpgreqbuf_unlock_and_return;
  2202. }
  2203. /* The next mmap will map the MJPEG buffers - could
  2204. * also be *_PLAY, but it doesn't matter here */
  2205. fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
  2206. jpgreqbuf_unlock_and_return:
  2207. up(&zr->resource_lock);
  2208. return res;
  2209. }
  2210. break;
  2211. case BUZIOC_QBUF_CAPT:
  2212. {
  2213. int *frame = arg, res;
  2214. dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_CAPT - frame=%d\n",
  2215. ZR_DEVNAME(zr), *frame);
  2216. down(&zr->resource_lock);
  2217. res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_COMPRESS);
  2218. up(&zr->resource_lock);
  2219. return res;
  2220. }
  2221. break;
  2222. case BUZIOC_QBUF_PLAY:
  2223. {
  2224. int *frame = arg, res;
  2225. dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_PLAY - frame=%d\n",
  2226. ZR_DEVNAME(zr), *frame);
  2227. down(&zr->resource_lock);
  2228. res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_DECOMPRESS);
  2229. up(&zr->resource_lock);
  2230. return res;
  2231. }
  2232. break;
  2233. case BUZIOC_SYNC:
  2234. {
  2235. struct zoran_sync *bsync = arg;
  2236. int res;
  2237. dprintk(3, KERN_DEBUG "%s: BUZIOC_SYNC\n", ZR_DEVNAME(zr));
  2238. down(&zr->resource_lock);
  2239. res = jpg_sync(file, bsync);
  2240. up(&zr->resource_lock);
  2241. return res;
  2242. }
  2243. break;
  2244. case BUZIOC_G_STATUS:
  2245. {
  2246. struct zoran_status *bstat = arg;
  2247. int norm, input, status, res = 0;
  2248. dprintk(3, KERN_DEBUG "%s: BUZIOC_G_STATUS\n", ZR_DEVNAME(zr));
  2249. if (zr->codec_mode != BUZ_MODE_IDLE) {
  2250. dprintk(1,
  2251. KERN_ERR
  2252. "%s: BUZIOC_G_STATUS called but Buz in capture/playback mode\n",
  2253. ZR_DEVNAME(zr));
  2254. return -EINVAL;
  2255. }
  2256. input = zr->card.input[bstat->input].muxsel;
  2257. norm = VIDEO_MODE_AUTO;
  2258. down(&zr->resource_lock);
  2259. if (zr->codec_mode != BUZ_MODE_IDLE) {
  2260. dprintk(1,
  2261. KERN_ERR
  2262. "%s: BUZIOC_G_STATUS called, but Buz in capture/playback mode\n",
  2263. ZR_DEVNAME(zr));
  2264. res = -EINVAL;
  2265. goto gstat_unlock_and_return;
  2266. }
  2267. decoder_command(zr, DECODER_SET_INPUT, &input);
  2268. decoder_command(zr, DECODER_SET_NORM, &norm);
  2269. /* sleep 1 second */
  2270. ssleep(1);
  2271. /* Get status of video decoder */
  2272. decoder_command(zr, DECODER_GET_STATUS, &status);
  2273. /* restore previous input and norm */
  2274. input = zr->card.input[zr->input].muxsel;
  2275. decoder_command(zr, DECODER_SET_INPUT, &input);
  2276. decoder_command(zr, DECODER_SET_NORM, &zr->norm);
  2277. gstat_unlock_and_return:
  2278. up(&zr->resource_lock);
  2279. if (!res) {
  2280. bstat->signal =
  2281. (status & DECODER_STATUS_GOOD) ? 1 : 0;
  2282. if (status & DECODER_STATUS_NTSC)
  2283. bstat->norm = VIDEO_MODE_NTSC;
  2284. else if (status & DECODER_STATUS_SECAM)
  2285. bstat->norm = VIDEO_MODE_SECAM;
  2286. else
  2287. bstat->norm = VIDEO_MODE_PAL;
  2288. bstat->color =
  2289. (status & DECODER_STATUS_COLOR) ? 1 : 0;
  2290. }
  2291. return res;
  2292. }
  2293. break;
  2294. #ifdef HAVE_V4L2
  2295. /* The new video4linux2 capture interface - much nicer than video4linux1, since
  2296. * it allows for integrating the JPEG capturing calls inside standard v4l2
  2297. */
  2298. case VIDIOC_QUERYCAP:
  2299. {
  2300. struct v4l2_capability *cap = arg;
  2301. dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCAP\n", ZR_DEVNAME(zr));
  2302. memset(cap, 0, sizeof(*cap));
  2303. strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card));
  2304. strncpy(cap->driver, "zoran", sizeof(cap->driver));
  2305. snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
  2306. pci_name(zr->pci_dev));
  2307. cap->version =
  2308. KERNEL_VERSION(MAJOR_VERSION, MINOR_VERSION,
  2309. RELEASE_VERSION);
  2310. cap->capabilities = ZORAN_V4L2_VID_FLAGS;
  2311. return 0;
  2312. }
  2313. break;
  2314. case VIDIOC_ENUM_FMT:
  2315. {
  2316. struct v4l2_fmtdesc *fmt = arg;
  2317. int index = fmt->index, num = -1, i, flag = 0, type =
  2318. fmt->type;
  2319. dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUM_FMT - index=%d\n",
  2320. ZR_DEVNAME(zr), fmt->index);
  2321. switch (fmt->type) {
  2322. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  2323. flag = ZORAN_FORMAT_CAPTURE;
  2324. break;
  2325. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  2326. flag = ZORAN_FORMAT_PLAYBACK;
  2327. break;
  2328. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2329. flag = ZORAN_FORMAT_OVERLAY;
  2330. break;
  2331. default:
  2332. dprintk(1,
  2333. KERN_ERR
  2334. "%s: VIDIOC_ENUM_FMT - unknown type %d\n",
  2335. ZR_DEVNAME(zr), fmt->type);
  2336. return -EINVAL;
  2337. }
  2338. for (i = 0; i < zoran_num_formats; i++) {
  2339. if (zoran_formats[i].flags & flag)
  2340. num++;
  2341. if (num == fmt->index)
  2342. break;
  2343. }
  2344. if (fmt->index < 0 /* late, but not too late */ ||
  2345. i == zoran_num_formats)
  2346. return -EINVAL;
  2347. memset(fmt, 0, sizeof(*fmt));
  2348. fmt->index = index;
  2349. fmt->type = type;
  2350. strncpy(fmt->description, zoran_formats[i].name, 31);
  2351. fmt->pixelformat = zoran_formats[i].fourcc;
  2352. if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
  2353. fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
  2354. return 0;
  2355. }
  2356. break;
  2357. case VIDIOC_G_FMT:
  2358. {
  2359. struct v4l2_format *fmt = arg;
  2360. int type = fmt->type;
  2361. dprintk(5, KERN_DEBUG "%s: VIDIOC_G_FMT\n", ZR_DEVNAME(zr));
  2362. memset(fmt, 0, sizeof(*fmt));
  2363. fmt->type = type;
  2364. switch (fmt->type) {
  2365. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2366. down(&zr->resource_lock);
  2367. fmt->fmt.win.w.left = fh->overlay_settings.x;
  2368. fmt->fmt.win.w.top = fh->overlay_settings.y;
  2369. fmt->fmt.win.w.width = fh->overlay_settings.width;
  2370. fmt->fmt.win.w.height =
  2371. fh->overlay_settings.height;
  2372. if (fh->overlay_settings.width * 2 >
  2373. BUZ_MAX_HEIGHT)
  2374. fmt->fmt.win.field = V4L2_FIELD_INTERLACED;
  2375. else
  2376. fmt->fmt.win.field = V4L2_FIELD_TOP;
  2377. up(&zr->resource_lock);
  2378. break;
  2379. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  2380. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  2381. down(&zr->resource_lock);
  2382. if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  2383. fh->map_mode == ZORAN_MAP_MODE_RAW) {
  2384. fmt->fmt.pix.width =
  2385. fh->v4l_settings.width;
  2386. fmt->fmt.pix.height =
  2387. fh->v4l_settings.height;
  2388. fmt->fmt.pix.sizeimage =
  2389. fh->v4l_buffers.buffer_size;
  2390. fmt->fmt.pix.pixelformat =
  2391. fh->v4l_settings.format->fourcc;
  2392. fmt->fmt.pix.colorspace =
  2393. fh->v4l_settings.format->colorspace;
  2394. fmt->fmt.pix.bytesperline = 0;
  2395. if (BUZ_MAX_HEIGHT <
  2396. (fh->v4l_settings.height * 2))
  2397. fmt->fmt.pix.field =
  2398. V4L2_FIELD_INTERLACED;
  2399. else
  2400. fmt->fmt.pix.field =
  2401. V4L2_FIELD_TOP;
  2402. } else {
  2403. fmt->fmt.pix.width =
  2404. fh->jpg_settings.img_width /
  2405. fh->jpg_settings.HorDcm;
  2406. fmt->fmt.pix.height =
  2407. fh->jpg_settings.img_height /
  2408. (fh->jpg_settings.VerDcm *
  2409. fh->jpg_settings.TmpDcm);
  2410. fmt->fmt.pix.sizeimage =
  2411. zoran_v4l2_calc_bufsize(&fh->
  2412. jpg_settings);
  2413. fmt->fmt.pix.pixelformat =
  2414. V4L2_PIX_FMT_MJPEG;
  2415. if (fh->jpg_settings.TmpDcm == 1)
  2416. fmt->fmt.pix.field =
  2417. (fh->jpg_settings.
  2418. odd_even ? V4L2_FIELD_SEQ_BT :
  2419. V4L2_FIELD_SEQ_BT);
  2420. else
  2421. fmt->fmt.pix.field =
  2422. (fh->jpg_settings.
  2423. odd_even ? V4L2_FIELD_TOP :
  2424. V4L2_FIELD_BOTTOM);
  2425. fmt->fmt.pix.bytesperline = 0;
  2426. fmt->fmt.pix.colorspace =
  2427. V4L2_COLORSPACE_SMPTE170M;
  2428. }
  2429. up(&zr->resource_lock);
  2430. break;
  2431. default:
  2432. dprintk(1,
  2433. KERN_ERR
  2434. "%s: VIDIOC_G_FMT - unsupported type %d\n",
  2435. ZR_DEVNAME(zr), fmt->type);
  2436. return -EINVAL;
  2437. }
  2438. return 0;
  2439. }
  2440. break;
  2441. case VIDIOC_S_FMT:
  2442. {
  2443. struct v4l2_format *fmt = arg;
  2444. int i, res = 0;
  2445. __u32 printformat;
  2446. dprintk(3, KERN_DEBUG "%s: VIDIOC_S_FMT - type=%d, ",
  2447. ZR_DEVNAME(zr), fmt->type);
  2448. switch (fmt->type) {
  2449. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2450. dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n",
  2451. fmt->fmt.win.w.left, fmt->fmt.win.w.top,
  2452. fmt->fmt.win.w.width,
  2453. fmt->fmt.win.w.height,
  2454. fmt->fmt.win.clipcount,
  2455. fmt->fmt.win.bitmap);
  2456. down(&zr->resource_lock);
  2457. res =
  2458. setup_window(file, fmt->fmt.win.w.left,
  2459. fmt->fmt.win.w.top,
  2460. fmt->fmt.win.w.width,
  2461. fmt->fmt.win.w.height,
  2462. (struct video_clip __user *)
  2463. fmt->fmt.win.clips,
  2464. fmt->fmt.win.clipcount,
  2465. fmt->fmt.win.bitmap);
  2466. up(&zr->resource_lock);
  2467. return res;
  2468. break;
  2469. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  2470. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  2471. printformat =
  2472. __cpu_to_le32(fmt->fmt.pix.pixelformat);
  2473. dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n",
  2474. fmt->fmt.pix.width, fmt->fmt.pix.height,
  2475. fmt->fmt.pix.pixelformat,
  2476. (char *) &printformat);
  2477. if (fmt->fmt.pix.bytesperline > 0) {
  2478. dprintk(5,
  2479. KERN_ERR "%s: bpl not supported\n",
  2480. ZR_DEVNAME(zr));
  2481. return -EINVAL;
  2482. }
  2483. /* we can be requested to do JPEG/raw playback/capture */
  2484. if (!
  2485. (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  2486. (fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  2487. fmt->fmt.pix.pixelformat ==
  2488. V4L2_PIX_FMT_MJPEG))) {
  2489. dprintk(1,
  2490. KERN_ERR
  2491. "%s: VIDIOC_S_FMT - unknown type %d/0x%x(%4.4s) combination\n",
  2492. ZR_DEVNAME(zr), fmt->type,
  2493. fmt->fmt.pix.pixelformat,
  2494. (char *) &printformat);
  2495. return -EINVAL;
  2496. }
  2497. if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
  2498. down(&zr->resource_lock);
  2499. settings = fh->jpg_settings;
  2500. if (fh->v4l_buffers.allocated ||
  2501. fh->jpg_buffers.allocated) {
  2502. dprintk(1,
  2503. KERN_ERR
  2504. "%s: VIDIOC_S_FMT - cannot change capture mode\n",
  2505. ZR_DEVNAME(zr));
  2506. res = -EBUSY;
  2507. goto sfmtjpg_unlock_and_return;
  2508. }
  2509. /* we actually need to set 'real' parameters now */
  2510. if ((fmt->fmt.pix.height * 2) >
  2511. BUZ_MAX_HEIGHT)
  2512. settings.TmpDcm = 1;
  2513. else
  2514. settings.TmpDcm = 2;
  2515. settings.decimation = 0;
  2516. if (fmt->fmt.pix.height <=
  2517. fh->jpg_settings.img_height / 2)
  2518. settings.VerDcm = 2;
  2519. else
  2520. settings.VerDcm = 1;
  2521. if (fmt->fmt.pix.width <=
  2522. fh->jpg_settings.img_width / 4)
  2523. settings.HorDcm = 4;
  2524. else if (fmt->fmt.pix.width <=
  2525. fh->jpg_settings.img_width / 2)
  2526. settings.HorDcm = 2;
  2527. else
  2528. settings.HorDcm = 1;
  2529. if (settings.TmpDcm == 1)
  2530. settings.field_per_buff = 2;
  2531. else
  2532. settings.field_per_buff = 1;
  2533. /* check */
  2534. if ((res =
  2535. zoran_check_jpg_settings(zr,
  2536. &settings)))
  2537. goto sfmtjpg_unlock_and_return;
  2538. /* it's ok, so set them */
  2539. fh->jpg_settings = settings;
  2540. /* tell the user what we actually did */
  2541. fmt->fmt.pix.width =
  2542. settings.img_width / settings.HorDcm;
  2543. fmt->fmt.pix.height =
  2544. settings.img_height * 2 /
  2545. (settings.TmpDcm * settings.VerDcm);
  2546. if (settings.TmpDcm == 1)
  2547. fmt->fmt.pix.field =
  2548. (fh->jpg_settings.
  2549. odd_even ? V4L2_FIELD_SEQ_TB :
  2550. V4L2_FIELD_SEQ_BT);
  2551. else
  2552. fmt->fmt.pix.field =
  2553. (fh->jpg_settings.
  2554. odd_even ? V4L2_FIELD_TOP :
  2555. V4L2_FIELD_BOTTOM);
  2556. fh->jpg_buffers.buffer_size =
  2557. zoran_v4l2_calc_bufsize(&fh->
  2558. jpg_settings);
  2559. fmt->fmt.pix.sizeimage =
  2560. fh->jpg_buffers.buffer_size;
  2561. /* we hereby abuse this variable to show that
  2562. * we're gonna do mjpeg capture */
  2563. fh->map_mode =
  2564. (fmt->type ==
  2565. V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
  2566. ZORAN_MAP_MODE_JPG_REC :
  2567. ZORAN_MAP_MODE_JPG_PLAY;
  2568. sfmtjpg_unlock_and_return:
  2569. up(&zr->resource_lock);
  2570. } else {
  2571. for (i = 0; i < zoran_num_formats; i++)
  2572. if (fmt->fmt.pix.pixelformat ==
  2573. zoran_formats[i].fourcc)
  2574. break;
  2575. if (i == zoran_num_formats) {
  2576. dprintk(1,
  2577. KERN_ERR
  2578. "%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x (%4.4s)\n",
  2579. ZR_DEVNAME(zr),
  2580. fmt->fmt.pix.pixelformat,
  2581. (char *) &printformat);
  2582. return -EINVAL;
  2583. }
  2584. down(&zr->resource_lock);
  2585. if (fh->jpg_buffers.allocated ||
  2586. (fh->v4l_buffers.allocated &&
  2587. fh->v4l_buffers.active !=
  2588. ZORAN_FREE)) {
  2589. dprintk(1,
  2590. KERN_ERR
  2591. "%s: VIDIOC_S_FMT - cannot change capture mode\n",
  2592. ZR_DEVNAME(zr));
  2593. res = -EBUSY;
  2594. goto sfmtv4l_unlock_and_return;
  2595. }
  2596. if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
  2597. fmt->fmt.pix.height =
  2598. BUZ_MAX_HEIGHT;
  2599. if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
  2600. fmt->fmt.pix.width = BUZ_MAX_WIDTH;
  2601. if ((res =
  2602. zoran_v4l_set_format(file,
  2603. fmt->fmt.pix.
  2604. width,
  2605. fmt->fmt.pix.
  2606. height,
  2607. &zoran_formats
  2608. [i])))
  2609. goto sfmtv4l_unlock_and_return;
  2610. /* tell the user the
  2611. * results/missing stuff */
  2612. fmt->fmt.pix.sizeimage = fh->v4l_buffers.buffer_size /*zr->gbpl * zr->gheight */
  2613. ;
  2614. if (BUZ_MAX_HEIGHT <
  2615. (fh->v4l_settings.height * 2))
  2616. fmt->fmt.pix.field =
  2617. V4L2_FIELD_INTERLACED;
  2618. else
  2619. fmt->fmt.pix.field =
  2620. V4L2_FIELD_TOP;
  2621. fh->map_mode = ZORAN_MAP_MODE_RAW;
  2622. sfmtv4l_unlock_and_return:
  2623. up(&zr->resource_lock);
  2624. }
  2625. break;
  2626. default:
  2627. dprintk(3, "unsupported\n");
  2628. dprintk(1,
  2629. KERN_ERR
  2630. "%s: VIDIOC_S_FMT - unsupported type %d\n",
  2631. ZR_DEVNAME(zr), fmt->type);
  2632. return -EINVAL;
  2633. }
  2634. return res;
  2635. }
  2636. break;
  2637. case VIDIOC_G_FBUF:
  2638. {
  2639. struct v4l2_framebuffer *fb = arg;
  2640. dprintk(3, KERN_DEBUG "%s: VIDIOC_G_FBUF\n", ZR_DEVNAME(zr));
  2641. memset(fb, 0, sizeof(*fb));
  2642. down(&zr->resource_lock);
  2643. fb->base = zr->buffer.base;
  2644. fb->fmt.width = zr->buffer.width;
  2645. fb->fmt.height = zr->buffer.height;
  2646. if (zr->overlay_settings.format) {
  2647. fb->fmt.pixelformat =
  2648. fh->overlay_settings.format->fourcc;
  2649. }
  2650. fb->fmt.bytesperline = zr->buffer.bytesperline;
  2651. up(&zr->resource_lock);
  2652. fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
  2653. fb->fmt.field = V4L2_FIELD_INTERLACED;
  2654. fb->flags = V4L2_FBUF_FLAG_OVERLAY;
  2655. fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
  2656. return 0;
  2657. }
  2658. break;
  2659. case VIDIOC_S_FBUF:
  2660. {
  2661. int i, res = 0;
  2662. struct v4l2_framebuffer *fb = arg;
  2663. __u32 printformat = __cpu_to_le32(fb->fmt.pixelformat);
  2664. dprintk(3,
  2665. KERN_DEBUG
  2666. "%s: VIDIOC_S_FBUF - base=0x%p, size=%dx%d, bpl=%d, fmt=0x%x (%4.4s)\n",
  2667. ZR_DEVNAME(zr), fb->base, fb->fmt.width, fb->fmt.height,
  2668. fb->fmt.bytesperline, fb->fmt.pixelformat,
  2669. (char *) &printformat);
  2670. for (i = 0; i < zoran_num_formats; i++)
  2671. if (zoran_formats[i].fourcc == fb->fmt.pixelformat)
  2672. break;
  2673. if (i == zoran_num_formats) {
  2674. dprintk(1,
  2675. KERN_ERR
  2676. "%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
  2677. ZR_DEVNAME(zr), fb->fmt.pixelformat,
  2678. (char *) &printformat);
  2679. return -EINVAL;
  2680. }
  2681. down(&zr->resource_lock);
  2682. res =
  2683. setup_fbuffer(file, fb->base, &zoran_formats[i],
  2684. fb->fmt.width, fb->fmt.height,
  2685. fb->fmt.bytesperline);
  2686. up(&zr->resource_lock);
  2687. return res;
  2688. }
  2689. break;
  2690. case VIDIOC_OVERLAY:
  2691. {
  2692. int *on = arg, res;
  2693. dprintk(3, KERN_DEBUG "%s: VIDIOC_PREVIEW - on=%d\n",
  2694. ZR_DEVNAME(zr), *on);
  2695. down(&zr->resource_lock);
  2696. res = setup_overlay(file, *on);
  2697. up(&zr->resource_lock);
  2698. return res;
  2699. }
  2700. break;
  2701. case VIDIOC_REQBUFS:
  2702. {
  2703. struct v4l2_requestbuffers *req = arg;
  2704. int res = 0;
  2705. dprintk(3, KERN_DEBUG "%s: VIDIOC_REQBUFS - type=%d\n",
  2706. ZR_DEVNAME(zr), req->type);
  2707. if (req->memory != V4L2_MEMORY_MMAP) {
  2708. dprintk(1,
  2709. KERN_ERR
  2710. "%s: only MEMORY_MMAP capture is supported, not %d\n",
  2711. ZR_DEVNAME(zr), req->memory);
  2712. return -EINVAL;
  2713. }
  2714. down(&zr->resource_lock);
  2715. if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
  2716. dprintk(1,
  2717. KERN_ERR
  2718. "%s: VIDIOC_REQBUFS - buffers allready allocated\n",
  2719. ZR_DEVNAME(zr));
  2720. res = -EBUSY;
  2721. goto v4l2reqbuf_unlock_and_return;
  2722. }
  2723. if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
  2724. req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  2725. /* control user input */
  2726. if (req->count < 2)
  2727. req->count = 2;
  2728. if (req->count > v4l_nbufs)
  2729. req->count = v4l_nbufs;
  2730. fh->v4l_buffers.num_buffers = req->count;
  2731. if (v4l_fbuffer_alloc(file)) {
  2732. res = -ENOMEM;
  2733. goto v4l2reqbuf_unlock_and_return;
  2734. }
  2735. /* The next mmap will map the V4L buffers */
  2736. fh->map_mode = ZORAN_MAP_MODE_RAW;
  2737. } else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
  2738. fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
  2739. /* we need to calculate size ourselves now */
  2740. if (req->count < 4)
  2741. req->count = 4;
  2742. if (req->count > jpg_nbufs)
  2743. req->count = jpg_nbufs;
  2744. fh->jpg_buffers.num_buffers = req->count;
  2745. fh->jpg_buffers.buffer_size =
  2746. zoran_v4l2_calc_bufsize(&fh->jpg_settings);
  2747. if (jpg_fbuffer_alloc(file)) {
  2748. res = -ENOMEM;
  2749. goto v4l2reqbuf_unlock_and_return;
  2750. }
  2751. /* The next mmap will map the MJPEG buffers */
  2752. if (req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  2753. fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
  2754. else
  2755. fh->map_mode = ZORAN_MAP_MODE_JPG_PLAY;
  2756. } else {
  2757. dprintk(1,
  2758. KERN_ERR
  2759. "%s: VIDIOC_REQBUFS - unknown type %d\n",
  2760. ZR_DEVNAME(zr), req->type);
  2761. res = -EINVAL;
  2762. goto v4l2reqbuf_unlock_and_return;
  2763. }
  2764. v4l2reqbuf_unlock_and_return:
  2765. up(&zr->resource_lock);
  2766. return 0;
  2767. }
  2768. break;
  2769. case VIDIOC_QUERYBUF:
  2770. {
  2771. struct v4l2_buffer *buf = arg;
  2772. __u32 type = buf->type;
  2773. int index = buf->index, res;
  2774. dprintk(3,
  2775. KERN_DEBUG
  2776. "%s: VIDIOC_QUERYBUF - index=%d, type=%d\n",
  2777. ZR_DEVNAME(zr), buf->index, buf->type);
  2778. memset(buf, 0, sizeof(buf));
  2779. buf->type = type;
  2780. buf->index = index;
  2781. down(&zr->resource_lock);
  2782. res = zoran_v4l2_buffer_status(file, buf, buf->index);
  2783. up(&zr->resource_lock);
  2784. return res;
  2785. }
  2786. break;
  2787. case VIDIOC_QBUF:
  2788. {
  2789. struct v4l2_buffer *buf = arg;
  2790. int res = 0, codec_mode, buf_type;
  2791. dprintk(3,
  2792. KERN_DEBUG "%s: VIDIOC_QBUF - type=%d, index=%d\n",
  2793. ZR_DEVNAME(zr), buf->type, buf->index);
  2794. down(&zr->resource_lock);
  2795. switch (fh->map_mode) {
  2796. case ZORAN_MAP_MODE_RAW:
  2797. if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  2798. dprintk(1,
  2799. KERN_ERR
  2800. "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
  2801. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  2802. res = -EINVAL;
  2803. goto qbuf_unlock_and_return;
  2804. }
  2805. res = zoran_v4l_queue_frame(file, buf->index);
  2806. if (res)
  2807. goto qbuf_unlock_and_return;
  2808. if (!zr->v4l_memgrab_active &&
  2809. fh->v4l_buffers.active == ZORAN_LOCKED)
  2810. zr36057_set_memgrab(zr, 1);
  2811. break;
  2812. case ZORAN_MAP_MODE_JPG_REC:
  2813. case ZORAN_MAP_MODE_JPG_PLAY:
  2814. if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
  2815. buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  2816. codec_mode = BUZ_MODE_MOTION_DECOMPRESS;
  2817. } else {
  2818. buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  2819. codec_mode = BUZ_MODE_MOTION_COMPRESS;
  2820. }
  2821. if (buf->type != buf_type) {
  2822. dprintk(1,
  2823. KERN_ERR
  2824. "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
  2825. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  2826. res = -EINVAL;
  2827. goto qbuf_unlock_and_return;
  2828. }
  2829. res =
  2830. zoran_jpg_queue_frame(file, buf->index,
  2831. codec_mode);
  2832. if (res != 0)
  2833. goto qbuf_unlock_and_return;
  2834. if (zr->codec_mode == BUZ_MODE_IDLE &&
  2835. fh->jpg_buffers.active == ZORAN_LOCKED) {
  2836. zr36057_enable_jpg(zr, codec_mode);
  2837. }
  2838. break;
  2839. default:
  2840. dprintk(1,
  2841. KERN_ERR
  2842. "%s: VIDIOC_QBUF - unsupported type %d\n",
  2843. ZR_DEVNAME(zr), buf->type);
  2844. res = -EINVAL;
  2845. goto qbuf_unlock_and_return;
  2846. }
  2847. qbuf_unlock_and_return:
  2848. up(&zr->resource_lock);
  2849. return res;
  2850. }
  2851. break;
  2852. case VIDIOC_DQBUF:
  2853. {
  2854. struct v4l2_buffer *buf = arg;
  2855. int res = 0, buf_type, num = -1; /* compiler borks here (?) */
  2856. dprintk(3, KERN_DEBUG "%s: VIDIOC_DQBUF - type=%d\n",
  2857. ZR_DEVNAME(zr), buf->type);
  2858. down(&zr->resource_lock);
  2859. switch (fh->map_mode) {
  2860. case ZORAN_MAP_MODE_RAW:
  2861. if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  2862. dprintk(1,
  2863. KERN_ERR
  2864. "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
  2865. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  2866. res = -EINVAL;
  2867. goto dqbuf_unlock_and_return;
  2868. }
  2869. num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
  2870. if (file->f_flags & O_NONBLOCK &&
  2871. zr->v4l_buffers.buffer[num].state !=
  2872. BUZ_STATE_DONE) {
  2873. res = -EAGAIN;
  2874. goto dqbuf_unlock_and_return;
  2875. }
  2876. res = v4l_sync(file, num);
  2877. if (res)
  2878. goto dqbuf_unlock_and_return;
  2879. else
  2880. zr->v4l_sync_tail++;
  2881. res = zoran_v4l2_buffer_status(file, buf, num);
  2882. break;
  2883. case ZORAN_MAP_MODE_JPG_REC:
  2884. case ZORAN_MAP_MODE_JPG_PLAY:
  2885. {
  2886. struct zoran_sync bs;
  2887. if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
  2888. buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  2889. else
  2890. buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  2891. if (buf->type != buf_type) {
  2892. dprintk(1,
  2893. KERN_ERR
  2894. "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
  2895. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  2896. res = -EINVAL;
  2897. goto dqbuf_unlock_and_return;
  2898. }
  2899. num =
  2900. zr->jpg_pend[zr->
  2901. jpg_que_tail & BUZ_MASK_FRAME];
  2902. if (file->f_flags & O_NONBLOCK &&
  2903. zr->jpg_buffers.buffer[num].state !=
  2904. BUZ_STATE_DONE) {
  2905. res = -EAGAIN;
  2906. goto dqbuf_unlock_and_return;
  2907. }
  2908. res = jpg_sync(file, &bs);
  2909. if (res)
  2910. goto dqbuf_unlock_and_return;
  2911. res =
  2912. zoran_v4l2_buffer_status(file, buf, bs.frame);
  2913. break;
  2914. }
  2915. default:
  2916. dprintk(1,
  2917. KERN_ERR
  2918. "%s: VIDIOC_DQBUF - unsupported type %d\n",
  2919. ZR_DEVNAME(zr), buf->type);
  2920. res = -EINVAL;
  2921. goto dqbuf_unlock_and_return;
  2922. }
  2923. dqbuf_unlock_and_return:
  2924. up(&zr->resource_lock);
  2925. return res;
  2926. }
  2927. break;
  2928. case VIDIOC_STREAMON:
  2929. {
  2930. int res = 0;
  2931. dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMON\n", ZR_DEVNAME(zr));
  2932. down(&zr->resource_lock);
  2933. switch (fh->map_mode) {
  2934. case ZORAN_MAP_MODE_RAW: /* raw capture */
  2935. if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
  2936. fh->v4l_buffers.active != ZORAN_ACTIVE) {
  2937. res = -EBUSY;
  2938. goto strmon_unlock_and_return;
  2939. }
  2940. zr->v4l_buffers.active = fh->v4l_buffers.active =
  2941. ZORAN_LOCKED;
  2942. zr->v4l_settings = fh->v4l_settings;
  2943. zr->v4l_sync_tail = zr->v4l_pend_tail;
  2944. if (!zr->v4l_memgrab_active &&
  2945. zr->v4l_pend_head != zr->v4l_pend_tail) {
  2946. zr36057_set_memgrab(zr, 1);
  2947. }
  2948. break;
  2949. case ZORAN_MAP_MODE_JPG_REC:
  2950. case ZORAN_MAP_MODE_JPG_PLAY:
  2951. /* what is the codec mode right now? */
  2952. if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
  2953. fh->jpg_buffers.active != ZORAN_ACTIVE) {
  2954. res = -EBUSY;
  2955. goto strmon_unlock_and_return;
  2956. }
  2957. zr->jpg_buffers.active = fh->jpg_buffers.active =
  2958. ZORAN_LOCKED;
  2959. if (zr->jpg_que_head != zr->jpg_que_tail) {
  2960. /* Start the jpeg codec when the first frame is queued */
  2961. jpeg_start(zr);
  2962. }
  2963. break;
  2964. default:
  2965. dprintk(1,
  2966. KERN_ERR
  2967. "%s: VIDIOC_STREAMON - invalid map mode %d\n",
  2968. ZR_DEVNAME(zr), fh->map_mode);
  2969. res = -EINVAL;
  2970. goto strmon_unlock_and_return;
  2971. }
  2972. strmon_unlock_and_return:
  2973. up(&zr->resource_lock);
  2974. return res;
  2975. }
  2976. break;
  2977. case VIDIOC_STREAMOFF:
  2978. {
  2979. int i, res = 0;
  2980. dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMOFF\n", ZR_DEVNAME(zr));
  2981. down(&zr->resource_lock);
  2982. switch (fh->map_mode) {
  2983. case ZORAN_MAP_MODE_RAW: /* raw capture */
  2984. if (fh->v4l_buffers.active == ZORAN_FREE &&
  2985. zr->v4l_buffers.active != ZORAN_FREE) {
  2986. res = -EPERM; /* stay off other's settings! */
  2987. goto strmoff_unlock_and_return;
  2988. }
  2989. if (zr->v4l_buffers.active == ZORAN_FREE)
  2990. goto strmoff_unlock_and_return;
  2991. /* unload capture */
  2992. if (zr->v4l_memgrab_active)
  2993. zr36057_set_memgrab(zr, 0);
  2994. for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
  2995. zr->v4l_buffers.buffer[i].state =
  2996. BUZ_STATE_USER;
  2997. fh->v4l_buffers = zr->v4l_buffers;
  2998. zr->v4l_buffers.active = fh->v4l_buffers.active =
  2999. ZORAN_FREE;
  3000. zr->v4l_grab_seq = 0;
  3001. zr->v4l_pend_head = zr->v4l_pend_tail = 0;
  3002. zr->v4l_sync_tail = 0;
  3003. break;
  3004. case ZORAN_MAP_MODE_JPG_REC:
  3005. case ZORAN_MAP_MODE_JPG_PLAY:
  3006. if (fh->jpg_buffers.active == ZORAN_FREE &&
  3007. zr->jpg_buffers.active != ZORAN_FREE) {
  3008. res = -EPERM; /* stay off other's settings! */
  3009. goto strmoff_unlock_and_return;
  3010. }
  3011. if (zr->jpg_buffers.active == ZORAN_FREE)
  3012. goto strmoff_unlock_and_return;
  3013. res =
  3014. jpg_qbuf(file, -1,
  3015. (fh->map_mode ==
  3016. ZORAN_MAP_MODE_JPG_REC) ?
  3017. BUZ_MODE_MOTION_COMPRESS :
  3018. BUZ_MODE_MOTION_DECOMPRESS);
  3019. if (res)
  3020. goto strmoff_unlock_and_return;
  3021. break;
  3022. default:
  3023. dprintk(1,
  3024. KERN_ERR
  3025. "%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
  3026. ZR_DEVNAME(zr), fh->map_mode);
  3027. res = -EINVAL;
  3028. goto strmoff_unlock_and_return;
  3029. }
  3030. strmoff_unlock_and_return:
  3031. up(&zr->resource_lock);
  3032. return res;
  3033. }
  3034. break;
  3035. case VIDIOC_QUERYCTRL:
  3036. {
  3037. struct v4l2_queryctrl *ctrl = arg;
  3038. dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCTRL - id=%d\n",
  3039. ZR_DEVNAME(zr), ctrl->id);
  3040. /* we only support hue/saturation/contrast/brightness */
  3041. if (ctrl->id < V4L2_CID_BRIGHTNESS ||
  3042. ctrl->id > V4L2_CID_HUE)
  3043. return -EINVAL;
  3044. else {
  3045. int id = ctrl->id;
  3046. memset(ctrl, 0, sizeof(*ctrl));
  3047. ctrl->id = id;
  3048. }
  3049. switch (ctrl->id) {
  3050. case V4L2_CID_BRIGHTNESS:
  3051. strncpy(ctrl->name, "Brightness", 31);
  3052. break;
  3053. case V4L2_CID_CONTRAST:
  3054. strncpy(ctrl->name, "Contrast", 31);
  3055. break;
  3056. case V4L2_CID_SATURATION:
  3057. strncpy(ctrl->name, "Saturation", 31);
  3058. break;
  3059. case V4L2_CID_HUE:
  3060. strncpy(ctrl->name, "Hue", 31);
  3061. break;
  3062. }
  3063. ctrl->minimum = 0;
  3064. ctrl->maximum = 65535;
  3065. ctrl->step = 1;
  3066. ctrl->default_value = 32768;
  3067. ctrl->type = V4L2_CTRL_TYPE_INTEGER;
  3068. return 0;
  3069. }
  3070. break;
  3071. case VIDIOC_G_CTRL:
  3072. {
  3073. struct v4l2_control *ctrl = arg;
  3074. dprintk(3, KERN_DEBUG "%s: VIDIOC_G_CTRL - id=%d\n",
  3075. ZR_DEVNAME(zr), ctrl->id);
  3076. /* we only support hue/saturation/contrast/brightness */
  3077. if (ctrl->id < V4L2_CID_BRIGHTNESS ||
  3078. ctrl->id > V4L2_CID_HUE)
  3079. return -EINVAL;
  3080. down(&zr->resource_lock);
  3081. switch (ctrl->id) {
  3082. case V4L2_CID_BRIGHTNESS:
  3083. ctrl->value = zr->brightness;
  3084. break;
  3085. case V4L2_CID_CONTRAST:
  3086. ctrl->value = zr->contrast;
  3087. break;
  3088. case V4L2_CID_SATURATION:
  3089. ctrl->value = zr->saturation;
  3090. break;
  3091. case V4L2_CID_HUE:
  3092. ctrl->value = zr->hue;
  3093. break;
  3094. }
  3095. up(&zr->resource_lock);
  3096. return 0;
  3097. }
  3098. break;
  3099. case VIDIOC_S_CTRL:
  3100. {
  3101. struct v4l2_control *ctrl = arg;
  3102. struct video_picture pict;
  3103. dprintk(3, KERN_DEBUG "%s: VIDIOC_S_CTRL - id=%d\n",
  3104. ZR_DEVNAME(zr), ctrl->id);
  3105. /* we only support hue/saturation/contrast/brightness */
  3106. if (ctrl->id < V4L2_CID_BRIGHTNESS ||
  3107. ctrl->id > V4L2_CID_HUE)
  3108. return -EINVAL;
  3109. if (ctrl->value < 0 || ctrl->value > 65535) {
  3110. dprintk(1,
  3111. KERN_ERR
  3112. "%s: VIDIOC_S_CTRL - invalid value %d for id=%d\n",
  3113. ZR_DEVNAME(zr), ctrl->value, ctrl->id);
  3114. return -EINVAL;
  3115. }
  3116. down(&zr->resource_lock);
  3117. switch (ctrl->id) {
  3118. case V4L2_CID_BRIGHTNESS:
  3119. zr->brightness = ctrl->value;
  3120. break;
  3121. case V4L2_CID_CONTRAST:
  3122. zr->contrast = ctrl->value;
  3123. break;
  3124. case V4L2_CID_SATURATION:
  3125. zr->saturation = ctrl->value;
  3126. break;
  3127. case V4L2_CID_HUE:
  3128. zr->hue = ctrl->value;
  3129. break;
  3130. }
  3131. pict.brightness = zr->brightness;
  3132. pict.contrast = zr->contrast;
  3133. pict.colour = zr->saturation;
  3134. pict.hue = zr->hue;
  3135. decoder_command(zr, DECODER_SET_PICTURE, &pict);
  3136. up(&zr->resource_lock);
  3137. return 0;
  3138. }
  3139. break;
  3140. case VIDIOC_ENUMSTD:
  3141. {
  3142. struct v4l2_standard *std = arg;
  3143. dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMSTD - index=%d\n",
  3144. ZR_DEVNAME(zr), std->index);
  3145. if (std->index < 0 || std->index >= (zr->card.norms + 1))
  3146. return -EINVAL;
  3147. else {
  3148. int id = std->index;
  3149. memset(std, 0, sizeof(*std));
  3150. std->index = id;
  3151. }
  3152. if (std->index == zr->card.norms) {
  3153. /* if we have autodetect, ... */
  3154. struct video_decoder_capability caps;
  3155. decoder_command(zr, DECODER_GET_CAPABILITIES,
  3156. &caps);
  3157. if (caps.flags & VIDEO_DECODER_AUTO) {
  3158. std->id = V4L2_STD_ALL;
  3159. strncpy(std->name, "Autodetect", 31);
  3160. return 0;
  3161. } else
  3162. return -EINVAL;
  3163. }
  3164. switch (std->index) {
  3165. case 0:
  3166. std->id = V4L2_STD_PAL;
  3167. strncpy(std->name, "PAL", 31);
  3168. std->frameperiod.numerator = 1;
  3169. std->frameperiod.denominator = 25;
  3170. std->framelines = zr->card.tvn[0]->Ht;
  3171. break;
  3172. case 1:
  3173. std->id = V4L2_STD_NTSC;
  3174. strncpy(std->name, "NTSC", 31);
  3175. std->frameperiod.numerator = 1001;
  3176. std->frameperiod.denominator = 30000;
  3177. std->framelines = zr->card.tvn[1]->Ht;
  3178. break;
  3179. case 2:
  3180. std->id = V4L2_STD_SECAM;
  3181. strncpy(std->name, "SECAM", 31);
  3182. std->frameperiod.numerator = 1;
  3183. std->frameperiod.denominator = 25;
  3184. std->framelines = zr->card.tvn[2]->Ht;
  3185. break;
  3186. }
  3187. return 0;
  3188. }
  3189. break;
  3190. case VIDIOC_G_STD:
  3191. {
  3192. v4l2_std_id *std = arg;
  3193. int norm;
  3194. dprintk(3, KERN_DEBUG "%s: VIDIOC_G_STD\n", ZR_DEVNAME(zr));
  3195. down(&zr->resource_lock);
  3196. norm = zr->norm;
  3197. up(&zr->resource_lock);
  3198. switch (norm) {
  3199. case VIDEO_MODE_PAL:
  3200. *std = V4L2_STD_PAL;
  3201. break;
  3202. case VIDEO_MODE_NTSC:
  3203. *std = V4L2_STD_NTSC;
  3204. break;
  3205. case VIDEO_MODE_SECAM:
  3206. *std = V4L2_STD_SECAM;
  3207. break;
  3208. }
  3209. return 0;
  3210. }
  3211. break;
  3212. case VIDIOC_S_STD:
  3213. {
  3214. int norm = -1, res = 0;
  3215. v4l2_std_id *std = arg;
  3216. dprintk(3, KERN_DEBUG "%s: VIDIOC_S_STD - norm=0x%llx\n",
  3217. ZR_DEVNAME(zr), (unsigned long long)*std);
  3218. if (*std == V4L2_STD_PAL)
  3219. norm = VIDEO_MODE_PAL;
  3220. else if (*std == V4L2_STD_NTSC)
  3221. norm = VIDEO_MODE_NTSC;
  3222. else if (*std == V4L2_STD_SECAM)
  3223. norm = VIDEO_MODE_SECAM;
  3224. else if (*std == V4L2_STD_ALL)
  3225. norm = VIDEO_MODE_AUTO;
  3226. else {
  3227. dprintk(1,
  3228. KERN_ERR
  3229. "%s: VIDIOC_S_STD - invalid norm 0x%llx\n",
  3230. ZR_DEVNAME(zr), (unsigned long long)*std);
  3231. return -EINVAL;
  3232. }
  3233. down(&zr->resource_lock);
  3234. if ((res = zoran_set_norm(zr, norm)))
  3235. goto sstd_unlock_and_return;
  3236. res = wait_grab_pending(zr);
  3237. sstd_unlock_and_return:
  3238. up(&zr->resource_lock);
  3239. return res;
  3240. }
  3241. break;
  3242. case VIDIOC_ENUMINPUT:
  3243. {
  3244. struct v4l2_input *inp = arg;
  3245. int status;
  3246. dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMINPUT - index=%d\n",
  3247. ZR_DEVNAME(zr), inp->index);
  3248. if (inp->index < 0 || inp->index >= zr->card.inputs)
  3249. return -EINVAL;
  3250. else {
  3251. int id = inp->index;
  3252. memset(inp, 0, sizeof(*inp));
  3253. inp->index = id;
  3254. }
  3255. strncpy(inp->name, zr->card.input[inp->index].name,
  3256. sizeof(inp->name) - 1);
  3257. inp->type = V4L2_INPUT_TYPE_CAMERA;
  3258. inp->std = V4L2_STD_ALL;
  3259. /* Get status of video decoder */
  3260. down(&zr->resource_lock);
  3261. decoder_command(zr, DECODER_GET_STATUS, &status);
  3262. up(&zr->resource_lock);
  3263. if (!(status & DECODER_STATUS_GOOD)) {
  3264. inp->status |= V4L2_IN_ST_NO_POWER;
  3265. inp->status |= V4L2_IN_ST_NO_SIGNAL;
  3266. }
  3267. if (!(status & DECODER_STATUS_COLOR))
  3268. inp->status |= V4L2_IN_ST_NO_COLOR;
  3269. return 0;
  3270. }
  3271. break;
  3272. case VIDIOC_G_INPUT:
  3273. {
  3274. int *input = arg;
  3275. dprintk(3, KERN_DEBUG "%s: VIDIOC_G_INPUT\n", ZR_DEVNAME(zr));
  3276. down(&zr->resource_lock);
  3277. *input = zr->input;
  3278. up(&zr->resource_lock);
  3279. return 0;
  3280. }
  3281. break;
  3282. case VIDIOC_S_INPUT:
  3283. {
  3284. int *input = arg, res = 0;
  3285. dprintk(3, KERN_DEBUG "%s: VIDIOC_S_INPUT - input=%d\n",
  3286. ZR_DEVNAME(zr), *input);
  3287. down(&zr->resource_lock);
  3288. if ((res = zoran_set_input(zr, *input)))
  3289. goto sinput_unlock_and_return;
  3290. /* Make sure the changes come into effect */
  3291. res = wait_grab_pending(zr);
  3292. sinput_unlock_and_return:
  3293. up(&zr->resource_lock);
  3294. return res;
  3295. }
  3296. break;
  3297. case VIDIOC_ENUMOUTPUT:
  3298. {
  3299. struct v4l2_output *outp = arg;
  3300. dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMOUTPUT - index=%d\n",
  3301. ZR_DEVNAME(zr), outp->index);
  3302. if (outp->index != 0)
  3303. return -EINVAL;
  3304. memset(outp, 0, sizeof(*outp));
  3305. outp->index = 0;
  3306. outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
  3307. strncpy(outp->name, "Autodetect", 31);
  3308. return 0;
  3309. }
  3310. break;
  3311. case VIDIOC_G_OUTPUT:
  3312. {
  3313. int *output = arg;
  3314. dprintk(3, KERN_DEBUG "%s: VIDIOC_G_OUTPUT\n", ZR_DEVNAME(zr));
  3315. *output = 0;
  3316. return 0;
  3317. }
  3318. break;
  3319. case VIDIOC_S_OUTPUT:
  3320. {
  3321. int *output = arg;
  3322. dprintk(3, KERN_DEBUG "%s: VIDIOC_S_OUTPUT - output=%d\n",
  3323. ZR_DEVNAME(zr), *output);
  3324. if (*output != 0)
  3325. return -EINVAL;
  3326. return 0;
  3327. }
  3328. break;
  3329. /* cropping (sub-frame capture) */
  3330. case VIDIOC_CROPCAP:
  3331. {
  3332. struct v4l2_cropcap *cropcap = arg;
  3333. int type = cropcap->type, res = 0;
  3334. dprintk(3, KERN_ERR "%s: VIDIOC_CROPCAP - type=%d\n",
  3335. ZR_DEVNAME(zr), cropcap->type);
  3336. memset(cropcap, 0, sizeof(*cropcap));
  3337. cropcap->type = type;
  3338. down(&zr->resource_lock);
  3339. if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  3340. (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  3341. fh->map_mode == ZORAN_MAP_MODE_RAW)) {
  3342. dprintk(1,
  3343. KERN_ERR
  3344. "%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
  3345. ZR_DEVNAME(zr));
  3346. res = -EINVAL;
  3347. goto cropcap_unlock_and_return;
  3348. }
  3349. cropcap->bounds.top = cropcap->bounds.left = 0;
  3350. cropcap->bounds.width = BUZ_MAX_WIDTH;
  3351. cropcap->bounds.height = BUZ_MAX_HEIGHT;
  3352. cropcap->defrect.top = cropcap->defrect.left = 0;
  3353. cropcap->defrect.width = BUZ_MIN_WIDTH;
  3354. cropcap->defrect.height = BUZ_MIN_HEIGHT;
  3355. cropcap_unlock_and_return:
  3356. up(&zr->resource_lock);
  3357. return res;
  3358. }
  3359. break;
  3360. case VIDIOC_G_CROP:
  3361. {
  3362. struct v4l2_crop *crop = arg;
  3363. int type = crop->type, res = 0;
  3364. dprintk(3, KERN_ERR "%s: VIDIOC_G_CROP - type=%d\n",
  3365. ZR_DEVNAME(zr), crop->type);
  3366. memset(crop, 0, sizeof(*crop));
  3367. crop->type = type;
  3368. down(&zr->resource_lock);
  3369. if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  3370. (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  3371. fh->map_mode == ZORAN_MAP_MODE_RAW)) {
  3372. dprintk(1,
  3373. KERN_ERR
  3374. "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
  3375. ZR_DEVNAME(zr));
  3376. res = -EINVAL;
  3377. goto gcrop_unlock_and_return;
  3378. }
  3379. crop->c.top = fh->jpg_settings.img_y;
  3380. crop->c.left = fh->jpg_settings.img_x;
  3381. crop->c.width = fh->jpg_settings.img_width;
  3382. crop->c.height = fh->jpg_settings.img_height;
  3383. gcrop_unlock_and_return:
  3384. up(&zr->resource_lock);
  3385. return res;
  3386. }
  3387. break;
  3388. case VIDIOC_S_CROP:
  3389. {
  3390. struct v4l2_crop *crop = arg;
  3391. int res = 0;
  3392. settings = fh->jpg_settings;
  3393. dprintk(3,
  3394. KERN_ERR
  3395. "%s: VIDIOC_S_CROP - type=%d, x=%d,y=%d,w=%d,h=%d\n",
  3396. ZR_DEVNAME(zr), crop->type, crop->c.left, crop->c.top,
  3397. crop->c.width, crop->c.height);
  3398. down(&zr->resource_lock);
  3399. if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
  3400. dprintk(1,
  3401. KERN_ERR
  3402. "%s: VIDIOC_S_CROP - cannot change settings while active\n",
  3403. ZR_DEVNAME(zr));
  3404. res = -EBUSY;
  3405. goto scrop_unlock_and_return;
  3406. }
  3407. if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  3408. (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  3409. fh->map_mode == ZORAN_MAP_MODE_RAW)) {
  3410. dprintk(1,
  3411. KERN_ERR
  3412. "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
  3413. ZR_DEVNAME(zr));
  3414. res = -EINVAL;
  3415. goto scrop_unlock_and_return;
  3416. }
  3417. /* move into a form that we understand */
  3418. settings.img_x = crop->c.left;
  3419. settings.img_y = crop->c.top;
  3420. settings.img_width = crop->c.width;
  3421. settings.img_height = crop->c.height;
  3422. /* check validity */
  3423. if ((res = zoran_check_jpg_settings(zr, &settings)))
  3424. goto scrop_unlock_and_return;
  3425. /* accept */
  3426. fh->jpg_settings = settings;
  3427. scrop_unlock_and_return:
  3428. up(&zr->resource_lock);
  3429. return res;
  3430. }
  3431. break;
  3432. case VIDIOC_G_JPEGCOMP:
  3433. {
  3434. struct v4l2_jpegcompression *params = arg;
  3435. dprintk(3, KERN_DEBUG "%s: VIDIOC_G_JPEGCOMP\n",
  3436. ZR_DEVNAME(zr));
  3437. memset(params, 0, sizeof(*params));
  3438. down(&zr->resource_lock);
  3439. params->quality = fh->jpg_settings.jpg_comp.quality;
  3440. params->APPn = fh->jpg_settings.jpg_comp.APPn;
  3441. memcpy(params->APP_data,
  3442. fh->jpg_settings.jpg_comp.APP_data,
  3443. fh->jpg_settings.jpg_comp.APP_len);
  3444. params->APP_len = fh->jpg_settings.jpg_comp.APP_len;
  3445. memcpy(params->COM_data,
  3446. fh->jpg_settings.jpg_comp.COM_data,
  3447. fh->jpg_settings.jpg_comp.COM_len);
  3448. params->COM_len = fh->jpg_settings.jpg_comp.COM_len;
  3449. params->jpeg_markers =
  3450. fh->jpg_settings.jpg_comp.jpeg_markers;
  3451. up(&zr->resource_lock);
  3452. return 0;
  3453. }
  3454. break;
  3455. case VIDIOC_S_JPEGCOMP:
  3456. {
  3457. struct v4l2_jpegcompression *params = arg;
  3458. int res = 0;
  3459. settings = fh->jpg_settings;
  3460. dprintk(3,
  3461. KERN_DEBUG
  3462. "%s: VIDIOC_S_JPEGCOMP - quality=%d, APPN=%d, APP_len=%d, COM_len=%d\n",
  3463. ZR_DEVNAME(zr), params->quality, params->APPn,
  3464. params->APP_len, params->COM_len);
  3465. settings.jpg_comp = *params;
  3466. down(&zr->resource_lock);
  3467. if (fh->v4l_buffers.active != ZORAN_FREE ||
  3468. fh->jpg_buffers.active != ZORAN_FREE) {
  3469. dprintk(1,
  3470. KERN_WARNING
  3471. "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
  3472. ZR_DEVNAME(zr));
  3473. res = -EBUSY;
  3474. goto sjpegc_unlock_and_return;
  3475. }
  3476. if ((res = zoran_check_jpg_settings(zr, &settings)))
  3477. goto sjpegc_unlock_and_return;
  3478. if (!fh->jpg_buffers.allocated)
  3479. fh->jpg_buffers.buffer_size =
  3480. zoran_v4l2_calc_bufsize(&fh->jpg_settings);
  3481. fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
  3482. sjpegc_unlock_and_return:
  3483. up(&zr->resource_lock);
  3484. return 0;
  3485. }
  3486. break;
  3487. case VIDIOC_QUERYSTD: /* why is this useful? */
  3488. {
  3489. v4l2_std_id *std = arg;
  3490. dprintk(3,
  3491. KERN_DEBUG "%s: VIDIOC_QUERY_STD - std=0x%llx\n",
  3492. ZR_DEVNAME(zr), (unsigned long long)*std);
  3493. if (*std == V4L2_STD_ALL || *std == V4L2_STD_NTSC ||
  3494. *std == V4L2_STD_PAL || (*std == V4L2_STD_SECAM &&
  3495. zr->card.norms == 3)) {
  3496. return 0;
  3497. }
  3498. return -EINVAL;
  3499. }
  3500. break;
  3501. case VIDIOC_TRY_FMT:
  3502. {
  3503. struct v4l2_format *fmt = arg;
  3504. int res = 0;
  3505. dprintk(3, KERN_DEBUG "%s: VIDIOC_TRY_FMT - type=%d\n",
  3506. ZR_DEVNAME(zr), fmt->type);
  3507. switch (fmt->type) {
  3508. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  3509. down(&zr->resource_lock);
  3510. if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH)
  3511. fmt->fmt.win.w.width = BUZ_MAX_WIDTH;
  3512. if (fmt->fmt.win.w.width < BUZ_MIN_WIDTH)
  3513. fmt->fmt.win.w.width = BUZ_MIN_WIDTH;
  3514. if (fmt->fmt.win.w.height > BUZ_MAX_HEIGHT)
  3515. fmt->fmt.win.w.height = BUZ_MAX_HEIGHT;
  3516. if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT)
  3517. fmt->fmt.win.w.height = BUZ_MIN_HEIGHT;
  3518. up(&zr->resource_lock);
  3519. break;
  3520. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  3521. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  3522. if (fmt->fmt.pix.bytesperline > 0)
  3523. return -EINVAL;
  3524. down(&zr->resource_lock);
  3525. if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
  3526. settings = fh->jpg_settings;
  3527. /* we actually need to set 'real' parameters now */
  3528. if ((fmt->fmt.pix.height * 2) >
  3529. BUZ_MAX_HEIGHT)
  3530. settings.TmpDcm = 1;
  3531. else
  3532. settings.TmpDcm = 2;
  3533. settings.decimation = 0;
  3534. if (fmt->fmt.pix.height <=
  3535. fh->jpg_settings.img_height / 2)
  3536. settings.VerDcm = 2;
  3537. else
  3538. settings.VerDcm = 1;
  3539. if (fmt->fmt.pix.width <=
  3540. fh->jpg_settings.img_width / 4)
  3541. settings.HorDcm = 4;
  3542. else if (fmt->fmt.pix.width <=
  3543. fh->jpg_settings.img_width / 2)
  3544. settings.HorDcm = 2;
  3545. else
  3546. settings.HorDcm = 1;
  3547. if (settings.TmpDcm == 1)
  3548. settings.field_per_buff = 2;
  3549. else
  3550. settings.field_per_buff = 1;
  3551. /* check */
  3552. if ((res =
  3553. zoran_check_jpg_settings(zr,
  3554. &settings)))
  3555. goto tryfmt_unlock_and_return;
  3556. /* tell the user what we actually did */
  3557. fmt->fmt.pix.width =
  3558. settings.img_width / settings.HorDcm;
  3559. fmt->fmt.pix.height =
  3560. settings.img_height * 2 /
  3561. (settings.TmpDcm * settings.VerDcm);
  3562. if (settings.TmpDcm == 1)
  3563. fmt->fmt.pix.field =
  3564. (fh->jpg_settings.
  3565. odd_even ? V4L2_FIELD_SEQ_TB :
  3566. V4L2_FIELD_SEQ_BT);
  3567. else
  3568. fmt->fmt.pix.field =
  3569. (fh->jpg_settings.
  3570. odd_even ? V4L2_FIELD_TOP :
  3571. V4L2_FIELD_BOTTOM);
  3572. fmt->fmt.pix.sizeimage =
  3573. zoran_v4l2_calc_bufsize(&settings);
  3574. } else if (fmt->type ==
  3575. V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  3576. int i;
  3577. for (i = 0; i < zoran_num_formats; i++)
  3578. if (zoran_formats[i].fourcc ==
  3579. fmt->fmt.pix.pixelformat)
  3580. break;
  3581. if (i == zoran_num_formats) {
  3582. res = -EINVAL;
  3583. goto tryfmt_unlock_and_return;
  3584. }
  3585. if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
  3586. fmt->fmt.pix.width = BUZ_MAX_WIDTH;
  3587. if (fmt->fmt.pix.width < BUZ_MIN_WIDTH)
  3588. fmt->fmt.pix.width = BUZ_MIN_WIDTH;
  3589. if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
  3590. fmt->fmt.pix.height =
  3591. BUZ_MAX_HEIGHT;
  3592. if (fmt->fmt.pix.height < BUZ_MIN_HEIGHT)
  3593. fmt->fmt.pix.height =
  3594. BUZ_MIN_HEIGHT;
  3595. } else {
  3596. res = -EINVAL;
  3597. goto tryfmt_unlock_and_return;
  3598. }
  3599. tryfmt_unlock_and_return:
  3600. up(&zr->resource_lock);
  3601. return res;
  3602. break;
  3603. default:
  3604. return -EINVAL;
  3605. }
  3606. return 0;
  3607. }
  3608. break;
  3609. #endif
  3610. default:
  3611. dprintk(1, KERN_DEBUG "%s: UNKNOWN ioctl cmd: 0x%x\n",
  3612. ZR_DEVNAME(zr), cmd);
  3613. return -ENOIOCTLCMD;
  3614. break;
  3615. }
  3616. return 0;
  3617. }
  3618. static int
  3619. zoran_ioctl (struct inode *inode,
  3620. struct file *file,
  3621. unsigned int cmd,
  3622. unsigned long arg)
  3623. {
  3624. return video_usercopy(inode, file, cmd, arg, zoran_do_ioctl);
  3625. }
  3626. static unsigned int
  3627. zoran_poll (struct file *file,
  3628. poll_table *wait)
  3629. {
  3630. struct zoran_fh *fh = file->private_data;
  3631. struct zoran *zr = fh->zr;
  3632. wait_queue_head_t *queue = NULL;
  3633. int res = 0, frame;
  3634. /* we should check whether buffers are ready to be synced on
  3635. * (w/o waits - O_NONBLOCK) here
  3636. * if ready for read (sync), return POLLIN|POLLRDNORM,
  3637. * if ready for write (sync), return POLLOUT|POLLWRNORM,
  3638. * if error, return POLLERR,
  3639. * if no buffers queued or so, return POLLNVAL
  3640. */
  3641. down(&zr->resource_lock);
  3642. switch (fh->map_mode) {
  3643. case ZORAN_MAP_MODE_RAW:
  3644. if (fh->v4l_buffers.active == ZORAN_FREE ||
  3645. zr->v4l_pend_head == zr->v4l_pend_tail) {
  3646. dprintk(1,
  3647. "%s: zoran_poll() - no buffers queued\n",
  3648. ZR_DEVNAME(zr));
  3649. res = POLLNVAL;
  3650. goto poll_unlock_and_return;
  3651. }
  3652. queue = &zr->v4l_capq;
  3653. frame = zr->v4l_pend[zr->v4l_pend_tail & V4L_MASK_FRAME];
  3654. poll_wait(file, queue, wait);
  3655. if (fh->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
  3656. res = POLLIN | POLLRDNORM;
  3657. break;
  3658. case ZORAN_MAP_MODE_JPG_REC:
  3659. case ZORAN_MAP_MODE_JPG_PLAY:
  3660. if (fh->jpg_buffers.active == ZORAN_FREE ||
  3661. zr->jpg_que_head == zr->jpg_que_tail) {
  3662. dprintk(1,
  3663. "%s: zoran_poll() - no buffers queued\n",
  3664. ZR_DEVNAME(zr));
  3665. res = POLLNVAL;
  3666. goto poll_unlock_and_return;
  3667. }
  3668. queue = &zr->jpg_capq;
  3669. frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
  3670. poll_wait(file, queue, wait);
  3671. if (fh->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
  3672. if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
  3673. res = POLLIN | POLLRDNORM;
  3674. else
  3675. res = POLLOUT | POLLWRNORM;
  3676. }
  3677. break;
  3678. default:
  3679. dprintk(1,
  3680. "%s: zoran_poll() - internal error, unknown map_mode=%d\n",
  3681. ZR_DEVNAME(zr), fh->map_mode);
  3682. res = POLLNVAL;
  3683. goto poll_unlock_and_return;
  3684. }
  3685. poll_unlock_and_return:
  3686. up(&zr->resource_lock);
  3687. return res;
  3688. }
  3689. /*
  3690. * This maps the buffers to user space.
  3691. *
  3692. * Depending on the state of fh->map_mode
  3693. * the V4L or the MJPEG buffers are mapped
  3694. * per buffer or all together
  3695. *
  3696. * Note that we need to connect to some
  3697. * unmap signal event to unmap the de-allocate
  3698. * the buffer accordingly (zoran_vm_close())
  3699. */
  3700. static void
  3701. zoran_vm_open (struct vm_area_struct *vma)
  3702. {
  3703. struct zoran_mapping *map = vma->vm_private_data;
  3704. map->count++;
  3705. }
  3706. static void
  3707. zoran_vm_close (struct vm_area_struct *vma)
  3708. {
  3709. struct zoran_mapping *map = vma->vm_private_data;
  3710. struct file *file = map->file;
  3711. struct zoran_fh *fh = file->private_data;
  3712. struct zoran *zr = fh->zr;
  3713. int i;
  3714. map->count--;
  3715. if (map->count == 0) {
  3716. switch (fh->map_mode) {
  3717. case ZORAN_MAP_MODE_JPG_REC:
  3718. case ZORAN_MAP_MODE_JPG_PLAY:
  3719. dprintk(3, KERN_INFO "%s: munmap(MJPEG)\n",
  3720. ZR_DEVNAME(zr));
  3721. for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
  3722. if (fh->jpg_buffers.buffer[i].map == map) {
  3723. fh->jpg_buffers.buffer[i].map =
  3724. NULL;
  3725. }
  3726. }
  3727. kfree(map);
  3728. for (i = 0; i < fh->jpg_buffers.num_buffers; i++)
  3729. if (fh->jpg_buffers.buffer[i].map)
  3730. break;
  3731. if (i == fh->jpg_buffers.num_buffers) {
  3732. down(&zr->resource_lock);
  3733. if (fh->jpg_buffers.active != ZORAN_FREE) {
  3734. jpg_qbuf(file, -1, zr->codec_mode);
  3735. zr->jpg_buffers.allocated = 0;
  3736. zr->jpg_buffers.active =
  3737. fh->jpg_buffers.active =
  3738. ZORAN_FREE;
  3739. }
  3740. //jpg_fbuffer_free(file);
  3741. fh->jpg_buffers.allocated = 0;
  3742. fh->jpg_buffers.ready_to_be_freed = 1;
  3743. up(&zr->resource_lock);
  3744. }
  3745. break;
  3746. case ZORAN_MAP_MODE_RAW:
  3747. dprintk(3, KERN_INFO "%s: munmap(V4L)\n",
  3748. ZR_DEVNAME(zr));
  3749. for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
  3750. if (fh->v4l_buffers.buffer[i].map == map) {
  3751. /* unqueue/unmap */
  3752. fh->v4l_buffers.buffer[i].map =
  3753. NULL;
  3754. }
  3755. }
  3756. kfree(map);
  3757. for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
  3758. if (fh->v4l_buffers.buffer[i].map)
  3759. break;
  3760. if (i == fh->v4l_buffers.num_buffers) {
  3761. down(&zr->resource_lock);
  3762. if (fh->v4l_buffers.active != ZORAN_FREE) {
  3763. zr36057_set_memgrab(zr, 0);
  3764. zr->v4l_buffers.allocated = 0;
  3765. zr->v4l_buffers.active =
  3766. fh->v4l_buffers.active =
  3767. ZORAN_FREE;
  3768. }
  3769. //v4l_fbuffer_free(file);
  3770. fh->v4l_buffers.allocated = 0;
  3771. fh->v4l_buffers.ready_to_be_freed = 1;
  3772. up(&zr->resource_lock);
  3773. }
  3774. break;
  3775. default:
  3776. printk(KERN_ERR
  3777. "%s: munmap() - internal error - unknown map mode %d\n",
  3778. ZR_DEVNAME(zr), fh->map_mode);
  3779. break;
  3780. }
  3781. }
  3782. }
  3783. static struct vm_operations_struct zoran_vm_ops = {
  3784. .open = zoran_vm_open,
  3785. .close = zoran_vm_close,
  3786. };
  3787. static int
  3788. zoran_mmap (struct file *file,
  3789. struct vm_area_struct *vma)
  3790. {
  3791. struct zoran_fh *fh = file->private_data;
  3792. struct zoran *zr = fh->zr;
  3793. unsigned long size = (vma->vm_end - vma->vm_start);
  3794. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  3795. int i, j;
  3796. unsigned long page, start = vma->vm_start, todo, pos, fraglen;
  3797. int first, last;
  3798. struct zoran_mapping *map;
  3799. int res = 0;
  3800. dprintk(3,
  3801. KERN_INFO "%s: mmap(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
  3802. ZR_DEVNAME(zr),
  3803. fh->map_mode == ZORAN_MAP_MODE_RAW ? "V4L" : "MJPEG",
  3804. vma->vm_start, vma->vm_end, size);
  3805. if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
  3806. !(vma->vm_flags & VM_WRITE)) {
  3807. dprintk(1,
  3808. KERN_ERR
  3809. "%s: mmap() - no MAP_SHARED/PROT_{READ,WRITE} given\n",
  3810. ZR_DEVNAME(zr));
  3811. return -EINVAL;
  3812. }
  3813. switch (fh->map_mode) {
  3814. case ZORAN_MAP_MODE_JPG_REC:
  3815. case ZORAN_MAP_MODE_JPG_PLAY:
  3816. /* lock */
  3817. down(&zr->resource_lock);
  3818. /* Map the MJPEG buffers */
  3819. if (!fh->jpg_buffers.allocated) {
  3820. dprintk(1,
  3821. KERN_ERR
  3822. "%s: zoran_mmap(MJPEG) - buffers not yet allocated\n",
  3823. ZR_DEVNAME(zr));
  3824. res = -ENOMEM;
  3825. goto jpg_mmap_unlock_and_return;
  3826. }
  3827. first = offset / fh->jpg_buffers.buffer_size;
  3828. last = first - 1 + size / fh->jpg_buffers.buffer_size;
  3829. if (offset % fh->jpg_buffers.buffer_size != 0 ||
  3830. size % fh->jpg_buffers.buffer_size != 0 || first < 0 ||
  3831. last < 0 || first >= fh->jpg_buffers.num_buffers ||
  3832. last >= fh->jpg_buffers.num_buffers) {
  3833. dprintk(1,
  3834. KERN_ERR
  3835. "%s: mmap(MJPEG) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
  3836. ZR_DEVNAME(zr), offset, size,
  3837. fh->jpg_buffers.buffer_size,
  3838. fh->jpg_buffers.num_buffers);
  3839. res = -EINVAL;
  3840. goto jpg_mmap_unlock_and_return;
  3841. }
  3842. for (i = first; i <= last; i++) {
  3843. if (fh->jpg_buffers.buffer[i].map) {
  3844. dprintk(1,
  3845. KERN_ERR
  3846. "%s: mmap(MJPEG) - buffer %d already mapped\n",
  3847. ZR_DEVNAME(zr), i);
  3848. res = -EBUSY;
  3849. goto jpg_mmap_unlock_and_return;
  3850. }
  3851. }
  3852. /* map these buffers (v4l_buffers[i]) */
  3853. map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
  3854. if (!map) {
  3855. res = -ENOMEM;
  3856. goto jpg_mmap_unlock_and_return;
  3857. }
  3858. map->file = file;
  3859. map->count = 1;
  3860. vma->vm_ops = &zoran_vm_ops;
  3861. vma->vm_flags |= VM_DONTEXPAND;
  3862. vma->vm_private_data = map;
  3863. for (i = first; i <= last; i++) {
  3864. for (j = 0;
  3865. j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
  3866. j++) {
  3867. fraglen =
  3868. (le32_to_cpu(fh->jpg_buffers.buffer[i].
  3869. frag_tab[2 * j + 1]) & ~1) << 1;
  3870. todo = size;
  3871. if (todo > fraglen)
  3872. todo = fraglen;
  3873. pos =
  3874. le32_to_cpu((unsigned long) fh->jpg_buffers.
  3875. buffer[i].frag_tab[2 * j]);
  3876. /* should just be pos on i386 */
  3877. page = virt_to_phys(bus_to_virt(pos))
  3878. >> PAGE_SHIFT;
  3879. if (remap_pfn_range(vma, start, page,
  3880. todo, PAGE_SHARED)) {
  3881. dprintk(1,
  3882. KERN_ERR
  3883. "%s: zoran_mmap(V4L) - remap_pfn_range failed\n",
  3884. ZR_DEVNAME(zr));
  3885. res = -EAGAIN;
  3886. goto jpg_mmap_unlock_and_return;
  3887. }
  3888. size -= todo;
  3889. start += todo;
  3890. if (size == 0)
  3891. break;
  3892. if (le32_to_cpu(fh->jpg_buffers.buffer[i].
  3893. frag_tab[2 * j + 1]) & 1)
  3894. break; /* was last fragment */
  3895. }
  3896. fh->jpg_buffers.buffer[i].map = map;
  3897. if (size == 0)
  3898. break;
  3899. }
  3900. jpg_mmap_unlock_and_return:
  3901. up(&zr->resource_lock);
  3902. break;
  3903. case ZORAN_MAP_MODE_RAW:
  3904. down(&zr->resource_lock);
  3905. /* Map the V4L buffers */
  3906. if (!fh->v4l_buffers.allocated) {
  3907. dprintk(1,
  3908. KERN_ERR
  3909. "%s: zoran_mmap(V4L) - buffers not yet allocated\n",
  3910. ZR_DEVNAME(zr));
  3911. res = -ENOMEM;
  3912. goto v4l_mmap_unlock_and_return;
  3913. }
  3914. first = offset / fh->v4l_buffers.buffer_size;
  3915. last = first - 1 + size / fh->v4l_buffers.buffer_size;
  3916. if (offset % fh->v4l_buffers.buffer_size != 0 ||
  3917. size % fh->v4l_buffers.buffer_size != 0 || first < 0 ||
  3918. last < 0 || first >= fh->v4l_buffers.num_buffers ||
  3919. last >= fh->v4l_buffers.buffer_size) {
  3920. dprintk(1,
  3921. KERN_ERR
  3922. "%s: mmap(V4L) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
  3923. ZR_DEVNAME(zr), offset, size,
  3924. fh->v4l_buffers.buffer_size,
  3925. fh->v4l_buffers.num_buffers);
  3926. res = -EINVAL;
  3927. goto v4l_mmap_unlock_and_return;
  3928. }
  3929. for (i = first; i <= last; i++) {
  3930. if (fh->v4l_buffers.buffer[i].map) {
  3931. dprintk(1,
  3932. KERN_ERR
  3933. "%s: mmap(V4L) - buffer %d already mapped\n",
  3934. ZR_DEVNAME(zr), i);
  3935. res = -EBUSY;
  3936. goto v4l_mmap_unlock_and_return;
  3937. }
  3938. }
  3939. /* map these buffers (v4l_buffers[i]) */
  3940. map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
  3941. if (!map) {
  3942. res = -ENOMEM;
  3943. goto v4l_mmap_unlock_and_return;
  3944. }
  3945. map->file = file;
  3946. map->count = 1;
  3947. vma->vm_ops = &zoran_vm_ops;
  3948. vma->vm_flags |= VM_DONTEXPAND;
  3949. vma->vm_private_data = map;
  3950. for (i = first; i <= last; i++) {
  3951. todo = size;
  3952. if (todo > fh->v4l_buffers.buffer_size)
  3953. todo = fh->v4l_buffers.buffer_size;
  3954. page = fh->v4l_buffers.buffer[i].fbuffer_phys;
  3955. if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
  3956. todo, PAGE_SHARED)) {
  3957. dprintk(1,
  3958. KERN_ERR
  3959. "%s: zoran_mmap(V4L)i - remap_pfn_range failed\n",
  3960. ZR_DEVNAME(zr));
  3961. res = -EAGAIN;
  3962. goto v4l_mmap_unlock_and_return;
  3963. }
  3964. size -= todo;
  3965. start += todo;
  3966. fh->v4l_buffers.buffer[i].map = map;
  3967. if (size == 0)
  3968. break;
  3969. }
  3970. v4l_mmap_unlock_and_return:
  3971. up(&zr->resource_lock);
  3972. break;
  3973. default:
  3974. dprintk(1,
  3975. KERN_ERR
  3976. "%s: zoran_mmap() - internal error - unknown map mode %d\n",
  3977. ZR_DEVNAME(zr), fh->map_mode);
  3978. break;
  3979. }
  3980. return 0;
  3981. }
  3982. static struct file_operations zoran_fops = {
  3983. .owner = THIS_MODULE,
  3984. .open = zoran_open,
  3985. .release = zoran_close,
  3986. .ioctl = zoran_ioctl,
  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. };