1 // SPDX-License-Identifier: GPL-2.0
3 * HMS Profinet Client Driver
5 * Copyright (C) 2018 Arcx Inc
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/slab.h>
13 /* move to <linux/fieldbus_dev.h> when taking this out of staging */
14 #include "../fieldbus_dev.h"
16 /* move to <linux/anybuss-client.h> when taking this out of staging */
17 #include "anybuss-client.h"
19 #define PROFI_DPRAM_SIZE 512
22 * ---------------------------------------------------------------
23 * Anybus Profinet mailbox messages - definitions
24 * ---------------------------------------------------------------
25 * note that we're depending on the layout of these structures being
26 * exactly as advertised.
34 struct fieldbus_dev fbdev;
35 struct anybuss_client *client;
36 struct mutex enable_lock; /* serializes card enable */
41 profi_read_area(struct fieldbus_dev *fbdev, char __user *buf, size_t size,
44 struct profi_priv *priv = container_of(fbdev, struct profi_priv, fbdev);
46 return anybuss_read_output(priv->client, buf, size, offset);
50 profi_write_area(struct fieldbus_dev *fbdev, const char __user *buf,
51 size_t size, loff_t *offset)
53 struct profi_priv *priv = container_of(fbdev, struct profi_priv, fbdev);
55 return anybuss_write_input(priv->client, buf, size, offset);
58 static int profi_id_get(struct fieldbus_dev *fbdev, char *buf,
61 struct profi_priv *priv = container_of(fbdev, struct profi_priv, fbdev);
62 struct msg_mac_addr response;
65 ret = anybuss_recv_msg(priv->client, 0x0010, &response,
69 return snprintf(buf, max_size, "%pM\n", response.addr);
72 static bool profi_enable_get(struct fieldbus_dev *fbdev)
74 struct profi_priv *priv = container_of(fbdev, struct profi_priv, fbdev);
77 mutex_lock(&priv->enable_lock);
78 power_on = priv->power_on;
79 mutex_unlock(&priv->enable_lock);
84 static int __profi_enable(struct profi_priv *priv)
87 struct anybuss_client *client = priv->client;
88 /* Initialization Sequence, Generic Anybus Mode */
89 const struct anybuss_memcfg mem_cfg = {
91 .input_dpram = PROFI_DPRAM_SIZE,
92 .input_total = PROFI_DPRAM_SIZE,
94 .output_dpram = PROFI_DPRAM_SIZE,
95 .output_total = PROFI_DPRAM_SIZE,
96 .offl_mode = FIELDBUS_DEV_OFFL_MODE_CLEAR,
100 * switch anybus off then on, this ensures we can do a complete
101 * configuration cycle in case anybus was already on.
103 anybuss_set_power(client, false);
104 ret = anybuss_set_power(client, true);
107 ret = anybuss_start_init(client, &mem_cfg);
110 ret = anybuss_finish_init(client);
113 priv->power_on = true;
117 anybuss_set_power(client, false);
118 priv->power_on = false;
122 static int __profi_disable(struct profi_priv *priv)
124 struct anybuss_client *client = priv->client;
126 anybuss_set_power(client, false);
127 priv->power_on = false;
131 static int profi_simple_enable(struct fieldbus_dev *fbdev, bool enable)
134 struct profi_priv *priv = container_of(fbdev, struct profi_priv, fbdev);
136 mutex_lock(&priv->enable_lock);
138 ret = __profi_enable(priv);
140 ret = __profi_disable(priv);
141 mutex_unlock(&priv->enable_lock);
146 static void profi_on_area_updated(struct anybuss_client *client)
148 struct profi_priv *priv = anybuss_get_drvdata(client);
150 fieldbus_dev_area_updated(&priv->fbdev);
153 static void profi_on_online_changed(struct anybuss_client *client, bool online)
155 struct profi_priv *priv = anybuss_get_drvdata(client);
157 fieldbus_dev_online_changed(&priv->fbdev, online);
160 static int profinet_probe(struct anybuss_client *client)
162 struct profi_priv *priv;
163 struct device *dev = &client->dev;
166 client->on_area_updated = profi_on_area_updated;
167 client->on_online_changed = profi_on_online_changed;
168 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
171 mutex_init(&priv->enable_lock);
172 priv->client = client;
173 priv->fbdev.read_area_sz = PROFI_DPRAM_SIZE;
174 priv->fbdev.write_area_sz = PROFI_DPRAM_SIZE;
175 priv->fbdev.card_name = "HMS Profinet IRT (Anybus-S)";
176 priv->fbdev.fieldbus_type = FIELDBUS_DEV_TYPE_PROFINET;
177 priv->fbdev.read_area = profi_read_area;
178 priv->fbdev.write_area = profi_write_area;
179 priv->fbdev.fieldbus_id_get = profi_id_get;
180 priv->fbdev.enable_get = profi_enable_get;
181 priv->fbdev.simple_enable_set = profi_simple_enable;
182 priv->fbdev.parent = dev;
183 err = fieldbus_dev_register(&priv->fbdev);
186 dev_info(dev, "card detected, registered as %s",
187 dev_name(priv->fbdev.dev));
188 anybuss_set_drvdata(client, priv);
193 static void profinet_remove(struct anybuss_client *client)
195 struct profi_priv *priv = anybuss_get_drvdata(client);
197 fieldbus_dev_unregister(&priv->fbdev);
200 static struct anybuss_client_driver profinet_driver = {
201 .probe = profinet_probe,
202 .remove = profinet_remove,
204 .name = "hms-profinet",
205 .owner = THIS_MODULE,
210 static int __init profinet_init(void)
212 return anybuss_client_driver_register(&profinet_driver);
214 module_init(profinet_init);
216 static void __exit profinet_exit(void)
218 return anybuss_client_driver_unregister(&profinet_driver);
220 module_exit(profinet_exit);
222 MODULE_AUTHOR("Sven Van Asbroeck <TheSven73@gmail.com>");
223 MODULE_DESCRIPTION("HMS Profinet IRT Driver (Anybus-S)");
224 MODULE_LICENSE("GPL v2");