simserial.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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/init.h>
  19. #include <linux/errno.h>
  20. #include <linux/sched.h>
  21. #include <linux/tty.h>
  22. #include <linux/tty_flip.h>
  23. #include <linux/major.h>
  24. #include <linux/fcntl.h>
  25. #include <linux/mm.h>
  26. #include <linux/seq_file.h>
  27. #include <linux/slab.h>
  28. #include <linux/capability.h>
  29. #include <linux/console.h>
  30. #include <linux/module.h>
  31. #include <linux/serial.h>
  32. #include <linux/serialP.h>
  33. #include <linux/sysrq.h>
  34. #include <asm/irq.h>
  35. #include <asm/hpsim.h>
  36. #include <asm/hw_irq.h>
  37. #include <asm/uaccess.h>
  38. #include "hpsim_ssc.h"
  39. #undef SIMSERIAL_DEBUG /* define this to get some debug information */
  40. #define KEYBOARD_INTR 3 /* must match with simulator! */
  41. #define NR_PORTS 1 /* only one port for now */
  42. static char *serial_name = "SimSerial driver";
  43. static char *serial_version = "0.6";
  44. /*
  45. * This has been extracted from asm/serial.h. We need one eventually but
  46. * I don't know exactly what we're going to put in it so just fake one
  47. * for now.
  48. */
  49. #define BASE_BAUD ( 1843200 / 16 )
  50. #define STD_COM_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_SKIP_TEST)
  51. /*
  52. * Most of the values here are meaningless to this particular driver.
  53. * However some values must be preserved for the code (leveraged from serial.c
  54. * to work correctly).
  55. * port must not be 0
  56. * type must not be UNKNOWN
  57. * So I picked arbitrary (guess from where?) values instead
  58. */
  59. static struct serial_state rs_table[NR_PORTS]={
  60. /* UART CLK PORT IRQ FLAGS */
  61. { BASE_BAUD, 0x3F8, 0, STD_COM_FLAGS, PORT_16550 } /* ttyS0 */
  62. };
  63. /*
  64. * Just for the fun of it !
  65. */
  66. static struct serial_uart_config uart_config[] = {
  67. { "unknown", 1, 0 },
  68. { "8250", 1, 0 },
  69. { "16450", 1, 0 },
  70. { "16550", 1, 0 },
  71. { "16550A", 16, UART_CLEAR_FIFO | UART_USE_FIFO },
  72. { "cirrus", 1, 0 },
  73. { "ST16650", 1, UART_CLEAR_FIFO | UART_STARTECH },
  74. { "ST16650V2", 32, UART_CLEAR_FIFO | UART_USE_FIFO |
  75. UART_STARTECH },
  76. { "TI16750", 64, UART_CLEAR_FIFO | UART_USE_FIFO},
  77. { NULL, 0}
  78. };
  79. struct tty_driver *hp_simserial_driver;
  80. static struct console *console;
  81. static unsigned char *tmp_buf;
  82. extern struct console *console_drivers; /* from kernel/printk.c */
  83. /*
  84. * ------------------------------------------------------------
  85. * rs_stop() and rs_start()
  86. *
  87. * This routines are called before setting or resetting tty->stopped.
  88. * They enable or disable transmitter interrupts, as necessary.
  89. * ------------------------------------------------------------
  90. */
  91. static void rs_stop(struct tty_struct *tty)
  92. {
  93. #ifdef SIMSERIAL_DEBUG
  94. printk("rs_stop: tty->stopped=%d tty->hw_stopped=%d tty->flow_stopped=%d\n",
  95. tty->stopped, tty->hw_stopped, tty->flow_stopped);
  96. #endif
  97. }
  98. static void rs_start(struct tty_struct *tty)
  99. {
  100. #ifdef SIMSERIAL_DEBUG
  101. printk("rs_start: tty->stopped=%d tty->hw_stopped=%d tty->flow_stopped=%d\n",
  102. tty->stopped, tty->hw_stopped, tty->flow_stopped);
  103. #endif
  104. }
  105. static void receive_chars(struct tty_struct *tty)
  106. {
  107. unsigned char ch;
  108. static unsigned char seen_esc = 0;
  109. while ( (ch = ia64_ssc(0, 0, 0, 0, SSC_GETCHAR)) ) {
  110. if ( ch == 27 && seen_esc == 0 ) {
  111. seen_esc = 1;
  112. continue;
  113. } else {
  114. if ( seen_esc==1 && ch == 'O' ) {
  115. seen_esc = 2;
  116. continue;
  117. } else if ( seen_esc == 2 ) {
  118. if ( ch == 'P' ) /* F1 */
  119. show_state();
  120. #ifdef CONFIG_MAGIC_SYSRQ
  121. if ( ch == 'S' ) { /* F4 */
  122. do
  123. ch = ia64_ssc(0, 0, 0, 0,
  124. SSC_GETCHAR);
  125. while (!ch);
  126. handle_sysrq(ch);
  127. }
  128. #endif
  129. seen_esc = 0;
  130. continue;
  131. }
  132. }
  133. seen_esc = 0;
  134. if (tty_insert_flip_char(tty, ch, TTY_NORMAL) == 0)
  135. break;
  136. }
  137. tty_flip_buffer_push(tty);
  138. }
  139. /*
  140. * This is the serial driver's interrupt routine for a single port
  141. */
  142. static irqreturn_t rs_interrupt_single(int irq, void *dev_id)
  143. {
  144. struct serial_state *info = dev_id;
  145. if (!info->tport.tty) {
  146. printk(KERN_INFO "simrs_interrupt_single: info|tty=0 info=%p problem\n", info);
  147. return IRQ_NONE;
  148. }
  149. /*
  150. * pretty simple in our case, because we only get interrupts
  151. * on inbound traffic
  152. */
  153. receive_chars(info->tport.tty);
  154. return IRQ_HANDLED;
  155. }
  156. /*
  157. * -------------------------------------------------------------------
  158. * Here ends the serial interrupt routines.
  159. * -------------------------------------------------------------------
  160. */
  161. static int rs_put_char(struct tty_struct *tty, unsigned char ch)
  162. {
  163. struct serial_state *info = tty->driver_data;
  164. unsigned long flags;
  165. if (!tty || !info->xmit.buf)
  166. return 0;
  167. local_irq_save(flags);
  168. if (CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) == 0) {
  169. local_irq_restore(flags);
  170. return 0;
  171. }
  172. info->xmit.buf[info->xmit.head] = ch;
  173. info->xmit.head = (info->xmit.head + 1) & (SERIAL_XMIT_SIZE-1);
  174. local_irq_restore(flags);
  175. return 1;
  176. }
  177. static void transmit_chars(struct tty_struct *tty, struct serial_state *info,
  178. int *intr_done)
  179. {
  180. int count;
  181. unsigned long flags;
  182. local_irq_save(flags);
  183. if (info->x_char) {
  184. char c = info->x_char;
  185. console->write(console, &c, 1);
  186. info->icount.tx++;
  187. info->x_char = 0;
  188. goto out;
  189. }
  190. if (info->xmit.head == info->xmit.tail || tty->stopped ||
  191. tty->hw_stopped) {
  192. #ifdef SIMSERIAL_DEBUG
  193. printk("transmit_chars: head=%d, tail=%d, stopped=%d\n",
  194. info->xmit.head, info->xmit.tail, tty->stopped);
  195. #endif
  196. goto out;
  197. }
  198. /*
  199. * We removed the loop and try to do it in to chunks. We need
  200. * 2 operations maximum because it's a ring buffer.
  201. *
  202. * First from current to tail if possible.
  203. * Then from the beginning of the buffer until necessary
  204. */
  205. count = min(CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE),
  206. SERIAL_XMIT_SIZE - info->xmit.tail);
  207. console->write(console, info->xmit.buf+info->xmit.tail, count);
  208. info->xmit.tail = (info->xmit.tail+count) & (SERIAL_XMIT_SIZE-1);
  209. /*
  210. * We have more at the beginning of the buffer
  211. */
  212. count = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  213. if (count) {
  214. console->write(console, info->xmit.buf, count);
  215. info->xmit.tail += count;
  216. }
  217. out:
  218. local_irq_restore(flags);
  219. }
  220. static void rs_flush_chars(struct tty_struct *tty)
  221. {
  222. struct serial_state *info = tty->driver_data;
  223. if (info->xmit.head == info->xmit.tail || tty->stopped || tty->hw_stopped ||
  224. !info->xmit.buf)
  225. return;
  226. transmit_chars(tty, info, NULL);
  227. }
  228. static int rs_write(struct tty_struct * tty,
  229. const unsigned char *buf, int count)
  230. {
  231. struct serial_state *info = tty->driver_data;
  232. int c, ret = 0;
  233. unsigned long flags;
  234. if (!tty || !info->xmit.buf || !tmp_buf) return 0;
  235. local_irq_save(flags);
  236. while (1) {
  237. c = CIRC_SPACE_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  238. if (count < c)
  239. c = count;
  240. if (c <= 0) {
  241. break;
  242. }
  243. memcpy(info->xmit.buf + info->xmit.head, buf, c);
  244. info->xmit.head = ((info->xmit.head + c) &
  245. (SERIAL_XMIT_SIZE-1));
  246. buf += c;
  247. count -= c;
  248. ret += c;
  249. }
  250. local_irq_restore(flags);
  251. /*
  252. * Hey, we transmit directly from here in our case
  253. */
  254. if (CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE)
  255. && !tty->stopped && !tty->hw_stopped) {
  256. transmit_chars(tty, info, NULL);
  257. }
  258. return ret;
  259. }
  260. static int rs_write_room(struct tty_struct *tty)
  261. {
  262. struct serial_state *info = tty->driver_data;
  263. return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  264. }
  265. static int rs_chars_in_buffer(struct tty_struct *tty)
  266. {
  267. struct serial_state *info = tty->driver_data;
  268. return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  269. }
  270. static void rs_flush_buffer(struct tty_struct *tty)
  271. {
  272. struct serial_state *info = tty->driver_data;
  273. unsigned long flags;
  274. local_irq_save(flags);
  275. info->xmit.head = info->xmit.tail = 0;
  276. local_irq_restore(flags);
  277. tty_wakeup(tty);
  278. }
  279. /*
  280. * This function is used to send a high-priority XON/XOFF character to
  281. * the device
  282. */
  283. static void rs_send_xchar(struct tty_struct *tty, char ch)
  284. {
  285. struct serial_state *info = tty->driver_data;
  286. info->x_char = ch;
  287. if (ch) {
  288. /*
  289. * I guess we could call console->write() directly but
  290. * let's do that for now.
  291. */
  292. transmit_chars(tty, info, NULL);
  293. }
  294. }
  295. /*
  296. * ------------------------------------------------------------
  297. * rs_throttle()
  298. *
  299. * This routine is called by the upper-layer tty layer to signal that
  300. * incoming characters should be throttled.
  301. * ------------------------------------------------------------
  302. */
  303. static void rs_throttle(struct tty_struct * tty)
  304. {
  305. if (I_IXOFF(tty)) rs_send_xchar(tty, STOP_CHAR(tty));
  306. printk(KERN_INFO "simrs_throttle called\n");
  307. }
  308. static void rs_unthrottle(struct tty_struct * tty)
  309. {
  310. struct serial_state *info = tty->driver_data;
  311. if (I_IXOFF(tty)) {
  312. if (info->x_char)
  313. info->x_char = 0;
  314. else
  315. rs_send_xchar(tty, START_CHAR(tty));
  316. }
  317. printk(KERN_INFO "simrs_unthrottle called\n");
  318. }
  319. static int rs_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
  320. {
  321. if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
  322. (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
  323. (cmd != TIOCMIWAIT)) {
  324. if (tty->flags & (1 << TTY_IO_ERROR))
  325. return -EIO;
  326. }
  327. switch (cmd) {
  328. case TIOCGSERIAL:
  329. printk(KERN_INFO "simrs_ioctl TIOCGSERIAL called\n");
  330. return 0;
  331. case TIOCSSERIAL:
  332. printk(KERN_INFO "simrs_ioctl TIOCSSERIAL called\n");
  333. return 0;
  334. case TIOCSERCONFIG:
  335. printk(KERN_INFO "rs_ioctl: TIOCSERCONFIG called\n");
  336. return -EINVAL;
  337. case TIOCSERGETLSR: /* Get line status register */
  338. printk(KERN_INFO "rs_ioctl: TIOCSERGETLSR called\n");
  339. return -EINVAL;
  340. case TIOCSERGSTRUCT:
  341. printk(KERN_INFO "rs_ioctl: TIOCSERGSTRUCT called\n");
  342. #if 0
  343. if (copy_to_user((struct async_struct *) arg,
  344. info, sizeof(struct async_struct)))
  345. return -EFAULT;
  346. #endif
  347. return 0;
  348. /*
  349. * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  350. * - mask passed in arg for lines of interest
  351. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  352. * Caller should use TIOCGICOUNT to see which one it was
  353. */
  354. case TIOCMIWAIT:
  355. printk(KERN_INFO "rs_ioctl: TIOCMIWAIT: called\n");
  356. return 0;
  357. case TIOCSERGWILD:
  358. case TIOCSERSWILD:
  359. /* "setserial -W" is called in Debian boot */
  360. printk (KERN_INFO "TIOCSER?WILD ioctl obsolete, ignored.\n");
  361. return 0;
  362. default:
  363. return -ENOIOCTLCMD;
  364. }
  365. return 0;
  366. }
  367. #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
  368. static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
  369. {
  370. /* Handle turning off CRTSCTS */
  371. if ((old_termios->c_cflag & CRTSCTS) &&
  372. !(tty->termios->c_cflag & CRTSCTS)) {
  373. tty->hw_stopped = 0;
  374. rs_start(tty);
  375. }
  376. }
  377. /*
  378. * This routine will shutdown a serial port; interrupts are disabled, and
  379. * DTR is dropped if the hangup on close termio flag is on.
  380. */
  381. static void shutdown(struct tty_struct *tty, struct serial_state *info)
  382. {
  383. unsigned long flags;
  384. if (!(info->flags & ASYNC_INITIALIZED))
  385. return;
  386. #ifdef SIMSERIAL_DEBUG
  387. printk("Shutting down serial port %d (irq %d)...\n", info->line,
  388. info->irq);
  389. #endif
  390. local_irq_save(flags);
  391. {
  392. if (info->irq)
  393. free_irq(info->irq, info);
  394. if (info->xmit.buf) {
  395. free_page((unsigned long) info->xmit.buf);
  396. info->xmit.buf = NULL;
  397. }
  398. set_bit(TTY_IO_ERROR, &tty->flags);
  399. info->flags &= ~ASYNC_INITIALIZED;
  400. }
  401. local_irq_restore(flags);
  402. }
  403. /*
  404. * ------------------------------------------------------------
  405. * rs_close()
  406. *
  407. * This routine is called when the serial port gets closed. First, we
  408. * wait for the last remaining data to be sent. Then, we unlink its
  409. * async structure from the interrupt chain if necessary, and we free
  410. * that IRQ if nothing is left in the chain.
  411. * ------------------------------------------------------------
  412. */
  413. static void rs_close(struct tty_struct *tty, struct file * filp)
  414. {
  415. struct serial_state *info = tty->driver_data;
  416. unsigned long flags;
  417. if (!info)
  418. return;
  419. local_irq_save(flags);
  420. if (tty_hung_up_p(filp)) {
  421. #ifdef SIMSERIAL_DEBUG
  422. printk("rs_close: hung_up\n");
  423. #endif
  424. local_irq_restore(flags);
  425. return;
  426. }
  427. #ifdef SIMSERIAL_DEBUG
  428. printk("rs_close ttys%d, count = %d\n", info->line, info->tport.count);
  429. #endif
  430. if ((tty->count == 1) && (info->tport.count != 1)) {
  431. /*
  432. * Uh, oh. tty->count is 1, which means that the tty
  433. * structure will be freed. info->tport.count should always
  434. * be one in these conditions. If it's greater than
  435. * one, we've got real problems, since it means the
  436. * serial port won't be shutdown.
  437. */
  438. printk(KERN_ERR "rs_close: bad serial port count; tty->count is 1, "
  439. "info->tport.count is %d\n", info->tport.count);
  440. info->tport.count = 1;
  441. }
  442. if (--info->tport.count < 0) {
  443. printk(KERN_ERR "rs_close: bad serial port count for ttys%d: %d\n",
  444. info->line, info->tport.count);
  445. info->tport.count = 0;
  446. }
  447. if (info->tport.count) {
  448. local_irq_restore(flags);
  449. return;
  450. }
  451. info->flags |= ASYNC_CLOSING;
  452. local_irq_restore(flags);
  453. /*
  454. * Now we wait for the transmit buffer to clear; and we notify
  455. * the line discipline to only process XON/XOFF characters.
  456. */
  457. shutdown(tty, info);
  458. rs_flush_buffer(tty);
  459. tty_ldisc_flush(tty);
  460. info->tport.tty = NULL;
  461. if (info->tport.blocked_open) {
  462. if (info->tport.close_delay)
  463. schedule_timeout_interruptible(info->tport.close_delay);
  464. wake_up_interruptible(&info->tport.open_wait);
  465. }
  466. info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
  467. wake_up_interruptible(&info->tport.close_wait);
  468. }
  469. /*
  470. * rs_wait_until_sent() --- wait until the transmitter is empty
  471. */
  472. static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
  473. {
  474. }
  475. /*
  476. * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
  477. */
  478. static void rs_hangup(struct tty_struct *tty)
  479. {
  480. struct serial_state *info = tty->driver_data;
  481. #ifdef SIMSERIAL_DEBUG
  482. printk("rs_hangup: called\n");
  483. #endif
  484. rs_flush_buffer(tty);
  485. if (info->flags & ASYNC_CLOSING)
  486. return;
  487. shutdown(tty, info);
  488. info->tport.count = 0;
  489. info->flags &= ~ASYNC_NORMAL_ACTIVE;
  490. info->tport.tty = NULL;
  491. wake_up_interruptible(&info->tport.open_wait);
  492. }
  493. static int startup(struct tty_struct *tty, struct serial_state *state)
  494. {
  495. unsigned long flags;
  496. int retval=0;
  497. unsigned long page;
  498. page = get_zeroed_page(GFP_KERNEL);
  499. if (!page)
  500. return -ENOMEM;
  501. local_irq_save(flags);
  502. if (state->flags & ASYNC_INITIALIZED) {
  503. free_page(page);
  504. goto errout;
  505. }
  506. if (!state->port || !state->type) {
  507. set_bit(TTY_IO_ERROR, &tty->flags);
  508. free_page(page);
  509. goto errout;
  510. }
  511. if (state->xmit.buf)
  512. free_page(page);
  513. else
  514. state->xmit.buf = (unsigned char *) page;
  515. #ifdef SIMSERIAL_DEBUG
  516. printk("startup: ttys%d (irq %d)...", state->line, state->irq);
  517. #endif
  518. /*
  519. * Allocate the IRQ if necessary
  520. */
  521. if (state->irq) {
  522. retval = request_irq(state->irq, rs_interrupt_single, 0,
  523. "simserial", state);
  524. if (retval)
  525. goto errout;
  526. }
  527. clear_bit(TTY_IO_ERROR, &tty->flags);
  528. state->xmit.head = state->xmit.tail = 0;
  529. #if 0
  530. /*
  531. * Set up serial timers...
  532. */
  533. timer_table[RS_TIMER].expires = jiffies + 2*HZ/100;
  534. timer_active |= 1 << RS_TIMER;
  535. #endif
  536. /*
  537. * Set up the tty->alt_speed kludge
  538. */
  539. if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  540. tty->alt_speed = 57600;
  541. if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  542. tty->alt_speed = 115200;
  543. if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
  544. tty->alt_speed = 230400;
  545. if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
  546. tty->alt_speed = 460800;
  547. state->flags |= ASYNC_INITIALIZED;
  548. local_irq_restore(flags);
  549. return 0;
  550. errout:
  551. local_irq_restore(flags);
  552. return retval;
  553. }
  554. /*
  555. * This routine is called whenever a serial port is opened. It
  556. * enables interrupts for a serial port, linking in its async structure into
  557. * the IRQ chain. It also performs the serial-specific
  558. * initialization for the tty structure.
  559. */
  560. static int rs_open(struct tty_struct *tty, struct file * filp)
  561. {
  562. struct serial_state *info = rs_table + tty->index;
  563. int retval;
  564. unsigned long page;
  565. info->tport.count++;
  566. info->tport.tty = tty;
  567. tty->driver_data = info;
  568. tty->port = &info->tport;
  569. #ifdef SIMSERIAL_DEBUG
  570. printk("rs_open %s, count = %d\n", tty->name, info->tport.count);
  571. #endif
  572. tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  573. if (!tmp_buf) {
  574. page = get_zeroed_page(GFP_KERNEL);
  575. if (!page)
  576. return -ENOMEM;
  577. if (tmp_buf)
  578. free_page(page);
  579. else
  580. tmp_buf = (unsigned char *) page;
  581. }
  582. /*
  583. * If the port is the middle of closing, bail out now
  584. */
  585. if (tty_hung_up_p(filp) || (info->flags & ASYNC_CLOSING)) {
  586. if (info->flags & ASYNC_CLOSING)
  587. interruptible_sleep_on(&info->tport.close_wait);
  588. #ifdef SERIAL_DO_RESTART
  589. return ((info->flags & ASYNC_HUP_NOTIFY) ?
  590. -EAGAIN : -ERESTARTSYS);
  591. #else
  592. return -EAGAIN;
  593. #endif
  594. }
  595. /*
  596. * Start up serial port
  597. */
  598. retval = startup(tty, info);
  599. if (retval) {
  600. return retval;
  601. }
  602. /*
  603. * figure out which console to use (should be one already)
  604. */
  605. console = console_drivers;
  606. while (console) {
  607. if ((console->flags & CON_ENABLED) && console->write) break;
  608. console = console->next;
  609. }
  610. #ifdef SIMSERIAL_DEBUG
  611. printk("rs_open ttys%d successful\n", info->line);
  612. #endif
  613. return 0;
  614. }
  615. /*
  616. * /proc fs routines....
  617. */
  618. static inline void line_info(struct seq_file *m, struct serial_state *state)
  619. {
  620. seq_printf(m, "%d: uart:%s port:%lX irq:%d\n",
  621. state->line, uart_config[state->type].name,
  622. state->port, state->irq);
  623. }
  624. static int rs_proc_show(struct seq_file *m, void *v)
  625. {
  626. int i;
  627. seq_printf(m, "simserinfo:1.0 driver:%s\n", serial_version);
  628. for (i = 0; i < NR_PORTS; i++)
  629. line_info(m, &rs_table[i]);
  630. return 0;
  631. }
  632. static int rs_proc_open(struct inode *inode, struct file *file)
  633. {
  634. return single_open(file, rs_proc_show, NULL);
  635. }
  636. static const struct file_operations rs_proc_fops = {
  637. .owner = THIS_MODULE,
  638. .open = rs_proc_open,
  639. .read = seq_read,
  640. .llseek = seq_lseek,
  641. .release = single_release,
  642. };
  643. /*
  644. * ---------------------------------------------------------------------
  645. * rs_init() and friends
  646. *
  647. * rs_init() is called at boot-time to initialize the serial driver.
  648. * ---------------------------------------------------------------------
  649. */
  650. /*
  651. * This routine prints out the appropriate serial driver version
  652. * number, and identifies which options were configured into this
  653. * driver.
  654. */
  655. static inline void show_serial_version(void)
  656. {
  657. printk(KERN_INFO "%s version %s with", serial_name, serial_version);
  658. printk(KERN_INFO " no serial options enabled\n");
  659. }
  660. static const struct tty_operations hp_ops = {
  661. .open = rs_open,
  662. .close = rs_close,
  663. .write = rs_write,
  664. .put_char = rs_put_char,
  665. .flush_chars = rs_flush_chars,
  666. .write_room = rs_write_room,
  667. .chars_in_buffer = rs_chars_in_buffer,
  668. .flush_buffer = rs_flush_buffer,
  669. .ioctl = rs_ioctl,
  670. .throttle = rs_throttle,
  671. .unthrottle = rs_unthrottle,
  672. .send_xchar = rs_send_xchar,
  673. .set_termios = rs_set_termios,
  674. .stop = rs_stop,
  675. .start = rs_start,
  676. .hangup = rs_hangup,
  677. .wait_until_sent = rs_wait_until_sent,
  678. .proc_fops = &rs_proc_fops,
  679. };
  680. /*
  681. * The serial driver boot-time initialization code!
  682. */
  683. static int __init
  684. simrs_init (void)
  685. {
  686. int i, rc;
  687. struct serial_state *state;
  688. if (!ia64_platform_is("hpsim"))
  689. return -ENODEV;
  690. hp_simserial_driver = alloc_tty_driver(NR_PORTS);
  691. if (!hp_simserial_driver)
  692. return -ENOMEM;
  693. show_serial_version();
  694. /* Initialize the tty_driver structure */
  695. hp_simserial_driver->driver_name = "simserial";
  696. hp_simserial_driver->name = "ttyS";
  697. hp_simserial_driver->major = TTY_MAJOR;
  698. hp_simserial_driver->minor_start = 64;
  699. hp_simserial_driver->type = TTY_DRIVER_TYPE_SERIAL;
  700. hp_simserial_driver->subtype = SERIAL_TYPE_NORMAL;
  701. hp_simserial_driver->init_termios = tty_std_termios;
  702. hp_simserial_driver->init_termios.c_cflag =
  703. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  704. hp_simserial_driver->flags = TTY_DRIVER_REAL_RAW;
  705. tty_set_operations(hp_simserial_driver, &hp_ops);
  706. /*
  707. * Let's have a little bit of fun !
  708. */
  709. for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) {
  710. tty_port_init(&state->tport);
  711. state->tport.close_delay = 0; /* XXX really 0? */
  712. if (state->type == PORT_UNKNOWN) continue;
  713. if (!state->irq) {
  714. if ((rc = hpsim_get_irq(KEYBOARD_INTR)) < 0)
  715. panic("%s: out of interrupt vectors!\n",
  716. __func__);
  717. state->irq = rc;
  718. }
  719. printk(KERN_INFO "ttyS%d at 0x%04lx (irq = %d) is a %s\n",
  720. state->line,
  721. state->port, state->irq,
  722. uart_config[state->type].name);
  723. }
  724. if (tty_register_driver(hp_simserial_driver))
  725. panic("Couldn't register simserial driver\n");
  726. return 0;
  727. }
  728. #ifndef MODULE
  729. __initcall(simrs_init);
  730. #endif