zoran_card.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645
  1. /*
  2. * Zoran zr36057/zr36067 PCI controller driver, for the
  3. * Pinnacle/Miro DC10/DC10+/DC30/DC30+, Iomega Buz, Linux
  4. * Media Labs LML33/LML33R10.
  5. *
  6. * This part handles card-specific data and detection
  7. *
  8. * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
  9. *
  10. * Currently maintained by:
  11. * Ronald Bultje <rbultje@ronald.bitfreak.net>
  12. * Laurent Pinchart <laurent.pinchart@skynet.be>
  13. * Mailinglist <mjpeg-users@lists.sf.net>
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  28. */
  29. #include <linux/delay.h>
  30. #include <linux/types.h>
  31. #include <linux/kernel.h>
  32. #include <linux/module.h>
  33. #include <linux/init.h>
  34. #include <linux/vmalloc.h>
  35. #include <linux/proc_fs.h>
  36. #include <linux/i2c.h>
  37. #include <linux/i2c-algo-bit.h>
  38. #include <linux/videodev.h>
  39. #include <media/v4l2-common.h>
  40. #include <linux/spinlock.h>
  41. #include <linux/sem.h>
  42. #include <linux/kmod.h>
  43. #include <linux/wait.h>
  44. #include <linux/pci.h>
  45. #include <linux/interrupt.h>
  46. #include <linux/video_decoder.h>
  47. #include <linux/video_encoder.h>
  48. #include <linux/mutex.h>
  49. #include <asm/io.h>
  50. #include "videocodec.h"
  51. #include "zoran.h"
  52. #include "zoran_card.h"
  53. #include "zoran_device.h"
  54. #include "zoran_procfs.h"
  55. extern const struct zoran_format zoran_formats[];
  56. static int card[BUZ_MAX] = { [0 ... (BUZ_MAX-1)] = -1 };
  57. module_param_array(card, int, NULL, 0444);
  58. MODULE_PARM_DESC(card, "Card type");
  59. static int encoder[BUZ_MAX] = { [0 ... (BUZ_MAX-1)] = -1 };
  60. module_param_array(encoder, int, NULL, 0444);
  61. MODULE_PARM_DESC(encoder, "Video encoder chip");
  62. static int decoder[BUZ_MAX] = { [0 ... (BUZ_MAX-1)] = -1 };
  63. module_param_array(decoder, int, NULL, 0444);
  64. MODULE_PARM_DESC(decoder, "Video decoder chip");
  65. /*
  66. The video mem address of the video card.
  67. The driver has a little database for some videocards
  68. to determine it from there. If your video card is not in there
  69. you have either to give it to the driver as a parameter
  70. or set in in a VIDIOCSFBUF ioctl
  71. */
  72. static unsigned long vidmem; /* default = 0 - Video memory base address */
  73. module_param(vidmem, ulong, 0444);
  74. MODULE_PARM_DESC(vidmem, "Default video memory base address");
  75. /*
  76. Default input and video norm at startup of the driver.
  77. */
  78. static unsigned int default_input; /* default 0 = Composite, 1 = S-Video */
  79. module_param(default_input, uint, 0444);
  80. MODULE_PARM_DESC(default_input,
  81. "Default input (0=Composite, 1=S-Video, 2=Internal)");
  82. static int default_mux = 1; /* 6 Eyes input selection */
  83. module_param(default_mux, int, 0644);
  84. MODULE_PARM_DESC(default_mux,
  85. "Default 6 Eyes mux setting (Input selection)");
  86. static int default_norm; /* default 0 = PAL, 1 = NTSC 2 = SECAM */
  87. module_param(default_norm, int, 0444);
  88. MODULE_PARM_DESC(default_norm, "Default norm (0=PAL, 1=NTSC, 2=SECAM)");
  89. /* /dev/videoN, -1 for autodetect */
  90. static int video_nr[BUZ_MAX] = { [0 ... (BUZ_MAX-1)] = -1 };
  91. module_param_array(video_nr, int, NULL, 0444);
  92. MODULE_PARM_DESC(video_nr, "Video device number (-1=Auto)");
  93. /*
  94. Number and size of grab buffers for Video 4 Linux
  95. The vast majority of applications should not need more than 2,
  96. the very popular BTTV driver actually does ONLY have 2.
  97. Time sensitive applications might need more, the maximum
  98. is VIDEO_MAX_FRAME (defined in <linux/videodev.h>).
  99. The size is set so that the maximum possible request
  100. can be satisfied. Decrease it, if bigphys_area alloc'd
  101. memory is low. If you don't have the bigphys_area patch,
  102. set it to 128 KB. Will you allow only to grab small
  103. images with V4L, but that's better than nothing.
  104. v4l_bufsize has to be given in KB !
  105. */
  106. int v4l_nbufs = 2;
  107. int v4l_bufsize = 128; /* Everybody should be able to work with this setting */
  108. module_param(v4l_nbufs, int, 0644);
  109. MODULE_PARM_DESC(v4l_nbufs, "Maximum number of V4L buffers to use");
  110. module_param(v4l_bufsize, int, 0644);
  111. MODULE_PARM_DESC(v4l_bufsize, "Maximum size per V4L buffer (in kB)");
  112. int jpg_nbufs = 32;
  113. int jpg_bufsize = 512; /* max size for 100% quality full-PAL frame */
  114. module_param(jpg_nbufs, int, 0644);
  115. MODULE_PARM_DESC(jpg_nbufs, "Maximum number of JPG buffers to use");
  116. module_param(jpg_bufsize, int, 0644);
  117. MODULE_PARM_DESC(jpg_bufsize, "Maximum size per JPG buffer (in kB)");
  118. int pass_through = 0; /* 1=Pass through TV signal when device is not used */
  119. /* 0=Show color bar when device is not used (LML33: only if lml33dpath=1) */
  120. module_param(pass_through, int, 0644);
  121. MODULE_PARM_DESC(pass_through,
  122. "Pass TV signal through to TV-out when idling");
  123. int zr36067_debug = 1;
  124. module_param_named(debug, zr36067_debug, int, 0644);
  125. MODULE_PARM_DESC(debug, "Debug level (0-5)");
  126. MODULE_DESCRIPTION("Zoran-36057/36067 JPEG codec driver");
  127. MODULE_AUTHOR("Serguei Miridonov");
  128. MODULE_LICENSE("GPL");
  129. #define ZR_DEVICE(subven, subdev, data) { \
  130. .vendor = PCI_VENDOR_ID_ZORAN, .device = PCI_DEVICE_ID_ZORAN_36057, \
  131. .subvendor = (subven), .subdevice = (subdev), .driver_data = (data) }
  132. static struct pci_device_id zr36067_pci_tbl[] = {
  133. ZR_DEVICE(PCI_VENDOR_ID_MIRO, PCI_DEVICE_ID_MIRO_DC10PLUS, DC10plus),
  134. ZR_DEVICE(PCI_VENDOR_ID_MIRO, PCI_DEVICE_ID_MIRO_DC30PLUS, DC30plus),
  135. ZR_DEVICE(PCI_VENDOR_ID_ELECTRONICDESIGNGMBH, PCI_DEVICE_ID_LML_33R10, LML33R10),
  136. ZR_DEVICE(PCI_VENDOR_ID_IOMEGA, PCI_DEVICE_ID_IOMEGA_BUZ, BUZ),
  137. ZR_DEVICE(PCI_ANY_ID, PCI_ANY_ID, NUM_CARDS),
  138. {0}
  139. };
  140. MODULE_DEVICE_TABLE(pci, zr36067_pci_tbl);
  141. static unsigned int zoran_num; /* number of cards found */
  142. /* videocodec bus functions ZR36060 */
  143. static u32
  144. zr36060_read (struct videocodec *codec,
  145. u16 reg)
  146. {
  147. struct zoran *zr = (struct zoran *) codec->master_data->data;
  148. __u32 data;
  149. if (post_office_wait(zr)
  150. || post_office_write(zr, 0, 1, reg >> 8)
  151. || post_office_write(zr, 0, 2, reg & 0xff)) {
  152. return -1;
  153. }
  154. data = post_office_read(zr, 0, 3) & 0xff;
  155. return data;
  156. }
  157. static void
  158. zr36060_write (struct videocodec *codec,
  159. u16 reg,
  160. u32 val)
  161. {
  162. struct zoran *zr = (struct zoran *) codec->master_data->data;
  163. if (post_office_wait(zr)
  164. || post_office_write(zr, 0, 1, reg >> 8)
  165. || post_office_write(zr, 0, 2, reg & 0xff)) {
  166. return;
  167. }
  168. post_office_write(zr, 0, 3, val & 0xff);
  169. }
  170. /* videocodec bus functions ZR36050 */
  171. static u32
  172. zr36050_read (struct videocodec *codec,
  173. u16 reg)
  174. {
  175. struct zoran *zr = (struct zoran *) codec->master_data->data;
  176. __u32 data;
  177. if (post_office_wait(zr)
  178. || post_office_write(zr, 1, 0, reg >> 2)) { // reg. HIGHBYTES
  179. return -1;
  180. }
  181. data = post_office_read(zr, 0, reg & 0x03) & 0xff; // reg. LOWBYTES + read
  182. return data;
  183. }
  184. static void
  185. zr36050_write (struct videocodec *codec,
  186. u16 reg,
  187. u32 val)
  188. {
  189. struct zoran *zr = (struct zoran *) codec->master_data->data;
  190. if (post_office_wait(zr)
  191. || post_office_write(zr, 1, 0, reg >> 2)) { // reg. HIGHBYTES
  192. return;
  193. }
  194. post_office_write(zr, 0, reg & 0x03, val & 0xff); // reg. LOWBYTES + wr. data
  195. }
  196. /* videocodec bus functions ZR36016 */
  197. static u32
  198. zr36016_read (struct videocodec *codec,
  199. u16 reg)
  200. {
  201. struct zoran *zr = (struct zoran *) codec->master_data->data;
  202. __u32 data;
  203. if (post_office_wait(zr)) {
  204. return -1;
  205. }
  206. data = post_office_read(zr, 2, reg & 0x03) & 0xff; // read
  207. return data;
  208. }
  209. /* hack for in zoran_device.c */
  210. void
  211. zr36016_write (struct videocodec *codec,
  212. u16 reg,
  213. u32 val)
  214. {
  215. struct zoran *zr = (struct zoran *) codec->master_data->data;
  216. if (post_office_wait(zr)) {
  217. return;
  218. }
  219. post_office_write(zr, 2, reg & 0x03, val & 0x0ff); // wr. data
  220. }
  221. /*
  222. * Board specific information
  223. */
  224. static void
  225. dc10_init (struct zoran *zr)
  226. {
  227. dprintk(3, KERN_DEBUG "%s: dc10_init()\n", ZR_DEVNAME(zr));
  228. /* Pixel clock selection */
  229. GPIO(zr, 4, 0);
  230. GPIO(zr, 5, 1);
  231. /* Enable the video bus sync signals */
  232. GPIO(zr, 7, 0);
  233. }
  234. static void
  235. dc10plus_init (struct zoran *zr)
  236. {
  237. dprintk(3, KERN_DEBUG "%s: dc10plus_init()\n", ZR_DEVNAME(zr));
  238. }
  239. static void
  240. buz_init (struct zoran *zr)
  241. {
  242. dprintk(3, KERN_DEBUG "%s: buz_init()\n", ZR_DEVNAME(zr));
  243. /* some stuff from Iomega */
  244. pci_write_config_dword(zr->pci_dev, 0xfc, 0x90680f15);
  245. pci_write_config_dword(zr->pci_dev, 0x0c, 0x00012020);
  246. pci_write_config_dword(zr->pci_dev, 0xe8, 0xc0200000);
  247. }
  248. static void
  249. lml33_init (struct zoran *zr)
  250. {
  251. dprintk(3, KERN_DEBUG "%s: lml33_init()\n", ZR_DEVNAME(zr));
  252. GPIO(zr, 2, 1); // Set Composite input/output
  253. }
  254. static void
  255. avs6eyes_init (struct zoran *zr)
  256. {
  257. // AverMedia 6-Eyes original driver by Christer Weinigel
  258. // Lifted straight from Christer's old driver and
  259. // modified slightly by Martin Samuelsson.
  260. int mux = default_mux; /* 1 = BT866, 7 = VID1 */
  261. GPIO(zr, 4, 1); /* Bt866 SLEEP on */
  262. udelay(2);
  263. GPIO(zr, 0, 1); /* ZR36060 /RESET on */
  264. GPIO(zr, 1, 0); /* ZR36060 /SLEEP on */
  265. GPIO(zr, 2, mux & 1); /* MUX S0 */
  266. GPIO(zr, 3, 0); /* /FRAME on */
  267. GPIO(zr, 4, 0); /* Bt866 SLEEP off */
  268. GPIO(zr, 5, mux & 2); /* MUX S1 */
  269. GPIO(zr, 6, 0); /* ? */
  270. GPIO(zr, 7, mux & 4); /* MUX S2 */
  271. }
  272. static char *
  273. i2cid_to_modulename (u16 i2c_id)
  274. {
  275. char *name = NULL;
  276. switch (i2c_id) {
  277. case I2C_DRIVERID_SAA7110:
  278. name = "saa7110";
  279. break;
  280. case I2C_DRIVERID_SAA7111A:
  281. name = "saa7111";
  282. break;
  283. case I2C_DRIVERID_SAA7114:
  284. name = "saa7114";
  285. break;
  286. case I2C_DRIVERID_SAA7185B:
  287. name = "saa7185";
  288. break;
  289. case I2C_DRIVERID_ADV7170:
  290. name = "adv7170";
  291. break;
  292. case I2C_DRIVERID_ADV7175:
  293. name = "adv7175";
  294. break;
  295. case I2C_DRIVERID_BT819:
  296. name = "bt819";
  297. break;
  298. case I2C_DRIVERID_BT856:
  299. name = "bt856";
  300. break;
  301. case I2C_DRIVERID_BT866:
  302. name = "bt866";
  303. break;
  304. case I2C_DRIVERID_VPX3220:
  305. name = "vpx3220";
  306. break;
  307. case I2C_DRIVERID_KS0127:
  308. name = "ks0127";
  309. break;
  310. }
  311. return name;
  312. }
  313. static char *
  314. codecid_to_modulename (u16 codecid)
  315. {
  316. char *name = NULL;
  317. switch (codecid) {
  318. case CODEC_TYPE_ZR36060:
  319. name = "zr36060";
  320. break;
  321. case CODEC_TYPE_ZR36050:
  322. name = "zr36050";
  323. break;
  324. case CODEC_TYPE_ZR36016:
  325. name = "zr36016";
  326. break;
  327. }
  328. return name;
  329. }
  330. // struct tvnorm {
  331. // u16 Wt, Wa, HStart, HSyncStart, Ht, Ha, VStart;
  332. // };
  333. static struct tvnorm f50sqpixel = { 944, 768, 83, 880, 625, 576, 16 };
  334. static struct tvnorm f60sqpixel = { 780, 640, 51, 716, 525, 480, 12 };
  335. static struct tvnorm f50ccir601 = { 864, 720, 75, 804, 625, 576, 18 };
  336. static struct tvnorm f60ccir601 = { 858, 720, 57, 788, 525, 480, 16 };
  337. static struct tvnorm f50ccir601_lml33 = { 864, 720, 75+34, 804, 625, 576, 18 };
  338. static struct tvnorm f60ccir601_lml33 = { 858, 720, 57+34, 788, 525, 480, 16 };
  339. /* The DC10 (57/16/50) uses VActive as HSync, so HStart must be 0 */
  340. static struct tvnorm f50sqpixel_dc10 = { 944, 768, 0, 880, 625, 576, 0 };
  341. static struct tvnorm f60sqpixel_dc10 = { 780, 640, 0, 716, 525, 480, 12 };
  342. /* FIXME: I cannot swap U and V in saa7114, so i do one
  343. * pixel left shift in zoran (75 -> 74)
  344. * (Maxim Yevtyushkin <max@linuxmedialabs.com>) */
  345. static struct tvnorm f50ccir601_lm33r10 = { 864, 720, 74+54, 804, 625, 576, 18 };
  346. static struct tvnorm f60ccir601_lm33r10 = { 858, 720, 56+54, 788, 525, 480, 16 };
  347. /* FIXME: The ks0127 seem incapable of swapping U and V, too, which is why I
  348. * copy Maxim's left shift hack for the 6 Eyes.
  349. *
  350. * Christer's driver used the unshifted norms, though...
  351. * /Sam */
  352. static struct tvnorm f50ccir601_avs6eyes = { 864, 720, 74, 804, 625, 576, 18 };
  353. static struct tvnorm f60ccir601_avs6eyes = { 858, 720, 56, 788, 525, 480, 16 };
  354. static struct card_info zoran_cards[NUM_CARDS] __devinitdata = {
  355. {
  356. .type = DC10_old,
  357. .name = "DC10(old)",
  358. .i2c_decoder = I2C_DRIVERID_VPX3220,
  359. .video_codec = CODEC_TYPE_ZR36050,
  360. .video_vfe = CODEC_TYPE_ZR36016,
  361. .inputs = 3,
  362. .input = {
  363. { 1, "Composite" },
  364. { 2, "S-Video" },
  365. { 0, "Internal/comp" }
  366. },
  367. .norms = 3,
  368. .tvn = {
  369. &f50sqpixel_dc10,
  370. &f60sqpixel_dc10,
  371. &f50sqpixel_dc10
  372. },
  373. .jpeg_int = 0,
  374. .vsync_int = ZR36057_ISR_GIRQ1,
  375. .gpio = { 2, 1, -1, 3, 7, 0, 4, 5 },
  376. .gpio_pol = { 0, 0, 0, 1, 0, 0, 0, 0 },
  377. .gpcs = { -1, 0 },
  378. .vfe_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
  379. .gws_not_connected = 0,
  380. .input_mux = 0,
  381. .init = &dc10_init,
  382. }, {
  383. .type = DC10_new,
  384. .name = "DC10(new)",
  385. .i2c_decoder = I2C_DRIVERID_SAA7110,
  386. .i2c_encoder = I2C_DRIVERID_ADV7175,
  387. .video_codec = CODEC_TYPE_ZR36060,
  388. .inputs = 3,
  389. .input = {
  390. { 0, "Composite" },
  391. { 7, "S-Video" },
  392. { 5, "Internal/comp" }
  393. },
  394. .norms = 3,
  395. .tvn = {
  396. &f50sqpixel,
  397. &f60sqpixel,
  398. &f50sqpixel},
  399. .jpeg_int = ZR36057_ISR_GIRQ0,
  400. .vsync_int = ZR36057_ISR_GIRQ1,
  401. .gpio = { 3, 0, 6, 1, 2, -1, 4, 5 },
  402. .gpio_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
  403. .gpcs = { -1, 1},
  404. .vfe_pol = { 1, 1, 1, 1, 0, 0, 0, 0 },
  405. .gws_not_connected = 0,
  406. .input_mux = 0,
  407. .init = &dc10plus_init,
  408. }, {
  409. .type = DC10plus,
  410. .name = "DC10plus",
  411. .i2c_decoder = I2C_DRIVERID_SAA7110,
  412. .i2c_encoder = I2C_DRIVERID_ADV7175,
  413. .video_codec = CODEC_TYPE_ZR36060,
  414. .inputs = 3,
  415. .input = {
  416. { 0, "Composite" },
  417. { 7, "S-Video" },
  418. { 5, "Internal/comp" }
  419. },
  420. .norms = 3,
  421. .tvn = {
  422. &f50sqpixel,
  423. &f60sqpixel,
  424. &f50sqpixel
  425. },
  426. .jpeg_int = ZR36057_ISR_GIRQ0,
  427. .vsync_int = ZR36057_ISR_GIRQ1,
  428. .gpio = { 3, 0, 6, 1, 2, -1, 4, 5 },
  429. .gpio_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
  430. .gpcs = { -1, 1 },
  431. .vfe_pol = { 1, 1, 1, 1, 0, 0, 0, 0 },
  432. .gws_not_connected = 0,
  433. .input_mux = 0,
  434. .init = &dc10plus_init,
  435. }, {
  436. .type = DC30,
  437. .name = "DC30",
  438. .i2c_decoder = I2C_DRIVERID_VPX3220,
  439. .i2c_encoder = I2C_DRIVERID_ADV7175,
  440. .video_codec = CODEC_TYPE_ZR36050,
  441. .video_vfe = CODEC_TYPE_ZR36016,
  442. .inputs = 3,
  443. .input = {
  444. { 1, "Composite" },
  445. { 2, "S-Video" },
  446. { 0, "Internal/comp" }
  447. },
  448. .norms = 3,
  449. .tvn = {
  450. &f50sqpixel_dc10,
  451. &f60sqpixel_dc10,
  452. &f50sqpixel_dc10
  453. },
  454. .jpeg_int = 0,
  455. .vsync_int = ZR36057_ISR_GIRQ1,
  456. .gpio = { 2, 1, -1, 3, 7, 0, 4, 5 },
  457. .gpio_pol = { 0, 0, 0, 1, 0, 0, 0, 0 },
  458. .gpcs = { -1, 0 },
  459. .vfe_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
  460. .gws_not_connected = 0,
  461. .input_mux = 0,
  462. .init = &dc10_init,
  463. }, {
  464. .type = DC30plus,
  465. .name = "DC30plus",
  466. .i2c_decoder = I2C_DRIVERID_VPX3220,
  467. .i2c_encoder = I2C_DRIVERID_ADV7175,
  468. .video_codec = CODEC_TYPE_ZR36050,
  469. .video_vfe = CODEC_TYPE_ZR36016,
  470. .inputs = 3,
  471. .input = {
  472. { 1, "Composite" },
  473. { 2, "S-Video" },
  474. { 0, "Internal/comp" }
  475. },
  476. .norms = 3,
  477. .tvn = {
  478. &f50sqpixel_dc10,
  479. &f60sqpixel_dc10,
  480. &f50sqpixel_dc10
  481. },
  482. .jpeg_int = 0,
  483. .vsync_int = ZR36057_ISR_GIRQ1,
  484. .gpio = { 2, 1, -1, 3, 7, 0, 4, 5 },
  485. .gpio_pol = { 0, 0, 0, 1, 0, 0, 0, 0 },
  486. .gpcs = { -1, 0 },
  487. .vfe_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
  488. .gws_not_connected = 0,
  489. .input_mux = 0,
  490. .init = &dc10_init,
  491. }, {
  492. .type = LML33,
  493. .name = "LML33",
  494. .i2c_decoder = I2C_DRIVERID_BT819,
  495. .i2c_encoder = I2C_DRIVERID_BT856,
  496. .video_codec = CODEC_TYPE_ZR36060,
  497. .inputs = 2,
  498. .input = {
  499. { 0, "Composite" },
  500. { 7, "S-Video" }
  501. },
  502. .norms = 2,
  503. .tvn = {
  504. &f50ccir601_lml33,
  505. &f60ccir601_lml33,
  506. NULL
  507. },
  508. .jpeg_int = ZR36057_ISR_GIRQ1,
  509. .vsync_int = ZR36057_ISR_GIRQ0,
  510. .gpio = { 1, -1, 3, 5, 7, -1, -1, -1 },
  511. .gpio_pol = { 0, 0, 0, 0, 1, 0, 0, 0 },
  512. .gpcs = { 3, 1 },
  513. .vfe_pol = { 1, 1, 0, 0, 0, 1, 0, 0 },
  514. .gws_not_connected = 1,
  515. .input_mux = 0,
  516. .init = &lml33_init,
  517. }, {
  518. .type = LML33R10,
  519. .name = "LML33R10",
  520. .i2c_decoder = I2C_DRIVERID_SAA7114,
  521. .i2c_encoder = I2C_DRIVERID_ADV7170,
  522. .video_codec = CODEC_TYPE_ZR36060,
  523. .inputs = 2,
  524. .input = {
  525. { 0, "Composite" },
  526. { 7, "S-Video" }
  527. },
  528. .norms = 2,
  529. .tvn = {
  530. &f50ccir601_lm33r10,
  531. &f60ccir601_lm33r10,
  532. NULL
  533. },
  534. .jpeg_int = ZR36057_ISR_GIRQ1,
  535. .vsync_int = ZR36057_ISR_GIRQ0,
  536. .gpio = { 1, -1, 3, 5, 7, -1, -1, -1 },
  537. .gpio_pol = { 0, 0, 0, 0, 1, 0, 0, 0 },
  538. .gpcs = { 3, 1 },
  539. .vfe_pol = { 1, 1, 0, 0, 0, 1, 0, 0 },
  540. .gws_not_connected = 1,
  541. .input_mux = 0,
  542. .init = &lml33_init,
  543. }, {
  544. .type = BUZ,
  545. .name = "Buz",
  546. .i2c_decoder = I2C_DRIVERID_SAA7111A,
  547. .i2c_encoder = I2C_DRIVERID_SAA7185B,
  548. .video_codec = CODEC_TYPE_ZR36060,
  549. .inputs = 2,
  550. .input = {
  551. { 3, "Composite" },
  552. { 7, "S-Video" }
  553. },
  554. .norms = 3,
  555. .tvn = {
  556. &f50ccir601,
  557. &f60ccir601,
  558. &f50ccir601
  559. },
  560. .jpeg_int = ZR36057_ISR_GIRQ1,
  561. .vsync_int = ZR36057_ISR_GIRQ0,
  562. .gpio = { 1, -1, 3, -1, -1, -1, -1, -1 },
  563. .gpio_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
  564. .gpcs = { 3, 1 },
  565. .vfe_pol = { 1, 1, 0, 0, 0, 1, 0, 0 },
  566. .gws_not_connected = 1,
  567. .input_mux = 0,
  568. .init = &buz_init,
  569. }, {
  570. .type = AVS6EYES,
  571. .name = "6-Eyes",
  572. /* AverMedia chose not to brand the 6-Eyes. Thus it
  573. can't be autodetected, and requires card=x. */
  574. .i2c_decoder = I2C_DRIVERID_KS0127,
  575. .i2c_encoder = I2C_DRIVERID_BT866,
  576. .video_codec = CODEC_TYPE_ZR36060,
  577. .inputs = 10,
  578. .input = {
  579. { 0, "Composite 1" },
  580. { 1, "Composite 2" },
  581. { 2, "Composite 3" },
  582. { 4, "Composite 4" },
  583. { 5, "Composite 5" },
  584. { 6, "Composite 6" },
  585. { 8, "S-Video 1" },
  586. { 9, "S-Video 2" },
  587. {10, "S-Video 3" },
  588. {15, "YCbCr" }
  589. },
  590. .norms = 2,
  591. .tvn = {
  592. &f50ccir601_avs6eyes,
  593. &f60ccir601_avs6eyes,
  594. NULL
  595. },
  596. .jpeg_int = ZR36057_ISR_GIRQ1,
  597. .vsync_int = ZR36057_ISR_GIRQ0,
  598. .gpio = { 1, 0, 3, -1, -1, -1, -1, -1 },// Validity unknown /Sam
  599. .gpio_pol = { 0, 0, 0, 0, 0, 0, 0, 0 }, // Validity unknown /Sam
  600. .gpcs = { 3, 1 }, // Validity unknown /Sam
  601. .vfe_pol = { 1, 0, 0, 0, 0, 1, 0, 0 }, // Validity unknown /Sam
  602. .gws_not_connected = 1,
  603. .input_mux = 1,
  604. .init = &avs6eyes_init,
  605. }
  606. };
  607. /*
  608. * I2C functions
  609. */
  610. /* software I2C functions */
  611. static int
  612. zoran_i2c_getsda (void *data)
  613. {
  614. struct zoran *zr = (struct zoran *) data;
  615. return (btread(ZR36057_I2CBR) >> 1) & 1;
  616. }
  617. static int
  618. zoran_i2c_getscl (void *data)
  619. {
  620. struct zoran *zr = (struct zoran *) data;
  621. return btread(ZR36057_I2CBR) & 1;
  622. }
  623. static void
  624. zoran_i2c_setsda (void *data,
  625. int state)
  626. {
  627. struct zoran *zr = (struct zoran *) data;
  628. if (state)
  629. zr->i2cbr |= 2;
  630. else
  631. zr->i2cbr &= ~2;
  632. btwrite(zr->i2cbr, ZR36057_I2CBR);
  633. }
  634. static void
  635. zoran_i2c_setscl (void *data,
  636. int state)
  637. {
  638. struct zoran *zr = (struct zoran *) data;
  639. if (state)
  640. zr->i2cbr |= 1;
  641. else
  642. zr->i2cbr &= ~1;
  643. btwrite(zr->i2cbr, ZR36057_I2CBR);
  644. }
  645. static int
  646. zoran_i2c_client_register (struct i2c_client *client)
  647. {
  648. struct zoran *zr = (struct zoran *) i2c_get_adapdata(client->adapter);
  649. int res = 0;
  650. dprintk(2,
  651. KERN_DEBUG "%s: i2c_client_register() - driver id = %d\n",
  652. ZR_DEVNAME(zr), client->driver->id);
  653. mutex_lock(&zr->resource_lock);
  654. if (zr->user > 0) {
  655. /* we're already busy, so we keep a reference to
  656. * them... Could do a lot of stuff here, but this
  657. * is easiest. (Did I ever mention I'm a lazy ass?)
  658. */
  659. res = -EBUSY;
  660. goto clientreg_unlock_and_return;
  661. }
  662. if (client->driver->id == zr->card.i2c_decoder)
  663. zr->decoder = client;
  664. else if (client->driver->id == zr->card.i2c_encoder)
  665. zr->encoder = client;
  666. else {
  667. res = -ENODEV;
  668. goto clientreg_unlock_and_return;
  669. }
  670. clientreg_unlock_and_return:
  671. mutex_unlock(&zr->resource_lock);
  672. return res;
  673. }
  674. static int
  675. zoran_i2c_client_unregister (struct i2c_client *client)
  676. {
  677. struct zoran *zr = (struct zoran *) i2c_get_adapdata(client->adapter);
  678. int res = 0;
  679. dprintk(2, KERN_DEBUG "%s: i2c_client_unregister()\n", ZR_DEVNAME(zr));
  680. mutex_lock(&zr->resource_lock);
  681. if (zr->user > 0) {
  682. res = -EBUSY;
  683. goto clientunreg_unlock_and_return;
  684. }
  685. /* try to locate it */
  686. if (client == zr->encoder) {
  687. zr->encoder = NULL;
  688. } else if (client == zr->decoder) {
  689. zr->decoder = NULL;
  690. snprintf(ZR_DEVNAME(zr), sizeof(ZR_DEVNAME(zr)), "MJPEG[%d]", zr->id);
  691. }
  692. clientunreg_unlock_and_return:
  693. mutex_unlock(&zr->resource_lock);
  694. return res;
  695. }
  696. static const struct i2c_algo_bit_data zoran_i2c_bit_data_template = {
  697. .setsda = zoran_i2c_setsda,
  698. .setscl = zoran_i2c_setscl,
  699. .getsda = zoran_i2c_getsda,
  700. .getscl = zoran_i2c_getscl,
  701. .udelay = 10,
  702. .timeout = 100,
  703. };
  704. static int
  705. zoran_register_i2c (struct zoran *zr)
  706. {
  707. memcpy(&zr->i2c_algo, &zoran_i2c_bit_data_template,
  708. sizeof(struct i2c_algo_bit_data));
  709. zr->i2c_algo.data = zr;
  710. zr->i2c_adapter.class = I2C_CLASS_TV_ANALOG;
  711. zr->i2c_adapter.id = I2C_HW_B_ZR36067;
  712. zr->i2c_adapter.client_register = zoran_i2c_client_register;
  713. zr->i2c_adapter.client_unregister = zoran_i2c_client_unregister;
  714. strlcpy(zr->i2c_adapter.name, ZR_DEVNAME(zr),
  715. sizeof(zr->i2c_adapter.name));
  716. i2c_set_adapdata(&zr->i2c_adapter, zr);
  717. zr->i2c_adapter.algo_data = &zr->i2c_algo;
  718. zr->i2c_adapter.dev.parent = &zr->pci_dev->dev;
  719. return i2c_bit_add_bus(&zr->i2c_adapter);
  720. }
  721. static void
  722. zoran_unregister_i2c (struct zoran *zr)
  723. {
  724. i2c_del_adapter(&zr->i2c_adapter);
  725. }
  726. /* Check a zoran_params struct for correctness, insert default params */
  727. int
  728. zoran_check_jpg_settings (struct zoran *zr,
  729. struct zoran_jpg_settings *settings)
  730. {
  731. int err = 0, err0 = 0;
  732. dprintk(4,
  733. KERN_DEBUG
  734. "%s: check_jpg_settings() - dec: %d, Hdcm: %d, Vdcm: %d, Tdcm: %d\n",
  735. ZR_DEVNAME(zr), settings->decimation, settings->HorDcm,
  736. settings->VerDcm, settings->TmpDcm);
  737. dprintk(4,
  738. KERN_DEBUG
  739. "%s: check_jpg_settings() - x: %d, y: %d, w: %d, y: %d\n",
  740. ZR_DEVNAME(zr), settings->img_x, settings->img_y,
  741. settings->img_width, settings->img_height);
  742. /* Check decimation, set default values for decimation = 1, 2, 4 */
  743. switch (settings->decimation) {
  744. case 1:
  745. settings->HorDcm = 1;
  746. settings->VerDcm = 1;
  747. settings->TmpDcm = 1;
  748. settings->field_per_buff = 2;
  749. settings->img_x = 0;
  750. settings->img_y = 0;
  751. settings->img_width = BUZ_MAX_WIDTH;
  752. settings->img_height = BUZ_MAX_HEIGHT / 2;
  753. break;
  754. case 2:
  755. settings->HorDcm = 2;
  756. settings->VerDcm = 1;
  757. settings->TmpDcm = 2;
  758. settings->field_per_buff = 1;
  759. settings->img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
  760. settings->img_y = 0;
  761. settings->img_width =
  762. (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
  763. settings->img_height = BUZ_MAX_HEIGHT / 2;
  764. break;
  765. case 4:
  766. if (zr->card.type == DC10_new) {
  767. dprintk(1,
  768. KERN_DEBUG
  769. "%s: check_jpg_settings() - HDec by 4 is not supported on the DC10\n",
  770. ZR_DEVNAME(zr));
  771. err0++;
  772. break;
  773. }
  774. settings->HorDcm = 4;
  775. settings->VerDcm = 2;
  776. settings->TmpDcm = 2;
  777. settings->field_per_buff = 1;
  778. settings->img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
  779. settings->img_y = 0;
  780. settings->img_width =
  781. (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
  782. settings->img_height = BUZ_MAX_HEIGHT / 2;
  783. break;
  784. case 0:
  785. /* We have to check the data the user has set */
  786. if (settings->HorDcm != 1 && settings->HorDcm != 2 &&
  787. (zr->card.type == DC10_new || settings->HorDcm != 4))
  788. err0++;
  789. if (settings->VerDcm != 1 && settings->VerDcm != 2)
  790. err0++;
  791. if (settings->TmpDcm != 1 && settings->TmpDcm != 2)
  792. err0++;
  793. if (settings->field_per_buff != 1 &&
  794. settings->field_per_buff != 2)
  795. err0++;
  796. if (settings->img_x < 0)
  797. err0++;
  798. if (settings->img_y < 0)
  799. err0++;
  800. if (settings->img_width < 0)
  801. err0++;
  802. if (settings->img_height < 0)
  803. err0++;
  804. if (settings->img_x + settings->img_width > BUZ_MAX_WIDTH)
  805. err0++;
  806. if (settings->img_y + settings->img_height >
  807. BUZ_MAX_HEIGHT / 2)
  808. err0++;
  809. if (settings->HorDcm && settings->VerDcm) {
  810. if (settings->img_width %
  811. (16 * settings->HorDcm) != 0)
  812. err0++;
  813. if (settings->img_height %
  814. (8 * settings->VerDcm) != 0)
  815. err0++;
  816. }
  817. if (err0) {
  818. dprintk(1,
  819. KERN_ERR
  820. "%s: check_jpg_settings() - error in params for decimation = 0\n",
  821. ZR_DEVNAME(zr));
  822. err++;
  823. }
  824. break;
  825. default:
  826. dprintk(1,
  827. KERN_ERR
  828. "%s: check_jpg_settings() - decimation = %d, must be 0, 1, 2 or 4\n",
  829. ZR_DEVNAME(zr), settings->decimation);
  830. err++;
  831. break;
  832. }
  833. if (settings->jpg_comp.quality > 100)
  834. settings->jpg_comp.quality = 100;
  835. if (settings->jpg_comp.quality < 5)
  836. settings->jpg_comp.quality = 5;
  837. if (settings->jpg_comp.APPn < 0)
  838. settings->jpg_comp.APPn = 0;
  839. if (settings->jpg_comp.APPn > 15)
  840. settings->jpg_comp.APPn = 15;
  841. if (settings->jpg_comp.APP_len < 0)
  842. settings->jpg_comp.APP_len = 0;
  843. if (settings->jpg_comp.APP_len > 60)
  844. settings->jpg_comp.APP_len = 60;
  845. if (settings->jpg_comp.COM_len < 0)
  846. settings->jpg_comp.COM_len = 0;
  847. if (settings->jpg_comp.COM_len > 60)
  848. settings->jpg_comp.COM_len = 60;
  849. if (err)
  850. return -EINVAL;
  851. return 0;
  852. }
  853. void
  854. zoran_open_init_params (struct zoran *zr)
  855. {
  856. int i;
  857. /* User must explicitly set a window */
  858. zr->overlay_settings.is_set = 0;
  859. zr->overlay_mask = NULL;
  860. zr->overlay_active = ZORAN_FREE;
  861. zr->v4l_memgrab_active = 0;
  862. zr->v4l_overlay_active = 0;
  863. zr->v4l_grab_frame = NO_GRAB_ACTIVE;
  864. zr->v4l_grab_seq = 0;
  865. zr->v4l_settings.width = 192;
  866. zr->v4l_settings.height = 144;
  867. zr->v4l_settings.format = &zoran_formats[7]; /* YUY2 - YUV-4:2:2 packed */
  868. zr->v4l_settings.bytesperline =
  869. zr->v4l_settings.width *
  870. ((zr->v4l_settings.format->depth + 7) / 8);
  871. /* DMA ring stuff for V4L */
  872. zr->v4l_pend_tail = 0;
  873. zr->v4l_pend_head = 0;
  874. zr->v4l_sync_tail = 0;
  875. zr->v4l_buffers.active = ZORAN_FREE;
  876. for (i = 0; i < VIDEO_MAX_FRAME; i++) {
  877. zr->v4l_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
  878. }
  879. zr->v4l_buffers.allocated = 0;
  880. for (i = 0; i < BUZ_MAX_FRAME; i++) {
  881. zr->jpg_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
  882. }
  883. zr->jpg_buffers.active = ZORAN_FREE;
  884. zr->jpg_buffers.allocated = 0;
  885. /* Set necessary params and call zoran_check_jpg_settings to set the defaults */
  886. zr->jpg_settings.decimation = 1;
  887. zr->jpg_settings.jpg_comp.quality = 50; /* default compression factor 8 */
  888. if (zr->card.type != BUZ)
  889. zr->jpg_settings.odd_even = 1;
  890. else
  891. zr->jpg_settings.odd_even = 0;
  892. zr->jpg_settings.jpg_comp.APPn = 0;
  893. zr->jpg_settings.jpg_comp.APP_len = 0; /* No APPn marker */
  894. memset(zr->jpg_settings.jpg_comp.APP_data, 0,
  895. sizeof(zr->jpg_settings.jpg_comp.APP_data));
  896. zr->jpg_settings.jpg_comp.COM_len = 0; /* No COM marker */
  897. memset(zr->jpg_settings.jpg_comp.COM_data, 0,
  898. sizeof(zr->jpg_settings.jpg_comp.COM_data));
  899. zr->jpg_settings.jpg_comp.jpeg_markers =
  900. JPEG_MARKER_DHT | JPEG_MARKER_DQT;
  901. i = zoran_check_jpg_settings(zr, &zr->jpg_settings);
  902. if (i)
  903. dprintk(1,
  904. KERN_ERR
  905. "%s: zoran_open_init_params() internal error\n",
  906. ZR_DEVNAME(zr));
  907. clear_interrupt_counters(zr);
  908. zr->testing = 0;
  909. }
  910. static void __devinit
  911. test_interrupts (struct zoran *zr)
  912. {
  913. DEFINE_WAIT(wait);
  914. int timeout, icr;
  915. clear_interrupt_counters(zr);
  916. zr->testing = 1;
  917. icr = btread(ZR36057_ICR);
  918. btwrite(0x78000000 | ZR36057_ICR_IntPinEn, ZR36057_ICR);
  919. prepare_to_wait(&zr->test_q, &wait, TASK_INTERRUPTIBLE);
  920. timeout = schedule_timeout(HZ);
  921. finish_wait(&zr->test_q, &wait);
  922. btwrite(0, ZR36057_ICR);
  923. btwrite(0x78000000, ZR36057_ISR);
  924. zr->testing = 0;
  925. dprintk(5, KERN_INFO "%s: Testing interrupts...\n", ZR_DEVNAME(zr));
  926. if (timeout) {
  927. dprintk(1, ": time spent: %d\n", 1 * HZ - timeout);
  928. }
  929. if (zr36067_debug > 1)
  930. print_interrupts(zr);
  931. btwrite(icr, ZR36057_ICR);
  932. }
  933. static int __devinit
  934. zr36057_init (struct zoran *zr)
  935. {
  936. int j, err;
  937. int two = 2;
  938. int zero = 0;
  939. dprintk(1,
  940. KERN_INFO
  941. "%s: zr36057_init() - initializing card[%d], zr=%p\n",
  942. ZR_DEVNAME(zr), zr->id, zr);
  943. /* default setup of all parameters which will persist between opens */
  944. zr->user = 0;
  945. init_waitqueue_head(&zr->v4l_capq);
  946. init_waitqueue_head(&zr->jpg_capq);
  947. init_waitqueue_head(&zr->test_q);
  948. zr->jpg_buffers.allocated = 0;
  949. zr->v4l_buffers.allocated = 0;
  950. zr->buffer.base = (void *) vidmem;
  951. zr->buffer.width = 0;
  952. zr->buffer.height = 0;
  953. zr->buffer.depth = 0;
  954. zr->buffer.bytesperline = 0;
  955. /* Avoid nonsense settings from user for default input/norm */
  956. if (default_norm < VIDEO_MODE_PAL &&
  957. default_norm > VIDEO_MODE_SECAM)
  958. default_norm = VIDEO_MODE_PAL;
  959. zr->norm = default_norm;
  960. if (!(zr->timing = zr->card.tvn[zr->norm])) {
  961. dprintk(1,
  962. KERN_WARNING
  963. "%s: zr36057_init() - default TV standard not supported by hardware. PAL will be used.\n",
  964. ZR_DEVNAME(zr));
  965. zr->norm = VIDEO_MODE_PAL;
  966. zr->timing = zr->card.tvn[zr->norm];
  967. }
  968. if (default_input > zr->card.inputs-1) {
  969. dprintk(1,
  970. KERN_WARNING
  971. "%s: default_input value %d out of range (0-%d)\n",
  972. ZR_DEVNAME(zr), default_input, zr->card.inputs-1);
  973. default_input = 0;
  974. }
  975. zr->input = default_input;
  976. /* Should the following be reset at every open ? */
  977. zr->hue = 32768;
  978. zr->contrast = 32768;
  979. zr->saturation = 32768;
  980. zr->brightness = 32768;
  981. /* default setup (will be repeated at every open) */
  982. zoran_open_init_params(zr);
  983. /* allocate memory *before* doing anything to the hardware
  984. * in case allocation fails */
  985. zr->stat_com = kzalloc(BUZ_NUM_STAT_COM * 4, GFP_KERNEL);
  986. zr->video_dev = kmalloc(sizeof(struct video_device), GFP_KERNEL);
  987. if (!zr->stat_com || !zr->video_dev) {
  988. dprintk(1,
  989. KERN_ERR
  990. "%s: zr36057_init() - kmalloc (STAT_COM) failed\n",
  991. ZR_DEVNAME(zr));
  992. err = -ENOMEM;
  993. goto exit_free;
  994. }
  995. for (j = 0; j < BUZ_NUM_STAT_COM; j++) {
  996. zr->stat_com[j] = cpu_to_le32(1); /* mark as unavailable to zr36057 */
  997. }
  998. /*
  999. * Now add the template and register the device unit.
  1000. */
  1001. memcpy(zr->video_dev, &zoran_template, sizeof(zoran_template));
  1002. strcpy(zr->video_dev->name, ZR_DEVNAME(zr));
  1003. err = video_register_device(zr->video_dev, VFL_TYPE_GRABBER, video_nr[zr->id]);
  1004. if (err < 0)
  1005. goto exit_free;
  1006. video_set_drvdata(zr->video_dev, zr);
  1007. zoran_init_hardware(zr);
  1008. if (zr36067_debug > 2)
  1009. detect_guest_activity(zr);
  1010. test_interrupts(zr);
  1011. if (!pass_through) {
  1012. decoder_command(zr, DECODER_ENABLE_OUTPUT, &zero);
  1013. encoder_command(zr, ENCODER_SET_INPUT, &two);
  1014. }
  1015. zr->zoran_proc = NULL;
  1016. zr->initialized = 1;
  1017. return 0;
  1018. exit_free:
  1019. kfree(zr->stat_com);
  1020. kfree(zr->video_dev);
  1021. return err;
  1022. }
  1023. static void __devexit zoran_remove(struct pci_dev *pdev)
  1024. {
  1025. struct zoran *zr = pci_get_drvdata(pdev);
  1026. if (!zr->initialized)
  1027. goto exit_free;
  1028. /* unregister videocodec bus */
  1029. if (zr->codec) {
  1030. struct videocodec_master *master = zr->codec->master_data;
  1031. videocodec_detach(zr->codec);
  1032. kfree(master);
  1033. }
  1034. if (zr->vfe) {
  1035. struct videocodec_master *master = zr->vfe->master_data;
  1036. videocodec_detach(zr->vfe);
  1037. kfree(master);
  1038. }
  1039. /* unregister i2c bus */
  1040. zoran_unregister_i2c(zr);
  1041. /* disable PCI bus-mastering */
  1042. zoran_set_pci_master(zr, 0);
  1043. /* put chip into reset */
  1044. btwrite(0, ZR36057_SPGPPCR);
  1045. free_irq(zr->pci_dev->irq, zr);
  1046. /* unmap and free memory */
  1047. kfree(zr->stat_com);
  1048. zoran_proc_cleanup(zr);
  1049. iounmap(zr->zr36057_mem);
  1050. pci_disable_device(zr->pci_dev);
  1051. video_unregister_device(zr->video_dev);
  1052. exit_free:
  1053. pci_set_drvdata(pdev, NULL);
  1054. kfree(zr);
  1055. }
  1056. void
  1057. zoran_vdev_release (struct video_device *vdev)
  1058. {
  1059. kfree(vdev);
  1060. }
  1061. static struct videocodec_master * __devinit
  1062. zoran_setup_videocodec (struct zoran *zr,
  1063. int type)
  1064. {
  1065. struct videocodec_master *m = NULL;
  1066. m = kmalloc(sizeof(struct videocodec_master), GFP_KERNEL);
  1067. if (!m) {
  1068. dprintk(1,
  1069. KERN_ERR
  1070. "%s: zoran_setup_videocodec() - no memory\n",
  1071. ZR_DEVNAME(zr));
  1072. return m;
  1073. }
  1074. /* magic and type are unused for master struct. Makes sense only at
  1075. codec structs.
  1076. In the past, .type were initialized to the old V4L1 .hardware
  1077. value, as VID_HARDWARE_ZR36067
  1078. */
  1079. m->magic = 0L;
  1080. m->type = 0;
  1081. m->flags = CODEC_FLAG_ENCODER | CODEC_FLAG_DECODER;
  1082. strncpy(m->name, ZR_DEVNAME(zr), sizeof(m->name));
  1083. m->data = zr;
  1084. switch (type)
  1085. {
  1086. case CODEC_TYPE_ZR36060:
  1087. m->readreg = zr36060_read;
  1088. m->writereg = zr36060_write;
  1089. m->flags |= CODEC_FLAG_JPEG | CODEC_FLAG_VFE;
  1090. break;
  1091. case CODEC_TYPE_ZR36050:
  1092. m->readreg = zr36050_read;
  1093. m->writereg = zr36050_write;
  1094. m->flags |= CODEC_FLAG_JPEG;
  1095. break;
  1096. case CODEC_TYPE_ZR36016:
  1097. m->readreg = zr36016_read;
  1098. m->writereg = zr36016_write;
  1099. m->flags |= CODEC_FLAG_VFE;
  1100. break;
  1101. }
  1102. return m;
  1103. }
  1104. /*
  1105. * Scan for a Buz card (actually for the PCI controller ZR36057),
  1106. * request the irq and map the io memory
  1107. */
  1108. static int __devinit zoran_probe(struct pci_dev *pdev,
  1109. const struct pci_device_id *ent)
  1110. {
  1111. unsigned char latency, need_latency;
  1112. struct zoran *zr;
  1113. int result;
  1114. struct videocodec_master *master_vfe = NULL;
  1115. struct videocodec_master *master_codec = NULL;
  1116. int card_num;
  1117. char *i2c_enc_name, *i2c_dec_name, *codec_name, *vfe_name;
  1118. unsigned int nr;
  1119. nr = zoran_num++;
  1120. if (nr >= BUZ_MAX) {
  1121. dprintk(1,
  1122. KERN_ERR
  1123. "%s: driver limited to %d card(s) maximum\n",
  1124. ZORAN_NAME, BUZ_MAX);
  1125. return -ENOENT;
  1126. }
  1127. zr = kzalloc(sizeof(struct zoran), GFP_KERNEL);
  1128. if (!zr) {
  1129. dprintk(1,
  1130. KERN_ERR
  1131. "%s: find_zr36057() - kzalloc failed\n",
  1132. ZORAN_NAME);
  1133. return -ENOMEM;
  1134. }
  1135. zr->pci_dev = pdev;
  1136. zr->id = nr;
  1137. snprintf(ZR_DEVNAME(zr), sizeof(ZR_DEVNAME(zr)), "MJPEG[%u]", zr->id);
  1138. spin_lock_init(&zr->spinlock);
  1139. mutex_init(&zr->resource_lock);
  1140. if (pci_enable_device(pdev))
  1141. goto zr_free_mem;
  1142. pci_read_config_byte(zr->pci_dev, PCI_CLASS_REVISION, &zr->revision);
  1143. dprintk(1,
  1144. KERN_INFO
  1145. "%s: Zoran ZR360%c7 (rev %d), irq: %d, memory: 0x%08llx\n",
  1146. ZR_DEVNAME(zr), zr->revision < 2 ? '5' : '6', zr->revision,
  1147. zr->pci_dev->irq, (uint64_t)pci_resource_start(zr->pci_dev, 0));
  1148. if (zr->revision >= 2) {
  1149. dprintk(1,
  1150. KERN_INFO
  1151. "%s: Subsystem vendor=0x%04x id=0x%04x\n",
  1152. ZR_DEVNAME(zr), zr->pci_dev->subsystem_vendor,
  1153. zr->pci_dev->subsystem_device);
  1154. }
  1155. /* Use auto-detected card type? */
  1156. if (card[nr] == -1) {
  1157. if (zr->revision < 2) {
  1158. dprintk(1,
  1159. KERN_ERR
  1160. "%s: No card type specified, please use the card=X module parameter\n",
  1161. ZR_DEVNAME(zr));
  1162. dprintk(1,
  1163. KERN_ERR
  1164. "%s: It is not possible to auto-detect ZR36057 based cards\n",
  1165. ZR_DEVNAME(zr));
  1166. goto zr_free_mem;
  1167. }
  1168. card_num = ent->driver_data;
  1169. if (card_num >= NUM_CARDS) {
  1170. dprintk(1,
  1171. KERN_ERR
  1172. "%s: Unknown card, try specifying card=X module parameter\n",
  1173. ZR_DEVNAME(zr));
  1174. goto zr_free_mem;
  1175. }
  1176. dprintk(3,
  1177. KERN_DEBUG
  1178. "%s: %s() - card %s detected\n",
  1179. ZR_DEVNAME(zr), __func__, zoran_cards[card_num].name);
  1180. } else {
  1181. card_num = card[nr];
  1182. if (card_num >= NUM_CARDS || card_num < 0) {
  1183. dprintk(1,
  1184. KERN_ERR
  1185. "%s: User specified card type %d out of range (0 .. %d)\n",
  1186. ZR_DEVNAME(zr), card_num, NUM_CARDS - 1);
  1187. goto zr_free_mem;
  1188. }
  1189. }
  1190. /* even though we make this a non pointer and thus
  1191. * theoretically allow for making changes to this struct
  1192. * on a per-individual card basis at runtime, this is
  1193. * strongly discouraged. This structure is intended to
  1194. * keep general card information, no settings or anything */
  1195. zr->card = zoran_cards[card_num];
  1196. snprintf(ZR_DEVNAME(zr), sizeof(ZR_DEVNAME(zr)),
  1197. "%s[%u]", zr->card.name, zr->id);
  1198. zr->zr36057_mem = pci_ioremap_bar(zr->pci_dev, 0);
  1199. if (!zr->zr36057_mem) {
  1200. dprintk(1,
  1201. KERN_ERR
  1202. "%s: %s() - ioremap failed\n",
  1203. ZR_DEVNAME(zr), __func__);
  1204. goto zr_free_mem;
  1205. }
  1206. result = request_irq(zr->pci_dev->irq, zoran_irq,
  1207. IRQF_SHARED | IRQF_DISABLED, ZR_DEVNAME(zr), zr);
  1208. if (result < 0) {
  1209. if (result == -EINVAL) {
  1210. dprintk(1,
  1211. KERN_ERR
  1212. "%s: find_zr36057() - bad irq number or handler\n",
  1213. ZR_DEVNAME(zr));
  1214. } else if (result == -EBUSY) {
  1215. dprintk(1,
  1216. KERN_ERR
  1217. "%s: find_zr36057() - IRQ %d busy, change your PnP config in BIOS\n",
  1218. ZR_DEVNAME(zr), zr->pci_dev->irq);
  1219. } else {
  1220. dprintk(1,
  1221. KERN_ERR
  1222. "%s: find_zr36057() - can't assign irq, error code %d\n",
  1223. ZR_DEVNAME(zr), result);
  1224. }
  1225. goto zr_unmap;
  1226. }
  1227. /* set PCI latency timer */
  1228. pci_read_config_byte(zr->pci_dev, PCI_LATENCY_TIMER,
  1229. &latency);
  1230. need_latency = zr->revision > 1 ? 32 : 48;
  1231. if (latency != need_latency) {
  1232. dprintk(2,
  1233. KERN_INFO
  1234. "%s: Changing PCI latency from %d to %d\n",
  1235. ZR_DEVNAME(zr), latency, need_latency);
  1236. pci_write_config_byte(zr->pci_dev, PCI_LATENCY_TIMER,
  1237. need_latency);
  1238. }
  1239. zr36057_restart(zr);
  1240. /* i2c */
  1241. dprintk(2, KERN_INFO "%s: Initializing i2c bus...\n",
  1242. ZR_DEVNAME(zr));
  1243. /* i2c decoder */
  1244. if (decoder[zr->id] != -1) {
  1245. i2c_dec_name = i2cid_to_modulename(decoder[zr->id]);
  1246. zr->card.i2c_decoder = decoder[zr->id];
  1247. } else if (zr->card.i2c_decoder != 0) {
  1248. i2c_dec_name = i2cid_to_modulename(zr->card.i2c_decoder);
  1249. } else {
  1250. i2c_dec_name = NULL;
  1251. }
  1252. if (i2c_dec_name) {
  1253. result = request_module(i2c_dec_name);
  1254. if (result < 0) {
  1255. dprintk(1,
  1256. KERN_ERR
  1257. "%s: failed to load module %s: %d\n",
  1258. ZR_DEVNAME(zr), i2c_dec_name, result);
  1259. }
  1260. }
  1261. /* i2c encoder */
  1262. if (encoder[zr->id] != -1) {
  1263. i2c_enc_name = i2cid_to_modulename(encoder[zr->id]);
  1264. zr->card.i2c_encoder = encoder[zr->id];
  1265. } else if (zr->card.i2c_encoder != 0) {
  1266. i2c_enc_name = i2cid_to_modulename(zr->card.i2c_encoder);
  1267. } else {
  1268. i2c_enc_name = NULL;
  1269. }
  1270. if (i2c_enc_name) {
  1271. result = request_module(i2c_enc_name);
  1272. if (result < 0) {
  1273. dprintk(1,
  1274. KERN_ERR
  1275. "%s: failed to load module %s: %d\n",
  1276. ZR_DEVNAME(zr), i2c_enc_name, result);
  1277. }
  1278. }
  1279. if (zoran_register_i2c(zr) < 0) {
  1280. dprintk(1,
  1281. KERN_ERR
  1282. "%s: find_zr36057() - can't initialize i2c bus\n",
  1283. ZR_DEVNAME(zr));
  1284. goto zr_free_irq;
  1285. }
  1286. dprintk(2,
  1287. KERN_INFO "%s: Initializing videocodec bus...\n",
  1288. ZR_DEVNAME(zr));
  1289. if (zr->card.video_codec) {
  1290. codec_name = codecid_to_modulename(zr->card.video_codec);
  1291. if (codec_name) {
  1292. result = request_module(codec_name);
  1293. if (result) {
  1294. dprintk(1,
  1295. KERN_ERR
  1296. "%s: failed to load modules %s: %d\n",
  1297. ZR_DEVNAME(zr), codec_name, result);
  1298. }
  1299. }
  1300. }
  1301. if (zr->card.video_vfe) {
  1302. vfe_name = codecid_to_modulename(zr->card.video_vfe);
  1303. if (vfe_name) {
  1304. result = request_module(vfe_name);
  1305. if (result < 0) {
  1306. dprintk(1,
  1307. KERN_ERR
  1308. "%s: failed to load modules %s: %d\n",
  1309. ZR_DEVNAME(zr), vfe_name, result);
  1310. }
  1311. }
  1312. }
  1313. /* reset JPEG codec */
  1314. jpeg_codec_sleep(zr, 1);
  1315. jpeg_codec_reset(zr);
  1316. /* video bus enabled */
  1317. /* display codec revision */
  1318. if (zr->card.video_codec != 0) {
  1319. master_codec = zoran_setup_videocodec(zr, zr->card.video_codec);
  1320. if (!master_codec)
  1321. goto zr_unreg_i2c;
  1322. zr->codec = videocodec_attach(master_codec);
  1323. if (!zr->codec) {
  1324. dprintk(1,
  1325. KERN_ERR
  1326. "%s: find_zr36057() - no codec found\n",
  1327. ZR_DEVNAME(zr));
  1328. goto zr_free_codec;
  1329. }
  1330. if (zr->codec->type != zr->card.video_codec) {
  1331. dprintk(1,
  1332. KERN_ERR
  1333. "%s: find_zr36057() - wrong codec\n",
  1334. ZR_DEVNAME(zr));
  1335. goto zr_detach_codec;
  1336. }
  1337. }
  1338. if (zr->card.video_vfe != 0) {
  1339. master_vfe = zoran_setup_videocodec(zr, zr->card.video_vfe);
  1340. if (!master_vfe)
  1341. goto zr_detach_codec;
  1342. zr->vfe = videocodec_attach(master_vfe);
  1343. if (!zr->vfe) {
  1344. dprintk(1,
  1345. KERN_ERR
  1346. "%s: find_zr36057() - no VFE found\n",
  1347. ZR_DEVNAME(zr));
  1348. goto zr_free_vfe;
  1349. }
  1350. if (zr->vfe->type != zr->card.video_vfe) {
  1351. dprintk(1,
  1352. KERN_ERR
  1353. "%s: find_zr36057() = wrong VFE\n",
  1354. ZR_DEVNAME(zr));
  1355. goto zr_detach_vfe;
  1356. }
  1357. }
  1358. /* take care of Natoma chipset and a revision 1 zr36057 */
  1359. if ((pci_pci_problems & PCIPCI_NATOMA) && zr->revision <= 1) {
  1360. zr->jpg_buffers.need_contiguous = 1;
  1361. dprintk(1,
  1362. KERN_INFO
  1363. "%s: ZR36057/Natoma bug, max. buffer size is 128K\n",
  1364. ZR_DEVNAME(zr));
  1365. }
  1366. if (zr36057_init(zr) < 0)
  1367. goto zr_detach_vfe;
  1368. zoran_proc_init(zr);
  1369. pci_set_drvdata(pdev, zr);
  1370. return 0;
  1371. zr_detach_vfe:
  1372. videocodec_detach(zr->vfe);
  1373. zr_free_vfe:
  1374. kfree(master_vfe);
  1375. zr_detach_codec:
  1376. videocodec_detach(zr->codec);
  1377. zr_free_codec:
  1378. kfree(master_codec);
  1379. zr_unreg_i2c:
  1380. zoran_unregister_i2c(zr);
  1381. zr_free_irq:
  1382. btwrite(0, ZR36057_SPGPPCR);
  1383. free_irq(zr->pci_dev->irq, zr);
  1384. zr_unmap:
  1385. iounmap(zr->zr36057_mem);
  1386. zr_free_mem:
  1387. kfree(zr);
  1388. return -ENODEV;
  1389. }
  1390. static struct pci_driver zoran_driver = {
  1391. .name = "zr36067",
  1392. .id_table = zr36067_pci_tbl,
  1393. .probe = zoran_probe,
  1394. .remove = zoran_remove,
  1395. };
  1396. static int __init zoran_init(void)
  1397. {
  1398. int res;
  1399. printk(KERN_INFO "Zoran MJPEG board driver version %d.%d.%d\n",
  1400. MAJOR_VERSION, MINOR_VERSION, RELEASE_VERSION);
  1401. /* check the parameters we have been given, adjust if necessary */
  1402. if (v4l_nbufs < 2)
  1403. v4l_nbufs = 2;
  1404. if (v4l_nbufs > VIDEO_MAX_FRAME)
  1405. v4l_nbufs = VIDEO_MAX_FRAME;
  1406. /* The user specfies the in KB, we want them in byte
  1407. * (and page aligned) */
  1408. v4l_bufsize = PAGE_ALIGN(v4l_bufsize * 1024);
  1409. if (v4l_bufsize < 32768)
  1410. v4l_bufsize = 32768;
  1411. /* 2 MB is arbitrary but sufficient for the maximum possible images */
  1412. if (v4l_bufsize > 2048 * 1024)
  1413. v4l_bufsize = 2048 * 1024;
  1414. if (jpg_nbufs < 4)
  1415. jpg_nbufs = 4;
  1416. if (jpg_nbufs > BUZ_MAX_FRAME)
  1417. jpg_nbufs = BUZ_MAX_FRAME;
  1418. jpg_bufsize = PAGE_ALIGN(jpg_bufsize * 1024);
  1419. if (jpg_bufsize < 8192)
  1420. jpg_bufsize = 8192;
  1421. if (jpg_bufsize > (512 * 1024))
  1422. jpg_bufsize = 512 * 1024;
  1423. /* Use parameter for vidmem or try to find a video card */
  1424. if (vidmem) {
  1425. dprintk(1,
  1426. KERN_INFO
  1427. "%s: Using supplied video memory base address @ 0x%lx\n",
  1428. ZORAN_NAME, vidmem);
  1429. }
  1430. /* random nonsense */
  1431. dprintk(6, KERN_DEBUG "Jotti is een held!\n");
  1432. /* some mainboards might not do PCI-PCI data transfer well */
  1433. if (pci_pci_problems & (PCIPCI_FAIL|PCIAGP_FAIL|PCIPCI_ALIMAGIK)) {
  1434. dprintk(1,
  1435. KERN_WARNING
  1436. "%s: chipset does not support reliable PCI-PCI DMA\n",
  1437. ZORAN_NAME);
  1438. }
  1439. res = pci_register_driver(&zoran_driver);
  1440. if (res) {
  1441. dprintk(1,
  1442. KERN_ERR
  1443. "%s: Unable to register ZR36057 driver\n",
  1444. ZORAN_NAME);
  1445. return res;
  1446. }
  1447. return 0;
  1448. }
  1449. static void __exit zoran_exit(void)
  1450. {
  1451. pci_unregister_driver(&zoran_driver);
  1452. }
  1453. module_init(zoran_init);
  1454. module_exit(zoran_exit);