/drivers/char/istallion.c

https://bitbucket.org/evzijst/gittest · C · 5276 lines · 3590 code · 777 blank · 909 comment · 755 complexity · 35cbe6dab5652e5cf384df0804d64b1d MD5 · raw file

Large files are truncated click here to view the full file

  1. /*****************************************************************************/
  2. /*
  3. * istallion.c -- stallion intelligent multiport serial driver.
  4. *
  5. * Copyright (C) 1996-1999 Stallion Technologies
  6. * Copyright (C) 1994-1996 Greg Ungerer.
  7. *
  8. * This code is loosely based on the Linux serial driver, written by
  9. * Linus Torvalds, Theodore T'so and others.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. /*****************************************************************************/
  26. #include <linux/config.h>
  27. #include <linux/module.h>
  28. #include <linux/slab.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/tty.h>
  31. #include <linux/tty_flip.h>
  32. #include <linux/serial.h>
  33. #include <linux/cdk.h>
  34. #include <linux/comstats.h>
  35. #include <linux/istallion.h>
  36. #include <linux/ioport.h>
  37. #include <linux/delay.h>
  38. #include <linux/init.h>
  39. #include <linux/devfs_fs_kernel.h>
  40. #include <linux/device.h>
  41. #include <linux/wait.h>
  42. #include <asm/io.h>
  43. #include <asm/uaccess.h>
  44. #ifdef CONFIG_PCI
  45. #include <linux/pci.h>
  46. #endif
  47. /*****************************************************************************/
  48. /*
  49. * Define different board types. Not all of the following board types
  50. * are supported by this driver. But I will use the standard "assigned"
  51. * board numbers. Currently supported boards are abbreviated as:
  52. * ECP = EasyConnection 8/64, ONB = ONboard, BBY = Brumby and
  53. * STAL = Stallion.
  54. */
  55. #define BRD_UNKNOWN 0
  56. #define BRD_STALLION 1
  57. #define BRD_BRUMBY4 2
  58. #define BRD_ONBOARD2 3
  59. #define BRD_ONBOARD 4
  60. #define BRD_BRUMBY8 5
  61. #define BRD_BRUMBY16 6
  62. #define BRD_ONBOARDE 7
  63. #define BRD_ONBOARD32 9
  64. #define BRD_ONBOARD2_32 10
  65. #define BRD_ONBOARDRS 11
  66. #define BRD_EASYIO 20
  67. #define BRD_ECH 21
  68. #define BRD_ECHMC 22
  69. #define BRD_ECP 23
  70. #define BRD_ECPE 24
  71. #define BRD_ECPMC 25
  72. #define BRD_ECHPCI 26
  73. #define BRD_ECH64PCI 27
  74. #define BRD_EASYIOPCI 28
  75. #define BRD_ECPPCI 29
  76. #define BRD_BRUMBY BRD_BRUMBY4
  77. /*
  78. * Define a configuration structure to hold the board configuration.
  79. * Need to set this up in the code (for now) with the boards that are
  80. * to be configured into the system. This is what needs to be modified
  81. * when adding/removing/modifying boards. Each line entry in the
  82. * stli_brdconf[] array is a board. Each line contains io/irq/memory
  83. * ranges for that board (as well as what type of board it is).
  84. * Some examples:
  85. * { BRD_ECP, 0x2a0, 0, 0xcc000, 0, 0 },
  86. * This line will configure an EasyConnection 8/64 at io address 2a0,
  87. * and shared memory address of cc000. Multiple EasyConnection 8/64
  88. * boards can share the same shared memory address space. No interrupt
  89. * is required for this board type.
  90. * Another example:
  91. * { BRD_ECPE, 0x5000, 0, 0x80000000, 0, 0 },
  92. * This line will configure an EasyConnection 8/64 EISA in slot 5 and
  93. * shared memory address of 0x80000000 (2 GByte). Multiple
  94. * EasyConnection 8/64 EISA boards can share the same shared memory
  95. * address space. No interrupt is required for this board type.
  96. * Another example:
  97. * { BRD_ONBOARD, 0x240, 0, 0xd0000, 0, 0 },
  98. * This line will configure an ONboard (ISA type) at io address 240,
  99. * and shared memory address of d0000. Multiple ONboards can share
  100. * the same shared memory address space. No interrupt required.
  101. * Another example:
  102. * { BRD_BRUMBY4, 0x360, 0, 0xc8000, 0, 0 },
  103. * This line will configure a Brumby board (any number of ports!) at
  104. * io address 360 and shared memory address of c8000. All Brumby boards
  105. * configured into a system must have their own separate io and memory
  106. * addresses. No interrupt is required.
  107. * Another example:
  108. * { BRD_STALLION, 0x330, 0, 0xd0000, 0, 0 },
  109. * This line will configure an original Stallion board at io address 330
  110. * and shared memory address d0000 (this would only be valid for a "V4.0"
  111. * or Rev.O Stallion board). All Stallion boards configured into the
  112. * system must have their own separate io and memory addresses. No
  113. * interrupt is required.
  114. */
  115. typedef struct {
  116. int brdtype;
  117. int ioaddr1;
  118. int ioaddr2;
  119. unsigned long memaddr;
  120. int irq;
  121. int irqtype;
  122. } stlconf_t;
  123. static stlconf_t stli_brdconf[] = {
  124. /*{ BRD_ECP, 0x2a0, 0, 0xcc000, 0, 0 },*/
  125. };
  126. static int stli_nrbrds = sizeof(stli_brdconf) / sizeof(stlconf_t);
  127. /*
  128. * There is some experimental EISA board detection code in this driver.
  129. * By default it is disabled, but for those that want to try it out,
  130. * then set the define below to be 1.
  131. */
  132. #define STLI_EISAPROBE 0
  133. /*****************************************************************************/
  134. /*
  135. * Define some important driver characteristics. Device major numbers
  136. * allocated as per Linux Device Registry.
  137. */
  138. #ifndef STL_SIOMEMMAJOR
  139. #define STL_SIOMEMMAJOR 28
  140. #endif
  141. #ifndef STL_SERIALMAJOR
  142. #define STL_SERIALMAJOR 24
  143. #endif
  144. #ifndef STL_CALLOUTMAJOR
  145. #define STL_CALLOUTMAJOR 25
  146. #endif
  147. /*****************************************************************************/
  148. /*
  149. * Define our local driver identity first. Set up stuff to deal with
  150. * all the local structures required by a serial tty driver.
  151. */
  152. static char *stli_drvtitle = "Stallion Intelligent Multiport Serial Driver";
  153. static char *stli_drvname = "istallion";
  154. static char *stli_drvversion = "5.6.0";
  155. static char *stli_serialname = "ttyE";
  156. static struct tty_driver *stli_serial;
  157. /*
  158. * We will need to allocate a temporary write buffer for chars that
  159. * come direct from user space. The problem is that a copy from user
  160. * space might cause a page fault (typically on a system that is
  161. * swapping!). All ports will share one buffer - since if the system
  162. * is already swapping a shared buffer won't make things any worse.
  163. */
  164. static char *stli_tmpwritebuf;
  165. static DECLARE_MUTEX(stli_tmpwritesem);
  166. #define STLI_TXBUFSIZE 4096
  167. /*
  168. * Use a fast local buffer for cooked characters. Typically a whole
  169. * bunch of cooked characters come in for a port, 1 at a time. So we
  170. * save those up into a local buffer, then write out the whole lot
  171. * with a large memcpy. Just use 1 buffer for all ports, since its
  172. * use it is only need for short periods of time by each port.
  173. */
  174. static char *stli_txcookbuf;
  175. static int stli_txcooksize;
  176. static int stli_txcookrealsize;
  177. static struct tty_struct *stli_txcooktty;
  178. /*
  179. * Define a local default termios struct. All ports will be created
  180. * with this termios initially. Basically all it defines is a raw port
  181. * at 9600 baud, 8 data bits, no parity, 1 stop bit.
  182. */
  183. static struct termios stli_deftermios = {
  184. .c_cflag = (B9600 | CS8 | CREAD | HUPCL | CLOCAL),
  185. .c_cc = INIT_C_CC,
  186. };
  187. /*
  188. * Define global stats structures. Not used often, and can be
  189. * re-used for each stats call.
  190. */
  191. static comstats_t stli_comstats;
  192. static combrd_t stli_brdstats;
  193. static asystats_t stli_cdkstats;
  194. static stlibrd_t stli_dummybrd;
  195. static stliport_t stli_dummyport;
  196. /*****************************************************************************/
  197. static stlibrd_t *stli_brds[STL_MAXBRDS];
  198. static int stli_shared;
  199. /*
  200. * Per board state flags. Used with the state field of the board struct.
  201. * Not really much here... All we need to do is keep track of whether
  202. * the board has been detected, and whether it is actually running a slave
  203. * or not.
  204. */
  205. #define BST_FOUND 0x1
  206. #define BST_STARTED 0x2
  207. /*
  208. * Define the set of port state flags. These are marked for internal
  209. * state purposes only, usually to do with the state of communications
  210. * with the slave. Most of them need to be updated atomically, so always
  211. * use the bit setting operations (unless protected by cli/sti).
  212. */
  213. #define ST_INITIALIZING 1
  214. #define ST_OPENING 2
  215. #define ST_CLOSING 3
  216. #define ST_CMDING 4
  217. #define ST_TXBUSY 5
  218. #define ST_RXING 6
  219. #define ST_DOFLUSHRX 7
  220. #define ST_DOFLUSHTX 8
  221. #define ST_DOSIGS 9
  222. #define ST_RXSTOP 10
  223. #define ST_GETSIGS 11
  224. /*
  225. * Define an array of board names as printable strings. Handy for
  226. * referencing boards when printing trace and stuff.
  227. */
  228. static char *stli_brdnames[] = {
  229. "Unknown",
  230. "Stallion",
  231. "Brumby",
  232. "ONboard-MC",
  233. "ONboard",
  234. "Brumby",
  235. "Brumby",
  236. "ONboard-EI",
  237. (char *) NULL,
  238. "ONboard",
  239. "ONboard-MC",
  240. "ONboard-MC",
  241. (char *) NULL,
  242. (char *) NULL,
  243. (char *) NULL,
  244. (char *) NULL,
  245. (char *) NULL,
  246. (char *) NULL,
  247. (char *) NULL,
  248. (char *) NULL,
  249. "EasyIO",
  250. "EC8/32-AT",
  251. "EC8/32-MC",
  252. "EC8/64-AT",
  253. "EC8/64-EI",
  254. "EC8/64-MC",
  255. "EC8/32-PCI",
  256. "EC8/64-PCI",
  257. "EasyIO-PCI",
  258. "EC/RA-PCI",
  259. };
  260. /*****************************************************************************/
  261. #ifdef MODULE
  262. /*
  263. * Define some string labels for arguments passed from the module
  264. * load line. These allow for easy board definitions, and easy
  265. * modification of the io, memory and irq resoucres.
  266. */
  267. static char *board0[8];
  268. static char *board1[8];
  269. static char *board2[8];
  270. static char *board3[8];
  271. static char **stli_brdsp[] = {
  272. (char **) &board0,
  273. (char **) &board1,
  274. (char **) &board2,
  275. (char **) &board3
  276. };
  277. /*
  278. * Define a set of common board names, and types. This is used to
  279. * parse any module arguments.
  280. */
  281. typedef struct stlibrdtype {
  282. char *name;
  283. int type;
  284. } stlibrdtype_t;
  285. static stlibrdtype_t stli_brdstr[] = {
  286. { "stallion", BRD_STALLION },
  287. { "1", BRD_STALLION },
  288. { "brumby", BRD_BRUMBY },
  289. { "brumby4", BRD_BRUMBY },
  290. { "brumby/4", BRD_BRUMBY },
  291. { "brumby-4", BRD_BRUMBY },
  292. { "brumby8", BRD_BRUMBY },
  293. { "brumby/8", BRD_BRUMBY },
  294. { "brumby-8", BRD_BRUMBY },
  295. { "brumby16", BRD_BRUMBY },
  296. { "brumby/16", BRD_BRUMBY },
  297. { "brumby-16", BRD_BRUMBY },
  298. { "2", BRD_BRUMBY },
  299. { "onboard2", BRD_ONBOARD2 },
  300. { "onboard-2", BRD_ONBOARD2 },
  301. { "onboard/2", BRD_ONBOARD2 },
  302. { "onboard-mc", BRD_ONBOARD2 },
  303. { "onboard/mc", BRD_ONBOARD2 },
  304. { "onboard-mca", BRD_ONBOARD2 },
  305. { "onboard/mca", BRD_ONBOARD2 },
  306. { "3", BRD_ONBOARD2 },
  307. { "onboard", BRD_ONBOARD },
  308. { "onboardat", BRD_ONBOARD },
  309. { "4", BRD_ONBOARD },
  310. { "onboarde", BRD_ONBOARDE },
  311. { "onboard-e", BRD_ONBOARDE },
  312. { "onboard/e", BRD_ONBOARDE },
  313. { "onboard-ei", BRD_ONBOARDE },
  314. { "onboard/ei", BRD_ONBOARDE },
  315. { "7", BRD_ONBOARDE },
  316. { "ecp", BRD_ECP },
  317. { "ecpat", BRD_ECP },
  318. { "ec8/64", BRD_ECP },
  319. { "ec8/64-at", BRD_ECP },
  320. { "ec8/64-isa", BRD_ECP },
  321. { "23", BRD_ECP },
  322. { "ecpe", BRD_ECPE },
  323. { "ecpei", BRD_ECPE },
  324. { "ec8/64-e", BRD_ECPE },
  325. { "ec8/64-ei", BRD_ECPE },
  326. { "24", BRD_ECPE },
  327. { "ecpmc", BRD_ECPMC },
  328. { "ec8/64-mc", BRD_ECPMC },
  329. { "ec8/64-mca", BRD_ECPMC },
  330. { "25", BRD_ECPMC },
  331. { "ecppci", BRD_ECPPCI },
  332. { "ec/ra", BRD_ECPPCI },
  333. { "ec/ra-pc", BRD_ECPPCI },
  334. { "ec/ra-pci", BRD_ECPPCI },
  335. { "29", BRD_ECPPCI },
  336. };
  337. /*
  338. * Define the module agruments.
  339. */
  340. MODULE_AUTHOR("Greg Ungerer");
  341. MODULE_DESCRIPTION("Stallion Intelligent Multiport Serial Driver");
  342. MODULE_LICENSE("GPL");
  343. MODULE_PARM(board0, "1-3s");
  344. MODULE_PARM_DESC(board0, "Board 0 config -> name[,ioaddr[,memaddr]");
  345. MODULE_PARM(board1, "1-3s");
  346. MODULE_PARM_DESC(board1, "Board 1 config -> name[,ioaddr[,memaddr]");
  347. MODULE_PARM(board2, "1-3s");
  348. MODULE_PARM_DESC(board2, "Board 2 config -> name[,ioaddr[,memaddr]");
  349. MODULE_PARM(board3, "1-3s");
  350. MODULE_PARM_DESC(board3, "Board 3 config -> name[,ioaddr[,memaddr]");
  351. #endif
  352. /*
  353. * Set up a default memory address table for EISA board probing.
  354. * The default addresses are all bellow 1Mbyte, which has to be the
  355. * case anyway. They should be safe, since we only read values from
  356. * them, and interrupts are disabled while we do it. If the higher
  357. * memory support is compiled in then we also try probing around
  358. * the 1Gb, 2Gb and 3Gb areas as well...
  359. */
  360. static unsigned long stli_eisamemprobeaddrs[] = {
  361. 0xc0000, 0xd0000, 0xe0000, 0xf0000,
  362. 0x80000000, 0x80010000, 0x80020000, 0x80030000,
  363. 0x40000000, 0x40010000, 0x40020000, 0x40030000,
  364. 0xc0000000, 0xc0010000, 0xc0020000, 0xc0030000,
  365. 0xff000000, 0xff010000, 0xff020000, 0xff030000,
  366. };
  367. static int stli_eisamempsize = sizeof(stli_eisamemprobeaddrs) / sizeof(unsigned long);
  368. int stli_eisaprobe = STLI_EISAPROBE;
  369. /*
  370. * Define the Stallion PCI vendor and device IDs.
  371. */
  372. #ifdef CONFIG_PCI
  373. #ifndef PCI_VENDOR_ID_STALLION
  374. #define PCI_VENDOR_ID_STALLION 0x124d
  375. #endif
  376. #ifndef PCI_DEVICE_ID_ECRA
  377. #define PCI_DEVICE_ID_ECRA 0x0004
  378. #endif
  379. static struct pci_device_id istallion_pci_tbl[] = {
  380. { PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_ECRA, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  381. { 0 }
  382. };
  383. MODULE_DEVICE_TABLE(pci, istallion_pci_tbl);
  384. #endif /* CONFIG_PCI */
  385. /*****************************************************************************/
  386. /*
  387. * Hardware configuration info for ECP boards. These defines apply
  388. * to the directly accessible io ports of the ECP. There is a set of
  389. * defines for each ECP board type, ISA, EISA, MCA and PCI.
  390. */
  391. #define ECP_IOSIZE 4
  392. #define ECP_MEMSIZE (128 * 1024)
  393. #define ECP_PCIMEMSIZE (256 * 1024)
  394. #define ECP_ATPAGESIZE (4 * 1024)
  395. #define ECP_MCPAGESIZE (4 * 1024)
  396. #define ECP_EIPAGESIZE (64 * 1024)
  397. #define ECP_PCIPAGESIZE (64 * 1024)
  398. #define STL_EISAID 0x8c4e
  399. /*
  400. * Important defines for the ISA class of ECP board.
  401. */
  402. #define ECP_ATIREG 0
  403. #define ECP_ATCONFR 1
  404. #define ECP_ATMEMAR 2
  405. #define ECP_ATMEMPR 3
  406. #define ECP_ATSTOP 0x1
  407. #define ECP_ATINTENAB 0x10
  408. #define ECP_ATENABLE 0x20
  409. #define ECP_ATDISABLE 0x00
  410. #define ECP_ATADDRMASK 0x3f000
  411. #define ECP_ATADDRSHFT 12
  412. /*
  413. * Important defines for the EISA class of ECP board.
  414. */
  415. #define ECP_EIIREG 0
  416. #define ECP_EIMEMARL 1
  417. #define ECP_EICONFR 2
  418. #define ECP_EIMEMARH 3
  419. #define ECP_EIENABLE 0x1
  420. #define ECP_EIDISABLE 0x0
  421. #define ECP_EISTOP 0x4
  422. #define ECP_EIEDGE 0x00
  423. #define ECP_EILEVEL 0x80
  424. #define ECP_EIADDRMASKL 0x00ff0000
  425. #define ECP_EIADDRSHFTL 16
  426. #define ECP_EIADDRMASKH 0xff000000
  427. #define ECP_EIADDRSHFTH 24
  428. #define ECP_EIBRDENAB 0xc84
  429. #define ECP_EISAID 0x4
  430. /*
  431. * Important defines for the Micro-channel class of ECP board.
  432. * (It has a lot in common with the ISA boards.)
  433. */
  434. #define ECP_MCIREG 0
  435. #define ECP_MCCONFR 1
  436. #define ECP_MCSTOP 0x20
  437. #define ECP_MCENABLE 0x80
  438. #define ECP_MCDISABLE 0x00
  439. /*
  440. * Important defines for the PCI class of ECP board.
  441. * (It has a lot in common with the other ECP boards.)
  442. */
  443. #define ECP_PCIIREG 0
  444. #define ECP_PCICONFR 1
  445. #define ECP_PCISTOP 0x01
  446. /*
  447. * Hardware configuration info for ONboard and Brumby boards. These
  448. * defines apply to the directly accessible io ports of these boards.
  449. */
  450. #define ONB_IOSIZE 16
  451. #define ONB_MEMSIZE (64 * 1024)
  452. #define ONB_ATPAGESIZE (64 * 1024)
  453. #define ONB_MCPAGESIZE (64 * 1024)
  454. #define ONB_EIMEMSIZE (128 * 1024)
  455. #define ONB_EIPAGESIZE (64 * 1024)
  456. /*
  457. * Important defines for the ISA class of ONboard board.
  458. */
  459. #define ONB_ATIREG 0
  460. #define ONB_ATMEMAR 1
  461. #define ONB_ATCONFR 2
  462. #define ONB_ATSTOP 0x4
  463. #define ONB_ATENABLE 0x01
  464. #define ONB_ATDISABLE 0x00
  465. #define ONB_ATADDRMASK 0xff0000
  466. #define ONB_ATADDRSHFT 16
  467. #define ONB_MEMENABLO 0
  468. #define ONB_MEMENABHI 0x02
  469. /*
  470. * Important defines for the EISA class of ONboard board.
  471. */
  472. #define ONB_EIIREG 0
  473. #define ONB_EIMEMARL 1
  474. #define ONB_EICONFR 2
  475. #define ONB_EIMEMARH 3
  476. #define ONB_EIENABLE 0x1
  477. #define ONB_EIDISABLE 0x0
  478. #define ONB_EISTOP 0x4
  479. #define ONB_EIEDGE 0x00
  480. #define ONB_EILEVEL 0x80
  481. #define ONB_EIADDRMASKL 0x00ff0000
  482. #define ONB_EIADDRSHFTL 16
  483. #define ONB_EIADDRMASKH 0xff000000
  484. #define ONB_EIADDRSHFTH 24
  485. #define ONB_EIBRDENAB 0xc84
  486. #define ONB_EISAID 0x1
  487. /*
  488. * Important defines for the Brumby boards. They are pretty simple,
  489. * there is not much that is programmably configurable.
  490. */
  491. #define BBY_IOSIZE 16
  492. #define BBY_MEMSIZE (64 * 1024)
  493. #define BBY_PAGESIZE (16 * 1024)
  494. #define BBY_ATIREG 0
  495. #define BBY_ATCONFR 1
  496. #define BBY_ATSTOP 0x4
  497. /*
  498. * Important defines for the Stallion boards. They are pretty simple,
  499. * there is not much that is programmably configurable.
  500. */
  501. #define STAL_IOSIZE 16
  502. #define STAL_MEMSIZE (64 * 1024)
  503. #define STAL_PAGESIZE (64 * 1024)
  504. /*
  505. * Define the set of status register values for EasyConnection panels.
  506. * The signature will return with the status value for each panel. From
  507. * this we can determine what is attached to the board - before we have
  508. * actually down loaded any code to it.
  509. */
  510. #define ECH_PNLSTATUS 2
  511. #define ECH_PNL16PORT 0x20
  512. #define ECH_PNLIDMASK 0x07
  513. #define ECH_PNLXPID 0x40
  514. #define ECH_PNLINTRPEND 0x80
  515. /*
  516. * Define some macros to do things to the board. Even those these boards
  517. * are somewhat related there is often significantly different ways of
  518. * doing some operation on it (like enable, paging, reset, etc). So each
  519. * board class has a set of functions which do the commonly required
  520. * operations. The macros below basically just call these functions,
  521. * generally checking for a NULL function - which means that the board
  522. * needs nothing done to it to achieve this operation!
  523. */
  524. #define EBRDINIT(brdp) \
  525. if (brdp->init != NULL) \
  526. (* brdp->init)(brdp)
  527. #define EBRDENABLE(brdp) \
  528. if (brdp->enable != NULL) \
  529. (* brdp->enable)(brdp);
  530. #define EBRDDISABLE(brdp) \
  531. if (brdp->disable != NULL) \
  532. (* brdp->disable)(brdp);
  533. #define EBRDINTR(brdp) \
  534. if (brdp->intr != NULL) \
  535. (* brdp->intr)(brdp);
  536. #define EBRDRESET(brdp) \
  537. if (brdp->reset != NULL) \
  538. (* brdp->reset)(brdp);
  539. #define EBRDGETMEMPTR(brdp,offset) \
  540. (* brdp->getmemptr)(brdp, offset, __LINE__)
  541. /*
  542. * Define the maximal baud rate, and the default baud base for ports.
  543. */
  544. #define STL_MAXBAUD 460800
  545. #define STL_BAUDBASE 115200
  546. #define STL_CLOSEDELAY (5 * HZ / 10)
  547. /*****************************************************************************/
  548. /*
  549. * Define macros to extract a brd or port number from a minor number.
  550. */
  551. #define MINOR2BRD(min) (((min) & 0xc0) >> 6)
  552. #define MINOR2PORT(min) ((min) & 0x3f)
  553. /*
  554. * Define a baud rate table that converts termios baud rate selector
  555. * into the actual baud rate value. All baud rate calculations are based
  556. * on the actual baud rate required.
  557. */
  558. static unsigned int stli_baudrates[] = {
  559. 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
  560. 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600
  561. };
  562. /*****************************************************************************/
  563. /*
  564. * Define some handy local macros...
  565. */
  566. #undef MIN
  567. #define MIN(a,b) (((a) <= (b)) ? (a) : (b))
  568. #undef TOLOWER
  569. #define TOLOWER(x) ((((x) >= 'A') && ((x) <= 'Z')) ? ((x) + 0x20) : (x))
  570. /*****************************************************************************/
  571. /*
  572. * Prototype all functions in this driver!
  573. */
  574. #ifdef MODULE
  575. static void stli_argbrds(void);
  576. static int stli_parsebrd(stlconf_t *confp, char **argp);
  577. static unsigned long stli_atol(char *str);
  578. #endif
  579. int stli_init(void);
  580. static int stli_open(struct tty_struct *tty, struct file *filp);
  581. static void stli_close(struct tty_struct *tty, struct file *filp);
  582. static int stli_write(struct tty_struct *tty, const unsigned char *buf, int count);
  583. static void stli_putchar(struct tty_struct *tty, unsigned char ch);
  584. static void stli_flushchars(struct tty_struct *tty);
  585. static int stli_writeroom(struct tty_struct *tty);
  586. static int stli_charsinbuffer(struct tty_struct *tty);
  587. static int stli_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
  588. static void stli_settermios(struct tty_struct *tty, struct termios *old);
  589. static void stli_throttle(struct tty_struct *tty);
  590. static void stli_unthrottle(struct tty_struct *tty);
  591. static void stli_stop(struct tty_struct *tty);
  592. static void stli_start(struct tty_struct *tty);
  593. static void stli_flushbuffer(struct tty_struct *tty);
  594. static void stli_breakctl(struct tty_struct *tty, int state);
  595. static void stli_waituntilsent(struct tty_struct *tty, int timeout);
  596. static void stli_sendxchar(struct tty_struct *tty, char ch);
  597. static void stli_hangup(struct tty_struct *tty);
  598. static int stli_portinfo(stlibrd_t *brdp, stliport_t *portp, int portnr, char *pos);
  599. static int stli_brdinit(stlibrd_t *brdp);
  600. static int stli_startbrd(stlibrd_t *brdp);
  601. static ssize_t stli_memread(struct file *fp, char __user *buf, size_t count, loff_t *offp);
  602. static ssize_t stli_memwrite(struct file *fp, const char __user *buf, size_t count, loff_t *offp);
  603. static int stli_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, unsigned long arg);
  604. static void stli_brdpoll(stlibrd_t *brdp, volatile cdkhdr_t *hdrp);
  605. static void stli_poll(unsigned long arg);
  606. static int stli_hostcmd(stlibrd_t *brdp, stliport_t *portp);
  607. static int stli_initopen(stlibrd_t *brdp, stliport_t *portp);
  608. static int stli_rawopen(stlibrd_t *brdp, stliport_t *portp, unsigned long arg, int wait);
  609. static int stli_rawclose(stlibrd_t *brdp, stliport_t *portp, unsigned long arg, int wait);
  610. static int stli_waitcarrier(stlibrd_t *brdp, stliport_t *portp, struct file *filp);
  611. static void stli_dohangup(void *arg);
  612. static int stli_setport(stliport_t *portp);
  613. static int stli_cmdwait(stlibrd_t *brdp, stliport_t *portp, unsigned long cmd, void *arg, int size, int copyback);
  614. static void stli_sendcmd(stlibrd_t *brdp, stliport_t *portp, unsigned long cmd, void *arg, int size, int copyback);
  615. static void stli_dodelaycmd(stliport_t *portp, volatile cdkctrl_t *cp);
  616. static void stli_mkasyport(stliport_t *portp, asyport_t *pp, struct termios *tiosp);
  617. static void stli_mkasysigs(asysigs_t *sp, int dtr, int rts);
  618. static long stli_mktiocm(unsigned long sigvalue);
  619. static void stli_read(stlibrd_t *brdp, stliport_t *portp);
  620. static int stli_getserial(stliport_t *portp, struct serial_struct __user *sp);
  621. static int stli_setserial(stliport_t *portp, struct serial_struct __user *sp);
  622. static int stli_getbrdstats(combrd_t __user *bp);
  623. static int stli_getportstats(stliport_t *portp, comstats_t __user *cp);
  624. static int stli_portcmdstats(stliport_t *portp);
  625. static int stli_clrportstats(stliport_t *portp, comstats_t __user *cp);
  626. static int stli_getportstruct(stliport_t __user *arg);
  627. static int stli_getbrdstruct(stlibrd_t __user *arg);
  628. static void *stli_memalloc(int len);
  629. static stlibrd_t *stli_allocbrd(void);
  630. static void stli_ecpinit(stlibrd_t *brdp);
  631. static void stli_ecpenable(stlibrd_t *brdp);
  632. static void stli_ecpdisable(stlibrd_t *brdp);
  633. static char *stli_ecpgetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
  634. static void stli_ecpreset(stlibrd_t *brdp);
  635. static void stli_ecpintr(stlibrd_t *brdp);
  636. static void stli_ecpeiinit(stlibrd_t *brdp);
  637. static void stli_ecpeienable(stlibrd_t *brdp);
  638. static void stli_ecpeidisable(stlibrd_t *brdp);
  639. static char *stli_ecpeigetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
  640. static void stli_ecpeireset(stlibrd_t *brdp);
  641. static void stli_ecpmcenable(stlibrd_t *brdp);
  642. static void stli_ecpmcdisable(stlibrd_t *brdp);
  643. static char *stli_ecpmcgetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
  644. static void stli_ecpmcreset(stlibrd_t *brdp);
  645. static void stli_ecppciinit(stlibrd_t *brdp);
  646. static char *stli_ecppcigetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
  647. static void stli_ecppcireset(stlibrd_t *brdp);
  648. static void stli_onbinit(stlibrd_t *brdp);
  649. static void stli_onbenable(stlibrd_t *brdp);
  650. static void stli_onbdisable(stlibrd_t *brdp);
  651. static char *stli_onbgetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
  652. static void stli_onbreset(stlibrd_t *brdp);
  653. static void stli_onbeinit(stlibrd_t *brdp);
  654. static void stli_onbeenable(stlibrd_t *brdp);
  655. static void stli_onbedisable(stlibrd_t *brdp);
  656. static char *stli_onbegetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
  657. static void stli_onbereset(stlibrd_t *brdp);
  658. static void stli_bbyinit(stlibrd_t *brdp);
  659. static char *stli_bbygetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
  660. static void stli_bbyreset(stlibrd_t *brdp);
  661. static void stli_stalinit(stlibrd_t *brdp);
  662. static char *stli_stalgetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
  663. static void stli_stalreset(stlibrd_t *brdp);
  664. static stliport_t *stli_getport(int brdnr, int panelnr, int portnr);
  665. static int stli_initecp(stlibrd_t *brdp);
  666. static int stli_initonb(stlibrd_t *brdp);
  667. static int stli_eisamemprobe(stlibrd_t *brdp);
  668. static int stli_initports(stlibrd_t *brdp);
  669. #ifdef CONFIG_PCI
  670. static int stli_initpcibrd(int brdtype, struct pci_dev *devp);
  671. #endif
  672. /*****************************************************************************/
  673. /*
  674. * Define the driver info for a user level shared memory device. This
  675. * device will work sort of like the /dev/kmem device - except that it
  676. * will give access to the shared memory on the Stallion intelligent
  677. * board. This is also a very useful debugging tool.
  678. */
  679. static struct file_operations stli_fsiomem = {
  680. .owner = THIS_MODULE,
  681. .read = stli_memread,
  682. .write = stli_memwrite,
  683. .ioctl = stli_memioctl,
  684. };
  685. /*****************************************************************************/
  686. /*
  687. * Define a timer_list entry for our poll routine. The slave board
  688. * is polled every so often to see if anything needs doing. This is
  689. * much cheaper on host cpu than using interrupts. It turns out to
  690. * not increase character latency by much either...
  691. */
  692. static struct timer_list stli_timerlist = TIMER_INITIALIZER(stli_poll, 0, 0);
  693. static int stli_timeron;
  694. /*
  695. * Define the calculation for the timeout routine.
  696. */
  697. #define STLI_TIMEOUT (jiffies + 1)
  698. /*****************************************************************************/
  699. static struct class_simple *istallion_class;
  700. #ifdef MODULE
  701. /*
  702. * Loadable module initialization stuff.
  703. */
  704. static int __init istallion_module_init(void)
  705. {
  706. unsigned long flags;
  707. #ifdef DEBUG
  708. printk("init_module()\n");
  709. #endif
  710. save_flags(flags);
  711. cli();
  712. stli_init();
  713. restore_flags(flags);
  714. return(0);
  715. }
  716. /*****************************************************************************/
  717. static void __exit istallion_module_exit(void)
  718. {
  719. stlibrd_t *brdp;
  720. stliport_t *portp;
  721. unsigned long flags;
  722. int i, j;
  723. #ifdef DEBUG
  724. printk("cleanup_module()\n");
  725. #endif
  726. printk(KERN_INFO "Unloading %s: version %s\n", stli_drvtitle,
  727. stli_drvversion);
  728. save_flags(flags);
  729. cli();
  730. /*
  731. * Free up all allocated resources used by the ports. This includes
  732. * memory and interrupts.
  733. */
  734. if (stli_timeron) {
  735. stli_timeron = 0;
  736. del_timer(&stli_timerlist);
  737. }
  738. i = tty_unregister_driver(stli_serial);
  739. if (i) {
  740. printk("STALLION: failed to un-register tty driver, "
  741. "errno=%d\n", -i);
  742. restore_flags(flags);
  743. return;
  744. }
  745. put_tty_driver(stli_serial);
  746. for (i = 0; i < 4; i++) {
  747. devfs_remove("staliomem/%d", i);
  748. class_simple_device_remove(MKDEV(STL_SIOMEMMAJOR, i));
  749. }
  750. devfs_remove("staliomem");
  751. class_simple_destroy(istallion_class);
  752. if ((i = unregister_chrdev(STL_SIOMEMMAJOR, "staliomem")))
  753. printk("STALLION: failed to un-register serial memory device, "
  754. "errno=%d\n", -i);
  755. if (stli_tmpwritebuf != (char *) NULL)
  756. kfree(stli_tmpwritebuf);
  757. if (stli_txcookbuf != (char *) NULL)
  758. kfree(stli_txcookbuf);
  759. for (i = 0; (i < stli_nrbrds); i++) {
  760. if ((brdp = stli_brds[i]) == (stlibrd_t *) NULL)
  761. continue;
  762. for (j = 0; (j < STL_MAXPORTS); j++) {
  763. portp = brdp->ports[j];
  764. if (portp != (stliport_t *) NULL) {
  765. if (portp->tty != (struct tty_struct *) NULL)
  766. tty_hangup(portp->tty);
  767. kfree(portp);
  768. }
  769. }
  770. iounmap(brdp->membase);
  771. if (brdp->iosize > 0)
  772. release_region(brdp->iobase, brdp->iosize);
  773. kfree(brdp);
  774. stli_brds[i] = (stlibrd_t *) NULL;
  775. }
  776. restore_flags(flags);
  777. }
  778. module_init(istallion_module_init);
  779. module_exit(istallion_module_exit);
  780. /*****************************************************************************/
  781. /*
  782. * Check for any arguments passed in on the module load command line.
  783. */
  784. static void stli_argbrds(void)
  785. {
  786. stlconf_t conf;
  787. stlibrd_t *brdp;
  788. int nrargs, i;
  789. #ifdef DEBUG
  790. printk("stli_argbrds()\n");
  791. #endif
  792. nrargs = sizeof(stli_brdsp) / sizeof(char **);
  793. for (i = stli_nrbrds; (i < nrargs); i++) {
  794. memset(&conf, 0, sizeof(conf));
  795. if (stli_parsebrd(&conf, stli_brdsp[i]) == 0)
  796. continue;
  797. if ((brdp = stli_allocbrd()) == (stlibrd_t *) NULL)
  798. continue;
  799. stli_nrbrds = i + 1;
  800. brdp->brdnr = i;
  801. brdp->brdtype = conf.brdtype;
  802. brdp->iobase = conf.ioaddr1;
  803. brdp->memaddr = conf.memaddr;
  804. stli_brdinit(brdp);
  805. }
  806. }
  807. /*****************************************************************************/
  808. /*
  809. * Convert an ascii string number into an unsigned long.
  810. */
  811. static unsigned long stli_atol(char *str)
  812. {
  813. unsigned long val;
  814. int base, c;
  815. char *sp;
  816. val = 0;
  817. sp = str;
  818. if ((*sp == '0') && (*(sp+1) == 'x')) {
  819. base = 16;
  820. sp += 2;
  821. } else if (*sp == '0') {
  822. base = 8;
  823. sp++;
  824. } else {
  825. base = 10;
  826. }
  827. for (; (*sp != 0); sp++) {
  828. c = (*sp > '9') ? (TOLOWER(*sp) - 'a' + 10) : (*sp - '0');
  829. if ((c < 0) || (c >= base)) {
  830. printk("STALLION: invalid argument %s\n", str);
  831. val = 0;
  832. break;
  833. }
  834. val = (val * base) + c;
  835. }
  836. return(val);
  837. }
  838. /*****************************************************************************/
  839. /*
  840. * Parse the supplied argument string, into the board conf struct.
  841. */
  842. static int stli_parsebrd(stlconf_t *confp, char **argp)
  843. {
  844. char *sp;
  845. int nrbrdnames, i;
  846. #ifdef DEBUG
  847. printk("stli_parsebrd(confp=%x,argp=%x)\n", (int) confp, (int) argp);
  848. #endif
  849. if ((argp[0] == (char *) NULL) || (*argp[0] == 0))
  850. return(0);
  851. for (sp = argp[0], i = 0; ((*sp != 0) && (i < 25)); sp++, i++)
  852. *sp = TOLOWER(*sp);
  853. nrbrdnames = sizeof(stli_brdstr) / sizeof(stlibrdtype_t);
  854. for (i = 0; (i < nrbrdnames); i++) {
  855. if (strcmp(stli_brdstr[i].name, argp[0]) == 0)
  856. break;
  857. }
  858. if (i >= nrbrdnames) {
  859. printk("STALLION: unknown board name, %s?\n", argp[0]);
  860. return(0);
  861. }
  862. confp->brdtype = stli_brdstr[i].type;
  863. if ((argp[1] != (char *) NULL) && (*argp[1] != 0))
  864. confp->ioaddr1 = stli_atol(argp[1]);
  865. if ((argp[2] != (char *) NULL) && (*argp[2] != 0))
  866. confp->memaddr = stli_atol(argp[2]);
  867. return(1);
  868. }
  869. #endif
  870. /*****************************************************************************/
  871. /*
  872. * Local driver kernel malloc routine.
  873. */
  874. static void *stli_memalloc(int len)
  875. {
  876. return((void *) kmalloc(len, GFP_KERNEL));
  877. }
  878. /*****************************************************************************/
  879. static int stli_open(struct tty_struct *tty, struct file *filp)
  880. {
  881. stlibrd_t *brdp;
  882. stliport_t *portp;
  883. unsigned int minordev;
  884. int brdnr, portnr, rc;
  885. #ifdef DEBUG
  886. printk("stli_open(tty=%x,filp=%x): device=%s\n", (int) tty,
  887. (int) filp, tty->name);
  888. #endif
  889. minordev = tty->index;
  890. brdnr = MINOR2BRD(minordev);
  891. if (brdnr >= stli_nrbrds)
  892. return(-ENODEV);
  893. brdp = stli_brds[brdnr];
  894. if (brdp == (stlibrd_t *) NULL)
  895. return(-ENODEV);
  896. if ((brdp->state & BST_STARTED) == 0)
  897. return(-ENODEV);
  898. portnr = MINOR2PORT(minordev);
  899. if ((portnr < 0) || (portnr > brdp->nrports))
  900. return(-ENODEV);
  901. portp = brdp->ports[portnr];
  902. if (portp == (stliport_t *) NULL)
  903. return(-ENODEV);
  904. if (portp->devnr < 1)
  905. return(-ENODEV);
  906. /*
  907. * Check if this port is in the middle of closing. If so then wait
  908. * until it is closed then return error status based on flag settings.
  909. * The sleep here does not need interrupt protection since the wakeup
  910. * for it is done with the same context.
  911. */
  912. if (portp->flags & ASYNC_CLOSING) {
  913. interruptible_sleep_on(&portp->close_wait);
  914. if (portp->flags & ASYNC_HUP_NOTIFY)
  915. return(-EAGAIN);
  916. return(-ERESTARTSYS);
  917. }
  918. /*
  919. * On the first open of the device setup the port hardware, and
  920. * initialize the per port data structure. Since initializing the port
  921. * requires several commands to the board we will need to wait for any
  922. * other open that is already initializing the port.
  923. */
  924. portp->tty = tty;
  925. tty->driver_data = portp;
  926. portp->refcount++;
  927. wait_event_interruptible(portp->raw_wait,
  928. !test_bit(ST_INITIALIZING, &portp->state));
  929. if (signal_pending(current))
  930. return(-ERESTARTSYS);
  931. if ((portp->flags & ASYNC_INITIALIZED) == 0) {
  932. set_bit(ST_INITIALIZING, &portp->state);
  933. if ((rc = stli_initopen(brdp, portp)) >= 0) {
  934. portp->flags |= ASYNC_INITIALIZED;
  935. clear_bit(TTY_IO_ERROR, &tty->flags);
  936. }
  937. clear_bit(ST_INITIALIZING, &portp->state);
  938. wake_up_interruptible(&portp->raw_wait);
  939. if (rc < 0)
  940. return(rc);
  941. }
  942. /*
  943. * Check if this port is in the middle of closing. If so then wait
  944. * until it is closed then return error status, based on flag settings.
  945. * The sleep here does not need interrupt protection since the wakeup
  946. * for it is done with the same context.
  947. */
  948. if (portp->flags & ASYNC_CLOSING) {
  949. interruptible_sleep_on(&portp->close_wait);
  950. if (portp->flags & ASYNC_HUP_NOTIFY)
  951. return(-EAGAIN);
  952. return(-ERESTARTSYS);
  953. }
  954. /*
  955. * Based on type of open being done check if it can overlap with any
  956. * previous opens still in effect. If we are a normal serial device
  957. * then also we might have to wait for carrier.
  958. */
  959. if (!(filp->f_flags & O_NONBLOCK)) {
  960. if ((rc = stli_waitcarrier(brdp, portp, filp)) != 0)
  961. return(rc);
  962. }
  963. portp->flags |= ASYNC_NORMAL_ACTIVE;
  964. return(0);
  965. }
  966. /*****************************************************************************/
  967. static void stli_close(struct tty_struct *tty, struct file *filp)
  968. {
  969. stlibrd_t *brdp;
  970. stliport_t *portp;
  971. unsigned long flags;
  972. #ifdef DEBUG
  973. printk("stli_close(tty=%x,filp=%x)\n", (int) tty, (int) filp);
  974. #endif
  975. portp = tty->driver_data;
  976. if (portp == (stliport_t *) NULL)
  977. return;
  978. save_flags(flags);
  979. cli();
  980. if (tty_hung_up_p(filp)) {
  981. restore_flags(flags);
  982. return;
  983. }
  984. if ((tty->count == 1) && (portp->refcount != 1))
  985. portp->refcount = 1;
  986. if (portp->refcount-- > 1) {
  987. restore_flags(flags);
  988. return;
  989. }
  990. portp->flags |= ASYNC_CLOSING;
  991. /*
  992. * May want to wait for data to drain before closing. The BUSY flag
  993. * keeps track of whether we are still transmitting or not. It is
  994. * updated by messages from the slave - indicating when all chars
  995. * really have drained.
  996. */
  997. if (tty == stli_txcooktty)
  998. stli_flushchars(tty);
  999. tty->closing = 1;
  1000. if (portp->closing_wait != ASYNC_CLOSING_WAIT_NONE)
  1001. tty_wait_until_sent(tty, portp->closing_wait);
  1002. portp->flags &= ~ASYNC_INITIALIZED;
  1003. brdp = stli_brds[portp->brdnr];
  1004. stli_rawclose(brdp, portp, 0, 0);
  1005. if (tty->termios->c_cflag & HUPCL) {
  1006. stli_mkasysigs(&portp->asig, 0, 0);
  1007. if (test_bit(ST_CMDING, &portp->state))
  1008. set_bit(ST_DOSIGS, &portp->state);
  1009. else
  1010. stli_sendcmd(brdp, portp, A_SETSIGNALS, &portp->asig,
  1011. sizeof(asysigs_t), 0);
  1012. }
  1013. clear_bit(ST_TXBUSY, &portp->state);
  1014. clear_bit(ST_RXSTOP, &portp->state);
  1015. set_bit(TTY_IO_ERROR, &tty->flags);
  1016. if (tty->ldisc.flush_buffer)
  1017. (tty->ldisc.flush_buffer)(tty);
  1018. set_bit(ST_DOFLUSHRX, &portp->state);
  1019. stli_flushbuffer(tty);
  1020. tty->closing = 0;
  1021. portp->tty = (struct tty_struct *) NULL;
  1022. if (portp->openwaitcnt) {
  1023. if (portp->close_delay)
  1024. msleep_interruptible(jiffies_to_msecs(portp->close_delay));
  1025. wake_up_interruptible(&portp->open_wait);
  1026. }
  1027. portp->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
  1028. wake_up_interruptible(&portp->close_wait);
  1029. restore_flags(flags);
  1030. }
  1031. /*****************************************************************************/
  1032. /*
  1033. * Carry out first open operations on a port. This involves a number of
  1034. * commands to be sent to the slave. We need to open the port, set the
  1035. * notification events, set the initial port settings, get and set the
  1036. * initial signal values. We sleep and wait in between each one. But
  1037. * this still all happens pretty quickly.
  1038. */
  1039. static int stli_initopen(stlibrd_t *brdp, stliport_t *portp)
  1040. {
  1041. struct tty_struct *tty;
  1042. asynotify_t nt;
  1043. asyport_t aport;
  1044. int rc;
  1045. #ifdef DEBUG
  1046. printk("stli_initopen(brdp=%x,portp=%x)\n", (int) brdp, (int) portp);
  1047. #endif
  1048. if ((rc = stli_rawopen(brdp, portp, 0, 1)) < 0)
  1049. return(rc);
  1050. memset(&nt, 0, sizeof(asynotify_t));
  1051. nt.data = (DT_TXLOW | DT_TXEMPTY | DT_RXBUSY | DT_RXBREAK);
  1052. nt.signal = SG_DCD;
  1053. if ((rc = stli_cmdwait(brdp, portp, A_SETNOTIFY, &nt,
  1054. sizeof(asynotify_t), 0)) < 0)
  1055. return(rc);
  1056. tty = portp->tty;
  1057. if (tty == (struct tty_struct *) NULL)
  1058. return(-ENODEV);
  1059. stli_mkasyport(portp, &aport, tty->termios);
  1060. if ((rc = stli_cmdwait(brdp, portp, A_SETPORT, &aport,
  1061. sizeof(asyport_t), 0)) < 0)
  1062. return(rc);
  1063. set_bit(ST_GETSIGS, &portp->state);
  1064. if ((rc = stli_cmdwait(brdp, portp, A_GETSIGNALS, &portp->asig,
  1065. sizeof(asysigs_t), 1)) < 0)
  1066. return(rc);
  1067. if (test_and_clear_bit(ST_GETSIGS, &portp->state))
  1068. portp->sigs = stli_mktiocm(portp->asig.sigvalue);
  1069. stli_mkasysigs(&portp->asig, 1, 1);
  1070. if ((rc = stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig,
  1071. sizeof(asysigs_t), 0)) < 0)
  1072. return(rc);
  1073. return(0);
  1074. }
  1075. /*****************************************************************************/
  1076. /*
  1077. * Send an open message to the slave. This will sleep waiting for the
  1078. * acknowledgement, so must have user context. We need to co-ordinate
  1079. * with close events here, since we don't want open and close events
  1080. * to overlap.
  1081. */
  1082. static int stli_rawopen(stlibrd_t *brdp, stliport_t *portp, unsigned long arg, int wait)
  1083. {
  1084. volatile cdkhdr_t *hdrp;
  1085. volatile cdkctrl_t *cp;
  1086. volatile unsigned char *bits;
  1087. unsigned long flags;
  1088. int rc;
  1089. #ifdef DEBUG
  1090. printk("stli_rawopen(brdp=%x,portp=%x,arg=%x,wait=%d)\n",
  1091. (int) brdp, (int) portp, (int) arg, wait);
  1092. #endif
  1093. /*
  1094. * Send a message to the slave to open this port.
  1095. */
  1096. save_flags(flags);
  1097. cli();
  1098. /*
  1099. * Slave is already closing this port. This can happen if a hangup
  1100. * occurs on this port. So we must wait until it is complete. The
  1101. * order of opens and closes may not be preserved across shared
  1102. * memory, so we must wait until it is complete.
  1103. */
  1104. wait_event_interruptible(portp->raw_wait,
  1105. !test_bit(ST_CLOSING, &portp->state));
  1106. if (signal_pending(current)) {
  1107. restore_flags(flags);
  1108. return -ERESTARTSYS;
  1109. }
  1110. /*
  1111. * Everything is ready now, so write the open message into shared
  1112. * memory. Once the message is in set the service bits to say that
  1113. * this port wants service.
  1114. */
  1115. EBRDENABLE(brdp);
  1116. cp = &((volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr))->ctrl;
  1117. cp->openarg = arg;
  1118. cp->open = 1;
  1119. hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
  1120. bits = ((volatile unsigned char *) hdrp) + brdp->slaveoffset +
  1121. portp->portidx;
  1122. *bits |= portp->portbit;
  1123. EBRDDISABLE(brdp);
  1124. if (wait == 0) {
  1125. restore_flags(flags);
  1126. return(0);
  1127. }
  1128. /*
  1129. * Slave is in action, so now we must wait for the open acknowledgment
  1130. * to come back.
  1131. */
  1132. rc = 0;
  1133. set_bit(ST_OPENING, &portp->state);
  1134. wait_event_interruptible(portp->raw_wait,
  1135. !test_bit(ST_OPENING, &portp->state));
  1136. if (signal_pending(current))
  1137. rc = -ERESTARTSYS;
  1138. restore_flags(flags);
  1139. if ((rc == 0) && (portp->rc != 0))
  1140. rc = -EIO;
  1141. return(rc);
  1142. }
  1143. /*****************************************************************************/
  1144. /*
  1145. * Send a close message to the slave. Normally this will sleep waiting
  1146. * for the acknowledgement, but if wait parameter is 0 it will not. If
  1147. * wait is true then must have user context (to sleep).
  1148. */
  1149. static int stli_rawclose(stlibrd_t *brdp, stliport_t *portp, unsigned long arg, int wait)
  1150. {
  1151. volatile cdkhdr_t *hdrp;
  1152. volatile cdkctrl_t *cp;
  1153. volatile unsigned char *bits;
  1154. unsigned long flags;
  1155. int rc;
  1156. #ifdef DEBUG
  1157. printk("stli_rawclose(brdp=%x,portp=%x,arg=%x,wait=%d)\n",
  1158. (int) brdp, (int) portp, (int) arg, wait);
  1159. #endif
  1160. save_flags(flags);
  1161. cli();
  1162. /*
  1163. * Slave is already closing this port. This can happen if a hangup
  1164. * occurs on this port.
  1165. */
  1166. if (wait) {
  1167. wait_event_interruptible(portp->raw_wait,
  1168. !test_bit(ST_CLOSING, &portp->state));
  1169. if (signal_pending(current)) {
  1170. restore_flags(flags);
  1171. return -ERESTARTSYS;
  1172. }
  1173. }
  1174. /*
  1175. * Write the close command into shared memory.
  1176. */
  1177. EBRDENABLE(brdp);
  1178. cp = &((volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr))->ctrl;
  1179. cp->closearg = arg;
  1180. cp->close = 1;
  1181. hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
  1182. bits = ((volatile unsigned char *) hdrp) + brdp->slaveoffset +
  1183. portp->portidx;
  1184. *bits |= portp->portbit;
  1185. EBRDDISABLE(brdp);
  1186. set_bit(ST_CLOSING, &portp->state);
  1187. if (wait == 0) {
  1188. restore_flags(flags);
  1189. return(0);
  1190. }
  1191. /*
  1192. * Slave is in action, so now we must wait for the open acknowledgment
  1193. * to come back.
  1194. */
  1195. rc = 0;
  1196. wait_event_interruptible(portp->raw_wait,
  1197. !test_bit(ST_CLOSING, &portp->state));
  1198. if (signal_pending(current))
  1199. rc = -ERESTARTSYS;
  1200. restore_flags(flags);
  1201. if ((rc == 0) && (portp->rc != 0))
  1202. rc = -EIO;
  1203. return(rc);
  1204. }
  1205. /*****************************************************************************/
  1206. /*
  1207. * Send a command to the slave and wait for the response. This must
  1208. * have user context (it sleeps). This routine is generic in that it
  1209. * can send any type of command. Its purpose is to wait for that command
  1210. * to complete (as opposed to initiating the command then returning).
  1211. */
  1212. static int stli_cmdwait(stlibrd_t *brdp, stliport_t *portp, unsigned long cmd, void *arg, int size, int copyback)
  1213. {
  1214. unsigned long flags;
  1215. #ifdef DEBUG
  1216. printk("stli_cmdwait(brdp=%x,portp=%x,cmd=%x,arg=%x,size=%d,"
  1217. "copyback=%d)\n", (int) brdp, (int) portp, (int) cmd,
  1218. (int) arg, size, copyback);
  1219. #endif
  1220. save_flags(flags);
  1221. cli();
  1222. wait_event_interruptible(portp->raw_wait,
  1223. !test_bit(ST_CMDING, &portp->state));
  1224. if (signal_pending(current)) {
  1225. restore_flags(flags);
  1226. return -ERESTARTSYS;
  1227. }
  1228. stli_sendcmd(brdp, portp, cmd, arg, size, copyback);
  1229. wait_event_interruptible(portp->raw_wait,
  1230. !test_bit(ST_CMDING, &portp->state));
  1231. if (signal_pending(current)) {
  1232. restore_flags(flags);
  1233. return -ERESTARTSYS;
  1234. }
  1235. restore_flags(flags);
  1236. if (portp->rc != 0)
  1237. return(-EIO);
  1238. return(0);
  1239. }
  1240. /*****************************************************************************/
  1241. /*
  1242. * Send the termios settings for this port to the slave. This sleeps
  1243. * waiting for the command to complete - so must have user context.
  1244. */
  1245. static int stli_setport(stliport_t *portp)
  1246. {
  1247. stlibrd_t *brdp;
  1248. asyport_t aport;
  1249. #ifdef DEBUG
  1250. printk("stli_setport(portp=%x)\n", (int) portp);
  1251. #endif
  1252. if (portp == (stliport_t *) NULL)
  1253. return(-ENODEV);
  1254. if (portp->tty == (struct tty_struct *) NULL)
  1255. return(-ENODEV);
  1256. if ((portp->brdnr < 0) && (portp->brdnr >= stli_nrbrds))
  1257. return(-ENODEV);
  1258. brdp = stli_brds[portp->brdnr];
  1259. if (brdp == (stlibrd_t *) NULL)
  1260. return(-ENODEV);
  1261. stli_mkasyport(portp, &aport, portp->tty->termios);
  1262. return(stli_cmdwait(brdp, portp, A_SETPORT, &aport, sizeof(asyport_t), 0));
  1263. }
  1264. /*****************************************************************************/
  1265. /*
  1266. * Possibly need to wait for carrier (DCD signal) to come high. Say
  1267. * maybe because if we are clocal then we don't need to wait...
  1268. */
  1269. static int stli_waitcarrier(stlibrd_t *brdp, stliport_t *portp, struct file *filp)
  1270. {
  1271. unsigned long flags;
  1272. int rc, doclocal;
  1273. #ifdef DEBUG
  1274. printk("stli_waitcarrier(brdp=%x,portp=%x,filp=%x)\n",
  1275. (int) brdp, (int) portp, (int) filp);
  1276. #endif
  1277. rc = 0;
  1278. doclocal = 0;
  1279. if (portp->tty->termios->c_cflag & CLOCAL)
  1280. doclocal++;
  1281. save_flags(flags);
  1282. cli();
  1283. portp->openwaitcnt++;
  1284. if (! tty_hung_up_p(filp))
  1285. portp->refcount--;
  1286. for (;;) {
  1287. stli_mkasysigs(&portp->asig, 1, 1);
  1288. if ((rc = stli_cmdwait(brdp, portp, A_SETSIGNALS,
  1289. &portp->asig, sizeof(asysigs_t), 0)) < 0)
  1290. break;
  1291. if (tty_hung_up_p(filp) ||
  1292. ((portp->flags & ASYNC_INITIALIZED) == 0)) {
  1293. if (portp->flags & ASYNC_HUP_NOTIFY)
  1294. rc = -EBUSY;
  1295. else
  1296. rc = -ERESTARTSYS;
  1297. break;
  1298. }
  1299. if (((portp->flags & ASYNC_CLOSING) == 0) &&
  1300. (doclocal || (portp->sigs & TIOCM_CD))) {
  1301. break;
  1302. }
  1303. if (signal_pending(current)) {
  1304. rc = -ERESTARTSYS;
  1305. break;
  1306. }
  1307. interruptible_sleep_on(&portp->open_wait);
  1308. }
  1309. if (! tty_hung_up_p(filp))
  1310. portp->refcount++;
  1311. portp->openwaitcnt--;
  1312. restore_flags(flags);
  1313. return(rc);
  1314. }
  1315. /*****************************************************************************/
  1316. /*
  1317. * Write routine. Take the data and put it in the shared memory ring
  1318. * queue. If port is not already sending chars then need to mark the
  1319. * service bits for this port.
  1320. */
  1321. static int stli_write(struct tty_struct *tty, const unsigned char *buf, int count)
  1322. {
  1323. volatile cdkasy_t *ap;
  1324. volatile cdkhdr_t *hdrp;
  1325. volatile unsigned char *bits;
  1326. unsigned char *shbuf, *chbuf;
  1327. stliport_t *portp;
  1328. stlibrd_t *brdp;
  1329. unsigned int len, stlen, head, tail, size;
  1330. unsigned long flags;
  1331. #ifdef DEBUG
  1332. printk("stli_write(tty=%x,buf=%x,count=%d)\n",
  1333. (int) tty, (int) buf, count);
  1334. #endif
  1335. if ((tty == (struct tty_struct *) NULL) ||
  1336. (stli_tmpwritebuf == (char *) NULL))
  1337. return(0);
  1338. if (tty == stli_txcooktty)
  1339. stli_flushchars(tty);
  1340. portp = tty->driver_data;
  1341. if (portp == (stliport_t *) NULL)
  1342. return(0);
  1343. if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
  1344. return(0);
  1345. brdp = stli_brds[portp->brdnr];
  1346. if (brdp == (stlibrd_t *) NULL)
  1347. return(0);
  1348. chbuf = (unsigned char *) buf;
  1349. /*
  1350. * All data is now local, shove as much as possible into shared memory.
  1351. */
  1352. save_flags(flags);
  1353. cli();
  1354. EBRDENABLE(brdp);
  1355. ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
  1356. head = (unsigned int) ap->txq.head;
  1357. tail = (unsigned int) ap->txq.tail;
  1358. if (tail != ((unsigned int) ap->txq.tail))
  1359. tail = (unsigned int) ap->txq.tail;
  1360. size = portp->txsize;
  1361. if (head >= tail) {
  1362. len = size - (head - tail) - 1;
  1363. stlen = size - head;
  1364. } else {
  1365. len = tail - head - 1;
  1366. stlen = len;
  1367. }
  1368. len = MIN(len, count);
  1369. count = 0;
  1370. shbuf = (char *) EBRDGETMEMPTR(brdp, portp->txoffset);
  1371. while (len > 0) {
  1372. stlen = MIN(len, stlen);
  1373. memcpy((shbuf + head), chbuf, stlen);
  1374. chbuf += stlen;
  1375. len -= stlen;
  1376. count += stlen;
  1377. head += stlen;
  1378. if (head >= size) {
  1379. head = 0;
  1380. stlen = tail;
  1381. }
  1382. }
  1383. ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
  1384. ap->txq.head = head;
  1385. if (test_bit(ST_TXBUSY, &portp->state)) {
  1386. if (ap->changed.data & DT_TXEMPTY)
  1387. ap->changed.data &= ~DT_TXEMPTY;
  1388. }
  1389. hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
  1390. bits = ((volatile unsigned char *) hdrp) + brdp->slaveoffset +
  1391. portp->portidx;
  1392. *bits |= portp->portbit;
  1393. set_bit(ST_TXBUSY, &portp->state);
  1394. EBRDDISABLE(brdp);
  1395. restore_flags(flags);
  1396. return(count);
  1397. }
  1398. /*****************************************************************************/
  1399. /*
  1400. * Output a single character. We put it into a temporary local buffer
  1401. * (for speed) then write out that buffer when the flushchars routine
  1402. * is called. There is a safety catch here so that if some other port
  1403. * writes chars before the current buffer has been, then we write them
  1404. * first them do the new ports.
  1405. */
  1406. static void stli_putchar(struct tty_struct *tty, unsigned char ch)
  1407. {
  1408. #ifdef DEBUG
  1409. printk("stli_putchar(tty=%x,ch=%x)\n", (int) tty, (int) ch);
  1410. #endif
  1411. if (tty == (struct tty_struct *) NULL)
  1412. return;
  1413. if (tty != stli_txcooktty) {
  1414. if (stli_txcooktty != (struct tty_struct *) NULL)
  1415. stli_flushchars(stli_txcooktty);
  1416. stli_txcooktty = tty;
  1417. }
  1418. stli_txcookbuf[stli_txcooksize++] = ch;
  1419. }
  1420. /*****************************************************************************/
  1421. /*
  1422. * Transfer characters from the local TX cooking buffer to the board.
  1423. * We sort of ignore the tty that gets passed in here. We rely on the
  1424. * info stored with the TX cook buffer to tell us which port to flush
  1425. * the data on. In any case we clean out the TX cook buffer, for re-use
  1426. * by someone else.
  1427. */
  1428. static void stli_flushchars(struct tty_struct *tty)
  1429. {
  1430. volatile cdkhdr_t *hdrp;
  1431. volatile unsigned char *bits;
  1432. volatile cdkasy_t *ap;
  1433. struct tty_struct *cooktty;
  1434. stliport_t *portp;
  1435. stlibrd_t *brdp;
  1436. unsigned int len, stlen, head, tail, size, count, cooksize;
  1437. unsigned char *buf, *shbuf;
  1438. unsigned long flags;
  1439. #ifdef DEBUG
  1440. printk("stli_flushchars(tty=%x)\n", (int) tty);
  1441. #endif
  1442. cooksize = stli_txcooksize;
  1443. cooktty = stli_txcooktty;
  1444. stli_txcooksize = 0;
  1445. stli_txcookrealsize = 0;
  1446. stli_txcooktty = (struct tty_struct *) NULL;
  1447. if (tty == (struct tty_struct *) NULL)
  1448. return;
  1449. if (cooktty == (struct tty_struct *) NULL)
  1450. return;
  1451. if (tty != cooktty)
  1452. tty = cooktty;
  1453. if (cooksize == 0)
  1454. return;
  1455. portp = tty->driver_data;
  1456. if (portp == (stliport_t *) NULL)
  1457. return;
  1458. if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
  1459. return;
  1460. brdp = stli_brds[portp->brdnr];
  1461. if (brdp == (stlibrd_t *) NULL)
  1462. return;
  1463. save_flags(flags);
  1464. cli();
  1465. EBRDENABLE(brdp);
  1466. ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
  1467. head = (unsigned int) ap->txq.head;
  1468. tail = (unsigned int) ap->txq.tail;
  1469. if (tail != ((unsigned int) ap->txq.tail))
  1470. tail = (unsigned int) ap->txq.tail;
  1471. size = portp->txsize;
  1472. if (head >= tail) {
  1473. len = size - (head - tail) - 1;
  1474. stlen = size - head;
  1475. } else {
  1476. len = tail - head - 1;
  1477. stlen = len;
  1478. }
  1479. len = MIN(len, cooksize);
  1480. count = 0;
  1481. shbuf = (char *) EBRDGETMEMPTR(brdp, portp->txoffset);
  1482. buf = stli_txcookbuf;
  1483. while (len > 0) {
  1484. stlen = MIN(len, stlen);
  1485. memcpy((shbuf + head), buf, stlen);
  1486. buf += stlen;
  1487. len -= stlen;
  1488. count += stlen;
  1489. head += stlen;
  1490. if (head >= size) {
  1491. head = 0;
  1492. stlen = tail;
  1493. }
  1494. }
  1495. ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
  1496. ap->txq.head = head;
  1497. if (test_bit(ST_TXBUSY, &portp->state)) {
  1498. if (ap->changed.data & DT_TXEMPTY)
  1499. ap->changed.data &= ~DT_TXEMPTY;
  1500. }
  1501. hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
  1502. bits = ((volatile unsigned char *) hdrp) + brdp->slaveoffset +
  1503. portp->portidx;
  1504. *bits |= portp->portbit;
  1505. set_bit(ST_TXBUSY, &portp->state);
  1506. EBRDDISABLE(brdp);
  1507. restore_flags(flags);
  1508. }
  1509. /*****************************************************************************/
  1510. static int stli_writeroom(struct tty_struct *tty)
  1511. {
  1512. volatile cdkasyrq_t *rp;
  1513. stliport_t *portp;
  1514. stlibrd_t *brdp;
  1515. unsigned int head, tail, len;
  1516. unsigned long flags;
  1517. #ifdef DEBUG
  1518. printk("stli_writeroom(tty=%x)\n", (int) tty);
  1519. #endif
  1520. if (tty == (struct tty_struct *) NULL)
  1521. return(0);
  1522. if (tty == stli_txcooktty) {
  1523. if (stli_txcookrealsize != 0) {
  1524. len = stli_txcookrealsize - stli_txcooksize;
  1525. return(len);
  1526. }
  1527. }
  1528. portp = tty->driver_data;
  1529. if (portp == (stliport_t *) NULL)
  1530. return(0);
  1531. if ((portp->brdnr <