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
{
45 struct comedi_krange range
;
48 struct serial2002_private
{
50 int port
; /* /dev/ttyS<port> */
51 int speed
; /* baudrate */
53 unsigned int ao_readback
[32];
54 unsigned char digital_in_mapping
[32];
55 unsigned char digital_out_mapping
[32];
56 unsigned char analog_in_mapping
[32];
57 unsigned char analog_out_mapping
[32];
58 unsigned char encoder_in_mapping
[32];
59 struct serial2002_range_table_t in_range
[32], out_range
[32];
63 enum { is_invalid
, is_digital
, is_channel
} kind
;
69 * The configuration serial_data.value read from the device is
70 * a bitmask that defines specific options of a channel:
72 * 4:0 - the channel to configure
73 * 7:5 - the kind of channel
74 * 9:8 - the command used to configure the channel
76 * The remaining bits vary in use depending on the command:
78 * BITS 15:10 - the channel bits (maxdata)
79 * MIN/MAX 12:10 - the units multiplier for the scale
80 * 13 - the sign of the scale
81 * 33:14 - the base value for the range
83 #define S2002_CFG_CHAN(x) ((x) & 0x1f)
84 #define S2002_CFG_KIND(x) (((x) >> 5) & 0x7)
85 #define S2002_CFG_KIND_INVALID 0
86 #define S2002_CFG_KIND_DIGITAL_IN 1
87 #define S2002_CFG_KIND_DIGITAL_OUT 2
88 #define S2002_CFG_KIND_ANALOG_IN 3
89 #define S2002_CFG_KIND_ANALOG_OUT 4
90 #define S2002_CFG_KIND_ENCODER_IN 5
91 #define S2002_CFG_CMD(x) (((x) >> 8) & 0x3)
92 #define S2002_CFG_CMD_BITS 0
93 #define S2002_CFG_CMD_MIN 1
94 #define S2002_CFG_CMD_MAX 2
95 #define S2002_CFG_BITS(x) (((x) >> 10) & 0x3f)
96 #define S2002_CFG_UNITS(x) (((x) >> 10) & 0x7)
97 #define S2002_CFG_SIGN(x) (((x) >> 13) & 0x1)
98 #define S2002_CFG_BASE(x) (((x) >> 14) & 0xfffff)
100 static long serial2002_tty_ioctl(struct file
*f
, unsigned op
,
103 if (f
->f_op
->unlocked_ioctl
)
104 return f
->f_op
->unlocked_ioctl(f
, op
, param
);
109 static int serial2002_tty_write(struct file
*f
, unsigned char *buf
, int count
)
111 const char __user
*p
= (__force
const char __user
*)buf
;
118 result
= f
->f_op
->write(f
, p
, count
, &f
->f_pos
);
123 static int serial2002_tty_readb(struct file
*f
, unsigned char *buf
)
125 char __user
*p
= (__force
char __user
*)buf
;
128 return f
->f_op
->read(f
, p
, 1, &f
->f_pos
);
131 static void serial2002_tty_read_poll_wait(struct file
*f
, int timeout
)
133 struct poll_wqueues table
;
134 struct timeval start
, now
;
136 do_gettimeofday(&start
);
137 poll_initwait(&table
);
142 mask
= f
->f_op
->poll(f
, &table
.pt
);
143 if (mask
& (POLLRDNORM
| POLLRDBAND
| POLLIN
|
144 POLLHUP
| POLLERR
)) {
147 do_gettimeofday(&now
);
148 elapsed
= (1000000 * (now
.tv_sec
- start
.tv_sec
) +
149 now
.tv_usec
- start
.tv_usec
);
150 if (elapsed
> timeout
)
152 set_current_state(TASK_INTERRUPTIBLE
);
153 schedule_timeout(((timeout
- elapsed
) * HZ
) / 10000);
155 poll_freewait(&table
);
158 static int serial2002_tty_read(struct file
*f
, int timeout
)
170 serial2002_tty_read_poll_wait(f
, timeout
);
172 if (serial2002_tty_readb(f
, &ch
) == 1)
175 /* Device does not support poll, busy wait */
180 if (retries
>= timeout
)
183 if (serial2002_tty_readb(f
, &ch
) == 1) {
195 static void serial2002_tty_setspeed(struct file
*f
, int speed
)
197 struct termios termios
;
198 struct serial_struct serial
;
205 serial2002_tty_ioctl(f
, TCGETS
, (unsigned long)&termios
);
209 termios
.c_cflag
= CLOCAL
| CS8
| CREAD
;
210 termios
.c_cc
[VMIN
] = 0;
211 termios
.c_cc
[VTIME
] = 0;
214 termios
.c_cflag
|= B2400
;
217 termios
.c_cflag
|= B4800
;
220 termios
.c_cflag
|= B9600
;
223 termios
.c_cflag
|= B19200
;
226 termios
.c_cflag
|= B38400
;
229 termios
.c_cflag
|= B57600
;
232 termios
.c_cflag
|= B115200
;
235 termios
.c_cflag
|= B9600
;
238 serial2002_tty_ioctl(f
, TCSETS
, (unsigned long)&termios
);
240 /* Set low latency */
241 serial2002_tty_ioctl(f
, TIOCGSERIAL
, (unsigned long)&serial
);
242 serial
.flags
|= ASYNC_LOW_LATENCY
;
243 serial2002_tty_ioctl(f
, TIOCSSERIAL
, (unsigned long)&serial
);
248 static void serial2002_poll_digital(struct file
*f
, int channel
)
252 cmd
= 0x40 | (channel
& 0x1f);
253 serial2002_tty_write(f
, &cmd
, 1);
256 static void serial2002_poll_channel(struct file
*f
, int channel
)
260 cmd
= 0x60 | (channel
& 0x1f);
261 serial2002_tty_write(f
, &cmd
, 1);
264 static struct serial_data
serial2002_read(struct file
*f
, int timeout
)
266 struct serial_data result
;
269 result
.kind
= is_invalid
;
274 int data
= serial2002_tty_read(f
, timeout
);
279 } else if (data
& 0x80) {
280 result
.value
= (result
.value
<< 7) | (data
& 0x7f);
283 switch ((data
>> 5) & 0x03) {
286 result
.kind
= is_digital
;
290 result
.kind
= is_digital
;
295 (result
.value
<< 2) | ((data
& 0x60) >> 5);
296 result
.kind
= is_channel
;
298 result
.index
= data
& 0x1f;
306 static void serial2002_write(struct file
*f
, struct serial_data data
)
308 if (data
.kind
== is_digital
) {
310 ((data
.value
<< 5) & 0x20) | (data
.index
& 0x1f);
311 serial2002_tty_write(f
, &ch
, 1);
316 if (data
.value
>= (1L << 30)) {
317 ch
[i
] = 0x80 | ((data
.value
>> 30) & 0x03);
320 if (data
.value
>= (1L << 23)) {
321 ch
[i
] = 0x80 | ((data
.value
>> 23) & 0x7f);
324 if (data
.value
>= (1L << 16)) {
325 ch
[i
] = 0x80 | ((data
.value
>> 16) & 0x7f);
328 if (data
.value
>= (1L << 9)) {
329 ch
[i
] = 0x80 | ((data
.value
>> 9) & 0x7f);
332 ch
[i
] = 0x80 | ((data
.value
>> 2) & 0x7f);
334 ch
[i
] = ((data
.value
<< 5) & 0x60) | (data
.index
& 0x1f);
336 serial2002_tty_write(f
, ch
, i
);
347 static int serial2002_setup_subdevice(struct comedi_subdevice
*s
,
348 struct config_t
*cfg
,
349 struct serial2002_range_table_t
*range
,
350 unsigned char *mapping
,
353 const struct comedi_lrange
**range_table_list
= NULL
;
354 unsigned int *maxdata_list
;
357 for (chan
= 0, j
= 0; j
< 32; j
++) {
358 if (cfg
[j
].kind
== kind
)
363 kfree(s
->maxdata_list
);
364 maxdata_list
= kmalloc_array(s
->n_chan
, sizeof(unsigned int),
368 s
->maxdata_list
= maxdata_list
;
369 kfree(s
->range_table_list
);
370 s
->range_table
= NULL
;
371 s
->range_table_list
= NULL
;
372 if (kind
== 1 || kind
== 2) {
373 s
->range_table
= &range_digital
;
375 range_table_list
= kmalloc_array(s
->n_chan
, sizeof(*range
),
377 if (!range_table_list
)
379 s
->range_table_list
= range_table_list
;
381 for (chan
= 0, j
= 0; j
< 32; j
++) {
382 if (cfg
[j
].kind
== kind
) {
387 range
[j
].range
.min
= cfg
[j
].min
;
388 range
[j
].range
.max
= cfg
[j
].max
;
389 range_table_list
[chan
] =
390 (const struct comedi_lrange
*)&range
[j
];
392 maxdata_list
[chan
] = ((long long)1 << cfg
[j
].bits
) - 1;
399 static int serial2002_setup_subdevs(struct comedi_device
*dev
)
401 struct serial2002_private
*devpriv
= dev
->private;
402 struct config_t
*di_cfg
;
403 struct config_t
*do_cfg
;
404 struct config_t
*ai_cfg
;
405 struct config_t
*ao_cfg
;
406 struct config_t
*cfg
;
407 struct comedi_subdevice
*s
;
411 /* Allocate the temporary structs to hold the configuration data */
412 di_cfg
= kcalloc(32, sizeof(*cfg
), GFP_KERNEL
);
413 do_cfg
= kcalloc(32, sizeof(*cfg
), GFP_KERNEL
);
414 ai_cfg
= kcalloc(32, sizeof(*cfg
), GFP_KERNEL
);
415 ao_cfg
= kcalloc(32, sizeof(*cfg
), GFP_KERNEL
);
416 if (!di_cfg
|| !do_cfg
|| !ai_cfg
|| !ao_cfg
) {
418 goto err_alloc_configs
;
421 /* Read the configuration from the connected device */
422 serial2002_tty_setspeed(devpriv
->tty
, devpriv
->speed
);
423 serial2002_poll_channel(devpriv
->tty
, 31);
425 struct serial_data data
= serial2002_read(devpriv
->tty
, 1000);
426 int kind
= S2002_CFG_KIND(data
.value
);
427 int channel
= S2002_CFG_CHAN(data
.value
);
428 int range
= S2002_CFG_BASE(data
.value
);
429 int cmd
= S2002_CFG_CMD(data
.value
);
431 if (data
.kind
!= is_channel
|| data
.index
!= 31 ||
432 kind
== S2002_CFG_KIND_INVALID
)
436 case S2002_CFG_KIND_DIGITAL_IN
:
439 case S2002_CFG_KIND_DIGITAL_OUT
:
442 case S2002_CFG_KIND_ANALOG_IN
:
445 case S2002_CFG_KIND_ANALOG_OUT
:
448 case S2002_CFG_KIND_ENCODER_IN
:
456 continue; /* unknown kind, skip it */
458 cfg
[channel
].kind
= kind
;
461 case S2002_CFG_CMD_BITS
:
462 cfg
[channel
].bits
= S2002_CFG_BITS(data
.value
);
464 case S2002_CFG_CMD_MIN
:
465 case S2002_CFG_CMD_MAX
:
466 switch (S2002_CFG_UNITS(data
.value
)) {
477 if (S2002_CFG_SIGN(data
.value
))
479 if (cmd
== S2002_CFG_CMD_MIN
)
480 cfg
[channel
].min
= range
;
482 cfg
[channel
].max
= range
;
487 /* Fill in subdevice data */
488 for (i
= 0; i
<= 4; i
++) {
489 unsigned char *mapping
= NULL
;
490 struct serial2002_range_table_t
*range
= NULL
;
493 s
= &dev
->subdevices
[i
];
498 mapping
= devpriv
->digital_in_mapping
;
499 kind
= S2002_CFG_KIND_DIGITAL_IN
;
503 mapping
= devpriv
->digital_out_mapping
;
504 kind
= S2002_CFG_KIND_DIGITAL_OUT
;
508 mapping
= devpriv
->analog_in_mapping
;
509 range
= devpriv
->in_range
;
510 kind
= S2002_CFG_KIND_ANALOG_IN
;
514 mapping
= devpriv
->analog_out_mapping
;
515 range
= devpriv
->out_range
;
516 kind
= S2002_CFG_KIND_ANALOG_OUT
;
520 mapping
= devpriv
->encoder_in_mapping
;
521 range
= devpriv
->in_range
;
522 kind
= S2002_CFG_KIND_ENCODER_IN
;
526 if (serial2002_setup_subdevice(s
, cfg
, range
, mapping
, kind
))
527 break; /* err handled below */
531 * Failed to allocate maxdata_list or range_table_list
532 * for a subdevice that needed it.
535 for (i
= 0; i
<= 4; i
++) {
536 s
= &dev
->subdevices
[i
];
537 kfree(s
->maxdata_list
);
538 s
->maxdata_list
= NULL
;
539 kfree(s
->range_table_list
);
540 s
->range_table_list
= NULL
;
552 filp_close(devpriv
->tty
, NULL
);
560 static int serial2002_open(struct comedi_device
*dev
)
562 struct serial2002_private
*devpriv
= dev
->private;
566 sprintf(port
, "/dev/ttyS%d", devpriv
->port
);
567 devpriv
->tty
= filp_open(port
, O_RDWR
, 0);
568 if (IS_ERR(devpriv
->tty
)) {
569 result
= (int)PTR_ERR(devpriv
->tty
);
570 dev_err(dev
->class_dev
, "file open error = %d\n", result
);
572 result
= serial2002_setup_subdevs(dev
);
577 static void serial2002_close(struct comedi_device
*dev
)
579 struct serial2002_private
*devpriv
= dev
->private;
581 if (!IS_ERR(devpriv
->tty
) && devpriv
->tty
)
582 filp_close(devpriv
->tty
, NULL
);
585 static int serial2002_di_insn_read(struct comedi_device
*dev
,
586 struct comedi_subdevice
*s
,
587 struct comedi_insn
*insn
,
590 struct serial2002_private
*devpriv
= dev
->private;
594 chan
= devpriv
->digital_in_mapping
[CR_CHAN(insn
->chanspec
)];
595 for (n
= 0; n
< insn
->n
; n
++) {
596 struct serial_data read
;
598 serial2002_poll_digital(devpriv
->tty
, chan
);
600 read
= serial2002_read(devpriv
->tty
, 1000);
601 if (read
.kind
!= is_digital
|| read
.index
== chan
)
604 data
[n
] = read
.value
;
609 static int serial2002_do_insn_write(struct comedi_device
*dev
,
610 struct comedi_subdevice
*s
,
611 struct comedi_insn
*insn
,
614 struct serial2002_private
*devpriv
= dev
->private;
618 chan
= devpriv
->digital_out_mapping
[CR_CHAN(insn
->chanspec
)];
619 for (n
= 0; n
< insn
->n
; n
++) {
620 struct serial_data write
;
622 write
.kind
= is_digital
;
624 write
.value
= data
[n
];
625 serial2002_write(devpriv
->tty
, write
);
630 static int serial2002_ai_insn_read(struct comedi_device
*dev
,
631 struct comedi_subdevice
*s
,
632 struct comedi_insn
*insn
,
635 struct serial2002_private
*devpriv
= dev
->private;
639 chan
= devpriv
->analog_in_mapping
[CR_CHAN(insn
->chanspec
)];
640 for (n
= 0; n
< insn
->n
; n
++) {
641 struct serial_data read
;
643 serial2002_poll_channel(devpriv
->tty
, chan
);
645 read
= serial2002_read(devpriv
->tty
, 1000);
646 if (read
.kind
!= is_channel
|| read
.index
== chan
)
649 data
[n
] = read
.value
;
654 static int serial2002_ao_insn_write(struct comedi_device
*dev
,
655 struct comedi_subdevice
*s
,
656 struct comedi_insn
*insn
,
659 struct serial2002_private
*devpriv
= dev
->private;
663 chan
= devpriv
->analog_out_mapping
[CR_CHAN(insn
->chanspec
)];
664 for (n
= 0; n
< insn
->n
; n
++) {
665 struct serial_data write
;
667 write
.kind
= is_channel
;
669 write
.value
= data
[n
];
670 serial2002_write(devpriv
->tty
, write
);
671 devpriv
->ao_readback
[chan
] = data
[n
];
676 static int serial2002_ao_insn_read(struct comedi_device
*dev
,
677 struct comedi_subdevice
*s
,
678 struct comedi_insn
*insn
,
681 struct serial2002_private
*devpriv
= dev
->private;
683 int chan
= CR_CHAN(insn
->chanspec
);
685 for (n
= 0; n
< insn
->n
; n
++)
686 data
[n
] = devpriv
->ao_readback
[chan
];
691 static int serial2002_encoder_insn_read(struct comedi_device
*dev
,
692 struct comedi_subdevice
*s
,
693 struct comedi_insn
*insn
,
696 struct serial2002_private
*devpriv
= dev
->private;
700 chan
= devpriv
->encoder_in_mapping
[CR_CHAN(insn
->chanspec
)];
701 for (n
= 0; n
< insn
->n
; n
++) {
702 struct serial_data read
;
704 serial2002_poll_channel(devpriv
->tty
, chan
);
706 read
= serial2002_read(devpriv
->tty
, 1000);
707 if (read
.kind
!= is_channel
|| read
.index
== chan
)
710 data
[n
] = read
.value
;
715 static int serial2002_attach(struct comedi_device
*dev
,
716 struct comedi_devconfig
*it
)
718 struct serial2002_private
*devpriv
;
719 struct comedi_subdevice
*s
;
722 devpriv
= comedi_alloc_devpriv(dev
, sizeof(*devpriv
));
726 devpriv
->port
= it
->options
[0];
727 devpriv
->speed
= it
->options
[1];
729 ret
= comedi_alloc_subdevices(dev
, 5);
733 /* digital input subdevice */
734 s
= &dev
->subdevices
[0];
735 s
->type
= COMEDI_SUBD_DI
;
736 s
->subdev_flags
= SDF_READABLE
;
739 s
->range_table
= &range_digital
;
740 s
->insn_read
= serial2002_di_insn_read
;
742 /* digital output subdevice */
743 s
= &dev
->subdevices
[1];
744 s
->type
= COMEDI_SUBD_DO
;
745 s
->subdev_flags
= SDF_WRITABLE
;
748 s
->range_table
= &range_digital
;
749 s
->insn_write
= serial2002_do_insn_write
;
751 /* analog input subdevice */
752 s
= &dev
->subdevices
[2];
753 s
->type
= COMEDI_SUBD_AI
;
754 s
->subdev_flags
= SDF_READABLE
| SDF_GROUND
;
757 s
->range_table
= NULL
;
758 s
->insn_read
= serial2002_ai_insn_read
;
760 /* analog output subdevice */
761 s
= &dev
->subdevices
[3];
762 s
->type
= COMEDI_SUBD_AO
;
763 s
->subdev_flags
= SDF_WRITABLE
;
766 s
->range_table
= NULL
;
767 s
->insn_write
= serial2002_ao_insn_write
;
768 s
->insn_read
= serial2002_ao_insn_read
;
770 /* encoder input subdevice */
771 s
= &dev
->subdevices
[4];
772 s
->type
= COMEDI_SUBD_COUNTER
;
773 s
->subdev_flags
= SDF_READABLE
| SDF_LSAMPL
;
776 s
->range_table
= NULL
;
777 s
->insn_read
= serial2002_encoder_insn_read
;
779 dev
->open
= serial2002_open
;
780 dev
->close
= serial2002_close
;
785 static void serial2002_detach(struct comedi_device
*dev
)
787 struct comedi_subdevice
*s
;
790 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
791 s
= &dev
->subdevices
[i
];
792 kfree(s
->maxdata_list
);
793 kfree(s
->range_table_list
);
797 static struct comedi_driver serial2002_driver
= {
798 .driver_name
= "serial2002",
799 .module
= THIS_MODULE
,
800 .attach
= serial2002_attach
,
801 .detach
= serial2002_detach
,
803 module_comedi_driver(serial2002_driver
);
805 MODULE_AUTHOR("Comedi http://www.comedi.org");
806 MODULE_DESCRIPTION("Comedi low-level driver");
807 MODULE_LICENSE("GPL");