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