Skip to contentSkip to conversation

Regmap over USB for Multifunction USB Device (gpio, display, ...)

RFC In review Noralf Trønnes 7 participants 27 messages Feb 16, 2020 – Feb 29, 2020

Subsystems (7)
DRM drivers USB DRM misc Driver core +3 more subsystems
lore
Share & tools
lore
#0 lore raw

cover Regmap over USB for Multifunction USB Device (gpio, display, ...)

Hi,

A while back I had the idea to turn a Raspberry Pi Zero into a $5 USB to HDMI/SDTV/DSI/DPI display adapter.

Thinking about how to represent the display to the driver I realised that hardware use registers as API. And Linux does have a generic register abstraction: regmap. Furthermore this means that if I can do a regmap over USB implementation, it will be easy to do other functions like gpio, adc and others. After a few iterations trying to understand the USB subsystem and satisfying driver requirements, I now have something that looks promising.

I'm sending out an early version hoping to get feedback especially on the core parts that handles regmap and interrupts.

Overview:

          USB Host          :         USB Device
                            :
            --------------  :  ------------------
----------  | mfd: mud   |  :  | f_mud          |  ----------
| Driver |  --------------  :  |                |  | Driver |
----------  | regmap-usb |  :  | (mud_regmap)   |  ----------
            --------------  :  ------------------

I've attached 2 drivers:
- gpio/pinctrl: is more or less finished
- display: needs a lot more work

USB3 device
I've only tested this with usb2 devices (Pi, BBB) so I should get myself a usb3 gadget capable board. My searching didn't turn up much, so this seems to be quite rare. ROCK960 has USB 3.0 type C OTG but the price is $139 which is a bit expensive for this hobby project. Does anyone know of a cheap board?

Noralf.

Noralf Trønnes (9):
regmap: Add USB support
mfd: Add driver for Multifunction USB Device
usb: gadget: function: Add Multifunction USB Device support
pinctrl: Add Multifunction USB Device pinctrl driver
usb: gadget: function: mud: Add gpio support
regmap: Speed up _regmap_raw_write_impl() for large buffers
drm: Add Multifunction USB Device display driver drm/client: Add drm_client_init_from_id() and drm_client_modeset_set()
usb: gadget: function: mud: Add display support

 drivers/base/regmap/Kconfig              |    8 +-
 drivers/base/regmap/Makefile             |    1 +
 drivers/base/regmap/regmap-usb.c         | 1026 ++++++++++++++++++
 drivers/base/regmap/regmap.c             |   10 +-
 drivers/gpu/drm/Kconfig                  |    2 +
 drivers/gpu/drm/Makefile                 |    1 +
 drivers/gpu/drm/drm_client.c             |   37 +
 drivers/gpu/drm/drm_client_modeset.c     |   52 +
 drivers/gpu/drm/mud/Kconfig              |   18 +
 drivers/gpu/drm/mud/Makefile             |    4 +
 drivers/gpu/drm/mud/mud_drm.c            | 1198 ++++++++++++++++++++++
 drivers/gpu/drm/mud/mud_drm.h            |  137 +++
 drivers/gpu/drm/mud/mud_drm_gadget.c     |  889 ++++++++++++++++
 drivers/mfd/Kconfig                      |    8 +
 drivers/mfd/Makefile                     |    1 +
 drivers/mfd/mud.c                        |  580 +++++++++++
 drivers/pinctrl/Kconfig                  |    9 +
 drivers/pinctrl/Makefile                 |    1 +
 drivers/pinctrl/pinctrl-mud.c            |  657 ++++++++++++
 drivers/pinctrl/pinctrl-mud.h            |   89 ++
 drivers/usb/gadget/Kconfig               |   36 +
 drivers/usb/gadget/function/Makefile     |    6 +
 drivers/usb/gadget/function/f_mud.c      |  913 +++++++++++++++++
 drivers/usb/gadget/function/f_mud.h      |  210 ++++
 drivers/usb/gadget/function/f_mud_drm.c  |  181 ++++
 drivers/usb/gadget/function/f_mud_pins.c |  962 +++++++++++++++++
 drivers/usb/gadget/function/mud_regmap.c |  936 +++++++++++++++++
 include/drm/drm_client.h                 |    4 +
 include/linux/mfd/mud.h                  |   16 +
 include/linux/regmap.h                   |   23 +
 include/linux/regmap_usb.h               |   97 ++
 31 files changed, 8107 insertions(+), 5 deletions(-)
 create mode 100644 drivers/base/regmap/regmap-usb.c
 create mode 100644 drivers/gpu/drm/mud/Kconfig
 create mode 100644 drivers/gpu/drm/mud/Makefile
 create mode 100644 drivers/gpu/drm/mud/mud_drm.c
 create mode 100644 drivers/gpu/drm/mud/mud_drm.h
 create mode 100644 drivers/gpu/drm/mud/mud_drm_gadget.c
 create mode 100644 drivers/mfd/mud.c
 create mode 100644 drivers/pinctrl/pinctrl-mud.c
 create mode 100644 drivers/pinctrl/pinctrl-mud.h
 create mode 100644 drivers/usb/gadget/function/f_mud.c
 create mode 100644 drivers/usb/gadget/function/f_mud.h
 create mode 100644 drivers/usb/gadget/function/f_mud_drm.c
 create mode 100644 drivers/usb/gadget/function/f_mud_pins.c
 create mode 100644 drivers/usb/gadget/function/mud_regmap.c
 create mode 100644 include/linux/mfd/mud.h
 create mode 100644 include/linux/regmap_usb.h
Signature · 1 line
2.23.0
#1 lore raw

RFC 1/9 regmap: Add USB support

Add support for regmap over USB for use with the Multifunction USB Device. Two endpoints IN/OUT are used. Up to 255 regmaps are supported on one USB interface. The register index width is always 32-bit, but the register value can be 8, 16 or 32 bits wide. LZ4 compression is supported on bulk transfers.

---
 drivers/base/regmap/Kconfig      |    8 +-
 drivers/base/regmap/Makefile     |    1 +
 drivers/base/regmap/regmap-usb.c | 1026 ++++++++++++++++++++++++++++++
 include/linux/regmap.h           |   23 +
 include/linux/regmap_usb.h       |   97 +++
 5 files changed, 1154 insertions(+), 1 deletion(-)
 create mode 100644 drivers/base/regmap/regmap-usb.c
 create mode 100644 include/linux/regmap_usb.h
drivers/base/regmap/Kconfig +7 -1
index 0fd6f97ee523..6c937c196825 100644
@@ -4,7 +4,7 @@
 # subsystems should select the appropriate symbols.
 
 config REGMAP
-	default y if (REGMAP_I2C || REGMAP_SPI || REGMAP_SPMI || REGMAP_W1 || REGMAP_AC97 || REGMAP_MMIO || REGMAP_IRQ || REGMAP_SCCB || REGMAP_I3C)
+	default y if (REGMAP_I2C || REGMAP_SPI || REGMAP_SPMI || REGMAP_W1 || REGMAP_AC97 || REGMAP_MMIO || REGMAP_IRQ || REGMAP_SCCB || REGMAP_I3C || REGMAP_USB)
 	select IRQ_DOMAIN if REGMAP_IRQ
 	bool
 
@@ -53,3 +53,9 @@ config REGMAP_SCCB
 config REGMAP_I3C
 	tristate
 	depends on I3C
+
+config REGMAP_USB
+	tristate
+	depends on USB
+	select LZ4_COMPRESS
+	select LZ4_DECOMPRESS
drivers/base/regmap/Makefile +1 -0
index ff6c7d8ec1cd..7e6932f100ea 100644
@@ -17,3 +17,4 @@ obj-$(CONFIG_REGMAP_W1) += regmap-w1.o
 obj-$(CONFIG_REGMAP_SOUNDWIRE) += regmap-sdw.o
 obj-$(CONFIG_REGMAP_SCCB) += regmap-sccb.o
 obj-$(CONFIG_REGMAP_I3C) += regmap-i3c.o
+obj-$(CONFIG_REGMAP_USB) += regmap-usb.o
drivers/base/regmap/regmap-usb.c new file +1026 -0

1,026 lines not on this page · raw message

include/linux/regmap.h +23 -0
index dfe493ac692d..c25ae1a98538 100644
@@ -32,6 +32,7 @@ struct regmap_range_cfg;
 struct regmap_field;
 struct snd_ac97;
 struct sdw_slave;
+struct usb_interface;
 
 /* An enum of all the supported cache types */
 enum regcache_type {
@@ -618,6 +619,12 @@ struct regmap *__devm_regmap_init_sdw(struct sdw_slave *sdw,
 				 const struct regmap_config *config,
 				 struct lock_class_key *lock_key,
 				 const char *lock_name);
+struct regmap *__devm_regmap_init_usb(struct device *dev,
+				      struct usb_interface *interface,
+				      unsigned int index,
+				      const struct regmap_config *config,
+				      struct lock_class_key *lock_key,
+				      const char *lock_name);
 struct regmap *__devm_regmap_init_slimbus(struct slim_device *slimbus,
 				 const struct regmap_config *config,
 				 struct lock_class_key *lock_key,
@@ -971,6 +978,22 @@ bool regmap_ac97_default_volatile(struct device *dev, unsigned int reg);
 	__regmap_lockdep_wrapper(__devm_regmap_init_sdw, #config,	\
 				sdw, config)
 
+/**
+ * devm_regmap_init_usb() - Initialise managed register map
+ *
+ * @dev: Parent device
+ * @interface: USB interface
+ * @index: Index of register
+ * @config: Configuration for register map
+ *
+ * The return value will be an ERR_PTR() on error or a valid pointer
+ * to a struct regmap. The regmap will be automatically freed by the
+ * device management code.
+ */
+#define devm_regmap_init_usb(dev, interface, index, config)		\
+	__regmap_lockdep_wrapper(__devm_regmap_init_usb, #config,	\
+				dev, interface, index, config)
+
 /**
  * devm_regmap_init_slimbus() - Initialise managed register map
  *
include/linux/regmap_usb.h new file +97 -0

97 lines not on this page · raw message

Signature · 1 line
2.23.0
maintainer
#2 lore raw

Re: [RFC 1/9] regmap: Add USB support

Quoted 5 lines · Noralf Trønnes

Add support for regmap over USB for use with the Multifunction USB Device. Two endpoints IN/OUT are used. Up to 255 regmaps are supported on one USB interface. The register index width is always 32-bit, but the register value can be 8, 16 or 32 bits wide. LZ4 compression is supported on bulk transfers.

This looks like a custom thing for some particular devices rather than a thing that will work for any USB device? If that is the case then this should have a more specific name.

drivers/base/regmap/regmap-usb.c line 3

Quoted 5 lines

+++ b/drivers/base/regmap/regmap-usb.c

drivers/base/regmap/regmap-usb.c new file +3 -0
@@ -0,0 +1,1026 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Register map access API - USB support

Why is this GPL-2.0-or-later? The rest of the code is just plain old GPL-2.0.

Please also make the entire comment a C++ one so things look more intentional.

Review line 5

Quoted 2 lines
(quoted diff) +2 -0
+
+	ktime_t start; /* FIXME: Temporary debug/perf aid */

Add tracepoints, most of your debugging stuff looks like you want to use tracepoints - you can easily work out time differences with them by postprocessing the log, they get very fine grained timestamps.

drivers/base/regmap/regmap-usb.c line 123

Quoted 8 lines
drivers/base/regmap/regmap-usb.c new file +8 -0
+	mutex_lock(&regmap_usb_interfaces_lock);
+	list_for_each_entry(entry, &regmap_usb_interfaces, link)
+		if (entry->interface == interface) {
+			ruif = entry;
+			ruif->refcount++;
+			goto out_unlock;
+		}
+

You're missing some { } here.

drivers/base/regmap/regmap-usb.c line 257

Quoted 7 lines
drivers/base/regmap/regmap-usb.c new file +7 -0
+static long mud_drm_throughput(ktime_t begin, ktime_t end, size_t len)
+{
+	long throughput;
+
+	throughput = ktime_us_delta(end, begin);
+	throughput = throughput ? (len * 1000) / throughput : 0;
+	throughput = throughput * 1000 / 1024;

Please write normal conditional statements to improve legibility.

drivers/base/regmap/regmap-usb.c line 742

Quoted 6 lines
drivers/base/regmap/regmap-usb.c new file +6 -0
+static int regmap_usb_gather_write(void *context,
+				   const void *reg, size_t reg_len,
+				   const void *val, size_t val_len)
+{
+	return regmap_usb_transfer(context, false, reg, (void *)val, val_len);
+}

Why are we casting away a const here? If we really need to modify the raw data that's being transmitted take a copy.

Quoted 7 lines
drivers/base/regmap/regmap-usb.c new file +7 -0
+static int regmap_usb_write(void *context, const void *data, size_t count)
+{
+	struct regmap_usb_context *ctx = context;
+	size_t val_len = count - sizeof(u32);
+	void *val;
+	int ret;
+

drivers/base/regmap/regmap-usb.c line 760

Quoted 10 lines
drivers/base/regmap/regmap-usb.c new file +10 -0
+	/* buffer needs to be properly aligned for DMA use */
+	val = kmemdup(data + sizeof(u32), val_len, GFP_KERNEL);
+	if (!val)
+		return -ENOMEM;
+
+	ret = regmap_usb_gather_write(ctx, data, sizeof(u32), val, val_len);
+	kfree(val);
+
+	return ret;
+}

This looks like you just don't support a straight write operation, if you need to do this emulation push it up the stack.

drivers/base/regmap/regmap-usb.c line 940

Quoted 5 lines
drivers/base/regmap/regmap-usb.c new file +5 -0
+		regmap_usb_debugfs_init(map);
+
+	return map;
+}
+EXPORT_SYMBOL(__devm_regmap_init_usb);

No, this needs to be EXPORT_SYMBOL_GPL - the regmap core is and this isn't going to be useful without those bits of the code anyway.

#3 lore raw

Re: [RFC 1/9] regmap: Add USB support

Den 17.02.2020 13.11, skrev Mark Brown:

Quoted 20 lines · Noralf Trønnes
Quoted 19 lines

Add support for regmap over USB for use with the Multifunction USB Device. Two endpoints IN/OUT are used. Up to 255 regmaps are supported on one USB interface. The register index width is always 32-bit, but the register value can be 8, 16 or 32 bits wide. LZ4 compression is supported on bulk transfers.

drivers/base/regmap/regmap-usb.c new file +12 -0
+static int regmap_usb_write(void *context, const void *data, size_t count)
+{
+	struct regmap_usb_context *ctx = context;
+	size_t val_len = count - sizeof(u32);
+	void *val;
+	int ret;
+
+	/* buffer needs to be properly aligned for DMA use */
+	val = kmemdup(data + sizeof(u32), val_len, GFP_KERNEL);
+	if (!val)
+		return -ENOMEM;
+
Quoted 9 lines

drivers/base/regmap/regmap-usb.c line 760

Quoted 5 lines
drivers/base/regmap/regmap-usb.c new file +5 -0
+	ret = regmap_usb_gather_write(ctx, data, sizeof(u32), val, val_len);
+	kfree(val);
+
+	return ret;
+}

This looks like you just don't support a straight write operation, if you need to do this emulation push it up the stack.

After going through the stack I realise that I have a problem. What I have failed to fully comprehend is this regmap requirement:

	if (val_len % map->format.val_bytes)
		return -EINVAL;

There will be a spi and i2c driver down the line which will transfer buffers of any size, and having to use 8-bit register values will not be great.

When I started writing regmap-usb 6 months ago I didn't know where it would take me. The result is now a Multifuntion USB device with an mfd driver. So, no usage except for the children of the mfd device.

Dropping regmap won't lead to any increased code size to speak of, so instead of trying to make regmap match my use case, I think I'll put this code inside the mfd driver.

Oversights like this was one of the things I was hoping to unearth with this RFC.

Thanks,

Noralf.

maintainer
#4 lore raw

Re: [RFC 1/9] regmap: Add USB support

Quoted 8 lines · Noralf Trønnes

Den 17.02.2020 13.11, skrev Mark Brown:

Quoted 2 lines

This looks like you just don't support a straight write operation, if you need to do this emulation push it up the stack.

After going through the stack I realise that I have a problem. What I have failed to fully comprehend is this regmap requirement:

Quoted 6 lines
	if (val_len % map->format.val_bytes)
		return -EINVAL;

There will be a spi and i2c driver down the line which will transfer buffers of any size, and having to use 8-bit register values will not be great.

Out of interest why are 8 bit registers going to be a problem?

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#5 lore raw

Re: [RFC 1/9] regmap: Add USB support

Den 17.02.2020 22.39, skrev Mark Brown:

Quoted 9 lines · Noralf Trønnes
Quoted 7 lines

Den 17.02.2020 13.11, skrev Mark Brown:

Quoted 2 lines

This looks like you just don't support a straight write operation, if you need to do this emulation push it up the stack.

After going through the stack I realise that I have a problem. What I have failed to fully comprehend is this regmap requirement:

Quoted 9 lines
Quoted 6 lines
	if (val_len % map->format.val_bytes)
		return -EINVAL;

There will be a spi and i2c driver down the line which will transfer buffers of any size, and having to use 8-bit register values will not be great.

Out of interest why are 8 bit registers going to be a problem?

I have written 3 drivers so far and they all have some registers that need to deal with values larger than 255. If I would need to add a lot of code because of dropping regmap, then I would have looked at ways to work around this in order to keep regmap, hi/lo registers perhaps with wrapping access functions. But it looks like the LOC won't change much, I need a few lines to ensure values are little endian, but I can also remove some lines that's not needed anymore.

Noralf.

maintainer
#6 lore raw

Re: [RFC 1/9] regmap: Add USB support

Quoted 11 lines · Noralf Trønnes

Den 17.02.2020 22.39, skrev Mark Brown:

Quoted 1 line

Out of interest why are 8 bit registers going to be a problem?

I have written 3 drivers so far and they all have some registers that need to deal with values larger than 255. If I would need to add a lot of code because of dropping regmap, then I would have looked at ways to work around this in order to keep regmap, hi/lo registers perhaps with wrapping access functions. But it looks like the LOC won't change much, I need a few lines to ensure values are little endian, but I can also remove some lines that's not needed anymore.

Right, so you effectively have mixed register sizes which regmap isn't going to be super happy with (assuming you need all the actual display data to be 8 bit "registers"). One thing you could do there if you wanted to try the regmap route is to have multiple regmaps but since it's not clear what you're really getting from regmap other than the trace functionality you're probably right that it's not worth bothering.

The only other thing I can think of is packing RGB into a single register so you're display data isn't 8 bit but that's probably not sensible from a graphics point of view (I didn't really look at that code so no idea what you're doing there).

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#7 lore raw

RFC 2/9 mfd: Add driver for Multifunction USB Device

In reply to Noralf Trønnes

A Multifunction USB Device is a device that supports functions like gpio and display or any other function that can be represented as a USB regmap. Interrupts over USB is also supported if such an endpoint is present.

---
 drivers/mfd/Kconfig     |   8 +
 drivers/mfd/Makefile    |   1 +
 drivers/mfd/mud.c       | 580 ++++++++++++++++++++++++++++++++++++++++
 include/linux/mfd/mud.h |  16 ++
 4 files changed, 605 insertions(+)
 create mode 100644 drivers/mfd/mud.c
 create mode 100644 include/linux/mfd/mud.h
drivers/mfd/Kconfig +8 -0
index 52818dbcfe1f..9950794d907e 100644
@@ -1968,6 +1968,14 @@ config MFD_STMFX
 	  additional drivers must be enabled in order to use the functionality
 	  of the device.
 
+config MFD_MUD
+	tristate "Multifunction USB Device core driver"
+	depends on USB
+	select MFD_CORE
+	select REGMAP_USB
+	help
+	  Select this to get support for the Multifunction USB Device.
+
 menu "Multimedia Capabilities Port drivers"
 	depends on ARCH_SA1100
 
drivers/mfd/Makefile +1 -0
index 29e6767dd60c..0adfab9afaed 100644
@@ -255,4 +255,5 @@ obj-$(CONFIG_MFD_ROHM_BD70528)	+= rohm-bd70528.o
 obj-$(CONFIG_MFD_ROHM_BD718XX)	+= rohm-bd718x7.o
 obj-$(CONFIG_MFD_STMFX) 	+= stmfx.o
 obj-$(CONFIG_MFD_RPISENSE_CORE)	+= rpisense-core.o
+obj-$(CONFIG_MFD_MUD)		+= mud.o
 
drivers/mfd/mud.c new file +580 -0

580 lines not on this page · raw message

include/linux/mfd/mud.h new file +16 -0
new file mode 100644
index 000000000000..b2059fa57429
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#ifndef __LINUX_MUD_H
+#define __LINUX_MUD_H
+
+#include <linux/regmap_usb.h>
+
+struct usb_interface;
+
+struct mud_cell_pdata {
+	struct usb_interface *interface;
+	unsigned int index;
+	struct regmap_usb_map_descriptor desc;
+};
+
+#endif
Signature · 1 line
2.23.0
#8 lore raw

Re: [RFC 2/9] mfd: Add driver for Multifunction USB Device

I'd really like someone from USB to have a look through this too.

I'll do a quick first pass and provide some general comments though.

Quoted 3 lines · Noralf Trønnes

A Multifunction USB Device is a device that supports functions like gpio and display or any other function that can be represented as a USB regmap. Interrupts over USB is also supported if such an endpoint is present.

Do you have a datasheet?

Quoted 5 lines
---
 drivers/mfd/Kconfig     |   8 +
 drivers/mfd/Makefile    |   1 +
 drivers/mfd/mud.c       | 580 ++++++++++++++++++++++++++++++++++++++++

As interesting as a "mud driver" sounds. Something more forthcoming might be better, "multi_usb" as a very quick example, but perhaps something more imaginative/distinguishing would be better.

Quoted 60 lines
 include/linux/mfd/mud.h |  16 ++
 4 files changed, 605 insertions(+)
 create mode 100644 drivers/mfd/mud.c
 create mode 100644 include/linux/mfd/mud.h
drivers/mfd/Kconfig +8 -0
index 52818dbcfe1f..9950794d907e 100644
@@ -1968,6 +1968,14 @@ config MFD_STMFX
 	  additional drivers must be enabled in order to use the functionality
 	  of the device.
 
+config MFD_MUD
+	tristate "Multifunction USB Device core driver"
+	depends on USB
+	select MFD_CORE
+	select REGMAP_USB
+	help
+	  Select this to get support for the Multifunction USB Device.
+
 menu "Multimedia Capabilities Port drivers"
 	depends on ARCH_SA1100
 
drivers/mfd/Makefile +1 -0
index 29e6767dd60c..0adfab9afaed 100644
@@ -255,4 +255,5 @@ obj-$(CONFIG_MFD_ROHM_BD70528)	+= rohm-bd70528.o
 obj-$(CONFIG_MFD_ROHM_BD718XX)	+= rohm-bd718x7.o
 obj-$(CONFIG_MFD_STMFX) 	+= stmfx.o
 obj-$(CONFIG_MFD_RPISENSE_CORE)	+= rpisense-core.o
+obj-$(CONFIG_MFD_MUD)		+= mud.o
 
drivers/mfd/mud.c new file +20 -0
new file mode 100644
index 000000000000..f5f31478656d
@@ -0,0 +1,580 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright 2020 Noralf Trønnes
+ */
+
+#include <linux/bitmap.h>
+#include <linux/interrupt.h>
+#include <linux/irq.h>
+#include <linux/irqdomain.h>
+#include <linux/list.h>
+#include <linux/mfd/core.h>
+#include <linux/mfd/mud.h>
+#include <linux/module.h>
+#include <linux/regmap.h>
+#include <linux/regmap_usb.h>
+#include <linux/seq_file.h>
+#include <linux/slab.h>
+#include <linux/spinlock.h>
+#include <linux/usb.h>
+#include <linux/workqueue.h>

drivers/mfd/mud.c line 30

Quoted 10 lines
drivers/mfd/mud.c new file +10 -0
+
+/* Temporary debugging aid */
+#undef dev_dbg
+#define dev_dbg dev_info
+
+#define mdebug(fmt, ...)			\
+do {						\
+	if (1)					\
+		pr_debug(fmt, ##__VA_ARGS__);	\
+} while (0)

No thank you.

We already have quite a few debugging facilities in the kernel.

Quoted 12 lines
drivers/mfd/mud.c new file +12 -0
+struct mud_irq_event {
+	struct list_head node;
+	DECLARE_BITMAP(status, REGMAP_USB_MAX_MAPS);
+};
+
+struct mud_irq {
+	struct irq_domain *domain;
+	unsigned int num_irqs;
+
+	struct workqueue_struct	*workq;
+	struct work_struct work;
+	struct urb *urb;

drivers/mfd/mud.c line 53

Quoted 10 lines
drivers/mfd/mud.c new file +10 -0
+
+	spinlock_t lock; /* Protect the values below */
+	unsigned long *mask;
+	u16 tag;
+	struct list_head eventlist;
+
+	unsigned int stats_illegal;
+	unsigned int stats_already_seen;
+	unsigned int stats_lost;
+};

I think this should have a Kerneldoc header.

drivers/mfd/mud.c line 55

Quoted 1 line
drivers/mfd/mud.c new file +1 -0
+struct mud_device {

s/device/ddata/

drivers/mfd/mud.c line 58

Quoted 4 lines
drivers/mfd/mud.c new file +3 -0
+	struct usb_device *usb;
+	struct mud_irq *mirq;
 
+	struct mfd_cell *cells;

Why does this need to be in here?

drivers/mfd/mud.c line 59

Quoted 1 line
drivers/mfd/mud.c new file +1 -0
+	unsigned int num_cells;

Why do these need to be stored in device data?

drivers/mfd/mud.c line 69

Quoted 10 lines
drivers/mfd/mud.c new file +10 -0
+};
+
+static void mud_irq_work(struct work_struct *work)
+{
+	struct mud_irq *mirq = container_of(work, struct mud_irq, work);
+	struct mud_irq_event *event;
+	unsigned long n, flags;
+	unsigned int irq;
+
+	mdebug("%s: IN\n", __func__);

All of these prints need to go. They have no place in an upstreamed driver and the whole thing reads much better without them.

drivers/mfd/mud.c line 79

Quoted 9 lines
drivers/mfd/mud.c new file +9 -0
+	while (true) {
+		spin_lock_irqsave(&mirq->lock, flags);
+		event = list_first_entry_or_null(&mirq->eventlist, struct mud_irq_event, node);
+		if (event) {
+			list_del(&event->node);
+			mdebug("    status: %*pb\n", mirq->num_irqs, event->status);
+			bitmap_and(event->status, event->status, mirq->mask, mirq->num_irqs);
+		}
+		spin_unlock_irqrestore(&mirq->lock, flags);

'\n'

Quoted 11 lines
drivers/mfd/mud.c new file +11 -0
+		if (!event)
+			break;
+
+		for_each_set_bit(n, event->status, mirq->num_irqs) {
+			irq = irq_find_mapping(mirq->domain, n);
+			mdebug("        n=%lu irq=%u\n", n, irq);
+			if (irq)
+				handle_nested_irq(irq);
+		}
+
+		kfree(event);

drivers/mfd/mud.c line 100

Quoted 10 lines
drivers/mfd/mud.c new file +10 -0
+	}
+
+	mdebug("%s: OUT\n", __func__);
+}
+
+#define BYTES_PER_LONG	(BITS_PER_LONG / BITS_PER_BYTE)
+
+static void mud_irq_queue(struct urb *urb)
+{
+	u8 *buf = urb->transfer_buffer + sizeof(u16);

Comment.

Quoted 16 lines
drivers/mfd/mud.c new file +16 -0
+	struct mud_irq *mirq = urb->context;
+	struct device *dev = &urb->dev->dev;
+	struct mud_irq_event *event = NULL;
+	unsigned int i, tag, diff;
+	unsigned long flags;
+
+	if (urb->actual_length != urb->transfer_buffer_length) {
+		dev_err_once(dev, "Interrupt packet wrong length: %u\n",
+			     urb->actual_length);
+		mirq->stats_illegal++;
+		return;
+	}
+
+	spin_lock_irqsave(&mirq->lock, flags);
+
+	tag = le16_to_cpup(urb->transfer_buffer);

drivers/mfd/mud.c line 126

Quoted 10 lines
drivers/mfd/mud.c new file +10 -0
+	if (tag == mirq->tag) {
+		dev_dbg(dev, "Interrupt tag=%u already seen, ignoring\n", tag);
+		mirq->stats_already_seen++;
+		goto unlock;
+	}
+
+	if (tag > mirq->tag)
+		diff = tag - mirq->tag;
+	else
+		diff = U16_MAX - mirq->tag + tag + 1;

Comment.

drivers/mfd/mud.c line 137

Quoted 10 lines
drivers/mfd/mud.c new file +10 -0
+	if (diff > 1) {
+		dev_err_once(dev, "Interrupts lost: %u\n", diff - 1);
+		mirq->stats_lost += diff - 1;
+	}
+
+	event = kzalloc(sizeof(*event), GFP_ATOMIC);
+	if (!event) {
+		mirq->stats_lost += 1;
+		goto unlock;
+	}

This hides a potential OOM issue.

Not sure what to do about that. Maybe the USB guys have an idea.

drivers/mfd/mud.c line 149

Quoted 11 lines
drivers/mfd/mud.c new file +11 -0
+	list_add_tail(&event->node, &mirq->eventlist);
+
+	for (i = 0; i < (urb->transfer_buffer_length - sizeof(u16)); i++) {
+		unsigned long *val = &event->status[i / BYTES_PER_LONG];
+		unsigned int mod = i % BYTES_PER_LONG;
+
+		if (!mod)
+			*val = buf[i];
+		else
+			*val |= ((unsigned long)buf[i]) << (mod * BITS_PER_BYTE);
+	}

Comment. In fact, comments throughout please.

(I'm going to stop saying "comment" from here on).

Quoted 12 lines
drivers/mfd/mud.c new file +12 -0
+	mdebug("%s: tag=%u\n", __func__, tag);
+
+	mirq->tag = tag;
+unlock:
+	spin_unlock_irqrestore(&mirq->lock, flags);
+
+	if (event)
+		queue_work(mirq->workq, &mirq->work);
+}
+
+static void mud_irq_urb_completion(struct urb *urb)
+{

drivers/mfd/mud.c line 172

Quoted 10 lines
drivers/mfd/mud.c new file +10 -0
+	struct device *dev = &urb->dev->dev;
+	int ret;
+
+	mdebug("%s: actual_length=%u\n", __func__, urb->actual_length);
+
+	switch (urb->status) {
+	case 0:
+		mud_irq_queue(urb);
+		break;
+	case -EPROTO:	/* FIXME: verify: dwc2 reports this on disconnect */

What does this mean? Why can't you fix it now?

drivers/mfd/mud.c line 176

Quoted 4 lines
drivers/mfd/mud.c new file +4 -0
+	case -ECONNRESET:
+	case -ENOENT:
+	case -ESHUTDOWN:
+		dev_dbg(dev, "irq urb shutting down with status: %d\n", urb->status);

s/irq/IRQ/ in all comments and prints.

Same with URB?

drivers/mfd/mud.c line 180

Quoted 4 lines
drivers/mfd/mud.c new file +4 -0
+		return;
+	default:
+		dev_dbg(dev, "irq urb failure with status: %d\n", urb->status);
+		break;

So it's failed, but you're going to attempt to submit it anyway?

Quoted 26 lines
drivers/mfd/mud.c new file +26 -0
+	}
+
+	ret = usb_submit_urb(urb, GFP_ATOMIC);
+	if (ret && ret != -ENODEV)
+		dev_err(dev, "irq usb_submit_urb failed with result %d\n", ret);
+}
+
+static void mud_irq_mask(struct irq_data *data)
+{
+	struct mud_irq *mirq = irq_data_get_irq_chip_data(data);
+	unsigned long flags;
+
+	mdebug("%s: hwirq=%lu\n", __func__, data->hwirq);
+
+	spin_lock_irqsave(&mirq->lock, flags);
+	clear_bit(data->hwirq, mirq->mask);
+	spin_unlock_irqrestore(&mirq->lock, flags);
+}
+
+static void mud_irq_unmask(struct irq_data *data)
+{
+	struct mud_irq *mirq = irq_data_get_irq_chip_data(data);
+	unsigned long flags;
+
+	mdebug("%s: hwirq=%lu\n", __func__, data->hwirq);
+

drivers/mfd/mud.c line 216

Quoted 10 lines
drivers/mfd/mud.c new file +10 -0
+	spin_lock_irqsave(&mirq->lock, flags);
+	set_bit(data->hwirq, mirq->mask);
+	spin_unlock_irqrestore(&mirq->lock, flags);
+}
+
+static struct irq_chip mud_irq_chip = {
+	.name			= "mud-irq",
+	.irq_mask		= mud_irq_mask,
+	.irq_unmask		= mud_irq_unmask,
+};

This tabbing seems excessive.

drivers/mfd/mud.c line 220

Quoted 3 lines
drivers/mfd/mud.c new file +3 -0
+static void __maybe_unused
+mud_irq_domain_debug_show(struct seq_file *m, struct irq_domain *d,
+			  struct irq_data *data, int ind)

Best not to over-shorten variable names for no good reason.

'm', 'd' and 'ind' aren't good choices.

Quoted 179 lines
drivers/mfd/mud.c new file +179 -0
+{
+	struct mud_irq *mirq = d ? d->host_data : irq_data_get_irq_chip_data(data);
+	unsigned long flags;
+
+	spin_lock_irqsave(&mirq->lock, flags);
+
+	seq_printf(m, "%*sTag:          %u\n", ind, "", mirq->tag);
+	seq_printf(m, "%*sIllegal:      %u\n", ind, "", mirq->stats_illegal);
+	seq_printf(m, "%*sAlready seen: %u\n", ind, "", mirq->stats_already_seen);
+	seq_printf(m, "%*sLost:         %u\n", ind, "", mirq->stats_lost);
+
+	spin_unlock_irqrestore(&mirq->lock, flags);
+}
+
+static int mud_irq_domain_map(struct irq_domain *d, unsigned int virq,
+			      irq_hw_number_t hwirq)
+{
+	irq_set_chip_data(virq, d->host_data);
+	irq_set_chip_and_handler(virq, &mud_irq_chip, handle_simple_irq);
+	irq_set_nested_thread(virq, true);
+	irq_set_noprobe(virq);
+
+	return 0;
+}
+
+static void mud_irq_domain_unmap(struct irq_domain *d, unsigned int virq)
+{
+	irq_set_chip_and_handler(virq, NULL, NULL);
+	irq_set_chip_data(virq, NULL);
+}
+
+static const struct irq_domain_ops mud_irq_ops = {
+	.map		= mud_irq_domain_map,
+	.unmap		= mud_irq_domain_unmap,
+#ifdef CONFIG_GENERIC_IRQ_DEBUGFS
+	.debug_show	= mud_irq_domain_debug_show,
+#endif
+};
+
+static int mud_irq_start(struct mud_irq *mirq)
+{
+	if (!mirq)
+		return 0;
+
+	return usb_submit_urb(mirq->urb, GFP_KERNEL);
+}
+
+static void mud_irq_stop(struct mud_irq *mirq)
+{
+	if (!mirq)
+		return;
+
+	usb_kill_urb(mirq->urb);
+	flush_work(&mirq->work);
+}
+
+static void mud_irq_release(struct mud_irq *mirq)
+{
+	if (!mirq)
+		return;
+
+	if (mirq->workq)
+		destroy_workqueue(mirq->workq);
+
+	if (mirq->domain) {
+		irq_hw_number_t hwirq;
+
+		for (hwirq = 0; hwirq < mirq->num_irqs; hwirq++)
+			irq_dispose_mapping(irq_find_mapping(mirq->domain, hwirq));
+
+		irq_domain_remove(mirq->domain);
+	}
+
+	usb_free_coherent(mirq->urb->dev, mirq->urb->transfer_buffer_length,
+			  mirq->urb->transfer_buffer, mirq->urb->transfer_dma);
+	usb_free_urb(mirq->urb);
+	bitmap_free(mirq->mask);
+	kfree(mirq);
+}
+
+static struct mud_irq *mud_irq_create(struct usb_interface *interface,
+				      unsigned int num_irqs)
+{
+	struct usb_device *usb = interface_to_usbdev(interface);
+	struct device *dev = &interface->dev;
+	struct usb_endpoint_descriptor *ep;
+	struct fwnode_handle *fn;
+	struct urb *urb = NULL;
+	struct mud_irq *mirq;
+	void *buf = NULL;
+	size_t buf_size;
+	int ret;
+
+	mdebug("%s: dev->id=%d\n", __func__, dev->id);
+
+	ret = usb_find_int_in_endpoint(interface->cur_altsetting, &ep);
+	if (ret == -ENXIO)
+		return NULL;
+	if (ret)
+		return ERR_PTR(ret);
+
+	mirq = kzalloc(sizeof(*mirq), GFP_KERNEL);
+	if (!mirq)
+		return ERR_PTR(-ENOMEM);
+
+	mirq->mask = bitmap_zalloc(num_irqs, GFP_KERNEL);
+	if (!mirq->mask) {
+		ret = -ENOMEM;
+		goto release;
+	}
+
+	spin_lock_init(&mirq->lock);
+	mirq->num_irqs = num_irqs;
+
+	urb = usb_alloc_urb(0, GFP_KERNEL);
+	if (!urb) {
+		ret = -ENOMEM;
+		goto release;
+	}
+
+	buf_size = usb_endpoint_maxp(ep);
+	if (buf_size != (sizeof(u16) + DIV_ROUND_UP(num_irqs, BITS_PER_BYTE))) {
+		dev_err(dev, "Interrupt endpoint wMaxPacketSize too small: %zu\n", buf_size);
+		ret = -EINVAL;
+		goto release;
+	}
+
+	buf = usb_alloc_coherent(usb, buf_size, GFP_KERNEL, &urb->transfer_dma);
+	if (!buf) {
+		usb_free_urb(urb);
+		ret = -ENOMEM;
+		goto release;
+	}
+
+	usb_fill_int_urb(urb, usb,
+			 usb_rcvintpipe(usb, usb_endpoint_num(ep)),
+			 buf, buf_size, mud_irq_urb_completion,
+			 mirq, ep->bInterval);
+	urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
+
+	mirq->urb = urb;
+
+	if (dev->of_node) {
+		fn = of_node_to_fwnode(dev->of_node);
+	} else {
+		fn = irq_domain_alloc_named_fwnode("mud-irq");
+		if (!fn) {
+			ret = -ENOMEM;
+			goto release;
+		}
+	}
+
+	mirq->domain = irq_domain_create_linear(fn, num_irqs, &mud_irq_ops, mirq);
+	if (!dev->of_node)
+		irq_domain_free_fwnode(fn);
+	if (!mirq->domain) {
+		ret = -ENOMEM;
+		goto release;
+	}
+
+	INIT_LIST_HEAD(&mirq->eventlist);
+	INIT_WORK(&mirq->work, mud_irq_work);
+
+	mirq->workq = alloc_workqueue("mud-irq/%s", WQ_HIGHPRI, 0, dev_name(dev));
+	if (!mirq->workq) {
+		ret = -ENOMEM;
+		goto release;
+	}
+
+	return mirq;
+
+release:
+	mud_irq_release(mirq);
+
+	return ERR_PTR(ret);
+}
+
+static int mud_probe_regmap(struct usb_interface *interface, struct mfd_cell *cell,
+			    unsigned int index, struct mud_irq *mirq)

drivers/mfd/mud.c line 409

Quoted 10 lines
drivers/mfd/mud.c new file +10 -0
+{
+	struct mud_cell_pdata *pdata;
+	struct resource *res = NULL;
+	int ret;
+
+	pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
+	if (!pdata)
+		return -ENOMEM;
+
+	ret = regmap_usb_get_map_descriptor(interface, index, &pdata->desc);

Can you give an example of what a desc might look like?

I'm particularly interested in pdata->desc.name.

drivers/mfd/mud.c

Quoted 2 lines
drivers/mfd/mud.c new file +2 -0
+	if (ret)
+		goto error;

This will attempt to free 'res' which is currently NULL.

drivers/mfd/mud.c line 424

Quoted 12 lines
drivers/mfd/mud.c new file +12 -0
+	mdebug("%s: name='%s' index=%u\n", __func__, pdata->desc.name, index);
+	mdebug("    bRegisterValueBits=%u\n", pdata->desc.bRegisterValueBits);
+	mdebug("    bCompression=0x%02x\n", pdata->desc.bCompression);
+	mdebug("    bMaxTransferSizeOrder=%u (%ukB)\n",
+	       pdata->desc.bMaxTransferSizeOrder,
+	       (1 << pdata->desc.bMaxTransferSizeOrder) / 1024);
+
+	if (mirq) {
+		res = kzalloc(sizeof(*res), GFP_KERNEL);
+		if (!res) {
+			ret = -ENOMEM;
+			goto error;

This will attempt to free 'res' which is currently NULL.

drivers/mfd/mud.c line 436

Quoted 12 lines
drivers/mfd/mud.c new file +12 -0
+		}
+
+		res->flags = IORESOURCE_IRQ;
+		res->start = irq_create_mapping(mirq->domain, index);
+		mdebug("    res->start=%u\n", (unsigned int)res->start);
+		res->end = res->start;
+
+		cell->resources = res;
+		cell->num_resources = 1;
+	}
+
+	pdata->interface = interface;

This looks like something that should be stored in ddata.

drivers/mfd/mud.c line 437

Quoted 1 line
drivers/mfd/mud.c new file +1 -0
+	pdata->index = index;

Don't usually like indexes - what is this used for?

Quoted 4 lines
drivers/mfd/mud.c new file +4 -0
+	cell->name = pdata->desc.name;
+	cell->platform_data = pdata;
+	cell->pdata_size = sizeof(*pdata);
+	/*

drivers/mfd/mud.c line 451

Quoted 10 lines
drivers/mfd/mud.c new file +10 -0
+	 * A Multifunction USB Device can have multiple functions of the same
+	 * type. mfd_add_device() in its current form will only match on the
+	 * first node in the Device Tree.
+	 */
+	cell->of_compatible = cell->name;
+
+	return 0;
+
+error:
+	kfree(res);

I think you should remove this line, as it's never useful here.

Quoted 62 lines
drivers/mfd/mud.c new file +62 -0
+	kfree(pdata);
+
+	return ret;
+}
+
+static void mud_free(struct mud_device *mud)
+{
+	unsigned int i;
+
+	mud_irq_release(mud->mirq);
+
+	for (i = 0; i < mud->num_cells; i++) {
+		kfree(mud->cells[i].platform_data);
+		kfree(mud->cells[i].resources);
+	}
+
+	kfree(mud->cells);
+	kfree(mud);
+}
+
+static int mud_probe(struct usb_interface *interface,
+		     const struct usb_device_id *id)
+{
+	struct device *dev = &interface->dev;
+	unsigned int i, num_regmaps;
+	struct mud_device *mud;
+	int ret;
+
+	mdebug("%s: interface->dev.of_node=%px usb->dev.of_node=%px",
+	       __func__, interface->dev.of_node,
+	       usb_get_dev(interface_to_usbdev(interface))->dev.of_node);
+
+	ret = regmap_usb_get_interface_descriptor(interface, &num_regmaps);
+	if (ret)
+		return ret;
+	if (!num_regmaps)
+		return -EINVAL;
+
+	mdebug("%s: num_regmaps=%u\n", __func__, num_regmaps);
+
+	mud = kzalloc(sizeof(*mud), GFP_KERNEL);
+	if (!mud)
+		return -ENOMEM;
+
+	mud->mirq = mud_irq_create(interface, num_regmaps);
+	if (IS_ERR(mud->mirq)) {
+		ret = PTR_ERR(mud->mirq);
+		goto err_free;
+	}
+
+	mud->num_cells = num_regmaps;
+	mud->cells = kcalloc(num_regmaps, sizeof(*mud->cells), GFP_KERNEL);
+	if (!mud->cells)
+		goto err_free;
+
+	for (i = 0; i < num_regmaps; i++) {
+		ret = mud_probe_regmap(interface, &mud->cells[i], i, mud->mirq);
+		if (ret) {
+			dev_err(dev, "Failed to probe regmap index %i (error %d)\n", i, ret);
+			goto err_free;
+		}
+	}

drivers/mfd/mud.c line 523

Quoted 10 lines
drivers/mfd/mud.c new file +10 -0
+
+	ret = mud_irq_start(mud->mirq);
+	if (ret) {
+		dev_err(dev, "Failed to start irq (error %d)\n", ret);
+		goto err_free;
+	}
+
+	ret = mfd_add_hotplug_devices(dev, mud->cells, mud->num_cells);
+	if (ret) {
+		dev_err(dev, "Failed to add mfd devices to core.");

"MFD" is not a thing. It's made up.

"Failed to register child devices" makes more sense.

NIT: I don't see full stops in any of your other messages.

Quoted 29 lines
drivers/mfd/mud.c new file +29 -0
+		goto err_stop;
+	}
+
+	mud->usb = usb_get_dev(interface_to_usbdev(interface));
+
+	usb_set_intfdata(interface, mud);
+
+	if (mud->usb->product)
+		dev_info(dev, "%s\n", mud->usb->product);
+
+	return 0;
+
+err_stop:
+	mud_irq_stop(mud->mirq);
+err_free:
+	mud_free(mud);
+
+	return ret;
+}
+
+static void mud_disconnect(struct usb_interface *interface)
+{
+	struct mud_device *mud = usb_get_intfdata(interface);
+
+	mfd_remove_devices(&interface->dev);
+	mud_irq_stop(mud->mirq);
+	usb_put_dev(mud->usb);
+	mud_free(mud);
+

drivers/mfd/mud.c line 562

Quoted 10 lines
drivers/mfd/mud.c new file +10 -0
+	dev_dbg(&interface->dev, "disconnected\n");
+}
+
+static const struct usb_device_id mud_table[] = {
+	/*
+	 * FIXME:
+	 * Apply for a proper pid: https://github.com/openmoko/openmoko-usb-oui
+	 *
+	 * Or maybe the Linux Foundation will provide one from their vendor id.
+	 */

Probably not a good idea to take this into the upstream kernel without a valid, registered PID. Suggest you do this *first*.

Quoted 25 lines
drivers/mfd/mud.c new file +18 -0
+	{ USB_DEVICE_INTERFACE_CLASS(0x1d50, 0x6150, USB_CLASS_VENDOR_SPEC) },
+	{ }
+};
+
+MODULE_DEVICE_TABLE(usb, mud_table);
+
+static struct usb_driver mud_driver = {
+	.name		= "mud",
+	.probe		= mud_probe,
+	.disconnect	= mud_disconnect,
+	.id_table	= mud_table,
+};
+
+module_usb_driver(mud_driver);
+
+MODULE_DESCRIPTION("Generic USB Device mfd core driver");
+MODULE_AUTHOR("Noralf Trønnes <[email protected]>");
+MODULE_LICENSE("GPL");
include/linux/mfd/mud.h new file +1 -0
new file mode 100644
index 000000000000..b2059fa57429
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */

include/linux/mfd/mud.h line 11

Quoted 10 lines
include/linux/mfd/mud.h new file +10 -0
+
+#ifndef __LINUX_MUD_H
+#define __LINUX_MUD_H
+
+#include <linux/regmap_usb.h>
+
+struct usb_interface;
+
+struct mud_cell_pdata {
+	struct usb_interface *interface;

Shouldn't be here - more of a ddata thing.

include/linux/mfd/mud.h line 12

Quoted 1 line
include/linux/mfd/mud.h new file +1 -0
+	unsigned int index;

Indexes are generally not a good idea.

include/linux/mfd/mud.h line 13

Quoted 1 line
include/linux/mfd/mud.h new file +1 -0
+	struct regmap_usb_map_descriptor desc;

Shouldn't be here - more of a ddata thing.

Quoted 3 lines
include/linux/mfd/mud.h new file +3 -0
+};
+
+#endif
Signature · 8 lines
Lee Jones [李琼斯]
Linaro Services Technical Lead
Linaro.org │ Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#9 lore raw

Re: [RFC 2/9] mfd: Add driver for Multifunction USB Device

Den 27.02.2020 10.09, skrev Lee Jones:

Quoted 10 lines

I'd really like someone from USB to have a look through this too.

I'll do a quick first pass and provide some general comments though.

Quoted 3 lines · Noralf Trønnes

A Multifunction USB Device is a device that supports functions like gpio and display or any other function that can be represented as a USB regmap. Interrupts over USB is also supported if such an endpoint is present.

Do you have a datasheet?

As mentioned in the cover letter this is about turning a Linux board like the Raspberry Pi into a USB gadget that presents functions like display, gpio, spi, i2c to a host over USB. Patch 3 in this series contains the gadget side of this mfd driver.

Patch 1 has the register over USB code implemented as a regmap. After talking to Mark Brown I realised that regmap has a limitation (no variable register value width) so my plan is to include that code into this mfd driver instead.

Quoted 4 lines
Quoted 3 lines

---

Quoted 14 lines

drivers/mfd/mud.c line 172

Quoted 12 lines
drivers/mfd/mud.c new file +12 -0
+static void mud_irq_urb_completion(struct urb *urb)
+{
+	struct device *dev = &urb->dev->dev;
+	int ret;
+
+	mdebug("%s: actual_length=%u\n", __func__, urb->actual_length);
+
+	switch (urb->status) {
+	case 0:
+		mud_irq_queue(urb);
+		break;
+	case -EPROTO:	/* FIXME: verify: dwc2 reports this on disconnect */

What does this mean? Why can't you fix it now?

I don't know if this is a dwc2 driver problem or if EPROTO is a valid disconnect error. I haven't seen it in other gadget drivers, so I need to look more into this or even better if someone from USB can answer this.

Quoted 8 lines

drivers/mfd/mud.c line 176

Quoted 5 lines
drivers/mfd/mud.c new file +4 -0
+	case -ECONNRESET:
+	case -ENOENT:
+	case -ESHUTDOWN:
+		dev_dbg(dev, "irq urb shutting down with status: %d\n", urb->status);

s/irq/IRQ/ in all comments and prints.

Quoted 8 lines

Same with URB?

drivers/mfd/mud.c line 180

Quoted 4 lines
drivers/mfd/mud.c new file +4 -0
+		return;
+	default:
+		dev_dbg(dev, "irq urb failure with status: %d\n", urb->status);
+		break;

So it's failed, but you're going to attempt to submit it anyway?

Yes, I don't know the reason why it failed, it might succeed the next time. But this is also something that someone with real life experience with USB failures could weigh in on. Maybe I should send a reset request so the device can reset its state machine, I don't know.

Quoted 24 lines
Quoted 11 lines
drivers/mfd/mud.c new file +8 -0
+	}
+
+	ret = usb_submit_urb(urb, GFP_ATOMIC);
+	if (ret && ret != -ENODEV)
+		dev_err(dev, "irq usb_submit_urb failed with result %d\n", ret);
+}
 
 
+static int mud_probe_regmap(struct usb_interface *interface, struct mfd_cell *cell,
+			    unsigned int index, struct mud_irq *mirq)

drivers/mfd/mud.c line 409

Quoted 10 lines
drivers/mfd/mud.c new file +10 -0
+{
+	struct mud_cell_pdata *pdata;
+	struct resource *res = NULL;
+	int ret;
+
+	pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
+	if (!pdata)
+		return -ENOMEM;
+
+	ret = regmap_usb_get_map_descriptor(interface, index, &pdata->desc);

Can you give an example of what a desc might look like?

Quoted 2 lines

I'm particularly interested in pdata->desc.name.

This is the definition:

/**
* struct regmap_usb_map_descriptor - Regmap descriptor
* @bLength: Size of descriptor in bytes
* @bDescriptorType: DescriptorType (REGMAP_USB_DT_MAP)
* @name: Register name (NUL terminated)
* @bRegisterValueBits: Number of bits in the register value
* @bCompression: Supported compression types
* @bMaxTransferSizeOrder: Maximum transfer size the device can handle
as log2.
*/

struct regmap_usb_map_descriptor {
	__u8 bLength;
	__u8 bDescriptorType;

	__u8 name[32];
	__u8 bRegisterValueBits;
	__u8 bCompression;
#define REGMAP_USB_COMPRESSION_LZ4	BIT(0)
	__u8 bMaxTransferSizeOrder;
} __packed;
Quoted 35 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+	if (ret)
+		goto error;

This will attempt to free 'res' which is currently NULL.

drivers/mfd/mud.c line 424

Quoted 12 lines
drivers/mfd/mud.c new file +12 -0
+	mdebug("%s: name='%s' index=%u\n", __func__, pdata->desc.name, index);
+	mdebug("    bRegisterValueBits=%u\n", pdata->desc.bRegisterValueBits);
+	mdebug("    bCompression=0x%02x\n", pdata->desc.bCompression);
+	mdebug("    bMaxTransferSizeOrder=%u (%ukB)\n",
+	       pdata->desc.bMaxTransferSizeOrder,
+	       (1 << pdata->desc.bMaxTransferSizeOrder) / 1024);
+
+	if (mirq) {
+		res = kzalloc(sizeof(*res), GFP_KERNEL);
+		if (!res) {
+			ret = -ENOMEM;
+			goto error;

This will attempt to free 'res' which is currently NULL.

drivers/mfd/mud.c line 436

Quoted 12 lines
drivers/mfd/mud.c new file +12 -0
+		}
+
+		res->flags = IORESOURCE_IRQ;
+		res->start = irq_create_mapping(mirq->domain, index);
+		mdebug("    res->start=%u\n", (unsigned int)res->start);
+		res->end = res->start;
+
+		cell->resources = res;
+		cell->num_resources = 1;
+	}
+
+	pdata->interface = interface;

This looks like something that should be stored in ddata.

Quoted 3 lines

drivers/mfd/mud.c line 437

Quoted 1 line
drivers/mfd/mud.c new file +1 -0
+	pdata->index = index;

Don't usually like indexes - what is this used for?

A maximum of 255 register maps are supported on one USB interface and this index tells which one it is. It's passed in the USB transfer header.

Quoted 24 lines
Quoted 5 lines
drivers/mfd/mud.c new file +4 -0
+	cell->name = pdata->desc.name;
+	cell->platform_data = pdata;
+	cell->pdata_size = sizeof(*pdata);
+	/*

drivers/mfd/mud.c line 451

Quoted 10 lines
drivers/mfd/mud.c new file +10 -0
+	 * A Multifunction USB Device can have multiple functions of the same
+	 * type. mfd_add_device() in its current form will only match on the
+	 * first node in the Device Tree.
+	 */
+	cell->of_compatible = cell->name;
+
+	return 0;
+
+error:
+	kfree(res);

I think you should remove this line, as it's never useful here.

Quoted 5 lines
drivers/mfd/mud.c new file +5 -0
+	kfree(pdata);
+
+	return ret;
+}
+
Quoted 10 lines

drivers/mfd/mud.c line 562

Quoted 7 lines
drivers/mfd/mud.c new file +7 -0
+static const struct usb_device_id mud_table[] = {
+	/*
+	 * FIXME:
+	 * Apply for a proper pid: https://github.com/openmoko/openmoko-usb-oui
+	 *
+	 * Or maybe the Linux Foundation will provide one from their vendor id.
+	 */

Probably not a good idea to take this into the upstream kernel without a valid, registered PID. Suggest you do this *first*.

I didn't know if my work was fundementally flawed in some way that made it difficult to get merged. Hence the RFC to ask for help from people knowledgeable in this area. So I'm hoping that some USB people will have a look on this as well.

If this multifunction idea doesn't work out, then I'll just do the USB display part and it will only be a drm driver. So at the moment I don't know what kind of USB device this will be: multifuntion or display. When I know then I'll get a PID.

Noralf.

Quoted 4 lines

drivers/mfd/mud.c line 565

Quoted 4 lines
drivers/mfd/mud.c new file +3 -0
+	{ USB_DEVICE_INTERFACE_CLASS(0x1d50, 0x6150, USB_CLASS_VENDOR_SPEC) },
+	{ }
+};

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#10 lore raw

Re: [RFC 2/9] mfd: Add driver for Multifunction USB Device

Quoted 15 lines · Noralf Trønnes
Quoted 6 lines
Quoted 6 lines
drivers/mfd/mud.c new file +6 -0
+static void mud_irq_urb_completion(struct urb *urb)
+{
+	struct device *dev = &urb->dev->dev;
+	int ret;
+
+	mdebug("%s: actual_length=%u\n", __func__, urb->actual_length);
Quoted 8 lines

drivers/mfd/mud.c line 172

Quoted 6 lines
drivers/mfd/mud.c new file +6 -0
+
+	switch (urb->status) {
+	case 0:
+		mud_irq_queue(urb);
+		break;
+	case -EPROTO:	/* FIXME: verify: dwc2 reports this on disconnect */

What does this mean? Why can't you fix it now?

Quoted 2 lines

I don't know if this is a dwc2 driver problem or if EPROTO is a valid disconnect error. I haven't seen it in other gadget drivers, so I need

Note: This is not a gadget driver. You should be looking in device drivers.

Quoted 1 line

to look more into this or even better if someone from USB can answer this.

See Documentation/driver-api/usb/error-codes.rst. In short, -EPROTO is one of several status codes you may get when an URB fails because the device was disconnected.

Quoted 16 lines
Quoted 7 lines

drivers/mfd/mud.c line 176

Quoted 4 lines
drivers/mfd/mud.c new file +4 -0
+	case -ECONNRESET:
+	case -ENOENT:
+	case -ESHUTDOWN:
+		dev_dbg(dev, "irq urb shutting down with status: %d\n", urb->status);

s/irq/IRQ/ in all comments and prints.

Quoted 8 lines

Same with URB?

drivers/mfd/mud.c line 180

Quoted 4 lines
drivers/mfd/mud.c new file +4 -0
+		return;
+	default:
+		dev_dbg(dev, "irq urb failure with status: %d\n", urb->status);
+		break;

So it's failed, but you're going to attempt to submit it anyway?

Quoted 4 lines

Yes, I don't know the reason why it failed, it might succeed the next time. But this is also something that someone with real life experience with USB failures could weigh in on. Maybe I should send a reset request so the device can reset its state machine, I don't know.

USB connections are usually pretty reliable. Sometimes there are transient errors, but they are relatively rare. No one would criticize a driver for giving up the first time it gets an error (especially if there was an easy way to reset it) -- but people will get annoyed if a ton of error messages shows up on the console whenever they unplug the device.

In general, the overall design of the driver seems to be reasonable. I can't judge the interfaces with other subsystems or the other aspects of their design, but the USB part is okay. (I haven't gone through it in detail.)

Alan Stern

#11 lore raw

RFC 3/9 usb: gadget: function: Add Multifunction USB Device support

In reply to Noralf Trønnes

This is the gadget side of the mfd host driver. It provides a USB function that drivers can hook into providing functions like gpio and display as regmaps to the host. These drivers are configured through configfs.

---
 drivers/usb/gadget/Kconfig               |  10 +
 drivers/usb/gadget/function/Makefile     |   2 +
 drivers/usb/gadget/function/f_mud.c      | 913 ++++++++++++++++++++++
 drivers/usb/gadget/function/f_mud.h      | 210 +++++
 drivers/usb/gadget/function/mud_regmap.c | 936 +++++++++++++++++++++++
 5 files changed, 2071 insertions(+)
 create mode 100644 drivers/usb/gadget/function/f_mud.c
 create mode 100644 drivers/usb/gadget/function/f_mud.h
 create mode 100644 drivers/usb/gadget/function/mud_regmap.c
drivers/usb/gadget/Kconfig +10 -0
index 02ff850278b1..9551876ffe08 100644
@@ -216,6 +216,9 @@ config USB_F_PRINTER
 config USB_F_TCM
 	tristate
 
+config USB_F_MUD
+	tristate
+
 # this first set of drivers all depend on bulk-capable hardware.
 
 config USB_CONFIGFS
@@ -483,6 +486,13 @@ config USB_CONFIGFS_F_TCM
 	  Both protocols can work on USB2.0 and USB3.0.
 	  UAS utilizes the USB 3.0 feature called streams support.
 
+menuconfig USB_CONFIGFS_F_MUD
+	bool "Multifunction USB Device"
+	depends on USB_CONFIGFS
+	select USB_F_MUD
+	help
+	  Core support for the Multifunction USB Device.
+
 choice
 	tristate "USB Gadget precomposed configurations"
 	default USB_ETH
drivers/usb/gadget/function/Makefile +2 -0
index 5d3a6cf02218..b6e31b511521 100644
@@ -50,3 +50,5 @@ usb_f_printer-y			:= f_printer.o
 obj-$(CONFIG_USB_F_PRINTER)	+= usb_f_printer.o
 usb_f_tcm-y			:= f_tcm.o
 obj-$(CONFIG_USB_F_TCM)		+= usb_f_tcm.o
+usb_f_mud-y			:= f_mud.o mud_regmap.o
+obj-$(CONFIG_USB_F_MUD)		+= usb_f_mud.o
drivers/usb/gadget/function/f_mud.c new file +913 -0

913 lines not on this page · raw message

drivers/usb/gadget/function/f_mud.h new file +210 -0

210 lines not on this page · raw message

drivers/usb/gadget/function/mud_regmap.c new file +936 -0

936 lines not on this page · raw message

Signature · 1 line
2.23.0
#12 lore raw

RFC 4/9 pinctrl: Add Multifunction USB Device pinctrl driver

In reply to Noralf Trønnes

The Multifunction USB Device has optional support for gpio and pin configuration. Interrupts are supported if the device supports it.

---
 drivers/pinctrl/Kconfig       |   9 +
 drivers/pinctrl/Makefile      |   1 +
 drivers/pinctrl/pinctrl-mud.c | 657 ++++++++++++++++++++++++++++++++++
 drivers/pinctrl/pinctrl-mud.h |  89 +++++
 4 files changed, 756 insertions(+)
 create mode 100644 drivers/pinctrl/pinctrl-mud.c
 create mode 100644 drivers/pinctrl/pinctrl-mud.h
drivers/pinctrl/Kconfig +9 -0
index df0ef69dd474..ee3532c64411 100644
@@ -384,6 +384,15 @@ config PINCTRL_OCELOT
 	select OF_GPIO
 	select REGMAP_MMIO
 
+config PINCTRL_MUD
+	tristate "Multifunction USB Device pinctrl driver"
+	depends on MFD_MUD
+	select GENERIC_PINCONF
+	select GPIOLIB
+	select GPIOLIB_IRQCHIP
+	help
+	  Support for GPIOs on Multifunction USB Devices.
+
 source "drivers/pinctrl/actions/Kconfig"
 source "drivers/pinctrl/aspeed/Kconfig"
 source "drivers/pinctrl/bcm/Kconfig"
drivers/pinctrl/Makefile +1 -0
index 879f312bfb75..782cc7f286b7 100644
@@ -47,6 +47,7 @@ obj-$(CONFIG_PINCTRL_INGENIC)	+= pinctrl-ingenic.o
 obj-$(CONFIG_PINCTRL_RK805)	+= pinctrl-rk805.o
 obj-$(CONFIG_PINCTRL_OCELOT)	+= pinctrl-ocelot.o
 obj-$(CONFIG_PINCTRL_EQUILIBRIUM)   += pinctrl-equilibrium.o
+obj-$(CONFIG_PINCTRL_MUD)	+= pinctrl-mud.o
 
 obj-y				+= actions/
 obj-$(CONFIG_ARCH_ASPEED)	+= aspeed/
drivers/pinctrl/pinctrl-mud.c new file +657 -0

657 lines not on this page · raw message

drivers/pinctrl/pinctrl-mud.h new file +89 -0

89 lines not on this page · raw message

Signature · 1 line
2.23.0
#13 lore raw

RFC 5/9 usb: gadget: function: mud: Add gpio support

In reply to Noralf Trønnes

Add optional gpio functionality to the Multifunction USB Device.

---
 drivers/usb/gadget/Kconfig               |  14 +
 drivers/usb/gadget/function/Makefile     |   2 +
 drivers/usb/gadget/function/f_mud_pins.c | 962 +++++++++++++++++++++++
 3 files changed, 978 insertions(+)
 create mode 100644 drivers/usb/gadget/function/f_mud_pins.c
drivers/usb/gadget/Kconfig +14 -0
index 9551876ffe08..d6285146ec76 100644
@@ -219,6 +219,9 @@ config USB_F_TCM
 config USB_F_MUD
 	tristate
 
+config USB_F_MUD_PINS
+	tristate
+
 # this first set of drivers all depend on bulk-capable hardware.
 
 config USB_CONFIGFS
@@ -493,6 +496,17 @@ menuconfig USB_CONFIGFS_F_MUD
 	help
 	  Core support for the Multifunction USB Device.
 
+if USB_F_MUD
+
+config USB_CONFIGFS_F_MUD_PINS
+	bool "Multifunction USB Device GPIO"
+	depends on PINCTRL
+	select USB_F_MUD_PINS
+	help
+	  GPIO support for the Multifunction USB Device.
+
+endif # USB_F_MUD
+
 choice
 	tristate "USB Gadget precomposed configurations"
 	default USB_ETH
drivers/usb/gadget/function/Makefile +2 -0
index b6e31b511521..2e24227fcc12 100644
@@ -52,3 +52,5 @@ usb_f_tcm-y			:= f_tcm.o
 obj-$(CONFIG_USB_F_TCM)		+= usb_f_tcm.o
 usb_f_mud-y			:= f_mud.o mud_regmap.o
 obj-$(CONFIG_USB_F_MUD)		+= usb_f_mud.o
+usb_f_mud_pins-y		:= f_mud_pins.o
+obj-$(CONFIG_USB_F_MUD_PINS)	+= usb_f_mud_pins.o
drivers/usb/gadget/function/f_mud_pins.c new file +962 -0

962 lines not on this page · raw message

Signature · 1 line
2.23.0
#14 lore raw

RFC 6/9 regmap: Speed up _regmap_raw_write_impl() for large buffers

In reply to Noralf Trønnes

When writing a 3MB buffer the unwritable check in _regmap_raw_write_impl() adds a ~20ms overhead on a Raspberry Pi 4.
Amend this by avoiding the check if it's not necessary.

---
 drivers/base/regmap/regmap.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/base/regmap/regmap.c +6 -4
index 19f57ccfbe1d..cd876309a74b 100644
@@ -1489,10 +1489,12 @@ static int _regmap_raw_write_impl(struct regmap *map, unsigned int reg,
 	WARN_ON(!map->bus);
 
 	/* Check for unwritable registers before we start */
-	for (i = 0; i < val_len / map->format.val_bytes; i++)
-		if (!regmap_writeable(map,
-				     reg + regmap_get_offset(map, i)))
-			return -EINVAL;
+	if (map->max_register || map->writeable_reg || map->wr_table) {
+		for (i = 0; i < val_len / map->format.val_bytes; i++)
+			if (!regmap_writeable(map,
+					      reg + regmap_get_offset(map, i)))
+				return -EINVAL;
+	}
 
 	if (!map->cache_bypass && map->format.parse_val) {
 		unsigned int ival;
Signature · 1 line
2.23.0
maintainer
#15 lore raw

Re: [RFC 6/9] regmap: Speed up _regmap_raw_write_impl() for large buffers

Quoted 3 lines · Noralf Trønnes

When writing a 3MB buffer the unwritable check in _regmap_raw_write_impl() adds a ~20ms overhead on a Raspberry Pi 4.
Amend this by avoiding the check if it's not necessary.

This is a generic optimization, why is it mixed in with the rest of this series? There is no dependency either way :(

drivers/base/regmap/regmap.c static int _regmap_raw_write_impl(struct regmap *map, unsigned int reg, · line 1497

Quoted 11 lines
drivers/base/regmap/regmap.c +6 -4
 	/* Check for unwritable registers before we start */
-	for (i = 0; i < val_len / map->format.val_bytes; i++)
-		if (!regmap_writeable(map,
-				     reg + regmap_get_offset(map, i)))
-			return -EINVAL;
+	if (map->max_register || map->writeable_reg || map->wr_table) {
+		for (i = 0; i < val_len / map->format.val_bytes; i++)
+			if (!regmap_writeable(map,
+					      reg + regmap_get_offset(map, i)))
+				return -EINVAL;
+	}

This is going to break if there is any change to the implementation of regmap_writeable(). The code should at least be next to that if not actually shared so that this doesn't happen. I'd suggest implementing a function regmap_writeable_range() and then making regmap_writeable() call that.

#16 lore raw

RFC 7/9 drm: Add Multifunction USB Device display driver

In reply to Noralf Trønnes

The Multifunction USB Device has optional support for displays. LZ4 compression is used if the device supports it.

The driver is MIT licensed in the hope that parts of it can be used on the BSD's.

---
 drivers/gpu/drm/Kconfig       |    2 +
 drivers/gpu/drm/Makefile      |    1 +
 drivers/gpu/drm/mud/Kconfig   |   15 +
 drivers/gpu/drm/mud/Makefile  |    3 +
 drivers/gpu/drm/mud/mud_drm.c | 1198 +++++++++++++++++++++++++++++++++
 drivers/gpu/drm/mud/mud_drm.h |  137 ++++
 6 files changed, 1356 insertions(+)
 create mode 100644 drivers/gpu/drm/mud/Kconfig
 create mode 100644 drivers/gpu/drm/mud/Makefile
 create mode 100644 drivers/gpu/drm/mud/mud_drm.c
 create mode 100644 drivers/gpu/drm/mud/mud_drm.h
drivers/gpu/drm/Kconfig +2 -0
index bfdadc3667e0..8ddc0d0e82cc 100644
@@ -387,6 +387,8 @@ source "drivers/gpu/drm/aspeed/Kconfig"
 
 source "drivers/gpu/drm/mcde/Kconfig"
 
+source "drivers/gpu/drm/mud/Kconfig"
+
 # Keep legacy drivers last
 
 menuconfig DRM_LEGACY
drivers/gpu/drm/Makefile +1 -0
index 9f1c7c486f88..5a5eab598d39 100644
@@ -122,3 +122,4 @@ obj-$(CONFIG_DRM_LIMA)  += lima/
 obj-$(CONFIG_DRM_PANFROST) += panfrost/
 obj-$(CONFIG_DRM_ASPEED_GFX) += aspeed/
 obj-$(CONFIG_DRM_MCDE) += mcde/
+obj-y			+= mud/
drivers/gpu/drm/mud/Kconfig new file +15 -0
new file mode 100644
index 000000000000..440e994ca0a2
@@ -0,0 +1,15 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+config DRM_MUD
+	tristate "Multifunction USB Device Display"
+	depends on DRM && USB && MFD_MUD
+	select LZ4_COMPRESS
+	select DRM_KMS_HELPER
+
+	select DRM_GEM_CMA_HELPER
+	select DRM_GEM_SHMEM_HELPER
+
+	select BACKLIGHT_CLASS_DEVICE
+	help
+	 This is a KMS driver for Multifunction USB Device displays or display
+	 adapters.
drivers/gpu/drm/mud/Makefile new file +3 -0
new file mode 100644
index 000000000000..d5941d33bcd9
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+obj-$(CONFIG_DRM_MUD)			+= mud_drm.o
drivers/gpu/drm/mud/mud_drm.c new file +1198 -0

1,198 lines not on this page · raw message

drivers/gpu/drm/mud/mud_drm.h new file +137 -0

137 lines not on this page · raw message

Signature · 1 line
2.23.0
#17 lore raw

RFC 8/9 drm/client: Add drm_client_init_from_id() and drm_client_modeset_set()

In reply to Noralf Trønnes

drm_client_init_from_id() provides a way for clients to add a client based on the minor. drm_client_modeset_set() provides a way to set the modeset for clients that handles connectors and display mode on their own.

---
 drivers/gpu/drm/drm_client.c         | 37 ++++++++++++++++++++
 drivers/gpu/drm/drm_client_modeset.c | 52 ++++++++++++++++++++++++++++
 include/drm/drm_client.h             |  4 +++
 3 files changed, 93 insertions(+)
drivers/gpu/drm/drm_client.c +37 -0
index d9a2e3695525..dbd73fe8d987 100644
@@ -112,6 +112,43 @@ int drm_client_init(struct drm_device *dev, struct drm_client_dev *client,
 }
 EXPORT_SYMBOL(drm_client_init);
 
+/**
+ * drm_client_init_from_id - Initialise a DRM client
+ * @minor_id: DRM minor id
+ * @client: DRM client
+ * @name: Client name
+ * @funcs: DRM client functions (optional)
+ *
+ * This function looks up the drm_device using the minor id and initializes the client.
+ * It also registeres the client to avoid a possible race with DRM device unregister.
+ *
+ * See drm_client_init() and drm_client_register().
+ *
+ * Returns:
+ * Zero on success or negative error code on failure.
+ */
+int drm_client_init_from_id(unsigned int minor_id, struct drm_client_dev *client,
+			    const char *name, const struct drm_client_funcs *funcs)
+{
+	struct drm_minor *minor;
+	int ret;
+
+	minor = drm_minor_acquire(minor_id);
+	if (IS_ERR(minor))
+		return PTR_ERR(minor);
+
+	mutex_lock(&minor->dev->clientlist_mutex);
+	ret = drm_client_init(minor->dev, client, name, funcs);
+	if (!ret)
+		list_add(&client->list, &minor->dev->clientlist);
+	mutex_unlock(&minor->dev->clientlist_mutex);
+
+	drm_minor_release(minor);
+
+	return ret;
+}
+EXPORT_SYMBOL(drm_client_init_from_id);
+
 /**
  * drm_client_register - Register client
  * @client: DRM client
drivers/gpu/drm/drm_client_modeset.c +52 -0

58 lines not on this page · raw message

include/drm/drm_client.h +4 -0
index 5cf2c5dd8b1e..97e4157d07c5 100644
@@ -104,6 +104,8 @@ struct drm_client_dev {
 
 int drm_client_init(struct drm_device *dev, struct drm_client_dev *client,
 		    const char *name, const struct drm_client_funcs *funcs);
+int drm_client_init_from_id(unsigned int minor_id, struct drm_client_dev *client,
+			    const char *name, const struct drm_client_funcs *funcs);
 void drm_client_release(struct drm_client_dev *client);
 void drm_client_register(struct drm_client_dev *client);
 
@@ -155,6 +157,8 @@ void drm_client_buffer_vunmap(struct drm_client_buffer *buffer);
 int drm_client_modeset_create(struct drm_client_dev *client);
 void drm_client_modeset_free(struct drm_client_dev *client);
 int drm_client_modeset_probe(struct drm_client_dev *client, unsigned int width, unsigned int height);
+int drm_client_modeset_set(struct drm_client_dev *client, struct drm_connector *connector,
+			   struct drm_display_mode *mode, struct drm_framebuffer *fb);
 bool drm_client_rotation(struct drm_mode_set *modeset, unsigned int *rotation);
 int drm_client_modeset_commit_force(struct drm_client_dev *client);
 int drm_client_modeset_commit(struct drm_client_dev *client);
Signature · 6 lines
2.23.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#18 lore raw

Re: [RFC 8/9] drm/client: Add drm_client_init_from_id() and drm_client_modeset_set()

Quoted 18 lines · Noralf Trønnes

drm_client_init_from_id() provides a way for clients to add a client based on the minor. drm_client_modeset_set() provides a way to set the modeset for clients that handles connectors and display mode on their own.

---
 drivers/gpu/drm/drm_client.c         | 37 ++++++++++++++++++++
 drivers/gpu/drm/drm_client_modeset.c | 52 ++++++++++++++++++++++++++++
 include/drm/drm_client.h             |  4 +++
 3 files changed, 93 insertions(+)
drivers/gpu/drm/drm_client.c +0 -0
index d9a2e3695525..dbd73fe8d987 100644
@@ -112,6 +112,43 @@ int drm_client_init(struct drm_device *dev, struct drm_client_dev *client,
 }
 EXPORT_SYMBOL(drm_client_init);

drivers/gpu/drm/drm_client.c int drm_client_init(struct drm_device *dev, struct drm_client_dev *client, · line 123

Quoted 10 lines
drivers/gpu/drm/drm_client.c +9 -0
 
+/**
+ * drm_client_init_from_id - Initialise a DRM client
+ * @minor_id: DRM minor id
+ * @client: DRM client
+ * @name: Client name
+ * @funcs: DRM client functions (optional)
+ *
+ * This function looks up the drm_device using the minor id and initializes the client.
+ * It also registeres the client to avoid a possible race with DRM device unregister.

I think another sentence here would be good, explaining that this is for drivers outside of drm to expose a specific drm driver in some fashion, and just outright mention your use-case as an example.

I'm also not sure whether lookup-by-minor is the greatest thing for kernel-internal lookups like this, maybe Greg has some idea?

Quoted 44 lines
drivers/gpu/drm/drm_client.c +28 -0
+ *
+ * See drm_client_init() and drm_client_register().
+ *
+ * Returns:
+ * Zero on success or negative error code on failure.
+ */
+int drm_client_init_from_id(unsigned int minor_id, struct drm_client_dev *client,
+			    const char *name, const struct drm_client_funcs *funcs)
+{
+	struct drm_minor *minor;
+	int ret;
+
+	minor = drm_minor_acquire(minor_id);
+	if (IS_ERR(minor))
+		return PTR_ERR(minor);
+
+	mutex_lock(&minor->dev->clientlist_mutex);
+	ret = drm_client_init(minor->dev, client, name, funcs);
+	if (!ret)
+		list_add(&client->list, &minor->dev->clientlist);
+	mutex_unlock(&minor->dev->clientlist_mutex);
+
+	drm_minor_release(minor);
+
+	return ret;
+}
+EXPORT_SYMBOL(drm_client_init_from_id);
+
 /**
  * drm_client_register - Register client
  * @client: DRM client
drivers/gpu/drm/drm_client_modeset.c +5 -0
index 895b73f23079..9396267e646c 100644
@@ -807,6 +807,58 @@ int drm_client_modeset_probe(struct drm_client_dev *client, unsigned int width,
 }
 EXPORT_SYMBOL(drm_client_modeset_probe);
 
+/**
+ * drm_client_modeset_set() - Set modeset
+ * @client: DRM client
+ * @connector: Connector
+ * @mode: Display mode

drivers/gpu/drm/drm_client_modeset.c int drm_client_modeset_probe(struct drm_client_dev *client, unsigned int width, · line 824

Quoted 10 lines
drivers/gpu/drm/drm_client_modeset.c +10 -0
+ * @fb: Framebuffer
+ *
+ * This function releases any current modeset info and sets the new modeset in
+ * the client's modeset array.
+ *
+ * Returns:
+ * Zero on success or negative error code on failure.
+ */
+int drm_client_modeset_set(struct drm_client_dev *client, struct drm_connector *connector,
+			   struct drm_display_mode *mode, struct drm_framebuffer *fb)

Hm, since you need the functionality would be kinda neat to wire this up for the fbdev emulation too. Ofc without reallocating the framebuffer (fbdev can't do that), so would be limited to lower resolutions than we booted with.
-Daniel

Quoted 69 lines
drivers/gpu/drm/drm_client_modeset.c +37 -0
+{
+	struct drm_mode_set *modeset;
+	int ret = -ENOENT;
+
+	mutex_lock(&client->modeset_mutex);
+
+	drm_client_modeset_release(client);
+
+	if (!connector || !mode || !fb) {
+		ret = 0;
+		goto unlock;
+	}
+
+	drm_client_for_each_modeset(modeset, client) {
+		if (!connector_has_possible_crtc(connector, modeset->crtc))
+			continue;
+
+		modeset->mode = drm_mode_duplicate(client->dev, mode);
+		if (!modeset->mode) {
+			ret = -ENOMEM;
+			break;
+		}
+
+		drm_connector_get(connector);
+		modeset->connectors[modeset->num_connectors++] = connector;
+
+		modeset->fb = fb;
+		ret = 0;
+		break;
+	}
+unlock:
+	mutex_unlock(&client->modeset_mutex);
+
+	return ret;
+}
+EXPORT_SYMBOL(drm_client_modeset_set);
+
 /**
  * drm_client_rotation() - Check the initial rotation value
  * @modeset: DRM modeset
include/drm/drm_client.h +4 -0
index 5cf2c5dd8b1e..97e4157d07c5 100644
@@ -104,6 +104,8 @@ struct drm_client_dev {
 
 int drm_client_init(struct drm_device *dev, struct drm_client_dev *client,
 		    const char *name, const struct drm_client_funcs *funcs);
+int drm_client_init_from_id(unsigned int minor_id, struct drm_client_dev *client,
+			    const char *name, const struct drm_client_funcs *funcs);
 void drm_client_release(struct drm_client_dev *client);
 void drm_client_register(struct drm_client_dev *client);
 
@@ -155,6 +157,8 @@ void drm_client_buffer_vunmap(struct drm_client_buffer *buffer);
 int drm_client_modeset_create(struct drm_client_dev *client);
 void drm_client_modeset_free(struct drm_client_dev *client);
 int drm_client_modeset_probe(struct drm_client_dev *client, unsigned int width, unsigned int height);
+int drm_client_modeset_set(struct drm_client_dev *client, struct drm_connector *connector,
+			   struct drm_display_mode *mode, struct drm_framebuffer *fb);
 bool drm_client_rotation(struct drm_mode_set *modeset, unsigned int *rotation);
 int drm_client_modeset_commit_force(struct drm_client_dev *client);
 int drm_client_modeset_commit(struct drm_client_dev *client);
Signature · 6 lines
2.23.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#19 lore raw

Re: [RFC 8/9] drm/client: Add drm_client_init_from_id() and drm_client_modeset_set()

Den 17.02.2020 10.38, skrev Daniel Vetter:

Quoted 34 lines · Noralf Trønnes
Quoted 18 lines

drm_client_init_from_id() provides a way for clients to add a client based on the minor. drm_client_modeset_set() provides a way to set the modeset for clients that handles connectors and display mode on their own.

---
 drivers/gpu/drm/drm_client.c         | 37 ++++++++++++++++++++
 drivers/gpu/drm/drm_client_modeset.c | 52 ++++++++++++++++++++++++++++
 include/drm/drm_client.h             |  4 +++
 3 files changed, 93 insertions(+)
drivers/gpu/drm/drm_client.c +0 -0
index d9a2e3695525..dbd73fe8d987 100644
@@ -112,6 +112,43 @@ int drm_client_init(struct drm_device *dev, struct drm_client_dev *client,
 }
 EXPORT_SYMBOL(drm_client_init);

drivers/gpu/drm/drm_client.c int drm_client_init(struct drm_device *dev, struct drm_client_dev *client, · line 123

Quoted 10 lines
drivers/gpu/drm/drm_client.c +9 -0
 
+/**
+ * drm_client_init_from_id - Initialise a DRM client
+ * @minor_id: DRM minor id
+ * @client: DRM client
+ * @name: Client name
+ * @funcs: DRM client functions (optional)
+ *
+ * This function looks up the drm_device using the minor id and initializes the client.
+ * It also registeres the client to avoid a possible race with DRM device unregister.

I think another sentence here would be good, explaining that this is for drivers outside of drm to expose a specific drm driver in some fashion, and just outright mention your use-case as an example.

Quoted 3 lines

I'm also not sure whether lookup-by-minor is the greatest thing for kernel-internal lookups like this, maybe Greg has some idea?

What alternatives do you see? Parent device name?

I did a scan to see what others are doing, and most have a consumer name lookup on the struct device (Device Tree or lookup tables):

struct reset_control *__reset_control_get(struct device *dev, const char *id,

					  int index, bool shared,
					  bool optional, bool acquired);

struct iio_channel *iio_channel_get(struct device *dev,

				    const char *consumer_channel);

struct regulator *__must_check regulator_get(struct device *dev,

					     const char *id);

struct pwm_device *pwm_get(struct device *dev, const char *con_id)

struct gpio_desc *__must_check gpiod_get(struct device *dev,

					 const char *con_id,
					 enum gpiod_flags flags);

SPI and I2C use the bus index as lookup:

extern struct i2c_adapter *i2c_get_adapter(int nr);

extern struct spi_controller *spi_busnum_to_master(u16 busnum);

Noralf.

Quoted 23 lines
Quoted 23 lines
drivers/gpu/drm/drm_client.c +23 -0
+ *
+ * See drm_client_init() and drm_client_register().
+ *
+ * Returns:
+ * Zero on success or negative error code on failure.
+ */
+int drm_client_init_from_id(unsigned int minor_id, struct drm_client_dev *client,
+			    const char *name, const struct drm_client_funcs *funcs)
+{
+	struct drm_minor *minor;
+	int ret;
+
+	minor = drm_minor_acquire(minor_id);
+	if (IS_ERR(minor))
+		return PTR_ERR(minor);
+
+	mutex_lock(&minor->dev->clientlist_mutex);
+	ret = drm_client_init(minor->dev, client, name, funcs);
+	if (!ret)
+		list_add(&client->list, &minor->dev->clientlist);
+	mutex_unlock(&minor->dev->clientlist_mutex);
+
+	drm_minor_release(minor);
Quoted 8 lines

drivers/gpu/drm/drm_client.c int drm_client_init(struct drm_device *dev, struct drm_client_dev *client, · line 154

Quoted 8 lines
drivers/gpu/drm/drm_client.c +5 -0
+
+	return ret;
+}
+EXPORT_SYMBOL(drm_client_init_from_id);
+
 /**
  * drm_client_register - Register client
  * @client: DRM client

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#20 lore raw

Re: [RFC 8/9] drm/client: Add drm_client_init_from_id() and drm_client_modeset_set()

Quoted 35 lines · Noralf Trønnes

Den 17.02.2020 10.38, skrev Daniel Vetter:

Quoted 34 lines · Noralf Trønnes
Quoted 18 lines

drm_client_init_from_id() provides a way for clients to add a client based on the minor. drm_client_modeset_set() provides a way to set the modeset for clients that handles connectors and display mode on their own.

---
 drivers/gpu/drm/drm_client.c         | 37 ++++++++++++++++++++
 drivers/gpu/drm/drm_client_modeset.c | 52 ++++++++++++++++++++++++++++
 include/drm/drm_client.h             |  4 +++
 3 files changed, 93 insertions(+)
drivers/gpu/drm/drm_client.c +0 -0
index d9a2e3695525..dbd73fe8d987 100644
@@ -112,6 +112,43 @@ int drm_client_init(struct drm_device *dev, struct drm_client_dev *client,
 }
 EXPORT_SYMBOL(drm_client_init);

drivers/gpu/drm/drm_client.c int drm_client_init(struct drm_device *dev, struct drm_client_dev *client, · line 123

Quoted 10 lines
drivers/gpu/drm/drm_client.c +9 -0
 
+/**
+ * drm_client_init_from_id - Initialise a DRM client
+ * @minor_id: DRM minor id
+ * @client: DRM client
+ * @name: Client name
+ * @funcs: DRM client functions (optional)
+ *
+ * This function looks up the drm_device using the minor id and initializes the client.
+ * It also registeres the client to avoid a possible race with DRM device unregister.

I think another sentence here would be good, explaining that this is for drivers outside of drm to expose a specific drm driver in some fashion, and just outright mention your use-case as an example.

Quoted 8 lines
Quoted 3 lines

I'm also not sure whether lookup-by-minor is the greatest thing for kernel-internal lookups like this, maybe Greg has some idea?

What alternatives do you see? Parent device name?

I did a scan to see what others are doing, and most have a consumer name lookup on the struct device (Device Tree or lookup tables):

I think those are all for other purposes. What we want here is that some interface things binds to something else. I was thinking sysfs paths to the underlying struct device (i.e. the one in drm_device->dev) might be neater, since that's more unique. Using minors we get that entire trouble of having duplicates (and in the past triplicatres) due to our uapi-flavors. But in sysfs there's only one underlying device.

Anyway if there's no precedence and Greg doesn't have ideas either then I guess we can just go with minor ids. It's about as good as anything else really.
-Daniel

Quoted 58 lines

struct reset_control *__reset_control_get(struct device *dev, const char *id,
int index, bool shared, bool optional, bool acquired);

struct iio_channel *iio_channel_get(struct device *dev,

                                    const char *consumer_channel);

struct regulator *__must_check regulator_get(struct device *dev,

                                             const char *id);

struct pwm_device *pwm_get(struct device *dev, const char *con_id)

struct gpio_desc *__must_check gpiod_get(struct device *dev,
const char *con_id, enum gpiod_flags flags);

SPI and I2C use the bus index as lookup:

extern struct i2c_adapter *i2c_get_adapter(int nr);

extern struct spi_controller *spi_busnum_to_master(u16 busnum);

Noralf.

Quoted 31 lines
Quoted 31 lines
drivers/gpu/drm/drm_client.c +28 -0
+ *
+ * See drm_client_init() and drm_client_register().
+ *
+ * Returns:
+ * Zero on success or negative error code on failure.
+ */
+int drm_client_init_from_id(unsigned int minor_id, struct drm_client_dev *client,
+                        const char *name, const struct drm_client_funcs *funcs)
+{
+    struct drm_minor *minor;
+    int ret;
+
+    minor = drm_minor_acquire(minor_id);
+    if (IS_ERR(minor))
+            return PTR_ERR(minor);
+
+    mutex_lock(&minor->dev->clientlist_mutex);
+    ret = drm_client_init(minor->dev, client, name, funcs);
+    if (!ret)
+            list_add(&client->list, &minor->dev->clientlist);
+    mutex_unlock(&minor->dev->clientlist_mutex);
+
+    drm_minor_release(minor);
+
+    return ret;
+}
+EXPORT_SYMBOL(drm_client_init_from_id);
+
 /**
  * drm_client_register - Register client
  * @client: DRM client
Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#21 lore raw

RFC 9/9 usb: gadget: function: mud: Add display support

In reply to Noralf Trønnes

Add optional display functionality to the Multifunction USB Device. The bulk of the code is placed in the drm subsystem since it's reaching into the drm internals.

---
 drivers/gpu/drm/mud/Kconfig             |   3 +
 drivers/gpu/drm/mud/Makefile            |   1 +
 drivers/gpu/drm/mud/mud_drm_gadget.c    | 889 ++++++++++++++++++++++++
 drivers/usb/gadget/Kconfig              |  12 +
 drivers/usb/gadget/function/Makefile    |   2 +
 drivers/usb/gadget/function/f_mud_drm.c | 181 +++++
 6 files changed, 1088 insertions(+)
 create mode 100644 drivers/gpu/drm/mud/mud_drm_gadget.c
 create mode 100644 drivers/usb/gadget/function/f_mud_drm.c
drivers/gpu/drm/mud/Kconfig +3 -0
index 440e994ca0a2..b3c6d073cc9c 100644
@@ -13,3 +13,6 @@ config DRM_MUD
 	help
 	 This is a KMS driver for Multifunction USB Device displays or display
 	 adapters.
+
+config DRM_MUD_GADGET
+	tristate
drivers/gpu/drm/mud/Makefile +1 -0
index d5941d33bcd9..56d2c39ac0eb 100644
@@ -1,3 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0-or-later
 
 obj-$(CONFIG_DRM_MUD)			+= mud_drm.o
+obj-$(CONFIG_DRM_MUD_GADGET)		+= mud_drm_gadget.o
drivers/gpu/drm/mud/mud_drm_gadget.c new file +889 -0

889 lines not on this page · raw message

drivers/usb/gadget/Kconfig +12 -0
index d6285146ec76..e30cb039f35d 100644
@@ -219,6 +219,9 @@ config USB_F_TCM
 config USB_F_MUD
 	tristate
 
+config USB_F_MUD_DRM
+	tristate
+
 config USB_F_MUD_PINS
 	tristate
 
@@ -498,6 +501,15 @@ menuconfig USB_CONFIGFS_F_MUD
 
 if USB_F_MUD
 
+config USB_CONFIGFS_F_MUD_DRM
+	bool "Multifunction USB Device Display"
+	depends on DRM
+	select DRM_MUD_GADGET
+	select USB_F_MUD_DRM
+	select LZ4_DECOMPRESS
+	help
+	  Display support for the Multifunction USB Device.
+
 config USB_CONFIGFS_F_MUD_PINS
 	bool "Multifunction USB Device GPIO"
 	depends on PINCTRL
drivers/usb/gadget/function/Makefile +2 -0
index 2e24227fcc12..d0f93ce6bbe9 100644
@@ -52,5 +52,7 @@ usb_f_tcm-y			:= f_tcm.o
 obj-$(CONFIG_USB_F_TCM)		+= usb_f_tcm.o
 usb_f_mud-y			:= f_mud.o mud_regmap.o
 obj-$(CONFIG_USB_F_MUD)		+= usb_f_mud.o
+usb_f_mud_drm-y			:= f_mud_drm.o
+obj-$(CONFIG_USB_F_MUD_DRM)	+= usb_f_mud_drm.o
 usb_f_mud_pins-y		:= f_mud_pins.o
 obj-$(CONFIG_USB_F_MUD_PINS)	+= usb_f_mud_pins.o
drivers/usb/gadget/function/f_mud_drm.c new file +181 -0

181 lines not on this page · raw message

Signature · 1 line
2.23.0
#22 lore raw
In reply to Noralf Trønnes
Quoted 32 lines · Noralf Trønnes

Hi,

A while back I had the idea to turn a Raspberry Pi Zero into a $5 USB to HDMI/SDTV/DSI/DPI display adapter.

Thinking about how to represent the display to the driver I realised that hardware use registers as API. And Linux does have a generic register abstraction: regmap. Furthermore this means that if I can do a regmap over USB implementation, it will be easy to do other functions like gpio, adc and others. After a few iterations trying to understand the USB subsystem and satisfying driver requirements, I now have something that looks promising.

I'm sending out an early version hoping to get feedback especially on the core parts that handles regmap and interrupts.

Overview:

          USB Host          :         USB Device
                            :
            --------------  :  ------------------
----------  | mfd: mud   |  :  | f_mud          |  ----------
| Driver |  --------------  :  |                |  | Driver |
----------  | regmap-usb |  :  | (mud_regmap)   |  ----------
            --------------  :  ------------------

I've attached 2 drivers:
- gpio/pinctrl: is more or less finished
- display: needs a lot more work

Quoted 8 lines

USB3 device
I've only tested this with usb2 devices (Pi, BBB) so I should get myself a usb3 gadget capable board. My searching didn't turn up much, so this seems to be quite rare. ROCK960 has USB 3.0 type C OTG but the price is $139 which is a bit expensive for this hobby project. Does anyone know of a cheap board?

Noralf.

Pretty cool idea, and the drm side looks really tiny (after all your drm_client work that is). I think the important feedback here is for the usb and regmap stuff, that's totally out of my area. I've commented on the drm_client patch, but that's just minor stuff, imo looks good.

Cheers, Daniel

Quoted 69 lines

Noralf Trønnes (9):
regmap: Add USB support
mfd: Add driver for Multifunction USB Device
usb: gadget: function: Add Multifunction USB Device support
pinctrl: Add Multifunction USB Device pinctrl driver
usb: gadget: function: mud: Add gpio support
regmap: Speed up _regmap_raw_write_impl() for large buffers
drm: Add Multifunction USB Device display driver drm/client: Add drm_client_init_from_id() and drm_client_modeset_set()
usb: gadget: function: mud: Add display support

 drivers/base/regmap/Kconfig              |    8 +-
 drivers/base/regmap/Makefile             |    1 +
 drivers/base/regmap/regmap-usb.c         | 1026 ++++++++++++++++++
 drivers/base/regmap/regmap.c             |   10 +-
 drivers/gpu/drm/Kconfig                  |    2 +
 drivers/gpu/drm/Makefile                 |    1 +
 drivers/gpu/drm/drm_client.c             |   37 +
 drivers/gpu/drm/drm_client_modeset.c     |   52 +
 drivers/gpu/drm/mud/Kconfig              |   18 +
 drivers/gpu/drm/mud/Makefile             |    4 +
 drivers/gpu/drm/mud/mud_drm.c            | 1198 ++++++++++++++++++++++
 drivers/gpu/drm/mud/mud_drm.h            |  137 +++
 drivers/gpu/drm/mud/mud_drm_gadget.c     |  889 ++++++++++++++++
 drivers/mfd/Kconfig                      |    8 +
 drivers/mfd/Makefile                     |    1 +
 drivers/mfd/mud.c                        |  580 +++++++++++
 drivers/pinctrl/Kconfig                  |    9 +
 drivers/pinctrl/Makefile                 |    1 +
 drivers/pinctrl/pinctrl-mud.c            |  657 ++++++++++++
 drivers/pinctrl/pinctrl-mud.h            |   89 ++
 drivers/usb/gadget/Kconfig               |   36 +
 drivers/usb/gadget/function/Makefile     |    6 +
 drivers/usb/gadget/function/f_mud.c      |  913 +++++++++++++++++
 drivers/usb/gadget/function/f_mud.h      |  210 ++++
 drivers/usb/gadget/function/f_mud_drm.c  |  181 ++++
 drivers/usb/gadget/function/f_mud_pins.c |  962 +++++++++++++++++
 drivers/usb/gadget/function/mud_regmap.c |  936 +++++++++++++++++
 include/drm/drm_client.h                 |    4 +
 include/linux/mfd/mud.h                  |   16 +
 include/linux/regmap.h                   |   23 +
 include/linux/regmap_usb.h               |   97 ++
 31 files changed, 8107 insertions(+), 5 deletions(-)
 create mode 100644 drivers/base/regmap/regmap-usb.c
 create mode 100644 drivers/gpu/drm/mud/Kconfig
 create mode 100644 drivers/gpu/drm/mud/Makefile
 create mode 100644 drivers/gpu/drm/mud/mud_drm.c
 create mode 100644 drivers/gpu/drm/mud/mud_drm.h
 create mode 100644 drivers/gpu/drm/mud/mud_drm_gadget.c
 create mode 100644 drivers/mfd/mud.c
 create mode 100644 drivers/pinctrl/pinctrl-mud.c
 create mode 100644 drivers/pinctrl/pinctrl-mud.h
 create mode 100644 drivers/usb/gadget/function/f_mud.c
 create mode 100644 drivers/usb/gadget/function/f_mud.h
 create mode 100644 drivers/usb/gadget/function/f_mud_drm.c
 create mode 100644 drivers/usb/gadget/function/f_mud_pins.c
 create mode 100644 drivers/usb/gadget/function/mud_regmap.c
 create mode 100644 include/linux/mfd/mud.h
 create mode 100644 include/linux/regmap_usb.h
Signature · 6 lines
2.23.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#23 lore raw
In reply to Noralf Trønnes

Hi,

Quoted 18 lines · Noralf Trønnes

Hi,

A while back I had the idea to turn a Raspberry Pi Zero into a $5 USB to HDMI/SDTV/DSI/DPI display adapter.

Thinking about how to represent the display to the driver I realised that hardware use registers as API. And Linux does have a generic register abstraction: regmap. Furthermore this means that if I can do a regmap over USB implementation, it will be easy to do other functions like gpio, adc and others. After a few iterations trying to understand the USB subsystem and satisfying driver requirements, I now have something that looks promising.

I'm sending out an early version hoping to get feedback especially on the core parts that handles regmap and interrupts.

Overview:

Quoted 8 lines
          USB Host          :         USB Device
                            :
            --------------  :  ------------------
----------  | mfd: mud   |  :  | f_mud          |  ----------
| Driver |  --------------  :  |                |  | Driver |
----------  | regmap-usb |  :  | (mud_regmap)   |  ----------
            --------------  :  ------------------

The idea is really like ARA's greybus, but much simpler ! Anyway nice idea, do you have good performance over USB2 and RPi's awful DWC2 gagdet controller ?

Neil

Quoted 67 lines

I've attached 2 drivers:
- gpio/pinctrl: is more or less finished
- display: needs a lot more work

USB3 device
I've only tested this with usb2 devices (Pi, BBB) so I should get myself a usb3 gadget capable board. My searching didn't turn up much, so this seems to be quite rare. ROCK960 has USB 3.0 type C OTG but the price is $139 which is a bit expensive for this hobby project. Does anyone know of a cheap board?

Noralf.

Noralf Trønnes (9):
regmap: Add USB support
mfd: Add driver for Multifunction USB Device
usb: gadget: function: Add Multifunction USB Device support
pinctrl: Add Multifunction USB Device pinctrl driver
usb: gadget: function: mud: Add gpio support
regmap: Speed up _regmap_raw_write_impl() for large buffers
drm: Add Multifunction USB Device display driver drm/client: Add drm_client_init_from_id() and drm_client_modeset_set()
usb: gadget: function: mud: Add display support

 drivers/base/regmap/Kconfig              |    8 +-
 drivers/base/regmap/Makefile             |    1 +
 drivers/base/regmap/regmap-usb.c         | 1026 ++++++++++++++++++
 drivers/base/regmap/regmap.c             |   10 +-
 drivers/gpu/drm/Kconfig                  |    2 +
 drivers/gpu/drm/Makefile                 |    1 +
 drivers/gpu/drm/drm_client.c             |   37 +
 drivers/gpu/drm/drm_client_modeset.c     |   52 +
 drivers/gpu/drm/mud/Kconfig              |   18 +
 drivers/gpu/drm/mud/Makefile             |    4 +
 drivers/gpu/drm/mud/mud_drm.c            | 1198 ++++++++++++++++++++++
 drivers/gpu/drm/mud/mud_drm.h            |  137 +++
 drivers/gpu/drm/mud/mud_drm_gadget.c     |  889 ++++++++++++++++
 drivers/mfd/Kconfig                      |    8 +
 drivers/mfd/Makefile                     |    1 +
 drivers/mfd/mud.c                        |  580 +++++++++++
 drivers/pinctrl/Kconfig                  |    9 +
 drivers/pinctrl/Makefile                 |    1 +
 drivers/pinctrl/pinctrl-mud.c            |  657 ++++++++++++
 drivers/pinctrl/pinctrl-mud.h            |   89 ++
 drivers/usb/gadget/Kconfig               |   36 +
 drivers/usb/gadget/function/Makefile     |    6 +
 drivers/usb/gadget/function/f_mud.c      |  913 +++++++++++++++++
 drivers/usb/gadget/function/f_mud.h      |  210 ++++
 drivers/usb/gadget/function/f_mud_drm.c  |  181 ++++
 drivers/usb/gadget/function/f_mud_pins.c |  962 +++++++++++++++++
 drivers/usb/gadget/function/mud_regmap.c |  936 +++++++++++++++++
 include/drm/drm_client.h                 |    4 +
 include/linux/mfd/mud.h                  |   16 +
 include/linux/regmap.h                   |   23 +
 include/linux/regmap_usb.h               |   97 ++
 31 files changed, 8107 insertions(+), 5 deletions(-)
 create mode 100644 drivers/base/regmap/regmap-usb.c
 create mode 100644 drivers/gpu/drm/mud/Kconfig
 create mode 100644 drivers/gpu/drm/mud/Makefile
 create mode 100644 drivers/gpu/drm/mud/mud_drm.c
 create mode 100644 drivers/gpu/drm/mud/mud_drm.h
 create mode 100644 drivers/gpu/drm/mud/mud_drm_gadget.c
 create mode 100644 drivers/mfd/mud.c
 create mode 100644 drivers/pinctrl/pinctrl-mud.c
Quoted 9 lines
 create mode 100644 drivers/pinctrl/pinctrl-mud.h
 create mode 100644 drivers/usb/gadget/function/f_mud.c
 create mode 100644 drivers/usb/gadget/function/f_mud.h
 create mode 100644 drivers/usb/gadget/function/f_mud_drm.c
 create mode 100644 drivers/usb/gadget/function/f_mud_pins.c
 create mode 100644 drivers/usb/gadget/function/mud_regmap.c
 create mode 100644 include/linux/mfd/mud.h
 create mode 100644 include/linux/regmap_usb.h

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#24 lore raw

Den 17.02.2020 11.32, skrev Neil Armstrong:

Quoted 30 lines

Hi,

Quoted 18 lines · Noralf Trønnes

Hi,

A while back I had the idea to turn a Raspberry Pi Zero into a $5 USB to HDMI/SDTV/DSI/DPI display adapter.

Thinking about how to represent the display to the driver I realised that hardware use registers as API. And Linux does have a generic register abstraction: regmap. Furthermore this means that if I can do a regmap over USB implementation, it will be easy to do other functions like gpio, adc and others. After a few iterations trying to understand the USB subsystem and satisfying driver requirements, I now have something that looks promising.

I'm sending out an early version hoping to get feedback especially on the core parts that handles regmap and interrupts.

Overview:

Quoted 8 lines
          USB Host          :         USB Device
                            :
            --------------  :  ------------------
----------  | mfd: mud   |  :  | f_mud          |  ----------
| Driver |  --------------  :  |                |  | Driver |
----------  | regmap-usb |  :  | (mud_regmap)   |  ----------
            --------------  :  ------------------
Quoted 4 lines

The idea is really like ARA's greybus, but much simpler ! Anyway nice idea, do you have good performance over USB2 and RPi's awful DWC2 gagdet controller ?

Not as good as I was hoping for. If I disable compression I'm getting 5 fps for a 1.5MB framebuffer (7800 kB/s):

$ modetest -M mud_drm -s 35:1024x768@RG16 -v
setting mode 1024x768-60.00Hz@RG16 on connectors 35, crtc 33
freq: 5.07Hz

When I tried reading I discovered that it was almost 3 times faster than writing.

The zero gadget (loop testing) confirmed my findings:

Device:

$ sudo modprobe g_zero
[   44.221890] zero gadget: Gadget Zero, version: Cinco de Mayo 2008
[   44.221906] zero gadget: zero ready
[   60.751451] zero gadget: high-speed config #3: source/sink

Host:

$ sudo ~/testusb -a -t <n> -g 64 -s 16384
/dev/bus/usb/001/010 test 27,  107.230669 secs	-> 1000 / 107 =  9MB/s
/dev/bus/usb/001/010 test 28,   37.791292 secs	-> 1000 / 37  = 27MB/s
[73983.796552] usbtest 1-1.3:3.0: TEST 27: bulk write 1000Mbytes
[74205.060204] usbtest 1-1.3:3.0: TEST 28: bulk read 1000Mbytes
$ sudo ~/testusb -a -t <n> -g 64 -s 16384
/dev/bus/usb/001/010 test 5,  107.421535 secs
/dev/bus/usb/001/010 test 6,   38.189712 secs
[74893.204170] usbtest 1-1.3:3.0: TEST 5:  write 1000 sglists 64 entries
of 16384 bytes
[75012.592222] usbtest 1-1.3:3.0: TEST 6:  read 1000 sglists 64 entries
of 16384 bytes

I have tried Raspberry Pi1 and Pi4 as host (2 different controllers) and Pi Zero and Beaglebone Black as device, getting similar result.

I found this post having the same issue:

Re: Asymmetric speed results with testusb/usbtest/g_zero
https://www.spinics.net/lists/linux-usb/msg100588.html

I haven't got a usb analyzer, but adding printk to dwc2_assign_and_init_hc() showed that IN interrupts were 2-3 ms apart but OUT interrupts were ~8 ms apart.

Noralf.

#25 lore raw
In reply to Noralf Trønnes
Quoted 28 lines · Noralf Trønnes

Hi,

A while back I had the idea to turn a Raspberry Pi Zero into a $5 USB to HDMI/SDTV/DSI/DPI display adapter.

Thinking about how to represent the display to the driver I realised that hardware use registers as API. And Linux does have a generic register abstraction: regmap. Furthermore this means that if I can do a regmap over USB implementation, it will be easy to do other functions like gpio, adc and others. After a few iterations trying to understand the USB subsystem and satisfying driver requirements, I now have something that looks promising.

I'm sending out an early version hoping to get feedback especially on the core parts that handles regmap and interrupts.

Overview:

          USB Host          :         USB Device
                            :
            --------------  :  ------------------
----------  | mfd: mud   |  :  | f_mud          |  ----------
| Driver |  --------------  :  |                |  | Driver |
----------  | regmap-usb |  :  | (mud_regmap)   |  ----------
            --------------  :  ------------------
Quoted 3 lines

I've attached 2 drivers:
- gpio/pinctrl: is more or less finished
- display: needs a lot more work

Can regmap-usb be used for drivers/mfd/dln2.c for example?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#26 lore raw

Den 18.02.2020 21.57, skrev Andy Shevchenko:

Quoted 29 lines · Noralf Trønnes
Quoted 28 lines

Hi,

A while back I had the idea to turn a Raspberry Pi Zero into a $5 USB to HDMI/SDTV/DSI/DPI display adapter.

Thinking about how to represent the display to the driver I realised that hardware use registers as API. And Linux does have a generic register abstraction: regmap. Furthermore this means that if I can do a regmap over USB implementation, it will be easy to do other functions like gpio, adc and others. After a few iterations trying to understand the USB subsystem and satisfying driver requirements, I now have something that looks promising.

I'm sending out an early version hoping to get feedback especially on the core parts that handles regmap and interrupts.

Overview:

          USB Host          :         USB Device
                            :
            --------------  :  ------------------
----------  | mfd: mud   |  :  | f_mud          |  ----------
| Driver |  --------------  :  |                |  | Driver |
----------  | regmap-usb |  :  | (mud_regmap)   |  ----------
            --------------  :  ------------------
Quoted 6 lines
Quoted 3 lines

I've attached 2 drivers:
- gpio/pinctrl: is more or less finished
- display: needs a lot more work

Can regmap-usb be used for drivers/mfd/dln2.c for example?

No, apparently dln-2 uses custom protocol structs for each function with a common header. regmap-usb is a register abstraction with the ability to bulk read/write multiple registers in one transfer.

I see that dln-2 does a lot of what I'm aiming for except that it doesn't have the display part.

Noralf.