1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include <linux/module.h>
22#include <linux/delay.h>
23#include <linux/init.h>
24
25#include <net/irda/irda.h>
26
27#include "sir-dev.h"
28
29static int mcp2120_reset(struct sir_dev *dev);
30static int mcp2120_open(struct sir_dev *dev);
31static int mcp2120_close(struct sir_dev *dev);
32static int mcp2120_change_speed(struct sir_dev *dev, unsigned speed);
33
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
39
40#define MCP2120_COMMIT 0x11
41
42static struct dongle_driver mcp2120 = {
43 .owner = THIS_MODULE,
44 .driver_name = "Microchip MCP2120",
45 .type = IRDA_MCP2120_DONGLE,
46 .open = mcp2120_open,
47 .close = mcp2120_close,
48 .reset = mcp2120_reset,
49 .set_speed = mcp2120_change_speed,
50};
51
52static int __init mcp2120_sir_init(void)
53{
54 return irda_register_dongle(&mcp2120);
55}
56
57static void __exit mcp2120_sir_cleanup(void)
58{
59 irda_unregister_dongle(&mcp2120);
60}
61
62static int mcp2120_open(struct sir_dev *dev)
63{
64 struct qos_info *qos = &dev->qos;
65
66 IRDA_DEBUG(2, "%s()\n", __func__);
67
68
69
70 qos->baud_rate.bits &= IR_9600|IR_19200|IR_38400|IR_57600|IR_115200;
71 qos->min_turn_time.bits = 0x01;
72 irda_qos_bits_to_value(qos);
73
74 return 0;
75}
76
77static int mcp2120_close(struct sir_dev *dev)
78{
79 IRDA_DEBUG(2, "%s()\n", __func__);
80
81
82
83 sirdev_set_dtr_rts(dev, TRUE, TRUE);
84
85
86 return 0;
87}
88
89
90
91
92
93
94
95
96#define MCP2120_STATE_WAIT_SPEED (SIRDEV_STATE_DONGLE_SPEED+1)
97
98static int mcp2120_change_speed(struct sir_dev *dev, unsigned speed)
99{
100 unsigned state = dev->fsm.substate;
101 unsigned delay = 0;
102 u8 control[2];
103 static int ret = 0;
104
105 IRDA_DEBUG(2, "%s()\n", __func__);
106
107 switch (state) {
108 case SIRDEV_STATE_DONGLE_SPEED:
109
110 sirdev_set_dtr_rts(dev, TRUE, FALSE);
111 udelay(500);
112
113 ret = 0;
114 switch (speed) {
115 default:
116 speed = 9600;
117 ret = -EINVAL;
118
119 case 9600:
120 control[0] = MCP2120_9600;
121
122 break;
123 case 19200:
124 control[0] = MCP2120_19200;
125
126 break;
127 case 34800:
128 control[0] = MCP2120_38400;
129
130 break;
131 case 57600:
132 control[0] = MCP2120_57600;
133
134 break;
135 case 115200:
136 control[0] = MCP2120_115200;
137
138 break;
139 }
140 control[1] = MCP2120_COMMIT;
141
142
143 sirdev_raw_write(dev, control, 2);
144 dev->speed = speed;
145
146 state = MCP2120_STATE_WAIT_SPEED;
147 delay = 100;
148
149 break;
150
151 case MCP2120_STATE_WAIT_SPEED:
152
153 sirdev_set_dtr_rts(dev, FALSE, FALSE);
154
155 break;
156
157 default:
158 IRDA_ERROR("%s(), undefine state %d\n", __func__, state);
159 ret = -EINVAL;
160 break;
161 }
162 dev->fsm.substate = state;
163 return (delay > 0) ? delay : ret;
164}
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181#define MCP2120_STATE_WAIT1_RESET (SIRDEV_STATE_DONGLE_RESET+1)
182#define MCP2120_STATE_WAIT2_RESET (SIRDEV_STATE_DONGLE_RESET+2)
183
184static int mcp2120_reset(struct sir_dev *dev)
185{
186 unsigned state = dev->fsm.substate;
187 unsigned delay = 0;
188 int ret = 0;
189
190 IRDA_DEBUG(2, "%s()\n", __func__);
191
192 switch (state) {
193 case SIRDEV_STATE_DONGLE_RESET:
194
195
196 sirdev_set_dtr_rts(dev, TRUE, TRUE);
197 state = MCP2120_STATE_WAIT1_RESET;
198 delay = 50;
199 break;
200
201 case MCP2120_STATE_WAIT1_RESET:
202
203
204 sirdev_set_dtr_rts(dev, FALSE, FALSE);
205 state = MCP2120_STATE_WAIT2_RESET;
206 delay = 50;
207 break;
208
209 case MCP2120_STATE_WAIT2_RESET:
210
211
212 sirdev_set_dtr_rts(dev, FALSE, FALSE);
213 break;
214
215 default:
216 IRDA_ERROR("%s(), undefined state %d\n", __func__, state);
217 ret = -EINVAL;
218 break;
219 }
220 dev->fsm.substate = state;
221 return (delay > 0) ? delay : ret;
222}
223
224MODULE_AUTHOR("Felix Tang <tangf@eyetap.org>");
225MODULE_DESCRIPTION("Microchip MCP2120");
226MODULE_LICENSE("GPL");
227MODULE_ALIAS("irda-dongle-9");
228
229module_init(mcp2120_sir_init);
230module_exit(mcp2120_sir_cleanup);
231