Wiegand bus driver and GPIO bitbanged controller
v3 cover Wiegand bus driver and GPIO bitbanged controller
Hello,
thank you for the feedback regarding last patch. I have decided against using a copyright line despite the suggestion.
CHANGELOG since PATCHv2:
- dropped the Wiegand controller example from the dt-bindings doc, as the real one is in the bitbanged controller schema
- fixed some indentation issues
- removed controller class
- functions with "__" as prefix are no longer exported
- improved comment style
- fixed the line length of the code to 100 columns
- removed dev_warn calls for uinitialized controller attributes, instead the driver informs about the situation using dev_info and sets the uninitialized attribute to its default value
- removed modalias from wiegand_device structure, as I have realized it is no longer needed
- removed the list from wiegand_controller structure
- used the tool "pahole" to optimally reorganize all the structures
- ordered headers alphabetically
- removed unnecessary casts
- used attribute group instead of creating the files automatically
Martin Zaťovič (4):
dt-bindings: wiegand: add Wiegand controller common properties
wiegand: add Wiegand bus driver
dt-bindings: wiegand: add GPIO bitbanged Wiegand controller
wiegand: add Wiegand GPIO bitbanged controller driver
.../ABI/testing/sysfs-driver-wiegand-gpio | 9 + .../bindings/wiegand/wiegand-controller.yaml | 39 ++ .../bindings/wiegand/wiegand-gpio.yaml | 51 ++ MAINTAINERS | 14 + drivers/Kconfig | 2 + drivers/Makefile | 1 + drivers/wiegand/Kconfig | 28 + drivers/wiegand/Makefile | 2 + drivers/wiegand/wiegand-gpio.c | 316 +++++++++++ drivers/wiegand/wiegand.c | 500 ++++++++++++++++++ include/linux/wiegand.h | 155 ++++++ 11 files changed, 1117 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-driver-wiegand-gpio create mode 100644 Documentation/devicetree/bindings/wiegand/wiegand-controller.yaml create mode 100644 Documentation/devicetree/bindings/wiegand/wiegand-gpio.yaml create mode 100644 drivers/wiegand/Kconfig create mode 100644 drivers/wiegand/Makefile create mode 100644 drivers/wiegand/wiegand-gpio.c create mode 100644 drivers/wiegand/wiegand.c create mode 100644 include/linux/wiegand.h
Signature · 1 line
2.39.2
PATCH v3 1/4 dt-bindings: wiegand: add Wiegand controller common properties
Wiegand bus is defined by a Wiegand controller node. This node can contain one or more device nodes for devices attached to the controller(it is advised to only connect one device as Wiegand is a point-to-point bus).
Wiegand controller needs to specify several attributes such as the pulse length in order to function properly. These attributes are documented here.
--- .../bindings/wiegand/wiegand-controller.yaml | 39 +++++++++++++++++++ MAINTAINERS | 5 +++ 2 files changed, 44 insertions(+) create mode 100644 Documentation/devicetree/bindings/wiegand/wiegand-controller.yaml
Documentation/devicetree/bindings/wiegand/wiegand-controller.yaml new file +39 -0
@@ -0,0 +1,39 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/wiegand/wiegand-controller.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Wiegand Generic Controller Common Properties + +maintainers: + - Martin Zaťovič <[email protected]> + +description: + Wiegand busses can be described with a node for the Wiegand controller device + and a set of child nodes for each SPI slave on the bus. + +properties: + $nodename: + pattern: "^wiegand(@.*|-[0-9a-f])?$" + + pulse-len-us: + description: | + Length of the low pulse in microseconds. + + interval-len-us: + description: | + Length of a whole bit (both the pulse and the high phase) in microseconds. + + frame-gap-us: + description: | + Length of the last bit of a frame (both the pulse and the high phase) in + microseconds. + +required: + - compatible + - pulse-len-us + - interval-len-us + - frame-gap-us + +additionalProperties: true
MAINTAINERS +5 -0
@@ -22512,6 +22512,11 @@ L: [email protected] S: Maintained F: drivers/rtc/rtc-sd3078.c +WIEGAND BUS DRIVER +M: Martin Zaťovič <[email protected]> +S: Maintained +F: Documentation/devicetree/bindings/wiegand/wiegand-controller.yaml + WIIMOTE HID DRIVER M: David Rheinsberg <[email protected]> L: [email protected]
Signature · 1 line
2.39.2
Re: [PATCHv3 1/4] dt-bindings: wiegand: add Wiegand controller common properties
Quoted 34 lines · Martin Zaťovič
Wiegand bus is defined by a Wiegand controller node. This node can contain one or more device nodes for devices attached to the controller(it is advised to only connect one device as Wiegand is a point-to-point bus).
Wiegand controller needs to specify several attributes such as the pulse length in order to function properly. These attributes are documented here.
Acked-by Linus Walleij <[email protected]>Signed-off-by Martin Zaťovič <[email protected]>--- .../bindings/wiegand/wiegand-controller.yaml | 39 +++++++++++++++++++ MAINTAINERS | 5 +++ 2 files changed, 44 insertions(+) create mode 100644 Documentation/devicetree/bindings/wiegand/wiegand-controller.yamlDocumentation/devicetree/bindings/wiegand/wiegand-controller.yaml new file +11
-0@@ -0,0 +1,39 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/wiegand/wiegand-controller.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Wiegand Generic Controller Common Properties + +maintainers: + - Martin Zaťovič <[email protected]> +
Documentation/devicetree/bindings/wiegand/wiegand-controller.yaml line 21
Quoted 10 lines
Documentation/devicetree/bindings/wiegand/wiegand-controller.yaml new file +10
-0+description: + Wiegand busses can be described with a node for the Wiegand controller device + and a set of child nodes for each SPI slave on the bus. + +properties: + $nodename: + pattern: "^wiegand(@.*|-[0-9a-f])?$" + + pulse-len-us: + description: |
Don't need '|' here and elsewhere.
With that fixed,
Quoted 37 lines
Documentation/devicetree/bindings/wiegand/wiegand-controller.yaml new file +18
-0+ Length of the low pulse in microseconds. + + interval-len-us: + description: | + Length of a whole bit (both the pulse and the high phase) in microseconds. + + frame-gap-us: + description: | + Length of the last bit of a frame (both the pulse and the high phase) in + microseconds. + +required: + - compatible + - pulse-len-us + - interval-len-us + - frame-gap-us + +additionalProperties: trueMAINTAINERS +5
-0@@ -22512,6 +22512,11 @@ L: [email protected] S: Maintained F: drivers/rtc/rtc-sd3078.c +WIEGAND BUS DRIVER +M: Martin Zaťovič <[email protected]> +S: Maintained +F: Documentation/devicetree/bindings/wiegand/wiegand-controller.yaml + WIIMOTE HID DRIVER M: David Rheinsberg <[email protected]> L: [email protected]Signature · 1 line
2.39.2
PATCH v3 2/4 wiegand: add Wiegand bus driver
In reply to Martin ZaťovičAdd a bus driver for Wiegand protocol. The bus driver handles Wiegand controller and Wiegand device managemement and driver matching. The bus driver defines the structures for Wiegand controllers and Wiegand devices.
Wiegand controller structure represents a master and contains attributes such as the payload_len for configuring the size of a single Wiegand message in bits. It also stores the controller attributes defined in the devicetree.
Each Wiegand controller should be associated with one Wiegand device, as Wiegand is typically a point-to-point bus.
--- MAINTAINERS | 2 + drivers/Kconfig | 2 + drivers/Makefile | 1 + drivers/wiegand/Kconfig | 20 ++ drivers/wiegand/Makefile | 1 + drivers/wiegand/wiegand.c | 500 ++++++++++++++++++++++++++++++++++++++ include/linux/wiegand.h | 155 ++++++++++++ 7 files changed, 681 insertions(+) create mode 100644 drivers/wiegand/Kconfig create mode 100644 drivers/wiegand/Makefile create mode 100644 drivers/wiegand/wiegand.c create mode 100644 include/linux/wiegand.h
MAINTAINERS +2 -0
@@ -22516,6 +22516,8 @@ WIEGAND BUS DRIVER M: Martin Zaťovič <[email protected]> S: Maintained F: Documentation/devicetree/bindings/wiegand/wiegand-controller.yaml +F: drivers/wiegand/wiegand.c +F: include/linux/wiegand.h WIIMOTE HID DRIVER M: David Rheinsberg <[email protected]>
drivers/Kconfig +2 -0
@@ -67,6 +67,8 @@ source "drivers/spi/Kconfig" source "drivers/spmi/Kconfig" +source "drivers/wiegand/Kconfig" + source "drivers/hsi/Kconfig" source "drivers/pps/Kconfig"
drivers/Makefile +1 -0
@@ -150,6 +150,7 @@ obj-$(CONFIG_VHOST_RING) += vhost/ obj-$(CONFIG_VHOST_IOTLB) += vhost/ obj-$(CONFIG_VHOST) += vhost/ obj-$(CONFIG_VLYNQ) += vlynq/ +obj-$(CONFIG_WIEGAND) += wiegand/ obj-$(CONFIG_GREYBUS) += greybus/ obj-$(CONFIG_COMEDI) += comedi/ obj-$(CONFIG_STAGING) += staging/
drivers/wiegand/Kconfig new file +20 -0
@@ -0,0 +1,20 @@ +config WIEGAND + tristate "Wiegand Bus driver" + help + The "Wiegand Interface" is an asynchronous low-level protocol + or wiring standard. It is typically used for point-to-point + communication. The data length of Wiegand messages is not defined, + so the devices usually default to 26, 36 or 37 bits per message. + The throughput of Wiegand depends on the selected pulse length and + the intervals between pulses, in comparison to other busses it + is generally rather slow. + + Despite its higher age, Wiegand remains widely used in access + control systems to connect a card swipe mechanism. Such mechanisms + utilize the Wiegand effect to transfer data from the card to + the reader. + + Wiegand uses two wires to transmit the data D0 and D1. Both lines + are initially pulled up. When a bit of value 0 is being transmitted, + the D0 line is pulled down. Similarly, when a bit of value 1 is being + transmitted, the D1 line is pulled down.
drivers/wiegand/Makefile new file +1 -0
@@ -0,0 +1 @@ +obj-$(CONFIG_WIEGAND) += wiegand.o
drivers/wiegand/wiegand.c new file +500 -0
500 lines not on this page · raw message
include/linux/wiegand.h new file +155 -0
155 lines not on this page · raw message
Signature · 1 line
2.39.2
Re: [PATCHv3 2/4] wiegand: add Wiegand bus driver
Quoted 12 lines · Martin Zaťovič
Add a bus driver for Wiegand protocol. The bus driver handles Wiegand controller and Wiegand device managemement and driver matching. The bus driver defines the structures for Wiegand controllers and Wiegand devices.
Wiegand controller structure represents a master and contains attributes such as the
payload_lenfor configuring the size of a single Wiegand message in bits. It also stores the controller attributes defined in the devicetree.Each Wiegand controller should be associated with one Wiegand device, as Wiegand is typically a point-to-point bus.
...
drivers/wiegand/wiegand.c line 14
Quoted 13 lines
drivers/wiegand/wiegand.c new file +12
-0+#include <linux/device.h> +#include <linux/dmaengine.h> +#include <linux/dma-mapping.h> +#include <linux/module.h> +#include <linux/of.h> +#include <linux/of_device.h> +#include <linux/property.h> +#include <linux/slab.h> +#include <linux/wiegand.h> + +static struct bus_type wiegand_bus_type; +static DEFINE_IDR (wiegand_controller_idr);
Why not IDA or even xarray?
...
Quoted 1 line
+static
DEFINE_MUTEX(board_lock);
Or locks need a good description for what they are.
...
drivers/wiegand/wiegand.c line 20
Quoted 4 lines
drivers/wiegand/wiegand.c new file +4
-0+static void devm_wiegand_release_controller (struct device *dev,void *ctlr) +{ +wiegand_controller_put (*(struct wiegand_controller **)ctlr); +}
This is not used in the following function, so can be moved closer to its user.
...
Quoted 9 lines
drivers/wiegand/wiegand.c new file +9
-0+struct wiegand_controller *devm_wiegand_alloc_controller (struct device *dev,unsigned int size, +bool slave) +{ +struct wiegand_controller **ptr, *ctlr; + + ptr =devres_alloc (devm_wiegand_release_controller,sizeof (*ptr),GFP_KERNEL ); +if (!ptr) +return NULL ; +
drivers/wiegand/wiegand.c line 63
Quoted 10 lines
drivers/wiegand/wiegand.c new file +10
-0+ ctlr =wiegand_alloc_controller (dev, size, slave); +if (ctlr) { + ctlr->devm_allocated = true; + *ptr = ctlr; +devres_add (dev, ptr); + }else { +devres_free (ptr); + } + +return ctlr;
Can this utilize devm_add_action_or_reset()?
Quoted 1 line
+}
...
Quoted 2 lines
+/**
+ *of_register_wiegand_device- allocates and registers a new Wiegand device based on devicetree
NAK for OF only code. New, esp. bus, code must be agnostic. We have all means for that.
register_wiegand_device() or similar name.
Quoted 5 lines
+ * node
+ * @ctlr: controller structure to attach device to
+ * @nc: devicetree node for the device
+ */
+static structwiegand_device*of_register_wiegand_device(structwiegand_controller*ctlr,
Ditto.
drivers/wiegand/wiegand.c line 86
Quoted 1 line
drivers/wiegand/wiegand.c new file +1
-0+ struct device_node *nc)
struct fwnode_handle *fwnode
Quoted 4 lines
drivers/wiegand/wiegand.c new file +4
-0+{ +struct wiegand_device *wiegand; +int rc; +
drivers/wiegand/wiegand.c line 100
Quoted 10 lines
drivers/wiegand/wiegand.c new file +9
-0+ wiegand =wiegand_alloc_device (ctlr); +if (!wiegand) { +dev_err (&ctlr->dev,"wiegad_device alloc error for %pOF\n" , nc); + rc = -ENOMEM ; + goto err_out; + } +of_node_get (nc); + wiegand->dev.of_node = nc; + wiegand->dev.fwnode =of_fwnode_handle (nc);
device_set_node(&wiegand->dev, fwnode_handle_get(fwnode));
Quoted 31 lines
drivers/wiegand/wiegand.c new file +31
-0+ rc =wiegand_add_device (wiegand); +if (rc) { +dev_err (&ctlr->dev,"wiegand_device register error %pOF\n" , nc); + goto err_of_node_put; + } + +/* check if more devices are connected to the bus */ +if (ctlr->device_count >1 ) +dev_warn (&ctlr->dev,"Wiegand is a point-to-point bus, it is advised to only connect one device per Wiegand bus. The devices may not communicate using the same pulse length, format or else.\n" ); + +return wiegand; + +err_of_node_put: +of_node_put (nc); +err_out: +wiegand_dev_put (wiegand); +return ERR_PTR (rc); +} + +/** +* of_register_wiegand_devices - creates a wiegand device for all children of a controller +* devicetree node +* @ctlr: controller structure to check +*/ +static void of_register_wiegand_devices (struct wiegand_controller *ctlr) +{ +struct wiegand_device *wiegand; +struct device_node *nc; + +if (!ctlr->dev.of_node) +return ;
drivers/wiegand/wiegand.c line 142
Quoted 10 lines
drivers/wiegand/wiegand.c new file +10
-0+ +for_each_available_child_of_node (ctlr->dev.of_node, nc) { +if (of_node_test_and_set_flag (nc,OF_POPULATED )) +continue ; + wiegand =of_register_wiegand_device (ctlr, nc); +if (IS_ERR (wiegand)) { +dev_warn (&ctlr->dev,"Failed to create wiegand device for %pOF\n" , nc); +of_node_clear_flag (nc,OF_POPULATED ); + } + }
No way. Use agnostic approach. See above for some suggestions.
Quoted 1 line
+}
...
drivers/wiegand/wiegand.c line 159
Quoted 2 lines
drivers/wiegand/wiegand.c new file +2
-0+if (!dev) +return -ENODEV ;
When is it true and why is it a problem?
drivers/wiegand/wiegand.c line 166
Quoted 2 lines
drivers/wiegand/wiegand.c new file +2
-0+if (ctlr->dev.of_node) { + id =of_alias_get_id (ctlr->dev.of_node,"wiegand" );
Why? What does this bring to us and why it's so important?
drivers/wiegand/wiegand.c line 174
Quoted 8 lines
drivers/wiegand/wiegand.c new file +8
-0+if (id >0 ) { + ctlr->bus_num = id; +mutex_lock (&board_lock); + id =idr_alloc (&wiegand_controller_idr, ctlr, ctlr->bus_num, + ctlr->bus_num +1 ,GFP_KERNEL ); +mutex_unlock (&board_lock); +if (WARN (id <0 ,"couldn't get idr" )) +return id == -ENOSPC ? -EBUSY : id;
Why rewriting error code?
Quoted 23 lines
drivers/wiegand/wiegand.c new file +23
-0+ } +device_property_read_u32 (&ctlr->dev,"pulse-len-us" , &ctlr->pulse_len); +device_property_read_u32 (&ctlr->dev,"interval-len-us" , &ctlr->interval_len); +device_property_read_u32 (&ctlr->dev,"frame-gap-us" , &ctlr->frame_gap); + } +if (ctlr->bus_num <0 ) { + first_dynamic =of_alias_get_highest_id ("wiegand" ); +if (first_dynamic <0 ) + first_dynamic =0 ; +else + first_dynamic++; + +mutex_lock (&board_lock); + id =idr_alloc (&wiegand_controller_idr, ctlr, first_dynamic, +0 ,GFP_KERNEL ); +mutex_unlock (&board_lock); +if (WARN (id <0 ,"couldn't get idr\n" )) +return id; + ctlr->bus_num = id; + } + +if (ctlr->pulse_len ==0 ) { +dev_warn (&ctlr->dev,"pulse_len is not initialized, setting the default value 50us\n" );
drivers/wiegand/wiegand.c line 207
Quoted 10 lines
drivers/wiegand/wiegand.c new file +10
-0+ ctlr->pulse_len =50 ; + } +if (ctlr->interval_len ==0 ) { +dev_warn (&ctlr->dev,"interval_len is not initialized, setting the default value 2000us\n" ); + ctlr->interval_len =2000 ; + } +if (ctlr->frame_gap ==0 ) { +dev_warn (&ctlr->dev,"frame_gap is not initialized, setting the default value 2000us\n" ); + ctlr->frame_gap =2000 ; + }
Why warnings? Can't it survive without them? (See, for example, how I²C controllers get timings).
Quoted 17 lines
drivers/wiegand/wiegand.c new file +17
-0+dev_set_name (&ctlr->dev,"wiegand%u" , ctlr->bus_num); + ctlr->device_count =0 ; + + status =device_add (&ctlr->dev); +if (status <0 ) + goto free_bus_id; + +of_register_wiegand_devices (ctlr); + +return status; + +free_bus_id: +mutex_lock (&board_lock); +idr_remove (&wiegand_controller_idr, ctlr->bus_num); +mutex_unlock (&board_lock); +return status; +}
...
Quoted 6 lines
drivers/wiegand/wiegand.c new file +6
-0+int devm_wiegand_register_controller (struct device *dev,struct wiegand_controller *ctlr) +{ +struct wiegand_controller **ptr; +int ret; + + ptr =devres_alloc (devm_wiegand_unregister,sizeof (*ptr),GFP_KERNEL );
drivers/wiegand/wiegand.c line 272
Quoted 10 lines
drivers/wiegand/wiegand.c new file +10
-0+if (!ptr) +return -ENOMEM ; + + ret =wiegand_register_controller (ctlr); +if (!ret) { + *ptr = ctlr; +devres_add (dev, ptr); + }else { +devres_free (ptr); + }
devm_add_action_or_reset() ?
Quoted 2 lines
(quoted diff) +2
-0+ return ret; +}
...
drivers/wiegand/wiegand.c line 293
Quoted 6 lines
drivers/wiegand/wiegand.c new file +6
-0+struct wiegand_device *wiegand_alloc_device (struct wiegand_controller *ctlr) +{ +struct wiegand_device *wiegand; + +if (!wiegand_controller_get (ctlr)) +return NULL ;
Is it important to be called before pure memory allocation? The reference counting on the existing resource is less failure prone, right?
Quoted 14 lines
drivers/wiegand/wiegand.c new file +14
-0+ wiegand =kzalloc (sizeof (*wiegand),GFP_KERNEL ); +if (!wiegand) { +wiegand_controller_put (ctlr); +return NULL ; + } + + wiegand->controller = ctlr; + wiegand->dev.parent = &ctlr->dev; + wiegand->dev.bus = &wiegand_bus_type; + wiegand->dev.release = wieganddev_release; + +device_initialize (&wiegand->dev); +return wiegand; +}
...
drivers/wiegand/wiegand.c line 343
Quoted 3 lines
drivers/wiegand/wiegand.c new file +3
-0+static void wiegand_dev_set_name (struct wiegand_device *wiegand,u8 id) +{ +dev_set_name (&wiegand->dev,"%s.%u" ,dev_name (&wiegand->controller->dev), id);
Why error is ignored?
Quoted 1 line
+}
...
drivers/wiegand/wiegand.c line 386
Quoted 4 lines
drivers/wiegand/wiegand.c new file +4
-0+if (wiegand->dev.of_node) { +of_node_clear_flag (wiegand->dev.of_node,OF_POPULATED ); +of_node_put (wiegand->dev.of_node); + }
fwnode APIs, please.
...
drivers/wiegand/wiegand.c line 424
Quoted 10 lines
drivers/wiegand/wiegand.c new file +10
-0+static int wiegand_probe (struct device *dev) +{ +struct wiegand_device *wiegand =to_wiegand_device (dev); +const struct wiegand_driver *wdrv =to_wiegand_driver (dev->driver); +int ret =0 ; + +if (wdrv->probe) + ret = wdrv->probe (wiegand); + +return ret;
Hmm... this is just
if (wdrv->probe) return wdrv->probe(wiegand); return 0;
Quoted 1 line
+}
...
Quoted 14 lines
drivers/wiegand/wiegand.c new file +14
-0+if (wdrv->driver.of_match_table) { +const struct of_device_id *of_id; + +for (of_id = wdrv->driver.of_match_table; of_id->compatible[0 ]; + of_id++) { +const char *of_name; + +/* remove vendor prefix */ + of_name =strnchr (of_id->compatible, +sizeof (of_id->compatible),',' ); +if (of_name) + of_name++; +else + of_name = of_id->compatible;
drivers/wiegand/wiegand.c line 472
Quoted 10 lines
drivers/wiegand/wiegand.c new file +10
-0+ +if (wdrv->driver.name) { +if (strcmp (wdrv->driver.name, of_name) ==0 ) +continue ; + } + +pr_warn ("Wiegand driver %s has no device_id for %s\n" , + wdrv->driver.name, of_id->compatible); + } + }
This looks like very much of a repetition of the existing code somewhere.
...
include/linux/wiegand.h line 26
Quoted 13 lines
include/linux/wiegand.h new file +13
-0+/** +* struct wiegand_device - Wiegand listener device +* @dev - drivers structure of the device +* @id - unique device id +* @controller - Wiegand controller associated with the device +* @modalias - Name of the driver to use with this device, or its alias. +*/ +struct wiegand_device { +struct device dev; +u8 id; +struct wiegand_controller *controller; +char modalias[WIEGAND_NAME_SIZE ]; +};
Wondering if this can be made an opaque pointer instead.
...
Quoted 8 lines
+/**
+ * structwiegand_controller- Wiegand master or slave interface
+ * @dev - Device interface of the controller
+ * @list - Link with the globalwiegand_controllerlist
+ * @bus_num - Board-specific identifier for Wiegand controller
+ * @pulse_len: length of the low pulse in usec; defaults to 50us
+ * @interval_len: length of a whole bit (both the pulse and the high phase) in usec;
+ * defaults to 2000us
Quoted 8 lines
+ * @frame_gap: length of the last bit of a frame (both the pulse and the high phase) in usec;
+ * defaults tointerval_len
+ *device_count- Counter of devices connected to the same Wiegand bus(controller).
+ *devm_allocated- Whether the allocation of this struct is devres-managed
+ * slave - Whether the controller is a slave(receives data).
+ *transfer_message- Send a message on the bus.
+ * setup - Setup a device.
+ * cleanup - Cleanup after a device.
At some point you lost the grip on @ key button.
Quoted 1 line
+ */
...
include/linux/wiegand.h line 68
Quoted 6 lines
include/linux/wiegand.h new file +5
-0+struct wiegand_driver { +const struct wiegand_device_id *id_table; +int (*probe)(struct wiegand_device *wiegand); +void (*remove)(struct wiegand_device *wiegand); +struct device_driver driver;
Making it first may save a few assembly instructions in pointer arithmetics. Check if I'm right with bloat-o-meter (you will need a user of this data type, of course).
Quoted 1 line
+};
...
include/linux/wiegand.h line 147
Quoted 1 line
include/linux/wiegand.h new file +1
-0+driver_unregister (&wdrv->driver);
Yep, here and...
include/linux/wiegand.h line 152
Quoted 1 line
include/linux/wiegand.h new file +1
-0+ return drv ? container_of(drv, struct wiegand_driver, driver) : NULL;
...here you will save code bytes.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCHv3 2/4] wiegand: add Wiegand bus driver
Quoted 1 line · Andy Shevchenko
On Wed, Mar 01, 2023 at 03:28:33PM +0100, Martin Zaťovič wrote:
...
Quoted 2 lines
drivers/wiegand/wiegand.c line 8
Quoted 2 lines
drivers/wiegand/wiegand.c new file +2
-0+#include <linux/of.h> +#include <linux/of_device.h>
Forgot to add that these are probably optional. Or at least OF specific code should leave in a separate file with a good explanation why we even have it to begin with.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCHv3 2/4] wiegand: add Wiegand bus driver
In reply to Andy ShevchenkoQuoted 73 lines · Andy Shevchenko
Quoted 12 lines · Martin Zaťovič
Add a bus driver for Wiegand protocol. The bus driver handles Wiegand controller and Wiegand device managemement and driver matching. The bus driver defines the structures for Wiegand controllers and Wiegand devices.
Wiegand controller structure represents a master and contains attributes such as the
payload_lenfor configuring the size of a single Wiegand message in bits. It also stores the controller attributes defined in the devicetree.Each Wiegand controller should be associated with one Wiegand device, as Wiegand is typically a point-to-point bus.
...
drivers/wiegand/wiegand.c line 14
Quoted 13 lines
drivers/wiegand/wiegand.c new file +12
-0+#include <linux/device.h> +#include <linux/dmaengine.h> +#include <linux/dma-mapping.h> +#include <linux/module.h> +#include <linux/of.h> +#include <linux/of_device.h> +#include <linux/property.h> +#include <linux/slab.h> +#include <linux/wiegand.h> + +static struct bus_type wiegand_bus_type; +static DEFINE_IDR (wiegand_controller_idr);Why not IDA or even xarray?
...
Quoted 1 line
+static
DEFINE_MUTEX(board_lock);Or locks need a good description for what they are.
...
drivers/wiegand/wiegand.c line 20
Quoted 4 lines
drivers/wiegand/wiegand.c new file +4
-0+static void devm_wiegand_release_controller (struct device *dev,void *ctlr) +{ +wiegand_controller_put (*(struct wiegand_controller **)ctlr); +}This is not used in the following function, so can be moved closer to its user.
...
Quoted 9 lines
drivers/wiegand/wiegand.c new file +9
-0+struct wiegand_controller *devm_wiegand_alloc_controller (struct device *dev,unsigned int size, +bool slave) +{ +struct wiegand_controller **ptr, *ctlr; + + ptr =devres_alloc (devm_wiegand_release_controller,sizeof (*ptr),GFP_KERNEL ); +if (!ptr) +return NULL ; +drivers/wiegand/wiegand.c line 63
Quoted 10 lines
drivers/wiegand/wiegand.c new file +10
-0+ ctlr =wiegand_alloc_controller (dev, size, slave); +if (ctlr) { + ctlr->devm_allocated = true; + *ptr = ctlr; +devres_add (dev, ptr); + }else { +devres_free (ptr); + } + +return ctlr;Can this utilize
devm_add_action_or_reset()?Quoted 1 line
+}
Quoted 7 lines
...
Quoted 2 lines
+/**
+ *of_register_wiegand_device- allocates and registers a new Wiegand device based on devicetreeNAK for OF only code. New, esp. bus, code must be agnostic. We have all means for that.
In one of the previous versions of this patch series, there was also the possibility to instantiate the device from another driver. I have been told, that this is not the way to go anymore, unless there is a very specific reason for that. I did not find such reason, so I have removed this suport.
The only other device instantiating method I could think of is ACPI, however I believe it would be just dead code no one would use, as the Wiegand interface is not normally used on non-embedded devices. This is the point Evgeny Boger argued for as well. Is there any other device instantiating method I am not aware of, that would be suitable for this bus?
Quoted 347 lines
register_wiegand_device()or similar name.Quoted 5 lines
+ * node
+ * @ctlr: controller structure to attach device to
+ * @nc: devicetree node for the device
+ */
+static structwiegand_device*of_register_wiegand_device(structwiegand_controller*ctlr,Ditto.
drivers/wiegand/wiegand.c line 86
Quoted 1 line
drivers/wiegand/wiegand.c new file +1
-0+ struct device_node *nc)struct
fwnode_handle*fwnodeQuoted 4 lines
drivers/wiegand/wiegand.c new file +4
-0+{ +struct wiegand_device *wiegand; +int rc; +drivers/wiegand/wiegand.c line 100
Quoted 10 lines
drivers/wiegand/wiegand.c new file +9
-0+ wiegand =wiegand_alloc_device (ctlr); +if (!wiegand) { +dev_err (&ctlr->dev,"wiegad_device alloc error for %pOF\n" , nc); + rc = -ENOMEM ; + goto err_out; + } +of_node_get (nc); + wiegand->dev.of_node = nc; + wiegand->dev.fwnode =of_fwnode_handle (nc);device_set_node(&wiegand->dev, fwnode_handle_get(fwnode));Quoted 31 lines
drivers/wiegand/wiegand.c new file +31
-0+ rc =wiegand_add_device (wiegand); +if (rc) { +dev_err (&ctlr->dev,"wiegand_device register error %pOF\n" , nc); + goto err_of_node_put; + } + +/* check if more devices are connected to the bus */ +if (ctlr->device_count >1 ) +dev_warn (&ctlr->dev,"Wiegand is a point-to-point bus, it is advised to only connect one device per Wiegand bus. The devices may not communicate using the same pulse length, format or else.\n" ); + +return wiegand; + +err_of_node_put: +of_node_put (nc); +err_out: +wiegand_dev_put (wiegand); +return ERR_PTR (rc); +} + +/** +* of_register_wiegand_devices - creates a wiegand device for all children of a controller +* devicetree node +* @ctlr: controller structure to check +*/ +static void of_register_wiegand_devices (struct wiegand_controller *ctlr) +{ +struct wiegand_device *wiegand; +struct device_node *nc; + +if (!ctlr->dev.of_node) +return ;drivers/wiegand/wiegand.c line 142
Quoted 10 lines
drivers/wiegand/wiegand.c new file +10
-0+ +for_each_available_child_of_node (ctlr->dev.of_node, nc) { +if (of_node_test_and_set_flag (nc,OF_POPULATED )) +continue ; + wiegand =of_register_wiegand_device (ctlr, nc); +if (IS_ERR (wiegand)) { +dev_warn (&ctlr->dev,"Failed to create wiegand device for %pOF\n" , nc); +of_node_clear_flag (nc,OF_POPULATED ); + } + }No way. Use agnostic approach. See above for some suggestions.
Quoted 1 line
+}
...
drivers/wiegand/wiegand.c line 159
Quoted 2 lines
drivers/wiegand/wiegand.c new file +2
-0+if (!dev) +return -ENODEV ;When is it true and why is it a problem?
drivers/wiegand/wiegand.c line 166
Quoted 2 lines
drivers/wiegand/wiegand.c new file +2
-0+if (ctlr->dev.of_node) { + id =of_alias_get_id (ctlr->dev.of_node,"wiegand" );Why? What does this bring to us and why it's so important?
drivers/wiegand/wiegand.c line 174
Quoted 8 lines
drivers/wiegand/wiegand.c new file +8
-0+if (id >0 ) { + ctlr->bus_num = id; +mutex_lock (&board_lock); + id =idr_alloc (&wiegand_controller_idr, ctlr, ctlr->bus_num, + ctlr->bus_num +1 ,GFP_KERNEL ); +mutex_unlock (&board_lock); +if (WARN (id <0 ,"couldn't get idr" )) +return id == -ENOSPC ? -EBUSY : id;Why rewriting error code?
Quoted 23 lines
drivers/wiegand/wiegand.c new file +23
-0+ } +device_property_read_u32 (&ctlr->dev,"pulse-len-us" , &ctlr->pulse_len); +device_property_read_u32 (&ctlr->dev,"interval-len-us" , &ctlr->interval_len); +device_property_read_u32 (&ctlr->dev,"frame-gap-us" , &ctlr->frame_gap); + } +if (ctlr->bus_num <0 ) { + first_dynamic =of_alias_get_highest_id ("wiegand" ); +if (first_dynamic <0 ) + first_dynamic =0 ; +else + first_dynamic++; + +mutex_lock (&board_lock); + id =idr_alloc (&wiegand_controller_idr, ctlr, first_dynamic, +0 ,GFP_KERNEL ); +mutex_unlock (&board_lock); +if (WARN (id <0 ,"couldn't get idr\n" )) +return id; + ctlr->bus_num = id; + } + +if (ctlr->pulse_len ==0 ) { +dev_warn (&ctlr->dev,"pulse_len is not initialized, setting the default value 50us\n" );drivers/wiegand/wiegand.c line 207
Quoted 10 lines
drivers/wiegand/wiegand.c new file +10
-0+ ctlr->pulse_len =50 ; + } +if (ctlr->interval_len ==0 ) { +dev_warn (&ctlr->dev,"interval_len is not initialized, setting the default value 2000us\n" ); + ctlr->interval_len =2000 ; + } +if (ctlr->frame_gap ==0 ) { +dev_warn (&ctlr->dev,"frame_gap is not initialized, setting the default value 2000us\n" ); + ctlr->frame_gap =2000 ; + }Why warnings? Can't it survive without them? (See, for example, how I²C controllers get timings).
Quoted 17 lines
drivers/wiegand/wiegand.c new file +17
-0+dev_set_name (&ctlr->dev,"wiegand%u" , ctlr->bus_num); + ctlr->device_count =0 ; + + status =device_add (&ctlr->dev); +if (status <0 ) + goto free_bus_id; + +of_register_wiegand_devices (ctlr); + +return status; + +free_bus_id: +mutex_lock (&board_lock); +idr_remove (&wiegand_controller_idr, ctlr->bus_num); +mutex_unlock (&board_lock); +return status; +}...
Quoted 6 lines
drivers/wiegand/wiegand.c new file +6
-0+int devm_wiegand_register_controller (struct device *dev,struct wiegand_controller *ctlr) +{ +struct wiegand_controller **ptr; +int ret; + + ptr =devres_alloc (devm_wiegand_unregister,sizeof (*ptr),GFP_KERNEL );drivers/wiegand/wiegand.c line 272
Quoted 10 lines
drivers/wiegand/wiegand.c new file +10
-0+if (!ptr) +return -ENOMEM ; + + ret =wiegand_register_controller (ctlr); +if (!ret) { + *ptr = ctlr; +devres_add (dev, ptr); + }else { +devres_free (ptr); + }
devm_add_action_or_reset()?Quoted 2 lines
(quoted diff) +2
-0+ return ret; +}...
drivers/wiegand/wiegand.c line 293
Quoted 6 lines
drivers/wiegand/wiegand.c new file +6
-0+struct wiegand_device *wiegand_alloc_device (struct wiegand_controller *ctlr) +{ +struct wiegand_device *wiegand; + +if (!wiegand_controller_get (ctlr)) +return NULL ;Is it important to be called before pure memory allocation? The reference counting on the existing resource is less failure prone, right?
Quoted 14 lines
drivers/wiegand/wiegand.c new file +14
-0+ wiegand =kzalloc (sizeof (*wiegand),GFP_KERNEL ); +if (!wiegand) { +wiegand_controller_put (ctlr); +return NULL ; + } + + wiegand->controller = ctlr; + wiegand->dev.parent = &ctlr->dev; + wiegand->dev.bus = &wiegand_bus_type; + wiegand->dev.release = wieganddev_release; + +device_initialize (&wiegand->dev); +return wiegand; +}...
drivers/wiegand/wiegand.c line 343
Quoted 3 lines
drivers/wiegand/wiegand.c new file +3
-0+static void wiegand_dev_set_name (struct wiegand_device *wiegand,u8 id) +{ +dev_set_name (&wiegand->dev,"%s.%u" ,dev_name (&wiegand->controller->dev), id);Why error is ignored?
Quoted 1 line
+}
...
drivers/wiegand/wiegand.c line 386
Quoted 4 lines
drivers/wiegand/wiegand.c new file +4
-0+if (wiegand->dev.of_node) { +of_node_clear_flag (wiegand->dev.of_node,OF_POPULATED ); +of_node_put (wiegand->dev.of_node); + }fwnode APIs, please.
...
drivers/wiegand/wiegand.c line 424
Quoted 10 lines
drivers/wiegand/wiegand.c new file +10
-0+static int wiegand_probe (struct device *dev) +{ +struct wiegand_device *wiegand =to_wiegand_device (dev); +const struct wiegand_driver *wdrv =to_wiegand_driver (dev->driver); +int ret =0 ; + +if (wdrv->probe) + ret = wdrv->probe (wiegand); + +return ret;Hmm... this is just
if (wdrv->probe) return wdrv->probe(wiegand); return 0;Quoted 1 line
+}
...
Quoted 14 lines
drivers/wiegand/wiegand.c new file +14
-0+if (wdrv->driver.of_match_table) { +const struct of_device_id *of_id; + +for (of_id = wdrv->driver.of_match_table; of_id->compatible[0 ]; + of_id++) { +const char *of_name; + +/* remove vendor prefix */ + of_name =strnchr (of_id->compatible, +sizeof (of_id->compatible),',' ); +if (of_name) + of_name++; +else + of_name = of_id->compatible;drivers/wiegand/wiegand.c line 472
Quoted 10 lines
drivers/wiegand/wiegand.c new file +10
-0+ +if (wdrv->driver.name) { +if (strcmp (wdrv->driver.name, of_name) ==0 ) +continue ; + } + +pr_warn ("Wiegand driver %s has no device_id for %s\n" , + wdrv->driver.name, of_id->compatible); + } + }This looks like very much of a repetition of the existing code somewhere.
...
include/linux/wiegand.h line 26
Quoted 13 lines
include/linux/wiegand.h new file +13
-0+/** +* struct wiegand_device - Wiegand listener device +* @dev - drivers structure of the device +* @id - unique device id +* @controller - Wiegand controller associated with the device +* @modalias - Name of the driver to use with this device, or its alias. +*/ +struct wiegand_device { +struct device dev; +u8 id; +struct wiegand_controller *controller; +char modalias[WIEGAND_NAME_SIZE ]; +};Wondering if this can be made an opaque pointer instead.
...
Quoted 8 lines
+/**
+ * structwiegand_controller- Wiegand master or slave interface
+ * @dev - Device interface of the controller
+ * @list - Link with the globalwiegand_controllerlist
+ * @bus_num - Board-specific identifier for Wiegand controller
+ * @pulse_len: length of the low pulse in usec; defaults to 50us
+ * @interval_len: length of a whole bit (both the pulse and the high phase) in usec;
+ * defaults to 2000usQuoted 8 lines
+ * @frame_gap: length of the last bit of a frame (both the pulse and the high phase) in usec;
+ * defaults tointerval_len
+ *device_count- Counter of devices connected to the same Wiegand bus(controller).
+ *devm_allocated- Whether the allocation of this struct is devres-managed
+ * slave - Whether the controller is a slave(receives data).
+ *transfer_message- Send a message on the bus.
+ * setup - Setup a device.
+ * cleanup - Cleanup after a device.At some point you lost the grip on @ key button.
Quoted 1 line
+ */
...
include/linux/wiegand.h line 68
Quoted 6 lines
include/linux/wiegand.h new file +5
-0+struct wiegand_driver { +const struct wiegand_device_id *id_table; +int (*probe)(struct wiegand_device *wiegand); +void (*remove)(struct wiegand_device *wiegand); +struct device_driver driver;Making it first may save a few assembly instructions in pointer arithmetics. Check if I'm right with bloat-o-meter (you will need a user of this data type, of course).
Quoted 1 line
+};
...
include/linux/wiegand.h line 147
Quoted 1 line
include/linux/wiegand.h new file +1
-0+driver_unregister (&wdrv->driver);Yep, here and...
include/linux/wiegand.h line 152
Quoted 1 line
include/linux/wiegand.h new file +1
-0+ return drv ? container_of(drv, struct wiegand_driver, driver) : NULL;...here you will save code bytes.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCHv3 2/4] wiegand: add Wiegand bus driver
Quoted 11 lines · Martin Zaťovič
Quoted 5 lines · Andy Shevchenko
Quoted 2 lines
+/**
+ *of_register_wiegand_device- allocates and registers a new Wiegand device based on devicetreeNAK for OF only code. New, esp. bus, code must be agnostic. We have all means for that.
In one of the previous versions of this patch series, there was also the possibility to instantiate the device from another driver. I have been told, that this is not the way to go anymore, unless there is a very specific reason for that. I did not find such reason, so I have removed this suport.
I don't know for sure but I think Andy simply means that you should take a look in include/linux/property.h and replace every function named of_* with the corresponding fwnode_* or device_* function from that file, and it should all just magically work the same, but abstracted away from device tree. It's not much more than a search/replace and rename operation (unless there is a "hole" in the fwnode implementations... I hope not.)
In the end you can keep just <linux/property.h> and drop <linux/of.h> and <linux/of_device.h> if this works.
Yours,
Linus Walleij
Re: [PATCHv3 2/4] wiegand: add Wiegand bus driver
Quoted 2 lines · Linus Walleij
Quoted 1 line · Martin Zaťovič
On Wed, Mar 01, 2023 at 06:23:54PM +0200, Andy Shevchenko wrote:
...
Quoted 17 lines
Quoted 9 lines
Quoted 5 lines
Quoted 2 lines
+/**
+ *of_register_wiegand_device- allocates and registers a new Wiegand device based on devicetreeNAK for OF only code. New, esp. bus, code must be agnostic. We have all means for that.
In one of the previous versions of this patch series, there was also the possibility to instantiate the device from another driver. I have been told, that this is not the way to go anymore, unless there is a very specific reason for that. I did not find such reason, so I have removed this suport.
I don't know for sure but I think Andy simply means that you should take a look in
include/linux/property.hand replace every function named of_* with the corresponding fwnode_* or device_* function from that file, and it should all just magically work the same, but abstracted away from device tree. It's not much more than a search/replace and rename operation (unless there is a "hole" in the fwnode implementations... I hope not.)
Quoted 2 lines
In the end you can keep just <
linux/property.h> and drop <linux/of.h> and <linux/of_device.h> if this works.
Yes. And the argument for embedded devices it's exactly why the non-OF interface is needed as we want to have a possibility to connect devices on ACPI (or another, if any) based platform without changing the code.
Signature · 2 lines
With Best Regards, Andy Shevchenko
kernel test robot
build botRe: [PATCHv3 2/4] wiegand: add Wiegand bus driver
In reply to Martin ZaťovičI love your patch! Yet something to improve:
Hi Martin,
I love your patch! Yet something to improve:
[auto build test ERROR on robh/for-next]
[also build test ERROR on linus/master v6.2 next-20230301] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Martin-Za-ovi/dt-bindings-wiegand-add-Wiegand-controller-common-properties/20230301-223030 base: https://git.kernel.org/pub/scm/linux/kernel/git/robh/linux.git for-next patch link: https://lore.kernel.org/r/20230301142835.19614-3-m.zatovic1%40gmail.com patch subject: [PATCHv3 2/4] wiegand: add Wiegand bus driver config: sh-allmodconfig (https://download.01.org/0day-ci/archive/20230302/[email protected]/config) compiler: sh4-linux-gcc (GCC) 12.1.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/c62b833f42989e355d82cd20b7803e0228e33792
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Martin-Za-ovi/dt-bindings-wiegand-add-Wiegand-controller-common-properties/20230301-223030 git checkout c62b833f42989e355d82cd20b7803e0228e33792
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=sh olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=sh SHELL=/bin/bash drivers/wiegand/
If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Link: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
>> drivers/wiegand/wiegand.c:441:27: error: initialization of 'void (*)(struct device *)' from incompatible pointer type 'int (*)(struct device *)' [-Werror=incompatible-pointer-types]
441 | .remove = wiegand_remove,
| ^~~~~~~~~~~~~~
drivers/wiegand/wiegand.c:441:27: note: (near initialization for 'wiegand_bus_type.remove')
cc1: some warnings being treated as errors
vim +441 drivers/wiegand/wiegand.c
436
437 static struct bus_type wiegand_bus_type = {
438 .name = "wiegand",
439 .match = wiegand_match_device,
440 .probe = wiegand_probe,
> 441 .remove = wiegand_remove,
442 };
443
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests
A message that is not in the archive (sent off-list or not yet received): CAPGNi97oc+tSrt-NF4KZhcEyUfZTo4PT0ms8zYSFVEyzOBq4ZA@mail.gmail.com
Re: [PATCHv3 2/4] wiegand: add Wiegand bus driver
Quoted 1 line · Martin Zaťovič
Thank you for the code review Andy!
I'm not sure why you mentioned this in the reply to kernel build bot message.
Quoted 9 lines
Regarding the OF only code - I have been told, that implementing my own ID table
so that a device can be added from another driver is no longer the way to go.
The only other option I can think of is ACPI device enumeration, which I did not
implement as I really believe that Wiegand will only be used on embedded devices.
Quoted 5 lines
Despite that I respect the message - the code should be agnostic and count with
every option so I will add ACPI support. Is there any other way of instantiating
devices I am not aware of?
This is also seems to me unrelated to this message. Can you reply to the correct message with enough context, please?
Quoted 73 lines
st 1. 3. 2023 o 23:53 kernel test robot <[email protected]> napísal(a):
Quoted 71 lines
Hi Martin,
I love your patch! Yet something to improve:
[auto build test ERROR on robh/for-next]
[also build test ERROR on linus/master v6.2 next-20230301] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]url:
https://github.com/intel-lab-lkp/linux/commits/Martin-Za-ovi/dt-bindings-wiegand-add-Wiegand-controller-common-properties/20230301-223030
base: https://git.kernel.org/pub/scm/linux/kernel/git/robh/linux.git
for-next
patch link:
https://lore.kernel.org/r/20230301142835.19614-3-m.zatovic1%40gmail.com
patch subject: [PATCHv3 2/4] wiegand: add Wiegand bus driver
config: sh-allmodconfig (
https://download.01.org/0day-ci/archive/20230302/[email protected]/config
)
compiler: sh4-linux-gcc (GCC) 12.1.0
reproduce (this is a W=1 build):
wget
https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross
-O ~/bin/make.cross
chmod +x ~/bin/make.cross
#
https://github.com/intel-lab-lkp/linux/commit/c62b833f42989e355d82cd20b7803e0228e33792
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review
Martin-Za-ovi/dt-bindings-wiegand-add-Wiegand-controller-common-properties/20230301-223030
git checkoutc62b833f42989e355d82cd20b7803e0228e33792
# save the config file
mkdirbuild_dir&& cp config build_dir/.config COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross
W=1 O=build_dir ARCH=sh olddefconfig
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross
W=1 O=build_dir ARCH=sh SHELL=/bin/bashdrivers/wiegand/If you fix the issue, kindly add following tag where applicable | Reported-by: kernel test robot <[email protected]>
| Link:
https://lore.kernel.org/oe-kbuild-all/[email protected]/All errors (new ones prefixed by >>):
Quoted 1 line
Quoted 1 line
drivers/wiegand/wiegand.c:441:27: error: initialization of 'void(*)(struct device *)' from incompatible pointer type 'int (*)(struct device *)' [-Werror=incompatible-pointer-types]
441 | .remove =wiegand_remove,
| ^~~~~~~~~~~~~~
drivers/wiegand/wiegand.c:441:27: note: (near initialization for
'wiegand_bus_type.remove')
cc1: some warnings being treated as errorsvim +441
drivers/wiegand/wiegand.c436
437 static structbus_typewiegand_bus_type= {438 .name = "wiegand", 439 .match = wiegand_match_device, 440 .probe = wiegand_probe, > 441 .remove = wiegand_remove, 442 }; 443--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests
Signature · 2 lines
With Best Regards, Andy Shevchenko
PATCH v3 3/4 dt-bindings: wiegand: add GPIO bitbanged Wiegand controller
In reply to Martin ZaťovičGPIO bitbanged Wiegand controller requires definitions of GPIO lines to be used on top of the common Wiegand properties. Wiegand utilizes two such lines - D0(low data line) and D1(high data line).
--- .../bindings/wiegand/wiegand-gpio.yaml | 51 +++++++++++++++++++ MAINTAINERS | 5 ++ 2 files changed, 56 insertions(+) create mode 100644 Documentation/devicetree/bindings/wiegand/wiegand-gpio.yaml
Documentation/devicetree/bindings/wiegand/wiegand-gpio.yaml new file +51 -0
@@ -0,0 +1,51 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/wiegand/wiegand-gpio.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: GPIO bitbanged Wiegand interface devicetree bindings + +maintainers: + - Martin Zaťovič <[email protected]> + +description: + This represents the GPIO lines used for bit-banged Wiegand on dedicated GPIO + lines. + +allOf: + - $ref: /schemas/wiegand/wiegand-controller.yaml# + +properties: + compatible: + const: wiegand-gpio + + data-hi-gpios: + description: GPIO used as Wiegands data-hi line. + maxItems: 1 + + data-lo-gpios: + description: GPIO used as Wiegands data-lo line. + maxItems: 1 + +required: + - compatible + - data-hi-gpios + - data-lo-gpios + +unevaluatedProperties: false + +examples: + - | + #include <dt-bindings/gpio/gpio.h> + + wiegand@f00 { + compatible = "wiegand-gpio"; + pulse-len-us = <50>; + interval-len-us = <2000>; + frame-gap-us = <2000>; + data-lo-gpios = <&gpio2 6 (GPIO_ACTIVE_HIGH|GPIO_OPEN_DRAIN)>; + data-hi-gpios = <&gpio2 7 (GPIO_ACTIVE_HIGH|GPIO_OPEN_DRAIN)>; + + /* devices */ + };
MAINTAINERS +5 -0
@@ -22519,6 +22519,11 @@ F: Documentation/devicetree/bindings/wiegand/wiegand-controller.yaml F: drivers/wiegand/wiegand.c F: include/linux/wiegand.h +WIEGAND GPIO BITBANG DRIVER +M: Martin Zaťovič <[email protected]> +S: Maintained +F: Documentation/devicetree/bindings/wiegand/wiegand-gpio.yaml + WIIMOTE HID DRIVER M: David Rheinsberg <[email protected]> L: [email protected]
Signature · 1 line
2.39.2
Re: [PATCHv3 3/4] dt-bindings: wiegand: add GPIO bitbanged Wiegand controller
Quoted 42 lines · Martin Zaťovič
GPIO bitbanged Wiegand controller requires definitions of GPIO lines to be used on top of the common Wiegand properties. Wiegand utilizes two such lines - D0(low data line) and D1(high data line).
Acked-by Linus Walleij <[email protected]>Signed-off-by Martin Zaťovič <[email protected]>--- .../bindings/wiegand/wiegand-gpio.yaml | 51 +++++++++++++++++++ MAINTAINERS | 5 ++ 2 files changed, 56 insertions(+)create mode 100644
Documentation/devicetree/bindings/wiegand/wiegand-gpio.yamlDocumentation/devicetree/bindings/wiegand/wiegand-gpio.yaml new file +24
-0@@ -0,0 +1,51 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/wiegand/wiegand-gpio.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: GPIO bitbanged Wiegand interface devicetree bindings + +maintainers: + - Martin Zaťovič <[email protected]> + +description: + This represents the GPIO lines used for bit-banged Wiegand on dedicated GPIO + lines. + +allOf: + - $ref: /schemas/wiegand/wiegand-controller.yaml# + +properties: + compatible: + const: wiegand-gpio + + data-hi-gpios: + description: GPIO used as Wiegands data-hi line.
Documentation/devicetree/bindings/wiegand/wiegand-gpio.yaml line 34
Quoted 10 lines
Documentation/devicetree/bindings/wiegand/wiegand-gpio.yaml new file +10
-0+ maxItems: 1 + + data-lo-gpios: + description: GPIO used as Wiegands data-lo line. + maxItems: 1 + +required: + - compatible + - data-hi-gpios + - data-lo-gpios
In my experience, the data lines are usually labeled D0/D1, sometimes DATA0/DATA1. Data high/data low marking is very rare.
Quoted 33 lines
Documentation/devicetree/bindings/wiegand/wiegand-gpio.yaml new file +17
-0+ +unevaluatedProperties: false + +examples: + - | + #include <dt-bindings/gpio/gpio.h> + + wiegand@f00 { + compatible = "wiegand-gpio"; + pulse-len-us = <50>; + interval-len-us = <2000>; + frame-gap-us = <2000>; + data-lo-gpios = <&gpio2 6 (GPIO_ACTIVE_HIGH|GPIO_OPEN_DRAIN)>; + data-hi-gpios = <&gpio2 7 (GPIO_ACTIVE_HIGH|GPIO_OPEN_DRAIN)>; + + /* devices */ + };MAINTAINERS +5
-0@@ -22519,6 +22519,11 @@ F: Documentation/devicetree/bindings/wiegand/wiegand-controller.yaml F: drivers/wiegand/wiegand.c F: include/linux/wiegand.h +WIEGAND GPIO BITBANG DRIVER +M: Martin Zaťovič <[email protected]> +S: Maintained +F: Documentation/devicetree/bindings/wiegand/wiegand-gpio.yaml + WIIMOTE HID DRIVER M: David Rheinsberg <[email protected]> L: [email protected]
Signature · 3 lines
Kind regards, Evgeny Boger CTO @ Wiren Board
PATCH v3 4/4 wiegand: add Wiegand GPIO bitbanged controller driver
In reply to Martin ZaťovičThis controller formats the data to a Wiegand format and bit-bangs the message on devicetree defined GPIO lines.
Several attributes need to be defined in the devicetree in order for this driver to work, namely the data-hi-gpios, data-lo-gpios, pulse-len, frame-gap and interval-len. These attributes are documented in the devicetree bindings documentation files.
The driver creates a dev file for writing messages on the bus. It also creates a sysfs file to control the payload length of messages(in bits). If a message is shorter than the set payload length, it will be discarded. On the other hand, if a message is longer, the additional bits will be stripped off.
--- .../ABI/testing/sysfs-driver-wiegand-gpio | 9 + MAINTAINERS | 2 + drivers/wiegand/Kconfig | 8 + drivers/wiegand/Makefile | 1 + drivers/wiegand/wiegand-gpio.c | 316 ++++++++++++++++++ 5 files changed, 336 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-driver-wiegand-gpio create mode 100644 drivers/wiegand/wiegand-gpio.c
Documentation/ABI/testing/sysfs-driver-wiegand-gpio new file +9 -0
@@ -0,0 +1,9 @@ +What: /sys/devices/platform/.../wiegand-gpio-attributes/payload_len +Date: January 2023 +Contact: Martin Zaťovič <[email protected]> +Description: + Read/set the payload length of messages sent by Wiegand GPIO + bit-banging controller in bits. The default value is 26, as + that is the most widely-used length of Wiegand messages. + Controller will only send messages of at least the set length + and it will strip off bits of longer messages.
MAINTAINERS +2 -0
@@ -22522,7 +22522,9 @@ F: include/linux/wiegand.h WIEGAND GPIO BITBANG DRIVER M: Martin Zaťovič <[email protected]> S: Maintained +F: Documentation/ABI/testing/sysfs-driver-wiegand-gpio F: Documentation/devicetree/bindings/wiegand/wiegand-gpio.yaml +F: drivers/wiegand/wiegand-gpio.c WIIMOTE HID DRIVER M: David Rheinsberg <[email protected]>
drivers/wiegand/Kconfig +8 -0
@@ -18,3 +18,11 @@ config WIEGAND are initially pulled up. When a bit of value 0 is being transmitted, the D0 line is pulled down. Similarly, when a bit of value 1 is being transmitted, the D1 line is pulled down. + +config WIEGAND_GPIO + tristate "GPIO-based wiegand master (write only)" + depends on WIEGAND + help + This GPIO bitbanging Wiegand controller uses the libgpiod library to + utilize GPIO lines for sending Wiegand data. Userspace may access + the Wiegand GPIO interface via a dev entry.
drivers/wiegand/Makefile +1 -0
@@ -1 +1,2 @@ obj-$(CONFIG_WIEGAND) += wiegand.o +obj-$(CONFIG_WIEGAND_GPIO) += wiegand-gpio.o
drivers/wiegand/wiegand-gpio.c new file +316 -0
316 lines not on this page · raw message
Signature · 1 line
2.39.2
Re: [PATCHv3 4/4] wiegand: add Wiegand GPIO bitbanged controller driver
Quoted 13 lines · Martin Zaťovič
This controller formats the data to a Wiegand format and bit-bangs the message on devicetree defined GPIO lines.
Several attributes need to be defined in the devicetree in order for this driver to work, namely the data-hi-gpios, data-lo-gpios, pulse-len, frame-gap and interval-len. These attributes are documented in the devicetree bindings documentation files.
The driver creates a dev file for writing messages on the bus. It also creates a sysfs file to control the payload length of messages(in bits). If a message is shorter than the set payload length, it will be discarded. On the other hand, if a message is longer, the additional bits will be stripped off.
...
Quoted 1 line
+Date: January 2023
Blast from the past?
...
drivers/wiegand/Kconfig config WIEGAND · line 28
Quoted 7 lines
drivers/wiegand/Kconfig +7
-0+config WIEGAND_GPIO + tristate "GPIO-based wiegand master (write only)" + depends on WIEGAND + help + This GPIO bitbanging Wiegand controller uses the libgpiod library to + utilize GPIO lines for sending Wiegand data. Userspace may access + the Wiegand GPIO interface via a dev entry.
What will be the name of the module if M?
...
Quoted 1 line
+#include <
linux/of.h>
No way.
...
drivers/wiegand/wiegand-gpio.c line 18
Quoted 5 lines
drivers/wiegand/wiegand-gpio.c new file +4
-0+struct wiegand_gpio { +struct device *dev; +struct wiegand_controller *ctlr; +struct miscdevice misc_dev;
Make it first, same idea as per previous patch comments.
drivers/wiegand/wiegand-gpio.c line 23
Quoted 6 lines
drivers/wiegand/wiegand-gpio.c new file +5
-0+struct mutex mutex; +struct gpio_desc *gpio_data_hi; +struct gpio_desc *gpio_data_lo; +struct file_operations fops; +u8 data[WIEGAND_MAX_PAYLEN_BYTES ];
Have you considered DMA alignment? Is it a problem or not here?
Quoted 1 line
+};
...
drivers/wiegand/wiegand-gpio.c line 40
Quoted 6 lines
drivers/wiegand/wiegand-gpio.c new file +5
-0+static ssize_t store_ulong (u32 *val,const char *buf,size_t size,unsigned long max) +{ +int rc; +u32 new; +if (max !=0 && new > max)
First part of the conditional is redundant. When you have such a user, you may add the restriction back.
Quoted 5 lines
drivers/wiegand/wiegand-gpio.c new file +5
-0+return -EINVAL ; + + *val = new; +return size; +}
...
drivers/wiegand/wiegand-gpio.c line 70
Quoted 3 lines
drivers/wiegand/wiegand-gpio.c new file +3
-0+static struct attribute *wiegand_gpio_attrs[] = { + &dev_attr_payload_len.attr, +NULL ,
No comma for the terminator entry.
Review
Quoted 2 lines
(quoted diff) +2
-0+}; +
Redundant blank line.
Quoted 1 line
+ATTRIBUTE_GROUPS(wiegand_gpio);
...
Quoted 5 lines
drivers/wiegand/wiegand-gpio.c new file +5
-0+void wiegand_gpio_send_bit (struct wiegand_gpio *wiegand_gpio,bool value,bool last) +{ +u32 pulse_len = wiegand_gpio->ctlr->pulse_len; +u32 interval_len = wiegand_gpio->ctlr->interval_len; +u32 frame_gap = wiegand_gpio->ctlr->frame_gap;
drivers/wiegand/wiegand-gpio.c line 95
Quoted 10 lines
drivers/wiegand/wiegand-gpio.c new file +10
-0+struct gpio_desc *gpio = value ? wiegand_gpio->gpio_data_hi : wiegand_gpio->gpio_data_lo; + +gpiod_set_value_cansleep (gpio,0 ); +udelay (pulse_len); +gpiod_set_value_cansleep (gpio,1 ); + +if (last) +udelay (frame_gap - pulse_len); +else +udelay (interval_len - pulse_len);
This is quite dangerous. You may end up with CPU 100% load for a long time without any way out. What is the range and why udelay() can't be replaced with usleep_range() for longer waits?
Quoted 1 line
+}
...
drivers/wiegand/wiegand-gpio.c line 105
Quoted 8 lines
drivers/wiegand/wiegand-gpio.c new file +8
-0+/* This function is used for writing from file in dev directory */ +static int wiegand_gpio_write_by_bits (struct wiegand_gpio *wiegand_gpio,u16 bitlen) +{ +size_t i; +bool bit_value, is_last_bit; + +for (i =0 ; i < bitlen; i++) { + bit_value = ((wiegand_gpio->data[i /8 ] >> (7 - (i %8 ))) &0x01 );
Ah, your buffer should probably be a bitmap.
Also consider bitmap_get_value8().
Quoted 8 lines
drivers/wiegand/wiegand-gpio.c new file +8
-0+ is_last_bit = (i +1 ) == bitlen; +wiegand_gpio_send_bit (wiegand_gpio, bit_value, is_last_bit); + } + +return 0 ; +} + +static ssize_t wiegand_gpio_get_user_data(struct wiegand_gpio *wiegand_gpio, char __user const *buf,
drivers/wiegand/wiegand-gpio.c line 123
Quoted 10 lines
drivers/wiegand/wiegand-gpio.c new file +9
-0+ size_t len) +{ +size_t rc; + +if (len >WIEGAND_MAX_PAYLEN_BYTES ) +return -EBADMSG ; + rc =copy_from_user (&wiegand_gpio->data[0 ], buf,WIEGAND_MAX_PAYLEN_BYTES ); +if (rc <0 ) +return rc;
This is wrong. Homework: read the documentation and existing code to see why and how to fix.
Quoted 2 lines
(quoted diff) +2
-0+ return len; +}
...
drivers/wiegand/wiegand-gpio.c line 150
Quoted 9 lines
drivers/wiegand/wiegand-gpio.c new file +9
-0+static ssize_t wiegand_gpio_fwrite (struct file *filp,char __user const *buf,size_t len, +loff_t *offset) +{ +struct wiegand_gpio_instance *info = filp->private_data; +struct wiegand_gpio *wiegand_gpio = info->dev; +u32 msg_length = wiegand_gpio->ctlr->payload_len; +int rc; + +if (buf ==NULL || len ==0 || len *8 < msg_length)
!buf
DIV_ROUND_UP(msg_length / 8) > len
less overflow prone.
Quoted 8 lines
drivers/wiegand/wiegand-gpio.c new file +8
-0+return -EINVAL ; + + rc =wiegand_gpio_get_user_data (wiegand_gpio, buf, len); +if (rc <0 ) +return rc; + +wiegand_gpio_write_by_bits (wiegand_gpio, msg_length); +
drivers/wiegand/wiegand-gpio.c line 168
Quoted 10 lines
drivers/wiegand/wiegand-gpio.c new file +9
-0+return len; +} +static int wiegand_gpio_fopen (struct inode *ino,struct file *filp) +{ +int rc; +struct wiegand_gpio_instance *info; + struct wiegand_gpio *wiegand_gpio = container_of(filp->f_op, struct wiegand_gpio, fops); + +mutex_lock (&wiegand_gpio->mutex);
Can it be interrupted by a signal?
Quoted 23 lines
drivers/wiegand/wiegand-gpio.c new file +23
-0+if ((filp->f_flags &O_ACCMODE ) ==O_RDONLY || (filp->f_flags &O_ACCMODE ) ==O_RDWR ) { +dev_err (wiegand_gpio->dev,"Device is write only\n" ); + rc = -EIO ; + goto err; + } + + info =kzalloc (sizeof (*info),GFP_KERNEL ); +if (!info) { + rc = -ENOMEM ; + goto err; + } + + info->dev = wiegand_gpio; + info->flags = filp->f_flags; +mutex_unlock (&wiegand_gpio->mutex); + + filp->private_data = info; + +return 0 ; +err: +mutex_unlock (&wiegand_gpio->mutex); +return rc; +}
...
drivers/wiegand/wiegand-gpio.c line 199
Quoted 1 line
drivers/wiegand/wiegand-gpio.c new file +1
-0+u8 msg_bytelength = (msg_bitlen %8 ) ? (msg_bitlen /8 ) +1 : (msg_bitlen /8 );
DIV_ROUND_UP() (you will need math.h)
...
drivers/wiegand/wiegand-gpio.c line 230
Quoted 2 lines
drivers/wiegand/wiegand-gpio.c new file +2
-0+if (dev->of_node) + master->dev.of_node = device->dev.of_node;
No.
...
Review
Quoted 2 lines
(quoted diff) +2
-0+ if (status) + return status;
What's this and why is it here?
I'm afraid you haven't compiled this code... :-(
...
drivers/wiegand/wiegand-gpio.c line 236
Quoted 2 lines
drivers/wiegand/wiegand-gpio.c new file +2
-0+ master->transfer_message = &wiegand_gpio_transfer_message; + master->payload_len =26 ;/* set standard 26-bit format */
Can you replace master with some of the suggested words? Or is this a terminology from the specification of the bus?
...
drivers/wiegand/wiegand-gpio.c line 255
Quoted 4 lines
drivers/wiegand/wiegand-gpio.c new file +4
-0+ status =wiegand_gpio_request (dev, wiegand_gpio); +if (status) { +dev_err (wiegand_gpio->dev,"failed at requesting GPIOs\n" ); +return status;
return dev_error_probe();
Ditto for the rest.
Quoted 1 line
(quoted diff) +1
-0+ }
...
drivers/wiegand/wiegand-gpio.c line 259
Quoted 2 lines
drivers/wiegand/wiegand-gpio.c new file +2
-0+ status =gpiod_direction_output (wiegand_gpio->gpio_data_hi,1 ); + status |=gpiod_direction_output (wiegand_gpio->gpio_data_lo,1 );
Huh?!
...
drivers/wiegand/wiegand-gpio.c line 271
Quoted 1 line
drivers/wiegand/wiegand-gpio.c new file +1
-0+ wiegand_gpio->misc_dev.name =kasprintf (GFP_KERNEL ,"wiegand-gpio%u" , master->bus_num);
No checks?
...
drivers/wiegand/wiegand-gpio.c line 283
Quoted 1 line
drivers/wiegand/wiegand-gpio.c new file +1
-0+ dev->groups = wiegand_gpio_groups;
Feels like this can be moved to dev_groups member of the struct driver.
Quoted 3 lines
drivers/wiegand/wiegand-gpio.c new file +3
-0+ +return status; +}
...
drivers/wiegand/wiegand-gpio.c line 299
Quoted 4 lines
drivers/wiegand/wiegand-gpio.c new file +3
-0+static const struct of_device_id wiegand_gpio_dt_idtable[] = { + { .compatible ="wiegand-gpio" , }, + {},
No comma for the terminator entry.
Quoted 1 line
+};
...
Quoted 1 line
+
Redundant blank line.
Quoted 1 line
+module_platform_driver(wiegand_gpio_driver);
Signature · 2 lines
With Best Regards, Andy Shevchenko
kernel test robot
build botRe: [PATCHv3 4/4] wiegand: add Wiegand GPIO bitbanged controller driver
In reply to Martin ZaťovičI love your patch! Perhaps something to improve:
Hi Martin,
I love your patch! Perhaps something to improve:
[auto build test WARNING on robh/for-next]
[also build test WARNING on linus/master v6.2 next-20230301] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Martin-Za-ovi/dt-bindings-wiegand-add-Wiegand-controller-common-properties/20230301-223030 base: https://git.kernel.org/pub/scm/linux/kernel/git/robh/linux.git for-next patch link: https://lore.kernel.org/r/20230301142835.19614-5-m.zatovic1%40gmail.com patch subject: [PATCHv3 4/4] wiegand: add Wiegand GPIO bitbanged controller driver config: sh-allmodconfig (https://download.01.org/0day-ci/archive/20230302/[email protected]/config) compiler: sh4-linux-gcc (GCC) 12.1.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/641c36b9878a19ea4977f0e14df22c7475b423df
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Martin-Za-ovi/dt-bindings-wiegand-add-Wiegand-controller-common-properties/20230301-223030 git checkout 641c36b9878a19ea4977f0e14df22c7475b423df
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=sh olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=sh SHELL=/bin/bash drivers/
If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Link: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> drivers/wiegand/wiegand-gpio.c:50:9: warning: no previous prototype for 'payload_len_show' [-Wmissing-prototypes]
50 | ssize_t payload_len_show(struct device *dev, struct device_attribute *attr, char *buf)
| ^~~~~~~~~~~~~~~~
>> drivers/wiegand/wiegand-gpio.c:58:9: warning: no previous prototype for 'payload_len_store' [-Wmissing-prototypes]
58 | ssize_t payload_len_store(struct device *dev, struct device_attribute *attr, const char *buf,
| ^~~~~~~~~~~~~~~~~
>> drivers/wiegand/wiegand-gpio.c:81:6: warning: no previous prototype for 'wiegand_gpio_send_bit' [-Wmissing-prototypes]
81 | void wiegand_gpio_send_bit(struct wiegand_gpio *wiegand_gpio, bool value, bool last)
| ^~~~~~~~~~~~~~~~~~~~~
>> drivers/wiegand/wiegand-gpio.c:195:5: warning: no previous prototype for 'wiegand_gpio_transfer_message' [-Wmissing-prototypes] 195 | int wiegand_gpio_transfer_message(struct wiegand_device *dev, u8 *message, u8 msg_bitlen)
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
vim +/payload_len_show +50 drivers/wiegand/wiegand-gpio.c
46
47 /*
48 * Attribute file for setting payload length of Wiegand messages.
49 */
> 50 ssize_t payload_len_show(struct device *dev, struct device_attribute *attr, char *buf)
51 {
52 struct wiegand_gpio *wiegand_gpio = dev_get_drvdata(dev);
53 struct wiegand_controller *ctlr = wiegand_gpio->ctlr;
54
55 return sysfs_emit(buf, "%u\n", ctlr->payload_len);
56 }
57
> 58 ssize_t payload_len_store(struct device *dev, struct device_attribute *attr, const char *buf,
59 size_t count)
60 {
61 struct wiegand_gpio *wiegand_gpio = dev_get_drvdata(dev);
62 struct wiegand_controller *ctlr = wiegand_gpio->ctlr;
63
64 return store_ulong(&(ctlr->payload_len), buf, count, WIEGAND_MAX_PAYLEN_BYTES * 8);
65 }
66 DEVICE_ATTR_RW(payload_len);
67
68 static struct attribute *wiegand_gpio_attrs[] = {
69 &dev_attr_payload_len.attr,
70 NULL,
71 };
72
73 ATTRIBUTE_GROUPS(wiegand_gpio);
74
75 /*
76 * To send a bit of value 1 following the wiegand protocol, one must set
77 * the wiegand_data_hi to low for the duration of pulse. Similarly to send
78 * a bit of value 0, the wiegand_data_lo is set to low for pulse duration.
79 * This way the two lines are never low at the same time.
80 */
> 81 void wiegand_gpio_send_bit(struct wiegand_gpio *wiegand_gpio, bool value, bool last)
82 {
83 u32 pulse_len = wiegand_gpio->ctlr->pulse_len;
84 u32 interval_len = wiegand_gpio->ctlr->interval_len;
85 u32 frame_gap = wiegand_gpio->ctlr->frame_gap;
86 struct gpio_desc *gpio = value ? wiegand_gpio->gpio_data_hi : wiegand_gpio->gpio_data_lo;
87
88 gpiod_set_value_cansleep(gpio, 0);
89 udelay(pulse_len);
90 gpiod_set_value_cansleep(gpio, 1);
91
92 if (last)
93 udelay(frame_gap - pulse_len);
94 else
95 udelay(interval_len - pulse_len);
96 }
97
98 /* This function is used for writing from file in dev directory */
99 static int wiegand_gpio_write_by_bits(struct wiegand_gpio *wiegand_gpio, u16 bitlen)
100 {
101 size_t i;
102 bool bit_value, is_last_bit;
103
104 for (i = 0; i < bitlen; i++) {
105 bit_value = ((wiegand_gpio->data[i / 8] >> (7 - (i % 8))) & 0x01);
106 is_last_bit = (i + 1) == bitlen;
107 wiegand_gpio_send_bit(wiegand_gpio, bit_value, is_last_bit);
108 }
109
110 return 0;
111 }
112
113 static ssize_t wiegand_gpio_get_user_data(struct wiegand_gpio *wiegand_gpio, char __user const *buf,
114 size_t len)
115 {
116 size_t rc;
117
118 if (len > WIEGAND_MAX_PAYLEN_BYTES)
119 return -EBADMSG;
120
121 rc = copy_from_user(&wiegand_gpio->data[0], buf, WIEGAND_MAX_PAYLEN_BYTES);
122 if (rc < 0)
123 return rc;
124
125 return len;
126 }
127
128 static int wiegand_gpio_frelease(struct inode *ino, struct file *filp)
129 {
130 struct wiegand_gpio_instance *info = filp->private_data;
131 struct wiegand_gpio *wiegand_gpio = info->dev;
132
133 mutex_lock(&wiegand_gpio->mutex);
134 info->flags = 0;
135 mutex_unlock(&wiegand_gpio->mutex);
136
137 kfree(info);
138
139 return 0;
140 }
141
142 static ssize_t wiegand_gpio_fwrite(struct file *filp, char __user const *buf, size_t len,
143 loff_t *offset)
144 {
145 struct wiegand_gpio_instance *info = filp->private_data;
146 struct wiegand_gpio *wiegand_gpio = info->dev;
147 u32 msg_length = wiegand_gpio->ctlr->payload_len;
148 int rc;
149
150 if (buf == NULL || len == 0 || len * 8 < msg_length)
151 return -EINVAL;
152
153 rc = wiegand_gpio_get_user_data(wiegand_gpio, buf, len);
154 if (rc < 0)
155 return rc;
156
157 wiegand_gpio_write_by_bits(wiegand_gpio, msg_length);
158
159 return len;
160 }
161
162 static int wiegand_gpio_fopen(struct inode *ino, struct file *filp)
163 {
164 int rc;
165 struct wiegand_gpio_instance *info;
166 struct wiegand_gpio *wiegand_gpio = container_of(filp->f_op, struct wiegand_gpio, fops);
167
168 mutex_lock(&wiegand_gpio->mutex);
169
170 if ((filp->f_flags & O_ACCMODE) == O_RDONLY || (filp->f_flags & O_ACCMODE) == O_RDWR) {
171 dev_err(wiegand_gpio->dev, "Device is write only\n");
172 rc = -EIO;
173 goto err;
174 }
175
176 info = kzalloc(sizeof(*info), GFP_KERNEL);
177 if (!info) {
178 rc = -ENOMEM;
179 goto err;
180 }
181
182 info->dev = wiegand_gpio;
183 info->flags = filp->f_flags;
184 mutex_unlock(&wiegand_gpio->mutex);
185
186 filp->private_data = info;
187
188 return 0;
189 err:
190 mutex_unlock(&wiegand_gpio->mutex);
191 return rc;
192 }
193
194 /* This function is used by device drivers */
> 195 int wiegand_gpio_transfer_message(struct wiegand_device *dev, u8 *message, u8 msg_bitlen) 196 {
197 struct wiegand_controller *ctlr = dev->controller;
198 struct wiegand_gpio *wiegand_gpio = wiegand_master_get_devdata(ctlr);
199 u8 msg_bytelength = (msg_bitlen % 8) ? (msg_bitlen / 8) + 1 : (msg_bitlen / 8);
200
201 memcpy(wiegand_gpio->data, message, msg_bytelength);
202 wiegand_gpio_write_by_bits(wiegand_gpio, msg_bitlen);
203
204 return 0;
205 }
206
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests
Re: [PATCHv3 4/4] wiegand: add Wiegand GPIO bitbanged controller driver
In reply to Martin ZaťovičQuoted 97 lines · Martin Zaťovič
This controller formats the data to a Wiegand format and bit-bangs the message on devicetree defined GPIO lines.
Several attributes need to be defined in the devicetree in order for this driver to work, namely the data-hi-gpios, data-lo-gpios, pulse-len, frame-gap and interval-len. These attributes are documented in the devicetree bindings documentation files.
The driver creates a dev file for writing messages on the bus. It also creates a sysfs file to control the payload length of messages(in bits). If a message is shorter than the set payload length, it will be discarded. On the other hand, if a message is longer, the additional bits will be stripped off.
Acked-by Linus Walleij <[email protected]>Signed-off-by Martin Zaťovič <[email protected]>--- .../ABI/testing/sysfs-driver-wiegand-gpio | 9 + MAINTAINERS | 2 + drivers/wiegand/Kconfig | 8 + drivers/wiegand/Makefile | 1 + drivers/wiegand/wiegand-gpio.c | 316 ++++++++++++++++++ 5 files changed, 336 insertions(+)create mode 100644
Documentation/ABI/testing/sysfs-driver-wiegand-gpiocreate mode 100644drivers/wiegand/wiegand-gpio.cDocumentation/ABI/testing/sysfs-driver-wiegand-gpio new file +9
-0@@ -0,0 +1,9 @@ +What: /sys/devices/platform/.../wiegand-gpio-attributes/payload_len +Date: January 2023 +Contact: Martin Zaťovič <[email protected]> +Description: + Read/set the payload length of messages sent by Wiegand GPIO + bit-banging controller in bits. The default value is 26, as + that is the most widely-used length of Wiegand messages. + Controller will only send messages of at least the set length + and it will strip off bits of longer messages.MAINTAINERS +2
-0@@ -22522,7 +22522,9 @@ F: include/linux/wiegand.h WIEGAND GPIO BITBANG DRIVER M: Martin Zaťovič <[email protected]> S: Maintained +F: Documentation/ABI/testing/sysfs-driver-wiegand-gpio F: Documentation/devicetree/bindings/wiegand/wiegand-gpio.yaml +F: drivers/wiegand/wiegand-gpio.c WIIMOTE HID DRIVER M: David Rheinsberg <[email protected]>drivers/wiegand/Kconfig +8
-0@@ -18,3 +18,11 @@ config WIEGAND are initially pulled up. When a bit of value 0 is being transmitted, the D0 line is pulled down. Similarly, when a bit of value 1 is being transmitted, the D1 line is pulled down. + +config WIEGAND_GPIO + tristate "GPIO-based wiegand master (write only)" + depends on WIEGAND + help + This GPIO bitbanging Wiegand controller uses the libgpiod library to + utilize GPIO lines for sending Wiegand data. Userspace may access + the Wiegand GPIO interface via a dev entry.drivers/wiegand/Makefile +1
-0@@ -1 +1,2 @@ obj-$(CONFIG_WIEGAND) += wiegand.o +obj-$(CONFIG_WIEGAND_GPIO) += wiegand-gpio.odrivers/wiegand/wiegand-gpio.c new file +13
-0@@ -0,0 +1,316 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include <linux/delay.h> +#include <linux/gpio/consumer.h> +#include <linux/miscdevice.h> +#include <linux/module.h> +#include <linux/of.h> +#include <linux/poll.h> +#include <linux/platform_device.h> +#include <linux/slab.h> +#include <linux/wiegand.h> + +#define WIEGAND_MAX_PAYLEN_BYTES 256
drivers/wiegand/wiegand-gpio.c line 23
Quoted 10 lines
drivers/wiegand/wiegand-gpio.c new file +10
-0+ +struct wiegand_gpio { +struct device *dev; +struct wiegand_controller *ctlr; +struct miscdevice misc_dev; +struct mutex mutex; +struct gpio_desc *gpio_data_hi; +struct gpio_desc *gpio_data_lo; +struct file_operations fops; +u8 data[WIEGAND_MAX_PAYLEN_BYTES ];
maybe use bitmap for this?
Quoted 249 lines
drivers/wiegand/wiegand-gpio.c new file +249
-0+}; + +struct wiegand_gpio_instance { +struct wiegand_gpio *dev; +unsigned long flags; +}; + +static ssize_t store_ulong (u32 *val,const char *buf,size_t size,unsigned long max) +{ +int rc; +u32 new; + + rc =kstrtou32 (buf,0 , &new); +if (rc) +return rc; + +if (max !=0 && new > max) +return -EINVAL ; + + *val = new; +return size; +} + +/* +* Attribute file for setting payload length of Wiegand messages. +*/ +ssize_t payload_len_show (struct device *dev,struct device_attribute *attr,char *buf) +{ +struct wiegand_gpio *wiegand_gpio =dev_get_drvdata (dev); +struct wiegand_controller *ctlr = wiegand_gpio->ctlr; + +return sysfs_emit (buf,"%u\n" , ctlr->payload_len); +} + +ssize_t payload_len_store (struct device *dev,struct device_attribute *attr,const char *buf, +size_t count) +{ +struct wiegand_gpio *wiegand_gpio =dev_get_drvdata (dev); +struct wiegand_controller *ctlr = wiegand_gpio->ctlr; + +return store_ulong (&(ctlr->payload_len), buf, count,WIEGAND_MAX_PAYLEN_BYTES *8 ); +} +DEVICE_ATTR_RW (payload_len); + +static struct attribute *wiegand_gpio_attrs[] = { + &dev_attr_payload_len.attr, +NULL , +}; + +ATTRIBUTE_GROUPS (wiegand_gpio); + +/* +* To send a bit of value 1 following the wiegand protocol, one must set +* the wiegand_data_hi to low for the duration of pulse. Similarly to send +* a bit of value 0, the wiegand_data_lo is set to low for pulse duration. +* This way the two lines are never low at the same time. +*/ +void wiegand_gpio_send_bit (struct wiegand_gpio *wiegand_gpio,bool value,bool last) +{ +u32 pulse_len = wiegand_gpio->ctlr->pulse_len; +u32 interval_len = wiegand_gpio->ctlr->interval_len; +u32 frame_gap = wiegand_gpio->ctlr->frame_gap; +struct gpio_desc *gpio = value ? wiegand_gpio->gpio_data_hi : wiegand_gpio->gpio_data_lo; + +gpiod_set_value_cansleep (gpio,0 ); +udelay (pulse_len); +gpiod_set_value_cansleep (gpio,1 ); + +if (last) +udelay (frame_gap - pulse_len); +else +udelay (interval_len - pulse_len); +} + +/* This function is used for writing from file in dev directory */ +static int wiegand_gpio_write_by_bits (struct wiegand_gpio *wiegand_gpio,u16 bitlen) +{ +size_t i; +bool bit_value, is_last_bit; + +for (i =0 ; i < bitlen; i++) { + bit_value = ((wiegand_gpio->data[i /8 ] >> (7 - (i %8 ))) &0x01 ); + is_last_bit = (i +1 ) == bitlen; +wiegand_gpio_send_bit (wiegand_gpio, bit_value, is_last_bit); + } + +return 0 ; +} + +static ssize_t wiegand_gpio_get_user_data (struct wiegand_gpio *wiegand_gpio,char __user const *buf, +size_t len) +{ +size_t rc; + +if (len >WIEGAND_MAX_PAYLEN_BYTES ) +return -EBADMSG ; + + rc =copy_from_user (&wiegand_gpio->data[0 ], buf,WIEGAND_MAX_PAYLEN_BYTES ); +if (rc <0 ) +return rc; + +return len; +} + +static int wiegand_gpio_frelease (struct inode *ino,struct file *filp) +{ +struct wiegand_gpio_instance *info = filp->private_data; +struct wiegand_gpio *wiegand_gpio = info->dev; + +mutex_lock (&wiegand_gpio->mutex); + info->flags =0 ; +mutex_unlock (&wiegand_gpio->mutex); + +kfree (info); + +return 0 ; +} + +static ssize_t wiegand_gpio_fwrite (struct file *filp,char __user const *buf,size_t len, +loff_t *offset) +{ +struct wiegand_gpio_instance *info = filp->private_data; +struct wiegand_gpio *wiegand_gpio = info->dev; +u32 msg_length = wiegand_gpio->ctlr->payload_len; +int rc; + +if (buf ==NULL || len ==0 || len *8 < msg_length) +return -EINVAL ; + + rc =wiegand_gpio_get_user_data (wiegand_gpio, buf, len); +if (rc <0 ) +return rc; + +wiegand_gpio_write_by_bits (wiegand_gpio, msg_length); + +return len; +} + +static int wiegand_gpio_fopen (struct inode *ino,struct file *filp) +{ +int rc; +struct wiegand_gpio_instance *info; + struct wiegand_gpio *wiegand_gpio = container_of(filp->f_op, struct wiegand_gpio, fops); + +mutex_lock (&wiegand_gpio->mutex); + +if ((filp->f_flags &O_ACCMODE ) ==O_RDONLY || (filp->f_flags &O_ACCMODE ) ==O_RDWR ) { +dev_err (wiegand_gpio->dev,"Device is write only\n" ); + rc = -EIO ; + goto err; + } + + info =kzalloc (sizeof (*info),GFP_KERNEL ); +if (!info) { + rc = -ENOMEM ; + goto err; + } + + info->dev = wiegand_gpio; + info->flags = filp->f_flags; +mutex_unlock (&wiegand_gpio->mutex); + + filp->private_data = info; + +return 0 ; +err: +mutex_unlock (&wiegand_gpio->mutex); +return rc; +} + +/* This function is used by device drivers */ +int wiegand_gpio_transfer_message (struct wiegand_device *dev,u8 *message,u8 msg_bitlen) +{ +struct wiegand_controller *ctlr = dev->controller; +struct wiegand_gpio *wiegand_gpio =wiegand_master_get_devdata (ctlr); +u8 msg_bytelength = (msg_bitlen %8 ) ? (msg_bitlen /8 ) +1 : (msg_bitlen /8 ); + +memcpy (wiegand_gpio->data, message, msg_bytelength); +wiegand_gpio_write_by_bits (wiegand_gpio, msg_bitlen); + +return 0 ; +} + +static int wiegand_gpio_request (struct device *dev,struct wiegand_gpio *wiegand_gpio) +{ +/* Get GPIO lines using device tree bindings. */ + wiegand_gpio->gpio_data_lo =devm_gpiod_get (dev,"data-lo" ,GPIOD_OUT_HIGH ); +if (IS_ERR (wiegand_gpio->gpio_data_lo)) +return PTR_ERR (wiegand_gpio->gpio_data_lo); + + wiegand_gpio->gpio_data_hi =devm_gpiod_get (dev,"data-hi" ,GPIOD_OUT_HIGH ); +return PTR_ERR_OR_ZERO (wiegand_gpio->gpio_data_hi); +} + +static int wiegand_gpio_probe (struct platform_device *device) +{ +int status; +struct wiegand_controller *master; +struct wiegand_gpio *wiegand_gpio; +struct device *dev = &device->dev; + + master =devm_wiegand_alloc_master (dev,sizeof (*wiegand_gpio)); +if (!master) +return -ENOMEM ; + +if (dev->of_node) + master->dev.of_node = device->dev.of_node; + +if (status) +return status; + + master->transfer_message = &wiegand_gpio_transfer_message; + master->payload_len =26 ;/* set standard 26-bit format */ + + wiegand_gpio =wiegand_master_get_devdata (master); + wiegand_gpio->ctlr = master; + wiegand_gpio->fops.owner =THIS_MODULE ; + wiegand_gpio->fops.open = wiegand_gpio_fopen; + wiegand_gpio->fops.release = wiegand_gpio_frelease; + wiegand_gpio->fops.write = wiegand_gpio_fwrite; + +platform_set_drvdata (device, wiegand_gpio); + + master->bus_num = device->id; + wiegand_gpio->dev = dev; + +mutex_init (&wiegand_gpio->mutex); + + status =wiegand_gpio_request (dev, wiegand_gpio); +if (status) { +dev_err (wiegand_gpio->dev,"failed at requesting GPIOs\n" ); +return status; + } + + status =gpiod_direction_output (wiegand_gpio->gpio_data_hi,1 ); + status |=gpiod_direction_output (wiegand_gpio->gpio_data_lo,1 ); +if (status) { +dev_err (wiegand_gpio->dev,"failed to set GPIOs direction\n" ); +return status; + } + + status =devm_wiegand_register_master (dev, master); +if (status) { +dev_err (wiegand_gpio->dev,"failed to register master\n" ); +return status; + } + + wiegand_gpio->misc_dev.name =kasprintf (GFP_KERNEL ,"wiegand-gpio%u" , master->bus_num); + wiegand_gpio->misc_dev.minor =MISC_DYNAMIC_MINOR ;
drivers/wiegand/wiegand-gpio.c line 282
Quoted 10 lines
drivers/wiegand/wiegand-gpio.c new file +10
-0+ wiegand_gpio->misc_dev.fops = &wiegand_gpio->fops; + + status =misc_register (&wiegand_gpio->misc_dev); +if (status) { +dev_err (wiegand_gpio->dev,"couldn't register misc device\n" ); +return status; + } + wiegand_gpio->misc_dev.parent = wiegand_gpio->dev; + +device_create_file (dev, &dev_attr_payload_len);
As far as I understand, Wiegand GPIO bit-banged driver is just one of the many possible driver implementations. I can imagine having dedicated hardware to send Wiegand messages. So it seems a little strange to have chardev API implemented in the bit-banged driver and not in the core, so it's common for all possible driver implementations.
Quoted 34 lines
drivers/wiegand/wiegand-gpio.c new file +34
-0+ dev->groups = wiegand_gpio_groups; + +return status; +} + +static int wiegand_gpio_remove (struct platform_device *device) +{ +struct wiegand_gpio *wiegand_gpio =platform_get_drvdata (device); + +misc_deregister (&wiegand_gpio->misc_dev); + +return 0 ; +} + +static const struct of_device_id wiegand_gpio_dt_idtable[] = { + { .compatible ="wiegand-gpio" , }, + {}, +}; +MODULE_DEVICE_TABLE (of, wiegand_gpio_dt_idtable); + +static struct platform_driver wiegand_gpio_driver = { + .driver = { + .name ="wiegand-gpio" , + .of_match_table = wiegand_gpio_dt_idtable, + }, + .probe = wiegand_gpio_probe, + .remove = wiegand_gpio_remove, +}; + +module_platform_driver (wiegand_gpio_driver); + +MODULE_LICENSE ("GPL" ); +MODULE_DESCRIPTION ("Wiegand write-only driver realized by GPIOs" ); +MODULE_AUTHOR ("Martin Zaťovič <[email protected]>" );
Signature · 3 lines
Kind regards, Evgeny Boger CTO @ Wiren Board
Quoted 4 lines · Martin Zaťovič
Hello,
thank you for the feedback regarding last patch. I have decided against using a copyright line despite the suggestion.
Thank you for an update. More work is needed.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Hi Martin,
Thank you for you work! I'm currently working on Wiegand *receiver* kernel driver, and hopefully we can make both sending and receiving Wiegand implementation in kernel.
I tried to read all the discussion for the previous series, but still don't quite understand why do we need the infrastructure to be that complex. I mean Wiegand is point-to-point connection, so why do we even need bus/controller/device abstractions at all? There will be always just the single device per controller, right?
Signature · 3 lines
Kind regards, Evgeny Boger CTO @ Wiren Board