simserial.c 23 KB

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