vfc_dev.c 16 KB

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