simserial.c 13 KB

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