i2lib.c 65 KB

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