Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /*****************************************************************************/ |
2 | /* | |
3 | * moxa.c -- MOXA Intellio family multiport serial driver. | |
4 | * | |
b9705b60 JS |
5 | * Copyright (C) 1999-2000 Moxa Technologies (support@moxa.com). |
6 | * Copyright (c) 2007 Jiri Slaby <jirislaby@gmail.com> | |
1da177e4 LT |
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. | |
1da177e4 LT |
15 | */ |
16 | ||
17 | /* | |
18 | * MOXA Intellio Series Driver | |
19 | * for : LINUX | |
20 | * date : 1999/1/7 | |
21 | * version : 5.1 | |
22 | */ | |
23 | ||
1da177e4 LT |
24 | #include <linux/module.h> |
25 | #include <linux/types.h> | |
26 | #include <linux/mm.h> | |
27 | #include <linux/ioport.h> | |
28 | #include <linux/errno.h> | |
03718234 | 29 | #include <linux/firmware.h> |
1da177e4 LT |
30 | #include <linux/signal.h> |
31 | #include <linux/sched.h> | |
32 | #include <linux/timer.h> | |
33 | #include <linux/interrupt.h> | |
34 | #include <linux/tty.h> | |
35 | #include <linux/tty_flip.h> | |
36 | #include <linux/major.h> | |
37 | #include <linux/string.h> | |
38 | #include <linux/fcntl.h> | |
39 | #include <linux/ptrace.h> | |
40 | #include <linux/serial.h> | |
41 | #include <linux/tty_driver.h> | |
42 | #include <linux/delay.h> | |
43 | #include <linux/pci.h> | |
44 | #include <linux/init.h> | |
45 | #include <linux/bitops.h> | |
46 | ||
47 | #include <asm/system.h> | |
48 | #include <asm/io.h> | |
49 | #include <asm/uaccess.h> | |
50 | ||
03718234 JS |
51 | #include "moxa.h" |
52 | ||
b9705b60 | 53 | #define MOXA_VERSION "6.0k" |
1da177e4 | 54 | |
03718234 JS |
55 | #define MOXA_FW_HDRLEN 32 |
56 | ||
11324edd | 57 | #define MOXAMAJOR 172 |
1da177e4 | 58 | |
11324edd | 59 | #define MAX_BOARDS 4 /* Don't change this value */ |
1da177e4 | 60 | #define MAX_PORTS_PER_BOARD 32 /* Don't change this value */ |
11324edd | 61 | #define MAX_PORTS (MAX_BOARDS * MAX_PORTS_PER_BOARD) |
1da177e4 | 62 | |
08d01c79 JS |
63 | #define MOXA_IS_320(brd) ((brd)->boardType == MOXA_BOARD_C320_ISA || \ |
64 | (brd)->boardType == MOXA_BOARD_C320_PCI) | |
65 | ||
1da177e4 LT |
66 | /* |
67 | * Define the Moxa PCI vendor and device IDs. | |
68 | */ | |
11324edd JS |
69 | #define MOXA_BUS_TYPE_ISA 0 |
70 | #define MOXA_BUS_TYPE_PCI 1 | |
1da177e4 | 71 | |
1da177e4 LT |
72 | enum { |
73 | MOXA_BOARD_C218_PCI = 1, | |
74 | MOXA_BOARD_C218_ISA, | |
75 | MOXA_BOARD_C320_PCI, | |
76 | MOXA_BOARD_C320_ISA, | |
77 | MOXA_BOARD_CP204J, | |
78 | }; | |
79 | ||
80 | static char *moxa_brdname[] = | |
81 | { | |
82 | "C218 Turbo PCI series", | |
83 | "C218 Turbo ISA series", | |
84 | "C320 Turbo PCI series", | |
85 | "C320 Turbo ISA series", | |
86 | "CP-204J series", | |
87 | }; | |
88 | ||
89 | #ifdef CONFIG_PCI | |
90 | static struct pci_device_id moxa_pcibrds[] = { | |
5ebb4078 JS |
91 | { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C218), |
92 | .driver_data = MOXA_BOARD_C218_PCI }, | |
93 | { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C320), | |
94 | .driver_data = MOXA_BOARD_C320_PCI }, | |
95 | { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP204J), | |
96 | .driver_data = MOXA_BOARD_CP204J }, | |
1da177e4 LT |
97 | { 0 } |
98 | }; | |
99 | MODULE_DEVICE_TABLE(pci, moxa_pcibrds); | |
100 | #endif /* CONFIG_PCI */ | |
101 | ||
03718234 JS |
102 | struct moxa_port; |
103 | ||
8f8ecbad | 104 | static struct moxa_board_conf { |
1da177e4 LT |
105 | int boardType; |
106 | int numPorts; | |
1da177e4 | 107 | int busType; |
8f8ecbad | 108 | |
810ab09b | 109 | unsigned int ready; |
8f8ecbad | 110 | |
03718234 JS |
111 | struct moxa_port *ports; |
112 | ||
8f8ecbad JS |
113 | void __iomem *basemem; |
114 | void __iomem *intNdx; | |
115 | void __iomem *intPend; | |
116 | void __iomem *intTable; | |
117 | } moxa_boards[MAX_BOARDS]; | |
118 | ||
119 | struct mxser_mstatus { | |
120 | tcflag_t cflag; | |
121 | int cts; | |
122 | int dsr; | |
123 | int ri; | |
124 | int dcd; | |
9dff89cd | 125 | }; |
1da177e4 | 126 | |
8f8ecbad JS |
127 | struct moxaq_str { |
128 | int inq; | |
129 | int outq; | |
130 | }; | |
1da177e4 | 131 | |
8f8ecbad | 132 | struct moxa_port { |
9de6a51f | 133 | struct tty_port port; |
b4173f45 | 134 | struct moxa_board_conf *board; |
7bcf97d1 JS |
135 | void __iomem *tableAddr; |
136 | ||
1da177e4 | 137 | int type; |
1da177e4 | 138 | int cflag; |
7bcf97d1 | 139 | unsigned long statusflags; |
1da177e4 | 140 | |
7bcf97d1 JS |
141 | u8 DCDState; |
142 | u8 lineCtrl; | |
143 | u8 lowChkFlag; | |
8f8ecbad | 144 | }; |
1da177e4 | 145 | |
74d7d97b JS |
146 | struct mon_str { |
147 | int tick; | |
148 | int rxcnt[MAX_PORTS]; | |
149 | int txcnt[MAX_PORTS]; | |
150 | }; | |
151 | ||
1da177e4 LT |
152 | /* statusflags */ |
153 | #define TXSTOPPED 0x1 | |
154 | #define LOWWAIT 0x2 | |
155 | #define EMPTYWAIT 0x4 | |
156 | #define THROTTLE 0x8 | |
157 | ||
1da177e4 LT |
158 | #define SERIAL_DO_RESTART |
159 | ||
1da177e4 LT |
160 | #define WAKEUP_CHARS 256 |
161 | ||
1da177e4 | 162 | static int ttymajor = MOXAMAJOR; |
74d7d97b JS |
163 | static struct mon_str moxaLog; |
164 | static unsigned int moxaFuncTout = HZ / 2; | |
7bcf97d1 | 165 | static unsigned int moxaLowWaterChk; |
a8f5cda0 | 166 | static DEFINE_MUTEX(moxa_openlock); |
1da177e4 LT |
167 | /* Variables for insmod */ |
168 | #ifdef MODULE | |
d353eca4 JS |
169 | static unsigned long baseaddr[MAX_BOARDS]; |
170 | static unsigned int type[MAX_BOARDS]; | |
171 | static unsigned int numports[MAX_BOARDS]; | |
1da177e4 LT |
172 | #endif |
173 | ||
174 | MODULE_AUTHOR("William Chen"); | |
175 | MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver"); | |
176 | MODULE_LICENSE("GPL"); | |
177 | #ifdef MODULE | |
d353eca4 JS |
178 | module_param_array(type, uint, NULL, 0); |
179 | MODULE_PARM_DESC(type, "card type: C218=2, C320=4"); | |
180 | module_param_array(baseaddr, ulong, NULL, 0); | |
181 | MODULE_PARM_DESC(baseaddr, "base address"); | |
182 | module_param_array(numports, uint, NULL, 0); | |
183 | MODULE_PARM_DESC(numports, "numports (ignored for C218)"); | |
1da177e4 LT |
184 | #endif |
185 | module_param(ttymajor, int, 0); | |
1da177e4 | 186 | |
1da177e4 LT |
187 | /* |
188 | * static functions: | |
189 | */ | |
1da177e4 LT |
190 | static int moxa_open(struct tty_struct *, struct file *); |
191 | static void moxa_close(struct tty_struct *, struct file *); | |
192 | static int moxa_write(struct tty_struct *, const unsigned char *, int); | |
193 | static int moxa_write_room(struct tty_struct *); | |
194 | static void moxa_flush_buffer(struct tty_struct *); | |
195 | static int moxa_chars_in_buffer(struct tty_struct *); | |
1da177e4 LT |
196 | static void moxa_throttle(struct tty_struct *); |
197 | static void moxa_unthrottle(struct tty_struct *); | |
606d099c | 198 | static void moxa_set_termios(struct tty_struct *, struct ktermios *); |
1da177e4 LT |
199 | static void moxa_stop(struct tty_struct *); |
200 | static void moxa_start(struct tty_struct *); | |
201 | static void moxa_hangup(struct tty_struct *); | |
202 | static int moxa_tiocmget(struct tty_struct *tty, struct file *file); | |
203 | static int moxa_tiocmset(struct tty_struct *tty, struct file *file, | |
204 | unsigned int set, unsigned int clear); | |
205 | static void moxa_poll(unsigned long); | |
db1acaa6 | 206 | static void moxa_set_tty_param(struct tty_struct *, struct ktermios *); |
6f56b658 | 207 | static void moxa_setup_empty_event(struct tty_struct *); |
d450b5a0 | 208 | static void moxa_shut_down(struct tty_struct *); |
31f35939 | 209 | static int moxa_carrier_raised(struct tty_port *); |
1da177e4 LT |
210 | /* |
211 | * moxa board interface functions: | |
212 | */ | |
b4173f45 JS |
213 | static void MoxaPortEnable(struct moxa_port *); |
214 | static void MoxaPortDisable(struct moxa_port *); | |
215 | static int MoxaPortSetTermio(struct moxa_port *, struct ktermios *, speed_t); | |
216 | static int MoxaPortGetLineOut(struct moxa_port *, int *, int *); | |
217 | static void MoxaPortLineCtrl(struct moxa_port *, int, int); | |
218 | static void MoxaPortFlowCtrl(struct moxa_port *, int, int, int, int, int); | |
219 | static int MoxaPortLineStatus(struct moxa_port *); | |
b4173f45 | 220 | static void MoxaPortFlushData(struct moxa_port *, int); |
d450b5a0 | 221 | static int MoxaPortWriteData(struct tty_struct *, const unsigned char *, int); |
7bcf97d1 | 222 | static int MoxaPortReadData(struct moxa_port *); |
b4173f45 JS |
223 | static int MoxaPortTxQueue(struct moxa_port *); |
224 | static int MoxaPortRxQueue(struct moxa_port *); | |
225 | static int MoxaPortTxFree(struct moxa_port *); | |
226 | static void MoxaPortTxDisable(struct moxa_port *); | |
227 | static void MoxaPortTxEnable(struct moxa_port *); | |
8f8ecbad JS |
228 | static int moxa_get_serial_info(struct moxa_port *, struct serial_struct __user *); |
229 | static int moxa_set_serial_info(struct moxa_port *, struct serial_struct __user *); | |
b4173f45 | 230 | static void MoxaSetFifo(struct moxa_port *port, int enable); |
1da177e4 | 231 | |
74d7d97b JS |
232 | /* |
233 | * I/O functions | |
234 | */ | |
235 | ||
236 | static void moxa_wait_finish(void __iomem *ofsAddr) | |
237 | { | |
238 | unsigned long end = jiffies + moxaFuncTout; | |
239 | ||
240 | while (readw(ofsAddr + FuncCode) != 0) | |
241 | if (time_after(jiffies, end)) | |
242 | return; | |
243 | if (readw(ofsAddr + FuncCode) != 0 && printk_ratelimit()) | |
244 | printk(KERN_WARNING "moxa function expired\n"); | |
245 | } | |
246 | ||
eaa95a8d | 247 | static void moxafunc(void __iomem *ofsAddr, u16 cmd, u16 arg) |
74d7d97b JS |
248 | { |
249 | writew(arg, ofsAddr + FuncArg); | |
250 | writew(cmd, ofsAddr + FuncCode); | |
251 | moxa_wait_finish(ofsAddr); | |
252 | } | |
253 | ||
7bcf97d1 JS |
254 | static void moxa_low_water_check(void __iomem *ofsAddr) |
255 | { | |
256 | u16 rptr, wptr, mask, len; | |
257 | ||
258 | if (readb(ofsAddr + FlagStat) & Xoff_state) { | |
259 | rptr = readw(ofsAddr + RXrptr); | |
260 | wptr = readw(ofsAddr + RXwptr); | |
261 | mask = readw(ofsAddr + RX_mask); | |
262 | len = (wptr - rptr) & mask; | |
263 | if (len <= Low_water) | |
264 | moxafunc(ofsAddr, FC_SendXon, 0); | |
265 | } | |
266 | } | |
267 | ||
74d7d97b JS |
268 | /* |
269 | * TTY operations | |
270 | */ | |
271 | ||
272 | static int moxa_ioctl(struct tty_struct *tty, struct file *file, | |
273 | unsigned int cmd, unsigned long arg) | |
274 | { | |
275 | struct moxa_port *ch = tty->driver_data; | |
276 | void __user *argp = (void __user *)arg; | |
a8f5cda0 | 277 | int status, ret = 0; |
74d7d97b JS |
278 | |
279 | if (tty->index == MAX_PORTS) { | |
280 | if (cmd != MOXA_GETDATACOUNT && cmd != MOXA_GET_IOQUEUE && | |
281 | cmd != MOXA_GETMSTATUS) | |
282 | return -EINVAL; | |
283 | } else if (!ch) | |
284 | return -ENODEV; | |
285 | ||
286 | switch (cmd) { | |
287 | case MOXA_GETDATACOUNT: | |
288 | moxaLog.tick = jiffies; | |
a8f5cda0 JS |
289 | if (copy_to_user(argp, &moxaLog, sizeof(moxaLog))) |
290 | ret = -EFAULT; | |
291 | break; | |
74d7d97b JS |
292 | case MOXA_FLUSH_QUEUE: |
293 | MoxaPortFlushData(ch, arg); | |
a8f5cda0 | 294 | break; |
74d7d97b JS |
295 | case MOXA_GET_IOQUEUE: { |
296 | struct moxaq_str __user *argm = argp; | |
297 | struct moxaq_str tmp; | |
298 | struct moxa_port *p; | |
299 | unsigned int i, j; | |
300 | ||
a8f5cda0 | 301 | mutex_lock(&moxa_openlock); |
74d7d97b JS |
302 | for (i = 0; i < MAX_BOARDS; i++) { |
303 | p = moxa_boards[i].ports; | |
304 | for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) { | |
305 | memset(&tmp, 0, sizeof(tmp)); | |
306 | if (moxa_boards[i].ready) { | |
307 | tmp.inq = MoxaPortRxQueue(p); | |
308 | tmp.outq = MoxaPortTxQueue(p); | |
309 | } | |
a8f5cda0 JS |
310 | if (copy_to_user(argm, &tmp, sizeof(tmp))) { |
311 | mutex_unlock(&moxa_openlock); | |
74d7d97b | 312 | return -EFAULT; |
a8f5cda0 | 313 | } |
74d7d97b JS |
314 | } |
315 | } | |
a8f5cda0 JS |
316 | mutex_unlock(&moxa_openlock); |
317 | break; | |
74d7d97b JS |
318 | } case MOXA_GET_OQUEUE: |
319 | status = MoxaPortTxQueue(ch); | |
a8f5cda0 JS |
320 | ret = put_user(status, (unsigned long __user *)argp); |
321 | break; | |
74d7d97b JS |
322 | case MOXA_GET_IQUEUE: |
323 | status = MoxaPortRxQueue(ch); | |
a8f5cda0 JS |
324 | ret = put_user(status, (unsigned long __user *)argp); |
325 | break; | |
74d7d97b JS |
326 | case MOXA_GETMSTATUS: { |
327 | struct mxser_mstatus __user *argm = argp; | |
328 | struct mxser_mstatus tmp; | |
329 | struct moxa_port *p; | |
330 | unsigned int i, j; | |
331 | ||
a8f5cda0 | 332 | mutex_lock(&moxa_openlock); |
74d7d97b JS |
333 | for (i = 0; i < MAX_BOARDS; i++) { |
334 | p = moxa_boards[i].ports; | |
335 | for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) { | |
d450b5a0 | 336 | struct tty_struct *ttyp; |
74d7d97b JS |
337 | memset(&tmp, 0, sizeof(tmp)); |
338 | if (!moxa_boards[i].ready) | |
339 | goto copy; | |
340 | ||
341 | status = MoxaPortLineStatus(p); | |
342 | if (status & 1) | |
343 | tmp.cts = 1; | |
344 | if (status & 2) | |
345 | tmp.dsr = 1; | |
346 | if (status & 4) | |
347 | tmp.dcd = 1; | |
348 | ||
d450b5a0 AC |
349 | ttyp = tty_port_tty_get(&p->port); |
350 | if (!ttyp || !ttyp->termios) | |
74d7d97b JS |
351 | tmp.cflag = p->cflag; |
352 | else | |
d450b5a0 AC |
353 | tmp.cflag = ttyp->termios->c_cflag; |
354 | tty_kref_put(tty); | |
74d7d97b | 355 | copy: |
a8f5cda0 JS |
356 | if (copy_to_user(argm, &tmp, sizeof(tmp))) { |
357 | mutex_unlock(&moxa_openlock); | |
74d7d97b | 358 | return -EFAULT; |
a8f5cda0 | 359 | } |
74d7d97b JS |
360 | } |
361 | } | |
a8f5cda0 JS |
362 | mutex_unlock(&moxa_openlock); |
363 | break; | |
74d7d97b JS |
364 | } |
365 | case TIOCGSERIAL: | |
a8f5cda0 JS |
366 | mutex_lock(&moxa_openlock); |
367 | ret = moxa_get_serial_info(ch, argp); | |
368 | mutex_unlock(&moxa_openlock); | |
369 | break; | |
74d7d97b | 370 | case TIOCSSERIAL: |
a8f5cda0 JS |
371 | mutex_lock(&moxa_openlock); |
372 | ret = moxa_set_serial_info(ch, argp); | |
373 | mutex_unlock(&moxa_openlock); | |
374 | break; | |
375 | default: | |
376 | ret = -ENOIOCTLCMD; | |
74d7d97b | 377 | } |
a8f5cda0 | 378 | return ret; |
74d7d97b JS |
379 | } |
380 | ||
9e98966c | 381 | static int moxa_break_ctl(struct tty_struct *tty, int state) |
74d7d97b JS |
382 | { |
383 | struct moxa_port *port = tty->driver_data; | |
384 | ||
385 | moxafunc(port->tableAddr, state ? FC_SendBreak : FC_StopBreak, | |
386 | Magic_code); | |
9e98966c | 387 | return 0; |
74d7d97b JS |
388 | } |
389 | ||
b68e31d0 | 390 | static const struct tty_operations moxa_ops = { |
1da177e4 LT |
391 | .open = moxa_open, |
392 | .close = moxa_close, | |
393 | .write = moxa_write, | |
394 | .write_room = moxa_write_room, | |
395 | .flush_buffer = moxa_flush_buffer, | |
396 | .chars_in_buffer = moxa_chars_in_buffer, | |
1da177e4 LT |
397 | .ioctl = moxa_ioctl, |
398 | .throttle = moxa_throttle, | |
399 | .unthrottle = moxa_unthrottle, | |
400 | .set_termios = moxa_set_termios, | |
401 | .stop = moxa_stop, | |
402 | .start = moxa_start, | |
403 | .hangup = moxa_hangup, | |
74d7d97b | 404 | .break_ctl = moxa_break_ctl, |
1da177e4 LT |
405 | .tiocmget = moxa_tiocmget, |
406 | .tiocmset = moxa_tiocmset, | |
407 | }; | |
408 | ||
31f35939 AC |
409 | static const struct tty_port_operations moxa_port_ops = { |
410 | .carrier_raised = moxa_carrier_raised, | |
411 | }; | |
412 | ||
aa7e5221 | 413 | static struct tty_driver *moxaDriver; |
aa7e5221 | 414 | static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0); |
34af946a | 415 | static DEFINE_SPINLOCK(moxa_lock); |
33f0f88f | 416 | |
74d7d97b JS |
417 | /* |
418 | * HW init | |
419 | */ | |
420 | ||
03718234 JS |
421 | static int moxa_check_fw_model(struct moxa_board_conf *brd, u8 model) |
422 | { | |
423 | switch (brd->boardType) { | |
424 | case MOXA_BOARD_C218_ISA: | |
425 | case MOXA_BOARD_C218_PCI: | |
426 | if (model != 1) | |
427 | goto err; | |
428 | break; | |
429 | case MOXA_BOARD_CP204J: | |
430 | if (model != 3) | |
431 | goto err; | |
432 | break; | |
433 | default: | |
434 | if (model != 2) | |
435 | goto err; | |
436 | break; | |
437 | } | |
438 | return 0; | |
439 | err: | |
440 | return -EINVAL; | |
441 | } | |
442 | ||
443 | static int moxa_check_fw(const void *ptr) | |
444 | { | |
445 | const __le16 *lptr = ptr; | |
446 | ||
447 | if (*lptr != cpu_to_le16(0x7980)) | |
448 | return -EINVAL; | |
449 | ||
450 | return 0; | |
451 | } | |
452 | ||
453 | static int moxa_load_bios(struct moxa_board_conf *brd, const u8 *buf, | |
454 | size_t len) | |
455 | { | |
456 | void __iomem *baseAddr = brd->basemem; | |
457 | u16 tmp; | |
458 | ||
459 | writeb(HW_reset, baseAddr + Control_reg); /* reset */ | |
460 | msleep(10); | |
461 | memset_io(baseAddr, 0, 4096); | |
462 | memcpy_toio(baseAddr, buf, len); /* download BIOS */ | |
463 | writeb(0, baseAddr + Control_reg); /* restart */ | |
464 | ||
465 | msleep(2000); | |
466 | ||
467 | switch (brd->boardType) { | |
468 | case MOXA_BOARD_C218_ISA: | |
469 | case MOXA_BOARD_C218_PCI: | |
470 | tmp = readw(baseAddr + C218_key); | |
471 | if (tmp != C218_KeyCode) | |
472 | goto err; | |
473 | break; | |
474 | case MOXA_BOARD_CP204J: | |
475 | tmp = readw(baseAddr + C218_key); | |
476 | if (tmp != CP204J_KeyCode) | |
477 | goto err; | |
478 | break; | |
479 | default: | |
480 | tmp = readw(baseAddr + C320_key); | |
481 | if (tmp != C320_KeyCode) | |
482 | goto err; | |
483 | tmp = readw(baseAddr + C320_status); | |
484 | if (tmp != STS_init) { | |
eaa95a8d | 485 | printk(KERN_ERR "MOXA: bios upload failed -- CPU/Basic " |
03718234 JS |
486 | "module not found\n"); |
487 | return -EIO; | |
488 | } | |
489 | break; | |
490 | } | |
491 | ||
492 | return 0; | |
493 | err: | |
eaa95a8d | 494 | printk(KERN_ERR "MOXA: bios upload failed -- board not found\n"); |
03718234 JS |
495 | return -EIO; |
496 | } | |
497 | ||
498 | static int moxa_load_320b(struct moxa_board_conf *brd, const u8 *ptr, | |
499 | size_t len) | |
500 | { | |
501 | void __iomem *baseAddr = brd->basemem; | |
502 | ||
503 | if (len < 7168) { | |
eaa95a8d | 504 | printk(KERN_ERR "MOXA: invalid 320 bios -- too short\n"); |
03718234 JS |
505 | return -EINVAL; |
506 | } | |
507 | ||
508 | writew(len - 7168 - 2, baseAddr + C320bapi_len); | |
509 | writeb(1, baseAddr + Control_reg); /* Select Page 1 */ | |
510 | memcpy_toio(baseAddr + DynPage_addr, ptr, 7168); | |
511 | writeb(2, baseAddr + Control_reg); /* Select Page 2 */ | |
512 | memcpy_toio(baseAddr + DynPage_addr, ptr + 7168, len - 7168); | |
513 | ||
514 | return 0; | |
515 | } | |
516 | ||
5292bcd3 | 517 | static int moxa_real_load_code(struct moxa_board_conf *brd, const void *ptr, |
03718234 JS |
518 | size_t len) |
519 | { | |
520 | void __iomem *baseAddr = brd->basemem; | |
b46f69cd | 521 | const __le16 *uptr = ptr; |
03718234 | 522 | size_t wlen, len2, j; |
5292bcd3 | 523 | unsigned long key, loadbuf, loadlen, checksum, checksum_ok; |
08d01c79 | 524 | unsigned int i, retry; |
03718234 JS |
525 | u16 usum, keycode; |
526 | ||
5292bcd3 JS |
527 | keycode = (brd->boardType == MOXA_BOARD_CP204J) ? CP204J_KeyCode : |
528 | C218_KeyCode; | |
03718234 | 529 | |
5292bcd3 JS |
530 | switch (brd->boardType) { |
531 | case MOXA_BOARD_CP204J: | |
532 | case MOXA_BOARD_C218_ISA: | |
533 | case MOXA_BOARD_C218_PCI: | |
534 | key = C218_key; | |
535 | loadbuf = C218_LoadBuf; | |
536 | loadlen = C218DLoad_len; | |
537 | checksum = C218check_sum; | |
538 | checksum_ok = C218chksum_ok; | |
539 | break; | |
540 | default: | |
541 | key = C320_key; | |
542 | keycode = C320_KeyCode; | |
543 | loadbuf = C320_LoadBuf; | |
544 | loadlen = C320DLoad_len; | |
545 | checksum = C320check_sum; | |
546 | checksum_ok = C320chksum_ok; | |
547 | break; | |
03718234 | 548 | } |
03718234 JS |
549 | |
550 | usum = 0; | |
551 | wlen = len >> 1; | |
552 | for (i = 0; i < wlen; i++) | |
553 | usum += le16_to_cpu(uptr[i]); | |
554 | retry = 0; | |
555 | do { | |
556 | wlen = len >> 1; | |
557 | j = 0; | |
558 | while (wlen) { | |
559 | len2 = (wlen > 2048) ? 2048 : wlen; | |
560 | wlen -= len2; | |
5292bcd3 | 561 | memcpy_toio(baseAddr + loadbuf, ptr + j, len2 << 1); |
03718234 | 562 | j += len2 << 1; |
5292bcd3 JS |
563 | |
564 | writew(len2, baseAddr + loadlen); | |
565 | writew(0, baseAddr + key); | |
566 | for (i = 0; i < 100; i++) { | |
567 | if (readw(baseAddr + key) == keycode) | |
03718234 JS |
568 | break; |
569 | msleep(10); | |
570 | } | |
5292bcd3 | 571 | if (readw(baseAddr + key) != keycode) |
03718234 JS |
572 | return -EIO; |
573 | } | |
5292bcd3 JS |
574 | writew(0, baseAddr + loadlen); |
575 | writew(usum, baseAddr + checksum); | |
576 | writew(0, baseAddr + key); | |
577 | for (i = 0; i < 100; i++) { | |
578 | if (readw(baseAddr + key) == keycode) | |
03718234 JS |
579 | break; |
580 | msleep(10); | |
581 | } | |
582 | retry++; | |
5292bcd3 JS |
583 | } while ((readb(baseAddr + checksum_ok) != 1) && (retry < 3)); |
584 | if (readb(baseAddr + checksum_ok) != 1) | |
03718234 JS |
585 | return -EIO; |
586 | ||
5292bcd3 | 587 | writew(0, baseAddr + key); |
03718234 JS |
588 | for (i = 0; i < 600; i++) { |
589 | if (readw(baseAddr + Magic_no) == Magic_code) | |
590 | break; | |
591 | msleep(10); | |
592 | } | |
593 | if (readw(baseAddr + Magic_no) != Magic_code) | |
594 | return -EIO; | |
595 | ||
08d01c79 | 596 | if (MOXA_IS_320(brd)) { |
5292bcd3 JS |
597 | if (brd->busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */ |
598 | writew(0x3800, baseAddr + TMS320_PORT1); | |
599 | writew(0x3900, baseAddr + TMS320_PORT2); | |
600 | writew(28499, baseAddr + TMS320_CLOCK); | |
601 | } else { | |
602 | writew(0x3200, baseAddr + TMS320_PORT1); | |
603 | writew(0x3400, baseAddr + TMS320_PORT2); | |
604 | writew(19999, baseAddr + TMS320_CLOCK); | |
605 | } | |
03718234 JS |
606 | } |
607 | writew(1, baseAddr + Disable_IRQ); | |
608 | writew(0, baseAddr + Magic_no); | |
609 | for (i = 0; i < 500; i++) { | |
610 | if (readw(baseAddr + Magic_no) == Magic_code) | |
611 | break; | |
612 | msleep(10); | |
613 | } | |
614 | if (readw(baseAddr + Magic_no) != Magic_code) | |
615 | return -EIO; | |
616 | ||
08d01c79 | 617 | if (MOXA_IS_320(brd)) { |
5292bcd3 JS |
618 | j = readw(baseAddr + Module_cnt); |
619 | if (j <= 0) | |
620 | return -EIO; | |
621 | brd->numPorts = j * 8; | |
622 | writew(j, baseAddr + Module_no); | |
623 | writew(0, baseAddr + Magic_no); | |
624 | for (i = 0; i < 600; i++) { | |
625 | if (readw(baseAddr + Magic_no) == Magic_code) | |
626 | break; | |
627 | msleep(10); | |
628 | } | |
629 | if (readw(baseAddr + Magic_no) != Magic_code) | |
630 | return -EIO; | |
03718234 | 631 | } |
03718234 JS |
632 | brd->intNdx = baseAddr + IRQindex; |
633 | brd->intPend = baseAddr + IRQpending; | |
634 | brd->intTable = baseAddr + IRQtable; | |
635 | ||
636 | return 0; | |
637 | } | |
638 | ||
639 | static int moxa_load_code(struct moxa_board_conf *brd, const void *ptr, | |
640 | size_t len) | |
641 | { | |
642 | void __iomem *ofsAddr, *baseAddr = brd->basemem; | |
643 | struct moxa_port *port; | |
644 | int retval, i; | |
645 | ||
646 | if (len % 2) { | |
eaa95a8d | 647 | printk(KERN_ERR "MOXA: bios length is not even\n"); |
03718234 JS |
648 | return -EINVAL; |
649 | } | |
650 | ||
5292bcd3 JS |
651 | retval = moxa_real_load_code(brd, ptr, len); /* may change numPorts */ |
652 | if (retval) | |
653 | return retval; | |
654 | ||
03718234 JS |
655 | switch (brd->boardType) { |
656 | case MOXA_BOARD_C218_ISA: | |
657 | case MOXA_BOARD_C218_PCI: | |
658 | case MOXA_BOARD_CP204J: | |
03718234 JS |
659 | port = brd->ports; |
660 | for (i = 0; i < brd->numPorts; i++, port++) { | |
b4173f45 | 661 | port->board = brd; |
03718234 JS |
662 | port->DCDState = 0; |
663 | port->tableAddr = baseAddr + Extern_table + | |
664 | Extern_size * i; | |
665 | ofsAddr = port->tableAddr; | |
666 | writew(C218rx_mask, ofsAddr + RX_mask); | |
667 | writew(C218tx_mask, ofsAddr + TX_mask); | |
668 | writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb); | |
669 | writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb); | |
670 | ||
671 | writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb); | |
672 | writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb); | |
673 | ||
674 | } | |
675 | break; | |
676 | default: | |
03718234 JS |
677 | port = brd->ports; |
678 | for (i = 0; i < brd->numPorts; i++, port++) { | |
b4173f45 | 679 | port->board = brd; |
03718234 JS |
680 | port->DCDState = 0; |
681 | port->tableAddr = baseAddr + Extern_table + | |
682 | Extern_size * i; | |
683 | ofsAddr = port->tableAddr; | |
684 | switch (brd->numPorts) { | |
685 | case 8: | |
686 | writew(C320p8rx_mask, ofsAddr + RX_mask); | |
687 | writew(C320p8tx_mask, ofsAddr + TX_mask); | |
688 | writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb); | |
689 | writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb); | |
690 | writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb); | |
691 | writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb); | |
692 | ||
693 | break; | |
694 | case 16: | |
695 | writew(C320p16rx_mask, ofsAddr + RX_mask); | |
696 | writew(C320p16tx_mask, ofsAddr + TX_mask); | |
697 | writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb); | |
698 | writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb); | |
699 | writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb); | |
700 | writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb); | |
701 | break; | |
702 | ||
703 | case 24: | |
704 | writew(C320p24rx_mask, ofsAddr + RX_mask); | |
705 | writew(C320p24tx_mask, ofsAddr + TX_mask); | |
706 | writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb); | |
707 | writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb); | |
708 | writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb); | |
709 | writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb); | |
710 | break; | |
711 | case 32: | |
712 | writew(C320p32rx_mask, ofsAddr + RX_mask); | |
713 | writew(C320p32tx_mask, ofsAddr + TX_mask); | |
714 | writew(C320p32tx_ofs, ofsAddr + Ofs_txb); | |
715 | writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb); | |
716 | writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb); | |
717 | writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb); | |
718 | writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb); | |
719 | break; | |
720 | } | |
721 | } | |
722 | break; | |
723 | } | |
03718234 JS |
724 | return 0; |
725 | } | |
726 | ||
727 | static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw) | |
728 | { | |
2bca76e8 | 729 | const void *ptr = fw->data; |
03718234 JS |
730 | char rsn[64]; |
731 | u16 lens[5]; | |
732 | size_t len; | |
733 | unsigned int a, lenp, lencnt; | |
734 | int ret = -EINVAL; | |
735 | struct { | |
736 | __le32 magic; /* 0x34303430 */ | |
737 | u8 reserved1[2]; | |
738 | u8 type; /* UNIX = 3 */ | |
739 | u8 model; /* C218T=1, C320T=2, CP204=3 */ | |
740 | u8 reserved2[8]; | |
741 | __le16 len[5]; | |
2bca76e8 | 742 | } const *hdr = ptr; |
03718234 JS |
743 | |
744 | BUILD_BUG_ON(ARRAY_SIZE(hdr->len) != ARRAY_SIZE(lens)); | |
745 | ||
746 | if (fw->size < MOXA_FW_HDRLEN) { | |
747 | strcpy(rsn, "too short (even header won't fit)"); | |
748 | goto err; | |
749 | } | |
750 | if (hdr->magic != cpu_to_le32(0x30343034)) { | |
751 | sprintf(rsn, "bad magic: %.8x", le32_to_cpu(hdr->magic)); | |
752 | goto err; | |
753 | } | |
754 | if (hdr->type != 3) { | |
755 | sprintf(rsn, "not for linux, type is %u", hdr->type); | |
756 | goto err; | |
757 | } | |
758 | if (moxa_check_fw_model(brd, hdr->model)) { | |
759 | sprintf(rsn, "not for this card, model is %u", hdr->model); | |
760 | goto err; | |
761 | } | |
762 | ||
763 | len = MOXA_FW_HDRLEN; | |
764 | lencnt = hdr->model == 2 ? 5 : 3; | |
765 | for (a = 0; a < ARRAY_SIZE(lens); a++) { | |
766 | lens[a] = le16_to_cpu(hdr->len[a]); | |
767 | if (lens[a] && len + lens[a] <= fw->size && | |
768 | moxa_check_fw(&fw->data[len])) | |
eaa95a8d | 769 | printk(KERN_WARNING "MOXA firmware: unexpected input " |
03718234 JS |
770 | "at offset %u, but going on\n", (u32)len); |
771 | if (!lens[a] && a < lencnt) { | |
772 | sprintf(rsn, "too few entries in fw file"); | |
773 | goto err; | |
774 | } | |
775 | len += lens[a]; | |
776 | } | |
777 | ||
778 | if (len != fw->size) { | |
779 | sprintf(rsn, "bad length: %u (should be %u)", (u32)fw->size, | |
780 | (u32)len); | |
781 | goto err; | |
782 | } | |
783 | ||
784 | ptr += MOXA_FW_HDRLEN; | |
785 | lenp = 0; /* bios */ | |
786 | ||
787 | strcpy(rsn, "read above"); | |
788 | ||
789 | ret = moxa_load_bios(brd, ptr, lens[lenp]); | |
790 | if (ret) | |
791 | goto err; | |
792 | ||
793 | /* we skip the tty section (lens[1]), since we don't need it */ | |
794 | ptr += lens[lenp] + lens[lenp + 1]; | |
795 | lenp += 2; /* comm */ | |
796 | ||
797 | if (hdr->model == 2) { | |
798 | ret = moxa_load_320b(brd, ptr, lens[lenp]); | |
799 | if (ret) | |
800 | goto err; | |
801 | /* skip another tty */ | |
802 | ptr += lens[lenp] + lens[lenp + 1]; | |
803 | lenp += 2; | |
804 | } | |
805 | ||
806 | ret = moxa_load_code(brd, ptr, lens[lenp]); | |
807 | if (ret) | |
808 | goto err; | |
809 | ||
810 | return 0; | |
811 | err: | |
812 | printk(KERN_ERR "firmware failed to load, reason: %s\n", rsn); | |
813 | return ret; | |
814 | } | |
815 | ||
816 | static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev) | |
817 | { | |
818 | const struct firmware *fw; | |
819 | const char *file; | |
810ab09b JS |
820 | struct moxa_port *p; |
821 | unsigned int i; | |
03718234 JS |
822 | int ret; |
823 | ||
810ab09b JS |
824 | brd->ports = kcalloc(MAX_PORTS_PER_BOARD, sizeof(*brd->ports), |
825 | GFP_KERNEL); | |
826 | if (brd->ports == NULL) { | |
827 | printk(KERN_ERR "cannot allocate memory for ports\n"); | |
828 | ret = -ENOMEM; | |
829 | goto err; | |
830 | } | |
831 | ||
832 | for (i = 0, p = brd->ports; i < MAX_PORTS_PER_BOARD; i++, p++) { | |
44b7d1b3 | 833 | tty_port_init(&p->port); |
31f35939 | 834 | p->port.ops = &moxa_port_ops; |
810ab09b | 835 | p->type = PORT_16550A; |
810ab09b | 836 | p->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL; |
810ab09b JS |
837 | } |
838 | ||
03718234 JS |
839 | switch (brd->boardType) { |
840 | case MOXA_BOARD_C218_ISA: | |
841 | case MOXA_BOARD_C218_PCI: | |
842 | file = "c218tunx.cod"; | |
843 | break; | |
844 | case MOXA_BOARD_CP204J: | |
845 | file = "cp204unx.cod"; | |
846 | break; | |
847 | default: | |
848 | file = "c320tunx.cod"; | |
849 | break; | |
850 | } | |
851 | ||
852 | ret = request_firmware(&fw, file, dev); | |
853 | if (ret) { | |
ec09cd56 JS |
854 | printk(KERN_ERR "MOXA: request_firmware failed. Make sure " |
855 | "you've placed '%s' file into your firmware " | |
856 | "loader directory (e.g. /lib/firmware)\n", | |
857 | file); | |
810ab09b | 858 | goto err_free; |
03718234 JS |
859 | } |
860 | ||
861 | ret = moxa_load_fw(brd, fw); | |
862 | ||
863 | release_firmware(fw); | |
810ab09b JS |
864 | |
865 | if (ret) | |
866 | goto err_free; | |
867 | ||
2a541341 | 868 | spin_lock_bh(&moxa_lock); |
810ab09b | 869 | brd->ready = 1; |
0bcc4caa JS |
870 | if (!timer_pending(&moxaTimer)) |
871 | mod_timer(&moxaTimer, jiffies + HZ / 50); | |
2a541341 | 872 | spin_unlock_bh(&moxa_lock); |
0bcc4caa | 873 | |
810ab09b JS |
874 | return 0; |
875 | err_free: | |
876 | kfree(brd->ports); | |
877 | err: | |
03718234 JS |
878 | return ret; |
879 | } | |
880 | ||
810ab09b JS |
881 | static void moxa_board_deinit(struct moxa_board_conf *brd) |
882 | { | |
a8f5cda0 JS |
883 | unsigned int a, opened; |
884 | ||
885 | mutex_lock(&moxa_openlock); | |
7bcf97d1 | 886 | spin_lock_bh(&moxa_lock); |
810ab09b | 887 | brd->ready = 0; |
7bcf97d1 | 888 | spin_unlock_bh(&moxa_lock); |
a8f5cda0 JS |
889 | |
890 | /* pci hot-un-plug support */ | |
891 | for (a = 0; a < brd->numPorts; a++) | |
d450b5a0 AC |
892 | if (brd->ports[a].port.flags & ASYNC_INITIALIZED) { |
893 | struct tty_struct *tty = tty_port_tty_get( | |
894 | &brd->ports[a].port); | |
895 | if (tty) { | |
896 | tty_hangup(tty); | |
897 | tty_kref_put(tty); | |
898 | } | |
899 | } | |
a8f5cda0 JS |
900 | while (1) { |
901 | opened = 0; | |
902 | for (a = 0; a < brd->numPorts; a++) | |
9de6a51f | 903 | if (brd->ports[a].port.flags & ASYNC_INITIALIZED) |
a8f5cda0 JS |
904 | opened++; |
905 | mutex_unlock(&moxa_openlock); | |
906 | if (!opened) | |
907 | break; | |
908 | msleep(50); | |
909 | mutex_lock(&moxa_openlock); | |
910 | } | |
911 | ||
810ab09b JS |
912 | iounmap(brd->basemem); |
913 | brd->basemem = NULL; | |
914 | kfree(brd->ports); | |
915 | } | |
916 | ||
1da177e4 | 917 | #ifdef CONFIG_PCI |
9cde5bf0 JS |
918 | static int __devinit moxa_pci_probe(struct pci_dev *pdev, |
919 | const struct pci_device_id *ent) | |
1da177e4 | 920 | { |
9cde5bf0 JS |
921 | struct moxa_board_conf *board; |
922 | unsigned int i; | |
923 | int board_type = ent->driver_data; | |
924 | int retval; | |
925 | ||
926 | retval = pci_enable_device(pdev); | |
7aeb95da JS |
927 | if (retval) { |
928 | dev_err(&pdev->dev, "can't enable pci device\n"); | |
9cde5bf0 | 929 | goto err; |
7aeb95da | 930 | } |
9cde5bf0 JS |
931 | |
932 | for (i = 0; i < MAX_BOARDS; i++) | |
933 | if (moxa_boards[i].basemem == NULL) | |
934 | break; | |
935 | ||
936 | retval = -ENODEV; | |
937 | if (i >= MAX_BOARDS) { | |
7aeb95da | 938 | dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards " |
9cde5bf0 JS |
939 | "found. Board is ignored.\n", MAX_BOARDS); |
940 | goto err; | |
941 | } | |
942 | ||
943 | board = &moxa_boards[i]; | |
e46a5e3f JS |
944 | |
945 | retval = pci_request_region(pdev, 2, "moxa-base"); | |
946 | if (retval) { | |
947 | dev_err(&pdev->dev, "can't request pci region 2\n"); | |
948 | goto err; | |
949 | } | |
950 | ||
24cb2335 | 951 | board->basemem = ioremap_nocache(pci_resource_start(pdev, 2), 0x4000); |
7aeb95da JS |
952 | if (board->basemem == NULL) { |
953 | dev_err(&pdev->dev, "can't remap io space 2\n"); | |
e46a5e3f | 954 | goto err_reg; |
7aeb95da | 955 | } |
9cde5bf0 | 956 | |
1da177e4 LT |
957 | board->boardType = board_type; |
958 | switch (board_type) { | |
959 | case MOXA_BOARD_C218_ISA: | |
960 | case MOXA_BOARD_C218_PCI: | |
961 | board->numPorts = 8; | |
962 | break; | |
963 | ||
964 | case MOXA_BOARD_CP204J: | |
965 | board->numPorts = 4; | |
966 | break; | |
967 | default: | |
968 | board->numPorts = 0; | |
969 | break; | |
970 | } | |
971 | board->busType = MOXA_BUS_TYPE_PCI; | |
a784bf7c | 972 | |
03718234 JS |
973 | retval = moxa_init_board(board, &pdev->dev); |
974 | if (retval) | |
975 | goto err_base; | |
976 | ||
9cde5bf0 | 977 | pci_set_drvdata(pdev, board); |
1da177e4 | 978 | |
bb9f910a JS |
979 | dev_info(&pdev->dev, "board '%s' ready (%u ports, firmware loaded)\n", |
980 | moxa_brdname[board_type - 1], board->numPorts); | |
981 | ||
eaa95a8d | 982 | return 0; |
03718234 JS |
983 | err_base: |
984 | iounmap(board->basemem); | |
985 | board->basemem = NULL; | |
e46a5e3f JS |
986 | err_reg: |
987 | pci_release_region(pdev, 2); | |
9cde5bf0 JS |
988 | err: |
989 | return retval; | |
990 | } | |
991 | ||
992 | static void __devexit moxa_pci_remove(struct pci_dev *pdev) | |
993 | { | |
994 | struct moxa_board_conf *brd = pci_get_drvdata(pdev); | |
995 | ||
810ab09b JS |
996 | moxa_board_deinit(brd); |
997 | ||
e46a5e3f | 998 | pci_release_region(pdev, 2); |
1da177e4 | 999 | } |
a784bf7c JS |
1000 | |
1001 | static struct pci_driver moxa_pci_driver = { | |
1002 | .name = "moxa", | |
1003 | .id_table = moxa_pcibrds, | |
1004 | .probe = moxa_pci_probe, | |
1005 | .remove = __devexit_p(moxa_pci_remove) | |
1006 | }; | |
1da177e4 LT |
1007 | #endif /* CONFIG_PCI */ |
1008 | ||
1009 | static int __init moxa_init(void) | |
1010 | { | |
810ab09b | 1011 | unsigned int isabrds = 0; |
d353eca4 | 1012 | int retval = 0; |
1da177e4 | 1013 | |
7aeb95da JS |
1014 | printk(KERN_INFO "MOXA Intellio family driver version %s\n", |
1015 | MOXA_VERSION); | |
1da177e4 LT |
1016 | moxaDriver = alloc_tty_driver(MAX_PORTS + 1); |
1017 | if (!moxaDriver) | |
1018 | return -ENOMEM; | |
1019 | ||
1da177e4 | 1020 | moxaDriver->owner = THIS_MODULE; |
9b4e3b13 | 1021 | moxaDriver->name = "ttyMX"; |
1da177e4 LT |
1022 | moxaDriver->major = ttymajor; |
1023 | moxaDriver->minor_start = 0; | |
1024 | moxaDriver->type = TTY_DRIVER_TYPE_SERIAL; | |
1025 | moxaDriver->subtype = SERIAL_TYPE_NORMAL; | |
1026 | moxaDriver->init_termios = tty_std_termios; | |
1da177e4 | 1027 | moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL; |
606d099c AC |
1028 | moxaDriver->init_termios.c_ispeed = 9600; |
1029 | moxaDriver->init_termios.c_ospeed = 9600; | |
1da177e4 LT |
1030 | moxaDriver->flags = TTY_DRIVER_REAL_RAW; |
1031 | tty_set_operations(moxaDriver, &moxa_ops); | |
1032 | ||
1da177e4 | 1033 | if (tty_register_driver(moxaDriver)) { |
eaa95a8d | 1034 | printk(KERN_ERR "can't register MOXA Smartio tty driver!\n"); |
1da177e4 LT |
1035 | put_tty_driver(moxaDriver); |
1036 | return -1; | |
1037 | } | |
1da177e4 | 1038 | |
d353eca4 | 1039 | /* Find the boards defined from module args. */ |
1da177e4 | 1040 | #ifdef MODULE |
d353eca4 JS |
1041 | { |
1042 | struct moxa_board_conf *brd = moxa_boards; | |
810ab09b | 1043 | unsigned int i; |
1da177e4 | 1044 | for (i = 0; i < MAX_BOARDS; i++) { |
d353eca4 JS |
1045 | if (!baseaddr[i]) |
1046 | break; | |
1047 | if (type[i] == MOXA_BOARD_C218_ISA || | |
1048 | type[i] == MOXA_BOARD_C320_ISA) { | |
7aeb95da | 1049 | pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n", |
d353eca4 JS |
1050 | isabrds + 1, moxa_brdname[type[i] - 1], |
1051 | baseaddr[i]); | |
1052 | brd->boardType = type[i]; | |
1053 | brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 : | |
1054 | numports[i]; | |
1055 | brd->busType = MOXA_BUS_TYPE_ISA; | |
24cb2335 | 1056 | brd->basemem = ioremap_nocache(baseaddr[i], 0x4000); |
d353eca4 | 1057 | if (!brd->basemem) { |
eaa95a8d | 1058 | printk(KERN_ERR "MOXA: can't remap %lx\n", |
d353eca4 | 1059 | baseaddr[i]); |
1da177e4 LT |
1060 | continue; |
1061 | } | |
03718234 JS |
1062 | if (moxa_init_board(brd, NULL)) { |
1063 | iounmap(brd->basemem); | |
1064 | brd->basemem = NULL; | |
1065 | continue; | |
1066 | } | |
d353eca4 | 1067 | |
bb9f910a JS |
1068 | printk(KERN_INFO "MOXA isa board found at 0x%.8lu and " |
1069 | "ready (%u ports, firmware loaded)\n", | |
1070 | baseaddr[i], brd->numPorts); | |
1071 | ||
d353eca4 JS |
1072 | brd++; |
1073 | isabrds++; | |
1da177e4 LT |
1074 | } |
1075 | } | |
d353eca4 | 1076 | } |
1da177e4 | 1077 | #endif |
a784bf7c | 1078 | |
1da177e4 | 1079 | #ifdef CONFIG_PCI |
a784bf7c JS |
1080 | retval = pci_register_driver(&moxa_pci_driver); |
1081 | if (retval) { | |
eaa95a8d | 1082 | printk(KERN_ERR "Can't register MOXA pci driver!\n"); |
d353eca4 | 1083 | if (isabrds) |
a784bf7c | 1084 | retval = 0; |
1da177e4 LT |
1085 | } |
1086 | #endif | |
a784bf7c | 1087 | |
a784bf7c | 1088 | return retval; |
1da177e4 LT |
1089 | } |
1090 | ||
1091 | static void __exit moxa_exit(void) | |
1092 | { | |
eaa95a8d | 1093 | unsigned int i; |
1da177e4 | 1094 | |
9cde5bf0 | 1095 | #ifdef CONFIG_PCI |
a784bf7c | 1096 | pci_unregister_driver(&moxa_pci_driver); |
9cde5bf0 | 1097 | #endif |
a784bf7c | 1098 | |
810ab09b JS |
1099 | for (i = 0; i < MAX_BOARDS; i++) /* ISA boards */ |
1100 | if (moxa_boards[i].ready) | |
1101 | moxa_board_deinit(&moxa_boards[i]); | |
2a541341 JS |
1102 | |
1103 | del_timer_sync(&moxaTimer); | |
1104 | ||
1105 | if (tty_unregister_driver(moxaDriver)) | |
1106 | printk(KERN_ERR "Couldn't unregister MOXA Intellio family " | |
1107 | "serial driver\n"); | |
1108 | put_tty_driver(moxaDriver); | |
1da177e4 LT |
1109 | } |
1110 | ||
1111 | module_init(moxa_init); | |
1112 | module_exit(moxa_exit); | |
1113 | ||
d450b5a0 | 1114 | static void moxa_close_port(struct tty_struct *tty) |
a8f5cda0 | 1115 | { |
d450b5a0 AC |
1116 | struct moxa_port *ch = tty->driver_data; |
1117 | moxa_shut_down(tty); | |
a8f5cda0 | 1118 | MoxaPortFlushData(ch, 2); |
9de6a51f | 1119 | ch->port.flags &= ~ASYNC_NORMAL_ACTIVE; |
d450b5a0 AC |
1120 | tty->driver_data = NULL; |
1121 | tty_port_tty_set(&ch->port, NULL); | |
a8f5cda0 JS |
1122 | } |
1123 | ||
31f35939 AC |
1124 | static int moxa_carrier_raised(struct tty_port *port) |
1125 | { | |
1126 | struct moxa_port *ch = container_of(port, struct moxa_port, port); | |
1127 | int dcd; | |
1128 | ||
1129 | spin_lock_bh(&moxa_lock); | |
1130 | dcd = ch->DCDState; | |
1131 | spin_unlock_bh(&moxa_lock); | |
1132 | return dcd; | |
1133 | } | |
1134 | ||
a8f5cda0 JS |
1135 | static int moxa_block_till_ready(struct tty_struct *tty, struct file *filp, |
1136 | struct moxa_port *ch) | |
1137 | { | |
31f35939 | 1138 | struct tty_port *port = &ch->port; |
a8f5cda0 JS |
1139 | DEFINE_WAIT(wait); |
1140 | int retval = 0; | |
1141 | u8 dcd; | |
1142 | ||
1143 | while (1) { | |
31f35939 | 1144 | prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE); |
a8f5cda0 JS |
1145 | if (tty_hung_up_p(filp)) { |
1146 | #ifdef SERIAL_DO_RESTART | |
1147 | retval = -ERESTARTSYS; | |
1148 | #else | |
1149 | retval = -EAGAIN; | |
1150 | #endif | |
1151 | break; | |
1152 | } | |
31f35939 | 1153 | dcd = tty_port_carrier_raised(port); |
a8f5cda0 JS |
1154 | if (dcd) |
1155 | break; | |
1156 | ||
1157 | if (signal_pending(current)) { | |
1158 | retval = -ERESTARTSYS; | |
1159 | break; | |
1160 | } | |
1161 | schedule(); | |
1162 | } | |
31f35939 | 1163 | finish_wait(&port->open_wait, &wait); |
a8f5cda0 JS |
1164 | |
1165 | return retval; | |
1166 | } | |
1167 | ||
1da177e4 LT |
1168 | static int moxa_open(struct tty_struct *tty, struct file *filp) |
1169 | { | |
810ab09b | 1170 | struct moxa_board_conf *brd; |
8f8ecbad | 1171 | struct moxa_port *ch; |
1da177e4 LT |
1172 | int port; |
1173 | int retval; | |
1da177e4 | 1174 | |
11324edd | 1175 | port = tty->index; |
1da177e4 | 1176 | if (port == MAX_PORTS) { |
74d7d97b | 1177 | return capable(CAP_SYS_ADMIN) ? 0 : -EPERM; |
1da177e4 | 1178 | } |
a8f5cda0 JS |
1179 | if (mutex_lock_interruptible(&moxa_openlock)) |
1180 | return -ERESTARTSYS; | |
810ab09b | 1181 | brd = &moxa_boards[port / MAX_PORTS_PER_BOARD]; |
a8f5cda0 JS |
1182 | if (!brd->ready) { |
1183 | mutex_unlock(&moxa_openlock); | |
810ab09b | 1184 | return -ENODEV; |
a8f5cda0 | 1185 | } |
1da177e4 | 1186 | |
810ab09b | 1187 | ch = &brd->ports[port % MAX_PORTS_PER_BOARD]; |
9de6a51f | 1188 | ch->port.count++; |
1da177e4 | 1189 | tty->driver_data = ch; |
d450b5a0 | 1190 | tty_port_tty_set(&ch->port, tty); |
9de6a51f | 1191 | if (!(ch->port.flags & ASYNC_INITIALIZED)) { |
1da177e4 | 1192 | ch->statusflags = 0; |
db1acaa6 | 1193 | moxa_set_tty_param(tty, tty->termios); |
b4173f45 JS |
1194 | MoxaPortLineCtrl(ch, 1, 1); |
1195 | MoxaPortEnable(ch); | |
a8f5cda0 | 1196 | MoxaSetFifo(ch, ch->type == PORT_16550A); |
9de6a51f | 1197 | ch->port.flags |= ASYNC_INITIALIZED; |
1da177e4 | 1198 | } |
a8f5cda0 | 1199 | mutex_unlock(&moxa_openlock); |
1da177e4 | 1200 | |
a8f5cda0 JS |
1201 | retval = 0; |
1202 | if (!(filp->f_flags & O_NONBLOCK) && !C_CLOCAL(tty)) | |
1203 | retval = moxa_block_till_ready(tty, filp, ch); | |
1204 | mutex_lock(&moxa_openlock); | |
1205 | if (retval) { | |
9de6a51f AC |
1206 | if (ch->port.count) /* 0 means already hung up... */ |
1207 | if (--ch->port.count == 0) | |
d450b5a0 | 1208 | moxa_close_port(tty); |
a8f5cda0 | 1209 | } else |
9de6a51f | 1210 | ch->port.flags |= ASYNC_NORMAL_ACTIVE; |
a8f5cda0 | 1211 | mutex_unlock(&moxa_openlock); |
1da177e4 | 1212 | |
a8f5cda0 | 1213 | return retval; |
1da177e4 LT |
1214 | } |
1215 | ||
1216 | static void moxa_close(struct tty_struct *tty, struct file *filp) | |
1217 | { | |
8f8ecbad | 1218 | struct moxa_port *ch; |
1da177e4 LT |
1219 | int port; |
1220 | ||
11324edd | 1221 | port = tty->index; |
a8f5cda0 | 1222 | if (port == MAX_PORTS || tty_hung_up_p(filp)) |
1da177e4 | 1223 | return; |
1da177e4 | 1224 | |
a8f5cda0 JS |
1225 | mutex_lock(&moxa_openlock); |
1226 | ch = tty->driver_data; | |
1227 | if (ch == NULL) | |
1228 | goto unlock; | |
9de6a51f | 1229 | if (tty->count == 1 && ch->port.count != 1) { |
7aeb95da | 1230 | printk(KERN_WARNING "moxa_close: bad serial port count; " |
9de6a51f AC |
1231 | "tty->count is 1, ch->port.count is %d\n", ch->port.count); |
1232 | ch->port.count = 1; | |
1da177e4 | 1233 | } |
9de6a51f | 1234 | if (--ch->port.count < 0) { |
7aeb95da JS |
1235 | printk(KERN_WARNING "moxa_close: bad serial port count, " |
1236 | "device=%s\n", tty->name); | |
9de6a51f | 1237 | ch->port.count = 0; |
1da177e4 | 1238 | } |
9de6a51f | 1239 | if (ch->port.count) |
a8f5cda0 | 1240 | goto unlock; |
1da177e4 LT |
1241 | |
1242 | ch->cflag = tty->termios->c_cflag; | |
9de6a51f | 1243 | if (ch->port.flags & ASYNC_INITIALIZED) { |
6f56b658 | 1244 | moxa_setup_empty_event(tty); |
1da177e4 | 1245 | tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */ |
1da177e4 | 1246 | } |
1da177e4 | 1247 | |
d450b5a0 | 1248 | moxa_close_port(tty); |
a8f5cda0 JS |
1249 | unlock: |
1250 | mutex_unlock(&moxa_openlock); | |
1da177e4 LT |
1251 | } |
1252 | ||
1253 | static int moxa_write(struct tty_struct *tty, | |
1254 | const unsigned char *buf, int count) | |
1255 | { | |
b4173f45 | 1256 | struct moxa_port *ch = tty->driver_data; |
b4173f45 | 1257 | int len; |
1da177e4 | 1258 | |
1da177e4 | 1259 | if (ch == NULL) |
b4173f45 | 1260 | return 0; |
33f0f88f | 1261 | |
7bcf97d1 | 1262 | spin_lock_bh(&moxa_lock); |
d450b5a0 | 1263 | len = MoxaPortWriteData(tty, buf, count); |
7bcf97d1 | 1264 | spin_unlock_bh(&moxa_lock); |
1da177e4 | 1265 | |
1da177e4 | 1266 | ch->statusflags |= LOWWAIT; |
eaa95a8d | 1267 | return len; |
1da177e4 LT |
1268 | } |
1269 | ||
1270 | static int moxa_write_room(struct tty_struct *tty) | |
1271 | { | |
8f8ecbad | 1272 | struct moxa_port *ch; |
1da177e4 LT |
1273 | |
1274 | if (tty->stopped) | |
eaa95a8d | 1275 | return 0; |
b4173f45 | 1276 | ch = tty->driver_data; |
1da177e4 | 1277 | if (ch == NULL) |
eaa95a8d | 1278 | return 0; |
b4173f45 | 1279 | return MoxaPortTxFree(ch); |
1da177e4 LT |
1280 | } |
1281 | ||
1282 | static void moxa_flush_buffer(struct tty_struct *tty) | |
1283 | { | |
b4173f45 | 1284 | struct moxa_port *ch = tty->driver_data; |
1da177e4 LT |
1285 | |
1286 | if (ch == NULL) | |
1287 | return; | |
b4173f45 | 1288 | MoxaPortFlushData(ch, 1); |
1da177e4 LT |
1289 | tty_wakeup(tty); |
1290 | } | |
1291 | ||
1292 | static int moxa_chars_in_buffer(struct tty_struct *tty) | |
1293 | { | |
b4173f45 | 1294 | struct moxa_port *ch = tty->driver_data; |
1da177e4 | 1295 | int chars; |
1da177e4 LT |
1296 | |
1297 | /* | |
1298 | * Sigh...I have to check if driver_data is NULL here, because | |
1299 | * if an open() fails, the TTY subsystem eventually calls | |
1300 | * tty_wait_until_sent(), which calls the driver's chars_in_buffer() | |
1301 | * routine. And since the open() failed, we return 0 here. TDJ | |
1302 | */ | |
1303 | if (ch == NULL) | |
eaa95a8d | 1304 | return 0; |
978e595f | 1305 | lock_kernel(); |
b4173f45 | 1306 | chars = MoxaPortTxQueue(ch); |
1da177e4 LT |
1307 | if (chars) { |
1308 | /* | |
1309 | * Make it possible to wakeup anything waiting for output | |
1310 | * in tty_ioctl.c, etc. | |
1311 | */ | |
1312 | if (!(ch->statusflags & EMPTYWAIT)) | |
6f56b658 | 1313 | moxa_setup_empty_event(tty); |
1da177e4 | 1314 | } |
978e595f | 1315 | unlock_kernel(); |
eaa95a8d | 1316 | return chars; |
1da177e4 LT |
1317 | } |
1318 | ||
1da177e4 LT |
1319 | static int moxa_tiocmget(struct tty_struct *tty, struct file *file) |
1320 | { | |
a8f5cda0 | 1321 | struct moxa_port *ch; |
1da177e4 LT |
1322 | int flag = 0, dtr, rts; |
1323 | ||
a8f5cda0 JS |
1324 | mutex_lock(&moxa_openlock); |
1325 | ch = tty->driver_data; | |
1326 | if (!ch) { | |
1327 | mutex_unlock(&moxa_openlock); | |
74d7d97b | 1328 | return -EINVAL; |
a8f5cda0 | 1329 | } |
1da177e4 | 1330 | |
b4173f45 | 1331 | MoxaPortGetLineOut(ch, &dtr, &rts); |
1da177e4 LT |
1332 | if (dtr) |
1333 | flag |= TIOCM_DTR; | |
1334 | if (rts) | |
1335 | flag |= TIOCM_RTS; | |
b4173f45 | 1336 | dtr = MoxaPortLineStatus(ch); |
1da177e4 LT |
1337 | if (dtr & 1) |
1338 | flag |= TIOCM_CTS; | |
1339 | if (dtr & 2) | |
1340 | flag |= TIOCM_DSR; | |
1341 | if (dtr & 4) | |
1342 | flag |= TIOCM_CD; | |
a8f5cda0 | 1343 | mutex_unlock(&moxa_openlock); |
1da177e4 LT |
1344 | return flag; |
1345 | } | |
1346 | ||
1347 | static int moxa_tiocmset(struct tty_struct *tty, struct file *file, | |
1348 | unsigned int set, unsigned int clear) | |
1349 | { | |
a8f5cda0 | 1350 | struct moxa_port *ch; |
1da177e4 LT |
1351 | int port; |
1352 | int dtr, rts; | |
1353 | ||
11324edd | 1354 | port = tty->index; |
a8f5cda0 JS |
1355 | mutex_lock(&moxa_openlock); |
1356 | ch = tty->driver_data; | |
1357 | if (!ch) { | |
1358 | mutex_unlock(&moxa_openlock); | |
74d7d97b | 1359 | return -EINVAL; |
a8f5cda0 | 1360 | } |
1da177e4 | 1361 | |
b4173f45 | 1362 | MoxaPortGetLineOut(ch, &dtr, &rts); |
1da177e4 LT |
1363 | if (set & TIOCM_RTS) |
1364 | rts = 1; | |
1365 | if (set & TIOCM_DTR) | |
1366 | dtr = 1; | |
1367 | if (clear & TIOCM_RTS) | |
1368 | rts = 0; | |
1369 | if (clear & TIOCM_DTR) | |
1370 | dtr = 0; | |
b4173f45 | 1371 | MoxaPortLineCtrl(ch, dtr, rts); |
a8f5cda0 | 1372 | mutex_unlock(&moxa_openlock); |
1da177e4 LT |
1373 | return 0; |
1374 | } | |
1375 | ||
1da177e4 LT |
1376 | static void moxa_throttle(struct tty_struct *tty) |
1377 | { | |
eaa95a8d | 1378 | struct moxa_port *ch = tty->driver_data; |
1da177e4 LT |
1379 | |
1380 | ch->statusflags |= THROTTLE; | |
1381 | } | |
1382 | ||
1383 | static void moxa_unthrottle(struct tty_struct *tty) | |
1384 | { | |
eaa95a8d | 1385 | struct moxa_port *ch = tty->driver_data; |
1da177e4 LT |
1386 | |
1387 | ch->statusflags &= ~THROTTLE; | |
1388 | } | |
1389 | ||
1390 | static void moxa_set_termios(struct tty_struct *tty, | |
eaa95a8d | 1391 | struct ktermios *old_termios) |
1da177e4 | 1392 | { |
eaa95a8d | 1393 | struct moxa_port *ch = tty->driver_data; |
1da177e4 LT |
1394 | |
1395 | if (ch == NULL) | |
1396 | return; | |
db1acaa6 | 1397 | moxa_set_tty_param(tty, old_termios); |
eaa95a8d | 1398 | if (!(old_termios->c_cflag & CLOCAL) && C_CLOCAL(tty)) |
9de6a51f | 1399 | wake_up_interruptible(&ch->port.open_wait); |
1da177e4 LT |
1400 | } |
1401 | ||
1402 | static void moxa_stop(struct tty_struct *tty) | |
1403 | { | |
eaa95a8d | 1404 | struct moxa_port *ch = tty->driver_data; |
1da177e4 LT |
1405 | |
1406 | if (ch == NULL) | |
1407 | return; | |
b4173f45 | 1408 | MoxaPortTxDisable(ch); |
1da177e4 LT |
1409 | ch->statusflags |= TXSTOPPED; |
1410 | } | |
1411 | ||
1412 | ||
1413 | static void moxa_start(struct tty_struct *tty) | |
1414 | { | |
eaa95a8d | 1415 | struct moxa_port *ch = tty->driver_data; |
1da177e4 LT |
1416 | |
1417 | if (ch == NULL) | |
1418 | return; | |
1419 | ||
1420 | if (!(ch->statusflags & TXSTOPPED)) | |
1421 | return; | |
1422 | ||
b4173f45 | 1423 | MoxaPortTxEnable(ch); |
1da177e4 LT |
1424 | ch->statusflags &= ~TXSTOPPED; |
1425 | } | |
1426 | ||
1427 | static void moxa_hangup(struct tty_struct *tty) | |
1428 | { | |
a8f5cda0 | 1429 | struct moxa_port *ch; |
1da177e4 | 1430 | |
a8f5cda0 JS |
1431 | mutex_lock(&moxa_openlock); |
1432 | ch = tty->driver_data; | |
1433 | if (ch == NULL) { | |
1434 | mutex_unlock(&moxa_openlock); | |
1435 | return; | |
1436 | } | |
9de6a51f | 1437 | ch->port.count = 0; |
d450b5a0 | 1438 | moxa_close_port(tty); |
a8f5cda0 JS |
1439 | mutex_unlock(&moxa_openlock); |
1440 | ||
9de6a51f | 1441 | wake_up_interruptible(&ch->port.open_wait); |
1da177e4 LT |
1442 | } |
1443 | ||
7bcf97d1 | 1444 | static void moxa_new_dcdstate(struct moxa_port *p, u8 dcd) |
1da177e4 | 1445 | { |
d450b5a0 | 1446 | struct tty_struct *tty; |
7bcf97d1 | 1447 | dcd = !!dcd; |
1da177e4 | 1448 | |
d450b5a0 AC |
1449 | if (dcd != p->DCDState) { |
1450 | tty = tty_port_tty_get(&p->port); | |
1451 | if (tty && C_CLOCAL(tty) && !dcd) | |
1452 | tty_hangup(tty); | |
1453 | tty_kref_put(tty); | |
7bcf97d1 JS |
1454 | } |
1455 | p->DCDState = dcd; | |
1456 | } | |
1da177e4 | 1457 | |
7bcf97d1 JS |
1458 | static int moxa_poll_port(struct moxa_port *p, unsigned int handle, |
1459 | u16 __iomem *ip) | |
1460 | { | |
d450b5a0 | 1461 | struct tty_struct *tty = tty_port_tty_get(&p->port); |
7bcf97d1 | 1462 | void __iomem *ofsAddr; |
9de6a51f | 1463 | unsigned int inited = p->port.flags & ASYNC_INITIALIZED; |
7bcf97d1 JS |
1464 | u16 intr; |
1465 | ||
1466 | if (tty) { | |
1467 | if ((p->statusflags & EMPTYWAIT) && | |
1468 | MoxaPortTxQueue(p) == 0) { | |
1469 | p->statusflags &= ~EMPTYWAIT; | |
1470 | tty_wakeup(tty); | |
1471 | } | |
1472 | if ((p->statusflags & LOWWAIT) && !tty->stopped && | |
1473 | MoxaPortTxQueue(p) <= WAKEUP_CHARS) { | |
1474 | p->statusflags &= ~LOWWAIT; | |
1475 | tty_wakeup(tty); | |
1476 | } | |
1477 | ||
1478 | if (inited && !(p->statusflags & THROTTLE) && | |
1479 | MoxaPortRxQueue(p) > 0) { /* RX */ | |
1480 | MoxaPortReadData(p); | |
1481 | tty_schedule_flip(tty); | |
1482 | } | |
1483 | } else { | |
1484 | p->statusflags &= ~EMPTYWAIT; | |
1485 | MoxaPortFlushData(p, 0); /* flush RX */ | |
1da177e4 | 1486 | } |
0bcc4caa | 1487 | |
7bcf97d1 | 1488 | if (!handle) /* nothing else to do */ |
0e0fd7d7 | 1489 | goto put; |
7bcf97d1 JS |
1490 | |
1491 | intr = readw(ip); /* port irq status */ | |
1492 | if (intr == 0) | |
0e0fd7d7 | 1493 | goto put; |
7bcf97d1 JS |
1494 | |
1495 | writew(0, ip); /* ACK port */ | |
1496 | ofsAddr = p->tableAddr; | |
1497 | if (intr & IntrTx) /* disable tx intr */ | |
1498 | writew(readw(ofsAddr + HostStat) & ~WakeupTx, | |
1499 | ofsAddr + HostStat); | |
1500 | ||
1501 | if (!inited) | |
0e0fd7d7 | 1502 | goto put; |
7bcf97d1 JS |
1503 | |
1504 | if (tty && (intr & IntrBreak) && !I_IGNBRK(tty)) { /* BREAK */ | |
1505 | tty_insert_flip_char(tty, 0, TTY_BREAK); | |
1506 | tty_schedule_flip(tty); | |
1507 | } | |
1508 | ||
1509 | if (intr & IntrLine) | |
1510 | moxa_new_dcdstate(p, readb(ofsAddr + FlagStat) & DCD_state); | |
0e0fd7d7 JS |
1511 | put: |
1512 | tty_kref_put(tty); | |
7bcf97d1 JS |
1513 | |
1514 | return 0; | |
1515 | } | |
1516 | ||
1517 | static void moxa_poll(unsigned long ignored) | |
1518 | { | |
1519 | struct moxa_board_conf *brd; | |
1520 | u16 __iomem *ip; | |
2a541341 | 1521 | unsigned int card, port, served = 0; |
7bcf97d1 JS |
1522 | |
1523 | spin_lock(&moxa_lock); | |
1da177e4 | 1524 | for (card = 0; card < MAX_BOARDS; card++) { |
7bcf97d1 JS |
1525 | brd = &moxa_boards[card]; |
1526 | if (!brd->ready) | |
1da177e4 | 1527 | continue; |
7bcf97d1 | 1528 | |
2a541341 JS |
1529 | served++; |
1530 | ||
7bcf97d1 JS |
1531 | ip = NULL; |
1532 | if (readb(brd->intPend) == 0xff) | |
1533 | ip = brd->intTable + readb(brd->intNdx); | |
1534 | ||
1535 | for (port = 0; port < brd->numPorts; port++) | |
1536 | moxa_poll_port(&brd->ports[port], !!ip, ip + port); | |
1537 | ||
1538 | if (ip) | |
1539 | writeb(0, brd->intPend); /* ACK */ | |
1540 | ||
1541 | if (moxaLowWaterChk) { | |
1542 | struct moxa_port *p = brd->ports; | |
1543 | for (port = 0; port < brd->numPorts; port++, p++) | |
1544 | if (p->lowChkFlag) { | |
1545 | p->lowChkFlag = 0; | |
1546 | moxa_low_water_check(p->tableAddr); | |
1da177e4 | 1547 | } |
1da177e4 LT |
1548 | } |
1549 | } | |
7bcf97d1 | 1550 | moxaLowWaterChk = 0; |
1da177e4 | 1551 | |
2a541341 JS |
1552 | if (served) |
1553 | mod_timer(&moxaTimer, jiffies + HZ / 50); | |
1554 | spin_unlock(&moxa_lock); | |
1da177e4 LT |
1555 | } |
1556 | ||
1557 | /******************************************************************************/ | |
1558 | ||
db1acaa6 | 1559 | static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios) |
1da177e4 | 1560 | { |
eaa95a8d JS |
1561 | register struct ktermios *ts = tty->termios; |
1562 | struct moxa_port *ch = tty->driver_data; | |
db1acaa6 | 1563 | int rts, cts, txflow, rxflow, xany, baud; |
1da177e4 | 1564 | |
1da177e4 LT |
1565 | rts = cts = txflow = rxflow = xany = 0; |
1566 | if (ts->c_cflag & CRTSCTS) | |
1567 | rts = cts = 1; | |
1568 | if (ts->c_iflag & IXON) | |
1569 | txflow = 1; | |
1570 | if (ts->c_iflag & IXOFF) | |
1571 | rxflow = 1; | |
1572 | if (ts->c_iflag & IXANY) | |
1573 | xany = 1; | |
db1acaa6 AC |
1574 | |
1575 | /* Clear the features we don't support */ | |
1576 | ts->c_cflag &= ~CMSPAR; | |
b4173f45 JS |
1577 | MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany); |
1578 | baud = MoxaPortSetTermio(ch, ts, tty_get_baud_rate(tty)); | |
db1acaa6 AC |
1579 | if (baud == -1) |
1580 | baud = tty_termios_baud_rate(old_termios); | |
1581 | /* Not put the baud rate into the termios data */ | |
1582 | tty_encode_baud_rate(tty, baud, baud); | |
1da177e4 LT |
1583 | } |
1584 | ||
6f56b658 | 1585 | static void moxa_setup_empty_event(struct tty_struct *tty) |
1da177e4 | 1586 | { |
8f8ecbad | 1587 | struct moxa_port *ch = tty->driver_data; |
1da177e4 | 1588 | |
7bcf97d1 | 1589 | spin_lock_bh(&moxa_lock); |
1da177e4 | 1590 | ch->statusflags |= EMPTYWAIT; |
7bcf97d1 | 1591 | spin_unlock_bh(&moxa_lock); |
1da177e4 LT |
1592 | } |
1593 | ||
d450b5a0 | 1594 | static void moxa_shut_down(struct tty_struct *tty) |
1da177e4 | 1595 | { |
d450b5a0 | 1596 | struct moxa_port *ch = tty->driver_data; |
1da177e4 | 1597 | |
9de6a51f | 1598 | if (!(ch->port.flags & ASYNC_INITIALIZED)) |
1da177e4 LT |
1599 | return; |
1600 | ||
b4173f45 | 1601 | MoxaPortDisable(ch); |
1da177e4 LT |
1602 | |
1603 | /* | |
1604 | * If we're a modem control device and HUPCL is on, drop RTS & DTR. | |
1605 | */ | |
d450b5a0 | 1606 | if (C_HUPCL(tty)) |
b4173f45 | 1607 | MoxaPortLineCtrl(ch, 0, 0); |
1da177e4 | 1608 | |
a8f5cda0 | 1609 | spin_lock_bh(&moxa_lock); |
9de6a51f | 1610 | ch->port.flags &= ~ASYNC_INITIALIZED; |
a8f5cda0 | 1611 | spin_unlock_bh(&moxa_lock); |
1da177e4 LT |
1612 | } |
1613 | ||
1da177e4 LT |
1614 | /***************************************************************************** |
1615 | * Driver level functions: * | |
1da177e4 | 1616 | *****************************************************************************/ |
1da177e4 | 1617 | |
b4173f45 | 1618 | static void MoxaPortFlushData(struct moxa_port *port, int mode) |
1da177e4 LT |
1619 | { |
1620 | void __iomem *ofsAddr; | |
eaa95a8d | 1621 | if (mode < 0 || mode > 2) |
1da177e4 | 1622 | return; |
b4173f45 | 1623 | ofsAddr = port->tableAddr; |
1da177e4 LT |
1624 | moxafunc(ofsAddr, FC_FlushQueue, mode); |
1625 | if (mode != 1) { | |
b4173f45 | 1626 | port->lowChkFlag = 0; |
6f56b658 | 1627 | moxa_low_water_check(ofsAddr); |
1da177e4 LT |
1628 | } |
1629 | } | |
1630 | ||
1da177e4 LT |
1631 | /* |
1632 | * Moxa Port Number Description: | |
1633 | * | |
1634 | * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And, | |
1635 | * the port number using in MOXA driver functions will be 0 to 31 for | |
1636 | * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96 | |
1637 | * to 127 for fourth. For example, if you setup three MOXA boards, | |
1638 | * first board is C218, second board is C320-16 and third board is | |
1639 | * C320-32. The port number of first board (C218 - 8 ports) is from | |
1640 | * 0 to 7. The port number of second board (C320 - 16 ports) is form | |
1641 | * 32 to 47. The port number of third board (C320 - 32 ports) is from | |
1642 | * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to | |
1643 | * 127 will be invalid. | |
1644 | * | |
1645 | * | |
1646 | * Moxa Functions Description: | |
1647 | * | |
1648 | * Function 1: Driver initialization routine, this routine must be | |
1649 | * called when initialized driver. | |
1650 | * Syntax: | |
1651 | * void MoxaDriverInit(); | |
1652 | * | |
1653 | * | |
1654 | * Function 2: Moxa driver private IOCTL command processing. | |
1655 | * Syntax: | |
1656 | * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port); | |
1657 | * | |
1658 | * unsigned int cmd : IOCTL command | |
1659 | * unsigned long arg : IOCTL argument | |
1660 | * int port : port number (0 - 127) | |
1661 | * | |
1662 | * return: 0 (OK) | |
1663 | * -EINVAL | |
1664 | * -ENOIOCTLCMD | |
1665 | * | |
1666 | * | |
1da177e4 LT |
1667 | * Function 6: Enable this port to start Tx/Rx data. |
1668 | * Syntax: | |
1669 | * void MoxaPortEnable(int port); | |
1670 | * int port : port number (0 - 127) | |
1671 | * | |
1672 | * | |
1673 | * Function 7: Disable this port | |
1674 | * Syntax: | |
1675 | * void MoxaPortDisable(int port); | |
1676 | * int port : port number (0 - 127) | |
1677 | * | |
1678 | * | |
1da177e4 LT |
1679 | * Function 10: Setting baud rate of this port. |
1680 | * Syntax: | |
08d01c79 | 1681 | * speed_t MoxaPortSetBaud(int port, speed_t baud); |
1da177e4 LT |
1682 | * int port : port number (0 - 127) |
1683 | * long baud : baud rate (50 - 115200) | |
1684 | * | |
1685 | * return: 0 : this port is invalid or baud < 50 | |
1686 | * 50 - 115200 : the real baud rate set to the port, if | |
1687 | * the argument baud is large than maximun | |
1688 | * available baud rate, the real setting | |
1689 | * baud rate will be the maximun baud rate. | |
1690 | * | |
1691 | * | |
1da177e4 LT |
1692 | * Function 12: Configure the port. |
1693 | * Syntax: | |
606d099c | 1694 | * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud); |
1da177e4 | 1695 | * int port : port number (0 - 127) |
606d099c | 1696 | * struct ktermios * termio : termio structure pointer |
c7bce309 | 1697 | * speed_t baud : baud rate |
1da177e4 LT |
1698 | * |
1699 | * return: -1 : this port is invalid or termio == NULL | |
1700 | * 0 : setting O.K. | |
1701 | * | |
1702 | * | |
1703 | * Function 13: Get the DTR/RTS state of this port. | |
1704 | * Syntax: | |
1705 | * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState); | |
1706 | * int port : port number (0 - 127) | |
1707 | * int * dtrState : pointer to INT to receive the current DTR | |
1708 | * state. (if NULL, this function will not | |
1709 | * write to this address) | |
1710 | * int * rtsState : pointer to INT to receive the current RTS | |
1711 | * state. (if NULL, this function will not | |
1712 | * write to this address) | |
1713 | * | |
1714 | * return: -1 : this port is invalid | |
1715 | * 0 : O.K. | |
1716 | * | |
1717 | * | |
1718 | * Function 14: Setting the DTR/RTS output state of this port. | |
1719 | * Syntax: | |
1720 | * void MoxaPortLineCtrl(int port, int dtrState, int rtsState); | |
1721 | * int port : port number (0 - 127) | |
1722 | * int dtrState : DTR output state (0: off, 1: on) | |
1723 | * int rtsState : RTS output state (0: off, 1: on) | |
1724 | * | |
1725 | * | |
1726 | * Function 15: Setting the flow control of this port. | |
1727 | * Syntax: | |
1728 | * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow, | |
1729 | * int txFlow,int xany); | |
1730 | * int port : port number (0 - 127) | |
1731 | * int rtsFlow : H/W RTS flow control (0: no, 1: yes) | |
1732 | * int ctsFlow : H/W CTS flow control (0: no, 1: yes) | |
1733 | * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes) | |
1734 | * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes) | |
1735 | * int xany : S/W XANY flow control (0: no, 1: yes) | |
1736 | * | |
1737 | * | |
1738 | * Function 16: Get ths line status of this port | |
1739 | * Syntax: | |
1740 | * int MoxaPortLineStatus(int port); | |
1741 | * int port : port number (0 - 127) | |
1742 | * | |
1743 | * return: Bit 0 - CTS state (0: off, 1: on) | |
1744 | * Bit 1 - DSR state (0: off, 1: on) | |
1745 | * Bit 2 - DCD state (0: off, 1: on) | |
1746 | * | |
1747 | * | |
1da177e4 LT |
1748 | * Function 19: Flush the Rx/Tx buffer data of this port. |
1749 | * Syntax: | |
1750 | * void MoxaPortFlushData(int port, int mode); | |
1751 | * int port : port number (0 - 127) | |
1752 | * int mode | |
1753 | * 0 : flush the Rx buffer | |
1754 | * 1 : flush the Tx buffer | |
1755 | * 2 : flush the Rx and Tx buffer | |
1756 | * | |
1757 | * | |
1758 | * Function 20: Write data. | |
1759 | * Syntax: | |
1760 | * int MoxaPortWriteData(int port, unsigned char * buffer, int length); | |
1761 | * int port : port number (0 - 127) | |
1762 | * unsigned char * buffer : pointer to write data buffer. | |
1763 | * int length : write data length | |
1764 | * | |
1765 | * return: 0 - length : real write data length | |
1766 | * | |
1767 | * | |
1768 | * Function 21: Read data. | |
1769 | * Syntax: | |
33f0f88f | 1770 | * int MoxaPortReadData(int port, struct tty_struct *tty); |
1da177e4 | 1771 | * int port : port number (0 - 127) |
33f0f88f | 1772 | * struct tty_struct *tty : tty for data |
1da177e4 LT |
1773 | * |
1774 | * return: 0 - length : real read data length | |
1775 | * | |
1776 | * | |
1da177e4 LT |
1777 | * Function 24: Get the Tx buffer current queued data bytes |
1778 | * Syntax: | |
1779 | * int MoxaPortTxQueue(int port); | |
1780 | * int port : port number (0 - 127) | |
1781 | * | |
1782 | * return: .. : Tx buffer current queued data bytes | |
1783 | * | |
1784 | * | |
1785 | * Function 25: Get the Tx buffer current free space | |
1786 | * Syntax: | |
1787 | * int MoxaPortTxFree(int port); | |
1788 | * int port : port number (0 - 127) | |
1789 | * | |
1790 | * return: .. : Tx buffer current free space | |
1791 | * | |
1792 | * | |
1793 | * Function 26: Get the Rx buffer current queued data bytes | |
1794 | * Syntax: | |
1795 | * int MoxaPortRxQueue(int port); | |
1796 | * int port : port number (0 - 127) | |
1797 | * | |
1798 | * return: .. : Rx buffer current queued data bytes | |
1799 | * | |
1800 | * | |
1da177e4 LT |
1801 | * Function 28: Disable port data transmission. |
1802 | * Syntax: | |
1803 | * void MoxaPortTxDisable(int port); | |
1804 | * int port : port number (0 - 127) | |
1805 | * | |
1806 | * | |
1807 | * Function 29: Enable port data transmission. | |
1808 | * Syntax: | |
1809 | * void MoxaPortTxEnable(int port); | |
1810 | * int port : port number (0 - 127) | |
1811 | * | |
1812 | * | |
1da177e4 LT |
1813 | * Function 31: Get the received BREAK signal count and reset it. |
1814 | * Syntax: | |
1815 | * int MoxaPortResetBrkCnt(int port); | |
1816 | * int port : port number (0 - 127) | |
1817 | * | |
1818 | * return: 0 - .. : BREAK signal count | |
1819 | * | |
1820 | * | |
1da177e4 | 1821 | */ |
1da177e4 | 1822 | |
b4173f45 | 1823 | static void MoxaPortEnable(struct moxa_port *port) |
1da177e4 LT |
1824 | { |
1825 | void __iomem *ofsAddr; | |
eaa95a8d | 1826 | u16 lowwater = 512; |
1da177e4 | 1827 | |
b4173f45 | 1828 | ofsAddr = port->tableAddr; |
1da177e4 | 1829 | writew(lowwater, ofsAddr + Low_water); |
08d01c79 | 1830 | if (MOXA_IS_320(port->board)) |
1da177e4 | 1831 | moxafunc(ofsAddr, FC_SetBreakIrq, 0); |
eaa95a8d JS |
1832 | else |
1833 | writew(readw(ofsAddr + HostStat) | WakeupBreak, | |
1834 | ofsAddr + HostStat); | |
1da177e4 LT |
1835 | |
1836 | moxafunc(ofsAddr, FC_SetLineIrq, Magic_code); | |
1837 | moxafunc(ofsAddr, FC_FlushQueue, 2); | |
1838 | ||
1839 | moxafunc(ofsAddr, FC_EnableCH, Magic_code); | |
1840 | MoxaPortLineStatus(port); | |
1841 | } | |
1842 | ||
b4173f45 | 1843 | static void MoxaPortDisable(struct moxa_port *port) |
1da177e4 | 1844 | { |
b4173f45 | 1845 | void __iomem *ofsAddr = port->tableAddr; |
1da177e4 LT |
1846 | |
1847 | moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */ | |
1848 | moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code); | |
1849 | writew(0, ofsAddr + HostStat); | |
1850 | moxafunc(ofsAddr, FC_DisableCH, Magic_code); | |
1851 | } | |
1852 | ||
08d01c79 | 1853 | static speed_t MoxaPortSetBaud(struct moxa_port *port, speed_t baud) |
1da177e4 | 1854 | { |
08d01c79 JS |
1855 | void __iomem *ofsAddr = port->tableAddr; |
1856 | unsigned int clock, val; | |
1857 | speed_t max; | |
1da177e4 | 1858 | |
08d01c79 JS |
1859 | max = MOXA_IS_320(port->board) ? 460800 : 921600; |
1860 | if (baud < 50) | |
eaa95a8d | 1861 | return 0; |
1da177e4 LT |
1862 | if (baud > max) |
1863 | baud = max; | |
08d01c79 | 1864 | clock = 921600; |
1da177e4 LT |
1865 | val = clock / baud; |
1866 | moxafunc(ofsAddr, FC_SetBaud, val); | |
1867 | baud = clock / val; | |
eaa95a8d | 1868 | return baud; |
1da177e4 LT |
1869 | } |
1870 | ||
b4173f45 JS |
1871 | static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio, |
1872 | speed_t baud) | |
1da177e4 LT |
1873 | { |
1874 | void __iomem *ofsAddr; | |
1875 | tcflag_t cflag; | |
1da177e4 LT |
1876 | tcflag_t mode = 0; |
1877 | ||
b4173f45 | 1878 | ofsAddr = port->tableAddr; |
1da177e4 LT |
1879 | cflag = termio->c_cflag; /* termio->c_cflag */ |
1880 | ||
1881 | mode = termio->c_cflag & CSIZE; | |
1882 | if (mode == CS5) | |
1883 | mode = MX_CS5; | |
1884 | else if (mode == CS6) | |
1885 | mode = MX_CS6; | |
1886 | else if (mode == CS7) | |
1887 | mode = MX_CS7; | |
1888 | else if (mode == CS8) | |
1889 | mode = MX_CS8; | |
1890 | ||
1891 | if (termio->c_cflag & CSTOPB) { | |
1892 | if (mode == MX_CS5) | |
1893 | mode |= MX_STOP15; | |
1894 | else | |
1895 | mode |= MX_STOP2; | |
1896 | } else | |
1897 | mode |= MX_STOP1; | |
1898 | ||
1899 | if (termio->c_cflag & PARENB) { | |
1900 | if (termio->c_cflag & PARODD) | |
1901 | mode |= MX_PARODD; | |
1902 | else | |
1903 | mode |= MX_PAREVEN; | |
1904 | } else | |
1905 | mode |= MX_PARNONE; | |
1906 | ||
eaa95a8d | 1907 | moxafunc(ofsAddr, FC_SetDataMode, (u16)mode); |
1da177e4 | 1908 | |
08d01c79 JS |
1909 | if (MOXA_IS_320(port->board) && baud >= 921600) |
1910 | return -1; | |
1911 | ||
db1acaa6 | 1912 | baud = MoxaPortSetBaud(port, baud); |
1da177e4 LT |
1913 | |
1914 | if (termio->c_iflag & (IXON | IXOFF | IXANY)) { | |
1915 | writeb(termio->c_cc[VSTART], ofsAddr + FuncArg); | |
1916 | writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1); | |
1917 | writeb(FC_SetXonXoff, ofsAddr + FuncCode); | |
6f56b658 | 1918 | moxa_wait_finish(ofsAddr); |
1da177e4 LT |
1919 | |
1920 | } | |
eaa95a8d | 1921 | return baud; |
1da177e4 LT |
1922 | } |
1923 | ||
b4173f45 JS |
1924 | static int MoxaPortGetLineOut(struct moxa_port *port, int *dtrState, |
1925 | int *rtsState) | |
1da177e4 | 1926 | { |
b4173f45 JS |
1927 | if (dtrState) |
1928 | *dtrState = !!(port->lineCtrl & DTR_ON); | |
1929 | if (rtsState) | |
1930 | *rtsState = !!(port->lineCtrl & RTS_ON); | |
1931 | ||
eaa95a8d | 1932 | return 0; |
1da177e4 LT |
1933 | } |
1934 | ||
b4173f45 | 1935 | static void MoxaPortLineCtrl(struct moxa_port *port, int dtr, int rts) |
1da177e4 | 1936 | { |
eaa95a8d | 1937 | u8 mode = 0; |
1da177e4 | 1938 | |
1da177e4 LT |
1939 | if (dtr) |
1940 | mode |= DTR_ON; | |
1941 | if (rts) | |
1942 | mode |= RTS_ON; | |
b4173f45 JS |
1943 | port->lineCtrl = mode; |
1944 | moxafunc(port->tableAddr, FC_LineControl, mode); | |
1da177e4 LT |
1945 | } |
1946 | ||
b4173f45 JS |
1947 | static void MoxaPortFlowCtrl(struct moxa_port *port, int rts, int cts, |
1948 | int txflow, int rxflow, int txany) | |
1da177e4 | 1949 | { |
b4173f45 | 1950 | int mode = 0; |
1da177e4 | 1951 | |
1da177e4 LT |
1952 | if (rts) |
1953 | mode |= RTS_FlowCtl; | |
1954 | if (cts) | |
1955 | mode |= CTS_FlowCtl; | |
1956 | if (txflow) | |
1957 | mode |= Tx_FlowCtl; | |
1958 | if (rxflow) | |
1959 | mode |= Rx_FlowCtl; | |
1960 | if (txany) | |
1961 | mode |= IXM_IXANY; | |
b4173f45 | 1962 | moxafunc(port->tableAddr, FC_SetFlowCtl, mode); |
1da177e4 LT |
1963 | } |
1964 | ||
b4173f45 | 1965 | static int MoxaPortLineStatus(struct moxa_port *port) |
1da177e4 LT |
1966 | { |
1967 | void __iomem *ofsAddr; | |
1968 | int val; | |
1969 | ||
b4173f45 | 1970 | ofsAddr = port->tableAddr; |
08d01c79 | 1971 | if (MOXA_IS_320(port->board)) { |
1da177e4 LT |
1972 | moxafunc(ofsAddr, FC_LineStatus, 0); |
1973 | val = readw(ofsAddr + FuncArg); | |
1974 | } else { | |
1975 | val = readw(ofsAddr + FlagStat) >> 4; | |
1976 | } | |
1977 | val &= 0x0B; | |
7bcf97d1 | 1978 | if (val & 8) |
1da177e4 | 1979 | val |= 4; |
a8f5cda0 | 1980 | spin_lock_bh(&moxa_lock); |
7bcf97d1 | 1981 | moxa_new_dcdstate(port, val & 8); |
a8f5cda0 | 1982 | spin_unlock_bh(&moxa_lock); |
1da177e4 | 1983 | val &= 7; |
7bcf97d1 | 1984 | return val; |
1da177e4 LT |
1985 | } |
1986 | ||
d450b5a0 | 1987 | static int MoxaPortWriteData(struct tty_struct *tty, |
2108eba5 | 1988 | const unsigned char *buffer, int len) |
1da177e4 | 1989 | { |
d450b5a0 | 1990 | struct moxa_port *port = tty->driver_data; |
1da177e4 | 1991 | void __iomem *baseAddr, *ofsAddr, *ofs; |
2108eba5 JS |
1992 | unsigned int c, total; |
1993 | u16 head, tail, tx_mask, spage, epage; | |
1994 | u16 pageno, pageofs, bufhead; | |
1da177e4 | 1995 | |
b4173f45 JS |
1996 | ofsAddr = port->tableAddr; |
1997 | baseAddr = port->board->basemem; | |
1da177e4 LT |
1998 | tx_mask = readw(ofsAddr + TX_mask); |
1999 | spage = readw(ofsAddr + Page_txb); | |
2000 | epage = readw(ofsAddr + EndPage_txb); | |
2001 | tail = readw(ofsAddr + TXwptr); | |
2002 | head = readw(ofsAddr + TXrptr); | |
2108eba5 | 2003 | c = (head > tail) ? (head - tail - 1) : (head - tail + tx_mask); |
1da177e4 LT |
2004 | if (c > len) |
2005 | c = len; | |
9de6a51f | 2006 | moxaLog.txcnt[port->port.tty->index] += c; |
1da177e4 LT |
2007 | total = c; |
2008 | if (spage == epage) { | |
2009 | bufhead = readw(ofsAddr + Ofs_txb); | |
2010 | writew(spage, baseAddr + Control_reg); | |
2011 | while (c > 0) { | |
2012 | if (head > tail) | |
2013 | len = head - tail - 1; | |
2014 | else | |
2015 | len = tx_mask + 1 - tail; | |
2016 | len = (c > len) ? len : c; | |
2017 | ofs = baseAddr + DynPage_addr + bufhead + tail; | |
2108eba5 JS |
2018 | memcpy_toio(ofs, buffer, len); |
2019 | buffer += len; | |
1da177e4 LT |
2020 | tail = (tail + len) & tx_mask; |
2021 | c -= len; | |
2022 | } | |
1da177e4 | 2023 | } else { |
1da177e4 LT |
2024 | pageno = spage + (tail >> 13); |
2025 | pageofs = tail & Page_mask; | |
2108eba5 JS |
2026 | while (c > 0) { |
2027 | len = Page_size - pageofs; | |
2028 | if (len > c) | |
2029 | len = c; | |
1da177e4 LT |
2030 | writeb(pageno, baseAddr + Control_reg); |
2031 | ofs = baseAddr + DynPage_addr + pageofs; | |
2108eba5 JS |
2032 | memcpy_toio(ofs, buffer, len); |
2033 | buffer += len; | |
1da177e4 LT |
2034 | if (++pageno == epage) |
2035 | pageno = spage; | |
2036 | pageofs = 0; | |
2108eba5 JS |
2037 | c -= len; |
2038 | } | |
2039 | tail = (tail + total) & tx_mask; | |
1da177e4 | 2040 | } |
2108eba5 | 2041 | writew(tail, ofsAddr + TXwptr); |
1da177e4 | 2042 | writeb(1, ofsAddr + CD180TXirq); /* start to send */ |
2108eba5 | 2043 | return total; |
1da177e4 LT |
2044 | } |
2045 | ||
7bcf97d1 | 2046 | static int MoxaPortReadData(struct moxa_port *port) |
1da177e4 | 2047 | { |
9de6a51f | 2048 | struct tty_struct *tty = port->port.tty; |
2108eba5 | 2049 | unsigned char *dst; |
1da177e4 | 2050 | void __iomem *baseAddr, *ofsAddr, *ofs; |
2108eba5 JS |
2051 | unsigned int count, len, total; |
2052 | u16 tail, rx_mask, spage, epage; | |
2053 | u16 pageno, pageofs, bufhead, head; | |
1da177e4 | 2054 | |
b4173f45 JS |
2055 | ofsAddr = port->tableAddr; |
2056 | baseAddr = port->board->basemem; | |
1da177e4 LT |
2057 | head = readw(ofsAddr + RXrptr); |
2058 | tail = readw(ofsAddr + RXwptr); | |
2059 | rx_mask = readw(ofsAddr + RX_mask); | |
2060 | spage = readw(ofsAddr + Page_rxb); | |
2061 | epage = readw(ofsAddr + EndPage_rxb); | |
2108eba5 | 2062 | count = (tail >= head) ? (tail - head) : (tail - head + rx_mask + 1); |
1da177e4 | 2063 | if (count == 0) |
33f0f88f | 2064 | return 0; |
1da177e4 | 2065 | |
33f0f88f | 2066 | total = count; |
7bcf97d1 | 2067 | moxaLog.rxcnt[tty->index] += total; |
1da177e4 LT |
2068 | if (spage == epage) { |
2069 | bufhead = readw(ofsAddr + Ofs_rxb); | |
2070 | writew(spage, baseAddr + Control_reg); | |
2071 | while (count > 0) { | |
1da177e4 | 2072 | ofs = baseAddr + DynPage_addr + bufhead + head; |
2108eba5 JS |
2073 | len = (tail >= head) ? (tail - head) : |
2074 | (rx_mask + 1 - head); | |
2075 | len = tty_prepare_flip_string(tty, &dst, | |
2076 | min(len, count)); | |
2077 | memcpy_fromio(dst, ofs, len); | |
1da177e4 LT |
2078 | head = (head + len) & rx_mask; |
2079 | count -= len; | |
2080 | } | |
1da177e4 | 2081 | } else { |
1da177e4 LT |
2082 | pageno = spage + (head >> 13); |
2083 | pageofs = head & Page_mask; | |
2108eba5 | 2084 | while (count > 0) { |
1da177e4 LT |
2085 | writew(pageno, baseAddr + Control_reg); |
2086 | ofs = baseAddr + DynPage_addr + pageofs; | |
2108eba5 JS |
2087 | len = tty_prepare_flip_string(tty, &dst, |
2088 | min(Page_size - pageofs, count)); | |
2089 | memcpy_fromio(dst, ofs, len); | |
2090 | ||
2091 | count -= len; | |
2092 | pageofs = (pageofs + len) & Page_mask; | |
2093 | if (pageofs == 0 && ++pageno == epage) | |
1da177e4 | 2094 | pageno = spage; |
2108eba5 JS |
2095 | } |
2096 | head = (head + total) & rx_mask; | |
1da177e4 | 2097 | } |
2108eba5 JS |
2098 | writew(head, ofsAddr + RXrptr); |
2099 | if (readb(ofsAddr + FlagStat) & Xoff_state) { | |
1da177e4 | 2100 | moxaLowWaterChk = 1; |
b4173f45 | 2101 | port->lowChkFlag = 1; |
1da177e4 | 2102 | } |
2108eba5 | 2103 | return total; |
1da177e4 LT |
2104 | } |
2105 | ||
2106 | ||
b4173f45 | 2107 | static int MoxaPortTxQueue(struct moxa_port *port) |
1da177e4 | 2108 | { |
b4173f45 | 2109 | void __iomem *ofsAddr = port->tableAddr; |
2108eba5 | 2110 | u16 rptr, wptr, mask; |
1da177e4 | 2111 | |
1da177e4 LT |
2112 | rptr = readw(ofsAddr + TXrptr); |
2113 | wptr = readw(ofsAddr + TXwptr); | |
2114 | mask = readw(ofsAddr + TX_mask); | |
2108eba5 | 2115 | return (wptr - rptr) & mask; |
1da177e4 LT |
2116 | } |
2117 | ||
b4173f45 | 2118 | static int MoxaPortTxFree(struct moxa_port *port) |
1da177e4 | 2119 | { |
b4173f45 | 2120 | void __iomem *ofsAddr = port->tableAddr; |
2108eba5 | 2121 | u16 rptr, wptr, mask; |
1da177e4 | 2122 | |
1da177e4 LT |
2123 | rptr = readw(ofsAddr + TXrptr); |
2124 | wptr = readw(ofsAddr + TXwptr); | |
2125 | mask = readw(ofsAddr + TX_mask); | |
2108eba5 | 2126 | return mask - ((wptr - rptr) & mask); |
1da177e4 LT |
2127 | } |
2128 | ||
b4173f45 | 2129 | static int MoxaPortRxQueue(struct moxa_port *port) |
1da177e4 | 2130 | { |
b4173f45 | 2131 | void __iomem *ofsAddr = port->tableAddr; |
2108eba5 | 2132 | u16 rptr, wptr, mask; |
1da177e4 | 2133 | |
1da177e4 LT |
2134 | rptr = readw(ofsAddr + RXrptr); |
2135 | wptr = readw(ofsAddr + RXwptr); | |
2136 | mask = readw(ofsAddr + RX_mask); | |
2108eba5 | 2137 | return (wptr - rptr) & mask; |
1da177e4 LT |
2138 | } |
2139 | ||
b4173f45 | 2140 | static void MoxaPortTxDisable(struct moxa_port *port) |
1da177e4 | 2141 | { |
b4173f45 | 2142 | moxafunc(port->tableAddr, FC_SetXoffState, Magic_code); |
1da177e4 LT |
2143 | } |
2144 | ||
b4173f45 | 2145 | static void MoxaPortTxEnable(struct moxa_port *port) |
1da177e4 | 2146 | { |
b4173f45 | 2147 | moxafunc(port->tableAddr, FC_SetXonState, Magic_code); |
1da177e4 LT |
2148 | } |
2149 | ||
8f8ecbad | 2150 | static int moxa_get_serial_info(struct moxa_port *info, |
eaa95a8d | 2151 | struct serial_struct __user *retinfo) |
1da177e4 | 2152 | { |
eaa95a8d JS |
2153 | struct serial_struct tmp = { |
2154 | .type = info->type, | |
9de6a51f AC |
2155 | .line = info->port.tty->index, |
2156 | .flags = info->port.flags, | |
eaa95a8d | 2157 | .baud_base = 921600, |
44b7d1b3 | 2158 | .close_delay = info->port.close_delay |
eaa95a8d JS |
2159 | }; |
2160 | return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0; | |
1da177e4 LT |
2161 | } |
2162 | ||
2163 | ||
8f8ecbad | 2164 | static int moxa_set_serial_info(struct moxa_port *info, |
eaa95a8d | 2165 | struct serial_struct __user *new_info) |
1da177e4 LT |
2166 | { |
2167 | struct serial_struct new_serial; | |
2168 | ||
eaa95a8d | 2169 | if (copy_from_user(&new_serial, new_info, sizeof(new_serial))) |
1da177e4 LT |
2170 | return -EFAULT; |
2171 | ||
eaa95a8d JS |
2172 | if (new_serial.irq != 0 || new_serial.port != 0 || |
2173 | new_serial.custom_divisor != 0 || | |
2174 | new_serial.baud_base != 921600) | |
2175 | return -EPERM; | |
1da177e4 LT |
2176 | |
2177 | if (!capable(CAP_SYS_ADMIN)) { | |
2178 | if (((new_serial.flags & ~ASYNC_USR_MASK) != | |
9de6a51f | 2179 | (info->port.flags & ~ASYNC_USR_MASK))) |
eaa95a8d JS |
2180 | return -EPERM; |
2181 | } else | |
44b7d1b3 | 2182 | info->port.close_delay = new_serial.close_delay * HZ / 100; |
1da177e4 LT |
2183 | |
2184 | new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS); | |
9de6a51f | 2185 | new_serial.flags |= (info->port.flags & ASYNC_FLAGS); |
1da177e4 | 2186 | |
eaa95a8d | 2187 | MoxaSetFifo(info, new_serial.type == PORT_16550A); |
1da177e4 LT |
2188 | |
2189 | info->type = new_serial.type; | |
eaa95a8d | 2190 | return 0; |
1da177e4 LT |
2191 | } |
2192 | ||
2193 | ||
2194 | ||
2195 | /***************************************************************************** | |
2196 | * Static local functions: * | |
2197 | *****************************************************************************/ | |
1da177e4 | 2198 | |
b4173f45 | 2199 | static void MoxaSetFifo(struct moxa_port *port, int enable) |
1da177e4 | 2200 | { |
b4173f45 | 2201 | void __iomem *ofsAddr = port->tableAddr; |
1da177e4 LT |
2202 | |
2203 | if (!enable) { | |
2204 | moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0); | |
2205 | moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1); | |
2206 | } else { | |
2207 | moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3); | |
2208 | moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16); | |
2209 | } | |
2210 | } |