i2lib.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209
  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. ip2trace(CHANN, ITRC_QUEUE, 3, 1, totalsize);
  572. WRITE_UNLOCK_IRQRESTORE(lock_var_p, flags);
  573. } else
  574. ip2trace(CHANN, ITRC_QUEUE, 3, 1, totalsize);
  575. /* Prepare to wait for buffers to empty */
  576. serviceOutgoingFifo(pB); // Dump what we got
  577. if (timeout == 0) {
  578. return 0; // Tired of waiting
  579. }
  580. if (timeout > 0)
  581. timeout--; // So negative values == forever
  582. if (!in_interrupt()) {
  583. schedule_timeout_interruptible(1); // short nap
  584. } else {
  585. // we cannot sched/sleep in interrupt silly
  586. return 0;
  587. }
  588. if (signal_pending(current)) {
  589. return 0; // Wake up! Time to die!!!
  590. }
  591. ip2trace (CHANN, ITRC_QUEUE, 4, 0 );
  592. } // end of for(;;)
  593. // At this point we have room and the lock - stick them in.
  594. channel = pCh->infl.hd.i2sChannel;
  595. pInsert = &pBuf[stuffIndex]; // Pointer to start of packet
  596. pDest = CMD_OF(pInsert); // Pointer to start of command
  597. // When we start counting, the block is the size of the header
  598. for (blocksize = sizeof(i2CmdHeader), count = nCommands,
  599. lastended = 0, ppCs = &pCs0;
  600. count;
  601. count--, ppCs++)
  602. {
  603. pCs = *ppCs; // Points to command protocol structure
  604. // If this is a bookmark request command, post the fact that a bookmark
  605. // request is pending. NOTE THIS TRICK ONLY WORKS BECAUSE CMD_BMARK_REQ
  606. // has no parameters! The more general solution would be to reference
  607. // pCs->cmd[0].
  608. if (pCs == CMD_BMARK_REQ) {
  609. pCh->bookMarks++;
  610. ip2trace (CHANN, ITRC_DRAIN, 30, 1, pCh->bookMarks );
  611. }
  612. cnt = pCs->length;
  613. // If this command would put us over the maximum block size or
  614. // if the last command had to be at the end of a block, we end
  615. // the existing block here and start a new one.
  616. if ((blocksize + cnt > maxBlock) || lastended) {
  617. ip2trace (CHANN, ITRC_QUEUE, 5, 0 );
  618. PTYPE_OF(pInsert) = type;
  619. CHANNEL_OF(pInsert) = channel;
  620. // count here does not include the header
  621. CMD_COUNT_OF(pInsert) = blocksize - sizeof(i2CmdHeader);
  622. stuffIndex += blocksize;
  623. if(stuffIndex >= maxBuff) {
  624. stuffIndex = 0;
  625. pInsert = pBuf;
  626. }
  627. pInsert = &pBuf[stuffIndex]; // Pointer to start of next pkt
  628. pDest = CMD_OF(pInsert);
  629. blocksize = sizeof(i2CmdHeader);
  630. }
  631. // Now we know there is room for this one in the current block
  632. blocksize += cnt; // Total bytes in this command
  633. pSource = pCs->cmd; // Copy the command into the buffer
  634. while (cnt--) {
  635. *pDest++ = *pSource++;
  636. }
  637. // If this command had to end a block, then we will make sure to account
  638. // for it should there be any more blocks.
  639. lastended = pCs->flags & END;
  640. } // end for
  641. // Clean up the final block by writing header, etc
  642. PTYPE_OF(pInsert) = type;
  643. CHANNEL_OF(pInsert) = channel;
  644. // count here does not include the header
  645. CMD_COUNT_OF(pInsert) = blocksize - sizeof(i2CmdHeader);
  646. stuffIndex += blocksize;
  647. if(stuffIndex >= maxBuff) {
  648. stuffIndex = 0;
  649. pInsert = pBuf;
  650. }
  651. // Updates the index, and post the need for service. When adding these to
  652. // the queue of channels, we turn off the interrupt while doing so,
  653. // because at interrupt level we might want to push a channel back to the
  654. // end of the queue.
  655. switch(type)
  656. {
  657. case PTYPE_INLINE:
  658. pCh->Obuf_stuff = stuffIndex; // Store buffer pointer
  659. WRITE_UNLOCK_IRQRESTORE(&pCh->Obuf_spinlock,flags);
  660. pB->debugInlineQueued++;
  661. // Add the channel pointer to list of channels needing service (first
  662. // come...), if it's not already there.
  663. i2QueueNeeds(pB, pCh, NEED_INLINE);
  664. break;
  665. case PTYPE_BYPASS:
  666. pCh->Cbuf_stuff = stuffIndex; // Store buffer pointer
  667. WRITE_UNLOCK_IRQRESTORE(&pCh->Cbuf_spinlock,flags);
  668. pB->debugBypassQueued++;
  669. // Add the channel pointer to list of channels needing service (first
  670. // come...), if it's not already there.
  671. i2QueueNeeds(pB, pCh, NEED_BYPASS);
  672. break;
  673. }
  674. ip2trace (CHANN, ITRC_QUEUE, ITRC_RETURN, 1, nCommands );
  675. return nCommands; // Good status: number of commands sent
  676. }
  677. //******************************************************************************
  678. // Function: i2GetStatus(pCh,resetBits)
  679. // Parameters: Pointer to a channel structure
  680. // Bit map of status bits to clear
  681. // Returns: Bit map of current status bits
  682. //
  683. // Description:
  684. // Returns the state of data set signals, and whether a break has been received,
  685. // (see i2lib.h for bit-mapped result). resetBits is a bit-map of any status
  686. // bits to be cleared: I2_BRK, I2_PAR, I2_FRA, I2_OVR,... These are cleared
  687. // AFTER the condition is passed. If pCh does not point to a valid channel,
  688. // returns -1 (which would be impossible otherwise.
  689. //******************************************************************************
  690. static int
  691. i2GetStatus(i2ChanStrPtr pCh, int resetBits)
  692. {
  693. unsigned short status;
  694. i2eBordStrPtr pB;
  695. ip2trace (CHANN, ITRC_STATUS, ITRC_ENTER, 2, pCh->dataSetIn, resetBits );
  696. // Make sure the channel exists, otherwise do nothing */
  697. if ( !i2Validate ( pCh ) )
  698. return -1;
  699. pB = pCh->pMyBord;
  700. status = pCh->dataSetIn;
  701. // Clear any specified error bits: but note that only actual error bits can
  702. // be cleared, regardless of the value passed.
  703. if (resetBits)
  704. {
  705. pCh->dataSetIn &= ~(resetBits & (I2_BRK | I2_PAR | I2_FRA | I2_OVR));
  706. pCh->dataSetIn &= ~(I2_DDCD | I2_DCTS | I2_DDSR | I2_DRI);
  707. }
  708. ip2trace (CHANN, ITRC_STATUS, ITRC_RETURN, 1, pCh->dataSetIn );
  709. return status;
  710. }
  711. //******************************************************************************
  712. // Function: i2Input(pChpDest,count)
  713. // Parameters: Pointer to a channel structure
  714. // Pointer to data buffer
  715. // Number of bytes to read
  716. // Returns: Number of bytes read, or -1 for error
  717. //
  718. // Description:
  719. // Strips data from the input buffer and writes it to pDest. If there is a
  720. // collosal blunder, (invalid structure pointers or the like), returns -1.
  721. // Otherwise, returns the number of bytes read.
  722. //******************************************************************************
  723. static int
  724. i2Input(i2ChanStrPtr pCh)
  725. {
  726. int amountToMove;
  727. unsigned short stripIndex;
  728. int count;
  729. unsigned long flags = 0;
  730. ip2trace (CHANN, ITRC_INPUT, ITRC_ENTER, 0);
  731. // Ensure channel structure seems real
  732. if ( !i2Validate( pCh ) ) {
  733. count = -1;
  734. goto i2Input_exit;
  735. }
  736. WRITE_LOCK_IRQSAVE(&pCh->Ibuf_spinlock,flags);
  737. // initialize some accelerators and private copies
  738. stripIndex = pCh->Ibuf_strip;
  739. count = pCh->Ibuf_stuff - stripIndex;
  740. // If buffer is empty or requested data count was 0, (trivial case) return
  741. // without any further thought.
  742. if ( count == 0 ) {
  743. WRITE_UNLOCK_IRQRESTORE(&pCh->Ibuf_spinlock,flags);
  744. goto i2Input_exit;
  745. }
  746. // Adjust for buffer wrap
  747. if ( count < 0 ) {
  748. count += IBUF_SIZE;
  749. }
  750. // Don't give more than can be taken by the line discipline
  751. amountToMove = pCh->pTTY->receive_room;
  752. if (count > amountToMove) {
  753. count = amountToMove;
  754. }
  755. // How much could we copy without a wrap?
  756. amountToMove = IBUF_SIZE - stripIndex;
  757. if (amountToMove > count) {
  758. amountToMove = count;
  759. }
  760. // Move the first block
  761. pCh->pTTY->ldisc.receive_buf( pCh->pTTY,
  762. &(pCh->Ibuf[stripIndex]), NULL, amountToMove );
  763. // If we needed to wrap, do the second data move
  764. if (count > amountToMove) {
  765. pCh->pTTY->ldisc.receive_buf( pCh->pTTY,
  766. pCh->Ibuf, NULL, count - amountToMove );
  767. }
  768. // Bump and wrap the stripIndex all at once by the amount of data read. This
  769. // method is good regardless of whether the data was in one or two pieces.
  770. stripIndex += count;
  771. if (stripIndex >= IBUF_SIZE) {
  772. stripIndex -= IBUF_SIZE;
  773. }
  774. pCh->Ibuf_strip = stripIndex;
  775. // Update our flow control information and possibly queue ourselves to send
  776. // it, depending on how much data has been stripped since the last time a
  777. // packet was sent.
  778. pCh->infl.asof += count;
  779. if ((pCh->sinceLastFlow += count) >= pCh->whenSendFlow) {
  780. pCh->sinceLastFlow -= pCh->whenSendFlow;
  781. WRITE_UNLOCK_IRQRESTORE(&pCh->Ibuf_spinlock,flags);
  782. i2QueueNeeds(pCh->pMyBord, pCh, NEED_FLOW);
  783. } else {
  784. WRITE_UNLOCK_IRQRESTORE(&pCh->Ibuf_spinlock,flags);
  785. }
  786. i2Input_exit:
  787. ip2trace (CHANN, ITRC_INPUT, ITRC_RETURN, 1, count);
  788. return count;
  789. }
  790. //******************************************************************************
  791. // Function: i2InputFlush(pCh)
  792. // Parameters: Pointer to a channel structure
  793. // Returns: Number of bytes stripped, or -1 for error
  794. //
  795. // Description:
  796. // Strips any data from the input buffer. If there is a collosal blunder,
  797. // (invalid structure pointers or the like), returns -1. Otherwise, returns the
  798. // number of bytes stripped.
  799. //******************************************************************************
  800. static int
  801. i2InputFlush(i2ChanStrPtr pCh)
  802. {
  803. int count;
  804. unsigned long flags;
  805. // Ensure channel structure seems real
  806. if ( !i2Validate ( pCh ) )
  807. return -1;
  808. ip2trace (CHANN, ITRC_INPUT, 10, 0);
  809. WRITE_LOCK_IRQSAVE(&pCh->Ibuf_spinlock,flags);
  810. count = pCh->Ibuf_stuff - pCh->Ibuf_strip;
  811. // Adjust for buffer wrap
  812. if (count < 0) {
  813. count += IBUF_SIZE;
  814. }
  815. // Expedient way to zero out the buffer
  816. pCh->Ibuf_strip = pCh->Ibuf_stuff;
  817. // Update our flow control information and possibly queue ourselves to send
  818. // it, depending on how much data has been stripped since the last time a
  819. // packet was sent.
  820. pCh->infl.asof += count;
  821. if ( (pCh->sinceLastFlow += count) >= pCh->whenSendFlow )
  822. {
  823. pCh->sinceLastFlow -= pCh->whenSendFlow;
  824. WRITE_UNLOCK_IRQRESTORE(&pCh->Ibuf_spinlock,flags);
  825. i2QueueNeeds(pCh->pMyBord, pCh, NEED_FLOW);
  826. } else {
  827. WRITE_UNLOCK_IRQRESTORE(&pCh->Ibuf_spinlock,flags);
  828. }
  829. ip2trace (CHANN, ITRC_INPUT, 19, 1, count);
  830. return count;
  831. }
  832. //******************************************************************************
  833. // Function: i2InputAvailable(pCh)
  834. // Parameters: Pointer to a channel structure
  835. // Returns: Number of bytes available, or -1 for error
  836. //
  837. // Description:
  838. // If there is a collosal blunder, (invalid structure pointers or the like),
  839. // returns -1. Otherwise, returns the number of bytes stripped. Otherwise,
  840. // returns the number of bytes available in the buffer.
  841. //******************************************************************************
  842. #if 0
  843. static int
  844. i2InputAvailable(i2ChanStrPtr pCh)
  845. {
  846. int count;
  847. // Ensure channel structure seems real
  848. if ( !i2Validate ( pCh ) ) return -1;
  849. // initialize some accelerators and private copies
  850. READ_LOCK_IRQSAVE(&pCh->Ibuf_spinlock,flags);
  851. count = pCh->Ibuf_stuff - pCh->Ibuf_strip;
  852. READ_UNLOCK_IRQRESTORE(&pCh->Ibuf_spinlock,flags);
  853. // Adjust for buffer wrap
  854. if (count < 0)
  855. {
  856. count += IBUF_SIZE;
  857. }
  858. return count;
  859. }
  860. #endif
  861. //******************************************************************************
  862. // Function: i2Output(pCh, pSource, count)
  863. // Parameters: Pointer to channel structure
  864. // Pointer to source data
  865. // Number of bytes to send
  866. // Returns: Number of bytes sent, or -1 for error
  867. //
  868. // Description:
  869. // Queues the data at pSource to be sent as data packets to the board. If there
  870. // is a collosal blunder, (invalid structure pointers or the like), returns -1.
  871. // Otherwise, returns the number of bytes written. What if there is not enough
  872. // room for all the data? If pCh->channelOptions & CO_NBLOCK_WRITE is set, then
  873. // we transfer as many characters as we can now, then return. If this bit is
  874. // clear (default), routine will spin along until all the data is buffered.
  875. // Should this occur, the 1-ms delay routine is called while waiting to avoid
  876. // applications that one cannot break out of.
  877. //******************************************************************************
  878. static int
  879. i2Output(i2ChanStrPtr pCh, const char *pSource, int count)
  880. {
  881. i2eBordStrPtr pB;
  882. unsigned char *pInsert;
  883. int amountToMove;
  884. int countOriginal = count;
  885. unsigned short channel;
  886. unsigned short stuffIndex;
  887. unsigned long flags;
  888. int bailout = 10;
  889. ip2trace (CHANN, ITRC_OUTPUT, ITRC_ENTER, 2, count, 0 );
  890. // Ensure channel structure seems real
  891. if ( !i2Validate ( pCh ) )
  892. return -1;
  893. // initialize some accelerators and private copies
  894. pB = pCh->pMyBord;
  895. channel = pCh->infl.hd.i2sChannel;
  896. // If the board has gone fatal, return bad, and also hit the trap routine if
  897. // it exists.
  898. if (pB->i2eFatal) {
  899. if (pB->i2eFatalTrap) {
  900. (*(pB)->i2eFatalTrap)(pB);
  901. }
  902. return -1;
  903. }
  904. // Proceed as though we would do everything
  905. while ( count > 0 ) {
  906. // How much room in output buffer is there?
  907. READ_LOCK_IRQSAVE(&pCh->Obuf_spinlock,flags);
  908. amountToMove = pCh->Obuf_strip - pCh->Obuf_stuff - 1;
  909. READ_UNLOCK_IRQRESTORE(&pCh->Obuf_spinlock,flags);
  910. if (amountToMove < 0) {
  911. amountToMove += OBUF_SIZE;
  912. }
  913. // Subtract off the headers size and see how much room there is for real
  914. // data. If this is negative, we will discover later.
  915. amountToMove -= sizeof (i2DataHeader);
  916. // Don't move more (now) than can go in a single packet
  917. if ( amountToMove > (int)(MAX_OBUF_BLOCK - sizeof(i2DataHeader)) ) {
  918. amountToMove = MAX_OBUF_BLOCK - sizeof(i2DataHeader);
  919. }
  920. // Don't move more than the count we were given
  921. if (amountToMove > count) {
  922. amountToMove = count;
  923. }
  924. // Now we know how much we must move: NB because the ring buffers have
  925. // an overflow area at the end, we needn't worry about wrapping in the
  926. // middle of a packet.
  927. // Small WINDOW here with no LOCK but I can't call Flush with LOCK
  928. // We would be flushing (or ending flush) anyway
  929. ip2trace (CHANN, ITRC_OUTPUT, 10, 1, amountToMove );
  930. if ( !(pCh->flush_flags && i2RetryFlushOutput(pCh) )
  931. && amountToMove > 0 )
  932. {
  933. WRITE_LOCK_IRQSAVE(&pCh->Obuf_spinlock,flags);
  934. stuffIndex = pCh->Obuf_stuff;
  935. // Had room to move some data: don't know whether the block size,
  936. // buffer space, or what was the limiting factor...
  937. pInsert = &(pCh->Obuf[stuffIndex]);
  938. // Set up the header
  939. CHANNEL_OF(pInsert) = channel;
  940. PTYPE_OF(pInsert) = PTYPE_DATA;
  941. TAG_OF(pInsert) = 0;
  942. ID_OF(pInsert) = ID_ORDINARY_DATA;
  943. DATA_COUNT_OF(pInsert) = amountToMove;
  944. // Move the data
  945. memcpy( (char*)(DATA_OF(pInsert)), pSource, amountToMove );
  946. // Adjust pointers and indices
  947. pSource += amountToMove;
  948. pCh->Obuf_char_count += amountToMove;
  949. stuffIndex += amountToMove + sizeof(i2DataHeader);
  950. count -= amountToMove;
  951. if (stuffIndex >= OBUF_SIZE) {
  952. stuffIndex = 0;
  953. }
  954. pCh->Obuf_stuff = stuffIndex;
  955. WRITE_UNLOCK_IRQRESTORE(&pCh->Obuf_spinlock,flags);
  956. ip2trace (CHANN, ITRC_OUTPUT, 13, 1, stuffIndex );
  957. } else {
  958. // Cannot move data
  959. // becuz we need to stuff a flush
  960. // or amount to move is <= 0
  961. ip2trace(CHANN, ITRC_OUTPUT, 14, 3,
  962. amountToMove, pB->i2eFifoRemains,
  963. pB->i2eWaitingForEmptyFifo );
  964. // Put this channel back on queue
  965. // this ultimatly gets more data or wakes write output
  966. i2QueueNeeds(pB, pCh, NEED_INLINE);
  967. if ( pB->i2eWaitingForEmptyFifo ) {
  968. ip2trace (CHANN, ITRC_OUTPUT, 16, 0 );
  969. // or schedule
  970. if (!in_interrupt()) {
  971. ip2trace (CHANN, ITRC_OUTPUT, 61, 0 );
  972. schedule_timeout_interruptible(2);
  973. if (signal_pending(current)) {
  974. break;
  975. }
  976. continue;
  977. } else {
  978. ip2trace (CHANN, ITRC_OUTPUT, 62, 0 );
  979. // let interrupt in = WAS restore_flags()
  980. // We hold no lock nor is irq off anymore???
  981. break;
  982. }
  983. break; // from while(count)
  984. }
  985. else if ( pB->i2eFifoRemains < 32 && !pB->i2eTxMailEmpty ( pB ) )
  986. {
  987. ip2trace (CHANN, ITRC_OUTPUT, 19, 2,
  988. pB->i2eFifoRemains,
  989. pB->i2eTxMailEmpty );
  990. break; // from while(count)
  991. } else if ( pCh->channelNeeds & NEED_CREDIT ) {
  992. ip2trace (CHANN, ITRC_OUTPUT, 22, 0 );
  993. break; // from while(count)
  994. } else if ( --bailout) {
  995. // Try to throw more things (maybe not us) in the fifo if we're
  996. // not already waiting for it.
  997. ip2trace (CHANN, ITRC_OUTPUT, 20, 0 );
  998. serviceOutgoingFifo(pB);
  999. //break; CONTINUE;
  1000. } else {
  1001. ip2trace (CHANN, ITRC_OUTPUT, 21, 3,
  1002. pB->i2eFifoRemains,
  1003. pB->i2eOutMailWaiting,
  1004. pB->i2eWaitingForEmptyFifo );
  1005. break; // from while(count)
  1006. }
  1007. }
  1008. } // End of while(count)
  1009. i2QueueNeeds(pB, pCh, NEED_INLINE);
  1010. // We drop through either when the count expires, or when there is some
  1011. // count left, but there was a non-blocking write.
  1012. if (countOriginal > count) {
  1013. ip2trace (CHANN, ITRC_OUTPUT, 17, 2, countOriginal, count );
  1014. serviceOutgoingFifo( pB );
  1015. }
  1016. ip2trace (CHANN, ITRC_OUTPUT, ITRC_RETURN, 2, countOriginal, count );
  1017. return countOriginal - count;
  1018. }
  1019. //******************************************************************************
  1020. // Function: i2FlushOutput(pCh)
  1021. // Parameters: Pointer to a channel structure
  1022. // Returns: Nothing
  1023. //
  1024. // Description:
  1025. // Sends bypass command to start flushing (waiting possibly forever until there
  1026. // is room), then sends inline command to stop flushing output, (again waiting
  1027. // possibly forever).
  1028. //******************************************************************************
  1029. static inline void
  1030. i2FlushOutput(i2ChanStrPtr pCh)
  1031. {
  1032. ip2trace (CHANN, ITRC_FLUSH, 1, 1, pCh->flush_flags );
  1033. if (pCh->flush_flags)
  1034. return;
  1035. if ( 1 != i2QueueCommands(PTYPE_BYPASS, pCh, 0, 1, CMD_STARTFL) ) {
  1036. pCh->flush_flags = STARTFL_FLAG; // Failed - flag for later
  1037. ip2trace (CHANN, ITRC_FLUSH, 2, 0 );
  1038. } else if ( 1 != i2QueueCommands(PTYPE_INLINE, pCh, 0, 1, CMD_STOPFL) ) {
  1039. pCh->flush_flags = STOPFL_FLAG; // Failed - flag for later
  1040. ip2trace (CHANN, ITRC_FLUSH, 3, 0 );
  1041. }
  1042. }
  1043. static int
  1044. i2RetryFlushOutput(i2ChanStrPtr pCh)
  1045. {
  1046. int old_flags = pCh->flush_flags;
  1047. ip2trace (CHANN, ITRC_FLUSH, 14, 1, old_flags );
  1048. pCh->flush_flags = 0; // Clear flag so we can avoid recursion
  1049. // and queue the commands
  1050. if ( old_flags & STARTFL_FLAG ) {
  1051. if ( 1 == i2QueueCommands(PTYPE_BYPASS, pCh, 0, 1, CMD_STARTFL) ) {
  1052. old_flags = STOPFL_FLAG; //Success - send stop flush
  1053. } else {
  1054. old_flags = STARTFL_FLAG; //Failure - Flag for retry later
  1055. }
  1056. ip2trace (CHANN, ITRC_FLUSH, 15, 1, old_flags );
  1057. }
  1058. if ( old_flags & STOPFL_FLAG ) {
  1059. if (1 == i2QueueCommands(PTYPE_INLINE, pCh, 0, 1, CMD_STOPFL)) {
  1060. old_flags = 0; // Success - clear flags
  1061. }
  1062. ip2trace (CHANN, ITRC_FLUSH, 16, 1, old_flags );
  1063. }
  1064. pCh->flush_flags = old_flags;
  1065. ip2trace (CHANN, ITRC_FLUSH, 17, 1, old_flags );
  1066. return old_flags;
  1067. }
  1068. //******************************************************************************
  1069. // Function: i2DrainOutput(pCh,timeout)
  1070. // Parameters: Pointer to a channel structure
  1071. // Maximum period to wait
  1072. // Returns: ?
  1073. //
  1074. // Description:
  1075. // Uses the bookmark request command to ask the board to send a bookmark back as
  1076. // soon as all the data is completely sent.
  1077. //******************************************************************************
  1078. static void
  1079. i2DrainWakeup(unsigned long d)
  1080. {
  1081. i2ChanStrPtr pCh = (i2ChanStrPtr)d;
  1082. ip2trace (CHANN, ITRC_DRAIN, 10, 1, pCh->BookmarkTimer.expires );
  1083. pCh->BookmarkTimer.expires = 0;
  1084. wake_up_interruptible( &pCh->pBookmarkWait );
  1085. }
  1086. static void
  1087. i2DrainOutput(i2ChanStrPtr pCh, int timeout)
  1088. {
  1089. wait_queue_t wait;
  1090. i2eBordStrPtr pB;
  1091. ip2trace (CHANN, ITRC_DRAIN, ITRC_ENTER, 1, pCh->BookmarkTimer.expires);
  1092. pB = pCh->pMyBord;
  1093. // If the board has gone fatal, return bad,
  1094. // and also hit the trap routine if it exists.
  1095. if (pB->i2eFatal) {
  1096. if (pB->i2eFatalTrap) {
  1097. (*(pB)->i2eFatalTrap)(pB);
  1098. }
  1099. return;
  1100. }
  1101. if ((timeout > 0) && (pCh->BookmarkTimer.expires == 0 )) {
  1102. // One per customer (channel)
  1103. setup_timer(&pCh->BookmarkTimer, i2DrainWakeup,
  1104. (unsigned long)pCh);
  1105. ip2trace (CHANN, ITRC_DRAIN, 1, 1, pCh->BookmarkTimer.expires );
  1106. mod_timer(&pCh->BookmarkTimer, jiffies + timeout);
  1107. }
  1108. i2QueueCommands( PTYPE_INLINE, pCh, -1, 1, CMD_BMARK_REQ );
  1109. init_waitqueue_entry(&wait, current);
  1110. add_wait_queue(&(pCh->pBookmarkWait), &wait);
  1111. set_current_state( TASK_INTERRUPTIBLE );
  1112. serviceOutgoingFifo( pB );
  1113. schedule(); // Now we take our interruptible sleep on
  1114. // Clean up the queue
  1115. set_current_state( TASK_RUNNING );
  1116. remove_wait_queue(&(pCh->pBookmarkWait), &wait);
  1117. // if expires == 0 then timer poped, then do not need to del_timer
  1118. if ((timeout > 0) && pCh->BookmarkTimer.expires &&
  1119. time_before(jiffies, pCh->BookmarkTimer.expires)) {
  1120. del_timer( &(pCh->BookmarkTimer) );
  1121. pCh->BookmarkTimer.expires = 0;
  1122. ip2trace (CHANN, ITRC_DRAIN, 3, 1, pCh->BookmarkTimer.expires );
  1123. }
  1124. ip2trace (CHANN, ITRC_DRAIN, ITRC_RETURN, 1, pCh->BookmarkTimer.expires );
  1125. return;
  1126. }
  1127. //******************************************************************************
  1128. // Function: i2OutputFree(pCh)
  1129. // Parameters: Pointer to a channel structure
  1130. // Returns: Space in output buffer
  1131. //
  1132. // Description:
  1133. // Returns -1 if very gross error. Otherwise returns the amount of bytes still
  1134. // free in the output buffer.
  1135. //******************************************************************************
  1136. static int
  1137. i2OutputFree(i2ChanStrPtr pCh)
  1138. {
  1139. int amountToMove;
  1140. unsigned long flags;
  1141. // Ensure channel structure seems real
  1142. if ( !i2Validate ( pCh ) ) {
  1143. return -1;
  1144. }
  1145. READ_LOCK_IRQSAVE(&pCh->Obuf_spinlock,flags);
  1146. amountToMove = pCh->Obuf_strip - pCh->Obuf_stuff - 1;
  1147. READ_UNLOCK_IRQRESTORE(&pCh->Obuf_spinlock,flags);
  1148. if (amountToMove < 0) {
  1149. amountToMove += OBUF_SIZE;
  1150. }
  1151. // If this is negative, we will discover later
  1152. amountToMove -= sizeof(i2DataHeader);
  1153. return (amountToMove < 0) ? 0 : amountToMove;
  1154. }
  1155. static void
  1156. ip2_owake( PTTY tp)
  1157. {
  1158. i2ChanStrPtr pCh;
  1159. if (tp == NULL) return;
  1160. pCh = tp->driver_data;
  1161. ip2trace (CHANN, ITRC_SICMD, 10, 2, tp->flags,
  1162. (1 << TTY_DO_WRITE_WAKEUP) );
  1163. tty_wakeup(tp);
  1164. }
  1165. static inline void
  1166. set_baud_params(i2eBordStrPtr pB)
  1167. {
  1168. int i,j;
  1169. i2ChanStrPtr *pCh;
  1170. pCh = (i2ChanStrPtr *) pB->i2eChannelPtr;
  1171. for (i = 0; i < ABS_MAX_BOXES; i++) {
  1172. if (pB->channelBtypes.bid_value[i]) {
  1173. if (BID_HAS_654(pB->channelBtypes.bid_value[i])) {
  1174. for (j = 0; j < ABS_BIGGEST_BOX; j++) {
  1175. if (pCh[i*16+j] == NULL)
  1176. break;
  1177. (pCh[i*16+j])->BaudBase = 921600; // MAX for ST654
  1178. (pCh[i*16+j])->BaudDivisor = 96;
  1179. }
  1180. } else { // has cirrus cd1400
  1181. for (j = 0; j < ABS_BIGGEST_BOX; j++) {
  1182. if (pCh[i*16+j] == NULL)
  1183. break;
  1184. (pCh[i*16+j])->BaudBase = 115200; // MAX for CD1400
  1185. (pCh[i*16+j])->BaudDivisor = 12;
  1186. }
  1187. }
  1188. }
  1189. }
  1190. }
  1191. //******************************************************************************
  1192. // Function: i2StripFifo(pB)
  1193. // Parameters: Pointer to a board structure
  1194. // Returns: ?
  1195. //
  1196. // Description:
  1197. // Strips all the available data from the incoming FIFO, identifies the type of
  1198. // packet, and either buffers the data or does what needs to be done.
  1199. //
  1200. // Note there is no overflow checking here: if the board sends more data than it
  1201. // ought to, we will not detect it here, but blindly overflow...
  1202. //******************************************************************************
  1203. // A buffer for reading in blocks for unknown channels
  1204. static unsigned char junkBuffer[IBUF_SIZE];
  1205. // A buffer to read in a status packet. Because of the size of the count field
  1206. // for these things, the maximum packet size must be less than MAX_CMD_PACK_SIZE
  1207. static unsigned char cmdBuffer[MAX_CMD_PACK_SIZE + 4];
  1208. // This table changes the bit order from MSR order given by STAT_MODEM packet to
  1209. // status bits used in our library.
  1210. static char xlatDss[16] = {
  1211. 0 | 0 | 0 | 0 ,
  1212. 0 | 0 | 0 | I2_CTS ,
  1213. 0 | 0 | I2_DSR | 0 ,
  1214. 0 | 0 | I2_DSR | I2_CTS ,
  1215. 0 | I2_RI | 0 | 0 ,
  1216. 0 | I2_RI | 0 | I2_CTS ,
  1217. 0 | I2_RI | I2_DSR | 0 ,
  1218. 0 | I2_RI | I2_DSR | I2_CTS ,
  1219. I2_DCD | 0 | 0 | 0 ,
  1220. I2_DCD | 0 | 0 | I2_CTS ,
  1221. I2_DCD | 0 | I2_DSR | 0 ,
  1222. I2_DCD | 0 | I2_DSR | I2_CTS ,
  1223. I2_DCD | I2_RI | 0 | 0 ,
  1224. I2_DCD | I2_RI | 0 | I2_CTS ,
  1225. I2_DCD | I2_RI | I2_DSR | 0 ,
  1226. I2_DCD | I2_RI | I2_DSR | I2_CTS };
  1227. static inline void
  1228. i2StripFifo(i2eBordStrPtr pB)
  1229. {
  1230. i2ChanStrPtr pCh;
  1231. int channel;
  1232. int count;
  1233. unsigned short stuffIndex;
  1234. int amountToRead;
  1235. unsigned char *pc, *pcLimit;
  1236. unsigned char uc;
  1237. unsigned char dss_change;
  1238. unsigned long bflags,cflags;
  1239. // ip2trace (ITRC_NO_PORT, ITRC_SFIFO, ITRC_ENTER, 0 );
  1240. while (HAS_INPUT(pB)) {
  1241. // ip2trace (ITRC_NO_PORT, ITRC_SFIFO, 2, 0 );
  1242. // Process packet from fifo a one atomic unit
  1243. WRITE_LOCK_IRQSAVE(&pB->read_fifo_spinlock,bflags);
  1244. // The first word (or two bytes) will have channel number and type of
  1245. // packet, possibly other information
  1246. pB->i2eLeadoffWord[0] = iiReadWord(pB);
  1247. switch(PTYPE_OF(pB->i2eLeadoffWord))
  1248. {
  1249. case PTYPE_DATA:
  1250. pB->got_input = 1;
  1251. // ip2trace (ITRC_NO_PORT, ITRC_SFIFO, 3, 0 );
  1252. channel = CHANNEL_OF(pB->i2eLeadoffWord); /* Store channel */
  1253. count = iiReadWord(pB); /* Count is in the next word */
  1254. // NEW: Check the count for sanity! Should the hardware fail, our death
  1255. // is more pleasant. While an oversize channel is acceptable (just more
  1256. // than the driver supports), an over-length count clearly means we are
  1257. // sick!
  1258. if ( ((unsigned int)count) > IBUF_SIZE ) {
  1259. pB->i2eFatal = 2;
  1260. WRITE_UNLOCK_IRQRESTORE(&pB->read_fifo_spinlock,bflags);
  1261. return; /* Bail out ASAP */
  1262. }
  1263. // Channel is illegally big ?
  1264. if ((channel >= pB->i2eChannelCnt) ||
  1265. (NULL==(pCh = ((i2ChanStrPtr*)pB->i2eChannelPtr)[channel])))
  1266. {
  1267. iiReadBuf(pB, junkBuffer, count);
  1268. WRITE_UNLOCK_IRQRESTORE(&pB->read_fifo_spinlock,bflags);
  1269. break; /* From switch: ready for next packet */
  1270. }
  1271. // Channel should be valid, then
  1272. // If this is a hot-key, merely post its receipt for now. These are
  1273. // always supposed to be 1-byte packets, so we won't even check the
  1274. // count. Also we will post an acknowledgement to the board so that
  1275. // more data can be forthcoming. Note that we are not trying to use
  1276. // these sequences in this driver, merely to robustly ignore them.
  1277. if(ID_OF(pB->i2eLeadoffWord) == ID_HOT_KEY)
  1278. {
  1279. pCh->hotKeyIn = iiReadWord(pB) & 0xff;
  1280. WRITE_UNLOCK_IRQRESTORE(&pB->read_fifo_spinlock,bflags);
  1281. i2QueueCommands(PTYPE_BYPASS, pCh, 0, 1, CMD_HOTACK);
  1282. break; /* From the switch: ready for next packet */
  1283. }
  1284. // Normal data! We crudely assume there is room for the data in our
  1285. // buffer because the board wouldn't have exceeded his credit limit.
  1286. WRITE_LOCK_IRQSAVE(&pCh->Ibuf_spinlock,cflags);
  1287. // We have 2 locks now
  1288. stuffIndex = pCh->Ibuf_stuff;
  1289. amountToRead = IBUF_SIZE - stuffIndex;
  1290. if (amountToRead > count)
  1291. amountToRead = count;
  1292. // stuffIndex would have been already adjusted so there would
  1293. // always be room for at least one, and count is always at least
  1294. // one.
  1295. iiReadBuf(pB, &(pCh->Ibuf[stuffIndex]), amountToRead);
  1296. pCh->icount.rx += amountToRead;
  1297. // Update the stuffIndex by the amount of data moved. Note we could
  1298. // never ask for more data than would just fit. However, we might
  1299. // have read in one more byte than we wanted because the read
  1300. // rounds up to even bytes. If this byte is on the end of the
  1301. // packet, and is padding, we ignore it. If the byte is part of
  1302. // the actual data, we need to move it.
  1303. stuffIndex += amountToRead;
  1304. if (stuffIndex >= IBUF_SIZE) {
  1305. if ((amountToRead & 1) && (count > amountToRead)) {
  1306. pCh->Ibuf[0] = pCh->Ibuf[IBUF_SIZE];
  1307. amountToRead++;
  1308. stuffIndex = 1;
  1309. } else {
  1310. stuffIndex = 0;
  1311. }
  1312. }
  1313. // If there is anything left over, read it as well
  1314. if (count > amountToRead) {
  1315. amountToRead = count - amountToRead;
  1316. iiReadBuf(pB, &(pCh->Ibuf[stuffIndex]), amountToRead);
  1317. pCh->icount.rx += amountToRead;
  1318. stuffIndex += amountToRead;
  1319. }
  1320. // Update stuff index
  1321. pCh->Ibuf_stuff = stuffIndex;
  1322. WRITE_UNLOCK_IRQRESTORE(&pCh->Ibuf_spinlock,cflags);
  1323. WRITE_UNLOCK_IRQRESTORE(&pB->read_fifo_spinlock,bflags);
  1324. #ifdef USE_IQ
  1325. schedule_work(&pCh->tqueue_input);
  1326. #else
  1327. do_input(&pCh->tqueue_input);
  1328. #endif
  1329. // Note we do not need to maintain any flow-control credits at this
  1330. // time: if we were to increment .asof and decrement .room, there
  1331. // would be no net effect. Instead, when we strip data, we will
  1332. // increment .asof and leave .room unchanged.
  1333. break; // From switch: ready for next packet
  1334. case PTYPE_STATUS:
  1335. ip2trace (ITRC_NO_PORT, ITRC_SFIFO, 4, 0 );
  1336. count = CMD_COUNT_OF(pB->i2eLeadoffWord);
  1337. iiReadBuf(pB, cmdBuffer, count);
  1338. // We can release early with buffer grab
  1339. WRITE_UNLOCK_IRQRESTORE(&pB->read_fifo_spinlock,bflags);
  1340. pc = cmdBuffer;
  1341. pcLimit = &(cmdBuffer[count]);
  1342. while (pc < pcLimit) {
  1343. channel = *pc++;
  1344. ip2trace (channel, ITRC_SFIFO, 7, 2, channel, *pc );
  1345. /* check for valid channel */
  1346. if (channel < pB->i2eChannelCnt
  1347. &&
  1348. (pCh = (((i2ChanStrPtr*)pB->i2eChannelPtr)[channel])) != NULL
  1349. )
  1350. {
  1351. dss_change = 0;
  1352. switch (uc = *pc++)
  1353. {
  1354. /* Breaks and modem signals are easy: just update status */
  1355. case STAT_CTS_UP:
  1356. if ( !(pCh->dataSetIn & I2_CTS) )
  1357. {
  1358. pCh->dataSetIn |= I2_DCTS;
  1359. pCh->icount.cts++;
  1360. dss_change = 1;
  1361. }
  1362. pCh->dataSetIn |= I2_CTS;
  1363. break;
  1364. case STAT_CTS_DN:
  1365. if ( pCh->dataSetIn & I2_CTS )
  1366. {
  1367. pCh->dataSetIn |= I2_DCTS;
  1368. pCh->icount.cts++;
  1369. dss_change = 1;
  1370. }
  1371. pCh->dataSetIn &= ~I2_CTS;
  1372. break;
  1373. case STAT_DCD_UP:
  1374. ip2trace (channel, ITRC_MODEM, 1, 1, pCh->dataSetIn );
  1375. if ( !(pCh->dataSetIn & I2_DCD) )
  1376. {
  1377. ip2trace (CHANN, ITRC_MODEM, 2, 0 );
  1378. pCh->dataSetIn |= I2_DDCD;
  1379. pCh->icount.dcd++;
  1380. dss_change = 1;
  1381. }
  1382. pCh->dataSetIn |= I2_DCD;
  1383. ip2trace (channel, ITRC_MODEM, 3, 1, pCh->dataSetIn );
  1384. break;
  1385. case STAT_DCD_DN:
  1386. ip2trace (channel, ITRC_MODEM, 4, 1, pCh->dataSetIn );
  1387. if ( pCh->dataSetIn & I2_DCD )
  1388. {
  1389. ip2trace (channel, ITRC_MODEM, 5, 0 );
  1390. pCh->dataSetIn |= I2_DDCD;
  1391. pCh->icount.dcd++;
  1392. dss_change = 1;
  1393. }
  1394. pCh->dataSetIn &= ~I2_DCD;
  1395. ip2trace (channel, ITRC_MODEM, 6, 1, pCh->dataSetIn );
  1396. break;
  1397. case STAT_DSR_UP:
  1398. if ( !(pCh->dataSetIn & I2_DSR) )
  1399. {
  1400. pCh->dataSetIn |= I2_DDSR;
  1401. pCh->icount.dsr++;
  1402. dss_change = 1;
  1403. }
  1404. pCh->dataSetIn |= I2_DSR;
  1405. break;
  1406. case STAT_DSR_DN:
  1407. if ( pCh->dataSetIn & I2_DSR )
  1408. {
  1409. pCh->dataSetIn |= I2_DDSR;
  1410. pCh->icount.dsr++;
  1411. dss_change = 1;
  1412. }
  1413. pCh->dataSetIn &= ~I2_DSR;
  1414. break;
  1415. case STAT_RI_UP:
  1416. if ( !(pCh->dataSetIn & I2_RI) )
  1417. {
  1418. pCh->dataSetIn |= I2_DRI;
  1419. pCh->icount.rng++;
  1420. dss_change = 1;
  1421. }
  1422. pCh->dataSetIn |= I2_RI ;
  1423. break;
  1424. case STAT_RI_DN:
  1425. // to be compat with serial.c
  1426. //if ( pCh->dataSetIn & I2_RI )
  1427. //{
  1428. // pCh->dataSetIn |= I2_DRI;
  1429. // pCh->icount.rng++;
  1430. // dss_change = 1;
  1431. //}
  1432. pCh->dataSetIn &= ~I2_RI ;
  1433. break;
  1434. case STAT_BRK_DET:
  1435. pCh->dataSetIn |= I2_BRK;
  1436. pCh->icount.brk++;
  1437. dss_change = 1;
  1438. break;
  1439. // Bookmarks? one less request we're waiting for
  1440. case STAT_BMARK:
  1441. pCh->bookMarks--;
  1442. if (pCh->bookMarks <= 0 ) {
  1443. pCh->bookMarks = 0;
  1444. wake_up_interruptible( &pCh->pBookmarkWait );
  1445. ip2trace (channel, ITRC_DRAIN, 20, 1, pCh->BookmarkTimer.expires );
  1446. }
  1447. break;
  1448. // Flow control packets? Update the new credits, and if
  1449. // someone was waiting for output, queue him up again.
  1450. case STAT_FLOW:
  1451. pCh->outfl.room =
  1452. ((flowStatPtr)pc)->room -
  1453. (pCh->outfl.asof - ((flowStatPtr)pc)->asof);
  1454. ip2trace (channel, ITRC_STFLW, 1, 1, pCh->outfl.room );
  1455. if (pCh->channelNeeds & NEED_CREDIT)
  1456. {
  1457. ip2trace (channel, ITRC_STFLW, 2, 1, pCh->channelNeeds);
  1458. pCh->channelNeeds &= ~NEED_CREDIT;
  1459. i2QueueNeeds(pB, pCh, NEED_INLINE);
  1460. if ( pCh->pTTY )
  1461. ip2_owake(pCh->pTTY);
  1462. }
  1463. ip2trace (channel, ITRC_STFLW, 3, 1, pCh->channelNeeds);
  1464. pc += sizeof(flowStat);
  1465. break;
  1466. /* Special packets: */
  1467. /* Just copy the information into the channel structure */
  1468. case STAT_STATUS:
  1469. pCh->channelStatus = *((debugStatPtr)pc);
  1470. pc += sizeof(debugStat);
  1471. break;
  1472. case STAT_TXCNT:
  1473. pCh->channelTcount = *((cntStatPtr)pc);
  1474. pc += sizeof(cntStat);
  1475. break;
  1476. case STAT_RXCNT:
  1477. pCh->channelRcount = *((cntStatPtr)pc);
  1478. pc += sizeof(cntStat);
  1479. break;
  1480. case STAT_BOXIDS:
  1481. pB->channelBtypes = *((bidStatPtr)pc);
  1482. pc += sizeof(bidStat);
  1483. set_baud_params(pB);
  1484. break;
  1485. case STAT_HWFAIL:
  1486. i2QueueCommands (PTYPE_INLINE, pCh, 0, 1, CMD_HW_TEST);
  1487. pCh->channelFail = *((failStatPtr)pc);
  1488. pc += sizeof(failStat);
  1489. break;
  1490. /* No explicit match? then
  1491. * Might be an error packet...
  1492. */
  1493. default:
  1494. switch (uc & STAT_MOD_ERROR)
  1495. {
  1496. case STAT_ERROR:
  1497. if (uc & STAT_E_PARITY) {
  1498. pCh->dataSetIn |= I2_PAR;
  1499. pCh->icount.parity++;
  1500. }
  1501. if (uc & STAT_E_FRAMING){
  1502. pCh->dataSetIn |= I2_FRA;
  1503. pCh->icount.frame++;
  1504. }
  1505. if (uc & STAT_E_OVERRUN){
  1506. pCh->dataSetIn |= I2_OVR;
  1507. pCh->icount.overrun++;
  1508. }
  1509. break;
  1510. case STAT_MODEM:
  1511. // the answer to DSS_NOW request (not change)
  1512. pCh->dataSetIn = (pCh->dataSetIn
  1513. & ~(I2_RI | I2_CTS | I2_DCD | I2_DSR) )
  1514. | xlatDss[uc & 0xf];
  1515. wake_up_interruptible ( &pCh->dss_now_wait );
  1516. default:
  1517. break;
  1518. }
  1519. } /* End of switch on status type */
  1520. if (dss_change) {
  1521. #ifdef USE_IQ
  1522. schedule_work(&pCh->tqueue_status);
  1523. #else
  1524. do_status(&pCh->tqueue_status);
  1525. #endif
  1526. }
  1527. }
  1528. else /* Or else, channel is invalid */
  1529. {
  1530. // Even though the channel is invalid, we must test the
  1531. // status to see how much additional data it has (to be
  1532. // skipped)
  1533. switch (*pc++)
  1534. {
  1535. case STAT_FLOW:
  1536. pc += 4; /* Skip the data */
  1537. break;
  1538. default:
  1539. break;
  1540. }
  1541. }
  1542. } // End of while (there is still some status packet left)
  1543. break;
  1544. default: // Neither packet? should be impossible
  1545. ip2trace (ITRC_NO_PORT, ITRC_SFIFO, 5, 1,
  1546. PTYPE_OF(pB->i2eLeadoffWord) );
  1547. WRITE_UNLOCK_IRQRESTORE(&pB->read_fifo_spinlock,
  1548. bflags);
  1549. break;
  1550. } // End of switch on type of packets
  1551. } //while(board HAS_INPUT)
  1552. ip2trace (ITRC_NO_PORT, ITRC_SFIFO, ITRC_RETURN, 0 );
  1553. // Send acknowledgement to the board even if there was no data!
  1554. pB->i2eOutMailWaiting |= MB_IN_STRIPPED;
  1555. return;
  1556. }
  1557. //******************************************************************************
  1558. // Function: i2Write2Fifo(pB,address,count)
  1559. // Parameters: Pointer to a board structure, source address, byte count
  1560. // Returns: bytes written
  1561. //
  1562. // Description:
  1563. // Writes count bytes to board io address(implied) from source
  1564. // Adjusts count, leaves reserve for next time around bypass cmds
  1565. //******************************************************************************
  1566. static int
  1567. i2Write2Fifo(i2eBordStrPtr pB, unsigned char *source, int count,int reserve)
  1568. {
  1569. int rc = 0;
  1570. unsigned long flags;
  1571. WRITE_LOCK_IRQSAVE(&pB->write_fifo_spinlock,flags);
  1572. if (!pB->i2eWaitingForEmptyFifo) {
  1573. if (pB->i2eFifoRemains > (count+reserve)) {
  1574. pB->i2eFifoRemains -= count;
  1575. iiWriteBuf(pB, source, count);
  1576. pB->i2eOutMailWaiting |= MB_OUT_STUFFED;
  1577. rc = count;
  1578. }
  1579. }
  1580. WRITE_UNLOCK_IRQRESTORE(&pB->write_fifo_spinlock,flags);
  1581. return rc;
  1582. }
  1583. //******************************************************************************
  1584. // Function: i2StuffFifoBypass(pB)
  1585. // Parameters: Pointer to a board structure
  1586. // Returns: Nothing
  1587. //
  1588. // Description:
  1589. // Stuffs as many bypass commands into the fifo as possible. This is simpler
  1590. // than stuffing data or inline commands to fifo, since we do not have
  1591. // flow-control to deal with.
  1592. //******************************************************************************
  1593. static inline void
  1594. i2StuffFifoBypass(i2eBordStrPtr pB)
  1595. {
  1596. i2ChanStrPtr pCh;
  1597. unsigned char *pRemove;
  1598. unsigned short stripIndex;
  1599. unsigned short packetSize;
  1600. unsigned short paddedSize;
  1601. unsigned short notClogged = 1;
  1602. unsigned long flags;
  1603. int bailout = 1000;
  1604. // Continue processing so long as there are entries, or there is room in the
  1605. // fifo. Each entry represents a channel with something to do.
  1606. while ( --bailout && notClogged &&
  1607. (NULL != (pCh = i2DeQueueNeeds(pB,NEED_BYPASS))))
  1608. {
  1609. WRITE_LOCK_IRQSAVE(&pCh->Cbuf_spinlock,flags);
  1610. stripIndex = pCh->Cbuf_strip;
  1611. // as long as there are packets for this channel...
  1612. while (stripIndex != pCh->Cbuf_stuff) {
  1613. pRemove = &(pCh->Cbuf[stripIndex]);
  1614. packetSize = CMD_COUNT_OF(pRemove) + sizeof(i2CmdHeader);
  1615. paddedSize = ROUNDUP(packetSize);
  1616. if (paddedSize > 0) {
  1617. if ( 0 == i2Write2Fifo(pB, pRemove, paddedSize,0)) {
  1618. notClogged = 0; /* fifo full */
  1619. i2QueueNeeds(pB, pCh, NEED_BYPASS); // Put back on queue
  1620. break; // Break from the channel
  1621. }
  1622. }
  1623. #ifdef DEBUG_FIFO
  1624. WriteDBGBuf("BYPS", pRemove, paddedSize);
  1625. #endif /* DEBUG_FIFO */
  1626. pB->debugBypassCount++;
  1627. pRemove += packetSize;
  1628. stripIndex += packetSize;
  1629. if (stripIndex >= CBUF_SIZE) {
  1630. stripIndex = 0;
  1631. pRemove = pCh->Cbuf;
  1632. }
  1633. }
  1634. // Done with this channel. Move to next, removing this one from
  1635. // the queue of channels if we cleaned it out (i.e., didn't get clogged.
  1636. pCh->Cbuf_strip = stripIndex;
  1637. WRITE_UNLOCK_IRQRESTORE(&pCh->Cbuf_spinlock,flags);
  1638. } // Either clogged or finished all the work
  1639. #ifdef IP2DEBUG_TRACE
  1640. if ( !bailout ) {
  1641. ip2trace (ITRC_NO_PORT, ITRC_ERROR, 1, 0 );
  1642. }
  1643. #endif
  1644. }
  1645. //******************************************************************************
  1646. // Function: i2StuffFifoFlow(pB)
  1647. // Parameters: Pointer to a board structure
  1648. // Returns: Nothing
  1649. //
  1650. // Description:
  1651. // Stuffs as many flow control packets into the fifo as possible. This is easier
  1652. // even than doing normal bypass commands, because there is always at most one
  1653. // packet, already assembled, for each channel.
  1654. //******************************************************************************
  1655. static inline void
  1656. i2StuffFifoFlow(i2eBordStrPtr pB)
  1657. {
  1658. i2ChanStrPtr pCh;
  1659. unsigned short paddedSize = ROUNDUP(sizeof(flowIn));
  1660. ip2trace (ITRC_NO_PORT, ITRC_SFLOW, ITRC_ENTER, 2,
  1661. pB->i2eFifoRemains, paddedSize );
  1662. // Continue processing so long as there are entries, or there is room in the
  1663. // fifo. Each entry represents a channel with something to do.
  1664. while ( (NULL != (pCh = i2DeQueueNeeds(pB,NEED_FLOW)))) {
  1665. pB->debugFlowCount++;
  1666. // NO Chan LOCK needed ???
  1667. if ( 0 == i2Write2Fifo(pB,(unsigned char *)&(pCh->infl),paddedSize,0)) {
  1668. break;
  1669. }
  1670. #ifdef DEBUG_FIFO
  1671. WriteDBGBuf("FLOW",(unsigned char *) &(pCh->infl), paddedSize);
  1672. #endif /* DEBUG_FIFO */
  1673. } // Either clogged or finished all the work
  1674. ip2trace (ITRC_NO_PORT, ITRC_SFLOW, ITRC_RETURN, 0 );
  1675. }
  1676. //******************************************************************************
  1677. // Function: i2StuffFifoInline(pB)
  1678. // Parameters: Pointer to a board structure
  1679. // Returns: Nothing
  1680. //
  1681. // Description:
  1682. // Stuffs as much data and inline commands into the fifo as possible. This is
  1683. // the most complex fifo-stuffing operation, since there if now the channel
  1684. // flow-control issue to deal with.
  1685. //******************************************************************************
  1686. static inline void
  1687. i2StuffFifoInline(i2eBordStrPtr pB)
  1688. {
  1689. i2ChanStrPtr pCh;
  1690. unsigned char *pRemove;
  1691. unsigned short stripIndex;
  1692. unsigned short packetSize;
  1693. unsigned short paddedSize;
  1694. unsigned short notClogged = 1;
  1695. unsigned short flowsize;
  1696. unsigned long flags;
  1697. int bailout = 1000;
  1698. int bailout2;
  1699. ip2trace (ITRC_NO_PORT, ITRC_SICMD, ITRC_ENTER, 3, pB->i2eFifoRemains,
  1700. pB->i2Dbuf_strip, pB->i2Dbuf_stuff );
  1701. // Continue processing so long as there are entries, or there is room in the
  1702. // fifo. Each entry represents a channel with something to do.
  1703. while ( --bailout && notClogged &&
  1704. (NULL != (pCh = i2DeQueueNeeds(pB,NEED_INLINE))) )
  1705. {
  1706. WRITE_LOCK_IRQSAVE(&pCh->Obuf_spinlock,flags);
  1707. stripIndex = pCh->Obuf_strip;
  1708. ip2trace (CHANN, ITRC_SICMD, 3, 2, stripIndex, pCh->Obuf_stuff );
  1709. // as long as there are packets for this channel...
  1710. bailout2 = 1000;
  1711. while ( --bailout2 && stripIndex != pCh->Obuf_stuff) {
  1712. pRemove = &(pCh->Obuf[stripIndex]);
  1713. // Must determine whether this be a data or command packet to
  1714. // calculate correctly the header size and the amount of
  1715. // flow-control credit this type of packet will use.
  1716. if (PTYPE_OF(pRemove) == PTYPE_DATA) {
  1717. flowsize = DATA_COUNT_OF(pRemove);
  1718. packetSize = flowsize + sizeof(i2DataHeader);
  1719. } else {
  1720. flowsize = CMD_COUNT_OF(pRemove);
  1721. packetSize = flowsize + sizeof(i2CmdHeader);
  1722. }
  1723. flowsize = CREDIT_USAGE(flowsize);
  1724. paddedSize = ROUNDUP(packetSize);
  1725. ip2trace (CHANN, ITRC_SICMD, 4, 2, pB->i2eFifoRemains, paddedSize );
  1726. // If we don't have enough credits from the board to send the data,
  1727. // flag the channel that we are waiting for flow control credit, and
  1728. // break out. This will clean up this channel and remove us from the
  1729. // queue of hot things to do.
  1730. ip2trace (CHANN, ITRC_SICMD, 5, 2, pCh->outfl.room, flowsize );
  1731. if (pCh->outfl.room <= flowsize) {
  1732. // Do Not have the credits to send this packet.
  1733. i2QueueNeeds(pB, pCh, NEED_CREDIT);
  1734. notClogged = 0;
  1735. break; // So to do next channel
  1736. }
  1737. if ( (paddedSize > 0)
  1738. && ( 0 == i2Write2Fifo(pB, pRemove, paddedSize, 128))) {
  1739. // Do Not have room in fifo to send this packet.
  1740. notClogged = 0;
  1741. i2QueueNeeds(pB, pCh, NEED_INLINE);
  1742. break; // Break from the channel
  1743. }
  1744. #ifdef DEBUG_FIFO
  1745. WriteDBGBuf("DATA", pRemove, paddedSize);
  1746. #endif /* DEBUG_FIFO */
  1747. pB->debugInlineCount++;
  1748. pCh->icount.tx += flowsize;
  1749. // Update current credits
  1750. pCh->outfl.room -= flowsize;
  1751. pCh->outfl.asof += flowsize;
  1752. if (PTYPE_OF(pRemove) == PTYPE_DATA) {
  1753. pCh->Obuf_char_count -= DATA_COUNT_OF(pRemove);
  1754. }
  1755. pRemove += packetSize;
  1756. stripIndex += packetSize;
  1757. ip2trace (CHANN, ITRC_SICMD, 6, 2, stripIndex, pCh->Obuf_strip);
  1758. if (stripIndex >= OBUF_SIZE) {
  1759. stripIndex = 0;
  1760. pRemove = pCh->Obuf;
  1761. ip2trace (CHANN, ITRC_SICMD, 7, 1, stripIndex );
  1762. }
  1763. } /* while */
  1764. if ( !bailout2 ) {
  1765. ip2trace (CHANN, ITRC_ERROR, 3, 0 );
  1766. }
  1767. // Done with this channel. Move to next, removing this one from the
  1768. // queue of channels if we cleaned it out (i.e., didn't get clogged.
  1769. pCh->Obuf_strip = stripIndex;
  1770. WRITE_UNLOCK_IRQRESTORE(&pCh->Obuf_spinlock,flags);
  1771. if ( notClogged )
  1772. {
  1773. ip2trace (CHANN, ITRC_SICMD, 8, 0 );
  1774. if ( pCh->pTTY ) {
  1775. ip2_owake(pCh->pTTY);
  1776. }
  1777. }
  1778. } // Either clogged or finished all the work
  1779. if ( !bailout ) {
  1780. ip2trace (ITRC_NO_PORT, ITRC_ERROR, 4, 0 );
  1781. }
  1782. ip2trace (ITRC_NO_PORT, ITRC_SICMD, ITRC_RETURN, 1,pB->i2Dbuf_strip);
  1783. }
  1784. //******************************************************************************
  1785. // Function: serviceOutgoingFifo(pB)
  1786. // Parameters: Pointer to a board structure
  1787. // Returns: Nothing
  1788. //
  1789. // Description:
  1790. // Helper routine to put data in the outgoing fifo, if we aren't already waiting
  1791. // for something to be there. If the fifo has only room for a very little data,
  1792. // go head and hit the board with a mailbox hit immediately. Otherwise, it will
  1793. // have to happen later in the interrupt processing. Since this routine may be
  1794. // called both at interrupt and foreground time, we must turn off interrupts
  1795. // during the entire process.
  1796. //******************************************************************************
  1797. static void
  1798. serviceOutgoingFifo(i2eBordStrPtr pB)
  1799. {
  1800. // If we aren't currently waiting for the board to empty our fifo, service
  1801. // everything that is pending, in priority order (especially, Bypass before
  1802. // Inline).
  1803. if ( ! pB->i2eWaitingForEmptyFifo )
  1804. {
  1805. i2StuffFifoFlow(pB);
  1806. i2StuffFifoBypass(pB);
  1807. i2StuffFifoInline(pB);
  1808. iiSendPendingMail(pB);
  1809. }
  1810. }
  1811. //******************************************************************************
  1812. // Function: i2ServiceBoard(pB)
  1813. // Parameters: Pointer to a board structure
  1814. // Returns: Nothing
  1815. //
  1816. // Description:
  1817. // Normally this is called from interrupt level, but there is deliberately
  1818. // nothing in here specific to being called from interrupt level. All the
  1819. // hardware-specific, interrupt-specific things happen at the outer levels.
  1820. //
  1821. // For example, a timer interrupt could drive this routine for some sort of
  1822. // polled operation. The only requirement is that the programmer deal with any
  1823. // atomiticity/concurrency issues that result.
  1824. //
  1825. // This routine responds to the board's having sent mailbox information to the
  1826. // host (which would normally cause an interrupt). This routine reads the
  1827. // incoming mailbox. If there is no data in it, this board did not create the
  1828. // interrupt and/or has nothing to be done to it. (Except, if we have been
  1829. // waiting to write mailbox data to it, we may do so.
  1830. //
  1831. // Based on the value in the mailbox, we may take various actions.
  1832. //
  1833. // No checking here of pB validity: after all, it shouldn't have been called by
  1834. // the handler unless pB were on the list.
  1835. //******************************************************************************
  1836. static inline int
  1837. i2ServiceBoard ( i2eBordStrPtr pB )
  1838. {
  1839. unsigned inmail;
  1840. unsigned long flags;
  1841. /* This should be atomic because of the way we are called... */
  1842. if (NO_MAIL_HERE == ( inmail = pB->i2eStartMail ) ) {
  1843. inmail = iiGetMail(pB);
  1844. }
  1845. pB->i2eStartMail = NO_MAIL_HERE;
  1846. ip2trace (ITRC_NO_PORT, ITRC_INTR, 2, 1, inmail );
  1847. if (inmail != NO_MAIL_HERE) {
  1848. // If the board has gone fatal, nothing to do but hit a bit that will
  1849. // alert foreground tasks to protest!
  1850. if ( inmail & MB_FATAL_ERROR ) {
  1851. pB->i2eFatal = 1;
  1852. goto exit_i2ServiceBoard;
  1853. }
  1854. /* Assuming no fatal condition, we proceed to do work */
  1855. if ( inmail & MB_IN_STUFFED ) {
  1856. pB->i2eFifoInInts++;
  1857. i2StripFifo(pB); /* There might be incoming packets */
  1858. }
  1859. if (inmail & MB_OUT_STRIPPED) {
  1860. pB->i2eFifoOutInts++;
  1861. WRITE_LOCK_IRQSAVE(&pB->write_fifo_spinlock,flags);
  1862. pB->i2eFifoRemains = pB->i2eFifoSize;
  1863. pB->i2eWaitingForEmptyFifo = 0;
  1864. WRITE_UNLOCK_IRQRESTORE(&pB->write_fifo_spinlock,flags);
  1865. ip2trace (ITRC_NO_PORT, ITRC_INTR, 30, 1, pB->i2eFifoRemains );
  1866. }
  1867. serviceOutgoingFifo(pB);
  1868. }
  1869. ip2trace (ITRC_NO_PORT, ITRC_INTR, 8, 0 );
  1870. exit_i2ServiceBoard:
  1871. return 0;
  1872. }