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)
63 struct serial2002_range_table_t {
67 struct comedi_krange range;
72 int port; // /dev/ttyS<port>
73 int speed; // baudrate
75 unsigned int ao_readback[32];
76 unsigned char digital_in_mapping[32];
77 unsigned char digital_out_mapping[32];
78 unsigned char analog_in_mapping[32];
79 unsigned char analog_out_mapping[32];
80 unsigned char encoder_in_mapping[32];
81 struct serial2002_range_table_t in_range[32], out_range[32];
85 * most drivers define the following macro to make it easy to
86 * access the private structure.
88 #define devpriv ((serial2002_private *)dev->private)
90 static int serial2002_attach(struct comedi_device * dev, struct comedi_devconfig * it);
91 static int serial2002_detach(struct comedi_device * dev);
92 struct comedi_driver driver_serial2002 = {
93 driver_name:"serial2002",
95 attach:serial2002_attach,
96 detach:serial2002_detach,
97 board_name:&serial2002_boards[0].name,
98 offset:sizeof(serial2002_board),
99 num_names:sizeof(serial2002_boards) / sizeof(serial2002_board),
102 static int serial2002_di_rinsn(struct comedi_device * dev, struct comedi_subdevice * s,
103 struct comedi_insn * insn, unsigned int * data);
104 static int serial2002_do_winsn(struct comedi_device * dev, struct comedi_subdevice * s,
105 struct comedi_insn * insn, unsigned int * data);
106 static int serial2002_ai_rinsn(struct comedi_device * dev, struct comedi_subdevice * s,
107 struct comedi_insn * insn, unsigned int * data);
108 static int serial2002_ao_winsn(struct comedi_device * dev, struct comedi_subdevice * s,
109 struct comedi_insn * insn, unsigned int * data);
110 static int serial2002_ao_rinsn(struct comedi_device * dev, struct comedi_subdevice * s,
111 struct comedi_insn * insn, unsigned int * data);
114 enum { is_invalid, is_digital, is_channel } kind;
119 static long tty_ioctl(struct file *f, unsigned op, unsigned long param)
121 #ifdef HAVE_UNLOCKED_IOCTL
122 if (f->f_op->unlocked_ioctl) {
123 return f->f_op->unlocked_ioctl(f, op, param);
126 if (f->f_op->ioctl) {
127 return f->f_op->ioctl(f->f_dentry->d_inode, f, op, param);
132 static int tty_write(struct file *f, unsigned char *buf, int count)
140 result = f->f_op->write(f, buf, count, &f->f_pos);
147 * On 2.6.26.3 this occaisonally gave me page faults, worked around by
148 * settings.c_cc[VMIN] = 0; settings.c_cc[VTIME] = 0
150 static int tty_available(struct file *f)
157 tty_ioctl(f, FIONREAD, (unsigned long)&result);
163 static int tty_read(struct file *f, int timeout)
174 struct poll_wqueues table;
175 struct timeval start, now;
177 do_gettimeofday(&start);
178 poll_initwait(&table);
183 mask = f->f_op->poll(f, &table.pt);
184 if (mask & (POLLRDNORM | POLLRDBAND | POLLIN |
185 POLLHUP | POLLERR)) {
188 do_gettimeofday(&now);
190 (1000000 * (now.tv_sec - start.tv_sec) +
191 now.tv_usec - start.tv_usec);
192 if (elapsed > timeout) {
195 set_current_state(TASK_INTERRUPTIBLE);
196 schedule_timeout(((timeout -
197 elapsed) * HZ) / 10000);
199 poll_freewait(&table);
204 if (f->f_op->read(f, &ch, 1, &f->f_pos) == 1) {
209 /* Device does not support poll, busy wait */
215 if (retries >= timeout) {
220 if (f->f_op->read(f, &ch, 1, &f->f_pos) == 1) {
232 static void tty_setspeed(struct file *f, int speed)
240 struct termios settings;
242 tty_ioctl(f, TCGETS, (unsigned long)&settings);
243 // printk("Speed: %d\n", settings.c_cflag & (CBAUD | CBAUDEX));
244 settings.c_iflag = 0;
245 settings.c_oflag = 0;
246 settings.c_lflag = 0;
247 settings.c_cflag = CLOCAL | CS8 | CREAD;
248 settings.c_cc[VMIN] = 0;
249 settings.c_cc[VTIME] = 0;
252 settings.c_cflag |= B2400;
256 settings.c_cflag |= B4800;
260 settings.c_cflag |= B9600;
264 settings.c_cflag |= B19200;
268 settings.c_cflag |= B38400;
272 settings.c_cflag |= B57600;
276 settings.c_cflag |= B115200;
280 settings.c_cflag |= B9600;
284 tty_ioctl(f, TCSETS, (unsigned long)&settings);
285 // printk("Speed: %d\n", settings.c_cflag & (CBAUD | CBAUDEX));
289 struct serial_struct settings;
291 tty_ioctl(f, TIOCGSERIAL, (unsigned long)&settings);
292 settings.flags |= ASYNC_LOW_LATENCY;
293 tty_ioctl(f, TIOCSSERIAL, (unsigned long)&settings);
299 static void poll_digital(struct file *f, int channel)
303 cmd = 0x40 | (channel & 0x1f);
304 tty_write(f, &cmd, 1);
307 static void poll_channel(struct file *f, int channel)
311 cmd = 0x60 | (channel & 0x1f);
312 tty_write(f, &cmd, 1);
315 static struct serial_data serial_read(struct file *f, int timeout)
317 struct serial_data result;
320 result.kind = is_invalid;
325 int data = tty_read(f, timeout);
329 printk("serial2002 error\n");
331 } else if (data & 0x80) {
332 result.value = (result.value << 7) | (data & 0x7f);
335 switch ((data >> 5) & 0x03) {
338 result.kind = is_digital;
343 result.kind = is_digital;
350 value << 2) | ((data & 0x60) >> 5);
351 result.kind = is_channel;
353 result.index = data & 0x1f;
361 static void serial_write(struct file *f, struct serial_data data)
363 if (data.kind == is_digital) {
365 ((data.value << 5) & 0x20) | (data.index & 0x1f);
366 tty_write(f, &ch, 1);
370 if (data.value >= (1L << 30)) {
371 ch[i] = 0x80 | ((data.value >> 30) & 0x03);
374 if (data.value >= (1L << 23)) {
375 ch[i] = 0x80 | ((data.value >> 23) & 0x7f);
378 if (data.value >= (1L << 16)) {
379 ch[i] = 0x80 | ((data.value >> 16) & 0x7f);
382 if (data.value >= (1L << 9)) {
383 ch[i] = 0x80 | ((data.value >> 9) & 0x7f);
386 ch[i] = 0x80 | ((data.value >> 2) & 0x7f);
388 ch[i] = ((data.value << 5) & 0x60) | (data.index & 0x1f);
394 static void serial_2002_open(struct comedi_device * dev)
398 sprintf(port, "/dev/ttyS%d", devpriv->port);
399 devpriv->tty = filp_open(port, 0, O_RDWR);
400 if (IS_ERR(devpriv->tty)) {
401 printk("serial_2002: file open error = %ld\n",
402 PTR_ERR(devpriv->tty));
410 config_t dig_in_config[32];
411 config_t dig_out_config[32];
412 config_t chan_in_config[32];
413 config_t chan_out_config[32];
416 for (i = 0; i < 32; i++) {
417 dig_in_config[i].kind = 0;
418 dig_in_config[i].bits = 0;
419 dig_in_config[i].min = 0;
420 dig_in_config[i].max = 0;
421 dig_out_config[i].kind = 0;
422 dig_out_config[i].bits = 0;
423 dig_out_config[i].min = 0;
424 dig_out_config[i].max = 0;
425 chan_in_config[i].kind = 0;
426 chan_in_config[i].bits = 0;
427 chan_in_config[i].min = 0;
428 chan_in_config[i].max = 0;
429 chan_out_config[i].kind = 0;
430 chan_out_config[i].bits = 0;
431 chan_out_config[i].min = 0;
432 chan_out_config[i].max = 0;
435 tty_setspeed(devpriv->tty, devpriv->speed);
436 poll_channel(devpriv->tty, 31); // Start reading configuration
438 struct serial_data data;
440 data = serial_read(devpriv->tty, 1000);
441 if (data.kind != is_channel || data.index != 31
442 || !(data.value & 0xe0)) {
445 int command, channel, kind;
446 config_t *cur_config = 0;
448 channel = data.value & 0x1f;
449 kind = (data.value >> 5) & 0x7;
450 command = (data.value >> 8) & 0x3;
453 cur_config = dig_in_config;
457 cur_config = dig_out_config;
461 cur_config = chan_in_config;
465 cur_config = chan_out_config;
469 cur_config = chan_in_config;
475 cur_config[channel].kind = kind;
555 for (i = 0; i <= 4; i++) {
556 // Fill in subdev data
558 unsigned char *mapping = 0;
559 struct serial2002_range_table_t *range = 0;
565 mapping = devpriv->digital_in_mapping;
571 mapping = devpriv->digital_out_mapping;
577 mapping = devpriv->analog_in_mapping;
578 range = devpriv->in_range;
584 mapping = devpriv->analog_out_mapping;
585 range = devpriv->out_range;
591 mapping = devpriv->encoder_in_mapping;
592 range = devpriv->in_range;
602 struct comedi_subdevice *s;
603 const struct comedi_lrange **range_table_list = NULL;
604 unsigned int *maxdata_list;
607 for (chan = 0, j = 0; j < 32; j++) {
608 if (c[j].kind == kind) {
612 s = &dev->subdevices[i];
615 if (s->maxdata_list) {
616 kfree(s->maxdata_list);
618 s->maxdata_list = maxdata_list =
619 kmalloc(sizeof(unsigned int) * s->n_chan,
621 if (s->range_table_list) {
622 kfree(s->range_table_list);
626 s->range_table_list = range_table_list =
628 (struct serial2002_range_table_t) *
629 s->n_chan, GFP_KERNEL);
631 for (chan = 0, j = 0; j < 32; j++) {
632 if (c[j].kind == kind) {
642 range_table_list[chan] =
648 ((long long)1 << c[j].
658 static void serial_2002_close(struct comedi_device * dev)
660 if (!IS_ERR(devpriv->tty) && (devpriv->tty != 0)) {
661 filp_close(devpriv->tty, 0);
665 static int serial2002_di_rinsn(struct comedi_device * dev, struct comedi_subdevice * s,
666 struct comedi_insn * insn, unsigned int * data)
671 chan = devpriv->digital_in_mapping[CR_CHAN(insn->chanspec)];
672 for (n = 0; n < insn->n; n++) {
673 struct serial_data read;
675 poll_digital(devpriv->tty, chan);
677 read = serial_read(devpriv->tty, 1000);
678 if (read.kind != is_digital || read.index == chan) {
682 data[n] = read.value;
687 static int serial2002_do_winsn(struct comedi_device * dev, struct comedi_subdevice * s,
688 struct comedi_insn * insn, unsigned int * data)
693 chan = devpriv->digital_out_mapping[CR_CHAN(insn->chanspec)];
694 for (n = 0; n < insn->n; n++) {
695 struct serial_data write;
697 write.kind = is_digital;
699 write.value = data[n];
700 serial_write(devpriv->tty, write);
705 static int serial2002_ai_rinsn(struct comedi_device * dev, struct comedi_subdevice * s,
706 struct comedi_insn * insn, unsigned int * data)
711 chan = devpriv->analog_in_mapping[CR_CHAN(insn->chanspec)];
712 for (n = 0; n < insn->n; n++) {
713 struct serial_data read;
715 poll_channel(devpriv->tty, chan);
717 read = serial_read(devpriv->tty, 1000);
718 if (read.kind != is_channel || read.index == chan) {
722 data[n] = read.value;
727 static int serial2002_ao_winsn(struct comedi_device * dev, struct comedi_subdevice * s,
728 struct comedi_insn * insn, unsigned int * data)
733 chan = devpriv->analog_out_mapping[CR_CHAN(insn->chanspec)];
734 for (n = 0; n < insn->n; n++) {
735 struct serial_data write;
737 write.kind = is_channel;
739 write.value = data[n];
740 serial_write(devpriv->tty, write);
741 devpriv->ao_readback[chan] = data[n];
746 static int serial2002_ao_rinsn(struct comedi_device * dev, struct comedi_subdevice * s,
747 struct comedi_insn * insn, unsigned int * data)
750 int chan = CR_CHAN(insn->chanspec);
752 for (n = 0; n < insn->n; n++) {
753 data[n] = devpriv->ao_readback[chan];
759 static int serial2002_ei_rinsn(struct comedi_device * dev, struct comedi_subdevice * s,
760 struct comedi_insn * insn, unsigned int * data)
765 chan = devpriv->encoder_in_mapping[CR_CHAN(insn->chanspec)];
766 for (n = 0; n < insn->n; n++) {
767 struct serial_data read;
769 poll_channel(devpriv->tty, chan);
771 read = serial_read(devpriv->tty, 1000);
772 if (read.kind != is_channel || read.index == chan) {
776 data[n] = read.value;
781 static int serial2002_attach(struct comedi_device * dev, struct comedi_devconfig * it)
783 struct comedi_subdevice *s;
785 printk("comedi%d: serial2002: ", dev->minor);
786 dev->board_name = thisboard->name;
787 if (alloc_private(dev, sizeof(serial2002_private)) < 0) {
790 dev->open = serial_2002_open;
791 dev->close = serial_2002_close;
792 devpriv->port = it->options[0];
793 devpriv->speed = it->options[1];
794 printk("/dev/ttyS%d @ %d\n", devpriv->port, devpriv->speed);
796 if (alloc_subdevices(dev, 5) < 0)
799 /* digital input subdevice */
800 s = dev->subdevices + 0;
801 s->type = COMEDI_SUBD_DI;
802 s->subdev_flags = SDF_READABLE;
805 s->range_table = &range_digital;
806 s->insn_read = &serial2002_di_rinsn;
808 /* digital output subdevice */
809 s = dev->subdevices + 1;
810 s->type = COMEDI_SUBD_DO;
811 s->subdev_flags = SDF_WRITEABLE;
814 s->range_table = &range_digital;
815 s->insn_write = &serial2002_do_winsn;
817 /* analog input subdevice */
818 s = dev->subdevices + 2;
819 s->type = COMEDI_SUBD_AI;
820 s->subdev_flags = SDF_READABLE | SDF_GROUND;
824 s->insn_read = &serial2002_ai_rinsn;
826 /* analog output subdevice */
827 s = dev->subdevices + 3;
828 s->type = COMEDI_SUBD_AO;
829 s->subdev_flags = SDF_WRITEABLE;
833 s->insn_write = &serial2002_ao_winsn;
834 s->insn_read = &serial2002_ao_rinsn;
836 /* encoder input subdevice */
837 s = dev->subdevices + 4;
838 s->type = COMEDI_SUBD_COUNTER;
839 s->subdev_flags = SDF_READABLE | SDF_LSAMPL;
843 s->insn_read = &serial2002_ei_rinsn;
848 static int serial2002_detach(struct comedi_device * dev)
850 struct comedi_subdevice *s;
853 printk("comedi%d: serial2002: remove\n", dev->minor);
854 for (i = 0; i < 4; i++) {
855 s = &dev->subdevices[i];
856 if (s->maxdata_list) {
857 kfree(s->maxdata_list);
859 if (s->range_table_list) {
860 kfree(s->range_table_list);
866 COMEDI_INITCLEANUP(driver_serial2002);