vfc_dev.c 17 KB

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