8253xsyn.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:27k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* -*- linux-c -*- */
  2. /* $Id: 8253xsyn.c,v 1.17 2002/02/10 22:17:25 martillo Exp $
  3.  * 8253xsyn.c: SYNC TTY Driver for the SIEMENS SAB8253X DUSCC.
  4.  *
  5.  * Implementation, modifications and extensions
  6.  * Copyright (C) 2001 By Joachim Martillo, Telford Tools, Inc.
  7.  *
  8.  * This program is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU General Public License
  10.  * as published by the Free Software Foundation; either version
  11.  * 2 of the License, or (at your option) any later version.
  12.  */
  13. /* Standard in kernel modules */
  14. #define DEFINE_VARIABLE
  15. #include <linux/module.h>   /* Specifically, a module */
  16. #include <asm/io.h>
  17. #include <linux/timer.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/tty.h>
  20. #include <linux/tty_flip.h>
  21. #include <linux/mm.h>
  22. #include <linux/version.h>
  23. #include <asm/uaccess.h>
  24. #include "8253xctl.h"
  25. #include "8253x.h"
  26. #include <linux/pci.h>
  27. #include <linux/fs.h>
  28. #ifdef MODULE
  29. #undef XCONFIG_SERIAL_CONSOLE
  30. #endif
  31. static void sab8253x_flush_to_ldiscS(void *private_) /* need a separate version for sync
  32.  there are no flags associated with
  33.  received sync TTY data*/
  34. {
  35. struct tty_struct *tty = (struct tty_struct *) private_;
  36. unsigned char *cp;
  37. int count;
  38. struct sab_port *port;
  39. struct sk_buff *skb;  
  40. if(tty)
  41. {
  42. port = (struct sab_port *)tty->driver_data;
  43. }
  44. else
  45. {
  46. return;
  47. }
  48. if(port == NULL)
  49. {
  50. return;
  51. }
  52. if (test_bit(TTY_DONT_FLIP, &tty->flags)) 
  53. {
  54. queue_task(&tty->flip.tqueue, &tq_timer);
  55. return;
  56. }
  57. /* note that a hangup may have occurred -- perhaps should check for that */
  58. port->DoingInterrupt = 1;
  59. while(port->sab8253xc_rcvbuflist && (skb_queue_len(port->sab8253xc_rcvbuflist) > 0))
  60. {
  61. skb = skb_dequeue(port->sab8253xc_rcvbuflist);
  62. count = skb->data_len;
  63. cp = skb->data;
  64. (*tty->ldisc.receive_buf)(tty, cp, 0, count);
  65. dev_kfree_skb_any(skb);
  66. }
  67. port->DoingInterrupt = 0;
  68. }
  69. void sab8253x_flush_charsS(struct tty_struct *tty)
  70. {
  71. struct sab_port *port = (struct sab_port *)tty->driver_data;
  72. if (sab8253x_serial_paranoia_check(port, tty->device, "sab8253x_flush_chars"))
  73. {
  74. return;
  75. }
  76. if ((Sab8253xCountTransmit(port) <= 0) || tty->stopped || tty->hw_stopped)
  77. { /* can't flush */
  78. return;
  79. }
  80. sab8253x_start_txS(port);
  81. }
  82. /*
  83.  * ------------------------------------------------------------
  84.  * sab8253x_stopS() and sab8253x_startS()
  85.  *
  86.  * This routines are called before setting or resetting tty->stopped.
  87.  * They enable or disable transmitter interrupts, as necessary.
  88.  * ------------------------------------------------------------
  89.  */
  90. void sab8253x_stopS(struct tty_struct *tty)
  91. {
  92. struct sab_port *port = (struct sab_port *)tty->driver_data;
  93. /* can't do anything here */
  94. if (sab8253x_serial_paranoia_check(port, tty->device, "sab8253x_stop"))
  95. {
  96. return;
  97. }
  98. /*  interrupt handles it all*/
  99. /* turning off XPR is not an option in sync mode */
  100. }
  101. void sab8253x_startS(struct tty_struct *tty)
  102. {
  103. struct sab_port *port = (struct sab_port *)tty->driver_data;
  104. if (sab8253x_serial_paranoia_check(port, tty->device, "sab8253x_start"))
  105. {
  106. return;
  107. }
  108. sab8253x_start_txS(port);
  109. }
  110. static void sab8253x_receive_charsS(struct sab_port *port,
  111.      union sab8253x_irq_status *stat)
  112. {
  113. struct tty_struct *tty = port->tty;
  114. unsigned char buf[32];
  115. int free_fifo = 0;
  116. int reset_fifo = 0;
  117. int msg_done = 0;
  118. int msg_bad = 0;
  119. int count = 0;
  120. int total_size = 0;
  121. int rstatus = 0;
  122. struct sk_buff *skb;
  123. /* Read number of BYTES (Character + Status) available. */
  124. if((stat->images[ISR1_IDX] & SAB82532_ISR1_RDO) || (stat->images[ISR0_IDX] & SAB82532_ISR0_RFO) )
  125. {
  126. ++msg_bad;
  127. ++free_fifo;
  128. ++reset_fifo;
  129. }
  130. else
  131. {
  132. if (stat->images[ISR0_IDX] & SAB82532_ISR0_RPF) 
  133. {
  134. count = port->recv_fifo_size;
  135. ++free_fifo;
  136. }
  137. if (stat->images[ISR0_IDX] & SAB82532_ISR0_RME) 
  138. {
  139. count = READB(port, rbcl);
  140. count &= (port->recv_fifo_size - 1);
  141. ++msg_done;
  142. ++free_fifo;
  143. total_size = READB(port, rbch);
  144. if(total_size & SAB82532_RBCH_OV) /* need to revisit for 4096 byte frames */
  145. {
  146. msg_bad++;
  147. }
  148. rstatus = READB(port, rsta);
  149. if((rstatus & SAB82532_RSTA_VFR) == 0)
  150. {
  151. msg_bad++;
  152. }
  153. if(rstatus & SAB82532_RSTA_RDO)
  154. {
  155. msg_bad++;
  156. }
  157. if((rstatus & SAB82532_RSTA_CRC) == 0)
  158. {
  159. msg_bad++;
  160. }
  161. if(rstatus & SAB82532_RSTA_RAB)
  162. {
  163. msg_bad++;
  164. }
  165. }
  166. }
  167. /* Read the FIFO. */
  168. (*port->readfifo)(port, buf, count);
  169. /* Issue Receive Message Complete command. */
  170. if (free_fifo) 
  171. {
  172. sab8253x_cec_wait(port);
  173. WRITEB(port, cmdr, SAB82532_CMDR_RMC);
  174. }
  175. if(reset_fifo)
  176. {
  177. sab8253x_cec_wait(port);
  178. WRITEB(port, cmdr, SAB82532_CMDR_RHR);
  179. }
  180. if(msg_bad)
  181. {
  182. port->msgbufindex = 0;
  183. return;
  184. }
  185. memcpy(&port->msgbuf[port->msgbufindex], buf, count);
  186. port->msgbufindex += count;
  187. #ifdef CONSOLE_SUPPORT
  188. if (port->is_console)
  189. {
  190. wake_up(&keypress_wait);
  191. }
  192. #endif
  193. if(msg_done)
  194. {
  195. if(port->msgbufindex <= 3) /* min is 1 char + 2 CRC + status byte */
  196. {
  197. port->msgbufindex = 0;
  198. return;
  199. }
  200. total_size = port->msgbufindex - 3; /* strip off the crc16 and the status byte */
  201. port->msgbufindex = 0;
  202. /* ignore the receive buffer waiting -- we know the correct size here */
  203. if (!tty)
  204. {
  205. return;
  206. }
  207. if(skb = dev_alloc_skb(total_size), skb)
  208. {
  209. memcpy(skb->data, &port->msgbuf[0], total_size);
  210. skb->tail = (skb->data + total_size);
  211. skb->data_len = total_size;
  212. skb->len = total_size;
  213. skb_queue_tail(port->sab8253xc_rcvbuflist, skb);
  214. }
  215. queue_task(&tty->flip.tqueue, &tq_timer); /* clear out flip buffer as fast as possible
  216.    * maybe should not be done unconditionally hear
  217.    * but should be within the above consequence
  218.    * clause */
  219. }
  220. }
  221. static void sab8253x_check_statusS(struct sab_port *port,
  222.     union sab8253x_irq_status *stat)
  223. {
  224. struct tty_struct *tty = port->tty;
  225. int modem_change = 0;
  226. mctlsig_t         *sig;
  227. if (!tty)
  228. {
  229. return;
  230. }
  231. /* check_modem:*/
  232. /* Checking DCD */
  233. sig = &port->dcd;
  234. if (stat->images[sig->irq] & sig->irqmask) 
  235. {
  236. sig->val = ISON(port,dcd);
  237. port->icount.dcd++;
  238. modem_change++;
  239. }
  240. /* Checking CTS */
  241. sig = &port->cts;
  242. if (stat->images[sig->irq] & sig->irqmask) 
  243. {
  244. sig->val = ISON(port,cts);
  245. port->icount.cts++;
  246. modem_change++;
  247. }
  248. /* Checking DSR */
  249. sig = &port->dsr;
  250. if (stat->images[sig->irq] & sig->irqmask) 
  251. {
  252. sig->val = ISON(port,dsr);
  253. port->icount.dsr++;
  254. modem_change++;
  255. }
  256. if (modem_change)
  257. {
  258. wake_up_interruptible(&port->delta_msr_wait);
  259. }
  260. sig = &port->dcd;
  261. if ((port->flags & FLAG8253X_CHECK_CD) &&
  262.     (stat->images[sig->irq] & sig->irqmask)) 
  263. {
  264. if (sig->val)
  265. {
  266. wake_up_interruptible(&port->open_wait);
  267. }
  268. else if (!((port->flags & FLAG8253X_CALLOUT_ACTIVE) &&
  269.    (port->flags & FLAG8253X_CALLOUT_NOHUP))) 
  270. {
  271. #if 0 /* requires more investigation */
  272. MOD_INC_USE_COUNT;
  273. if (schedule_task(&port->tqueue_hangup) == 0)
  274. {
  275. MOD_DEC_USE_COUNT;
  276. }
  277. #endif
  278. }
  279. }
  280. sig = &port->cts;
  281. if (port->flags & FLAG8253X_CTS_FLOW) 
  282. { /* not setting this yet */
  283. if (port->tty->hw_stopped) 
  284. {
  285. if (sig->val) 
  286. {
  287. port->tty->hw_stopped = 0;
  288. sab8253x_sched_event(port, SAB8253X_EVENT_WRITE_WAKEUP);
  289. sab8253x_start_txS(port);
  290. }
  291. else 
  292. {
  293. if(!(getccr2configS(port) & SAB82532_CCR2_TOE))
  294. {
  295. if (!(sig->val)) 
  296. {
  297. port->tty->hw_stopped = 1;
  298. }
  299. }
  300. }
  301. }
  302. }
  303. /*
  304.  * This routine is called to set the UART divisor registers to match
  305.  * the specified baud rate for a serial port.
  306.  */
  307. static void sab8253x_change_speedS(struct sab_port *port)
  308. {
  309. unsigned long flags,baud;
  310. tcflag_t cflag;
  311. u8         ccr2=0,ccr4=0,ebrg=0;
  312. int i, bits;
  313. #ifdef DEBUGGING
  314. printk("Change speed!  ");
  315. #endif
  316. if (!port->tty || !port->tty->termios) 
  317. {
  318. #ifdef DEBUGGING
  319. printk("NOT!n");
  320. #endif
  321. return;
  322. }
  323. #ifdef DEBUGGING
  324. printk(" for real.n");
  325. #endif
  326. cflag = port->tty->termios->c_cflag;
  327. /* Byte size and parity */
  328. switch (cflag & CSIZE) 
  329. {
  330. case CS5: 
  331. bits = 7; 
  332. break;
  333. case CS6: 
  334. bits = 8; 
  335. break;
  336. case CS7: 
  337. bits = 9; 
  338. break;
  339. default:
  340. case CS8: 
  341. bits = 10; 
  342. break;
  343. }
  344. if (cflag & CSTOPB) 
  345. {
  346. bits++;
  347. }
  348. if (cflag & PARENB) 
  349. {
  350. bits++;
  351. }
  352. /* Determine EBRG values based on the "encoded"baud rate */
  353. i = cflag & CBAUD;
  354. switch(i)
  355. {
  356. case B0:
  357. baud=0;
  358. break;
  359. case  B50:
  360. baud=100;
  361. break;
  362. case  B75:
  363. baud=150;
  364. break;
  365. case  B110:
  366. baud=220;
  367. break;
  368. case  B134:
  369. baud=269;
  370. break;
  371. case  B150:
  372. baud=300;
  373. break;
  374. case  B200:
  375. baud=400;
  376. break;
  377. case B300:
  378. baud=600;
  379. break;
  380. case B600:
  381. baud=1200;
  382. break;
  383. case B1200:
  384. baud=2400;
  385. break;
  386. case B1800:
  387. baud=3600;
  388. break;
  389. case B2400:
  390. baud=4800;
  391. break;
  392. case B4800:
  393. baud=9600;
  394. break;
  395. case B9600:
  396. baud=19200;
  397. break;
  398. case B19200:
  399. baud=38400;
  400. break;
  401. case  B38400:
  402. if(port->custspeed)
  403. {
  404. baud=port->custspeed<<1;
  405. }
  406. else
  407. {
  408. baud=76800;
  409. }
  410. break;
  411. case B57600:
  412. baud=115200;
  413. break;
  414. #ifdef SKIPTHIS
  415. case B76800:
  416. baud=153600;
  417. break;
  418. case B153600:
  419. baud=307200;
  420. break;
  421. #endif
  422. case B230400:
  423. baud=460800;
  424. break;
  425. case  B460800:
  426. baud=921600;
  427. break;
  428. case B115200:
  429. default:
  430. baud=230400;
  431. break;
  432. }
  433. if(!sab8253x_baud(port,baud,&ebrg,&ccr2,&ccr4,&(port->baud))) 
  434. {
  435. printk("Aurora Warning. baudrate %ld could not be set! Using 115200",baud);
  436. baud=230400;
  437. sab8253x_baud(port,baud,&ebrg,&ccr2,&ccr4,&(port->baud));
  438. }
  439. if (port->baud)
  440. port->timeout = (port->xmit_fifo_size * HZ * bits) / port->baud;
  441. else
  442. port->timeout = 0;
  443. port->timeout += HZ / 50; /* Add .02 seconds of slop */
  444. /* CTS flow control flags */
  445. if (cflag & CRTSCTS)
  446. port->flags |= FLAG8253X_CTS_FLOW;
  447. else
  448. port->flags &= ~(FLAG8253X_CTS_FLOW);
  449. if (cflag & CLOCAL)
  450. port->flags &= ~(FLAG8253X_CHECK_CD);
  451. else
  452. port->flags |= FLAG8253X_CHECK_CD;
  453. if (port->tty)
  454. port->tty->hw_stopped = 0;
  455. /*
  456.  * Set up parity check flag
  457.  * XXX: not implemented, yet.
  458.  */
  459. #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
  460. /*
  461.  * Characters to ignore
  462.  * XXX: not implemented, yet.
  463.  */
  464. /*
  465.  * !!! ignore all characters if CREAD is not set
  466.  * XXX: not implemented, yet.
  467.  */
  468. if ((cflag & CREAD) == 0)
  469. port->ignore_status_mask |= SAB82532_ISR0_RPF;
  470. save_flags(flags); 
  471. cli();
  472. sab8253x_cec_wait(port);
  473. WRITEB(port, bgr, ebrg);
  474. WRITEB(port, ccr2, READB(port, ccr2) & ~(0xc0)); /* clear out current baud rage */
  475. WRITEB(port, ccr2, READB(port, ccr2) | ccr2);
  476. WRITEB(port, ccr4, (READB(port,ccr4) & ~SAB82532_CCR4_EBRG) | ccr4);
  477. if (port->flags & FLAG8253X_CTS_FLOW) 
  478. {
  479. WRITEB(port, mode, READB(port,mode) & ~(SAB82532_MODE_RTS));
  480. port->interrupt_mask1 &= ~(SAB82532_IMR1_CSC);
  481. WRITEB(port, imr1, port->interrupt_mask1);
  482. else 
  483. {
  484. WRITEB(port, mode, READB(port,mode) | SAB82532_MODE_RTS);
  485. port->interrupt_mask1 |= SAB82532_IMR1_CSC;
  486. WRITEB(port, imr1, port->interrupt_mask1);
  487. }
  488. WRITEB(port, mode, READB(port, mode) | SAB82532_MODE_RAC);
  489. restore_flags(flags);
  490. }
  491. void sab8253x_set_termiosS(struct tty_struct *tty,
  492.    struct termios *old_termios)
  493. {
  494. struct sab_port *port = (struct sab_port *)tty->driver_data;
  495. if((tty->termios->c_cflag == old_termios->c_cflag) && 
  496.    (RELEVANT_IFLAG(tty->termios->c_iflag) == RELEVANT_IFLAG(old_termios->c_iflag)))
  497. {
  498. return;
  499. }
  500. if(!port)
  501. {
  502. return;
  503. }
  504. sab8253x_change_speedS(port);
  505. /* Handle transition to B0 status */
  506. if ((old_termios->c_cflag & CBAUD) &&
  507.     !(tty->termios->c_cflag & CBAUD)) 
  508. {
  509. LOWER(port,rts);
  510. LOWER(port,dtr);
  511. }
  512. /* Handle transition away from B0 status */
  513. if (!(old_termios->c_cflag & CBAUD) &&
  514.     (tty->termios->c_cflag & CBAUD)) 
  515. {
  516. RAISE(port,dtr);
  517. if (!tty->hw_stopped ||
  518.     !(tty->termios->c_cflag & CRTSCTS)) 
  519. {
  520. RAISE(port,rts);
  521. }
  522. }
  523. /* Handle turning off CRTSCTS */
  524. if ((old_termios->c_cflag & CRTSCTS) &&
  525.     !(tty->termios->c_cflag & CRTSCTS)) 
  526. {
  527. tty->hw_stopped = 0;
  528. sab8253x_startS(tty);
  529. }
  530. }
  531. static int sab8253x_startupS(struct sab_port *port)
  532. {
  533. unsigned long flags;
  534. int retval = 0;
  535. save_flags(flags); cli();
  536. port->msgbufindex = 0;
  537. port->xmit_buf = NULL;
  538. port->buffergreedy = 0;
  539. if (port->flags & FLAG8253X_INITIALIZED) 
  540. {
  541. goto errout;
  542. }
  543. if (!port->regs) 
  544. {
  545. if (port->tty)
  546. {
  547. set_bit(TTY_IO_ERROR, &port->tty->flags);
  548. }
  549. retval = -ENODEV;
  550. goto errout;
  551. }
  552. /*
  553.  * Initialize the Hardware
  554.  */
  555. sab8253x_init_lineS(port);
  556. #if 0 /* maybe should be conditional */
  557. if (port->tty->termios->c_cflag & CBAUD) 
  558. {
  559. #endif
  560. /* Activate RTS */
  561. RAISE(port,rts);
  562. /* Activate DTR */
  563. RAISE(port,dtr);
  564. #if 0
  565. }
  566. #endif
  567. /*
  568.  * Initialize the modem signals values
  569.  */
  570. port->dcd.val=ISON(port,dcd);
  571. port->cts.val=ISON(port,cts);
  572. port->dsr.val=ISON(port,dsr);
  573. /*
  574.  * Finally, enable interrupts
  575.  */
  576. port->interrupt_mask0 = SAB82532_IMR0_RFS | SAB82532_IMR0_PCE |
  577. SAB82532_IMR0_PLLA | SAB82532_IMR0_RSC | SAB82532_IMR0_CDSC;
  578. /*((port->ccontrol.ccr2 & SAB82532_CCR2_TOE) ? SAB82532_IMR0_CDSC : 0); */
  579. WRITEB(port,imr0,port->interrupt_mask0);
  580. port->interrupt_mask1 = SAB82532_IMR1_EOP | SAB82532_IMR1_XMR |
  581. SAB82532_IMR1_TIN | SAB82532_IMR1_XPR;
  582. WRITEB(port, imr1, port->interrupt_mask1);
  583. port->all_sent = 1;
  584. if (port->tty)
  585. {
  586. clear_bit(TTY_IO_ERROR, &port->tty->flags);
  587. }
  588. port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
  589. /*
  590.  * and set the speed of the serial port
  591.  */
  592. sab8253x_change_speedS(port);
  593. port->flags |= FLAG8253X_INITIALIZED;
  594. port->receive_chars = sab8253x_receive_charsS;
  595. port->transmit_chars = sab8253x_transmit_charsS;
  596. port->check_status = sab8253x_check_statusS;
  597. port->receive_test = (SAB82532_ISR0_RME | SAB82532_ISR0_RFO | SAB82532_ISR0_RPF);
  598. port->transmit_test = (SAB82532_ISR1_ALLS | SAB82532_ISR1_RDO | SAB82532_ISR1_XPR |
  599.        SAB82532_ISR1_XDU | SAB82532_ISR1_CSC);
  600. port->check_status_test = (SAB82532_ISR1_CSC);
  601. /*((port->ccontrol.ccr2 & SAB82532_CCR2_TOE) ? 0 : SAB82532_ISR0_CDSC));*/
  602. restore_flags(flags);
  603. return 0;
  604.  errout:
  605. restore_flags(flags);
  606. return retval;
  607. }
  608. static void sab8253x_shutdownS(struct sab_port *port)
  609. {
  610. unsigned long flags;
  611. if (!(port->flags & FLAG8253X_INITIALIZED))
  612. {
  613. return;
  614. }
  615. save_flags(flags); cli(); /* Disable interrupts */
  616. /*
  617.  * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
  618.  * here so the queue might never be waken up
  619.  */
  620. wake_up_interruptible(&port->delta_msr_wait);
  621. if (port->xmit_buf) 
  622. {
  623. port->xmit_buf = 0;
  624. }
  625. #ifdef XCONFIG_SERIAL_CONSOLE
  626. if (port->is_console) 
  627. {
  628. port->interrupt_mask0 = 
  629. SAB82532_IMR0_PERR | SAB82532_IMR0_FERR |
  630. /*SAB82532_IMR0_TIME |*/
  631. SAB82532_IMR0_PLLA | SAB82532_IMR0_CDSC;
  632. WRITEB(port,imr0,port->interrupt_mask0);
  633. port->interrupt_mask1 = 
  634. SAB82532_IMR1_BRKT | SAB82532_IMR1_ALLS |
  635. SAB82532_IMR1_XOFF | SAB82532_IMR1_TIN |
  636. SAB82532_IMR1_CSC | SAB82532_IMR1_XON |
  637. SAB82532_IMR1_XPR;
  638. WRITEB(port,imr1,port->interrupt_mask1);
  639. if (port->tty)
  640. {
  641. set_bit(TTY_IO_ERROR, &port->tty->flags);
  642. }
  643. port->flags &= ~FLAG8253X_INITIALIZED;
  644. restore_flags(flags);
  645. return;
  646. }
  647. #endif
  648. /* Disable Interrupts */
  649. port->interrupt_mask0 = 0xff;
  650. WRITEB(port, imr0, port->interrupt_mask0);
  651. port->interrupt_mask1 = 0xff;
  652. WRITEB(port, imr1, port->interrupt_mask1);
  653. if (!port->tty || (port->tty->termios->c_cflag & HUPCL)) 
  654. {
  655. LOWER(port,rts);
  656. LOWER(port,dtr);
  657. }
  658. /* Disable Receiver */
  659. CLEAR_REG_BIT(port,mode,SAB82532_MODE_RAC);
  660. /* Power Down */
  661. CLEAR_REG_BIT(port,ccr0,SAB82532_CCR0_PU);
  662. if (port->tty)
  663. {
  664. set_bit(TTY_IO_ERROR, &port->tty->flags);
  665. }
  666. port->flags &= ~FLAG8253X_INITIALIZED;
  667. restore_flags(flags);
  668. }
  669. int sab8253x_writeS(struct tty_struct * tty, int from_user,
  670.     const unsigned char *buf, int count)
  671. {
  672. struct sab_port *port = (struct sab_port *)tty->driver_data;
  673. struct sk_buff *skb;
  674. int truelength = 0;
  675. int do_queue = 1;
  676. if (sab8253x_serial_paranoia_check(port, tty->device, "sab8253x_write"))
  677. {
  678. return 0;
  679. }
  680. if(count == 0)
  681. {
  682. return 0;
  683. }
  684. if(port->active2.transmit == NULL)
  685. {
  686. return 0;
  687. }
  688. if((port->active2.transmit->Count & OWNER) == OWN_SAB)
  689. {
  690. sab8253x_start_txS(port); /* no descriptor slot */
  691. return 0;
  692. }
  693. #ifndef FREEININTERRUPT
  694. skb = port->active2.transmit->HostVaddr; /* current slot value */
  695. if(port->buffergreedy == 0) /* are we avoiding buffer free's */
  696. { /* no */
  697. if((skb != NULL) || /* not OWN_SAB from above */
  698.    (port->active2.transmit->crcindex != 0)) 
  699. {
  700. register RING_DESCRIPTOR *freeme;
  701. freeme = port->active2.transmit;
  702. do
  703. {
  704. if((freeme->crcindex == 0) && (freeme->HostVaddr == NULL))
  705. {
  706. break;
  707. }
  708. if(freeme->HostVaddr)
  709. {
  710. skb_unlink((struct sk_buff*)freeme->HostVaddr);
  711. dev_kfree_skb_any((struct sk_buff*)freeme->HostVaddr);
  712. freeme->HostVaddr = NULL;
  713. }
  714. freeme->sendcrc = 0;
  715. freeme->crcindex = 0;
  716. freeme = (RING_DESCRIPTOR*) freeme->VNext;
  717. }
  718. while((freeme->Count & OWNER) != OWN_SAB);
  719. }
  720. skb = NULL; /* buffer was freed */
  721. }
  722. if(skb != NULL) /* potentially useful */
  723. {
  724. truelength = (skb->end - skb->head);
  725. if(truelength >= count)
  726. {
  727. skb->data = skb->head; /* this buffer is already queued */
  728. skb->tail = skb->head;
  729. do_queue = 0;
  730. }
  731. else
  732. {
  733. skb_unlink(skb);
  734. dev_kfree_skb_any(skb);
  735. skb = NULL;
  736. port->active2.transmit->HostVaddr = NULL;
  737. }
  738. }
  739. /* in all cases the following is allowed */
  740. port->active2.transmit->sendcrc = 0;
  741. port->active2.transmit->crcindex = 0;
  742. #endif
  743. if(skb == NULL)
  744. {
  745. if(port->DoingInterrupt)
  746. {
  747. skb = alloc_skb(count, GFP_ATOMIC);
  748. }
  749. else
  750. {
  751. skb = alloc_skb(count, GFP_KERNEL);
  752. }
  753. }
  754. if(skb == NULL)
  755. {
  756. printk(KERN_ALERT "sab8253xs: no skbuffs available.n");
  757. return 0;
  758. }
  759. if(from_user)
  760. {
  761. copy_from_user(skb->data, buf, count);
  762. }
  763. else
  764. {
  765. memcpy(skb->data, buf, count);
  766. }
  767. skb->tail = (skb->data + count);
  768. skb->data_len = count;
  769. skb->len = count;
  770. if(do_queue)
  771. {
  772. skb_queue_head(port->sab8253xbuflist, skb);
  773. }
  774. port->active2.transmit->HostVaddr = skb;
  775. port->active2.transmit->sendcrc = 0;
  776. port->active2.transmit->crcindex = 0;
  777. port->active2.transmit->Count = (OWN_SAB|count);
  778. port->active2.transmit = port->active2.transmit->VNext;
  779. sab8253x_start_txS(port);
  780. return count;
  781. }
  782. void sab8253x_throttleS(struct tty_struct * tty)
  783. {
  784. struct sab_port *port = (struct sab_port *)tty->driver_data;
  785. if (sab8253x_serial_paranoia_check(port, tty->device, "sab8253x_throttleS"))
  786. {
  787. return;
  788. }
  789. if (!tty)
  790. {
  791. return;
  792. }
  793. if (I_IXOFF(tty))
  794. {
  795. sab8253x_send_xcharS(tty, STOP_CHAR(tty));
  796. }
  797. }
  798. void sab8253x_unthrottleS(struct tty_struct * tty)
  799. {
  800. struct sab_port *port = (struct sab_port *)tty->driver_data;
  801. if (sab8253x_serial_paranoia_check(port, tty->device, "sab8253x_unthrottle"))
  802. {
  803. return;
  804. }
  805. if (!tty)
  806. {
  807. return;
  808. }
  809. if (I_IXOFF(tty)) 
  810. {
  811. sab8253x_send_xcharS(tty, START_CHAR(tty));
  812. }
  813. }
  814. void sab8253x_send_xcharS(struct tty_struct *tty, char ch)
  815. {
  816. struct sab_port *port = (struct sab_port *)tty->driver_data;
  817. unsigned long flags;
  818. int stopped;
  819. int hw_stopped;
  820. if (sab8253x_serial_paranoia_check(port, tty->device, "sab8253x_send_xcharS"))
  821. {
  822. return;
  823. }
  824. if (!tty)
  825. {
  826. return;
  827. }
  828. if(port->sabnext2.transmit == NULL)
  829. {
  830. return;
  831. }
  832. save_flags(flags); cli();
  833. if((port->sabnext2.transmit->Count & OWNER) == OWN_SAB) /* may overwrite a character
  834.  * -- but putting subsequent
  835.  * XONs or XOFFs later in the
  836.  * stream could cause problems
  837.  * with the XON and XOFF protocol */
  838. {
  839. port->sabnext2.transmit->sendcrc = 1;
  840. port->sabnext2.transmit->crcindex = 3;
  841. port->sabnext2.transmit->crc = (ch << 24); /* LITTLE ENDIAN */
  842. restore_flags(flags);
  843. }
  844. else
  845. {
  846. restore_flags(flags);
  847. sab8253x_writeS(tty, 0, &ch, 1);
  848. }
  849. stopped = tty->stopped;
  850. hw_stopped = tty->hw_stopped;
  851. tty->stopped = 0;
  852. tty->hw_stopped = 0;
  853. sab8253x_start_txS(port);
  854. tty->stopped = stopped;
  855. tty->hw_stopped = hw_stopped;
  856. }
  857. void sab8253x_breakS(struct tty_struct *tty, int break_state)
  858. {
  859. struct sab_port *port = (struct sab_port *) tty->driver_data;
  860. if (sab8253x_serial_paranoia_check(port, tty->device, "sab8253x_breakS"))
  861. {
  862. return;
  863. } /* can't break in sync mode */
  864. }
  865. void sab8253x_closeS(struct tty_struct *tty, struct file * filp)
  866. {
  867. struct sab_port *port = (struct sab_port *)tty->driver_data;
  868. unsigned long flags;
  869. MOD_DEC_USE_COUNT;
  870. if (sab8253x_serial_paranoia_check(port, tty->device, "sab8253x_closeS"))
  871. {
  872. return;
  873. }
  874. if(port->open_type == OPEN_SYNC_NET)
  875. { /* port->tty field should already be NULL */
  876. return;
  877. }
  878. save_flags(flags); cli();
  879. --(port->count);
  880. if (tty_hung_up_p(filp)) 
  881. {
  882. if(port->count == 0) /* I think the reason for the weirdness
  883.    relates to freeing of structures in
  884.    the tty driver */
  885. {
  886. port->open_type = OPEN_NOT;
  887. }
  888. else if(port->count < 0)
  889. {
  890. printk(KERN_ALERT "XX20: port->count went negative.n");
  891. port->count = 0;
  892. port->open_type = OPEN_NOT;
  893. }
  894. restore_flags(flags);
  895. return;
  896. }
  897. #if 0
  898. if ((tty->count == 1) && (port->count != 0)) 
  899. {
  900. /*
  901.  * Uh, oh.  tty->count is 1, which means that the tty
  902.  * structure will be freed.  port->count should always
  903.  * be one in these conditions.  If it's greater than
  904.  * one, we've got real problems, since it means the
  905.  * serial port won't be shutdown.
  906.  */
  907. printk("sab8253x_close: bad serial port count; tty->count is 1,"
  908.        " port->count is %dn", port->count);
  909. port->count = 0;
  910. }
  911. #endif
  912. if (port->count < 0) 
  913. {
  914. printk(KERN_ALERT "sab8253x_close: bad serial port count for ttys%d: %dn",
  915.        port->line, port->count);
  916. port->count = 0;
  917. }
  918. if (port->count) 
  919. {
  920. restore_flags(flags);
  921. return;
  922. }
  923. port->flags |= FLAG8253X_CLOSING;
  924. /*
  925.  * Save the termios structure, since this port may have
  926.  * separate termios for callout and dialin.
  927.  */
  928. if (port->flags & FLAG8253X_NORMAL_ACTIVE)
  929. {
  930. port->normal_termios = *tty->termios;
  931. }
  932. if (port->flags & FLAG8253X_CALLOUT_ACTIVE)
  933. {
  934. port->callout_termios = *tty->termios;
  935. }
  936. /*
  937.  * Now we wait for the transmit buffer to clear; and we notify 
  938.  * the line discipline to only process XON/XOFF characters.
  939.  */
  940. tty->closing = 1;
  941. if (port->closing_wait != SAB8253X_CLOSING_WAIT_NONE) 
  942. {
  943. tty_wait_until_sent(tty, port->closing_wait);
  944. }
  945. /*
  946.  * At this point we stop accepting input.  To do this, we
  947.  * disable the receive line status interrupts, and turn off
  948.  * the receiver.
  949.  */
  950. #if 0
  951. port->interrupt_mask0 |= SAB82532_IMR0_TCD; /* not needed for sync */
  952. #endif
  953. WRITEB(port,imr0,port->interrupt_mask0);
  954. CLEAR_REG_BIT(port, mode, SAB82532_MODE_RAC); /* turn off receiver */
  955. if (port->flags & FLAG8253X_INITIALIZED) 
  956. {
  957. /*
  958.  * Before we drop DTR, make sure the UART transmitter
  959.  * has completely drained; this is especially
  960.  * important if there is a transmit FIFO!
  961.  */
  962. sab8253x_wait_until_sent(tty, port->timeout);
  963. }
  964. sab8253x_shutdownS(port);
  965. Sab8253xCleanUpTransceiveN(port);
  966. if (tty->driver.flush_buffer)
  967. {
  968. tty->driver.flush_buffer(tty);
  969. }
  970. if (tty->ldisc.flush_buffer)
  971. {
  972. tty->ldisc.flush_buffer(tty);
  973. }
  974. tty->closing = 0;
  975. port->event = 0;
  976. port->tty = 0;
  977. if (port->blocked_open) 
  978. {
  979. if (port->close_delay) 
  980. {
  981. current->state = TASK_INTERRUPTIBLE;
  982. schedule_timeout(port->close_delay);
  983. }
  984. wake_up_interruptible(&port->open_wait);
  985. }
  986. port->flags &= ~(FLAG8253X_NORMAL_ACTIVE|FLAG8253X_CALLOUT_ACTIVE|
  987.  FLAG8253X_CLOSING);
  988. wake_up_interruptible(&port->close_wait);
  989. port->open_type = OPEN_NOT;
  990. restore_flags(flags);
  991. }
  992. void sab8253x_hangupS(struct tty_struct *tty)
  993. {
  994. struct sab_port * port = (struct sab_port *)tty->driver_data;
  995. if (sab8253x_serial_paranoia_check(port, tty->device, "sab8253x_hangupS"))
  996. {
  997. return;
  998. }
  999. #ifdef XCONFIG_SERIAL_CONSOLE
  1000. if (port->is_console)
  1001. {
  1002. return;
  1003. }
  1004. #endif
  1005. sab8253x_flush_buffer(tty);
  1006. if(port)
  1007. {
  1008. sab8253x_shutdownS(port);
  1009. Sab8253xCleanUpTransceiveN(port);
  1010. port->event = 0;
  1011. port->flags &= ~(FLAG8253X_NORMAL_ACTIVE|FLAG8253X_CALLOUT_ACTIVE);
  1012. port->tty = 0;
  1013. wake_up_interruptible(&port->open_wait);
  1014. }
  1015. }
  1016. int sab8253x_openS(struct tty_struct *tty, struct file * filp)
  1017. {
  1018. struct sab_port *port;
  1019. int retval, line;
  1020. int counter;
  1021. unsigned long flags;
  1022. MOD_INC_USE_COUNT;  
  1023. line = MINOR(tty->device) - tty->driver.minor_start;
  1024. for(counter = 0, port = AuraPortRoot; 
  1025.     (counter < line) && (port != NULL); 
  1026.     ++counter)
  1027. {
  1028. port = port->next;
  1029. }
  1030. if (!port) 
  1031. {
  1032. printk(KERN_ALERT "sab8253x_openS: can't find structure for line %dn",
  1033.        line);
  1034. return -ENODEV;
  1035. }
  1036. save_flags(flags); /* Need to protect port->tty element */
  1037. cli();
  1038. if(port->tty == 0)
  1039. {
  1040. port->tty = tty;
  1041. tty->flip.tqueue.routine = sab8253x_flush_to_ldiscS;
  1042. }
  1043. tty->driver_data = port;
  1044. if(port->function != FUNCTION_NR)
  1045. {
  1046. ++(port->count);
  1047. restore_flags(flags);
  1048. return -ENODEV; /* only allowed if there are no restrictions on the port */
  1049. }
  1050. if(port->open_type == OPEN_SYNC_NET)
  1051. {
  1052. port->tty = NULL; /* Don't bother with open counting here
  1053.    but make sure the tty field is NULL*/
  1054. restore_flags(flags);
  1055. return -EBUSY;
  1056. }
  1057. restore_flags(flags);
  1058. if (sab8253x_serial_paranoia_check(port, tty->device, "sab8253x_openS"))
  1059. {
  1060. ++(port->count);
  1061. return -ENODEV;
  1062. }
  1063. #ifdef DEBUG_OPEN
  1064. printk("sab8253x_open %s%d, count = %dn", tty->driver.name, port->line,
  1065.        port->count);
  1066. #endif
  1067. /*
  1068.  * If the port is in the middle of closing, bail out now.
  1069.  */
  1070. if (tty_hung_up_p(filp) ||
  1071.     (port->flags & FLAG8253X_CLOSING)) 
  1072. {
  1073. if (port->flags & FLAG8253X_CLOSING)
  1074. {
  1075. interruptible_sleep_on(&port->close_wait);
  1076. }
  1077. #ifdef SERIAL_DO_RESTART
  1078. ++(port->count);
  1079. return ((port->flags & FLAG8253X_HUP_NOTIFY) ?
  1080. -EAGAIN : -ERESTARTSYS);
  1081. #else
  1082. ++(port->count);
  1083. return -EAGAIN;
  1084. #endif
  1085. }
  1086. if(port->flags & FLAG8253X_NORMAL_ACTIVE)
  1087. {
  1088. if(port->open_type == OPEN_ASYNC)
  1089. {
  1090. ++(port->count);
  1091. return -EBUSY; /* can't reopen in sync mode */
  1092. }
  1093. }
  1094. if(port->open_type > OPEN_SYNC) /* can reopen a SYNC_TTY */
  1095. {
  1096. return -EBUSY;
  1097. }
  1098. if(Sab8253xSetUpLists(port))
  1099. {
  1100. ++(port->count);
  1101. return -ENODEV;
  1102. }
  1103. if(Sab8253xInitDescriptors2(port, sab8253xs_listsize, sab8253xs_rbufsize))
  1104. {
  1105. ++(port->count);
  1106. return -ENODEV;
  1107. }
  1108. retval = sab8253x_startupS(port);
  1109. if (retval)
  1110. {
  1111. ++(port->count);
  1112. return retval; /* does not check channel mode */
  1113. }
  1114. retval = sab8253x_block_til_ready(tty, filp, port); /* checks channel mode */
  1115. ++(port->count);
  1116. if (retval) 
  1117. {
  1118. return retval;
  1119. }
  1120. port->tty = tty; /* may change here once through the block */
  1121. /* because now the port belongs to an new tty */
  1122. tty->flip.tqueue.routine = sab8253x_flush_to_ldiscS;
  1123. if(Sab8253xSetUpLists(port))
  1124. {
  1125. return -ENODEV;
  1126. }
  1127. if(Sab8253xInitDescriptors2(port, sab8253xs_listsize, sab8253xs_rbufsize))
  1128. {
  1129. Sab8253xCleanUpTransceiveN(port); /* the network functions should be okay -- only difference */
  1130. /* is the crc32 that is appended */
  1131. return -ENODEV;
  1132. }
  1133. /*
  1134.  * Start up serial port
  1135.  */
  1136. retval = sab8253x_startupS(port); /* in case cu was running the first time
  1137.    * the function was called*/
  1138. if (retval)
  1139. {
  1140. return retval; /* does not check channel mode */
  1141. }
  1142. if ((port->count == 1) &&
  1143.     (port->flags & FLAG8253X_SPLIT_TERMIOS)) 
  1144. {
  1145. if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
  1146. {
  1147. *tty->termios = port->normal_termios;
  1148. }
  1149. else 
  1150. {
  1151. *tty->termios = port->callout_termios;
  1152. }
  1153. sab8253x_change_speedS(port);
  1154. }
  1155. #ifdef XCONFIG_SERIAL_CONSOLE
  1156. if (sab8253x_console.cflag && sab8253x_console.index == line) 
  1157. {
  1158. tty->termios->c_cflag = sab8253x_console.cflag;
  1159. sab8253x_console.cflag = 0;
  1160. change_speed(port);
  1161. }
  1162. #endif
  1163. port->session = current->session;
  1164. port->pgrp = current->pgrp;
  1165. port->open_type = OPEN_SYNC;
  1166. return 0;
  1167. }