PageRenderTime 68ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/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
Possible License(s): LGPL-2.0, AGPL-1.0, GPL-2.0
  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. struct sx_port *port = tty->driver_data;
  1611. int rv;
  1612. func_enter();
  1613. lock_kernel();
  1614. if (flag)
  1615. rv = sx_send_command(port, HS_START, -1, HS_IDLE_BREAK);
  1616. else
  1617. rv = sx_send_command(port, HS_STOP, -1, HS_IDLE_OPEN);
  1618. if (rv != 1)
  1619. printk(KERN_ERR "sx: couldn't send break (%x).\n",
  1620. read_sx_byte(port->board, CHAN_OFFSET(port, hi_hstat)));
  1621. unlock_kernel();
  1622. func_exit();
  1623. return 0;
  1624. }
  1625. static int sx_tiocmget(struct tty_struct *tty, struct file *file)
  1626. {
  1627. struct sx_port *port = tty->driver_data;
  1628. return sx_getsignals(port);
  1629. }
  1630. static int sx_tiocmset(struct tty_struct *tty, struct file *file,
  1631. unsigned int set, unsigned int clear)
  1632. {
  1633. struct sx_port *port = tty->driver_data;
  1634. int rts = -1, dtr = -1;
  1635. if (set & TIOCM_RTS)
  1636. rts = 1;
  1637. if (set & TIOCM_DTR)
  1638. dtr = 1;
  1639. if (clear & TIOCM_RTS)
  1640. rts = 0;
  1641. if (clear & TIOCM_DTR)
  1642. dtr = 0;
  1643. sx_setsignals(port, dtr, rts);
  1644. sx_reconfigure_port(port);
  1645. return 0;
  1646. }
  1647. static int sx_ioctl(struct tty_struct *tty, struct file *filp,
  1648. unsigned int cmd, unsigned long arg)
  1649. {
  1650. int rc;
  1651. struct sx_port *port = tty->driver_data;
  1652. void __user *argp = (void __user *)arg;
  1653. /* func_enter2(); */
  1654. rc = 0;
  1655. lock_kernel();
  1656. switch (cmd) {
  1657. case TIOCGSERIAL:
  1658. rc = gs_getserial(&port->gs, argp);
  1659. break;
  1660. case TIOCSSERIAL:
  1661. rc = gs_setserial(&port->gs, argp);
  1662. break;
  1663. default:
  1664. rc = -ENOIOCTLCMD;
  1665. break;
  1666. }
  1667. unlock_kernel();
  1668. /* func_exit(); */
  1669. return rc;
  1670. }
  1671. /* The throttle/unthrottle scheme for the Specialix card is different
  1672. * from other drivers and deserves some explanation.
  1673. * The Specialix hardware takes care of XON/XOFF
  1674. * and CTS/RTS flow control itself. This means that all we have to
  1675. * do when signalled by the upper tty layer to throttle/unthrottle is
  1676. * to make a note of it here. When we come to read characters from the
  1677. * rx buffers on the card (sx_receive_chars()) we look to see if the
  1678. * upper layer can accept more (as noted here in sx_rx_throt[]).
  1679. * If it can't we simply don't remove chars from the cards buffer.
  1680. * When the tty layer can accept chars, we again note that here and when
  1681. * sx_receive_chars() is called it will remove them from the cards buffer.
  1682. * The card will notice that a ports buffer has drained below some low
  1683. * water mark and will unflow control the line itself, using whatever
  1684. * flow control scheme is in use for that port. -- Simon Allen
  1685. */
  1686. static void sx_throttle(struct tty_struct *tty)
  1687. {
  1688. struct sx_port *port = tty->driver_data;
  1689. func_enter2();
  1690. /* If the port is using any type of input flow
  1691. * control then throttle the port.
  1692. */
  1693. if ((tty->termios->c_cflag & CRTSCTS) || (I_IXOFF(tty))) {
  1694. port->gs.port.flags |= SX_RX_THROTTLE;
  1695. }
  1696. func_exit();
  1697. }
  1698. static void sx_unthrottle(struct tty_struct *tty)
  1699. {
  1700. struct sx_port *port = tty->driver_data;
  1701. func_enter2();
  1702. /* Always unthrottle even if flow control is not enabled on
  1703. * this port in case we disabled flow control while the port
  1704. * was throttled
  1705. */
  1706. port->gs.port.flags &= ~SX_RX_THROTTLE;
  1707. func_exit();
  1708. return;
  1709. }
  1710. /* ********************************************************************** *
  1711. * Here are the initialization routines. *
  1712. * ********************************************************************** */
  1713. static int sx_init_board(struct sx_board *board)
  1714. {
  1715. int addr;
  1716. int chans;
  1717. int type;
  1718. func_enter();
  1719. /* This is preceded by downloading the download code. */
  1720. board->flags |= SX_BOARD_INITIALIZED;
  1721. if (read_sx_byte(board, 0))
  1722. /* CF boards may need this. */
  1723. write_sx_byte(board, 0, 0);
  1724. /* This resets the processor again, to make sure it didn't do any
  1725. foolish things while we were downloading the image */
  1726. if (!sx_reset(board))
  1727. return 0;
  1728. sx_start_board(board);
  1729. udelay(10);
  1730. if (!sx_busy_wait_neq(board, 0, 0xff, 0)) {
  1731. printk(KERN_ERR "sx: Ooops. Board won't initialize.\n");
  1732. return 0;
  1733. }
  1734. /* Ok. So now the processor on the card is running. It gathered
  1735. some info for us... */
  1736. sx_dprintk(SX_DEBUG_INIT, "The sxcard structure:\n");
  1737. if (sx_debug & SX_DEBUG_INIT)
  1738. my_hd_io(board->base, 0x10);
  1739. sx_dprintk(SX_DEBUG_INIT, "the first sx_module structure:\n");
  1740. if (sx_debug & SX_DEBUG_INIT)
  1741. my_hd_io(board->base + 0x80, 0x30);
  1742. sx_dprintk(SX_DEBUG_INIT, "init_status: %x, %dk memory, firmware "
  1743. "V%x.%02x,\n",
  1744. read_sx_byte(board, 0), read_sx_byte(board, 1),
  1745. read_sx_byte(board, 5), read_sx_byte(board, 4));
  1746. if (read_sx_byte(board, 0) == 0xff) {
  1747. printk(KERN_INFO "sx: No modules found. Sorry.\n");
  1748. board->nports = 0;
  1749. return 0;
  1750. }
  1751. chans = 0;
  1752. if (IS_SX_BOARD(board)) {
  1753. sx_write_board_word(board, cc_int_count, sx_maxints);
  1754. } else {
  1755. if (sx_maxints)
  1756. sx_write_board_word(board, cc_int_count,
  1757. SI_PROCESSOR_CLOCK / 8 / sx_maxints);
  1758. }
  1759. /* grab the first module type... */
  1760. /* board->ta_type = mod_compat_type (read_sx_byte (board, 0x80 + 0x08)); */
  1761. board->ta_type = mod_compat_type(sx_read_module_byte(board, 0x80,
  1762. mc_chip));
  1763. /* XXX byteorder */
  1764. for (addr = 0x80; addr != 0; addr = read_sx_word(board, addr) & 0x7fff){
  1765. type = sx_read_module_byte(board, addr, mc_chip);
  1766. sx_dprintk(SX_DEBUG_INIT, "Module at %x: %d channels\n",
  1767. addr, read_sx_byte(board, addr + 2));
  1768. chans += sx_read_module_byte(board, addr, mc_type);
  1769. sx_dprintk(SX_DEBUG_INIT, "module is an %s, which has %s/%s "
  1770. "panels\n",
  1771. mod_type_s(type),
  1772. pan_type_s(sx_read_module_byte(board, addr,
  1773. mc_mods) & 0xf),
  1774. pan_type_s(sx_read_module_byte(board, addr,
  1775. mc_mods) >> 4));
  1776. sx_dprintk(SX_DEBUG_INIT, "CD1400 versions: %x/%x, ASIC "
  1777. "version: %x\n",
  1778. sx_read_module_byte(board, addr, mc_rev1),
  1779. sx_read_module_byte(board, addr, mc_rev2),
  1780. sx_read_module_byte(board, addr, mc_mtaasic_rev));
  1781. /* The following combinations are illegal: It should theoretically
  1782. work, but timing problems make the bus HANG. */
  1783. if (mod_compat_type(type) != board->ta_type) {
  1784. printk(KERN_ERR "sx: This is an invalid "
  1785. "configuration.\nDon't mix TA/MTA/SXDC on the "
  1786. "same hostadapter.\n");
  1787. chans = 0;
  1788. break;
  1789. }
  1790. if ((IS_EISA_BOARD(board) ||
  1791. IS_SI_BOARD(board)) &&
  1792. (mod_compat_type(type) == 4)) {
  1793. printk(KERN_ERR "sx: This is an invalid "
  1794. "configuration.\nDon't use SXDCs on an SI/XIO "
  1795. "adapter.\n");
  1796. chans = 0;
  1797. break;
  1798. }
  1799. #if 0 /* Problem fixed: firmware 3.05 */
  1800. if (IS_SX_BOARD(board) && (type == TA8)) {
  1801. /* There are some issues with the firmware and the DCD/RTS
  1802. lines. It might work if you tie them together or something.
  1803. It might also work if you get a newer sx_firmware. Therefore
  1804. this is just a warning. */
  1805. printk(KERN_WARNING
  1806. "sx: The SX host doesn't work too well "
  1807. "with the TA8 adapters.\nSpecialix is working on it.\n");
  1808. }
  1809. #endif
  1810. }
  1811. if (chans) {
  1812. if (board->irq > 0) {
  1813. /* fixed irq, probably PCI */
  1814. if (sx_irqmask & (1 << board->irq)) { /* may we use this irq? */
  1815. if (request_irq(board->irq, sx_interrupt,
  1816. IRQF_SHARED | IRQF_DISABLED,
  1817. "sx", board)) {
  1818. printk(KERN_ERR "sx: Cannot allocate "
  1819. "irq %d.\n", board->irq);
  1820. board->irq = 0;
  1821. }
  1822. } else
  1823. board->irq = 0;
  1824. } else if (board->irq < 0 && sx_irqmask) {
  1825. /* auto-allocate irq */
  1826. int irqnr;
  1827. int irqmask = sx_irqmask & (IS_SX_BOARD(board) ?
  1828. SX_ISA_IRQ_MASK : SI2_ISA_IRQ_MASK);
  1829. for (irqnr = 15; irqnr > 0; irqnr--)
  1830. if (irqmask & (1 << irqnr))
  1831. if (!request_irq(irqnr, sx_interrupt,
  1832. IRQF_SHARED | IRQF_DISABLED,
  1833. "sx", board))
  1834. break;
  1835. if (!irqnr)
  1836. printk(KERN_ERR "sx: Cannot allocate IRQ.\n");
  1837. board->irq = irqnr;
  1838. } else
  1839. board->irq = 0;
  1840. if (board->irq) {
  1841. /* Found a valid interrupt, start up interrupts! */
  1842. sx_dprintk(SX_DEBUG_INIT, "Using irq %d.\n",
  1843. board->irq);
  1844. sx_start_interrupts(board);
  1845. board->poll = sx_slowpoll;
  1846. board->flags |= SX_IRQ_ALLOCATED;
  1847. } else {
  1848. /* no irq: setup board for polled operation */
  1849. board->poll = sx_poll;
  1850. sx_dprintk(SX_DEBUG_INIT, "Using poll-interval %d.\n",
  1851. board->poll);
  1852. }
  1853. /* The timer should be initialized anyway: That way we can
  1854. safely del_timer it when the module is unloaded. */
  1855. setup_timer(&board->timer, sx_pollfunc, (unsigned long)board);
  1856. if (board->poll)
  1857. mod_timer(&board->timer, jiffies + board->poll);
  1858. } else {
  1859. board->irq = 0;
  1860. }
  1861. board->nports = chans;
  1862. sx_dprintk(SX_DEBUG_INIT, "returning %d ports.", board->nports);
  1863. func_exit();
  1864. return chans;
  1865. }
  1866. static void __devinit printheader(void)
  1867. {
  1868. static int header_printed;
  1869. if (!header_printed) {
  1870. printk(KERN_INFO "Specialix SX driver "
  1871. "(C) 1998/1999 R.E.Wolff@BitWizard.nl\n");
  1872. printk(KERN_INFO "sx: version " __stringify(SX_VERSION) "\n");
  1873. header_printed = 1;
  1874. }
  1875. }
  1876. static int __devinit probe_sx(struct sx_board *board)
  1877. {
  1878. struct vpd_prom vpdp;
  1879. char *p;
  1880. int i;
  1881. func_enter();
  1882. if (!IS_CF_BOARD(board)) {
  1883. sx_dprintk(SX_DEBUG_PROBE, "Going to verify vpd prom at %p.\n",
  1884. board->base + SX_VPD_ROM);
  1885. if (sx_debug & SX_DEBUG_PROBE)
  1886. my_hd_io(board->base + SX_VPD_ROM, 0x40);
  1887. p = (char *)&vpdp;
  1888. for (i = 0; i < sizeof(struct vpd_prom); i++)
  1889. *p++ = read_sx_byte(board, SX_VPD_ROM + i * 2);
  1890. if (sx_debug & SX_DEBUG_PROBE)
  1891. my_hd(&vpdp, 0x20);
  1892. sx_dprintk(SX_DEBUG_PROBE, "checking identifier...\n");
  1893. if (strncmp(vpdp.identifier, SX_VPD_IDENT_STRING, 16) != 0) {
  1894. sx_dprintk(SX_DEBUG_PROBE, "Got non-SX identifier: "
  1895. "'%s'\n", vpdp.identifier);
  1896. return 0;
  1897. }
  1898. }
  1899. printheader();
  1900. if (!IS_CF_BOARD(board)) {
  1901. printk(KERN_DEBUG "sx: Found an SX board at %lx\n",
  1902. board->hw_base);
  1903. printk(KERN_DEBUG "sx: hw_rev: %d, assembly level: %d, "
  1904. "uniq ID:%08x, ",
  1905. vpdp.hwrev, vpdp.hwass, vpdp.uniqid);
  1906. printk("Manufactured: %d/%d\n", 1970 + vpdp.myear, vpdp.mweek);
  1907. if ((((vpdp.uniqid >> 24) & SX_UNIQUEID_MASK) !=
  1908. SX_PCI_UNIQUEID1) && (((vpdp.uniqid >> 24) &
  1909. SX_UNIQUEID_MASK) != SX_ISA_UNIQUEID1)) {
  1910. /* This might be a bit harsh. This was the primary
  1911. reason the SX/ISA card didn't work at first... */
  1912. printk(KERN_ERR "sx: Hmm. Not an SX/PCI or SX/ISA "
  1913. "card. Sorry: giving up.\n");
  1914. return (0);
  1915. }
  1916. if (((vpdp.uniqid >> 24) & SX_UNIQUEID_MASK) ==
  1917. SX_ISA_UNIQUEID1) {
  1918. if (((unsigned long)board->hw_base) & 0x8000) {
  1919. printk(KERN_WARNING "sx: Warning: There may be "
  1920. "hardware problems with the card at "
  1921. "%lx.\n", board->hw_base);
  1922. printk(KERN_WARNING "sx: Read sx.txt for more "
  1923. "info.\n");
  1924. }
  1925. }
  1926. }
  1927. board->nports = -1;
  1928. /* This resets the processor, and keeps it off the bus. */
  1929. if (!sx_reset(board))
  1930. return 0;
  1931. sx_dprintk(SX_DEBUG_INIT, "reset the board...\n");
  1932. func_exit();
  1933. return 1;
  1934. }
  1935. #if defined(CONFIG_ISA) || defined(CONFIG_EISA)
  1936. /* Specialix probes for this card at 32k increments from 640k to 16M.
  1937. I consider machines with less than 16M unlikely nowadays, so I'm
  1938. not probing above 1Mb. Also, 0xa0000, 0xb0000, are taken by the VGA
  1939. card. 0xe0000 and 0xf0000 are taken by the BIOS. That only leaves
  1940. 0xc0000, 0xc8000, 0xd0000 and 0xd8000 . */
  1941. static int __devinit probe_si(struct sx_board *board)
  1942. {
  1943. int i;
  1944. func_enter();
  1945. sx_dprintk(SX_DEBUG_PROBE, "Going to verify SI signature hw %lx at "
  1946. "%p.\n", board->hw_base, board->base + SI2_ISA_ID_BASE);
  1947. if (sx_debug & SX_DEBUG_PROBE)
  1948. my_hd_io(board->base + SI2_ISA_ID_BASE, 0x8);
  1949. if (!IS_EISA_BOARD(board)) {
  1950. if (IS_SI1_BOARD(board)) {
  1951. for (i = 0; i < 8; i++) {
  1952. write_sx_byte(board, SI2_ISA_ID_BASE + 7 - i,i);
  1953. }
  1954. }
  1955. for (i = 0; i < 8; i++) {
  1956. if ((read_sx_byte(board, SI2_ISA_ID_BASE + 7 - i) & 7)
  1957. != i) {
  1958. func_exit();
  1959. return 0;
  1960. }
  1961. }
  1962. }
  1963. /* Now we're pretty much convinced that there is an SI board here,
  1964. but to prevent trouble, we'd better double check that we don't
  1965. have an SI1 board when we're probing for an SI2 board.... */
  1966. write_sx_byte(board, SI2_ISA_ID_BASE, 0x10);
  1967. if (IS_SI1_BOARD(board)) {
  1968. /* This should be an SI1 board, which has this
  1969. location writable... */
  1970. if (read_sx_byte(board, SI2_ISA_ID_BASE) != 0x10) {
  1971. func_exit();
  1972. return 0;
  1973. }
  1974. } else {
  1975. /* This should be an SI2 board, which has the bottom
  1976. 3 bits non-writable... */
  1977. if (read_sx_byte(board, SI2_ISA_ID_BASE) == 0x10) {
  1978. func_exit();
  1979. return 0;
  1980. }
  1981. }
  1982. /* Now we're pretty much convinced that there is an SI board here,
  1983. but to prevent trouble, we'd better double check that we don't
  1984. have an SI1 board when we're probing for an SI2 board.... */
  1985. write_sx_byte(board, SI2_ISA_ID_BASE, 0x10);
  1986. if (IS_SI1_BOARD(board)) {
  1987. /* This should be an SI1 board, which has this
  1988. location writable... */
  1989. if (read_sx_byte(board, SI2_ISA_ID_BASE) != 0x10) {
  1990. func_exit();
  1991. return 0;
  1992. }
  1993. } else {
  1994. /* This should be an SI2 board, which has the bottom
  1995. 3 bits non-writable... */
  1996. if (read_sx_byte(board, SI2_ISA_ID_BASE) == 0x10) {
  1997. func_exit();
  1998. return 0;
  1999. }
  2000. }
  2001. printheader();
  2002. printk(KERN_DEBUG "sx: Found an SI board at %lx\n", board->hw_base);
  2003. /* Compared to the SX boards, it is a complete guess as to what
  2004. this card is up to... */
  2005. board->nports = -1;
  2006. /* This resets the processor, and keeps it off the bus. */
  2007. if (!sx_reset(board))
  2008. return 0;
  2009. sx_dprintk(SX_DEBUG_INIT, "reset the board...\n");
  2010. func_exit();
  2011. return 1;
  2012. }
  2013. #endif
  2014. static const struct tty_operations sx_ops = {
  2015. .break_ctl = sx_break,
  2016. .open = sx_open,
  2017. .close = gs_close,
  2018. .write = gs_write,
  2019. .put_char = gs_put_char,
  2020. .flush_chars = gs_flush_chars,
  2021. .write_room = gs_write_room,
  2022. .chars_in_buffer = gs_chars_in_buffer,
  2023. .flush_buffer = gs_flush_buffer,
  2024. .ioctl = sx_ioctl,
  2025. .throttle = sx_throttle,
  2026. .unthrottle = sx_unthrottle,
  2027. .set_termios = gs_set_termios,
  2028. .stop = gs_stop,
  2029. .start = gs_start,
  2030. .hangup = gs_hangup,
  2031. .tiocmget = sx_tiocmget,
  2032. .tiocmset = sx_tiocmset,
  2033. };
  2034. static const struct tty_port_operations sx_port_ops = {
  2035. .carrier_raised = sx_carrier_raised,
  2036. };
  2037. static int sx_init_drivers(void)
  2038. {
  2039. int error;
  2040. func_enter();
  2041. sx_driver = alloc_tty_driver(sx_nports);
  2042. if (!sx_driver)
  2043. return 1;
  2044. sx_driver->owner = THIS_MODULE;
  2045. sx_driver->driver_name = "specialix_sx";
  2046. sx_driver->name = "ttyX";
  2047. sx_driver->major = SX_NORMAL_MAJOR;
  2048. sx_driver->type = TTY_DRIVER_TYPE_SERIAL;
  2049. sx_driver->subtype = SERIAL_TYPE_NORMAL;
  2050. sx_driver->init_termios = tty_std_termios;
  2051. sx_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  2052. sx_driver->init_termios.c_ispeed = 9600;
  2053. sx_driver->init_termios.c_ospeed = 9600;
  2054. sx_driver->flags = TTY_DRIVER_REAL_RAW;
  2055. tty_set_operations(sx_driver, &sx_ops);
  2056. if ((error = tty_register_driver(sx_driver))) {
  2057. put_tty_driver(sx_driver);
  2058. printk(KERN_ERR "sx: Couldn't register sx driver, error = %d\n",
  2059. error);
  2060. return 1;
  2061. }
  2062. func_exit();
  2063. return 0;
  2064. }
  2065. static int sx_init_portstructs(int nboards, int nports)
  2066. {
  2067. struct sx_board *board;
  2068. struct sx_port *port;
  2069. int i, j;
  2070. int addr, chans;
  2071. int portno;
  2072. func_enter();
  2073. /* Many drivers statically allocate the maximum number of ports
  2074. There is no reason not to allocate them dynamically.
  2075. Is there? -- REW */
  2076. sx_ports = kcalloc(nports, sizeof(struct sx_port), GFP_KERNEL);
  2077. if (!sx_ports)
  2078. return -ENOMEM;
  2079. port = sx_ports;
  2080. for (i = 0; i < nboards; i++) {
  2081. board = &boards[i];
  2082. board->ports = port;
  2083. for (j = 0; j < boards[i].nports; j++) {
  2084. sx_dprintk(SX_DEBUG_INIT, "initing port %d\n", j);
  2085. tty_port_init(&port->gs.port);
  2086. port->gs.port.ops = &sx_port_ops;
  2087. port->gs.magic = SX_MAGIC;
  2088. port->gs.close_delay = HZ / 2;
  2089. port->gs.closing_wait = 30 * HZ;
  2090. port->board = board;
  2091. port->gs.rd = &sx_real_driver;
  2092. #ifdef NEW_WRITE_LOCKING
  2093. port->gs.port_write_mutex = MUTEX;
  2094. #endif
  2095. spin_lock_init(&port->gs.driver_lock);
  2096. /*
  2097. * Initializing wait queue
  2098. */
  2099. port++;
  2100. }
  2101. }
  2102. port = sx_ports;
  2103. portno = 0;
  2104. for (i = 0; i < nboards; i++) {
  2105. board = &boards[i];
  2106. board->port_base = portno;
  2107. /* Possibly the configuration was rejected. */
  2108. sx_dprintk(SX_DEBUG_PROBE, "Board has %d channels\n",
  2109. board->nports);
  2110. if (board->nports <= 0)
  2111. continue;
  2112. /* XXX byteorder ?? */
  2113. for (addr = 0x80; addr != 0;
  2114. addr = read_sx_word(board, addr) & 0x7fff) {
  2115. chans = sx_read_module_byte(board, addr, mc_type);
  2116. sx_dprintk(SX_DEBUG_PROBE, "Module at %x: %d "
  2117. "channels\n", addr, chans);
  2118. sx_dprintk(SX_DEBUG_PROBE, "Port at");
  2119. for (j = 0; j < chans; j++) {
  2120. /* The "sx-way" is the way it SHOULD be done.
  2121. That way in the future, the firmware may for
  2122. example pack the structures a bit more
  2123. efficient. Neil tells me it isn't going to
  2124. happen anytime soon though. */
  2125. if (IS_SX_BOARD(board))
  2126. port->ch_base = sx_read_module_word(
  2127. board, addr + j * 2,
  2128. mc_chan_pointer);
  2129. else
  2130. port->ch_base = addr + 0x100 + 0x300 *j;
  2131. sx_dprintk(SX_DEBUG_PROBE, " %x",
  2132. port->ch_base);
  2133. port->line = portno++;
  2134. port++;
  2135. }
  2136. sx_dprintk(SX_DEBUG_PROBE, "\n");
  2137. }
  2138. /* This has to be done earlier. */
  2139. /* board->flags |= SX_BOARD_INITIALIZED; */
  2140. }
  2141. func_exit();
  2142. return 0;
  2143. }
  2144. static unsigned int sx_find_free_board(void)
  2145. {
  2146. unsigned int i;
  2147. for (i = 0; i < SX_NBOARDS; i++)
  2148. if (!(boards[i].flags & SX_BOARD_PRESENT))
  2149. break;
  2150. return i;
  2151. }
  2152. static void __exit sx_release_drivers(void)
  2153. {
  2154. func_enter();
  2155. tty_unregister_driver(sx_driver);
  2156. put_tty_driver(sx_driver);
  2157. func_exit();
  2158. }
  2159. static void __devexit sx_remove_card(struct sx_board *board,
  2160. struct pci_dev *pdev)
  2161. {
  2162. if (board->flags & SX_BOARD_INITIALIZED) {
  2163. /* The board should stop messing with us. (actually I mean the
  2164. interrupt) */
  2165. sx_reset(board);
  2166. if ((board->irq) && (board->flags & SX_IRQ_ALLOCATED))
  2167. free_irq(board->irq, board);
  2168. /* It is safe/allowed to del_timer a non-active timer */
  2169. del_timer(&board->timer);
  2170. if (pdev) {
  2171. #ifdef CONFIG_PCI
  2172. iounmap(board->base2);
  2173. pci_release_region(pdev, IS_CF_BOARD(board) ? 3 : 2);
  2174. #endif
  2175. } else {
  2176. iounmap(board->base);
  2177. release_region(board->hw_base, board->hw_len);
  2178. }
  2179. board->flags &= ~(SX_BOARD_INITIALIZED | SX_BOARD_PRESENT);
  2180. }
  2181. }
  2182. #ifdef CONFIG_EISA
  2183. static int __devinit sx_eisa_probe(struct device *dev)
  2184. {
  2185. struct eisa_device *edev = to_eisa_device(dev);
  2186. struct sx_board *board;
  2187. unsigned long eisa_slot = edev->base_addr;
  2188. unsigned int i;
  2189. int retval = -EIO;
  2190. mutex_lock(&sx_boards_lock);
  2191. i = sx_find_free_board();
  2192. if (i == SX_NBOARDS) {
  2193. mutex_unlock(&sx_boards_lock);
  2194. goto err;
  2195. }
  2196. board = &boards[i];
  2197. board->flags |= SX_BOARD_PRESENT;
  2198. mutex_unlock(&sx_boards_lock);
  2199. dev_info(dev, "XIO : Signature found in EISA slot %lu, "
  2200. "Product %d Rev %d (REPORT THIS TO LKLM)\n",
  2201. eisa_slot >> 12,
  2202. inb(eisa_slot + EISA_VENDOR_ID_OFFSET + 2),
  2203. inb(eisa_slot + EISA_VENDOR_ID_OFFSET + 3));
  2204. board->eisa_base = eisa_slot;
  2205. board->flags &= ~SX_BOARD_TYPE;
  2206. board->flags |= SI_EISA_BOARD;
  2207. board->hw_base = ((inb(eisa_slot + 0xc01) << 8) +
  2208. inb(eisa_slot + 0xc00)) << 16;
  2209. board->hw_len = SI2_EISA_WINDOW_LEN;
  2210. if (!request_region(board->hw_base, board->hw_len, "sx")) {
  2211. dev_err(dev, "can't request region\n");
  2212. goto err_flag;
  2213. }
  2214. board->base2 =
  2215. board->base = ioremap_nocache(board->hw_base, SI2_EISA_WINDOW_LEN);
  2216. if (!board->base) {
  2217. dev_err(dev, "can't remap memory\n");
  2218. goto err_reg;
  2219. }
  2220. sx_dprintk(SX_DEBUG_PROBE, "IO hw_base address: %lx\n", board->hw_base);
  2221. sx_dprintk(SX_DEBUG_PROBE, "base: %p\n", board->base);
  2222. board->irq = inb(eisa_slot + 0xc02) >> 4;
  2223. sx_dprintk(SX_DEBUG_PROBE, "IRQ: %d\n", board->irq);
  2224. if (!probe_si(board))
  2225. goto err_unmap;
  2226. dev_set_drvdata(dev, board);
  2227. return 0;
  2228. err_unmap:
  2229. iounmap(board->base);
  2230. err_reg:
  2231. release_region(board->hw_base, board->hw_len);
  2232. err_flag:
  2233. board->flags &= ~SX_BOARD_PRESENT;
  2234. err:
  2235. return retval;
  2236. }
  2237. static int __devexit sx_eisa_remove(struct device *dev)
  2238. {
  2239. struct sx_board *board = dev_get_drvdata(dev);
  2240. sx_remove_card(board, NULL);
  2241. return 0;
  2242. }
  2243. static struct eisa_device_id sx_eisa_tbl[] = {
  2244. { "SLX" },
  2245. { "" }
  2246. };
  2247. MODULE_DEVICE_TABLE(eisa, sx_eisa_tbl);
  2248. static struct eisa_driver sx_eisadriver = {
  2249. .id_table = sx_eisa_tbl,
  2250. .driver = {
  2251. .name = "sx",
  2252. .probe = sx_eisa_probe,
  2253. .remove = __devexit_p(sx_eisa_remove),
  2254. }
  2255. };
  2256. #endif
  2257. #ifdef CONFIG_PCI
  2258. /********************************************************
  2259. * Setting bit 17 in the CNTRL register of the PLX 9050 *
  2260. * chip forces a retry on writes while a read is pending.*
  2261. * This is to prevent the card locking up on Intel Xeon *
  2262. * multiprocessor systems with the NX chipset. -- NV *
  2263. ********************************************************/
  2264. /* Newer cards are produced with this bit set from the configuration
  2265. EEprom. As the bit is read/write for the CPU, we can fix it here,
  2266. if we detect that it isn't set correctly. -- REW */
  2267. static void __devinit fix_sx_pci(struct pci_dev *pdev, struct sx_board *board)
  2268. {
  2269. unsigned int hwbase;
  2270. void __iomem *rebase;
  2271. unsigned int t;
  2272. #define CNTRL_REG_OFFSET 0x50
  2273. #define CNTRL_REG_GOODVALUE 0x18260000
  2274. pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &hwbase);
  2275. hwbase &= PCI_BASE_ADDRESS_MEM_MASK;
  2276. rebase = ioremap_nocache(hwbase, 0x80);
  2277. t = readl(rebase + CNTRL_REG_OFFSET);
  2278. if (t != CNTRL_REG_GOODVALUE) {
  2279. printk(KERN_DEBUG "sx: performing cntrl reg fix: %08x -> "
  2280. "%08x\n", t, CNTRL_REG_GOODVALUE);
  2281. writel(CNTRL_REG_GOODVALUE, rebase + CNTRL_REG_OFFSET);
  2282. }
  2283. iounmap(rebase);
  2284. }
  2285. #endif
  2286. static int __devinit sx_pci_probe(struct pci_dev *pdev,
  2287. const struct pci_device_id *ent)
  2288. {
  2289. #ifdef CONFIG_PCI
  2290. struct sx_board *board;
  2291. unsigned int i, reg;
  2292. int retval = -EIO;
  2293. mutex_lock(&sx_boards_lock);
  2294. i = sx_find_free_board();
  2295. if (i == SX_NBOARDS) {
  2296. mutex_unlock(&sx_boards_lock);
  2297. goto err;
  2298. }
  2299. board = &boards[i];
  2300. board->flags |= SX_BOARD_PRESENT;
  2301. mutex_unlock(&sx_boards_lock);
  2302. retval = pci_enable_device(pdev);
  2303. if (retval)
  2304. goto err_flag;
  2305. board->flags &= ~SX_BOARD_TYPE;
  2306. board->flags |= (pdev->subsystem_vendor == 0x200) ? SX_PCI_BOARD :
  2307. SX_CFPCI_BOARD;
  2308. /* CF boards use base address 3.... */
  2309. reg = IS_CF_BOARD(board) ? 3 : 2;
  2310. retval = pci_request_region(pdev, reg, "sx");
  2311. if (retval) {
  2312. dev_err(&pdev->dev, "can't request region\n");
  2313. goto err_flag;
  2314. }
  2315. board->hw_base = pci_resource_start(pdev, reg);
  2316. board->base2 =
  2317. board->base = ioremap_nocache(board->hw_base, WINDOW_LEN(board));
  2318. if (!board->base) {
  2319. dev_err(&pdev->dev, "ioremap failed\n");
  2320. goto err_reg;
  2321. }
  2322. /* Most of the stuff on the CF board is offset by 0x18000 .... */
  2323. if (IS_CF_BOARD(board))
  2324. board->base += 0x18000;
  2325. board->irq = pdev->irq;
  2326. dev_info(&pdev->dev, "Got a specialix card: %p(%d) %x.\n", board->base,
  2327. board->irq, board->flags);
  2328. if (!probe_sx(board)) {
  2329. retval = -EIO;
  2330. goto err_unmap;
  2331. }
  2332. fix_sx_pci(pdev, board);
  2333. pci_set_drvdata(pdev, board);
  2334. return 0;
  2335. err_unmap:
  2336. iounmap(board->base2);
  2337. err_reg:
  2338. pci_release_region(pdev, reg);
  2339. err_flag:
  2340. board->flags &= ~SX_BOARD_PRESENT;
  2341. err:
  2342. return retval;
  2343. #else
  2344. return -ENODEV;
  2345. #endif
  2346. }
  2347. static void __devexit sx_pci_remove(struct pci_dev *pdev)
  2348. {
  2349. struct sx_board *board = pci_get_drvdata(pdev);
  2350. sx_remove_card(board, pdev);
  2351. }
  2352. /* Specialix has a whole bunch of cards with 0x2000 as the device ID. They say
  2353. its because the standard requires it. So check for SUBVENDOR_ID. */
  2354. static struct pci_device_id sx_pci_tbl[] = {
  2355. { PCI_VENDOR_ID_SPECIALIX, PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8,
  2356. .subvendor = PCI_ANY_ID, .subdevice = 0x0200 },
  2357. { PCI_VENDOR_ID_SPECIALIX, PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8,
  2358. .subvendor = PCI_ANY_ID, .subdevice = 0x0300 },
  2359. { 0 }
  2360. };
  2361. MODULE_DEVICE_TABLE(pci, sx_pci_tbl);
  2362. static struct pci_driver sx_pcidriver = {
  2363. .name = "sx",
  2364. .id_table = sx_pci_tbl,
  2365. .probe = sx_pci_probe,
  2366. .remove = __devexit_p(sx_pci_remove)
  2367. };
  2368. static int __init sx_init(void)
  2369. {
  2370. #ifdef CONFIG_EISA
  2371. int retval1;
  2372. #endif
  2373. #ifdef CONFIG_ISA
  2374. struct sx_board *board;
  2375. unsigned int i;
  2376. #endif
  2377. unsigned int found = 0;
  2378. int retval;
  2379. func_enter();
  2380. sx_dprintk(SX_DEBUG_INIT, "Initing sx module... (sx_debug=%d)\n",
  2381. sx_debug);
  2382. if (abs((long)(&sx_debug) - sx_debug) < 0x10000) {
  2383. printk(KERN_WARNING "sx: sx_debug is an address, instead of a "
  2384. "value. Assuming -1.\n(%p)\n", &sx_debug);
  2385. sx_debug = -1;
  2386. }
  2387. if (misc_register(&sx_fw_device) < 0) {
  2388. printk(KERN_ERR "SX: Unable to register firmware loader "
  2389. "driver.\n");
  2390. return -EIO;
  2391. }
  2392. #ifdef CONFIG_ISA
  2393. for (i = 0; i < NR_SX_ADDRS; i++) {
  2394. board = &boards[found];
  2395. board->hw_base = sx_probe_addrs[i];
  2396. board->hw_len = SX_WINDOW_LEN;
  2397. if (!request_region(board->hw_base, board->hw_len, "sx"))
  2398. continue;
  2399. board->base2 =
  2400. board->base = ioremap_nocache(board->hw_base, board->hw_len);
  2401. if (!board->base)
  2402. goto err_sx_reg;
  2403. board->flags &= ~SX_BOARD_TYPE;
  2404. board->flags |= SX_ISA_BOARD;
  2405. board->irq = sx_irqmask ? -1 : 0;
  2406. if (probe_sx(board)) {
  2407. board->flags |= SX_BOARD_PRESENT;
  2408. found++;
  2409. } else {
  2410. iounmap(board->base);
  2411. err_sx_reg:
  2412. release_region(board->hw_base, board->hw_len);
  2413. }
  2414. }
  2415. for (i = 0; i < NR_SI_ADDRS; i++) {
  2416. board = &boards[found];
  2417. board->hw_base = si_probe_addrs[i];
  2418. board->hw_len = SI2_ISA_WINDOW_LEN;
  2419. if (!request_region(board->hw_base, board->hw_len, "sx"))
  2420. continue;
  2421. board->base2 =
  2422. board->base = ioremap_nocache(board->hw_base, board->hw_len);
  2423. if (!board->base)
  2424. goto err_si_reg;
  2425. board->flags &= ~SX_BOARD_TYPE;
  2426. board->flags |= SI_ISA_BOARD;
  2427. board->irq = sx_irqmask ? -1 : 0;
  2428. if (probe_si(board)) {
  2429. board->flags |= SX_BOARD_PRESENT;
  2430. found++;
  2431. } else {
  2432. iounmap(board->base);
  2433. err_si_reg:
  2434. release_region(board->hw_base, board->hw_len);
  2435. }
  2436. }
  2437. for (i = 0; i < NR_SI1_ADDRS; i++) {
  2438. board = &boards[found];
  2439. board->hw_base = si1_probe_addrs[i];
  2440. board->hw_len = SI1_ISA_WINDOW_LEN;
  2441. if (!request_region(board->hw_base, board->hw_len, "sx"))
  2442. continue;
  2443. board->base2 =
  2444. board->base = ioremap_nocache(board->hw_base, board->hw_len);
  2445. if (!board->base)
  2446. goto err_si1_reg;
  2447. board->flags &= ~SX_BOARD_TYPE;
  2448. board->flags |= SI1_ISA_BOARD;
  2449. board->irq = sx_irqmask ? -1 : 0;
  2450. if (probe_si(board)) {
  2451. board->flags |= SX_BOARD_PRESENT;
  2452. found++;
  2453. } else {
  2454. iounmap(board->base);
  2455. err_si1_reg:
  2456. release_region(board->hw_base, board->hw_len);
  2457. }
  2458. }
  2459. #endif
  2460. #ifdef CONFIG_EISA
  2461. retval1 = eisa_driver_register(&sx_eisadriver);
  2462. #endif
  2463. retval = pci_register_driver(&sx_pcidriver);
  2464. if (found) {
  2465. printk(KERN_INFO "sx: total of %d boards detected.\n", found);
  2466. retval = 0;
  2467. } else if (retval) {
  2468. #ifdef CONFIG_EISA
  2469. retval = retval1;
  2470. if (retval1)
  2471. #endif
  2472. misc_deregister(&sx_fw_device);
  2473. }
  2474. func_exit();
  2475. return retval;
  2476. }
  2477. static void __exit sx_exit(void)
  2478. {
  2479. int i;
  2480. func_enter();
  2481. #ifdef CONFIG_EISA
  2482. eisa_driver_unregister(&sx_eisadriver);
  2483. #endif
  2484. pci_unregister_driver(&sx_pcidriver);
  2485. for (i = 0; i < SX_NBOARDS; i++)
  2486. sx_remove_card(&boards[i], NULL);
  2487. if (misc_deregister(&sx_fw_device) < 0) {
  2488. printk(KERN_INFO "sx: couldn't deregister firmware loader "
  2489. "device\n");
  2490. }
  2491. sx_dprintk(SX_DEBUG_CLEANUP, "Cleaning up drivers (%d)\n",
  2492. sx_initialized);
  2493. if (sx_initialized)
  2494. sx_release_drivers();
  2495. kfree(sx_ports);
  2496. func_exit();
  2497. }
  2498. module_init(sx_init);
  2499. module_exit(sx_exit);