zoran_driver.c 79 KB

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