0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include <linux/kernel.h>
0016 #include <linux/module.h>
0017 #include <linux/firmware.h>
0018 #include <linux/jiffies.h>
0019 #include <linux/serial.h>
0020 #include <linux/serial_reg.h>
0021 #include <linux/slab.h>
0022 #include <linux/tty.h>
0023 #include <linux/tty_driver.h>
0024 #include <linux/tty_flip.h>
0025 #include <linux/uaccess.h>
0026 #include <linux/usb.h>
0027 #include <linux/usb/serial.h>
0028 #include <asm/unaligned.h>
0029
0030
0031 #define MX_USBSERIAL_VID 0x110A
0032 #define MX_UPORT1250_PID 0x1250
0033 #define MX_UPORT1251_PID 0x1251
0034 #define MX_UPORT1410_PID 0x1410
0035 #define MX_UPORT1450_PID 0x1450
0036 #define MX_UPORT1451_PID 0x1451
0037 #define MX_UPORT1618_PID 0x1618
0038 #define MX_UPORT1658_PID 0x1658
0039 #define MX_UPORT1613_PID 0x1613
0040 #define MX_UPORT1653_PID 0x1653
0041
0042
0043 #define HEADER_SIZE 4
0044 #define EVENT_LENGTH 8
0045 #define DOWN_BLOCK_SIZE 64
0046
0047
0048 #define VER_ADDR_1 0x20
0049 #define VER_ADDR_2 0x24
0050 #define VER_ADDR_3 0x28
0051
0052
0053 #define RQ_VENDOR_NONE 0x00
0054 #define RQ_VENDOR_SET_BAUD 0x01
0055 #define RQ_VENDOR_SET_LINE 0x02
0056 #define RQ_VENDOR_SET_CHARS 0x03
0057 #define RQ_VENDOR_SET_RTS 0x04
0058 #define RQ_VENDOR_SET_DTR 0x05
0059 #define RQ_VENDOR_SET_XONXOFF 0x06
0060 #define RQ_VENDOR_SET_RX_HOST_EN 0x07
0061 #define RQ_VENDOR_SET_OPEN 0x08
0062 #define RQ_VENDOR_PURGE 0x09
0063 #define RQ_VENDOR_SET_MCR 0x0A
0064 #define RQ_VENDOR_SET_BREAK 0x0B
0065
0066 #define RQ_VENDOR_START_FW_DOWN 0x0C
0067 #define RQ_VENDOR_STOP_FW_DOWN 0x0D
0068 #define RQ_VENDOR_QUERY_FW_READY 0x0E
0069
0070 #define RQ_VENDOR_SET_FIFO_DISABLE 0x0F
0071 #define RQ_VENDOR_SET_INTERFACE 0x10
0072 #define RQ_VENDOR_SET_HIGH_PERFOR 0x11
0073
0074 #define RQ_VENDOR_ERASE_BLOCK 0x12
0075 #define RQ_VENDOR_WRITE_PAGE 0x13
0076 #define RQ_VENDOR_PREPARE_WRITE 0x14
0077 #define RQ_VENDOR_CONFIRM_WRITE 0x15
0078 #define RQ_VENDOR_LOCATE 0x16
0079
0080 #define RQ_VENDOR_START_ROM_DOWN 0x17
0081 #define RQ_VENDOR_ROM_DATA 0x18
0082 #define RQ_VENDOR_STOP_ROM_DOWN 0x19
0083 #define RQ_VENDOR_FW_DATA 0x20
0084
0085 #define RQ_VENDOR_RESET_DEVICE 0x23
0086 #define RQ_VENDOR_QUERY_FW_CONFIG 0x24
0087
0088 #define RQ_VENDOR_GET_VERSION 0x81
0089 #define RQ_VENDOR_GET_PAGE 0x82
0090 #define RQ_VENDOR_GET_ROM_PROC 0x83
0091
0092 #define RQ_VENDOR_GET_INQUEUE 0x84
0093 #define RQ_VENDOR_GET_OUTQUEUE 0x85
0094
0095 #define RQ_VENDOR_GET_MSR 0x86
0096
0097
0098 #define UPORT_EVENT_NONE 0
0099 #define UPORT_EVENT_TXBUF_THRESHOLD 1
0100 #define UPORT_EVENT_SEND_NEXT 2
0101 #define UPORT_EVENT_MSR 3
0102 #define UPORT_EVENT_LSR 4
0103 #define UPORT_EVENT_MCR 5
0104
0105
0106 #define SERIAL_EV_CTS 0x0008
0107 #define SERIAL_EV_DSR 0x0010
0108 #define SERIAL_EV_RLSD 0x0020
0109
0110
0111 #define SERIAL_EV_XOFF 0x40
0112
0113
0114 #define MX_WORDLENGTH_5 5
0115 #define MX_WORDLENGTH_6 6
0116 #define MX_WORDLENGTH_7 7
0117 #define MX_WORDLENGTH_8 8
0118
0119 #define MX_PARITY_NONE 0
0120 #define MX_PARITY_ODD 1
0121 #define MX_PARITY_EVEN 2
0122 #define MX_PARITY_MARK 3
0123 #define MX_PARITY_SPACE 4
0124
0125 #define MX_STOP_BITS_1 0
0126 #define MX_STOP_BITS_1_5 1
0127 #define MX_STOP_BITS_2 2
0128
0129 #define MX_RTS_DISABLE 0x0
0130 #define MX_RTS_ENABLE 0x1
0131 #define MX_RTS_HW 0x2
0132 #define MX_RTS_NO_CHANGE 0x3
0133
0134 #define MX_INT_RS232 0
0135 #define MX_INT_2W_RS485 1
0136 #define MX_INT_RS422 2
0137 #define MX_INT_4W_RS485 3
0138
0139
0140 #define MX_WAIT_FOR_CTS 0x0001
0141 #define MX_WAIT_FOR_DSR 0x0002
0142 #define MX_WAIT_FOR_DCD 0x0004
0143 #define MX_WAIT_FOR_XON 0x0008
0144 #define MX_WAIT_FOR_START_TX 0x0010
0145 #define MX_WAIT_FOR_UNTHROTTLE 0x0020
0146 #define MX_WAIT_FOR_LOW_WATER 0x0040
0147 #define MX_WAIT_FOR_SEND_NEXT 0x0080
0148
0149 #define MX_UPORT_2_PORT BIT(0)
0150 #define MX_UPORT_4_PORT BIT(1)
0151 #define MX_UPORT_8_PORT BIT(2)
0152 #define MX_UPORT_16_PORT BIT(3)
0153
0154
0155 struct mxuport_port {
0156 u8 mcr_state;
0157 u8 msr_state;
0158 struct mutex mutex;
0159 spinlock_t spinlock;
0160 };
0161
0162
0163 static const struct usb_device_id mxuport_idtable[] = {
0164 { USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1250_PID),
0165 .driver_info = MX_UPORT_2_PORT },
0166 { USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1251_PID),
0167 .driver_info = MX_UPORT_2_PORT },
0168 { USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1410_PID),
0169 .driver_info = MX_UPORT_4_PORT },
0170 { USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1450_PID),
0171 .driver_info = MX_UPORT_4_PORT },
0172 { USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1451_PID),
0173 .driver_info = MX_UPORT_4_PORT },
0174 { USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1618_PID),
0175 .driver_info = MX_UPORT_8_PORT },
0176 { USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1658_PID),
0177 .driver_info = MX_UPORT_8_PORT },
0178 { USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1613_PID),
0179 .driver_info = MX_UPORT_16_PORT },
0180 { USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1653_PID),
0181 .driver_info = MX_UPORT_16_PORT },
0182 {}
0183 };
0184
0185 MODULE_DEVICE_TABLE(usb, mxuport_idtable);
0186
0187
0188
0189
0190
0191
0192 static int mxuport_prepare_write_buffer(struct usb_serial_port *port,
0193 void *dest, size_t size)
0194 {
0195 u8 *buf = dest;
0196 int count;
0197
0198 count = kfifo_out_locked(&port->write_fifo, buf + HEADER_SIZE,
0199 size - HEADER_SIZE,
0200 &port->lock);
0201
0202 put_unaligned_be16(port->port_number, buf);
0203 put_unaligned_be16(count, buf + 2);
0204
0205 dev_dbg(&port->dev, "%s - size %zd count %d\n", __func__,
0206 size, count);
0207
0208 return count + HEADER_SIZE;
0209 }
0210
0211
0212 static int mxuport_recv_ctrl_urb(struct usb_serial *serial,
0213 u8 request, u16 value, u16 index,
0214 u8 *data, size_t size)
0215 {
0216 int status;
0217
0218 status = usb_control_msg(serial->dev,
0219 usb_rcvctrlpipe(serial->dev, 0),
0220 request,
0221 (USB_DIR_IN | USB_TYPE_VENDOR |
0222 USB_RECIP_DEVICE), value, index,
0223 data, size,
0224 USB_CTRL_GET_TIMEOUT);
0225 if (status < 0) {
0226 dev_err(&serial->interface->dev,
0227 "%s - usb_control_msg failed (%d)\n",
0228 __func__, status);
0229 return status;
0230 }
0231
0232 if (status != size) {
0233 dev_err(&serial->interface->dev,
0234 "%s - short read (%d / %zd)\n",
0235 __func__, status, size);
0236 return -EIO;
0237 }
0238
0239 return status;
0240 }
0241
0242
0243 static int mxuport_send_ctrl_data_urb(struct usb_serial *serial,
0244 u8 request,
0245 u16 value, u16 index,
0246 u8 *data, size_t size)
0247 {
0248 int status;
0249
0250 status = usb_control_msg(serial->dev,
0251 usb_sndctrlpipe(serial->dev, 0),
0252 request,
0253 (USB_DIR_OUT | USB_TYPE_VENDOR |
0254 USB_RECIP_DEVICE), value, index,
0255 data, size,
0256 USB_CTRL_SET_TIMEOUT);
0257 if (status < 0) {
0258 dev_err(&serial->interface->dev,
0259 "%s - usb_control_msg failed (%d)\n",
0260 __func__, status);
0261 return status;
0262 }
0263
0264 return 0;
0265 }
0266
0267
0268 static int mxuport_send_ctrl_urb(struct usb_serial *serial,
0269 u8 request, u16 value, u16 index)
0270 {
0271 return mxuport_send_ctrl_data_urb(serial, request, value, index,
0272 NULL, 0);
0273 }
0274
0275
0276
0277
0278
0279
0280
0281
0282
0283
0284 static void mxuport_throttle(struct tty_struct *tty)
0285 {
0286 struct usb_serial_port *port = tty->driver_data;
0287 struct usb_serial *serial = port->serial;
0288
0289 dev_dbg(&port->dev, "%s\n", __func__);
0290
0291 mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN,
0292 0, port->port_number);
0293 }
0294
0295
0296
0297
0298
0299
0300
0301
0302 static void mxuport_unthrottle(struct tty_struct *tty)
0303 {
0304
0305 struct usb_serial_port *port = tty->driver_data;
0306 struct usb_serial *serial = port->serial;
0307
0308 dev_dbg(&port->dev, "%s\n", __func__);
0309
0310 mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN,
0311 1, port->port_number);
0312 }
0313
0314
0315
0316
0317
0318 static void mxuport_process_read_urb_data(struct usb_serial_port *port,
0319 char *data, int size)
0320 {
0321 int i;
0322
0323 if (port->sysrq) {
0324 for (i = 0; i < size; i++, data++) {
0325 if (!usb_serial_handle_sysrq_char(port, *data))
0326 tty_insert_flip_char(&port->port, *data,
0327 TTY_NORMAL);
0328 }
0329 } else {
0330 tty_insert_flip_string(&port->port, data, size);
0331 }
0332 tty_flip_buffer_push(&port->port);
0333 }
0334
0335 static void mxuport_msr_event(struct usb_serial_port *port, u8 buf[4])
0336 {
0337 struct mxuport_port *mxport = usb_get_serial_port_data(port);
0338 u8 rcv_msr_hold = buf[2] & 0xF0;
0339 u16 rcv_msr_event = get_unaligned_be16(buf);
0340 unsigned long flags;
0341
0342 if (rcv_msr_event == 0)
0343 return;
0344
0345
0346 spin_lock_irqsave(&mxport->spinlock, flags);
0347
0348 dev_dbg(&port->dev, "%s - current MSR status = 0x%x\n",
0349 __func__, mxport->msr_state);
0350
0351 if (rcv_msr_hold & UART_MSR_CTS) {
0352 mxport->msr_state |= UART_MSR_CTS;
0353 dev_dbg(&port->dev, "%s - CTS high\n", __func__);
0354 } else {
0355 mxport->msr_state &= ~UART_MSR_CTS;
0356 dev_dbg(&port->dev, "%s - CTS low\n", __func__);
0357 }
0358
0359 if (rcv_msr_hold & UART_MSR_DSR) {
0360 mxport->msr_state |= UART_MSR_DSR;
0361 dev_dbg(&port->dev, "%s - DSR high\n", __func__);
0362 } else {
0363 mxport->msr_state &= ~UART_MSR_DSR;
0364 dev_dbg(&port->dev, "%s - DSR low\n", __func__);
0365 }
0366
0367 if (rcv_msr_hold & UART_MSR_DCD) {
0368 mxport->msr_state |= UART_MSR_DCD;
0369 dev_dbg(&port->dev, "%s - DCD high\n", __func__);
0370 } else {
0371 mxport->msr_state &= ~UART_MSR_DCD;
0372 dev_dbg(&port->dev, "%s - DCD low\n", __func__);
0373 }
0374 spin_unlock_irqrestore(&mxport->spinlock, flags);
0375
0376 if (rcv_msr_event &
0377 (SERIAL_EV_CTS | SERIAL_EV_DSR | SERIAL_EV_RLSD)) {
0378
0379 if (rcv_msr_event & SERIAL_EV_CTS) {
0380 port->icount.cts++;
0381 dev_dbg(&port->dev, "%s - CTS change\n", __func__);
0382 }
0383
0384 if (rcv_msr_event & SERIAL_EV_DSR) {
0385 port->icount.dsr++;
0386 dev_dbg(&port->dev, "%s - DSR change\n", __func__);
0387 }
0388
0389 if (rcv_msr_event & SERIAL_EV_RLSD) {
0390 port->icount.dcd++;
0391 dev_dbg(&port->dev, "%s - DCD change\n", __func__);
0392 }
0393 wake_up_interruptible(&port->port.delta_msr_wait);
0394 }
0395 }
0396
0397 static void mxuport_lsr_event(struct usb_serial_port *port, u8 buf[4])
0398 {
0399 u8 lsr_event = buf[2];
0400
0401 if (lsr_event & UART_LSR_BI) {
0402 port->icount.brk++;
0403 dev_dbg(&port->dev, "%s - break error\n", __func__);
0404 }
0405
0406 if (lsr_event & UART_LSR_FE) {
0407 port->icount.frame++;
0408 dev_dbg(&port->dev, "%s - frame error\n", __func__);
0409 }
0410
0411 if (lsr_event & UART_LSR_PE) {
0412 port->icount.parity++;
0413 dev_dbg(&port->dev, "%s - parity error\n", __func__);
0414 }
0415
0416 if (lsr_event & UART_LSR_OE) {
0417 port->icount.overrun++;
0418 dev_dbg(&port->dev, "%s - overrun error\n", __func__);
0419 }
0420 }
0421
0422
0423
0424
0425
0426 static void mxuport_process_read_urb_event(struct usb_serial_port *port,
0427 u8 buf[4], u32 event)
0428 {
0429 dev_dbg(&port->dev, "%s - receive event : %04x\n", __func__, event);
0430
0431 switch (event) {
0432 case UPORT_EVENT_SEND_NEXT:
0433
0434
0435
0436
0437 break;
0438 case UPORT_EVENT_MSR:
0439 mxuport_msr_event(port, buf);
0440 break;
0441 case UPORT_EVENT_LSR:
0442 mxuport_lsr_event(port, buf);
0443 break;
0444 case UPORT_EVENT_MCR:
0445
0446
0447
0448
0449
0450
0451
0452 break;
0453 default:
0454 dev_dbg(&port->dev, "Unexpected event\n");
0455 break;
0456 }
0457 }
0458
0459
0460
0461
0462
0463 static void mxuport_process_read_urb_demux_data(struct urb *urb)
0464 {
0465 struct usb_serial_port *port = urb->context;
0466 struct usb_serial *serial = port->serial;
0467 u8 *data = urb->transfer_buffer;
0468 u8 *end = data + urb->actual_length;
0469 struct usb_serial_port *demux_port;
0470 u8 *ch;
0471 u16 rcv_port;
0472 u16 rcv_len;
0473
0474 while (data < end) {
0475 if (data + HEADER_SIZE > end) {
0476 dev_warn(&port->dev, "%s - message with short header\n",
0477 __func__);
0478 return;
0479 }
0480
0481 rcv_port = get_unaligned_be16(data);
0482 if (rcv_port >= serial->num_ports) {
0483 dev_warn(&port->dev, "%s - message for invalid port\n",
0484 __func__);
0485 return;
0486 }
0487
0488 demux_port = serial->port[rcv_port];
0489 rcv_len = get_unaligned_be16(data + 2);
0490 if (!rcv_len || data + HEADER_SIZE + rcv_len > end) {
0491 dev_warn(&port->dev, "%s - short data\n", __func__);
0492 return;
0493 }
0494
0495 if (tty_port_initialized(&demux_port->port)) {
0496 ch = data + HEADER_SIZE;
0497 mxuport_process_read_urb_data(demux_port, ch, rcv_len);
0498 } else {
0499 dev_dbg(&demux_port->dev, "%s - data for closed port\n",
0500 __func__);
0501 }
0502 data += HEADER_SIZE + rcv_len;
0503 }
0504 }
0505
0506
0507
0508
0509
0510 static void mxuport_process_read_urb_demux_event(struct urb *urb)
0511 {
0512 struct usb_serial_port *port = urb->context;
0513 struct usb_serial *serial = port->serial;
0514 u8 *data = urb->transfer_buffer;
0515 u8 *end = data + urb->actual_length;
0516 struct usb_serial_port *demux_port;
0517 u8 *ch;
0518 u16 rcv_port;
0519 u16 rcv_event;
0520
0521 while (data < end) {
0522 if (data + EVENT_LENGTH > end) {
0523 dev_warn(&port->dev, "%s - message with short event\n",
0524 __func__);
0525 return;
0526 }
0527
0528 rcv_port = get_unaligned_be16(data);
0529 if (rcv_port >= serial->num_ports) {
0530 dev_warn(&port->dev, "%s - message for invalid port\n",
0531 __func__);
0532 return;
0533 }
0534
0535 demux_port = serial->port[rcv_port];
0536 if (tty_port_initialized(&demux_port->port)) {
0537 ch = data + HEADER_SIZE;
0538 rcv_event = get_unaligned_be16(data + 2);
0539 mxuport_process_read_urb_event(demux_port, ch,
0540 rcv_event);
0541 } else {
0542 dev_dbg(&demux_port->dev,
0543 "%s - event for closed port\n", __func__);
0544 }
0545 data += EVENT_LENGTH;
0546 }
0547 }
0548
0549
0550
0551
0552
0553
0554 static void mxuport_process_read_urb(struct urb *urb)
0555 {
0556 struct usb_serial_port *port = urb->context;
0557 struct usb_serial *serial = port->serial;
0558
0559 if (port == serial->port[0])
0560 mxuport_process_read_urb_demux_data(urb);
0561
0562 if (port == serial->port[1])
0563 mxuport_process_read_urb_demux_event(urb);
0564 }
0565
0566
0567
0568
0569
0570 static bool mxuport_tx_empty(struct usb_serial_port *port)
0571 {
0572 struct usb_serial *serial = port->serial;
0573 bool is_empty = true;
0574 u32 txlen;
0575 u8 *len_buf;
0576 int err;
0577
0578 len_buf = kzalloc(4, GFP_KERNEL);
0579 if (!len_buf)
0580 goto out;
0581
0582 err = mxuport_recv_ctrl_urb(serial, RQ_VENDOR_GET_OUTQUEUE, 0,
0583 port->port_number, len_buf, 4);
0584 if (err < 0)
0585 goto out;
0586
0587 txlen = get_unaligned_be32(len_buf);
0588 dev_dbg(&port->dev, "%s - tx len = %u\n", __func__, txlen);
0589
0590 if (txlen != 0)
0591 is_empty = false;
0592
0593 out:
0594 kfree(len_buf);
0595 return is_empty;
0596 }
0597
0598 static int mxuport_set_mcr(struct usb_serial_port *port, u8 mcr_state)
0599 {
0600 struct usb_serial *serial = port->serial;
0601 int err;
0602
0603 dev_dbg(&port->dev, "%s - %02x\n", __func__, mcr_state);
0604
0605 err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_MCR,
0606 mcr_state, port->port_number);
0607 if (err)
0608 dev_err(&port->dev, "%s - failed to change MCR\n", __func__);
0609
0610 return err;
0611 }
0612
0613 static int mxuport_set_dtr(struct usb_serial_port *port, int on)
0614 {
0615 struct mxuport_port *mxport = usb_get_serial_port_data(port);
0616 struct usb_serial *serial = port->serial;
0617 int err;
0618
0619 mutex_lock(&mxport->mutex);
0620
0621 err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_DTR,
0622 !!on, port->port_number);
0623 if (!err) {
0624 if (on)
0625 mxport->mcr_state |= UART_MCR_DTR;
0626 else
0627 mxport->mcr_state &= ~UART_MCR_DTR;
0628 }
0629
0630 mutex_unlock(&mxport->mutex);
0631
0632 return err;
0633 }
0634
0635 static int mxuport_set_rts(struct usb_serial_port *port, u8 state)
0636 {
0637 struct mxuport_port *mxport = usb_get_serial_port_data(port);
0638 struct usb_serial *serial = port->serial;
0639 int err;
0640 u8 mcr_state;
0641
0642 mutex_lock(&mxport->mutex);
0643 mcr_state = mxport->mcr_state;
0644
0645 switch (state) {
0646 case MX_RTS_DISABLE:
0647 mcr_state &= ~UART_MCR_RTS;
0648 break;
0649 case MX_RTS_ENABLE:
0650 mcr_state |= UART_MCR_RTS;
0651 break;
0652 case MX_RTS_HW:
0653
0654
0655
0656
0657 break;
0658 default:
0659
0660
0661
0662
0663 err = -EINVAL;
0664 goto out;
0665 }
0666 err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RTS,
0667 state, port->port_number);
0668 if (!err)
0669 mxport->mcr_state = mcr_state;
0670
0671 out:
0672 mutex_unlock(&mxport->mutex);
0673
0674 return err;
0675 }
0676
0677 static void mxuport_dtr_rts(struct usb_serial_port *port, int on)
0678 {
0679 struct mxuport_port *mxport = usb_get_serial_port_data(port);
0680 u8 mcr_state;
0681 int err;
0682
0683 mutex_lock(&mxport->mutex);
0684 mcr_state = mxport->mcr_state;
0685
0686 if (on)
0687 mcr_state |= (UART_MCR_RTS | UART_MCR_DTR);
0688 else
0689 mcr_state &= ~(UART_MCR_RTS | UART_MCR_DTR);
0690
0691 err = mxuport_set_mcr(port, mcr_state);
0692 if (!err)
0693 mxport->mcr_state = mcr_state;
0694
0695 mutex_unlock(&mxport->mutex);
0696 }
0697
0698 static int mxuport_tiocmset(struct tty_struct *tty, unsigned int set,
0699 unsigned int clear)
0700 {
0701 struct usb_serial_port *port = tty->driver_data;
0702 struct mxuport_port *mxport = usb_get_serial_port_data(port);
0703 int err;
0704 u8 mcr_state;
0705
0706 mutex_lock(&mxport->mutex);
0707 mcr_state = mxport->mcr_state;
0708
0709 if (set & TIOCM_RTS)
0710 mcr_state |= UART_MCR_RTS;
0711
0712 if (set & TIOCM_DTR)
0713 mcr_state |= UART_MCR_DTR;
0714
0715 if (clear & TIOCM_RTS)
0716 mcr_state &= ~UART_MCR_RTS;
0717
0718 if (clear & TIOCM_DTR)
0719 mcr_state &= ~UART_MCR_DTR;
0720
0721 err = mxuport_set_mcr(port, mcr_state);
0722 if (!err)
0723 mxport->mcr_state = mcr_state;
0724
0725 mutex_unlock(&mxport->mutex);
0726
0727 return err;
0728 }
0729
0730 static int mxuport_tiocmget(struct tty_struct *tty)
0731 {
0732 struct mxuport_port *mxport;
0733 struct usb_serial_port *port = tty->driver_data;
0734 unsigned int result;
0735 unsigned long flags;
0736 unsigned int msr;
0737 unsigned int mcr;
0738
0739 mxport = usb_get_serial_port_data(port);
0740
0741 mutex_lock(&mxport->mutex);
0742 spin_lock_irqsave(&mxport->spinlock, flags);
0743
0744 msr = mxport->msr_state;
0745 mcr = mxport->mcr_state;
0746
0747 spin_unlock_irqrestore(&mxport->spinlock, flags);
0748 mutex_unlock(&mxport->mutex);
0749
0750 result = (((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) |
0751 ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) |
0752 ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) |
0753 ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) |
0754 ((msr & UART_MSR_RI) ? TIOCM_RI : 0) |
0755 ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0));
0756
0757 dev_dbg(&port->dev, "%s - 0x%04x\n", __func__, result);
0758
0759 return result;
0760 }
0761
0762 static int mxuport_set_termios_flow(struct tty_struct *tty,
0763 struct ktermios *old_termios,
0764 struct usb_serial_port *port,
0765 struct usb_serial *serial)
0766 {
0767 u8 xon = START_CHAR(tty);
0768 u8 xoff = STOP_CHAR(tty);
0769 int enable;
0770 int err;
0771 u8 *buf;
0772 u8 rts;
0773
0774 buf = kmalloc(2, GFP_KERNEL);
0775 if (!buf)
0776 return -ENOMEM;
0777
0778
0779 if (I_IXOFF(tty) || I_IXON(tty)) {
0780 enable = 1;
0781 buf[0] = xon;
0782 buf[1] = xoff;
0783
0784 err = mxuport_send_ctrl_data_urb(serial, RQ_VENDOR_SET_CHARS,
0785 0, port->port_number,
0786 buf, 2);
0787 if (err)
0788 goto out;
0789
0790 dev_dbg(&port->dev, "%s - XON = 0x%02x, XOFF = 0x%02x\n",
0791 __func__, xon, xoff);
0792 } else {
0793 enable = 0;
0794 }
0795
0796 err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_XONXOFF,
0797 enable, port->port_number);
0798 if (err)
0799 goto out;
0800
0801 rts = MX_RTS_NO_CHANGE;
0802
0803
0804 if (!old_termios ||
0805 C_CRTSCTS(tty) != (old_termios->c_cflag & CRTSCTS)) {
0806 if (C_CRTSCTS(tty))
0807 rts = MX_RTS_HW;
0808 else
0809 rts = MX_RTS_ENABLE;
0810 }
0811
0812 if (C_BAUD(tty)) {
0813 if (old_termios && (old_termios->c_cflag & CBAUD) == B0) {
0814
0815 if (C_CRTSCTS(tty))
0816 rts = MX_RTS_HW;
0817 else
0818 rts = MX_RTS_ENABLE;
0819 mxuport_set_dtr(port, 1);
0820 }
0821 } else {
0822
0823 rts = MX_RTS_DISABLE;
0824 mxuport_set_dtr(port, 0);
0825 }
0826
0827 if (rts != MX_RTS_NO_CHANGE)
0828 err = mxuport_set_rts(port, rts);
0829
0830 out:
0831 kfree(buf);
0832 return err;
0833 }
0834
0835 static void mxuport_set_termios(struct tty_struct *tty,
0836 struct usb_serial_port *port,
0837 struct ktermios *old_termios)
0838 {
0839 struct usb_serial *serial = port->serial;
0840 u8 *buf;
0841 u8 data_bits;
0842 u8 stop_bits;
0843 u8 parity;
0844 int baud;
0845 int err;
0846
0847 if (old_termios &&
0848 !tty_termios_hw_change(&tty->termios, old_termios) &&
0849 tty->termios.c_iflag == old_termios->c_iflag) {
0850 dev_dbg(&port->dev, "%s - nothing to change\n", __func__);
0851 return;
0852 }
0853
0854 buf = kmalloc(4, GFP_KERNEL);
0855 if (!buf)
0856 return;
0857
0858
0859 switch (C_CSIZE(tty)) {
0860 case CS5:
0861 data_bits = MX_WORDLENGTH_5;
0862 break;
0863 case CS6:
0864 data_bits = MX_WORDLENGTH_6;
0865 break;
0866 case CS7:
0867 data_bits = MX_WORDLENGTH_7;
0868 break;
0869 case CS8:
0870 default:
0871 data_bits = MX_WORDLENGTH_8;
0872 break;
0873 }
0874
0875
0876 if (C_PARENB(tty)) {
0877 if (C_CMSPAR(tty)) {
0878 if (C_PARODD(tty))
0879 parity = MX_PARITY_MARK;
0880 else
0881 parity = MX_PARITY_SPACE;
0882 } else {
0883 if (C_PARODD(tty))
0884 parity = MX_PARITY_ODD;
0885 else
0886 parity = MX_PARITY_EVEN;
0887 }
0888 } else {
0889 parity = MX_PARITY_NONE;
0890 }
0891
0892
0893 if (C_CSTOPB(tty))
0894 stop_bits = MX_STOP_BITS_2;
0895 else
0896 stop_bits = MX_STOP_BITS_1;
0897
0898 buf[0] = data_bits;
0899 buf[1] = parity;
0900 buf[2] = stop_bits;
0901 buf[3] = 0;
0902
0903 err = mxuport_send_ctrl_data_urb(serial, RQ_VENDOR_SET_LINE,
0904 0, port->port_number, buf, 4);
0905 if (err)
0906 goto out;
0907
0908 err = mxuport_set_termios_flow(tty, old_termios, port, serial);
0909 if (err)
0910 goto out;
0911
0912 baud = tty_get_baud_rate(tty);
0913 if (!baud)
0914 baud = 9600;
0915
0916
0917 put_unaligned_le32(baud, buf);
0918
0919 err = mxuport_send_ctrl_data_urb(serial, RQ_VENDOR_SET_BAUD,
0920 0, port->port_number,
0921 buf, 4);
0922 if (err)
0923 goto out;
0924
0925 dev_dbg(&port->dev, "baud_rate : %d\n", baud);
0926 dev_dbg(&port->dev, "data_bits : %d\n", data_bits);
0927 dev_dbg(&port->dev, "parity : %d\n", parity);
0928 dev_dbg(&port->dev, "stop_bits : %d\n", stop_bits);
0929
0930 out:
0931 kfree(buf);
0932 }
0933
0934
0935
0936
0937
0938 static int mxuport_calc_num_ports(struct usb_serial *serial,
0939 struct usb_serial_endpoints *epds)
0940 {
0941 unsigned long features = (unsigned long)usb_get_serial_data(serial);
0942 int num_ports;
0943 int i;
0944
0945 if (features & MX_UPORT_2_PORT) {
0946 num_ports = 2;
0947 } else if (features & MX_UPORT_4_PORT) {
0948 num_ports = 4;
0949 } else if (features & MX_UPORT_8_PORT) {
0950 num_ports = 8;
0951 } else if (features & MX_UPORT_16_PORT) {
0952 num_ports = 16;
0953 } else {
0954 dev_warn(&serial->interface->dev,
0955 "unknown device, assuming two ports\n");
0956 num_ports = 2;
0957 }
0958
0959
0960
0961
0962
0963 BUILD_BUG_ON(ARRAY_SIZE(epds->bulk_out) < 16);
0964
0965 for (i = 1; i < num_ports; ++i)
0966 epds->bulk_out[i] = epds->bulk_out[0];
0967
0968 epds->num_bulk_out = num_ports;
0969
0970 return num_ports;
0971 }
0972
0973
0974 static int mxuport_get_fw_version(struct usb_serial *serial, u32 *version)
0975 {
0976 u8 *ver_buf;
0977 int err;
0978
0979 ver_buf = kzalloc(4, GFP_KERNEL);
0980 if (!ver_buf)
0981 return -ENOMEM;
0982
0983
0984 err = mxuport_recv_ctrl_urb(serial, RQ_VENDOR_GET_VERSION, 0, 0,
0985 ver_buf, 4);
0986 if (err != 4) {
0987 err = -EIO;
0988 goto out;
0989 }
0990
0991 *version = (ver_buf[0] << 16) | (ver_buf[1] << 8) | ver_buf[2];
0992 err = 0;
0993 out:
0994 kfree(ver_buf);
0995 return err;
0996 }
0997
0998
0999 static int mxuport_download_fw(struct usb_serial *serial,
1000 const struct firmware *fw_p)
1001 {
1002 u8 *fw_buf;
1003 size_t txlen;
1004 size_t fwidx;
1005 int err;
1006
1007 fw_buf = kmalloc(DOWN_BLOCK_SIZE, GFP_KERNEL);
1008 if (!fw_buf)
1009 return -ENOMEM;
1010
1011 dev_dbg(&serial->interface->dev, "Starting firmware download...\n");
1012 err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_START_FW_DOWN, 0, 0);
1013 if (err)
1014 goto out;
1015
1016 fwidx = 0;
1017 do {
1018 txlen = min_t(size_t, (fw_p->size - fwidx), DOWN_BLOCK_SIZE);
1019
1020 memcpy(fw_buf, &fw_p->data[fwidx], txlen);
1021 err = mxuport_send_ctrl_data_urb(serial, RQ_VENDOR_FW_DATA,
1022 0, 0, fw_buf, txlen);
1023 if (err) {
1024 mxuport_send_ctrl_urb(serial, RQ_VENDOR_STOP_FW_DOWN,
1025 0, 0);
1026 goto out;
1027 }
1028
1029 fwidx += txlen;
1030 usleep_range(1000, 2000);
1031
1032 } while (fwidx < fw_p->size);
1033
1034 msleep(1000);
1035 err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_STOP_FW_DOWN, 0, 0);
1036 if (err)
1037 goto out;
1038
1039 msleep(1000);
1040 err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_QUERY_FW_READY, 0, 0);
1041
1042 out:
1043 kfree(fw_buf);
1044 return err;
1045 }
1046
1047 static int mxuport_probe(struct usb_serial *serial,
1048 const struct usb_device_id *id)
1049 {
1050 u16 productid = le16_to_cpu(serial->dev->descriptor.idProduct);
1051 const struct firmware *fw_p = NULL;
1052 u32 version;
1053 int local_ver;
1054 char buf[32];
1055 int err;
1056
1057
1058 err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_QUERY_FW_CONFIG, 0, 0);
1059 if (err) {
1060 mxuport_send_ctrl_urb(serial, RQ_VENDOR_RESET_DEVICE, 0, 0);
1061 return err;
1062 }
1063
1064 err = mxuport_get_fw_version(serial, &version);
1065 if (err < 0)
1066 return err;
1067
1068 dev_dbg(&serial->interface->dev, "Device firmware version v%x.%x.%x\n",
1069 (version & 0xff0000) >> 16,
1070 (version & 0xff00) >> 8,
1071 (version & 0xff));
1072
1073 snprintf(buf, sizeof(buf) - 1, "moxa/moxa-%04x.fw", productid);
1074
1075 err = request_firmware(&fw_p, buf, &serial->interface->dev);
1076 if (err) {
1077 dev_warn(&serial->interface->dev, "Firmware %s not found\n",
1078 buf);
1079
1080
1081 err = 0;
1082 } else {
1083 local_ver = ((fw_p->data[VER_ADDR_1] << 16) |
1084 (fw_p->data[VER_ADDR_2] << 8) |
1085 fw_p->data[VER_ADDR_3]);
1086 dev_dbg(&serial->interface->dev,
1087 "Available firmware version v%x.%x.%x\n",
1088 fw_p->data[VER_ADDR_1], fw_p->data[VER_ADDR_2],
1089 fw_p->data[VER_ADDR_3]);
1090 if (local_ver > version) {
1091 err = mxuport_download_fw(serial, fw_p);
1092 if (err)
1093 goto out;
1094 err = mxuport_get_fw_version(serial, &version);
1095 if (err < 0)
1096 goto out;
1097 }
1098 }
1099
1100 dev_info(&serial->interface->dev,
1101 "Using device firmware version v%x.%x.%x\n",
1102 (version & 0xff0000) >> 16,
1103 (version & 0xff00) >> 8,
1104 (version & 0xff));
1105
1106
1107
1108
1109
1110 usb_set_serial_data(serial, (void *)id->driver_info);
1111 out:
1112 if (fw_p)
1113 release_firmware(fw_p);
1114 return err;
1115 }
1116
1117
1118 static int mxuport_port_probe(struct usb_serial_port *port)
1119 {
1120 struct usb_serial *serial = port->serial;
1121 struct mxuport_port *mxport;
1122 int err;
1123
1124 mxport = devm_kzalloc(&port->dev, sizeof(struct mxuport_port),
1125 GFP_KERNEL);
1126 if (!mxport)
1127 return -ENOMEM;
1128
1129 mutex_init(&mxport->mutex);
1130 spin_lock_init(&mxport->spinlock);
1131
1132
1133 usb_set_serial_port_data(port, mxport);
1134
1135
1136 err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_FIFO_DISABLE,
1137 0, port->port_number);
1138 if (err)
1139 return err;
1140
1141
1142 err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_HIGH_PERFOR,
1143 0, port->port_number);
1144 if (err)
1145 return err;
1146
1147
1148 return mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_INTERFACE,
1149 MX_INT_RS232,
1150 port->port_number);
1151 }
1152
1153 static int mxuport_attach(struct usb_serial *serial)
1154 {
1155 struct usb_serial_port *port0 = serial->port[0];
1156 struct usb_serial_port *port1 = serial->port[1];
1157 int err;
1158
1159
1160
1161
1162
1163
1164
1165
1166 err = usb_serial_generic_submit_read_urbs(port0, GFP_KERNEL);
1167 if (err)
1168 return err;
1169
1170 err = usb_serial_generic_submit_read_urbs(port1, GFP_KERNEL);
1171 if (err) {
1172 usb_serial_generic_close(port0);
1173 return err;
1174 }
1175
1176 return 0;
1177 }
1178
1179 static void mxuport_release(struct usb_serial *serial)
1180 {
1181 struct usb_serial_port *port0 = serial->port[0];
1182 struct usb_serial_port *port1 = serial->port[1];
1183
1184 usb_serial_generic_close(port1);
1185 usb_serial_generic_close(port0);
1186 }
1187
1188 static int mxuport_open(struct tty_struct *tty, struct usb_serial_port *port)
1189 {
1190 struct mxuport_port *mxport = usb_get_serial_port_data(port);
1191 struct usb_serial *serial = port->serial;
1192 int err;
1193
1194
1195 err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN,
1196 1, port->port_number);
1197 if (err)
1198 return err;
1199
1200 err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_OPEN,
1201 1, port->port_number);
1202 if (err) {
1203 mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN,
1204 0, port->port_number);
1205 return err;
1206 }
1207
1208
1209 if (tty)
1210 mxuport_set_termios(tty, port, NULL);
1211
1212
1213
1214
1215
1216 mxport->msr_state = 0;
1217
1218 return err;
1219 }
1220
1221 static void mxuport_close(struct usb_serial_port *port)
1222 {
1223 struct usb_serial *serial = port->serial;
1224
1225 mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_OPEN, 0,
1226 port->port_number);
1227
1228 mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN, 0,
1229 port->port_number);
1230 }
1231
1232
1233 static void mxuport_break_ctl(struct tty_struct *tty, int break_state)
1234 {
1235 struct usb_serial_port *port = tty->driver_data;
1236 struct usb_serial *serial = port->serial;
1237 int enable;
1238
1239 if (break_state == -1) {
1240 enable = 1;
1241 dev_dbg(&port->dev, "%s - sending break\n", __func__);
1242 } else {
1243 enable = 0;
1244 dev_dbg(&port->dev, "%s - clearing break\n", __func__);
1245 }
1246
1247 mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_BREAK,
1248 enable, port->port_number);
1249 }
1250
1251 static int mxuport_resume(struct usb_serial *serial)
1252 {
1253 struct usb_serial_port *port;
1254 int c = 0;
1255 int i;
1256 int r;
1257
1258 for (i = 0; i < 2; i++) {
1259 port = serial->port[i];
1260
1261 r = usb_serial_generic_submit_read_urbs(port, GFP_NOIO);
1262 if (r < 0)
1263 c++;
1264 }
1265
1266 for (i = 0; i < serial->num_ports; i++) {
1267 port = serial->port[i];
1268 if (!tty_port_initialized(&port->port))
1269 continue;
1270
1271 r = usb_serial_generic_write_start(port, GFP_NOIO);
1272 if (r < 0)
1273 c++;
1274 }
1275
1276 return c ? -EIO : 0;
1277 }
1278
1279 static struct usb_serial_driver mxuport_device = {
1280 .driver = {
1281 .owner = THIS_MODULE,
1282 .name = "mxuport",
1283 },
1284 .description = "MOXA UPort",
1285 .id_table = mxuport_idtable,
1286 .num_bulk_in = 2,
1287 .num_bulk_out = 1,
1288 .probe = mxuport_probe,
1289 .port_probe = mxuport_port_probe,
1290 .attach = mxuport_attach,
1291 .release = mxuport_release,
1292 .calc_num_ports = mxuport_calc_num_ports,
1293 .open = mxuport_open,
1294 .close = mxuport_close,
1295 .set_termios = mxuport_set_termios,
1296 .break_ctl = mxuport_break_ctl,
1297 .tx_empty = mxuport_tx_empty,
1298 .tiocmiwait = usb_serial_generic_tiocmiwait,
1299 .get_icount = usb_serial_generic_get_icount,
1300 .throttle = mxuport_throttle,
1301 .unthrottle = mxuport_unthrottle,
1302 .tiocmget = mxuport_tiocmget,
1303 .tiocmset = mxuport_tiocmset,
1304 .dtr_rts = mxuport_dtr_rts,
1305 .process_read_urb = mxuport_process_read_urb,
1306 .prepare_write_buffer = mxuport_prepare_write_buffer,
1307 .resume = mxuport_resume,
1308 };
1309
1310 static struct usb_serial_driver *const serial_drivers[] = {
1311 &mxuport_device, NULL
1312 };
1313
1314 module_usb_serial_driver(serial_drivers, mxuport_idtable);
1315
1316 MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch>");
1317 MODULE_AUTHOR("<support@moxa.com>");
1318 MODULE_LICENSE("GPL");