jsm_tty.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. /************************************************************************
  2. * Copyright 2003 Digi International (www.digi.com)
  3. *
  4. * Copyright (C) 2004 IBM Corporation. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2, or (at your option)
  9. * any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED; without even the
  13. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  14. * PURPOSE. See the GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 * Temple Place - Suite 330, Boston,
  19. * MA 02111-1307, USA.
  20. *
  21. * Contact Information:
  22. * Scott H Kilau <Scott_Kilau@digi.com>
  23. * Ananda Venkatarman <mansarov@us.ibm.com>
  24. * Modifications:
  25. * 01/19/06: changed jsm_input routine to use the dynamically allocated
  26. * tty_buffer changes. Contributors: Scott Kilau and Ananda V.
  27. ***********************************************************************/
  28. #include <linux/tty.h>
  29. #include <linux/tty_flip.h>
  30. #include <linux/serial_reg.h>
  31. #include <linux/delay.h> /* For udelay */
  32. #include <linux/pci.h>
  33. #include <linux/slab.h>
  34. #include "jsm.h"
  35. static DECLARE_BITMAP(linemap, MAXLINES);
  36. static void jsm_carrier(struct jsm_channel *ch);
  37. static inline int jsm_get_mstat(struct jsm_channel *ch)
  38. {
  39. unsigned char mstat;
  40. unsigned result;
  41. jsm_printk(IOCTL, INFO, &ch->ch_bd->pci_dev, "start\n");
  42. mstat = (ch->ch_mostat | ch->ch_mistat);
  43. result = 0;
  44. if (mstat & UART_MCR_DTR)
  45. result |= TIOCM_DTR;
  46. if (mstat & UART_MCR_RTS)
  47. result |= TIOCM_RTS;
  48. if (mstat & UART_MSR_CTS)
  49. result |= TIOCM_CTS;
  50. if (mstat & UART_MSR_DSR)
  51. result |= TIOCM_DSR;
  52. if (mstat & UART_MSR_RI)
  53. result |= TIOCM_RI;
  54. if (mstat & UART_MSR_DCD)
  55. result |= TIOCM_CD;
  56. jsm_printk(IOCTL, INFO, &ch->ch_bd->pci_dev, "finish\n");
  57. return result;
  58. }
  59. static unsigned int jsm_tty_tx_empty(struct uart_port *port)
  60. {
  61. return TIOCSER_TEMT;
  62. }
  63. /*
  64. * Return modem signals to ld.
  65. */
  66. static unsigned int jsm_tty_get_mctrl(struct uart_port *port)
  67. {
  68. int result;
  69. struct jsm_channel *channel = (struct jsm_channel *)port;
  70. jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "start\n");
  71. result = jsm_get_mstat(channel);
  72. if (result < 0)
  73. return -ENXIO;
  74. jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "finish\n");
  75. return result;
  76. }
  77. /*
  78. * jsm_set_modem_info()
  79. *
  80. * Set modem signals, called by ld.
  81. */
  82. static void jsm_tty_set_mctrl(struct uart_port *port, unsigned int mctrl)
  83. {
  84. struct jsm_channel *channel = (struct jsm_channel *)port;
  85. jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "start\n");
  86. if (mctrl & TIOCM_RTS)
  87. channel->ch_mostat |= UART_MCR_RTS;
  88. else
  89. channel->ch_mostat &= ~UART_MCR_RTS;
  90. if (mctrl & TIOCM_DTR)
  91. channel->ch_mostat |= UART_MCR_DTR;
  92. else
  93. channel->ch_mostat &= ~UART_MCR_DTR;
  94. channel->ch_bd->bd_ops->assert_modem_signals(channel);
  95. jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "finish\n");
  96. udelay(10);
  97. }
  98. static void jsm_tty_start_tx(struct uart_port *port)
  99. {
  100. struct jsm_channel *channel = (struct jsm_channel *)port;
  101. jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "start\n");
  102. channel->ch_flags &= ~(CH_STOP);
  103. jsm_tty_write(port);
  104. jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "finish\n");
  105. }
  106. static void jsm_tty_stop_tx(struct uart_port *port)
  107. {
  108. struct jsm_channel *channel = (struct jsm_channel *)port;
  109. jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "start\n");
  110. channel->ch_flags |= (CH_STOP);
  111. jsm_printk(IOCTL, INFO, &channel->ch_bd->pci_dev, "finish\n");
  112. }
  113. static void jsm_tty_send_xchar(struct uart_port *port, char ch)
  114. {
  115. unsigned long lock_flags;
  116. struct jsm_channel *channel = (struct jsm_channel *)port;
  117. struct ktermios *termios;
  118. spin_lock_irqsave(&port->lock, lock_flags);
  119. termios = port->state->port.tty->termios;
  120. if (ch == termios->c_cc[VSTART])
  121. channel->ch_bd->bd_ops->send_start_character(channel);
  122. if (ch == termios->c_cc[VSTOP])
  123. channel->ch_bd->bd_ops->send_stop_character(channel);
  124. spin_unlock_irqrestore(&port->lock, lock_flags);
  125. }
  126. static void jsm_tty_stop_rx(struct uart_port *port)
  127. {
  128. struct jsm_channel *channel = (struct jsm_channel *)port;
  129. channel->ch_bd->bd_ops->disable_receiver(channel);
  130. }
  131. static void jsm_tty_enable_ms(struct uart_port *port)
  132. {
  133. /* Nothing needed */
  134. }
  135. static void jsm_tty_break(struct uart_port *port, int break_state)
  136. {
  137. unsigned long lock_flags;
  138. struct jsm_channel *channel = (struct jsm_channel *)port;
  139. spin_lock_irqsave(&port->lock, lock_flags);
  140. if (break_state == -1)
  141. channel->ch_bd->bd_ops->send_break(channel);
  142. else
  143. channel->ch_bd->bd_ops->clear_break(channel, 0);
  144. spin_unlock_irqrestore(&port->lock, lock_flags);
  145. }
  146. static int jsm_tty_open(struct uart_port *port)
  147. {
  148. struct jsm_board *brd;
  149. struct jsm_channel *channel = (struct jsm_channel *)port;
  150. struct ktermios *termios;
  151. /* Get board pointer from our array of majors we have allocated */
  152. brd = channel->ch_bd;
  153. /*
  154. * Allocate channel buffers for read/write/error.
  155. * Set flag, so we don't get trounced on.
  156. */
  157. channel->ch_flags |= (CH_OPENING);
  158. /* Drop locks, as malloc with GFP_KERNEL can sleep */
  159. if (!channel->ch_rqueue) {
  160. channel->ch_rqueue = kzalloc(RQUEUESIZE, GFP_KERNEL);
  161. if (!channel->ch_rqueue) {
  162. jsm_printk(INIT, ERR, &channel->ch_bd->pci_dev,
  163. "unable to allocate read queue buf");
  164. return -ENOMEM;
  165. }
  166. }
  167. if (!channel->ch_equeue) {
  168. channel->ch_equeue = kzalloc(EQUEUESIZE, GFP_KERNEL);
  169. if (!channel->ch_equeue) {
  170. jsm_printk(INIT, ERR, &channel->ch_bd->pci_dev,
  171. "unable to allocate error queue buf");
  172. return -ENOMEM;
  173. }
  174. }
  175. if (!channel->ch_wqueue) {
  176. channel->ch_wqueue = kzalloc(WQUEUESIZE, GFP_KERNEL);
  177. if (!channel->ch_wqueue) {
  178. jsm_printk(INIT, ERR, &channel->ch_bd->pci_dev,
  179. "unable to allocate write queue buf");
  180. return -ENOMEM;
  181. }
  182. }
  183. channel->ch_flags &= ~(CH_OPENING);
  184. /*
  185. * Initialize if neither terminal is open.
  186. */
  187. jsm_printk(OPEN, INFO, &channel->ch_bd->pci_dev,
  188. "jsm_open: initializing channel in open...\n");
  189. /*
  190. * Flush input queues.
  191. */
  192. channel->ch_r_head = channel->ch_r_tail = 0;
  193. channel->ch_e_head = channel->ch_e_tail = 0;
  194. channel->ch_w_head = channel->ch_w_tail = 0;
  195. brd->bd_ops->flush_uart_write(channel);
  196. brd->bd_ops->flush_uart_read(channel);
  197. channel->ch_flags = 0;
  198. channel->ch_cached_lsr = 0;
  199. channel->ch_stops_sent = 0;
  200. termios = port->state->port.tty->termios;
  201. channel->ch_c_cflag = termios->c_cflag;
  202. channel->ch_c_iflag = termios->c_iflag;
  203. channel->ch_c_oflag = termios->c_oflag;
  204. channel->ch_c_lflag = termios->c_lflag;
  205. channel->ch_startc = termios->c_cc[VSTART];
  206. channel->ch_stopc = termios->c_cc[VSTOP];
  207. /* Tell UART to init itself */
  208. brd->bd_ops->uart_init(channel);
  209. /*
  210. * Run param in case we changed anything
  211. */
  212. brd->bd_ops->param(channel);
  213. jsm_carrier(channel);
  214. channel->ch_open_count++;
  215. jsm_printk(OPEN, INFO, &channel->ch_bd->pci_dev, "finish\n");
  216. return 0;
  217. }
  218. static void jsm_tty_close(struct uart_port *port)
  219. {
  220. struct jsm_board *bd;
  221. struct ktermios *ts;
  222. struct jsm_channel *channel = (struct jsm_channel *)port;
  223. jsm_printk(CLOSE, INFO, &channel->ch_bd->pci_dev, "start\n");
  224. bd = channel->ch_bd;
  225. ts = port->state->port.tty->termios;
  226. channel->ch_flags &= ~(CH_STOPI);
  227. channel->ch_open_count--;
  228. /*
  229. * If we have HUPCL set, lower DTR and RTS
  230. */
  231. if (channel->ch_c_cflag & HUPCL) {
  232. jsm_printk(CLOSE, INFO, &channel->ch_bd->pci_dev,
  233. "Close. HUPCL set, dropping DTR/RTS\n");
  234. /* Drop RTS/DTR */
  235. channel->ch_mostat &= ~(UART_MCR_DTR | UART_MCR_RTS);
  236. bd->bd_ops->assert_modem_signals(channel);
  237. }
  238. /* Turn off UART interrupts for this port */
  239. channel->ch_bd->bd_ops->uart_off(channel);
  240. jsm_printk(CLOSE, INFO, &channel->ch_bd->pci_dev, "finish\n");
  241. }
  242. static void jsm_tty_set_termios(struct uart_port *port,
  243. struct ktermios *termios,
  244. struct ktermios *old_termios)
  245. {
  246. unsigned long lock_flags;
  247. struct jsm_channel *channel = (struct jsm_channel *)port;
  248. spin_lock_irqsave(&port->lock, lock_flags);
  249. channel->ch_c_cflag = termios->c_cflag;
  250. channel->ch_c_iflag = termios->c_iflag;
  251. channel->ch_c_oflag = termios->c_oflag;
  252. channel->ch_c_lflag = termios->c_lflag;
  253. channel->ch_startc = termios->c_cc[VSTART];
  254. channel->ch_stopc = termios->c_cc[VSTOP];
  255. channel->ch_bd->bd_ops->param(channel);
  256. jsm_carrier(channel);
  257. spin_unlock_irqrestore(&port->lock, lock_flags);
  258. }
  259. static const char *jsm_tty_type(struct uart_port *port)
  260. {
  261. return "jsm";
  262. }
  263. static void jsm_tty_release_port(struct uart_port *port)
  264. {
  265. }
  266. static int jsm_tty_request_port(struct uart_port *port)
  267. {
  268. return 0;
  269. }
  270. static void jsm_config_port(struct uart_port *port, int flags)
  271. {
  272. port->type = PORT_JSM;
  273. }
  274. static struct uart_ops jsm_ops = {
  275. .tx_empty = jsm_tty_tx_empty,
  276. .set_mctrl = jsm_tty_set_mctrl,
  277. .get_mctrl = jsm_tty_get_mctrl,
  278. .stop_tx = jsm_tty_stop_tx,
  279. .start_tx = jsm_tty_start_tx,
  280. .send_xchar = jsm_tty_send_xchar,
  281. .stop_rx = jsm_tty_stop_rx,
  282. .enable_ms = jsm_tty_enable_ms,
  283. .break_ctl = jsm_tty_break,
  284. .startup = jsm_tty_open,
  285. .shutdown = jsm_tty_close,
  286. .set_termios = jsm_tty_set_termios,
  287. .type = jsm_tty_type,
  288. .release_port = jsm_tty_release_port,
  289. .request_port = jsm_tty_request_port,
  290. .config_port = jsm_config_port,
  291. };
  292. /*
  293. * jsm_tty_init()
  294. *
  295. * Init the tty subsystem. Called once per board after board has been
  296. * downloaded and init'ed.
  297. */
  298. int __devinit jsm_tty_init(struct jsm_board *brd)
  299. {
  300. int i;
  301. void __iomem *vaddr;
  302. struct jsm_channel *ch;
  303. if (!brd)
  304. return -ENXIO;
  305. jsm_printk(INIT, INFO, &brd->pci_dev, "start\n");
  306. /*
  307. * Initialize board structure elements.
  308. */
  309. brd->nasync = brd->maxports;
  310. /*
  311. * Allocate channel memory that might not have been allocated
  312. * when the driver was first loaded.
  313. */
  314. for (i = 0; i < brd->nasync; i++) {
  315. if (!brd->channels[i]) {
  316. /*
  317. * Okay to malloc with GFP_KERNEL, we are not at
  318. * interrupt context, and there are no locks held.
  319. */
  320. brd->channels[i] = kzalloc(sizeof(struct jsm_channel), GFP_KERNEL);
  321. if (!brd->channels[i]) {
  322. jsm_printk(CORE, ERR, &brd->pci_dev,
  323. "%s:%d Unable to allocate memory for channel struct\n",
  324. __FILE__, __LINE__);
  325. }
  326. }
  327. }
  328. ch = brd->channels[0];
  329. vaddr = brd->re_map_membase;
  330. /* Set up channel variables */
  331. for (i = 0; i < brd->nasync; i++, ch = brd->channels[i]) {
  332. if (!brd->channels[i])
  333. continue;
  334. spin_lock_init(&ch->ch_lock);
  335. if (brd->bd_uart_offset == 0x200)
  336. ch->ch_neo_uart = vaddr + (brd->bd_uart_offset * i);
  337. ch->ch_bd = brd;
  338. ch->ch_portnum = i;
  339. /* .25 second delay */
  340. ch->ch_close_delay = 250;
  341. init_waitqueue_head(&ch->ch_flags_wait);
  342. }
  343. jsm_printk(INIT, INFO, &brd->pci_dev, "finish\n");
  344. return 0;
  345. }
  346. int jsm_uart_port_init(struct jsm_board *brd)
  347. {
  348. int i, rc;
  349. unsigned int line;
  350. struct jsm_channel *ch;
  351. if (!brd)
  352. return -ENXIO;
  353. jsm_printk(INIT, INFO, &brd->pci_dev, "start\n");
  354. /*
  355. * Initialize board structure elements.
  356. */
  357. brd->nasync = brd->maxports;
  358. /* Set up channel variables */
  359. for (i = 0; i < brd->nasync; i++, ch = brd->channels[i]) {
  360. if (!brd->channels[i])
  361. continue;
  362. brd->channels[i]->uart_port.irq = brd->irq;
  363. brd->channels[i]->uart_port.uartclk = 14745600;
  364. brd->channels[i]->uart_port.type = PORT_JSM;
  365. brd->channels[i]->uart_port.iotype = UPIO_MEM;
  366. brd->channels[i]->uart_port.membase = brd->re_map_membase;
  367. brd->channels[i]->uart_port.fifosize = 16;
  368. brd->channels[i]->uart_port.ops = &jsm_ops;
  369. line = find_first_zero_bit(linemap, MAXLINES);
  370. if (line >= MAXLINES) {
  371. printk(KERN_INFO "jsm: linemap is full, added device failed\n");
  372. continue;
  373. } else
  374. set_bit(line, linemap);
  375. brd->channels[i]->uart_port.line = line;
  376. rc = uart_add_one_port (&jsm_uart_driver, &brd->channels[i]->uart_port);
  377. if (rc){
  378. printk(KERN_INFO "jsm: Port %d failed. Aborting...\n", i);
  379. return rc;
  380. }
  381. else
  382. printk(KERN_INFO "jsm: Port %d added\n", i);
  383. }
  384. jsm_printk(INIT, INFO, &brd->pci_dev, "finish\n");
  385. return 0;
  386. }
  387. int jsm_remove_uart_port(struct jsm_board *brd)
  388. {
  389. int i;
  390. struct jsm_channel *ch;
  391. if (!brd)
  392. return -ENXIO;
  393. jsm_printk(INIT, INFO, &brd->pci_dev, "start\n");
  394. /*
  395. * Initialize board structure elements.
  396. */
  397. brd->nasync = brd->maxports;
  398. /* Set up channel variables */
  399. for (i = 0; i < brd->nasync; i++) {
  400. if (!brd->channels[i])
  401. continue;
  402. ch = brd->channels[i];
  403. clear_bit(ch->uart_port.line, linemap);
  404. uart_remove_one_port(&jsm_uart_driver, &brd->channels[i]->uart_port);
  405. }
  406. jsm_printk(INIT, INFO, &brd->pci_dev, "finish\n");
  407. return 0;
  408. }
  409. void jsm_input(struct jsm_channel *ch)
  410. {
  411. struct jsm_board *bd;
  412. struct tty_struct *tp;
  413. u32 rmask;
  414. u16 head;
  415. u16 tail;
  416. int data_len;
  417. unsigned long lock_flags;
  418. int len = 0;
  419. int n = 0;
  420. int s = 0;
  421. int i = 0;
  422. jsm_printk(READ, INFO, &ch->ch_bd->pci_dev, "start\n");
  423. if (!ch)
  424. return;
  425. tp = ch->uart_port.state->port.tty;
  426. bd = ch->ch_bd;
  427. if(!bd)
  428. return;
  429. spin_lock_irqsave(&ch->ch_lock, lock_flags);
  430. /*
  431. *Figure the number of characters in the buffer.
  432. *Exit immediately if none.
  433. */
  434. rmask = RQUEUEMASK;
  435. head = ch->ch_r_head & rmask;
  436. tail = ch->ch_r_tail & rmask;
  437. data_len = (head - tail) & rmask;
  438. if (data_len == 0) {
  439. spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
  440. return;
  441. }
  442. jsm_printk(READ, INFO, &ch->ch_bd->pci_dev, "start\n");
  443. /*
  444. *If the device is not open, or CREAD is off, flush
  445. *input data and return immediately.
  446. */
  447. if (!tp ||
  448. !(tp->termios->c_cflag & CREAD) ) {
  449. jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
  450. "input. dropping %d bytes on port %d...\n", data_len, ch->ch_portnum);
  451. ch->ch_r_head = tail;
  452. /* Force queue flow control to be released, if needed */
  453. jsm_check_queue_flow_control(ch);
  454. spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
  455. return;
  456. }
  457. /*
  458. * If we are throttled, simply don't read any data.
  459. */
  460. if (ch->ch_flags & CH_STOPI) {
  461. spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
  462. jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
  463. "Port %d throttled, not reading any data. head: %x tail: %x\n",
  464. ch->ch_portnum, head, tail);
  465. return;
  466. }
  467. jsm_printk(READ, INFO, &ch->ch_bd->pci_dev, "start 2\n");
  468. if (data_len <= 0) {
  469. spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
  470. jsm_printk(READ, INFO, &ch->ch_bd->pci_dev, "jsm_input 1\n");
  471. return;
  472. }
  473. len = tty_buffer_request_room(tp, data_len);
  474. n = len;
  475. /*
  476. * n now contains the most amount of data we can copy,
  477. * bounded either by the flip buffer size or the amount
  478. * of data the card actually has pending...
  479. */
  480. while (n) {
  481. s = ((head >= tail) ? head : RQUEUESIZE) - tail;
  482. s = min(s, n);
  483. if (s <= 0)
  484. break;
  485. /*
  486. * If conditions are such that ld needs to see all
  487. * UART errors, we will have to walk each character
  488. * and error byte and send them to the buffer one at
  489. * a time.
  490. */
  491. if (I_PARMRK(tp) || I_BRKINT(tp) || I_INPCK(tp)) {
  492. for (i = 0; i < s; i++) {
  493. /*
  494. * Give the Linux ld the flags in the
  495. * format it likes.
  496. */
  497. if (*(ch->ch_equeue +tail +i) & UART_LSR_BI)
  498. tty_insert_flip_char(tp, *(ch->ch_rqueue +tail +i), TTY_BREAK);
  499. else if (*(ch->ch_equeue +tail +i) & UART_LSR_PE)
  500. tty_insert_flip_char(tp, *(ch->ch_rqueue +tail +i), TTY_PARITY);
  501. else if (*(ch->ch_equeue +tail +i) & UART_LSR_FE)
  502. tty_insert_flip_char(tp, *(ch->ch_rqueue +tail +i), TTY_FRAME);
  503. else
  504. tty_insert_flip_char(tp, *(ch->ch_rqueue +tail +i), TTY_NORMAL);
  505. }
  506. } else {
  507. tty_insert_flip_string(tp, ch->ch_rqueue + tail, s) ;
  508. }
  509. tail += s;
  510. n -= s;
  511. /* Flip queue if needed */
  512. tail &= rmask;
  513. }
  514. ch->ch_r_tail = tail & rmask;
  515. ch->ch_e_tail = tail & rmask;
  516. jsm_check_queue_flow_control(ch);
  517. spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
  518. /* Tell the tty layer its okay to "eat" the data now */
  519. tty_flip_buffer_push(tp);
  520. jsm_printk(IOCTL, INFO, &ch->ch_bd->pci_dev, "finish\n");
  521. }
  522. static void jsm_carrier(struct jsm_channel *ch)
  523. {
  524. struct jsm_board *bd;
  525. int virt_carrier = 0;
  526. int phys_carrier = 0;
  527. jsm_printk(CARR, INFO, &ch->ch_bd->pci_dev, "start\n");
  528. if (!ch)
  529. return;
  530. bd = ch->ch_bd;
  531. if (!bd)
  532. return;
  533. if (ch->ch_mistat & UART_MSR_DCD) {
  534. jsm_printk(CARR, INFO, &ch->ch_bd->pci_dev,
  535. "mistat: %x D_CD: %x\n", ch->ch_mistat, ch->ch_mistat & UART_MSR_DCD);
  536. phys_carrier = 1;
  537. }
  538. if (ch->ch_c_cflag & CLOCAL)
  539. virt_carrier = 1;
  540. jsm_printk(CARR, INFO, &ch->ch_bd->pci_dev,
  541. "DCD: physical: %d virt: %d\n", phys_carrier, virt_carrier);
  542. /*
  543. * Test for a VIRTUAL carrier transition to HIGH.
  544. */
  545. if (((ch->ch_flags & CH_FCAR) == 0) && (virt_carrier == 1)) {
  546. /*
  547. * When carrier rises, wake any threads waiting
  548. * for carrier in the open routine.
  549. */
  550. jsm_printk(CARR, INFO, &ch->ch_bd->pci_dev,
  551. "carrier: virt DCD rose\n");
  552. if (waitqueue_active(&(ch->ch_flags_wait)))
  553. wake_up_interruptible(&ch->ch_flags_wait);
  554. }
  555. /*
  556. * Test for a PHYSICAL carrier transition to HIGH.
  557. */
  558. if (((ch->ch_flags & CH_CD) == 0) && (phys_carrier == 1)) {
  559. /*
  560. * When carrier rises, wake any threads waiting
  561. * for carrier in the open routine.
  562. */
  563. jsm_printk(CARR, INFO, &ch->ch_bd->pci_dev,
  564. "carrier: physical DCD rose\n");
  565. if (waitqueue_active(&(ch->ch_flags_wait)))
  566. wake_up_interruptible(&ch->ch_flags_wait);
  567. }
  568. /*
  569. * Test for a PHYSICAL transition to low, so long as we aren't
  570. * currently ignoring physical transitions (which is what "virtual
  571. * carrier" indicates).
  572. *
  573. * The transition of the virtual carrier to low really doesn't
  574. * matter... it really only means "ignore carrier state", not
  575. * "make pretend that carrier is there".
  576. */
  577. if ((virt_carrier == 0) && ((ch->ch_flags & CH_CD) != 0)
  578. && (phys_carrier == 0)) {
  579. /*
  580. * When carrier drops:
  581. *
  582. * Drop carrier on all open units.
  583. *
  584. * Flush queues, waking up any task waiting in the
  585. * line discipline.
  586. *
  587. * Send a hangup to the control terminal.
  588. *
  589. * Enable all select calls.
  590. */
  591. if (waitqueue_active(&(ch->ch_flags_wait)))
  592. wake_up_interruptible(&ch->ch_flags_wait);
  593. }
  594. /*
  595. * Make sure that our cached values reflect the current reality.
  596. */
  597. if (virt_carrier == 1)
  598. ch->ch_flags |= CH_FCAR;
  599. else
  600. ch->ch_flags &= ~CH_FCAR;
  601. if (phys_carrier == 1)
  602. ch->ch_flags |= CH_CD;
  603. else
  604. ch->ch_flags &= ~CH_CD;
  605. }
  606. void jsm_check_queue_flow_control(struct jsm_channel *ch)
  607. {
  608. struct board_ops *bd_ops = ch->ch_bd->bd_ops;
  609. int qleft;
  610. /* Store how much space we have left in the queue */
  611. if ((qleft = ch->ch_r_tail - ch->ch_r_head - 1) < 0)
  612. qleft += RQUEUEMASK + 1;
  613. /*
  614. * Check to see if we should enforce flow control on our queue because
  615. * the ld (or user) isn't reading data out of our queue fast enuf.
  616. *
  617. * NOTE: This is done based on what the current flow control of the
  618. * port is set for.
  619. *
  620. * 1) HWFLOW (RTS) - Turn off the UART's Receive interrupt.
  621. * This will cause the UART's FIFO to back up, and force
  622. * the RTS signal to be dropped.
  623. * 2) SWFLOW (IXOFF) - Keep trying to send a stop character to
  624. * the other side, in hopes it will stop sending data to us.
  625. * 3) NONE - Nothing we can do. We will simply drop any extra data
  626. * that gets sent into us when the queue fills up.
  627. */
  628. if (qleft < 256) {
  629. /* HWFLOW */
  630. if (ch->ch_c_cflag & CRTSCTS) {
  631. if(!(ch->ch_flags & CH_RECEIVER_OFF)) {
  632. bd_ops->disable_receiver(ch);
  633. ch->ch_flags |= (CH_RECEIVER_OFF);
  634. jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
  635. "Internal queue hit hilevel mark (%d)! Turning off interrupts.\n",
  636. qleft);
  637. }
  638. }
  639. /* SWFLOW */
  640. else if (ch->ch_c_iflag & IXOFF) {
  641. if (ch->ch_stops_sent <= MAX_STOPS_SENT) {
  642. bd_ops->send_stop_character(ch);
  643. ch->ch_stops_sent++;
  644. jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
  645. "Sending stop char! Times sent: %x\n", ch->ch_stops_sent);
  646. }
  647. }
  648. }
  649. /*
  650. * Check to see if we should unenforce flow control because
  651. * ld (or user) finally read enuf data out of our queue.
  652. *
  653. * NOTE: This is done based on what the current flow control of the
  654. * port is set for.
  655. *
  656. * 1) HWFLOW (RTS) - Turn back on the UART's Receive interrupt.
  657. * This will cause the UART's FIFO to raise RTS back up,
  658. * which will allow the other side to start sending data again.
  659. * 2) SWFLOW (IXOFF) - Send a start character to
  660. * the other side, so it will start sending data to us again.
  661. * 3) NONE - Do nothing. Since we didn't do anything to turn off the
  662. * other side, we don't need to do anything now.
  663. */
  664. if (qleft > (RQUEUESIZE / 2)) {
  665. /* HWFLOW */
  666. if (ch->ch_c_cflag & CRTSCTS) {
  667. if (ch->ch_flags & CH_RECEIVER_OFF) {
  668. bd_ops->enable_receiver(ch);
  669. ch->ch_flags &= ~(CH_RECEIVER_OFF);
  670. jsm_printk(READ, INFO, &ch->ch_bd->pci_dev,
  671. "Internal queue hit lowlevel mark (%d)! Turning on interrupts.\n",
  672. qleft);
  673. }
  674. }
  675. /* SWFLOW */
  676. else if (ch->ch_c_iflag & IXOFF && ch->ch_stops_sent) {
  677. ch->ch_stops_sent = 0;
  678. bd_ops->send_start_character(ch);
  679. jsm_printk(READ, INFO, &ch->ch_bd->pci_dev, "Sending start char!\n");
  680. }
  681. }
  682. }
  683. /*
  684. * jsm_tty_write()
  685. *
  686. * Take data from the user or kernel and send it out to the FEP.
  687. * In here exists all the Transparent Print magic as well.
  688. */
  689. int jsm_tty_write(struct uart_port *port)
  690. {
  691. int bufcount;
  692. int data_count = 0,data_count1 =0;
  693. u16 head;
  694. u16 tail;
  695. u16 tmask;
  696. u32 remain;
  697. int temp_tail = port->state->xmit.tail;
  698. struct jsm_channel *channel = (struct jsm_channel *)port;
  699. tmask = WQUEUEMASK;
  700. head = (channel->ch_w_head) & tmask;
  701. tail = (channel->ch_w_tail) & tmask;
  702. if ((bufcount = tail - head - 1) < 0)
  703. bufcount += WQUEUESIZE;
  704. bufcount = min(bufcount, 56);
  705. remain = WQUEUESIZE - head;
  706. data_count = 0;
  707. if (bufcount >= remain) {
  708. bufcount -= remain;
  709. while ((port->state->xmit.head != temp_tail) &&
  710. (data_count < remain)) {
  711. channel->ch_wqueue[head++] =
  712. port->state->xmit.buf[temp_tail];
  713. temp_tail++;
  714. temp_tail &= (UART_XMIT_SIZE - 1);
  715. data_count++;
  716. }
  717. if (data_count == remain) head = 0;
  718. }
  719. data_count1 = 0;
  720. if (bufcount > 0) {
  721. remain = bufcount;
  722. while ((port->state->xmit.head != temp_tail) &&
  723. (data_count1 < remain)) {
  724. channel->ch_wqueue[head++] =
  725. port->state->xmit.buf[temp_tail];
  726. temp_tail++;
  727. temp_tail &= (UART_XMIT_SIZE - 1);
  728. data_count1++;
  729. }
  730. }
  731. port->state->xmit.tail = temp_tail;
  732. data_count += data_count1;
  733. if (data_count) {
  734. head &= tmask;
  735. channel->ch_w_head = head;
  736. }
  737. if (data_count) {
  738. channel->ch_bd->bd_ops->copy_data_from_queue_to_uart(channel);
  739. }
  740. return data_count;
  741. }