Commit | Line | Data |
---|---|---|
578c0183 AB |
1 | /* |
2 | comedi/drivers/serial2002.c | |
3 | Skeleton code for a Comedi driver | |
4 | ||
5 | COMEDI - Linux Control and Measurement Device Interface | |
6 | Copyright (C) 2002 Anders Blomdell <anders.blomdell@control.lth.se> | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program; if not, write to the Free Software | |
20 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
21 | ||
22 | */ | |
23 | ||
24 | /* | |
25 | Driver: serial2002 | |
26 | Description: Driver for serial connected hardware | |
27 | Devices: | |
28 | Author: Anders Blomdell | |
29 | Updated: Fri, 7 Jun 2002 12:56:45 -0700 | |
30 | Status: in development | |
31 | ||
32 | */ | |
33 | ||
34 | #include "../comedidev.h" | |
35 | ||
36 | #include <linux/delay.h> | |
37 | #include <linux/ioport.h> | |
02921484 | 38 | #include <linux/sched.h> |
5a0e3ad6 | 39 | #include <linux/slab.h> |
578c0183 | 40 | |
b041267e | 41 | #include <linux/termios.h> |
578c0183 AB |
42 | #include <asm/ioctls.h> |
43 | #include <linux/serial.h> | |
44 | #include <linux/poll.h> | |
45 | ||
ca9ed0f2 | 46 | struct serial2002_board { |
578c0183 | 47 | const char *name; |
ca9ed0f2 | 48 | }; |
578c0183 | 49 | |
b3fb588d BP |
50 | struct serial2002_range_table_t { |
51 | ||
2696fb57 | 52 | /* HACK... */ |
578c0183 | 53 | int length; |
1f6325d6 | 54 | struct comedi_krange range; |
b3fb588d BP |
55 | }; |
56 | ||
e21ffa44 BP |
57 | struct serial2002_private { |
58 | ||
2696fb57 BP |
59 | int port; /* /dev/ttyS<port> */ |
60 | int speed; /* baudrate */ | |
578c0183 | 61 | struct file *tty; |
790c5541 | 62 | unsigned int ao_readback[32]; |
578c0183 AB |
63 | unsigned char digital_in_mapping[32]; |
64 | unsigned char digital_out_mapping[32]; | |
65 | unsigned char analog_in_mapping[32]; | |
66 | unsigned char analog_out_mapping[32]; | |
67 | unsigned char encoder_in_mapping[32]; | |
b3fb588d | 68 | struct serial2002_range_table_t in_range[32], out_range[32]; |
e21ffa44 BP |
69 | }; |
70 | ||
578c0183 AB |
71 | /* |
72 | * most drivers define the following macro to make it easy to | |
73 | * access the private structure. | |
74 | */ | |
e21ffa44 | 75 | #define devpriv ((struct serial2002_private *)dev->private) |
578c0183 | 76 | |
578c0183 AB |
77 | struct serial_data { |
78 | enum { is_invalid, is_digital, is_channel } kind; | |
79 | int index; | |
80 | unsigned long value; | |
81 | }; | |
82 | ||
83 | static long tty_ioctl(struct file *f, unsigned op, unsigned long param) | |
84 | { | |
00a1855c | 85 | if (f->f_op->unlocked_ioctl) |
578c0183 | 86 | return f->f_op->unlocked_ioctl(f, op, param); |
00a1855c | 87 | |
578c0183 AB |
88 | return -ENOSYS; |
89 | } | |
90 | ||
91 | static int tty_write(struct file *f, unsigned char *buf, int count) | |
92 | { | |
93 | int result; | |
94 | mm_segment_t oldfs; | |
95 | ||
96 | oldfs = get_fs(); | |
97 | set_fs(KERNEL_DS); | |
98 | f->f_pos = 0; | |
99 | result = f->f_op->write(f, buf, count, &f->f_pos); | |
100 | set_fs(oldfs); | |
101 | return result; | |
102 | } | |
103 | ||
104 | #if 0 | |
105 | /* | |
106 | * On 2.6.26.3 this occaisonally gave me page faults, worked around by | |
107 | * settings.c_cc[VMIN] = 0; settings.c_cc[VTIME] = 0 | |
108 | */ | |
109 | static int tty_available(struct file *f) | |
110 | { | |
111 | long result = 0; | |
112 | mm_segment_t oldfs; | |
113 | ||
114 | oldfs = get_fs(); | |
115 | set_fs(KERNEL_DS); | |
116 | tty_ioctl(f, FIONREAD, (unsigned long)&result); | |
117 | set_fs(oldfs); | |
118 | return result; | |
119 | } | |
120 | #endif | |
121 | ||
122 | static int tty_read(struct file *f, int timeout) | |
123 | { | |
124 | int result; | |
125 | ||
126 | result = -1; | |
127 | if (!IS_ERR(f)) { | |
128 | mm_segment_t oldfs; | |
129 | ||
130 | oldfs = get_fs(); | |
131 | set_fs(KERNEL_DS); | |
132 | if (f->f_op->poll) { | |
133 | struct poll_wqueues table; | |
134 | struct timeval start, now; | |
135 | ||
136 | do_gettimeofday(&start); | |
137 | poll_initwait(&table); | |
138 | while (1) { | |
139 | long elapsed; | |
140 | int mask; | |
141 | ||
142 | mask = f->f_op->poll(f, &table.pt); | |
143 | if (mask & (POLLRDNORM | POLLRDBAND | POLLIN | | |
0a85b6f0 | 144 | POLLHUP | POLLERR)) { |
578c0183 AB |
145 | break; |
146 | } | |
147 | do_gettimeofday(&now); | |
148 | elapsed = | |
0a85b6f0 MT |
149 | (1000000 * (now.tv_sec - start.tv_sec) + |
150 | now.tv_usec - start.tv_usec); | |
b041267e | 151 | if (elapsed > timeout) |
578c0183 | 152 | break; |
578c0183 AB |
153 | set_current_state(TASK_INTERRUPTIBLE); |
154 | schedule_timeout(((timeout - | |
0a85b6f0 | 155 | elapsed) * HZ) / 10000); |
578c0183 AB |
156 | } |
157 | poll_freewait(&table); | |
158 | { | |
0a85b6f0 | 159 | unsigned char ch; |
578c0183 | 160 | |
0a85b6f0 | 161 | f->f_pos = 0; |
b041267e | 162 | if (f->f_op->read(f, &ch, 1, &f->f_pos) == 1) |
0a85b6f0 | 163 | result = ch; |
578c0183 AB |
164 | } |
165 | } else { | |
166 | /* Device does not support poll, busy wait */ | |
167 | int retries = 0; | |
168 | while (1) { | |
356cdbcb | 169 | unsigned char ch; |
578c0183 AB |
170 | |
171 | retries++; | |
b041267e | 172 | if (retries >= timeout) |
578c0183 | 173 | break; |
578c0183 AB |
174 | |
175 | f->f_pos = 0; | |
176 | if (f->f_op->read(f, &ch, 1, &f->f_pos) == 1) { | |
356cdbcb | 177 | result = ch; |
578c0183 AB |
178 | break; |
179 | } | |
5f74ea14 | 180 | udelay(100); |
578c0183 AB |
181 | } |
182 | } | |
183 | set_fs(oldfs); | |
184 | } | |
185 | return result; | |
186 | } | |
187 | ||
188 | static void tty_setspeed(struct file *f, int speed) | |
189 | { | |
190 | mm_segment_t oldfs; | |
191 | ||
192 | oldfs = get_fs(); | |
193 | set_fs(KERNEL_DS); | |
194 | { | |
2696fb57 | 195 | /* Set speed */ |
578c0183 AB |
196 | struct termios settings; |
197 | ||
198 | tty_ioctl(f, TCGETS, (unsigned long)&settings); | |
2696fb57 | 199 | /* printk("Speed: %d\n", settings.c_cflag & (CBAUD | CBAUDEX)); */ |
578c0183 AB |
200 | settings.c_iflag = 0; |
201 | settings.c_oflag = 0; | |
202 | settings.c_lflag = 0; | |
203 | settings.c_cflag = CLOCAL | CS8 | CREAD; | |
204 | settings.c_cc[VMIN] = 0; | |
205 | settings.c_cc[VTIME] = 0; | |
206 | switch (speed) { | |
207 | case 2400:{ | |
208 | settings.c_cflag |= B2400; | |
209 | } | |
210 | break; | |
211 | case 4800:{ | |
212 | settings.c_cflag |= B4800; | |
213 | } | |
214 | break; | |
215 | case 9600:{ | |
216 | settings.c_cflag |= B9600; | |
217 | } | |
218 | break; | |
219 | case 19200:{ | |
220 | settings.c_cflag |= B19200; | |
221 | } | |
222 | break; | |
223 | case 38400:{ | |
224 | settings.c_cflag |= B38400; | |
225 | } | |
226 | break; | |
227 | case 57600:{ | |
228 | settings.c_cflag |= B57600; | |
229 | } | |
230 | break; | |
231 | case 115200:{ | |
232 | settings.c_cflag |= B115200; | |
233 | } | |
234 | break; | |
235 | default:{ | |
236 | settings.c_cflag |= B9600; | |
237 | } | |
238 | break; | |
239 | } | |
240 | tty_ioctl(f, TCSETS, (unsigned long)&settings); | |
2696fb57 | 241 | /* printk("Speed: %d\n", settings.c_cflag & (CBAUD | CBAUDEX)); */ |
578c0183 AB |
242 | } |
243 | { | |
2696fb57 | 244 | /* Set low latency */ |
578c0183 AB |
245 | struct serial_struct settings; |
246 | ||
247 | tty_ioctl(f, TIOCGSERIAL, (unsigned long)&settings); | |
248 | settings.flags |= ASYNC_LOW_LATENCY; | |
249 | tty_ioctl(f, TIOCSSERIAL, (unsigned long)&settings); | |
250 | } | |
251 | ||
252 | set_fs(oldfs); | |
253 | } | |
254 | ||
255 | static void poll_digital(struct file *f, int channel) | |
256 | { | |
257 | char cmd; | |
258 | ||
259 | cmd = 0x40 | (channel & 0x1f); | |
260 | tty_write(f, &cmd, 1); | |
261 | } | |
262 | ||
263 | static void poll_channel(struct file *f, int channel) | |
264 | { | |
265 | char cmd; | |
266 | ||
267 | cmd = 0x60 | (channel & 0x1f); | |
268 | tty_write(f, &cmd, 1); | |
269 | } | |
270 | ||
271 | static struct serial_data serial_read(struct file *f, int timeout) | |
272 | { | |
273 | struct serial_data result; | |
274 | int length; | |
275 | ||
276 | result.kind = is_invalid; | |
277 | result.index = 0; | |
278 | result.value = 0; | |
279 | length = 0; | |
280 | while (1) { | |
281 | int data = tty_read(f, timeout); | |
282 | ||
283 | length++; | |
284 | if (data < 0) { | |
b041267e | 285 | printk(KERN_ERR "serial2002 error\n"); |
578c0183 AB |
286 | break; |
287 | } else if (data & 0x80) { | |
288 | result.value = (result.value << 7) | (data & 0x7f); | |
289 | } else { | |
290 | if (length == 1) { | |
291 | switch ((data >> 5) & 0x03) { | |
292 | case 0:{ | |
293 | result.value = 0; | |
294 | result.kind = is_digital; | |
295 | } | |
296 | break; | |
297 | case 1:{ | |
298 | result.value = 1; | |
299 | result.kind = is_digital; | |
300 | } | |
301 | break; | |
302 | } | |
303 | } else { | |
304 | result.value = | |
0a85b6f0 | 305 | (result.value << 2) | ((data & 0x60) >> 5); |
578c0183 AB |
306 | result.kind = is_channel; |
307 | } | |
308 | result.index = data & 0x1f; | |
309 | break; | |
310 | } | |
311 | } | |
312 | return result; | |
313 | ||
314 | } | |
315 | ||
316 | static void serial_write(struct file *f, struct serial_data data) | |
317 | { | |
318 | if (data.kind == is_digital) { | |
319 | unsigned char ch = | |
0a85b6f0 | 320 | ((data.value << 5) & 0x20) | (data.index & 0x1f); |
578c0183 AB |
321 | tty_write(f, &ch, 1); |
322 | } else { | |
323 | unsigned char ch[6]; | |
324 | int i = 0; | |
325 | if (data.value >= (1L << 30)) { | |
326 | ch[i] = 0x80 | ((data.value >> 30) & 0x03); | |
327 | i++; | |
328 | } | |
329 | if (data.value >= (1L << 23)) { | |
330 | ch[i] = 0x80 | ((data.value >> 23) & 0x7f); | |
331 | i++; | |
332 | } | |
333 | if (data.value >= (1L << 16)) { | |
334 | ch[i] = 0x80 | ((data.value >> 16) & 0x7f); | |
335 | i++; | |
336 | } | |
337 | if (data.value >= (1L << 9)) { | |
338 | ch[i] = 0x80 | ((data.value >> 9) & 0x7f); | |
339 | i++; | |
340 | } | |
341 | ch[i] = 0x80 | ((data.value >> 2) & 0x7f); | |
342 | i++; | |
343 | ch[i] = ((data.value << 5) & 0x60) | (data.index & 0x1f); | |
344 | i++; | |
345 | tty_write(f, ch, i); | |
346 | } | |
347 | } | |
348 | ||
3c17ba07 | 349 | static int serial_2002_open(struct comedi_device *dev) |
578c0183 | 350 | { |
3c17ba07 | 351 | int result; |
578c0183 AB |
352 | char port[20]; |
353 | ||
354 | sprintf(port, "/dev/ttyS%d", devpriv->port); | |
2021937c | 355 | devpriv->tty = filp_open(port, O_RDWR, 0); |
578c0183 | 356 | if (IS_ERR(devpriv->tty)) { |
3c17ba07 | 357 | result = (int)PTR_ERR(devpriv->tty); |
b041267e | 358 | printk(KERN_ERR "serial_2002: file open error = %d\n", result); |
578c0183 | 359 | } else { |
e2b523aa BP |
360 | struct config_t { |
361 | ||
67a6efb1 IA |
362 | short int kind; |
363 | short int bits; | |
578c0183 AB |
364 | int min; |
365 | int max; | |
e2b523aa BP |
366 | }; |
367 | ||
668f272e IA |
368 | struct config_t *dig_in_config; |
369 | struct config_t *dig_out_config; | |
370 | struct config_t *chan_in_config; | |
371 | struct config_t *chan_out_config; | |
578c0183 AB |
372 | int i; |
373 | ||
9e7f2256 | 374 | result = 0; |
668f272e IA |
375 | dig_in_config = kcalloc(32, sizeof(struct config_t), |
376 | GFP_KERNEL); | |
377 | dig_out_config = kcalloc(32, sizeof(struct config_t), | |
378 | GFP_KERNEL); | |
379 | chan_in_config = kcalloc(32, sizeof(struct config_t), | |
380 | GFP_KERNEL); | |
381 | chan_out_config = kcalloc(32, sizeof(struct config_t), | |
382 | GFP_KERNEL); | |
383 | if (!dig_in_config || !dig_out_config | |
384 | || !chan_in_config || !chan_out_config) { | |
385 | result = -ENOMEM; | |
386 | goto err_alloc_configs; | |
578c0183 AB |
387 | } |
388 | ||
389 | tty_setspeed(devpriv->tty, devpriv->speed); | |
2696fb57 | 390 | poll_channel(devpriv->tty, 31); /* Start reading configuration */ |
578c0183 AB |
391 | while (1) { |
392 | struct serial_data data; | |
393 | ||
394 | data = serial_read(devpriv->tty, 1000); | |
395 | if (data.kind != is_channel || data.index != 31 | |
0a85b6f0 | 396 | || !(data.value & 0xe0)) { |
578c0183 AB |
397 | break; |
398 | } else { | |
399 | int command, channel, kind; | |
c5da2090 | 400 | struct config_t *cur_config = NULL; |
578c0183 AB |
401 | |
402 | channel = data.value & 0x1f; | |
403 | kind = (data.value >> 5) & 0x7; | |
404 | command = (data.value >> 8) & 0x3; | |
405 | switch (kind) { | |
406 | case 1:{ | |
407 | cur_config = dig_in_config; | |
408 | } | |
409 | break; | |
410 | case 2:{ | |
411 | cur_config = dig_out_config; | |
412 | } | |
413 | break; | |
414 | case 3:{ | |
415 | cur_config = chan_in_config; | |
416 | } | |
417 | break; | |
418 | case 4:{ | |
419 | cur_config = chan_out_config; | |
420 | } | |
421 | break; | |
422 | case 5:{ | |
423 | cur_config = chan_in_config; | |
424 | } | |
425 | break; | |
426 | } | |
427 | ||
428 | if (cur_config) { | |
429 | cur_config[channel].kind = kind; | |
430 | switch (command) { | |
431 | case 0:{ | |
0a85b6f0 MT |
432 | cur_config[channel].bits |
433 | = | |
434 | (data.value >> 10) & | |
435 | 0x3f; | |
578c0183 AB |
436 | } |
437 | break; | |
438 | case 1:{ | |
439 | int unit, sign, min; | |
0a85b6f0 MT |
440 | unit = |
441 | (data.value >> 10) & | |
442 | 0x7; | |
443 | sign = | |
444 | (data.value >> 13) & | |
445 | 0x1; | |
446 | min = | |
447 | (data.value >> 14) & | |
448 | 0xfffff; | |
578c0183 AB |
449 | |
450 | switch (unit) { | |
451 | case 0:{ | |
0a85b6f0 MT |
452 | min = |
453 | min | |
454 | * | |
455 | 1000000; | |
578c0183 AB |
456 | } |
457 | break; | |
458 | case 1:{ | |
0a85b6f0 MT |
459 | min = |
460 | min | |
461 | * | |
462 | 1000; | |
578c0183 AB |
463 | } |
464 | break; | |
465 | case 2:{ | |
0a85b6f0 MT |
466 | min = |
467 | min | |
468 | * 1; | |
578c0183 AB |
469 | } |
470 | break; | |
471 | } | |
e9d1cf89 | 472 | if (sign) |
578c0183 | 473 | min = -min; |
0a85b6f0 MT |
474 | cur_config[channel].min |
475 | = min; | |
578c0183 AB |
476 | } |
477 | break; | |
478 | case 2:{ | |
479 | int unit, sign, max; | |
0a85b6f0 MT |
480 | unit = |
481 | (data.value >> 10) & | |
482 | 0x7; | |
483 | sign = | |
484 | (data.value >> 13) & | |
485 | 0x1; | |
486 | max = | |
487 | (data.value >> 14) & | |
488 | 0xfffff; | |
578c0183 AB |
489 | |
490 | switch (unit) { | |
491 | case 0:{ | |
0a85b6f0 MT |
492 | max = |
493 | max | |
494 | * | |
495 | 1000000; | |
578c0183 AB |
496 | } |
497 | break; | |
498 | case 1:{ | |
0a85b6f0 MT |
499 | max = |
500 | max | |
501 | * | |
502 | 1000; | |
578c0183 AB |
503 | } |
504 | break; | |
505 | case 2:{ | |
0a85b6f0 MT |
506 | max = |
507 | max | |
508 | * 1; | |
578c0183 AB |
509 | } |
510 | break; | |
511 | } | |
e9d1cf89 | 512 | if (sign) |
578c0183 | 513 | max = -max; |
0a85b6f0 MT |
514 | cur_config[channel].max |
515 | = max; | |
578c0183 AB |
516 | } |
517 | break; | |
518 | } | |
519 | } | |
520 | } | |
521 | } | |
522 | for (i = 0; i <= 4; i++) { | |
2696fb57 | 523 | /* Fill in subdev data */ |
e2b523aa | 524 | struct config_t *c; |
c5da2090 IA |
525 | unsigned char *mapping = NULL; |
526 | struct serial2002_range_table_t *range = NULL; | |
578c0183 AB |
527 | int kind = 0; |
528 | ||
529 | switch (i) { | |
530 | case 0:{ | |
531 | c = dig_in_config; | |
532 | mapping = devpriv->digital_in_mapping; | |
533 | kind = 1; | |
534 | } | |
535 | break; | |
536 | case 1:{ | |
537 | c = dig_out_config; | |
538 | mapping = devpriv->digital_out_mapping; | |
539 | kind = 2; | |
540 | } | |
541 | break; | |
542 | case 2:{ | |
543 | c = chan_in_config; | |
544 | mapping = devpriv->analog_in_mapping; | |
545 | range = devpriv->in_range; | |
546 | kind = 3; | |
547 | } | |
548 | break; | |
549 | case 3:{ | |
550 | c = chan_out_config; | |
551 | mapping = devpriv->analog_out_mapping; | |
552 | range = devpriv->out_range; | |
553 | kind = 4; | |
554 | } | |
555 | break; | |
556 | case 4:{ | |
557 | c = chan_in_config; | |
558 | mapping = devpriv->encoder_in_mapping; | |
559 | range = devpriv->in_range; | |
560 | kind = 5; | |
561 | } | |
562 | break; | |
563 | default:{ | |
c5da2090 | 564 | c = NULL; |
578c0183 AB |
565 | } |
566 | break; | |
567 | } | |
568 | if (c) { | |
34c43922 | 569 | struct comedi_subdevice *s; |
0a85b6f0 MT |
570 | const struct comedi_lrange **range_table_list = |
571 | NULL; | |
578c0183 AB |
572 | unsigned int *maxdata_list; |
573 | int j, chan; | |
574 | ||
575 | for (chan = 0, j = 0; j < 32; j++) { | |
e9d1cf89 | 576 | if (c[j].kind == kind) |
578c0183 | 577 | chan++; |
578c0183 AB |
578 | } |
579 | s = &dev->subdevices[i]; | |
580 | s->n_chan = chan; | |
581 | s->maxdata = 0; | |
9e7f2256 | 582 | kfree(s->maxdata_list); |
578c0183 | 583 | s->maxdata_list = maxdata_list = |
0a85b6f0 MT |
584 | kmalloc(sizeof(unsigned int) * s->n_chan, |
585 | GFP_KERNEL); | |
9e7f2256 IA |
586 | if (!s->maxdata_list) |
587 | break; /* error handled below */ | |
588 | kfree(s->range_table_list); | |
589 | s->range_table = NULL; | |
590 | s->range_table_list = NULL; | |
0edc7d83 IA |
591 | if (kind == 1 || kind == 2) { |
592 | s->range_table = &range_digital; | |
593 | } else if (range) { | |
578c0183 | 594 | s->range_table_list = range_table_list = |
0a85b6f0 MT |
595 | kmalloc(sizeof |
596 | (struct | |
597 | serial2002_range_table_t) * | |
598 | s->n_chan, GFP_KERNEL); | |
9e7f2256 IA |
599 | if (!s->range_table_list) |
600 | break; /* err handled below */ | |
578c0183 AB |
601 | } |
602 | for (chan = 0, j = 0; j < 32; j++) { | |
603 | if (c[j].kind == kind) { | |
e9d1cf89 | 604 | if (mapping) |
578c0183 | 605 | mapping[chan] = j; |
578c0183 AB |
606 | if (range) { |
607 | range[j].length = 1; | |
608 | range[j].range.min = | |
0a85b6f0 | 609 | c[j].min; |
578c0183 | 610 | range[j].range.max = |
0a85b6f0 | 611 | c[j].max; |
578c0183 | 612 | range_table_list[chan] = |
0a85b6f0 MT |
613 | (const struct |
614 | comedi_lrange *) | |
615 | &range[j]; | |
578c0183 AB |
616 | } |
617 | maxdata_list[chan] = | |
0a85b6f0 MT |
618 | ((long long)1 << c[j].bits) |
619 | - 1; | |
578c0183 AB |
620 | chan++; |
621 | } | |
622 | } | |
623 | } | |
624 | } | |
9e7f2256 IA |
625 | if (i <= 4) { |
626 | /* Failed to allocate maxdata_list or range_table_list | |
627 | * for a subdevice that needed it. */ | |
628 | result = -ENOMEM; | |
629 | for (i = 0; i <= 4; i++) { | |
630 | struct comedi_subdevice *s; | |
631 | ||
632 | s = &dev->subdevices[i]; | |
633 | kfree(s->maxdata_list); | |
634 | s->maxdata_list = NULL; | |
635 | kfree(s->range_table_list); | |
636 | s->range_table_list = NULL; | |
637 | } | |
638 | } | |
668f272e IA |
639 | |
640 | err_alloc_configs: | |
641 | kfree(dig_in_config); | |
642 | kfree(dig_out_config); | |
643 | kfree(chan_in_config); | |
644 | kfree(chan_out_config); | |
645 | ||
9e7f2256 IA |
646 | if (result) { |
647 | if (devpriv->tty) { | |
27494c09 | 648 | filp_close(devpriv->tty, NULL); |
9e7f2256 IA |
649 | devpriv->tty = NULL; |
650 | } | |
651 | } | |
578c0183 | 652 | } |
3c17ba07 | 653 | return result; |
578c0183 AB |
654 | } |
655 | ||
da91b269 | 656 | static void serial_2002_close(struct comedi_device *dev) |
578c0183 | 657 | { |
27494c09 HS |
658 | if (!IS_ERR(devpriv->tty) && devpriv->tty) |
659 | filp_close(devpriv->tty, NULL); | |
578c0183 AB |
660 | } |
661 | ||
0a85b6f0 MT |
662 | static int serial2002_di_rinsn(struct comedi_device *dev, |
663 | struct comedi_subdevice *s, | |
664 | struct comedi_insn *insn, unsigned int *data) | |
578c0183 AB |
665 | { |
666 | int n; | |
667 | int chan; | |
668 | ||
669 | chan = devpriv->digital_in_mapping[CR_CHAN(insn->chanspec)]; | |
670 | for (n = 0; n < insn->n; n++) { | |
671 | struct serial_data read; | |
672 | ||
673 | poll_digital(devpriv->tty, chan); | |
674 | while (1) { | |
675 | read = serial_read(devpriv->tty, 1000); | |
e9d1cf89 | 676 | if (read.kind != is_digital || read.index == chan) |
578c0183 | 677 | break; |
578c0183 AB |
678 | } |
679 | data[n] = read.value; | |
680 | } | |
681 | return n; | |
682 | } | |
683 | ||
0a85b6f0 MT |
684 | static int serial2002_do_winsn(struct comedi_device *dev, |
685 | struct comedi_subdevice *s, | |
686 | struct comedi_insn *insn, unsigned int *data) | |
578c0183 AB |
687 | { |
688 | int n; | |
689 | int chan; | |
690 | ||
691 | chan = devpriv->digital_out_mapping[CR_CHAN(insn->chanspec)]; | |
692 | for (n = 0; n < insn->n; n++) { | |
693 | struct serial_data write; | |
694 | ||
695 | write.kind = is_digital; | |
696 | write.index = chan; | |
697 | write.value = data[n]; | |
698 | serial_write(devpriv->tty, write); | |
699 | } | |
700 | return n; | |
701 | } | |
702 | ||
0a85b6f0 MT |
703 | static int serial2002_ai_rinsn(struct comedi_device *dev, |
704 | struct comedi_subdevice *s, | |
705 | struct comedi_insn *insn, unsigned int *data) | |
578c0183 AB |
706 | { |
707 | int n; | |
708 | int chan; | |
709 | ||
710 | chan = devpriv->analog_in_mapping[CR_CHAN(insn->chanspec)]; | |
711 | for (n = 0; n < insn->n; n++) { | |
712 | struct serial_data read; | |
713 | ||
714 | poll_channel(devpriv->tty, chan); | |
715 | while (1) { | |
716 | read = serial_read(devpriv->tty, 1000); | |
e9d1cf89 | 717 | if (read.kind != is_channel || read.index == chan) |
578c0183 | 718 | break; |
578c0183 AB |
719 | } |
720 | data[n] = read.value; | |
721 | } | |
722 | return n; | |
723 | } | |
724 | ||
0a85b6f0 MT |
725 | static int serial2002_ao_winsn(struct comedi_device *dev, |
726 | struct comedi_subdevice *s, | |
727 | struct comedi_insn *insn, unsigned int *data) | |
578c0183 AB |
728 | { |
729 | int n; | |
730 | int chan; | |
731 | ||
732 | chan = devpriv->analog_out_mapping[CR_CHAN(insn->chanspec)]; | |
733 | for (n = 0; n < insn->n; n++) { | |
734 | struct serial_data write; | |
735 | ||
736 | write.kind = is_channel; | |
737 | write.index = chan; | |
738 | write.value = data[n]; | |
739 | serial_write(devpriv->tty, write); | |
740 | devpriv->ao_readback[chan] = data[n]; | |
741 | } | |
742 | return n; | |
743 | } | |
744 | ||
0a85b6f0 MT |
745 | static int serial2002_ao_rinsn(struct comedi_device *dev, |
746 | struct comedi_subdevice *s, | |
747 | struct comedi_insn *insn, unsigned int *data) | |
578c0183 AB |
748 | { |
749 | int n; | |
750 | int chan = CR_CHAN(insn->chanspec); | |
751 | ||
e9d1cf89 | 752 | for (n = 0; n < insn->n; n++) |
578c0183 | 753 | data[n] = devpriv->ao_readback[chan]; |
578c0183 AB |
754 | |
755 | return n; | |
756 | } | |
757 | ||
0a85b6f0 MT |
758 | static int serial2002_ei_rinsn(struct comedi_device *dev, |
759 | struct comedi_subdevice *s, | |
760 | struct comedi_insn *insn, unsigned int *data) | |
578c0183 AB |
761 | { |
762 | int n; | |
763 | int chan; | |
764 | ||
765 | chan = devpriv->encoder_in_mapping[CR_CHAN(insn->chanspec)]; | |
766 | for (n = 0; n < insn->n; n++) { | |
767 | struct serial_data read; | |
768 | ||
769 | poll_channel(devpriv->tty, chan); | |
770 | while (1) { | |
771 | read = serial_read(devpriv->tty, 1000); | |
e9d1cf89 | 772 | if (read.kind != is_channel || read.index == chan) |
578c0183 | 773 | break; |
578c0183 AB |
774 | } |
775 | data[n] = read.value; | |
776 | } | |
777 | return n; | |
778 | } | |
779 | ||
0a85b6f0 MT |
780 | static int serial2002_attach(struct comedi_device *dev, |
781 | struct comedi_devconfig *it) | |
578c0183 | 782 | { |
609dd342 | 783 | const struct serial2002_board *board = comedi_board(dev); |
34c43922 | 784 | struct comedi_subdevice *s; |
8b6c5694 | 785 | int ret; |
578c0183 | 786 | |
f41ad667 | 787 | dev_dbg(dev->class_dev, "serial2002: attach\n"); |
609dd342 | 788 | dev->board_name = board->name; |
e9d1cf89 | 789 | if (alloc_private(dev, sizeof(struct serial2002_private)) < 0) |
578c0183 | 790 | return -ENOMEM; |
578c0183 AB |
791 | dev->open = serial_2002_open; |
792 | dev->close = serial_2002_close; | |
793 | devpriv->port = it->options[0]; | |
794 | devpriv->speed = it->options[1]; | |
f41ad667 | 795 | dev_dbg(dev->class_dev, "/dev/ttyS%d @ %d\n", devpriv->port, |
3dbeb83c | 796 | devpriv->speed); |
578c0183 | 797 | |
8b6c5694 HS |
798 | ret = comedi_alloc_subdevices(dev, 5); |
799 | if (ret) | |
800 | return ret; | |
578c0183 AB |
801 | |
802 | /* digital input subdevice */ | |
803 | s = dev->subdevices + 0; | |
804 | s->type = COMEDI_SUBD_DI; | |
805 | s->subdev_flags = SDF_READABLE; | |
806 | s->n_chan = 0; | |
807 | s->maxdata = 1; | |
808 | s->range_table = &range_digital; | |
809 | s->insn_read = &serial2002_di_rinsn; | |
810 | ||
811 | /* digital output subdevice */ | |
812 | s = dev->subdevices + 1; | |
813 | s->type = COMEDI_SUBD_DO; | |
814 | s->subdev_flags = SDF_WRITEABLE; | |
815 | s->n_chan = 0; | |
816 | s->maxdata = 1; | |
817 | s->range_table = &range_digital; | |
818 | s->insn_write = &serial2002_do_winsn; | |
819 | ||
820 | /* analog input subdevice */ | |
821 | s = dev->subdevices + 2; | |
822 | s->type = COMEDI_SUBD_AI; | |
823 | s->subdev_flags = SDF_READABLE | SDF_GROUND; | |
824 | s->n_chan = 0; | |
825 | s->maxdata = 1; | |
27494c09 | 826 | s->range_table = NULL; |
578c0183 AB |
827 | s->insn_read = &serial2002_ai_rinsn; |
828 | ||
829 | /* analog output subdevice */ | |
830 | s = dev->subdevices + 3; | |
831 | s->type = COMEDI_SUBD_AO; | |
832 | s->subdev_flags = SDF_WRITEABLE; | |
833 | s->n_chan = 0; | |
834 | s->maxdata = 1; | |
27494c09 | 835 | s->range_table = NULL; |
578c0183 AB |
836 | s->insn_write = &serial2002_ao_winsn; |
837 | s->insn_read = &serial2002_ao_rinsn; | |
838 | ||
839 | /* encoder input subdevice */ | |
840 | s = dev->subdevices + 4; | |
841 | s->type = COMEDI_SUBD_COUNTER; | |
842 | s->subdev_flags = SDF_READABLE | SDF_LSAMPL; | |
843 | s->n_chan = 0; | |
844 | s->maxdata = 1; | |
27494c09 | 845 | s->range_table = NULL; |
578c0183 AB |
846 | s->insn_read = &serial2002_ei_rinsn; |
847 | ||
848 | return 1; | |
849 | } | |
850 | ||
484ecc95 | 851 | static void serial2002_detach(struct comedi_device *dev) |
578c0183 | 852 | { |
34c43922 | 853 | struct comedi_subdevice *s; |
578c0183 AB |
854 | int i; |
855 | ||
fa3b5d9a | 856 | for (i = 0; i < 5; i++) { |
578c0183 | 857 | s = &dev->subdevices[i]; |
e4e1f289 IM |
858 | kfree(s->maxdata_list); |
859 | kfree(s->range_table_list); | |
578c0183 | 860 | } |
578c0183 AB |
861 | } |
862 | ||
cb1648b4 HS |
863 | static const struct serial2002_board serial2002_boards[] = { |
864 | { | |
865 | .name = "serial2002" | |
866 | }, | |
867 | }; | |
868 | ||
294f930d | 869 | static struct comedi_driver serial2002_driver = { |
cb1648b4 HS |
870 | .driver_name = "serial2002", |
871 | .module = THIS_MODULE, | |
872 | .attach = serial2002_attach, | |
873 | .detach = serial2002_detach, | |
874 | .board_name = &serial2002_boards[0].name, | |
875 | .offset = sizeof(struct serial2002_board), | |
876 | .num_names = ARRAY_SIZE(serial2002_boards), | |
877 | }; | |
294f930d | 878 | module_comedi_driver(serial2002_driver); |
90f703d3 AT |
879 | |
880 | MODULE_AUTHOR("Comedi http://www.comedi.org"); | |
881 | MODULE_DESCRIPTION("Comedi low-level driver"); | |
882 | MODULE_LICENSE("GPL"); |