simserial.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. /*
  2. * Simulated Serial Driver (fake serial)
  3. *
  4. * This driver is mostly used for bringup purposes and will go away.
  5. * It has a strong dependency on the system console. All outputs
  6. * are rerouted to the same facility as the one used by printk which, in our
  7. * case means sys_sim.c console (goes via the simulator). The code hereafter
  8. * is completely leveraged from the serial.c driver.
  9. *
  10. * Copyright (C) 1999-2000, 2002-2003 Hewlett-Packard Co
  11. * Stephane Eranian <eranian@hpl.hp.com>
  12. * David Mosberger-Tang <davidm@hpl.hp.com>
  13. *
  14. * 02/04/00 D. Mosberger Merged in serial.c bug fixes in rs_close().
  15. * 02/25/00 D. Mosberger Synced up with 2.3.99pre-5 version of serial.c.
  16. * 07/30/02 D. Mosberger Replace sti()/cli() with explicit spinlocks & local irq masking
  17. */
  18. #include <linux/init.h>
  19. #include <linux/errno.h>
  20. #include <linux/sched.h>
  21. #include <linux/tty.h>
  22. #include <linux/tty_flip.h>
  23. #include <linux/major.h>
  24. #include <linux/fcntl.h>
  25. #include <linux/mm.h>
  26. #include <linux/slab.h>
  27. #include <linux/capability.h>
  28. #include <linux/console.h>
  29. #include <linux/module.h>
  30. #include <linux/serial.h>
  31. #include <linux/serialP.h>
  32. #include <linux/sysrq.h>
  33. #include <asm/irq.h>
  34. #include <asm/hw_irq.h>
  35. #include <asm/uaccess.h>
  36. #undef SIMSERIAL_DEBUG /* define this to get some debug information */
  37. #define KEYBOARD_INTR 3 /* must match with simulator! */
  38. #define NR_PORTS 1 /* only one port for now */
  39. #define IRQ_T(info) ((info->flags & ASYNC_SHARE_IRQ) ? IRQF_SHARED : IRQF_DISABLED)
  40. #define SSC_GETCHAR 21
  41. extern long ia64_ssc (long, long, long, long, int);
  42. extern void ia64_ssc_connect_irq (long intr, long irq);
  43. static char *serial_name = "SimSerial driver";
  44. static char *serial_version = "0.6";
  45. /*
  46. * This has been extracted from asm/serial.h. We need one eventually but
  47. * I don't know exactly what we're going to put in it so just fake one
  48. * for now.
  49. */
  50. #define BASE_BAUD ( 1843200 / 16 )
  51. #define STD_COM_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_SKIP_TEST)
  52. /*
  53. * Most of the values here are meaningless to this particular driver.
  54. * However some values must be preserved for the code (leveraged from serial.c
  55. * to work correctly).
  56. * port must not be 0
  57. * type must not be UNKNOWN
  58. * So I picked arbitrary (guess from where?) values instead
  59. */
  60. static struct serial_state rs_table[NR_PORTS]={
  61. /* UART CLK PORT IRQ FLAGS */
  62. { 0, BASE_BAUD, 0x3F8, 0, STD_COM_FLAGS,0,PORT_16550 } /* ttyS0 */
  63. };
  64. /*
  65. * Just for the fun of it !
  66. */
  67. static struct serial_uart_config uart_config[] = {
  68. { "unknown", 1, 0 },
  69. { "8250", 1, 0 },
  70. { "16450", 1, 0 },
  71. { "16550", 1, 0 },
  72. { "16550A", 16, UART_CLEAR_FIFO | UART_USE_FIFO },
  73. { "cirrus", 1, 0 },
  74. { "ST16650", 1, UART_CLEAR_FIFO | UART_STARTECH },
  75. { "ST16650V2", 32, UART_CLEAR_FIFO | UART_USE_FIFO |
  76. UART_STARTECH },
  77. { "TI16750", 64, UART_CLEAR_FIFO | UART_USE_FIFO},
  78. { NULL, 0}
  79. };
  80. struct tty_driver *hp_simserial_driver;
  81. static struct async_struct *IRQ_ports[NR_IRQS];
  82. static struct console *console;
  83. static unsigned char *tmp_buf;
  84. extern struct console *console_drivers; /* from kernel/printk.c */
  85. /*
  86. * ------------------------------------------------------------
  87. * rs_stop() and rs_start()
  88. *
  89. * This routines are called before setting or resetting tty->stopped.
  90. * They enable or disable transmitter interrupts, as necessary.
  91. * ------------------------------------------------------------
  92. */
  93. static void rs_stop(struct tty_struct *tty)
  94. {
  95. #ifdef SIMSERIAL_DEBUG
  96. printk("rs_stop: tty->stopped=%d tty->hw_stopped=%d tty->flow_stopped=%d\n",
  97. tty->stopped, tty->hw_stopped, tty->flow_stopped);
  98. #endif
  99. }
  100. static void rs_start(struct tty_struct *tty)
  101. {
  102. #ifdef SIMSERIAL_DEBUG
  103. printk("rs_start: tty->stopped=%d tty->hw_stopped=%d tty->flow_stopped=%d\n",
  104. tty->stopped, tty->hw_stopped, tty->flow_stopped);
  105. #endif
  106. }
  107. static void receive_chars(struct tty_struct *tty)
  108. {
  109. unsigned char ch;
  110. static unsigned char seen_esc = 0;
  111. while ( (ch = ia64_ssc(0, 0, 0, 0, SSC_GETCHAR)) ) {
  112. if ( ch == 27 && seen_esc == 0 ) {
  113. seen_esc = 1;
  114. continue;
  115. } else {
  116. if ( seen_esc==1 && ch == 'O' ) {
  117. seen_esc = 2;
  118. continue;
  119. } else if ( seen_esc == 2 ) {
  120. if ( ch == 'P' ) /* F1 */
  121. show_state();
  122. #ifdef CONFIG_MAGIC_SYSRQ
  123. if ( ch == 'S' ) { /* F4 */
  124. do
  125. ch = ia64_ssc(0, 0, 0, 0,
  126. SSC_GETCHAR);
  127. while (!ch);
  128. handle_sysrq(ch, NULL);
  129. }
  130. #endif
  131. seen_esc = 0;
  132. continue;
  133. }
  134. }
  135. seen_esc = 0;
  136. if (tty_insert_flip_char(tty, ch, TTY_NORMAL) == 0)
  137. break;
  138. }
  139. tty_flip_buffer_push(tty);
  140. }
  141. /*
  142. * This is the serial driver's interrupt routine for a single port
  143. */
  144. static irqreturn_t rs_interrupt_single(int irq, void *dev_id)
  145. {
  146. struct async_struct * info;
  147. /*
  148. * I don't know exactly why they don't use the dev_id opaque data
  149. * pointer instead of this extra lookup table
  150. */
  151. info = IRQ_ports[irq];
  152. if (!info || !info->tty) {
  153. printk(KERN_INFO "simrs_interrupt_single: info|tty=0 info=%p problem\n", info);
  154. return IRQ_NONE;
  155. }
  156. /*
  157. * pretty simple in our case, because we only get interrupts
  158. * on inbound traffic
  159. */
  160. receive_chars(info->tty);
  161. return IRQ_HANDLED;
  162. }
  163. /*
  164. * -------------------------------------------------------------------
  165. * Here ends the serial interrupt routines.
  166. * -------------------------------------------------------------------
  167. */
  168. #if 0
  169. /*
  170. * not really used in our situation so keep them commented out for now
  171. */
  172. static DECLARE_TASK_QUEUE(tq_serial); /* used to be at the top of the file */
  173. static void do_serial_bh(void)
  174. {
  175. run_task_queue(&tq_serial);
  176. printk(KERN_ERR "do_serial_bh: called\n");
  177. }
  178. #endif
  179. static void do_softint(struct work_struct *private_)
  180. {
  181. printk(KERN_ERR "simserial: do_softint called\n");
  182. }
  183. static int rs_put_char(struct tty_struct *tty, unsigned char ch)
  184. {
  185. struct async_struct *info = (struct async_struct *)tty->driver_data;
  186. unsigned long flags;
  187. if (!tty || !info->xmit.buf)
  188. return 0;
  189. local_irq_save(flags);
  190. if (CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) == 0) {
  191. local_irq_restore(flags);
  192. return 0;
  193. }
  194. info->xmit.buf[info->xmit.head] = ch;
  195. info->xmit.head = (info->xmit.head + 1) & (SERIAL_XMIT_SIZE-1);
  196. local_irq_restore(flags);
  197. return 1;
  198. }
  199. static void transmit_chars(struct async_struct *info, int *intr_done)
  200. {
  201. int count;
  202. unsigned long flags;
  203. local_irq_save(flags);
  204. if (info->x_char) {
  205. char c = info->x_char;
  206. console->write(console, &c, 1);
  207. info->state->icount.tx++;
  208. info->x_char = 0;
  209. goto out;
  210. }
  211. if (info->xmit.head == info->xmit.tail || info->tty->stopped || info->tty->hw_stopped) {
  212. #ifdef SIMSERIAL_DEBUG
  213. printk("transmit_chars: head=%d, tail=%d, stopped=%d\n",
  214. info->xmit.head, info->xmit.tail, info->tty->stopped);
  215. #endif
  216. goto out;
  217. }
  218. /*
  219. * We removed the loop and try to do it in to chunks. We need
  220. * 2 operations maximum because it's a ring buffer.
  221. *
  222. * First from current to tail if possible.
  223. * Then from the beginning of the buffer until necessary
  224. */
  225. count = min(CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE),
  226. SERIAL_XMIT_SIZE - info->xmit.tail);
  227. console->write(console, info->xmit.buf+info->xmit.tail, count);
  228. info->xmit.tail = (info->xmit.tail+count) & (SERIAL_XMIT_SIZE-1);
  229. /*
  230. * We have more at the beginning of the buffer
  231. */
  232. count = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  233. if (count) {
  234. console->write(console, info->xmit.buf, count);
  235. info->xmit.tail += count;
  236. }
  237. out:
  238. local_irq_restore(flags);
  239. }
  240. static void rs_flush_chars(struct tty_struct *tty)
  241. {
  242. struct async_struct *info = (struct async_struct *)tty->driver_data;
  243. if (info->xmit.head == info->xmit.tail || tty->stopped || tty->hw_stopped ||
  244. !info->xmit.buf)
  245. return;
  246. transmit_chars(info, NULL);
  247. }
  248. static int rs_write(struct tty_struct * tty,
  249. const unsigned char *buf, int count)
  250. {
  251. int c, ret = 0;
  252. struct async_struct *info = (struct async_struct *)tty->driver_data;
  253. unsigned long flags;
  254. if (!tty || !info->xmit.buf || !tmp_buf) return 0;
  255. local_irq_save(flags);
  256. while (1) {
  257. c = CIRC_SPACE_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  258. if (count < c)
  259. c = count;
  260. if (c <= 0) {
  261. break;
  262. }
  263. memcpy(info->xmit.buf + info->xmit.head, buf, c);
  264. info->xmit.head = ((info->xmit.head + c) &
  265. (SERIAL_XMIT_SIZE-1));
  266. buf += c;
  267. count -= c;
  268. ret += c;
  269. }
  270. local_irq_restore(flags);
  271. /*
  272. * Hey, we transmit directly from here in our case
  273. */
  274. if (CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE)
  275. && !tty->stopped && !tty->hw_stopped) {
  276. transmit_chars(info, NULL);
  277. }
  278. return ret;
  279. }
  280. static int rs_write_room(struct tty_struct *tty)
  281. {
  282. struct async_struct *info = (struct async_struct *)tty->driver_data;
  283. return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  284. }
  285. static int rs_chars_in_buffer(struct tty_struct *tty)
  286. {
  287. struct async_struct *info = (struct async_struct *)tty->driver_data;
  288. return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  289. }
  290. static void rs_flush_buffer(struct tty_struct *tty)
  291. {
  292. struct async_struct *info = (struct async_struct *)tty->driver_data;
  293. unsigned long flags;
  294. local_irq_save(flags);
  295. info->xmit.head = info->xmit.tail = 0;
  296. local_irq_restore(flags);
  297. wake_up_interruptible(&tty->write_wait);
  298. if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
  299. tty->ldisc.write_wakeup)
  300. (tty->ldisc.write_wakeup)(tty);
  301. }
  302. /*
  303. * This function is used to send a high-priority XON/XOFF character to
  304. * the device
  305. */
  306. static void rs_send_xchar(struct tty_struct *tty, char ch)
  307. {
  308. struct async_struct *info = (struct async_struct *)tty->driver_data;
  309. info->x_char = ch;
  310. if (ch) {
  311. /*
  312. * I guess we could call console->write() directly but
  313. * let's do that for now.
  314. */
  315. transmit_chars(info, NULL);
  316. }
  317. }
  318. /*
  319. * ------------------------------------------------------------
  320. * rs_throttle()
  321. *
  322. * This routine is called by the upper-layer tty layer to signal that
  323. * incoming characters should be throttled.
  324. * ------------------------------------------------------------
  325. */
  326. static void rs_throttle(struct tty_struct * tty)
  327. {
  328. if (I_IXOFF(tty)) rs_send_xchar(tty, STOP_CHAR(tty));
  329. printk(KERN_INFO "simrs_throttle called\n");
  330. }
  331. static void rs_unthrottle(struct tty_struct * tty)
  332. {
  333. struct async_struct *info = (struct async_struct *)tty->driver_data;
  334. if (I_IXOFF(tty)) {
  335. if (info->x_char)
  336. info->x_char = 0;
  337. else
  338. rs_send_xchar(tty, START_CHAR(tty));
  339. }
  340. printk(KERN_INFO "simrs_unthrottle called\n");
  341. }
  342. /*
  343. * rs_break() --- routine which turns the break handling on or off
  344. */
  345. static void rs_break(struct tty_struct *tty, int break_state)
  346. {
  347. }
  348. static int rs_ioctl(struct tty_struct *tty, struct file * file,
  349. unsigned int cmd, unsigned long arg)
  350. {
  351. if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
  352. (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
  353. (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
  354. if (tty->flags & (1 << TTY_IO_ERROR))
  355. return -EIO;
  356. }
  357. switch (cmd) {
  358. case TIOCMGET:
  359. printk(KERN_INFO "rs_ioctl: TIOCMGET called\n");
  360. return -EINVAL;
  361. case TIOCMBIS:
  362. case TIOCMBIC:
  363. case TIOCMSET:
  364. printk(KERN_INFO "rs_ioctl: TIOCMBIS/BIC/SET called\n");
  365. return -EINVAL;
  366. case TIOCGSERIAL:
  367. printk(KERN_INFO "simrs_ioctl TIOCGSERIAL called\n");
  368. return 0;
  369. case TIOCSSERIAL:
  370. printk(KERN_INFO "simrs_ioctl TIOCSSERIAL called\n");
  371. return 0;
  372. case TIOCSERCONFIG:
  373. printk(KERN_INFO "rs_ioctl: TIOCSERCONFIG called\n");
  374. return -EINVAL;
  375. case TIOCSERGETLSR: /* Get line status register */
  376. printk(KERN_INFO "rs_ioctl: TIOCSERGETLSR called\n");
  377. return -EINVAL;
  378. case TIOCSERGSTRUCT:
  379. printk(KERN_INFO "rs_ioctl: TIOCSERGSTRUCT called\n");
  380. #if 0
  381. if (copy_to_user((struct async_struct *) arg,
  382. info, sizeof(struct async_struct)))
  383. return -EFAULT;
  384. #endif
  385. return 0;
  386. /*
  387. * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  388. * - mask passed in arg for lines of interest
  389. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  390. * Caller should use TIOCGICOUNT to see which one it was
  391. */
  392. case TIOCMIWAIT:
  393. printk(KERN_INFO "rs_ioctl: TIOCMIWAIT: called\n");
  394. return 0;
  395. /*
  396. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  397. * Return: write counters to the user passed counter struct
  398. * NB: both 1->0 and 0->1 transitions are counted except for
  399. * RI where only 0->1 is counted.
  400. */
  401. case TIOCGICOUNT:
  402. printk(KERN_INFO "rs_ioctl: TIOCGICOUNT called\n");
  403. return 0;
  404. case TIOCSERGWILD:
  405. case TIOCSERSWILD:
  406. /* "setserial -W" is called in Debian boot */
  407. printk (KERN_INFO "TIOCSER?WILD ioctl obsolete, ignored.\n");
  408. return 0;
  409. default:
  410. return -ENOIOCTLCMD;
  411. }
  412. return 0;
  413. }
  414. #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
  415. static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
  416. {
  417. unsigned int cflag = tty->termios->c_cflag;
  418. if ( (cflag == old_termios->c_cflag)
  419. && ( RELEVANT_IFLAG(tty->termios->c_iflag)
  420. == RELEVANT_IFLAG(old_termios->c_iflag)))
  421. return;
  422. /* Handle turning off CRTSCTS */
  423. if ((old_termios->c_cflag & CRTSCTS) &&
  424. !(tty->termios->c_cflag & CRTSCTS)) {
  425. tty->hw_stopped = 0;
  426. rs_start(tty);
  427. }
  428. }
  429. /*
  430. * This routine will shutdown a serial port; interrupts are disabled, and
  431. * DTR is dropped if the hangup on close termio flag is on.
  432. */
  433. static void shutdown(struct async_struct * info)
  434. {
  435. unsigned long flags;
  436. struct serial_state *state;
  437. int retval;
  438. if (!(info->flags & ASYNC_INITIALIZED)) return;
  439. state = info->state;
  440. #ifdef SIMSERIAL_DEBUG
  441. printk("Shutting down serial port %d (irq %d)....", info->line,
  442. state->irq);
  443. #endif
  444. local_irq_save(flags);
  445. {
  446. /*
  447. * First unlink the serial port from the IRQ chain...
  448. */
  449. if (info->next_port)
  450. info->next_port->prev_port = info->prev_port;
  451. if (info->prev_port)
  452. info->prev_port->next_port = info->next_port;
  453. else
  454. IRQ_ports[state->irq] = info->next_port;
  455. /*
  456. * Free the IRQ, if necessary
  457. */
  458. if (state->irq && (!IRQ_ports[state->irq] ||
  459. !IRQ_ports[state->irq]->next_port)) {
  460. if (IRQ_ports[state->irq]) {
  461. free_irq(state->irq, NULL);
  462. retval = request_irq(state->irq, rs_interrupt_single,
  463. IRQ_T(info), "serial", NULL);
  464. if (retval)
  465. printk(KERN_ERR "serial shutdown: request_irq: error %d"
  466. " Couldn't reacquire IRQ.\n", retval);
  467. } else
  468. free_irq(state->irq, NULL);
  469. }
  470. if (info->xmit.buf) {
  471. free_page((unsigned long) info->xmit.buf);
  472. info->xmit.buf = NULL;
  473. }
  474. if (info->tty) set_bit(TTY_IO_ERROR, &info->tty->flags);
  475. info->flags &= ~ASYNC_INITIALIZED;
  476. }
  477. local_irq_restore(flags);
  478. }
  479. /*
  480. * ------------------------------------------------------------
  481. * rs_close()
  482. *
  483. * This routine is called when the serial port gets closed. First, we
  484. * wait for the last remaining data to be sent. Then, we unlink its
  485. * async structure from the interrupt chain if necessary, and we free
  486. * that IRQ if nothing is left in the chain.
  487. * ------------------------------------------------------------
  488. */
  489. static void rs_close(struct tty_struct *tty, struct file * filp)
  490. {
  491. struct async_struct * info = (struct async_struct *)tty->driver_data;
  492. struct serial_state *state;
  493. unsigned long flags;
  494. if (!info ) return;
  495. state = info->state;
  496. local_irq_save(flags);
  497. if (tty_hung_up_p(filp)) {
  498. #ifdef SIMSERIAL_DEBUG
  499. printk("rs_close: hung_up\n");
  500. #endif
  501. local_irq_restore(flags);
  502. return;
  503. }
  504. #ifdef SIMSERIAL_DEBUG
  505. printk("rs_close ttys%d, count = %d\n", info->line, state->count);
  506. #endif
  507. if ((tty->count == 1) && (state->count != 1)) {
  508. /*
  509. * Uh, oh. tty->count is 1, which means that the tty
  510. * structure will be freed. state->count should always
  511. * be one in these conditions. If it's greater than
  512. * one, we've got real problems, since it means the
  513. * serial port won't be shutdown.
  514. */
  515. printk(KERN_ERR "rs_close: bad serial port count; tty->count is 1, "
  516. "state->count is %d\n", state->count);
  517. state->count = 1;
  518. }
  519. if (--state->count < 0) {
  520. printk(KERN_ERR "rs_close: bad serial port count for ttys%d: %d\n",
  521. info->line, state->count);
  522. state->count = 0;
  523. }
  524. if (state->count) {
  525. local_irq_restore(flags);
  526. return;
  527. }
  528. info->flags |= ASYNC_CLOSING;
  529. local_irq_restore(flags);
  530. /*
  531. * Now we wait for the transmit buffer to clear; and we notify
  532. * the line discipline to only process XON/XOFF characters.
  533. */
  534. shutdown(info);
  535. if (tty->ops->flush_buffer)
  536. tty->ops->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. __func__);
  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