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.
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
29 #include <linux/module.h>
30 #include "../comedidev.h"
32 #include <linux/delay.h>
33 #include <linux/sched.h>
34 #include <linux/slab.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 {
45 struct comedi_krange range;
48 struct serial2002_private {
50 int port; /* /dev/ttyS<port> */
51 int speed; /* baudrate */
53 unsigned int ao_readback[32];
54 unsigned char digital_in_mapping[32];
55 unsigned char digital_out_mapping[32];
56 unsigned char analog_in_mapping[32];
57 unsigned char analog_out_mapping[32];
58 unsigned char encoder_in_mapping[32];
59 struct serial2002_range_table_t in_range[32], out_range[32];
63 enum { is_invalid, is_digital, is_channel } kind;
69 * The configuration serial_data.value read from the device is
70 * a bitmask that defines specific options of a channel:
72 * 4:0 - the channel to configure
73 * 7:5 - the kind of channel
74 * 9:8 - the command used to configure the channel
76 * The remaining bits vary in use depending on the command:
78 * BITS 15:10 - the channel bits (maxdata)
79 * MIN/MAX 12:10 - the units multiplier for the scale
80 * 13 - the sign of the scale
81 * 33:14 - the base value for the range
83 #define S2002_CFG_CHAN(x) ((x) & 0x1f)
84 #define S2002_CFG_KIND(x) (((x) >> 5) & 0x7)
85 #define S2002_CFG_KIND_INVALID 0
86 #define S2002_CFG_KIND_DIGITAL_IN 1
87 #define S2002_CFG_KIND_DIGITAL_OUT 2
88 #define S2002_CFG_KIND_ANALOG_IN 3
89 #define S2002_CFG_KIND_ANALOG_OUT 4
90 #define S2002_CFG_KIND_ENCODER_IN 5
91 #define S2002_CFG_CMD(x) (((x) >> 8) & 0x3)
92 #define S2002_CFG_CMD_BITS 0
93 #define S2002_CFG_CMD_MIN 1
94 #define S2002_CFG_CMD_MAX 2
95 #define S2002_CFG_BITS(x) (((x) >> 10) & 0x3f)
96 #define S2002_CFG_UNITS(x) (((x) >> 10) & 0x7)
97 #define S2002_CFG_SIGN(x) (((x) >> 13) & 0x1)
98 #define S2002_CFG_BASE(x) (((x) >> 14) & 0xfffff)
100 static long serial2002_tty_ioctl(struct file *f, unsigned op,
103 if (f->f_op->unlocked_ioctl)
104 return f->f_op->unlocked_ioctl(f, op, param);
109 static int serial2002_tty_write(struct file *f, unsigned char *buf, int count)
111 const char __user *p = (__force const char __user *)buf;
118 result = f->f_op->write(f, p, count, &f->f_pos);
123 static int serial2002_tty_readb(struct file *f, unsigned char *buf)
125 char __user *p = (__force char __user *)buf;
128 return f->f_op->read(f, p, 1, &f->f_pos);
131 static void serial2002_tty_read_poll_wait(struct file *f, int timeout)
133 struct poll_wqueues table;
134 struct timeval start, now;
136 do_gettimeofday(&start);
137 poll_initwait(&table);
142 mask = f->f_op->poll(f, &table.pt);
143 if (mask & (POLLRDNORM | POLLRDBAND | POLLIN |
144 POLLHUP | POLLERR)) {
147 do_gettimeofday(&now);
148 elapsed = (1000000 * (now.tv_sec - start.tv_sec) +
149 now.tv_usec - start.tv_usec);
150 if (elapsed > timeout)
152 set_current_state(TASK_INTERRUPTIBLE);
153 schedule_timeout(((timeout - elapsed) * HZ) / 10000);
155 poll_freewait(&table);
158 static int serial2002_tty_read(struct file *f, int timeout)
170 serial2002_tty_read_poll_wait(f, timeout);
172 if (serial2002_tty_readb(f, &ch) == 1)
175 /* Device does not support poll, busy wait */
179 if (retries >= timeout)
182 if (serial2002_tty_readb(f, &ch) == 1) {
194 static void serial2002_tty_setspeed(struct file *f, int speed)
196 struct termios termios;
197 struct serial_struct serial;
204 serial2002_tty_ioctl(f, TCGETS, (unsigned long)&termios);
208 termios.c_cflag = CLOCAL | CS8 | CREAD;
209 termios.c_cc[VMIN] = 0;
210 termios.c_cc[VTIME] = 0;
213 termios.c_cflag |= B2400;
216 termios.c_cflag |= B4800;
219 termios.c_cflag |= B9600;
222 termios.c_cflag |= B19200;
225 termios.c_cflag |= B38400;
228 termios.c_cflag |= B57600;
231 termios.c_cflag |= B115200;
234 termios.c_cflag |= B9600;
237 serial2002_tty_ioctl(f, TCSETS, (unsigned long)&termios);
239 /* Set low latency */
240 serial2002_tty_ioctl(f, TIOCGSERIAL, (unsigned long)&serial);
241 serial.flags |= ASYNC_LOW_LATENCY;
242 serial2002_tty_ioctl(f, TIOCSSERIAL, (unsigned long)&serial);
247 static void serial2002_poll_digital(struct file *f, int channel)
251 cmd = 0x40 | (channel & 0x1f);
252 serial2002_tty_write(f, &cmd, 1);
255 static void serial2002_poll_channel(struct file *f, int channel)
259 cmd = 0x60 | (channel & 0x1f);
260 serial2002_tty_write(f, &cmd, 1);
263 static struct serial_data serial2002_read(struct file *f, int timeout)
265 struct serial_data result;
268 result.kind = is_invalid;
273 int data = serial2002_tty_read(f, timeout);
278 } else if (data & 0x80) {
279 result.value = (result.value << 7) | (data & 0x7f);
282 switch ((data >> 5) & 0x03) {
285 result.kind = is_digital;
289 result.kind = is_digital;
294 (result.value << 2) | ((data & 0x60) >> 5);
295 result.kind = is_channel;
297 result.index = data & 0x1f;
305 static void serial2002_write(struct file *f, struct serial_data data)
307 if (data.kind == is_digital) {
309 ((data.value << 5) & 0x20) | (data.index & 0x1f);
310 serial2002_tty_write(f, &ch, 1);
314 if (data.value >= (1L << 30)) {
315 ch[i] = 0x80 | ((data.value >> 30) & 0x03);
318 if (data.value >= (1L << 23)) {
319 ch[i] = 0x80 | ((data.value >> 23) & 0x7f);
322 if (data.value >= (1L << 16)) {
323 ch[i] = 0x80 | ((data.value >> 16) & 0x7f);
326 if (data.value >= (1L << 9)) {
327 ch[i] = 0x80 | ((data.value >> 9) & 0x7f);
330 ch[i] = 0x80 | ((data.value >> 2) & 0x7f);
332 ch[i] = ((data.value << 5) & 0x60) | (data.index & 0x1f);
334 serial2002_tty_write(f, ch, i);
345 static int serial2002_setup_subdevice(struct comedi_subdevice *s,
346 struct config_t *cfg,
347 struct serial2002_range_table_t *range,
348 unsigned char *mapping,
351 const struct comedi_lrange **range_table_list = NULL;
352 unsigned int *maxdata_list;
355 for (chan = 0, j = 0; j < 32; j++) {
356 if (cfg[j].kind == kind)
361 kfree(s->maxdata_list);
362 maxdata_list = kmalloc(sizeof(unsigned int) * s->n_chan, GFP_KERNEL);
365 s->maxdata_list = maxdata_list;
366 kfree(s->range_table_list);
367 s->range_table = NULL;
368 s->range_table_list = NULL;
369 if (kind == 1 || kind == 2) {
370 s->range_table = &range_digital;
373 kmalloc(sizeof(struct serial2002_range_table_t) *
374 s->n_chan, GFP_KERNEL);
375 if (!range_table_list)
377 s->range_table_list = range_table_list;
379 for (chan = 0, j = 0; j < 32; j++) {
380 if (cfg[j].kind == kind) {
385 range[j].range.min = cfg[j].min;
386 range[j].range.max = cfg[j].max;
387 range_table_list[chan] =
388 (const struct comedi_lrange *)&range[j];
390 maxdata_list[chan] = ((long long)1 << cfg[j].bits) - 1;
397 static int serial2002_setup_subdevs(struct comedi_device *dev)
399 struct serial2002_private *devpriv = dev->private;
400 struct config_t *di_cfg;
401 struct config_t *do_cfg;
402 struct config_t *ai_cfg;
403 struct config_t *ao_cfg;
404 struct config_t *cfg;
405 struct comedi_subdevice *s;
409 /* Allocate the temporary structs to hold the configuration data */
410 di_cfg = kcalloc(32, sizeof(*cfg), GFP_KERNEL);
411 do_cfg = kcalloc(32, sizeof(*cfg), GFP_KERNEL);
412 ai_cfg = kcalloc(32, sizeof(*cfg), GFP_KERNEL);
413 ao_cfg = kcalloc(32, sizeof(*cfg), GFP_KERNEL);
414 if (!di_cfg || !do_cfg || !ai_cfg || !ao_cfg) {
416 goto err_alloc_configs;
419 /* Read the configuration from the connected device */
420 serial2002_tty_setspeed(devpriv->tty, devpriv->speed);
421 serial2002_poll_channel(devpriv->tty, 31);
423 struct serial_data data;
425 data = serial2002_read(devpriv->tty, 1000);
426 if (data.kind != is_channel || data.index != 31 ||
427 S2002_CFG_KIND(data.value) == S2002_CFG_KIND_INVALID) {
430 int channel = S2002_CFG_CHAN(data.value);
431 int range = S2002_CFG_BASE(data.value);
433 switch (S2002_CFG_KIND(data.value)) {
434 case S2002_CFG_KIND_DIGITAL_IN:
437 case S2002_CFG_KIND_DIGITAL_OUT:
440 case S2002_CFG_KIND_ANALOG_IN:
443 case S2002_CFG_KIND_ANALOG_OUT:
446 case S2002_CFG_KIND_ENCODER_IN:
454 continue; /* unknown kind, skip it */
456 cfg[channel].kind = S2002_CFG_KIND(data.value);
458 switch (S2002_CFG_CMD(data.value)) {
459 case S2002_CFG_CMD_BITS:
460 cfg[channel].bits = S2002_CFG_BITS(data.value);
462 case S2002_CFG_CMD_MIN:
463 case S2002_CFG_CMD_MAX:
464 switch (S2002_CFG_UNITS(data.value)) {
475 if (S2002_CFG_SIGN(data.value))
477 if (S2002_CFG_CMD(data.value) ==
479 cfg[channel].min = range;
481 cfg[channel].max = range;
487 /* Fill in subdevice data */
488 for (i = 0; i <= 4; i++) {
489 unsigned char *mapping = NULL;
490 struct serial2002_range_table_t *range = NULL;
493 s = &dev->subdevices[i];
498 mapping = devpriv->digital_in_mapping;
499 kind = S2002_CFG_KIND_DIGITAL_IN;
503 mapping = devpriv->digital_out_mapping;
504 kind = S2002_CFG_KIND_DIGITAL_OUT;
508 mapping = devpriv->analog_in_mapping;
509 range = devpriv->in_range;
510 kind = S2002_CFG_KIND_ANALOG_IN;
514 mapping = devpriv->analog_out_mapping;
515 range = devpriv->out_range;
516 kind = S2002_CFG_KIND_ANALOG_OUT;
520 mapping = devpriv->encoder_in_mapping;
521 range = devpriv->in_range;
522 kind = S2002_CFG_KIND_ENCODER_IN;
526 if (serial2002_setup_subdevice(s, cfg, range, mapping, kind))
527 break; /* err handled below */
531 * Failed to allocate maxdata_list or range_table_list
532 * for a subdevice that needed it.
535 for (i = 0; i <= 4; i++) {
536 s = &dev->subdevices[i];
537 kfree(s->maxdata_list);
538 s->maxdata_list = NULL;
539 kfree(s->range_table_list);
540 s->range_table_list = NULL;
552 filp_close(devpriv->tty, NULL);
560 static int serial2002_open(struct comedi_device *dev)
562 struct serial2002_private *devpriv = dev->private;
566 sprintf(port, "/dev/ttyS%d", devpriv->port);
567 devpriv->tty = filp_open(port, O_RDWR, 0);
568 if (IS_ERR(devpriv->tty)) {
569 result = (int)PTR_ERR(devpriv->tty);
570 dev_err(dev->class_dev, "file open error = %d\n", result);
572 result = serial2002_setup_subdevs(dev);
577 static void serial2002_close(struct comedi_device *dev)
579 struct serial2002_private *devpriv = dev->private;
581 if (!IS_ERR(devpriv->tty) && devpriv->tty)
582 filp_close(devpriv->tty, NULL);
585 static int serial2002_di_insn_read(struct comedi_device *dev,
586 struct comedi_subdevice *s,
587 struct comedi_insn *insn,
590 struct serial2002_private *devpriv = dev->private;
594 chan = devpriv->digital_in_mapping[CR_CHAN(insn->chanspec)];
595 for (n = 0; n < insn->n; n++) {
596 struct serial_data read;
598 serial2002_poll_digital(devpriv->tty, chan);
600 read = serial2002_read(devpriv->tty, 1000);
601 if (read.kind != is_digital || read.index == chan)
604 data[n] = read.value;
609 static int serial2002_do_insn_write(struct comedi_device *dev,
610 struct comedi_subdevice *s,
611 struct comedi_insn *insn,
614 struct serial2002_private *devpriv = dev->private;
618 chan = devpriv->digital_out_mapping[CR_CHAN(insn->chanspec)];
619 for (n = 0; n < insn->n; n++) {
620 struct serial_data write;
622 write.kind = is_digital;
624 write.value = data[n];
625 serial2002_write(devpriv->tty, write);
630 static int serial2002_ai_insn_read(struct comedi_device *dev,
631 struct comedi_subdevice *s,
632 struct comedi_insn *insn,
635 struct serial2002_private *devpriv = dev->private;
639 chan = devpriv->analog_in_mapping[CR_CHAN(insn->chanspec)];
640 for (n = 0; n < insn->n; n++) {
641 struct serial_data read;
643 serial2002_poll_channel(devpriv->tty, chan);
645 read = serial2002_read(devpriv->tty, 1000);
646 if (read.kind != is_channel || read.index == chan)
649 data[n] = read.value;
654 static int serial2002_ao_insn_write(struct comedi_device *dev,
655 struct comedi_subdevice *s,
656 struct comedi_insn *insn,
659 struct serial2002_private *devpriv = dev->private;
663 chan = devpriv->analog_out_mapping[CR_CHAN(insn->chanspec)];
664 for (n = 0; n < insn->n; n++) {
665 struct serial_data write;
667 write.kind = is_channel;
669 write.value = data[n];
670 serial2002_write(devpriv->tty, write);
671 devpriv->ao_readback[chan] = data[n];
676 static int serial2002_ao_insn_read(struct comedi_device *dev,
677 struct comedi_subdevice *s,
678 struct comedi_insn *insn,
681 struct serial2002_private *devpriv = dev->private;
683 int chan = CR_CHAN(insn->chanspec);
685 for (n = 0; n < insn->n; n++)
686 data[n] = devpriv->ao_readback[chan];
691 static int serial2002_encoder_insn_read(struct comedi_device *dev,
692 struct comedi_subdevice *s,
693 struct comedi_insn *insn,
696 struct serial2002_private *devpriv = dev->private;
700 chan = devpriv->encoder_in_mapping[CR_CHAN(insn->chanspec)];
701 for (n = 0; n < insn->n; n++) {
702 struct serial_data read;
704 serial2002_poll_channel(devpriv->tty, chan);
706 read = serial2002_read(devpriv->tty, 1000);
707 if (read.kind != is_channel || read.index == chan)
710 data[n] = read.value;
715 static int serial2002_attach(struct comedi_device *dev,
716 struct comedi_devconfig *it)
718 struct serial2002_private *devpriv;
719 struct comedi_subdevice *s;
722 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
726 devpriv->port = it->options[0];
727 devpriv->speed = it->options[1];
729 ret = comedi_alloc_subdevices(dev, 5);
733 /* digital input subdevice */
734 s = &dev->subdevices[0];
735 s->type = COMEDI_SUBD_DI;
736 s->subdev_flags = SDF_READABLE;
739 s->range_table = &range_digital;
740 s->insn_read = serial2002_di_insn_read;
742 /* digital output subdevice */
743 s = &dev->subdevices[1];
744 s->type = COMEDI_SUBD_DO;
745 s->subdev_flags = SDF_WRITEABLE;
748 s->range_table = &range_digital;
749 s->insn_write = serial2002_do_insn_write;
751 /* analog input subdevice */
752 s = &dev->subdevices[2];
753 s->type = COMEDI_SUBD_AI;
754 s->subdev_flags = SDF_READABLE | SDF_GROUND;
757 s->range_table = NULL;
758 s->insn_read = serial2002_ai_insn_read;
760 /* analog output subdevice */
761 s = &dev->subdevices[3];
762 s->type = COMEDI_SUBD_AO;
763 s->subdev_flags = SDF_WRITEABLE;
766 s->range_table = NULL;
767 s->insn_write = serial2002_ao_insn_write;
768 s->insn_read = serial2002_ao_insn_read;
770 /* encoder input subdevice */
771 s = &dev->subdevices[4];
772 s->type = COMEDI_SUBD_COUNTER;
773 s->subdev_flags = SDF_READABLE | SDF_LSAMPL;
776 s->range_table = NULL;
777 s->insn_read = serial2002_encoder_insn_read;
779 dev->open = serial2002_open;
780 dev->close = serial2002_close;
785 static void serial2002_detach(struct comedi_device *dev)
787 struct comedi_subdevice *s;
790 for (i = 0; i < dev->n_subdevices; i++) {
791 s = &dev->subdevices[i];
792 kfree(s->maxdata_list);
793 kfree(s->range_table_list);
797 static struct comedi_driver serial2002_driver = {
798 .driver_name = "serial2002",
799 .module = THIS_MODULE,
800 .attach = serial2002_attach,
801 .detach = serial2002_detach,
803 module_comedi_driver(serial2002_driver);
805 MODULE_AUTHOR("Comedi http://www.comedi.org");
806 MODULE_DESCRIPTION("Comedi low-level driver");
807 MODULE_LICENSE("GPL");