diff options
Diffstat (limited to 'drivers/net/irda/sir_dev.c')
-rw-r--r-- | drivers/net/irda/sir_dev.c | 315 |
1 files changed, 311 insertions, 4 deletions
diff --git a/drivers/net/irda/sir_dev.c b/drivers/net/irda/sir_dev.c index ea7c9464d46a..3b5854d10c17 100644 --- a/drivers/net/irda/sir_dev.c +++ b/drivers/net/irda/sir_dev.c | |||
@@ -23,6 +23,298 @@ | |||
23 | 23 | ||
24 | #include "sir-dev.h" | 24 | #include "sir-dev.h" |
25 | 25 | ||
26 | |||
27 | static struct workqueue_struct *irda_sir_wq; | ||
28 | |||
29 | /* STATE MACHINE */ | ||
30 | |||
31 | /* substate handler of the config-fsm to handle the cases where we want | ||
32 | * to wait for transmit completion before changing the port configuration | ||
33 | */ | ||
34 | |||
35 | static int sirdev_tx_complete_fsm(struct sir_dev *dev) | ||
36 | { | ||
37 | struct sir_fsm *fsm = &dev->fsm; | ||
38 | unsigned next_state, delay; | ||
39 | unsigned bytes_left; | ||
40 | |||
41 | do { | ||
42 | next_state = fsm->substate; /* default: stay in current substate */ | ||
43 | delay = 0; | ||
44 | |||
45 | switch(fsm->substate) { | ||
46 | |||
47 | case SIRDEV_STATE_WAIT_XMIT: | ||
48 | if (dev->drv->chars_in_buffer) | ||
49 | bytes_left = dev->drv->chars_in_buffer(dev); | ||
50 | else | ||
51 | bytes_left = 0; | ||
52 | if (!bytes_left) { | ||
53 | next_state = SIRDEV_STATE_WAIT_UNTIL_SENT; | ||
54 | break; | ||
55 | } | ||
56 | |||
57 | if (dev->speed > 115200) | ||
58 | delay = (bytes_left*8*10000) / (dev->speed/100); | ||
59 | else if (dev->speed > 0) | ||
60 | delay = (bytes_left*10*10000) / (dev->speed/100); | ||
61 | else | ||
62 | delay = 0; | ||
63 | /* expected delay (usec) until remaining bytes are sent */ | ||
64 | if (delay < 100) { | ||
65 | udelay(delay); | ||
66 | delay = 0; | ||
67 | break; | ||
68 | } | ||
69 | /* sleep some longer delay (msec) */ | ||
70 | delay = (delay+999) / 1000; | ||
71 | break; | ||
72 | |||
73 | case SIRDEV_STATE_WAIT_UNTIL_SENT: | ||
74 | /* block until underlaying hardware buffer are empty */ | ||
75 | if (dev->drv->wait_until_sent) | ||
76 | dev->drv->wait_until_sent(dev); | ||
77 | next_state = SIRDEV_STATE_TX_DONE; | ||
78 | break; | ||
79 | |||
80 | case SIRDEV_STATE_TX_DONE: | ||
81 | return 0; | ||
82 | |||
83 | default: | ||
84 | IRDA_ERROR("%s - undefined state\n", __FUNCTION__); | ||
85 | return -EINVAL; | ||
86 | } | ||
87 | fsm->substate = next_state; | ||
88 | } while (delay == 0); | ||
89 | return delay; | ||
90 | } | ||
91 | |||
92 | /* | ||
93 | * Function sirdev_config_fsm | ||
94 | * | ||
95 | * State machine to handle the configuration of the device (and attached dongle, if any). | ||
96 | * This handler is scheduled for execution in kIrDAd context, so we can sleep. | ||
97 | * however, kIrDAd is shared by all sir_dev devices so we better don't sleep there too | ||
98 | * long. Instead, for longer delays we start a timer to reschedule us later. | ||
99 | * On entry, fsm->sem is always locked and the netdev xmit queue stopped. | ||
100 | * Both must be unlocked/restarted on completion - but only on final exit. | ||
101 | */ | ||
102 | |||
103 | static void sirdev_config_fsm(void *data) | ||
104 | { | ||
105 | struct sir_dev *dev = data; | ||
106 | struct sir_fsm *fsm = &dev->fsm; | ||
107 | int next_state; | ||
108 | int ret = -1; | ||
109 | unsigned delay; | ||
110 | |||
111 | IRDA_DEBUG(2, "%s(), <%ld>\n", __FUNCTION__, jiffies); | ||
112 | |||
113 | do { | ||
114 | IRDA_DEBUG(3, "%s - state=0x%04x / substate=0x%04x\n", | ||
115 | __FUNCTION__, fsm->state, fsm->substate); | ||
116 | |||
117 | next_state = fsm->state; | ||
118 | delay = 0; | ||
119 | |||
120 | switch(fsm->state) { | ||
121 | |||
122 | case SIRDEV_STATE_DONGLE_OPEN: | ||
123 | if (dev->dongle_drv != NULL) { | ||
124 | ret = sirdev_put_dongle(dev); | ||
125 | if (ret) { | ||
126 | fsm->result = -EINVAL; | ||
127 | next_state = SIRDEV_STATE_ERROR; | ||
128 | break; | ||
129 | } | ||
130 | } | ||
131 | |||
132 | /* Initialize dongle */ | ||
133 | ret = sirdev_get_dongle(dev, fsm->param); | ||
134 | if (ret) { | ||
135 | fsm->result = ret; | ||
136 | next_state = SIRDEV_STATE_ERROR; | ||
137 | break; | ||
138 | } | ||
139 | |||
140 | /* Dongles are powered through the modem control lines which | ||
141 | * were just set during open. Before resetting, let's wait for | ||
142 | * the power to stabilize. This is what some dongle drivers did | ||
143 | * in open before, while others didn't - should be safe anyway. | ||
144 | */ | ||
145 | |||
146 | delay = 50; | ||
147 | fsm->substate = SIRDEV_STATE_DONGLE_RESET; | ||
148 | next_state = SIRDEV_STATE_DONGLE_RESET; | ||
149 | |||
150 | fsm->param = 9600; | ||
151 | |||
152 | break; | ||
153 | |||
154 | case SIRDEV_STATE_DONGLE_CLOSE: | ||
155 | /* shouldn't we just treat this as success=? */ | ||
156 | if (dev->dongle_drv == NULL) { | ||
157 | fsm->result = -EINVAL; | ||
158 | next_state = SIRDEV_STATE_ERROR; | ||
159 | break; | ||
160 | } | ||
161 | |||
162 | ret = sirdev_put_dongle(dev); | ||
163 | if (ret) { | ||
164 | fsm->result = ret; | ||
165 | next_state = SIRDEV_STATE_ERROR; | ||
166 | break; | ||
167 | } | ||
168 | next_state = SIRDEV_STATE_DONE; | ||
169 | break; | ||
170 | |||
171 | case SIRDEV_STATE_SET_DTR_RTS: | ||
172 | ret = sirdev_set_dtr_rts(dev, | ||
173 | (fsm->param&0x02) ? TRUE : FALSE, | ||
174 | (fsm->param&0x01) ? TRUE : FALSE); | ||
175 | next_state = SIRDEV_STATE_DONE; | ||
176 | break; | ||
177 | |||
178 | case SIRDEV_STATE_SET_SPEED: | ||
179 | fsm->substate = SIRDEV_STATE_WAIT_XMIT; | ||
180 | next_state = SIRDEV_STATE_DONGLE_CHECK; | ||
181 | break; | ||
182 | |||
183 | case SIRDEV_STATE_DONGLE_CHECK: | ||
184 | ret = sirdev_tx_complete_fsm(dev); | ||
185 | if (ret < 0) { | ||
186 | fsm->result = ret; | ||
187 | next_state = SIRDEV_STATE_ERROR; | ||
188 | break; | ||
189 | } | ||
190 | if ((delay=ret) != 0) | ||
191 | break; | ||
192 | |||
193 | if (dev->dongle_drv) { | ||
194 | fsm->substate = SIRDEV_STATE_DONGLE_RESET; | ||
195 | next_state = SIRDEV_STATE_DONGLE_RESET; | ||
196 | } | ||
197 | else { | ||
198 | dev->speed = fsm->param; | ||
199 | next_state = SIRDEV_STATE_PORT_SPEED; | ||
200 | } | ||
201 | break; | ||
202 | |||
203 | case SIRDEV_STATE_DONGLE_RESET: | ||
204 | if (dev->dongle_drv->reset) { | ||
205 | ret = dev->dongle_drv->reset(dev); | ||
206 | if (ret < 0) { | ||
207 | fsm->result = ret; | ||
208 | next_state = SIRDEV_STATE_ERROR; | ||
209 | break; | ||
210 | } | ||
211 | } | ||
212 | else | ||
213 | ret = 0; | ||
214 | if ((delay=ret) == 0) { | ||
215 | /* set serial port according to dongle default speed */ | ||
216 | if (dev->drv->set_speed) | ||
217 | dev->drv->set_speed(dev, dev->speed); | ||
218 | fsm->substate = SIRDEV_STATE_DONGLE_SPEED; | ||
219 | next_state = SIRDEV_STATE_DONGLE_SPEED; | ||
220 | } | ||
221 | break; | ||
222 | |||
223 | case SIRDEV_STATE_DONGLE_SPEED: | ||
224 | if (dev->dongle_drv->reset) { | ||
225 | ret = dev->dongle_drv->set_speed(dev, fsm->param); | ||
226 | if (ret < 0) { | ||
227 | fsm->result = ret; | ||
228 | next_state = SIRDEV_STATE_ERROR; | ||
229 | break; | ||
230 | } | ||
231 | } | ||
232 | else | ||
233 | ret = 0; | ||
234 | if ((delay=ret) == 0) | ||
235 | next_state = SIRDEV_STATE_PORT_SPEED; | ||
236 | break; | ||
237 | |||
238 | case SIRDEV_STATE_PORT_SPEED: | ||
239 | /* Finally we are ready to change the serial port speed */ | ||
240 | if (dev->drv->set_speed) | ||
241 | dev->drv->set_speed(dev, dev->speed); | ||
242 | dev->new_speed = 0; | ||
243 | next_state = SIRDEV_STATE_DONE; | ||
244 | break; | ||
245 | |||
246 | case SIRDEV_STATE_DONE: | ||
247 | /* Signal network layer so it can send more frames */ | ||
248 | netif_wake_queue(dev->netdev); | ||
249 | next_state = SIRDEV_STATE_COMPLETE; | ||
250 | break; | ||
251 | |||
252 | default: | ||
253 | IRDA_ERROR("%s - undefined state\n", __FUNCTION__); | ||
254 | fsm->result = -EINVAL; | ||
255 | /* fall thru */ | ||
256 | |||
257 | case SIRDEV_STATE_ERROR: | ||
258 | IRDA_ERROR("%s - error: %d\n", __FUNCTION__, fsm->result); | ||
259 | |||
260 | #if 0 /* don't enable this before we have netdev->tx_timeout to recover */ | ||
261 | netif_stop_queue(dev->netdev); | ||
262 | #else | ||
263 | netif_wake_queue(dev->netdev); | ||
264 | #endif | ||
265 | /* fall thru */ | ||
266 | |||
267 | case SIRDEV_STATE_COMPLETE: | ||
268 | /* config change finished, so we are not busy any longer */ | ||
269 | sirdev_enable_rx(dev); | ||
270 | up(&fsm->sem); | ||
271 | return; | ||
272 | } | ||
273 | fsm->state = next_state; | ||
274 | } while(!delay); | ||
275 | |||
276 | queue_delayed_work(irda_sir_wq, &fsm->work, msecs_to_jiffies(delay)); | ||
277 | } | ||
278 | |||
279 | /* schedule some device configuration task for execution by kIrDAd | ||
280 | * on behalf of the above state machine. | ||
281 | * can be called from process or interrupt/tasklet context. | ||
282 | */ | ||
283 | |||
284 | int sirdev_schedule_request(struct sir_dev *dev, int initial_state, unsigned param) | ||
285 | { | ||
286 | struct sir_fsm *fsm = &dev->fsm; | ||
287 | |||
288 | IRDA_DEBUG(2, "%s - state=0x%04x / param=%u\n", __FUNCTION__, initial_state, param); | ||
289 | |||
290 | if (down_trylock(&fsm->sem)) { | ||
291 | if (in_interrupt() || in_atomic() || irqs_disabled()) { | ||
292 | IRDA_DEBUG(1, "%s(), state machine busy!\n", __FUNCTION__); | ||
293 | return -EWOULDBLOCK; | ||
294 | } else | ||
295 | down(&fsm->sem); | ||
296 | } | ||
297 | |||
298 | if (fsm->state == SIRDEV_STATE_DEAD) { | ||
299 | /* race with sirdev_close should never happen */ | ||
300 | IRDA_ERROR("%s(), instance staled!\n", __FUNCTION__); | ||
301 | up(&fsm->sem); | ||
302 | return -ESTALE; /* or better EPIPE? */ | ||
303 | } | ||
304 | |||
305 | netif_stop_queue(dev->netdev); | ||
306 | atomic_set(&dev->enable_rx, 0); | ||
307 | |||
308 | fsm->state = initial_state; | ||
309 | fsm->param = param; | ||
310 | fsm->result = 0; | ||
311 | |||
312 | INIT_WORK(&fsm->work, sirdev_config_fsm, dev); | ||
313 | queue_work(irda_sir_wq, &fsm->work); | ||
314 | return 0; | ||
315 | } | ||
316 | |||
317 | |||
26 | /***************************************************************************/ | 318 | /***************************************************************************/ |
27 | 319 | ||
28 | void sirdev_enable_rx(struct sir_dev *dev) | 320 | void sirdev_enable_rx(struct sir_dev *dev) |
@@ -619,10 +911,6 @@ struct sir_dev * sirdev_get_instance(const struct sir_driver *drv, const char *n | |||
619 | spin_lock_init(&dev->tx_lock); | 911 | spin_lock_init(&dev->tx_lock); |
620 | init_MUTEX(&dev->fsm.sem); | 912 | init_MUTEX(&dev->fsm.sem); |
621 | 913 | ||
622 | INIT_LIST_HEAD(&dev->fsm.rq.lh_request); | ||
623 | dev->fsm.rq.pending = 0; | ||
624 | init_timer(&dev->fsm.rq.timer); | ||
625 | |||
626 | dev->drv = drv; | 914 | dev->drv = drv; |
627 | dev->netdev = ndev; | 915 | dev->netdev = ndev; |
628 | 916 | ||
@@ -682,3 +970,22 @@ int sirdev_put_instance(struct sir_dev *dev) | |||
682 | } | 970 | } |
683 | EXPORT_SYMBOL(sirdev_put_instance); | 971 | EXPORT_SYMBOL(sirdev_put_instance); |
684 | 972 | ||
973 | static int __init sir_wq_init(void) | ||
974 | { | ||
975 | irda_sir_wq = create_singlethread_workqueue("irda_sir_wq"); | ||
976 | if (!irda_sir_wq) | ||
977 | return -ENOMEM; | ||
978 | return 0; | ||
979 | } | ||
980 | |||
981 | static void __exit sir_wq_exit(void) | ||
982 | { | ||
983 | destroy_workqueue(irda_sir_wq); | ||
984 | } | ||
985 | |||
986 | module_init(sir_wq_init); | ||
987 | module_exit(sir_wq_exit); | ||
988 | |||
989 | MODULE_AUTHOR("Martin Diehl <info@mdiehl.de>"); | ||
990 | MODULE_DESCRIPTION("IrDA SIR core"); | ||
991 | MODULE_LICENSE("GPL"); | ||