vfc_dev.c 16 KB

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