aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/layerscape/patches-5.4/701-net-0308-staging-fsl_ppfe-add-support-for-a-char-dev-for-link.patch
blob: c445952222b197c205e960cb91510a1ddf376ae3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
From a41d4af2ed4b02edca9d7f69955893d407274dc2 Mon Sep 17 00:00:00 2001
From: Shreyansh Jain <shreyansh.jain@nxp.com>
Date: Wed, 6 Jun 2018 14:19:34 +0530
Subject: [PATCH] staging: fsl_ppfe: add support for a char dev for link status

Read and IOCTL support is added. Application would need to open,
read/ioctl the /dev/pfe_us_cdev device.
select is pending as it requires a wait_queue.

Signed-off-by: Shreyansh Jain <shreyansh.jain@nxp.com>
Signed-off-by: Calvin Johnson <calvin.johnson@nxp.com>
---
 drivers/staging/fsl_ppfe/Makefile   |   3 +-
 drivers/staging/fsl_ppfe/pfe_cdev.c | 207 ++++++++++++++++++++++++++++++++++++
 drivers/staging/fsl_ppfe/pfe_cdev.h |  52 +++++++++
 drivers/staging/fsl_ppfe/pfe_eth.c  |  14 +++
 drivers/staging/fsl_ppfe/pfe_mod.c  |  14 +++
 5 files changed, 289 insertions(+), 1 deletion(-)
 create mode 100644 drivers/staging/fsl_ppfe/pfe_cdev.c
 create mode 100644 drivers/staging/fsl_ppfe/pfe_cdev.h

--- a/drivers/staging/fsl_ppfe/Makefile
+++ b/drivers/staging/fsl_ppfe/Makefile
@@ -16,4 +16,5 @@ pfe-y += pfe_mod.o \
 	pfe_sysfs.o \
 	pfe_debugfs.o \
 	pfe_ls1012a_platform.o \
-	pfe_hal.o
+	pfe_hal.o \
+	pfe_cdev.o
--- /dev/null
+++ b/drivers/staging/fsl_ppfe/pfe_cdev.c
@@ -0,0 +1,207 @@
+/*
+ * Copyright 2018 NXP
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/* @pfe_cdev.c.
+ *  Dummy device representing the PFE US in userspace.
+ *  - used for interacting with the kernel layer for link status
+ */
+
+#include "pfe_cdev.h"
+
+static int pfe_majno;
+static struct class *pfe_char_class;
+static struct device *pfe_char_dev;
+
+struct pfe_shared_info link_states[PFE_CDEV_ETH_COUNT];
+
+static int pfe_cdev_open(struct inode *inp, struct file *fp)
+{
+	pr_debug("PFE CDEV device opened.\n");
+	return 0;
+}
+
+static ssize_t pfe_cdev_read(struct file *fp, char *buf,
+			     size_t len, loff_t *off)
+{
+	int ret = 0;
+
+	pr_info("PFE CDEV attempt copying (%lu) size of user.\n",
+		sizeof(link_states));
+
+	pr_debug("Dump link_state on screen before copy_to_user\n");
+	for (; ret < PFE_CDEV_ETH_COUNT; ret++) {
+		pr_debug("%u  %u", link_states[ret].phy_id,
+			 link_states[ret].state);
+		pr_debug("\n");
+	}
+
+	/* Copy to user the value in buffer sized len */
+	ret = copy_to_user(buf, &link_states, sizeof(link_states));
+	if (ret != 0) {
+		pr_err("Failed to send (%d)bytes of (%lu) requested.\n",
+		       ret, len);
+		return -EFAULT;
+	}
+
+	/* offset set back to 0 as there is contextual reading offset */
+	*off = 0;
+	pr_debug("Read of (%lu) bytes performed.\n", sizeof(link_states));
+
+	return sizeof(link_states);
+}
+
+/**
+ * This function is for getting some commands from user through non-IOCTL
+ * channel. It can used to configure the device.
+ * TODO: To be filled in future, if require duplex communication with user
+ * space.
+ */
+static ssize_t pfe_cdev_write(struct file *fp, const char *buf,
+			      size_t len, loff_t *off)
+{
+	pr_info("PFE CDEV Write operation not supported!\n");
+
+	return -EFAULT;
+}
+
+static int pfe_cdev_release(struct inode *inp, struct file *fp)
+{
+	pr_info("PFE_CDEV: Device successfully closed\n");
+	return 0;
+}
+
+static long pfe_cdev_ioctl(struct file *fp, unsigned int cmd,
+			   unsigned long arg)
+{
+	int ret = -EFAULT;
+	int __user *argp = (int __user *)arg;
+
+	pr_debug("PFE CDEV IOCTL Called with cmd=(%u)\n", cmd);
+
+	switch (cmd) {
+	case PFE_CDEV_ETH0_STATE_GET:
+		/* Return an unsigned int (link state) for ETH0 */
+		*argp = link_states[0].state;
+		pr_debug("Returning state=%d for ETH0\n", *argp);
+		ret = 0;
+		break;
+	case PFE_CDEV_ETH1_STATE_GET:
+		/* Return an unsigned int (link state) for ETH0 */
+		*argp = link_states[1].state;
+		pr_debug("Returning state=%d for ETH1\n", *argp);
+		ret = 0;
+		break;
+	default:
+		pr_info("Unsupport cmd (%d) for PFE CDEV.\n", cmd);
+		break;
+	};
+
+	return ret;
+}
+
+static unsigned int pfe_cdev_poll(struct file *fp,
+				  struct poll_table_struct *wait)
+{
+	pr_info("PFE CDEV poll method not supported\n");
+	return 0;
+}
+
+static const struct file_operations pfe_cdev_fops = {
+	.open = pfe_cdev_open,
+	.read = pfe_cdev_read,
+	.write = pfe_cdev_write,
+	.release = pfe_cdev_release,
+	.unlocked_ioctl = pfe_cdev_ioctl,
+	.poll = pfe_cdev_poll,
+};
+
+int pfe_cdev_init(void)
+{
+	int ret;
+
+	pr_debug("PFE CDEV initialization begin\n");
+
+	/* Register the major number for the device */
+	pfe_majno = register_chrdev(0, PFE_CDEV_NAME, &pfe_cdev_fops);
+	if (pfe_majno < 0) {
+		pr_err("Unable to register PFE CDEV. PFE CDEV not available\n");
+		ret = pfe_majno;
+		goto cleanup;
+	}
+
+	pr_debug("PFE CDEV assigned major number: %d\n", pfe_majno);
+
+	/* Register the class for the device */
+	pfe_char_class = class_create(THIS_MODULE, PFE_CLASS_NAME);
+	if (IS_ERR(pfe_char_class)) {
+		pr_err(
+		"Failed to init class for PFE CDEV. PFE CDEV not available.\n");
+		goto cleanup;
+	}
+
+	pr_debug("PFE CDEV Class created successfully.\n");
+
+	/* Create the device without any parent and without any callback data */
+	    pfe_char_dev = device_create(pfe_char_class, NULL,
+					 MKDEV(pfe_majno, 0), NULL,
+					 PFE_CDEV_NAME);
+	if (IS_ERR(pfe_char_dev)) {
+		pr_err("Unable to PFE CDEV device. PFE CDEV not available.\n");
+		ret = PTR_ERR(pfe_char_dev);
+		goto cleanup;
+	}
+
+	/* Information structure being shared with the userspace */
+	memset(link_states, 0, sizeof(struct pfe_shared_info) *
+			PFE_CDEV_ETH_COUNT);
+
+	pr_info("PFE CDEV created: %s\n", PFE_CDEV_NAME);
+
+	ret = 0;
+	return ret;
+
+cleanup:
+	if (!IS_ERR(pfe_char_class))
+		class_destroy(pfe_char_class);
+
+	if (pfe_majno > 0)
+		unregister_chrdev(pfe_majno, PFE_CDEV_NAME);
+
+	ret = -EFAULT;
+	return ret;
+}
+
+void pfe_cdev_exit(void)
+{
+	if (!IS_ERR(pfe_char_dev))
+		device_destroy(pfe_char_class, MKDEV(pfe_majno, 0));
+
+	if (!IS_ERR(pfe_char_class)) {
+		class_unregister(pfe_char_class);
+		class_destroy(pfe_char_class);
+	}
+
+	if (pfe_majno > 0)
+		unregister_chrdev(pfe_majno, PFE_CDEV_NAME);
+
+	/* reset the variables */
+	pfe_majno = 0;
+	pfe_char_class = NULL;
+	pfe_char_dev = NULL;
+
+	pr_info("PFE CDEV Removed.\n");
+}
--- /dev/null
+++ b/drivers/staging/fsl_ppfe/pfe_cdev.h
@@ -0,0 +1,52 @@
+/*
+ * Copyright 2018 NXP
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _PFE_CDEV_H_
+#define _PFE_CDEV_H_
+
+#include <linux/init.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/kernel.h>
+#include <linux/fs.h>
+#include <linux/uaccess.h>
+#include <linux/poll.h>
+
+#define  PFE_CDEV_NAME "pfe_us_cdev"
+#define  PFE_CLASS_NAME  "ppfe_us"
+
+/* Extracted from ls1012a_pfe_platform_data, there are 3 interfaces which are
+ * supported by PFE driver. Should be updated if number of eth devices are
+ * changed.
+ */
+#define PFE_CDEV_ETH_COUNT 3
+
+struct pfe_shared_info {
+	uint32_t phy_id; /* Link phy ID */
+	uint8_t state;  /* Has either 0 or 1 */
+};
+
+extern struct pfe_shared_info link_states[PFE_CDEV_ETH_COUNT];
+
+/* IOCTL Commands */
+#define PFE_CDEV_ETH0_STATE_GET	0
+#define PFE_CDEV_ETH1_STATE_GET	1
+
+int pfe_cdev_init(void);
+void pfe_cdev_exit(void);
+
+#endif /* _PFE_CDEV_H_ */
--- a/drivers/staging/fsl_ppfe/pfe_eth.c
+++ b/drivers/staging/fsl_ppfe/pfe_eth.c
@@ -55,6 +55,7 @@
 
 #include "pfe_mod.h"
 #include "pfe_eth.h"
+#include "pfe_cdev.h"
 
 #define LS1012A_REV_1_0		0x87040010
 
@@ -1160,6 +1161,19 @@ static void pfe_eth_adjust_link(struct n
 		phy_print_status(phydev);
 
 	spin_unlock_irqrestore(&priv->lock, flags);
+
+	/* Now, dump the details to the cdev.
+	 * XXX: Locking would be required? (uniprocess arch)
+	 *      Or, maybe move it in spinlock above
+	 */
+	if (us && priv->einfo->gem_id < PFE_CDEV_ETH_COUNT) {
+		pr_debug("Changing link state from (%u) to (%u) for ID=(%u)\n",
+			 link_states[priv->einfo->gem_id].state,
+			 phydev->link,
+			 priv->einfo->gem_id);
+		link_states[priv->einfo->gem_id].phy_id = priv->einfo->gem_id;
+		link_states[priv->einfo->gem_id].state = phydev->link;
+	}
 }
 
 /* pfe_phy_exit
--- a/drivers/staging/fsl_ppfe/pfe_mod.c
+++ b/drivers/staging/fsl_ppfe/pfe_mod.c
@@ -18,6 +18,7 @@
 
 #include <linux/dma-mapping.h>
 #include "pfe_mod.h"
+#include "pfe_cdev.h"
 
 unsigned int us;
 module_param(us, uint, 0444);
@@ -92,8 +93,18 @@ firmware_init:
 	if (rc < 0)
 		goto err_debugfs;
 
+	if (us) {
+		/* Creating a character device */
+		rc = pfe_cdev_init();
+		if (rc < 0)
+			goto err_cdev;
+	}
+
 	return 0;
 
+err_cdev:
+	pfe_debugfs_exit(pfe);
+
 err_debugfs:
 	pfe_sysfs_exit(pfe);
 
@@ -129,6 +140,9 @@ int pfe_remove(struct pfe *pfe)
 {
 	pr_info("%s\n", __func__);
 
+	if (us)
+		pfe_cdev_exit();
+
 	pfe_debugfs_exit(pfe);
 
 	pfe_sysfs_exit(pfe);