zr36120.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072
  1. /*
  2. zr36120.c - Zoran 36120/36125 based framegrabbers
  3. Copyright (C) 1998-1999 Pauline Middelink <middelin@polyware.nl>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include <linux/module.h>
  17. #include <linux/delay.h>
  18. #include <linux/init.h>
  19. #include <linux/errno.h>
  20. #include <linux/fs.h>
  21. #include <linux/kernel.h>
  22. #include <linux/major.h>
  23. #include <linux/slab.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/mm.h>
  26. #include <linux/pci.h>
  27. #include <linux/signal.h>
  28. #include <linux/wait.h>
  29. #include <asm/io.h>
  30. #include <asm/pgtable.h>
  31. #include <asm/page.h>
  32. #include <linux/sched.h>
  33. #include <linux/video_decoder.h>
  34. #include <asm/uaccess.h>
  35. #include "tuner.h"
  36. #include "zr36120.h"
  37. #include "zr36120_mem.h"
  38. /* mark an required function argument unused - lintism */
  39. #define UNUSED(x) (void)(x)
  40. /* sensible default */
  41. #ifndef CARDTYPE
  42. #define CARDTYPE 0
  43. #endif
  44. /* Anybody who uses more than four? */
  45. #define ZORAN_MAX 4
  46. static unsigned int triton1=0; /* triton1 chipset? */
  47. static unsigned int cardtype[ZORAN_MAX]={ [ 0 ... ZORAN_MAX-1 ] = CARDTYPE };
  48. static int video_nr = -1;
  49. static int vbi_nr = -1;
  50. static struct pci_device_id zr36120_pci_tbl[] = {
  51. { PCI_VENDOR_ID_ZORAN,PCI_DEVICE_ID_ZORAN_36120,
  52. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  53. { 0 }
  54. };
  55. MODULE_DEVICE_TABLE(pci, zr36120_pci_tbl);
  56. MODULE_AUTHOR("Pauline Middelink <middelin@polyware.nl>");
  57. MODULE_DESCRIPTION("Zoran ZR36120 based framegrabber");
  58. MODULE_LICENSE("GPL");
  59. module_param(triton1, uint, 0);
  60. module_param_array(cardtype, uint, NULL, 0);
  61. module_param(video_nr, int, 0);
  62. module_param(vbi_nr, int, 0);
  63. static int zoran_cards;
  64. static struct zoran zorans[ZORAN_MAX];
  65. /*
  66. * the meaning of each element can be found in zr36120.h
  67. * Determining the value of gpdir/gpval can be tricky. The
  68. * best way is to run the card under the original software
  69. * and read the values from the general purpose registers
  70. * 0x28 and 0x2C. How you do that is left as an exercise
  71. * to the impatient reader :)
  72. */
  73. #define T 1 /* to separate the bools from the ints */
  74. #define F 0
  75. static struct tvcard tvcards[] = {
  76. /* reported working by <middelin@polyware.nl> */
  77. /*0*/ { "Trust Victor II",
  78. 2, 0, T, T, T, T, 0x7F, 0x80, { 1, SVHS(6) }, { 0 } },
  79. /* reported working by <Michael.Paxton@aihw.gov.au> */
  80. /*1*/ { "Aitech WaveWatcher TV-PCI",
  81. 3, 0, T, F, T, T, 0x7F, 0x80, { 1, TUNER(3), SVHS(6) }, { 0 } },
  82. /* reported working by ? */
  83. /*2*/ { "Genius Video Wonder PCI Video Capture Card",
  84. 2, 0, T, T, T, T, 0x7F, 0x80, { 1, SVHS(6) }, { 0 } },
  85. /* reported working by <Pascal.Gabriel@wanadoo.fr> */
  86. /*3*/ { "Guillemot Maxi-TV PCI",
  87. 2, 0, T, T, T, T, 0x7F, 0x80, { 1, SVHS(6) }, { 0 } },
  88. /* reported working by "Craig Whitmore <lennon@igrin.co.nz> */
  89. /*4*/ { "Quadrant Buster",
  90. 3, 3, T, F, T, T, 0x7F, 0x80, { SVHS(1), TUNER(2), 3 }, { 1, 2, 3 } },
  91. /* a debug entry which has all inputs mapped */
  92. /*5*/ { "ZR36120 based framegrabber (all inputs enabled)",
  93. 6, 0, T, T, T, T, 0x7F, 0x80, { 1, 2, 3, 4, 5, 6 }, { 0 } }
  94. };
  95. #undef T
  96. #undef F
  97. #define NRTVCARDS (sizeof(tvcards)/sizeof(tvcards[0]))
  98. #ifdef __sparc__
  99. #define ENDIANESS 0
  100. #else
  101. #define ENDIANESS ZORAN_VFEC_LE
  102. #endif
  103. static struct { const char name[8]; uint mode; uint bpp; } palette2fmt[] = {
  104. /* n/a */ { "n/a", 0, 0 },
  105. /* GREY */ { "GRAY", 0, 0 },
  106. /* HI240 */ { "HI240", 0, 0 },
  107. /* RGB565 */ { "RGB565", ZORAN_VFEC_RGB_RGB565|ENDIANESS, 2 },
  108. /* RGB24 */ { "RGB24", ZORAN_VFEC_RGB_RGB888|ENDIANESS|ZORAN_VFEC_PACK24, 3 },
  109. /* RGB32 */ { "RGB32", ZORAN_VFEC_RGB_RGB888|ENDIANESS, 4 },
  110. /* RGB555 */ { "RGB555", ZORAN_VFEC_RGB_RGB555|ENDIANESS, 2 },
  111. /* YUV422 */ { "YUV422", ZORAN_VFEC_RGB_YUV422|ENDIANESS, 2 },
  112. /* YUYV */ { "YUYV", 0, 0 },
  113. /* UYVY */ { "UYVY", 0, 0 },
  114. /* YUV420 */ { "YUV420", 0, 0 },
  115. /* YUV411 */ { "YUV411", 0, 0 },
  116. /* RAW */ { "RAW", 0, 0 },
  117. /* YUV422P */ { "YUV422P", 0, 0 },
  118. /* YUV411P */ { "YUV411P", 0, 0 }};
  119. #define NRPALETTES (sizeof(palette2fmt)/sizeof(palette2fmt[0]))
  120. #undef ENDIANESS
  121. /* ----------------------------------------------------------------------- */
  122. /* ZORAN chipset detector */
  123. /* shamelessly stolen from bttv.c */
  124. /* Reason for beeing here: we need to detect if we are running on a */
  125. /* Triton based chipset, and if so, enable a certain bit */
  126. /* ----------------------------------------------------------------------- */
  127. static
  128. void __init handle_chipset(void)
  129. {
  130. /* Just in case some nut set this to something dangerous */
  131. if (triton1)
  132. triton1 = ZORAN_VDC_TRICOM;
  133. if (pci_pci_problems & PCIPCI_TRITON) {
  134. printk(KERN_INFO "zoran: Host bridge 82437FX Triton PIIX\n");
  135. triton1 = ZORAN_VDC_TRICOM;
  136. }
  137. }
  138. /* ----------------------------------------------------------------------- */
  139. /* ZORAN functions */
  140. /* ----------------------------------------------------------------------- */
  141. static void zoran_set_geo(struct zoran* ztv, struct vidinfo* i);
  142. #if 0 /* unused */
  143. static
  144. void zoran_dump(struct zoran *ztv)
  145. {
  146. char str[256];
  147. char *p=str; /* shut up, gcc! */
  148. int i;
  149. for (i=0; i<0x60; i+=4) {
  150. if ((i % 16) == 0) {
  151. if (i) printk("%s\n",str);
  152. p = str;
  153. p+= sprintf(str, KERN_DEBUG " %04x: ",i);
  154. }
  155. p += sprintf(p, "%08x ",zrread(i));
  156. }
  157. }
  158. #endif /* unused */
  159. static
  160. void reap_states(struct zoran* ztv)
  161. {
  162. /* count frames */
  163. ztv->fieldnr++;
  164. /*
  165. * Are we busy at all?
  166. * This depends on if there is a workqueue AND the
  167. * videotransfer is enabled on the chip...
  168. */
  169. if (ztv->workqueue && (zrread(ZORAN_VDC) & ZORAN_VDC_VIDEN))
  170. {
  171. struct vidinfo* newitem;
  172. /* did we get a complete frame? */
  173. if (zrread(ZORAN_VSTR) & ZORAN_VSTR_GRAB)
  174. return;
  175. DEBUG(printk(CARD_DEBUG "completed %s at %p\n",CARD,ztv->workqueue->kindof==FBUFFER_GRAB?"grab":"read",ztv->workqueue));
  176. /* we are done with this buffer, tell everyone */
  177. ztv->workqueue->status = FBUFFER_DONE;
  178. ztv->workqueue->fieldnr = ztv->fieldnr;
  179. /* not good, here for BTTV_FIELDNR reasons */
  180. ztv->lastfieldnr = ztv->fieldnr;
  181. switch (ztv->workqueue->kindof) {
  182. case FBUFFER_GRAB:
  183. wake_up_interruptible(&ztv->grabq);
  184. break;
  185. case FBUFFER_VBI:
  186. wake_up_interruptible(&ztv->vbiq);
  187. break;
  188. default:
  189. printk(CARD_INFO "somebody killed the workqueue (kindof=%d)!\n",CARD,ztv->workqueue->kindof);
  190. }
  191. /* item completed, skip to next item in queue */
  192. write_lock(&ztv->lock);
  193. newitem = ztv->workqueue->next;
  194. ztv->workqueue->next = 0; /* mark completed */
  195. ztv->workqueue = newitem;
  196. write_unlock(&ztv->lock);
  197. }
  198. /*
  199. * ok, so it seems we have nothing in progress right now.
  200. * Lets see if we can find some work.
  201. */
  202. if (ztv->workqueue)
  203. {
  204. struct vidinfo* newitem;
  205. again:
  206. DEBUG(printk(CARD_DEBUG "starting %s at %p\n",CARD,ztv->workqueue->kindof==FBUFFER_GRAB?"grab":"read",ztv->workqueue));
  207. /* loadup the frame settings */
  208. read_lock(&ztv->lock);
  209. zoran_set_geo(ztv,ztv->workqueue);
  210. read_unlock(&ztv->lock);
  211. switch (ztv->workqueue->kindof) {
  212. case FBUFFER_GRAB:
  213. case FBUFFER_VBI:
  214. zrand(~ZORAN_OCR_OVLEN, ZORAN_OCR);
  215. zror(ZORAN_VSTR_SNAPSHOT,ZORAN_VSTR);
  216. zror(ZORAN_VDC_VIDEN,ZORAN_VDC);
  217. /* start single-shot grab */
  218. zror(ZORAN_VSTR_GRAB, ZORAN_VSTR);
  219. break;
  220. default:
  221. printk(CARD_INFO "what is this doing on the queue? (kindof=%d)\n",CARD,ztv->workqueue->kindof);
  222. write_lock(&ztv->lock);
  223. newitem = ztv->workqueue->next;
  224. ztv->workqueue->next = 0;
  225. ztv->workqueue = newitem;
  226. write_unlock(&ztv->lock);
  227. if (newitem)
  228. goto again; /* yeah, sure.. */
  229. }
  230. /* bye for now */
  231. return;
  232. }
  233. DEBUG(printk(CARD_DEBUG "nothing in queue\n",CARD));
  234. /*
  235. * What? Even the workqueue is empty? Am i really here
  236. * for nothing? Did i come all that way to... do nothing?
  237. */
  238. /* do we need to overlay? */
  239. if (test_bit(STATE_OVERLAY, &ztv->state))
  240. {
  241. /* are we already overlaying? */
  242. if (!(zrread(ZORAN_OCR) & ZORAN_OCR_OVLEN) ||
  243. !(zrread(ZORAN_VDC) & ZORAN_VDC_VIDEN))
  244. {
  245. DEBUG(printk(CARD_DEBUG "starting overlay\n",CARD));
  246. read_lock(&ztv->lock);
  247. zoran_set_geo(ztv,&ztv->overinfo);
  248. read_unlock(&ztv->lock);
  249. zror(ZORAN_OCR_OVLEN, ZORAN_OCR);
  250. zrand(~ZORAN_VSTR_SNAPSHOT,ZORAN_VSTR);
  251. zror(ZORAN_VDC_VIDEN,ZORAN_VDC);
  252. }
  253. /*
  254. * leave overlaying on, but turn interrupts off.
  255. */
  256. zrand(~ZORAN_ICR_EN,ZORAN_ICR);
  257. return;
  258. }
  259. /* do we have any VBI idle time processing? */
  260. if (test_bit(STATE_VBI, &ztv->state))
  261. {
  262. struct vidinfo* item;
  263. struct vidinfo* lastitem;
  264. /* protect the workqueue */
  265. write_lock(&ztv->lock);
  266. lastitem = ztv->workqueue;
  267. if (lastitem)
  268. while (lastitem->next) lastitem = lastitem->next;
  269. for (item=ztv->readinfo; item!=ztv->readinfo+ZORAN_VBI_BUFFERS; item++)
  270. if (item->next == 0 && item->status == FBUFFER_FREE)
  271. {
  272. DEBUG(printk(CARD_DEBUG "%p added to queue\n",CARD,item));
  273. item->status = FBUFFER_BUSY;
  274. if (!lastitem)
  275. ztv->workqueue = item;
  276. else
  277. lastitem->next = item;
  278. lastitem = item;
  279. }
  280. write_unlock(&ztv->lock);
  281. if (ztv->workqueue)
  282. goto again; /* hey, _i_ graduated :) */
  283. }
  284. /*
  285. * Then we must be realy IDLE
  286. */
  287. DEBUG(printk(CARD_DEBUG "turning off\n",CARD));
  288. /* nothing further to do, disable DMA and further IRQs */
  289. zrand(~ZORAN_VDC_VIDEN,ZORAN_VDC);
  290. zrand(~ZORAN_ICR_EN,ZORAN_ICR);
  291. }
  292. static
  293. void zoran_irq(int irq, void *dev_id, struct pt_regs * regs)
  294. {
  295. u32 stat,estat;
  296. int count = 0;
  297. struct zoran *ztv = dev_id;
  298. UNUSED(irq); UNUSED(regs);
  299. for (;;) {
  300. /* get/clear interrupt status bits */
  301. stat=zrread(ZORAN_ISR);
  302. estat=stat & zrread(ZORAN_ICR);
  303. if (!estat)
  304. return;
  305. zrwrite(estat,ZORAN_ISR);
  306. IDEBUG(printk(CARD_DEBUG "estat %08x\n",CARD,estat));
  307. IDEBUG(printk(CARD_DEBUG " stat %08x\n",CARD,stat));
  308. if (estat & ZORAN_ISR_CODE)
  309. {
  310. IDEBUG(printk(CARD_DEBUG "CodReplIRQ\n",CARD));
  311. }
  312. if (estat & ZORAN_ISR_GIRQ0)
  313. {
  314. IDEBUG(printk(CARD_DEBUG "GIRQ0\n",CARD));
  315. if (!ztv->card->usegirq1)
  316. reap_states(ztv);
  317. }
  318. if (estat & ZORAN_ISR_GIRQ1)
  319. {
  320. IDEBUG(printk(CARD_DEBUG "GIRQ1\n",CARD));
  321. if (ztv->card->usegirq1)
  322. reap_states(ztv);
  323. }
  324. count++;
  325. if (count > 10)
  326. printk(CARD_ERR "irq loop %d (%x)\n",CARD,count,estat);
  327. if (count > 20)
  328. {
  329. zrwrite(0, ZORAN_ICR);
  330. printk(CARD_ERR "IRQ lockup, cleared int mask\n",CARD);
  331. }
  332. }
  333. }
  334. static
  335. int zoran_muxsel(struct zoran* ztv, int channel, int norm)
  336. {
  337. int rv;
  338. /* set the new video norm */
  339. rv = i2c_control_device(&(ztv->i2c), I2C_DRIVERID_VIDEODECODER, DECODER_SET_NORM, &norm);
  340. if (rv)
  341. return rv;
  342. ztv->norm = norm;
  343. /* map the given channel to the cards decoder's channel */
  344. channel = ztv->card->video_mux[channel] & CHANNEL_MASK;
  345. /* set the new channel */
  346. rv = i2c_control_device(&(ztv->i2c), I2C_DRIVERID_VIDEODECODER, DECODER_SET_INPUT, &channel);
  347. return rv;
  348. }
  349. /* Tell the interrupt handler what to to. */
  350. static
  351. void zoran_cap(struct zoran* ztv, int on)
  352. {
  353. DEBUG(printk(CARD_DEBUG "zoran_cap(%d) state=%x\n",CARD,on,ztv->state));
  354. if (on) {
  355. ztv->running = 1;
  356. /*
  357. * turn interrupts (back) on. The DMA will be enabled
  358. * inside the irq handler when it detects a restart.
  359. */
  360. zror(ZORAN_ICR_EN,ZORAN_ICR);
  361. }
  362. else {
  363. /*
  364. * turn both interrupts and DMA off
  365. */
  366. zrand(~ZORAN_VDC_VIDEN,ZORAN_VDC);
  367. zrand(~ZORAN_ICR_EN,ZORAN_ICR);
  368. ztv->running = 0;
  369. }
  370. }
  371. static ulong dmask[] = {
  372. 0xFFFFFFFF, 0xFFFFFFFE, 0xFFFFFFFC, 0xFFFFFFF8,
  373. 0xFFFFFFF0, 0xFFFFFFE0, 0xFFFFFFC0, 0xFFFFFF80,
  374. 0xFFFFFF00, 0xFFFFFE00, 0xFFFFFC00, 0xFFFFF800,
  375. 0xFFFFF000, 0xFFFFE000, 0xFFFFC000, 0xFFFF8000,
  376. 0xFFFF0000, 0xFFFE0000, 0xFFFC0000, 0xFFF80000,
  377. 0xFFF00000, 0xFFE00000, 0xFFC00000, 0xFF800000,
  378. 0xFF000000, 0xFE000000, 0xFC000000, 0xF8000000,
  379. 0xF0000000, 0xE0000000, 0xC0000000, 0x80000000
  380. };
  381. static
  382. void zoran_built_overlay(struct zoran* ztv, int count, struct video_clip *vcp)
  383. {
  384. ulong* mtop;
  385. int ystep = (ztv->vidXshift + ztv->vidWidth+31)/32; /* next DWORD */
  386. int i;
  387. DEBUG(printk(KERN_DEBUG " overlay at %p, ystep=%d, clips=%d\n",ztv->overinfo.overlay,ystep,count));
  388. for (i=0; i<count; i++) {
  389. struct video_clip *vp = vcp+i;
  390. UNUSED(vp);
  391. DEBUG(printk(KERN_DEBUG " %d: clip(%d,%d,%d,%d)\n", i,vp->x,vp->y,vp->width,vp->height));
  392. }
  393. /*
  394. * activate the visible portion of the screen
  395. * Note we take some shortcuts here, because we
  396. * know the width can never be < 32. (I.e. a DWORD)
  397. * We also assume the overlay starts somewhere in
  398. * the FIRST dword.
  399. */
  400. {
  401. int start = ztv->vidXshift;
  402. ulong firstd = dmask[start];
  403. ulong lastd = ~dmask[(start + ztv->overinfo.w) & 31];
  404. mtop = ztv->overinfo.overlay;
  405. for (i=0; i<ztv->overinfo.h; i++) {
  406. int w = ztv->vidWidth;
  407. ulong* line = mtop;
  408. if (start & 31) {
  409. *line++ = firstd;
  410. w -= 32-(start&31);
  411. }
  412. memset(line, ~0, w/8);
  413. if (w & 31)
  414. line[w/32] = lastd;
  415. mtop += ystep;
  416. }
  417. }
  418. /* process clipping regions */
  419. for (i=0; i<count; i++) {
  420. int h;
  421. if (vcp->x < 0 || (uint)vcp->x > ztv->overinfo.w ||
  422. vcp->y < 0 || vcp->y > ztv->overinfo.h ||
  423. vcp->width < 0 || (uint)(vcp->x+vcp->width) > ztv->overinfo.w ||
  424. vcp->height < 0 || (vcp->y+vcp->height) > ztv->overinfo.h)
  425. {
  426. DEBUG(printk(CARD_DEBUG "invalid clipzone (%d,%d,%d,%d) not in (0,0,%d,%d), adapting\n",CARD,vcp->x,vcp->y,vcp->width,vcp->height,ztv->overinfo.w,ztv->overinfo.h));
  427. if (vcp->x < 0) vcp->x = 0;
  428. if ((uint)vcp->x > ztv->overinfo.w) vcp->x = ztv->overinfo.w;
  429. if (vcp->y < 0) vcp->y = 0;
  430. if (vcp->y > ztv->overinfo.h) vcp->y = ztv->overinfo.h;
  431. if (vcp->width < 0) vcp->width = 0;
  432. if ((uint)(vcp->x+vcp->width) > ztv->overinfo.w) vcp->width = ztv->overinfo.w - vcp->x;
  433. if (vcp->height < 0) vcp->height = 0;
  434. if (vcp->y+vcp->height > ztv->overinfo.h) vcp->height = ztv->overinfo.h - vcp->y;
  435. // continue;
  436. }
  437. mtop = &ztv->overinfo.overlay[vcp->y*ystep];
  438. for (h=0; h<=vcp->height; h++) {
  439. int w;
  440. int x = ztv->vidXshift + vcp->x;
  441. for (w=0; w<=vcp->width; w++) {
  442. clear_bit(x&31, &mtop[x/32]);
  443. x++;
  444. }
  445. mtop += ystep;
  446. }
  447. ++vcp;
  448. }
  449. mtop = ztv->overinfo.overlay;
  450. zrwrite(virt_to_bus(mtop), ZORAN_MTOP);
  451. zrwrite(virt_to_bus(mtop+ystep), ZORAN_MBOT);
  452. zraor((ztv->vidInterlace*ystep)<<0,~ZORAN_OCR_MASKSTRIDE,ZORAN_OCR);
  453. }
  454. struct tvnorm
  455. {
  456. u16 Wt, Wa, Ht, Ha, HStart, VStart;
  457. };
  458. static struct tvnorm tvnorms[] = {
  459. /* PAL-BDGHI */
  460. /* { 864, 720, 625, 576, 131, 21 },*/
  461. /*00*/ { 864, 768, 625, 576, 81, 17 },
  462. /* NTSC */
  463. /*01*/ { 858, 720, 525, 480, 121, 10 },
  464. /* SECAM */
  465. /*02*/ { 864, 720, 625, 576, 131, 21 },
  466. /* BW50 */
  467. /*03*/ { 864, 720, 625, 576, 131, 21 },
  468. /* BW60 */
  469. /*04*/ { 858, 720, 525, 480, 121, 10 }
  470. };
  471. #define TVNORMS (sizeof(tvnorms)/sizeof(tvnorm))
  472. /*
  473. * Program the chip for a setup as described in the vidinfo struct.
  474. *
  475. * Side-effects: calculates vidXshift, vidInterlace,
  476. * vidHeight, vidWidth which are used in a later stage
  477. * to calculate the overlay mask
  478. *
  479. * This is an internal function, as such it does not check the
  480. * validity of the struct members... Spectaculair crashes will
  481. * follow /very/ quick when you're wrong and the chip right :)
  482. */
  483. static
  484. void zoran_set_geo(struct zoran* ztv, struct vidinfo* i)
  485. {
  486. ulong top, bot;
  487. int stride;
  488. int winWidth, winHeight;
  489. int maxWidth, maxHeight, maxXOffset, maxYOffset;
  490. long vfec;
  491. DEBUG(printk(CARD_DEBUG "set_geo(rect=(%d,%d,%d,%d), norm=%d, format=%d, bpp=%d, bpl=%d, busadr=%lx, overlay=%p)\n",CARD,i->x,i->y,i->w,i->h,ztv->norm,i->format,i->bpp,i->bpl,i->busadr,i->overlay));
  492. /*
  493. * make sure the DMA transfers are inhibited during our
  494. * reprogramming of the chip
  495. */
  496. zrand(~ZORAN_VDC_VIDEN,ZORAN_VDC);
  497. maxWidth = tvnorms[ztv->norm].Wa;
  498. maxHeight = tvnorms[ztv->norm].Ha/2;
  499. maxXOffset = tvnorms[ztv->norm].HStart;
  500. maxYOffset = tvnorms[ztv->norm].VStart;
  501. /* setup vfec register (keep ExtFl,TopField and VCLKPol settings) */
  502. vfec = (zrread(ZORAN_VFEC) & (ZORAN_VFEC_EXTFL|ZORAN_VFEC_TOPFIELD|ZORAN_VFEC_VCLKPOL)) |
  503. (palette2fmt[i->format].mode & (ZORAN_VFEC_RGB|ZORAN_VFEC_ERRDIF|ZORAN_VFEC_LE|ZORAN_VFEC_PACK24));
  504. /*
  505. * Set top, bottom ptrs. Since these must be DWORD aligned,
  506. * possible adjust the x and the width of the window.
  507. * so the endposition stay the same. The vidXshift will make
  508. * sure we are not writing pixels before the requested x.
  509. */
  510. ztv->vidXshift = 0;
  511. winWidth = i->w;
  512. if (winWidth < 0)
  513. winWidth = -winWidth;
  514. top = i->busadr + i->x*i->bpp + i->y*i->bpl;
  515. if (top & 3) {
  516. ztv->vidXshift = (top & 3) / i->bpp;
  517. winWidth += ztv->vidXshift;
  518. DEBUG(printk(KERN_DEBUG " window-x shifted %d pixels left\n",ztv->vidXshift));
  519. top &= ~3;
  520. }
  521. /*
  522. * bottom points to next frame but in interleaved mode we want
  523. * to 'mix' the 2 frames to one capture, so 'bot' points to one
  524. * (physical) line below the top line.
  525. */
  526. bot = top + i->bpl;
  527. zrwrite(top,ZORAN_VTOP);
  528. zrwrite(bot,ZORAN_VBOT);
  529. /*
  530. * Make sure the winWidth is DWORD aligned too,
  531. * thereby automaticly making sure the stride to the
  532. * next line is DWORD aligned too (as required by spec).
  533. */
  534. if ((winWidth*i->bpp) & 3) {
  535. DEBUG(printk(KERN_DEBUG " window-width enlarged by %d pixels\n",(winWidth*i->bpp) & 3));
  536. winWidth += (winWidth*i->bpp) & 3;
  537. }
  538. /* determine the DispMode and stride */
  539. if (i->h >= 0 && i->h <= maxHeight) {
  540. /* single frame grab suffices for this height. */
  541. vfec |= ZORAN_VFEC_DISPMOD;
  542. ztv->vidInterlace = 0;
  543. stride = i->bpl - (winWidth*i->bpp);
  544. winHeight = i->h;
  545. }
  546. else {
  547. /* interleaving needed for this height */
  548. ztv->vidInterlace = 1;
  549. stride = i->bpl*2 - (winWidth*i->bpp);
  550. winHeight = i->h/2;
  551. }
  552. if (winHeight < 0) /* can happen for VBI! */
  553. winHeight = -winHeight;
  554. /* safety net, sometimes bpl is too short??? */
  555. if (stride<0) {
  556. DEBUG(printk(CARD_DEBUG "WARNING stride = %d\n",CARD,stride));
  557. stride = 0;
  558. }
  559. zraor((winHeight<<12)|(winWidth<<0),~(ZORAN_VDC_VIDWINHT|ZORAN_VDC_VIDWINWID), ZORAN_VDC);
  560. zraor(stride<<16,~ZORAN_VSTR_DISPSTRIDE,ZORAN_VSTR);
  561. /* remember vidWidth, vidHeight for overlay calculations */
  562. ztv->vidWidth = winWidth;
  563. ztv->vidHeight = winHeight;
  564. DEBUG(printk(KERN_DEBUG " top=%08lx, bottom=%08lx\n",top,bot));
  565. DEBUG(printk(KERN_DEBUG " winWidth=%d, winHeight=%d\n",winWidth,winHeight));
  566. DEBUG(printk(KERN_DEBUG " maxWidth=%d, maxHeight=%d\n",maxWidth,maxHeight));
  567. DEBUG(printk(KERN_DEBUG " stride=%d\n",stride));
  568. /*
  569. * determine horizontal scales and crops
  570. */
  571. if (i->w < 0) {
  572. int Hstart = 1;
  573. int Hend = Hstart + winWidth;
  574. DEBUG(printk(KERN_DEBUG " Y: scale=0, start=%d, end=%d\n", Hstart, Hend));
  575. zraor((Hstart<<10)|(Hend<<0),~(ZORAN_VFEH_HSTART|ZORAN_VFEH_HEND),ZORAN_VFEH);
  576. }
  577. else {
  578. int Wa = maxWidth;
  579. int X = (winWidth*64+Wa-1)/Wa;
  580. int We = winWidth*64/X;
  581. int HorDcm = 64-X;
  582. int hcrop1 = 2*(Wa-We)/4;
  583. /*
  584. * BUGFIX: Juha Nurmela <junki@qn-lpr2-165.quicknet.inet.fi>
  585. * found the solution to the color phase shift.
  586. * See ChangeLog for the full explanation)
  587. */
  588. int Hstart = (maxXOffset + hcrop1) | 1;
  589. int Hend = Hstart + We - 1;
  590. DEBUG(printk(KERN_DEBUG " X: scale=%d, start=%d, end=%d\n", HorDcm, Hstart, Hend));
  591. zraor((Hstart<<10)|(Hend<<0),~(ZORAN_VFEH_HSTART|ZORAN_VFEH_HEND),ZORAN_VFEH);
  592. vfec |= HorDcm<<14;
  593. if (HorDcm<16)
  594. vfec |= ZORAN_VFEC_HFILTER_1; /* no filter */
  595. else if (HorDcm<32)
  596. vfec |= ZORAN_VFEC_HFILTER_3; /* 3 tap filter */
  597. else if (HorDcm<48)
  598. vfec |= ZORAN_VFEC_HFILTER_4; /* 4 tap filter */
  599. else vfec |= ZORAN_VFEC_HFILTER_5; /* 5 tap filter */
  600. }
  601. /*
  602. * Determine vertical scales and crops
  603. *
  604. * when height is negative, we want to read starting at line 0
  605. * One day someone might need access to these lines...
  606. */
  607. if (i->h < 0) {
  608. int Vstart = 0;
  609. int Vend = Vstart + winHeight;
  610. DEBUG(printk(KERN_DEBUG " Y: scale=0, start=%d, end=%d\n", Vstart, Vend));
  611. zraor((Vstart<<10)|(Vend<<0),~(ZORAN_VFEV_VSTART|ZORAN_VFEV_VEND),ZORAN_VFEV);
  612. }
  613. else {
  614. int Ha = maxHeight;
  615. int Y = (winHeight*64+Ha-1)/Ha;
  616. int He = winHeight*64/Y;
  617. int VerDcm = 64-Y;
  618. int vcrop1 = 2*(Ha-He)/4;
  619. int Vstart = maxYOffset + vcrop1;
  620. int Vend = Vstart + He - 1;
  621. DEBUG(printk(KERN_DEBUG " Y: scale=%d, start=%d, end=%d\n", VerDcm, Vstart, Vend));
  622. zraor((Vstart<<10)|(Vend<<0),~(ZORAN_VFEV_VSTART|ZORAN_VFEV_VEND),ZORAN_VFEV);
  623. vfec |= VerDcm<<8;
  624. }
  625. DEBUG(printk(KERN_DEBUG " F: format=%d(=%s)\n",i->format,palette2fmt[i->format].name));
  626. /* setup the requested format */
  627. zrwrite(vfec, ZORAN_VFEC);
  628. }
  629. static
  630. void zoran_common_open(struct zoran* ztv, int flags)
  631. {
  632. UNUSED(flags);
  633. /* already opened? */
  634. if (ztv->users++ != 0)
  635. return;
  636. /* unmute audio */
  637. /* /what/ audio? */
  638. ztv->state = 0;
  639. /* setup the encoder to the initial values */
  640. ztv->picture.colour=254<<7;
  641. ztv->picture.brightness=128<<8;
  642. ztv->picture.hue=128<<8;
  643. ztv->picture.contrast=216<<7;
  644. i2c_control_device(&ztv->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_SET_PICTURE, &ztv->picture);
  645. /* default to the composite input since my camera is there */
  646. zoran_muxsel(ztv, 0, VIDEO_MODE_PAL);
  647. }
  648. static
  649. void zoran_common_close(struct zoran* ztv)
  650. {
  651. if (--ztv->users != 0)
  652. return;
  653. /* mute audio */
  654. /* /what/ audio? */
  655. /* stop the chip */
  656. zoran_cap(ztv, 0);
  657. }
  658. /*
  659. * Open a zoran card. Right now the flags are just a hack
  660. */
  661. static int zoran_open(struct video_device *dev, int flags)
  662. {
  663. struct zoran *ztv = (struct zoran*)dev;
  664. struct vidinfo* item;
  665. char* pos;
  666. DEBUG(printk(CARD_DEBUG "open(dev,%d)\n",CARD,flags));
  667. /*********************************************
  668. * We really should be doing lazy allocing...
  669. *********************************************/
  670. /* allocate a frame buffer */
  671. if (!ztv->fbuffer)
  672. ztv->fbuffer = bmalloc(ZORAN_MAX_FBUFSIZE);
  673. if (!ztv->fbuffer) {
  674. /* could not get a buffer, bail out */
  675. return -ENOBUFS;
  676. }
  677. /* at this time we _always_ have a framebuffer */
  678. memset(ztv->fbuffer,0,ZORAN_MAX_FBUFSIZE);
  679. if (!ztv->overinfo.overlay)
  680. ztv->overinfo.overlay = kmalloc(1024*1024/8, GFP_KERNEL);
  681. if (!ztv->overinfo.overlay) {
  682. /* could not get an overlay buffer, bail out */
  683. bfree(ztv->fbuffer, ZORAN_MAX_FBUFSIZE);
  684. return -ENOBUFS;
  685. }
  686. /* at this time we _always_ have a overlay */
  687. /* clear buffer status, and give them a DMAable address */
  688. pos = ztv->fbuffer;
  689. for (item=ztv->grabinfo; item!=ztv->grabinfo+ZORAN_MAX_FBUFFERS; item++)
  690. {
  691. item->status = FBUFFER_FREE;
  692. item->memadr = pos;
  693. item->busadr = virt_to_bus(pos);
  694. pos += ZORAN_MAX_FBUFFER;
  695. }
  696. /* do the common part of all open's */
  697. zoran_common_open(ztv, flags);
  698. return 0;
  699. }
  700. static
  701. void zoran_close(struct video_device* dev)
  702. {
  703. struct zoran *ztv = (struct zoran*)dev;
  704. DEBUG(printk(CARD_DEBUG "close(dev)\n",CARD));
  705. /* driver specific closure */
  706. clear_bit(STATE_OVERLAY, &ztv->state);
  707. zoran_common_close(ztv);
  708. /*
  709. * This is sucky but right now I can't find a good way to
  710. * be sure its safe to free the buffer. We wait 5-6 fields
  711. * which is more than sufficient to be sure.
  712. */
  713. msleep(100); /* Wait 1/10th of a second */
  714. /* free the allocated framebuffer */
  715. bfree(ztv->fbuffer, ZORAN_MAX_FBUFSIZE);
  716. ztv->fbuffer = 0;
  717. kfree(ztv->overinfo.overlay);
  718. ztv->overinfo.overlay = 0;
  719. }
  720. /*
  721. * This read function could be used reentrant in a SMP situation.
  722. *
  723. * This is made possible by the spinlock which is kept till we
  724. * found and marked a buffer for our own use. The lock must
  725. * be released as soon as possible to prevent lock contention.
  726. */
  727. static
  728. long zoran_read(struct video_device* dev, char* buf, unsigned long count, int nonblock)
  729. {
  730. struct zoran *ztv = (struct zoran*)dev;
  731. unsigned long max;
  732. struct vidinfo* unused = 0;
  733. struct vidinfo* done = 0;
  734. DEBUG(printk(CARD_DEBUG "zoran_read(%p,%ld,%d)\n",CARD,buf,count,nonblock));
  735. /* find ourself a free or completed buffer */
  736. for (;;) {
  737. struct vidinfo* item;
  738. write_lock_irq(&ztv->lock);
  739. for (item=ztv->grabinfo; item!=ztv->grabinfo+ZORAN_MAX_FBUFFERS; item++)
  740. {
  741. if (!unused && item->status == FBUFFER_FREE)
  742. unused = item;
  743. if (!done && item->status == FBUFFER_DONE)
  744. done = item;
  745. }
  746. if (done || unused)
  747. break;
  748. /* no more free buffers, wait for them. */
  749. write_unlock_irq(&ztv->lock);
  750. if (nonblock)
  751. return -EWOULDBLOCK;
  752. interruptible_sleep_on(&ztv->grabq);
  753. if (signal_pending(current))
  754. return -EINTR;
  755. }
  756. /* Do we have 'ready' data? */
  757. if (!done) {
  758. /* no? than this will take a while... */
  759. if (nonblock) {
  760. write_unlock_irq(&ztv->lock);
  761. return -EWOULDBLOCK;
  762. }
  763. /* mark the unused buffer as wanted */
  764. unused->status = FBUFFER_BUSY;
  765. unused->w = 320;
  766. unused->h = 240;
  767. unused->format = VIDEO_PALETTE_RGB24;
  768. unused->bpp = palette2fmt[unused->format].bpp;
  769. unused->bpl = unused->w * unused->bpp;
  770. unused->next = 0;
  771. { /* add to tail of queue */
  772. struct vidinfo* oldframe = ztv->workqueue;
  773. if (!oldframe) ztv->workqueue = unused;
  774. else {
  775. while (oldframe->next) oldframe = oldframe->next;
  776. oldframe->next = unused;
  777. }
  778. }
  779. write_unlock_irq(&ztv->lock);
  780. /* tell the state machine we want it filled /NOW/ */
  781. zoran_cap(ztv, 1);
  782. /* wait till this buffer gets grabbed */
  783. wait_event_interruptible(ztv->grabq,
  784. (unused->status != FBUFFER_BUSY));
  785. /* see if a signal did it */
  786. if (signal_pending(current))
  787. return -EINTR;
  788. done = unused;
  789. }
  790. else
  791. write_unlock_irq(&ztv->lock);
  792. /* Yes! we got data! */
  793. max = done->bpl * done->h;
  794. if (count > max)
  795. count = max;
  796. if (copy_to_user((void*)buf, done->memadr, count))
  797. count = -EFAULT;
  798. /* keep the engine running */
  799. done->status = FBUFFER_FREE;
  800. // zoran_cap(ztv,1);
  801. /* tell listeners this buffer became free */
  802. wake_up_interruptible(&ztv->grabq);
  803. /* goodbye */
  804. DEBUG(printk(CARD_DEBUG "zoran_read() returns %lu\n",CARD,count));
  805. return count;
  806. }
  807. static
  808. long zoran_write(struct video_device* dev, const char* buf, unsigned long count, int nonblock)
  809. {
  810. struct zoran *ztv = (struct zoran *)dev;
  811. UNUSED(ztv); UNUSED(dev); UNUSED(buf); UNUSED(count); UNUSED(nonblock);
  812. DEBUG(printk(CARD_DEBUG "zoran_write\n",CARD));
  813. return -EINVAL;
  814. }
  815. static
  816. unsigned int zoran_poll(struct video_device *dev, struct file *file, poll_table *wait)
  817. {
  818. struct zoran *ztv = (struct zoran *)dev;
  819. struct vidinfo* item;
  820. unsigned int mask = 0;
  821. poll_wait(file, &ztv->grabq, wait);
  822. for (item=ztv->grabinfo; item!=ztv->grabinfo+ZORAN_MAX_FBUFFERS; item++)
  823. if (item->status == FBUFFER_DONE)
  824. {
  825. mask |= (POLLIN | POLLRDNORM);
  826. break;
  827. }
  828. DEBUG(printk(CARD_DEBUG "zoran_poll()=%x\n",CARD,mask));
  829. return mask;
  830. }
  831. /* append a new clipregion to the vector of video_clips */
  832. static
  833. void new_clip(struct video_window* vw, struct video_clip* vcp, int x, int y, int w, int h)
  834. {
  835. vcp[vw->clipcount].x = x;
  836. vcp[vw->clipcount].y = y;
  837. vcp[vw->clipcount].width = w;
  838. vcp[vw->clipcount].height = h;
  839. vw->clipcount++;
  840. }
  841. static
  842. int zoran_ioctl(struct video_device* dev, unsigned int cmd, void *arg)
  843. {
  844. struct zoran* ztv = (struct zoran*)dev;
  845. switch (cmd) {
  846. case VIDIOCGCAP:
  847. {
  848. struct video_capability c;
  849. DEBUG(printk(CARD_DEBUG "VIDIOCGCAP\n",CARD));
  850. strcpy(c.name,ztv->video_dev.name);
  851. c.type = VID_TYPE_CAPTURE|
  852. VID_TYPE_OVERLAY|
  853. VID_TYPE_CLIPPING|
  854. VID_TYPE_FRAMERAM|
  855. VID_TYPE_SCALES;
  856. if (ztv->have_tuner)
  857. c.type |= VID_TYPE_TUNER;
  858. if (ztv->have_decoder) {
  859. c.channels = ztv->card->video_inputs;
  860. c.audios = ztv->card->audio_inputs;
  861. } else
  862. /* no decoder -> no channels */
  863. c.channels = c.audios = 0;
  864. c.maxwidth = 768;
  865. c.maxheight = 576;
  866. c.minwidth = 32;
  867. c.minheight = 32;
  868. if (copy_to_user(arg,&c,sizeof(c)))
  869. return -EFAULT;
  870. break;
  871. }
  872. case VIDIOCGCHAN:
  873. {
  874. struct video_channel v;
  875. int mux;
  876. if (copy_from_user(&v, arg,sizeof(v)))
  877. return -EFAULT;
  878. DEBUG(printk(CARD_DEBUG "VIDIOCGCHAN(%d)\n",CARD,v.channel));
  879. v.flags=VIDEO_VC_AUDIO
  880. #ifdef VIDEO_VC_NORM
  881. |VIDEO_VC_NORM
  882. #endif
  883. ;
  884. v.tuners=0;
  885. v.type=VIDEO_TYPE_CAMERA;
  886. #ifdef I_EXPECT_POSSIBLE_NORMS_IN_THE_API
  887. v.norm=VIDEO_MODE_PAL|
  888. VIDEO_MODE_NTSC|
  889. VIDEO_MODE_SECAM;
  890. #else
  891. v.norm=VIDEO_MODE_PAL;
  892. #endif
  893. /* too many inputs? no decoder -> no channels */
  894. if (!ztv->have_decoder || v.channel < 0 || v.channel >= ztv->card->video_inputs)
  895. return -EINVAL;
  896. /* now determine the name of the channel */
  897. mux = ztv->card->video_mux[v.channel];
  898. if (mux & IS_TUNER) {
  899. /* lets assume only one tuner, yes? */
  900. strcpy(v.name,"Television");
  901. v.type = VIDEO_TYPE_TV;
  902. if (ztv->have_tuner) {
  903. v.flags |= VIDEO_VC_TUNER;
  904. v.tuners = 1;
  905. }
  906. }
  907. else if (mux & IS_SVHS)
  908. sprintf(v.name,"S-Video-%d",v.channel);
  909. else
  910. sprintf(v.name,"CVBS-%d",v.channel);
  911. if (copy_to_user(arg,&v,sizeof(v)))
  912. return -EFAULT;
  913. break;
  914. }
  915. case VIDIOCSCHAN:
  916. { /* set video channel */
  917. struct video_channel v;
  918. if (copy_from_user(&v, arg,sizeof(v)))
  919. return -EFAULT;
  920. DEBUG(printk(CARD_DEBUG "VIDIOCSCHAN(%d,%d)\n",CARD,v.channel,v.norm));
  921. /* too many inputs? no decoder -> no channels */
  922. if (!ztv->have_decoder || v.channel >= ztv->card->video_inputs || v.channel < 0)
  923. return -EINVAL;
  924. if (v.norm != VIDEO_MODE_PAL &&
  925. v.norm != VIDEO_MODE_NTSC &&
  926. v.norm != VIDEO_MODE_SECAM &&
  927. v.norm != VIDEO_MODE_AUTO)
  928. return -EOPNOTSUPP;
  929. /* make it happen, nr1! */
  930. return zoran_muxsel(ztv,v.channel,v.norm);
  931. }
  932. case VIDIOCGTUNER:
  933. {
  934. struct video_tuner v;
  935. if (copy_from_user(&v, arg,sizeof(v)))
  936. return -EFAULT;
  937. DEBUG(printk(CARD_DEBUG "VIDIOCGTUNER(%d)\n",CARD,v.tuner));
  938. /* Only no or one tuner for now */
  939. if (!ztv->have_tuner || v.tuner)
  940. return -EINVAL;
  941. strcpy(v.name,"Television");
  942. v.rangelow = 0;
  943. v.rangehigh = ~0;
  944. v.flags = VIDEO_TUNER_PAL|VIDEO_TUNER_NTSC|VIDEO_TUNER_SECAM;
  945. v.mode = ztv->norm;
  946. v.signal = 0xFFFF; /* unknown */
  947. if (copy_to_user(arg,&v,sizeof(v)))
  948. return -EFAULT;
  949. break;
  950. }
  951. case VIDIOCSTUNER:
  952. {
  953. struct video_tuner v;
  954. if (copy_from_user(&v, arg, sizeof(v)))
  955. return -EFAULT;
  956. DEBUG(printk(CARD_DEBUG "VIDIOCSTUNER(%d,%d)\n",CARD,v.tuner,v.mode));
  957. /* Only no or one tuner for now */
  958. if (!ztv->have_tuner || v.tuner)
  959. return -EINVAL;
  960. /* and it only has certain valid modes */
  961. if( v.mode != VIDEO_MODE_PAL &&
  962. v.mode != VIDEO_MODE_NTSC &&
  963. v.mode != VIDEO_MODE_SECAM)
  964. return -EOPNOTSUPP;
  965. /* engage! */
  966. return zoran_muxsel(ztv,v.tuner,v.mode);
  967. }
  968. case VIDIOCGPICT:
  969. {
  970. struct video_picture p = ztv->picture;
  971. DEBUG(printk(CARD_DEBUG "VIDIOCGPICT\n",CARD));
  972. p.depth = ztv->depth;
  973. switch (p.depth) {
  974. case 8: p.palette=VIDEO_PALETTE_YUV422;
  975. break;
  976. case 15: p.palette=VIDEO_PALETTE_RGB555;
  977. break;
  978. case 16: p.palette=VIDEO_PALETTE_RGB565;
  979. break;
  980. case 24: p.palette=VIDEO_PALETTE_RGB24;
  981. break;
  982. case 32: p.palette=VIDEO_PALETTE_RGB32;
  983. break;
  984. }
  985. if (copy_to_user(arg, &p, sizeof(p)))
  986. return -EFAULT;
  987. break;
  988. }
  989. case VIDIOCSPICT:
  990. {
  991. struct video_picture p;
  992. if (copy_from_user(&p, arg,sizeof(p)))
  993. return -EFAULT;
  994. DEBUG(printk(CARD_DEBUG "VIDIOCSPICT(%d,%d,%d,%d,%d,%d,%d)\n",CARD,p.brightness,p.hue,p.colour,p.contrast,p.whiteness,p.depth,p.palette));
  995. /* depth must match with framebuffer */
  996. if (p.depth != ztv->depth)
  997. return -EINVAL;
  998. /* check if palette matches this bpp */
  999. if (p.palette>NRPALETTES ||
  1000. palette2fmt[p.palette].bpp != ztv->overinfo.bpp)
  1001. return -EINVAL;
  1002. write_lock_irq(&ztv->lock);
  1003. ztv->overinfo.format = p.palette;
  1004. ztv->picture = p;
  1005. write_unlock_irq(&ztv->lock);
  1006. /* tell the decoder */
  1007. i2c_control_device(&ztv->i2c, I2C_DRIVERID_VIDEODECODER, DECODER_SET_PICTURE, &p);
  1008. break;
  1009. }
  1010. case VIDIOCGWIN:
  1011. {
  1012. struct video_window vw;
  1013. DEBUG(printk(CARD_DEBUG "VIDIOCGWIN\n",CARD));
  1014. read_lock(&ztv->lock);
  1015. vw.x = ztv->overinfo.x;
  1016. vw.y = ztv->overinfo.y;
  1017. vw.width = ztv->overinfo.w;
  1018. vw.height = ztv->overinfo.h;
  1019. vw.chromakey= 0;
  1020. vw.flags = 0;
  1021. if (ztv->vidInterlace)
  1022. vw.flags|=VIDEO_WINDOW_INTERLACE;
  1023. read_unlock(&ztv->lock);
  1024. if (copy_to_user(arg,&vw,sizeof(vw)))
  1025. return -EFAULT;
  1026. break;
  1027. }
  1028. case VIDIOCSWIN:
  1029. {
  1030. struct video_window vw;
  1031. struct video_clip *vcp;
  1032. int on;
  1033. if (copy_from_user(&vw,arg,sizeof(vw)))
  1034. return -EFAULT;
  1035. DEBUG(printk(CARD_DEBUG "VIDIOCSWIN(%d,%d,%d,%d,%x,%d)\n",CARD,vw.x,vw.y,vw.width,vw.height,vw.flags,vw.clipcount));
  1036. if (vw.flags)
  1037. return -EINVAL;
  1038. if (vw.clipcount <0 || vw.clipcount>256)
  1039. return -EDOM; /* Too many! */
  1040. /*
  1041. * Do any clips.
  1042. */
  1043. vcp = vmalloc(sizeof(struct video_clip)*(vw.clipcount+4));
  1044. if (vcp==NULL)
  1045. return -ENOMEM;
  1046. if (vw.clipcount && copy_from_user(vcp,vw.clips,sizeof(struct video_clip)*vw.clipcount)) {
  1047. vfree(vcp);
  1048. return -EFAULT;
  1049. }
  1050. on = ztv->running;
  1051. if (on)
  1052. zoran_cap(ztv, 0);
  1053. /*
  1054. * strange, it seems xawtv sometimes calls us with 0
  1055. * width and/or height. Ignore these values
  1056. */
  1057. if (vw.x == 0)
  1058. vw.x = ztv->overinfo.x;
  1059. if (vw.y == 0)
  1060. vw.y = ztv->overinfo.y;
  1061. /* by now we are committed to the new data... */
  1062. write_lock_irq(&ztv->lock);
  1063. ztv->overinfo.x = vw.x;
  1064. ztv->overinfo.y = vw.y;
  1065. ztv->overinfo.w = vw.width;
  1066. ztv->overinfo.h = vw.height;
  1067. write_unlock_irq(&ztv->lock);
  1068. /*
  1069. * Impose display clips
  1070. */
  1071. if (vw.x+vw.width > ztv->swidth)
  1072. new_clip(&vw, vcp, ztv->swidth-vw.x, 0, vw.width-1, vw.height-1);
  1073. if (vw.y+vw.height > ztv->sheight)
  1074. new_clip(&vw, vcp, 0, ztv->sheight-vw.y, vw.width-1, vw.height-1);
  1075. /* built the requested clipping zones */
  1076. zoran_set_geo(ztv, &ztv->overinfo);
  1077. zoran_built_overlay(ztv, vw.clipcount, vcp);
  1078. vfree(vcp);
  1079. /* if we were on, restart the video engine */
  1080. if (on)
  1081. zoran_cap(ztv, 1);
  1082. break;
  1083. }
  1084. case VIDIOCCAPTURE:
  1085. {
  1086. int v;
  1087. if (get_user(v, (int *)arg))
  1088. return -EFAULT;
  1089. DEBUG(printk(CARD_DEBUG "VIDIOCCAPTURE(%d)\n",CARD,v));
  1090. if (v==0) {
  1091. clear_bit(STATE_OVERLAY, &ztv->state);
  1092. zoran_cap(ztv, 1);
  1093. }
  1094. else {
  1095. /* is VIDIOCSFBUF, VIDIOCSWIN done? */
  1096. if (ztv->overinfo.busadr==0 || ztv->overinfo.w==0 || ztv->overinfo.h==0)
  1097. return -EINVAL;
  1098. set_bit(STATE_OVERLAY, &ztv->state);
  1099. zoran_cap(ztv, 1);
  1100. }
  1101. break;
  1102. }
  1103. case VIDIOCGFBUF:
  1104. {
  1105. struct video_buffer v;
  1106. DEBUG(printk(CARD_DEBUG "VIDIOCGFBUF\n",CARD));
  1107. read_lock(&ztv->lock);
  1108. v.base = (void *)ztv->overinfo.busadr;
  1109. v.height = ztv->sheight;
  1110. v.width = ztv->swidth;
  1111. v.depth = ztv->depth;
  1112. v.bytesperline = ztv->overinfo.bpl;
  1113. read_unlock(&ztv->lock);
  1114. if(copy_to_user(arg, &v,sizeof(v)))
  1115. return -EFAULT;
  1116. break;
  1117. }
  1118. case VIDIOCSFBUF:
  1119. {
  1120. struct video_buffer v;
  1121. if(!capable(CAP_SYS_ADMIN))
  1122. return -EPERM;
  1123. if (copy_from_user(&v, arg,sizeof(v)))
  1124. return -EFAULT;
  1125. DEBUG(printk(CARD_DEBUG "VIDIOCSFBUF(%p,%d,%d,%d,%d)\n",CARD,v.base, v.width,v.height,v.depth,v.bytesperline));
  1126. if (v.depth!=15 && v.depth!=16 && v.depth!=24 && v.depth!=32)
  1127. return -EINVAL;
  1128. if (v.bytesperline<1)
  1129. return -EINVAL;
  1130. if (ztv->running)
  1131. return -EBUSY;
  1132. write_lock_irq(&ztv->lock);
  1133. ztv->overinfo.busadr = (ulong)v.base;
  1134. ztv->sheight = v.height;
  1135. ztv->swidth = v.width;
  1136. ztv->depth = v.depth; /* bits per pixel */
  1137. ztv->overinfo.bpp = ((v.depth+1)&0x38)/8;/* bytes per pixel */
  1138. ztv->overinfo.bpl = v.bytesperline; /* bytes per line */
  1139. write_unlock_irq(&ztv->lock);
  1140. break;
  1141. }
  1142. case VIDIOCKEY:
  1143. {
  1144. /* Will be handled higher up .. */
  1145. break;
  1146. }
  1147. case VIDIOCSYNC:
  1148. {
  1149. int i;
  1150. if (get_user(i, (int *) arg))
  1151. return -EFAULT;
  1152. DEBUG(printk(CARD_DEBUG "VIDEOCSYNC(%d)\n",CARD,i));
  1153. if (i<0 || i>ZORAN_MAX_FBUFFERS)
  1154. return -EINVAL;
  1155. switch (ztv->grabinfo[i].status) {
  1156. case FBUFFER_FREE:
  1157. return -EINVAL;
  1158. case FBUFFER_BUSY:
  1159. /* wait till this buffer gets grabbed */
  1160. wait_event_interruptible(ztv->grabq,
  1161. (ztv->grabinfo[i].status != FBUFFER_BUSY));
  1162. /* see if a signal did it */
  1163. if (signal_pending(current))
  1164. return -EINTR;
  1165. /* don't fall through; a DONE buffer is not UNUSED */
  1166. break;
  1167. case FBUFFER_DONE:
  1168. ztv->grabinfo[i].status = FBUFFER_FREE;
  1169. /* tell ppl we have a spare buffer */
  1170. wake_up_interruptible(&ztv->grabq);
  1171. break;
  1172. }
  1173. DEBUG(printk(CARD_DEBUG "VIDEOCSYNC(%d) returns\n",CARD,i));
  1174. break;
  1175. }
  1176. case VIDIOCMCAPTURE:
  1177. {
  1178. struct video_mmap vm;
  1179. struct vidinfo* frame;
  1180. if (copy_from_user(&vm,arg,sizeof(vm)))
  1181. return -EFAULT;
  1182. DEBUG(printk(CARD_DEBUG "VIDIOCMCAPTURE(%d,(%d,%d),%d)\n",CARD,vm.frame,vm.width,vm.height,vm.format));
  1183. if (vm.frame<0 || vm.frame>ZORAN_MAX_FBUFFERS ||
  1184. vm.width<32 || vm.width>768 ||
  1185. vm.height<32 || vm.height>576 ||
  1186. vm.format>NRPALETTES ||
  1187. palette2fmt[vm.format].mode == 0)
  1188. return -EINVAL;
  1189. /* we are allowed to take over UNUSED and DONE buffers */
  1190. frame = &ztv->grabinfo[vm.frame];
  1191. if (frame->status == FBUFFER_BUSY)
  1192. return -EBUSY;
  1193. /* setup the other parameters if they are given */
  1194. write_lock_irq(&ztv->lock);
  1195. frame->w = vm.width;
  1196. frame->h = vm.height;
  1197. frame->format = vm.format;
  1198. frame->bpp = palette2fmt[frame->format].bpp;
  1199. frame->bpl = frame->w*frame->bpp;
  1200. frame->status = FBUFFER_BUSY;
  1201. frame->next = 0;
  1202. { /* add to tail of queue */
  1203. struct vidinfo* oldframe = ztv->workqueue;
  1204. if (!oldframe) ztv->workqueue = frame;
  1205. else {
  1206. while (oldframe->next) oldframe = oldframe->next;
  1207. oldframe->next = frame;
  1208. }
  1209. }
  1210. write_unlock_irq(&ztv->lock);
  1211. zoran_cap(ztv, 1);
  1212. break;
  1213. }
  1214. case VIDIOCGMBUF:
  1215. {
  1216. struct video_mbuf mb;
  1217. int i;
  1218. DEBUG(printk(CARD_DEBUG "VIDIOCGMBUF\n",CARD));
  1219. mb.size = ZORAN_MAX_FBUFSIZE;
  1220. mb.frames = ZORAN_MAX_FBUFFERS;
  1221. for (i=0; i<ZORAN_MAX_FBUFFERS; i++)
  1222. mb.offsets[i] = i*ZORAN_MAX_FBUFFER;
  1223. if(copy_to_user(arg, &mb,sizeof(mb)))
  1224. return -EFAULT;
  1225. break;
  1226. }
  1227. case VIDIOCGUNIT:
  1228. {
  1229. struct video_unit vu;
  1230. DEBUG(printk(CARD_DEBUG "VIDIOCGUNIT\n",CARD));
  1231. vu.video = ztv->video_dev.minor;
  1232. vu.vbi = ztv->vbi_dev.minor;
  1233. vu.radio = VIDEO_NO_UNIT;
  1234. vu.audio = VIDEO_NO_UNIT;
  1235. vu.teletext = VIDEO_NO_UNIT;
  1236. if(copy_to_user(arg, &vu,sizeof(vu)))
  1237. return -EFAULT;
  1238. break;
  1239. }
  1240. case VIDIOCGFREQ:
  1241. {
  1242. unsigned long v = ztv->tuner_freq;
  1243. if (copy_to_user(arg,&v,sizeof(v)))
  1244. return -EFAULT;
  1245. DEBUG(printk(CARD_DEBUG "VIDIOCGFREQ\n",CARD));
  1246. break;
  1247. }
  1248. case VIDIOCSFREQ:
  1249. {
  1250. unsigned long v;
  1251. if (copy_from_user(&v, arg, sizeof(v)))
  1252. return -EFAULT;
  1253. DEBUG(printk(CARD_DEBUG "VIDIOCSFREQ\n",CARD));
  1254. if (ztv->have_tuner) {
  1255. int fixme = v;
  1256. if (i2c_control_device(&(ztv->i2c), I2C_DRIVERID_TUNER, TUNER_SET_TVFREQ, &fixme) < 0)
  1257. return -EAGAIN;
  1258. }
  1259. ztv->tuner_freq = v;
  1260. break;
  1261. }
  1262. /* Why isn't this in the API?
  1263. * And why doesn't it take a buffer number?
  1264. case BTTV_FIELDNR:
  1265. {
  1266. unsigned long v = ztv->lastfieldnr;
  1267. if (copy_to_user(arg,&v,sizeof(v)))
  1268. return -EFAULT;
  1269. DEBUG(printk(CARD_DEBUG "BTTV_FIELDNR\n",CARD));
  1270. break;
  1271. }
  1272. */
  1273. default:
  1274. return -ENOIOCTLCMD;
  1275. }
  1276. return 0;
  1277. }
  1278. static
  1279. int zoran_mmap(struct vm_area_struct *vma, struct video_device* dev, const char* adr, unsigned long size)
  1280. {
  1281. struct zoran* ztv = (struct zoran*)dev;
  1282. unsigned long start = (unsigned long)adr;
  1283. unsigned long pos;
  1284. DEBUG(printk(CARD_DEBUG "zoran_mmap(0x%p,%ld)\n",CARD,adr,size));
  1285. /* sanity checks */
  1286. if (size > ZORAN_MAX_FBUFSIZE || !ztv->fbuffer)
  1287. return -EINVAL;
  1288. /* start mapping the whole shabang to user memory */
  1289. pos = (unsigned long)ztv->fbuffer;
  1290. while (size>0) {
  1291. unsigned long pfn = virt_to_phys((void*)pos) >> PAGE_SHIFT;
  1292. if (remap_pfn_range(vma, start, pfn, PAGE_SIZE, PAGE_SHARED))
  1293. return -EAGAIN;
  1294. start += PAGE_SIZE;
  1295. pos += PAGE_SIZE;
  1296. size -= PAGE_SIZE;
  1297. }
  1298. return 0;
  1299. }
  1300. static struct video_device zr36120_template=
  1301. {
  1302. .owner = THIS_MODULE,
  1303. .name = "UNSET",
  1304. .type = VID_TYPE_TUNER|VID_TYPE_CAPTURE|VID_TYPE_OVERLAY,
  1305. .hardware = VID_HARDWARE_ZR36120,
  1306. .open = zoran_open,
  1307. .close = zoran_close,
  1308. .read = zoran_read,
  1309. .write = zoran_write,
  1310. .poll = zoran_poll,
  1311. .ioctl = zoran_ioctl,
  1312. .compat_ioctl = v4l_compat_ioctl32,
  1313. .mmap = zoran_mmap,
  1314. .minor = -1,
  1315. };
  1316. static
  1317. int vbi_open(struct video_device *dev, int flags)
  1318. {
  1319. struct zoran *ztv = dev->priv;
  1320. struct vidinfo* item;
  1321. DEBUG(printk(CARD_DEBUG "vbi_open(dev,%d)\n",CARD,flags));
  1322. /*
  1323. * During VBI device open, we continiously grab VBI-like
  1324. * data in the vbi buffer when we have nothing to do.
  1325. * Only when there is an explicit request for VBI data
  1326. * (read call) we /force/ a read.
  1327. */
  1328. /* allocate buffers */
  1329. for (item=ztv->readinfo; item!=ztv->readinfo+ZORAN_VBI_BUFFERS; item++)
  1330. {
  1331. item->status = FBUFFER_FREE;
  1332. /* alloc */
  1333. if (!item->memadr) {
  1334. item->memadr = bmalloc(ZORAN_VBI_BUFSIZE);
  1335. if (!item->memadr) {
  1336. /* could not get a buffer, bail out */
  1337. while (item != ztv->readinfo) {
  1338. item--;
  1339. bfree(item->memadr, ZORAN_VBI_BUFSIZE);
  1340. item->memadr = 0;
  1341. item->busadr = 0;
  1342. }
  1343. return -ENOBUFS;
  1344. }
  1345. }
  1346. /* determine the DMAable address */
  1347. item->busadr = virt_to_bus(item->memadr);
  1348. }
  1349. /* do the common part of all open's */
  1350. zoran_common_open(ztv, flags);
  1351. set_bit(STATE_VBI, &ztv->state);
  1352. /* start read-ahead */
  1353. zoran_cap(ztv, 1);
  1354. return 0;
  1355. }
  1356. static
  1357. void vbi_close(struct video_device *dev)
  1358. {
  1359. struct zoran *ztv = dev->priv;
  1360. struct vidinfo* item;
  1361. DEBUG(printk(CARD_DEBUG "vbi_close(dev)\n",CARD));
  1362. /* driver specific closure */
  1363. clear_bit(STATE_VBI, &ztv->state);
  1364. zoran_common_close(ztv);
  1365. /*
  1366. * This is sucky but right now I can't find a good way to
  1367. * be sure its safe to free the buffer. We wait 5-6 fields
  1368. * which is more than sufficient to be sure.
  1369. */
  1370. msleep(100); /* Wait 1/10th of a second */
  1371. for (item=ztv->readinfo; item!=ztv->readinfo+ZORAN_VBI_BUFFERS; item++)
  1372. {
  1373. if (item->memadr)
  1374. bfree(item->memadr, ZORAN_VBI_BUFSIZE);
  1375. item->memadr = 0;
  1376. }
  1377. }
  1378. /*
  1379. * This read function could be used reentrant in a SMP situation.
  1380. *
  1381. * This is made possible by the spinlock which is kept till we
  1382. * found and marked a buffer for our own use. The lock must
  1383. * be released as soon as possible to prevent lock contention.
  1384. */
  1385. static
  1386. long vbi_read(struct video_device* dev, char* buf, unsigned long count, int nonblock)
  1387. {
  1388. struct zoran *ztv = dev->priv;
  1389. unsigned long max;
  1390. struct vidinfo* unused = 0;
  1391. struct vidinfo* done = 0;
  1392. DEBUG(printk(CARD_DEBUG "vbi_read(0x%p,%ld,%d)\n",CARD,buf,count,nonblock));
  1393. /* find ourself a free or completed buffer */
  1394. for (;;) {
  1395. struct vidinfo* item;
  1396. write_lock_irq(&ztv->lock);
  1397. for (item=ztv->readinfo; item!=ztv->readinfo+ZORAN_VBI_BUFFERS; item++) {
  1398. if (!unused && item->status == FBUFFER_FREE)
  1399. unused = item;
  1400. if (!done && item->status == FBUFFER_DONE)
  1401. done = item;
  1402. }
  1403. if (done || unused)
  1404. break;
  1405. /* no more free buffers, wait for them. */
  1406. write_unlock_irq(&ztv->lock);
  1407. if (nonblock)
  1408. return -EWOULDBLOCK;
  1409. interruptible_sleep_on(&ztv->vbiq);
  1410. if (signal_pending(current))
  1411. return -EINTR;
  1412. }
  1413. /* Do we have 'ready' data? */
  1414. if (!done) {
  1415. /* no? than this will take a while... */
  1416. if (nonblock) {
  1417. write_unlock_irq(&ztv->lock);
  1418. return -EWOULDBLOCK;
  1419. }
  1420. /* mark the unused buffer as wanted */
  1421. unused->status = FBUFFER_BUSY;
  1422. unused->next = 0;
  1423. { /* add to tail of queue */
  1424. struct vidinfo* oldframe = ztv->workqueue;
  1425. if (!oldframe) ztv->workqueue = unused;
  1426. else {
  1427. while (oldframe->next) oldframe = oldframe->next;
  1428. oldframe->next = unused;
  1429. }
  1430. }
  1431. write_unlock_irq(&ztv->lock);
  1432. /* tell the state machine we want it filled /NOW/ */
  1433. zoran_cap(ztv, 1);
  1434. /* wait till this buffer gets grabbed */
  1435. wait_event_interruptible(ztv->vbiq,
  1436. (unused->status != FBUFFER_BUSY));
  1437. /* see if a signal did it */
  1438. if (signal_pending(current))
  1439. return -EINTR;
  1440. done = unused;
  1441. }
  1442. else
  1443. write_unlock_irq(&ztv->lock);
  1444. /* Yes! we got data! */
  1445. max = done->bpl * -done->h;
  1446. if (count > max)
  1447. count = max;
  1448. /* check if the user gave us enough room to write the data */
  1449. if (!access_ok(VERIFY_WRITE, buf, count)) {
  1450. count = -EFAULT;
  1451. goto out;
  1452. }
  1453. /*
  1454. * Now transform/strip the data from YUV to Y-only
  1455. * NB. Assume the Y is in the LSB of the YUV data.
  1456. */
  1457. {
  1458. unsigned char* optr = buf;
  1459. unsigned char* eptr = buf+count;
  1460. /* are we beeing accessed from an old driver? */
  1461. if (count == 2*19*2048) {
  1462. /*
  1463. * Extreme HACK, old VBI programs expect 2048 points
  1464. * of data, and we only got 864 orso. Double each
  1465. * datapoint and clear the rest of the line.
  1466. * This way we have appear to have a
  1467. * sample_frequency of 29.5 Mc.
  1468. */
  1469. int x,y;
  1470. unsigned char* iptr = done->memadr+1;
  1471. for (y=done->h; optr<eptr && y<0; y++)
  1472. {
  1473. /* copy to doubled data to userland */
  1474. for (x=0; optr+1<eptr && x<-done->w; x++)
  1475. {
  1476. unsigned char a = iptr[x*2];
  1477. __put_user(a, optr++);
  1478. __put_user(a, optr++);
  1479. }
  1480. /* and clear the rest of the line */
  1481. for (x*=2; optr<eptr && x<done->bpl; x++)
  1482. __put_user(0, optr++);
  1483. /* next line */
  1484. iptr += done->bpl;
  1485. }
  1486. }
  1487. else {
  1488. /*
  1489. * Other (probably newer) programs asked
  1490. * us what geometry we are using, and are
  1491. * reading the correct size.
  1492. */
  1493. int x,y;
  1494. unsigned char* iptr = done->memadr+1;
  1495. for (y=done->h; optr<eptr && y<0; y++)
  1496. {
  1497. /* copy to doubled data to userland */
  1498. for (x=0; optr<eptr && x<-done->w; x++)
  1499. __put_user(iptr[x*2], optr++);
  1500. /* and clear the rest of the line */
  1501. for (;optr<eptr && x<done->bpl; x++)
  1502. __put_user(0, optr++);
  1503. /* next line */
  1504. iptr += done->bpl;
  1505. }
  1506. }
  1507. /* API compliance:
  1508. * place the framenumber (half fieldnr) in the last long
  1509. */
  1510. __put_user(done->fieldnr/2, ((ulong*)eptr)[-1]);
  1511. }
  1512. /* keep the engine running */
  1513. done->status = FBUFFER_FREE;
  1514. zoran_cap(ztv, 1);
  1515. /* tell listeners this buffer just became free */
  1516. wake_up_interruptible(&ztv->vbiq);
  1517. /* goodbye */
  1518. out:
  1519. DEBUG(printk(CARD_DEBUG "vbi_read() returns %lu\n",CARD,count));
  1520. return count;
  1521. }
  1522. static
  1523. unsigned int vbi_poll(struct video_device *dev, struct file *file, poll_table *wait)
  1524. {
  1525. struct zoran *ztv = dev->priv;
  1526. struct vidinfo* item;
  1527. unsigned int mask = 0;
  1528. poll_wait(file, &ztv->vbiq, wait);
  1529. for (item=ztv->readinfo; item!=ztv->readinfo+ZORAN_VBI_BUFFERS; item++)
  1530. if (item->status == FBUFFER_DONE)
  1531. {
  1532. mask |= (POLLIN | POLLRDNORM);
  1533. break;
  1534. }
  1535. DEBUG(printk(CARD_DEBUG "vbi_poll()=%x\n",CARD,mask));
  1536. return mask;
  1537. }
  1538. static
  1539. int vbi_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
  1540. {
  1541. struct zoran* ztv = dev->priv;
  1542. switch (cmd) {
  1543. case VIDIOCGVBIFMT:
  1544. {
  1545. struct vbi_format f;
  1546. DEBUG(printk(CARD_DEBUG "VIDIOCGVBIINFO\n",CARD));
  1547. f.sampling_rate = 14750000UL;
  1548. f.samples_per_line = -ztv->readinfo[0].w;
  1549. f.sample_format = VIDEO_PALETTE_RAW;
  1550. f.start[0] = f.start[1] = ztv->readinfo[0].y;
  1551. f.start[1] += 312;
  1552. f.count[0] = f.count[1] = -ztv->readinfo[0].h;
  1553. f.flags = VBI_INTERLACED;
  1554. if (copy_to_user(arg,&f,sizeof(f)))
  1555. return -EFAULT;
  1556. break;
  1557. }
  1558. case VIDIOCSVBIFMT:
  1559. {
  1560. struct vbi_format f;
  1561. int i;
  1562. if (copy_from_user(&f, arg,sizeof(f)))
  1563. return -EFAULT;
  1564. DEBUG(printk(CARD_DEBUG "VIDIOCSVBIINFO(%d,%d,%d,%d,%d,%d,%d,%x)\n",CARD,f.sampling_rate,f.samples_per_line,f.sample_format,f.start[0],f.start[1],f.count[0],f.count[1],f.flags));
  1565. /* lots of parameters are fixed... (PAL) */
  1566. if (f.sampling_rate != 14750000UL ||
  1567. f.samples_per_line > 864 ||
  1568. f.sample_format != VIDEO_PALETTE_RAW ||
  1569. f.start[0] < 0 ||
  1570. f.start[0] != f.start[1]-312 ||
  1571. f.count[0] != f.count[1] ||
  1572. f.start[0]+f.count[0] >= 288 ||
  1573. f.flags != VBI_INTERLACED)
  1574. return -EINVAL;
  1575. write_lock_irq(&ztv->lock);
  1576. ztv->readinfo[0].y = f.start[0];
  1577. ztv->readinfo[0].w = -f.samples_per_line;
  1578. ztv->readinfo[0].h = -f.count[0];
  1579. ztv->readinfo[0].bpl = f.samples_per_line*ztv->readinfo[0].bpp;
  1580. for (i=1; i<ZORAN_VBI_BUFFERS; i++)
  1581. ztv->readinfo[i] = ztv->readinfo[i];
  1582. write_unlock_irq(&ztv->lock);
  1583. break;
  1584. }
  1585. default:
  1586. return -ENOIOCTLCMD;
  1587. }
  1588. return 0;
  1589. }
  1590. static struct video_device vbi_template=
  1591. {
  1592. .owner = THIS_MODULE,
  1593. .name = "UNSET",
  1594. .type = VID_TYPE_CAPTURE|VID_TYPE_TELETEXT,
  1595. .hardware = VID_HARDWARE_ZR36120,
  1596. .open = vbi_open,
  1597. .close = vbi_close,
  1598. .read = vbi_read,
  1599. .write = zoran_write,
  1600. .poll = vbi_poll,
  1601. .ioctl = vbi_ioctl,
  1602. .minor = -1,
  1603. };
  1604. /*
  1605. * Scan for a Zoran chip, request the irq and map the io memory
  1606. */
  1607. static
  1608. int __init find_zoran(void)
  1609. {
  1610. int result;
  1611. struct zoran *ztv;
  1612. struct pci_dev *dev = NULL;
  1613. unsigned char revision;
  1614. int zoran_num=0;
  1615. while ((dev = pci_find_device(PCI_VENDOR_ID_ZORAN,PCI_DEVICE_ID_ZORAN_36120, dev)))
  1616. {
  1617. /* Ok, a ZR36120/ZR36125 found! */
  1618. ztv = &zorans[zoran_num];
  1619. ztv->dev = dev;
  1620. if (pci_enable_device(dev))
  1621. return -EIO;
  1622. pci_read_config_byte(dev, PCI_CLASS_REVISION, &revision);
  1623. printk(KERN_INFO "zoran: Zoran %x (rev %d) ",
  1624. dev->device, revision);
  1625. printk("bus: %d, devfn: %d, irq: %d, ",
  1626. dev->bus->number, dev->devfn, dev->irq);
  1627. printk("memory: 0x%08lx.\n", ztv->zoran_adr);
  1628. ztv->zoran_mem = ioremap(ztv->zoran_adr, 0x1000);
  1629. DEBUG(printk(KERN_DEBUG "zoran: mapped-memory at 0x%p\n",ztv->zoran_mem));
  1630. result = request_irq(dev->irq, zoran_irq,
  1631. SA_SHIRQ|SA_INTERRUPT,"zoran", ztv);
  1632. if (result==-EINVAL)
  1633. {
  1634. iounmap(ztv->zoran_mem);
  1635. printk(KERN_ERR "zoran: Bad irq number or handler\n");
  1636. return -EINVAL;
  1637. }
  1638. if (result==-EBUSY)
  1639. printk(KERN_ERR "zoran: IRQ %d busy, change your PnP config in BIOS\n",dev->irq);
  1640. if (result < 0) {
  1641. iounmap(ztv->zoran_mem);
  1642. return result;
  1643. }
  1644. /* Enable bus-mastering */
  1645. pci_set_master(dev);
  1646. zoran_num++;
  1647. }
  1648. if(zoran_num)
  1649. printk(KERN_INFO "zoran: %d Zoran card(s) found.\n",zoran_num);
  1650. return zoran_num;
  1651. }
  1652. static
  1653. int __init init_zoran(int card)
  1654. {
  1655. struct zoran *ztv = &zorans[card];
  1656. int i;
  1657. /* if the given cardtype valid? */
  1658. if (cardtype[card]>=NRTVCARDS) {
  1659. printk(KERN_INFO "invalid cardtype(%d) detected\n",cardtype[card]);
  1660. return -1;
  1661. }
  1662. /* reset the zoran */
  1663. zrand(~ZORAN_PCI_SOFTRESET,ZORAN_PCI);
  1664. udelay(10);
  1665. zror(ZORAN_PCI_SOFTRESET,ZORAN_PCI);
  1666. udelay(10);
  1667. /* zoran chip specific details */
  1668. ztv->card = tvcards+cardtype[card]; /* point to the selected card */
  1669. ztv->norm = 0; /* PAL */
  1670. ztv->tuner_freq = 0;
  1671. /* videocard details */
  1672. ztv->swidth = 800;
  1673. ztv->sheight = 600;
  1674. ztv->depth = 16;
  1675. /* State details */
  1676. ztv->fbuffer = 0;
  1677. ztv->overinfo.kindof = FBUFFER_OVERLAY;
  1678. ztv->overinfo.status = FBUFFER_FREE;
  1679. ztv->overinfo.x = 0;
  1680. ztv->overinfo.y = 0;
  1681. ztv->overinfo.w = 768; /* 640 */
  1682. ztv->overinfo.h = 576; /* 480 */
  1683. ztv->overinfo.format = VIDEO_PALETTE_RGB565;
  1684. ztv->overinfo.bpp = palette2fmt[ztv->overinfo.format].bpp;
  1685. ztv->overinfo.bpl = ztv->overinfo.bpp*ztv->swidth;
  1686. ztv->overinfo.busadr = 0;
  1687. ztv->overinfo.memadr = 0;
  1688. ztv->overinfo.overlay = 0;
  1689. for (i=0; i<ZORAN_MAX_FBUFFERS; i++) {
  1690. ztv->grabinfo[i] = ztv->overinfo;
  1691. ztv->grabinfo[i].kindof = FBUFFER_GRAB;
  1692. }
  1693. init_waitqueue_head(&ztv->grabq);
  1694. /* VBI details */
  1695. ztv->readinfo[0] = ztv->overinfo;
  1696. ztv->readinfo[0].kindof = FBUFFER_VBI;
  1697. ztv->readinfo[0].w = -864;
  1698. ztv->readinfo[0].h = -38;
  1699. ztv->readinfo[0].format = VIDEO_PALETTE_YUV422;
  1700. ztv->readinfo[0].bpp = palette2fmt[ztv->readinfo[0].format].bpp;
  1701. ztv->readinfo[0].bpl = 1024*ztv->readinfo[0].bpp;
  1702. for (i=1; i<ZORAN_VBI_BUFFERS; i++)
  1703. ztv->readinfo[i] = ztv->readinfo[0];
  1704. init_waitqueue_head(&ztv->vbiq);
  1705. /* maintenance data */
  1706. ztv->have_decoder = 0;
  1707. ztv->have_tuner = 0;
  1708. ztv->tuner_type = 0;
  1709. ztv->running = 0;
  1710. ztv->users = 0;
  1711. rwlock_init(&ztv->lock);
  1712. ztv->workqueue = 0;
  1713. ztv->fieldnr = 0;
  1714. ztv->lastfieldnr = 0;
  1715. if (triton1)
  1716. zrand(~ZORAN_VDC_TRICOM, ZORAN_VDC);
  1717. /* external FL determines TOP frame */
  1718. zror(ZORAN_VFEC_EXTFL, ZORAN_VFEC);
  1719. /* set HSpol */
  1720. if (ztv->card->hsync_pos)
  1721. zrwrite(ZORAN_VFEH_HSPOL, ZORAN_VFEH);
  1722. /* set VSpol */
  1723. if (ztv->card->vsync_pos)
  1724. zrwrite(ZORAN_VFEV_VSPOL, ZORAN_VFEV);
  1725. /* Set the proper General Purpuse register bits */
  1726. /* implicit: no softreset, 0 waitstates */
  1727. zrwrite(ZORAN_PCI_SOFTRESET|(ztv->card->gpdir<<0),ZORAN_PCI);
  1728. /* implicit: 3 duration and recovery PCI clocks on guest 0-3 */
  1729. zrwrite(ztv->card->gpval<<24,ZORAN_GUEST);
  1730. /* clear interrupt status */
  1731. zrwrite(~0, ZORAN_ISR);
  1732. /*
  1733. * i2c template
  1734. */
  1735. ztv->i2c = zoran_i2c_bus_template;
  1736. sprintf(ztv->i2c.name,"zoran-%d",card);
  1737. ztv->i2c.data = ztv;
  1738. /*
  1739. * Now add the template and register the device unit
  1740. */
  1741. ztv->video_dev = zr36120_template;
  1742. strcpy(ztv->video_dev.name, ztv->i2c.name);
  1743. ztv->video_dev.priv = ztv;
  1744. if (video_register_device(&ztv->video_dev, VFL_TYPE_GRABBER, video_nr) < 0)
  1745. return -1;
  1746. ztv->vbi_dev = vbi_template;
  1747. strcpy(ztv->vbi_dev.name, ztv->i2c.name);
  1748. ztv->vbi_dev.priv = ztv;
  1749. if (video_register_device(&ztv->vbi_dev, VFL_TYPE_VBI, vbi_nr) < 0) {
  1750. video_unregister_device(&ztv->video_dev);
  1751. return -1;
  1752. }
  1753. i2c_register_bus(&ztv->i2c);
  1754. /* set interrupt mask - the PIN enable will be set later */
  1755. zrwrite(ZORAN_ICR_GIRQ0|ZORAN_ICR_GIRQ1|ZORAN_ICR_CODE, ZORAN_ICR);
  1756. printk(KERN_INFO "%s: installed %s\n",ztv->i2c.name,ztv->card->name);
  1757. return 0;
  1758. }
  1759. static
  1760. void release_zoran(int max)
  1761. {
  1762. struct zoran *ztv;
  1763. int i;
  1764. for (i=0;i<max; i++)
  1765. {
  1766. ztv = &zorans[i];
  1767. /* turn off all capturing, DMA and IRQs */
  1768. /* reset the zoran */
  1769. zrand(~ZORAN_PCI_SOFTRESET,ZORAN_PCI);
  1770. udelay(10);
  1771. zror(ZORAN_PCI_SOFTRESET,ZORAN_PCI);
  1772. udelay(10);
  1773. /* first disable interrupts before unmapping the memory! */
  1774. zrwrite(0, ZORAN_ICR);
  1775. zrwrite(0xffffffffUL,ZORAN_ISR);
  1776. /* free it */
  1777. free_irq(ztv->dev->irq,ztv);
  1778. /* unregister i2c_bus */
  1779. i2c_unregister_bus((&ztv->i2c));
  1780. /* unmap and free memory */
  1781. if (ztv->zoran_mem)
  1782. iounmap(ztv->zoran_mem);
  1783. video_unregister_device(&ztv->video_dev);
  1784. video_unregister_device(&ztv->vbi_dev);
  1785. }
  1786. }
  1787. void __exit zr36120_exit(void)
  1788. {
  1789. release_zoran(zoran_cards);
  1790. }
  1791. int __init zr36120_init(void)
  1792. {
  1793. int card;
  1794. handle_chipset();
  1795. zoran_cards = find_zoran();
  1796. if (zoran_cards<0)
  1797. /* no cards found, no need for a driver */
  1798. return -EIO;
  1799. /* initialize Zorans */
  1800. for (card=0; card<zoran_cards; card++) {
  1801. if (init_zoran(card)<0) {
  1802. /* only release the zorans we have registered */
  1803. release_zoran(card);
  1804. return -EIO;
  1805. }
  1806. }
  1807. return 0;
  1808. }
  1809. module_init(zr36120_init);
  1810. module_exit(zr36120_exit);