hc
2024-11-01 2f529f9b558ca1c1bd74be7437a84e4711743404
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
/*
 * Copyright (C) 2006 Wolfgang Grandegger <wg@grandegger.com>
 *
 * Derived from RTnet project file stack/rtdev.c:
 *
 * Copyright (C) 1999       Lineo, Inc
 *               1999, 2002 David A. Schleef <ds@schleef.org>
 *               2002       Ulrich Marx <marx@kammer.uni-hannover.de>
 *               2003-2005  Jan Kiszka <jan.kiszka@web.de>
 *
 * 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, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
 
#include <linux/spinlock.h>
#include <linux/if.h>
#include <linux/if_arp.h>
#include <linux/netdevice.h>
#include <linux/module.h>
 
#include "rtcan_internal.h"
#include "rtcan_dev.h"
 
 
static struct rtcan_device *rtcan_devices[RTCAN_MAX_DEVICES];
static DEFINE_RTDM_LOCK(rtcan_devices_rt_lock);
 
DEFINE_SEMAPHORE(rtcan_devices_nrt_lock);
 
/* Spinlock for all reception lists and also for some members in
 * struct rtcan_socket */
DEFINE_RTDM_LOCK(rtcan_socket_lock);
 
/* Spinlock for all reception lists and also for some members in
 * struct rtcan_socket */
DEFINE_RTDM_LOCK(rtcan_recv_list_lock);
 
static inline struct rtcan_device *__rtcan_dev_get_by_name(const char *name)
{
    int i;
    struct rtcan_device *dev;
 
 
    for (i = 0; i < RTCAN_MAX_DEVICES; i++) {
   dev = rtcan_devices[i];
   if ((dev != NULL) && (strncmp(dev->name, name, IFNAMSIZ) == 0))
       return dev;
    }
    return NULL;
}
 
 
struct rtcan_device *rtcan_dev_get_by_name(const char *name)
{
    struct rtcan_device *dev;
#ifdef RTCAN_USE_REFCOUNT
    rtdm_lockctx_t context;
#endif
 
 
#ifdef RTCAN_USE_REFCOUNT
    rtdm_lock_get_irqsave(&rtcan_devices_rt_lock, context);
#endif
 
    dev = __rtcan_dev_get_by_name(name);
 
#ifdef RTCAN_USE_REFCOUNT
    if (dev != NULL)
   atomic_inc(&dev->refcount);
    rtdm_lock_put_irqrestore(&rtcan_devices_rt_lock, context);
#endif
 
    return dev;
}
 
 
static inline struct rtcan_device *__rtcan_dev_get_by_index(int ifindex)
{
    return rtcan_devices[ifindex - 1];
}
 
 
struct rtcan_device *rtcan_dev_get_by_index(int ifindex)
{
    struct rtcan_device *dev;
#ifdef RTCAN_USE_REFCOUNT
    rtdm_lockctx_t context;
#endif
 
 
    if ((ifindex <= 0) || (ifindex > RTCAN_MAX_DEVICES))
   return NULL;
 
#ifdef RTCAN_USE_REFCOUNT
    rtdm_lock_get_irqsave(&rtcan_devices_rt_lock, context);
#endif
 
    dev = __rtcan_dev_get_by_index(ifindex);
 
#ifdef RTCAN_USE_REFCOUNT
    if (dev != NULL)
   atomic_inc(&dev->refcount);
    rtdm_lock_put_irqrestore(&rtcan_devices_rt_lock, context);
#endif
 
    return dev;
}
 
 
void rtcan_dev_alloc_name(struct rtcan_device *dev, const char *mask)
{
    char buf[IFNAMSIZ];
    struct rtcan_device *tmp;
    int i;
 
 
    for (i = 0; i < RTCAN_MAX_DEVICES; i++) {
   ksformat(buf, IFNAMSIZ, mask, i);
   if ((tmp = rtcan_dev_get_by_name(buf)) == NULL) {
       strncpy(dev->name, buf, IFNAMSIZ);
       break;
   }
#ifdef RTCAN_USE_REFCOUNT
   else
       rtcan_dev_dereference(tmp);
#endif
    }
}
 
 
struct rtcan_device *rtcan_dev_alloc(int sizeof_priv, int sizeof_board_priv)
{
    struct rtcan_device *dev;
    struct rtcan_recv *recv_list_elem;
    int alloc_size;
    int j;
 
 
    alloc_size = sizeof(*dev) + sizeof_priv + sizeof_board_priv;
 
    dev = (struct rtcan_device *)kmalloc(alloc_size, GFP_KERNEL);
    if (dev == NULL) {
   printk(KERN_ERR "rtcan: cannot allocate rtcan device\n");
   return NULL;
    }
 
    memset(dev, 0, alloc_size);
 
    sema_init(&dev->nrt_lock, 1);
 
    rtdm_lock_init(&dev->device_lock);
 
    /* Init TX Semaphore, will be destroyed forthwith
     * when setting stop mode */
    rtdm_sem_init(&dev->tx_sem, 0);
#ifdef RTCAN_USE_REFCOUNT
    atomic_set(&dev->refcount, 0);
#endif
 
    /* Initialize receive list */
    dev->empty_list = recv_list_elem = dev->receivers;
    for (j = 0; j < RTCAN_MAX_RECEIVERS - 1; j++, recv_list_elem++)
   recv_list_elem->next = recv_list_elem + 1;
    recv_list_elem->next = NULL;
    dev->free_entries = RTCAN_MAX_RECEIVERS;
 
    if (sizeof_priv)
   dev->priv = (void *)((unsigned long)dev + sizeof(*dev));
    if (sizeof_board_priv)
   dev->board_priv = (void *)((unsigned long)dev + sizeof(*dev) + sizeof_priv);
 
    return dev;
}
 
void rtcan_dev_free (struct rtcan_device *dev)
{
    if (dev != NULL) {
   rtdm_sem_destroy(&dev->tx_sem);
   kfree(dev);
    }
}
 
 
static inline int __rtcan_dev_new_index(void)
{
    int i;
 
 
    for (i = 0; i < RTCAN_MAX_DEVICES; i++)
   if (rtcan_devices[i] == NULL)
        return i+1;
 
    return -ENOMEM;
}
 
 
int rtcan_dev_register(struct rtcan_device *dev)
{
    rtdm_lockctx_t context;
    int ret;
 
    down(&rtcan_devices_nrt_lock);
 
    if ((ret = __rtcan_dev_new_index()) < 0) {
   up(&rtcan_devices_nrt_lock);
   return ret;
    }
    dev->ifindex = ret;
 
    if (strchr(dev->name,'%') != NULL)
   rtcan_dev_alloc_name(dev, dev->name);
 
    if (__rtcan_dev_get_by_name(dev->name) != NULL) {
   up(&rtcan_devices_nrt_lock);
   return -EEXIST;
    }
 
    rtdm_lock_get_irqsave(&rtcan_devices_rt_lock, context);
 
    rtcan_devices[dev->ifindex - 1] = dev;
 
    rtdm_lock_put_irqrestore(&rtcan_devices_rt_lock, context);
    rtcan_dev_create_proc(dev);
 
    up(&rtcan_devices_nrt_lock);
 
    printk("rtcan: registered %s\n", dev->name);
 
    return 0;
}
 
 
int rtcan_dev_unregister(struct rtcan_device *dev)
{
    rtdm_lockctx_t context;
 
 
    RTCAN_ASSERT(dev->ifindex != 0,
        printk("RTCAN: device %s/%p was not registered\n",
           dev->name, dev); return -ENODEV;);
 
    /* If device is running, close it first. */
    if (CAN_STATE_OPERATING(dev->state))
   return -EBUSY;
 
    down(&rtcan_devices_nrt_lock);
 
    rtcan_dev_remove_proc(dev);
 
    rtdm_lock_get_irqsave(&rtcan_devices_rt_lock, context);
 
#ifdef RTCAN_USE_REFCOUNT
    while (atomic_read(&dev->refcount) > 0) {
   rtdm_lock_put_irqrestore(&rtcan_devices_rt_lock, context);
   up(&rtcan_devices_nrt_lock);
 
   RTCAN_DBG("RTCAN: unregistering %s deferred (refcount = %d)\n",
         dev->name, atomic_read(&dev->refcount));
   set_current_state(TASK_UNINTERRUPTIBLE);
   schedule_timeout(1*HZ); /* wait a second */
 
   down(&rtcan_devices_nrt_lock);
   rtdm_lock_get_irqsave(&rtcan_devices_rt_lock, context);
    }
#endif
    rtcan_devices[dev->ifindex - 1] = NULL;
 
    rtdm_lock_put_irqrestore(&rtcan_devices_rt_lock, context);
    up(&rtcan_devices_nrt_lock);
 
#ifdef RTCAN_USE_REFCOUNT
    RTCAN_ASSERT(atomic_read(&dev->refcount) == 0,
        printk("RTCAN: dev reference counter < 0!\n"););
#endif
 
    printk("RTCAN: unregistered %s\n", dev->name);
 
    return 0;
}
 
 
EXPORT_SYMBOL_GPL(rtcan_socket_lock);
EXPORT_SYMBOL_GPL(rtcan_recv_list_lock);
 
EXPORT_SYMBOL_GPL(rtcan_dev_free);
 
EXPORT_SYMBOL_GPL(rtcan_dev_alloc);
EXPORT_SYMBOL_GPL(rtcan_dev_alloc_name);
 
EXPORT_SYMBOL_GPL(rtcan_dev_register);
EXPORT_SYMBOL_GPL(rtcan_dev_unregister);
 
EXPORT_SYMBOL_GPL(rtcan_dev_get_by_name);
EXPORT_SYMBOL_GPL(rtcan_dev_get_by_index);