i2lib.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207
  1. /*******************************************************************************
  2. *
  3. * (c) 1999 by Computone Corporation
  4. *
  5. ********************************************************************************
  6. *
  7. *
  8. * PACKAGE: Linux tty Device Driver for IntelliPort family of multiport
  9. * serial I/O controllers.
  10. *
  11. * DESCRIPTION: High-level interface code for the device driver. Uses the
  12. * Extremely Low Level Interface Support (i2ellis.c). Provides an
  13. * interface to the standard loadware, to support drivers or
  14. * application code. (This is included source code, not a separate
  15. * compilation module.)
  16. *
  17. *******************************************************************************/
  18. //------------------------------------------------------------------------------
  19. // Note on Strategy:
  20. // Once the board has been initialized, it will interrupt us when:
  21. // 1) It has something in the fifo for us to read (incoming data, flow control
  22. // packets, or whatever).
  23. // 2) It has stripped whatever we have sent last time in the FIFO (and
  24. // consequently is ready for more).
  25. //
  26. // Note also that the buffer sizes declared in i2lib.h are VERY SMALL. This
  27. // worsens performance considerably, but is done so that a great many channels
  28. // might use only a little memory.
  29. //------------------------------------------------------------------------------
  30. //------------------------------------------------------------------------------
  31. // Revision History:
  32. //
  33. // 0.00 - 4/16/91 --- First Draft
  34. // 0.01 - 4/29/91 --- 1st beta release
  35. // 0.02 - 6/14/91 --- Changes to allow small model compilation
  36. // 0.03 - 6/17/91 MAG Break reporting protected from interrupts routines with
  37. // in-line asm added for moving data to/from ring buffers,
  38. // replacing a variety of methods used previously.
  39. // 0.04 - 6/21/91 MAG Initial flow-control packets not queued until
  40. // i2_enable_interrupts time. Former versions would enqueue
  41. // them at i2_init_channel time, before we knew how many
  42. // channels were supposed to exist!
  43. // 0.05 - 10/12/91 MAG Major changes: works through the ellis.c routines now;
  44. // supports new 16-bit protocol and expandable boards.
  45. // - 10/24/91 MAG Most changes in place and stable.
  46. // 0.06 - 2/20/92 MAG Format of CMD_HOTACK corrected: the command takes no
  47. // argument.
  48. // 0.07 -- 3/11/92 MAG Support added to store special packet types at interrupt
  49. // level (mostly responses to specific commands.)
  50. // 0.08 -- 3/30/92 MAG Support added for STAT_MODEM packet
  51. // 0.09 -- 6/24/93 MAG i2Link... needed to update number of boards BEFORE
  52. // turning on the interrupt.
  53. // 0.10 -- 6/25/93 MAG To avoid gruesome death from a bad board, we sanity check
  54. // some incoming.
  55. //
  56. // 1.1 - 12/25/96 AKM Linux version.
  57. // - 10/09/98 DMC Revised Linux version.
  58. //------------------------------------------------------------------------------
  59. //************
  60. //* Includes *
  61. //************
  62. #include <linux/sched.h>
  63. #include "i2lib.h"
  64. //***********************
  65. //* Function Prototypes *
  66. //***********************
  67. static void i2QueueNeeds(i2eBordStrPtr, i2ChanStrPtr, int);
  68. static i2ChanStrPtr i2DeQueueNeeds(i2eBordStrPtr, int );
  69. static void i2StripFifo(i2eBordStrPtr);
  70. static void i2StuffFifoBypass(i2eBordStrPtr);
  71. static void i2StuffFifoFlow(i2eBordStrPtr);
  72. static void i2StuffFifoInline(i2eBordStrPtr);
  73. static int i2RetryFlushOutput(i2ChanStrPtr);
  74. // Not a documented part of the library routines (careful...) but the Diagnostic
  75. // i2diag.c finds them useful to help the throughput in certain limited
  76. // single-threaded operations.
  77. static void iiSendPendingMail(i2eBordStrPtr);
  78. static void serviceOutgoingFifo(i2eBordStrPtr);
  79. // Functions defined in ip2.c as part of interrupt handling
  80. static void do_input(struct work_struct *);
  81. static void do_status(struct work_struct *);
  82. //***************
  83. //* Debug Data *
  84. //***************
  85. #ifdef DEBUG_FIFO
  86. unsigned char DBGBuf[0x4000];
  87. unsigned short I = 0;
  88. static void
  89. WriteDBGBuf(char *s, unsigned char *src, unsigned short n )
  90. {
  91. char *p = src;
  92. // XXX: We need a spin lock here if we ever use this again
  93. while (*s) { // copy label
  94. DBGBuf[I] = *s++;
  95. I = I++ & 0x3fff;
  96. }
  97. while (n--) { // copy data
  98. DBGBuf[I] = *p++;
  99. I = I++ & 0x3fff;
  100. }
  101. }
  102. static void
  103. fatality(i2eBordStrPtr pB )
  104. {
  105. int i;
  106. for (i=0;i<sizeof(DBGBuf);i++) {
  107. if ((i%16) == 0)
  108. printk("\n%4x:",i);
  109. printk("%02x ",DBGBuf[i]);
  110. }
  111. printk("\n");
  112. for (i=0;i<sizeof(DBGBuf);i++) {
  113. if ((i%16) == 0)
  114. printk("\n%4x:",i);
  115. if (DBGBuf[i] >= ' ' && DBGBuf[i] <= '~') {
  116. printk(" %c ",DBGBuf[i]);
  117. } else {
  118. printk(" . ");
  119. }
  120. }
  121. printk("\n");
  122. printk("Last index %x\n",I);
  123. }
  124. #endif /* DEBUG_FIFO */
  125. //********
  126. //* Code *
  127. //********
  128. static inline int
  129. i2Validate ( i2ChanStrPtr pCh )
  130. {
  131. //ip2trace(pCh->port_index, ITRC_VERIFY,ITRC_ENTER,2,pCh->validity,
  132. // (CHANNEL_MAGIC | CHANNEL_SUPPORT));
  133. return ((pCh->validity & (CHANNEL_MAGIC_BITS | CHANNEL_SUPPORT))
  134. == (CHANNEL_MAGIC | CHANNEL_SUPPORT));
  135. }
  136. static void iiSendPendingMail_t(unsigned long data)
  137. {
  138. i2eBordStrPtr pB = (i2eBordStrPtr)data;
  139. iiSendPendingMail(pB);
  140. }
  141. //******************************************************************************
  142. // Function: iiSendPendingMail(pB)
  143. // Parameters: Pointer to a board structure
  144. // Returns: Nothing
  145. //
  146. // Description:
  147. // If any outgoing mail bits are set and there is outgoing mailbox is empty,
  148. // send the mail and clear the bits.
  149. //******************************************************************************
  150. static void
  151. iiSendPendingMail(i2eBordStrPtr pB)
  152. {
  153. if (pB->i2eOutMailWaiting && (!pB->i2eWaitingForEmptyFifo) )
  154. {
  155. if (iiTrySendMail(pB, pB->i2eOutMailWaiting))
  156. {
  157. /* If we were already waiting for fifo to empty,
  158. * or just sent MB_OUT_STUFFED, then we are
  159. * still waiting for it to empty, until we should
  160. * receive an MB_IN_STRIPPED from the board.
  161. */
  162. pB->i2eWaitingForEmptyFifo |=
  163. (pB->i2eOutMailWaiting & MB_OUT_STUFFED);
  164. pB->i2eOutMailWaiting = 0;
  165. pB->SendPendingRetry = 0;
  166. } else {
  167. /* The only time we hit this area is when "iiTrySendMail" has
  168. failed. That only occurs when the outbound mailbox is
  169. still busy with the last message. We take a short breather
  170. to let the board catch up with itself and then try again.
  171. 16 Retries is the limit - then we got a borked board.
  172. /\/\|=mhw=|\/\/ */
  173. if( ++pB->SendPendingRetry < 16 ) {
  174. setup_timer(&pB->SendPendingTimer,
  175. iiSendPendingMail_t, (unsigned long)pB);
  176. mod_timer(&pB->SendPendingTimer, jiffies + 1);
  177. } else {
  178. printk( KERN_ERR "IP2: iiSendPendingMail unable to queue outbound mail\n" );
  179. }
  180. }
  181. }
  182. }
  183. //******************************************************************************
  184. // Function: i2InitChannels(pB, nChannels, pCh)
  185. // Parameters: Pointer to Ellis Board structure
  186. // Number of channels to initialize
  187. // Pointer to first element in an array of channel structures
  188. // Returns: Success or failure
  189. //
  190. // Description:
  191. //
  192. // This function patches pointers, back-pointers, and initializes all the
  193. // elements in the channel structure array.
  194. //
  195. // This should be run after the board structure is initialized, through having
  196. // loaded the standard loadware (otherwise it complains).
  197. //
  198. // In any case, it must be done before any serious work begins initializing the
  199. // irq's or sending commands...
  200. //
  201. //******************************************************************************
  202. static int
  203. i2InitChannels ( i2eBordStrPtr pB, int nChannels, i2ChanStrPtr pCh)
  204. {
  205. int index, stuffIndex;
  206. i2ChanStrPtr *ppCh;
  207. if (pB->i2eValid != I2E_MAGIC) {
  208. COMPLETE(pB, I2EE_BADMAGIC);
  209. }
  210. if (pB->i2eState != II_STATE_STDLOADED) {
  211. COMPLETE(pB, I2EE_BADSTATE);
  212. }
  213. LOCK_INIT(&pB->read_fifo_spinlock);
  214. LOCK_INIT(&pB->write_fifo_spinlock);
  215. LOCK_INIT(&pB->Dbuf_spinlock);
  216. LOCK_INIT(&pB->Bbuf_spinlock);
  217. LOCK_INIT(&pB->Fbuf_spinlock);
  218. // NO LOCK needed yet - this is init
  219. pB->i2eChannelPtr = pCh;
  220. pB->i2eChannelCnt = nChannels;
  221. pB->i2Fbuf_strip = pB->i2Fbuf_stuff = 0;
  222. pB->i2Dbuf_strip = pB->i2Dbuf_stuff = 0;
  223. pB->i2Bbuf_strip = pB->i2Bbuf_stuff = 0;
  224. pB->SendPendingRetry = 0;
  225. memset ( pCh, 0, sizeof (i2ChanStr) * nChannels );
  226. for (index = stuffIndex = 0, ppCh = (i2ChanStrPtr *)(pB->i2Fbuf);
  227. nChannels && index < ABS_MOST_PORTS;
  228. index++)
  229. {
  230. if ( !(pB->i2eChannelMap[index >> 4] & (1 << (index & 0xf)) ) ) {
  231. continue;
  232. }
  233. LOCK_INIT(&pCh->Ibuf_spinlock);
  234. LOCK_INIT(&pCh->Obuf_spinlock);
  235. LOCK_INIT(&pCh->Cbuf_spinlock);
  236. LOCK_INIT(&pCh->Pbuf_spinlock);
  237. // NO LOCK needed yet - this is init
  238. // Set up validity flag according to support level
  239. if (pB->i2eGoodMap[index >> 4] & (1 << (index & 0xf)) ) {
  240. pCh->validity = CHANNEL_MAGIC | CHANNEL_SUPPORT;
  241. } else {
  242. pCh->validity = CHANNEL_MAGIC;
  243. }
  244. pCh->pMyBord = pB; /* Back-pointer */
  245. // Prepare an outgoing flow-control packet to send as soon as the chance
  246. // occurs.
  247. if ( pCh->validity & CHANNEL_SUPPORT ) {
  248. pCh->infl.hd.i2sChannel = index;
  249. pCh->infl.hd.i2sCount = 5;
  250. pCh->infl.hd.i2sType = PTYPE_BYPASS;
  251. pCh->infl.fcmd = 37;
  252. pCh->infl.asof = 0;
  253. pCh->infl.room = IBUF_SIZE - 1;
  254. pCh->whenSendFlow = (IBUF_SIZE/5)*4; // when 80% full
  255. // The following is similar to calling i2QueueNeeds, except that this
  256. // is done in longhand, since we are setting up initial conditions on
  257. // many channels at once.
  258. pCh->channelNeeds = NEED_FLOW; // Since starting from scratch
  259. pCh->sinceLastFlow = 0; // No bytes received since last flow
  260. // control packet was queued
  261. stuffIndex++;
  262. *ppCh++ = pCh; // List this channel as needing
  263. // initial flow control packet sent
  264. }
  265. // Don't allow anything to be sent until the status packets come in from
  266. // the board.
  267. pCh->outfl.asof = 0;
  268. pCh->outfl.room = 0;
  269. // Initialize all the ring buffers
  270. pCh->Ibuf_stuff = pCh->Ibuf_strip = 0;
  271. pCh->Obuf_stuff = pCh->Obuf_strip = 0;
  272. pCh->Cbuf_stuff = pCh->Cbuf_strip = 0;
  273. memset( &pCh->icount, 0, sizeof (struct async_icount) );
  274. pCh->hotKeyIn = HOT_CLEAR;
  275. pCh->channelOptions = 0;
  276. pCh->bookMarks = 0;
  277. init_waitqueue_head(&pCh->pBookmarkWait);
  278. init_waitqueue_head(&pCh->open_wait);
  279. init_waitqueue_head(&pCh->close_wait);
  280. init_waitqueue_head(&pCh->delta_msr_wait);
  281. // Set base and divisor so default custom rate is 9600
  282. pCh->BaudBase = 921600; // MAX for ST654, changed after we get
  283. pCh->BaudDivisor = 96; // the boxids (UART types) later
  284. pCh->dataSetIn = 0;
  285. pCh->dataSetOut = 0;
  286. pCh->wopen = 0;
  287. pCh->throttled = 0;
  288. pCh->speed = CBR_9600;
  289. pCh->flags = 0;
  290. pCh->ClosingDelay = 5*HZ/10;
  291. pCh->ClosingWaitTime = 30*HZ;
  292. // Initialize task queue objects
  293. INIT_WORK(&pCh->tqueue_input, do_input);
  294. INIT_WORK(&pCh->tqueue_status, do_status);
  295. #ifdef IP2DEBUG_TRACE
  296. pCh->trace = ip2trace;
  297. #endif
  298. ++pCh;
  299. --nChannels;
  300. }
  301. // No need to check for wrap here; this is initialization.
  302. pB->i2Fbuf_stuff = stuffIndex;
  303. COMPLETE(pB, I2EE_GOOD);
  304. }
  305. //******************************************************************************
  306. // Function: i2DeQueueNeeds(pB, type)
  307. // Parameters: Pointer to a board structure
  308. // type bit map: may include NEED_INLINE, NEED_BYPASS, or NEED_FLOW
  309. // Returns:
  310. // Pointer to a channel structure
  311. //
  312. // Description: Returns pointer struct of next channel that needs service of
  313. // the type specified. Otherwise returns a NULL reference.
  314. //
  315. //******************************************************************************
  316. static i2ChanStrPtr
  317. i2DeQueueNeeds(i2eBordStrPtr pB, int type)
  318. {
  319. unsigned short queueIndex;
  320. unsigned long flags;
  321. i2ChanStrPtr pCh = NULL;
  322. switch(type) {
  323. case NEED_INLINE:
  324. WRITE_LOCK_IRQSAVE(&pB->Dbuf_spinlock,flags);
  325. if ( pB->i2Dbuf_stuff != pB->i2Dbuf_strip)
  326. {
  327. queueIndex = pB->i2Dbuf_strip;
  328. pCh = pB->i2Dbuf[queueIndex];
  329. queueIndex++;
  330. if (queueIndex >= CH_QUEUE_SIZE) {
  331. queueIndex = 0;
  332. }
  333. pB->i2Dbuf_strip = queueIndex;
  334. pCh->channelNeeds &= ~NEED_INLINE;
  335. }
  336. WRITE_UNLOCK_IRQRESTORE(&pB->Dbuf_spinlock,flags);
  337. break;
  338. case NEED_BYPASS:
  339. WRITE_LOCK_IRQSAVE(&pB->Bbuf_spinlock,flags);
  340. if (pB->i2Bbuf_stuff != pB->i2Bbuf_strip)
  341. {
  342. queueIndex = pB->i2Bbuf_strip;
  343. pCh = pB->i2Bbuf[queueIndex];
  344. queueIndex++;
  345. if (queueIndex >= CH_QUEUE_SIZE) {
  346. queueIndex = 0;
  347. }
  348. pB->i2Bbuf_strip = queueIndex;
  349. pCh->channelNeeds &= ~NEED_BYPASS;
  350. }
  351. WRITE_UNLOCK_IRQRESTORE(&pB->Bbuf_spinlock,flags);
  352. break;
  353. case NEED_FLOW:
  354. WRITE_LOCK_IRQSAVE(&pB->Fbuf_spinlock,flags);
  355. if (pB->i2Fbuf_stuff != pB->i2Fbuf_strip)
  356. {
  357. queueIndex = pB->i2Fbuf_strip;
  358. pCh = pB->i2Fbuf[queueIndex];
  359. queueIndex++;
  360. if (queueIndex >= CH_QUEUE_SIZE) {
  361. queueIndex = 0;
  362. }
  363. pB->i2Fbuf_strip = queueIndex;
  364. pCh->channelNeeds &= ~NEED_FLOW;
  365. }
  366. WRITE_UNLOCK_IRQRESTORE(&pB->Fbuf_spinlock,flags);
  367. break;
  368. default:
  369. printk(KERN_ERR "i2DeQueueNeeds called with bad type:%x\n",type);
  370. break;
  371. }
  372. return pCh;
  373. }
  374. //******************************************************************************
  375. // Function: i2QueueNeeds(pB, pCh, type)
  376. // Parameters: Pointer to a board structure
  377. // Pointer to a channel structure
  378. // type bit map: may include NEED_INLINE, NEED_BYPASS, or NEED_FLOW
  379. // Returns: Nothing
  380. //
  381. // Description:
  382. // For each type of need selected, if the given channel is not already in the
  383. // queue, adds it, and sets the flag indicating it is in the queue.
  384. //******************************************************************************
  385. static void
  386. i2QueueNeeds(i2eBordStrPtr pB, i2ChanStrPtr pCh, int type)
  387. {
  388. unsigned short queueIndex;
  389. unsigned long flags;
  390. // We turn off all the interrupts during this brief process, since the
  391. // interrupt-level code might want to put things on the queue as well.
  392. switch (type) {
  393. case NEED_INLINE:
  394. WRITE_LOCK_IRQSAVE(&pB->Dbuf_spinlock,flags);
  395. if ( !(pCh->channelNeeds & NEED_INLINE) )
  396. {
  397. pCh->channelNeeds |= NEED_INLINE;
  398. queueIndex = pB->i2Dbuf_stuff;
  399. pB->i2Dbuf[queueIndex++] = pCh;
  400. if (queueIndex >= CH_QUEUE_SIZE)
  401. queueIndex = 0;
  402. pB->i2Dbuf_stuff = queueIndex;
  403. }
  404. WRITE_UNLOCK_IRQRESTORE(&pB->Dbuf_spinlock,flags);
  405. break;
  406. case NEED_BYPASS:
  407. WRITE_LOCK_IRQSAVE(&pB->Bbuf_spinlock,flags);
  408. if ((type & NEED_BYPASS) && !(pCh->channelNeeds & NEED_BYPASS))
  409. {
  410. pCh->channelNeeds |= NEED_BYPASS;
  411. queueIndex = pB->i2Bbuf_stuff;
  412. pB->i2Bbuf[queueIndex++] = pCh;
  413. if (queueIndex >= CH_QUEUE_SIZE)
  414. queueIndex = 0;
  415. pB->i2Bbuf_stuff = queueIndex;
  416. }
  417. WRITE_UNLOCK_IRQRESTORE(&pB->Bbuf_spinlock,flags);
  418. break;
  419. case NEED_FLOW:
  420. WRITE_LOCK_IRQSAVE(&pB->Fbuf_spinlock,flags);
  421. if ((type & NEED_FLOW) && !(pCh->channelNeeds & NEED_FLOW))
  422. {
  423. pCh->channelNeeds |= NEED_FLOW;
  424. queueIndex = pB->i2Fbuf_stuff;
  425. pB->i2Fbuf[queueIndex++] = pCh;
  426. if (queueIndex >= CH_QUEUE_SIZE)
  427. queueIndex = 0;
  428. pB->i2Fbuf_stuff = queueIndex;
  429. }
  430. WRITE_UNLOCK_IRQRESTORE(&pB->Fbuf_spinlock,flags);
  431. break;
  432. case NEED_CREDIT:
  433. pCh->channelNeeds |= NEED_CREDIT;
  434. break;
  435. default:
  436. printk(KERN_ERR "i2QueueNeeds called with bad type:%x\n",type);
  437. break;
  438. }
  439. return;
  440. }
  441. //******************************************************************************
  442. // Function: i2QueueCommands(type, pCh, timeout, nCommands, pCs,...)
  443. // Parameters: type - PTYPE_BYPASS or PTYPE_INLINE
  444. // pointer to the channel structure
  445. // maximum period to wait
  446. // number of commands (n)
  447. // n commands
  448. // Returns: Number of commands sent, or -1 for error
  449. //
  450. // get board lock before calling
  451. //
  452. // Description:
  453. // Queues up some commands to be sent to a channel. To send possibly several
  454. // bypass or inline commands to the given channel. The timeout parameter
  455. // indicates how many HUNDREDTHS OF SECONDS to wait until there is room:
  456. // 0 = return immediately if no room, -ive = wait forever, +ive = number of
  457. // 1/100 seconds to wait. Return values:
  458. // -1 Some kind of nasty error: bad channel structure or invalid arguments.
  459. // 0 No room to send all the commands
  460. // (+) Number of commands sent
  461. //******************************************************************************
  462. static int
  463. i2QueueCommands(int type, i2ChanStrPtr pCh, int timeout, int nCommands,
  464. cmdSyntaxPtr pCs0,...)
  465. {
  466. int totalsize = 0;
  467. int blocksize;
  468. int lastended;
  469. cmdSyntaxPtr *ppCs;
  470. cmdSyntaxPtr pCs;
  471. int count;
  472. int flag;
  473. i2eBordStrPtr pB;
  474. unsigned short maxBlock;
  475. unsigned short maxBuff;
  476. short bufroom;
  477. unsigned short stuffIndex;
  478. unsigned char *pBuf;
  479. unsigned char *pInsert;
  480. unsigned char *pDest, *pSource;
  481. unsigned short channel;
  482. int cnt;
  483. unsigned long flags = 0;
  484. rwlock_t *lock_var_p = NULL;
  485. // Make sure the channel exists, otherwise do nothing
  486. if ( !i2Validate ( pCh ) ) {
  487. return -1;
  488. }
  489. ip2trace (CHANN, ITRC_QUEUE, ITRC_ENTER, 0 );
  490. pB = pCh->pMyBord;
  491. // Board must also exist, and THE INTERRUPT COMMAND ALREADY SENT
  492. if (pB->i2eValid != I2E_MAGIC || pB->i2eUsingIrq == IRQ_UNDEFINED) {
  493. return -2;
  494. }
  495. // If the board has gone fatal, return bad, and also hit the trap routine if
  496. // it exists.
  497. if (pB->i2eFatal) {
  498. if ( pB->i2eFatalTrap ) {
  499. (*(pB)->i2eFatalTrap)(pB);
  500. }
  501. return -3;
  502. }
  503. // Set up some variables, Which buffers are we using? How big are they?
  504. switch(type)
  505. {
  506. case PTYPE_INLINE:
  507. flag = INL;
  508. maxBlock = MAX_OBUF_BLOCK;
  509. maxBuff = OBUF_SIZE;
  510. pBuf = pCh->Obuf;
  511. break;
  512. case PTYPE_BYPASS:
  513. flag = BYP;
  514. maxBlock = MAX_CBUF_BLOCK;
  515. maxBuff = CBUF_SIZE;
  516. pBuf = pCh->Cbuf;
  517. break;
  518. default:
  519. return -4;
  520. }
  521. // Determine the total size required for all the commands
  522. totalsize = blocksize = sizeof(i2CmdHeader);
  523. lastended = 0;
  524. ppCs = &pCs0;
  525. for ( count = nCommands; count; count--, ppCs++)
  526. {
  527. pCs = *ppCs;
  528. cnt = pCs->length;
  529. // Will a new block be needed for this one?
  530. // Two possible reasons: too
  531. // big or previous command has to be at the end of a packet.
  532. if ((blocksize + cnt > maxBlock) || lastended) {
  533. blocksize = sizeof(i2CmdHeader);
  534. totalsize += sizeof(i2CmdHeader);
  535. }
  536. totalsize += cnt;
  537. blocksize += cnt;
  538. // If this command had to end a block, then we will make sure to
  539. // account for it should there be any more blocks.
  540. lastended = pCs->flags & END;
  541. }
  542. for (;;) {
  543. // Make sure any pending flush commands go out before we add more data.
  544. if ( !( pCh->flush_flags && i2RetryFlushOutput( pCh ) ) ) {
  545. // How much room (this time through) ?
  546. switch(type) {
  547. case PTYPE_INLINE:
  548. lock_var_p = &pCh->Obuf_spinlock;
  549. WRITE_LOCK_IRQSAVE(lock_var_p,flags);
  550. stuffIndex = pCh->Obuf_stuff;
  551. bufroom = pCh->Obuf_strip - stuffIndex;
  552. break;
  553. case PTYPE_BYPASS:
  554. lock_var_p = &pCh->Cbuf_spinlock;
  555. WRITE_LOCK_IRQSAVE(lock_var_p,flags);
  556. stuffIndex = pCh->Cbuf_stuff;
  557. bufroom = pCh->Cbuf_strip - stuffIndex;
  558. break;
  559. default:
  560. return -5;
  561. }
  562. if (--bufroom < 0) {
  563. bufroom += maxBuff;
  564. }
  565. ip2trace (CHANN, ITRC_QUEUE, 2, 1, bufroom );
  566. // Check for overflow
  567. if (totalsize <= bufroom) {
  568. // Normal Expected path - We still hold LOCK
  569. break; /* from for()- Enough room: goto proceed */
  570. }
  571. }
  572. ip2trace (CHANN, ITRC_QUEUE, 3, 1, totalsize );
  573. // Prepare to wait for buffers to empty
  574. WRITE_UNLOCK_IRQRESTORE(lock_var_p,flags);
  575. serviceOutgoingFifo(pB); // Dump what we got
  576. if (timeout == 0) {
  577. return 0; // Tired of waiting
  578. }
  579. if (timeout > 0)
  580. timeout--; // So negative values == forever
  581. if (!in_interrupt()) {
  582. schedule_timeout_interruptible(1); // short nap
  583. } else {
  584. // we cannot sched/sleep in interrrupt silly
  585. return 0;
  586. }
  587. if (signal_pending(current)) {
  588. return 0; // Wake up! Time to die!!!
  589. }
  590. ip2trace (CHANN, ITRC_QUEUE, 4, 0 );
  591. } // end of for(;;)
  592. // At this point we have room and the lock - stick them in.
  593. channel = pCh->infl.hd.i2sChannel;
  594. pInsert = &pBuf[stuffIndex]; // Pointer to start of packet
  595. pDest = CMD_OF(pInsert); // Pointer to start of command
  596. // When we start counting, the block is the size of the header
  597. for (blocksize = sizeof(i2CmdHeader), count = nCommands,
  598. lastended = 0, ppCs = &pCs0;
  599. count;
  600. count--, ppCs++)
  601. {
  602. pCs = *ppCs; // Points to command protocol structure
  603. // If this is a bookmark request command, post the fact that a bookmark
  604. // request is pending. NOTE THIS TRICK ONLY WORKS BECAUSE CMD_BMARK_REQ
  605. // has no parameters! The more general solution would be to reference
  606. // pCs->cmd[0].
  607. if (pCs == CMD_BMARK_REQ) {
  608. pCh->bookMarks++;
  609. ip2trace (CHANN, ITRC_DRAIN, 30, 1, pCh->bookMarks );
  610. }
  611. cnt = pCs->length;
  612. // If this command would put us over the maximum block size or
  613. // if the last command had to be at the end of a block, we end
  614. // the existing block here and start a new one.
  615. if ((blocksize + cnt > maxBlock) || lastended) {
  616. ip2trace (CHANN, ITRC_QUEUE, 5, 0 );
  617. PTYPE_OF(pInsert) = type;
  618. CHANNEL_OF(pInsert) = channel;
  619. // count here does not include the header
  620. CMD_COUNT_OF(pInsert) = blocksize - sizeof(i2CmdHeader);
  621. stuffIndex += blocksize;
  622. if(stuffIndex >= maxBuff) {
  623. stuffIndex = 0;
  624. pInsert = pBuf;
  625. }
  626. pInsert = &pBuf[stuffIndex]; // Pointer to start of next pkt
  627. pDest = CMD_OF(pInsert);
  628. blocksize = sizeof(i2CmdHeader);
  629. }
  630. // Now we know there is room for this one in the current block
  631. blocksize += cnt; // Total bytes in this command
  632. pSource = pCs->cmd; // Copy the command into the buffer
  633. while (cnt--) {
  634. *pDest++ = *pSource++;
  635. }
  636. // If this command had to end a block, then we will make sure to account
  637. // for it should there be any more blocks.
  638. lastended = pCs->flags & END;
  639. } // end for
  640. // Clean up the final block by writing header, etc
  641. PTYPE_OF(pInsert) = type;
  642. CHANNEL_OF(pInsert) = channel;
  643. // count here does not include the header
  644. CMD_COUNT_OF(pInsert) = blocksize - sizeof(i2CmdHeader);
  645. stuffIndex += blocksize;
  646. if(stuffIndex >= maxBuff) {
  647. stuffIndex = 0;
  648. pInsert = pBuf;
  649. }
  650. // Updates the index, and post the need for service. When adding these to
  651. // the queue of channels, we turn off the interrupt while doing so,
  652. // because at interrupt level we might want to push a channel back to the
  653. // end of the queue.
  654. switch(type)
  655. {
  656. case PTYPE_INLINE:
  657. pCh->Obuf_stuff = stuffIndex; // Store buffer pointer
  658. WRITE_UNLOCK_IRQRESTORE(&pCh->Obuf_spinlock,flags);
  659. pB->debugInlineQueued++;
  660. // Add the channel pointer to list of channels needing service (first
  661. // come...), if it's not already there.
  662. i2QueueNeeds(pB, pCh, NEED_INLINE);
  663. break;
  664. case PTYPE_BYPASS:
  665. pCh->Cbuf_stuff = stuffIndex; // Store buffer pointer
  666. WRITE_UNLOCK_IRQRESTORE(&pCh->Cbuf_spinlock,flags);
  667. pB->debugBypassQueued++;
  668. // Add the channel pointer to list of channels needing service (first
  669. // come...), if it's not already there.
  670. i2QueueNeeds(pB, pCh, NEED_BYPASS);
  671. break;
  672. }
  673. ip2trace (CHANN, ITRC_QUEUE, ITRC_RETURN, 1, nCommands );
  674. return nCommands; // Good status: number of commands sent
  675. }
  676. //******************************************************************************
  677. // Function: i2GetStatus(pCh,resetBits)
  678. // Parameters: Pointer to a channel structure
  679. // Bit map of status bits to clear
  680. // Returns: Bit map of current status bits
  681. //
  682. // Description:
  683. // Returns the state of data set signals, and whether a break has been received,
  684. // (see i2lib.h for bit-mapped result). resetBits is a bit-map of any status
  685. // bits to be cleared: I2_BRK, I2_PAR, I2_FRA, I2_OVR,... These are cleared
  686. // AFTER the condition is passed. If pCh does not point to a valid channel,
  687. // returns -1 (which would be impossible otherwise.
  688. //******************************************************************************
  689. static int
  690. i2GetStatus(i2ChanStrPtr pCh, int resetBits)
  691. {
  692. unsigned short status;
  693. i2eBordStrPtr pB;
  694. ip2trace (CHANN, ITRC_STATUS, ITRC_ENTER, 2, pCh->dataSetIn, resetBits );
  695. // Make sure the channel exists, otherwise do nothing */
  696. if ( !i2Validate ( pCh ) )
  697. return -1;
  698. pB = pCh->pMyBord;
  699. status = pCh->dataSetIn;
  700. // Clear any specified error bits: but note that only actual error bits can
  701. // be cleared, regardless of the value passed.
  702. if (resetBits)
  703. {
  704. pCh->dataSetIn &= ~(resetBits & (I2_BRK | I2_PAR | I2_FRA | I2_OVR));
  705. pCh->dataSetIn &= ~(I2_DDCD | I2_DCTS | I2_DDSR | I2_DRI);
  706. }
  707. ip2trace (CHANN, ITRC_STATUS, ITRC_RETURN, 1, pCh->dataSetIn );
  708. return status;
  709. }
  710. //******************************************************************************
  711. // Function: i2Input(pChpDest,count)
  712. // Parameters: Pointer to a channel structure
  713. // Pointer to data buffer
  714. // Number of bytes to read
  715. // Returns: Number of bytes read, or -1 for error
  716. //
  717. // Description:
  718. // Strips data from the input buffer and writes it to pDest. If there is a
  719. // collosal blunder, (invalid structure pointers or the like), returns -1.
  720. // Otherwise, returns the number of bytes read.
  721. //******************************************************************************
  722. static int
  723. i2Input(i2ChanStrPtr pCh)
  724. {
  725. int amountToMove;
  726. unsigned short stripIndex;
  727. int count;
  728. unsigned long flags = 0;
  729. ip2trace (CHANN, ITRC_INPUT, ITRC_ENTER, 0);
  730. // Ensure channel structure seems real
  731. if ( !i2Validate( pCh ) ) {
  732. count = -1;
  733. goto i2Input_exit;
  734. }
  735. WRITE_LOCK_IRQSAVE(&pCh->Ibuf_spinlock,flags);
  736. // initialize some accelerators and private copies
  737. stripIndex = pCh->Ibuf_strip;
  738. count = pCh->Ibuf_stuff - stripIndex;
  739. // If buffer is empty or requested data count was 0, (trivial case) return
  740. // without any further thought.
  741. if ( count == 0 ) {
  742. WRITE_UNLOCK_IRQRESTORE(&pCh->Ibuf_spinlock,flags);
  743. goto i2Input_exit;
  744. }
  745. // Adjust for buffer wrap
  746. if ( count < 0 ) {
  747. count += IBUF_SIZE;
  748. }
  749. // Don't give more than can be taken by the line discipline
  750. amountToMove = pCh->pTTY->receive_room;
  751. if (count > amountToMove) {
  752. count = amountToMove;
  753. }
  754. // How much could we copy without a wrap?
  755. amountToMove = IBUF_SIZE - stripIndex;
  756. if (amountToMove > count) {
  757. amountToMove = count;
  758. }
  759. // Move the first block
  760. pCh->pTTY->ldisc.receive_buf( pCh->pTTY,
  761. &(pCh->Ibuf[stripIndex]), NULL, amountToMove );
  762. // If we needed to wrap, do the second data move
  763. if (count > amountToMove) {
  764. pCh->pTTY->ldisc.receive_buf( pCh->pTTY,
  765. pCh->Ibuf, NULL, count - amountToMove );
  766. }
  767. // Bump and wrap the stripIndex all at once by the amount of data read. This
  768. // method is good regardless of whether the data was in one or two pieces.
  769. stripIndex += count;
  770. if (stripIndex >= IBUF_SIZE) {
  771. stripIndex -= IBUF_SIZE;
  772. }
  773. pCh->Ibuf_strip = stripIndex;
  774. // Update our flow control information and possibly queue ourselves to send
  775. // it, depending on how much data has been stripped since the last time a
  776. // packet was sent.
  777. pCh->infl.asof += count;
  778. if ((pCh->sinceLastFlow += count) >= pCh->whenSendFlow) {
  779. pCh->sinceLastFlow -= pCh->whenSendFlow;
  780. WRITE_UNLOCK_IRQRESTORE(&pCh->Ibuf_spinlock,flags);
  781. i2QueueNeeds(pCh->pMyBord, pCh, NEED_FLOW);
  782. } else {
  783. WRITE_UNLOCK_IRQRESTORE(&pCh->Ibuf_spinlock,flags);
  784. }
  785. i2Input_exit:
  786. ip2trace (CHANN, ITRC_INPUT, ITRC_RETURN, 1, count);
  787. return count;
  788. }
  789. //******************************************************************************
  790. // Function: i2InputFlush(pCh)
  791. // Parameters: Pointer to a channel structure
  792. // Returns: Number of bytes stripped, or -1 for error
  793. //
  794. // Description:
  795. // Strips any data from the input buffer. If there is a collosal blunder,
  796. // (invalid structure pointers or the like), returns -1. Otherwise, returns the
  797. // number of bytes stripped.
  798. //******************************************************************************
  799. static int
  800. i2InputFlush(i2ChanStrPtr pCh)
  801. {
  802. int count;
  803. unsigned long flags;
  804. // Ensure channel structure seems real
  805. if ( !i2Validate ( pCh ) )
  806. return -1;
  807. ip2trace (CHANN, ITRC_INPUT, 10, 0);
  808. WRITE_LOCK_IRQSAVE(&pCh->Ibuf_spinlock,flags);
  809. count = pCh->Ibuf_stuff - pCh->Ibuf_strip;
  810. // Adjust for buffer wrap
  811. if (count < 0) {
  812. count += IBUF_SIZE;
  813. }
  814. // Expedient way to zero out the buffer
  815. pCh->Ibuf_strip = pCh->Ibuf_stuff;
  816. // Update our flow control information and possibly queue ourselves to send
  817. // it, depending on how much data has been stripped since the last time a
  818. // packet was sent.
  819. pCh->infl.asof += count;
  820. if ( (pCh->sinceLastFlow += count) >= pCh->whenSendFlow )
  821. {
  822. pCh->sinceLastFlow -= pCh->whenSendFlow;
  823. WRITE_UNLOCK_IRQRESTORE(&pCh->Ibuf_spinlock,flags);
  824. i2QueueNeeds(pCh->pMyBord, pCh, NEED_FLOW);
  825. } else {
  826. WRITE_UNLOCK_IRQRESTORE(&pCh->Ibuf_spinlock,flags);
  827. }
  828. ip2trace (CHANN, ITRC_INPUT, 19, 1, count);
  829. return count;
  830. }
  831. //******************************************************************************
  832. // Function: i2InputAvailable(pCh)
  833. // Parameters: Pointer to a channel structure
  834. // Returns: Number of bytes available, or -1 for error
  835. //
  836. // Description:
  837. // If there is a collosal blunder, (invalid structure pointers or the like),
  838. // returns -1. Otherwise, returns the number of bytes stripped. Otherwise,
  839. // returns the number of bytes available in the buffer.
  840. //******************************************************************************
  841. #if 0
  842. static int
  843. i2InputAvailable(i2ChanStrPtr pCh)
  844. {
  845. int count;
  846. // Ensure channel structure seems real
  847. if ( !i2Validate ( pCh ) ) return -1;
  848. // initialize some accelerators and private copies
  849. READ_LOCK_IRQSAVE(&pCh->Ibuf_spinlock,flags);
  850. count = pCh->Ibuf_stuff - pCh->Ibuf_strip;
  851. READ_UNLOCK_IRQRESTORE(&pCh->Ibuf_spinlock,flags);
  852. // Adjust for buffer wrap
  853. if (count < 0)
  854. {
  855. count += IBUF_SIZE;
  856. }
  857. return count;
  858. }
  859. #endif
  860. //******************************************************************************
  861. // Function: i2Output(pCh, pSource, count)
  862. // Parameters: Pointer to channel structure
  863. // Pointer to source data
  864. // Number of bytes to send
  865. // Returns: Number of bytes sent, or -1 for error
  866. //
  867. // Description:
  868. // Queues the data at pSource to be sent as data packets to the board. If there
  869. // is a collosal blunder, (invalid structure pointers or the like), returns -1.
  870. // Otherwise, returns the number of bytes written. What if there is not enough
  871. // room for all the data? If pCh->channelOptions & CO_NBLOCK_WRITE is set, then
  872. // we transfer as many characters as we can now, then return. If this bit is
  873. // clear (default), routine will spin along until all the data is buffered.
  874. // Should this occur, the 1-ms delay routine is called while waiting to avoid
  875. // applications that one cannot break out of.
  876. //******************************************************************************
  877. static int
  878. i2Output(i2ChanStrPtr pCh, const char *pSource, int count)
  879. {
  880. i2eBordStrPtr pB;
  881. unsigned char *pInsert;
  882. int amountToMove;
  883. int countOriginal = count;
  884. unsigned short channel;
  885. unsigned short stuffIndex;
  886. unsigned long flags;
  887. int bailout = 10;
  888. ip2trace (CHANN, ITRC_OUTPUT, ITRC_ENTER, 2, count, 0 );
  889. // Ensure channel structure seems real
  890. if ( !i2Validate ( pCh ) )
  891. return -1;
  892. // initialize some accelerators and private copies
  893. pB = pCh->pMyBord;
  894. channel = pCh->infl.hd.i2sChannel;
  895. // If the board has gone fatal, return bad, and also hit the trap routine if
  896. // it exists.
  897. if (pB->i2eFatal) {
  898. if (pB->i2eFatalTrap) {
  899. (*(pB)->i2eFatalTrap)(pB);
  900. }
  901. return -1;
  902. }
  903. // Proceed as though we would do everything
  904. while ( count > 0 ) {
  905. // How much room in output buffer is there?
  906. READ_LOCK_IRQSAVE(&pCh->Obuf_spinlock,flags);
  907. amountToMove = pCh->Obuf_strip - pCh->Obuf_stuff - 1;
  908. READ_UNLOCK_IRQRESTORE(&pCh->Obuf_spinlock,flags);
  909. if (amountToMove < 0) {
  910. amountToMove += OBUF_SIZE;
  911. }
  912. // Subtract off the headers size and see how much room there is for real
  913. // data. If this is negative, we will discover later.
  914. amountToMove -= sizeof (i2DataHeader);
  915. // Don't move more (now) than can go in a single packet
  916. if ( amountToMove > (int)(MAX_OBUF_BLOCK - sizeof(i2DataHeader)) ) {
  917. amountToMove = MAX_OBUF_BLOCK - sizeof(i2DataHeader);
  918. }
  919. // Don't move more than the count we were given
  920. if (amountToMove > count) {
  921. amountToMove = count;
  922. }
  923. // Now we know how much we must move: NB because the ring buffers have
  924. // an overflow area at the end, we needn't worry about wrapping in the
  925. // middle of a packet.
  926. // Small WINDOW here with no LOCK but I can't call Flush with LOCK
  927. // We would be flushing (or ending flush) anyway
  928. ip2trace (CHANN, ITRC_OUTPUT, 10, 1, amountToMove );
  929. if ( !(pCh->flush_flags && i2RetryFlushOutput(pCh) )
  930. && amountToMove > 0 )
  931. {
  932. WRITE_LOCK_IRQSAVE(&pCh->Obuf_spinlock,flags);
  933. stuffIndex = pCh->Obuf_stuff;
  934. // Had room to move some data: don't know whether the block size,
  935. // buffer space, or what was the limiting factor...
  936. pInsert = &(pCh->Obuf[stuffIndex]);
  937. // Set up the header
  938. CHANNEL_OF(pInsert) = channel;
  939. PTYPE_OF(pInsert) = PTYPE_DATA;
  940. TAG_OF(pInsert) = 0;
  941. ID_OF(pInsert) = ID_ORDINARY_DATA;
  942. DATA_COUNT_OF(pInsert) = amountToMove;
  943. // Move the data
  944. memcpy( (char*)(DATA_OF(pInsert)), pSource, amountToMove );
  945. // Adjust pointers and indices
  946. pSource += amountToMove;
  947. pCh->Obuf_char_count += amountToMove;
  948. stuffIndex += amountToMove + sizeof(i2DataHeader);
  949. count -= amountToMove;
  950. if (stuffIndex >= OBUF_SIZE) {
  951. stuffIndex = 0;
  952. }
  953. pCh->Obuf_stuff = stuffIndex;
  954. WRITE_UNLOCK_IRQRESTORE(&pCh->Obuf_spinlock,flags);
  955. ip2trace (CHANN, ITRC_OUTPUT, 13, 1, stuffIndex );
  956. } else {
  957. // Cannot move data
  958. // becuz we need to stuff a flush
  959. // or amount to move is <= 0
  960. ip2trace(CHANN, ITRC_OUTPUT, 14, 3,
  961. amountToMove, pB->i2eFifoRemains,
  962. pB->i2eWaitingForEmptyFifo );
  963. // Put this channel back on queue
  964. // this ultimatly gets more data or wakes write output
  965. i2QueueNeeds(pB, pCh, NEED_INLINE);
  966. if ( pB->i2eWaitingForEmptyFifo ) {
  967. ip2trace (CHANN, ITRC_OUTPUT, 16, 0 );
  968. // or schedule
  969. if (!in_interrupt()) {
  970. ip2trace (CHANN, ITRC_OUTPUT, 61, 0 );
  971. schedule_timeout_interruptible(2);
  972. if (signal_pending(current)) {
  973. break;
  974. }
  975. continue;
  976. } else {
  977. ip2trace (CHANN, ITRC_OUTPUT, 62, 0 );
  978. // let interrupt in = WAS restore_flags()
  979. // We hold no lock nor is irq off anymore???
  980. break;
  981. }
  982. break; // from while(count)
  983. }
  984. else if ( pB->i2eFifoRemains < 32 && !pB->i2eTxMailEmpty ( pB ) )
  985. {
  986. ip2trace (CHANN, ITRC_OUTPUT, 19, 2,
  987. pB->i2eFifoRemains,
  988. pB->i2eTxMailEmpty );
  989. break; // from while(count)
  990. } else if ( pCh->channelNeeds & NEED_CREDIT ) {
  991. ip2trace (CHANN, ITRC_OUTPUT, 22, 0 );
  992. break; // from while(count)
  993. } else if ( --bailout) {
  994. // Try to throw more things (maybe not us) in the fifo if we're
  995. // not already waiting for it.
  996. ip2trace (CHANN, ITRC_OUTPUT, 20, 0 );
  997. serviceOutgoingFifo(pB);
  998. //break; CONTINUE;
  999. } else {
  1000. ip2trace (CHANN, ITRC_OUTPUT, 21, 3,
  1001. pB->i2eFifoRemains,
  1002. pB->i2eOutMailWaiting,
  1003. pB->i2eWaitingForEmptyFifo );
  1004. break; // from while(count)
  1005. }
  1006. }
  1007. } // End of while(count)
  1008. i2QueueNeeds(pB, pCh, NEED_INLINE);
  1009. // We drop through either when the count expires, or when there is some
  1010. // count left, but there was a non-blocking write.
  1011. if (countOriginal > count) {
  1012. ip2trace (CHANN, ITRC_OUTPUT, 17, 2, countOriginal, count );
  1013. serviceOutgoingFifo( pB );
  1014. }
  1015. ip2trace (CHANN, ITRC_OUTPUT, ITRC_RETURN, 2, countOriginal, count );
  1016. return countOriginal - count;
  1017. }
  1018. //******************************************************************************
  1019. // Function: i2FlushOutput(pCh)
  1020. // Parameters: Pointer to a channel structure
  1021. // Returns: Nothing
  1022. //
  1023. // Description:
  1024. // Sends bypass command to start flushing (waiting possibly forever until there
  1025. // is room), then sends inline command to stop flushing output, (again waiting
  1026. // possibly forever).
  1027. //******************************************************************************
  1028. static inline void
  1029. i2FlushOutput(i2ChanStrPtr pCh)
  1030. {
  1031. ip2trace (CHANN, ITRC_FLUSH, 1, 1, pCh->flush_flags );
  1032. if (pCh->flush_flags)
  1033. return;
  1034. if ( 1 != i2QueueCommands(PTYPE_BYPASS, pCh, 0, 1, CMD_STARTFL) ) {
  1035. pCh->flush_flags = STARTFL_FLAG; // Failed - flag for later
  1036. ip2trace (CHANN, ITRC_FLUSH, 2, 0 );
  1037. } else if ( 1 != i2QueueCommands(PTYPE_INLINE, pCh, 0, 1, CMD_STOPFL) ) {
  1038. pCh->flush_flags = STOPFL_FLAG; // Failed - flag for later
  1039. ip2trace (CHANN, ITRC_FLUSH, 3, 0 );
  1040. }
  1041. }
  1042. static int
  1043. i2RetryFlushOutput(i2ChanStrPtr pCh)
  1044. {
  1045. int old_flags = pCh->flush_flags;
  1046. ip2trace (CHANN, ITRC_FLUSH, 14, 1, old_flags );
  1047. pCh->flush_flags = 0; // Clear flag so we can avoid recursion
  1048. // and queue the commands
  1049. if ( old_flags & STARTFL_FLAG ) {
  1050. if ( 1 == i2QueueCommands(PTYPE_BYPASS, pCh, 0, 1, CMD_STARTFL) ) {
  1051. old_flags = STOPFL_FLAG; //Success - send stop flush
  1052. } else {
  1053. old_flags = STARTFL_FLAG; //Failure - Flag for retry later
  1054. }
  1055. ip2trace (CHANN, ITRC_FLUSH, 15, 1, old_flags );
  1056. }
  1057. if ( old_flags & STOPFL_FLAG ) {
  1058. if (1 == i2QueueCommands(PTYPE_INLINE, pCh, 0, 1, CMD_STOPFL)) {
  1059. old_flags = 0; // Success - clear flags
  1060. }
  1061. ip2trace (CHANN, ITRC_FLUSH, 16, 1, old_flags );
  1062. }
  1063. pCh->flush_flags = old_flags;
  1064. ip2trace (CHANN, ITRC_FLUSH, 17, 1, old_flags );
  1065. return old_flags;
  1066. }
  1067. //******************************************************************************
  1068. // Function: i2DrainOutput(pCh,timeout)
  1069. // Parameters: Pointer to a channel structure
  1070. // Maximum period to wait
  1071. // Returns: ?
  1072. //
  1073. // Description:
  1074. // Uses the bookmark request command to ask the board to send a bookmark back as
  1075. // soon as all the data is completely sent.
  1076. //******************************************************************************
  1077. static void
  1078. i2DrainWakeup(unsigned long d)
  1079. {
  1080. i2ChanStrPtr pCh = (i2ChanStrPtr)d;
  1081. ip2trace (CHANN, ITRC_DRAIN, 10, 1, pCh->BookmarkTimer.expires );
  1082. pCh->BookmarkTimer.expires = 0;
  1083. wake_up_interruptible( &pCh->pBookmarkWait );
  1084. }
  1085. static void
  1086. i2DrainOutput(i2ChanStrPtr pCh, int timeout)
  1087. {
  1088. wait_queue_t wait;
  1089. i2eBordStrPtr pB;
  1090. ip2trace (CHANN, ITRC_DRAIN, ITRC_ENTER, 1, pCh->BookmarkTimer.expires);
  1091. pB = pCh->pMyBord;
  1092. // If the board has gone fatal, return bad,
  1093. // and also hit the trap routine if it exists.
  1094. if (pB->i2eFatal) {
  1095. if (pB->i2eFatalTrap) {
  1096. (*(pB)->i2eFatalTrap)(pB);
  1097. }
  1098. return;
  1099. }
  1100. if ((timeout > 0) && (pCh->BookmarkTimer.expires == 0 )) {
  1101. // One per customer (channel)
  1102. setup_timer(&pCh->BookmarkTimer, i2DrainWakeup,
  1103. (unsigned long)pCh);
  1104. ip2trace (CHANN, ITRC_DRAIN, 1, 1, pCh->BookmarkTimer.expires );
  1105. mod_timer(&pCh->BookmarkTimer, jiffies + timeout);
  1106. }
  1107. i2QueueCommands( PTYPE_INLINE, pCh, -1, 1, CMD_BMARK_REQ );
  1108. init_waitqueue_entry(&wait, current);
  1109. add_wait_queue(&(pCh->pBookmarkWait), &wait);
  1110. set_current_state( TASK_INTERRUPTIBLE );
  1111. serviceOutgoingFifo( pB );
  1112. schedule(); // Now we take our interruptible sleep on
  1113. // Clean up the queue
  1114. set_current_state( TASK_RUNNING );
  1115. remove_wait_queue(&(pCh->pBookmarkWait), &wait);
  1116. // if expires == 0 then timer poped, then do not need to del_timer
  1117. if ((timeout > 0) && pCh->BookmarkTimer.expires &&
  1118. time_before(jiffies, pCh->BookmarkTimer.expires)) {
  1119. del_timer( &(pCh->BookmarkTimer) );
  1120. pCh->BookmarkTimer.expires = 0;
  1121. ip2trace (CHANN, ITRC_DRAIN, 3, 1, pCh->BookmarkTimer.expires );
  1122. }
  1123. ip2trace (CHANN, ITRC_DRAIN, ITRC_RETURN, 1, pCh->BookmarkTimer.expires );
  1124. return;
  1125. }
  1126. //******************************************************************************
  1127. // Function: i2OutputFree(pCh)
  1128. // Parameters: Pointer to a channel structure
  1129. // Returns: Space in output buffer
  1130. //
  1131. // Description:
  1132. // Returns -1 if very gross error. Otherwise returns the amount of bytes still
  1133. // free in the output buffer.
  1134. //******************************************************************************
  1135. static int
  1136. i2OutputFree(i2ChanStrPtr pCh)
  1137. {
  1138. int amountToMove;
  1139. unsigned long flags;
  1140. // Ensure channel structure seems real
  1141. if ( !i2Validate ( pCh ) ) {
  1142. return -1;
  1143. }
  1144. READ_LOCK_IRQSAVE(&pCh->Obuf_spinlock,flags);
  1145. amountToMove = pCh->Obuf_strip - pCh->Obuf_stuff - 1;
  1146. READ_UNLOCK_IRQRESTORE(&pCh->Obuf_spinlock,flags);
  1147. if (amountToMove < 0) {
  1148. amountToMove += OBUF_SIZE;
  1149. }
  1150. // If this is negative, we will discover later
  1151. amountToMove -= sizeof(i2DataHeader);
  1152. return (amountToMove < 0) ? 0 : amountToMove;
  1153. }
  1154. static void
  1155. ip2_owake( PTTY tp)
  1156. {
  1157. i2ChanStrPtr pCh;
  1158. if (tp == NULL) return;
  1159. pCh = tp->driver_data;
  1160. ip2trace (CHANN, ITRC_SICMD, 10, 2, tp->flags,
  1161. (1 << TTY_DO_WRITE_WAKEUP) );
  1162. tty_wakeup(tp);
  1163. }
  1164. static inline void
  1165. set_baud_params(i2eBordStrPtr pB)
  1166. {
  1167. int i,j;
  1168. i2ChanStrPtr *pCh;
  1169. pCh = (i2ChanStrPtr *) pB->i2eChannelPtr;
  1170. for (i = 0; i < ABS_MAX_BOXES; i++) {
  1171. if (pB->channelBtypes.bid_value[i]) {
  1172. if (BID_HAS_654(pB->channelBtypes.bid_value[i])) {
  1173. for (j = 0; j < ABS_BIGGEST_BOX; j++) {
  1174. if (pCh[i*16+j] == NULL)
  1175. break;
  1176. (pCh[i*16+j])->BaudBase = 921600; // MAX for ST654
  1177. (pCh[i*16+j])->BaudDivisor = 96;
  1178. }
  1179. } else { // has cirrus cd1400
  1180. for (j = 0; j < ABS_BIGGEST_BOX; j++) {
  1181. if (pCh[i*16+j] == NULL)
  1182. break;
  1183. (pCh[i*16+j])->BaudBase = 115200; // MAX for CD1400
  1184. (pCh[i*16+j])->BaudDivisor = 12;
  1185. }
  1186. }
  1187. }
  1188. }
  1189. }
  1190. //******************************************************************************
  1191. // Function: i2StripFifo(pB)
  1192. // Parameters: Pointer to a board structure
  1193. // Returns: ?
  1194. //
  1195. // Description:
  1196. // Strips all the available data from the incoming FIFO, identifies the type of
  1197. // packet, and either buffers the data or does what needs to be done.
  1198. //
  1199. // Note there is no overflow checking here: if the board sends more data than it
  1200. // ought to, we will not detect it here, but blindly overflow...
  1201. //******************************************************************************
  1202. // A buffer for reading in blocks for unknown channels
  1203. static unsigned char junkBuffer[IBUF_SIZE];
  1204. // A buffer to read in a status packet. Because of the size of the count field
  1205. // for these things, the maximum packet size must be less than MAX_CMD_PACK_SIZE
  1206. static unsigned char cmdBuffer[MAX_CMD_PACK_SIZE + 4];
  1207. // This table changes the bit order from MSR order given by STAT_MODEM packet to
  1208. // status bits used in our library.
  1209. static char xlatDss[16] = {
  1210. 0 | 0 | 0 | 0 ,
  1211. 0 | 0 | 0 | I2_CTS ,
  1212. 0 | 0 | I2_DSR | 0 ,
  1213. 0 | 0 | I2_DSR | I2_CTS ,
  1214. 0 | I2_RI | 0 | 0 ,
  1215. 0 | I2_RI | 0 | I2_CTS ,
  1216. 0 | I2_RI | I2_DSR | 0 ,
  1217. 0 | I2_RI | I2_DSR | I2_CTS ,
  1218. I2_DCD | 0 | 0 | 0 ,
  1219. I2_DCD | 0 | 0 | I2_CTS ,
  1220. I2_DCD | 0 | I2_DSR | 0 ,
  1221. I2_DCD | 0 | I2_DSR | I2_CTS ,
  1222. I2_DCD | I2_RI | 0 | 0 ,
  1223. I2_DCD | I2_RI | 0 | I2_CTS ,
  1224. I2_DCD | I2_RI | I2_DSR | 0 ,
  1225. I2_DCD | I2_RI | I2_DSR | I2_CTS };
  1226. static inline void
  1227. i2StripFifo(i2eBordStrPtr pB)
  1228. {
  1229. i2ChanStrPtr pCh;
  1230. int channel;
  1231. int count;
  1232. unsigned short stuffIndex;
  1233. int amountToRead;
  1234. unsigned char *pc, *pcLimit;
  1235. unsigned char uc;
  1236. unsigned char dss_change;
  1237. unsigned long bflags,cflags;
  1238. // ip2trace (ITRC_NO_PORT, ITRC_SFIFO, ITRC_ENTER, 0 );
  1239. while (HAS_INPUT(pB)) {
  1240. // ip2trace (ITRC_NO_PORT, ITRC_SFIFO, 2, 0 );
  1241. // Process packet from fifo a one atomic unit
  1242. WRITE_LOCK_IRQSAVE(&pB->read_fifo_spinlock,bflags);
  1243. // The first word (or two bytes) will have channel number and type of
  1244. // packet, possibly other information
  1245. pB->i2eLeadoffWord[0] = iiReadWord(pB);
  1246. switch(PTYPE_OF(pB->i2eLeadoffWord))
  1247. {
  1248. case PTYPE_DATA:
  1249. pB->got_input = 1;
  1250. // ip2trace (ITRC_NO_PORT, ITRC_SFIFO, 3, 0 );
  1251. channel = CHANNEL_OF(pB->i2eLeadoffWord); /* Store channel */
  1252. count = iiReadWord(pB); /* Count is in the next word */
  1253. // NEW: Check the count for sanity! Should the hardware fail, our death
  1254. // is more pleasant. While an oversize channel is acceptable (just more
  1255. // than the driver supports), an over-length count clearly means we are
  1256. // sick!
  1257. if ( ((unsigned int)count) > IBUF_SIZE ) {
  1258. pB->i2eFatal = 2;
  1259. WRITE_UNLOCK_IRQRESTORE(&pB->read_fifo_spinlock,bflags);
  1260. return; /* Bail out ASAP */
  1261. }
  1262. // Channel is illegally big ?
  1263. if ((channel >= pB->i2eChannelCnt) ||
  1264. (NULL==(pCh = ((i2ChanStrPtr*)pB->i2eChannelPtr)[channel])))
  1265. {
  1266. iiReadBuf(pB, junkBuffer, count);
  1267. WRITE_UNLOCK_IRQRESTORE(&pB->read_fifo_spinlock,bflags);
  1268. break; /* From switch: ready for next packet */
  1269. }
  1270. // Channel should be valid, then
  1271. // If this is a hot-key, merely post its receipt for now. These are
  1272. // always supposed to be 1-byte packets, so we won't even check the
  1273. // count. Also we will post an acknowledgement to the board so that
  1274. // more data can be forthcoming. Note that we are not trying to use
  1275. // these sequences in this driver, merely to robustly ignore them.
  1276. if(ID_OF(pB->i2eLeadoffWord) == ID_HOT_KEY)
  1277. {
  1278. pCh->hotKeyIn = iiReadWord(pB) & 0xff;
  1279. WRITE_UNLOCK_IRQRESTORE(&pB->read_fifo_spinlock,bflags);
  1280. i2QueueCommands(PTYPE_BYPASS, pCh, 0, 1, CMD_HOTACK);
  1281. break; /* From the switch: ready for next packet */
  1282. }
  1283. // Normal data! We crudely assume there is room for the data in our
  1284. // buffer because the board wouldn't have exceeded his credit limit.
  1285. WRITE_LOCK_IRQSAVE(&pCh->Ibuf_spinlock,cflags);
  1286. // We have 2 locks now
  1287. stuffIndex = pCh->Ibuf_stuff;
  1288. amountToRead = IBUF_SIZE - stuffIndex;
  1289. if (amountToRead > count)
  1290. amountToRead = count;
  1291. // stuffIndex would have been already adjusted so there would
  1292. // always be room for at least one, and count is always at least
  1293. // one.
  1294. iiReadBuf(pB, &(pCh->Ibuf[stuffIndex]), amountToRead);
  1295. pCh->icount.rx += amountToRead;
  1296. // Update the stuffIndex by the amount of data moved. Note we could
  1297. // never ask for more data than would just fit. However, we might
  1298. // have read in one more byte than we wanted because the read
  1299. // rounds up to even bytes. If this byte is on the end of the
  1300. // packet, and is padding, we ignore it. If the byte is part of
  1301. // the actual data, we need to move it.
  1302. stuffIndex += amountToRead;
  1303. if (stuffIndex >= IBUF_SIZE) {
  1304. if ((amountToRead & 1) && (count > amountToRead)) {
  1305. pCh->Ibuf[0] = pCh->Ibuf[IBUF_SIZE];
  1306. amountToRead++;
  1307. stuffIndex = 1;
  1308. } else {
  1309. stuffIndex = 0;
  1310. }
  1311. }
  1312. // If there is anything left over, read it as well
  1313. if (count > amountToRead) {
  1314. amountToRead = count - amountToRead;
  1315. iiReadBuf(pB, &(pCh->Ibuf[stuffIndex]), amountToRead);
  1316. pCh->icount.rx += amountToRead;
  1317. stuffIndex += amountToRead;
  1318. }
  1319. // Update stuff index
  1320. pCh->Ibuf_stuff = stuffIndex;
  1321. WRITE_UNLOCK_IRQRESTORE(&pCh->Ibuf_spinlock,cflags);
  1322. WRITE_UNLOCK_IRQRESTORE(&pB->read_fifo_spinlock,bflags);
  1323. #ifdef USE_IQ
  1324. schedule_work(&pCh->tqueue_input);
  1325. #else
  1326. do_input(&pCh->tqueue_input);
  1327. #endif
  1328. // Note we do not need to maintain any flow-control credits at this
  1329. // time: if we were to increment .asof and decrement .room, there
  1330. // would be no net effect. Instead, when we strip data, we will
  1331. // increment .asof and leave .room unchanged.
  1332. break; // From switch: ready for next packet
  1333. case PTYPE_STATUS:
  1334. ip2trace (ITRC_NO_PORT, ITRC_SFIFO, 4, 0 );
  1335. count = CMD_COUNT_OF(pB->i2eLeadoffWord);
  1336. iiReadBuf(pB, cmdBuffer, count);
  1337. // We can release early with buffer grab
  1338. WRITE_UNLOCK_IRQRESTORE(&pB->read_fifo_spinlock,bflags);
  1339. pc = cmdBuffer;
  1340. pcLimit = &(cmdBuffer[count]);
  1341. while (pc < pcLimit) {
  1342. channel = *pc++;
  1343. ip2trace (channel, ITRC_SFIFO, 7, 2, channel, *pc );
  1344. /* check for valid channel */
  1345. if (channel < pB->i2eChannelCnt
  1346. &&
  1347. (pCh = (((i2ChanStrPtr*)pB->i2eChannelPtr)[channel])) != NULL
  1348. )
  1349. {
  1350. dss_change = 0;
  1351. switch (uc = *pc++)
  1352. {
  1353. /* Breaks and modem signals are easy: just update status */
  1354. case STAT_CTS_UP:
  1355. if ( !(pCh->dataSetIn & I2_CTS) )
  1356. {
  1357. pCh->dataSetIn |= I2_DCTS;
  1358. pCh->icount.cts++;
  1359. dss_change = 1;
  1360. }
  1361. pCh->dataSetIn |= I2_CTS;
  1362. break;
  1363. case STAT_CTS_DN:
  1364. if ( pCh->dataSetIn & I2_CTS )
  1365. {
  1366. pCh->dataSetIn |= I2_DCTS;
  1367. pCh->icount.cts++;
  1368. dss_change = 1;
  1369. }
  1370. pCh->dataSetIn &= ~I2_CTS;
  1371. break;
  1372. case STAT_DCD_UP:
  1373. ip2trace (channel, ITRC_MODEM, 1, 1, pCh->dataSetIn );
  1374. if ( !(pCh->dataSetIn & I2_DCD) )
  1375. {
  1376. ip2trace (CHANN, ITRC_MODEM, 2, 0 );
  1377. pCh->dataSetIn |= I2_DDCD;
  1378. pCh->icount.dcd++;
  1379. dss_change = 1;
  1380. }
  1381. pCh->dataSetIn |= I2_DCD;
  1382. ip2trace (channel, ITRC_MODEM, 3, 1, pCh->dataSetIn );
  1383. break;
  1384. case STAT_DCD_DN:
  1385. ip2trace (channel, ITRC_MODEM, 4, 1, pCh->dataSetIn );
  1386. if ( pCh->dataSetIn & I2_DCD )
  1387. {
  1388. ip2trace (channel, ITRC_MODEM, 5, 0 );
  1389. pCh->dataSetIn |= I2_DDCD;
  1390. pCh->icount.dcd++;
  1391. dss_change = 1;
  1392. }
  1393. pCh->dataSetIn &= ~I2_DCD;
  1394. ip2trace (channel, ITRC_MODEM, 6, 1, pCh->dataSetIn );
  1395. break;
  1396. case STAT_DSR_UP:
  1397. if ( !(pCh->dataSetIn & I2_DSR) )
  1398. {
  1399. pCh->dataSetIn |= I2_DDSR;
  1400. pCh->icount.dsr++;
  1401. dss_change = 1;
  1402. }
  1403. pCh->dataSetIn |= I2_DSR;
  1404. break;
  1405. case STAT_DSR_DN:
  1406. if ( pCh->dataSetIn & I2_DSR )
  1407. {
  1408. pCh->dataSetIn |= I2_DDSR;
  1409. pCh->icount.dsr++;
  1410. dss_change = 1;
  1411. }
  1412. pCh->dataSetIn &= ~I2_DSR;
  1413. break;
  1414. case STAT_RI_UP:
  1415. if ( !(pCh->dataSetIn & I2_RI) )
  1416. {
  1417. pCh->dataSetIn |= I2_DRI;
  1418. pCh->icount.rng++;
  1419. dss_change = 1;
  1420. }
  1421. pCh->dataSetIn |= I2_RI ;
  1422. break;
  1423. case STAT_RI_DN:
  1424. // to be compat with serial.c
  1425. //if ( pCh->dataSetIn & I2_RI )
  1426. //{
  1427. // pCh->dataSetIn |= I2_DRI;
  1428. // pCh->icount.rng++;
  1429. // dss_change = 1;
  1430. //}
  1431. pCh->dataSetIn &= ~I2_RI ;
  1432. break;
  1433. case STAT_BRK_DET:
  1434. pCh->dataSetIn |= I2_BRK;
  1435. pCh->icount.brk++;
  1436. dss_change = 1;
  1437. break;
  1438. // Bookmarks? one less request we're waiting for
  1439. case STAT_BMARK:
  1440. pCh->bookMarks--;
  1441. if (pCh->bookMarks <= 0 ) {
  1442. pCh->bookMarks = 0;
  1443. wake_up_interruptible( &pCh->pBookmarkWait );
  1444. ip2trace (channel, ITRC_DRAIN, 20, 1, pCh->BookmarkTimer.expires );
  1445. }
  1446. break;
  1447. // Flow control packets? Update the new credits, and if
  1448. // someone was waiting for output, queue him up again.
  1449. case STAT_FLOW:
  1450. pCh->outfl.room =
  1451. ((flowStatPtr)pc)->room -
  1452. (pCh->outfl.asof - ((flowStatPtr)pc)->asof);
  1453. ip2trace (channel, ITRC_STFLW, 1, 1, pCh->outfl.room );
  1454. if (pCh->channelNeeds & NEED_CREDIT)
  1455. {
  1456. ip2trace (channel, ITRC_STFLW, 2, 1, pCh->channelNeeds);
  1457. pCh->channelNeeds &= ~NEED_CREDIT;
  1458. i2QueueNeeds(pB, pCh, NEED_INLINE);
  1459. if ( pCh->pTTY )
  1460. ip2_owake(pCh->pTTY);
  1461. }
  1462. ip2trace (channel, ITRC_STFLW, 3, 1, pCh->channelNeeds);
  1463. pc += sizeof(flowStat);
  1464. break;
  1465. /* Special packets: */
  1466. /* Just copy the information into the channel structure */
  1467. case STAT_STATUS:
  1468. pCh->channelStatus = *((debugStatPtr)pc);
  1469. pc += sizeof(debugStat);
  1470. break;
  1471. case STAT_TXCNT:
  1472. pCh->channelTcount = *((cntStatPtr)pc);
  1473. pc += sizeof(cntStat);
  1474. break;
  1475. case STAT_RXCNT:
  1476. pCh->channelRcount = *((cntStatPtr)pc);
  1477. pc += sizeof(cntStat);
  1478. break;
  1479. case STAT_BOXIDS:
  1480. pB->channelBtypes = *((bidStatPtr)pc);
  1481. pc += sizeof(bidStat);
  1482. set_baud_params(pB);
  1483. break;
  1484. case STAT_HWFAIL:
  1485. i2QueueCommands (PTYPE_INLINE, pCh, 0, 1, CMD_HW_TEST);
  1486. pCh->channelFail = *((failStatPtr)pc);
  1487. pc += sizeof(failStat);
  1488. break;
  1489. /* No explicit match? then
  1490. * Might be an error packet...
  1491. */
  1492. default:
  1493. switch (uc & STAT_MOD_ERROR)
  1494. {
  1495. case STAT_ERROR:
  1496. if (uc & STAT_E_PARITY) {
  1497. pCh->dataSetIn |= I2_PAR;
  1498. pCh->icount.parity++;
  1499. }
  1500. if (uc & STAT_E_FRAMING){
  1501. pCh->dataSetIn |= I2_FRA;
  1502. pCh->icount.frame++;
  1503. }
  1504. if (uc & STAT_E_OVERRUN){
  1505. pCh->dataSetIn |= I2_OVR;
  1506. pCh->icount.overrun++;
  1507. }
  1508. break;
  1509. case STAT_MODEM:
  1510. // the answer to DSS_NOW request (not change)
  1511. pCh->dataSetIn = (pCh->dataSetIn
  1512. & ~(I2_RI | I2_CTS | I2_DCD | I2_DSR) )
  1513. | xlatDss[uc & 0xf];
  1514. wake_up_interruptible ( &pCh->dss_now_wait );
  1515. default:
  1516. break;
  1517. }
  1518. } /* End of switch on status type */
  1519. if (dss_change) {
  1520. #ifdef USE_IQ
  1521. schedule_work(&pCh->tqueue_status);
  1522. #else
  1523. do_status(&pCh->tqueue_status);
  1524. #endif
  1525. }
  1526. }
  1527. else /* Or else, channel is invalid */
  1528. {
  1529. // Even though the channel is invalid, we must test the
  1530. // status to see how much additional data it has (to be
  1531. // skipped)
  1532. switch (*pc++)
  1533. {
  1534. case STAT_FLOW:
  1535. pc += 4; /* Skip the data */
  1536. break;
  1537. default:
  1538. break;
  1539. }
  1540. }
  1541. } // End of while (there is still some status packet left)
  1542. break;
  1543. default: // Neither packet? should be impossible
  1544. ip2trace (ITRC_NO_PORT, ITRC_SFIFO, 5, 1,
  1545. PTYPE_OF(pB->i2eLeadoffWord) );
  1546. break;
  1547. } // End of switch on type of packets
  1548. } //while(board HAS_INPUT)
  1549. ip2trace (ITRC_NO_PORT, ITRC_SFIFO, ITRC_RETURN, 0 );
  1550. // Send acknowledgement to the board even if there was no data!
  1551. pB->i2eOutMailWaiting |= MB_IN_STRIPPED;
  1552. return;
  1553. }
  1554. //******************************************************************************
  1555. // Function: i2Write2Fifo(pB,address,count)
  1556. // Parameters: Pointer to a board structure, source address, byte count
  1557. // Returns: bytes written
  1558. //
  1559. // Description:
  1560. // Writes count bytes to board io address(implied) from source
  1561. // Adjusts count, leaves reserve for next time around bypass cmds
  1562. //******************************************************************************
  1563. static int
  1564. i2Write2Fifo(i2eBordStrPtr pB, unsigned char *source, int count,int reserve)
  1565. {
  1566. int rc = 0;
  1567. unsigned long flags;
  1568. WRITE_LOCK_IRQSAVE(&pB->write_fifo_spinlock,flags);
  1569. if (!pB->i2eWaitingForEmptyFifo) {
  1570. if (pB->i2eFifoRemains > (count+reserve)) {
  1571. pB->i2eFifoRemains -= count;
  1572. iiWriteBuf(pB, source, count);
  1573. pB->i2eOutMailWaiting |= MB_OUT_STUFFED;
  1574. rc = count;
  1575. }
  1576. }
  1577. WRITE_UNLOCK_IRQRESTORE(&pB->write_fifo_spinlock,flags);
  1578. return rc;
  1579. }
  1580. //******************************************************************************
  1581. // Function: i2StuffFifoBypass(pB)
  1582. // Parameters: Pointer to a board structure
  1583. // Returns: Nothing
  1584. //
  1585. // Description:
  1586. // Stuffs as many bypass commands into the fifo as possible. This is simpler
  1587. // than stuffing data or inline commands to fifo, since we do not have
  1588. // flow-control to deal with.
  1589. //******************************************************************************
  1590. static inline void
  1591. i2StuffFifoBypass(i2eBordStrPtr pB)
  1592. {
  1593. i2ChanStrPtr pCh;
  1594. unsigned char *pRemove;
  1595. unsigned short stripIndex;
  1596. unsigned short packetSize;
  1597. unsigned short paddedSize;
  1598. unsigned short notClogged = 1;
  1599. unsigned long flags;
  1600. int bailout = 1000;
  1601. // Continue processing so long as there are entries, or there is room in the
  1602. // fifo. Each entry represents a channel with something to do.
  1603. while ( --bailout && notClogged &&
  1604. (NULL != (pCh = i2DeQueueNeeds(pB,NEED_BYPASS))))
  1605. {
  1606. WRITE_LOCK_IRQSAVE(&pCh->Cbuf_spinlock,flags);
  1607. stripIndex = pCh->Cbuf_strip;
  1608. // as long as there are packets for this channel...
  1609. while (stripIndex != pCh->Cbuf_stuff) {
  1610. pRemove = &(pCh->Cbuf[stripIndex]);
  1611. packetSize = CMD_COUNT_OF(pRemove) + sizeof(i2CmdHeader);
  1612. paddedSize = ROUNDUP(packetSize);
  1613. if (paddedSize > 0) {
  1614. if ( 0 == i2Write2Fifo(pB, pRemove, paddedSize,0)) {
  1615. notClogged = 0; /* fifo full */
  1616. i2QueueNeeds(pB, pCh, NEED_BYPASS); // Put back on queue
  1617. break; // Break from the channel
  1618. }
  1619. }
  1620. #ifdef DEBUG_FIFO
  1621. WriteDBGBuf("BYPS", pRemove, paddedSize);
  1622. #endif /* DEBUG_FIFO */
  1623. pB->debugBypassCount++;
  1624. pRemove += packetSize;
  1625. stripIndex += packetSize;
  1626. if (stripIndex >= CBUF_SIZE) {
  1627. stripIndex = 0;
  1628. pRemove = pCh->Cbuf;
  1629. }
  1630. }
  1631. // Done with this channel. Move to next, removing this one from
  1632. // the queue of channels if we cleaned it out (i.e., didn't get clogged.
  1633. pCh->Cbuf_strip = stripIndex;
  1634. WRITE_UNLOCK_IRQRESTORE(&pCh->Cbuf_spinlock,flags);
  1635. } // Either clogged or finished all the work
  1636. #ifdef IP2DEBUG_TRACE
  1637. if ( !bailout ) {
  1638. ip2trace (ITRC_NO_PORT, ITRC_ERROR, 1, 0 );
  1639. }
  1640. #endif
  1641. }
  1642. //******************************************************************************
  1643. // Function: i2StuffFifoFlow(pB)
  1644. // Parameters: Pointer to a board structure
  1645. // Returns: Nothing
  1646. //
  1647. // Description:
  1648. // Stuffs as many flow control packets into the fifo as possible. This is easier
  1649. // even than doing normal bypass commands, because there is always at most one
  1650. // packet, already assembled, for each channel.
  1651. //******************************************************************************
  1652. static inline void
  1653. i2StuffFifoFlow(i2eBordStrPtr pB)
  1654. {
  1655. i2ChanStrPtr pCh;
  1656. unsigned short paddedSize = ROUNDUP(sizeof(flowIn));
  1657. ip2trace (ITRC_NO_PORT, ITRC_SFLOW, ITRC_ENTER, 2,
  1658. pB->i2eFifoRemains, paddedSize );
  1659. // Continue processing so long as there are entries, or there is room in the
  1660. // fifo. Each entry represents a channel with something to do.
  1661. while ( (NULL != (pCh = i2DeQueueNeeds(pB,NEED_FLOW)))) {
  1662. pB->debugFlowCount++;
  1663. // NO Chan LOCK needed ???
  1664. if ( 0 == i2Write2Fifo(pB,(unsigned char *)&(pCh->infl),paddedSize,0)) {
  1665. break;
  1666. }
  1667. #ifdef DEBUG_FIFO
  1668. WriteDBGBuf("FLOW",(unsigned char *) &(pCh->infl), paddedSize);
  1669. #endif /* DEBUG_FIFO */
  1670. } // Either clogged or finished all the work
  1671. ip2trace (ITRC_NO_PORT, ITRC_SFLOW, ITRC_RETURN, 0 );
  1672. }
  1673. //******************************************************************************
  1674. // Function: i2StuffFifoInline(pB)
  1675. // Parameters: Pointer to a board structure
  1676. // Returns: Nothing
  1677. //
  1678. // Description:
  1679. // Stuffs as much data and inline commands into the fifo as possible. This is
  1680. // the most complex fifo-stuffing operation, since there if now the channel
  1681. // flow-control issue to deal with.
  1682. //******************************************************************************
  1683. static inline void
  1684. i2StuffFifoInline(i2eBordStrPtr pB)
  1685. {
  1686. i2ChanStrPtr pCh;
  1687. unsigned char *pRemove;
  1688. unsigned short stripIndex;
  1689. unsigned short packetSize;
  1690. unsigned short paddedSize;
  1691. unsigned short notClogged = 1;
  1692. unsigned short flowsize;
  1693. unsigned long flags;
  1694. int bailout = 1000;
  1695. int bailout2;
  1696. ip2trace (ITRC_NO_PORT, ITRC_SICMD, ITRC_ENTER, 3, pB->i2eFifoRemains,
  1697. pB->i2Dbuf_strip, pB->i2Dbuf_stuff );
  1698. // Continue processing so long as there are entries, or there is room in the
  1699. // fifo. Each entry represents a channel with something to do.
  1700. while ( --bailout && notClogged &&
  1701. (NULL != (pCh = i2DeQueueNeeds(pB,NEED_INLINE))) )
  1702. {
  1703. WRITE_LOCK_IRQSAVE(&pCh->Obuf_spinlock,flags);
  1704. stripIndex = pCh->Obuf_strip;
  1705. ip2trace (CHANN, ITRC_SICMD, 3, 2, stripIndex, pCh->Obuf_stuff );
  1706. // as long as there are packets for this channel...
  1707. bailout2 = 1000;
  1708. while ( --bailout2 && stripIndex != pCh->Obuf_stuff) {
  1709. pRemove = &(pCh->Obuf[stripIndex]);
  1710. // Must determine whether this be a data or command packet to
  1711. // calculate correctly the header size and the amount of
  1712. // flow-control credit this type of packet will use.
  1713. if (PTYPE_OF(pRemove) == PTYPE_DATA) {
  1714. flowsize = DATA_COUNT_OF(pRemove);
  1715. packetSize = flowsize + sizeof(i2DataHeader);
  1716. } else {
  1717. flowsize = CMD_COUNT_OF(pRemove);
  1718. packetSize = flowsize + sizeof(i2CmdHeader);
  1719. }
  1720. flowsize = CREDIT_USAGE(flowsize);
  1721. paddedSize = ROUNDUP(packetSize);
  1722. ip2trace (CHANN, ITRC_SICMD, 4, 2, pB->i2eFifoRemains, paddedSize );
  1723. // If we don't have enough credits from the board to send the data,
  1724. // flag the channel that we are waiting for flow control credit, and
  1725. // break out. This will clean up this channel and remove us from the
  1726. // queue of hot things to do.
  1727. ip2trace (CHANN, ITRC_SICMD, 5, 2, pCh->outfl.room, flowsize );
  1728. if (pCh->outfl.room <= flowsize) {
  1729. // Do Not have the credits to send this packet.
  1730. i2QueueNeeds(pB, pCh, NEED_CREDIT);
  1731. notClogged = 0;
  1732. break; // So to do next channel
  1733. }
  1734. if ( (paddedSize > 0)
  1735. && ( 0 == i2Write2Fifo(pB, pRemove, paddedSize, 128))) {
  1736. // Do Not have room in fifo to send this packet.
  1737. notClogged = 0;
  1738. i2QueueNeeds(pB, pCh, NEED_INLINE);
  1739. break; // Break from the channel
  1740. }
  1741. #ifdef DEBUG_FIFO
  1742. WriteDBGBuf("DATA", pRemove, paddedSize);
  1743. #endif /* DEBUG_FIFO */
  1744. pB->debugInlineCount++;
  1745. pCh->icount.tx += flowsize;
  1746. // Update current credits
  1747. pCh->outfl.room -= flowsize;
  1748. pCh->outfl.asof += flowsize;
  1749. if (PTYPE_OF(pRemove) == PTYPE_DATA) {
  1750. pCh->Obuf_char_count -= DATA_COUNT_OF(pRemove);
  1751. }
  1752. pRemove += packetSize;
  1753. stripIndex += packetSize;
  1754. ip2trace (CHANN, ITRC_SICMD, 6, 2, stripIndex, pCh->Obuf_strip);
  1755. if (stripIndex >= OBUF_SIZE) {
  1756. stripIndex = 0;
  1757. pRemove = pCh->Obuf;
  1758. ip2trace (CHANN, ITRC_SICMD, 7, 1, stripIndex );
  1759. }
  1760. } /* while */
  1761. if ( !bailout2 ) {
  1762. ip2trace (CHANN, ITRC_ERROR, 3, 0 );
  1763. }
  1764. // Done with this channel. Move to next, removing this one from the
  1765. // queue of channels if we cleaned it out (i.e., didn't get clogged.
  1766. pCh->Obuf_strip = stripIndex;
  1767. WRITE_UNLOCK_IRQRESTORE(&pCh->Obuf_spinlock,flags);
  1768. if ( notClogged )
  1769. {
  1770. ip2trace (CHANN, ITRC_SICMD, 8, 0 );
  1771. if ( pCh->pTTY ) {
  1772. ip2_owake(pCh->pTTY);
  1773. }
  1774. }
  1775. } // Either clogged or finished all the work
  1776. if ( !bailout ) {
  1777. ip2trace (ITRC_NO_PORT, ITRC_ERROR, 4, 0 );
  1778. }
  1779. ip2trace (ITRC_NO_PORT, ITRC_SICMD, ITRC_RETURN, 1,pB->i2Dbuf_strip);
  1780. }
  1781. //******************************************************************************
  1782. // Function: serviceOutgoingFifo(pB)
  1783. // Parameters: Pointer to a board structure
  1784. // Returns: Nothing
  1785. //
  1786. // Description:
  1787. // Helper routine to put data in the outgoing fifo, if we aren't already waiting
  1788. // for something to be there. If the fifo has only room for a very little data,
  1789. // go head and hit the board with a mailbox hit immediately. Otherwise, it will
  1790. // have to happen later in the interrupt processing. Since this routine may be
  1791. // called both at interrupt and foreground time, we must turn off interrupts
  1792. // during the entire process.
  1793. //******************************************************************************
  1794. static void
  1795. serviceOutgoingFifo(i2eBordStrPtr pB)
  1796. {
  1797. // If we aren't currently waiting for the board to empty our fifo, service
  1798. // everything that is pending, in priority order (especially, Bypass before
  1799. // Inline).
  1800. if ( ! pB->i2eWaitingForEmptyFifo )
  1801. {
  1802. i2StuffFifoFlow(pB);
  1803. i2StuffFifoBypass(pB);
  1804. i2StuffFifoInline(pB);
  1805. iiSendPendingMail(pB);
  1806. }
  1807. }
  1808. //******************************************************************************
  1809. // Function: i2ServiceBoard(pB)
  1810. // Parameters: Pointer to a board structure
  1811. // Returns: Nothing
  1812. //
  1813. // Description:
  1814. // Normally this is called from interrupt level, but there is deliberately
  1815. // nothing in here specific to being called from interrupt level. All the
  1816. // hardware-specific, interrupt-specific things happen at the outer levels.
  1817. //
  1818. // For example, a timer interrupt could drive this routine for some sort of
  1819. // polled operation. The only requirement is that the programmer deal with any
  1820. // atomiticity/concurrency issues that result.
  1821. //
  1822. // This routine responds to the board's having sent mailbox information to the
  1823. // host (which would normally cause an interrupt). This routine reads the
  1824. // incoming mailbox. If there is no data in it, this board did not create the
  1825. // interrupt and/or has nothing to be done to it. (Except, if we have been
  1826. // waiting to write mailbox data to it, we may do so.
  1827. //
  1828. // Based on the value in the mailbox, we may take various actions.
  1829. //
  1830. // No checking here of pB validity: after all, it shouldn't have been called by
  1831. // the handler unless pB were on the list.
  1832. //******************************************************************************
  1833. static inline int
  1834. i2ServiceBoard ( i2eBordStrPtr pB )
  1835. {
  1836. unsigned inmail;
  1837. unsigned long flags;
  1838. /* This should be atomic because of the way we are called... */
  1839. if (NO_MAIL_HERE == ( inmail = pB->i2eStartMail ) ) {
  1840. inmail = iiGetMail(pB);
  1841. }
  1842. pB->i2eStartMail = NO_MAIL_HERE;
  1843. ip2trace (ITRC_NO_PORT, ITRC_INTR, 2, 1, inmail );
  1844. if (inmail != NO_MAIL_HERE) {
  1845. // If the board has gone fatal, nothing to do but hit a bit that will
  1846. // alert foreground tasks to protest!
  1847. if ( inmail & MB_FATAL_ERROR ) {
  1848. pB->i2eFatal = 1;
  1849. goto exit_i2ServiceBoard;
  1850. }
  1851. /* Assuming no fatal condition, we proceed to do work */
  1852. if ( inmail & MB_IN_STUFFED ) {
  1853. pB->i2eFifoInInts++;
  1854. i2StripFifo(pB); /* There might be incoming packets */
  1855. }
  1856. if (inmail & MB_OUT_STRIPPED) {
  1857. pB->i2eFifoOutInts++;
  1858. WRITE_LOCK_IRQSAVE(&pB->write_fifo_spinlock,flags);
  1859. pB->i2eFifoRemains = pB->i2eFifoSize;
  1860. pB->i2eWaitingForEmptyFifo = 0;
  1861. WRITE_UNLOCK_IRQRESTORE(&pB->write_fifo_spinlock,flags);
  1862. ip2trace (ITRC_NO_PORT, ITRC_INTR, 30, 1, pB->i2eFifoRemains );
  1863. }
  1864. serviceOutgoingFifo(pB);
  1865. }
  1866. ip2trace (ITRC_NO_PORT, ITRC_INTR, 8, 0 );
  1867. exit_i2ServiceBoard:
  1868. return 0;
  1869. }