/arch/ia64/hp/sim/simserial.c

https://bitbucket.org/evzijst/gittest · C · 1032 lines · 689 code · 150 blank · 193 comment · 137 complexity · 55e9cbad67c75557c82b5a8a45d2de19 MD5 · raw file

  1. /*
  2. * Simulated Serial Driver (fake serial)
  3. *
  4. * This driver is mostly used for bringup purposes and will go away.
  5. * It has a strong dependency on the system console. All outputs
  6. * are rerouted to the same facility as the one used by printk which, in our
  7. * case means sys_sim.c console (goes via the simulator). The code hereafter
  8. * is completely leveraged from the serial.c driver.
  9. *
  10. * Copyright (C) 1999-2000, 2002-2003 Hewlett-Packard Co
  11. * Stephane Eranian <eranian@hpl.hp.com>
  12. * David Mosberger-Tang <davidm@hpl.hp.com>
  13. *
  14. * 02/04/00 D. Mosberger Merged in serial.c bug fixes in rs_close().
  15. * 02/25/00 D. Mosberger Synced up with 2.3.99pre-5 version of serial.c.
  16. * 07/30/02 D. Mosberger Replace sti()/cli() with explicit spinlocks & local irq masking
  17. */
  18. #include <linux/config.h>
  19. #include <linux/init.h>
  20. #include <linux/errno.h>
  21. #include <linux/sched.h>
  22. #include <linux/tty.h>
  23. #include <linux/tty_flip.h>
  24. #include <linux/major.h>
  25. #include <linux/fcntl.h>
  26. #include <linux/mm.h>
  27. #include <linux/slab.h>
  28. #include <linux/console.h>
  29. #include <linux/module.h>
  30. #include <linux/serial.h>
  31. #include <linux/serialP.h>
  32. #include <asm/irq.h>
  33. #include <asm/hw_irq.h>
  34. #include <asm/uaccess.h>
  35. #ifdef CONFIG_KDB
  36. # include <linux/kdb.h>
  37. #endif
  38. #undef SIMSERIAL_DEBUG /* define this to get some debug information */
  39. #define KEYBOARD_INTR 3 /* must match with simulator! */
  40. #define NR_PORTS 1 /* only one port for now */
  41. #define SERIAL_INLINE 1
  42. #ifdef SERIAL_INLINE
  43. #define _INLINE_ inline
  44. #endif
  45. #define IRQ_T(info) ((info->flags & ASYNC_SHARE_IRQ) ? SA_SHIRQ : SA_INTERRUPT)
  46. #define SSC_GETCHAR 21
  47. extern long ia64_ssc (long, long, long, long, int);
  48. extern void ia64_ssc_connect_irq (long intr, long irq);
  49. static char *serial_name = "SimSerial driver";
  50. static char *serial_version = "0.6";
  51. /*
  52. * This has been extracted from asm/serial.h. We need one eventually but
  53. * I don't know exactly what we're going to put in it so just fake one
  54. * for now.
  55. */
  56. #define BASE_BAUD ( 1843200 / 16 )
  57. #define STD_COM_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_SKIP_TEST)
  58. /*
  59. * Most of the values here are meaningless to this particular driver.
  60. * However some values must be preserved for the code (leveraged from serial.c
  61. * to work correctly).
  62. * port must not be 0
  63. * type must not be UNKNOWN
  64. * So I picked arbitrary (guess from where?) values instead
  65. */
  66. static struct serial_state rs_table[NR_PORTS]={
  67. /* UART CLK PORT IRQ FLAGS */
  68. { 0, BASE_BAUD, 0x3F8, 0, STD_COM_FLAGS,0,PORT_16550 } /* ttyS0 */
  69. };
  70. /*
  71. * Just for the fun of it !
  72. */
  73. static struct serial_uart_config uart_config[] = {
  74. { "unknown", 1, 0 },
  75. { "8250", 1, 0 },
  76. { "16450", 1, 0 },
  77. { "16550", 1, 0 },
  78. { "16550A", 16, UART_CLEAR_FIFO | UART_USE_FIFO },
  79. { "cirrus", 1, 0 },
  80. { "ST16650", 1, UART_CLEAR_FIFO | UART_STARTECH },
  81. { "ST16650V2", 32, UART_CLEAR_FIFO | UART_USE_FIFO |
  82. UART_STARTECH },
  83. { "TI16750", 64, UART_CLEAR_FIFO | UART_USE_FIFO},
  84. { 0, 0}
  85. };
  86. struct tty_driver *hp_simserial_driver;
  87. static struct async_struct *IRQ_ports[NR_IRQS];
  88. static struct console *console;
  89. static unsigned char *tmp_buf;
  90. static DECLARE_MUTEX(tmp_buf_sem);
  91. extern struct console *console_drivers; /* from kernel/printk.c */
  92. /*
  93. * ------------------------------------------------------------
  94. * rs_stop() and rs_start()
  95. *
  96. * This routines are called before setting or resetting tty->stopped.
  97. * They enable or disable transmitter interrupts, as necessary.
  98. * ------------------------------------------------------------
  99. */
  100. static void rs_stop(struct tty_struct *tty)
  101. {
  102. #ifdef SIMSERIAL_DEBUG
  103. printk("rs_stop: tty->stopped=%d tty->hw_stopped=%d tty->flow_stopped=%d\n",
  104. tty->stopped, tty->hw_stopped, tty->flow_stopped);
  105. #endif
  106. }
  107. static void rs_start(struct tty_struct *tty)
  108. {
  109. #if SIMSERIAL_DEBUG
  110. printk("rs_start: tty->stopped=%d tty->hw_stopped=%d tty->flow_stopped=%d\n",
  111. tty->stopped, tty->hw_stopped, tty->flow_stopped);
  112. #endif
  113. }
  114. static void receive_chars(struct tty_struct *tty, struct pt_regs *regs)
  115. {
  116. unsigned char ch;
  117. static unsigned char seen_esc = 0;
  118. while ( (ch = ia64_ssc(0, 0, 0, 0, SSC_GETCHAR)) ) {
  119. if ( ch == 27 && seen_esc == 0 ) {
  120. seen_esc = 1;
  121. continue;
  122. } else {
  123. if ( seen_esc==1 && ch == 'O' ) {
  124. seen_esc = 2;
  125. continue;
  126. } else if ( seen_esc == 2 ) {
  127. if ( ch == 'P' ) show_state(); /* F1 key */
  128. #ifdef CONFIG_KDB
  129. if ( ch == 'S' )
  130. kdb(KDB_REASON_KEYBOARD, 0, (kdb_eframe_t) regs);
  131. #endif
  132. seen_esc = 0;
  133. continue;
  134. }
  135. }
  136. seen_esc = 0;
  137. if (tty->flip.count >= TTY_FLIPBUF_SIZE) break;
  138. *tty->flip.char_buf_ptr = ch;
  139. *tty->flip.flag_buf_ptr = 0;
  140. tty->flip.flag_buf_ptr++;
  141. tty->flip.char_buf_ptr++;
  142. tty->flip.count++;
  143. }
  144. tty_flip_buffer_push(tty);
  145. }
  146. /*
  147. * This is the serial driver's interrupt routine for a single port
  148. */
  149. static irqreturn_t rs_interrupt_single(int irq, void *dev_id, struct pt_regs * regs)
  150. {
  151. struct async_struct * info;
  152. /*
  153. * I don't know exactly why they don't use the dev_id opaque data
  154. * pointer instead of this extra lookup table
  155. */
  156. info = IRQ_ports[irq];
  157. if (!info || !info->tty) {
  158. printk(KERN_INFO "simrs_interrupt_single: info|tty=0 info=%p problem\n", info);
  159. return IRQ_NONE;
  160. }
  161. /*
  162. * pretty simple in our case, because we only get interrupts
  163. * on inbound traffic
  164. */
  165. receive_chars(info->tty, regs);
  166. return IRQ_HANDLED;
  167. }
  168. /*
  169. * -------------------------------------------------------------------
  170. * Here ends the serial interrupt routines.
  171. * -------------------------------------------------------------------
  172. */
  173. #if 0
  174. /*
  175. * not really used in our situation so keep them commented out for now
  176. */
  177. static DECLARE_TASK_QUEUE(tq_serial); /* used to be at the top of the file */
  178. static void do_serial_bh(void)
  179. {
  180. run_task_queue(&tq_serial);
  181. printk(KERN_ERR "do_serial_bh: called\n");
  182. }
  183. #endif
  184. static void do_softint(void *private_)
  185. {
  186. printk(KERN_ERR "simserial: do_softint called\n");
  187. }
  188. static void rs_put_char(struct tty_struct *tty, unsigned char ch)
  189. {
  190. struct async_struct *info = (struct async_struct *)tty->driver_data;
  191. unsigned long flags;
  192. if (!tty || !info->xmit.buf) return;
  193. local_irq_save(flags);
  194. if (CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) == 0) {
  195. local_irq_restore(flags);
  196. return;
  197. }
  198. info->xmit.buf[info->xmit.head] = ch;
  199. info->xmit.head = (info->xmit.head + 1) & (SERIAL_XMIT_SIZE-1);
  200. local_irq_restore(flags);
  201. }
  202. static _INLINE_ void transmit_chars(struct async_struct *info, int *intr_done)
  203. {
  204. int count;
  205. unsigned long flags;
  206. local_irq_save(flags);
  207. if (info->x_char) {
  208. char c = info->x_char;
  209. console->write(console, &c, 1);
  210. info->state->icount.tx++;
  211. info->x_char = 0;
  212. goto out;
  213. }
  214. if (info->xmit.head == info->xmit.tail || info->tty->stopped || info->tty->hw_stopped) {
  215. #ifdef SIMSERIAL_DEBUG
  216. printk("transmit_chars: head=%d, tail=%d, stopped=%d\n",
  217. info->xmit.head, info->xmit.tail, info->tty->stopped);
  218. #endif
  219. goto out;
  220. }
  221. /*
  222. * We removed the loop and try to do it in to chunks. We need
  223. * 2 operations maximum because it's a ring buffer.
  224. *
  225. * First from current to tail if possible.
  226. * Then from the beginning of the buffer until necessary
  227. */
  228. count = min(CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE),
  229. SERIAL_XMIT_SIZE - info->xmit.tail);
  230. console->write(console, info->xmit.buf+info->xmit.tail, count);
  231. info->xmit.tail = (info->xmit.tail+count) & (SERIAL_XMIT_SIZE-1);
  232. /*
  233. * We have more at the beginning of the buffer
  234. */
  235. count = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  236. if (count) {
  237. console->write(console, info->xmit.buf, count);
  238. info->xmit.tail += count;
  239. }
  240. out:
  241. local_irq_restore(flags);
  242. }
  243. static void rs_flush_chars(struct tty_struct *tty)
  244. {
  245. struct async_struct *info = (struct async_struct *)tty->driver_data;
  246. if (info->xmit.head == info->xmit.tail || tty->stopped || tty->hw_stopped ||
  247. !info->xmit.buf)
  248. return;
  249. transmit_chars(info, NULL);
  250. }
  251. static int rs_write(struct tty_struct * tty,
  252. const unsigned char *buf, int count)
  253. {
  254. int c, ret = 0;
  255. struct async_struct *info = (struct async_struct *)tty->driver_data;
  256. unsigned long flags;
  257. if (!tty || !info->xmit.buf || !tmp_buf) return 0;
  258. local_irq_save(flags);
  259. while (1) {
  260. c = CIRC_SPACE_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  261. if (count < c)
  262. c = count;
  263. if (c <= 0) {
  264. break;
  265. }
  266. memcpy(info->xmit.buf + info->xmit.head, buf, c);
  267. info->xmit.head = ((info->xmit.head + c) &
  268. (SERIAL_XMIT_SIZE-1));
  269. buf += c;
  270. count -= c;
  271. ret += c;
  272. }
  273. local_irq_restore(flags);
  274. /*
  275. * Hey, we transmit directly from here in our case
  276. */
  277. if (CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE)
  278. && !tty->stopped && !tty->hw_stopped) {
  279. transmit_chars(info, NULL);
  280. }
  281. return ret;
  282. }
  283. static int rs_write_room(struct tty_struct *tty)
  284. {
  285. struct async_struct *info = (struct async_struct *)tty->driver_data;
  286. return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  287. }
  288. static int rs_chars_in_buffer(struct tty_struct *tty)
  289. {
  290. struct async_struct *info = (struct async_struct *)tty->driver_data;
  291. return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  292. }
  293. static void rs_flush_buffer(struct tty_struct *tty)
  294. {
  295. struct async_struct *info = (struct async_struct *)tty->driver_data;
  296. unsigned long flags;
  297. local_irq_save(flags);
  298. info->xmit.head = info->xmit.tail = 0;
  299. local_irq_restore(flags);
  300. wake_up_interruptible(&tty->write_wait);
  301. if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
  302. tty->ldisc.write_wakeup)
  303. (tty->ldisc.write_wakeup)(tty);
  304. }
  305. /*
  306. * This function is used to send a high-priority XON/XOFF character to
  307. * the device
  308. */
  309. static void rs_send_xchar(struct tty_struct *tty, char ch)
  310. {
  311. struct async_struct *info = (struct async_struct *)tty->driver_data;
  312. info->x_char = ch;
  313. if (ch) {
  314. /*
  315. * I guess we could call console->write() directly but
  316. * let's do that for now.
  317. */
  318. transmit_chars(info, NULL);
  319. }
  320. }
  321. /*
  322. * ------------------------------------------------------------
  323. * rs_throttle()
  324. *
  325. * This routine is called by the upper-layer tty layer to signal that
  326. * incoming characters should be throttled.
  327. * ------------------------------------------------------------
  328. */
  329. static void rs_throttle(struct tty_struct * tty)
  330. {
  331. if (I_IXOFF(tty)) rs_send_xchar(tty, STOP_CHAR(tty));
  332. printk(KERN_INFO "simrs_throttle called\n");
  333. }
  334. static void rs_unthrottle(struct tty_struct * tty)
  335. {
  336. struct async_struct *info = (struct async_struct *)tty->driver_data;
  337. if (I_IXOFF(tty)) {
  338. if (info->x_char)
  339. info->x_char = 0;
  340. else
  341. rs_send_xchar(tty, START_CHAR(tty));
  342. }
  343. printk(KERN_INFO "simrs_unthrottle called\n");
  344. }
  345. /*
  346. * rs_break() --- routine which turns the break handling on or off
  347. */
  348. static void rs_break(struct tty_struct *tty, int break_state)
  349. {
  350. }
  351. static int rs_ioctl(struct tty_struct *tty, struct file * file,
  352. unsigned int cmd, unsigned long arg)
  353. {
  354. if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
  355. (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
  356. (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
  357. if (tty->flags & (1 << TTY_IO_ERROR))
  358. return -EIO;
  359. }
  360. switch (cmd) {
  361. case TIOCMGET:
  362. printk(KERN_INFO "rs_ioctl: TIOCMGET called\n");
  363. return -EINVAL;
  364. case TIOCMBIS:
  365. case TIOCMBIC:
  366. case TIOCMSET:
  367. printk(KERN_INFO "rs_ioctl: TIOCMBIS/BIC/SET called\n");
  368. return -EINVAL;
  369. case TIOCGSERIAL:
  370. printk(KERN_INFO "simrs_ioctl TIOCGSERIAL called\n");
  371. return 0;
  372. case TIOCSSERIAL:
  373. printk(KERN_INFO "simrs_ioctl TIOCSSERIAL called\n");
  374. return 0;
  375. case TIOCSERCONFIG:
  376. printk(KERN_INFO "rs_ioctl: TIOCSERCONFIG called\n");
  377. return -EINVAL;
  378. case TIOCSERGETLSR: /* Get line status register */
  379. printk(KERN_INFO "rs_ioctl: TIOCSERGETLSR called\n");
  380. return -EINVAL;
  381. case TIOCSERGSTRUCT:
  382. printk(KERN_INFO "rs_ioctl: TIOCSERGSTRUCT called\n");
  383. #if 0
  384. if (copy_to_user((struct async_struct *) arg,
  385. info, sizeof(struct async_struct)))
  386. return -EFAULT;
  387. #endif
  388. return 0;
  389. /*
  390. * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  391. * - mask passed in arg for lines of interest
  392. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  393. * Caller should use TIOCGICOUNT to see which one it was
  394. */
  395. case TIOCMIWAIT:
  396. printk(KERN_INFO "rs_ioctl: TIOCMIWAIT: called\n");
  397. return 0;
  398. /*
  399. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  400. * Return: write counters to the user passed counter struct
  401. * NB: both 1->0 and 0->1 transitions are counted except for
  402. * RI where only 0->1 is counted.
  403. */
  404. case TIOCGICOUNT:
  405. printk(KERN_INFO "rs_ioctl: TIOCGICOUNT called\n");
  406. return 0;
  407. case TIOCSERGWILD:
  408. case TIOCSERSWILD:
  409. /* "setserial -W" is called in Debian boot */
  410. printk (KERN_INFO "TIOCSER?WILD ioctl obsolete, ignored.\n");
  411. return 0;
  412. default:
  413. return -ENOIOCTLCMD;
  414. }
  415. return 0;
  416. }
  417. #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
  418. static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
  419. {
  420. unsigned int cflag = tty->termios->c_cflag;
  421. if ( (cflag == old_termios->c_cflag)
  422. && ( RELEVANT_IFLAG(tty->termios->c_iflag)
  423. == RELEVANT_IFLAG(old_termios->c_iflag)))
  424. return;
  425. /* Handle turning off CRTSCTS */
  426. if ((old_termios->c_cflag & CRTSCTS) &&
  427. !(tty->termios->c_cflag & CRTSCTS)) {
  428. tty->hw_stopped = 0;
  429. rs_start(tty);
  430. }
  431. }
  432. /*
  433. * This routine will shutdown a serial port; interrupts are disabled, and
  434. * DTR is dropped if the hangup on close termio flag is on.
  435. */
  436. static void shutdown(struct async_struct * info)
  437. {
  438. unsigned long flags;
  439. struct serial_state *state;
  440. int retval;
  441. if (!(info->flags & ASYNC_INITIALIZED)) return;
  442. state = info->state;
  443. #ifdef SIMSERIAL_DEBUG
  444. printk("Shutting down serial port %d (irq %d)....", info->line,
  445. state->irq);
  446. #endif
  447. local_irq_save(flags);
  448. {
  449. /*
  450. * First unlink the serial port from the IRQ chain...
  451. */
  452. if (info->next_port)
  453. info->next_port->prev_port = info->prev_port;
  454. if (info->prev_port)
  455. info->prev_port->next_port = info->next_port;
  456. else
  457. IRQ_ports[state->irq] = info->next_port;
  458. /*
  459. * Free the IRQ, if necessary
  460. */
  461. if (state->irq && (!IRQ_ports[state->irq] ||
  462. !IRQ_ports[state->irq]->next_port)) {
  463. if (IRQ_ports[state->irq]) {
  464. free_irq(state->irq, NULL);
  465. retval = request_irq(state->irq, rs_interrupt_single,
  466. IRQ_T(info), "serial", NULL);
  467. if (retval)
  468. printk(KERN_ERR "serial shutdown: request_irq: error %d"
  469. " Couldn't reacquire IRQ.\n", retval);
  470. } else
  471. free_irq(state->irq, NULL);
  472. }
  473. if (info->xmit.buf) {
  474. free_page((unsigned long) info->xmit.buf);
  475. info->xmit.buf = 0;
  476. }
  477. if (info->tty) set_bit(TTY_IO_ERROR, &info->tty->flags);
  478. info->flags &= ~ASYNC_INITIALIZED;
  479. }
  480. local_irq_restore(flags);
  481. }
  482. /*
  483. * ------------------------------------------------------------
  484. * rs_close()
  485. *
  486. * This routine is called when the serial port gets closed. First, we
  487. * wait for the last remaining data to be sent. Then, we unlink its
  488. * async structure from the interrupt chain if necessary, and we free
  489. * that IRQ if nothing is left in the chain.
  490. * ------------------------------------------------------------
  491. */
  492. static void rs_close(struct tty_struct *tty, struct file * filp)
  493. {
  494. struct async_struct * info = (struct async_struct *)tty->driver_data;
  495. struct serial_state *state;
  496. unsigned long flags;
  497. if (!info ) return;
  498. state = info->state;
  499. local_irq_save(flags);
  500. if (tty_hung_up_p(filp)) {
  501. #ifdef SIMSERIAL_DEBUG
  502. printk("rs_close: hung_up\n");
  503. #endif
  504. local_irq_restore(flags);
  505. return;
  506. }
  507. #ifdef SIMSERIAL_DEBUG
  508. printk("rs_close ttys%d, count = %d\n", info->line, state->count);
  509. #endif
  510. if ((tty->count == 1) && (state->count != 1)) {
  511. /*
  512. * Uh, oh. tty->count is 1, which means that the tty
  513. * structure will be freed. state->count should always
  514. * be one in these conditions. If it's greater than
  515. * one, we've got real problems, since it means the
  516. * serial port won't be shutdown.
  517. */
  518. printk(KERN_ERR "rs_close: bad serial port count; tty->count is 1, "
  519. "state->count is %d\n", state->count);
  520. state->count = 1;
  521. }
  522. if (--state->count < 0) {
  523. printk(KERN_ERR "rs_close: bad serial port count for ttys%d: %d\n",
  524. info->line, state->count);
  525. state->count = 0;
  526. }
  527. if (state->count) {
  528. local_irq_restore(flags);
  529. return;
  530. }
  531. info->flags |= ASYNC_CLOSING;
  532. local_irq_restore(flags);
  533. /*
  534. * Now we wait for the transmit buffer to clear; and we notify
  535. * the line discipline to only process XON/XOFF characters.
  536. */
  537. shutdown(info);
  538. if (tty->driver->flush_buffer) tty->driver->flush_buffer(tty);
  539. if (tty->ldisc.flush_buffer) tty->ldisc.flush_buffer(tty);
  540. info->event = 0;
  541. info->tty = 0;
  542. if (info->blocked_open) {
  543. if (info->close_delay) {
  544. current->state = TASK_INTERRUPTIBLE;
  545. schedule_timeout(info->close_delay);
  546. }
  547. wake_up_interruptible(&info->open_wait);
  548. }
  549. info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
  550. wake_up_interruptible(&info->close_wait);
  551. }
  552. /*
  553. * rs_wait_until_sent() --- wait until the transmitter is empty
  554. */
  555. static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
  556. {
  557. }
  558. /*
  559. * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
  560. */
  561. static void rs_hangup(struct tty_struct *tty)
  562. {
  563. struct async_struct * info = (struct async_struct *)tty->driver_data;
  564. struct serial_state *state = info->state;
  565. #ifdef SIMSERIAL_DEBUG
  566. printk("rs_hangup: called\n");
  567. #endif
  568. state = info->state;
  569. rs_flush_buffer(tty);
  570. if (info->flags & ASYNC_CLOSING)
  571. return;
  572. shutdown(info);
  573. info->event = 0;
  574. state->count = 0;
  575. info->flags &= ~ASYNC_NORMAL_ACTIVE;
  576. info->tty = 0;
  577. wake_up_interruptible(&info->open_wait);
  578. }
  579. static int get_async_struct(int line, struct async_struct **ret_info)
  580. {
  581. struct async_struct *info;
  582. struct serial_state *sstate;
  583. sstate = rs_table + line;
  584. sstate->count++;
  585. if (sstate->info) {
  586. *ret_info = sstate->info;
  587. return 0;
  588. }
  589. info = kmalloc(sizeof(struct async_struct), GFP_KERNEL);
  590. if (!info) {
  591. sstate->count--;
  592. return -ENOMEM;
  593. }
  594. memset(info, 0, sizeof(struct async_struct));
  595. init_waitqueue_head(&info->open_wait);
  596. init_waitqueue_head(&info->close_wait);
  597. init_waitqueue_head(&info->delta_msr_wait);
  598. info->magic = SERIAL_MAGIC;
  599. info->port = sstate->port;
  600. info->flags = sstate->flags;
  601. info->xmit_fifo_size = sstate->xmit_fifo_size;
  602. info->line = line;
  603. INIT_WORK(&info->work, do_softint, info);
  604. info->state = sstate;
  605. if (sstate->info) {
  606. kfree(info);
  607. *ret_info = sstate->info;
  608. return 0;
  609. }
  610. *ret_info = sstate->info = info;
  611. return 0;
  612. }
  613. static int
  614. startup(struct async_struct *info)
  615. {
  616. unsigned long flags;
  617. int retval=0;
  618. irqreturn_t (*handler)(int, void *, struct pt_regs *);
  619. struct serial_state *state= info->state;
  620. unsigned long page;
  621. page = get_zeroed_page(GFP_KERNEL);
  622. if (!page)
  623. return -ENOMEM;
  624. local_irq_save(flags);
  625. if (info->flags & ASYNC_INITIALIZED) {
  626. free_page(page);
  627. goto errout;
  628. }
  629. if (!state->port || !state->type) {
  630. if (info->tty) set_bit(TTY_IO_ERROR, &info->tty->flags);
  631. free_page(page);
  632. goto errout;
  633. }
  634. if (info->xmit.buf)
  635. free_page(page);
  636. else
  637. info->xmit.buf = (unsigned char *) page;
  638. #ifdef SIMSERIAL_DEBUG
  639. printk("startup: ttys%d (irq %d)...", info->line, state->irq);
  640. #endif
  641. /*
  642. * Allocate the IRQ if necessary
  643. */
  644. if (state->irq && (!IRQ_ports[state->irq] ||
  645. !IRQ_ports[state->irq]->next_port)) {
  646. if (IRQ_ports[state->irq]) {
  647. retval = -EBUSY;
  648. goto errout;
  649. } else
  650. handler = rs_interrupt_single;
  651. retval = request_irq(state->irq, handler, IRQ_T(info), "simserial", NULL);
  652. if (retval) {
  653. if (capable(CAP_SYS_ADMIN)) {
  654. if (info->tty)
  655. set_bit(TTY_IO_ERROR,
  656. &info->tty->flags);
  657. retval = 0;
  658. }
  659. goto errout;
  660. }
  661. }
  662. /*
  663. * Insert serial port into IRQ chain.
  664. */
  665. info->prev_port = 0;
  666. info->next_port = IRQ_ports[state->irq];
  667. if (info->next_port)
  668. info->next_port->prev_port = info;
  669. IRQ_ports[state->irq] = info;
  670. if (info->tty) clear_bit(TTY_IO_ERROR, &info->tty->flags);
  671. info->xmit.head = info->xmit.tail = 0;
  672. #if 0
  673. /*
  674. * Set up serial timers...
  675. */
  676. timer_table[RS_TIMER].expires = jiffies + 2*HZ/100;
  677. timer_active |= 1 << RS_TIMER;
  678. #endif
  679. /*
  680. * Set up the tty->alt_speed kludge
  681. */
  682. if (info->tty) {
  683. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  684. info->tty->alt_speed = 57600;
  685. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  686. info->tty->alt_speed = 115200;
  687. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
  688. info->tty->alt_speed = 230400;
  689. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
  690. info->tty->alt_speed = 460800;
  691. }
  692. info->flags |= ASYNC_INITIALIZED;
  693. local_irq_restore(flags);
  694. return 0;
  695. errout:
  696. local_irq_restore(flags);
  697. return retval;
  698. }
  699. /*
  700. * This routine is called whenever a serial port is opened. It
  701. * enables interrupts for a serial port, linking in its async structure into
  702. * the IRQ chain. It also performs the serial-specific
  703. * initialization for the tty structure.
  704. */
  705. static int rs_open(struct tty_struct *tty, struct file * filp)
  706. {
  707. struct async_struct *info;
  708. int retval, line;
  709. unsigned long page;
  710. line = tty->index;
  711. if ((line < 0) || (line >= NR_PORTS))
  712. return -ENODEV;
  713. retval = get_async_struct(line, &info);
  714. if (retval)
  715. return retval;
  716. tty->driver_data = info;
  717. info->tty = tty;
  718. #ifdef SIMSERIAL_DEBUG
  719. printk("rs_open %s, count = %d\n", tty->name, info->state->count);
  720. #endif
  721. info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  722. if (!tmp_buf) {
  723. page = get_zeroed_page(GFP_KERNEL);
  724. if (!page)
  725. return -ENOMEM;
  726. if (tmp_buf)
  727. free_page(page);
  728. else
  729. tmp_buf = (unsigned char *) page;
  730. }
  731. /*
  732. * If the port is the middle of closing, bail out now
  733. */
  734. if (tty_hung_up_p(filp) ||
  735. (info->flags & ASYNC_CLOSING)) {
  736. if (info->flags & ASYNC_CLOSING)
  737. interruptible_sleep_on(&info->close_wait);
  738. #ifdef SERIAL_DO_RESTART
  739. return ((info->flags & ASYNC_HUP_NOTIFY) ?
  740. -EAGAIN : -ERESTARTSYS);
  741. #else
  742. return -EAGAIN;
  743. #endif
  744. }
  745. /*
  746. * Start up serial port
  747. */
  748. retval = startup(info);
  749. if (retval) {
  750. return retval;
  751. }
  752. /*
  753. * figure out which console to use (should be one already)
  754. */
  755. console = console_drivers;
  756. while (console) {
  757. if ((console->flags & CON_ENABLED) && console->write) break;
  758. console = console->next;
  759. }
  760. #ifdef SIMSERIAL_DEBUG
  761. printk("rs_open ttys%d successful\n", info->line);
  762. #endif
  763. return 0;
  764. }
  765. /*
  766. * /proc fs routines....
  767. */
  768. static inline int line_info(char *buf, struct serial_state *state)
  769. {
  770. return sprintf(buf, "%d: uart:%s port:%lX irq:%d\n",
  771. state->line, uart_config[state->type].name,
  772. state->port, state->irq);
  773. }
  774. static int rs_read_proc(char *page, char **start, off_t off, int count,
  775. int *eof, void *data)
  776. {
  777. int i, len = 0, l;
  778. off_t begin = 0;
  779. len += sprintf(page, "simserinfo:1.0 driver:%s\n", serial_version);
  780. for (i = 0; i < NR_PORTS && len < 4000; i++) {
  781. l = line_info(page + len, &rs_table[i]);
  782. len += l;
  783. if (len+begin > off+count)
  784. goto done;
  785. if (len+begin < off) {
  786. begin += len;
  787. len = 0;
  788. }
  789. }
  790. *eof = 1;
  791. done:
  792. if (off >= len+begin)
  793. return 0;
  794. *start = page + (begin-off);
  795. return ((count < begin+len-off) ? count : begin+len-off);
  796. }
  797. /*
  798. * ---------------------------------------------------------------------
  799. * rs_init() and friends
  800. *
  801. * rs_init() is called at boot-time to initialize the serial driver.
  802. * ---------------------------------------------------------------------
  803. */
  804. /*
  805. * This routine prints out the appropriate serial driver version
  806. * number, and identifies which options were configured into this
  807. * driver.
  808. */
  809. static inline void show_serial_version(void)
  810. {
  811. printk(KERN_INFO "%s version %s with", serial_name, serial_version);
  812. printk(KERN_INFO " no serial options enabled\n");
  813. }
  814. static struct tty_operations hp_ops = {
  815. .open = rs_open,
  816. .close = rs_close,
  817. .write = rs_write,
  818. .put_char = rs_put_char,
  819. .flush_chars = rs_flush_chars,
  820. .write_room = rs_write_room,
  821. .chars_in_buffer = rs_chars_in_buffer,
  822. .flush_buffer = rs_flush_buffer,
  823. .ioctl = rs_ioctl,
  824. .throttle = rs_throttle,
  825. .unthrottle = rs_unthrottle,
  826. .send_xchar = rs_send_xchar,
  827. .set_termios = rs_set_termios,
  828. .stop = rs_stop,
  829. .start = rs_start,
  830. .hangup = rs_hangup,
  831. .break_ctl = rs_break,
  832. .wait_until_sent = rs_wait_until_sent,
  833. .read_proc = rs_read_proc,
  834. };
  835. /*
  836. * The serial driver boot-time initialization code!
  837. */
  838. static int __init
  839. simrs_init (void)
  840. {
  841. int i;
  842. struct serial_state *state;
  843. if (!ia64_platform_is("hpsim"))
  844. return -ENODEV;
  845. hp_simserial_driver = alloc_tty_driver(1);
  846. if (!hp_simserial_driver)
  847. return -ENOMEM;
  848. show_serial_version();
  849. /* Initialize the tty_driver structure */
  850. hp_simserial_driver->owner = THIS_MODULE;
  851. hp_simserial_driver->driver_name = "simserial";
  852. hp_simserial_driver->name = "ttyS";
  853. hp_simserial_driver->major = TTY_MAJOR;
  854. hp_simserial_driver->minor_start = 64;
  855. hp_simserial_driver->type = TTY_DRIVER_TYPE_SERIAL;
  856. hp_simserial_driver->subtype = SERIAL_TYPE_NORMAL;
  857. hp_simserial_driver->init_termios = tty_std_termios;
  858. hp_simserial_driver->init_termios.c_cflag =
  859. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  860. hp_simserial_driver->flags = TTY_DRIVER_REAL_RAW;
  861. tty_set_operations(hp_simserial_driver, &hp_ops);
  862. /*
  863. * Let's have a little bit of fun !
  864. */
  865. for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) {
  866. if (state->type == PORT_UNKNOWN) continue;
  867. if (!state->irq) {
  868. state->irq = assign_irq_vector(AUTO_ASSIGN);
  869. ia64_ssc_connect_irq(KEYBOARD_INTR, state->irq);
  870. }
  871. printk(KERN_INFO "ttyS%d at 0x%04lx (irq = %d) is a %s\n",
  872. state->line,
  873. state->port, state->irq,
  874. uart_config[state->type].name);
  875. }
  876. if (tty_register_driver(hp_simserial_driver))
  877. panic("Couldn't register simserial driver\n");
  878. return 0;
  879. }
  880. #ifndef MODULE
  881. __initcall(simrs_init);
  882. #endif