zoran_driver.c 93 KB

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