int sam_ajoy_initialization(void) { int ret; int fd; int i; /* NOTE: The ADC driver was initialized earlier in the bring-up sequence. */ /* Open the ADC driver for reading. */ fd = open("/dev/adc0", O_RDONLY); if (fd < 0) { int errcode = get_errno(); ierr("ERROR: Failed to open /dev/adc0: %d\n", errcode); return -errcode; } /* Detach the file structure from the file descriptor so that it can be * used on any thread. */ ret = file_detach(fd, &g_adcfile); if (ret < 0) { ierr("ERROR: Failed to detach from file descriptor: %d\n", ret); (void)close(fd); return ret; } /* Configure the GPIO pins as interrupting inputs. */ for (i = 0; i < AJOY_NGPIOS; i++) { /* Configure the PIO as an input */ sam_configpio(g_joypio[i]); /* Configure PIO interrupts, attach the interrupt handler, but leave * the interrupt disabled. */ sam_pioirq(g_joypio[i]); (void)irq_attach(g_joyirq[i], ajoy_interrupt); sam_pioirqdisable(g_joyirq[i]); } /* Register the joystick device as /dev/ajoy0 */ ret = ajoy_register("/dev/ajoy0", &g_ajoylower); if (ret < 0) { ierr("ERROR: ajoy_register failed: %d\n", ret); file_close_detached(&g_adcfile); } return ret; }
int board_ajoy_initialize(void) { int ret; int i; #ifndef NO_JOYSTICK_ADC int fd; iinfo("Initialize ADC driver: /dev/adc0\n"); /* NOTE: The ADC driver was initialized earlier in the bring-up sequence. */ /* Open the ADC driver for reading. */ fd = open("/dev/adc0", O_RDONLY); if (fd < 0) { int errcode = get_errno(); ierr("ERROR: Failed to open /dev/adc0: %d\n", errcode); return -errcode; } /* Detach the file structure from the file descriptor so that it can be * used on any thread. */ ret = file_detach(fd, &g_adcfile); if (ret < 0) { ierr("ERROR: Failed to detach from file descriptor: %d\n", ret); (void)close(fd); return ret; } #endif /* Configure the GPIO pins as interrupting inputs. NOTE: This is * unnecessary for interrupting pins since it will also be done by * stm32_gpiosetevent(). */ for (i = 0; i < AJOY_NGPIOS; i++) { /* Configure the PIO as an input */ stm32_configgpio(g_joygpio[i]); } /* Register the joystick device as /dev/ajoy0 */ iinfo("Initialize joystick driver: /dev/ajoy0\n"); ret = ajoy_register("/dev/ajoy0", &g_ajoylower); if (ret < 0) { ierr("ERROR: ajoy_register failed: %d\n", ret); #ifndef NO_JOYSTICK_ADC file_close_detached(&g_adcfile); #endif } return ret; }
int pty_register(int minor) { FAR struct pty_devpair_s *devpair; int pipe_a[2]; int pipe_b[2]; char devname[16]; int ret; /* Allocate a device instance */ devpair = kmm_zalloc(sizeof(struct pty_devpair_s)); if (devpair == NULL) { return -ENOMEM; } /* Initialize semaphores */ nxsem_init(&devpair->pp_slavesem, 0, 0); nxsem_init(&devpair->pp_exclsem, 0, 1); /* The pp_slavesem semaphore is used for signaling and, hence, should not * have priority inheritance enabled. */ nxsem_setprotocol(&devpair->pp_slavesem, SEM_PRIO_NONE); #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS devpair->pp_minor = minor; #endif devpair->pp_locked = true; devpair->pp_master.pd_devpair = devpair; devpair->pp_master.pd_master = true; devpair->pp_slave.pd_devpair = devpair; /* Create two pipes: * * pipe_a: Master source, slave sink (TX, slave-to-master) * pipe_b: Master sink, slave source (RX, master-to-slave) */ ret = pipe2(pipe_a, CONFIG_PSEUDOTERM_TXBUFSIZE); if (ret < 0) { goto errout_with_devpair; } ret = pipe2(pipe_b, CONFIG_PSEUDOTERM_RXBUFSIZE); if (ret < 0) { goto errout_with_pipea; } /* Detach the pipe file descriptors (closing them in the process) * * fd[0] is for reading; * fd[1] is for writing. */ ret = file_detach(pipe_a[0], &devpair->pp_master.pd_src); if (ret < 0) { goto errout_with_pipeb; } pipe_a[0] = -1; ret = file_detach(pipe_a[1], &devpair->pp_slave.pd_sink); if (ret < 0) { goto errout_with_pipeb; } pipe_a[1] = -1; ret = file_detach(pipe_b[0], &devpair->pp_slave.pd_src); if (ret < 0) { goto errout_with_pipeb; } pipe_b[0] = -1; ret = file_detach(pipe_b[1], &devpair->pp_master.pd_sink); if (ret < 0) { goto errout_with_pipeb; } pipe_b[1] = -1; /* Register the slave device * * BSD style (deprecated): /dev/ttypN * SUSv1 style: /dev/pts/N * * Where N is the minor number */ #ifdef CONFIG_PSEUDOTERM_BSD snprintf(devname, 16, "/dev/ttyp%d", minor); #else snprintf(devname, 16, "/dev/pts/%d", minor); #endif ret = register_driver(devname, &g_pty_fops, 0666, &devpair->pp_slave); if (ret < 0) { goto errout_with_pipeb; } /* Register the master device * * BSD style (deprecated): /dev/ptyN * SUSv1 style: Master: /dev/ptmx (multiplexor, see ptmx.c) * * Where N is the minor number */ snprintf(devname, 16, "/dev/pty%d", minor); ret = register_driver(devname, &g_pty_fops, 0666, &devpair->pp_master); if (ret < 0) { goto errout_with_slave; } return OK; errout_with_slave: #ifdef CONFIG_PSEUDOTERM_BSD snprintf(devname, 16, "/dev/ttyp%d", minor); #else snprintf(devname, 16, "/dev/pts/%d", minor); #endif (void)unregister_driver(devname); errout_with_pipeb: if (pipe_b[0] >= 0) { close(pipe_b[0]); } else { (void)file_close_detached(&devpair->pp_master.pd_src); } if (pipe_b[1] >= 0) { close(pipe_b[1]); } else { (void)file_close_detached(&devpair->pp_slave.pd_sink); } errout_with_pipea: if (pipe_a[0] >= 0) { close(pipe_a[0]); } else { (void)file_close_detached(&devpair->pp_slave.pd_src); } if (pipe_a[1] >= 0) { close(pipe_a[1]); } else { (void)file_close_detached(&devpair->pp_master.pd_sink); } errout_with_devpair: nxsem_destroy(&devpair->pp_exclsem); nxsem_destroy(&devpair->pp_slavesem); kmm_free(devpair); return ret; }
int syslog_dev_initialize(FAR const char *devpath, int oflags, int mode) { int fd; int ret; /* At this point, the only expected states are SYSLOG_UNINITIALIZED or * SYSLOG_REOPEN.. Not SYSLOG_INITIALIZING, SYSLOG_FAILURE, SYSLOG_OPENED. */ DEBUGASSERT(g_syslog_dev.sl_state == SYSLOG_UNINITIALIZED || g_syslog_dev.sl_state == SYSLOG_REOPEN); /* Save the path to the device in case we have to re-open it. * If we get here and sl_devpath is not equal to NULL, that is a clue * that we will are re-openingthe file. */ if (g_syslog_dev.sl_state == SYSLOG_REOPEN) { /* Re-opening: Then we should already have a copy of the path to the * device. */ DEBUGASSERT(g_syslog_dev.sl_devpath != NULL && strcmp(g_syslog_dev.sl_devpath, devpath) == 0); } else { /* Initializing. Copy the device path so that we can use it if we * have to re-open the file. */ DEBUGASSERT(g_syslog_dev.sl_devpath == NULL); g_syslog_dev.sl_oflags = oflags; g_syslog_dev.sl_mode = mode; g_syslog_dev.sl_devpath = strdup(devpath); DEBUGASSERT(g_syslog_dev.sl_devpath != NULL); } g_syslog_dev.sl_state = SYSLOG_INITIALIZING; /* Open the device driver. */ fd = open(devpath, oflags, mode); if (fd < 0) { int errcode = get_errno(); DEBUGASSERT(errcode > 0); /* We failed to open the file. Perhaps it does exist? Perhaps it * exists, but is not ready because it depends on insertion of a * removable device? * * In any case we will attempt to re-open the device repeatedly. * The assumption is that the device path is valid but that the * driver has not yet been registered or a removable device has * not yet been installed. */ g_syslog_dev.sl_state = SYSLOG_REOPEN; return -errcode; } /* Detach the file descriptor from the file structure. The file * descriptor is a task-specific concept. Detaching the file * descriptor allows us to use the device on all threads in all tasks. */ ret = file_detach(fd, &g_syslog_dev.sl_file); if (ret < 0) { /* This should not happen and means that something very bad has * occurred. */ g_syslog_dev.sl_state = SYSLOG_FAILURE; close(fd); return ret; } /* The SYSLOG device is open and ready for writing. */ nxsem_init(&g_syslog_dev.sl_sem, 0, 1); g_syslog_dev.sl_holder = NO_HOLDER; g_syslog_dev.sl_state = SYSLOG_OPENED; return OK; }
int pty_register(int minor) { FAR struct pty_devpair_s *devpair; int pipe_a[2]; int pipe_b[2]; char devname[16]; int ret; /* Allocate a device instance */ devpair = kmm_zalloc(sizeof(struct pty_devpair_s)); if (devpair == NULL) { return -ENOMEM; } sem_init(&devpair->pp_slavesem, 0, 0); sem_init(&devpair->pp_exclsem, 0, 1); #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS devpair->pp_minor = minor; #endif devpair->pp_locked = true; devpair->pp_master.pd_devpair = devpair; devpair->pp_master.pd_master = true; devpair->pp_slave.pd_devpair = devpair; /* Create two pipes */ ret = pipe(pipe_a); if (ret < 0) { goto errout_with_devpair; } ret = pipe(pipe_b); if (ret < 0) { goto errout_with_pipea; } /* Detach the pipe file descriptors (closing them in the process) * * fd[0] is for reading; * fd[1] is for writing. */ ret = file_detach(pipe_a[0], &devpair->pp_master.pd_src); if (ret < 0) { goto errout_with_pipeb; } pipe_a[0] = -1; ret = file_detach(pipe_a[1], &devpair->pp_slave.pd_sink); if (ret < 0) { goto errout_with_pipeb; } pipe_a[1] = -1; ret = file_detach(pipe_b[0], &devpair->pp_slave.pd_src); if (ret < 0) { goto errout_with_pipeb; } pipe_b[0] = -1; ret = file_detach(pipe_b[1], &devpair->pp_master.pd_sink); if (ret < 0) { goto errout_with_pipeb; } pipe_b[1] = -1; /* Register the slave device * * BSD style (deprecated): /dev/ttypN * SUSv1 style: /dev/pts/N * * Where N is the minor number */ #ifdef CONFIG_PSEUDOTERM_BSD snprintf(devname, 16, "/dev/ttyp%d", minor); #else snprintf(devname, 16, "/dev/pts/%d", minor); #endif ret = register_driver(devname, &pty_fops, 0666, &devpair->pp_slave); if (ret < 0) { goto errout_with_pipeb; } /* Register the master device * * BSD style (deprecated): /dev/ptyN * SUSv1 style: Master: /dev/ptmx (multiplexor, see ptmx.c) * * Where N is the minor number */ snprintf(devname, 16, "/dev/pty%d", minor); ret = register_driver(devname, &pty_fops, 0666, &devpair->pp_master); if (ret < 0) { goto errout_with_slave; } return OK; errout_with_slave: #ifdef CONFIG_PSEUDOTERM_BSD snprintf(devname, 16, "/dev/ttyp%d", minor); #else snprintf(devname, 16, "/dev/pts/%d", minor); #endif (void)unregister_driver(devname); errout_with_pipeb: if (pipe_b[0] >= 0) { close(pipe_b[0]); } else { (void)file_close_detached(&devpair->pp_master.pd_src); } if (pipe_b[1] >= 0) { close(pipe_b[1]); } else { (void)file_close_detached(&devpair->pp_slave.pd_sink); } errout_with_pipea: if (pipe_a[0] >= 0) { close(pipe_a[0]); } else { (void)file_close_detached(&devpair->pp_slave.pd_src); } if (pipe_a[1] >= 0) { close(pipe_a[1]); } else { (void)file_close_detached(&devpair->pp_master.pd_sink); } errout_with_devpair: sem_destroy(&devpair->pp_exclsem); sem_destroy(&devpair->pp_slavesem); kmm_free(devpair); return ret; }
/* * (Re-)read the "users" file into memory. */ static int file_instantiate(CONF_SECTION *conf, void **instance) { struct file_instance *inst; int rcode; inst = rad_malloc(sizeof *inst); if (!inst) { return -1; } memset(inst, 0, sizeof(*inst)); if (cf_section_parse(conf, inst, module_config) < 0) { free(inst); return -1; } rcode = getusersfile(inst->usersfile, &inst->users, inst->compat_mode); if (rcode != 0) { radlog(L_ERR|L_CONS, "Errors reading %s", inst->usersfile); file_detach(inst); return -1; } rcode = getusersfile(inst->acctusersfile, &inst->acctusers, inst->compat_mode); if (rcode != 0) { radlog(L_ERR|L_CONS, "Errors reading %s", inst->acctusersfile); file_detach(inst); return -1; } /* * Get the pre-proxy stuff */ rcode = getusersfile(inst->preproxy_usersfile, &inst->preproxy_users, inst->compat_mode); if (rcode != 0) { radlog(L_ERR|L_CONS, "Errors reading %s", inst->preproxy_usersfile); file_detach(inst); return -1; } rcode = getusersfile(inst->auth_usersfile, &inst->auth_users, inst->compat_mode); if (rcode != 0) { radlog(L_ERR|L_CONS, "Errors reading %s", inst->auth_usersfile); file_detach(inst); return -1; } rcode = getusersfile(inst->postproxy_usersfile, &inst->postproxy_users, inst->compat_mode); if (rcode != 0) { radlog(L_ERR|L_CONS, "Errors reading %s", inst->postproxy_usersfile); file_detach(inst); return -1; } rcode = getusersfile(inst->postauth_usersfile, &inst->postauth_users, inst->compat_mode); if (rcode != 0) { radlog(L_ERR|L_CONS, "Errors reading %s", inst->postauth_usersfile); file_detach(inst); return -1; } *instance = inst; return 0; }
FAR struct mtd_dev_s *blockmtd_initialize(FAR const char *path, size_t offset, size_t mtdlen, int16_t sectsize, int32_t erasesize) { FAR struct file_dev_s *priv; size_t nblocks; int mode; int ret; int fd; /* Create an instance of the FILE MTD device state structure */ priv = (FAR struct file_dev_s *)kmm_zalloc(sizeof(struct file_dev_s)); if (!priv) { ferr("ERROR: Failed to allocate the FILE MTD state structure\n"); return NULL; } /* Determine the file open mode */ mode = O_RDOK; #ifdef CONFIG_FS_WRITABLE mode |= O_WROK; #endif /* Try to open the file. NOTE that block devices will use a character * driver proxy. */ fd = open(path, mode); if (fd <0) { ferr("ERROR: Failed to open the FILE MTD file %s\n", path); kmm_free(priv); return NULL; } /* Detach the file descriptor from the open file */ ret = file_detach(fd, &priv->mtdfile); if (ret < 0) { ferr("ERROR: Failed to detail the FILE MTD file %s\n", path); close(fd); kmm_free(priv); return NULL; } /* Set the block size based on the provided sectsize parameter */ if (sectsize <= 0) { priv->blocksize = CONFIG_FILEMTD_BLOCKSIZE; } else { priv->blocksize = sectsize; } /* Set the erase size based on the provided erasesize parameter */ if (erasesize <= 0) { priv->erasesize = CONFIG_FILEMTD_ERASESIZE; } else { priv->erasesize = erasesize; } /* Force the size to be an even number of the erase block size */ nblocks = mtdlen / priv->erasesize; if (nblocks < 3) { ferr("ERROR: Need to provide at least three full erase block\n"); file_close_detached(&priv->mtdfile); kmm_free(priv); return NULL; } /* Perform initialization as necessary. (unsupported methods were * nullified by kmm_zalloc). */ priv->mtd.erase = filemtd_erase; priv->mtd.bread = filemtd_bread; priv->mtd.bwrite = filemtd_bwrite; priv->mtd.read = filemtd_byteread; #ifdef CONFIG_MTD_BYTE_WRITE priv->mtd.write = file_bytewrite; #endif priv->mtd.ioctl = filemtd_ioctl; priv->offset = offset; priv->nblocks = nblocks; #ifdef CONFIG_MTD_REGISTRATION /* Register the MTD with the procfs system if enabled */ mtd_register(&priv->mtd, "filemtd"); #endif return &priv->mtd; }