ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / sbus / char / vfc_dev.c
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
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/string.h>
20 #include <linux/slab.h>
21 #include <linux/errno.h>
22 #include <linux/sched.h>
23 #include <linux/fs.h>
24 #include <linux/smp_lock.h>
25 #include <linux/delay.h>
26 #include <linux/spinlock.h>
27 #include <linux/mm.h>
28
29 #include <asm/openprom.h>
30 #include <asm/oplib.h>
31 #include <asm/io.h>
32 #include <asm/system.h>
33 #include <asm/sbus.h>
34 #include <asm/page.h>
35 #include <asm/pgtable.h>
36 #include <asm/uaccess.h>
37
38 #define VFC_MAJOR (60)
39
40 #if 0
41 #define VFC_IOCTL_DEBUG
42 #endif
43
44 #include "vfc.h"
45 #include <asm/vfc_ioctls.h>
46
47 static struct file_operations vfc_fops;
48 struct vfc_dev **vfc_dev_lst;
49 static char vfcstr[]="vfc";
50 static unsigned char saa9051_init_array[VFC_SAA9051_NR] = {
51         0x00, 0x64, 0x72, 0x52,
52         0x36, 0x18, 0xff, 0x20,
53         0xfc, 0x77, 0xe3, 0x50,
54         0x3e
55 };
56
57 void vfc_lock_device(struct vfc_dev *dev)
58 {
59         down(&dev->device_lock_sem);
60 }
61
62 void vfc_unlock_device(struct vfc_dev *dev)
63 {
64         up(&dev->device_lock_sem);
65 }
66
67
68 void vfc_captstat_reset(struct vfc_dev *dev) 
69 {
70         dev->control_reg |= VFC_CONTROL_CAPTRESET;
71         sbus_writel(dev->control_reg, &dev->regs->control);
72         dev->control_reg &= ~VFC_CONTROL_CAPTRESET;
73         sbus_writel(dev->control_reg, &dev->regs->control);
74         dev->control_reg |= VFC_CONTROL_CAPTRESET;
75         sbus_writel(dev->control_reg, &dev->regs->control);
76 }
77
78 void vfc_memptr_reset(struct vfc_dev *dev) 
79 {
80         dev->control_reg |= VFC_CONTROL_MEMPTR;
81         sbus_writel(dev->control_reg, &dev->regs->control);
82         dev->control_reg &= ~VFC_CONTROL_MEMPTR;
83         sbus_writel(dev->control_reg, &dev->regs->control);
84         dev->control_reg |= VFC_CONTROL_MEMPTR; 
85         sbus_writel(dev->control_reg, &dev->regs->control);
86 }
87
88 int vfc_csr_init(struct vfc_dev *dev)
89 {
90         dev->control_reg = 0x80000000;
91         sbus_writel(dev->control_reg, &dev->regs->control);
92         udelay(200); 
93         dev->control_reg &= ~0x80000000;
94         sbus_writel(dev->control_reg, &dev->regs->control);
95         udelay(100); 
96         sbus_writel(0x0f000000, &dev->regs->i2c_magic2);
97
98         vfc_memptr_reset(dev);
99
100         dev->control_reg &= ~VFC_CONTROL_DIAGMODE;
101         dev->control_reg &= ~VFC_CONTROL_CAPTURE;
102         dev->control_reg |= 0x40000000;
103         sbus_writel(dev->control_reg, &dev->regs->control);
104
105         vfc_captstat_reset(dev);
106
107         return 0;
108 }
109
110 int vfc_saa9051_init(struct vfc_dev *dev)
111 {
112         int i;
113
114         for (i = 0; i < VFC_SAA9051_NR; i++)
115                 dev->saa9051_state_array[i] = saa9051_init_array[i];
116
117         vfc_i2c_sendbuf(dev,VFC_SAA9051_ADDR,
118                         dev->saa9051_state_array, VFC_SAA9051_NR);
119         return 0;
120 }
121
122 int init_vfc_hw(struct vfc_dev *dev) 
123 {
124         vfc_lock_device(dev);
125         vfc_csr_init(dev);
126
127         vfc_pcf8584_init(dev);
128         vfc_init_i2c_bus(dev); /* hopefully this doesn't undo the magic
129                                   sun code above*/
130         vfc_saa9051_init(dev);
131         vfc_unlock_device(dev);
132         return 0; 
133 }
134
135 int init_vfc_devstruct(struct vfc_dev *dev, int instance) 
136 {
137         dev->instance=instance;
138         init_MUTEX(&dev->device_lock_sem);
139         dev->control_reg=0;
140         init_waitqueue_head(&dev->poll_wait);
141         dev->busy=0;
142         return 0;
143 }
144
145 int init_vfc_device(struct sbus_dev *sdev,struct vfc_dev *dev, int instance)
146 {
147         if(dev == NULL) {
148                 printk(KERN_ERR "VFC: Bogus pointer passed\n");
149                 return -ENOMEM;
150         }
151         printk("Initializing vfc%d\n",instance);
152         dev->regs = NULL;
153         dev->regs = (volatile struct vfc_regs *)
154                 sbus_ioremap(&sdev->resource[0], 0,
155                              sizeof(struct vfc_regs), vfcstr);
156         dev->which_io = sdev->reg_addrs[0].which_io;
157         dev->phys_regs = (struct vfc_regs *) sdev->reg_addrs[0].phys_addr;
158         if (dev->regs == NULL)
159                 return -EIO;
160
161         printk("vfc%d: registers mapped at phys_addr: 0x%lx\n    virt_addr: 0x%lx\n",
162                instance,(unsigned long)sdev->reg_addrs[0].phys_addr,(unsigned long)dev->regs);
163
164         if (init_vfc_devstruct(dev, instance))
165                 return -EINVAL;
166         if (init_vfc_hw(dev))
167                 return -EIO;
168
169         devfs_mk_cdev(MKDEV(VFC_MAJOR, instance),
170                         S_IFCHR | S_IRUSR | S_IWUSR,
171                         "vfc/%d", instance);
172         return 0;
173 }
174
175
176 struct vfc_dev *vfc_get_dev_ptr(int instance) 
177 {
178         return vfc_dev_lst[instance];
179 }
180
181 static spinlock_t vfc_dev_lock = SPIN_LOCK_UNLOCKED;
182
183 static int vfc_open(struct inode *inode, struct file *file) 
184 {
185         struct vfc_dev *dev;
186
187         spin_lock(&vfc_dev_lock);
188         dev = vfc_get_dev_ptr(iminor(inode));
189         if (dev == NULL) {
190                 spin_unlock(&vfc_dev_lock);
191                 return -ENODEV;
192         }
193         if (dev->busy) {
194                 spin_unlock(&vfc_dev_lock);
195                 return -EBUSY;
196         }
197
198         dev->busy = 1;
199         spin_unlock(&vfc_dev_lock);
200
201         vfc_lock_device(dev);
202         
203         vfc_csr_init(dev);
204         vfc_pcf8584_init(dev);
205         vfc_init_i2c_bus(dev);
206         vfc_saa9051_init(dev);
207         vfc_memptr_reset(dev);
208         vfc_captstat_reset(dev);
209         
210         vfc_unlock_device(dev);
211         return 0;
212 }
213
214 static int vfc_release(struct inode *inode,struct file *file) 
215 {
216         struct vfc_dev *dev;
217
218         spin_lock(&vfc_dev_lock);
219         dev = vfc_get_dev_ptr(iminor(inode));
220         if (!dev || !dev->busy) {
221                 spin_unlock(&vfc_dev_lock);
222                 return -EINVAL;
223         }
224         dev->busy = 0;
225         spin_unlock(&vfc_dev_lock);
226         return 0;
227 }
228
229 static int vfc_debug(struct vfc_dev *dev, int cmd, unsigned long arg) 
230 {
231         struct vfc_debug_inout inout;
232         unsigned char *buffer;
233
234         if (!capable(CAP_SYS_ADMIN))
235                 return -EPERM;
236
237         switch(cmd) {
238         case VFC_I2C_SEND:
239                 if(copy_from_user(&inout, (void *)arg, sizeof(inout)))
240                         return -EFAULT;
241
242                 buffer = kmalloc(inout.len*sizeof(char), GFP_KERNEL);
243                 if (buffer == NULL)
244                         return -ENOMEM;
245
246                 if(copy_from_user(buffer, inout.buffer, 
247                                   inout.len*sizeof(char))) {
248                         kfree(buffer);
249                         return -EFAULT;
250                 }
251                 
252
253                 vfc_lock_device(dev);
254                 inout.ret=
255                         vfc_i2c_sendbuf(dev,inout.addr & 0xff,
256                                         inout.buffer,inout.len);
257
258                 if (copy_to_user((void *)arg,&inout,sizeof(inout))) {
259                         kfree(buffer);
260                         return -EFAULT;
261                 }
262                 vfc_unlock_device(dev);
263
264                 break;
265         case VFC_I2C_RECV:
266                 if (copy_from_user(&inout, (void *)arg, sizeof(inout)))
267                         return -EFAULT;
268
269                 buffer = kmalloc(inout.len, GFP_KERNEL);
270                 if (buffer == NULL)
271                         return -ENOMEM;
272
273                 memset(buffer,0,inout.len*sizeof(char));
274                 vfc_lock_device(dev);
275                 inout.ret=
276                         vfc_i2c_recvbuf(dev,inout.addr & 0xff
277                                         ,buffer,inout.len);
278                 vfc_unlock_device(dev);
279                 
280                 if (copy_to_user(inout.buffer, buffer, inout.len)) {
281                         kfree(buffer);
282                         return -EFAULT;
283                 }
284                 if (copy_to_user((void *)arg,&inout,sizeof(inout))) {
285                         kfree(buffer);
286                         return -EFAULT;
287                 }
288                 kfree(buffer);
289                 break;
290         default:
291                 return -EINVAL;
292         };
293
294         return 0;
295 }
296
297 int vfc_capture_start(struct vfc_dev *dev) 
298 {
299         vfc_captstat_reset(dev);
300         dev->control_reg = sbus_readl(&dev->regs->control);
301         if((dev->control_reg & VFC_STATUS_CAPTURE)) {
302                 printk(KERN_ERR "vfc%d: vfc capture status not reset\n",
303                        dev->instance);
304                 return -EIO;
305         }
306
307         vfc_lock_device(dev);
308         dev->control_reg &= ~VFC_CONTROL_CAPTURE;
309         sbus_writel(dev->control_reg, &dev->regs->control);
310         dev->control_reg |= VFC_CONTROL_CAPTURE;
311         sbus_writel(dev->control_reg, &dev->regs->control);
312         dev->control_reg &= ~VFC_CONTROL_CAPTURE;
313         sbus_writel(dev->control_reg, &dev->regs->control);
314         vfc_unlock_device(dev);
315
316         return 0;
317 }
318
319 int vfc_capture_poll(struct vfc_dev *dev) 
320 {
321         int timeout = 1000;
322
323         while (!timeout--) {
324                 if (dev->regs->control & VFC_STATUS_CAPTURE)
325                         break;
326                 vfc_i2c_delay_no_busy(dev, 100);
327         }
328         if(!timeout) {
329                 printk(KERN_WARNING "vfc%d: capture timed out\n",
330                        dev->instance);
331                 return -ETIMEDOUT;
332         }
333         return 0;
334 }
335
336
337
338 static int vfc_set_control_ioctl(struct inode *inode, struct file *file, 
339                           struct vfc_dev *dev, unsigned long arg) 
340 {
341         int setcmd, ret = 0;
342
343         if (copy_from_user(&setcmd,(void *)arg,sizeof(unsigned int)))
344                 return -EFAULT;
345
346         VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCSCTRL) arg=0x%x\n",
347                                 dev->instance,setcmd));
348
349         switch(setcmd) {
350         case MEMPRST:
351                 vfc_lock_device(dev);
352                 vfc_memptr_reset(dev);
353                 vfc_unlock_device(dev);
354                 ret=0;
355                 break;
356         case CAPTRCMD:
357                 vfc_capture_start(dev);
358                 vfc_capture_poll(dev);
359                 break;
360         case DIAGMODE:
361                 if(capable(CAP_SYS_ADMIN)) {
362                         vfc_lock_device(dev);
363                         dev->control_reg |= VFC_CONTROL_DIAGMODE;
364                         sbus_writel(dev->control_reg, &dev->regs->control);
365                         vfc_unlock_device(dev);
366                         ret = 0;
367                 } else {
368                         ret = -EPERM; 
369                 }
370                 break;
371         case NORMMODE:
372                 vfc_lock_device(dev);
373                 dev->control_reg &= ~VFC_CONTROL_DIAGMODE;
374                 sbus_writel(dev->control_reg, &dev->regs->control);
375                 vfc_unlock_device(dev);
376                 ret = 0;
377                 break;
378         case CAPTRSTR:
379                 vfc_capture_start(dev);
380                 ret = 0;
381                 break;
382         case CAPTRWAIT:
383                 vfc_capture_poll(dev);
384                 ret = 0;
385                 break;
386         default:
387                 ret = -EINVAL;
388                 break;
389         };
390
391         return ret;
392 }
393
394
395 int vfc_port_change_ioctl(struct inode *inode, struct file *file, 
396                           struct vfc_dev *dev, unsigned long arg) 
397 {
398         int ret = 0;
399         int cmd;
400
401         if(copy_from_user(&cmd, (void *)arg, sizeof(unsigned int))) {
402                 VFC_IOCTL_DEBUG_PRINTK(("vfc%d: User passed bogus pointer to "
403                                         "vfc_port_change_ioctl\n",
404                                         dev->instance));
405                 return -EFAULT;
406         }
407         
408         VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCPORTCHG) arg=0x%x\n",
409                                 dev->instance, cmd));
410
411         switch(cmd) {
412         case 1:
413         case 2:
414                 VFC_SAA9051_SA(dev,VFC_SAA9051_HSY_START) = 0x72; 
415                 VFC_SAA9051_SA(dev,VFC_SAA9051_HSY_STOP) = 0x52;
416                 VFC_SAA9051_SA(dev,VFC_SAA9051_HC_START) = 0x36;
417                 VFC_SAA9051_SA(dev,VFC_SAA9051_HC_STOP) = 0x18;
418                 VFC_SAA9051_SA(dev,VFC_SAA9051_HORIZ_PEAK) = VFC_SAA9051_BP2;
419                 VFC_SAA9051_SA(dev,VFC_SAA9051_C3) = VFC_SAA9051_CT | VFC_SAA9051_SS3;
420                 VFC_SAA9051_SA(dev,VFC_SAA9051_SECAM_DELAY) = 0x3e;
421                 break;
422         case 3:
423                 VFC_SAA9051_SA(dev,VFC_SAA9051_HSY_START) = 0x3a;
424                 VFC_SAA9051_SA(dev,VFC_SAA9051_HSY_STOP) = 0x17;
425                 VFC_SAA9051_SA(dev,VFC_SAA9051_HC_START) = 0xfa;
426                 VFC_SAA9051_SA(dev,VFC_SAA9051_HC_STOP) = 0xde;
427                 VFC_SAA9051_SA(dev,VFC_SAA9051_HORIZ_PEAK) =
428                         VFC_SAA9051_BY | VFC_SAA9051_PF | VFC_SAA9051_BP2;
429                 VFC_SAA9051_SA(dev,VFC_SAA9051_C3) = VFC_SAA9051_YC;
430                 VFC_SAA9051_SA(dev,VFC_SAA9051_SECAM_DELAY) = 0;
431                 VFC_SAA9051_SA(dev,VFC_SAA9051_C2) &=
432                         ~(VFC_SAA9051_SS0 | VFC_SAA9051_SS1);
433                 break;
434         default:
435                 ret = -EINVAL;
436                 return ret;
437                 break;
438         }
439
440         switch(cmd) {
441         case 1:
442                 VFC_SAA9051_SA(dev,VFC_SAA9051_C2) |=
443                         (VFC_SAA9051_SS0 | VFC_SAA9051_SS1);
444                 break;
445         case 2:
446                 VFC_SAA9051_SA(dev,VFC_SAA9051_C2) &=
447                         ~(VFC_SAA9051_SS0 | VFC_SAA9051_SS1);
448                 VFC_SAA9051_SA(dev,VFC_SAA9051_C2) |= VFC_SAA9051_SS0; 
449                 break;
450         case 3:
451                 break;
452         default:
453                 ret = -EINVAL;
454                 return ret;
455                 break;
456         }
457         VFC_SAA9051_SA(dev,VFC_SAA9051_C3) &= ~(VFC_SAA9051_SS2);
458         ret=vfc_update_saa9051(dev);
459         udelay(500);
460         VFC_SAA9051_SA(dev,VFC_SAA9051_C3) |= (VFC_SAA9051_SS2);
461         ret=vfc_update_saa9051(dev);
462         return ret;
463 }
464
465 int vfc_set_video_ioctl(struct inode *inode, struct file *file, 
466                         struct vfc_dev *dev, unsigned long arg) 
467 {
468         int ret = 0;
469         int cmd;
470
471         if(copy_from_user(&cmd, (void *)arg, sizeof(unsigned int))) {
472                 VFC_IOCTL_DEBUG_PRINTK(("vfc%d: User passed bogus pointer to "
473                                         "vfc_set_video_ioctl\n",
474                                         dev->instance));
475                 return ret;
476         }
477         
478         VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCSVID) arg=0x%x\n",
479                                 dev->instance, cmd));
480         switch(cmd) {
481         case STD_NTSC:
482                 VFC_SAA9051_SA(dev,VFC_SAA9051_C1) &= ~VFC_SAA9051_ALT;
483                 VFC_SAA9051_SA(dev,VFC_SAA9051_C1) |= VFC_SAA9051_YPN | 
484                         VFC_SAA9051_CCFR0 | VFC_SAA9051_CCFR1 | VFC_SAA9051_FS;
485                 ret = vfc_update_saa9051(dev);
486                 break;
487         case STD_PAL:
488                 VFC_SAA9051_SA(dev,VFC_SAA9051_C1) &= ~(VFC_SAA9051_YPN | 
489                                                         VFC_SAA9051_CCFR1 | 
490                                                         VFC_SAA9051_CCFR0 |
491                                                         VFC_SAA9051_FS);
492                 VFC_SAA9051_SA(dev,VFC_SAA9051_C1) |= VFC_SAA9051_ALT;
493                 ret = vfc_update_saa9051(dev);
494                 break;
495
496         case COLOR_ON:
497                 VFC_SAA9051_SA(dev,VFC_SAA9051_C1) |= VFC_SAA9051_CO;
498                 VFC_SAA9051_SA(dev,VFC_SAA9051_HORIZ_PEAK) &=
499                         ~(VFC_SAA9051_BY | VFC_SAA9051_PF);
500                 ret = vfc_update_saa9051(dev);
501                 break;
502         case MONO:
503                 VFC_SAA9051_SA(dev,VFC_SAA9051_C1) &= ~(VFC_SAA9051_CO);
504                 VFC_SAA9051_SA(dev,VFC_SAA9051_HORIZ_PEAK) |=
505                         (VFC_SAA9051_BY | VFC_SAA9051_PF);
506                 ret = vfc_update_saa9051(dev);
507                 break;
508         default:
509                 ret = -EINVAL;
510                 break;
511         };
512
513         return ret;
514 }
515
516 int vfc_get_video_ioctl(struct inode *inode, struct file *file, 
517                         struct vfc_dev *dev, unsigned long arg) 
518 {
519         int ret = 0;
520         unsigned int status = NO_LOCK;
521         unsigned char buf[1];
522
523         if(vfc_i2c_recvbuf(dev, VFC_SAA9051_ADDR, buf, 1)) {
524                 printk(KERN_ERR "vfc%d: Unable to get status\n",
525                        dev->instance);
526                 return -EIO;
527         }
528
529         if(buf[0] & VFC_SAA9051_HLOCK) {
530                 status = NO_LOCK;
531         } else if(buf[0] & VFC_SAA9051_FD) {
532                 if(buf[0] & VFC_SAA9051_CD)
533                         status = NTSC_COLOR;
534                 else
535                         status = NTSC_NOCOLOR;
536         } else {
537                 if(buf[0] & VFC_SAA9051_CD)
538                         status = PAL_COLOR;
539                 else
540                         status = PAL_NOCOLOR;
541         }
542         VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCGVID) returning status 0x%x; "
543                                 "buf[0]=%x\n", dev->instance, status, buf[0]));
544
545         if (copy_to_user((void *)arg,&status,sizeof(unsigned int))) {
546                 VFC_IOCTL_DEBUG_PRINTK(("vfc%d: User passed bogus pointer to "
547                                         "vfc_get_video_ioctl\n",
548                                         dev->instance));
549                 return ret;
550         }
551         return ret;
552 }
553
554 static int vfc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
555               unsigned long arg) 
556 {
557         int ret = 0;
558         unsigned int tmp;
559         struct vfc_dev *dev;
560
561         dev = vfc_get_dev_ptr(iminor(inode));
562         if(dev == NULL)
563                 return -ENODEV;
564         
565         switch(cmd & 0x0000ffff) {
566         case VFCGCTRL:
567 #if 0
568                 VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCGCTRL)\n", dev->instance));
569 #endif
570                 tmp = sbus_readl(&dev->regs->control);
571                 if(copy_to_user((void *)arg, &tmp, sizeof(unsigned int))) {
572                         ret = -EFAULT;
573                         break;
574                 }
575                 ret = 0;
576                 break;
577         case VFCSCTRL:
578                 ret = vfc_set_control_ioctl(inode, file, dev, arg);
579                 break;
580         case VFCGVID:
581                 ret = vfc_get_video_ioctl(inode, file, dev, arg);
582                 break;
583         case VFCSVID:
584                 ret = vfc_set_video_ioctl(inode, file, dev, arg);
585                 break;
586         case VFCHUE:
587                 VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCHUE)\n", dev->instance));
588                 if(copy_from_user(&tmp,(void *)arg,sizeof(unsigned int))) {
589                         VFC_IOCTL_DEBUG_PRINTK(("vfc%d: User passed bogus pointer "
590                                                 "to IOCTL(VFCHUE)", dev->instance));
591                         ret = -EFAULT;
592                 } else {
593                         VFC_SAA9051_SA(dev,VFC_SAA9051_HUE) = tmp;
594                         vfc_update_saa9051(dev);
595                         ret = 0;
596                 }
597                 break;
598         case VFCPORTCHG:
599                 ret = vfc_port_change_ioctl(inode, file, dev, arg);
600                 break;
601         case VFCRDINFO:
602                 ret = -EINVAL;
603                 VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCRDINFO)\n", dev->instance));
604                 break;
605         default:
606                 ret = vfc_debug(vfc_get_dev_ptr(iminor(inode)),
607                                 cmd, arg);
608                 break;
609         };
610
611         return ret;
612 }
613
614 static int vfc_mmap(struct inode *inode, struct file *file, 
615                     struct vm_area_struct *vma) 
616 {
617         unsigned int map_size, ret, map_offset;
618         struct vfc_dev *dev;
619         
620         dev = vfc_get_dev_ptr(iminor(inode));
621         if(dev == NULL)
622                 return -ENODEV;
623
624         map_size = vma->vm_end - vma->vm_start;
625         if(map_size > sizeof(struct vfc_regs)) 
626                 map_size = sizeof(struct vfc_regs);
627
628         vma->vm_flags |=
629                 (VM_SHM | VM_LOCKED | VM_IO | VM_MAYREAD | VM_MAYWRITE | VM_MAYSHARE);
630         map_offset = (unsigned int) (long)dev->phys_regs;
631         ret = io_remap_page_range(vma, vma->vm_start, map_offset, map_size, 
632                                   vma->vm_page_prot, dev->which_io);
633
634         if(ret)
635                 return -EAGAIN;
636
637         return 0;
638 }
639
640
641 static struct file_operations vfc_fops = {
642         .owner =        THIS_MODULE,
643         .llseek =       no_llseek,
644         .ioctl =        vfc_ioctl,
645         .mmap =         vfc_mmap,
646         .open =         vfc_open,
647         .release =      vfc_release,
648 };
649
650 static int vfc_probe(void)
651 {
652         struct sbus_bus *sbus;
653         struct sbus_dev *sdev = NULL;
654         int ret;
655         int instance = 0, cards = 0;
656
657         for_all_sbusdev(sdev, sbus) {
658                 if (strcmp(sdev->prom_name, "vfc") == 0) {
659                         cards++;
660                         continue;
661                 }
662         }
663
664         if (!cards)
665                 return -ENODEV;
666
667         vfc_dev_lst = (struct vfc_dev **)kmalloc(sizeof(struct vfc_dev *) *
668                                                  (cards+1),
669                                                  GFP_KERNEL);
670         if (vfc_dev_lst == NULL)
671                 return -ENOMEM;
672         memset(vfc_dev_lst, 0, sizeof(struct vfc_dev *) * (cards + 1));
673         vfc_dev_lst[cards] = NULL;
674
675         ret = register_chrdev(VFC_MAJOR, vfcstr, &vfc_fops);
676         if(ret) {
677                 printk(KERN_ERR "Unable to get major number %d\n", VFC_MAJOR);
678                 kfree(vfc_dev_lst);
679                 return -EIO;
680         }
681         devfs_mk_dir("vfc");
682         instance = 0;
683         for_all_sbusdev(sdev, sbus) {
684                 if (strcmp(sdev->prom_name, "vfc") == 0) {
685                         vfc_dev_lst[instance]=(struct vfc_dev *)
686                                 kmalloc(sizeof(struct vfc_dev), GFP_KERNEL);
687                         if (vfc_dev_lst[instance] == NULL)
688                                 return -ENOMEM;
689                         ret = init_vfc_device(sdev,
690                                               vfc_dev_lst[instance],
691                                               instance);
692                         if(ret) {
693                                 printk(KERN_ERR "Unable to initialize"
694                                        " vfc%d device\n",
695                                        instance);
696                         } else {
697                         }
698                 
699                         instance++;
700                         continue;
701                 }
702         }
703
704         return 0;
705 }
706
707 #ifdef MODULE
708 int init_module(void)
709 #else 
710 int vfc_init(void)
711 #endif
712 {
713         return vfc_probe();
714 }
715
716 #ifdef MODULE
717 static void deinit_vfc_device(struct vfc_dev *dev)
718 {
719         if(dev == NULL)
720                 return;
721         devfs_remove("vfc/%d", dev->instance);
722         sbus_iounmap((unsigned long)dev->regs, sizeof(struct vfc_regs));
723         kfree(dev);
724 }
725
726 void cleanup_module(void)
727 {
728         struct vfc_dev **devp;
729
730         unregister_chrdev(VFC_MAJOR,vfcstr);
731
732         for (devp = vfc_dev_lst; *devp; devp++)
733                 deinit_vfc_device(*devp);
734
735         devfs_remove("vfc");
736         kfree(vfc_dev_lst);
737         return;
738 }
739 #endif
740
741 MODULE_LICENSE("GPL");
742