3 * Comedi driver for serial connected hardware
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
28 #include <linux/module.h>
29 #include "../comedidev.h"
31 #include <linux/delay.h>
32 #include <linux/sched.h>
33 #include <linux/slab.h>
34 #include <linux/ktime.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 {
44 struct comedi_krange range;
47 struct serial2002_private {
48 int port; /* /dev/ttyS<port> */
49 int speed; /* baudrate */
51 unsigned int ao_readback[32];
52 unsigned char digital_in_mapping[32];
53 unsigned char digital_out_mapping[32];
54 unsigned char analog_in_mapping[32];
55 unsigned char analog_out_mapping[32];
56 unsigned char encoder_in_mapping[32];
57 struct serial2002_range_table_t in_range[32], out_range[32];
61 enum { is_invalid, is_digital, is_channel } kind;
67 * The configuration serial_data.value read from the device is
68 * a bitmask that defines specific options of a channel:
70 * 4:0 - the channel to configure
71 * 7:5 - the kind of channel
72 * 9:8 - the command used to configure the channel
74 * The remaining bits vary in use depending on the command:
76 * BITS 15:10 - the channel bits (maxdata)
77 * MIN/MAX 12:10 - the units multiplier for the scale
78 * 13 - the sign of the scale
79 * 33:14 - the base value for the range
81 #define S2002_CFG_CHAN(x) ((x) & 0x1f)
82 #define S2002_CFG_KIND(x) (((x) >> 5) & 0x7)
83 #define S2002_CFG_KIND_INVALID 0
84 #define S2002_CFG_KIND_DIGITAL_IN 1
85 #define S2002_CFG_KIND_DIGITAL_OUT 2
86 #define S2002_CFG_KIND_ANALOG_IN 3
87 #define S2002_CFG_KIND_ANALOG_OUT 4
88 #define S2002_CFG_KIND_ENCODER_IN 5
89 #define S2002_CFG_CMD(x) (((x) >> 8) & 0x3)
90 #define S2002_CFG_CMD_BITS 0
91 #define S2002_CFG_CMD_MIN 1
92 #define S2002_CFG_CMD_MAX 2
93 #define S2002_CFG_BITS(x) (((x) >> 10) & 0x3f)
94 #define S2002_CFG_UNITS(x) (((x) >> 10) & 0x7)
95 #define S2002_CFG_SIGN(x) (((x) >> 13) & 0x1)
96 #define S2002_CFG_BASE(x) (((x) >> 14) & 0xfffff)
98 static long serial2002_tty_ioctl(struct file *f, unsigned op,
101 if (f->f_op->unlocked_ioctl)
102 return f->f_op->unlocked_ioctl(f, op, param);
107 static int serial2002_tty_write(struct file *f, unsigned char *buf, int count)
109 const char __user *p = (__force const char __user *)buf;
116 result = __vfs_write(f, p, count, &offset);
121 static void serial2002_tty_read_poll_wait(struct file *f, int timeout)
123 struct poll_wqueues table;
127 poll_initwait(&table);
132 mask = f->f_op->poll(f, &table.pt);
133 if (mask & (POLLRDNORM | POLLRDBAND | POLLIN |
134 POLLHUP | POLLERR)) {
138 elapsed = ktime_us_delta(now, start);
139 if (elapsed > timeout)
141 set_current_state(TASK_INTERRUPTIBLE);
142 schedule_timeout(((timeout - elapsed) * HZ) / 10000);
144 poll_freewait(&table);
147 static int serial2002_tty_read(struct file *f, int timeout)
155 char __user *p = (__force char __user *)&ch;
161 serial2002_tty_read_poll_wait(f, timeout);
163 if (__vfs_read(f, p, 1, &offset) == 1)
166 /* Device does not support poll, busy wait */
171 if (retries >= timeout)
174 if (__vfs_read(f, p, 1, &offset) == 1) {
178 usleep_range(100, 1000);
186 static void serial2002_tty_setspeed(struct file *f, int speed)
188 struct termios termios;
189 struct serial_struct serial;
196 serial2002_tty_ioctl(f, TCGETS, (unsigned long)&termios);
200 termios.c_cflag = CLOCAL | CS8 | CREAD;
201 termios.c_cc[VMIN] = 0;
202 termios.c_cc[VTIME] = 0;
205 termios.c_cflag |= B2400;
208 termios.c_cflag |= B4800;
211 termios.c_cflag |= B9600;
214 termios.c_cflag |= B19200;
217 termios.c_cflag |= B38400;
220 termios.c_cflag |= B57600;
223 termios.c_cflag |= B115200;
226 termios.c_cflag |= B9600;
229 serial2002_tty_ioctl(f, TCSETS, (unsigned long)&termios);
231 /* Set low latency */
232 serial2002_tty_ioctl(f, TIOCGSERIAL, (unsigned long)&serial);
233 serial.flags |= ASYNC_LOW_LATENCY;
234 serial2002_tty_ioctl(f, TIOCSSERIAL, (unsigned long)&serial);
239 static void serial2002_poll_digital(struct file *f, int channel)
243 cmd = 0x40 | (channel & 0x1f);
244 serial2002_tty_write(f, &cmd, 1);
247 static void serial2002_poll_channel(struct file *f, int channel)
251 cmd = 0x60 | (channel & 0x1f);
252 serial2002_tty_write(f, &cmd, 1);
255 static struct serial_data serial2002_read(struct file *f, int timeout)
257 struct serial_data result;
260 result.kind = is_invalid;
265 int data = serial2002_tty_read(f, timeout);
270 } else if (data & 0x80) {
271 result.value = (result.value << 7) | (data & 0x7f);
274 switch ((data >> 5) & 0x03) {
277 result.kind = is_digital;
281 result.kind = is_digital;
286 (result.value << 2) | ((data & 0x60) >> 5);
287 result.kind = is_channel;
289 result.index = data & 0x1f;
296 static void serial2002_write(struct file *f, struct serial_data data)
298 if (data.kind == is_digital) {
300 ((data.value << 5) & 0x20) | (data.index & 0x1f);
301 serial2002_tty_write(f, &ch, 1);
306 if (data.value >= (1L << 30)) {
307 ch[i] = 0x80 | ((data.value >> 30) & 0x03);
310 if (data.value >= (1L << 23)) {
311 ch[i] = 0x80 | ((data.value >> 23) & 0x7f);
314 if (data.value >= (1L << 16)) {
315 ch[i] = 0x80 | ((data.value >> 16) & 0x7f);
318 if (data.value >= (1L << 9)) {
319 ch[i] = 0x80 | ((data.value >> 9) & 0x7f);
322 ch[i] = 0x80 | ((data.value >> 2) & 0x7f);
324 ch[i] = ((data.value << 5) & 0x60) | (data.index & 0x1f);
326 serial2002_tty_write(f, ch, i);
337 static int serial2002_setup_subdevice(struct comedi_subdevice *s,
338 struct config_t *cfg,
339 struct serial2002_range_table_t *range,
340 unsigned char *mapping,
343 const struct comedi_lrange **range_table_list = NULL;
344 unsigned int *maxdata_list;
347 for (chan = 0, j = 0; j < 32; j++) {
348 if (cfg[j].kind == kind)
353 kfree(s->maxdata_list);
354 maxdata_list = kmalloc_array(s->n_chan, sizeof(unsigned int),
358 s->maxdata_list = maxdata_list;
359 kfree(s->range_table_list);
360 s->range_table = NULL;
361 s->range_table_list = NULL;
362 if (kind == 1 || kind == 2) {
363 s->range_table = &range_digital;
365 range_table_list = kmalloc_array(s->n_chan, sizeof(*range),
367 if (!range_table_list)
369 s->range_table_list = range_table_list;
371 for (chan = 0, j = 0; j < 32; j++) {
372 if (cfg[j].kind == kind) {
375 if (range && range_table_list) {
377 range[j].range.min = cfg[j].min;
378 range[j].range.max = cfg[j].max;
379 range_table_list[chan] =
380 (const struct comedi_lrange *)&range[j];
382 maxdata_list[chan] = ((long long)1 << cfg[j].bits) - 1;
389 static int serial2002_setup_subdevs(struct comedi_device *dev)
391 struct serial2002_private *devpriv = dev->private;
392 struct config_t *di_cfg;
393 struct config_t *do_cfg;
394 struct config_t *ai_cfg;
395 struct config_t *ao_cfg;
396 struct config_t *cfg;
397 struct comedi_subdevice *s;
401 /* Allocate the temporary structs to hold the configuration data */
402 di_cfg = kcalloc(32, sizeof(*cfg), GFP_KERNEL);
403 do_cfg = kcalloc(32, sizeof(*cfg), GFP_KERNEL);
404 ai_cfg = kcalloc(32, sizeof(*cfg), GFP_KERNEL);
405 ao_cfg = kcalloc(32, sizeof(*cfg), GFP_KERNEL);
406 if (!di_cfg || !do_cfg || !ai_cfg || !ao_cfg) {
408 goto err_alloc_configs;
411 /* Read the configuration from the connected device */
412 serial2002_tty_setspeed(devpriv->tty, devpriv->speed);
413 serial2002_poll_channel(devpriv->tty, 31);
415 struct serial_data data = serial2002_read(devpriv->tty, 1000);
416 int kind = S2002_CFG_KIND(data.value);
417 int channel = S2002_CFG_CHAN(data.value);
418 int range = S2002_CFG_BASE(data.value);
419 int cmd = S2002_CFG_CMD(data.value);
421 if (data.kind != is_channel || data.index != 31 ||
422 kind == S2002_CFG_KIND_INVALID)
426 case S2002_CFG_KIND_DIGITAL_IN:
429 case S2002_CFG_KIND_DIGITAL_OUT:
432 case S2002_CFG_KIND_ANALOG_IN:
435 case S2002_CFG_KIND_ANALOG_OUT:
438 case S2002_CFG_KIND_ENCODER_IN:
446 continue; /* unknown kind, skip it */
448 cfg[channel].kind = kind;
451 case S2002_CFG_CMD_BITS:
452 cfg[channel].bits = S2002_CFG_BITS(data.value);
454 case S2002_CFG_CMD_MIN:
455 case S2002_CFG_CMD_MAX:
456 switch (S2002_CFG_UNITS(data.value)) {
467 if (S2002_CFG_SIGN(data.value))
469 if (cmd == S2002_CFG_CMD_MIN)
470 cfg[channel].min = range;
472 cfg[channel].max = range;
477 /* Fill in subdevice data */
478 for (i = 0; i <= 4; i++) {
479 unsigned char *mapping = NULL;
480 struct serial2002_range_table_t *range = NULL;
483 s = &dev->subdevices[i];
488 mapping = devpriv->digital_in_mapping;
489 kind = S2002_CFG_KIND_DIGITAL_IN;
493 mapping = devpriv->digital_out_mapping;
494 kind = S2002_CFG_KIND_DIGITAL_OUT;
498 mapping = devpriv->analog_in_mapping;
499 range = devpriv->in_range;
500 kind = S2002_CFG_KIND_ANALOG_IN;
504 mapping = devpriv->analog_out_mapping;
505 range = devpriv->out_range;
506 kind = S2002_CFG_KIND_ANALOG_OUT;
510 mapping = devpriv->encoder_in_mapping;
511 range = devpriv->in_range;
512 kind = S2002_CFG_KIND_ENCODER_IN;
516 if (serial2002_setup_subdevice(s, cfg, range, mapping, kind))
517 break; /* err handled below */
521 * Failed to allocate maxdata_list or range_table_list
522 * for a subdevice that needed it.
525 for (i = 0; i <= 4; i++) {
526 s = &dev->subdevices[i];
527 kfree(s->maxdata_list);
528 s->maxdata_list = NULL;
529 kfree(s->range_table_list);
530 s->range_table_list = NULL;
542 filp_close(devpriv->tty, NULL);
550 static int serial2002_open(struct comedi_device *dev)
552 struct serial2002_private *devpriv = dev->private;
556 sprintf(port, "/dev/ttyS%d", devpriv->port);
557 devpriv->tty = filp_open(port, O_RDWR, 0);
558 if (IS_ERR(devpriv->tty)) {
559 result = (int)PTR_ERR(devpriv->tty);
560 dev_err(dev->class_dev, "file open error = %d\n", result);
562 result = serial2002_setup_subdevs(dev);
567 static void serial2002_close(struct comedi_device *dev)
569 struct serial2002_private *devpriv = dev->private;
571 if (!IS_ERR(devpriv->tty) && devpriv->tty)
572 filp_close(devpriv->tty, NULL);
575 static int serial2002_di_insn_read(struct comedi_device *dev,
576 struct comedi_subdevice *s,
577 struct comedi_insn *insn,
580 struct serial2002_private *devpriv = dev->private;
584 chan = devpriv->digital_in_mapping[CR_CHAN(insn->chanspec)];
585 for (n = 0; n < insn->n; n++) {
586 struct serial_data read;
588 serial2002_poll_digital(devpriv->tty, chan);
590 read = serial2002_read(devpriv->tty, 1000);
591 if (read.kind != is_digital || read.index == chan)
594 data[n] = read.value;
599 static int serial2002_do_insn_write(struct comedi_device *dev,
600 struct comedi_subdevice *s,
601 struct comedi_insn *insn,
604 struct serial2002_private *devpriv = dev->private;
608 chan = devpriv->digital_out_mapping[CR_CHAN(insn->chanspec)];
609 for (n = 0; n < insn->n; n++) {
610 struct serial_data write;
612 write.kind = is_digital;
614 write.value = data[n];
615 serial2002_write(devpriv->tty, write);
620 static int serial2002_ai_insn_read(struct comedi_device *dev,
621 struct comedi_subdevice *s,
622 struct comedi_insn *insn,
625 struct serial2002_private *devpriv = dev->private;
629 chan = devpriv->analog_in_mapping[CR_CHAN(insn->chanspec)];
630 for (n = 0; n < insn->n; n++) {
631 struct serial_data read;
633 serial2002_poll_channel(devpriv->tty, chan);
635 read = serial2002_read(devpriv->tty, 1000);
636 if (read.kind != is_channel || read.index == chan)
639 data[n] = read.value;
644 static int serial2002_ao_insn_write(struct comedi_device *dev,
645 struct comedi_subdevice *s,
646 struct comedi_insn *insn,
649 struct serial2002_private *devpriv = dev->private;
653 chan = devpriv->analog_out_mapping[CR_CHAN(insn->chanspec)];
654 for (n = 0; n < insn->n; n++) {
655 struct serial_data write;
657 write.kind = is_channel;
659 write.value = data[n];
660 serial2002_write(devpriv->tty, write);
661 devpriv->ao_readback[chan] = data[n];
666 static int serial2002_ao_insn_read(struct comedi_device *dev,
667 struct comedi_subdevice *s,
668 struct comedi_insn *insn,
671 struct serial2002_private *devpriv = dev->private;
673 int chan = CR_CHAN(insn->chanspec);
675 for (n = 0; n < insn->n; n++)
676 data[n] = devpriv->ao_readback[chan];
681 static int serial2002_encoder_insn_read(struct comedi_device *dev,
682 struct comedi_subdevice *s,
683 struct comedi_insn *insn,
686 struct serial2002_private *devpriv = dev->private;
690 chan = devpriv->encoder_in_mapping[CR_CHAN(insn->chanspec)];
691 for (n = 0; n < insn->n; n++) {
692 struct serial_data read;
694 serial2002_poll_channel(devpriv->tty, chan);
696 read = serial2002_read(devpriv->tty, 1000);
697 if (read.kind != is_channel || read.index == chan)
700 data[n] = read.value;
705 static int serial2002_attach(struct comedi_device *dev,
706 struct comedi_devconfig *it)
708 struct serial2002_private *devpriv;
709 struct comedi_subdevice *s;
712 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
716 devpriv->port = it->options[0];
717 devpriv->speed = it->options[1];
719 ret = comedi_alloc_subdevices(dev, 5);
723 /* digital input subdevice */
724 s = &dev->subdevices[0];
725 s->type = COMEDI_SUBD_DI;
726 s->subdev_flags = SDF_READABLE;
729 s->range_table = &range_digital;
730 s->insn_read = serial2002_di_insn_read;
732 /* digital output subdevice */
733 s = &dev->subdevices[1];
734 s->type = COMEDI_SUBD_DO;
735 s->subdev_flags = SDF_WRITABLE;
738 s->range_table = &range_digital;
739 s->insn_write = serial2002_do_insn_write;
741 /* analog input subdevice */
742 s = &dev->subdevices[2];
743 s->type = COMEDI_SUBD_AI;
744 s->subdev_flags = SDF_READABLE | SDF_GROUND;
747 s->range_table = NULL;
748 s->insn_read = serial2002_ai_insn_read;
750 /* analog output subdevice */
751 s = &dev->subdevices[3];
752 s->type = COMEDI_SUBD_AO;
753 s->subdev_flags = SDF_WRITABLE;
756 s->range_table = NULL;
757 s->insn_write = serial2002_ao_insn_write;
758 s->insn_read = serial2002_ao_insn_read;
760 /* encoder input subdevice */
761 s = &dev->subdevices[4];
762 s->type = COMEDI_SUBD_COUNTER;
763 s->subdev_flags = SDF_READABLE | SDF_LSAMPL;
766 s->range_table = NULL;
767 s->insn_read = serial2002_encoder_insn_read;
769 dev->open = serial2002_open;
770 dev->close = serial2002_close;
775 static void serial2002_detach(struct comedi_device *dev)
777 struct comedi_subdevice *s;
780 for (i = 0; i < dev->n_subdevices; i++) {
781 s = &dev->subdevices[i];
782 kfree(s->maxdata_list);
783 kfree(s->range_table_list);
787 static struct comedi_driver serial2002_driver = {
788 .driver_name = "serial2002",
789 .module = THIS_MODULE,
790 .attach = serial2002_attach,
791 .detach = serial2002_detach,
793 module_comedi_driver(serial2002_driver);
795 MODULE_AUTHOR("Comedi http://www.comedi.org");
796 MODULE_DESCRIPTION("Comedi low-level driver");
797 MODULE_LICENSE("GPL");