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 IRDA_WARNING("%s(), resetting dongle timed out!\n",
110 case IRDA_TASK_CHILD_DONE
:
111 /* Set DTR to enter command mode */
112 self
->set_dtr_rts(self
->dev
, TRUE
, FALSE
);
118 control
[0] = MCP2120_9600
;
119 //printk("mcp2120 9600\n");
122 control
[0] = MCP2120_19200
;
123 //printk("mcp2120 19200\n");
126 control
[0] = MCP2120_38400
;
127 //printk("mcp2120 38400\n");
130 control
[0] = MCP2120_57600
;
131 //printk("mcp2120 57600\n");
134 control
[0] = MCP2120_115200
;
135 //printk("mcp2120 115200\n");
138 control
[1] = MCP2120_COMMIT
;
140 /* Write control bytes */
141 self
->write(self
->dev
, control
, 2);
143 irda_task_next_state(task
, IRDA_TASK_WAIT
);
144 ret
= msecs_to_jiffies(100);
145 //printk("mcp2120_change_speed irda_child_done\n");
148 /* Go back to normal mode */
149 self
->set_dtr_rts(self
->dev
, FALSE
, FALSE
);
150 irda_task_next_state(task
, IRDA_TASK_DONE
);
151 self
->speed_task
= NULL
;
152 //printk("mcp2120_change_speed irda_task_wait\n");
155 IRDA_ERROR("%s(), unknown state %d\n",
156 __FUNCTION__
, task
->state
);
157 irda_task_next_state(task
, IRDA_TASK_DONE
);
158 self
->speed_task
= NULL
;
166 * Function mcp2120_reset (driver)
168 * This function resets the mcp2120 dongle.
170 * Info: -set RTS to reset mcp2120
171 * -set DTR to set mcp2120 software command mode
172 * -mcp2120 defaults to 9600 baud after reset
175 * 0. Set RTS to reset mcp2120.
176 * 1. Clear RTS and wait for device reset timer of 30 ms (max).
181 static int mcp2120_reset(struct irda_task
*task
)
183 dongle_t
*self
= (dongle_t
*) task
->instance
;
186 self
->reset_task
= task
;
188 switch (task
->state
) {
190 //printk("mcp2120_reset irda_task_init\n");
191 /* Reset dongle by setting RTS*/
192 self
->set_dtr_rts(self
->dev
, TRUE
, TRUE
);
193 irda_task_next_state(task
, IRDA_TASK_WAIT1
);
194 ret
= msecs_to_jiffies(50);
196 case IRDA_TASK_WAIT1
:
197 //printk("mcp2120_reset irda_task_wait1\n");
198 /* clear RTS and wait for at least 30 ms. */
199 self
->set_dtr_rts(self
->dev
, FALSE
, FALSE
);
200 irda_task_next_state(task
, IRDA_TASK_WAIT2
);
201 ret
= msecs_to_jiffies(50);
203 case IRDA_TASK_WAIT2
:
204 //printk("mcp2120_reset irda_task_wait2\n");
205 /* Go back to normal mode */
206 self
->set_dtr_rts(self
->dev
, FALSE
, FALSE
);
207 irda_task_next_state(task
, IRDA_TASK_DONE
);
208 self
->reset_task
= NULL
;
211 IRDA_ERROR("%s(), unknown state %d\n",
212 __FUNCTION__
, task
->state
);
213 irda_task_next_state(task
, IRDA_TASK_DONE
);
214 self
->reset_task
= NULL
;
221 MODULE_AUTHOR("Felix Tang <tangf@eyetap.org>");
222 MODULE_DESCRIPTION("Microchip MCP2120");
223 MODULE_LICENSE("GPL");
224 MODULE_ALIAS("irda-dongle-9"); /* IRDA_MCP2120_DONGLE */
227 * Function init_module (void)
229 * Initialize MCP2120 module
232 module_init(mcp2120_init
);
235 * Function cleanup_module (void)
237 * Cleanup MCP2120 module
240 module_exit(mcp2120_cleanup
);