simserial.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  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/circ_buf.h>
  30. #include <linux/console.h>
  31. #include <linux/module.h>
  32. #include <linux/serial.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. struct serial_state {
  43. struct tty_port port;
  44. struct circ_buf xmit;
  45. int irq;
  46. int x_char;
  47. };
  48. static char *serial_name = "SimSerial driver";
  49. static char *serial_version = "0.6";
  50. static struct serial_state rs_table[NR_PORTS];
  51. struct tty_driver *hp_simserial_driver;
  52. static struct console *console;
  53. extern struct console *console_drivers; /* from kernel/printk.c */
  54. static void receive_chars(struct tty_struct *tty)
  55. {
  56. unsigned char ch;
  57. static unsigned char seen_esc = 0;
  58. while ( (ch = ia64_ssc(0, 0, 0, 0, SSC_GETCHAR)) ) {
  59. if ( ch == 27 && seen_esc == 0 ) {
  60. seen_esc = 1;
  61. continue;
  62. } else {
  63. if ( seen_esc==1 && ch == 'O' ) {
  64. seen_esc = 2;
  65. continue;
  66. } else if ( seen_esc == 2 ) {
  67. if ( ch == 'P' ) /* F1 */
  68. show_state();
  69. #ifdef CONFIG_MAGIC_SYSRQ
  70. if ( ch == 'S' ) { /* F4 */
  71. do
  72. ch = ia64_ssc(0, 0, 0, 0,
  73. SSC_GETCHAR);
  74. while (!ch);
  75. handle_sysrq(ch);
  76. }
  77. #endif
  78. seen_esc = 0;
  79. continue;
  80. }
  81. }
  82. seen_esc = 0;
  83. if (tty_insert_flip_char(tty, ch, TTY_NORMAL) == 0)
  84. break;
  85. }
  86. tty_flip_buffer_push(tty);
  87. }
  88. /*
  89. * This is the serial driver's interrupt routine for a single port
  90. */
  91. static irqreturn_t rs_interrupt_single(int irq, void *dev_id)
  92. {
  93. struct serial_state *info = dev_id;
  94. struct tty_struct *tty = tty_port_tty_get(&info->port);
  95. if (!tty) {
  96. printk(KERN_INFO "simrs_interrupt_single: info|tty=0 info=%p problem\n", info);
  97. return IRQ_NONE;
  98. }
  99. /*
  100. * pretty simple in our case, because we only get interrupts
  101. * on inbound traffic
  102. */
  103. receive_chars(tty);
  104. tty_kref_put(tty);
  105. return IRQ_HANDLED;
  106. }
  107. /*
  108. * -------------------------------------------------------------------
  109. * Here ends the serial interrupt routines.
  110. * -------------------------------------------------------------------
  111. */
  112. static int rs_put_char(struct tty_struct *tty, unsigned char ch)
  113. {
  114. struct serial_state *info = tty->driver_data;
  115. unsigned long flags;
  116. if (!tty || !info->xmit.buf)
  117. return 0;
  118. local_irq_save(flags);
  119. if (CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) == 0) {
  120. local_irq_restore(flags);
  121. return 0;
  122. }
  123. info->xmit.buf[info->xmit.head] = ch;
  124. info->xmit.head = (info->xmit.head + 1) & (SERIAL_XMIT_SIZE-1);
  125. local_irq_restore(flags);
  126. return 1;
  127. }
  128. static void transmit_chars(struct tty_struct *tty, struct serial_state *info,
  129. int *intr_done)
  130. {
  131. int count;
  132. unsigned long flags;
  133. local_irq_save(flags);
  134. if (info->x_char) {
  135. char c = info->x_char;
  136. console->write(console, &c, 1);
  137. info->x_char = 0;
  138. goto out;
  139. }
  140. if (info->xmit.head == info->xmit.tail || tty->stopped ||
  141. tty->hw_stopped) {
  142. #ifdef SIMSERIAL_DEBUG
  143. printk("transmit_chars: head=%d, tail=%d, stopped=%d\n",
  144. info->xmit.head, info->xmit.tail, tty->stopped);
  145. #endif
  146. goto out;
  147. }
  148. /*
  149. * We removed the loop and try to do it in to chunks. We need
  150. * 2 operations maximum because it's a ring buffer.
  151. *
  152. * First from current to tail if possible.
  153. * Then from the beginning of the buffer until necessary
  154. */
  155. count = min(CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE),
  156. SERIAL_XMIT_SIZE - info->xmit.tail);
  157. console->write(console, info->xmit.buf+info->xmit.tail, count);
  158. info->xmit.tail = (info->xmit.tail+count) & (SERIAL_XMIT_SIZE-1);
  159. /*
  160. * We have more at the beginning of the buffer
  161. */
  162. count = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  163. if (count) {
  164. console->write(console, info->xmit.buf, count);
  165. info->xmit.tail += count;
  166. }
  167. out:
  168. local_irq_restore(flags);
  169. }
  170. static void rs_flush_chars(struct tty_struct *tty)
  171. {
  172. struct serial_state *info = tty->driver_data;
  173. if (info->xmit.head == info->xmit.tail || tty->stopped || tty->hw_stopped ||
  174. !info->xmit.buf)
  175. return;
  176. transmit_chars(tty, info, NULL);
  177. }
  178. static int rs_write(struct tty_struct * tty,
  179. const unsigned char *buf, int count)
  180. {
  181. struct serial_state *info = tty->driver_data;
  182. int c, ret = 0;
  183. unsigned long flags;
  184. if (!tty || !info->xmit.buf)
  185. return 0;
  186. local_irq_save(flags);
  187. while (1) {
  188. c = CIRC_SPACE_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  189. if (count < c)
  190. c = count;
  191. if (c <= 0) {
  192. break;
  193. }
  194. memcpy(info->xmit.buf + info->xmit.head, buf, c);
  195. info->xmit.head = ((info->xmit.head + c) &
  196. (SERIAL_XMIT_SIZE-1));
  197. buf += c;
  198. count -= c;
  199. ret += c;
  200. }
  201. local_irq_restore(flags);
  202. /*
  203. * Hey, we transmit directly from here in our case
  204. */
  205. if (CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE)
  206. && !tty->stopped && !tty->hw_stopped) {
  207. transmit_chars(tty, info, NULL);
  208. }
  209. return ret;
  210. }
  211. static int rs_write_room(struct tty_struct *tty)
  212. {
  213. struct serial_state *info = tty->driver_data;
  214. return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  215. }
  216. static int rs_chars_in_buffer(struct tty_struct *tty)
  217. {
  218. struct serial_state *info = tty->driver_data;
  219. return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  220. }
  221. static void rs_flush_buffer(struct tty_struct *tty)
  222. {
  223. struct serial_state *info = tty->driver_data;
  224. unsigned long flags;
  225. local_irq_save(flags);
  226. info->xmit.head = info->xmit.tail = 0;
  227. local_irq_restore(flags);
  228. tty_wakeup(tty);
  229. }
  230. /*
  231. * This function is used to send a high-priority XON/XOFF character to
  232. * the device
  233. */
  234. static void rs_send_xchar(struct tty_struct *tty, char ch)
  235. {
  236. struct serial_state *info = tty->driver_data;
  237. info->x_char = ch;
  238. if (ch) {
  239. /*
  240. * I guess we could call console->write() directly but
  241. * let's do that for now.
  242. */
  243. transmit_chars(tty, info, NULL);
  244. }
  245. }
  246. /*
  247. * ------------------------------------------------------------
  248. * rs_throttle()
  249. *
  250. * This routine is called by the upper-layer tty layer to signal that
  251. * incoming characters should be throttled.
  252. * ------------------------------------------------------------
  253. */
  254. static void rs_throttle(struct tty_struct * tty)
  255. {
  256. if (I_IXOFF(tty)) rs_send_xchar(tty, STOP_CHAR(tty));
  257. printk(KERN_INFO "simrs_throttle called\n");
  258. }
  259. static void rs_unthrottle(struct tty_struct * tty)
  260. {
  261. struct serial_state *info = tty->driver_data;
  262. if (I_IXOFF(tty)) {
  263. if (info->x_char)
  264. info->x_char = 0;
  265. else
  266. rs_send_xchar(tty, START_CHAR(tty));
  267. }
  268. printk(KERN_INFO "simrs_unthrottle called\n");
  269. }
  270. static int rs_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
  271. {
  272. if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
  273. (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
  274. (cmd != TIOCMIWAIT)) {
  275. if (tty->flags & (1 << TTY_IO_ERROR))
  276. return -EIO;
  277. }
  278. switch (cmd) {
  279. case TIOCGSERIAL:
  280. printk(KERN_INFO "simrs_ioctl TIOCGSERIAL called\n");
  281. return 0;
  282. case TIOCSSERIAL:
  283. printk(KERN_INFO "simrs_ioctl TIOCSSERIAL called\n");
  284. return 0;
  285. case TIOCSERCONFIG:
  286. printk(KERN_INFO "rs_ioctl: TIOCSERCONFIG called\n");
  287. return -EINVAL;
  288. case TIOCSERGETLSR: /* Get line status register */
  289. printk(KERN_INFO "rs_ioctl: TIOCSERGETLSR called\n");
  290. return -EINVAL;
  291. case TIOCSERGSTRUCT:
  292. printk(KERN_INFO "rs_ioctl: TIOCSERGSTRUCT called\n");
  293. #if 0
  294. if (copy_to_user((struct async_struct *) arg,
  295. info, sizeof(struct async_struct)))
  296. return -EFAULT;
  297. #endif
  298. return 0;
  299. /*
  300. * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  301. * - mask passed in arg for lines of interest
  302. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  303. * Caller should use TIOCGICOUNT to see which one it was
  304. */
  305. case TIOCMIWAIT:
  306. printk(KERN_INFO "rs_ioctl: TIOCMIWAIT: called\n");
  307. return 0;
  308. case TIOCSERGWILD:
  309. case TIOCSERSWILD:
  310. /* "setserial -W" is called in Debian boot */
  311. printk (KERN_INFO "TIOCSER?WILD ioctl obsolete, ignored.\n");
  312. return 0;
  313. default:
  314. return -ENOIOCTLCMD;
  315. }
  316. return 0;
  317. }
  318. #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
  319. static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
  320. {
  321. /* Handle turning off CRTSCTS */
  322. if ((old_termios->c_cflag & CRTSCTS) &&
  323. !(tty->termios->c_cflag & CRTSCTS)) {
  324. tty->hw_stopped = 0;
  325. }
  326. }
  327. /*
  328. * This routine will shutdown a serial port; interrupts are disabled, and
  329. * DTR is dropped if the hangup on close termio flag is on.
  330. */
  331. static void shutdown(struct tty_struct *tty, struct serial_state *info)
  332. {
  333. unsigned long flags;
  334. if (!(info->port.flags & ASYNC_INITIALIZED))
  335. return;
  336. #ifdef SIMSERIAL_DEBUG
  337. printk("Shutting down serial port %d (irq %d)...\n", info->line,
  338. info->irq);
  339. #endif
  340. local_irq_save(flags);
  341. {
  342. if (info->irq)
  343. free_irq(info->irq, info);
  344. if (info->xmit.buf) {
  345. free_page((unsigned long) info->xmit.buf);
  346. info->xmit.buf = NULL;
  347. }
  348. set_bit(TTY_IO_ERROR, &tty->flags);
  349. info->port.flags &= ~ASYNC_INITIALIZED;
  350. }
  351. local_irq_restore(flags);
  352. }
  353. /*
  354. * ------------------------------------------------------------
  355. * rs_close()
  356. *
  357. * This routine is called when the serial port gets closed. First, we
  358. * wait for the last remaining data to be sent. Then, we unlink its
  359. * async structure from the interrupt chain if necessary, and we free
  360. * that IRQ if nothing is left in the chain.
  361. * ------------------------------------------------------------
  362. */
  363. static void rs_close(struct tty_struct *tty, struct file * filp)
  364. {
  365. struct serial_state *info = tty->driver_data;
  366. struct tty_port *port = &info->port;
  367. if (!info)
  368. return;
  369. if (tty_port_close_start(port, tty, filp) == 0)
  370. return;
  371. /*
  372. * Now we wait for the transmit buffer to clear; and we notify
  373. * the line discipline to only process XON/XOFF characters.
  374. */
  375. shutdown(tty, info);
  376. rs_flush_buffer(tty);
  377. tty_port_close_end(port, tty);
  378. tty_port_tty_set(port, NULL);
  379. }
  380. /*
  381. * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
  382. */
  383. static void rs_hangup(struct tty_struct *tty)
  384. {
  385. struct serial_state *info = tty->driver_data;
  386. struct tty_port *port = &info->port;
  387. #ifdef SIMSERIAL_DEBUG
  388. printk("rs_hangup: called\n");
  389. #endif
  390. rs_flush_buffer(tty);
  391. if (port->flags & ASYNC_CLOSING)
  392. return;
  393. shutdown(tty, info);
  394. port->count = 0;
  395. port->flags &= ~ASYNC_NORMAL_ACTIVE;
  396. tty_port_tty_set(port, NULL);
  397. wake_up_interruptible(&port->open_wait);
  398. }
  399. static int activate(struct tty_port *port, struct tty_struct *tty)
  400. {
  401. struct serial_state *state = container_of(port, struct serial_state,
  402. port);
  403. unsigned long flags;
  404. int retval=0;
  405. unsigned long page;
  406. page = get_zeroed_page(GFP_KERNEL);
  407. if (!page)
  408. return -ENOMEM;
  409. local_irq_save(flags);
  410. if (state->xmit.buf)
  411. free_page(page);
  412. else
  413. state->xmit.buf = (unsigned char *) page;
  414. /*
  415. * Allocate the IRQ if necessary
  416. */
  417. if (state->irq) {
  418. retval = request_irq(state->irq, rs_interrupt_single, 0,
  419. "simserial", state);
  420. if (retval)
  421. goto errout;
  422. }
  423. state->xmit.head = state->xmit.tail = 0;
  424. /*
  425. * Set up the tty->alt_speed kludge
  426. */
  427. if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  428. tty->alt_speed = 57600;
  429. if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  430. tty->alt_speed = 115200;
  431. if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
  432. tty->alt_speed = 230400;
  433. if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
  434. tty->alt_speed = 460800;
  435. errout:
  436. local_irq_restore(flags);
  437. return retval;
  438. }
  439. /*
  440. * This routine is called whenever a serial port is opened. It
  441. * enables interrupts for a serial port, linking in its async structure into
  442. * the IRQ chain. It also performs the serial-specific
  443. * initialization for the tty structure.
  444. */
  445. static int rs_open(struct tty_struct *tty, struct file * filp)
  446. {
  447. struct serial_state *info = rs_table + tty->index;
  448. struct tty_port *port = &info->port;
  449. tty->driver_data = info;
  450. tty->low_latency = (port->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  451. /*
  452. * figure out which console to use (should be one already)
  453. */
  454. console = console_drivers;
  455. while (console) {
  456. if ((console->flags & CON_ENABLED) && console->write) break;
  457. console = console->next;
  458. }
  459. return tty_port_open(port, tty, filp);
  460. }
  461. /*
  462. * /proc fs routines....
  463. */
  464. static int rs_proc_show(struct seq_file *m, void *v)
  465. {
  466. int i;
  467. seq_printf(m, "simserinfo:1.0 driver:%s\n", serial_version);
  468. for (i = 0; i < NR_PORTS; i++)
  469. seq_printf(m, "%d: uart:16550 port:3F8 irq:%d\n",
  470. i, rs_table[i].irq);
  471. return 0;
  472. }
  473. static int rs_proc_open(struct inode *inode, struct file *file)
  474. {
  475. return single_open(file, rs_proc_show, NULL);
  476. }
  477. static const struct file_operations rs_proc_fops = {
  478. .owner = THIS_MODULE,
  479. .open = rs_proc_open,
  480. .read = seq_read,
  481. .llseek = seq_lseek,
  482. .release = single_release,
  483. };
  484. /*
  485. * ---------------------------------------------------------------------
  486. * rs_init() and friends
  487. *
  488. * rs_init() is called at boot-time to initialize the serial driver.
  489. * ---------------------------------------------------------------------
  490. */
  491. /*
  492. * This routine prints out the appropriate serial driver version
  493. * number, and identifies which options were configured into this
  494. * driver.
  495. */
  496. static inline void show_serial_version(void)
  497. {
  498. printk(KERN_INFO "%s version %s with", serial_name, serial_version);
  499. printk(KERN_INFO " no serial options enabled\n");
  500. }
  501. static const struct tty_operations hp_ops = {
  502. .open = rs_open,
  503. .close = rs_close,
  504. .write = rs_write,
  505. .put_char = rs_put_char,
  506. .flush_chars = rs_flush_chars,
  507. .write_room = rs_write_room,
  508. .chars_in_buffer = rs_chars_in_buffer,
  509. .flush_buffer = rs_flush_buffer,
  510. .ioctl = rs_ioctl,
  511. .throttle = rs_throttle,
  512. .unthrottle = rs_unthrottle,
  513. .send_xchar = rs_send_xchar,
  514. .set_termios = rs_set_termios,
  515. .hangup = rs_hangup,
  516. .proc_fops = &rs_proc_fops,
  517. };
  518. static const struct tty_port_operations hp_port_ops = {
  519. .activate = activate,
  520. };
  521. /*
  522. * The serial driver boot-time initialization code!
  523. */
  524. static int __init simrs_init(void)
  525. {
  526. struct serial_state *state;
  527. int retval;
  528. if (!ia64_platform_is("hpsim"))
  529. return -ENODEV;
  530. hp_simserial_driver = alloc_tty_driver(NR_PORTS);
  531. if (!hp_simserial_driver)
  532. return -ENOMEM;
  533. show_serial_version();
  534. /* Initialize the tty_driver structure */
  535. hp_simserial_driver->driver_name = "simserial";
  536. hp_simserial_driver->name = "ttyS";
  537. hp_simserial_driver->major = TTY_MAJOR;
  538. hp_simserial_driver->minor_start = 64;
  539. hp_simserial_driver->type = TTY_DRIVER_TYPE_SERIAL;
  540. hp_simserial_driver->subtype = SERIAL_TYPE_NORMAL;
  541. hp_simserial_driver->init_termios = tty_std_termios;
  542. hp_simserial_driver->init_termios.c_cflag =
  543. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  544. hp_simserial_driver->flags = TTY_DRIVER_REAL_RAW;
  545. tty_set_operations(hp_simserial_driver, &hp_ops);
  546. /*
  547. * Let's have a little bit of fun !
  548. */
  549. state = rs_table;
  550. tty_port_init(&state->port);
  551. state->port.ops = &hp_port_ops;
  552. state->port.close_delay = 0; /* XXX really 0? */
  553. retval = hpsim_get_irq(KEYBOARD_INTR);
  554. if (retval < 0) {
  555. printk(KERN_ERR "%s: out of interrupt vectors!\n",
  556. __func__);
  557. goto err_free_tty;
  558. }
  559. state->irq = retval;
  560. /* the port is imaginary */
  561. printk(KERN_INFO "ttyS0 at 0x03f8 (irq = %d) is a 16550\n", state->irq);
  562. retval = tty_register_driver(hp_simserial_driver);
  563. if (retval) {
  564. printk(KERN_ERR "Couldn't register simserial driver\n");
  565. goto err_free_tty;
  566. }
  567. return 0;
  568. err_free_tty:
  569. put_tty_driver(hp_simserial_driver);
  570. return retval;
  571. }
  572. #ifndef MODULE
  573. __initcall(simrs_init);
  574. #endif