/** * @brief Close the Analogy device related with the descriptor * * The file descriptor is associated with a context. The context is * one of the enabler of asynchronous transfers. So, by closing the * file descriptor, the programer must keep in mind that the currently * occuring asynchronous transfer will cancelled. * * @param[in] dsc Device descriptor * * @return 0 on success. Otherwise: * * - -EINVAL is returned if some argument is missing or wrong; the * the dsc pointer should be checked; check also the kernel log * ("dmesg") * */ int a4l_close(a4l_desc_t * dsc) { /* Basic checking */ if (dsc == NULL) return -EINVAL; return a4l_sys_close(dsc->fd); }
/** * @brief Open an Analogy device and basically fill the descriptor * * @param[out] dsc Device descriptor * @param[in] fname Device name * * @return 0 on success. Otherwise: * * - -EINVAL is returned if some argument is missing or wrong; the * fname and the dsc pointer should be checked; check also the * kernel log ("dmesg") * - -EFAULT is returned if a user <-> kernel transfer went wrong * */ int a4l_open(a4l_desc_t *dsc, const char *fname) { int ret; /* Basic checking */ if (dsc == NULL) return -EINVAL; /* Initializes the descriptor */ memset(dsc, 0, sizeof(a4l_desc_t)); /* Opens the driver */ dsc->fd = a4l_sys_open(fname); if (dsc->fd < 0) return dsc->fd; /* Basically fills the descriptor */ ret = a4l_sys_desc(dsc->fd, dsc, A4L_BSC_DESC); if (ret < 0) { a4l_sys_close(dsc->fd); } return ret; }
int main(int argc, char *argv[]) { int c; char *devfile; int err = 0, fd = -1; /* Compute arguments */ while ((c = getopt_long(argc, argv, "hvqVrR:W:S:", a4l_conf_opts, NULL)) >= 0) { switch (c) { case 'h': do_print_usage(); goto out_a4l_config; case 'v': vlevel = 2; break; case 'q': vlevel = 0; break; case 'V': do_print_version(); goto out_a4l_config; case 'r': actions |= DO_DETACH; break; case 'R': case 'W': fprintf(stdout, "analogy_config: the option --read-buffer-size " "and --write-buffer-size will be deprecated; " "please use --buffer-size instead (-S)\n"); case 'S': actions |= DO_BUFCONFIG; bufsize = strtoul(optarg, NULL, 0); break; default: do_print_usage(); goto out_a4l_config; } } /* Here we have choice: - if the option -r is set, only one additional option is useful - if the option -S is set without no attach options - if the option -S is set with attach options */ if ((actions & DO_DETACH) && argc - optind < 1 ) { fprintf(stderr, "analogy_config: specify a device to detach\n"); goto out_a4l_config; } if ((actions & DO_DETACH) && (actions & DO_BUFCONFIG)) { fprintf(stderr, "analogy_config: skipping buffer size configuration" "because of detach action\n"); } if (!(actions & DO_DETACH) && !(actions & DO_BUFCONFIG) && argc - optind < 2) { do_print_usage(); goto out_a4l_config; } else if (!(actions & DO_DETACH) && argc - optind >= 2) actions |= DO_ATTACH; /* Whatever the action, we need to retrieve the device path */ devfile = argv[optind]; /* Init the descriptor structure */ /* Open the specified file */ fd = a4l_sys_open(devfile); if (fd < 0) { err = fd; fprintf(stderr, "analogy_config: a4l_open failed err=%d\n", err); goto out_a4l_config; } if (actions & DO_DETACH) { err = a4l_sys_detach(fd); if (err < 0) fprintf(stderr, "analogy_config: detach failed err=%d\n", err); goto out_a4l_config; } if (actions & DO_ATTACH) { a4l_lnkdesc_t lnkdsc; memset(&lnkdsc, 0, sizeof(a4l_lnkdesc_t)); /* Fill the descriptor with the driver name */ lnkdsc.bname = argv[optind + 1]; lnkdsc.bname_size = strlen(argv[optind + 1]); /* Process driver-specific options */ if (argc - optind == 3) { err = process_extra_arg(&lnkdsc, argv[optind + 2]); if (err < 0) goto out_a4l_config; } /* Go... */ err = a4l_sys_attach(fd, &lnkdsc); if (err < 0) { fprintf(stderr, "analogy_config: attach failed err=%d\n", err); goto out_a4l_config; } if (lnkdsc.opts != NULL) free(lnkdsc.opts); } if (actions & DO_BUFCONFIG) { err = a4l_sys_bufcfg(fd, A4L_BUF_DEFMAGIC, bufsize); if (err < 0) { fprintf(stderr, "analogy_config: bufffer configuraiton failed " "(err=%d)\n", err); goto out_a4l_config; } } out_a4l_config: if (fd >= 0) a4l_sys_close(fd); return err; }