/** * led_classdev_register - register a new object of led_classdev class. * @dev: The device to register. * @led_cdev: the led_classdev structure for this device. */ int led_classdev_register(struct device *parent, struct led_classdev *led_cdev) { led_cdev->class_dev = class_device_create(leds_class, NULL, 0, parent, "%s", led_cdev->name); if (unlikely(IS_ERR(led_cdev->class_dev))) return PTR_ERR(led_cdev->class_dev); class_set_devdata(led_cdev->class_dev, led_cdev); /* register the attributes */ class_device_create_file(led_cdev->class_dev, &class_device_attr_brightness); /* add to the list of leds */ write_lock(&leds_list_lock); list_add_tail(&led_cdev->node, &leds_list); write_unlock(&leds_list_lock); #ifdef CONFIG_LEDS_TRIGGERS rwlock_init(&led_cdev->trigger_lock); led_trigger_set_default(led_cdev); class_device_create_file(led_cdev->class_dev, &class_device_attr_trigger); #endif printk(KERN_INFO "Registered led device: %s\n", led_cdev->class_dev->class_id); return 0; }
void switch_dev_unregister(struct switch_dev *sdev) { class_device_remove_file(sdev->cdev, &class_device_attr_name); class_device_remove_file(sdev->cdev, &class_device_attr_state); class_device_destroy(switch_class, MKDEV(0, sdev->index)); class_set_devdata(sdev->cdev, NULL); }
int switch_dev_register(struct switch_dev *sdev) { int ret; if (!switch_class) { ret = create_switch_class(); if (ret < 0) return ret; } sdev->index = atomic_inc_return(&device_count); sdev->cdev = class_device_create(switch_class, NULL, MKDEV(0, sdev->index), NULL, sdev->name); if (IS_ERR(sdev->cdev)) return PTR_ERR(sdev->cdev); ret = class_device_create_file(sdev->cdev, &class_device_attr_state); if (ret < 0) goto err_create_file_1; ret = class_device_create_file(sdev->cdev, &class_device_attr_name); if (ret < 0) goto err_create_file_2; class_set_devdata(sdev->cdev, sdev); sdev->state = 0; return 0; err_create_file_2: class_device_remove_file(sdev->cdev, &class_device_attr_state); err_create_file_1: class_device_destroy(switch_class, MKDEV(0, sdev->index)); printk(KERN_ERR "switch: Failed to register driver %s\n", sdev->name); return ret; }
/** * * This function is called when installing the driver for a device * @param pdev Pointer to the PCI device * */ static int __devinit pcidriver_probe(struct pci_dev *pdev, const struct pci_device_id *id) { int err; int devno; pcidriver_privdata_t *privdata; int devid; /* At the moment there is no difference between these boards here, other than * printing a different message in the log. * * However, there is some difference in the interrupt handling functions. */ if ( (id->vendor == MPRACE1_VENDOR_ID) && (id->device == MPRACE1_DEVICE_ID)) { /* It is a mpRACE-1 */ mod_info( "Found mpRACE-1 at %s\n", dev_name(&pdev->dev)); /* Set bus master */ pci_set_master(pdev); } else if ((id->vendor == PCIXTEST_VENDOR_ID) && (id->device == PCIXTEST_DEVICE_ID)) { /* It is a PCI-X Test board */ mod_info( "Found PCI-X test board at %s\n", dev_name(&pdev->dev)); } else if ((id->vendor == PCIEPLDA_VENDOR_ID) && (id->device == PCIEPLDA_DEVICE_ID)) { /* It is a PCI-X Test board */ mod_info( "Found PCIe PLDA test board at %s\n", dev_name(&pdev->dev)); } else if ((id->vendor == PCIEABB_VENDOR_ID) && (id->device == PCIEABB_DEVICE_ID)) { /* It is a PCI-X Test board */ mod_info( "Found PCIe ABB test board at %s\n", dev_name(&pdev->dev)); } else if ((id->vendor == PCIXPG4_VENDOR_ID) && (id->device == PCIXPG4_DEVICE_ID)) { /* It is a PCI-X PROGRAPE4 board */ mod_info( "Found PCI-X PROGRAPE-4 board at %s\n", dev_name(&pdev->dev)); } else if ((id->vendor == PCI64PG4_VENDOR_ID) && (id->device == PCI64PG4_DEVICE_ID)) { /* It is a PCI-64 PROGRAPE4 board */ mod_info( "Found PCI-64b/66 PROGRAPE-4 board at %s\n", dev_name(&pdev->dev)); } else if ((id->vendor == PCIE_XILINX_VENDOR_ID) && (id->device == PCIE_ML605_DEVICE_ID)) { /* It is a PCI-E Xilinx ML605 evaluation board */ mod_info("Found ML605 board at %s\n", dev_name(&pdev->dev)); } else { /* It is something else */ mod_info( "Found unknown board (%x:%x) at %s\n", id->vendor, id->device, dev_name(&pdev->dev)); } /* Enable the device */ if ((err = pci_enable_device(pdev)) != 0) { mod_info("Couldn't enable device\n"); goto probe_pcien_fail; } /* Set Memory-Write-Invalidate support */ if ((err = pci_set_mwi(pdev)) != 0) mod_info("MWI not supported. Continue without enabling MWI.\n"); /* Get / Increment the device id */ devid = atomic_inc_return(&pcidriver_deviceCount) - 1; if (devid >= MAXDEVICES) { mod_info("Maximum number of devices reached! Increase MAXDEVICES.\n"); err = -ENOMSG; goto probe_maxdevices_fail; } /* Allocate and initialize the private data for this device */ if ((privdata = kcalloc(1, sizeof(*privdata), GFP_KERNEL)) == NULL) { err = -ENOMEM; goto probe_nomem; } INIT_LIST_HEAD(&(privdata->kmem_list)); spin_lock_init(&(privdata->kmemlist_lock)); atomic_set(&privdata->kmem_count, 0); INIT_LIST_HEAD(&(privdata->umem_list)); spin_lock_init(&(privdata->umemlist_lock)); atomic_set(&privdata->umem_count, 0); pci_set_drvdata( pdev, privdata ); privdata->pdev = pdev; /* Device add to sysfs */ devno = MKDEV(MAJOR(pcidriver_devt), MINOR(pcidriver_devt) + devid); privdata->devno = devno; if (pcidriver_class != NULL) { /* FIXME: some error checking missing here */ privdata->class_dev = class_device_create(pcidriver_class, NULL, devno, &(pdev->dev), NODENAMEFMT, MINOR(pcidriver_devt) + devid, privdata); class_set_devdata( privdata->class_dev, privdata ); mod_info("Device /dev/%s%d added\n",NODENAME,MINOR(pcidriver_devt) + devid); } /* Setup mmaped BARs into kernel space */ if ((err = pcidriver_probe_irq(privdata)) != 0) goto probe_irq_probe_fail; /* Populate sysfs attributes for the class device */ /* TODO: correct errorhandling. ewww. must remove the files in reversed order :-( */ #define sysfs_attr(name) do { \ if (class_device_create_file(sysfs_attr_def_pointer, &sysfs_attr_def_name(name)) != 0) \ goto probe_device_create_fail; \ } while (0) #ifdef ENABLE_IRQ sysfs_attr(irq_count); sysfs_attr(irq_queues); #endif sysfs_attr(mmap_mode); sysfs_attr(mmap_area); sysfs_attr(kmem_count); sysfs_attr(kmem_alloc); sysfs_attr(kmem_free); sysfs_attr(kbuffers); sysfs_attr(umappings); sysfs_attr(umem_unmap); #undef sysfs_attr /* Register character device */ cdev_init( &(privdata->cdev), &pcidriver_fops ); #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,35) privdata->cdev.owner = THIS_MODULE; #endif privdata->cdev.ops = &pcidriver_fops; err = cdev_add( &privdata->cdev, devno, 1 ); if (err) { mod_info( "Couldn't add character device.\n" ); goto probe_cdevadd_fail; } return 0; probe_device_create_fail: probe_cdevadd_fail: probe_irq_probe_fail: pcidriver_irq_unmap_bars(privdata); kfree(privdata); probe_nomem: atomic_dec(&pcidriver_deviceCount); probe_maxdevices_fail: pci_disable_device(pdev); probe_pcien_fail: return err; }
static int StmMonitorProbe(struct device *dev) { int Result; int i; struct platform_device *MonitorDeviceData; unsigned int *Timer; unsigned int TimerPhysical; MonitorDeviceData = to_platform_device(dev); if (!MonitorDeviceData) { MONITOR_ERROR("%s: Device probe failed. Check your kernel SoC config!!\n", __FUNCTION__); return -ENODEV; } ModuleContext = kzalloc (sizeof (struct ModuleContext_s), GFP_KERNEL); if (ModuleContext == NULL) { MONITOR_ERROR("Unable to allocate device memory\n"); return -ENOMEM; } TimerPhysical = platform_get_resource(MonitorDeviceData, IORESOURCE_MEM, 0)->start; Timer = ioremap(TimerPhysical,0x4); mutex_init (&(ModuleContext->Lock)); mutex_lock (&(ModuleContext->Lock)); Result = alloc_chrdev_region (&FirstDevice, 0, MONITOR_MAX_DEVICES, DEVICE_NAME); if (Result < 0) { printk (KERN_ERR "%s: unable to allocate device numbers\n",__FUNCTION__); return -ENODEV; } ModuleContext->DeviceClass = class_create (THIS_MODULE, DEVICE_NAME); if (IS_ERR(ModuleContext->DeviceClass)) { printk (KERN_ERR "%s: unable to create device class\n",__FUNCTION__); ModuleContext->DeviceClass = NULL; return -ENODEV; } for (i = 0; i < MONITOR_MAX_DEVICES; i++) { struct DeviceContext_s* DeviceContext = &ModuleContext->DeviceContext[i]; int DevNo = MKDEV(MAJOR(FirstDevice), i); struct file_operations* FileOps; DeviceContext->TimerPhysical = TimerPhysical; DeviceContext->Timer = Timer; FileOps = MonitorInit (DeviceContext); DeviceContext->ModuleContext = ModuleContext; cdev_init (&(DeviceContext->CDev), FileOps); DeviceContext->CDev.owner = THIS_MODULE; kobject_set_name (&(DeviceContext->CDev.kobj), "%s%d", DEVICE_NAME, i); Result = cdev_add (&(DeviceContext->CDev), DevNo, 1); if (Result != 0) { printk (KERN_ERR "%s: unable to add device\n",__FUNCTION__); return -ENODEV; } #if defined(__TDT__) && (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30)) DeviceContext->ClassDevice = device_create (ModuleContext->DeviceClass, NULL, DeviceContext->CDev.dev, NULL, kobject_name (&(DeviceContext->CDev.kobj))); #else DeviceContext->ClassDevice = class_device_create (ModuleContext->DeviceClass, NULL, DeviceContext->CDev.dev, NULL, kobject_name (&(DeviceContext->CDev.kobj))); #endif if (IS_ERR(DeviceContext->ClassDevice)) { printk (KERN_ERR "%s: unable to create class device\n",__FUNCTION__); DeviceContext->ClassDevice = NULL; return -ENODEV; } #if defined(__TDT__) && (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30)) class_set_devdata (DeviceContext->ClassDevice, DeviceContext); #endif } mutex_unlock (&(ModuleContext->Lock)); MONITOR_DEBUG("STM monitor device loaded\n"); return 0; }
static int __devinit omap_rtc_probe(struct platform_device *pdev) { struct resource *res, *mem; struct rtc_device *rtc; u8 reg, new_ctrl; omap_rtc_timer = platform_get_irq(pdev, 0); if (omap_rtc_timer <= 0) { pr_debug("%s: no update irq?\n", pdev->name); return -ENOENT; } omap_rtc_alarm = platform_get_irq(pdev, 1); if (omap_rtc_alarm <= 0) { pr_debug("%s: no alarm irq?\n", pdev->name); return -ENOENT; } /* NOTE: using static mapping for RTC registers */ res = platform_get_resource(pdev, IORESOURCE_MEM, 0); if (res && res->start != OMAP_RTC_BASE) { pr_debug("%s: RTC registers at %08x, expected %08x\n", pdev->name, (unsigned) res->start, OMAP_RTC_BASE); return -ENOENT; } if (res) mem = request_mem_region(res->start, res->end - res->start + 1, pdev->name); else mem = NULL; if (!mem) { pr_debug("%s: RTC registers at %08x are not free\n", pdev->name, OMAP_RTC_BASE); return -EBUSY; } rtc = rtc_device_register(pdev->name, &pdev->dev, &omap_rtc_ops, THIS_MODULE); if (IS_ERR(rtc)) { pr_debug("%s: can't register RTC device, err %ld\n", pdev->name, PTR_ERR(rtc)); goto fail; } platform_set_drvdata(pdev, rtc); class_set_devdata(&rtc->class_dev, mem); /* clear pending irqs, and set 1/second periodic, * which we'll use instead of update irqs */ rtc_write(0, OMAP_RTC_INTERRUPTS_REG); /* clear old status */ reg = rtc_read(OMAP_RTC_STATUS_REG); if (reg & (u8) OMAP_RTC_STATUS_POWER_UP) { pr_info("%s: RTC power up reset detected\n", pdev->name); rtc_write(OMAP_RTC_STATUS_POWER_UP, OMAP_RTC_STATUS_REG); } if (reg & (u8) OMAP_RTC_STATUS_ALARM) rtc_write(OMAP_RTC_STATUS_ALARM, OMAP_RTC_STATUS_REG); /* handle periodic and alarm irqs */ if (request_irq(omap_rtc_timer, rtc_irq, SA_INTERRUPT, rtc->class_dev.class_id, &rtc->class_dev)) { pr_debug("%s: RTC timer interrupt IRQ%d already claimed\n", pdev->name, omap_rtc_timer); goto fail0; } if (request_irq(omap_rtc_alarm, rtc_irq, SA_INTERRUPT, rtc->class_dev.class_id, &rtc->class_dev)) { pr_debug("%s: RTC alarm interrupt IRQ%d already claimed\n", pdev->name, omap_rtc_alarm); goto fail1; } /* On boards with split power, RTC_ON_NOFF won't reset the RTC */ reg = rtc_read(OMAP_RTC_CTRL_REG); if (reg & (u8) OMAP_RTC_CTRL_STOP) pr_info("%s: already running\n", pdev->name); /* force to 24 hour mode */ new_ctrl = reg & ~(OMAP_RTC_CTRL_SPLIT|OMAP_RTC_CTRL_AUTO_COMP); new_ctrl |= OMAP_RTC_CTRL_STOP; /* BOARD-SPECIFIC CUSTOMIZATION CAN GO HERE: * * - Boards wired so that RTC_WAKE_INT does something, and muxed * right (W13_1610_RTC_WAKE_INT is the default after chip reset), * should initialize the device wakeup flag appropriately. * * - Boards wired so RTC_ON_nOFF is used as the reset signal, * rather than nPWRON_RESET, should forcibly enable split * power mode. (Some chip errata report that RTC_CTRL_SPLIT * is write-only, and always reads as zero...) */ device_init_wakeup(&pdev->dev, 0); if (new_ctrl & (u8) OMAP_RTC_CTRL_SPLIT) pr_info("%s: split power mode\n", pdev->name); if (reg != new_ctrl) rtc_write(new_ctrl, OMAP_RTC_CTRL_REG); return 0; fail1: free_irq(omap_rtc_timer, NULL); fail0: rtc_device_unregister(rtc); fail: release_resource(mem); return -EIO; }
static int __devinit goldfish_switch_probe(struct platform_device *pdev) { int ret; struct resource *r; struct goldfish_switch *qs; uint32_t base; uint32_t name_len; r = platform_get_resource(pdev, IORESOURCE_MEM, 0); if(r == NULL) { ret = -ENODEV; goto err_no_io_base; } base = IO_ADDRESS(r->start - IO_START); name_len = readl(base + SW_NAME_LEN); qs = kzalloc(sizeof(*qs) + name_len + 1, GFP_KERNEL); if(qs == NULL) { ret = -ENOMEM; goto err_qs_alloc_failed; } platform_set_drvdata(pdev, qs); qs->base = base; r = platform_get_resource(pdev, IORESOURCE_IRQ, 0); if(r == NULL) { ret = -ENODEV; goto err_no_irq; } qs->irq = r->start; writel(qs->name, base + SW_NAME_PTR); qs->name[name_len] = '\0'; writel(0, base + SW_INT_ENABLE); qs->flags = readl(base + SW_FLAGS); qs->state = readl(base + SW_STATE); INIT_WORK(&qs->work, goldfish_switch_work); qs->cdev = class_device_create(goldfish_switch_class, NULL, 0, &pdev->dev, "%s", qs->name); if(unlikely(IS_ERR(qs->cdev))) { ret = PTR_ERR(qs->cdev); goto err_class_device_create_failed; } class_set_devdata(qs->cdev, qs); ret = class_device_create_file(qs->cdev, &class_device_attr_state); if(ret) goto err_class_device_create_file_failed; ret = class_device_create_file(qs->cdev, &class_device_attr_direction); if(ret) goto err_class_device_create_file_failed; ret = request_irq(qs->irq, goldfish_switch_interrupt, IRQF_SHARED, "goldfish_switch", qs); if(ret) goto err_request_irq_failed; writel(1, base + SW_INT_ENABLE); return 0; // free_irq(qs->irq, qs); err_request_irq_failed: err_class_device_create_file_failed: class_device_unregister(qs->cdev); err_class_device_create_failed: err_no_irq: kfree(qs); err_qs_alloc_failed: err_no_io_base: printk("goldfish_switch_probe failed %d\n", ret); return ret; }