zoran_card.c 39 KB

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