sx.c 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884
  1. /* sx.c -- driver for the Specialix SX series cards.
  2. *
  3. * This driver will also support the older SI, and XIO cards.
  4. *
  5. *
  6. * (C) 1998 - 2004 R.E.Wolff@BitWizard.nl
  7. *
  8. * Simon Allen (simonallen@cix.compulink.co.uk) wrote a previous
  9. * version of this driver. Some fragments may have been copied. (none
  10. * yet :-)
  11. *
  12. * Specialix pays for the development and support of this driver.
  13. * Please DO contact support@specialix.co.uk if you require
  14. * support. But please read the documentation (sx.txt) first.
  15. *
  16. *
  17. *
  18. * This program is free software; you can redistribute it and/or
  19. * modify it under the terms of the GNU General Public License as
  20. * published by the Free Software Foundation; either version 2 of
  21. * the License, or (at your option) any later version.
  22. *
  23. * This program is distributed in the hope that it will be
  24. * useful, but WITHOUT ANY WARRANTY; without even the implied
  25. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  26. * PURPOSE. See the GNU General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU General Public
  29. * License along with this program; if not, write to the Free
  30. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
  31. * USA.
  32. *
  33. * Revision history:
  34. * Revision 1.33 2000/03/09 10:00:00 pvdl,wolff
  35. * - Fixed module and port counting
  36. * - Fixed signal handling
  37. * - Fixed an Ooops
  38. *
  39. * Revision 1.32 2000/03/07 09:00:00 wolff,pvdl
  40. * - Fixed some sx_dprintk typos
  41. * - added detection for an invalid board/module configuration
  42. *
  43. * Revision 1.31 2000/03/06 12:00:00 wolff,pvdl
  44. * - Added support for EISA
  45. *
  46. * Revision 1.30 2000/01/21 17:43:06 wolff
  47. * - Added support for SX+
  48. *
  49. * Revision 1.26 1999/08/05 15:22:14 wolff
  50. * - Port to 2.3.x
  51. * - Reformatted to Linus' liking.
  52. *
  53. * Revision 1.25 1999/07/30 14:24:08 wolff
  54. * Had accidentally left "gs_debug" set to "-1" instead of "off" (=0).
  55. *
  56. * Revision 1.24 1999/07/28 09:41:52 wolff
  57. * - I noticed the remark about use-count straying in sx.txt. I checked
  58. * sx_open, and found a few places where that could happen. I hope it's
  59. * fixed now.
  60. *
  61. * Revision 1.23 1999/07/28 08:56:06 wolff
  62. * - Fixed crash when sx_firmware run twice.
  63. * - Added sx_slowpoll as a module parameter (I guess nobody really wanted
  64. * to change it from the default... )
  65. * - Fixed a stupid editing problem I introduced in 1.22.
  66. * - Fixed dropping characters on a termios change.
  67. *
  68. * Revision 1.22 1999/07/26 21:01:43 wolff
  69. * Russell Brown noticed that I had overlooked 4 out of six modem control
  70. * signals in sx_getsignals. Ooops.
  71. *
  72. * Revision 1.21 1999/07/23 09:11:33 wolff
  73. * I forgot to free dynamically allocated memory when the driver is unloaded.
  74. *
  75. * Revision 1.20 1999/07/20 06:25:26 wolff
  76. * The "closing wait" wasn't honoured. Thanks to James Griffiths for
  77. * reporting this.
  78. *
  79. * Revision 1.19 1999/07/11 08:59:59 wolff
  80. * Fixed an oops in close, when an open was pending. Changed the memtest
  81. * a bit. Should also test the board in word-mode, however my card fails the
  82. * memtest then. I still have to figure out what is wrong...
  83. *
  84. * Revision 1.18 1999/06/10 09:38:42 wolff
  85. * Changed the format of the firmware revision from %04x to %x.%02x .
  86. *
  87. * Revision 1.17 1999/06/04 09:44:35 wolff
  88. * fixed problem: reference to pci stuff when config_pci was off...
  89. * Thanks to Jorge Novo for noticing this.
  90. *
  91. * Revision 1.16 1999/06/02 08:30:15 wolff
  92. * added/removed the workaround for the DCD bug in the Firmware.
  93. * A bit more debugging code to locate that...
  94. *
  95. * Revision 1.15 1999/06/01 11:35:30 wolff
  96. * when DCD is left low (floating?), on TA's the firmware first tells us
  97. * that DCD is high, but after a short while suddenly comes to the
  98. * conclusion that it is low. All this would be fine, if it weren't that
  99. * Unix requires us to send a "hangup" signal in that case. This usually
  100. * all happens BEFORE the program has had a chance to ioctl the device
  101. * into clocal mode..
  102. *
  103. * Revision 1.14 1999/05/25 11:18:59 wolff
  104. * Added PCI-fix.
  105. * Added checks for return code of sx_sendcommand.
  106. * Don't issue "reconfig" if port isn't open yet. (bit us on TA modules...)
  107. *
  108. * Revision 1.13 1999/04/29 15:18:01 wolff
  109. * Fixed an "oops" that showed on SuSE 6.0 systems.
  110. * Activate DTR again after stty 0.
  111. *
  112. * Revision 1.12 1999/04/29 07:49:52 wolff
  113. * Improved "stty 0" handling a bit. (used to change baud to 9600 assuming
  114. * the connection would be dropped anyway. That is not always the case,
  115. * and confuses people).
  116. * Told the card to always monitor the modem signals.
  117. * Added support for dynamic gs_debug adjustments.
  118. * Now tells the rest of the system the number of ports.
  119. *
  120. * Revision 1.11 1999/04/24 11:11:30 wolff
  121. * Fixed two stupid typos in the memory test.
  122. *
  123. * Revision 1.10 1999/04/24 10:53:39 wolff
  124. * Added some of Christian's suggestions.
  125. * Fixed an HW_COOK_IN bug (ISIG was not in I_OTHER. We used to trust the
  126. * card to send the signal to the process.....)
  127. *
  128. * Revision 1.9 1999/04/23 07:26:38 wolff
  129. * Included Christian Lademann's 2.0 compile-warning fixes and interrupt
  130. * assignment redesign.
  131. * Cleanup of some other stuff.
  132. *
  133. * Revision 1.8 1999/04/16 13:05:30 wolff
  134. * fixed a DCD change unnoticed bug.
  135. *
  136. * Revision 1.7 1999/04/14 22:19:51 wolff
  137. * Fixed typo that showed up in 2.0.x builds (get_user instead of Get_user!)
  138. *
  139. * Revision 1.6 1999/04/13 18:40:20 wolff
  140. * changed misc-minor to 161, as assigned by HPA.
  141. *
  142. * Revision 1.5 1999/04/13 15:12:25 wolff
  143. * Fixed use-count leak when "hangup" occurred.
  144. * Added workaround for a stupid-PCIBIOS bug.
  145. *
  146. *
  147. * Revision 1.4 1999/04/01 22:47:40 wolff
  148. * Fixed < 1M linux-2.0 problem.
  149. * (vremap isn't compatible with ioremap in that case)
  150. *
  151. * Revision 1.3 1999/03/31 13:45:45 wolff
  152. * Firmware loading is now done through a separate IOCTL.
  153. *
  154. * Revision 1.2 1999/03/28 12:22:29 wolff
  155. * rcs cleanup
  156. *
  157. * Revision 1.1 1999/03/28 12:10:34 wolff
  158. * Readying for release on 2.0.x (sorry David, 1.01 becomes 1.1 for RCS).
  159. *
  160. * Revision 0.12 1999/03/28 09:20:10 wolff
  161. * Fixed problem in 0.11, continueing cleanup.
  162. *
  163. * Revision 0.11 1999/03/28 08:46:44 wolff
  164. * cleanup. Not good.
  165. *
  166. * Revision 0.10 1999/03/28 08:09:43 wolff
  167. * Fixed loosing characters on close.
  168. *
  169. * Revision 0.9 1999/03/21 22:52:01 wolff
  170. * Ported back to 2.2.... (minor things)
  171. *
  172. * Revision 0.8 1999/03/21 22:40:33 wolff
  173. * Port to 2.0
  174. *
  175. * Revision 0.7 1999/03/21 19:06:34 wolff
  176. * Fixed hangup processing.
  177. *
  178. * Revision 0.6 1999/02/05 08:45:14 wolff
  179. * fixed real_raw problems. Inclusion into kernel imminent.
  180. *
  181. * Revision 0.5 1998/12/21 23:51:06 wolff
  182. * Snatched a nasty bug: sx_transmit_chars was getting re-entered, and it
  183. * shouldn't have. THATs why I want to have transmit interrupts even when
  184. * the buffer is empty.
  185. *
  186. * Revision 0.4 1998/12/17 09:34:46 wolff
  187. * PPP works. ioctl works. Basically works!
  188. *
  189. * Revision 0.3 1998/12/15 13:05:18 wolff
  190. * It works! Wow! Gotta start implementing IOCTL and stuff....
  191. *
  192. * Revision 0.2 1998/12/01 08:33:53 wolff
  193. * moved over to 2.1.130
  194. *
  195. * Revision 0.1 1998/11/03 21:23:51 wolff
  196. * Initial revision. Detects SX card.
  197. *
  198. * */
  199. #define SX_VERSION 1.33
  200. #include <linux/module.h>
  201. #include <linux/kdev_t.h>
  202. #include <linux/kernel.h>
  203. #include <linux/sched.h>
  204. #include <linux/ioport.h>
  205. #include <linux/interrupt.h>
  206. #include <linux/errno.h>
  207. #include <linux/tty.h>
  208. #include <linux/tty_flip.h>
  209. #include <linux/mm.h>
  210. #include <linux/serial.h>
  211. #include <linux/fcntl.h>
  212. #include <linux/major.h>
  213. #include <linux/delay.h>
  214. #include <linux/eisa.h>
  215. #include <linux/pci.h>
  216. #include <linux/slab.h>
  217. #include <linux/init.h>
  218. #include <linux/miscdevice.h>
  219. #include <linux/bitops.h>
  220. #include <asm/io.h>
  221. #include <asm/uaccess.h>
  222. /* The 3.0.0 version of sxboards/sxwindow.h uses BYTE and WORD.... */
  223. #define BYTE u8
  224. #define WORD u16
  225. /* .... but the 3.0.4 version uses _u8 and _u16. */
  226. #define _u8 u8
  227. #define _u16 u16
  228. #include "sxboards.h"
  229. #include "sxwindow.h"
  230. #include <linux/generic_serial.h>
  231. #include "sx.h"
  232. /* I don't think that this driver can handle more than 256 ports on
  233. one machine. You'll have to increase the number of boards in sx.h
  234. if you want more than 4 boards. */
  235. #ifndef PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8
  236. #define PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8 0x2000
  237. #endif
  238. /* Configurable options:
  239. (Don't be too sure that it'll work if you toggle them) */
  240. /* Am I paranoid or not ? ;-) */
  241. #undef SX_PARANOIA_CHECK
  242. /* 20 -> 2000 per second. The card should rate-limit interrupts at 100
  243. Hz, but it is user configurable. I don't recommend going above 1000
  244. Hz. The interrupt ratelimit might trigger if the interrupt is
  245. shared with a very active other device. */
  246. #define IRQ_RATE_LIMIT 20
  247. /* Sharing interrupts is possible now. If the other device wants more
  248. than 2000 interrupts per second, we'd gracefully decline further
  249. interrupts. That's not what we want. On the other hand, if the
  250. other device interrupts 2000 times a second, don't use the SX
  251. interrupt. Use polling. */
  252. #undef IRQ_RATE_LIMIT
  253. #if 0
  254. /* Not implemented */
  255. /*
  256. * The following defines are mostly for testing purposes. But if you need
  257. * some nice reporting in your syslog, you can define them also.
  258. */
  259. #define SX_REPORT_FIFO
  260. #define SX_REPORT_OVERRUN
  261. #endif
  262. /* Function prototypes */
  263. static void sx_disable_tx_interrupts(void *ptr);
  264. static void sx_enable_tx_interrupts(void *ptr);
  265. static void sx_disable_rx_interrupts(void *ptr);
  266. static void sx_enable_rx_interrupts(void *ptr);
  267. static int sx_get_CD(void *ptr);
  268. static void sx_shutdown_port(void *ptr);
  269. static int sx_set_real_termios(void *ptr);
  270. static void sx_close(void *ptr);
  271. static int sx_chars_in_buffer(void *ptr);
  272. static int sx_init_board(struct sx_board *board);
  273. static int sx_init_portstructs(int nboards, int nports);
  274. static int sx_fw_ioctl(struct inode *inode, struct file *filp,
  275. unsigned int cmd, unsigned long arg);
  276. static int sx_init_drivers(void);
  277. static struct tty_driver *sx_driver;
  278. static DEFINE_MUTEX(sx_boards_lock);
  279. static struct sx_board boards[SX_NBOARDS];
  280. static struct sx_port *sx_ports;
  281. static int sx_initialized;
  282. static int sx_nports;
  283. static int sx_debug;
  284. /* You can have the driver poll your card.
  285. - Set sx_poll to 1 to poll every timer tick (10ms on Intel).
  286. This is used when the card cannot use an interrupt for some reason.
  287. - set sx_slowpoll to 100 to do an extra poll once a second (on Intel). If
  288. the driver misses an interrupt (report this if it DOES happen to you!)
  289. everything will continue to work....
  290. */
  291. static int sx_poll = 1;
  292. static int sx_slowpoll;
  293. /* The card limits the number of interrupts per second.
  294. At 115k2 "100" should be sufficient.
  295. If you're using higher baudrates, you can increase this...
  296. */
  297. static int sx_maxints = 100;
  298. #ifdef CONFIG_ISA
  299. /* These are the only open spaces in my computer. Yours may have more
  300. or less.... -- REW
  301. duh: Card at 0xa0000 is possible on HP Netserver?? -- pvdl
  302. */
  303. static int sx_probe_addrs[] = {
  304. 0xc0000, 0xd0000, 0xe0000,
  305. 0xc8000, 0xd8000, 0xe8000
  306. };
  307. static int si_probe_addrs[] = {
  308. 0xc0000, 0xd0000, 0xe0000,
  309. 0xc8000, 0xd8000, 0xe8000, 0xa0000
  310. };
  311. static int si1_probe_addrs[] = {
  312. 0xd0000
  313. };
  314. #define NR_SX_ADDRS ARRAY_SIZE(sx_probe_addrs)
  315. #define NR_SI_ADDRS ARRAY_SIZE(si_probe_addrs)
  316. #define NR_SI1_ADDRS ARRAY_SIZE(si1_probe_addrs)
  317. module_param_array(sx_probe_addrs, int, NULL, 0);
  318. module_param_array(si_probe_addrs, int, NULL, 0);
  319. #endif
  320. /* Set the mask to all-ones. This alas, only supports 32 interrupts.
  321. Some architectures may need more. */
  322. static int sx_irqmask = -1;
  323. module_param(sx_poll, int, 0);
  324. module_param(sx_slowpoll, int, 0);
  325. module_param(sx_maxints, int, 0);
  326. module_param(sx_debug, int, 0);
  327. module_param(sx_irqmask, int, 0);
  328. MODULE_LICENSE("GPL");
  329. static struct real_driver sx_real_driver = {
  330. sx_disable_tx_interrupts,
  331. sx_enable_tx_interrupts,
  332. sx_disable_rx_interrupts,
  333. sx_enable_rx_interrupts,
  334. sx_get_CD,
  335. sx_shutdown_port,
  336. sx_set_real_termios,
  337. sx_chars_in_buffer,
  338. sx_close,
  339. };
  340. /*
  341. This driver can spew a whole lot of debugging output at you. If you
  342. need maximum performance, you should disable the DEBUG define. To
  343. aid in debugging in the field, I'm leaving the compile-time debug
  344. features enabled, and disable them "runtime". That allows me to
  345. instruct people with problems to enable debugging without requiring
  346. them to recompile...
  347. */
  348. #define DEBUG
  349. #ifdef DEBUG
  350. #define sx_dprintk(f, str...) if (sx_debug & f) printk (str)
  351. #else
  352. #define sx_dprintk(f, str...) /* nothing */
  353. #endif
  354. #define func_enter() sx_dprintk(SX_DEBUG_FLOW, "sx: enter %s\n",__FUNCTION__)
  355. #define func_exit() sx_dprintk(SX_DEBUG_FLOW, "sx: exit %s\n",__FUNCTION__)
  356. #define func_enter2() sx_dprintk(SX_DEBUG_FLOW, "sx: enter %s (port %d)\n", \
  357. __FUNCTION__, port->line)
  358. /*
  359. * Firmware loader driver specific routines
  360. *
  361. */
  362. static const struct file_operations sx_fw_fops = {
  363. .owner = THIS_MODULE,
  364. .ioctl = sx_fw_ioctl,
  365. };
  366. static struct miscdevice sx_fw_device = {
  367. SXCTL_MISC_MINOR, "sxctl", &sx_fw_fops
  368. };
  369. #ifdef SX_PARANOIA_CHECK
  370. /* This doesn't work. Who's paranoid around here? Not me! */
  371. static inline int sx_paranoia_check(struct sx_port const *port,
  372. char *name, const char *routine)
  373. {
  374. static const char *badmagic = KERN_ERR "sx: Warning: bad sx port magic "
  375. "number for device %s in %s\n";
  376. static const char *badinfo = KERN_ERR "sx: Warning: null sx port for "
  377. "device %s in %s\n";
  378. if (!port) {
  379. printk(badinfo, name, routine);
  380. return 1;
  381. }
  382. if (port->magic != SX_MAGIC) {
  383. printk(badmagic, name, routine);
  384. return 1;
  385. }
  386. return 0;
  387. }
  388. #else
  389. #define sx_paranoia_check(a,b,c) 0
  390. #endif
  391. /* The timeouts. First try 30 times as fast as possible. Then give
  392. the card some time to breathe between accesses. (Otherwise the
  393. processor on the card might not be able to access its OWN bus... */
  394. #define TIMEOUT_1 30
  395. #define TIMEOUT_2 1000000
  396. #ifdef DEBUG
  397. static void my_hd_io(void __iomem *p, int len)
  398. {
  399. int i, j, ch;
  400. unsigned char __iomem *addr = p;
  401. for (i = 0; i < len; i += 16) {
  402. printk("%p ", addr + i);
  403. for (j = 0; j < 16; j++) {
  404. printk("%02x %s", readb(addr + j + i),
  405. (j == 7) ? " " : "");
  406. }
  407. for (j = 0; j < 16; j++) {
  408. ch = readb(addr + j + i);
  409. printk("%c", (ch < 0x20) ? '.' :
  410. ((ch > 0x7f) ? '.' : ch));
  411. }
  412. printk("\n");
  413. }
  414. }
  415. static void my_hd(void *p, int len)
  416. {
  417. int i, j, ch;
  418. unsigned char *addr = p;
  419. for (i = 0; i < len; i += 16) {
  420. printk("%p ", addr + i);
  421. for (j = 0; j < 16; j++) {
  422. printk("%02x %s", addr[j + i], (j == 7) ? " " : "");
  423. }
  424. for (j = 0; j < 16; j++) {
  425. ch = addr[j + i];
  426. printk("%c", (ch < 0x20) ? '.' :
  427. ((ch > 0x7f) ? '.' : ch));
  428. }
  429. printk("\n");
  430. }
  431. }
  432. #endif
  433. /* This needs redoing for Alpha -- REW -- Done. */
  434. static inline void write_sx_byte(struct sx_board *board, int offset, u8 byte)
  435. {
  436. writeb(byte, board->base + offset);
  437. }
  438. static inline u8 read_sx_byte(struct sx_board *board, int offset)
  439. {
  440. return readb(board->base + offset);
  441. }
  442. static inline void write_sx_word(struct sx_board *board, int offset, u16 word)
  443. {
  444. writew(word, board->base + offset);
  445. }
  446. static inline u16 read_sx_word(struct sx_board *board, int offset)
  447. {
  448. return readw(board->base + offset);
  449. }
  450. static int sx_busy_wait_eq(struct sx_board *board,
  451. int offset, int mask, int correctval)
  452. {
  453. int i;
  454. func_enter();
  455. for (i = 0; i < TIMEOUT_1; i++)
  456. if ((read_sx_byte(board, offset) & mask) == correctval) {
  457. func_exit();
  458. return 1;
  459. }
  460. for (i = 0; i < TIMEOUT_2; i++) {
  461. if ((read_sx_byte(board, offset) & mask) == correctval) {
  462. func_exit();
  463. return 1;
  464. }
  465. udelay(1);
  466. }
  467. func_exit();
  468. return 0;
  469. }
  470. static int sx_busy_wait_neq(struct sx_board *board,
  471. int offset, int mask, int badval)
  472. {
  473. int i;
  474. func_enter();
  475. for (i = 0; i < TIMEOUT_1; i++)
  476. if ((read_sx_byte(board, offset) & mask) != badval) {
  477. func_exit();
  478. return 1;
  479. }
  480. for (i = 0; i < TIMEOUT_2; i++) {
  481. if ((read_sx_byte(board, offset) & mask) != badval) {
  482. func_exit();
  483. return 1;
  484. }
  485. udelay(1);
  486. }
  487. func_exit();
  488. return 0;
  489. }
  490. /* 5.6.4 of 6210028 r2.3 */
  491. static int sx_reset(struct sx_board *board)
  492. {
  493. func_enter();
  494. if (IS_SX_BOARD(board)) {
  495. write_sx_byte(board, SX_CONFIG, 0);
  496. write_sx_byte(board, SX_RESET, 1); /* Value doesn't matter */
  497. if (!sx_busy_wait_eq(board, SX_RESET_STATUS, 1, 0)) {
  498. printk(KERN_INFO "sx: Card doesn't respond to "
  499. "reset...\n");
  500. return 0;
  501. }
  502. } else if (IS_EISA_BOARD(board)) {
  503. outb(board->irq << 4, board->eisa_base + 0xc02);
  504. } else if (IS_SI1_BOARD(board)) {
  505. write_sx_byte(board, SI1_ISA_RESET, 0); /*value doesn't matter*/
  506. } else {
  507. /* Gory details of the SI/ISA board */
  508. write_sx_byte(board, SI2_ISA_RESET, SI2_ISA_RESET_SET);
  509. write_sx_byte(board, SI2_ISA_IRQ11, SI2_ISA_IRQ11_CLEAR);
  510. write_sx_byte(board, SI2_ISA_IRQ12, SI2_ISA_IRQ12_CLEAR);
  511. write_sx_byte(board, SI2_ISA_IRQ15, SI2_ISA_IRQ15_CLEAR);
  512. write_sx_byte(board, SI2_ISA_INTCLEAR, SI2_ISA_INTCLEAR_CLEAR);
  513. write_sx_byte(board, SI2_ISA_IRQSET, SI2_ISA_IRQSET_CLEAR);
  514. }
  515. func_exit();
  516. return 1;
  517. }
  518. /* This doesn't work on machines where "NULL" isn't 0 */
  519. /* If you have one of those, someone will need to write
  520. the equivalent of this, which will amount to about 3 lines. I don't
  521. want to complicate this right now. -- REW
  522. (See, I do write comments every now and then :-) */
  523. #define OFFSETOF(strct, elem) ((long)&(((struct strct *)NULL)->elem))
  524. #define CHAN_OFFSET(port,elem) (port->ch_base + OFFSETOF (_SXCHANNEL, elem))
  525. #define MODU_OFFSET(board,addr,elem) (addr + OFFSETOF (_SXMODULE, elem))
  526. #define BRD_OFFSET(board,elem) (OFFSETOF (_SXCARD, elem))
  527. #define sx_write_channel_byte(port, elem, val) \
  528. write_sx_byte (port->board, CHAN_OFFSET (port, elem), val)
  529. #define sx_read_channel_byte(port, elem) \
  530. read_sx_byte (port->board, CHAN_OFFSET (port, elem))
  531. #define sx_write_channel_word(port, elem, val) \
  532. write_sx_word (port->board, CHAN_OFFSET (port, elem), val)
  533. #define sx_read_channel_word(port, elem) \
  534. read_sx_word (port->board, CHAN_OFFSET (port, elem))
  535. #define sx_write_module_byte(board, addr, elem, val) \
  536. write_sx_byte (board, MODU_OFFSET (board, addr, elem), val)
  537. #define sx_read_module_byte(board, addr, elem) \
  538. read_sx_byte (board, MODU_OFFSET (board, addr, elem))
  539. #define sx_write_module_word(board, addr, elem, val) \
  540. write_sx_word (board, MODU_OFFSET (board, addr, elem), val)
  541. #define sx_read_module_word(board, addr, elem) \
  542. read_sx_word (board, MODU_OFFSET (board, addr, elem))
  543. #define sx_write_board_byte(board, elem, val) \
  544. write_sx_byte (board, BRD_OFFSET (board, elem), val)
  545. #define sx_read_board_byte(board, elem) \
  546. read_sx_byte (board, BRD_OFFSET (board, elem))
  547. #define sx_write_board_word(board, elem, val) \
  548. write_sx_word (board, BRD_OFFSET (board, elem), val)
  549. #define sx_read_board_word(board, elem) \
  550. read_sx_word (board, BRD_OFFSET (board, elem))
  551. static int sx_start_board(struct sx_board *board)
  552. {
  553. if (IS_SX_BOARD(board)) {
  554. write_sx_byte(board, SX_CONFIG, SX_CONF_BUSEN);
  555. } else if (IS_EISA_BOARD(board)) {
  556. write_sx_byte(board, SI2_EISA_OFF, SI2_EISA_VAL);
  557. outb((board->irq << 4) | 4, board->eisa_base + 0xc02);
  558. } else if (IS_SI1_BOARD(board)) {
  559. write_sx_byte(board, SI1_ISA_RESET_CLEAR, 0);
  560. write_sx_byte(board, SI1_ISA_INTCL, 0);
  561. } else {
  562. /* Don't bug me about the clear_set.
  563. I haven't the foggiest idea what it's about -- REW */
  564. write_sx_byte(board, SI2_ISA_RESET, SI2_ISA_RESET_CLEAR);
  565. write_sx_byte(board, SI2_ISA_INTCLEAR, SI2_ISA_INTCLEAR_SET);
  566. }
  567. return 1;
  568. }
  569. #define SX_IRQ_REG_VAL(board) \
  570. ((board->flags & SX_ISA_BOARD) ? (board->irq << 4) : 0)
  571. /* Note. The SX register is write-only. Therefore, we have to enable the
  572. bus too. This is a no-op, if you don't mess with this driver... */
  573. static int sx_start_interrupts(struct sx_board *board)
  574. {
  575. /* Don't call this with board->irq == 0 */
  576. if (IS_SX_BOARD(board)) {
  577. write_sx_byte(board, SX_CONFIG, SX_IRQ_REG_VAL(board) |
  578. SX_CONF_BUSEN | SX_CONF_HOSTIRQ);
  579. } else if (IS_EISA_BOARD(board)) {
  580. inb(board->eisa_base + 0xc03);
  581. } else if (IS_SI1_BOARD(board)) {
  582. write_sx_byte(board, SI1_ISA_INTCL, 0);
  583. write_sx_byte(board, SI1_ISA_INTCL_CLEAR, 0);
  584. } else {
  585. switch (board->irq) {
  586. case 11:
  587. write_sx_byte(board, SI2_ISA_IRQ11, SI2_ISA_IRQ11_SET);
  588. break;
  589. case 12:
  590. write_sx_byte(board, SI2_ISA_IRQ12, SI2_ISA_IRQ12_SET);
  591. break;
  592. case 15:
  593. write_sx_byte(board, SI2_ISA_IRQ15, SI2_ISA_IRQ15_SET);
  594. break;
  595. default:
  596. printk(KERN_INFO "sx: SI/XIO card doesn't support "
  597. "interrupt %d.\n", board->irq);
  598. return 0;
  599. }
  600. write_sx_byte(board, SI2_ISA_INTCLEAR, SI2_ISA_INTCLEAR_SET);
  601. }
  602. return 1;
  603. }
  604. static int sx_send_command(struct sx_port *port,
  605. int command, int mask, int newstat)
  606. {
  607. func_enter2();
  608. write_sx_byte(port->board, CHAN_OFFSET(port, hi_hstat), command);
  609. func_exit();
  610. return sx_busy_wait_eq(port->board, CHAN_OFFSET(port, hi_hstat), mask,
  611. newstat);
  612. }
  613. static char *mod_type_s(int module_type)
  614. {
  615. switch (module_type) {
  616. case TA4:
  617. return "TA4";
  618. case TA8:
  619. return "TA8";
  620. case TA4_ASIC:
  621. return "TA4_ASIC";
  622. case TA8_ASIC:
  623. return "TA8_ASIC";
  624. case MTA_CD1400:
  625. return "MTA_CD1400";
  626. case SXDC:
  627. return "SXDC";
  628. default:
  629. return "Unknown/invalid";
  630. }
  631. }
  632. static char *pan_type_s(int pan_type)
  633. {
  634. switch (pan_type) {
  635. case MOD_RS232DB25:
  636. return "MOD_RS232DB25";
  637. case MOD_RS232RJ45:
  638. return "MOD_RS232RJ45";
  639. case MOD_RS422DB25:
  640. return "MOD_RS422DB25";
  641. case MOD_PARALLEL:
  642. return "MOD_PARALLEL";
  643. case MOD_2_RS232DB25:
  644. return "MOD_2_RS232DB25";
  645. case MOD_2_RS232RJ45:
  646. return "MOD_2_RS232RJ45";
  647. case MOD_2_RS422DB25:
  648. return "MOD_2_RS422DB25";
  649. case MOD_RS232DB25MALE:
  650. return "MOD_RS232DB25MALE";
  651. case MOD_2_PARALLEL:
  652. return "MOD_2_PARALLEL";
  653. case MOD_BLANK:
  654. return "empty";
  655. default:
  656. return "invalid";
  657. }
  658. }
  659. static int mod_compat_type(int module_type)
  660. {
  661. return module_type >> 4;
  662. }
  663. static void sx_reconfigure_port(struct sx_port *port)
  664. {
  665. if (sx_read_channel_byte(port, hi_hstat) == HS_IDLE_OPEN) {
  666. if (sx_send_command(port, HS_CONFIG, -1, HS_IDLE_OPEN) != 1) {
  667. printk(KERN_WARNING "sx: Sent reconfigure command, but "
  668. "card didn't react.\n");
  669. }
  670. } else {
  671. sx_dprintk(SX_DEBUG_TERMIOS, "sx: Not sending reconfigure: "
  672. "port isn't open (%02x).\n",
  673. sx_read_channel_byte(port, hi_hstat));
  674. }
  675. }
  676. static void sx_setsignals(struct sx_port *port, int dtr, int rts)
  677. {
  678. int t;
  679. func_enter2();
  680. t = sx_read_channel_byte(port, hi_op);
  681. if (dtr >= 0)
  682. t = dtr ? (t | OP_DTR) : (t & ~OP_DTR);
  683. if (rts >= 0)
  684. t = rts ? (t | OP_RTS) : (t & ~OP_RTS);
  685. sx_write_channel_byte(port, hi_op, t);
  686. sx_dprintk(SX_DEBUG_MODEMSIGNALS, "setsignals: %d/%d\n", dtr, rts);
  687. func_exit();
  688. }
  689. static int sx_getsignals(struct sx_port *port)
  690. {
  691. int i_stat, o_stat;
  692. o_stat = sx_read_channel_byte(port, hi_op);
  693. i_stat = sx_read_channel_byte(port, hi_ip);
  694. sx_dprintk(SX_DEBUG_MODEMSIGNALS, "getsignals: %d/%d (%d/%d) "
  695. "%02x/%02x\n",
  696. (o_stat & OP_DTR) != 0, (o_stat & OP_RTS) != 0,
  697. port->c_dcd, sx_get_CD(port),
  698. sx_read_channel_byte(port, hi_ip),
  699. sx_read_channel_byte(port, hi_state));
  700. return (((o_stat & OP_DTR) ? TIOCM_DTR : 0) |
  701. ((o_stat & OP_RTS) ? TIOCM_RTS : 0) |
  702. ((i_stat & IP_CTS) ? TIOCM_CTS : 0) |
  703. ((i_stat & IP_DCD) ? TIOCM_CAR : 0) |
  704. ((i_stat & IP_DSR) ? TIOCM_DSR : 0) |
  705. ((i_stat & IP_RI) ? TIOCM_RNG : 0));
  706. }
  707. static void sx_set_baud(struct sx_port *port)
  708. {
  709. int t;
  710. if (port->board->ta_type == MOD_SXDC) {
  711. switch (port->gs.baud) {
  712. /* Save some typing work... */
  713. #define e(x) case x: t = BAUD_ ## x; break
  714. e(50);
  715. e(75);
  716. e(110);
  717. e(150);
  718. e(200);
  719. e(300);
  720. e(600);
  721. e(1200);
  722. e(1800);
  723. e(2000);
  724. e(2400);
  725. e(4800);
  726. e(7200);
  727. e(9600);
  728. e(14400);
  729. e(19200);
  730. e(28800);
  731. e(38400);
  732. e(56000);
  733. e(57600);
  734. e(64000);
  735. e(76800);
  736. e(115200);
  737. e(128000);
  738. e(150000);
  739. e(230400);
  740. e(256000);
  741. e(460800);
  742. e(921600);
  743. case 134:
  744. t = BAUD_134_5;
  745. break;
  746. case 0:
  747. t = -1;
  748. break;
  749. default:
  750. /* Can I return "invalid"? */
  751. t = BAUD_9600;
  752. printk(KERN_INFO "sx: unsupported baud rate: %d.\n",
  753. port->gs.baud);
  754. break;
  755. }
  756. #undef e
  757. if (t > 0) {
  758. /* The baud rate is not set to 0, so we're enabeling DTR... -- REW */
  759. sx_setsignals(port, 1, -1);
  760. /* XXX This is not TA & MTA compatible */
  761. sx_write_channel_byte(port, hi_csr, 0xff);
  762. sx_write_channel_byte(port, hi_txbaud, t);
  763. sx_write_channel_byte(port, hi_rxbaud, t);
  764. } else {
  765. sx_setsignals(port, 0, -1);
  766. }
  767. } else {
  768. switch (port->gs.baud) {
  769. #define e(x) case x: t = CSR_ ## x; break
  770. e(75);
  771. e(150);
  772. e(300);
  773. e(600);
  774. e(1200);
  775. e(2400);
  776. e(4800);
  777. e(1800);
  778. e(9600);
  779. e(19200);
  780. e(57600);
  781. e(38400);
  782. /* TA supports 110, but not 115200, MTA supports 115200, but not 110 */
  783. case 110:
  784. if (port->board->ta_type == MOD_TA) {
  785. t = CSR_110;
  786. break;
  787. } else {
  788. t = CSR_9600;
  789. printk(KERN_INFO "sx: Unsupported baud rate: "
  790. "%d.\n", port->gs.baud);
  791. break;
  792. }
  793. case 115200:
  794. if (port->board->ta_type == MOD_TA) {
  795. t = CSR_9600;
  796. printk(KERN_INFO "sx: Unsupported baud rate: "
  797. "%d.\n", port->gs.baud);
  798. break;
  799. } else {
  800. t = CSR_110;
  801. break;
  802. }
  803. case 0:
  804. t = -1;
  805. break;
  806. default:
  807. t = CSR_9600;
  808. printk(KERN_INFO "sx: Unsupported baud rate: %d.\n",
  809. port->gs.baud);
  810. break;
  811. }
  812. #undef e
  813. if (t >= 0) {
  814. sx_setsignals(port, 1, -1);
  815. sx_write_channel_byte(port, hi_csr, t * 0x11);
  816. } else {
  817. sx_setsignals(port, 0, -1);
  818. }
  819. }
  820. }
  821. /* Simon Allen's version of this routine was 225 lines long. 85 is a lot
  822. better. -- REW */
  823. static int sx_set_real_termios(void *ptr)
  824. {
  825. struct sx_port *port = ptr;
  826. func_enter2();
  827. if (!port->gs.tty)
  828. return 0;
  829. /* What is this doing here? -- REW
  830. Ha! figured it out. It is to allow you to get DTR active again
  831. if you've dropped it with stty 0. Moved to set_baud, where it
  832. belongs (next to the drop dtr if baud == 0) -- REW */
  833. /* sx_setsignals (port, 1, -1); */
  834. sx_set_baud(port);
  835. #define CFLAG port->gs.tty->termios->c_cflag
  836. sx_write_channel_byte(port, hi_mr1,
  837. (C_PARENB(port->gs.tty) ? MR1_WITH : MR1_NONE) |
  838. (C_PARODD(port->gs.tty) ? MR1_ODD : MR1_EVEN) |
  839. (C_CRTSCTS(port->gs.tty) ? MR1_RTS_RXFLOW : 0) |
  840. (((CFLAG & CSIZE) == CS8) ? MR1_8_BITS : 0) |
  841. (((CFLAG & CSIZE) == CS7) ? MR1_7_BITS : 0) |
  842. (((CFLAG & CSIZE) == CS6) ? MR1_6_BITS : 0) |
  843. (((CFLAG & CSIZE) == CS5) ? MR1_5_BITS : 0));
  844. sx_write_channel_byte(port, hi_mr2,
  845. (C_CRTSCTS(port->gs.tty) ? MR2_CTS_TXFLOW : 0) |
  846. (C_CSTOPB(port->gs.tty) ? MR2_2_STOP :
  847. MR2_1_STOP));
  848. switch (CFLAG & CSIZE) {
  849. case CS8:
  850. sx_write_channel_byte(port, hi_mask, 0xff);
  851. break;
  852. case CS7:
  853. sx_write_channel_byte(port, hi_mask, 0x7f);
  854. break;
  855. case CS6:
  856. sx_write_channel_byte(port, hi_mask, 0x3f);
  857. break;
  858. case CS5:
  859. sx_write_channel_byte(port, hi_mask, 0x1f);
  860. break;
  861. default:
  862. printk(KERN_INFO "sx: Invalid wordsize: %u\n", CFLAG & CSIZE);
  863. break;
  864. }
  865. sx_write_channel_byte(port, hi_prtcl,
  866. (I_IXON(port->gs.tty) ? SP_TXEN : 0) |
  867. (I_IXOFF(port->gs.tty) ? SP_RXEN : 0) |
  868. (I_IXANY(port->gs.tty) ? SP_TANY : 0) | SP_DCEN);
  869. sx_write_channel_byte(port, hi_break,
  870. (I_IGNBRK(port->gs.tty) ? BR_IGN : 0 |
  871. I_BRKINT(port->gs.tty) ? BR_INT : 0));
  872. sx_write_channel_byte(port, hi_txon, START_CHAR(port->gs.tty));
  873. sx_write_channel_byte(port, hi_rxon, START_CHAR(port->gs.tty));
  874. sx_write_channel_byte(port, hi_txoff, STOP_CHAR(port->gs.tty));
  875. sx_write_channel_byte(port, hi_rxoff, STOP_CHAR(port->gs.tty));
  876. sx_reconfigure_port(port);
  877. /* Tell line discipline whether we will do input cooking */
  878. if (I_OTHER(port->gs.tty)) {
  879. clear_bit(TTY_HW_COOK_IN, &port->gs.tty->flags);
  880. } else {
  881. set_bit(TTY_HW_COOK_IN, &port->gs.tty->flags);
  882. }
  883. sx_dprintk(SX_DEBUG_TERMIOS, "iflags: %x(%d) ",
  884. port->gs.tty->termios->c_iflag, I_OTHER(port->gs.tty));
  885. /* Tell line discipline whether we will do output cooking.
  886. * If OPOST is set and no other output flags are set then we can do output
  887. * processing. Even if only *one* other flag in the O_OTHER group is set
  888. * we do cooking in software.
  889. */
  890. if (O_OPOST(port->gs.tty) && !O_OTHER(port->gs.tty)) {
  891. set_bit(TTY_HW_COOK_OUT, &port->gs.tty->flags);
  892. } else {
  893. clear_bit(TTY_HW_COOK_OUT, &port->gs.tty->flags);
  894. }
  895. sx_dprintk(SX_DEBUG_TERMIOS, "oflags: %x(%d)\n",
  896. port->gs.tty->termios->c_oflag, O_OTHER(port->gs.tty));
  897. /* port->c_dcd = sx_get_CD (port); */
  898. func_exit();
  899. return 0;
  900. }
  901. /* ********************************************************************** *
  902. * the interrupt related routines *
  903. * ********************************************************************** */
  904. /* Note:
  905. Other drivers use the macro "MIN" to calculate how much to copy.
  906. This has the disadvantage that it will evaluate parts twice. That's
  907. expensive when it's IO (and the compiler cannot optimize those away!).
  908. Moreover, I'm not sure that you're race-free.
  909. I assign a value, and then only allow the value to decrease. This
  910. is always safe. This makes the code a few lines longer, and you
  911. know I'm dead against that, but I think it is required in this
  912. case. */
  913. static void sx_transmit_chars(struct sx_port *port)
  914. {
  915. int c;
  916. int tx_ip;
  917. int txroom;
  918. func_enter2();
  919. sx_dprintk(SX_DEBUG_TRANSMIT, "Port %p: transmit %d chars\n",
  920. port, port->gs.xmit_cnt);
  921. if (test_and_set_bit(SX_PORT_TRANSMIT_LOCK, &port->locks)) {
  922. return;
  923. }
  924. while (1) {
  925. c = port->gs.xmit_cnt;
  926. sx_dprintk(SX_DEBUG_TRANSMIT, "Copying %d ", c);
  927. tx_ip = sx_read_channel_byte(port, hi_txipos);
  928. /* Took me 5 minutes to deduce this formula.
  929. Luckily it is literally in the manual in section 6.5.4.3.5 */
  930. txroom = (sx_read_channel_byte(port, hi_txopos) - tx_ip - 1) &
  931. 0xff;
  932. /* Don't copy more bytes than there is room for in the buffer */
  933. if (c > txroom)
  934. c = txroom;
  935. sx_dprintk(SX_DEBUG_TRANSMIT, " %d(%d) ", c, txroom);
  936. /* Don't copy past the end of the hardware transmit buffer */
  937. if (c > 0x100 - tx_ip)
  938. c = 0x100 - tx_ip;
  939. sx_dprintk(SX_DEBUG_TRANSMIT, " %d(%d) ", c, 0x100 - tx_ip);
  940. /* Don't copy pas the end of the source buffer */
  941. if (c > SERIAL_XMIT_SIZE - port->gs.xmit_tail)
  942. c = SERIAL_XMIT_SIZE - port->gs.xmit_tail;
  943. sx_dprintk(SX_DEBUG_TRANSMIT, " %d(%ld) \n",
  944. c, SERIAL_XMIT_SIZE - port->gs.xmit_tail);
  945. /* If for one reason or another, we can't copy more data, we're
  946. done! */
  947. if (c == 0)
  948. break;
  949. memcpy_toio(port->board->base + CHAN_OFFSET(port, hi_txbuf) +
  950. tx_ip, port->gs.xmit_buf + port->gs.xmit_tail, c);
  951. /* Update the pointer in the card */
  952. sx_write_channel_byte(port, hi_txipos, (tx_ip + c) & 0xff);
  953. /* Update the kernel buffer end */
  954. port->gs.xmit_tail = (port->gs.xmit_tail + c) &
  955. (SERIAL_XMIT_SIZE - 1);
  956. /* This one last. (this is essential)
  957. It would allow others to start putting more data into the
  958. buffer! */
  959. port->gs.xmit_cnt -= c;
  960. }
  961. if (port->gs.xmit_cnt == 0) {
  962. sx_disable_tx_interrupts(port);
  963. }
  964. if ((port->gs.xmit_cnt <= port->gs.wakeup_chars) && port->gs.tty) {
  965. tty_wakeup(port->gs.tty);
  966. sx_dprintk(SX_DEBUG_TRANSMIT, "Waking up.... ldisc (%d)....\n",
  967. port->gs.wakeup_chars);
  968. }
  969. clear_bit(SX_PORT_TRANSMIT_LOCK, &port->locks);
  970. func_exit();
  971. }
  972. /* Note the symmetry between receiving chars and transmitting them!
  973. Note: The kernel should have implemented both a receive buffer and
  974. a transmit buffer. */
  975. /* Inlined: Called only once. Remove the inline when you add another call */
  976. static inline void sx_receive_chars(struct sx_port *port)
  977. {
  978. int c;
  979. int rx_op;
  980. struct tty_struct *tty;
  981. int copied = 0;
  982. unsigned char *rp;
  983. func_enter2();
  984. tty = port->gs.tty;
  985. while (1) {
  986. rx_op = sx_read_channel_byte(port, hi_rxopos);
  987. c = (sx_read_channel_byte(port, hi_rxipos) - rx_op) & 0xff;
  988. sx_dprintk(SX_DEBUG_RECEIVE, "rxop=%d, c = %d.\n", rx_op, c);
  989. /* Don't copy past the end of the hardware receive buffer */
  990. if (rx_op + c > 0x100)
  991. c = 0x100 - rx_op;
  992. sx_dprintk(SX_DEBUG_RECEIVE, "c = %d.\n", c);
  993. /* Don't copy more bytes than there is room for in the buffer */
  994. c = tty_prepare_flip_string(tty, &rp, c);
  995. sx_dprintk(SX_DEBUG_RECEIVE, "c = %d.\n", c);
  996. /* If for one reason or another, we can't copy more data, we're done! */
  997. if (c == 0)
  998. break;
  999. sx_dprintk(SX_DEBUG_RECEIVE, "Copying over %d chars. First is "
  1000. "%d at %lx\n", c, read_sx_byte(port->board,
  1001. CHAN_OFFSET(port, hi_rxbuf) + rx_op),
  1002. CHAN_OFFSET(port, hi_rxbuf));
  1003. memcpy_fromio(rp, port->board->base +
  1004. CHAN_OFFSET(port, hi_rxbuf) + rx_op, c);
  1005. /* This one last. ( Not essential.)
  1006. It allows the card to start putting more data into the
  1007. buffer!
  1008. Update the pointer in the card */
  1009. sx_write_channel_byte(port, hi_rxopos, (rx_op + c) & 0xff);
  1010. copied += c;
  1011. }
  1012. if (copied) {
  1013. struct timeval tv;
  1014. do_gettimeofday(&tv);
  1015. sx_dprintk(SX_DEBUG_RECEIVE, "pushing flipq port %d (%3d "
  1016. "chars): %d.%06d (%d/%d)\n", port->line,
  1017. copied, (int)(tv.tv_sec % 60), (int)tv.tv_usec,
  1018. tty->raw, tty->real_raw);
  1019. /* Tell the rest of the system the news. Great news. New
  1020. characters! */
  1021. tty_flip_buffer_push(tty);
  1022. /* tty_schedule_flip (tty); */
  1023. }
  1024. func_exit();
  1025. }
  1026. /* Inlined: it is called only once. Remove the inline if you add another
  1027. call */
  1028. static inline void sx_check_modem_signals(struct sx_port *port)
  1029. {
  1030. int hi_state;
  1031. int c_dcd;
  1032. hi_state = sx_read_channel_byte(port, hi_state);
  1033. sx_dprintk(SX_DEBUG_MODEMSIGNALS, "Checking modem signals (%d/%d)\n",
  1034. port->c_dcd, sx_get_CD(port));
  1035. if (hi_state & ST_BREAK) {
  1036. hi_state &= ~ST_BREAK;
  1037. sx_dprintk(SX_DEBUG_MODEMSIGNALS, "got a break.\n");
  1038. sx_write_channel_byte(port, hi_state, hi_state);
  1039. gs_got_break(&port->gs);
  1040. }
  1041. if (hi_state & ST_DCD) {
  1042. hi_state &= ~ST_DCD;
  1043. sx_dprintk(SX_DEBUG_MODEMSIGNALS, "got a DCD change.\n");
  1044. sx_write_channel_byte(port, hi_state, hi_state);
  1045. c_dcd = sx_get_CD(port);
  1046. sx_dprintk(SX_DEBUG_MODEMSIGNALS, "DCD is now %d\n", c_dcd);
  1047. if (c_dcd != port->c_dcd) {
  1048. port->c_dcd = c_dcd;
  1049. if (sx_get_CD(port)) {
  1050. /* DCD went UP */
  1051. if ((sx_read_channel_byte(port, hi_hstat) !=
  1052. HS_IDLE_CLOSED) &&
  1053. !(port->gs.tty->termios->
  1054. c_cflag & CLOCAL)) {
  1055. /* Are we blocking in open? */
  1056. sx_dprintk(SX_DEBUG_MODEMSIGNALS, "DCD "
  1057. "active, unblocking open\n");
  1058. wake_up_interruptible(&port->gs.
  1059. open_wait);
  1060. } else {
  1061. sx_dprintk(SX_DEBUG_MODEMSIGNALS, "DCD "
  1062. "raised. Ignoring.\n");
  1063. }
  1064. } else {
  1065. /* DCD went down! */
  1066. if (!(port->gs.tty->termios->c_cflag & CLOCAL)){
  1067. sx_dprintk(SX_DEBUG_MODEMSIGNALS, "DCD "
  1068. "dropped. hanging up....\n");
  1069. tty_hangup(port->gs.tty);
  1070. } else {
  1071. sx_dprintk(SX_DEBUG_MODEMSIGNALS, "DCD "
  1072. "dropped. ignoring.\n");
  1073. }
  1074. }
  1075. } else {
  1076. sx_dprintk(SX_DEBUG_MODEMSIGNALS, "Hmmm. card told us "
  1077. "DCD changed, but it didn't.\n");
  1078. }
  1079. }
  1080. }
  1081. /* This is what an interrupt routine should look like.
  1082. * Small, elegant, clear.
  1083. */
  1084. static irqreturn_t sx_interrupt(int irq, void *ptr)
  1085. {
  1086. struct sx_board *board = ptr;
  1087. struct sx_port *port;
  1088. int i;
  1089. func_enter();
  1090. sx_dprintk(SX_DEBUG_FLOW, "sx: enter sx_interrupt (%d/%d)\n", irq,
  1091. board->irq);
  1092. /* AAargh! The order in which to do these things is essential and
  1093. not trivial.
  1094. - Rate limit goes before "recursive". Otherwise a series of
  1095. recursive calls will hang the machine in the interrupt routine.
  1096. - hardware twiddling goes before "recursive". Otherwise when we
  1097. poll the card, and a recursive interrupt happens, we won't
  1098. ack the card, so it might keep on interrupting us. (especially
  1099. level sensitive interrupt systems like PCI).
  1100. - Rate limit goes before hardware twiddling. Otherwise we won't
  1101. catch a card that has gone bonkers.
  1102. - The "initialized" test goes after the hardware twiddling. Otherwise
  1103. the card will stick us in the interrupt routine again.
  1104. - The initialized test goes before recursive.
  1105. */
  1106. #ifdef IRQ_RATE_LIMIT
  1107. /* Aaargh! I'm ashamed. This costs more lines-of-code than the
  1108. actual interrupt routine!. (Well, used to when I wrote that
  1109. comment) */
  1110. {
  1111. static int lastjif;
  1112. static int nintr = 0;
  1113. if (lastjif == jiffies) {
  1114. if (++nintr > IRQ_RATE_LIMIT) {
  1115. free_irq(board->irq, board);
  1116. printk(KERN_ERR "sx: Too many interrupts. "
  1117. "Turning off interrupt %d.\n",
  1118. board->irq);
  1119. }
  1120. } else {
  1121. lastjif = jiffies;
  1122. nintr = 0;
  1123. }
  1124. }
  1125. #endif
  1126. if (board->irq == irq) {
  1127. /* Tell the card we've noticed the interrupt. */
  1128. sx_write_board_word(board, cc_int_pending, 0);
  1129. if (IS_SX_BOARD(board)) {
  1130. write_sx_byte(board, SX_RESET_IRQ, 1);
  1131. } else if (IS_EISA_BOARD(board)) {
  1132. inb(board->eisa_base + 0xc03);
  1133. write_sx_word(board, 8, 0);
  1134. } else {
  1135. write_sx_byte(board, SI2_ISA_INTCLEAR,
  1136. SI2_ISA_INTCLEAR_CLEAR);
  1137. write_sx_byte(board, SI2_ISA_INTCLEAR,
  1138. SI2_ISA_INTCLEAR_SET);
  1139. }
  1140. }
  1141. if (!sx_initialized)
  1142. return IRQ_HANDLED;
  1143. if (!(board->flags & SX_BOARD_INITIALIZED))
  1144. return IRQ_HANDLED;
  1145. if (test_and_set_bit(SX_BOARD_INTR_LOCK, &board->locks)) {
  1146. printk(KERN_ERR "Recursive interrupt! (%d)\n", board->irq);
  1147. return IRQ_HANDLED;
  1148. }
  1149. for (i = 0; i < board->nports; i++) {
  1150. port = &board->ports[i];
  1151. if (port->gs.flags & GS_ACTIVE) {
  1152. if (sx_read_channel_byte(port, hi_state)) {
  1153. sx_dprintk(SX_DEBUG_INTERRUPTS, "Port %d: "
  1154. "modem signal change?... \n",i);
  1155. sx_check_modem_signals(port);
  1156. }
  1157. if (port->gs.xmit_cnt) {
  1158. sx_transmit_chars(port);
  1159. }
  1160. if (!(port->gs.flags & SX_RX_THROTTLE)) {
  1161. sx_receive_chars(port);
  1162. }
  1163. }
  1164. }
  1165. clear_bit(SX_BOARD_INTR_LOCK, &board->locks);
  1166. sx_dprintk(SX_DEBUG_FLOW, "sx: exit sx_interrupt (%d/%d)\n", irq,
  1167. board->irq);
  1168. func_exit();
  1169. return IRQ_HANDLED;
  1170. }
  1171. static void sx_pollfunc(unsigned long data)
  1172. {
  1173. struct sx_board *board = (struct sx_board *)data;
  1174. func_enter();
  1175. sx_interrupt(0, board);
  1176. mod_timer(&board->timer, jiffies + sx_poll);
  1177. func_exit();
  1178. }
  1179. /* ********************************************************************** *
  1180. * Here are the routines that actually *
  1181. * interface with the generic_serial driver *
  1182. * ********************************************************************** */
  1183. /* Ehhm. I don't know how to fiddle with interrupts on the SX card. --REW */
  1184. /* Hmm. Ok I figured it out. You don't. */
  1185. static void sx_disable_tx_interrupts(void *ptr)
  1186. {
  1187. struct sx_port *port = ptr;
  1188. func_enter2();
  1189. port->gs.flags &= ~GS_TX_INTEN;
  1190. func_exit();
  1191. }
  1192. static void sx_enable_tx_interrupts(void *ptr)
  1193. {
  1194. struct sx_port *port = ptr;
  1195. int data_in_buffer;
  1196. func_enter2();
  1197. /* First transmit the characters that we're supposed to */
  1198. sx_transmit_chars(port);
  1199. /* The sx card will never interrupt us if we don't fill the buffer
  1200. past 25%. So we keep considering interrupts off if that's the case. */
  1201. data_in_buffer = (sx_read_channel_byte(port, hi_txipos) -
  1202. sx_read_channel_byte(port, hi_txopos)) & 0xff;
  1203. /* XXX Must be "HIGH_WATER" for SI card according to doc. */
  1204. if (data_in_buffer < LOW_WATER)
  1205. port->gs.flags &= ~GS_TX_INTEN;
  1206. func_exit();
  1207. }
  1208. static void sx_disable_rx_interrupts(void *ptr)
  1209. {
  1210. /* struct sx_port *port = ptr; */
  1211. func_enter();
  1212. func_exit();
  1213. }
  1214. static void sx_enable_rx_interrupts(void *ptr)
  1215. {
  1216. /* struct sx_port *port = ptr; */
  1217. func_enter();
  1218. func_exit();
  1219. }
  1220. /* Jeez. Isn't this simple? */
  1221. static int sx_get_CD(void *ptr)
  1222. {
  1223. struct sx_port *port = ptr;
  1224. func_enter2();
  1225. func_exit();
  1226. return ((sx_read_channel_byte(port, hi_ip) & IP_DCD) != 0);
  1227. }
  1228. /* Jeez. Isn't this simple? */
  1229. static int sx_chars_in_buffer(void *ptr)
  1230. {
  1231. struct sx_port *port = ptr;
  1232. func_enter2();
  1233. func_exit();
  1234. return ((sx_read_channel_byte(port, hi_txipos) -
  1235. sx_read_channel_byte(port, hi_txopos)) & 0xff);
  1236. }
  1237. static void sx_shutdown_port(void *ptr)
  1238. {
  1239. struct sx_port *port = ptr;
  1240. func_enter();
  1241. port->gs.flags &= ~GS_ACTIVE;
  1242. if (port->gs.tty && (port->gs.tty->termios->c_cflag & HUPCL)) {
  1243. sx_setsignals(port, 0, 0);
  1244. sx_reconfigure_port(port);
  1245. }
  1246. func_exit();
  1247. }
  1248. /* ********************************************************************** *
  1249. * Here are the routines that actually *
  1250. * interface with the rest of the system *
  1251. * ********************************************************************** */
  1252. static int sx_open(struct tty_struct *tty, struct file *filp)
  1253. {
  1254. struct sx_port *port;
  1255. int retval, line;
  1256. unsigned long flags;
  1257. func_enter();
  1258. if (!sx_initialized) {
  1259. return -EIO;
  1260. }
  1261. line = tty->index;
  1262. sx_dprintk(SX_DEBUG_OPEN, "%d: opening line %d. tty=%p ctty=%p, "
  1263. "np=%d)\n", current->pid, line, tty,
  1264. current->signal->tty, sx_nports);
  1265. if ((line < 0) || (line >= SX_NPORTS) || (line >= sx_nports))
  1266. return -ENODEV;
  1267. port = &sx_ports[line];
  1268. port->c_dcd = 0; /* Make sure that the first interrupt doesn't detect a
  1269. 1 -> 0 transition. */
  1270. sx_dprintk(SX_DEBUG_OPEN, "port = %p c_dcd = %d\n", port, port->c_dcd);
  1271. spin_lock_irqsave(&port->gs.driver_lock, flags);
  1272. tty->driver_data = port;
  1273. port->gs.tty = tty;
  1274. port->gs.count++;
  1275. spin_unlock_irqrestore(&port->gs.driver_lock, flags);
  1276. sx_dprintk(SX_DEBUG_OPEN, "starting port\n");
  1277. /*
  1278. * Start up serial port
  1279. */
  1280. retval = gs_init_port(&port->gs);
  1281. sx_dprintk(SX_DEBUG_OPEN, "done gs_init\n");
  1282. if (retval) {
  1283. port->gs.count--;
  1284. return retval;
  1285. }
  1286. port->gs.flags |= GS_ACTIVE;
  1287. if (port->gs.count <= 1)
  1288. sx_setsignals(port, 1, 1);
  1289. #if 0
  1290. if (sx_debug & SX_DEBUG_OPEN)
  1291. my_hd(port, sizeof(*port));
  1292. #else
  1293. if (sx_debug & SX_DEBUG_OPEN)
  1294. my_hd_io(port->board->base + port->ch_base, sizeof(*port));
  1295. #endif
  1296. if (port->gs.count <= 1) {
  1297. if (sx_send_command(port, HS_LOPEN, -1, HS_IDLE_OPEN) != 1) {
  1298. printk(KERN_ERR "sx: Card didn't respond to LOPEN "
  1299. "command.\n");
  1300. spin_lock_irqsave(&port->gs.driver_lock, flags);
  1301. port->gs.count--;
  1302. spin_unlock_irqrestore(&port->gs.driver_lock, flags);
  1303. return -EIO;
  1304. }
  1305. }
  1306. retval = gs_block_til_ready(port, filp);
  1307. sx_dprintk(SX_DEBUG_OPEN, "Block til ready returned %d. Count=%d\n",
  1308. retval, port->gs.count);
  1309. if (retval) {
  1310. /*
  1311. * Don't lower gs.count here because sx_close() will be called later
  1312. */
  1313. return retval;
  1314. }
  1315. /* tty->low_latency = 1; */
  1316. port->c_dcd = sx_get_CD(port);
  1317. sx_dprintk(SX_DEBUG_OPEN, "at open: cd=%d\n", port->c_dcd);
  1318. func_exit();
  1319. return 0;
  1320. }
  1321. static void sx_close(void *ptr)
  1322. {
  1323. struct sx_port *port = ptr;
  1324. /* Give the port 5 seconds to close down. */
  1325. int to = 5 * HZ;
  1326. func_enter();
  1327. sx_setsignals(port, 0, 0);
  1328. sx_reconfigure_port(port);
  1329. sx_send_command(port, HS_CLOSE, 0, 0);
  1330. while (to-- && (sx_read_channel_byte(port, hi_hstat) != HS_IDLE_CLOSED))
  1331. if (msleep_interruptible(10))
  1332. break;
  1333. if (sx_read_channel_byte(port, hi_hstat) != HS_IDLE_CLOSED) {
  1334. if (sx_send_command(port, HS_FORCE_CLOSED, -1, HS_IDLE_CLOSED)
  1335. != 1) {
  1336. printk(KERN_ERR "sx: sent the force_close command, but "
  1337. "card didn't react\n");
  1338. } else
  1339. sx_dprintk(SX_DEBUG_CLOSE, "sent the force_close "
  1340. "command.\n");
  1341. }
  1342. sx_dprintk(SX_DEBUG_CLOSE, "waited %d jiffies for close. count=%d\n",
  1343. 5 * HZ - to - 1, port->gs.count);
  1344. if (port->gs.count) {
  1345. sx_dprintk(SX_DEBUG_CLOSE, "WARNING port count:%d\n",
  1346. port->gs.count);
  1347. /*printk("%s SETTING port count to zero: %p count: %d\n",
  1348. __FUNCTION__, port, port->gs.count);
  1349. port->gs.count = 0;*/
  1350. }
  1351. func_exit();
  1352. }
  1353. /* This is relatively thorough. But then again it is only 20 lines. */
  1354. #define MARCHUP for (i = min; i < max; i++)
  1355. #define MARCHDOWN for (i = max - 1; i >= min; i--)
  1356. #define W0 write_sx_byte(board, i, 0x55)
  1357. #define W1 write_sx_byte(board, i, 0xaa)
  1358. #define R0 if (read_sx_byte(board, i) != 0x55) return 1
  1359. #define R1 if (read_sx_byte(board, i) != 0xaa) return 1
  1360. /* This memtest takes a human-noticable time. You normally only do it
  1361. once a boot, so I guess that it is worth it. */
  1362. static int do_memtest(struct sx_board *board, int min, int max)
  1363. {
  1364. int i;
  1365. /* This is a marchb. Theoretically, marchb catches much more than
  1366. simpler tests. In practise, the longer test just catches more
  1367. intermittent errors. -- REW
  1368. (For the theory behind memory testing see:
  1369. Testing Semiconductor Memories by A.J. van de Goor.) */
  1370. MARCHUP {
  1371. W0;
  1372. }
  1373. MARCHUP {
  1374. R0;
  1375. W1;
  1376. R1;
  1377. W0;
  1378. R0;
  1379. W1;
  1380. }
  1381. MARCHUP {
  1382. R1;
  1383. W0;
  1384. W1;
  1385. }
  1386. MARCHDOWN {
  1387. R1;
  1388. W0;
  1389. W1;
  1390. W0;
  1391. }
  1392. MARCHDOWN {
  1393. R0;
  1394. W1;
  1395. W0;
  1396. }
  1397. return 0;
  1398. }
  1399. #undef MARCHUP
  1400. #undef MARCHDOWN
  1401. #undef W0
  1402. #undef W1
  1403. #undef R0
  1404. #undef R1
  1405. #define MARCHUP for (i = min; i < max; i += 2)
  1406. #define MARCHDOWN for (i = max - 1; i >= min; i -= 2)
  1407. #define W0 write_sx_word(board, i, 0x55aa)
  1408. #define W1 write_sx_word(board, i, 0xaa55)
  1409. #define R0 if (read_sx_word(board, i) != 0x55aa) return 1
  1410. #define R1 if (read_sx_word(board, i) != 0xaa55) return 1
  1411. #if 0
  1412. /* This memtest takes a human-noticable time. You normally only do it
  1413. once a boot, so I guess that it is worth it. */
  1414. static int do_memtest_w(struct sx_board *board, int min, int max)
  1415. {
  1416. int i;
  1417. MARCHUP {
  1418. W0;
  1419. }
  1420. MARCHUP {
  1421. R0;
  1422. W1;
  1423. R1;
  1424. W0;
  1425. R0;
  1426. W1;
  1427. }
  1428. MARCHUP {
  1429. R1;
  1430. W0;
  1431. W1;
  1432. }
  1433. MARCHDOWN {
  1434. R1;
  1435. W0;
  1436. W1;
  1437. W0;
  1438. }
  1439. MARCHDOWN {
  1440. R0;
  1441. W1;
  1442. W0;
  1443. }
  1444. return 0;
  1445. }
  1446. #endif
  1447. static int sx_fw_ioctl(struct inode *inode, struct file *filp,
  1448. unsigned int cmd, unsigned long arg)
  1449. {
  1450. int rc = 0;
  1451. int __user *descr = (int __user *)arg;
  1452. int i;
  1453. static struct sx_board *board = NULL;
  1454. int nbytes, offset;
  1455. unsigned long data;
  1456. char *tmp;
  1457. func_enter();
  1458. #if 0
  1459. /* Removed superuser check: Sysops can use the permissions on the device
  1460. file to restrict access. Recommendation: Root only. (root.root 600) */
  1461. if (!capable(CAP_SYS_ADMIN)) {
  1462. return -EPERM;
  1463. }
  1464. #endif
  1465. sx_dprintk(SX_DEBUG_FIRMWARE, "IOCTL %x: %lx\n", cmd, arg);
  1466. if (!board)
  1467. board = &boards[0];
  1468. if (board->flags & SX_BOARD_PRESENT) {
  1469. sx_dprintk(SX_DEBUG_FIRMWARE, "Board present! (%x)\n",
  1470. board->flags);
  1471. } else {
  1472. sx_dprintk(SX_DEBUG_FIRMWARE, "Board not present! (%x) all:",
  1473. board->flags);
  1474. for (i = 0; i < SX_NBOARDS; i++)
  1475. sx_dprintk(SX_DEBUG_FIRMWARE, "<%x> ", boards[i].flags);
  1476. sx_dprintk(SX_DEBUG_FIRMWARE, "\n");
  1477. return -EIO;
  1478. }
  1479. switch (cmd) {
  1480. case SXIO_SET_BOARD:
  1481. sx_dprintk(SX_DEBUG_FIRMWARE, "set board to %ld\n", arg);
  1482. if (arg >= SX_NBOARDS)
  1483. return -EIO;
  1484. sx_dprintk(SX_DEBUG_FIRMWARE, "not out of range\n");
  1485. if (!(boards[arg].flags & SX_BOARD_PRESENT))
  1486. return -EIO;
  1487. sx_dprintk(SX_DEBUG_FIRMWARE, ".. and present!\n");
  1488. board = &boards[arg];
  1489. break;
  1490. case SXIO_GET_TYPE:
  1491. rc = -ENOENT; /* If we manage to miss one, return error. */
  1492. if (IS_SX_BOARD(board))
  1493. rc = SX_TYPE_SX;
  1494. if (IS_CF_BOARD(board))
  1495. rc = SX_TYPE_CF;
  1496. if (IS_SI_BOARD(board))
  1497. rc = SX_TYPE_SI;
  1498. if (IS_SI1_BOARD(board))
  1499. rc = SX_TYPE_SI;
  1500. if (IS_EISA_BOARD(board))
  1501. rc = SX_TYPE_SI;
  1502. sx_dprintk(SX_DEBUG_FIRMWARE, "returning type= %d\n", rc);
  1503. break;
  1504. case SXIO_DO_RAMTEST:
  1505. if (sx_initialized) /* Already initialized: better not ramtest the board. */
  1506. return -EPERM;
  1507. if (IS_SX_BOARD(board)) {
  1508. rc = do_memtest(board, 0, 0x7000);
  1509. if (!rc)
  1510. rc = do_memtest(board, 0, 0x7000);
  1511. /*if (!rc) rc = do_memtest_w (board, 0, 0x7000); */
  1512. } else {
  1513. rc = do_memtest(board, 0, 0x7ff8);
  1514. /* if (!rc) rc = do_memtest_w (board, 0, 0x7ff8); */
  1515. }
  1516. sx_dprintk(SX_DEBUG_FIRMWARE, "returning memtest result= %d\n",
  1517. rc);
  1518. break;
  1519. case SXIO_DOWNLOAD:
  1520. if (sx_initialized) /* Already initialized */
  1521. return -EEXIST;
  1522. if (!sx_reset(board))
  1523. return -EIO;
  1524. sx_dprintk(SX_DEBUG_INIT, "reset the board...\n");
  1525. tmp = kmalloc(SX_CHUNK_SIZE, GFP_USER);
  1526. if (!tmp)
  1527. return -ENOMEM;
  1528. get_user(nbytes, descr++);
  1529. get_user(offset, descr++);
  1530. get_user(data, descr++);
  1531. while (nbytes && data) {
  1532. for (i = 0; i < nbytes; i += SX_CHUNK_SIZE) {
  1533. if (copy_from_user(tmp, (char __user *)data + i,
  1534. (i + SX_CHUNK_SIZE > nbytes) ?
  1535. nbytes - i : SX_CHUNK_SIZE)) {
  1536. kfree(tmp);
  1537. return -EFAULT;
  1538. }
  1539. memcpy_toio(board->base2 + offset + i, tmp,
  1540. (i + SX_CHUNK_SIZE > nbytes) ?
  1541. nbytes - i : SX_CHUNK_SIZE);
  1542. }
  1543. get_user(nbytes, descr++);
  1544. get_user(offset, descr++);
  1545. get_user(data, descr++);
  1546. }
  1547. kfree(tmp);
  1548. sx_nports += sx_init_board(board);
  1549. rc = sx_nports;
  1550. break;
  1551. case SXIO_INIT:
  1552. if (sx_initialized) /* Already initialized */
  1553. return -EEXIST;
  1554. /* This is not allowed until all boards are initialized... */
  1555. for (i = 0; i < SX_NBOARDS; i++) {
  1556. if ((boards[i].flags & SX_BOARD_PRESENT) &&
  1557. !(boards[i].flags & SX_BOARD_INITIALIZED))
  1558. return -EIO;
  1559. }
  1560. for (i = 0; i < SX_NBOARDS; i++)
  1561. if (!(boards[i].flags & SX_BOARD_PRESENT))
  1562. break;
  1563. sx_dprintk(SX_DEBUG_FIRMWARE, "initing portstructs, %d boards, "
  1564. "%d channels, first board: %d ports\n",
  1565. i, sx_nports, boards[0].nports);
  1566. rc = sx_init_portstructs(i, sx_nports);
  1567. sx_init_drivers();
  1568. if (rc >= 0)
  1569. sx_initialized++;
  1570. break;
  1571. case SXIO_SETDEBUG:
  1572. sx_debug = arg;
  1573. break;
  1574. case SXIO_GETDEBUG:
  1575. rc = sx_debug;
  1576. break;
  1577. case SXIO_GETGSDEBUG:
  1578. case SXIO_SETGSDEBUG:
  1579. rc = -EINVAL;
  1580. break;
  1581. case SXIO_GETNPORTS:
  1582. rc = sx_nports;
  1583. break;
  1584. default:
  1585. printk(KERN_WARNING "Unknown ioctl on firmware device (%x).\n",
  1586. cmd);
  1587. break;
  1588. }
  1589. func_exit();
  1590. return rc;
  1591. }
  1592. static void sx_break(struct tty_struct *tty, int flag)
  1593. {
  1594. struct sx_port *port = tty->driver_data;
  1595. int rv;
  1596. func_enter();
  1597. if (flag)
  1598. rv = sx_send_command(port, HS_START, -1, HS_IDLE_BREAK);
  1599. else
  1600. rv = sx_send_command(port, HS_STOP, -1, HS_IDLE_OPEN);
  1601. if (rv != 1)
  1602. printk(KERN_ERR "sx: couldn't send break (%x).\n",
  1603. read_sx_byte(port->board, CHAN_OFFSET(port, hi_hstat)));
  1604. func_exit();
  1605. }
  1606. static int sx_tiocmget(struct tty_struct *tty, struct file *file)
  1607. {
  1608. struct sx_port *port = tty->driver_data;
  1609. return sx_getsignals(port);
  1610. }
  1611. static int sx_tiocmset(struct tty_struct *tty, struct file *file,
  1612. unsigned int set, unsigned int clear)
  1613. {
  1614. struct sx_port *port = tty->driver_data;
  1615. int rts = -1, dtr = -1;
  1616. if (set & TIOCM_RTS)
  1617. rts = 1;
  1618. if (set & TIOCM_DTR)
  1619. dtr = 1;
  1620. if (clear & TIOCM_RTS)
  1621. rts = 0;
  1622. if (clear & TIOCM_DTR)
  1623. dtr = 0;
  1624. sx_setsignals(port, dtr, rts);
  1625. sx_reconfigure_port(port);
  1626. return 0;
  1627. }
  1628. static int sx_ioctl(struct tty_struct *tty, struct file *filp,
  1629. unsigned int cmd, unsigned long arg)
  1630. {
  1631. int rc;
  1632. struct sx_port *port = tty->driver_data;
  1633. void __user *argp = (void __user *)arg;
  1634. int ival;
  1635. /* func_enter2(); */
  1636. rc = 0;
  1637. switch (cmd) {
  1638. case TIOCGSOFTCAR:
  1639. rc = put_user(((tty->termios->c_cflag & CLOCAL) ? 1 : 0),
  1640. (unsigned __user *)argp);
  1641. break;
  1642. case TIOCSSOFTCAR:
  1643. if ((rc = get_user(ival, (unsigned __user *)argp)) == 0) {
  1644. tty->termios->c_cflag =
  1645. (tty->termios->c_cflag & ~CLOCAL) |
  1646. (ival ? CLOCAL : 0);
  1647. }
  1648. break;
  1649. case TIOCGSERIAL:
  1650. rc = gs_getserial(&port->gs, argp);
  1651. break;
  1652. case TIOCSSERIAL:
  1653. rc = gs_setserial(&port->gs, argp);
  1654. break;
  1655. default:
  1656. rc = -ENOIOCTLCMD;
  1657. break;
  1658. }
  1659. /* func_exit(); */
  1660. return rc;
  1661. }
  1662. /* The throttle/unthrottle scheme for the Specialix card is different
  1663. * from other drivers and deserves some explanation.
  1664. * The Specialix hardware takes care of XON/XOFF
  1665. * and CTS/RTS flow control itself. This means that all we have to
  1666. * do when signalled by the upper tty layer to throttle/unthrottle is
  1667. * to make a note of it here. When we come to read characters from the
  1668. * rx buffers on the card (sx_receive_chars()) we look to see if the
  1669. * upper layer can accept more (as noted here in sx_rx_throt[]).
  1670. * If it can't we simply don't remove chars from the cards buffer.
  1671. * When the tty layer can accept chars, we again note that here and when
  1672. * sx_receive_chars() is called it will remove them from the cards buffer.
  1673. * The card will notice that a ports buffer has drained below some low
  1674. * water mark and will unflow control the line itself, using whatever
  1675. * flow control scheme is in use for that port. -- Simon Allen
  1676. */
  1677. static void sx_throttle(struct tty_struct *tty)
  1678. {
  1679. struct sx_port *port = (struct sx_port *)tty->driver_data;
  1680. func_enter2();
  1681. /* If the port is using any type of input flow
  1682. * control then throttle the port.
  1683. */
  1684. if ((tty->termios->c_cflag & CRTSCTS) || (I_IXOFF(tty))) {
  1685. port->gs.flags |= SX_RX_THROTTLE;
  1686. }
  1687. func_exit();
  1688. }
  1689. static void sx_unthrottle(struct tty_struct *tty)
  1690. {
  1691. struct sx_port *port = (struct sx_port *)tty->driver_data;
  1692. func_enter2();
  1693. /* Always unthrottle even if flow control is not enabled on
  1694. * this port in case we disabled flow control while the port
  1695. * was throttled
  1696. */
  1697. port->gs.flags &= ~SX_RX_THROTTLE;
  1698. func_exit();
  1699. return;
  1700. }
  1701. /* ********************************************************************** *
  1702. * Here are the initialization routines. *
  1703. * ********************************************************************** */
  1704. static int sx_init_board(struct sx_board *board)
  1705. {
  1706. int addr;
  1707. int chans;
  1708. int type;
  1709. func_enter();
  1710. /* This is preceded by downloading the download code. */
  1711. board->flags |= SX_BOARD_INITIALIZED;
  1712. if (read_sx_byte(board, 0))
  1713. /* CF boards may need this. */
  1714. write_sx_byte(board, 0, 0);
  1715. /* This resets the processor again, to make sure it didn't do any
  1716. foolish things while we were downloading the image */
  1717. if (!sx_reset(board))
  1718. return 0;
  1719. sx_start_board(board);
  1720. udelay(10);
  1721. if (!sx_busy_wait_neq(board, 0, 0xff, 0)) {
  1722. printk(KERN_ERR "sx: Ooops. Board won't initialize.\n");
  1723. return 0;
  1724. }
  1725. /* Ok. So now the processor on the card is running. It gathered
  1726. some info for us... */
  1727. sx_dprintk(SX_DEBUG_INIT, "The sxcard structure:\n");
  1728. if (sx_debug & SX_DEBUG_INIT)
  1729. my_hd_io(board->base, 0x10);
  1730. sx_dprintk(SX_DEBUG_INIT, "the first sx_module structure:\n");
  1731. if (sx_debug & SX_DEBUG_INIT)
  1732. my_hd_io(board->base + 0x80, 0x30);
  1733. sx_dprintk(SX_DEBUG_INIT, "init_status: %x, %dk memory, firmware "
  1734. "V%x.%02x,\n",
  1735. read_sx_byte(board, 0), read_sx_byte(board, 1),
  1736. read_sx_byte(board, 5), read_sx_byte(board, 4));
  1737. if (read_sx_byte(board, 0) == 0xff) {
  1738. printk(KERN_INFO "sx: No modules found. Sorry.\n");
  1739. board->nports = 0;
  1740. return 0;
  1741. }
  1742. chans = 0;
  1743. if (IS_SX_BOARD(board)) {
  1744. sx_write_board_word(board, cc_int_count, sx_maxints);
  1745. } else {
  1746. if (sx_maxints)
  1747. sx_write_board_word(board, cc_int_count,
  1748. SI_PROCESSOR_CLOCK / 8 / sx_maxints);
  1749. }
  1750. /* grab the first module type... */
  1751. /* board->ta_type = mod_compat_type (read_sx_byte (board, 0x80 + 0x08)); */
  1752. board->ta_type = mod_compat_type(sx_read_module_byte(board, 0x80,
  1753. mc_chip));
  1754. /* XXX byteorder */
  1755. for (addr = 0x80; addr != 0; addr = read_sx_word(board, addr) & 0x7fff){
  1756. type = sx_read_module_byte(board, addr, mc_chip);
  1757. sx_dprintk(SX_DEBUG_INIT, "Module at %x: %d channels\n",
  1758. addr, read_sx_byte(board, addr + 2));
  1759. chans += sx_read_module_byte(board, addr, mc_type);
  1760. sx_dprintk(SX_DEBUG_INIT, "module is an %s, which has %s/%s "
  1761. "panels\n",
  1762. mod_type_s(type),
  1763. pan_type_s(sx_read_module_byte(board, addr,
  1764. mc_mods) & 0xf),
  1765. pan_type_s(sx_read_module_byte(board, addr,
  1766. mc_mods) >> 4));
  1767. sx_dprintk(SX_DEBUG_INIT, "CD1400 versions: %x/%x, ASIC "
  1768. "version: %x\n",
  1769. sx_read_module_byte(board, addr, mc_rev1),
  1770. sx_read_module_byte(board, addr, mc_rev2),
  1771. sx_read_module_byte(board, addr, mc_mtaasic_rev));
  1772. /* The following combinations are illegal: It should theoretically
  1773. work, but timing problems make the bus HANG. */
  1774. if (mod_compat_type(type) != board->ta_type) {
  1775. printk(KERN_ERR "sx: This is an invalid "
  1776. "configuration.\nDon't mix TA/MTA/SXDC on the "
  1777. "same hostadapter.\n");
  1778. chans = 0;
  1779. break;
  1780. }
  1781. if ((IS_EISA_BOARD(board) ||
  1782. IS_SI_BOARD(board)) &&
  1783. (mod_compat_type(type) == 4)) {
  1784. printk(KERN_ERR "sx: This is an invalid "
  1785. "configuration.\nDon't use SXDCs on an SI/XIO "
  1786. "adapter.\n");
  1787. chans = 0;
  1788. break;
  1789. }
  1790. #if 0 /* Problem fixed: firmware 3.05 */
  1791. if (IS_SX_BOARD(board) && (type == TA8)) {
  1792. /* There are some issues with the firmware and the DCD/RTS
  1793. lines. It might work if you tie them together or something.
  1794. It might also work if you get a newer sx_firmware. Therefore
  1795. this is just a warning. */
  1796. printk(KERN_WARNING
  1797. "sx: The SX host doesn't work too well "
  1798. "with the TA8 adapters.\nSpecialix is working on it.\n");
  1799. }
  1800. #endif
  1801. }
  1802. if (chans) {
  1803. if (board->irq > 0) {
  1804. /* fixed irq, probably PCI */
  1805. if (sx_irqmask & (1 << board->irq)) { /* may we use this irq? */
  1806. if (request_irq(board->irq, sx_interrupt,
  1807. IRQF_SHARED | IRQF_DISABLED,
  1808. "sx", board)) {
  1809. printk(KERN_ERR "sx: Cannot allocate "
  1810. "irq %d.\n", board->irq);
  1811. board->irq = 0;
  1812. }
  1813. } else
  1814. board->irq = 0;
  1815. } else if (board->irq < 0 && sx_irqmask) {
  1816. /* auto-allocate irq */
  1817. int irqnr;
  1818. int irqmask = sx_irqmask & (IS_SX_BOARD(board) ?
  1819. SX_ISA_IRQ_MASK : SI2_ISA_IRQ_MASK);
  1820. for (irqnr = 15; irqnr > 0; irqnr--)
  1821. if (irqmask & (1 << irqnr))
  1822. if (!request_irq(irqnr, sx_interrupt,
  1823. IRQF_SHARED | IRQF_DISABLED,
  1824. "sx", board))
  1825. break;
  1826. if (!irqnr)
  1827. printk(KERN_ERR "sx: Cannot allocate IRQ.\n");
  1828. board->irq = irqnr;
  1829. } else
  1830. board->irq = 0;
  1831. if (board->irq) {
  1832. /* Found a valid interrupt, start up interrupts! */
  1833. sx_dprintk(SX_DEBUG_INIT, "Using irq %d.\n",
  1834. board->irq);
  1835. sx_start_interrupts(board);
  1836. board->poll = sx_slowpoll;
  1837. board->flags |= SX_IRQ_ALLOCATED;
  1838. } else {
  1839. /* no irq: setup board for polled operation */
  1840. board->poll = sx_poll;
  1841. sx_dprintk(SX_DEBUG_INIT, "Using poll-interval %d.\n",
  1842. board->poll);
  1843. }
  1844. /* The timer should be initialized anyway: That way we can
  1845. safely del_timer it when the module is unloaded. */
  1846. setup_timer(&board->timer, sx_pollfunc, (unsigned long)board);
  1847. if (board->poll)
  1848. mod_timer(&board->timer, jiffies + board->poll);
  1849. } else {
  1850. board->irq = 0;
  1851. }
  1852. board->nports = chans;
  1853. sx_dprintk(SX_DEBUG_INIT, "returning %d ports.", board->nports);
  1854. func_exit();
  1855. return chans;
  1856. }
  1857. static void __devinit printheader(void)
  1858. {
  1859. static int header_printed;
  1860. if (!header_printed) {
  1861. printk(KERN_INFO "Specialix SX driver "
  1862. "(C) 1998/1999 R.E.Wolff@BitWizard.nl\n");
  1863. printk(KERN_INFO "sx: version " __stringify(SX_VERSION) "\n");
  1864. header_printed = 1;
  1865. }
  1866. }
  1867. static int __devinit probe_sx(struct sx_board *board)
  1868. {
  1869. struct vpd_prom vpdp;
  1870. char *p;
  1871. int i;
  1872. func_enter();
  1873. if (!IS_CF_BOARD(board)) {
  1874. sx_dprintk(SX_DEBUG_PROBE, "Going to verify vpd prom at %p.\n",
  1875. board->base + SX_VPD_ROM);
  1876. if (sx_debug & SX_DEBUG_PROBE)
  1877. my_hd_io(board->base + SX_VPD_ROM, 0x40);
  1878. p = (char *)&vpdp;
  1879. for (i = 0; i < sizeof(struct vpd_prom); i++)
  1880. *p++ = read_sx_byte(board, SX_VPD_ROM + i * 2);
  1881. if (sx_debug & SX_DEBUG_PROBE)
  1882. my_hd(&vpdp, 0x20);
  1883. sx_dprintk(SX_DEBUG_PROBE, "checking identifier...\n");
  1884. if (strncmp(vpdp.identifier, SX_VPD_IDENT_STRING, 16) != 0) {
  1885. sx_dprintk(SX_DEBUG_PROBE, "Got non-SX identifier: "
  1886. "'%s'\n", vpdp.identifier);
  1887. return 0;
  1888. }
  1889. }
  1890. printheader();
  1891. if (!IS_CF_BOARD(board)) {
  1892. printk(KERN_DEBUG "sx: Found an SX board at %lx\n",
  1893. board->hw_base);
  1894. printk(KERN_DEBUG "sx: hw_rev: %d, assembly level: %d, "
  1895. "uniq ID:%08x, ",
  1896. vpdp.hwrev, vpdp.hwass, vpdp.uniqid);
  1897. printk("Manufactured: %d/%d\n", 1970 + vpdp.myear, vpdp.mweek);
  1898. if ((((vpdp.uniqid >> 24) & SX_UNIQUEID_MASK) !=
  1899. SX_PCI_UNIQUEID1) && (((vpdp.uniqid >> 24) &
  1900. SX_UNIQUEID_MASK) != SX_ISA_UNIQUEID1)) {
  1901. /* This might be a bit harsh. This was the primary
  1902. reason the SX/ISA card didn't work at first... */
  1903. printk(KERN_ERR "sx: Hmm. Not an SX/PCI or SX/ISA "
  1904. "card. Sorry: giving up.\n");
  1905. return (0);
  1906. }
  1907. if (((vpdp.uniqid >> 24) & SX_UNIQUEID_MASK) ==
  1908. SX_ISA_UNIQUEID1) {
  1909. if (((unsigned long)board->hw_base) & 0x8000) {
  1910. printk(KERN_WARNING "sx: Warning: There may be "
  1911. "hardware problems with the card at "
  1912. "%lx.\n", board->hw_base);
  1913. printk(KERN_WARNING "sx: Read sx.txt for more "
  1914. "info.\n");
  1915. }
  1916. }
  1917. }
  1918. board->nports = -1;
  1919. /* This resets the processor, and keeps it off the bus. */
  1920. if (!sx_reset(board))
  1921. return 0;
  1922. sx_dprintk(SX_DEBUG_INIT, "reset the board...\n");
  1923. func_exit();
  1924. return 1;
  1925. }
  1926. #if defined(CONFIG_ISA) || defined(CONFIG_EISA)
  1927. /* Specialix probes for this card at 32k increments from 640k to 16M.
  1928. I consider machines with less than 16M unlikely nowadays, so I'm
  1929. not probing above 1Mb. Also, 0xa0000, 0xb0000, are taken by the VGA
  1930. card. 0xe0000 and 0xf0000 are taken by the BIOS. That only leaves
  1931. 0xc0000, 0xc8000, 0xd0000 and 0xd8000 . */
  1932. static int __devinit probe_si(struct sx_board *board)
  1933. {
  1934. int i;
  1935. func_enter();
  1936. sx_dprintk(SX_DEBUG_PROBE, "Going to verify SI signature hw %lx at "
  1937. "%p.\n", board->hw_base, board->base + SI2_ISA_ID_BASE);
  1938. if (sx_debug & SX_DEBUG_PROBE)
  1939. my_hd_io(board->base + SI2_ISA_ID_BASE, 0x8);
  1940. if (!IS_EISA_BOARD(board)) {
  1941. if (IS_SI1_BOARD(board)) {
  1942. for (i = 0; i < 8; i++) {
  1943. write_sx_byte(board, SI2_ISA_ID_BASE + 7 - i,i);
  1944. }
  1945. }
  1946. for (i = 0; i < 8; i++) {
  1947. if ((read_sx_byte(board, SI2_ISA_ID_BASE + 7 - i) & 7)
  1948. != i) {
  1949. func_exit();
  1950. return 0;
  1951. }
  1952. }
  1953. }
  1954. /* Now we're pretty much convinced that there is an SI board here,
  1955. but to prevent trouble, we'd better double check that we don't
  1956. have an SI1 board when we're probing for an SI2 board.... */
  1957. write_sx_byte(board, SI2_ISA_ID_BASE, 0x10);
  1958. if (IS_SI1_BOARD(board)) {
  1959. /* This should be an SI1 board, which has this
  1960. location writable... */
  1961. if (read_sx_byte(board, SI2_ISA_ID_BASE) != 0x10) {
  1962. func_exit();
  1963. return 0;
  1964. }
  1965. } else {
  1966. /* This should be an SI2 board, which has the bottom
  1967. 3 bits non-writable... */
  1968. if (read_sx_byte(board, SI2_ISA_ID_BASE) == 0x10) {
  1969. func_exit();
  1970. return 0;
  1971. }
  1972. }
  1973. /* Now we're pretty much convinced that there is an SI board here,
  1974. but to prevent trouble, we'd better double check that we don't
  1975. have an SI1 board when we're probing for an SI2 board.... */
  1976. write_sx_byte(board, SI2_ISA_ID_BASE, 0x10);
  1977. if (IS_SI1_BOARD(board)) {
  1978. /* This should be an SI1 board, which has this
  1979. location writable... */
  1980. if (read_sx_byte(board, SI2_ISA_ID_BASE) != 0x10) {
  1981. func_exit();
  1982. return 0;
  1983. }
  1984. } else {
  1985. /* This should be an SI2 board, which has the bottom
  1986. 3 bits non-writable... */
  1987. if (read_sx_byte(board, SI2_ISA_ID_BASE) == 0x10) {
  1988. func_exit();
  1989. return 0;
  1990. }
  1991. }
  1992. printheader();
  1993. printk(KERN_DEBUG "sx: Found an SI board at %lx\n", board->hw_base);
  1994. /* Compared to the SX boards, it is a complete guess as to what
  1995. this card is up to... */
  1996. board->nports = -1;
  1997. /* This resets the processor, and keeps it off the bus. */
  1998. if (!sx_reset(board))
  1999. return 0;
  2000. sx_dprintk(SX_DEBUG_INIT, "reset the board...\n");
  2001. func_exit();
  2002. return 1;
  2003. }
  2004. #endif
  2005. static const struct tty_operations sx_ops = {
  2006. .break_ctl = sx_break,
  2007. .open = sx_open,
  2008. .close = gs_close,
  2009. .write = gs_write,
  2010. .put_char = gs_put_char,
  2011. .flush_chars = gs_flush_chars,
  2012. .write_room = gs_write_room,
  2013. .chars_in_buffer = gs_chars_in_buffer,
  2014. .flush_buffer = gs_flush_buffer,
  2015. .ioctl = sx_ioctl,
  2016. .throttle = sx_throttle,
  2017. .unthrottle = sx_unthrottle,
  2018. .set_termios = gs_set_termios,
  2019. .stop = gs_stop,
  2020. .start = gs_start,
  2021. .hangup = gs_hangup,
  2022. .tiocmget = sx_tiocmget,
  2023. .tiocmset = sx_tiocmset,
  2024. };
  2025. static int sx_init_drivers(void)
  2026. {
  2027. int error;
  2028. func_enter();
  2029. sx_driver = alloc_tty_driver(sx_nports);
  2030. if (!sx_driver)
  2031. return 1;
  2032. sx_driver->owner = THIS_MODULE;
  2033. sx_driver->driver_name = "specialix_sx";
  2034. sx_driver->name = "ttyX";
  2035. sx_driver->major = SX_NORMAL_MAJOR;
  2036. sx_driver->type = TTY_DRIVER_TYPE_SERIAL;
  2037. sx_driver->subtype = SERIAL_TYPE_NORMAL;
  2038. sx_driver->init_termios = tty_std_termios;
  2039. sx_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  2040. sx_driver->init_termios.c_ispeed = 9600;
  2041. sx_driver->init_termios.c_ospeed = 9600;
  2042. sx_driver->flags = TTY_DRIVER_REAL_RAW;
  2043. tty_set_operations(sx_driver, &sx_ops);
  2044. if ((error = tty_register_driver(sx_driver))) {
  2045. put_tty_driver(sx_driver);
  2046. printk(KERN_ERR "sx: Couldn't register sx driver, error = %d\n",
  2047. error);
  2048. return 1;
  2049. }
  2050. func_exit();
  2051. return 0;
  2052. }
  2053. static int sx_init_portstructs(int nboards, int nports)
  2054. {
  2055. struct sx_board *board;
  2056. struct sx_port *port;
  2057. int i, j;
  2058. int addr, chans;
  2059. int portno;
  2060. func_enter();
  2061. /* Many drivers statically allocate the maximum number of ports
  2062. There is no reason not to allocate them dynamically.
  2063. Is there? -- REW */
  2064. sx_ports = kcalloc(nports, sizeof(struct sx_port), GFP_KERNEL);
  2065. if (!sx_ports)
  2066. return -ENOMEM;
  2067. port = sx_ports;
  2068. for (i = 0; i < nboards; i++) {
  2069. board = &boards[i];
  2070. board->ports = port;
  2071. for (j = 0; j < boards[i].nports; j++) {
  2072. sx_dprintk(SX_DEBUG_INIT, "initing port %d\n", j);
  2073. port->gs.magic = SX_MAGIC;
  2074. port->gs.close_delay = HZ / 2;
  2075. port->gs.closing_wait = 30 * HZ;
  2076. port->board = board;
  2077. port->gs.rd = &sx_real_driver;
  2078. #ifdef NEW_WRITE_LOCKING
  2079. port->gs.port_write_mutex = MUTEX;
  2080. #endif
  2081. spin_lock_init(&port->gs.driver_lock);
  2082. /*
  2083. * Initializing wait queue
  2084. */
  2085. init_waitqueue_head(&port->gs.open_wait);
  2086. init_waitqueue_head(&port->gs.close_wait);
  2087. port++;
  2088. }
  2089. }
  2090. port = sx_ports;
  2091. portno = 0;
  2092. for (i = 0; i < nboards; i++) {
  2093. board = &boards[i];
  2094. board->port_base = portno;
  2095. /* Possibly the configuration was rejected. */
  2096. sx_dprintk(SX_DEBUG_PROBE, "Board has %d channels\n",
  2097. board->nports);
  2098. if (board->nports <= 0)
  2099. continue;
  2100. /* XXX byteorder ?? */
  2101. for (addr = 0x80; addr != 0;
  2102. addr = read_sx_word(board, addr) & 0x7fff) {
  2103. chans = sx_read_module_byte(board, addr, mc_type);
  2104. sx_dprintk(SX_DEBUG_PROBE, "Module at %x: %d "
  2105. "channels\n", addr, chans);
  2106. sx_dprintk(SX_DEBUG_PROBE, "Port at");
  2107. for (j = 0; j < chans; j++) {
  2108. /* The "sx-way" is the way it SHOULD be done.
  2109. That way in the future, the firmware may for
  2110. example pack the structures a bit more
  2111. efficient. Neil tells me it isn't going to
  2112. happen anytime soon though. */
  2113. if (IS_SX_BOARD(board))
  2114. port->ch_base = sx_read_module_word(
  2115. board, addr + j * 2,
  2116. mc_chan_pointer);
  2117. else
  2118. port->ch_base = addr + 0x100 + 0x300 *j;
  2119. sx_dprintk(SX_DEBUG_PROBE, " %x",
  2120. port->ch_base);
  2121. port->line = portno++;
  2122. port++;
  2123. }
  2124. sx_dprintk(SX_DEBUG_PROBE, "\n");
  2125. }
  2126. /* This has to be done earlier. */
  2127. /* board->flags |= SX_BOARD_INITIALIZED; */
  2128. }
  2129. func_exit();
  2130. return 0;
  2131. }
  2132. static unsigned int sx_find_free_board(void)
  2133. {
  2134. unsigned int i;
  2135. for (i = 0; i < SX_NBOARDS; i++)
  2136. if (!(boards[i].flags & SX_BOARD_PRESENT))
  2137. break;
  2138. return i;
  2139. }
  2140. static void __exit sx_release_drivers(void)
  2141. {
  2142. func_enter();
  2143. tty_unregister_driver(sx_driver);
  2144. put_tty_driver(sx_driver);
  2145. func_exit();
  2146. }
  2147. static void __devexit sx_remove_card(struct sx_board *board,
  2148. struct pci_dev *pdev)
  2149. {
  2150. if (board->flags & SX_BOARD_INITIALIZED) {
  2151. /* The board should stop messing with us. (actually I mean the
  2152. interrupt) */
  2153. sx_reset(board);
  2154. if ((board->irq) && (board->flags & SX_IRQ_ALLOCATED))
  2155. free_irq(board->irq, board);
  2156. /* It is safe/allowed to del_timer a non-active timer */
  2157. del_timer(&board->timer);
  2158. if (pdev) {
  2159. #ifdef CONFIG_PCI
  2160. pci_iounmap(pdev, board->base);
  2161. pci_release_region(pdev, IS_CF_BOARD(board) ? 3 : 2);
  2162. #endif
  2163. } else {
  2164. iounmap(board->base);
  2165. release_region(board->hw_base, board->hw_len);
  2166. }
  2167. board->flags &= ~(SX_BOARD_INITIALIZED | SX_BOARD_PRESENT);
  2168. }
  2169. }
  2170. #ifdef CONFIG_EISA
  2171. static int __devinit sx_eisa_probe(struct device *dev)
  2172. {
  2173. struct eisa_device *edev = to_eisa_device(dev);
  2174. struct sx_board *board;
  2175. unsigned long eisa_slot = edev->base_addr;
  2176. unsigned int i;
  2177. int retval = -EIO;
  2178. mutex_lock(&sx_boards_lock);
  2179. i = sx_find_free_board();
  2180. if (i == SX_NBOARDS) {
  2181. mutex_unlock(&sx_boards_lock);
  2182. goto err;
  2183. }
  2184. board = &boards[i];
  2185. board->flags |= SX_BOARD_PRESENT;
  2186. mutex_unlock(&sx_boards_lock);
  2187. dev_info(dev, "XIO : Signature found in EISA slot %lu, "
  2188. "Product %d Rev %d (REPORT THIS TO LKLM)\n",
  2189. eisa_slot >> 12,
  2190. inb(eisa_slot + EISA_VENDOR_ID_OFFSET + 2),
  2191. inb(eisa_slot + EISA_VENDOR_ID_OFFSET + 3));
  2192. board->eisa_base = eisa_slot;
  2193. board->flags &= ~SX_BOARD_TYPE;
  2194. board->flags |= SI_EISA_BOARD;
  2195. board->hw_base = ((inb(eisa_slot + 0xc01) << 8) +
  2196. inb(eisa_slot + 0xc00)) << 16;
  2197. board->hw_len = SI2_EISA_WINDOW_LEN;
  2198. if (!request_region(board->hw_base, board->hw_len, "sx")) {
  2199. dev_err(dev, "can't request region\n");
  2200. goto err_flag;
  2201. }
  2202. board->base2 =
  2203. board->base = ioremap(board->hw_base, SI2_EISA_WINDOW_LEN);
  2204. if (!board->base) {
  2205. dev_err(dev, "can't remap memory\n");
  2206. goto err_reg;
  2207. }
  2208. sx_dprintk(SX_DEBUG_PROBE, "IO hw_base address: %lx\n", board->hw_base);
  2209. sx_dprintk(SX_DEBUG_PROBE, "base: %p\n", board->base);
  2210. board->irq = inb(eisa_slot + 0xc02) >> 4;
  2211. sx_dprintk(SX_DEBUG_PROBE, "IRQ: %d\n", board->irq);
  2212. if (!probe_si(board))
  2213. goto err_unmap;
  2214. dev_set_drvdata(dev, board);
  2215. return 0;
  2216. err_unmap:
  2217. iounmap(board->base);
  2218. err_reg:
  2219. release_region(board->hw_base, board->hw_len);
  2220. err_flag:
  2221. board->flags &= ~SX_BOARD_PRESENT;
  2222. err:
  2223. return retval;
  2224. }
  2225. static int __devexit sx_eisa_remove(struct device *dev)
  2226. {
  2227. struct sx_board *board = dev_get_drvdata(dev);
  2228. sx_remove_card(board, NULL);
  2229. return 0;
  2230. }
  2231. static struct eisa_device_id sx_eisa_tbl[] = {
  2232. { "SLX" },
  2233. { "" }
  2234. };
  2235. MODULE_DEVICE_TABLE(eisa, sx_eisa_tbl);
  2236. static struct eisa_driver sx_eisadriver = {
  2237. .id_table = sx_eisa_tbl,
  2238. .driver = {
  2239. .name = "sx",
  2240. .probe = sx_eisa_probe,
  2241. .remove = __devexit_p(sx_eisa_remove),
  2242. }
  2243. };
  2244. #endif
  2245. #ifdef CONFIG_PCI
  2246. /********************************************************
  2247. * Setting bit 17 in the CNTRL register of the PLX 9050 *
  2248. * chip forces a retry on writes while a read is pending.*
  2249. * This is to prevent the card locking up on Intel Xeon *
  2250. * multiprocessor systems with the NX chipset. -- NV *
  2251. ********************************************************/
  2252. /* Newer cards are produced with this bit set from the configuration
  2253. EEprom. As the bit is read/write for the CPU, we can fix it here,
  2254. if we detect that it isn't set correctly. -- REW */
  2255. static void __devinit fix_sx_pci(struct pci_dev *pdev, struct sx_board *board)
  2256. {
  2257. unsigned int hwbase;
  2258. void __iomem *rebase;
  2259. unsigned int t;
  2260. #define CNTRL_REG_OFFSET 0x50
  2261. #define CNTRL_REG_GOODVALUE 0x18260000
  2262. pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &hwbase);
  2263. hwbase &= PCI_BASE_ADDRESS_MEM_MASK;
  2264. rebase = ioremap(hwbase, 0x80);
  2265. t = readl(rebase + CNTRL_REG_OFFSET);
  2266. if (t != CNTRL_REG_GOODVALUE) {
  2267. printk(KERN_DEBUG "sx: performing cntrl reg fix: %08x -> "
  2268. "%08x\n", t, CNTRL_REG_GOODVALUE);
  2269. writel(CNTRL_REG_GOODVALUE, rebase + CNTRL_REG_OFFSET);
  2270. }
  2271. iounmap(rebase);
  2272. }
  2273. #endif
  2274. static int __devinit sx_pci_probe(struct pci_dev *pdev,
  2275. const struct pci_device_id *ent)
  2276. {
  2277. #ifdef CONFIG_PCI
  2278. struct sx_board *board;
  2279. unsigned int i, reg;
  2280. int retval = -EIO;
  2281. mutex_lock(&sx_boards_lock);
  2282. i = sx_find_free_board();
  2283. if (i == SX_NBOARDS) {
  2284. mutex_unlock(&sx_boards_lock);
  2285. goto err;
  2286. }
  2287. board = &boards[i];
  2288. board->flags |= SX_BOARD_PRESENT;
  2289. mutex_unlock(&sx_boards_lock);
  2290. retval = pci_enable_device(pdev);
  2291. if (retval)
  2292. goto err_flag;
  2293. board->flags &= ~SX_BOARD_TYPE;
  2294. board->flags |= (pdev->subsystem_vendor == 0x200) ? SX_PCI_BOARD :
  2295. SX_CFPCI_BOARD;
  2296. /* CF boards use base address 3.... */
  2297. reg = IS_CF_BOARD(board) ? 3 : 2;
  2298. retval = pci_request_region(pdev, reg, "sx");
  2299. if (retval) {
  2300. dev_err(&pdev->dev, "can't request region\n");
  2301. goto err_flag;
  2302. }
  2303. board->hw_base = pci_resource_start(pdev, reg);
  2304. board->base2 =
  2305. board->base = pci_iomap(pdev, reg, WINDOW_LEN(board));
  2306. if (!board->base) {
  2307. dev_err(&pdev->dev, "ioremap failed\n");
  2308. goto err_reg;
  2309. }
  2310. /* Most of the stuff on the CF board is offset by 0x18000 .... */
  2311. if (IS_CF_BOARD(board))
  2312. board->base += 0x18000;
  2313. board->irq = pdev->irq;
  2314. dev_info(&pdev->dev, "Got a specialix card: %p(%d) %x.\n", board->base,
  2315. board->irq, board->flags);
  2316. if (!probe_sx(board)) {
  2317. retval = -EIO;
  2318. goto err_unmap;
  2319. }
  2320. fix_sx_pci(pdev, board);
  2321. pci_set_drvdata(pdev, board);
  2322. return 0;
  2323. err_unmap:
  2324. pci_iounmap(pdev, board->base);
  2325. err_reg:
  2326. pci_release_region(pdev, reg);
  2327. err_flag:
  2328. board->flags &= ~SX_BOARD_PRESENT;
  2329. err:
  2330. return retval;
  2331. #else
  2332. return -ENODEV;
  2333. #endif
  2334. }
  2335. static void __devexit sx_pci_remove(struct pci_dev *pdev)
  2336. {
  2337. struct sx_board *board = pci_get_drvdata(pdev);
  2338. sx_remove_card(board, pdev);
  2339. }
  2340. /* Specialix has a whole bunch of cards with 0x2000 as the device ID. They say
  2341. its because the standard requires it. So check for SUBVENDOR_ID. */
  2342. static struct pci_device_id sx_pci_tbl[] = {
  2343. { PCI_VENDOR_ID_SPECIALIX, PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8,
  2344. .subvendor = 0x0200,.subdevice = PCI_ANY_ID },
  2345. { PCI_VENDOR_ID_SPECIALIX, PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8,
  2346. .subvendor = 0x0300,.subdevice = PCI_ANY_ID },
  2347. { 0 }
  2348. };
  2349. MODULE_DEVICE_TABLE(pci, sx_pci_tbl);
  2350. static struct pci_driver sx_pcidriver = {
  2351. .name = "sx",
  2352. .id_table = sx_pci_tbl,
  2353. .probe = sx_pci_probe,
  2354. .remove = __devexit_p(sx_pci_remove)
  2355. };
  2356. static int __init sx_init(void)
  2357. {
  2358. #ifdef CONFIG_EISA
  2359. int retval1;
  2360. #endif
  2361. #ifdef CONFIG_ISA
  2362. struct sx_board *board;
  2363. unsigned int i;
  2364. #endif
  2365. unsigned int found = 0;
  2366. int retval;
  2367. func_enter();
  2368. sx_dprintk(SX_DEBUG_INIT, "Initing sx module... (sx_debug=%d)\n",
  2369. sx_debug);
  2370. if (abs((long)(&sx_debug) - sx_debug) < 0x10000) {
  2371. printk(KERN_WARNING "sx: sx_debug is an address, instead of a "
  2372. "value. Assuming -1.\n(%p)\n", &sx_debug);
  2373. sx_debug = -1;
  2374. }
  2375. if (misc_register(&sx_fw_device) < 0) {
  2376. printk(KERN_ERR "SX: Unable to register firmware loader "
  2377. "driver.\n");
  2378. return -EIO;
  2379. }
  2380. #ifdef CONFIG_ISA
  2381. for (i = 0; i < NR_SX_ADDRS; i++) {
  2382. board = &boards[found];
  2383. board->hw_base = sx_probe_addrs[i];
  2384. board->hw_len = SX_WINDOW_LEN;
  2385. if (!request_region(board->hw_base, board->hw_len, "sx"))
  2386. continue;
  2387. board->base2 =
  2388. board->base = ioremap(board->hw_base, board->hw_len);
  2389. if (!board->base)
  2390. goto err_sx_reg;
  2391. board->flags &= ~SX_BOARD_TYPE;
  2392. board->flags |= SX_ISA_BOARD;
  2393. board->irq = sx_irqmask ? -1 : 0;
  2394. if (probe_sx(board)) {
  2395. board->flags |= SX_BOARD_PRESENT;
  2396. found++;
  2397. } else {
  2398. iounmap(board->base);
  2399. err_sx_reg:
  2400. release_region(board->hw_base, board->hw_len);
  2401. }
  2402. }
  2403. for (i = 0; i < NR_SI_ADDRS; i++) {
  2404. board = &boards[found];
  2405. board->hw_base = si_probe_addrs[i];
  2406. board->hw_len = SI2_ISA_WINDOW_LEN;
  2407. if (!request_region(board->hw_base, board->hw_len, "sx"))
  2408. continue;
  2409. board->base2 =
  2410. board->base = ioremap(board->hw_base, board->hw_len);
  2411. if (!board->base)
  2412. goto err_si_reg;
  2413. board->flags &= ~SX_BOARD_TYPE;
  2414. board->flags |= SI_ISA_BOARD;
  2415. board->irq = sx_irqmask ? -1 : 0;
  2416. if (probe_si(board)) {
  2417. board->flags |= SX_BOARD_PRESENT;
  2418. found++;
  2419. } else {
  2420. iounmap(board->base);
  2421. err_si_reg:
  2422. release_region(board->hw_base, board->hw_len);
  2423. }
  2424. }
  2425. for (i = 0; i < NR_SI1_ADDRS; i++) {
  2426. board = &boards[found];
  2427. board->hw_base = si1_probe_addrs[i];
  2428. board->hw_len = SI1_ISA_WINDOW_LEN;
  2429. if (!request_region(board->hw_base, board->hw_len, "sx"))
  2430. continue;
  2431. board->base2 =
  2432. board->base = ioremap(board->hw_base, board->hw_len);
  2433. if (!board->base)
  2434. goto err_si1_reg;
  2435. board->flags &= ~SX_BOARD_TYPE;
  2436. board->flags |= SI1_ISA_BOARD;
  2437. board->irq = sx_irqmask ? -1 : 0;
  2438. if (probe_si(board)) {
  2439. board->flags |= SX_BOARD_PRESENT;
  2440. found++;
  2441. } else {
  2442. iounmap(board->base);
  2443. err_si1_reg:
  2444. release_region(board->hw_base, board->hw_len);
  2445. }
  2446. }
  2447. #endif
  2448. #ifdef CONFIG_EISA
  2449. retval1 = eisa_driver_register(&sx_eisadriver);
  2450. #endif
  2451. retval = pci_register_driver(&sx_pcidriver);
  2452. if (found) {
  2453. printk(KERN_INFO "sx: total of %d boards detected.\n", found);
  2454. retval = 0;
  2455. } else if (retval) {
  2456. #ifdef CONFIG_EISA
  2457. retval = retval1;
  2458. if (retval1)
  2459. #endif
  2460. misc_deregister(&sx_fw_device);
  2461. }
  2462. func_exit();
  2463. return retval;
  2464. }
  2465. static void __exit sx_exit(void)
  2466. {
  2467. int i;
  2468. func_enter();
  2469. #ifdef CONFIG_EISA
  2470. eisa_driver_unregister(&sx_eisadriver);
  2471. #endif
  2472. pci_unregister_driver(&sx_pcidriver);
  2473. for (i = 0; i < SX_NBOARDS; i++)
  2474. sx_remove_card(&boards[i], NULL);
  2475. if (misc_deregister(&sx_fw_device) < 0) {
  2476. printk(KERN_INFO "sx: couldn't deregister firmware loader "
  2477. "device\n");
  2478. }
  2479. sx_dprintk(SX_DEBUG_CLEANUP, "Cleaning up drivers (%d)\n",
  2480. sx_initialized);
  2481. if (sx_initialized)
  2482. sx_release_drivers();
  2483. kfree(sx_ports);
  2484. func_exit();
  2485. }
  2486. module_init(sx_init);
  2487. module_exit(sx_exit);