jsm_tty.c 25 KB

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