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