MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / net / irda / mcp2120.c
blobd170df507b6ff59fe3008bf895ef39d4255b2159
1 /*********************************************************************
2 *
3 *
4 * Filename: mcp2120.c
5 * Version: 1.0
6 * Description: Implementation for the MCP2120 (Microchip)
7 * Status: Experimental.
8 * Author: Felix Tang (tangf@eyetap.org)
9 * Created at: Sun Mar 31 19:32:12 EST 2002
10 * Based on code by: Dag Brattli <dagb@cs.uit.no>
12 * Copyright (c) 2002 Felix Tang, All Rights Reserved.
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.
19 ********************************************************************/
21 #include <linux/module.h>
22 #include <linux/delay.h>
23 #include <linux/tty.h>
24 #include <linux/init.h>
26 #include <net/irda/irda.h>
27 #include <net/irda/irda_device.h>
29 static int mcp2120_reset(struct irda_task *task);
30 static void mcp2120_open(dongle_t *self, struct qos_info *qos);
31 static void mcp2120_close(dongle_t *self);
32 static int mcp2120_change_speed(struct irda_task *task);
34 #define MCP2120_9600 0x87
35 #define MCP2120_19200 0x8B
36 #define MCP2120_38400 0x85
37 #define MCP2120_57600 0x83
38 #define MCP2120_115200 0x81
40 #define MCP2120_COMMIT 0x11
42 static struct dongle_reg dongle = {
43 .type = IRDA_MCP2120_DONGLE,
44 .open = mcp2120_open,
45 .close = mcp2120_close,
46 .reset = mcp2120_reset,
47 .change_speed = mcp2120_change_speed,
48 .owner = THIS_MODULE,
51 static int __init mcp2120_init(void)
53 return irda_device_register_dongle(&dongle);
56 static void __exit mcp2120_cleanup(void)
58 irda_device_unregister_dongle(&dongle);
61 static void mcp2120_open(dongle_t *self, struct qos_info *qos)
63 qos->baud_rate.bits &= IR_9600|IR_19200|IR_38400|IR_57600|IR_115200;
64 qos->min_turn_time.bits = 0x01;
67 static void mcp2120_close(dongle_t *self)
69 /* Power off dongle */
70 /* reset and inhibit mcp2120 */
71 self->set_dtr_rts(self->dev, TRUE, TRUE);
72 //self->set_dtr_rts(self->dev, FALSE, FALSE);
76 * Function mcp2120_change_speed (dev, speed)
78 * Set the speed for the MCP2120.
81 static int mcp2120_change_speed(struct irda_task *task)
83 dongle_t *self = (dongle_t *) task->instance;
84 __u32 speed = (__u32) task->param;
85 __u8 control[2];
86 int ret = 0;
88 self->speed_task = task;
90 switch (task->state) {
91 case IRDA_TASK_INIT:
92 /* Need to reset the dongle and go to 9600 bps before
93 programming */
94 //printk("Dmcp2120_change_speed irda_task_init\n");
95 if (irda_task_execute(self, mcp2120_reset, NULL, task,
96 (void *) speed))
98 /* Dongle need more time to reset */
99 irda_task_next_state(task, IRDA_TASK_CHILD_WAIT);
101 /* Give reset 1 sec to finish */
102 ret = msecs_to_jiffies(1000);
104 break;
105 case IRDA_TASK_CHILD_WAIT:
106 WARNING("%s(), resetting dongle timed out!\n", __FUNCTION__);
107 ret = -1;
108 break;
109 case IRDA_TASK_CHILD_DONE:
110 /* Set DTR to enter command mode */
111 self->set_dtr_rts(self->dev, TRUE, FALSE);
112 udelay(500);
114 switch (speed) {
115 case 9600:
116 default:
117 control[0] = MCP2120_9600;
118 //printk("mcp2120 9600\n");
119 break;
120 case 19200:
121 control[0] = MCP2120_19200;
122 //printk("mcp2120 19200\n");
123 break;
124 case 34800:
125 control[0] = MCP2120_38400;
126 //printk("mcp2120 38400\n");
127 break;
128 case 57600:
129 control[0] = MCP2120_57600;
130 //printk("mcp2120 57600\n");
131 break;
132 case 115200:
133 control[0] = MCP2120_115200;
134 //printk("mcp2120 115200\n");
135 break;
137 control[1] = MCP2120_COMMIT;
139 /* Write control bytes */
140 self->write(self->dev, control, 2);
142 irda_task_next_state(task, IRDA_TASK_WAIT);
143 ret = msecs_to_jiffies(100);
144 //printk("mcp2120_change_speed irda_child_done\n");
145 break;
146 case IRDA_TASK_WAIT:
147 /* Go back to normal mode */
148 self->set_dtr_rts(self->dev, FALSE, FALSE);
149 irda_task_next_state(task, IRDA_TASK_DONE);
150 self->speed_task = NULL;
151 //printk("mcp2120_change_speed irda_task_wait\n");
152 break;
153 default:
154 ERROR("%s(), unknown state %d\n", __FUNCTION__, task->state);
155 irda_task_next_state(task, IRDA_TASK_DONE);
156 self->speed_task = NULL;
157 ret = -1;
158 break;
160 return ret;
164 * Function mcp2120_reset (driver)
166 * This function resets the mcp2120 dongle.
168 * Info: -set RTS to reset mcp2120
169 * -set DTR to set mcp2120 software command mode
170 * -mcp2120 defaults to 9600 baud after reset
172 * Algorithm:
173 * 0. Set RTS to reset mcp2120.
174 * 1. Clear RTS and wait for device reset timer of 30 ms (max).
179 static int mcp2120_reset(struct irda_task *task)
181 dongle_t *self = (dongle_t *) task->instance;
182 int ret = 0;
184 self->reset_task = task;
186 switch (task->state) {
187 case IRDA_TASK_INIT:
188 //printk("mcp2120_reset irda_task_init\n");
189 /* Reset dongle by setting RTS*/
190 self->set_dtr_rts(self->dev, TRUE, TRUE);
191 irda_task_next_state(task, IRDA_TASK_WAIT1);
192 ret = msecs_to_jiffies(50);
193 break;
194 case IRDA_TASK_WAIT1:
195 //printk("mcp2120_reset irda_task_wait1\n");
196 /* clear RTS and wait for at least 30 ms. */
197 self->set_dtr_rts(self->dev, FALSE, FALSE);
198 irda_task_next_state(task, IRDA_TASK_WAIT2);
199 ret = msecs_to_jiffies(50);
200 break;
201 case IRDA_TASK_WAIT2:
202 //printk("mcp2120_reset irda_task_wait2\n");
203 /* Go back to normal mode */
204 self->set_dtr_rts(self->dev, FALSE, FALSE);
205 irda_task_next_state(task, IRDA_TASK_DONE);
206 self->reset_task = NULL;
207 break;
208 default:
209 ERROR("%s(), unknown state %d\n", __FUNCTION__, task->state);
210 irda_task_next_state(task, IRDA_TASK_DONE);
211 self->reset_task = NULL;
212 ret = -1;
213 break;
215 return ret;
218 MODULE_AUTHOR("Felix Tang <tangf@eyetap.org>");
219 MODULE_DESCRIPTION("Microchip MCP2120");
220 MODULE_LICENSE("GPL");
221 MODULE_ALIAS("irda-dongle-9"); /* IRDA_MCP2120_DONGLE */
224 * Function init_module (void)
226 * Initialize MCP2120 module
229 module_init(mcp2120_init);
232 * Function cleanup_module (void)
234 * Cleanup MCP2120 module
237 module_exit(mcp2120_cleanup);