PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/tty/amiserial.c

https://bitbucket.org/wisechild/galaxy-nexus
C | 2176 lines | 1501 code | 300 blank | 375 comment | 311 complexity | 01b6291100683568ca1723f1d0b4c418 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. * Serial driver for the amiga builtin port.
  3. *
  4. * This code was created by taking serial.c version 4.30 from kernel
  5. * release 2.3.22, replacing all hardware related stuff with the
  6. * corresponding amiga hardware actions, and removing all irrelevant
  7. * code. As a consequence, it uses many of the constants and names
  8. * associated with the registers and bits of 16550 compatible UARTS -
  9. * but only to keep track of status, etc in the state variables. It
  10. * was done this was to make it easier to keep the code in line with
  11. * (non hardware specific) changes to serial.c.
  12. *
  13. * The port is registered with the tty driver as minor device 64, and
  14. * therefore other ports should should only use 65 upwards.
  15. *
  16. * Richard Lucock 28/12/99
  17. *
  18. * Copyright (C) 1991, 1992 Linus Torvalds
  19. * Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997,
  20. * 1998, 1999 Theodore Ts'o
  21. *
  22. */
  23. /*
  24. * Serial driver configuration section. Here are the various options:
  25. *
  26. * SERIAL_PARANOIA_CHECK
  27. * Check the magic number for the async_structure where
  28. * ever possible.
  29. */
  30. #include <linux/delay.h>
  31. #undef SERIAL_PARANOIA_CHECK
  32. #define SERIAL_DO_RESTART
  33. /* Set of debugging defines */
  34. #undef SERIAL_DEBUG_INTR
  35. #undef SERIAL_DEBUG_OPEN
  36. #undef SERIAL_DEBUG_FLOW
  37. #undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  38. /* Sanity checks */
  39. #if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
  40. #define DBG_CNT(s) printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
  41. tty->name, (info->flags), serial_driver->refcount,info->count,tty->count,s)
  42. #else
  43. #define DBG_CNT(s)
  44. #endif
  45. /*
  46. * End of serial driver configuration section.
  47. */
  48. #include <linux/module.h>
  49. #include <linux/types.h>
  50. #include <linux/serial.h>
  51. #include <linux/serialP.h>
  52. #include <linux/serial_reg.h>
  53. static char *serial_version = "4.30";
  54. #include <linux/errno.h>
  55. #include <linux/signal.h>
  56. #include <linux/sched.h>
  57. #include <linux/kernel.h>
  58. #include <linux/timer.h>
  59. #include <linux/interrupt.h>
  60. #include <linux/tty.h>
  61. #include <linux/tty_flip.h>
  62. #include <linux/console.h>
  63. #include <linux/major.h>
  64. #include <linux/string.h>
  65. #include <linux/fcntl.h>
  66. #include <linux/ptrace.h>
  67. #include <linux/ioport.h>
  68. #include <linux/mm.h>
  69. #include <linux/seq_file.h>
  70. #include <linux/slab.h>
  71. #include <linux/init.h>
  72. #include <linux/bitops.h>
  73. #include <linux/platform_device.h>
  74. #include <asm/setup.h>
  75. #include <asm/system.h>
  76. #include <asm/irq.h>
  77. #include <asm/amigahw.h>
  78. #include <asm/amigaints.h>
  79. #define custom amiga_custom
  80. static char *serial_name = "Amiga-builtin serial driver";
  81. static struct tty_driver *serial_driver;
  82. /* number of characters left in xmit buffer before we ask for more */
  83. #define WAKEUP_CHARS 256
  84. static struct async_struct *IRQ_ports;
  85. static unsigned char current_ctl_bits;
  86. static void change_speed(struct async_struct *info, struct ktermios *old);
  87. static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
  88. static struct serial_state rs_table[1];
  89. #define NR_PORTS ARRAY_SIZE(rs_table)
  90. #include <asm/uaccess.h>
  91. #define serial_isroot() (capable(CAP_SYS_ADMIN))
  92. static inline int serial_paranoia_check(struct async_struct *info,
  93. char *name, const char *routine)
  94. {
  95. #ifdef SERIAL_PARANOIA_CHECK
  96. static const char *badmagic =
  97. "Warning: bad magic number for serial struct (%s) in %s\n";
  98. static const char *badinfo =
  99. "Warning: null async_struct for (%s) in %s\n";
  100. if (!info) {
  101. printk(badinfo, name, routine);
  102. return 1;
  103. }
  104. if (info->magic != SERIAL_MAGIC) {
  105. printk(badmagic, name, routine);
  106. return 1;
  107. }
  108. #endif
  109. return 0;
  110. }
  111. /* some serial hardware definitions */
  112. #define SDR_OVRUN (1<<15)
  113. #define SDR_RBF (1<<14)
  114. #define SDR_TBE (1<<13)
  115. #define SDR_TSRE (1<<12)
  116. #define SERPER_PARENB (1<<15)
  117. #define AC_SETCLR (1<<15)
  118. #define AC_UARTBRK (1<<11)
  119. #define SER_DTR (1<<7)
  120. #define SER_RTS (1<<6)
  121. #define SER_DCD (1<<5)
  122. #define SER_CTS (1<<4)
  123. #define SER_DSR (1<<3)
  124. static __inline__ void rtsdtr_ctrl(int bits)
  125. {
  126. ciab.pra = ((bits & (SER_RTS | SER_DTR)) ^ (SER_RTS | SER_DTR)) | (ciab.pra & ~(SER_RTS | SER_DTR));
  127. }
  128. /*
  129. * ------------------------------------------------------------
  130. * rs_stop() and rs_start()
  131. *
  132. * This routines are called before setting or resetting tty->stopped.
  133. * They enable or disable transmitter interrupts, as necessary.
  134. * ------------------------------------------------------------
  135. */
  136. static void rs_stop(struct tty_struct *tty)
  137. {
  138. struct async_struct *info = tty->driver_data;
  139. unsigned long flags;
  140. if (serial_paranoia_check(info, tty->name, "rs_stop"))
  141. return;
  142. local_irq_save(flags);
  143. if (info->IER & UART_IER_THRI) {
  144. info->IER &= ~UART_IER_THRI;
  145. /* disable Tx interrupt and remove any pending interrupts */
  146. custom.intena = IF_TBE;
  147. mb();
  148. custom.intreq = IF_TBE;
  149. mb();
  150. }
  151. local_irq_restore(flags);
  152. }
  153. static void rs_start(struct tty_struct *tty)
  154. {
  155. struct async_struct *info = tty->driver_data;
  156. unsigned long flags;
  157. if (serial_paranoia_check(info, tty->name, "rs_start"))
  158. return;
  159. local_irq_save(flags);
  160. if (info->xmit.head != info->xmit.tail
  161. && info->xmit.buf
  162. && !(info->IER & UART_IER_THRI)) {
  163. info->IER |= UART_IER_THRI;
  164. custom.intena = IF_SETCLR | IF_TBE;
  165. mb();
  166. /* set a pending Tx Interrupt, transmitter should restart now */
  167. custom.intreq = IF_SETCLR | IF_TBE;
  168. mb();
  169. }
  170. local_irq_restore(flags);
  171. }
  172. /*
  173. * ----------------------------------------------------------------------
  174. *
  175. * Here starts the interrupt handling routines. All of the following
  176. * subroutines are declared as inline and are folded into
  177. * rs_interrupt(). They were separated out for readability's sake.
  178. *
  179. * Note: rs_interrupt() is a "fast" interrupt, which means that it
  180. * runs with interrupts turned off. People who may want to modify
  181. * rs_interrupt() should try to keep the interrupt handler as fast as
  182. * possible. After you are done making modifications, it is not a bad
  183. * idea to do:
  184. *
  185. * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
  186. *
  187. * and look at the resulting assemble code in serial.s.
  188. *
  189. * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
  190. * -----------------------------------------------------------------------
  191. */
  192. /*
  193. * This routine is used by the interrupt handler to schedule
  194. * processing in the software interrupt portion of the driver.
  195. */
  196. static void rs_sched_event(struct async_struct *info,
  197. int event)
  198. {
  199. info->event |= 1 << event;
  200. tasklet_schedule(&info->tlet);
  201. }
  202. static void receive_chars(struct async_struct *info)
  203. {
  204. int status;
  205. int serdatr;
  206. struct tty_struct *tty = info->tty;
  207. unsigned char ch, flag;
  208. struct async_icount *icount;
  209. int oe = 0;
  210. icount = &info->state->icount;
  211. status = UART_LSR_DR; /* We obviously have a character! */
  212. serdatr = custom.serdatr;
  213. mb();
  214. custom.intreq = IF_RBF;
  215. mb();
  216. if((serdatr & 0x1ff) == 0)
  217. status |= UART_LSR_BI;
  218. if(serdatr & SDR_OVRUN)
  219. status |= UART_LSR_OE;
  220. ch = serdatr & 0xff;
  221. icount->rx++;
  222. #ifdef SERIAL_DEBUG_INTR
  223. printk("DR%02x:%02x...", ch, status);
  224. #endif
  225. flag = TTY_NORMAL;
  226. /*
  227. * We don't handle parity or frame errors - but I have left
  228. * the code in, since I'm not sure that the errors can't be
  229. * detected.
  230. */
  231. if (status & (UART_LSR_BI | UART_LSR_PE |
  232. UART_LSR_FE | UART_LSR_OE)) {
  233. /*
  234. * For statistics only
  235. */
  236. if (status & UART_LSR_BI) {
  237. status &= ~(UART_LSR_FE | UART_LSR_PE);
  238. icount->brk++;
  239. } else if (status & UART_LSR_PE)
  240. icount->parity++;
  241. else if (status & UART_LSR_FE)
  242. icount->frame++;
  243. if (status & UART_LSR_OE)
  244. icount->overrun++;
  245. /*
  246. * Now check to see if character should be
  247. * ignored, and mask off conditions which
  248. * should be ignored.
  249. */
  250. if (status & info->ignore_status_mask)
  251. goto out;
  252. status &= info->read_status_mask;
  253. if (status & (UART_LSR_BI)) {
  254. #ifdef SERIAL_DEBUG_INTR
  255. printk("handling break....");
  256. #endif
  257. flag = TTY_BREAK;
  258. if (info->flags & ASYNC_SAK)
  259. do_SAK(tty);
  260. } else if (status & UART_LSR_PE)
  261. flag = TTY_PARITY;
  262. else if (status & UART_LSR_FE)
  263. flag = TTY_FRAME;
  264. if (status & UART_LSR_OE) {
  265. /*
  266. * Overrun is special, since it's
  267. * reported immediately, and doesn't
  268. * affect the current character
  269. */
  270. oe = 1;
  271. }
  272. }
  273. tty_insert_flip_char(tty, ch, flag);
  274. if (oe == 1)
  275. tty_insert_flip_char(tty, 0, TTY_OVERRUN);
  276. tty_flip_buffer_push(tty);
  277. out:
  278. return;
  279. }
  280. static void transmit_chars(struct async_struct *info)
  281. {
  282. custom.intreq = IF_TBE;
  283. mb();
  284. if (info->x_char) {
  285. custom.serdat = info->x_char | 0x100;
  286. mb();
  287. info->state->icount.tx++;
  288. info->x_char = 0;
  289. return;
  290. }
  291. if (info->xmit.head == info->xmit.tail
  292. || info->tty->stopped
  293. || info->tty->hw_stopped) {
  294. info->IER &= ~UART_IER_THRI;
  295. custom.intena = IF_TBE;
  296. mb();
  297. return;
  298. }
  299. custom.serdat = info->xmit.buf[info->xmit.tail++] | 0x100;
  300. mb();
  301. info->xmit.tail = info->xmit.tail & (SERIAL_XMIT_SIZE-1);
  302. info->state->icount.tx++;
  303. if (CIRC_CNT(info->xmit.head,
  304. info->xmit.tail,
  305. SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
  306. rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
  307. #ifdef SERIAL_DEBUG_INTR
  308. printk("THRE...");
  309. #endif
  310. if (info->xmit.head == info->xmit.tail) {
  311. custom.intena = IF_TBE;
  312. mb();
  313. info->IER &= ~UART_IER_THRI;
  314. }
  315. }
  316. static void check_modem_status(struct async_struct *info)
  317. {
  318. unsigned char status = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
  319. unsigned char dstatus;
  320. struct async_icount *icount;
  321. /* Determine bits that have changed */
  322. dstatus = status ^ current_ctl_bits;
  323. current_ctl_bits = status;
  324. if (dstatus) {
  325. icount = &info->state->icount;
  326. /* update input line counters */
  327. if (dstatus & SER_DSR)
  328. icount->dsr++;
  329. if (dstatus & SER_DCD) {
  330. icount->dcd++;
  331. #ifdef CONFIG_HARD_PPS
  332. if ((info->flags & ASYNC_HARDPPS_CD) &&
  333. !(status & SER_DCD))
  334. hardpps();
  335. #endif
  336. }
  337. if (dstatus & SER_CTS)
  338. icount->cts++;
  339. wake_up_interruptible(&info->delta_msr_wait);
  340. }
  341. if ((info->flags & ASYNC_CHECK_CD) && (dstatus & SER_DCD)) {
  342. #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
  343. printk("ttyS%d CD now %s...", info->line,
  344. (!(status & SER_DCD)) ? "on" : "off");
  345. #endif
  346. if (!(status & SER_DCD))
  347. wake_up_interruptible(&info->open_wait);
  348. else {
  349. #ifdef SERIAL_DEBUG_OPEN
  350. printk("doing serial hangup...");
  351. #endif
  352. if (info->tty)
  353. tty_hangup(info->tty);
  354. }
  355. }
  356. if (info->flags & ASYNC_CTS_FLOW) {
  357. if (info->tty->hw_stopped) {
  358. if (!(status & SER_CTS)) {
  359. #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
  360. printk("CTS tx start...");
  361. #endif
  362. info->tty->hw_stopped = 0;
  363. info->IER |= UART_IER_THRI;
  364. custom.intena = IF_SETCLR | IF_TBE;
  365. mb();
  366. /* set a pending Tx Interrupt, transmitter should restart now */
  367. custom.intreq = IF_SETCLR | IF_TBE;
  368. mb();
  369. rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
  370. return;
  371. }
  372. } else {
  373. if ((status & SER_CTS)) {
  374. #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
  375. printk("CTS tx stop...");
  376. #endif
  377. info->tty->hw_stopped = 1;
  378. info->IER &= ~UART_IER_THRI;
  379. /* disable Tx interrupt and remove any pending interrupts */
  380. custom.intena = IF_TBE;
  381. mb();
  382. custom.intreq = IF_TBE;
  383. mb();
  384. }
  385. }
  386. }
  387. }
  388. static irqreturn_t ser_vbl_int( int irq, void *data)
  389. {
  390. /* vbl is just a periodic interrupt we tie into to update modem status */
  391. struct async_struct * info = IRQ_ports;
  392. /*
  393. * TBD - is it better to unregister from this interrupt or to
  394. * ignore it if MSI is clear ?
  395. */
  396. if(info->IER & UART_IER_MSI)
  397. check_modem_status(info);
  398. return IRQ_HANDLED;
  399. }
  400. static irqreturn_t ser_rx_int(int irq, void *dev_id)
  401. {
  402. struct async_struct * info;
  403. #ifdef SERIAL_DEBUG_INTR
  404. printk("ser_rx_int...");
  405. #endif
  406. info = IRQ_ports;
  407. if (!info || !info->tty)
  408. return IRQ_NONE;
  409. receive_chars(info);
  410. info->last_active = jiffies;
  411. #ifdef SERIAL_DEBUG_INTR
  412. printk("end.\n");
  413. #endif
  414. return IRQ_HANDLED;
  415. }
  416. static irqreturn_t ser_tx_int(int irq, void *dev_id)
  417. {
  418. struct async_struct * info;
  419. if (custom.serdatr & SDR_TBE) {
  420. #ifdef SERIAL_DEBUG_INTR
  421. printk("ser_tx_int...");
  422. #endif
  423. info = IRQ_ports;
  424. if (!info || !info->tty)
  425. return IRQ_NONE;
  426. transmit_chars(info);
  427. info->last_active = jiffies;
  428. #ifdef SERIAL_DEBUG_INTR
  429. printk("end.\n");
  430. #endif
  431. }
  432. return IRQ_HANDLED;
  433. }
  434. /*
  435. * -------------------------------------------------------------------
  436. * Here ends the serial interrupt routines.
  437. * -------------------------------------------------------------------
  438. */
  439. /*
  440. * This routine is used to handle the "bottom half" processing for the
  441. * serial driver, known also the "software interrupt" processing.
  442. * This processing is done at the kernel interrupt level, after the
  443. * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
  444. * is where time-consuming activities which can not be done in the
  445. * interrupt driver proper are done; the interrupt driver schedules
  446. * them using rs_sched_event(), and they get done here.
  447. */
  448. static void do_softint(unsigned long private_)
  449. {
  450. struct async_struct *info = (struct async_struct *) private_;
  451. struct tty_struct *tty;
  452. tty = info->tty;
  453. if (!tty)
  454. return;
  455. if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event))
  456. tty_wakeup(tty);
  457. }
  458. /*
  459. * ---------------------------------------------------------------
  460. * Low level utility subroutines for the serial driver: routines to
  461. * figure out the appropriate timeout for an interrupt chain, routines
  462. * to initialize and startup a serial port, and routines to shutdown a
  463. * serial port. Useful stuff like that.
  464. * ---------------------------------------------------------------
  465. */
  466. static int startup(struct async_struct * info)
  467. {
  468. unsigned long flags;
  469. int retval=0;
  470. unsigned long page;
  471. page = get_zeroed_page(GFP_KERNEL);
  472. if (!page)
  473. return -ENOMEM;
  474. local_irq_save(flags);
  475. if (info->flags & ASYNC_INITIALIZED) {
  476. free_page(page);
  477. goto errout;
  478. }
  479. if (info->xmit.buf)
  480. free_page(page);
  481. else
  482. info->xmit.buf = (unsigned char *) page;
  483. #ifdef SERIAL_DEBUG_OPEN
  484. printk("starting up ttys%d ...", info->line);
  485. #endif
  486. /* Clear anything in the input buffer */
  487. custom.intreq = IF_RBF;
  488. mb();
  489. retval = request_irq(IRQ_AMIGA_VERTB, ser_vbl_int, 0, "serial status", info);
  490. if (retval) {
  491. if (serial_isroot()) {
  492. if (info->tty)
  493. set_bit(TTY_IO_ERROR,
  494. &info->tty->flags);
  495. retval = 0;
  496. }
  497. goto errout;
  498. }
  499. /* enable both Rx and Tx interrupts */
  500. custom.intena = IF_SETCLR | IF_RBF | IF_TBE;
  501. mb();
  502. info->IER = UART_IER_MSI;
  503. /* remember current state of the DCD and CTS bits */
  504. current_ctl_bits = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
  505. IRQ_ports = info;
  506. info->MCR = 0;
  507. if (info->tty->termios->c_cflag & CBAUD)
  508. info->MCR = SER_DTR | SER_RTS;
  509. rtsdtr_ctrl(info->MCR);
  510. if (info->tty)
  511. clear_bit(TTY_IO_ERROR, &info->tty->flags);
  512. info->xmit.head = info->xmit.tail = 0;
  513. /*
  514. * Set up the tty->alt_speed kludge
  515. */
  516. if (info->tty) {
  517. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  518. info->tty->alt_speed = 57600;
  519. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  520. info->tty->alt_speed = 115200;
  521. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
  522. info->tty->alt_speed = 230400;
  523. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
  524. info->tty->alt_speed = 460800;
  525. }
  526. /*
  527. * and set the speed of the serial port
  528. */
  529. change_speed(info, NULL);
  530. info->flags |= ASYNC_INITIALIZED;
  531. local_irq_restore(flags);
  532. return 0;
  533. errout:
  534. local_irq_restore(flags);
  535. return retval;
  536. }
  537. /*
  538. * This routine will shutdown a serial port; interrupts are disabled, and
  539. * DTR is dropped if the hangup on close termio flag is on.
  540. */
  541. static void shutdown(struct async_struct * info)
  542. {
  543. unsigned long flags;
  544. struct serial_state *state;
  545. if (!(info->flags & ASYNC_INITIALIZED))
  546. return;
  547. state = info->state;
  548. #ifdef SERIAL_DEBUG_OPEN
  549. printk("Shutting down serial port %d ....\n", info->line);
  550. #endif
  551. local_irq_save(flags); /* Disable interrupts */
  552. /*
  553. * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
  554. * here so the queue might never be waken up
  555. */
  556. wake_up_interruptible(&info->delta_msr_wait);
  557. IRQ_ports = NULL;
  558. /*
  559. * Free the IRQ, if necessary
  560. */
  561. free_irq(IRQ_AMIGA_VERTB, info);
  562. if (info->xmit.buf) {
  563. free_page((unsigned long) info->xmit.buf);
  564. info->xmit.buf = NULL;
  565. }
  566. info->IER = 0;
  567. custom.intena = IF_RBF | IF_TBE;
  568. mb();
  569. /* disable break condition */
  570. custom.adkcon = AC_UARTBRK;
  571. mb();
  572. if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
  573. info->MCR &= ~(SER_DTR|SER_RTS);
  574. rtsdtr_ctrl(info->MCR);
  575. if (info->tty)
  576. set_bit(TTY_IO_ERROR, &info->tty->flags);
  577. info->flags &= ~ASYNC_INITIALIZED;
  578. local_irq_restore(flags);
  579. }
  580. /*
  581. * This routine is called to set the UART divisor registers to match
  582. * the specified baud rate for a serial port.
  583. */
  584. static void change_speed(struct async_struct *info,
  585. struct ktermios *old_termios)
  586. {
  587. int quot = 0, baud_base, baud;
  588. unsigned cflag, cval = 0;
  589. int bits;
  590. unsigned long flags;
  591. if (!info->tty || !info->tty->termios)
  592. return;
  593. cflag = info->tty->termios->c_cflag;
  594. /* Byte size is always 8 bits plus parity bit if requested */
  595. cval = 3; bits = 10;
  596. if (cflag & CSTOPB) {
  597. cval |= 0x04;
  598. bits++;
  599. }
  600. if (cflag & PARENB) {
  601. cval |= UART_LCR_PARITY;
  602. bits++;
  603. }
  604. if (!(cflag & PARODD))
  605. cval |= UART_LCR_EPAR;
  606. #ifdef CMSPAR
  607. if (cflag & CMSPAR)
  608. cval |= UART_LCR_SPAR;
  609. #endif
  610. /* Determine divisor based on baud rate */
  611. baud = tty_get_baud_rate(info->tty);
  612. if (!baud)
  613. baud = 9600; /* B0 transition handled in rs_set_termios */
  614. baud_base = info->state->baud_base;
  615. if (baud == 38400 &&
  616. ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
  617. quot = info->state->custom_divisor;
  618. else {
  619. if (baud == 134)
  620. /* Special case since 134 is really 134.5 */
  621. quot = (2*baud_base / 269);
  622. else if (baud)
  623. quot = baud_base / baud;
  624. }
  625. /* If the quotient is zero refuse the change */
  626. if (!quot && old_termios) {
  627. /* FIXME: Will need updating for new tty in the end */
  628. info->tty->termios->c_cflag &= ~CBAUD;
  629. info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
  630. baud = tty_get_baud_rate(info->tty);
  631. if (!baud)
  632. baud = 9600;
  633. if (baud == 38400 &&
  634. ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
  635. quot = info->state->custom_divisor;
  636. else {
  637. if (baud == 134)
  638. /* Special case since 134 is really 134.5 */
  639. quot = (2*baud_base / 269);
  640. else if (baud)
  641. quot = baud_base / baud;
  642. }
  643. }
  644. /* As a last resort, if the quotient is zero, default to 9600 bps */
  645. if (!quot)
  646. quot = baud_base / 9600;
  647. info->quot = quot;
  648. info->timeout = ((info->xmit_fifo_size*HZ*bits*quot) / baud_base);
  649. info->timeout += HZ/50; /* Add .02 seconds of slop */
  650. /* CTS flow control flag and modem status interrupts */
  651. info->IER &= ~UART_IER_MSI;
  652. if (info->flags & ASYNC_HARDPPS_CD)
  653. info->IER |= UART_IER_MSI;
  654. if (cflag & CRTSCTS) {
  655. info->flags |= ASYNC_CTS_FLOW;
  656. info->IER |= UART_IER_MSI;
  657. } else
  658. info->flags &= ~ASYNC_CTS_FLOW;
  659. if (cflag & CLOCAL)
  660. info->flags &= ~ASYNC_CHECK_CD;
  661. else {
  662. info->flags |= ASYNC_CHECK_CD;
  663. info->IER |= UART_IER_MSI;
  664. }
  665. /* TBD:
  666. * Does clearing IER_MSI imply that we should disable the VBL interrupt ?
  667. */
  668. /*
  669. * Set up parity check flag
  670. */
  671. info->read_status_mask = UART_LSR_OE | UART_LSR_DR;
  672. if (I_INPCK(info->tty))
  673. info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
  674. if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
  675. info->read_status_mask |= UART_LSR_BI;
  676. /*
  677. * Characters to ignore
  678. */
  679. info->ignore_status_mask = 0;
  680. if (I_IGNPAR(info->tty))
  681. info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
  682. if (I_IGNBRK(info->tty)) {
  683. info->ignore_status_mask |= UART_LSR_BI;
  684. /*
  685. * If we're ignore parity and break indicators, ignore
  686. * overruns too. (For real raw support).
  687. */
  688. if (I_IGNPAR(info->tty))
  689. info->ignore_status_mask |= UART_LSR_OE;
  690. }
  691. /*
  692. * !!! ignore all characters if CREAD is not set
  693. */
  694. if ((cflag & CREAD) == 0)
  695. info->ignore_status_mask |= UART_LSR_DR;
  696. local_irq_save(flags);
  697. {
  698. short serper;
  699. /* Set up the baud rate */
  700. serper = quot - 1;
  701. /* Enable or disable parity bit */
  702. if(cval & UART_LCR_PARITY)
  703. serper |= (SERPER_PARENB);
  704. custom.serper = serper;
  705. mb();
  706. }
  707. info->LCR = cval; /* Save LCR */
  708. local_irq_restore(flags);
  709. }
  710. static int rs_put_char(struct tty_struct *tty, unsigned char ch)
  711. {
  712. struct async_struct *info;
  713. unsigned long flags;
  714. info = tty->driver_data;
  715. if (serial_paranoia_check(info, tty->name, "rs_put_char"))
  716. return 0;
  717. if (!info->xmit.buf)
  718. return 0;
  719. local_irq_save(flags);
  720. if (CIRC_SPACE(info->xmit.head,
  721. info->xmit.tail,
  722. SERIAL_XMIT_SIZE) == 0) {
  723. local_irq_restore(flags);
  724. return 0;
  725. }
  726. info->xmit.buf[info->xmit.head++] = ch;
  727. info->xmit.head &= SERIAL_XMIT_SIZE-1;
  728. local_irq_restore(flags);
  729. return 1;
  730. }
  731. static void rs_flush_chars(struct tty_struct *tty)
  732. {
  733. struct async_struct *info = tty->driver_data;
  734. unsigned long flags;
  735. if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
  736. return;
  737. if (info->xmit.head == info->xmit.tail
  738. || tty->stopped
  739. || tty->hw_stopped
  740. || !info->xmit.buf)
  741. return;
  742. local_irq_save(flags);
  743. info->IER |= UART_IER_THRI;
  744. custom.intena = IF_SETCLR | IF_TBE;
  745. mb();
  746. /* set a pending Tx Interrupt, transmitter should restart now */
  747. custom.intreq = IF_SETCLR | IF_TBE;
  748. mb();
  749. local_irq_restore(flags);
  750. }
  751. static int rs_write(struct tty_struct * tty, const unsigned char *buf, int count)
  752. {
  753. int c, ret = 0;
  754. struct async_struct *info;
  755. unsigned long flags;
  756. info = tty->driver_data;
  757. if (serial_paranoia_check(info, tty->name, "rs_write"))
  758. return 0;
  759. if (!info->xmit.buf)
  760. return 0;
  761. local_irq_save(flags);
  762. while (1) {
  763. c = CIRC_SPACE_TO_END(info->xmit.head,
  764. info->xmit.tail,
  765. SERIAL_XMIT_SIZE);
  766. if (count < c)
  767. c = count;
  768. if (c <= 0) {
  769. break;
  770. }
  771. memcpy(info->xmit.buf + info->xmit.head, buf, c);
  772. info->xmit.head = ((info->xmit.head + c) &
  773. (SERIAL_XMIT_SIZE-1));
  774. buf += c;
  775. count -= c;
  776. ret += c;
  777. }
  778. local_irq_restore(flags);
  779. if (info->xmit.head != info->xmit.tail
  780. && !tty->stopped
  781. && !tty->hw_stopped
  782. && !(info->IER & UART_IER_THRI)) {
  783. info->IER |= UART_IER_THRI;
  784. local_irq_disable();
  785. custom.intena = IF_SETCLR | IF_TBE;
  786. mb();
  787. /* set a pending Tx Interrupt, transmitter should restart now */
  788. custom.intreq = IF_SETCLR | IF_TBE;
  789. mb();
  790. local_irq_restore(flags);
  791. }
  792. return ret;
  793. }
  794. static int rs_write_room(struct tty_struct *tty)
  795. {
  796. struct async_struct *info = tty->driver_data;
  797. if (serial_paranoia_check(info, tty->name, "rs_write_room"))
  798. return 0;
  799. return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  800. }
  801. static int rs_chars_in_buffer(struct tty_struct *tty)
  802. {
  803. struct async_struct *info = tty->driver_data;
  804. if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
  805. return 0;
  806. return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  807. }
  808. static void rs_flush_buffer(struct tty_struct *tty)
  809. {
  810. struct async_struct *info = tty->driver_data;
  811. unsigned long flags;
  812. if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
  813. return;
  814. local_irq_save(flags);
  815. info->xmit.head = info->xmit.tail = 0;
  816. local_irq_restore(flags);
  817. tty_wakeup(tty);
  818. }
  819. /*
  820. * This function is used to send a high-priority XON/XOFF character to
  821. * the device
  822. */
  823. static void rs_send_xchar(struct tty_struct *tty, char ch)
  824. {
  825. struct async_struct *info = tty->driver_data;
  826. unsigned long flags;
  827. if (serial_paranoia_check(info, tty->name, "rs_send_char"))
  828. return;
  829. info->x_char = ch;
  830. if (ch) {
  831. /* Make sure transmit interrupts are on */
  832. /* Check this ! */
  833. local_irq_save(flags);
  834. if(!(custom.intenar & IF_TBE)) {
  835. custom.intena = IF_SETCLR | IF_TBE;
  836. mb();
  837. /* set a pending Tx Interrupt, transmitter should restart now */
  838. custom.intreq = IF_SETCLR | IF_TBE;
  839. mb();
  840. }
  841. local_irq_restore(flags);
  842. info->IER |= UART_IER_THRI;
  843. }
  844. }
  845. /*
  846. * ------------------------------------------------------------
  847. * rs_throttle()
  848. *
  849. * This routine is called by the upper-layer tty layer to signal that
  850. * incoming characters should be throttled.
  851. * ------------------------------------------------------------
  852. */
  853. static void rs_throttle(struct tty_struct * tty)
  854. {
  855. struct async_struct *info = tty->driver_data;
  856. unsigned long flags;
  857. #ifdef SERIAL_DEBUG_THROTTLE
  858. char buf[64];
  859. printk("throttle %s: %d....\n", tty_name(tty, buf),
  860. tty->ldisc.chars_in_buffer(tty));
  861. #endif
  862. if (serial_paranoia_check(info, tty->name, "rs_throttle"))
  863. return;
  864. if (I_IXOFF(tty))
  865. rs_send_xchar(tty, STOP_CHAR(tty));
  866. if (tty->termios->c_cflag & CRTSCTS)
  867. info->MCR &= ~SER_RTS;
  868. local_irq_save(flags);
  869. rtsdtr_ctrl(info->MCR);
  870. local_irq_restore(flags);
  871. }
  872. static void rs_unthrottle(struct tty_struct * tty)
  873. {
  874. struct async_struct *info = tty->driver_data;
  875. unsigned long flags;
  876. #ifdef SERIAL_DEBUG_THROTTLE
  877. char buf[64];
  878. printk("unthrottle %s: %d....\n", tty_name(tty, buf),
  879. tty->ldisc.chars_in_buffer(tty));
  880. #endif
  881. if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
  882. return;
  883. if (I_IXOFF(tty)) {
  884. if (info->x_char)
  885. info->x_char = 0;
  886. else
  887. rs_send_xchar(tty, START_CHAR(tty));
  888. }
  889. if (tty->termios->c_cflag & CRTSCTS)
  890. info->MCR |= SER_RTS;
  891. local_irq_save(flags);
  892. rtsdtr_ctrl(info->MCR);
  893. local_irq_restore(flags);
  894. }
  895. /*
  896. * ------------------------------------------------------------
  897. * rs_ioctl() and friends
  898. * ------------------------------------------------------------
  899. */
  900. static int get_serial_info(struct async_struct * info,
  901. struct serial_struct __user * retinfo)
  902. {
  903. struct serial_struct tmp;
  904. struct serial_state *state = info->state;
  905. if (!retinfo)
  906. return -EFAULT;
  907. memset(&tmp, 0, sizeof(tmp));
  908. tty_lock();
  909. tmp.type = state->type;
  910. tmp.line = state->line;
  911. tmp.port = state->port;
  912. tmp.irq = state->irq;
  913. tmp.flags = state->flags;
  914. tmp.xmit_fifo_size = state->xmit_fifo_size;
  915. tmp.baud_base = state->baud_base;
  916. tmp.close_delay = state->close_delay;
  917. tmp.closing_wait = state->closing_wait;
  918. tmp.custom_divisor = state->custom_divisor;
  919. tty_unlock();
  920. if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
  921. return -EFAULT;
  922. return 0;
  923. }
  924. static int set_serial_info(struct async_struct * info,
  925. struct serial_struct __user * new_info)
  926. {
  927. struct serial_struct new_serial;
  928. struct serial_state old_state, *state;
  929. unsigned int change_irq,change_port;
  930. int retval = 0;
  931. if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
  932. return -EFAULT;
  933. tty_lock();
  934. state = info->state;
  935. old_state = *state;
  936. change_irq = new_serial.irq != state->irq;
  937. change_port = (new_serial.port != state->port);
  938. if(change_irq || change_port || (new_serial.xmit_fifo_size != state->xmit_fifo_size)) {
  939. tty_unlock();
  940. return -EINVAL;
  941. }
  942. if (!serial_isroot()) {
  943. if ((new_serial.baud_base != state->baud_base) ||
  944. (new_serial.close_delay != state->close_delay) ||
  945. (new_serial.xmit_fifo_size != state->xmit_fifo_size) ||
  946. ((new_serial.flags & ~ASYNC_USR_MASK) !=
  947. (state->flags & ~ASYNC_USR_MASK)))
  948. return -EPERM;
  949. state->flags = ((state->flags & ~ASYNC_USR_MASK) |
  950. (new_serial.flags & ASYNC_USR_MASK));
  951. info->flags = ((info->flags & ~ASYNC_USR_MASK) |
  952. (new_serial.flags & ASYNC_USR_MASK));
  953. state->custom_divisor = new_serial.custom_divisor;
  954. goto check_and_exit;
  955. }
  956. if (new_serial.baud_base < 9600) {
  957. tty_unlock();
  958. return -EINVAL;
  959. }
  960. /*
  961. * OK, past this point, all the error checking has been done.
  962. * At this point, we start making changes.....
  963. */
  964. state->baud_base = new_serial.baud_base;
  965. state->flags = ((state->flags & ~ASYNC_FLAGS) |
  966. (new_serial.flags & ASYNC_FLAGS));
  967. info->flags = ((state->flags & ~ASYNC_INTERNAL_FLAGS) |
  968. (info->flags & ASYNC_INTERNAL_FLAGS));
  969. state->custom_divisor = new_serial.custom_divisor;
  970. state->close_delay = new_serial.close_delay * HZ/100;
  971. state->closing_wait = new_serial.closing_wait * HZ/100;
  972. info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  973. check_and_exit:
  974. if (info->flags & ASYNC_INITIALIZED) {
  975. if (((old_state.flags & ASYNC_SPD_MASK) !=
  976. (state->flags & ASYNC_SPD_MASK)) ||
  977. (old_state.custom_divisor != state->custom_divisor)) {
  978. if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  979. info->tty->alt_speed = 57600;
  980. if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  981. info->tty->alt_speed = 115200;
  982. if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
  983. info->tty->alt_speed = 230400;
  984. if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
  985. info->tty->alt_speed = 460800;
  986. change_speed(info, NULL);
  987. }
  988. } else
  989. retval = startup(info);
  990. tty_unlock();
  991. return retval;
  992. }
  993. /*
  994. * get_lsr_info - get line status register info
  995. *
  996. * Purpose: Let user call ioctl() to get info when the UART physically
  997. * is emptied. On bus types like RS485, the transmitter must
  998. * release the bus after transmitting. This must be done when
  999. * the transmit shift register is empty, not be done when the
  1000. * transmit holding register is empty. This functionality
  1001. * allows an RS485 driver to be written in user space.
  1002. */
  1003. static int get_lsr_info(struct async_struct * info, unsigned int __user *value)
  1004. {
  1005. unsigned char status;
  1006. unsigned int result;
  1007. unsigned long flags;
  1008. local_irq_save(flags);
  1009. status = custom.serdatr;
  1010. mb();
  1011. local_irq_restore(flags);
  1012. result = ((status & SDR_TSRE) ? TIOCSER_TEMT : 0);
  1013. if (copy_to_user(value, &result, sizeof(int)))
  1014. return -EFAULT;
  1015. return 0;
  1016. }
  1017. static int rs_tiocmget(struct tty_struct *tty)
  1018. {
  1019. struct async_struct * info = tty->driver_data;
  1020. unsigned char control, status;
  1021. unsigned long flags;
  1022. if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
  1023. return -ENODEV;
  1024. if (tty->flags & (1 << TTY_IO_ERROR))
  1025. return -EIO;
  1026. control = info->MCR;
  1027. local_irq_save(flags);
  1028. status = ciab.pra;
  1029. local_irq_restore(flags);
  1030. return ((control & SER_RTS) ? TIOCM_RTS : 0)
  1031. | ((control & SER_DTR) ? TIOCM_DTR : 0)
  1032. | (!(status & SER_DCD) ? TIOCM_CAR : 0)
  1033. | (!(status & SER_DSR) ? TIOCM_DSR : 0)
  1034. | (!(status & SER_CTS) ? TIOCM_CTS : 0);
  1035. }
  1036. static int rs_tiocmset(struct tty_struct *tty, unsigned int set,
  1037. unsigned int clear)
  1038. {
  1039. struct async_struct * info = tty->driver_data;
  1040. unsigned long flags;
  1041. if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
  1042. return -ENODEV;
  1043. if (tty->flags & (1 << TTY_IO_ERROR))
  1044. return -EIO;
  1045. local_irq_save(flags);
  1046. if (set & TIOCM_RTS)
  1047. info->MCR |= SER_RTS;
  1048. if (set & TIOCM_DTR)
  1049. info->MCR |= SER_DTR;
  1050. if (clear & TIOCM_RTS)
  1051. info->MCR &= ~SER_RTS;
  1052. if (clear & TIOCM_DTR)
  1053. info->MCR &= ~SER_DTR;
  1054. rtsdtr_ctrl(info->MCR);
  1055. local_irq_restore(flags);
  1056. return 0;
  1057. }
  1058. /*
  1059. * rs_break() --- routine which turns the break handling on or off
  1060. */
  1061. static int rs_break(struct tty_struct *tty, int break_state)
  1062. {
  1063. struct async_struct * info = tty->driver_data;
  1064. unsigned long flags;
  1065. if (serial_paranoia_check(info, tty->name, "rs_break"))
  1066. return -EINVAL;
  1067. local_irq_save(flags);
  1068. if (break_state == -1)
  1069. custom.adkcon = AC_SETCLR | AC_UARTBRK;
  1070. else
  1071. custom.adkcon = AC_UARTBRK;
  1072. mb();
  1073. local_irq_restore(flags);
  1074. return 0;
  1075. }
  1076. /*
  1077. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  1078. * Return: write counters to the user passed counter struct
  1079. * NB: both 1->0 and 0->1 transitions are counted except for
  1080. * RI where only 0->1 is counted.
  1081. */
  1082. static int rs_get_icount(struct tty_struct *tty,
  1083. struct serial_icounter_struct *icount)
  1084. {
  1085. struct async_struct *info = tty->driver_data;
  1086. struct async_icount cnow;
  1087. unsigned long flags;
  1088. local_irq_save(flags);
  1089. cnow = info->state->icount;
  1090. local_irq_restore(flags);
  1091. icount->cts = cnow.cts;
  1092. icount->dsr = cnow.dsr;
  1093. icount->rng = cnow.rng;
  1094. icount->dcd = cnow.dcd;
  1095. icount->rx = cnow.rx;
  1096. icount->tx = cnow.tx;
  1097. icount->frame = cnow.frame;
  1098. icount->overrun = cnow.overrun;
  1099. icount->parity = cnow.parity;
  1100. icount->brk = cnow.brk;
  1101. icount->buf_overrun = cnow.buf_overrun;
  1102. return 0;
  1103. }
  1104. static int rs_ioctl(struct tty_struct *tty,
  1105. unsigned int cmd, unsigned long arg)
  1106. {
  1107. struct async_struct * info = tty->driver_data;
  1108. struct async_icount cprev, cnow; /* kernel counter temps */
  1109. void __user *argp = (void __user *)arg;
  1110. unsigned long flags;
  1111. if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
  1112. return -ENODEV;
  1113. if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
  1114. (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
  1115. (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
  1116. if (tty->flags & (1 << TTY_IO_ERROR))
  1117. return -EIO;
  1118. }
  1119. switch (cmd) {
  1120. case TIOCGSERIAL:
  1121. return get_serial_info(info, argp);
  1122. case TIOCSSERIAL:
  1123. return set_serial_info(info, argp);
  1124. case TIOCSERCONFIG:
  1125. return 0;
  1126. case TIOCSERGETLSR: /* Get line status register */
  1127. return get_lsr_info(info, argp);
  1128. case TIOCSERGSTRUCT:
  1129. if (copy_to_user(argp,
  1130. info, sizeof(struct async_struct)))
  1131. return -EFAULT;
  1132. return 0;
  1133. /*
  1134. * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  1135. * - mask passed in arg for lines of interest
  1136. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  1137. * Caller should use TIOCGICOUNT to see which one it was
  1138. */
  1139. case TIOCMIWAIT:
  1140. local_irq_save(flags);
  1141. /* note the counters on entry */
  1142. cprev = info->state->icount;
  1143. local_irq_restore(flags);
  1144. while (1) {
  1145. interruptible_sleep_on(&info->delta_msr_wait);
  1146. /* see if a signal did it */
  1147. if (signal_pending(current))
  1148. return -ERESTARTSYS;
  1149. local_irq_save(flags);
  1150. cnow = info->state->icount; /* atomic copy */
  1151. local_irq_restore(flags);
  1152. if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
  1153. cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
  1154. return -EIO; /* no change => error */
  1155. if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  1156. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  1157. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
  1158. ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
  1159. return 0;
  1160. }
  1161. cprev = cnow;
  1162. }
  1163. /* NOTREACHED */
  1164. case TIOCSERGWILD:
  1165. case TIOCSERSWILD:
  1166. /* "setserial -W" is called in Debian boot */
  1167. printk ("TIOCSER?WILD ioctl obsolete, ignored.\n");
  1168. return 0;
  1169. default:
  1170. return -ENOIOCTLCMD;
  1171. }
  1172. return 0;
  1173. }
  1174. static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
  1175. {
  1176. struct async_struct *info = tty->driver_data;
  1177. unsigned long flags;
  1178. unsigned int cflag = tty->termios->c_cflag;
  1179. change_speed(info, old_termios);
  1180. /* Handle transition to B0 status */
  1181. if ((old_termios->c_cflag & CBAUD) &&
  1182. !(cflag & CBAUD)) {
  1183. info->MCR &= ~(SER_DTR|SER_RTS);
  1184. local_irq_save(flags);
  1185. rtsdtr_ctrl(info->MCR);
  1186. local_irq_restore(flags);
  1187. }
  1188. /* Handle transition away from B0 status */
  1189. if (!(old_termios->c_cflag & CBAUD) &&
  1190. (cflag & CBAUD)) {
  1191. info->MCR |= SER_DTR;
  1192. if (!(tty->termios->c_cflag & CRTSCTS) ||
  1193. !test_bit(TTY_THROTTLED, &tty->flags)) {
  1194. info->MCR |= SER_RTS;
  1195. }
  1196. local_irq_save(flags);
  1197. rtsdtr_ctrl(info->MCR);
  1198. local_irq_restore(flags);
  1199. }
  1200. /* Handle turning off CRTSCTS */
  1201. if ((old_termios->c_cflag & CRTSCTS) &&
  1202. !(tty->termios->c_cflag & CRTSCTS)) {
  1203. tty->hw_stopped = 0;
  1204. rs_start(tty);
  1205. }
  1206. #if 0
  1207. /*
  1208. * No need to wake up processes in open wait, since they
  1209. * sample the CLOCAL flag once, and don't recheck it.
  1210. * XXX It's not clear whether the current behavior is correct
  1211. * or not. Hence, this may change.....
  1212. */
  1213. if (!(old_termios->c_cflag & CLOCAL) &&
  1214. (tty->termios->c_cflag & CLOCAL))
  1215. wake_up_interruptible(&info->open_wait);
  1216. #endif
  1217. }
  1218. /*
  1219. * ------------------------------------------------------------
  1220. * rs_close()
  1221. *
  1222. * This routine is called when the serial port gets closed. First, we
  1223. * wait for the last remaining data to be sent. Then, we unlink its
  1224. * async structure from the interrupt chain if necessary, and we free
  1225. * that IRQ if nothing is left in the chain.
  1226. * ------------------------------------------------------------
  1227. */
  1228. static void rs_close(struct tty_struct *tty, struct file * filp)
  1229. {
  1230. struct async_struct * info = tty->driver_data;
  1231. struct serial_state *state;
  1232. unsigned long flags;
  1233. if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
  1234. return;
  1235. state = info->state;
  1236. local_irq_save(flags);
  1237. if (tty_hung_up_p(filp)) {
  1238. DBG_CNT("before DEC-hung");
  1239. local_irq_restore(flags);
  1240. return;
  1241. }
  1242. #ifdef SERIAL_DEBUG_OPEN
  1243. printk("rs_close ttys%d, count = %d\n", info->line, state->count);
  1244. #endif
  1245. if ((tty->count == 1) && (state->count != 1)) {
  1246. /*
  1247. * Uh, oh. tty->count is 1, which means that the tty
  1248. * structure will be freed. state->count should always
  1249. * be one in these conditions. If it's greater than
  1250. * one, we've got real problems, since it means the
  1251. * serial port won't be shutdown.
  1252. */
  1253. printk("rs_close: bad serial port count; tty->count is 1, "
  1254. "state->count is %d\n", state->count);
  1255. state->count = 1;
  1256. }
  1257. if (--state->count < 0) {
  1258. printk("rs_close: bad serial port count for ttys%d: %d\n",
  1259. info->line, state->count);
  1260. state->count = 0;
  1261. }
  1262. if (state->count) {
  1263. DBG_CNT("before DEC-2");
  1264. local_irq_restore(flags);
  1265. return;
  1266. }
  1267. info->flags |= ASYNC_CLOSING;
  1268. /*
  1269. * Now we wait for the transmit buffer to clear; and we notify
  1270. * the line discipline to only process XON/XOFF characters.
  1271. */
  1272. tty->closing = 1;
  1273. if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
  1274. tty_wait_until_sent(tty, info->closing_wait);
  1275. /*
  1276. * At this point we stop accepting input. To do this, we
  1277. * disable the receive line status interrupts, and tell the
  1278. * interrupt driver to stop checking the data ready bit in the
  1279. * line status register.
  1280. */
  1281. info->read_status_mask &= ~UART_LSR_DR;
  1282. if (info->flags & ASYNC_INITIALIZED) {
  1283. /* disable receive interrupts */
  1284. custom.intena = IF_RBF;
  1285. mb();
  1286. /* clear any pending receive interrupt */
  1287. custom.intreq = IF_RBF;
  1288. mb();
  1289. /*
  1290. * Before we drop DTR, make sure the UART transmitter
  1291. * has completely drained; this is especially
  1292. * important if there is a transmit FIFO!
  1293. */
  1294. rs_wait_until_sent(tty, info->timeout);
  1295. }
  1296. shutdown(info);
  1297. rs_flush_buffer(tty);
  1298. tty_ldisc_flush(tty);
  1299. tty->closing = 0;
  1300. info->event = 0;
  1301. info->tty = NULL;
  1302. if (info->blocked_open) {
  1303. if (info->close_delay) {
  1304. msleep_interruptible(jiffies_to_msecs(info->close_delay));
  1305. }
  1306. wake_up_interruptible(&info->open_wait);
  1307. }
  1308. info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
  1309. wake_up_interruptible(&info->close_wait);
  1310. local_irq_restore(flags);
  1311. }
  1312. /*
  1313. * rs_wait_until_sent() --- wait until the transmitter is empty
  1314. */
  1315. static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
  1316. {
  1317. struct async_struct * info = tty->driver_data;
  1318. unsigned long orig_jiffies, char_time;
  1319. int tty_was_locked = tty_locked();
  1320. int lsr;
  1321. if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
  1322. return;
  1323. if (info->xmit_fifo_size == 0)
  1324. return; /* Just in case.... */
  1325. orig_jiffies = jiffies;
  1326. /*
  1327. * tty_wait_until_sent is called from lots of places,
  1328. * with or without the BTM.
  1329. */
  1330. if (!tty_was_locked)
  1331. tty_lock();
  1332. /*
  1333. * Set the check interval to be 1/5 of the estimated time to
  1334. * send a single character, and make it at least 1. The check
  1335. * interval should also be less than the timeout.
  1336. *
  1337. * Note: we have to use pretty tight timings here to satisfy
  1338. * the NIST-PCTS.
  1339. */
  1340. char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
  1341. char_time = char_time / 5;
  1342. if (char_time == 0)
  1343. char_time = 1;
  1344. if (timeout)
  1345. char_time = min_t(unsigned long, char_time, timeout);
  1346. /*
  1347. * If the transmitter hasn't cleared in twice the approximate
  1348. * amount of time to send the entire FIFO, it probably won't
  1349. * ever clear. This assumes the UART isn't doing flow
  1350. * control, which is currently the case. Hence, if it ever
  1351. * takes longer than info->timeout, this is probably due to a
  1352. * UART bug of some kind. So, we clamp the timeout parameter at
  1353. * 2*info->timeout.
  1354. */
  1355. if (!timeout || timeout > 2*info->timeout)
  1356. timeout = 2*info->timeout;
  1357. #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  1358. printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
  1359. printk("jiff=%lu...", jiffies);
  1360. #endif
  1361. while(!((lsr = custom.serdatr) & SDR_TSRE)) {
  1362. #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  1363. printk("serdatr = %d (jiff=%lu)...", lsr, jiffies);
  1364. #endif
  1365. msleep_interruptible(jiffies_to_msecs(char_time));
  1366. if (signal_pending(current))
  1367. break;
  1368. if (timeout && time_after(jiffies, orig_jiffies + timeout))
  1369. break;
  1370. }
  1371. __set_current_state(TASK_RUNNING);
  1372. if (!tty_was_locked)
  1373. tty_unlock();
  1374. #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  1375. printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
  1376. #endif
  1377. }
  1378. /*
  1379. * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
  1380. */
  1381. static void rs_hangup(struct tty_struct *tty)
  1382. {
  1383. struct async_struct * info = tty->driver_data;
  1384. struct serial_state *state = info->state;
  1385. if (serial_paranoia_check(info, tty->name, "rs_hangup"))
  1386. return;
  1387. state = info->state;
  1388. rs_flush_buffer(tty);
  1389. shutdown(info);
  1390. info->event = 0;
  1391. state->count = 0;
  1392. info->flags &= ~ASYNC_NORMAL_ACTIVE;
  1393. info->tty = NULL;
  1394. wake_up_interruptible(&info->open_wait);
  1395. }
  1396. /*
  1397. * ------------------------------------------------------------
  1398. * rs_open() and friends
  1399. * ------------------------------------------------------------
  1400. */
  1401. static int block_til_ready(struct tty_struct *tty, struct file * filp,
  1402. struct async_struct *info)
  1403. {
  1404. #ifdef DECLARE_WAITQUEUE
  1405. DECLARE_WAITQUEUE(wait, current);
  1406. #else
  1407. struct wait_queue wait = { current, NULL };
  1408. #endif
  1409. struct serial_state *state = info->state;
  1410. int retval;
  1411. int do_clocal = 0, extra_count = 0;
  1412. unsigned long flags;
  1413. /*
  1414. * If the device is in the middle of being closed, then block
  1415. * until it's done, and then try again.
  1416. */
  1417. if (tty_hung_up_p(filp) ||
  1418. (info->flags & ASYNC_CLOSING)) {
  1419. if (info->flags & ASYNC_CLOSING)
  1420. interruptible_sleep_on(&info->close_wait);
  1421. #ifdef SERIAL_DO_RESTART
  1422. return ((info->flags & ASYNC_HUP_NOTIFY) ?
  1423. -EAGAIN : -ERESTARTSYS);
  1424. #else
  1425. return -EAGAIN;
  1426. #endif
  1427. }
  1428. /*
  1429. * If non-blocking mode is set, or the port is not enabled,
  1430. * then make the check up front and then exit.
  1431. */
  1432. if ((filp->f_flags & O_NONBLOCK) ||
  1433. (tty->flags & (1 << TTY_IO_ERROR))) {
  1434. info->flags |= ASYNC_NORMAL_ACTIVE;
  1435. return 0;
  1436. }
  1437. if (tty->termios->c_cflag & CLOCAL)
  1438. do_clocal = 1;
  1439. /*
  1440. * Block waiting for the carrier detect and the line to become
  1441. * free (i.e., not in use by the callout). While we are in
  1442. * this loop, state->count is dropped by one, so that
  1443. * rs_close() knows when to free things. We restore it upon
  1444. * exit, either normal or abnormal.
  1445. */
  1446. retval = 0;
  1447. add_wait_queue(&info->open_wait, &wait);
  1448. #ifdef SERIAL_DEBUG_OPEN
  1449. printk("block_til_ready before block: ttys%d, count = %d\n",
  1450. state->line, state->count);
  1451. #endif
  1452. local_irq_save(flags);
  1453. if (!tty_hung_up_p(filp)) {
  1454. extra_count = 1;
  1455. state->count--;
  1456. }
  1457. local_irq_restore(flags);
  1458. info->blocked_open++;
  1459. while (1) {
  1460. local_irq_save(flags);
  1461. if (tty->termios->c_cflag & CBAUD)
  1462. rtsdtr_ctrl(SER_DTR|SER_RTS);
  1463. local_irq_restore(flags);
  1464. set_current_state(TASK_INTERRUPTIBLE);
  1465. if (tty_hung_up_p(filp) ||
  1466. !(info->flags & ASYNC_INITIALIZED)) {
  1467. #ifdef SERIAL_DO_RESTART
  1468. if (info->flags & ASYNC_HUP_NOTIFY)
  1469. retval = -EAGAIN;
  1470. else
  1471. retval = -ERESTARTSYS;
  1472. #else
  1473. retval = -EAGAIN;
  1474. #endif
  1475. break;
  1476. }
  1477. if (!(info->flags & ASYNC_CLOSING) &&
  1478. (do_clocal || (!(ciab.pra & SER_DCD)) ))
  1479. break;
  1480. if (signal_pending(current)) {
  1481. retval = -ERESTARTSYS;
  1482. break;
  1483. }
  1484. #ifdef SERIAL_DEBUG_OPEN
  1485. printk("block_til_ready blocking: ttys%d, count = %d\n",
  1486. info->line, state->count);
  1487. #endif
  1488. tty_unlock();
  1489. schedule();
  1490. tty_lock();
  1491. }
  1492. __set_current_state(TASK_RUNNING);
  1493. remove_wait_queue(&info->open_wait, &wait);
  1494. if (extra_count)
  1495. state->count++;
  1496. info->blocked_open--;
  1497. #ifdef SERIAL_DEBUG_OPEN
  1498. printk("block_til_ready after blocking: ttys%d, count = %d\n",
  1499. info->line, state->count);
  1500. #endif
  1501. if (retval)
  1502. return retval;
  1503. info->flags |= ASYNC_NORMAL_ACTIVE;
  1504. return 0;
  1505. }
  1506. static int get_async_struct(int line, struct async_struct **ret_info)
  1507. {
  1508. struct async_struct *info;
  1509. struct serial_state *sstate;
  1510. sstate = rs_table + line;
  1511. sstate->count++;
  1512. if (sstate->info) {
  1513. *ret_info = sstate->info;
  1514. return 0;
  1515. }
  1516. info = kzalloc(sizeof(struct async_struct), GFP_KERNEL);
  1517. if (!info) {
  1518. sstate->count--;
  1519. return -ENOMEM;
  1520. }
  1521. #ifdef DECLARE_WAITQUEUE
  1522. init_waitqueue_head(&info->open_wait);
  1523. init_waitqueue_head(&info->close_wait);
  1524. init_waitqueue_head(&info->delta_msr_wait);
  1525. #endif
  1526. info->magic = SERIAL_MAGIC;
  1527. info->port = sstate->port;
  1528. info->flags = sstate->flags;
  1529. info->xmit_fifo_size = sstate->xmit_fifo_size;
  1530. info->line = line;
  1531. tasklet_init(&info->tlet, do_softint, (unsigned long)info);
  1532. info->state = sstate;
  1533. if (sstate->info) {
  1534. kfree(info);
  1535. *ret_info = sstate->info;
  1536. return 0;
  1537. }
  1538. *ret_info = sstate->info = info;
  1539. return 0;
  1540. }
  1541. /*
  1542. * This routine is called whenever a serial port is opened. It
  1543. * enables interrupts for a serial port, linking in its async structure into
  1544. * the IRQ chain. It also performs the serial-specific
  1545. * initialization for the tty structure.
  1546. */
  1547. static int rs_open(struct tty_struct *tty, struct file * filp)
  1548. {
  1549. struct async_struct *info;
  1550. int retval, line;
  1551. line = tty->index;
  1552. if ((line < 0) || (line >= NR_PORTS)) {
  1553. return -ENODEV;
  1554. }
  1555. retval = get_async_struct(line, &info);
  1556. if (retval) {
  1557. return retval;
  1558. }
  1559. tty->driver_data = info;
  1560. info->tty = tty;
  1561. if (serial_paranoia_check(info, tty->name, "rs_open"))
  1562. return -ENODEV;
  1563. #ifdef SERIAL_DEBUG_OPEN
  1564. printk("rs_open %s, count = %d\n", tty->name, info->state->count);
  1565. #endif
  1566. info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  1567. /*
  1568. * If the port is the middle of closing, bail out now
  1569. */
  1570. if (tty_hung_up_p(filp) ||
  1571. (info->flags & ASYNC_CLOSING)) {
  1572. if (info->flags & ASYNC_CLOSING)
  1573. interruptible_sleep_on(&info->close_wait);
  1574. #ifdef SERIAL_DO_RESTART
  1575. return ((info->flags & ASYNC_HUP_NOTIFY) ?
  1576. -EAGAIN : -ERESTARTSYS);
  1577. #else
  1578. return -EAGAIN;
  1579. #endif
  1580. }
  1581. /*
  1582. * Start up serial port
  1583. */
  1584. retval = startup(info);
  1585. if (retval) {
  1586. return retval;
  1587. }
  1588. retval = block_til_ready(tty, filp, info);
  1589. if (retval) {
  1590. #ifdef SERIAL_DEBUG_OPEN
  1591. printk("rs_open returning after block_til_ready with %d\n",
  1592. retval);
  1593. #endif
  1594. return retval;
  1595. }
  1596. #ifdef SERIAL_DEBUG_OPEN
  1597. printk("rs_open %s successful...", tty->name);
  1598. #endif
  1599. return 0;
  1600. }
  1601. /*
  1602. * /proc fs routines....
  1603. */
  1604. static inline void line_info(struct seq_file *m, struct serial_state *state)
  1605. {
  1606. struct async_struct *info = state->info, scr_info;
  1607. char stat_buf[30], control, status;
  1608. unsigned long flags;
  1609. seq_printf(m, "%d: uart:amiga_builtin",state->line);
  1610. /*
  1611. * Figure out the current RS-232 lines
  1612. */
  1613. if (!info) {
  1614. info = &scr_info; /* This is just for serial_{in,out} */
  1615. info->magic = SERIAL_MAGIC;
  1616. info->flags = state->flags;
  1617. info->quot = 0;
  1618. info->tty = NULL;
  1619. }
  1620. local_irq_save(flags);
  1621. status = ciab.pra;
  1622. control = info ? info->MCR : status;
  1623. local_irq_restore(flags);
  1624. stat_buf[0] = 0;
  1625. stat_buf[1] = 0;
  1626. if(!(control & SER_RTS))
  1627. strcat(stat_buf, "|RTS");
  1628. if(!(status & SER_CTS))
  1629. strcat(stat_buf, "|CTS");
  1630. if(!(control & SER_DTR))
  1631. strcat(stat_buf, "|DTR");
  1632. if(!(status & SER_DSR))
  1633. strcat(stat_buf, "|DSR");
  1634. if(!(status & SER_DCD))
  1635. strcat(stat_buf, "|CD");
  1636. if (info->quot) {
  1637. seq_printf(m, " baud:%d", state->baud_base / info->quot);
  1638. }
  1639. seq_printf(m, " tx:%d rx:%d", state->icount.tx, state->icount.rx);
  1640. if (state->icount.frame)
  1641. seq_printf(m, " fe:%d", state->icount.frame);
  1642. if (state->icount.parity)
  1643. seq_printf(m, " pe:%d", state->icount.parity);
  1644. if (state->icount.brk)
  1645. seq_printf(m, " brk:%d", state->icount.brk);
  1646. if (state->icount.overrun)
  1647. seq_printf(m, " oe:%d", state->icount.overrun);
  1648. /*
  1649. * Last thing is the RS-232 status lines
  1650. */
  1651. seq_printf(m, " %s\n", stat_buf+1);
  1652. }
  1653. static int rs_proc_show(struct seq_file *m, void *v)
  1654. {
  1655. seq_printf(m, "serinfo:1.0 driver:%s\n", serial_version);
  1656. line_info(m, &rs_table[0]);
  1657. return 0;
  1658. }
  1659. static int rs_proc_open(struct inode *inode, struct file *file)
  1660. {
  1661. return single_open(file, rs_proc_show, NULL);
  1662. }
  1663. static const struct file_operations rs_proc_fops = {
  1664. .owner = THIS_MODULE,
  1665. .open = rs_proc_open,
  1666. .read = seq_read,
  1667. .llseek = seq_lseek,
  1668. .release = single_release,
  1669. };
  1670. /*
  1671. * ---------------------------------------------------------------------
  1672. * rs_init() and friends
  1673. *
  1674. * rs_init() is called at boot-time to initialize the serial driver.
  1675. * ---------------------------------------------------------------------
  1676. */
  1677. /*
  1678. * This routine prints out the appropriate serial driver version
  1679. * number, and identifies which options were configured into this
  1680. * driver.
  1681. */
  1682. static void show_serial_version(void)
  1683. {
  1684. printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
  1685. }
  1686. static const struct tty_operations serial_ops = {
  1687. .open = rs_open,
  1688. .close = rs_close,
  1689. .write = rs_write,
  1690. .put_char = rs_put_char,
  1691. .flush_chars = rs_flush_chars,
  1692. .write_room = rs_write_room,
  1693. .chars_in_buffer = rs_chars_in_buffer,
  1694. .flush_buffer = rs_flush_buffer,
  1695. .ioctl = rs_ioctl,
  1696. .throttle = rs_throttle,
  1697. .unthrottle = rs_unthrottle,
  1698. .set_termios = rs_set_termios,
  1699. .stop = rs_stop,
  1700. .start = rs_start,
  1701. .hangup = rs_hangup,
  1702. .break_ctl = rs_break,
  1703. .send_xchar = rs_send_xchar,
  1704. .wait_until_sent = rs_wait_until_sent,
  1705. .tiocmget = rs_tiocmget,
  1706. .tiocmset = rs_tiocmset,
  1707. .get_icount = rs_get_icount,
  1708. .proc_fops = &rs_proc_fops,
  1709. };
  1710. /*
  1711. * The serial driver boot-time initialization code!
  1712. */
  1713. static int __init amiga_serial_probe(struct platform_device *pdev)
  1714. {
  1715. unsigned long flags;
  1716. struct serial_state * state;
  1717. int error;
  1718. serial_driver = alloc_tty_driver(1);
  1719. if (!serial_driver)
  1720. return -ENOMEM;
  1721. IRQ_ports = NULL;
  1722. show_serial_version();
  1723. /* Initialize the tty_driver structure */
  1724. serial_driver->owner = THIS_MODULE;
  1725. serial_driver->driver_name = "amiserial";
  1726. serial_driver->name = "ttyS";
  1727. serial_driver->major = TTY_MAJOR;
  1728. serial_driver->minor_start = 64;
  1729. serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
  1730. serial_driver->subtype = SERIAL_TYPE_NORMAL;
  1731. serial_driver->init_termios = tty_std_termios;
  1732. serial_driver->init_termios.c_cflag =
  1733. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  1734. serial_driver->flags = TTY_DRIVER_REAL_RAW;
  1735. tty_set_operations(serial_driver, &serial_ops);
  1736. error = tty_register_driver(serial_driver);
  1737. if (error)
  1738. goto fail_put_tty_driver;
  1739. state = rs_table;
  1740. state->magic = SSTATE_MAGIC;
  1741. state->port = (int)&custom.serdatr; /* Just to give it a value */
  1742. state->line = 0;
  1743. state->custom_divisor = 0;
  1744. state->close_delay = 5*HZ/10;
  1745. state->closing_wait = 30*HZ;
  1746. state->icount.cts = state->icount.dsr =
  1747. state->icount.rng = state->icount.dcd = 0;
  1748. state->icount.rx = state->icount.tx = 0;
  1749. state->icount.frame = state->icount.parity = 0;
  1750. state->icount.overrun = state->icount.brk = 0;
  1751. printk(KERN_INFO "ttyS%d is the amiga builtin serial port\n",
  1752. state->line);
  1753. /* Hardware set up */
  1754. state->baud_base = amiga_colorclock;
  1755. state->xmit_fifo_size = 1;
  1756. /* set ISRs, and then disable the rx interrupts */
  1757. error = request_irq(IRQ_AMIGA_TBE, ser_tx_int, 0, "serial TX", state);
  1758. if (error)
  1759. goto fail_unregister;
  1760. error = request_irq(IRQ_AMIGA_RBF, ser_rx_int, IRQF_DISABLED,
  1761. "serial RX", state);
  1762. if (error)
  1763. goto fail_free_irq;
  1764. local_irq_save(flags);
  1765. /* turn off Rx and Tx interrupts */
  1766. custom.intena = IF_RBF | IF_TBE;
  1767. mb();
  1768. /* clear any pending interrupt */
  1769. custom.intreq = IF_RBF | IF_TBE;
  1770. mb();
  1771. local_irq_restore(flags);
  1772. /*
  1773. * set the appropriate directions for the modem control flags,
  1774. * and clear RTS and DTR
  1775. */
  1776. ciab.ddra |= (SER_DTR | SER_RTS); /* outputs */
  1777. ciab.ddra &= ~(SER_DCD | SER_CTS | SER_DSR); /* inputs */
  1778. platform_set_drvdata(pdev, state);
  1779. return 0;
  1780. fail_free_irq:
  1781. free_irq(IRQ_AMIGA_TBE, state);
  1782. fail_unregister:
  1783. tty_unregister_driver(serial_driver);
  1784. fail_put_tty_driver:
  1785. put_tty_driver(serial_driver);
  1786. return error;
  1787. }
  1788. static int __exit amiga_serial_remove(struct platform_device *pdev)
  1789. {
  1790. int error;
  1791. struct serial_state *state = platform_get_drvdata(pdev);
  1792. struct async_struct *info = state->info;
  1793. /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
  1794. tasklet_kill(&info->tlet);
  1795. if ((error = tty_unregister_driver(serial_driver)))
  1796. printk("SERIAL: failed to unregister serial driver (%d)\n",
  1797. error);
  1798. put_tty_driver(serial_driver);
  1799. rs_table[0].info = NULL;
  1800. kfree(info);
  1801. free_irq(IRQ_AMIGA_TBE, rs_table);
  1802. free_irq(IRQ_AMIGA_RBF, rs_table);
  1803. platform_set_drvdata(pdev, NULL);
  1804. return error;
  1805. }
  1806. static struct platform_driver amiga_serial_driver = {
  1807. .remove = __exit_p(amiga_serial_remove),
  1808. .driver = {
  1809. .name = "amiga-serial",
  1810. .owner = THIS_MODULE,
  1811. },
  1812. };
  1813. static int __init amiga_serial_init(void)
  1814. {
  1815. return platform_driver_probe(&amiga_serial_driver, amiga_serial_probe);
  1816. }
  1817. module_init(amiga_serial_init);
  1818. static void __exit amiga_serial_exit(void)
  1819. {
  1820. platform_driver_unregister(&amiga_serial_driver);
  1821. }
  1822. module_exit(amiga_serial_exit);
  1823. #if defined(CONFIG_SERIAL_CONSOLE) && !defined(MODULE)
  1824. /*
  1825. * ------------------------------------------------------------
  1826. * Serial console driver
  1827. * ------------------------------------------------------------
  1828. */
  1829. static void amiga_serial_putc(char c)
  1830. {
  1831. custom.serdat = (unsigned char)c | 0x100;
  1832. while (!(custom.serdatr & 0x2000))
  1833. barrier();
  1834. }
  1835. /*
  1836. * Print a string to the serial port trying not to disturb
  1837. * any possible real use of the port...
  1838. *
  1839. * The console must be locked when we get here.
  1840. */
  1841. static void serial_console_write(struct console *co, const char *s,
  1842. unsigned count)
  1843. {
  1844. unsigned short intena = custom.intenar;
  1845. custom.intena = IF_TBE;
  1846. while (count--) {
  1847. if (*s == '\n')
  1848. amiga_serial_putc('\r');
  1849. amiga_serial_putc(*s++);
  1850. }
  1851. custom.intena = IF_SETCLR | (intena & IF_TBE);
  1852. }
  1853. static struct tty_driver *serial_console_device(struct console *c, int *index)
  1854. {
  1855. *index = 0;
  1856. return serial_driver;
  1857. }
  1858. static struct console sercons = {
  1859. .name = "ttyS",
  1860. .write = serial_console_write,
  1861. .device = serial_console_device,
  1862. .flags = CON_PRINTBUFFER,
  1863. .index = -1,
  1864. };
  1865. /*
  1866. * Register console.
  1867. */
  1868. static int __init amiserial_console_init(void)
  1869. {
  1870. register_console(&sercons);
  1871. return 0;
  1872. }
  1873. console_initcall(amiserial_console_init);
  1874. #endif /* CONFIG_SERIAL_CONSOLE && !MODULE */
  1875. MODULE_LICENSE("GPL");
  1876. MODULE_ALIAS("platform:amiga-serial");