riotty.c 18 KB

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