1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/firmware.h>
22#include <linux/jiffies.h>
23#include <linux/serial.h>
24#include <linux/serial_reg.h>
25#include <linux/slab.h>
26#include <linux/tty.h>
27#include <linux/tty_driver.h>
28#include <linux/tty_flip.h>
29#include <linux/uaccess.h>
30#include <linux/usb.h>
31#include <linux/usb/serial.h>
32#include <asm/unaligned.h>
33
34
35#define MX_USBSERIAL_VID 0x110A
36#define MX_UPORT1250_PID 0x1250
37#define MX_UPORT1251_PID 0x1251
38#define MX_UPORT1410_PID 0x1410
39#define MX_UPORT1450_PID 0x1450
40#define MX_UPORT1451_PID 0x1451
41#define MX_UPORT1618_PID 0x1618
42#define MX_UPORT1658_PID 0x1658
43#define MX_UPORT1613_PID 0x1613
44#define MX_UPORT1653_PID 0x1653
45
46
47#define HEADER_SIZE 4
48#define EVENT_LENGTH 8
49#define DOWN_BLOCK_SIZE 64
50
51
52#define VER_ADDR_1 0x20
53#define VER_ADDR_2 0x24
54#define VER_ADDR_3 0x28
55
56
57#define RQ_VENDOR_NONE 0x00
58#define RQ_VENDOR_SET_BAUD 0x01
59#define RQ_VENDOR_SET_LINE 0x02
60#define RQ_VENDOR_SET_CHARS 0x03
61#define RQ_VENDOR_SET_RTS 0x04
62#define RQ_VENDOR_SET_DTR 0x05
63#define RQ_VENDOR_SET_XONXOFF 0x06
64#define RQ_VENDOR_SET_RX_HOST_EN 0x07
65#define RQ_VENDOR_SET_OPEN 0x08
66#define RQ_VENDOR_PURGE 0x09
67#define RQ_VENDOR_SET_MCR 0x0A
68#define RQ_VENDOR_SET_BREAK 0x0B
69
70#define RQ_VENDOR_START_FW_DOWN 0x0C
71#define RQ_VENDOR_STOP_FW_DOWN 0x0D
72#define RQ_VENDOR_QUERY_FW_READY 0x0E
73
74#define RQ_VENDOR_SET_FIFO_DISABLE 0x0F
75#define RQ_VENDOR_SET_INTERFACE 0x10
76#define RQ_VENDOR_SET_HIGH_PERFOR 0x11
77
78#define RQ_VENDOR_ERASE_BLOCK 0x12
79#define RQ_VENDOR_WRITE_PAGE 0x13
80#define RQ_VENDOR_PREPARE_WRITE 0x14
81#define RQ_VENDOR_CONFIRM_WRITE 0x15
82#define RQ_VENDOR_LOCATE 0x16
83
84#define RQ_VENDOR_START_ROM_DOWN 0x17
85#define RQ_VENDOR_ROM_DATA 0x18
86#define RQ_VENDOR_STOP_ROM_DOWN 0x19
87#define RQ_VENDOR_FW_DATA 0x20
88
89#define RQ_VENDOR_RESET_DEVICE 0x23
90#define RQ_VENDOR_QUERY_FW_CONFIG 0x24
91
92#define RQ_VENDOR_GET_VERSION 0x81
93#define RQ_VENDOR_GET_PAGE 0x82
94#define RQ_VENDOR_GET_ROM_PROC 0x83
95
96#define RQ_VENDOR_GET_INQUEUE 0x84
97#define RQ_VENDOR_GET_OUTQUEUE 0x85
98
99#define RQ_VENDOR_GET_MSR 0x86
100
101
102#define UPORT_EVENT_NONE 0
103#define UPORT_EVENT_TXBUF_THRESHOLD 1
104#define UPORT_EVENT_SEND_NEXT 2
105#define UPORT_EVENT_MSR 3
106#define UPORT_EVENT_LSR 4
107#define UPORT_EVENT_MCR 5
108
109
110#define SERIAL_EV_CTS 0x0008
111#define SERIAL_EV_DSR 0x0010
112#define SERIAL_EV_RLSD 0x0020
113
114
115#define SERIAL_EV_XOFF 0x40
116
117
118#define MX_WORDLENGTH_5 5
119#define MX_WORDLENGTH_6 6
120#define MX_WORDLENGTH_7 7
121#define MX_WORDLENGTH_8 8
122
123#define MX_PARITY_NONE 0
124#define MX_PARITY_ODD 1
125#define MX_PARITY_EVEN 2
126#define MX_PARITY_MARK 3
127#define MX_PARITY_SPACE 4
128
129#define MX_STOP_BITS_1 0
130#define MX_STOP_BITS_1_5 1
131#define MX_STOP_BITS_2 2
132
133#define MX_RTS_DISABLE 0x0
134#define MX_RTS_ENABLE 0x1
135#define MX_RTS_HW 0x2
136#define MX_RTS_NO_CHANGE 0x3
137
138#define MX_INT_RS232 0
139#define MX_INT_2W_RS485 1
140#define MX_INT_RS422 2
141#define MX_INT_4W_RS485 3
142
143
144#define MX_WAIT_FOR_CTS 0x0001
145#define MX_WAIT_FOR_DSR 0x0002
146#define MX_WAIT_FOR_DCD 0x0004
147#define MX_WAIT_FOR_XON 0x0008
148#define MX_WAIT_FOR_START_TX 0x0010
149#define MX_WAIT_FOR_UNTHROTTLE 0x0020
150#define MX_WAIT_FOR_LOW_WATER 0x0040
151#define MX_WAIT_FOR_SEND_NEXT 0x0080
152
153#define MX_UPORT_2_PORT BIT(0)
154#define MX_UPORT_4_PORT BIT(1)
155#define MX_UPORT_8_PORT BIT(2)
156#define MX_UPORT_16_PORT BIT(3)
157
158
159struct mxuport_port {
160 u8 mcr_state;
161 u8 msr_state;
162 struct mutex mutex;
163 spinlock_t spinlock;
164};
165
166
167static const struct usb_device_id mxuport_idtable[] = {
168 { USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1250_PID),
169 .driver_info = MX_UPORT_2_PORT },
170 { USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1251_PID),
171 .driver_info = MX_UPORT_2_PORT },
172 { USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1410_PID),
173 .driver_info = MX_UPORT_4_PORT },
174 { USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1450_PID),
175 .driver_info = MX_UPORT_4_PORT },
176 { USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1451_PID),
177 .driver_info = MX_UPORT_4_PORT },
178 { USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1618_PID),
179 .driver_info = MX_UPORT_8_PORT },
180 { USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1658_PID),
181 .driver_info = MX_UPORT_8_PORT },
182 { USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1613_PID),
183 .driver_info = MX_UPORT_16_PORT },
184 { USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1653_PID),
185 .driver_info = MX_UPORT_16_PORT },
186 {}
187};
188
189MODULE_DEVICE_TABLE(usb, mxuport_idtable);
190
191
192
193
194
195
196static int mxuport_prepare_write_buffer(struct usb_serial_port *port,
197 void *dest, size_t size)
198{
199 u8 *buf = dest;
200 int count;
201
202 count = kfifo_out_locked(&port->write_fifo, buf + HEADER_SIZE,
203 size - HEADER_SIZE,
204 &port->lock);
205
206 put_unaligned_be16(port->port_number, buf);
207 put_unaligned_be16(count, buf + 2);
208
209 dev_dbg(&port->dev, "%s - size %zd count %d\n", __func__,
210 size, count);
211
212 return count + HEADER_SIZE;
213}
214
215
216static int mxuport_recv_ctrl_urb(struct usb_serial *serial,
217 u8 request, u16 value, u16 index,
218 u8 *data, size_t size)
219{
220 int status;
221
222 status = usb_control_msg(serial->dev,
223 usb_rcvctrlpipe(serial->dev, 0),
224 request,
225 (USB_DIR_IN | USB_TYPE_VENDOR |
226 USB_RECIP_DEVICE), value, index,
227 data, size,
228 USB_CTRL_GET_TIMEOUT);
229 if (status < 0) {
230 dev_err(&serial->interface->dev,
231 "%s - usb_control_msg failed (%d)\n",
232 __func__, status);
233 return status;
234 }
235
236 if (status != size) {
237 dev_err(&serial->interface->dev,
238 "%s - short read (%d / %zd)\n",
239 __func__, status, size);
240 return -EIO;
241 }
242
243 return status;
244}
245
246
247static int mxuport_send_ctrl_data_urb(struct usb_serial *serial,
248 u8 request,
249 u16 value, u16 index,
250 u8 *data, size_t size)
251{
252 int status;
253
254 status = usb_control_msg(serial->dev,
255 usb_sndctrlpipe(serial->dev, 0),
256 request,
257 (USB_DIR_OUT | USB_TYPE_VENDOR |
258 USB_RECIP_DEVICE), value, index,
259 data, size,
260 USB_CTRL_SET_TIMEOUT);
261 if (status < 0) {
262 dev_err(&serial->interface->dev,
263 "%s - usb_control_msg failed (%d)\n",
264 __func__, status);
265 return status;
266 }
267
268 if (status != size) {
269 dev_err(&serial->interface->dev,
270 "%s - short write (%d / %zd)\n",
271 __func__, status, size);
272 return -EIO;
273 }
274
275 return 0;
276}
277
278
279static int mxuport_send_ctrl_urb(struct usb_serial *serial,
280 u8 request, u16 value, u16 index)
281{
282 return mxuport_send_ctrl_data_urb(serial, request, value, index,
283 NULL, 0);
284}
285
286
287
288
289
290
291
292
293
294
295static void mxuport_throttle(struct tty_struct *tty)
296{
297 struct usb_serial_port *port = tty->driver_data;
298 struct usb_serial *serial = port->serial;
299
300 dev_dbg(&port->dev, "%s\n", __func__);
301
302 mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN,
303 0, port->port_number);
304}
305
306
307
308
309
310
311
312
313static void mxuport_unthrottle(struct tty_struct *tty)
314{
315
316 struct usb_serial_port *port = tty->driver_data;
317 struct usb_serial *serial = port->serial;
318
319 dev_dbg(&port->dev, "%s\n", __func__);
320
321 mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN,
322 1, port->port_number);
323}
324
325
326
327
328
329static void mxuport_process_read_urb_data(struct usb_serial_port *port,
330 char *data, int size)
331{
332 int i;
333
334 if (!port->port.console || !port->sysrq) {
335 tty_insert_flip_string(&port->port, data, size);
336 } else {
337 for (i = 0; i < size; i++, data++) {
338 if (!usb_serial_handle_sysrq_char(port, *data))
339 tty_insert_flip_char(&port->port, *data,
340 TTY_NORMAL);
341 }
342 }
343 tty_flip_buffer_push(&port->port);
344}
345
346static void mxuport_msr_event(struct usb_serial_port *port, u8 buf[4])
347{
348 struct mxuport_port *mxport = usb_get_serial_port_data(port);
349 u8 rcv_msr_hold = buf[2] & 0xF0;
350 u16 rcv_msr_event = get_unaligned_be16(buf);
351 unsigned long flags;
352
353 if (rcv_msr_event == 0)
354 return;
355
356
357 spin_lock_irqsave(&mxport->spinlock, flags);
358
359 dev_dbg(&port->dev, "%s - current MSR status = 0x%x\n",
360 __func__, mxport->msr_state);
361
362 if (rcv_msr_hold & UART_MSR_CTS) {
363 mxport->msr_state |= UART_MSR_CTS;
364 dev_dbg(&port->dev, "%s - CTS high\n", __func__);
365 } else {
366 mxport->msr_state &= ~UART_MSR_CTS;
367 dev_dbg(&port->dev, "%s - CTS low\n", __func__);
368 }
369
370 if (rcv_msr_hold & UART_MSR_DSR) {
371 mxport->msr_state |= UART_MSR_DSR;
372 dev_dbg(&port->dev, "%s - DSR high\n", __func__);
373 } else {
374 mxport->msr_state &= ~UART_MSR_DSR;
375 dev_dbg(&port->dev, "%s - DSR low\n", __func__);
376 }
377
378 if (rcv_msr_hold & UART_MSR_DCD) {
379 mxport->msr_state |= UART_MSR_DCD;
380 dev_dbg(&port->dev, "%s - DCD high\n", __func__);
381 } else {
382 mxport->msr_state &= ~UART_MSR_DCD;
383 dev_dbg(&port->dev, "%s - DCD low\n", __func__);
384 }
385 spin_unlock_irqrestore(&mxport->spinlock, flags);
386
387 if (rcv_msr_event &
388 (SERIAL_EV_CTS | SERIAL_EV_DSR | SERIAL_EV_RLSD)) {
389
390 if (rcv_msr_event & SERIAL_EV_CTS) {
391 port->icount.cts++;
392 dev_dbg(&port->dev, "%s - CTS change\n", __func__);
393 }
394
395 if (rcv_msr_event & SERIAL_EV_DSR) {
396 port->icount.dsr++;
397 dev_dbg(&port->dev, "%s - DSR change\n", __func__);
398 }
399
400 if (rcv_msr_event & SERIAL_EV_RLSD) {
401 port->icount.dcd++;
402 dev_dbg(&port->dev, "%s - DCD change\n", __func__);
403 }
404 wake_up_interruptible(&port->port.delta_msr_wait);
405 }
406}
407
408static void mxuport_lsr_event(struct usb_serial_port *port, u8 buf[4])
409{
410 u8 lsr_event = buf[2];
411
412 if (lsr_event & UART_LSR_BI) {
413 port->icount.brk++;
414 dev_dbg(&port->dev, "%s - break error\n", __func__);
415 }
416
417 if (lsr_event & UART_LSR_FE) {
418 port->icount.frame++;
419 dev_dbg(&port->dev, "%s - frame error\n", __func__);
420 }
421
422 if (lsr_event & UART_LSR_PE) {
423 port->icount.parity++;
424 dev_dbg(&port->dev, "%s - parity error\n", __func__);
425 }
426
427 if (lsr_event & UART_LSR_OE) {
428 port->icount.overrun++;
429 dev_dbg(&port->dev, "%s - overrun error\n", __func__);
430 }
431}
432
433
434
435
436
437static void mxuport_process_read_urb_event(struct usb_serial_port *port,
438 u8 buf[4], u32 event)
439{
440 dev_dbg(&port->dev, "%s - receive event : %04x\n", __func__, event);
441
442 switch (event) {
443 case UPORT_EVENT_SEND_NEXT:
444
445
446
447
448 break;
449 case UPORT_EVENT_MSR:
450 mxuport_msr_event(port, buf);
451 break;
452 case UPORT_EVENT_LSR:
453 mxuport_lsr_event(port, buf);
454 break;
455 case UPORT_EVENT_MCR:
456
457
458
459
460
461
462
463 break;
464 default:
465 dev_dbg(&port->dev, "Unexpected event\n");
466 break;
467 }
468}
469
470
471
472
473
474static void mxuport_process_read_urb_demux_data(struct urb *urb)
475{
476 struct usb_serial_port *port = urb->context;
477 struct usb_serial *serial = port->serial;
478 u8 *data = urb->transfer_buffer;
479 u8 *end = data + urb->actual_length;
480 struct usb_serial_port *demux_port;
481 u8 *ch;
482 u16 rcv_port;
483 u16 rcv_len;
484
485 while (data < end) {
486 if (data + HEADER_SIZE > end) {
487 dev_warn(&port->dev, "%s - message with short header\n",
488 __func__);
489 return;
490 }
491
492 rcv_port = get_unaligned_be16(data);
493 if (rcv_port >= serial->num_ports) {
494 dev_warn(&port->dev, "%s - message for invalid port\n",
495 __func__);
496 return;
497 }
498
499 demux_port = serial->port[rcv_port];
500 rcv_len = get_unaligned_be16(data + 2);
501 if (!rcv_len || data + HEADER_SIZE + rcv_len > end) {
502 dev_warn(&port->dev, "%s - short data\n", __func__);
503 return;
504 }
505
506 if (tty_port_initialized(&demux_port->port)) {
507 ch = data + HEADER_SIZE;
508 mxuport_process_read_urb_data(demux_port, ch, rcv_len);
509 } else {
510 dev_dbg(&demux_port->dev, "%s - data for closed port\n",
511 __func__);
512 }
513 data += HEADER_SIZE + rcv_len;
514 }
515}
516
517
518
519
520
521static void mxuport_process_read_urb_demux_event(struct urb *urb)
522{
523 struct usb_serial_port *port = urb->context;
524 struct usb_serial *serial = port->serial;
525 u8 *data = urb->transfer_buffer;
526 u8 *end = data + urb->actual_length;
527 struct usb_serial_port *demux_port;
528 u8 *ch;
529 u16 rcv_port;
530 u16 rcv_event;
531
532 while (data < end) {
533 if (data + EVENT_LENGTH > end) {
534 dev_warn(&port->dev, "%s - message with short event\n",
535 __func__);
536 return;
537 }
538
539 rcv_port = get_unaligned_be16(data);
540 if (rcv_port >= serial->num_ports) {
541 dev_warn(&port->dev, "%s - message for invalid port\n",
542 __func__);
543 return;
544 }
545
546 demux_port = serial->port[rcv_port];
547 if (tty_port_initialized(&demux_port->port)) {
548 ch = data + HEADER_SIZE;
549 rcv_event = get_unaligned_be16(data + 2);
550 mxuport_process_read_urb_event(demux_port, ch,
551 rcv_event);
552 } else {
553 dev_dbg(&demux_port->dev,
554 "%s - event for closed port\n", __func__);
555 }
556 data += EVENT_LENGTH;
557 }
558}
559
560
561
562
563
564
565static void mxuport_process_read_urb(struct urb *urb)
566{
567 struct usb_serial_port *port = urb->context;
568 struct usb_serial *serial = port->serial;
569
570 if (port == serial->port[0])
571 mxuport_process_read_urb_demux_data(urb);
572
573 if (port == serial->port[1])
574 mxuport_process_read_urb_demux_event(urb);
575}
576
577
578
579
580
581static bool mxuport_tx_empty(struct usb_serial_port *port)
582{
583 struct usb_serial *serial = port->serial;
584 bool is_empty = true;
585 u32 txlen;
586 u8 *len_buf;
587 int err;
588
589 len_buf = kzalloc(4, GFP_KERNEL);
590 if (!len_buf)
591 goto out;
592
593 err = mxuport_recv_ctrl_urb(serial, RQ_VENDOR_GET_OUTQUEUE, 0,
594 port->port_number, len_buf, 4);
595 if (err < 0)
596 goto out;
597
598 txlen = get_unaligned_be32(len_buf);
599 dev_dbg(&port->dev, "%s - tx len = %u\n", __func__, txlen);
600
601 if (txlen != 0)
602 is_empty = false;
603
604out:
605 kfree(len_buf);
606 return is_empty;
607}
608
609static int mxuport_set_mcr(struct usb_serial_port *port, u8 mcr_state)
610{
611 struct usb_serial *serial = port->serial;
612 int err;
613
614 dev_dbg(&port->dev, "%s - %02x\n", __func__, mcr_state);
615
616 err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_MCR,
617 mcr_state, port->port_number);
618 if (err)
619 dev_err(&port->dev, "%s - failed to change MCR\n", __func__);
620
621 return err;
622}
623
624static int mxuport_set_dtr(struct usb_serial_port *port, int on)
625{
626 struct mxuport_port *mxport = usb_get_serial_port_data(port);
627 struct usb_serial *serial = port->serial;
628 int err;
629
630 mutex_lock(&mxport->mutex);
631
632 err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_DTR,
633 !!on, port->port_number);
634 if (!err) {
635 if (on)
636 mxport->mcr_state |= UART_MCR_DTR;
637 else
638 mxport->mcr_state &= ~UART_MCR_DTR;
639 }
640
641 mutex_unlock(&mxport->mutex);
642
643 return err;
644}
645
646static int mxuport_set_rts(struct usb_serial_port *port, u8 state)
647{
648 struct mxuport_port *mxport = usb_get_serial_port_data(port);
649 struct usb_serial *serial = port->serial;
650 int err;
651 u8 mcr_state;
652
653 mutex_lock(&mxport->mutex);
654 mcr_state = mxport->mcr_state;
655
656 switch (state) {
657 case MX_RTS_DISABLE:
658 mcr_state &= ~UART_MCR_RTS;
659 break;
660 case MX_RTS_ENABLE:
661 mcr_state |= UART_MCR_RTS;
662 break;
663 case MX_RTS_HW:
664
665
666
667
668 break;
669 default:
670
671
672
673
674 err = -EINVAL;
675 goto out;
676 }
677 err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RTS,
678 state, port->port_number);
679 if (!err)
680 mxport->mcr_state = mcr_state;
681
682out:
683 mutex_unlock(&mxport->mutex);
684
685 return err;
686}
687
688static void mxuport_dtr_rts(struct usb_serial_port *port, int on)
689{
690 struct mxuport_port *mxport = usb_get_serial_port_data(port);
691 u8 mcr_state;
692 int err;
693
694 mutex_lock(&mxport->mutex);
695 mcr_state = mxport->mcr_state;
696
697 if (on)
698 mcr_state |= (UART_MCR_RTS | UART_MCR_DTR);
699 else
700 mcr_state &= ~(UART_MCR_RTS | UART_MCR_DTR);
701
702 err = mxuport_set_mcr(port, mcr_state);
703 if (!err)
704 mxport->mcr_state = mcr_state;
705
706 mutex_unlock(&mxport->mutex);
707}
708
709static int mxuport_tiocmset(struct tty_struct *tty, unsigned int set,
710 unsigned int clear)
711{
712 struct usb_serial_port *port = tty->driver_data;
713 struct mxuport_port *mxport = usb_get_serial_port_data(port);
714 int err;
715 u8 mcr_state;
716
717 mutex_lock(&mxport->mutex);
718 mcr_state = mxport->mcr_state;
719
720 if (set & TIOCM_RTS)
721 mcr_state |= UART_MCR_RTS;
722
723 if (set & TIOCM_DTR)
724 mcr_state |= UART_MCR_DTR;
725
726 if (clear & TIOCM_RTS)
727 mcr_state &= ~UART_MCR_RTS;
728
729 if (clear & TIOCM_DTR)
730 mcr_state &= ~UART_MCR_DTR;
731
732 err = mxuport_set_mcr(port, mcr_state);
733 if (!err)
734 mxport->mcr_state = mcr_state;
735
736 mutex_unlock(&mxport->mutex);
737
738 return err;
739}
740
741static int mxuport_tiocmget(struct tty_struct *tty)
742{
743 struct mxuport_port *mxport;
744 struct usb_serial_port *port = tty->driver_data;
745 unsigned int result;
746 unsigned long flags;
747 unsigned int msr;
748 unsigned int mcr;
749
750 mxport = usb_get_serial_port_data(port);
751
752 mutex_lock(&mxport->mutex);
753 spin_lock_irqsave(&mxport->spinlock, flags);
754
755 msr = mxport->msr_state;
756 mcr = mxport->mcr_state;
757
758 spin_unlock_irqrestore(&mxport->spinlock, flags);
759 mutex_unlock(&mxport->mutex);
760
761 result = (((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) |
762 ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) |
763 ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) |
764 ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) |
765 ((msr & UART_MSR_RI) ? TIOCM_RI : 0) |
766 ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0));
767
768 dev_dbg(&port->dev, "%s - 0x%04x\n", __func__, result);
769
770 return result;
771}
772
773static int mxuport_set_termios_flow(struct tty_struct *tty,
774 struct ktermios *old_termios,
775 struct usb_serial_port *port,
776 struct usb_serial *serial)
777{
778 u8 xon = START_CHAR(tty);
779 u8 xoff = STOP_CHAR(tty);
780 int enable;
781 int err;
782 u8 *buf;
783 u8 rts;
784
785 buf = kmalloc(2, GFP_KERNEL);
786 if (!buf)
787 return -ENOMEM;
788
789
790 if (I_IXOFF(tty) || I_IXON(tty)) {
791 enable = 1;
792 buf[0] = xon;
793 buf[1] = xoff;
794
795 err = mxuport_send_ctrl_data_urb(serial, RQ_VENDOR_SET_CHARS,
796 0, port->port_number,
797 buf, 2);
798 if (err)
799 goto out;
800
801 dev_dbg(&port->dev, "%s - XON = 0x%02x, XOFF = 0x%02x\n",
802 __func__, xon, xoff);
803 } else {
804 enable = 0;
805 }
806
807 err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_XONXOFF,
808 enable, port->port_number);
809 if (err)
810 goto out;
811
812 rts = MX_RTS_NO_CHANGE;
813
814
815 if (!old_termios ||
816 C_CRTSCTS(tty) != (old_termios->c_cflag & CRTSCTS)) {
817 if (C_CRTSCTS(tty))
818 rts = MX_RTS_HW;
819 else
820 rts = MX_RTS_ENABLE;
821 }
822
823 if (C_BAUD(tty)) {
824 if (old_termios && (old_termios->c_cflag & CBAUD) == B0) {
825
826 if (C_CRTSCTS(tty))
827 rts = MX_RTS_HW;
828 else
829 rts = MX_RTS_ENABLE;
830 mxuport_set_dtr(port, 1);
831 }
832 } else {
833
834 rts = MX_RTS_DISABLE;
835 mxuport_set_dtr(port, 0);
836 }
837
838 if (rts != MX_RTS_NO_CHANGE)
839 err = mxuport_set_rts(port, rts);
840
841out:
842 kfree(buf);
843 return err;
844}
845
846static void mxuport_set_termios(struct tty_struct *tty,
847 struct usb_serial_port *port,
848 struct ktermios *old_termios)
849{
850 struct usb_serial *serial = port->serial;
851 u8 *buf;
852 u8 data_bits;
853 u8 stop_bits;
854 u8 parity;
855 int baud;
856 int err;
857
858 if (old_termios &&
859 !tty_termios_hw_change(&tty->termios, old_termios) &&
860 tty->termios.c_iflag == old_termios->c_iflag) {
861 dev_dbg(&port->dev, "%s - nothing to change\n", __func__);
862 return;
863 }
864
865 buf = kmalloc(4, GFP_KERNEL);
866 if (!buf)
867 return;
868
869
870 switch (C_CSIZE(tty)) {
871 case CS5:
872 data_bits = MX_WORDLENGTH_5;
873 break;
874 case CS6:
875 data_bits = MX_WORDLENGTH_6;
876 break;
877 case CS7:
878 data_bits = MX_WORDLENGTH_7;
879 break;
880 case CS8:
881 default:
882 data_bits = MX_WORDLENGTH_8;
883 break;
884 }
885
886
887 if (C_PARENB(tty)) {
888 if (C_CMSPAR(tty)) {
889 if (C_PARODD(tty))
890 parity = MX_PARITY_MARK;
891 else
892 parity = MX_PARITY_SPACE;
893 } else {
894 if (C_PARODD(tty))
895 parity = MX_PARITY_ODD;
896 else
897 parity = MX_PARITY_EVEN;
898 }
899 } else {
900 parity = MX_PARITY_NONE;
901 }
902
903
904 if (C_CSTOPB(tty))
905 stop_bits = MX_STOP_BITS_2;
906 else
907 stop_bits = MX_STOP_BITS_1;
908
909 buf[0] = data_bits;
910 buf[1] = parity;
911 buf[2] = stop_bits;
912 buf[3] = 0;
913
914 err = mxuport_send_ctrl_data_urb(serial, RQ_VENDOR_SET_LINE,
915 0, port->port_number, buf, 4);
916 if (err)
917 goto out;
918
919 err = mxuport_set_termios_flow(tty, old_termios, port, serial);
920 if (err)
921 goto out;
922
923 baud = tty_get_baud_rate(tty);
924 if (!baud)
925 baud = 9600;
926
927
928 put_unaligned_le32(baud, buf);
929
930 err = mxuport_send_ctrl_data_urb(serial, RQ_VENDOR_SET_BAUD,
931 0, port->port_number,
932 buf, 4);
933 if (err)
934 goto out;
935
936 dev_dbg(&port->dev, "baud_rate : %d\n", baud);
937 dev_dbg(&port->dev, "data_bits : %d\n", data_bits);
938 dev_dbg(&port->dev, "parity : %d\n", parity);
939 dev_dbg(&port->dev, "stop_bits : %d\n", stop_bits);
940
941out:
942 kfree(buf);
943}
944
945
946
947
948
949static int mxuport_calc_num_ports(struct usb_serial *serial,
950 struct usb_serial_endpoints *epds)
951{
952 unsigned long features = (unsigned long)usb_get_serial_data(serial);
953 int num_ports;
954 int i;
955
956 if (features & MX_UPORT_2_PORT) {
957 num_ports = 2;
958 } else if (features & MX_UPORT_4_PORT) {
959 num_ports = 4;
960 } else if (features & MX_UPORT_8_PORT) {
961 num_ports = 8;
962 } else if (features & MX_UPORT_16_PORT) {
963 num_ports = 16;
964 } else {
965 dev_warn(&serial->interface->dev,
966 "unknown device, assuming two ports\n");
967 num_ports = 2;
968 }
969
970
971
972
973
974 BUILD_BUG_ON(ARRAY_SIZE(epds->bulk_out) < 16);
975
976 for (i = 1; i < num_ports; ++i)
977 epds->bulk_out[i] = epds->bulk_out[0];
978
979 epds->num_bulk_out = num_ports;
980
981 return num_ports;
982}
983
984
985static int mxuport_get_fw_version(struct usb_serial *serial, u32 *version)
986{
987 u8 *ver_buf;
988 int err;
989
990 ver_buf = kzalloc(4, GFP_KERNEL);
991 if (!ver_buf)
992 return -ENOMEM;
993
994
995 err = mxuport_recv_ctrl_urb(serial, RQ_VENDOR_GET_VERSION, 0, 0,
996 ver_buf, 4);
997 if (err != 4) {
998 err = -EIO;
999 goto out;
1000 }
1001
1002 *version = (ver_buf[0] << 16) | (ver_buf[1] << 8) | ver_buf[2];
1003 err = 0;
1004out:
1005 kfree(ver_buf);
1006 return err;
1007}
1008
1009
1010static int mxuport_download_fw(struct usb_serial *serial,
1011 const struct firmware *fw_p)
1012{
1013 u8 *fw_buf;
1014 size_t txlen;
1015 size_t fwidx;
1016 int err;
1017
1018 fw_buf = kmalloc(DOWN_BLOCK_SIZE, GFP_KERNEL);
1019 if (!fw_buf)
1020 return -ENOMEM;
1021
1022 dev_dbg(&serial->interface->dev, "Starting firmware download...\n");
1023 err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_START_FW_DOWN, 0, 0);
1024 if (err)
1025 goto out;
1026
1027 fwidx = 0;
1028 do {
1029 txlen = min_t(size_t, (fw_p->size - fwidx), DOWN_BLOCK_SIZE);
1030
1031 memcpy(fw_buf, &fw_p->data[fwidx], txlen);
1032 err = mxuport_send_ctrl_data_urb(serial, RQ_VENDOR_FW_DATA,
1033 0, 0, fw_buf, txlen);
1034 if (err) {
1035 mxuport_send_ctrl_urb(serial, RQ_VENDOR_STOP_FW_DOWN,
1036 0, 0);
1037 goto out;
1038 }
1039
1040 fwidx += txlen;
1041 usleep_range(1000, 2000);
1042
1043 } while (fwidx < fw_p->size);
1044
1045 msleep(1000);
1046 err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_STOP_FW_DOWN, 0, 0);
1047 if (err)
1048 goto out;
1049
1050 msleep(1000);
1051 err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_QUERY_FW_READY, 0, 0);
1052
1053out:
1054 kfree(fw_buf);
1055 return err;
1056}
1057
1058static int mxuport_probe(struct usb_serial *serial,
1059 const struct usb_device_id *id)
1060{
1061 u16 productid = le16_to_cpu(serial->dev->descriptor.idProduct);
1062 const struct firmware *fw_p = NULL;
1063 u32 version;
1064 int local_ver;
1065 char buf[32];
1066 int err;
1067
1068
1069 err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_QUERY_FW_CONFIG, 0, 0);
1070 if (err) {
1071 mxuport_send_ctrl_urb(serial, RQ_VENDOR_RESET_DEVICE, 0, 0);
1072 return err;
1073 }
1074
1075 err = mxuport_get_fw_version(serial, &version);
1076 if (err < 0)
1077 return err;
1078
1079 dev_dbg(&serial->interface->dev, "Device firmware version v%x.%x.%x\n",
1080 (version & 0xff0000) >> 16,
1081 (version & 0xff00) >> 8,
1082 (version & 0xff));
1083
1084 snprintf(buf, sizeof(buf) - 1, "moxa/moxa-%04x.fw", productid);
1085
1086 err = request_firmware(&fw_p, buf, &serial->interface->dev);
1087 if (err) {
1088 dev_warn(&serial->interface->dev, "Firmware %s not found\n",
1089 buf);
1090
1091
1092 err = 0;
1093 } else {
1094 local_ver = ((fw_p->data[VER_ADDR_1] << 16) |
1095 (fw_p->data[VER_ADDR_2] << 8) |
1096 fw_p->data[VER_ADDR_3]);
1097 dev_dbg(&serial->interface->dev,
1098 "Available firmware version v%x.%x.%x\n",
1099 fw_p->data[VER_ADDR_1], fw_p->data[VER_ADDR_2],
1100 fw_p->data[VER_ADDR_3]);
1101 if (local_ver > version) {
1102 err = mxuport_download_fw(serial, fw_p);
1103 if (err)
1104 goto out;
1105 err = mxuport_get_fw_version(serial, &version);
1106 if (err < 0)
1107 goto out;
1108 }
1109 }
1110
1111 dev_info(&serial->interface->dev,
1112 "Using device firmware version v%x.%x.%x\n",
1113 (version & 0xff0000) >> 16,
1114 (version & 0xff00) >> 8,
1115 (version & 0xff));
1116
1117
1118
1119
1120
1121 usb_set_serial_data(serial, (void *)id->driver_info);
1122out:
1123 if (fw_p)
1124 release_firmware(fw_p);
1125 return err;
1126}
1127
1128
1129static int mxuport_port_probe(struct usb_serial_port *port)
1130{
1131 struct usb_serial *serial = port->serial;
1132 struct mxuport_port *mxport;
1133 int err;
1134
1135 mxport = devm_kzalloc(&port->dev, sizeof(struct mxuport_port),
1136 GFP_KERNEL);
1137 if (!mxport)
1138 return -ENOMEM;
1139
1140 mutex_init(&mxport->mutex);
1141 spin_lock_init(&mxport->spinlock);
1142
1143
1144 usb_set_serial_port_data(port, mxport);
1145
1146
1147 err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_FIFO_DISABLE,
1148 0, port->port_number);
1149 if (err)
1150 return err;
1151
1152
1153 err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_HIGH_PERFOR,
1154 0, port->port_number);
1155 if (err)
1156 return err;
1157
1158
1159 return mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_INTERFACE,
1160 MX_INT_RS232,
1161 port->port_number);
1162}
1163
1164static int mxuport_attach(struct usb_serial *serial)
1165{
1166 struct usb_serial_port *port0 = serial->port[0];
1167 struct usb_serial_port *port1 = serial->port[1];
1168 int err;
1169
1170
1171
1172
1173
1174
1175
1176
1177 err = usb_serial_generic_submit_read_urbs(port0, GFP_KERNEL);
1178 if (err)
1179 return err;
1180
1181 err = usb_serial_generic_submit_read_urbs(port1, GFP_KERNEL);
1182 if (err) {
1183 usb_serial_generic_close(port0);
1184 return err;
1185 }
1186
1187 return 0;
1188}
1189
1190static void mxuport_release(struct usb_serial *serial)
1191{
1192 struct usb_serial_port *port0 = serial->port[0];
1193 struct usb_serial_port *port1 = serial->port[1];
1194
1195 usb_serial_generic_close(port1);
1196 usb_serial_generic_close(port0);
1197}
1198
1199static int mxuport_open(struct tty_struct *tty, struct usb_serial_port *port)
1200{
1201 struct mxuport_port *mxport = usb_get_serial_port_data(port);
1202 struct usb_serial *serial = port->serial;
1203 int err;
1204
1205
1206 err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN,
1207 1, port->port_number);
1208 if (err)
1209 return err;
1210
1211 err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_OPEN,
1212 1, port->port_number);
1213 if (err) {
1214 mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN,
1215 0, port->port_number);
1216 return err;
1217 }
1218
1219
1220 if (tty)
1221 mxuport_set_termios(tty, port, NULL);
1222
1223
1224
1225
1226
1227 mxport->msr_state = 0;
1228
1229 return err;
1230}
1231
1232static void mxuport_close(struct usb_serial_port *port)
1233{
1234 struct usb_serial *serial = port->serial;
1235
1236 mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_OPEN, 0,
1237 port->port_number);
1238
1239 mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN, 0,
1240 port->port_number);
1241}
1242
1243
1244static void mxuport_break_ctl(struct tty_struct *tty, int break_state)
1245{
1246 struct usb_serial_port *port = tty->driver_data;
1247 struct usb_serial *serial = port->serial;
1248 int enable;
1249
1250 if (break_state == -1) {
1251 enable = 1;
1252 dev_dbg(&port->dev, "%s - sending break\n", __func__);
1253 } else {
1254 enable = 0;
1255 dev_dbg(&port->dev, "%s - clearing break\n", __func__);
1256 }
1257
1258 mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_BREAK,
1259 enable, port->port_number);
1260}
1261
1262static int mxuport_resume(struct usb_serial *serial)
1263{
1264 struct usb_serial_port *port;
1265 int c = 0;
1266 int i;
1267 int r;
1268
1269 for (i = 0; i < 2; i++) {
1270 port = serial->port[i];
1271
1272 r = usb_serial_generic_submit_read_urbs(port, GFP_NOIO);
1273 if (r < 0)
1274 c++;
1275 }
1276
1277 for (i = 0; i < serial->num_ports; i++) {
1278 port = serial->port[i];
1279 if (!tty_port_initialized(&port->port))
1280 continue;
1281
1282 r = usb_serial_generic_write_start(port, GFP_NOIO);
1283 if (r < 0)
1284 c++;
1285 }
1286
1287 return c ? -EIO : 0;
1288}
1289
1290static struct usb_serial_driver mxuport_device = {
1291 .driver = {
1292 .owner = THIS_MODULE,
1293 .name = "mxuport",
1294 },
1295 .description = "MOXA UPort",
1296 .id_table = mxuport_idtable,
1297 .num_bulk_in = 2,
1298 .num_bulk_out = 1,
1299 .probe = mxuport_probe,
1300 .port_probe = mxuport_port_probe,
1301 .attach = mxuport_attach,
1302 .release = mxuport_release,
1303 .calc_num_ports = mxuport_calc_num_ports,
1304 .open = mxuport_open,
1305 .close = mxuport_close,
1306 .set_termios = mxuport_set_termios,
1307 .break_ctl = mxuport_break_ctl,
1308 .tx_empty = mxuport_tx_empty,
1309 .tiocmiwait = usb_serial_generic_tiocmiwait,
1310 .get_icount = usb_serial_generic_get_icount,
1311 .throttle = mxuport_throttle,
1312 .unthrottle = mxuport_unthrottle,
1313 .tiocmget = mxuport_tiocmget,
1314 .tiocmset = mxuport_tiocmset,
1315 .dtr_rts = mxuport_dtr_rts,
1316 .process_read_urb = mxuport_process_read_urb,
1317 .prepare_write_buffer = mxuport_prepare_write_buffer,
1318 .resume = mxuport_resume,
1319};
1320
1321static struct usb_serial_driver *const serial_drivers[] = {
1322 &mxuport_device, NULL
1323};
1324
1325module_usb_serial_driver(serial_drivers, mxuport_idtable);
1326
1327MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch>");
1328MODULE_AUTHOR("<support@moxa.com>");
1329MODULE_LICENSE("GPL");
1330