int a4l_assign_driver(a4l_cxt_t * cxt, a4l_drv_t * drv, a4l_lnkdesc_t * link_arg) { int ret = 0; a4l_dev_t *dev = a4l_get_dev(cxt); __a4l_dbg(1, core_dbg, "a4l_assign_driver: minor=%d\n", a4l_get_minor(cxt)); dev->driver = drv; if (drv->privdata_size == 0) __a4l_dbg(1, core_dbg, "a4l_assign_driver: warning! " "the field priv will not be usable\n"); else { INIT_LIST_HEAD(&dev->subdvsq); dev->priv = rtdm_malloc(drv->privdata_size); if (dev->priv == NULL && drv->privdata_size != 0) { __a4l_err("a4l_assign_driver: " "call(alloc) failed\n"); ret = -ENOMEM; goto out_assign_driver; } /* Initialize the private data even if it not our role (the driver should do it), that may prevent hard to find bugs */ memset(dev->priv, 0, drv->privdata_size); } if ((ret = drv->attach(dev, link_arg)) != 0) __a4l_err("a4l_assign_driver: " "call(drv->attach) failed (ret=%d)\n", ret); out_assign_driver: /* Increments module's count */ if (ret == 0 && (!try_module_get(drv->owner))) { __a4l_err("a4l_assign_driver: " "driver's owner field wrongly set\n"); ret = -ENODEV; } if (ret != 0 && dev->priv != NULL) { rtdm_free(dev->priv); dev->driver = NULL; } return ret; }
int a4l_ioctl_devinfo(a4l_cxt_t * cxt, void *arg) { a4l_dvinfo_t info; a4l_dev_t *dev = a4l_get_dev(cxt); __a4l_dbg(1, core_dbg, "a4l_ioctl_devinfo: minor=%d\n", a4l_get_minor(cxt)); memset(&info, 0, sizeof(a4l_dvinfo_t)); if (test_bit(A4L_DEV_ATTACHED, &dev->flags)) { int len = (strlen(dev->driver->board_name) > A4L_NAMELEN) ? A4L_NAMELEN : strlen(dev->driver->board_name); memcpy(info.board_name, dev->driver->board_name, len); info.nb_subd = dev->transfer.nb_subd; info.idx_read_subd = dev->transfer.idx_read_subd; info.idx_write_subd = dev->transfer.idx_write_subd; } if (rtdm_safe_copy_to_user(cxt->user_info, arg, &info, sizeof(a4l_dvinfo_t)) != 0) return -EFAULT; return 0; }
int a4l_device_attach(a4l_cxt_t * cxt, void *arg) { int ret = 0; a4l_lnkdesc_t link_arg; a4l_drv_t *drv = NULL; __a4l_dbg(1, core_dbg, "a4l_device_attach: minor=%d\n", a4l_get_minor(cxt)); if ((ret = a4l_fill_lnkdesc(cxt, &link_arg, arg)) != 0) goto out_attach; if ((ret = a4l_lct_drv(link_arg.bname, &drv)) != 0) { __a4l_err("a4l_device_attach: " "cannot find board name %s\n", link_arg.bname); goto out_attach; } if ((ret = a4l_assign_driver(cxt, drv, &link_arg)) != 0) goto out_attach; out_attach: a4l_free_lnkdesc(cxt, &link_arg); return ret; }
int a4l_release_driver(a4l_cxt_t * cxt) { int ret = 0; a4l_dev_t *dev = a4l_get_dev(cxt); __a4l_dbg(1, core_dbg, "a4l_release_driver: minor=%d\n", a4l_get_minor(cxt)); if ((ret = dev->driver->detach(dev)) != 0) goto out_release_driver; /* Decrease module's count so as to allow module unloading */ module_put(dev->driver->owner); /* In case, the driver developer did not free the subdevices */ while (&dev->subdvsq != dev->subdvsq.next) { struct list_head *this = dev->subdvsq.next; a4l_subd_t *tmp = list_entry(this, a4l_subd_t, list); list_del(this); rtdm_free(tmp); } /* Free the private field */ rtdm_free(dev->priv); dev->driver = NULL; out_release_driver: return ret; }
void a4l_free_lnkdesc(a4l_cxt_t * cxt, a4l_lnkdesc_t * link_arg) { __a4l_dbg(1, core_dbg, "a4l_free_lnkdesc: minor=%d\n", a4l_get_minor(cxt)); if (link_arg->bname != NULL) rtdm_free(link_arg->bname); if (link_arg->opts != NULL) rtdm_free(link_arg->opts); }
int a4l_ioctl_devcfg(a4l_cxt_t * cxt, void *arg) { int ret = 0; __a4l_dbg(1, core_dbg, "a4l_ioctl_devcfg: minor=%d\n", a4l_get_minor(cxt)); if (a4l_test_rt() != 0) return -EPERM; if (arg == NULL) { /* Basic checking */ if (!test_bit (A4L_DEV_ATTACHED, &(a4l_get_dev(cxt)->flags))) { __a4l_err("a4l_ioctl_devcfg: " "free device, no driver to detach\n"); return -EINVAL; } /* Removes the related proc file */ a4l_proc_detach(cxt); /* Frees the transfer structure and its related data */ if ((ret = a4l_cleanup_transfer(cxt)) != 0) return ret; /* Frees the device and the driver from each other */ if ((ret = a4l_device_detach(cxt)) == 0) clear_bit(A4L_DEV_ATTACHED, &(a4l_get_dev(cxt)->flags)); } else { /* Basic checking */ if (test_bit (A4L_DEV_ATTACHED, &(a4l_get_dev(cxt)->flags))) { __a4l_err("a4l_ioctl_devcfg: " "linked device, cannot attach more driver\n"); return -EINVAL; } /* Pre-initialization of the transfer structure */ a4l_presetup_transfer(cxt); /* Links the device with the driver */ if ((ret = a4l_device_attach(cxt, arg)) != 0) return ret; /* Creates the transfer structure and the related proc file */ if ((ret = a4l_setup_transfer(cxt)) != 0 || (ret = a4l_proc_attach(cxt)) != 0) a4l_device_detach(cxt); else set_bit(A4L_DEV_ATTACHED, &(a4l_get_dev(cxt)->flags)); } return ret; }
/* --- Command descriptor management functions --- */ int a4l_fill_cmddesc(struct a4l_device_context *cxt, struct a4l_cmd_desc *desc, unsigned int **chan_descs, void *arg) { unsigned int *tmpchans = NULL; int ret = 0; ret = rtdm_safe_copy_from_user(rtdm_private_to_fd(cxt), desc, arg, sizeof(struct a4l_cmd_desc)); if (ret != 0) goto out_cmddesc; if (desc->nb_chan == 0) { ret = -EINVAL; goto out_cmddesc; } tmpchans = rtdm_malloc(desc->nb_chan * sizeof(unsigned int)); if (tmpchans == NULL) { ret = -ENOMEM; goto out_cmddesc; } ret = rtdm_safe_copy_from_user(rtdm_private_to_fd(cxt), tmpchans, desc->chan_descs, desc->nb_chan * sizeof(unsigned int)); if (ret != 0) { __a4l_err("%s invalid arguments \n", __FUNCTION__); goto out_cmddesc; } *chan_descs = desc->chan_descs; desc->chan_descs = tmpchans; __a4l_dbg(1, core_dbg, "desc dump: \n"); __a4l_dbg(1, core_dbg, "\t->idx_subd=%u\n", desc->idx_subd); __a4l_dbg(1, core_dbg, "\t->flags=%lu\n", desc->flags); __a4l_dbg(1, core_dbg, "\t->nb_chan=%u\n", desc->nb_chan); __a4l_dbg(1, core_dbg, "\t->chan_descs=0x%x\n", *desc->chan_descs); __a4l_dbg(1, core_dbg, "\t->data_len=%u\n", desc->data_len); __a4l_dbg(1, core_dbg, "\t->pdata=0x%p\n", desc->data); out_cmddesc: if (ret != 0) { __a4l_err("a4l_fill_cmddesc: %d \n", ret); if (tmpchans != NULL) rtdm_free(tmpchans); desc->chan_descs = NULL; } return ret; }
int a4l_device_detach(a4l_cxt_t * cxt) { a4l_dev_t *dev = a4l_get_dev(cxt); __a4l_dbg(1, core_dbg, "a4l_device_detach: minor=%d\n", a4l_get_minor(cxt)); if (dev->driver == NULL) { __a4l_err("a4l_device_detach: " "incoherent state, driver not reachable\n"); return -ENXIO; } return a4l_release_driver(cxt); }
int a4l_fill_lnkdesc(a4l_cxt_t * cxt, a4l_lnkdesc_t * link_arg, void *arg) { int ret; char *tmpname = NULL; void *tmpopts = NULL; __a4l_dbg(1, core_dbg, "a4l_fill_lnkdesc: minor=%d\n", a4l_get_minor(cxt)); ret = rtdm_safe_copy_from_user(cxt->user_info, link_arg, arg, sizeof(a4l_lnkdesc_t)); if (ret != 0) { __a4l_err("a4l_fill_lnkdesc: " "call1(copy_from_user) failed\n"); goto out_get_lnkdesc; } if (link_arg->bname_size != 0 && link_arg->bname != NULL) { tmpname = rtdm_malloc(link_arg->bname_size + 1); if (tmpname == NULL) { __a4l_err("a4l_fill_lnkdesc: " "call1(alloc) failed\n"); ret = -ENOMEM; goto out_get_lnkdesc; } tmpname[link_arg->bname_size] = 0; ret = rtdm_safe_copy_from_user(cxt->user_info, tmpname, link_arg->bname, link_arg->bname_size); if (ret != 0) { __a4l_err("a4l_fill_lnkdesc: " "call2(copy_from_user) failed\n"); goto out_get_lnkdesc; } } else { __a4l_err("a4l_fill_lnkdesc: board name missing\n"); ret = -EINVAL; goto out_get_lnkdesc; } if (link_arg->opts_size != 0 && link_arg->opts != NULL) { tmpopts = rtdm_malloc(link_arg->opts_size); if (tmpopts == NULL) { __a4l_err("a4l_fill_lnkdesc: " "call2(alloc) failed\n"); ret = -ENOMEM; goto out_get_lnkdesc; } ret = rtdm_safe_copy_from_user(cxt->user_info, tmpopts, link_arg->opts, link_arg->opts_size); if (ret != 0) { __a4l_err("a4l_fill_lnkdesc: " "call3(copy_from_user) failed\n"); goto out_get_lnkdesc; } } link_arg->bname = tmpname; link_arg->opts = tmpopts; out_get_lnkdesc: if (tmpname == NULL) { link_arg->bname = NULL; link_arg->bname_size = 0; } if (tmpopts == NULL) { link_arg->opts = NULL; link_arg->opts_size = 0; } return ret; }
int a4l_ioctl_cmd(struct a4l_device_context * ctx, void *arg) { int ret = 0, simul_flag = 0; struct a4l_cmd_desc *cmd_desc = NULL; struct a4l_device *dev = a4l_get_dev(ctx); unsigned int *chan_descs, *tmp; struct a4l_subdevice *subd; /* The command launching cannot be done in real-time because of some possible buffer allocations in the drivers */ if (rtdm_in_rt_context()) return -ENOSYS; /* Basically check the device */ if (!test_bit(A4L_DEV_ATTACHED_NR, &dev->flags)) { __a4l_err("a4l_ioctl_cmd: cannot command " "an unattached device\n"); return -EINVAL; } /* Allocates the command */ cmd_desc = (struct a4l_cmd_desc *) rtdm_malloc(sizeof(struct a4l_cmd_desc)); if (cmd_desc == NULL) return -ENOMEM; memset(cmd_desc, 0, sizeof(struct a4l_cmd_desc)); /* Gets the command */ ret = a4l_fill_cmddesc(ctx, cmd_desc, &chan_descs, arg); if (ret != 0) goto out_ioctl_cmd; /* Checks the command */ ret = a4l_check_cmddesc(ctx, cmd_desc); if (ret != 0) goto out_ioctl_cmd; ret = a4l_check_generic_cmdcnt(cmd_desc); if (ret != 0) goto out_ioctl_cmd; ret = a4l_check_specific_cmdcnt(ctx, cmd_desc); if (ret != 0) goto out_ioctl_cmd; __a4l_dbg(1, core_dbg,"1st cmd checks passed\n"); subd = dev->transfer.subds[cmd_desc->idx_subd]; /* Tests the command with the cmdtest function */ if (cmd_desc->flags & A4L_CMD_SIMUL) { simul_flag = 1; if (!subd->do_cmdtest) { __a4l_err("a4l_ioctl_cmd: driver's cmd_test NULL\n"); ret = -EINVAL; goto out_ioctl_cmd; } ret = subd->do_cmdtest(subd, cmd_desc); if (ret != 0) { __a4l_err("a4l_ioctl_cmd: driver's cmd_test failed\n"); goto out_ioctl_cmd; } __a4l_dbg(1, core_dbg, "driver's cmd checks passed\n"); goto out_ioctl_cmd; } /* Gets the transfer system ready */ ret = a4l_setup_buffer(ctx, cmd_desc); if (ret < 0) goto out_ioctl_cmd; /* Eventually launches the command */ ret = subd->do_cmd(subd, cmd_desc); if (ret != 0) { a4l_cancel_buffer(ctx); goto out_ioctl_cmd; } out_ioctl_cmd: if (simul_flag) { /* copy the kernel based descriptor */ tmp = cmd_desc->chan_descs; /* return the user based descriptor */ cmd_desc->chan_descs = chan_descs; rtdm_safe_copy_to_user(rtdm_private_to_fd(ctx), arg, cmd_desc, sizeof(struct a4l_cmd_desc)); /* make sure we release the memory associated to the kernel */ cmd_desc->chan_descs = tmp; } if (ret != 0 || simul_flag == 1) { a4l_free_cmddesc(cmd_desc); rtdm_free(cmd_desc); } return ret; }