Re: [PATCH 3/5] drivers/auxdisplay: senshat: Raspberry Pi Sense HAT display driver

From: Matthias Brugger
Date: Wed Nov 03 2021 - 12:47:46 EST




On 29/10/2021 23:55, Charles Mirabile wrote:
This patch implements control of the 8x8 RGB LED matrix display.

Signed-off-by: Charles Mirabile <cmirabil@xxxxxxxxxx>
Signed-off-by: Mwesigwa Guma <mguma@xxxxxxxxxx>
Signed-off-by: Joel Savitz <jsavitz@xxxxxxxxxx>
---
drivers/auxdisplay/Kconfig | 7 +
drivers/auxdisplay/Makefile | 1 +
drivers/auxdisplay/sensehat-display.c | 258 ++++++++++++++++++++++++++
3 files changed, 266 insertions(+)
create mode 100644 drivers/auxdisplay/sensehat-display.c

[....]
diff --git a/drivers/auxdisplay/sensehat-display.c b/drivers/auxdisplay/sensehat-display.c
new file mode 100644
index 000000000000..5980ad23fd4f
--- /dev/null
+++ b/drivers/auxdisplay/sensehat-display.c
[...]
+
+static ssize_t
+sensehat_display_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_pos)
+{
+ struct sensehat *sensehat = container_of(filp->private_data, struct sensehat, display.mdev);
+ struct sensehat_display *sensehat_display = &sensehat->display;
+ u8 temp[VMEM_SIZE];
+
+ if (*f_pos >= VMEM_SIZE)
+ return -EFBIG;
+ if (*f_pos + count > VMEM_SIZE)
+ count = VMEM_SIZE - *f_pos;
+ if (copy_from_user(temp, buf, count))
+ return -EFAULT;
+ if (mutex_lock_interruptible(&sensehat_display->rw_mtx))
+ return -ERESTARTSYS;
+ memcpy(sensehat_display->vmem + *f_pos, temp, count);

So we copy from buf into temp, from temp into vmem and then we 'transform' vmem into the representation of pixel_data.

As you are implementing the user-space interface, couldn't we just change the pixel representation there and get rid of the transformation?

+ sensehat_update_display(sensehat);
+ *f_pos += count;
+ mutex_unlock(&sensehat_display->rw_mtx);
+ return count;
+}
+
+static long sensehat_display_ioctl(struct file *filp, unsigned int cmd,
+ unsigned long arg)
+{
+ struct sensehat *sensehat = container_of(filp->private_data, struct sensehat, display.mdev);
+ struct sensehat_display *sensehat_display = &sensehat->display;
+ void __user *user_ptr = (void __user *)arg;
+ u8 temp[GAMMA_SIZE];
+ int ret;
+
+ if (mutex_lock_interruptible(&sensehat_display->rw_mtx))
+ return -ERESTARTSYS;
+ switch (cmd) {
+ case SENSEDISP_IOGET_GAMMA:
+ if (copy_to_user(user_ptr, sensehat_display->gamma, GAMMA_SIZE)) {
+ ret = -EFAULT;
+ goto out_unlock;
+ }
+ ret = 0;
+ goto out_unlock;
+ case SENSEDISP_IOSET_GAMMA:
+ if (copy_from_user(temp, user_ptr, GAMMA_SIZE)) {
+ ret = -EFAULT;
+ goto out_unlock;
+ }
+ ret = 0;
+ goto out_update;

That's just a 'break;' correct?

Regards,
Matthias