blob: b562665e9fb1e4c722d55ebcefcf46305e690d6a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Edgeport USB Serial Converter driver
3 *
4 * Copyright (C) 2000-2002 Inside Out Networks, All rights reserved.
5 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.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 * Supports the following devices:
13 * EP/1 EP/2 EP/4 EP/21 EP/22 EP/221 EP/42 EP/421 WATCHPORT
14 *
15 * For questions or problems with this driver, contact Inside Out
16 * Networks technical support, or Peter Berger <pberger@brimson.com>,
17 * or Al Borchers <alborchers@steinerpoint.com>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/kernel.h>
21#include <linux/jiffies.h>
22#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/slab.h>
24#include <linux/tty.h>
25#include <linux/tty_driver.h>
26#include <linux/tty_flip.h>
27#include <linux/module.h>
28#include <linux/spinlock.h>
Matthias Kaehlcke241ca642007-12-04 16:15:40 +010029#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/serial.h>
Johan Hovold55090762014-04-25 15:23:03 +020031#include <linux/swab.h>
Johan Hovoldd733cec2010-05-19 00:01:37 +020032#include <linux/kfifo.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/ioctl.h>
Jaswinder Singhd12b2192008-07-04 23:06:09 +053034#include <linux/firmware.h>
Alan Cox2742fd82008-04-29 14:45:15 +010035#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070037#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "io_16654.h"
40#include "io_usbvend.h"
41#include "io_ti.h"
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
44#define DRIVER_DESC "Edgeport USB Serial Driver"
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#define EPROM_PAGE_SIZE 64
47
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/* different hardware types */
50#define HARDWARE_TYPE_930 0
51#define HARDWARE_TYPE_TIUMP 1
52
Alan Cox2742fd82008-04-29 14:45:15 +010053/* IOCTL_PRIVATE_TI_GET_MODE Definitions */
54#define TI_MODE_CONFIGURING 0 /* Device has not entered start device */
55#define TI_MODE_BOOT 1 /* Staying in boot mode */
56#define TI_MODE_DOWNLOAD 2 /* Made it to download mode */
57#define TI_MODE_TRANSITIONING 3 /* Currently in boot mode but
58 transitioning to download mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60/* read urb state */
61#define EDGE_READ_URB_RUNNING 0
62#define EDGE_READ_URB_STOPPING 1
63#define EDGE_READ_URB_STOPPED 2
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#define EDGE_CLOSING_WAIT 4000 /* in .01 sec */
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68/* Product information read from the Edgeport */
Alan Cox2742fd82008-04-29 14:45:15 +010069struct product_info {
70 int TiMode; /* Current TI Mode */
71 __u8 hardware_type; /* Type of hardware */
Linus Torvalds1da177e2005-04-16 15:20:36 -070072} __attribute__((packed));
73
Peter E. Bergerc0e34832015-07-31 01:55:06 -050074/*
75 * Edgeport firmware header
76 *
77 * "build_number" has been set to 0 in all three of the images I have
78 * seen, and Digi Tech Support suggests that it is safe to ignore it.
79 *
80 * "length" is the number of bytes of actual data following the header.
81 *
82 * "checksum" is the low order byte resulting from adding the values of
83 * all the data bytes.
84 */
85struct edgeport_fw_hdr {
86 u8 major_version;
87 u8 minor_version;
88 __le16 build_number;
89 __le16 length;
90 u8 checksum;
91} __packed;
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093struct edgeport_port {
94 __u16 uart_base;
95 __u16 dma_address;
96 __u8 shadow_msr;
97 __u8 shadow_mcr;
98 __u8 shadow_lsr;
99 __u8 lsr_mask;
Martin Olsson19af5cd2009-04-23 11:37:37 +0200100 __u32 ump_read_timeout; /* Number of milliseconds the UMP will
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 wait without data before completing
102 a read short */
103 int baud_rate;
104 int close_pending;
105 int lsr_event;
Johan Hovold48ee5802013-03-21 12:37:10 +0100106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 struct edgeport_serial *edge_serial;
108 struct usb_serial_port *port;
Alan Cox2742fd82008-04-29 14:45:15 +0100109 __u8 bUartMode; /* Port type, 0: RS232, etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 spinlock_t ep_lock;
111 int ep_read_urb_state;
112 int ep_write_urb_in_use;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113};
114
115struct edgeport_serial {
116 struct product_info product_info;
Alan Cox2742fd82008-04-29 14:45:15 +0100117 u8 TI_I2C_Type; /* Type of I2C in UMP */
118 u8 TiReadI2C; /* Set to TRUE if we have read the
119 I2c in Boot Mode */
Matthias Kaehlcke241ca642007-12-04 16:15:40 +0100120 struct mutex es_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 int num_ports_open;
122 struct usb_serial *serial;
Peter E. Bergerc0e34832015-07-31 01:55:06 -0500123 int fw_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124};
125
126
127/* Devices that this driver supports */
Németh Márton7d40d7e2010-01-10 15:34:24 +0100128static const struct usb_device_id edgeport_1port_id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
130 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
131 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
132 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
133 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
134 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
135 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
136 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
137 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
138 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
139 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
140 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
141 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
142 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
143 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
144 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
145 { }
146};
147
Németh Márton7d40d7e2010-01-10 15:34:24 +0100148static const struct usb_device_id edgeport_2port_id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
150 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
151 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
152 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
153 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
154 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
155 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
156 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
157 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
158 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
159 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
160 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -0400161 /* The 4, 8 and 16 port devices show up as multiple 2 port devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -0400163 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
164 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
165 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
166 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 { }
168};
169
170/* Devices that this driver supports */
Németh Márton7d40d7e2010-01-10 15:34:24 +0100171static const struct usb_device_id id_table_combined[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
173 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
174 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
175 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
176 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
177 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
178 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
179 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
180 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
181 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
182 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
183 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
184 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
185 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
186 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
187 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
188 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
189 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
190 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
191 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
192 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
193 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
194 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
195 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
196 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
197 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
198 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
199 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
200 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -0400201 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
202 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
203 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
204 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 { }
206};
207
Alan Cox2742fd82008-04-29 14:45:15 +0100208MODULE_DEVICE_TABLE(usb, id_table_combined);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210static int closing_wait = EDGE_CLOSING_WAIT;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030211static bool ignore_cpu_rev;
Alan Cox2742fd82008-04-29 14:45:15 +0100212static int default_uart_mode; /* RS232 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Jiri Slaby2e124b42013-01-03 15:53:06 +0100214static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
215 int length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217static void stop_read(struct edgeport_port *edge_port);
218static int restart_read(struct edgeport_port *edge_port);
219
Alan Cox95da3102008-07-22 11:09:07 +0100220static void edge_set_termios(struct tty_struct *tty,
221 struct usb_serial_port *port, struct ktermios *old_termios);
Jiri Slabyae3759c2013-04-04 22:41:01 +0200222static void edge_send(struct usb_serial_port *port, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -0400224/* sysfs attributes */
225static int edge_create_sysfs_attrs(struct usb_serial_port *port);
226static int edge_remove_sysfs_attrs(struct usb_serial_port *port);
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Peter E. Bergerc3ece7e2015-07-31 01:55:05 -0500229/* Timeouts in msecs: firmware downloads take longer */
230#define TI_VSEND_TIMEOUT_DEFAULT 1000
231#define TI_VSEND_TIMEOUT_FW_DOWNLOAD 10000
232
Alan Cox2742fd82008-04-29 14:45:15 +0100233static int ti_vread_sync(struct usb_device *dev, __u8 request,
234 __u16 value, __u16 index, u8 *data, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
236 int status;
237
Alan Cox2742fd82008-04-29 14:45:15 +0100238 status = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request,
239 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
240 value, index, data, size, 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 if (status < 0)
242 return status;
243 if (status != size) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700244 dev_dbg(&dev->dev, "%s - wanted to write %d, but only wrote %d\n",
245 __func__, size, status);
Alan Cox2742fd82008-04-29 14:45:15 +0100246 return -ECOMM;
247 }
248 return 0;
249}
250
Peter E. Bergerc3ece7e2015-07-31 01:55:05 -0500251static int ti_vsend_sync(struct usb_device *dev, u8 request, u16 value,
252 u16 index, u8 *data, int size, int timeout)
Alan Cox2742fd82008-04-29 14:45:15 +0100253{
254 int status;
255
256 status = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
257 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
Peter E. Bergerc3ece7e2015-07-31 01:55:05 -0500258 value, index, data, size, timeout);
Alan Cox2742fd82008-04-29 14:45:15 +0100259 if (status < 0)
260 return status;
261 if (status != size) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700262 dev_dbg(&dev->dev, "%s - wanted to write %d, but only wrote %d\n",
263 __func__, size, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return -ECOMM;
265 }
266 return 0;
267}
268
Alan Cox2742fd82008-04-29 14:45:15 +0100269static int send_cmd(struct usb_device *dev, __u8 command,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 __u8 moduleid, __u16 value, u8 *data,
271 int size)
272{
Peter E. Bergerc3ece7e2015-07-31 01:55:05 -0500273 return ti_vsend_sync(dev, command, value, moduleid, data, size,
274 TI_VSEND_TIMEOUT_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
277/* clear tx/rx buffers and fifo in TI UMP */
Alan Cox2742fd82008-04-29 14:45:15 +0100278static int purge_port(struct usb_serial_port *port, __u16 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Greg Kroah-Hartman11438322013-06-06 10:32:00 -0700280 int port_number = port->port_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700282 dev_dbg(&port->dev, "%s - port %d, mask %x\n", __func__, port_number, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Alan Cox2742fd82008-04-29 14:45:15 +0100284 return send_cmd(port->serial->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 UMPC_PURGE_PORT,
286 (__u8)(UMPM_UART1_PORT + port_number),
287 mask,
288 NULL,
289 0);
290}
291
292/**
Alan Cox2742fd82008-04-29 14:45:15 +0100293 * read_download_mem - Read edgeport memory from TI chip
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 * @dev: usb device pointer
295 * @start_address: Device CPU address at which to read
296 * @length: Length of above data
297 * @address_type: Can read both XDATA and I2C
298 * @buffer: pointer to input data buffer
299 */
Alan Cox2742fd82008-04-29 14:45:15 +0100300static int read_download_mem(struct usb_device *dev, int start_address,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 int length, __u8 address_type, __u8 *buffer)
302{
303 int status = 0;
304 __u8 read_length;
Johan Hovold55090762014-04-25 15:23:03 +0200305 u16 be_start_address;
Alan Cox2742fd82008-04-29 14:45:15 +0100306
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700307 dev_dbg(&dev->dev, "%s - @ %x for %d\n", __func__, start_address, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 /* Read in blocks of 64 bytes
310 * (TI firmware can't handle more than 64 byte reads)
311 */
312 while (length) {
313 if (length > 64)
Alan Cox2742fd82008-04-29 14:45:15 +0100314 read_length = 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 else
316 read_length = (__u8)length;
317
318 if (read_length > 1) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700319 dev_dbg(&dev->dev, "%s - @ %x for %d\n", __func__, start_address, read_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
Johan Hovold55090762014-04-25 15:23:03 +0200321 /*
322 * NOTE: Must use swab as wIndex is sent in little-endian
323 * byte order regardless of host byte order.
324 */
325 be_start_address = swab16((u16)start_address);
Alan Cox2742fd82008-04-29 14:45:15 +0100326 status = ti_vread_sync(dev, UMPC_MEMORY_READ,
327 (__u16)address_type,
Johan Hovold55090762014-04-25 15:23:03 +0200328 be_start_address,
Alan Cox2742fd82008-04-29 14:45:15 +0100329 buffer, read_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 if (status) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700332 dev_dbg(&dev->dev, "%s - ERROR %x\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return status;
334 }
335
Alan Cox2742fd82008-04-29 14:45:15 +0100336 if (read_length > 1)
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100337 usb_serial_debug_data(&dev->dev, __func__, read_length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 /* Update pointers/length */
340 start_address += read_length;
341 buffer += read_length;
342 length -= read_length;
343 }
Alan Cox2742fd82008-04-29 14:45:15 +0100344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return status;
346}
347
Alan Cox2742fd82008-04-29 14:45:15 +0100348static int read_ram(struct usb_device *dev, int start_address,
349 int length, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
Alan Cox2742fd82008-04-29 14:45:15 +0100351 return read_download_mem(dev, start_address, length,
352 DTK_ADDR_SPACE_XDATA, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
355/* Read edgeport memory to a given block */
Alan Cox2742fd82008-04-29 14:45:15 +0100356static int read_boot_mem(struct edgeport_serial *serial,
357 int start_address, int length, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
359 int status = 0;
360 int i;
361
Alan Cox2742fd82008-04-29 14:45:15 +0100362 for (i = 0; i < length; i++) {
363 status = ti_vread_sync(serial->serial->dev,
364 UMPC_MEMORY_READ, serial->TI_I2C_Type,
365 (__u16)(start_address+i), &buffer[i], 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (status) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700367 dev_dbg(&serial->serial->dev->dev, "%s - ERROR %x\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return status;
369 }
370 }
371
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700372 dev_dbg(&serial->serial->dev->dev, "%s - start_address = %x, length = %d\n",
373 __func__, start_address, length);
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100374 usb_serial_debug_data(&serial->serial->dev->dev, __func__, length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 serial->TiReadI2C = 1;
377
378 return status;
379}
380
381/* Write given block to TI EPROM memory */
Alan Cox2742fd82008-04-29 14:45:15 +0100382static int write_boot_mem(struct edgeport_serial *serial,
383 int start_address, int length, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
385 int status = 0;
386 int i;
Johan Hovolde9305d22009-12-28 23:01:50 +0100387 u8 *temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 /* Must do a read before write */
390 if (!serial->TiReadI2C) {
Johan Hovolde9305d22009-12-28 23:01:50 +0100391 temp = kmalloc(1, GFP_KERNEL);
Johan Hovold10c642d2013-12-29 19:22:56 +0100392 if (!temp)
Johan Hovolde9305d22009-12-28 23:01:50 +0100393 return -ENOMEM;
Johan Hovold10c642d2013-12-29 19:22:56 +0100394
Johan Hovolde9305d22009-12-28 23:01:50 +0100395 status = read_boot_mem(serial, 0, 1, temp);
396 kfree(temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (status)
398 return status;
399 }
400
Alan Cox2742fd82008-04-29 14:45:15 +0100401 for (i = 0; i < length; ++i) {
Peter E. Bergerc3ece7e2015-07-31 01:55:05 -0500402 status = ti_vsend_sync(serial->serial->dev, UMPC_MEMORY_WRITE,
403 buffer[i], (u16)(i + start_address), NULL,
404 0, TI_VSEND_TIMEOUT_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (status)
406 return status;
407 }
408
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700409 dev_dbg(&serial->serial->dev->dev, "%s - start_sddr = %x, length = %d\n", __func__, start_address, length);
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100410 usb_serial_debug_data(&serial->serial->dev->dev, __func__, length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 return status;
413}
414
415
416/* Write edgeport I2C memory to TI chip */
Alan Cox2742fd82008-04-29 14:45:15 +0100417static int write_i2c_mem(struct edgeport_serial *serial,
418 int start_address, int length, __u8 address_type, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700420 struct device *dev = &serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 int status = 0;
422 int write_length;
Johan Hovold55090762014-04-25 15:23:03 +0200423 u16 be_start_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 /* We can only send a maximum of 1 aligned byte page at a time */
Alan Cox2742fd82008-04-29 14:45:15 +0100426
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300427 /* calculate the number of bytes left in the first page */
Alan Cox2742fd82008-04-29 14:45:15 +0100428 write_length = EPROM_PAGE_SIZE -
429 (start_address & (EPROM_PAGE_SIZE - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 if (write_length > length)
432 write_length = length;
433
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700434 dev_dbg(dev, "%s - BytesInFirstPage Addr = %x, length = %d\n",
435 __func__, start_address, write_length);
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100436 usb_serial_debug_data(dev, __func__, write_length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Johan Hovold55090762014-04-25 15:23:03 +0200438 /*
439 * Write first page.
440 *
441 * NOTE: Must use swab as wIndex is sent in little-endian byte order
442 * regardless of host byte order.
443 */
444 be_start_address = swab16((u16)start_address);
Peter E. Bergerc3ece7e2015-07-31 01:55:05 -0500445 status = ti_vsend_sync(serial->serial->dev, UMPC_MEMORY_WRITE,
446 (u16)address_type, be_start_address,
447 buffer, write_length, TI_VSEND_TIMEOUT_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 if (status) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700449 dev_dbg(dev, "%s - ERROR %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 return status;
451 }
452
453 length -= write_length;
454 start_address += write_length;
455 buffer += write_length;
456
Alan Cox2742fd82008-04-29 14:45:15 +0100457 /* We should be aligned now -- can write
458 max page size bytes at a time */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 while (length) {
460 if (length > EPROM_PAGE_SIZE)
461 write_length = EPROM_PAGE_SIZE;
462 else
463 write_length = length;
464
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700465 dev_dbg(dev, "%s - Page Write Addr = %x, length = %d\n",
466 __func__, start_address, write_length);
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100467 usb_serial_debug_data(dev, __func__, write_length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Johan Hovold55090762014-04-25 15:23:03 +0200469 /*
470 * Write next page.
471 *
472 * NOTE: Must use swab as wIndex is sent in little-endian byte
473 * order regardless of host byte order.
474 */
475 be_start_address = swab16((u16)start_address);
Alan Cox2742fd82008-04-29 14:45:15 +0100476 status = ti_vsend_sync(serial->serial->dev, UMPC_MEMORY_WRITE,
Peter E. Bergerc3ece7e2015-07-31 01:55:05 -0500477 (u16)address_type, be_start_address, buffer,
478 write_length, TI_VSEND_TIMEOUT_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (status) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700480 dev_err(dev, "%s - ERROR %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 return status;
482 }
Alan Cox2742fd82008-04-29 14:45:15 +0100483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 length -= write_length;
485 start_address += write_length;
486 buffer += write_length;
487 }
488 return status;
489}
490
491/* Examine the UMP DMA registers and LSR
Alan Cox2742fd82008-04-29 14:45:15 +0100492 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 * Check the MSBit of the X and Y DMA byte count registers.
494 * A zero in this bit indicates that the TX DMA buffers are empty
495 * then check the TX Empty bit in the UART.
496 */
Alan Cox2742fd82008-04-29 14:45:15 +0100497static int tx_active(struct edgeport_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
499 int status;
500 struct out_endpoint_desc_block *oedb;
501 __u8 *lsr;
502 int bytes_left = 0;
503
Alan Cox2742fd82008-04-29 14:45:15 +0100504 oedb = kmalloc(sizeof(*oedb), GFP_KERNEL);
Johan Hovold10c642d2013-12-29 19:22:56 +0100505 if (!oedb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Alan Cox2742fd82008-04-29 14:45:15 +0100508 lsr = kmalloc(1, GFP_KERNEL); /* Sigh, that's right, just one byte,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 as not all platforms can do DMA
510 from stack */
511 if (!lsr) {
512 kfree(oedb);
513 return -ENOMEM;
514 }
515 /* Read the DMA Count Registers */
Alan Cox2742fd82008-04-29 14:45:15 +0100516 status = read_ram(port->port->serial->dev, port->dma_address,
517 sizeof(*oedb), (void *)oedb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 if (status)
519 goto exit_is_tx_active;
520
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700521 dev_dbg(&port->port->dev, "%s - XByteCount 0x%X\n", __func__, oedb->XByteCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 /* and the LSR */
Alan Cox2742fd82008-04-29 14:45:15 +0100524 status = read_ram(port->port->serial->dev,
525 port->uart_base + UMPMEM_OFFS_UART_LSR, 1, lsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 if (status)
528 goto exit_is_tx_active;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700529 dev_dbg(&port->port->dev, "%s - LSR = 0x%X\n", __func__, *lsr);
Alan Cox2742fd82008-04-29 14:45:15 +0100530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 /* If either buffer has data or we are transmitting then return TRUE */
Alan Cox2742fd82008-04-29 14:45:15 +0100532 if ((oedb->XByteCount & 0x80) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 bytes_left += 64;
534
Alan Cox2742fd82008-04-29 14:45:15 +0100535 if ((*lsr & UMP_UART_LSR_TX_MASK) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 bytes_left += 1;
537
538 /* We return Not Active if we get any kind of error */
539exit_is_tx_active:
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700540 dev_dbg(&port->port->dev, "%s - return %d\n", __func__, bytes_left);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 kfree(lsr);
543 kfree(oedb);
544 return bytes_left;
545}
546
Alan Cox2742fd82008-04-29 14:45:15 +0100547static int choose_config(struct usb_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Alan Cox2742fd82008-04-29 14:45:15 +0100549 /*
550 * There may be multiple configurations on this device, in which case
551 * we would need to read and parse all of them to find out which one
552 * we want. However, we just support one config at this point,
553 * configuration # 1, which is Config Descriptor 0.
554 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700556 dev_dbg(&dev->dev, "%s - Number of Interfaces = %d\n",
557 __func__, dev->config->desc.bNumInterfaces);
558 dev_dbg(&dev->dev, "%s - MAX Power = %d\n",
559 __func__, dev->config->desc.bMaxPower * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 if (dev->config->desc.bNumInterfaces != 1) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700562 dev_err(&dev->dev, "%s - bNumInterfaces is not 1, ERROR!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return -ENODEV;
564 }
565
566 return 0;
567}
568
Alan Cox2742fd82008-04-29 14:45:15 +0100569static int read_rom(struct edgeport_serial *serial,
570 int start_address, int length, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
572 int status;
573
574 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
Alan Cox2742fd82008-04-29 14:45:15 +0100575 status = read_download_mem(serial->serial->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 start_address,
577 length,
578 serial->TI_I2C_Type,
579 buffer);
580 } else {
Alan Cox2742fd82008-04-29 14:45:15 +0100581 status = read_boot_mem(serial, start_address, length,
582 buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 return status;
585}
586
Alan Cox2742fd82008-04-29 14:45:15 +0100587static int write_rom(struct edgeport_serial *serial, int start_address,
588 int length, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
590 if (serial->product_info.TiMode == TI_MODE_BOOT)
Alan Cox2742fd82008-04-29 14:45:15 +0100591 return write_boot_mem(serial, start_address, length,
592 buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD)
Alan Cox2742fd82008-04-29 14:45:15 +0100595 return write_i2c_mem(serial, start_address, length,
596 serial->TI_I2C_Type, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return -EINVAL;
598}
599
600
601
602/* Read a descriptor header from I2C based on type */
Alan Cox2742fd82008-04-29 14:45:15 +0100603static int get_descriptor_addr(struct edgeport_serial *serial,
604 int desc_type, struct ti_i2c_desc *rom_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
606 int start_address;
607 int status;
608
609 /* Search for requested descriptor in I2C */
610 start_address = 2;
611 do {
Alan Cox2742fd82008-04-29 14:45:15 +0100612 status = read_rom(serial,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 start_address,
614 sizeof(struct ti_i2c_desc),
Alan Cox2742fd82008-04-29 14:45:15 +0100615 (__u8 *)rom_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 if (status)
617 return 0;
618
619 if (rom_desc->Type == desc_type)
620 return start_address;
621
Johan Hovold55090762014-04-25 15:23:03 +0200622 start_address = start_address + sizeof(struct ti_i2c_desc) +
623 le16_to_cpu(rom_desc->Size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 } while ((start_address < TI_MAX_I2C_SIZE) && rom_desc->Type);
Alan Cox2742fd82008-04-29 14:45:15 +0100626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return 0;
628}
629
630/* Validate descriptor checksum */
Alan Cox2742fd82008-04-29 14:45:15 +0100631static int valid_csum(struct ti_i2c_desc *rom_desc, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
633 __u16 i;
634 __u8 cs = 0;
635
Johan Hovold55090762014-04-25 15:23:03 +0200636 for (i = 0; i < le16_to_cpu(rom_desc->Size); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 cs = (__u8)(cs + buffer[i]);
Alan Cox2742fd82008-04-29 14:45:15 +0100638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 if (cs != rom_desc->CheckSum) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700640 pr_debug("%s - Mismatch %x - %x", __func__, rom_desc->CheckSum, cs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return -EINVAL;
642 }
643 return 0;
644}
645
646/* Make sure that the I2C image is good */
Alan Cox2742fd82008-04-29 14:45:15 +0100647static int check_i2c_image(struct edgeport_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
649 struct device *dev = &serial->serial->dev->dev;
650 int status = 0;
651 struct ti_i2c_desc *rom_desc;
652 int start_address = 2;
653 __u8 *buffer;
654 __u16 ttype;
655
Alan Cox2742fd82008-04-29 14:45:15 +0100656 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
Johan Hovold10c642d2013-12-29 19:22:56 +0100657 if (!rom_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return -ENOMEM;
Johan Hovold10c642d2013-12-29 19:22:56 +0100659
Alan Cox2742fd82008-04-29 14:45:15 +0100660 buffer = kmalloc(TI_MAX_I2C_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (!buffer) {
Alan Cox2742fd82008-04-29 14:45:15 +0100662 kfree(rom_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return -ENOMEM;
664 }
665
Alan Cox2742fd82008-04-29 14:45:15 +0100666 /* Read the first byte (Signature0) must be 0x52 or 0x10 */
667 status = read_rom(serial, 0, 1, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if (status)
Alan Cox2742fd82008-04-29 14:45:15 +0100669 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 if (*buffer != UMP5152 && *buffer != UMP3410) {
Alan Cox2742fd82008-04-29 14:45:15 +0100672 dev_err(dev, "%s - invalid buffer signature\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 status = -ENODEV;
Alan Cox2742fd82008-04-29 14:45:15 +0100674 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
676
677 do {
Alan Cox2742fd82008-04-29 14:45:15 +0100678 /* Validate the I2C */
679 status = read_rom(serial,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 start_address,
681 sizeof(struct ti_i2c_desc),
682 (__u8 *)rom_desc);
683 if (status)
684 break;
685
Alan Cox2742fd82008-04-29 14:45:15 +0100686 if ((start_address + sizeof(struct ti_i2c_desc) +
Johan Hovold55090762014-04-25 15:23:03 +0200687 le16_to_cpu(rom_desc->Size)) > TI_MAX_I2C_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 status = -ENODEV;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700689 dev_dbg(dev, "%s - structure too big, erroring out.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 break;
691 }
692
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700693 dev_dbg(dev, "%s Type = 0x%x\n", __func__, rom_desc->Type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Alan Cox2742fd82008-04-29 14:45:15 +0100695 /* Skip type 2 record */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 ttype = rom_desc->Type & 0x0f;
Alan Cox2742fd82008-04-29 14:45:15 +0100697 if (ttype != I2C_DESC_TYPE_FIRMWARE_BASIC
698 && ttype != I2C_DESC_TYPE_FIRMWARE_AUTO) {
699 /* Read the descriptor data */
700 status = read_rom(serial, start_address +
701 sizeof(struct ti_i2c_desc),
Johan Hovold55090762014-04-25 15:23:03 +0200702 le16_to_cpu(rom_desc->Size),
703 buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (status)
705 break;
706
Alan Cox2742fd82008-04-29 14:45:15 +0100707 status = valid_csum(rom_desc, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if (status)
709 break;
710 }
Alan Cox2742fd82008-04-29 14:45:15 +0100711 start_address = start_address + sizeof(struct ti_i2c_desc) +
Johan Hovold55090762014-04-25 15:23:03 +0200712 le16_to_cpu(rom_desc->Size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Alan Cox2742fd82008-04-29 14:45:15 +0100714 } while ((rom_desc->Type != I2C_DESC_TYPE_ION) &&
715 (start_address < TI_MAX_I2C_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Alan Cox2742fd82008-04-29 14:45:15 +0100717 if ((rom_desc->Type != I2C_DESC_TYPE_ION) ||
718 (start_address > TI_MAX_I2C_SIZE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 status = -ENODEV;
720
Alan Cox2742fd82008-04-29 14:45:15 +0100721out:
722 kfree(buffer);
723 kfree(rom_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 return status;
725}
726
Alan Cox2742fd82008-04-29 14:45:15 +0100727static int get_manuf_info(struct edgeport_serial *serial, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
729 int status;
730 int start_address;
731 struct ti_i2c_desc *rom_desc;
732 struct edge_ti_manuf_descriptor *desc;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700733 struct device *dev = &serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Alan Cox2742fd82008-04-29 14:45:15 +0100735 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
Johan Hovold10c642d2013-12-29 19:22:56 +0100736 if (!rom_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 return -ENOMEM;
Johan Hovold10c642d2013-12-29 19:22:56 +0100738
Alan Cox2742fd82008-04-29 14:45:15 +0100739 start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_ION,
740 rom_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742 if (!start_address) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700743 dev_dbg(dev, "%s - Edge Descriptor not found in I2C\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 status = -ENODEV;
745 goto exit;
746 }
747
Alan Cox2742fd82008-04-29 14:45:15 +0100748 /* Read the descriptor data */
749 status = read_rom(serial, start_address+sizeof(struct ti_i2c_desc),
Johan Hovold55090762014-04-25 15:23:03 +0200750 le16_to_cpu(rom_desc->Size), buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if (status)
752 goto exit;
Alan Cox2742fd82008-04-29 14:45:15 +0100753
754 status = valid_csum(rom_desc, buffer);
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 desc = (struct edge_ti_manuf_descriptor *)buffer;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700757 dev_dbg(dev, "%s - IonConfig 0x%x\n", __func__, desc->IonConfig);
758 dev_dbg(dev, "%s - Version %d\n", __func__, desc->Version);
759 dev_dbg(dev, "%s - Cpu/Board 0x%x\n", __func__, desc->CpuRev_BoardRev);
760 dev_dbg(dev, "%s - NumPorts %d\n", __func__, desc->NumPorts);
761 dev_dbg(dev, "%s - NumVirtualPorts %d\n", __func__, desc->NumVirtualPorts);
762 dev_dbg(dev, "%s - TotalPorts %d\n", __func__, desc->TotalPorts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764exit:
Alan Cox2742fd82008-04-29 14:45:15 +0100765 kfree(rom_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 return status;
767}
768
769/* Build firmware header used for firmware update */
Peter E. Bergerc0e34832015-07-31 01:55:06 -0500770static int build_i2c_fw_hdr(u8 *header, struct device *dev,
771 const struct firmware *fw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
773 __u8 *buffer;
774 int buffer_size;
775 int i;
776 __u8 cs = 0;
777 struct ti_i2c_desc *i2c_header;
778 struct ti_i2c_image_header *img_header;
779 struct ti_i2c_firmware_rec *firmware_rec;
Peter E. Bergerc0e34832015-07-31 01:55:06 -0500780 struct edgeport_fw_hdr *fw_hdr = (struct edgeport_fw_hdr *)fw->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Alan Cox2742fd82008-04-29 14:45:15 +0100782 /* In order to update the I2C firmware we must change the type 2 record
783 * to type 0xF2. This will force the UMP to come up in Boot Mode.
784 * Then while in boot mode, the driver will download the latest
785 * firmware (padded to 15.5k) into the UMP ram. And finally when the
786 * device comes back up in download mode the driver will cause the new
787 * firmware to be copied from the UMP Ram to I2C and the firmware will
788 * update the record type from 0xf2 to 0x02.
789 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Alan Cox2742fd82008-04-29 14:45:15 +0100791 /* Allocate a 15.5k buffer + 2 bytes for version number
792 * (Firmware Record) */
793 buffer_size = (((1024 * 16) - 512 ) +
794 sizeof(struct ti_i2c_firmware_rec));
795
796 buffer = kmalloc(buffer_size, GFP_KERNEL);
Johan Hovold10c642d2013-12-29 19:22:56 +0100797 if (!buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return -ENOMEM;
Alan Cox2742fd82008-04-29 14:45:15 +0100799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 // Set entire image of 0xffs
Alan Cox2742fd82008-04-29 14:45:15 +0100801 memset(buffer, 0xff, buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Alan Cox2742fd82008-04-29 14:45:15 +0100803 /* Copy version number into firmware record */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 firmware_rec = (struct ti_i2c_firmware_rec *)buffer;
805
Peter E. Bergerc0e34832015-07-31 01:55:06 -0500806 firmware_rec->Ver_Major = fw_hdr->major_version;
807 firmware_rec->Ver_Minor = fw_hdr->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Alan Cox2742fd82008-04-29 14:45:15 +0100809 /* Pointer to fw_down memory image */
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530810 img_header = (struct ti_i2c_image_header *)&fw->data[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Alan Cox2742fd82008-04-29 14:45:15 +0100812 memcpy(buffer + sizeof(struct ti_i2c_firmware_rec),
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530813 &fw->data[4 + sizeof(struct ti_i2c_image_header)],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 le16_to_cpu(img_header->Length));
815
816 for (i=0; i < buffer_size; i++) {
817 cs = (__u8)(cs + buffer[i]);
818 }
819
Alan Cox2742fd82008-04-29 14:45:15 +0100820 kfree(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Alan Cox2742fd82008-04-29 14:45:15 +0100822 /* Build new header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 i2c_header = (struct ti_i2c_desc *)header;
824 firmware_rec = (struct ti_i2c_firmware_rec*)i2c_header->Data;
Alan Cox2742fd82008-04-29 14:45:15 +0100825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 i2c_header->Type = I2C_DESC_TYPE_FIRMWARE_BLANK;
Johan Hovoldc03890f2014-04-26 11:53:44 +0200827 i2c_header->Size = cpu_to_le16(buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 i2c_header->CheckSum = cs;
Peter E. Bergerc0e34832015-07-31 01:55:06 -0500829 firmware_rec->Ver_Major = fw_hdr->major_version;
830 firmware_rec->Ver_Minor = fw_hdr->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 return 0;
833}
834
835/* Try to figure out what type of I2c we have */
Alan Cox2742fd82008-04-29 14:45:15 +0100836static int i2c_type_bootmode(struct edgeport_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700838 struct device *dev = &serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 int status;
Johan Hovolde9305d22009-12-28 23:01:50 +0100840 u8 *data;
841
842 data = kmalloc(1, GFP_KERNEL);
Johan Hovold10c642d2013-12-29 19:22:56 +0100843 if (!data)
Johan Hovolde9305d22009-12-28 23:01:50 +0100844 return -ENOMEM;
Alan Cox2742fd82008-04-29 14:45:15 +0100845
846 /* Try to read type 2 */
847 status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
Johan Hovolde9305d22009-12-28 23:01:50 +0100848 DTK_ADDR_SPACE_I2C_TYPE_II, 0, data, 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 if (status)
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700850 dev_dbg(dev, "%s - read 2 status error = %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 else
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700852 dev_dbg(dev, "%s - read 2 data = 0x%x\n", __func__, *data);
Johan Hovolde9305d22009-12-28 23:01:50 +0100853 if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700854 dev_dbg(dev, "%s - ROM_TYPE_II\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
Johan Hovolde9305d22009-12-28 23:01:50 +0100856 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 }
858
Alan Cox2742fd82008-04-29 14:45:15 +0100859 /* Try to read type 3 */
860 status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
Johan Hovolde9305d22009-12-28 23:01:50 +0100861 DTK_ADDR_SPACE_I2C_TYPE_III, 0, data, 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 if (status)
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700863 dev_dbg(dev, "%s - read 3 status error = %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 else
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700865 dev_dbg(dev, "%s - read 2 data = 0x%x\n", __func__, *data);
Johan Hovolde9305d22009-12-28 23:01:50 +0100866 if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700867 dev_dbg(dev, "%s - ROM_TYPE_III\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_III;
Johan Hovolde9305d22009-12-28 23:01:50 +0100869 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 }
871
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700872 dev_dbg(dev, "%s - Unknown\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
Johan Hovolde9305d22009-12-28 23:01:50 +0100874 status = -ENODEV;
875out:
876 kfree(data);
877 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
879
Alan Cox2742fd82008-04-29 14:45:15 +0100880static int bulk_xfer(struct usb_serial *serial, void *buffer,
881 int length, int *num_sent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
883 int status;
884
Alan Cox2742fd82008-04-29 14:45:15 +0100885 status = usb_bulk_msg(serial->dev,
886 usb_sndbulkpipe(serial->dev,
887 serial->port[0]->bulk_out_endpointAddress),
888 buffer, length, num_sent, 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return status;
890}
891
892/* Download given firmware image to the device (IN BOOT MODE) */
Alan Cox2742fd82008-04-29 14:45:15 +0100893static int download_code(struct edgeport_serial *serial, __u8 *image,
894 int image_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
896 int status = 0;
897 int pos;
898 int transfer;
899 int done;
900
Alan Cox2742fd82008-04-29 14:45:15 +0100901 /* Transfer firmware image */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 for (pos = 0; pos < image_length; ) {
Alan Cox2742fd82008-04-29 14:45:15 +0100903 /* Read the next buffer from file */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 transfer = image_length - pos;
905 if (transfer > EDGE_FW_BULK_MAX_PACKET_SIZE)
906 transfer = EDGE_FW_BULK_MAX_PACKET_SIZE;
907
Alan Cox2742fd82008-04-29 14:45:15 +0100908 /* Transfer data */
909 status = bulk_xfer(serial->serial, &image[pos],
910 transfer, &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 if (status)
912 break;
Alan Cox2742fd82008-04-29 14:45:15 +0100913 /* Advance buffer pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 pos += done;
915 }
916
917 return status;
918}
919
Alan Cox2742fd82008-04-29 14:45:15 +0100920/* FIXME!!! */
921static int config_boot_dev(struct usb_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
923 return 0;
924}
925
Alan Cox2742fd82008-04-29 14:45:15 +0100926static int ti_cpu_rev(struct edge_ti_manuf_descriptor *desc)
927{
928 return TI_GET_CPU_REVISION(desc->CpuRev_BoardRev);
929}
930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931/**
932 * DownloadTIFirmware - Download run-time operating firmware to the TI5052
Alan Cox2742fd82008-04-29 14:45:15 +0100933 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 * This routine downloads the main operating code into the TI5052, using the
935 * boot code already burned into E2PROM or ROM.
936 */
Peter E. Bergerc0e34832015-07-31 01:55:06 -0500937static int download_fw(struct edgeport_serial *serial,
938 const struct firmware *fw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
940 struct device *dev = &serial->serial->dev->dev;
941 int status = 0;
942 int start_address;
943 struct edge_ti_manuf_descriptor *ti_manuf_desc;
944 struct usb_interface_descriptor *interface;
945 int download_cur_ver;
946 int download_new_ver;
Peter E. Bergerc0e34832015-07-31 01:55:06 -0500947 struct edgeport_fw_hdr *fw_hdr = (struct edgeport_fw_hdr *)fw->data;
948
949 if (fw->size < sizeof(struct edgeport_fw_hdr)) {
950 dev_err(&serial->serial->interface->dev,
951 "Incomplete firmware header.\n");
952 return -EINVAL;
953 }
954
955 /* If on-board version is newer, "fw_version" will be updated below. */
956 serial->fw_version = (fw_hdr->major_version << 8) +
957 fw_hdr->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 /* This routine is entered by both the BOOT mode and the Download mode
960 * We can determine which code is running by the reading the config
961 * descriptor and if we have only one bulk pipe it is in boot mode
962 */
963 serial->product_info.hardware_type = HARDWARE_TYPE_TIUMP;
964
965 /* Default to type 2 i2c */
966 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
967
Alan Cox2742fd82008-04-29 14:45:15 +0100968 status = choose_config(serial->serial->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (status)
970 return status;
971
972 interface = &serial->serial->interface->cur_altsetting->desc;
973 if (!interface) {
Alan Cox2742fd82008-04-29 14:45:15 +0100974 dev_err(dev, "%s - no interface set, error!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 return -ENODEV;
976 }
977
Alan Cox2742fd82008-04-29 14:45:15 +0100978 /*
979 * Setup initial mode -- the default mode 0 is TI_MODE_CONFIGURING
980 * if we have more than one endpoint we are definitely in download
981 * mode
982 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 if (interface->bNumEndpoints > 1)
984 serial->product_info.TiMode = TI_MODE_DOWNLOAD;
985 else
Alan Cox2742fd82008-04-29 14:45:15 +0100986 /* Otherwise we will remain in configuring mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 serial->product_info.TiMode = TI_MODE_CONFIGURING;
988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 /********************************************************************/
990 /* Download Mode */
991 /********************************************************************/
992 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
993 struct ti_i2c_desc *rom_desc;
994
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700995 dev_dbg(dev, "%s - RUNNING IN DOWNLOAD MODE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Alan Cox2742fd82008-04-29 14:45:15 +0100997 status = check_i2c_image(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (status) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700999 dev_dbg(dev, "%s - DOWNLOAD MODE -- BAD I2C\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 return status;
1001 }
Alan Cox2742fd82008-04-29 14:45:15 +01001002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 /* Validate Hardware version number
1004 * Read Manufacturing Descriptor from TI Based Edgeport
1005 */
Alan Cox2742fd82008-04-29 14:45:15 +01001006 ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
Johan Hovold10c642d2013-12-29 19:22:56 +01001007 if (!ti_manuf_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 return -ENOMEM;
Johan Hovold10c642d2013-12-29 19:22:56 +01001009
Alan Cox2742fd82008-04-29 14:45:15 +01001010 status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001012 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 return status;
1014 }
1015
Alan Cox2742fd82008-04-29 14:45:15 +01001016 /* Check version number of ION descriptor */
1017 if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001018 dev_dbg(dev, "%s - Wrong CPU Rev %d (Must be 2)\n",
Alan Cox2742fd82008-04-29 14:45:15 +01001019 __func__, ti_cpu_rev(ti_manuf_desc));
1020 kfree(ti_manuf_desc);
1021 return -EINVAL;
1022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Alan Cox2742fd82008-04-29 14:45:15 +01001024 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 if (!rom_desc) {
Alan Cox2742fd82008-04-29 14:45:15 +01001026 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return -ENOMEM;
1028 }
1029
Alan Cox2742fd82008-04-29 14:45:15 +01001030 /* Search for type 2 record (firmware record) */
1031 start_address = get_descriptor_addr(serial,
1032 I2C_DESC_TYPE_FIRMWARE_BASIC, rom_desc);
1033 if (start_address != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 struct ti_i2c_firmware_rec *firmware_version;
Johan Hovolde9305d22009-12-28 23:01:50 +01001035 u8 *record;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001037 dev_dbg(dev, "%s - Found Type FIRMWARE (Type 2) record\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Alan Cox2742fd82008-04-29 14:45:15 +01001039 firmware_version = kmalloc(sizeof(*firmware_version),
1040 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 if (!firmware_version) {
Alan Cox2742fd82008-04-29 14:45:15 +01001042 kfree(rom_desc);
1043 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 return -ENOMEM;
1045 }
1046
Alan Cox2742fd82008-04-29 14:45:15 +01001047 /* Validate version number
1048 * Read the descriptor data
1049 */
1050 status = read_rom(serial, start_address +
1051 sizeof(struct ti_i2c_desc),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 sizeof(struct ti_i2c_firmware_rec),
1053 (__u8 *)firmware_version);
1054 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001055 kfree(firmware_version);
1056 kfree(rom_desc);
1057 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 return status;
1059 }
1060
Alan Cox2742fd82008-04-29 14:45:15 +01001061 /* Check version number of download with current
1062 version in I2c */
1063 download_cur_ver = (firmware_version->Ver_Major << 8) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 (firmware_version->Ver_Minor);
Peter E. Bergerc0e34832015-07-31 01:55:06 -05001065 download_new_ver = (fw_hdr->major_version << 8) +
1066 (fw_hdr->minor_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001068 dev_dbg(dev, "%s - >> FW Versions Device %d.%d Driver %d.%d\n",
1069 __func__, firmware_version->Ver_Major,
1070 firmware_version->Ver_Minor,
Peter E. Bergerc0e34832015-07-31 01:55:06 -05001071 fw_hdr->major_version, fw_hdr->minor_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Alan Cox2742fd82008-04-29 14:45:15 +01001073 /* Check if we have an old version in the I2C and
1074 update if necessary */
Greg Kroah-Hartman0827a9f2010-08-17 15:15:37 -07001075 if (download_cur_ver < download_new_ver) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001076 dev_dbg(dev, "%s - Update I2C dld from %d.%d to %d.%d\n",
1077 __func__,
1078 firmware_version->Ver_Major,
1079 firmware_version->Ver_Minor,
Peter E. Bergerc0e34832015-07-31 01:55:06 -05001080 fw_hdr->major_version,
1081 fw_hdr->minor_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Johan Hovolde9305d22009-12-28 23:01:50 +01001083 record = kmalloc(1, GFP_KERNEL);
1084 if (!record) {
Johan Hovolde9305d22009-12-28 23:01:50 +01001085 kfree(firmware_version);
1086 kfree(rom_desc);
1087 kfree(ti_manuf_desc);
1088 return -ENOMEM;
1089 }
Alan Cox2742fd82008-04-29 14:45:15 +01001090 /* In order to update the I2C firmware we must
1091 * change the type 2 record to type 0xF2. This
1092 * will force the UMP to come up in Boot Mode.
1093 * Then while in boot mode, the driver will
1094 * download the latest firmware (padded to
1095 * 15.5k) into the UMP ram. Finally when the
1096 * device comes back up in download mode the
1097 * driver will cause the new firmware to be
1098 * copied from the UMP Ram to I2C and the
1099 * firmware will update the record type from
1100 * 0xf2 to 0x02.
1101 */
Johan Hovolde9305d22009-12-28 23:01:50 +01001102 *record = I2C_DESC_TYPE_FIRMWARE_BLANK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Alan Cox2742fd82008-04-29 14:45:15 +01001104 /* Change the I2C Firmware record type to
1105 0xf2 to trigger an update */
1106 status = write_rom(serial, start_address,
Johan Hovolde9305d22009-12-28 23:01:50 +01001107 sizeof(*record), record);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 if (status) {
Johan Hovolde9305d22009-12-28 23:01:50 +01001109 kfree(record);
Alan Cox2742fd82008-04-29 14:45:15 +01001110 kfree(firmware_version);
1111 kfree(rom_desc);
1112 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 return status;
1114 }
1115
Alan Cox2742fd82008-04-29 14:45:15 +01001116 /* verify the write -- must do this in order
1117 * for write to complete before we do the
1118 * hardware reset
1119 */
1120 status = read_rom(serial,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 start_address,
Johan Hovolde9305d22009-12-28 23:01:50 +01001122 sizeof(*record),
1123 record);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 if (status) {
Johan Hovolde9305d22009-12-28 23:01:50 +01001125 kfree(record);
Alan Cox2742fd82008-04-29 14:45:15 +01001126 kfree(firmware_version);
1127 kfree(rom_desc);
1128 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 return status;
1130 }
1131
Johan Hovolde9305d22009-12-28 23:01:50 +01001132 if (*record != I2C_DESC_TYPE_FIRMWARE_BLANK) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001133 dev_err(dev, "%s - error resetting device\n", __func__);
Johan Hovolde9305d22009-12-28 23:01:50 +01001134 kfree(record);
Alan Cox2742fd82008-04-29 14:45:15 +01001135 kfree(firmware_version);
1136 kfree(rom_desc);
1137 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 return -ENODEV;
1139 }
1140
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001141 dev_dbg(dev, "%s - HARDWARE RESET\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Alan Cox2742fd82008-04-29 14:45:15 +01001143 /* Reset UMP -- Back to BOOT MODE */
1144 status = ti_vsend_sync(serial->serial->dev,
1145 UMPC_HARDWARE_RESET,
Peter E. Bergerc3ece7e2015-07-31 01:55:05 -05001146 0, 0, NULL, 0,
1147 TI_VSEND_TIMEOUT_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001149 dev_dbg(dev, "%s - HARDWARE RESET return %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
1151 /* return an error on purpose. */
Johan Hovolde9305d22009-12-28 23:01:50 +01001152 kfree(record);
Alan Cox2742fd82008-04-29 14:45:15 +01001153 kfree(firmware_version);
1154 kfree(rom_desc);
1155 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 return -ENODEV;
Peter E. Bergerc0e34832015-07-31 01:55:06 -05001157 } else {
1158 /* Same or newer fw version is already loaded */
1159 serial->fw_version = download_cur_ver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 }
Alan Cox2742fd82008-04-29 14:45:15 +01001161 kfree(firmware_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 }
Alan Cox2742fd82008-04-29 14:45:15 +01001163 /* Search for type 0xF2 record (firmware blank record) */
1164 else if ((start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_FIRMWARE_BLANK, rom_desc)) != 0) {
1165#define HEADER_SIZE (sizeof(struct ti_i2c_desc) + \
1166 sizeof(struct ti_i2c_firmware_rec))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 __u8 *header;
1168 __u8 *vheader;
1169
Alan Cox2742fd82008-04-29 14:45:15 +01001170 header = kmalloc(HEADER_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 if (!header) {
Alan Cox2742fd82008-04-29 14:45:15 +01001172 kfree(rom_desc);
1173 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 return -ENOMEM;
1175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Alan Cox2742fd82008-04-29 14:45:15 +01001177 vheader = kmalloc(HEADER_SIZE, GFP_KERNEL);
1178 if (!vheader) {
Alan Cox2742fd82008-04-29 14:45:15 +01001179 kfree(header);
1180 kfree(rom_desc);
1181 kfree(ti_manuf_desc);
1182 return -ENOMEM;
1183 }
1184
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001185 dev_dbg(dev, "%s - Found Type BLANK FIRMWARE (Type F2) record\n", __func__);
Alan Cox2742fd82008-04-29 14:45:15 +01001186
1187 /*
1188 * In order to update the I2C firmware we must change
1189 * the type 2 record to type 0xF2. This will force the
1190 * UMP to come up in Boot Mode. Then while in boot
1191 * mode, the driver will download the latest firmware
1192 * (padded to 15.5k) into the UMP ram. Finally when the
1193 * device comes back up in download mode the driver
1194 * will cause the new firmware to be copied from the
1195 * UMP Ram to I2C and the firmware will update the
1196 * record type from 0xf2 to 0x02.
1197 */
Peter E. Bergerc0e34832015-07-31 01:55:06 -05001198 status = build_i2c_fw_hdr(header, dev, fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001200 kfree(vheader);
1201 kfree(header);
1202 kfree(rom_desc);
1203 kfree(ti_manuf_desc);
Roel Kluinfd6e5bb2010-08-10 14:29:19 -07001204 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
1206
Alan Cox2742fd82008-04-29 14:45:15 +01001207 /* Update I2C with type 0xf2 record with correct
1208 size and checksum */
1209 status = write_rom(serial,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 start_address,
1211 HEADER_SIZE,
1212 header);
1213 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001214 kfree(vheader);
1215 kfree(header);
1216 kfree(rom_desc);
1217 kfree(ti_manuf_desc);
Roel Kluin9800eb32010-07-20 15:29:08 -07001218 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 }
1220
Alan Cox2742fd82008-04-29 14:45:15 +01001221 /* verify the write -- must do this in order for
1222 write to complete before we do the hardware reset */
1223 status = read_rom(serial, start_address,
1224 HEADER_SIZE, vheader);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 if (status) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001227 dev_dbg(dev, "%s - can't read header back\n", __func__);
Alan Cox2742fd82008-04-29 14:45:15 +01001228 kfree(vheader);
1229 kfree(header);
1230 kfree(rom_desc);
1231 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 return status;
1233 }
1234 if (memcmp(vheader, header, HEADER_SIZE)) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001235 dev_dbg(dev, "%s - write download record failed\n", __func__);
Alan Cox2742fd82008-04-29 14:45:15 +01001236 kfree(vheader);
1237 kfree(header);
1238 kfree(rom_desc);
1239 kfree(ti_manuf_desc);
Roel Kluin1e297092010-07-02 00:36:43 +02001240 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 }
1242
Alan Cox2742fd82008-04-29 14:45:15 +01001243 kfree(vheader);
1244 kfree(header);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001246 dev_dbg(dev, "%s - Start firmware update\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Alan Cox2742fd82008-04-29 14:45:15 +01001248 /* Tell firmware to copy download image into I2C */
1249 status = ti_vsend_sync(serial->serial->dev,
Peter E. Bergerc3ece7e2015-07-31 01:55:05 -05001250 UMPC_COPY_DNLD_TO_I2C,
1251 0, 0, NULL, 0,
1252 TI_VSEND_TIMEOUT_FW_DOWNLOAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001254 dev_dbg(dev, "%s - Update complete 0x%x\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001256 dev_err(dev,
1257 "%s - UMPC_COPY_DNLD_TO_I2C failed\n",
1258 __func__);
1259 kfree(rom_desc);
1260 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 return status;
1262 }
1263 }
1264
1265 // The device is running the download code
Alan Cox2742fd82008-04-29 14:45:15 +01001266 kfree(rom_desc);
1267 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 return 0;
1269 }
1270
1271 /********************************************************************/
1272 /* Boot Mode */
1273 /********************************************************************/
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001274 dev_dbg(dev, "%s - RUNNING IN BOOT MODE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Alan Cox2742fd82008-04-29 14:45:15 +01001276 /* Configure the TI device so we can use the BULK pipes for download */
1277 status = config_boot_dev(serial->serial->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 if (status)
1279 return status;
1280
Alan Cox2742fd82008-04-29 14:45:15 +01001281 if (le16_to_cpu(serial->serial->dev->descriptor.idVendor)
1282 != USB_VENDOR_ID_ION) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001283 dev_dbg(dev, "%s - VID = 0x%x\n", __func__,
1284 le16_to_cpu(serial->serial->dev->descriptor.idVendor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
Alan Cox2742fd82008-04-29 14:45:15 +01001286 goto stayinbootmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 }
1288
Alan Cox2742fd82008-04-29 14:45:15 +01001289 /* We have an ION device (I2c Must be programmed)
1290 Determine I2C image type */
1291 if (i2c_type_bootmode(serial))
1292 goto stayinbootmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Alan Cox2742fd82008-04-29 14:45:15 +01001294 /* Check for ION Vendor ID and that the I2C is valid */
1295 if (!check_i2c_image(serial)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 struct ti_i2c_image_header *header;
1297 int i;
1298 __u8 cs = 0;
1299 __u8 *buffer;
1300 int buffer_size;
1301
1302 /* Validate Hardware version number
1303 * Read Manufacturing Descriptor from TI Based Edgeport
1304 */
Alan Cox2742fd82008-04-29 14:45:15 +01001305 ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
Johan Hovold10c642d2013-12-29 19:22:56 +01001306 if (!ti_manuf_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return -ENOMEM;
Johan Hovold10c642d2013-12-29 19:22:56 +01001308
Alan Cox2742fd82008-04-29 14:45:15 +01001309 status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001311 kfree(ti_manuf_desc);
1312 goto stayinbootmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 }
1314
Alan Cox2742fd82008-04-29 14:45:15 +01001315 /* Check for version 2 */
1316 if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001317 dev_dbg(dev, "%s - Wrong CPU Rev %d (Must be 2)\n",
1318 __func__, ti_cpu_rev(ti_manuf_desc));
Alan Cox2742fd82008-04-29 14:45:15 +01001319 kfree(ti_manuf_desc);
1320 goto stayinbootmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 }
1322
Alan Cox2742fd82008-04-29 14:45:15 +01001323 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 /*
Alan Cox2742fd82008-04-29 14:45:15 +01001326 * In order to update the I2C firmware we must change the type
1327 * 2 record to type 0xF2. This will force the UMP to come up
1328 * in Boot Mode. Then while in boot mode, the driver will
1329 * download the latest firmware (padded to 15.5k) into the
1330 * UMP ram. Finally when the device comes back up in download
1331 * mode the driver will cause the new firmware to be copied
1332 * from the UMP Ram to I2C and the firmware will update the
1333 * record type from 0xf2 to 0x02.
1334 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 * Do we really have to copy the whole firmware image,
1336 * or could we do this in place!
1337 */
1338
Alan Cox2742fd82008-04-29 14:45:15 +01001339 /* Allocate a 15.5k buffer + 3 byte header */
1340 buffer_size = (((1024 * 16) - 512) +
1341 sizeof(struct ti_i2c_image_header));
1342 buffer = kmalloc(buffer_size, GFP_KERNEL);
Johan Hovold10c642d2013-12-29 19:22:56 +01001343 if (!buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 return -ENOMEM;
Alan Cox2742fd82008-04-29 14:45:15 +01001345
1346 /* Initialize the buffer to 0xff (pad the buffer) */
1347 memset(buffer, 0xff, buffer_size);
Jaswinder Singhd12b2192008-07-04 23:06:09 +05301348 memcpy(buffer, &fw->data[4], fw->size - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Alan Cox2742fd82008-04-29 14:45:15 +01001350 for (i = sizeof(struct ti_i2c_image_header);
1351 i < buffer_size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 cs = (__u8)(cs + buffer[i]);
1353 }
Alan Cox2742fd82008-04-29 14:45:15 +01001354
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 header = (struct ti_i2c_image_header *)buffer;
Alan Cox2742fd82008-04-29 14:45:15 +01001356
1357 /* update length and checksum after padding */
1358 header->Length = cpu_to_le16((__u16)(buffer_size -
1359 sizeof(struct ti_i2c_image_header)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 header->CheckSum = cs;
1361
Alan Cox2742fd82008-04-29 14:45:15 +01001362 /* Download the operational code */
Peter E. Bergerc0e34832015-07-31 01:55:06 -05001363 dev_dbg(dev, "%s - Downloading operational code image version %d.%d (TI UMP)\n",
1364 __func__,
1365 fw_hdr->major_version, fw_hdr->minor_version);
Alan Cox2742fd82008-04-29 14:45:15 +01001366 status = download_code(serial, buffer, buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Alan Cox2742fd82008-04-29 14:45:15 +01001368 kfree(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370 if (status) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001371 dev_dbg(dev, "%s - Error downloading operational code image\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 return status;
1373 }
1374
Alan Cox2742fd82008-04-29 14:45:15 +01001375 /* Device will reboot */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 serial->product_info.TiMode = TI_MODE_TRANSITIONING;
1377
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001378 dev_dbg(dev, "%s - Download successful -- Device rebooting...\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 /* return an error on purpose */
1381 return -ENODEV;
1382 }
1383
Alan Cox2742fd82008-04-29 14:45:15 +01001384stayinbootmode:
1385 /* Eprom is invalid or blank stay in boot mode */
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001386 dev_dbg(dev, "%s - STAYING IN BOOT MODE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 serial->product_info.TiMode = TI_MODE_BOOT;
1388
1389 return 0;
1390}
1391
1392
Alan Cox2742fd82008-04-29 14:45:15 +01001393static int ti_do_config(struct edgeport_port *port, int feature, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394{
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001395 int port_number = port->port->port_number;
1396
Alan Cox2742fd82008-04-29 14:45:15 +01001397 on = !!on; /* 1 or 0 not bitmask */
1398 return send_cmd(port->port->serial->dev,
1399 feature, (__u8)(UMPM_UART1_PORT + port_number),
1400 on, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401}
1402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Alan Cox2742fd82008-04-29 14:45:15 +01001404static int restore_mcr(struct edgeport_port *port, __u8 mcr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
1406 int status = 0;
1407
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001408 dev_dbg(&port->port->dev, "%s - %x\n", __func__, mcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Alan Cox2742fd82008-04-29 14:45:15 +01001410 status = ti_do_config(port, UMPC_SET_CLR_DTR, mcr & MCR_DTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 if (status)
1412 return status;
Alan Cox2742fd82008-04-29 14:45:15 +01001413 status = ti_do_config(port, UMPC_SET_CLR_RTS, mcr & MCR_RTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 if (status)
1415 return status;
Alan Cox2742fd82008-04-29 14:45:15 +01001416 return ti_do_config(port, UMPC_SET_CLR_LOOPBACK, mcr & MCR_LOOPBACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417}
1418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419/* Convert TI LSR to standard UART flags */
Alan Cox2742fd82008-04-29 14:45:15 +01001420static __u8 map_line_status(__u8 ti_lsr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421{
1422 __u8 lsr = 0;
1423
1424#define MAP_FLAG(flagUmp, flagUart) \
1425 if (ti_lsr & flagUmp) \
1426 lsr |= flagUart;
1427
1428 MAP_FLAG(UMP_UART_LSR_OV_MASK, LSR_OVER_ERR) /* overrun */
1429 MAP_FLAG(UMP_UART_LSR_PE_MASK, LSR_PAR_ERR) /* parity error */
1430 MAP_FLAG(UMP_UART_LSR_FE_MASK, LSR_FRM_ERR) /* framing error */
1431 MAP_FLAG(UMP_UART_LSR_BR_MASK, LSR_BREAK) /* break detected */
Alan Cox2742fd82008-04-29 14:45:15 +01001432 MAP_FLAG(UMP_UART_LSR_RX_MASK, LSR_RX_AVAIL) /* rx data available */
1433 MAP_FLAG(UMP_UART_LSR_TX_MASK, LSR_TX_EMPTY) /* tx hold reg empty */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
1435#undef MAP_FLAG
1436
1437 return lsr;
1438}
1439
Alan Cox2742fd82008-04-29 14:45:15 +01001440static void handle_new_msr(struct edgeport_port *edge_port, __u8 msr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
1442 struct async_icount *icount;
1443 struct tty_struct *tty;
1444
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001445 dev_dbg(&edge_port->port->dev, "%s - %02x\n", __func__, msr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Alan Cox2742fd82008-04-29 14:45:15 +01001447 if (msr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
1448 EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
Johan Hovoldcf9a9d62013-03-21 12:37:09 +01001449 icount = &edge_port->port->icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 /* update input line counters */
1452 if (msr & EDGEPORT_MSR_DELTA_CTS)
1453 icount->cts++;
1454 if (msr & EDGEPORT_MSR_DELTA_DSR)
1455 icount->dsr++;
1456 if (msr & EDGEPORT_MSR_DELTA_CD)
1457 icount->dcd++;
1458 if (msr & EDGEPORT_MSR_DELTA_RI)
1459 icount->rng++;
Johan Hovold48ee5802013-03-21 12:37:10 +01001460 wake_up_interruptible(&edge_port->port->port.delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 }
1462
1463 /* Save the new modem status */
1464 edge_port->shadow_msr = msr & 0xf0;
1465
Alan Cox4a90f092008-10-13 10:39:46 +01001466 tty = tty_port_tty_get(&edge_port->port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 /* handle CTS flow control */
1468 if (tty && C_CRTSCTS(tty)) {
Peter Hurleyd95e3ca2014-09-10 15:06:27 -04001469 if (msr & EDGEPORT_MSR_CTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 }
Alan Cox4a90f092008-10-13 10:39:46 +01001472 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473}
1474
Alan Cox2742fd82008-04-29 14:45:15 +01001475static void handle_new_lsr(struct edgeport_port *edge_port, int lsr_data,
1476 __u8 lsr, __u8 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
1478 struct async_icount *icount;
Alan Cox2742fd82008-04-29 14:45:15 +01001479 __u8 new_lsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR |
1480 LSR_FRM_ERR | LSR_BREAK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001482 dev_dbg(&edge_port->port->dev, "%s - %02x\n", __func__, new_lsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
1484 edge_port->shadow_lsr = lsr;
1485
Alan Cox2742fd82008-04-29 14:45:15 +01001486 if (new_lsr & LSR_BREAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 /*
1488 * Parity and Framing errors only count if they
1489 * occur exclusive of a break being received.
1490 */
1491 new_lsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
1493 /* Place LSR data byte into Rx buffer */
Jiri Slaby2e124b42013-01-03 15:53:06 +01001494 if (lsr_data)
1495 edge_tty_recv(edge_port->port, &data, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
1497 /* update input line counters */
Johan Hovoldcf9a9d62013-03-21 12:37:09 +01001498 icount = &edge_port->port->icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 if (new_lsr & LSR_BREAK)
1500 icount->brk++;
1501 if (new_lsr & LSR_OVER_ERR)
1502 icount->overrun++;
1503 if (new_lsr & LSR_PAR_ERR)
1504 icount->parity++;
1505 if (new_lsr & LSR_FRM_ERR)
1506 icount->frame++;
1507}
1508
1509
Alan Cox2742fd82008-04-29 14:45:15 +01001510static void edge_interrupt_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511{
Ming Leicdc97792008-02-24 18:41:47 +08001512 struct edgeport_serial *edge_serial = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 struct usb_serial_port *port;
1514 struct edgeport_port *edge_port;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001515 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 unsigned char *data = urb->transfer_buffer;
1517 int length = urb->actual_length;
1518 int port_number;
1519 int function;
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001520 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 __u8 lsr;
1522 __u8 msr;
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001523 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001525 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 case 0:
1527 /* success */
1528 break;
1529 case -ECONNRESET:
1530 case -ENOENT:
1531 case -ESHUTDOWN:
1532 /* this urb is terminated, clean up */
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001533 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001534 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 return;
1536 default:
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001537 dev_err(&urb->dev->dev, "%s - nonzero urb status received: "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001538 "%d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 goto exit;
1540 }
1541
1542 if (!length) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001543 dev_dbg(&urb->dev->dev, "%s - no data in urb\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 goto exit;
1545 }
1546
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001547 dev = &edge_serial->serial->dev->dev;
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001548 usb_serial_debug_data(dev, __func__, length, data);
Alan Cox2742fd82008-04-29 14:45:15 +01001549
1550 if (length != 2) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001551 dev_dbg(dev, "%s - expecting packet of size 2, got %d\n", __func__, length);
Alan Cox2742fd82008-04-29 14:45:15 +01001552 goto exit;
1553 }
1554
1555 port_number = TIUMP_GET_PORT_FROM_CODE(data[0]);
1556 function = TIUMP_GET_FUNC_FROM_CODE(data[0]);
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001557 dev_dbg(dev, "%s - port_number %d, function %d, info 0x%x\n", __func__,
1558 port_number, function, data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 port = edge_serial->serial->port[port_number];
1560 edge_port = usb_get_serial_port_data(port);
1561 if (!edge_port) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001562 dev_dbg(dev, "%s - edge_port not found\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 return;
1564 }
1565 switch (function) {
1566 case TIUMP_INTERRUPT_CODE_LSR:
Alan Cox2742fd82008-04-29 14:45:15 +01001567 lsr = map_line_status(data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 if (lsr & UMP_UART_LSR_DATA_MASK) {
Alan Cox2742fd82008-04-29 14:45:15 +01001569 /* Save the LSR event for bulk read
1570 completion routine */
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001571 dev_dbg(dev, "%s - LSR Event Port %u LSR Status = %02x\n",
1572 __func__, port_number, lsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 edge_port->lsr_event = 1;
1574 edge_port->lsr_mask = lsr;
1575 } else {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001576 dev_dbg(dev, "%s - ===== Port %d LSR Status = %02x ======\n",
1577 __func__, port_number, lsr);
Alan Cox2742fd82008-04-29 14:45:15 +01001578 handle_new_lsr(edge_port, 0, lsr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 }
1580 break;
1581
Alan Cox2742fd82008-04-29 14:45:15 +01001582 case TIUMP_INTERRUPT_CODE_MSR: /* MSR */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 /* Copy MSR from UMP */
1584 msr = data[1];
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001585 dev_dbg(dev, "%s - ===== Port %u MSR Status = %02x ======\n",
1586 __func__, port_number, msr);
Alan Cox2742fd82008-04-29 14:45:15 +01001587 handle_new_msr(edge_port, msr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 break;
1589
1590 default:
Alan Cox2742fd82008-04-29 14:45:15 +01001591 dev_err(&urb->dev->dev,
1592 "%s - Unknown Interrupt code from UMP %x\n",
1593 __func__, data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 break;
Alan Cox2742fd82008-04-29 14:45:15 +01001595
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 }
1597
1598exit:
Alan Cox2742fd82008-04-29 14:45:15 +01001599 retval = usb_submit_urb(urb, GFP_ATOMIC);
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001600 if (retval)
Alan Cox2742fd82008-04-29 14:45:15 +01001601 dev_err(&urb->dev->dev,
1602 "%s - usb_submit_urb failed with result %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001603 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604}
1605
Alan Cox2742fd82008-04-29 14:45:15 +01001606static void edge_bulk_in_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607{
Ming Leicdc97792008-02-24 18:41:47 +08001608 struct edgeport_port *edge_port = urb->context;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001609 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001611 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 int port_number;
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001613 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001615 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 case 0:
1617 /* success */
1618 break;
1619 case -ECONNRESET:
1620 case -ENOENT:
1621 case -ESHUTDOWN:
1622 /* this urb is terminated, clean up */
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001623 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 return;
1625 default:
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001626 dev_err(&urb->dev->dev, "%s - nonzero read bulk status received: %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 }
1628
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001629 if (status == -EPIPE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 goto exit;
1631
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001632 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001633 dev_err(&urb->dev->dev, "%s - stopping read!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 return;
1635 }
1636
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001637 port_number = edge_port->port->port_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
1639 if (edge_port->lsr_event) {
1640 edge_port->lsr_event = 0;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001641 dev_dbg(dev, "%s ===== Port %u LSR Status = %02x, Data = %02x ======\n",
1642 __func__, port_number, edge_port->lsr_mask, *data);
Alan Cox2742fd82008-04-29 14:45:15 +01001643 handle_new_lsr(edge_port, 1, edge_port->lsr_mask, *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 /* Adjust buffer length/pointer */
1645 --urb->actual_length;
1646 ++data;
1647 }
1648
Jiri Slaby2e124b42013-01-03 15:53:06 +01001649 if (urb->actual_length) {
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001650 usb_serial_debug_data(dev, __func__, urb->actual_length, data);
Alan Cox2742fd82008-04-29 14:45:15 +01001651 if (edge_port->close_pending)
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001652 dev_dbg(dev, "%s - close pending, dropping data on the floor\n",
Alan Cox2742fd82008-04-29 14:45:15 +01001653 __func__);
1654 else
Jiri Slaby2e124b42013-01-03 15:53:06 +01001655 edge_tty_recv(edge_port->port, data,
Jiri Slaby05c7cd32013-01-03 15:53:04 +01001656 urb->actual_length);
Johan Hovoldcf9a9d62013-03-21 12:37:09 +01001657 edge_port->port->icount.rx += urb->actual_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 }
1659
1660exit:
1661 /* continue read unless stopped */
1662 spin_lock(&edge_port->ep_lock);
Johan Hovold58330412011-11-06 19:06:28 +01001663 if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001664 retval = usb_submit_urb(urb, GFP_ATOMIC);
Johan Hovold58330412011-11-06 19:06:28 +01001665 else if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPED;
Johan Hovold58330412011-11-06 19:06:28 +01001667
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 spin_unlock(&edge_port->ep_lock);
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001669 if (retval)
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001670 dev_err(dev, "%s - usb_submit_urb failed with result %d\n", __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671}
1672
Jiri Slaby2e124b42013-01-03 15:53:06 +01001673static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
1674 int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675{
Alan Cox2742fd82008-04-29 14:45:15 +01001676 int queued;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Jiri Slaby05c7cd32013-01-03 15:53:04 +01001678 queued = tty_insert_flip_string(&port->port, data, length);
Alan Cox2742fd82008-04-29 14:45:15 +01001679 if (queued < length)
Jiri Slaby05c7cd32013-01-03 15:53:04 +01001680 dev_err(&port->dev, "%s - dropping data, %d bytes lost\n",
Alan Cox2742fd82008-04-29 14:45:15 +01001681 __func__, length - queued);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001682 tty_flip_buffer_push(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683}
1684
Alan Cox2742fd82008-04-29 14:45:15 +01001685static void edge_bulk_out_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686{
Ming Leicdc97792008-02-24 18:41:47 +08001687 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001689 int status = urb->status;
Alan Cox4a90f092008-10-13 10:39:46 +01001690 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 edge_port->ep_write_urb_in_use = 0;
1693
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001694 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 case 0:
1696 /* success */
1697 break;
1698 case -ECONNRESET:
1699 case -ENOENT:
1700 case -ESHUTDOWN:
1701 /* this urb is terminated, clean up */
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001702 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001703 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 return;
1705 default:
Johan Hovold22a416c2012-02-10 13:20:51 +01001706 dev_err_console(port, "%s - nonzero write bulk status "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001707 "received: %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 }
1709
1710 /* send any buffered data */
Alan Cox4a90f092008-10-13 10:39:46 +01001711 tty = tty_port_tty_get(&port->port);
Jiri Slabyae3759c2013-04-04 22:41:01 +02001712 edge_send(port, tty);
Alan Cox4a90f092008-10-13 10:39:46 +01001713 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714}
1715
Alan Coxa509a7e2009-09-19 13:13:26 -07001716static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717{
1718 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1719 struct edgeport_serial *edge_serial;
1720 struct usb_device *dev;
1721 struct urb *urb;
1722 int port_number;
1723 int status;
1724 u16 open_settings;
1725 u8 transaction_timeout;
1726
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 if (edge_port == NULL)
1728 return -ENODEV;
1729
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001730 port_number = port->port_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
1732 dev = port->serial->dev;
1733
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 /* turn off loopback */
Alan Cox2742fd82008-04-29 14:45:15 +01001735 status = ti_do_config(edge_port, UMPC_SET_CLR_LOOPBACK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001737 dev_err(&port->dev,
1738 "%s - cannot send clear loopback command, %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001739 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 return status;
1741 }
Alan Cox2742fd82008-04-29 14:45:15 +01001742
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 /* set up the port settings */
Alan Cox95da3102008-07-22 11:09:07 +01001744 if (tty)
Alan Coxadc8d742012-07-14 15:31:47 +01001745 edge_set_termios(tty, port, &tty->termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
1747 /* open up the port */
1748
1749 /* milliseconds to timeout for DMA transfer */
1750 transaction_timeout = 2;
1751
Alan Cox2742fd82008-04-29 14:45:15 +01001752 edge_port->ump_read_timeout =
1753 max(20, ((transaction_timeout * 3) / 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Alan Cox2742fd82008-04-29 14:45:15 +01001755 /* milliseconds to timeout for DMA transfer */
1756 open_settings = (u8)(UMP_DMA_MODE_CONTINOUS |
1757 UMP_PIPE_TRANS_TIMEOUT_ENA |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 (transaction_timeout << 2));
1759
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001760 dev_dbg(&port->dev, "%s - Sending UMPC_OPEN_PORT\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
1762 /* Tell TI to open and start the port */
Alan Cox2742fd82008-04-29 14:45:15 +01001763 status = send_cmd(dev, UMPC_OPEN_PORT,
1764 (u8)(UMPM_UART1_PORT + port_number), open_settings, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001766 dev_err(&port->dev, "%s - cannot send open command, %d\n",
1767 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 return status;
1769 }
1770
1771 /* Start the DMA? */
Alan Cox2742fd82008-04-29 14:45:15 +01001772 status = send_cmd(dev, UMPC_START_PORT,
1773 (u8)(UMPM_UART1_PORT + port_number), 0, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001775 dev_err(&port->dev, "%s - cannot send start DMA command, %d\n",
1776 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 return status;
1778 }
1779
1780 /* Clear TX and RX buffers in UMP */
Alan Cox2742fd82008-04-29 14:45:15 +01001781 status = purge_port(port, UMP_PORT_DIR_OUT | UMP_PORT_DIR_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001783 dev_err(&port->dev,
1784 "%s - cannot send clear buffers command, %d\n",
1785 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 return status;
1787 }
1788
1789 /* Read Initial MSR */
Alan Cox2742fd82008-04-29 14:45:15 +01001790 status = ti_vread_sync(dev, UMPC_READ_MSR, 0,
1791 (__u16)(UMPM_UART1_PORT + port_number),
1792 &edge_port->shadow_msr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001794 dev_err(&port->dev, "%s - cannot send read MSR command, %d\n",
1795 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 return status;
1797 }
1798
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001799 dev_dbg(&port->dev, "ShadowMSR 0x%X\n", edge_port->shadow_msr);
Alan Cox2742fd82008-04-29 14:45:15 +01001800
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 /* Set Initial MCR */
1802 edge_port->shadow_mcr = MCR_RTS | MCR_DTR;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001803 dev_dbg(&port->dev, "ShadowMCR 0x%X\n", edge_port->shadow_mcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
1805 edge_serial = edge_port->edge_serial;
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01001806 if (mutex_lock_interruptible(&edge_serial->es_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 return -ERESTARTSYS;
1808 if (edge_serial->num_ports_open == 0) {
Alan Cox2742fd82008-04-29 14:45:15 +01001809 /* we are the first port to open, post the interrupt urb */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 urb = edge_serial->serial->port[0]->interrupt_in_urb;
1811 if (!urb) {
Alan Cox2742fd82008-04-29 14:45:15 +01001812 dev_err(&port->dev,
1813 "%s - no interrupt urb present, exiting\n",
1814 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 status = -EINVAL;
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01001816 goto release_es_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 urb->context = edge_serial;
Alan Cox2742fd82008-04-29 14:45:15 +01001819 status = usb_submit_urb(urb, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001821 dev_err(&port->dev,
1822 "%s - usb_submit_urb failed with value %d\n",
1823 __func__, status);
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01001824 goto release_es_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 }
1826 }
1827
1828 /*
1829 * reset the data toggle on the bulk endpoints to work around bug in
1830 * host controllers where things get out of sync some times
1831 */
Alan Cox2742fd82008-04-29 14:45:15 +01001832 usb_clear_halt(dev, port->write_urb->pipe);
1833 usb_clear_halt(dev, port->read_urb->pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
1835 /* start up our bulk read urb */
1836 urb = port->read_urb;
1837 if (!urb) {
Alan Cox2742fd82008-04-29 14:45:15 +01001838 dev_err(&port->dev, "%s - no read urb present, exiting\n",
1839 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 status = -EINVAL;
1841 goto unlink_int_urb;
1842 }
1843 edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 urb->context = edge_port;
Alan Cox2742fd82008-04-29 14:45:15 +01001845 status = usb_submit_urb(urb, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001847 dev_err(&port->dev,
1848 "%s - read bulk usb_submit_urb failed with value %d\n",
1849 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 goto unlink_int_urb;
1851 }
1852
1853 ++edge_serial->num_ports_open;
1854
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01001855 goto release_es_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
1857unlink_int_urb:
1858 if (edge_port->edge_serial->num_ports_open == 0)
1859 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01001860release_es_lock:
1861 mutex_unlock(&edge_serial->es_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 return status;
1863}
1864
Alan Cox335f8512009-06-11 12:26:29 +01001865static void edge_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866{
1867 struct edgeport_serial *edge_serial;
1868 struct edgeport_port *edge_port;
Johan Hovoldaf581052012-03-26 20:31:38 +02001869 struct usb_serial *serial = port->serial;
Johan Hovold77de2512013-01-14 16:52:54 +01001870 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 int port_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 edge_serial = usb_get_serial_data(port->serial);
1874 edge_port = usb_get_serial_port_data(port);
Alan Cox2742fd82008-04-29 14:45:15 +01001875 if (edge_serial == NULL || edge_port == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 return;
Alan Cox2742fd82008-04-29 14:45:15 +01001877
1878 /* The bulkreadcompletion routine will check
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 * this flag and dump add read data */
1880 edge_port->close_pending = 1;
1881
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 usb_kill_urb(port->read_urb);
1883 usb_kill_urb(port->write_urb);
1884 edge_port->ep_write_urb_in_use = 0;
Johan Hovold77de2512013-01-14 16:52:54 +01001885 spin_lock_irqsave(&edge_port->ep_lock, flags);
Johan Hovoldddca16e2013-06-26 16:47:37 +02001886 kfifo_reset_out(&port->write_fifo);
Johan Hovold77de2512013-01-14 16:52:54 +01001887 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001889 dev_dbg(&port->dev, "%s - send umpc_close_port\n", __func__);
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001890 port_number = port->port_number;
Johan Hovoldbca87e92013-03-21 12:37:38 +01001891 send_cmd(serial->dev, UMPC_CLOSE_PORT,
1892 (__u8)(UMPM_UART1_PORT + port_number), 0, NULL, 0);
Johan Hovoldaf581052012-03-26 20:31:38 +02001893
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01001894 mutex_lock(&edge_serial->es_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 --edge_port->edge_serial->num_ports_open;
1896 if (edge_port->edge_serial->num_ports_open <= 0) {
1897 /* last port is now closed, let's shut down our interrupt urb */
1898 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
1899 edge_port->edge_serial->num_ports_open = 0;
1900 }
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01001901 mutex_unlock(&edge_serial->es_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 edge_port->close_pending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903}
1904
Alan Cox95da3102008-07-22 11:09:07 +01001905static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
1906 const unsigned char *data, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907{
1908 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 if (count == 0) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001911 dev_dbg(&port->dev, "%s - write request of 0 bytes\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 return 0;
1913 }
1914
1915 if (edge_port == NULL)
1916 return -ENODEV;
1917 if (edge_port->close_pending == 1)
1918 return -ENODEV;
1919
Johan Hovoldddca16e2013-06-26 16:47:37 +02001920 count = kfifo_in_locked(&port->write_fifo, data, count,
Johan Hovoldd733cec2010-05-19 00:01:37 +02001921 &edge_port->ep_lock);
Jiri Slabyae3759c2013-04-04 22:41:01 +02001922 edge_send(port, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
1924 return count;
1925}
1926
Jiri Slabyae3759c2013-04-04 22:41:01 +02001927static void edge_send(struct usb_serial_port *port, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928{
1929 int count, result;
1930 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 unsigned long flags;
1932
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 spin_lock_irqsave(&edge_port->ep_lock, flags);
1934
1935 if (edge_port->ep_write_urb_in_use) {
1936 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1937 return;
1938 }
1939
Johan Hovoldddca16e2013-06-26 16:47:37 +02001940 count = kfifo_out(&port->write_fifo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 port->write_urb->transfer_buffer,
1942 port->bulk_out_size);
1943
1944 if (count == 0) {
1945 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1946 return;
1947 }
1948
1949 edge_port->ep_write_urb_in_use = 1;
1950
1951 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1952
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001953 usb_serial_debug_data(&port->dev, __func__, count, port->write_urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
1955 /* set up our urb */
Johan Hovoldfd119612011-11-06 19:06:30 +01001956 port->write_urb->transfer_buffer_length = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
1958 /* send the data out the bulk port */
1959 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
1960 if (result) {
Johan Hovold22a416c2012-02-10 13:20:51 +01001961 dev_err_console(port,
Alan Cox2742fd82008-04-29 14:45:15 +01001962 "%s - failed submitting write urb, error %d\n",
1963 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 edge_port->ep_write_urb_in_use = 0;
Alan Cox2742fd82008-04-29 14:45:15 +01001965 /* TODO: reschedule edge_send */
1966 } else
Johan Hovoldcf9a9d62013-03-21 12:37:09 +01001967 edge_port->port->icount.tx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
1969 /* wakeup any process waiting for writes to complete */
1970 /* there is now more room in the buffer for new writes */
Alan Cox2742fd82008-04-29 14:45:15 +01001971 if (tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973}
1974
Alan Cox95da3102008-07-22 11:09:07 +01001975static int edge_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976{
Alan Cox95da3102008-07-22 11:09:07 +01001977 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1979 int room = 0;
1980 unsigned long flags;
1981
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 if (edge_port == NULL)
Alan Cox2742fd82008-04-29 14:45:15 +01001983 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 if (edge_port->close_pending == 1)
Alan Cox2742fd82008-04-29 14:45:15 +01001985 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
1987 spin_lock_irqsave(&edge_port->ep_lock, flags);
Johan Hovoldddca16e2013-06-26 16:47:37 +02001988 room = kfifo_avail(&port->write_fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1990
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001991 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 return room;
1993}
1994
Alan Cox95da3102008-07-22 11:09:07 +01001995static int edge_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996{
Alan Cox95da3102008-07-22 11:09:07 +01001997 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1999 int chars = 0;
2000 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 if (edge_port == NULL)
Alan Cox2742fd82008-04-29 14:45:15 +01002002 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
2004 spin_lock_irqsave(&edge_port->ep_lock, flags);
Johan Hovoldddca16e2013-06-26 16:47:37 +02002005 chars = kfifo_len(&port->write_fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2007
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002008 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 return chars;
2010}
2011
Johan Hovoldb16634a2013-05-05 20:32:31 +02002012static bool edge_tx_empty(struct usb_serial_port *port)
2013{
2014 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2015 int ret;
2016
2017 ret = tx_active(edge_port);
2018 if (ret > 0)
2019 return false;
2020
2021 return true;
2022}
2023
Alan Cox95da3102008-07-22 11:09:07 +01002024static void edge_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025{
Alan Cox95da3102008-07-22 11:09:07 +01002026 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 int status;
2029
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 if (edge_port == NULL)
2031 return;
2032
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 /* if we are implementing XON/XOFF, send the stop character */
2034 if (I_IXOFF(tty)) {
2035 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01002036 status = edge_write(tty, port, &stop_char, 1);
2037 if (status <= 0) {
2038 dev_err(&port->dev, "%s - failed to write stop character, %d\n", __func__, status);
2039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 }
2041
2042 /* if we are implementing RTS/CTS, stop reads */
2043 /* and the Edgeport will clear the RTS line */
2044 if (C_CRTSCTS(tty))
2045 stop_read(edge_port);
2046
2047}
2048
Alan Cox95da3102008-07-22 11:09:07 +01002049static void edge_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
Alan Cox95da3102008-07-22 11:09:07 +01002051 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 int status;
2054
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 if (edge_port == NULL)
2056 return;
2057
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 /* if we are implementing XON/XOFF, send the start character */
2059 if (I_IXOFF(tty)) {
2060 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01002061 status = edge_write(tty, port, &start_char, 1);
2062 if (status <= 0) {
2063 dev_err(&port->dev, "%s - failed to write start character, %d\n", __func__, status);
2064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 /* if we are implementing RTS/CTS, restart reads */
2067 /* are the Edgeport will assert the RTS line */
2068 if (C_CRTSCTS(tty)) {
2069 status = restart_read(edge_port);
2070 if (status)
Alan Cox2742fd82008-04-29 14:45:15 +01002071 dev_err(&port->dev,
2072 "%s - read bulk usb_submit_urb failed: %d\n",
2073 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 }
2075
2076}
2077
2078static void stop_read(struct edgeport_port *edge_port)
2079{
2080 unsigned long flags;
2081
2082 spin_lock_irqsave(&edge_port->ep_lock, flags);
2083
2084 if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
2085 edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPING;
2086 edge_port->shadow_mcr &= ~MCR_RTS;
2087
2088 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2089}
2090
2091static int restart_read(struct edgeport_port *edge_port)
2092{
2093 struct urb *urb;
2094 int status = 0;
2095 unsigned long flags;
2096
2097 spin_lock_irqsave(&edge_port->ep_lock, flags);
2098
2099 if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPED) {
2100 urb = edge_port->port->read_urb;
Oliver Neukumefdff602007-06-05 10:50:48 +02002101 status = usb_submit_urb(urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 }
2103 edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
2104 edge_port->shadow_mcr |= MCR_RTS;
2105
2106 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2107
2108 return status;
2109}
2110
Alan Cox95da3102008-07-22 11:09:07 +01002111static void change_port_settings(struct tty_struct *tty,
2112 struct edgeport_port *edge_port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113{
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002114 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 struct ump_uart_config *config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 int baud;
2117 unsigned cflag;
2118 int status;
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07002119 int port_number = edge_port->port->port_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Alan Cox95da3102008-07-22 11:09:07 +01002121 config = kmalloc (sizeof (*config), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 if (!config) {
Alan Coxadc8d742012-07-14 15:31:47 +01002123 tty->termios = *old_termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 return;
2125 }
2126
Alan Coxadc8d742012-07-14 15:31:47 +01002127 cflag = tty->termios.c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
2129 config->wFlags = 0;
2130
2131 /* These flags must be set */
2132 config->wFlags |= UMP_MASK_UART_FLAGS_RECEIVE_MS_INT;
2133 config->wFlags |= UMP_MASK_UART_FLAGS_AUTO_START_ON_ERR;
2134 config->bUartMode = (__u8)(edge_port->bUartMode);
2135
2136 switch (cflag & CSIZE) {
Alan Cox2742fd82008-04-29 14:45:15 +01002137 case CS5:
2138 config->bDataBits = UMP_UART_CHAR5BITS;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002139 dev_dbg(dev, "%s - data bits = 5\n", __func__);
Alan Cox2742fd82008-04-29 14:45:15 +01002140 break;
2141 case CS6:
2142 config->bDataBits = UMP_UART_CHAR6BITS;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002143 dev_dbg(dev, "%s - data bits = 6\n", __func__);
Alan Cox2742fd82008-04-29 14:45:15 +01002144 break;
2145 case CS7:
2146 config->bDataBits = UMP_UART_CHAR7BITS;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002147 dev_dbg(dev, "%s - data bits = 7\n", __func__);
Alan Cox2742fd82008-04-29 14:45:15 +01002148 break;
2149 default:
2150 case CS8:
2151 config->bDataBits = UMP_UART_CHAR8BITS;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002152 dev_dbg(dev, "%s - data bits = 8\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 break;
2154 }
2155
2156 if (cflag & PARENB) {
2157 if (cflag & PARODD) {
2158 config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
2159 config->bParity = UMP_UART_ODDPARITY;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002160 dev_dbg(dev, "%s - parity = odd\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 } else {
2162 config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
2163 config->bParity = UMP_UART_EVENPARITY;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002164 dev_dbg(dev, "%s - parity = even\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 }
2166 } else {
Alan Cox2742fd82008-04-29 14:45:15 +01002167 config->bParity = UMP_UART_NOPARITY;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002168 dev_dbg(dev, "%s - parity = none\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 }
2170
2171 if (cflag & CSTOPB) {
2172 config->bStopBits = UMP_UART_STOPBIT2;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002173 dev_dbg(dev, "%s - stop bits = 2\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 } else {
2175 config->bStopBits = UMP_UART_STOPBIT1;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002176 dev_dbg(dev, "%s - stop bits = 1\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 }
2178
2179 /* figure out the flow control settings */
2180 if (cflag & CRTSCTS) {
2181 config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X_CTS_FLOW;
2182 config->wFlags |= UMP_MASK_UART_FLAGS_RTS_FLOW;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002183 dev_dbg(dev, "%s - RTS/CTS is enabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 } else {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002185 dev_dbg(dev, "%s - RTS/CTS is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 restart_read(edge_port);
2187 }
2188
Alan Cox2742fd82008-04-29 14:45:15 +01002189 /* if we are implementing XON/XOFF, set the start and stop
2190 character in the device */
2191 config->cXon = START_CHAR(tty);
2192 config->cXoff = STOP_CHAR(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
Alan Cox2742fd82008-04-29 14:45:15 +01002194 /* if we are implementing INBOUND XON/XOFF */
2195 if (I_IXOFF(tty)) {
2196 config->wFlags |= UMP_MASK_UART_FLAGS_IN_X;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002197 dev_dbg(dev, "%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2198 __func__, config->cXon, config->cXoff);
Alan Cox2742fd82008-04-29 14:45:15 +01002199 } else
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002200 dev_dbg(dev, "%s - INBOUND XON/XOFF is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
Alan Cox2742fd82008-04-29 14:45:15 +01002202 /* if we are implementing OUTBOUND XON/XOFF */
2203 if (I_IXON(tty)) {
2204 config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002205 dev_dbg(dev, "%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2206 __func__, config->cXon, config->cXoff);
Alan Cox2742fd82008-04-29 14:45:15 +01002207 } else
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002208 dev_dbg(dev, "%s - OUTBOUND XON/XOFF is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Alan Coxadc8d742012-07-14 15:31:47 +01002210 tty->termios.c_cflag &= ~CMSPAR;
Alan Coxd5f5bcd2008-01-03 16:57:33 +00002211
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 /* Round the baud rate */
2213 baud = tty_get_baud_rate(tty);
2214 if (!baud) {
2215 /* pick a default, any default... */
2216 baud = 9600;
Alan Coxd5f5bcd2008-01-03 16:57:33 +00002217 } else
2218 tty_encode_baud_rate(tty, baud, baud);
2219
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 edge_port->baud_rate = baud;
2221 config->wBaudRate = (__u16)((461550L + baud/2) / baud);
2222
Alan Coxd5f5bcd2008-01-03 16:57:33 +00002223 /* FIXME: Recompute actual baud from divisor here */
2224
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002225 dev_dbg(dev, "%s - baud rate = %d, wBaudRate = %d\n", __func__, baud, config->wBaudRate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002227 dev_dbg(dev, "wBaudRate: %d\n", (int)(461550L / config->wBaudRate));
2228 dev_dbg(dev, "wFlags: 0x%x\n", config->wFlags);
2229 dev_dbg(dev, "bDataBits: %d\n", config->bDataBits);
2230 dev_dbg(dev, "bParity: %d\n", config->bParity);
2231 dev_dbg(dev, "bStopBits: %d\n", config->bStopBits);
2232 dev_dbg(dev, "cXon: %d\n", config->cXon);
2233 dev_dbg(dev, "cXoff: %d\n", config->cXoff);
2234 dev_dbg(dev, "bUartMode: %d\n", config->bUartMode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
2236 /* move the word values into big endian mode */
Alan Cox2742fd82008-04-29 14:45:15 +01002237 cpu_to_be16s(&config->wFlags);
2238 cpu_to_be16s(&config->wBaudRate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Alan Cox2742fd82008-04-29 14:45:15 +01002240 status = send_cmd(edge_port->port->serial->dev, UMPC_SET_CONFIG,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 (__u8)(UMPM_UART1_PORT + port_number),
Alan Cox2742fd82008-04-29 14:45:15 +01002242 0, (__u8 *)config, sizeof(*config));
2243 if (status)
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002244 dev_dbg(dev, "%s - error %d when trying to write config to device\n",
2245 __func__, status);
Alan Cox2742fd82008-04-29 14:45:15 +01002246 kfree(config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247}
2248
Alan Cox2e0ddd62008-07-22 11:12:33 +01002249static void edge_set_termios(struct tty_struct *tty,
Alan Cox95da3102008-07-22 11:09:07 +01002250 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251{
2252 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Alan Cox95da3102008-07-22 11:09:07 +01002253 unsigned int cflag;
2254
Alan Coxadc8d742012-07-14 15:31:47 +01002255 cflag = tty->termios.c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002257 dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__,
Linus Torvaldsd9a80742012-10-01 13:23:01 -07002258 tty->termios.c_cflag, tty->termios.c_iflag);
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002259 dev_dbg(&port->dev, "%s - old clfag %08x old iflag %08x\n", __func__,
2260 old_termios->c_cflag, old_termios->c_iflag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
2262 if (edge_port == NULL)
2263 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 /* change the port settings to the new ones specified */
Alan Cox95da3102008-07-22 11:09:07 +01002265 change_port_settings(tty, edge_port, old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266}
2267
Alan Cox20b9d172011-02-14 16:26:50 +00002268static int edge_tiocmset(struct tty_struct *tty,
Alan Cox2742fd82008-04-29 14:45:15 +01002269 unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270{
Alan Cox95da3102008-07-22 11:09:07 +01002271 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2273 unsigned int mcr;
Alan Cox3d71fe02008-02-20 21:38:32 +00002274 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
Alan Cox3d71fe02008-02-20 21:38:32 +00002276 spin_lock_irqsave(&edge_port->ep_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 mcr = edge_port->shadow_mcr;
2278 if (set & TIOCM_RTS)
2279 mcr |= MCR_RTS;
2280 if (set & TIOCM_DTR)
2281 mcr |= MCR_DTR;
2282 if (set & TIOCM_LOOP)
2283 mcr |= MCR_LOOPBACK;
2284
2285 if (clear & TIOCM_RTS)
2286 mcr &= ~MCR_RTS;
2287 if (clear & TIOCM_DTR)
2288 mcr &= ~MCR_DTR;
2289 if (clear & TIOCM_LOOP)
2290 mcr &= ~MCR_LOOPBACK;
2291
2292 edge_port->shadow_mcr = mcr;
Alan Cox3d71fe02008-02-20 21:38:32 +00002293 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
Alan Cox2742fd82008-04-29 14:45:15 +01002295 restore_mcr(edge_port, mcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 return 0;
2297}
2298
Alan Cox60b33c12011-02-14 16:26:14 +00002299static int edge_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300{
Alan Cox95da3102008-07-22 11:09:07 +01002301 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2303 unsigned int result = 0;
2304 unsigned int msr;
2305 unsigned int mcr;
Alan Cox3d71fe02008-02-20 21:38:32 +00002306 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
Alan Cox3d71fe02008-02-20 21:38:32 +00002308 spin_lock_irqsave(&edge_port->ep_lock, flags);
2309
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 msr = edge_port->shadow_msr;
2311 mcr = edge_port->shadow_mcr;
2312 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
2313 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
2314 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
2315 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
2316 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
2317 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
2318
2319
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002320 dev_dbg(&port->dev, "%s -- %x\n", __func__, result);
Alan Cox3d71fe02008-02-20 21:38:32 +00002321 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
2323 return result;
2324}
2325
Alan Cox2742fd82008-04-29 14:45:15 +01002326static int get_serial_info(struct edgeport_port *edge_port,
2327 struct serial_struct __user *retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328{
2329 struct serial_struct tmp;
Johan Hovoldf40d7812013-01-14 16:52:58 +01002330 unsigned cwait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
2332 if (!retinfo)
2333 return -EFAULT;
2334
Johan Hovoldf40d7812013-01-14 16:52:58 +01002335 cwait = edge_port->port->port.closing_wait;
2336 if (cwait != ASYNC_CLOSING_WAIT_NONE)
Johan Hovoldb6fd35e2013-04-18 17:33:17 +02002337 cwait = jiffies_to_msecs(cwait) / 10;
Johan Hovoldf40d7812013-01-14 16:52:58 +01002338
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 memset(&tmp, 0, sizeof(tmp));
2340
2341 tmp.type = PORT_16550A;
Greg Kroah-Hartmane5b1e202013-06-07 11:04:28 -07002342 tmp.line = edge_port->port->minor;
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07002343 tmp.port = edge_port->port->port_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 tmp.irq = 0;
2345 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2346 tmp.xmit_fifo_size = edge_port->port->bulk_out_size;
2347 tmp.baud_base = 9600;
2348 tmp.close_delay = 5*HZ;
Johan Hovoldf40d7812013-01-14 16:52:58 +01002349 tmp.closing_wait = cwait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
2351 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2352 return -EFAULT;
2353 return 0;
2354}
2355
Alan Cox00a0d0d2011-02-14 16:27:06 +00002356static int edge_ioctl(struct tty_struct *tty,
Alan Cox2742fd82008-04-29 14:45:15 +01002357 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358{
Alan Cox95da3102008-07-22 11:09:07 +01002359 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 switch (cmd) {
Alan Cox2742fd82008-04-29 14:45:15 +01002363 case TIOCGSERIAL:
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002364 dev_dbg(&port->dev, "%s - TIOCGSERIAL\n", __func__);
Alan Cox2742fd82008-04-29 14:45:15 +01002365 return get_serial_info(edge_port,
2366 (struct serial_struct __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 return -ENOIOCTLCMD;
2369}
2370
Alan Cox95da3102008-07-22 11:09:07 +01002371static void edge_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372{
Alan Cox95da3102008-07-22 11:09:07 +01002373 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2375 int status;
Alan Cox2742fd82008-04-29 14:45:15 +01002376 int bv = 0; /* Off */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
Alan Cox95da3102008-07-22 11:09:07 +01002378 if (break_state == -1)
Alan Cox2742fd82008-04-29 14:45:15 +01002379 bv = 1; /* On */
2380 status = ti_do_config(edge_port, UMPC_SET_CLR_BREAK, bv);
2381 if (status)
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002382 dev_dbg(&port->dev, "%s - error %d sending break set/clear command.\n",
2383 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384}
2385
Alan Cox2742fd82008-04-29 14:45:15 +01002386static int edge_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387{
2388 struct edgeport_serial *edge_serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 int status;
Peter E. Bergerc0e34832015-07-31 01:55:06 -05002390 const struct firmware *fw;
2391 const char *fw_name = "edgeport/down3.bin";
2392 struct device *dev = &serial->interface->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
2394 /* create our private serial structure */
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +01002395 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
Johan Hovold10c642d2013-12-29 19:22:56 +01002396 if (!edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 return -ENOMEM;
Johan Hovold10c642d2013-12-29 19:22:56 +01002398
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01002399 mutex_init(&edge_serial->es_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 edge_serial->serial = serial;
2401 usb_set_serial_data(serial, edge_serial);
2402
Peter E. Bergerc0e34832015-07-31 01:55:06 -05002403 status = request_firmware(&fw, fw_name, dev);
2404 if (status) {
2405 dev_err(dev, "Failed to load image \"%s\" err %d\n",
2406 fw_name, status);
2407 kfree(edge_serial);
2408 return status;
2409 }
2410
2411 status = download_fw(edge_serial, fw);
2412 release_firmware(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01002414 kfree(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 return status;
2416 }
2417
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419}
2420
Alan Sternf9c99bb2009-06-02 11:53:55 -04002421static void edge_disconnect(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422{
Alan Sternf9c99bb2009-06-02 11:53:55 -04002423}
2424
2425static void edge_release(struct usb_serial *serial)
2426{
Jesper Juhl0d46c002007-07-16 22:17:25 +02002427 kfree(usb_get_serial_data(serial));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428}
2429
Johan Hovold00361532012-10-17 13:34:58 +02002430static int edge_port_probe(struct usb_serial_port *port)
2431{
2432 struct edgeport_port *edge_port;
2433 int ret;
2434
2435 edge_port = kzalloc(sizeof(*edge_port), GFP_KERNEL);
2436 if (!edge_port)
2437 return -ENOMEM;
2438
Johan Hovold00361532012-10-17 13:34:58 +02002439 spin_lock_init(&edge_port->ep_lock);
2440 edge_port->port = port;
2441 edge_port->edge_serial = usb_get_serial_data(port->serial);
2442 edge_port->bUartMode = default_uart_mode;
2443
Johan Hovold0fce06d2013-06-26 16:47:38 +02002444 switch (port->port_number) {
2445 case 0:
2446 edge_port->uart_base = UMPMEM_BASE_UART1;
2447 edge_port->dma_address = UMPD_OEDB1_ADDRESS;
2448 break;
2449 case 1:
2450 edge_port->uart_base = UMPMEM_BASE_UART2;
2451 edge_port->dma_address = UMPD_OEDB2_ADDRESS;
2452 break;
2453 default:
2454 dev_err(&port->dev, "unknown port number\n");
2455 ret = -ENODEV;
2456 goto err;
2457 }
2458
2459 dev_dbg(&port->dev,
2460 "%s - port_number = %d, uart_base = %04x, dma_address = %04x\n",
2461 __func__, port->port_number, edge_port->uart_base,
2462 edge_port->dma_address);
2463
Johan Hovold00361532012-10-17 13:34:58 +02002464 usb_set_serial_port_data(port, edge_port);
2465
Johan Hovold5d8c61b2012-10-18 11:43:28 +02002466 ret = edge_create_sysfs_attrs(port);
Johan Hovold0fce06d2013-06-26 16:47:38 +02002467 if (ret)
2468 goto err;
Johan Hovold5d8c61b2012-10-18 11:43:28 +02002469
Johan Hovoldf40d7812013-01-14 16:52:58 +01002470 port->port.closing_wait = msecs_to_jiffies(closing_wait * 10);
Johan Hovoldd7be6222013-06-26 16:47:23 +02002471 port->port.drain_delay = 1;
Johan Hovoldf40d7812013-01-14 16:52:58 +01002472
Johan Hovold00361532012-10-17 13:34:58 +02002473 return 0;
Johan Hovold0fce06d2013-06-26 16:47:38 +02002474err:
2475 kfree(edge_port);
2476
2477 return ret;
Johan Hovold00361532012-10-17 13:34:58 +02002478}
2479
2480static int edge_port_remove(struct usb_serial_port *port)
2481{
2482 struct edgeport_port *edge_port;
2483
2484 edge_port = usb_get_serial_port_data(port);
Johan Hovold00361532012-10-17 13:34:58 +02002485 edge_remove_sysfs_attrs(port);
Johan Hovold00361532012-10-17 13:34:58 +02002486 kfree(edge_port);
2487
2488 return 0;
2489}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002491/* Sysfs Attributes */
2492
Greg Kroah-Hartman154547c2013-08-23 16:18:34 -07002493static ssize_t uart_mode_show(struct device *dev,
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002494 struct device_attribute *attr, char *buf)
2495{
2496 struct usb_serial_port *port = to_usb_serial_port(dev);
2497 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2498
2499 return sprintf(buf, "%d\n", edge_port->bUartMode);
2500}
2501
Greg Kroah-Hartman154547c2013-08-23 16:18:34 -07002502static ssize_t uart_mode_store(struct device *dev,
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002503 struct device_attribute *attr, const char *valbuf, size_t count)
2504{
2505 struct usb_serial_port *port = to_usb_serial_port(dev);
2506 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2507 unsigned int v = simple_strtoul(valbuf, NULL, 0);
2508
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002509 dev_dbg(dev, "%s: setting uart_mode = %d\n", __func__, v);
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002510
2511 if (v < 256)
2512 edge_port->bUartMode = v;
2513 else
Harvey Harrison441b62c2008-03-03 16:08:34 -08002514 dev_err(dev, "%s - uart_mode %d is invalid\n", __func__, v);
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002515
2516 return count;
2517}
Greg Kroah-Hartman154547c2013-08-23 16:18:34 -07002518static DEVICE_ATTR_RW(uart_mode);
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002519
2520static int edge_create_sysfs_attrs(struct usb_serial_port *port)
2521{
2522 return device_create_file(&port->dev, &dev_attr_uart_mode);
2523}
2524
2525static int edge_remove_sysfs_attrs(struct usb_serial_port *port)
2526{
2527 device_remove_file(&port->dev, &dev_attr_uart_mode);
2528 return 0;
2529}
2530
2531
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -07002532static struct usb_serial_driver edgeport_1port_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07002533 .driver = {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07002534 .owner = THIS_MODULE,
2535 .name = "edgeport_ti_1",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07002536 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07002537 .description = "Edgeport TI 1 port adapter",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 .id_table = edgeport_1port_id_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 .num_ports = 1,
2540 .open = edge_open,
2541 .close = edge_close,
2542 .throttle = edge_throttle,
2543 .unthrottle = edge_unthrottle,
2544 .attach = edge_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04002545 .disconnect = edge_disconnect,
2546 .release = edge_release,
Johan Hovold00361532012-10-17 13:34:58 +02002547 .port_probe = edge_port_probe,
2548 .port_remove = edge_port_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 .ioctl = edge_ioctl,
2550 .set_termios = edge_set_termios,
2551 .tiocmget = edge_tiocmget,
2552 .tiocmset = edge_tiocmset,
Johan Hovold48ee5802013-03-21 12:37:10 +01002553 .tiocmiwait = usb_serial_generic_tiocmiwait,
Johan Hovoldcf9a9d62013-03-21 12:37:09 +01002554 .get_icount = usb_serial_generic_get_icount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 .write = edge_write,
2556 .write_room = edge_write_room,
2557 .chars_in_buffer = edge_chars_in_buffer,
Johan Hovoldb16634a2013-05-05 20:32:31 +02002558 .tx_empty = edge_tx_empty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 .break_ctl = edge_break,
2560 .read_int_callback = edge_interrupt_callback,
2561 .read_bulk_callback = edge_bulk_in_callback,
2562 .write_bulk_callback = edge_bulk_out_callback,
2563};
2564
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -07002565static struct usb_serial_driver edgeport_2port_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07002566 .driver = {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07002567 .owner = THIS_MODULE,
2568 .name = "edgeport_ti_2",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07002569 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07002570 .description = "Edgeport TI 2 port adapter",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 .id_table = edgeport_2port_id_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 .num_ports = 2,
2573 .open = edge_open,
2574 .close = edge_close,
2575 .throttle = edge_throttle,
2576 .unthrottle = edge_unthrottle,
2577 .attach = edge_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04002578 .disconnect = edge_disconnect,
2579 .release = edge_release,
Johan Hovold00361532012-10-17 13:34:58 +02002580 .port_probe = edge_port_probe,
2581 .port_remove = edge_port_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 .ioctl = edge_ioctl,
2583 .set_termios = edge_set_termios,
2584 .tiocmget = edge_tiocmget,
2585 .tiocmset = edge_tiocmset,
Johan Hovold48ee5802013-03-21 12:37:10 +01002586 .tiocmiwait = usb_serial_generic_tiocmiwait,
Johan Hovoldcf9a9d62013-03-21 12:37:09 +01002587 .get_icount = usb_serial_generic_get_icount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 .write = edge_write,
2589 .write_room = edge_write_room,
2590 .chars_in_buffer = edge_chars_in_buffer,
Johan Hovoldb16634a2013-05-05 20:32:31 +02002591 .tx_empty = edge_tx_empty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 .break_ctl = edge_break,
2593 .read_int_callback = edge_interrupt_callback,
2594 .read_bulk_callback = edge_bulk_in_callback,
2595 .write_bulk_callback = edge_bulk_out_callback,
2596};
2597
Alan Stern7dbe2462012-02-23 14:56:57 -05002598static struct usb_serial_driver * const serial_drivers[] = {
2599 &edgeport_1port_device, &edgeport_2port_device, NULL
2600};
2601
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07002602module_usb_serial_driver(serial_drivers, id_table_combined);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604MODULE_AUTHOR(DRIVER_AUTHOR);
2605MODULE_DESCRIPTION(DRIVER_DESC);
2606MODULE_LICENSE("GPL");
Jaswinder Singhd12b2192008-07-04 23:06:09 +05302607MODULE_FIRMWARE("edgeport/down3.bin");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609module_param(closing_wait, int, S_IRUGO | S_IWUSR);
2610MODULE_PARM_DESC(closing_wait, "Maximum wait for data to drain, in .01 secs");
2611
2612module_param(ignore_cpu_rev, bool, S_IRUGO | S_IWUSR);
Alan Cox2742fd82008-04-29 14:45:15 +01002613MODULE_PARM_DESC(ignore_cpu_rev,
2614 "Ignore the cpu revision when connecting to a device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002616module_param(default_uart_mode, int, S_IRUGO | S_IWUSR);
2617MODULE_PARM_DESC(default_uart_mode, "Default uart_mode, 0=RS232, ...");