aboutsummaryrefslogtreecommitdiffstats
path: root/ubuntu/omnibook/pio.c
blob: 312811efdf791aa286316b16942a38bf024305be (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * pio.c -- low level functions I/O ports
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * Written by Soós Péter <sp@osb.hu>, 2002-2004
 * Modified by Mathieu Bérard <mathieu.berard@crans.org>, 2006
 */

#include "omnibook.h"

#include <linux/types.h>
#include <linux/delay.h>
#include <linux/sched.h>
#include <linux/spinlock.h>
#include <linux/ioport.h>

#include <asm/io.h>
#include "hardware.h"

/*
 * IO port backend. Only support single or dual ports operations
 * private data structure: it's the linked list of requested ports
 * 
 * Race condition issue: omnibook_pio_init/exit functions are only called from
 * omnibook_backend_match and omnibook_remove from init.c, this should happen
 * only at module init/exit time so there is no need for a lock.
 */

struct pio_priv_data_t {
	unsigned long addr;
	struct kref refcount;
	struct list_head list;
};

static struct pio_priv_data_t pio_priv_data = {
	.addr = 0,
	.list = LIST_HEAD_INIT(pio_priv_data.list),
};

/*
 * Match an entry in the linked list helper function: see if we have and entry
 * whose addr field match maddr
 */
static struct pio_priv_data_t *omnibook_match_port(struct pio_priv_data_t *data,
						      unsigned long maddr)
{
	struct pio_priv_data_t *cursor;

	list_for_each_entry(cursor, &data->list, list) {
		if (cursor->addr == maddr) {
			return cursor;
		}
	}
	return NULL;
}

/*
 * See if we have to request raddr
 */
static int omnibook_claim_port(struct pio_priv_data_t *data, unsigned long raddr)
{
	struct pio_priv_data_t *match, *new;

	match = omnibook_match_port(data, raddr);
	if (match) {
		/* Already requested by us: increment kref and quit */
		kref_get(&match->refcount);
		return 0;
	}

	/* there was no match: request the region and add to list */
	if (!request_region(raddr, 1, OMNIBOOK_MODULE_NAME)) {
		printk(O_ERR "Request I/O port error\n");
		return -ENODEV;
	}

	new = kmalloc(sizeof(struct pio_priv_data_t), GFP_KERNEL);
	if (!new) {
		release_region(raddr, 1);
		return -ENOMEM;
	}

	kref_init(&new->refcount);
	new->addr = raddr;
	list_add(&new->list, &data->list);

	return 0;
}

/*
 * Register read_addr and write_addr
 */
static int omnibook_pio_init(const struct omnibook_operation *io_op)
{
	int retval = 0;

	if (io_op->read_addr
	    && (retval = omnibook_claim_port(io_op->backend->data, io_op->read_addr)))
		goto out;

	if (io_op->write_addr && (io_op->write_addr != io_op->read_addr))
		retval = omnibook_claim_port(io_op->backend->data, io_op->write_addr);

      out:
	return retval;
}

/*
 * REALLY release a port
 */
static void omnibook_free_port(struct kref *ref)
{
	struct pio_priv_data_t *data;

	data = container_of(ref, struct pio_priv_data_t, refcount);
	release_region(data->addr, 1);
	list_del(&data->list);
	kfree(data);
}

/*
 * Unregister read_addr and write_addr
 */
static void omnibook_pio_exit(const struct omnibook_operation *io_op)
{
	struct pio_priv_data_t *match;

	match = omnibook_match_port(io_op->backend->data, io_op->read_addr);
	if (match)
		kref_put(&match->refcount, omnibook_free_port);

	match = omnibook_match_port(io_op->backend->data, io_op->write_addr);
	if (match)
		kref_put(&match->refcount, omnibook_free_port);

}

static int omnibook_io_read(const struct omnibook_operation *io_op, u8 * value)
{
	*value = inb(io_op->read_addr);
	if (io_op->read_mask)
		*value &= io_op->read_mask;
	return 0;
}

static int omnibook_io_write(const struct omnibook_operation *io_op, u8 value)
{
	outb(io_op->write_addr, value);
	return 0;
}

/*
 * Backend interface declarations
 */
struct omnibook_backend pio_backend = {
	.name = "pio",
	.data = &pio_priv_data,
	.init = omnibook_pio_init,
	.exit = omnibook_pio_exit,
	.byte_read = omnibook_io_read,
	.byte_write = omnibook_io_write,
};

/* End of file */