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.
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.
26 Description: Driver for serial connected hardware
28 Author: Anders Blomdell
29 Updated: Fri, 7 Jun 2002 12:56:45 -0700
30 Status: in development
34 #include "../comedidev.h"
36 #include <linux/delay.h>
37 #include <linux/ioport.h>
39 #include <asm/termios.h>
40 #include <asm/ioctls.h>
41 #include <linux/serial.h>
42 #include <linux/poll.h>
45 * Board descriptions for two imaginary boards. Describing the
46 * boards in this way is optional, and completely driver-dependent.
47 * Some drivers use arrays such as this, other do not.
49 typedef struct serial2002_board_struct
{
53 static const serial2002_board serial2002_boards
[] = {
59 * Useful for shorthand access to the particular board structure
61 #define thisboard ((const serial2002_board *)dev->board_ptr)
67 } serial2002_range_table_t
;
70 int port
; // /dev/ttyS<port>
71 int speed
; // baudrate
73 unsigned int ao_readback
[32];
74 unsigned char digital_in_mapping
[32];
75 unsigned char digital_out_mapping
[32];
76 unsigned char analog_in_mapping
[32];
77 unsigned char analog_out_mapping
[32];
78 unsigned char encoder_in_mapping
[32];
79 serial2002_range_table_t in_range
[32], out_range
[32];
83 * most drivers define the following macro to make it easy to
84 * access the private structure.
86 #define devpriv ((serial2002_private *)dev->private)
88 static int serial2002_attach(struct comedi_device
* dev
, comedi_devconfig
* it
);
89 static int serial2002_detach(struct comedi_device
* dev
);
90 struct comedi_driver driver_serial2002
= {
91 driver_name
:"serial2002",
93 attach
:serial2002_attach
,
94 detach
:serial2002_detach
,
95 board_name
:&serial2002_boards
[0].name
,
96 offset
:sizeof(serial2002_board
),
97 num_names
:sizeof(serial2002_boards
) / sizeof(serial2002_board
),
100 static int serial2002_di_rinsn(struct comedi_device
* dev
, struct comedi_subdevice
* s
,
101 struct comedi_insn
* insn
, unsigned int * data
);
102 static int serial2002_do_winsn(struct comedi_device
* dev
, struct comedi_subdevice
* s
,
103 struct comedi_insn
* insn
, unsigned int * data
);
104 static int serial2002_ai_rinsn(struct comedi_device
* dev
, struct comedi_subdevice
* s
,
105 struct comedi_insn
* insn
, unsigned int * data
);
106 static int serial2002_ao_winsn(struct comedi_device
* dev
, struct comedi_subdevice
* s
,
107 struct comedi_insn
* insn
, unsigned int * data
);
108 static int serial2002_ao_rinsn(struct comedi_device
* dev
, struct comedi_subdevice
* s
,
109 struct comedi_insn
* insn
, unsigned int * data
);
112 enum { is_invalid
, is_digital
, is_channel
} kind
;
117 static long tty_ioctl(struct file
*f
, unsigned op
, unsigned long param
)
119 #ifdef HAVE_UNLOCKED_IOCTL
120 if (f
->f_op
->unlocked_ioctl
) {
121 return f
->f_op
->unlocked_ioctl(f
, op
, param
);
124 if (f
->f_op
->ioctl
) {
125 return f
->f_op
->ioctl(f
->f_dentry
->d_inode
, f
, op
, param
);
130 static int tty_write(struct file
*f
, unsigned char *buf
, int count
)
138 result
= f
->f_op
->write(f
, buf
, count
, &f
->f_pos
);
145 * On 2.6.26.3 this occaisonally gave me page faults, worked around by
146 * settings.c_cc[VMIN] = 0; settings.c_cc[VTIME] = 0
148 static int tty_available(struct file
*f
)
155 tty_ioctl(f
, FIONREAD
, (unsigned long)&result
);
161 static int tty_read(struct file
*f
, int timeout
)
172 struct poll_wqueues table
;
173 struct timeval start
, now
;
175 do_gettimeofday(&start
);
176 poll_initwait(&table
);
181 mask
= f
->f_op
->poll(f
, &table
.pt
);
182 if (mask
& (POLLRDNORM
| POLLRDBAND
| POLLIN
|
183 POLLHUP
| POLLERR
)) {
186 do_gettimeofday(&now
);
188 (1000000 * (now
.tv_sec
- start
.tv_sec
) +
189 now
.tv_usec
- start
.tv_usec
);
190 if (elapsed
> timeout
) {
193 set_current_state(TASK_INTERRUPTIBLE
);
194 schedule_timeout(((timeout
-
195 elapsed
) * HZ
) / 10000);
197 poll_freewait(&table
);
202 if (f
->f_op
->read(f
, &ch
, 1, &f
->f_pos
) == 1) {
207 /* Device does not support poll, busy wait */
213 if (retries
>= timeout
) {
218 if (f
->f_op
->read(f
, &ch
, 1, &f
->f_pos
) == 1) {
230 static void tty_setspeed(struct file
*f
, int speed
)
238 struct termios settings
;
240 tty_ioctl(f
, TCGETS
, (unsigned long)&settings
);
241 // printk("Speed: %d\n", settings.c_cflag & (CBAUD | CBAUDEX));
242 settings
.c_iflag
= 0;
243 settings
.c_oflag
= 0;
244 settings
.c_lflag
= 0;
245 settings
.c_cflag
= CLOCAL
| CS8
| CREAD
;
246 settings
.c_cc
[VMIN
] = 0;
247 settings
.c_cc
[VTIME
] = 0;
250 settings
.c_cflag
|= B2400
;
254 settings
.c_cflag
|= B4800
;
258 settings
.c_cflag
|= B9600
;
262 settings
.c_cflag
|= B19200
;
266 settings
.c_cflag
|= B38400
;
270 settings
.c_cflag
|= B57600
;
274 settings
.c_cflag
|= B115200
;
278 settings
.c_cflag
|= B9600
;
282 tty_ioctl(f
, TCSETS
, (unsigned long)&settings
);
283 // printk("Speed: %d\n", settings.c_cflag & (CBAUD | CBAUDEX));
287 struct serial_struct settings
;
289 tty_ioctl(f
, TIOCGSERIAL
, (unsigned long)&settings
);
290 settings
.flags
|= ASYNC_LOW_LATENCY
;
291 tty_ioctl(f
, TIOCSSERIAL
, (unsigned long)&settings
);
297 static void poll_digital(struct file
*f
, int channel
)
301 cmd
= 0x40 | (channel
& 0x1f);
302 tty_write(f
, &cmd
, 1);
305 static void poll_channel(struct file
*f
, int channel
)
309 cmd
= 0x60 | (channel
& 0x1f);
310 tty_write(f
, &cmd
, 1);
313 static struct serial_data
serial_read(struct file
*f
, int timeout
)
315 struct serial_data result
;
318 result
.kind
= is_invalid
;
323 int data
= tty_read(f
, timeout
);
327 printk("serial2002 error\n");
329 } else if (data
& 0x80) {
330 result
.value
= (result
.value
<< 7) | (data
& 0x7f);
333 switch ((data
>> 5) & 0x03) {
336 result
.kind
= is_digital
;
341 result
.kind
= is_digital
;
348 value
<< 2) | ((data
& 0x60) >> 5);
349 result
.kind
= is_channel
;
351 result
.index
= data
& 0x1f;
359 static void serial_write(struct file
*f
, struct serial_data data
)
361 if (data
.kind
== is_digital
) {
363 ((data
.value
<< 5) & 0x20) | (data
.index
& 0x1f);
364 tty_write(f
, &ch
, 1);
368 if (data
.value
>= (1L << 30)) {
369 ch
[i
] = 0x80 | ((data
.value
>> 30) & 0x03);
372 if (data
.value
>= (1L << 23)) {
373 ch
[i
] = 0x80 | ((data
.value
>> 23) & 0x7f);
376 if (data
.value
>= (1L << 16)) {
377 ch
[i
] = 0x80 | ((data
.value
>> 16) & 0x7f);
380 if (data
.value
>= (1L << 9)) {
381 ch
[i
] = 0x80 | ((data
.value
>> 9) & 0x7f);
384 ch
[i
] = 0x80 | ((data
.value
>> 2) & 0x7f);
386 ch
[i
] = ((data
.value
<< 5) & 0x60) | (data
.index
& 0x1f);
392 static void serial_2002_open(struct comedi_device
* dev
)
396 sprintf(port
, "/dev/ttyS%d", devpriv
->port
);
397 devpriv
->tty
= filp_open(port
, 0, O_RDWR
);
398 if (IS_ERR(devpriv
->tty
)) {
399 printk("serial_2002: file open error = %ld\n",
400 PTR_ERR(devpriv
->tty
));
408 config_t dig_in_config
[32];
409 config_t dig_out_config
[32];
410 config_t chan_in_config
[32];
411 config_t chan_out_config
[32];
414 for (i
= 0; i
< 32; i
++) {
415 dig_in_config
[i
].kind
= 0;
416 dig_in_config
[i
].bits
= 0;
417 dig_in_config
[i
].min
= 0;
418 dig_in_config
[i
].max
= 0;
419 dig_out_config
[i
].kind
= 0;
420 dig_out_config
[i
].bits
= 0;
421 dig_out_config
[i
].min
= 0;
422 dig_out_config
[i
].max
= 0;
423 chan_in_config
[i
].kind
= 0;
424 chan_in_config
[i
].bits
= 0;
425 chan_in_config
[i
].min
= 0;
426 chan_in_config
[i
].max
= 0;
427 chan_out_config
[i
].kind
= 0;
428 chan_out_config
[i
].bits
= 0;
429 chan_out_config
[i
].min
= 0;
430 chan_out_config
[i
].max
= 0;
433 tty_setspeed(devpriv
->tty
, devpriv
->speed
);
434 poll_channel(devpriv
->tty
, 31); // Start reading configuration
436 struct serial_data data
;
438 data
= serial_read(devpriv
->tty
, 1000);
439 if (data
.kind
!= is_channel
|| data
.index
!= 31
440 || !(data
.value
& 0xe0)) {
443 int command
, channel
, kind
;
444 config_t
*cur_config
= 0;
446 channel
= data
.value
& 0x1f;
447 kind
= (data
.value
>> 5) & 0x7;
448 command
= (data
.value
>> 8) & 0x3;
451 cur_config
= dig_in_config
;
455 cur_config
= dig_out_config
;
459 cur_config
= chan_in_config
;
463 cur_config
= chan_out_config
;
467 cur_config
= chan_in_config
;
473 cur_config
[channel
].kind
= kind
;
553 for (i
= 0; i
<= 4; i
++) {
554 // Fill in subdev data
556 unsigned char *mapping
= 0;
557 serial2002_range_table_t
*range
= 0;
563 mapping
= devpriv
->digital_in_mapping
;
569 mapping
= devpriv
->digital_out_mapping
;
575 mapping
= devpriv
->analog_in_mapping
;
576 range
= devpriv
->in_range
;
582 mapping
= devpriv
->analog_out_mapping
;
583 range
= devpriv
->out_range
;
589 mapping
= devpriv
->encoder_in_mapping
;
590 range
= devpriv
->in_range
;
600 struct comedi_subdevice
*s
;
601 const struct comedi_lrange
**range_table_list
= NULL
;
602 unsigned int *maxdata_list
;
605 for (chan
= 0, j
= 0; j
< 32; j
++) {
606 if (c
[j
].kind
== kind
) {
610 s
= &dev
->subdevices
[i
];
613 if (s
->maxdata_list
) {
614 kfree(s
->maxdata_list
);
616 s
->maxdata_list
= maxdata_list
=
617 kmalloc(sizeof(unsigned int) * s
->n_chan
,
619 if (s
->range_table_list
) {
620 kfree(s
->range_table_list
);
624 s
->range_table_list
= range_table_list
=
626 (serial2002_range_table_t
) *
627 s
->n_chan
, GFP_KERNEL
);
629 for (chan
= 0, j
= 0; j
< 32; j
++) {
630 if (c
[j
].kind
== kind
) {
640 range_table_list
[chan
] =
646 ((long long)1 << c
[j
].
656 static void serial_2002_close(struct comedi_device
* dev
)
658 if (!IS_ERR(devpriv
->tty
) && (devpriv
->tty
!= 0)) {
659 filp_close(devpriv
->tty
, 0);
663 static int serial2002_di_rinsn(struct comedi_device
* dev
, struct comedi_subdevice
* s
,
664 struct comedi_insn
* insn
, unsigned int * data
)
669 chan
= devpriv
->digital_in_mapping
[CR_CHAN(insn
->chanspec
)];
670 for (n
= 0; n
< insn
->n
; n
++) {
671 struct serial_data read
;
673 poll_digital(devpriv
->tty
, chan
);
675 read
= serial_read(devpriv
->tty
, 1000);
676 if (read
.kind
!= is_digital
|| read
.index
== chan
) {
680 data
[n
] = read
.value
;
685 static int serial2002_do_winsn(struct comedi_device
* dev
, struct comedi_subdevice
* s
,
686 struct comedi_insn
* insn
, unsigned int * data
)
691 chan
= devpriv
->digital_out_mapping
[CR_CHAN(insn
->chanspec
)];
692 for (n
= 0; n
< insn
->n
; n
++) {
693 struct serial_data write
;
695 write
.kind
= is_digital
;
697 write
.value
= data
[n
];
698 serial_write(devpriv
->tty
, write
);
703 static int serial2002_ai_rinsn(struct comedi_device
* dev
, struct comedi_subdevice
* s
,
704 struct comedi_insn
* insn
, unsigned int * data
)
709 chan
= devpriv
->analog_in_mapping
[CR_CHAN(insn
->chanspec
)];
710 for (n
= 0; n
< insn
->n
; n
++) {
711 struct serial_data read
;
713 poll_channel(devpriv
->tty
, chan
);
715 read
= serial_read(devpriv
->tty
, 1000);
716 if (read
.kind
!= is_channel
|| read
.index
== chan
) {
720 data
[n
] = read
.value
;
725 static int serial2002_ao_winsn(struct comedi_device
* dev
, struct comedi_subdevice
* s
,
726 struct comedi_insn
* insn
, unsigned int * data
)
731 chan
= devpriv
->analog_out_mapping
[CR_CHAN(insn
->chanspec
)];
732 for (n
= 0; n
< insn
->n
; n
++) {
733 struct serial_data write
;
735 write
.kind
= is_channel
;
737 write
.value
= data
[n
];
738 serial_write(devpriv
->tty
, write
);
739 devpriv
->ao_readback
[chan
] = data
[n
];
744 static int serial2002_ao_rinsn(struct comedi_device
* dev
, struct comedi_subdevice
* s
,
745 struct comedi_insn
* insn
, unsigned int * data
)
748 int chan
= CR_CHAN(insn
->chanspec
);
750 for (n
= 0; n
< insn
->n
; n
++) {
751 data
[n
] = devpriv
->ao_readback
[chan
];
757 static int serial2002_ei_rinsn(struct comedi_device
* dev
, struct comedi_subdevice
* s
,
758 struct comedi_insn
* insn
, unsigned int * data
)
763 chan
= devpriv
->encoder_in_mapping
[CR_CHAN(insn
->chanspec
)];
764 for (n
= 0; n
< insn
->n
; n
++) {
765 struct serial_data read
;
767 poll_channel(devpriv
->tty
, chan
);
769 read
= serial_read(devpriv
->tty
, 1000);
770 if (read
.kind
!= is_channel
|| read
.index
== chan
) {
774 data
[n
] = read
.value
;
779 static int serial2002_attach(struct comedi_device
* dev
, comedi_devconfig
* it
)
781 struct comedi_subdevice
*s
;
783 printk("comedi%d: serial2002: ", dev
->minor
);
784 dev
->board_name
= thisboard
->name
;
785 if (alloc_private(dev
, sizeof(serial2002_private
)) < 0) {
788 dev
->open
= serial_2002_open
;
789 dev
->close
= serial_2002_close
;
790 devpriv
->port
= it
->options
[0];
791 devpriv
->speed
= it
->options
[1];
792 printk("/dev/ttyS%d @ %d\n", devpriv
->port
, devpriv
->speed
);
794 if (alloc_subdevices(dev
, 5) < 0)
797 /* digital input subdevice */
798 s
= dev
->subdevices
+ 0;
799 s
->type
= COMEDI_SUBD_DI
;
800 s
->subdev_flags
= SDF_READABLE
;
803 s
->range_table
= &range_digital
;
804 s
->insn_read
= &serial2002_di_rinsn
;
806 /* digital output subdevice */
807 s
= dev
->subdevices
+ 1;
808 s
->type
= COMEDI_SUBD_DO
;
809 s
->subdev_flags
= SDF_WRITEABLE
;
812 s
->range_table
= &range_digital
;
813 s
->insn_write
= &serial2002_do_winsn
;
815 /* analog input subdevice */
816 s
= dev
->subdevices
+ 2;
817 s
->type
= COMEDI_SUBD_AI
;
818 s
->subdev_flags
= SDF_READABLE
| SDF_GROUND
;
822 s
->insn_read
= &serial2002_ai_rinsn
;
824 /* analog output subdevice */
825 s
= dev
->subdevices
+ 3;
826 s
->type
= COMEDI_SUBD_AO
;
827 s
->subdev_flags
= SDF_WRITEABLE
;
831 s
->insn_write
= &serial2002_ao_winsn
;
832 s
->insn_read
= &serial2002_ao_rinsn
;
834 /* encoder input subdevice */
835 s
= dev
->subdevices
+ 4;
836 s
->type
= COMEDI_SUBD_COUNTER
;
837 s
->subdev_flags
= SDF_READABLE
| SDF_LSAMPL
;
841 s
->insn_read
= &serial2002_ei_rinsn
;
846 static int serial2002_detach(struct comedi_device
* dev
)
848 struct comedi_subdevice
*s
;
851 printk("comedi%d: serial2002: remove\n", dev
->minor
);
852 for (i
= 0; i
< 4; i
++) {
853 s
= &dev
->subdevices
[i
];
854 if (s
->maxdata_list
) {
855 kfree(s
->maxdata_list
);
857 if (s
->range_table_list
) {
858 kfree(s
->range_table_list
);
864 COMEDI_INITCLEANUP(driver_serial2002
);