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, "%02X:%02X:%02X:%02X:%02X:%02X\n",
70 response.addr[0], response.addr[1],
71 response.addr[2], response.addr[3],
72 response.addr[4], response.addr[5]);
75 static bool profi_enable_get(struct fieldbus_dev *fbdev)
77 struct profi_priv *priv = container_of(fbdev, struct profi_priv, fbdev);
80 mutex_lock(&priv->enable_lock);
81 power_on = priv->power_on;
82 mutex_unlock(&priv->enable_lock);
87 static int __profi_enable(struct profi_priv *priv)
90 struct anybuss_client *client = priv->client;
91 /* Initialization Sequence, Generic Anybus Mode */
92 const struct anybuss_memcfg mem_cfg = {
94 .input_dpram = PROFI_DPRAM_SIZE,
95 .input_total = PROFI_DPRAM_SIZE,
97 .output_dpram = PROFI_DPRAM_SIZE,
98 .output_total = PROFI_DPRAM_SIZE,
99 .offl_mode = FIELDBUS_DEV_OFFL_MODE_CLEAR,
103 * switch anybus off then on, this ensures we can do a complete
104 * configuration cycle in case anybus was already on.
106 anybuss_set_power(client, false);
107 ret = anybuss_set_power(client, true);
110 ret = anybuss_start_init(client, &mem_cfg);
113 ret = anybuss_finish_init(client);
116 priv->power_on = true;
120 anybuss_set_power(client, false);
121 priv->power_on = false;
125 static int __profi_disable(struct profi_priv *priv)
127 struct anybuss_client *client = priv->client;
129 anybuss_set_power(client, false);
130 priv->power_on = false;
134 static int profi_simple_enable(struct fieldbus_dev *fbdev, bool enable)
137 struct profi_priv *priv = container_of(fbdev, struct profi_priv, fbdev);
139 mutex_lock(&priv->enable_lock);
141 ret = __profi_enable(priv);
143 ret = __profi_disable(priv);
144 mutex_unlock(&priv->enable_lock);
149 static void profi_on_area_updated(struct anybuss_client *client)
151 struct profi_priv *priv = anybuss_get_drvdata(client);
153 fieldbus_dev_area_updated(&priv->fbdev);
156 static void profi_on_online_changed(struct anybuss_client *client, bool online)
158 struct profi_priv *priv = anybuss_get_drvdata(client);
160 fieldbus_dev_online_changed(&priv->fbdev, online);
163 static int profinet_probe(struct anybuss_client *client)
165 struct profi_priv *priv;
166 struct device *dev = &client->dev;
169 client->on_area_updated = profi_on_area_updated;
170 client->on_online_changed = profi_on_online_changed;
171 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
174 mutex_init(&priv->enable_lock);
175 priv->client = client;
176 priv->fbdev.read_area_sz = PROFI_DPRAM_SIZE;
177 priv->fbdev.write_area_sz = PROFI_DPRAM_SIZE;
178 priv->fbdev.card_name = "HMS Profinet IRT (Anybus-S)";
179 priv->fbdev.fieldbus_type = FIELDBUS_DEV_TYPE_PROFINET;
180 priv->fbdev.read_area = profi_read_area;
181 priv->fbdev.write_area = profi_write_area;
182 priv->fbdev.fieldbus_id_get = profi_id_get;
183 priv->fbdev.enable_get = profi_enable_get;
184 priv->fbdev.simple_enable_set = profi_simple_enable;
185 priv->fbdev.parent = dev;
186 err = fieldbus_dev_register(&priv->fbdev);
189 dev_info(dev, "card detected, registered as %s",
190 dev_name(priv->fbdev.dev));
191 anybuss_set_drvdata(client, priv);
196 static int profinet_remove(struct anybuss_client *client)
198 struct profi_priv *priv = anybuss_get_drvdata(client);
200 fieldbus_dev_unregister(&priv->fbdev);
204 static struct anybuss_client_driver profinet_driver = {
205 .probe = profinet_probe,
206 .remove = profinet_remove,
208 .name = "hms-profinet",
209 .owner = THIS_MODULE,
214 static int __init profinet_init(void)
216 return anybuss_client_driver_register(&profinet_driver);
218 module_init(profinet_init);
220 static void __exit profinet_exit(void)
222 return anybuss_client_driver_unregister(&profinet_driver);
224 module_exit(profinet_exit);
227 MODULE_DESCRIPTION("HMS Profinet IRT Driver (Anybus-S)");
228 MODULE_LICENSE("GPL v2");