2 comedi/drivers/serial2002.c
3 Skeleton code for a Comedi driver
5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 2002 Anders Blomdell <anders.blomdell@control.lth.se>
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.
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.
21 Description: Driver for serial connected hardware
23 Author: Anders Blomdell
24 Updated: Fri, 7 Jun 2002 12:56:45 -0700
25 Status: in development
29 #include <linux/module.h>
30 #include "../comedidev.h"
32 #include <linux/delay.h>
33 #include <linux/sched.h>
34 #include <linux/slab.h>
36 #include <linux/termios.h>
37 #include <asm/ioctls.h>
38 #include <linux/serial.h>
39 #include <linux/poll.h>
41 struct serial2002_range_table_t
{
44 struct comedi_krange range
;
47 struct serial2002_private
{
48 int port
; /* /dev/ttyS<port> */
49 int speed
; /* baudrate */
51 unsigned int ao_readback
[32];
52 unsigned char digital_in_mapping
[32];
53 unsigned char digital_out_mapping
[32];
54 unsigned char analog_in_mapping
[32];
55 unsigned char analog_out_mapping
[32];
56 unsigned char encoder_in_mapping
[32];
57 struct serial2002_range_table_t in_range
[32], out_range
[32];
61 enum { is_invalid
, is_digital
, is_channel
} kind
;
67 * The configuration serial_data.value read from the device is
68 * a bitmask that defines specific options of a channel:
70 * 4:0 - the channel to configure
71 * 7:5 - the kind of channel
72 * 9:8 - the command used to configure the channel
74 * The remaining bits vary in use depending on the command:
76 * BITS 15:10 - the channel bits (maxdata)
77 * MIN/MAX 12:10 - the units multiplier for the scale
78 * 13 - the sign of the scale
79 * 33:14 - the base value for the range
81 #define S2002_CFG_CHAN(x) ((x) & 0x1f)
82 #define S2002_CFG_KIND(x) (((x) >> 5) & 0x7)
83 #define S2002_CFG_KIND_INVALID 0
84 #define S2002_CFG_KIND_DIGITAL_IN 1
85 #define S2002_CFG_KIND_DIGITAL_OUT 2
86 #define S2002_CFG_KIND_ANALOG_IN 3
87 #define S2002_CFG_KIND_ANALOG_OUT 4
88 #define S2002_CFG_KIND_ENCODER_IN 5
89 #define S2002_CFG_CMD(x) (((x) >> 8) & 0x3)
90 #define S2002_CFG_CMD_BITS 0
91 #define S2002_CFG_CMD_MIN 1
92 #define S2002_CFG_CMD_MAX 2
93 #define S2002_CFG_BITS(x) (((x) >> 10) & 0x3f)
94 #define S2002_CFG_UNITS(x) (((x) >> 10) & 0x7)
95 #define S2002_CFG_SIGN(x) (((x) >> 13) & 0x1)
96 #define S2002_CFG_BASE(x) (((x) >> 14) & 0xfffff)
98 static long serial2002_tty_ioctl(struct file
*f
, unsigned op
,
101 if (f
->f_op
->unlocked_ioctl
)
102 return f
->f_op
->unlocked_ioctl(f
, op
, param
);
107 static int serial2002_tty_write(struct file
*f
, unsigned char *buf
, int count
)
109 const char __user
*p
= (__force
const char __user
*)buf
;
116 result
= __vfs_write(f
, p
, count
, &offset
);
121 static void serial2002_tty_read_poll_wait(struct file
*f
, int timeout
)
123 struct poll_wqueues table
;
124 struct timeval start
, now
;
126 do_gettimeofday(&start
);
127 poll_initwait(&table
);
132 mask
= f
->f_op
->poll(f
, &table
.pt
);
133 if (mask
& (POLLRDNORM
| POLLRDBAND
| POLLIN
|
134 POLLHUP
| POLLERR
)) {
137 do_gettimeofday(&now
);
138 elapsed
= 1000000 * (now
.tv_sec
- start
.tv_sec
) +
139 now
.tv_usec
- start
.tv_usec
;
140 if (elapsed
> timeout
)
142 set_current_state(TASK_INTERRUPTIBLE
);
143 schedule_timeout(((timeout
- elapsed
) * HZ
) / 10000);
145 poll_freewait(&table
);
148 static int serial2002_tty_read(struct file
*f
, int timeout
)
156 char __user
*p
= (__force
char __user
*)&ch
;
162 serial2002_tty_read_poll_wait(f
, timeout
);
164 if (__vfs_read(f
, p
, 1, &offset
) == 1)
167 /* Device does not support poll, busy wait */
172 if (retries
>= timeout
)
175 if (__vfs_read(f
, p
, 1, &offset
) == 1) {
187 static void serial2002_tty_setspeed(struct file
*f
, int speed
)
189 struct termios termios
;
190 struct serial_struct serial
;
197 serial2002_tty_ioctl(f
, TCGETS
, (unsigned long)&termios
);
201 termios
.c_cflag
= CLOCAL
| CS8
| CREAD
;
202 termios
.c_cc
[VMIN
] = 0;
203 termios
.c_cc
[VTIME
] = 0;
206 termios
.c_cflag
|= B2400
;
209 termios
.c_cflag
|= B4800
;
212 termios
.c_cflag
|= B9600
;
215 termios
.c_cflag
|= B19200
;
218 termios
.c_cflag
|= B38400
;
221 termios
.c_cflag
|= B57600
;
224 termios
.c_cflag
|= B115200
;
227 termios
.c_cflag
|= B9600
;
230 serial2002_tty_ioctl(f
, TCSETS
, (unsigned long)&termios
);
232 /* Set low latency */
233 serial2002_tty_ioctl(f
, TIOCGSERIAL
, (unsigned long)&serial
);
234 serial
.flags
|= ASYNC_LOW_LATENCY
;
235 serial2002_tty_ioctl(f
, TIOCSSERIAL
, (unsigned long)&serial
);
240 static void serial2002_poll_digital(struct file
*f
, int channel
)
244 cmd
= 0x40 | (channel
& 0x1f);
245 serial2002_tty_write(f
, &cmd
, 1);
248 static void serial2002_poll_channel(struct file
*f
, int channel
)
252 cmd
= 0x60 | (channel
& 0x1f);
253 serial2002_tty_write(f
, &cmd
, 1);
256 static struct serial_data
serial2002_read(struct file
*f
, int timeout
)
258 struct serial_data result
;
261 result
.kind
= is_invalid
;
266 int data
= serial2002_tty_read(f
, timeout
);
271 } else if (data
& 0x80) {
272 result
.value
= (result
.value
<< 7) | (data
& 0x7f);
275 switch ((data
>> 5) & 0x03) {
278 result
.kind
= is_digital
;
282 result
.kind
= is_digital
;
287 (result
.value
<< 2) | ((data
& 0x60) >> 5);
288 result
.kind
= is_channel
;
290 result
.index
= data
& 0x1f;
297 static void serial2002_write(struct file
*f
, struct serial_data data
)
299 if (data
.kind
== is_digital
) {
301 ((data
.value
<< 5) & 0x20) | (data
.index
& 0x1f);
302 serial2002_tty_write(f
, &ch
, 1);
307 if (data
.value
>= (1L << 30)) {
308 ch
[i
] = 0x80 | ((data
.value
>> 30) & 0x03);
311 if (data
.value
>= (1L << 23)) {
312 ch
[i
] = 0x80 | ((data
.value
>> 23) & 0x7f);
315 if (data
.value
>= (1L << 16)) {
316 ch
[i
] = 0x80 | ((data
.value
>> 16) & 0x7f);
319 if (data
.value
>= (1L << 9)) {
320 ch
[i
] = 0x80 | ((data
.value
>> 9) & 0x7f);
323 ch
[i
] = 0x80 | ((data
.value
>> 2) & 0x7f);
325 ch
[i
] = ((data
.value
<< 5) & 0x60) | (data
.index
& 0x1f);
327 serial2002_tty_write(f
, ch
, i
);
338 static int serial2002_setup_subdevice(struct comedi_subdevice
*s
,
339 struct config_t
*cfg
,
340 struct serial2002_range_table_t
*range
,
341 unsigned char *mapping
,
344 const struct comedi_lrange
**range_table_list
= NULL
;
345 unsigned int *maxdata_list
;
348 for (chan
= 0, j
= 0; j
< 32; j
++) {
349 if (cfg
[j
].kind
== kind
)
354 kfree(s
->maxdata_list
);
355 maxdata_list
= kmalloc_array(s
->n_chan
, sizeof(unsigned int),
359 s
->maxdata_list
= maxdata_list
;
360 kfree(s
->range_table_list
);
361 s
->range_table
= NULL
;
362 s
->range_table_list
= NULL
;
363 if (kind
== 1 || kind
== 2) {
364 s
->range_table
= &range_digital
;
366 range_table_list
= kmalloc_array(s
->n_chan
, sizeof(*range
),
368 if (!range_table_list
)
370 s
->range_table_list
= range_table_list
;
372 for (chan
= 0, j
= 0; j
< 32; j
++) {
373 if (cfg
[j
].kind
== kind
) {
376 if (range
&& range_table_list
) {
378 range
[j
].range
.min
= cfg
[j
].min
;
379 range
[j
].range
.max
= cfg
[j
].max
;
380 range_table_list
[chan
] =
381 (const struct comedi_lrange
*)&range
[j
];
383 maxdata_list
[chan
] = ((long long)1 << cfg
[j
].bits
) - 1;
390 static int serial2002_setup_subdevs(struct comedi_device
*dev
)
392 struct serial2002_private
*devpriv
= dev
->private;
393 struct config_t
*di_cfg
;
394 struct config_t
*do_cfg
;
395 struct config_t
*ai_cfg
;
396 struct config_t
*ao_cfg
;
397 struct config_t
*cfg
;
398 struct comedi_subdevice
*s
;
402 /* Allocate the temporary structs to hold the configuration data */
403 di_cfg
= kcalloc(32, sizeof(*cfg
), GFP_KERNEL
);
404 do_cfg
= kcalloc(32, sizeof(*cfg
), GFP_KERNEL
);
405 ai_cfg
= kcalloc(32, sizeof(*cfg
), GFP_KERNEL
);
406 ao_cfg
= kcalloc(32, sizeof(*cfg
), GFP_KERNEL
);
407 if (!di_cfg
|| !do_cfg
|| !ai_cfg
|| !ao_cfg
) {
409 goto err_alloc_configs
;
412 /* Read the configuration from the connected device */
413 serial2002_tty_setspeed(devpriv
->tty
, devpriv
->speed
);
414 serial2002_poll_channel(devpriv
->tty
, 31);
416 struct serial_data data
= serial2002_read(devpriv
->tty
, 1000);
417 int kind
= S2002_CFG_KIND(data
.value
);
418 int channel
= S2002_CFG_CHAN(data
.value
);
419 int range
= S2002_CFG_BASE(data
.value
);
420 int cmd
= S2002_CFG_CMD(data
.value
);
422 if (data
.kind
!= is_channel
|| data
.index
!= 31 ||
423 kind
== S2002_CFG_KIND_INVALID
)
427 case S2002_CFG_KIND_DIGITAL_IN
:
430 case S2002_CFG_KIND_DIGITAL_OUT
:
433 case S2002_CFG_KIND_ANALOG_IN
:
436 case S2002_CFG_KIND_ANALOG_OUT
:
439 case S2002_CFG_KIND_ENCODER_IN
:
447 continue; /* unknown kind, skip it */
449 cfg
[channel
].kind
= kind
;
452 case S2002_CFG_CMD_BITS
:
453 cfg
[channel
].bits
= S2002_CFG_BITS(data
.value
);
455 case S2002_CFG_CMD_MIN
:
456 case S2002_CFG_CMD_MAX
:
457 switch (S2002_CFG_UNITS(data
.value
)) {
468 if (S2002_CFG_SIGN(data
.value
))
470 if (cmd
== S2002_CFG_CMD_MIN
)
471 cfg
[channel
].min
= range
;
473 cfg
[channel
].max
= range
;
478 /* Fill in subdevice data */
479 for (i
= 0; i
<= 4; i
++) {
480 unsigned char *mapping
= NULL
;
481 struct serial2002_range_table_t
*range
= NULL
;
484 s
= &dev
->subdevices
[i
];
489 mapping
= devpriv
->digital_in_mapping
;
490 kind
= S2002_CFG_KIND_DIGITAL_IN
;
494 mapping
= devpriv
->digital_out_mapping
;
495 kind
= S2002_CFG_KIND_DIGITAL_OUT
;
499 mapping
= devpriv
->analog_in_mapping
;
500 range
= devpriv
->in_range
;
501 kind
= S2002_CFG_KIND_ANALOG_IN
;
505 mapping
= devpriv
->analog_out_mapping
;
506 range
= devpriv
->out_range
;
507 kind
= S2002_CFG_KIND_ANALOG_OUT
;
511 mapping
= devpriv
->encoder_in_mapping
;
512 range
= devpriv
->in_range
;
513 kind
= S2002_CFG_KIND_ENCODER_IN
;
517 if (serial2002_setup_subdevice(s
, cfg
, range
, mapping
, kind
))
518 break; /* err handled below */
522 * Failed to allocate maxdata_list or range_table_list
523 * for a subdevice that needed it.
526 for (i
= 0; i
<= 4; i
++) {
527 s
= &dev
->subdevices
[i
];
528 kfree(s
->maxdata_list
);
529 s
->maxdata_list
= NULL
;
530 kfree(s
->range_table_list
);
531 s
->range_table_list
= NULL
;
543 filp_close(devpriv
->tty
, NULL
);
551 static int serial2002_open(struct comedi_device
*dev
)
553 struct serial2002_private
*devpriv
= dev
->private;
557 sprintf(port
, "/dev/ttyS%d", devpriv
->port
);
558 devpriv
->tty
= filp_open(port
, O_RDWR
, 0);
559 if (IS_ERR(devpriv
->tty
)) {
560 result
= (int)PTR_ERR(devpriv
->tty
);
561 dev_err(dev
->class_dev
, "file open error = %d\n", result
);
563 result
= serial2002_setup_subdevs(dev
);
568 static void serial2002_close(struct comedi_device
*dev
)
570 struct serial2002_private
*devpriv
= dev
->private;
572 if (!IS_ERR(devpriv
->tty
) && devpriv
->tty
)
573 filp_close(devpriv
->tty
, NULL
);
576 static int serial2002_di_insn_read(struct comedi_device
*dev
,
577 struct comedi_subdevice
*s
,
578 struct comedi_insn
*insn
,
581 struct serial2002_private
*devpriv
= dev
->private;
585 chan
= devpriv
->digital_in_mapping
[CR_CHAN(insn
->chanspec
)];
586 for (n
= 0; n
< insn
->n
; n
++) {
587 struct serial_data read
;
589 serial2002_poll_digital(devpriv
->tty
, chan
);
591 read
= serial2002_read(devpriv
->tty
, 1000);
592 if (read
.kind
!= is_digital
|| read
.index
== chan
)
595 data
[n
] = read
.value
;
600 static int serial2002_do_insn_write(struct comedi_device
*dev
,
601 struct comedi_subdevice
*s
,
602 struct comedi_insn
*insn
,
605 struct serial2002_private
*devpriv
= dev
->private;
609 chan
= devpriv
->digital_out_mapping
[CR_CHAN(insn
->chanspec
)];
610 for (n
= 0; n
< insn
->n
; n
++) {
611 struct serial_data write
;
613 write
.kind
= is_digital
;
615 write
.value
= data
[n
];
616 serial2002_write(devpriv
->tty
, write
);
621 static int serial2002_ai_insn_read(struct comedi_device
*dev
,
622 struct comedi_subdevice
*s
,
623 struct comedi_insn
*insn
,
626 struct serial2002_private
*devpriv
= dev
->private;
630 chan
= devpriv
->analog_in_mapping
[CR_CHAN(insn
->chanspec
)];
631 for (n
= 0; n
< insn
->n
; n
++) {
632 struct serial_data read
;
634 serial2002_poll_channel(devpriv
->tty
, chan
);
636 read
= serial2002_read(devpriv
->tty
, 1000);
637 if (read
.kind
!= is_channel
|| read
.index
== chan
)
640 data
[n
] = read
.value
;
645 static int serial2002_ao_insn_write(struct comedi_device
*dev
,
646 struct comedi_subdevice
*s
,
647 struct comedi_insn
*insn
,
650 struct serial2002_private
*devpriv
= dev
->private;
654 chan
= devpriv
->analog_out_mapping
[CR_CHAN(insn
->chanspec
)];
655 for (n
= 0; n
< insn
->n
; n
++) {
656 struct serial_data write
;
658 write
.kind
= is_channel
;
660 write
.value
= data
[n
];
661 serial2002_write(devpriv
->tty
, write
);
662 devpriv
->ao_readback
[chan
] = data
[n
];
667 static int serial2002_ao_insn_read(struct comedi_device
*dev
,
668 struct comedi_subdevice
*s
,
669 struct comedi_insn
*insn
,
672 struct serial2002_private
*devpriv
= dev
->private;
674 int chan
= CR_CHAN(insn
->chanspec
);
676 for (n
= 0; n
< insn
->n
; n
++)
677 data
[n
] = devpriv
->ao_readback
[chan
];
682 static int serial2002_encoder_insn_read(struct comedi_device
*dev
,
683 struct comedi_subdevice
*s
,
684 struct comedi_insn
*insn
,
687 struct serial2002_private
*devpriv
= dev
->private;
691 chan
= devpriv
->encoder_in_mapping
[CR_CHAN(insn
->chanspec
)];
692 for (n
= 0; n
< insn
->n
; n
++) {
693 struct serial_data read
;
695 serial2002_poll_channel(devpriv
->tty
, chan
);
697 read
= serial2002_read(devpriv
->tty
, 1000);
698 if (read
.kind
!= is_channel
|| read
.index
== chan
)
701 data
[n
] = read
.value
;
706 static int serial2002_attach(struct comedi_device
*dev
,
707 struct comedi_devconfig
*it
)
709 struct serial2002_private
*devpriv
;
710 struct comedi_subdevice
*s
;
713 devpriv
= comedi_alloc_devpriv(dev
, sizeof(*devpriv
));
717 devpriv
->port
= it
->options
[0];
718 devpriv
->speed
= it
->options
[1];
720 ret
= comedi_alloc_subdevices(dev
, 5);
724 /* digital input subdevice */
725 s
= &dev
->subdevices
[0];
726 s
->type
= COMEDI_SUBD_DI
;
727 s
->subdev_flags
= SDF_READABLE
;
730 s
->range_table
= &range_digital
;
731 s
->insn_read
= serial2002_di_insn_read
;
733 /* digital output subdevice */
734 s
= &dev
->subdevices
[1];
735 s
->type
= COMEDI_SUBD_DO
;
736 s
->subdev_flags
= SDF_WRITABLE
;
739 s
->range_table
= &range_digital
;
740 s
->insn_write
= serial2002_do_insn_write
;
742 /* analog input subdevice */
743 s
= &dev
->subdevices
[2];
744 s
->type
= COMEDI_SUBD_AI
;
745 s
->subdev_flags
= SDF_READABLE
| SDF_GROUND
;
748 s
->range_table
= NULL
;
749 s
->insn_read
= serial2002_ai_insn_read
;
751 /* analog output subdevice */
752 s
= &dev
->subdevices
[3];
753 s
->type
= COMEDI_SUBD_AO
;
754 s
->subdev_flags
= SDF_WRITABLE
;
757 s
->range_table
= NULL
;
758 s
->insn_write
= serial2002_ao_insn_write
;
759 s
->insn_read
= serial2002_ao_insn_read
;
761 /* encoder input subdevice */
762 s
= &dev
->subdevices
[4];
763 s
->type
= COMEDI_SUBD_COUNTER
;
764 s
->subdev_flags
= SDF_READABLE
| SDF_LSAMPL
;
767 s
->range_table
= NULL
;
768 s
->insn_read
= serial2002_encoder_insn_read
;
770 dev
->open
= serial2002_open
;
771 dev
->close
= serial2002_close
;
776 static void serial2002_detach(struct comedi_device
*dev
)
778 struct comedi_subdevice
*s
;
781 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
782 s
= &dev
->subdevices
[i
];
783 kfree(s
->maxdata_list
);
784 kfree(s
->range_table_list
);
788 static struct comedi_driver serial2002_driver
= {
789 .driver_name
= "serial2002",
790 .module
= THIS_MODULE
,
791 .attach
= serial2002_attach
,
792 .detach
= serial2002_detach
,
794 module_comedi_driver(serial2002_driver
);
796 MODULE_AUTHOR("Comedi http://www.comedi.org");
797 MODULE_DESCRIPTION("Comedi low-level driver");
798 MODULE_LICENSE("GPL");