riotty.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. /*
  2. ** -----------------------------------------------------------------------------
  3. **
  4. ** Perle Specialix driver for Linux
  5. ** Ported from existing RIO Driver for SCO sources.
  6. *
  7. * (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. **
  23. ** Module : riotty.c
  24. ** SID : 1.3
  25. ** Last Modified : 11/6/98 10:33:47
  26. ** Retrieved : 11/6/98 10:33:50
  27. **
  28. ** ident @(#)riotty.c 1.3
  29. **
  30. ** -----------------------------------------------------------------------------
  31. */
  32. #ifdef SCCS_LABELS
  33. static char *_riotty_c_sccs_ = "@(#)riotty.c 1.3";
  34. #endif
  35. #define __EXPLICIT_DEF_H__
  36. #include <linux/module.h>
  37. #include <linux/slab.h>
  38. #include <linux/errno.h>
  39. #include <linux/tty.h>
  40. #include <linux/string.h>
  41. #include <asm/io.h>
  42. #include <asm/system.h>
  43. #include <asm/string.h>
  44. #include <asm/uaccess.h>
  45. #include <linux/termios.h>
  46. #include <linux/serial.h>
  47. #include <linux/generic_serial.h>
  48. #include "linux_compat.h"
  49. #include "rio_linux.h"
  50. #include "pkt.h"
  51. #include "daemon.h"
  52. #include "rio.h"
  53. #include "riospace.h"
  54. #include "cmdpkt.h"
  55. #include "map.h"
  56. #include "rup.h"
  57. #include "port.h"
  58. #include "riodrvr.h"
  59. #include "rioinfo.h"
  60. #include "func.h"
  61. #include "errors.h"
  62. #include "pci.h"
  63. #include "parmmap.h"
  64. #include "unixrup.h"
  65. #include "board.h"
  66. #include "host.h"
  67. #include "phb.h"
  68. #include "link.h"
  69. #include "cmdblk.h"
  70. #include "route.h"
  71. #include "cirrus.h"
  72. #include "rioioctl.h"
  73. #include "param.h"
  74. static void RIOClearUp(struct Port *PortP);
  75. /* Below belongs in func.h */
  76. int RIOShortCommand(struct rio_info *p, struct Port *PortP, int command, int len, int arg);
  77. extern struct rio_info *p;
  78. int riotopen(struct tty_struct *tty, struct file *filp)
  79. {
  80. unsigned int SysPort;
  81. int repeat_this = 250;
  82. struct Port *PortP; /* pointer to the port structure */
  83. unsigned long flags;
  84. int retval = 0;
  85. func_enter();
  86. /* Make sure driver_data is NULL in case the rio isn't booted jet. Else gs_close
  87. is going to oops.
  88. */
  89. tty->driver_data = NULL;
  90. SysPort = rio_minor(tty);
  91. if (p->RIOFailed) {
  92. rio_dprintk(RIO_DEBUG_TTY, "System initialisation failed\n");
  93. func_exit();
  94. return -ENXIO;
  95. }
  96. rio_dprintk(RIO_DEBUG_TTY, "port open SysPort %d (mapped:%d)\n", SysPort, p->RIOPortp[SysPort]->Mapped);
  97. /*
  98. ** Validate that we have received a legitimate request.
  99. ** Currently, just check that we are opening a port on
  100. ** a host card that actually exists, and that the port
  101. ** has been mapped onto a host.
  102. */
  103. if (SysPort >= RIO_PORTS) { /* out of range ? */
  104. rio_dprintk(RIO_DEBUG_TTY, "Illegal port number %d\n", SysPort);
  105. func_exit();
  106. return -ENXIO;
  107. }
  108. /*
  109. ** Grab pointer to the port stucture
  110. */
  111. PortP = p->RIOPortp[SysPort]; /* Get control struc */
  112. rio_dprintk(RIO_DEBUG_TTY, "PortP: %p\n", PortP);
  113. if (!PortP->Mapped) { /* we aren't mapped yet! */
  114. /*
  115. ** The system doesn't know which RTA this port
  116. ** corresponds to.
  117. */
  118. rio_dprintk(RIO_DEBUG_TTY, "port not mapped into system\n");
  119. func_exit();
  120. return -ENXIO;
  121. }
  122. tty->driver_data = PortP;
  123. PortP->gs.tty = tty;
  124. PortP->gs.count++;
  125. rio_dprintk(RIO_DEBUG_TTY, "%d bytes in tx buffer\n", PortP->gs.xmit_cnt);
  126. retval = gs_init_port(&PortP->gs);
  127. if (retval) {
  128. PortP->gs.count--;
  129. return -ENXIO;
  130. }
  131. /*
  132. ** If the host hasn't been booted yet, then
  133. ** fail
  134. */
  135. if ((PortP->HostP->Flags & RUN_STATE) != RC_RUNNING) {
  136. rio_dprintk(RIO_DEBUG_TTY, "Host not running\n");
  137. func_exit();
  138. return -ENXIO;
  139. }
  140. /*
  141. ** If the RTA has not booted yet and the user has choosen to block
  142. ** until the RTA is present then we must spin here waiting for
  143. ** the RTA to boot.
  144. */
  145. /* I find the above code a bit hairy. I find the below code
  146. easier to read and shorter. Now, if it works too that would
  147. be great... -- REW
  148. */
  149. rio_dprintk(RIO_DEBUG_TTY, "Checking if RTA has booted... \n");
  150. while (!(PortP->HostP->Mapping[PortP->RupNum].Flags & RTA_BOOTED)) {
  151. if (!PortP->WaitUntilBooted) {
  152. rio_dprintk(RIO_DEBUG_TTY, "RTA never booted\n");
  153. func_exit();
  154. return -ENXIO;
  155. }
  156. /* Under Linux you'd normally use a wait instead of this
  157. busy-waiting. I'll stick with the old implementation for
  158. now. --REW
  159. */
  160. if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
  161. rio_dprintk(RIO_DEBUG_TTY, "RTA_wait_for_boot: EINTR in delay \n");
  162. func_exit();
  163. return -EINTR;
  164. }
  165. if (repeat_this-- <= 0) {
  166. rio_dprintk(RIO_DEBUG_TTY, "Waiting for RTA to boot timeout\n");
  167. func_exit();
  168. return -EIO;
  169. }
  170. }
  171. rio_dprintk(RIO_DEBUG_TTY, "RTA has been booted\n");
  172. rio_spin_lock_irqsave(&PortP->portSem, flags);
  173. if (p->RIOHalted) {
  174. goto bombout;
  175. }
  176. /*
  177. ** If the port is in the final throws of being closed,
  178. ** we should wait here (politely), waiting
  179. ** for it to finish, so that it doesn't close us!
  180. */
  181. while ((PortP->State & RIO_CLOSING) && !p->RIOHalted) {
  182. rio_dprintk(RIO_DEBUG_TTY, "Waiting for RIO_CLOSING to go away\n");
  183. if (repeat_this-- <= 0) {
  184. rio_dprintk(RIO_DEBUG_TTY, "Waiting for not idle closed broken by signal\n");
  185. RIOPreemptiveCmd(p, PortP, RIOC_FCLOSE);
  186. retval = -EINTR;
  187. goto bombout;
  188. }
  189. rio_spin_unlock_irqrestore(&PortP->portSem, flags);
  190. if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
  191. rio_spin_lock_irqsave(&PortP->portSem, flags);
  192. retval = -EINTR;
  193. goto bombout;
  194. }
  195. rio_spin_lock_irqsave(&PortP->portSem, flags);
  196. }
  197. if (!PortP->Mapped) {
  198. rio_dprintk(RIO_DEBUG_TTY, "Port unmapped while closing!\n");
  199. rio_spin_unlock_irqrestore(&PortP->portSem, flags);
  200. retval = -ENXIO;
  201. func_exit();
  202. return retval;
  203. }
  204. if (p->RIOHalted) {
  205. goto bombout;
  206. }
  207. /*
  208. ** 15.10.1998 ARG - ESIL 0761 part fix
  209. ** RIO has it's own CTSFLOW and RTSFLOW flags in 'Config' in the port structure,
  210. ** we need to make sure that the flags are clear when the port is opened.
  211. */
  212. /* Uh? Suppose I turn these on and then another process opens
  213. the port again? The flags get cleared! Not good. -- REW */
  214. if (!(PortP->State & (RIO_LOPEN | RIO_MOPEN))) {
  215. PortP->Config &= ~(RIO_CTSFLOW | RIO_RTSFLOW);
  216. }
  217. if (!(PortP->firstOpen)) { /* First time ? */
  218. rio_dprintk(RIO_DEBUG_TTY, "First open for this port\n");
  219. PortP->firstOpen++;
  220. PortP->CookMode = 0; /* XXX RIOCookMode(tp); */
  221. PortP->InUse = NOT_INUSE;
  222. /* Tentative fix for bug PR27. Didn't work. */
  223. /* PortP->gs.xmit_cnt = 0; */
  224. rio_spin_unlock_irqrestore(&PortP->portSem, flags);
  225. /* Someone explain to me why this delay/config is
  226. here. If I read the docs correctly the "open"
  227. command piggybacks the parameters immediately.
  228. -- REW */
  229. RIOParam(PortP, RIOC_OPEN, 1, OK_TO_SLEEP); /* Open the port */
  230. rio_spin_lock_irqsave(&PortP->portSem, flags);
  231. /*
  232. ** wait for the port to be not closed.
  233. */
  234. while (!(PortP->PortState & PORT_ISOPEN) && !p->RIOHalted) {
  235. rio_dprintk(RIO_DEBUG_TTY, "Waiting for PORT_ISOPEN-currently %x\n", PortP->PortState);
  236. rio_spin_unlock_irqrestore(&PortP->portSem, flags);
  237. if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
  238. rio_dprintk(RIO_DEBUG_TTY, "Waiting for open to finish broken by signal\n");
  239. RIOPreemptiveCmd(p, PortP, RIOC_FCLOSE);
  240. func_exit();
  241. return -EINTR;
  242. }
  243. rio_spin_lock_irqsave(&PortP->portSem, flags);
  244. }
  245. if (p->RIOHalted) {
  246. retval = -EIO;
  247. bombout:
  248. /* RIOClearUp( PortP ); */
  249. rio_spin_unlock_irqrestore(&PortP->portSem, flags);
  250. return retval;
  251. }
  252. rio_dprintk(RIO_DEBUG_TTY, "PORT_ISOPEN found\n");
  253. }
  254. rio_dprintk(RIO_DEBUG_TTY, "Modem - test for carrier\n");
  255. /*
  256. ** ACTION
  257. ** insert test for carrier here. -- ???
  258. ** I already see that test here. What's the deal? -- REW
  259. */
  260. if ((PortP->gs.tty->termios->c_cflag & CLOCAL) ||
  261. (PortP->ModemState & RIOC_MSVR1_CD)) {
  262. rio_dprintk(RIO_DEBUG_TTY, "open(%d) Modem carr on\n", SysPort);
  263. /*
  264. tp->tm.c_state |= CARR_ON;
  265. wakeup((caddr_t) &tp->tm.c_canq);
  266. */
  267. PortP->State |= RIO_CARR_ON;
  268. wake_up_interruptible(&PortP->gs.open_wait);
  269. } else { /* no carrier - wait for DCD */
  270. /*
  271. while (!(PortP->gs.tty->termios->c_state & CARR_ON) &&
  272. !(filp->f_flags & O_NONBLOCK) && !p->RIOHalted )
  273. */
  274. while (!(PortP->State & RIO_CARR_ON) && !(filp->f_flags & O_NONBLOCK) && !p->RIOHalted) {
  275. rio_dprintk(RIO_DEBUG_TTY, "open(%d) sleeping for carr on\n", SysPort);
  276. /*
  277. PortP->gs.tty->termios->c_state |= WOPEN;
  278. */
  279. PortP->State |= RIO_WOPEN;
  280. rio_spin_unlock_irqrestore(&PortP->portSem, flags);
  281. if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
  282. rio_spin_lock_irqsave(&PortP->portSem, flags);
  283. /*
  284. ** ACTION: verify that this is a good thing
  285. ** to do here. -- ???
  286. ** I think it's OK. -- REW
  287. */
  288. rio_dprintk(RIO_DEBUG_TTY, "open(%d) sleeping for carr broken by signal\n", SysPort);
  289. RIOPreemptiveCmd(p, PortP, RIOC_FCLOSE);
  290. /*
  291. tp->tm.c_state &= ~WOPEN;
  292. */
  293. PortP->State &= ~RIO_WOPEN;
  294. rio_spin_unlock_irqrestore(&PortP->portSem, flags);
  295. func_exit();
  296. return -EINTR;
  297. }
  298. rio_spin_lock_irqsave(&PortP->portSem, flags);
  299. }
  300. PortP->State &= ~RIO_WOPEN;
  301. }
  302. if (p->RIOHalted)
  303. goto bombout;
  304. rio_dprintk(RIO_DEBUG_TTY, "Setting RIO_MOPEN\n");
  305. PortP->State |= RIO_MOPEN;
  306. if (p->RIOHalted)
  307. goto bombout;
  308. rio_dprintk(RIO_DEBUG_TTY, "high level open done\n");
  309. /*
  310. ** Count opens for port statistics reporting
  311. */
  312. if (PortP->statsGather)
  313. PortP->opens++;
  314. rio_spin_unlock_irqrestore(&PortP->portSem, flags);
  315. rio_dprintk(RIO_DEBUG_TTY, "Returning from open\n");
  316. func_exit();
  317. return 0;
  318. }
  319. /*
  320. ** RIOClose the port.
  321. ** The operating system thinks that this is last close for the device.
  322. ** As there are two interfaces to the port (Modem and tty), we need to
  323. ** check that both are closed before we close the device.
  324. */
  325. int riotclose(void *ptr)
  326. {
  327. struct Port *PortP = ptr; /* pointer to the port structure */
  328. int deleted = 0;
  329. int try = -1; /* Disable the timeouts by setting them to -1 */
  330. int repeat_this = -1; /* Congrats to those having 15 years of
  331. uptime! (You get to break the driver.) */
  332. unsigned long end_time;
  333. struct tty_struct *tty;
  334. unsigned long flags;
  335. int rv = 0;
  336. rio_dprintk(RIO_DEBUG_TTY, "port close SysPort %d\n", PortP->PortNum);
  337. /* PortP = p->RIOPortp[SysPort]; */
  338. rio_dprintk(RIO_DEBUG_TTY, "Port is at address %p\n", PortP);
  339. /* tp = PortP->TtyP; *//* Get tty */
  340. tty = PortP->gs.tty;
  341. rio_dprintk(RIO_DEBUG_TTY, "TTY is at address %p\n", tty);
  342. if (PortP->gs.closing_wait)
  343. end_time = jiffies + PortP->gs.closing_wait;
  344. else
  345. end_time = jiffies + MAX_SCHEDULE_TIMEOUT;
  346. rio_spin_lock_irqsave(&PortP->portSem, flags);
  347. /*
  348. ** Setting this flag will make any process trying to open
  349. ** this port block until we are complete closing it.
  350. */
  351. PortP->State |= RIO_CLOSING;
  352. if ((PortP->State & RIO_DELETED)) {
  353. rio_dprintk(RIO_DEBUG_TTY, "Close on deleted RTA\n");
  354. deleted = 1;
  355. }
  356. if (p->RIOHalted) {
  357. RIOClearUp(PortP);
  358. rv = -EIO;
  359. goto close_end;
  360. }
  361. rio_dprintk(RIO_DEBUG_TTY, "Clear bits\n");
  362. /*
  363. ** clear the open bits for this device
  364. */
  365. PortP->State &= ~RIO_MOPEN;
  366. PortP->State &= ~RIO_CARR_ON;
  367. PortP->ModemState &= ~RIOC_MSVR1_CD;
  368. /*
  369. ** If the device was open as both a Modem and a tty line
  370. ** then we need to wimp out here, as the port has not really
  371. ** been finally closed (gee, whizz!) The test here uses the
  372. ** bit for the OTHER mode of operation, to see if THAT is
  373. ** still active!
  374. */
  375. if ((PortP->State & (RIO_LOPEN | RIO_MOPEN))) {
  376. /*
  377. ** The port is still open for the other task -
  378. ** return, pretending that we are still active.
  379. */
  380. rio_dprintk(RIO_DEBUG_TTY, "Channel %d still open !\n", PortP->PortNum);
  381. PortP->State &= ~RIO_CLOSING;
  382. if (PortP->firstOpen)
  383. PortP->firstOpen--;
  384. rio_spin_unlock_irqrestore(&PortP->portSem, flags);
  385. return -EIO;
  386. }
  387. rio_dprintk(RIO_DEBUG_TTY, "Closing down - everything must go!\n");
  388. PortP->State &= ~RIO_DYNOROD;
  389. /*
  390. ** This is where we wait for the port
  391. ** to drain down before closing. Bye-bye....
  392. ** (We never meant to do this)
  393. */
  394. rio_dprintk(RIO_DEBUG_TTY, "Timeout 1 starts\n");
  395. if (!deleted)
  396. while ((PortP->InUse != NOT_INUSE) && !p->RIOHalted && (PortP->TxBufferIn != PortP->TxBufferOut)) {
  397. if (repeat_this-- <= 0) {
  398. rv = -EINTR;
  399. rio_dprintk(RIO_DEBUG_TTY, "Waiting for not idle closed broken by signal\n");
  400. RIOPreemptiveCmd(p, PortP, RIOC_FCLOSE);
  401. goto close_end;
  402. }
  403. rio_dprintk(RIO_DEBUG_TTY, "Calling timeout to flush in closing\n");
  404. rio_spin_unlock_irqrestore(&PortP->portSem, flags);
  405. if (RIODelay_ni(PortP, HUNDRED_MS * 10) == RIO_FAIL) {
  406. rio_dprintk(RIO_DEBUG_TTY, "RTA EINTR in delay \n");
  407. rv = -EINTR;
  408. rio_spin_lock_irqsave(&PortP->portSem, flags);
  409. goto close_end;
  410. }
  411. rio_spin_lock_irqsave(&PortP->portSem, flags);
  412. }
  413. PortP->TxBufferIn = PortP->TxBufferOut = 0;
  414. repeat_this = 0xff;
  415. PortP->InUse = 0;
  416. if ((PortP->State & (RIO_LOPEN | RIO_MOPEN))) {
  417. /*
  418. ** The port has been re-opened for the other task -
  419. ** return, pretending that we are still active.
  420. */
  421. rio_dprintk(RIO_DEBUG_TTY, "Channel %d re-open!\n", PortP->PortNum);
  422. PortP->State &= ~RIO_CLOSING;
  423. rio_spin_unlock_irqrestore(&PortP->portSem, flags);
  424. if (PortP->firstOpen)
  425. PortP->firstOpen--;
  426. return -EIO;
  427. }
  428. if (p->RIOHalted) {
  429. RIOClearUp(PortP);
  430. goto close_end;
  431. }
  432. /* Can't call RIOShortCommand with the port locked. */
  433. rio_spin_unlock_irqrestore(&PortP->portSem, flags);
  434. if (RIOShortCommand(p, PortP, RIOC_CLOSE, 1, 0) == RIO_FAIL) {
  435. RIOPreemptiveCmd(p, PortP, RIOC_FCLOSE);
  436. rio_spin_lock_irqsave(&PortP->portSem, flags);
  437. goto close_end;
  438. }
  439. if (!deleted)
  440. while (try && (PortP->PortState & PORT_ISOPEN)) {
  441. try--;
  442. if (time_after(jiffies, end_time)) {
  443. rio_dprintk(RIO_DEBUG_TTY, "Run out of tries - force the bugger shut!\n");
  444. RIOPreemptiveCmd(p, PortP, RIOC_FCLOSE);
  445. break;
  446. }
  447. rio_dprintk(RIO_DEBUG_TTY, "Close: PortState:ISOPEN is %d\n", PortP->PortState & PORT_ISOPEN);
  448. if (p->RIOHalted) {
  449. RIOClearUp(PortP);
  450. rio_spin_lock_irqsave(&PortP->portSem, flags);
  451. goto close_end;
  452. }
  453. if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
  454. rio_dprintk(RIO_DEBUG_TTY, "RTA EINTR in delay \n");
  455. RIOPreemptiveCmd(p, PortP, RIOC_FCLOSE);
  456. break;
  457. }
  458. }
  459. rio_spin_lock_irqsave(&PortP->portSem, flags);
  460. rio_dprintk(RIO_DEBUG_TTY, "Close: try was %d on completion\n", try);
  461. /* RIOPreemptiveCmd(p, PortP, RIOC_FCLOSE); */
  462. /*
  463. ** 15.10.1998 ARG - ESIL 0761 part fix
  464. ** RIO has it's own CTSFLOW and RTSFLOW flags in 'Config' in the port structure,** we need to make sure that the flags are clear when the port is opened.
  465. */
  466. PortP->Config &= ~(RIO_CTSFLOW | RIO_RTSFLOW);
  467. /*
  468. ** Count opens for port statistics reporting
  469. */
  470. if (PortP->statsGather)
  471. PortP->closes++;
  472. close_end:
  473. /* XXX: Why would a "DELETED" flag be reset here? I'd have
  474. thought that a "deleted" flag means that the port was
  475. permanently gone, but here we can make it reappear by it
  476. being in close during the "deletion".
  477. */
  478. PortP->State &= ~(RIO_CLOSING | RIO_DELETED);
  479. if (PortP->firstOpen)
  480. PortP->firstOpen--;
  481. rio_spin_unlock_irqrestore(&PortP->portSem, flags);
  482. rio_dprintk(RIO_DEBUG_TTY, "Return from close\n");
  483. return rv;
  484. }
  485. static void RIOClearUp(struct Port *PortP)
  486. {
  487. rio_dprintk(RIO_DEBUG_TTY, "RIOHalted set\n");
  488. PortP->Config = 0; /* Direct semaphore */
  489. PortP->PortState = 0;
  490. PortP->firstOpen = 0;
  491. PortP->FlushCmdBodge = 0;
  492. PortP->ModemState = PortP->CookMode = 0;
  493. PortP->Mapped = 0;
  494. PortP->WflushFlag = 0;
  495. PortP->MagicFlags = 0;
  496. PortP->RxDataStart = 0;
  497. PortP->TxBufferIn = 0;
  498. PortP->TxBufferOut = 0;
  499. }
  500. /*
  501. ** Put a command onto a port.
  502. ** The PortPointer, command, length and arg are passed.
  503. ** The len is the length *inclusive* of the command byte,
  504. ** and so for a command that takes no data, len==1.
  505. ** The arg is a single byte, and is only used if len==2.
  506. ** Other values of len aren't allowed, and will cause
  507. ** a panic.
  508. */
  509. int RIOShortCommand(struct rio_info *p, struct Port *PortP, int command, int len, int arg)
  510. {
  511. struct PKT __iomem *PacketP;
  512. int retries = 20; /* at 10 per second -> 2 seconds */
  513. unsigned long flags;
  514. rio_dprintk(RIO_DEBUG_TTY, "entering shortcommand.\n");
  515. if (PortP->State & RIO_DELETED) {
  516. rio_dprintk(RIO_DEBUG_TTY, "Short command to deleted RTA ignored\n");
  517. return RIO_FAIL;
  518. }
  519. rio_spin_lock_irqsave(&PortP->portSem, flags);
  520. /*
  521. ** If the port is in use for pre-emptive command, then wait for it to
  522. ** be free again.
  523. */
  524. while ((PortP->InUse != NOT_INUSE) && !p->RIOHalted) {
  525. rio_dprintk(RIO_DEBUG_TTY, "Waiting for not in use (%d)\n", retries);
  526. rio_spin_unlock_irqrestore(&PortP->portSem, flags);
  527. if (retries-- <= 0) {
  528. return RIO_FAIL;
  529. }
  530. if (RIODelay_ni(PortP, HUNDRED_MS) == RIO_FAIL) {
  531. return RIO_FAIL;
  532. }
  533. rio_spin_lock_irqsave(&PortP->portSem, flags);
  534. }
  535. if (PortP->State & RIO_DELETED) {
  536. rio_dprintk(RIO_DEBUG_TTY, "Short command to deleted RTA ignored\n");
  537. rio_spin_unlock_irqrestore(&PortP->portSem, flags);
  538. return RIO_FAIL;
  539. }
  540. while (!can_add_transmit(&PacketP, PortP) && !p->RIOHalted) {
  541. rio_dprintk(RIO_DEBUG_TTY, "Waiting to add short command to queue (%d)\n", retries);
  542. rio_spin_unlock_irqrestore(&PortP->portSem, flags);
  543. if (retries-- <= 0) {
  544. rio_dprintk(RIO_DEBUG_TTY, "out of tries. Failing\n");
  545. return RIO_FAIL;
  546. }
  547. if (RIODelay_ni(PortP, HUNDRED_MS) == RIO_FAIL) {
  548. return RIO_FAIL;
  549. }
  550. rio_spin_lock_irqsave(&PortP->portSem, flags);
  551. }
  552. if (p->RIOHalted) {
  553. rio_spin_unlock_irqrestore(&PortP->portSem, flags);
  554. return RIO_FAIL;
  555. }
  556. /*
  557. ** set the command byte and the argument byte
  558. */
  559. writeb(command, &PacketP->data[0]);
  560. if (len == 2)
  561. writeb(arg, &PacketP->data[1]);
  562. /*
  563. ** set the length of the packet and set the command bit.
  564. */
  565. writeb(PKT_CMD_BIT | len, &PacketP->len);
  566. add_transmit(PortP);
  567. /*
  568. ** Count characters transmitted for port statistics reporting
  569. */
  570. if (PortP->statsGather)
  571. PortP->txchars += len;
  572. rio_spin_unlock_irqrestore(&PortP->portSem, flags);
  573. return p->RIOHalted ? RIO_FAIL : ~RIO_FAIL;
  574. }