simserial.c 26 KB

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