vfc_dev.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  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/mm.h>
  25. #include <asm/openprom.h>
  26. #include <asm/oplib.h>
  27. #include <asm/io.h>
  28. #include <asm/system.h>
  29. #include <asm/sbus.h>
  30. #include <asm/page.h>
  31. #include <asm/pgtable.h>
  32. #include <asm/uaccess.h>
  33. #define VFC_MAJOR (60)
  34. #if 0
  35. #define VFC_IOCTL_DEBUG
  36. #endif
  37. #include "vfc.h"
  38. #include <asm/vfc_ioctls.h>
  39. static const struct file_operations vfc_fops;
  40. struct vfc_dev **vfc_dev_lst;
  41. static char vfcstr[]="vfc";
  42. static unsigned char saa9051_init_array[VFC_SAA9051_NR] = {
  43. 0x00, 0x64, 0x72, 0x52,
  44. 0x36, 0x18, 0xff, 0x20,
  45. 0xfc, 0x77, 0xe3, 0x50,
  46. 0x3e
  47. };
  48. void vfc_lock_device(struct vfc_dev *dev)
  49. {
  50. down(&dev->device_lock_sem);
  51. }
  52. void vfc_unlock_device(struct vfc_dev *dev)
  53. {
  54. up(&dev->device_lock_sem);
  55. }
  56. void vfc_captstat_reset(struct vfc_dev *dev)
  57. {
  58. dev->control_reg |= VFC_CONTROL_CAPTRESET;
  59. sbus_writel(dev->control_reg, &dev->regs->control);
  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. }
  65. void vfc_memptr_reset(struct vfc_dev *dev)
  66. {
  67. dev->control_reg |= VFC_CONTROL_MEMPTR;
  68. sbus_writel(dev->control_reg, &dev->regs->control);
  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. }
  74. int vfc_csr_init(struct vfc_dev *dev)
  75. {
  76. dev->control_reg = 0x80000000;
  77. sbus_writel(dev->control_reg, &dev->regs->control);
  78. udelay(200);
  79. dev->control_reg &= ~0x80000000;
  80. sbus_writel(dev->control_reg, &dev->regs->control);
  81. udelay(100);
  82. sbus_writel(0x0f000000, &dev->regs->i2c_magic2);
  83. vfc_memptr_reset(dev);
  84. dev->control_reg &= ~VFC_CONTROL_DIAGMODE;
  85. dev->control_reg &= ~VFC_CONTROL_CAPTURE;
  86. dev->control_reg |= 0x40000000;
  87. sbus_writel(dev->control_reg, &dev->regs->control);
  88. vfc_captstat_reset(dev);
  89. return 0;
  90. }
  91. int vfc_saa9051_init(struct vfc_dev *dev)
  92. {
  93. int i;
  94. for (i = 0; i < VFC_SAA9051_NR; i++)
  95. dev->saa9051_state_array[i] = saa9051_init_array[i];
  96. vfc_i2c_sendbuf(dev,VFC_SAA9051_ADDR,
  97. dev->saa9051_state_array, VFC_SAA9051_NR);
  98. return 0;
  99. }
  100. int init_vfc_hw(struct vfc_dev *dev)
  101. {
  102. vfc_lock_device(dev);
  103. vfc_csr_init(dev);
  104. vfc_pcf8584_init(dev);
  105. vfc_init_i2c_bus(dev); /* hopefully this doesn't undo the magic
  106. sun code above*/
  107. vfc_saa9051_init(dev);
  108. vfc_unlock_device(dev);
  109. return 0;
  110. }
  111. int init_vfc_devstruct(struct vfc_dev *dev, int instance)
  112. {
  113. dev->instance=instance;
  114. init_MUTEX(&dev->device_lock_sem);
  115. dev->control_reg=0;
  116. dev->busy=0;
  117. return 0;
  118. }
  119. int init_vfc_device(struct sbus_dev *sdev,struct vfc_dev *dev, int instance)
  120. {
  121. if(dev == NULL) {
  122. printk(KERN_ERR "VFC: Bogus pointer passed\n");
  123. return -ENOMEM;
  124. }
  125. printk("Initializing vfc%d\n",instance);
  126. dev->regs = NULL;
  127. dev->regs = (volatile struct vfc_regs __iomem *)
  128. sbus_ioremap(&sdev->resource[0], 0,
  129. sizeof(struct vfc_regs), vfcstr);
  130. dev->which_io = sdev->reg_addrs[0].which_io;
  131. dev->phys_regs = (struct vfc_regs *) sdev->reg_addrs[0].phys_addr;
  132. if (dev->regs == NULL)
  133. return -EIO;
  134. printk("vfc%d: registers mapped at phys_addr: 0x%lx\n virt_addr: 0x%lx\n",
  135. instance,(unsigned long)sdev->reg_addrs[0].phys_addr,(unsigned long)dev->regs);
  136. if (init_vfc_devstruct(dev, instance))
  137. return -EINVAL;
  138. if (init_vfc_hw(dev))
  139. return -EIO;
  140. return 0;
  141. }
  142. struct vfc_dev *vfc_get_dev_ptr(int instance)
  143. {
  144. return vfc_dev_lst[instance];
  145. }
  146. static DEFINE_SPINLOCK(vfc_dev_lock);
  147. static int vfc_open(struct inode *inode, struct file *file)
  148. {
  149. struct vfc_dev *dev;
  150. spin_lock(&vfc_dev_lock);
  151. dev = vfc_get_dev_ptr(iminor(inode));
  152. if (dev == NULL) {
  153. spin_unlock(&vfc_dev_lock);
  154. return -ENODEV;
  155. }
  156. if (dev->busy) {
  157. spin_unlock(&vfc_dev_lock);
  158. return -EBUSY;
  159. }
  160. dev->busy = 1;
  161. spin_unlock(&vfc_dev_lock);
  162. vfc_lock_device(dev);
  163. vfc_csr_init(dev);
  164. vfc_pcf8584_init(dev);
  165. vfc_init_i2c_bus(dev);
  166. vfc_saa9051_init(dev);
  167. vfc_memptr_reset(dev);
  168. vfc_captstat_reset(dev);
  169. vfc_unlock_device(dev);
  170. return 0;
  171. }
  172. static int vfc_release(struct inode *inode,struct file *file)
  173. {
  174. struct vfc_dev *dev;
  175. spin_lock(&vfc_dev_lock);
  176. dev = vfc_get_dev_ptr(iminor(inode));
  177. if (!dev || !dev->busy) {
  178. spin_unlock(&vfc_dev_lock);
  179. return -EINVAL;
  180. }
  181. dev->busy = 0;
  182. spin_unlock(&vfc_dev_lock);
  183. return 0;
  184. }
  185. static int vfc_debug(struct vfc_dev *dev, int cmd, void __user *argp)
  186. {
  187. struct vfc_debug_inout inout;
  188. unsigned char *buffer;
  189. if (!capable(CAP_SYS_ADMIN))
  190. return -EPERM;
  191. switch(cmd) {
  192. case VFC_I2C_SEND:
  193. if(copy_from_user(&inout, argp, sizeof(inout)))
  194. return -EFAULT;
  195. buffer = kmalloc(inout.len, GFP_KERNEL);
  196. if (buffer == NULL)
  197. return -ENOMEM;
  198. if(copy_from_user(buffer, inout.buffer, inout.len)) {
  199. kfree(buffer);
  200. return -EFAULT;
  201. }
  202. vfc_lock_device(dev);
  203. inout.ret=
  204. vfc_i2c_sendbuf(dev,inout.addr & 0xff,
  205. buffer,inout.len);
  206. if (copy_to_user(argp,&inout,sizeof(inout))) {
  207. vfc_unlock_device(dev);
  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 = kcalloc(cards + 1, sizeof(struct vfc_dev*), GFP_KERNEL);
  569. if (vfc_dev_lst == NULL)
  570. return -ENOMEM;
  571. vfc_dev_lst[cards] = NULL;
  572. ret = register_chrdev(VFC_MAJOR, vfcstr, &vfc_fops);
  573. if(ret) {
  574. printk(KERN_ERR "Unable to get major number %d\n", VFC_MAJOR);
  575. kfree(vfc_dev_lst);
  576. return -EIO;
  577. }
  578. instance = 0;
  579. for_all_sbusdev(sdev, sbus) {
  580. if (strcmp(sdev->prom_name, "vfc") == 0) {
  581. vfc_dev_lst[instance]=(struct vfc_dev *)
  582. kmalloc(sizeof(struct vfc_dev), GFP_KERNEL);
  583. if (vfc_dev_lst[instance] == NULL)
  584. return -ENOMEM;
  585. ret = init_vfc_device(sdev,
  586. vfc_dev_lst[instance],
  587. instance);
  588. if(ret) {
  589. printk(KERN_ERR "Unable to initialize"
  590. " vfc%d device\n",
  591. instance);
  592. } else {
  593. }
  594. instance++;
  595. continue;
  596. }
  597. }
  598. return 0;
  599. }
  600. #ifdef MODULE
  601. int init_module(void)
  602. #else
  603. int vfc_init(void)
  604. #endif
  605. {
  606. return vfc_probe();
  607. }
  608. #ifdef MODULE
  609. static void deinit_vfc_device(struct vfc_dev *dev)
  610. {
  611. if(dev == NULL)
  612. return;
  613. sbus_iounmap(dev->regs, sizeof(struct vfc_regs));
  614. kfree(dev);
  615. }
  616. void cleanup_module(void)
  617. {
  618. struct vfc_dev **devp;
  619. unregister_chrdev(VFC_MAJOR,vfcstr);
  620. for (devp = vfc_dev_lst; *devp; devp++)
  621. deinit_vfc_device(*devp);
  622. kfree(vfc_dev_lst);
  623. return;
  624. }
  625. #endif
  626. MODULE_LICENSE("GPL");