simserial.c 25 KB

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