simserial.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  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). The code hereafter
  8. * is completely leveraged from the serial.c driver.
  9. *
  10. * Copyright (C) 1999-2000, 2002-2003 Hewlett-Packard Co
  11. * Stephane Eranian <eranian@hpl.hp.com>
  12. * David Mosberger-Tang <davidm@hpl.hp.com>
  13. *
  14. * 02/04/00 D. Mosberger Merged in serial.c bug fixes in rs_close().
  15. * 02/25/00 D. Mosberger Synced up with 2.3.99pre-5 version of serial.c.
  16. * 07/30/02 D. Mosberger Replace sti()/cli() with explicit spinlocks & local irq masking
  17. */
  18. #include <linux/init.h>
  19. #include <linux/errno.h>
  20. #include <linux/sched.h>
  21. #include <linux/tty.h>
  22. #include <linux/tty_flip.h>
  23. #include <linux/major.h>
  24. #include <linux/fcntl.h>
  25. #include <linux/mm.h>
  26. #include <linux/slab.h>
  27. #include <linux/capability.h>
  28. #include <linux/console.h>
  29. #include <linux/module.h>
  30. #include <linux/serial.h>
  31. #include <linux/serialP.h>
  32. #include <linux/sysrq.h>
  33. #include <asm/irq.h>
  34. #include <asm/hw_irq.h>
  35. #include <asm/uaccess.h>
  36. #undef SIMSERIAL_DEBUG /* define this to get some debug information */
  37. #define KEYBOARD_INTR 3 /* must match with simulator! */
  38. #define NR_PORTS 1 /* only one port for now */
  39. #define IRQ_T(info) ((info->flags & ASYNC_SHARE_IRQ) ? IRQF_SHARED : IRQF_DISABLED)
  40. #define SSC_GETCHAR 21
  41. extern long ia64_ssc (long, long, long, long, int);
  42. extern void ia64_ssc_connect_irq (long intr, long irq);
  43. static char *serial_name = "SimSerial driver";
  44. static char *serial_version = "0.6";
  45. /*
  46. * This has been extracted from asm/serial.h. We need one eventually but
  47. * I don't know exactly what we're going to put in it so just fake one
  48. * for now.
  49. */
  50. #define BASE_BAUD ( 1843200 / 16 )
  51. #define STD_COM_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_SKIP_TEST)
  52. /*
  53. * Most of the values here are meaningless to this particular driver.
  54. * However some values must be preserved for the code (leveraged from serial.c
  55. * to work correctly).
  56. * port must not be 0
  57. * type must not be UNKNOWN
  58. * So I picked arbitrary (guess from where?) values instead
  59. */
  60. static struct serial_state rs_table[NR_PORTS]={
  61. /* UART CLK PORT IRQ FLAGS */
  62. { 0, BASE_BAUD, 0x3F8, 0, STD_COM_FLAGS,0,PORT_16550 } /* ttyS0 */
  63. };
  64. /*
  65. * Just for the fun of it !
  66. */
  67. static struct serial_uart_config uart_config[] = {
  68. { "unknown", 1, 0 },
  69. { "8250", 1, 0 },
  70. { "16450", 1, 0 },
  71. { "16550", 1, 0 },
  72. { "16550A", 16, UART_CLEAR_FIFO | UART_USE_FIFO },
  73. { "cirrus", 1, 0 },
  74. { "ST16650", 1, UART_CLEAR_FIFO | UART_STARTECH },
  75. { "ST16650V2", 32, UART_CLEAR_FIFO | UART_USE_FIFO |
  76. UART_STARTECH },
  77. { "TI16750", 64, UART_CLEAR_FIFO | UART_USE_FIFO},
  78. { NULL, 0}
  79. };
  80. struct tty_driver *hp_simserial_driver;
  81. static struct async_struct *IRQ_ports[NR_IRQS];
  82. static struct console *console;
  83. static unsigned char *tmp_buf;
  84. extern struct console *console_drivers; /* from kernel/printk.c */
  85. /*
  86. * ------------------------------------------------------------
  87. * rs_stop() and rs_start()
  88. *
  89. * This routines are called before setting or resetting tty->stopped.
  90. * They enable or disable transmitter interrupts, as necessary.
  91. * ------------------------------------------------------------
  92. */
  93. static void rs_stop(struct tty_struct *tty)
  94. {
  95. #ifdef SIMSERIAL_DEBUG
  96. printk("rs_stop: tty->stopped=%d tty->hw_stopped=%d tty->flow_stopped=%d\n",
  97. tty->stopped, tty->hw_stopped, tty->flow_stopped);
  98. #endif
  99. }
  100. static void rs_start(struct tty_struct *tty)
  101. {
  102. #ifdef SIMSERIAL_DEBUG
  103. printk("rs_start: tty->stopped=%d tty->hw_stopped=%d tty->flow_stopped=%d\n",
  104. tty->stopped, tty->hw_stopped, tty->flow_stopped);
  105. #endif
  106. }
  107. static void receive_chars(struct tty_struct *tty)
  108. {
  109. unsigned char ch;
  110. static unsigned char seen_esc = 0;
  111. while ( (ch = ia64_ssc(0, 0, 0, 0, SSC_GETCHAR)) ) {
  112. if ( ch == 27 && seen_esc == 0 ) {
  113. seen_esc = 1;
  114. continue;
  115. } else {
  116. if ( seen_esc==1 && ch == 'O' ) {
  117. seen_esc = 2;
  118. continue;
  119. } else if ( seen_esc == 2 ) {
  120. if ( ch == 'P' ) /* F1 */
  121. show_state();
  122. #ifdef CONFIG_MAGIC_SYSRQ
  123. if ( ch == 'S' ) { /* F4 */
  124. do
  125. ch = ia64_ssc(0, 0, 0, 0,
  126. SSC_GETCHAR);
  127. while (!ch);
  128. handle_sysrq(ch, NULL);
  129. }
  130. #endif
  131. seen_esc = 0;
  132. continue;
  133. }
  134. }
  135. seen_esc = 0;
  136. if (tty_insert_flip_char(tty, ch, TTY_NORMAL) == 0)
  137. break;
  138. }
  139. tty_flip_buffer_push(tty);
  140. }
  141. /*
  142. * This is the serial driver's interrupt routine for a single port
  143. */
  144. static irqreturn_t rs_interrupt_single(int irq, void *dev_id)
  145. {
  146. struct async_struct * info;
  147. /*
  148. * I don't know exactly why they don't use the dev_id opaque data
  149. * pointer instead of this extra lookup table
  150. */
  151. info = IRQ_ports[irq];
  152. if (!info || !info->tty) {
  153. printk(KERN_INFO "simrs_interrupt_single: info|tty=0 info=%p problem\n", info);
  154. return IRQ_NONE;
  155. }
  156. /*
  157. * pretty simple in our case, because we only get interrupts
  158. * on inbound traffic
  159. */
  160. receive_chars(info->tty);
  161. return IRQ_HANDLED;
  162. }
  163. /*
  164. * -------------------------------------------------------------------
  165. * Here ends the serial interrupt routines.
  166. * -------------------------------------------------------------------
  167. */
  168. static void do_softint(struct work_struct *private_)
  169. {
  170. printk(KERN_ERR "simserial: do_softint called\n");
  171. }
  172. static int rs_put_char(struct tty_struct *tty, unsigned char ch)
  173. {
  174. struct async_struct *info = (struct async_struct *)tty->driver_data;
  175. unsigned long flags;
  176. if (!tty || !info->xmit.buf)
  177. return 0;
  178. local_irq_save(flags);
  179. if (CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) == 0) {
  180. local_irq_restore(flags);
  181. return 0;
  182. }
  183. info->xmit.buf[info->xmit.head] = ch;
  184. info->xmit.head = (info->xmit.head + 1) & (SERIAL_XMIT_SIZE-1);
  185. local_irq_restore(flags);
  186. return 1;
  187. }
  188. static void transmit_chars(struct async_struct *info, int *intr_done)
  189. {
  190. int count;
  191. unsigned long flags;
  192. local_irq_save(flags);
  193. if (info->x_char) {
  194. char c = info->x_char;
  195. console->write(console, &c, 1);
  196. info->state->icount.tx++;
  197. info->x_char = 0;
  198. goto out;
  199. }
  200. if (info->xmit.head == info->xmit.tail || info->tty->stopped || info->tty->hw_stopped) {
  201. #ifdef SIMSERIAL_DEBUG
  202. printk("transmit_chars: head=%d, tail=%d, stopped=%d\n",
  203. info->xmit.head, info->xmit.tail, info->tty->stopped);
  204. #endif
  205. goto out;
  206. }
  207. /*
  208. * We removed the loop and try to do it in to chunks. We need
  209. * 2 operations maximum because it's a ring buffer.
  210. *
  211. * First from current to tail if possible.
  212. * Then from the beginning of the buffer until necessary
  213. */
  214. count = min(CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE),
  215. SERIAL_XMIT_SIZE - info->xmit.tail);
  216. console->write(console, info->xmit.buf+info->xmit.tail, count);
  217. info->xmit.tail = (info->xmit.tail+count) & (SERIAL_XMIT_SIZE-1);
  218. /*
  219. * We have more at the beginning of the buffer
  220. */
  221. count = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  222. if (count) {
  223. console->write(console, info->xmit.buf, count);
  224. info->xmit.tail += count;
  225. }
  226. out:
  227. local_irq_restore(flags);
  228. }
  229. static void rs_flush_chars(struct tty_struct *tty)
  230. {
  231. struct async_struct *info = (struct async_struct *)tty->driver_data;
  232. if (info->xmit.head == info->xmit.tail || tty->stopped || tty->hw_stopped ||
  233. !info->xmit.buf)
  234. return;
  235. transmit_chars(info, NULL);
  236. }
  237. static int rs_write(struct tty_struct * tty,
  238. const unsigned char *buf, int count)
  239. {
  240. int c, ret = 0;
  241. struct async_struct *info = (struct async_struct *)tty->driver_data;
  242. unsigned long flags;
  243. if (!tty || !info->xmit.buf || !tmp_buf) return 0;
  244. local_irq_save(flags);
  245. while (1) {
  246. c = CIRC_SPACE_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  247. if (count < c)
  248. c = count;
  249. if (c <= 0) {
  250. break;
  251. }
  252. memcpy(info->xmit.buf + info->xmit.head, buf, c);
  253. info->xmit.head = ((info->xmit.head + c) &
  254. (SERIAL_XMIT_SIZE-1));
  255. buf += c;
  256. count -= c;
  257. ret += c;
  258. }
  259. local_irq_restore(flags);
  260. /*
  261. * Hey, we transmit directly from here in our case
  262. */
  263. if (CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE)
  264. && !tty->stopped && !tty->hw_stopped) {
  265. transmit_chars(info, NULL);
  266. }
  267. return ret;
  268. }
  269. static int rs_write_room(struct tty_struct *tty)
  270. {
  271. struct async_struct *info = (struct async_struct *)tty->driver_data;
  272. return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  273. }
  274. static int rs_chars_in_buffer(struct tty_struct *tty)
  275. {
  276. struct async_struct *info = (struct async_struct *)tty->driver_data;
  277. return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  278. }
  279. static void rs_flush_buffer(struct tty_struct *tty)
  280. {
  281. struct async_struct *info = (struct async_struct *)tty->driver_data;
  282. unsigned long flags;
  283. local_irq_save(flags);
  284. info->xmit.head = info->xmit.tail = 0;
  285. local_irq_restore(flags);
  286. tty_wakeup(tty);
  287. }
  288. /*
  289. * This function is used to send a high-priority XON/XOFF character to
  290. * the device
  291. */
  292. static void rs_send_xchar(struct tty_struct *tty, char ch)
  293. {
  294. struct async_struct *info = (struct async_struct *)tty->driver_data;
  295. info->x_char = ch;
  296. if (ch) {
  297. /*
  298. * I guess we could call console->write() directly but
  299. * let's do that for now.
  300. */
  301. transmit_chars(info, NULL);
  302. }
  303. }
  304. /*
  305. * ------------------------------------------------------------
  306. * rs_throttle()
  307. *
  308. * This routine is called by the upper-layer tty layer to signal that
  309. * incoming characters should be throttled.
  310. * ------------------------------------------------------------
  311. */
  312. static void rs_throttle(struct tty_struct * tty)
  313. {
  314. if (I_IXOFF(tty)) rs_send_xchar(tty, STOP_CHAR(tty));
  315. printk(KERN_INFO "simrs_throttle called\n");
  316. }
  317. static void rs_unthrottle(struct tty_struct * tty)
  318. {
  319. struct async_struct *info = (struct async_struct *)tty->driver_data;
  320. if (I_IXOFF(tty)) {
  321. if (info->x_char)
  322. info->x_char = 0;
  323. else
  324. rs_send_xchar(tty, START_CHAR(tty));
  325. }
  326. printk(KERN_INFO "simrs_unthrottle called\n");
  327. }
  328. static int rs_ioctl(struct tty_struct *tty, struct file * file,
  329. unsigned int cmd, unsigned long arg)
  330. {
  331. if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
  332. (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
  333. (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
  334. if (tty->flags & (1 << TTY_IO_ERROR))
  335. return -EIO;
  336. }
  337. switch (cmd) {
  338. case TIOCGSERIAL:
  339. printk(KERN_INFO "simrs_ioctl TIOCGSERIAL called\n");
  340. return 0;
  341. case TIOCSSERIAL:
  342. printk(KERN_INFO "simrs_ioctl TIOCSSERIAL called\n");
  343. return 0;
  344. case TIOCSERCONFIG:
  345. printk(KERN_INFO "rs_ioctl: TIOCSERCONFIG called\n");
  346. return -EINVAL;
  347. case TIOCSERGETLSR: /* Get line status register */
  348. printk(KERN_INFO "rs_ioctl: TIOCSERGETLSR called\n");
  349. return -EINVAL;
  350. case TIOCSERGSTRUCT:
  351. printk(KERN_INFO "rs_ioctl: TIOCSERGSTRUCT called\n");
  352. #if 0
  353. if (copy_to_user((struct async_struct *) arg,
  354. info, sizeof(struct async_struct)))
  355. return -EFAULT;
  356. #endif
  357. return 0;
  358. /*
  359. * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  360. * - mask passed in arg for lines of interest
  361. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  362. * Caller should use TIOCGICOUNT to see which one it was
  363. */
  364. case TIOCMIWAIT:
  365. printk(KERN_INFO "rs_ioctl: TIOCMIWAIT: called\n");
  366. return 0;
  367. /*
  368. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  369. * Return: write counters to the user passed counter struct
  370. * NB: both 1->0 and 0->1 transitions are counted except for
  371. * RI where only 0->1 is counted.
  372. */
  373. case TIOCGICOUNT:
  374. printk(KERN_INFO "rs_ioctl: TIOCGICOUNT called\n");
  375. return 0;
  376. case TIOCSERGWILD:
  377. case TIOCSERSWILD:
  378. /* "setserial -W" is called in Debian boot */
  379. printk (KERN_INFO "TIOCSER?WILD ioctl obsolete, ignored.\n");
  380. return 0;
  381. default:
  382. return -ENOIOCTLCMD;
  383. }
  384. return 0;
  385. }
  386. #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
  387. static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
  388. {
  389. /* Handle turning off CRTSCTS */
  390. if ((old_termios->c_cflag & CRTSCTS) &&
  391. !(tty->termios->c_cflag & CRTSCTS)) {
  392. tty->hw_stopped = 0;
  393. rs_start(tty);
  394. }
  395. }
  396. /*
  397. * This routine will shutdown a serial port; interrupts are disabled, and
  398. * DTR is dropped if the hangup on close termio flag is on.
  399. */
  400. static void shutdown(struct async_struct * info)
  401. {
  402. unsigned long flags;
  403. struct serial_state *state;
  404. int retval;
  405. if (!(info->flags & ASYNC_INITIALIZED)) return;
  406. state = info->state;
  407. #ifdef SIMSERIAL_DEBUG
  408. printk("Shutting down serial port %d (irq %d)....", info->line,
  409. state->irq);
  410. #endif
  411. local_irq_save(flags);
  412. {
  413. /*
  414. * First unlink the serial port from the IRQ chain...
  415. */
  416. if (info->next_port)
  417. info->next_port->prev_port = info->prev_port;
  418. if (info->prev_port)
  419. info->prev_port->next_port = info->next_port;
  420. else
  421. IRQ_ports[state->irq] = info->next_port;
  422. /*
  423. * Free the IRQ, if necessary
  424. */
  425. if (state->irq && (!IRQ_ports[state->irq] ||
  426. !IRQ_ports[state->irq]->next_port)) {
  427. if (IRQ_ports[state->irq]) {
  428. free_irq(state->irq, NULL);
  429. retval = request_irq(state->irq, rs_interrupt_single,
  430. IRQ_T(info), "serial", NULL);
  431. if (retval)
  432. printk(KERN_ERR "serial shutdown: request_irq: error %d"
  433. " Couldn't reacquire IRQ.\n", retval);
  434. } else
  435. free_irq(state->irq, NULL);
  436. }
  437. if (info->xmit.buf) {
  438. free_page((unsigned long) info->xmit.buf);
  439. info->xmit.buf = NULL;
  440. }
  441. if (info->tty) set_bit(TTY_IO_ERROR, &info->tty->flags);
  442. info->flags &= ~ASYNC_INITIALIZED;
  443. }
  444. local_irq_restore(flags);
  445. }
  446. /*
  447. * ------------------------------------------------------------
  448. * rs_close()
  449. *
  450. * This routine is called when the serial port gets closed. First, we
  451. * wait for the last remaining data to be sent. Then, we unlink its
  452. * async structure from the interrupt chain if necessary, and we free
  453. * that IRQ if nothing is left in the chain.
  454. * ------------------------------------------------------------
  455. */
  456. static void rs_close(struct tty_struct *tty, struct file * filp)
  457. {
  458. struct async_struct * info = (struct async_struct *)tty->driver_data;
  459. struct serial_state *state;
  460. unsigned long flags;
  461. if (!info ) return;
  462. state = info->state;
  463. local_irq_save(flags);
  464. if (tty_hung_up_p(filp)) {
  465. #ifdef SIMSERIAL_DEBUG
  466. printk("rs_close: hung_up\n");
  467. #endif
  468. local_irq_restore(flags);
  469. return;
  470. }
  471. #ifdef SIMSERIAL_DEBUG
  472. printk("rs_close ttys%d, count = %d\n", info->line, state->count);
  473. #endif
  474. if ((tty->count == 1) && (state->count != 1)) {
  475. /*
  476. * Uh, oh. tty->count is 1, which means that the tty
  477. * structure will be freed. state->count should always
  478. * be one in these conditions. If it's greater than
  479. * one, we've got real problems, since it means the
  480. * serial port won't be shutdown.
  481. */
  482. printk(KERN_ERR "rs_close: bad serial port count; tty->count is 1, "
  483. "state->count is %d\n", state->count);
  484. state->count = 1;
  485. }
  486. if (--state->count < 0) {
  487. printk(KERN_ERR "rs_close: bad serial port count for ttys%d: %d\n",
  488. info->line, state->count);
  489. state->count = 0;
  490. }
  491. if (state->count) {
  492. local_irq_restore(flags);
  493. return;
  494. }
  495. info->flags |= ASYNC_CLOSING;
  496. local_irq_restore(flags);
  497. /*
  498. * Now we wait for the transmit buffer to clear; and we notify
  499. * the line discipline to only process XON/XOFF characters.
  500. */
  501. shutdown(info);
  502. rs_flush_buffer(tty);
  503. tty_ldisc_flush(tty);
  504. info->event = 0;
  505. info->tty = NULL;
  506. if (info->blocked_open) {
  507. if (info->close_delay)
  508. schedule_timeout_interruptible(info->close_delay);
  509. wake_up_interruptible(&info->open_wait);
  510. }
  511. info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
  512. wake_up_interruptible(&info->close_wait);
  513. }
  514. /*
  515. * rs_wait_until_sent() --- wait until the transmitter is empty
  516. */
  517. static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
  518. {
  519. }
  520. /*
  521. * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
  522. */
  523. static void rs_hangup(struct tty_struct *tty)
  524. {
  525. struct async_struct * info = (struct async_struct *)tty->driver_data;
  526. struct serial_state *state = info->state;
  527. #ifdef SIMSERIAL_DEBUG
  528. printk("rs_hangup: called\n");
  529. #endif
  530. state = info->state;
  531. rs_flush_buffer(tty);
  532. if (info->flags & ASYNC_CLOSING)
  533. return;
  534. shutdown(info);
  535. info->event = 0;
  536. state->count = 0;
  537. info->flags &= ~ASYNC_NORMAL_ACTIVE;
  538. info->tty = NULL;
  539. wake_up_interruptible(&info->open_wait);
  540. }
  541. static int get_async_struct(int line, struct async_struct **ret_info)
  542. {
  543. struct async_struct *info;
  544. struct serial_state *sstate;
  545. sstate = rs_table + line;
  546. sstate->count++;
  547. if (sstate->info) {
  548. *ret_info = sstate->info;
  549. return 0;
  550. }
  551. info = kzalloc(sizeof(struct async_struct), GFP_KERNEL);
  552. if (!info) {
  553. sstate->count--;
  554. return -ENOMEM;
  555. }
  556. init_waitqueue_head(&info->open_wait);
  557. init_waitqueue_head(&info->close_wait);
  558. init_waitqueue_head(&info->delta_msr_wait);
  559. info->magic = SERIAL_MAGIC;
  560. info->port = sstate->port;
  561. info->flags = sstate->flags;
  562. info->xmit_fifo_size = sstate->xmit_fifo_size;
  563. info->line = line;
  564. INIT_WORK(&info->work, do_softint);
  565. info->state = sstate;
  566. if (sstate->info) {
  567. kfree(info);
  568. *ret_info = sstate->info;
  569. return 0;
  570. }
  571. *ret_info = sstate->info = info;
  572. return 0;
  573. }
  574. static int
  575. startup(struct async_struct *info)
  576. {
  577. unsigned long flags;
  578. int retval=0;
  579. irq_handler_t handler;
  580. struct serial_state *state= info->state;
  581. unsigned long page;
  582. page = get_zeroed_page(GFP_KERNEL);
  583. if (!page)
  584. return -ENOMEM;
  585. local_irq_save(flags);
  586. if (info->flags & ASYNC_INITIALIZED) {
  587. free_page(page);
  588. goto errout;
  589. }
  590. if (!state->port || !state->type) {
  591. if (info->tty) set_bit(TTY_IO_ERROR, &info->tty->flags);
  592. free_page(page);
  593. goto errout;
  594. }
  595. if (info->xmit.buf)
  596. free_page(page);
  597. else
  598. info->xmit.buf = (unsigned char *) page;
  599. #ifdef SIMSERIAL_DEBUG
  600. printk("startup: ttys%d (irq %d)...", info->line, state->irq);
  601. #endif
  602. /*
  603. * Allocate the IRQ if necessary
  604. */
  605. if (state->irq && (!IRQ_ports[state->irq] ||
  606. !IRQ_ports[state->irq]->next_port)) {
  607. if (IRQ_ports[state->irq]) {
  608. retval = -EBUSY;
  609. goto errout;
  610. } else
  611. handler = rs_interrupt_single;
  612. retval = request_irq(state->irq, handler, IRQ_T(info), "simserial", NULL);
  613. if (retval) {
  614. if (capable(CAP_SYS_ADMIN)) {
  615. if (info->tty)
  616. set_bit(TTY_IO_ERROR,
  617. &info->tty->flags);
  618. retval = 0;
  619. }
  620. goto errout;
  621. }
  622. }
  623. /*
  624. * Insert serial port into IRQ chain.
  625. */
  626. info->prev_port = NULL;
  627. info->next_port = IRQ_ports[state->irq];
  628. if (info->next_port)
  629. info->next_port->prev_port = info;
  630. IRQ_ports[state->irq] = info;
  631. if (info->tty) clear_bit(TTY_IO_ERROR, &info->tty->flags);
  632. info->xmit.head = info->xmit.tail = 0;
  633. #if 0
  634. /*
  635. * Set up serial timers...
  636. */
  637. timer_table[RS_TIMER].expires = jiffies + 2*HZ/100;
  638. timer_active |= 1 << RS_TIMER;
  639. #endif
  640. /*
  641. * Set up the tty->alt_speed kludge
  642. */
  643. if (info->tty) {
  644. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  645. info->tty->alt_speed = 57600;
  646. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  647. info->tty->alt_speed = 115200;
  648. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
  649. info->tty->alt_speed = 230400;
  650. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
  651. info->tty->alt_speed = 460800;
  652. }
  653. info->flags |= ASYNC_INITIALIZED;
  654. local_irq_restore(flags);
  655. return 0;
  656. errout:
  657. local_irq_restore(flags);
  658. return retval;
  659. }
  660. /*
  661. * This routine is called whenever a serial port is opened. It
  662. * enables interrupts for a serial port, linking in its async structure into
  663. * the IRQ chain. It also performs the serial-specific
  664. * initialization for the tty structure.
  665. */
  666. static int rs_open(struct tty_struct *tty, struct file * filp)
  667. {
  668. struct async_struct *info;
  669. int retval, line;
  670. unsigned long page;
  671. line = tty->index;
  672. if ((line < 0) || (line >= NR_PORTS))
  673. return -ENODEV;
  674. retval = get_async_struct(line, &info);
  675. if (retval)
  676. return retval;
  677. tty->driver_data = info;
  678. info->tty = tty;
  679. #ifdef SIMSERIAL_DEBUG
  680. printk("rs_open %s, count = %d\n", tty->name, info->state->count);
  681. #endif
  682. info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  683. if (!tmp_buf) {
  684. page = get_zeroed_page(GFP_KERNEL);
  685. if (!page)
  686. return -ENOMEM;
  687. if (tmp_buf)
  688. free_page(page);
  689. else
  690. tmp_buf = (unsigned char *) page;
  691. }
  692. /*
  693. * If the port is the middle of closing, bail out now
  694. */
  695. if (tty_hung_up_p(filp) ||
  696. (info->flags & ASYNC_CLOSING)) {
  697. if (info->flags & ASYNC_CLOSING)
  698. interruptible_sleep_on(&info->close_wait);
  699. #ifdef SERIAL_DO_RESTART
  700. return ((info->flags & ASYNC_HUP_NOTIFY) ?
  701. -EAGAIN : -ERESTARTSYS);
  702. #else
  703. return -EAGAIN;
  704. #endif
  705. }
  706. /*
  707. * Start up serial port
  708. */
  709. retval = startup(info);
  710. if (retval) {
  711. return retval;
  712. }
  713. /*
  714. * figure out which console to use (should be one already)
  715. */
  716. console = console_drivers;
  717. while (console) {
  718. if ((console->flags & CON_ENABLED) && console->write) break;
  719. console = console->next;
  720. }
  721. #ifdef SIMSERIAL_DEBUG
  722. printk("rs_open ttys%d successful\n", info->line);
  723. #endif
  724. return 0;
  725. }
  726. /*
  727. * /proc fs routines....
  728. */
  729. static inline int line_info(char *buf, struct serial_state *state)
  730. {
  731. return sprintf(buf, "%d: uart:%s port:%lX irq:%d\n",
  732. state->line, uart_config[state->type].name,
  733. state->port, state->irq);
  734. }
  735. static int rs_read_proc(char *page, char **start, off_t off, int count,
  736. int *eof, void *data)
  737. {
  738. int i, len = 0, l;
  739. off_t begin = 0;
  740. len += sprintf(page, "simserinfo:1.0 driver:%s\n", serial_version);
  741. for (i = 0; i < NR_PORTS && len < 4000; i++) {
  742. l = line_info(page + len, &rs_table[i]);
  743. len += l;
  744. if (len+begin > off+count)
  745. goto done;
  746. if (len+begin < off) {
  747. begin += len;
  748. len = 0;
  749. }
  750. }
  751. *eof = 1;
  752. done:
  753. if (off >= len+begin)
  754. return 0;
  755. *start = page + (begin-off);
  756. return ((count < begin+len-off) ? count : begin+len-off);
  757. }
  758. /*
  759. * ---------------------------------------------------------------------
  760. * rs_init() and friends
  761. *
  762. * rs_init() is called at boot-time to initialize the serial driver.
  763. * ---------------------------------------------------------------------
  764. */
  765. /*
  766. * This routine prints out the appropriate serial driver version
  767. * number, and identifies which options were configured into this
  768. * driver.
  769. */
  770. static inline void show_serial_version(void)
  771. {
  772. printk(KERN_INFO "%s version %s with", serial_name, serial_version);
  773. printk(KERN_INFO " no serial options enabled\n");
  774. }
  775. static const struct tty_operations hp_ops = {
  776. .open = rs_open,
  777. .close = rs_close,
  778. .write = rs_write,
  779. .put_char = rs_put_char,
  780. .flush_chars = rs_flush_chars,
  781. .write_room = rs_write_room,
  782. .chars_in_buffer = rs_chars_in_buffer,
  783. .flush_buffer = rs_flush_buffer,
  784. .ioctl = rs_ioctl,
  785. .throttle = rs_throttle,
  786. .unthrottle = rs_unthrottle,
  787. .send_xchar = rs_send_xchar,
  788. .set_termios = rs_set_termios,
  789. .stop = rs_stop,
  790. .start = rs_start,
  791. .hangup = rs_hangup,
  792. .wait_until_sent = rs_wait_until_sent,
  793. .read_proc = rs_read_proc,
  794. };
  795. /*
  796. * The serial driver boot-time initialization code!
  797. */
  798. static int __init
  799. simrs_init (void)
  800. {
  801. int i, rc;
  802. struct serial_state *state;
  803. if (!ia64_platform_is("hpsim"))
  804. return -ENODEV;
  805. hp_simserial_driver = alloc_tty_driver(1);
  806. if (!hp_simserial_driver)
  807. return -ENOMEM;
  808. show_serial_version();
  809. /* Initialize the tty_driver structure */
  810. hp_simserial_driver->owner = THIS_MODULE;
  811. hp_simserial_driver->driver_name = "simserial";
  812. hp_simserial_driver->name = "ttyS";
  813. hp_simserial_driver->major = TTY_MAJOR;
  814. hp_simserial_driver->minor_start = 64;
  815. hp_simserial_driver->type = TTY_DRIVER_TYPE_SERIAL;
  816. hp_simserial_driver->subtype = SERIAL_TYPE_NORMAL;
  817. hp_simserial_driver->init_termios = tty_std_termios;
  818. hp_simserial_driver->init_termios.c_cflag =
  819. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  820. hp_simserial_driver->flags = TTY_DRIVER_REAL_RAW;
  821. tty_set_operations(hp_simserial_driver, &hp_ops);
  822. /*
  823. * Let's have a little bit of fun !
  824. */
  825. for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) {
  826. if (state->type == PORT_UNKNOWN) continue;
  827. if (!state->irq) {
  828. if ((rc = assign_irq_vector(AUTO_ASSIGN)) < 0)
  829. panic("%s: out of interrupt vectors!\n",
  830. __func__);
  831. state->irq = rc;
  832. ia64_ssc_connect_irq(KEYBOARD_INTR, state->irq);
  833. }
  834. printk(KERN_INFO "ttyS%d at 0x%04lx (irq = %d) is a %s\n",
  835. state->line,
  836. state->port, state->irq,
  837. uart_config[state->type].name);
  838. }
  839. if (tty_register_driver(hp_simserial_driver))
  840. panic("Couldn't register simserial driver\n");
  841. return 0;
  842. }
  843. #ifndef MODULE
  844. __initcall(simrs_init);
  845. #endif