/drivers/char/stallion.c

https://bitbucket.org/evzijst/gittest · C · 5197 lines · 3602 code · 765 blank · 830 comment · 795 complexity · 61f0d2e5e8c11ce6db5bedb3fbc920ec MD5 · raw file

Large files are truncated click here to view the full file

  1. /*****************************************************************************/
  2. /*
  3. * stallion.c -- stallion 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/cd1400.h>
  34. #include <linux/sc26198.h>
  35. #include <linux/comstats.h>
  36. #include <linux/stallion.h>
  37. #include <linux/ioport.h>
  38. #include <linux/init.h>
  39. #include <linux/smp_lock.h>
  40. #include <linux/devfs_fs_kernel.h>
  41. #include <linux/device.h>
  42. #include <linux/delay.h>
  43. #include <asm/io.h>
  44. #include <asm/uaccess.h>
  45. #ifdef CONFIG_PCI
  46. #include <linux/pci.h>
  47. #endif
  48. /*****************************************************************************/
  49. /*
  50. * Define different board types. Use the standard Stallion "assigned"
  51. * board numbers. Boards supported in this driver are abbreviated as
  52. * EIO = EasyIO and ECH = EasyConnection 8/32.
  53. */
  54. #define BRD_EASYIO 20
  55. #define BRD_ECH 21
  56. #define BRD_ECHMC 22
  57. #define BRD_ECHPCI 26
  58. #define BRD_ECH64PCI 27
  59. #define BRD_EASYIOPCI 28
  60. /*
  61. * Define a configuration structure to hold the board configuration.
  62. * Need to set this up in the code (for now) with the boards that are
  63. * to be configured into the system. This is what needs to be modified
  64. * when adding/removing/modifying boards. Each line entry in the
  65. * stl_brdconf[] array is a board. Each line contains io/irq/memory
  66. * ranges for that board (as well as what type of board it is).
  67. * Some examples:
  68. * { BRD_EASYIO, 0x2a0, 0, 0, 10, 0 },
  69. * This line would configure an EasyIO board (4 or 8, no difference),
  70. * at io address 2a0 and irq 10.
  71. * Another example:
  72. * { BRD_ECH, 0x2a8, 0x280, 0, 12, 0 },
  73. * This line will configure an EasyConnection 8/32 board at primary io
  74. * address 2a8, secondary io address 280 and irq 12.
  75. * Enter as many lines into this array as you want (only the first 4
  76. * will actually be used!). Any combination of EasyIO and EasyConnection
  77. * boards can be specified. EasyConnection 8/32 boards can share their
  78. * secondary io addresses between each other.
  79. *
  80. * NOTE: there is no need to put any entries in this table for PCI
  81. * boards. They will be found automatically by the driver - provided
  82. * PCI BIOS32 support is compiled into the kernel.
  83. */
  84. typedef struct {
  85. int brdtype;
  86. int ioaddr1;
  87. int ioaddr2;
  88. unsigned long memaddr;
  89. int irq;
  90. int irqtype;
  91. } stlconf_t;
  92. static stlconf_t stl_brdconf[] = {
  93. /*{ BRD_EASYIO, 0x2a0, 0, 0, 10, 0 },*/
  94. };
  95. static int stl_nrbrds = sizeof(stl_brdconf) / sizeof(stlconf_t);
  96. /*****************************************************************************/
  97. /*
  98. * Define some important driver characteristics. Device major numbers
  99. * allocated as per Linux Device Registry.
  100. */
  101. #ifndef STL_SIOMEMMAJOR
  102. #define STL_SIOMEMMAJOR 28
  103. #endif
  104. #ifndef STL_SERIALMAJOR
  105. #define STL_SERIALMAJOR 24
  106. #endif
  107. #ifndef STL_CALLOUTMAJOR
  108. #define STL_CALLOUTMAJOR 25
  109. #endif
  110. /*
  111. * Set the TX buffer size. Bigger is better, but we don't want
  112. * to chew too much memory with buffers!
  113. */
  114. #define STL_TXBUFLOW 512
  115. #define STL_TXBUFSIZE 4096
  116. /*****************************************************************************/
  117. /*
  118. * Define our local driver identity first. Set up stuff to deal with
  119. * all the local structures required by a serial tty driver.
  120. */
  121. static char *stl_drvtitle = "Stallion Multiport Serial Driver";
  122. static char *stl_drvname = "stallion";
  123. static char *stl_drvversion = "5.6.0";
  124. static struct tty_driver *stl_serial;
  125. /*
  126. * We will need to allocate a temporary write buffer for chars that
  127. * come direct from user space. The problem is that a copy from user
  128. * space might cause a page fault (typically on a system that is
  129. * swapping!). All ports will share one buffer - since if the system
  130. * is already swapping a shared buffer won't make things any worse.
  131. */
  132. static char *stl_tmpwritebuf;
  133. static DECLARE_MUTEX(stl_tmpwritesem);
  134. /*
  135. * Define a local default termios struct. All ports will be created
  136. * with this termios initially. Basically all it defines is a raw port
  137. * at 9600, 8 data bits, 1 stop bit.
  138. */
  139. static struct termios stl_deftermios = {
  140. .c_cflag = (B9600 | CS8 | CREAD | HUPCL | CLOCAL),
  141. .c_cc = INIT_C_CC,
  142. };
  143. /*
  144. * Define global stats structures. Not used often, and can be
  145. * re-used for each stats call.
  146. */
  147. static comstats_t stl_comstats;
  148. static combrd_t stl_brdstats;
  149. static stlbrd_t stl_dummybrd;
  150. static stlport_t stl_dummyport;
  151. /*
  152. * Define global place to put buffer overflow characters.
  153. */
  154. static char stl_unwanted[SC26198_RXFIFOSIZE];
  155. /*****************************************************************************/
  156. static stlbrd_t *stl_brds[STL_MAXBRDS];
  157. /*
  158. * Per board state flags. Used with the state field of the board struct.
  159. * Not really much here!
  160. */
  161. #define BRD_FOUND 0x1
  162. /*
  163. * Define the port structure istate flags. These set of flags are
  164. * modified at interrupt time - so setting and reseting them needs
  165. * to be atomic. Use the bit clear/setting routines for this.
  166. */
  167. #define ASYI_TXBUSY 1
  168. #define ASYI_TXLOW 2
  169. #define ASYI_DCDCHANGE 3
  170. #define ASYI_TXFLOWED 4
  171. /*
  172. * Define an array of board names as printable strings. Handy for
  173. * referencing boards when printing trace and stuff.
  174. */
  175. static char *stl_brdnames[] = {
  176. (char *) NULL,
  177. (char *) NULL,
  178. (char *) NULL,
  179. (char *) NULL,
  180. (char *) NULL,
  181. (char *) NULL,
  182. (char *) NULL,
  183. (char *) NULL,
  184. (char *) NULL,
  185. (char *) NULL,
  186. (char *) NULL,
  187. (char *) NULL,
  188. (char *) NULL,
  189. (char *) NULL,
  190. (char *) NULL,
  191. (char *) NULL,
  192. (char *) NULL,
  193. (char *) NULL,
  194. (char *) NULL,
  195. (char *) NULL,
  196. "EasyIO",
  197. "EC8/32-AT",
  198. "EC8/32-MC",
  199. (char *) NULL,
  200. (char *) NULL,
  201. (char *) NULL,
  202. "EC8/32-PCI",
  203. "EC8/64-PCI",
  204. "EasyIO-PCI",
  205. };
  206. /*****************************************************************************/
  207. /*
  208. * Define some string labels for arguments passed from the module
  209. * load line. These allow for easy board definitions, and easy
  210. * modification of the io, memory and irq resoucres.
  211. */
  212. static int stl_nargs = 0;
  213. static char *board0[4];
  214. static char *board1[4];
  215. static char *board2[4];
  216. static char *board3[4];
  217. static char **stl_brdsp[] = {
  218. (char **) &board0,
  219. (char **) &board1,
  220. (char **) &board2,
  221. (char **) &board3
  222. };
  223. /*
  224. * Define a set of common board names, and types. This is used to
  225. * parse any module arguments.
  226. */
  227. typedef struct stlbrdtype {
  228. char *name;
  229. int type;
  230. } stlbrdtype_t;
  231. static stlbrdtype_t stl_brdstr[] = {
  232. { "easyio", BRD_EASYIO },
  233. { "eio", BRD_EASYIO },
  234. { "20", BRD_EASYIO },
  235. { "ec8/32", BRD_ECH },
  236. { "ec8/32-at", BRD_ECH },
  237. { "ec8/32-isa", BRD_ECH },
  238. { "ech", BRD_ECH },
  239. { "echat", BRD_ECH },
  240. { "21", BRD_ECH },
  241. { "ec8/32-mc", BRD_ECHMC },
  242. { "ec8/32-mca", BRD_ECHMC },
  243. { "echmc", BRD_ECHMC },
  244. { "echmca", BRD_ECHMC },
  245. { "22", BRD_ECHMC },
  246. { "ec8/32-pc", BRD_ECHPCI },
  247. { "ec8/32-pci", BRD_ECHPCI },
  248. { "26", BRD_ECHPCI },
  249. { "ec8/64-pc", BRD_ECH64PCI },
  250. { "ec8/64-pci", BRD_ECH64PCI },
  251. { "ech-pci", BRD_ECH64PCI },
  252. { "echpci", BRD_ECH64PCI },
  253. { "echpc", BRD_ECH64PCI },
  254. { "27", BRD_ECH64PCI },
  255. { "easyio-pc", BRD_EASYIOPCI },
  256. { "easyio-pci", BRD_EASYIOPCI },
  257. { "eio-pci", BRD_EASYIOPCI },
  258. { "eiopci", BRD_EASYIOPCI },
  259. { "28", BRD_EASYIOPCI },
  260. };
  261. /*
  262. * Define the module agruments.
  263. */
  264. MODULE_AUTHOR("Greg Ungerer");
  265. MODULE_DESCRIPTION("Stallion Multiport Serial Driver");
  266. MODULE_LICENSE("GPL");
  267. module_param_array(board0, charp, &stl_nargs, 0);
  268. MODULE_PARM_DESC(board0, "Board 0 config -> name[,ioaddr[,ioaddr2][,irq]]");
  269. module_param_array(board1, charp, &stl_nargs, 0);
  270. MODULE_PARM_DESC(board1, "Board 1 config -> name[,ioaddr[,ioaddr2][,irq]]");
  271. module_param_array(board2, charp, &stl_nargs, 0);
  272. MODULE_PARM_DESC(board2, "Board 2 config -> name[,ioaddr[,ioaddr2][,irq]]");
  273. module_param_array(board3, charp, &stl_nargs, 0);
  274. MODULE_PARM_DESC(board3, "Board 3 config -> name[,ioaddr[,ioaddr2][,irq]]");
  275. /*****************************************************************************/
  276. /*
  277. * Hardware ID bits for the EasyIO and ECH boards. These defines apply
  278. * to the directly accessible io ports of these boards (not the uarts -
  279. * they are in cd1400.h and sc26198.h).
  280. */
  281. #define EIO_8PORTRS 0x04
  282. #define EIO_4PORTRS 0x05
  283. #define EIO_8PORTDI 0x00
  284. #define EIO_8PORTM 0x06
  285. #define EIO_MK3 0x03
  286. #define EIO_IDBITMASK 0x07
  287. #define EIO_BRDMASK 0xf0
  288. #define ID_BRD4 0x10
  289. #define ID_BRD8 0x20
  290. #define ID_BRD16 0x30
  291. #define EIO_INTRPEND 0x08
  292. #define EIO_INTEDGE 0x00
  293. #define EIO_INTLEVEL 0x08
  294. #define EIO_0WS 0x10
  295. #define ECH_ID 0xa0
  296. #define ECH_IDBITMASK 0xe0
  297. #define ECH_BRDENABLE 0x08
  298. #define ECH_BRDDISABLE 0x00
  299. #define ECH_INTENABLE 0x01
  300. #define ECH_INTDISABLE 0x00
  301. #define ECH_INTLEVEL 0x02
  302. #define ECH_INTEDGE 0x00
  303. #define ECH_INTRPEND 0x01
  304. #define ECH_BRDRESET 0x01
  305. #define ECHMC_INTENABLE 0x01
  306. #define ECHMC_BRDRESET 0x02
  307. #define ECH_PNLSTATUS 2
  308. #define ECH_PNL16PORT 0x20
  309. #define ECH_PNLIDMASK 0x07
  310. #define ECH_PNLXPID 0x40
  311. #define ECH_PNLINTRPEND 0x80
  312. #define ECH_ADDR2MASK 0x1e0
  313. /*
  314. * Define the vector mapping bits for the programmable interrupt board
  315. * hardware. These bits encode the interrupt for the board to use - it
  316. * is software selectable (except the EIO-8M).
  317. */
  318. static unsigned char stl_vecmap[] = {
  319. 0xff, 0xff, 0xff, 0x04, 0x06, 0x05, 0xff, 0x07,
  320. 0xff, 0xff, 0x00, 0x02, 0x01, 0xff, 0xff, 0x03
  321. };
  322. /*
  323. * Set up enable and disable macros for the ECH boards. They require
  324. * the secondary io address space to be activated and deactivated.
  325. * This way all ECH boards can share their secondary io region.
  326. * If this is an ECH-PCI board then also need to set the page pointer
  327. * to point to the correct page.
  328. */
  329. #define BRDENABLE(brdnr,pagenr) \
  330. if (stl_brds[(brdnr)]->brdtype == BRD_ECH) \
  331. outb((stl_brds[(brdnr)]->ioctrlval | ECH_BRDENABLE), \
  332. stl_brds[(brdnr)]->ioctrl); \
  333. else if (stl_brds[(brdnr)]->brdtype == BRD_ECHPCI) \
  334. outb((pagenr), stl_brds[(brdnr)]->ioctrl);
  335. #define BRDDISABLE(brdnr) \
  336. if (stl_brds[(brdnr)]->brdtype == BRD_ECH) \
  337. outb((stl_brds[(brdnr)]->ioctrlval | ECH_BRDDISABLE), \
  338. stl_brds[(brdnr)]->ioctrl);
  339. #define STL_CD1400MAXBAUD 230400
  340. #define STL_SC26198MAXBAUD 460800
  341. #define STL_BAUDBASE 115200
  342. #define STL_CLOSEDELAY (5 * HZ / 10)
  343. /*****************************************************************************/
  344. #ifdef CONFIG_PCI
  345. /*
  346. * Define the Stallion PCI vendor and device IDs.
  347. */
  348. #ifndef PCI_VENDOR_ID_STALLION
  349. #define PCI_VENDOR_ID_STALLION 0x124d
  350. #endif
  351. #ifndef PCI_DEVICE_ID_ECHPCI832
  352. #define PCI_DEVICE_ID_ECHPCI832 0x0000
  353. #endif
  354. #ifndef PCI_DEVICE_ID_ECHPCI864
  355. #define PCI_DEVICE_ID_ECHPCI864 0x0002
  356. #endif
  357. #ifndef PCI_DEVICE_ID_EIOPCI
  358. #define PCI_DEVICE_ID_EIOPCI 0x0003
  359. #endif
  360. /*
  361. * Define structure to hold all Stallion PCI boards.
  362. */
  363. typedef struct stlpcibrd {
  364. unsigned short vendid;
  365. unsigned short devid;
  366. int brdtype;
  367. } stlpcibrd_t;
  368. static stlpcibrd_t stl_pcibrds[] = {
  369. { PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_ECHPCI864, BRD_ECH64PCI },
  370. { PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_EIOPCI, BRD_EASYIOPCI },
  371. { PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_ECHPCI832, BRD_ECHPCI },
  372. { PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_87410, BRD_ECHPCI },
  373. };
  374. static int stl_nrpcibrds = sizeof(stl_pcibrds) / sizeof(stlpcibrd_t);
  375. #endif
  376. /*****************************************************************************/
  377. /*
  378. * Define macros to extract a brd/port number from a minor number.
  379. */
  380. #define MINOR2BRD(min) (((min) & 0xc0) >> 6)
  381. #define MINOR2PORT(min) ((min) & 0x3f)
  382. /*
  383. * Define a baud rate table that converts termios baud rate selector
  384. * into the actual baud rate value. All baud rate calculations are
  385. * based on the actual baud rate required.
  386. */
  387. static unsigned int stl_baudrates[] = {
  388. 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
  389. 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600
  390. };
  391. /*
  392. * Define some handy local macros...
  393. */
  394. #undef MIN
  395. #define MIN(a,b) (((a) <= (b)) ? (a) : (b))
  396. #undef TOLOWER
  397. #define TOLOWER(x) ((((x) >= 'A') && ((x) <= 'Z')) ? ((x) + 0x20) : (x))
  398. /*****************************************************************************/
  399. /*
  400. * Declare all those functions in this driver!
  401. */
  402. static void stl_argbrds(void);
  403. static int stl_parsebrd(stlconf_t *confp, char **argp);
  404. static unsigned long stl_atol(char *str);
  405. int stl_init(void);
  406. static int stl_open(struct tty_struct *tty, struct file *filp);
  407. static void stl_close(struct tty_struct *tty, struct file *filp);
  408. static int stl_write(struct tty_struct *tty, const unsigned char *buf, int count);
  409. static void stl_putchar(struct tty_struct *tty, unsigned char ch);
  410. static void stl_flushchars(struct tty_struct *tty);
  411. static int stl_writeroom(struct tty_struct *tty);
  412. static int stl_charsinbuffer(struct tty_struct *tty);
  413. static int stl_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
  414. static void stl_settermios(struct tty_struct *tty, struct termios *old);
  415. static void stl_throttle(struct tty_struct *tty);
  416. static void stl_unthrottle(struct tty_struct *tty);
  417. static void stl_stop(struct tty_struct *tty);
  418. static void stl_start(struct tty_struct *tty);
  419. static void stl_flushbuffer(struct tty_struct *tty);
  420. static void stl_breakctl(struct tty_struct *tty, int state);
  421. static void stl_waituntilsent(struct tty_struct *tty, int timeout);
  422. static void stl_sendxchar(struct tty_struct *tty, char ch);
  423. static void stl_hangup(struct tty_struct *tty);
  424. static int stl_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, unsigned long arg);
  425. static int stl_portinfo(stlport_t *portp, int portnr, char *pos);
  426. static int stl_readproc(char *page, char **start, off_t off, int count, int *eof, void *data);
  427. static int stl_brdinit(stlbrd_t *brdp);
  428. static int stl_initports(stlbrd_t *brdp, stlpanel_t *panelp);
  429. static int stl_getserial(stlport_t *portp, struct serial_struct __user *sp);
  430. static int stl_setserial(stlport_t *portp, struct serial_struct __user *sp);
  431. static int stl_getbrdstats(combrd_t __user *bp);
  432. static int stl_getportstats(stlport_t *portp, comstats_t __user *cp);
  433. static int stl_clrportstats(stlport_t *portp, comstats_t __user *cp);
  434. static int stl_getportstruct(stlport_t __user *arg);
  435. static int stl_getbrdstruct(stlbrd_t __user *arg);
  436. static int stl_waitcarrier(stlport_t *portp, struct file *filp);
  437. static int stl_eiointr(stlbrd_t *brdp);
  438. static int stl_echatintr(stlbrd_t *brdp);
  439. static int stl_echmcaintr(stlbrd_t *brdp);
  440. static int stl_echpciintr(stlbrd_t *brdp);
  441. static int stl_echpci64intr(stlbrd_t *brdp);
  442. static void stl_offintr(void *private);
  443. static void *stl_memalloc(int len);
  444. static stlbrd_t *stl_allocbrd(void);
  445. static stlport_t *stl_getport(int brdnr, int panelnr, int portnr);
  446. static inline int stl_initbrds(void);
  447. static inline int stl_initeio(stlbrd_t *brdp);
  448. static inline int stl_initech(stlbrd_t *brdp);
  449. static inline int stl_getbrdnr(void);
  450. #ifdef CONFIG_PCI
  451. static inline int stl_findpcibrds(void);
  452. static inline int stl_initpcibrd(int brdtype, struct pci_dev *devp);
  453. #endif
  454. /*
  455. * CD1400 uart specific handling functions.
  456. */
  457. static void stl_cd1400setreg(stlport_t *portp, int regnr, int value);
  458. static int stl_cd1400getreg(stlport_t *portp, int regnr);
  459. static int stl_cd1400updatereg(stlport_t *portp, int regnr, int value);
  460. static int stl_cd1400panelinit(stlbrd_t *brdp, stlpanel_t *panelp);
  461. static void stl_cd1400portinit(stlbrd_t *brdp, stlpanel_t *panelp, stlport_t *portp);
  462. static void stl_cd1400setport(stlport_t *portp, struct termios *tiosp);
  463. static int stl_cd1400getsignals(stlport_t *portp);
  464. static void stl_cd1400setsignals(stlport_t *portp, int dtr, int rts);
  465. static void stl_cd1400ccrwait(stlport_t *portp);
  466. static void stl_cd1400enablerxtx(stlport_t *portp, int rx, int tx);
  467. static void stl_cd1400startrxtx(stlport_t *portp, int rx, int tx);
  468. static void stl_cd1400disableintrs(stlport_t *portp);
  469. static void stl_cd1400sendbreak(stlport_t *portp, int len);
  470. static void stl_cd1400flowctrl(stlport_t *portp, int state);
  471. static void stl_cd1400sendflow(stlport_t *portp, int state);
  472. static void stl_cd1400flush(stlport_t *portp);
  473. static int stl_cd1400datastate(stlport_t *portp);
  474. static void stl_cd1400eiointr(stlpanel_t *panelp, unsigned int iobase);
  475. static void stl_cd1400echintr(stlpanel_t *panelp, unsigned int iobase);
  476. static void stl_cd1400txisr(stlpanel_t *panelp, int ioaddr);
  477. static void stl_cd1400rxisr(stlpanel_t *panelp, int ioaddr);
  478. static void stl_cd1400mdmisr(stlpanel_t *panelp, int ioaddr);
  479. static inline int stl_cd1400breakisr(stlport_t *portp, int ioaddr);
  480. /*
  481. * SC26198 uart specific handling functions.
  482. */
  483. static void stl_sc26198setreg(stlport_t *portp, int regnr, int value);
  484. static int stl_sc26198getreg(stlport_t *portp, int regnr);
  485. static int stl_sc26198updatereg(stlport_t *portp, int regnr, int value);
  486. static int stl_sc26198getglobreg(stlport_t *portp, int regnr);
  487. static int stl_sc26198panelinit(stlbrd_t *brdp, stlpanel_t *panelp);
  488. static void stl_sc26198portinit(stlbrd_t *brdp, stlpanel_t *panelp, stlport_t *portp);
  489. static void stl_sc26198setport(stlport_t *portp, struct termios *tiosp);
  490. static int stl_sc26198getsignals(stlport_t *portp);
  491. static void stl_sc26198setsignals(stlport_t *portp, int dtr, int rts);
  492. static void stl_sc26198enablerxtx(stlport_t *portp, int rx, int tx);
  493. static void stl_sc26198startrxtx(stlport_t *portp, int rx, int tx);
  494. static void stl_sc26198disableintrs(stlport_t *portp);
  495. static void stl_sc26198sendbreak(stlport_t *portp, int len);
  496. static void stl_sc26198flowctrl(stlport_t *portp, int state);
  497. static void stl_sc26198sendflow(stlport_t *portp, int state);
  498. static void stl_sc26198flush(stlport_t *portp);
  499. static int stl_sc26198datastate(stlport_t *portp);
  500. static void stl_sc26198wait(stlport_t *portp);
  501. static void stl_sc26198txunflow(stlport_t *portp, struct tty_struct *tty);
  502. static void stl_sc26198intr(stlpanel_t *panelp, unsigned int iobase);
  503. static void stl_sc26198txisr(stlport_t *port);
  504. static void stl_sc26198rxisr(stlport_t *port, unsigned int iack);
  505. static void stl_sc26198rxbadch(stlport_t *portp, unsigned char status, char ch);
  506. static void stl_sc26198rxbadchars(stlport_t *portp);
  507. static void stl_sc26198otherisr(stlport_t *port, unsigned int iack);
  508. /*****************************************************************************/
  509. /*
  510. * Generic UART support structure.
  511. */
  512. typedef struct uart {
  513. int (*panelinit)(stlbrd_t *brdp, stlpanel_t *panelp);
  514. void (*portinit)(stlbrd_t *brdp, stlpanel_t *panelp, stlport_t *portp);
  515. void (*setport)(stlport_t *portp, struct termios *tiosp);
  516. int (*getsignals)(stlport_t *portp);
  517. void (*setsignals)(stlport_t *portp, int dtr, int rts);
  518. void (*enablerxtx)(stlport_t *portp, int rx, int tx);
  519. void (*startrxtx)(stlport_t *portp, int rx, int tx);
  520. void (*disableintrs)(stlport_t *portp);
  521. void (*sendbreak)(stlport_t *portp, int len);
  522. void (*flowctrl)(stlport_t *portp, int state);
  523. void (*sendflow)(stlport_t *portp, int state);
  524. void (*flush)(stlport_t *portp);
  525. int (*datastate)(stlport_t *portp);
  526. void (*intr)(stlpanel_t *panelp, unsigned int iobase);
  527. } uart_t;
  528. /*
  529. * Define some macros to make calling these functions nice and clean.
  530. */
  531. #define stl_panelinit (* ((uart_t *) panelp->uartp)->panelinit)
  532. #define stl_portinit (* ((uart_t *) portp->uartp)->portinit)
  533. #define stl_setport (* ((uart_t *) portp->uartp)->setport)
  534. #define stl_getsignals (* ((uart_t *) portp->uartp)->getsignals)
  535. #define stl_setsignals (* ((uart_t *) portp->uartp)->setsignals)
  536. #define stl_enablerxtx (* ((uart_t *) portp->uartp)->enablerxtx)
  537. #define stl_startrxtx (* ((uart_t *) portp->uartp)->startrxtx)
  538. #define stl_disableintrs (* ((uart_t *) portp->uartp)->disableintrs)
  539. #define stl_sendbreak (* ((uart_t *) portp->uartp)->sendbreak)
  540. #define stl_flowctrl (* ((uart_t *) portp->uartp)->flowctrl)
  541. #define stl_sendflow (* ((uart_t *) portp->uartp)->sendflow)
  542. #define stl_flush (* ((uart_t *) portp->uartp)->flush)
  543. #define stl_datastate (* ((uart_t *) portp->uartp)->datastate)
  544. /*****************************************************************************/
  545. /*
  546. * CD1400 UART specific data initialization.
  547. */
  548. static uart_t stl_cd1400uart = {
  549. stl_cd1400panelinit,
  550. stl_cd1400portinit,
  551. stl_cd1400setport,
  552. stl_cd1400getsignals,
  553. stl_cd1400setsignals,
  554. stl_cd1400enablerxtx,
  555. stl_cd1400startrxtx,
  556. stl_cd1400disableintrs,
  557. stl_cd1400sendbreak,
  558. stl_cd1400flowctrl,
  559. stl_cd1400sendflow,
  560. stl_cd1400flush,
  561. stl_cd1400datastate,
  562. stl_cd1400eiointr
  563. };
  564. /*
  565. * Define the offsets within the register bank of a cd1400 based panel.
  566. * These io address offsets are common to the EasyIO board as well.
  567. */
  568. #define EREG_ADDR 0
  569. #define EREG_DATA 4
  570. #define EREG_RXACK 5
  571. #define EREG_TXACK 6
  572. #define EREG_MDACK 7
  573. #define EREG_BANKSIZE 8
  574. #define CD1400_CLK 25000000
  575. #define CD1400_CLK8M 20000000
  576. /*
  577. * Define the cd1400 baud rate clocks. These are used when calculating
  578. * what clock and divisor to use for the required baud rate. Also
  579. * define the maximum baud rate allowed, and the default base baud.
  580. */
  581. static int stl_cd1400clkdivs[] = {
  582. CD1400_CLK0, CD1400_CLK1, CD1400_CLK2, CD1400_CLK3, CD1400_CLK4
  583. };
  584. /*****************************************************************************/
  585. /*
  586. * SC26198 UART specific data initization.
  587. */
  588. static uart_t stl_sc26198uart = {
  589. stl_sc26198panelinit,
  590. stl_sc26198portinit,
  591. stl_sc26198setport,
  592. stl_sc26198getsignals,
  593. stl_sc26198setsignals,
  594. stl_sc26198enablerxtx,
  595. stl_sc26198startrxtx,
  596. stl_sc26198disableintrs,
  597. stl_sc26198sendbreak,
  598. stl_sc26198flowctrl,
  599. stl_sc26198sendflow,
  600. stl_sc26198flush,
  601. stl_sc26198datastate,
  602. stl_sc26198intr
  603. };
  604. /*
  605. * Define the offsets within the register bank of a sc26198 based panel.
  606. */
  607. #define XP_DATA 0
  608. #define XP_ADDR 1
  609. #define XP_MODID 2
  610. #define XP_STATUS 2
  611. #define XP_IACK 3
  612. #define XP_BANKSIZE 4
  613. /*
  614. * Define the sc26198 baud rate table. Offsets within the table
  615. * represent the actual baud rate selector of sc26198 registers.
  616. */
  617. static unsigned int sc26198_baudtable[] = {
  618. 50, 75, 150, 200, 300, 450, 600, 900, 1200, 1800, 2400, 3600,
  619. 4800, 7200, 9600, 14400, 19200, 28800, 38400, 57600, 115200,
  620. 230400, 460800, 921600
  621. };
  622. #define SC26198_NRBAUDS (sizeof(sc26198_baudtable) / sizeof(unsigned int))
  623. /*****************************************************************************/
  624. /*
  625. * Define the driver info for a user level control device. Used mainly
  626. * to get at port stats - only not using the port device itself.
  627. */
  628. static struct file_operations stl_fsiomem = {
  629. .owner = THIS_MODULE,
  630. .ioctl = stl_memioctl,
  631. };
  632. /*****************************************************************************/
  633. static struct class_simple *stallion_class;
  634. /*
  635. * Loadable module initialization stuff.
  636. */
  637. static int __init stallion_module_init(void)
  638. {
  639. unsigned long flags;
  640. #ifdef DEBUG
  641. printk("init_module()\n");
  642. #endif
  643. save_flags(flags);
  644. cli();
  645. stl_init();
  646. restore_flags(flags);
  647. return(0);
  648. }
  649. /*****************************************************************************/
  650. static void __exit stallion_module_exit(void)
  651. {
  652. stlbrd_t *brdp;
  653. stlpanel_t *panelp;
  654. stlport_t *portp;
  655. unsigned long flags;
  656. int i, j, k;
  657. #ifdef DEBUG
  658. printk("cleanup_module()\n");
  659. #endif
  660. printk(KERN_INFO "Unloading %s: version %s\n", stl_drvtitle,
  661. stl_drvversion);
  662. save_flags(flags);
  663. cli();
  664. /*
  665. * Free up all allocated resources used by the ports. This includes
  666. * memory and interrupts. As part of this process we will also do
  667. * a hangup on every open port - to try to flush out any processes
  668. * hanging onto ports.
  669. */
  670. i = tty_unregister_driver(stl_serial);
  671. put_tty_driver(stl_serial);
  672. if (i) {
  673. printk("STALLION: failed to un-register tty driver, "
  674. "errno=%d\n", -i);
  675. restore_flags(flags);
  676. return;
  677. }
  678. for (i = 0; i < 4; i++) {
  679. devfs_remove("staliomem/%d", i);
  680. class_simple_device_remove(MKDEV(STL_SIOMEMMAJOR, i));
  681. }
  682. devfs_remove("staliomem");
  683. if ((i = unregister_chrdev(STL_SIOMEMMAJOR, "staliomem")))
  684. printk("STALLION: failed to un-register serial memory device, "
  685. "errno=%d\n", -i);
  686. class_simple_destroy(stallion_class);
  687. if (stl_tmpwritebuf != (char *) NULL)
  688. kfree(stl_tmpwritebuf);
  689. for (i = 0; (i < stl_nrbrds); i++) {
  690. if ((brdp = stl_brds[i]) == (stlbrd_t *) NULL)
  691. continue;
  692. free_irq(brdp->irq, brdp);
  693. for (j = 0; (j < STL_MAXPANELS); j++) {
  694. panelp = brdp->panels[j];
  695. if (panelp == (stlpanel_t *) NULL)
  696. continue;
  697. for (k = 0; (k < STL_PORTSPERPANEL); k++) {
  698. portp = panelp->ports[k];
  699. if (portp == (stlport_t *) NULL)
  700. continue;
  701. if (portp->tty != (struct tty_struct *) NULL)
  702. stl_hangup(portp->tty);
  703. if (portp->tx.buf != (char *) NULL)
  704. kfree(portp->tx.buf);
  705. kfree(portp);
  706. }
  707. kfree(panelp);
  708. }
  709. release_region(brdp->ioaddr1, brdp->iosize1);
  710. if (brdp->iosize2 > 0)
  711. release_region(brdp->ioaddr2, brdp->iosize2);
  712. kfree(brdp);
  713. stl_brds[i] = (stlbrd_t *) NULL;
  714. }
  715. restore_flags(flags);
  716. }
  717. module_init(stallion_module_init);
  718. module_exit(stallion_module_exit);
  719. /*****************************************************************************/
  720. /*
  721. * Check for any arguments passed in on the module load command line.
  722. */
  723. static void stl_argbrds(void)
  724. {
  725. stlconf_t conf;
  726. stlbrd_t *brdp;
  727. int i;
  728. #ifdef DEBUG
  729. printk("stl_argbrds()\n");
  730. #endif
  731. for (i = stl_nrbrds; (i < stl_nargs); i++) {
  732. memset(&conf, 0, sizeof(conf));
  733. if (stl_parsebrd(&conf, stl_brdsp[i]) == 0)
  734. continue;
  735. if ((brdp = stl_allocbrd()) == (stlbrd_t *) NULL)
  736. continue;
  737. stl_nrbrds = i + 1;
  738. brdp->brdnr = i;
  739. brdp->brdtype = conf.brdtype;
  740. brdp->ioaddr1 = conf.ioaddr1;
  741. brdp->ioaddr2 = conf.ioaddr2;
  742. brdp->irq = conf.irq;
  743. brdp->irqtype = conf.irqtype;
  744. stl_brdinit(brdp);
  745. }
  746. }
  747. /*****************************************************************************/
  748. /*
  749. * Convert an ascii string number into an unsigned long.
  750. */
  751. static unsigned long stl_atol(char *str)
  752. {
  753. unsigned long val;
  754. int base, c;
  755. char *sp;
  756. val = 0;
  757. sp = str;
  758. if ((*sp == '0') && (*(sp+1) == 'x')) {
  759. base = 16;
  760. sp += 2;
  761. } else if (*sp == '0') {
  762. base = 8;
  763. sp++;
  764. } else {
  765. base = 10;
  766. }
  767. for (; (*sp != 0); sp++) {
  768. c = (*sp > '9') ? (TOLOWER(*sp) - 'a' + 10) : (*sp - '0');
  769. if ((c < 0) || (c >= base)) {
  770. printk("STALLION: invalid argument %s\n", str);
  771. val = 0;
  772. break;
  773. }
  774. val = (val * base) + c;
  775. }
  776. return(val);
  777. }
  778. /*****************************************************************************/
  779. /*
  780. * Parse the supplied argument string, into the board conf struct.
  781. */
  782. static int stl_parsebrd(stlconf_t *confp, char **argp)
  783. {
  784. char *sp;
  785. int nrbrdnames, i;
  786. #ifdef DEBUG
  787. printk("stl_parsebrd(confp=%x,argp=%x)\n", (int) confp, (int) argp);
  788. #endif
  789. if ((argp[0] == (char *) NULL) || (*argp[0] == 0))
  790. return(0);
  791. for (sp = argp[0], i = 0; ((*sp != 0) && (i < 25)); sp++, i++)
  792. *sp = TOLOWER(*sp);
  793. nrbrdnames = sizeof(stl_brdstr) / sizeof(stlbrdtype_t);
  794. for (i = 0; (i < nrbrdnames); i++) {
  795. if (strcmp(stl_brdstr[i].name, argp[0]) == 0)
  796. break;
  797. }
  798. if (i >= nrbrdnames) {
  799. printk("STALLION: unknown board name, %s?\n", argp[0]);
  800. return(0);
  801. }
  802. confp->brdtype = stl_brdstr[i].type;
  803. i = 1;
  804. if ((argp[i] != (char *) NULL) && (*argp[i] != 0))
  805. confp->ioaddr1 = stl_atol(argp[i]);
  806. i++;
  807. if (confp->brdtype == BRD_ECH) {
  808. if ((argp[i] != (char *) NULL) && (*argp[i] != 0))
  809. confp->ioaddr2 = stl_atol(argp[i]);
  810. i++;
  811. }
  812. if ((argp[i] != (char *) NULL) && (*argp[i] != 0))
  813. confp->irq = stl_atol(argp[i]);
  814. return(1);
  815. }
  816. /*****************************************************************************/
  817. /*
  818. * Local driver kernel memory allocation routine.
  819. */
  820. static void *stl_memalloc(int len)
  821. {
  822. return((void *) kmalloc(len, GFP_KERNEL));
  823. }
  824. /*****************************************************************************/
  825. /*
  826. * Allocate a new board structure. Fill out the basic info in it.
  827. */
  828. static stlbrd_t *stl_allocbrd(void)
  829. {
  830. stlbrd_t *brdp;
  831. brdp = (stlbrd_t *) stl_memalloc(sizeof(stlbrd_t));
  832. if (brdp == (stlbrd_t *) NULL) {
  833. printk("STALLION: failed to allocate memory (size=%d)\n",
  834. sizeof(stlbrd_t));
  835. return((stlbrd_t *) NULL);
  836. }
  837. memset(brdp, 0, sizeof(stlbrd_t));
  838. brdp->magic = STL_BOARDMAGIC;
  839. return(brdp);
  840. }
  841. /*****************************************************************************/
  842. static int stl_open(struct tty_struct *tty, struct file *filp)
  843. {
  844. stlport_t *portp;
  845. stlbrd_t *brdp;
  846. unsigned int minordev;
  847. int brdnr, panelnr, portnr, rc;
  848. #ifdef DEBUG
  849. printk("stl_open(tty=%x,filp=%x): device=%s\n", (int) tty,
  850. (int) filp, tty->name);
  851. #endif
  852. minordev = tty->index;
  853. brdnr = MINOR2BRD(minordev);
  854. if (brdnr >= stl_nrbrds)
  855. return(-ENODEV);
  856. brdp = stl_brds[brdnr];
  857. if (brdp == (stlbrd_t *) NULL)
  858. return(-ENODEV);
  859. minordev = MINOR2PORT(minordev);
  860. for (portnr = -1, panelnr = 0; (panelnr < STL_MAXPANELS); panelnr++) {
  861. if (brdp->panels[panelnr] == (stlpanel_t *) NULL)
  862. break;
  863. if (minordev < brdp->panels[panelnr]->nrports) {
  864. portnr = minordev;
  865. break;
  866. }
  867. minordev -= brdp->panels[panelnr]->nrports;
  868. }
  869. if (portnr < 0)
  870. return(-ENODEV);
  871. portp = brdp->panels[panelnr]->ports[portnr];
  872. if (portp == (stlport_t *) NULL)
  873. return(-ENODEV);
  874. /*
  875. * On the first open of the device setup the port hardware, and
  876. * initialize the per port data structure.
  877. */
  878. portp->tty = tty;
  879. tty->driver_data = portp;
  880. portp->refcount++;
  881. if ((portp->flags & ASYNC_INITIALIZED) == 0) {
  882. if (portp->tx.buf == (char *) NULL) {
  883. portp->tx.buf = (char *) stl_memalloc(STL_TXBUFSIZE);
  884. if (portp->tx.buf == (char *) NULL)
  885. return(-ENOMEM);
  886. portp->tx.head = portp->tx.buf;
  887. portp->tx.tail = portp->tx.buf;
  888. }
  889. stl_setport(portp, tty->termios);
  890. portp->sigs = stl_getsignals(portp);
  891. stl_setsignals(portp, 1, 1);
  892. stl_enablerxtx(portp, 1, 1);
  893. stl_startrxtx(portp, 1, 0);
  894. clear_bit(TTY_IO_ERROR, &tty->flags);
  895. portp->flags |= ASYNC_INITIALIZED;
  896. }
  897. /*
  898. * Check if this port is in the middle of closing. If so then wait
  899. * until it is closed then return error status, based on flag settings.
  900. * The sleep here does not need interrupt protection since the wakeup
  901. * for it is done with the same context.
  902. */
  903. if (portp->flags & ASYNC_CLOSING) {
  904. interruptible_sleep_on(&portp->close_wait);
  905. if (portp->flags & ASYNC_HUP_NOTIFY)
  906. return(-EAGAIN);
  907. return(-ERESTARTSYS);
  908. }
  909. /*
  910. * Based on type of open being done check if it can overlap with any
  911. * previous opens still in effect. If we are a normal serial device
  912. * then also we might have to wait for carrier.
  913. */
  914. if (!(filp->f_flags & O_NONBLOCK)) {
  915. if ((rc = stl_waitcarrier(portp, filp)) != 0)
  916. return(rc);
  917. }
  918. portp->flags |= ASYNC_NORMAL_ACTIVE;
  919. return(0);
  920. }
  921. /*****************************************************************************/
  922. /*
  923. * Possibly need to wait for carrier (DCD signal) to come high. Say
  924. * maybe because if we are clocal then we don't need to wait...
  925. */
  926. static int stl_waitcarrier(stlport_t *portp, struct file *filp)
  927. {
  928. unsigned long flags;
  929. int rc, doclocal;
  930. #ifdef DEBUG
  931. printk("stl_waitcarrier(portp=%x,filp=%x)\n", (int) portp, (int) filp);
  932. #endif
  933. rc = 0;
  934. doclocal = 0;
  935. if (portp->tty->termios->c_cflag & CLOCAL)
  936. doclocal++;
  937. save_flags(flags);
  938. cli();
  939. portp->openwaitcnt++;
  940. if (! tty_hung_up_p(filp))
  941. portp->refcount--;
  942. for (;;) {
  943. stl_setsignals(portp, 1, 1);
  944. if (tty_hung_up_p(filp) ||
  945. ((portp->flags & ASYNC_INITIALIZED) == 0)) {
  946. if (portp->flags & ASYNC_HUP_NOTIFY)
  947. rc = -EBUSY;
  948. else
  949. rc = -ERESTARTSYS;
  950. break;
  951. }
  952. if (((portp->flags & ASYNC_CLOSING) == 0) &&
  953. (doclocal || (portp->sigs & TIOCM_CD))) {
  954. break;
  955. }
  956. if (signal_pending(current)) {
  957. rc = -ERESTARTSYS;
  958. break;
  959. }
  960. interruptible_sleep_on(&portp->open_wait);
  961. }
  962. if (! tty_hung_up_p(filp))
  963. portp->refcount++;
  964. portp->openwaitcnt--;
  965. restore_flags(flags);
  966. return(rc);
  967. }
  968. /*****************************************************************************/
  969. static void stl_close(struct tty_struct *tty, struct file *filp)
  970. {
  971. stlport_t *portp;
  972. unsigned long flags;
  973. #ifdef DEBUG
  974. printk("stl_close(tty=%x,filp=%x)\n", (int) tty, (int) filp);
  975. #endif
  976. portp = tty->driver_data;
  977. if (portp == (stlport_t *) NULL)
  978. return;
  979. save_flags(flags);
  980. cli();
  981. if (tty_hung_up_p(filp)) {
  982. restore_flags(flags);
  983. return;
  984. }
  985. if ((tty->count == 1) && (portp->refcount != 1))
  986. portp->refcount = 1;
  987. if (portp->refcount-- > 1) {
  988. restore_flags(flags);
  989. return;
  990. }
  991. portp->refcount = 0;
  992. portp->flags |= ASYNC_CLOSING;
  993. /*
  994. * May want to wait for any data to drain before closing. The BUSY
  995. * flag keeps track of whether we are still sending or not - it is
  996. * very accurate for the cd1400, not quite so for the sc26198.
  997. * (The sc26198 has no "end-of-data" interrupt only empty FIFO)
  998. */
  999. tty->closing = 1;
  1000. if (portp->closing_wait != ASYNC_CLOSING_WAIT_NONE)
  1001. tty_wait_until_sent(tty, portp->closing_wait);
  1002. stl_waituntilsent(tty, (HZ / 2));
  1003. portp->flags &= ~ASYNC_INITIALIZED;
  1004. stl_disableintrs(portp);
  1005. if (tty->termios->c_cflag & HUPCL)
  1006. stl_setsignals(portp, 0, 0);
  1007. stl_enablerxtx(portp, 0, 0);
  1008. stl_flushbuffer(tty);
  1009. portp->istate = 0;
  1010. if (portp->tx.buf != (char *) NULL) {
  1011. kfree(portp->tx.buf);
  1012. portp->tx.buf = (char *) NULL;
  1013. portp->tx.head = (char *) NULL;
  1014. portp->tx.tail = (char *) NULL;
  1015. }
  1016. set_bit(TTY_IO_ERROR, &tty->flags);
  1017. tty_ldisc_flush(tty);
  1018. tty->closing = 0;
  1019. portp->tty = (struct tty_struct *) NULL;
  1020. if (portp->openwaitcnt) {
  1021. if (portp->close_delay)
  1022. msleep_interruptible(jiffies_to_msecs(portp->close_delay));
  1023. wake_up_interruptible(&portp->open_wait);
  1024. }
  1025. portp->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
  1026. wake_up_interruptible(&portp->close_wait);
  1027. restore_flags(flags);
  1028. }
  1029. /*****************************************************************************/
  1030. /*
  1031. * Write routine. Take data and stuff it in to the TX ring queue.
  1032. * If transmit interrupts are not running then start them.
  1033. */
  1034. static int stl_write(struct tty_struct *tty, const unsigned char *buf, int count)
  1035. {
  1036. stlport_t *portp;
  1037. unsigned int len, stlen;
  1038. unsigned char *chbuf;
  1039. char *head, *tail;
  1040. #ifdef DEBUG
  1041. printk("stl_write(tty=%x,buf=%x,count=%d)\n",
  1042. (int) tty, (int) buf, count);
  1043. #endif
  1044. if ((tty == (struct tty_struct *) NULL) ||
  1045. (stl_tmpwritebuf == (char *) NULL))
  1046. return(0);
  1047. portp = tty->driver_data;
  1048. if (portp == (stlport_t *) NULL)
  1049. return(0);
  1050. if (portp->tx.buf == (char *) NULL)
  1051. return(0);
  1052. /*
  1053. * If copying direct from user space we must cater for page faults,
  1054. * causing us to "sleep" here for a while. To handle this copy in all
  1055. * the data we need now, into a local buffer. Then when we got it all
  1056. * copy it into the TX buffer.
  1057. */
  1058. chbuf = (unsigned char *) buf;
  1059. head = portp->tx.head;
  1060. tail = portp->tx.tail;
  1061. if (head >= tail) {
  1062. len = STL_TXBUFSIZE - (head - tail) - 1;
  1063. stlen = STL_TXBUFSIZE - (head - portp->tx.buf);
  1064. } else {
  1065. len = tail - head - 1;
  1066. stlen = len;
  1067. }
  1068. len = MIN(len, count);
  1069. count = 0;
  1070. while (len > 0) {
  1071. stlen = MIN(len, stlen);
  1072. memcpy(head, chbuf, stlen);
  1073. len -= stlen;
  1074. chbuf += stlen;
  1075. count += stlen;
  1076. head += stlen;
  1077. if (head >= (portp->tx.buf + STL_TXBUFSIZE)) {
  1078. head = portp->tx.buf;
  1079. stlen = tail - head;
  1080. }
  1081. }
  1082. portp->tx.head = head;
  1083. clear_bit(ASYI_TXLOW, &portp->istate);
  1084. stl_startrxtx(portp, -1, 1);
  1085. return(count);
  1086. }
  1087. /*****************************************************************************/
  1088. static void stl_putchar(struct tty_struct *tty, unsigned char ch)
  1089. {
  1090. stlport_t *portp;
  1091. unsigned int len;
  1092. char *head, *tail;
  1093. #ifdef DEBUG
  1094. printk("stl_putchar(tty=%x,ch=%x)\n", (int) tty, (int) ch);
  1095. #endif
  1096. if (tty == (struct tty_struct *) NULL)
  1097. return;
  1098. portp = tty->driver_data;
  1099. if (portp == (stlport_t *) NULL)
  1100. return;
  1101. if (portp->tx.buf == (char *) NULL)
  1102. return;
  1103. head = portp->tx.head;
  1104. tail = portp->tx.tail;
  1105. len = (head >= tail) ? (STL_TXBUFSIZE - (head - tail)) : (tail - head);
  1106. len--;
  1107. if (len > 0) {
  1108. *head++ = ch;
  1109. if (head >= (portp->tx.buf + STL_TXBUFSIZE))
  1110. head = portp->tx.buf;
  1111. }
  1112. portp->tx.head = head;
  1113. }
  1114. /*****************************************************************************/
  1115. /*
  1116. * If there are any characters in the buffer then make sure that TX
  1117. * interrupts are on and get'em out. Normally used after the putchar
  1118. * routine has been called.
  1119. */
  1120. static void stl_flushchars(struct tty_struct *tty)
  1121. {
  1122. stlport_t *portp;
  1123. #ifdef DEBUG
  1124. printk("stl_flushchars(tty=%x)\n", (int) tty);
  1125. #endif
  1126. if (tty == (struct tty_struct *) NULL)
  1127. return;
  1128. portp = tty->driver_data;
  1129. if (portp == (stlport_t *) NULL)
  1130. return;
  1131. if (portp->tx.buf == (char *) NULL)
  1132. return;
  1133. #if 0
  1134. if (tty->stopped || tty->hw_stopped ||
  1135. (portp->tx.head == portp->tx.tail))
  1136. return;
  1137. #endif
  1138. stl_startrxtx(portp, -1, 1);
  1139. }
  1140. /*****************************************************************************/
  1141. static int stl_writeroom(struct tty_struct *tty)
  1142. {
  1143. stlport_t *portp;
  1144. char *head, *tail;
  1145. #ifdef DEBUG
  1146. printk("stl_writeroom(tty=%x)\n", (int) tty);
  1147. #endif
  1148. if (tty == (struct tty_struct *) NULL)
  1149. return(0);
  1150. portp = tty->driver_data;
  1151. if (portp == (stlport_t *) NULL)
  1152. return(0);
  1153. if (portp->tx.buf == (char *) NULL)
  1154. return(0);
  1155. head = portp->tx.head;
  1156. tail = portp->tx.tail;
  1157. return((head >= tail) ? (STL_TXBUFSIZE - (head - tail) - 1) : (tail - head - 1));
  1158. }
  1159. /*****************************************************************************/
  1160. /*
  1161. * Return number of chars in the TX buffer. Normally we would just
  1162. * calculate the number of chars in the buffer and return that, but if
  1163. * the buffer is empty and TX interrupts are still on then we return
  1164. * that the buffer still has 1 char in it. This way whoever called us
  1165. * will not think that ALL chars have drained - since the UART still
  1166. * must have some chars in it (we are busy after all).
  1167. */
  1168. static int stl_charsinbuffer(struct tty_struct *tty)
  1169. {
  1170. stlport_t *portp;
  1171. unsigned int size;
  1172. char *head, *tail;
  1173. #ifdef DEBUG
  1174. printk("stl_charsinbuffer(tty=%x)\n", (int) tty);
  1175. #endif
  1176. if (tty == (struct tty_struct *) NULL)
  1177. return(0);
  1178. portp = tty->driver_data;
  1179. if (portp == (stlport_t *) NULL)
  1180. return(0);
  1181. if (portp->tx.buf == (char *) NULL)
  1182. return(0);
  1183. head = portp->tx.head;
  1184. tail = portp->tx.tail;
  1185. size = (head >= tail) ? (head - tail) : (STL_TXBUFSIZE - (tail - head));
  1186. if ((size == 0) && test_bit(ASYI_TXBUSY, &portp->istate))
  1187. size = 1;
  1188. return(size);
  1189. }
  1190. /*****************************************************************************/
  1191. /*
  1192. * Generate the serial struct info.
  1193. */
  1194. static int stl_getserial(stlport_t *portp, struct serial_struct __user *sp)
  1195. {
  1196. struct serial_struct sio;
  1197. stlbrd_t *brdp;
  1198. #ifdef DEBUG
  1199. printk("stl_getserial(portp=%x,sp=%x)\n", (int) portp, (int) sp);
  1200. #endif
  1201. memset(&sio, 0, sizeof(struct serial_struct));
  1202. sio.line = portp->portnr;
  1203. sio.port = portp->ioaddr;
  1204. sio.flags = portp->flags;
  1205. sio.baud_base = portp->baud_base;
  1206. sio.close_delay = portp->close_delay;
  1207. sio.closing_wait = portp->closing_wait;
  1208. sio.custom_divisor = portp->custom_divisor;
  1209. sio.hub6 = 0;
  1210. if (portp->uartp == &stl_cd1400uart) {
  1211. sio.type = PORT_CIRRUS;
  1212. sio.xmit_fifo_size = CD1400_TXFIFOSIZE;
  1213. } else {
  1214. sio.type = PORT_UNKNOWN;
  1215. sio.xmit_fifo_size = SC26198_TXFIFOSIZE;
  1216. }
  1217. brdp = stl_brds[portp->brdnr];
  1218. if (brdp != (stlbrd_t *) NULL)
  1219. sio.irq = brdp->irq;
  1220. return copy_to_user(sp, &sio, sizeof(struct serial_struct)) ? -EFAULT : 0;
  1221. }
  1222. /*****************************************************************************/
  1223. /*
  1224. * Set port according to the serial struct info.
  1225. * At this point we do not do any auto-configure stuff, so we will
  1226. * just quietly ignore any requests to change irq, etc.
  1227. */
  1228. static int stl_setserial(stlport_t *portp, struct serial_struct __user *sp)
  1229. {
  1230. struct serial_struct sio;
  1231. #ifdef DEBUG
  1232. printk("stl_setserial(portp=%x,sp=%x)\n", (int) portp, (int) sp);
  1233. #endif
  1234. if (copy_from_user(&sio, sp, sizeof(struct serial_struct)))
  1235. return -EFAULT;
  1236. if (!capable(CAP_SYS_ADMIN)) {
  1237. if ((sio.baud_base != portp->baud_base) ||
  1238. (sio.close_delay != portp->close_delay) ||
  1239. ((sio.flags & ~ASYNC_USR_MASK) !=
  1240. (portp->flags & ~ASYNC_USR_MASK)))
  1241. return(-EPERM);
  1242. }
  1243. portp->flags = (portp->flags & ~ASYNC_USR_MASK) |
  1244. (sio.flags & ASYNC_USR_MASK);
  1245. portp->baud_base = sio.baud_base;
  1246. portp->close_delay = sio.close_delay;
  1247. portp->closing_wait = sio.closing_wait;
  1248. portp->custom_divisor = sio.custom_divisor;
  1249. stl_setport(portp, portp->tty->termios);
  1250. return(0);
  1251. }
  1252. /*****************************************************************************/
  1253. static int stl_tiocmget(struct tty_struct *tty, struct file *file)
  1254. {
  1255. stlport_t *portp;
  1256. if (tty == (struct tty_struct *) NULL)
  1257. return(-ENODEV);
  1258. portp = tty->driver_data;
  1259. if (portp == (stlport_t *) NULL)
  1260. return(-ENODEV);
  1261. if (tty->flags & (1 << TTY_IO_ERROR))
  1262. return(-EIO);
  1263. return stl_getsignals(portp);
  1264. }
  1265. static int stl_tiocmset(struct tty_struct *tty, struct file *file,
  1266. unsigned int set, unsigned int clear)
  1267. {
  1268. stlport_t *portp;
  1269. int rts = -1, dtr = -1;
  1270. if (tty == (struct tty_struct *) NULL)
  1271. return(-ENODEV);
  1272. portp = tty->driver_data;
  1273. if (portp == (stlport_t *) NULL)
  1274. return(-ENODEV);
  1275. if (tty->flags & (1 << TTY_IO_ERROR))
  1276. return(-EIO);
  1277. if (set & TIOCM_RTS)
  1278. rts = 1;
  1279. if (set & TIOCM_DTR)
  1280. dtr = 1;
  1281. if (clear & TIOCM_RTS)
  1282. rts = 0;
  1283. if (clear & TIOCM_DTR)
  1284. dtr = 0;
  1285. stl_setsignals(portp, dtr, rts);
  1286. return 0;
  1287. }
  1288. static int stl_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
  1289. {
  1290. stlport_t *portp;
  1291. unsigned int ival;
  1292. int rc;
  1293. void __user *argp = (void __user *)arg;
  1294. #ifdef DEBUG
  1295. printk("stl_ioctl(tty=%x,file=%x,cmd=%x,arg=%x)\n",
  1296. (int) tty, (int) file, cmd, (int) arg);
  1297. #endif
  1298. if (tty == (struct tty_struct *) NULL)
  1299. return(-ENODEV);
  1300. portp = tty->driver_data;
  1301. if (portp == (stlport_t *) NULL)
  1302. return(-ENODEV);
  1303. if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
  1304. (cmd != COM_GETPORTSTATS) && (cmd != COM_CLRPORTSTATS)) {
  1305. if (tty->flags & (1 << TTY_IO_ERROR))
  1306. return(-EIO);
  1307. }
  1308. rc = 0;
  1309. switch (cmd) {
  1310. case TIOCGSOFTCAR:
  1311. rc = put_user(((tty->termios->c_cflag & CLOCAL) ? 1 : 0),
  1312. (unsigned __user *) argp);
  1313. break;
  1314. case TIOCSSOFTCAR:
  1315. if (get_user(ival, (unsigned int __user *) arg))
  1316. return -EFAULT;
  1317. tty->termios->c_cflag =
  1318. (tty->termios->c_cflag & ~CLOCAL) |
  1319. (ival ? CLOCAL : 0);
  1320. break;
  1321. case TIOCGSERIAL:
  1322. rc = stl_getserial(portp, argp);
  1323. break;
  1324. case TIOCSSERIAL:
  1325. rc = stl_setserial(portp, argp);
  1326. break;
  1327. case COM_GETPORTSTATS:
  1328. rc = stl_getportstats(portp, argp);
  1329. break;
  1330. case COM_CLRPORTSTATS:
  1331. rc = stl_clrportstats(portp, argp);
  1332. break;
  1333. case TIOCSERCONFIG:
  1334. case TIOCSERGWILD:
  1335. case TIOCSERSWILD:
  1336. case TIOCSERGETLSR:
  1337. case TIOCSERGSTRUCT:
  1338. case TIOCSERGETMULTI:
  1339. case TIOCSERSETMULTI:
  1340. default:
  1341. rc = -ENOIOCTLCMD;
  1342. break;
  1343. }
  1344. return(rc);
  1345. }
  1346. /*****************************************************************************/
  1347. static void stl_settermios(struct tty_struct *tty, struct termios *old)
  1348. {
  1349. stlport_t *portp;
  1350. struct termios *tiosp;
  1351. #ifdef DEBUG
  1352. printk("stl_settermios(tty=%x,old=%x)\n", (int) tty, (int) old);
  1353. #endif
  1354. if (tty == (struct tty_struct *) NULL)
  1355. return;
  1356. portp = tty->driver_data;
  1357. if (portp == (stlport_t *) NULL)
  1358. return;
  1359. tiosp = tty->termios;
  1360. if ((tiosp->c_cflag == old->c_cflag) &&
  1361. (tiosp->c_iflag == old->c_iflag))
  1362. return;
  1363. stl_setport(portp, tiosp);
  1364. stl_setsignals(portp, ((tiosp->c_cflag & (CBAUD & ~CBAUDEX)) ? 1 : 0),
  1365. -1);
  1366. if ((old->c_cflag & CRTSCTS) && ((tiosp->c_cflag & CRTSCTS) == 0)) {
  1367. tty->hw_stopped = 0;
  1368. stl_start(tty);
  1369. }
  1370. if (((old->c_cflag & CLOCAL) == 0) && (tiosp->c_cflag & CLOCAL))
  1371. wake_up_interruptible(&portp->open_wait);
  1372. }
  1373. /*****************************************************************************/
  1374. /*
  1375. * Attempt to flow control who ever is sending us data. Based on termios
  1376. * settings use software or/and hardware flow control.
  1377. */
  1378. static void stl_throttle(struct tty_struct *tty)
  1379. {
  1380. stlport_t *portp;
  1381. #ifdef DEBUG
  1382. printk("stl_throttle(tty=%x)\n", (int) tty);
  1383. #endif
  1384. if (tty == (struct tty_struct *) NULL)
  1385. return;
  1386. portp = tty->driver_data;
  1387. if (portp == (stlport_t *) NULL)
  1388. return;
  1389. stl_flowctrl(portp, 0);
  1390. }
  1391. /*****************************************************************************/
  1392. /*
  1393. * Unflow control the device sending us data...
  1394. */
  1395. static void stl_unthrottle(struct tty_struct *tty)
  1396. {
  1397. stlport_t *portp;
  1398. #ifdef DEBUG
  1399. printk("stl_unthrottle(tty=%x)\n", (int) tty);
  1400. #endif
  1401. if (tty == (struct tty_struct *) NULL)
  1402. return;
  1403. portp = tty->driver_data;
  1404. if (portp == (stlport_t *) NULL)
  1405. return;
  1406. stl_flowctrl(portp, 1);
  1407. }
  1408. /*****************************************************************************/
  1409. /*
  1410. * Stop the transmitter. Basically to do this we will just turn TX
  1411. * interrupts off.
  1412. */
  1413. static void stl_stop(struct tty_struct *tty)
  1414. {
  1415. stlport_t *portp;
  1416. #ifdef DEBUG
  1417. printk("stl_stop(tty=%x)\n", (int) tty);
  1418. #endif
  1419. if (tty == (struct tty_struct *) NULL)
  1420. return;
  1421. portp = tty->driver_data;
  1422. if (portp == (stlport_t *) NULL)
  1423. return;
  1424. stl_startrxtx(portp, -1, 0);
  1425. }
  1426. /*****************************************************************************/
  1427. /*
  1428. * Start the transmitter again. Just turn TX interrupts back on.
  1429. */
  1430. static void stl_start(struct tty_struct *tty)
  1431. {
  1432. stlport_t *portp;
  1433. #ifdef DEBUG
  1434. printk("stl_start(tty=%x)\n", (int) tty);
  1435. #endif
  1436. if (tty == (struct tty_struct *) NULL)
  1437. return;
  1438. portp = tty->driver_data;
  1439. if (portp == (stlport_t *) NULL)
  1440. return;
  1441. stl_startrxtx(portp, -1, 1);
  1442. }
  1443. /*****************************************************************************/
  1444. /*
  1445. * Hangup this port. This is pretty much like closing the port, only
  1446. * a little more brutal. No waiting for data to drain. Shutdown the
  1447. * port and maybe drop signals.
  1448. */
  1449. static void stl_hangup(struct tty_struct *tty)
  1450. {
  1451. stlport_t *portp;
  1452. #ifdef DEBUG
  1453. printk("stl_hangup(tty=%x)\n", (int) tty);
  1454. #endif
  1455. if (tty == (struct tty_struct *) NULL)
  1456. return;
  1457. portp = tty->driver_data;
  1458. if (portp == (stlport_t *) NULL)
  1459. return;
  1460. portp->flags &= ~ASYNC_INITIALIZED;
  1461. stl_disableintrs(portp);
  1462. if (tty->termios->c_cflag & HUPCL)
  1463. stl_setsignals(portp, 0, 0);
  1464. stl_enablerxtx(portp, 0, 0);
  1465. stl_flushbuffer(tty);
  1466. portp->istate = 0;
  1467. set_bit(TTY_IO_ERROR, &tty->flags);
  1468. if (portp->tx.buf != (char *) NULL) {
  1469. kfree(portp->tx.buf);
  1470. portp->tx.buf = (char *) NULL;
  1471. portp->tx.head = (char *) NULL;
  1472. portp->tx.tail = (char *) NULL;
  1473. }
  1474. portp->tty = (struct tty_struct *) NULL;
  1475. portp->flags &= ~ASYNC_NORMAL_ACTIVE;
  1476. portp->refcount = 0;
  1477. wake_up_interruptible(&portp->open_wait);
  1478. }
  1479. /*****************************************************************************/
  1480. static void stl_flushbuffer(struct tty_struct *tty)
  1481. {
  1482. stlport_t *portp;
  1483. #ifdef DEBUG
  1484. printk("stl_flushbuffer(tty=%x)\n", (int) tty);
  1485. #endif
  1486. if (tty == (struct tty_struct *) NULL)
  1487. return;
  1488. portp = tty->driver_data;
  1489. if (portp == (stlport_t *) NULL)
  1490. return;
  1491. stl_flush(portp);
  1492. tty_wakeup(tty);
  1493. }
  1494. /*****************************************************************************/
  1495. static void stl_breakctl(struct tty_struct *tty, int state)
  1496. {
  1497. stlport_t *portp;
  1498. #ifdef DEBUG
  1499. printk("stl_breakctl(tty=%x,state=%d)\n", (int) tty, state);
  1500. #endif
  1501. if (tty == (struct tty_struct *) NULL)
  1502. return;
  1503. portp = tty->driver_data;
  1504. if (portp == (stlport_t *) NULL)
  1505. return;
  1506. stl_sendbreak(portp, ((state == -1) ? 1 : 2));
  1507. }
  1508. /*****************************************************************************/
  1509. static void stl_waituntilsent(struct tty_struct *tty, int timeout)
  1510. {
  1511. stlport_t *portp;
  1512. unsigned long tend;
  1513. #ifdef DEBUG
  1514. printk("stl_waituntilsent(tty=%x,timeout=%d)\n", (int) tty, timeout);
  1515. #endif
  1516. if (tty == (struct tty_struct *) NULL)
  1517. return;
  1518. portp = tty->driver_data;
  1519. if (portp == (stlport_t *) NULL)
  1520. return;
  1521. if (timeout == 0)
  1522. timeout = HZ;
  1523. tend = jiffies + timeout;
  1524. while (stl_datastate(portp)) {
  1525. if (signal_pending(current))
  1526. break;
  1527. msleep_interruptible(20);
  1528. if (time_after_eq(jiffies, tend))
  1529. break;
  1530. }
  1531. }
  1532. /*****************************************************************************/
  1533. static void stl_sendxchar(struct tty_struct *tty, char ch)
  1534. {
  1535. stlport_t *portp;
  1536. #ifdef DEBUG
  1537. printk("stl_sendxchar(tty=%x,ch=%x)\n", (int) tty, ch);
  1538. #endif
  1539. if (tty == (struct tty_struct *) NULL)
  1540. return;
  1541. portp = tty->driver_data;
  1542. if (portp == (stlport_t *) NULL)
  1543. return;
  1544. if (ch == STOP_CHAR(tty))
  1545. stl_sendflow(portp, 0);
  1546. else if (ch == START_CHAR(tty))
  1547. stl_sendflow(portp, 1);
  1548. else
  1549. stl_putchar(tty, ch);
  1550. }
  1551. /*****************************************************************************/
  1552. #define MAXLINE 80
  1553. /*
  1554. * Format info for a specified port. The line is deliberately limited
  1555. * to 80 characters. (If it is too long it will be truncated, if too
  1556. * short then padded with spaces).
  1557. */
  1558. static int stl_portinfo(stlport_t *portp, int portnr, char *pos)
  1559. {
  1560. char *sp;
  1561. int sigs, cnt;
  1562. sp = pos;
  1563. sp += sprintf(sp, "%d: uart:%s tx:%d rx:%d",
  1564. portnr, (portp->hwid == 1) ? "SC26198" : "CD1400",
  1565. (int) portp->stats.txtotal, (int) portp->stats.rxtotal);
  1566. if (portp->stats.rxframing)
  1567. sp += sprintf(sp, " fe:%d", (int) portp->stats.rxframing);
  1568. if (portp->stats.rxparity)
  1569. sp += spri