vfc_dev.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. /*
  2. * drivers/sbus/char/vfc_dev.c
  3. *
  4. * Driver for the Videopix Frame Grabber.
  5. *
  6. * In order to use the VFC you need to program the video controller
  7. * chip. This chip is the Phillips SAA9051. You need to call their
  8. * documentation ordering line to get the docs.
  9. *
  10. * There is very little documentation on the VFC itself. There is
  11. * some useful info that can be found in the manuals that come with
  12. * the card. I will hopefully write some better docs at a later date.
  13. *
  14. * Copyright (C) 1996 Manish Vachharajani (mvachhar@noc.rutgers.edu)
  15. * */
  16. #include <linux/module.h>
  17. #include <linux/kernel.h>
  18. #include <linux/string.h>
  19. #include <linux/slab.h>
  20. #include <linux/errno.h>
  21. #include <linux/fs.h>
  22. #include <linux/delay.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/mutex.h>
  25. #include <linux/mm.h>
  26. #include <asm/openprom.h>
  27. #include <asm/oplib.h>
  28. #include <asm/io.h>
  29. #include <asm/system.h>
  30. #include <asm/sbus.h>
  31. #include <asm/page.h>
  32. #include <asm/pgtable.h>
  33. #include <asm/uaccess.h>
  34. #define VFC_MAJOR (60)
  35. #if 0
  36. #define VFC_IOCTL_DEBUG
  37. #endif
  38. #include "vfc.h"
  39. #include <asm/vfc_ioctls.h>
  40. static const struct file_operations vfc_fops;
  41. struct vfc_dev **vfc_dev_lst;
  42. static char vfcstr[]="vfc";
  43. static unsigned char saa9051_init_array[VFC_SAA9051_NR] = {
  44. 0x00, 0x64, 0x72, 0x52,
  45. 0x36, 0x18, 0xff, 0x20,
  46. 0xfc, 0x77, 0xe3, 0x50,
  47. 0x3e
  48. };
  49. void vfc_lock_device(struct vfc_dev *dev)
  50. {
  51. mutex_lock(&dev->device_lock_mtx);
  52. }
  53. void vfc_unlock_device(struct vfc_dev *dev)
  54. {
  55. mutex_unlock(&dev->device_lock_mtx);
  56. }
  57. void vfc_captstat_reset(struct vfc_dev *dev)
  58. {
  59. dev->control_reg |= VFC_CONTROL_CAPTRESET;
  60. sbus_writel(dev->control_reg, &dev->regs->control);
  61. dev->control_reg &= ~VFC_CONTROL_CAPTRESET;
  62. sbus_writel(dev->control_reg, &dev->regs->control);
  63. dev->control_reg |= VFC_CONTROL_CAPTRESET;
  64. sbus_writel(dev->control_reg, &dev->regs->control);
  65. }
  66. void vfc_memptr_reset(struct vfc_dev *dev)
  67. {
  68. dev->control_reg |= VFC_CONTROL_MEMPTR;
  69. sbus_writel(dev->control_reg, &dev->regs->control);
  70. dev->control_reg &= ~VFC_CONTROL_MEMPTR;
  71. sbus_writel(dev->control_reg, &dev->regs->control);
  72. dev->control_reg |= VFC_CONTROL_MEMPTR;
  73. sbus_writel(dev->control_reg, &dev->regs->control);
  74. }
  75. int vfc_csr_init(struct vfc_dev *dev)
  76. {
  77. dev->control_reg = 0x80000000;
  78. sbus_writel(dev->control_reg, &dev->regs->control);
  79. udelay(200);
  80. dev->control_reg &= ~0x80000000;
  81. sbus_writel(dev->control_reg, &dev->regs->control);
  82. udelay(100);
  83. sbus_writel(0x0f000000, &dev->regs->i2c_magic2);
  84. vfc_memptr_reset(dev);
  85. dev->control_reg &= ~VFC_CONTROL_DIAGMODE;
  86. dev->control_reg &= ~VFC_CONTROL_CAPTURE;
  87. dev->control_reg |= 0x40000000;
  88. sbus_writel(dev->control_reg, &dev->regs->control);
  89. vfc_captstat_reset(dev);
  90. return 0;
  91. }
  92. int vfc_saa9051_init(struct vfc_dev *dev)
  93. {
  94. int i;
  95. for (i = 0; i < VFC_SAA9051_NR; i++)
  96. dev->saa9051_state_array[i] = saa9051_init_array[i];
  97. vfc_i2c_sendbuf(dev,VFC_SAA9051_ADDR,
  98. dev->saa9051_state_array, VFC_SAA9051_NR);
  99. return 0;
  100. }
  101. int init_vfc_hw(struct vfc_dev *dev)
  102. {
  103. vfc_lock_device(dev);
  104. vfc_csr_init(dev);
  105. vfc_pcf8584_init(dev);
  106. vfc_init_i2c_bus(dev); /* hopefully this doesn't undo the magic
  107. sun code above*/
  108. vfc_saa9051_init(dev);
  109. vfc_unlock_device(dev);
  110. return 0;
  111. }
  112. int init_vfc_devstruct(struct vfc_dev *dev, int instance)
  113. {
  114. dev->instance=instance;
  115. init_MUTEX(&dev->device_lock_sem);
  116. dev->control_reg=0;
  117. dev->busy=0;
  118. return 0;
  119. }
  120. int init_vfc_device(struct sbus_dev *sdev,struct vfc_dev *dev, int instance)
  121. {
  122. if(dev == NULL) {
  123. printk(KERN_ERR "VFC: Bogus pointer passed\n");
  124. return -ENOMEM;
  125. }
  126. printk("Initializing vfc%d\n",instance);
  127. dev->regs = NULL;
  128. dev->regs = (volatile struct vfc_regs __iomem *)
  129. sbus_ioremap(&sdev->resource[0], 0,
  130. sizeof(struct vfc_regs), vfcstr);
  131. dev->which_io = sdev->reg_addrs[0].which_io;
  132. dev->phys_regs = (struct vfc_regs *) sdev->reg_addrs[0].phys_addr;
  133. if (dev->regs == NULL)
  134. return -EIO;
  135. printk("vfc%d: registers mapped at phys_addr: 0x%lx\n virt_addr: 0x%lx\n",
  136. instance,(unsigned long)sdev->reg_addrs[0].phys_addr,(unsigned long)dev->regs);
  137. if (init_vfc_devstruct(dev, instance))
  138. return -EINVAL;
  139. if (init_vfc_hw(dev))
  140. return -EIO;
  141. return 0;
  142. }
  143. struct vfc_dev *vfc_get_dev_ptr(int instance)
  144. {
  145. return vfc_dev_lst[instance];
  146. }
  147. static DEFINE_SPINLOCK(vfc_dev_lock);
  148. static int vfc_open(struct inode *inode, struct file *file)
  149. {
  150. struct vfc_dev *dev;
  151. spin_lock(&vfc_dev_lock);
  152. dev = vfc_get_dev_ptr(iminor(inode));
  153. if (dev == NULL) {
  154. spin_unlock(&vfc_dev_lock);
  155. return -ENODEV;
  156. }
  157. if (dev->busy) {
  158. spin_unlock(&vfc_dev_lock);
  159. return -EBUSY;
  160. }
  161. dev->busy = 1;
  162. spin_unlock(&vfc_dev_lock);
  163. vfc_lock_device(dev);
  164. vfc_csr_init(dev);
  165. vfc_pcf8584_init(dev);
  166. vfc_init_i2c_bus(dev);
  167. vfc_saa9051_init(dev);
  168. vfc_memptr_reset(dev);
  169. vfc_captstat_reset(dev);
  170. vfc_unlock_device(dev);
  171. return 0;
  172. }
  173. static int vfc_release(struct inode *inode,struct file *file)
  174. {
  175. struct vfc_dev *dev;
  176. spin_lock(&vfc_dev_lock);
  177. dev = vfc_get_dev_ptr(iminor(inode));
  178. if (!dev || !dev->busy) {
  179. spin_unlock(&vfc_dev_lock);
  180. return -EINVAL;
  181. }
  182. dev->busy = 0;
  183. spin_unlock(&vfc_dev_lock);
  184. return 0;
  185. }
  186. static int vfc_debug(struct vfc_dev *dev, int cmd, void __user *argp)
  187. {
  188. struct vfc_debug_inout inout;
  189. unsigned char *buffer;
  190. if (!capable(CAP_SYS_ADMIN))
  191. return -EPERM;
  192. switch(cmd) {
  193. case VFC_I2C_SEND:
  194. if(copy_from_user(&inout, argp, sizeof(inout)))
  195. return -EFAULT;
  196. buffer = kmalloc(inout.len, GFP_KERNEL);
  197. if (buffer == NULL)
  198. return -ENOMEM;
  199. if(copy_from_user(buffer, inout.buffer, inout.len)) {
  200. kfree(buffer);
  201. return -EFAULT;
  202. }
  203. vfc_lock_device(dev);
  204. inout.ret=
  205. vfc_i2c_sendbuf(dev,inout.addr & 0xff,
  206. buffer,inout.len);
  207. if (copy_to_user(argp,&inout,sizeof(inout))) {
  208. vfc_unlock_device(dev);
  209. kfree(buffer);
  210. return -EFAULT;
  211. }
  212. vfc_unlock_device(dev);
  213. break;
  214. case VFC_I2C_RECV:
  215. if (copy_from_user(&inout, argp, sizeof(inout)))
  216. return -EFAULT;
  217. buffer = kzalloc(inout.len, GFP_KERNEL);
  218. if (buffer == NULL)
  219. return -ENOMEM;
  220. vfc_lock_device(dev);
  221. inout.ret=
  222. vfc_i2c_recvbuf(dev,inout.addr & 0xff
  223. ,buffer,inout.len);
  224. vfc_unlock_device(dev);
  225. if (copy_to_user(inout.buffer, buffer, inout.len)) {
  226. kfree(buffer);
  227. return -EFAULT;
  228. }
  229. if (copy_to_user(argp,&inout,sizeof(inout))) {
  230. kfree(buffer);
  231. return -EFAULT;
  232. }
  233. kfree(buffer);
  234. break;
  235. default:
  236. return -EINVAL;
  237. };
  238. return 0;
  239. }
  240. int vfc_capture_start(struct vfc_dev *dev)
  241. {
  242. vfc_captstat_reset(dev);
  243. dev->control_reg = sbus_readl(&dev->regs->control);
  244. if((dev->control_reg & VFC_STATUS_CAPTURE)) {
  245. printk(KERN_ERR "vfc%d: vfc capture status not reset\n",
  246. dev->instance);
  247. return -EIO;
  248. }
  249. vfc_lock_device(dev);
  250. dev->control_reg &= ~VFC_CONTROL_CAPTURE;
  251. sbus_writel(dev->control_reg, &dev->regs->control);
  252. dev->control_reg |= VFC_CONTROL_CAPTURE;
  253. sbus_writel(dev->control_reg, &dev->regs->control);
  254. dev->control_reg &= ~VFC_CONTROL_CAPTURE;
  255. sbus_writel(dev->control_reg, &dev->regs->control);
  256. vfc_unlock_device(dev);
  257. return 0;
  258. }
  259. int vfc_capture_poll(struct vfc_dev *dev)
  260. {
  261. int timeout = 1000;
  262. while (!timeout--) {
  263. if (sbus_readl(&dev->regs->control) & VFC_STATUS_CAPTURE)
  264. break;
  265. vfc_i2c_delay_no_busy(dev, 100);
  266. }
  267. if(!timeout) {
  268. printk(KERN_WARNING "vfc%d: capture timed out\n",
  269. dev->instance);
  270. return -ETIMEDOUT;
  271. }
  272. return 0;
  273. }
  274. static int vfc_set_control_ioctl(struct inode *inode, struct file *file,
  275. struct vfc_dev *dev, unsigned long arg)
  276. {
  277. int setcmd, ret = 0;
  278. if (copy_from_user(&setcmd,(void __user *)arg,sizeof(unsigned int)))
  279. return -EFAULT;
  280. VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCSCTRL) arg=0x%x\n",
  281. dev->instance,setcmd));
  282. switch(setcmd) {
  283. case MEMPRST:
  284. vfc_lock_device(dev);
  285. vfc_memptr_reset(dev);
  286. vfc_unlock_device(dev);
  287. ret=0;
  288. break;
  289. case CAPTRCMD:
  290. vfc_capture_start(dev);
  291. vfc_capture_poll(dev);
  292. break;
  293. case DIAGMODE:
  294. if(capable(CAP_SYS_ADMIN)) {
  295. vfc_lock_device(dev);
  296. dev->control_reg |= VFC_CONTROL_DIAGMODE;
  297. sbus_writel(dev->control_reg, &dev->regs->control);
  298. vfc_unlock_device(dev);
  299. ret = 0;
  300. } else {
  301. ret = -EPERM;
  302. }
  303. break;
  304. case NORMMODE:
  305. vfc_lock_device(dev);
  306. dev->control_reg &= ~VFC_CONTROL_DIAGMODE;
  307. sbus_writel(dev->control_reg, &dev->regs->control);
  308. vfc_unlock_device(dev);
  309. ret = 0;
  310. break;
  311. case CAPTRSTR:
  312. vfc_capture_start(dev);
  313. ret = 0;
  314. break;
  315. case CAPTRWAIT:
  316. vfc_capture_poll(dev);
  317. ret = 0;
  318. break;
  319. default:
  320. ret = -EINVAL;
  321. break;
  322. };
  323. return ret;
  324. }
  325. int vfc_port_change_ioctl(struct inode *inode, struct file *file,
  326. struct vfc_dev *dev, unsigned long arg)
  327. {
  328. int ret = 0;
  329. int cmd;
  330. if(copy_from_user(&cmd, (void __user *)arg, sizeof(unsigned int))) {
  331. VFC_IOCTL_DEBUG_PRINTK(("vfc%d: User passed bogus pointer to "
  332. "vfc_port_change_ioctl\n",
  333. dev->instance));
  334. return -EFAULT;
  335. }
  336. VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCPORTCHG) arg=0x%x\n",
  337. dev->instance, cmd));
  338. switch(cmd) {
  339. case 1:
  340. case 2:
  341. VFC_SAA9051_SA(dev,VFC_SAA9051_HSY_START) = 0x72;
  342. VFC_SAA9051_SA(dev,VFC_SAA9051_HSY_STOP) = 0x52;
  343. VFC_SAA9051_SA(dev,VFC_SAA9051_HC_START) = 0x36;
  344. VFC_SAA9051_SA(dev,VFC_SAA9051_HC_STOP) = 0x18;
  345. VFC_SAA9051_SA(dev,VFC_SAA9051_HORIZ_PEAK) = VFC_SAA9051_BP2;
  346. VFC_SAA9051_SA(dev,VFC_SAA9051_C3) = VFC_SAA9051_CT | VFC_SAA9051_SS3;
  347. VFC_SAA9051_SA(dev,VFC_SAA9051_SECAM_DELAY) = 0x3e;
  348. break;
  349. case 3:
  350. VFC_SAA9051_SA(dev,VFC_SAA9051_HSY_START) = 0x3a;
  351. VFC_SAA9051_SA(dev,VFC_SAA9051_HSY_STOP) = 0x17;
  352. VFC_SAA9051_SA(dev,VFC_SAA9051_HC_START) = 0xfa;
  353. VFC_SAA9051_SA(dev,VFC_SAA9051_HC_STOP) = 0xde;
  354. VFC_SAA9051_SA(dev,VFC_SAA9051_HORIZ_PEAK) =
  355. VFC_SAA9051_BY | VFC_SAA9051_PF | VFC_SAA9051_BP2;
  356. VFC_SAA9051_SA(dev,VFC_SAA9051_C3) = VFC_SAA9051_YC;
  357. VFC_SAA9051_SA(dev,VFC_SAA9051_SECAM_DELAY) = 0;
  358. VFC_SAA9051_SA(dev,VFC_SAA9051_C2) &=
  359. ~(VFC_SAA9051_SS0 | VFC_SAA9051_SS1);
  360. break;
  361. default:
  362. ret = -EINVAL;
  363. return ret;
  364. break;
  365. }
  366. switch(cmd) {
  367. case 1:
  368. VFC_SAA9051_SA(dev,VFC_SAA9051_C2) |=
  369. (VFC_SAA9051_SS0 | VFC_SAA9051_SS1);
  370. break;
  371. case 2:
  372. VFC_SAA9051_SA(dev,VFC_SAA9051_C2) &=
  373. ~(VFC_SAA9051_SS0 | VFC_SAA9051_SS1);
  374. VFC_SAA9051_SA(dev,VFC_SAA9051_C2) |= VFC_SAA9051_SS0;
  375. break;
  376. case 3:
  377. break;
  378. default:
  379. ret = -EINVAL;
  380. return ret;
  381. break;
  382. }
  383. VFC_SAA9051_SA(dev,VFC_SAA9051_C3) &= ~(VFC_SAA9051_SS2);
  384. ret=vfc_update_saa9051(dev);
  385. udelay(500);
  386. VFC_SAA9051_SA(dev,VFC_SAA9051_C3) |= (VFC_SAA9051_SS2);
  387. ret=vfc_update_saa9051(dev);
  388. return ret;
  389. }
  390. int vfc_set_video_ioctl(struct inode *inode, struct file *file,
  391. struct vfc_dev *dev, unsigned long arg)
  392. {
  393. int ret = 0;
  394. int cmd;
  395. if(copy_from_user(&cmd, (void __user *)arg, sizeof(unsigned int))) {
  396. VFC_IOCTL_DEBUG_PRINTK(("vfc%d: User passed bogus pointer to "
  397. "vfc_set_video_ioctl\n",
  398. dev->instance));
  399. return ret;
  400. }
  401. VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCSVID) arg=0x%x\n",
  402. dev->instance, cmd));
  403. switch(cmd) {
  404. case STD_NTSC:
  405. VFC_SAA9051_SA(dev,VFC_SAA9051_C1) &= ~VFC_SAA9051_ALT;
  406. VFC_SAA9051_SA(dev,VFC_SAA9051_C1) |= VFC_SAA9051_YPN |
  407. VFC_SAA9051_CCFR0 | VFC_SAA9051_CCFR1 | VFC_SAA9051_FS;
  408. ret = vfc_update_saa9051(dev);
  409. break;
  410. case STD_PAL:
  411. VFC_SAA9051_SA(dev,VFC_SAA9051_C1) &= ~(VFC_SAA9051_YPN |
  412. VFC_SAA9051_CCFR1 |
  413. VFC_SAA9051_CCFR0 |
  414. VFC_SAA9051_FS);
  415. VFC_SAA9051_SA(dev,VFC_SAA9051_C1) |= VFC_SAA9051_ALT;
  416. ret = vfc_update_saa9051(dev);
  417. break;
  418. case COLOR_ON:
  419. VFC_SAA9051_SA(dev,VFC_SAA9051_C1) |= VFC_SAA9051_CO;
  420. VFC_SAA9051_SA(dev,VFC_SAA9051_HORIZ_PEAK) &=
  421. ~(VFC_SAA9051_BY | VFC_SAA9051_PF);
  422. ret = vfc_update_saa9051(dev);
  423. break;
  424. case MONO:
  425. VFC_SAA9051_SA(dev,VFC_SAA9051_C1) &= ~(VFC_SAA9051_CO);
  426. VFC_SAA9051_SA(dev,VFC_SAA9051_HORIZ_PEAK) |=
  427. (VFC_SAA9051_BY | VFC_SAA9051_PF);
  428. ret = vfc_update_saa9051(dev);
  429. break;
  430. default:
  431. ret = -EINVAL;
  432. break;
  433. };
  434. return ret;
  435. }
  436. int vfc_get_video_ioctl(struct inode *inode, struct file *file,
  437. struct vfc_dev *dev, unsigned long arg)
  438. {
  439. int ret = 0;
  440. unsigned int status = NO_LOCK;
  441. unsigned char buf[1];
  442. if(vfc_i2c_recvbuf(dev, VFC_SAA9051_ADDR, buf, 1)) {
  443. printk(KERN_ERR "vfc%d: Unable to get status\n",
  444. dev->instance);
  445. return -EIO;
  446. }
  447. if(buf[0] & VFC_SAA9051_HLOCK) {
  448. status = NO_LOCK;
  449. } else if(buf[0] & VFC_SAA9051_FD) {
  450. if(buf[0] & VFC_SAA9051_CD)
  451. status = NTSC_COLOR;
  452. else
  453. status = NTSC_NOCOLOR;
  454. } else {
  455. if(buf[0] & VFC_SAA9051_CD)
  456. status = PAL_COLOR;
  457. else
  458. status = PAL_NOCOLOR;
  459. }
  460. VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCGVID) returning status 0x%x; "
  461. "buf[0]=%x\n", dev->instance, status, buf[0]));
  462. if (copy_to_user((void __user *)arg,&status,sizeof(unsigned int))) {
  463. VFC_IOCTL_DEBUG_PRINTK(("vfc%d: User passed bogus pointer to "
  464. "vfc_get_video_ioctl\n",
  465. dev->instance));
  466. return ret;
  467. }
  468. return ret;
  469. }
  470. static int vfc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  471. unsigned long arg)
  472. {
  473. int ret = 0;
  474. unsigned int tmp;
  475. struct vfc_dev *dev;
  476. void __user *argp = (void __user *)arg;
  477. dev = vfc_get_dev_ptr(iminor(inode));
  478. if(dev == NULL)
  479. return -ENODEV;
  480. switch(cmd & 0x0000ffff) {
  481. case VFCGCTRL:
  482. #if 0
  483. VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCGCTRL)\n", dev->instance));
  484. #endif
  485. tmp = sbus_readl(&dev->regs->control);
  486. if(copy_to_user(argp, &tmp, sizeof(unsigned int))) {
  487. ret = -EFAULT;
  488. break;
  489. }
  490. ret = 0;
  491. break;
  492. case VFCSCTRL:
  493. ret = vfc_set_control_ioctl(inode, file, dev, arg);
  494. break;
  495. case VFCGVID:
  496. ret = vfc_get_video_ioctl(inode, file, dev, arg);
  497. break;
  498. case VFCSVID:
  499. ret = vfc_set_video_ioctl(inode, file, dev, arg);
  500. break;
  501. case VFCHUE:
  502. VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCHUE)\n", dev->instance));
  503. if(copy_from_user(&tmp,argp,sizeof(unsigned int))) {
  504. VFC_IOCTL_DEBUG_PRINTK(("vfc%d: User passed bogus pointer "
  505. "to IOCTL(VFCHUE)", dev->instance));
  506. ret = -EFAULT;
  507. } else {
  508. VFC_SAA9051_SA(dev,VFC_SAA9051_HUE) = tmp;
  509. vfc_update_saa9051(dev);
  510. ret = 0;
  511. }
  512. break;
  513. case VFCPORTCHG:
  514. ret = vfc_port_change_ioctl(inode, file, dev, arg);
  515. break;
  516. case VFCRDINFO:
  517. ret = -EINVAL;
  518. VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCRDINFO)\n", dev->instance));
  519. break;
  520. default:
  521. ret = vfc_debug(vfc_get_dev_ptr(iminor(inode)), cmd, argp);
  522. break;
  523. };
  524. return ret;
  525. }
  526. static int vfc_mmap(struct file *file, struct vm_area_struct *vma)
  527. {
  528. unsigned int map_size, ret, map_offset;
  529. struct vfc_dev *dev;
  530. dev = vfc_get_dev_ptr(iminor(file->f_path.dentry->d_inode));
  531. if(dev == NULL)
  532. return -ENODEV;
  533. map_size = vma->vm_end - vma->vm_start;
  534. if(map_size > sizeof(struct vfc_regs))
  535. map_size = sizeof(struct vfc_regs);
  536. vma->vm_flags |=
  537. (VM_MAYREAD | VM_MAYWRITE | VM_MAYSHARE);
  538. map_offset = (unsigned int) (long)dev->phys_regs;
  539. ret = io_remap_pfn_range(vma, vma->vm_start,
  540. MK_IOSPACE_PFN(dev->which_io,
  541. map_offset >> PAGE_SHIFT),
  542. map_size, vma->vm_page_prot);
  543. if(ret)
  544. return -EAGAIN;
  545. return 0;
  546. }
  547. static const struct file_operations vfc_fops = {
  548. .owner = THIS_MODULE,
  549. .llseek = no_llseek,
  550. .ioctl = vfc_ioctl,
  551. .mmap = vfc_mmap,
  552. .open = vfc_open,
  553. .release = vfc_release,
  554. };
  555. static int vfc_probe(void)
  556. {
  557. struct sbus_bus *sbus;
  558. struct sbus_dev *sdev = NULL;
  559. int ret;
  560. int instance = 0, cards = 0;
  561. for_all_sbusdev(sdev, sbus) {
  562. if (strcmp(sdev->prom_name, "vfc") == 0) {
  563. cards++;
  564. continue;
  565. }
  566. }
  567. if (!cards)
  568. return -ENODEV;
  569. vfc_dev_lst = kcalloc(cards + 1, sizeof(struct vfc_dev*), GFP_KERNEL);
  570. if (vfc_dev_lst == NULL)
  571. return -ENOMEM;
  572. vfc_dev_lst[cards] = NULL;
  573. ret = register_chrdev(VFC_MAJOR, vfcstr, &vfc_fops);
  574. if(ret) {
  575. printk(KERN_ERR "Unable to get major number %d\n", VFC_MAJOR);
  576. kfree(vfc_dev_lst);
  577. return -EIO;
  578. }
  579. instance = 0;
  580. for_all_sbusdev(sdev, sbus) {
  581. if (strcmp(sdev->prom_name, "vfc") == 0) {
  582. vfc_dev_lst[instance]=(struct vfc_dev *)
  583. kmalloc(sizeof(struct vfc_dev), GFP_KERNEL);
  584. if (vfc_dev_lst[instance] == NULL)
  585. return -ENOMEM;
  586. ret = init_vfc_device(sdev,
  587. vfc_dev_lst[instance],
  588. instance);
  589. if(ret) {
  590. printk(KERN_ERR "Unable to initialize"
  591. " vfc%d device\n",
  592. instance);
  593. } else {
  594. }
  595. instance++;
  596. continue;
  597. }
  598. }
  599. return 0;
  600. }
  601. #ifdef MODULE
  602. int init_module(void)
  603. #else
  604. int vfc_init(void)
  605. #endif
  606. {
  607. return vfc_probe();
  608. }
  609. #ifdef MODULE
  610. static void deinit_vfc_device(struct vfc_dev *dev)
  611. {
  612. if(dev == NULL)
  613. return;
  614. sbus_iounmap(dev->regs, sizeof(struct vfc_regs));
  615. kfree(dev);
  616. }
  617. void cleanup_module(void)
  618. {
  619. struct vfc_dev **devp;
  620. unregister_chrdev(VFC_MAJOR,vfcstr);
  621. for (devp = vfc_dev_lst; *devp; devp++)
  622. deinit_vfc_device(*devp);
  623. kfree(vfc_dev_lst);
  624. return;
  625. }
  626. #endif
  627. MODULE_LICENSE("GPL");