1 /* Copyright (C) 2010 - 2013 UNISYS CORPORATION
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or (at
7 * your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
12 * NON INFRINGEMENT. See the GNU General Public License for more
16 /** @file *********************************************************************
18 * Handle procfs-specific tasks.
19 * Note that this file does not know about any module-specific things, nor
20 * does it know anything about what information to reveal as part of the proc
21 * entries. The 2 functions that take care of displaying device and
22 * driver specific information are passed as parameters to
23 * visor_easyproc_InitDriver().
25 * void show_device_info(struct seq_file *seq, void *p);
26 * void show_driver_info(struct seq_file *seq);
28 * The second parameter to show_device_info is actually a pointer to the
29 * device-specific info to show. It is the context that was originally
30 * passed to visor_easyproc_InitDevice().
32 ******************************************************************************
35 #include <linux/proc_fs.h>
41 #define MYDRVNAME "easyproc"
46 * /proc/<ProcId> ProcDir
47 * /proc/<ProcId>/driver ProcDriverDir
48 * /proc/<ProcId>/driver/diag ProcDriverDiagFile
49 * /proc/<ProcId>/device ProcDeviceDir
50 * /proc/<ProcId>/device/0 procDevicexDir
51 * /proc/<ProcId>/device/0/diag procDevicexDiagFile
55 static ssize_t proc_write_device(struct file *file, const char __user *buffer,
56 size_t count, loff_t *ppos);
57 static ssize_t proc_write_driver(struct file *file, const char __user *buffer,
58 size_t count, loff_t *ppos);
60 static struct proc_dir_entry *
61 createProcDir(char *name, struct proc_dir_entry *parent)
63 struct proc_dir_entry *p = proc_mkdir_mode(name, S_IFDIR, parent);
66 ERRDRV("failed to create /proc directory %s", name);
70 static int seq_show_driver(struct seq_file *seq, void *offset);
71 static int proc_open_driver(struct inode *inode, struct file *file)
73 return single_open(file, seq_show_driver, PDE_DATA(inode));
75 static const struct file_operations proc_fops_driver = {
76 .open = proc_open_driver,
78 .write = proc_write_driver,
80 .release = single_release,
83 static int seq_show_device(struct seq_file *seq, void *offset);
84 static int seq_show_device_property(struct seq_file *seq, void *offset);
85 static int proc_open_device(struct inode *inode, struct file *file)
87 return single_open(file, seq_show_device, PDE_DATA(inode));
89 static const struct file_operations proc_fops_device = {
90 .open = proc_open_device,
92 .write = proc_write_device,
94 .release = single_release,
96 static int proc_open_device_property(struct inode *inode, struct file *file)
98 return single_open(file, seq_show_device_property, PDE_DATA(inode));
100 static const struct file_operations proc_fops_device_property = {
101 .open = proc_open_device_property,
104 .release = single_release,
109 void visor_easyproc_InitDriver(struct easyproc_driver_info *pdriver,
111 void (*show_driver_info)(struct seq_file *),
112 void (*show_device_info)(struct seq_file *,
115 memset(pdriver, 0, sizeof(struct easyproc_driver_info));
116 pdriver->ProcId = procId;
117 if (pdriver->ProcId == NULL)
118 ERRDRV("ProcId cannot be NULL (trouble ahead)!");
119 pdriver->Show_driver_info = show_driver_info;
120 pdriver->Show_device_info = show_device_info;
121 if (pdriver->ProcDir == NULL)
122 pdriver->ProcDir = createProcDir(pdriver->ProcId, NULL);
123 if ((pdriver->ProcDir != NULL) && (pdriver->ProcDriverDir == NULL))
124 pdriver->ProcDriverDir = createProcDir("driver",
126 if ((pdriver->ProcDir != NULL) && (pdriver->ProcDeviceDir == NULL))
127 pdriver->ProcDeviceDir = createProcDir("device",
129 if ((pdriver->ProcDriverDir != NULL) &&
130 (pdriver->ProcDriverDiagFile == NULL)) {
131 pdriver->ProcDriverDiagFile =
132 proc_create_data("diag", 0,
133 pdriver->ProcDriverDir,
134 &proc_fops_driver, pdriver);
135 if (pdriver->ProcDriverDiagFile == NULL)
136 ERRDRV("failed to register /proc/%s/driver/diag entry",
140 EXPORT_SYMBOL_GPL(visor_easyproc_InitDriver);
144 void visor_easyproc_InitDriverEx(struct easyproc_driver_info *pdriver,
146 void (*show_driver_info)(struct seq_file *),
147 void (*show_device_info)(struct seq_file *,
149 void (*write_driver_info)(char *buf,
152 void (*write_device_info)(char *buf,
157 visor_easyproc_InitDriver(pdriver, procId,
158 show_driver_info, show_device_info);
159 pdriver->Write_driver_info = write_driver_info;
160 pdriver->Write_device_info = write_device_info;
162 EXPORT_SYMBOL_GPL(visor_easyproc_InitDriverEx);
166 void visor_easyproc_DeInitDriver(struct easyproc_driver_info *pdriver)
168 if (pdriver->ProcDriverDiagFile != NULL) {
169 remove_proc_entry("diag", pdriver->ProcDriverDir);
170 pdriver->ProcDriverDiagFile = NULL;
172 if (pdriver->ProcDriverDir != NULL) {
173 remove_proc_entry("driver", pdriver->ProcDir);
174 pdriver->ProcDriverDir = NULL;
176 if (pdriver->ProcDeviceDir != NULL) {
177 remove_proc_entry("device", pdriver->ProcDir);
178 pdriver->ProcDeviceDir = NULL;
180 if (pdriver->ProcDir != NULL) {
181 remove_proc_entry(pdriver->ProcId, NULL);
182 pdriver->ProcDir = NULL;
184 pdriver->ProcId = NULL;
185 pdriver->Show_driver_info = NULL;
186 pdriver->Show_device_info = NULL;
187 pdriver->Write_driver_info = NULL;
188 pdriver->Write_device_info = NULL;
190 EXPORT_SYMBOL_GPL(visor_easyproc_DeInitDriver);
194 void visor_easyproc_InitDevice(struct easyproc_driver_info *pdriver,
195 struct easyproc_device_info *p, int devno,
198 if ((pdriver->ProcDeviceDir != NULL) && (p->procDevicexDir == NULL)) {
201 sprintf(s, "%d", devno);
202 p->procDevicexDir = createProcDir(s, pdriver->ProcDeviceDir);
205 p->devdata = devdata;
206 p->pdriver = pdriver;
208 if ((p->procDevicexDir != NULL) && (p->procDevicexDiagFile == NULL)) {
209 p->procDevicexDiagFile =
210 proc_create_data("diag", 0, p->procDevicexDir,
211 &proc_fops_device, p);
212 if (p->procDevicexDiagFile == NULL)
213 ERRDEVX(devno, "failed to register /proc/%s/device/%d/diag entry",
214 pdriver->ProcId, devno
217 memset(&(p->device_property_info[0]), 0,
218 sizeof(p->device_property_info));
220 EXPORT_SYMBOL_GPL(visor_easyproc_InitDevice);
224 void visor_easyproc_CreateDeviceProperty(struct easyproc_device_info *p,
225 void (*show_property_info)
226 (struct seq_file *, void *),
230 struct easyproc_device_property_info *px = NULL;
232 if (p->procDevicexDir == NULL) {
233 ERRDRV("state error");
236 for (i = 0; i < ARRAY_SIZE(p->device_property_info); i++) {
237 if (p->device_property_info[i].procEntry == NULL) {
238 px = &(p->device_property_info[i]);
243 ERRDEVX(p->devno, "too many device properties");
246 px->devdata = p->devdata;
247 px->pdriver = p->pdriver;
248 px->procEntry = proc_create_data(property_name, 0, p->procDevicexDir,
249 &proc_fops_device_property, px);
250 if (strlen(property_name)+1 > sizeof(px->property_name)) {
251 ERRDEVX(p->devno, "device property name %s too long",
255 strcpy(px->property_name, property_name);
256 if (px->procEntry == NULL) {
258 "failed to register /proc/%s/device/%d/%s entry",
259 p->pdriver->ProcId, p->devno, property_name);
262 px->show_device_property_info = show_property_info;
264 EXPORT_SYMBOL_GPL(visor_easyproc_CreateDeviceProperty);
268 void visor_easyproc_DeInitDevice(struct easyproc_driver_info *pdriver,
269 struct easyproc_device_info *p, int devno)
273 for (i = 0; i < ARRAY_SIZE(p->device_property_info); i++) {
274 if (p->device_property_info[i].procEntry != NULL) {
275 struct easyproc_device_property_info *px =
276 &(p->device_property_info[i]);
277 remove_proc_entry(px->property_name, p->procDevicexDir);
278 px->procEntry = NULL;
281 if (p->procDevicexDiagFile != NULL) {
282 remove_proc_entry("diag", p->procDevicexDir);
283 p->procDevicexDiagFile = NULL;
285 if (p->procDevicexDir != NULL) {
288 sprintf(s, "%d", devno);
289 remove_proc_entry(s, pdriver->ProcDeviceDir);
290 p->procDevicexDir = NULL;
295 EXPORT_SYMBOL_GPL(visor_easyproc_DeInitDevice);
299 static int seq_show_driver(struct seq_file *seq, void *offset)
301 struct easyproc_driver_info *p =
302 (struct easyproc_driver_info *)(seq->private);
305 (*(p->Show_driver_info))(seq);
311 static int seq_show_device(struct seq_file *seq, void *offset)
313 struct easyproc_device_info *p =
314 (struct easyproc_device_info *)(seq->private);
315 if ((!p) || (!(p->pdriver)))
317 (*(p->pdriver->Show_device_info))(seq, p->devdata);
323 static int seq_show_device_property(struct seq_file *seq, void *offset)
325 struct easyproc_device_property_info *p =
326 (struct easyproc_device_property_info *)(seq->private);
327 if ((!p) || (!(p->show_device_property_info)))
329 (*(p->show_device_property_info))(seq, p->devdata);
335 static ssize_t proc_write_driver(struct file *file, const char __user *buffer,
336 size_t count, loff_t *ppos)
338 struct seq_file *seq = (struct seq_file *)file->private_data;
339 struct easyproc_driver_info *p = NULL;
344 p = (struct easyproc_driver_info *)(seq->private);
345 if ((!p) || (!(p->Write_driver_info)))
347 if (count >= sizeof(local_buf))
349 if (copy_from_user(local_buf, buffer, count))
351 local_buf[count] = '\0'; /* be friendly */
352 (*(p->Write_driver_info))(local_buf, count, ppos);
358 static ssize_t proc_write_device(struct file *file, const char __user *buffer,
359 size_t count, loff_t *ppos)
361 struct seq_file *seq = (struct seq_file *)file->private_data;
362 struct easyproc_device_info *p = NULL;
367 p = (struct easyproc_device_info *)(seq->private);
368 if ((!p) || (!(p->pdriver)) || (!(p->pdriver->Write_device_info)))
370 if (count >= sizeof(local_buf))
372 if (copy_from_user(local_buf, buffer, count))
374 local_buf[count] = '\0'; /* be friendly */
375 (*(p->pdriver->Write_device_info))(local_buf, count, ppos, p->devdata);