simserial.c 13 KB

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