static ssize_t pfsWrite(struct file *file, const char *buffer, size_t count, loff_t *pffset) { int ret; unsigned int copied; ret = kfifo_from_user(&myfifo, buffer, count, &copied); wc+=1; return ret?ret:copied; }
static ssize_t fifo_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) { int ret; unsigned int copied; if (mutex_lock_interruptible(&write_lock)) return -ERESTARTSYS; ret = kfifo_from_user(&test, buf, count, &copied); mutex_unlock(&write_lock); return ret ? ret : copied; }
static int iio_kfifo_write(struct iio_buffer *r, size_t n, const char __user *buf) { struct iio_kfifo *kf = iio_to_kfifo(r); int ret, copied; mutex_lock(&kf->user_lock); if (!kfifo_initialized(&kf->kf) || n < kfifo_esize(&kf->kf)) ret = -EINVAL; else ret = kfifo_from_user(&kf->kf, buf, n, &copied); mutex_unlock(&kf->user_lock); if (ret) return ret; return copied; }
ssize_t prod_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_pos) { int ret; int copied; pr_info("%s() : FIFO size = %d, count = %d\n", __func__, (int)kfifo_len(&fifo), (int)count); if (down_interruptible(&prod_sem)) return -ERESTARTSYS; while ((int)kfifo_avail(&fifo) <= 0) { /* full */ up(&prod_sem); if (filp->f_flags & O_NONBLOCK) return -EAGAIN; pr_info("%s() : \"%s\" going to sleep\n", __func__, current->comm); if (wait_event_interruptible(prod_que,\ (((int)kfifo_avail(&fifo)) > 0))) { pr_info("%s() wait_event_interruptible() : signal: " "tell the fs layer to handle it\n", __func__); return -ERESTARTSYS; /* signal: inform the fs layer to handle it */ } if (down_interruptible(&prod_sem)) return -ERESTARTSYS; pr_info("%s() : \"%s\" waken from sleep\n", __func__, current->comm); } count = min((int)count, (int)kfifo_avail(&fifo)); pr_info("%s() : \"%s\" data to copy = %li bytes\n", __func__, current->comm, (long)count); ret = kfifo_from_user(&fifo, buf, count, &copied); up(&prod_sem); if (ret < 0) return -EFAULT; pr_info("%s() : \"%s\" copied %d bytes.FIFO new SIZE = %d\n", __func__, current->comm, copied, (int)kfifo_len(&fifo)); pr_info("%s() : \"%s\" waking up consumer processes\n", __func__, current->comm); wake_up_interruptible(&cons_que); return ret ? ret : copied; }
static ssize_t pi433_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_pos) { struct pi433_instance *instance; struct pi433_device *device; int retval; unsigned int required, available, copied; instance = filp->private_data; device = instance->device; /* * check, whether internal buffer (tx thread) is big enough * for requested size */ if (count > MAX_MSG_SIZE) return -EMSGSIZE; /* * write the following sequence into fifo: * - tx_cfg * - size of message * - message */ mutex_lock(&device->tx_fifo_lock); required = sizeof(instance->tx_cfg) + sizeof(size_t) + count; available = kfifo_avail(&device->tx_fifo); if (required > available) { dev_dbg(device->dev, "write to fifo failed: %d bytes required but %d available", required, available); mutex_unlock(&device->tx_fifo_lock); return -EAGAIN; } retval = kfifo_in(&device->tx_fifo, &instance->tx_cfg, sizeof(instance->tx_cfg)); if (retval != sizeof(instance->tx_cfg)) goto abort; retval = kfifo_in(&device->tx_fifo, &count, sizeof(size_t)); if (retval != sizeof(size_t)) goto abort; retval = kfifo_from_user(&device->tx_fifo, buf, count, &copied); if (retval || copied != count) goto abort; mutex_unlock(&device->tx_fifo_lock); /* start transfer */ wake_up_interruptible(&device->tx_wait_queue); dev_dbg(device->dev, "write: generated new msg with %d bytes.", copied); return copied; abort: dev_warn(device->dev, "write to fifo failed, non recoverable: 0x%x", retval); mutex_unlock(&device->tx_fifo_lock); return -EAGAIN; }