|
1 /****************************************************************************** |
|
2 * |
|
3 * m o d u l e . c |
|
4 * |
|
5 * EtherCAT-Master-Treiber |
|
6 * |
|
7 * Autoren: Wilhelm Hagemeister, Florian Pose |
|
8 * |
|
9 * $Id$ |
|
10 * |
|
11 * (C) Copyright IgH 2005 |
|
12 * Ingenieurgemeinschaft IgH |
|
13 * Heinz-Bäcker Str. 34 |
|
14 * D-45356 Essen |
|
15 * Tel.: +49 201/61 99 31 |
|
16 * Fax.: +49 201/61 98 36 |
|
17 * E-mail: sp@igh-essen.com |
|
18 * |
|
19 *****************************************************************************/ |
|
20 |
|
21 #include <linux/module.h> |
|
22 #include <linux/kernel.h> |
|
23 #include <linux/init.h> |
|
24 |
|
25 #include "master.h" |
|
26 #include "device.h" |
|
27 |
|
28 /*****************************************************************************/ |
|
29 |
|
30 int __init ec_init_module(void); |
|
31 void __exit ec_cleanup_module(void); |
|
32 |
|
33 /*****************************************************************************/ |
|
34 |
|
35 #define LIT(X) #X |
|
36 #define STR(X) LIT(X) |
|
37 |
|
38 #define COMPILE_INFO "Revision " STR(EC_REV) \ |
|
39 ", compiled by " STR(EC_USER) \ |
|
40 " at " STR(EC_DATE) |
|
41 |
|
42 /*****************************************************************************/ |
|
43 |
|
44 int ec_master_count = 1; |
|
45 ec_master_t *ec_masters = NULL; |
|
46 int *ec_masters_reserved = NULL; |
|
47 |
|
48 /*****************************************************************************/ |
|
49 |
|
50 MODULE_AUTHOR ("Wilhelm Hagemeister <hm@igh-essen.com>," |
|
51 "Florian Pose <fp@igh-essen.com>"); |
|
52 MODULE_DESCRIPTION ("EtherCAT master driver module"); |
|
53 MODULE_LICENSE("GPL"); |
|
54 MODULE_VERSION(COMPILE_INFO); |
|
55 |
|
56 module_param(ec_master_count, int, 1); |
|
57 MODULE_PARM_DESC(ec_master_count, "Number of EtherCAT master to initialize."); |
|
58 |
|
59 /*****************************************************************************/ |
|
60 |
|
61 /** |
|
62 Init-Funktion des EtherCAT-Master-Treibermodules |
|
63 |
|
64 Initialisiert soviele Master, wie im Parameter ec_master_count |
|
65 angegeben wurde (Default ist 1). |
|
66 |
|
67 @returns 0, wenn alles o.k., -1 bei ungueltiger Anzahl Master |
|
68 oder zu wenig Speicher. |
|
69 */ |
|
70 |
|
71 int __init ec_init_module(void) |
|
72 { |
|
73 unsigned int i; |
|
74 |
|
75 printk(KERN_ERR "EtherCAT: Master driver, %s\n", COMPILE_INFO); |
|
76 |
|
77 if (ec_master_count < 1) { |
|
78 printk(KERN_ERR "EtherCAT: Error - Illegal" |
|
79 " ec_master_count: %i\n", ec_master_count); |
|
80 return -1; |
|
81 } |
|
82 |
|
83 printk(KERN_ERR "EtherCAT: Initializing %i EtherCAT master(s)...\n", |
|
84 ec_master_count); |
|
85 |
|
86 if ((ec_masters = |
|
87 (ec_master_t *) kmalloc(sizeof(ec_master_t) |
|
88 * ec_master_count, |
|
89 GFP_KERNEL)) == NULL) { |
|
90 printk(KERN_ERR "EtherCAT: Could not allocate" |
|
91 " memory for EtherCAT master(s)!\n"); |
|
92 return -1; |
|
93 } |
|
94 |
|
95 if ((ec_masters_reserved = |
|
96 (int *) kmalloc(sizeof(int) * ec_master_count, |
|
97 GFP_KERNEL)) == NULL) { |
|
98 printk(KERN_ERR "EtherCAT: Could not allocate" |
|
99 " memory for reservation flags!\n"); |
|
100 kfree(ec_masters); |
|
101 return -1; |
|
102 } |
|
103 |
|
104 for (i = 0; i < ec_master_count; i++) |
|
105 { |
|
106 ec_master_init(&ec_masters[i]); |
|
107 ec_masters_reserved[i] = 0; |
|
108 } |
|
109 |
|
110 printk(KERN_ERR "EtherCAT: Master driver initialized.\n"); |
|
111 |
|
112 return 0; |
|
113 } |
|
114 |
|
115 /*****************************************************************************/ |
|
116 |
|
117 /** |
|
118 Cleanup-Funktion des EtherCAT-Master-Treibermoduls |
|
119 |
|
120 Entfernt alle Master-Instanzen. |
|
121 */ |
|
122 |
|
123 void __exit ec_cleanup_module(void) |
|
124 { |
|
125 unsigned int i; |
|
126 |
|
127 printk(KERN_ERR "EtherCAT: Cleaning up master driver...\n"); |
|
128 |
|
129 if (ec_masters) |
|
130 { |
|
131 for (i = 0; i < ec_master_count; i++) |
|
132 { |
|
133 if (ec_masters_reserved[i]) { |
|
134 printk(KERN_WARNING "EtherCAT: Warning -" |
|
135 " Master %i is still in use!\n", i); |
|
136 } |
|
137 |
|
138 ec_master_clear(&ec_masters[i]); |
|
139 } |
|
140 |
|
141 kfree(ec_masters); |
|
142 } |
|
143 |
|
144 printk(KERN_ERR "EtherCAT: Master driver cleaned up.\n"); |
|
145 } |
|
146 |
|
147 /****************************************************************************** |
|
148 * |
|
149 * Treiberschnittstelle |
|
150 * |
|
151 *****************************************************************************/ |
|
152 |
|
153 /** |
|
154 Setzt das EtherCAT-Geraet, auf dem der Master arbeitet. |
|
155 |
|
156 |
|
157 @param master Der EtherCAT-Master |
|
158 @param device Das EtherCAT-Geraet |
|
159 @return 0, wenn alles o.k., |
|
160 < 0, wenn bereits ein Geraet registriert |
|
161 oder das Geraet nicht geoeffnet werden konnte. |
|
162 */ |
|
163 |
|
164 ec_device_t *EtherCAT_dev_register(unsigned int master_index, |
|
165 struct net_device *dev, |
|
166 irqreturn_t (*isr)(int, void *, |
|
167 struct pt_regs *), |
|
168 struct module *module) |
|
169 { |
|
170 ec_device_t *ecd; |
|
171 |
|
172 if (master_index >= ec_master_count) { |
|
173 printk(KERN_ERR "EtherCAT: Master %i does not exist!\n", master_index); |
|
174 return NULL; |
|
175 } |
|
176 |
|
177 if (!dev) { |
|
178 printk("EtherCAT: Device is NULL!\n"); |
|
179 return NULL; |
|
180 } |
|
181 |
|
182 if (ec_masters[master_index].device_registered) { |
|
183 printk(KERN_ERR "EtherCAT: Master %i already has a device!\n", |
|
184 master_index); |
|
185 return NULL; |
|
186 } |
|
187 |
|
188 ecd = &ec_masters[master_index].device; |
|
189 |
|
190 if (ec_device_init(ecd) < 0) { |
|
191 return NULL; |
|
192 } |
|
193 |
|
194 ecd->dev = dev; |
|
195 ecd->tx_skb->dev = dev; |
|
196 ecd->rx_skb->dev = dev; |
|
197 ecd->isr = isr; |
|
198 ecd->module = module; |
|
199 |
|
200 return ecd; |
|
201 } |
|
202 |
|
203 /*****************************************************************************/ |
|
204 |
|
205 /** |
|
206 Loescht das EtherCAT-Geraet, auf dem der Master arbeitet. |
|
207 |
|
208 @param master Der EtherCAT-Master |
|
209 @param device Das EtherCAT-Geraet |
|
210 */ |
|
211 |
|
212 void EtherCAT_dev_unregister(unsigned int master_index) |
|
213 { |
|
214 if (master_index >= ec_master_count) { |
|
215 printk(KERN_WARNING "EtherCAT: Master %i does not exist!\n", master_index); |
|
216 return; |
|
217 } |
|
218 |
|
219 ec_masters[master_index].device_registered = 0; |
|
220 ec_device_clear(&ec_masters[master_index].device); |
|
221 } |
|
222 |
|
223 /****************************************************************************** |
|
224 * |
|
225 * Echtzeitschnittstelle |
|
226 * |
|
227 *****************************************************************************/ |
|
228 |
|
229 /** |
|
230 Reserviert einen bestimmten EtherCAT-Master und das zugehörige Gerät. |
|
231 |
|
232 Gibt einen Zeiger auf den reservierten EtherCAT-Master zurueck. |
|
233 |
|
234 @param index Index des gewuenschten Masters |
|
235 @returns Zeiger auf EtherCAT-Master oder NULL, wenn Parameter ungueltig. |
|
236 */ |
|
237 |
|
238 ec_master_t *EtherCAT_rt_request_master(int index) |
|
239 { |
|
240 ec_master_t *master; |
|
241 |
|
242 if (index < 0 || index >= ec_master_count) { |
|
243 printk(KERN_ERR "EtherCAT: Master %i does not exist!\n", index); |
|
244 goto req_return; |
|
245 } |
|
246 |
|
247 if (ec_masters_reserved[index]) { |
|
248 printk(KERN_ERR "EtherCAT: Master %i already in use!\n", index); |
|
249 goto req_return; |
|
250 } |
|
251 |
|
252 master = &ec_masters[index]; |
|
253 |
|
254 if (!master->device_registered) { |
|
255 printk(KERN_ERR "EtherCAT: Master %i has no device assigned yet!\n", |
|
256 index); |
|
257 goto req_return; |
|
258 } |
|
259 |
|
260 if (!try_module_get(master->device.module)) { |
|
261 printk(KERN_ERR "EtherCAT: Could not reserve device module!\n"); |
|
262 goto req_return; |
|
263 } |
|
264 |
|
265 if (ec_master_open(master) < 0) { |
|
266 printk(KERN_ERR "EtherCAT: Could not open device!\n"); |
|
267 goto req_module_put; |
|
268 } |
|
269 |
|
270 if (ec_scan_for_slaves(master) != 0) { |
|
271 printk(KERN_ERR "EtherCAT: Could not scan for slaves!\n"); |
|
272 goto req_close; |
|
273 } |
|
274 |
|
275 ec_masters_reserved[index] = 1; |
|
276 printk(KERN_INFO "EtherCAT: Reserved master %i.\n", index); |
|
277 |
|
278 return master; |
|
279 |
|
280 req_close: |
|
281 ec_master_close(master); |
|
282 |
|
283 req_module_put: |
|
284 module_put(master->device.module); |
|
285 |
|
286 req_return: |
|
287 return NULL; |
|
288 } |
|
289 |
|
290 /*****************************************************************************/ |
|
291 |
|
292 /** |
|
293 Gibt einen zuvor angeforderten EtherCAT-Master wieder frei. |
|
294 |
|
295 @param master Zeiger auf den freizugebenden EtherCAT-Master. |
|
296 */ |
|
297 |
|
298 void EtherCAT_rt_release_master(ec_master_t *master) |
|
299 { |
|
300 unsigned int i; |
|
301 |
|
302 for (i = 0; i < ec_master_count; i++) |
|
303 { |
|
304 if (&ec_masters[i] == master) |
|
305 { |
|
306 if (!master->device_registered) { |
|
307 printk(KERN_WARNING "EtherCAT: Could not release device" |
|
308 "module because of no device!\n"); |
|
309 return; |
|
310 } |
|
311 |
|
312 ec_master_close(master); |
|
313 module_put(master->device.module); |
|
314 ec_masters_reserved[i] = 0; |
|
315 |
|
316 printk(KERN_INFO "EtherCAT: Released master %i.\n", i); |
|
317 |
|
318 return; |
|
319 } |
|
320 } |
|
321 |
|
322 printk(KERN_WARNING "EtherCAT: Master %X was never requested!\n", |
|
323 (unsigned int) master); |
|
324 } |
|
325 |
|
326 /*****************************************************************************/ |
|
327 |
|
328 module_init(ec_init_module); |
|
329 module_exit(ec_cleanup_module); |
|
330 |
|
331 EXPORT_SYMBOL(EtherCAT_dev_register); |
|
332 EXPORT_SYMBOL(EtherCAT_dev_unregister); |
|
333 EXPORT_SYMBOL(EtherCAT_rt_request_master); |
|
334 EXPORT_SYMBOL(EtherCAT_rt_release_master); |
|
335 |
|
336 /*****************************************************************************/ |