1 /* -*- linux-c -*- */
2 /*
3 * Copyright (C) 2001 By Joachim Martillo, Telford Tools, Inc.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
9 *
10 **/
11
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/pci.h>
15 #include <linux/stddef.h>
16 #include <linux/string.h>
17 #include <linux/sockios.h>
18 #include <asm/io.h>
19 #include <asm/byteorder.h>
20 #include <asm/pgtable.h>
21 #include <linux/skbuff.h>
22 #include <linux/if_arp.h>
23 #include <linux/fs.h>
24 #include <linux/sched.h>
25 #include <asm/uaccess.h>
26 #include <linux/version.h>
27 #include <linux/etherdevice.h>
28 #include "Reg9050.h"
29 #include "8253xctl.h"
30 #include "ring.h"
31 #include "8253x.h"
32 #include "crc32dcl.h"
33
34 /* turns network packet into a pseudoethernet */
35 /* frame -- does ethernet stuff that 8253x does */
36 /* not do -- makes minimum 64 bytes add crc, etc*/
37 int
sab8253xn_write2(struct sk_buff * skb,struct net_device * dev)38 sab8253xn_write2(struct sk_buff *skb, struct net_device *dev)
39 {
40 size_t cnt;
41 unsigned int flags;
42 SAB_PORT *priv = (SAB_PORT*) dev->priv;
43 struct sk_buff *substitute;
44
45 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 0)
46 if(dev->tbusy != 0) /* something of an error */
47 {
48 ++(priv->Counters.tx_drops);
49 dev_kfree_skb_any(skb);
50 return -EBUSY; /* only during release */
51 }
52 #endif
53
54 if(priv->active2.transmit == NULL)
55 {
56 return -ENOMEM;
57 }
58
59 DEBUGPRINT((KERN_ALERT "sab8253x: sending IP packet(bytes):\n"));
60
61 DEBUGPRINT((KERN_ALERT "sab8253x: start address is %p.\n", skb->data));
62
63 cnt = skb->tail - skb->data;
64 cnt = MIN(cnt, sab8253xn_rbufsize);
65 if(cnt < ETH_ZLEN)
66 {
67 if((skb->end - skb->data) >= ETH_ZLEN)
68 {
69 skb->tail = (skb->data + ETH_ZLEN);
70 cnt = ETH_ZLEN;
71 }
72 else
73 {
74 substitute = dev_alloc_skb(ETH_ZLEN);
75 if(substitute == NULL)
76 {
77 dev_kfree_skb_any(skb);
78 return 0;
79 }
80 substitute->tail = (substitute->data + ETH_ZLEN);
81 memcpy(substitute->data, skb->data, cnt);
82 cnt = ETH_ZLEN;
83 dev_kfree_skb_any(skb);
84 skb = substitute;
85 }
86 }
87
88 save_flags(flags); cli();
89 if((priv->active2.transmit->Count & OWNER) == OWN_SAB)
90 {
91 ++(priv->Counters.tx_drops);
92 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 0)
93 dev->tbusy = 1;
94 #else
95 netif_stop_queue (dev);
96 #endif
97 priv->tx_full = 1;
98 restore_flags(flags);
99 return 1;
100 }
101 restore_flags(flags);
102 #ifndef FREEINTERRUPT
103 if(priv->active2.transmit->HostVaddr != NULL)
104 {
105 register RING_DESCRIPTOR *freeme;
106
107 freeme = priv->active2.transmit;
108 do
109 {
110 skb_unlink((struct sk_buff*)freeme->HostVaddr);
111 dev_kfree_skb_any((struct sk_buff*)freeme->HostVaddr);
112 freeme->HostVaddr = NULL;
113 freeme = (RING_DESCRIPTOR*) freeme->VNext;
114 }
115 while(((freeme->Count & OWNER) != OWN_SAB) &&
116 (freeme->HostVaddr != NULL));
117 }
118 #endif
119 dev->trans_start = jiffies;
120 skb_queue_head(priv->sab8253xbuflist, skb);
121 priv->active2.transmit->HostVaddr = skb;
122 priv->active2.transmit->sendcrc = 1;
123 priv->active2.transmit->crcindex = 0;
124 priv->active2.transmit->crc = fn_calc_memory_crc32(skb->data, cnt);
125 priv->active2.transmit->Count = (OWN_SAB|cnt); /* must be this order */
126 priv->active2.transmit =
127 (RING_DESCRIPTOR*) priv->active2.transmit->VNext;
128 priv->Counters.transmitbytes += cnt;
129 sab8253x_start_txS(priv);
130 return 0;
131 }
132
133 /* packetizes the received character */
134 /* stream */
sab8253x_receive_charsN(struct sab_port * port,union sab8253x_irq_status * stat)135 static void sab8253x_receive_charsN(struct sab_port *port,
136 union sab8253x_irq_status *stat)
137 {
138 unsigned char buf[32];
139 int free_fifo = 0;
140 int reset_fifo = 0;
141 int msg_done = 0;
142 int msg_bad = 0;
143 int count = 0;
144 int total_size = 0;
145 int rstatus = 0;
146 struct sk_buff *skb;
147
148 /* Read number of BYTES (Character + Status) available. */
149
150 if((stat->images[ISR1_IDX] & SAB82532_ISR1_RDO) || (stat->images[ISR0_IDX] & SAB82532_ISR0_RFO) )
151 {
152 ++msg_bad;
153 ++free_fifo;
154 ++reset_fifo;
155 }
156 else
157 {
158 if (stat->images[ISR0_IDX] & SAB82532_ISR0_RPF)
159 {
160 count = port->recv_fifo_size;
161 ++free_fifo;
162 }
163
164 if (stat->images[ISR0_IDX] & SAB82532_ISR0_RME)
165 {
166 count = READB(port,rbcl);
167 count &= (port->recv_fifo_size - 1);
168 ++msg_done;
169 ++free_fifo;
170
171 total_size = READB(port, rbch);
172 if(total_size & SAB82532_RBCH_OV)
173 {
174 msg_bad++;
175 }
176
177 rstatus = READB(port, rsta);
178 if((rstatus & SAB82532_RSTA_VFR) == 0)
179 {
180 msg_bad++;
181 }
182 if(rstatus & SAB82532_RSTA_RDO)
183 {
184 msg_bad++;
185 }
186 if((rstatus & SAB82532_RSTA_CRC) == 0)
187 {
188 msg_bad++;
189 }
190 if(rstatus & SAB82532_RSTA_RAB)
191 {
192 msg_bad++;
193 }
194 }
195 }
196
197 /* Read the FIFO. */
198 (*port->readfifo)(port, buf, count);
199
200 /* Issue Receive Message Complete command. */
201
202 if (free_fifo)
203 {
204 sab8253x_cec_wait(port);
205 WRITEB(port, cmdr, SAB82532_CMDR_RMC);
206 }
207
208 if(reset_fifo)
209 {
210 sab8253x_cec_wait(port);
211 WRITEB(port, cmdr, SAB82532_CMDR_RHR);
212 }
213
214 if(port->active2.receive == NULL)
215 {
216 return;
217 }
218
219 if(msg_bad)
220 {
221 ++(port->Counters.rx_drops);
222 port->active2.receive->HostVaddr->tail = port->active2.receive->HostVaddr->data; /* clear the buffer */
223 port->active2.receive->Count = sab8253xn_rbufsize|OWN_SAB;
224 return;
225 }
226
227 memcpy(port->active2.receive->HostVaddr->tail, buf, count);
228 port->active2.receive->HostVaddr->tail += count;
229
230 if(msg_done)
231 {
232 port->active2.receive->Count =
233 (port->active2.receive->HostVaddr->tail - port->active2.receive->HostVaddr->data);
234 if((port->active2.receive->Count < (ETH_ZLEN+4+3)) || /* 4 is the CRC32 size 3 bytes from the SAB part */
235 (skb = dev_alloc_skb(sab8253xn_rbufsize), skb == NULL))
236 {
237 ++(port->Counters.rx_drops);
238 port->active2.receive->HostVaddr->tail = port->active2.receive->HostVaddr->data;
239 /* clear the buffer */
240 port->active2.receive->Count = sab8253xn_rbufsize|OWN_SAB;
241 }
242 else
243 {
244 port->active2.receive->Count -= 3;
245 port->active2.receive->HostVaddr->len = port->active2.receive->Count;
246 port->active2.receive->HostVaddr->pkt_type = PACKET_HOST;
247 port->active2.receive->HostVaddr->dev = port->dev;
248 port->active2.receive->HostVaddr->protocol =
249 eth_type_trans(port->active2.receive->HostVaddr, port->dev);
250 port->active2.receive->HostVaddr->tail -= 3;
251 ++(port->Counters.receivepacket);
252 port->Counters.receivebytes += port->active2.receive->Count;
253 skb_unlink(port->active2.receive->HostVaddr);
254
255 netif_rx(port->active2.receive->HostVaddr);
256
257 skb_queue_head(port->sab8253xbuflist, skb);
258 port->active2.receive->HostVaddr = skb;
259 port->active2.receive->Count = sab8253xn_rbufsize|OWN_SAB;
260 }
261 }
262 }
263
sab8253x_check_statusN(struct sab_port * port,union sab8253x_irq_status * stat)264 static void sab8253x_check_statusN(struct sab_port *port,
265 union sab8253x_irq_status *stat)
266 {
267 int modem_change = 0;
268 mctlsig_t *sig;
269
270
271 if (stat->images[ISR0_IDX] & SAB82532_ISR0_RFO)
272 {
273 port->icount.buf_overrun++;
274 }
275
276 /* Checking DCD */
277 sig = &port->dcd;
278 if (stat->images[sig->irq] & sig->irqmask)
279 {
280 sig->val = ISON(port,dcd);
281 port->icount.dcd++;
282 modem_change++;
283 }
284 /* Checking CTS */
285 sig = &port->cts;
286 if (stat->images[sig->irq] & sig->irqmask)
287 {
288 sig->val = ISON(port,cts);
289 port->icount.cts++;
290 modem_change++;
291 }
292 /* Checking DSR */
293 sig = &port->dsr;
294 if (stat->images[sig->irq] & sig->irqmask)
295 {
296 sig->val = ISON(port,dsr);
297 port->icount.dsr++;
298 modem_change++;
299 }
300 if (modem_change)
301 {
302 wake_up_interruptible(&port->delta_msr_wait);
303 }
304
305 sig = &port->dcd;
306 if ((port->flags & FLAG8253X_CHECK_CD) &&
307 (stat->images[sig->irq] & sig->irqmask))
308 {
309
310 if (sig->val)
311 {
312 netif_carrier_on(port->dev);
313 }
314 else if (!((port->flags & FLAG8253X_CALLOUT_ACTIVE) &&
315 (port->flags & FLAG8253X_CALLOUT_NOHUP)))
316 {
317 netif_carrier_off(port->dev);
318 }
319 }
320 #if 0 /* need to think about CTS/RTS stuff for a network driver */
321 sig = &port->cts;
322 if (port->flags & FLAG8253X_CTS_FLOW)
323 { /* not setting this yet */
324 if (port->tty->hw_stopped)
325 {
326 if (sig->val)
327 {
328
329 port->tty->hw_stopped = 0;
330 sab8253x_sched_event(port, RS_EVENT_WRITE_WAKEUP);
331 port->interrupt_mask1 &= ~(SAB82532_IMR1_XPR);
332 WRITEB(port, imr1, port->interrupt_mask1);
333 sab8253x_start_txS(port);
334 }
335 }
336 else
337 {
338 if (!(sig->val))
339 {
340 port->tty->hw_stopped = 1;
341 }
342 }
343 }
344 #endif
345 }
346
Sab8253xCollectStats(struct net_device * dev)347 static void Sab8253xCollectStats(struct net_device *dev)
348 {
349
350 struct net_device_stats *statsp =
351 &((SAB_PORT*) dev->priv)->stats;
352
353 memset(statsp, 0, sizeof(struct net_device_stats));
354
355 statsp->rx_packets +=
356 ((SAB_PORT*)dev->priv)->Counters.receivepacket;
357 statsp->tx_packets +=
358 ((SAB_PORT*)dev->priv)->Counters.transmitpacket;
359 statsp->tx_dropped +=
360 ((SAB_PORT*)dev->priv)->Counters.tx_drops;
361 statsp->rx_dropped +=
362 ((SAB_PORT*)dev->priv)->Counters.rx_drops;
363 }
364
sab8253xn_stats(struct net_device * dev)365 struct net_device_stats *sab8253xn_stats(struct net_device *dev)
366 {
367 SAB_PORT *priv = (SAB_PORT*) dev->priv;
368
369 Sab8253xCollectStats(dev);
370 return &priv->stats;
371 }
372
373 /* minimal ioctls -- more to be added later */
sab8253xn_ioctl(struct net_device * dev,struct ifreq * ifr,int cmd)374 int sab8253xn_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
375 {
376
377 SAB_PORT *priv = (SAB_PORT*) dev->priv;
378
379 switch(cmd)
380 {
381 case SAB8253XCLEARCOUNTERS:
382 memset(&priv->Counters, 0, sizeof(struct counters));
383 break;
384
385 default:
386 break;
387 }
388 return 0;
389 }
390
391 #if 0
392 static int sab8253x_block_til_readyN(SAB_PORT *port)
393 {
394 DECLARE_WAITQUEUE(wait, current);
395 int retval;
396 int do_clocal = 0;
397 unsigned long flags;
398
399 /*
400 * If the device is in the middle of being closed, then block
401 * until it's done, and then try again.
402 */
403 if (port->flags & FLAG8253X_CLOSING)
404 {
405 if (port->flags & FLAG8253X_CLOSING)
406 {
407 interruptible_sleep_on(&port->close_wait);
408 }
409 #ifdef SERIAL_DO_RESTART
410 if (port->flags & FLAG8253X_HUP_NOTIFY)
411 {
412 return -EAGAIN;
413 }
414 else
415 {
416 return -ERESTARTSYS;
417 }
418 #else
419 return -EAGAIN;
420 #endif
421 }
422
423 /*
424 * this is not a callout device
425 */
426
427 /* suppose callout active */
428 if (port->flags & FLAG8253X_CALLOUT_ACTIVE)
429 {
430 if (port->normal_termios.c_cflag & CLOCAL)
431 {
432 do_clocal = 1;
433 }
434 }
435
436 /*
437 * Block waiting for the carrier detect and the line to become
438 * free (i.e., not in use by the callout). While we are in
439 * this loop, port->count is dropped by one, so that
440 * sab8253x_close() knows when to free things. We restore it upon
441 * exit, either normal or abnormal.
442 */
443 retval = 0;
444 add_wait_queue(&port->open_wait, &wait);
445 port->blocked_open++;
446 while (1)
447 {
448 save_flags(flags); cli();
449 if (!(port->flags & FLAG8253X_CALLOUT_ACTIVE))
450 {
451 RAISE(port,dtr);
452 RAISE(port,rts); /* maybe not correct for sync */
453 /*
454 * ??? Why changing the mode here?
455 * port->regs->rw.mode |= SAB82532_MODE_FRTS;
456 * port->regs->rw.mode &= ~(SAB82532_MODE_RTS);
457 */
458 }
459 restore_flags(flags);
460 current->state = TASK_INTERRUPTIBLE;
461 if (!(port->flags & FLAG8253X_INITIALIZED))
462 {
463 #ifdef SERIAL_DO_RESTART
464 if (port->flags & FLAG8253X_HUP_NOTIFY)
465 {
466 retval = -EAGAIN;
467 }
468 else
469 {
470 retval = -ERESTARTSYS;
471 }
472 #else
473 retval = -EAGAIN;
474 #endif
475 break;
476 }
477 if (!(port->flags & FLAG8253X_CALLOUT_ACTIVE) &&
478 !(port->flags & FLAG8253X_CLOSING) &&
479 (do_clocal || ISON(port,dcd)))
480 {
481 break;
482 }
483 #ifdef DEBUG_OPEN
484 printk("block_til_readyN:2 flags = 0x%x\n",port->flags);
485 #endif
486 if (signal_pending(current))
487 {
488 retval = -ERESTARTSYS;
489 break;
490 }
491 schedule();
492 }
493 current->state = TASK_RUNNING;
494 remove_wait_queue(&port->open_wait, &wait);
495 port->blocked_open--;
496 if (retval)
497 {
498 return retval;
499 }
500 port->flags |= FLAG8253X_NORMAL_ACTIVE; /* is this a good flag? */
501 return 0;
502 }
503 #endif
504
sab8253x_startupN(struct sab_port * port)505 int sab8253x_startupN(struct sab_port *port)
506 {
507 unsigned long flags;
508 int retval = 0;
509
510 save_flags(flags); cli();
511
512 if (port->flags & FLAG8253X_INITIALIZED)
513 {
514 goto errout;
515 }
516
517 if (!port->regs)
518 {
519 retval = -ENODEV;
520 goto errout;
521 }
522 /*
523 * Initialize the Hardware
524 */
525 sab8253x_init_lineS(port); /* nothing in this function
526 * refers to tty structure */
527
528 /* Activate RTS */
529 RAISE(port,rts);
530 /* Activate DTR */
531 RAISE(port,dtr);
532 /*
533 * Initialize the modem signals values
534 */
535 port->dcd.val=ISON(port,dcd);
536 port->cts.val=ISON(port,cts);
537 port->dsr.val=ISON(port,dsr);
538 /*
539 * Finally, enable interrupts
540 */
541
542 port->interrupt_mask0 = SAB82532_IMR0_RFS | SAB82532_IMR0_PCE |
543 SAB82532_IMR0_PLLA | SAB82532_IMR0_RSC | SAB82532_IMR0_CDSC;
544 /*((port->ccontrol.ccr2 & SAB82532_CCR2_TOE) ? SAB82532_IMR0_CDSC : 0); */
545
546 WRITEB(port,imr0,port->interrupt_mask0);
547 port->interrupt_mask1 = SAB82532_IMR1_EOP | SAB82532_IMR1_XMR |
548 SAB82532_IMR1_TIN | SAB82532_IMR1_XPR;
549 WRITEB(port, imr1, port->interrupt_mask1);
550 port->all_sent = 1;
551
552
553 /*
554 * and set the speed of the serial port
555 */
556 sab8253x_change_speedN(port);
557
558 port->flags |= FLAG8253X_INITIALIZED; /* bad name for indicating to other functionalities status */
559 port->receive_chars = sab8253x_receive_charsN;
560 port->transmit_chars = sab8253x_transmit_charsS;
561 port->check_status = sab8253x_check_statusN;
562 port->receive_test = (SAB82532_ISR0_RME | SAB82532_ISR0_RFO | SAB82532_ISR0_RPF);
563 port->transmit_test = (SAB82532_ISR1_ALLS | SAB82532_ISR1_RDO | SAB82532_ISR1_XPR |
564 SAB82532_ISR1_XDU | SAB82532_ISR1_CSC);
565 port->check_status_test = (SAB82532_ISR1_CSC);
566
567 /*((port->ccontrol.ccr2 & SAB82532_CCR2_TOE) ? 0 : SAB82532_ISR0_CDSC));*/
568
569 restore_flags(flags);
570 return 0;
571
572 errout:
573 restore_flags(flags);
574 return retval;
575 }
576
sab8253xn_open(struct net_device * dev)577 int sab8253xn_open(struct net_device *dev)
578 {
579 unsigned int retval;
580 SAB_PORT *priv = (SAB_PORT*) dev->priv;
581
582 if(priv->function != FUNCTION_NR)
583 {
584 return -ENODEV; /* only allowed if there are no restrictions on the port */
585 }
586
587
588 if(priv->flags & FLAG8253X_CLOSING) /* try again after the TTY close finishes */
589 {
590 #ifdef SERIAL_DO_RESTART
591 return ((priv->flags & FLAG8253X_HUP_NOTIFY) ?
592 -EAGAIN : -ERESTARTSYS); /* The ifconfig UP will just fail */
593 #else
594 return -EAGAIN;
595 #endif
596 }
597
598 /*
599 * Maybe start up serial port -- may already be running a TTY
600 */
601 if(priv->flags & FLAG8253X_NORMAL_ACTIVE) /* probably should be a test open at all */
602 {
603 return -EBUSY; /* can't reopen in NET */
604 }
605
606 if(Sab8253xSetUpLists(priv))
607 {
608 return -ENODEV;
609 }
610
611 if(Sab8253xInitDescriptors2(priv, sab8253xn_listsize, sab8253xn_rbufsize))
612 {
613 Sab8253xCleanUpTransceiveN(priv);
614 return -ENODEV;
615 }
616 netif_carrier_off(dev);
617
618 priv->open_type = OPEN_SYNC_NET;
619 priv->tty = 0;
620
621 retval = sab8253x_startupN(priv);
622 if (retval)
623 {
624 Sab8253xCleanUpTransceiveN(priv);
625 return retval;
626 }
627
628 priv->flags |= FLAG8253X_NETWORK; /* flag the call out driver that it has to reinitialize the port */
629 priv->tx_full = 0;
630 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 0)
631 dev->start = 1;
632 dev->tbusy = 0;
633 #else
634 netif_start_queue(dev);
635 #endif
636
637 priv->flags |= FLAG8253X_NORMAL_ACTIVE; /* is this a good flag? */
638 MOD_INC_USE_COUNT;
639 return 0; /* success */
640 }
641 /* stop the PPC, free all skbuffers */
sab8253xn_release(struct net_device * dev)642 int sab8253xn_release(struct net_device *dev) /* stop */
643 {
644 SAB_PORT *priv = (SAB_PORT*) dev->priv;
645 unsigned long flags;
646
647 printk(KERN_ALERT "sab8253xn: network interface going down.\n");
648 save_flags(flags); cli();
649
650 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 0)
651 dev->start = 0;
652 dev->tbusy = 1;
653 #else
654 netif_stop_queue (dev);
655 #endif
656
657 sab8253x_shutdownN(priv);
658 Sab8253xCleanUpTransceiveN(priv);
659 netif_carrier_off(dev);
660 priv->flags &= ~FLAG8253X_NETWORK;
661 priv->flags &= ~(FLAG8253X_NORMAL_ACTIVE|/*FLAG8253X_CALLOUT_ACTIVE|*/
662 FLAG8253X_CLOSING);
663 priv->open_type = OPEN_NOT;
664 MOD_DEC_USE_COUNT;
665 restore_flags(flags);
666 return 0;
667 }
668
669 SAB_PORT *current_sab_port = NULL;
670
sab8253xn_init(struct net_device * dev)671 int sab8253xn_init(struct net_device *dev)
672 {
673
674 SAB_PORT *priv;
675
676 printk(KERN_ALERT "sab8253xn: initializing SAB8253X network driver instance.\n");
677
678 priv = current_sab_port;
679 dev->priv = priv;
680
681 if(dev->priv == NULL)
682 {
683 printk(KERN_ALERT "sab8253xn: could not find active port!\n");
684 return -ENOMEM;
685 }
686 priv->dev = dev;
687
688 ether_setup(dev);
689
690 dev->irq = priv->irq;
691 dev->hard_start_xmit = sab8253xn_write2;
692 dev->do_ioctl = sab8253xn_ioctl;
693 dev->open = sab8253xn_open;
694 dev->stop = sab8253xn_release;
695 dev->get_stats = sab8253xn_stats;
696 dev->base_addr = (unsigned) priv->regs;
697 /* should I do a request region here */
698 priv->next_dev = Sab8253xRoot;
699 Sab8253xRoot = dev;
700 return 0;
701 }
702
703