zoran_card.c 41 KB

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