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 <linux/poll.h>
29 #include "Reg9050.h"
30 #include "8253xctl.h"
31 #include "ring.h"
32 #include "8253x.h"
33 #include "crc32dcl.h"
34
35 /* a raw character driver -- theoretically for implementing custom protocols,
36 * async interrupts can be used for getting indication that a packet has
37 * been successfully transmitted.
38 */
39
40
41 /* the application read routine, can block according */
42 /* to flag, returns one packet at a time */
sab8253xc_read(struct file * filep,char * cptr,size_t cnt,loff_t * loffp)43 int sab8253xc_read(struct file *filep, char *cptr, size_t cnt, loff_t *loffp)
44 {
45 unsigned int length;
46 unsigned long flags;
47 SAB_PORT *port = filep->private_data;
48 struct sk_buff *skb;
49
50 DEBUGPRINT((KERN_ALERT "Attempting to read %i bytes.\n", cnt));
51
52
53 if(port->sab8253xc_rcvbuflist == NULL)
54 {
55 return -ENOMEM;
56 }
57
58 save_flags(flags); cli();
59 if(skb_queue_len(port->sab8253xc_rcvbuflist) == 0)
60 {
61 port->rx_empty = 1;
62 if(filep->f_flags & O_NONBLOCK)
63 {
64 restore_flags(flags);
65 return -EAGAIN;
66 }
67 restore_flags(flags);
68 interruptible_sleep_on(&port->read_wait);
69 }
70 else
71 {
72 restore_flags(flags);
73 }
74
75 skb = skb_peek(port->sab8253xc_rcvbuflist);
76 length = skb->tail - skb->data;
77 if(cnt < length)
78 {
79 return -ENOMEM;
80 }
81
82 skb = skb_dequeue(port->sab8253xc_rcvbuflist);
83
84 save_flags(flags); cli();
85 if(skb_queue_len(port->sab8253xc_rcvbuflist) <= 0)
86 {
87 port->rx_empty = 1;
88 }
89 restore_flags(flags);
90
91 DEBUGPRINT((KERN_ALERT "Copying to user space %s.\n", skb->data));
92 copy_to_user(cptr, skb->data, length);
93 dev_kfree_skb_any(skb);
94 return length;
95 }
96
97 /* application write */
98
sab8253xc_write(struct file * filep,const char * cptr,size_t cnt,loff_t * loffp)99 int sab8253xc_write(struct file *filep, const char *cptr, size_t cnt, loff_t *loffp)
100 {
101 struct sk_buff *skb;
102 unsigned long flags;
103 SAB_PORT *port = filep->private_data;
104
105 if(cnt > sab8253xc_rbufsize) /* should not send bigger than can be received */
106 {
107 return -ENOMEM;
108 }
109
110 if(port->active2.transmit == NULL)
111 {
112 return -ENOMEM;
113 }
114
115 save_flags(flags); cli(); /* can block on write when */
116 /* no space in transmit circular */
117 /* array. */
118 if((port->active2.transmit->Count & OWNER) == OWN_SAB)
119 {
120 ++(port->Counters.tx_drops);
121 port->tx_full = 1;
122 restore_flags(flags);
123 if(filep->f_flags & O_NONBLOCK)
124 {
125 return -EAGAIN;
126 }
127 interruptible_sleep_on(&port->write_wait);
128 }
129 else
130 {
131 restore_flags(flags);
132 }
133
134 #ifndef FREEINTERRUPT
135 if((port->active2.transmit->HostVaddr != NULL) || /* not OWN_SAB from above */
136 (port->active2.transmit->crcindex != 0))
137 {
138 register RING_DESCRIPTOR *freeme;
139
140 freeme = port->active2.transmit;
141 do
142 {
143 if((freeme->crcindex == 0) && (freeme->HostVaddr == NULL))
144 {
145 break;
146 }
147 if(freeme->HostVaddr)
148 {
149 skb_unlink((struct sk_buff*)freeme->HostVaddr);
150 dev_kfree_skb_any((struct sk_buff*)freeme->HostVaddr);
151 freeme->HostVaddr = NULL;
152 }
153 freeme->sendcrc = 0;
154 freeme->crcindex = 0;
155 freeme = (RING_DESCRIPTOR*) freeme->VNext;
156 }
157 while((freeme->Count & OWNER) != OWN_SAB);
158 }
159 #endif
160
161 skb = alloc_skb(cnt, GFP_KERNEL); /* not called from int as with tty */
162 if(skb == NULL)
163 {
164 return -ENOMEM;
165 }
166 copy_from_user(skb->data, cptr, cnt);
167 skb->tail = (skb->data + cnt);
168 skb->len = cnt;
169 skb->data_len = cnt;
170
171 skb_queue_head(port->sab8253xbuflist, skb);
172 port->active2.transmit->HostVaddr = skb;
173 port->active2.transmit->sendcrc = 0;
174 port->active2.transmit->crcindex = 0;
175 port->active2.transmit->Count = (OWN_SAB|cnt); /* must be this order */
176 port->active2.transmit =
177 (RING_DESCRIPTOR*) port->active2.transmit->VNext;
178 port->Counters.transmitbytes += cnt;
179 sab8253x_start_txS(port);
180 return cnt;
181 }
182
sab8253x_receive_charsC(struct sab_port * port,union sab8253x_irq_status * stat)183 static void sab8253x_receive_charsC(struct sab_port *port,
184 union sab8253x_irq_status *stat)
185 {
186 unsigned char buf[32];
187 int free_fifo = 0;
188 int reset_fifo = 0;
189 int msg_done = 0;
190 int msg_bad = 0;
191 int count = 0;
192 int total_size = 0;
193 int rstatus = 0;
194 struct sk_buff *skb;
195
196 /* Read number of BYTES (Character + Status) available. */
197
198 if((stat->images[ISR1_IDX] & SAB82532_ISR1_RDO) || (stat->images[ISR0_IDX] & SAB82532_ISR0_RFO) )
199 {
200 ++msg_bad;
201 ++free_fifo;
202 ++reset_fifo;
203 }
204 else
205 {
206 if (stat->images[ISR0_IDX] & SAB82532_ISR0_RPF)
207 {
208 count = port->recv_fifo_size;
209 ++free_fifo;
210 }
211
212 if (stat->images[ISR0_IDX] & SAB82532_ISR0_RME)
213 {
214 count = READB(port, rbcl);
215 count &= (port->recv_fifo_size - 1);
216 ++msg_done;
217 ++free_fifo;
218
219 total_size = READB(port, rbch);
220 if(total_size & SAB82532_RBCH_OV) /* need to revisit for 4096 byte frames */
221 {
222 msg_bad++;
223 }
224
225 rstatus = READB(port, rsta);
226 if((rstatus & SAB82532_RSTA_VFR) == 0)
227 {
228 msg_bad++;
229 }
230 if(rstatus & SAB82532_RSTA_RDO)
231 {
232 msg_bad++;
233 }
234 if((rstatus & SAB82532_RSTA_CRC) == 0)
235 {
236 msg_bad++;
237 }
238 if(rstatus & SAB82532_RSTA_RAB)
239 {
240 msg_bad++;
241 }
242 }
243 }
244
245 /* Read the FIFO. */
246
247 (*port->readfifo)(port, buf, count);
248
249
250 /* Issue Receive Message Complete command. */
251
252 if (free_fifo)
253 {
254 sab8253x_cec_wait(port);
255 WRITEB(port, cmdr, SAB82532_CMDR_RMC);
256 }
257
258 if(reset_fifo)
259 {
260 sab8253x_cec_wait(port);
261 WRITEB(port, cmdr, SAB82532_CMDR_RHR);
262 }
263
264 if(msg_bad)
265 {
266 port->msgbufindex = 0;
267 return;
268 }
269
270 memcpy(&port->msgbuf[port->msgbufindex], buf, count);
271 port->msgbufindex += count;
272
273 if(msg_done)
274 {
275
276 if(port->msgbufindex <= 3) /* min is 1 char + 2 CRC + status byte */
277 {
278 port->msgbufindex = 0;
279 return;
280 }
281
282 total_size = port->msgbufindex - 3; /* strip off the crc16 and the status byte */
283 port->msgbufindex = 0;
284
285 /* ignore the receive buffer waiting -- we know the correct size here */
286
287 if(skb = dev_alloc_skb(total_size), skb)
288 {
289 memcpy(skb->data, &port->msgbuf[0], total_size);
290 skb->tail = (skb->data + total_size);
291 skb->data_len = total_size;
292 skb->len = total_size;
293 skb_queue_tail(port->sab8253xc_rcvbuflist, skb);
294 if(port->rx_empty)
295 {
296 port->rx_empty = 0;
297 wake_up_interruptible(&port->read_wait);
298 }
299 if(port->async_queue)
300 {
301 kill_fasync(&port->async_queue, SIGIO, POLL_IN);
302 }
303 }
304 }
305 }
306
sab8253x_check_statusC(struct sab_port * port,union sab8253x_irq_status * stat)307 static void sab8253x_check_statusC(struct sab_port *port,
308 union sab8253x_irq_status *stat)
309 {
310 int modem_change = 0;
311 mctlsig_t *sig;
312
313 if (stat->images[ISR0_IDX] & SAB82532_ISR0_RFO)
314 {
315 port->icount.buf_overrun++;
316 }
317
318 /* Checking DCD */
319 sig = &port->dcd;
320 if (stat->images[sig->irq] & sig->irqmask)
321 {
322 sig->val = ISON(port,dcd);
323 port->icount.dcd++;
324 modem_change++;
325 }
326 /* Checking CTS */
327 sig = &port->cts;
328 if (stat->images[sig->irq] & sig->irqmask)
329 {
330 sig->val = ISON(port,cts);
331 port->icount.cts++;
332 modem_change++;
333 }
334 /* Checking DSR */
335 sig = &port->dsr;
336 if (stat->images[sig->irq] & sig->irqmask)
337 {
338 sig->val = ISON(port,dsr);
339 port->icount.dsr++;
340 modem_change++;
341 }
342 if (modem_change)
343 {
344 wake_up_interruptible(&port->delta_msr_wait);
345 }
346
347 sig = &port->dcd;
348 if ((port->flags & FLAG8253X_CHECK_CD) &&
349 (stat->images[sig->irq] & sig->irqmask))
350 {
351
352 if (sig->val)
353 {
354 wake_up_interruptible(&port->open_wait); /* in case waiting in block_til_ready */
355 }
356 else if (!((port->flags & FLAG8253X_CALLOUT_ACTIVE) &&
357 (port->flags & FLAG8253X_CALLOUT_NOHUP)))
358 {
359 /* I think the code needs to walk through all the proces that have opened this
360 * port and send a SIGHUP to them -- need to investigate somewhat more*/
361 }
362 }
363 }
364
sab8253x_startupC(struct sab_port * port)365 static int sab8253x_startupC(struct sab_port *port)
366 {
367 unsigned long flags;
368 int retval = 0;
369
370 save_flags(flags); cli();
371
372 if (port->flags & FLAG8253X_INITIALIZED)
373 {
374 goto errout;
375 }
376
377 if (!port->regs)
378 {
379 retval = -ENODEV;
380 goto errout;
381 }
382 /*
383 * Initialize the Hardware
384 */
385 sab8253x_init_lineS(port); /* nothing in this function
386 * refers to tty structure */
387
388 /* Activate RTS */
389 RAISE(port,rts);
390
391 /* Activate DTR */
392 RAISE(port,dtr);
393
394
395 /*
396 * Initialize the modem signals values
397 */
398 port->dcd.val=ISON(port,dcd);
399 port->cts.val=ISON(port,cts);
400 port->dsr.val=ISON(port,dsr);
401
402 /*
403 * Finally, enable interrupts
404 */
405
406 port->interrupt_mask0 = SAB82532_IMR0_RFS | SAB82532_IMR0_PCE |
407 SAB82532_IMR0_PLLA | SAB82532_IMR0_RSC | SAB82532_IMR0_CDSC;
408 #if 0
409 ((port->ccontrol.ccr2 & SAB82532_CCR2_TOE) ? SAB82532_IMR0_CDSC : 0); /* the weird way the cards work
410 * when clocking CD seems to
411 * monitor txclk*/
412 #endif
413 WRITEB(port,imr0,port->interrupt_mask0);
414 port->interrupt_mask1 = SAB82532_IMR1_EOP | SAB82532_IMR1_XMR |
415 SAB82532_IMR1_TIN | SAB82532_IMR1_XPR;
416 WRITEB(port, imr1, port->interrupt_mask1);
417 port->all_sent = 1;
418
419
420 /*
421 * and set the speed of the serial port
422 */
423 sab8253x_change_speedN(port);
424
425 port->flags |= FLAG8253X_INITIALIZED; /* bad name for indicating to other functionalities status */
426 port->receive_chars = sab8253x_receive_charsC;
427 port->transmit_chars = sab8253x_transmit_charsS;
428 port->check_status = sab8253x_check_statusC;
429 port->receive_test = (SAB82532_ISR0_RME | SAB82532_ISR0_RFO | SAB82532_ISR0_RPF);
430 port->transmit_test = (SAB82532_ISR1_ALLS | SAB82532_ISR1_RDO | SAB82532_ISR1_XPR |
431 SAB82532_ISR1_XDU | SAB82532_ISR1_CSC);
432 port->check_status_test = SAB82532_ISR1_CSC;
433
434 restore_flags(flags);
435 return 0;
436
437 errout:
438 restore_flags(flags);
439 return retval;
440 }
441
sab8253x_block_til_readyC(struct file * filp,struct sab_port * port)442 static int sab8253x_block_til_readyC(struct file* filp, struct sab_port *port)
443 {
444 DECLARE_WAITQUEUE(wait, current);
445 int retval;
446 int do_clocal = 1; /* cheating -- I need to understand how
447 signals behave synchronously better*/
448 unsigned long flags;
449
450 /*
451 * If the device is in the middle of being closed, then block
452 * until it's done, and then try again.
453 */
454 if (port->flags & FLAG8253X_CLOSING)
455 {
456 interruptible_sleep_on(&port->close_wait); /* finish up previous close */
457
458 #ifdef SERIAL_DO_RESTART
459 if (port->flags & FLAG8253X_HUP_NOTIFY)
460 {
461 return -EAGAIN;
462 }
463 else
464 {
465 return -ERESTARTSYS;
466 }
467 #else
468 return -EAGAIN;
469 #endif
470 }
471
472 /* sort out async vs sync tty, not call out */
473 /*
474 * If non-blocking mode is set, or the port is not enabled,
475 * then make the check up front and then exit.
476 */
477
478 if (filp->f_flags & O_NONBLOCK)
479 {
480 if (port->flags & FLAG8253X_CALLOUT_ACTIVE)
481 {
482 return -EBUSY;
483 }
484 port->flags |= FLAG8253X_NORMAL_ACTIVE;
485 return 0;
486 }
487
488 if (port->flags & FLAG8253X_CALLOUT_ACTIVE)
489 {
490 if (port->normal_termios.c_cflag & CLOCAL)
491 {
492 do_clocal = 1;
493 }
494 }
495
496 /*
497 * Block waiting for the carrier detect and the line to become
498 * free (i.e., not in use by the callout). While we are in
499 * this loop, port->count is dropped by one, so that
500 * sab8253x_close() knows when to free things. We restore it upon
501 * exit, either normal or abnormal.
502 */
503
504 /* The port decrement logic is probably */
505 /* broken -- hence if def'd out -- it does*/
506 retval = 0;
507 add_wait_queue(&port->open_wait, &wait); /* starts the wait but does not block here */
508 port->blocked_open++;
509 while (1) /* on some devices when providing clock have to just assume connection */
510 {
511 save_flags(flags);
512 cli();
513 if (!(port->flags & FLAG8253X_CALLOUT_ACTIVE))
514 {
515 RAISE(port, dtr);
516 RAISE(port, rts); /* maybe not correct for sync */
517 /*
518 * ??? Why changing the mode here?
519 * port->regs->rw.mode |= SAB82532_MODE_FRTS;
520 * port->regs->rw.mode &= ~(SAB82532_MODE_RTS);
521 */
522 }
523 restore_flags(flags);;
524 current->state = TASK_INTERRUPTIBLE;
525 if (!(port->flags & FLAG8253X_INITIALIZED))
526 {
527 #ifdef SERIAL_DO_RESTART
528 if (port->flags & FLAG8253X_HUP_NOTIFY)
529 {
530 retval = -EAGAIN;
531 }
532 else
533 {
534 retval = -ERESTARTSYS;
535 }
536 #else
537 retval = -EAGAIN;
538 #endif
539 break;
540 }
541
542 if (!(port->flags & FLAG8253X_CALLOUT_ACTIVE) &&
543 !(port->flags & FLAG8253X_CLOSING) &&
544 (do_clocal || ISON(port,dcd)))
545 {
546 break;
547 }
548 #ifdef DEBUG_OPEN
549 printk("sab8253x_block_til_ready:2 flags = 0x%x\n",port->flags);
550 #endif
551 if (signal_pending(current))
552 {
553 retval = -ERESTARTSYS;
554 break;
555 }
556 #ifdef DEBUG_OPEN
557 printk("sab8253x_block_til_readyC blocking: ttyS%d, count = %d, flags = %x, clocal = %d, vstr = %02x\n",
558 port->line, port->count, port->flags, do_clocal, READB(port,vstr));
559 #endif
560 schedule();
561 }
562 current->state = TASK_RUNNING;
563 remove_wait_queue(&port->open_wait, &wait);
564 port->blocked_open--;
565
566 #ifdef DEBUG_OPEN
567 printk("sab8253x_block_til_ready after blockingC: ttys%d, count = %d\n",
568 port->line, port->count);
569 #endif
570
571 if (retval)
572 {
573 return retval;
574 }
575 port->flags |= FLAG8253X_NORMAL_ACTIVE;
576 return 0;
577 }
578
579
sab8253xc_open(struct inode * inodep,struct file * filep)580 int sab8253xc_open(struct inode *inodep, struct file *filep)
581 {
582 unsigned int line;
583 unsigned int retval;
584 unsigned int counter;
585 SAB_PORT *port;
586
587 line = MINOR(inodep->i_rdev); /* let's find which physical device to use */
588 /* minor dev number indexes through the port */
589 /* list */
590
591 for(counter = 0, port = AuraPortRoot;
592 (counter < line) && (port != NULL);
593 ++counter)
594 {
595 port = port->next;
596 }
597
598
599 if (!port)
600 {
601 printk(KERN_ALERT "sab8253xc_open: can't find structure for line %d\n",
602 line);
603 return -ENODEV;
604 }
605
606 if(port->function == FUNCTION_NA)
607 { /* port 2 on 1020s and 1520s */
608 return -ENODEV;
609 }
610
611 switch(port->open_type)
612 {
613 case OPEN_ASYNC:
614 if(!(port->flags & FLAG8253X_CALLOUT_ACTIVE))
615 {
616 return -EBUSY;
617 }
618 break;
619
620 case OPEN_SYNC_CHAR:
621 case OPEN_NOT:
622 port->tty = NULL;
623 port->open_type = OPEN_SYNC_CHAR;
624 break;
625
626 default:
627 return -EBUSY;
628 }
629
630 /*
631 * Maybe start up serial port -- may already be running in callout mode
632 */
633
634 if(Sab8253xSetUpLists(port))
635 {
636 if(port->open_type == OPEN_SYNC_CHAR)
637 {
638 port->open_type = OPEN_NOT;
639 }
640 return -ENODEV;
641 }
642 if(Sab8253xInitDescriptors2(port, sab8253xc_listsize, sab8253xc_rbufsize))
643 {
644 Sab8253xCleanUpTransceiveN(port); /* the network functions should be okay -- only difference */
645 /* is the crc32 that is appended */
646 if(port->open_type == OPEN_SYNC_CHAR)
647 {
648 port->open_type = OPEN_NOT;
649 }
650 return -ENODEV;
651 }
652 retval = sab8253x_startupC(port); /* does not do anything if call out active */
653 if (retval)
654 {
655 if(port->open_type == OPEN_SYNC_CHAR)
656 {
657 port->open_type = OPEN_NOT;
658 }
659 return retval;
660 }
661
662 MOD_INC_USE_COUNT; /* might block */
663 /* note logic different from tty
664 open failure does not call the
665 close routine */
666 retval = sab8253x_block_til_readyC(filep, port); /* need to wait for completion of callout */
667 if(retval)
668 {
669 if(port->open_type == OPEN_SYNC_CHAR)
670 {
671 port->open_type = OPEN_NOT;
672 }
673 MOD_DEC_USE_COUNT; /* something went wrong */
674 return retval;
675 }
676
677 port->tty = NULL;
678 port->open_type = OPEN_SYNC_CHAR;
679 if(Sab8253xSetUpLists(port))
680 {
681 port->open_type = OPEN_NOT;
682 return -ENODEV;
683 }
684 if(Sab8253xInitDescriptors2(port, sab8253xc_listsize, sab8253xc_rbufsize))
685 {
686 port->open_type = OPEN_NOT;
687 Sab8253xCleanUpTransceiveN(port); /* the network functions should be okay -- only difference */
688 /* is the crc32 that is appended */
689 return -ENODEV;
690 }
691 retval = sab8253x_startupC(port); /* ditto */
692 if (retval)
693 {
694 port->open_type = OPEN_NOT;
695 Sab8253xCleanUpTransceiveN(port);
696 return retval;
697 }
698 port->tx_full = 0;
699 port->rx_empty = 1;
700 port->count++;
701 port->session = current->session;
702 port->pgrp = current->pgrp;
703 filep->private_data = port;
704 MOD_INC_USE_COUNT;
705 return 0; /* success */
706 }
707
sab8253xc_release(struct inode * inodep,struct file * filep)708 int sab8253xc_release(struct inode *inodep, struct file *filep)
709 {
710 SAB_PORT *port = (SAB_PORT*) filep->private_data;
711 unsigned long flags;
712
713 save_flags(flags); cli();
714
715 --(port->count);
716 if(port->count <= 0)
717 {
718 sab8253x_shutdownN(port);
719 Sab8253xCleanUpTransceiveN(port);
720 port->count = 0;
721 port->open_type = OPEN_NOT;
722 }
723 sab8253xc_fasync(-1, filep, 0);
724 MOD_DEC_USE_COUNT;
725 restore_flags(flags);
726 return 0;
727 }
728
sab8253xc_poll(struct file * fileobj,struct poll_table_struct * polltab)729 unsigned int sab8253xc_poll(struct file *fileobj, struct poll_table_struct *polltab)
730 {
731 SAB_PORT *port = fileobj->private_data;
732 unsigned int mask = 0;
733
734 poll_wait(fileobj, &port->write_wait, polltab);
735 poll_wait(fileobj, &port->read_wait, polltab);
736 if(port->rx_empty == 0)
737 {
738 mask |= POLLIN | POLLRDNORM;
739 }
740 if(port->tx_full == 0)
741 {
742 mask |= POLLOUT | POLLWRNORM;
743 }
744 return mask;
745 }
746
sab8253xc_ioctl(struct inode * iobj,struct file * fileobj,unsigned int cmd,unsigned long length)747 int sab8253xc_ioctl(struct inode *iobj, struct file *fileobj, unsigned int cmd, unsigned long length)
748 {
749 return 0;
750 }
751
sab8253xc_fasync(int fd,struct file * fileobj,int mode)752 int sab8253xc_fasync(int fd, struct file * fileobj, int mode)
753 {
754 SAB_PORT *port = fileobj->private_data;
755
756 return fasync_helper(fd, fileobj, mode, &port->async_queue); /* I am a little baffled -- does async_helper */
757 /* work on the basis of a port or on an open */
758 /* basis*/
759 }
760
761