zr36120.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071
  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_PARM(triton1,"i");
  60. MODULE_PARM(cardtype,"1-" __MODULE_STRING(ZORAN_MAX) "i");
  61. MODULE_PARM(video_nr,"i");
  62. MODULE_PARM(vbi_nr,"i");
  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. .mmap = zoran_mmap,
  1313. .minor = -1,
  1314. };
  1315. static
  1316. int vbi_open(struct video_device *dev, int flags)
  1317. {
  1318. struct zoran *ztv = dev->priv;
  1319. struct vidinfo* item;
  1320. DEBUG(printk(CARD_DEBUG "vbi_open(dev,%d)\n",CARD,flags));
  1321. /*
  1322. * During VBI device open, we continiously grab VBI-like
  1323. * data in the vbi buffer when we have nothing to do.
  1324. * Only when there is an explicit request for VBI data
  1325. * (read call) we /force/ a read.
  1326. */
  1327. /* allocate buffers */
  1328. for (item=ztv->readinfo; item!=ztv->readinfo+ZORAN_VBI_BUFFERS; item++)
  1329. {
  1330. item->status = FBUFFER_FREE;
  1331. /* alloc */
  1332. if (!item->memadr) {
  1333. item->memadr = bmalloc(ZORAN_VBI_BUFSIZE);
  1334. if (!item->memadr) {
  1335. /* could not get a buffer, bail out */
  1336. while (item != ztv->readinfo) {
  1337. item--;
  1338. bfree(item->memadr, ZORAN_VBI_BUFSIZE);
  1339. item->memadr = 0;
  1340. item->busadr = 0;
  1341. }
  1342. return -ENOBUFS;
  1343. }
  1344. }
  1345. /* determine the DMAable address */
  1346. item->busadr = virt_to_bus(item->memadr);
  1347. }
  1348. /* do the common part of all open's */
  1349. zoran_common_open(ztv, flags);
  1350. set_bit(STATE_VBI, &ztv->state);
  1351. /* start read-ahead */
  1352. zoran_cap(ztv, 1);
  1353. return 0;
  1354. }
  1355. static
  1356. void vbi_close(struct video_device *dev)
  1357. {
  1358. struct zoran *ztv = dev->priv;
  1359. struct vidinfo* item;
  1360. DEBUG(printk(CARD_DEBUG "vbi_close(dev)\n",CARD));
  1361. /* driver specific closure */
  1362. clear_bit(STATE_VBI, &ztv->state);
  1363. zoran_common_close(ztv);
  1364. /*
  1365. * This is sucky but right now I can't find a good way to
  1366. * be sure its safe to free the buffer. We wait 5-6 fields
  1367. * which is more than sufficient to be sure.
  1368. */
  1369. msleep(100); /* Wait 1/10th of a second */
  1370. for (item=ztv->readinfo; item!=ztv->readinfo+ZORAN_VBI_BUFFERS; item++)
  1371. {
  1372. if (item->memadr)
  1373. bfree(item->memadr, ZORAN_VBI_BUFSIZE);
  1374. item->memadr = 0;
  1375. }
  1376. }
  1377. /*
  1378. * This read function could be used reentrant in a SMP situation.
  1379. *
  1380. * This is made possible by the spinlock which is kept till we
  1381. * found and marked a buffer for our own use. The lock must
  1382. * be released as soon as possible to prevent lock contention.
  1383. */
  1384. static
  1385. long vbi_read(struct video_device* dev, char* buf, unsigned long count, int nonblock)
  1386. {
  1387. struct zoran *ztv = dev->priv;
  1388. unsigned long max;
  1389. struct vidinfo* unused = 0;
  1390. struct vidinfo* done = 0;
  1391. DEBUG(printk(CARD_DEBUG "vbi_read(0x%p,%ld,%d)\n",CARD,buf,count,nonblock));
  1392. /* find ourself a free or completed buffer */
  1393. for (;;) {
  1394. struct vidinfo* item;
  1395. write_lock_irq(&ztv->lock);
  1396. for (item=ztv->readinfo; item!=ztv->readinfo+ZORAN_VBI_BUFFERS; item++) {
  1397. if (!unused && item->status == FBUFFER_FREE)
  1398. unused = item;
  1399. if (!done && item->status == FBUFFER_DONE)
  1400. done = item;
  1401. }
  1402. if (done || unused)
  1403. break;
  1404. /* no more free buffers, wait for them. */
  1405. write_unlock_irq(&ztv->lock);
  1406. if (nonblock)
  1407. return -EWOULDBLOCK;
  1408. interruptible_sleep_on(&ztv->vbiq);
  1409. if (signal_pending(current))
  1410. return -EINTR;
  1411. }
  1412. /* Do we have 'ready' data? */
  1413. if (!done) {
  1414. /* no? than this will take a while... */
  1415. if (nonblock) {
  1416. write_unlock_irq(&ztv->lock);
  1417. return -EWOULDBLOCK;
  1418. }
  1419. /* mark the unused buffer as wanted */
  1420. unused->status = FBUFFER_BUSY;
  1421. unused->next = 0;
  1422. { /* add to tail of queue */
  1423. struct vidinfo* oldframe = ztv->workqueue;
  1424. if (!oldframe) ztv->workqueue = unused;
  1425. else {
  1426. while (oldframe->next) oldframe = oldframe->next;
  1427. oldframe->next = unused;
  1428. }
  1429. }
  1430. write_unlock_irq(&ztv->lock);
  1431. /* tell the state machine we want it filled /NOW/ */
  1432. zoran_cap(ztv, 1);
  1433. /* wait till this buffer gets grabbed */
  1434. wait_event_interruptible(ztv->vbiq,
  1435. (unused->status != FBUFFER_BUSY));
  1436. /* see if a signal did it */
  1437. if (signal_pending(current))
  1438. return -EINTR;
  1439. done = unused;
  1440. }
  1441. else
  1442. write_unlock_irq(&ztv->lock);
  1443. /* Yes! we got data! */
  1444. max = done->bpl * -done->h;
  1445. if (count > max)
  1446. count = max;
  1447. /* check if the user gave us enough room to write the data */
  1448. if (!access_ok(VERIFY_WRITE, buf, count)) {
  1449. count = -EFAULT;
  1450. goto out;
  1451. }
  1452. /*
  1453. * Now transform/strip the data from YUV to Y-only
  1454. * NB. Assume the Y is in the LSB of the YUV data.
  1455. */
  1456. {
  1457. unsigned char* optr = buf;
  1458. unsigned char* eptr = buf+count;
  1459. /* are we beeing accessed from an old driver? */
  1460. if (count == 2*19*2048) {
  1461. /*
  1462. * Extreme HACK, old VBI programs expect 2048 points
  1463. * of data, and we only got 864 orso. Double each
  1464. * datapoint and clear the rest of the line.
  1465. * This way we have appear to have a
  1466. * sample_frequency of 29.5 Mc.
  1467. */
  1468. int x,y;
  1469. unsigned char* iptr = done->memadr+1;
  1470. for (y=done->h; optr<eptr && y<0; y++)
  1471. {
  1472. /* copy to doubled data to userland */
  1473. for (x=0; optr+1<eptr && x<-done->w; x++)
  1474. {
  1475. unsigned char a = iptr[x*2];
  1476. __put_user(a, optr++);
  1477. __put_user(a, optr++);
  1478. }
  1479. /* and clear the rest of the line */
  1480. for (x*=2; optr<eptr && x<done->bpl; x++)
  1481. __put_user(0, optr++);
  1482. /* next line */
  1483. iptr += done->bpl;
  1484. }
  1485. }
  1486. else {
  1487. /*
  1488. * Other (probably newer) programs asked
  1489. * us what geometry we are using, and are
  1490. * reading the correct size.
  1491. */
  1492. int x,y;
  1493. unsigned char* iptr = done->memadr+1;
  1494. for (y=done->h; optr<eptr && y<0; y++)
  1495. {
  1496. /* copy to doubled data to userland */
  1497. for (x=0; optr<eptr && x<-done->w; x++)
  1498. __put_user(iptr[x*2], optr++);
  1499. /* and clear the rest of the line */
  1500. for (;optr<eptr && x<done->bpl; x++)
  1501. __put_user(0, optr++);
  1502. /* next line */
  1503. iptr += done->bpl;
  1504. }
  1505. }
  1506. /* API compliance:
  1507. * place the framenumber (half fieldnr) in the last long
  1508. */
  1509. __put_user(done->fieldnr/2, ((ulong*)eptr)[-1]);
  1510. }
  1511. /* keep the engine running */
  1512. done->status = FBUFFER_FREE;
  1513. zoran_cap(ztv, 1);
  1514. /* tell listeners this buffer just became free */
  1515. wake_up_interruptible(&ztv->vbiq);
  1516. /* goodbye */
  1517. out:
  1518. DEBUG(printk(CARD_DEBUG "vbi_read() returns %lu\n",CARD,count));
  1519. return count;
  1520. }
  1521. static
  1522. unsigned int vbi_poll(struct video_device *dev, struct file *file, poll_table *wait)
  1523. {
  1524. struct zoran *ztv = dev->priv;
  1525. struct vidinfo* item;
  1526. unsigned int mask = 0;
  1527. poll_wait(file, &ztv->vbiq, wait);
  1528. for (item=ztv->readinfo; item!=ztv->readinfo+ZORAN_VBI_BUFFERS; item++)
  1529. if (item->status == FBUFFER_DONE)
  1530. {
  1531. mask |= (POLLIN | POLLRDNORM);
  1532. break;
  1533. }
  1534. DEBUG(printk(CARD_DEBUG "vbi_poll()=%x\n",CARD,mask));
  1535. return mask;
  1536. }
  1537. static
  1538. int vbi_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
  1539. {
  1540. struct zoran* ztv = dev->priv;
  1541. switch (cmd) {
  1542. case VIDIOCGVBIFMT:
  1543. {
  1544. struct vbi_format f;
  1545. DEBUG(printk(CARD_DEBUG "VIDIOCGVBIINFO\n",CARD));
  1546. f.sampling_rate = 14750000UL;
  1547. f.samples_per_line = -ztv->readinfo[0].w;
  1548. f.sample_format = VIDEO_PALETTE_RAW;
  1549. f.start[0] = f.start[1] = ztv->readinfo[0].y;
  1550. f.start[1] += 312;
  1551. f.count[0] = f.count[1] = -ztv->readinfo[0].h;
  1552. f.flags = VBI_INTERLACED;
  1553. if (copy_to_user(arg,&f,sizeof(f)))
  1554. return -EFAULT;
  1555. break;
  1556. }
  1557. case VIDIOCSVBIFMT:
  1558. {
  1559. struct vbi_format f;
  1560. int i;
  1561. if (copy_from_user(&f, arg,sizeof(f)))
  1562. return -EFAULT;
  1563. 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));
  1564. /* lots of parameters are fixed... (PAL) */
  1565. if (f.sampling_rate != 14750000UL ||
  1566. f.samples_per_line > 864 ||
  1567. f.sample_format != VIDEO_PALETTE_RAW ||
  1568. f.start[0] < 0 ||
  1569. f.start[0] != f.start[1]-312 ||
  1570. f.count[0] != f.count[1] ||
  1571. f.start[0]+f.count[0] >= 288 ||
  1572. f.flags != VBI_INTERLACED)
  1573. return -EINVAL;
  1574. write_lock_irq(&ztv->lock);
  1575. ztv->readinfo[0].y = f.start[0];
  1576. ztv->readinfo[0].w = -f.samples_per_line;
  1577. ztv->readinfo[0].h = -f.count[0];
  1578. ztv->readinfo[0].bpl = f.samples_per_line*ztv->readinfo[0].bpp;
  1579. for (i=1; i<ZORAN_VBI_BUFFERS; i++)
  1580. ztv->readinfo[i] = ztv->readinfo[i];
  1581. write_unlock_irq(&ztv->lock);
  1582. break;
  1583. }
  1584. default:
  1585. return -ENOIOCTLCMD;
  1586. }
  1587. return 0;
  1588. }
  1589. static struct video_device vbi_template=
  1590. {
  1591. .owner = THIS_MODULE,
  1592. .name = "UNSET",
  1593. .type = VID_TYPE_CAPTURE|VID_TYPE_TELETEXT,
  1594. .hardware = VID_HARDWARE_ZR36120,
  1595. .open = vbi_open,
  1596. .close = vbi_close,
  1597. .read = vbi_read,
  1598. .write = zoran_write,
  1599. .poll = vbi_poll,
  1600. .ioctl = vbi_ioctl,
  1601. .minor = -1,
  1602. };
  1603. /*
  1604. * Scan for a Zoran chip, request the irq and map the io memory
  1605. */
  1606. static
  1607. int __init find_zoran(void)
  1608. {
  1609. int result;
  1610. struct zoran *ztv;
  1611. struct pci_dev *dev = NULL;
  1612. unsigned char revision;
  1613. int zoran_num=0;
  1614. while ((dev = pci_find_device(PCI_VENDOR_ID_ZORAN,PCI_DEVICE_ID_ZORAN_36120, dev)))
  1615. {
  1616. /* Ok, a ZR36120/ZR36125 found! */
  1617. ztv = &zorans[zoran_num];
  1618. ztv->dev = dev;
  1619. if (pci_enable_device(dev))
  1620. return -EIO;
  1621. pci_read_config_byte(dev, PCI_CLASS_REVISION, &revision);
  1622. printk(KERN_INFO "zoran: Zoran %x (rev %d) ",
  1623. dev->device, revision);
  1624. printk("bus: %d, devfn: %d, irq: %d, ",
  1625. dev->bus->number, dev->devfn, dev->irq);
  1626. printk("memory: 0x%08lx.\n", ztv->zoran_adr);
  1627. ztv->zoran_mem = ioremap(ztv->zoran_adr, 0x1000);
  1628. DEBUG(printk(KERN_DEBUG "zoran: mapped-memory at 0x%p\n",ztv->zoran_mem));
  1629. result = request_irq(dev->irq, zoran_irq,
  1630. SA_SHIRQ|SA_INTERRUPT,"zoran", ztv);
  1631. if (result==-EINVAL)
  1632. {
  1633. iounmap(ztv->zoran_mem);
  1634. printk(KERN_ERR "zoran: Bad irq number or handler\n");
  1635. return -EINVAL;
  1636. }
  1637. if (result==-EBUSY)
  1638. printk(KERN_ERR "zoran: IRQ %d busy, change your PnP config in BIOS\n",dev->irq);
  1639. if (result < 0) {
  1640. iounmap(ztv->zoran_mem);
  1641. return result;
  1642. }
  1643. /* Enable bus-mastering */
  1644. pci_set_master(dev);
  1645. zoran_num++;
  1646. }
  1647. if(zoran_num)
  1648. printk(KERN_INFO "zoran: %d Zoran card(s) found.\n",zoran_num);
  1649. return zoran_num;
  1650. }
  1651. static
  1652. int __init init_zoran(int card)
  1653. {
  1654. struct zoran *ztv = &zorans[card];
  1655. int i;
  1656. /* if the given cardtype valid? */
  1657. if (cardtype[card]>=NRTVCARDS) {
  1658. printk(KERN_INFO "invalid cardtype(%d) detected\n",cardtype[card]);
  1659. return -1;
  1660. }
  1661. /* reset the zoran */
  1662. zrand(~ZORAN_PCI_SOFTRESET,ZORAN_PCI);
  1663. udelay(10);
  1664. zror(ZORAN_PCI_SOFTRESET,ZORAN_PCI);
  1665. udelay(10);
  1666. /* zoran chip specific details */
  1667. ztv->card = tvcards+cardtype[card]; /* point to the selected card */
  1668. ztv->norm = 0; /* PAL */
  1669. ztv->tuner_freq = 0;
  1670. /* videocard details */
  1671. ztv->swidth = 800;
  1672. ztv->sheight = 600;
  1673. ztv->depth = 16;
  1674. /* State details */
  1675. ztv->fbuffer = 0;
  1676. ztv->overinfo.kindof = FBUFFER_OVERLAY;
  1677. ztv->overinfo.status = FBUFFER_FREE;
  1678. ztv->overinfo.x = 0;
  1679. ztv->overinfo.y = 0;
  1680. ztv->overinfo.w = 768; /* 640 */
  1681. ztv->overinfo.h = 576; /* 480 */
  1682. ztv->overinfo.format = VIDEO_PALETTE_RGB565;
  1683. ztv->overinfo.bpp = palette2fmt[ztv->overinfo.format].bpp;
  1684. ztv->overinfo.bpl = ztv->overinfo.bpp*ztv->swidth;
  1685. ztv->overinfo.busadr = 0;
  1686. ztv->overinfo.memadr = 0;
  1687. ztv->overinfo.overlay = 0;
  1688. for (i=0; i<ZORAN_MAX_FBUFFERS; i++) {
  1689. ztv->grabinfo[i] = ztv->overinfo;
  1690. ztv->grabinfo[i].kindof = FBUFFER_GRAB;
  1691. }
  1692. init_waitqueue_head(&ztv->grabq);
  1693. /* VBI details */
  1694. ztv->readinfo[0] = ztv->overinfo;
  1695. ztv->readinfo[0].kindof = FBUFFER_VBI;
  1696. ztv->readinfo[0].w = -864;
  1697. ztv->readinfo[0].h = -38;
  1698. ztv->readinfo[0].format = VIDEO_PALETTE_YUV422;
  1699. ztv->readinfo[0].bpp = palette2fmt[ztv->readinfo[0].format].bpp;
  1700. ztv->readinfo[0].bpl = 1024*ztv->readinfo[0].bpp;
  1701. for (i=1; i<ZORAN_VBI_BUFFERS; i++)
  1702. ztv->readinfo[i] = ztv->readinfo[0];
  1703. init_waitqueue_head(&ztv->vbiq);
  1704. /* maintenance data */
  1705. ztv->have_decoder = 0;
  1706. ztv->have_tuner = 0;
  1707. ztv->tuner_type = 0;
  1708. ztv->running = 0;
  1709. ztv->users = 0;
  1710. rwlock_init(&ztv->lock);
  1711. ztv->workqueue = 0;
  1712. ztv->fieldnr = 0;
  1713. ztv->lastfieldnr = 0;
  1714. if (triton1)
  1715. zrand(~ZORAN_VDC_TRICOM, ZORAN_VDC);
  1716. /* external FL determines TOP frame */
  1717. zror(ZORAN_VFEC_EXTFL, ZORAN_VFEC);
  1718. /* set HSpol */
  1719. if (ztv->card->hsync_pos)
  1720. zrwrite(ZORAN_VFEH_HSPOL, ZORAN_VFEH);
  1721. /* set VSpol */
  1722. if (ztv->card->vsync_pos)
  1723. zrwrite(ZORAN_VFEV_VSPOL, ZORAN_VFEV);
  1724. /* Set the proper General Purpuse register bits */
  1725. /* implicit: no softreset, 0 waitstates */
  1726. zrwrite(ZORAN_PCI_SOFTRESET|(ztv->card->gpdir<<0),ZORAN_PCI);
  1727. /* implicit: 3 duration and recovery PCI clocks on guest 0-3 */
  1728. zrwrite(ztv->card->gpval<<24,ZORAN_GUEST);
  1729. /* clear interrupt status */
  1730. zrwrite(~0, ZORAN_ISR);
  1731. /*
  1732. * i2c template
  1733. */
  1734. ztv->i2c = zoran_i2c_bus_template;
  1735. sprintf(ztv->i2c.name,"zoran-%d",card);
  1736. ztv->i2c.data = ztv;
  1737. /*
  1738. * Now add the template and register the device unit
  1739. */
  1740. ztv->video_dev = zr36120_template;
  1741. strcpy(ztv->video_dev.name, ztv->i2c.name);
  1742. ztv->video_dev.priv = ztv;
  1743. if (video_register_device(&ztv->video_dev, VFL_TYPE_GRABBER, video_nr) < 0)
  1744. return -1;
  1745. ztv->vbi_dev = vbi_template;
  1746. strcpy(ztv->vbi_dev.name, ztv->i2c.name);
  1747. ztv->vbi_dev.priv = ztv;
  1748. if (video_register_device(&ztv->vbi_dev, VFL_TYPE_VBI, vbi_nr) < 0) {
  1749. video_unregister_device(&ztv->video_dev);
  1750. return -1;
  1751. }
  1752. i2c_register_bus(&ztv->i2c);
  1753. /* set interrupt mask - the PIN enable will be set later */
  1754. zrwrite(ZORAN_ICR_GIRQ0|ZORAN_ICR_GIRQ1|ZORAN_ICR_CODE, ZORAN_ICR);
  1755. printk(KERN_INFO "%s: installed %s\n",ztv->i2c.name,ztv->card->name);
  1756. return 0;
  1757. }
  1758. static
  1759. void release_zoran(int max)
  1760. {
  1761. struct zoran *ztv;
  1762. int i;
  1763. for (i=0;i<max; i++)
  1764. {
  1765. ztv = &zorans[i];
  1766. /* turn off all capturing, DMA and IRQs */
  1767. /* reset the zoran */
  1768. zrand(~ZORAN_PCI_SOFTRESET,ZORAN_PCI);
  1769. udelay(10);
  1770. zror(ZORAN_PCI_SOFTRESET,ZORAN_PCI);
  1771. udelay(10);
  1772. /* first disable interrupts before unmapping the memory! */
  1773. zrwrite(0, ZORAN_ICR);
  1774. zrwrite(0xffffffffUL,ZORAN_ISR);
  1775. /* free it */
  1776. free_irq(ztv->dev->irq,ztv);
  1777. /* unregister i2c_bus */
  1778. i2c_unregister_bus((&ztv->i2c));
  1779. /* unmap and free memory */
  1780. if (ztv->zoran_mem)
  1781. iounmap(ztv->zoran_mem);
  1782. video_unregister_device(&ztv->video_dev);
  1783. video_unregister_device(&ztv->vbi_dev);
  1784. }
  1785. }
  1786. void __exit zr36120_exit(void)
  1787. {
  1788. release_zoran(zoran_cards);
  1789. }
  1790. int __init zr36120_init(void)
  1791. {
  1792. int card;
  1793. handle_chipset();
  1794. zoran_cards = find_zoran();
  1795. if (zoran_cards<0)
  1796. /* no cards found, no need for a driver */
  1797. return -EIO;
  1798. /* initialize Zorans */
  1799. for (card=0; card<zoran_cards; card++) {
  1800. if (init_zoran(card)<0) {
  1801. /* only release the zorans we have registered */
  1802. release_zoran(card);
  1803. return -EIO;
  1804. }
  1805. }
  1806. return 0;
  1807. }
  1808. module_init(zr36120_init);
  1809. module_exit(zr36120_exit);