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.3 17 Jun 2004
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"
16 #define __NO_VERSION__
17 #include <linux/module.h>
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);
24 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,3,0))
25 static int can_proc_readlink(struct proc_dir_entry *ent, char *page);
28 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,25)
29 #define CAN_PROC_ROOT (&proc_root)
31 #define CAN_PROC_ROOT (NULL)
32 #endif /* >= 2.6.26 */
34 static int cc=0; /* static counter for each CAN chip */
36 static struct canproc_t can_proc_base;
37 static struct canproc_t *base=&can_proc_base;
39 /* The following functions are needed only for kernel version 2.2. Kernel
40 * version 2.4 already defines them for us.
42 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,3,0))
43 static void can_fill_inode(struct inode *inode, int fill)
51 static struct proc_dir_entry * can_create_proc_entry(const char *name, mode_t mode,
52 struct proc_dir_entry *parent)
54 struct proc_dir_entry *new_entry = NULL;
64 new_entry = (struct proc_dir_entry *)
65 can_checked_malloc(sizeof(struct proc_dir_entry)+namelen+1);
67 if (new_entry == NULL)
70 memset(new_entry, 0, sizeof(struct proc_dir_entry));
72 /* Store copy of the proc entry name */
73 namestore = ((char *) new_entry) + sizeof(struct proc_dir_entry);
74 memcpy(namestore, name, namelen + 1);
76 new_entry->low_ino = 0;
77 new_entry->namelen = namelen;
78 new_entry->name = namestore;
79 new_entry->mode = mode;
81 new_entry->fill_inode = can_fill_inode;
82 new_entry->parent = parent;
84 proc_register(parent, new_entry);
89 static int can_remove_proc_entry(struct proc_dir_entry *del, struct proc_dir_entry *parent)
92 proc_unregister(parent, del->low_ino);
93 can_checked_free(del);
100 static int can_proc_readlink(struct proc_dir_entry *ent, char *page)
102 char *link_dest = (char*)ent->data;
104 strcpy(page, link_dest);
105 return strlen(link_dest);
110 /* This compatibility version of proc_symlink does not store local copy of destination */
111 static inline struct proc_dir_entry *can_proc_symlink(const char *name,
112 struct proc_dir_entry *parent, const char *dest)
114 struct proc_dir_entry *entry;
117 entry = can_create_proc_entry(name, S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO, parent);
120 entry->readlink_proc = can_proc_readlink;
125 #else /* Functions forwarded for kernel 2.4 and above */
127 static inline struct proc_dir_entry * can_create_proc_entry(const char *name, mode_t mode,
128 struct proc_dir_entry *parent)
130 return create_proc_entry(name, mode, parent);
134 /* This does not fully follow linux 2.4 and 2.6 prototype to simplify 2.2.x compatibility */
135 /* The newer kernels use entry name instead of pointer to the entry */
136 static int can_remove_proc_entry(struct proc_dir_entry *del, struct proc_dir_entry *parent)
138 if(!del) return -ENODEV;
139 remove_proc_entry(del->name,parent);
143 static inline struct proc_dir_entry *can_proc_symlink(const char *name,
144 struct proc_dir_entry *parent, const char *dest)
146 return proc_symlink(name, parent, dest);
149 #endif /* Functions required for kernel 2.2 */
151 /* can_init_procdir registers the entire CAN directory tree recursively at
154 int can_init_procdir(void)
157 struct candevice_t *candev;
158 base->can_proc_entry = can_create_proc_entry("can", S_IFDIR | S_IRUGO |
159 S_IXUGO, CAN_PROC_ROOT);
160 if (base->can_proc_entry == NULL)
163 for (board=0; board<hardware_p->nr_boards; board++) {
164 candev=hardware_p->candevice[board];
165 if(candev) add_channel_to_procdir(candev);
171 /* can_delete_procdir removes the entire CAN tree from the proc system */
172 int can_delete_procdir(void)
174 if (remove_channel_from_procdir())
177 if (can_remove_proc_entry(base->can_proc_entry, CAN_PROC_ROOT))
183 static int can_chip_procinfo(char *buf, char **start, off_t offset,
184 int count, int *eof, void *data)
186 struct canchip_t *chip=data;
189 /* Generic chip info */
190 len += sprintf(buf+len,"type : %s\n",chip->chip_type);
191 len += sprintf(buf+len,"index : %d\n",chip->chip_idx);
192 len += sprintf(buf+len,"irq : %d\n",chip->chip_irq);
193 len += sprintf(buf+len,"addr : %lu\n",
194 can_ioptr2ulong(chip->chip_base_addr));
195 len += sprintf(buf+len,"config : %s\n",
196 (chip->flags & CHIP_CONFIGURED) ? "yes":"no");
197 len += sprintf(buf+len,"clock : %ld Hz\n",chip->clock);
198 len += sprintf(buf+len,"baud : %ld\n",chip->baudrate);
199 len += sprintf(buf+len,"num obj : %d\n",chip->max_objects);
203 /* Chip specific info if available */
204 if(chip->chipspecops->get_info)
205 len += (chip->chipspecops->get_info)(chip,buf+len);
213 int add_channel_to_procdir(struct candevice_t *candev)
217 for (i=0; i < candev->nr_all_chips; i++) {
219 base->channel[cc] = (struct channelproc_t *)
220 can_checked_malloc(sizeof(struct channelproc_t));
221 if (base->channel[cc] == NULL)
224 sprintf(base->channel[cc]->ch_name, "channel%d",cc);
226 base->channel[cc]->ch_entry = can_create_proc_entry(
227 base->channel[cc]->ch_name,
228 S_IFDIR | S_IRUGO |S_IXUGO,
229 base->can_proc_entry);
231 if (base->channel[cc]->ch_entry == NULL)
234 add_object_to_procdir(cc);
236 create_proc_read_entry("chip_info", /* proc entry name */
237 0, /* protection mask, 0->default */
238 base->channel[cc]->ch_entry, /* parent dir, NULL->/proc */
248 int remove_channel_from_procdir(void)
254 if(!base->channel[cc]) continue;
256 remove_proc_entry("chip_info", base->channel[cc]->ch_entry);
258 if (remove_object_from_procdir(cc))
261 /* name: base->channel[cc]->ch_name */
262 if (can_remove_proc_entry(base->channel[cc]->ch_entry,
263 base->can_proc_entry))
266 can_checked_free(base->channel[cc]);
267 base->channel[cc] = NULL;
274 int add_object_to_procdir(int chip_nr)
278 max_objects=chips_p[chip_nr]->max_objects;
280 for (i=0; i<max_objects; i++) {
281 base->channel[chip_nr]->object[i] = (struct objectproc_t *)
282 can_checked_malloc(sizeof(struct objectproc_t));
284 if (base->channel[chip_nr]->object[i] == NULL)
287 sprintf(base->channel[chip_nr]->object[i]->obj_name,"object%d",i);
288 sprintf(base->channel[chip_nr]->object[i]->lnk_name,"dev");
290 base->channel[chip_nr]->object[i]->obj_entry = can_create_proc_entry(
291 base->channel[chip_nr]->object[i]->obj_name,
292 S_IFDIR | S_IRUGO | S_IXUGO,
293 base->channel[chip_nr]->ch_entry);
294 if (base->channel[chip_nr]->object[i]->obj_entry == NULL)
297 sprintf(base->channel[chip_nr]->object[i]->lnk_dev,"/dev/can%d",
298 chips_p[chip_nr]->msgobj[i]->minor);
300 base->channel[chip_nr]->object[i]->lnk = can_proc_symlink(
301 base->channel[chip_nr]->object[i]->lnk_name,
302 base->channel[chip_nr]->object[i]->obj_entry,
303 base->channel[chip_nr]->object[i]->lnk_dev);
304 if (base->channel[chip_nr]->object[i]->lnk == NULL)
311 int remove_object_from_procdir(int chip_nr)
315 obj=chips_p[chip_nr]->max_objects;
317 for (i=0; i<obj; i++) {
318 if(!base->channel[chip_nr]->object[i]) continue;
320 /* name: base->channel[chip_nr]->object[i]->lnk_name */
321 if (can_remove_proc_entry( base->channel[chip_nr]->object[i]->lnk,
322 base->channel[chip_nr]->object[i]->obj_entry))
324 /* name: base->channel[chip_nr]->object[i]->obj_name */
325 if (can_remove_proc_entry(
326 base->channel[chip_nr]->object[i]->obj_entry,
327 base->channel[chip_nr]->ch_entry))
330 can_checked_free(base->channel[chip_nr]->object[i]);
332 base->channel[chip_nr]->object[i]=NULL;