static void __exit mei_exit(void) { unregister_chrdev_region(mei_devt, MEI_MAX_DEVS); class_destroy(mei_class); mei_cl_bus_exit(); }
int __init init_module(void) { int result; word divisor; unsigned char reg; struct resource *base_res; // request the IO port region #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0) base_res = request_region(PORT_COM1,8,"uart"); printk("<1>""uart: INIT_MOD\n"); if (!base_res) { printk("<1>""uart: can't get I/O address 0x%x\n",PORT_COM1); return 1; } #else if ( check_region(PORT_COM1, 8) ) { printk(KERN_INFO "uart: Can't get I/O address 0x%x\n", PORT_COM1); return -1; } request_region(PORT_COM1, 8, "uart"); #endif #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0) if(uart_major) { if ( register_chrdev_region(MKDEV(uart_major,0), 1, "uart") < 0 ) { printk ("register_chrdev_region() fail\n"); release_region(PORT_COM1,8); return -1; } } else { if (alloc_chrdev_region(&devno, 0, 1, "uart") < 0) { printk ("alloc_chrdev_region() fail\n"); release_region(PORT_COM1,8); return -1; } uart_major=MAJOR(devno); } cdev_init(&mycdev, &uart_fops); mycdev.owner=THIS_MODULE; if(cdev_add(&mycdev, MKDEV(uart_major,0), 1)) { printk ("Error adding cdev\n"); unregister_chrdev_region(MKDEV(uart_major, 0), 1); release_region(PORT_COM1,8); } #else uart_major = register_chrdev(0, "uart", &uart_fops); if (uart_major < 0) { printk("<1>" "uart: can't get major number\n"); release_region(PORT_COM1,8); return -1; } #endif // allocate read/write buffer prbuf = (unsigned char *)__get_free_page(GFP_KERNEL); pwbuf = (unsigned char *)__get_free_page(GFP_KERNEL); if ( !prbuf || !pwbuf ) { release_region(PORT_COM1,8); #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0) cdev_del(&mycdev); unregister_chrdev_region(MKDEV(uart_major, 0), 1); #else unregister_chrdev(uart_major, "uart"); #endif return -ENOMEM; } // PORT_COM1, 2400 outb(DLR_ON,(PORT_COM1 + REG_LCR)); reg = inb(PORT_COM1+REG_LCR); divisor = 48; outw(divisor, PORT_COM1); // BITS_8 | PARITY_NONE | STOP_ONE outb((BITS_8 | PARITY_NONE | STOP_ONE), (PORT_COM1 + REG_LCR)); result = 0; // request interrupt for comport1 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24) result = request_irq(COM_IRQ, uart_interrupt,SA_INTERRUPT,"uart",NULL); #else result = request_irq(COM_IRQ, uart_interrupt,IRQF_DISABLED,"uart",NULL); #endif if(result) { printk("<1>""can't register irq\n"); if( prbuf) free_page((unsigned long)prbuf); if( pwbuf) free_page((unsigned long)pwbuf); release_region(PORT_COM1,8); #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0) cdev_del(&mycdev); unregister_chrdev_region(MKDEV(uart_major, 0), 1); #else unregister_chrdev(uart_major, "uart"); #endif return -EFAULT; } else { printk("<1>""interrupt on\n"); outb(COM1_IRQ_ON,(PORT_COM1 + REG_MCR)); outb(REC_IRQ_ON,(PORT_COM1 + REG_IER)); } ring_buffer_init (&ring); return 0; }
/** * @brief character device module init function * @return success: 0 * @see */ static int32_t __init gp_csi_module_init(void) { int32_t ret; int32_t devno; dev_t dev; struct device *device; DEBUG(KERN_WARNING "ModuleInit: csi \n"); /* allocate a major number to csi module */ ret = alloc_chrdev_region( &dev, CSI_MINOR, 1, "csi0" ); if( ret<0 ) { DIAG_ERROR("CSI: can't get major\n"); goto fail_init; } /* allocate a structure for csi device */ csi_devices = kmalloc(sizeof(gp_csi_dev_t), GFP_KERNEL); if(!csi_devices) { DIAG_ERROR("CSI: can't kmalloc\n"); ret = -ENOMEM; goto fail_kmalloc; } memset(csi_devices, 0, sizeof(gp_csi_dev_t)); csi_devices->major = MAJOR(dev); /* create class for csi character device */ csi_devices->csi_class = class_create(THIS_MODULE, "csi"); if (IS_ERR(csi_devices->csi_class)) { DIAG_ERROR("CSI: can't create class\n"); ret = -EFAULT; goto fail_create_class; } /* request a irq for csi interrupt service */ ret = request_irq(IRQ_SENSOR, gp_csi_irq_handler, 0, "CSI_IRQ", csi_devices); if (ret < 0) { DIAG_ERROR("CSI: request csi irq fail\n"); goto fail_request_irq; } /* create character device node */ devno = MKDEV(csi_devices->major, CSI_MINOR); cdev_init(&(csi_devices->c_dev), &csi_fops); csi_devices->c_dev.owner = THIS_MODULE; csi_devices->c_dev.ops = &csi_fops; ret = cdev_add(&(csi_devices->c_dev), devno, 1); if(ret < 0) { DIAG_ERROR("CSI: cdev_add error\n"); goto fail_device_register; } device = device_create( csi_devices->csi_class, NULL, devno, NULL, "csi%d", 0); if(!device) { DIAG_ERROR("CSI: device_create error\n"); goto fail_device_create; } csi_devices->sdidx = NO_INPUT; CLEAR(csi_devices->in_que, 0xFF); CLEAR(csi_devices->out_que, 0xFF); /* initial the semaphore */ init_MUTEX(&(csi_devices->sem)); platform_device_register(&csi_device); return platform_driver_register(&csi_driver); fail_device_create: cdev_del(&csi_devices->c_dev); fail_device_register: free_irq(IRQ_SENSOR, csi_devices); fail_request_irq: class_destroy(csi_devices->csi_class); fail_create_class: kfree(csi_devices); fail_kmalloc: unregister_chrdev_region(dev, CSI_NR_DEVS); fail_init: return ret; }
static ssize_t raw_file_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) { struct iovec local_iov = { .iov_base = (char __user *)buf, .iov_len = count }; return generic_file_write_nolock(file, &local_iov, 1, ppos); } static ssize_t raw_file_aio_write(struct kiocb *iocb, const char __user *buf, size_t count, loff_t pos) { struct iovec local_iov = { .iov_base = (char __user *)buf, .iov_len = count }; return generic_file_aio_write_nolock(iocb, &local_iov, 1, &iocb->ki_pos); } static struct file_operations raw_fops = { .read = generic_file_read, .aio_read = generic_file_aio_read, .write = raw_file_write, .aio_write = raw_file_aio_write, .open = raw_open, .release= raw_release, .ioctl = raw_ioctl, .readv = generic_file_readv, .writev = generic_file_writev, .owner = THIS_MODULE, }; static struct file_operations raw_ctl_fops = { .ioctl = raw_ctl_ioctl, .open = raw_open, .owner = THIS_MODULE, }; static struct cdev raw_cdev = { .kobj = {.name = "raw", }, .owner = THIS_MODULE, }; static int __init raw_init(void) { int i; dev_t dev = MKDEV(RAW_MAJOR, 0); if (register_chrdev_region(dev, MAX_RAW_MINORS, "raw")) goto error; cdev_init(&raw_cdev, &raw_fops); if (cdev_add(&raw_cdev, dev, MAX_RAW_MINORS)) { kobject_put(&raw_cdev.kobj); unregister_chrdev_region(dev, MAX_RAW_MINORS); goto error; } devfs_mk_cdev(MKDEV(RAW_MAJOR, 0), S_IFCHR | S_IRUGO | S_IWUGO, "raw/rawctl"); for (i = 1; i < MAX_RAW_MINORS; i++) devfs_mk_cdev(MKDEV(RAW_MAJOR, i), S_IFCHR | S_IRUGO | S_IWUGO, "raw/raw%d", i); return 0; error: printk(KERN_ERR "error register raw device\n"); return 1; }
static int __init init_ugidctl(void) { dev_t dev; int rc; rc = find_syscalls(); if (rc) return rc; dprintk("found sys_setuid() 0x%p", ugidctl_sys_setuid); dprintk("found sys_setgid() 0x%p", ugidctl_sys_setgid); dprintk("found sys_setgroups() 0x%p\n", ugidctl_sys_setgroups); rc = -ENOMEM; uid_cache = KMEM_CACHE(ugidctl_uid_node, 0); if (!uid_cache) { printk(KERN_ERR "%s: cannot allocate uid cache\n", MOD_NAME); goto out; } gid_cache = KMEM_CACHE(ugidctl_gid_node, 0); if (!gid_cache) { printk(KERN_ERR "%s: cannot allocate gid cache\n", MOD_NAME); goto out_uid_cache; } rc = alloc_chrdev_region(&dev, 0, 1, DEV_NAME); if (rc) { printk(KERN_ERR "%s: failed to register chrdev region\n", MOD_NAME); goto out_gid_cache; } ugidctl_major = MAJOR(dev); ugidctl_class = class_create(THIS_MODULE, DEV_NAME); if (IS_ERR(ugidctl_class)) { printk(KERN_ERR "%s: failed to register with sysfs\n", MOD_NAME); rc = PTR_ERR(ugidctl_class); goto out_region; } cdev_init(&ugidctl_cdev, &ugidctl_fops); rc = cdev_add(&ugidctl_cdev, dev, 1); if (rc) { printk(KERN_ERR "%s: failed to add char device\n", MOD_NAME); goto out_class; } device_create(ugidctl_class, NULL, dev, NULL, DEV_NAME); // printk(KERN_INFO "%s: v" MOD_VERSION " loaded\n", MOD_NAME); return 0; out_class: class_destroy(ugidctl_class); out_region: unregister_chrdev_region(dev, 1); out_gid_cache: kmem_cache_destroy(gid_cache); out_uid_cache: kmem_cache_destroy(uid_cache); out: return rc; }
int __init nand_debug_init(void) { int result, ret; struct device *device = NULL; dev_t devno = MKDEV(nand_debug_major, nand_debug_minor); if(nand_debug_major) { result = register_chrdev_region(devno, 1, "nand_debug"); } else { result = alloc_chrdev_region(&devno, 0, 1, "nand_debug"); nand_debug_major = MAJOR(devno); } if(result < 0) { printk(KERN_ERR "[NAND_DEBUG_DRV] LINE(%d) Register device failed!\n", __LINE__); goto fail_init; } /* create class for csi character device */ nand_debug_class = class_create(THIS_MODULE, "nand_debug"); if (IS_ERR(nand_debug_class)) { printk(KERN_ERR "nand_debug: can't create class\n"); ret = -EFAULT; goto fail_create_class; } memset(&nand_debug_dev, 0, sizeof(nand_debug_dev)); cdev_init(&nand_debug_dev, &nand_debug_fops); nand_debug_dev.owner = THIS_MODULE; nand_debug_dev.ops = &nand_debug_fops; if(cdev_add(&nand_debug_dev, devno, 1)) { printk(KERN_ERR "[NAND_DEBUG_DRV] LINE(%d) Error adding Nand_debug device!\n", __LINE__); goto fail_device_register; } /* create character device node */ devno = MKDEV(nand_debug_major, 0); device = device_create( nand_debug_class, NULL, devno, NULL, "nand_debug"); if(!device){ printk(KERN_ERR"nand_debug: device_create error\n"); goto fail_device_create; } printk(KERN_INFO "[NAND_DEBUG_DRV] Major:%d, Minor:%d\n",MAJOR(devno),MINOR(devno)); printk(KERN_INFO "[NAND_DEBUG_DRV] Nand_debug driver has been initialized successfully!\n"); return 0; fail_device_create: cdev_del(&nand_debug_dev); fail_device_register: class_destroy(nand_debug_class); fail_create_class: unregister_chrdev_region(devno, 1); fail_init: return ret; }
/* * 模块加载方法 */ static ini __init hello_init(void){ int err = -1; dev_t dev = 0; struct device* temp = NULL; printk(KERN_ALERT"Initializing hello device.\n"); /* * 动态分配主设备号和从设备号 */ err = alloc_chrdev_region(&dev, 0, 1, HELLO_DEVICE_NODE_NAME); if(err < 0){ printk(KERN_ALERT"Failed to alloc char dev region.\n"); goto fail; } hello_major = MAJOR(dev); hello_minor = MINOR(dev); /* * 分配hello设备结构体变量 */ hello_dev = kmalloc(sizeof(struct hello_android_dev), GFP_KERNEL); if(!hello_dev){ err = -ENOMEM; printk(KERN_ALERT"Failed to alloc hello_dev.\n"); goto unregister; } /* * 初始化设备 */ err = __hello_setup_dev(hello_dev); if(err){ printk(KERN_ALERT"Failed to setup dev:%d.\n", err); goto cleanup; } /* * 在/sys/class/目录下创建设备类别目录hello */ hello_class = class_create(THIS_MODULE, HELLO_DEVICE_CLASS_NAME); if(IS_ERR(hello_class)){ err = PTR_ERR(hello_class); printk(KERN_ALERT"Failed to create hello class.\n"); goto destroy_cdev; } /* * 在/dev/目录和/sys/class/hello目录下分别创建设备文件hello */ temp = device_create(hello_class, NULL, dev, "%s", HELLO_DEVICE_FILE_NAME); if(IS_ERR(temp)){ err = PRT_ERR(temp); printk(KERN_ALERT"Failed to create hello device.\n"); goto destroy_class; } /* * 在/sys/class/hello/hello目录下创建属性文件val */ err = device_create_file(temp, &dev_attr_val); if(err < 0){ printk(KERN_ALERT"Failed to create attribute val.\n"); goto destroy_device; } dev_set_drvdata(temp, hello_dev); /* * 创建/proc/hello文件 */ hello_create_proc(); printk(KERN_ALERT"Successed to initalize hello device.\n"); return 0; destroy_device: device_destroy(hello_class, dev); destroy_class: class_destroy(hello_class); destroy_cdev: cdev_del(&(hello_dev->dev)); cleanup: kfree(hello_dev); unregister: unregister_chrdev_region(MKDEV(hello_major, hello_minor), 1); fail: return err; }
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) { 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; /* 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); printk(KERN_WARNING "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() */ 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; /* succeed */ 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 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 void uio_major_cleanup(void) { unregister_chrdev_region(MKDEV(uio_major, 0), UIO_MAX_DEVICES); cdev_del(uio_cdev); }
//----------------------------------------------------------------- // pci initialization function // enable pci & register character device static int test_pci_probe (struct pci_dev *pdev, const struct pci_device_id *id) { int err; char irq; int alloc_ret = 0; int cdev_err = 0; short vendor_id, device_id; //----------------------------------------------------------------- // config PCI // enable pci device err = pci_enable_device(pdev); if(err) { printk(KERN_ERR "can't enable pci device\n"); goto error; } tprintk("PCI enabled for %s\n", DRIVER_TEST_NAME); // request PCI region // bar 0 ... MMIO dev_data->mmio_base = pci_resource_start(pdev, BAR_MMIO); dev_data->mmio_length = pci_resource_len(pdev, BAR_MMIO); dev_data->mmio_flags = pci_resource_flags(pdev, BAR_MMIO); tprintk( "mmio_base: %lx, mmio_length: %lx, mmio_flags: %lx\n", dev_data->mmio_base, dev_data->mmio_length, dev_data->mmio_flags); dev_data->mmio_addr = ioremap(dev_data->mmio_base, TEST_PCI_MEMSIZE); if(!(dev_data->mmio_flags & IORESOURCE_MEM)){ printk(KERN_ERR "BAR%d is not for mmio\n", BAR_MMIO); goto error; } err = pci_request_region(pdev, BAR_MMIO, DRIVER_TEST_NAME); if(err) { printk(KERN_ERR "%s :error pci_request_region MMIO\n", __func__); goto error; } // bar 1 ... IO port dev_data->pio_base = pci_resource_start(pdev, BAR_PIO); dev_data->pio_length = pci_resource_len(pdev, BAR_PIO); dev_data->pio_flags = pci_resource_flags(pdev, BAR_PIO); tprintk("pio_base: %lx, pio_length: %lx, pio_flags: %lx\n", dev_data->pio_base, dev_data->pio_length, dev_data->pio_flags); if(!(dev_data->pio_flags & IORESOURCE_IO)){ printk(KERN_ERR "BAR%d is not for pio\n", BAR_PIO); goto error; } err = pci_request_region(pdev, BAR_PIO, DRIVER_TEST_NAME); if(err) { printk(KERN_ERR "%s :error pci_request_region PIO\n", __func__); goto error; } // show PCI configuration data // define at include/uapi/linux/pci_regs.h pci_read_config_word(pdev, PCI_VENDOR_ID, &vendor_id); pci_read_config_word(pdev, PCI_DEVICE_ID, &device_id); tprintk("PCI Vendor ID:%x, Device ID:%x\n", vendor_id, device_id); dev_data->pdev = pdev; dev_data->pio_memsize = TEST_PIO_DATASIZE; tprintk("sucess allocate i/o region\n"); //----------------------------------------------------------------- // config irq // get irq number irq = pdev->irq; // same as pci_read_config_byte(pdev, PCI_INTERRUPT_LINE, &irq); tprintk("device irq: %d\n", irq); err = request_irq(irq, test_pci_handler, 0, DRIVER_TEST_NAME, pdev); if(err){ printk(KERN_ERR "%s :error request irq %d\n", __func__, irq); goto error; } //----------------------------------------------------------------- // register character device // allocate major number alloc_ret = alloc_chrdev_region(&test_pci_devt, test_pci_minor, test_pci_devs_max, DRIVER_TEST_NAME); if(alloc_ret) goto error; test_pci_major = MAJOR(test_pci_devt); dev_data->cdev = (struct cdev*)kmalloc(sizeof(struct cdev), GFP_KERNEL); if(!dev_data->cdev) goto error; cdev_init(dev_data->cdev, &test_pci_fops); dev_data->cdev->owner = THIS_MODULE; cdev_err = cdev_add(dev_data->cdev, test_pci_devt, test_pci_devs_max); if(cdev_err) goto error; tprintk("%s driver(major %d) installed.\n", DRIVER_TEST_NAME, test_pci_major); //----------------------------------------------------------------- // config DMA err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)); if(err) { printk(KERN_ERR "Cannot set DMA mask\n"); goto error; } pci_set_master(pdev); // allocate consistent DMA dev_data->cdma_buffer = pci_alloc_consistent(pdev, TEST_CDMA_BUFFER_SIZE, &dev_data->cdma_addr); if(dev_data->cdma_buffer == NULL) { printk(KERN_ERR "Cannot allocate consistent DMA buffer\n"); goto error; } dev_data->cdma_len = TEST_CDMA_BUFFER_SIZE; // send consistent DMA info to device outl(dev_data->cdma_addr, dev_data->pio_base + TEST_SET_CDMA_ADDR); outl(dev_data->cdma_len, dev_data->pio_base + TEST_SET_CDMA_LEN); tprintk("cdma_addr : %x\n", dev_data->cdma_addr); // streaming DMA dev_data->sdma_buffer = kmalloc(TEST_SDMA_BUFFER_SIZE, GFP_KERNEL); if(dev_data->sdma_buffer == NULL) { printk(KERN_ERR "Cannot allocate streaming DMA buffer\n"); goto error; } init_waitqueue_head(&(dev_data->sdma_q)); return 0; error: tprintk("PCI load error\n"); if(cdev_err == 0) cdev_del(dev_data->cdev); if(alloc_ret == 0) unregister_chrdev_region(test_pci_devt, test_pci_devs_max); return -1; }
static void __exit torch_module_deinit(void) { device_destroy(torch.classptr, devnum); class_destroy(torch.classptr); unregister_chrdev_region(devnum, 1); }
/*---------------------------------------------------------- * pipe_init *---------------------------------------------------------*/ static int pipe_init(void) { dev_t dev = MKDEV(pipe_major, 0); int alloc_ret = 0; int cdev_err = 0; struct device *class_dev = NULL; /* * register major number */ /* reserve major number */ if (pipe_major) { alloc_ret = register_chrdev_region(dev, pipe_dev_count, DRIVER_NAME); if (alloc_ret < 0) { printk(KERN_ERR "pipe: unable to get major %d\n", pipe_major); goto error; } if (pipe_major == 0) pipe_major = alloc_ret; } else { alloc_ret = alloc_chrdev_region(&dev, pipe_minor, pipe_dev_count, DRIVER_NAME); if (alloc_ret) { printk(KERN_ERR "pipe: unable to get major \n"); goto error; } pipe_major = MAJOR(dev); } /* register system call handler(fops) */ cdev_init(&pipe_cdev, &pipe_ops); /* register to kernel */ pipe_cdev.owner = THIS_MODULE; pipe_cdev.ops = &pipe_ops; cdev_err = cdev_add (&pipe_cdev, MKDEV(pipe_major, pipe_minor), pipe_dev_count); if (cdev_err) { goto error; } /* register class */ pipe_class = class_create(THIS_MODULE, DRIVER_NAME); if (IS_ERR(pipe_class)) { goto error; } class_dev = device_create(pipe_class, NULL, MKDEV(pipe_major, pipe_minor), NULL, DRIVER_NAME); if (IS_ERR(class_dev)) printk(KERN_ERR "pipe: can't create device\n"); return 0; error: if (cdev_err == 0) cdev_del(&pipe_cdev); if (alloc_ret == 0) unregister_chrdev_region(MKDEV(pipe_major, 0), pipe_dev_count); return -1; }
static void __exit membuf_exit(void) { cdev_del(&membuf_dev.dev); unregister_chrdev_region(membuf_dev.devno, MEMBUF_DEV_COUNT); }
static int __init bt_hwctl_init(void) { int ret = -1, err = -1; platform_driver_register(&mt6622_driver); if (!(bh = kzalloc(sizeof(struct bt_hwctl), GFP_KERNEL))) { BT_HWCTL_ERR("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_ERR("alloc chrdev region failed\n"); goto ERR_EXIT; } BT_HWCTL_INFO("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_ERR("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_ERR("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"); /* request gpio used by BT */ //mt_bt_gpio_init(); BT_HWCTL_INFO("Bluetooth hardware control driver initialized\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; }