1 /*********************************************************************
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
,
45 .close
= mcp2120_close
,
46 .reset
= mcp2120_reset
,
47 .change_speed
= mcp2120_change_speed
,
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
;
88 self
->speed_task
= task
;
90 switch (task
->state
) {
92 /* Need to reset the dongle and go to 9600 bps before
94 //printk("Dmcp2120_change_speed irda_task_init\n");
95 if (irda_task_execute(self
, mcp2120_reset
, NULL
, task
,
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);
105 case IRDA_TASK_CHILD_WAIT
:
106 WARNING("%s(), resetting dongle timed out!\n", __FUNCTION__
);
109 case IRDA_TASK_CHILD_DONE
:
110 /* Set DTR to enter command mode */
111 self
->set_dtr_rts(self
->dev
, TRUE
, FALSE
);
117 control
[0] = MCP2120_9600
;
118 //printk("mcp2120 9600\n");
121 control
[0] = MCP2120_19200
;
122 //printk("mcp2120 19200\n");
125 control
[0] = MCP2120_38400
;
126 //printk("mcp2120 38400\n");
129 control
[0] = MCP2120_57600
;
130 //printk("mcp2120 57600\n");
133 control
[0] = MCP2120_115200
;
134 //printk("mcp2120 115200\n");
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");
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");
154 ERROR("%s(), unknown state %d\n", __FUNCTION__
, task
->state
);
155 irda_task_next_state(task
, IRDA_TASK_DONE
);
156 self
->speed_task
= NULL
;
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
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
;
184 self
->reset_task
= task
;
186 switch (task
->state
) {
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);
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);
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
;
209 ERROR("%s(), unknown state %d\n", __FUNCTION__
, task
->state
);
210 irda_task_next_state(task
, IRDA_TASK_DONE
);
211 self
->reset_task
= NULL
;
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
);