/** * mei_amthif_complete - complete amthif callback. * * @cl: host client * @cb: callback block. */ void mei_amthif_complete(struct mei_cl *cl, struct mei_cl_cb *cb) { struct mei_device *dev = cl->dev; if (cb->fop_type == MEI_FOP_WRITE) { if (!cb->status) { dev->iamthif_stall_timer = MEI_IAMTHIF_STALL_TIMER; mei_io_cb_free(cb); return; } /* * in case of error enqueue the write cb to complete read list * so it can be propagated to the reader */ list_add_tail(&cb->list, &cl->rd_completed); wake_up_interruptible(&cl->rx_wait); return; } if (!dev->iamthif_canceled) { dev->iamthif_state = MEI_IAMTHIF_READ_COMPLETE; dev->iamthif_stall_timer = 0; list_add_tail(&cb->list, &cl->rd_completed); dev_dbg(dev->dev, "amthif read completed\n"); } else { mei_amthif_run_next_cmd(dev); } dev_dbg(dev->dev, "completing amthif call back.\n"); wake_up_interruptible(&cl->rx_wait); }
/** * mei_clear_lists - removes all callbacks associated with file * * @dev: device structure * @file: file structure * * mei_clear_lists is called to clear resources associated with file * when application calls close function or Ctrl-C was pressed * * Return: true if callback removed from the list, false otherwise */ static bool mei_clear_lists(struct mei_device *dev, const struct file *file) { bool removed = false; struct mei_cl *cl = &dev->iamthif_cl; /* remove callbacks associated with a file */ mei_clear_list(dev, file, &dev->amthif_cmd_list.list); if (mei_clear_list(dev, file, &cl->rd_completed)) removed = true; mei_clear_list(dev, file, &dev->ctrl_rd_list.list); if (mei_clear_list(dev, file, &dev->ctrl_wr_list.list)) removed = true; if (mei_clear_list(dev, file, &dev->write_waiting_list.list)) removed = true; if (mei_clear_list(dev, file, &dev->write_list.list)) removed = true; /* check if iamthif_current_cb not NULL */ if (dev->iamthif_current_cb && !removed) { /* check file and iamthif current cb association */ if (dev->iamthif_current_cb->fp == file) { /* remove cb */ mei_io_cb_free(dev->iamthif_current_cb); dev->iamthif_current_cb = NULL; removed = true; } } return removed; }
/** * mei_amthif_read_start - queue message for sending read credential * * @cl: host client * @file: file pointer of message recipient * * Return: 0 on success, <0 on failure. */ static int mei_amthif_read_start(struct mei_cl *cl, const struct file *file) { struct mei_device *dev = cl->dev; struct mei_cl_cb *cb; int rets; cb = mei_io_cb_init(cl, MEI_FOP_READ, file); if (!cb) { rets = -ENOMEM; goto err; } rets = mei_io_cb_alloc_buf(cb, mei_cl_mtu(cl)); if (rets) goto err; list_add_tail(&cb->list, &dev->ctrl_wr_list.list); dev->iamthif_state = MEI_IAMTHIF_READING; dev->iamthif_fp = cb->fp; dev->iamthif_current_cb = cb; return 0; err: mei_io_cb_free(cb); return rets; }
/** * mei_release - the release function * * @inode: pointer to inode structure * @file: pointer to file structure * * returns 0 on success, <0 on error */ static int mei_release(struct inode *inode, struct file *file) { struct mei_cl *cl = file->private_data; struct mei_cl_cb *cb; struct mei_device *dev; int rets = 0; if (WARN_ON(!cl || !cl->dev)) return -ENODEV; dev = cl->dev; mutex_lock(&dev->device_lock); if (cl == &dev->iamthif_cl) { rets = mei_amthif_release(dev, file); goto out; } if (cl->state == MEI_FILE_CONNECTED) { cl->state = MEI_FILE_DISCONNECTING; dev_dbg(&dev->pdev->dev, "disconnecting client host client = %d, " "ME client = %d\n", cl->host_client_id, cl->me_client_id); rets = mei_cl_disconnect(cl); } mei_cl_flush_queues(cl); dev_dbg(&dev->pdev->dev, "remove client host client = %d, ME client = %d\n", cl->host_client_id, cl->me_client_id); mei_cl_unlink(cl); /* free read cb */ cb = NULL; if (cl->read_cb) { cb = mei_cl_find_read_cb(cl); /* Remove entry from read list */ if (cb) list_del(&cb->list); cb = cl->read_cb; cl->read_cb = NULL; } file->private_data = NULL; mei_io_cb_free(cb); kfree(cl); out: mutex_unlock(&dev->device_lock); return rets; }
/** * mei_clear_list - removes all callbacks associated with file * from mei_cb_list * * @dev: device structure. * @file: file structure * @mei_cb_list: callbacks list * * mei_clear_list is called to clear resources associated with file * when application calls close function or Ctrl-C was pressed * * Return: true if callback removed from the list, false otherwise */ static bool mei_clear_list(struct mei_device *dev, const struct file *file, struct list_head *mei_cb_list) { struct mei_cl *cl = &dev->iamthif_cl; struct mei_cl_cb *cb, *next; bool removed = false; /* list all list member */ list_for_each_entry_safe(cb, next, mei_cb_list, list) { /* check if list member associated with a file */ if (file == cb->fp) { /* check if cb equal to current iamthif cb */ if (dev->iamthif_current_cb == cb) { dev->iamthif_current_cb = NULL; /* send flow control to iamthif client */ mei_hbm_cl_flow_control_req(dev, cl); } /* free all allocated buffers */ mei_io_cb_free(cb); removed = true; } } return removed; }
/** * mei_write - the write function. * * @file: pointer to file structure * @ubuf: pointer to user buffer * @length: buffer length * @offset: data offset in buffer * * returns >=0 data length on success , <0 on error */ static ssize_t mei_write(struct file *file, const char __user *ubuf, size_t length, loff_t *offset) { struct mei_cl *cl = file->private_data; struct mei_cl_cb *write_cb = NULL; struct mei_device *dev; unsigned long timeout = 0; int rets; int id; if (WARN_ON(!cl || !cl->dev)) return -ENODEV; dev = cl->dev; mutex_lock(&dev->device_lock); if (dev->dev_state != MEI_DEV_ENABLED) { rets = -ENODEV; goto out; } id = mei_me_cl_by_id(dev, cl->me_client_id); if (id < 0) { rets = -ENOTTY; goto out; } if (length == 0) { rets = 0; goto out; } if (length > dev->me_clients[id].props.max_msg_length) { rets = -EFBIG; goto out; } if (cl->state != MEI_FILE_CONNECTED) { dev_err(&dev->pdev->dev, "host client = %d, is not connected to ME client = %d", cl->host_client_id, cl->me_client_id); rets = -ENODEV; goto out; } if (cl == &dev->iamthif_cl) { write_cb = mei_amthif_find_read_list_entry(dev, file); if (write_cb) { timeout = write_cb->read_time + mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER); if (time_after(jiffies, timeout) || cl->reading_state == MEI_READ_COMPLETE) { *offset = 0; list_del(&write_cb->list); mei_io_cb_free(write_cb); write_cb = NULL; } } } /* free entry used in read */ if (cl->reading_state == MEI_READ_COMPLETE) { *offset = 0; write_cb = mei_cl_find_read_cb(cl); if (write_cb) { list_del(&write_cb->list); mei_io_cb_free(write_cb); write_cb = NULL; cl->reading_state = MEI_IDLE; cl->read_cb = NULL; } } else if (cl->reading_state == MEI_IDLE) *offset = 0; write_cb = mei_io_cb_init(cl, file); if (!write_cb) { dev_err(&dev->pdev->dev, "write cb allocation failed\n"); rets = -ENOMEM; goto out; } rets = mei_io_cb_alloc_req_buf(write_cb, length); if (rets) goto out; rets = copy_from_user(write_cb->request_buffer.data, ubuf, length); if (rets) { dev_dbg(&dev->pdev->dev, "failed to copy data from userland\n"); rets = -EFAULT; goto out; } if (cl == &dev->iamthif_cl) { rets = mei_amthif_write(dev, write_cb); if (rets) { dev_err(&dev->pdev->dev, "amthif write failed with status = %d\n", rets); goto out; } mutex_unlock(&dev->device_lock); return length; } rets = mei_cl_write(cl, write_cb, false); out: mutex_unlock(&dev->device_lock); if (rets < 0) mei_io_cb_free(write_cb); return rets; }
/** * mei_read - the read function. * * @file: pointer to file structure * @ubuf: pointer to user buffer * @length: buffer length * @offset: data offset in buffer * * returns >=0 data length on success , <0 on error */ static ssize_t mei_read(struct file *file, char __user *ubuf, size_t length, loff_t *offset) { struct mei_cl *cl = file->private_data; struct mei_cl_cb *cb_pos = NULL; struct mei_cl_cb *cb = NULL; struct mei_device *dev; int rets; int err; if (WARN_ON(!cl || !cl->dev)) return -ENODEV; dev = cl->dev; mutex_lock(&dev->device_lock); if (dev->dev_state != MEI_DEV_ENABLED) { rets = -ENODEV; goto out; } if (length == 0) { rets = 0; goto out; } if (cl == &dev->iamthif_cl) { rets = mei_amthif_read(dev, file, ubuf, length, offset); goto out; } if (cl->read_cb) { cb = cl->read_cb; /* read what left */ if (cb->buf_idx > *offset) goto copy_buffer; /* offset is beyond buf_idx we have no more data return 0 */ if (cb->buf_idx > 0 && cb->buf_idx <= *offset) { rets = 0; goto free; } /* Offset needs to be cleaned for contiguous reads*/ if (cb->buf_idx == 0 && *offset > 0) *offset = 0; } else if (*offset > 0) { *offset = 0; } err = mei_cl_read_start(cl, length); if (err && err != -EBUSY) { dev_dbg(&dev->pdev->dev, "mei start read failure with status = %d\n", err); rets = err; goto out; } if (MEI_READ_COMPLETE != cl->reading_state && !waitqueue_active(&cl->rx_wait)) { if (file->f_flags & O_NONBLOCK) { rets = -EAGAIN; goto out; } mutex_unlock(&dev->device_lock); if (wait_event_interruptible(cl->rx_wait, MEI_READ_COMPLETE == cl->reading_state || mei_cl_is_transitioning(cl))) { if (signal_pending(current)) return -EINTR; return -ERESTARTSYS; } mutex_lock(&dev->device_lock); if (mei_cl_is_transitioning(cl)) { rets = -EBUSY; goto out; } } cb = cl->read_cb; if (!cb) { rets = -ENODEV; goto out; } if (cl->reading_state != MEI_READ_COMPLETE) { rets = 0; goto out; } /* now copy the data to user space */ copy_buffer: dev_dbg(&dev->pdev->dev, "buf.size = %d buf.idx= %ld\n", cb->response_buffer.size, cb->buf_idx); if (length == 0 || ubuf == NULL || *offset > cb->buf_idx) { rets = -EMSGSIZE; goto free; } /* length is being truncated to PAGE_SIZE, * however buf_idx may point beyond that */ length = min_t(size_t, length, cb->buf_idx - *offset); if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length)) { dev_dbg(&dev->pdev->dev, "failed to copy data to userland\n"); rets = -EFAULT; goto free; } rets = length; *offset += length; if ((unsigned long)*offset < cb->buf_idx) goto out; free: cb_pos = mei_cl_find_read_cb(cl); /* Remove entry from read list */ if (cb_pos) list_del(&cb_pos->list); mei_io_cb_free(cb); cl->reading_state = MEI_IDLE; cl->read_cb = NULL; out: dev_dbg(&dev->pdev->dev, "end mei read rets= %d\n", rets); mutex_unlock(&dev->device_lock); return rets; }
/** * mei_write - the write function. * * @file: pointer to file structure * @ubuf: pointer to user buffer * @length: buffer length * @offset: data offset in buffer * * Return: >=0 data length on success , <0 on error */ static ssize_t mei_write(struct file *file, const char __user *ubuf, size_t length, loff_t *offset) { struct mei_cl *cl = file->private_data; struct mei_cl_cb *cb; struct mei_device *dev; int rets; if (WARN_ON(!cl || !cl->dev)) return -ENODEV; dev = cl->dev; mutex_lock(&dev->device_lock); if (dev->dev_state != MEI_DEV_ENABLED) { rets = -ENODEV; goto out; } if (!mei_cl_is_connected(cl)) { cl_err(dev, cl, "is not connected"); rets = -ENODEV; goto out; } if (!mei_me_cl_is_active(cl->me_cl)) { rets = -ENOTTY; goto out; } if (length > mei_cl_mtu(cl)) { rets = -EFBIG; goto out; } if (length == 0) { rets = 0; goto out; } *offset = 0; cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, file); if (!cb) { rets = -ENOMEM; goto out; } rets = copy_from_user(cb->buf.data, ubuf, length); if (rets) { dev_dbg(dev->dev, "failed to copy data from userland\n"); rets = -EFAULT; mei_io_cb_free(cb); goto out; } if (cl == &dev->iamthif_cl) { rets = mei_amthif_write(cl, cb); if (!rets) rets = length; goto out; } rets = mei_cl_write(cl, cb); out: mutex_unlock(&dev->device_lock); return rets; }
/** * mei_read - the read function. * * @file: pointer to file structure * @ubuf: pointer to user buffer * @length: buffer length * @offset: data offset in buffer * * Return: >=0 data length on success , <0 on error */ static ssize_t mei_read(struct file *file, char __user *ubuf, size_t length, loff_t *offset) { struct mei_cl *cl = file->private_data; struct mei_device *dev; struct mei_cl_cb *cb = NULL; bool nonblock = !!(file->f_flags & O_NONBLOCK); int rets; if (WARN_ON(!cl || !cl->dev)) return -ENODEV; dev = cl->dev; mutex_lock(&dev->device_lock); if (dev->dev_state != MEI_DEV_ENABLED) { rets = -ENODEV; goto out; } if (length == 0) { rets = 0; goto out; } if (ubuf == NULL) { rets = -EMSGSIZE; goto out; } cb = mei_cl_read_cb(cl, file); if (cb) goto copy_buffer; if (*offset > 0) *offset = 0; rets = mei_cl_read_start(cl, length, file); if (rets && rets != -EBUSY) { cl_dbg(dev, cl, "mei start read failure status = %d\n", rets); goto out; } if (nonblock) { rets = -EAGAIN; goto out; } if (rets == -EBUSY && !mei_cl_enqueue_ctrl_wr_cb(cl, length, MEI_FOP_READ, file)) { rets = -ENOMEM; goto out; } do { mutex_unlock(&dev->device_lock); if (wait_event_interruptible(cl->rx_wait, (!list_empty(&cl->rd_completed)) || (!mei_cl_is_connected(cl)))) { if (signal_pending(current)) return -EINTR; return -ERESTARTSYS; } mutex_lock(&dev->device_lock); if (!mei_cl_is_connected(cl)) { rets = -ENODEV; goto out; } cb = mei_cl_read_cb(cl, file); } while (!cb); copy_buffer: /* now copy the data to user space */ if (cb->status) { rets = cb->status; cl_dbg(dev, cl, "read operation failed %d\n", rets); goto free; } cl_dbg(dev, cl, "buf.size = %zu buf.idx = %zu offset = %lld\n", cb->buf.size, cb->buf_idx, *offset); if (*offset >= cb->buf_idx) { rets = 0; goto free; } /* length is being truncated to PAGE_SIZE, * however buf_idx may point beyond that */ length = min_t(size_t, length, cb->buf_idx - *offset); if (copy_to_user(ubuf, cb->buf.data + *offset, length)) { dev_dbg(dev->dev, "failed to copy data to userland\n"); rets = -EFAULT; goto free; } rets = length; *offset += length; /* not all data was read, keep the cb */ if (*offset < cb->buf_idx) goto out; free: mei_io_cb_free(cb); *offset = 0; out: cl_dbg(dev, cl, "end mei read rets = %d\n", rets); mutex_unlock(&dev->device_lock); return rets; }
/** * mei_write - the write function. * * @file: pointer to file structure * @ubuf: pointer to user buffer * @length: buffer length * @offset: data offset in buffer * * Return: >=0 data length on success , <0 on error */ static ssize_t mei_write(struct file *file, const char __user *ubuf, size_t length, loff_t *offset) { struct mei_cl *cl = file->private_data; struct mei_cl_cb *cb; struct mei_device *dev; int rets; if (WARN_ON(!cl || !cl->dev)) return -ENODEV; dev = cl->dev; mutex_lock(&dev->device_lock); if (dev->dev_state != MEI_DEV_ENABLED) { rets = -ENODEV; goto out; } if (!mei_cl_is_connected(cl)) { cl_err(dev, cl, "is not connected"); rets = -ENODEV; goto out; } if (!mei_me_cl_is_active(cl->me_cl)) { rets = -ENOTTY; goto out; } if (length > mei_cl_mtu(cl)) { rets = -EFBIG; goto out; } if (length == 0) { rets = 0; goto out; } while (cl->tx_cb_queued >= dev->tx_queue_limit) { if (file->f_flags & O_NONBLOCK) { rets = -EAGAIN; goto out; } mutex_unlock(&dev->device_lock); rets = wait_event_interruptible(cl->tx_wait, cl->writing_state == MEI_WRITE_COMPLETE || (!mei_cl_is_connected(cl))); mutex_lock(&dev->device_lock); if (rets) { if (signal_pending(current)) rets = -EINTR; goto out; } if (!mei_cl_is_connected(cl)) { rets = -ENODEV; goto out; } } *offset = 0; cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, file); if (!cb) { rets = -ENOMEM; goto out; } rets = copy_from_user(cb->buf.data, ubuf, length); if (rets) { dev_dbg(dev->dev, "failed to copy data from userland\n"); rets = -EFAULT; mei_io_cb_free(cb); goto out; } rets = mei_cl_write(cl, cb); out: mutex_unlock(&dev->device_lock); return rets; }
/** * mei_read - the read function. * * @file: pointer to file structure * @ubuf: pointer to user buffer * @length: buffer length * @offset: data offset in buffer * * returns >=0 data length on success , <0 on error */ static ssize_t mei_read(struct file *file, char __user *ubuf, size_t length, loff_t *offset) { struct mei_cl *cl = file->private_data; struct mei_cl_cb *cb_pos = NULL; struct mei_cl_cb *cb = NULL; struct mei_device *dev; int i; int rets; int err; if (WARN_ON(!cl || !cl->dev)) return -ENODEV; dev = cl->dev; mutex_lock(&dev->device_lock); if (dev->dev_state != MEI_DEV_ENABLED) { rets = -ENODEV; goto out; } if ((cl->sm_state & MEI_WD_STATE_INDEPENDENCE_MSG_SENT) == 0) { /* Do not allow to read watchdog client */ i = mei_me_cl_by_uuid(dev, &mei_wd_guid); if (i >= 0) { struct mei_me_client *me_client = &dev->me_clients[i]; if (cl->me_client_id == me_client->client_id) { rets = -EBADF; goto out; } } } else { cl->sm_state &= ~MEI_WD_STATE_INDEPENDENCE_MSG_SENT; } if (cl == &dev->iamthif_cl) { rets = mei_amthif_read(dev, file, ubuf, length, offset); goto out; } if (cl->read_cb && cl->read_cb->buf_idx > *offset) { cb = cl->read_cb; goto copy_buffer; } else if (cl->read_cb && cl->read_cb->buf_idx > 0 && cl->read_cb->buf_idx <= *offset) { cb = cl->read_cb; rets = 0; goto free; } else if ((!cl->read_cb || !cl->read_cb->buf_idx) && *offset > 0) { /*Offset needs to be cleaned for contiguous reads*/ *offset = 0; rets = 0; goto out; } err = mei_cl_read_start(cl, length); if (err && err != -EBUSY) { dev_dbg(&dev->pdev->dev, "mei start read failure with status = %d\n", err); rets = err; goto out; } if (MEI_READ_COMPLETE != cl->reading_state && !waitqueue_active(&cl->rx_wait)) { if (file->f_flags & O_NONBLOCK) { rets = -EAGAIN; goto out; } mutex_unlock(&dev->device_lock); if (wait_event_interruptible(cl->rx_wait, (MEI_READ_COMPLETE == cl->reading_state || MEI_FILE_INITIALIZING == cl->state || MEI_FILE_DISCONNECTED == cl->state || MEI_FILE_DISCONNECTING == cl->state))) { if (signal_pending(current)) return -EINTR; return -ERESTARTSYS; } mutex_lock(&dev->device_lock); if (MEI_FILE_INITIALIZING == cl->state || MEI_FILE_DISCONNECTED == cl->state || MEI_FILE_DISCONNECTING == cl->state) { rets = -EBUSY; goto out; } } cb = cl->read_cb; if (!cb) { rets = -ENODEV; goto out; } if (cl->reading_state != MEI_READ_COMPLETE) { rets = 0; goto out; } /* now copy the data to user space */ copy_buffer: dev_dbg(&dev->pdev->dev, "buf.size = %d buf.idx= %ld\n", cb->response_buffer.size, cb->buf_idx); if (length == 0 || ubuf == NULL || *offset > cb->buf_idx) { rets = -EMSGSIZE; goto free; } /* length is being truncated to PAGE_SIZE, * however buf_idx may point beyond that */ length = min_t(size_t, length, cb->buf_idx - *offset); if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length)) { rets = -EFAULT; goto free; } rets = length; *offset += length; if ((unsigned long)*offset < cb->buf_idx) goto out; free: cb_pos = mei_cl_find_read_cb(cl); /* Remove entry from read list */ if (cb_pos) list_del(&cb_pos->list); mei_io_cb_free(cb); cl->reading_state = MEI_IDLE; cl->read_cb = NULL; out: dev_dbg(&dev->pdev->dev, "end mei read rets= %d\n", rets); mutex_unlock(&dev->device_lock); return rets; }
/** * mei_amthif_read - read data from AMTHIF client * * @dev: the device structure * @file: pointer to file object * @ubuf: pointer to user data in user space * @length: data length to read * @offset: data read offset * * Locking: called under "dev->device_lock" lock * * Return: * returned data length on success, * zero if no data to read, * negative on failure. */ int mei_amthif_read(struct mei_device *dev, struct file *file, char __user *ubuf, size_t length, loff_t *offset) { struct mei_cl *cl = file->private_data; struct mei_cl_cb *cb; int rets; int wait_ret; dev_dbg(dev->dev, "checking amthif data\n"); cb = mei_cl_read_cb(cl, file); /* Check for if we can block or not*/ if (cb == NULL && file->f_flags & O_NONBLOCK) return -EAGAIN; dev_dbg(dev->dev, "waiting for amthif data\n"); while (cb == NULL) { /* unlock the Mutex */ mutex_unlock(&dev->device_lock); wait_ret = wait_event_interruptible(cl->rx_wait, !list_empty(&cl->rd_completed) || !mei_cl_is_connected(cl)); /* Locking again the Mutex */ mutex_lock(&dev->device_lock); if (wait_ret) return -ERESTARTSYS; if (!mei_cl_is_connected(cl)) { rets = -ENODEV; goto out; } cb = mei_cl_read_cb(cl, file); } if (cb->status) { rets = cb->status; dev_dbg(dev->dev, "read operation failed %d\n", rets); goto free; } dev_dbg(dev->dev, "Got amthif data\n"); /* if the whole message will fit remove it from the list */ if (cb->buf_idx >= *offset && length >= (cb->buf_idx - *offset)) list_del_init(&cb->list); else if (cb->buf_idx <= *offset) { /* end of the message has been reached */ list_del_init(&cb->list); rets = 0; goto free; } /* else means that not full buffer will be read and do not * remove message from deletion list */ dev_dbg(dev->dev, "amthif cb->buf.size - %zu cb->buf_idx - %zu\n", cb->buf.size, cb->buf_idx); /* length is being truncated to PAGE_SIZE, however, * the buf_idx may point beyond */ length = min_t(size_t, length, (cb->buf_idx - *offset)); if (copy_to_user(ubuf, cb->buf.data + *offset, length)) { dev_dbg(dev->dev, "failed to copy data to userland\n"); rets = -EFAULT; } else { rets = length; if ((*offset + length) < cb->buf_idx) { *offset += length; goto out; } } free: dev_dbg(dev->dev, "free amthif cb memory.\n"); *offset = 0; mei_io_cb_free(cb); out: return rets; }
/** * mei_write - the write function. * * @file: pointer to file structure * @ubuf: pointer to user buffer * @length: buffer length * @offset: data offset in buffer * * Return: >=0 data length on success , <0 on error */ static ssize_t mei_write(struct file *file, const char __user *ubuf, size_t length, loff_t *offset) { struct mei_cl *cl = file->private_data; struct mei_cl_cb *write_cb = NULL; struct mei_device *dev; unsigned long timeout = 0; int rets; if (WARN_ON(!cl || !cl->dev)) return -ENODEV; dev = cl->dev; mutex_lock(&dev->device_lock); if (dev->dev_state != MEI_DEV_ENABLED) { rets = -ENODEV; goto out; } if (!mei_cl_is_connected(cl)) { cl_err(dev, cl, "is not connected"); rets = -ENODEV; goto out; } if (!mei_me_cl_is_active(cl->me_cl)) { rets = -ENOTTY; goto out; } if (length > mei_cl_mtu(cl)) { rets = -EFBIG; goto out; } if (length == 0) { rets = 0; goto out; } if (cl == &dev->iamthif_cl) { write_cb = mei_amthif_find_read_list_entry(dev, file); if (write_cb) { timeout = write_cb->read_time + mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER); if (time_after(jiffies, timeout)) { *offset = 0; mei_io_cb_free(write_cb); write_cb = NULL; } } } *offset = 0; write_cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, file); if (!write_cb) { rets = -ENOMEM; goto out; } rets = copy_from_user(write_cb->buf.data, ubuf, length); if (rets) { dev_dbg(dev->dev, "failed to copy data from userland\n"); rets = -EFAULT; goto out; } if (cl == &dev->iamthif_cl) { rets = mei_amthif_write(cl, write_cb); if (rets) { dev_err(dev->dev, "amthif write failed with status = %d\n", rets); goto out; } mutex_unlock(&dev->device_lock); return length; } rets = mei_cl_write(cl, write_cb, false); out: mutex_unlock(&dev->device_lock); if (rets < 0) mei_io_cb_free(write_cb); return rets; }
/** * mei_read - the read function. * * @file: pointer to file structure * @ubuf: pointer to user buffer * @length: buffer length * @offset: data offset in buffer * * Return: >=0 data length on success , <0 on error */ static ssize_t mei_read(struct file *file, char __user *ubuf, size_t length, loff_t *offset) { struct mei_cl *cl = file->private_data; struct mei_device *dev; struct mei_cl_cb *cb = NULL; int rets; int err; if (WARN_ON(!cl || !cl->dev)) return -ENODEV; dev = cl->dev; mutex_lock(&dev->device_lock); if (dev->dev_state != MEI_DEV_ENABLED) { rets = -ENODEV; goto out; } if (length == 0) { rets = 0; goto out; } if (cl == &dev->iamthif_cl) { rets = mei_amthif_read(dev, file, ubuf, length, offset); goto out; } cb = mei_cl_read_cb(cl, file); if (cb) { /* read what left */ if (cb->buf_idx > *offset) goto copy_buffer; /* offset is beyond buf_idx we have no more data return 0 */ if (cb->buf_idx > 0 && cb->buf_idx <= *offset) { rets = 0; goto free; } /* Offset needs to be cleaned for contiguous reads*/ if (cb->buf_idx == 0 && *offset > 0) *offset = 0; } else if (*offset > 0) { *offset = 0; } err = mei_cl_read_start(cl, length, file); if (err && err != -EBUSY) { cl_dbg(dev, cl, "mei start read failure status = %d\n", err); rets = err; goto out; } if (list_empty(&cl->rd_completed) && !waitqueue_active(&cl->rx_wait)) { if (file->f_flags & O_NONBLOCK) { rets = -EAGAIN; goto out; } mutex_unlock(&dev->device_lock); if (wait_event_interruptible(cl->rx_wait, (!list_empty(&cl->rd_completed)) || (!mei_cl_is_connected(cl)))) { if (signal_pending(current)) return -EINTR; return -ERESTARTSYS; } mutex_lock(&dev->device_lock); if (!mei_cl_is_connected(cl)) { rets = -EBUSY; goto out; } } cb = mei_cl_read_cb(cl, file); if (!cb) { if (mei_cl_is_fixed_address(cl) && dev->allow_fixed_address) { cb = mei_cl_read_cb(cl, NULL); if (cb) goto copy_buffer; } rets = 0; goto out; } copy_buffer: /* now copy the data to user space */ if (cb->status) { rets = cb->status; cl_dbg(dev, cl, "read operation failed %d\n", rets); goto free; } cl_dbg(dev, cl, "buf.size = %d buf.idx = %ld\n", cb->buf.size, cb->buf_idx); if (length == 0 || ubuf == NULL || *offset > cb->buf_idx) { rets = -EMSGSIZE; goto free; } /* length is being truncated to PAGE_SIZE, * however buf_idx may point beyond that */ length = min_t(size_t, length, cb->buf_idx - *offset); if (copy_to_user(ubuf, cb->buf.data + *offset, length)) { dev_dbg(dev->dev, "failed to copy data to userland\n"); rets = -EFAULT; goto free; } rets = length; *offset += length; if ((unsigned long)*offset < cb->buf_idx) goto out; free: mei_io_cb_free(cb); out: cl_dbg(dev, cl, "end mei read rets = %d\n", rets); mutex_unlock(&dev->device_lock); return rets; }