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 int 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)
110 return kernel_write(f, buf, count, &pos);
113 static void serial2002_tty_read_poll_wait(struct file *f, int timeout)
115 struct poll_wqueues table;
119 poll_initwait(&table);
124 mask = f->f_op->poll(f, &table.pt);
125 if (mask & (POLLRDNORM | POLLRDBAND | POLLIN |
126 POLLHUP | POLLERR)) {
130 elapsed = ktime_us_delta(now, start);
131 if (elapsed > timeout)
133 set_current_state(TASK_INTERRUPTIBLE);
134 schedule_timeout(((timeout - elapsed) * HZ) / 10000);
136 poll_freewait(&table);
139 static int serial2002_tty_read(struct file *f, int timeout)
148 serial2002_tty_read_poll_wait(f, timeout);
150 if (kernel_read(f, &ch, 1, &pos) == 1)
153 /* Device does not support poll, busy wait */
158 if (retries >= timeout)
161 if (kernel_read(f, &ch, 1, &pos) == 1) {
165 usleep_range(100, 1000);
172 static void serial2002_tty_setspeed(struct file *f, int speed)
174 struct termios termios;
175 struct serial_struct serial;
182 serial2002_tty_ioctl(f, TCGETS, (unsigned long)&termios);
186 termios.c_cflag = CLOCAL | CS8 | CREAD;
187 termios.c_cc[VMIN] = 0;
188 termios.c_cc[VTIME] = 0;
191 termios.c_cflag |= B2400;
194 termios.c_cflag |= B4800;
197 termios.c_cflag |= B9600;
200 termios.c_cflag |= B19200;
203 termios.c_cflag |= B38400;
206 termios.c_cflag |= B57600;
209 termios.c_cflag |= B115200;
212 termios.c_cflag |= B9600;
215 serial2002_tty_ioctl(f, TCSETS, (unsigned long)&termios);
217 /* Set low latency */
218 serial2002_tty_ioctl(f, TIOCGSERIAL, (unsigned long)&serial);
219 serial.flags |= ASYNC_LOW_LATENCY;
220 serial2002_tty_ioctl(f, TIOCSSERIAL, (unsigned long)&serial);
225 static void serial2002_poll_digital(struct file *f, int channel)
229 cmd = 0x40 | (channel & 0x1f);
230 serial2002_tty_write(f, &cmd, 1);
233 static void serial2002_poll_channel(struct file *f, int channel)
237 cmd = 0x60 | (channel & 0x1f);
238 serial2002_tty_write(f, &cmd, 1);
241 static struct serial_data serial2002_read(struct file *f, int timeout)
243 struct serial_data result;
246 result.kind = is_invalid;
251 int data = serial2002_tty_read(f, timeout);
256 } else if (data & 0x80) {
257 result.value = (result.value << 7) | (data & 0x7f);
260 switch ((data >> 5) & 0x03) {
263 result.kind = is_digital;
267 result.kind = is_digital;
272 (result.value << 2) | ((data & 0x60) >> 5);
273 result.kind = is_channel;
275 result.index = data & 0x1f;
282 static void serial2002_write(struct file *f, struct serial_data data)
284 if (data.kind == is_digital) {
286 ((data.value << 5) & 0x20) | (data.index & 0x1f);
287 serial2002_tty_write(f, &ch, 1);
292 if (data.value >= (1L << 30)) {
293 ch[i] = 0x80 | ((data.value >> 30) & 0x03);
296 if (data.value >= (1L << 23)) {
297 ch[i] = 0x80 | ((data.value >> 23) & 0x7f);
300 if (data.value >= (1L << 16)) {
301 ch[i] = 0x80 | ((data.value >> 16) & 0x7f);
304 if (data.value >= (1L << 9)) {
305 ch[i] = 0x80 | ((data.value >> 9) & 0x7f);
308 ch[i] = 0x80 | ((data.value >> 2) & 0x7f);
310 ch[i] = ((data.value << 5) & 0x60) | (data.index & 0x1f);
312 serial2002_tty_write(f, ch, i);
323 static int serial2002_setup_subdevice(struct comedi_subdevice *s,
324 struct config_t *cfg,
325 struct serial2002_range_table_t *range,
326 unsigned char *mapping,
329 const struct comedi_lrange **range_table_list = NULL;
330 unsigned int *maxdata_list;
333 for (chan = 0, j = 0; j < 32; j++) {
334 if (cfg[j].kind == kind)
339 kfree(s->maxdata_list);
340 maxdata_list = kmalloc_array(s->n_chan, sizeof(unsigned int),
344 s->maxdata_list = maxdata_list;
345 kfree(s->range_table_list);
346 s->range_table = NULL;
347 s->range_table_list = NULL;
348 if (kind == 1 || kind == 2) {
349 s->range_table = &range_digital;
351 range_table_list = kmalloc_array(s->n_chan, sizeof(*range),
353 if (!range_table_list)
355 s->range_table_list = range_table_list;
357 for (chan = 0, j = 0; j < 32; j++) {
358 if (cfg[j].kind == kind) {
361 if (range && range_table_list) {
363 range[j].range.min = cfg[j].min;
364 range[j].range.max = cfg[j].max;
365 range_table_list[chan] =
366 (const struct comedi_lrange *)&range[j];
368 if (cfg[j].bits < 32)
369 maxdata_list[chan] = (1u << cfg[j].bits) - 1;
371 maxdata_list[chan] = 0xffffffff;
378 static int serial2002_setup_subdevs(struct comedi_device *dev)
380 struct serial2002_private *devpriv = dev->private;
381 struct config_t *di_cfg;
382 struct config_t *do_cfg;
383 struct config_t *ai_cfg;
384 struct config_t *ao_cfg;
385 struct config_t *cfg;
386 struct comedi_subdevice *s;
390 /* Allocate the temporary structs to hold the configuration data */
391 di_cfg = kcalloc(32, sizeof(*cfg), GFP_KERNEL);
392 do_cfg = kcalloc(32, sizeof(*cfg), GFP_KERNEL);
393 ai_cfg = kcalloc(32, sizeof(*cfg), GFP_KERNEL);
394 ao_cfg = kcalloc(32, sizeof(*cfg), GFP_KERNEL);
395 if (!di_cfg || !do_cfg || !ai_cfg || !ao_cfg) {
397 goto err_alloc_configs;
400 /* Read the configuration from the connected device */
401 serial2002_tty_setspeed(devpriv->tty, devpriv->speed);
402 serial2002_poll_channel(devpriv->tty, 31);
404 struct serial_data data = serial2002_read(devpriv->tty, 1000);
405 int kind = S2002_CFG_KIND(data.value);
406 int channel = S2002_CFG_CHAN(data.value);
407 int range = S2002_CFG_BASE(data.value);
408 int cmd = S2002_CFG_CMD(data.value);
410 if (data.kind != is_channel || data.index != 31 ||
411 kind == S2002_CFG_KIND_INVALID)
415 case S2002_CFG_KIND_DIGITAL_IN:
418 case S2002_CFG_KIND_DIGITAL_OUT:
421 case S2002_CFG_KIND_ANALOG_IN:
424 case S2002_CFG_KIND_ANALOG_OUT:
427 case S2002_CFG_KIND_ENCODER_IN:
435 continue; /* unknown kind, skip it */
437 cfg[channel].kind = kind;
440 case S2002_CFG_CMD_BITS:
441 cfg[channel].bits = S2002_CFG_BITS(data.value);
443 case S2002_CFG_CMD_MIN:
444 case S2002_CFG_CMD_MAX:
445 switch (S2002_CFG_UNITS(data.value)) {
456 if (S2002_CFG_SIGN(data.value))
458 if (cmd == S2002_CFG_CMD_MIN)
459 cfg[channel].min = range;
461 cfg[channel].max = range;
466 /* Fill in subdevice data */
467 for (i = 0; i <= 4; i++) {
468 unsigned char *mapping = NULL;
469 struct serial2002_range_table_t *range = NULL;
472 s = &dev->subdevices[i];
477 mapping = devpriv->digital_in_mapping;
478 kind = S2002_CFG_KIND_DIGITAL_IN;
482 mapping = devpriv->digital_out_mapping;
483 kind = S2002_CFG_KIND_DIGITAL_OUT;
487 mapping = devpriv->analog_in_mapping;
488 range = devpriv->in_range;
489 kind = S2002_CFG_KIND_ANALOG_IN;
493 mapping = devpriv->analog_out_mapping;
494 range = devpriv->out_range;
495 kind = S2002_CFG_KIND_ANALOG_OUT;
499 mapping = devpriv->encoder_in_mapping;
500 range = devpriv->in_range;
501 kind = S2002_CFG_KIND_ENCODER_IN;
505 if (serial2002_setup_subdevice(s, cfg, range, mapping, kind))
506 break; /* err handled below */
510 * Failed to allocate maxdata_list or range_table_list
511 * for a subdevice that needed it.
514 for (i = 0; i <= 4; i++) {
515 s = &dev->subdevices[i];
516 kfree(s->maxdata_list);
517 s->maxdata_list = NULL;
518 kfree(s->range_table_list);
519 s->range_table_list = NULL;
531 filp_close(devpriv->tty, NULL);
539 static int serial2002_open(struct comedi_device *dev)
541 struct serial2002_private *devpriv = dev->private;
545 sprintf(port, "/dev/ttyS%d", devpriv->port);
546 devpriv->tty = filp_open(port, O_RDWR, 0);
547 if (IS_ERR(devpriv->tty)) {
548 result = (int)PTR_ERR(devpriv->tty);
549 dev_err(dev->class_dev, "file open error = %d\n", result);
551 result = serial2002_setup_subdevs(dev);
556 static void serial2002_close(struct comedi_device *dev)
558 struct serial2002_private *devpriv = dev->private;
560 if (!IS_ERR(devpriv->tty) && devpriv->tty)
561 filp_close(devpriv->tty, NULL);
564 static int serial2002_di_insn_read(struct comedi_device *dev,
565 struct comedi_subdevice *s,
566 struct comedi_insn *insn,
569 struct serial2002_private *devpriv = dev->private;
573 chan = devpriv->digital_in_mapping[CR_CHAN(insn->chanspec)];
574 for (n = 0; n < insn->n; n++) {
575 struct serial_data read;
577 serial2002_poll_digital(devpriv->tty, chan);
579 read = serial2002_read(devpriv->tty, 1000);
580 if (read.kind != is_digital || read.index == chan)
583 data[n] = read.value;
588 static int serial2002_do_insn_write(struct comedi_device *dev,
589 struct comedi_subdevice *s,
590 struct comedi_insn *insn,
593 struct serial2002_private *devpriv = dev->private;
597 chan = devpriv->digital_out_mapping[CR_CHAN(insn->chanspec)];
598 for (n = 0; n < insn->n; n++) {
599 struct serial_data write;
601 write.kind = is_digital;
603 write.value = data[n];
604 serial2002_write(devpriv->tty, write);
609 static int serial2002_ai_insn_read(struct comedi_device *dev,
610 struct comedi_subdevice *s,
611 struct comedi_insn *insn,
614 struct serial2002_private *devpriv = dev->private;
618 chan = devpriv->analog_in_mapping[CR_CHAN(insn->chanspec)];
619 for (n = 0; n < insn->n; n++) {
620 struct serial_data read;
622 serial2002_poll_channel(devpriv->tty, chan);
624 read = serial2002_read(devpriv->tty, 1000);
625 if (read.kind != is_channel || read.index == chan)
628 data[n] = read.value;
633 static int serial2002_ao_insn_write(struct comedi_device *dev,
634 struct comedi_subdevice *s,
635 struct comedi_insn *insn,
638 struct serial2002_private *devpriv = dev->private;
642 chan = devpriv->analog_out_mapping[CR_CHAN(insn->chanspec)];
643 for (n = 0; n < insn->n; n++) {
644 struct serial_data write;
646 write.kind = is_channel;
648 write.value = data[n];
649 serial2002_write(devpriv->tty, write);
650 devpriv->ao_readback[chan] = data[n];
655 static int serial2002_ao_insn_read(struct comedi_device *dev,
656 struct comedi_subdevice *s,
657 struct comedi_insn *insn,
660 struct serial2002_private *devpriv = dev->private;
662 int chan = CR_CHAN(insn->chanspec);
664 for (n = 0; n < insn->n; n++)
665 data[n] = devpriv->ao_readback[chan];
670 static int serial2002_encoder_insn_read(struct comedi_device *dev,
671 struct comedi_subdevice *s,
672 struct comedi_insn *insn,
675 struct serial2002_private *devpriv = dev->private;
679 chan = devpriv->encoder_in_mapping[CR_CHAN(insn->chanspec)];
680 for (n = 0; n < insn->n; n++) {
681 struct serial_data read;
683 serial2002_poll_channel(devpriv->tty, chan);
685 read = serial2002_read(devpriv->tty, 1000);
686 if (read.kind != is_channel || read.index == chan)
689 data[n] = read.value;
694 static int serial2002_attach(struct comedi_device *dev,
695 struct comedi_devconfig *it)
697 struct serial2002_private *devpriv;
698 struct comedi_subdevice *s;
701 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
705 devpriv->port = it->options[0];
706 devpriv->speed = it->options[1];
708 ret = comedi_alloc_subdevices(dev, 5);
712 /* digital input subdevice */
713 s = &dev->subdevices[0];
714 s->type = COMEDI_SUBD_DI;
715 s->subdev_flags = SDF_READABLE;
718 s->range_table = &range_digital;
719 s->insn_read = serial2002_di_insn_read;
721 /* digital output subdevice */
722 s = &dev->subdevices[1];
723 s->type = COMEDI_SUBD_DO;
724 s->subdev_flags = SDF_WRITABLE;
727 s->range_table = &range_digital;
728 s->insn_write = serial2002_do_insn_write;
730 /* analog input subdevice */
731 s = &dev->subdevices[2];
732 s->type = COMEDI_SUBD_AI;
733 s->subdev_flags = SDF_READABLE | SDF_GROUND;
736 s->range_table = NULL;
737 s->insn_read = serial2002_ai_insn_read;
739 /* analog output subdevice */
740 s = &dev->subdevices[3];
741 s->type = COMEDI_SUBD_AO;
742 s->subdev_flags = SDF_WRITABLE;
745 s->range_table = NULL;
746 s->insn_write = serial2002_ao_insn_write;
747 s->insn_read = serial2002_ao_insn_read;
749 /* encoder input subdevice */
750 s = &dev->subdevices[4];
751 s->type = COMEDI_SUBD_COUNTER;
752 s->subdev_flags = SDF_READABLE | SDF_LSAMPL;
755 s->range_table = NULL;
756 s->insn_read = serial2002_encoder_insn_read;
758 dev->open = serial2002_open;
759 dev->close = serial2002_close;
764 static void serial2002_detach(struct comedi_device *dev)
766 struct comedi_subdevice *s;
769 for (i = 0; i < dev->n_subdevices; i++) {
770 s = &dev->subdevices[i];
771 kfree(s->maxdata_list);
772 kfree(s->range_table_list);
776 static struct comedi_driver serial2002_driver = {
777 .driver_name = "serial2002",
778 .module = THIS_MODULE,
779 .attach = serial2002_attach,
780 .detach = serial2002_detach,
782 module_comedi_driver(serial2002_driver);
784 MODULE_AUTHOR("Comedi http://www.comedi.org");
785 MODULE_DESCRIPTION("Comedi low-level driver");
786 MODULE_LICENSE("GPL");