-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcd_platform_driver_sysfs.c
290 lines (230 loc) · 8.61 KB
/
pcd_platform_driver_sysfs.c
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
/* -----------------------------------------------------------------------------
* Filename : pcd_platform_driver.c
* Description: A pseudo-character platform driver which handles N platform
* pcdevices
* Created : Thursday 7 August 2022 02:08:10 IST
* Version : 1.0
* Author : Harish Kumar, Embedded Linux Developer, harishec031@gmail.com
*
* Copyright (c) 2022 Harish Kumar
* ----------------------------------------------------------------------------- */
#include "pcd_platform_driver_sysfs.h"
struct pcdrv_private_data pcdrv_data;
struct device_config pcdev_config[] = {
[PCDEVA1X] = {.config_item1 = 60, .config_item2 = 21},
[PCDEVB1X] = {.config_item1 = 50, .config_item2 = 22},
[PCDEVC1X] = {.config_item1 = 40, .config_item2 = 23},
[PCDEVD1X] = {.config_item1 = 30, .config_item2 = 24},
};
/* file operations of the driver */
struct file_operations pcd_fops = {
.open = pcd_open,
.read = pcd_read,
.write = pcd_write,
.llseek = pcd_lseek,
.release = pcd_release,
.owner = THIS_MODULE
};
ssize_t
show_max_size (struct device *dev,
struct device_attribute *attr,
char *buf) {
int ret;
/* get access to the device private data */
struct pcdev_private_data *dev_data = dev_get_drvdata(dev->parent);
if(!dev_data) {
pr_info("null reference by dev_data, %p\n", dev_data);
return 0;
}
mutex_lock(&dev_data->pcd_lock);
ret = sprintf(buf, "%d\n", dev_data->pdata.size);
mutex_unlock(&dev_data->pcd_lock);
return ret;
}
ssize_t
show_serial_number (struct device *dev,
struct device_attribute *attr,
char *buf) {
int ret;
/* get access to the device private data */
struct pcdev_private_data *dev_data = dev_get_drvdata(dev->parent);
if(!dev_data) {
pr_info("null reference by dev_data, %p\n", dev_data);
return 0;
}
mutex_lock(&dev_data->pcd_lock);
ret = sprintf(buf, "%s\n", dev_data->pdata.serial_number);
mutex_unlock(&dev_data->pcd_lock);
return ret;
}
ssize_t
store_max_size (struct device *dev,
struct device_attribute *attr,
const char *buf,
size_t count) {
long result;
int ret;
struct pcdev_private_data *dev_data = dev_get_drvdata(dev->parent);
mutex_lock(&dev_data->pcd_lock);
ret = kstrtol(buf, 10, &result);
if(ret){
return ret;
}
dev_data->pdata.size = result;
dev_data->buffer = krealloc(dev_data->buffer, dev_data->pdata.size, GFP_KERNEL);
mutex_unlock(&dev_data->pcd_lock);
return count;
}
/* create 2 variables of struct device_attribute */
static DEVICE_ATTR(max_size, S_IRUGO|S_IWUSR, show_max_size, store_max_size);
static DEVICE_ATTR(serial_number, S_IRUGO, show_serial_number, NULL);
struct attribute *pcd_attrs[] = {
&dev_attr_max_size.attr,
&dev_attr_serial_number.attr,
NULL
};
struct attribute_group pcd_attr_group = {
.attrs = pcd_attrs
};
int pcd_sysfs_create_file(struct device *pcd_dev) {
#if 0
int ret = sysfs_create_file(&pcd_dev->kobj, &dev_attr_max_size.attr);
if(ret)
return ret;
return sysfs_create_file(&pcd_dev->kobj, &dev_attr_serial_number.attr);
#endif
return sysfs_create_group(&pcd_dev->kobj, &pcd_attr_group);
}
/* Gets called when matched platform device is found */
int pcd_platform_driver_probe (struct platform_device *pdev) {
u32 ret;
struct pcdev_private_data *dev_data;
struct pcdev_platform_data *pdata;
struct device *dev = &pdev->dev;
pr_info("A device is detected\n");
/* 1. Get the platform data */
pdata = (struct pcdev_platform_data *)dev_get_platdata(&pdev->dev);
if(!pdata) {
pr_info("No platform data available\n");
ret = -EINVAL;
return ret;
}
/* 2. Dynamically allocate memory for the device private data */
dev_data = devm_kzalloc(&pdev->dev, sizeof(*dev_data), GFP_KERNEL);
if(!dev_data) {
pr_info("Cannot allocate memory\n");
return -ENOMEM;
}
/* Save the device private data pointer in platform device structure */
dev_set_drvdata(&pdev->dev, dev_data);
dev_data->pdata.size = pdata->size;
dev_data->pdata.perm = pdata->perm;
dev_data->pdata.serial_number = pdata->serial_number;
pr_info("Device serial number = %s\n", dev_data->pdata.serial_number);
pr_info("Device size = %d\n", dev_data->pdata.size);
pr_info("Device permission = %d\n", dev_data->pdata.perm);
pr_info("Config item 1 = %d\n", pcdev_config[pdev->id_entry->driver_data].config_item1);
pr_info("Config item 2 = %d\n", pcdev_config[pdev->id_entry->driver_data].config_item2);
/* 3. Dynamically allocate memory for the device buffer using size
* information from the platform data */
dev_data->buffer = devm_kzalloc(&pdev->dev, dev_data->pdata.size, GFP_KERNEL);
if(!dev_data->buffer) {
pr_info("Cannot allocate memory\n");
return -ENOMEM;
}
/* 4. Get the device number */
dev_data->device_number = pcdrv_data.device_num_base + pdev->id;
/* 5. Do cdev init and cdev add */
cdev_init(&dev_data->pcd_cdev, &pcd_fops);
dev_data->pcd_cdev.owner = THIS_MODULE;
ret = cdev_add(&dev_data->pcd_cdev, dev_data->device_number, 1);
if (ret < 0) {
pr_err("cdev add failed\n");
return ret;
}
/* 6. Create device file for the detected platform device */
pcdrv_data.device_pcd = device_create(pcdrv_data.class_pcd, dev, dev_data->device_number, NULL, "pcdev-%d", pdev->id);
if(IS_ERR(pcdrv_data.device_pcd)) {
pr_err("device create failed\n");
ret = PTR_ERR(pcdrv_data.device_pcd);
cdev_del(&dev_data->pcd_cdev);
return ret;
}
pcdrv_data.total_devices++;
ret = pcd_sysfs_create_file(pcdrv_data.device_pcd);
if(ret) {
device_destroy(pcdrv_data.class_pcd, dev_data->device_number);
return ret;
}
pr_info("Probe was successful\n");
return 0;
}
/* Gets called when the device is removed from the system */
int pcd_platform_driver_remove (struct platform_device *pdev) {
struct pcdev_private_data *dev_data = dev_get_drvdata(&pdev->dev);
/* 1. Remove a device that was created with device_create() */
device_destroy(pcdrv_data.class_pcd, dev_data->device_number);
/* 2. Remove a cdev entry from the system */
cdev_del(&dev_data->pcd_cdev);
/* 3. Free the memory held by the device */
/* NOTE: will be taken care by the kernel
* since we've used device resource managed kernel api
*/
pcdrv_data.total_devices--;
pr_info("A device is removed\n");
return 0;
}
struct platform_device_id pcdevs_ids[] = {
[0] = {.name= "pcdev-A1x", .driver_data = PCDEVA1X},
[1] = {.name= "pcdev-B1x", .driver_data = PCDEVB1X},
[2] = {.name= "pcdev-C1x", .driver_data = PCDEVC1X},
[3] = {.name= "pcdev-D1x", .driver_data = PCDEVD1X},
{} /* NULL terminated */
};
struct platform_driver pcd_platform_driver = {
.probe = pcd_platform_driver_probe,
.remove = pcd_platform_driver_remove,
.id_table = pcdevs_ids,
.driver = {
.name = "pseudo-char-device"
}
};
#define MAX_DEVICES 10
static int __init pcd_platform_driver_init(void)
{
u32 ret;
/* 1. Dynamically allocate a device number for MAX_DEVICES */
ret = alloc_chrdev_region(&pcdrv_data.device_num_base, 0, MAX_DEVICES, "pcdevs");
if(ret < 0) {
pr_err("Alloc chrdev failed\n");
return ret;
}
/* 2. Create device class under /sys/class */
pcdrv_data.class_pcd = class_create(THIS_MODULE, "pcd_class");
if(IS_ERR(pcdrv_data.class_pcd)) {
pr_err("class creation failed\n");
ret = PTR_ERR(pcdrv_data.class_pcd);
unregister_chrdev_region(pcdrv_data.device_num_base, MAX_DEVICES);
return ret;
}
/* 3. Register a platform driver */
platform_driver_register(&pcd_platform_driver);
pr_info("pcd platform driver loaded\n");
return 0;
}
static void __exit pcd_platform_driver_cleanup(void)
{
/* 1. Unregister the platform driver */
platform_driver_unregister(&pcd_platform_driver);
/* 2. Class destroy */
class_destroy(pcdrv_data.class_pcd);
/* 3. Unregister device numbers for MAX_DEVICES */
unregister_chrdev_region(pcdrv_data.device_num_base, MAX_DEVICES);
pr_info("pcd platform driver unloaded\n");
}
module_init(pcd_platform_driver_init);
module_exit(pcd_platform_driver_cleanup);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("harish");
MODULE_DESCRIPTION("A pseudo-character platform driver which handles N platform pcdevices");
MODULE_INFO(board, "Beaglebone Black REV A5");