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>
38 #include <linux/sched.h>
39 #include <linux/slab.h>
41 #include <linux/termios.h>
42 #include <asm/ioctls.h>
43 #include <linux/serial.h>
44 #include <linux/poll.h>
46 struct serial2002_board {
50 struct serial2002_range_table_t {
54 struct comedi_krange range;
57 struct serial2002_private {
59 int port; /* /dev/ttyS<port> */
60 int speed; /* baudrate */
62 unsigned int ao_readback[32];
63 unsigned char digital_in_mapping[32];
64 unsigned char digital_out_mapping[32];
65 unsigned char analog_in_mapping[32];
66 unsigned char analog_out_mapping[32];
67 unsigned char encoder_in_mapping[32];
68 struct serial2002_range_table_t in_range[32], out_range[32];
72 * most drivers define the following macro to make it easy to
73 * access the private structure.
75 #define devpriv ((struct serial2002_private *)dev->private)
78 enum { is_invalid, is_digital, is_channel } kind;
83 static long tty_ioctl(struct file *f, unsigned op, unsigned long param)
85 if (f->f_op->unlocked_ioctl)
86 return f->f_op->unlocked_ioctl(f, op, param);
91 static int tty_write(struct file *f, unsigned char *buf, int count)
99 result = f->f_op->write(f, buf, count, &f->f_pos);
106 * On 2.6.26.3 this occaisonally gave me page faults, worked around by
107 * settings.c_cc[VMIN] = 0; settings.c_cc[VTIME] = 0
109 static int tty_available(struct file *f)
116 tty_ioctl(f, FIONREAD, (unsigned long)&result);
122 static int tty_read(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);
149 (1000000 * (now.tv_sec - start.tv_sec) +
150 now.tv_usec - start.tv_usec);
151 if (elapsed > timeout)
153 set_current_state(TASK_INTERRUPTIBLE);
154 schedule_timeout(((timeout -
155 elapsed) * HZ) / 10000);
157 poll_freewait(&table);
162 if (f->f_op->read(f, &ch, 1, &f->f_pos) == 1)
166 /* Device does not support poll, busy wait */
172 if (retries >= timeout)
176 if (f->f_op->read(f, &ch, 1, &f->f_pos) == 1) {
188 static void tty_setspeed(struct file *f, int speed)
196 struct termios settings;
198 tty_ioctl(f, TCGETS, (unsigned long)&settings);
199 /* printk("Speed: %d\n", settings.c_cflag & (CBAUD | CBAUDEX)); */
200 settings.c_iflag = 0;
201 settings.c_oflag = 0;
202 settings.c_lflag = 0;
203 settings.c_cflag = CLOCAL | CS8 | CREAD;
204 settings.c_cc[VMIN] = 0;
205 settings.c_cc[VTIME] = 0;
208 settings.c_cflag |= B2400;
212 settings.c_cflag |= B4800;
216 settings.c_cflag |= B9600;
220 settings.c_cflag |= B19200;
224 settings.c_cflag |= B38400;
228 settings.c_cflag |= B57600;
232 settings.c_cflag |= B115200;
236 settings.c_cflag |= B9600;
240 tty_ioctl(f, TCSETS, (unsigned long)&settings);
241 /* printk("Speed: %d\n", settings.c_cflag & (CBAUD | CBAUDEX)); */
244 /* Set low latency */
245 struct serial_struct settings;
247 tty_ioctl(f, TIOCGSERIAL, (unsigned long)&settings);
248 settings.flags |= ASYNC_LOW_LATENCY;
249 tty_ioctl(f, TIOCSSERIAL, (unsigned long)&settings);
255 static void poll_digital(struct file *f, int channel)
259 cmd = 0x40 | (channel & 0x1f);
260 tty_write(f, &cmd, 1);
263 static void poll_channel(struct file *f, int channel)
267 cmd = 0x60 | (channel & 0x1f);
268 tty_write(f, &cmd, 1);
271 static struct serial_data serial_read(struct file *f, int timeout)
273 struct serial_data result;
276 result.kind = is_invalid;
281 int data = tty_read(f, timeout);
285 printk(KERN_ERR "serial2002 error\n");
287 } else if (data & 0x80) {
288 result.value = (result.value << 7) | (data & 0x7f);
291 switch ((data >> 5) & 0x03) {
294 result.kind = is_digital;
299 result.kind = is_digital;
305 (result.value << 2) | ((data & 0x60) >> 5);
306 result.kind = is_channel;
308 result.index = data & 0x1f;
316 static void serial_write(struct file *f, struct serial_data data)
318 if (data.kind == is_digital) {
320 ((data.value << 5) & 0x20) | (data.index & 0x1f);
321 tty_write(f, &ch, 1);
325 if (data.value >= (1L << 30)) {
326 ch[i] = 0x80 | ((data.value >> 30) & 0x03);
329 if (data.value >= (1L << 23)) {
330 ch[i] = 0x80 | ((data.value >> 23) & 0x7f);
333 if (data.value >= (1L << 16)) {
334 ch[i] = 0x80 | ((data.value >> 16) & 0x7f);
337 if (data.value >= (1L << 9)) {
338 ch[i] = 0x80 | ((data.value >> 9) & 0x7f);
341 ch[i] = 0x80 | ((data.value >> 2) & 0x7f);
343 ch[i] = ((data.value << 5) & 0x60) | (data.index & 0x1f);
349 static int serial_2002_open(struct comedi_device *dev)
354 sprintf(port, "/dev/ttyS%d", devpriv->port);
355 devpriv->tty = filp_open(port, O_RDWR, 0);
356 if (IS_ERR(devpriv->tty)) {
357 result = (int)PTR_ERR(devpriv->tty);
358 printk(KERN_ERR "serial_2002: file open error = %d\n", result);
368 struct config_t *dig_in_config;
369 struct config_t *dig_out_config;
370 struct config_t *chan_in_config;
371 struct config_t *chan_out_config;
375 dig_in_config = kcalloc(32, sizeof(struct config_t),
377 dig_out_config = kcalloc(32, sizeof(struct config_t),
379 chan_in_config = kcalloc(32, sizeof(struct config_t),
381 chan_out_config = kcalloc(32, sizeof(struct config_t),
383 if (!dig_in_config || !dig_out_config
384 || !chan_in_config || !chan_out_config) {
386 goto err_alloc_configs;
389 tty_setspeed(devpriv->tty, devpriv->speed);
390 poll_channel(devpriv->tty, 31); /* Start reading configuration */
392 struct serial_data data;
394 data = serial_read(devpriv->tty, 1000);
395 if (data.kind != is_channel || data.index != 31
396 || !(data.value & 0xe0)) {
399 int command, channel, kind;
400 struct config_t *cur_config = NULL;
402 channel = data.value & 0x1f;
403 kind = (data.value >> 5) & 0x7;
404 command = (data.value >> 8) & 0x3;
407 cur_config = dig_in_config;
411 cur_config = dig_out_config;
415 cur_config = chan_in_config;
419 cur_config = chan_out_config;
423 cur_config = chan_in_config;
429 cur_config[channel].kind = kind;
432 cur_config[channel].bits
474 cur_config[channel].min
514 cur_config[channel].max
522 for (i = 0; i <= 4; i++) {
523 /* Fill in subdev data */
525 unsigned char *mapping = NULL;
526 struct serial2002_range_table_t *range = NULL;
532 mapping = devpriv->digital_in_mapping;
538 mapping = devpriv->digital_out_mapping;
544 mapping = devpriv->analog_in_mapping;
545 range = devpriv->in_range;
551 mapping = devpriv->analog_out_mapping;
552 range = devpriv->out_range;
558 mapping = devpriv->encoder_in_mapping;
559 range = devpriv->in_range;
569 struct comedi_subdevice *s;
570 const struct comedi_lrange **range_table_list =
572 unsigned int *maxdata_list;
575 for (chan = 0, j = 0; j < 32; j++) {
576 if (c[j].kind == kind)
579 s = &dev->subdevices[i];
582 kfree(s->maxdata_list);
583 s->maxdata_list = maxdata_list =
584 kmalloc(sizeof(unsigned int) * s->n_chan,
586 if (!s->maxdata_list)
587 break; /* error handled below */
588 kfree(s->range_table_list);
589 s->range_table = NULL;
590 s->range_table_list = NULL;
592 s->range_table_list = range_table_list =
595 serial2002_range_table_t) *
596 s->n_chan, GFP_KERNEL);
597 if (!s->range_table_list)
598 break; /* err handled below */
600 for (chan = 0, j = 0; j < 32; j++) {
601 if (c[j].kind == kind) {
610 range_table_list[chan] =
616 ((long long)1 << c[j].bits)
624 /* Failed to allocate maxdata_list or range_table_list
625 * for a subdevice that needed it. */
627 for (i = 0; i <= 4; i++) {
628 struct comedi_subdevice *s;
630 s = &dev->subdevices[i];
631 kfree(s->maxdata_list);
632 s->maxdata_list = NULL;
633 kfree(s->range_table_list);
634 s->range_table_list = NULL;
639 kfree(dig_in_config);
640 kfree(dig_out_config);
641 kfree(chan_in_config);
642 kfree(chan_out_config);
646 filp_close(devpriv->tty, NULL);
654 static void serial_2002_close(struct comedi_device *dev)
656 if (!IS_ERR(devpriv->tty) && devpriv->tty)
657 filp_close(devpriv->tty, NULL);
660 static int serial2002_di_rinsn(struct comedi_device *dev,
661 struct comedi_subdevice *s,
662 struct comedi_insn *insn, unsigned int *data)
667 chan = devpriv->digital_in_mapping[CR_CHAN(insn->chanspec)];
668 for (n = 0; n < insn->n; n++) {
669 struct serial_data read;
671 poll_digital(devpriv->tty, chan);
673 read = serial_read(devpriv->tty, 1000);
674 if (read.kind != is_digital || read.index == chan)
677 data[n] = read.value;
682 static int serial2002_do_winsn(struct comedi_device *dev,
683 struct comedi_subdevice *s,
684 struct comedi_insn *insn, unsigned int *data)
689 chan = devpriv->digital_out_mapping[CR_CHAN(insn->chanspec)];
690 for (n = 0; n < insn->n; n++) {
691 struct serial_data write;
693 write.kind = is_digital;
695 write.value = data[n];
696 serial_write(devpriv->tty, write);
701 static int serial2002_ai_rinsn(struct comedi_device *dev,
702 struct comedi_subdevice *s,
703 struct comedi_insn *insn, unsigned int *data)
708 chan = devpriv->analog_in_mapping[CR_CHAN(insn->chanspec)];
709 for (n = 0; n < insn->n; n++) {
710 struct serial_data read;
712 poll_channel(devpriv->tty, chan);
714 read = serial_read(devpriv->tty, 1000);
715 if (read.kind != is_channel || read.index == chan)
718 data[n] = read.value;
723 static int serial2002_ao_winsn(struct comedi_device *dev,
724 struct comedi_subdevice *s,
725 struct comedi_insn *insn, unsigned int *data)
730 chan = devpriv->analog_out_mapping[CR_CHAN(insn->chanspec)];
731 for (n = 0; n < insn->n; n++) {
732 struct serial_data write;
734 write.kind = is_channel;
736 write.value = data[n];
737 serial_write(devpriv->tty, write);
738 devpriv->ao_readback[chan] = data[n];
743 static int serial2002_ao_rinsn(struct comedi_device *dev,
744 struct comedi_subdevice *s,
745 struct comedi_insn *insn, unsigned int *data)
748 int chan = CR_CHAN(insn->chanspec);
750 for (n = 0; n < insn->n; n++)
751 data[n] = devpriv->ao_readback[chan];
756 static int serial2002_ei_rinsn(struct comedi_device *dev,
757 struct comedi_subdevice *s,
758 struct comedi_insn *insn, unsigned int *data)
763 chan = devpriv->encoder_in_mapping[CR_CHAN(insn->chanspec)];
764 for (n = 0; n < insn->n; n++) {
765 struct serial_data read;
767 poll_channel(devpriv->tty, chan);
769 read = serial_read(devpriv->tty, 1000);
770 if (read.kind != is_channel || read.index == chan)
773 data[n] = read.value;
778 static int serial2002_attach(struct comedi_device *dev,
779 struct comedi_devconfig *it)
781 const struct serial2002_board *board = comedi_board(dev);
782 struct comedi_subdevice *s;
785 dev_dbg(dev->class_dev, "serial2002: attach\n");
786 dev->board_name = board->name;
787 if (alloc_private(dev, sizeof(struct serial2002_private)) < 0)
789 dev->open = serial_2002_open;
790 dev->close = serial_2002_close;
791 devpriv->port = it->options[0];
792 devpriv->speed = it->options[1];
793 dev_dbg(dev->class_dev, "/dev/ttyS%d @ %d\n", devpriv->port,
796 ret = comedi_alloc_subdevices(dev, 5);
800 /* digital input subdevice */
801 s = dev->subdevices + 0;
802 s->type = COMEDI_SUBD_DI;
803 s->subdev_flags = SDF_READABLE;
806 s->range_table = &range_digital;
807 s->insn_read = &serial2002_di_rinsn;
809 /* digital output subdevice */
810 s = dev->subdevices + 1;
811 s->type = COMEDI_SUBD_DO;
812 s->subdev_flags = SDF_WRITEABLE;
815 s->range_table = &range_digital;
816 s->insn_write = &serial2002_do_winsn;
818 /* analog input subdevice */
819 s = dev->subdevices + 2;
820 s->type = COMEDI_SUBD_AI;
821 s->subdev_flags = SDF_READABLE | SDF_GROUND;
824 s->range_table = NULL;
825 s->insn_read = &serial2002_ai_rinsn;
827 /* analog output subdevice */
828 s = dev->subdevices + 3;
829 s->type = COMEDI_SUBD_AO;
830 s->subdev_flags = SDF_WRITEABLE;
833 s->range_table = NULL;
834 s->insn_write = &serial2002_ao_winsn;
835 s->insn_read = &serial2002_ao_rinsn;
837 /* encoder input subdevice */
838 s = dev->subdevices + 4;
839 s->type = COMEDI_SUBD_COUNTER;
840 s->subdev_flags = SDF_READABLE | SDF_LSAMPL;
843 s->range_table = NULL;
844 s->insn_read = &serial2002_ei_rinsn;
849 static void serial2002_detach(struct comedi_device *dev)
851 struct comedi_subdevice *s;
854 for (i = 0; i < 5; i++) {
855 s = &dev->subdevices[i];
856 kfree(s->maxdata_list);
857 kfree(s->range_table_list);
861 static const struct serial2002_board serial2002_boards[] = {
867 static struct comedi_driver serial2002_driver = {
868 .driver_name = "serial2002",
869 .module = THIS_MODULE,
870 .attach = serial2002_attach,
871 .detach = serial2002_detach,
872 .board_name = &serial2002_boards[0].name,
873 .offset = sizeof(struct serial2002_board),
874 .num_names = ARRAY_SIZE(serial2002_boards),
876 module_comedi_driver(serial2002_driver);
878 MODULE_AUTHOR("Comedi http://www.comedi.org");
879 MODULE_DESCRIPTION("Comedi low-level driver");
880 MODULE_LICENSE("GPL");