1/*********************************************************************
2 *
3 * Filename:      litelink.c
4 * Version:       1.1
5 * Description:   Driver for the Parallax LiteLink dongle
6 * Status:        Stable
7 * Author:        Dag Brattli <dagb@cs.uit.no>
8 * Created at:    Fri May  7 12:50:33 1999
9 * Modified at:   Fri Dec 17 09:14:23 1999
10 * Modified by:   Dag Brattli <dagb@cs.uit.no>
11 *
12 *     Copyright (c) 1999 Dag Brattli, All Rights Reserved.
13 *
14 *     This program is free software; you can redistribute it and/or
15 *     modify it under the terms of the GNU General Public License as
16 *     published by the Free Software Foundation; either version 2 of
17 *     the License, or (at your option) any later version.
18 *
19 *     This program is distributed in the hope that it will be useful,
20 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
21 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 *     GNU General Public License for more details.
23 *
24 *     You should have received a copy of the GNU General Public License
25 *     along with this program; if not, write to the Free Software
26 *     Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27 *     MA 02111-1307 USA
28 *
29 ********************************************************************/
30
31#include <linux/module.h>
32#include <linux/delay.h>
33#include <linux/tty.h>
34#include <linux/init.h>
35
36#include <net/irda/irda.h>
37#include <net/irda/irda_device.h>
38
39#define MIN_DELAY 25      /* 15 us, but wait a little more to be sure */
40#define MAX_DELAY 10000   /* 1 ms */
41
42static void litelink_open(dongle_t *self, struct qos_info *qos);
43static void litelink_close(dongle_t *self);
44static int  litelink_change_speed(struct irda_task *task);
45static int  litelink_reset(struct irda_task *task);
46
47/* These are the baudrates supported */
48static __u32 baud_rates[] = { 115200, 57600, 38400, 19200, 9600 };
49
50static struct dongle_reg dongle = {
51	.type = IRDA_LITELINK_DONGLE,
52	.open = litelink_open,
53	.close = litelink_close,
54	.reset = litelink_reset,
55	.change_speed = litelink_change_speed,
56	.owner = THIS_MODULE,
57};
58
59static int __init litelink_init(void)
60{
61	return irda_device_register_dongle(&dongle);
62}
63
64static void __exit litelink_cleanup(void)
65{
66	irda_device_unregister_dongle(&dongle);
67}
68
69static void litelink_open(dongle_t *self, struct qos_info *qos)
70{
71	qos->baud_rate.bits &= IR_9600|IR_19200|IR_38400|IR_57600|IR_115200;
72	qos->min_turn_time.bits = 0x7f; /* Needs 0.01 ms */
73}
74
75static void litelink_close(dongle_t *self)
76{
77	/* Power off dongle */
78	self->set_dtr_rts(self->dev, FALSE, FALSE);
79}
80
81/*
82 * Function litelink_change_speed (task)
83 *
84 *    Change speed of the Litelink dongle. To cycle through the available
85 *    baud rates, pulse RTS low for a few ms.
86 */
87static int litelink_change_speed(struct irda_task *task)
88{
89	dongle_t *self = (dongle_t *) task->instance;
90	__u32 speed = (__u32) task->param;
91        int i;
92
93	/* Clear RTS to reset dongle */
94	self->set_dtr_rts(self->dev, TRUE, FALSE);
95
96	/* Sleep a minimum of 15 us */
97	udelay(MIN_DELAY);
98
99	/* Go back to normal mode */
100	self->set_dtr_rts(self->dev, TRUE, TRUE);
101
102	/* Sleep a minimum of 15 us */
103	udelay(MIN_DELAY);
104
105	/* Cycle through avaiable baudrates until we reach the correct one */
106	for (i=0; i<5 && baud_rates[i] != speed; i++) {
107		/* Set DTR, clear RTS */
108		self->set_dtr_rts(self->dev, FALSE, TRUE);
109
110		/* Sleep a minimum of 15 us */
111		udelay(MIN_DELAY);
112
113		/* Set DTR, Set RTS */
114		self->set_dtr_rts(self->dev, TRUE, TRUE);
115
116		/* Sleep a minimum of 15 us */
117		udelay(MIN_DELAY);
118        }
119	irda_task_next_state(task, IRDA_TASK_DONE);
120
121	return 0;
122}
123
124/*
125 * Function litelink_reset (task)
126 *
127 *      Reset the Litelink type dongle.
128 *
129 */
130static int litelink_reset(struct irda_task *task)
131{
132	dongle_t *self = (dongle_t *) task->instance;
133
134	/* Power on dongle */
135	self->set_dtr_rts(self->dev, TRUE, TRUE);
136
137	/* Sleep a minimum of 15 us */
138	udelay(MIN_DELAY);
139
140	/* Clear RTS to reset dongle */
141	self->set_dtr_rts(self->dev, TRUE, FALSE);
142
143	/* Sleep a minimum of 15 us */
144	udelay(MIN_DELAY);
145
146	/* Go back to normal mode */
147	self->set_dtr_rts(self->dev, TRUE, TRUE);
148
149	/* Sleep a minimum of 15 us */
150	udelay(MIN_DELAY);
151
152	/* This dongles speed defaults to 115200 bps */
153	self->speed = 115200;
154
155	irda_task_next_state(task, IRDA_TASK_DONE);
156
157	return 0;
158}
159
160MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
161MODULE_DESCRIPTION("Parallax Litelink dongle driver");
162MODULE_LICENSE("GPL");
163MODULE_ALIAS("irda-dongle-5"); /* IRDA_LITELINK_DONGLE */
164
165/*
166 * Function init_module (void)
167 *
168 *    Initialize Litelink module
169 *
170 */
171module_init(litelink_init);
172
173/*
174 * Function cleanup_module (void)
175 *
176 *    Cleanup Litelink module
177 *
178 */
179module_exit(litelink_cleanup);
180