simserial.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  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, NULL);
  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, struct file * file,
  330. unsigned int cmd, unsigned long arg)
  331. {
  332. if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
  333. (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
  334. (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
  335. if (tty->flags & (1 << TTY_IO_ERROR))
  336. return -EIO;
  337. }
  338. switch (cmd) {
  339. case TIOCGSERIAL:
  340. printk(KERN_INFO "simrs_ioctl TIOCGSERIAL called\n");
  341. return 0;
  342. case TIOCSSERIAL:
  343. printk(KERN_INFO "simrs_ioctl TIOCSSERIAL called\n");
  344. return 0;
  345. case TIOCSERCONFIG:
  346. printk(KERN_INFO "rs_ioctl: TIOCSERCONFIG called\n");
  347. return -EINVAL;
  348. case TIOCSERGETLSR: /* Get line status register */
  349. printk(KERN_INFO "rs_ioctl: TIOCSERGETLSR called\n");
  350. return -EINVAL;
  351. case TIOCSERGSTRUCT:
  352. printk(KERN_INFO "rs_ioctl: TIOCSERGSTRUCT called\n");
  353. #if 0
  354. if (copy_to_user((struct async_struct *) arg,
  355. info, sizeof(struct async_struct)))
  356. return -EFAULT;
  357. #endif
  358. return 0;
  359. /*
  360. * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  361. * - mask passed in arg for lines of interest
  362. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  363. * Caller should use TIOCGICOUNT to see which one it was
  364. */
  365. case TIOCMIWAIT:
  366. printk(KERN_INFO "rs_ioctl: TIOCMIWAIT: called\n");
  367. return 0;
  368. /*
  369. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  370. * Return: write counters to the user passed counter struct
  371. * NB: both 1->0 and 0->1 transitions are counted except for
  372. * RI where only 0->1 is counted.
  373. */
  374. case TIOCGICOUNT:
  375. printk(KERN_INFO "rs_ioctl: TIOCGICOUNT called\n");
  376. return 0;
  377. case TIOCSERGWILD:
  378. case TIOCSERSWILD:
  379. /* "setserial -W" is called in Debian boot */
  380. printk (KERN_INFO "TIOCSER?WILD ioctl obsolete, ignored.\n");
  381. return 0;
  382. default:
  383. return -ENOIOCTLCMD;
  384. }
  385. return 0;
  386. }
  387. #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
  388. static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
  389. {
  390. /* Handle turning off CRTSCTS */
  391. if ((old_termios->c_cflag & CRTSCTS) &&
  392. !(tty->termios->c_cflag & CRTSCTS)) {
  393. tty->hw_stopped = 0;
  394. rs_start(tty);
  395. }
  396. }
  397. /*
  398. * This routine will shutdown a serial port; interrupts are disabled, and
  399. * DTR is dropped if the hangup on close termio flag is on.
  400. */
  401. static void shutdown(struct async_struct * info)
  402. {
  403. unsigned long flags;
  404. struct serial_state *state;
  405. int retval;
  406. if (!(info->flags & ASYNC_INITIALIZED)) return;
  407. state = info->state;
  408. #ifdef SIMSERIAL_DEBUG
  409. printk("Shutting down serial port %d (irq %d)....", info->line,
  410. state->irq);
  411. #endif
  412. local_irq_save(flags);
  413. {
  414. /*
  415. * First unlink the serial port from the IRQ chain...
  416. */
  417. if (info->next_port)
  418. info->next_port->prev_port = info->prev_port;
  419. if (info->prev_port)
  420. info->prev_port->next_port = info->next_port;
  421. else
  422. IRQ_ports[state->irq] = info->next_port;
  423. /*
  424. * Free the IRQ, if necessary
  425. */
  426. if (state->irq && (!IRQ_ports[state->irq] ||
  427. !IRQ_ports[state->irq]->next_port)) {
  428. if (IRQ_ports[state->irq]) {
  429. free_irq(state->irq, NULL);
  430. retval = request_irq(state->irq, rs_interrupt_single,
  431. IRQ_T(info), "serial", NULL);
  432. if (retval)
  433. printk(KERN_ERR "serial shutdown: request_irq: error %d"
  434. " Couldn't reacquire IRQ.\n", retval);
  435. } else
  436. free_irq(state->irq, NULL);
  437. }
  438. if (info->xmit.buf) {
  439. free_page((unsigned long) info->xmit.buf);
  440. info->xmit.buf = NULL;
  441. }
  442. if (info->tty) set_bit(TTY_IO_ERROR, &info->tty->flags);
  443. info->flags &= ~ASYNC_INITIALIZED;
  444. }
  445. local_irq_restore(flags);
  446. }
  447. /*
  448. * ------------------------------------------------------------
  449. * rs_close()
  450. *
  451. * This routine is called when the serial port gets closed. First, we
  452. * wait for the last remaining data to be sent. Then, we unlink its
  453. * async structure from the interrupt chain if necessary, and we free
  454. * that IRQ if nothing is left in the chain.
  455. * ------------------------------------------------------------
  456. */
  457. static void rs_close(struct tty_struct *tty, struct file * filp)
  458. {
  459. struct async_struct * info = (struct async_struct *)tty->driver_data;
  460. struct serial_state *state;
  461. unsigned long flags;
  462. if (!info ) return;
  463. state = info->state;
  464. local_irq_save(flags);
  465. if (tty_hung_up_p(filp)) {
  466. #ifdef SIMSERIAL_DEBUG
  467. printk("rs_close: hung_up\n");
  468. #endif
  469. local_irq_restore(flags);
  470. return;
  471. }
  472. #ifdef SIMSERIAL_DEBUG
  473. printk("rs_close ttys%d, count = %d\n", info->line, state->count);
  474. #endif
  475. if ((tty->count == 1) && (state->count != 1)) {
  476. /*
  477. * Uh, oh. tty->count is 1, which means that the tty
  478. * structure will be freed. state->count should always
  479. * be one in these conditions. If it's greater than
  480. * one, we've got real problems, since it means the
  481. * serial port won't be shutdown.
  482. */
  483. printk(KERN_ERR "rs_close: bad serial port count; tty->count is 1, "
  484. "state->count is %d\n", state->count);
  485. state->count = 1;
  486. }
  487. if (--state->count < 0) {
  488. printk(KERN_ERR "rs_close: bad serial port count for ttys%d: %d\n",
  489. info->line, state->count);
  490. state->count = 0;
  491. }
  492. if (state->count) {
  493. local_irq_restore(flags);
  494. return;
  495. }
  496. info->flags |= ASYNC_CLOSING;
  497. local_irq_restore(flags);
  498. /*
  499. * Now we wait for the transmit buffer to clear; and we notify
  500. * the line discipline to only process XON/XOFF characters.
  501. */
  502. shutdown(info);
  503. rs_flush_buffer(tty);
  504. tty_ldisc_flush(tty);
  505. info->event = 0;
  506. info->tty = NULL;
  507. if (info->blocked_open) {
  508. if (info->close_delay)
  509. schedule_timeout_interruptible(info->close_delay);
  510. wake_up_interruptible(&info->open_wait);
  511. }
  512. info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
  513. wake_up_interruptible(&info->close_wait);
  514. }
  515. /*
  516. * rs_wait_until_sent() --- wait until the transmitter is empty
  517. */
  518. static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
  519. {
  520. }
  521. /*
  522. * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
  523. */
  524. static void rs_hangup(struct tty_struct *tty)
  525. {
  526. struct async_struct * info = (struct async_struct *)tty->driver_data;
  527. struct serial_state *state = info->state;
  528. #ifdef SIMSERIAL_DEBUG
  529. printk("rs_hangup: called\n");
  530. #endif
  531. state = info->state;
  532. rs_flush_buffer(tty);
  533. if (info->flags & ASYNC_CLOSING)
  534. return;
  535. shutdown(info);
  536. info->event = 0;
  537. state->count = 0;
  538. info->flags &= ~ASYNC_NORMAL_ACTIVE;
  539. info->tty = NULL;
  540. wake_up_interruptible(&info->open_wait);
  541. }
  542. static int get_async_struct(int line, struct async_struct **ret_info)
  543. {
  544. struct async_struct *info;
  545. struct serial_state *sstate;
  546. sstate = rs_table + line;
  547. sstate->count++;
  548. if (sstate->info) {
  549. *ret_info = sstate->info;
  550. return 0;
  551. }
  552. info = kzalloc(sizeof(struct async_struct), GFP_KERNEL);
  553. if (!info) {
  554. sstate->count--;
  555. return -ENOMEM;
  556. }
  557. init_waitqueue_head(&info->open_wait);
  558. init_waitqueue_head(&info->close_wait);
  559. init_waitqueue_head(&info->delta_msr_wait);
  560. info->magic = SERIAL_MAGIC;
  561. info->port = sstate->port;
  562. info->flags = sstate->flags;
  563. info->xmit_fifo_size = sstate->xmit_fifo_size;
  564. info->line = line;
  565. INIT_WORK(&info->work, do_softint);
  566. info->state = sstate;
  567. if (sstate->info) {
  568. kfree(info);
  569. *ret_info = sstate->info;
  570. return 0;
  571. }
  572. *ret_info = sstate->info = info;
  573. return 0;
  574. }
  575. static int
  576. startup(struct async_struct *info)
  577. {
  578. unsigned long flags;
  579. int retval=0;
  580. irq_handler_t handler;
  581. struct serial_state *state= info->state;
  582. unsigned long page;
  583. page = get_zeroed_page(GFP_KERNEL);
  584. if (!page)
  585. return -ENOMEM;
  586. local_irq_save(flags);
  587. if (info->flags & ASYNC_INITIALIZED) {
  588. free_page(page);
  589. goto errout;
  590. }
  591. if (!state->port || !state->type) {
  592. if (info->tty) set_bit(TTY_IO_ERROR, &info->tty->flags);
  593. free_page(page);
  594. goto errout;
  595. }
  596. if (info->xmit.buf)
  597. free_page(page);
  598. else
  599. info->xmit.buf = (unsigned char *) page;
  600. #ifdef SIMSERIAL_DEBUG
  601. printk("startup: ttys%d (irq %d)...", info->line, state->irq);
  602. #endif
  603. /*
  604. * Allocate the IRQ if necessary
  605. */
  606. if (state->irq && (!IRQ_ports[state->irq] ||
  607. !IRQ_ports[state->irq]->next_port)) {
  608. if (IRQ_ports[state->irq]) {
  609. retval = -EBUSY;
  610. goto errout;
  611. } else
  612. handler = rs_interrupt_single;
  613. retval = request_irq(state->irq, handler, IRQ_T(info), "simserial", NULL);
  614. if (retval) {
  615. if (capable(CAP_SYS_ADMIN)) {
  616. if (info->tty)
  617. set_bit(TTY_IO_ERROR,
  618. &info->tty->flags);
  619. retval = 0;
  620. }
  621. goto errout;
  622. }
  623. }
  624. /*
  625. * Insert serial port into IRQ chain.
  626. */
  627. info->prev_port = NULL;
  628. info->next_port = IRQ_ports[state->irq];
  629. if (info->next_port)
  630. info->next_port->prev_port = info;
  631. IRQ_ports[state->irq] = info;
  632. if (info->tty) clear_bit(TTY_IO_ERROR, &info->tty->flags);
  633. info->xmit.head = info->xmit.tail = 0;
  634. #if 0
  635. /*
  636. * Set up serial timers...
  637. */
  638. timer_table[RS_TIMER].expires = jiffies + 2*HZ/100;
  639. timer_active |= 1 << RS_TIMER;
  640. #endif
  641. /*
  642. * Set up the tty->alt_speed kludge
  643. */
  644. if (info->tty) {
  645. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  646. info->tty->alt_speed = 57600;
  647. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  648. info->tty->alt_speed = 115200;
  649. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
  650. info->tty->alt_speed = 230400;
  651. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
  652. info->tty->alt_speed = 460800;
  653. }
  654. info->flags |= ASYNC_INITIALIZED;
  655. local_irq_restore(flags);
  656. return 0;
  657. errout:
  658. local_irq_restore(flags);
  659. return retval;
  660. }
  661. /*
  662. * This routine is called whenever a serial port is opened. It
  663. * enables interrupts for a serial port, linking in its async structure into
  664. * the IRQ chain. It also performs the serial-specific
  665. * initialization for the tty structure.
  666. */
  667. static int rs_open(struct tty_struct *tty, struct file * filp)
  668. {
  669. struct async_struct *info;
  670. int retval, line;
  671. unsigned long page;
  672. line = tty->index;
  673. if ((line < 0) || (line >= NR_PORTS))
  674. return -ENODEV;
  675. retval = get_async_struct(line, &info);
  676. if (retval)
  677. return retval;
  678. tty->driver_data = info;
  679. info->tty = tty;
  680. #ifdef SIMSERIAL_DEBUG
  681. printk("rs_open %s, count = %d\n", tty->name, info->state->count);
  682. #endif
  683. info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  684. if (!tmp_buf) {
  685. page = get_zeroed_page(GFP_KERNEL);
  686. if (!page)
  687. return -ENOMEM;
  688. if (tmp_buf)
  689. free_page(page);
  690. else
  691. tmp_buf = (unsigned char *) page;
  692. }
  693. /*
  694. * If the port is the middle of closing, bail out now
  695. */
  696. if (tty_hung_up_p(filp) ||
  697. (info->flags & ASYNC_CLOSING)) {
  698. if (info->flags & ASYNC_CLOSING)
  699. interruptible_sleep_on(&info->close_wait);
  700. #ifdef SERIAL_DO_RESTART
  701. return ((info->flags & ASYNC_HUP_NOTIFY) ?
  702. -EAGAIN : -ERESTARTSYS);
  703. #else
  704. return -EAGAIN;
  705. #endif
  706. }
  707. /*
  708. * Start up serial port
  709. */
  710. retval = startup(info);
  711. if (retval) {
  712. return retval;
  713. }
  714. /*
  715. * figure out which console to use (should be one already)
  716. */
  717. console = console_drivers;
  718. while (console) {
  719. if ((console->flags & CON_ENABLED) && console->write) break;
  720. console = console->next;
  721. }
  722. #ifdef SIMSERIAL_DEBUG
  723. printk("rs_open ttys%d successful\n", info->line);
  724. #endif
  725. return 0;
  726. }
  727. /*
  728. * /proc fs routines....
  729. */
  730. static inline void line_info(struct seq_file *m, struct serial_state *state)
  731. {
  732. seq_printf(m, "%d: uart:%s port:%lX irq:%d\n",
  733. state->line, uart_config[state->type].name,
  734. state->port, state->irq);
  735. }
  736. static int rs_proc_show(struct seq_file *m, void *v)
  737. {
  738. int i;
  739. seq_printf(m, "simserinfo:1.0 driver:%s\n", serial_version);
  740. for (i = 0; i < NR_PORTS; i++)
  741. line_info(m, &rs_table[i]);
  742. return 0;
  743. }
  744. static int rs_proc_open(struct inode *inode, struct file *file)
  745. {
  746. return single_open(file, rs_proc_show, NULL);
  747. }
  748. static const struct file_operations rs_proc_fops = {
  749. .owner = THIS_MODULE,
  750. .open = rs_proc_open,
  751. .read = seq_read,
  752. .llseek = seq_lseek,
  753. .release = single_release,
  754. };
  755. /*
  756. * ---------------------------------------------------------------------
  757. * rs_init() and friends
  758. *
  759. * rs_init() is called at boot-time to initialize the serial driver.
  760. * ---------------------------------------------------------------------
  761. */
  762. /*
  763. * This routine prints out the appropriate serial driver version
  764. * number, and identifies which options were configured into this
  765. * driver.
  766. */
  767. static inline void show_serial_version(void)
  768. {
  769. printk(KERN_INFO "%s version %s with", serial_name, serial_version);
  770. printk(KERN_INFO " no serial options enabled\n");
  771. }
  772. static const struct tty_operations hp_ops = {
  773. .open = rs_open,
  774. .close = rs_close,
  775. .write = rs_write,
  776. .put_char = rs_put_char,
  777. .flush_chars = rs_flush_chars,
  778. .write_room = rs_write_room,
  779. .chars_in_buffer = rs_chars_in_buffer,
  780. .flush_buffer = rs_flush_buffer,
  781. .ioctl = rs_ioctl,
  782. .throttle = rs_throttle,
  783. .unthrottle = rs_unthrottle,
  784. .send_xchar = rs_send_xchar,
  785. .set_termios = rs_set_termios,
  786. .stop = rs_stop,
  787. .start = rs_start,
  788. .hangup = rs_hangup,
  789. .wait_until_sent = rs_wait_until_sent,
  790. .proc_fops = &rs_proc_fops,
  791. };
  792. /*
  793. * The serial driver boot-time initialization code!
  794. */
  795. static int __init
  796. simrs_init (void)
  797. {
  798. int i, rc;
  799. struct serial_state *state;
  800. if (!ia64_platform_is("hpsim"))
  801. return -ENODEV;
  802. hp_simserial_driver = alloc_tty_driver(1);
  803. if (!hp_simserial_driver)
  804. return -ENOMEM;
  805. show_serial_version();
  806. /* Initialize the tty_driver structure */
  807. hp_simserial_driver->owner = THIS_MODULE;
  808. hp_simserial_driver->driver_name = "simserial";
  809. hp_simserial_driver->name = "ttyS";
  810. hp_simserial_driver->major = TTY_MAJOR;
  811. hp_simserial_driver->minor_start = 64;
  812. hp_simserial_driver->type = TTY_DRIVER_TYPE_SERIAL;
  813. hp_simserial_driver->subtype = SERIAL_TYPE_NORMAL;
  814. hp_simserial_driver->init_termios = tty_std_termios;
  815. hp_simserial_driver->init_termios.c_cflag =
  816. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  817. hp_simserial_driver->flags = TTY_DRIVER_REAL_RAW;
  818. tty_set_operations(hp_simserial_driver, &hp_ops);
  819. /*
  820. * Let's have a little bit of fun !
  821. */
  822. for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) {
  823. if (state->type == PORT_UNKNOWN) continue;
  824. if (!state->irq) {
  825. if ((rc = assign_irq_vector(AUTO_ASSIGN)) < 0)
  826. panic("%s: out of interrupt vectors!\n",
  827. __func__);
  828. state->irq = rc;
  829. ia64_ssc_connect_irq(KEYBOARD_INTR, state->irq);
  830. }
  831. printk(KERN_INFO "ttyS%d at 0x%04lx (irq = %d) is a %s\n",
  832. state->line,
  833. state->port, state->irq,
  834. uart_config[state->type].name);
  835. }
  836. if (tty_register_driver(hp_simserial_driver))
  837. panic("Couldn't register simserial driver\n");
  838. return 0;
  839. }
  840. #ifndef MODULE
  841. __initcall(simrs_init);
  842. #endif