blob: 55b2b31bd7abbf0d2dbcde64af303c952b442821 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
47static struct file_operations vfc_fops;
48struct vfc_dev **vfc_dev_lst;
49static char vfcstr[]="vfc";
50static 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
57void vfc_lock_device(struct vfc_dev *dev)
58{
59 down(&dev->device_lock_sem);
60}
61
62void vfc_unlock_device(struct vfc_dev *dev)
63{
64 up(&dev->device_lock_sem);
65}
66
67
68void 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
78void 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
88int 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
110int 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
122int 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
135int 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 dev->busy=0;
141 return 0;
142}
143
144int init_vfc_device(struct sbus_dev *sdev,struct vfc_dev *dev, int instance)
145{
146 if(dev == NULL) {
147 printk(KERN_ERR "VFC: Bogus pointer passed\n");
148 return -ENOMEM;
149 }
150 printk("Initializing vfc%d\n",instance);
151 dev->regs = NULL;
Al Virob7c690b2005-12-06 05:50:56 -0500152 dev->regs = (volatile struct vfc_regs __iomem *)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 sbus_ioremap(&sdev->resource[0], 0,
154 sizeof(struct vfc_regs), vfcstr);
155 dev->which_io = sdev->reg_addrs[0].which_io;
156 dev->phys_regs = (struct vfc_regs *) sdev->reg_addrs[0].phys_addr;
157 if (dev->regs == NULL)
158 return -EIO;
159
160 printk("vfc%d: registers mapped at phys_addr: 0x%lx\n virt_addr: 0x%lx\n",
161 instance,(unsigned long)sdev->reg_addrs[0].phys_addr,(unsigned long)dev->regs);
162
163 if (init_vfc_devstruct(dev, instance))
164 return -EINVAL;
165 if (init_vfc_hw(dev))
166 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return 0;
168}
169
170
171struct vfc_dev *vfc_get_dev_ptr(int instance)
172{
173 return vfc_dev_lst[instance];
174}
175
176static DEFINE_SPINLOCK(vfc_dev_lock);
177
178static int vfc_open(struct inode *inode, struct file *file)
179{
180 struct vfc_dev *dev;
181
182 spin_lock(&vfc_dev_lock);
183 dev = vfc_get_dev_ptr(iminor(inode));
184 if (dev == NULL) {
185 spin_unlock(&vfc_dev_lock);
186 return -ENODEV;
187 }
188 if (dev->busy) {
189 spin_unlock(&vfc_dev_lock);
190 return -EBUSY;
191 }
192
193 dev->busy = 1;
194 spin_unlock(&vfc_dev_lock);
195
196 vfc_lock_device(dev);
197
198 vfc_csr_init(dev);
199 vfc_pcf8584_init(dev);
200 vfc_init_i2c_bus(dev);
201 vfc_saa9051_init(dev);
202 vfc_memptr_reset(dev);
203 vfc_captstat_reset(dev);
204
205 vfc_unlock_device(dev);
206 return 0;
207}
208
209static int vfc_release(struct inode *inode,struct file *file)
210{
211 struct vfc_dev *dev;
212
213 spin_lock(&vfc_dev_lock);
214 dev = vfc_get_dev_ptr(iminor(inode));
215 if (!dev || !dev->busy) {
216 spin_unlock(&vfc_dev_lock);
217 return -EINVAL;
218 }
219 dev->busy = 0;
220 spin_unlock(&vfc_dev_lock);
221 return 0;
222}
223
224static int vfc_debug(struct vfc_dev *dev, int cmd, void __user *argp)
225{
226 struct vfc_debug_inout inout;
227 unsigned char *buffer;
228
229 if (!capable(CAP_SYS_ADMIN))
230 return -EPERM;
231
232 switch(cmd) {
233 case VFC_I2C_SEND:
234 if(copy_from_user(&inout, argp, sizeof(inout)))
235 return -EFAULT;
236
237 buffer = kmalloc(inout.len, GFP_KERNEL);
238 if (buffer == NULL)
239 return -ENOMEM;
240
241 if(copy_from_user(buffer, inout.buffer, inout.len)) {
242 kfree(buffer);
243 return -EFAULT;
244 }
245
246
247 vfc_lock_device(dev);
248 inout.ret=
249 vfc_i2c_sendbuf(dev,inout.addr & 0xff,
250 buffer,inout.len);
251
252 if (copy_to_user(argp,&inout,sizeof(inout))) {
253 kfree(buffer);
254 return -EFAULT;
255 }
256 vfc_unlock_device(dev);
257
258 break;
259 case VFC_I2C_RECV:
260 if (copy_from_user(&inout, argp, sizeof(inout)))
261 return -EFAULT;
262
263 buffer = kmalloc(inout.len, GFP_KERNEL);
264 if (buffer == NULL)
265 return -ENOMEM;
266
267 memset(buffer,0,inout.len);
268 vfc_lock_device(dev);
269 inout.ret=
270 vfc_i2c_recvbuf(dev,inout.addr & 0xff
271 ,buffer,inout.len);
272 vfc_unlock_device(dev);
273
274 if (copy_to_user(inout.buffer, buffer, inout.len)) {
275 kfree(buffer);
276 return -EFAULT;
277 }
278 if (copy_to_user(argp,&inout,sizeof(inout))) {
279 kfree(buffer);
280 return -EFAULT;
281 }
282 kfree(buffer);
283 break;
284 default:
285 return -EINVAL;
286 };
287
288 return 0;
289}
290
291int vfc_capture_start(struct vfc_dev *dev)
292{
293 vfc_captstat_reset(dev);
294 dev->control_reg = sbus_readl(&dev->regs->control);
295 if((dev->control_reg & VFC_STATUS_CAPTURE)) {
296 printk(KERN_ERR "vfc%d: vfc capture status not reset\n",
297 dev->instance);
298 return -EIO;
299 }
300
301 vfc_lock_device(dev);
302 dev->control_reg &= ~VFC_CONTROL_CAPTURE;
303 sbus_writel(dev->control_reg, &dev->regs->control);
304 dev->control_reg |= VFC_CONTROL_CAPTURE;
305 sbus_writel(dev->control_reg, &dev->regs->control);
306 dev->control_reg &= ~VFC_CONTROL_CAPTURE;
307 sbus_writel(dev->control_reg, &dev->regs->control);
308 vfc_unlock_device(dev);
309
310 return 0;
311}
312
313int vfc_capture_poll(struct vfc_dev *dev)
314{
315 int timeout = 1000;
316
317 while (!timeout--) {
Al Virob7c690b2005-12-06 05:50:56 -0500318 if (sbus_readl(&dev->regs->control) & VFC_STATUS_CAPTURE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 break;
320 vfc_i2c_delay_no_busy(dev, 100);
321 }
322 if(!timeout) {
323 printk(KERN_WARNING "vfc%d: capture timed out\n",
324 dev->instance);
325 return -ETIMEDOUT;
326 }
327 return 0;
328}
329
330
331
332static int vfc_set_control_ioctl(struct inode *inode, struct file *file,
333 struct vfc_dev *dev, unsigned long arg)
334{
335 int setcmd, ret = 0;
336
337 if (copy_from_user(&setcmd,(void __user *)arg,sizeof(unsigned int)))
338 return -EFAULT;
339
340 VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCSCTRL) arg=0x%x\n",
341 dev->instance,setcmd));
342
343 switch(setcmd) {
344 case MEMPRST:
345 vfc_lock_device(dev);
346 vfc_memptr_reset(dev);
347 vfc_unlock_device(dev);
348 ret=0;
349 break;
350 case CAPTRCMD:
351 vfc_capture_start(dev);
352 vfc_capture_poll(dev);
353 break;
354 case DIAGMODE:
355 if(capable(CAP_SYS_ADMIN)) {
356 vfc_lock_device(dev);
357 dev->control_reg |= VFC_CONTROL_DIAGMODE;
358 sbus_writel(dev->control_reg, &dev->regs->control);
359 vfc_unlock_device(dev);
360 ret = 0;
361 } else {
362 ret = -EPERM;
363 }
364 break;
365 case NORMMODE:
366 vfc_lock_device(dev);
367 dev->control_reg &= ~VFC_CONTROL_DIAGMODE;
368 sbus_writel(dev->control_reg, &dev->regs->control);
369 vfc_unlock_device(dev);
370 ret = 0;
371 break;
372 case CAPTRSTR:
373 vfc_capture_start(dev);
374 ret = 0;
375 break;
376 case CAPTRWAIT:
377 vfc_capture_poll(dev);
378 ret = 0;
379 break;
380 default:
381 ret = -EINVAL;
382 break;
383 };
384
385 return ret;
386}
387
388
389int vfc_port_change_ioctl(struct inode *inode, struct file *file,
390 struct vfc_dev *dev, unsigned long arg)
391{
392 int ret = 0;
393 int cmd;
394
395 if(copy_from_user(&cmd, (void __user *)arg, sizeof(unsigned int))) {
396 VFC_IOCTL_DEBUG_PRINTK(("vfc%d: User passed bogus pointer to "
397 "vfc_port_change_ioctl\n",
398 dev->instance));
399 return -EFAULT;
400 }
401
402 VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCPORTCHG) arg=0x%x\n",
403 dev->instance, cmd));
404
405 switch(cmd) {
406 case 1:
407 case 2:
408 VFC_SAA9051_SA(dev,VFC_SAA9051_HSY_START) = 0x72;
409 VFC_SAA9051_SA(dev,VFC_SAA9051_HSY_STOP) = 0x52;
410 VFC_SAA9051_SA(dev,VFC_SAA9051_HC_START) = 0x36;
411 VFC_SAA9051_SA(dev,VFC_SAA9051_HC_STOP) = 0x18;
412 VFC_SAA9051_SA(dev,VFC_SAA9051_HORIZ_PEAK) = VFC_SAA9051_BP2;
413 VFC_SAA9051_SA(dev,VFC_SAA9051_C3) = VFC_SAA9051_CT | VFC_SAA9051_SS3;
414 VFC_SAA9051_SA(dev,VFC_SAA9051_SECAM_DELAY) = 0x3e;
415 break;
416 case 3:
417 VFC_SAA9051_SA(dev,VFC_SAA9051_HSY_START) = 0x3a;
418 VFC_SAA9051_SA(dev,VFC_SAA9051_HSY_STOP) = 0x17;
419 VFC_SAA9051_SA(dev,VFC_SAA9051_HC_START) = 0xfa;
420 VFC_SAA9051_SA(dev,VFC_SAA9051_HC_STOP) = 0xde;
421 VFC_SAA9051_SA(dev,VFC_SAA9051_HORIZ_PEAK) =
422 VFC_SAA9051_BY | VFC_SAA9051_PF | VFC_SAA9051_BP2;
423 VFC_SAA9051_SA(dev,VFC_SAA9051_C3) = VFC_SAA9051_YC;
424 VFC_SAA9051_SA(dev,VFC_SAA9051_SECAM_DELAY) = 0;
425 VFC_SAA9051_SA(dev,VFC_SAA9051_C2) &=
426 ~(VFC_SAA9051_SS0 | VFC_SAA9051_SS1);
427 break;
428 default:
429 ret = -EINVAL;
430 return ret;
431 break;
432 }
433
434 switch(cmd) {
435 case 1:
436 VFC_SAA9051_SA(dev,VFC_SAA9051_C2) |=
437 (VFC_SAA9051_SS0 | VFC_SAA9051_SS1);
438 break;
439 case 2:
440 VFC_SAA9051_SA(dev,VFC_SAA9051_C2) &=
441 ~(VFC_SAA9051_SS0 | VFC_SAA9051_SS1);
442 VFC_SAA9051_SA(dev,VFC_SAA9051_C2) |= VFC_SAA9051_SS0;
443 break;
444 case 3:
445 break;
446 default:
447 ret = -EINVAL;
448 return ret;
449 break;
450 }
451 VFC_SAA9051_SA(dev,VFC_SAA9051_C3) &= ~(VFC_SAA9051_SS2);
452 ret=vfc_update_saa9051(dev);
453 udelay(500);
454 VFC_SAA9051_SA(dev,VFC_SAA9051_C3) |= (VFC_SAA9051_SS2);
455 ret=vfc_update_saa9051(dev);
456 return ret;
457}
458
459int vfc_set_video_ioctl(struct inode *inode, struct file *file,
460 struct vfc_dev *dev, unsigned long arg)
461{
462 int ret = 0;
463 int cmd;
464
465 if(copy_from_user(&cmd, (void __user *)arg, sizeof(unsigned int))) {
466 VFC_IOCTL_DEBUG_PRINTK(("vfc%d: User passed bogus pointer to "
467 "vfc_set_video_ioctl\n",
468 dev->instance));
469 return ret;
470 }
471
472 VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCSVID) arg=0x%x\n",
473 dev->instance, cmd));
474 switch(cmd) {
475 case STD_NTSC:
476 VFC_SAA9051_SA(dev,VFC_SAA9051_C1) &= ~VFC_SAA9051_ALT;
477 VFC_SAA9051_SA(dev,VFC_SAA9051_C1) |= VFC_SAA9051_YPN |
478 VFC_SAA9051_CCFR0 | VFC_SAA9051_CCFR1 | VFC_SAA9051_FS;
479 ret = vfc_update_saa9051(dev);
480 break;
481 case STD_PAL:
482 VFC_SAA9051_SA(dev,VFC_SAA9051_C1) &= ~(VFC_SAA9051_YPN |
483 VFC_SAA9051_CCFR1 |
484 VFC_SAA9051_CCFR0 |
485 VFC_SAA9051_FS);
486 VFC_SAA9051_SA(dev,VFC_SAA9051_C1) |= VFC_SAA9051_ALT;
487 ret = vfc_update_saa9051(dev);
488 break;
489
490 case COLOR_ON:
491 VFC_SAA9051_SA(dev,VFC_SAA9051_C1) |= VFC_SAA9051_CO;
492 VFC_SAA9051_SA(dev,VFC_SAA9051_HORIZ_PEAK) &=
493 ~(VFC_SAA9051_BY | VFC_SAA9051_PF);
494 ret = vfc_update_saa9051(dev);
495 break;
496 case MONO:
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 default:
503 ret = -EINVAL;
504 break;
505 };
506
507 return ret;
508}
509
510int vfc_get_video_ioctl(struct inode *inode, struct file *file,
511 struct vfc_dev *dev, unsigned long arg)
512{
513 int ret = 0;
514 unsigned int status = NO_LOCK;
515 unsigned char buf[1];
516
517 if(vfc_i2c_recvbuf(dev, VFC_SAA9051_ADDR, buf, 1)) {
518 printk(KERN_ERR "vfc%d: Unable to get status\n",
519 dev->instance);
520 return -EIO;
521 }
522
523 if(buf[0] & VFC_SAA9051_HLOCK) {
524 status = NO_LOCK;
525 } else if(buf[0] & VFC_SAA9051_FD) {
526 if(buf[0] & VFC_SAA9051_CD)
527 status = NTSC_COLOR;
528 else
529 status = NTSC_NOCOLOR;
530 } else {
531 if(buf[0] & VFC_SAA9051_CD)
532 status = PAL_COLOR;
533 else
534 status = PAL_NOCOLOR;
535 }
536 VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCGVID) returning status 0x%x; "
537 "buf[0]=%x\n", dev->instance, status, buf[0]));
538
539 if (copy_to_user((void __user *)arg,&status,sizeof(unsigned int))) {
540 VFC_IOCTL_DEBUG_PRINTK(("vfc%d: User passed bogus pointer to "
541 "vfc_get_video_ioctl\n",
542 dev->instance));
543 return ret;
544 }
545 return ret;
546}
547
548static int vfc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
549 unsigned long arg)
550{
551 int ret = 0;
552 unsigned int tmp;
553 struct vfc_dev *dev;
554 void __user *argp = (void __user *)arg;
555
556 dev = vfc_get_dev_ptr(iminor(inode));
557 if(dev == NULL)
558 return -ENODEV;
559
560 switch(cmd & 0x0000ffff) {
561 case VFCGCTRL:
562#if 0
563 VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCGCTRL)\n", dev->instance));
564#endif
565 tmp = sbus_readl(&dev->regs->control);
566 if(copy_to_user(argp, &tmp, sizeof(unsigned int))) {
567 ret = -EFAULT;
568 break;
569 }
570 ret = 0;
571 break;
572 case VFCSCTRL:
573 ret = vfc_set_control_ioctl(inode, file, dev, arg);
574 break;
575 case VFCGVID:
576 ret = vfc_get_video_ioctl(inode, file, dev, arg);
577 break;
578 case VFCSVID:
579 ret = vfc_set_video_ioctl(inode, file, dev, arg);
580 break;
581 case VFCHUE:
582 VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCHUE)\n", dev->instance));
583 if(copy_from_user(&tmp,argp,sizeof(unsigned int))) {
584 VFC_IOCTL_DEBUG_PRINTK(("vfc%d: User passed bogus pointer "
585 "to IOCTL(VFCHUE)", dev->instance));
586 ret = -EFAULT;
587 } else {
588 VFC_SAA9051_SA(dev,VFC_SAA9051_HUE) = tmp;
589 vfc_update_saa9051(dev);
590 ret = 0;
591 }
592 break;
593 case VFCPORTCHG:
594 ret = vfc_port_change_ioctl(inode, file, dev, arg);
595 break;
596 case VFCRDINFO:
597 ret = -EINVAL;
598 VFC_IOCTL_DEBUG_PRINTK(("vfc%d: IOCTL(VFCRDINFO)\n", dev->instance));
599 break;
600 default:
601 ret = vfc_debug(vfc_get_dev_ptr(iminor(inode)), cmd, argp);
602 break;
603 };
604
605 return ret;
606}
607
608static int vfc_mmap(struct file *file, struct vm_area_struct *vma)
609{
610 unsigned int map_size, ret, map_offset;
611 struct vfc_dev *dev;
612
613 dev = vfc_get_dev_ptr(iminor(file->f_dentry->d_inode));
614 if(dev == NULL)
615 return -ENODEV;
616
617 map_size = vma->vm_end - vma->vm_start;
618 if(map_size > sizeof(struct vfc_regs))
619 map_size = sizeof(struct vfc_regs);
620
621 vma->vm_flags |=
Christoph Lameter68402dd2006-06-25 05:46:47 -0700622 (VM_MAYREAD | VM_MAYWRITE | VM_MAYSHARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 map_offset = (unsigned int) (long)dev->phys_regs;
624 ret = io_remap_pfn_range(vma, vma->vm_start,
625 MK_IOSPACE_PFN(dev->which_io,
626 map_offset >> PAGE_SHIFT),
627 map_size, vma->vm_page_prot);
628
629 if(ret)
630 return -EAGAIN;
631
632 return 0;
633}
634
635
636static struct file_operations vfc_fops = {
637 .owner = THIS_MODULE,
638 .llseek = no_llseek,
639 .ioctl = vfc_ioctl,
640 .mmap = vfc_mmap,
641 .open = vfc_open,
642 .release = vfc_release,
643};
644
645static int vfc_probe(void)
646{
647 struct sbus_bus *sbus;
648 struct sbus_dev *sdev = NULL;
649 int ret;
650 int instance = 0, cards = 0;
651
652 for_all_sbusdev(sdev, sbus) {
653 if (strcmp(sdev->prom_name, "vfc") == 0) {
654 cards++;
655 continue;
656 }
657 }
658
659 if (!cards)
660 return -ENODEV;
661
662 vfc_dev_lst = (struct vfc_dev **)kmalloc(sizeof(struct vfc_dev *) *
663 (cards+1),
664 GFP_KERNEL);
665 if (vfc_dev_lst == NULL)
666 return -ENOMEM;
667 memset(vfc_dev_lst, 0, sizeof(struct vfc_dev *) * (cards + 1));
668 vfc_dev_lst[cards] = NULL;
669
670 ret = register_chrdev(VFC_MAJOR, vfcstr, &vfc_fops);
671 if(ret) {
672 printk(KERN_ERR "Unable to get major number %d\n", VFC_MAJOR);
673 kfree(vfc_dev_lst);
674 return -EIO;
675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 instance = 0;
677 for_all_sbusdev(sdev, sbus) {
678 if (strcmp(sdev->prom_name, "vfc") == 0) {
679 vfc_dev_lst[instance]=(struct vfc_dev *)
680 kmalloc(sizeof(struct vfc_dev), GFP_KERNEL);
681 if (vfc_dev_lst[instance] == NULL)
682 return -ENOMEM;
683 ret = init_vfc_device(sdev,
684 vfc_dev_lst[instance],
685 instance);
686 if(ret) {
687 printk(KERN_ERR "Unable to initialize"
688 " vfc%d device\n",
689 instance);
690 } else {
691 }
692
693 instance++;
694 continue;
695 }
696 }
697
698 return 0;
699}
700
701#ifdef MODULE
702int init_module(void)
703#else
704int vfc_init(void)
705#endif
706{
707 return vfc_probe();
708}
709
710#ifdef MODULE
711static void deinit_vfc_device(struct vfc_dev *dev)
712{
713 if(dev == NULL)
714 return;
Al Virob7c690b2005-12-06 05:50:56 -0500715 sbus_iounmap(dev->regs, sizeof(struct vfc_regs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 kfree(dev);
717}
718
719void cleanup_module(void)
720{
721 struct vfc_dev **devp;
722
723 unregister_chrdev(VFC_MAJOR,vfcstr);
724
725 for (devp = vfc_dev_lst; *devp; devp++)
726 deinit_vfc_device(*devp);
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 kfree(vfc_dev_lst);
729 return;
730}
731#endif
732
733MODULE_LICENSE("GPL");
734