static int WIFI_init(void) { dev_t dev = MKDEV(WIFI_major, 0); INT32 alloc_ret = 0; INT32 cdev_err = 0; #if WMT_CREATE_NODE_DYNAMIC struct device * wmtwifi_dev = NULL; #endif /* static allocate chrdev */ alloc_ret = register_chrdev_region(dev, 1, WIFI_DRIVER_NAME); if (alloc_ret) { WIFI_ERR_FUNC("Fail to register chrdev\n"); return alloc_ret; } cdev_init(&WIFI_cdev, &WIFI_fops); WIFI_cdev.owner = THIS_MODULE; cdev_err = cdev_add(&WIFI_cdev, dev, WIFI_devs); if (cdev_err) { goto error; } #if WMT_CREATE_NODE_DYNAMIC //mknod replace wmtwifi_class = class_create(THIS_MODULE,"wmtWifi"); if(IS_ERR(wmtwifi_class)) goto error; wmtwifi_dev = device_create(wmtwifi_class,NULL,dev,NULL,"wmtWifi"); if(IS_ERR(wmtwifi_dev)) goto error; #endif sema_init(&wr_mtx, 1); WIFI_INFO_FUNC("%s driver(major %d) installed.\n", WIFI_DRIVER_NAME, WIFI_major); retflag = 0; wlan_mode = WLAN_MODE_HALT; pf_set_p2p_mode = NULL; return 0; error: #if WMT_CREATE_NODE_DYNAMIC if(!IS_ERR(wmtwifi_dev)) device_destroy(wmtwifi_class,dev); if(!IS_ERR(wmtwifi_class)){ class_destroy(wmtwifi_class); wmtwifi_class = NULL; } #endif if (cdev_err == 0) { cdev_del(&WIFI_cdev); } if (alloc_ret == 0) { unregister_chrdev_region(dev, WIFI_devs); } return -1; }
static int __init bt_hwctl_init(void) { int ret = -1, err = -1; BT_HWCTL_DEBUG("bt_hwctl_init\n"); platform_driver_register(&mt6622_driver); if (!(bh = kzalloc(sizeof(struct bt_hwctl), GFP_KERNEL))) { BT_HWCTL_ALERT("bt_hwctl_init allocate dev struct failed\n"); err = -ENOMEM; goto ERR_EXIT; } ret = alloc_chrdev_region(&bh->dev_t, 0, 1, BTHWCTL_NAME); if (ret) { BT_HWCTL_ALERT("alloc chrdev region failed\n"); goto ERR_EXIT; } BT_HWCTL_DEBUG("alloc %s:%d:%d\n", BTHWCTL_NAME, MAJOR(bh->dev_t), MINOR(bh->dev_t)); cdev_init(&bh->cdev, &bt_hwctl_fops); bh->cdev.owner = THIS_MODULE; bh->cdev.ops = &bt_hwctl_fops; err = cdev_add(&bh->cdev, bh->dev_t, 1); if (err) { BT_HWCTL_ALERT("add chrdev failed\n"); goto ERR_EXIT; } bh->cls = class_create(THIS_MODULE, BTHWCTL_NAME); if (IS_ERR(bh->cls)) { err = PTR_ERR(bh->cls); BT_HWCTL_ALERT("class_create failed, errno:%d\n", err); goto ERR_EXIT; } bh->dev = device_create(bh->cls, NULL, bh->dev_t, NULL, BTHWCTL_NAME); mutex_init(&bh->sem); init_waitqueue_head(&eint_wait); wake_lock_init(&mt6622_irq_wakelock, WAKE_LOCK_SUSPEND, "mt6622_irq_wakelock"); /*INIT_WORK(&mtk_wcn_bt_event_work, mtk_wcn_bt_work_fun); mtk_wcn_bt_workqueue = create_singlethread_workqueue("mtk_wcn_bt"); if (!mtk_wcn_bt_workqueue) { printk("create_singlethread_workqueue failed.\n"); err = -ESRCH; goto ERR_EXIT; }*/ /* request gpio used by BT */ //mt_bt_gpio_init(); BT_HWCTL_DEBUG("bt_hwctl_init ok\n"); return 0; ERR_EXIT: if (err == 0) cdev_del(&bh->cdev); if (ret == 0) unregister_chrdev_region(bh->dev_t, 1); if (bh){ kfree(bh); bh = NULL; } return -1; }
static int init_char_device(void) { unsigned int i,ret; char_device_buf_minor0 = (char *)kmalloc(MAX_LENGTH*sizeof(char), GFP_KERNEL); char_device_buf_minor1 = (char *)kmalloc(MAX_LENGTH*sizeof(char), GFP_KERNEL); char_device_buf_minor2 = (char *)kmalloc(MAX_LENGTH*sizeof(char), GFP_KERNEL); if( !char_device_buf_minor0 ) return -ENOSPC; if( !char_device_buf_minor1 ) return -ENOSPC; if( !char_device_buf_minor2 ) return -ENOSPC; char_device_file_ops.owner = THIS_MODULE, char_device_file_ops.read = char_device_read; char_device_file_ops.read = char_device_read; char_device_file_ops.write = char_device_write; char_device_file_ops.open = char_device_open; char_device_file_ops.release = char_device_release; ret=alloc_chrdev_region(&mydev,0,3,"veda_cdrv"); char_device_id= MAJOR(mydev);//extract major no /* Let's Start Udev stuff */ #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14) veda_class = class_simple_create(THIS_MODULE,"Veda"); if(IS_ERR(veda_class)){ printk(KERN_ERR "Error registering veda class\n"); } for(i=0;i<3;i++) class_simple_device_add(veda_class,MKDEV(char_device_id,i),NULL,"veda_cdrv",i); #endif #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14) veda_class = class_create(THIS_MODULE,"Veda"); if(IS_ERR(veda_class)){ printk(KERN_ERR "Error registering veda class\n"); } for(i=0;i<3;i++) device_create(veda_class,NULL,MKDEV(char_device_id,i),"veda_cdrv"); #endif /*Register our character Device*/ veda_cdev= cdev_alloc(); veda_cdev->owner=THIS_MODULE; veda_cdev->ops= &char_device_file_ops; for(i=0;i<3;i++) ret=cdev_add(veda_cdev,MKDEV(char_device_id,i),3); if( ret < 0 ) { printk("Error registering device driver\n"); return ret; } printk("Device Registered with MAJOR NO[%d]\n",char_device_id); for(i=0; i<MAX_LENGTH; i++) char_device_buf_minor0[i] = 0; for(i=0; i<MAX_LENGTH; i++) char_device_buf_minor1[i] = 0; for(i=0; i<MAX_LENGTH; i++) char_device_buf_minor2[i] = 0; init_waitqueue_head( &veda_queue0 ); init_waitqueue_head( &veda_queue1 ); init_waitqueue_head( &veda_queue2 ); mutex_init(&vmut0); mutex_init(&vmut1); mutex_init(&vmut2); return 0; }
static int __devinit asuspec_probe(struct i2c_client *client, const struct i2c_device_id *id) { int err = 0; ASUSPEC_INFO("asuspec probe\n"); err = sysfs_create_group(&client->dev.kobj, &asuspec_smbus_group); if (err) { ASUSPEC_ERR("Unable to create the sysfs\n"); goto exit; } ec_chip = kzalloc(sizeof (struct asuspec_chip), GFP_KERNEL); if (!ec_chip) { ASUSPEC_ERR("Memory allocation fails\n"); err = -ENOMEM; goto exit; } ec_chip->pad_pid = tegra3_get_project_id(); i2c_set_clientdata(client, ec_chip); ec_chip->client = client; ec_chip->client->driver = &asuspec_driver; ec_chip->client->flags = 1; init_timer(&ec_chip->asuspec_timer); ec_chip->asuspec_timer.function = asuspec_enter_s3_timer; wake_lock_init(&ec_chip->wake_lock, WAKE_LOCK_SUSPEND, "asuspec_wake"); mutex_init(&ec_chip->lock); mutex_init(&ec_chip->irq_lock); mutex_init(&ec_chip->state_change_lock); ec_chip->ec_ram_init = 0; ec_chip->audio_recording = 0; ec_chip->status = 0; ec_chip->ec_in_s3 = 0; ec_chip->apwake_disabled = 0; ec_chip->storage_total = 0; ec_chip->storage_avail = 0; asuspec_dockram_init(client); cdev_add(asuspec_cdev,asuspec_dev,1) ; ec_chip->pad_sdev.name = PAD_SDEV_NAME; ec_chip->pad_sdev.print_name = asuspec_switch_name; ec_chip->pad_sdev.print_state = asuspec_switch_state; if(switch_dev_register(&ec_chip->pad_sdev) < 0){ ASUSPEC_ERR("switch_dev_register for pad failed!\n"); } switch_set_state(&ec_chip->pad_sdev, 0); ec_chip->apower_sdev.name = APOWER_SDEV_NAME; ec_chip->apower_sdev.print_name = apower_switch_name; ec_chip->apower_sdev.print_state = apower_switch_state; ec_chip->apower_state = 0; if(switch_dev_register(&ec_chip->apower_sdev) < 0){ ASUSPEC_ERR("switch_dev_register for apower failed!\n"); } switch_set_state(&ec_chip->apower_sdev, ec_chip->apower_state); asuspec_wq = create_singlethread_workqueue("asuspec_wq"); INIT_DELAYED_WORK_DEFERRABLE(&ec_chip->asuspec_work, asuspec_work_function); INIT_DELAYED_WORK_DEFERRABLE(&ec_chip->asuspec_init_work, asuspec_init_work_function); INIT_DELAYED_WORK_DEFERRABLE(&ec_chip->asuspec_fw_update_work, asuspec_fw_update_work_function); INIT_DELAYED_WORK_DEFERRABLE(&ec_chip->asuspec_enter_s3_work, asuspec_enter_s3_work_function); INIT_DELAYED_WORK_DEFERRABLE(&asuspec_stress_work, asuspec_stresstest_work_function); asuspec_irq_ec_request(client); asuspec_irq_ec_apwake(client); queue_delayed_work(asuspec_wq, &ec_chip->asuspec_init_work, 0); return 0; exit: return err; }
static void __init unix98_pty_init(void) { ptm_driver = tty_alloc_driver(NR_UNIX98_PTY_MAX, TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM | TTY_DRIVER_DYNAMIC_ALLOC); if (IS_ERR(ptm_driver)) panic("Couldn't allocate Unix98 ptm driver"); pts_driver = tty_alloc_driver(NR_UNIX98_PTY_MAX, TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM | TTY_DRIVER_DYNAMIC_ALLOC); if (IS_ERR(pts_driver)) panic("Couldn't allocate Unix98 pts driver"); ptm_driver->driver_name = "pty_master"; ptm_driver->name = "ptm"; ptm_driver->major = UNIX98_PTY_MASTER_MAJOR; ptm_driver->minor_start = 0; ptm_driver->type = TTY_DRIVER_TYPE_PTY; ptm_driver->subtype = PTY_TYPE_MASTER; ptm_driver->init_termios = tty_std_termios; ptm_driver->init_termios.c_iflag = 0; ptm_driver->init_termios.c_oflag = 0; ptm_driver->init_termios.c_cflag = B38400 | CS8 | CREAD; ptm_driver->init_termios.c_lflag = 0; ptm_driver->init_termios.c_ispeed = 38400; ptm_driver->init_termios.c_ospeed = 38400; ptm_driver->other = pts_driver; tty_set_operations(ptm_driver, &ptm_unix98_ops); pts_driver->driver_name = "pty_slave"; pts_driver->name = "pts"; pts_driver->major = UNIX98_PTY_SLAVE_MAJOR; pts_driver->minor_start = 0; pts_driver->type = TTY_DRIVER_TYPE_PTY; pts_driver->subtype = PTY_TYPE_SLAVE; pts_driver->init_termios = tty_std_termios; pts_driver->init_termios.c_cflag = B38400 | CS8 | CREAD; pts_driver->init_termios.c_ispeed = 38400; pts_driver->init_termios.c_ospeed = 38400; pts_driver->other = ptm_driver; tty_set_operations(pts_driver, &pty_unix98_ops); if (tty_register_driver(ptm_driver)) panic("Couldn't register Unix98 ptm driver"); if (tty_register_driver(pts_driver)) panic("Couldn't register Unix98 pts driver"); /* Now create the /dev/ptmx special device */ tty_default_fops(&ptmx_fops); ptmx_fops.open = ptmx_open; cdev_init(&ptmx_cdev, &ptmx_fops); if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) || register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0) panic("Couldn't register /dev/ptmx driver"); device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx"); }
static int frandom_init_module(void) { int result; if (frandom_bufsize < 256) { printk(KERN_ERR "frandom: Refused to load because frandom_bufsize=%d < 256\n",frandom_bufsize); return -EINVAL; } if ((frandom_chunklimit != 0) && (frandom_chunklimit < 256)) { printk(KERN_ERR "frandom: Refused to load because frandom_chunklimit=%d < 256 and != 0\n",frandom_chunklimit); return -EINVAL; } erandom_state = kmalloc(sizeof(struct frandom_state), GFP_KERNEL); if (!erandom_state) return -ENOMEM; erandom_state->buf = kmalloc(256, GFP_KERNEL); if (!erandom_state->buf) { kfree(erandom_state); return -ENOMEM; } sema_init(&erandom_state->sem, 1); erandom_seeded = 0; frandom_class = class_create(THIS_MODULE, "fastrng"); if (IS_ERR(frandom_class)) { result = PTR_ERR(frandom_class); printk(KERN_WARNING "frandom: Failed to register class fastrng\n"); goto error0; } cdev_init(&frandom_cdev, &frandom_fops); frandom_cdev.owner = THIS_MODULE; result = cdev_add(&frandom_cdev, MKDEV(frandom_major, frandom_minor), 1); if (result) { printk(KERN_WARNING "frandom: Failed to add cdev for /dev/frandom\n"); goto error1; } result = register_chrdev_region(MKDEV(frandom_major, frandom_minor), 1, "/dev/frandom"); if (result < 0) { printk(KERN_WARNING "frandom: can't get major/minor %d/%d\n", frandom_major, frandom_minor); goto error2; } frandom_device = device_create(frandom_class, NULL, MKDEV(frandom_major, frandom_minor), NULL, "frandom"); if (IS_ERR(frandom_device)) { printk(KERN_WARNING "frandom: Failed to create frandom device\n"); goto error3; } cdev_init(&erandom_cdev, &frandom_fops); erandom_cdev.owner = THIS_MODULE; result = cdev_add(&erandom_cdev, MKDEV(frandom_major, erandom_minor), 1); if (result) { printk(KERN_WARNING "frandom: Failed to add cdev for /dev/erandom\n"); goto error4; } result = register_chrdev_region(MKDEV(frandom_major, erandom_minor), 1, "/dev/erandom"); if (result < 0) { printk(KERN_WARNING "frandom: can't get major/minor %d/%d\n", frandom_major, erandom_minor); goto error5; } erandom_device = device_create(frandom_class, NULL, MKDEV(frandom_major, erandom_minor), NULL, "erandom"); if (IS_ERR(erandom_device)) { printk(KERN_WARNING "frandom: Failed to create erandom device\n"); goto error6; } return 0; error6: unregister_chrdev_region(MKDEV(frandom_major, erandom_minor), 1); error5: cdev_del(&erandom_cdev); error4: device_destroy(frandom_class, MKDEV(frandom_major, frandom_minor)); error3: unregister_chrdev_region(MKDEV(frandom_major, frandom_minor), 1); error2: cdev_del(&frandom_cdev); error1: class_destroy(frandom_class); error0: kfree(erandom_state->buf); kfree(erandom_state); return result; }
static int WMT_init(void) { dev_t devID = MKDEV(gWmtMajor, 0); INT32 cdevErr = -1; INT32 ret = -1; #if REMOVE_MK_NODE struct device * wmt_dev = NULL; #endif WMT_INFO_FUNC("WMT Version= %s DATE=%s\n" , MTK_WMT_VERSION, MTK_WMT_DATE); WMT_INFO_FUNC("COMBO Driver Version= %s\n" , MTK_COMBO_DRIVER_VERSION); /* Prepare a UCHAR device */ /*static allocate chrdev*/ stp_drv_init(); ret = register_chrdev_region(devID, WMT_DEV_NUM, WMT_DRIVER_NAME); if (ret) { WMT_ERR_FUNC("fail to register chrdev\n"); return ret; } cdev_init(&gWmtCdev, &gWmtFops); gWmtCdev.owner = THIS_MODULE; cdevErr = cdev_add(&gWmtCdev, devID, WMT_DEV_NUM); if (cdevErr) { WMT_ERR_FUNC("cdev_add() fails (%d) \n", cdevErr); goto error; } WMT_INFO_FUNC("driver(major %d) installed \n", gWmtMajor); #if REMOVE_MK_NODE //mknod replace wmt_class = class_create(THIS_MODULE,"stpwmt"); if(IS_ERR(wmt_class)) goto error; wmt_dev = device_create(wmt_class,NULL,devID,NULL,"stpwmt"); if(IS_ERR(wmt_dev)) goto error; #endif #if 0 pWmtDevCtx = wmt_drv_create(); if (!pWmtDevCtx) { WMT_ERR_FUNC("wmt_drv_create() fails \n"); goto error; } ret = wmt_drv_init(pWmtDevCtx); if (ret) { WMT_ERR_FUNC("wmt_drv_init() fails (%d) \n", ret); goto error; } WMT_INFO_FUNC("stp_btmcb_reg\n"); wmt_cdev_btmcb_reg(); ret = wmt_drv_start(pWmtDevCtx); if (ret) { WMT_ERR_FUNC("wmt_drv_start() fails (%d) \n", ret); goto error; } #endif platform_driver_register(&wmt_platform_driver); ret = wmt_lib_init(); if (ret) { WMT_ERR_FUNC("wmt_lib_init() fails (%d) \n", ret); goto error; } #if CFG_WMT_DBG_SUPPORT wmt_dev_dbg_setup(); #endif #if defined(CONFIG_THERMAL) && defined(CONFIG_THERMAL_OPEN) WMT_INFO_FUNC("wmt_dev_tm_setup\n"); wmt_dev_tm_setup(); mtk_wcn_hif_sdio_update_cb_reg(wmt_dev_tra_sdio_update); #endif WMT_INFO_FUNC("success \n"); return 0; error: wmt_lib_deinit(); #if CFG_WMT_DBG_SUPPORT wmt_dev_dbg_remove(); #endif #if REMOVE_MK_NODE if(!IS_ERR(wmt_dev)) device_destroy(wmt_class,devID); if(!IS_ERR(wmt_class)){ class_destroy(wmt_class); wmt_class = NULL; } #endif if (cdevErr == 0) { cdev_del(&gWmtCdev); } if (ret == 0) { unregister_chrdev_region(devID, WMT_DEV_NUM); gWmtMajor = -1; } WMT_ERR_FUNC("fail \n"); return -1; }
static int spec_probe(struct pci_dev *pdev, const struct pci_device_id *id) { int i; int ret; int minor; struct spec_dev *dev; dev = kzalloc(sizeof(struct spec_dev), GFP_KERNEL); if (!dev) { dev_err(&pdev->dev, "failed to allocate spec_dev\n"); ret = -ENOMEM; goto err_alloc; } dev->dev = &pdev->dev; dev->pdev = pdev; pci_set_drvdata(pdev, dev); INIT_WORK(&dev->work, spec_load_firmware); init_waitqueue_head(&dev->irq_queue); cdev_init(&dev->cdev, &spec_file_ops); mutex_init(&dev->mutex); if (dmasize > SPEC_MAX_DMABUFSIZE) { pr_warning(KBUILD_MODNAME ": %s: DMA buffer size too big, using 0x%x\n", __func__, SPEC_MAX_DMABUFSIZE); dmasize = SPEC_MAX_DMABUFSIZE; } dev->dmabuf = __vmalloc(dmasize, GFP_KERNEL | __GFP_ZERO, PAGE_KERNEL); if (!dev->dmabuf) { ret = -ENOMEM; goto err_dmaalloc; } mutex_lock(&spec_minors_mutex); minor = spec_get_free(); if (minor == SPEC_MAX_MINORS) { dev_err(&pdev->dev, "too many devices!\n"); ret = -EIO; goto err_dis; } mutex_unlock(&spec_minors_mutex); if (pdev->irq > 0) { i = request_irq(pdev->irq, spec_irq_handler, IRQF_SHARED, driver_name, dev); if (i < 0) pr_err(KBUILD_MODNAME "can't request irq %i, error %i\n", pdev->irq, i); else dev->flags |= SPEC_FLAG_IRQREQUEST; } ret = pci_enable_device(pdev); if (ret < 0) { dev_err(&pdev->dev, "failed to enable SPEC device\n"); goto err_enable; } ret = pci_request_regions(pdev, driver_name); if (ret) { dev_err(&pdev->dev, "failed to request PCI resources\n"); goto err_resource; } /* Save BAR0, BAR2 and BAR4 and try to remap them */ for (i = 0; i < 3; i++) { struct resource *r = &pdev->resource[2*i]; if (!r->start) continue; dev->area[i] = r; if (r->flags & IORESOURCE_MEM) dev->remap[i] = ioremap(r->start, r->end - r->start + 1); if (SPEC_DEBUG) { pr_info(KBUILD_MODNAME ": %s: BAR%i: %llx-%llx (size: 0x%llx) - %08lx\n", __func__, i, (long long)r->start, (long long)r->end, (long long)(r->end - r->start + 1), r->flags); } } ret = cdev_add(&dev->cdev, MKDEV(spec_major, minor), 1); if (ret) { dev_err(&pdev->dev, "chardev registration failed\n"); goto err_cdev; } #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,29)) if (IS_ERR(device_create(spec_class, &pdev->dev, MKDEV(spec_major, minor), NULL, "spec%u", minor))) #else if (IS_ERR(device_create(spec_class, &pdev->dev, MKDEV(spec_major, minor), "spec%u", minor))) #endif dev_err(&pdev->dev, "can't create device\n"); if (fwname) spec_request_firmware(dev); return 0; err_cdev: for (i = 0; i < 3; i++) iounmap(dev->remap[i]); err_resource: pci_disable_device(pdev); err_enable: if (dev->flags & SPEC_FLAG_IRQREQUEST) free_irq(pdev->irq, dev); /* FIXME: Reenable if we are shared */ spec_minors[minor] = 0; mutex_unlock(&spec_minors_mutex); err_dis: kfree(dev->dmabuf); err_dmaalloc: kfree(dev); err_alloc: return ret; }
static int xordev_probe(struct pci_dev *dev, const struct pci_device_id *id) /* PCI device initializer */ { uint32_t i, j; int err = 0; struct xordev_str *xordev; dev_t devt; mutex_lock(&global_lock); /* find empty slot and initialize */ for (i = 0; xordev_data[i].devno != 0 && i < MAX_DEVICES; i++); if (i == MAX_DEVICES){ printk (KERN_NOTICE "Too many xordev devices!"); return -1; } xordev = &xordev_data[i]; memset(xordev, 0, sizeof(struct xordev_str)); /* clear everything */ xordev->devno = MKDEV(major, i * 3); mutex_init(&xordev->lock); xordev->spinlock = SPIN_LOCK_UNLOCKED; for (j = 0; j < 3; j++) { devt = MKDEV(major, i * 3 + j); /* creating char device */ cdev_init(&xordev->cdev[j], &xordev_fops); xordev->cdev[j].ops = &xordev_fops; err = cdev_add(&xordev->cdev[j], devt, 1); if (err) { printk(KERN_NOTICE "Error %d adding XorDev %d", err, i); goto fail; } /* udev device create */ xordev->dev[j] = device_create(xordev_class, 0, devt, xordev, dev_names[j], i); if (IS_ERR(xordev->dev[j])) { err = PTR_ERR(xordev->dev[j]); printk(KERN_NOTICE "Error %d creating udev device", err); goto udev_create_fail; } } pci_set_drvdata(dev, xordev); /* enable PCI device */ err = pci_enable_device(dev); if (err) { printk(KERN_NOTICE "Error %d enabling XorDev PCI device %d", err, i); goto pci_enable_fail; } err = pci_request_regions(dev, DRVNAME); if (err) { printk(KERN_NOTICE "Error %d requesting XorDev %d regions", err, i); goto pci_regions_fail; } xordev->bar = pci_iomap(dev, 0, 4096); if (NULL == xordev->bar) { printk(KERN_NOTICE "Can't map XorDev %d iomem", i); goto pci_iomap_fail; } /* enable DMA */ pci_set_master(dev); err = pci_set_dma_mask(dev, DMA_BIT_MASK(32)); if (err) { printk(KERN_NOTICE "Can't set DMA mask for XorDev %d", i); goto pci_iomap_fail; } err = pci_set_consistent_dma_mask(dev, DMA_BIT_MASK(32)); if (err) { printk(KERN_NOTICE "Can't set consistent DMA mask for XorDev %d", i); goto pci_iomap_fail; } /* alloc buffers */ xordev->cpu_addr[0] = dma_alloc_coherent(&dev->dev, BUFFER_SIZE * 3, &xordev->dma_addr[0], 0); if (xordev->cpu_addr) { xordev->cpu_addr[1] = xordev->cpu_addr[0] + BUFFER_SIZE; xordev->dma_addr[1] = xordev->dma_addr[0] + BUFFER_SIZE; xordev->cpu_addr[2] = xordev->cpu_addr[1] + BUFFER_SIZE; xordev->dma_addr[2] = xordev->dma_addr[1] + BUFFER_SIZE; memset(xordev->offset, 0, 12); iowrite32(xordev->dma_addr[0], xordev->bar + BAR_SRC1); iowrite32(xordev->dma_addr[1], xordev->bar + BAR_SRC2); iowrite32(xordev->dma_addr[2], xordev->bar + BAR_DST); } else { printk(KERN_NOTICE "Can't allocate DMA memory for XorDev %d", i); goto pci_iomap_fail; } /* waiting queue */ init_waitqueue_head(&xordev->queue); /* interruptions */ err = request_irq(dev->irq, xordev_irq, IRQF_SHARED, DRVNAME, xordev); if (err) { printk(KERN_NOTICE "Can't register irq handler for XorDev %d", i); goto request_irq_fail; } mutex_unlock(&global_lock); return 0; request_irq_fail: pci_iounmap(dev, xordev->bar); pci_iomap_fail: pci_release_regions(dev); pci_regions_fail: pci_disable_device(dev); pci_enable_fail: for (i = 0; i < j; i++) device_destroy(xordev_class, MKDEV(MAJOR(xordev->devno), MINOR(xordev->devno) + i)); udev_create_fail: for (i = 0; i < j; i++) cdev_del(&xordev->cdev[j]); fail: mutex_destroy(&xordev->mutex); xordev->devno = 0; mutex_unlock(&global_lock); return err; }
/* * First routine called when the kernel module is loaded */ static int __init tf_device_register(void) { int error; struct tf_device *dev = &g_tf_dev; struct tf_device_stats *dev_stats = &dev->stats; dprintk(KERN_INFO "tf_device_register()\n"); /* * Initialize the device */ dev->dev_number = MKDEV(device_major_number, TF_DEVICE_MINOR_NUMBER); cdev_init(&dev->cdev, &g_tf_device_file_ops); dev->cdev.owner = THIS_MODULE; INIT_LIST_HEAD(&dev->connection_list); spin_lock_init(&dev->connection_list_lock); /* register the sysfs object driver stats */ dev_stats->kobj_type.sysfs_ops = &kobj_sysfs_operations; dev_stats->kobj_stat_attribute.name = "info"; dev_stats->kobj_stat_attribute.mode = S_IRUGO; dev_stats->kobj_attribute_list[0] = &dev_stats->kobj_stat_attribute; dev_stats->kobj_type.default_attrs = dev_stats->kobj_attribute_list, error = kobject_init_and_add(&(dev_stats->kobj), &(dev_stats->kobj_type), NULL, "%s", TF_DEVICE_BASE_NAME); if (error) { kobject_put(&dev_stats->kobj); goto kobject_init_and_add_failed; } register_syscore_ops((struct syscore_ops *)&g_tf_syscore_ops); /* * Register the char device. */ printk(KERN_INFO "Registering char device %s (%u:%u)\n", TF_DEVICE_BASE_NAME, MAJOR(dev->dev_number), MINOR(dev->dev_number)); error = register_chrdev_region(dev->dev_number, 1, TF_DEVICE_BASE_NAME); if (error != 0) { printk(KERN_ERR "tf_device_register():" " register_chrdev_region failed (error %d)!\n", error); goto register_chrdev_region_failed; } error = cdev_add(&dev->cdev, dev->dev_number, 1); if (error != 0) { printk(KERN_ERR "tf_device_register(): " "cdev_add failed (error %d)!\n", error); goto cdev_add_failed; } /* * Initialize the communication with the Secure World. */ #ifdef CONFIG_TF_TRUSTZONE dev->sm.soft_int_irq = soft_interrupt; #endif error = tf_init(&g_tf_dev.sm); if (error != S_SUCCESS) { dprintk(KERN_ERR "tf_device_register(): " "tf_init failed (error %d)!\n", error); goto init_failed; } #ifdef CONFIG_ANDROID #ifdef CONFIG_TF_DRIVER_CRYPTO_FIPS error = tf_self_test_post_init(&(g_tf_dev.kobj)); /* N.B. error > 0 indicates a POST failure, which will not prevent the module from loading. */ if (error < 0) { dprintk(KERN_ERR "tf_device_register(): " "tf_self_test_post_vectors failed (error %d)!\n", error); goto post_failed; } #endif tf_class = class_create(THIS_MODULE, TF_DEVICE_BASE_NAME); device_create(tf_class, NULL, dev->dev_number, NULL, TF_DEVICE_BASE_NAME); #endif #ifdef CONFIG_TF_ZEBRA /* * Initializes the /dev/tf_ctrl device node. */ error = tf_ctrl_device_register(); if (error) goto init_failed; #endif #ifdef CONFIG_BENCH_SECURE_CYCLE run_bogo_mips(); address_cache_property((unsigned long) &tf_device_register); #endif /* * Successful completion. */ dprintk(KERN_INFO "tf_device_register(): Success\n"); return 0; /* * Error: undo all operations in the reverse order */ init_failed: cdev_del(&dev->cdev); cdev_add_failed: unregister_chrdev_region(dev->dev_number, 1); register_chrdev_region_failed: unregister_syscore_ops((struct syscore_ops *)&g_tf_syscore_ops); kobject_init_and_add_failed: kobject_del(&g_tf_dev.stats.kobj); dprintk(KERN_INFO "tf_device_register(): Failure (error %d)\n", error); return error; }
static int __init mic_init(void) { int ret, i; adapter_init(); unaligned_cache = micscif_kmem_cache_create(); if (!unaligned_cache) { ret = -ENOMEM; goto init_free_ports; } mic_lindata.dd_pcidriver.name = "mic"; mic_lindata.dd_pcidriver.id_table = mic_pci_tbl; mic_lindata.dd_pcidriver.probe = mic_probe; mic_lindata.dd_pcidriver.remove = mic_remove; mic_lindata.dd_pcidriver.driver.pm = &pci_dev_pm_ops; mic_lindata.dd_pcidriver.shutdown = mic_shutdown; if ((ret = alloc_chrdev_region(&mic_lindata.dd_dev, 0, MAX_DLDR_MINORS, "mic") != 0)) { printk("Error allocating device nodes: %d\n", ret); goto init_free_ports; } cdev_init(&mic_lindata.dd_cdev, &mic_fops); mic_lindata.dd_cdev.owner = THIS_MODULE; mic_lindata.dd_cdev.ops = &mic_fops; if ((ret = cdev_add(&mic_lindata.dd_cdev, mic_lindata.dd_dev, MAX_DLDR_MINORS) != 0)) { kobject_put(&mic_lindata.dd_cdev.kobj); goto init_free_region; } mic_lindata.dd_class = class_create(THIS_MODULE, "mic"); if (IS_ERR(mic_lindata.dd_class)) { printk("MICDLDR: Error createing mic class\n"); cdev_del(&mic_lindata.dd_cdev); ret = PTR_ERR(mic_lindata.dd_class); goto init_free_region; } #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,31) mic_lindata.dd_class->devnode = mic_devnode; #endif mic_lindata.dd_hostdev = device_create(mic_lindata.dd_class, NULL, mic_lindata.dd_dev, NULL, "ctrl"); mic_lindata.dd_scifdev = device_create(mic_lindata.dd_class, NULL, mic_lindata.dd_dev + 1, NULL, "scif"); ret = sysfs_create_group(&mic_lindata.dd_hostdev->kobj, &host_attr_group); ret = sysfs_create_group(&mic_lindata.dd_scifdev->kobj, &scif_attr_group); #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,31) mic_lindata.dd_class->devnode = NULL; #endif if (micveth_init(mic_lindata.dd_hostdev)) printk(KERN_ERR "%s: micveth_init failed\n", __func__); ret = pci_register_driver(&mic_lindata.dd_pcidriver); if (ret) { micscif_destroy(); printk("mic: failed to register pci driver %d\n", ret); goto clean_unregister; } if (!mic_data.dd_numdevs) { printk("mic: No MIC boards present. SCIF available in loopback mode\n"); } else { printk("mic: number of devices detected %d \n", mic_data.dd_numdevs); } for (i = 0; i < mic_data.dd_numdevs; i++) { mic_ctx_t *mic_ctx = get_per_dev_ctx(i); wait_event(mic_ctx->ioremapwq, mic_ctx->aper.va || mic_ctx->state == MIC_RESETFAIL); destroy_workqueue(mic_ctx->ioremapworkq); } micveth_init_legacy(mic_data.dd_numdevs, mic_lindata.dd_hostdev); ret = acptboot_init(); #ifdef USE_VCONSOLE micvcons_create(mic_data.dd_numdevs); #endif /* Initialize Data structures for PM Disconnect */ ret = micpm_disconn_init(mic_data.dd_numdevs + 1); if (ret) printk(KERN_ERR "%s: Failed to initialize PM disconnect" " data structures. PM may not work as expected." " ret = %d\n", __func__, ret); register_pm_notifier(&mic_pm_notifer); #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,34)) ret = pm_qos_add_requirement(PM_QOS_CPU_DMA_LATENCY, "mic", mic_pm_qos_cpu_dma_lat); if (ret) { printk(KERN_ERR "%s %d mic_pm_qos_cpu_dma_lat %d ret %d\n", __func__, __LINE__, mic_pm_qos_cpu_dma_lat, ret); ret = 0; /* Dont fail driver load due to PM QoS API. Fall through */ } #endif return 0; clean_unregister: device_destroy(mic_lindata.dd_class, mic_lindata.dd_dev + 1); device_destroy(mic_lindata.dd_class, mic_lindata.dd_dev); class_destroy(mic_lindata.dd_class); cdev_del(&mic_lindata.dd_cdev); unregister_pm_notifier(&mic_pm_notifer); init_free_region: unregister_chrdev_region(mic_lindata.dd_dev, MAX_DLDR_MINORS); init_free_ports: micpm_uninit(); return ret; }
static int __must_check communication_device_init( struct communication_device *self, dev_t major_minor, struct class *class) { int err; self->class = class; /* initialize lock and wait queue */ spin_lock_init(&self->lock); init_waitqueue_head(&self->wq); /* create good old cdev at (major,0) */ cdev_init(&self->good_old_cdev, &communication_device_cdev_fops); err = cdev_add(&self->good_old_cdev, major_minor, 1); BUG_ON(err); /* add sysfs bahoowazoo */ self->sysfs_device = device_create( /*class*/ self->class, /*parent device*/ NULL, /*char device*/ major_minor, /*device private data*/ self, /*printf format for device node name in /dev*/ DRIVER_NAME "-%d", /*format-filling data*/ MINOR(major_minor)); BUG_ON(self->sysfs_device == NULL); return 0; }
/* * init method: */ static int msm_uart_aux_init(void) { int ret = 0; void *res; dev_t dev = MKDEV( driver_major_no, 0 ); // register class "MSM_UART_AUX" irda_class = class_create( THIS_MODULE, DEV_NAME ); if( IS_ERR( irda_class ) ) { printk(KERN_ERR DEV_NAME ":class create failed\n" ); ret = PTR_ERR( irda_class ); goto err_class; } pr_debug(DEV_NAME ": init_module: %s start\n", DEV_NAME ); // Register char_dev "MSM_UART_AUX" if ( driver_major_no ) { ret = register_chrdev_region( dev, 2, DEV_NAME ); } else { ret = alloc_chrdev_region( &dev, 0, 2, DEV_NAME ); driver_major_no = MAJOR(dev); } if ( ret < 0 ) { printk(KERN_ERR DEV_NAME ":can't set major %d\n", driver_major_no ); goto err_alloc; } if ( driver_major_no == 0 ) { driver_major_no = ret; pr_debug(DEV_NAME ": Major no. is assigned to %d.\n", ret); } dev = MKDEV( driver_major_no, 0 ); printk(KERN_INFO DEV_NAME ": UART Major No = %d\n", driver_major_no ); cdev_init( &char_dev, &driver_fops ); char_dev.owner = THIS_MODULE; ret = cdev_add( &char_dev, dev, 1 ); if ( ret < 0 ) { printk(KERN_ERR DEV_NAME ": cdev_add failed\n" ); goto err_cdev; } irda_device = device_create( irda_class, NULL, dev, NULL, DEV_NAME ); if( IS_ERR( irda_device ) ) { printk(KERN_ERR DEV_NAME ": device create failed\n" ); ret = PTR_ERR( irda_device ); goto err_device; } // register driver "MSM_UART_AUX" ret = platform_driver_register( &irda_driver ); if ( ret < 0 ) { printk(KERN_ERR DEV_NAME ": platform driver register failed\n" ); goto err_platform; } // Set UART MemBase res = request_mem_region(THIS_UART_MEMBASE, THIS_UART_MEMSIZE, "sir_iomem" ); if (res < 0) { printk(KERN_ERR DEV_NAME ":request mem region fail.\n" ); ret = -EBUSY; goto err_mem; } uart_membase = (unsigned long)ioremap_nocache(THIS_UART_MEMBASE,THIS_UART_MEMSIZE); if ( uart_membase == 0 ) { printk(KERN_ERR DEV_NAME ":ioremap() fail.\n" ); ret = -EBUSY; goto err_iomap; } pr_debug(DEV_NAME ": ioremap() retuns %x\n", (int)uart_membase); printk(KERN_INFO DEV_NAME ": ioremap() retuns %x\n", (int)uart_membase ); // RX Config /* FUJITSU:2011-09-28 gpio chg start */ #if defined(CONFIG_MACH_F11APO) || defined(CONFIG_MACH_F12APON) if((system_rev != 0x08) || (system_rev != 0x0F)) { uart_rx2_en_34.pull = PM_GPIO_PULL_UP_1P5; } #elif defined(CONFIG_MACH_FJI12) if((system_rev != 0x00) || (system_rev != 0x07)) { uart_rx2_en_34.pull = PM_GPIO_PULL_UP_1P5; } #endif /* FUJITSU:2011-09-28 gpio chg end */ /* FUJITSU:2011-12-01 mod irda start */ #if 0 ret = pm8058_gpio_config( PM8058_UART_RX2_34, &uart_rx2_en_34 ); #else ret = pm8xxx_gpio_config( PM8058_UART_RX2_34, &uart_rx2_en_34 ); #endif /* FUJITSU:2011-12-01 mod irda end */ if ( ret ) { printk( KERN_ERR "%s: PM8058_UART_RX config failed[%d]\n", __func__, ret ); } // SD -> High /* FUJITSU:2011-09-28 gpio chg start */ #if defined(CONFIG_MACH_F11APO) || defined(CONFIG_MACH_F12APON) if((system_rev != 0x08) || (system_rev != 0x0F)) { irda_pwdown_26.output_buffer = PM_GPIO_OUT_BUF_OPEN_DRAIN; irda_pwdown_26.out_strength = PM_GPIO_STRENGTH_NO; } #elif defined(CONFIG_MACH_FJI12) if((system_rev != 0x00) || (system_rev != 0x07)) { irda_pwdown_26.output_buffer = PM_GPIO_OUT_BUF_OPEN_DRAIN; irda_pwdown_26.out_strength = PM_GPIO_STRENGTH_NO; } #endif /* FUJITSU:2011-09-28 gpio chg end */ /* FUJITSU:2011-12-01 mod irda start */ #if 0 ret = pm8058_gpio_config( PM8058_IRDA_PWD_26, &irda_pwdown_26 ); #else ret = pm8xxx_gpio_config( PM8058_IRDA_PWD_26, &irda_pwdown_26 ); #endif /* FUJITSU:2011-12-01 mod irda end */ if( ret < 0 ) { printk( KERN_ERR "%s: PM8058_IRDA_PWD_26 config failed[%d]\n", __func__, ret ); } /* FUJITSU:2011-12-01 mod irda start */ #if 0 gpio_set_value_cansleep( PM8058_GPIO_PM_TO_SYS( PM8058_IRDA_PWD_26 ), 1 ); #else gpio_set_value_cansleep( PM8058_IRDA_PWD_26, 1 ); #endif /* FUJITSU:2011-12-01 mod irda end */ // Init Normally printk(KERN_INFO DEV_NAME ":Load normally\n"); return 0; err_iomap: release_mem_region( THIS_UART_MEMBASE, THIS_UART_MEMSIZE ); err_mem: platform_driver_unregister( &irda_driver ); err_platform: device_destroy( irda_class, MKDEV( driver_major_no, 0 ) ); err_device: cdev_del( &char_dev ); err_cdev: unregister_chrdev_region( dev, 2 ); err_alloc: class_destroy( irda_class ); err_class: return ret; }
static int msm_gemini_init(struct platform_device *pdev) { int rc = -1; struct device *dev; GMN_DBG("%s:%d]\n", __func__, __LINE__); msm_gemini_device_p = __msm_gemini_init(pdev); if (msm_gemini_device_p == NULL) { GMN_PR_ERR("%s: initialization failed\n", __func__); goto fail; } rc = alloc_chrdev_region(&msm_gemini_devno, 0, 1, MSM_GEMINI_NAME); if (rc < 0) { GMN_PR_ERR("%s: failed to allocate chrdev\n", __func__); goto fail_1; } if (!msm_gemini_class) { msm_gemini_class = class_create(THIS_MODULE, MSM_GEMINI_NAME); if (IS_ERR(msm_gemini_class)) { rc = PTR_ERR(msm_gemini_class); GMN_PR_ERR("%s: create device class failed\n", __func__); goto fail_2; } } dev = device_create(msm_gemini_class, NULL, MKDEV(MAJOR(msm_gemini_devno), MINOR(msm_gemini_devno)), NULL, "%s%d", MSM_GEMINI_NAME, 0); if (IS_ERR(dev)) { GMN_PR_ERR("%s: error creating device\n", __func__); rc = -ENODEV; goto fail_3; } cdev_init(&msm_gemini_device_p->cdev, &msm_gemini_fops); msm_gemini_device_p->cdev.owner = THIS_MODULE; msm_gemini_device_p->cdev.ops = (const struct file_operations *) &msm_gemini_fops; rc = cdev_add(&msm_gemini_device_p->cdev, msm_gemini_devno, 1); if (rc < 0) { GMN_PR_ERR("%s: error adding cdev\n", __func__); rc = -ENODEV; goto fail_4; } printk(KERN_INFO "%s %s: success\n", __func__, MSM_GEMINI_NAME); return rc; fail_4: device_destroy(msm_gemini_class, msm_gemini_devno); fail_3: class_destroy(msm_gemini_class); fail_2: unregister_chrdev_region(msm_gemini_devno, 1); fail_1: __msm_gemini_exit(msm_gemini_device_p); fail: return rc; }
static int __init SiiMhlInit(void) { int32_t ret; dev_t devno; printk("\n============================================\n"); printk("%s driver starting!\n", MHL_DRIVER_NAME); sprintf(BUILT_TIME,"Build: %s", __TIME__"-"__DATE__); printk("Version: %s \n%s \n",DRV_VERSION,BUILT_TIME); printk("register_chrdev %s\n", MHL_DRIVER_NAME); printk("============================================\n"); //InitDebugSW(); if (devMajor) { devno = MKDEV(devMajor, 0); ret = register_chrdev_region(devno, MHL_DRIVER_MINOR_MAX, MHL_DRIVER_NAME); } else { ret = alloc_chrdev_region(&devno, 0, MHL_DRIVER_MINOR_MAX, MHL_DRIVER_NAME); devMajor = MAJOR(devno); } if (ret) { printk("register_chrdev %d, %s failed, error code: %d\n", devMajor, MHL_DRIVER_NAME, ret); return ret; } cdev_init(&siiMhlCdev, &siiMhlFops); siiMhlCdev.owner = THIS_MODULE; ret = cdev_add(&siiMhlCdev, devno, MHL_DRIVER_MINOR_MAX); if (ret) { printk("cdev_add %s failed %d\n", MHL_DRIVER_NAME, ret); goto free_chrdev; } siiMhlClass = class_create(THIS_MODULE, "mhl"); if (IS_ERR(siiMhlClass)) { printk("class_create failed %d\n", ret); ret = PTR_ERR(siiMhlClass); goto free_cdev; } siiMhlClass->dev_attrs = driver_attribs; gDriverContext.pDevice = device_create(siiMhlClass, NULL, MKDEV(devMajor, 0), NULL, "%s", MHL_DEVICE_NAME); if (IS_ERR(gDriverContext.pDevice)) { printk("class_device_create failed %s %d\n", MHL_DEVICE_NAME, ret); ret = PTR_ERR(gDriverContext.pDevice); goto free_class; } ret = StartMhlTxDevice(); if(ret == 0) { #ifdef USE_PROC mhldrv_create_proc(); #endif printk(KERN_NOTICE" mhldrv initialized successfully\n"); return 0; } else { device_destroy(siiMhlClass, MKDEV(devMajor, 0)); } free_class: class_destroy(siiMhlClass); free_cdev: cdev_del(&siiMhlCdev); free_chrdev: unregister_chrdev_region(MKDEV(devMajor, 0), MHL_DRIVER_MINOR_MAX); return ret; }
/** * platform driver * */ static int __devinit dsps_probe(struct platform_device *pdev) { int ret; pr_debug("%s.\n", __func__); if (pdev->dev.platform_data == NULL) { pr_err("%s: platform data is NULL.\n", __func__); return -ENODEV; } drv = kzalloc(sizeof(*drv), GFP_KERNEL); if (drv == NULL) { pr_err("%s: kzalloc fail.\n", __func__); goto alloc_err; } atomic_set(&drv->wd_crash, 0); atomic_set(&drv->crash_in_progress, 0); drv->pdata = pdev->dev.platform_data; drv->dev_class = class_create(THIS_MODULE, DRV_NAME); if (drv->dev_class == NULL) { pr_err("%s: class_create fail.\n", __func__); goto res_err; } ret = alloc_chrdev_region(&drv->dev_num, 0, 1, DRV_NAME); if (ret) { pr_err("%s: alloc_chrdev_region fail.\n", __func__); goto alloc_chrdev_region_err; } drv->dev = device_create(drv->dev_class, NULL, drv->dev_num, drv, DRV_NAME); if (IS_ERR(drv->dev)) { pr_err("%s: device_create fail.\n", __func__); goto device_create_err; } drv->cdev = cdev_alloc(); if (drv->cdev == NULL) { pr_err("%s: cdev_alloc fail.\n", __func__); goto cdev_alloc_err; } cdev_init(drv->cdev, &dsps_fops); drv->cdev->owner = THIS_MODULE; ret = cdev_add(drv->cdev, drv->dev_num, 1); if (ret) { pr_err("%s: cdev_add fail.\n", __func__); goto cdev_add_err; } ret = dsps_alloc_resources(pdev); if (ret) { pr_err("%s: failed to allocate dsps resources.\n", __func__); goto cdev_add_err; } ret = smsm_state_cb_register(SMSM_DSPS_STATE, SMSM_RESET, dsps_smsm_state_cb, 0); if (ret) { pr_err("%s: smsm_state_cb_register fail %d\n", __func__, ret); goto smsm_register_err; } ret = ssr_register_subsystem(&dsps_ssrops); if (ret) { pr_err("%s: ssr_register_subsystem fail %d\n", __func__, ret); goto ssr_register_err; } return 0; ssr_register_err: smsm_state_cb_deregister(SMSM_DSPS_STATE, SMSM_RESET, dsps_smsm_state_cb, 0); smsm_register_err: cdev_del(drv->cdev); cdev_add_err: kfree(drv->cdev); cdev_alloc_err: device_destroy(drv->dev_class, drv->dev_num); device_create_err: unregister_chrdev_region(drv->dev_num, 1); alloc_chrdev_region_err: class_destroy(drv->dev_class); res_err: kfree(drv); drv = NULL; alloc_err: return -ENODEV; }
struct tegra_dc_ext *tegra_dc_ext_register(struct nvhost_device *ndev, struct tegra_dc *dc) { int ret; struct tegra_dc_ext *ext; int devno; ext = kzalloc(sizeof(*ext), GFP_KERNEL); if (!ext) return ERR_PTR(-ENOMEM); BUG_ON(!tegra_dc_ext_devno); devno = tegra_dc_ext_devno + head_count + 1; cdev_init(&ext->cdev, &tegra_dc_devops); ext->cdev.owner = THIS_MODULE; ret = cdev_add(&ext->cdev, devno, 1); if (ret) { dev_err(&ndev->dev, "Failed to create character device\n"); goto cleanup_alloc; } ext->dev = device_create(tegra_dc_ext_class, &ndev->dev, devno, NULL, "tegra_dc_%d", ndev->id); if (IS_ERR(ext->dev)) { ret = PTR_ERR(ext->dev); goto cleanup_cdev; } ext->dc = dc; ext->nvmap = nvmap_create_client(nvmap_dev, "tegra_dc_ext"); if (!ext->nvmap) { ret = -ENOMEM; goto cleanup_device; } ret = tegra_dc_ext_setup_windows(ext); if (ret) goto cleanup_nvmap; mutex_init(&ext->cursor.lock); mutex_init(&ext->enable_change_lock); head_count++; return ext; cleanup_nvmap: nvmap_client_put(ext->nvmap); cleanup_device: device_del(ext->dev); cleanup_cdev: cdev_del(&ext->cdev); cleanup_alloc: kfree(ext); return ERR_PTR(ret); }
static int dm_init(void) { int result; int devno; struct drvr_mem * memDev; struct drvr_prog * progDev; struct i2c_adapter *i2c_adap; dev_t dev = 0; result = alloc_chrdev_region(&dev, 0, 2, DEVICE_NAME); gDrvrMajor = MAJOR(dev); if (result < 0) { printk(KERN_ALERT "Registering char device failed with %d\n", gDrvrMajor); return result; } drvr_devices = kmalloc(2 * sizeof(struct drvr_device), GFP_KERNEL); if (!drvr_devices) { dm_exit(); return -ENOMEM; } drvr_class = class_create(THIS_MODULE, DEVICE_NAME); memset(drvr_devices, 0, 2 * sizeof(struct drvr_device)); /*Initializing main mdevice for prog*/ devno = MKDEV(gDrvrMajor, 0); drvr_devices[0].type = prog; progDev = &(drvr_devices[0].data.prog); prog_device = device_create(drvr_class, NULL, devno, NULL, DEVICE_NAME); // should create /dev entry for main node drvr_devices[0].opened = 0; /*Do the i2c stuff*/ i2c_adap = i2c_get_adapter(1); // todo need to check i2c adapter id if (i2c_adap == NULL) { printk("Cannot get adapter 1 \n"); dm_exit(); return -1; } progDev->i2c_io = i2c_new_device(i2c_adap, &io_exp_info); i2c_put_adapter(i2c_adap); //don't know what it does, seems to release the adapter ... if (prog_device == NULL) { class_destroy(drvr_class); drvr_devices[0].opened = 0; dm_exit(); return -ENOMEM;; } cdev_init(&(drvr_devices[0].cdev), &dm_ops); drvr_devices[0].cdev.owner = THIS_MODULE; drvr_devices[0].cdev.ops = &dm_ops; cdev_add(&(drvr_devices[0].cdev), devno, 1); //printk(KERN_INFO "'mknod /dev/%s c %d %d'.\n", DEVICE_NAME, gDrvrMajor, 0); /* Initialize each device. */ devno = MKDEV(gDrvrMajor, 1); drvr_devices[1].type = mem; memDev = &(drvr_devices[1].data.mem); memDev->base_addr = (unsigned short *) (FPGA_BASE_ADDR); device_create(drvr_class, prog_device, devno, NULL, DEVICE_NAME_MEM); cdev_init(&(drvr_devices[1].cdev), &dm_ops); (drvr_devices[1].cdev).owner = THIS_MODULE; (drvr_devices[1].cdev).ops = &dm_ops; cdev_add(&(drvr_devices[1].cdev), devno, 1); drvr_devices[1].opened = 0; init_completion(&dma_comp); return ioctl_init(); }
static int omap_rproc_probe(struct platform_device *pdev) { int ret = 0, major, minor; struct device *tmpdev; struct device *dev = &pdev->dev; struct omap_rproc_platform_data *pdata = dev->platform_data; struct omap_rproc *rproc; if (!pdata || !pdata->name || !pdata->oh_name || !pdata->ops) return -EINVAL; dev_info(dev, "%s: adding rproc %s\n", __func__, pdata->name); rproc = kzalloc(sizeof(struct omap_rproc), GFP_KERNEL); if (!rproc) { dev_err(dev, "%s: kzalloc failed\n", __func__); ret = -ENOMEM; goto out; } platform_set_drvdata(pdev, rproc); major = MAJOR(omap_rproc_dev); minor = atomic_read(&num_of_rprocs); atomic_inc(&num_of_rprocs); rproc->dev = dev; rproc->minor = minor; atomic_set(&rproc->count, 0); rproc->name = pdata->name; mutex_init(&rproc->lock); BLOCKING_INIT_NOTIFIER_HEAD(&rproc->notifier); cdev_init(&rproc->cdev, &omap_rproc_fops); rproc->cdev.owner = THIS_MODULE; ret = cdev_add(&rproc->cdev, MKDEV(major, minor), 1); if (ret) { dev_err(dev, "%s: cdev_add failed: %d\n", __func__, ret); goto free_rproc; } tmpdev = device_create(omap_rproc_class, NULL, MKDEV(major, minor), NULL, OMAP_RPROC_NAME "%d", minor); if (IS_ERR(tmpdev)) { ret = PTR_ERR(tmpdev); pr_err("%s: device_create failed: %d\n", __func__, ret); goto clean_cdev; } pr_info("%s initialized %s, major: %d, base-minor: %d\n", OMAP_RPROC_NAME, pdata->name, MAJOR(omap_rproc_dev), minor); return 0; clean_cdev: cdev_del(&rproc->cdev); free_rproc: kfree(rproc); out: return ret; }
static int __init ali_otp_init ( void ) { int result; struct device *clsdev; struct ali_otp_dev *otp; ALI_OTP_DEBUG("%s, %d.\n", __FUNCTION__, __LINE__); /* Enable CSA module in SEE by PRC. */ //ali_m36_csa_see_init(); otp = &g_ali_otp_device; mutex_init(&otp->ioctl_mutex); result = alloc_chrdev_region(&otp->dev_id, 0, 1, "ali_otp"); if (result < 0) { ALI_OTP_DEBUG("%s, %d\n", __FUNCTION__, __LINE__); goto fail; } ALI_OTP_DEBUG("%s, dev_id:%d.\n", __FUNCTION__, otp->dev_id); cdev_init(&(otp->cdev), &g_ali_otp_fops); otp->cdev.owner = THIS_MODULE; result = cdev_add(&otp->cdev, otp->dev_id, 1); /* Fail gracefully if need be. */ if (result) { ALI_OTP_DEBUG("cdev_add() failed, result:%d\n", result); goto fail; } g_ali_otp_class = class_create(THIS_MODULE, "ali_otp_class"); if (IS_ERR(g_ali_otp_class)) { result = PTR_ERR(g_ali_otp_class); goto fail; } ALI_OTP_DEBUG("%s, %d.\n", __FUNCTION__, __LINE__); clsdev = device_create(g_ali_otp_class, NULL, otp->dev_id, otp, "ali_otp"); if (IS_ERR(clsdev)) { ALI_OTP_DEBUG(KERN_ERR "device_create() failed!\n"); result = PTR_ERR(clsdev); goto fail; } ali_otp_hw_init(); #ifdef CONFIG_ALI_CHIP_M3912 ali_otp_read(0x84 * 4, (unsigned char *)(&result), 4); ALI_OTP_DEBUG("[ %s, %d ], Chip ID = 0x%08x, [0x84] = 0x%08x\n", __FUNCTION__, __LINE__, *(unsigned int *)0xB8000000, result); if ((0x00200000 != result) && (0x00200038 != result)) { ALI_OTP_DEBUG("[ %s %d ], reboot!\n", __FUNCTION__, __LINE__); kernel_restart(NULL); } #endif #ifdef CONFIG_ALI_CHIP_M3901C ali_otp_read(0x84 * 4, (unsigned char *)(&result), 4); ALI_OTP_DEBUG("[ %s, %d ], Chip ID = 0x%08x, [0x84] = 0x%08x\n", __FUNCTION__, __LINE__, *(unsigned int *)0xB8000000, result); if ((0x00300000 != result) && (0x00300038 != result)) { ALI_OTP_DEBUG("[ %s %d ], reboot!\n", __FUNCTION__, __LINE__); kernel_restart(NULL); } #endif ALI_OTP_DEBUG("%s, %d.\n", __FUNCTION__, __LINE__); return(0); fail: return(-1); }
static int msm_jpeg_init_dev(struct platform_device *pdev) { int rc = -1; struct device *dev; struct msm_jpeg_device *msm_jpeg_device_p; char devname[10]; msm_jpeg_device_p = kzalloc(sizeof(struct msm_jpeg_device), GFP_ATOMIC); if (!msm_jpeg_device_p) { JPEG_PR_ERR("%s: no mem\n", __func__); return -EFAULT; } msm_jpeg_device_p->pdev = pdev; if (pdev->dev.of_node) of_property_read_u32((&pdev->dev)->of_node, "cell-index", &pdev->id); snprintf(devname, sizeof(devname), "%s%d", MSM_JPEG_NAME, pdev->id); rc = __msm_jpeg_init(msm_jpeg_device_p); if (rc < -1) { JPEG_PR_ERR("%s: initialization failed\n", __func__); goto fail; } v4l2_subdev_init(&msm_jpeg_device_p->subdev, &msm_jpeg_subdev_ops); v4l2_set_subdev_hostdata(&msm_jpeg_device_p->subdev, msm_jpeg_device_p); JPEG_DBG("%s: msm_jpeg_device_p 0x%x", __func__, (uint32_t)msm_jpeg_device_p); rc = alloc_chrdev_region(&msm_jpeg_device_p->msm_jpeg_devno, 0, 1, devname); if (rc < 0) { JPEG_PR_ERR("%s: failed to allocate chrdev\n", __func__); goto fail_1; } if (!msm_jpeg_device_p->msm_jpeg_class) { msm_jpeg_device_p->msm_jpeg_class = class_create(THIS_MODULE, devname); if (IS_ERR(msm_jpeg_device_p->msm_jpeg_class)) { rc = PTR_ERR(msm_jpeg_device_p->msm_jpeg_class); JPEG_PR_ERR("%s: create device class failed\n", __func__); goto fail_2; } } dev = device_create(msm_jpeg_device_p->msm_jpeg_class, NULL, MKDEV(MAJOR(msm_jpeg_device_p->msm_jpeg_devno), MINOR(msm_jpeg_device_p->msm_jpeg_devno)), NULL, "%s%d", MSM_JPEG_NAME, pdev->id); if (IS_ERR(dev)) { JPEG_PR_ERR("%s: error creating device\n", __func__); rc = -ENODEV; goto fail_3; } cdev_init(&msm_jpeg_device_p->cdev, &msm_jpeg_fops); msm_jpeg_device_p->cdev.owner = THIS_MODULE; msm_jpeg_device_p->cdev.ops = (const struct file_operations *) &msm_jpeg_fops; rc = cdev_add(&msm_jpeg_device_p->cdev, msm_jpeg_device_p->msm_jpeg_devno, 1); if (rc < 0) { JPEG_PR_ERR("%s: error adding cdev\n", __func__); rc = -ENODEV; goto fail_4; } platform_set_drvdata(pdev, &msm_jpeg_device_p); JPEG_DBG("%s %s%d: success\n", __func__, MSM_JPEG_NAME, pdev->id); return rc; fail_4: device_destroy(msm_jpeg_device_p->msm_jpeg_class, msm_jpeg_device_p->msm_jpeg_devno); fail_3: class_destroy(msm_jpeg_device_p->msm_jpeg_class); fail_2: unregister_chrdev_region(msm_jpeg_device_p->msm_jpeg_devno, 1); fail_1: __msm_jpeg_exit(msm_jpeg_device_p); fail: kfree(msm_jpeg_device_p); return rc; }
static int fdev_init(void) { int result = 0; char *name = "firstdev"; pr_alert("DEVICE:%s\n", name); pr_alert("The process is \"%s\" (pid %i)\n", current->comm, current->pid); pr_alert("UTS_RELEASE:%s", UTS_RELEASE); pr_alert("KERNEL_VERSION:%d", KERNEL_VERSION(2, 6, 10)); unsigned int firstminor = 0; int err; err = alloc_chrdev_region(&dev, firstminor, count, name); if (!err) { pr_alert("alloc_chrdev_region successful."); pr_alert("dev_t:%d,Major=%d,Minor=%d", dev, MAJOR(dev), MINOR(dev)); } else { pr_alert("alloc_chrdev_region failed."); } fdev_p = kmalloc_array(count, sizeof(struct fdev), GFP_KERNEL); if (!fdev_p) { result = -ENOMEM; pr_alert("kmalloc fdev_p failed."); goto fail; } else { pr_alert("kmalloc fdev_p successful."); } memset(fdev_p, 0, count * sizeof(struct fdev)); int i, major, devno; major = MAJOR(dev); for (i = 0; i < count; ++i) { struct fdev *devp = &fdev_p[i]; sema_init(&devp->sem, 1); devno = MKDEV(major, i); devp->major = major; devp->minor = i; devp->quantum_count = QUANTUM_DEFAULT; devp->qset_count = QSET_DEFAULT; cdev_init(&devp->cdev, &fops); devp->cdev.owner = THIS_MODULE; devp->cdev.ops = &fops; err = cdev_add(&devp->cdev, devno, 1); if (err) pr_alert("Error %d adding firstdev %d", err, i); else pr_alert("Successful adding firstdev %d", i); } return 0; fail: fdev_exit(); return result; }
/// Init and exit of module. static int __init memain_pci_init(void) { int result = 0; dev_t dev = MKDEV(major, 0); PDEBUG("executed.\n"); ME_INIT_LOCK(&me_lock); // Register the character device. if (major) { result = register_chrdev_region(dev, 1, ME_NAME_DRIVER); } else { result = alloc_chrdev_region(&dev, 0, 1, ME_NAME_DRIVER); major = MAJOR(dev); } if (result < 0) { PERROR("Can't get major driver no.\n"); goto INIT_ERROR_1; } cdevp = cdev_alloc(); if (!cdevp) { PERROR("Can't get character device structure.\n"); result = -ENOMEM; goto INIT_ERROR_2; } cdevp->ops = &me_file_operations; cdevp->owner = THIS_MODULE; result = cdev_add(cdevp, dev, 1); if (result < 0) { PERROR("Cannot add character device structure.\n"); goto INIT_ERROR_3; } // Register pci driver. This will return 0 if the PCI subsystem is not available. result = pci_register_driver(&me_pci_driver); if (result < 0) { PERROR("Can't register pci driver.\n"); goto INIT_ERROR_3; } PLOG("Loaded: %s version: %08x\n", ME_NAME_DRIVER, ME_VERSION_DRIVER); memain_class = class_create(THIS_MODULE, ME_NAME_DRIVER); memain_dev = device_create(memain_class, NULL, dev, #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26) NULL, #endif ME_NAME_NODE); return 0; // INIT_ERROR_4: // pci_unregister_driver(&me_pci_driver); INIT_ERROR_3: cdev_del(cdevp); INIT_ERROR_2: unregister_chrdev_region(dev, 1); INIT_ERROR_1: return result; }
static int char_reg_setup_cdev (void) { int retval; int temp = 0; struct device *class_dev; dev_t devid = MKDEV (helloworld_major, helloworld_minor); scull_devices = kmalloc(helloworld_nr_device * sizeof (struct scull_dev),\ GFP_KERNEL); memset((char *)scull_devices, 0,\ helloworld_nr_device * sizeof(struct scull_dev)); for (; temp < helloworld_nr_device; temp++) { scull_devices[temp].quantum = quantum; scull_devices[temp].qset = qset; scull_devices[temp].size = 0; cdev_init(&scull_devices[temp].cdev, &helloworld_fops); scull_devices[temp].cdev.ops = &helloworld_fops; scull_devices[temp].cdev.owner = THIS_MODULE; } retval= cdev_add(&scull_devices[temp].cdev, devid, helloworld_nr_device); if (retval < 0) { DEBUG (1, "err_cdev_add."); goto err_cdev_add; } helloworld_class = class_create(THIS_MODULE, "hbgk_class"); if (IS_ERR(helloworld_class)) { retval = PTR_ERR(helloworld_class); DEBUG (1, "err_class_create."); goto err_class_create; } for (temp = 0; temp < helloworld_nr_device; temp++) { class_dev = device_create(helloworld_class, NULL, MKDEV(helloworld_major, temp), NULL, "hbgk_device%d", temp); //class_dev = device_create(bsg_class, parent, dev, NULL, "%s", devname); if (IS_ERR(class_dev)) { retval= PTR_ERR(class_dev); DEBUG (1, "err_device_create %d", temp); goto err_device_create; } } #if 0 cdev_p= cdev_alloc(); if (!cdev_p) { DEBUG(1, "cdev_alloc failed\n"); goto out; } cdev_p->owner = THIS_MODULE; cdev_p->ops = &helloworld_fops; err = cdev_add(cdev_p, devid, HELLOWORLD_MAX_DEVICE); if (err < 0) { DEBUG (1, "cdev_add fialed.\n"); goto err_cdev_add; } #endif return 0; err_device_create: class_destroy(helloworld_class); err_class_create: for (temp = 0; temp < helloworld_nr_device; temp++) { cdev_del(&scull_devices[temp].cdev); scull_trim(&scull_devices[temp]); } err_cdev_add: kfree (scull_devices); scull_devices = NULL; return retval; }
static int __init smbdirect_init(void) { int res = 0; proc_create("driver/smbdirect", 0, NULL, &smbd_proc_fops); smbd_device = kmalloc(sizeof(struct smbd_device), GFP_KERNEL | __GFP_ZERO); if (smbd_device == NULL) { return -ENOMEM; } mutex_init(&smbd_device->connection_list_mutex); INIT_LIST_HEAD(&smbd_device->connection_list); init_waitqueue_head(&smbd_device->conn_queue); res = alloc_chrdev_region(&smbd_device->smbdirect_dev_no, 0, 1, "smbdirect"); if (res < 0) { printk(KERN_ERR "Major number allocation failed\n"); return res; } smbd_device->smbd_wq = alloc_workqueue("SMB-Direct Work Queue", 0, 0); if (smbd_device->smbd_wq == NULL) { printk(KERN_ERR "Unable to allocate work queue\n"); return -ENOMEM; } cdev_init(&smbd_device->cdev, &smbd_fops); smbd_device->cdev.owner = THIS_MODULE; res = cdev_add(&smbd_device->cdev, smbd_device->smbdirect_dev_no, 1); if (res) { printk(KERN_ERR "Unable to add smbdirect device: %d\n", res); goto out_wq; } smbd_device->kio_class = class_create(THIS_MODULE, "smbdirect"); if (IS_ERR(smbd_device->kio_class)) { printk(KERN_ERR "class_create(): err: %ld\n", PTR_ERR(smbd_device->kio_class)); goto out_cdev; } smbd_device->kio_device = device_create(smbd_device->kio_class, NULL, smbd_device->smbdirect_dev_no, NULL, "smbdirect"); if (IS_ERR(smbd_device->kio_device)) { printk(KERN_ERR "device_create(): err: %ld\n", PTR_ERR(smbd_device->kio_device)); goto out; } return 0; out: if (smbd_device->kio_device != NULL) { device_destroy(smbd_device->kio_class, smbd_device->smbdirect_dev_no); } if (smbd_device->kio_class != NULL) { class_destroy(smbd_device->kio_class); } out_cdev: cdev_del(&smbd_device->cdev); out_wq: destroy_workqueue(smbd_device->smbd_wq); if (smbd_device != NULL) { kfree(smbd_device); } return res; }
static int motor_add_one(unsigned int id, unsigned int *params) { int status, err; struct cdev *motor_cdev; struct platform_device *pdev; struct gpio_pwm_platform_data pdata; if ( mot_nump[id] < 4 ) { printk(KERN_INFO "stepper: nothing to register for id: %d.\n", id); return 0; } g_enable[id] = params[1]; g_dir[id] = params[2]; g_step[id] = params[3]; g_lpwr[id] = params[4]; polarity[id] = params[5]; /* sanity check */ if ( !( g_enable[id] && g_dir[id] && g_step[id])) { printk(KERN_INFO "stepper: missing parameters, exit driver.\n"); goto err_para; } /* request and set pwm channel and gpio pins */ pdev = platform_device_alloc("gpio_pwm", g_step[id]); if (!pdev) { err = -ENOMEM; goto err_para; } pdata.gpio = g_step[id]; err = platform_device_add_data(pdev, &pdata, sizeof(pdata)); if (err) goto err; err = platform_device_add(pdev); if (err) goto err; pwmc[id] = pwm_request("gpio_pwm", g_step[id], "stepper"); if (pwmc[id] != NULL) { goto err_pwm; } motor_pwm_set (pwmc[id], 0); /* set default pwm pulse */ if ( gpio_request(g_enable[id], "motor-enable") < 0 ) { goto err_gpioenable; } gpio_direction_output(g_enable[id] ,0); if ( gpio_request(g_dir[id], "motor-ccw") < 0) { goto err_gpiodir; } gpio_direction_output(g_dir[id] ,0); if (g_lpwr[id] != 0) { if ( gpio_request(g_lpwr[id], "motor-lowpwr") < 0 ) { goto err_gpiolwr; } gpio_direction_output(g_lpwr[id] ,0); } /* set to home */ steps[id] = 0; /* alloc a new device number (major: dynamic, minor: 0) */ status = alloc_chrdev_region(&motor_devno, 0, 1, "motor"); /* create a new char device */ motor_cdev = cdev_alloc(); if(motor_cdev == NULL) { status=-ENOMEM; goto err_dev; } /*save the cdev for id's */ mot_map[id] = (int) motor_cdev; motor_cdev->owner = THIS_MODULE; motor_cdev->ops = &motor_fops; status = cdev_add(motor_cdev, motor_devno, 1); if(status){ goto err_dev; } device_create(motor_class, NULL, motor_devno, NULL, "motor%d", params[0]); printk(KERN_INFO "stepper: motor%d registred on major: %u; minor: %u\n", \ params[0], MAJOR(motor_devno), MINOR(motor_devno)); return 0; err: printk(KERN_INFO "stepper: err\n"); err_dev: printk(KERN_INFO "stepper: err_dev\n"); err_gpiolwr: printk(KERN_INFO "stepper: err_gpiolwr\n"); err_gpiodir: printk(KERN_INFO "stepper: err_gpiodir\n"); err_gpioenable: printk(KERN_INFO "stepper: err_gpioenable\n"); err_gpiostep: printk(KERN_INFO "stepper: err_gpiostep "); err_pwm: printk(KERN_INFO "stepper: err_pwm\n"); err_para: printk(KERN_INFO "stepper: Error management not yet implemented. \ Please reboot your board %d\n",g_step[id]); return -1; }
static int lf1000_gpio_probe(struct platform_device *pdev) { int ret = 0; struct resource *res; #ifdef CPU_LF1000 struct resource *res_cur; #endif res = platform_get_resource(pdev, IORESOURCE_MEM, 0); if(!res) { printk(KERN_ERR "gpio: failed to get resource\n"); return -ENXIO; } if(!request_mem_region(res->start, (res->end - res->start)+1, "lf1000_gpio")) { printk(KERN_ERR "gpio: failed to get region\n"); return -EBUSY; } gpio.mem = ioremap_nocache(res->start, (res->end - res->start)+1); if(gpio.mem == NULL) { printk(KERN_ERR "gpio: failed to remap\n"); ret = -ENOMEM; goto fail_remap; } #ifdef CPU_LF1000 res_cur = platform_get_resource(pdev, IORESOURCE_MEM, 1); if(!res_cur) { printk(KERN_ERR "gpio: failed to get resource\n"); ret = -ENXIO; goto fail_remap; } if(!request_mem_region(res_cur->start, (res_cur->end - res_cur->start) + 1, "lf1000_gpio_cur")) { printk(KERN_ERR "gpio: failed to get region\n"); ret = -EBUSY; goto fail_remap; } gpio.mem_cur = ioremap_nocache(res_cur->start, (res_cur->end - res_cur->start) + 1); if(gpio.mem_cur == NULL) { printk(KERN_ERR "gpio: failed to remap\n"); ret = -ENOMEM; goto fail_remap_cur; } #endif /* turn off GPIO interrupts */ gpio_set_int32(GPIO_PORT_A, 0); gpio_set_int32(GPIO_PORT_B, 0); gpio_set_int32(GPIO_PORT_C, 0); gpio.devnum = MKDEV(GPIO_MAJOR, 0); cdev_init(&gpio.cdev, &gpio_fops); gpio.cdev.owner = THIS_MODULE; gpio.cdev.ops = &gpio_fops; ret = cdev_add(&gpio.cdev, gpio.devnum, 1); if(ret) { printk(KERN_ALERT "gpio: failed to get a device\n"); goto fail_dev; } gpio.irq = platform_get_irq(pdev, 0); if(gpio.irq < 0) { printk(KERN_INFO "gpio: failed to get IRQ\n"); ret = gpio.irq; goto fail_irq; } ret = request_irq(gpio.irq, gpio_irq, SA_INTERRUPT|SA_SAMPLE_RANDOM, "gpio", NULL); if(ret) { printk(KERN_ERR "gpio: requesting IRQ failed\n"); goto fail_irq; } sysfs_create_group(&pdev->dev.kobj, &gpio_attr_group); return 0; fail_irq: cdev_del(&gpio.cdev); fail_dev: iounmap(gpio.mem); #ifdef CPU_LF1000 fail_remap_cur: release_mem_region(res_cur->start, (res_cur->end - res_cur->start) + 1); #endif fail_remap: release_mem_region(res->start, (res->end - res->start) + 1); return ret; }
static int frandom_init_module(void) { int result; /* The buffer size MUST be at least 256 bytes, because we assume that minimal length in init_rand_state(). */ if (frandom_bufsize < 256) { pr_err("frandom: Invalid frandom_bufsize: %d\n", frandom_bufsize); return -EINVAL; } if ((frandom_chunklimit != 0) && (frandom_chunklimit < 256)) { pr_err("frandom: Invalid frandom_chunklimit: %d\n", frandom_chunklimit); return -EINVAL; } erandom_state = kmalloc(sizeof(struct frandom_state), GFP_KERNEL); if (!erandom_state) return -ENOMEM; /* This specific buffer is only used for seeding, so we need 256 bytes exactly */ erandom_state->buf = kmalloc(256, GFP_KERNEL); if (!erandom_state->buf) { kfree(erandom_state); return -ENOMEM; } sema_init(&erandom_state->sem, 1); /* Init semaphore as a mutex */ erandom_seeded = 0; frandom_class = class_create(THIS_MODULE, "fastrng"); if (IS_ERR(frandom_class)) { result = PTR_ERR(frandom_class); pr_warn("frandom: Failed to register class fastrng\n"); goto error0; } /* * Register your major, and accept a dynamic number. This is the * first thing to do, in order to avoid releasing other module's * fops in frandom_cleanup_module() */ result = alloc_chrdev_region(&frandom_devt, 0, NR_FRANDOM_DEVS, "frandom"); if (result < 0) { pr_warn("frandom: failed to alloc frandom region\n"); goto error1; } frandom_minor = MINOR(frandom_devt); erandom_minor = frandom_minor + 1; erandom_devt = MKDEV(MAJOR(frandom_devt), erandom_minor); cdev_init(&frandom_cdev, &frandom_fops); frandom_cdev.owner = THIS_MODULE; result = cdev_add(&frandom_cdev, frandom_devt, 1); if (result) { pr_warn("frandom: Failed to add cdev for /dev/frandom\n"); goto error2; } frandom_device = device_create(frandom_class, NULL, frandom_devt, NULL, "frandom"); if (IS_ERR(frandom_device)) { pr_warn("frandom: Failed to create frandom device\n"); goto error3; } cdev_init(&erandom_cdev, &frandom_fops); erandom_cdev.owner = THIS_MODULE; result = cdev_add(&erandom_cdev, erandom_devt, 1); if (result) { pr_warn("frandom: Failed to add cdev for /dev/erandom\n"); goto error4; } erandom_device = device_create(frandom_class, NULL, erandom_devt, NULL, "erandom"); if (IS_ERR(erandom_device)) { pr_warn("frandom: Failed to create erandom device\n"); goto error5; } return 0; /* succeed */ error5: cdev_del(&erandom_cdev); error4: device_destroy(frandom_class, frandom_devt); error3: cdev_del(&frandom_cdev); error2: unregister_chrdev_region(frandom_devt, NR_FRANDOM_DEVS); error1: class_destroy(frandom_class); error0: kfree(erandom_state->buf); kfree(erandom_state); return result; }
static int memdev_init(void) { int result; int i; dev_t devno = MKDEV(mem_major, 0); /*静态申请设备号 register_chrdev_region(dev_t first,unsigned int count,char *name) First :要分配的设备编号范围的初始值(次设备号常设为0); Count:表示从起始设备号开始连续的设备号数目,需要注意的是 count 不能过大,不然有可能溢出到下一个主设备号上; Name:编号相关联的设备名称. (/proc/devices); */ if(mem_major) result = register_chrdev_region(devno, 2, "memdev");/*次设备0~1 对应 mem_fops 2~255 都不对应*/ else /*动态分配设备号 参数 dev ,在系统调用成功后,会把得到的设备号方到这个参数中; 参数 firstminor 是请求的第一个次设备号,一般为 0 ; 参数 count 表示一个范围值; 参数 name 表示设备名。 */ { result = alloc_chrdev_region(&devno, 0, 2, "memdev"); mem_major = MAJOR(devno); } if (result < 0) return result; cdev_init(&cdev, &mem_fops); cdev.owner = THIS_MODULE; cdev.ops = &mem_fops; /*注册字符设备*/ cdev_add(&cdev, MKDEV(mem_major, 0), MEMDEV_NR_DEVS); /*为设备描述结构分配内存*/ mem_devp = kmalloc(MEMDEV_NR_DEVS * sizeof(struct mem_dev), GFP_KERNEL); if (!mem_devp) /*申请失败*/ { result = -ENOMEM; goto fail_malloc; } memset(mem_devp, 0, sizeof(struct mem_dev)); /*为设备分配内存*/ for (i=0; i< MEMDEV_NR_DEVS; i++) { mem_devp[i].size = MEMDEV_SIZE; mem_devp[i].data = kmalloc(MEMDEV_SIZE, GFP_KERNEL); memset(mem_devp[i].data, 0, MEMDEV_SIZE); } /*自动创建设备文件*/ myclass = class_create(THIS_MODULE, "test_char");/* file_operations中.owner = THIS_MODULE,在 sys 下创建类目录 /sys/class/test_char;(类的名字)*/ class_dev = device_create(myclass,NULL, MKDEV(mem_major, 0), NULL, "memdev0"); /* device_create 在 sys 下创建设备目录 /sys/device/memdev0,如果它所属的类不为空, 创建 /sys/class/test_char/memdev0 到 /sys/device/memdev0 的链接; */ return 0; fail_malloc: unregister_chrdev_region(devno, 1); return result; }
/* * scdrv_init * * Called at boot time to initialize the system controller communication * facility. */ int __init scdrv_init(void) { geoid_t geoid; cnodeid_t cnode; char devname[32]; char *devnamep; struct sysctl_data_s *scd; void *salbuf; struct class_simple *snsc_class; dev_t first_dev, dev; nasid_t event_nasid = ia64_sn_get_console_nasid(); if (alloc_chrdev_region(&first_dev, 0, numionodes, SYSCTL_BASENAME) < 0) { printk("%s: failed to register SN system controller device\n", __FUNCTION__); return -ENODEV; } snsc_class = class_simple_create(THIS_MODULE, SYSCTL_BASENAME); for (cnode = 0; cnode < numionodes; cnode++) { geoid = cnodeid_get_geoid(cnode); devnamep = devname; format_module_id(devnamep, geo_module(geoid), MODULE_FORMAT_BRIEF); devnamep = devname + strlen(devname); sprintf(devnamep, "#%d", geo_slab(geoid)); /* allocate sysctl device data */ scd = kmalloc(sizeof (struct sysctl_data_s), GFP_KERNEL); if (!scd) { printk("%s: failed to allocate device info" "for %s/%s\n", __FUNCTION__, SYSCTL_BASENAME, devname); continue; } memset(scd, 0, sizeof (struct sysctl_data_s)); /* initialize sysctl device data fields */ scd->scd_nasid = cnodeid_to_nasid(cnode); if (!(salbuf = kmalloc(SCDRV_BUFSZ, GFP_KERNEL))) { printk("%s: failed to allocate driver buffer" "(%s%s)\n", __FUNCTION__, SYSCTL_BASENAME, devname); kfree(scd); continue; } if (ia64_sn_irtr_init(scd->scd_nasid, salbuf, SCDRV_BUFSZ) < 0) { printk ("%s: failed to initialize SAL for" " system controller communication" " (%s/%s): outdated PROM?\n", __FUNCTION__, SYSCTL_BASENAME, devname); kfree(scd); kfree(salbuf); continue; } dev = first_dev + cnode; cdev_init(&scd->scd_cdev, &scdrv_fops); if (cdev_add(&scd->scd_cdev, dev, 1)) { printk("%s: failed to register system" " controller device (%s%s)\n", __FUNCTION__, SYSCTL_BASENAME, devname); kfree(scd); kfree(salbuf); continue; } class_simple_device_add(snsc_class, dev, NULL, "%s", devname); ia64_sn_irtr_intr_enable(scd->scd_nasid, 0 /*ignored */ , SAL_IROUTER_INTR_RECV); /* on the console nasid, prepare to receive * system controller environmental events */ if(scd->scd_nasid == event_nasid) { scdrv_event_init(scd); } } return 0; }