simserial.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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. tty->hw_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. tty->hw_stopped || !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 && !tty->hw_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->flags & (1 << TTY_IO_ERROR))
  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. static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
  275. {
  276. /* Handle turning off CRTSCTS */
  277. if ((old_termios->c_cflag & CRTSCTS) &&
  278. !(tty->termios.c_cflag & CRTSCTS)) {
  279. tty->hw_stopped = 0;
  280. }
  281. }
  282. /*
  283. * This routine will shutdown a serial port; interrupts are disabled, and
  284. * DTR is dropped if the hangup on close termio flag is on.
  285. */
  286. static void shutdown(struct tty_port *port)
  287. {
  288. struct serial_state *info = container_of(port, struct serial_state,
  289. port);
  290. unsigned long flags;
  291. local_irq_save(flags);
  292. if (info->irq)
  293. free_irq(info->irq, info);
  294. if (info->xmit.buf) {
  295. free_page((unsigned long) info->xmit.buf);
  296. info->xmit.buf = NULL;
  297. }
  298. local_irq_restore(flags);
  299. }
  300. static void rs_close(struct tty_struct *tty, struct file * filp)
  301. {
  302. struct serial_state *info = tty->driver_data;
  303. tty_port_close(&info->port, tty, filp);
  304. }
  305. static void rs_hangup(struct tty_struct *tty)
  306. {
  307. struct serial_state *info = tty->driver_data;
  308. rs_flush_buffer(tty);
  309. tty_port_hangup(&info->port);
  310. }
  311. static int activate(struct tty_port *port, struct tty_struct *tty)
  312. {
  313. struct serial_state *state = container_of(port, struct serial_state,
  314. port);
  315. unsigned long flags, page;
  316. int retval = 0;
  317. page = get_zeroed_page(GFP_KERNEL);
  318. if (!page)
  319. return -ENOMEM;
  320. local_irq_save(flags);
  321. if (state->xmit.buf)
  322. free_page(page);
  323. else
  324. state->xmit.buf = (unsigned char *) page;
  325. if (state->irq) {
  326. retval = request_irq(state->irq, rs_interrupt_single, 0,
  327. "simserial", state);
  328. if (retval)
  329. goto errout;
  330. }
  331. state->xmit.head = state->xmit.tail = 0;
  332. /*
  333. * Set up the tty->alt_speed kludge
  334. */
  335. if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  336. tty->alt_speed = 57600;
  337. if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  338. tty->alt_speed = 115200;
  339. if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
  340. tty->alt_speed = 230400;
  341. if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
  342. tty->alt_speed = 460800;
  343. errout:
  344. local_irq_restore(flags);
  345. return retval;
  346. }
  347. /*
  348. * This routine is called whenever a serial port is opened. It
  349. * enables interrupts for a serial port, linking in its async structure into
  350. * the IRQ chain. It also performs the serial-specific
  351. * initialization for the tty structure.
  352. */
  353. static int rs_open(struct tty_struct *tty, struct file * filp)
  354. {
  355. struct serial_state *info = rs_table + tty->index;
  356. struct tty_port *port = &info->port;
  357. tty->driver_data = info;
  358. port->low_latency = (port->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  359. /*
  360. * figure out which console to use (should be one already)
  361. */
  362. console = console_drivers;
  363. while (console) {
  364. if ((console->flags & CON_ENABLED) && console->write) break;
  365. console = console->next;
  366. }
  367. return tty_port_open(port, tty, filp);
  368. }
  369. /*
  370. * /proc fs routines....
  371. */
  372. static int rs_proc_show(struct seq_file *m, void *v)
  373. {
  374. int i;
  375. seq_printf(m, "simserinfo:1.0\n");
  376. for (i = 0; i < NR_PORTS; i++)
  377. seq_printf(m, "%d: uart:16550 port:3F8 irq:%d\n",
  378. i, rs_table[i].irq);
  379. return 0;
  380. }
  381. static int rs_proc_open(struct inode *inode, struct file *file)
  382. {
  383. return single_open(file, rs_proc_show, NULL);
  384. }
  385. static const struct file_operations rs_proc_fops = {
  386. .owner = THIS_MODULE,
  387. .open = rs_proc_open,
  388. .read = seq_read,
  389. .llseek = seq_lseek,
  390. .release = single_release,
  391. };
  392. static const struct tty_operations hp_ops = {
  393. .open = rs_open,
  394. .close = rs_close,
  395. .write = rs_write,
  396. .put_char = rs_put_char,
  397. .flush_chars = rs_flush_chars,
  398. .write_room = rs_write_room,
  399. .chars_in_buffer = rs_chars_in_buffer,
  400. .flush_buffer = rs_flush_buffer,
  401. .ioctl = rs_ioctl,
  402. .throttle = rs_throttle,
  403. .unthrottle = rs_unthrottle,
  404. .send_xchar = rs_send_xchar,
  405. .set_termios = rs_set_termios,
  406. .hangup = rs_hangup,
  407. .proc_fops = &rs_proc_fops,
  408. };
  409. static const struct tty_port_operations hp_port_ops = {
  410. .activate = activate,
  411. .shutdown = shutdown,
  412. };
  413. static int __init simrs_init(void)
  414. {
  415. struct serial_state *state;
  416. int retval;
  417. if (!ia64_platform_is("hpsim"))
  418. return -ENODEV;
  419. hp_simserial_driver = alloc_tty_driver(NR_PORTS);
  420. if (!hp_simserial_driver)
  421. return -ENOMEM;
  422. printk(KERN_INFO "SimSerial driver with no serial options enabled\n");
  423. /* Initialize the tty_driver structure */
  424. hp_simserial_driver->driver_name = "simserial";
  425. hp_simserial_driver->name = "ttyS";
  426. hp_simserial_driver->major = TTY_MAJOR;
  427. hp_simserial_driver->minor_start = 64;
  428. hp_simserial_driver->type = TTY_DRIVER_TYPE_SERIAL;
  429. hp_simserial_driver->subtype = SERIAL_TYPE_NORMAL;
  430. hp_simserial_driver->init_termios = tty_std_termios;
  431. hp_simserial_driver->init_termios.c_cflag =
  432. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  433. hp_simserial_driver->flags = TTY_DRIVER_REAL_RAW;
  434. tty_set_operations(hp_simserial_driver, &hp_ops);
  435. state = rs_table;
  436. tty_port_init(&state->port);
  437. state->port.ops = &hp_port_ops;
  438. state->port.close_delay = 0; /* XXX really 0? */
  439. retval = hpsim_get_irq(KEYBOARD_INTR);
  440. if (retval < 0) {
  441. printk(KERN_ERR "%s: out of interrupt vectors!\n",
  442. __func__);
  443. goto err_free_tty;
  444. }
  445. state->irq = retval;
  446. /* the port is imaginary */
  447. printk(KERN_INFO "ttyS0 at 0x03f8 (irq = %d) is a 16550\n", state->irq);
  448. tty_port_link_device(&state->port, hp_simserial_driver, 0);
  449. retval = tty_register_driver(hp_simserial_driver);
  450. if (retval) {
  451. printk(KERN_ERR "Couldn't register simserial driver\n");
  452. goto err_free_tty;
  453. }
  454. return 0;
  455. err_free_tty:
  456. put_tty_driver(hp_simserial_driver);
  457. tty_port_destroy(&state->port);
  458. return retval;
  459. }
  460. #ifndef MODULE
  461. __initcall(simrs_init);
  462. #endif