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>
38 #include <linux/sched.h>
40 #include <asm/termios.h>
41 #include <asm/ioctls.h>
42 #include <linux/serial.h>
43 #include <linux/poll.h>
46 * Board descriptions for two imaginary boards. Describing the
47 * boards in this way is optional, and completely driver-dependent.
48 * Some drivers use arrays such as this, other do not.
50 struct serial2002_board
{
54 static const struct serial2002_board serial2002_boards
[] = {
60 * Useful for shorthand access to the particular board structure
62 #define thisboard ((const struct serial2002_board *)dev->board_ptr)
64 struct serial2002_range_table_t
{
68 struct comedi_krange range
;
71 struct serial2002_private
{
73 int port
; /* /dev/ttyS<port> */
74 int speed
; /* baudrate */
76 unsigned int ao_readback
[32];
77 unsigned char digital_in_mapping
[32];
78 unsigned char digital_out_mapping
[32];
79 unsigned char analog_in_mapping
[32];
80 unsigned char analog_out_mapping
[32];
81 unsigned char encoder_in_mapping
[32];
82 struct serial2002_range_table_t in_range
[32], out_range
[32];
86 * most drivers define the following macro to make it easy to
87 * access the private structure.
89 #define devpriv ((struct serial2002_private *)dev->private)
91 static int serial2002_attach(struct comedi_device
*dev
,
92 struct comedi_devconfig
*it
);
93 static int serial2002_detach(struct comedi_device
*dev
);
94 struct comedi_driver driver_serial2002
= {
95 .driver_name
= "serial2002",
96 .module
= THIS_MODULE
,
97 .attach
= serial2002_attach
,
98 .detach
= serial2002_detach
,
99 .board_name
= &serial2002_boards
[0].name
,
100 .offset
= sizeof(struct serial2002_board
),
101 .num_names
= ARRAY_SIZE(serial2002_boards
),
104 static int serial2002_di_rinsn(struct comedi_device
*dev
,
105 struct comedi_subdevice
*s
,
106 struct comedi_insn
*insn
, unsigned int *data
);
107 static int serial2002_do_winsn(struct comedi_device
*dev
,
108 struct comedi_subdevice
*s
,
109 struct comedi_insn
*insn
, unsigned int *data
);
110 static int serial2002_ai_rinsn(struct comedi_device
*dev
,
111 struct comedi_subdevice
*s
,
112 struct comedi_insn
*insn
, unsigned int *data
);
113 static int serial2002_ao_winsn(struct comedi_device
*dev
,
114 struct comedi_subdevice
*s
,
115 struct comedi_insn
*insn
, unsigned int *data
);
116 static int serial2002_ao_rinsn(struct comedi_device
*dev
,
117 struct comedi_subdevice
*s
,
118 struct comedi_insn
*insn
, unsigned int *data
);
121 enum { is_invalid
, is_digital
, is_channel
} kind
;
126 static long tty_ioctl(struct file
*f
, unsigned op
, unsigned long param
)
128 if (f
->f_op
->unlocked_ioctl
)
129 return f
->f_op
->unlocked_ioctl(f
, op
, param
);
134 static int tty_write(struct file
*f
, unsigned char *buf
, int count
)
142 result
= f
->f_op
->write(f
, buf
, count
, &f
->f_pos
);
149 * On 2.6.26.3 this occaisonally gave me page faults, worked around by
150 * settings.c_cc[VMIN] = 0; settings.c_cc[VTIME] = 0
152 static int tty_available(struct file
*f
)
159 tty_ioctl(f
, FIONREAD
, (unsigned long)&result
);
165 static int tty_read(struct file
*f
, int timeout
)
176 struct poll_wqueues table
;
177 struct timeval start
, now
;
179 do_gettimeofday(&start
);
180 poll_initwait(&table
);
185 mask
= f
->f_op
->poll(f
, &table
.pt
);
186 if (mask
& (POLLRDNORM
| POLLRDBAND
| POLLIN
|
187 POLLHUP
| POLLERR
)) {
190 do_gettimeofday(&now
);
192 (1000000 * (now
.tv_sec
- start
.tv_sec
) +
193 now
.tv_usec
- start
.tv_usec
);
194 if (elapsed
> timeout
) {
197 set_current_state(TASK_INTERRUPTIBLE
);
198 schedule_timeout(((timeout
-
199 elapsed
) * HZ
) / 10000);
201 poll_freewait(&table
);
206 if (f
->f_op
->read(f
, &ch
, 1, &f
->f_pos
) == 1) {
211 /* Device does not support poll, busy wait */
217 if (retries
>= timeout
) {
222 if (f
->f_op
->read(f
, &ch
, 1, &f
->f_pos
) == 1) {
234 static void tty_setspeed(struct file
*f
, int speed
)
242 struct termios settings
;
244 tty_ioctl(f
, TCGETS
, (unsigned long)&settings
);
245 /* printk("Speed: %d\n", settings.c_cflag & (CBAUD | CBAUDEX)); */
246 settings
.c_iflag
= 0;
247 settings
.c_oflag
= 0;
248 settings
.c_lflag
= 0;
249 settings
.c_cflag
= CLOCAL
| CS8
| CREAD
;
250 settings
.c_cc
[VMIN
] = 0;
251 settings
.c_cc
[VTIME
] = 0;
254 settings
.c_cflag
|= B2400
;
258 settings
.c_cflag
|= B4800
;
262 settings
.c_cflag
|= B9600
;
266 settings
.c_cflag
|= B19200
;
270 settings
.c_cflag
|= B38400
;
274 settings
.c_cflag
|= B57600
;
278 settings
.c_cflag
|= B115200
;
282 settings
.c_cflag
|= B9600
;
286 tty_ioctl(f
, TCSETS
, (unsigned long)&settings
);
287 /* printk("Speed: %d\n", settings.c_cflag & (CBAUD | CBAUDEX)); */
290 /* Set low latency */
291 struct serial_struct settings
;
293 tty_ioctl(f
, TIOCGSERIAL
, (unsigned long)&settings
);
294 settings
.flags
|= ASYNC_LOW_LATENCY
;
295 tty_ioctl(f
, TIOCSSERIAL
, (unsigned long)&settings
);
301 static void poll_digital(struct file
*f
, int channel
)
305 cmd
= 0x40 | (channel
& 0x1f);
306 tty_write(f
, &cmd
, 1);
309 static void poll_channel(struct file
*f
, int channel
)
313 cmd
= 0x60 | (channel
& 0x1f);
314 tty_write(f
, &cmd
, 1);
317 static struct serial_data
serial_read(struct file
*f
, int timeout
)
319 struct serial_data result
;
322 result
.kind
= is_invalid
;
327 int data
= tty_read(f
, timeout
);
331 printk("serial2002 error\n");
333 } else if (data
& 0x80) {
334 result
.value
= (result
.value
<< 7) | (data
& 0x7f);
337 switch ((data
>> 5) & 0x03) {
340 result
.kind
= is_digital
;
345 result
.kind
= is_digital
;
351 (result
.value
<< 2) | ((data
& 0x60) >> 5);
352 result
.kind
= is_channel
;
354 result
.index
= data
& 0x1f;
362 static void serial_write(struct file
*f
, struct serial_data data
)
364 if (data
.kind
== is_digital
) {
366 ((data
.value
<< 5) & 0x20) | (data
.index
& 0x1f);
367 tty_write(f
, &ch
, 1);
371 if (data
.value
>= (1L << 30)) {
372 ch
[i
] = 0x80 | ((data
.value
>> 30) & 0x03);
375 if (data
.value
>= (1L << 23)) {
376 ch
[i
] = 0x80 | ((data
.value
>> 23) & 0x7f);
379 if (data
.value
>= (1L << 16)) {
380 ch
[i
] = 0x80 | ((data
.value
>> 16) & 0x7f);
383 if (data
.value
>= (1L << 9)) {
384 ch
[i
] = 0x80 | ((data
.value
>> 9) & 0x7f);
387 ch
[i
] = 0x80 | ((data
.value
>> 2) & 0x7f);
389 ch
[i
] = ((data
.value
<< 5) & 0x60) | (data
.index
& 0x1f);
395 static void serial_2002_open(struct comedi_device
*dev
)
399 sprintf(port
, "/dev/ttyS%d", devpriv
->port
);
400 devpriv
->tty
= filp_open(port
, O_RDWR
, 0);
401 if (IS_ERR(devpriv
->tty
)) {
402 printk("serial_2002: file open error = %ld\n",
403 PTR_ERR(devpriv
->tty
));
413 struct config_t dig_in_config
[32];
414 struct config_t dig_out_config
[32];
415 struct config_t chan_in_config
[32];
416 struct config_t chan_out_config
[32];
419 for (i
= 0; i
< 32; i
++) {
420 dig_in_config
[i
].kind
= 0;
421 dig_in_config
[i
].bits
= 0;
422 dig_in_config
[i
].min
= 0;
423 dig_in_config
[i
].max
= 0;
424 dig_out_config
[i
].kind
= 0;
425 dig_out_config
[i
].bits
= 0;
426 dig_out_config
[i
].min
= 0;
427 dig_out_config
[i
].max
= 0;
428 chan_in_config
[i
].kind
= 0;
429 chan_in_config
[i
].bits
= 0;
430 chan_in_config
[i
].min
= 0;
431 chan_in_config
[i
].max
= 0;
432 chan_out_config
[i
].kind
= 0;
433 chan_out_config
[i
].bits
= 0;
434 chan_out_config
[i
].min
= 0;
435 chan_out_config
[i
].max
= 0;
438 tty_setspeed(devpriv
->tty
, devpriv
->speed
);
439 poll_channel(devpriv
->tty
, 31); /* Start reading configuration */
441 struct serial_data data
;
443 data
= serial_read(devpriv
->tty
, 1000);
444 if (data
.kind
!= is_channel
|| data
.index
!= 31
445 || !(data
.value
& 0xe0)) {
448 int command
, channel
, kind
;
449 struct config_t
*cur_config
= 0;
451 channel
= data
.value
& 0x1f;
452 kind
= (data
.value
>> 5) & 0x7;
453 command
= (data
.value
>> 8) & 0x3;
456 cur_config
= dig_in_config
;
460 cur_config
= dig_out_config
;
464 cur_config
= chan_in_config
;
468 cur_config
= chan_out_config
;
472 cur_config
= chan_in_config
;
478 cur_config
[channel
].kind
= kind
;
481 cur_config
[channel
].bits
524 cur_config
[channel
].min
565 cur_config
[channel
].max
573 for (i
= 0; i
<= 4; i
++) {
574 /* Fill in subdev data */
576 unsigned char *mapping
= 0;
577 struct serial2002_range_table_t
*range
= 0;
583 mapping
= devpriv
->digital_in_mapping
;
589 mapping
= devpriv
->digital_out_mapping
;
595 mapping
= devpriv
->analog_in_mapping
;
596 range
= devpriv
->in_range
;
602 mapping
= devpriv
->analog_out_mapping
;
603 range
= devpriv
->out_range
;
609 mapping
= devpriv
->encoder_in_mapping
;
610 range
= devpriv
->in_range
;
620 struct comedi_subdevice
*s
;
621 const struct comedi_lrange
**range_table_list
=
623 unsigned int *maxdata_list
;
626 for (chan
= 0, j
= 0; j
< 32; j
++) {
627 if (c
[j
].kind
== kind
) {
631 s
= &dev
->subdevices
[i
];
634 if (s
->maxdata_list
) {
635 kfree(s
->maxdata_list
);
637 s
->maxdata_list
= maxdata_list
=
638 kmalloc(sizeof(unsigned int) * s
->n_chan
,
640 if (s
->range_table_list
) {
641 kfree(s
->range_table_list
);
645 s
->range_table_list
= range_table_list
=
648 serial2002_range_table_t
) *
649 s
->n_chan
, GFP_KERNEL
);
651 for (chan
= 0, j
= 0; j
< 32; j
++) {
652 if (c
[j
].kind
== kind
) {
662 range_table_list
[chan
] =
668 ((long long)1 << c
[j
].bits
)
678 static void serial_2002_close(struct comedi_device
*dev
)
680 if (!IS_ERR(devpriv
->tty
) && (devpriv
->tty
!= 0)) {
681 filp_close(devpriv
->tty
, 0);
685 static int serial2002_di_rinsn(struct comedi_device
*dev
,
686 struct comedi_subdevice
*s
,
687 struct comedi_insn
*insn
, unsigned int *data
)
692 chan
= devpriv
->digital_in_mapping
[CR_CHAN(insn
->chanspec
)];
693 for (n
= 0; n
< insn
->n
; n
++) {
694 struct serial_data read
;
696 poll_digital(devpriv
->tty
, chan
);
698 read
= serial_read(devpriv
->tty
, 1000);
699 if (read
.kind
!= is_digital
|| read
.index
== chan
) {
703 data
[n
] = read
.value
;
708 static int serial2002_do_winsn(struct comedi_device
*dev
,
709 struct comedi_subdevice
*s
,
710 struct comedi_insn
*insn
, unsigned int *data
)
715 chan
= devpriv
->digital_out_mapping
[CR_CHAN(insn
->chanspec
)];
716 for (n
= 0; n
< insn
->n
; n
++) {
717 struct serial_data write
;
719 write
.kind
= is_digital
;
721 write
.value
= data
[n
];
722 serial_write(devpriv
->tty
, write
);
727 static int serial2002_ai_rinsn(struct comedi_device
*dev
,
728 struct comedi_subdevice
*s
,
729 struct comedi_insn
*insn
, unsigned int *data
)
734 chan
= devpriv
->analog_in_mapping
[CR_CHAN(insn
->chanspec
)];
735 for (n
= 0; n
< insn
->n
; n
++) {
736 struct serial_data read
;
738 poll_channel(devpriv
->tty
, chan
);
740 read
= serial_read(devpriv
->tty
, 1000);
741 if (read
.kind
!= is_channel
|| read
.index
== chan
) {
745 data
[n
] = read
.value
;
750 static int serial2002_ao_winsn(struct comedi_device
*dev
,
751 struct comedi_subdevice
*s
,
752 struct comedi_insn
*insn
, unsigned int *data
)
757 chan
= devpriv
->analog_out_mapping
[CR_CHAN(insn
->chanspec
)];
758 for (n
= 0; n
< insn
->n
; n
++) {
759 struct serial_data write
;
761 write
.kind
= is_channel
;
763 write
.value
= data
[n
];
764 serial_write(devpriv
->tty
, write
);
765 devpriv
->ao_readback
[chan
] = data
[n
];
770 static int serial2002_ao_rinsn(struct comedi_device
*dev
,
771 struct comedi_subdevice
*s
,
772 struct comedi_insn
*insn
, unsigned int *data
)
775 int chan
= CR_CHAN(insn
->chanspec
);
777 for (n
= 0; n
< insn
->n
; n
++) {
778 data
[n
] = devpriv
->ao_readback
[chan
];
784 static int serial2002_ei_rinsn(struct comedi_device
*dev
,
785 struct comedi_subdevice
*s
,
786 struct comedi_insn
*insn
, unsigned int *data
)
791 chan
= devpriv
->encoder_in_mapping
[CR_CHAN(insn
->chanspec
)];
792 for (n
= 0; n
< insn
->n
; n
++) {
793 struct serial_data read
;
795 poll_channel(devpriv
->tty
, chan
);
797 read
= serial_read(devpriv
->tty
, 1000);
798 if (read
.kind
!= is_channel
|| read
.index
== chan
) {
802 data
[n
] = read
.value
;
807 static int serial2002_attach(struct comedi_device
*dev
,
808 struct comedi_devconfig
*it
)
810 struct comedi_subdevice
*s
;
812 printk("comedi%d: serial2002: ", dev
->minor
);
813 dev
->board_name
= thisboard
->name
;
814 if (alloc_private(dev
, sizeof(struct serial2002_private
)) < 0) {
817 dev
->open
= serial_2002_open
;
818 dev
->close
= serial_2002_close
;
819 devpriv
->port
= it
->options
[0];
820 devpriv
->speed
= it
->options
[1];
821 printk("/dev/ttyS%d @ %d\n", devpriv
->port
, devpriv
->speed
);
823 if (alloc_subdevices(dev
, 5) < 0)
826 /* digital input subdevice */
827 s
= dev
->subdevices
+ 0;
828 s
->type
= COMEDI_SUBD_DI
;
829 s
->subdev_flags
= SDF_READABLE
;
832 s
->range_table
= &range_digital
;
833 s
->insn_read
= &serial2002_di_rinsn
;
835 /* digital output subdevice */
836 s
= dev
->subdevices
+ 1;
837 s
->type
= COMEDI_SUBD_DO
;
838 s
->subdev_flags
= SDF_WRITEABLE
;
841 s
->range_table
= &range_digital
;
842 s
->insn_write
= &serial2002_do_winsn
;
844 /* analog input subdevice */
845 s
= dev
->subdevices
+ 2;
846 s
->type
= COMEDI_SUBD_AI
;
847 s
->subdev_flags
= SDF_READABLE
| SDF_GROUND
;
851 s
->insn_read
= &serial2002_ai_rinsn
;
853 /* analog output subdevice */
854 s
= dev
->subdevices
+ 3;
855 s
->type
= COMEDI_SUBD_AO
;
856 s
->subdev_flags
= SDF_WRITEABLE
;
860 s
->insn_write
= &serial2002_ao_winsn
;
861 s
->insn_read
= &serial2002_ao_rinsn
;
863 /* encoder input subdevice */
864 s
= dev
->subdevices
+ 4;
865 s
->type
= COMEDI_SUBD_COUNTER
;
866 s
->subdev_flags
= SDF_READABLE
| SDF_LSAMPL
;
870 s
->insn_read
= &serial2002_ei_rinsn
;
875 static int serial2002_detach(struct comedi_device
*dev
)
877 struct comedi_subdevice
*s
;
880 printk("comedi%d: serial2002: remove\n", dev
->minor
);
881 for (i
= 0; i
< 4; i
++) {
882 s
= &dev
->subdevices
[i
];
883 if (s
->maxdata_list
) {
884 kfree(s
->maxdata_list
);
886 if (s
->range_table_list
) {
887 kfree(s
->range_table_list
);
893 COMEDI_INITCLEANUP(driver_serial2002
);