Merge branch 'for-2.6.31' of git://git.linux-nfs.org/projects/trondmy/nfs-2.6
[pandora-kernel.git] / drivers / staging / meilhaus / me8200_di.c
1 /**
2  * @file me8200_di.c
3  *
4  * @brief ME-8200 digital input subdevice instance.
5  * @note Copyright (C) 2007 Meilhaus Electronic GmbH (support@meilhaus.de)
6  * @author Guenter Gebhardt
7  * @author Krzysztof Gantzke    (k.gantzke@meilhaus.de)
8  */
9
10 /*
11  * Copyright (C) 2007 Meilhaus Electronic GmbH (support@meilhaus.de)
12  *
13  * This file is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26  */
27
28 #ifndef __KERNEL__
29 #  define __KERNEL__
30 #endif
31
32 ///Includes
33 #include <linux/module.h>
34
35 #include <linux/slab.h>
36 #include <linux/spinlock.h>
37 #include <linux/io.h>
38 #include <linux/types.h>
39 #include <linux/interrupt.h>
40
41 #include "medefines.h"
42 #include "meerror.h"
43
44 #include "meids.h"
45 #include "medebug.h"
46 #include "me8200_reg.h"
47 #include "me8200_di_reg.h"
48 #include "me8200_di.h"
49
50 /// Defines
51 static void me8200_di_destructor(struct me_subdevice *subdevice);
52 static int me8200_di_io_irq_start(me_subdevice_t *subdevice,
53                                   struct file *filep,
54                                   int channel,
55                                   int irq_source,
56                                   int irq_edge, int irq_arg, int flags);
57 static int me8200_di_io_irq_wait(me_subdevice_t *subdevice,
58                                  struct file *filep,
59                                  int channel,
60                                  int *irq_count,
61                                  int *value, int time_out, int flags);
62 static int me8200_di_io_irq_stop(me_subdevice_t *subdevice,
63                                  struct file *filep, int channel, int flags);
64 static int me8200_di_io_single_config(me_subdevice_t *subdevice,
65                                       struct file *filep,
66                                       int channel,
67                                       int single_config,
68                                       int ref,
69                                       int trig_chan,
70                                       int trig_type, int trig_edge, int flags);
71 static int me8200_di_io_single_read(me_subdevice_t *subdevice,
72                                     struct file *filep,
73                                     int channel,
74                                     int *value, int time_out, int flags);
75 static int me8200_di_io_reset_subdevice(struct me_subdevice *subdevice,
76                                         struct file *filep, int flags);
77 static int me8200_di_query_number_channels(me_subdevice_t *subdevice,
78                                            int *number);
79 static int me8200_di_query_subdevice_type(me_subdevice_t *subdevice,
80                                           int *type, int *subtype);
81 static int me8200_di_query_subdevice_caps(me_subdevice_t *subdevice,
82                                           int *caps);
83 static irqreturn_t me8200_isr(int irq, void *dev_id);
84 static irqreturn_t me8200_isr_EX(int irq, void *dev_id);
85 static void me8200_di_check_version(me8200_di_subdevice_t *instance,
86                                     unsigned long addr);
87
88 ///Functions
89 static int me8200_di_io_irq_start(me_subdevice_t *subdevice,
90                                   struct file *filep,
91                                   int channel,
92                                   int irq_source,
93                                   int irq_edge, int irq_arg, int flags)
94 {
95         me8200_di_subdevice_t *instance;
96         int err = ME_ERRNO_SUCCESS;
97         volatile uint8_t tmp;
98         unsigned long status;
99
100         PDEBUG("executed.\n");
101
102         instance = (me8200_di_subdevice_t *) subdevice;
103
104         if (irq_source == ME_IRQ_SOURCE_DIO_PATTERN) {
105                 if (flags &
106                     ~(ME_IO_IRQ_START_PATTERN_FILTERING |
107                       ME_IO_IRQ_START_DIO_BYTE)) {
108                         PERROR("Invalid flag specified.\n");
109                         return ME_ERRNO_INVALID_FLAGS;
110                 }
111
112                 if (irq_edge != ME_IRQ_EDGE_NOT_USED) {
113                         PERROR("Invalid irq edge specified.\n");
114                         return ME_ERRNO_INVALID_IRQ_EDGE;
115                 }
116         } else if (irq_source == ME_IRQ_SOURCE_DIO_MASK) {
117                 if (flags &
118                     ~(ME_IO_IRQ_START_EXTENDED_STATUS |
119                       ME_IO_IRQ_START_DIO_BYTE)) {
120                         PERROR("Invalid flag specified.\n");
121                         return ME_ERRNO_INVALID_FLAGS;
122                 }
123
124                 if ((irq_edge != ME_IRQ_EDGE_RISING)
125                     && (irq_edge != ME_IRQ_EDGE_FALLING)
126                     && (irq_edge != ME_IRQ_EDGE_ANY)) {
127                         PERROR("Invalid irq edge specified.\n");
128                         return ME_ERRNO_INVALID_IRQ_EDGE;
129                 }
130
131                 if (!(irq_arg & 0xFF)) {
132                         PERROR("No mask specified.\n");
133                         return ME_ERRNO_INVALID_IRQ_ARG;
134                 }
135         } else {
136                 PERROR("Invalid irq source specified.\n");
137                 return ME_ERRNO_INVALID_IRQ_SOURCE;
138         }
139
140         if (channel) {
141                 PERROR("Invalid channel specified.\n");
142                 return ME_ERRNO_INVALID_CHANNEL;
143         }
144
145         ME_SUBDEVICE_ENTER;
146
147         spin_lock_irqsave(&instance->subdevice_lock, status);
148         if (irq_source == ME_IRQ_SOURCE_DIO_PATTERN) {
149                 outb(irq_arg, instance->compare_reg);
150                 PDEBUG_REG("compare_reg outb(0x%lX+0x%lX)=0x%x\n",
151                            instance->reg_base,
152                            instance->compare_reg - instance->reg_base, irq_arg);
153                 outb(0xFF, instance->mask_reg);
154                 PDEBUG_REG("mask_reg outb(0x%lX+0x%lX)=0x%x\n",
155                            instance->reg_base,
156                            instance->mask_reg - instance->reg_base, 0xff);
157                 instance->compare_value = irq_arg;
158                 instance->filtering_flag =
159                     (flags & ME_IO_IRQ_START_PATTERN_FILTERING) ? 1 : 0;
160         }
161         if (irq_source == ME_IRQ_SOURCE_DIO_MASK) {
162                 outb(irq_arg, instance->mask_reg);
163                 PDEBUG_REG("mask_reg outb(0x%lX+0x%lX)=0x%x\n",
164                            instance->reg_base,
165                            instance->mask_reg - instance->reg_base, irq_arg);
166                 instance->filtering_flag = 0;
167         }
168
169         spin_lock(instance->irq_mode_lock);
170         tmp = inb(instance->irq_mode_reg);
171         tmp &=
172             ~(ME8200_IRQ_MODE_MASK <<
173               (ME8200_IRQ_MODE_DI_SHIFT * instance->di_idx));
174         if (irq_source == ME_IRQ_SOURCE_DIO_PATTERN) {
175                 tmp |=
176                     ME8200_IRQ_MODE_MASK_COMPARE << (ME8200_IRQ_MODE_DI_SHIFT *
177                                                      instance->di_idx);
178         }
179
180         if (irq_source == ME_IRQ_SOURCE_DIO_MASK) {
181                 tmp |=
182                     ME8200_IRQ_MODE_MASK_MASK << (ME8200_IRQ_MODE_DI_SHIFT *
183                                                   instance->di_idx);
184         }
185         outb(tmp, instance->irq_mode_reg);
186         PDEBUG_REG("irq_mode_reg outb(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
187                    instance->irq_mode_reg - instance->reg_base, tmp);
188         spin_unlock(instance->irq_mode_lock);
189
190         spin_lock(instance->irq_ctrl_lock);
191         tmp = inb(instance->irq_ctrl_reg);
192         tmp |=
193             (ME8200_DI_IRQ_CTRL_BIT_CLEAR <<
194              (ME8200_DI_IRQ_CTRL_SHIFT * instance->di_idx));
195         tmp |=
196             ME8200_DI_IRQ_CTRL_BIT_ENABLE << (ME8200_DI_IRQ_CTRL_SHIFT *
197                                               instance->di_idx);
198
199         if (irq_source == ME_IRQ_SOURCE_DIO_MASK) {
200                 tmp &=
201                     ~(ME8200_DI_IRQ_CTRL_MASK_EDGE <<
202                       (ME8200_DI_IRQ_CTRL_SHIFT * instance->di_idx));
203                 if (irq_edge == ME_IRQ_EDGE_RISING) {
204                         tmp |=
205                             ME8200_DI_IRQ_CTRL_MASK_EDGE_RISING <<
206                             (ME8200_DI_IRQ_CTRL_SHIFT * instance->di_idx);
207                 } else if (irq_edge == ME_IRQ_EDGE_FALLING) {
208                         tmp |=
209                             ME8200_DI_IRQ_CTRL_MASK_EDGE_FALLING <<
210                             (ME8200_DI_IRQ_CTRL_SHIFT * instance->di_idx);
211                 } else if (irq_edge == ME_IRQ_EDGE_ANY) {
212                         tmp |=
213                             ME8200_DI_IRQ_CTRL_MASK_EDGE_ANY <<
214                             (ME8200_DI_IRQ_CTRL_SHIFT * instance->di_idx);
215                 }
216         }
217         outb(tmp, instance->irq_ctrl_reg);
218         PDEBUG_REG("irq_ctrl_reg outb(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
219                    instance->irq_ctrl_reg - instance->reg_base, tmp);
220         tmp &=
221             ~(ME8200_DI_IRQ_CTRL_BIT_CLEAR <<
222               (ME8200_DI_IRQ_CTRL_SHIFT * instance->di_idx));
223         outb(tmp, instance->irq_ctrl_reg);
224         PDEBUG_REG("irq_ctrl_reg outb(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
225                    instance->irq_ctrl_reg - instance->reg_base, tmp);
226
227         instance->line_value = inb(instance->port_reg);
228         spin_unlock(instance->irq_ctrl_lock);
229
230         instance->rised = 0;
231         instance->status_value = 0;
232         instance->status_value_edges = 0;
233         instance->status_flag = flags & ME_IO_IRQ_START_EXTENDED_STATUS;
234         spin_unlock_irqrestore(&instance->subdevice_lock, status);
235         ME_SUBDEVICE_EXIT;
236
237         return err;
238 }
239
240 static int me8200_di_io_irq_wait(me_subdevice_t *subdevice,
241                                  struct file *filep,
242                                  int channel,
243                                  int *irq_count,
244                                  int *value, int time_out, int flags)
245 {
246         me8200_di_subdevice_t *instance;
247         int err = ME_ERRNO_SUCCESS;
248         long t = 0;
249         unsigned long cpu_flags;
250         int count;
251
252         PDEBUG("executed.\n");
253         PDEVELOP("PID: %d.\n", current->pid);
254
255         instance = (me8200_di_subdevice_t *) subdevice;
256
257         if (flags &
258             ~(ME_IO_IRQ_WAIT_NORMAL_STATUS | ME_IO_IRQ_WAIT_EXTENDED_STATUS)) {
259                 PERROR("Invalid flag specified.\n");
260                 return ME_ERRNO_INVALID_FLAGS;
261         }
262
263         if (channel) {
264                 PERROR("Invalid channel specified.\n");
265                 return ME_ERRNO_INVALID_CHANNEL;
266         }
267
268         if (time_out < 0) {
269                 PERROR("Invalid time_out specified.\n");
270                 return ME_ERRNO_INVALID_TIMEOUT;
271         }
272
273         if (time_out) {
274                 t = (time_out * HZ) / 1000;
275
276                 if (t == 0)
277                         t = 1;
278         }
279
280         ME_SUBDEVICE_ENTER;
281
282         if (instance->rised <= 0) {
283                 instance->rised = 0;
284                 count = instance->count;
285
286                 if (time_out) {
287                         t = wait_event_interruptible_timeout(instance->
288                                                              wait_queue,
289                                                              ((count !=
290                                                                instance->count)
291                                                               || (instance->
292                                                                   rised < 0)),
293                                                              t);
294 //                      t = wait_event_interruptible_timeout(instance->wait_queue, (instance->rised != 0), t);
295                         if (t == 0) {
296                                 PERROR("Wait on interrupt timed out.\n");
297                                 err = ME_ERRNO_TIMEOUT;
298                         }
299                 } else {
300                         wait_event_interruptible(instance->wait_queue,
301                                                  ((count != instance->count)
302                                                   || (instance->rised < 0)));
303 //                      wait_event_interruptible(instance->wait_queue, (instance->rised != 0));
304                 }
305
306                 if (instance->rised < 0) {
307                         PERROR("Wait on interrupt aborted by user.\n");
308                         err = ME_ERRNO_CANCELLED;
309                 }
310         }
311
312         if (signal_pending(current)) {
313                 PERROR("Wait on interrupt aborted by signal.\n");
314                 err = ME_ERRNO_SIGNAL;
315         }
316
317         spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
318         *irq_count = instance->count;
319         if (!err) {
320                 if (flags & ME_IO_IRQ_WAIT_NORMAL_STATUS) {
321                         *value = instance->status_value;
322                 } else if (flags & ME_IO_IRQ_WAIT_EXTENDED_STATUS) {
323                         *value = instance->status_value_edges;
324                 } else {        // Use default
325                         if (!instance->status_flag) {
326                                 *value = instance->status_value;
327                         } else {
328                                 *value = instance->status_value_edges;
329                         }
330                 }
331                 instance->rised = 0;
332 /*
333                         instance->status_value = 0;
334                         instance->status_value_edges = 0;
335 */
336         } else {
337                 *value = 0;
338         }
339         spin_unlock_irqrestore(&instance->subdevice_lock, cpu_flags);
340
341         ME_SUBDEVICE_EXIT;
342
343         return err;
344 }
345
346 static int me8200_di_io_irq_stop(me_subdevice_t *subdevice,
347                                  struct file *filep, int channel, int flags)
348 {
349         me8200_di_subdevice_t *instance;
350         uint8_t tmp;
351         unsigned long status;
352
353         PDEBUG("executed.\n");
354
355         instance = (me8200_di_subdevice_t *) subdevice;
356
357         if (flags) {
358                 PERROR("Invalid flag specified.\n");
359                 return ME_ERRNO_INVALID_FLAGS;
360         }
361
362         if (channel) {
363                 PERROR("Invalid channel specified.\n");
364                 return ME_ERRNO_INVALID_CHANNEL;
365         }
366
367         ME_SUBDEVICE_ENTER spin_lock_irqsave(&instance->subdevice_lock, status);
368         spin_lock(instance->irq_ctrl_lock);
369         tmp = inb(instance->irq_ctrl_reg);
370         tmp |=
371             (ME8200_DI_IRQ_CTRL_BIT_ENABLE <<
372              (ME8200_DI_IRQ_CTRL_SHIFT * instance->di_idx));
373         outb(tmp, instance->irq_ctrl_reg);
374         PDEBUG_REG("irq_ctrl_reg outb(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
375                    instance->irq_ctrl_reg - instance->reg_base, tmp);
376         tmp &=
377             ~(ME8200_DI_IRQ_CTRL_BIT_ENABLE <<
378               (ME8200_DI_IRQ_CTRL_SHIFT * instance->di_idx));
379         tmp |=
380             (ME8200_DI_IRQ_CTRL_BIT_CLEAR <<
381              (ME8200_DI_IRQ_CTRL_SHIFT * instance->di_idx));
382 //                      tmp &= ~(ME8200_DI_IRQ_CTRL_BIT_CLEAR << (ME8200_DI_IRQ_CTRL_SHIFT * instance->di_idx));
383         outb(tmp, instance->irq_ctrl_reg);
384         PDEBUG_REG("irq_ctrl_reg outb(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
385                    instance->irq_ctrl_reg - instance->reg_base, tmp);
386         spin_unlock(instance->irq_ctrl_lock);
387
388         instance->rised = -1;
389         instance->status_value = 0;
390         instance->status_value_edges = 0;
391         instance->filtering_flag = 0;
392         spin_unlock_irqrestore(&instance->subdevice_lock, status);
393         wake_up_interruptible_all(&instance->wait_queue);
394
395         ME_SUBDEVICE_EXIT;
396
397         return ME_ERRNO_SUCCESS;
398 }
399
400 static int me8200_di_io_single_config(me_subdevice_t *subdevice,
401                                       struct file *filep,
402                                       int channel,
403                                       int single_config,
404                                       int ref,
405                                       int trig_chan,
406                                       int trig_type, int trig_edge, int flags)
407 {
408         me8200_di_subdevice_t *instance;
409         int err = ME_ERRNO_SUCCESS;
410         unsigned long status;
411
412         PDEBUG("executed.\n");
413
414         instance = (me8200_di_subdevice_t *) subdevice;
415
416         ME_SUBDEVICE_ENTER;
417
418         spin_lock_irqsave(&instance->subdevice_lock, status);
419
420         switch (flags) {
421         case ME_IO_SINGLE_CONFIG_NO_FLAGS:
422         case ME_IO_SINGLE_CONFIG_DIO_BYTE:
423                 if (channel == 0) {
424                         if (single_config == ME_SINGLE_CONFIG_DIO_INPUT) {
425                         } else {
426                                 PERROR("Invalid port direction specified.\n");
427                                 err = ME_ERRNO_INVALID_SINGLE_CONFIG;
428                         }
429                 } else {
430                         PERROR("Invalid channel number.\n");
431                         err = ME_ERRNO_INVALID_CHANNEL;
432                 }
433                 break;
434
435         default:
436                 PERROR("Invalid flags specified.\n");
437                 err = ME_ERRNO_INVALID_FLAGS;
438         }
439
440         spin_unlock_irqrestore(&instance->subdevice_lock, status);
441
442         ME_SUBDEVICE_EXIT;
443
444         return err;
445 }
446
447 static int me8200_di_io_single_read(me_subdevice_t *subdevice,
448                                     struct file *filep,
449                                     int channel,
450                                     int *value, int time_out, int flags)
451 {
452         me8200_di_subdevice_t *instance;
453         int err = ME_ERRNO_SUCCESS;
454         unsigned long status;
455
456         PDEBUG("executed.\n");
457
458         instance = (me8200_di_subdevice_t *) subdevice;
459
460         ME_SUBDEVICE_ENTER;
461
462         spin_lock_irqsave(&instance->subdevice_lock, status);
463
464         switch (flags) {
465         case ME_IO_SINGLE_TYPE_DIO_BIT:
466                 if ((channel >= 0) && (channel < 8)) {
467                         *value = inb(instance->port_reg) & (0x1 << channel);
468                 } else {
469                         PERROR("Invalid bit number specified.\n");
470                         err = ME_ERRNO_INVALID_CHANNEL;
471                 }
472                 break;
473
474         case ME_IO_SINGLE_NO_FLAGS:
475         case ME_IO_SINGLE_TYPE_DIO_BYTE:
476                 if (channel == 0) {
477                         *value = inb(instance->port_reg);
478                 } else {
479                         PERROR("Invalid channel number.\n");
480                         err = ME_ERRNO_INVALID_CHANNEL;
481                 }
482                 break;
483
484         default:
485                 PERROR("Invalid flags specified.\n");
486                 err = ME_ERRNO_INVALID_FLAGS;
487         }
488
489         spin_unlock_irqrestore(&instance->subdevice_lock, status);
490
491         ME_SUBDEVICE_EXIT;
492
493         return err;
494 }
495
496 static int me8200_di_io_reset_subdevice(struct me_subdevice *subdevice,
497                                         struct file *filep, int flags)
498 {
499         me8200_di_subdevice_t *instance = (me8200_di_subdevice_t *) subdevice;
500
501         PDEBUG("executed.\n");
502
503         if (flags) {
504                 PERROR("Invalid flag specified.\n");
505                 return ME_ERRNO_INVALID_FLAGS;
506         }
507
508         instance->count = 0;
509         return me8200_di_io_irq_stop(subdevice, filep, 0, 0);
510 }
511
512 static int me8200_di_query_number_channels(me_subdevice_t *subdevice,
513                                            int *number)
514 {
515         PDEBUG("executed.\n");
516         *number = 8;
517         return ME_ERRNO_SUCCESS;
518 }
519
520 static int me8200_di_query_subdevice_type(me_subdevice_t *subdevice,
521                                           int *type, int *subtype)
522 {
523         PDEBUG("executed.\n");
524         *type = ME_TYPE_DI;
525         *subtype = ME_SUBTYPE_SINGLE;
526         return ME_ERRNO_SUCCESS;
527 }
528
529 static int me8200_di_query_subdevice_caps(me_subdevice_t *subdevice, int *caps)
530 {
531         PDEBUG("executed.\n");
532         *caps =
533             ME_CAPS_DIO_BIT_PATTERN_IRQ |
534             ME_CAPS_DIO_BIT_MASK_IRQ_EDGE_RISING |
535             ME_CAPS_DIO_BIT_MASK_IRQ_EDGE_FALLING |
536             ME_CAPS_DIO_BIT_MASK_IRQ_EDGE_ANY;
537         return ME_ERRNO_SUCCESS;
538 }
539
540 static irqreturn_t me8200_isr(int irq, void *dev_id)
541 {
542         me8200_di_subdevice_t *instance;
543         uint8_t ctrl;
544         uint8_t irq_status;
545         uint8_t line_value = 0;
546         uint8_t line_status = 0;
547         uint32_t status_val = 0;
548
549         instance = (me8200_di_subdevice_t *) dev_id;
550
551         if (irq != instance->irq) {
552                 PERROR("Incorrect interrupt num: %d.\n", irq);
553                 return IRQ_NONE;
554         }
555
556         irq_status = inb(instance->irq_status_reg);
557         if (!irq_status) {
558                 PINFO
559                     ("%ld Shared interrupt. %s(): idx=%d irq_status_reg=0x%04X\n",
560                      jiffies, __func__, instance->di_idx, irq_status);
561                 return IRQ_NONE;
562         }
563
564         PDEBUG("executed.\n");
565
566         spin_lock(&instance->subdevice_lock);
567         spin_lock(instance->irq_ctrl_lock);
568         ctrl = inb(instance->irq_ctrl_reg);
569         ctrl |=
570             ME8200_DI_IRQ_CTRL_BIT_CLEAR << (ME8200_DI_IRQ_CTRL_SHIFT *
571                                              instance->di_idx);
572         outb(ctrl, instance->irq_ctrl_reg);
573         PDEBUG_REG("irq_ctrl_reg outb(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
574                    instance->irq_ctrl_reg - instance->reg_base, ctrl);
575         ctrl &=
576             ~(ME8200_DI_IRQ_CTRL_BIT_CLEAR <<
577               (ME8200_DI_IRQ_CTRL_SHIFT * instance->di_idx));
578         outb(ctrl, instance->irq_ctrl_reg);
579         PDEBUG_REG("irq_ctrl_reg outb(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
580                    instance->irq_ctrl_reg - instance->reg_base, ctrl);
581
582         line_value = inb(instance->port_reg);
583         spin_unlock(instance->irq_ctrl_lock);
584
585         line_status = ((uint8_t) instance->line_value ^ line_value);
586
587         // Make extended information.
588         status_val |= (0x00FF & (~(uint8_t) instance->line_value & line_value)) << 16;  //Raise
589         status_val |= (0x00FF & ((uint8_t) instance->line_value & ~line_value));        //Fall
590
591         instance->line_value = (int)line_value;
592
593         if (instance->rised == 0) {
594                 instance->status_value = irq_status | line_status;
595                 instance->status_value_edges = status_val;
596         } else {
597                 instance->status_value |= irq_status | line_status;
598                 instance->status_value_edges |= status_val;
599         }
600
601         if (instance->filtering_flag) { // For compare mode only.
602                 if (instance->compare_value == instance->line_value) {
603                         instance->rised = 1;
604                         instance->count++;
605                 }
606         } else {
607                 instance->rised = 1;
608                 instance->count++;
609         }
610         spin_unlock(&instance->subdevice_lock);
611
612         spin_unlock(&instance->subdevice_lock);
613
614         wake_up_interruptible_all(&instance->wait_queue);
615
616         return IRQ_HANDLED;
617 }
618
619 static irqreturn_t me8200_isr_EX(int irq, void *dev_id)
620 {
621         me8200_di_subdevice_t *instance;
622         uint8_t irq_status = 0;
623         uint16_t irq_status_EX = 0;
624         uint32_t status_val = 0;
625         int i, j;
626
627         instance = (me8200_di_subdevice_t *) dev_id;
628
629         if (irq != instance->irq) {
630                 PERROR("Incorrect interrupt num: %d.\n", irq);
631                 return IRQ_NONE;
632         }
633
634         PDEBUG("executed.\n");
635
636         //Reset latches. Copy status to extended registers.
637         irq_status = inb(instance->irq_status_reg);
638         PDEBUG_REG("idx=%d irq_status_reg=0x%02X\n", instance->di_idx,
639                    irq_status);
640
641         if (!irq_status) {
642                 PINFO
643                     ("%ld Shared interrupt. %s(): idx=%d irq_status_reg=0x%04X\n",
644                      jiffies, __func__, instance->di_idx, irq_status);
645                 return IRQ_NONE;
646         }
647
648         irq_status_EX = inb(instance->irq_status_low_reg);
649         irq_status_EX |= (inb(instance->irq_status_high_reg) << 8);
650
651         PDEVELOP("EXTENDED REG: 0x%04x\n", irq_status_EX);
652         instance->line_value = inb(instance->port_reg);
653
654         // Format extended information.
655         for (i = 0, j = 0; i < 8; i++, j += 2) {
656                 status_val |= ((0x01 << j) & irq_status_EX) >> (j - i); //Fall
657                 status_val |= ((0x01 << (j + 1)) & irq_status_EX) << (15 - j + i);      //Raise
658         }
659
660         spin_lock(&instance->subdevice_lock);
661         if (instance->rised == 0) {
662                 instance->status_value = irq_status;
663                 instance->status_value_edges = status_val;
664         } else {
665                 instance->status_value |= irq_status;
666                 instance->status_value_edges |= status_val;
667         }
668
669         if (instance->filtering_flag) { // For compare mode only.
670                 if (instance->compare_value == instance->line_value) {
671                         instance->rised = 1;
672                         instance->count++;
673                 }
674         } else {
675                 instance->rised = 1;
676                 instance->count++;
677         }
678         spin_unlock(&instance->subdevice_lock);
679
680         wake_up_interruptible_all(&instance->wait_queue);
681
682         return IRQ_HANDLED;
683 }
684
685 static void me8200_di_destructor(struct me_subdevice *subdevice)
686 {
687         me8200_di_subdevice_t *instance;
688
689         PDEBUG("executed.\n");
690
691         instance = (me8200_di_subdevice_t *) subdevice;
692
693         free_irq(instance->irq, (void *)instance);
694         me_subdevice_deinit(&instance->base);
695         kfree(instance);
696 }
697
698 me8200_di_subdevice_t *me8200_di_constructor(uint32_t me8200_regbase,
699                                              unsigned int di_idx,
700                                              int irq,
701                                              spinlock_t *irq_ctrl_lock,
702                                              spinlock_t *irq_mode_lock)
703 {
704         me8200_di_subdevice_t *subdevice;
705         int err;
706
707         PDEBUG("executed.\n");
708
709         /* Allocate memory for subdevice instance */
710         subdevice = kmalloc(sizeof(me8200_di_subdevice_t), GFP_KERNEL);
711
712         if (!subdevice) {
713                 PERROR("Cannot get memory for subdevice instance.\n");
714                 return NULL;
715         }
716
717         memset(subdevice, 0, sizeof(me8200_di_subdevice_t));
718
719         /* Initialize subdevice base class */
720         err = me_subdevice_init(&subdevice->base);
721
722         if (err) {
723                 PERROR("Cannot initialize subdevice base class instance.\n");
724                 kfree(subdevice);
725                 return NULL;
726         }
727         // Check firmware version.
728         me8200_di_check_version(subdevice,
729                                 me8200_regbase + ME8200_FIRMWARE_VERSION_REG);
730
731         // Initialize spin locks.
732         spin_lock_init(&subdevice->subdevice_lock);
733
734         subdevice->irq_ctrl_lock = irq_ctrl_lock;
735         subdevice->irq_mode_lock = irq_mode_lock;
736
737         /* Save the subdevice index. */
738         subdevice->di_idx = di_idx;
739
740         /* Initialize registers */
741         if (di_idx == 0) {
742                 subdevice->port_reg = me8200_regbase + ME8200_DI_PORT_0_REG;
743                 subdevice->mask_reg = me8200_regbase + ME8200_DI_MASK_0_REG;
744                 subdevice->compare_reg =
745                     me8200_regbase + ME8200_DI_COMPARE_0_REG;
746                 subdevice->irq_status_reg =
747                     me8200_regbase + ME8200_DI_CHANGE_0_REG;
748
749                 subdevice->irq_status_low_reg =
750                     me8200_regbase + ME8200_DI_EXTEND_CHANGE_0_LOW_REG;
751                 subdevice->irq_status_high_reg =
752                     me8200_regbase + ME8200_DI_EXTEND_CHANGE_0_HIGH_REG;
753         } else if (di_idx == 1) {
754                 subdevice->port_reg = me8200_regbase + ME8200_DI_PORT_1_REG;
755                 subdevice->mask_reg = me8200_regbase + ME8200_DI_MASK_1_REG;
756                 subdevice->compare_reg =
757                     me8200_regbase + ME8200_DI_COMPARE_1_REG;
758                 subdevice->irq_status_reg =
759                     me8200_regbase + ME8200_DI_CHANGE_1_REG;
760
761                 subdevice->irq_status_low_reg =
762                     me8200_regbase + ME8200_DI_EXTEND_CHANGE_1_LOW_REG;
763                 subdevice->irq_status_high_reg =
764                     me8200_regbase + ME8200_DI_EXTEND_CHANGE_1_HIGH_REG;
765         } else {
766                 PERROR("Wrong subdevice idx=%d.\n", di_idx);
767                 kfree(subdevice);
768                 return NULL;
769         }
770         subdevice->irq_ctrl_reg = me8200_regbase + ME8200_DI_IRQ_CTRL_REG;
771         subdevice->irq_mode_reg = me8200_regbase + ME8200_IRQ_MODE_REG;
772 #ifdef MEDEBUG_DEBUG_REG
773         subdevice->reg_base = me8200_regbase;
774 #endif
775
776         /* Initialize wait queue */
777         init_waitqueue_head(&subdevice->wait_queue);
778
779         /* Overload base class methods. */
780         subdevice->base.me_subdevice_io_irq_start = me8200_di_io_irq_start;
781         subdevice->base.me_subdevice_io_irq_wait = me8200_di_io_irq_wait;
782         subdevice->base.me_subdevice_io_irq_stop = me8200_di_io_irq_stop;
783         subdevice->base.me_subdevice_io_reset_subdevice =
784             me8200_di_io_reset_subdevice;
785         subdevice->base.me_subdevice_io_single_config =
786             me8200_di_io_single_config;
787         subdevice->base.me_subdevice_io_single_read = me8200_di_io_single_read;
788         subdevice->base.me_subdevice_query_number_channels =
789             me8200_di_query_number_channels;
790         subdevice->base.me_subdevice_query_subdevice_type =
791             me8200_di_query_subdevice_type;
792         subdevice->base.me_subdevice_query_subdevice_caps =
793             me8200_di_query_subdevice_caps;
794         subdevice->base.me_subdevice_destructor = me8200_di_destructor;
795
796         subdevice->rised = 0;
797         subdevice->count = 0;
798
799         /* Register interrupt service routine. */
800         subdevice->irq = irq;
801         if (subdevice->version > 0) {   // NEW
802                 err = request_irq(subdevice->irq, me8200_isr_EX,
803                                   IRQF_DISABLED | IRQF_SHARED,
804                                   ME8200_NAME, (void *)subdevice);
805         } else {                //OLD
806                 err = request_irq(subdevice->irq, me8200_isr,
807                                   IRQF_DISABLED | IRQF_SHARED,
808                                   ME8200_NAME, (void *)subdevice);
809         }
810
811         if (err) {
812                 PERROR("Cannot initialize subdevice base class instance.\n");
813                 kfree(subdevice);
814                 return NULL;
815         }
816         PDEBUG("Registred irq=%d.\n", subdevice->irq);
817
818         return subdevice;
819 }
820
821 static void me8200_di_check_version(me8200_di_subdevice_t *instance,
822                                     unsigned long addr)
823 {
824
825         PDEBUG("executed.\n");
826         instance->version = 0x000000FF & inb(addr);
827         PDEVELOP("me8200 firmware version: %d\n", instance->version);
828
829         /// @note Fix for wrong values in this registry.
830         if ((instance->version < 0x7) || (instance->version > 0x1F))
831                 instance->version = 0x0;
832 }