Staging: comedi: Remove pcmmio_board typedef
[linux-2.6] / drivers / staging / comedi / drivers / serial2002.c
1 /*
2     comedi/drivers/serial2002.c
3     Skeleton code for a Comedi driver
4
5     COMEDI - Linux Control and Measurement Device Interface
6     Copyright (C) 2002 Anders Blomdell <anders.blomdell@control.lth.se>
7
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.
12
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.
17
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.
21
22 */
23
24 /*
25 Driver: serial2002
26 Description: Driver for serial connected hardware
27 Devices:
28 Author: Anders Blomdell
29 Updated: Fri,  7 Jun 2002 12:56:45 -0700
30 Status: in development
31
32 */
33
34 #include "../comedidev.h"
35
36 #include <linux/delay.h>
37 #include <linux/ioport.h>
38
39 #include <asm/termios.h>
40 #include <asm/ioctls.h>
41 #include <linux/serial.h>
42 #include <linux/poll.h>
43
44 /*
45  * Board descriptions for two imaginary boards.  Describing the
46  * boards in this way is optional, and completely driver-dependent.
47  * Some drivers use arrays such as this, other do not.
48  */
49 typedef struct serial2002_board_struct {
50         const char *name;
51 } serial2002_board;
52
53 static const serial2002_board serial2002_boards[] = {
54         {
55       name:     "serial2002"}
56 };
57
58 /*
59  * Useful for shorthand access to the particular board structure
60  */
61 #define thisboard ((const serial2002_board *)dev->board_ptr)
62
63 struct serial2002_range_table_t {
64
65         // HACK...
66         int length;
67         struct comedi_krange range;
68 };
69
70
71 typedef struct {
72         int port;               // /dev/ttyS<port>
73         int speed;              // baudrate
74         struct file *tty;
75         unsigned int ao_readback[32];
76         unsigned char digital_in_mapping[32];
77         unsigned char digital_out_mapping[32];
78         unsigned char analog_in_mapping[32];
79         unsigned char analog_out_mapping[32];
80         unsigned char encoder_in_mapping[32];
81         struct serial2002_range_table_t in_range[32], out_range[32];
82 } serial2002_private;
83
84 /*
85  * most drivers define the following macro to make it easy to
86  * access the private structure.
87  */
88 #define devpriv ((serial2002_private *)dev->private)
89
90 static int serial2002_attach(struct comedi_device * dev, struct comedi_devconfig * it);
91 static int serial2002_detach(struct comedi_device * dev);
92 struct comedi_driver driver_serial2002 = {
93       driver_name:"serial2002",
94       module:THIS_MODULE,
95       attach:serial2002_attach,
96       detach:serial2002_detach,
97       board_name:&serial2002_boards[0].name,
98       offset:sizeof(serial2002_board),
99       num_names:sizeof(serial2002_boards) / sizeof(serial2002_board),
100 };
101
102 static int serial2002_di_rinsn(struct comedi_device * dev, struct comedi_subdevice * s,
103         struct comedi_insn * insn, unsigned int * data);
104 static int serial2002_do_winsn(struct comedi_device * dev, struct comedi_subdevice * s,
105         struct comedi_insn * insn, unsigned int * data);
106 static int serial2002_ai_rinsn(struct comedi_device * dev, struct comedi_subdevice * s,
107         struct comedi_insn * insn, unsigned int * data);
108 static int serial2002_ao_winsn(struct comedi_device * dev, struct comedi_subdevice * s,
109         struct comedi_insn * insn, unsigned int * data);
110 static int serial2002_ao_rinsn(struct comedi_device * dev, struct comedi_subdevice * s,
111         struct comedi_insn * insn, unsigned int * data);
112
113 struct serial_data {
114         enum { is_invalid, is_digital, is_channel } kind;
115         int index;
116         unsigned long value;
117 };
118
119 static long tty_ioctl(struct file *f, unsigned op, unsigned long param)
120 {
121 #ifdef HAVE_UNLOCKED_IOCTL
122         if (f->f_op->unlocked_ioctl) {
123                 return f->f_op->unlocked_ioctl(f, op, param);
124         }
125 #endif
126         if (f->f_op->ioctl) {
127                 return f->f_op->ioctl(f->f_dentry->d_inode, f, op, param);
128         }
129         return -ENOSYS;
130 }
131
132 static int tty_write(struct file *f, unsigned char *buf, int count)
133 {
134         int result;
135         mm_segment_t oldfs;
136
137         oldfs = get_fs();
138         set_fs(KERNEL_DS);
139         f->f_pos = 0;
140         result = f->f_op->write(f, buf, count, &f->f_pos);
141         set_fs(oldfs);
142         return result;
143 }
144
145 #if 0
146 /*
147  * On 2.6.26.3 this occaisonally gave me page faults, worked around by
148  * settings.c_cc[VMIN] = 0; settings.c_cc[VTIME] = 0
149  */
150 static int tty_available(struct file *f)
151 {
152         long result = 0;
153         mm_segment_t oldfs;
154
155         oldfs = get_fs();
156         set_fs(KERNEL_DS);
157         tty_ioctl(f, FIONREAD, (unsigned long)&result);
158         set_fs(oldfs);
159         return result;
160 }
161 #endif
162
163 static int tty_read(struct file *f, int timeout)
164 {
165         int result;
166
167         result = -1;
168         if (!IS_ERR(f)) {
169                 mm_segment_t oldfs;
170
171                 oldfs = get_fs();
172                 set_fs(KERNEL_DS);
173                 if (f->f_op->poll) {
174                         struct poll_wqueues table;
175                         struct timeval start, now;
176
177                         do_gettimeofday(&start);
178                         poll_initwait(&table);
179                         while (1) {
180                                 long elapsed;
181                                 int mask;
182
183                                 mask = f->f_op->poll(f, &table.pt);
184                                 if (mask & (POLLRDNORM | POLLRDBAND | POLLIN |
185                                                 POLLHUP | POLLERR)) {
186                                         break;
187                                 }
188                                 do_gettimeofday(&now);
189                                 elapsed =
190                                         (1000000 * (now.tv_sec - start.tv_sec) +
191                                         now.tv_usec - start.tv_usec);
192                                 if (elapsed > timeout) {
193                                         break;
194                                 }
195                                 set_current_state(TASK_INTERRUPTIBLE);
196                                 schedule_timeout(((timeout -
197                                                         elapsed) * HZ) / 10000);
198                         }
199                         poll_freewait(&table);
200                         {
201                           unsigned char ch;
202
203                           f->f_pos = 0;
204                           if (f->f_op->read(f, &ch, 1, &f->f_pos) == 1) {
205                             result = ch;
206                           }
207                         }
208                 } else {
209                         /* Device does not support poll, busy wait */
210                         int retries = 0;
211                         while (1) {
212                                 unsigned char ch;
213
214                                 retries++;
215                                 if (retries >= timeout) {
216                                         break;
217                                 }
218
219                                 f->f_pos = 0;
220                                 if (f->f_op->read(f, &ch, 1, &f->f_pos) == 1) {
221                                         result = ch;
222                                         break;
223                                 }
224                                 comedi_udelay(100);
225                         }
226                 }
227                 set_fs(oldfs);
228         }
229         return result;
230 }
231
232 static void tty_setspeed(struct file *f, int speed)
233 {
234         mm_segment_t oldfs;
235
236         oldfs = get_fs();
237         set_fs(KERNEL_DS);
238         {
239                 // Set speed
240                 struct termios settings;
241
242                 tty_ioctl(f, TCGETS, (unsigned long)&settings);
243 //    printk("Speed: %d\n", settings.c_cflag & (CBAUD | CBAUDEX));
244                 settings.c_iflag = 0;
245                 settings.c_oflag = 0;
246                 settings.c_lflag = 0;
247                 settings.c_cflag = CLOCAL | CS8 | CREAD;
248                 settings.c_cc[VMIN] = 0;
249                 settings.c_cc[VTIME] = 0;
250                 switch (speed) {
251                 case 2400:{
252                                 settings.c_cflag |= B2400;
253                         }
254                         break;
255                 case 4800:{
256                                 settings.c_cflag |= B4800;
257                         }
258                         break;
259                 case 9600:{
260                                 settings.c_cflag |= B9600;
261                         }
262                         break;
263                 case 19200:{
264                                 settings.c_cflag |= B19200;
265                         }
266                         break;
267                 case 38400:{
268                                 settings.c_cflag |= B38400;
269                         }
270                         break;
271                 case 57600:{
272                                 settings.c_cflag |= B57600;
273                         }
274                         break;
275                 case 115200:{
276                                 settings.c_cflag |= B115200;
277                         }
278                         break;
279                 default:{
280                                 settings.c_cflag |= B9600;
281                         }
282                         break;
283                 }
284                 tty_ioctl(f, TCSETS, (unsigned long)&settings);
285 //    printk("Speed: %d\n", settings.c_cflag & (CBAUD | CBAUDEX));
286         }
287         {
288                 // Set low latency
289                 struct serial_struct settings;
290
291                 tty_ioctl(f, TIOCGSERIAL, (unsigned long)&settings);
292                 settings.flags |= ASYNC_LOW_LATENCY;
293                 tty_ioctl(f, TIOCSSERIAL, (unsigned long)&settings);
294         }
295
296         set_fs(oldfs);
297 }
298
299 static void poll_digital(struct file *f, int channel)
300 {
301         char cmd;
302
303         cmd = 0x40 | (channel & 0x1f);
304         tty_write(f, &cmd, 1);
305 }
306
307 static void poll_channel(struct file *f, int channel)
308 {
309         char cmd;
310
311         cmd = 0x60 | (channel & 0x1f);
312         tty_write(f, &cmd, 1);
313 }
314
315 static struct serial_data serial_read(struct file *f, int timeout)
316 {
317         struct serial_data result;
318         int length;
319
320         result.kind = is_invalid;
321         result.index = 0;
322         result.value = 0;
323         length = 0;
324         while (1) {
325                 int data = tty_read(f, timeout);
326
327                 length++;
328                 if (data < 0) {
329                         printk("serial2002 error\n");
330                         break;
331                 } else if (data & 0x80) {
332                         result.value = (result.value << 7) | (data & 0x7f);
333                 } else {
334                         if (length == 1) {
335                                 switch ((data >> 5) & 0x03) {
336                                 case 0:{
337                                                 result.value = 0;
338                                                 result.kind = is_digital;
339                                         }
340                                         break;
341                                 case 1:{
342                                                 result.value = 1;
343                                                 result.kind = is_digital;
344                                         }
345                                         break;
346                                 }
347                         } else {
348                                 result.value =
349                                         (result.
350                                         value << 2) | ((data & 0x60) >> 5);
351                                 result.kind = is_channel;
352                         }
353                         result.index = data & 0x1f;
354                         break;
355                 }
356         }
357         return result;
358
359 }
360
361 static void serial_write(struct file *f, struct serial_data data)
362 {
363         if (data.kind == is_digital) {
364                 unsigned char ch =
365                         ((data.value << 5) & 0x20) | (data.index & 0x1f);
366                 tty_write(f, &ch, 1);
367         } else {
368                 unsigned char ch[6];
369                 int i = 0;
370                 if (data.value >= (1L << 30)) {
371                         ch[i] = 0x80 | ((data.value >> 30) & 0x03);
372                         i++;
373                 }
374                 if (data.value >= (1L << 23)) {
375                         ch[i] = 0x80 | ((data.value >> 23) & 0x7f);
376                         i++;
377                 }
378                 if (data.value >= (1L << 16)) {
379                         ch[i] = 0x80 | ((data.value >> 16) & 0x7f);
380                         i++;
381                 }
382                 if (data.value >= (1L << 9)) {
383                         ch[i] = 0x80 | ((data.value >> 9) & 0x7f);
384                         i++;
385                 }
386                 ch[i] = 0x80 | ((data.value >> 2) & 0x7f);
387                 i++;
388                 ch[i] = ((data.value << 5) & 0x60) | (data.index & 0x1f);
389                 i++;
390                 tty_write(f, ch, i);
391         }
392 }
393
394 static void serial_2002_open(struct comedi_device * dev)
395 {
396         char port[20];
397
398         sprintf(port, "/dev/ttyS%d", devpriv->port);
399         devpriv->tty = filp_open(port, 0, O_RDWR);
400         if (IS_ERR(devpriv->tty)) {
401                 printk("serial_2002: file open error = %ld\n",
402                         PTR_ERR(devpriv->tty));
403         } else {
404                 typedef struct {
405                         int kind;
406                         int bits;
407                         int min;
408                         int max;
409                 } config_t;
410                 config_t dig_in_config[32];
411                 config_t dig_out_config[32];
412                 config_t chan_in_config[32];
413                 config_t chan_out_config[32];
414                 int i;
415
416                 for (i = 0; i < 32; i++) {
417                         dig_in_config[i].kind = 0;
418                         dig_in_config[i].bits = 0;
419                         dig_in_config[i].min = 0;
420                         dig_in_config[i].max = 0;
421                         dig_out_config[i].kind = 0;
422                         dig_out_config[i].bits = 0;
423                         dig_out_config[i].min = 0;
424                         dig_out_config[i].max = 0;
425                         chan_in_config[i].kind = 0;
426                         chan_in_config[i].bits = 0;
427                         chan_in_config[i].min = 0;
428                         chan_in_config[i].max = 0;
429                         chan_out_config[i].kind = 0;
430                         chan_out_config[i].bits = 0;
431                         chan_out_config[i].min = 0;
432                         chan_out_config[i].max = 0;
433                 }
434
435                 tty_setspeed(devpriv->tty, devpriv->speed);
436                 poll_channel(devpriv->tty, 31); // Start reading configuration
437                 while (1) {
438                         struct serial_data data;
439
440                         data = serial_read(devpriv->tty, 1000);
441                         if (data.kind != is_channel || data.index != 31
442                                 || !(data.value & 0xe0)) {
443                                 break;
444                         } else {
445                                 int command, channel, kind;
446                                 config_t *cur_config = 0;
447
448                                 channel = data.value & 0x1f;
449                                 kind = (data.value >> 5) & 0x7;
450                                 command = (data.value >> 8) & 0x3;
451                                 switch (kind) {
452                                 case 1:{
453                                                 cur_config = dig_in_config;
454                                         }
455                                         break;
456                                 case 2:{
457                                                 cur_config = dig_out_config;
458                                         }
459                                         break;
460                                 case 3:{
461                                                 cur_config = chan_in_config;
462                                         }
463                                         break;
464                                 case 4:{
465                                                 cur_config = chan_out_config;
466                                         }
467                                         break;
468                                 case 5:{
469                                                 cur_config = chan_in_config;
470                                         }
471                                         break;
472                                 }
473
474                                 if (cur_config) {
475                                         cur_config[channel].kind = kind;
476                                         switch (command) {
477                                         case 0:{
478                                                         cur_config[channel].
479                                                                 bits =
480                                                                 (data.
481                                                                 value >> 10) &
482                                                                 0x3f;
483                                                 }
484                                                 break;
485                                         case 1:{
486                                                         int unit, sign, min;
487                                                         unit = (data.
488                                                                 value >> 10) &
489                                                                 0x7;
490                                                         sign = (data.
491                                                                 value >> 13) &
492                                                                 0x1;
493                                                         min = (data.
494                                                                 value >> 14) &
495                                                                 0xfffff;
496
497                                                         switch (unit) {
498                                                         case 0:{
499                                                                         min = min * 1000000;
500                                                                 }
501                                                                 break;
502                                                         case 1:{
503                                                                         min = min * 1000;
504                                                                 }
505                                                                 break;
506                                                         case 2:{
507                                                                         min = min * 1;
508                                                                 }
509                                                                 break;
510                                                         }
511                                                         if (sign) {
512                                                                 min = -min;
513                                                         }
514                                                         cur_config[channel].
515                                                                 min = min;
516                                                 }
517                                                 break;
518                                         case 2:{
519                                                         int unit, sign, max;
520                                                         unit = (data.
521                                                                 value >> 10) &
522                                                                 0x7;
523                                                         sign = (data.
524                                                                 value >> 13) &
525                                                                 0x1;
526                                                         max = (data.
527                                                                 value >> 14) &
528                                                                 0xfffff;
529
530                                                         switch (unit) {
531                                                         case 0:{
532                                                                         max = max * 1000000;
533                                                                 }
534                                                                 break;
535                                                         case 1:{
536                                                                         max = max * 1000;
537                                                                 }
538                                                                 break;
539                                                         case 2:{
540                                                                         max = max * 1;
541                                                                 }
542                                                                 break;
543                                                         }
544                                                         if (sign) {
545                                                                 max = -max;
546                                                         }
547                                                         cur_config[channel].
548                                                                 max = max;
549                                                 }
550                                                 break;
551                                         }
552                                 }
553                         }
554                 }
555                 for (i = 0; i <= 4; i++) {
556                         // Fill in subdev data
557                         config_t *c;
558                         unsigned char *mapping = 0;
559                         struct serial2002_range_table_t *range = 0;
560                         int kind = 0;
561
562                         switch (i) {
563                         case 0:{
564                                         c = dig_in_config;
565                                         mapping = devpriv->digital_in_mapping;
566                                         kind = 1;
567                                 }
568                                 break;
569                         case 1:{
570                                         c = dig_out_config;
571                                         mapping = devpriv->digital_out_mapping;
572                                         kind = 2;
573                                 }
574                                 break;
575                         case 2:{
576                                         c = chan_in_config;
577                                         mapping = devpriv->analog_in_mapping;
578                                         range = devpriv->in_range;
579                                         kind = 3;
580                                 }
581                                 break;
582                         case 3:{
583                                         c = chan_out_config;
584                                         mapping = devpriv->analog_out_mapping;
585                                         range = devpriv->out_range;
586                                         kind = 4;
587                                 }
588                                 break;
589                         case 4:{
590                                         c = chan_in_config;
591                                         mapping = devpriv->encoder_in_mapping;
592                                         range = devpriv->in_range;
593                                         kind = 5;
594                                 }
595                                 break;
596                         default:{
597                                         c = 0;
598                                 }
599                                 break;
600                         }
601                         if (c) {
602                                 struct comedi_subdevice *s;
603                                 const struct comedi_lrange **range_table_list = NULL;
604                                 unsigned int *maxdata_list;
605                                 int j, chan;
606
607                                 for (chan = 0, j = 0; j < 32; j++) {
608                                         if (c[j].kind == kind) {
609                                                 chan++;
610                                         }
611                                 }
612                                 s = &dev->subdevices[i];
613                                 s->n_chan = chan;
614                                 s->maxdata = 0;
615                                 if (s->maxdata_list) {
616                                         kfree(s->maxdata_list);
617                                 }
618                                 s->maxdata_list = maxdata_list =
619                                         kmalloc(sizeof(unsigned int) * s->n_chan,
620                                         GFP_KERNEL);
621                                 if (s->range_table_list) {
622                                         kfree(s->range_table_list);
623                                 }
624                                 if (range) {
625                                         s->range_table = 0;
626                                         s->range_table_list = range_table_list =
627                                                 kmalloc(sizeof
628                                                 (struct serial2002_range_table_t) *
629                                                 s->n_chan, GFP_KERNEL);
630                                 }
631                                 for (chan = 0, j = 0; j < 32; j++) {
632                                         if (c[j].kind == kind) {
633                                                 if (mapping) {
634                                                         mapping[chan] = j;
635                                                 }
636                                                 if (range) {
637                                                         range[j].length = 1;
638                                                         range[j].range.min =
639                                                                 c[j].min;
640                                                         range[j].range.max =
641                                                                 c[j].max;
642                                                         range_table_list[chan] =
643                                                                 (const struct
644                                                                 comedi_lrange *)
645                                                                 &range[j];
646                                                 }
647                                                 maxdata_list[chan] =
648                                                         ((long long)1 << c[j].
649                                                         bits) - 1;
650                                                 chan++;
651                                         }
652                                 }
653                         }
654                 }
655         }
656 }
657
658 static void serial_2002_close(struct comedi_device * dev)
659 {
660         if (!IS_ERR(devpriv->tty) && (devpriv->tty != 0)) {
661                 filp_close(devpriv->tty, 0);
662         }
663 }
664
665 static int serial2002_di_rinsn(struct comedi_device * dev, struct comedi_subdevice * s,
666         struct comedi_insn * insn, unsigned int * data)
667 {
668         int n;
669         int chan;
670
671         chan = devpriv->digital_in_mapping[CR_CHAN(insn->chanspec)];
672         for (n = 0; n < insn->n; n++) {
673                 struct serial_data read;
674
675                 poll_digital(devpriv->tty, chan);
676                 while (1) {
677                         read = serial_read(devpriv->tty, 1000);
678                         if (read.kind != is_digital || read.index == chan) {
679                                 break;
680                         }
681                 }
682                 data[n] = read.value;
683         }
684         return n;
685 }
686
687 static int serial2002_do_winsn(struct comedi_device * dev, struct comedi_subdevice * s,
688         struct comedi_insn * insn, unsigned int * data)
689 {
690         int n;
691         int chan;
692
693         chan = devpriv->digital_out_mapping[CR_CHAN(insn->chanspec)];
694         for (n = 0; n < insn->n; n++) {
695                 struct serial_data write;
696
697                 write.kind = is_digital;
698                 write.index = chan;
699                 write.value = data[n];
700                 serial_write(devpriv->tty, write);
701         }
702         return n;
703 }
704
705 static int serial2002_ai_rinsn(struct comedi_device * dev, struct comedi_subdevice * s,
706         struct comedi_insn * insn, unsigned int * data)
707 {
708         int n;
709         int chan;
710
711         chan = devpriv->analog_in_mapping[CR_CHAN(insn->chanspec)];
712         for (n = 0; n < insn->n; n++) {
713                 struct serial_data read;
714
715                 poll_channel(devpriv->tty, chan);
716                 while (1) {
717                         read = serial_read(devpriv->tty, 1000);
718                         if (read.kind != is_channel || read.index == chan) {
719                                 break;
720                         }
721                 }
722                 data[n] = read.value;
723         }
724         return n;
725 }
726
727 static int serial2002_ao_winsn(struct comedi_device * dev, struct comedi_subdevice * s,
728         struct comedi_insn * insn, unsigned int * data)
729 {
730         int n;
731         int chan;
732
733         chan = devpriv->analog_out_mapping[CR_CHAN(insn->chanspec)];
734         for (n = 0; n < insn->n; n++) {
735                 struct serial_data write;
736
737                 write.kind = is_channel;
738                 write.index = chan;
739                 write.value = data[n];
740                 serial_write(devpriv->tty, write);
741                 devpriv->ao_readback[chan] = data[n];
742         }
743         return n;
744 }
745
746 static int serial2002_ao_rinsn(struct comedi_device * dev, struct comedi_subdevice * s,
747         struct comedi_insn * insn, unsigned int * data)
748 {
749         int n;
750         int chan = CR_CHAN(insn->chanspec);
751
752         for (n = 0; n < insn->n; n++) {
753                 data[n] = devpriv->ao_readback[chan];
754         }
755
756         return n;
757 }
758
759 static int serial2002_ei_rinsn(struct comedi_device * dev, struct comedi_subdevice * s,
760         struct comedi_insn * insn, unsigned int * data)
761 {
762         int n;
763         int chan;
764
765         chan = devpriv->encoder_in_mapping[CR_CHAN(insn->chanspec)];
766         for (n = 0; n < insn->n; n++) {
767                 struct serial_data read;
768
769                 poll_channel(devpriv->tty, chan);
770                 while (1) {
771                         read = serial_read(devpriv->tty, 1000);
772                         if (read.kind != is_channel || read.index == chan) {
773                                 break;
774                         }
775                 }
776                 data[n] = read.value;
777         }
778         return n;
779 }
780
781 static int serial2002_attach(struct comedi_device * dev, struct comedi_devconfig * it)
782 {
783         struct comedi_subdevice *s;
784
785         printk("comedi%d: serial2002: ", dev->minor);
786         dev->board_name = thisboard->name;
787         if (alloc_private(dev, sizeof(serial2002_private)) < 0) {
788                 return -ENOMEM;
789         }
790         dev->open = serial_2002_open;
791         dev->close = serial_2002_close;
792         devpriv->port = it->options[0];
793         devpriv->speed = it->options[1];
794         printk("/dev/ttyS%d @ %d\n", devpriv->port, devpriv->speed);
795
796         if (alloc_subdevices(dev, 5) < 0)
797                 return -ENOMEM;
798
799         /* digital input subdevice */
800         s = dev->subdevices + 0;
801         s->type = COMEDI_SUBD_DI;
802         s->subdev_flags = SDF_READABLE;
803         s->n_chan = 0;
804         s->maxdata = 1;
805         s->range_table = &range_digital;
806         s->insn_read = &serial2002_di_rinsn;
807
808         /* digital output subdevice */
809         s = dev->subdevices + 1;
810         s->type = COMEDI_SUBD_DO;
811         s->subdev_flags = SDF_WRITEABLE;
812         s->n_chan = 0;
813         s->maxdata = 1;
814         s->range_table = &range_digital;
815         s->insn_write = &serial2002_do_winsn;
816
817         /* analog input subdevice */
818         s = dev->subdevices + 2;
819         s->type = COMEDI_SUBD_AI;
820         s->subdev_flags = SDF_READABLE | SDF_GROUND;
821         s->n_chan = 0;
822         s->maxdata = 1;
823         s->range_table = 0;
824         s->insn_read = &serial2002_ai_rinsn;
825
826         /* analog output subdevice */
827         s = dev->subdevices + 3;
828         s->type = COMEDI_SUBD_AO;
829         s->subdev_flags = SDF_WRITEABLE;
830         s->n_chan = 0;
831         s->maxdata = 1;
832         s->range_table = 0;
833         s->insn_write = &serial2002_ao_winsn;
834         s->insn_read = &serial2002_ao_rinsn;
835
836         /* encoder input subdevice */
837         s = dev->subdevices + 4;
838         s->type = COMEDI_SUBD_COUNTER;
839         s->subdev_flags = SDF_READABLE | SDF_LSAMPL;
840         s->n_chan = 0;
841         s->maxdata = 1;
842         s->range_table = 0;
843         s->insn_read = &serial2002_ei_rinsn;
844
845         return 1;
846 }
847
848 static int serial2002_detach(struct comedi_device * dev)
849 {
850         struct comedi_subdevice *s;
851         int i;
852
853         printk("comedi%d: serial2002: remove\n", dev->minor);
854         for (i = 0; i < 4; i++) {
855                 s = &dev->subdevices[i];
856                 if (s->maxdata_list) {
857                         kfree(s->maxdata_list);
858                 }
859                 if (s->range_table_list) {
860                         kfree(s->range_table_list);
861                 }
862         }
863         return 0;
864 }
865
866 COMEDI_INITCLEANUP(driver_serial2002);