simserial.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  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. { BASE_BAUD, 0x3F8, 0, STD_COM_FLAGS, 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 int rs_put_char(struct tty_struct *tty, unsigned char ch)
  170. {
  171. struct async_struct *info = (struct async_struct *)tty->driver_data;
  172. unsigned long flags;
  173. if (!tty || !info->xmit.buf)
  174. return 0;
  175. local_irq_save(flags);
  176. if (CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) == 0) {
  177. local_irq_restore(flags);
  178. return 0;
  179. }
  180. info->xmit.buf[info->xmit.head] = ch;
  181. info->xmit.head = (info->xmit.head + 1) & (SERIAL_XMIT_SIZE-1);
  182. local_irq_restore(flags);
  183. return 1;
  184. }
  185. static void transmit_chars(struct async_struct *info, int *intr_done)
  186. {
  187. int count;
  188. unsigned long flags;
  189. local_irq_save(flags);
  190. if (info->x_char) {
  191. char c = info->x_char;
  192. console->write(console, &c, 1);
  193. info->state->icount.tx++;
  194. info->x_char = 0;
  195. goto out;
  196. }
  197. if (info->xmit.head == info->xmit.tail || info->tty->stopped || info->tty->hw_stopped) {
  198. #ifdef SIMSERIAL_DEBUG
  199. printk("transmit_chars: head=%d, tail=%d, stopped=%d\n",
  200. info->xmit.head, info->xmit.tail, info->tty->stopped);
  201. #endif
  202. goto out;
  203. }
  204. /*
  205. * We removed the loop and try to do it in to chunks. We need
  206. * 2 operations maximum because it's a ring buffer.
  207. *
  208. * First from current to tail if possible.
  209. * Then from the beginning of the buffer until necessary
  210. */
  211. count = min(CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE),
  212. SERIAL_XMIT_SIZE - info->xmit.tail);
  213. console->write(console, info->xmit.buf+info->xmit.tail, count);
  214. info->xmit.tail = (info->xmit.tail+count) & (SERIAL_XMIT_SIZE-1);
  215. /*
  216. * We have more at the beginning of the buffer
  217. */
  218. count = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  219. if (count) {
  220. console->write(console, info->xmit.buf, count);
  221. info->xmit.tail += count;
  222. }
  223. out:
  224. local_irq_restore(flags);
  225. }
  226. static void rs_flush_chars(struct tty_struct *tty)
  227. {
  228. struct async_struct *info = (struct async_struct *)tty->driver_data;
  229. if (info->xmit.head == info->xmit.tail || tty->stopped || tty->hw_stopped ||
  230. !info->xmit.buf)
  231. return;
  232. transmit_chars(info, NULL);
  233. }
  234. static int rs_write(struct tty_struct * tty,
  235. const unsigned char *buf, int count)
  236. {
  237. int c, ret = 0;
  238. struct async_struct *info = (struct async_struct *)tty->driver_data;
  239. unsigned long flags;
  240. if (!tty || !info->xmit.buf || !tmp_buf) return 0;
  241. local_irq_save(flags);
  242. while (1) {
  243. c = CIRC_SPACE_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  244. if (count < c)
  245. c = count;
  246. if (c <= 0) {
  247. break;
  248. }
  249. memcpy(info->xmit.buf + info->xmit.head, buf, c);
  250. info->xmit.head = ((info->xmit.head + c) &
  251. (SERIAL_XMIT_SIZE-1));
  252. buf += c;
  253. count -= c;
  254. ret += c;
  255. }
  256. local_irq_restore(flags);
  257. /*
  258. * Hey, we transmit directly from here in our case
  259. */
  260. if (CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE)
  261. && !tty->stopped && !tty->hw_stopped) {
  262. transmit_chars(info, NULL);
  263. }
  264. return ret;
  265. }
  266. static int rs_write_room(struct tty_struct *tty)
  267. {
  268. struct async_struct *info = (struct async_struct *)tty->driver_data;
  269. return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  270. }
  271. static int rs_chars_in_buffer(struct tty_struct *tty)
  272. {
  273. struct async_struct *info = (struct async_struct *)tty->driver_data;
  274. return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  275. }
  276. static void rs_flush_buffer(struct tty_struct *tty)
  277. {
  278. struct async_struct *info = (struct async_struct *)tty->driver_data;
  279. unsigned long flags;
  280. local_irq_save(flags);
  281. info->xmit.head = info->xmit.tail = 0;
  282. local_irq_restore(flags);
  283. tty_wakeup(tty);
  284. }
  285. /*
  286. * This function is used to send a high-priority XON/XOFF character to
  287. * the device
  288. */
  289. static void rs_send_xchar(struct tty_struct *tty, char ch)
  290. {
  291. struct async_struct *info = (struct async_struct *)tty->driver_data;
  292. info->x_char = ch;
  293. if (ch) {
  294. /*
  295. * I guess we could call console->write() directly but
  296. * let's do that for now.
  297. */
  298. transmit_chars(info, NULL);
  299. }
  300. }
  301. /*
  302. * ------------------------------------------------------------
  303. * rs_throttle()
  304. *
  305. * This routine is called by the upper-layer tty layer to signal that
  306. * incoming characters should be throttled.
  307. * ------------------------------------------------------------
  308. */
  309. static void rs_throttle(struct tty_struct * tty)
  310. {
  311. if (I_IXOFF(tty)) rs_send_xchar(tty, STOP_CHAR(tty));
  312. printk(KERN_INFO "simrs_throttle called\n");
  313. }
  314. static void rs_unthrottle(struct tty_struct * tty)
  315. {
  316. struct async_struct *info = (struct async_struct *)tty->driver_data;
  317. if (I_IXOFF(tty)) {
  318. if (info->x_char)
  319. info->x_char = 0;
  320. else
  321. rs_send_xchar(tty, START_CHAR(tty));
  322. }
  323. printk(KERN_INFO "simrs_unthrottle called\n");
  324. }
  325. static int rs_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
  326. {
  327. if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
  328. (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
  329. (cmd != TIOCMIWAIT)) {
  330. if (tty->flags & (1 << TTY_IO_ERROR))
  331. return -EIO;
  332. }
  333. switch (cmd) {
  334. case TIOCGSERIAL:
  335. printk(KERN_INFO "simrs_ioctl TIOCGSERIAL called\n");
  336. return 0;
  337. case TIOCSSERIAL:
  338. printk(KERN_INFO "simrs_ioctl TIOCSSERIAL called\n");
  339. return 0;
  340. case TIOCSERCONFIG:
  341. printk(KERN_INFO "rs_ioctl: TIOCSERCONFIG called\n");
  342. return -EINVAL;
  343. case TIOCSERGETLSR: /* Get line status register */
  344. printk(KERN_INFO "rs_ioctl: TIOCSERGETLSR called\n");
  345. return -EINVAL;
  346. case TIOCSERGSTRUCT:
  347. printk(KERN_INFO "rs_ioctl: TIOCSERGSTRUCT called\n");
  348. #if 0
  349. if (copy_to_user((struct async_struct *) arg,
  350. info, sizeof(struct async_struct)))
  351. return -EFAULT;
  352. #endif
  353. return 0;
  354. /*
  355. * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  356. * - mask passed in arg for lines of interest
  357. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  358. * Caller should use TIOCGICOUNT to see which one it was
  359. */
  360. case TIOCMIWAIT:
  361. printk(KERN_INFO "rs_ioctl: TIOCMIWAIT: called\n");
  362. return 0;
  363. case TIOCSERGWILD:
  364. case TIOCSERSWILD:
  365. /* "setserial -W" is called in Debian boot */
  366. printk (KERN_INFO "TIOCSER?WILD ioctl obsolete, ignored.\n");
  367. return 0;
  368. default:
  369. return -ENOIOCTLCMD;
  370. }
  371. return 0;
  372. }
  373. #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
  374. static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
  375. {
  376. /* Handle turning off CRTSCTS */
  377. if ((old_termios->c_cflag & CRTSCTS) &&
  378. !(tty->termios->c_cflag & CRTSCTS)) {
  379. tty->hw_stopped = 0;
  380. rs_start(tty);
  381. }
  382. }
  383. /*
  384. * This routine will shutdown a serial port; interrupts are disabled, and
  385. * DTR is dropped if the hangup on close termio flag is on.
  386. */
  387. static void shutdown(struct async_struct * info)
  388. {
  389. unsigned long flags;
  390. struct serial_state *state;
  391. int retval;
  392. if (!(info->flags & ASYNC_INITIALIZED)) return;
  393. state = info->state;
  394. #ifdef SIMSERIAL_DEBUG
  395. printk("Shutting down serial port %d (irq %d)....", info->line,
  396. state->irq);
  397. #endif
  398. local_irq_save(flags);
  399. {
  400. /*
  401. * First unlink the serial port from the IRQ chain...
  402. */
  403. if (info->next_port)
  404. info->next_port->prev_port = info->prev_port;
  405. if (info->prev_port)
  406. info->prev_port->next_port = info->next_port;
  407. else
  408. IRQ_ports[state->irq] = info->next_port;
  409. /*
  410. * Free the IRQ, if necessary
  411. */
  412. if (state->irq && (!IRQ_ports[state->irq] ||
  413. !IRQ_ports[state->irq]->next_port)) {
  414. if (IRQ_ports[state->irq]) {
  415. free_irq(state->irq, NULL);
  416. retval = request_irq(state->irq, rs_interrupt_single,
  417. IRQ_T(info), "serial", 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. info->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. info->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. info->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. state = info->state;
  517. rs_flush_buffer(tty);
  518. if (info->flags & ASYNC_CLOSING)
  519. return;
  520. shutdown(info);
  521. state->count = 0;
  522. info->flags &= ~ASYNC_NORMAL_ACTIVE;
  523. info->tty = NULL;
  524. wake_up_interruptible(&info->open_wait);
  525. }
  526. static int get_async_struct(int line, struct async_struct **ret_info)
  527. {
  528. struct async_struct *info;
  529. struct serial_state *sstate;
  530. sstate = rs_table + line;
  531. sstate->count++;
  532. if (sstate->info) {
  533. *ret_info = sstate->info;
  534. return 0;
  535. }
  536. info = kzalloc(sizeof(struct async_struct), GFP_KERNEL);
  537. if (!info) {
  538. sstate->count--;
  539. return -ENOMEM;
  540. }
  541. init_waitqueue_head(&info->open_wait);
  542. init_waitqueue_head(&info->close_wait);
  543. info->port = sstate->port;
  544. info->flags = sstate->flags;
  545. info->xmit_fifo_size = sstate->xmit_fifo_size;
  546. info->line = line;
  547. info->state = sstate;
  548. if (sstate->info) {
  549. kfree(info);
  550. *ret_info = sstate->info;
  551. return 0;
  552. }
  553. *ret_info = sstate->info = info;
  554. return 0;
  555. }
  556. static int
  557. startup(struct async_struct *info)
  558. {
  559. unsigned long flags;
  560. int retval=0;
  561. irq_handler_t handler;
  562. struct serial_state *state= info->state;
  563. unsigned long page;
  564. page = get_zeroed_page(GFP_KERNEL);
  565. if (!page)
  566. return -ENOMEM;
  567. local_irq_save(flags);
  568. if (info->flags & ASYNC_INITIALIZED) {
  569. free_page(page);
  570. goto errout;
  571. }
  572. if (!state->port || !state->type) {
  573. if (info->tty) set_bit(TTY_IO_ERROR, &info->tty->flags);
  574. free_page(page);
  575. goto errout;
  576. }
  577. if (info->xmit.buf)
  578. free_page(page);
  579. else
  580. info->xmit.buf = (unsigned char *) page;
  581. #ifdef SIMSERIAL_DEBUG
  582. printk("startup: ttys%d (irq %d)...", info->line, state->irq);
  583. #endif
  584. /*
  585. * Allocate the IRQ if necessary
  586. */
  587. if (state->irq && (!IRQ_ports[state->irq] ||
  588. !IRQ_ports[state->irq]->next_port)) {
  589. if (IRQ_ports[state->irq]) {
  590. retval = -EBUSY;
  591. goto errout;
  592. } else
  593. handler = rs_interrupt_single;
  594. retval = request_irq(state->irq, handler, IRQ_T(info), "simserial", NULL);
  595. if (retval) {
  596. if (capable(CAP_SYS_ADMIN)) {
  597. if (info->tty)
  598. set_bit(TTY_IO_ERROR,
  599. &info->tty->flags);
  600. retval = 0;
  601. }
  602. goto errout;
  603. }
  604. }
  605. /*
  606. * Insert serial port into IRQ chain.
  607. */
  608. info->prev_port = NULL;
  609. info->next_port = IRQ_ports[state->irq];
  610. if (info->next_port)
  611. info->next_port->prev_port = info;
  612. IRQ_ports[state->irq] = info;
  613. if (info->tty) clear_bit(TTY_IO_ERROR, &info->tty->flags);
  614. info->xmit.head = info->xmit.tail = 0;
  615. #if 0
  616. /*
  617. * Set up serial timers...
  618. */
  619. timer_table[RS_TIMER].expires = jiffies + 2*HZ/100;
  620. timer_active |= 1 << RS_TIMER;
  621. #endif
  622. /*
  623. * Set up the tty->alt_speed kludge
  624. */
  625. if (info->tty) {
  626. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  627. info->tty->alt_speed = 57600;
  628. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  629. info->tty->alt_speed = 115200;
  630. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
  631. info->tty->alt_speed = 230400;
  632. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
  633. info->tty->alt_speed = 460800;
  634. }
  635. info->flags |= ASYNC_INITIALIZED;
  636. local_irq_restore(flags);
  637. return 0;
  638. errout:
  639. local_irq_restore(flags);
  640. return retval;
  641. }
  642. /*
  643. * This routine is called whenever a serial port is opened. It
  644. * enables interrupts for a serial port, linking in its async structure into
  645. * the IRQ chain. It also performs the serial-specific
  646. * initialization for the tty structure.
  647. */
  648. static int rs_open(struct tty_struct *tty, struct file * filp)
  649. {
  650. struct async_struct *info;
  651. int retval;
  652. unsigned long page;
  653. retval = get_async_struct(tty->index, &info);
  654. if (retval)
  655. return retval;
  656. tty->driver_data = info;
  657. info->tty = tty;
  658. #ifdef SIMSERIAL_DEBUG
  659. printk("rs_open %s, count = %d\n", tty->name, info->state->count);
  660. #endif
  661. info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  662. if (!tmp_buf) {
  663. page = get_zeroed_page(GFP_KERNEL);
  664. if (!page)
  665. return -ENOMEM;
  666. if (tmp_buf)
  667. free_page(page);
  668. else
  669. tmp_buf = (unsigned char *) page;
  670. }
  671. /*
  672. * If the port is the middle of closing, bail out now
  673. */
  674. if (tty_hung_up_p(filp) ||
  675. (info->flags & ASYNC_CLOSING)) {
  676. if (info->flags & ASYNC_CLOSING)
  677. interruptible_sleep_on(&info->close_wait);
  678. #ifdef SERIAL_DO_RESTART
  679. return ((info->flags & ASYNC_HUP_NOTIFY) ?
  680. -EAGAIN : -ERESTARTSYS);
  681. #else
  682. return -EAGAIN;
  683. #endif
  684. }
  685. /*
  686. * Start up serial port
  687. */
  688. retval = startup(info);
  689. if (retval) {
  690. return retval;
  691. }
  692. /*
  693. * figure out which console to use (should be one already)
  694. */
  695. console = console_drivers;
  696. while (console) {
  697. if ((console->flags & CON_ENABLED) && console->write) break;
  698. console = console->next;
  699. }
  700. #ifdef SIMSERIAL_DEBUG
  701. printk("rs_open ttys%d successful\n", info->line);
  702. #endif
  703. return 0;
  704. }
  705. /*
  706. * /proc fs routines....
  707. */
  708. static inline void line_info(struct seq_file *m, struct serial_state *state)
  709. {
  710. seq_printf(m, "%d: uart:%s port:%lX irq:%d\n",
  711. state->line, uart_config[state->type].name,
  712. state->port, state->irq);
  713. }
  714. static int rs_proc_show(struct seq_file *m, void *v)
  715. {
  716. int i;
  717. seq_printf(m, "simserinfo:1.0 driver:%s\n", serial_version);
  718. for (i = 0; i < NR_PORTS; i++)
  719. line_info(m, &rs_table[i]);
  720. return 0;
  721. }
  722. static int rs_proc_open(struct inode *inode, struct file *file)
  723. {
  724. return single_open(file, rs_proc_show, NULL);
  725. }
  726. static const struct file_operations rs_proc_fops = {
  727. .owner = THIS_MODULE,
  728. .open = rs_proc_open,
  729. .read = seq_read,
  730. .llseek = seq_lseek,
  731. .release = single_release,
  732. };
  733. /*
  734. * ---------------------------------------------------------------------
  735. * rs_init() and friends
  736. *
  737. * rs_init() is called at boot-time to initialize the serial driver.
  738. * ---------------------------------------------------------------------
  739. */
  740. /*
  741. * This routine prints out the appropriate serial driver version
  742. * number, and identifies which options were configured into this
  743. * driver.
  744. */
  745. static inline void show_serial_version(void)
  746. {
  747. printk(KERN_INFO "%s version %s with", serial_name, serial_version);
  748. printk(KERN_INFO " no serial options enabled\n");
  749. }
  750. static const struct tty_operations hp_ops = {
  751. .open = rs_open,
  752. .close = rs_close,
  753. .write = rs_write,
  754. .put_char = rs_put_char,
  755. .flush_chars = rs_flush_chars,
  756. .write_room = rs_write_room,
  757. .chars_in_buffer = rs_chars_in_buffer,
  758. .flush_buffer = rs_flush_buffer,
  759. .ioctl = rs_ioctl,
  760. .throttle = rs_throttle,
  761. .unthrottle = rs_unthrottle,
  762. .send_xchar = rs_send_xchar,
  763. .set_termios = rs_set_termios,
  764. .stop = rs_stop,
  765. .start = rs_start,
  766. .hangup = rs_hangup,
  767. .wait_until_sent = rs_wait_until_sent,
  768. .proc_fops = &rs_proc_fops,
  769. };
  770. /*
  771. * The serial driver boot-time initialization code!
  772. */
  773. static int __init
  774. simrs_init (void)
  775. {
  776. int i, rc;
  777. struct serial_state *state;
  778. if (!ia64_platform_is("hpsim"))
  779. return -ENODEV;
  780. hp_simserial_driver = alloc_tty_driver(NR_PORTS);
  781. if (!hp_simserial_driver)
  782. return -ENOMEM;
  783. show_serial_version();
  784. /* Initialize the tty_driver structure */
  785. hp_simserial_driver->driver_name = "simserial";
  786. hp_simserial_driver->name = "ttyS";
  787. hp_simserial_driver->major = TTY_MAJOR;
  788. hp_simserial_driver->minor_start = 64;
  789. hp_simserial_driver->type = TTY_DRIVER_TYPE_SERIAL;
  790. hp_simserial_driver->subtype = SERIAL_TYPE_NORMAL;
  791. hp_simserial_driver->init_termios = tty_std_termios;
  792. hp_simserial_driver->init_termios.c_cflag =
  793. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  794. hp_simserial_driver->flags = TTY_DRIVER_REAL_RAW;
  795. tty_set_operations(hp_simserial_driver, &hp_ops);
  796. /*
  797. * Let's have a little bit of fun !
  798. */
  799. for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) {
  800. if (state->type == PORT_UNKNOWN) continue;
  801. if (!state->irq) {
  802. if ((rc = assign_irq_vector(AUTO_ASSIGN)) < 0)
  803. panic("%s: out of interrupt vectors!\n",
  804. __func__);
  805. state->irq = rc;
  806. ia64_ssc_connect_irq(KEYBOARD_INTR, state->irq);
  807. }
  808. printk(KERN_INFO "ttyS%d at 0x%04lx (irq = %d) is a %s\n",
  809. state->line,
  810. state->port, state->irq,
  811. uart_config[state->type].name);
  812. }
  813. if (tty_register_driver(hp_simserial_driver))
  814. panic("Couldn't register simserial driver\n");
  815. return 0;
  816. }
  817. #ifndef MODULE
  818. __initcall(simrs_init);
  819. #endif