Wiegand bus driver and GPIO bitbanged controller
v5 cover Wiegand bus driver and GPIO bitbanged controller
Hello,
I have fixed all the pointed out issues and I present the next version of Wiegand bus driver and Wiegand GPIO bitbanged controller. The most noticable change is the change of the input data format, which is now ASCII hex string. Please let me know, if you see any issues.
I have also included a brief information about Wiegand in the commit message of the commit adding the bus driver, since there is no official datasheet that I could link.
CHANGELOG:wiegand.c
- kernel symbols are now part of WIEGAND namespace
- added includes for conatiner_of.h and types.h
- made IDA static
- removed redundant null checks and error messages
- added overflow check for memory allocation size
- used devm_add_action_or_reset() for controller allocation
- fixed comments according to kernel doc validator
- avoided dereferencing fwnode in struct device by using device_set_node()
- fixed fwnode format specifier
- removed the overly complicated system for getting timings and replaced it with a clear and easily understandable one
- unified the goto label names
- removed static keyword from struct bus_type
- used bitmap_parse_user() function to parse the user data, which also means that the input format has changed to ASCII hex string
- removed input length checks as they are not valid anymore using the new input format
wiegand.h
- removed unnecesary mod_devicetable.h include and added container_of.h and types.h includes
- added fs.h header for struct file_operations
- removed underscores in some variables names within macros
wiegand-gpio.c
- fixed some error codes
- used fsleep() in the wiegand_gpio_send_bit() function
- edited the loop in wiegand_gpio_write_by_bits() so that it doesnt check for the last bit in each iteration
- used the devm_gpiod_get_array() for acquiring the two gpio lines
- imported WIEGAND namespace
wiegand-gpio.yaml
- dropped devicetree bindings
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 | 46 ++ MAINTAINERS | 14 + drivers/Kconfig | 2 + drivers/Makefile | 1 + drivers/wiegand/Kconfig | 36 ++ drivers/wiegand/Makefile | 2 + drivers/wiegand/wiegand-gpio.c | 192 ++++++ drivers/wiegand/wiegand.c | 590 ++++++++++++++++++ include/linux/wiegand.h | 148 +++++ 11 files changed, 1079 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.40.1
PATCH v5 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
@@ -22922,6 +22922,11 @@ L: [email protected] S: Maintained F: drivers/hid/hid-wiimote* +WIEGAND BUS DRIVER +M: Martin Zaťovič <[email protected]> +S: Maintained +F: Documentation/devicetree/bindings/wiegand/wiegand-controller.yaml + WILOCITY WIL6210 WIRELESS DRIVER L: [email protected] S: Orphan
Signature · 1 line
2.40.1
Rob Herring
maintainerRe: [PATCHv5 1/4] dt-bindings: wiegand: add Wiegand controller common properties
Quoted 4 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).
How would multiple devices work? Seems like you'd need some sort of mux which would be another node. If there's not really any need, then I'd just say it's only a single device. Either way the binding needs to define this.
Quoted 24 lines
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]>Reviewed-by Rob Herring <[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 +4
-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#
Documentation/devicetree/bindings/wiegand/wiegand-controller.yaml line 14
Quoted 10 lines
Documentation/devicetree/bindings/wiegand/wiegand-controller.yaml new file +10
-0+$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.
Some more detail on what Wiegand is would be useful. Link to spec? What it is used for?
Documentation/devicetree/bindings/wiegand/wiegand-controller.yaml line 18
Quoted 4 lines
Documentation/devicetree/bindings/wiegand/wiegand-controller.yaml new file +4
-0+ +properties: + $nodename: + pattern: "^wiegand(@.*|-[0-9a-f])?$"
The '-[0-9a-f]' suffix should be decimal rather than hex.
Documentation/devicetree/bindings/wiegand/wiegand-controller.yaml line 31
Quoted 13 lines
Documentation/devicetree/bindings/wiegand/wiegand-controller.yaml new file +13
-0+ + 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.
If you have multiple devices, you need to define the child node format. Specifically, you need addresses for multiple devices. So you need to define the unit-address format, #address-cells and #size-cells values, and any constraints on 'reg' such as max address and/or number of entries.
Rob
Re: [PATCHv5 1/4] dt-bindings: wiegand: add Wiegand controller common properties
Quoted 40 lines · Rob Herring
Quoted 4 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).
How would multiple devices work? Seems like you'd need some sort of mux which would be another node. If there's not really any need, then I'd just say it's only a single device. Either way the binding needs to define this.
Quoted 29 lines
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]>Reviewed-by Rob Herring <[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 +9
-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:
Quoted 8 lines
Documentation/devicetree/bindings/wiegand/wiegand-controller.yaml line 14
Quoted 5 lines
Documentation/devicetree/bindings/wiegand/wiegand-controller.yaml new file +5
-0+ - 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.Some more detail on what Wiegand is would be useful. Link to spec? What it is used for?
There is no official Wiegand datasheet out there. I can include the summary of some proprietary documents about Wiegand, like the commit message of the second commit in the series.
Quoted 18 lines
Documentation/devicetree/bindings/wiegand/wiegand-controller.yaml line 18
Quoted 5 lines
Documentation/devicetree/bindings/wiegand/wiegand-controller.yaml new file +4
-0+ +properties: + $nodename: + pattern: "^wiegand(@.*|-[0-9a-f])?$"The '-[0-9a-f]' suffix should be decimal rather than hex.
Quoted 10 lines
Documentation/devicetree/bindings/wiegand/wiegand-controller.yaml new file +10
-0+ + 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:
Quoted 8 lines
Documentation/devicetree/bindings/wiegand/wiegand-controller.yaml line 31
Quoted 3 lines
Documentation/devicetree/bindings/wiegand/wiegand-controller.yaml new file +3
-0+ description: + Length of the last bit of a frame (both the pulse and the high phase) in + microseconds.If you have multiple devices, you need to define the child node format. Specifically, you need addresses for multiple devices. So you need to define the unit-address format, #address-cells and #size-cells values, and any constraints on 'reg' such as max address and/or number of entries.
I see.. the bus should really only allow one device to be connected. That means, there is no need to define anything else in this file, right? I will explicitely mention it in the Description and I will add the
unevaluatedPropertied:
type: object
to the wiegand-gpio.yaml file so that adding a child node is allowed.
Quoted 2 lines
Rob
With regards,
Martin Zaťovič
PATCH v5 2/4 wiegand: add Wiegand bus driver
In reply to Martin ZaťovičThe Wiegand protocol serves as a standardized interface protocol, extensively employed within electronic access control systems, to facilitate data exchange between credentials, readers, and door controllers. Its inception can be attributed to the widespread adoption of Wiegand card readers, leveraging the Wiegand effect - a physical phenomenon wherein a Wiegand wire (or card) generates small yet distinct magnetic fields. A Wiegand reader detects the magnetic pulses emitted by the internal wires of the card.
Three wires are central to the Wiegand protocol: a common ground wire and two distinct data wires designated as DATA0 and DATA1. During periods of inactivity, both DATA0 and DATA1 lines remain pulled up. For transmitting a '0,' the DATA0 line is pulled down while DATA1 remains pulled up; conversely, transmitting a '1' causes DATA1 to be pulled down while DATA0 remains pulled up. Notably, this protocol ensures that the two lines never simultaneously experience a low state.
Timing characteristics within the Wiegand protocol lack a uniform standardization, introducing variability between devices. Generally, pulse durations hover between 50 to 100 microseconds, while inter-pulse gaps span 20 to 100 milliseconds. There is no stop bit or similar delimiter to signal the conclusion of a message. Instead, the receiver either counts the bits within the message or enforces a timeout, often set at around ten times the inter-pulse gap duration.
The 26-Bit Wiegand Interface Standard, or 26-Bit Wiegand Format outlines the message format most commonly used among Wiegand devices. This format allocates the initial and terminal bits for parity. The subsequent eight bits following the initial parity bit are reserved for the Facility Code designed for distinct location identification. The remaining bits house the unique ID number. As the technology evolved, new Wiegand formats emerged, including 36-bit and 37-bit formats. It was also common practice for manufacturers to engineer devices compatible with proprietary Wiegand formats tailored to their specifications.
The Wiegand 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 contains all attributes that define the communication such as the payload_len for configuring the size of a single Wiegand message in bits, thus choosing a format. 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 | 25 ++ drivers/wiegand/Makefile | 1 + drivers/wiegand/wiegand.c | 590 ++++++++++++++++++++++++++++++++++++++ include/linux/wiegand.h | 148 ++++++++++ 7 files changed, 769 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
@@ -22926,6 +22926,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 WILOCITY WIL6210 WIRELESS DRIVER L: [email protected]
drivers/Kconfig +2 -0
@@ -145,6 +145,8 @@ source "drivers/vdpa/Kconfig" source "drivers/vhost/Kconfig" +source "drivers/wiegand/Kconfig" + source "drivers/hv/Kconfig" source "drivers/xen/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 +25 -0
@@ -0,0 +1,25 @@ +config WIEGAND + tristate "Wiegand Bus driver" + help + The "Wiegand Interface" stands as an asynchronous low-lever protocol + or a wiring standard. Its primary application is within point-to-point + communication scenarios, especially between a credential and a reader + as well as between card reader and a door controller. Wiegand's data + throughput tends to be modest relatively to other protocols and it + depends upon the selected pulse duration and intervals between pulses. + + Despite its relatively advanced age, Wiegand remains extensively + employed in access control systems, particularly for linking card + swipe mechanisms. These mechanisms harness the Wiegand effect as a + means of transferring data from a card to a reader. + + Wiegand uses two wires to transmit the data - DATA0 and DATA1. Both + lines are initially pulled up. When a bit of value 0 is being + transmitted, the DATA0 line is pulled down. Similarly, when a bit of + value 1 is being transmitted, the DATA1 line is pulled down. + + If you want Wiegand support, you should say Y here and also to the + specific controller driver(s) below. + + This Wiegand support can also be built as a module. If so, the module + will be called wiegand.
drivers/wiegand/Makefile new file +1 -0
@@ -0,0 +1 @@ +obj-$(CONFIG_WIEGAND) += wiegand.o
drivers/wiegand/wiegand.c new file +590 -0
590 lines not on this page · raw message
+MODULE_AUTHOR("Martin Zaťovič <[email protected]>");
include/linux/wiegand.h new file +148 -0
148 lines not on this page · raw message
Signature · 1 line
2.40.1
Re: [PATCHv5 2/4] wiegand: add Wiegand bus driver
Quoted 1 line · Martin Zaťovič
+static inline void
wiegand_controller_put(void *ptr)
Why is this a void *? It should be "struct wiegand_controller *"
Please never use void * if at all possible.
include/linux/wiegand.h line 139
Quoted 3 lines
include/linux/wiegand.h new file +3
-0+static inline void wiegand_unregister_driver (struct wiegand_driver *wdrv) +{ +if (wdrv)
How could this ever be true?
include/linux/wiegand.h line 145
Quoted 6 lines
include/linux/wiegand.h new file +6
-0+driver_unregister (&wdrv->driver); +} + +static inline struct wiegand_driver *to_wiegand_driver (struct device_driver *drv) +{ + return drv ? container_of(drv, struct wiegand_driver, driver) : NULL;
How could drv be NULL?
And you should make this a const *, right?
thanks,
greg k-h
Re: [PATCHv5 2/4] wiegand: add Wiegand bus driver
Quoted 4 lines · Greg KH
Quoted 1 line · Martin Zaťovič
+static inline void
wiegand_controller_put(void *ptr)Why is this a void *? It should be "struct
wiegand_controller*"
It is a void* because it is being passed to devm_add_action_or_reset() and this function only accepts a pointer to a function that accepts a void* parameter. I am not sure if there is a way around it.
Quoted 18 lines
Please never use void * if at all possible.
include/linux/wiegand.h line 139
Quoted 3 lines
include/linux/wiegand.h new file +3
-0+static inline void wiegand_unregister_driver (struct wiegand_driver *wdrv) +{ +if (wdrv)How could this ever be true?
include/linux/wiegand.h line 145
Quoted 6 lines
include/linux/wiegand.h new file +6
-0+driver_unregister (&wdrv->driver); +} + +static inline struct wiegand_driver *to_wiegand_driver (struct device_driver *drv) +{ + return drv ? container_of(drv, struct wiegand_driver, driver) : NULL;How could drv be NULL?
Quoted 1 line
And you should make this a const *, right?
Will fix the other issues. Thank you!
Quoted 4 lines
thanks,
greg k-h
Re: [PATCHv5 2/4] wiegand: add Wiegand bus driver
Quoted 9 lines · Martin Zaťovič
Quoted 4 lines · Greg KH
Quoted 1 line · Martin Zaťovič
+static inline void
wiegand_controller_put(void *ptr)Why is this a void *? It should be "struct
wiegand_controller*"It is a void* because it is being passed to
devm_add_action_or_reset()and this function only accepts a pointer to a function that accepts a void* parameter. I am not sure if there is a way around it.
Why is this passed to that function? This feels odd, I'll look at it again when you send your next version, but this is not a normal "put" type of call at all to have.
And inline functions can't be passed as parameters (well they can, but then they are no longer an inline function...)
thanks,
greg k-h
Re: [PATCHv5 2/4] wiegand: add Wiegand bus driver
In reply to Martin ZaťovičQuoted 2 lines · Martin Zaťovič
Quoted 1 line · Greg KH
On Thu, Aug 24, 2023 at 01:10:13PM +0200, Martin Zaťovič wrote:
...
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
+static inline void
wiegand_controller_put(void *ptr)Why is this a void *? It should be "struct
wiegand_controller*"It is a void* because it is being passed to
devm_add_action_or_reset()and this function only accepts a pointer to a function that accepts a void* parameter. I am not sure if there is a way around it.
I just replied with a deeper review with the answer to this.
Quoted 1 line
Quoted 1 line
Please never use void * if at all possible.
Correct. We better to have a type checking when possible. Esp. for the exported functions.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCHv5 2/4] wiegand: add Wiegand bus driver
In reply to Martin ZaťovičQuoted 40 lines · Martin Zaťovič
The Wiegand protocol serves as a standardized interface protocol, extensively employed within electronic access control systems, to facilitate data exchange between credentials, readers, and door controllers. Its inception can be attributed to the widespread adoption of Wiegand card readers, leveraging the Wiegand effect - a physical phenomenon wherein a Wiegand wire (or card) generates small yet distinct magnetic fields. A Wiegand reader detects the magnetic pulses emitted by the internal wires of the card.
Three wires are central to the Wiegand protocol: a common ground wire and two distinct data wires designated as DATA0 and DATA1. During periods of inactivity, both DATA0 and DATA1 lines remain pulled up. For transmitting a '0,' the DATA0 line is pulled down while DATA1 remains pulled up; conversely, transmitting a '1' causes DATA1 to be pulled down while DATA0 remains pulled up. Notably, this protocol ensures that the two lines never simultaneously experience a low state.
Timing characteristics within the Wiegand protocol lack a uniform standardization, introducing variability between devices. Generally, pulse durations hover between 50 to 100 microseconds, while inter-pulse gaps span 20 to 100 milliseconds. There is no stop bit or similar delimiter to signal the conclusion of a message. Instead, the receiver either counts the bits within the message or enforces a timeout, often set at around ten times the inter-pulse gap duration.
The 26-Bit Wiegand Interface Standard, or 26-Bit Wiegand Format outlines the message format most commonly used among Wiegand devices. This format allocates the initial and terminal bits for parity. The subsequent eight bits following the initial parity bit are reserved for the Facility Code designed for distinct location identification. The remaining bits house the unique ID number. As the technology evolved, new Wiegand formats emerged, including 36-bit and 37-bit formats. It was also common practice for manufacturers to engineer devices compatible with proprietary Wiegand formats tailored to their specifications.
The Wiegand 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 contains all attributes that define the communication such as the
payload_lenfor configuring the size of a single Wiegand message in bits, thus choosing a format. Each Wiegand controller should be associated with one Wiegand device, as Wiegand is typically a point-to-point bus.
...
Quoted 1 line
+#include <
linux/bitops.h>
Should be bitmap.h
drivers/wiegand/wiegand.c line 9
Quoted 6 lines
drivers/wiegand/wiegand.c new file +6
-0+#include <linux/cdev.h> +#include <linux/container_of.h> +#include <linux/device.h> +#include <linux/dmaengine.h> +#include <linux/dma-mapping.h> +#include <linux/module.h>
mutex.h ?
Quoted 5 lines
drivers/wiegand/wiegand.c new file +5
-0+#include <linux/of_device.h> +#include <linux/property.h> +#include <linux/slab.h> +#include <linux/types.h> +#include <linux/wiegand.h>
...
drivers/wiegand/wiegand.c line 67
Quoted 9 lines
drivers/wiegand/wiegand.c new file +9
-0+static ssize_t wiegand_fwrite (struct file *filp,char __user const *buf,size_t len, +loff_t *offset) +{ +int ret; +struct wiegand_controller *ctlr = filp->private_data; + +mutex_lock (&ctlr->file_lock); + +if (!buf || len ==0 )
Ouch?!
To avoid this, you can use cleanup.h and guard your functions with a lock, moreover in this case the check is not even needed to be performed under the lock.
Quoted 11 lines
drivers/wiegand/wiegand.c new file +11
-0+return -EINVAL ; + + ret =wiegand_get_user_data (ctlr, buf, len); +if (ret <0 ) +return ret; + + ctlr->transfer_message (ctlr); + +mutex_unlock (&ctlr->file_lock); +return len; +}
...
drivers/wiegand/wiegand.c line 87
Quoted 8 lines
drivers/wiegand/wiegand.c new file +8
-0+static int wiegand_fopen (struct inode *ino,struct file *filp) +{ +int ret; + struct wiegand_controller *ctlr = container_of(filp->f_op, struct wiegand_controller, fops); + + filp->private_data = ctlr; + +mutex_lock (&ctlr->file_lock);
Do you care about the call being interrupted by a signal? Ditto for other system calls in the framework.
Quoted 13 lines
drivers/wiegand/wiegand.c new file +13
-0+if ((filp->f_flags &O_ACCMODE ) ==O_RDONLY || (filp->f_flags &O_ACCMODE ) ==O_RDWR ) { +dev_err (ctlr->miscdev.this_device,"device is write only\n" ); + ret = -EIO ; + goto out_unlock; + } + +mutex_unlock (&ctlr->file_lock); +return 0 ; + +out_unlock: +mutex_unlock (&ctlr->file_lock); +return ret; +}
...
drivers/wiegand/wiegand.c line 141
Quoted 1 line
drivers/wiegand/wiegand.c new file +1
-0+size_t ctlr_size =ALIGN (sizeof (*ctlr),dma_get_cache_alignment ());
Yeah, yet another user for a new macro in overflow.h (not yet there, though).
Quoted 8 lines
drivers/wiegand/wiegand.c new file +8
-0+size_t total_size; + +if (check_add_overflow (size, ctlr_size, &total_size)) +return NULL ; + + ctlr =kzalloc (total_size,GFP_KERNEL ); +if (!ctlr) +return NULL ;
...
drivers/wiegand/wiegand.c line 182
Quoted 10 lines
drivers/wiegand/wiegand.c new file +10
-0+struct wiegand_controller *devm_wiegand_alloc_controller (struct device *dev,unsigned int size, +bool secondary) +{ +struct wiegand_controller *ctlr; + + ctlr =wiegand_alloc_controller (dev, size, secondary); +if (ctlr) + ctlr->devm_allocated = true; +else +return NULL ;
if (!ctrl) return NULL;
or maybe
return ERR_PTR(-ENOMEM);
Review
Quoted 2 lines
(quoted diff) +2
-0+ if (devm_add_action_or_reset(dev, wiegand_controller_put, ctlr)) + return NULL;
ret = ... if (ret) return ERR_PTR(ret);
?
Quoted 2 lines
(quoted diff) +2
-0+ + return ctlr;
...
Quoted 5 lines
+/**
+ *register_wiegand_device- allocates and registers a new Wiegand device
+ *
+ * @ctlr: controller structure to attach device to
+ * @node: firmware node for the device
Run
scripts/kernel-doc -v -none -Wall ...
against this file and fix all warnings.
drivers/wiegand/wiegand.c line 205
Quoted 3 lines
+ */
drivers/wiegand/wiegand.c new file +1
-0+ struct fwnode_handle *node)
Usually we call it fwnode.
...
drivers/wiegand/wiegand.c line 215
Quoted 1 line
drivers/wiegand/wiegand.c new file +1
-0+device_set_node (&wiegand->dev, node);
device_set_node(&wiegand->dev, fwnode_handle_get(fwnode));
drivers/wiegand/wiegand.c line 230
Quoted 3 lines
drivers/wiegand/wiegand.c new file +2
-0+out_node_put: +fwnode_handle_put (node);
Hmm... Shouldn't we do this at the ->release()?
Quoted 4 lines
drivers/wiegand/wiegand.c new file +4
-0+put_device (&wiegand->dev); +wiegand_cleanup (wiegand); +return ERR_PTR (ret); +}
...
drivers/wiegand/wiegand.c line 239
Quoted 1 line
drivers/wiegand/wiegand.c new file +1
-0+struct fwnode_handle *node;
fwnode
here and everywhere else.
...
drivers/wiegand/wiegand.c line 241
Quoted 1 line
drivers/wiegand/wiegand.c new file +1
-0+fwnode_for_each_available_child_node (ctlr->dev.fwnode, node) {
device_for_each_child_node()
Quoted 4 lines
drivers/wiegand/wiegand.c new file +4
-0+ wiegand =register_wiegand_device (ctlr, node); +if (IS_ERR (wiegand)) +dev_warn (&ctlr->dev,"failed to create wiegand device for %pfwf\n" , node); + }
...
drivers/wiegand/wiegand.c line 261
Quoted 1 line
drivers/wiegand/wiegand.c new file +1
-0+struct device *ctlr_dev = &ctlr->dev;
Just name it "dev". You can use similar approach in another functions, like the above, to make them look nicer.
...
drivers/wiegand/wiegand.c line 267
Quoted 5 lines
drivers/wiegand/wiegand.c new file +5
-0+ ret =device_property_read_u32 (ctlr_dev,"pulse-len-us" , &pulse_len); +if (!ret && pulse_len >0 ) + timing->pulse_len = pulse_len; +else + timing->pulse_len =WIEGAND_DEFAULT_PULSE_LEN ;
What about
/* Use default if property is absent or can't be read */ pulse_len = WIEGAND_DEFAULT_PULSE_LEN; device_property_read_u32(dev, "pulse-len-us", &pulse_len); timing->pulse_len = pulse_len;
Wrong values should be caught up by DT schema linter, no? If the parameters are 0 it's not your problem, although you can warn.
I'm not sure about this, so your current variant is okay.
Ditto for the rest.
...
Quoted 1 line
+static int
__unregister(struct device *dev, void *null)
It is recommended to use namespace even if it's static function.
...
Review
Quoted 2 lines
(quoted diff) +2
-0+void wiegand_unregister_controller(void *ptr) +{
Why the parameter is not properly typed?
Yes for devm you probably need a wrapper
static devm_..._unregister_...(void *ctrl)
{
wiegand_unregister_controller(ctrl)
}
For the exported _unregister, taking into account the possible optional support for this, you may need to check the parameter to be valid.
if (IS_ERR_OR_NULL(...)) // _OR_NULL probably is not needed as per above return;
drivers/wiegand/wiegand.c line 362
Quoted 9 lines
drivers/wiegand/wiegand.c new file +7
-0+} + status =misc_register (&ctlr->miscdev); +if (status) { +dev_err (&ctlr->dev,"couldn't register misc device\n" ); + goto out_free_ida_name; + } +mutex_init (&ctlr->file_lock);
Shouldn't it be needed to be initialized before device itself?
...
drivers/wiegand/wiegand.c line 365
Quoted 2 lines
drivers/wiegand/wiegand.c new file +2
-0+ status =device_add (&ctlr->dev); +if (status <0 )
Don't we need to call something like device_del() or put_device() at this point? OK, read doc for device_add() it clarifies this.
Quoted 1 line
drivers/wiegand/wiegand.c new file +1
-0+ goto out_free_ida_name_misc;
...
drivers/wiegand/wiegand.c line 480
Quoted 7 lines
drivers/wiegand/wiegand.c new file +7
-0+ status =__wiegand_add_device (wiegand); +if (!status) { + ctlr->device_count++; + wiegand->id = wiegand->controller->device_count; + } + +return status;
if (status) return status; ... return 0;
...
drivers/wiegand/wiegand.c line 503
Quoted 3 lines
drivers/wiegand/wiegand.c new file +3
-0+void wiegand_unregister_device (struct wiegand_device *wiegand) +{ +if (!wiegand)
IS_ERR_OR_NULL() or alike (see above)
drivers/wiegand/wiegand.c line 506
Quoted 3 lines
drivers/wiegand/wiegand.c new file +3
-0+return ; + +fwnode_handle_put (wiegand->dev.fwnode);
No, do not dereference fwnode in struct device, always use proper API and/or dev_fwnode() accessor.
Quoted 4 lines
drivers/wiegand/wiegand.c new file +4
-0+device_del (&wiegand->dev); +wiegand_cleanup (wiegand); +put_device (&wiegand->dev); +}
...
drivers/wiegand/wiegand.c line 532
Quoted 6 lines
drivers/wiegand/wiegand.c new file +6
-0+static int wiegand_match_device (struct device *dev,struct device_driver *drv) +{ +struct wiegand_device *wiegand_dev =to_wiegand_device (dev); + +if (of_driver_match_device (dev, drv)) +return 1 ;
I would add ACPI support as well as
if (acpi_driver_match_device(dev, drv)) return 1;
Quoted 2 lines
drivers/wiegand/wiegand.c new file +2
-0+return strcmp (wiegand_dev->modalias, drv->name) ==0 ; +}
...
drivers/wiegand/wiegand.c line 576
Quoted 7 lines
drivers/wiegand/wiegand.c new file +7
-0+static int __init wiegand_init (void ) +{ +int ret; + + ret =bus_register (&wiegand_bus_type); +if (ret <0 ) +pr_err ("Wiegand bus registration failed: %d\n" , ret);
pr_fmt() may help to have unified prefix for all messages printed with help of pr_*() macros.
Quoted 2 lines
(quoted diff) +2
-0+ return ret; +}
...
Quoted 1 line
+#define
WIEGAND_MAX_PAYLEN_BYTES256
I don't see you use BYTES, but rather BITS. Can you define _BITS instead?
...
Quoted 1 line
+struct
wiegand_device;
+ Blank line.
Quoted 2 lines
+/**
+ * structwiegand_timing- Wiegand timings in microseconds
Perhaps timings ?
Quoted 4 lines
+ * @pulse_len: length of the low pulse
+ * @interval_len: length of a whole bit (both the pulse and the high phase)
+ * @frame_gap: length of the last bit of a frame (both the pulse and the high phase)
+ */
...
include/linux/wiegand.h line 59
Quoted 1 line
include/linux/wiegand.h new file +1
-0+struct wiegand_timing timing;
Perhaps timings ?
...
Quoted 1 line
+#endif /*
H_LINUX_INCLUDE_LINUX_WIEGAND_H*/
Leading H is something unusual?
Signature · 2 lines
With Best Regards, Andy Shevchenko
PATCH v5 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 - DATA0(low data line) and DATA1(high data line).
--- .../bindings/wiegand/wiegand-gpio.yaml | 46 +++++++++++++++++++ MAINTAINERS | 5 ++ 2 files changed, 51 insertions(+) create mode 100644 Documentation/devicetree/bindings/wiegand/wiegand-gpio.yaml
Documentation/devicetree/bindings/wiegand/wiegand-gpio.yaml new file +46 -0
@@ -0,0 +1,46 @@ +# 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 properties + +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-gpios: + description: GPIOs used as Wiegand data lines, DATA0 and DATA1 respectivelly. + maxItems: 2 + +required: + - compatible + - data-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-gpios = <&gpio2 6 (GPIO_ACTIVE_HIGH|GPIO_OPEN_DRAIN)>, + <&gpio2 7 (GPIO_ACTIVE_HIGH|GPIO_OPEN_DRAIN)>; + + /* devices */ + };
MAINTAINERS +5 -0
@@ -22929,6 +22929,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 + WILOCITY WIL6210 WIRELESS DRIVER L: [email protected] S: Orphan
Signature · 1 line
2.40.1
Rob Herring
maintainerRe: [PATCHv5 3/4] dt-bindings: wiegand: add GPIO bitbanged Wiegand controller
Quoted 4 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 - DATA0(low data line) and DATA1(high data line).
Quoted 8 lines
Acked-by Linus Walleij <[email protected]>Signed-off-by Martin Zaťovič <[email protected]>--- .../bindings/wiegand/wiegand-gpio.yaml | 46 +++++++++++++++++++ MAINTAINERS | 5 ++ 2 files changed, 51 insertions(+) create mode 100644 Documentation/devicetree/bindings/wiegand/wiegand-gpio.yaml
My bot found errors running 'make DT_CHECKER_FLAGS=-m dt_binding_check' on your patch (DT_CHECKER_FLAGS is new in v5.13):
yamllint warnings/errors:
dtschema/dtc warnings/errors:
Documentation/devicetree/bindings/wiegand/wiegand-gpio.example.dts:20.21-29.11: Warning (unit_address_vs_reg): /example-0/wiegand@f00: node has a unit name, but no reg or ranges property
doc reference errors (make refcheckdocs):
See https://patchwork.ozlabs.org/project/devicetree-bindings/patch/[email protected]
The base for the series is generally the latest rc1. A different dependency should be noted in *this* patch.
If you already ran 'make dt_binding_check' and didn't see the above error(s), then make sure 'yamllint' is installed and dt-schema is up to date:
pip3 install dtschema --upgrade
Please check and re-submit after running the above command yourself. Note that DT_SCHEMA_FILES can be set to your schema file to speed up checking your schema. However, it must be unset to test all examples with your schema.
Rob Herring
maintainerRe: [PATCHv5 3/4] dt-bindings: wiegand: add GPIO bitbanged Wiegand controller
In reply to Martin ZaťovičQuoted 39 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 - DATA0(low data line) and DATA1(high data line).
Acked-by Linus Walleij <[email protected]>Signed-off-by Martin Zaťovič <[email protected]>--- .../bindings/wiegand/wiegand-gpio.yaml | 46 +++++++++++++++++++ MAINTAINERS | 5 ++ 2 files changed, 51 insertions(+) create mode 100644 Documentation/devicetree/bindings/wiegand/wiegand-gpio.yamlDocumentation/devicetree/bindings/wiegand/wiegand-gpio.yaml new file +21
-0@@ -0,0 +1,46 @@ +# 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 properties + +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
Documentation/devicetree/bindings/wiegand/wiegand-gpio.yaml line 31
Quoted 10 lines
Documentation/devicetree/bindings/wiegand/wiegand-gpio.yaml new file +10
-0+ + data-gpios: + description: GPIOs used as Wiegand data lines, DATA0 and DATA1 respectivelly. + maxItems: 2 + +required: + - compatible + - data-gpios + +unevaluatedProperties: false
You'll find this fails if you add an actual child node. Assuming you go with only 1 child node allowed, then you need:
unevaluatedProperties:
type: object
(There's not any way to say only 1 child, but multiple would have constraints in the common binding as I explained.)
Rob
PATCH v5 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.
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 | 11 + drivers/wiegand/Makefile | 1 + drivers/wiegand/wiegand-gpio.c | 192 ++++++++++++++++++ 5 files changed, 215 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: August 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 message 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
@@ -22932,7 +22932,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 WILOCITY WIL6210 WIRELESS DRIVER L: [email protected]
drivers/wiegand/Kconfig +11 -0
@@ -23,3 +23,14 @@ config WIEGAND This Wiegand support can also be built as a module. If so, the module will be called wiegand. + +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 file. + + This support is also available as a module. If so, the module will be + called wiegand-gpio.
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 +192 -0
192 lines not on this page · raw message
Signature · 1 line
2.40.1
Re: [PATCHv5 4/4] wiegand: add Wiegand GPIO bitbanged controller driver
Quoted 8 lines · Martin Zaťovič
This controller formats the data to a Wiegand format and bit-bangs the message on devicetree defined GPIO lines.
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: August 2023
Unrealistic. Use https://hansen.beer/~dave/phb/ to define Date and KernelVersion fields.
...
Quoted 1 line
+#define
WIEGAND_DEFAULT_PAYLOAD_LEN26
_LEN --> _BITS
...
drivers/wiegand/wiegand-gpio.c line 53
Quoted 1 line
drivers/wiegand/wiegand-gpio.c new file +1
-0+return store_ulong (&(ctlr->payload_len), buf, count,WIEGAND_MAX_PAYLEN_BYTES *8 );
Yeah, again use _BITS directly.
...
Quoted 2 lines
+/**
+ *wiegand_gpio_send_bit- Sends a single bit
Do not forget to validate this with scripts/kernel-doc.
Quoted 9 lines
+ * @wiegand_gpio: Instance of the Wiegand
+ * @value: Bit value to send
+ * @last: Indicates last bit of a message
+ *
+ * To send a bit of value 1 following the wiegand protocol, one must set
+ * thewiegand_data_hito low for the duration of pulse. Similarly to send
+ * a bit of value 0, thewiegand_data_lois set to low for pulse duration.
+ * This way the two lines are never low at the same time.
+ */
...
drivers/wiegand/wiegand-gpio.c line 83
Quoted 2 lines
drivers/wiegand/wiegand-gpio.c new file +2
-0+struct gpio_desc *gpio = value ? wiegand_gpio->gpios->desc[1 ] : + wiegand_gpio->gpios->desc[0 ];
Make it separate line.
gpio = value ? wiegand_gpio->gpios->desc[1] : wiegand_gpio->gpios->desc[0];
...
drivers/wiegand/wiegand-gpio.c line 126
Quoted 3 lines
drivers/wiegand/wiegand-gpio.c new file +2
-0+ wiegand_gpio->gpios =devm_gpiod_get_array (dev,"data" ,GPIOD_OUT_HIGH ); +
Redundant blank line.
drivers/wiegand/wiegand-gpio.c line 130
Quoted 4 lines
drivers/wiegand/wiegand-gpio.c new file +4
-0+if (IS_ERR (wiegand_gpio->gpios)) +dev_err (dev,"unable to get gpios\n" ); + +return PTR_ERR_OR_ZERO (wiegand_gpio->gpios);
Oh.
wiegand_gpio->gpios = devm_gpiod_get_array(dev, "data", GPIOD_OUT_HIGH); ret = PTR_...(); if (ret) dev_err(); return ret;
...
Quoted 1 line
+static int
wiegand_gpio_probe(struct platform_device *device)
device --> pdev
...
drivers/wiegand/wiegand-gpio.c line 174
Quoted 2 lines
drivers/wiegand/wiegand-gpio.c new file +2
-0+static const struct of_device_id wiegand_gpio_dt_idtable[] = { + { .compatible ="wiegand-gpio" , },
Inner comma is not needed.
Quoted 2 lines
(quoted diff) +2
-0+ {} +};
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCHv5 4/4] wiegand: add Wiegand GPIO bitbanged controller driver
Quoted 1 line · Andy Shevchenko
On Thu, Aug 24, 2023 at 01:10:15PM +0200, Martin Zaťovič wrote:
...
Quoted 4 lines
Quoted 1 line
+Date: August 2023
Unrealistic. Use https://hansen.beer/~dave/phb/ to define Date and KernelVersion fields.
Hint: should be for v6.7 at least.
Signature · 2 lines
With Best Regards, Andy Shevchenko