simserial.c 13 KB

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