static int __devinit pio2_probe(struct vme_dev *vdev) { struct pio2_card *card; int retval; int i; u8 reg; int vec; card = kzalloc(sizeof(struct pio2_card), GFP_KERNEL); if (card == NULL) { dev_err(&vdev->dev, "Unable to allocate card structure\n"); retval = -ENOMEM; goto err_struct; } card->id = vdev->num; card->bus = bus[card->id]; card->base = base[card->id]; card->irq_vector = vector[card->id]; card->irq_level = level[card->id] & PIO2_VME_INT_MASK; strncpy(card->variant, variant[card->id], PIO2_VARIANT_LENGTH); card->vdev = vdev; for (i = 0; i < PIO2_VARIANT_LENGTH; i++) { if (isdigit(card->variant[i]) == 0) { dev_err(&card->vdev->dev, "Variant invalid\n"); retval = -EINVAL; goto err_variant; } } /* * Bottom 4 bits of VME interrupt vector used to determine source, * provided vector should only use upper 4 bits. */ if (card->irq_vector & ~PIO2_VME_VECTOR_MASK) { dev_err(&card->vdev->dev, "Invalid VME IRQ Vector, vector must not use lower 4 bits\n"); retval = -EINVAL; goto err_vector; } /* * There is no way to determine the build variant or whether each bank * is input, output or both at run time. The inputs are also inverted * if configured as both. * * We pass in the board variant and use that to determine the * configuration of the banks. */ for (i = 1; i < PIO2_VARIANT_LENGTH; i++) { switch (card->variant[i]) { case '0': card->bank[i-1].config = NOFIT; break; case '1': case '2': case '3': case '4': card->bank[i-1].config = INPUT; break; case '5': card->bank[i-1].config = OUTPUT; break; case '6': case '7': case '8': case '9': card->bank[i-1].config = BOTH; break; } } /* Get a master window and position over regs */ card->window = vme_master_request(vdev, VME_A24, VME_SCT, VME_D16); if (card->window == NULL) { dev_err(&card->vdev->dev, "Unable to assign VME master resource\n"); retval = -EIO; goto err_window; } retval = vme_master_set(card->window, 1, card->base, 0x10000, VME_A24, (VME_SCT | VME_USER | VME_DATA), VME_D16); if (retval) { dev_err(&card->vdev->dev, "Unable to configure VME master resource\n"); goto err_set; } /* * There is also no obvious register which we can probe to determine * whether the provided base is valid. If we can read the "ID Register" * offset and the reset function doesn't error, assume we have a valid * location. */ retval = vme_master_read(card->window, ®, 1, PIO2_REGS_ID); if (retval < 0) { dev_err(&card->vdev->dev, "Unable to read from device\n"); goto err_read; } dev_dbg(&card->vdev->dev, "ID Register:%x\n", reg); /* * Ensure all the I/O is cleared. We can't read back the states, so * this is the only method we have to ensure that the I/O is in a known * state. */ retval = pio2_reset_card(card); if (retval) { dev_err(&card->vdev->dev, "Failed to reset card, is location valid?"); retval = -ENODEV; goto err_reset; } /* Configure VME Interrupts */ reg = card->irq_level; if (pio2_get_led(card)) reg |= PIO2_LED; if (loopback) reg |= PIO2_LOOP; retval = vme_master_write(card->window, ®, 1, PIO2_REGS_CTRL); if (retval < 0) return retval; /* Set VME vector */ retval = vme_master_write(card->window, &card->irq_vector, 1, PIO2_REGS_VME_VECTOR); if (retval < 0) return retval; /* Attach spurious interrupt handler. */ vec = card->irq_vector | PIO2_VME_VECTOR_SPUR; retval = vme_irq_request(vdev, card->irq_level, vec, &pio2_int, (void *)card); if (retval < 0) { dev_err(&card->vdev->dev, "Unable to attach VME interrupt vector0x%x, level 0x%x\n", vec, card->irq_level); goto err_irq; } /* Attach GPIO interrupt handlers. */ for (i = 0; i < 4; i++) { vec = card->irq_vector | PIO2_VECTOR_BANK[i]; retval = vme_irq_request(vdev, card->irq_level, vec, &pio2_int, (void *)card); if (retval < 0) { dev_err(&card->vdev->dev, "Unable to attach VME interrupt vector0x%x, level 0x%x\n", vec, card->irq_level); goto err_gpio_irq; } } /* Attach counter interrupt handlers. */ for (i = 0; i < 6; i++) { vec = card->irq_vector | PIO2_VECTOR_CNTR[i]; retval = vme_irq_request(vdev, card->irq_level, vec, &pio2_int, (void *)card); if (retval < 0) { dev_err(&card->vdev->dev, "Unable to attach VME interrupt vector0x%x, level 0x%x\n", vec, card->irq_level); goto err_cntr_irq; } } /* Register IO */ retval = pio2_gpio_init(card); if (retval < 0) { dev_err(&card->vdev->dev, "Unable to register with GPIO framework\n"); goto err_gpio; } /* Set LED - This also sets interrupt level */ retval = pio2_set_led(card, 0); if (retval < 0) { dev_err(&card->vdev->dev, "Unable to set LED\n"); goto err_led; } dev_set_drvdata(&card->vdev->dev, card); dev_info(&card->vdev->dev, "PIO2 (variant %s) configured at 0x%lx\n", card->variant, card->base); return 0; err_led: pio2_gpio_exit(card); err_gpio: i = 6; err_cntr_irq: while (i > 0) { i--; vec = card->irq_vector | PIO2_VECTOR_CNTR[i]; vme_irq_free(vdev, card->irq_level, vec); } i = 4; err_gpio_irq: while (i > 0) { i--; vec = card->irq_vector | PIO2_VECTOR_BANK[i]; vme_irq_free(vdev, card->irq_level, vec); } vec = (card->irq_vector & PIO2_VME_VECTOR_MASK) | PIO2_VME_VECTOR_SPUR; vme_irq_free(vdev, card->irq_level, vec); err_irq: pio2_reset_card(card); err_reset: err_read: vme_master_set(card->window, 0, 0, 0, VME_A16, 0, VME_D16); err_set: vme_master_free(card->window); err_window: err_vector: err_variant: kfree(card); err_struct: return retval; }
/* * In this simple access driver, the old behaviour is being preserved as much * as practical. We will therefore reserve the buffers and request the images * here so that we don't have to do it later. */ static int __init vme_user_probe(struct device *dev, int cur_bus, int cur_slot) { int i, err; char name[8]; /* Save pointer to the bridge device */ if (vme_user_bridge != NULL) { printk(KERN_ERR "%s: Driver can only be loaded for 1 device\n", driver_name); err = -EINVAL; goto err_dev; } vme_user_bridge = dev; /* Initialise descriptors */ for (i = 0; i < VME_DEVS; i++) { image[i].kern_buf = NULL; image[i].pci_buf = 0; init_MUTEX(&(image[i].sem)); image[i].device = NULL; image[i].resource = NULL; image[i].users = 0; } /* Initialise statistics counters */ reset_counters(); /* Assign major and minor numbers for the driver */ err = register_chrdev_region(MKDEV(VME_MAJOR, 0), VME_DEVS, driver_name); if (err) { printk(KERN_WARNING "%s: Error getting Major Number %d for " "driver.\n", driver_name, VME_MAJOR); goto err_region; } /* Register the driver as a char device */ vme_user_cdev = cdev_alloc(); vme_user_cdev->ops = &vme_user_fops; vme_user_cdev->owner = THIS_MODULE; err = cdev_add(vme_user_cdev, MKDEV(VME_MAJOR, 0), VME_DEVS); if (err) { printk(KERN_WARNING "%s: cdev_all failed\n", driver_name); goto err_char; } /* Request slave resources and allocate buffers (128kB wide) */ for (i = SLAVE_MINOR; i < (SLAVE_MAX + 1); i++) { /* XXX Need to properly request attributes */ image[i].resource = vme_slave_request(vme_user_bridge, VME_A16, VME_SCT); if (image[i].resource == NULL) { printk(KERN_WARNING "Unable to allocate slave " "resource\n"); goto err_slave; } image[i].size_buf = PCI_BUF_SIZE; image[i].kern_buf = vme_alloc_consistent(image[i].resource, image[i].size_buf, &(image[i].pci_buf)); if (image[i].kern_buf == NULL) { printk(KERN_WARNING "Unable to allocate memory for " "buffer\n"); image[i].pci_buf = 0; vme_slave_free(image[i].resource); err = -ENOMEM; goto err_slave; } } /* * Request master resources allocate page sized buffers for small * reads and writes */ for (i = MASTER_MINOR; i < (MASTER_MAX + 1); i++) { /* XXX Need to properly request attributes */ image[i].resource = vme_master_request(vme_user_bridge, VME_A32, VME_SCT, VME_D32); if (image[i].resource == NULL) { printk(KERN_WARNING "Unable to allocate master " "resource\n"); goto err_master; } } /* Create sysfs entries - on udev systems this creates the dev files */ vme_user_sysfs_class = class_create(THIS_MODULE, driver_name); if (IS_ERR(vme_user_sysfs_class)) { printk(KERN_ERR "Error creating vme_user class.\n"); err = PTR_ERR(vme_user_sysfs_class); goto err_class; } /* Add sysfs Entries */ for (i=0; i<VME_DEVS; i++) { switch (type[i]) { case MASTER_MINOR: sprintf(name,"bus/vme/m%%d"); break; case CONTROL_MINOR: sprintf(name,"bus/vme/ctl"); break; case SLAVE_MINOR: sprintf(name,"bus/vme/s%%d"); break; default: err = -EINVAL; goto err_sysfs; break; } image[i].device = device_create(vme_user_sysfs_class, NULL, MKDEV(VME_MAJOR, i), NULL, name, (type[i] == SLAVE_MINOR)? i - (MASTER_MAX + 1) : i); if (IS_ERR(image[i].device)) { printk("%s: Error creating sysfs device\n", driver_name); err = PTR_ERR(image[i].device); goto err_sysfs; } } return 0; /* Ensure counter set correcty to destroy all sysfs devices */ i = VME_DEVS; err_sysfs: while (i > 0){ i--; device_destroy(vme_user_sysfs_class, MKDEV(VME_MAJOR, i)); } class_destroy(vme_user_sysfs_class); /* Ensure counter set correcty to unalloc all master windows */ i = MASTER_MAX + 1; err_master: while (i > MASTER_MINOR) { i--; vme_master_free(image[i].resource); } /* * Ensure counter set correcty to unalloc all slave windows and buffers */ i = SLAVE_MAX + 1; err_slave: while (i > SLAVE_MINOR) { i--; vme_slave_free(image[i].resource); buf_unalloc(i); } err_class: cdev_del(vme_user_cdev); err_char: unregister_chrdev_region(MKDEV(VME_MAJOR, 0), VME_DEVS); err_region: err_dev: return err; }