zoran_card.c 39 KB

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