vfc_dev.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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/smp_lock.h>
  23. #include <linux/delay.h>
  24. #include <linux/spinlock.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. down(&dev->device_lock_sem);
  52. }
  53. void vfc_unlock_device(struct vfc_dev *dev)
  54. {
  55. up(&dev->device_lock_sem);
  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. kfree(buffer);
  209. return -EFAULT;
  210. }
  211. vfc_unlock_device(dev);
  212. break;
  213. case VFC_I2C_RECV:
  214. if (copy_from_user(&inout, argp, sizeof(inout)))
  215. return -EFAULT;
  216. buffer = kzalloc(inout.len, GFP_KERNEL);
  217. if (buffer == NULL)
  218. return -ENOMEM;
  219. vfc_lock_device(dev);
  220. inout.ret=
  221. vfc_i2c_recvbuf(dev,inout.addr & 0xff
  222. ,buffer,inout.len);
  223. vfc_unlock_device(dev);
  224. if (copy_to_user(inout.buffer, buffer, inout.len)) {
  225. kfree(buffer);
  226. return -EFAULT;
  227. }
  228. if (copy_to_user(argp,&inout,sizeof(inout))) {
  229. kfree(buffer);
  230. return -EFAULT;
  231. }
  232. kfree(buffer);
  233. break;
  234. default:
  235. return -EINVAL;
  236. };
  237. return 0;
  238. }
  239. int vfc_capture_start(struct vfc_dev *dev)
  240. {
  241. vfc_captstat_reset(dev);
  242. dev->control_reg = sbus_readl(&dev->regs->control);
  243. if((dev->control_reg & VFC_STATUS_CAPTURE)) {
  244. printk(KERN_ERR "vfc%d: vfc capture status not reset\n",
  245. dev->instance);
  246. return -EIO;
  247. }
  248. vfc_lock_device(dev);
  249. dev->control_reg &= ~VFC_CONTROL_CAPTURE;
  250. sbus_writel(dev->control_reg, &dev->regs->control);
  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. vfc_unlock_device(dev);
  256. return 0;
  257. }
  258. int vfc_capture_poll(struct vfc_dev *dev)
  259. {
  260. int timeout = 1000;
  261. while (!timeout--) {
  262. if (sbus_readl(&dev->regs->control) & VFC_STATUS_CAPTURE)
  263. break;
  264. vfc_i2c_delay_no_busy(dev, 100);
  265. }
  266. if(!timeout) {
  267. printk(KERN_WARNING "vfc%d: capture timed out\n",
  268. dev->instance);
  269. return -ETIMEDOUT;
  270. }
  271. return 0;
  272. }
  273. static int vfc_set_control_ioctl(struct inode *inode, struct file *file,
  274. struct vfc_dev *dev, unsigned long arg)
  275. {
  276. int setcmd, ret = 0;
  277. if (copy_from_user(&setcmd,(void __user *)arg,sizeof(unsigned int)))
  278. return -EFAULT;
  279. VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCSCTRL) arg=0x%x\n",
  280. dev->instance,setcmd));
  281. switch(setcmd) {
  282. case MEMPRST:
  283. vfc_lock_device(dev);
  284. vfc_memptr_reset(dev);
  285. vfc_unlock_device(dev);
  286. ret=0;
  287. break;
  288. case CAPTRCMD:
  289. vfc_capture_start(dev);
  290. vfc_capture_poll(dev);
  291. break;
  292. case DIAGMODE:
  293. if(capable(CAP_SYS_ADMIN)) {
  294. vfc_lock_device(dev);
  295. dev->control_reg |= VFC_CONTROL_DIAGMODE;
  296. sbus_writel(dev->control_reg, &dev->regs->control);
  297. vfc_unlock_device(dev);
  298. ret = 0;
  299. } else {
  300. ret = -EPERM;
  301. }
  302. break;
  303. case NORMMODE:
  304. vfc_lock_device(dev);
  305. dev->control_reg &= ~VFC_CONTROL_DIAGMODE;
  306. sbus_writel(dev->control_reg, &dev->regs->control);
  307. vfc_unlock_device(dev);
  308. ret = 0;
  309. break;
  310. case CAPTRSTR:
  311. vfc_capture_start(dev);
  312. ret = 0;
  313. break;
  314. case CAPTRWAIT:
  315. vfc_capture_poll(dev);
  316. ret = 0;
  317. break;
  318. default:
  319. ret = -EINVAL;
  320. break;
  321. };
  322. return ret;
  323. }
  324. int vfc_port_change_ioctl(struct inode *inode, struct file *file,
  325. struct vfc_dev *dev, unsigned long arg)
  326. {
  327. int ret = 0;
  328. int cmd;
  329. if(copy_from_user(&cmd, (void __user *)arg, sizeof(unsigned int))) {
  330. VFC_IOCTL_DEBUG_PRINTK(("vfc%d: User passed bogus pointer to "
  331. "vfc_port_change_ioctl\n",
  332. dev->instance));
  333. return -EFAULT;
  334. }
  335. VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCPORTCHG) arg=0x%x\n",
  336. dev->instance, cmd));
  337. switch(cmd) {
  338. case 1:
  339. case 2:
  340. VFC_SAA9051_SA(dev,VFC_SAA9051_HSY_START) = 0x72;
  341. VFC_SAA9051_SA(dev,VFC_SAA9051_HSY_STOP) = 0x52;
  342. VFC_SAA9051_SA(dev,VFC_SAA9051_HC_START) = 0x36;
  343. VFC_SAA9051_SA(dev,VFC_SAA9051_HC_STOP) = 0x18;
  344. VFC_SAA9051_SA(dev,VFC_SAA9051_HORIZ_PEAK) = VFC_SAA9051_BP2;
  345. VFC_SAA9051_SA(dev,VFC_SAA9051_C3) = VFC_SAA9051_CT | VFC_SAA9051_SS3;
  346. VFC_SAA9051_SA(dev,VFC_SAA9051_SECAM_DELAY) = 0x3e;
  347. break;
  348. case 3:
  349. VFC_SAA9051_SA(dev,VFC_SAA9051_HSY_START) = 0x3a;
  350. VFC_SAA9051_SA(dev,VFC_SAA9051_HSY_STOP) = 0x17;
  351. VFC_SAA9051_SA(dev,VFC_SAA9051_HC_START) = 0xfa;
  352. VFC_SAA9051_SA(dev,VFC_SAA9051_HC_STOP) = 0xde;
  353. VFC_SAA9051_SA(dev,VFC_SAA9051_HORIZ_PEAK) =
  354. VFC_SAA9051_BY | VFC_SAA9051_PF | VFC_SAA9051_BP2;
  355. VFC_SAA9051_SA(dev,VFC_SAA9051_C3) = VFC_SAA9051_YC;
  356. VFC_SAA9051_SA(dev,VFC_SAA9051_SECAM_DELAY) = 0;
  357. VFC_SAA9051_SA(dev,VFC_SAA9051_C2) &=
  358. ~(VFC_SAA9051_SS0 | VFC_SAA9051_SS1);
  359. break;
  360. default:
  361. ret = -EINVAL;
  362. return ret;
  363. break;
  364. }
  365. switch(cmd) {
  366. case 1:
  367. VFC_SAA9051_SA(dev,VFC_SAA9051_C2) |=
  368. (VFC_SAA9051_SS0 | VFC_SAA9051_SS1);
  369. break;
  370. case 2:
  371. VFC_SAA9051_SA(dev,VFC_SAA9051_C2) &=
  372. ~(VFC_SAA9051_SS0 | VFC_SAA9051_SS1);
  373. VFC_SAA9051_SA(dev,VFC_SAA9051_C2) |= VFC_SAA9051_SS0;
  374. break;
  375. case 3:
  376. break;
  377. default:
  378. ret = -EINVAL;
  379. return ret;
  380. break;
  381. }
  382. VFC_SAA9051_SA(dev,VFC_SAA9051_C3) &= ~(VFC_SAA9051_SS2);
  383. ret=vfc_update_saa9051(dev);
  384. udelay(500);
  385. VFC_SAA9051_SA(dev,VFC_SAA9051_C3) |= (VFC_SAA9051_SS2);
  386. ret=vfc_update_saa9051(dev);
  387. return ret;
  388. }
  389. int vfc_set_video_ioctl(struct inode *inode, struct file *file,
  390. struct vfc_dev *dev, unsigned long arg)
  391. {
  392. int ret = 0;
  393. int cmd;
  394. if(copy_from_user(&cmd, (void __user *)arg, sizeof(unsigned int))) {
  395. VFC_IOCTL_DEBUG_PRINTK(("vfc%d: User passed bogus pointer to "
  396. "vfc_set_video_ioctl\n",
  397. dev->instance));
  398. return ret;
  399. }
  400. VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCSVID) arg=0x%x\n",
  401. dev->instance, cmd));
  402. switch(cmd) {
  403. case STD_NTSC:
  404. VFC_SAA9051_SA(dev,VFC_SAA9051_C1) &= ~VFC_SAA9051_ALT;
  405. VFC_SAA9051_SA(dev,VFC_SAA9051_C1) |= VFC_SAA9051_YPN |
  406. VFC_SAA9051_CCFR0 | VFC_SAA9051_CCFR1 | VFC_SAA9051_FS;
  407. ret = vfc_update_saa9051(dev);
  408. break;
  409. case STD_PAL:
  410. VFC_SAA9051_SA(dev,VFC_SAA9051_C1) &= ~(VFC_SAA9051_YPN |
  411. VFC_SAA9051_CCFR1 |
  412. VFC_SAA9051_CCFR0 |
  413. VFC_SAA9051_FS);
  414. VFC_SAA9051_SA(dev,VFC_SAA9051_C1) |= VFC_SAA9051_ALT;
  415. ret = vfc_update_saa9051(dev);
  416. break;
  417. case COLOR_ON:
  418. VFC_SAA9051_SA(dev,VFC_SAA9051_C1) |= VFC_SAA9051_CO;
  419. VFC_SAA9051_SA(dev,VFC_SAA9051_HORIZ_PEAK) &=
  420. ~(VFC_SAA9051_BY | VFC_SAA9051_PF);
  421. ret = vfc_update_saa9051(dev);
  422. break;
  423. case MONO:
  424. VFC_SAA9051_SA(dev,VFC_SAA9051_C1) &= ~(VFC_SAA9051_CO);
  425. VFC_SAA9051_SA(dev,VFC_SAA9051_HORIZ_PEAK) |=
  426. (VFC_SAA9051_BY | VFC_SAA9051_PF);
  427. ret = vfc_update_saa9051(dev);
  428. break;
  429. default:
  430. ret = -EINVAL;
  431. break;
  432. };
  433. return ret;
  434. }
  435. int vfc_get_video_ioctl(struct inode *inode, struct file *file,
  436. struct vfc_dev *dev, unsigned long arg)
  437. {
  438. int ret = 0;
  439. unsigned int status = NO_LOCK;
  440. unsigned char buf[1];
  441. if(vfc_i2c_recvbuf(dev, VFC_SAA9051_ADDR, buf, 1)) {
  442. printk(KERN_ERR "vfc%d: Unable to get status\n",
  443. dev->instance);
  444. return -EIO;
  445. }
  446. if(buf[0] & VFC_SAA9051_HLOCK) {
  447. status = NO_LOCK;
  448. } else if(buf[0] & VFC_SAA9051_FD) {
  449. if(buf[0] & VFC_SAA9051_CD)
  450. status = NTSC_COLOR;
  451. else
  452. status = NTSC_NOCOLOR;
  453. } else {
  454. if(buf[0] & VFC_SAA9051_CD)
  455. status = PAL_COLOR;
  456. else
  457. status = PAL_NOCOLOR;
  458. }
  459. VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCGVID) returning status 0x%x; "
  460. "buf[0]=%x\n", dev->instance, status, buf[0]));
  461. if (copy_to_user((void __user *)arg,&status,sizeof(unsigned int))) {
  462. VFC_IOCTL_DEBUG_PRINTK(("vfc%d: User passed bogus pointer to "
  463. "vfc_get_video_ioctl\n",
  464. dev->instance));
  465. return ret;
  466. }
  467. return ret;
  468. }
  469. static int vfc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  470. unsigned long arg)
  471. {
  472. int ret = 0;
  473. unsigned int tmp;
  474. struct vfc_dev *dev;
  475. void __user *argp = (void __user *)arg;
  476. dev = vfc_get_dev_ptr(iminor(inode));
  477. if(dev == NULL)
  478. return -ENODEV;
  479. switch(cmd & 0x0000ffff) {
  480. case VFCGCTRL:
  481. #if 0
  482. VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCGCTRL)\n", dev->instance));
  483. #endif
  484. tmp = sbus_readl(&dev->regs->control);
  485. if(copy_to_user(argp, &tmp, sizeof(unsigned int))) {
  486. ret = -EFAULT;
  487. break;
  488. }
  489. ret = 0;
  490. break;
  491. case VFCSCTRL:
  492. ret = vfc_set_control_ioctl(inode, file, dev, arg);
  493. break;
  494. case VFCGVID:
  495. ret = vfc_get_video_ioctl(inode, file, dev, arg);
  496. break;
  497. case VFCSVID:
  498. ret = vfc_set_video_ioctl(inode, file, dev, arg);
  499. break;
  500. case VFCHUE:
  501. VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCHUE)\n", dev->instance));
  502. if(copy_from_user(&tmp,argp,sizeof(unsigned int))) {
  503. VFC_IOCTL_DEBUG_PRINTK(("vfc%d: User passed bogus pointer "
  504. "to IOCTL(VFCHUE)", dev->instance));
  505. ret = -EFAULT;
  506. } else {
  507. VFC_SAA9051_SA(dev,VFC_SAA9051_HUE) = tmp;
  508. vfc_update_saa9051(dev);
  509. ret = 0;
  510. }
  511. break;
  512. case VFCPORTCHG:
  513. ret = vfc_port_change_ioctl(inode, file, dev, arg);
  514. break;
  515. case VFCRDINFO:
  516. ret = -EINVAL;
  517. VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCRDINFO)\n", dev->instance));
  518. break;
  519. default:
  520. ret = vfc_debug(vfc_get_dev_ptr(iminor(inode)), cmd, argp);
  521. break;
  522. };
  523. return ret;
  524. }
  525. static int vfc_mmap(struct file *file, struct vm_area_struct *vma)
  526. {
  527. unsigned int map_size, ret, map_offset;
  528. struct vfc_dev *dev;
  529. dev = vfc_get_dev_ptr(iminor(file->f_path.dentry->d_inode));
  530. if(dev == NULL)
  531. return -ENODEV;
  532. map_size = vma->vm_end - vma->vm_start;
  533. if(map_size > sizeof(struct vfc_regs))
  534. map_size = sizeof(struct vfc_regs);
  535. vma->vm_flags |=
  536. (VM_MAYREAD | VM_MAYWRITE | VM_MAYSHARE);
  537. map_offset = (unsigned int) (long)dev->phys_regs;
  538. ret = io_remap_pfn_range(vma, vma->vm_start,
  539. MK_IOSPACE_PFN(dev->which_io,
  540. map_offset >> PAGE_SHIFT),
  541. map_size, vma->vm_page_prot);
  542. if(ret)
  543. return -EAGAIN;
  544. return 0;
  545. }
  546. static const struct file_operations vfc_fops = {
  547. .owner = THIS_MODULE,
  548. .llseek = no_llseek,
  549. .ioctl = vfc_ioctl,
  550. .mmap = vfc_mmap,
  551. .open = vfc_open,
  552. .release = vfc_release,
  553. };
  554. static int vfc_probe(void)
  555. {
  556. struct sbus_bus *sbus;
  557. struct sbus_dev *sdev = NULL;
  558. int ret;
  559. int instance = 0, cards = 0;
  560. for_all_sbusdev(sdev, sbus) {
  561. if (strcmp(sdev->prom_name, "vfc") == 0) {
  562. cards++;
  563. continue;
  564. }
  565. }
  566. if (!cards)
  567. return -ENODEV;
  568. vfc_dev_lst = kmalloc(sizeof(struct vfc_dev *) *
  569. (cards+1),
  570. GFP_KERNEL);
  571. if (vfc_dev_lst == NULL)
  572. return -ENOMEM;
  573. memset(vfc_dev_lst, 0, sizeof(struct vfc_dev *) * (cards + 1));
  574. vfc_dev_lst[cards] = NULL;
  575. ret = register_chrdev(VFC_MAJOR, vfcstr, &vfc_fops);
  576. if(ret) {
  577. printk(KERN_ERR "Unable to get major number %d\n", VFC_MAJOR);
  578. kfree(vfc_dev_lst);
  579. return -EIO;
  580. }
  581. instance = 0;
  582. for_all_sbusdev(sdev, sbus) {
  583. if (strcmp(sdev->prom_name, "vfc") == 0) {
  584. vfc_dev_lst[instance]=(struct vfc_dev *)
  585. kmalloc(sizeof(struct vfc_dev), GFP_KERNEL);
  586. if (vfc_dev_lst[instance] == NULL)
  587. return -ENOMEM;
  588. ret = init_vfc_device(sdev,
  589. vfc_dev_lst[instance],
  590. instance);
  591. if(ret) {
  592. printk(KERN_ERR "Unable to initialize"
  593. " vfc%d device\n",
  594. instance);
  595. } else {
  596. }
  597. instance++;
  598. continue;
  599. }
  600. }
  601. return 0;
  602. }
  603. #ifdef MODULE
  604. int init_module(void)
  605. #else
  606. int vfc_init(void)
  607. #endif
  608. {
  609. return vfc_probe();
  610. }
  611. #ifdef MODULE
  612. static void deinit_vfc_device(struct vfc_dev *dev)
  613. {
  614. if(dev == NULL)
  615. return;
  616. sbus_iounmap(dev->regs, sizeof(struct vfc_regs));
  617. kfree(dev);
  618. }
  619. void cleanup_module(void)
  620. {
  621. struct vfc_dev **devp;
  622. unregister_chrdev(VFC_MAJOR,vfcstr);
  623. for (devp = vfc_dev_lst; *devp; devp++)
  624. deinit_vfc_device(*devp);
  625. kfree(vfc_dev_lst);
  626. return;
  627. }
  628. #endif
  629. MODULE_LICENSE("GPL");