vfc_dev.c 17 KB

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