simserial.c 25 KB

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