vfc_dev.c 17 KB

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