simserial.c 24 KB

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