1/*********************************************************************
2 *
3 * Filename:      actisys.c
4 * Version:       1.0
5 * Description:   Implementation for the ACTiSYS IR-220L and IR-220L+
6 *                dongles
7 * Status:        Beta.
8 * Authors:       Dag Brattli <dagb@cs.uit.no> (initially)
9 *		  Jean Tourrilhes <jt@hpl.hp.com> (new version)
10 * Created at:    Wed Oct 21 20:02:35 1998
11 * Modified at:   Fri Dec 17 09:10:43 1999
12 * Modified by:   Dag Brattli <dagb@cs.uit.no>
13 *
14 *     Copyright (c) 1998-1999 Dag Brattli, All Rights Reserved.
15 *     Copyright (c) 1999 Jean Tourrilhes
16 *
17 *     This program is free software; you can redistribute it and/or
18 *     modify it under the terms of the GNU General Public License as
19 *     published by the Free Software Foundation; either version 2 of
20 *     the License, or (at your option) any later version.
21 *
22 *     Neither Dag Brattli nor University of Troms� admit liability nor
23 *     provide warranty for any of this software. This material is
24 *     provided "AS-IS" and at no charge.
25 *
26 ********************************************************************/
27
28/*
29 * Changelog
30 *
31 * 0.8 -> 0.9999 - Jean
32 *	o New initialisation procedure : much safer and correct
33 *	o New procedure the change speed : much faster and simpler
34 *	o Other cleanups & comments
35 *	Thanks to Lichen Wang @ Actisys for his excellent help...
36 */
37
38#include <linux/module.h>
39#include <linux/delay.h>
40#include <linux/tty.h>
41#include <linux/init.h>
42
43#include <net/irda/irda.h>
44#include <net/irda/irda_device.h>
45
46/*
47 * Define the timing of the pulses we send to the dongle (to reset it, and
48 * to toggle speeds). Basically, the limit here is the propagation speed of
49 * the signals through the serial port, the dongle being much faster.  Any
50 * serial port support 115 kb/s, so we are sure that pulses 8.5 us wide can
51 * go through cleanly . If you are on the wild side, you can try to lower
52 * this value (Actisys recommended me 2 us, and 0 us work for me on a P233!)
53 */
54#define MIN_DELAY 10	/* 10 us to be on the conservative side */
55
56static int  actisys_change_speed(struct irda_task *task);
57static int  actisys_reset(struct irda_task *task);
58static void actisys_open(dongle_t *self, struct qos_info *qos);
59static void actisys_close(dongle_t *self);
60
61/* These are the baudrates supported, in the order available */
62/* Note : the 220L doesn't support 38400, but we will fix that below */
63static __u32 baud_rates[] = { 9600, 19200, 57600, 115200, 38400 };
64#define MAX_SPEEDS 5
65
66static struct dongle_reg dongle = {
67	.type = IRDA_ACTISYS_DONGLE,
68	.open = actisys_open,
69	.close = actisys_close,
70	.reset = actisys_reset,
71	.change_speed = actisys_change_speed,
72	.owner = THIS_MODULE,
73};
74
75static struct dongle_reg dongle_plus = {
76	.type = IRDA_ACTISYS_PLUS_DONGLE,
77	.open = actisys_open,
78	.close = actisys_close,
79	.reset = actisys_reset,
80	.change_speed = actisys_change_speed,
81	.owner = THIS_MODULE,
82};
83
84/*
85 * Function actisys_change_speed (task)
86 *
87 *	There is two model of Actisys dongle we are dealing with,
88 * the 220L and 220L+. At this point, only irattach knows with
89 * kind the user has requested (it was an argument on irattach
90 * command line).
91 *	So, we register a dongle of each sort and let irattach
92 * pick the right one...
93 */
94static int __init actisys_init(void)
95{
96	int ret;
97
98	/* First, register an Actisys 220L dongle */
99	ret = irda_device_register_dongle(&dongle);
100	if (ret < 0)
101		return ret;
102	/* Now, register an Actisys 220L+ dongle */
103	ret = irda_device_register_dongle(&dongle_plus);
104	if (ret < 0) {
105		irda_device_unregister_dongle(&dongle);
106		return ret;
107	}
108	return 0;
109}
110
111static void __exit actisys_cleanup(void)
112{
113	/* We have to remove both dongles */
114	irda_device_unregister_dongle(&dongle);
115	irda_device_unregister_dongle(&dongle_plus);
116}
117
118static void actisys_open(dongle_t *self, struct qos_info *qos)
119{
120	/* Power on the dongle */
121	self->set_dtr_rts(self->dev, TRUE, TRUE);
122
123	/* Set the speeds we can accept */
124	qos->baud_rate.bits &= IR_9600|IR_19200|IR_38400|IR_57600|IR_115200;
125
126	/* Remove support for 38400 if this is not a 220L+ dongle */
127	if (self->issue->type == IRDA_ACTISYS_DONGLE)
128		qos->baud_rate.bits &= ~IR_38400;
129
130	qos->min_turn_time.bits = 0x7f; /* Needs 0.01 ms */
131}
132
133static void actisys_close(dongle_t *self)
134{
135	/* Power off the dongle */
136	self->set_dtr_rts(self->dev, FALSE, FALSE);
137}
138
139/*
140 * Function actisys_change_speed (task)
141 *
142 *    Change speed of the ACTiSYS IR-220L and IR-220L+ type IrDA dongles.
143 *    To cycle through the available baud rates, pulse RTS low for a few us.
144 *
145 *	First, we reset the dongle to always start from a known state.
146 *	Then, we cycle through the speeds by pulsing RTS low and then up.
147 *	The dongle allow us to pulse quite fast, se we can set speed in one go,
148 * which is must faster ( < 100 us) and less complex than what is found
149 * in some other dongle drivers...
150 *	Note that even if the new speed is the same as the current speed,
151 * we reassert the speed. This make sure that things are all right,
152 * and it's fast anyway...
153 *	By the way, this function will work for both type of dongles,
154 * because the additional speed is at the end of the sequence...
155 */
156static int actisys_change_speed(struct irda_task *task)
157{
158	dongle_t *self = (dongle_t *) task->instance;
159	__u32 speed = (__u32) task->param;	/* Target speed */
160	int ret = 0;
161	int i = 0;
162
163        IRDA_DEBUG(4, "%s(), speed=%d (was %d)\n", __FUNCTION__, speed,
164		   self->speed);
165
166	/* Go to a known state by reseting the dongle */
167
168	/* Reset the dongle : set DTR low for 10 us */
169	self->set_dtr_rts(self->dev, FALSE, TRUE);
170	udelay(MIN_DELAY);
171
172	/* Go back to normal mode (we are now at 9600 b/s) */
173	self->set_dtr_rts(self->dev, TRUE, TRUE);
174
175	/*
176	 * Now, we can set the speed requested. Send RTS pulses until we
177         * reach the target speed
178	 */
179	for (i=0; i<MAX_SPEEDS; i++) {
180		if (speed == baud_rates[i]) {
181			self->speed = baud_rates[i];
182			break;
183		}
184		/* Make sure previous pulse is finished */
185		udelay(MIN_DELAY);
186
187		/* Set RTS low for 10 us */
188		self->set_dtr_rts(self->dev, TRUE, FALSE);
189		udelay(MIN_DELAY);
190
191		/* Set RTS high for 10 us */
192		self->set_dtr_rts(self->dev, TRUE, TRUE);
193	}
194
195	/* Check if life is sweet... */
196	if (i >= MAX_SPEEDS)
197		ret = -1;  /* This should not happen */
198
199	/* Basta lavoro, on se casse d'ici... */
200	irda_task_next_state(task, IRDA_TASK_DONE);
201
202	return ret;
203}
204
205/*
206 * Function actisys_reset (task)
207 *
208 *      Reset the Actisys type dongle. Warning, this function must only be
209 *      called with a process context!
210 *
211 * We need to do two things in this function :
212 *	o first make sure that the dongle is in a state where it can operate
213 *	o second put the dongle in a know state
214 *
215 *	The dongle is powered of the RTS and DTR lines. In the dongle, there
216 * is a big capacitor to accommodate the current spikes. This capacitor
217 * takes a least 50 ms to be charged. In theory, the Bios set those lines
218 * up, so by the time we arrive here we should be set. It doesn't hurt
219 * to be on the conservative side, so we will wait...
220 *	Then, we set the speed to 9600 b/s to get in a known state (see in
221 * change_speed for details). It is needed because the IrDA stack
222 * has tried to set the speed immediately after our first return,
223 * so before we can be sure the dongle is up and running.
224 */
225static int actisys_reset(struct irda_task *task)
226{
227	dongle_t *self = (dongle_t *) task->instance;
228	int ret = 0;
229
230	IRDA_ASSERT(task != NULL, return -1;);
231
232	self->reset_task = task;
233
234	switch (task->state) {
235	case IRDA_TASK_INIT:
236		/* Set both DTR & RTS to power up the dongle */
237		/* In theory redundant with power up in actisys_open() */
238		self->set_dtr_rts(self->dev, TRUE, TRUE);
239
240		/* Sleep 50 ms to make sure capacitor is charged */
241		ret = msecs_to_jiffies(50);
242		irda_task_next_state(task, IRDA_TASK_WAIT);
243		break;
244	case IRDA_TASK_WAIT:
245		/* Reset the dongle : set DTR low for 10 us */
246		self->set_dtr_rts(self->dev, FALSE, TRUE);
247		udelay(MIN_DELAY);
248
249		/* Go back to normal mode */
250		self->set_dtr_rts(self->dev, TRUE, TRUE);
251
252		irda_task_next_state(task, IRDA_TASK_DONE);
253		self->reset_task = NULL;
254		self->speed = 9600;	/* That's the default */
255		break;
256	default:
257		IRDA_ERROR("%s(), unknown state %d\n",
258			   __FUNCTION__, task->state);
259		irda_task_next_state(task, IRDA_TASK_DONE);
260		self->reset_task = NULL;
261		ret = -1;
262		break;
263	}
264	return ret;
265}
266
267MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no> - Jean Tourrilhes <jt@hpl.hp.com>");
268MODULE_DESCRIPTION("ACTiSYS IR-220L and IR-220L+ dongle driver");
269MODULE_LICENSE("GPL");
270MODULE_ALIAS("irda-dongle-2"); /* IRDA_ACTISYS_DONGLE */
271MODULE_ALIAS("irda-dongle-3"); /* IRDA_ACTISYS_PLUS_DONGLE */
272
273
274/*
275 * Function init_module (void)
276 *
277 *    Initialize Actisys module
278 *
279 */
280module_init(actisys_init);
281
282/*
283 * Function cleanup_module (void)
284 *
285 *    Cleanup Actisys module
286 *
287 */
288module_exit(actisys_cleanup);
289