simserial.c 20 KB

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