zoran_driver.c 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392
  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/smp_lock.h>
  52. #include <linux/pci.h>
  53. #include <linux/vmalloc.h>
  54. #include <linux/wait.h>
  55. #include <linux/interrupt.h>
  56. #include <linux/i2c.h>
  57. #include <linux/i2c-algo-bit.h>
  58. #include <linux/spinlock.h>
  59. #include <linux/videodev.h>
  60. #include <media/v4l2-common.h>
  61. #include <media/v4l2-ioctl.h>
  62. #include "videocodec.h"
  63. #include <asm/byteorder.h>
  64. #include <asm/io.h>
  65. #include <asm/uaccess.h>
  66. #include <linux/proc_fs.h>
  67. #include <linux/mutex.h>
  68. #include "zoran.h"
  69. #include "zoran_device.h"
  70. #include "zoran_card.h"
  71. const struct zoran_format zoran_formats[] = {
  72. {
  73. .name = "15-bit RGB LE",
  74. .fourcc = V4L2_PIX_FMT_RGB555,
  75. .colorspace = V4L2_COLORSPACE_SRGB,
  76. .depth = 15,
  77. .flags = ZORAN_FORMAT_CAPTURE |
  78. ZORAN_FORMAT_OVERLAY,
  79. .vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif|
  80. ZR36057_VFESPFR_LittleEndian,
  81. }, {
  82. .name = "15-bit RGB BE",
  83. .fourcc = V4L2_PIX_FMT_RGB555X,
  84. .colorspace = V4L2_COLORSPACE_SRGB,
  85. .depth = 15,
  86. .flags = ZORAN_FORMAT_CAPTURE |
  87. ZORAN_FORMAT_OVERLAY,
  88. .vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif,
  89. }, {
  90. .name = "16-bit RGB LE",
  91. .fourcc = V4L2_PIX_FMT_RGB565,
  92. .colorspace = V4L2_COLORSPACE_SRGB,
  93. .depth = 16,
  94. .flags = ZORAN_FORMAT_CAPTURE |
  95. ZORAN_FORMAT_OVERLAY,
  96. .vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif|
  97. ZR36057_VFESPFR_LittleEndian,
  98. }, {
  99. .name = "16-bit RGB BE",
  100. .fourcc = V4L2_PIX_FMT_RGB565X,
  101. .colorspace = V4L2_COLORSPACE_SRGB,
  102. .depth = 16,
  103. .flags = ZORAN_FORMAT_CAPTURE |
  104. ZORAN_FORMAT_OVERLAY,
  105. .vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif,
  106. }, {
  107. .name = "24-bit RGB",
  108. .fourcc = V4L2_PIX_FMT_BGR24,
  109. .colorspace = V4L2_COLORSPACE_SRGB,
  110. .depth = 24,
  111. .flags = ZORAN_FORMAT_CAPTURE |
  112. ZORAN_FORMAT_OVERLAY,
  113. .vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_Pack24,
  114. }, {
  115. .name = "32-bit RGB LE",
  116. .fourcc = V4L2_PIX_FMT_BGR32,
  117. .colorspace = V4L2_COLORSPACE_SRGB,
  118. .depth = 32,
  119. .flags = ZORAN_FORMAT_CAPTURE |
  120. ZORAN_FORMAT_OVERLAY,
  121. .vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_LittleEndian,
  122. }, {
  123. .name = "32-bit RGB BE",
  124. .fourcc = V4L2_PIX_FMT_RGB32,
  125. .colorspace = V4L2_COLORSPACE_SRGB,
  126. .depth = 32,
  127. .flags = ZORAN_FORMAT_CAPTURE |
  128. ZORAN_FORMAT_OVERLAY,
  129. .vfespfr = ZR36057_VFESPFR_RGB888,
  130. }, {
  131. .name = "4:2:2, packed, YUYV",
  132. .fourcc = V4L2_PIX_FMT_YUYV,
  133. .colorspace = V4L2_COLORSPACE_SMPTE170M,
  134. .depth = 16,
  135. .flags = ZORAN_FORMAT_CAPTURE |
  136. ZORAN_FORMAT_OVERLAY,
  137. .vfespfr = ZR36057_VFESPFR_YUV422,
  138. }, {
  139. .name = "4:2:2, packed, UYVY",
  140. .fourcc = V4L2_PIX_FMT_UYVY,
  141. .colorspace = V4L2_COLORSPACE_SMPTE170M,
  142. .depth = 16,
  143. .flags = ZORAN_FORMAT_CAPTURE |
  144. ZORAN_FORMAT_OVERLAY,
  145. .vfespfr = ZR36057_VFESPFR_YUV422|ZR36057_VFESPFR_LittleEndian,
  146. }, {
  147. .name = "Hardware-encoded Motion-JPEG",
  148. .fourcc = V4L2_PIX_FMT_MJPEG,
  149. .colorspace = V4L2_COLORSPACE_SMPTE170M,
  150. .depth = 0,
  151. .flags = ZORAN_FORMAT_CAPTURE |
  152. ZORAN_FORMAT_PLAYBACK |
  153. ZORAN_FORMAT_COMPRESSED,
  154. }
  155. };
  156. #define NUM_FORMATS ARRAY_SIZE(zoran_formats)
  157. /* small helper function for calculating buffersizes for v4l2
  158. * we calculate the nearest higher power-of-two, which
  159. * will be the recommended buffersize */
  160. static __u32
  161. zoran_v4l2_calc_bufsize (struct zoran_jpg_settings *settings)
  162. {
  163. __u8 div = settings->VerDcm * settings->HorDcm * settings->TmpDcm;
  164. __u32 num = (1024 * 512) / (div);
  165. __u32 result = 2;
  166. num--;
  167. while (num) {
  168. num >>= 1;
  169. result <<= 1;
  170. }
  171. if (result > jpg_bufsize)
  172. return jpg_bufsize;
  173. if (result < 8192)
  174. return 8192;
  175. return result;
  176. }
  177. /* forward references */
  178. static void v4l_fbuffer_free(struct zoran_fh *fh);
  179. static void jpg_fbuffer_free(struct zoran_fh *fh);
  180. /* Set mapping mode */
  181. static void map_mode_raw(struct zoran_fh *fh)
  182. {
  183. fh->map_mode = ZORAN_MAP_MODE_RAW;
  184. fh->buffers.buffer_size = v4l_bufsize;
  185. fh->buffers.num_buffers = v4l_nbufs;
  186. }
  187. static void map_mode_jpg(struct zoran_fh *fh, int play)
  188. {
  189. fh->map_mode = play ? ZORAN_MAP_MODE_JPG_PLAY : ZORAN_MAP_MODE_JPG_REC;
  190. fh->buffers.buffer_size = jpg_bufsize;
  191. fh->buffers.num_buffers = jpg_nbufs;
  192. }
  193. static inline const char *mode_name(enum zoran_map_mode mode)
  194. {
  195. return mode == ZORAN_MAP_MODE_RAW ? "V4L" : "JPG";
  196. }
  197. /*
  198. * Allocate the V4L grab buffers
  199. *
  200. * These have to be pysically contiguous.
  201. */
  202. static int v4l_fbuffer_alloc(struct zoran_fh *fh)
  203. {
  204. struct zoran *zr = fh->zr;
  205. int i, off;
  206. unsigned char *mem;
  207. for (i = 0; i < fh->buffers.num_buffers; i++) {
  208. if (fh->buffers.buffer[i].v4l.fbuffer)
  209. dprintk(2,
  210. KERN_WARNING
  211. "%s: %s - buffer %d already allocated!?\n",
  212. ZR_DEVNAME(zr), __func__, i);
  213. //udelay(20);
  214. mem = kmalloc(fh->buffers.buffer_size,
  215. GFP_KERNEL | __GFP_NOWARN);
  216. if (!mem) {
  217. dprintk(1,
  218. KERN_ERR
  219. "%s: %s - kmalloc for V4L buf %d failed\n",
  220. ZR_DEVNAME(zr), __func__, i);
  221. v4l_fbuffer_free(fh);
  222. return -ENOBUFS;
  223. }
  224. fh->buffers.buffer[i].v4l.fbuffer = mem;
  225. fh->buffers.buffer[i].v4l.fbuffer_phys = virt_to_phys(mem);
  226. fh->buffers.buffer[i].v4l.fbuffer_bus = virt_to_bus(mem);
  227. for (off = 0; off < fh->buffers.buffer_size;
  228. off += PAGE_SIZE)
  229. SetPageReserved(virt_to_page(mem + off));
  230. dprintk(4,
  231. KERN_INFO
  232. "%s: %s - V4L frame %d mem 0x%lx (bus: 0x%llx)\n",
  233. ZR_DEVNAME(zr), __func__, i, (unsigned long) mem,
  234. (unsigned long long)virt_to_bus(mem));
  235. }
  236. fh->buffers.allocated = 1;
  237. return 0;
  238. }
  239. /* free the V4L grab buffers */
  240. static void v4l_fbuffer_free(struct zoran_fh *fh)
  241. {
  242. struct zoran *zr = fh->zr;
  243. int i, off;
  244. unsigned char *mem;
  245. dprintk(4, KERN_INFO "%s: %s\n", ZR_DEVNAME(zr), __func__);
  246. for (i = 0; i < fh->buffers.num_buffers; i++) {
  247. if (!fh->buffers.buffer[i].v4l.fbuffer)
  248. continue;
  249. mem = fh->buffers.buffer[i].v4l.fbuffer;
  250. for (off = 0; off < fh->buffers.buffer_size;
  251. off += PAGE_SIZE)
  252. ClearPageReserved(virt_to_page(mem + off));
  253. kfree(fh->buffers.buffer[i].v4l.fbuffer);
  254. fh->buffers.buffer[i].v4l.fbuffer = NULL;
  255. }
  256. fh->buffers.allocated = 0;
  257. }
  258. /*
  259. * Allocate the MJPEG grab buffers.
  260. *
  261. * If a Natoma chipset is present and this is a revision 1 zr36057,
  262. * each MJPEG buffer needs to be physically contiguous.
  263. * (RJ: This statement is from Dave Perks' original driver,
  264. * I could never check it because I have a zr36067)
  265. *
  266. * RJ: The contents grab buffers needs never be accessed in the driver.
  267. * Therefore there is no need to allocate them with vmalloc in order
  268. * to get a contiguous virtual memory space.
  269. * I don't understand why many other drivers first allocate them with
  270. * vmalloc (which uses internally also get_zeroed_page, but delivers you
  271. * virtual addresses) and then again have to make a lot of efforts
  272. * to get the physical address.
  273. *
  274. * Ben Capper:
  275. * On big-endian architectures (such as ppc) some extra steps
  276. * are needed. When reading and writing to the stat_com array
  277. * and fragment buffers, the device expects to see little-
  278. * endian values. The use of cpu_to_le32() and le32_to_cpu()
  279. * in this function (and one or two others in zoran_device.c)
  280. * ensure that these values are always stored in little-endian
  281. * form, regardless of architecture. The zr36057 does Very Bad
  282. * Things on big endian architectures if the stat_com array
  283. * and fragment buffers are not little-endian.
  284. */
  285. static int jpg_fbuffer_alloc(struct zoran_fh *fh)
  286. {
  287. struct zoran *zr = fh->zr;
  288. int i, j, off;
  289. u8 *mem;
  290. for (i = 0; i < fh->buffers.num_buffers; i++) {
  291. if (fh->buffers.buffer[i].jpg.frag_tab)
  292. dprintk(2,
  293. KERN_WARNING
  294. "%s: %s - buffer %d already allocated!?\n",
  295. ZR_DEVNAME(zr), __func__, i);
  296. /* Allocate fragment table for this buffer */
  297. mem = (void *)get_zeroed_page(GFP_KERNEL);
  298. if (mem == 0) {
  299. dprintk(1,
  300. KERN_ERR
  301. "%s: %s - get_zeroed_page (frag_tab) failed for buffer %d\n",
  302. ZR_DEVNAME(zr), __func__, i);
  303. jpg_fbuffer_free(fh);
  304. return -ENOBUFS;
  305. }
  306. fh->buffers.buffer[i].jpg.frag_tab = (__le32 *)mem;
  307. fh->buffers.buffer[i].jpg.frag_tab_bus = virt_to_bus(mem);
  308. if (fh->buffers.need_contiguous) {
  309. mem = kmalloc(fh->buffers.buffer_size, GFP_KERNEL);
  310. if (mem == NULL) {
  311. dprintk(1,
  312. KERN_ERR
  313. "%s: %s - kmalloc failed for buffer %d\n",
  314. ZR_DEVNAME(zr), __func__, i);
  315. jpg_fbuffer_free(fh);
  316. return -ENOBUFS;
  317. }
  318. fh->buffers.buffer[i].jpg.frag_tab[0] =
  319. cpu_to_le32(virt_to_bus(mem));
  320. fh->buffers.buffer[i].jpg.frag_tab[1] =
  321. cpu_to_le32((fh->buffers.buffer_size >> 1) | 1);
  322. for (off = 0; off < fh->buffers.buffer_size; off += PAGE_SIZE)
  323. SetPageReserved(virt_to_page(mem + off));
  324. } else {
  325. /* jpg_bufsize is already page aligned */
  326. for (j = 0; j < fh->buffers.buffer_size / PAGE_SIZE; j++) {
  327. mem = (void *)get_zeroed_page(GFP_KERNEL);
  328. if (mem == NULL) {
  329. dprintk(1,
  330. KERN_ERR
  331. "%s: %s - get_zeroed_page failed for buffer %d\n",
  332. ZR_DEVNAME(zr), __func__, i);
  333. jpg_fbuffer_free(fh);
  334. return -ENOBUFS;
  335. }
  336. fh->buffers.buffer[i].jpg.frag_tab[2 * j] =
  337. cpu_to_le32(virt_to_bus(mem));
  338. fh->buffers.buffer[i].jpg.frag_tab[2 * j + 1] =
  339. cpu_to_le32((PAGE_SIZE >> 2) << 1);
  340. SetPageReserved(virt_to_page(mem));
  341. }
  342. fh->buffers.buffer[i].jpg.frag_tab[2 * j - 1] |= cpu_to_le32(1);
  343. }
  344. }
  345. dprintk(4,
  346. KERN_DEBUG "%s: %s - %d KB allocated\n",
  347. ZR_DEVNAME(zr), __func__,
  348. (fh->buffers.num_buffers * fh->buffers.buffer_size) >> 10);
  349. fh->buffers.allocated = 1;
  350. return 0;
  351. }
  352. /* free the MJPEG grab buffers */
  353. static void jpg_fbuffer_free(struct zoran_fh *fh)
  354. {
  355. struct zoran *zr = fh->zr;
  356. int i, j, off;
  357. unsigned char *mem;
  358. __le32 frag_tab;
  359. struct zoran_buffer *buffer;
  360. dprintk(4, KERN_DEBUG "%s: %s\n", ZR_DEVNAME(zr), __func__);
  361. for (i = 0, buffer = &fh->buffers.buffer[0];
  362. i < fh->buffers.num_buffers; i++, buffer++) {
  363. if (!buffer->jpg.frag_tab)
  364. continue;
  365. if (fh->buffers.need_contiguous) {
  366. frag_tab = buffer->jpg.frag_tab[0];
  367. if (frag_tab) {
  368. mem = bus_to_virt(le32_to_cpu(frag_tab));
  369. for (off = 0; off < fh->buffers.buffer_size; off += PAGE_SIZE)
  370. ClearPageReserved(virt_to_page(mem + off));
  371. kfree(mem);
  372. buffer->jpg.frag_tab[0] = 0;
  373. buffer->jpg.frag_tab[1] = 0;
  374. }
  375. } else {
  376. for (j = 0; j < fh->buffers.buffer_size / PAGE_SIZE; j++) {
  377. frag_tab = buffer->jpg.frag_tab[2 * j];
  378. if (!frag_tab)
  379. break;
  380. ClearPageReserved(virt_to_page(bus_to_virt(le32_to_cpu(frag_tab))));
  381. free_page((unsigned long)bus_to_virt(le32_to_cpu(frag_tab)));
  382. buffer->jpg.frag_tab[2 * j] = 0;
  383. buffer->jpg.frag_tab[2 * j + 1] = 0;
  384. }
  385. }
  386. free_page((unsigned long)buffer->jpg.frag_tab);
  387. buffer->jpg.frag_tab = NULL;
  388. }
  389. fh->buffers.allocated = 0;
  390. }
  391. /*
  392. * V4L Buffer grabbing
  393. */
  394. static int
  395. zoran_v4l_set_format (struct zoran_fh *fh,
  396. int width,
  397. int height,
  398. const struct zoran_format *format)
  399. {
  400. struct zoran *zr = fh->zr;
  401. int bpp;
  402. /* Check size and format of the grab wanted */
  403. if (height < BUZ_MIN_HEIGHT || width < BUZ_MIN_WIDTH ||
  404. height > BUZ_MAX_HEIGHT || width > BUZ_MAX_WIDTH) {
  405. dprintk(1,
  406. KERN_ERR
  407. "%s: %s - wrong frame size (%dx%d)\n",
  408. ZR_DEVNAME(zr), __func__, width, height);
  409. return -EINVAL;
  410. }
  411. bpp = (format->depth + 7) / 8;
  412. /* Check against available buffer size */
  413. if (height * width * bpp > fh->buffers.buffer_size) {
  414. dprintk(1,
  415. KERN_ERR
  416. "%s: %s - video buffer size (%d kB) is too small\n",
  417. ZR_DEVNAME(zr), __func__, fh->buffers.buffer_size >> 10);
  418. return -EINVAL;
  419. }
  420. /* The video front end needs 4-byte alinged line sizes */
  421. if ((bpp == 2 && (width & 1)) || (bpp == 3 && (width & 3))) {
  422. dprintk(1,
  423. KERN_ERR
  424. "%s: %s - wrong frame alignment\n",
  425. ZR_DEVNAME(zr), __func__);
  426. return -EINVAL;
  427. }
  428. fh->v4l_settings.width = width;
  429. fh->v4l_settings.height = height;
  430. fh->v4l_settings.format = format;
  431. fh->v4l_settings.bytesperline = bpp * fh->v4l_settings.width;
  432. return 0;
  433. }
  434. static int zoran_v4l_queue_frame(struct zoran_fh *fh, int num)
  435. {
  436. struct zoran *zr = fh->zr;
  437. unsigned long flags;
  438. int res = 0;
  439. if (!fh->buffers.allocated) {
  440. dprintk(1,
  441. KERN_ERR
  442. "%s: %s - buffers not yet allocated\n",
  443. ZR_DEVNAME(zr), __func__);
  444. res = -ENOMEM;
  445. }
  446. /* No grabbing outside the buffer range! */
  447. if (num >= fh->buffers.num_buffers || num < 0) {
  448. dprintk(1,
  449. KERN_ERR
  450. "%s: %s - buffer %d is out of range\n",
  451. ZR_DEVNAME(zr), __func__, num);
  452. res = -EINVAL;
  453. }
  454. spin_lock_irqsave(&zr->spinlock, flags);
  455. if (fh->buffers.active == ZORAN_FREE) {
  456. if (zr->v4l_buffers.active == ZORAN_FREE) {
  457. zr->v4l_buffers = fh->buffers;
  458. fh->buffers.active = ZORAN_ACTIVE;
  459. } else {
  460. dprintk(1,
  461. KERN_ERR
  462. "%s: %s - another session is already capturing\n",
  463. ZR_DEVNAME(zr), __func__);
  464. res = -EBUSY;
  465. }
  466. }
  467. /* make sure a grab isn't going on currently with this buffer */
  468. if (!res) {
  469. switch (zr->v4l_buffers.buffer[num].state) {
  470. default:
  471. case BUZ_STATE_PEND:
  472. if (zr->v4l_buffers.active == ZORAN_FREE) {
  473. fh->buffers.active = ZORAN_FREE;
  474. zr->v4l_buffers.allocated = 0;
  475. }
  476. res = -EBUSY; /* what are you doing? */
  477. break;
  478. case BUZ_STATE_DONE:
  479. dprintk(2,
  480. KERN_WARNING
  481. "%s: %s - queueing buffer %d in state DONE!?\n",
  482. ZR_DEVNAME(zr), __func__, num);
  483. case BUZ_STATE_USER:
  484. /* since there is at least one unused buffer there's room for at least
  485. * one more pend[] entry */
  486. zr->v4l_pend[zr->v4l_pend_head++ & V4L_MASK_FRAME] = num;
  487. zr->v4l_buffers.buffer[num].state = BUZ_STATE_PEND;
  488. zr->v4l_buffers.buffer[num].bs.length =
  489. fh->v4l_settings.bytesperline *
  490. zr->v4l_settings.height;
  491. fh->buffers.buffer[num] = zr->v4l_buffers.buffer[num];
  492. break;
  493. }
  494. }
  495. spin_unlock_irqrestore(&zr->spinlock, flags);
  496. if (!res && zr->v4l_buffers.active == ZORAN_FREE)
  497. zr->v4l_buffers.active = fh->buffers.active;
  498. return res;
  499. }
  500. /*
  501. * Sync on a V4L buffer
  502. */
  503. static int v4l_sync(struct zoran_fh *fh, int frame)
  504. {
  505. struct zoran *zr = fh->zr;
  506. unsigned long flags;
  507. if (fh->buffers.active == ZORAN_FREE) {
  508. dprintk(1,
  509. KERN_ERR
  510. "%s: %s - no grab active for this session\n",
  511. ZR_DEVNAME(zr), __func__);
  512. return -EINVAL;
  513. }
  514. /* check passed-in frame number */
  515. if (frame >= fh->buffers.num_buffers || frame < 0) {
  516. dprintk(1,
  517. KERN_ERR "%s: %s - frame %d is invalid\n",
  518. ZR_DEVNAME(zr), __func__, frame);
  519. return -EINVAL;
  520. }
  521. /* Check if is buffer was queued at all */
  522. if (zr->v4l_buffers.buffer[frame].state == BUZ_STATE_USER) {
  523. dprintk(1,
  524. KERN_ERR
  525. "%s: %s - attempt to sync on a buffer which was not queued?\n",
  526. ZR_DEVNAME(zr), __func__);
  527. return -EPROTO;
  528. }
  529. /* wait on this buffer to get ready */
  530. if (!wait_event_interruptible_timeout(zr->v4l_capq,
  531. (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_PEND), 10*HZ))
  532. return -ETIME;
  533. if (signal_pending(current))
  534. return -ERESTARTSYS;
  535. /* buffer should now be in BUZ_STATE_DONE */
  536. if (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_DONE)
  537. dprintk(2,
  538. KERN_ERR "%s: %s - internal state error\n",
  539. ZR_DEVNAME(zr), __func__);
  540. zr->v4l_buffers.buffer[frame].state = BUZ_STATE_USER;
  541. fh->buffers.buffer[frame] = zr->v4l_buffers.buffer[frame];
  542. spin_lock_irqsave(&zr->spinlock, flags);
  543. /* Check if streaming capture has finished */
  544. if (zr->v4l_pend_tail == zr->v4l_pend_head) {
  545. zr36057_set_memgrab(zr, 0);
  546. if (zr->v4l_buffers.active == ZORAN_ACTIVE) {
  547. fh->buffers.active = zr->v4l_buffers.active = ZORAN_FREE;
  548. zr->v4l_buffers.allocated = 0;
  549. }
  550. }
  551. spin_unlock_irqrestore(&zr->spinlock, flags);
  552. return 0;
  553. }
  554. /*
  555. * Queue a MJPEG buffer for capture/playback
  556. */
  557. static int zoran_jpg_queue_frame(struct zoran_fh *fh, int num,
  558. enum zoran_codec_mode mode)
  559. {
  560. struct zoran *zr = fh->zr;
  561. unsigned long flags;
  562. int res = 0;
  563. /* Check if buffers are allocated */
  564. if (!fh->buffers.allocated) {
  565. dprintk(1,
  566. KERN_ERR
  567. "%s: %s - buffers not yet allocated\n",
  568. ZR_DEVNAME(zr), __func__);
  569. return -ENOMEM;
  570. }
  571. /* No grabbing outside the buffer range! */
  572. if (num >= fh->buffers.num_buffers || num < 0) {
  573. dprintk(1,
  574. KERN_ERR
  575. "%s: %s - buffer %d out of range\n",
  576. ZR_DEVNAME(zr), __func__, num);
  577. return -EINVAL;
  578. }
  579. /* what is the codec mode right now? */
  580. if (zr->codec_mode == BUZ_MODE_IDLE) {
  581. zr->jpg_settings = fh->jpg_settings;
  582. } else if (zr->codec_mode != mode) {
  583. /* wrong codec mode active - invalid */
  584. dprintk(1,
  585. KERN_ERR
  586. "%s: %s - codec in wrong mode\n",
  587. ZR_DEVNAME(zr), __func__);
  588. return -EINVAL;
  589. }
  590. if (fh->buffers.active == ZORAN_FREE) {
  591. if (zr->jpg_buffers.active == ZORAN_FREE) {
  592. zr->jpg_buffers = fh->buffers;
  593. fh->buffers.active = ZORAN_ACTIVE;
  594. } else {
  595. dprintk(1,
  596. KERN_ERR
  597. "%s: %s - another session is already capturing\n",
  598. ZR_DEVNAME(zr), __func__);
  599. res = -EBUSY;
  600. }
  601. }
  602. if (!res && zr->codec_mode == BUZ_MODE_IDLE) {
  603. /* Ok load up the jpeg codec */
  604. zr36057_enable_jpg(zr, mode);
  605. }
  606. spin_lock_irqsave(&zr->spinlock, flags);
  607. if (!res) {
  608. switch (zr->jpg_buffers.buffer[num].state) {
  609. case BUZ_STATE_DONE:
  610. dprintk(2,
  611. KERN_WARNING
  612. "%s: %s - queing frame in BUZ_STATE_DONE state!?\n",
  613. ZR_DEVNAME(zr), __func__);
  614. case BUZ_STATE_USER:
  615. /* since there is at least one unused buffer there's room for at
  616. *least one more pend[] entry */
  617. zr->jpg_pend[zr->jpg_que_head++ & BUZ_MASK_FRAME] = num;
  618. zr->jpg_buffers.buffer[num].state = BUZ_STATE_PEND;
  619. fh->buffers.buffer[num] = zr->jpg_buffers.buffer[num];
  620. zoran_feed_stat_com(zr);
  621. break;
  622. default:
  623. case BUZ_STATE_DMA:
  624. case BUZ_STATE_PEND:
  625. if (zr->jpg_buffers.active == ZORAN_FREE) {
  626. fh->buffers.active = ZORAN_FREE;
  627. zr->jpg_buffers.allocated = 0;
  628. }
  629. res = -EBUSY; /* what are you doing? */
  630. break;
  631. }
  632. }
  633. spin_unlock_irqrestore(&zr->spinlock, flags);
  634. if (!res && zr->jpg_buffers.active == ZORAN_FREE)
  635. zr->jpg_buffers.active = fh->buffers.active;
  636. return res;
  637. }
  638. static int jpg_qbuf(struct zoran_fh *fh, int frame, enum zoran_codec_mode mode)
  639. {
  640. struct zoran *zr = fh->zr;
  641. int res = 0;
  642. /* Does the user want to stop streaming? */
  643. if (frame < 0) {
  644. if (zr->codec_mode == mode) {
  645. if (fh->buffers.active == ZORAN_FREE) {
  646. dprintk(1,
  647. KERN_ERR
  648. "%s: %s(-1) - session not active\n",
  649. ZR_DEVNAME(zr), __func__);
  650. return -EINVAL;
  651. }
  652. fh->buffers.active = zr->jpg_buffers.active = ZORAN_FREE;
  653. zr->jpg_buffers.allocated = 0;
  654. zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
  655. return 0;
  656. } else {
  657. dprintk(1,
  658. KERN_ERR
  659. "%s: %s - stop streaming but not in streaming mode\n",
  660. ZR_DEVNAME(zr), __func__);
  661. return -EINVAL;
  662. }
  663. }
  664. if ((res = zoran_jpg_queue_frame(fh, frame, mode)))
  665. return res;
  666. /* Start the jpeg codec when the first frame is queued */
  667. if (!res && zr->jpg_que_head == 1)
  668. jpeg_start(zr);
  669. return res;
  670. }
  671. /*
  672. * Sync on a MJPEG buffer
  673. */
  674. static int jpg_sync(struct zoran_fh *fh, struct zoran_sync *bs)
  675. {
  676. struct zoran *zr = fh->zr;
  677. unsigned long flags;
  678. int frame;
  679. if (fh->buffers.active == ZORAN_FREE) {
  680. dprintk(1,
  681. KERN_ERR
  682. "%s: %s - capture is not currently active\n",
  683. ZR_DEVNAME(zr), __func__);
  684. return -EINVAL;
  685. }
  686. if (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS &&
  687. zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) {
  688. dprintk(1,
  689. KERN_ERR
  690. "%s: %s - codec not in streaming mode\n",
  691. ZR_DEVNAME(zr), __func__);
  692. return -EINVAL;
  693. }
  694. if (!wait_event_interruptible_timeout(zr->jpg_capq,
  695. (zr->jpg_que_tail != zr->jpg_dma_tail ||
  696. zr->jpg_dma_tail == zr->jpg_dma_head),
  697. 10*HZ)) {
  698. int isr;
  699. btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
  700. udelay(1);
  701. zr->codec->control(zr->codec, CODEC_G_STATUS,
  702. sizeof(isr), &isr);
  703. dprintk(1,
  704. KERN_ERR
  705. "%s: %s - timeout: codec isr=0x%02x\n",
  706. ZR_DEVNAME(zr), __func__, isr);
  707. return -ETIME;
  708. }
  709. if (signal_pending(current))
  710. return -ERESTARTSYS;
  711. spin_lock_irqsave(&zr->spinlock, flags);
  712. if (zr->jpg_dma_tail != zr->jpg_dma_head)
  713. frame = zr->jpg_pend[zr->jpg_que_tail++ & BUZ_MASK_FRAME];
  714. else
  715. frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
  716. /* buffer should now be in BUZ_STATE_DONE */
  717. if (zr->jpg_buffers.buffer[frame].state != BUZ_STATE_DONE)
  718. dprintk(2,
  719. KERN_ERR "%s: %s - internal state error\n",
  720. ZR_DEVNAME(zr), __func__);
  721. *bs = zr->jpg_buffers.buffer[frame].bs;
  722. bs->frame = frame;
  723. zr->jpg_buffers.buffer[frame].state = BUZ_STATE_USER;
  724. fh->buffers.buffer[frame] = zr->jpg_buffers.buffer[frame];
  725. spin_unlock_irqrestore(&zr->spinlock, flags);
  726. return 0;
  727. }
  728. static void zoran_open_init_session(struct zoran_fh *fh)
  729. {
  730. int i;
  731. struct zoran *zr = fh->zr;
  732. /* Per default, map the V4L Buffers */
  733. map_mode_raw(fh);
  734. /* take over the card's current settings */
  735. fh->overlay_settings = zr->overlay_settings;
  736. fh->overlay_settings.is_set = 0;
  737. fh->overlay_settings.format = zr->overlay_settings.format;
  738. fh->overlay_active = ZORAN_FREE;
  739. /* v4l settings */
  740. fh->v4l_settings = zr->v4l_settings;
  741. /* jpg settings */
  742. fh->jpg_settings = zr->jpg_settings;
  743. /* buffers */
  744. memset(&fh->buffers, 0, sizeof(fh->buffers));
  745. for (i = 0; i < MAX_FRAME; i++) {
  746. fh->buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
  747. fh->buffers.buffer[i].bs.frame = i;
  748. }
  749. fh->buffers.allocated = 0;
  750. fh->buffers.active = ZORAN_FREE;
  751. }
  752. static void zoran_close_end_session(struct zoran_fh *fh)
  753. {
  754. struct zoran *zr = fh->zr;
  755. /* overlay */
  756. if (fh->overlay_active != ZORAN_FREE) {
  757. fh->overlay_active = zr->overlay_active = ZORAN_FREE;
  758. zr->v4l_overlay_active = 0;
  759. if (!zr->v4l_memgrab_active)
  760. zr36057_overlay(zr, 0);
  761. zr->overlay_mask = NULL;
  762. }
  763. if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
  764. /* v4l capture */
  765. if (fh->buffers.active != ZORAN_FREE) {
  766. unsigned long flags;
  767. spin_lock_irqsave(&zr->spinlock, flags);
  768. zr36057_set_memgrab(zr, 0);
  769. zr->v4l_buffers.allocated = 0;
  770. zr->v4l_buffers.active = fh->buffers.active = ZORAN_FREE;
  771. spin_unlock_irqrestore(&zr->spinlock, flags);
  772. }
  773. /* v4l buffers */
  774. if (fh->buffers.allocated)
  775. v4l_fbuffer_free(fh);
  776. } else {
  777. /* jpg capture */
  778. if (fh->buffers.active != ZORAN_FREE) {
  779. zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
  780. zr->jpg_buffers.allocated = 0;
  781. zr->jpg_buffers.active = fh->buffers.active = ZORAN_FREE;
  782. }
  783. /* jpg buffers */
  784. if (fh->buffers.allocated)
  785. jpg_fbuffer_free(fh);
  786. }
  787. }
  788. /*
  789. * Open a zoran card. Right now the flags stuff is just playing
  790. */
  791. static int zoran_open(struct file *file)
  792. {
  793. struct zoran *zr = video_drvdata(file);
  794. struct zoran_fh *fh;
  795. int res, first_open = 0;
  796. dprintk(2, KERN_INFO "%s: %s(%s, pid=[%d]), users(-)=%d\n",
  797. ZR_DEVNAME(zr), __func__, current->comm, task_pid_nr(current), zr->user + 1);
  798. lock_kernel();
  799. if (zr->user >= 2048) {
  800. dprintk(1, KERN_ERR "%s: too many users (%d) on device\n",
  801. ZR_DEVNAME(zr), zr->user);
  802. res = -EBUSY;
  803. goto fail_unlock;
  804. }
  805. /* now, create the open()-specific file_ops struct */
  806. fh = kzalloc(sizeof(struct zoran_fh), GFP_KERNEL);
  807. if (!fh) {
  808. dprintk(1,
  809. KERN_ERR
  810. "%s: %s - allocation of zoran_fh failed\n",
  811. ZR_DEVNAME(zr), __func__);
  812. res = -ENOMEM;
  813. goto fail_unlock;
  814. }
  815. /* used to be BUZ_MAX_WIDTH/HEIGHT, but that gives overflows
  816. * on norm-change! */
  817. fh->overlay_mask =
  818. kmalloc(((768 + 31) / 32) * 576 * 4, GFP_KERNEL);
  819. if (!fh->overlay_mask) {
  820. dprintk(1,
  821. KERN_ERR
  822. "%s: %s - allocation of overlay_mask failed\n",
  823. ZR_DEVNAME(zr), __func__);
  824. res = -ENOMEM;
  825. goto fail_fh;
  826. }
  827. if (zr->user++ == 0)
  828. first_open = 1;
  829. /*mutex_unlock(&zr->resource_lock);*/
  830. /* default setup - TODO: look at flags */
  831. if (first_open) { /* First device open */
  832. zr36057_restart(zr);
  833. zoran_open_init_params(zr);
  834. zoran_init_hardware(zr);
  835. btor(ZR36057_ICR_IntPinEn, ZR36057_ICR);
  836. }
  837. /* set file_ops stuff */
  838. file->private_data = fh;
  839. fh->zr = zr;
  840. zoran_open_init_session(fh);
  841. unlock_kernel();
  842. return 0;
  843. fail_fh:
  844. kfree(fh);
  845. fail_unlock:
  846. unlock_kernel();
  847. dprintk(2, KERN_INFO "%s: open failed (%d), users(-)=%d\n",
  848. ZR_DEVNAME(zr), res, zr->user);
  849. return res;
  850. }
  851. static int
  852. zoran_close(struct file *file)
  853. {
  854. struct zoran_fh *fh = file->private_data;
  855. struct zoran *zr = fh->zr;
  856. dprintk(2, KERN_INFO "%s: %s(%s, pid=[%d]), users(+)=%d\n",
  857. ZR_DEVNAME(zr), __func__, current->comm, task_pid_nr(current), zr->user - 1);
  858. /* kernel locks (fs/device.c), so don't do that ourselves
  859. * (prevents deadlocks) */
  860. /*mutex_lock(&zr->resource_lock);*/
  861. zoran_close_end_session(fh);
  862. if (zr->user-- == 1) { /* Last process */
  863. /* Clean up JPEG process */
  864. wake_up_interruptible(&zr->jpg_capq);
  865. zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
  866. zr->jpg_buffers.allocated = 0;
  867. zr->jpg_buffers.active = ZORAN_FREE;
  868. /* disable interrupts */
  869. btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
  870. if (zr36067_debug > 1)
  871. print_interrupts(zr);
  872. /* Overlay off */
  873. zr->v4l_overlay_active = 0;
  874. zr36057_overlay(zr, 0);
  875. zr->overlay_mask = NULL;
  876. /* capture off */
  877. wake_up_interruptible(&zr->v4l_capq);
  878. zr36057_set_memgrab(zr, 0);
  879. zr->v4l_buffers.allocated = 0;
  880. zr->v4l_buffers.active = ZORAN_FREE;
  881. zoran_set_pci_master(zr, 0);
  882. if (!pass_through) { /* Switch to color bar */
  883. decoder_call(zr, video, s_stream, 0);
  884. encoder_call(zr, video, s_routing, 2, 0, 0);
  885. }
  886. }
  887. file->private_data = NULL;
  888. kfree(fh->overlay_mask);
  889. kfree(fh);
  890. dprintk(4, KERN_INFO "%s: %s done\n", ZR_DEVNAME(zr), __func__);
  891. return 0;
  892. }
  893. static ssize_t
  894. zoran_read (struct file *file,
  895. char __user *data,
  896. size_t count,
  897. loff_t *ppos)
  898. {
  899. /* we simply don't support read() (yet)... */
  900. return -EINVAL;
  901. }
  902. static ssize_t
  903. zoran_write (struct file *file,
  904. const char __user *data,
  905. size_t count,
  906. loff_t *ppos)
  907. {
  908. /* ...and the same goes for write() */
  909. return -EINVAL;
  910. }
  911. static int setup_fbuffer(struct zoran_fh *fh,
  912. void *base,
  913. const struct zoran_format *fmt,
  914. int width,
  915. int height,
  916. int bytesperline)
  917. {
  918. struct zoran *zr = fh->zr;
  919. /* (Ronald) v4l/v4l2 guidelines */
  920. if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
  921. return -EPERM;
  922. /* Don't allow frame buffer overlay if PCI or AGP is buggy, or on
  923. ALi Magik (that needs very low latency while the card needs a
  924. higher value always) */
  925. if (pci_pci_problems & (PCIPCI_FAIL | PCIAGP_FAIL | PCIPCI_ALIMAGIK))
  926. return -ENXIO;
  927. /* we need a bytesperline value, even if not given */
  928. if (!bytesperline)
  929. bytesperline = width * ((fmt->depth + 7) & ~7) / 8;
  930. #if 0
  931. if (zr->overlay_active) {
  932. /* dzjee... stupid users... don't even bother to turn off
  933. * overlay before changing the memory location...
  934. * normally, we would return errors here. However, one of
  935. * the tools that does this is... xawtv! and since xawtv
  936. * is used by +/- 99% of the users, we'd rather be user-
  937. * friendly and silently do as if nothing went wrong */
  938. dprintk(3,
  939. KERN_ERR
  940. "%s: %s - forced overlay turnoff because framebuffer changed\n",
  941. ZR_DEVNAME(zr), __func__);
  942. zr36057_overlay(zr, 0);
  943. }
  944. #endif
  945. if (!(fmt->flags & ZORAN_FORMAT_OVERLAY)) {
  946. dprintk(1,
  947. KERN_ERR
  948. "%s: %s - no valid overlay format given\n",
  949. ZR_DEVNAME(zr), __func__);
  950. return -EINVAL;
  951. }
  952. if (height <= 0 || width <= 0 || bytesperline <= 0) {
  953. dprintk(1,
  954. KERN_ERR
  955. "%s: %s - invalid height/width/bpl value (%d|%d|%d)\n",
  956. ZR_DEVNAME(zr), __func__, width, height, bytesperline);
  957. return -EINVAL;
  958. }
  959. if (bytesperline & 3) {
  960. dprintk(1,
  961. KERN_ERR
  962. "%s: %s - bytesperline (%d) must be 4-byte aligned\n",
  963. ZR_DEVNAME(zr), __func__, bytesperline);
  964. return -EINVAL;
  965. }
  966. zr->vbuf_base = (void *) ((unsigned long) base & ~3);
  967. zr->vbuf_height = height;
  968. zr->vbuf_width = width;
  969. zr->vbuf_depth = fmt->depth;
  970. zr->overlay_settings.format = fmt;
  971. zr->vbuf_bytesperline = bytesperline;
  972. /* The user should set new window parameters */
  973. zr->overlay_settings.is_set = 0;
  974. return 0;
  975. }
  976. static int setup_window(struct zoran_fh *fh, int x, int y, int width, int height,
  977. struct v4l2_clip __user *clips, int clipcount, void __user *bitmap)
  978. {
  979. struct zoran *zr = fh->zr;
  980. struct v4l2_clip *vcp = NULL;
  981. int on, end;
  982. if (!zr->vbuf_base) {
  983. dprintk(1,
  984. KERN_ERR
  985. "%s: %s - frame buffer has to be set first\n",
  986. ZR_DEVNAME(zr), __func__);
  987. return -EINVAL;
  988. }
  989. if (!fh->overlay_settings.format) {
  990. dprintk(1,
  991. KERN_ERR
  992. "%s: %s - no overlay format set\n",
  993. ZR_DEVNAME(zr), __func__);
  994. return -EINVAL;
  995. }
  996. /*
  997. * The video front end needs 4-byte alinged line sizes, we correct that
  998. * silently here if necessary
  999. */
  1000. if (zr->vbuf_depth == 15 || zr->vbuf_depth == 16) {
  1001. end = (x + width) & ~1; /* round down */
  1002. x = (x + 1) & ~1; /* round up */
  1003. width = end - x;
  1004. }
  1005. if (zr->vbuf_depth == 24) {
  1006. end = (x + width) & ~3; /* round down */
  1007. x = (x + 3) & ~3; /* round up */
  1008. width = end - x;
  1009. }
  1010. if (width > BUZ_MAX_WIDTH)
  1011. width = BUZ_MAX_WIDTH;
  1012. if (height > BUZ_MAX_HEIGHT)
  1013. height = BUZ_MAX_HEIGHT;
  1014. /* Check for vaild parameters */
  1015. if (width < BUZ_MIN_WIDTH || height < BUZ_MIN_HEIGHT ||
  1016. width > BUZ_MAX_WIDTH || height > BUZ_MAX_HEIGHT) {
  1017. dprintk(1,
  1018. KERN_ERR
  1019. "%s: %s - width = %d or height = %d invalid\n",
  1020. ZR_DEVNAME(zr), __func__, width, height);
  1021. return -EINVAL;
  1022. }
  1023. fh->overlay_settings.x = x;
  1024. fh->overlay_settings.y = y;
  1025. fh->overlay_settings.width = width;
  1026. fh->overlay_settings.height = height;
  1027. fh->overlay_settings.clipcount = clipcount;
  1028. /*
  1029. * If an overlay is running, we have to switch it off
  1030. * and switch it on again in order to get the new settings in effect.
  1031. *
  1032. * We also want to avoid that the overlay mask is written
  1033. * when an overlay is running.
  1034. */
  1035. on = zr->v4l_overlay_active && !zr->v4l_memgrab_active &&
  1036. zr->overlay_active != ZORAN_FREE &&
  1037. fh->overlay_active != ZORAN_FREE;
  1038. if (on)
  1039. zr36057_overlay(zr, 0);
  1040. /*
  1041. * Write the overlay mask if clips are wanted.
  1042. * We prefer a bitmap.
  1043. */
  1044. if (bitmap) {
  1045. /* fake value - it just means we want clips */
  1046. fh->overlay_settings.clipcount = 1;
  1047. if (copy_from_user(fh->overlay_mask, bitmap,
  1048. (width * height + 7) / 8)) {
  1049. return -EFAULT;
  1050. }
  1051. } else if (clipcount > 0) {
  1052. /* write our own bitmap from the clips */
  1053. vcp = vmalloc(sizeof(struct v4l2_clip) * (clipcount + 4));
  1054. if (vcp == NULL) {
  1055. dprintk(1,
  1056. KERN_ERR
  1057. "%s: %s - Alloc of clip mask failed\n",
  1058. ZR_DEVNAME(zr), __func__);
  1059. return -ENOMEM;
  1060. }
  1061. if (copy_from_user
  1062. (vcp, clips, sizeof(struct v4l2_clip) * clipcount)) {
  1063. vfree(vcp);
  1064. return -EFAULT;
  1065. }
  1066. write_overlay_mask(fh, vcp, clipcount);
  1067. vfree(vcp);
  1068. }
  1069. fh->overlay_settings.is_set = 1;
  1070. if (fh->overlay_active != ZORAN_FREE &&
  1071. zr->overlay_active != ZORAN_FREE)
  1072. zr->overlay_settings = fh->overlay_settings;
  1073. if (on)
  1074. zr36057_overlay(zr, 1);
  1075. /* Make sure the changes come into effect */
  1076. return wait_grab_pending(zr);
  1077. }
  1078. static int setup_overlay(struct zoran_fh *fh, int on)
  1079. {
  1080. struct zoran *zr = fh->zr;
  1081. /* If there is nothing to do, return immediatly */
  1082. if ((on && fh->overlay_active != ZORAN_FREE) ||
  1083. (!on && fh->overlay_active == ZORAN_FREE))
  1084. return 0;
  1085. /* check whether we're touching someone else's overlay */
  1086. if (on && zr->overlay_active != ZORAN_FREE &&
  1087. fh->overlay_active == ZORAN_FREE) {
  1088. dprintk(1,
  1089. KERN_ERR
  1090. "%s: %s - overlay is already active for another session\n",
  1091. ZR_DEVNAME(zr), __func__);
  1092. return -EBUSY;
  1093. }
  1094. if (!on && zr->overlay_active != ZORAN_FREE &&
  1095. fh->overlay_active == ZORAN_FREE) {
  1096. dprintk(1,
  1097. KERN_ERR
  1098. "%s: %s - you cannot cancel someone else's session\n",
  1099. ZR_DEVNAME(zr), __func__);
  1100. return -EPERM;
  1101. }
  1102. if (on == 0) {
  1103. zr->overlay_active = fh->overlay_active = ZORAN_FREE;
  1104. zr->v4l_overlay_active = 0;
  1105. /* When a grab is running, the video simply
  1106. * won't be switched on any more */
  1107. if (!zr->v4l_memgrab_active)
  1108. zr36057_overlay(zr, 0);
  1109. zr->overlay_mask = NULL;
  1110. } else {
  1111. if (!zr->vbuf_base || !fh->overlay_settings.is_set) {
  1112. dprintk(1,
  1113. KERN_ERR
  1114. "%s: %s - buffer or window not set\n",
  1115. ZR_DEVNAME(zr), __func__);
  1116. return -EINVAL;
  1117. }
  1118. if (!fh->overlay_settings.format) {
  1119. dprintk(1,
  1120. KERN_ERR
  1121. "%s: %s - no overlay format set\n",
  1122. ZR_DEVNAME(zr), __func__);
  1123. return -EINVAL;
  1124. }
  1125. zr->overlay_active = fh->overlay_active = ZORAN_LOCKED;
  1126. zr->v4l_overlay_active = 1;
  1127. zr->overlay_mask = fh->overlay_mask;
  1128. zr->overlay_settings = fh->overlay_settings;
  1129. if (!zr->v4l_memgrab_active)
  1130. zr36057_overlay(zr, 1);
  1131. /* When a grab is running, the video will be
  1132. * switched on when grab is finished */
  1133. }
  1134. /* Make sure the changes come into effect */
  1135. return wait_grab_pending(zr);
  1136. }
  1137. /* get the status of a buffer in the clients buffer queue */
  1138. static int zoran_v4l2_buffer_status(struct zoran_fh *fh,
  1139. struct v4l2_buffer *buf, int num)
  1140. {
  1141. struct zoran *zr = fh->zr;
  1142. unsigned long flags;
  1143. buf->flags = V4L2_BUF_FLAG_MAPPED;
  1144. switch (fh->map_mode) {
  1145. case ZORAN_MAP_MODE_RAW:
  1146. /* check range */
  1147. if (num < 0 || num >= fh->buffers.num_buffers ||
  1148. !fh->buffers.allocated) {
  1149. dprintk(1,
  1150. KERN_ERR
  1151. "%s: %s - wrong number or buffers not allocated\n",
  1152. ZR_DEVNAME(zr), __func__);
  1153. return -EINVAL;
  1154. }
  1155. spin_lock_irqsave(&zr->spinlock, flags);
  1156. dprintk(3,
  1157. KERN_DEBUG
  1158. "%s: %s() - raw active=%c, buffer %d: state=%c, map=%c\n",
  1159. ZR_DEVNAME(zr), __func__,
  1160. "FAL"[fh->buffers.active], num,
  1161. "UPMD"[zr->v4l_buffers.buffer[num].state],
  1162. fh->buffers.buffer[num].map ? 'Y' : 'N');
  1163. spin_unlock_irqrestore(&zr->spinlock, flags);
  1164. buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1165. buf->length = fh->buffers.buffer_size;
  1166. /* get buffer */
  1167. buf->bytesused = fh->buffers.buffer[num].bs.length;
  1168. if (fh->buffers.buffer[num].state == BUZ_STATE_DONE ||
  1169. fh->buffers.buffer[num].state == BUZ_STATE_USER) {
  1170. buf->sequence = fh->buffers.buffer[num].bs.seq;
  1171. buf->flags |= V4L2_BUF_FLAG_DONE;
  1172. buf->timestamp = fh->buffers.buffer[num].bs.timestamp;
  1173. } else {
  1174. buf->flags |= V4L2_BUF_FLAG_QUEUED;
  1175. }
  1176. if (fh->v4l_settings.height <= BUZ_MAX_HEIGHT / 2)
  1177. buf->field = V4L2_FIELD_TOP;
  1178. else
  1179. buf->field = V4L2_FIELD_INTERLACED;
  1180. break;
  1181. case ZORAN_MAP_MODE_JPG_REC:
  1182. case ZORAN_MAP_MODE_JPG_PLAY:
  1183. /* check range */
  1184. if (num < 0 || num >= fh->buffers.num_buffers ||
  1185. !fh->buffers.allocated) {
  1186. dprintk(1,
  1187. KERN_ERR
  1188. "%s: %s - wrong number or buffers not allocated\n",
  1189. ZR_DEVNAME(zr), __func__);
  1190. return -EINVAL;
  1191. }
  1192. buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
  1193. V4L2_BUF_TYPE_VIDEO_CAPTURE :
  1194. V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1195. buf->length = fh->buffers.buffer_size;
  1196. /* these variables are only written after frame has been captured */
  1197. if (fh->buffers.buffer[num].state == BUZ_STATE_DONE ||
  1198. fh->buffers.buffer[num].state == BUZ_STATE_USER) {
  1199. buf->sequence = fh->buffers.buffer[num].bs.seq;
  1200. buf->timestamp = fh->buffers.buffer[num].bs.timestamp;
  1201. buf->bytesused = fh->buffers.buffer[num].bs.length;
  1202. buf->flags |= V4L2_BUF_FLAG_DONE;
  1203. } else {
  1204. buf->flags |= V4L2_BUF_FLAG_QUEUED;
  1205. }
  1206. /* which fields are these? */
  1207. if (fh->jpg_settings.TmpDcm != 1)
  1208. buf->field = fh->jpg_settings.odd_even ?
  1209. V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
  1210. else
  1211. buf->field = fh->jpg_settings.odd_even ?
  1212. V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT;
  1213. break;
  1214. default:
  1215. dprintk(5,
  1216. KERN_ERR
  1217. "%s: %s - invalid buffer type|map_mode (%d|%d)\n",
  1218. ZR_DEVNAME(zr), __func__, buf->type, fh->map_mode);
  1219. return -EINVAL;
  1220. }
  1221. buf->memory = V4L2_MEMORY_MMAP;
  1222. buf->index = num;
  1223. buf->m.offset = buf->length * num;
  1224. return 0;
  1225. }
  1226. static int
  1227. zoran_set_norm (struct zoran *zr,
  1228. v4l2_std_id norm)
  1229. {
  1230. int on;
  1231. if (zr->v4l_buffers.active != ZORAN_FREE ||
  1232. zr->jpg_buffers.active != ZORAN_FREE) {
  1233. dprintk(1,
  1234. KERN_WARNING
  1235. "%s: %s called while in playback/capture mode\n",
  1236. ZR_DEVNAME(zr), __func__);
  1237. return -EBUSY;
  1238. }
  1239. if (!(norm & zr->card.norms)) {
  1240. dprintk(1,
  1241. KERN_ERR "%s: %s - unsupported norm %llx\n",
  1242. ZR_DEVNAME(zr), __func__, norm);
  1243. return -EINVAL;
  1244. }
  1245. if (norm == V4L2_STD_ALL) {
  1246. int status = 0;
  1247. v4l2_std_id std = 0;
  1248. decoder_call(zr, video, querystd, &std);
  1249. decoder_call(zr, core, s_std, std);
  1250. /* let changes come into effect */
  1251. ssleep(2);
  1252. decoder_call(zr, video, g_input_status, &status);
  1253. if (status & V4L2_IN_ST_NO_SIGNAL) {
  1254. dprintk(1,
  1255. KERN_ERR
  1256. "%s: %s - no norm detected\n",
  1257. ZR_DEVNAME(zr), __func__);
  1258. /* reset norm */
  1259. decoder_call(zr, core, s_std, zr->norm);
  1260. return -EIO;
  1261. }
  1262. norm = std;
  1263. }
  1264. if (norm & V4L2_STD_SECAM)
  1265. zr->timing = zr->card.tvn[2];
  1266. else if (norm & V4L2_STD_NTSC)
  1267. zr->timing = zr->card.tvn[1];
  1268. else
  1269. zr->timing = zr->card.tvn[0];
  1270. /* We switch overlay off and on since a change in the
  1271. * norm needs different VFE settings */
  1272. on = zr->overlay_active && !zr->v4l_memgrab_active;
  1273. if (on)
  1274. zr36057_overlay(zr, 0);
  1275. decoder_call(zr, core, s_std, norm);
  1276. encoder_call(zr, video, s_std_output, norm);
  1277. if (on)
  1278. zr36057_overlay(zr, 1);
  1279. /* Make sure the changes come into effect */
  1280. zr->norm = norm;
  1281. return 0;
  1282. }
  1283. static int
  1284. zoran_set_input (struct zoran *zr,
  1285. int input)
  1286. {
  1287. if (input == zr->input) {
  1288. return 0;
  1289. }
  1290. if (zr->v4l_buffers.active != ZORAN_FREE ||
  1291. zr->jpg_buffers.active != ZORAN_FREE) {
  1292. dprintk(1,
  1293. KERN_WARNING
  1294. "%s: %s called while in playback/capture mode\n",
  1295. ZR_DEVNAME(zr), __func__);
  1296. return -EBUSY;
  1297. }
  1298. if (input < 0 || input >= zr->card.inputs) {
  1299. dprintk(1,
  1300. KERN_ERR
  1301. "%s: %s - unnsupported input %d\n",
  1302. ZR_DEVNAME(zr), __func__, input);
  1303. return -EINVAL;
  1304. }
  1305. zr->input = input;
  1306. decoder_call(zr, video, s_routing,
  1307. zr->card.input[input].muxsel, 0, 0);
  1308. return 0;
  1309. }
  1310. /*
  1311. * ioctl routine
  1312. */
  1313. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1314. static long zoran_default(struct file *file, void *__fh, int cmd, void *arg)
  1315. {
  1316. struct zoran_fh *fh = __fh;
  1317. struct zoran *zr = fh->zr;
  1318. struct zoran_jpg_settings settings;
  1319. switch (cmd) {
  1320. case BUZIOC_G_PARAMS:
  1321. {
  1322. struct zoran_params *bparams = arg;
  1323. dprintk(3, KERN_DEBUG "%s: BUZIOC_G_PARAMS\n", ZR_DEVNAME(zr));
  1324. memset(bparams, 0, sizeof(struct zoran_params));
  1325. bparams->major_version = MAJOR_VERSION;
  1326. bparams->minor_version = MINOR_VERSION;
  1327. mutex_lock(&zr->resource_lock);
  1328. if (zr->norm & V4L2_STD_NTSC)
  1329. bparams->norm = VIDEO_MODE_NTSC;
  1330. else if (zr->norm & V4L2_STD_PAL)
  1331. bparams->norm = VIDEO_MODE_PAL;
  1332. else
  1333. bparams->norm = VIDEO_MODE_SECAM;
  1334. bparams->input = zr->input;
  1335. bparams->decimation = fh->jpg_settings.decimation;
  1336. bparams->HorDcm = fh->jpg_settings.HorDcm;
  1337. bparams->VerDcm = fh->jpg_settings.VerDcm;
  1338. bparams->TmpDcm = fh->jpg_settings.TmpDcm;
  1339. bparams->field_per_buff = fh->jpg_settings.field_per_buff;
  1340. bparams->img_x = fh->jpg_settings.img_x;
  1341. bparams->img_y = fh->jpg_settings.img_y;
  1342. bparams->img_width = fh->jpg_settings.img_width;
  1343. bparams->img_height = fh->jpg_settings.img_height;
  1344. bparams->odd_even = fh->jpg_settings.odd_even;
  1345. bparams->quality = fh->jpg_settings.jpg_comp.quality;
  1346. bparams->APPn = fh->jpg_settings.jpg_comp.APPn;
  1347. bparams->APP_len = fh->jpg_settings.jpg_comp.APP_len;
  1348. memcpy(bparams->APP_data,
  1349. fh->jpg_settings.jpg_comp.APP_data,
  1350. sizeof(bparams->APP_data));
  1351. bparams->COM_len = zr->jpg_settings.jpg_comp.COM_len;
  1352. memcpy(bparams->COM_data,
  1353. fh->jpg_settings.jpg_comp.COM_data,
  1354. sizeof(bparams->COM_data));
  1355. bparams->jpeg_markers =
  1356. fh->jpg_settings.jpg_comp.jpeg_markers;
  1357. mutex_unlock(&zr->resource_lock);
  1358. bparams->VFIFO_FB = 0;
  1359. return 0;
  1360. }
  1361. case BUZIOC_S_PARAMS:
  1362. {
  1363. struct zoran_params *bparams = arg;
  1364. int res = 0;
  1365. dprintk(3, KERN_DEBUG "%s: BUZIOC_S_PARAMS\n", ZR_DEVNAME(zr));
  1366. settings.decimation = bparams->decimation;
  1367. settings.HorDcm = bparams->HorDcm;
  1368. settings.VerDcm = bparams->VerDcm;
  1369. settings.TmpDcm = bparams->TmpDcm;
  1370. settings.field_per_buff = bparams->field_per_buff;
  1371. settings.img_x = bparams->img_x;
  1372. settings.img_y = bparams->img_y;
  1373. settings.img_width = bparams->img_width;
  1374. settings.img_height = bparams->img_height;
  1375. settings.odd_even = bparams->odd_even;
  1376. settings.jpg_comp.quality = bparams->quality;
  1377. settings.jpg_comp.APPn = bparams->APPn;
  1378. settings.jpg_comp.APP_len = bparams->APP_len;
  1379. memcpy(settings.jpg_comp.APP_data, bparams->APP_data,
  1380. sizeof(bparams->APP_data));
  1381. settings.jpg_comp.COM_len = bparams->COM_len;
  1382. memcpy(settings.jpg_comp.COM_data, bparams->COM_data,
  1383. sizeof(bparams->COM_data));
  1384. settings.jpg_comp.jpeg_markers = bparams->jpeg_markers;
  1385. mutex_lock(&zr->resource_lock);
  1386. if (zr->codec_mode != BUZ_MODE_IDLE) {
  1387. dprintk(1,
  1388. KERN_ERR
  1389. "%s: BUZIOC_S_PARAMS called, but Buz in capture/playback mode\n",
  1390. ZR_DEVNAME(zr));
  1391. res = -EINVAL;
  1392. goto sparams_unlock_and_return;
  1393. }
  1394. /* Check the params first before overwriting our
  1395. * nternal values */
  1396. if (zoran_check_jpg_settings(zr, &settings, 0)) {
  1397. res = -EINVAL;
  1398. goto sparams_unlock_and_return;
  1399. }
  1400. fh->jpg_settings = settings;
  1401. sparams_unlock_and_return:
  1402. mutex_unlock(&zr->resource_lock);
  1403. return res;
  1404. }
  1405. case BUZIOC_REQBUFS:
  1406. {
  1407. struct zoran_requestbuffers *breq = arg;
  1408. int res = 0;
  1409. dprintk(3,
  1410. KERN_DEBUG
  1411. "%s: BUZIOC_REQBUFS - count=%lu, size=%lu\n",
  1412. ZR_DEVNAME(zr), breq->count, breq->size);
  1413. /* Enforce reasonable lower and upper limits */
  1414. if (breq->count < 4)
  1415. breq->count = 4; /* Could be choosen smaller */
  1416. if (breq->count > jpg_nbufs)
  1417. breq->count = jpg_nbufs;
  1418. breq->size = PAGE_ALIGN(breq->size);
  1419. if (breq->size < 8192)
  1420. breq->size = 8192; /* Arbitrary */
  1421. /* breq->size is limited by 1 page for the stat_com
  1422. * tables to a Maximum of 2 MB */
  1423. if (breq->size > jpg_bufsize)
  1424. breq->size = jpg_bufsize;
  1425. mutex_lock(&zr->resource_lock);
  1426. if (fh->buffers.allocated) {
  1427. dprintk(1,
  1428. KERN_ERR
  1429. "%s: BUZIOC_REQBUFS - buffers already allocated\n",
  1430. ZR_DEVNAME(zr));
  1431. res = -EBUSY;
  1432. goto jpgreqbuf_unlock_and_return;
  1433. }
  1434. /* The next mmap will map the MJPEG buffers - could
  1435. * also be *_PLAY, but it doesn't matter here */
  1436. map_mode_jpg(fh, 0);
  1437. fh->buffers.num_buffers = breq->count;
  1438. fh->buffers.buffer_size = breq->size;
  1439. if (jpg_fbuffer_alloc(fh)) {
  1440. res = -ENOMEM;
  1441. goto jpgreqbuf_unlock_and_return;
  1442. }
  1443. jpgreqbuf_unlock_and_return:
  1444. mutex_unlock(&zr->resource_lock);
  1445. return res;
  1446. }
  1447. case BUZIOC_QBUF_CAPT:
  1448. {
  1449. int *frame = arg, res;
  1450. dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_CAPT - frame=%d\n",
  1451. ZR_DEVNAME(zr), *frame);
  1452. mutex_lock(&zr->resource_lock);
  1453. res = jpg_qbuf(fh, *frame, BUZ_MODE_MOTION_COMPRESS);
  1454. mutex_unlock(&zr->resource_lock);
  1455. return res;
  1456. }
  1457. case BUZIOC_QBUF_PLAY:
  1458. {
  1459. int *frame = arg, res;
  1460. dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_PLAY - frame=%d\n",
  1461. ZR_DEVNAME(zr), *frame);
  1462. mutex_lock(&zr->resource_lock);
  1463. res = jpg_qbuf(fh, *frame, BUZ_MODE_MOTION_DECOMPRESS);
  1464. mutex_unlock(&zr->resource_lock);
  1465. return res;
  1466. }
  1467. case BUZIOC_SYNC:
  1468. {
  1469. struct zoran_sync *bsync = arg;
  1470. int res;
  1471. dprintk(3, KERN_DEBUG "%s: BUZIOC_SYNC\n", ZR_DEVNAME(zr));
  1472. mutex_lock(&zr->resource_lock);
  1473. if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
  1474. dprintk(2, KERN_WARNING
  1475. "%s: %s - not in jpg capture mode\n",
  1476. ZR_DEVNAME(zr), __func__);
  1477. res = -EINVAL;
  1478. } else {
  1479. res = jpg_sync(fh, bsync);
  1480. }
  1481. mutex_unlock(&zr->resource_lock);
  1482. return res;
  1483. }
  1484. case BUZIOC_G_STATUS:
  1485. {
  1486. struct zoran_status *bstat = arg;
  1487. int status = 0, res = 0;
  1488. v4l2_std_id norm;
  1489. dprintk(3, KERN_DEBUG "%s: BUZIOC_G_STATUS\n", ZR_DEVNAME(zr));
  1490. if (zr->codec_mode != BUZ_MODE_IDLE) {
  1491. dprintk(1,
  1492. KERN_ERR
  1493. "%s: BUZIOC_G_STATUS called but Buz in capture/playback mode\n",
  1494. ZR_DEVNAME(zr));
  1495. return -EINVAL;
  1496. }
  1497. mutex_lock(&zr->resource_lock);
  1498. if (zr->codec_mode != BUZ_MODE_IDLE) {
  1499. dprintk(1,
  1500. KERN_ERR
  1501. "%s: BUZIOC_G_STATUS called, but Buz in capture/playback mode\n",
  1502. ZR_DEVNAME(zr));
  1503. res = -EINVAL;
  1504. goto gstat_unlock_and_return;
  1505. }
  1506. decoder_call(zr, video, s_routing,
  1507. zr->card.input[bstat->input].muxsel, 0, 0);
  1508. /* sleep 1 second */
  1509. ssleep(1);
  1510. /* Get status of video decoder */
  1511. decoder_call(zr, video, querystd, &norm);
  1512. decoder_call(zr, video, g_input_status, &status);
  1513. /* restore previous input and norm */
  1514. decoder_call(zr, video, s_routing,
  1515. zr->card.input[zr->input].muxsel, 0, 0);
  1516. gstat_unlock_and_return:
  1517. mutex_unlock(&zr->resource_lock);
  1518. if (!res) {
  1519. bstat->signal =
  1520. (status & V4L2_IN_ST_NO_SIGNAL) ? 0 : 1;
  1521. if (norm & V4L2_STD_NTSC)
  1522. bstat->norm = VIDEO_MODE_NTSC;
  1523. else if (norm & V4L2_STD_SECAM)
  1524. bstat->norm = VIDEO_MODE_SECAM;
  1525. else
  1526. bstat->norm = VIDEO_MODE_PAL;
  1527. bstat->color =
  1528. (status & V4L2_IN_ST_NO_COLOR) ? 0 : 1;
  1529. }
  1530. return res;
  1531. }
  1532. default:
  1533. return -EINVAL;
  1534. }
  1535. }
  1536. static int zoran_vidiocgmbuf(struct file *file, void *__fh, struct video_mbuf *vmbuf)
  1537. {
  1538. struct zoran_fh *fh = __fh;
  1539. struct zoran *zr = fh->zr;
  1540. int i, res = 0;
  1541. mutex_lock(&zr->resource_lock);
  1542. if (fh->buffers.allocated) {
  1543. dprintk(1,
  1544. KERN_ERR
  1545. "%s: VIDIOCGMBUF - buffers already allocated\n",
  1546. ZR_DEVNAME(zr));
  1547. res = -EINVAL;
  1548. goto v4l1reqbuf_unlock_and_return;
  1549. }
  1550. /* The next mmap will map the V4L buffers */
  1551. map_mode_raw(fh);
  1552. if (v4l_fbuffer_alloc(fh)) {
  1553. res = -ENOMEM;
  1554. goto v4l1reqbuf_unlock_and_return;
  1555. }
  1556. vmbuf->size = fh->buffers.num_buffers * fh->buffers.buffer_size;
  1557. vmbuf->frames = fh->buffers.num_buffers;
  1558. for (i = 0; i < vmbuf->frames; i++)
  1559. vmbuf->offsets[i] = i * fh->buffers.buffer_size;
  1560. v4l1reqbuf_unlock_and_return:
  1561. mutex_unlock(&zr->resource_lock);
  1562. return res;
  1563. }
  1564. #endif
  1565. static int zoran_querycap(struct file *file, void *__fh, struct v4l2_capability *cap)
  1566. {
  1567. struct zoran_fh *fh = __fh;
  1568. struct zoran *zr = fh->zr;
  1569. memset(cap, 0, sizeof(*cap));
  1570. strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card)-1);
  1571. strncpy(cap->driver, "zoran", sizeof(cap->driver)-1);
  1572. snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
  1573. pci_name(zr->pci_dev));
  1574. cap->version = KERNEL_VERSION(MAJOR_VERSION, MINOR_VERSION,
  1575. RELEASE_VERSION);
  1576. cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE |
  1577. V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OVERLAY;
  1578. return 0;
  1579. }
  1580. static int zoran_enum_fmt(struct zoran *zr, struct v4l2_fmtdesc *fmt, int flag)
  1581. {
  1582. unsigned int num, i;
  1583. for (num = i = 0; i < NUM_FORMATS; i++) {
  1584. if (zoran_formats[i].flags & flag && num++ == fmt->index) {
  1585. strncpy(fmt->description, zoran_formats[i].name,
  1586. sizeof(fmt->description) - 1);
  1587. /* fmt struct pre-zeroed, so adding '\0' not neeed */
  1588. fmt->pixelformat = zoran_formats[i].fourcc;
  1589. if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
  1590. fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
  1591. return 0;
  1592. }
  1593. }
  1594. return -EINVAL;
  1595. }
  1596. static int zoran_enum_fmt_vid_cap(struct file *file, void *__fh,
  1597. struct v4l2_fmtdesc *f)
  1598. {
  1599. struct zoran_fh *fh = __fh;
  1600. struct zoran *zr = fh->zr;
  1601. return zoran_enum_fmt(zr, f, ZORAN_FORMAT_CAPTURE);
  1602. }
  1603. static int zoran_enum_fmt_vid_out(struct file *file, void *__fh,
  1604. struct v4l2_fmtdesc *f)
  1605. {
  1606. struct zoran_fh *fh = __fh;
  1607. struct zoran *zr = fh->zr;
  1608. return zoran_enum_fmt(zr, f, ZORAN_FORMAT_PLAYBACK);
  1609. }
  1610. static int zoran_enum_fmt_vid_overlay(struct file *file, void *__fh,
  1611. struct v4l2_fmtdesc *f)
  1612. {
  1613. struct zoran_fh *fh = __fh;
  1614. struct zoran *zr = fh->zr;
  1615. return zoran_enum_fmt(zr, f, ZORAN_FORMAT_OVERLAY);
  1616. }
  1617. static int zoran_g_fmt_vid_out(struct file *file, void *__fh,
  1618. struct v4l2_format *fmt)
  1619. {
  1620. struct zoran_fh *fh = __fh;
  1621. struct zoran *zr = fh->zr;
  1622. mutex_lock(&zr->resource_lock);
  1623. fmt->fmt.pix.width = fh->jpg_settings.img_width / fh->jpg_settings.HorDcm;
  1624. fmt->fmt.pix.height = fh->jpg_settings.img_height * 2 /
  1625. (fh->jpg_settings.VerDcm * fh->jpg_settings.TmpDcm);
  1626. fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
  1627. fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
  1628. if (fh->jpg_settings.TmpDcm == 1)
  1629. fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
  1630. V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
  1631. else
  1632. fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
  1633. V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
  1634. fmt->fmt.pix.bytesperline = 0;
  1635. fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  1636. mutex_unlock(&zr->resource_lock);
  1637. return 0;
  1638. }
  1639. static int zoran_g_fmt_vid_cap(struct file *file, void *__fh,
  1640. struct v4l2_format *fmt)
  1641. {
  1642. struct zoran_fh *fh = __fh;
  1643. struct zoran *zr = fh->zr;
  1644. if (fh->map_mode != ZORAN_MAP_MODE_RAW)
  1645. return zoran_g_fmt_vid_out(file, fh, fmt);
  1646. mutex_lock(&zr->resource_lock);
  1647. fmt->fmt.pix.width = fh->v4l_settings.width;
  1648. fmt->fmt.pix.height = fh->v4l_settings.height;
  1649. fmt->fmt.pix.sizeimage = fh->v4l_settings.bytesperline *
  1650. fh->v4l_settings.height;
  1651. fmt->fmt.pix.pixelformat = fh->v4l_settings.format->fourcc;
  1652. fmt->fmt.pix.colorspace = fh->v4l_settings.format->colorspace;
  1653. fmt->fmt.pix.bytesperline = fh->v4l_settings.bytesperline;
  1654. if (BUZ_MAX_HEIGHT < (fh->v4l_settings.height * 2))
  1655. fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
  1656. else
  1657. fmt->fmt.pix.field = V4L2_FIELD_TOP;
  1658. mutex_unlock(&zr->resource_lock);
  1659. return 0;
  1660. }
  1661. static int zoran_g_fmt_vid_overlay(struct file *file, void *__fh,
  1662. struct v4l2_format *fmt)
  1663. {
  1664. struct zoran_fh *fh = __fh;
  1665. struct zoran *zr = fh->zr;
  1666. mutex_lock(&zr->resource_lock);
  1667. fmt->fmt.win.w.left = fh->overlay_settings.x;
  1668. fmt->fmt.win.w.top = fh->overlay_settings.y;
  1669. fmt->fmt.win.w.width = fh->overlay_settings.width;
  1670. fmt->fmt.win.w.height = fh->overlay_settings.height;
  1671. if (fh->overlay_settings.width * 2 > BUZ_MAX_HEIGHT)
  1672. fmt->fmt.win.field = V4L2_FIELD_INTERLACED;
  1673. else
  1674. fmt->fmt.win.field = V4L2_FIELD_TOP;
  1675. mutex_unlock(&zr->resource_lock);
  1676. return 0;
  1677. }
  1678. static int zoran_try_fmt_vid_overlay(struct file *file, void *__fh,
  1679. struct v4l2_format *fmt)
  1680. {
  1681. struct zoran_fh *fh = __fh;
  1682. struct zoran *zr = fh->zr;
  1683. mutex_lock(&zr->resource_lock);
  1684. if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH)
  1685. fmt->fmt.win.w.width = BUZ_MAX_WIDTH;
  1686. if (fmt->fmt.win.w.width < BUZ_MIN_WIDTH)
  1687. fmt->fmt.win.w.width = BUZ_MIN_WIDTH;
  1688. if (fmt->fmt.win.w.height > BUZ_MAX_HEIGHT)
  1689. fmt->fmt.win.w.height = BUZ_MAX_HEIGHT;
  1690. if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT)
  1691. fmt->fmt.win.w.height = BUZ_MIN_HEIGHT;
  1692. mutex_unlock(&zr->resource_lock);
  1693. return 0;
  1694. }
  1695. static int zoran_try_fmt_vid_out(struct file *file, void *__fh,
  1696. struct v4l2_format *fmt)
  1697. {
  1698. struct zoran_fh *fh = __fh;
  1699. struct zoran *zr = fh->zr;
  1700. struct zoran_jpg_settings settings;
  1701. int res = 0;
  1702. if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
  1703. return -EINVAL;
  1704. mutex_lock(&zr->resource_lock);
  1705. settings = fh->jpg_settings;
  1706. /* we actually need to set 'real' parameters now */
  1707. if ((fmt->fmt.pix.height * 2) > BUZ_MAX_HEIGHT)
  1708. settings.TmpDcm = 1;
  1709. else
  1710. settings.TmpDcm = 2;
  1711. settings.decimation = 0;
  1712. if (fmt->fmt.pix.height <= fh->jpg_settings.img_height / 2)
  1713. settings.VerDcm = 2;
  1714. else
  1715. settings.VerDcm = 1;
  1716. if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 4)
  1717. settings.HorDcm = 4;
  1718. else if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 2)
  1719. settings.HorDcm = 2;
  1720. else
  1721. settings.HorDcm = 1;
  1722. if (settings.TmpDcm == 1)
  1723. settings.field_per_buff = 2;
  1724. else
  1725. settings.field_per_buff = 1;
  1726. if (settings.HorDcm > 1) {
  1727. settings.img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
  1728. settings.img_width = (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
  1729. } else {
  1730. settings.img_x = 0;
  1731. settings.img_width = BUZ_MAX_WIDTH;
  1732. }
  1733. /* check */
  1734. res = zoran_check_jpg_settings(zr, &settings, 1);
  1735. if (res)
  1736. goto tryfmt_unlock_and_return;
  1737. /* tell the user what we actually did */
  1738. fmt->fmt.pix.width = settings.img_width / settings.HorDcm;
  1739. fmt->fmt.pix.height = settings.img_height * 2 /
  1740. (settings.TmpDcm * settings.VerDcm);
  1741. if (settings.TmpDcm == 1)
  1742. fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
  1743. V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
  1744. else
  1745. fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
  1746. V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
  1747. fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&settings);
  1748. fmt->fmt.pix.bytesperline = 0;
  1749. fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  1750. tryfmt_unlock_and_return:
  1751. mutex_unlock(&zr->resource_lock);
  1752. return res;
  1753. }
  1754. static int zoran_try_fmt_vid_cap(struct file *file, void *__fh,
  1755. struct v4l2_format *fmt)
  1756. {
  1757. struct zoran_fh *fh = __fh;
  1758. struct zoran *zr = fh->zr;
  1759. int bpp;
  1760. int i;
  1761. if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
  1762. return zoran_try_fmt_vid_out(file, fh, fmt);
  1763. mutex_lock(&zr->resource_lock);
  1764. for (i = 0; i < NUM_FORMATS; i++)
  1765. if (zoran_formats[i].fourcc == fmt->fmt.pix.pixelformat)
  1766. break;
  1767. if (i == NUM_FORMATS) {
  1768. mutex_unlock(&zr->resource_lock);
  1769. return -EINVAL;
  1770. }
  1771. bpp = DIV_ROUND_UP(zoran_formats[i].depth, 8);
  1772. v4l_bound_align_image(
  1773. &fmt->fmt.pix.width, BUZ_MIN_WIDTH, BUZ_MAX_WIDTH, bpp == 2 ? 1 : 2,
  1774. &fmt->fmt.pix.height, BUZ_MIN_HEIGHT, BUZ_MAX_HEIGHT, 0, 0);
  1775. mutex_unlock(&zr->resource_lock);
  1776. return 0;
  1777. }
  1778. static int zoran_s_fmt_vid_overlay(struct file *file, void *__fh,
  1779. struct v4l2_format *fmt)
  1780. {
  1781. struct zoran_fh *fh = __fh;
  1782. struct zoran *zr = fh->zr;
  1783. int res;
  1784. dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n",
  1785. fmt->fmt.win.w.left, fmt->fmt.win.w.top,
  1786. fmt->fmt.win.w.width,
  1787. fmt->fmt.win.w.height,
  1788. fmt->fmt.win.clipcount,
  1789. fmt->fmt.win.bitmap);
  1790. mutex_lock(&zr->resource_lock);
  1791. res = setup_window(fh, fmt->fmt.win.w.left, fmt->fmt.win.w.top,
  1792. fmt->fmt.win.w.width, fmt->fmt.win.w.height,
  1793. (struct v4l2_clip __user *)fmt->fmt.win.clips,
  1794. fmt->fmt.win.clipcount, fmt->fmt.win.bitmap);
  1795. mutex_unlock(&zr->resource_lock);
  1796. return res;
  1797. }
  1798. static int zoran_s_fmt_vid_out(struct file *file, void *__fh,
  1799. struct v4l2_format *fmt)
  1800. {
  1801. struct zoran_fh *fh = __fh;
  1802. struct zoran *zr = fh->zr;
  1803. __le32 printformat = __cpu_to_le32(fmt->fmt.pix.pixelformat);
  1804. struct zoran_jpg_settings settings;
  1805. int res = 0;
  1806. dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n",
  1807. fmt->fmt.pix.width, fmt->fmt.pix.height,
  1808. fmt->fmt.pix.pixelformat,
  1809. (char *) &printformat);
  1810. if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
  1811. return -EINVAL;
  1812. mutex_lock(&zr->resource_lock);
  1813. if (fh->buffers.allocated) {
  1814. dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - cannot change capture mode\n",
  1815. ZR_DEVNAME(zr));
  1816. res = -EBUSY;
  1817. goto sfmtjpg_unlock_and_return;
  1818. }
  1819. settings = fh->jpg_settings;
  1820. /* we actually need to set 'real' parameters now */
  1821. if (fmt->fmt.pix.height * 2 > BUZ_MAX_HEIGHT)
  1822. settings.TmpDcm = 1;
  1823. else
  1824. settings.TmpDcm = 2;
  1825. settings.decimation = 0;
  1826. if (fmt->fmt.pix.height <= fh->jpg_settings.img_height / 2)
  1827. settings.VerDcm = 2;
  1828. else
  1829. settings.VerDcm = 1;
  1830. if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 4)
  1831. settings.HorDcm = 4;
  1832. else if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 2)
  1833. settings.HorDcm = 2;
  1834. else
  1835. settings.HorDcm = 1;
  1836. if (settings.TmpDcm == 1)
  1837. settings.field_per_buff = 2;
  1838. else
  1839. settings.field_per_buff = 1;
  1840. if (settings.HorDcm > 1) {
  1841. settings.img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
  1842. settings.img_width = (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
  1843. } else {
  1844. settings.img_x = 0;
  1845. settings.img_width = BUZ_MAX_WIDTH;
  1846. }
  1847. /* check */
  1848. res = zoran_check_jpg_settings(zr, &settings, 0);
  1849. if (res)
  1850. goto sfmtjpg_unlock_and_return;
  1851. /* it's ok, so set them */
  1852. fh->jpg_settings = settings;
  1853. map_mode_jpg(fh, fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT);
  1854. fh->buffers.buffer_size = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
  1855. /* tell the user what we actually did */
  1856. fmt->fmt.pix.width = settings.img_width / settings.HorDcm;
  1857. fmt->fmt.pix.height = settings.img_height * 2 /
  1858. (settings.TmpDcm * settings.VerDcm);
  1859. if (settings.TmpDcm == 1)
  1860. fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
  1861. V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
  1862. else
  1863. fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
  1864. V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
  1865. fmt->fmt.pix.bytesperline = 0;
  1866. fmt->fmt.pix.sizeimage = fh->buffers.buffer_size;
  1867. fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  1868. sfmtjpg_unlock_and_return:
  1869. mutex_unlock(&zr->resource_lock);
  1870. return res;
  1871. }
  1872. static int zoran_s_fmt_vid_cap(struct file *file, void *__fh,
  1873. struct v4l2_format *fmt)
  1874. {
  1875. struct zoran_fh *fh = __fh;
  1876. struct zoran *zr = fh->zr;
  1877. int i;
  1878. int res = 0;
  1879. if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
  1880. return zoran_s_fmt_vid_out(file, fh, fmt);
  1881. for (i = 0; i < NUM_FORMATS; i++)
  1882. if (fmt->fmt.pix.pixelformat == zoran_formats[i].fourcc)
  1883. break;
  1884. if (i == NUM_FORMATS) {
  1885. dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x\n",
  1886. ZR_DEVNAME(zr), fmt->fmt.pix.pixelformat);
  1887. return -EINVAL;
  1888. }
  1889. mutex_lock(&zr->resource_lock);
  1890. if ((fh->map_mode != ZORAN_MAP_MODE_RAW && fh->buffers.allocated) ||
  1891. fh->buffers.active != ZORAN_FREE) {
  1892. dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - cannot change capture mode\n",
  1893. ZR_DEVNAME(zr));
  1894. res = -EBUSY;
  1895. goto sfmtv4l_unlock_and_return;
  1896. }
  1897. if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
  1898. fmt->fmt.pix.height = BUZ_MAX_HEIGHT;
  1899. if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
  1900. fmt->fmt.pix.width = BUZ_MAX_WIDTH;
  1901. map_mode_raw(fh);
  1902. res = zoran_v4l_set_format(fh, fmt->fmt.pix.width, fmt->fmt.pix.height,
  1903. &zoran_formats[i]);
  1904. if (res)
  1905. goto sfmtv4l_unlock_and_return;
  1906. /* tell the user the results/missing stuff */
  1907. fmt->fmt.pix.bytesperline = fh->v4l_settings.bytesperline;
  1908. fmt->fmt.pix.sizeimage = fh->v4l_settings.height * fh->v4l_settings.bytesperline;
  1909. fmt->fmt.pix.colorspace = fh->v4l_settings.format->colorspace;
  1910. if (BUZ_MAX_HEIGHT < (fh->v4l_settings.height * 2))
  1911. fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
  1912. else
  1913. fmt->fmt.pix.field = V4L2_FIELD_TOP;
  1914. sfmtv4l_unlock_and_return:
  1915. mutex_unlock(&zr->resource_lock);
  1916. return res;
  1917. }
  1918. static int zoran_g_fbuf(struct file *file, void *__fh,
  1919. struct v4l2_framebuffer *fb)
  1920. {
  1921. struct zoran_fh *fh = __fh;
  1922. struct zoran *zr = fh->zr;
  1923. memset(fb, 0, sizeof(*fb));
  1924. mutex_lock(&zr->resource_lock);
  1925. fb->base = zr->vbuf_base;
  1926. fb->fmt.width = zr->vbuf_width;
  1927. fb->fmt.height = zr->vbuf_height;
  1928. if (zr->overlay_settings.format)
  1929. fb->fmt.pixelformat = fh->overlay_settings.format->fourcc;
  1930. fb->fmt.bytesperline = zr->vbuf_bytesperline;
  1931. mutex_unlock(&zr->resource_lock);
  1932. fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
  1933. fb->fmt.field = V4L2_FIELD_INTERLACED;
  1934. fb->flags = V4L2_FBUF_FLAG_OVERLAY;
  1935. fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
  1936. return 0;
  1937. }
  1938. static int zoran_s_fbuf(struct file *file, void *__fh,
  1939. struct v4l2_framebuffer *fb)
  1940. {
  1941. struct zoran_fh *fh = __fh;
  1942. struct zoran *zr = fh->zr;
  1943. int i, res = 0;
  1944. __le32 printformat = __cpu_to_le32(fb->fmt.pixelformat);
  1945. for (i = 0; i < NUM_FORMATS; i++)
  1946. if (zoran_formats[i].fourcc == fb->fmt.pixelformat)
  1947. break;
  1948. if (i == NUM_FORMATS) {
  1949. dprintk(1, KERN_ERR "%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
  1950. ZR_DEVNAME(zr), fb->fmt.pixelformat,
  1951. (char *)&printformat);
  1952. return -EINVAL;
  1953. }
  1954. mutex_lock(&zr->resource_lock);
  1955. res = setup_fbuffer(fh, fb->base, &zoran_formats[i], fb->fmt.width,
  1956. fb->fmt.height, fb->fmt.bytesperline);
  1957. mutex_unlock(&zr->resource_lock);
  1958. return res;
  1959. }
  1960. static int zoran_overlay(struct file *file, void *__fh, unsigned int on)
  1961. {
  1962. struct zoran_fh *fh = __fh;
  1963. struct zoran *zr = fh->zr;
  1964. int res;
  1965. mutex_lock(&zr->resource_lock);
  1966. res = setup_overlay(fh, on);
  1967. mutex_unlock(&zr->resource_lock);
  1968. return res;
  1969. }
  1970. static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type);
  1971. static int zoran_reqbufs(struct file *file, void *__fh, struct v4l2_requestbuffers *req)
  1972. {
  1973. struct zoran_fh *fh = __fh;
  1974. struct zoran *zr = fh->zr;
  1975. int res = 0;
  1976. if (req->memory != V4L2_MEMORY_MMAP) {
  1977. dprintk(2,
  1978. KERN_ERR
  1979. "%s: only MEMORY_MMAP capture is supported, not %d\n",
  1980. ZR_DEVNAME(zr), req->memory);
  1981. return -EINVAL;
  1982. }
  1983. if (req->count == 0)
  1984. return zoran_streamoff(file, fh, req->type);
  1985. mutex_lock(&zr->resource_lock);
  1986. if (fh->buffers.allocated) {
  1987. dprintk(2,
  1988. KERN_ERR
  1989. "%s: VIDIOC_REQBUFS - buffers already allocated\n",
  1990. ZR_DEVNAME(zr));
  1991. res = -EBUSY;
  1992. goto v4l2reqbuf_unlock_and_return;
  1993. }
  1994. if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
  1995. req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1996. /* control user input */
  1997. if (req->count < 2)
  1998. req->count = 2;
  1999. if (req->count > v4l_nbufs)
  2000. req->count = v4l_nbufs;
  2001. /* The next mmap will map the V4L buffers */
  2002. map_mode_raw(fh);
  2003. fh->buffers.num_buffers = req->count;
  2004. if (v4l_fbuffer_alloc(fh)) {
  2005. res = -ENOMEM;
  2006. goto v4l2reqbuf_unlock_and_return;
  2007. }
  2008. } else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
  2009. fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
  2010. /* we need to calculate size ourselves now */
  2011. if (req->count < 4)
  2012. req->count = 4;
  2013. if (req->count > jpg_nbufs)
  2014. req->count = jpg_nbufs;
  2015. /* The next mmap will map the MJPEG buffers */
  2016. map_mode_jpg(fh, req->type == V4L2_BUF_TYPE_VIDEO_OUTPUT);
  2017. fh->buffers.num_buffers = req->count;
  2018. fh->buffers.buffer_size = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
  2019. if (jpg_fbuffer_alloc(fh)) {
  2020. res = -ENOMEM;
  2021. goto v4l2reqbuf_unlock_and_return;
  2022. }
  2023. } else {
  2024. dprintk(1,
  2025. KERN_ERR
  2026. "%s: VIDIOC_REQBUFS - unknown type %d\n",
  2027. ZR_DEVNAME(zr), req->type);
  2028. res = -EINVAL;
  2029. goto v4l2reqbuf_unlock_and_return;
  2030. }
  2031. v4l2reqbuf_unlock_and_return:
  2032. mutex_unlock(&zr->resource_lock);
  2033. return res;
  2034. }
  2035. static int zoran_querybuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
  2036. {
  2037. struct zoran_fh *fh = __fh;
  2038. struct zoran *zr = fh->zr;
  2039. int res;
  2040. mutex_lock(&zr->resource_lock);
  2041. res = zoran_v4l2_buffer_status(fh, buf, buf->index);
  2042. mutex_unlock(&zr->resource_lock);
  2043. return res;
  2044. }
  2045. static int zoran_qbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
  2046. {
  2047. struct zoran_fh *fh = __fh;
  2048. struct zoran *zr = fh->zr;
  2049. int res = 0, codec_mode, buf_type;
  2050. mutex_lock(&zr->resource_lock);
  2051. switch (fh->map_mode) {
  2052. case ZORAN_MAP_MODE_RAW:
  2053. if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  2054. dprintk(1, KERN_ERR
  2055. "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
  2056. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  2057. res = -EINVAL;
  2058. goto qbuf_unlock_and_return;
  2059. }
  2060. res = zoran_v4l_queue_frame(fh, buf->index);
  2061. if (res)
  2062. goto qbuf_unlock_and_return;
  2063. if (!zr->v4l_memgrab_active && fh->buffers.active == ZORAN_LOCKED)
  2064. zr36057_set_memgrab(zr, 1);
  2065. break;
  2066. case ZORAN_MAP_MODE_JPG_REC:
  2067. case ZORAN_MAP_MODE_JPG_PLAY:
  2068. if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
  2069. buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  2070. codec_mode = BUZ_MODE_MOTION_DECOMPRESS;
  2071. } else {
  2072. buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  2073. codec_mode = BUZ_MODE_MOTION_COMPRESS;
  2074. }
  2075. if (buf->type != buf_type) {
  2076. dprintk(1, KERN_ERR
  2077. "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
  2078. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  2079. res = -EINVAL;
  2080. goto qbuf_unlock_and_return;
  2081. }
  2082. res = zoran_jpg_queue_frame(fh, buf->index, codec_mode);
  2083. if (res != 0)
  2084. goto qbuf_unlock_and_return;
  2085. if (zr->codec_mode == BUZ_MODE_IDLE &&
  2086. fh->buffers.active == ZORAN_LOCKED)
  2087. zr36057_enable_jpg(zr, codec_mode);
  2088. break;
  2089. default:
  2090. dprintk(1, KERN_ERR
  2091. "%s: VIDIOC_QBUF - unsupported type %d\n",
  2092. ZR_DEVNAME(zr), buf->type);
  2093. res = -EINVAL;
  2094. break;
  2095. }
  2096. qbuf_unlock_and_return:
  2097. mutex_unlock(&zr->resource_lock);
  2098. return res;
  2099. }
  2100. static int zoran_dqbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
  2101. {
  2102. struct zoran_fh *fh = __fh;
  2103. struct zoran *zr = fh->zr;
  2104. int res = 0, buf_type, num = -1; /* compiler borks here (?) */
  2105. mutex_lock(&zr->resource_lock);
  2106. switch (fh->map_mode) {
  2107. case ZORAN_MAP_MODE_RAW:
  2108. if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  2109. dprintk(1, KERN_ERR
  2110. "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
  2111. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  2112. res = -EINVAL;
  2113. goto dqbuf_unlock_and_return;
  2114. }
  2115. num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
  2116. if (file->f_flags & O_NONBLOCK &&
  2117. zr->v4l_buffers.buffer[num].state != BUZ_STATE_DONE) {
  2118. res = -EAGAIN;
  2119. goto dqbuf_unlock_and_return;
  2120. }
  2121. res = v4l_sync(fh, num);
  2122. if (res)
  2123. goto dqbuf_unlock_and_return;
  2124. zr->v4l_sync_tail++;
  2125. res = zoran_v4l2_buffer_status(fh, buf, num);
  2126. break;
  2127. case ZORAN_MAP_MODE_JPG_REC:
  2128. case ZORAN_MAP_MODE_JPG_PLAY:
  2129. {
  2130. struct zoran_sync bs;
  2131. if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
  2132. buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  2133. else
  2134. buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  2135. if (buf->type != buf_type) {
  2136. dprintk(1, KERN_ERR
  2137. "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
  2138. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  2139. res = -EINVAL;
  2140. goto dqbuf_unlock_and_return;
  2141. }
  2142. num = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
  2143. if (file->f_flags & O_NONBLOCK &&
  2144. zr->jpg_buffers.buffer[num].state != BUZ_STATE_DONE) {
  2145. res = -EAGAIN;
  2146. goto dqbuf_unlock_and_return;
  2147. }
  2148. res = jpg_sync(fh, &bs);
  2149. if (res)
  2150. goto dqbuf_unlock_and_return;
  2151. res = zoran_v4l2_buffer_status(fh, buf, bs.frame);
  2152. break;
  2153. }
  2154. default:
  2155. dprintk(1, KERN_ERR
  2156. "%s: VIDIOC_DQBUF - unsupported type %d\n",
  2157. ZR_DEVNAME(zr), buf->type);
  2158. res = -EINVAL;
  2159. break;
  2160. }
  2161. dqbuf_unlock_and_return:
  2162. mutex_unlock(&zr->resource_lock);
  2163. return res;
  2164. }
  2165. static int zoran_streamon(struct file *file, void *__fh, enum v4l2_buf_type type)
  2166. {
  2167. struct zoran_fh *fh = __fh;
  2168. struct zoran *zr = fh->zr;
  2169. int res = 0;
  2170. mutex_lock(&zr->resource_lock);
  2171. switch (fh->map_mode) {
  2172. case ZORAN_MAP_MODE_RAW: /* raw capture */
  2173. if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
  2174. fh->buffers.active != ZORAN_ACTIVE) {
  2175. res = -EBUSY;
  2176. goto strmon_unlock_and_return;
  2177. }
  2178. zr->v4l_buffers.active = fh->buffers.active = ZORAN_LOCKED;
  2179. zr->v4l_settings = fh->v4l_settings;
  2180. zr->v4l_sync_tail = zr->v4l_pend_tail;
  2181. if (!zr->v4l_memgrab_active &&
  2182. zr->v4l_pend_head != zr->v4l_pend_tail) {
  2183. zr36057_set_memgrab(zr, 1);
  2184. }
  2185. break;
  2186. case ZORAN_MAP_MODE_JPG_REC:
  2187. case ZORAN_MAP_MODE_JPG_PLAY:
  2188. /* what is the codec mode right now? */
  2189. if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
  2190. fh->buffers.active != ZORAN_ACTIVE) {
  2191. res = -EBUSY;
  2192. goto strmon_unlock_and_return;
  2193. }
  2194. zr->jpg_buffers.active = fh->buffers.active = ZORAN_LOCKED;
  2195. if (zr->jpg_que_head != zr->jpg_que_tail) {
  2196. /* Start the jpeg codec when the first frame is queued */
  2197. jpeg_start(zr);
  2198. }
  2199. break;
  2200. default:
  2201. dprintk(1,
  2202. KERN_ERR
  2203. "%s: VIDIOC_STREAMON - invalid map mode %d\n",
  2204. ZR_DEVNAME(zr), fh->map_mode);
  2205. res = -EINVAL;
  2206. break;
  2207. }
  2208. strmon_unlock_and_return:
  2209. mutex_unlock(&zr->resource_lock);
  2210. return res;
  2211. }
  2212. static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type)
  2213. {
  2214. struct zoran_fh *fh = __fh;
  2215. struct zoran *zr = fh->zr;
  2216. int i, res = 0;
  2217. unsigned long flags;
  2218. mutex_lock(&zr->resource_lock);
  2219. switch (fh->map_mode) {
  2220. case ZORAN_MAP_MODE_RAW: /* raw capture */
  2221. if (fh->buffers.active == ZORAN_FREE &&
  2222. zr->v4l_buffers.active != ZORAN_FREE) {
  2223. res = -EPERM; /* stay off other's settings! */
  2224. goto strmoff_unlock_and_return;
  2225. }
  2226. if (zr->v4l_buffers.active == ZORAN_FREE)
  2227. goto strmoff_unlock_and_return;
  2228. spin_lock_irqsave(&zr->spinlock, flags);
  2229. /* unload capture */
  2230. if (zr->v4l_memgrab_active) {
  2231. zr36057_set_memgrab(zr, 0);
  2232. }
  2233. for (i = 0; i < fh->buffers.num_buffers; i++)
  2234. zr->v4l_buffers.buffer[i].state = BUZ_STATE_USER;
  2235. fh->buffers = zr->v4l_buffers;
  2236. zr->v4l_buffers.active = fh->buffers.active = ZORAN_FREE;
  2237. zr->v4l_grab_seq = 0;
  2238. zr->v4l_pend_head = zr->v4l_pend_tail = 0;
  2239. zr->v4l_sync_tail = 0;
  2240. spin_unlock_irqrestore(&zr->spinlock, flags);
  2241. break;
  2242. case ZORAN_MAP_MODE_JPG_REC:
  2243. case ZORAN_MAP_MODE_JPG_PLAY:
  2244. if (fh->buffers.active == ZORAN_FREE &&
  2245. zr->jpg_buffers.active != ZORAN_FREE) {
  2246. res = -EPERM; /* stay off other's settings! */
  2247. goto strmoff_unlock_and_return;
  2248. }
  2249. if (zr->jpg_buffers.active == ZORAN_FREE)
  2250. goto strmoff_unlock_and_return;
  2251. res = jpg_qbuf(fh, -1,
  2252. (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
  2253. BUZ_MODE_MOTION_COMPRESS :
  2254. BUZ_MODE_MOTION_DECOMPRESS);
  2255. if (res)
  2256. goto strmoff_unlock_and_return;
  2257. break;
  2258. default:
  2259. dprintk(1, KERN_ERR
  2260. "%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
  2261. ZR_DEVNAME(zr), fh->map_mode);
  2262. res = -EINVAL;
  2263. break;
  2264. }
  2265. strmoff_unlock_and_return:
  2266. mutex_unlock(&zr->resource_lock);
  2267. return res;
  2268. }
  2269. static int zoran_queryctrl(struct file *file, void *__fh,
  2270. struct v4l2_queryctrl *ctrl)
  2271. {
  2272. struct zoran_fh *fh = __fh;
  2273. struct zoran *zr = fh->zr;
  2274. /* we only support hue/saturation/contrast/brightness */
  2275. if (ctrl->id < V4L2_CID_BRIGHTNESS ||
  2276. ctrl->id > V4L2_CID_HUE)
  2277. return -EINVAL;
  2278. decoder_call(zr, core, queryctrl, ctrl);
  2279. return 0;
  2280. }
  2281. static int zoran_g_ctrl(struct file *file, void *__fh, struct v4l2_control *ctrl)
  2282. {
  2283. struct zoran_fh *fh = __fh;
  2284. struct zoran *zr = fh->zr;
  2285. /* we only support hue/saturation/contrast/brightness */
  2286. if (ctrl->id < V4L2_CID_BRIGHTNESS ||
  2287. ctrl->id > V4L2_CID_HUE)
  2288. return -EINVAL;
  2289. mutex_lock(&zr->resource_lock);
  2290. decoder_call(zr, core, g_ctrl, ctrl);
  2291. mutex_unlock(&zr->resource_lock);
  2292. return 0;
  2293. }
  2294. static int zoran_s_ctrl(struct file *file, void *__fh, struct v4l2_control *ctrl)
  2295. {
  2296. struct zoran_fh *fh = __fh;
  2297. struct zoran *zr = fh->zr;
  2298. /* we only support hue/saturation/contrast/brightness */
  2299. if (ctrl->id < V4L2_CID_BRIGHTNESS ||
  2300. ctrl->id > V4L2_CID_HUE)
  2301. return -EINVAL;
  2302. mutex_lock(&zr->resource_lock);
  2303. decoder_call(zr, core, s_ctrl, ctrl);
  2304. mutex_unlock(&zr->resource_lock);
  2305. return 0;
  2306. }
  2307. static int zoran_g_std(struct file *file, void *__fh, v4l2_std_id *std)
  2308. {
  2309. struct zoran_fh *fh = __fh;
  2310. struct zoran *zr = fh->zr;
  2311. mutex_lock(&zr->resource_lock);
  2312. *std = zr->norm;
  2313. mutex_unlock(&zr->resource_lock);
  2314. return 0;
  2315. }
  2316. static int zoran_s_std(struct file *file, void *__fh, v4l2_std_id *std)
  2317. {
  2318. struct zoran_fh *fh = __fh;
  2319. struct zoran *zr = fh->zr;
  2320. int res = 0;
  2321. mutex_lock(&zr->resource_lock);
  2322. res = zoran_set_norm(zr, *std);
  2323. if (res)
  2324. goto sstd_unlock_and_return;
  2325. res = wait_grab_pending(zr);
  2326. sstd_unlock_and_return:
  2327. mutex_unlock(&zr->resource_lock);
  2328. return res;
  2329. }
  2330. static int zoran_enum_input(struct file *file, void *__fh,
  2331. struct v4l2_input *inp)
  2332. {
  2333. struct zoran_fh *fh = __fh;
  2334. struct zoran *zr = fh->zr;
  2335. if (inp->index < 0 || inp->index >= zr->card.inputs)
  2336. return -EINVAL;
  2337. else {
  2338. int id = inp->index;
  2339. memset(inp, 0, sizeof(*inp));
  2340. inp->index = id;
  2341. }
  2342. strncpy(inp->name, zr->card.input[inp->index].name,
  2343. sizeof(inp->name) - 1);
  2344. inp->type = V4L2_INPUT_TYPE_CAMERA;
  2345. inp->std = V4L2_STD_ALL;
  2346. /* Get status of video decoder */
  2347. mutex_lock(&zr->resource_lock);
  2348. decoder_call(zr, video, g_input_status, &inp->status);
  2349. mutex_unlock(&zr->resource_lock);
  2350. return 0;
  2351. }
  2352. static int zoran_g_input(struct file *file, void *__fh, unsigned int *input)
  2353. {
  2354. struct zoran_fh *fh = __fh;
  2355. struct zoran *zr = fh->zr;
  2356. mutex_lock(&zr->resource_lock);
  2357. *input = zr->input;
  2358. mutex_unlock(&zr->resource_lock);
  2359. return 0;
  2360. }
  2361. static int zoran_s_input(struct file *file, void *__fh, unsigned int input)
  2362. {
  2363. struct zoran_fh *fh = __fh;
  2364. struct zoran *zr = fh->zr;
  2365. int res;
  2366. mutex_lock(&zr->resource_lock);
  2367. res = zoran_set_input(zr, input);
  2368. if (res)
  2369. goto sinput_unlock_and_return;
  2370. /* Make sure the changes come into effect */
  2371. res = wait_grab_pending(zr);
  2372. sinput_unlock_and_return:
  2373. mutex_unlock(&zr->resource_lock);
  2374. return res;
  2375. }
  2376. static int zoran_enum_output(struct file *file, void *__fh,
  2377. struct v4l2_output *outp)
  2378. {
  2379. if (outp->index != 0)
  2380. return -EINVAL;
  2381. memset(outp, 0, sizeof(*outp));
  2382. outp->index = 0;
  2383. outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
  2384. strncpy(outp->name, "Autodetect", sizeof(outp->name)-1);
  2385. return 0;
  2386. }
  2387. static int zoran_g_output(struct file *file, void *__fh, unsigned int *output)
  2388. {
  2389. *output = 0;
  2390. return 0;
  2391. }
  2392. static int zoran_s_output(struct file *file, void *__fh, unsigned int output)
  2393. {
  2394. if (output != 0)
  2395. return -EINVAL;
  2396. return 0;
  2397. }
  2398. /* cropping (sub-frame capture) */
  2399. static int zoran_cropcap(struct file *file, void *__fh,
  2400. struct v4l2_cropcap *cropcap)
  2401. {
  2402. struct zoran_fh *fh = __fh;
  2403. struct zoran *zr = fh->zr;
  2404. int type = cropcap->type, res = 0;
  2405. memset(cropcap, 0, sizeof(*cropcap));
  2406. cropcap->type = type;
  2407. mutex_lock(&zr->resource_lock);
  2408. if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  2409. (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  2410. fh->map_mode == ZORAN_MAP_MODE_RAW)) {
  2411. dprintk(1, KERN_ERR
  2412. "%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
  2413. ZR_DEVNAME(zr));
  2414. res = -EINVAL;
  2415. goto cropcap_unlock_and_return;
  2416. }
  2417. cropcap->bounds.top = cropcap->bounds.left = 0;
  2418. cropcap->bounds.width = BUZ_MAX_WIDTH;
  2419. cropcap->bounds.height = BUZ_MAX_HEIGHT;
  2420. cropcap->defrect.top = cropcap->defrect.left = 0;
  2421. cropcap->defrect.width = BUZ_MIN_WIDTH;
  2422. cropcap->defrect.height = BUZ_MIN_HEIGHT;
  2423. cropcap_unlock_and_return:
  2424. mutex_unlock(&zr->resource_lock);
  2425. return res;
  2426. }
  2427. static int zoran_g_crop(struct file *file, void *__fh, struct v4l2_crop *crop)
  2428. {
  2429. struct zoran_fh *fh = __fh;
  2430. struct zoran *zr = fh->zr;
  2431. int type = crop->type, res = 0;
  2432. memset(crop, 0, sizeof(*crop));
  2433. crop->type = type;
  2434. mutex_lock(&zr->resource_lock);
  2435. if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  2436. (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  2437. fh->map_mode == ZORAN_MAP_MODE_RAW)) {
  2438. dprintk(1,
  2439. KERN_ERR
  2440. "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
  2441. ZR_DEVNAME(zr));
  2442. res = -EINVAL;
  2443. goto gcrop_unlock_and_return;
  2444. }
  2445. crop->c.top = fh->jpg_settings.img_y;
  2446. crop->c.left = fh->jpg_settings.img_x;
  2447. crop->c.width = fh->jpg_settings.img_width;
  2448. crop->c.height = fh->jpg_settings.img_height;
  2449. gcrop_unlock_and_return:
  2450. mutex_unlock(&zr->resource_lock);
  2451. return res;
  2452. }
  2453. static int zoran_s_crop(struct file *file, void *__fh, struct v4l2_crop *crop)
  2454. {
  2455. struct zoran_fh *fh = __fh;
  2456. struct zoran *zr = fh->zr;
  2457. int res = 0;
  2458. struct zoran_jpg_settings settings;
  2459. settings = fh->jpg_settings;
  2460. mutex_lock(&zr->resource_lock);
  2461. if (fh->buffers.allocated) {
  2462. dprintk(1, KERN_ERR
  2463. "%s: VIDIOC_S_CROP - cannot change settings while active\n",
  2464. ZR_DEVNAME(zr));
  2465. res = -EBUSY;
  2466. goto scrop_unlock_and_return;
  2467. }
  2468. if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  2469. (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  2470. fh->map_mode == ZORAN_MAP_MODE_RAW)) {
  2471. dprintk(1, KERN_ERR
  2472. "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
  2473. ZR_DEVNAME(zr));
  2474. res = -EINVAL;
  2475. goto scrop_unlock_and_return;
  2476. }
  2477. /* move into a form that we understand */
  2478. settings.img_x = crop->c.left;
  2479. settings.img_y = crop->c.top;
  2480. settings.img_width = crop->c.width;
  2481. settings.img_height = crop->c.height;
  2482. /* check validity */
  2483. res = zoran_check_jpg_settings(zr, &settings, 0);
  2484. if (res)
  2485. goto scrop_unlock_and_return;
  2486. /* accept */
  2487. fh->jpg_settings = settings;
  2488. scrop_unlock_and_return:
  2489. mutex_unlock(&zr->resource_lock);
  2490. return res;
  2491. }
  2492. static int zoran_g_jpegcomp(struct file *file, void *__fh,
  2493. struct v4l2_jpegcompression *params)
  2494. {
  2495. struct zoran_fh *fh = __fh;
  2496. struct zoran *zr = fh->zr;
  2497. memset(params, 0, sizeof(*params));
  2498. mutex_lock(&zr->resource_lock);
  2499. params->quality = fh->jpg_settings.jpg_comp.quality;
  2500. params->APPn = fh->jpg_settings.jpg_comp.APPn;
  2501. memcpy(params->APP_data,
  2502. fh->jpg_settings.jpg_comp.APP_data,
  2503. fh->jpg_settings.jpg_comp.APP_len);
  2504. params->APP_len = fh->jpg_settings.jpg_comp.APP_len;
  2505. memcpy(params->COM_data,
  2506. fh->jpg_settings.jpg_comp.COM_data,
  2507. fh->jpg_settings.jpg_comp.COM_len);
  2508. params->COM_len = fh->jpg_settings.jpg_comp.COM_len;
  2509. params->jpeg_markers =
  2510. fh->jpg_settings.jpg_comp.jpeg_markers;
  2511. mutex_unlock(&zr->resource_lock);
  2512. return 0;
  2513. }
  2514. static int zoran_s_jpegcomp(struct file *file, void *__fh,
  2515. struct v4l2_jpegcompression *params)
  2516. {
  2517. struct zoran_fh *fh = __fh;
  2518. struct zoran *zr = fh->zr;
  2519. int res = 0;
  2520. struct zoran_jpg_settings settings;
  2521. settings = fh->jpg_settings;
  2522. settings.jpg_comp = *params;
  2523. mutex_lock(&zr->resource_lock);
  2524. if (fh->buffers.active != ZORAN_FREE) {
  2525. dprintk(1, KERN_WARNING
  2526. "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
  2527. ZR_DEVNAME(zr));
  2528. res = -EBUSY;
  2529. goto sjpegc_unlock_and_return;
  2530. }
  2531. res = zoran_check_jpg_settings(zr, &settings, 0);
  2532. if (res)
  2533. goto sjpegc_unlock_and_return;
  2534. if (!fh->buffers.allocated)
  2535. fh->buffers.buffer_size =
  2536. zoran_v4l2_calc_bufsize(&fh->jpg_settings);
  2537. fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
  2538. sjpegc_unlock_and_return:
  2539. mutex_unlock(&zr->resource_lock);
  2540. return res;
  2541. }
  2542. static unsigned int
  2543. zoran_poll (struct file *file,
  2544. poll_table *wait)
  2545. {
  2546. struct zoran_fh *fh = file->private_data;
  2547. struct zoran *zr = fh->zr;
  2548. int res = 0, frame;
  2549. unsigned long flags;
  2550. /* we should check whether buffers are ready to be synced on
  2551. * (w/o waits - O_NONBLOCK) here
  2552. * if ready for read (sync), return POLLIN|POLLRDNORM,
  2553. * if ready for write (sync), return POLLOUT|POLLWRNORM,
  2554. * if error, return POLLERR,
  2555. * if no buffers queued or so, return POLLNVAL
  2556. */
  2557. mutex_lock(&zr->resource_lock);
  2558. switch (fh->map_mode) {
  2559. case ZORAN_MAP_MODE_RAW:
  2560. poll_wait(file, &zr->v4l_capq, wait);
  2561. frame = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
  2562. spin_lock_irqsave(&zr->spinlock, flags);
  2563. dprintk(3,
  2564. KERN_DEBUG
  2565. "%s: %s() raw - active=%c, sync_tail=%lu/%c, pend_tail=%lu, pend_head=%lu\n",
  2566. ZR_DEVNAME(zr), __func__,
  2567. "FAL"[fh->buffers.active], zr->v4l_sync_tail,
  2568. "UPMD"[zr->v4l_buffers.buffer[frame].state],
  2569. zr->v4l_pend_tail, zr->v4l_pend_head);
  2570. /* Process is the one capturing? */
  2571. if (fh->buffers.active != ZORAN_FREE &&
  2572. /* Buffer ready to DQBUF? */
  2573. zr->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
  2574. res = POLLIN | POLLRDNORM;
  2575. spin_unlock_irqrestore(&zr->spinlock, flags);
  2576. break;
  2577. case ZORAN_MAP_MODE_JPG_REC:
  2578. case ZORAN_MAP_MODE_JPG_PLAY:
  2579. poll_wait(file, &zr->jpg_capq, wait);
  2580. frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
  2581. spin_lock_irqsave(&zr->spinlock, flags);
  2582. dprintk(3,
  2583. KERN_DEBUG
  2584. "%s: %s() jpg - active=%c, que_tail=%lu/%c, que_head=%lu, dma=%lu/%lu\n",
  2585. ZR_DEVNAME(zr), __func__,
  2586. "FAL"[fh->buffers.active], zr->jpg_que_tail,
  2587. "UPMD"[zr->jpg_buffers.buffer[frame].state],
  2588. zr->jpg_que_head, zr->jpg_dma_tail, zr->jpg_dma_head);
  2589. if (fh->buffers.active != ZORAN_FREE &&
  2590. zr->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
  2591. if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
  2592. res = POLLIN | POLLRDNORM;
  2593. else
  2594. res = POLLOUT | POLLWRNORM;
  2595. }
  2596. spin_unlock_irqrestore(&zr->spinlock, flags);
  2597. break;
  2598. default:
  2599. dprintk(1,
  2600. KERN_ERR
  2601. "%s: %s - internal error, unknown map_mode=%d\n",
  2602. ZR_DEVNAME(zr), __func__, fh->map_mode);
  2603. res = POLLNVAL;
  2604. }
  2605. mutex_unlock(&zr->resource_lock);
  2606. return res;
  2607. }
  2608. /*
  2609. * This maps the buffers to user space.
  2610. *
  2611. * Depending on the state of fh->map_mode
  2612. * the V4L or the MJPEG buffers are mapped
  2613. * per buffer or all together
  2614. *
  2615. * Note that we need to connect to some
  2616. * unmap signal event to unmap the de-allocate
  2617. * the buffer accordingly (zoran_vm_close())
  2618. */
  2619. static void
  2620. zoran_vm_open (struct vm_area_struct *vma)
  2621. {
  2622. struct zoran_mapping *map = vma->vm_private_data;
  2623. map->count++;
  2624. }
  2625. static void
  2626. zoran_vm_close (struct vm_area_struct *vma)
  2627. {
  2628. struct zoran_mapping *map = vma->vm_private_data;
  2629. struct zoran_fh *fh = map->file->private_data;
  2630. struct zoran *zr = fh->zr;
  2631. int i;
  2632. if (--map->count > 0)
  2633. return;
  2634. dprintk(3, KERN_INFO "%s: %s - munmap(%s)\n", ZR_DEVNAME(zr),
  2635. __func__, mode_name(fh->map_mode));
  2636. for (i = 0; i < fh->buffers.num_buffers; i++) {
  2637. if (fh->buffers.buffer[i].map == map)
  2638. fh->buffers.buffer[i].map = NULL;
  2639. }
  2640. kfree(map);
  2641. /* Any buffers still mapped? */
  2642. for (i = 0; i < fh->buffers.num_buffers; i++)
  2643. if (fh->buffers.buffer[i].map)
  2644. return;
  2645. dprintk(3, KERN_INFO "%s: %s - free %s buffers\n", ZR_DEVNAME(zr),
  2646. __func__, mode_name(fh->map_mode));
  2647. mutex_lock(&zr->resource_lock);
  2648. if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
  2649. if (fh->buffers.active != ZORAN_FREE) {
  2650. unsigned long flags;
  2651. spin_lock_irqsave(&zr->spinlock, flags);
  2652. zr36057_set_memgrab(zr, 0);
  2653. zr->v4l_buffers.allocated = 0;
  2654. zr->v4l_buffers.active = fh->buffers.active = ZORAN_FREE;
  2655. spin_unlock_irqrestore(&zr->spinlock, flags);
  2656. }
  2657. v4l_fbuffer_free(fh);
  2658. } else {
  2659. if (fh->buffers.active != ZORAN_FREE) {
  2660. jpg_qbuf(fh, -1, zr->codec_mode);
  2661. zr->jpg_buffers.allocated = 0;
  2662. zr->jpg_buffers.active = fh->buffers.active = ZORAN_FREE;
  2663. }
  2664. jpg_fbuffer_free(fh);
  2665. }
  2666. mutex_unlock(&zr->resource_lock);
  2667. }
  2668. static const struct vm_operations_struct zoran_vm_ops = {
  2669. .open = zoran_vm_open,
  2670. .close = zoran_vm_close,
  2671. };
  2672. static int
  2673. zoran_mmap (struct file *file,
  2674. struct vm_area_struct *vma)
  2675. {
  2676. struct zoran_fh *fh = file->private_data;
  2677. struct zoran *zr = fh->zr;
  2678. unsigned long size = (vma->vm_end - vma->vm_start);
  2679. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  2680. int i, j;
  2681. unsigned long page, start = vma->vm_start, todo, pos, fraglen;
  2682. int first, last;
  2683. struct zoran_mapping *map;
  2684. int res = 0;
  2685. dprintk(3,
  2686. KERN_INFO "%s: %s(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
  2687. ZR_DEVNAME(zr), __func__,
  2688. mode_name(fh->map_mode), vma->vm_start, vma->vm_end, size);
  2689. if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
  2690. !(vma->vm_flags & VM_WRITE)) {
  2691. dprintk(1,
  2692. KERN_ERR
  2693. "%s: %s - no MAP_SHARED/PROT_{READ,WRITE} given\n",
  2694. ZR_DEVNAME(zr), __func__);
  2695. return -EINVAL;
  2696. }
  2697. mutex_lock(&zr->resource_lock);
  2698. if (!fh->buffers.allocated) {
  2699. dprintk(1,
  2700. KERN_ERR
  2701. "%s: %s(%s) - buffers not yet allocated\n",
  2702. ZR_DEVNAME(zr), __func__, mode_name(fh->map_mode));
  2703. res = -ENOMEM;
  2704. goto mmap_unlock_and_return;
  2705. }
  2706. first = offset / fh->buffers.buffer_size;
  2707. last = first - 1 + size / fh->buffers.buffer_size;
  2708. if (offset % fh->buffers.buffer_size != 0 ||
  2709. size % fh->buffers.buffer_size != 0 || first < 0 ||
  2710. last < 0 || first >= fh->buffers.num_buffers ||
  2711. last >= fh->buffers.buffer_size) {
  2712. dprintk(1,
  2713. KERN_ERR
  2714. "%s: %s(%s) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
  2715. ZR_DEVNAME(zr), __func__, mode_name(fh->map_mode), offset, size,
  2716. fh->buffers.buffer_size,
  2717. fh->buffers.num_buffers);
  2718. res = -EINVAL;
  2719. goto mmap_unlock_and_return;
  2720. }
  2721. /* Check if any buffers are already mapped */
  2722. for (i = first; i <= last; i++) {
  2723. if (fh->buffers.buffer[i].map) {
  2724. dprintk(1,
  2725. KERN_ERR
  2726. "%s: %s(%s) - buffer %d already mapped\n",
  2727. ZR_DEVNAME(zr), __func__, mode_name(fh->map_mode), i);
  2728. res = -EBUSY;
  2729. goto mmap_unlock_and_return;
  2730. }
  2731. }
  2732. /* map these buffers */
  2733. map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
  2734. if (!map) {
  2735. res = -ENOMEM;
  2736. goto mmap_unlock_and_return;
  2737. }
  2738. map->file = file;
  2739. map->count = 1;
  2740. vma->vm_ops = &zoran_vm_ops;
  2741. vma->vm_flags |= VM_DONTEXPAND;
  2742. vma->vm_private_data = map;
  2743. if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
  2744. for (i = first; i <= last; i++) {
  2745. todo = size;
  2746. if (todo > fh->buffers.buffer_size)
  2747. todo = fh->buffers.buffer_size;
  2748. page = fh->buffers.buffer[i].v4l.fbuffer_phys;
  2749. if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
  2750. todo, PAGE_SHARED)) {
  2751. dprintk(1,
  2752. KERN_ERR
  2753. "%s: %s(V4L) - remap_pfn_range failed\n",
  2754. ZR_DEVNAME(zr), __func__);
  2755. res = -EAGAIN;
  2756. goto mmap_unlock_and_return;
  2757. }
  2758. size -= todo;
  2759. start += todo;
  2760. fh->buffers.buffer[i].map = map;
  2761. if (size == 0)
  2762. break;
  2763. }
  2764. } else {
  2765. for (i = first; i <= last; i++) {
  2766. for (j = 0;
  2767. j < fh->buffers.buffer_size / PAGE_SIZE;
  2768. j++) {
  2769. fraglen =
  2770. (le32_to_cpu(fh->buffers.buffer[i].jpg.
  2771. frag_tab[2 * j + 1]) & ~1) << 1;
  2772. todo = size;
  2773. if (todo > fraglen)
  2774. todo = fraglen;
  2775. pos =
  2776. le32_to_cpu(fh->buffers.
  2777. buffer[i].jpg.frag_tab[2 * j]);
  2778. /* should just be pos on i386 */
  2779. page = virt_to_phys(bus_to_virt(pos))
  2780. >> PAGE_SHIFT;
  2781. if (remap_pfn_range(vma, start, page,
  2782. todo, PAGE_SHARED)) {
  2783. dprintk(1,
  2784. KERN_ERR
  2785. "%s: %s(V4L) - remap_pfn_range failed\n",
  2786. ZR_DEVNAME(zr), __func__);
  2787. res = -EAGAIN;
  2788. goto mmap_unlock_and_return;
  2789. }
  2790. size -= todo;
  2791. start += todo;
  2792. if (size == 0)
  2793. break;
  2794. if (le32_to_cpu(fh->buffers.buffer[i].jpg.
  2795. frag_tab[2 * j + 1]) & 1)
  2796. break; /* was last fragment */
  2797. }
  2798. fh->buffers.buffer[i].map = map;
  2799. if (size == 0)
  2800. break;
  2801. }
  2802. }
  2803. mmap_unlock_and_return:
  2804. mutex_unlock(&zr->resource_lock);
  2805. return 0;
  2806. }
  2807. static const struct v4l2_ioctl_ops zoran_ioctl_ops = {
  2808. .vidioc_querycap = zoran_querycap,
  2809. .vidioc_cropcap = zoran_cropcap,
  2810. .vidioc_s_crop = zoran_s_crop,
  2811. .vidioc_g_crop = zoran_g_crop,
  2812. .vidioc_enum_input = zoran_enum_input,
  2813. .vidioc_g_input = zoran_g_input,
  2814. .vidioc_s_input = zoran_s_input,
  2815. .vidioc_enum_output = zoran_enum_output,
  2816. .vidioc_g_output = zoran_g_output,
  2817. .vidioc_s_output = zoran_s_output,
  2818. .vidioc_g_fbuf = zoran_g_fbuf,
  2819. .vidioc_s_fbuf = zoran_s_fbuf,
  2820. .vidioc_g_std = zoran_g_std,
  2821. .vidioc_s_std = zoran_s_std,
  2822. .vidioc_g_jpegcomp = zoran_g_jpegcomp,
  2823. .vidioc_s_jpegcomp = zoran_s_jpegcomp,
  2824. .vidioc_overlay = zoran_overlay,
  2825. .vidioc_reqbufs = zoran_reqbufs,
  2826. .vidioc_querybuf = zoran_querybuf,
  2827. .vidioc_qbuf = zoran_qbuf,
  2828. .vidioc_dqbuf = zoran_dqbuf,
  2829. .vidioc_streamon = zoran_streamon,
  2830. .vidioc_streamoff = zoran_streamoff,
  2831. .vidioc_enum_fmt_vid_cap = zoran_enum_fmt_vid_cap,
  2832. .vidioc_enum_fmt_vid_out = zoran_enum_fmt_vid_out,
  2833. .vidioc_enum_fmt_vid_overlay = zoran_enum_fmt_vid_overlay,
  2834. .vidioc_g_fmt_vid_cap = zoran_g_fmt_vid_cap,
  2835. .vidioc_g_fmt_vid_out = zoran_g_fmt_vid_out,
  2836. .vidioc_g_fmt_vid_overlay = zoran_g_fmt_vid_overlay,
  2837. .vidioc_s_fmt_vid_cap = zoran_s_fmt_vid_cap,
  2838. .vidioc_s_fmt_vid_out = zoran_s_fmt_vid_out,
  2839. .vidioc_s_fmt_vid_overlay = zoran_s_fmt_vid_overlay,
  2840. .vidioc_try_fmt_vid_cap = zoran_try_fmt_vid_cap,
  2841. .vidioc_try_fmt_vid_out = zoran_try_fmt_vid_out,
  2842. .vidioc_try_fmt_vid_overlay = zoran_try_fmt_vid_overlay,
  2843. .vidioc_queryctrl = zoran_queryctrl,
  2844. .vidioc_s_ctrl = zoran_s_ctrl,
  2845. .vidioc_g_ctrl = zoran_g_ctrl,
  2846. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  2847. .vidioc_default = zoran_default,
  2848. .vidiocgmbuf = zoran_vidiocgmbuf,
  2849. #endif
  2850. };
  2851. static const struct v4l2_file_operations zoran_fops = {
  2852. .owner = THIS_MODULE,
  2853. .open = zoran_open,
  2854. .release = zoran_close,
  2855. .ioctl = video_ioctl2,
  2856. .read = zoran_read,
  2857. .write = zoran_write,
  2858. .mmap = zoran_mmap,
  2859. .poll = zoran_poll,
  2860. };
  2861. struct video_device zoran_template __devinitdata = {
  2862. .name = ZORAN_NAME,
  2863. .fops = &zoran_fops,
  2864. .ioctl_ops = &zoran_ioctl_ops,
  2865. .release = &zoran_vdev_release,
  2866. .tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
  2867. .minor = -1
  2868. };