riotty.c 18 KB

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