File: i2c-philips-par.c

package info (click to toggle)
i2c 2.6.3-5
  • links: PTS
  • area: main
  • in suites: woody
  • size: 592 kB
  • ctags: 1,051
  • sloc: ansic: 6,419; perl: 637; makefile: 186
file content (309 lines) | stat: -rw-r--r-- 7,720 bytes parent folder | download
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/* ------------------------------------------------------------------------- */
/* i2c-philips-par.c i2c-hw access for philips style parallel port adapters  */
/* ------------------------------------------------------------------------- */
/*   Copyright (C) 1995-2000 Simon G. Vogl

    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 of the License, 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.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.		     */
/* ------------------------------------------------------------------------- */ 

/* With some changes from Kysti Mlkki <kmalkki@cc.hut.fi> and even
   Frodo Looijaard <frodol@dds.nl> */

/* $Id: i2c-philips-par.c,v 1.21 2001/10/14 15:04:46 mds Exp $ */

#include <linux/kernel.h>
#include <linux/ioport.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/stddef.h>
#include <linux/parport.h>
#include "i2c.h"
#include "i2c-algo-bit.h"

#ifndef __exit
#define __exit __init
#endif

static int type;

struct i2c_par
{
	struct pardevice *pdev;
	struct i2c_adapter adapter;
	struct i2c_algo_bit_data bit_lp_data;
	struct i2c_par *next;
};

static struct i2c_par *adapter_list;


/* ----- global defines -----------------------------------------------	*/
#define DEB(x)		/* should be reasonable open, close &c. 	*/
#define DEB2(x) 	/* low level debugging - very slow 		*/
#define DEBE(x)	x	/* error messages 				*/

/* ----- printer port defines ------------------------------------------*/
					/* Pin Port  Inverted	name	*/
#define I2C_ON		0x20		/* 12 status N	paper		*/
					/* ... only for phil. not used  */
#define I2C_SDA		0x80		/*  9 data   N	data7		*/
#define I2C_SCL		0x08		/* 17 ctrl   N	dsel		*/

#define I2C_SDAIN	0x80		/* 11 stat   Y	busy		*/
#define I2C_SCLIN	0x08		/* 15 stat   Y	enable		*/

#define I2C_DMASK	0x7f
#define I2C_CMASK	0xf7

/* ----- local functions ----------------------------------------------	*/

static void bit_lp_setscl(void *data, int state)
{
	/*be cautious about state of the control register - 
		touch only the one bit needed*/
	if (state) {
		parport_write_control((struct parport *) data,
		      parport_read_control((struct parport *) data)|I2C_SCL);
	} else {
		parport_write_control((struct parport *) data,
		      parport_read_control((struct parport *) data)&I2C_CMASK);
	}
}

static void bit_lp_setsda(void *data, int state)
{
	if (state) {
		parport_write_data((struct parport *) data, I2C_DMASK);
	} else {
		parport_write_data((struct parport *) data, I2C_SDA);
	}
}

static int bit_lp_getscl(void *data)
{
	return parport_read_status((struct parport *) data) & I2C_SCLIN;
}

static int bit_lp_getsda(void *data)
{
	return parport_read_status((struct parport *) data) & I2C_SDAIN;
}

static void bit_lp_setscl2(void *data, int state)
{
	if (state) {
		parport_write_data((struct parport *) data,
		      parport_read_data((struct parport *) data)|0x1);
	} else {
		parport_write_data((struct parport *) data,
		      parport_read_data((struct parport *) data)&0xfe);
	}
}

static void bit_lp_setsda2(void *data, int state)
{
	if (state) {
		parport_write_data((struct parport *) data,
		      parport_read_data((struct parport *) data)|0x2);
	} else {
		parport_write_data((struct parport *) data,
		      parport_read_data((struct parport *) data)&0xfd);
	}
}

static int bit_lp_getsda2(void *data)
{
	return (parport_read_status((struct parport *) data) & 
			             PARPORT_STATUS_BUSY) ? 0 : 1;
}

static int bit_lp_reg(struct i2c_client *client)
{
	return 0;
}

static int bit_lp_unreg(struct i2c_client *client)
{
	return 0;
}

static void bit_lp_inc_use(struct i2c_adapter *adap)
{
	MOD_INC_USE_COUNT;
}

static void bit_lp_dec_use(struct i2c_adapter *adap)
{
	MOD_DEC_USE_COUNT;
}

/* ------------------------------------------------------------------------
 * Encapsulate the above functions in the correct operations structure.
 * This is only done when more than one hardware adapter is supported.
 */
 
static struct i2c_algo_bit_data bit_lp_data = {
	NULL,
	bit_lp_setsda,
	bit_lp_setscl,
	bit_lp_getsda,
	bit_lp_getscl,
	80, 80, 100,		/*	waits, timeout */
}; 

static struct i2c_algo_bit_data bit_lp_data2 = {
	NULL,
	bit_lp_setsda2,
	bit_lp_setscl2,
	bit_lp_getsda2,
	NULL,
	80, 80, 100,		/*	waits, timeout */
}; 

static struct i2c_adapter bit_lp_ops = {
	"Philips Parallel port adapter",
	I2C_HW_B_LP,
	NULL,
	NULL,
	bit_lp_inc_use,
	bit_lp_dec_use,
	bit_lp_reg,

	bit_lp_unreg,
};

static void i2c_parport_attach (struct parport *port)
{
	struct i2c_par *adapter = kmalloc(sizeof(struct i2c_par),
					  GFP_KERNEL);
	if (!adapter) {
		printk("i2c-philips-par: Unable to malloc.\n");
		return;
	}

	printk("i2c-philips-par.o: attaching to %s\n", port->name);

	adapter->pdev = parport_register_device(port, "i2c-philips-par",
						NULL, NULL, NULL, 
						PARPORT_FLAG_EXCL,
						NULL);
	if (!adapter->pdev) {
		printk("i2c-philips-par: Unable to register with parport.\n");
		return;
	}

	adapter->adapter = bit_lp_ops;
	adapter->adapter.algo_data = &adapter->bit_lp_data;
	adapter->bit_lp_data = type ? bit_lp_data2 : bit_lp_data;
	adapter->bit_lp_data.data = port;

	/* reset hardware to sane state */
	parport_claim_or_block(adapter->pdev);
	bit_lp_setsda(port, 1);
	bit_lp_setscl(port, 1);
	parport_release(adapter->pdev);

	if (i2c_bit_add_bus(&adapter->adapter) < 0)
	{
		printk("i2c-philips-par: Unable to register with I2C.\n");
		parport_unregister_device(adapter->pdev);
		kfree(adapter);
		return;		/* No good */
	}

	adapter->next = adapter_list;
	adapter_list = adapter;
}

static void i2c_parport_detach (struct parport *port)
{
	struct i2c_par *adapter, *prev = NULL;

	for (adapter = adapter_list; adapter; adapter = adapter->next)
	{
		if (adapter->pdev->port == port)
		{
			parport_unregister_device(adapter->pdev);
			i2c_bit_del_bus(&adapter->adapter);
			if (prev)
				prev->next = adapter->next;
			else
				adapter_list = adapter->next;
			kfree(adapter);
			return;
		}
		prev = adapter;
	}
}


#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,4)
static struct parport_driver i2c_driver = {
	"i2c-philips-par",
	i2c_parport_attach,
	i2c_parport_detach,
	NULL
};
#endif

int __init i2c_bitlp_init(void)
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,3,4)
	struct parport *port;
#endif
	printk("i2c-philips-par.o: i2c Philips parallel port adapter module version %s (%s)\n", I2C_VERSION, I2C_DATE);

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,4)
	parport_register_driver(&i2c_driver);
#else
	for (port = parport_enumerate(); port; port=port->next)
		i2c_parport_attach(port);
#endif
	
	return 0;
}

void __exit i2c_bitlp_exit(void)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,4)
	parport_unregister_driver(&i2c_driver);
#else
	struct parport *port;
	for (port = parport_enumerate(); port; port=port->next)
		i2c_parport_detach(port);
#endif
}

EXPORT_NO_SYMBOLS;

MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
MODULE_DESCRIPTION("I2C-Bus adapter routines for Philips parallel port adapter");
#ifdef MODULE_LICENSE
MODULE_LICENSE("GPL");
#endif

MODULE_PARM(type, "i");

#ifdef MODULE
int init_module(void)
{
	return i2c_bitlp_init();
}

void cleanup_module(void)
{
	i2c_bitlp_exit();
}
#endif