blob: e3c3f57c2d828d42ecb35e12d8a1a048974d9ee6 [file] [log] [blame]
Mathieu OTHACEHE0b2b0932015-12-28 21:21:25 +01001/*
Mathieu OTHACEHE0b2b0932015-12-28 21:21:25 +01002 * USB Moxa UPORT 11x0 Serial Driver
3 *
4 * Copyright (C) 2007 MOXA Technologies Co., Ltd.
5 * Copyright (C) 2015 Mathieu Othacehe <m.othacehe@gmail.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 *
13 * Supports the following Moxa USB to serial converters:
14 * UPort 1110, 1 port RS-232 USB to Serial Hub.
15 * UPort 1130, 1 port RS-422/485 USB to Serial Hub.
16 * UPort 1130I, 1 port RS-422/485 USB to Serial Hub with isolation
17 * protection.
18 * UPort 1150, 1 port RS-232/422/485 USB to Serial Hub.
19 * UPort 1150I, 1 port RS-232/422/485 USB to Serial Hub with isolation
20 * protection.
21 */
22
23#include <linux/kernel.h>
24#include <linux/module.h>
25#include <linux/firmware.h>
26#include <linux/jiffies.h>
27#include <linux/serial.h>
28#include <linux/serial_reg.h>
29#include <linux/slab.h>
30#include <linux/spinlock.h>
31#include <linux/mutex.h>
32#include <linux/tty.h>
33#include <linux/tty_driver.h>
34#include <linux/tty_flip.h>
35#include <linux/uaccess.h>
36#include <linux/usb.h>
37#include <linux/usb/serial.h>
38
39/* Vendor and product ids */
40#define MXU1_VENDOR_ID 0x110a
41#define MXU1_1110_PRODUCT_ID 0x1110
42#define MXU1_1130_PRODUCT_ID 0x1130
43#define MXU1_1150_PRODUCT_ID 0x1150
44#define MXU1_1151_PRODUCT_ID 0x1151
45#define MXU1_1131_PRODUCT_ID 0x1131
46
47/* Commands */
48#define MXU1_GET_VERSION 0x01
49#define MXU1_GET_PORT_STATUS 0x02
50#define MXU1_GET_PORT_DEV_INFO 0x03
51#define MXU1_GET_CONFIG 0x04
52#define MXU1_SET_CONFIG 0x05
53#define MXU1_OPEN_PORT 0x06
54#define MXU1_CLOSE_PORT 0x07
55#define MXU1_START_PORT 0x08
56#define MXU1_STOP_PORT 0x09
57#define MXU1_TEST_PORT 0x0A
58#define MXU1_PURGE_PORT 0x0B
59#define MXU1_RESET_EXT_DEVICE 0x0C
60#define MXU1_GET_OUTQUEUE 0x0D
61#define MXU1_WRITE_DATA 0x80
62#define MXU1_READ_DATA 0x81
63#define MXU1_REQ_TYPE_CLASS 0x82
64
65/* Module identifiers */
66#define MXU1_I2C_PORT 0x01
67#define MXU1_IEEE1284_PORT 0x02
68#define MXU1_UART1_PORT 0x03
69#define MXU1_UART2_PORT 0x04
70#define MXU1_RAM_PORT 0x05
71
72/* Modem status */
73#define MXU1_MSR_DELTA_CTS 0x01
74#define MXU1_MSR_DELTA_DSR 0x02
75#define MXU1_MSR_DELTA_RI 0x04
76#define MXU1_MSR_DELTA_CD 0x08
77#define MXU1_MSR_CTS 0x10
78#define MXU1_MSR_DSR 0x20
79#define MXU1_MSR_RI 0x40
80#define MXU1_MSR_CD 0x80
81#define MXU1_MSR_DELTA_MASK 0x0F
82#define MXU1_MSR_MASK 0xF0
83
84/* Line status */
85#define MXU1_LSR_OVERRUN_ERROR 0x01
86#define MXU1_LSR_PARITY_ERROR 0x02
87#define MXU1_LSR_FRAMING_ERROR 0x04
88#define MXU1_LSR_BREAK 0x08
89#define MXU1_LSR_ERROR 0x0F
90#define MXU1_LSR_RX_FULL 0x10
91#define MXU1_LSR_TX_EMPTY 0x20
92
93/* Modem control */
94#define MXU1_MCR_LOOP 0x04
95#define MXU1_MCR_DTR 0x10
96#define MXU1_MCR_RTS 0x20
97
98/* Mask settings */
99#define MXU1_UART_ENABLE_RTS_IN 0x0001
100#define MXU1_UART_DISABLE_RTS 0x0002
101#define MXU1_UART_ENABLE_PARITY_CHECKING 0x0008
102#define MXU1_UART_ENABLE_DSR_OUT 0x0010
103#define MXU1_UART_ENABLE_CTS_OUT 0x0020
104#define MXU1_UART_ENABLE_X_OUT 0x0040
105#define MXU1_UART_ENABLE_XA_OUT 0x0080
106#define MXU1_UART_ENABLE_X_IN 0x0100
107#define MXU1_UART_ENABLE_DTR_IN 0x0800
108#define MXU1_UART_DISABLE_DTR 0x1000
109#define MXU1_UART_ENABLE_MS_INTS 0x2000
110#define MXU1_UART_ENABLE_AUTO_START_DMA 0x4000
111#define MXU1_UART_SEND_BREAK_SIGNAL 0x8000
112
113/* Parity */
114#define MXU1_UART_NO_PARITY 0x00
115#define MXU1_UART_ODD_PARITY 0x01
116#define MXU1_UART_EVEN_PARITY 0x02
117#define MXU1_UART_MARK_PARITY 0x03
118#define MXU1_UART_SPACE_PARITY 0x04
119
120/* Stop bits */
121#define MXU1_UART_1_STOP_BITS 0x00
122#define MXU1_UART_1_5_STOP_BITS 0x01
123#define MXU1_UART_2_STOP_BITS 0x02
124
125/* Bits per character */
126#define MXU1_UART_5_DATA_BITS 0x00
127#define MXU1_UART_6_DATA_BITS 0x01
128#define MXU1_UART_7_DATA_BITS 0x02
129#define MXU1_UART_8_DATA_BITS 0x03
130
131/* Operation modes */
132#define MXU1_UART_232 0x00
133#define MXU1_UART_485_RECEIVER_DISABLED 0x01
134#define MXU1_UART_485_RECEIVER_ENABLED 0x02
135
136/* Pipe transfer mode and timeout */
137#define MXU1_PIPE_MODE_CONTINUOUS 0x01
138#define MXU1_PIPE_MODE_MASK 0x03
139#define MXU1_PIPE_TIMEOUT_MASK 0x7C
140#define MXU1_PIPE_TIMEOUT_ENABLE 0x80
141
142/* Config struct */
143struct mxu1_uart_config {
144 __be16 wBaudRate;
145 __be16 wFlags;
146 u8 bDataBits;
147 u8 bParity;
148 u8 bStopBits;
149 char cXon;
150 char cXoff;
151 u8 bUartMode;
152} __packed;
153
154/* Purge modes */
155#define MXU1_PURGE_OUTPUT 0x00
156#define MXU1_PURGE_INPUT 0x80
157
158/* Read/Write data */
159#define MXU1_RW_DATA_ADDR_SFR 0x10
160#define MXU1_RW_DATA_ADDR_IDATA 0x20
161#define MXU1_RW_DATA_ADDR_XDATA 0x30
162#define MXU1_RW_DATA_ADDR_CODE 0x40
163#define MXU1_RW_DATA_ADDR_GPIO 0x50
164#define MXU1_RW_DATA_ADDR_I2C 0x60
165#define MXU1_RW_DATA_ADDR_FLASH 0x70
166#define MXU1_RW_DATA_ADDR_DSP 0x80
167
168#define MXU1_RW_DATA_UNSPECIFIED 0x00
169#define MXU1_RW_DATA_BYTE 0x01
170#define MXU1_RW_DATA_WORD 0x02
171#define MXU1_RW_DATA_DOUBLE_WORD 0x04
172
173struct mxu1_write_data_bytes {
174 u8 bAddrType;
175 u8 bDataType;
176 u8 bDataCounter;
177 __be16 wBaseAddrHi;
178 __be16 wBaseAddrLo;
179 u8 bData[0];
180} __packed;
181
182/* Interrupt codes */
183#define MXU1_CODE_HARDWARE_ERROR 0xFF
184#define MXU1_CODE_DATA_ERROR 0x03
185#define MXU1_CODE_MODEM_STATUS 0x04
186
187static inline int mxu1_get_func_from_code(unsigned char code)
188{
189 return code & 0x0f;
190}
191
192/* Download firmware max packet size */
193#define MXU1_DOWNLOAD_MAX_PACKET_SIZE 64
194
195/* Firmware image header */
196struct mxu1_firmware_header {
197 __le16 wLength;
198 u8 bCheckSum;
199} __packed;
200
201#define MXU1_UART_BASE_ADDR 0xFFA0
202#define MXU1_UART_OFFSET_MCR 0x0004
203
204#define MXU1_BAUD_BASE 923077
205
206#define MXU1_TRANSFER_TIMEOUT 2
207#define MXU1_DOWNLOAD_TIMEOUT 1000
208#define MXU1_DEFAULT_CLOSING_WAIT 4000 /* in .01 secs */
209
210struct mxu1_port {
211 u8 msr;
212 u8 mcr;
213 u8 uart_mode;
214 spinlock_t spinlock; /* Protects msr */
215 struct mutex mutex; /* Protects mcr */
216 bool send_break;
217};
218
219struct mxu1_device {
220 u16 mxd_model;
221};
222
223static const struct usb_device_id mxu1_idtable[] = {
224 { USB_DEVICE(MXU1_VENDOR_ID, MXU1_1110_PRODUCT_ID) },
225 { USB_DEVICE(MXU1_VENDOR_ID, MXU1_1130_PRODUCT_ID) },
226 { USB_DEVICE(MXU1_VENDOR_ID, MXU1_1150_PRODUCT_ID) },
227 { USB_DEVICE(MXU1_VENDOR_ID, MXU1_1151_PRODUCT_ID) },
228 { USB_DEVICE(MXU1_VENDOR_ID, MXU1_1131_PRODUCT_ID) },
229 { }
230};
231
232MODULE_DEVICE_TABLE(usb, mxu1_idtable);
233
234/* Write the given buffer out to the control pipe. */
235static int mxu1_send_ctrl_data_urb(struct usb_serial *serial,
236 u8 request,
237 u16 value, u16 index,
238 void *data, size_t size)
239{
240 int status;
241
242 status = usb_control_msg(serial->dev,
243 usb_sndctrlpipe(serial->dev, 0),
244 request,
245 (USB_DIR_OUT | USB_TYPE_VENDOR |
246 USB_RECIP_DEVICE), value, index,
247 data, size,
248 USB_CTRL_SET_TIMEOUT);
249 if (status < 0) {
250 dev_err(&serial->interface->dev,
251 "%s - usb_control_msg failed: %d\n",
252 __func__, status);
253 return status;
254 }
255
256 if (status != size) {
257 dev_err(&serial->interface->dev,
258 "%s - short write (%d / %zd)\n",
259 __func__, status, size);
260 return -EIO;
261 }
262
263 return 0;
264}
265
266/* Send a vendor request without any data */
267static int mxu1_send_ctrl_urb(struct usb_serial *serial,
268 u8 request, u16 value, u16 index)
269{
270 return mxu1_send_ctrl_data_urb(serial, request, value, index,
271 NULL, 0);
272}
273
274static int mxu1_download_firmware(struct usb_serial *serial,
275 const struct firmware *fw_p)
276{
277 int status = 0;
278 int buffer_size;
279 int pos;
280 int len;
281 int done;
282 u8 cs = 0;
283 u8 *buffer;
284 struct usb_device *dev = serial->dev;
285 struct mxu1_firmware_header *header;
286 unsigned int pipe;
287
288 pipe = usb_sndbulkpipe(dev, serial->port[0]->bulk_out_endpointAddress);
289
290 buffer_size = fw_p->size + sizeof(*header);
291 buffer = kmalloc(buffer_size, GFP_KERNEL);
292 if (!buffer)
293 return -ENOMEM;
294
295 memcpy(buffer, fw_p->data, fw_p->size);
296 memset(buffer + fw_p->size, 0xff, buffer_size - fw_p->size);
297
298 for (pos = sizeof(*header); pos < buffer_size; pos++)
299 cs = (u8)(cs + buffer[pos]);
300
301 header = (struct mxu1_firmware_header *)buffer;
302 header->wLength = cpu_to_le16(buffer_size - sizeof(*header));
303 header->bCheckSum = cs;
304
305 dev_dbg(&dev->dev, "%s - downloading firmware\n", __func__);
306
307 for (pos = 0; pos < buffer_size; pos += done) {
308 len = min(buffer_size - pos, MXU1_DOWNLOAD_MAX_PACKET_SIZE);
309
310 status = usb_bulk_msg(dev, pipe, buffer + pos, len, &done,
311 MXU1_DOWNLOAD_TIMEOUT);
312 if (status)
313 break;
314 }
315
316 kfree(buffer);
317
318 if (status) {
319 dev_err(&dev->dev, "failed to download firmware: %d\n", status);
320 return status;
321 }
322
323 msleep_interruptible(100);
324 usb_reset_device(dev);
325
326 dev_dbg(&dev->dev, "%s - download successful\n", __func__);
327
328 return 0;
329}
330
331static int mxu1_port_probe(struct usb_serial_port *port)
332{
333 struct mxu1_port *mxport;
334 struct mxu1_device *mxdev;
Johan Hovold924eccc2015-12-29 13:36:11 +0100335
336 if (!port->interrupt_in_urb) {
337 dev_err(&port->dev, "no interrupt urb\n");
338 return -ENODEV;
339 }
Mathieu OTHACEHE0b2b0932015-12-28 21:21:25 +0100340
341 mxport = kzalloc(sizeof(struct mxu1_port), GFP_KERNEL);
342 if (!mxport)
343 return -ENOMEM;
344
345 spin_lock_init(&mxport->spinlock);
346 mutex_init(&mxport->mutex);
347
348 mxdev = usb_get_serial_data(port->serial);
349
Mathieu OTHACEHE0b2b0932015-12-28 21:21:25 +0100350 switch (mxdev->mxd_model) {
351 case MXU1_1110_PRODUCT_ID:
352 case MXU1_1150_PRODUCT_ID:
353 case MXU1_1151_PRODUCT_ID:
354 mxport->uart_mode = MXU1_UART_232;
355 break;
356 case MXU1_1130_PRODUCT_ID:
357 case MXU1_1131_PRODUCT_ID:
358 mxport->uart_mode = MXU1_UART_485_RECEIVER_DISABLED;
359 break;
360 }
361
362 usb_set_serial_port_data(port, mxport);
363
364 port->port.closing_wait =
365 msecs_to_jiffies(MXU1_DEFAULT_CLOSING_WAIT * 10);
366 port->port.drain_delay = 1;
367
368 return 0;
369}
370
371static int mxu1_startup(struct usb_serial *serial)
372{
373 struct mxu1_device *mxdev;
374 struct usb_device *dev = serial->dev;
375 struct usb_host_interface *cur_altsetting;
376 char fw_name[32];
377 const struct firmware *fw_p = NULL;
378 int err;
Mathieu OTHACEHE0b2b0932015-12-28 21:21:25 +0100379
380 dev_dbg(&serial->interface->dev, "%s - product 0x%04X, num configurations %d, configuration value %d\n",
381 __func__, le16_to_cpu(dev->descriptor.idProduct),
382 dev->descriptor.bNumConfigurations,
383 dev->actconfig->desc.bConfigurationValue);
384
385 /* create device structure */
386 mxdev = kzalloc(sizeof(struct mxu1_device), GFP_KERNEL);
387 if (!mxdev)
388 return -ENOMEM;
389
390 usb_set_serial_data(serial, mxdev);
391
392 mxdev->mxd_model = le16_to_cpu(dev->descriptor.idProduct);
393
394 cur_altsetting = serial->interface->cur_altsetting;
395
396 /* if we have only 1 configuration, download firmware */
397 if (cur_altsetting->desc.bNumEndpoints == 1) {
398
399 snprintf(fw_name,
400 sizeof(fw_name),
401 "moxa/moxa-%04x.fw",
402 mxdev->mxd_model);
403
404 err = request_firmware(&fw_p, fw_name, &serial->interface->dev);
405 if (err) {
406 dev_err(&serial->interface->dev, "failed to request firmware: %d\n",
407 err);
Johan Hovolde69f7a62015-12-29 13:36:12 +0100408 goto err_free_mxdev;
Mathieu OTHACEHE0b2b0932015-12-28 21:21:25 +0100409 }
410
411 err = mxu1_download_firmware(serial, fw_p);
Johan Hovolde69f7a62015-12-29 13:36:12 +0100412 if (err)
413 goto err_release_firmware;
Mathieu OTHACEHE0b2b0932015-12-28 21:21:25 +0100414
Johan Hovolde69f7a62015-12-29 13:36:12 +0100415 /* device is being reset */
416 err = -ENODEV;
417 goto err_release_firmware;
Mathieu OTHACEHE0b2b0932015-12-28 21:21:25 +0100418 }
419
Johan Hovolde69f7a62015-12-29 13:36:12 +0100420 return 0;
421
422err_release_firmware:
423 release_firmware(fw_p);
424err_free_mxdev:
425 kfree(mxdev);
426
427 return err;
Mathieu OTHACEHE0b2b0932015-12-28 21:21:25 +0100428}
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,
Johan Hovold3645ea82015-12-29 13:36:15 +0100496 "%s - cflag 0x%08x, iflag 0x%08x\n", __func__, cflag, iflag);
Mathieu OTHACEHE0b2b0932015-12-28 21:21:25 +0100497
498 if (old_termios) {
Johan Hovold3645ea82015-12-29 13:36:15 +0100499 dev_dbg(&port->dev, "%s - old cflag 0x%08x, old iflag 0x%08x\n",
Mathieu OTHACEHE0b2b0932015-12-28 21:21:25 +0100500 __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)
Johan Hovold96315952015-12-29 13:36:13 +0100597 mcr |= MXU1_MCR_DTR | MXU1_MCR_RTS;
Mathieu OTHACEHE0b2b0932015-12-28 21:21:25 +0100598
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
Mathieu OTHACEHE0b2b0932015-12-28 21:21:25 +0100766 status = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
767 if (status) {
768 dev_err(&port->dev, "failed to submit interrupt urb: %d\n",
769 status);
770 return status;
771 }
772
773 if (tty)
774 mxu1_set_termios(tty, port, NULL);
775
776 status = mxu1_send_ctrl_urb(serial, MXU1_OPEN_PORT,
777 open_settings, MXU1_UART1_PORT);
778 if (status) {
Johan Hovold6ff9d272015-12-29 13:36:16 +0100779 dev_err(&port->dev, "cannot send open command: %d\n", status);
Mathieu OTHACEHE0b2b0932015-12-28 21:21:25 +0100780 goto unlink_int_urb;
781 }
782
783 status = mxu1_send_ctrl_urb(serial, MXU1_START_PORT,
784 0, MXU1_UART1_PORT);
785 if (status) {
Johan Hovold6ff9d272015-12-29 13:36:16 +0100786 dev_err(&port->dev, "cannot send start command: %d\n", status);
Mathieu OTHACEHE0b2b0932015-12-28 21:21:25 +0100787 goto unlink_int_urb;
788 }
789
790 status = mxu1_send_ctrl_urb(serial, MXU1_PURGE_PORT,
791 MXU1_PURGE_INPUT, MXU1_UART1_PORT);
792 if (status) {
Johan Hovold6ff9d272015-12-29 13:36:16 +0100793 dev_err(&port->dev, "cannot clear input buffers: %d\n",
794 status);
Mathieu OTHACEHE0b2b0932015-12-28 21:21:25 +0100795
796 goto unlink_int_urb;
797 }
798
799 status = mxu1_send_ctrl_urb(serial, MXU1_PURGE_PORT,
800 MXU1_PURGE_OUTPUT, MXU1_UART1_PORT);
801 if (status) {
Johan Hovold6ff9d272015-12-29 13:36:16 +0100802 dev_err(&port->dev, "cannot clear output buffers: %d\n",
803 status);
Mathieu OTHACEHE0b2b0932015-12-28 21:21:25 +0100804
805 goto unlink_int_urb;
806 }
807
808 /*
809 * reset the data toggle on the bulk endpoints to work around bug in
810 * host controllers where things get out of sync some times
811 */
812 usb_clear_halt(serial->dev, port->write_urb->pipe);
813 usb_clear_halt(serial->dev, port->read_urb->pipe);
814
815 if (tty)
816 mxu1_set_termios(tty, port, NULL);
817
818 status = mxu1_send_ctrl_urb(serial, MXU1_OPEN_PORT,
819 open_settings, MXU1_UART1_PORT);
820 if (status) {
Johan Hovold6ff9d272015-12-29 13:36:16 +0100821 dev_err(&port->dev, "cannot send open command: %d\n", status);
Mathieu OTHACEHE0b2b0932015-12-28 21:21:25 +0100822 goto unlink_int_urb;
823 }
824
825 status = mxu1_send_ctrl_urb(serial, MXU1_START_PORT,
826 0, MXU1_UART1_PORT);
827 if (status) {
Johan Hovold6ff9d272015-12-29 13:36:16 +0100828 dev_err(&port->dev, "cannot send start command: %d\n", status);
Mathieu OTHACEHE0b2b0932015-12-28 21:21:25 +0100829 goto unlink_int_urb;
830 }
831
832 status = usb_serial_generic_open(tty, port);
Johan Hovold6ff9d272015-12-29 13:36:16 +0100833 if (status)
Mathieu OTHACEHE0b2b0932015-12-28 21:21:25 +0100834 goto unlink_int_urb;
Mathieu OTHACEHE0b2b0932015-12-28 21:21:25 +0100835
Johan Hovold3645ea82015-12-29 13:36:15 +0100836 return 0;
Mathieu OTHACEHE0b2b0932015-12-28 21:21:25 +0100837
838unlink_int_urb:
839 usb_kill_urb(port->interrupt_in_urb);
840
841 return status;
842}
843
844static void mxu1_close(struct usb_serial_port *port)
845{
846 int status;
847
848 usb_serial_generic_close(port);
849 usb_kill_urb(port->interrupt_in_urb);
850
851 status = mxu1_send_ctrl_urb(port->serial, MXU1_CLOSE_PORT,
852 0, MXU1_UART1_PORT);
Johan Hovold3645ea82015-12-29 13:36:15 +0100853 if (status) {
Mathieu OTHACEHE0b2b0932015-12-28 21:21:25 +0100854 dev_err(&port->dev, "failed to send close port command: %d\n",
855 status);
Johan Hovold3645ea82015-12-29 13:36:15 +0100856 }
Mathieu OTHACEHE0b2b0932015-12-28 21:21:25 +0100857}
858
859static void mxu1_handle_new_msr(struct usb_serial_port *port, u8 msr)
860{
Johan Hovold3645ea82015-12-29 13:36:15 +0100861 struct mxu1_port *mxport = usb_get_serial_port_data(port);
Mathieu OTHACEHE0b2b0932015-12-28 21:21:25 +0100862 struct async_icount *icount;
Mathieu OTHACEHE0b2b0932015-12-28 21:21:25 +0100863 unsigned long flags;
864
865 dev_dbg(&port->dev, "%s - msr 0x%02X\n", __func__, msr);
866
Mathieu OTHACEHE0b2b0932015-12-28 21:21:25 +0100867 spin_lock_irqsave(&mxport->spinlock, flags);
868 mxport->msr = msr & MXU1_MSR_MASK;
869 spin_unlock_irqrestore(&mxport->spinlock, flags);
870
871 if (msr & MXU1_MSR_DELTA_MASK) {
872 icount = &port->icount;
873 if (msr & MXU1_MSR_DELTA_CTS)
874 icount->cts++;
875 if (msr & MXU1_MSR_DELTA_DSR)
876 icount->dsr++;
877 if (msr & MXU1_MSR_DELTA_CD)
878 icount->dcd++;
879 if (msr & MXU1_MSR_DELTA_RI)
880 icount->rng++;
881
882 wake_up_interruptible(&port->port.delta_msr_wait);
883 }
884}
885
886static void mxu1_interrupt_callback(struct urb *urb)
887{
888 struct usb_serial_port *port = urb->context;
889 unsigned char *data = urb->transfer_buffer;
890 int length = urb->actual_length;
891 int function;
892 int status;
893 u8 msr;
894
895 switch (urb->status) {
896 case 0:
897 break;
898 case -ECONNRESET:
899 case -ENOENT:
900 case -ESHUTDOWN:
901 dev_dbg(&port->dev, "%s - urb shutting down: %d\n",
902 __func__, urb->status);
903 return;
904 default:
905 dev_dbg(&port->dev, "%s - nonzero urb status: %d\n",
906 __func__, urb->status);
907 goto exit;
908 }
909
910 if (length != 2) {
911 dev_dbg(&port->dev, "%s - bad packet size: %d\n",
912 __func__, length);
913 goto exit;
914 }
915
916 if (data[0] == MXU1_CODE_HARDWARE_ERROR) {
Johan Hovold6ff9d272015-12-29 13:36:16 +0100917 dev_err(&port->dev, "hardware error: %d\n", data[1]);
Mathieu OTHACEHE0b2b0932015-12-28 21:21:25 +0100918 goto exit;
919 }
920
921 function = mxu1_get_func_from_code(data[0]);
922
923 dev_dbg(&port->dev, "%s - function %d, data 0x%02X\n",
924 __func__, function, data[1]);
925
926 switch (function) {
927 case MXU1_CODE_DATA_ERROR:
928 dev_dbg(&port->dev, "%s - DATA ERROR, data 0x%02X\n",
929 __func__, data[1]);
930 break;
931
932 case MXU1_CODE_MODEM_STATUS:
933 msr = data[1];
934 mxu1_handle_new_msr(port, msr);
935 break;
936
937 default:
Johan Hovold6ff9d272015-12-29 13:36:16 +0100938 dev_err(&port->dev, "unknown interrupt code: 0x%02X\n",
939 data[1]);
Mathieu OTHACEHE0b2b0932015-12-28 21:21:25 +0100940 break;
941 }
942
943exit:
944 status = usb_submit_urb(urb, GFP_ATOMIC);
Johan Hovold3645ea82015-12-29 13:36:15 +0100945 if (status) {
Mathieu OTHACEHE0b2b0932015-12-28 21:21:25 +0100946 dev_err(&port->dev, "resubmit interrupt urb failed: %d\n",
947 status);
Johan Hovold3645ea82015-12-29 13:36:15 +0100948 }
Mathieu OTHACEHE0b2b0932015-12-28 21:21:25 +0100949}
950
Johan Hovold554eb0f2015-12-29 13:36:14 +0100951static struct usb_serial_driver mxu11x0_device = {
Mathieu OTHACEHE0b2b0932015-12-28 21:21:25 +0100952 .driver = {
953 .owner = THIS_MODULE,
Johan Hovold554eb0f2015-12-29 13:36:14 +0100954 .name = "mxu11x0",
Mathieu OTHACEHE0b2b0932015-12-28 21:21:25 +0100955 },
956 .description = "MOXA UPort 11x0",
957 .id_table = mxu1_idtable,
958 .num_ports = 1,
959 .port_probe = mxu1_port_probe,
960 .attach = mxu1_startup,
961 .open = mxu1_open,
962 .close = mxu1_close,
963 .ioctl = mxu1_ioctl,
964 .set_termios = mxu1_set_termios,
965 .tiocmget = mxu1_tiocmget,
966 .tiocmset = mxu1_tiocmset,
967 .tiocmiwait = usb_serial_generic_tiocmiwait,
968 .get_icount = usb_serial_generic_get_icount,
969 .break_ctl = mxu1_break,
970 .read_int_callback = mxu1_interrupt_callback,
971};
972
973static struct usb_serial_driver *const serial_drivers[] = {
Johan Hovold554eb0f2015-12-29 13:36:14 +0100974 &mxu11x0_device, NULL
Mathieu OTHACEHE0b2b0932015-12-28 21:21:25 +0100975};
976
977module_usb_serial_driver(serial_drivers, mxu1_idtable);
978
979MODULE_AUTHOR("Mathieu Othacehe <m.othacehe@gmail.com>");
980MODULE_DESCRIPTION("MOXA UPort 11x0 USB to Serial Hub Driver");
981MODULE_LICENSE("GPL");
982MODULE_FIRMWARE("moxa/moxa-1110.fw");
983MODULE_FIRMWARE("moxa/moxa-1130.fw");
984MODULE_FIRMWARE("moxa/moxa-1131.fw");
985MODULE_FIRMWARE("moxa/moxa-1150.fw");
986MODULE_FIRMWARE("moxa/moxa-1151.fw");