aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMathieu OTHACEHE <m.othacehe@gmail.com>2015-12-28 15:21:25 -0500
committerJohan Hovold <johan@kernel.org>2015-12-29 03:28:23 -0500
commit0b2b093ad405b56a9e6f4f20a25da77ebfa9549c (patch)
tree0cc2baff3b2f9128cbb6b1c5fd419d070a8a502c
parent365a0442f6def11b0b3012778d6a838c7a8e6912 (diff)
USB: serial: add Moxa UPORT 11x0 driver
Add a driver which supports : - UPort 1110 : 1 port RS-232 USB to Serial Hub. - UPort 1130 : 1 port RS-422/485 USB to Serial Hub. - UPort 1130I : 1 port RS-422/485 USB to Serial Hub with Isolation. - UPort 1150 : 1 port RS-232/422/485 USB to Serial Hub. - UPort 1150I : 1 port RS-232/422/485 USB to Serial Hub with Isolation. This driver is based on GPL MOXA driver written by Hen Huang and available on MOXA website. The original driver was based on io_ti serial driver. Signed-off-by: Mathieu OTHACEHE <m.othacehe@gmail.com> Signed-off-by: Johan Hovold <johan@kernel.org>
-rw-r--r--drivers/usb/serial/Kconfig16
-rw-r--r--drivers/usb/serial/Makefile1
-rw-r--r--drivers/usb/serial/mxu11x0.c995
3 files changed, 1012 insertions, 0 deletions
diff --git a/drivers/usb/serial/Kconfig b/drivers/usb/serial/Kconfig
index 56ecb8b5115d..f612dda9c977 100644
--- a/drivers/usb/serial/Kconfig
+++ b/drivers/usb/serial/Kconfig
@@ -475,6 +475,22 @@ config USB_SERIAL_MOS7840
475 To compile this driver as a module, choose M here: the 475 To compile this driver as a module, choose M here: the
476 module will be called mos7840. If unsure, choose N. 476 module will be called mos7840. If unsure, choose N.
477 477
478config USB_SERIAL_MXUPORT11
479 tristate "USB Moxa UPORT 11x0 Serial Driver"
480 ---help---
481 Say Y here if you want to use a MOXA UPort 11x0 Serial hub.
482
483 This driver supports:
484
485 - UPort 1110 : 1 port RS-232 USB to Serial Hub.
486 - UPort 1130 : 1 port RS-422/485 USB to Serial Hub.
487 - UPort 1130I : 1 port RS-422/485 USB to Serial Hub with Isolation.
488 - UPort 1150 : 1 port RS-232/422/485 USB to Serial Hub.
489 - UPort 1150I : 1 port RS-232/422/485 USB to Serial Hub with Isolation.
490
491 To compile this driver as a module, choose M here: the
492 module will be called mxu11x0.
493
478config USB_SERIAL_MXUPORT 494config USB_SERIAL_MXUPORT
479 tristate "USB Moxa UPORT Serial Driver" 495 tristate "USB Moxa UPORT Serial Driver"
480 ---help--- 496 ---help---
diff --git a/drivers/usb/serial/Makefile b/drivers/usb/serial/Makefile
index 349d9df0895f..f3fa5e53702d 100644
--- a/drivers/usb/serial/Makefile
+++ b/drivers/usb/serial/Makefile
@@ -38,6 +38,7 @@ obj-$(CONFIG_USB_SERIAL_METRO) += metro-usb.o
38obj-$(CONFIG_USB_SERIAL_MOS7720) += mos7720.o 38obj-$(CONFIG_USB_SERIAL_MOS7720) += mos7720.o
39obj-$(CONFIG_USB_SERIAL_MOS7840) += mos7840.o 39obj-$(CONFIG_USB_SERIAL_MOS7840) += mos7840.o
40obj-$(CONFIG_USB_SERIAL_MXUPORT) += mxuport.o 40obj-$(CONFIG_USB_SERIAL_MXUPORT) += mxuport.o
41obj-$(CONFIG_USB_SERIAL_MXUPORT11) += mxu11x0.o
41obj-$(CONFIG_USB_SERIAL_NAVMAN) += navman.o 42obj-$(CONFIG_USB_SERIAL_NAVMAN) += navman.o
42obj-$(CONFIG_USB_SERIAL_OMNINET) += omninet.o 43obj-$(CONFIG_USB_SERIAL_OMNINET) += omninet.o
43obj-$(CONFIG_USB_SERIAL_OPTICON) += opticon.o 44obj-$(CONFIG_USB_SERIAL_OPTICON) += opticon.o
diff --git a/drivers/usb/serial/mxu11x0.c b/drivers/usb/serial/mxu11x0.c
new file mode 100644
index 000000000000..8884ca276e67
--- /dev/null
+++ b/drivers/usb/serial/mxu11x0.c
@@ -0,0 +1,995 @@
1/*
2 *
3 * USB Moxa UPORT 11x0 Serial Driver
4 *
5 * Copyright (C) 2007 MOXA Technologies Co., Ltd.
6 * Copyright (C) 2015 Mathieu Othacehe <m.othacehe@gmail.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 *
14 * Supports the following Moxa USB to serial converters:
15 * UPort 1110, 1 port RS-232 USB to Serial Hub.
16 * UPort 1130, 1 port RS-422/485 USB to Serial Hub.
17 * UPort 1130I, 1 port RS-422/485 USB to Serial Hub with isolation
18 * protection.
19 * UPort 1150, 1 port RS-232/422/485 USB to Serial Hub.
20 * UPort 1150I, 1 port RS-232/422/485 USB to Serial Hub with isolation
21 * protection.
22 */
23
24#include <linux/kernel.h>
25#include <linux/module.h>
26#include <linux/firmware.h>
27#include <linux/jiffies.h>
28#include <linux/serial.h>
29#include <linux/serial_reg.h>
30#include <linux/slab.h>
31#include <linux/spinlock.h>
32#include <linux/mutex.h>
33#include <linux/tty.h>
34#include <linux/tty_driver.h>
35#include <linux/tty_flip.h>
36#include <linux/uaccess.h>
37#include <linux/usb.h>
38#include <linux/usb/serial.h>
39
40/* Vendor and product ids */
41#define MXU1_VENDOR_ID 0x110a
42#define MXU1_1110_PRODUCT_ID 0x1110
43#define MXU1_1130_PRODUCT_ID 0x1130
44#define MXU1_1150_PRODUCT_ID 0x1150
45#define MXU1_1151_PRODUCT_ID 0x1151
46#define MXU1_1131_PRODUCT_ID 0x1131
47
48/* Commands */
49#define MXU1_GET_VERSION 0x01
50#define MXU1_GET_PORT_STATUS 0x02
51#define MXU1_GET_PORT_DEV_INFO 0x03
52#define MXU1_GET_CONFIG 0x04
53#define MXU1_SET_CONFIG 0x05
54#define MXU1_OPEN_PORT 0x06
55#define MXU1_CLOSE_PORT 0x07
56#define MXU1_START_PORT 0x08
57#define MXU1_STOP_PORT 0x09
58#define MXU1_TEST_PORT 0x0A
59#define MXU1_PURGE_PORT 0x0B
60#define MXU1_RESET_EXT_DEVICE 0x0C
61#define MXU1_GET_OUTQUEUE 0x0D
62#define MXU1_WRITE_DATA 0x80
63#define MXU1_READ_DATA 0x81
64#define MXU1_REQ_TYPE_CLASS 0x82
65
66/* Module identifiers */
67#define MXU1_I2C_PORT 0x01
68#define MXU1_IEEE1284_PORT 0x02
69#define MXU1_UART1_PORT 0x03
70#define MXU1_UART2_PORT 0x04
71#define MXU1_RAM_PORT 0x05
72
73/* Modem status */
74#define MXU1_MSR_DELTA_CTS 0x01
75#define MXU1_MSR_DELTA_DSR 0x02
76#define MXU1_MSR_DELTA_RI 0x04
77#define MXU1_MSR_DELTA_CD 0x08
78#define MXU1_MSR_CTS 0x10
79#define MXU1_MSR_DSR 0x20
80#define MXU1_MSR_RI 0x40
81#define MXU1_MSR_CD 0x80
82#define MXU1_MSR_DELTA_MASK 0x0F
83#define MXU1_MSR_MASK 0xF0
84
85/* Line status */
86#define MXU1_LSR_OVERRUN_ERROR 0x01
87#define MXU1_LSR_PARITY_ERROR 0x02
88#define MXU1_LSR_FRAMING_ERROR 0x04
89#define MXU1_LSR_BREAK 0x08
90#define MXU1_LSR_ERROR 0x0F
91#define MXU1_LSR_RX_FULL 0x10
92#define MXU1_LSR_TX_EMPTY 0x20
93
94/* Modem control */
95#define MXU1_MCR_LOOP 0x04
96#define MXU1_MCR_DTR 0x10
97#define MXU1_MCR_RTS 0x20
98
99/* Mask settings */
100#define MXU1_UART_ENABLE_RTS_IN 0x0001
101#define MXU1_UART_DISABLE_RTS 0x0002
102#define MXU1_UART_ENABLE_PARITY_CHECKING 0x0008
103#define MXU1_UART_ENABLE_DSR_OUT 0x0010
104#define MXU1_UART_ENABLE_CTS_OUT 0x0020
105#define MXU1_UART_ENABLE_X_OUT 0x0040
106#define MXU1_UART_ENABLE_XA_OUT 0x0080
107#define MXU1_UART_ENABLE_X_IN 0x0100
108#define MXU1_UART_ENABLE_DTR_IN 0x0800
109#define MXU1_UART_DISABLE_DTR 0x1000
110#define MXU1_UART_ENABLE_MS_INTS 0x2000
111#define MXU1_UART_ENABLE_AUTO_START_DMA 0x4000
112#define MXU1_UART_SEND_BREAK_SIGNAL 0x8000
113
114/* Parity */
115#define MXU1_UART_NO_PARITY 0x00
116#define MXU1_UART_ODD_PARITY 0x01
117#define MXU1_UART_EVEN_PARITY 0x02
118#define MXU1_UART_MARK_PARITY 0x03
119#define MXU1_UART_SPACE_PARITY 0x04
120
121/* Stop bits */
122#define MXU1_UART_1_STOP_BITS 0x00
123#define MXU1_UART_1_5_STOP_BITS 0x01
124#define MXU1_UART_2_STOP_BITS 0x02
125
126/* Bits per character */
127#define MXU1_UART_5_DATA_BITS 0x00
128#define MXU1_UART_6_DATA_BITS 0x01
129#define MXU1_UART_7_DATA_BITS 0x02
130#define MXU1_UART_8_DATA_BITS 0x03
131
132/* Operation modes */
133#define MXU1_UART_232 0x00
134#define MXU1_UART_485_RECEIVER_DISABLED 0x01
135#define MXU1_UART_485_RECEIVER_ENABLED 0x02
136
137/* Pipe transfer mode and timeout */
138#define MXU1_PIPE_MODE_CONTINUOUS 0x01
139#define MXU1_PIPE_MODE_MASK 0x03
140#define MXU1_PIPE_TIMEOUT_MASK 0x7C
141#define MXU1_PIPE_TIMEOUT_ENABLE 0x80
142
143/* Config struct */
144struct mxu1_uart_config {
145 __be16 wBaudRate;
146 __be16 wFlags;
147 u8 bDataBits;
148 u8 bParity;
149 u8 bStopBits;
150 char cXon;
151 char cXoff;
152 u8 bUartMode;
153} __packed;
154
155/* Purge modes */
156#define MXU1_PURGE_OUTPUT 0x00
157#define MXU1_PURGE_INPUT 0x80
158
159/* Read/Write data */
160#define MXU1_RW_DATA_ADDR_SFR 0x10
161#define MXU1_RW_DATA_ADDR_IDATA 0x20
162#define MXU1_RW_DATA_ADDR_XDATA 0x30
163#define MXU1_RW_DATA_ADDR_CODE 0x40
164#define MXU1_RW_DATA_ADDR_GPIO 0x50
165#define MXU1_RW_DATA_ADDR_I2C 0x60
166#define MXU1_RW_DATA_ADDR_FLASH 0x70
167#define MXU1_RW_DATA_ADDR_DSP 0x80
168
169#define MXU1_RW_DATA_UNSPECIFIED 0x00
170#define MXU1_RW_DATA_BYTE 0x01
171#define MXU1_RW_DATA_WORD 0x02
172#define MXU1_RW_DATA_DOUBLE_WORD 0x04
173
174struct mxu1_write_data_bytes {
175 u8 bAddrType;
176 u8 bDataType;
177 u8 bDataCounter;
178 __be16 wBaseAddrHi;
179 __be16 wBaseAddrLo;
180 u8 bData[0];
181} __packed;
182
183/* Interrupt codes */
184#define MXU1_CODE_HARDWARE_ERROR 0xFF
185#define MXU1_CODE_DATA_ERROR 0x03
186#define MXU1_CODE_MODEM_STATUS 0x04
187
188static inline int mxu1_get_func_from_code(unsigned char code)
189{
190 return code & 0x0f;
191}
192
193/* Download firmware max packet size */
194#define MXU1_DOWNLOAD_MAX_PACKET_SIZE 64
195
196/* Firmware image header */
197struct mxu1_firmware_header {
198 __le16 wLength;
199 u8 bCheckSum;
200} __packed;
201
202#define MXU1_UART_BASE_ADDR 0xFFA0
203#define MXU1_UART_OFFSET_MCR 0x0004
204
205#define MXU1_BAUD_BASE 923077
206
207#define MXU1_TRANSFER_TIMEOUT 2
208#define MXU1_DOWNLOAD_TIMEOUT 1000
209#define MXU1_DEFAULT_CLOSING_WAIT 4000 /* in .01 secs */
210
211struct mxu1_port {
212 u8 msr;
213 u8 mcr;
214 u8 uart_mode;
215 spinlock_t spinlock; /* Protects msr */
216 struct mutex mutex; /* Protects mcr */
217 bool send_break;
218};
219
220struct mxu1_device {
221 u16 mxd_model;
222};
223
224static const struct usb_device_id mxu1_idtable[] = {
225 { USB_DEVICE(MXU1_VENDOR_ID, MXU1_1110_PRODUCT_ID) },
226 { USB_DEVICE(MXU1_VENDOR_ID, MXU1_1130_PRODUCT_ID) },
227 { USB_DEVICE(MXU1_VENDOR_ID, MXU1_1150_PRODUCT_ID) },
228 { USB_DEVICE(MXU1_VENDOR_ID, MXU1_1151_PRODUCT_ID) },
229 { USB_DEVICE(MXU1_VENDOR_ID, MXU1_1131_PRODUCT_ID) },
230 { }
231};
232
233MODULE_DEVICE_TABLE(usb, mxu1_idtable);
234
235/* Write the given buffer out to the control pipe. */
236static int mxu1_send_ctrl_data_urb(struct usb_serial *serial,
237 u8 request,
238 u16 value, u16 index,
239 void *data, size_t size)
240{
241 int status;
242
243 status = usb_control_msg(serial->dev,
244 usb_sndctrlpipe(serial->dev, 0),
245 request,
246 (USB_DIR_OUT | USB_TYPE_VENDOR |
247 USB_RECIP_DEVICE), value, index,
248 data, size,
249 USB_CTRL_SET_TIMEOUT);
250 if (status < 0) {
251 dev_err(&serial->interface->dev,
252 "%s - usb_control_msg failed: %d\n",
253 __func__, status);
254 return status;
255 }
256
257 if (status != size) {
258 dev_err(&serial->interface->dev,
259 "%s - short write (%d / %zd)\n",
260 __func__, status, size);
261 return -EIO;
262 }
263
264 return 0;
265}
266
267/* Send a vendor request without any data */
268static int mxu1_send_ctrl_urb(struct usb_serial *serial,
269 u8 request, u16 value, u16 index)
270{
271 return mxu1_send_ctrl_data_urb(serial, request, value, index,
272 NULL, 0);
273}
274
275static int mxu1_download_firmware(struct usb_serial *serial,
276 const struct firmware *fw_p)
277{
278 int status = 0;
279 int buffer_size;
280 int pos;
281 int len;
282 int done;
283 u8 cs = 0;
284 u8 *buffer;
285 struct usb_device *dev = serial->dev;
286 struct mxu1_firmware_header *header;
287 unsigned int pipe;
288
289 pipe = usb_sndbulkpipe(dev, serial->port[0]->bulk_out_endpointAddress);
290
291 buffer_size = fw_p->size + sizeof(*header);
292 buffer = kmalloc(buffer_size, GFP_KERNEL);
293 if (!buffer)
294 return -ENOMEM;
295
296 memcpy(buffer, fw_p->data, fw_p->size);
297 memset(buffer + fw_p->size, 0xff, buffer_size - fw_p->size);
298
299 for (pos = sizeof(*header); pos < buffer_size; pos++)
300 cs = (u8)(cs + buffer[pos]);
301
302 header = (struct mxu1_firmware_header *)buffer;
303 header->wLength = cpu_to_le16(buffer_size - sizeof(*header));
304 header->bCheckSum = cs;
305
306 dev_dbg(&dev->dev, "%s - downloading firmware\n", __func__);
307
308 for (pos = 0; pos < buffer_size; pos += done) {
309 len = min(buffer_size - pos, MXU1_DOWNLOAD_MAX_PACKET_SIZE);
310
311 status = usb_bulk_msg(dev, pipe, buffer + pos, len, &done,
312 MXU1_DOWNLOAD_TIMEOUT);
313 if (status)
314 break;
315 }
316
317 kfree(buffer);
318
319 if (status) {
320 dev_err(&dev->dev, "failed to download firmware: %d\n", status);
321 return status;
322 }
323
324 msleep_interruptible(100);
325 usb_reset_device(dev);
326
327 dev_dbg(&dev->dev, "%s - download successful\n", __func__);
328
329 return 0;
330}
331
332static int mxu1_port_probe(struct usb_serial_port *port)
333{
334 struct mxu1_port *mxport;
335 struct mxu1_device *mxdev;
336 struct urb *urb;
337
338 mxport = kzalloc(sizeof(struct mxu1_port), GFP_KERNEL);
339 if (!mxport)
340 return -ENOMEM;
341
342 spin_lock_init(&mxport->spinlock);
343 mutex_init(&mxport->mutex);
344
345 mxdev = usb_get_serial_data(port->serial);
346
347 urb = port->interrupt_in_urb;
348 if (!urb) {
349 dev_err(&port->dev, "%s - no interrupt urb\n", __func__);
350 return -EINVAL;
351 }
352
353 switch (mxdev->mxd_model) {
354 case MXU1_1110_PRODUCT_ID:
355 case MXU1_1150_PRODUCT_ID:
356 case MXU1_1151_PRODUCT_ID:
357 mxport->uart_mode = MXU1_UART_232;
358 break;
359 case MXU1_1130_PRODUCT_ID:
360 case MXU1_1131_PRODUCT_ID:
361 mxport->uart_mode = MXU1_UART_485_RECEIVER_DISABLED;
362 break;
363 }
364
365 usb_set_serial_port_data(port, mxport);
366
367 port->port.closing_wait =
368 msecs_to_jiffies(MXU1_DEFAULT_CLOSING_WAIT * 10);
369 port->port.drain_delay = 1;
370
371 return 0;
372}
373
374static int mxu1_startup(struct usb_serial *serial)
375{
376 struct mxu1_device *mxdev;
377 struct usb_device *dev = serial->dev;
378 struct usb_host_interface *cur_altsetting;
379 char fw_name[32];
380 const struct firmware *fw_p = NULL;
381 int err;
382 int status = 0;
383
384 dev_dbg(&serial->interface->dev, "%s - product 0x%04X, num configurations %d, configuration value %d\n",
385 __func__, le16_to_cpu(dev->descriptor.idProduct),
386 dev->descriptor.bNumConfigurations,
387 dev->actconfig->desc.bConfigurationValue);
388
389 /* create device structure */
390 mxdev = kzalloc(sizeof(struct mxu1_device), GFP_KERNEL);
391 if (!mxdev)
392 return -ENOMEM;
393
394 usb_set_serial_data(serial, mxdev);
395
396 mxdev->mxd_model = le16_to_cpu(dev->descriptor.idProduct);
397
398 cur_altsetting = serial->interface->cur_altsetting;
399
400 /* if we have only 1 configuration, download firmware */
401 if (cur_altsetting->desc.bNumEndpoints == 1) {
402
403 snprintf(fw_name,
404 sizeof(fw_name),
405 "moxa/moxa-%04x.fw",
406 mxdev->mxd_model);
407
408 err = request_firmware(&fw_p, fw_name, &serial->interface->dev);
409 if (err) {
410 dev_err(&serial->interface->dev, "failed to request firmware: %d\n",
411 err);
412 kfree(mxdev);
413 return err;
414 }
415
416 err = mxu1_download_firmware(serial, fw_p);
417 if (err) {
418 release_firmware(fw_p);
419 kfree(mxdev);
420 return err;
421 }
422
423 status = -ENODEV;
424 release_firmware(fw_p);
425 }
426
427 return status;
428}
429
430static int mxu1_write_byte(struct usb_serial_port *port, u32 addr,
431 u8 mask, u8 byte)
432{
433 int status;
434 size_t size;
435 struct mxu1_write_data_bytes *data;
436
437 dev_dbg(&port->dev, "%s - addr 0x%08X, mask 0x%02X, byte 0x%02X\n",
438 __func__, addr, mask, byte);
439
440 size = sizeof(struct mxu1_write_data_bytes) + 2;
441 data = kzalloc(size, GFP_KERNEL);
442 if (!data)
443 return -ENOMEM;
444
445 data->bAddrType = MXU1_RW_DATA_ADDR_XDATA;
446 data->bDataType = MXU1_RW_DATA_BYTE;
447 data->bDataCounter = 1;
448 data->wBaseAddrHi = cpu_to_be16(addr >> 16);
449 data->wBaseAddrLo = cpu_to_be16(addr);
450 data->bData[0] = mask;
451 data->bData[1] = byte;
452
453 status = mxu1_send_ctrl_data_urb(port->serial, MXU1_WRITE_DATA, 0,
454 MXU1_RAM_PORT, data, size);
455 if (status < 0)
456 dev_err(&port->dev, "%s - failed: %d\n", __func__, status);
457
458 kfree(data);
459
460 return status;
461}
462
463static int mxu1_set_mcr(struct usb_serial_port *port, unsigned int mcr)
464{
465 int status;
466
467 status = mxu1_write_byte(port,
468 MXU1_UART_BASE_ADDR + MXU1_UART_OFFSET_MCR,
469 MXU1_MCR_RTS | MXU1_MCR_DTR | MXU1_MCR_LOOP,
470 mcr);
471 return status;
472}
473
474static void mxu1_set_termios(struct tty_struct *tty,
475 struct usb_serial_port *port,
476 struct ktermios *old_termios)
477{
478 struct mxu1_port *mxport = usb_get_serial_port_data(port);
479 struct mxu1_uart_config *config;
480 tcflag_t cflag, iflag;
481 speed_t baud;
482 int status;
483 unsigned int mcr;
484
485 cflag = tty->termios.c_cflag;
486 iflag = tty->termios.c_iflag;
487
488 if (old_termios &&
489 !tty_termios_hw_change(&tty->termios, old_termios) &&
490 tty->termios.c_iflag == old_termios->c_iflag) {
491 dev_dbg(&port->dev, "%s - nothing to change\n", __func__);
492 return;
493 }
494
495 dev_dbg(&port->dev,
496 "%s - clfag %08x, iflag %08x\n", __func__, cflag, iflag);
497
498 if (old_termios) {
499 dev_dbg(&port->dev, "%s - old clfag %08x, old iflag %08x\n",
500 __func__,
501 old_termios->c_cflag,
502 old_termios->c_iflag);
503 }
504
505 config = kzalloc(sizeof(*config), GFP_KERNEL);
506 if (!config)
507 return;
508
509 /* these flags must be set */
510 config->wFlags |= MXU1_UART_ENABLE_MS_INTS;
511 config->wFlags |= MXU1_UART_ENABLE_AUTO_START_DMA;
512 if (mxport->send_break)
513 config->wFlags |= MXU1_UART_SEND_BREAK_SIGNAL;
514 config->bUartMode = mxport->uart_mode;
515
516 switch (C_CSIZE(tty)) {
517 case CS5:
518 config->bDataBits = MXU1_UART_5_DATA_BITS;
519 break;
520 case CS6:
521 config->bDataBits = MXU1_UART_6_DATA_BITS;
522 break;
523 case CS7:
524 config->bDataBits = MXU1_UART_7_DATA_BITS;
525 break;
526 default:
527 case CS8:
528 config->bDataBits = MXU1_UART_8_DATA_BITS;
529 break;
530 }
531
532 if (C_PARENB(tty)) {
533 config->wFlags |= MXU1_UART_ENABLE_PARITY_CHECKING;
534 if (C_CMSPAR(tty)) {
535 if (C_PARODD(tty))
536 config->bParity = MXU1_UART_MARK_PARITY;
537 else
538 config->bParity = MXU1_UART_SPACE_PARITY;
539 } else {
540 if (C_PARODD(tty))
541 config->bParity = MXU1_UART_ODD_PARITY;
542 else
543 config->bParity = MXU1_UART_EVEN_PARITY;
544 }
545 } else {
546 config->bParity = MXU1_UART_NO_PARITY;
547 }
548
549 if (C_CSTOPB(tty))
550 config->bStopBits = MXU1_UART_2_STOP_BITS;
551 else
552 config->bStopBits = MXU1_UART_1_STOP_BITS;
553
554 if (C_CRTSCTS(tty)) {
555 /* RTS flow control must be off to drop RTS for baud rate B0 */
556 if (C_BAUD(tty) != B0)
557 config->wFlags |= MXU1_UART_ENABLE_RTS_IN;
558 config->wFlags |= MXU1_UART_ENABLE_CTS_OUT;
559 }
560
561 if (I_IXOFF(tty) || I_IXON(tty)) {
562 config->cXon = START_CHAR(tty);
563 config->cXoff = STOP_CHAR(tty);
564
565 if (I_IXOFF(tty))
566 config->wFlags |= MXU1_UART_ENABLE_X_IN;
567
568 if (I_IXON(tty))
569 config->wFlags |= MXU1_UART_ENABLE_X_OUT;
570 }
571
572 baud = tty_get_baud_rate(tty);
573 if (!baud)
574 baud = 9600;
575 config->wBaudRate = MXU1_BAUD_BASE / baud;
576
577 dev_dbg(&port->dev, "%s - BaudRate=%d, wBaudRate=%d, wFlags=0x%04X, bDataBits=%d, bParity=%d, bStopBits=%d, cXon=%d, cXoff=%d, bUartMode=%d\n",
578 __func__, baud, config->wBaudRate, config->wFlags,
579 config->bDataBits, config->bParity, config->bStopBits,
580 config->cXon, config->cXoff, config->bUartMode);
581
582 cpu_to_be16s(&config->wBaudRate);
583 cpu_to_be16s(&config->wFlags);
584
585 status = mxu1_send_ctrl_data_urb(port->serial, MXU1_SET_CONFIG, 0,
586 MXU1_UART1_PORT, config,
587 sizeof(*config));
588 if (status)
589 dev_err(&port->dev, "cannot set config: %d\n", status);
590
591 mutex_lock(&mxport->mutex);
592 mcr = mxport->mcr;
593
594 if (C_BAUD(tty) == B0)
595 mcr &= ~(MXU1_MCR_DTR | MXU1_MCR_RTS);
596 else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
597 mcr |= ~(MXU1_MCR_DTR | MXU1_MCR_RTS);
598
599 status = mxu1_set_mcr(port, mcr);
600 if (status)
601 dev_err(&port->dev, "cannot set modem control: %d\n", status);
602 else
603 mxport->mcr = mcr;
604
605 mutex_unlock(&mxport->mutex);
606
607 kfree(config);
608}
609
610static int mxu1_get_serial_info(struct usb_serial_port *port,
611 struct serial_struct __user *ret_arg)
612{
613 struct serial_struct ret_serial;
614 unsigned cwait;
615
616 if (!ret_arg)
617 return -EFAULT;
618
619 cwait = port->port.closing_wait;
620 if (cwait != ASYNC_CLOSING_WAIT_NONE)
621 cwait = jiffies_to_msecs(cwait) / 10;
622
623 memset(&ret_serial, 0, sizeof(ret_serial));
624
625 ret_serial.type = PORT_16550A;
626 ret_serial.line = port->minor;
627 ret_serial.port = 0;
628 ret_serial.xmit_fifo_size = port->bulk_out_size;
629 ret_serial.baud_base = MXU1_BAUD_BASE;
630 ret_serial.close_delay = 5*HZ;
631 ret_serial.closing_wait = cwait;
632
633 if (copy_to_user(ret_arg, &ret_serial, sizeof(*ret_arg)))
634 return -EFAULT;
635
636 return 0;
637}
638
639
640static int mxu1_set_serial_info(struct usb_serial_port *port,
641 struct serial_struct __user *new_arg)
642{
643 struct serial_struct new_serial;
644 unsigned cwait;
645
646 if (copy_from_user(&new_serial, new_arg, sizeof(new_serial)))
647 return -EFAULT;
648
649 cwait = new_serial.closing_wait;
650 if (cwait != ASYNC_CLOSING_WAIT_NONE)
651 cwait = msecs_to_jiffies(10 * new_serial.closing_wait);
652
653 port->port.closing_wait = cwait;
654
655 return 0;
656}
657
658static int mxu1_ioctl(struct tty_struct *tty,
659 unsigned int cmd, unsigned long arg)
660{
661 struct usb_serial_port *port = tty->driver_data;
662
663 switch (cmd) {
664 case TIOCGSERIAL:
665 return mxu1_get_serial_info(port,
666 (struct serial_struct __user *)arg);
667 case TIOCSSERIAL:
668 return mxu1_set_serial_info(port,
669 (struct serial_struct __user *)arg);
670 }
671
672 return -ENOIOCTLCMD;
673}
674
675static int mxu1_tiocmget(struct tty_struct *tty)
676{
677 struct usb_serial_port *port = tty->driver_data;
678 struct mxu1_port *mxport = usb_get_serial_port_data(port);
679 unsigned int result;
680 unsigned int msr;
681 unsigned int mcr;
682 unsigned long flags;
683
684 mutex_lock(&mxport->mutex);
685 spin_lock_irqsave(&mxport->spinlock, flags);
686
687 msr = mxport->msr;
688 mcr = mxport->mcr;
689
690 spin_unlock_irqrestore(&mxport->spinlock, flags);
691 mutex_unlock(&mxport->mutex);
692
693 result = ((mcr & MXU1_MCR_DTR) ? TIOCM_DTR : 0) |
694 ((mcr & MXU1_MCR_RTS) ? TIOCM_RTS : 0) |
695 ((mcr & MXU1_MCR_LOOP) ? TIOCM_LOOP : 0) |
696 ((msr & MXU1_MSR_CTS) ? TIOCM_CTS : 0) |
697 ((msr & MXU1_MSR_CD) ? TIOCM_CAR : 0) |
698 ((msr & MXU1_MSR_RI) ? TIOCM_RI : 0) |
699 ((msr & MXU1_MSR_DSR) ? TIOCM_DSR : 0);
700
701 dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result);
702
703 return result;
704}
705
706static int mxu1_tiocmset(struct tty_struct *tty,
707 unsigned int set, unsigned int clear)
708{
709 struct usb_serial_port *port = tty->driver_data;
710 struct mxu1_port *mxport = usb_get_serial_port_data(port);
711 int err;
712 unsigned int mcr;
713
714 mutex_lock(&mxport->mutex);
715 mcr = mxport->mcr;
716
717 if (set & TIOCM_RTS)
718 mcr |= MXU1_MCR_RTS;
719 if (set & TIOCM_DTR)
720 mcr |= MXU1_MCR_DTR;
721 if (set & TIOCM_LOOP)
722 mcr |= MXU1_MCR_LOOP;
723
724 if (clear & TIOCM_RTS)
725 mcr &= ~MXU1_MCR_RTS;
726 if (clear & TIOCM_DTR)
727 mcr &= ~MXU1_MCR_DTR;
728 if (clear & TIOCM_LOOP)
729 mcr &= ~MXU1_MCR_LOOP;
730
731 err = mxu1_set_mcr(port, mcr);
732 if (!err)
733 mxport->mcr = mcr;
734
735 mutex_unlock(&mxport->mutex);
736
737 return err;
738}
739
740static void mxu1_break(struct tty_struct *tty, int break_state)
741{
742 struct usb_serial_port *port = tty->driver_data;
743 struct mxu1_port *mxport = usb_get_serial_port_data(port);
744
745 if (break_state == -1)
746 mxport->send_break = true;
747 else
748 mxport->send_break = false;
749
750 mxu1_set_termios(tty, port, NULL);
751}
752
753static int mxu1_open(struct tty_struct *tty, struct usb_serial_port *port)
754{
755 struct mxu1_port *mxport = usb_get_serial_port_data(port);
756 struct usb_serial *serial = port->serial;
757 int status;
758 u16 open_settings;
759
760 open_settings = (MXU1_PIPE_MODE_CONTINUOUS |
761 MXU1_PIPE_TIMEOUT_ENABLE |
762 (MXU1_TRANSFER_TIMEOUT << 2));
763
764 mxport->msr = 0;
765
766 dev_dbg(&port->dev, "%s - start interrupt in urb\n", __func__);
767 status = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
768 if (status) {
769 dev_err(&port->dev, "failed to submit interrupt urb: %d\n",
770 status);
771 return status;
772 }
773
774 if (tty)
775 mxu1_set_termios(tty, port, NULL);
776
777 status = mxu1_send_ctrl_urb(serial, MXU1_OPEN_PORT,
778 open_settings, MXU1_UART1_PORT);
779 if (status) {
780 dev_err(&port->dev, "%s - cannot send open command: %d\n",
781 __func__, status);
782 goto unlink_int_urb;
783 }
784
785 status = mxu1_send_ctrl_urb(serial, MXU1_START_PORT,
786 0, MXU1_UART1_PORT);
787 if (status) {
788 dev_err(&port->dev, "%s - cannot send start command: %d\n",
789 __func__, status);
790 goto unlink_int_urb;
791 }
792
793 status = mxu1_send_ctrl_urb(serial, MXU1_PURGE_PORT,
794 MXU1_PURGE_INPUT, MXU1_UART1_PORT);
795 if (status) {
796 dev_err(&port->dev, "%s - cannot clear input buffers: %d\n",
797 __func__, status);
798
799 goto unlink_int_urb;
800 }
801
802 status = mxu1_send_ctrl_urb(serial, MXU1_PURGE_PORT,
803 MXU1_PURGE_OUTPUT, MXU1_UART1_PORT);
804 if (status) {
805 dev_err(&port->dev, "%s - cannot clear output buffers: %d\n",
806 __func__, status);
807
808 goto unlink_int_urb;
809 }
810
811 /*
812 * reset the data toggle on the bulk endpoints to work around bug in
813 * host controllers where things get out of sync some times
814 */
815 usb_clear_halt(serial->dev, port->write_urb->pipe);
816 usb_clear_halt(serial->dev, port->read_urb->pipe);
817
818 if (tty)
819 mxu1_set_termios(tty, port, NULL);
820
821 status = mxu1_send_ctrl_urb(serial, MXU1_OPEN_PORT,
822 open_settings, MXU1_UART1_PORT);
823 if (status) {
824 dev_err(&port->dev, "%s - cannot send open command: %d\n",
825 __func__, status);
826 goto unlink_int_urb;
827 }
828
829 status = mxu1_send_ctrl_urb(serial, MXU1_START_PORT,
830 0, MXU1_UART1_PORT);
831 if (status) {
832 dev_err(&port->dev, "%s - cannot send start command: %d\n",
833 __func__, status);
834 goto unlink_int_urb;
835 }
836
837 status = usb_serial_generic_open(tty, port);
838 if (status) {
839 dev_err(&port->dev, "%s - submit read urb failed: %d\n",
840 __func__, status);
841 goto unlink_int_urb;
842 }
843
844 return status;
845
846unlink_int_urb:
847 usb_kill_urb(port->interrupt_in_urb);
848
849 return status;
850}
851
852static void mxu1_close(struct usb_serial_port *port)
853{
854 int status;
855
856 usb_serial_generic_close(port);
857 usb_kill_urb(port->interrupt_in_urb);
858
859 status = mxu1_send_ctrl_urb(port->serial, MXU1_CLOSE_PORT,
860 0, MXU1_UART1_PORT);
861 if (status)
862 dev_err(&port->dev, "failed to send close port command: %d\n",
863 status);
864}
865
866static void mxu1_handle_new_msr(struct usb_serial_port *port, u8 msr)
867{
868 struct async_icount *icount;
869 struct mxu1_port *mxport;
870 unsigned long flags;
871
872 dev_dbg(&port->dev, "%s - msr 0x%02X\n", __func__, msr);
873
874 mxport = usb_get_serial_port_data(port);
875
876 spin_lock_irqsave(&mxport->spinlock, flags);
877 mxport->msr = msr & MXU1_MSR_MASK;
878 spin_unlock_irqrestore(&mxport->spinlock, flags);
879
880 if (msr & MXU1_MSR_DELTA_MASK) {
881 icount = &port->icount;
882 if (msr & MXU1_MSR_DELTA_CTS)
883 icount->cts++;
884 if (msr & MXU1_MSR_DELTA_DSR)
885 icount->dsr++;
886 if (msr & MXU1_MSR_DELTA_CD)
887 icount->dcd++;
888 if (msr & MXU1_MSR_DELTA_RI)
889 icount->rng++;
890
891 wake_up_interruptible(&port->port.delta_msr_wait);
892 }
893}
894
895static void mxu1_interrupt_callback(struct urb *urb)
896{
897 struct usb_serial_port *port = urb->context;
898 unsigned char *data = urb->transfer_buffer;
899 int length = urb->actual_length;
900 int function;
901 int status;
902 u8 msr;
903
904 switch (urb->status) {
905 case 0:
906 break;
907 case -ECONNRESET:
908 case -ENOENT:
909 case -ESHUTDOWN:
910 dev_dbg(&port->dev, "%s - urb shutting down: %d\n",
911 __func__, urb->status);
912 return;
913 default:
914 dev_dbg(&port->dev, "%s - nonzero urb status: %d\n",
915 __func__, urb->status);
916 goto exit;
917 }
918
919 if (length != 2) {
920 dev_dbg(&port->dev, "%s - bad packet size: %d\n",
921 __func__, length);
922 goto exit;
923 }
924
925 if (data[0] == MXU1_CODE_HARDWARE_ERROR) {
926 dev_err(&port->dev, "%s - hardware error: %d\n",
927 __func__, data[1]);
928 goto exit;
929 }
930
931 function = mxu1_get_func_from_code(data[0]);
932
933 dev_dbg(&port->dev, "%s - function %d, data 0x%02X\n",
934 __func__, function, data[1]);
935
936 switch (function) {
937 case MXU1_CODE_DATA_ERROR:
938 dev_dbg(&port->dev, "%s - DATA ERROR, data 0x%02X\n",
939 __func__, data[1]);
940 break;
941
942 case MXU1_CODE_MODEM_STATUS:
943 msr = data[1];
944 mxu1_handle_new_msr(port, msr);
945 break;
946
947 default:
948 dev_err(&port->dev, "%s - unknown interrupt code: 0x%02X\n",
949 __func__, data[1]);
950 break;
951 }
952
953exit:
954 status = usb_submit_urb(urb, GFP_ATOMIC);
955 if (status)
956 dev_err(&port->dev, "resubmit interrupt urb failed: %d\n",
957 status);
958}
959
960static struct usb_serial_driver mxuport11_device = {
961 .driver = {
962 .owner = THIS_MODULE,
963 .name = "mxuport11",
964 },
965 .description = "MOXA UPort 11x0",
966 .id_table = mxu1_idtable,
967 .num_ports = 1,
968 .port_probe = mxu1_port_probe,
969 .attach = mxu1_startup,
970 .open = mxu1_open,
971 .close = mxu1_close,
972 .ioctl = mxu1_ioctl,
973 .set_termios = mxu1_set_termios,
974 .tiocmget = mxu1_tiocmget,
975 .tiocmset = mxu1_tiocmset,
976 .tiocmiwait = usb_serial_generic_tiocmiwait,
977 .get_icount = usb_serial_generic_get_icount,
978 .break_ctl = mxu1_break,
979 .read_int_callback = mxu1_interrupt_callback,
980};
981
982static struct usb_serial_driver *const serial_drivers[] = {
983 &mxuport11_device, NULL
984};
985
986module_usb_serial_driver(serial_drivers, mxu1_idtable);
987
988MODULE_AUTHOR("Mathieu Othacehe <m.othacehe@gmail.com>");
989MODULE_DESCRIPTION("MOXA UPort 11x0 USB to Serial Hub Driver");
990MODULE_LICENSE("GPL");
991MODULE_FIRMWARE("moxa/moxa-1110.fw");
992MODULE_FIRMWARE("moxa/moxa-1130.fw");
993MODULE_FIRMWARE("moxa/moxa-1131.fw");
994MODULE_FIRMWARE("moxa/moxa-1150.fw");
995MODULE_FIRMWARE("moxa/moxa-1151.fw");