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)
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 if (cfg[j].bits < 32)
383 maxdata_list[chan] = (1u << cfg[j].bits) - 1;
385 maxdata_list[chan] = 0xffffffff;
392 static int serial2002_setup_subdevs(struct comedi_device *dev)
394 struct serial2002_private *devpriv = dev->private;
395 struct config_t *di_cfg;
396 struct config_t *do_cfg;
397 struct config_t *ai_cfg;
398 struct config_t *ao_cfg;
399 struct config_t *cfg;
400 struct comedi_subdevice *s;
404 /* Allocate the temporary structs to hold the configuration data */
405 di_cfg = kcalloc(32, sizeof(*cfg), GFP_KERNEL);
406 do_cfg = kcalloc(32, sizeof(*cfg), GFP_KERNEL);
407 ai_cfg = kcalloc(32, sizeof(*cfg), GFP_KERNEL);
408 ao_cfg = kcalloc(32, sizeof(*cfg), GFP_KERNEL);
409 if (!di_cfg || !do_cfg || !ai_cfg || !ao_cfg) {
411 goto err_alloc_configs;
414 /* Read the configuration from the connected device */
415 serial2002_tty_setspeed(devpriv->tty, devpriv->speed);
416 serial2002_poll_channel(devpriv->tty, 31);
418 struct serial_data data = serial2002_read(devpriv->tty, 1000);
419 int kind = S2002_CFG_KIND(data.value);
420 int channel = S2002_CFG_CHAN(data.value);
421 int range = S2002_CFG_BASE(data.value);
422 int cmd = S2002_CFG_CMD(data.value);
424 if (data.kind != is_channel || data.index != 31 ||
425 kind == S2002_CFG_KIND_INVALID)
429 case S2002_CFG_KIND_DIGITAL_IN:
432 case S2002_CFG_KIND_DIGITAL_OUT:
435 case S2002_CFG_KIND_ANALOG_IN:
438 case S2002_CFG_KIND_ANALOG_OUT:
441 case S2002_CFG_KIND_ENCODER_IN:
449 continue; /* unknown kind, skip it */
451 cfg[channel].kind = kind;
454 case S2002_CFG_CMD_BITS:
455 cfg[channel].bits = S2002_CFG_BITS(data.value);
457 case S2002_CFG_CMD_MIN:
458 case S2002_CFG_CMD_MAX:
459 switch (S2002_CFG_UNITS(data.value)) {
470 if (S2002_CFG_SIGN(data.value))
472 if (cmd == S2002_CFG_CMD_MIN)
473 cfg[channel].min = range;
475 cfg[channel].max = range;
480 /* Fill in subdevice data */
481 for (i = 0; i <= 4; i++) {
482 unsigned char *mapping = NULL;
483 struct serial2002_range_table_t *range = NULL;
486 s = &dev->subdevices[i];
491 mapping = devpriv->digital_in_mapping;
492 kind = S2002_CFG_KIND_DIGITAL_IN;
496 mapping = devpriv->digital_out_mapping;
497 kind = S2002_CFG_KIND_DIGITAL_OUT;
501 mapping = devpriv->analog_in_mapping;
502 range = devpriv->in_range;
503 kind = S2002_CFG_KIND_ANALOG_IN;
507 mapping = devpriv->analog_out_mapping;
508 range = devpriv->out_range;
509 kind = S2002_CFG_KIND_ANALOG_OUT;
513 mapping = devpriv->encoder_in_mapping;
514 range = devpriv->in_range;
515 kind = S2002_CFG_KIND_ENCODER_IN;
519 if (serial2002_setup_subdevice(s, cfg, range, mapping, kind))
520 break; /* err handled below */
524 * Failed to allocate maxdata_list or range_table_list
525 * for a subdevice that needed it.
528 for (i = 0; i <= 4; i++) {
529 s = &dev->subdevices[i];
530 kfree(s->maxdata_list);
531 s->maxdata_list = NULL;
532 kfree(s->range_table_list);
533 s->range_table_list = NULL;
545 filp_close(devpriv->tty, NULL);
553 static int serial2002_open(struct comedi_device *dev)
555 struct serial2002_private *devpriv = dev->private;
559 sprintf(port, "/dev/ttyS%d", devpriv->port);
560 devpriv->tty = filp_open(port, O_RDWR, 0);
561 if (IS_ERR(devpriv->tty)) {
562 result = (int)PTR_ERR(devpriv->tty);
563 dev_err(dev->class_dev, "file open error = %d\n", result);
565 result = serial2002_setup_subdevs(dev);
570 static void serial2002_close(struct comedi_device *dev)
572 struct serial2002_private *devpriv = dev->private;
574 if (!IS_ERR(devpriv->tty) && devpriv->tty)
575 filp_close(devpriv->tty, NULL);
578 static int serial2002_di_insn_read(struct comedi_device *dev,
579 struct comedi_subdevice *s,
580 struct comedi_insn *insn,
583 struct serial2002_private *devpriv = dev->private;
587 chan = devpriv->digital_in_mapping[CR_CHAN(insn->chanspec)];
588 for (n = 0; n < insn->n; n++) {
589 struct serial_data read;
591 serial2002_poll_digital(devpriv->tty, chan);
593 read = serial2002_read(devpriv->tty, 1000);
594 if (read.kind != is_digital || read.index == chan)
597 data[n] = read.value;
602 static int serial2002_do_insn_write(struct comedi_device *dev,
603 struct comedi_subdevice *s,
604 struct comedi_insn *insn,
607 struct serial2002_private *devpriv = dev->private;
611 chan = devpriv->digital_out_mapping[CR_CHAN(insn->chanspec)];
612 for (n = 0; n < insn->n; n++) {
613 struct serial_data write;
615 write.kind = is_digital;
617 write.value = data[n];
618 serial2002_write(devpriv->tty, write);
623 static int serial2002_ai_insn_read(struct comedi_device *dev,
624 struct comedi_subdevice *s,
625 struct comedi_insn *insn,
628 struct serial2002_private *devpriv = dev->private;
632 chan = devpriv->analog_in_mapping[CR_CHAN(insn->chanspec)];
633 for (n = 0; n < insn->n; n++) {
634 struct serial_data read;
636 serial2002_poll_channel(devpriv->tty, chan);
638 read = serial2002_read(devpriv->tty, 1000);
639 if (read.kind != is_channel || read.index == chan)
642 data[n] = read.value;
647 static int serial2002_ao_insn_write(struct comedi_device *dev,
648 struct comedi_subdevice *s,
649 struct comedi_insn *insn,
652 struct serial2002_private *devpriv = dev->private;
656 chan = devpriv->analog_out_mapping[CR_CHAN(insn->chanspec)];
657 for (n = 0; n < insn->n; n++) {
658 struct serial_data write;
660 write.kind = is_channel;
662 write.value = data[n];
663 serial2002_write(devpriv->tty, write);
664 devpriv->ao_readback[chan] = data[n];
669 static int serial2002_ao_insn_read(struct comedi_device *dev,
670 struct comedi_subdevice *s,
671 struct comedi_insn *insn,
674 struct serial2002_private *devpriv = dev->private;
676 int chan = CR_CHAN(insn->chanspec);
678 for (n = 0; n < insn->n; n++)
679 data[n] = devpriv->ao_readback[chan];
684 static int serial2002_encoder_insn_read(struct comedi_device *dev,
685 struct comedi_subdevice *s,
686 struct comedi_insn *insn,
689 struct serial2002_private *devpriv = dev->private;
693 chan = devpriv->encoder_in_mapping[CR_CHAN(insn->chanspec)];
694 for (n = 0; n < insn->n; n++) {
695 struct serial_data read;
697 serial2002_poll_channel(devpriv->tty, chan);
699 read = serial2002_read(devpriv->tty, 1000);
700 if (read.kind != is_channel || read.index == chan)
703 data[n] = read.value;
708 static int serial2002_attach(struct comedi_device *dev,
709 struct comedi_devconfig *it)
711 struct serial2002_private *devpriv;
712 struct comedi_subdevice *s;
715 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
719 devpriv->port = it->options[0];
720 devpriv->speed = it->options[1];
722 ret = comedi_alloc_subdevices(dev, 5);
726 /* digital input subdevice */
727 s = &dev->subdevices[0];
728 s->type = COMEDI_SUBD_DI;
729 s->subdev_flags = SDF_READABLE;
732 s->range_table = &range_digital;
733 s->insn_read = serial2002_di_insn_read;
735 /* digital output subdevice */
736 s = &dev->subdevices[1];
737 s->type = COMEDI_SUBD_DO;
738 s->subdev_flags = SDF_WRITABLE;
741 s->range_table = &range_digital;
742 s->insn_write = serial2002_do_insn_write;
744 /* analog input subdevice */
745 s = &dev->subdevices[2];
746 s->type = COMEDI_SUBD_AI;
747 s->subdev_flags = SDF_READABLE | SDF_GROUND;
750 s->range_table = NULL;
751 s->insn_read = serial2002_ai_insn_read;
753 /* analog output subdevice */
754 s = &dev->subdevices[3];
755 s->type = COMEDI_SUBD_AO;
756 s->subdev_flags = SDF_WRITABLE;
759 s->range_table = NULL;
760 s->insn_write = serial2002_ao_insn_write;
761 s->insn_read = serial2002_ao_insn_read;
763 /* encoder input subdevice */
764 s = &dev->subdevices[4];
765 s->type = COMEDI_SUBD_COUNTER;
766 s->subdev_flags = SDF_READABLE | SDF_LSAMPL;
769 s->range_table = NULL;
770 s->insn_read = serial2002_encoder_insn_read;
772 dev->open = serial2002_open;
773 dev->close = serial2002_close;
778 static void serial2002_detach(struct comedi_device *dev)
780 struct comedi_subdevice *s;
783 for (i = 0; i < dev->n_subdevices; i++) {
784 s = &dev->subdevices[i];
785 kfree(s->maxdata_list);
786 kfree(s->range_table_list);
790 static struct comedi_driver serial2002_driver = {
791 .driver_name = "serial2002",
792 .module = THIS_MODULE,
793 .attach = serial2002_attach,
794 .detach = serial2002_detach,
796 module_comedi_driver(serial2002_driver);
798 MODULE_AUTHOR("Comedi http://www.comedi.org");
799 MODULE_DESCRIPTION("Comedi low-level driver");
800 MODULE_LICENSE("GPL");