zoran_card.c 40 KB

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