zoran_driver.c 114 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648
  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. fmt->fmt.pix.colorspace =
  2496. V4L2_COLORSPACE_SMPTE170M;
  2497. /* we hereby abuse this variable to show that
  2498. * we're gonna do mjpeg capture */
  2499. fh->map_mode =
  2500. (fmt->type ==
  2501. V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
  2502. ZORAN_MAP_MODE_JPG_REC :
  2503. ZORAN_MAP_MODE_JPG_PLAY;
  2504. sfmtjpg_unlock_and_return:
  2505. mutex_unlock(&zr->resource_lock);
  2506. } else {
  2507. for (i = 0; i < NUM_FORMATS; i++)
  2508. if (fmt->fmt.pix.pixelformat ==
  2509. zoran_formats[i].fourcc)
  2510. break;
  2511. if (i == NUM_FORMATS) {
  2512. dprintk(1,
  2513. KERN_ERR
  2514. "%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x (%4.4s)\n",
  2515. ZR_DEVNAME(zr),
  2516. fmt->fmt.pix.pixelformat,
  2517. (char *) &printformat);
  2518. return -EINVAL;
  2519. }
  2520. mutex_lock(&zr->resource_lock);
  2521. if (fh->jpg_buffers.allocated ||
  2522. (fh->v4l_buffers.allocated &&
  2523. fh->v4l_buffers.active !=
  2524. ZORAN_FREE)) {
  2525. dprintk(1,
  2526. KERN_ERR
  2527. "%s: VIDIOC_S_FMT - cannot change capture mode\n",
  2528. ZR_DEVNAME(zr));
  2529. res = -EBUSY;
  2530. goto sfmtv4l_unlock_and_return;
  2531. }
  2532. if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
  2533. fmt->fmt.pix.height =
  2534. BUZ_MAX_HEIGHT;
  2535. if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
  2536. fmt->fmt.pix.width = BUZ_MAX_WIDTH;
  2537. if ((res =
  2538. zoran_v4l_set_format(file,
  2539. fmt->fmt.pix.
  2540. width,
  2541. fmt->fmt.pix.
  2542. height,
  2543. &zoran_formats
  2544. [i])))
  2545. goto sfmtv4l_unlock_and_return;
  2546. /* tell the user the
  2547. * results/missing stuff */
  2548. fmt->fmt.pix.bytesperline =
  2549. fh->v4l_settings.bytesperline;
  2550. fmt->fmt.pix.sizeimage =
  2551. fh->v4l_settings.height *
  2552. fh->v4l_settings.bytesperline;
  2553. fmt->fmt.pix.colorspace =
  2554. fh->v4l_settings.format->colorspace;
  2555. if (BUZ_MAX_HEIGHT <
  2556. (fh->v4l_settings.height * 2))
  2557. fmt->fmt.pix.field =
  2558. V4L2_FIELD_INTERLACED;
  2559. else
  2560. fmt->fmt.pix.field =
  2561. V4L2_FIELD_TOP;
  2562. fh->map_mode = ZORAN_MAP_MODE_RAW;
  2563. sfmtv4l_unlock_and_return:
  2564. mutex_unlock(&zr->resource_lock);
  2565. }
  2566. break;
  2567. default:
  2568. dprintk(1,
  2569. KERN_ERR
  2570. "%s: VIDIOC_S_FMT - unsupported type %d\n",
  2571. ZR_DEVNAME(zr), fmt->type);
  2572. return -EINVAL;
  2573. }
  2574. return res;
  2575. }
  2576. break;
  2577. case VIDIOC_G_FBUF:
  2578. {
  2579. struct v4l2_framebuffer *fb = arg;
  2580. dprintk(3, KERN_DEBUG "%s: VIDIOC_G_FBUF\n", ZR_DEVNAME(zr));
  2581. memset(fb, 0, sizeof(*fb));
  2582. mutex_lock(&zr->resource_lock);
  2583. fb->base = zr->buffer.base;
  2584. fb->fmt.width = zr->buffer.width;
  2585. fb->fmt.height = zr->buffer.height;
  2586. if (zr->overlay_settings.format) {
  2587. fb->fmt.pixelformat =
  2588. fh->overlay_settings.format->fourcc;
  2589. }
  2590. fb->fmt.bytesperline = zr->buffer.bytesperline;
  2591. mutex_unlock(&zr->resource_lock);
  2592. fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
  2593. fb->fmt.field = V4L2_FIELD_INTERLACED;
  2594. fb->flags = V4L2_FBUF_FLAG_OVERLAY;
  2595. fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
  2596. return 0;
  2597. }
  2598. break;
  2599. case VIDIOC_S_FBUF:
  2600. {
  2601. int i, res = 0;
  2602. struct v4l2_framebuffer *fb = arg;
  2603. __le32 printformat = __cpu_to_le32(fb->fmt.pixelformat);
  2604. dprintk(3,
  2605. KERN_DEBUG
  2606. "%s: VIDIOC_S_FBUF - base=0x%p, size=%dx%d, bpl=%d, fmt=0x%x (%4.4s)\n",
  2607. ZR_DEVNAME(zr), fb->base, fb->fmt.width, fb->fmt.height,
  2608. fb->fmt.bytesperline, fb->fmt.pixelformat,
  2609. (char *) &printformat);
  2610. for (i = 0; i < NUM_FORMATS; i++)
  2611. if (zoran_formats[i].fourcc == fb->fmt.pixelformat)
  2612. break;
  2613. if (i == NUM_FORMATS) {
  2614. dprintk(1,
  2615. KERN_ERR
  2616. "%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
  2617. ZR_DEVNAME(zr), fb->fmt.pixelformat,
  2618. (char *) &printformat);
  2619. return -EINVAL;
  2620. }
  2621. mutex_lock(&zr->resource_lock);
  2622. res =
  2623. setup_fbuffer(file, fb->base, &zoran_formats[i],
  2624. fb->fmt.width, fb->fmt.height,
  2625. fb->fmt.bytesperline);
  2626. mutex_unlock(&zr->resource_lock);
  2627. return res;
  2628. }
  2629. break;
  2630. case VIDIOC_OVERLAY:
  2631. {
  2632. int *on = arg, res;
  2633. dprintk(3, KERN_DEBUG "%s: VIDIOC_PREVIEW - on=%d\n",
  2634. ZR_DEVNAME(zr), *on);
  2635. mutex_lock(&zr->resource_lock);
  2636. res = setup_overlay(file, *on);
  2637. mutex_unlock(&zr->resource_lock);
  2638. return res;
  2639. }
  2640. break;
  2641. case VIDIOC_REQBUFS:
  2642. {
  2643. struct v4l2_requestbuffers *req = arg;
  2644. int res = 0;
  2645. dprintk(3, KERN_DEBUG "%s: VIDIOC_REQBUFS - type=%d\n",
  2646. ZR_DEVNAME(zr), req->type);
  2647. if (req->memory != V4L2_MEMORY_MMAP) {
  2648. dprintk(1,
  2649. KERN_ERR
  2650. "%s: only MEMORY_MMAP capture is supported, not %d\n",
  2651. ZR_DEVNAME(zr), req->memory);
  2652. return -EINVAL;
  2653. }
  2654. mutex_lock(&zr->resource_lock);
  2655. if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
  2656. dprintk(1,
  2657. KERN_ERR
  2658. "%s: VIDIOC_REQBUFS - buffers allready allocated\n",
  2659. ZR_DEVNAME(zr));
  2660. res = -EBUSY;
  2661. goto v4l2reqbuf_unlock_and_return;
  2662. }
  2663. if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
  2664. req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  2665. /* control user input */
  2666. if (req->count < 2)
  2667. req->count = 2;
  2668. if (req->count > v4l_nbufs)
  2669. req->count = v4l_nbufs;
  2670. fh->v4l_buffers.num_buffers = req->count;
  2671. if (v4l_fbuffer_alloc(file)) {
  2672. res = -ENOMEM;
  2673. goto v4l2reqbuf_unlock_and_return;
  2674. }
  2675. /* The next mmap will map the V4L buffers */
  2676. fh->map_mode = ZORAN_MAP_MODE_RAW;
  2677. } else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
  2678. fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
  2679. /* we need to calculate size ourselves now */
  2680. if (req->count < 4)
  2681. req->count = 4;
  2682. if (req->count > jpg_nbufs)
  2683. req->count = jpg_nbufs;
  2684. fh->jpg_buffers.num_buffers = req->count;
  2685. fh->jpg_buffers.buffer_size =
  2686. zoran_v4l2_calc_bufsize(&fh->jpg_settings);
  2687. if (jpg_fbuffer_alloc(file)) {
  2688. res = -ENOMEM;
  2689. goto v4l2reqbuf_unlock_and_return;
  2690. }
  2691. /* The next mmap will map the MJPEG buffers */
  2692. if (req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  2693. fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
  2694. else
  2695. fh->map_mode = ZORAN_MAP_MODE_JPG_PLAY;
  2696. } else {
  2697. dprintk(1,
  2698. KERN_ERR
  2699. "%s: VIDIOC_REQBUFS - unknown type %d\n",
  2700. ZR_DEVNAME(zr), req->type);
  2701. res = -EINVAL;
  2702. goto v4l2reqbuf_unlock_and_return;
  2703. }
  2704. v4l2reqbuf_unlock_and_return:
  2705. mutex_unlock(&zr->resource_lock);
  2706. return 0;
  2707. }
  2708. break;
  2709. case VIDIOC_QUERYBUF:
  2710. {
  2711. struct v4l2_buffer *buf = arg;
  2712. __u32 type = buf->type;
  2713. int index = buf->index, res;
  2714. dprintk(3,
  2715. KERN_DEBUG
  2716. "%s: VIDIOC_QUERYBUF - index=%d, type=%d\n",
  2717. ZR_DEVNAME(zr), buf->index, buf->type);
  2718. memset(buf, 0, sizeof(*buf));
  2719. buf->type = type;
  2720. buf->index = index;
  2721. mutex_lock(&zr->resource_lock);
  2722. res = zoran_v4l2_buffer_status(file, buf, buf->index);
  2723. mutex_unlock(&zr->resource_lock);
  2724. return res;
  2725. }
  2726. break;
  2727. case VIDIOC_QBUF:
  2728. {
  2729. struct v4l2_buffer *buf = arg;
  2730. int res = 0, codec_mode, buf_type;
  2731. dprintk(3,
  2732. KERN_DEBUG "%s: VIDIOC_QBUF - type=%d, index=%d\n",
  2733. ZR_DEVNAME(zr), buf->type, buf->index);
  2734. mutex_lock(&zr->resource_lock);
  2735. switch (fh->map_mode) {
  2736. case ZORAN_MAP_MODE_RAW:
  2737. if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  2738. dprintk(1,
  2739. KERN_ERR
  2740. "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
  2741. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  2742. res = -EINVAL;
  2743. goto qbuf_unlock_and_return;
  2744. }
  2745. res = zoran_v4l_queue_frame(file, buf->index);
  2746. if (res)
  2747. goto qbuf_unlock_and_return;
  2748. if (!zr->v4l_memgrab_active &&
  2749. fh->v4l_buffers.active == ZORAN_LOCKED)
  2750. zr36057_set_memgrab(zr, 1);
  2751. break;
  2752. case ZORAN_MAP_MODE_JPG_REC:
  2753. case ZORAN_MAP_MODE_JPG_PLAY:
  2754. if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
  2755. buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  2756. codec_mode = BUZ_MODE_MOTION_DECOMPRESS;
  2757. } else {
  2758. buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  2759. codec_mode = BUZ_MODE_MOTION_COMPRESS;
  2760. }
  2761. if (buf->type != buf_type) {
  2762. dprintk(1,
  2763. KERN_ERR
  2764. "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
  2765. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  2766. res = -EINVAL;
  2767. goto qbuf_unlock_and_return;
  2768. }
  2769. res =
  2770. zoran_jpg_queue_frame(file, buf->index,
  2771. codec_mode);
  2772. if (res != 0)
  2773. goto qbuf_unlock_and_return;
  2774. if (zr->codec_mode == BUZ_MODE_IDLE &&
  2775. fh->jpg_buffers.active == ZORAN_LOCKED) {
  2776. zr36057_enable_jpg(zr, codec_mode);
  2777. }
  2778. break;
  2779. default:
  2780. dprintk(1,
  2781. KERN_ERR
  2782. "%s: VIDIOC_QBUF - unsupported type %d\n",
  2783. ZR_DEVNAME(zr), buf->type);
  2784. res = -EINVAL;
  2785. goto qbuf_unlock_and_return;
  2786. }
  2787. qbuf_unlock_and_return:
  2788. mutex_unlock(&zr->resource_lock);
  2789. return res;
  2790. }
  2791. break;
  2792. case VIDIOC_DQBUF:
  2793. {
  2794. struct v4l2_buffer *buf = arg;
  2795. int res = 0, buf_type, num = -1; /* compiler borks here (?) */
  2796. dprintk(3, KERN_DEBUG "%s: VIDIOC_DQBUF - type=%d\n",
  2797. ZR_DEVNAME(zr), buf->type);
  2798. mutex_lock(&zr->resource_lock);
  2799. switch (fh->map_mode) {
  2800. case ZORAN_MAP_MODE_RAW:
  2801. if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  2802. dprintk(1,
  2803. KERN_ERR
  2804. "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
  2805. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  2806. res = -EINVAL;
  2807. goto dqbuf_unlock_and_return;
  2808. }
  2809. num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
  2810. if (file->f_flags & O_NONBLOCK &&
  2811. zr->v4l_buffers.buffer[num].state !=
  2812. BUZ_STATE_DONE) {
  2813. res = -EAGAIN;
  2814. goto dqbuf_unlock_and_return;
  2815. }
  2816. res = v4l_sync(file, num);
  2817. if (res)
  2818. goto dqbuf_unlock_and_return;
  2819. else
  2820. zr->v4l_sync_tail++;
  2821. res = zoran_v4l2_buffer_status(file, buf, num);
  2822. break;
  2823. case ZORAN_MAP_MODE_JPG_REC:
  2824. case ZORAN_MAP_MODE_JPG_PLAY:
  2825. {
  2826. struct zoran_sync bs;
  2827. if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
  2828. buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  2829. else
  2830. buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  2831. if (buf->type != buf_type) {
  2832. dprintk(1,
  2833. KERN_ERR
  2834. "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
  2835. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  2836. res = -EINVAL;
  2837. goto dqbuf_unlock_and_return;
  2838. }
  2839. num =
  2840. zr->jpg_pend[zr->
  2841. jpg_que_tail & BUZ_MASK_FRAME];
  2842. if (file->f_flags & O_NONBLOCK &&
  2843. zr->jpg_buffers.buffer[num].state !=
  2844. BUZ_STATE_DONE) {
  2845. res = -EAGAIN;
  2846. goto dqbuf_unlock_and_return;
  2847. }
  2848. res = jpg_sync(file, &bs);
  2849. if (res)
  2850. goto dqbuf_unlock_and_return;
  2851. res =
  2852. zoran_v4l2_buffer_status(file, buf, bs.frame);
  2853. break;
  2854. }
  2855. default:
  2856. dprintk(1,
  2857. KERN_ERR
  2858. "%s: VIDIOC_DQBUF - unsupported type %d\n",
  2859. ZR_DEVNAME(zr), buf->type);
  2860. res = -EINVAL;
  2861. goto dqbuf_unlock_and_return;
  2862. }
  2863. dqbuf_unlock_and_return:
  2864. mutex_unlock(&zr->resource_lock);
  2865. return res;
  2866. }
  2867. break;
  2868. case VIDIOC_STREAMON:
  2869. {
  2870. int res = 0;
  2871. dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMON\n", ZR_DEVNAME(zr));
  2872. mutex_lock(&zr->resource_lock);
  2873. switch (fh->map_mode) {
  2874. case ZORAN_MAP_MODE_RAW: /* raw capture */
  2875. if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
  2876. fh->v4l_buffers.active != ZORAN_ACTIVE) {
  2877. res = -EBUSY;
  2878. goto strmon_unlock_and_return;
  2879. }
  2880. zr->v4l_buffers.active = fh->v4l_buffers.active =
  2881. ZORAN_LOCKED;
  2882. zr->v4l_settings = fh->v4l_settings;
  2883. zr->v4l_sync_tail = zr->v4l_pend_tail;
  2884. if (!zr->v4l_memgrab_active &&
  2885. zr->v4l_pend_head != zr->v4l_pend_tail) {
  2886. zr36057_set_memgrab(zr, 1);
  2887. }
  2888. break;
  2889. case ZORAN_MAP_MODE_JPG_REC:
  2890. case ZORAN_MAP_MODE_JPG_PLAY:
  2891. /* what is the codec mode right now? */
  2892. if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
  2893. fh->jpg_buffers.active != ZORAN_ACTIVE) {
  2894. res = -EBUSY;
  2895. goto strmon_unlock_and_return;
  2896. }
  2897. zr->jpg_buffers.active = fh->jpg_buffers.active =
  2898. ZORAN_LOCKED;
  2899. if (zr->jpg_que_head != zr->jpg_que_tail) {
  2900. /* Start the jpeg codec when the first frame is queued */
  2901. jpeg_start(zr);
  2902. }
  2903. break;
  2904. default:
  2905. dprintk(1,
  2906. KERN_ERR
  2907. "%s: VIDIOC_STREAMON - invalid map mode %d\n",
  2908. ZR_DEVNAME(zr), fh->map_mode);
  2909. res = -EINVAL;
  2910. goto strmon_unlock_and_return;
  2911. }
  2912. strmon_unlock_and_return:
  2913. mutex_unlock(&zr->resource_lock);
  2914. return res;
  2915. }
  2916. break;
  2917. case VIDIOC_STREAMOFF:
  2918. {
  2919. int i, res = 0;
  2920. dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMOFF\n", ZR_DEVNAME(zr));
  2921. mutex_lock(&zr->resource_lock);
  2922. switch (fh->map_mode) {
  2923. case ZORAN_MAP_MODE_RAW: /* raw capture */
  2924. if (fh->v4l_buffers.active == ZORAN_FREE &&
  2925. zr->v4l_buffers.active != ZORAN_FREE) {
  2926. res = -EPERM; /* stay off other's settings! */
  2927. goto strmoff_unlock_and_return;
  2928. }
  2929. if (zr->v4l_buffers.active == ZORAN_FREE)
  2930. goto strmoff_unlock_and_return;
  2931. /* unload capture */
  2932. if (zr->v4l_memgrab_active) {
  2933. unsigned long flags;
  2934. spin_lock_irqsave(&zr->spinlock, flags);
  2935. zr36057_set_memgrab(zr, 0);
  2936. spin_unlock_irqrestore(&zr->spinlock, flags);
  2937. }
  2938. for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
  2939. zr->v4l_buffers.buffer[i].state =
  2940. BUZ_STATE_USER;
  2941. fh->v4l_buffers = zr->v4l_buffers;
  2942. zr->v4l_buffers.active = fh->v4l_buffers.active =
  2943. ZORAN_FREE;
  2944. zr->v4l_grab_seq = 0;
  2945. zr->v4l_pend_head = zr->v4l_pend_tail = 0;
  2946. zr->v4l_sync_tail = 0;
  2947. break;
  2948. case ZORAN_MAP_MODE_JPG_REC:
  2949. case ZORAN_MAP_MODE_JPG_PLAY:
  2950. if (fh->jpg_buffers.active == ZORAN_FREE &&
  2951. zr->jpg_buffers.active != ZORAN_FREE) {
  2952. res = -EPERM; /* stay off other's settings! */
  2953. goto strmoff_unlock_and_return;
  2954. }
  2955. if (zr->jpg_buffers.active == ZORAN_FREE)
  2956. goto strmoff_unlock_and_return;
  2957. res =
  2958. jpg_qbuf(file, -1,
  2959. (fh->map_mode ==
  2960. ZORAN_MAP_MODE_JPG_REC) ?
  2961. BUZ_MODE_MOTION_COMPRESS :
  2962. BUZ_MODE_MOTION_DECOMPRESS);
  2963. if (res)
  2964. goto strmoff_unlock_and_return;
  2965. break;
  2966. default:
  2967. dprintk(1,
  2968. KERN_ERR
  2969. "%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
  2970. ZR_DEVNAME(zr), fh->map_mode);
  2971. res = -EINVAL;
  2972. goto strmoff_unlock_and_return;
  2973. }
  2974. strmoff_unlock_and_return:
  2975. mutex_unlock(&zr->resource_lock);
  2976. return res;
  2977. }
  2978. break;
  2979. case VIDIOC_QUERYCTRL:
  2980. {
  2981. struct v4l2_queryctrl *ctrl = arg;
  2982. dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCTRL - id=%d\n",
  2983. ZR_DEVNAME(zr), ctrl->id);
  2984. /* we only support hue/saturation/contrast/brightness */
  2985. if (ctrl->id < V4L2_CID_BRIGHTNESS ||
  2986. ctrl->id > V4L2_CID_HUE)
  2987. return -EINVAL;
  2988. else {
  2989. int id = ctrl->id;
  2990. memset(ctrl, 0, sizeof(*ctrl));
  2991. ctrl->id = id;
  2992. }
  2993. switch (ctrl->id) {
  2994. case V4L2_CID_BRIGHTNESS:
  2995. strncpy(ctrl->name, "Brightness", sizeof(ctrl->name)-1);
  2996. break;
  2997. case V4L2_CID_CONTRAST:
  2998. strncpy(ctrl->name, "Contrast", sizeof(ctrl->name)-1);
  2999. break;
  3000. case V4L2_CID_SATURATION:
  3001. strncpy(ctrl->name, "Saturation", sizeof(ctrl->name)-1);
  3002. break;
  3003. case V4L2_CID_HUE:
  3004. strncpy(ctrl->name, "Hue", sizeof(ctrl->name)-1);
  3005. break;
  3006. }
  3007. ctrl->minimum = 0;
  3008. ctrl->maximum = 65535;
  3009. ctrl->step = 1;
  3010. ctrl->default_value = 32768;
  3011. ctrl->type = V4L2_CTRL_TYPE_INTEGER;
  3012. return 0;
  3013. }
  3014. break;
  3015. case VIDIOC_G_CTRL:
  3016. {
  3017. struct v4l2_control *ctrl = arg;
  3018. dprintk(3, KERN_DEBUG "%s: VIDIOC_G_CTRL - id=%d\n",
  3019. ZR_DEVNAME(zr), ctrl->id);
  3020. /* we only support hue/saturation/contrast/brightness */
  3021. if (ctrl->id < V4L2_CID_BRIGHTNESS ||
  3022. ctrl->id > V4L2_CID_HUE)
  3023. return -EINVAL;
  3024. mutex_lock(&zr->resource_lock);
  3025. switch (ctrl->id) {
  3026. case V4L2_CID_BRIGHTNESS:
  3027. ctrl->value = zr->brightness;
  3028. break;
  3029. case V4L2_CID_CONTRAST:
  3030. ctrl->value = zr->contrast;
  3031. break;
  3032. case V4L2_CID_SATURATION:
  3033. ctrl->value = zr->saturation;
  3034. break;
  3035. case V4L2_CID_HUE:
  3036. ctrl->value = zr->hue;
  3037. break;
  3038. }
  3039. mutex_unlock(&zr->resource_lock);
  3040. return 0;
  3041. }
  3042. break;
  3043. case VIDIOC_S_CTRL:
  3044. {
  3045. struct v4l2_control *ctrl = arg;
  3046. struct video_picture pict;
  3047. dprintk(3, KERN_DEBUG "%s: VIDIOC_S_CTRL - id=%d\n",
  3048. ZR_DEVNAME(zr), ctrl->id);
  3049. /* we only support hue/saturation/contrast/brightness */
  3050. if (ctrl->id < V4L2_CID_BRIGHTNESS ||
  3051. ctrl->id > V4L2_CID_HUE)
  3052. return -EINVAL;
  3053. if (ctrl->value < 0 || ctrl->value > 65535) {
  3054. dprintk(1,
  3055. KERN_ERR
  3056. "%s: VIDIOC_S_CTRL - invalid value %d for id=%d\n",
  3057. ZR_DEVNAME(zr), ctrl->value, ctrl->id);
  3058. return -EINVAL;
  3059. }
  3060. mutex_lock(&zr->resource_lock);
  3061. switch (ctrl->id) {
  3062. case V4L2_CID_BRIGHTNESS:
  3063. zr->brightness = ctrl->value;
  3064. break;
  3065. case V4L2_CID_CONTRAST:
  3066. zr->contrast = ctrl->value;
  3067. break;
  3068. case V4L2_CID_SATURATION:
  3069. zr->saturation = ctrl->value;
  3070. break;
  3071. case V4L2_CID_HUE:
  3072. zr->hue = ctrl->value;
  3073. break;
  3074. }
  3075. pict.brightness = zr->brightness;
  3076. pict.contrast = zr->contrast;
  3077. pict.colour = zr->saturation;
  3078. pict.hue = zr->hue;
  3079. decoder_command(zr, DECODER_SET_PICTURE, &pict);
  3080. mutex_unlock(&zr->resource_lock);
  3081. return 0;
  3082. }
  3083. break;
  3084. case VIDIOC_ENUMSTD:
  3085. {
  3086. struct v4l2_standard *std = arg;
  3087. dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMSTD - index=%d\n",
  3088. ZR_DEVNAME(zr), std->index);
  3089. if (std->index < 0 || std->index >= (zr->card.norms + 1))
  3090. return -EINVAL;
  3091. else {
  3092. int id = std->index;
  3093. memset(std, 0, sizeof(*std));
  3094. std->index = id;
  3095. }
  3096. if (std->index == zr->card.norms) {
  3097. /* if we have autodetect, ... */
  3098. struct video_decoder_capability caps;
  3099. decoder_command(zr, DECODER_GET_CAPABILITIES,
  3100. &caps);
  3101. if (caps.flags & VIDEO_DECODER_AUTO) {
  3102. std->id = V4L2_STD_ALL;
  3103. strncpy(std->name, "Autodetect", sizeof(std->name)-1);
  3104. return 0;
  3105. } else
  3106. return -EINVAL;
  3107. }
  3108. switch (std->index) {
  3109. case 0:
  3110. std->id = V4L2_STD_PAL;
  3111. strncpy(std->name, "PAL", sizeof(std->name)-1);
  3112. std->frameperiod.numerator = 1;
  3113. std->frameperiod.denominator = 25;
  3114. std->framelines = zr->card.tvn[0]->Ht;
  3115. break;
  3116. case 1:
  3117. std->id = V4L2_STD_NTSC;
  3118. strncpy(std->name, "NTSC", sizeof(std->name)-1);
  3119. std->frameperiod.numerator = 1001;
  3120. std->frameperiod.denominator = 30000;
  3121. std->framelines = zr->card.tvn[1]->Ht;
  3122. break;
  3123. case 2:
  3124. std->id = V4L2_STD_SECAM;
  3125. strncpy(std->name, "SECAM", sizeof(std->name)-1);
  3126. std->frameperiod.numerator = 1;
  3127. std->frameperiod.denominator = 25;
  3128. std->framelines = zr->card.tvn[2]->Ht;
  3129. break;
  3130. }
  3131. return 0;
  3132. }
  3133. break;
  3134. case VIDIOC_G_STD:
  3135. {
  3136. v4l2_std_id *std = arg;
  3137. int norm;
  3138. dprintk(3, KERN_DEBUG "%s: VIDIOC_G_STD\n", ZR_DEVNAME(zr));
  3139. mutex_lock(&zr->resource_lock);
  3140. norm = zr->norm;
  3141. mutex_unlock(&zr->resource_lock);
  3142. switch (norm) {
  3143. case VIDEO_MODE_PAL:
  3144. *std = V4L2_STD_PAL;
  3145. break;
  3146. case VIDEO_MODE_NTSC:
  3147. *std = V4L2_STD_NTSC;
  3148. break;
  3149. case VIDEO_MODE_SECAM:
  3150. *std = V4L2_STD_SECAM;
  3151. break;
  3152. }
  3153. return 0;
  3154. }
  3155. break;
  3156. case VIDIOC_S_STD:
  3157. {
  3158. int norm = -1, res = 0;
  3159. v4l2_std_id *std = arg;
  3160. dprintk(3, KERN_DEBUG "%s: VIDIOC_S_STD - norm=0x%llx\n",
  3161. ZR_DEVNAME(zr), (unsigned long long)*std);
  3162. if ((*std & V4L2_STD_PAL) && !(*std & ~V4L2_STD_PAL))
  3163. norm = VIDEO_MODE_PAL;
  3164. else if ((*std & V4L2_STD_NTSC) && !(*std & ~V4L2_STD_NTSC))
  3165. norm = VIDEO_MODE_NTSC;
  3166. else if ((*std & V4L2_STD_SECAM) && !(*std & ~V4L2_STD_SECAM))
  3167. norm = VIDEO_MODE_SECAM;
  3168. else if (*std == V4L2_STD_ALL)
  3169. norm = VIDEO_MODE_AUTO;
  3170. else {
  3171. dprintk(1,
  3172. KERN_ERR
  3173. "%s: VIDIOC_S_STD - invalid norm 0x%llx\n",
  3174. ZR_DEVNAME(zr), (unsigned long long)*std);
  3175. return -EINVAL;
  3176. }
  3177. mutex_lock(&zr->resource_lock);
  3178. if ((res = zoran_set_norm(zr, norm)))
  3179. goto sstd_unlock_and_return;
  3180. res = wait_grab_pending(zr);
  3181. sstd_unlock_and_return:
  3182. mutex_unlock(&zr->resource_lock);
  3183. return res;
  3184. }
  3185. break;
  3186. case VIDIOC_ENUMINPUT:
  3187. {
  3188. struct v4l2_input *inp = arg;
  3189. int status;
  3190. dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMINPUT - index=%d\n",
  3191. ZR_DEVNAME(zr), inp->index);
  3192. if (inp->index < 0 || inp->index >= zr->card.inputs)
  3193. return -EINVAL;
  3194. else {
  3195. int id = inp->index;
  3196. memset(inp, 0, sizeof(*inp));
  3197. inp->index = id;
  3198. }
  3199. strncpy(inp->name, zr->card.input[inp->index].name,
  3200. sizeof(inp->name) - 1);
  3201. inp->type = V4L2_INPUT_TYPE_CAMERA;
  3202. inp->std = V4L2_STD_ALL;
  3203. /* Get status of video decoder */
  3204. mutex_lock(&zr->resource_lock);
  3205. decoder_command(zr, DECODER_GET_STATUS, &status);
  3206. mutex_unlock(&zr->resource_lock);
  3207. if (!(status & DECODER_STATUS_GOOD)) {
  3208. inp->status |= V4L2_IN_ST_NO_POWER;
  3209. inp->status |= V4L2_IN_ST_NO_SIGNAL;
  3210. }
  3211. if (!(status & DECODER_STATUS_COLOR))
  3212. inp->status |= V4L2_IN_ST_NO_COLOR;
  3213. return 0;
  3214. }
  3215. break;
  3216. case VIDIOC_G_INPUT:
  3217. {
  3218. int *input = arg;
  3219. dprintk(3, KERN_DEBUG "%s: VIDIOC_G_INPUT\n", ZR_DEVNAME(zr));
  3220. mutex_lock(&zr->resource_lock);
  3221. *input = zr->input;
  3222. mutex_unlock(&zr->resource_lock);
  3223. return 0;
  3224. }
  3225. break;
  3226. case VIDIOC_S_INPUT:
  3227. {
  3228. int *input = arg, res = 0;
  3229. dprintk(3, KERN_DEBUG "%s: VIDIOC_S_INPUT - input=%d\n",
  3230. ZR_DEVNAME(zr), *input);
  3231. mutex_lock(&zr->resource_lock);
  3232. if ((res = zoran_set_input(zr, *input)))
  3233. goto sinput_unlock_and_return;
  3234. /* Make sure the changes come into effect */
  3235. res = wait_grab_pending(zr);
  3236. sinput_unlock_and_return:
  3237. mutex_unlock(&zr->resource_lock);
  3238. return res;
  3239. }
  3240. break;
  3241. case VIDIOC_ENUMOUTPUT:
  3242. {
  3243. struct v4l2_output *outp = arg;
  3244. dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMOUTPUT - index=%d\n",
  3245. ZR_DEVNAME(zr), outp->index);
  3246. if (outp->index != 0)
  3247. return -EINVAL;
  3248. memset(outp, 0, sizeof(*outp));
  3249. outp->index = 0;
  3250. outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
  3251. strncpy(outp->name, "Autodetect", sizeof(outp->name)-1);
  3252. return 0;
  3253. }
  3254. break;
  3255. case VIDIOC_G_OUTPUT:
  3256. {
  3257. int *output = arg;
  3258. dprintk(3, KERN_DEBUG "%s: VIDIOC_G_OUTPUT\n", ZR_DEVNAME(zr));
  3259. *output = 0;
  3260. return 0;
  3261. }
  3262. break;
  3263. case VIDIOC_S_OUTPUT:
  3264. {
  3265. int *output = arg;
  3266. dprintk(3, KERN_DEBUG "%s: VIDIOC_S_OUTPUT - output=%d\n",
  3267. ZR_DEVNAME(zr), *output);
  3268. if (*output != 0)
  3269. return -EINVAL;
  3270. return 0;
  3271. }
  3272. break;
  3273. /* cropping (sub-frame capture) */
  3274. case VIDIOC_CROPCAP:
  3275. {
  3276. struct v4l2_cropcap *cropcap = arg;
  3277. int type = cropcap->type, res = 0;
  3278. dprintk(3, KERN_ERR "%s: VIDIOC_CROPCAP - type=%d\n",
  3279. ZR_DEVNAME(zr), cropcap->type);
  3280. memset(cropcap, 0, sizeof(*cropcap));
  3281. cropcap->type = type;
  3282. mutex_lock(&zr->resource_lock);
  3283. if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  3284. (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  3285. fh->map_mode == ZORAN_MAP_MODE_RAW)) {
  3286. dprintk(1,
  3287. KERN_ERR
  3288. "%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
  3289. ZR_DEVNAME(zr));
  3290. res = -EINVAL;
  3291. goto cropcap_unlock_and_return;
  3292. }
  3293. cropcap->bounds.top = cropcap->bounds.left = 0;
  3294. cropcap->bounds.width = BUZ_MAX_WIDTH;
  3295. cropcap->bounds.height = BUZ_MAX_HEIGHT;
  3296. cropcap->defrect.top = cropcap->defrect.left = 0;
  3297. cropcap->defrect.width = BUZ_MIN_WIDTH;
  3298. cropcap->defrect.height = BUZ_MIN_HEIGHT;
  3299. cropcap_unlock_and_return:
  3300. mutex_unlock(&zr->resource_lock);
  3301. return res;
  3302. }
  3303. break;
  3304. case VIDIOC_G_CROP:
  3305. {
  3306. struct v4l2_crop *crop = arg;
  3307. int type = crop->type, res = 0;
  3308. dprintk(3, KERN_ERR "%s: VIDIOC_G_CROP - type=%d\n",
  3309. ZR_DEVNAME(zr), crop->type);
  3310. memset(crop, 0, sizeof(*crop));
  3311. crop->type = type;
  3312. mutex_lock(&zr->resource_lock);
  3313. if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  3314. (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  3315. fh->map_mode == ZORAN_MAP_MODE_RAW)) {
  3316. dprintk(1,
  3317. KERN_ERR
  3318. "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
  3319. ZR_DEVNAME(zr));
  3320. res = -EINVAL;
  3321. goto gcrop_unlock_and_return;
  3322. }
  3323. crop->c.top = fh->jpg_settings.img_y;
  3324. crop->c.left = fh->jpg_settings.img_x;
  3325. crop->c.width = fh->jpg_settings.img_width;
  3326. crop->c.height = fh->jpg_settings.img_height;
  3327. gcrop_unlock_and_return:
  3328. mutex_unlock(&zr->resource_lock);
  3329. return res;
  3330. }
  3331. break;
  3332. case VIDIOC_S_CROP:
  3333. {
  3334. struct v4l2_crop *crop = arg;
  3335. int res = 0;
  3336. settings = fh->jpg_settings;
  3337. dprintk(3,
  3338. KERN_ERR
  3339. "%s: VIDIOC_S_CROP - type=%d, x=%d,y=%d,w=%d,h=%d\n",
  3340. ZR_DEVNAME(zr), crop->type, crop->c.left, crop->c.top,
  3341. crop->c.width, crop->c.height);
  3342. mutex_lock(&zr->resource_lock);
  3343. if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
  3344. dprintk(1,
  3345. KERN_ERR
  3346. "%s: VIDIOC_S_CROP - cannot change settings while active\n",
  3347. ZR_DEVNAME(zr));
  3348. res = -EBUSY;
  3349. goto scrop_unlock_and_return;
  3350. }
  3351. if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  3352. (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  3353. fh->map_mode == ZORAN_MAP_MODE_RAW)) {
  3354. dprintk(1,
  3355. KERN_ERR
  3356. "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
  3357. ZR_DEVNAME(zr));
  3358. res = -EINVAL;
  3359. goto scrop_unlock_and_return;
  3360. }
  3361. /* move into a form that we understand */
  3362. settings.img_x = crop->c.left;
  3363. settings.img_y = crop->c.top;
  3364. settings.img_width = crop->c.width;
  3365. settings.img_height = crop->c.height;
  3366. /* check validity */
  3367. if ((res = zoran_check_jpg_settings(zr, &settings)))
  3368. goto scrop_unlock_and_return;
  3369. /* accept */
  3370. fh->jpg_settings = settings;
  3371. scrop_unlock_and_return:
  3372. mutex_unlock(&zr->resource_lock);
  3373. return res;
  3374. }
  3375. break;
  3376. case VIDIOC_G_JPEGCOMP:
  3377. {
  3378. struct v4l2_jpegcompression *params = arg;
  3379. dprintk(3, KERN_DEBUG "%s: VIDIOC_G_JPEGCOMP\n",
  3380. ZR_DEVNAME(zr));
  3381. memset(params, 0, sizeof(*params));
  3382. mutex_lock(&zr->resource_lock);
  3383. params->quality = fh->jpg_settings.jpg_comp.quality;
  3384. params->APPn = fh->jpg_settings.jpg_comp.APPn;
  3385. memcpy(params->APP_data,
  3386. fh->jpg_settings.jpg_comp.APP_data,
  3387. fh->jpg_settings.jpg_comp.APP_len);
  3388. params->APP_len = fh->jpg_settings.jpg_comp.APP_len;
  3389. memcpy(params->COM_data,
  3390. fh->jpg_settings.jpg_comp.COM_data,
  3391. fh->jpg_settings.jpg_comp.COM_len);
  3392. params->COM_len = fh->jpg_settings.jpg_comp.COM_len;
  3393. params->jpeg_markers =
  3394. fh->jpg_settings.jpg_comp.jpeg_markers;
  3395. mutex_unlock(&zr->resource_lock);
  3396. return 0;
  3397. }
  3398. break;
  3399. case VIDIOC_S_JPEGCOMP:
  3400. {
  3401. struct v4l2_jpegcompression *params = arg;
  3402. int res = 0;
  3403. settings = fh->jpg_settings;
  3404. dprintk(3,
  3405. KERN_DEBUG
  3406. "%s: VIDIOC_S_JPEGCOMP - quality=%d, APPN=%d, APP_len=%d, COM_len=%d\n",
  3407. ZR_DEVNAME(zr), params->quality, params->APPn,
  3408. params->APP_len, params->COM_len);
  3409. settings.jpg_comp = *params;
  3410. mutex_lock(&zr->resource_lock);
  3411. if (fh->v4l_buffers.active != ZORAN_FREE ||
  3412. fh->jpg_buffers.active != ZORAN_FREE) {
  3413. dprintk(1,
  3414. KERN_WARNING
  3415. "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
  3416. ZR_DEVNAME(zr));
  3417. res = -EBUSY;
  3418. goto sjpegc_unlock_and_return;
  3419. }
  3420. if ((res = zoran_check_jpg_settings(zr, &settings)))
  3421. goto sjpegc_unlock_and_return;
  3422. if (!fh->jpg_buffers.allocated)
  3423. fh->jpg_buffers.buffer_size =
  3424. zoran_v4l2_calc_bufsize(&fh->jpg_settings);
  3425. fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
  3426. sjpegc_unlock_and_return:
  3427. mutex_unlock(&zr->resource_lock);
  3428. return 0;
  3429. }
  3430. break;
  3431. case VIDIOC_QUERYSTD: /* why is this useful? */
  3432. {
  3433. v4l2_std_id *std = arg;
  3434. dprintk(3,
  3435. KERN_DEBUG "%s: VIDIOC_QUERY_STD - std=0x%llx\n",
  3436. ZR_DEVNAME(zr), (unsigned long long)*std);
  3437. if (*std == V4L2_STD_ALL || *std == V4L2_STD_NTSC ||
  3438. *std == V4L2_STD_PAL || (*std == V4L2_STD_SECAM &&
  3439. zr->card.norms == 3)) {
  3440. return 0;
  3441. }
  3442. return -EINVAL;
  3443. }
  3444. break;
  3445. case VIDIOC_TRY_FMT:
  3446. {
  3447. struct v4l2_format *fmt = arg;
  3448. int res = 0;
  3449. dprintk(3, KERN_DEBUG "%s: VIDIOC_TRY_FMT - type=%d\n",
  3450. ZR_DEVNAME(zr), fmt->type);
  3451. switch (fmt->type) {
  3452. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  3453. mutex_lock(&zr->resource_lock);
  3454. if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH)
  3455. fmt->fmt.win.w.width = BUZ_MAX_WIDTH;
  3456. if (fmt->fmt.win.w.width < BUZ_MIN_WIDTH)
  3457. fmt->fmt.win.w.width = BUZ_MIN_WIDTH;
  3458. if (fmt->fmt.win.w.height > BUZ_MAX_HEIGHT)
  3459. fmt->fmt.win.w.height = BUZ_MAX_HEIGHT;
  3460. if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT)
  3461. fmt->fmt.win.w.height = BUZ_MIN_HEIGHT;
  3462. mutex_unlock(&zr->resource_lock);
  3463. break;
  3464. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  3465. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  3466. if (fmt->fmt.pix.bytesperline > 0)
  3467. return -EINVAL;
  3468. mutex_lock(&zr->resource_lock);
  3469. if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
  3470. settings = fh->jpg_settings;
  3471. /* we actually need to set 'real' parameters now */
  3472. if ((fmt->fmt.pix.height * 2) >
  3473. BUZ_MAX_HEIGHT)
  3474. settings.TmpDcm = 1;
  3475. else
  3476. settings.TmpDcm = 2;
  3477. settings.decimation = 0;
  3478. if (fmt->fmt.pix.height <=
  3479. fh->jpg_settings.img_height / 2)
  3480. settings.VerDcm = 2;
  3481. else
  3482. settings.VerDcm = 1;
  3483. if (fmt->fmt.pix.width <=
  3484. fh->jpg_settings.img_width / 4)
  3485. settings.HorDcm = 4;
  3486. else if (fmt->fmt.pix.width <=
  3487. fh->jpg_settings.img_width / 2)
  3488. settings.HorDcm = 2;
  3489. else
  3490. settings.HorDcm = 1;
  3491. if (settings.TmpDcm == 1)
  3492. settings.field_per_buff = 2;
  3493. else
  3494. settings.field_per_buff = 1;
  3495. /* check */
  3496. if ((res =
  3497. zoran_check_jpg_settings(zr,
  3498. &settings)))
  3499. goto tryfmt_unlock_and_return;
  3500. /* tell the user what we actually did */
  3501. fmt->fmt.pix.width =
  3502. settings.img_width / settings.HorDcm;
  3503. fmt->fmt.pix.height =
  3504. settings.img_height * 2 /
  3505. (settings.TmpDcm * settings.VerDcm);
  3506. if (settings.TmpDcm == 1)
  3507. fmt->fmt.pix.field =
  3508. (fh->jpg_settings.
  3509. odd_even ? V4L2_FIELD_SEQ_TB :
  3510. V4L2_FIELD_SEQ_BT);
  3511. else
  3512. fmt->fmt.pix.field =
  3513. (fh->jpg_settings.
  3514. odd_even ? V4L2_FIELD_TOP :
  3515. V4L2_FIELD_BOTTOM);
  3516. fmt->fmt.pix.sizeimage =
  3517. zoran_v4l2_calc_bufsize(&settings);
  3518. } else if (fmt->type ==
  3519. V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  3520. int i;
  3521. for (i = 0; i < NUM_FORMATS; i++)
  3522. if (zoran_formats[i].fourcc ==
  3523. fmt->fmt.pix.pixelformat)
  3524. break;
  3525. if (i == NUM_FORMATS) {
  3526. res = -EINVAL;
  3527. goto tryfmt_unlock_and_return;
  3528. }
  3529. if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
  3530. fmt->fmt.pix.width = BUZ_MAX_WIDTH;
  3531. if (fmt->fmt.pix.width < BUZ_MIN_WIDTH)
  3532. fmt->fmt.pix.width = BUZ_MIN_WIDTH;
  3533. if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
  3534. fmt->fmt.pix.height =
  3535. BUZ_MAX_HEIGHT;
  3536. if (fmt->fmt.pix.height < BUZ_MIN_HEIGHT)
  3537. fmt->fmt.pix.height =
  3538. BUZ_MIN_HEIGHT;
  3539. } else {
  3540. res = -EINVAL;
  3541. goto tryfmt_unlock_and_return;
  3542. }
  3543. tryfmt_unlock_and_return:
  3544. mutex_unlock(&zr->resource_lock);
  3545. return res;
  3546. break;
  3547. default:
  3548. return -EINVAL;
  3549. }
  3550. return 0;
  3551. }
  3552. break;
  3553. default:
  3554. dprintk(1, KERN_DEBUG "%s: UNKNOWN ioctl cmd: 0x%x\n",
  3555. ZR_DEVNAME(zr), cmd);
  3556. return -ENOIOCTLCMD;
  3557. break;
  3558. }
  3559. return 0;
  3560. }
  3561. static int
  3562. zoran_ioctl (struct inode *inode,
  3563. struct file *file,
  3564. unsigned int cmd,
  3565. unsigned long arg)
  3566. {
  3567. return video_usercopy(inode, file, cmd, arg, zoran_do_ioctl);
  3568. }
  3569. static unsigned int
  3570. zoran_poll (struct file *file,
  3571. poll_table *wait)
  3572. {
  3573. struct zoran_fh *fh = file->private_data;
  3574. struct zoran *zr = fh->zr;
  3575. int res = 0, frame;
  3576. unsigned long flags;
  3577. /* we should check whether buffers are ready to be synced on
  3578. * (w/o waits - O_NONBLOCK) here
  3579. * if ready for read (sync), return POLLIN|POLLRDNORM,
  3580. * if ready for write (sync), return POLLOUT|POLLWRNORM,
  3581. * if error, return POLLERR,
  3582. * if no buffers queued or so, return POLLNVAL
  3583. */
  3584. mutex_lock(&zr->resource_lock);
  3585. switch (fh->map_mode) {
  3586. case ZORAN_MAP_MODE_RAW:
  3587. poll_wait(file, &zr->v4l_capq, wait);
  3588. frame = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
  3589. spin_lock_irqsave(&zr->spinlock, flags);
  3590. dprintk(3,
  3591. KERN_DEBUG
  3592. "%s: %s() raw - active=%c, sync_tail=%lu/%c, pend_tail=%lu, pend_head=%lu\n",
  3593. ZR_DEVNAME(zr), __func__,
  3594. "FAL"[fh->v4l_buffers.active], zr->v4l_sync_tail,
  3595. "UPMD"[zr->v4l_buffers.buffer[frame].state],
  3596. zr->v4l_pend_tail, zr->v4l_pend_head);
  3597. /* Process is the one capturing? */
  3598. if (fh->v4l_buffers.active != ZORAN_FREE &&
  3599. /* Buffer ready to DQBUF? */
  3600. zr->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
  3601. res = POLLIN | POLLRDNORM;
  3602. spin_unlock_irqrestore(&zr->spinlock, flags);
  3603. break;
  3604. case ZORAN_MAP_MODE_JPG_REC:
  3605. case ZORAN_MAP_MODE_JPG_PLAY:
  3606. poll_wait(file, &zr->jpg_capq, wait);
  3607. frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
  3608. spin_lock_irqsave(&zr->spinlock, flags);
  3609. dprintk(3,
  3610. KERN_DEBUG
  3611. "%s: %s() jpg - active=%c, que_tail=%lu/%c, que_head=%lu, dma=%lu/%lu\n",
  3612. ZR_DEVNAME(zr), __func__,
  3613. "FAL"[fh->jpg_buffers.active], zr->jpg_que_tail,
  3614. "UPMD"[zr->jpg_buffers.buffer[frame].state],
  3615. zr->jpg_que_head, zr->jpg_dma_tail, zr->jpg_dma_head);
  3616. if (fh->jpg_buffers.active != ZORAN_FREE &&
  3617. zr->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
  3618. if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
  3619. res = POLLIN | POLLRDNORM;
  3620. else
  3621. res = POLLOUT | POLLWRNORM;
  3622. }
  3623. spin_unlock_irqrestore(&zr->spinlock, flags);
  3624. break;
  3625. default:
  3626. dprintk(1,
  3627. KERN_ERR
  3628. "%s: zoran_poll() - internal error, unknown map_mode=%d\n",
  3629. ZR_DEVNAME(zr), fh->map_mode);
  3630. res = POLLNVAL;
  3631. }
  3632. mutex_unlock(&zr->resource_lock);
  3633. return res;
  3634. }
  3635. /*
  3636. * This maps the buffers to user space.
  3637. *
  3638. * Depending on the state of fh->map_mode
  3639. * the V4L or the MJPEG buffers are mapped
  3640. * per buffer or all together
  3641. *
  3642. * Note that we need to connect to some
  3643. * unmap signal event to unmap the de-allocate
  3644. * the buffer accordingly (zoran_vm_close())
  3645. */
  3646. static void
  3647. zoran_vm_open (struct vm_area_struct *vma)
  3648. {
  3649. struct zoran_mapping *map = vma->vm_private_data;
  3650. map->count++;
  3651. }
  3652. static void
  3653. zoran_vm_close (struct vm_area_struct *vma)
  3654. {
  3655. struct zoran_mapping *map = vma->vm_private_data;
  3656. struct file *file = map->file;
  3657. struct zoran_fh *fh = file->private_data;
  3658. struct zoran *zr = fh->zr;
  3659. int i;
  3660. map->count--;
  3661. if (map->count == 0) {
  3662. switch (fh->map_mode) {
  3663. case ZORAN_MAP_MODE_JPG_REC:
  3664. case ZORAN_MAP_MODE_JPG_PLAY:
  3665. dprintk(3, KERN_INFO "%s: munmap(MJPEG)\n",
  3666. ZR_DEVNAME(zr));
  3667. for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
  3668. if (fh->jpg_buffers.buffer[i].map == map) {
  3669. fh->jpg_buffers.buffer[i].map =
  3670. NULL;
  3671. }
  3672. }
  3673. kfree(map);
  3674. for (i = 0; i < fh->jpg_buffers.num_buffers; i++)
  3675. if (fh->jpg_buffers.buffer[i].map)
  3676. break;
  3677. if (i == fh->jpg_buffers.num_buffers) {
  3678. mutex_lock(&zr->resource_lock);
  3679. if (fh->jpg_buffers.active != ZORAN_FREE) {
  3680. jpg_qbuf(file, -1, zr->codec_mode);
  3681. zr->jpg_buffers.allocated = 0;
  3682. zr->jpg_buffers.active =
  3683. fh->jpg_buffers.active =
  3684. ZORAN_FREE;
  3685. }
  3686. //jpg_fbuffer_free(file);
  3687. fh->jpg_buffers.allocated = 0;
  3688. fh->jpg_buffers.ready_to_be_freed = 1;
  3689. mutex_unlock(&zr->resource_lock);
  3690. }
  3691. break;
  3692. case ZORAN_MAP_MODE_RAW:
  3693. dprintk(3, KERN_INFO "%s: munmap(V4L)\n",
  3694. ZR_DEVNAME(zr));
  3695. for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
  3696. if (fh->v4l_buffers.buffer[i].map == map) {
  3697. /* unqueue/unmap */
  3698. fh->v4l_buffers.buffer[i].map =
  3699. NULL;
  3700. }
  3701. }
  3702. kfree(map);
  3703. for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
  3704. if (fh->v4l_buffers.buffer[i].map)
  3705. break;
  3706. if (i == fh->v4l_buffers.num_buffers) {
  3707. mutex_lock(&zr->resource_lock);
  3708. if (fh->v4l_buffers.active != ZORAN_FREE) {
  3709. unsigned long flags;
  3710. spin_lock_irqsave(&zr->spinlock, flags);
  3711. zr36057_set_memgrab(zr, 0);
  3712. zr->v4l_buffers.allocated = 0;
  3713. zr->v4l_buffers.active =
  3714. fh->v4l_buffers.active =
  3715. ZORAN_FREE;
  3716. spin_unlock_irqrestore(&zr->spinlock, flags);
  3717. }
  3718. //v4l_fbuffer_free(file);
  3719. fh->v4l_buffers.allocated = 0;
  3720. fh->v4l_buffers.ready_to_be_freed = 1;
  3721. mutex_unlock(&zr->resource_lock);
  3722. }
  3723. break;
  3724. default:
  3725. printk(KERN_ERR
  3726. "%s: munmap() - internal error - unknown map mode %d\n",
  3727. ZR_DEVNAME(zr), fh->map_mode);
  3728. break;
  3729. }
  3730. }
  3731. }
  3732. static struct vm_operations_struct zoran_vm_ops = {
  3733. .open = zoran_vm_open,
  3734. .close = zoran_vm_close,
  3735. };
  3736. static int
  3737. zoran_mmap (struct file *file,
  3738. struct vm_area_struct *vma)
  3739. {
  3740. struct zoran_fh *fh = file->private_data;
  3741. struct zoran *zr = fh->zr;
  3742. unsigned long size = (vma->vm_end - vma->vm_start);
  3743. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  3744. int i, j;
  3745. unsigned long page, start = vma->vm_start, todo, pos, fraglen;
  3746. int first, last;
  3747. struct zoran_mapping *map;
  3748. int res = 0;
  3749. dprintk(3,
  3750. KERN_INFO "%s: mmap(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
  3751. ZR_DEVNAME(zr),
  3752. fh->map_mode == ZORAN_MAP_MODE_RAW ? "V4L" : "MJPEG",
  3753. vma->vm_start, vma->vm_end, size);
  3754. if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
  3755. !(vma->vm_flags & VM_WRITE)) {
  3756. dprintk(1,
  3757. KERN_ERR
  3758. "%s: mmap() - no MAP_SHARED/PROT_{READ,WRITE} given\n",
  3759. ZR_DEVNAME(zr));
  3760. return -EINVAL;
  3761. }
  3762. switch (fh->map_mode) {
  3763. case ZORAN_MAP_MODE_JPG_REC:
  3764. case ZORAN_MAP_MODE_JPG_PLAY:
  3765. /* lock */
  3766. mutex_lock(&zr->resource_lock);
  3767. /* Map the MJPEG buffers */
  3768. if (!fh->jpg_buffers.allocated) {
  3769. dprintk(1,
  3770. KERN_ERR
  3771. "%s: zoran_mmap(MJPEG) - buffers not yet allocated\n",
  3772. ZR_DEVNAME(zr));
  3773. res = -ENOMEM;
  3774. goto jpg_mmap_unlock_and_return;
  3775. }
  3776. first = offset / fh->jpg_buffers.buffer_size;
  3777. last = first - 1 + size / fh->jpg_buffers.buffer_size;
  3778. if (offset % fh->jpg_buffers.buffer_size != 0 ||
  3779. size % fh->jpg_buffers.buffer_size != 0 || first < 0 ||
  3780. last < 0 || first >= fh->jpg_buffers.num_buffers ||
  3781. last >= fh->jpg_buffers.num_buffers) {
  3782. dprintk(1,
  3783. KERN_ERR
  3784. "%s: mmap(MJPEG) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
  3785. ZR_DEVNAME(zr), offset, size,
  3786. fh->jpg_buffers.buffer_size,
  3787. fh->jpg_buffers.num_buffers);
  3788. res = -EINVAL;
  3789. goto jpg_mmap_unlock_and_return;
  3790. }
  3791. for (i = first; i <= last; i++) {
  3792. if (fh->jpg_buffers.buffer[i].map) {
  3793. dprintk(1,
  3794. KERN_ERR
  3795. "%s: mmap(MJPEG) - buffer %d already mapped\n",
  3796. ZR_DEVNAME(zr), i);
  3797. res = -EBUSY;
  3798. goto jpg_mmap_unlock_and_return;
  3799. }
  3800. }
  3801. /* map these buffers (v4l_buffers[i]) */
  3802. map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
  3803. if (!map) {
  3804. res = -ENOMEM;
  3805. goto jpg_mmap_unlock_and_return;
  3806. }
  3807. map->file = file;
  3808. map->count = 1;
  3809. vma->vm_ops = &zoran_vm_ops;
  3810. vma->vm_flags |= VM_DONTEXPAND;
  3811. vma->vm_private_data = map;
  3812. for (i = first; i <= last; i++) {
  3813. for (j = 0;
  3814. j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
  3815. j++) {
  3816. fraglen =
  3817. (le32_to_cpu(fh->jpg_buffers.buffer[i].
  3818. frag_tab[2 * j + 1]) & ~1) << 1;
  3819. todo = size;
  3820. if (todo > fraglen)
  3821. todo = fraglen;
  3822. pos =
  3823. le32_to_cpu(fh->jpg_buffers.
  3824. buffer[i].frag_tab[2 * j]);
  3825. /* should just be pos on i386 */
  3826. page = virt_to_phys(bus_to_virt(pos))
  3827. >> PAGE_SHIFT;
  3828. if (remap_pfn_range(vma, start, page,
  3829. todo, PAGE_SHARED)) {
  3830. dprintk(1,
  3831. KERN_ERR
  3832. "%s: zoran_mmap(V4L) - remap_pfn_range failed\n",
  3833. ZR_DEVNAME(zr));
  3834. res = -EAGAIN;
  3835. goto jpg_mmap_unlock_and_return;
  3836. }
  3837. size -= todo;
  3838. start += todo;
  3839. if (size == 0)
  3840. break;
  3841. if (le32_to_cpu(fh->jpg_buffers.buffer[i].
  3842. frag_tab[2 * j + 1]) & 1)
  3843. break; /* was last fragment */
  3844. }
  3845. fh->jpg_buffers.buffer[i].map = map;
  3846. if (size == 0)
  3847. break;
  3848. }
  3849. jpg_mmap_unlock_and_return:
  3850. mutex_unlock(&zr->resource_lock);
  3851. break;
  3852. case ZORAN_MAP_MODE_RAW:
  3853. mutex_lock(&zr->resource_lock);
  3854. /* Map the V4L buffers */
  3855. if (!fh->v4l_buffers.allocated) {
  3856. dprintk(1,
  3857. KERN_ERR
  3858. "%s: zoran_mmap(V4L) - buffers not yet allocated\n",
  3859. ZR_DEVNAME(zr));
  3860. res = -ENOMEM;
  3861. goto v4l_mmap_unlock_and_return;
  3862. }
  3863. first = offset / fh->v4l_buffers.buffer_size;
  3864. last = first - 1 + size / fh->v4l_buffers.buffer_size;
  3865. if (offset % fh->v4l_buffers.buffer_size != 0 ||
  3866. size % fh->v4l_buffers.buffer_size != 0 || first < 0 ||
  3867. last < 0 || first >= fh->v4l_buffers.num_buffers ||
  3868. last >= fh->v4l_buffers.buffer_size) {
  3869. dprintk(1,
  3870. KERN_ERR
  3871. "%s: mmap(V4L) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
  3872. ZR_DEVNAME(zr), offset, size,
  3873. fh->v4l_buffers.buffer_size,
  3874. fh->v4l_buffers.num_buffers);
  3875. res = -EINVAL;
  3876. goto v4l_mmap_unlock_and_return;
  3877. }
  3878. for (i = first; i <= last; i++) {
  3879. if (fh->v4l_buffers.buffer[i].map) {
  3880. dprintk(1,
  3881. KERN_ERR
  3882. "%s: mmap(V4L) - buffer %d already mapped\n",
  3883. ZR_DEVNAME(zr), i);
  3884. res = -EBUSY;
  3885. goto v4l_mmap_unlock_and_return;
  3886. }
  3887. }
  3888. /* map these buffers (v4l_buffers[i]) */
  3889. map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
  3890. if (!map) {
  3891. res = -ENOMEM;
  3892. goto v4l_mmap_unlock_and_return;
  3893. }
  3894. map->file = file;
  3895. map->count = 1;
  3896. vma->vm_ops = &zoran_vm_ops;
  3897. vma->vm_flags |= VM_DONTEXPAND;
  3898. vma->vm_private_data = map;
  3899. for (i = first; i <= last; i++) {
  3900. todo = size;
  3901. if (todo > fh->v4l_buffers.buffer_size)
  3902. todo = fh->v4l_buffers.buffer_size;
  3903. page = fh->v4l_buffers.buffer[i].fbuffer_phys;
  3904. if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
  3905. todo, PAGE_SHARED)) {
  3906. dprintk(1,
  3907. KERN_ERR
  3908. "%s: zoran_mmap(V4L)i - remap_pfn_range failed\n",
  3909. ZR_DEVNAME(zr));
  3910. res = -EAGAIN;
  3911. goto v4l_mmap_unlock_and_return;
  3912. }
  3913. size -= todo;
  3914. start += todo;
  3915. fh->v4l_buffers.buffer[i].map = map;
  3916. if (size == 0)
  3917. break;
  3918. }
  3919. v4l_mmap_unlock_and_return:
  3920. mutex_unlock(&zr->resource_lock);
  3921. break;
  3922. default:
  3923. dprintk(1,
  3924. KERN_ERR
  3925. "%s: zoran_mmap() - internal error - unknown map mode %d\n",
  3926. ZR_DEVNAME(zr), fh->map_mode);
  3927. break;
  3928. }
  3929. return 0;
  3930. }
  3931. static const struct file_operations zoran_fops = {
  3932. .owner = THIS_MODULE,
  3933. .open = zoran_open,
  3934. .release = zoran_close,
  3935. .ioctl = zoran_ioctl,
  3936. #ifdef CONFIG_COMPAT
  3937. .compat_ioctl = v4l_compat_ioctl32,
  3938. #endif
  3939. .llseek = no_llseek,
  3940. .read = zoran_read,
  3941. .write = zoran_write,
  3942. .mmap = zoran_mmap,
  3943. .poll = zoran_poll,
  3944. };
  3945. struct video_device zoran_template __devinitdata = {
  3946. .name = ZORAN_NAME,
  3947. .fops = &zoran_fops,
  3948. .release = &zoran_vdev_release,
  3949. .minor = -1
  3950. };