simserial.c 13 KB

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