/drivers/char/sx.c

https://bitbucket.org/cresqo/cm7-p500-kernel · C · 2894 lines · 2000 code · 388 blank · 506 comment · 329 complexity · 4687a44d3d9a40fa7059fcf6dd30472a MD5 · raw file

Large files are truncated click here to view the full file

  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/smp_lock.h>
  218. #include <linux/init.h>
  219. #include <linux/miscdevice.h>
  220. #include <linux/bitops.h>
  221. #include <asm/io.h>
  222. #include <asm/uaccess.h>
  223. /* The 3.0.0 version of sxboards/sxwindow.h uses BYTE and WORD.... */
  224. #define BYTE u8
  225. #define WORD u16
  226. /* .... but the 3.0.4 version uses _u8 and _u16. */
  227. #define _u8 u8
  228. #define _u16 u16
  229. #include "sxboards.h"
  230. #include "sxwindow.h"
  231. #include <linux/generic_serial.h>
  232. #include "sx.h"
  233. /* I don't think that this driver can handle more than 256 ports on
  234. one machine. You'll have to increase the number of boards in sx.h
  235. if you want more than 4 boards. */
  236. #ifndef PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8
  237. #define PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8 0x2000
  238. #endif
  239. /* Configurable options:
  240. (Don't be too sure that it'll work if you toggle them) */
  241. /* Am I paranoid or not ? ;-) */
  242. #undef SX_PARANOIA_CHECK
  243. /* 20 -> 2000 per second. The card should rate-limit interrupts at 100
  244. Hz, but it is user configurable. I don't recommend going above 1000
  245. Hz. The interrupt ratelimit might trigger if the interrupt is
  246. shared with a very active other device. */
  247. #define IRQ_RATE_LIMIT 20
  248. /* Sharing interrupts is possible now. If the other device wants more
  249. than 2000 interrupts per second, we'd gracefully decline further
  250. interrupts. That's not what we want. On the other hand, if the
  251. other device interrupts 2000 times a second, don't use the SX
  252. interrupt. Use polling. */
  253. #undef IRQ_RATE_LIMIT
  254. #if 0
  255. /* Not implemented */
  256. /*
  257. * The following defines are mostly for testing purposes. But if you need
  258. * some nice reporting in your syslog, you can define them also.
  259. */
  260. #define SX_REPORT_FIFO
  261. #define SX_REPORT_OVERRUN
  262. #endif
  263. /* Function prototypes */
  264. static void sx_disable_tx_interrupts(void *ptr);
  265. static void sx_enable_tx_interrupts(void *ptr);
  266. static void sx_disable_rx_interrupts(void *ptr);
  267. static void sx_enable_rx_interrupts(void *ptr);
  268. static int sx_carrier_raised(struct tty_port *port);
  269. static void sx_shutdown_port(void *ptr);
  270. static int sx_set_real_termios(void *ptr);
  271. static void sx_close(void *ptr);
  272. static int sx_chars_in_buffer(void *ptr);
  273. static int sx_init_board(struct sx_board *board);
  274. static int sx_init_portstructs(int nboards, int nports);
  275. static long sx_fw_ioctl(struct file *filp, unsigned int cmd,
  276. unsigned long arg);
  277. static int sx_init_drivers(void);
  278. static struct tty_driver *sx_driver;
  279. static DEFINE_MUTEX(sx_boards_lock);
  280. static struct sx_board boards[SX_NBOARDS];
  281. static struct sx_port *sx_ports;
  282. static int sx_initialized;
  283. static int sx_nports;
  284. static int sx_debug;
  285. /* You can have the driver poll your card.
  286. - Set sx_poll to 1 to poll every timer tick (10ms on Intel).
  287. This is used when the card cannot use an interrupt for some reason.
  288. - set sx_slowpoll to 100 to do an extra poll once a second (on Intel). If
  289. the driver misses an interrupt (report this if it DOES happen to you!)
  290. everything will continue to work....
  291. */
  292. static int sx_poll = 1;
  293. static int sx_slowpoll;
  294. /* The card limits the number of interrupts per second.
  295. At 115k2 "100" should be sufficient.
  296. If you're using higher baudrates, you can increase this...
  297. */
  298. static int sx_maxints = 100;
  299. #ifdef CONFIG_ISA
  300. /* These are the only open spaces in my computer. Yours may have more
  301. or less.... -- REW
  302. duh: Card at 0xa0000 is possible on HP Netserver?? -- pvdl
  303. */
  304. static int sx_probe_addrs[] = {
  305. 0xc0000, 0xd0000, 0xe0000,
  306. 0xc8000, 0xd8000, 0xe8000
  307. };
  308. static int si_probe_addrs[] = {
  309. 0xc0000, 0xd0000, 0xe0000,
  310. 0xc8000, 0xd8000, 0xe8000, 0xa0000
  311. };
  312. static int si1_probe_addrs[] = {
  313. 0xd0000
  314. };
  315. #define NR_SX_ADDRS ARRAY_SIZE(sx_probe_addrs)
  316. #define NR_SI_ADDRS ARRAY_SIZE(si_probe_addrs)
  317. #define NR_SI1_ADDRS ARRAY_SIZE(si1_probe_addrs)
  318. module_param_array(sx_probe_addrs, int, NULL, 0);
  319. module_param_array(si_probe_addrs, int, NULL, 0);
  320. #endif
  321. /* Set the mask to all-ones. This alas, only supports 32 interrupts.
  322. Some architectures may need more. */
  323. static int sx_irqmask = -1;
  324. module_param(sx_poll, int, 0);
  325. module_param(sx_slowpoll, int, 0);
  326. module_param(sx_maxints, int, 0);
  327. module_param(sx_debug, int, 0);
  328. module_param(sx_irqmask, int, 0);
  329. MODULE_LICENSE("GPL");
  330. static struct real_driver sx_real_driver = {
  331. sx_disable_tx_interrupts,
  332. sx_enable_tx_interrupts,
  333. sx_disable_rx_interrupts,
  334. sx_enable_rx_interrupts,
  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",__func__)
  355. #define func_exit() sx_dprintk(SX_DEBUG_FLOW, "sx: exit %s\n",__func__)
  356. #define func_enter2() sx_dprintk(SX_DEBUG_FLOW, "sx: enter %s (port %d)\n", \
  357. __func__, 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. .unlocked_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, tty_port_carrier_raised(&port->gs.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.port.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.port.tty->termios->c_cflag
  836. sx_write_channel_byte(port, hi_mr1,
  837. (C_PARENB(port->gs.port.tty) ? MR1_WITH : MR1_NONE) |
  838. (C_PARODD(port->gs.port.tty) ? MR1_ODD : MR1_EVEN) |
  839. (C_CRTSCTS(port->gs.port.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.port.tty) ? MR2_CTS_TXFLOW : 0) |
  846. (C_CSTOPB(port->gs.port.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",
  863. (unsigned int)CFLAG & CSIZE);
  864. break;
  865. }
  866. sx_write_channel_byte(port, hi_prtcl,
  867. (I_IXON(port->gs.port.tty) ? SP_TXEN : 0) |
  868. (I_IXOFF(port->gs.port.tty) ? SP_RXEN : 0) |
  869. (I_IXANY(port->gs.port.tty) ? SP_TANY : 0) | SP_DCEN);
  870. sx_write_channel_byte(port, hi_break,
  871. (I_IGNBRK(port->gs.port.tty) ? BR_IGN : 0 |
  872. I_BRKINT(port->gs.port.tty) ? BR_INT : 0));
  873. sx_write_channel_byte(port, hi_txon, START_CHAR(port->gs.port.tty));
  874. sx_write_channel_byte(port, hi_rxon, START_CHAR(port->gs.port.tty));
  875. sx_write_channel_byte(port, hi_txoff, STOP_CHAR(port->gs.port.tty));
  876. sx_write_channel_byte(port, hi_rxoff, STOP_CHAR(port->gs.port.tty));
  877. sx_reconfigure_port(port);
  878. /* Tell line discipline whether we will do input cooking */
  879. if (I_OTHER(port->gs.port.tty)) {
  880. clear_bit(TTY_HW_COOK_IN, &port->gs.port.tty->flags);
  881. } else {
  882. set_bit(TTY_HW_COOK_IN, &port->gs.port.tty->flags);
  883. }
  884. sx_dprintk(SX_DEBUG_TERMIOS, "iflags: %x(%d) ",
  885. (unsigned int)port->gs.port.tty->termios->c_iflag,
  886. I_OTHER(port->gs.port.tty));
  887. /* Tell line discipline whether we will do output cooking.
  888. * If OPOST is set and no other output flags are set then we can do output
  889. * processing. Even if only *one* other flag in the O_OTHER group is set
  890. * we do cooking in software.
  891. */
  892. if (O_OPOST(port->gs.port.tty) && !O_OTHER(port->gs.port.tty)) {
  893. set_bit(TTY_HW_COOK_OUT, &port->gs.port.tty->flags);
  894. } else {
  895. clear_bit(TTY_HW_COOK_OUT, &port->gs.port.tty->flags);
  896. }
  897. sx_dprintk(SX_DEBUG_TERMIOS, "oflags: %x(%d)\n",
  898. (unsigned int)port->gs.port.tty->termios->c_oflag,
  899. O_OTHER(port->gs.port.tty));
  900. /* port->c_dcd = sx_get_CD (port); */
  901. func_exit();
  902. return 0;
  903. }
  904. /* ********************************************************************** *
  905. * the interrupt related routines *
  906. * ********************************************************************** */
  907. /* Note:
  908. Other drivers use the macro "MIN" to calculate how much to copy.
  909. This has the disadvantage that it will evaluate parts twice. That's
  910. expensive when it's IO (and the compiler cannot optimize those away!).
  911. Moreover, I'm not sure that you're race-free.
  912. I assign a value, and then only allow the value to decrease. This
  913. is always safe. This makes the code a few lines longer, and you
  914. know I'm dead against that, but I think it is required in this
  915. case. */
  916. static void sx_transmit_chars(struct sx_port *port)
  917. {
  918. int c;
  919. int tx_ip;
  920. int txroom;
  921. func_enter2();
  922. sx_dprintk(SX_DEBUG_TRANSMIT, "Port %p: transmit %d chars\n",
  923. port, port->gs.xmit_cnt);
  924. if (test_and_set_bit(SX_PORT_TRANSMIT_LOCK, &port->locks)) {
  925. return;
  926. }
  927. while (1) {
  928. c = port->gs.xmit_cnt;
  929. sx_dprintk(SX_DEBUG_TRANSMIT, "Copying %d ", c);
  930. tx_ip = sx_read_channel_byte(port, hi_txipos);
  931. /* Took me 5 minutes to deduce this formula.
  932. Luckily it is literally in the manual in section 6.5.4.3.5 */
  933. txroom = (sx_read_channel_byte(port, hi_txopos) - tx_ip - 1) &
  934. 0xff;
  935. /* Don't copy more bytes than there is room for in the buffer */
  936. if (c > txroom)
  937. c = txroom;
  938. sx_dprintk(SX_DEBUG_TRANSMIT, " %d(%d) ", c, txroom);
  939. /* Don't copy past the end of the hardware transmit buffer */
  940. if (c > 0x100 - tx_ip)
  941. c = 0x100 - tx_ip;
  942. sx_dprintk(SX_DEBUG_TRANSMIT, " %d(%d) ", c, 0x100 - tx_ip);
  943. /* Don't copy pas the end of the source buffer */
  944. if (c > SERIAL_XMIT_SIZE - port->gs.xmit_tail)
  945. c = SERIAL_XMIT_SIZE - port->gs.xmit_tail;
  946. sx_dprintk(SX_DEBUG_TRANSMIT, " %d(%ld) \n",
  947. c, SERIAL_XMIT_SIZE - port->gs.xmit_tail);
  948. /* If for one reason or another, we can't copy more data, we're
  949. done! */
  950. if (c == 0)
  951. break;
  952. memcpy_toio(port->board->base + CHAN_OFFSET(port, hi_txbuf) +
  953. tx_ip, port->gs.xmit_buf + port->gs.xmit_tail, c);
  954. /* Update the pointer in the card */
  955. sx_write_channel_byte(port, hi_txipos, (tx_ip + c) & 0xff);
  956. /* Update the kernel buffer end */
  957. port->gs.xmit_tail = (port->gs.xmit_tail + c) &
  958. (SERIAL_XMIT_SIZE - 1);
  959. /* This one last. (this is essential)
  960. It would allow others to start putting more data into the
  961. buffer! */
  962. port->gs.xmit_cnt -= c;
  963. }
  964. if (port->gs.xmit_cnt == 0) {
  965. sx_disable_tx_interrupts(port);
  966. }
  967. if ((port->gs.xmit_cnt <= port->gs.wakeup_chars) && port->gs.port.tty) {
  968. tty_wakeup(port->gs.port.tty);
  969. sx_dprintk(SX_DEBUG_TRANSMIT, "Waking up.... ldisc (%d)....\n",
  970. port->gs.wakeup_chars);
  971. }
  972. clear_bit(SX_PORT_TRANSMIT_LOCK, &port->locks);
  973. func_exit();
  974. }
  975. /* Note the symmetry between receiving chars and transmitting them!
  976. Note: The kernel should have implemented both a receive buffer and
  977. a transmit buffer. */
  978. /* Inlined: Called only once. Remove the inline when you add another call */
  979. static inline void sx_receive_chars(struct sx_port *port)
  980. {
  981. int c;
  982. int rx_op;
  983. struct tty_struct *tty;
  984. int copied = 0;
  985. unsigned char *rp;
  986. func_enter2();
  987. tty = port->gs.port.tty;
  988. while (1) {
  989. rx_op = sx_read_channel_byte(port, hi_rxopos);
  990. c = (sx_read_channel_byte(port, hi_rxipos) - rx_op) & 0xff;
  991. sx_dprintk(SX_DEBUG_RECEIVE, "rxop=%d, c = %d.\n", rx_op, c);
  992. /* Don't copy past the end of the hardware receive buffer */
  993. if (rx_op + c > 0x100)
  994. c = 0x100 - rx_op;
  995. sx_dprintk(SX_DEBUG_RECEIVE, "c = %d.\n", c);
  996. /* Don't copy more bytes than there is room for in the buffer */
  997. c = tty_prepare_flip_string(tty, &rp, c);
  998. sx_dprintk(SX_DEBUG_RECEIVE, "c = %d.\n", c);
  999. /* If for one reason or another, we can't copy more data, we're done! */
  1000. if (c == 0)
  1001. break;
  1002. sx_dprintk(SX_DEBUG_RECEIVE, "Copying over %d chars. First is "
  1003. "%d at %lx\n", c, read_sx_byte(port->board,
  1004. CHAN_OFFSET(port, hi_rxbuf) + rx_op),
  1005. CHAN_OFFSET(port, hi_rxbuf));
  1006. memcpy_fromio(rp, port->board->base +
  1007. CHAN_OFFSET(port, hi_rxbuf) + rx_op, c);
  1008. /* This one last. ( Not essential.)
  1009. It allows the card to start putting more data into the
  1010. buffer!
  1011. Update the pointer in the card */
  1012. sx_write_channel_byte(port, hi_rxopos, (rx_op + c) & 0xff);
  1013. copied += c;
  1014. }
  1015. if (copied) {
  1016. struct timeval tv;
  1017. do_gettimeofday(&tv);
  1018. sx_dprintk(SX_DEBUG_RECEIVE, "pushing flipq port %d (%3d "
  1019. "chars): %d.%06d (%d/%d)\n", port->line,
  1020. copied, (int)(tv.tv_sec % 60), (int)tv.tv_usec,
  1021. tty->raw, tty->real_raw);
  1022. /* Tell the rest of the system the news. Great news. New
  1023. characters! */
  1024. tty_flip_buffer_push(tty);
  1025. /* tty_schedule_flip (tty); */
  1026. }
  1027. func_exit();
  1028. }
  1029. /* Inlined: it is called only once. Remove the inline if you add another
  1030. call */
  1031. static inline void sx_check_modem_signals(struct sx_port *port)
  1032. {
  1033. int hi_state;
  1034. int c_dcd;
  1035. hi_state = sx_read_channel_byte(port, hi_state);
  1036. sx_dprintk(SX_DEBUG_MODEMSIGNALS, "Checking modem signals (%d/%d)\n",
  1037. port->c_dcd, tty_port_carrier_raised(&port->gs.port));
  1038. if (hi_state & ST_BREAK) {
  1039. hi_state &= ~ST_BREAK;
  1040. sx_dprintk(SX_DEBUG_MODEMSIGNALS, "got a break.\n");
  1041. sx_write_channel_byte(port, hi_state, hi_state);
  1042. gs_got_break(&port->gs);
  1043. }
  1044. if (hi_state & ST_DCD) {
  1045. hi_state &= ~ST_DCD;
  1046. sx_dprintk(SX_DEBUG_MODEMSIGNALS, "got a DCD change.\n");
  1047. sx_write_channel_byte(port, hi_state, hi_state);
  1048. c_dcd = tty_port_carrier_raised(&port->gs.port);
  1049. sx_dprintk(SX_DEBUG_MODEMSIGNALS, "DCD is now %d\n", c_dcd);
  1050. if (c_dcd != port->c_dcd) {
  1051. port->c_dcd = c_dcd;
  1052. if (tty_port_carrier_raised(&port->gs.port)) {
  1053. /* DCD went UP */
  1054. if ((sx_read_channel_byte(port, hi_hstat) !=
  1055. HS_IDLE_CLOSED) &&
  1056. !(port->gs.port.tty->termios->
  1057. c_cflag & CLOCAL)) {
  1058. /* Are we blocking in open? */
  1059. sx_dprintk(SX_DEBUG_MODEMSIGNALS, "DCD "
  1060. "active, unblocking open\n");
  1061. wake_up_interruptible(&port->gs.port.
  1062. open_wait);
  1063. } else {
  1064. sx_dprintk(SX_DEBUG_MODEMSIGNALS, "DCD "
  1065. "raised. Ignoring.\n");
  1066. }
  1067. } else {
  1068. /* DCD went down! */
  1069. if (!(port->gs.port.tty->termios->c_cflag & CLOCAL)){
  1070. sx_dprintk(SX_DEBUG_MODEMSIGNALS, "DCD "
  1071. "dropped. hanging up....\n");
  1072. tty_hangup(port->gs.port.tty);
  1073. } else {
  1074. sx_dprintk(SX_DEBUG_MODEMSIGNALS, "DCD "
  1075. "dropped. ignoring.\n");
  1076. }
  1077. }
  1078. } else {
  1079. sx_dprintk(SX_DEBUG_MODEMSIGNALS, "Hmmm. card told us "
  1080. "DCD changed, but it didn't.\n");
  1081. }
  1082. }
  1083. }
  1084. /* This is what an interrupt routine should look like.
  1085. * Small, elegant, clear.
  1086. */
  1087. static irqreturn_t sx_interrupt(int irq, void *ptr)
  1088. {
  1089. struct sx_board *board = ptr;
  1090. struct sx_port *port;
  1091. int i;
  1092. func_enter();
  1093. sx_dprintk(SX_DEBUG_FLOW, "sx: enter sx_interrupt (%d/%d)\n", irq,
  1094. board->irq);
  1095. /* AAargh! The order in which to do these things is essential and
  1096. not trivial.
  1097. - Rate limit goes before "recursive". Otherwise a series of
  1098. recursive calls will hang the machine in the interrupt routine.
  1099. - hardware twiddling goes before "recursive". Otherwise when we
  1100. poll the card, and a recursive interrupt happens, we won't
  1101. ack the card, so it might keep on interrupting us. (especially
  1102. level sensitive interrupt systems like PCI).
  1103. - Rate limit goes before hardware twiddling. Otherwise we won't
  1104. catch a card that has gone bonkers.
  1105. - The "initialized" test goes after the hardware twiddling. Otherwise
  1106. the card will stick us in the interrupt routine again.
  1107. - The initialized test goes before recursive.
  1108. */
  1109. #ifdef IRQ_RATE_LIMIT
  1110. /* Aaargh! I'm ashamed. This costs more lines-of-code than the
  1111. actual interrupt routine!. (Well, used to when I wrote that
  1112. comment) */
  1113. {
  1114. static int lastjif;
  1115. static int nintr = 0;
  1116. if (lastjif == jiffies) {
  1117. if (++nintr > IRQ_RATE_LIMIT) {
  1118. free_irq(board->irq, board);
  1119. printk(KERN_ERR "sx: Too many interrupts. "
  1120. "Turning off interrupt %d.\n",
  1121. board->irq);
  1122. }
  1123. } else {
  1124. lastjif = jiffies;
  1125. nintr = 0;
  1126. }
  1127. }
  1128. #endif
  1129. if (board->irq == irq) {
  1130. /* Tell the card we've noticed the interrupt. */
  1131. sx_write_board_word(board, cc_int_pending, 0);
  1132. if (IS_SX_BOARD(board)) {
  1133. write_sx_byte(board, SX_RESET_IRQ, 1);
  1134. } else if (IS_EISA_BOARD(board)) {
  1135. inb(board->eisa_base + 0xc03);
  1136. write_sx_word(board, 8, 0);
  1137. } else {
  1138. write_sx_byte(board, SI2_ISA_INTCLEAR,
  1139. SI2_ISA_INTCLEAR_CLEAR);
  1140. write_sx_byte(board, SI2_ISA_INTCLEAR,
  1141. SI2_ISA_INTCLEAR_SET);
  1142. }
  1143. }
  1144. if (!sx_initialized)
  1145. return IRQ_HANDLED;
  1146. if (!(board->flags & SX_BOARD_INITIALIZED))
  1147. return IRQ_HANDLED;
  1148. if (test_and_set_bit(SX_BOARD_INTR_LOCK, &board->locks)) {
  1149. printk(KERN_ERR "Recursive interrupt! (%d)\n", board->irq);
  1150. return IRQ_HANDLED;
  1151. }
  1152. for (i = 0; i < board->nports; i++) {
  1153. port = &board->ports[i];
  1154. if (port->gs.port.flags & GS_ACTIVE) {
  1155. if (sx_read_channel_byte(port, hi_state)) {
  1156. sx_dprintk(SX_DEBUG_INTERRUPTS, "Port %d: "
  1157. "modem signal change?... \n",i);
  1158. sx_check_modem_signals(port);
  1159. }
  1160. if (port->gs.xmit_cnt) {
  1161. sx_transmit_chars(port);
  1162. }
  1163. if (!(port->gs.port.flags & SX_RX_THROTTLE)) {
  1164. sx_receive_chars(port);
  1165. }
  1166. }
  1167. }
  1168. clear_bit(SX_BOARD_INTR_LOCK, &board->locks);
  1169. sx_dprintk(SX_DEBUG_FLOW, "sx: exit sx_interrupt (%d/%d)\n", irq,
  1170. board->irq);
  1171. func_exit();
  1172. return IRQ_HANDLED;
  1173. }
  1174. static void sx_pollfunc(unsigned long data)
  1175. {
  1176. struct sx_board *board = (struct sx_board *)data;
  1177. func_enter();
  1178. sx_interrupt(0, board);
  1179. mod_timer(&board->timer, jiffies + sx_poll);
  1180. func_exit();
  1181. }
  1182. /* ********************************************************************** *
  1183. * Here are the routines that actually *
  1184. * interface with the generic_serial driver *
  1185. * ********************************************************************** */
  1186. /* Ehhm. I don't know how to fiddle with interrupts on the SX card. --REW */
  1187. /* Hmm. Ok I figured it out. You don't. */
  1188. static void sx_disable_tx_interrupts(void *ptr)
  1189. {
  1190. struct sx_port *port = ptr;
  1191. func_enter2();
  1192. port->gs.port.flags &= ~GS_TX_INTEN;
  1193. func_exit();
  1194. }
  1195. static void sx_enable_tx_interrupts(void *ptr)
  1196. {
  1197. struct sx_port *port = ptr;
  1198. int data_in_buffer;
  1199. func_enter2();
  1200. /* First transmit the characters that we're supposed to */
  1201. sx_transmit_chars(port);
  1202. /* The sx card will never interrupt us if we don't fill the buffer
  1203. past 25%. So we keep considering interrupts off if that's the case. */
  1204. data_in_buffer = (sx_read_channel_byte(port, hi_txipos) -
  1205. sx_read_channel_byte(port, hi_txopos)) & 0xff;
  1206. /* XXX Must be "HIGH_WATER" for SI card according to doc. */
  1207. if (data_in_buffer < LOW_WATER)
  1208. port->gs.port.flags &= ~GS_TX_INTEN;
  1209. func_exit();
  1210. }
  1211. static void sx_disable_rx_interrupts(void *ptr)
  1212. {
  1213. /* struct sx_port *port = ptr; */
  1214. func_enter();
  1215. func_exit();
  1216. }
  1217. static void sx_enable_rx_interrupts(void *ptr)
  1218. {
  1219. /* struct sx_port *port = ptr; */
  1220. func_enter();
  1221. func_exit();
  1222. }
  1223. /* Jeez. Isn't this simple? */
  1224. static int sx_carrier_raised(struct tty_port *port)
  1225. {
  1226. struct sx_port *sp = container_of(port, struct sx_port, gs.port);
  1227. return ((sx_read_channel_byte(sp, hi_ip) & IP_DCD) != 0);
  1228. }
  1229. /* Jeez. Isn't this simple? */
  1230. static int sx_chars_in_buffer(void *ptr)
  1231. {
  1232. struct sx_port *port = ptr;
  1233. func_enter2();
  1234. func_exit();
  1235. return ((sx_read_channel_byte(port, hi_txipos) -
  1236. sx_read_channel_byte(port, hi_txopos)) & 0xff);
  1237. }
  1238. static void sx_shutdown_port(void *ptr)
  1239. {
  1240. struct sx_port *port = ptr;
  1241. func_enter();
  1242. port->gs.port.flags &= ~GS_ACTIVE;
  1243. if (port->gs.port.tty && (port->gs.port.tty->termios->c_cflag & HUPCL)) {
  1244. sx_setsignals(port, 0, 0);
  1245. sx_reconfigure_port(port);
  1246. }
  1247. func_exit();
  1248. }
  1249. /* ********************************************************************** *
  1250. * Here are the routines that actually *
  1251. * interface with the rest of the system *
  1252. * ********************************************************************** */
  1253. static int sx_open(struct tty_struct *tty, struct file *filp)
  1254. {
  1255. struct sx_port *port;
  1256. int retval, line;
  1257. unsigned long flags;
  1258. func_enter();
  1259. if (!sx_initialized) {
  1260. return -EIO;
  1261. }
  1262. line = tty->index;
  1263. sx_dprintk(SX_DEBUG_OPEN, "%d: opening line %d. tty=%p ctty=%p, "
  1264. "np=%d)\n", task_pid_nr(current), line, tty,
  1265. current->signal->tty, sx_nports);
  1266. if ((line < 0) || (line >= SX_NPORTS) || (line >= sx_nports))
  1267. return -ENODEV;
  1268. port = &sx_ports[line];
  1269. port->c_dcd = 0; /* Make sure that the first interrupt doesn't detect a
  1270. 1 -> 0 transition. */
  1271. sx_dprintk(SX_DEBUG_OPEN, "port = %p c_dcd = %d\n", port, port->c_dcd);
  1272. spin_lock_irqsave(&port->gs.driver_lock, flags);
  1273. tty->driver_data = port;
  1274. port->gs.port.tty = tty;
  1275. port->gs.port.count++;
  1276. spin_unlock_irqrestore(&port->gs.driver_lock, flags);
  1277. sx_dprintk(SX_DEBUG_OPEN, "starting port\n");
  1278. /*
  1279. * Start up serial port
  1280. */
  1281. retval = gs_init_port(&port->gs);
  1282. sx_dprintk(SX_DEBUG_OPEN, "done gs_init\n");
  1283. if (retval) {
  1284. port->gs.port.count--;
  1285. return retval;
  1286. }
  1287. port->gs.port.flags |= GS_ACTIVE;
  1288. if (port->gs.port.count <= 1)
  1289. sx_setsignals(port, 1, 1);
  1290. #if 0
  1291. if (sx_debug & SX_DEBUG_OPEN)
  1292. my_hd(port, sizeof(*port));
  1293. #else
  1294. if (sx_debug & SX_DEBUG_OPEN)
  1295. my_hd_io(port->board->base + port->ch_base, sizeof(*port));
  1296. #endif
  1297. if (port->gs.port.count <= 1) {
  1298. if (sx_send_command(port, HS_LOPEN, -1, HS_IDLE_OPEN) != 1) {
  1299. printk(KERN_ERR "sx: Card didn't respond to LOPEN "
  1300. "command.\n");
  1301. spin_lock_irqsave(&port->gs.driver_lock, flags);
  1302. port->gs.port.count--;
  1303. spin_unlock_irqrestore(&port->gs.driver_lock, flags);
  1304. return -EIO;
  1305. }
  1306. }
  1307. retval = gs_block_til_ready(port, filp);
  1308. sx_dprintk(SX_DEBUG_OPEN, "Block til ready returned %d. Count=%d\n",
  1309. retval, port->gs.port.count);
  1310. if (retval) {
  1311. /*
  1312. * Don't lower gs.port.count here because sx_close() will be called later
  1313. */
  1314. return retval;
  1315. }
  1316. /* tty->low_latency = 1; */
  1317. port->c_dcd = sx_carrier_raised(&port->gs.port);
  1318. sx_dprintk(SX_DEBUG_OPEN, "at open: cd=%d\n", port->c_dcd);
  1319. func_exit();
  1320. return 0;
  1321. }
  1322. static void sx_close(void *ptr)
  1323. {
  1324. struct sx_port *port = ptr;
  1325. /* Give the port 5 seconds to close down. */
  1326. int to = 5 * HZ;
  1327. func_enter();
  1328. sx_setsignals(port, 0, 0);
  1329. sx_reconfigure_port(port);
  1330. sx_send_command(port, HS_CLOSE, 0, 0);
  1331. while (to-- && (sx_read_channel_byte(port, hi_hstat) != HS_IDLE_CLOSED))
  1332. if (msleep_interruptible(10))
  1333. break;
  1334. if (sx_read_channel_byte(port, hi_hstat) != HS_IDLE_CLOSED) {
  1335. if (sx_send_command(port, HS_FORCE_CLOSED, -1, HS_IDLE_CLOSED)
  1336. != 1) {
  1337. printk(KERN_ERR "sx: sent the force_close command, but "
  1338. "card didn't react\n");
  1339. } else
  1340. sx_dprintk(SX_DEBUG_CLOSE, "sent the force_close "
  1341. "command.\n");
  1342. }
  1343. sx_dprintk(SX_DEBUG_CLOSE, "waited %d jiffies for close. count=%d\n",
  1344. 5 * HZ - to - 1, port->gs.port.count);
  1345. if (port->gs.port.count) {
  1346. sx_dprintk(SX_DEBUG_CLOSE, "WARNING port count:%d\n",
  1347. port->gs.port.count);
  1348. /*printk("%s SETTING port count to zero: %p count: %d\n",
  1349. __func__, port, port->gs.port.count);
  1350. port->gs.port.count = 0;*/
  1351. }
  1352. func_exit();
  1353. }
  1354. /* This is relatively thorough. But then again it is only 20 lines. */
  1355. #define MARCHUP for (i = min; i < max; i++)
  1356. #define MARCHDOWN for (i = max - 1; i >= min; i--)
  1357. #define W0 write_sx_byte(board, i, 0x55)
  1358. #define W1 write_sx_byte(board, i, 0xaa)
  1359. #define R0 if (read_sx_byte(board, i) != 0x55) return 1
  1360. #define R1 if (read_sx_byte(board, i) != 0xaa) return 1
  1361. /* This memtest takes a human-noticable time. You normally only do it
  1362. once a boot, so I guess that it is worth it. */
  1363. static int do_memtest(struct sx_board *board, int min, int max)
  1364. {
  1365. int i;
  1366. /* This is a marchb. Theoretically, marchb catches much more than
  1367. simpler tests. In practise, the longer test just catches more
  1368. intermittent errors. -- REW
  1369. (For the theory behind memory testing see:
  1370. Testing Semiconductor Memories by A.J. van de Goor.) */
  1371. MARCHUP {
  1372. W0;
  1373. }
  1374. MARCHUP {
  1375. R0;
  1376. W1;
  1377. R1;
  1378. W0;
  1379. R0;
  1380. W1;
  1381. }
  1382. MARCHUP {
  1383. R1;
  1384. W0;
  1385. W1;
  1386. }
  1387. MARCHDOWN {
  1388. R1;
  1389. W0;
  1390. W1;
  1391. W0;
  1392. }
  1393. MARCHDOWN {
  1394. R0;
  1395. W1;
  1396. W0;
  1397. }
  1398. return 0;
  1399. }
  1400. #undef MARCHUP
  1401. #undef MARCHDOWN
  1402. #undef W0
  1403. #undef W1
  1404. #undef R0
  1405. #undef R1
  1406. #define MARCHUP for (i = min; i < max; i += 2)
  1407. #define MARCHDOWN for (i = max - 1; i >= min; i -= 2)
  1408. #define W0 write_sx_word(board, i, 0x55aa)
  1409. #define W1 write_sx_word(board, i, 0xaa55)
  1410. #define R0 if (read_sx_word(board, i) != 0x55aa) return 1
  1411. #define R1 if (read_sx_word(board, i) != 0xaa55) return 1
  1412. #if 0
  1413. /* This memtest takes a human-noticable time. You normally only do it
  1414. once a boot, so I guess that it is worth it. */
  1415. static int do_memtest_w(struct sx_board *board, int min, int max)
  1416. {
  1417. int i;
  1418. MARCHUP {
  1419. W0;
  1420. }
  1421. MARCHUP {
  1422. R0;
  1423. W1;
  1424. R1;
  1425. W0;
  1426. R0;
  1427. W1;
  1428. }
  1429. MARCHUP {
  1430. R1;
  1431. W0;
  1432. W1;
  1433. }
  1434. MARCHDOWN {
  1435. R1;
  1436. W0;
  1437. W1;
  1438. W0;
  1439. }
  1440. MARCHDOWN {
  1441. R0;
  1442. W1;
  1443. W0;
  1444. }
  1445. return 0;
  1446. }
  1447. #endif
  1448. static long sx_fw_ioctl(struct file *filp, unsigned int cmd,
  1449. unsigned long arg)
  1450. {
  1451. long rc = 0;
  1452. int __user *descr = (int __user *)arg;
  1453. int i;
  1454. static struct sx_board *board = NULL;
  1455. int nbytes, offset;
  1456. unsigned long data;
  1457. char *tmp;
  1458. func_enter();
  1459. if (!capable(CAP_SYS_RAWIO))
  1460. return -EPERM;
  1461. lock_kernel();
  1462. sx_dprintk(SX_DEBUG_FIRMWARE, "IOCTL %x: %lx\n", cmd, arg);
  1463. if (!board)
  1464. board = &boards[0];
  1465. if (board->flags & SX_BOARD_PRESENT) {
  1466. sx_dprintk(SX_DEBUG_FIRMWARE, "Board present! (%x)\n",
  1467. board->flags);
  1468. } else {
  1469. sx_dprintk(SX_DEBUG_FIRMWARE, "Board not present! (%x) all:",
  1470. board->flags);
  1471. for (i = 0; i < SX_NBOARDS; i++)
  1472. sx_dprintk(SX_DEBUG_FIRMWARE, "<%x> ", boards[i].flags);
  1473. sx_dprintk(SX_DEBUG_FIRMWARE, "\n");
  1474. rc = -EIO;
  1475. goto out;
  1476. }
  1477. switch (cmd) {
  1478. case SXIO_SET_BOARD:
  1479. sx_dprintk(SX_DEBUG_FIRMWARE, "set board to %ld\n", arg);
  1480. rc = -EIO;
  1481. if (arg >= SX_NBOARDS)
  1482. break;
  1483. sx_dprintk(SX_DEBUG_FIRMWARE, "not out of range\n");
  1484. if (!(boards[arg].flags & SX_BOARD_PRESENT))
  1485. break;
  1486. sx_dprintk(SX_DEBUG_FIRMWARE, ".. and present!\n");
  1487. board = &boards[arg];
  1488. rc = 0;
  1489. /* FIXME: And this does ... nothing?? */
  1490. break;
  1491. case SXIO_GET_TYPE:
  1492. rc = -ENOENT; /* If we manage to miss one, return error. */
  1493. if (IS_SX_BOARD(board))
  1494. rc = SX_TYPE_SX;
  1495. if (IS_CF_BOARD(board))
  1496. rc = SX_TYPE_CF;
  1497. if (IS_SI_BOARD(board))
  1498. rc = SX_TYPE_SI;
  1499. if (IS_SI1_BOARD(board))
  1500. rc = SX_TYPE_SI;
  1501. if (IS_EISA_BOARD(board))
  1502. rc = SX_TYPE_SI;
  1503. sx_dprintk(SX_DEBUG_FIRMWARE, "returning type= %ld\n", rc);
  1504. break;
  1505. case SXIO_DO_RAMTEST:
  1506. if (sx_initialized) { /* Already initialized: better not ramtest the board. */
  1507. rc = -EPERM;
  1508. break;
  1509. }
  1510. if (IS_SX_BOARD(board)) {
  1511. rc = do_memtest(board, 0, 0x7000);
  1512. if (!rc)
  1513. rc = do_memtest(board, 0, 0x7000);
  1514. /*if (!rc) rc = do_memtest_w (board, 0, 0x7000); */
  1515. } else {
  1516. rc = do_memtest(board, 0, 0x7ff8);
  1517. /* if (!rc) rc = do_memtest_w (board, 0, 0x7ff8); */
  1518. }
  1519. sx_dprintk(SX_DEBUG_FIRMWARE,
  1520. "returning memtest result= %ld\n", rc);
  1521. break;
  1522. case SXIO_DOWNLOAD:
  1523. if (sx_initialized) {/* Already initialized */
  1524. rc = -EEXIST;
  1525. break;
  1526. }
  1527. if (!sx_reset(board)) {
  1528. rc = -EIO;
  1529. break;
  1530. }
  1531. sx_dprintk(SX_DEBUG_INIT, "reset the board...\n");
  1532. tmp = kmalloc(SX_CHUNK_SIZE, GFP_USER);
  1533. if (!tmp) {
  1534. rc = -ENOMEM;
  1535. break;
  1536. }
  1537. /* FIXME: check returns */
  1538. get_user(nbytes, descr++);
  1539. get_user(offset, descr++);
  1540. get_user(data, descr++);
  1541. while (nbytes && data) {
  1542. for (i = 0; i < nbytes; i += SX_CHUNK_SIZE) {
  1543. if (copy_from_user(tmp, (char __user *)data + i,
  1544. (i + SX_CHUNK_SIZE > nbytes) ?
  1545. nbytes - i : SX_CHUNK_SIZE)) {
  1546. kfree(tmp);
  1547. rc = -EFAULT;
  1548. goto out;
  1549. }
  1550. memcpy_toio(board->base2 + offset + i, tmp,
  1551. (i + SX_CHUNK_SIZE > nbytes) ?
  1552. nbytes - i : SX_CHUNK_SIZE);
  1553. }
  1554. get_user(nbytes, descr++);
  1555. get_user(offset, descr++);
  1556. get_user(data, descr++);
  1557. }
  1558. kfree(tmp);
  1559. sx_nports += sx_init_board(board);
  1560. rc = sx_nports;
  1561. break;
  1562. case SXIO_INIT:
  1563. if (sx_initialized) { /* Already initialized */
  1564. rc = -EEXIST;
  1565. break;
  1566. }
  1567. /* This is not allowed until all boards are initialized... */
  1568. for (i = 0; i < SX_NBOARDS; i++) {
  1569. if ((boards[i].flags & SX_BOARD_PRESENT) &&
  1570. !(boards[i].flags & SX_BOARD_INITIALIZED)) {
  1571. rc = -EIO;
  1572. break;
  1573. }
  1574. }
  1575. for (i = 0; i < SX_NBOARDS; i++)
  1576. if (!(boards[i].flags & SX_BOARD_PRESENT))
  1577. break;
  1578. sx_dprintk(SX_DEBUG_FIRMWARE, "initing portstructs, %d boards, "
  1579. "%d channels, first board: %d ports\n",
  1580. i, sx_nports, boards[0].nports);
  1581. rc = sx_init_portstructs(i, sx_nports);
  1582. sx_init_drivers();
  1583. if (rc >= 0)
  1584. sx_initialized++;
  1585. break;
  1586. case SXIO_SETDEBUG:
  1587. sx_debug = arg;
  1588. break;
  1589. case SXIO_GETDEBUG:
  1590. rc = sx_debug;
  1591. break;
  1592. case SXIO_GETGSDEBUG:
  1593. case SXIO_SETGSDEBUG:
  1594. rc = -EINVAL;
  1595. break;
  1596. case SXIO_GETNPORTS:
  1597. rc = sx_nports;
  1598. break;
  1599. default:
  1600. rc = -ENOTTY;
  1601. break;
  1602. }
  1603. out:
  1604. unlock_kernel();
  1605. func_exit();
  1606. return rc;
  1607. }
  1608. static int sx_break(struct tty_struct *tty, int flag)
  1609. {
  1610. s