1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * Fieldbus Device Driver Core
4 *
5 */
6
7 #ifndef __FIELDBUS_DEV_H
8 #define __FIELDBUS_DEV_H
9
10 #include <linux/cdev.h>
11 #include <linux/wait.h>
12
13 enum fieldbus_dev_type {
14 FIELDBUS_DEV_TYPE_UNKNOWN = 0,
15 FIELDBUS_DEV_TYPE_PROFINET,
16 };
17
18 /**
19 * struct fieldbus_dev - Fieldbus device
20 * @read_area: [DRIVER] function to read the process data area of the
21 * device. same parameters/return values as
22 * the read function in struct file_operations
23 * @write_area: [DRIVER] function to write to the process data area of
24 * the device. same parameters/return values as
25 * the write function in struct file_operations
26 * @write_area_sz [DRIVER] size of the writable process data area
27 * @read_area_sz [DRIVER] size of the readable process data area
28 * @card_name [DRIVER] name of the card, e.g. "ACME Inc. profinet"
29 * @fieldbus_type [DRIVER] fieldbus type of this device, e.g.
30 * FIELDBUS_DEV_TYPE_PROFINET
31 * @enable_get [DRIVER] function which returns true if the card
32 * is enabled, false otherwise
33 * @fieldbus_id_get [DRIVER] function to retrieve the unique fieldbus id
34 * by which this device can be identified;
35 * return value follows the snprintf convention
36 * @simple_enable_set [DRIVER] (optional) function to enable the device
37 * according to its default settings
38 * @parent [DRIVER] (optional) the device's parent device
39 */
40 struct fieldbus_dev {
41 ssize_t (*read_area)(struct fieldbus_dev *fbdev, char __user *buf,
42 size_t size, loff_t *offset);
43 ssize_t (*write_area)(struct fieldbus_dev *fbdev,
44 const char __user *buf, size_t size,
45 loff_t *offset);
46 size_t write_area_sz, read_area_sz;
47 const char *card_name;
48 enum fieldbus_dev_type fieldbus_type;
49 bool (*enable_get)(struct fieldbus_dev *fbdev);
50 int (*fieldbus_id_get)(struct fieldbus_dev *fbdev, char *buf,
51 size_t max_size);
52 int (*simple_enable_set)(struct fieldbus_dev *fbdev, bool enable);
53 struct device *parent;
54
55 /* private data */
56 int id;
57 struct cdev cdev;
58 struct device *dev;
59 int dc_event;
60 wait_queue_head_t dc_wq;
61 bool online;
62 };
63
64 #if IS_ENABLED(CONFIG_FIELDBUS_DEV)
65
66 /**
67 * fieldbus_dev_unregister()
68 * - unregister a previously registered fieldbus device
69 * @fb: Device structure previously registered
70 **/
71 void fieldbus_dev_unregister(struct fieldbus_dev *fb);
72
73 /**
74 * fieldbus_dev_register()
75 * - register a device with the fieldbus device subsystem
76 * @fb: Device structure filled by the device driver
77 **/
78 int __must_check fieldbus_dev_register(struct fieldbus_dev *fb);
79
80 /**
81 * fieldbus_dev_area_updated()
82 * - notify the subsystem that an external fieldbus controller updated
83 * the process data area
84 * @fb: Device structure
85 **/
86 void fieldbus_dev_area_updated(struct fieldbus_dev *fb);
87
88 /**
89 * fieldbus_dev_online_changed()
90 * - notify the subsystem that the fieldbus online status changed
91 * @fb: Device structure
92 **/
93 void fieldbus_dev_online_changed(struct fieldbus_dev *fb, bool online);
94
95 #else /* IS_ENABLED(CONFIG_FIELDBUS_DEV) */
96
97 static inline void fieldbus_dev_unregister(struct fieldbus_dev *fb) {}
98 static inline int __must_check fieldbus_dev_register(struct fieldbus_dev *fb)
99 {
100 return -ENOTSUPP;
101 }
102
103 static inline void fieldbus_dev_area_updated(struct fieldbus_dev *fb) {}
104 static inline void fieldbus_dev_online_changed(struct fieldbus_dev *fb,
105 bool online) {}
106
107 #endif /* IS_ENABLED(CONFIG_FIELDBUS_DEV) */
108 #endif /* __FIELDBUS_DEV_H */