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>
39 #include <linux/slab.h>
41 #include <asm/termios.h>
42 #include <asm/ioctls.h>
43 #include <linux/serial.h>
44 #include <linux/poll.h>
47 * Board descriptions for two imaginary boards. Describing the
48 * boards in this way is optional, and completely driver-dependent.
49 * Some drivers use arrays such as this, other do not.
51 struct serial2002_board {
55 static const struct serial2002_board serial2002_boards[] = {
61 * Useful for shorthand access to the particular board structure
63 #define thisboard ((const struct serial2002_board *)dev->board_ptr)
65 struct serial2002_range_table_t {
69 struct comedi_krange range;
72 struct serial2002_private {
74 int port; /* /dev/ttyS<port> */
75 int speed; /* baudrate */
77 unsigned int ao_readback[32];
78 unsigned char digital_in_mapping[32];
79 unsigned char digital_out_mapping[32];
80 unsigned char analog_in_mapping[32];
81 unsigned char analog_out_mapping[32];
82 unsigned char encoder_in_mapping[32];
83 struct serial2002_range_table_t in_range[32], out_range[32];
87 * most drivers define the following macro to make it easy to
88 * access the private structure.
90 #define devpriv ((struct serial2002_private *)dev->private)
92 static int serial2002_attach(struct comedi_device *dev,
93 struct comedi_devconfig *it);
94 static int serial2002_detach(struct comedi_device *dev);
95 struct comedi_driver driver_serial2002 = {
96 .driver_name = "serial2002",
97 .module = THIS_MODULE,
98 .attach = serial2002_attach,
99 .detach = serial2002_detach,
100 .board_name = &serial2002_boards[0].name,
101 .offset = sizeof(struct serial2002_board),
102 .num_names = ARRAY_SIZE(serial2002_boards),
105 static int serial2002_di_rinsn(struct comedi_device *dev,
106 struct comedi_subdevice *s,
107 struct comedi_insn *insn, unsigned int *data);
108 static int serial2002_do_winsn(struct comedi_device *dev,
109 struct comedi_subdevice *s,
110 struct comedi_insn *insn, unsigned int *data);
111 static int serial2002_ai_rinsn(struct comedi_device *dev,
112 struct comedi_subdevice *s,
113 struct comedi_insn *insn, unsigned int *data);
114 static int serial2002_ao_winsn(struct comedi_device *dev,
115 struct comedi_subdevice *s,
116 struct comedi_insn *insn, unsigned int *data);
117 static int serial2002_ao_rinsn(struct comedi_device *dev,
118 struct comedi_subdevice *s,
119 struct comedi_insn *insn, unsigned int *data);
122 enum { is_invalid, is_digital, is_channel } kind;
127 static long tty_ioctl(struct file *f, unsigned op, unsigned long param)
129 if (f->f_op->unlocked_ioctl)
130 return f->f_op->unlocked_ioctl(f, op, param);
135 static int tty_write(struct file *f, unsigned char *buf, int count)
143 result = f->f_op->write(f, buf, count, &f->f_pos);
150 * On 2.6.26.3 this occaisonally gave me page faults, worked around by
151 * settings.c_cc[VMIN] = 0; settings.c_cc[VTIME] = 0
153 static int tty_available(struct file *f)
160 tty_ioctl(f, FIONREAD, (unsigned long)&result);
166 static int tty_read(struct file *f, int timeout)
177 struct poll_wqueues table;
178 struct timeval start, now;
180 do_gettimeofday(&start);
181 poll_initwait(&table);
186 mask = f->f_op->poll(f, &table.pt);
187 if (mask & (POLLRDNORM | POLLRDBAND | POLLIN |
188 POLLHUP | POLLERR)) {
191 do_gettimeofday(&now);
193 (1000000 * (now.tv_sec - start.tv_sec) +
194 now.tv_usec - start.tv_usec);
195 if (elapsed > timeout) {
198 set_current_state(TASK_INTERRUPTIBLE);
199 schedule_timeout(((timeout -
200 elapsed) * HZ) / 10000);
202 poll_freewait(&table);
207 if (f->f_op->read(f, &ch, 1, &f->f_pos) == 1) {
212 /* Device does not support poll, busy wait */
218 if (retries >= timeout) {
223 if (f->f_op->read(f, &ch, 1, &f->f_pos) == 1) {
235 static void tty_setspeed(struct file *f, int speed)
243 struct termios settings;
245 tty_ioctl(f, TCGETS, (unsigned long)&settings);
246 /* printk("Speed: %d\n", settings.c_cflag & (CBAUD | CBAUDEX)); */
247 settings.c_iflag = 0;
248 settings.c_oflag = 0;
249 settings.c_lflag = 0;
250 settings.c_cflag = CLOCAL | CS8 | CREAD;
251 settings.c_cc[VMIN] = 0;
252 settings.c_cc[VTIME] = 0;
255 settings.c_cflag |= B2400;
259 settings.c_cflag |= B4800;
263 settings.c_cflag |= B9600;
267 settings.c_cflag |= B19200;
271 settings.c_cflag |= B38400;
275 settings.c_cflag |= B57600;
279 settings.c_cflag |= B115200;
283 settings.c_cflag |= B9600;
287 tty_ioctl(f, TCSETS, (unsigned long)&settings);
288 /* printk("Speed: %d\n", settings.c_cflag & (CBAUD | CBAUDEX)); */
291 /* Set low latency */
292 struct serial_struct settings;
294 tty_ioctl(f, TIOCGSERIAL, (unsigned long)&settings);
295 settings.flags |= ASYNC_LOW_LATENCY;
296 tty_ioctl(f, TIOCSSERIAL, (unsigned long)&settings);
302 static void poll_digital(struct file *f, int channel)
306 cmd = 0x40 | (channel & 0x1f);
307 tty_write(f, &cmd, 1);
310 static void poll_channel(struct file *f, int channel)
314 cmd = 0x60 | (channel & 0x1f);
315 tty_write(f, &cmd, 1);
318 static struct serial_data serial_read(struct file *f, int timeout)
320 struct serial_data result;
323 result.kind = is_invalid;
328 int data = tty_read(f, timeout);
332 printk("serial2002 error\n");
334 } else if (data & 0x80) {
335 result.value = (result.value << 7) | (data & 0x7f);
338 switch ((data >> 5) & 0x03) {
341 result.kind = is_digital;
346 result.kind = is_digital;
352 (result.value << 2) | ((data & 0x60) >> 5);
353 result.kind = is_channel;
355 result.index = data & 0x1f;
363 static void serial_write(struct file *f, struct serial_data data)
365 if (data.kind == is_digital) {
367 ((data.value << 5) & 0x20) | (data.index & 0x1f);
368 tty_write(f, &ch, 1);
372 if (data.value >= (1L << 30)) {
373 ch[i] = 0x80 | ((data.value >> 30) & 0x03);
376 if (data.value >= (1L << 23)) {
377 ch[i] = 0x80 | ((data.value >> 23) & 0x7f);
380 if (data.value >= (1L << 16)) {
381 ch[i] = 0x80 | ((data.value >> 16) & 0x7f);
384 if (data.value >= (1L << 9)) {
385 ch[i] = 0x80 | ((data.value >> 9) & 0x7f);
388 ch[i] = 0x80 | ((data.value >> 2) & 0x7f);
390 ch[i] = ((data.value << 5) & 0x60) | (data.index & 0x1f);
396 static int serial_2002_open(struct comedi_device *dev)
401 sprintf(port, "/dev/ttyS%d", devpriv->port);
402 devpriv->tty = filp_open(port, O_RDWR, 0);
403 if (IS_ERR(devpriv->tty)) {
404 result = (int)PTR_ERR(devpriv->tty);
405 printk("serial_2002: file open error = %d\n", result);
415 struct config_t dig_in_config[32];
416 struct config_t dig_out_config[32];
417 struct config_t chan_in_config[32];
418 struct config_t chan_out_config[32];
422 for (i = 0; i < 32; i++) {
423 dig_in_config[i].kind = 0;
424 dig_in_config[i].bits = 0;
425 dig_in_config[i].min = 0;
426 dig_in_config[i].max = 0;
427 dig_out_config[i].kind = 0;
428 dig_out_config[i].bits = 0;
429 dig_out_config[i].min = 0;
430 dig_out_config[i].max = 0;
431 chan_in_config[i].kind = 0;
432 chan_in_config[i].bits = 0;
433 chan_in_config[i].min = 0;
434 chan_in_config[i].max = 0;
435 chan_out_config[i].kind = 0;
436 chan_out_config[i].bits = 0;
437 chan_out_config[i].min = 0;
438 chan_out_config[i].max = 0;
441 tty_setspeed(devpriv->tty, devpriv->speed);
442 poll_channel(devpriv->tty, 31); /* Start reading configuration */
444 struct serial_data data;
446 data = serial_read(devpriv->tty, 1000);
447 if (data.kind != is_channel || data.index != 31
448 || !(data.value & 0xe0)) {
451 int command, channel, kind;
452 struct config_t *cur_config = 0;
454 channel = data.value & 0x1f;
455 kind = (data.value >> 5) & 0x7;
456 command = (data.value >> 8) & 0x3;
459 cur_config = dig_in_config;
463 cur_config = dig_out_config;
467 cur_config = chan_in_config;
471 cur_config = chan_out_config;
475 cur_config = chan_in_config;
481 cur_config[channel].kind = kind;
484 cur_config[channel].bits
527 cur_config[channel].min
568 cur_config[channel].max
576 for (i = 0; i <= 4; i++) {
577 /* Fill in subdev data */
579 unsigned char *mapping = 0;
580 struct serial2002_range_table_t *range = 0;
586 mapping = devpriv->digital_in_mapping;
592 mapping = devpriv->digital_out_mapping;
598 mapping = devpriv->analog_in_mapping;
599 range = devpriv->in_range;
605 mapping = devpriv->analog_out_mapping;
606 range = devpriv->out_range;
612 mapping = devpriv->encoder_in_mapping;
613 range = devpriv->in_range;
623 struct comedi_subdevice *s;
624 const struct comedi_lrange **range_table_list =
626 unsigned int *maxdata_list;
629 for (chan = 0, j = 0; j < 32; j++) {
630 if (c[j].kind == kind) {
634 s = &dev->subdevices[i];
637 kfree(s->maxdata_list);
638 s->maxdata_list = maxdata_list =
639 kmalloc(sizeof(unsigned int) * s->n_chan,
641 if (!s->maxdata_list)
642 break; /* error handled below */
643 kfree(s->range_table_list);
644 s->range_table = NULL;
645 s->range_table_list = NULL;
647 s->range_table_list = range_table_list =
650 serial2002_range_table_t) *
651 s->n_chan, GFP_KERNEL);
652 if (!s->range_table_list)
653 break; /* err handled below */
655 for (chan = 0, j = 0; j < 32; j++) {
656 if (c[j].kind == kind) {
666 range_table_list[chan] =
672 ((long long)1 << c[j].bits)
680 /* Failed to allocate maxdata_list or range_table_list
681 * for a subdevice that needed it. */
683 for (i = 0; i <= 4; i++) {
684 struct comedi_subdevice *s;
686 s = &dev->subdevices[i];
687 kfree(s->maxdata_list);
688 s->maxdata_list = NULL;
689 kfree(s->range_table_list);
690 s->range_table_list = NULL;
695 filp_close(devpriv->tty, 0);
703 static void serial_2002_close(struct comedi_device *dev)
705 if (!IS_ERR(devpriv->tty) && (devpriv->tty != 0)) {
706 filp_close(devpriv->tty, 0);
710 static int serial2002_di_rinsn(struct comedi_device *dev,
711 struct comedi_subdevice *s,
712 struct comedi_insn *insn, unsigned int *data)
717 chan = devpriv->digital_in_mapping[CR_CHAN(insn->chanspec)];
718 for (n = 0; n < insn->n; n++) {
719 struct serial_data read;
721 poll_digital(devpriv->tty, chan);
723 read = serial_read(devpriv->tty, 1000);
724 if (read.kind != is_digital || read.index == chan) {
728 data[n] = read.value;
733 static int serial2002_do_winsn(struct comedi_device *dev,
734 struct comedi_subdevice *s,
735 struct comedi_insn *insn, unsigned int *data)
740 chan = devpriv->digital_out_mapping[CR_CHAN(insn->chanspec)];
741 for (n = 0; n < insn->n; n++) {
742 struct serial_data write;
744 write.kind = is_digital;
746 write.value = data[n];
747 serial_write(devpriv->tty, write);
752 static int serial2002_ai_rinsn(struct comedi_device *dev,
753 struct comedi_subdevice *s,
754 struct comedi_insn *insn, unsigned int *data)
759 chan = devpriv->analog_in_mapping[CR_CHAN(insn->chanspec)];
760 for (n = 0; n < insn->n; n++) {
761 struct serial_data read;
763 poll_channel(devpriv->tty, chan);
765 read = serial_read(devpriv->tty, 1000);
766 if (read.kind != is_channel || read.index == chan) {
770 data[n] = read.value;
775 static int serial2002_ao_winsn(struct comedi_device *dev,
776 struct comedi_subdevice *s,
777 struct comedi_insn *insn, unsigned int *data)
782 chan = devpriv->analog_out_mapping[CR_CHAN(insn->chanspec)];
783 for (n = 0; n < insn->n; n++) {
784 struct serial_data write;
786 write.kind = is_channel;
788 write.value = data[n];
789 serial_write(devpriv->tty, write);
790 devpriv->ao_readback[chan] = data[n];
795 static int serial2002_ao_rinsn(struct comedi_device *dev,
796 struct comedi_subdevice *s,
797 struct comedi_insn *insn, unsigned int *data)
800 int chan = CR_CHAN(insn->chanspec);
802 for (n = 0; n < insn->n; n++) {
803 data[n] = devpriv->ao_readback[chan];
809 static int serial2002_ei_rinsn(struct comedi_device *dev,
810 struct comedi_subdevice *s,
811 struct comedi_insn *insn, unsigned int *data)
816 chan = devpriv->encoder_in_mapping[CR_CHAN(insn->chanspec)];
817 for (n = 0; n < insn->n; n++) {
818 struct serial_data read;
820 poll_channel(devpriv->tty, chan);
822 read = serial_read(devpriv->tty, 1000);
823 if (read.kind != is_channel || read.index == chan) {
827 data[n] = read.value;
832 static int serial2002_attach(struct comedi_device *dev,
833 struct comedi_devconfig *it)
835 struct comedi_subdevice *s;
837 printk("comedi%d: serial2002: ", dev->minor);
838 dev->board_name = thisboard->name;
839 if (alloc_private(dev, sizeof(struct serial2002_private)) < 0) {
842 dev->open = serial_2002_open;
843 dev->close = serial_2002_close;
844 devpriv->port = it->options[0];
845 devpriv->speed = it->options[1];
846 printk("/dev/ttyS%d @ %d\n", devpriv->port, devpriv->speed);
848 if (alloc_subdevices(dev, 5) < 0)
851 /* digital input subdevice */
852 s = dev->subdevices + 0;
853 s->type = COMEDI_SUBD_DI;
854 s->subdev_flags = SDF_READABLE;
857 s->range_table = &range_digital;
858 s->insn_read = &serial2002_di_rinsn;
860 /* digital output subdevice */
861 s = dev->subdevices + 1;
862 s->type = COMEDI_SUBD_DO;
863 s->subdev_flags = SDF_WRITEABLE;
866 s->range_table = &range_digital;
867 s->insn_write = &serial2002_do_winsn;
869 /* analog input subdevice */
870 s = dev->subdevices + 2;
871 s->type = COMEDI_SUBD_AI;
872 s->subdev_flags = SDF_READABLE | SDF_GROUND;
876 s->insn_read = &serial2002_ai_rinsn;
878 /* analog output subdevice */
879 s = dev->subdevices + 3;
880 s->type = COMEDI_SUBD_AO;
881 s->subdev_flags = SDF_WRITEABLE;
885 s->insn_write = &serial2002_ao_winsn;
886 s->insn_read = &serial2002_ao_rinsn;
888 /* encoder input subdevice */
889 s = dev->subdevices + 4;
890 s->type = COMEDI_SUBD_COUNTER;
891 s->subdev_flags = SDF_READABLE | SDF_LSAMPL;
895 s->insn_read = &serial2002_ei_rinsn;
900 static int serial2002_detach(struct comedi_device *dev)
902 struct comedi_subdevice *s;
905 printk("comedi%d: serial2002: remove\n", dev->minor);
906 for (i = 0; i < 5; i++) {
907 s = &dev->subdevices[i];
908 if (s->maxdata_list) {
909 kfree(s->maxdata_list);
911 if (s->range_table_list) {
912 kfree(s->range_table_list);
918 COMEDI_INITCLEANUP(driver_serial2002);