simserial.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Simulated Serial Driver (fake serial)
  4. *
  5. * This driver is mostly used for bringup purposes and will go away.
  6. * It has a strong dependency on the system console. All outputs
  7. * are rerouted to the same facility as the one used by printk which, in our
  8. * case means sys_sim.c console (goes via the simulator).
  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. #include <linux/init.h>
  15. #include <linux/errno.h>
  16. #include <linux/sched.h>
  17. #include <linux/sched/debug.h>
  18. #include <linux/tty.h>
  19. #include <linux/tty_flip.h>
  20. #include <linux/major.h>
  21. #include <linux/fcntl.h>
  22. #include <linux/mm.h>
  23. #include <linux/seq_file.h>
  24. #include <linux/slab.h>
  25. #include <linux/capability.h>
  26. #include <linux/circ_buf.h>
  27. #include <linux/console.h>
  28. #include <linux/irq.h>
  29. #include <linux/module.h>
  30. #include <linux/serial.h>
  31. #include <linux/sysrq.h>
  32. #include <linux/uaccess.h>
  33. #include <asm/hpsim.h>
  34. #include "hpsim_ssc.h"
  35. #undef SIMSERIAL_DEBUG /* define this to get some debug information */
  36. #define KEYBOARD_INTR 3 /* must match with simulator! */
  37. #define NR_PORTS 1 /* only one port for now */
  38. struct serial_state {
  39. struct tty_port port;
  40. struct circ_buf xmit;
  41. int irq;
  42. int x_char;
  43. };
  44. static struct serial_state rs_table[NR_PORTS];
  45. struct tty_driver *hp_simserial_driver;
  46. static struct console *console;
  47. static void receive_chars(struct tty_port *port)
  48. {
  49. unsigned char ch;
  50. static unsigned char seen_esc = 0;
  51. while ( (ch = ia64_ssc(0, 0, 0, 0, SSC_GETCHAR)) ) {
  52. if (ch == 27 && seen_esc == 0) {
  53. seen_esc = 1;
  54. continue;
  55. } else if (seen_esc == 1 && ch == 'O') {
  56. seen_esc = 2;
  57. continue;
  58. } else if (seen_esc == 2) {
  59. if (ch == 'P') /* F1 */
  60. show_state();
  61. #ifdef CONFIG_MAGIC_SYSRQ
  62. if (ch == 'S') { /* F4 */
  63. do {
  64. ch = ia64_ssc(0, 0, 0, 0, SSC_GETCHAR);
  65. } while (!ch);
  66. handle_sysrq(ch);
  67. }
  68. #endif
  69. seen_esc = 0;
  70. continue;
  71. }
  72. seen_esc = 0;
  73. if (tty_insert_flip_char(port, ch, TTY_NORMAL) == 0)
  74. break;
  75. }
  76. tty_flip_buffer_push(port);
  77. }
  78. /*
  79. * This is the serial driver's interrupt routine for a single port
  80. */
  81. static irqreturn_t rs_interrupt_single(int irq, void *dev_id)
  82. {
  83. struct serial_state *info = dev_id;
  84. receive_chars(&info->port);
  85. return IRQ_HANDLED;
  86. }
  87. /*
  88. * -------------------------------------------------------------------
  89. * Here ends the serial interrupt routines.
  90. * -------------------------------------------------------------------
  91. */
  92. static int rs_put_char(struct tty_struct *tty, unsigned char ch)
  93. {
  94. struct serial_state *info = tty->driver_data;
  95. unsigned long flags;
  96. if (!info->xmit.buf)
  97. return 0;
  98. local_irq_save(flags);
  99. if (CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) == 0) {
  100. local_irq_restore(flags);
  101. return 0;
  102. }
  103. info->xmit.buf[info->xmit.head] = ch;
  104. info->xmit.head = (info->xmit.head + 1) & (SERIAL_XMIT_SIZE-1);
  105. local_irq_restore(flags);
  106. return 1;
  107. }
  108. static void transmit_chars(struct tty_struct *tty, struct serial_state *info,
  109. int *intr_done)
  110. {
  111. int count;
  112. unsigned long flags;
  113. local_irq_save(flags);
  114. if (info->x_char) {
  115. char c = info->x_char;
  116. console->write(console, &c, 1);
  117. info->x_char = 0;
  118. goto out;
  119. }
  120. if (info->xmit.head == info->xmit.tail || tty->stopped) {
  121. #ifdef SIMSERIAL_DEBUG
  122. printk("transmit_chars: head=%d, tail=%d, stopped=%d\n",
  123. info->xmit.head, info->xmit.tail, tty->stopped);
  124. #endif
  125. goto out;
  126. }
  127. /*
  128. * We removed the loop and try to do it in to chunks. We need
  129. * 2 operations maximum because it's a ring buffer.
  130. *
  131. * First from current to tail if possible.
  132. * Then from the beginning of the buffer until necessary
  133. */
  134. count = min(CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE),
  135. SERIAL_XMIT_SIZE - info->xmit.tail);
  136. console->write(console, info->xmit.buf+info->xmit.tail, count);
  137. info->xmit.tail = (info->xmit.tail+count) & (SERIAL_XMIT_SIZE-1);
  138. /*
  139. * We have more at the beginning of the buffer
  140. */
  141. count = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  142. if (count) {
  143. console->write(console, info->xmit.buf, count);
  144. info->xmit.tail += count;
  145. }
  146. out:
  147. local_irq_restore(flags);
  148. }
  149. static void rs_flush_chars(struct tty_struct *tty)
  150. {
  151. struct serial_state *info = tty->driver_data;
  152. if (info->xmit.head == info->xmit.tail || tty->stopped ||
  153. !info->xmit.buf)
  154. return;
  155. transmit_chars(tty, info, NULL);
  156. }
  157. static int rs_write(struct tty_struct * tty,
  158. const unsigned char *buf, int count)
  159. {
  160. struct serial_state *info = tty->driver_data;
  161. int c, ret = 0;
  162. unsigned long flags;
  163. if (!info->xmit.buf)
  164. return 0;
  165. local_irq_save(flags);
  166. while (1) {
  167. c = CIRC_SPACE_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  168. if (count < c)
  169. c = count;
  170. if (c <= 0) {
  171. break;
  172. }
  173. memcpy(info->xmit.buf + info->xmit.head, buf, c);
  174. info->xmit.head = ((info->xmit.head + c) &
  175. (SERIAL_XMIT_SIZE-1));
  176. buf += c;
  177. count -= c;
  178. ret += c;
  179. }
  180. local_irq_restore(flags);
  181. /*
  182. * Hey, we transmit directly from here in our case
  183. */
  184. if (CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) &&
  185. !tty->stopped)
  186. transmit_chars(tty, info, NULL);
  187. return ret;
  188. }
  189. static int rs_write_room(struct tty_struct *tty)
  190. {
  191. struct serial_state *info = tty->driver_data;
  192. return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  193. }
  194. static int rs_chars_in_buffer(struct tty_struct *tty)
  195. {
  196. struct serial_state *info = tty->driver_data;
  197. return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  198. }
  199. static void rs_flush_buffer(struct tty_struct *tty)
  200. {
  201. struct serial_state *info = tty->driver_data;
  202. unsigned long flags;
  203. local_irq_save(flags);
  204. info->xmit.head = info->xmit.tail = 0;
  205. local_irq_restore(flags);
  206. tty_wakeup(tty);
  207. }
  208. /*
  209. * This function is used to send a high-priority XON/XOFF character to
  210. * the device
  211. */
  212. static void rs_send_xchar(struct tty_struct *tty, char ch)
  213. {
  214. struct serial_state *info = tty->driver_data;
  215. info->x_char = ch;
  216. if (ch) {
  217. /*
  218. * I guess we could call console->write() directly but
  219. * let's do that for now.
  220. */
  221. transmit_chars(tty, info, NULL);
  222. }
  223. }
  224. /*
  225. * ------------------------------------------------------------
  226. * rs_throttle()
  227. *
  228. * This routine is called by the upper-layer tty layer to signal that
  229. * incoming characters should be throttled.
  230. * ------------------------------------------------------------
  231. */
  232. static void rs_throttle(struct tty_struct * tty)
  233. {
  234. if (I_IXOFF(tty))
  235. rs_send_xchar(tty, STOP_CHAR(tty));
  236. printk(KERN_INFO "simrs_throttle called\n");
  237. }
  238. static void rs_unthrottle(struct tty_struct * tty)
  239. {
  240. struct serial_state *info = tty->driver_data;
  241. if (I_IXOFF(tty)) {
  242. if (info->x_char)
  243. info->x_char = 0;
  244. else
  245. rs_send_xchar(tty, START_CHAR(tty));
  246. }
  247. printk(KERN_INFO "simrs_unthrottle called\n");
  248. }
  249. static int rs_setserial(struct tty_struct *tty, struct serial_struct *ss)
  250. {
  251. return 0;
  252. }
  253. static int rs_getserial(struct tty_struct *tty, struct serial_struct *ss)
  254. {
  255. return 0;
  256. }
  257. static int rs_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
  258. {
  259. if ((cmd != TIOCSERCONFIG) && (cmd != TIOCMIWAIT)) {
  260. if (tty_io_error(tty))
  261. return -EIO;
  262. }
  263. switch (cmd) {
  264. case TIOCMIWAIT:
  265. return 0;
  266. case TIOCSERCONFIG:
  267. case TIOCSERGETLSR: /* Get line status register */
  268. return -EINVAL;
  269. }
  270. return -ENOIOCTLCMD;
  271. }
  272. #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
  273. /*
  274. * This routine will shutdown a serial port; interrupts are disabled, and
  275. * DTR is dropped if the hangup on close termio flag is on.
  276. */
  277. static void shutdown(struct tty_port *port)
  278. {
  279. struct serial_state *info = container_of(port, struct serial_state,
  280. port);
  281. unsigned long flags;
  282. local_irq_save(flags);
  283. if (info->irq)
  284. free_irq(info->irq, info);
  285. if (info->xmit.buf) {
  286. free_page((unsigned long) info->xmit.buf);
  287. info->xmit.buf = NULL;
  288. }
  289. local_irq_restore(flags);
  290. }
  291. static void rs_close(struct tty_struct *tty, struct file * filp)
  292. {
  293. struct serial_state *info = tty->driver_data;
  294. tty_port_close(&info->port, tty, filp);
  295. }
  296. static void rs_hangup(struct tty_struct *tty)
  297. {
  298. struct serial_state *info = tty->driver_data;
  299. rs_flush_buffer(tty);
  300. tty_port_hangup(&info->port);
  301. }
  302. static int activate(struct tty_port *port, struct tty_struct *tty)
  303. {
  304. struct serial_state *state = container_of(port, struct serial_state,
  305. port);
  306. unsigned long flags, page;
  307. int retval = 0;
  308. page = get_zeroed_page(GFP_KERNEL);
  309. if (!page)
  310. return -ENOMEM;
  311. local_irq_save(flags);
  312. if (state->xmit.buf)
  313. free_page(page);
  314. else
  315. state->xmit.buf = (unsigned char *) page;
  316. if (state->irq) {
  317. retval = request_irq(state->irq, rs_interrupt_single, 0,
  318. "simserial", state);
  319. if (retval)
  320. goto errout;
  321. }
  322. state->xmit.head = state->xmit.tail = 0;
  323. errout:
  324. local_irq_restore(flags);
  325. return retval;
  326. }
  327. /*
  328. * This routine is called whenever a serial port is opened. It
  329. * enables interrupts for a serial port, linking in its async structure into
  330. * the IRQ chain. It also performs the serial-specific
  331. * initialization for the tty structure.
  332. */
  333. static int rs_open(struct tty_struct *tty, struct file * filp)
  334. {
  335. struct serial_state *info = rs_table + tty->index;
  336. struct tty_port *port = &info->port;
  337. tty->driver_data = info;
  338. port->low_latency = (port->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  339. /*
  340. * figure out which console to use (should be one already)
  341. */
  342. console = console_drivers;
  343. while (console) {
  344. if ((console->flags & CON_ENABLED) && console->write) break;
  345. console = console->next;
  346. }
  347. return tty_port_open(port, tty, filp);
  348. }
  349. /*
  350. * /proc fs routines....
  351. */
  352. static int rs_proc_show(struct seq_file *m, void *v)
  353. {
  354. int i;
  355. seq_printf(m, "simserinfo:1.0\n");
  356. for (i = 0; i < NR_PORTS; i++)
  357. seq_printf(m, "%d: uart:16550 port:3F8 irq:%d\n",
  358. i, rs_table[i].irq);
  359. return 0;
  360. }
  361. static const struct tty_operations hp_ops = {
  362. .open = rs_open,
  363. .close = rs_close,
  364. .write = rs_write,
  365. .put_char = rs_put_char,
  366. .flush_chars = rs_flush_chars,
  367. .write_room = rs_write_room,
  368. .chars_in_buffer = rs_chars_in_buffer,
  369. .flush_buffer = rs_flush_buffer,
  370. .ioctl = rs_ioctl,
  371. .throttle = rs_throttle,
  372. .unthrottle = rs_unthrottle,
  373. .send_xchar = rs_send_xchar,
  374. .set_serial = rs_setserial,
  375. .get_serial = rs_getserial,
  376. .hangup = rs_hangup,
  377. .proc_show = rs_proc_show,
  378. };
  379. static const struct tty_port_operations hp_port_ops = {
  380. .activate = activate,
  381. .shutdown = shutdown,
  382. };
  383. static int __init simrs_init(void)
  384. {
  385. struct serial_state *state;
  386. int retval;
  387. if (!ia64_platform_is("hpsim"))
  388. return -ENODEV;
  389. hp_simserial_driver = alloc_tty_driver(NR_PORTS);
  390. if (!hp_simserial_driver)
  391. return -ENOMEM;
  392. printk(KERN_INFO "SimSerial driver with no serial options enabled\n");
  393. /* Initialize the tty_driver structure */
  394. hp_simserial_driver->driver_name = "simserial";
  395. hp_simserial_driver->name = "ttyS";
  396. hp_simserial_driver->major = TTY_MAJOR;
  397. hp_simserial_driver->minor_start = 64;
  398. hp_simserial_driver->type = TTY_DRIVER_TYPE_SERIAL;
  399. hp_simserial_driver->subtype = SERIAL_TYPE_NORMAL;
  400. hp_simserial_driver->init_termios = tty_std_termios;
  401. hp_simserial_driver->init_termios.c_cflag =
  402. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  403. hp_simserial_driver->flags = TTY_DRIVER_REAL_RAW;
  404. tty_set_operations(hp_simserial_driver, &hp_ops);
  405. state = rs_table;
  406. tty_port_init(&state->port);
  407. state->port.ops = &hp_port_ops;
  408. state->port.close_delay = 0; /* XXX really 0? */
  409. retval = hpsim_get_irq(KEYBOARD_INTR);
  410. if (retval < 0) {
  411. printk(KERN_ERR "%s: out of interrupt vectors!\n",
  412. __func__);
  413. goto err_free_tty;
  414. }
  415. state->irq = retval;
  416. /* the port is imaginary */
  417. printk(KERN_INFO "ttyS0 at 0x03f8 (irq = %d) is a 16550\n", state->irq);
  418. tty_port_link_device(&state->port, hp_simserial_driver, 0);
  419. retval = tty_register_driver(hp_simserial_driver);
  420. if (retval) {
  421. printk(KERN_ERR "Couldn't register simserial driver\n");
  422. goto err_free_tty;
  423. }
  424. return 0;
  425. err_free_tty:
  426. put_tty_driver(hp_simserial_driver);
  427. tty_port_destroy(&state->port);
  428. return retval;
  429. }
  430. #ifndef MODULE
  431. __initcall(simrs_init);
  432. #endif