aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/irda/sir_kthread.c
blob: 18cea1099530a1131552879fa42fb304e6264e48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/*********************************************************************
 *
 *	sir_kthread.c:		dedicated thread to process scheduled
 *				sir device setup requests
 *
 *	Copyright (c) 2002 Martin Diehl
 *
 *	This program is free software; you can redistribute it and/or 
 *	modify it under the terms of the GNU General Public License as 
 *	published by the Free Software Foundation; either version 2 of 
 *	the License, or (at your option) any later version.
 *
 ********************************************************************/    

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/version.h>
#include <linux/init.h>
#include <linux/smp_lock.h>
#include <linux/completion.h>
#include <linux/delay.h>

#include <net/irda/irda.h>

#include "sir-dev.h"

/**************************************************************************
 *
 * kIrDAd kernel thread and config state machine
 *
 */

struct irda_request_queue {
	struct list_head request_list;
	spinlock_t lock;
	task_t *thread;
	struct completion exit;
	wait_queue_head_t kick, done;
	atomic_t num_pending;
};

static struct irda_request_queue irda_rq_queue;

static int irda_queue_request(struct irda_request *rq)
{
	int ret = 0;
	unsigned long flags;

	if (!test_and_set_bit(0, &rq->pending)) {
		spin_lock_irqsave(&irda_rq_queue.lock, flags);
		list_add_tail(&rq->lh_request, &irda_rq_queue.request_list);
		wake_up(&irda_rq_queue.kick);
		atomic_inc(&irda_rq_queue.num_pending);
		spin_unlock_irqrestore(&irda_rq_queue.lock, flags);
		ret = 1;
	}
	return ret;
}

static void irda_request_timer(unsigned long data)
{
	struct irda_request *rq = (struct irda_request *)data;
	unsigned long flags;
	
	spin_lock_irqsave(&irda_rq_queue.lock, flags);
	list_add_tail(&rq->lh_request, &irda_rq_queue.request_list);
	wake_up(&irda_rq_queue.kick);
	spin_unlock_irqrestore(&irda_rq_queue.lock, flags);
}

static int irda_queue_delayed_request(struct irda_request *rq, unsigned long delay)
{
	int ret = 0;
	struct timer_list *timer = &rq->timer;

	if (!test_and_set_bit(0, &rq->pending)) {
		timer->expires = jiffies + delay;
		timer->function = irda_request_timer;
		timer->data = (unsigned long)rq;
		atomic_inc(&irda_rq_queue.num_pending);
		add_timer(timer);
		ret = 1;
	}
	return ret;
}

static void run_irda_queue(void)
{
	unsigned long flags;
	struct list_head *entry, *tmp;
	struct irda_request *rq;

	spin_lock_irqsave(&irda_rq_queue.lock, flags);
	list_for_each_safe(entry, tmp, &irda_rq_queue.request_list) {
		rq = list_entry(entry, struct irda_request, lh_request);
		list_del_init(entry);
		spin_unlock_irqrestore(&irda_rq_queue.lock, flags);

		clear_bit(0, &rq->pending);
		rq->func(rq->data);

		if (atomic_dec_and_test(&irda_rq_queue.num_pending))
			wake_up(&irda_rq_queue.done);

		spin_lock_irqsave(&irda_rq_queue.lock, flags);
	}
	spin_unlock_irqrestore(&irda_rq_queue.lock, flags);
}		

static int irda_thread(void *startup)
{
	DECLARE_WAITQUEUE(wait, current);

	daemonize("kIrDAd");

	irda_rq_queue.thread = current;

	complete((struct completion *)startup);

	while (irda_rq_queue.thread != NULL) {

		/* We use TASK_INTERRUPTIBLE, rather than
		 * TASK_UNINTERRUPTIBLE.  Andrew Morton made this
		 * change ; he told me that it is safe, because "signal
		 * blocking is now handled in daemonize()", he added
		 * that the problem is that "uninterruptible sleep
		 * contributes to load average", making user worry.
		 * Jean II */
		set_task_state(current, TASK_INTERRUPTIBLE);
		add_wait_queue(&irda_rq_queue.kick, &wait);
		if (list_empty(&irda_rq_queue.request_list))
			schedule();
		else
			__set_task_state(current, TASK_RUNNING);
		remove_wait_queue(&irda_rq_queue.kick, &wait);

		/* make swsusp happy with our thread */
		if (current->flags & PF_FREEZE)
			refrigerator(PF_FREEZE);

		run_irda_queue();
	}

#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,35)
	reparent_to_init();
#endif
	complete_and_exit(&irda_rq_queue.exit, 0);
	/* never reached */
	return 0;
}


static void flush_irda_queue(void)
{
	if (atomic_read(&irda_rq_queue.num_pending)) {

		DECLARE_WAITQUEUE(wait, current);

		if (!list_empty(&irda_rq_queue.request_list))
			run_irda_queue();

		set_task_state(current, TASK_UNINTERRUPTIBLE);
		add_wait_queue(&irda_rq_queue.done, &wait);
		if (atomic_read(&irda_rq_queue.num_pending))
			schedule();
		else
			__set_task_state(current, TASK_RUNNING);
		remove_wait_queue(&irda_rq_queue.done, &wait);
	}
}

/* substate handler of the config-fsm to handle the cases where we want
 * to wait for transmit completion before changing the port configuration
 */

static int irda_tx_complete_fsm(struct sir_dev *dev)
{
	struct sir_fsm *fsm = &dev->fsm;
	unsigned next_state, delay;
	unsigned bytes_left;

	do {
		next_state = fsm->substate;	/* default: stay in current substate */
		delay = 0;

		switch(fsm->substate) {

		case SIRDEV_STATE_WAIT_XMIT:
			if (dev->drv->chars_in_buffer)
				bytes_left = dev->drv->chars_in_buffer(dev);
			else
				bytes_left = 0;
			if (!bytes_left) {
				next_state = SIRDEV_STATE_WAIT_UNTIL_SENT;
				break;
			}

			if (dev->speed > 115200)
				delay = (bytes_left*8*10000) / (dev->speed/100);
			else if (dev->speed > 0)
				delay = (bytes_left*10*10000) / (dev->speed/100);
			else
				delay = 0;
			/* expected delay (usec) until remaining bytes are sent */
			if (delay < 100) {
				udelay(delay);
				delay = 0;
				break;
			}
			/* sleep some longer delay (msec) */
			delay = (delay+999) / 1000;
			break;

		case SIRDEV_STATE_WAIT_UNTIL_SENT:
			/* block until underlaying hardware buffer are empty */
			if (dev->drv->wait_until_sent)
				dev->drv->wait_until_sent(dev);
			next_state = SIRDEV_STATE_TX_DONE;
			break;

		case SIRDEV_STATE_TX_DONE:
			return 0;

		default:
			IRDA_ERROR("%s - undefined state\n", __FUNCTION__);
			return -EINVAL;
		}
		fsm->substate = next_state;
	} while (delay == 0);
	return delay;
}

/*
 * Function irda_config_fsm
 *
 * State machine to handle the configuration of the device (and attached dongle, if any).
 * This handler is scheduled for execution in kIrDAd context, so we can sleep.
 * however, kIrDAd is shared by all sir_dev devices so we better don't sleep there too
 * long. Instead, for longer delays we start a timer to reschedule us later.
 * On entry, fsm->sem is always locked and the netdev xmit queue stopped.
 * Both must be unlocked/restarted on completion - but only on final exit.
 */

static void irda_config_fsm(void *data)
{
	struct sir_dev *dev = data;
	struct sir_fsm *fsm = &dev->fsm;
	int next_state;
	int ret = -1;
	unsigned delay;

	IRDA_DEBUG(2, "%s(), <%ld>\n", __FUNCTION__, jiffies); 

	do {
		IRDA_DEBUG(3, "%s - state=0x%04x / substate=0x%04x\n",
			__FUNCTION__, fsm->state, fsm->substate);

		next_state = fsm->state;
		delay = 0;

		switch(fsm->state) {

		case SIRDEV_STATE_DONGLE_OPEN:
			if (dev->dongle_drv != NULL) {
				ret = sirdev_put_dongle(dev);
				if (ret) {
					fsm->result = -EINVAL;
					next_state = SIRDEV_STATE_ERROR;
					break;
				}
			}

			/* Initialize dongle */
			ret = sirdev_get_dongle(dev, fsm->param);
			if (ret) {
				fsm->result = ret;
				next_state = SIRDEV_STATE_ERROR;
				break;
			}

			/* Dongles are powered through the modem control lines which
			 * were just set during open. Before resetting, let's wait for
			 * the power to stabilize. This is what some dongle drivers did
			 * in open before, while others didn't - should be safe anyway.
			 */

			delay = 50;
			fsm->substate = SIRDEV_STATE_DONGLE_RESET;
			next_state = SIRDEV_STATE_DONGLE_RESET;

			fsm->param = 9600;

			break;

		case SIRDEV_STATE_DONGLE_CLOSE:
			/* shouldn't we just treat this as success=? */
			if (dev->dongle_drv == NULL) {
				fsm->result = -EINVAL;
				next_state = SIRDEV_STATE_ERROR;
				break;
			}

			ret = sirdev_put_dongle(dev);
			if (ret) {
				fsm->result = ret;
				next_state = SIRDEV_STATE_ERROR;
				break;
			}
			next_state = SIRDEV_STATE_DONE;
			break;

		case SIRDEV_STATE_SET_DTR_RTS:
			ret = sirdev_set_dtr_rts(dev,
				(fsm->param&0x02) ? TRUE : FALSE,
				(fsm->param&0x01) ? TRUE : FALSE);
			next_state = SIRDEV_STATE_DONE;
			break;

		case SIRDEV_STATE_SET_SPEED:
			fsm->substate = SIRDEV_STATE_WAIT_XMIT;
			next_state = SIRDEV_STATE_DONGLE_CHECK;
			break;

		case SIRDEV_STATE_DONGLE_CHECK:
			ret = irda_tx_complete_fsm(dev);
			if (ret < 0) {
				fsm->result = ret;
				next_state = SIRDEV_STATE_ERROR;
				break;
			}
			if ((delay=ret) != 0)
				break;

			if (dev->dongle_drv) {
				fsm->substate = SIRDEV_STATE_DONGLE_RESET;
				next_state = SIRDEV_STATE_DONGLE_RESET;
			}
			else {
				dev->speed = fsm->param;
				next_state = SIRDEV_STATE_PORT_SPEED;
			}
			break;

		case SIRDEV_STATE_DONGLE_RESET:
			if (dev->dongle_drv->reset) {
				ret = dev->dongle_drv->reset(dev);	
				if (ret < 0) {
					fsm->result = ret;
					next_state = SIRDEV_STATE_ERROR;
					break;
				}
			}
			else
				ret = 0;
			if ((delay=ret) == 0) {
				/* set serial port according to dongle default speed */
				if (dev->drv->set_speed)
					dev->drv->set_speed(dev, dev->speed);
				fsm->substate = SIRDEV_STATE_DONGLE_SPEED;
				next_state = SIRDEV_STATE_DONGLE_SPEED;
			}
			break;

		case SIRDEV_STATE_DONGLE_SPEED:				
			if (dev->dongle_drv->reset) {
				ret = dev->dongle_drv->set_speed(dev, fsm->param);
				if (ret < 0) {
					fsm->result = ret;
					next_state = SIRDEV_STATE_ERROR;
					break;
				}
			}
			else
				ret = 0;
			if ((delay=ret) == 0)
				next_state = SIRDEV_STATE_PORT_SPEED;
			break;

		case SIRDEV_STATE_PORT_SPEED:
			/* Finally we are ready to change the serial port speed */
			if (dev->drv->set_speed)
				dev->drv->set_speed(dev, dev->speed);
			dev->new_speed = 0;
			next_state = SIRDEV_STATE_DONE;
			break;

		case SIRDEV_STATE_DONE:
			/* Signal network layer so it can send more frames */
			netif_wake_queue(dev->netdev);
			next_state = SIRDEV_STATE_COMPLETE;
			break;

		default:
			IRDA_ERROR("%s - undefined state\n", __FUNCTION__);
			fsm->result = -EINVAL;
			/* fall thru */

		case SIRDEV_STATE_ERROR:
			IRDA_ERROR("%s - error: %d\n", __FUNCTION__, fsm->result);

#if 0	/* don't enable this before we have netdev->tx_timeout to recover */
			netif_stop_queue(dev->netdev);
#else
			netif_wake_queue(dev->netdev);
#endif
			/* fall thru */

		case SIRDEV_STATE_COMPLETE:
			/* config change finished, so we are not busy any longer */
			sirdev_enable_rx(dev);
			up(&fsm->sem);
			return;
		}
		fsm->state = next_state;
	} while(!delay);

	irda_queue_delayed_request(&fsm->rq, msecs_to_jiffies(delay));
}

/* schedule some device configuration task for execution by kIrDAd
 * on behalf of the above state machine.
 * can be called from process or interrupt/tasklet context.
 */

int sirdev_schedule_request(struct sir_dev *dev, int initial_state, unsigned param)
{
	struct sir_fsm *fsm = &dev->fsm;
	int xmit_was_down;

	IRDA_DEBUG(2, "%s - state=0x%04x / param=%u\n", __FUNCTION__, initial_state, param);

	if (down_trylock(&fsm->sem)) {
		if (in_interrupt()  ||  in_atomic()  ||  irqs_disabled()) {
			IRDA_DEBUG(1, "%s(), state machine busy!\n", __FUNCTION__);
			return -EWOULDBLOCK;
		} else
			down(&fsm->sem);
	}

	if (fsm->state == SIRDEV_STATE_DEAD) {
		/* race with sirdev_close should never happen */
		IRDA_ERROR("%s(), instance staled!\n", __FUNCTION__);
		up(&fsm->sem);
		return -ESTALE;		/* or better EPIPE? */
	}

	xmit_was_down = netif_queue_stopped(dev->netdev);
	netif_stop_queue(dev->netdev);
	atomic_set(&dev->enable_rx, 0);

	fsm->state = initial_state;
	fsm->param = param;
	fsm->result = 0;

	INIT_LIST_HEAD(&fsm->rq.lh_request);
	fsm->rq.pending = 0;
	fsm->rq.func = irda_config_fsm;
	fsm->rq.data = dev;

	if (!irda_queue_request(&fsm->rq)) {	/* returns 0 on error! */
		atomic_set(&dev->enable_rx, 1);
		if (!xmit_was_down)
			netif_wake_queue(dev->netdev);		
		up(&fsm->sem);
		return -EAGAIN;
	}
	return 0;
}

int __init irda_thread_create(void)
{
	struct completion startup;
	int pid;

	spin_lock_init(&irda_rq_queue.lock);
	irda_rq_queue.thread = NULL;
	INIT_LIST_HEAD(&irda_rq_queue.request_list);
	init_waitqueue_head(&irda_rq_queue.kick);
	init_waitqueue_head(&irda_rq_queue.done);
	atomic_set(&irda_rq_queue.num_pending, 0);

	init_completion(&startup);
	pid = kernel_thread(irda_thread, &startup, CLONE_FS|CLONE_FILES);
	if (pid <= 0)
		return -EAGAIN;
	else
		wait_for_completion(&startup);

	return 0;
}

void __exit irda_thread_join(void) 
{
	if (irda_rq_queue.thread) {
		flush_irda_queue();
		init_completion(&irda_rq_queue.exit);
		irda_rq_queue.thread = NULL;
		wake_up(&irda_rq_queue.kick);		
		wait_for_completion(&irda_rq_queue.exit);
	}
}