]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/usb/serial/mxu11x0.c
Merge tag 'phy-for-4.5-rc' of git://git.kernel.org/pub/scm/linux/kernel/git/kishon...
[mirror_ubuntu-artful-kernel.git] / drivers / usb / serial / mxu11x0.c
1 /*
2 * 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 */
143 struct 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
173 struct 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
187 static 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 */
196 struct 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
210 struct 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
219 struct mxu1_device {
220 u16 mxd_model;
221 };
222
223 static 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
232 MODULE_DEVICE_TABLE(usb, mxu1_idtable);
233
234 /* Write the given buffer out to the control pipe. */
235 static 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 */
267 static 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
274 static 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
331 static int mxu1_port_probe(struct usb_serial_port *port)
332 {
333 struct mxu1_port *mxport;
334 struct mxu1_device *mxdev;
335
336 if (!port->interrupt_in_urb) {
337 dev_err(&port->dev, "no interrupt urb\n");
338 return -ENODEV;
339 }
340
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
350 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
371 static int mxu1_port_remove(struct usb_serial_port *port)
372 {
373 struct mxu1_port *mxport;
374
375 mxport = usb_get_serial_port_data(port);
376 kfree(mxport);
377
378 return 0;
379 }
380
381 static int mxu1_startup(struct usb_serial *serial)
382 {
383 struct mxu1_device *mxdev;
384 struct usb_device *dev = serial->dev;
385 struct usb_host_interface *cur_altsetting;
386 char fw_name[32];
387 const struct firmware *fw_p = NULL;
388 int err;
389
390 dev_dbg(&serial->interface->dev, "%s - product 0x%04X, num configurations %d, configuration value %d\n",
391 __func__, le16_to_cpu(dev->descriptor.idProduct),
392 dev->descriptor.bNumConfigurations,
393 dev->actconfig->desc.bConfigurationValue);
394
395 /* create device structure */
396 mxdev = kzalloc(sizeof(struct mxu1_device), GFP_KERNEL);
397 if (!mxdev)
398 return -ENOMEM;
399
400 usb_set_serial_data(serial, mxdev);
401
402 mxdev->mxd_model = le16_to_cpu(dev->descriptor.idProduct);
403
404 cur_altsetting = serial->interface->cur_altsetting;
405
406 /* if we have only 1 configuration, download firmware */
407 if (cur_altsetting->desc.bNumEndpoints == 1) {
408
409 snprintf(fw_name,
410 sizeof(fw_name),
411 "moxa/moxa-%04x.fw",
412 mxdev->mxd_model);
413
414 err = request_firmware(&fw_p, fw_name, &serial->interface->dev);
415 if (err) {
416 dev_err(&serial->interface->dev, "failed to request firmware: %d\n",
417 err);
418 goto err_free_mxdev;
419 }
420
421 err = mxu1_download_firmware(serial, fw_p);
422 if (err)
423 goto err_release_firmware;
424
425 /* device is being reset */
426 err = -ENODEV;
427 goto err_release_firmware;
428 }
429
430 return 0;
431
432 err_release_firmware:
433 release_firmware(fw_p);
434 err_free_mxdev:
435 kfree(mxdev);
436
437 return err;
438 }
439
440 static void mxu1_release(struct usb_serial *serial)
441 {
442 struct mxu1_device *mxdev;
443
444 mxdev = usb_get_serial_data(serial);
445 kfree(mxdev);
446 }
447
448 static int mxu1_write_byte(struct usb_serial_port *port, u32 addr,
449 u8 mask, u8 byte)
450 {
451 int status;
452 size_t size;
453 struct mxu1_write_data_bytes *data;
454
455 dev_dbg(&port->dev, "%s - addr 0x%08X, mask 0x%02X, byte 0x%02X\n",
456 __func__, addr, mask, byte);
457
458 size = sizeof(struct mxu1_write_data_bytes) + 2;
459 data = kzalloc(size, GFP_KERNEL);
460 if (!data)
461 return -ENOMEM;
462
463 data->bAddrType = MXU1_RW_DATA_ADDR_XDATA;
464 data->bDataType = MXU1_RW_DATA_BYTE;
465 data->bDataCounter = 1;
466 data->wBaseAddrHi = cpu_to_be16(addr >> 16);
467 data->wBaseAddrLo = cpu_to_be16(addr);
468 data->bData[0] = mask;
469 data->bData[1] = byte;
470
471 status = mxu1_send_ctrl_data_urb(port->serial, MXU1_WRITE_DATA, 0,
472 MXU1_RAM_PORT, data, size);
473 if (status < 0)
474 dev_err(&port->dev, "%s - failed: %d\n", __func__, status);
475
476 kfree(data);
477
478 return status;
479 }
480
481 static int mxu1_set_mcr(struct usb_serial_port *port, unsigned int mcr)
482 {
483 int status;
484
485 status = mxu1_write_byte(port,
486 MXU1_UART_BASE_ADDR + MXU1_UART_OFFSET_MCR,
487 MXU1_MCR_RTS | MXU1_MCR_DTR | MXU1_MCR_LOOP,
488 mcr);
489 return status;
490 }
491
492 static void mxu1_set_termios(struct tty_struct *tty,
493 struct usb_serial_port *port,
494 struct ktermios *old_termios)
495 {
496 struct mxu1_port *mxport = usb_get_serial_port_data(port);
497 struct mxu1_uart_config *config;
498 tcflag_t cflag, iflag;
499 speed_t baud;
500 int status;
501 unsigned int mcr;
502
503 cflag = tty->termios.c_cflag;
504 iflag = tty->termios.c_iflag;
505
506 if (old_termios &&
507 !tty_termios_hw_change(&tty->termios, old_termios) &&
508 tty->termios.c_iflag == old_termios->c_iflag) {
509 dev_dbg(&port->dev, "%s - nothing to change\n", __func__);
510 return;
511 }
512
513 dev_dbg(&port->dev,
514 "%s - cflag 0x%08x, iflag 0x%08x\n", __func__, cflag, iflag);
515
516 if (old_termios) {
517 dev_dbg(&port->dev, "%s - old cflag 0x%08x, old iflag 0x%08x\n",
518 __func__,
519 old_termios->c_cflag,
520 old_termios->c_iflag);
521 }
522
523 config = kzalloc(sizeof(*config), GFP_KERNEL);
524 if (!config)
525 return;
526
527 /* these flags must be set */
528 config->wFlags |= MXU1_UART_ENABLE_MS_INTS;
529 config->wFlags |= MXU1_UART_ENABLE_AUTO_START_DMA;
530 if (mxport->send_break)
531 config->wFlags |= MXU1_UART_SEND_BREAK_SIGNAL;
532 config->bUartMode = mxport->uart_mode;
533
534 switch (C_CSIZE(tty)) {
535 case CS5:
536 config->bDataBits = MXU1_UART_5_DATA_BITS;
537 break;
538 case CS6:
539 config->bDataBits = MXU1_UART_6_DATA_BITS;
540 break;
541 case CS7:
542 config->bDataBits = MXU1_UART_7_DATA_BITS;
543 break;
544 default:
545 case CS8:
546 config->bDataBits = MXU1_UART_8_DATA_BITS;
547 break;
548 }
549
550 if (C_PARENB(tty)) {
551 config->wFlags |= MXU1_UART_ENABLE_PARITY_CHECKING;
552 if (C_CMSPAR(tty)) {
553 if (C_PARODD(tty))
554 config->bParity = MXU1_UART_MARK_PARITY;
555 else
556 config->bParity = MXU1_UART_SPACE_PARITY;
557 } else {
558 if (C_PARODD(tty))
559 config->bParity = MXU1_UART_ODD_PARITY;
560 else
561 config->bParity = MXU1_UART_EVEN_PARITY;
562 }
563 } else {
564 config->bParity = MXU1_UART_NO_PARITY;
565 }
566
567 if (C_CSTOPB(tty))
568 config->bStopBits = MXU1_UART_2_STOP_BITS;
569 else
570 config->bStopBits = MXU1_UART_1_STOP_BITS;
571
572 if (C_CRTSCTS(tty)) {
573 /* RTS flow control must be off to drop RTS for baud rate B0 */
574 if (C_BAUD(tty) != B0)
575 config->wFlags |= MXU1_UART_ENABLE_RTS_IN;
576 config->wFlags |= MXU1_UART_ENABLE_CTS_OUT;
577 }
578
579 if (I_IXOFF(tty) || I_IXON(tty)) {
580 config->cXon = START_CHAR(tty);
581 config->cXoff = STOP_CHAR(tty);
582
583 if (I_IXOFF(tty))
584 config->wFlags |= MXU1_UART_ENABLE_X_IN;
585
586 if (I_IXON(tty))
587 config->wFlags |= MXU1_UART_ENABLE_X_OUT;
588 }
589
590 baud = tty_get_baud_rate(tty);
591 if (!baud)
592 baud = 9600;
593 config->wBaudRate = MXU1_BAUD_BASE / baud;
594
595 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",
596 __func__, baud, config->wBaudRate, config->wFlags,
597 config->bDataBits, config->bParity, config->bStopBits,
598 config->cXon, config->cXoff, config->bUartMode);
599
600 cpu_to_be16s(&config->wBaudRate);
601 cpu_to_be16s(&config->wFlags);
602
603 status = mxu1_send_ctrl_data_urb(port->serial, MXU1_SET_CONFIG, 0,
604 MXU1_UART1_PORT, config,
605 sizeof(*config));
606 if (status)
607 dev_err(&port->dev, "cannot set config: %d\n", status);
608
609 mutex_lock(&mxport->mutex);
610 mcr = mxport->mcr;
611
612 if (C_BAUD(tty) == B0)
613 mcr &= ~(MXU1_MCR_DTR | MXU1_MCR_RTS);
614 else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
615 mcr |= MXU1_MCR_DTR | MXU1_MCR_RTS;
616
617 status = mxu1_set_mcr(port, mcr);
618 if (status)
619 dev_err(&port->dev, "cannot set modem control: %d\n", status);
620 else
621 mxport->mcr = mcr;
622
623 mutex_unlock(&mxport->mutex);
624
625 kfree(config);
626 }
627
628 static int mxu1_get_serial_info(struct usb_serial_port *port,
629 struct serial_struct __user *ret_arg)
630 {
631 struct serial_struct ret_serial;
632 unsigned cwait;
633
634 if (!ret_arg)
635 return -EFAULT;
636
637 cwait = port->port.closing_wait;
638 if (cwait != ASYNC_CLOSING_WAIT_NONE)
639 cwait = jiffies_to_msecs(cwait) / 10;
640
641 memset(&ret_serial, 0, sizeof(ret_serial));
642
643 ret_serial.type = PORT_16550A;
644 ret_serial.line = port->minor;
645 ret_serial.port = 0;
646 ret_serial.xmit_fifo_size = port->bulk_out_size;
647 ret_serial.baud_base = MXU1_BAUD_BASE;
648 ret_serial.close_delay = 5*HZ;
649 ret_serial.closing_wait = cwait;
650
651 if (copy_to_user(ret_arg, &ret_serial, sizeof(*ret_arg)))
652 return -EFAULT;
653
654 return 0;
655 }
656
657
658 static int mxu1_set_serial_info(struct usb_serial_port *port,
659 struct serial_struct __user *new_arg)
660 {
661 struct serial_struct new_serial;
662 unsigned cwait;
663
664 if (copy_from_user(&new_serial, new_arg, sizeof(new_serial)))
665 return -EFAULT;
666
667 cwait = new_serial.closing_wait;
668 if (cwait != ASYNC_CLOSING_WAIT_NONE)
669 cwait = msecs_to_jiffies(10 * new_serial.closing_wait);
670
671 port->port.closing_wait = cwait;
672
673 return 0;
674 }
675
676 static int mxu1_ioctl(struct tty_struct *tty,
677 unsigned int cmd, unsigned long arg)
678 {
679 struct usb_serial_port *port = tty->driver_data;
680
681 switch (cmd) {
682 case TIOCGSERIAL:
683 return mxu1_get_serial_info(port,
684 (struct serial_struct __user *)arg);
685 case TIOCSSERIAL:
686 return mxu1_set_serial_info(port,
687 (struct serial_struct __user *)arg);
688 }
689
690 return -ENOIOCTLCMD;
691 }
692
693 static int mxu1_tiocmget(struct tty_struct *tty)
694 {
695 struct usb_serial_port *port = tty->driver_data;
696 struct mxu1_port *mxport = usb_get_serial_port_data(port);
697 unsigned int result;
698 unsigned int msr;
699 unsigned int mcr;
700 unsigned long flags;
701
702 mutex_lock(&mxport->mutex);
703 spin_lock_irqsave(&mxport->spinlock, flags);
704
705 msr = mxport->msr;
706 mcr = mxport->mcr;
707
708 spin_unlock_irqrestore(&mxport->spinlock, flags);
709 mutex_unlock(&mxport->mutex);
710
711 result = ((mcr & MXU1_MCR_DTR) ? TIOCM_DTR : 0) |
712 ((mcr & MXU1_MCR_RTS) ? TIOCM_RTS : 0) |
713 ((mcr & MXU1_MCR_LOOP) ? TIOCM_LOOP : 0) |
714 ((msr & MXU1_MSR_CTS) ? TIOCM_CTS : 0) |
715 ((msr & MXU1_MSR_CD) ? TIOCM_CAR : 0) |
716 ((msr & MXU1_MSR_RI) ? TIOCM_RI : 0) |
717 ((msr & MXU1_MSR_DSR) ? TIOCM_DSR : 0);
718
719 dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result);
720
721 return result;
722 }
723
724 static int mxu1_tiocmset(struct tty_struct *tty,
725 unsigned int set, unsigned int clear)
726 {
727 struct usb_serial_port *port = tty->driver_data;
728 struct mxu1_port *mxport = usb_get_serial_port_data(port);
729 int err;
730 unsigned int mcr;
731
732 mutex_lock(&mxport->mutex);
733 mcr = mxport->mcr;
734
735 if (set & TIOCM_RTS)
736 mcr |= MXU1_MCR_RTS;
737 if (set & TIOCM_DTR)
738 mcr |= MXU1_MCR_DTR;
739 if (set & TIOCM_LOOP)
740 mcr |= MXU1_MCR_LOOP;
741
742 if (clear & TIOCM_RTS)
743 mcr &= ~MXU1_MCR_RTS;
744 if (clear & TIOCM_DTR)
745 mcr &= ~MXU1_MCR_DTR;
746 if (clear & TIOCM_LOOP)
747 mcr &= ~MXU1_MCR_LOOP;
748
749 err = mxu1_set_mcr(port, mcr);
750 if (!err)
751 mxport->mcr = mcr;
752
753 mutex_unlock(&mxport->mutex);
754
755 return err;
756 }
757
758 static void mxu1_break(struct tty_struct *tty, int break_state)
759 {
760 struct usb_serial_port *port = tty->driver_data;
761 struct mxu1_port *mxport = usb_get_serial_port_data(port);
762
763 if (break_state == -1)
764 mxport->send_break = true;
765 else
766 mxport->send_break = false;
767
768 mxu1_set_termios(tty, port, NULL);
769 }
770
771 static int mxu1_open(struct tty_struct *tty, struct usb_serial_port *port)
772 {
773 struct mxu1_port *mxport = usb_get_serial_port_data(port);
774 struct usb_serial *serial = port->serial;
775 int status;
776 u16 open_settings;
777
778 open_settings = (MXU1_PIPE_MODE_CONTINUOUS |
779 MXU1_PIPE_TIMEOUT_ENABLE |
780 (MXU1_TRANSFER_TIMEOUT << 2));
781
782 mxport->msr = 0;
783
784 status = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
785 if (status) {
786 dev_err(&port->dev, "failed to submit interrupt urb: %d\n",
787 status);
788 return status;
789 }
790
791 if (tty)
792 mxu1_set_termios(tty, port, NULL);
793
794 status = mxu1_send_ctrl_urb(serial, MXU1_OPEN_PORT,
795 open_settings, MXU1_UART1_PORT);
796 if (status) {
797 dev_err(&port->dev, "cannot send open command: %d\n", status);
798 goto unlink_int_urb;
799 }
800
801 status = mxu1_send_ctrl_urb(serial, MXU1_START_PORT,
802 0, MXU1_UART1_PORT);
803 if (status) {
804 dev_err(&port->dev, "cannot send start command: %d\n", status);
805 goto unlink_int_urb;
806 }
807
808 status = mxu1_send_ctrl_urb(serial, MXU1_PURGE_PORT,
809 MXU1_PURGE_INPUT, MXU1_UART1_PORT);
810 if (status) {
811 dev_err(&port->dev, "cannot clear input buffers: %d\n",
812 status);
813
814 goto unlink_int_urb;
815 }
816
817 status = mxu1_send_ctrl_urb(serial, MXU1_PURGE_PORT,
818 MXU1_PURGE_OUTPUT, MXU1_UART1_PORT);
819 if (status) {
820 dev_err(&port->dev, "cannot clear output buffers: %d\n",
821 status);
822
823 goto unlink_int_urb;
824 }
825
826 /*
827 * reset the data toggle on the bulk endpoints to work around bug in
828 * host controllers where things get out of sync some times
829 */
830 usb_clear_halt(serial->dev, port->write_urb->pipe);
831 usb_clear_halt(serial->dev, port->read_urb->pipe);
832
833 if (tty)
834 mxu1_set_termios(tty, port, NULL);
835
836 status = mxu1_send_ctrl_urb(serial, MXU1_OPEN_PORT,
837 open_settings, MXU1_UART1_PORT);
838 if (status) {
839 dev_err(&port->dev, "cannot send open command: %d\n", status);
840 goto unlink_int_urb;
841 }
842
843 status = mxu1_send_ctrl_urb(serial, MXU1_START_PORT,
844 0, MXU1_UART1_PORT);
845 if (status) {
846 dev_err(&port->dev, "cannot send start command: %d\n", status);
847 goto unlink_int_urb;
848 }
849
850 status = usb_serial_generic_open(tty, port);
851 if (status)
852 goto unlink_int_urb;
853
854 return 0;
855
856 unlink_int_urb:
857 usb_kill_urb(port->interrupt_in_urb);
858
859 return status;
860 }
861
862 static void mxu1_close(struct usb_serial_port *port)
863 {
864 int status;
865
866 usb_serial_generic_close(port);
867 usb_kill_urb(port->interrupt_in_urb);
868
869 status = mxu1_send_ctrl_urb(port->serial, MXU1_CLOSE_PORT,
870 0, MXU1_UART1_PORT);
871 if (status) {
872 dev_err(&port->dev, "failed to send close port command: %d\n",
873 status);
874 }
875 }
876
877 static void mxu1_handle_new_msr(struct usb_serial_port *port, u8 msr)
878 {
879 struct mxu1_port *mxport = usb_get_serial_port_data(port);
880 struct async_icount *icount;
881 unsigned long flags;
882
883 dev_dbg(&port->dev, "%s - msr 0x%02X\n", __func__, msr);
884
885 spin_lock_irqsave(&mxport->spinlock, flags);
886 mxport->msr = msr & MXU1_MSR_MASK;
887 spin_unlock_irqrestore(&mxport->spinlock, flags);
888
889 if (msr & MXU1_MSR_DELTA_MASK) {
890 icount = &port->icount;
891 if (msr & MXU1_MSR_DELTA_CTS)
892 icount->cts++;
893 if (msr & MXU1_MSR_DELTA_DSR)
894 icount->dsr++;
895 if (msr & MXU1_MSR_DELTA_CD)
896 icount->dcd++;
897 if (msr & MXU1_MSR_DELTA_RI)
898 icount->rng++;
899
900 wake_up_interruptible(&port->port.delta_msr_wait);
901 }
902 }
903
904 static void mxu1_interrupt_callback(struct urb *urb)
905 {
906 struct usb_serial_port *port = urb->context;
907 unsigned char *data = urb->transfer_buffer;
908 int length = urb->actual_length;
909 int function;
910 int status;
911 u8 msr;
912
913 switch (urb->status) {
914 case 0:
915 break;
916 case -ECONNRESET:
917 case -ENOENT:
918 case -ESHUTDOWN:
919 dev_dbg(&port->dev, "%s - urb shutting down: %d\n",
920 __func__, urb->status);
921 return;
922 default:
923 dev_dbg(&port->dev, "%s - nonzero urb status: %d\n",
924 __func__, urb->status);
925 goto exit;
926 }
927
928 if (length != 2) {
929 dev_dbg(&port->dev, "%s - bad packet size: %d\n",
930 __func__, length);
931 goto exit;
932 }
933
934 if (data[0] == MXU1_CODE_HARDWARE_ERROR) {
935 dev_err(&port->dev, "hardware error: %d\n", data[1]);
936 goto exit;
937 }
938
939 function = mxu1_get_func_from_code(data[0]);
940
941 dev_dbg(&port->dev, "%s - function %d, data 0x%02X\n",
942 __func__, function, data[1]);
943
944 switch (function) {
945 case MXU1_CODE_DATA_ERROR:
946 dev_dbg(&port->dev, "%s - DATA ERROR, data 0x%02X\n",
947 __func__, data[1]);
948 break;
949
950 case MXU1_CODE_MODEM_STATUS:
951 msr = data[1];
952 mxu1_handle_new_msr(port, msr);
953 break;
954
955 default:
956 dev_err(&port->dev, "unknown interrupt code: 0x%02X\n",
957 data[1]);
958 break;
959 }
960
961 exit:
962 status = usb_submit_urb(urb, GFP_ATOMIC);
963 if (status) {
964 dev_err(&port->dev, "resubmit interrupt urb failed: %d\n",
965 status);
966 }
967 }
968
969 static struct usb_serial_driver mxu11x0_device = {
970 .driver = {
971 .owner = THIS_MODULE,
972 .name = "mxu11x0",
973 },
974 .description = "MOXA UPort 11x0",
975 .id_table = mxu1_idtable,
976 .num_ports = 1,
977 .port_probe = mxu1_port_probe,
978 .port_remove = mxu1_port_remove,
979 .attach = mxu1_startup,
980 .release = mxu1_release,
981 .open = mxu1_open,
982 .close = mxu1_close,
983 .ioctl = mxu1_ioctl,
984 .set_termios = mxu1_set_termios,
985 .tiocmget = mxu1_tiocmget,
986 .tiocmset = mxu1_tiocmset,
987 .tiocmiwait = usb_serial_generic_tiocmiwait,
988 .get_icount = usb_serial_generic_get_icount,
989 .break_ctl = mxu1_break,
990 .read_int_callback = mxu1_interrupt_callback,
991 };
992
993 static struct usb_serial_driver *const serial_drivers[] = {
994 &mxu11x0_device, NULL
995 };
996
997 module_usb_serial_driver(serial_drivers, mxu1_idtable);
998
999 MODULE_AUTHOR("Mathieu Othacehe <m.othacehe@gmail.com>");
1000 MODULE_DESCRIPTION("MOXA UPort 11x0 USB to Serial Hub Driver");
1001 MODULE_LICENSE("GPL");
1002 MODULE_FIRMWARE("moxa/moxa-1110.fw");
1003 MODULE_FIRMWARE("moxa/moxa-1130.fw");
1004 MODULE_FIRMWARE("moxa/moxa-1131.fw");
1005 MODULE_FIRMWARE("moxa/moxa-1150.fw");
1006 MODULE_FIRMWARE("moxa/moxa-1151.fw");