zoran_driver.c 114 KB

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