aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/irda/ma600.c
blob: f5e6836667fd2214baf427a22940157b23ed0c5f (plain)
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*********************************************************************
 *                
 * Filename:      ma600.c
 * Version:       0.1
 * Description:   Implementation of the MA600 dongle
 * Status:        Experimental.
 * Author:        Leung <95Etwl@alumni.ee.ust.hk> http://www.engsvr.ust/~eetwl95
 * Created at:    Sat Jun 10 20:02:35 2000
 * Modified at:   
 * Modified by:   
 *
 * Note: very thanks to Mr. Maru Wang <maru@mobileaction.com.tw> for providing 
 *       information on the MA600 dongle
 * 
 *     Copyright (c) 2000 Leung, All Rights Reserved.
 *      
 *     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., 59 Temple Place, Suite 330, Boston, 
 *     MA 02111-1307 USA
 *     
 ********************************************************************/

/* define this macro for release version */
//#define NDEBUG

#include <linux/module.h>
#include <linux/delay.h>
#include <linux/tty.h>
#include <linux/init.h>

#include <net/irda/irda.h>
#include <net/irda/irda_device.h>

#ifndef NDEBUG
	#undef IRDA_DEBUG
	#define IRDA_DEBUG(n, args...) (printk(KERN_DEBUG args))

	#undef ASSERT
	#define ASSERT(expr, func) \
	if(!(expr)) { \
	        printk( "Assertion failed! %s,%s,%s,line=%d\n",\
        	#expr,__FILE__,__FUNCTION__,__LINE__); \
	        func}
#endif

/* convert hex value to ascii hex */
static const char hexTbl[] = "0123456789ABCDEF";


static void ma600_open(dongle_t *self, struct qos_info *qos);
static void ma600_close(dongle_t *self);
static int  ma600_change_speed(struct irda_task *task);
static int  ma600_reset(struct irda_task *task);

/* control byte for MA600 */
#define MA600_9600	0x00
#define MA600_19200	0x01
#define MA600_38400	0x02
#define MA600_57600	0x03
#define MA600_115200	0x04
#define MA600_DEV_ID1	0x05
#define MA600_DEV_ID2	0x06
#define MA600_2400	0x08

static struct dongle_reg dongle = {
	.type = IRDA_MA600_DONGLE,
	.open = ma600_open,
	.close = ma600_close,
	.reset = ma600_reset,
	.change_speed = ma600_change_speed,
	.owner = THIS_MODULE,
};

static int __init ma600_init(void)
{
	IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
	return irda_device_register_dongle(&dongle);
}

static void __exit ma600_cleanup(void)
{
	IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
	irda_device_unregister_dongle(&dongle);
}

/*
	Power on:
		(0) Clear RTS and DTR for 1 second
		(1) Set RTS and DTR for 1 second
		(2) 9600 bps now
	Note: assume RTS, DTR are clear before
*/
static void ma600_open(dongle_t *self, struct qos_info *qos)
{
	IRDA_DEBUG(2, "%s()\n", __FUNCTION__);

	qos->baud_rate.bits &= IR_2400|IR_9600|IR_19200|IR_38400
				|IR_57600|IR_115200;
	qos->min_turn_time.bits = 0x01;		/* Needs at least 1 ms */	
	irda_qos_bits_to_value(qos);

	//self->set_dtr_rts(self->dev, FALSE, FALSE);
	// should wait 1 second

	self->set_dtr_rts(self->dev, TRUE, TRUE);
	// should wait 1 second
}

static void ma600_close(dongle_t *self)
{
	IRDA_DEBUG(2, "%s()\n", __FUNCTION__);

	/* Power off dongle */
	self->set_dtr_rts(self->dev, FALSE, FALSE);
}

static __u8 get_control_byte(__u32 speed)
{
	__u8 byte;

	switch (speed) {
	default:
	case 115200:
		byte = MA600_115200;
		break;
	case 57600:
		byte = MA600_57600;
		break;
	case 38400:
		byte = MA600_38400;
		break;
	case 19200:
		byte = MA600_19200;
		break;
	case 9600:
		byte = MA600_9600;
		break;
	case 2400:
		byte = MA600_2400;
		break;
	}

	return byte;
}

/*
 * Function ma600_change_speed (dev, state, speed)
 *
 *    Set the speed for the MA600 type dongle. Warning, this 
 *    function must be called with a process context!
 *
 *    Algorithm
 *    1. Reset
 *    2. clear RTS, set DTR and wait for 1ms
 *    3. send Control Byte to the MA600 through TXD to set new baud rate
 *       wait until the stop bit of Control Byte is sent (for 9600 baud rate, 
 *       it takes about 10 msec)
 *    4. set RTS, set DTR (return to NORMAL Operation)
 *    5. wait at least 10 ms, new setting (baud rate, etc) takes effect here 
 *       after
 */
static int ma600_change_speed(struct irda_task *task)
{
	dongle_t *self = (dongle_t *) task->instance;
	__u32 speed = (__u32) task->param;
	static __u8 byte;
	__u8 byte_echo;
	int ret = 0;
	
	IRDA_DEBUG(2, "%s()\n", __FUNCTION__);

	ASSERT(task != NULL, return -1;);

	if (self->speed_task && self->speed_task != task) {
		IRDA_DEBUG(0, "%s(), busy!\n", __FUNCTION__);
		return msecs_to_jiffies(10);
	} else {
		self->speed_task = task;
	}

	switch (task->state) {
	case IRDA_TASK_INIT:
	case IRDA_TASK_CHILD_INIT:
		/* 
		 * Need to reset the dongle and go to 9600 bps before
                 * programming 
		 */
		if (irda_task_execute(self, ma600_reset, NULL, task, 
				      (void *) speed)) {
			/* Dongle need more time to reset */
			irda_task_next_state(task, IRDA_TASK_CHILD_WAIT);
	
			/* give 1 second to finish */
			ret = msecs_to_jiffies(1000);
		} else {
			irda_task_next_state(task, IRDA_TASK_CHILD_DONE);
		}
		break;

	case IRDA_TASK_CHILD_WAIT:
		IRDA_WARNING("%s(), resetting dongle timed out!\n",
			     __FUNCTION__);
		ret = -1;
		break;

	case IRDA_TASK_CHILD_DONE:
		/* Set DTR, Clear RTS */
		self->set_dtr_rts(self->dev, TRUE, FALSE);
	
		ret = msecs_to_jiffies(1);		/* Sleep 1 ms */
		irda_task_next_state(task, IRDA_TASK_WAIT);
		break;

	case IRDA_TASK_WAIT:
		speed = (__u32) task->param;
		byte = get_control_byte(speed);

		/* Write control byte */
		self->write(self->dev, &byte, sizeof(byte));
		
		irda_task_next_state(task, IRDA_TASK_WAIT1);

		/* Wait at least 10 ms */
		ret = msecs_to_jiffies(15);
		break;

	case IRDA_TASK_WAIT1:
		/* Read control byte echo */
		self->read(self->dev, &byte_echo, sizeof(byte_echo));

		if(byte != byte_echo) {
			/* if control byte != echo, I don't know what to do */
			printk(KERN_WARNING "%s() control byte written != read!\n", __FUNCTION__);
			printk(KERN_WARNING "control byte = 0x%c%c\n", 
			       hexTbl[(byte>>4)&0x0f], hexTbl[byte&0x0f]);
			printk(KERN_WARNING "byte echo = 0x%c%c\n", 
			       hexTbl[(byte_echo>>4) & 0x0f], 
			       hexTbl[byte_echo & 0x0f]);
		#ifndef NDEBUG
		} else {
			IRDA_DEBUG(2, "%s() control byte write read OK\n", __FUNCTION__);
		#endif
		}

		/* Set DTR, Set RTS */
		self->set_dtr_rts(self->dev, TRUE, TRUE);

		irda_task_next_state(task, IRDA_TASK_WAIT2);

		/* Wait at least 10 ms */
		ret = msecs_to_jiffies(10);
		break;

	case IRDA_TASK_WAIT2:
		irda_task_next_state(task, IRDA_TASK_DONE);
		self->speed_task = NULL;
		break;

	default:
		IRDA_ERROR("%s(), unknown state %d\n",
			   __FUNCTION__, task->state);
		irda_task_next_state(task, IRDA_TASK_DONE);
		self->speed_task = NULL;
		ret = -1;
		break;
	}
	return ret;
}

/*
 * Function ma600_reset (driver)
 *
 *      This function resets the ma600 dongle. Warning, this function 
 *      must be called with a process context!! 
 *
 *      Algorithm:
 *    	  0. DTR=0, RTS=1 and wait 10 ms
 *    	  1. DTR=1, RTS=1 and wait 10 ms
 *        2. 9600 bps now
 */
int ma600_reset(struct irda_task *task)
{
	dongle_t *self = (dongle_t *) task->instance;
	int ret = 0;

	IRDA_DEBUG(2, "%s()\n", __FUNCTION__);

	ASSERT(task != NULL, return -1;);

	if (self->reset_task && self->reset_task != task) {
		IRDA_DEBUG(0, "%s(), busy!\n", __FUNCTION__);
		return msecs_to_jiffies(10);
	} else
		self->reset_task = task;
	
	switch (task->state) {
	case IRDA_TASK_INIT:
		/* Clear DTR and Set RTS */
		self->set_dtr_rts(self->dev, FALSE, TRUE);
		irda_task_next_state(task, IRDA_TASK_WAIT1);
		ret = msecs_to_jiffies(10);		/* Sleep 10 ms */
		break;
	case IRDA_TASK_WAIT1:
		/* Set DTR and RTS */
		self->set_dtr_rts(self->dev, TRUE, TRUE);
		irda_task_next_state(task, IRDA_TASK_WAIT2);
		ret = msecs_to_jiffies(10);		/* Sleep 10 ms */
		break;
	case IRDA_TASK_WAIT2:
		irda_task_next_state(task, IRDA_TASK_DONE);
		self->reset_task = NULL;
		break;
	default:
		IRDA_ERROR("%s(), unknown state %d\n",
			   __FUNCTION__, task->state);
		irda_task_next_state(task, IRDA_TASK_DONE);		
		self->reset_task = NULL;
		ret = -1;
	}
	return ret;
}

MODULE_AUTHOR("Leung <95Etwl@alumni.ee.ust.hk> http://www.engsvr.ust/~eetwl95");
MODULE_DESCRIPTION("MA600 dongle driver version 0.1");
MODULE_LICENSE("GPL");
MODULE_ALIAS("irda-dongle-11"); /* IRDA_MA600_DONGLE */
		
/*
 * Function init_module (void)
 *
 *    Initialize MA600 module
 *
 */
module_init(ma600_init);

/*
 * Function cleanup_module (void)
 *
 *    Cleanup MA600 module
 *
 */
module_exit(ma600_cleanup);