Merge branch 'for-2.6.31' of git://git.linux-nfs.org/projects/trondmy/nfs-2.6
[pandora-kernel.git] / drivers / staging / meilhaus / me8200_do.c
1 /**
2  * @file me8200_do.c
3  *
4  * @brief ME-8200 digital output 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 /*
33  * Includes
34  */
35 #include <linux/module.h>
36
37 #include <linux/slab.h>
38 #include <linux/spinlock.h>
39 #include <linux/interrupt.h>
40 #include <linux/io.h>
41 #include <linux/types.h>
42
43 #include "medefines.h"
44 #include "meinternal.h"
45 #include "meerror.h"
46
47 #include "meids.h"
48 #include "medebug.h"
49 #include "me8200_reg.h"
50 #include "me8200_do_reg.h"
51 #include "me8200_do.h"
52
53 /*
54  * Defines
55  */
56
57 /*
58  * Functions
59  */
60
61 static int me8200_do_io_irq_start(me_subdevice_t *subdevice,
62                                   struct file *filep,
63                                   int channel,
64                                   int irq_source,
65                                   int irq_edge, int irq_arg, int flags)
66 {
67         me8200_do_subdevice_t *instance;
68         int err = ME_ERRNO_SUCCESS;
69         uint8_t tmp;
70         unsigned long status;
71
72         if (flags & ~ME_IO_IRQ_START_DIO_BYTE) {
73                 PERROR("Invalid flag specified.\n");
74                 return ME_ERRNO_INVALID_FLAGS;
75         }
76
77         if (channel != 0) {
78                 PERROR("Invalid channel specified.\n");
79                 return ME_ERRNO_INVALID_CHANNEL;
80         }
81
82         if (irq_source != ME_IRQ_SOURCE_DIO_OVER_TEMP) {
83                 PERROR("Invalid interrupt source specified.\n");
84                 return ME_ERRNO_INVALID_IRQ_SOURCE;
85         }
86
87         PDEBUG("executed.\n");
88
89         instance = (me8200_do_subdevice_t *) subdevice;
90
91         ME_SUBDEVICE_ENTER;
92
93         spin_lock_irqsave(&instance->subdevice_lock, status);
94         spin_lock(instance->irq_mode_lock);
95         tmp = inb(instance->irq_ctrl_reg);
96         tmp |=
97             ME8200_IRQ_MODE_BIT_ENABLE_POWER << (ME8200_IRQ_MODE_POWER_SHIFT *
98                                                  instance->do_idx);
99         outb(tmp, instance->irq_ctrl_reg);
100         PDEBUG_REG("irq_ctrl_reg outb(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
101                    instance->irq_ctrl_reg - instance->reg_base, tmp);
102         spin_unlock(instance->irq_mode_lock);
103         instance->rised = 0;
104         spin_unlock_irqrestore(&instance->subdevice_lock, status);
105
106         ME_SUBDEVICE_EXIT;
107
108         return err;
109 }
110
111 static int me8200_do_io_irq_wait(me_subdevice_t *subdevice,
112                                  struct file *filep,
113                                  int channel,
114                                  int *irq_count,
115                                  int *value, int time_out, int flags)
116 {
117         me8200_do_subdevice_t *instance;
118         int err = ME_ERRNO_SUCCESS;
119         long t = 0;
120         unsigned long cpu_flags;
121
122         PDEBUG("executed.\n");
123
124         instance = (me8200_do_subdevice_t *) subdevice;
125
126         if (flags) {
127                 PERROR("Invalid flag specified.\n");
128                 return ME_ERRNO_INVALID_FLAGS;
129         }
130
131         if (time_out < 0) {
132                 PERROR("Invalid time_out specified.\n");
133                 return ME_ERRNO_INVALID_TIMEOUT;
134         }
135
136         if (time_out) {
137                 t = (time_out * HZ) / 1000;
138
139                 if (t == 0)
140                         t = 1;
141         }
142
143         ME_SUBDEVICE_ENTER;
144
145         if (instance->rised <= 0) {
146                 instance->rised = 0;
147
148                 if (time_out) {
149                         t = wait_event_interruptible_timeout(instance->
150                                                              wait_queue,
151                                                              (instance->rised !=
152                                                               0), t);
153
154                         if (t == 0) {
155                                 PERROR
156                                     ("Wait on external interrupt timed out.\n");
157                                 err = ME_ERRNO_TIMEOUT;
158                         }
159                 } else {
160                         wait_event_interruptible(instance->wait_queue,
161                                                  (instance->rised != 0));
162                 }
163
164                 if (instance->rised < 0) {
165                         PERROR("Wait on interrupt aborted by user.\n");
166                         err = ME_ERRNO_CANCELLED;
167                 }
168         }
169
170         if (signal_pending(current)) {
171                 PERROR("Wait on external interrupt aborted by signal.\n");
172                 err = ME_ERRNO_SIGNAL;
173         }
174
175         spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
176         instance->rised = 0;
177         *irq_count = instance->count;
178         *value = 0;
179         spin_unlock_irqrestore(&instance->subdevice_lock, cpu_flags);
180
181         ME_SUBDEVICE_EXIT;
182
183         return err;
184 }
185
186 static int me8200_do_io_irq_stop(me_subdevice_t *subdevice,
187                                  struct file *filep, int channel, int flags)
188 {
189         me8200_do_subdevice_t *instance;
190         uint8_t tmp;
191         unsigned long cpu_flags;
192
193         PDEBUG("executed.\n");
194
195         instance = (me8200_do_subdevice_t *) subdevice;
196
197         if (flags) {
198                 PERROR("Invalid flag specified.\n");
199                 return ME_ERRNO_INVALID_FLAGS;
200         }
201
202         ME_SUBDEVICE_ENTER;
203
204         spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
205         spin_lock(instance->irq_mode_lock);
206         tmp = inb(instance->irq_ctrl_reg);
207         tmp &=
208             ~(ME8200_IRQ_MODE_BIT_ENABLE_POWER <<
209               (ME8200_IRQ_MODE_POWER_SHIFT * instance->do_idx));
210         outb(tmp, instance->irq_ctrl_reg);
211         PDEBUG_REG("irq_ctrl_reg outb(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
212                    instance->irq_ctrl_reg - instance->reg_base, tmp);
213         spin_unlock(instance->irq_mode_lock);
214         instance->rised = -1;
215         spin_unlock_irqrestore(&instance->subdevice_lock, cpu_flags);
216         wake_up_interruptible_all(&instance->wait_queue);
217
218         ME_SUBDEVICE_EXIT;
219
220         return ME_ERRNO_SUCCESS;
221 }
222
223 static int me8200_do_io_reset_subdevice(struct me_subdevice *subdevice,
224                                         struct file *filep, int flags)
225 {
226         me8200_do_subdevice_t *instance;
227         unsigned long cpu_flags;
228         uint8_t tmp;
229
230         PDEBUG("executed.\n");
231
232         instance = (me8200_do_subdevice_t *) subdevice;
233
234         if (flags) {
235                 PERROR("Invalid flag specified.\n");
236                 return ME_ERRNO_INVALID_FLAGS;
237         }
238
239         ME_SUBDEVICE_ENTER;
240
241         spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
242         outb(0x00, instance->port_reg);
243         PDEBUG_REG("port_reg outb(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
244                    instance->port_reg - instance->reg_base, 0x00);
245         spin_lock(instance->irq_mode_lock);
246         tmp = inb(instance->irq_ctrl_reg);
247         tmp &=
248             ~(ME8200_IRQ_MODE_BIT_ENABLE_POWER <<
249               (ME8200_IRQ_MODE_POWER_SHIFT * instance->do_idx));
250         outb(tmp, instance->irq_ctrl_reg);
251         PDEBUG_REG("irq_ctrl_reg outb(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
252                    instance->irq_ctrl_reg - instance->reg_base, tmp);
253         spin_unlock(instance->irq_mode_lock);
254         instance->rised = -1;
255         instance->count = 0;
256         spin_unlock_irqrestore(&instance->subdevice_lock, cpu_flags);
257         wake_up_interruptible_all(&instance->wait_queue);
258
259         ME_SUBDEVICE_EXIT;
260
261         return ME_ERRNO_SUCCESS;
262 }
263
264 static int me8200_do_io_single_config(me_subdevice_t *subdevice,
265                                       struct file *filep,
266                                       int channel,
267                                       int single_config,
268                                       int ref,
269                                       int trig_chan,
270                                       int trig_type, int trig_edge, int flags)
271 {
272         me8200_do_subdevice_t *instance;
273         int err = ME_ERRNO_SUCCESS;
274         unsigned long status;
275
276         PDEBUG("executed.\n");
277
278         instance = (me8200_do_subdevice_t *) subdevice;
279
280         ME_SUBDEVICE_ENTER;
281
282         spin_lock_irqsave(&instance->subdevice_lock, status);
283         switch (flags) {
284         case ME_IO_SINGLE_CONFIG_NO_FLAGS:
285         case ME_IO_SINGLE_CONFIG_DIO_BYTE:
286                 if (channel == 0) {
287                         if (single_config == ME_SINGLE_CONFIG_DIO_OUTPUT) {
288                         } else {
289                                 PERROR("Invalid byte direction specified.\n");
290                                 err = ME_ERRNO_INVALID_SINGLE_CONFIG;
291                         }
292                 } else {
293                         PERROR("Invalid byte specified.\n");
294                         err = ME_ERRNO_INVALID_CHANNEL;
295                 }
296                 break;
297
298         default:
299                 PERROR("Invalid flags specified.\n");
300                 err = ME_ERRNO_INVALID_FLAGS;
301         }
302         spin_unlock_irqrestore(&instance->subdevice_lock, status);
303
304         ME_SUBDEVICE_EXIT;
305
306         return err;
307 }
308
309 static int me8200_do_io_single_read(me_subdevice_t *subdevice,
310                                     struct file *filep,
311                                     int channel,
312                                     int *value, int time_out, int flags)
313 {
314         me8200_do_subdevice_t *instance;
315         int err = ME_ERRNO_SUCCESS;
316         unsigned long status;
317
318         PDEBUG("executed.\n");
319
320         instance = (me8200_do_subdevice_t *) subdevice;
321
322         ME_SUBDEVICE_ENTER;
323
324         spin_lock_irqsave(&instance->subdevice_lock, status);
325         switch (flags) {
326         case ME_IO_SINGLE_TYPE_DIO_BIT:
327                 if ((channel >= 0) && (channel < 8)) {
328                         *value = inb(instance->port_reg) & (0x1 << channel);
329                 } else {
330                         PERROR("Invalid bit number specified.\n");
331                         err = ME_ERRNO_INVALID_CHANNEL;
332                 }
333                 break;
334
335         case ME_IO_SINGLE_NO_FLAGS:
336         case ME_IO_SINGLE_TYPE_DIO_BYTE:
337                 if (channel == 0) {
338                         *value = inb(instance->port_reg);
339                 } else {
340                         PERROR("Invalid byte number specified.\n");
341                         err = ME_ERRNO_INVALID_CHANNEL;
342                 }
343                 break;
344
345         default:
346                 PERROR("Invalid flags specified.\n");
347                 err = ME_ERRNO_INVALID_FLAGS;
348         }
349         spin_unlock_irqrestore(&instance->subdevice_lock, status);
350
351         ME_SUBDEVICE_EXIT;
352
353         return err;
354 }
355
356 static int me8200_do_io_single_write(me_subdevice_t *subdevice,
357                                      struct file *filep,
358                                      int channel,
359                                      int value, int time_out, int flags)
360 {
361         me8200_do_subdevice_t *instance;
362         int err = ME_ERRNO_SUCCESS;
363         uint8_t state;
364         unsigned long status;
365
366         PDEBUG("executed.\n");
367
368         instance = (me8200_do_subdevice_t *) subdevice;
369
370         ME_SUBDEVICE_ENTER;
371
372         spin_lock_irqsave(&instance->subdevice_lock, status);
373         switch (flags) {
374         case ME_IO_SINGLE_TYPE_DIO_BIT:
375                 if ((channel >= 0) && (channel < 8)) {
376                         state = inb(instance->port_reg);
377                         state =
378                             value ? (state | (0x1 << channel)) : (state &
379                                                                   ~(0x1 <<
380                                                                     channel));
381                         outb(state, instance->port_reg);
382                         PDEBUG_REG("port_reg outb(0x%lX+0x%lX)=0x%x\n",
383                                    instance->reg_base,
384                                    instance->port_reg - instance->reg_base,
385                                    state);
386                 } else {
387                         PERROR("Invalid bit number specified.\n");
388                         err = ME_ERRNO_INVALID_CHANNEL;
389                 }
390                 break;
391
392         case ME_IO_SINGLE_NO_FLAGS:
393         case ME_IO_SINGLE_TYPE_DIO_BYTE:
394                 if (channel == 0) {
395                         outb(value, instance->port_reg);
396                         PDEBUG_REG("port_reg outb(0x%lX+0x%lX)=0x%x\n",
397                                    instance->reg_base,
398                                    instance->port_reg - instance->reg_base,
399                                    value);
400                 } else {
401                         PERROR("Invalid byte number specified.\n");
402                         err = ME_ERRNO_INVALID_CHANNEL;
403                 }
404                 break;
405
406         default:
407                 PERROR("Invalid flags specified.\n");
408                 err = ME_ERRNO_INVALID_FLAGS;
409         }
410         spin_unlock_irqrestore(&instance->subdevice_lock, status);
411
412         ME_SUBDEVICE_EXIT;
413
414         return err;
415 }
416
417 static int me8200_do_query_number_channels(me_subdevice_t *subdevice,
418                                            int *number)
419 {
420         PDEBUG("executed.\n");
421         *number = 8;
422         return ME_ERRNO_SUCCESS;
423 }
424
425 static int me8200_do_query_subdevice_type(me_subdevice_t *subdevice,
426                                           int *type, int *subtype)
427 {
428         PDEBUG("executed.\n");
429         *type = ME_TYPE_DO;
430         *subtype = ME_SUBTYPE_SINGLE;
431         return ME_ERRNO_SUCCESS;
432 }
433
434 static int me8200_do_query_subdevice_caps(me_subdevice_t *subdevice, int *caps)
435 {
436         PDEBUG("executed.\n");
437         *caps = ME_CAPS_DIO_OVER_TEMP_IRQ;
438         return ME_ERRNO_SUCCESS;
439 }
440
441 static void me8200_do_destructor(struct me_subdevice *subdevice)
442 {
443         me8200_do_subdevice_t *instance;
444
445         PDEBUG("executed.\n");
446
447         instance = (me8200_do_subdevice_t *) subdevice;
448
449         free_irq(instance->irq, (void *)instance);
450         me_subdevice_deinit(&instance->base);
451         kfree(instance);
452 }
453
454 static irqreturn_t me8200_do_isr(int irq, void *dev_id)
455 {
456         me8200_do_subdevice_t *instance;
457         uint16_t ctrl;
458         uint8_t irq_status;
459
460         instance = (me8200_do_subdevice_t *) dev_id;
461
462         if (irq != instance->irq) {
463                 PERROR("Incorrect interrupt num: %d.\n", irq);
464                 return IRQ_NONE;
465         }
466
467         irq_status = inb(instance->irq_status_reg);
468         if (!
469             (irq_status &
470              (ME8200_DO_IRQ_STATUS_BIT_ACTIVE << instance->do_idx))) {
471                 PINFO
472                     ("%ld Shared interrupt. %s(): idx=%d irq_status_reg=0x%04X\n",
473                      jiffies, __func__, instance->do_idx, irq_status);
474                 return IRQ_NONE;
475         }
476
477         PDEBUG("executed.\n");
478
479         spin_lock(&instance->subdevice_lock);
480         instance->rised = 1;
481         instance->count++;
482
483         spin_lock(instance->irq_mode_lock);
484         ctrl = inw(instance->irq_ctrl_reg);
485         ctrl |= ME8200_IRQ_MODE_BIT_CLEAR_POWER << instance->do_idx;
486         outw(ctrl, instance->irq_ctrl_reg);
487         PDEBUG_REG("irq_ctrl_reg outw(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
488                    instance->irq_ctrl_reg - instance->reg_base, ctrl);
489         ctrl &= ~(ME8200_IRQ_MODE_BIT_CLEAR_POWER << instance->do_idx);
490         outw(ctrl, instance->irq_ctrl_reg);
491         PDEBUG_REG("irq_ctrl_reg outw(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
492                    instance->irq_ctrl_reg - instance->reg_base, ctrl);
493         spin_unlock(instance->irq_mode_lock);
494         spin_unlock(&instance->subdevice_lock);
495         wake_up_interruptible_all(&instance->wait_queue);
496
497         return IRQ_HANDLED;
498 }
499
500 me8200_do_subdevice_t *me8200_do_constructor(uint32_t reg_base,
501                                              unsigned int do_idx,
502                                              int irq,
503                                              spinlock_t *irq_mode_lock)
504 {
505         me8200_do_subdevice_t *subdevice;
506         int err;
507
508         PDEBUG("executed.\n");
509
510         /* Allocate memory for subdevice instance */
511         subdevice = kmalloc(sizeof(me8200_do_subdevice_t), GFP_KERNEL);
512
513         if (!subdevice) {
514                 PERROR("Cannot get memory for subdevice instance.\n");
515                 return NULL;
516         }
517
518         memset(subdevice, 0, sizeof(me8200_do_subdevice_t));
519
520         /* Initialize subdevice base class */
521         err = me_subdevice_init(&subdevice->base);
522
523         if (err) {
524                 PERROR("Cannot initialize subdevice base class instance.\n");
525                 kfree(subdevice);
526                 return NULL;
527         }
528         // Initialize spin locks.
529         spin_lock_init(&subdevice->subdevice_lock);
530
531         subdevice->irq_mode_lock = irq_mode_lock;
532
533         /* Save the index of the digital output */
534         subdevice->do_idx = do_idx;
535         subdevice->irq = irq;
536
537         /* Initialize the registers */
538         if (do_idx == 0) {
539                 subdevice->port_reg = reg_base + ME8200_DO_PORT_0_REG;
540         } else if (do_idx == 1) {
541                 subdevice->port_reg = reg_base + ME8200_DO_PORT_1_REG;
542         } else {
543                 PERROR("Wrong subdevice idx=%d.\n", do_idx);
544                 kfree(subdevice);
545                 return NULL;
546         }
547         subdevice->irq_ctrl_reg = reg_base + ME8200_IRQ_MODE_REG;
548         subdevice->irq_status_reg = reg_base + ME8200_DO_IRQ_STATUS_REG;
549 #ifdef MEDEBUG_DEBUG_REG
550         subdevice->reg_base = reg_base;
551 #endif
552
553         /* Initialize the wait queue */
554         init_waitqueue_head(&subdevice->wait_queue);
555
556         /* Request the interrupt line */
557         err = request_irq(irq, me8200_do_isr,
558                           IRQF_DISABLED | IRQF_SHARED,
559                           ME8200_NAME, (void *)subdevice);
560
561         if (err) {
562                 PERROR("Cannot get interrupt line.\n");
563                 kfree(subdevice);
564                 return NULL;
565         }
566         PINFO("Registered irq=%d.\n", irq);
567
568         /* Overload base class methods. */
569         subdevice->base.me_subdevice_io_irq_start = me8200_do_io_irq_start;
570         subdevice->base.me_subdevice_io_irq_wait = me8200_do_io_irq_wait;
571         subdevice->base.me_subdevice_io_irq_stop = me8200_do_io_irq_stop;
572         subdevice->base.me_subdevice_io_reset_subdevice =
573             me8200_do_io_reset_subdevice;
574         subdevice->base.me_subdevice_io_single_config =
575             me8200_do_io_single_config;
576         subdevice->base.me_subdevice_io_single_read = me8200_do_io_single_read;
577         subdevice->base.me_subdevice_io_single_write =
578             me8200_do_io_single_write;
579         subdevice->base.me_subdevice_query_number_channels =
580             me8200_do_query_number_channels;
581         subdevice->base.me_subdevice_query_subdevice_type =
582             me8200_do_query_subdevice_type;
583         subdevice->base.me_subdevice_query_subdevice_caps =
584             me8200_do_query_subdevice_caps;
585         subdevice->base.me_subdevice_destructor = me8200_do_destructor;
586
587         subdevice->rised = 0;
588         subdevice->count = 0;
589
590         return subdevice;
591 }