]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /********************************************************************* |
2 | * | |
3 | * Filename: irport.c | |
4 | * Version: 1.0 | |
5 | * Description: Half duplex serial port SIR driver for IrDA. | |
6 | * Status: Experimental. | |
7 | * Author: Dag Brattli <dagb@cs.uit.no> | |
8 | * Created at: Sun Aug 3 13:49:59 1997 | |
9 | * Modified at: Fri Jan 28 20:22:38 2000 | |
10 | * Modified by: Dag Brattli <dagb@cs.uit.no> | |
11 | * Sources: serial.c by Linus Torvalds | |
12 | * | |
13 | * Copyright (c) 1997, 1998, 1999-2000 Dag Brattli, All Rights Reserved. | |
14 | * Copyright (c) 2000-2003 Jean Tourrilhes, All Rights Reserved. | |
15 | * | |
16 | * This program is free software; you can redistribute it and/or | |
17 | * modify it under the terms of the GNU General Public License as | |
18 | * published by the Free Software Foundation; either version 2 of | |
19 | * the License, or (at your option) any later version. | |
20 | * | |
21 | * This program is distributed in the hope that it will be useful, | |
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
24 | * GNU General Public License for more details. | |
25 | * | |
26 | * You should have received a copy of the GNU General Public License | |
27 | * along with this program; if not, write to the Free Software | |
28 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, | |
29 | * MA 02111-1307 USA | |
30 | * | |
31 | * This driver is ment to be a small half duplex serial driver to be | |
32 | * used for IR-chipsets that has a UART (16550) compatibility mode. | |
33 | * Eventually it will replace irtty, because of irtty has some | |
34 | * problems that is hard to get around when we don't have control | |
35 | * over the serial driver. This driver may also be used by FIR | |
36 | * drivers to handle SIR mode for them. | |
37 | * | |
38 | ********************************************************************/ | |
39 | ||
40 | #include <linux/module.h> | |
41 | ||
42 | #include <linux/kernel.h> | |
43 | #include <linux/types.h> | |
44 | #include <linux/ioport.h> | |
45 | #include <linux/slab.h> | |
46 | #include <linux/string.h> | |
47 | #include <linux/skbuff.h> | |
48 | #include <linux/serial_reg.h> | |
49 | #include <linux/errno.h> | |
50 | #include <linux/init.h> | |
51 | #include <linux/spinlock.h> | |
52 | #include <linux/delay.h> | |
53 | #include <linux/rtnetlink.h> | |
54 | #include <linux/bitops.h> | |
55 | ||
56 | #include <asm/system.h> | |
57 | #include <asm/io.h> | |
58 | ||
59 | #include <net/irda/irda.h> | |
60 | #include <net/irda/wrapper.h> | |
61 | #include "irport.h" | |
62 | ||
63 | #define IO_EXTENT 8 | |
64 | ||
65 | /* | |
66 | * Currently you'll need to set these values using insmod like this: | |
67 | * insmod irport io=0x3e8 irq=11 | |
68 | */ | |
69 | static unsigned int io[] = { ~0, ~0, ~0, ~0 }; | |
70 | static unsigned int irq[] = { 0, 0, 0, 0 }; | |
71 | ||
72 | static unsigned int qos_mtt_bits = 0x03; | |
73 | ||
74 | static struct irport_cb *dev_self[] = { NULL, NULL, NULL, NULL}; | |
75 | static char *driver_name = "irport"; | |
76 | ||
77 | static inline void irport_write_wakeup(struct irport_cb *self); | |
78 | static inline int irport_write(int iobase, int fifo_size, __u8 *buf, int len); | |
79 | static inline void irport_receive(struct irport_cb *self); | |
80 | ||
81 | static int irport_net_ioctl(struct net_device *dev, struct ifreq *rq, | |
82 | int cmd); | |
83 | static inline int irport_is_receiving(struct irport_cb *self); | |
84 | static int irport_set_dtr_rts(struct net_device *dev, int dtr, int rts); | |
85 | static int irport_raw_write(struct net_device *dev, __u8 *buf, int len); | |
86 | static struct net_device_stats *irport_net_get_stats(struct net_device *dev); | |
87 | static int irport_change_speed_complete(struct irda_task *task); | |
88 | static void irport_timeout(struct net_device *dev); | |
89 | ||
90 | static irqreturn_t irport_interrupt(int irq, void *dev_id, | |
91 | struct pt_regs *regs); | |
92 | static int irport_hard_xmit(struct sk_buff *skb, struct net_device *dev); | |
93 | static void irport_change_speed(void *priv, __u32 speed); | |
94 | static int irport_net_open(struct net_device *dev); | |
95 | static int irport_net_close(struct net_device *dev); | |
96 | ||
97 | static struct irport_cb * | |
98 | irport_open(int i, unsigned int iobase, unsigned int irq) | |
99 | { | |
100 | struct net_device *dev; | |
101 | struct irport_cb *self; | |
102 | ||
103 | IRDA_DEBUG(1, "%s()\n", __FUNCTION__); | |
104 | ||
105 | /* Lock the port that we need */ | |
106 | if (!request_region(iobase, IO_EXTENT, driver_name)) { | |
107 | IRDA_DEBUG(0, "%s(), can't get iobase of 0x%03x\n", | |
108 | __FUNCTION__, iobase); | |
109 | goto err_out1; | |
110 | } | |
111 | ||
112 | /* | |
113 | * Allocate new instance of the driver | |
114 | */ | |
115 | dev = alloc_irdadev(sizeof(struct irport_cb)); | |
116 | if (!dev) { | |
117 | IRDA_ERROR("%s(), can't allocate memory for " | |
118 | "irda device!\n", __FUNCTION__); | |
119 | goto err_out2; | |
120 | } | |
121 | ||
122 | self = dev->priv; | |
123 | spin_lock_init(&self->lock); | |
124 | ||
125 | /* Need to store self somewhere */ | |
126 | dev_self[i] = self; | |
127 | self->priv = self; | |
128 | self->index = i; | |
129 | ||
130 | /* Initialize IO */ | |
131 | self->io.sir_base = iobase; | |
132 | self->io.sir_ext = IO_EXTENT; | |
133 | self->io.irq = irq; | |
134 | self->io.fifo_size = 16; /* 16550A and compatible */ | |
135 | ||
136 | /* Initialize QoS for this device */ | |
137 | irda_init_max_qos_capabilies(&self->qos); | |
138 | ||
139 | self->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600| | |
140 | IR_115200; | |
141 | ||
142 | self->qos.min_turn_time.bits = qos_mtt_bits; | |
143 | irda_qos_bits_to_value(&self->qos); | |
144 | ||
145 | /* Bootstrap ZeroCopy Rx */ | |
146 | self->rx_buff.truesize = IRDA_SKB_MAX_MTU; | |
147 | self->rx_buff.skb = __dev_alloc_skb(self->rx_buff.truesize, | |
148 | GFP_KERNEL); | |
149 | if (self->rx_buff.skb == NULL) { | |
150 | IRDA_ERROR("%s(), can't allocate memory for " | |
151 | "receive buffer!\n", __FUNCTION__); | |
152 | goto err_out3; | |
153 | } | |
154 | skb_reserve(self->rx_buff.skb, 1); | |
155 | self->rx_buff.head = self->rx_buff.skb->data; | |
156 | /* No need to memset the buffer, unless you are really pedantic */ | |
157 | ||
158 | /* Finish setup the Rx buffer descriptor */ | |
159 | self->rx_buff.in_frame = FALSE; | |
160 | self->rx_buff.state = OUTSIDE_FRAME; | |
161 | self->rx_buff.data = self->rx_buff.head; | |
162 | ||
163 | /* Specify how much memory we want */ | |
164 | self->tx_buff.truesize = 4000; | |
165 | ||
166 | /* Allocate memory if needed */ | |
167 | if (self->tx_buff.truesize > 0) { | |
168 | self->tx_buff.head = (__u8 *) kmalloc(self->tx_buff.truesize, | |
169 | GFP_KERNEL); | |
170 | if (self->tx_buff.head == NULL) { | |
171 | IRDA_ERROR("%s(), can't allocate memory for " | |
172 | "transmit buffer!\n", __FUNCTION__); | |
173 | goto err_out4; | |
174 | } | |
175 | memset(self->tx_buff.head, 0, self->tx_buff.truesize); | |
176 | } | |
177 | self->tx_buff.data = self->tx_buff.head; | |
178 | ||
179 | self->netdev = dev; | |
180 | /* Keep track of module usage */ | |
181 | SET_MODULE_OWNER(dev); | |
182 | ||
183 | /* May be overridden by piggyback drivers */ | |
184 | self->interrupt = irport_interrupt; | |
185 | self->change_speed = irport_change_speed; | |
186 | ||
187 | /* Override the network functions we need to use */ | |
188 | dev->hard_start_xmit = irport_hard_xmit; | |
189 | dev->tx_timeout = irport_timeout; | |
190 | dev->watchdog_timeo = HZ; /* Allow time enough for speed change */ | |
191 | dev->open = irport_net_open; | |
192 | dev->stop = irport_net_close; | |
193 | dev->get_stats = irport_net_get_stats; | |
194 | dev->do_ioctl = irport_net_ioctl; | |
195 | ||
196 | /* Make ifconfig display some details */ | |
197 | dev->base_addr = iobase; | |
198 | dev->irq = irq; | |
199 | ||
200 | if (register_netdev(dev)) { | |
201 | IRDA_ERROR("%s(), register_netdev() failed!\n", __FUNCTION__); | |
202 | goto err_out5; | |
203 | } | |
204 | IRDA_MESSAGE("IrDA: Registered device %s (irport io=0x%X irq=%d)\n", | |
205 | dev->name, iobase, irq); | |
206 | ||
207 | return self; | |
208 | err_out5: | |
209 | kfree(self->tx_buff.head); | |
210 | err_out4: | |
211 | kfree_skb(self->rx_buff.skb); | |
212 | err_out3: | |
213 | free_netdev(dev); | |
214 | dev_self[i] = NULL; | |
215 | err_out2: | |
216 | release_region(iobase, IO_EXTENT); | |
217 | err_out1: | |
218 | return NULL; | |
219 | } | |
220 | ||
221 | static int irport_close(struct irport_cb *self) | |
222 | { | |
223 | IRDA_ASSERT(self != NULL, return -1;); | |
224 | ||
225 | /* We are not using any dongle anymore! */ | |
226 | if (self->dongle) | |
227 | irda_device_dongle_cleanup(self->dongle); | |
228 | self->dongle = NULL; | |
229 | ||
230 | /* Remove netdevice */ | |
231 | unregister_netdev(self->netdev); | |
232 | ||
233 | /* Release the IO-port that this driver is using */ | |
234 | IRDA_DEBUG(0 , "%s(), Releasing Region %03x\n", | |
235 | __FUNCTION__, self->io.sir_base); | |
236 | release_region(self->io.sir_base, self->io.sir_ext); | |
237 | ||
238 | if (self->tx_buff.head) | |
239 | kfree(self->tx_buff.head); | |
240 | ||
241 | if (self->rx_buff.skb) | |
242 | kfree_skb(self->rx_buff.skb); | |
243 | self->rx_buff.skb = NULL; | |
244 | ||
245 | /* Remove ourselves */ | |
246 | dev_self[self->index] = NULL; | |
247 | free_netdev(self->netdev); | |
248 | ||
249 | return 0; | |
250 | } | |
251 | ||
252 | static void irport_stop(struct irport_cb *self) | |
253 | { | |
254 | int iobase; | |
255 | ||
256 | iobase = self->io.sir_base; | |
257 | ||
258 | /* We can't lock, we may be called from a FIR driver - Jean II */ | |
259 | ||
260 | /* We are not transmitting any more */ | |
261 | self->transmitting = 0; | |
262 | ||
263 | /* Reset UART */ | |
264 | outb(0, iobase+UART_MCR); | |
265 | ||
266 | /* Turn off interrupts */ | |
267 | outb(0, iobase+UART_IER); | |
268 | } | |
269 | ||
270 | static void irport_start(struct irport_cb *self) | |
271 | { | |
272 | int iobase; | |
273 | ||
274 | iobase = self->io.sir_base; | |
275 | ||
276 | irport_stop(self); | |
277 | ||
278 | /* We can't lock, we may be called from a FIR driver - Jean II */ | |
279 | ||
280 | /* Initialize UART */ | |
281 | outb(UART_LCR_WLEN8, iobase+UART_LCR); /* Reset DLAB */ | |
282 | outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), iobase+UART_MCR); | |
283 | ||
284 | /* Turn on interrups */ | |
285 | outb(UART_IER_RLSI | UART_IER_RDI |UART_IER_THRI, iobase+UART_IER); | |
286 | } | |
287 | ||
288 | /* | |
289 | * Function irport_probe (void) | |
290 | * | |
291 | * Start IO port | |
292 | * | |
293 | */ | |
294 | int irport_probe(int iobase) | |
295 | { | |
296 | IRDA_DEBUG(4, "%s(), iobase=%#x\n", __FUNCTION__, iobase); | |
297 | ||
298 | return 0; | |
299 | } | |
300 | ||
301 | /* | |
302 | * Function irport_get_fcr (speed) | |
303 | * | |
304 | * Compute value of fcr | |
305 | * | |
306 | */ | |
307 | static inline unsigned int irport_get_fcr(__u32 speed) | |
308 | { | |
309 | unsigned int fcr; /* FIFO control reg */ | |
310 | ||
311 | /* Enable fifos */ | |
312 | fcr = UART_FCR_ENABLE_FIFO; | |
313 | ||
314 | /* | |
315 | * Use trigger level 1 to avoid 3 ms. timeout delay at 9600 bps, and | |
316 | * almost 1,7 ms at 19200 bps. At speeds above that we can just forget | |
317 | * about this timeout since it will always be fast enough. | |
318 | */ | |
319 | if (speed < 38400) | |
320 | fcr |= UART_FCR_TRIGGER_1; | |
321 | else | |
322 | //fcr |= UART_FCR_TRIGGER_14; | |
323 | fcr |= UART_FCR_TRIGGER_8; | |
324 | ||
325 | return(fcr); | |
326 | } | |
327 | ||
328 | /* | |
329 | * Function irport_change_speed (self, speed) | |
330 | * | |
331 | * Set speed of IrDA port to specified baudrate | |
332 | * | |
333 | * This function should be called with irq off and spin-lock. | |
334 | */ | |
335 | static void irport_change_speed(void *priv, __u32 speed) | |
336 | { | |
337 | struct irport_cb *self = (struct irport_cb *) priv; | |
338 | int iobase; | |
339 | unsigned int fcr; /* FIFO control reg */ | |
340 | unsigned int lcr; /* Line control reg */ | |
341 | int divisor; | |
342 | ||
343 | IRDA_ASSERT(self != NULL, return;); | |
344 | IRDA_ASSERT(speed != 0, return;); | |
345 | ||
346 | IRDA_DEBUG(1, "%s(), Setting speed to: %d - iobase=%#x\n", | |
347 | __FUNCTION__, speed, self->io.sir_base); | |
348 | ||
349 | /* We can't lock, we may be called from a FIR driver - Jean II */ | |
350 | ||
351 | iobase = self->io.sir_base; | |
352 | ||
353 | /* Update accounting for new speed */ | |
354 | self->io.speed = speed; | |
355 | ||
356 | /* Turn off interrupts */ | |
357 | outb(0, iobase+UART_IER); | |
358 | ||
359 | divisor = SPEED_MAX/speed; | |
360 | ||
361 | /* Get proper fifo configuration */ | |
362 | fcr = irport_get_fcr(speed); | |
363 | ||
364 | /* IrDA ports use 8N1 */ | |
365 | lcr = UART_LCR_WLEN8; | |
366 | ||
367 | outb(UART_LCR_DLAB | lcr, iobase+UART_LCR); /* Set DLAB */ | |
368 | outb(divisor & 0xff, iobase+UART_DLL); /* Set speed */ | |
369 | outb(divisor >> 8, iobase+UART_DLM); | |
370 | outb(lcr, iobase+UART_LCR); /* Set 8N1 */ | |
371 | outb(fcr, iobase+UART_FCR); /* Enable FIFO's */ | |
372 | ||
373 | /* Turn on interrups */ | |
374 | /* This will generate a fatal interrupt storm. | |
375 | * People calling us will do that properly - Jean II */ | |
376 | //outb(/*UART_IER_RLSI|*/UART_IER_RDI/*|UART_IER_THRI*/, iobase+UART_IER); | |
377 | } | |
378 | ||
379 | /* | |
380 | * Function __irport_change_speed (instance, state, param) | |
381 | * | |
382 | * State machine for changing speed of the device. We do it this way since | |
383 | * we cannot use schedule_timeout() when we are in interrupt context | |
384 | * | |
385 | */ | |
386 | int __irport_change_speed(struct irda_task *task) | |
387 | { | |
388 | struct irport_cb *self; | |
389 | __u32 speed = (__u32) task->param; | |
390 | unsigned long flags = 0; | |
391 | int wasunlocked = 0; | |
392 | int ret = 0; | |
393 | ||
394 | IRDA_DEBUG(2, "%s(), <%ld>\n", __FUNCTION__, jiffies); | |
395 | ||
396 | self = (struct irport_cb *) task->instance; | |
397 | ||
398 | IRDA_ASSERT(self != NULL, return -1;); | |
399 | ||
400 | /* Locking notes : this function may be called from irq context with | |
401 | * spinlock, via irport_write_wakeup(), or from non-interrupt without | |
402 | * spinlock (from the task timer). Yuck ! | |
403 | * This is ugly, and unsafe is the spinlock is not already aquired. | |
404 | * This will be fixed when irda-task get rewritten. | |
405 | * Jean II */ | |
406 | if (!spin_is_locked(&self->lock)) { | |
407 | spin_lock_irqsave(&self->lock, flags); | |
408 | wasunlocked = 1; | |
409 | } | |
410 | ||
411 | switch (task->state) { | |
412 | case IRDA_TASK_INIT: | |
413 | case IRDA_TASK_WAIT: | |
414 | /* Are we ready to change speed yet? */ | |
415 | if (self->tx_buff.len > 0) { | |
416 | task->state = IRDA_TASK_WAIT; | |
417 | ||
418 | /* Try again later */ | |
419 | ret = msecs_to_jiffies(20); | |
420 | break; | |
421 | } | |
422 | ||
423 | if (self->dongle) | |
424 | irda_task_next_state(task, IRDA_TASK_CHILD_INIT); | |
425 | else | |
426 | irda_task_next_state(task, IRDA_TASK_CHILD_DONE); | |
427 | break; | |
428 | case IRDA_TASK_CHILD_INIT: | |
429 | /* Go to default speed */ | |
430 | self->change_speed(self->priv, 9600); | |
431 | ||
432 | /* Change speed of dongle */ | |
433 | if (irda_task_execute(self->dongle, | |
434 | self->dongle->issue->change_speed, | |
435 | NULL, task, (void *) speed)) | |
436 | { | |
437 | /* Dongle need more time to change its speed */ | |
438 | irda_task_next_state(task, IRDA_TASK_CHILD_WAIT); | |
439 | ||
440 | /* Give dongle 1 sec to finish */ | |
441 | ret = msecs_to_jiffies(1000); | |
442 | } else | |
443 | /* Child finished immediately */ | |
444 | irda_task_next_state(task, IRDA_TASK_CHILD_DONE); | |
445 | break; | |
446 | case IRDA_TASK_CHILD_WAIT: | |
447 | IRDA_WARNING("%s(), changing speed of dongle timed out!\n", __FUNCTION__); | |
448 | ret = -1; | |
449 | break; | |
450 | case IRDA_TASK_CHILD_DONE: | |
451 | /* Finally we are ready to change the speed */ | |
452 | self->change_speed(self->priv, speed); | |
453 | ||
454 | irda_task_next_state(task, IRDA_TASK_DONE); | |
455 | break; | |
456 | default: | |
457 | IRDA_ERROR("%s(), unknown state %d\n", | |
458 | __FUNCTION__, task->state); | |
459 | irda_task_next_state(task, IRDA_TASK_DONE); | |
460 | ret = -1; | |
461 | break; | |
462 | } | |
463 | /* Put stuff in the state we found them - Jean II */ | |
464 | if(wasunlocked) { | |
465 | spin_unlock_irqrestore(&self->lock, flags); | |
466 | } | |
467 | ||
468 | return ret; | |
469 | } | |
470 | ||
471 | /* | |
472 | * Function irport_change_speed_complete (task) | |
473 | * | |
474 | * Called when the change speed operation completes | |
475 | * | |
476 | */ | |
477 | static int irport_change_speed_complete(struct irda_task *task) | |
478 | { | |
479 | struct irport_cb *self; | |
480 | ||
481 | IRDA_DEBUG(1, "%s()\n", __FUNCTION__); | |
482 | ||
483 | self = (struct irport_cb *) task->instance; | |
484 | ||
485 | IRDA_ASSERT(self != NULL, return -1;); | |
486 | IRDA_ASSERT(self->netdev != NULL, return -1;); | |
487 | ||
488 | /* Finished changing speed, so we are not busy any longer */ | |
489 | /* Signal network layer so it can try to send the frame */ | |
490 | ||
491 | netif_wake_queue(self->netdev); | |
492 | ||
493 | return 0; | |
494 | } | |
495 | ||
496 | /* | |
497 | * Function irport_timeout (struct net_device *dev) | |
498 | * | |
499 | * The networking layer thinks we timed out. | |
500 | * | |
501 | */ | |
502 | ||
503 | static void irport_timeout(struct net_device *dev) | |
504 | { | |
505 | struct irport_cb *self; | |
506 | int iobase; | |
507 | int iir, lsr; | |
508 | unsigned long flags; | |
509 | ||
510 | self = (struct irport_cb *) dev->priv; | |
511 | IRDA_ASSERT(self != NULL, return;); | |
512 | iobase = self->io.sir_base; | |
513 | ||
514 | IRDA_WARNING("%s: transmit timed out, jiffies = %ld, trans_start = %ld\n", | |
515 | dev->name, jiffies, dev->trans_start); | |
516 | spin_lock_irqsave(&self->lock, flags); | |
517 | ||
518 | /* Debug what's happening... */ | |
519 | ||
520 | /* Get interrupt status */ | |
521 | lsr = inb(iobase+UART_LSR); | |
522 | /* Read interrupt register */ | |
523 | iir = inb(iobase+UART_IIR); | |
524 | IRDA_DEBUG(0, "%s(), iir=%02x, lsr=%02x, iobase=%#x\n", | |
525 | __FUNCTION__, iir, lsr, iobase); | |
526 | ||
527 | IRDA_DEBUG(0, "%s(), transmitting=%d, remain=%d, done=%d\n", | |
528 | __FUNCTION__, self->transmitting, self->tx_buff.len, | |
529 | self->tx_buff.data - self->tx_buff.head); | |
530 | ||
531 | /* Now, restart the port */ | |
532 | irport_start(self); | |
533 | self->change_speed(self->priv, self->io.speed); | |
534 | /* This will re-enable irqs */ | |
535 | outb(/*UART_IER_RLSI|*/UART_IER_RDI/*|UART_IER_THRI*/, iobase+UART_IER); | |
536 | dev->trans_start = jiffies; | |
537 | spin_unlock_irqrestore(&self->lock, flags); | |
538 | ||
539 | netif_wake_queue(dev); | |
540 | } | |
541 | ||
542 | /* | |
543 | * Function irport_wait_hw_transmitter_finish () | |
544 | * | |
545 | * Wait for the real end of HW transmission | |
546 | * | |
547 | * The UART is a strict FIFO, and we get called only when we have finished | |
548 | * pushing data to the FIFO, so the maximum amount of time we must wait | |
549 | * is only for the FIFO to drain out. | |
550 | * | |
551 | * We use a simple calibrated loop. We may need to adjust the loop | |
552 | * delay (udelay) to balance I/O traffic and latency. And we also need to | |
553 | * adjust the maximum timeout. | |
554 | * It would probably be better to wait for the proper interrupt, | |
555 | * but it doesn't seem to be available. | |
556 | * | |
557 | * We can't use jiffies or kernel timers because : | |
558 | * 1) We are called from the interrupt handler, which disable softirqs, | |
559 | * so jiffies won't be increased | |
560 | * 2) Jiffies granularity is usually very coarse (10ms), and we don't | |
561 | * want to wait that long to detect stuck hardware. | |
562 | * Jean II | |
563 | */ | |
564 | ||
565 | static void irport_wait_hw_transmitter_finish(struct irport_cb *self) | |
566 | { | |
567 | int iobase; | |
568 | int count = 1000; /* 1 ms */ | |
569 | ||
570 | iobase = self->io.sir_base; | |
571 | ||
572 | /* Calibrated busy loop */ | |
573 | while((count-- > 0) && !(inb(iobase+UART_LSR) & UART_LSR_TEMT)) | |
574 | udelay(1); | |
575 | ||
576 | if(count == 0) | |
577 | IRDA_DEBUG(0, "%s(): stuck transmitter\n", __FUNCTION__); | |
578 | } | |
579 | ||
580 | /* | |
581 | * Function irport_hard_start_xmit (struct sk_buff *skb, struct net_device *dev) | |
582 | * | |
583 | * Transmits the current frame until FIFO is full, then | |
584 | * waits until the next transmitt interrupt, and continues until the | |
585 | * frame is transmitted. | |
586 | */ | |
587 | static int irport_hard_xmit(struct sk_buff *skb, struct net_device *dev) | |
588 | { | |
589 | struct irport_cb *self; | |
590 | unsigned long flags; | |
591 | int iobase; | |
592 | s32 speed; | |
593 | ||
594 | IRDA_DEBUG(1, "%s()\n", __FUNCTION__); | |
595 | ||
596 | IRDA_ASSERT(dev != NULL, return 0;); | |
597 | ||
598 | self = (struct irport_cb *) dev->priv; | |
599 | IRDA_ASSERT(self != NULL, return 0;); | |
600 | ||
601 | iobase = self->io.sir_base; | |
602 | ||
603 | netif_stop_queue(dev); | |
604 | ||
605 | /* Make sure tests & speed change are atomic */ | |
606 | spin_lock_irqsave(&self->lock, flags); | |
607 | ||
608 | /* Check if we need to change the speed */ | |
609 | speed = irda_get_next_speed(skb); | |
610 | if ((speed != self->io.speed) && (speed != -1)) { | |
611 | /* Check for empty frame */ | |
612 | if (!skb->len) { | |
613 | /* | |
614 | * We send frames one by one in SIR mode (no | |
615 | * pipelining), so at this point, if we were sending | |
616 | * a previous frame, we just received the interrupt | |
617 | * telling us it is finished (UART_IIR_THRI). | |
618 | * Therefore, waiting for the transmitter to really | |
619 | * finish draining the fifo won't take too long. | |
620 | * And the interrupt handler is not expected to run. | |
621 | * - Jean II */ | |
622 | irport_wait_hw_transmitter_finish(self); | |
623 | /* Better go there already locked - Jean II */ | |
624 | irda_task_execute(self, __irport_change_speed, | |
625 | irport_change_speed_complete, | |
626 | NULL, (void *) speed); | |
627 | dev->trans_start = jiffies; | |
628 | spin_unlock_irqrestore(&self->lock, flags); | |
629 | dev_kfree_skb(skb); | |
630 | return 0; | |
631 | } else | |
632 | self->new_speed = speed; | |
633 | } | |
634 | ||
635 | /* Init tx buffer */ | |
636 | self->tx_buff.data = self->tx_buff.head; | |
637 | ||
638 | /* Copy skb to tx_buff while wrapping, stuffing and making CRC */ | |
639 | self->tx_buff.len = async_wrap_skb(skb, self->tx_buff.data, | |
640 | self->tx_buff.truesize); | |
641 | ||
642 | self->stats.tx_bytes += self->tx_buff.len; | |
643 | ||
644 | /* We are transmitting */ | |
645 | self->transmitting = 1; | |
646 | ||
647 | /* Turn on transmit finished interrupt. Will fire immediately! */ | |
648 | outb(UART_IER_THRI, iobase+UART_IER); | |
649 | ||
650 | dev->trans_start = jiffies; | |
651 | spin_unlock_irqrestore(&self->lock, flags); | |
652 | ||
653 | dev_kfree_skb(skb); | |
654 | ||
655 | return 0; | |
656 | } | |
657 | ||
658 | /* | |
659 | * Function irport_write (driver) | |
660 | * | |
661 | * Fill Tx FIFO with transmit data | |
662 | * | |
663 | * Called only from irport_write_wakeup() | |
664 | */ | |
665 | static inline int irport_write(int iobase, int fifo_size, __u8 *buf, int len) | |
666 | { | |
667 | int actual = 0; | |
668 | ||
669 | /* Fill FIFO with current frame */ | |
670 | while ((actual < fifo_size) && (actual < len)) { | |
671 | /* Transmit next byte */ | |
672 | outb(buf[actual], iobase+UART_TX); | |
673 | ||
674 | actual++; | |
675 | } | |
676 | ||
677 | return actual; | |
678 | } | |
679 | ||
680 | /* | |
681 | * Function irport_write_wakeup (tty) | |
682 | * | |
683 | * Called by the driver when there's room for more data. If we have | |
684 | * more packets to send, we send them here. | |
685 | * | |
686 | * Called only from irport_interrupt() | |
687 | * Make sure this function is *not* called while we are receiving, | |
688 | * otherwise we will reset fifo and loose data :-( | |
689 | */ | |
690 | static inline void irport_write_wakeup(struct irport_cb *self) | |
691 | { | |
692 | int actual = 0; | |
693 | int iobase; | |
694 | unsigned int fcr; | |
695 | ||
696 | IRDA_ASSERT(self != NULL, return;); | |
697 | ||
698 | IRDA_DEBUG(4, "%s()\n", __FUNCTION__); | |
699 | ||
700 | iobase = self->io.sir_base; | |
701 | ||
702 | /* Finished with frame? */ | |
703 | if (self->tx_buff.len > 0) { | |
704 | /* Write data left in transmit buffer */ | |
705 | actual = irport_write(iobase, self->io.fifo_size, | |
706 | self->tx_buff.data, self->tx_buff.len); | |
707 | self->tx_buff.data += actual; | |
708 | self->tx_buff.len -= actual; | |
709 | } else { | |
710 | /* | |
711 | * Now serial buffer is almost free & we can start | |
712 | * transmission of another packet. But first we must check | |
713 | * if we need to change the speed of the hardware | |
714 | */ | |
715 | if (self->new_speed) { | |
716 | irport_wait_hw_transmitter_finish(self); | |
717 | irda_task_execute(self, __irport_change_speed, | |
718 | irport_change_speed_complete, | |
719 | NULL, (void *) self->new_speed); | |
720 | self->new_speed = 0; | |
721 | } else { | |
722 | /* Tell network layer that we want more frames */ | |
723 | netif_wake_queue(self->netdev); | |
724 | } | |
725 | self->stats.tx_packets++; | |
726 | ||
727 | /* | |
728 | * Reset Rx FIFO to make sure that all reflected transmit data | |
729 | * is discarded. This is needed for half duplex operation | |
730 | */ | |
731 | fcr = irport_get_fcr(self->io.speed); | |
732 | fcr |= UART_FCR_CLEAR_RCVR; | |
733 | outb(fcr, iobase+UART_FCR); | |
734 | ||
735 | /* Finished transmitting */ | |
736 | self->transmitting = 0; | |
737 | ||
738 | /* Turn on receive interrupts */ | |
739 | outb(UART_IER_RDI, iobase+UART_IER); | |
740 | ||
741 | IRDA_DEBUG(1, "%s() : finished Tx\n", __FUNCTION__); | |
742 | } | |
743 | } | |
744 | ||
745 | /* | |
746 | * Function irport_receive (self) | |
747 | * | |
748 | * Receive one frame from the infrared port | |
749 | * | |
750 | * Called only from irport_interrupt() | |
751 | */ | |
752 | static inline void irport_receive(struct irport_cb *self) | |
753 | { | |
754 | int boguscount = 0; | |
755 | int iobase; | |
756 | ||
757 | IRDA_ASSERT(self != NULL, return;); | |
758 | ||
759 | iobase = self->io.sir_base; | |
760 | ||
761 | /* | |
762 | * Receive all characters in Rx FIFO, unwrap and unstuff them. | |
763 | * async_unwrap_char will deliver all found frames | |
764 | */ | |
765 | do { | |
766 | async_unwrap_char(self->netdev, &self->stats, &self->rx_buff, | |
767 | inb(iobase+UART_RX)); | |
768 | ||
769 | /* Make sure we don't stay here too long */ | |
770 | if (boguscount++ > 32) { | |
771 | IRDA_DEBUG(2,"%s(), breaking!\n", __FUNCTION__); | |
772 | break; | |
773 | } | |
774 | } while (inb(iobase+UART_LSR) & UART_LSR_DR); | |
775 | } | |
776 | ||
777 | /* | |
778 | * Function irport_interrupt (irq, dev_id, regs) | |
779 | * | |
780 | * Interrupt handler | |
781 | */ | |
782 | static irqreturn_t irport_interrupt(int irq, void *dev_id, | |
783 | struct pt_regs *regs) | |
784 | { | |
785 | struct net_device *dev = (struct net_device *) dev_id; | |
786 | struct irport_cb *self; | |
787 | int boguscount = 0; | |
788 | int iobase; | |
789 | int iir, lsr; | |
790 | int handled = 0; | |
791 | ||
792 | if (!dev) { | |
793 | IRDA_WARNING("%s() irq %d for unknown device.\n", __FUNCTION__, irq); | |
794 | return IRQ_NONE; | |
795 | } | |
796 | self = (struct irport_cb *) dev->priv; | |
797 | ||
798 | spin_lock(&self->lock); | |
799 | ||
800 | iobase = self->io.sir_base; | |
801 | ||
802 | /* Cut'n'paste interrupt routine from serial.c | |
803 | * This version try to minimise latency and I/O operations. | |
804 | * Simplified and modified to enforce half duplex operation. | |
805 | * - Jean II */ | |
806 | ||
807 | /* Check status even is iir reg is cleared, more robust and | |
808 | * eliminate a read on the I/O bus - Jean II */ | |
809 | do { | |
810 | /* Get interrupt status ; Clear interrupt */ | |
811 | lsr = inb(iobase+UART_LSR); | |
812 | ||
813 | /* Are we receiving or transmitting ? */ | |
814 | if(!self->transmitting) { | |
815 | /* Received something ? */ | |
816 | if (lsr & UART_LSR_DR) | |
817 | irport_receive(self); | |
818 | } else { | |
819 | /* Room in Tx fifo ? */ | |
820 | if (lsr & (UART_LSR_THRE | UART_LSR_TEMT)) | |
821 | irport_write_wakeup(self); | |
822 | } | |
823 | ||
824 | /* A bit hackish, but working as expected... Jean II */ | |
825 | if(lsr & (UART_LSR_THRE | UART_LSR_TEMT | UART_LSR_DR)) | |
826 | handled = 1; | |
827 | ||
828 | /* Make sure we don't stay here to long */ | |
829 | if (boguscount++ > 10) { | |
830 | IRDA_WARNING("%s() irq handler looping : lsr=%02x\n", | |
831 | __FUNCTION__, lsr); | |
832 | break; | |
833 | } | |
834 | ||
835 | /* Read interrupt register */ | |
836 | iir = inb(iobase+UART_IIR); | |
837 | ||
838 | /* Enable this debug only when no other options and at low | |
839 | * bit rates, otherwise it may cause Rx overruns (lsr=63). | |
840 | * - Jean II */ | |
841 | IRDA_DEBUG(6, "%s(), iir=%02x, lsr=%02x, iobase=%#x\n", | |
842 | __FUNCTION__, iir, lsr, iobase); | |
843 | ||
844 | /* As long as interrupt pending... */ | |
845 | } while ((iir & UART_IIR_NO_INT) == 0); | |
846 | ||
847 | spin_unlock(&self->lock); | |
848 | return IRQ_RETVAL(handled); | |
849 | } | |
850 | ||
851 | /* | |
852 | * Function irport_net_open (dev) | |
853 | * | |
854 | * Network device is taken up. Usually this is done by "ifconfig irda0 up" | |
855 | * | |
856 | */ | |
857 | static int irport_net_open(struct net_device *dev) | |
858 | { | |
859 | struct irport_cb *self; | |
860 | int iobase; | |
861 | char hwname[16]; | |
862 | unsigned long flags; | |
863 | ||
864 | IRDA_DEBUG(2, "%s()\n", __FUNCTION__); | |
865 | ||
866 | IRDA_ASSERT(dev != NULL, return -1;); | |
867 | self = (struct irport_cb *) dev->priv; | |
868 | ||
869 | iobase = self->io.sir_base; | |
870 | ||
871 | if (request_irq(self->io.irq, self->interrupt, 0, dev->name, | |
872 | (void *) dev)) { | |
873 | IRDA_DEBUG(0, "%s(), unable to allocate irq=%d\n", | |
874 | __FUNCTION__, self->io.irq); | |
875 | return -EAGAIN; | |
876 | } | |
877 | ||
878 | spin_lock_irqsave(&self->lock, flags); | |
879 | /* Init uart */ | |
880 | irport_start(self); | |
881 | /* Set 9600 bauds per default, including at the dongle */ | |
882 | irda_task_execute(self, __irport_change_speed, | |
883 | irport_change_speed_complete, | |
884 | NULL, (void *) 9600); | |
885 | spin_unlock_irqrestore(&self->lock, flags); | |
886 | ||
887 | ||
888 | /* Give self a hardware name */ | |
889 | sprintf(hwname, "SIR @ 0x%03x", self->io.sir_base); | |
890 | ||
891 | /* | |
892 | * Open new IrLAP layer instance, now that everything should be | |
893 | * initialized properly | |
894 | */ | |
895 | self->irlap = irlap_open(dev, &self->qos, hwname); | |
896 | ||
897 | /* Ready to play! */ | |
898 | ||
899 | netif_start_queue(dev); | |
900 | ||
901 | return 0; | |
902 | } | |
903 | ||
904 | /* | |
905 | * Function irport_net_close (self) | |
906 | * | |
907 | * Network device is taken down. Usually this is done by | |
908 | * "ifconfig irda0 down" | |
909 | */ | |
910 | static int irport_net_close(struct net_device *dev) | |
911 | { | |
912 | struct irport_cb *self; | |
913 | int iobase; | |
914 | unsigned long flags; | |
915 | ||
916 | IRDA_DEBUG(4, "%s()\n", __FUNCTION__); | |
917 | ||
918 | IRDA_ASSERT(dev != NULL, return -1;); | |
919 | self = (struct irport_cb *) dev->priv; | |
920 | ||
921 | IRDA_ASSERT(self != NULL, return -1;); | |
922 | ||
923 | iobase = self->io.sir_base; | |
924 | ||
925 | /* Stop device */ | |
926 | netif_stop_queue(dev); | |
927 | ||
928 | /* Stop and remove instance of IrLAP */ | |
929 | if (self->irlap) | |
930 | irlap_close(self->irlap); | |
931 | self->irlap = NULL; | |
932 | ||
933 | spin_lock_irqsave(&self->lock, flags); | |
934 | irport_stop(self); | |
935 | spin_unlock_irqrestore(&self->lock, flags); | |
936 | ||
937 | free_irq(self->io.irq, dev); | |
938 | ||
939 | return 0; | |
940 | } | |
941 | ||
942 | /* | |
943 | * Function irport_is_receiving (self) | |
944 | * | |
945 | * Returns true is we are currently receiving data | |
946 | * | |
947 | */ | |
948 | static inline int irport_is_receiving(struct irport_cb *self) | |
949 | { | |
950 | return (self->rx_buff.state != OUTSIDE_FRAME); | |
951 | } | |
952 | ||
953 | /* | |
954 | * Function irport_set_dtr_rts (tty, dtr, rts) | |
955 | * | |
956 | * This function can be used by dongles etc. to set or reset the status | |
957 | * of the dtr and rts lines | |
958 | */ | |
959 | static int irport_set_dtr_rts(struct net_device *dev, int dtr, int rts) | |
960 | { | |
961 | struct irport_cb *self = dev->priv; | |
962 | int iobase; | |
963 | ||
964 | IRDA_ASSERT(self != NULL, return -1;); | |
965 | ||
966 | iobase = self->io.sir_base; | |
967 | ||
968 | if (dtr) | |
969 | dtr = UART_MCR_DTR; | |
970 | if (rts) | |
971 | rts = UART_MCR_RTS; | |
972 | ||
973 | outb(dtr|rts|UART_MCR_OUT2, iobase+UART_MCR); | |
974 | ||
975 | return 0; | |
976 | } | |
977 | ||
978 | static int irport_raw_write(struct net_device *dev, __u8 *buf, int len) | |
979 | { | |
980 | struct irport_cb *self = (struct irport_cb *) dev->priv; | |
981 | int actual = 0; | |
982 | int iobase; | |
983 | ||
984 | IRDA_ASSERT(self != NULL, return -1;); | |
985 | ||
986 | iobase = self->io.sir_base; | |
987 | ||
988 | /* Tx FIFO should be empty! */ | |
989 | if (!(inb(iobase+UART_LSR) & UART_LSR_THRE)) { | |
990 | IRDA_DEBUG( 0, "%s(), failed, fifo not empty!\n", __FUNCTION__); | |
991 | return -1; | |
992 | } | |
993 | ||
994 | /* Fill FIFO with current frame */ | |
995 | while (actual < len) { | |
996 | /* Transmit next byte */ | |
997 | outb(buf[actual], iobase+UART_TX); | |
998 | actual++; | |
999 | } | |
1000 | ||
1001 | return actual; | |
1002 | } | |
1003 | ||
1004 | /* | |
1005 | * Function irport_net_ioctl (dev, rq, cmd) | |
1006 | * | |
1007 | * Process IOCTL commands for this device | |
1008 | * | |
1009 | */ | |
1010 | static int irport_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) | |
1011 | { | |
1012 | struct if_irda_req *irq = (struct if_irda_req *) rq; | |
1013 | struct irport_cb *self; | |
1014 | dongle_t *dongle; | |
1015 | unsigned long flags; | |
1016 | int ret = 0; | |
1017 | ||
1018 | IRDA_ASSERT(dev != NULL, return -1;); | |
1019 | ||
1020 | self = dev->priv; | |
1021 | ||
1022 | IRDA_ASSERT(self != NULL, return -1;); | |
1023 | ||
1024 | IRDA_DEBUG(2, "%s(), %s, (cmd=0x%X)\n", __FUNCTION__, dev->name, cmd); | |
1025 | ||
1026 | switch (cmd) { | |
1027 | case SIOCSBANDWIDTH: /* Set bandwidth */ | |
1028 | if (!capable(CAP_NET_ADMIN)) | |
1029 | ret = -EPERM; | |
1030 | else | |
1031 | irda_task_execute(self, __irport_change_speed, NULL, | |
1032 | NULL, (void *) irq->ifr_baudrate); | |
1033 | break; | |
1034 | case SIOCSDONGLE: /* Set dongle */ | |
1035 | if (!capable(CAP_NET_ADMIN)) { | |
1036 | ret = -EPERM; | |
1037 | break; | |
1038 | } | |
1039 | ||
1040 | /* Locking : | |
1041 | * irda_device_dongle_init() can't be locked. | |
1042 | * irda_task_execute() doesn't need to be locked. | |
1043 | * Jean II | |
1044 | */ | |
1045 | ||
1046 | /* Initialize dongle */ | |
1047 | dongle = irda_device_dongle_init(dev, irq->ifr_dongle); | |
1048 | if (!dongle) | |
1049 | break; | |
1050 | ||
1051 | dongle->set_mode = NULL; | |
1052 | dongle->read = NULL; | |
1053 | dongle->write = irport_raw_write; | |
1054 | dongle->set_dtr_rts = irport_set_dtr_rts; | |
1055 | ||
1056 | /* Now initialize the dongle! */ | |
1057 | dongle->issue->open(dongle, &self->qos); | |
1058 | ||
1059 | /* Reset dongle */ | |
1060 | irda_task_execute(dongle, dongle->issue->reset, NULL, NULL, | |
1061 | NULL); | |
1062 | ||
1063 | /* Make dongle available to driver only now to avoid | |
1064 | * race conditions - Jean II */ | |
1065 | self->dongle = dongle; | |
1066 | break; | |
1067 | case SIOCSMEDIABUSY: /* Set media busy */ | |
1068 | if (!capable(CAP_NET_ADMIN)) { | |
1069 | ret = -EPERM; | |
1070 | break; | |
1071 | } | |
1072 | ||
1073 | irda_device_set_media_busy(self->netdev, TRUE); | |
1074 | break; | |
1075 | case SIOCGRECEIVING: /* Check if we are receiving right now */ | |
1076 | irq->ifr_receiving = irport_is_receiving(self); | |
1077 | break; | |
1078 | case SIOCSDTRRTS: | |
1079 | if (!capable(CAP_NET_ADMIN)) { | |
1080 | ret = -EPERM; | |
1081 | break; | |
1082 | } | |
1083 | ||
1084 | /* No real need to lock... */ | |
1085 | spin_lock_irqsave(&self->lock, flags); | |
1086 | irport_set_dtr_rts(dev, irq->ifr_dtr, irq->ifr_rts); | |
1087 | spin_unlock_irqrestore(&self->lock, flags); | |
1088 | break; | |
1089 | default: | |
1090 | ret = -EOPNOTSUPP; | |
1091 | } | |
1092 | ||
1093 | return ret; | |
1094 | } | |
1095 | ||
1096 | static struct net_device_stats *irport_net_get_stats(struct net_device *dev) | |
1097 | { | |
1098 | struct irport_cb *self = (struct irport_cb *) dev->priv; | |
1099 | ||
1100 | return &self->stats; | |
1101 | } | |
1102 | ||
1103 | static int __init irport_init(void) | |
1104 | { | |
1105 | int i; | |
1106 | ||
1107 | for (i=0; (io[i] < 2000) && (i < 4); i++) { | |
1108 | if (irport_open(i, io[i], irq[i]) != NULL) | |
1109 | return 0; | |
1110 | } | |
1111 | /* | |
1112 | * Maybe something failed, but we can still be usable for FIR drivers | |
1113 | */ | |
1114 | return 0; | |
1115 | } | |
1116 | ||
1117 | /* | |
1118 | * Function irport_cleanup () | |
1119 | * | |
1120 | * Close all configured ports | |
1121 | * | |
1122 | */ | |
1123 | static void __exit irport_cleanup(void) | |
1124 | { | |
1125 | int i; | |
1126 | ||
1127 | IRDA_DEBUG( 4, "%s()\n", __FUNCTION__); | |
1128 | ||
1129 | for (i=0; i < 4; i++) { | |
1130 | if (dev_self[i]) | |
1131 | irport_close(dev_self[i]); | |
1132 | } | |
1133 | } | |
1134 | ||
1135 | MODULE_PARM(io, "1-4i"); | |
1136 | MODULE_PARM_DESC(io, "Base I/O addresses"); | |
1137 | MODULE_PARM(irq, "1-4i"); | |
1138 | MODULE_PARM_DESC(irq, "IRQ lines"); | |
1139 | ||
1140 | MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>"); | |
1141 | MODULE_DESCRIPTION("Half duplex serial driver for IrDA SIR mode"); | |
1142 | MODULE_LICENSE("GPL"); | |
1143 | ||
1144 | module_init(irport_init); | |
1145 | module_exit(irport_cleanup); | |
1146 |