blob: a9a81358477bcafc817b16497312ab29b850b760 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*********************************************************************
2 *
3 * Filename: ma600.c
4 * Version: 0.1
5 * Description: Implementation of the MA600 dongle
6 * Status: Experimental.
7 * Author: Leung <95Etwl@alumni.ee.ust.hk> http://www.engsvr.ust/~eetwl95
8 * Created at: Sat Jun 10 20:02:35 2000
9 * Modified at: Sat Aug 16 09:34:13 2003
10 * Modified by: Martin Diehl <mad@mdiehl.de> (modified for new sir_dev)
11 *
12 * Note: very thanks to Mr. Maru Wang <maru@mobileaction.com.tw> for providing
13 * information on the MA600 dongle
14 *
15 * Copyright (c) 2000 Leung, All Rights Reserved.
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 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
Jeff Kirshere8478de2013-12-06 06:28:44 -080028 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 *
30 ********************************************************************/
31
32#include <linux/module.h>
33#include <linux/delay.h>
34#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#include <net/irda/irda.h>
37
38#include "sir-dev.h"
39
40static int ma600_open(struct sir_dev *);
41static int ma600_close(struct sir_dev *);
42static int ma600_change_speed(struct sir_dev *, unsigned);
43static int ma600_reset(struct sir_dev *);
44
45/* control byte for MA600 */
46#define MA600_9600 0x00
47#define MA600_19200 0x01
48#define MA600_38400 0x02
49#define MA600_57600 0x03
50#define MA600_115200 0x04
51#define MA600_DEV_ID1 0x05
52#define MA600_DEV_ID2 0x06
53#define MA600_2400 0x08
54
55static struct dongle_driver ma600 = {
56 .owner = THIS_MODULE,
57 .driver_name = "MA600",
58 .type = IRDA_MA600_DONGLE,
59 .open = ma600_open,
60 .close = ma600_close,
61 .reset = ma600_reset,
62 .set_speed = ma600_change_speed,
63};
64
65
66static int __init ma600_sir_init(void)
67{
Harvey Harrisona97a6f12008-07-30 17:20:18 -070068 IRDA_DEBUG(2, "%s()\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 return irda_register_dongle(&ma600);
70}
71
72static void __exit ma600_sir_cleanup(void)
73{
Harvey Harrisona97a6f12008-07-30 17:20:18 -070074 IRDA_DEBUG(2, "%s()\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 irda_unregister_dongle(&ma600);
76}
77
78/*
79 Power on:
80 (0) Clear RTS and DTR for 1 second
81 (1) Set RTS and DTR for 1 second
82 (2) 9600 bps now
83 Note: assume RTS, DTR are clear before
84*/
85static int ma600_open(struct sir_dev *dev)
86{
87 struct qos_info *qos = &dev->qos;
88
Harvey Harrisona97a6f12008-07-30 17:20:18 -070089 IRDA_DEBUG(2, "%s()\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91 sirdev_set_dtr_rts(dev, TRUE, TRUE);
92
93 /* Explicitly set the speeds we can accept */
94 qos->baud_rate.bits &= IR_2400|IR_9600|IR_19200|IR_38400
95 |IR_57600|IR_115200;
96 /* Hm, 0x01 means 10ms - for >= 1ms we would need 0x07 */
97 qos->min_turn_time.bits = 0x01; /* Needs at least 1 ms */
98 irda_qos_bits_to_value(qos);
99
100 /* irda thread waits 50 msec for power settling */
101
102 return 0;
103}
104
105static int ma600_close(struct sir_dev *dev)
106{
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700107 IRDA_DEBUG(2, "%s()\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 /* Power off dongle */
110 sirdev_set_dtr_rts(dev, FALSE, FALSE);
111
112 return 0;
113}
114
115static __u8 get_control_byte(__u32 speed)
116{
117 __u8 byte;
118
119 switch (speed) {
120 default:
121 case 115200:
122 byte = MA600_115200;
123 break;
124 case 57600:
125 byte = MA600_57600;
126 break;
127 case 38400:
128 byte = MA600_38400;
129 break;
130 case 19200:
131 byte = MA600_19200;
132 break;
133 case 9600:
134 byte = MA600_9600;
135 break;
136 case 2400:
137 byte = MA600_2400;
138 break;
139 }
140
141 return byte;
142}
143
144/*
145 * Function ma600_change_speed (dev, speed)
146 *
147 * Set the speed for the MA600 type dongle.
148 *
149 * The dongle has already been reset to a known state (dongle default)
150 * We cycle through speeds by pulsing RTS low and then high.
151 */
152
153/*
154 * Function ma600_change_speed (dev, speed)
155 *
156 * Set the speed for the MA600 type dongle.
157 *
158 * Algorithm
159 * 1. Reset (already done by irda thread state machine)
160 * 2. clear RTS, set DTR and wait for 1ms
161 * 3. send Control Byte to the MA600 through TXD to set new baud rate
162 * wait until the stop bit of Control Byte is sent (for 9600 baud rate,
163 * it takes about 10 msec)
164 * 4. set RTS, set DTR (return to NORMAL Operation)
165 * 5. wait at least 10 ms, new setting (baud rate, etc) takes effect here
166 * after
167 */
168
169/* total delays are only about 20ms - let's just sleep for now to
170 * avoid the state machine complexity before we get things working
171 */
172
173static int ma600_change_speed(struct sir_dev *dev, unsigned speed)
174{
175 u8 byte;
176
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700177 IRDA_DEBUG(2, "%s(), speed=%d (was %d)\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 speed, dev->speed);
179
180 /* dongle already reset, dongle and port at default speed (9600) */
181
182 /* Set RTS low for 1 ms */
183 sirdev_set_dtr_rts(dev, TRUE, FALSE);
184 mdelay(1);
185
186 /* Write control byte */
187 byte = get_control_byte(speed);
188 sirdev_raw_write(dev, &byte, sizeof(byte));
189
190 /* Wait at least 10ms: fake wait_until_sent - 10 bits at 9600 baud*/
191 msleep(15); /* old ma600 uses 15ms */
192
193#if 1
194 /* read-back of the control byte. ma600 is the first dongle driver
195 * which uses this so there might be some unidentified issues.
196 * Disable this in case of problems with readback.
197 */
198
199 sirdev_raw_read(dev, &byte, sizeof(byte));
200 if (byte != get_control_byte(speed)) {
201 IRDA_WARNING("%s(): bad control byte read-back %02x != %02x\n",
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700202 __func__, (unsigned) byte,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 (unsigned) get_control_byte(speed));
204 return -1;
205 }
206 else
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700207 IRDA_DEBUG(2, "%s() control byte write read OK\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208#endif
209
210 /* Set DTR, Set RTS */
211 sirdev_set_dtr_rts(dev, TRUE, TRUE);
212
213 /* Wait at least 10ms */
214 msleep(10);
215
216 /* dongle is now switched to the new speed */
217 dev->speed = speed;
218
219 return 0;
220}
221
222/*
223 * Function ma600_reset (dev)
224 *
225 * This function resets the ma600 dongle.
226 *
227 * Algorithm:
228 * 0. DTR=0, RTS=1 and wait 10 ms
229 * 1. DTR=1, RTS=1 and wait 10 ms
230 * 2. 9600 bps now
231 */
232
233/* total delays are only about 20ms - let's just sleep for now to
234 * avoid the state machine complexity before we get things working
235 */
236
Hannes Eder0e49e642008-12-26 00:03:19 -0800237static int ma600_reset(struct sir_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700239 IRDA_DEBUG(2, "%s()\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 /* Reset the dongle : set DTR low for 10 ms */
242 sirdev_set_dtr_rts(dev, FALSE, TRUE);
243 msleep(10);
244
245 /* Go back to normal mode */
246 sirdev_set_dtr_rts(dev, TRUE, TRUE);
247 msleep(10);
248
249 dev->speed = 9600; /* That's the dongle-default */
250
251 return 0;
252}
253
254MODULE_AUTHOR("Leung <95Etwl@alumni.ee.ust.hk> http://www.engsvr.ust/~eetwl95");
255MODULE_DESCRIPTION("MA600 dongle driver version 0.1");
256MODULE_LICENSE("GPL");
257MODULE_ALIAS("irda-dongle-11"); /* IRDA_MA600_DONGLE */
258
259module_init(ma600_sir_init);
260module_exit(ma600_sir_cleanup);
261