forked from roboticslab-uc3m/jr3pci-linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjr3pci-ioctl.c
336 lines (307 loc) · 8.02 KB
/
jr3pci-ioctl.c
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include "jr3pci-ioctl.h"
void __iomem *jr3_base_address;
/** Board address to PCI virtual address conversion */
void __iomem *board2virtual(int ba) {
return (void*)jr3_base_address+4*ba;
}
/** Read data memory */
short readData(int ba, int card) {
return (short)readl(board2virtual(ba+card*CARD_OFFSET));
}
/** Write data memory */
void writeData(int ba, int data, int card) {
writel(data,board2virtual(ba+card*CARD_OFFSET));
}
void writeProgram(int pa, short data0, short data1, int card) {
writew(data0,board2virtual(pa+card*CARD_OFFSET));
writew(data1,(board2virtual(pa+card*CARD_OFFSET)+JR3_P8BIT_OFFSET));
}
/** Read program memory */
int readProgram(int pa, int card) {
int r;
r=readw(board2virtual(pa+card*CARD_OFFSET)) << 8;
r=r|readb(board2virtual(pa+card*CARD_OFFSET)+JR3_P8BIT_OFFSET);
return r;
}
int jr3Reset(int card) {
writeData(JR3_RESET_ADDRESS,0,card);
return 0;
}
int jr3ZeroOffs(int card) {
writeData(JR3_COMMAND0,JR3_CMD_RESETOFFSETS,card);
return 0;
}
int jr3Filter(unsigned long arg, int num_filter, int card) {
int i;
int ret=0;
int axload[6];
int address;
for (i = 0; i < 6; i++) {
address = JR3_FILTER0+0x08*num_filter+i;
axload[i]= (short) readData(address, card);
}
ret = copy_to_user((void *) arg, (int *) axload, sizeof(six_axis_array));
return ret;
}
/* Not tested */
int jr3SetFullScales(unsigned long arg, int card) {
int fs[8];
int ret=copy_from_user((int*) fs, (void *) arg, sizeof(force_array));
int i;
int address;
for (i=0; i< 8; i++) {
address=JR3_FULLSCALE+i;
writeData(address,fs[i],card);
}
writeData(JR3_COMMAND0,JR3_CMD_SETFULLSCALES,card);
return ret;
}
int jr3_open(struct inode *inode, struct file *filp) {
// MOD_INC_USE_COUNT;
return 0;
}
int jr3_release(struct inode *inode, struct file *filp) {
// MOD_DEC_USE_COUNT;
return 0;
}
int jr3GetFullScales(unsigned long arg, int card) {
int i;
int ret=0;
int fullscales[8];
for (i = 0; i < 8; i++)
fullscales[i]= readData(JR3_FULLSCALE+i, card);
ret = copy_to_user((void *) arg, (int *) fullscales, sizeof(force_array));
return ret;
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
int jr3_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
#else
long jr3_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
#endif
{
int err=0;
int ret=0;
int size = _IOC_SIZE(cmd); /* the size bitfield in cmd */
int type = _IOC_TYPE(cmd);
int nr = _IOC_NR(cmd);
if ( type != JR3_IOC_MAGIC) return -ENOTTY;
if ( nr > IOCTL_JR3_MAXNR) return -ENOTTY;
#ifndef LINUX_20
if (_IOC_DIR(cmd) & _IOC_READ)
err = !access_ok(VERIFY_WRITE, (void *)arg, size);
if (_IOC_DIR(cmd) & _IOC_WRITE)
err = !access_ok(VERIFY_READ, (void *)arg, size);
if (err) return -EFAULT;
#else
if (_IOC_DIR(cmd) & _IOC_READ)
err = verify_area(VERIFY_WRITE, (void *)arg, size);
if (_IOC_DIR(cmd) & _IOC_WRITE)
err = verify_area(VERIFY_READ, (void *)arg, size);
if (err) return err;
#endif
switch(cmd) {
case IOCTL0_JR3_RESET:
ret = jr3Reset(0);
break;
case IOCTL0_JR3_ZEROOFFS:
ret = jr3ZeroOffs(0);
break;
case IOCTL0_JR3_FILTER0:
ret = jr3Filter(arg, 0,0);
break;
case IOCTL0_JR3_FILTER1:
ret = jr3Filter(arg, 1,0);
break;
case IOCTL0_JR3_FILTER2:
ret = jr3Filter(arg, 2,0);
break;
case IOCTL0_JR3_FILTER3:
ret = jr3Filter(arg, 3,0);
break;
case IOCTL0_JR3_FILTER4:
ret = jr3Filter(arg, 4,0);
break;
case IOCTL0_JR3_FILTER5:
ret = jr3Filter(arg, 5,0);
break;
case IOCTL0_JR3_FILTER6:
ret = jr3Filter(arg, 6,0);
break;
case IOCTL0_JR3_GET_FULL_SCALES:
ret = jr3GetFullScales(arg,0);
break;
case IOCTL0_JR3_SET_FULL_SCALES:
if (PCI_DEVICE_ID_JR3==0x3114)
ret = jr3SetFullScales(arg,0);
else ret=-1;
break;
case IOCTL1_JR3_RESET:
if ((PCI_DEVICE_ID_JR3==0x3112)||(PCI_DEVICE_ID_JR3==0x3114))
ret = jr3Reset(1);
else ret=-1;
break;
case IOCTL1_JR3_ZEROOFFS:
if ((PCI_DEVICE_ID_JR3==0x3112)||(PCI_DEVICE_ID_JR3==0x3114))
ret = jr3ZeroOffs(1);
else ret=-1;
break;
case IOCTL1_JR3_FILTER0:
if ((PCI_DEVICE_ID_JR3==0x3112)||(PCI_DEVICE_ID_JR3==0x3114))
ret = jr3Filter(arg, 0,1);
else ret=-1;
break;
case IOCTL1_JR3_FILTER1:
if ((PCI_DEVICE_ID_JR3==0x3112)||(PCI_DEVICE_ID_JR3==0x3114))
ret = jr3Filter(arg, 1,1);
else ret=-1;
break;
case IOCTL1_JR3_FILTER2:
if ((PCI_DEVICE_ID_JR3==0x3112)||(PCI_DEVICE_ID_JR3==0x3114))
ret = jr3Filter(arg, 2,1);
else ret=-1;
break;
case IOCTL1_JR3_FILTER3:
if ((PCI_DEVICE_ID_JR3==0x3112)||(PCI_DEVICE_ID_JR3==0x3114))
ret = jr3Filter(arg, 3,1);
else ret=-1;
break;
case IOCTL1_JR3_FILTER4:
if ((PCI_DEVICE_ID_JR3==0x3112)||(PCI_DEVICE_ID_JR3==0x3114))
ret = jr3Filter(arg, 4,1);
else ret=-1;
break;
case IOCTL1_JR3_FILTER5:
if ((PCI_DEVICE_ID_JR3==0x3112)||(PCI_DEVICE_ID_JR3==0x3114))
ret = jr3Filter(arg, 5,1);
else ret=-1;
break;
case IOCTL1_JR3_FILTER6:
if ((PCI_DEVICE_ID_JR3==0x3112)||(PCI_DEVICE_ID_JR3==0x3114))
ret = jr3Filter(arg, 6,1);
else ret=-1;
break;
case IOCTL1_JR3_GET_FULL_SCALES:
if ((PCI_DEVICE_ID_JR3==0x3112)||(PCI_DEVICE_ID_JR3==0x3114))
ret = jr3GetFullScales(arg,1);
else ret=-1;
break;
case IOCTL1_JR3_SET_FULL_SCALES:
if (PCI_DEVICE_ID_JR3==0x3114)
ret = jr3SetFullScales(arg,1);
else ret=-1;
break;
case IOCTL2_JR3_RESET:
if (PCI_DEVICE_ID_JR3==0x3114)
ret = jr3Reset(2);
else ret=-1;
break;
case IOCTL2_JR3_ZEROOFFS:
if (PCI_DEVICE_ID_JR3==0x3114)
ret = jr3ZeroOffs(2);
else ret=-1;
break;
case IOCTL2_JR3_FILTER0:
if (PCI_DEVICE_ID_JR3==0x3114)
ret = jr3Filter(arg, 0,2);
else ret=-1;
break;
case IOCTL2_JR3_FILTER1:
if (PCI_DEVICE_ID_JR3==0x3114)
ret = jr3Filter(arg, 1,2);
else ret=-1;
break;
case IOCTL2_JR3_FILTER2:
if (PCI_DEVICE_ID_JR3==0x3114)
ret = jr3Filter(arg, 2,2);
else ret=-1;
break;
case IOCTL2_JR3_FILTER3:
if (PCI_DEVICE_ID_JR3==0x3114)
ret = jr3Filter(arg, 3,2);
else ret=-1;
break;
case IOCTL2_JR3_FILTER4:
if (PCI_DEVICE_ID_JR3==0x3114)
ret = jr3Filter(arg, 4,2);
else ret=-1;
break;
case IOCTL2_JR3_FILTER5:
if (PCI_DEVICE_ID_JR3==0x3114)
ret = jr3Filter(arg, 5,2);
else ret=-1;
break;
case IOCTL2_JR3_FILTER6:
if (PCI_DEVICE_ID_JR3==0x3114)
ret = jr3Filter(arg, 6,2);
else ret=-1;
break;
case IOCTL2_JR3_GET_FULL_SCALES:
if (PCI_DEVICE_ID_JR3==0x3114)
ret = jr3GetFullScales(arg,2);
else ret=-1;
break;
case IOCTL2_JR3_SET_FULL_SCALES:
if (PCI_DEVICE_ID_JR3==0x3114)
ret = jr3SetFullScales(arg,2);
else ret=-1;
break;
case IOCTL3_JR3_RESET:
if (PCI_DEVICE_ID_JR3==0x3114)
ret = jr3Reset(3);
else ret=-1;
break;
case IOCTL3_JR3_ZEROOFFS:
if (PCI_DEVICE_ID_JR3==0x3114)
ret = jr3ZeroOffs(3);
else ret=-1;
break;
case IOCTL3_JR3_FILTER0:
if (PCI_DEVICE_ID_JR3==0x3114)
ret = jr3Filter(arg, 0,3);
else ret=-1;
break;
case IOCTL3_JR3_FILTER1:
if (PCI_DEVICE_ID_JR3==0x3114)
ret = jr3Filter(arg, 1,3);
else ret=-1;
break;
case IOCTL3_JR3_FILTER2:
if (PCI_DEVICE_ID_JR3==0x3114)
ret = jr3Filter(arg, 2,3);
else ret=-1;
break;
case IOCTL3_JR3_FILTER3:
if (PCI_DEVICE_ID_JR3==0x3114)
ret = jr3Filter(arg, 3,3);
else ret=-1;
break;
case IOCTL3_JR3_FILTER4:
if (PCI_DEVICE_ID_JR3==0x3114)
ret = jr3Filter(arg, 4,3);
else ret=-1;
break;
case IOCTL3_JR3_FILTER5:
if (PCI_DEVICE_ID_JR3==0x3114)
ret = jr3Filter(arg, 5,3);
else ret=-1;
break;
case IOCTL3_JR3_FILTER6:
if (PCI_DEVICE_ID_JR3==0x3114)
ret = jr3Filter(arg, 6,3);
else ret=-1;
break;
case IOCTL3_JR3_GET_FULL_SCALES:
if (PCI_DEVICE_ID_JR3==0x3114)
ret = jr3GetFullScales(arg,3);
else ret=-1;
break;
case IOCTL3_JR3_SET_FULL_SCALES:
if (PCI_DEVICE_ID_JR3==0x3114)
ret = jr3SetFullScales(arg,3);
else ret=-1;
break;
default:
return -ENOTTY;
}
return ret;
}