Structured comments updated.
[lincan.git] / lincan / src / proc.c
1 /* proc.c
2  * Linux CAN-bus device driver.
3  * Written by Arnaud Westenberg email:arnaud@wanadoo.nl
4  * Rewritten for new CAN queues by Pavel Pisa - OCERA team member
5  * email:pisa@cmp.felk.cvut.cz
6  * This software is released under the GPL-License.
7  * Version lincan-0.2  9 Jul 2003
8  */ 
9
10 #include "../include/can.h"
11 #include "../include/can_sysdep.h"
12 #include "../include/main.h"
13 #include "../include/proc.h"
14 #include "../include/setup.h"
15
16 #define __NO_VERSION__
17 #include <linux/module.h>
18
19 int add_channel_to_procdir(struct candevice_t *candev);
20 int remove_channel_from_procdir(void);
21 int add_object_to_procdir(int chip_nr);
22 int remove_object_from_procdir(int chip_nr);
23
24 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,3,0))
25 static int can_proc_readlink(struct proc_dir_entry *ent, char *page);
26 #endif
27
28 static int cc=0; /* static counter for each CAN chip */
29
30 static struct canproc_t can_proc_base;
31 static struct canproc_t *base=&can_proc_base;
32
33 /* The following functions are needed only for kernel version 2.2. Kernel
34  * version 2.4 already defines them for us.
35  */
36 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,3,0))
37 static void can_fill_inode(struct inode *inode, int fill)
38 {
39         if (fill)
40                 MOD_INC_USE_COUNT;
41         else
42                 MOD_DEC_USE_COUNT;
43 }
44
45 static struct proc_dir_entry * can_create_proc_entry(const char *name, mode_t mode,
46                                         struct proc_dir_entry *parent)
47 {
48         struct proc_dir_entry *new_entry = NULL;
49         char *namestore;
50         int namelen;
51         
52         if(!name || !parent)
53                 return NULL;
54         namelen=strlen(name);
55         if(!namelen)
56                 return NULL;
57
58         new_entry = (struct proc_dir_entry *) 
59                         can_checked_malloc(sizeof(struct proc_dir_entry)+namelen+1);
60
61         if (new_entry == NULL)
62                 return NULL;
63
64         memset(new_entry, 0, sizeof(struct proc_dir_entry));
65
66         /* Store copy of the proc entry name */
67         namestore = ((char *) new_entry) + sizeof(struct proc_dir_entry);
68         memcpy(namestore, name, namelen + 1);
69
70         new_entry->low_ino = 0;
71         new_entry->namelen = namelen;
72         new_entry->name = namestore;
73         new_entry->mode = mode;
74         new_entry->nlink = 0;
75         new_entry->fill_inode = can_fill_inode;
76         new_entry->parent = parent;
77
78         proc_register(parent, new_entry);
79
80         return new_entry;
81 }
82
83 static int can_remove_proc_entry(struct proc_dir_entry *del, struct proc_dir_entry *parent)
84 {
85         if (del != NULL) {
86                 proc_unregister(parent, del->low_ino);
87                 can_checked_free(del);
88                 return 0;
89         }
90         else return -ENODEV;
91 }
92
93
94 static int can_proc_readlink(struct proc_dir_entry *ent, char *page)
95 {
96         char *link_dest = (char*)ent->data;
97         
98         strcpy(page, link_dest);
99         return strlen(link_dest);
100 }
101
102
103
104 /* This compatibility version of proc_symlink does not store local copy of destination */
105 static inline struct proc_dir_entry *can_proc_symlink(const char *name,
106                 struct proc_dir_entry *parent, const char *dest)
107 {
108         struct proc_dir_entry *entry;
109         
110         
111         entry = can_create_proc_entry(name, S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO, parent);
112         if (entry == NULL)
113                 return NULL;
114         entry->readlink_proc = can_proc_readlink;
115         entry->data = dest;
116         return entry;
117 }
118
119 #else /* Functions forwarded for kernel 2.4 and above */
120
121 static inline struct proc_dir_entry * can_create_proc_entry(const char *name, mode_t mode,
122                                         struct proc_dir_entry *parent)
123 {
124         return create_proc_entry(name, mode, parent);
125 }
126
127
128 /* This does not fully follow linux 2.4 and 2.6 prototype to simplify 2.2.x compatibility */
129 /* The newer kernels use entry name instead of pointer to the entry */
130 static int can_remove_proc_entry(struct proc_dir_entry *del, struct proc_dir_entry *parent)
131 {
132         if(!del) return -ENODEV;
133         remove_proc_entry(del->name,parent);
134         return 0;
135 }
136
137 static inline struct proc_dir_entry *can_proc_symlink(const char *name,
138                 struct proc_dir_entry *parent, const char *dest)
139 {
140         return proc_symlink(name, parent, dest);
141 }
142
143 #endif /* Functions required for kernel 2.2 */
144
145 /* can_init_procdir registers the entire CAN directory tree recursively at
146  * the proc system.
147  */
148 int can_init_procdir(void)
149 {
150         int board;
151         struct candevice_t *candev;
152         base->can_proc_entry = can_create_proc_entry("can", S_IFDIR | S_IRUGO | 
153                                         S_IXUGO, &proc_root);
154         if (base->can_proc_entry == NULL)
155                 return -ENODEV;
156
157         for (board=0; board<hardware_p->nr_boards; board++) {
158                 candev=hardware_p->candevice[board];
159                 if(candev) add_channel_to_procdir(candev);
160         } 
161
162         return 0;
163 }
164
165 /* can_delete_procdir removes the entire CAN tree from the proc system */
166 int can_delete_procdir(void)
167 {
168         if (remove_channel_from_procdir()) 
169                 return -ENODEV;
170         /* name: "can" */
171         if (can_remove_proc_entry(base->can_proc_entry, &proc_root)) 
172                 return -ENODEV;
173
174         return 0;
175 }
176
177 int add_channel_to_procdir(struct candevice_t *candev)
178 {
179         int i=0;
180
181         for (i=0; i < candev->nr_all_chips; i++) {
182
183                 base->channel[cc] = (struct channelproc_t *)
184                         can_checked_malloc(sizeof(struct channelproc_t));
185                 if (base->channel[cc] == NULL)
186                         return -ENOMEM;
187
188                 sprintf(base->channel[cc]->ch_name, "channel%d",cc);
189                                                 
190                 base->channel[cc]->ch_entry = can_create_proc_entry(
191                                                 base->channel[cc]->ch_name,
192                                                 S_IFDIR | S_IRUGO |S_IXUGO,
193                                                 base->can_proc_entry);
194
195                 if (base->channel[cc]->ch_entry == NULL)
196                         return -ENODEV;
197
198                 add_object_to_procdir(cc);
199
200                 cc++;
201         } 
202
203         return 0;
204 }
205
206 int remove_channel_from_procdir(void)
207 {
208         
209         while (cc != 0) {
210                 cc--;
211                 
212                 if(!base->channel[cc]) continue;
213                 
214                 if (remove_object_from_procdir(cc))
215                         return -ENODEV; 
216                         
217                 /* name: base->channel[cc]->ch_name */
218                 if (can_remove_proc_entry(base->channel[cc]->ch_entry,
219                                                         base->can_proc_entry))
220                         return -ENODEV;
221                         
222                 can_checked_free(base->channel[cc]);
223                 base->channel[cc] = NULL;
224         }
225
226         return 0;
227 }
228
229
230 int add_object_to_procdir(int chip_nr)
231 {
232         int i, max_objects;
233
234         max_objects=chips_p[chip_nr]->max_objects;
235
236         for (i=0; i<max_objects; i++) {
237                 base->channel[chip_nr]->object[i] = (struct objectproc_t *)
238                         can_checked_malloc(sizeof(struct objectproc_t));
239
240                 if (base->channel[chip_nr]->object[i] == NULL)
241                         return -ENOMEM;
242
243                 sprintf(base->channel[chip_nr]->object[i]->obj_name,"object%d",i);
244                 sprintf(base->channel[chip_nr]->object[i]->lnk_name,"dev");
245                                                                 
246                 base->channel[chip_nr]->object[i]->obj_entry = can_create_proc_entry(
247                                 base->channel[chip_nr]->object[i]->obj_name,
248                                 S_IFDIR | S_IRUGO | S_IXUGO,
249                                 base->channel[chip_nr]->ch_entry);
250                 if (base->channel[chip_nr]->object[i]->obj_entry == NULL)
251                         return -ENODEV;
252
253                 sprintf(base->channel[chip_nr]->object[i]->lnk_dev,"/dev/can%d",
254                         chips_p[chip_nr]->msgobj[i]->minor);
255
256                 base->channel[chip_nr]->object[i]->lnk = can_proc_symlink(
257                                 base->channel[chip_nr]->object[i]->lnk_name,
258                                 base->channel[chip_nr]->object[i]->obj_entry,
259                                 base->channel[chip_nr]->object[i]->lnk_dev);
260                 if (base->channel[chip_nr]->object[i]->lnk == NULL)
261                         return -ENODEV;
262
263         }
264         return 0;
265
266
267 int remove_object_from_procdir(int chip_nr)
268 {
269         int i=0, obj=0;
270
271         obj=chips_p[chip_nr]->max_objects;
272
273         for (i=0; i<obj; i++) {
274                 if(!base->channel[chip_nr]->object[i]) continue;
275                 
276                 /* name: base->channel[chip_nr]->object[i]->lnk_name */
277                 if (can_remove_proc_entry( base->channel[chip_nr]->object[i]->lnk,
278                                 base->channel[chip_nr]->object[i]->obj_entry))  
279                         return -ENODEV;
280                 /* name: base->channel[chip_nr]->object[i]->obj_name */
281                 if (can_remove_proc_entry(
282                                 base->channel[chip_nr]->object[i]->obj_entry,
283                                 base->channel[chip_nr]->ch_entry))
284                         return -ENODEV;
285
286                 can_checked_free(base->channel[chip_nr]->object[i]);
287
288                 base->channel[chip_nr]->object[i]=NULL;
289         }
290         return 0;
291 }
292