Blame view

drivers/net/irda/sir_dev.c 24.3 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
12
  /*********************************************************************
   *
   *	sir_dev.c:	irda sir network device
   * 
   *	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.
   *
   ********************************************************************/    
a6b7a4078   Alexey Dobriyan   net: remove inter...
13
  #include <linux/hardirq.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
14
15
  #include <linux/module.h>
  #include <linux/kernel.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
16
  #include <linux/slab.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
17
  #include <linux/init.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
18
19
20
21
22
23
24
  #include <linux/delay.h>
  
  #include <net/irda/irda.h>
  #include <net/irda/wrapper.h>
  #include <net/irda/irda_device.h>
  
  #include "sir-dev.h"
788252e66   Christoph Hellwig   [IRDA]: Switching...
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
  
  static struct workqueue_struct *irda_sir_wq;
  
  /* STATE MACHINE */
  
  /* 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 sirdev_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:
a97a6f107   Harvey Harrison   irda: replace __F...
83
84
  			IRDA_ERROR("%s - undefined state
  ", __func__);
788252e66   Christoph Hellwig   [IRDA]: Switching...
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
  			return -EINVAL;
  		}
  		fsm->substate = next_state;
  	} while (delay == 0);
  	return delay;
  }
  
  /*
   * Function sirdev_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.
   */
c4028958b   David Howells   WorkStruct: make ...
102
  static void sirdev_config_fsm(struct work_struct *work)
788252e66   Christoph Hellwig   [IRDA]: Switching...
103
  {
c4028958b   David Howells   WorkStruct: make ...
104
  	struct sir_dev *dev = container_of(work, struct sir_dev, fsm.work.work);
788252e66   Christoph Hellwig   [IRDA]: Switching...
105
106
107
108
  	struct sir_fsm *fsm = &dev->fsm;
  	int next_state;
  	int ret = -1;
  	unsigned delay;
a97a6f107   Harvey Harrison   irda: replace __F...
109
110
  	IRDA_DEBUG(2, "%s(), <%ld>
  ", __func__, jiffies);
788252e66   Christoph Hellwig   [IRDA]: Switching...
111
112
113
114
  
  	do {
  		IRDA_DEBUG(3, "%s - state=0x%04x / substate=0x%04x
  ",
a97a6f107   Harvey Harrison   irda: replace __F...
115
  			__func__, fsm->state, fsm->substate);
788252e66   Christoph Hellwig   [IRDA]: Switching...
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
  
  		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 = sirdev_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:
a97a6f107   Harvey Harrison   irda: replace __F...
253
254
  			IRDA_ERROR("%s - undefined state
  ", __func__);
788252e66   Christoph Hellwig   [IRDA]: Switching...
255
256
257
258
  			fsm->result = -EINVAL;
  			/* fall thru */
  
  		case SIRDEV_STATE_ERROR:
a97a6f107   Harvey Harrison   irda: replace __F...
259
260
  			IRDA_ERROR("%s - error: %d
  ", __func__, fsm->result);
788252e66   Christoph Hellwig   [IRDA]: Switching...
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
  
  #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);
  
  	queue_delayed_work(irda_sir_wq, &fsm->work, 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;
a97a6f107   Harvey Harrison   irda: replace __F...
289
290
291
  	IRDA_DEBUG(2, "%s - state=0x%04x / param=%u
  ", __func__,
  			initial_state, param);
788252e66   Christoph Hellwig   [IRDA]: Switching...
292
293
294
  
  	if (down_trylock(&fsm->sem)) {
  		if (in_interrupt()  ||  in_atomic()  ||  irqs_disabled()) {
a97a6f107   Harvey Harrison   irda: replace __F...
295
296
  			IRDA_DEBUG(1, "%s(), state machine busy!
  ", __func__);
788252e66   Christoph Hellwig   [IRDA]: Switching...
297
298
299
300
301
302
303
  			return -EWOULDBLOCK;
  		} else
  			down(&fsm->sem);
  	}
  
  	if (fsm->state == SIRDEV_STATE_DEAD) {
  		/* race with sirdev_close should never happen */
a97a6f107   Harvey Harrison   irda: replace __F...
304
305
  		IRDA_ERROR("%s(), instance staled!
  ", __func__);
788252e66   Christoph Hellwig   [IRDA]: Switching...
306
307
308
309
310
311
312
313
314
315
  		up(&fsm->sem);
  		return -ESTALE;		/* or better EPIPE? */
  	}
  
  	netif_stop_queue(dev->netdev);
  	atomic_set(&dev->enable_rx, 0);
  
  	fsm->state = initial_state;
  	fsm->param = param;
  	fsm->result = 0;
c4028958b   David Howells   WorkStruct: make ...
316
317
  	INIT_DELAYED_WORK(&fsm->work, sirdev_config_fsm);
  	queue_delayed_work(irda_sir_wq, &fsm->work, 0);
788252e66   Christoph Hellwig   [IRDA]: Switching...
318
319
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
  /***************************************************************************/
  
  void sirdev_enable_rx(struct sir_dev *dev)
  {
  	if (unlikely(atomic_read(&dev->enable_rx)))
  		return;
  
  	/* flush rx-buffer - should also help in case of problems with echo cancelation */
  	dev->rx_buff.data = dev->rx_buff.head;
  	dev->rx_buff.len = 0;
  	dev->rx_buff.in_frame = FALSE;
  	dev->rx_buff.state = OUTSIDE_FRAME;
  	atomic_set(&dev->enable_rx, 1);
  }
  
  static int sirdev_is_receiving(struct sir_dev *dev)
  {
  	if (!atomic_read(&dev->enable_rx))
  		return 0;
807540baa   Eric Dumazet   drivers/net: retu...
339
  	return dev->rx_buff.state != OUTSIDE_FRAME;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
340
341
342
343
344
  }
  
  int sirdev_set_dongle(struct sir_dev *dev, IRDA_DONGLE type)
  {
  	int err;
a97a6f107   Harvey Harrison   irda: replace __F...
345
346
  	IRDA_DEBUG(3, "%s : requesting dongle %d.
  ", __func__, type);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
347
348
349
350
351
352
353
354
355
  
  	err = sirdev_schedule_dongle_open(dev, type);
  	if (unlikely(err))
  		return err;
  	down(&dev->fsm.sem);		/* block until config change completed */
  	err = dev->fsm.result;
  	up(&dev->fsm.sem);
  	return err;
  }
214ad7843   Adrian Bunk   [IRDA]: kill driv...
356
  EXPORT_SYMBOL(sirdev_set_dongle);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
  
  /* used by dongle drivers for dongle programming */
  
  int sirdev_raw_write(struct sir_dev *dev, const char *buf, int len)
  {
  	unsigned long flags;
  	int ret;
  
  	if (unlikely(len > dev->tx_buff.truesize))
  		return -ENOSPC;
  
  	spin_lock_irqsave(&dev->tx_lock, flags);	/* serialize with other tx operations */
  	while (dev->tx_buff.len > 0) {			/* wait until tx idle */
  		spin_unlock_irqrestore(&dev->tx_lock, flags);
  		msleep(10);
  		spin_lock_irqsave(&dev->tx_lock, flags);
  	}
  
  	dev->tx_buff.data = dev->tx_buff.head;
  	memcpy(dev->tx_buff.data, buf, len);	
  	dev->tx_buff.len = len;
  
  	ret = dev->drv->do_write(dev, dev->tx_buff.data, dev->tx_buff.len);
  	if (ret > 0) {
a97a6f107   Harvey Harrison   irda: replace __F...
381
382
  		IRDA_DEBUG(3, "%s(), raw-tx started
  ", __func__);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
383
384
385
386
387
388
389
390
391
  
  		dev->tx_buff.data += ret;
  		dev->tx_buff.len -= ret;
  		dev->raw_tx = 1;
  		ret = len;		/* all data is going to be sent */
  	}
  	spin_unlock_irqrestore(&dev->tx_lock, flags);
  	return ret;
  }
214ad7843   Adrian Bunk   [IRDA]: kill driv...
392
  EXPORT_SYMBOL(sirdev_raw_write);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
  
  /* seems some dongle drivers may need this */
  
  int sirdev_raw_read(struct sir_dev *dev, char *buf, int len)
  {
  	int count;
  
  	if (atomic_read(&dev->enable_rx))
  		return -EIO;		/* fail if we expect irda-frames */
  
  	count = (len < dev->rx_buff.len) ? len : dev->rx_buff.len;
  
  	if (count > 0) {
  		memcpy(buf, dev->rx_buff.data, count);
  		dev->rx_buff.data += count;
  		dev->rx_buff.len -= count;
  	}
  
  	/* remaining stuff gets flushed when re-enabling normal rx */
  
  	return count;
  }
214ad7843   Adrian Bunk   [IRDA]: kill driv...
415
  EXPORT_SYMBOL(sirdev_raw_read);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
416
417
418
419
  
  int sirdev_set_dtr_rts(struct sir_dev *dev, int dtr, int rts)
  {
  	int ret = -ENXIO;
37e1370b7   Al Viro   drivers/net/irda:...
420
  	if (dev->drv->set_dtr_rts)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
421
422
423
  		ret =  dev->drv->set_dtr_rts(dev, dtr, rts);
  	return ret;
  }
214ad7843   Adrian Bunk   [IRDA]: kill driv...
424
  EXPORT_SYMBOL(sirdev_set_dtr_rts);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
  /**********************************************************************/
  
  /* called from client driver - likely with bh-context - to indicate
   * it made some progress with transmission. Hence we send the next
   * chunk, if any, or complete the skb otherwise
   */
  
  void sirdev_write_complete(struct sir_dev *dev)
  {
  	unsigned long flags;
  	struct sk_buff *skb;
  	int actual = 0;
  	int err;
  	
  	spin_lock_irqsave(&dev->tx_lock, flags);
  
  	IRDA_DEBUG(3, "%s() - dev->tx_buff.len = %d
  ",
a97a6f107   Harvey Harrison   irda: replace __F...
443
  		   __func__, dev->tx_buff.len);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
444
445
446
447
448
449
450
451
452
453
454
455
456
  
  	if (likely(dev->tx_buff.len > 0))  {
  		/* Write data left in transmit buffer */
  		actual = dev->drv->do_write(dev, dev->tx_buff.data, dev->tx_buff.len);
  
  		if (likely(actual>0)) {
  			dev->tx_buff.data += actual;
  			dev->tx_buff.len  -= actual;
  		}
  		else if (unlikely(actual<0)) {
  			/* could be dropped later when we have tx_timeout to recover */
  			IRDA_ERROR("%s: drv->do_write failed (%d)
  ",
a97a6f107   Harvey Harrison   irda: replace __F...
457
  				   __func__, actual);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
458
459
460
  			if ((skb=dev->tx_skb) != NULL) {
  				dev->tx_skb = NULL;
  				dev_kfree_skb_any(skb);
af0490810   Stephen Hemminger   irda: convert to ...
461
462
  				dev->netdev->stats.tx_errors++;
  				dev->netdev->stats.tx_dropped++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
463
464
465
466
467
468
469
470
471
472
473
474
475
476
  			}
  			dev->tx_buff.len = 0;
  		}
  		if (dev->tx_buff.len > 0)
  			goto done;	/* more data to send later */
  	}
  
  	if (unlikely(dev->raw_tx != 0)) {
  		/* in raw mode we are just done now after the buffer was sent
  		 * completely. Since this was requested by some dongle driver
  		 * running under the control of the irda-thread we must take
  		 * care here not to re-enable the queue. The queue will be
  		 * restarted when the irda-thread has completed the request.
  		 */
a97a6f107   Harvey Harrison   irda: replace __F...
477
478
  		IRDA_DEBUG(3, "%s(), raw-tx done
  ", __func__);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
  		dev->raw_tx = 0;
  		goto done;	/* no post-frame handling in raw mode */
  	}
  
  	/* we have finished now sending this skb.
  	 * update statistics and free the skb.
  	 * finally we check and trigger a pending speed change, if any.
  	 * if not we switch to rx mode and wake the queue for further
  	 * packets.
  	 * note the scheduled speed request blocks until the lower
  	 * client driver and the corresponding hardware has really
  	 * finished sending all data (xmit fifo drained f.e.)
  	 * before the speed change gets finally done and the queue
  	 * re-activated.
  	 */
a97a6f107   Harvey Harrison   irda: replace __F...
494
495
  	IRDA_DEBUG(5, "%s(), finished with frame!
  ", __func__);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
496
497
498
  		
  	if ((skb=dev->tx_skb) != NULL) {
  		dev->tx_skb = NULL;
af0490810   Stephen Hemminger   irda: convert to ...
499
500
  		dev->netdev->stats.tx_packets++;
  		dev->netdev->stats.tx_bytes += skb->len;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
501
502
503
504
  		dev_kfree_skb_any(skb);
  	}
  
  	if (unlikely(dev->new_speed > 0)) {
a97a6f107   Harvey Harrison   irda: replace __F...
505
506
  		IRDA_DEBUG(5, "%s(), Changing speed!
  ", __func__);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
507
508
509
510
511
512
513
  		err = sirdev_schedule_speed(dev, dev->new_speed);
  		if (unlikely(err)) {
  			/* should never happen
  			 * forget the speed change and hope the stack recovers
  			 */
  			IRDA_ERROR("%s - schedule speed change failed: %d
  ",
a97a6f107   Harvey Harrison   irda: replace __F...
514
  				   __func__, err);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
  			netif_wake_queue(dev->netdev);
  		}
  		/* else: success
  		 *	speed change in progress now
  		 *	on completion dev->new_speed gets cleared,
  		 *	rx-reenabled and the queue restarted
  		 */
  	}
  	else {
  		sirdev_enable_rx(dev);
  		netif_wake_queue(dev->netdev);
  	}
  
  done:
  	spin_unlock_irqrestore(&dev->tx_lock, flags);
  }
214ad7843   Adrian Bunk   [IRDA]: kill driv...
531
  EXPORT_SYMBOL(sirdev_write_complete);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
532
533
534
535
536
537
538
539
540
  
  /* called from client driver - likely with bh-context - to give us
   * some more received bytes. We put them into the rx-buffer,
   * normally unwrapping and building LAP-skb's (unless rx disabled)
   */
  
  int sirdev_receive(struct sir_dev *dev, const unsigned char *cp, size_t count) 
  {
  	if (!dev || !dev->netdev) {
a97a6f107   Harvey Harrison   irda: replace __F...
541
542
  		IRDA_WARNING("%s(), not ready yet!
  ", __func__);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
543
544
545
546
547
548
  		return -1;
  	}
  
  	if (!dev->irlap) {
  		IRDA_WARNING("%s - too early: %p / %zd!
  ",
a97a6f107   Harvey Harrison   irda: replace __F...
549
  			     __func__, cp, count);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
550
551
552
553
554
555
556
557
  		return -1;
  	}
  
  	if (cp==NULL) {
  		/* error already at lower level receive
  		 * just update stats and set media busy
  		 */
  		irda_device_set_media_busy(dev->netdev, TRUE);
af0490810   Stephen Hemminger   irda: convert to ...
558
  		dev->netdev->stats.rx_dropped++;
a97a6f107   Harvey Harrison   irda: replace __F...
559
560
  		IRDA_DEBUG(0, "%s; rx-drop: %zd
  ", __func__, count);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
561
562
563
564
565
566
567
  		return 0;
  	}
  
  	/* Read the characters into the buffer */
  	if (likely(atomic_read(&dev->enable_rx))) {
  		while (count--)
  			/* Unwrap and destuff one byte */
af0490810   Stephen Hemminger   irda: convert to ...
568
  			async_unwrap_char(dev->netdev, &dev->netdev->stats,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
  					  &dev->rx_buff, *cp++);
  	} else {
  		while (count--) {
  			/* rx not enabled: save the raw bytes and never
  			 * trigger any netif_rx. The received bytes are flushed
  			 * later when we re-enable rx but might be read meanwhile
  			 * by the dongle driver.
  			 */
  			dev->rx_buff.data[dev->rx_buff.len++] = *cp++;
  
  			/* What should we do when the buffer is full? */
  			if (unlikely(dev->rx_buff.len == dev->rx_buff.truesize))
  				dev->rx_buff.len = 0;
  		}
  	}
  
  	return 0;
  }
214ad7843   Adrian Bunk   [IRDA]: kill driv...
587
  EXPORT_SYMBOL(sirdev_receive);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
588
589
590
591
  
  /**********************************************************************/
  
  /* callbacks from network layer */
6518bbb80   Stephen Hemminger   irda: convert to ...
592
593
  static netdev_tx_t sirdev_hard_xmit(struct sk_buff *skb,
  					  struct net_device *ndev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
594
  {
4cf1653aa   Wang Chen   netdevice: safe c...
595
  	struct sir_dev *dev = netdev_priv(ndev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
596
597
598
599
  	unsigned long flags;
  	int actual = 0;
  	int err;
  	s32 speed;
ec634fe32   Patrick McHardy   net: convert rema...
600
  	IRDA_ASSERT(dev != NULL, return NETDEV_TX_OK;);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
601
602
  
  	netif_stop_queue(ndev);
a97a6f107   Harvey Harrison   irda: replace __F...
603
604
  	IRDA_DEBUG(3, "%s(), skb->len = %d
  ", __func__, skb->len);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
605
606
607
608
609
610
611
612
613
614
615
616
  
  	speed = irda_get_next_speed(skb);
  	if ((speed != dev->speed) && (speed != -1)) {
  		if (!skb->len) {
  			err = sirdev_schedule_speed(dev, speed);
  			if (unlikely(err == -EWOULDBLOCK)) {
  				/* Failed to initiate the speed change, likely the fsm
  				 * is still busy (pretty unlikely, but...)
  				 * We refuse to accept the skb and return with the queue
  				 * stopped so the network layer will retry after the
  				 * fsm completes and wakes the queue.
  				 */
5b5481402   Patrick McHardy   net: use symbolic...
617
  				 return NETDEV_TX_BUSY;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
618
619
620
621
622
623
624
625
626
627
628
629
630
  			}
  			else if (unlikely(err)) {
  				/* other fatal error - forget the speed change and
  				 * hope the stack will recover somehow
  				 */
  				 netif_start_queue(ndev);
  			}
  			/* else: success
  			 *	speed change in progress now
  			 *	on completion the queue gets restarted
  			 */
  
  			dev_kfree_skb_any(skb);
6ed106549   Patrick McHardy   net: use NETDEV_T...
631
  			return NETDEV_TX_OK;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
632
633
634
635
636
637
638
639
640
  		} else
  			dev->new_speed = speed;
  	}
  
  	/* Init tx buffer*/
  	dev->tx_buff.data = dev->tx_buff.head;
  
  	/* Check problems */
  	if(spin_is_locked(&dev->tx_lock)) {
a97a6f107   Harvey Harrison   irda: replace __F...
641
642
  		IRDA_DEBUG(3, "%s(), write not completed
  ", __func__);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
  	}
  
  	/* serialize with write completion */
  	spin_lock_irqsave(&dev->tx_lock, flags);
  
          /* Copy skb to tx_buff while wrapping, stuffing and making CRC */
  	dev->tx_buff.len = async_wrap_skb(skb, dev->tx_buff.data, dev->tx_buff.truesize); 
  
  	/* transmission will start now - disable receive.
  	 * if we are just in the middle of an incoming frame,
  	 * treat it as collision. probably it's a good idea to
  	 * reset the rx_buf OUTSIDE_FRAME in this case too?
  	 */
  	atomic_set(&dev->enable_rx, 0);
  	if (unlikely(sirdev_is_receiving(dev)))
af0490810   Stephen Hemminger   irda: convert to ...
658
  		dev->netdev->stats.collisions++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
659
660
661
662
663
  
  	actual = dev->drv->do_write(dev, dev->tx_buff.data, dev->tx_buff.len);
  
  	if (likely(actual > 0)) {
  		dev->tx_skb = skb;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
664
665
666
667
668
669
670
  		dev->tx_buff.data += actual;
  		dev->tx_buff.len -= actual;
  	}
  	else if (unlikely(actual < 0)) {
  		/* could be dropped later when we have tx_timeout to recover */
  		IRDA_ERROR("%s: drv->do_write failed (%d)
  ",
a97a6f107   Harvey Harrison   irda: replace __F...
671
  			   __func__, actual);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
672
  		dev_kfree_skb_any(skb);
af0490810   Stephen Hemminger   irda: convert to ...
673
674
  		dev->netdev->stats.tx_errors++;
  		dev->netdev->stats.tx_dropped++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
675
676
677
  		netif_wake_queue(ndev);
  	}
  	spin_unlock_irqrestore(&dev->tx_lock, flags);
6ed106549   Patrick McHardy   net: use NETDEV_T...
678
  	return NETDEV_TX_OK;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
679
680
681
682
683
684
685
  }
  
  /* called from network layer with rtnl hold */
  
  static int sirdev_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
  {
  	struct if_irda_req *irq = (struct if_irda_req *) rq;
4cf1653aa   Wang Chen   netdevice: safe c...
686
  	struct sir_dev *dev = netdev_priv(ndev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
687
688
689
  	int ret = 0;
  
  	IRDA_ASSERT(dev != NULL, return -1;);
a97a6f107   Harvey Harrison   irda: replace __F...
690
691
  	IRDA_DEBUG(3, "%s(), %s, (cmd=0x%X)
  ", __func__, ndev->name, cmd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
  	
  	switch (cmd) {
  	case SIOCSBANDWIDTH: /* Set bandwidth */
  		if (!capable(CAP_NET_ADMIN))
  			ret = -EPERM;
  		else
  			ret = sirdev_schedule_speed(dev, irq->ifr_baudrate);
  		/* cannot sleep here for completion
  		 * we are called from network layer with rtnl hold
  		 */
  		break;
  
  	case SIOCSDONGLE: /* Set dongle */
  		if (!capable(CAP_NET_ADMIN))
  			ret = -EPERM;
  		else
  			ret = sirdev_schedule_dongle_open(dev, irq->ifr_dongle);
  		/* cannot sleep here for completion
  		 * we are called from network layer with rtnl hold
  		 */
  		break;
  
  	case SIOCSMEDIABUSY: /* Set media busy */
  		if (!capable(CAP_NET_ADMIN))
  			ret = -EPERM;
  		else
  			irda_device_set_media_busy(dev->netdev, TRUE);
  		break;
  
  	case SIOCGRECEIVING: /* Check if we are receiving right now */
  		irq->ifr_receiving = sirdev_is_receiving(dev);
  		break;
  
  	case SIOCSDTRRTS:
  		if (!capable(CAP_NET_ADMIN))
  			ret = -EPERM;
  		else
  			ret = sirdev_schedule_dtr_rts(dev, irq->ifr_dtr, irq->ifr_rts);
  		/* cannot sleep here for completion
  		 * we are called from network layer with rtnl hold
  		 */
  		break;
  
  	case SIOCSMODE:
  #if 0
  		if (!capable(CAP_NET_ADMIN))
  			ret = -EPERM;
  		else
  			ret = sirdev_schedule_mode(dev, irq->ifr_mode);
  		/* cannot sleep here for completion
  		 * we are called from network layer with rtnl hold
  		 */
  		break;
  #endif
  	default:
  		ret = -EOPNOTSUPP;
  	}
  	
  	return ret;
  }
  
  /* ----------------------------------------------------------------------------- */
  
  #define SIRBUF_ALLOCSIZE 4269	/* worst case size of a wrapped IrLAP frame */
  
  static int sirdev_alloc_buffers(struct sir_dev *dev)
  {
  	dev->tx_buff.truesize = SIRBUF_ALLOCSIZE;
  	dev->rx_buff.truesize = IRDA_SKB_MAX_MTU; 
  
  	/* Bootstrap ZeroCopy Rx */
e4e90b210   David S. Miller   irda: Use __netde...
763
764
  	dev->rx_buff.skb = __netdev_alloc_skb(dev->netdev, dev->rx_buff.truesize,
  					      GFP_KERNEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
  	if (dev->rx_buff.skb == NULL)
  		return -ENOMEM;
  	skb_reserve(dev->rx_buff.skb, 1);
  	dev->rx_buff.head = dev->rx_buff.skb->data;
  
  	dev->tx_buff.head = kmalloc(dev->tx_buff.truesize, GFP_KERNEL);
  	if (dev->tx_buff.head == NULL) {
  		kfree_skb(dev->rx_buff.skb);
  		dev->rx_buff.skb = NULL;
  		dev->rx_buff.head = NULL;
  		return -ENOMEM;
  	}
  
  	dev->tx_buff.data = dev->tx_buff.head;
  	dev->rx_buff.data = dev->rx_buff.head;
  	dev->tx_buff.len = 0;
  	dev->rx_buff.len = 0;
  
  	dev->rx_buff.in_frame = FALSE;
  	dev->rx_buff.state = OUTSIDE_FRAME;
  	return 0;
  };
  
  static void sirdev_free_buffers(struct sir_dev *dev)
  {
875b48297   Wei Yongjun   irda: remove some...
790
  	kfree_skb(dev->rx_buff.skb);
b4558ea93   Jesper Juhl   drivers/net: Remo...
791
  	kfree(dev->tx_buff.head);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
792
793
794
795
796
797
  	dev->rx_buff.head = dev->tx_buff.head = NULL;
  	dev->rx_buff.skb = NULL;
  }
  
  static int sirdev_open(struct net_device *ndev)
  {
4cf1653aa   Wang Chen   netdevice: safe c...
798
  	struct sir_dev *dev = netdev_priv(ndev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
799
800
801
802
803
804
805
806
  	const struct sir_driver *drv = dev->drv;
  
  	if (!drv)
  		return -ENODEV;
  
  	/* increase the reference count of the driver module before doing serious stuff */
  	if (!try_module_get(drv->owner))
  		return -ESTALE;
a97a6f107   Harvey Harrison   irda: replace __F...
807
808
  	IRDA_DEBUG(2, "%s()
  ", __func__);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
  
  	if (sirdev_alloc_buffers(dev))
  		goto errout_dec;
  
  	if (!dev->drv->start_dev  ||  dev->drv->start_dev(dev))
  		goto errout_free;
  
  	sirdev_enable_rx(dev);
  	dev->raw_tx = 0;
  
  	netif_start_queue(ndev);
  	dev->irlap = irlap_open(ndev, &dev->qos, dev->hwname);
  	if (!dev->irlap)
  		goto errout_stop;
  
  	netif_wake_queue(ndev);
a97a6f107   Harvey Harrison   irda: replace __F...
825
826
  	IRDA_DEBUG(2, "%s - done, speed = %d
  ", __func__, dev->speed);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
  
  	return 0;
  
  errout_stop:
  	atomic_set(&dev->enable_rx, 0);
  	if (dev->drv->stop_dev)
  		dev->drv->stop_dev(dev);
  errout_free:
  	sirdev_free_buffers(dev);
  errout_dec:
  	module_put(drv->owner);
  	return -EAGAIN;
  }
  
  static int sirdev_close(struct net_device *ndev)
  {
4cf1653aa   Wang Chen   netdevice: safe c...
843
  	struct sir_dev *dev = netdev_priv(ndev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
844
  	const struct sir_driver *drv;
a97a6f107   Harvey Harrison   irda: replace __F...
845
846
  //	IRDA_DEBUG(0, "%s
  ", __func__);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
  
  	netif_stop_queue(ndev);
  
  	down(&dev->fsm.sem);		/* block on pending config completion */
  
  	atomic_set(&dev->enable_rx, 0);
  
  	if (unlikely(!dev->irlap))
  		goto out;
  	irlap_close(dev->irlap);
  	dev->irlap = NULL;
  
  	drv = dev->drv;
  	if (unlikely(!drv  ||  !dev->priv))
  		goto out;
  
  	if (drv->stop_dev)
  		drv->stop_dev(dev);
  
  	sirdev_free_buffers(dev);
  	module_put(drv->owner);
  
  out:
  	dev->speed = 0;
  	up(&dev->fsm.sem);
  	return 0;
  }
2b023f46c   Stephen Hemminger   irda: convert sir...
874
875
876
877
878
879
  static const struct net_device_ops sirdev_ops = {
  	.ndo_start_xmit	= sirdev_hard_xmit,
  	.ndo_open	= sirdev_open,
  	.ndo_stop	= sirdev_close,
  	.ndo_do_ioctl	= sirdev_ioctl,
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
880
881
882
883
884
885
  /* ----------------------------------------------------------------------------- */
  
  struct sir_dev * sirdev_get_instance(const struct sir_driver *drv, const char *name)
  {
  	struct net_device *ndev;
  	struct sir_dev *dev;
a97a6f107   Harvey Harrison   irda: replace __F...
886
887
  	IRDA_DEBUG(0, "%s - %s
  ", __func__, name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
888
889
890
891
892
893
894
895
896
897
898
899
900
  
  	/* instead of adding tests to protect against drv->do_write==NULL
  	 * at several places we refuse to create a sir_dev instance for
  	 * drivers which don't implement do_write.
  	 */
  	if (!drv ||  !drv->do_write)
  		return NULL;
  
  	/*
  	 *  Allocate new instance of the device
  	 */
  	ndev = alloc_irdadev(sizeof(*dev));
  	if (ndev == NULL) {
a97a6f107   Harvey Harrison   irda: replace __F...
901
902
  		IRDA_ERROR("%s - Can't allocate memory for IrDA control block!
  ", __func__);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
903
904
  		goto out;
  	}
4cf1653aa   Wang Chen   netdevice: safe c...
905
  	dev = netdev_priv(ndev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
906
907
908
909
910
911
912
913
914
915
916
917
  
  	irda_init_max_qos_capabilies(&dev->qos);
  	dev->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|IR_115200;
  	dev->qos.min_turn_time.bits = drv->qos_mtt_bits;
  	irda_qos_bits_to_value(&dev->qos);
  
  	strncpy(dev->hwname, name, sizeof(dev->hwname)-1);
  
  	atomic_set(&dev->enable_rx, 0);
  	dev->tx_skb = NULL;
  
  	spin_lock_init(&dev->tx_lock);
db7f859ec   Thomas Gleixner   irda: Semaphore c...
918
  	sema_init(&dev->fsm.sem, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
919

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
920
921
  	dev->drv = drv;
  	dev->netdev = ndev;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
922
  	/* Override the network functions we need to use */
2b023f46c   Stephen Hemminger   irda: convert sir...
923
  	ndev->netdev_ops = &sirdev_ops;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
924
925
  
  	if (register_netdev(ndev)) {
a97a6f107   Harvey Harrison   irda: replace __F...
926
927
  		IRDA_ERROR("%s(), register_netdev() failed!
  ", __func__);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
928
929
930
931
932
933
934
935
936
937
  		goto out_freenetdev;
  	}
  
  	return dev;
  
  out_freenetdev:
  	free_netdev(ndev);
  out:
  	return NULL;
  }
214ad7843   Adrian Bunk   [IRDA]: kill driv...
938
  EXPORT_SYMBOL(sirdev_get_instance);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
939
940
941
942
  
  int sirdev_put_instance(struct sir_dev *dev)
  {
  	int err = 0;
a97a6f107   Harvey Harrison   irda: replace __F...
943
944
  	IRDA_DEBUG(0, "%s
  ", __func__);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
945
946
947
948
949
950
951
952
953
  
  	atomic_set(&dev->enable_rx, 0);
  
  	netif_carrier_off(dev->netdev);
  	netif_device_detach(dev->netdev);
  
  	if (dev->dongle_drv)
  		err = sirdev_schedule_dongle_close(dev);
  	if (err)
a97a6f107   Harvey Harrison   irda: replace __F...
954
955
  		IRDA_ERROR("%s - error %d
  ", __func__, err);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
  
  	sirdev_close(dev->netdev);
  
  	down(&dev->fsm.sem);
  	dev->fsm.state = SIRDEV_STATE_DEAD;	/* mark staled */
  	dev->dongle_drv = NULL;
  	dev->priv = NULL;
  	up(&dev->fsm.sem);
  
  	/* Remove netdevice */
  	unregister_netdev(dev->netdev);
  
  	free_netdev(dev->netdev);
  
  	return 0;
  }
214ad7843   Adrian Bunk   [IRDA]: kill driv...
972
  EXPORT_SYMBOL(sirdev_put_instance);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
973

788252e66   Christoph Hellwig   [IRDA]: Switching...
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
  static int __init sir_wq_init(void)
  {
  	irda_sir_wq = create_singlethread_workqueue("irda_sir_wq");
  	if (!irda_sir_wq)
  		return -ENOMEM;
  	return 0;
  }
  
  static void __exit sir_wq_exit(void)
  {
  	destroy_workqueue(irda_sir_wq);
  }
  
  module_init(sir_wq_init);
  module_exit(sir_wq_exit);
  
  MODULE_AUTHOR("Martin Diehl <info@mdiehl.de>");
  MODULE_DESCRIPTION("IrDA SIR core");
  MODULE_LICENSE("GPL");