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 struct serial2002_board {
53 static const struct serial2002_board serial2002_boards[] = {
59 * Useful for shorthand access to the particular board structure
61 #define thisboard ((const struct serial2002_board *)dev->board_ptr)
63 struct serial2002_range_table_t {
67 struct comedi_krange range;
70 struct serial2002_private {
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 ((struct serial2002_private *)dev->private)
90 static int serial2002_attach(struct comedi_device *dev,
91 struct comedi_devconfig *it);
92 static int serial2002_detach(struct comedi_device *dev);
93 struct comedi_driver driver_serial2002 = {
94 .driver_name = "serial2002",
95 .module = THIS_MODULE,
96 .attach = serial2002_attach,
97 .detach = serial2002_detach,
98 .board_name = &serial2002_boards[0].name,
99 .offset = sizeof(struct serial2002_board),
100 .num_names = ARRAY_SIZE(serial2002_boards),
103 static int serial2002_di_rinsn(struct comedi_device *dev,
104 struct comedi_subdevice *s,
105 struct comedi_insn *insn, unsigned int *data);
106 static int serial2002_do_winsn(struct comedi_device *dev,
107 struct comedi_subdevice *s,
108 struct comedi_insn *insn, unsigned int *data);
109 static int serial2002_ai_rinsn(struct comedi_device *dev,
110 struct comedi_subdevice *s,
111 struct comedi_insn *insn, unsigned int *data);
112 static int serial2002_ao_winsn(struct comedi_device *dev,
113 struct comedi_subdevice *s,
114 struct comedi_insn *insn, unsigned int *data);
115 static int serial2002_ao_rinsn(struct comedi_device *dev,
116 struct comedi_subdevice *s,
117 struct comedi_insn *insn, unsigned int *data);
120 enum { is_invalid, is_digital, is_channel } kind;
125 static long tty_ioctl(struct file *f, unsigned op, unsigned long param)
127 #ifdef HAVE_UNLOCKED_IOCTL
128 if (f->f_op->unlocked_ioctl) {
129 return f->f_op->unlocked_ioctl(f, op, param);
132 if (f->f_op->ioctl) {
133 return f->f_op->ioctl(f->f_dentry->d_inode, f, op, param);
138 static int tty_write(struct file *f, unsigned char *buf, int count)
146 result = f->f_op->write(f, buf, count, &f->f_pos);
153 * On 2.6.26.3 this occaisonally gave me page faults, worked around by
154 * settings.c_cc[VMIN] = 0; settings.c_cc[VTIME] = 0
156 static int tty_available(struct file *f)
163 tty_ioctl(f, FIONREAD, (unsigned long)&result);
169 static int tty_read(struct file *f, int timeout)
180 struct poll_wqueues table;
181 struct timeval start, now;
183 do_gettimeofday(&start);
184 poll_initwait(&table);
189 mask = f->f_op->poll(f, &table.pt);
190 if (mask & (POLLRDNORM | POLLRDBAND | POLLIN |
191 POLLHUP | POLLERR)) {
194 do_gettimeofday(&now);
196 (1000000 * (now.tv_sec - start.tv_sec) +
197 now.tv_usec - start.tv_usec);
198 if (elapsed > timeout) {
201 set_current_state(TASK_INTERRUPTIBLE);
202 schedule_timeout(((timeout -
203 elapsed) * HZ) / 10000);
205 poll_freewait(&table);
210 if (f->f_op->read(f, &ch, 1, &f->f_pos) == 1) {
215 /* Device does not support poll, busy wait */
221 if (retries >= timeout) {
226 if (f->f_op->read(f, &ch, 1, &f->f_pos) == 1) {
238 static void tty_setspeed(struct file *f, int speed)
246 struct termios settings;
248 tty_ioctl(f, TCGETS, (unsigned long)&settings);
249 /* printk("Speed: %d\n", settings.c_cflag & (CBAUD | CBAUDEX)); */
250 settings.c_iflag = 0;
251 settings.c_oflag = 0;
252 settings.c_lflag = 0;
253 settings.c_cflag = CLOCAL | CS8 | CREAD;
254 settings.c_cc[VMIN] = 0;
255 settings.c_cc[VTIME] = 0;
258 settings.c_cflag |= B2400;
262 settings.c_cflag |= B4800;
266 settings.c_cflag |= B9600;
270 settings.c_cflag |= B19200;
274 settings.c_cflag |= B38400;
278 settings.c_cflag |= B57600;
282 settings.c_cflag |= B115200;
286 settings.c_cflag |= B9600;
290 tty_ioctl(f, TCSETS, (unsigned long)&settings);
291 /* printk("Speed: %d\n", settings.c_cflag & (CBAUD | CBAUDEX)); */
294 /* Set low latency */
295 struct serial_struct settings;
297 tty_ioctl(f, TIOCGSERIAL, (unsigned long)&settings);
298 settings.flags |= ASYNC_LOW_LATENCY;
299 tty_ioctl(f, TIOCSSERIAL, (unsigned long)&settings);
305 static void poll_digital(struct file *f, int channel)
309 cmd = 0x40 | (channel & 0x1f);
310 tty_write(f, &cmd, 1);
313 static void poll_channel(struct file *f, int channel)
317 cmd = 0x60 | (channel & 0x1f);
318 tty_write(f, &cmd, 1);
321 static struct serial_data serial_read(struct file *f, int timeout)
323 struct serial_data result;
326 result.kind = is_invalid;
331 int data = tty_read(f, timeout);
335 printk("serial2002 error\n");
337 } else if (data & 0x80) {
338 result.value = (result.value << 7) | (data & 0x7f);
341 switch ((data >> 5) & 0x03) {
344 result.kind = is_digital;
349 result.kind = is_digital;
355 (result.value << 2) | ((data & 0x60) >> 5);
356 result.kind = is_channel;
358 result.index = data & 0x1f;
366 static void serial_write(struct file *f, struct serial_data data)
368 if (data.kind == is_digital) {
370 ((data.value << 5) & 0x20) | (data.index & 0x1f);
371 tty_write(f, &ch, 1);
375 if (data.value >= (1L << 30)) {
376 ch[i] = 0x80 | ((data.value >> 30) & 0x03);
379 if (data.value >= (1L << 23)) {
380 ch[i] = 0x80 | ((data.value >> 23) & 0x7f);
383 if (data.value >= (1L << 16)) {
384 ch[i] = 0x80 | ((data.value >> 16) & 0x7f);
387 if (data.value >= (1L << 9)) {
388 ch[i] = 0x80 | ((data.value >> 9) & 0x7f);
391 ch[i] = 0x80 | ((data.value >> 2) & 0x7f);
393 ch[i] = ((data.value << 5) & 0x60) | (data.index & 0x1f);
399 static void serial_2002_open(struct comedi_device *dev)
403 sprintf(port, "/dev/ttyS%d", devpriv->port);
404 devpriv->tty = filp_open(port, 0, O_RDWR);
405 if (IS_ERR(devpriv->tty)) {
406 printk("serial_2002: file open error = %ld\n",
407 PTR_ERR(devpriv->tty));
417 struct config_t dig_in_config[32];
418 struct config_t dig_out_config[32];
419 struct config_t chan_in_config[32];
420 struct config_t chan_out_config[32];
423 for (i = 0; i < 32; i++) {
424 dig_in_config[i].kind = 0;
425 dig_in_config[i].bits = 0;
426 dig_in_config[i].min = 0;
427 dig_in_config[i].max = 0;
428 dig_out_config[i].kind = 0;
429 dig_out_config[i].bits = 0;
430 dig_out_config[i].min = 0;
431 dig_out_config[i].max = 0;
432 chan_in_config[i].kind = 0;
433 chan_in_config[i].bits = 0;
434 chan_in_config[i].min = 0;
435 chan_in_config[i].max = 0;
436 chan_out_config[i].kind = 0;
437 chan_out_config[i].bits = 0;
438 chan_out_config[i].min = 0;
439 chan_out_config[i].max = 0;
442 tty_setspeed(devpriv->tty, devpriv->speed);
443 poll_channel(devpriv->tty, 31); /* Start reading configuration */
445 struct serial_data data;
447 data = serial_read(devpriv->tty, 1000);
448 if (data.kind != is_channel || data.index != 31
449 || !(data.value & 0xe0)) {
452 int command, channel, kind;
453 struct config_t *cur_config = 0;
455 channel = data.value & 0x1f;
456 kind = (data.value >> 5) & 0x7;
457 command = (data.value >> 8) & 0x3;
460 cur_config = dig_in_config;
464 cur_config = dig_out_config;
468 cur_config = chan_in_config;
472 cur_config = chan_out_config;
476 cur_config = chan_in_config;
482 cur_config[channel].kind = kind;
485 cur_config[channel].bits
528 cur_config[channel].min
569 cur_config[channel].max
577 for (i = 0; i <= 4; i++) {
578 /* Fill in subdev data */
580 unsigned char *mapping = 0;
581 struct serial2002_range_table_t *range = 0;
587 mapping = devpriv->digital_in_mapping;
593 mapping = devpriv->digital_out_mapping;
599 mapping = devpriv->analog_in_mapping;
600 range = devpriv->in_range;
606 mapping = devpriv->analog_out_mapping;
607 range = devpriv->out_range;
613 mapping = devpriv->encoder_in_mapping;
614 range = devpriv->in_range;
624 struct comedi_subdevice *s;
625 const struct comedi_lrange **range_table_list =
627 unsigned int *maxdata_list;
630 for (chan = 0, j = 0; j < 32; j++) {
631 if (c[j].kind == kind) {
635 s = &dev->subdevices[i];
638 if (s->maxdata_list) {
639 kfree(s->maxdata_list);
641 s->maxdata_list = maxdata_list =
642 kmalloc(sizeof(unsigned int) * s->n_chan,
644 if (s->range_table_list) {
645 kfree(s->range_table_list);
649 s->range_table_list = range_table_list =
652 serial2002_range_table_t) *
653 s->n_chan, GFP_KERNEL);
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)
682 static void serial_2002_close(struct comedi_device *dev)
684 if (!IS_ERR(devpriv->tty) && (devpriv->tty != 0)) {
685 filp_close(devpriv->tty, 0);
689 static int serial2002_di_rinsn(struct comedi_device *dev,
690 struct comedi_subdevice *s,
691 struct comedi_insn *insn, unsigned int *data)
696 chan = devpriv->digital_in_mapping[CR_CHAN(insn->chanspec)];
697 for (n = 0; n < insn->n; n++) {
698 struct serial_data read;
700 poll_digital(devpriv->tty, chan);
702 read = serial_read(devpriv->tty, 1000);
703 if (read.kind != is_digital || read.index == chan) {
707 data[n] = read.value;
712 static int serial2002_do_winsn(struct comedi_device *dev,
713 struct comedi_subdevice *s,
714 struct comedi_insn *insn, unsigned int *data)
719 chan = devpriv->digital_out_mapping[CR_CHAN(insn->chanspec)];
720 for (n = 0; n < insn->n; n++) {
721 struct serial_data write;
723 write.kind = is_digital;
725 write.value = data[n];
726 serial_write(devpriv->tty, write);
731 static int serial2002_ai_rinsn(struct comedi_device *dev,
732 struct comedi_subdevice *s,
733 struct comedi_insn *insn, unsigned int *data)
738 chan = devpriv->analog_in_mapping[CR_CHAN(insn->chanspec)];
739 for (n = 0; n < insn->n; n++) {
740 struct serial_data read;
742 poll_channel(devpriv->tty, chan);
744 read = serial_read(devpriv->tty, 1000);
745 if (read.kind != is_channel || read.index == chan) {
749 data[n] = read.value;
754 static int serial2002_ao_winsn(struct comedi_device *dev,
755 struct comedi_subdevice *s,
756 struct comedi_insn *insn, unsigned int *data)
761 chan = devpriv->analog_out_mapping[CR_CHAN(insn->chanspec)];
762 for (n = 0; n < insn->n; n++) {
763 struct serial_data write;
765 write.kind = is_channel;
767 write.value = data[n];
768 serial_write(devpriv->tty, write);
769 devpriv->ao_readback[chan] = data[n];
774 static int serial2002_ao_rinsn(struct comedi_device *dev,
775 struct comedi_subdevice *s,
776 struct comedi_insn *insn, unsigned int *data)
779 int chan = CR_CHAN(insn->chanspec);
781 for (n = 0; n < insn->n; n++) {
782 data[n] = devpriv->ao_readback[chan];
788 static int serial2002_ei_rinsn(struct comedi_device *dev,
789 struct comedi_subdevice *s,
790 struct comedi_insn *insn, unsigned int *data)
795 chan = devpriv->encoder_in_mapping[CR_CHAN(insn->chanspec)];
796 for (n = 0; n < insn->n; n++) {
797 struct serial_data read;
799 poll_channel(devpriv->tty, chan);
801 read = serial_read(devpriv->tty, 1000);
802 if (read.kind != is_channel || read.index == chan) {
806 data[n] = read.value;
811 static int serial2002_attach(struct comedi_device *dev,
812 struct comedi_devconfig *it)
814 struct comedi_subdevice *s;
816 printk("comedi%d: serial2002: ", dev->minor);
817 dev->board_name = thisboard->name;
818 if (alloc_private(dev, sizeof(struct serial2002_private)) < 0) {
821 dev->open = serial_2002_open;
822 dev->close = serial_2002_close;
823 devpriv->port = it->options[0];
824 devpriv->speed = it->options[1];
825 printk("/dev/ttyS%d @ %d\n", devpriv->port, devpriv->speed);
827 if (alloc_subdevices(dev, 5) < 0)
830 /* digital input subdevice */
831 s = dev->subdevices + 0;
832 s->type = COMEDI_SUBD_DI;
833 s->subdev_flags = SDF_READABLE;
836 s->range_table = &range_digital;
837 s->insn_read = &serial2002_di_rinsn;
839 /* digital output subdevice */
840 s = dev->subdevices + 1;
841 s->type = COMEDI_SUBD_DO;
842 s->subdev_flags = SDF_WRITEABLE;
845 s->range_table = &range_digital;
846 s->insn_write = &serial2002_do_winsn;
848 /* analog input subdevice */
849 s = dev->subdevices + 2;
850 s->type = COMEDI_SUBD_AI;
851 s->subdev_flags = SDF_READABLE | SDF_GROUND;
855 s->insn_read = &serial2002_ai_rinsn;
857 /* analog output subdevice */
858 s = dev->subdevices + 3;
859 s->type = COMEDI_SUBD_AO;
860 s->subdev_flags = SDF_WRITEABLE;
864 s->insn_write = &serial2002_ao_winsn;
865 s->insn_read = &serial2002_ao_rinsn;
867 /* encoder input subdevice */
868 s = dev->subdevices + 4;
869 s->type = COMEDI_SUBD_COUNTER;
870 s->subdev_flags = SDF_READABLE | SDF_LSAMPL;
874 s->insn_read = &serial2002_ei_rinsn;
879 static int serial2002_detach(struct comedi_device *dev)
881 struct comedi_subdevice *s;
884 printk("comedi%d: serial2002: remove\n", dev->minor);
885 for (i = 0; i < 4; i++) {
886 s = &dev->subdevices[i];
887 if (s->maxdata_list) {
888 kfree(s->maxdata_list);
890 if (s->range_table_list) {
891 kfree(s->range_table_list);
897 COMEDI_INITCLEANUP(driver_serial2002);