blob: 6031ede90433b05399ec47f58d8798114d1d1664 [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 */
Peter E. Berger75899512015-09-16 03:13:02 -050057#define TI_MODE_TRANSITIONING 3 /*
58 * Currently in boot mode but
59 * transitioning to download mode
60 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62/* read urb state */
63#define EDGE_READ_URB_RUNNING 0
64#define EDGE_READ_URB_STOPPING 1
65#define EDGE_READ_URB_STOPPED 2
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#define EDGE_CLOSING_WAIT 4000 /* in .01 sec */
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70/* Product information read from the Edgeport */
Alan Cox2742fd82008-04-29 14:45:15 +010071struct product_info {
72 int TiMode; /* Current TI Mode */
73 __u8 hardware_type; /* Type of hardware */
Linus Torvalds1da177e2005-04-16 15:20:36 -070074} __attribute__((packed));
75
Peter E. Bergerc0e34832015-07-31 01:55:06 -050076/*
77 * Edgeport firmware header
78 *
79 * "build_number" has been set to 0 in all three of the images I have
80 * seen, and Digi Tech Support suggests that it is safe to ignore it.
81 *
82 * "length" is the number of bytes of actual data following the header.
83 *
84 * "checksum" is the low order byte resulting from adding the values of
85 * all the data bytes.
86 */
87struct edgeport_fw_hdr {
88 u8 major_version;
89 u8 minor_version;
90 __le16 build_number;
91 __le16 length;
92 u8 checksum;
93} __packed;
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095struct edgeport_port {
96 __u16 uart_base;
97 __u16 dma_address;
98 __u8 shadow_msr;
99 __u8 shadow_mcr;
100 __u8 shadow_lsr;
101 __u8 lsr_mask;
Peter E. Berger75899512015-09-16 03:13:02 -0500102 __u32 ump_read_timeout; /*
103 * Number of milliseconds the UMP will
104 * wait without data before completing
105 * a read short
106 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 int baud_rate;
108 int close_pending;
109 int lsr_event;
Johan Hovold48ee5802013-03-21 12:37:10 +0100110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 struct edgeport_serial *edge_serial;
112 struct usb_serial_port *port;
Alan Cox2742fd82008-04-29 14:45:15 +0100113 __u8 bUartMode; /* Port type, 0: RS232, etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 spinlock_t ep_lock;
115 int ep_read_urb_state;
116 int ep_write_urb_in_use;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117};
118
119struct edgeport_serial {
120 struct product_info product_info;
Alan Cox2742fd82008-04-29 14:45:15 +0100121 u8 TI_I2C_Type; /* Type of I2C in UMP */
Peter E. Berger75899512015-09-16 03:13:02 -0500122 u8 TiReadI2C; /*
123 * Set to TRUE if we have read the
124 * I2c in Boot Mode
125 */
Matthias Kaehlcke241ca642007-12-04 16:15:40 +0100126 struct mutex es_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 int num_ports_open;
128 struct usb_serial *serial;
Peter E. Berger26c78da2015-07-31 01:55:08 -0500129 struct delayed_work heartbeat_work;
Peter E. Bergerc0e34832015-07-31 01:55:06 -0500130 int fw_version;
Peter E. Berger26c78da2015-07-31 01:55:08 -0500131 bool use_heartbeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132};
133
134
135/* Devices that this driver supports */
Németh Márton7d40d7e2010-01-10 15:34:24 +0100136static const struct usb_device_id edgeport_1port_id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
138 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
139 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
140 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
141 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
142 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
143 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
144 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
145 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
146 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
147 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
148 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
149 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
150 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
151 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
152 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
153 { }
154};
155
Németh Márton7d40d7e2010-01-10 15:34:24 +0100156static const struct usb_device_id edgeport_2port_id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
158 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
159 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
160 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
161 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
162 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
163 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
164 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
165 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
166 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
167 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
168 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -0400169 /* The 4, 8 and 16 port devices show up as multiple 2 port devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -0400171 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
172 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
173 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
174 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 { }
176};
177
178/* Devices that this driver supports */
Németh Márton7d40d7e2010-01-10 15:34:24 +0100179static const struct usb_device_id id_table_combined[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
181 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
182 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
183 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
184 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
185 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
186 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
187 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
188 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
189 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
190 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
191 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
192 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
193 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
194 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
195 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
196 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
197 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
198 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
199 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
200 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
201 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
202 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
203 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
204 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
205 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
206 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
207 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
208 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -0400209 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
210 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
211 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
212 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 { }
214};
215
Alan Cox2742fd82008-04-29 14:45:15 +0100216MODULE_DEVICE_TABLE(usb, id_table_combined);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218static int closing_wait = EDGE_CLOSING_WAIT;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030219static bool ignore_cpu_rev;
Alan Cox2742fd82008-04-29 14:45:15 +0100220static int default_uart_mode; /* RS232 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Jiri Slaby2e124b42013-01-03 15:53:06 +0100222static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
223 int length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225static void stop_read(struct edgeport_port *edge_port);
226static int restart_read(struct edgeport_port *edge_port);
227
Alan Cox95da3102008-07-22 11:09:07 +0100228static void edge_set_termios(struct tty_struct *tty,
229 struct usb_serial_port *port, struct ktermios *old_termios);
Jiri Slabyae3759c2013-04-04 22:41:01 +0200230static void edge_send(struct usb_serial_port *port, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Peter E. Bergerbebf1f12015-09-16 03:13:00 -0500232static int do_download_mode(struct edgeport_serial *serial,
233 const struct firmware *fw);
234static int do_boot_mode(struct edgeport_serial *serial,
235 const struct firmware *fw);
236
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -0400237/* sysfs attributes */
238static int edge_create_sysfs_attrs(struct usb_serial_port *port);
239static int edge_remove_sysfs_attrs(struct usb_serial_port *port);
240
Peter E. Berger26c78da2015-07-31 01:55:08 -0500241/*
242 * Some release of Edgeport firmware "down3.bin" after version 4.80
243 * introduced code to automatically disconnect idle devices on some
244 * Edgeport models after periods of inactivity, typically ~60 seconds.
245 * This occurs without regard to whether ports on the device are open
246 * or not. Digi International Tech Support suggested:
247 *
248 * 1. Adding driver "heartbeat" code to reset the firmware timer by
249 * requesting a descriptor record every 15 seconds, which should be
250 * effective with newer firmware versions that require it, and benign
251 * with older versions that do not. In practice 40 seconds seems often
252 * enough.
253 * 2. The heartbeat code is currently required only on Edgeport/416 models.
254 */
255#define FW_HEARTBEAT_VERSION_CUTOFF ((4 << 8) + 80)
256#define FW_HEARTBEAT_SECS 40
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Peter E. Bergerc3ece7e2015-07-31 01:55:05 -0500258/* Timeouts in msecs: firmware downloads take longer */
259#define TI_VSEND_TIMEOUT_DEFAULT 1000
260#define TI_VSEND_TIMEOUT_FW_DOWNLOAD 10000
261
Alan Cox2742fd82008-04-29 14:45:15 +0100262static int ti_vread_sync(struct usb_device *dev, __u8 request,
263 __u16 value, __u16 index, u8 *data, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
265 int status;
266
Alan Cox2742fd82008-04-29 14:45:15 +0100267 status = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request,
268 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
269 value, index, data, size, 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 if (status < 0)
271 return status;
272 if (status != size) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700273 dev_dbg(&dev->dev, "%s - wanted to write %d, but only wrote %d\n",
274 __func__, size, status);
Alan Cox2742fd82008-04-29 14:45:15 +0100275 return -ECOMM;
276 }
277 return 0;
278}
279
Peter E. Bergerc3ece7e2015-07-31 01:55:05 -0500280static int ti_vsend_sync(struct usb_device *dev, u8 request, u16 value,
281 u16 index, u8 *data, int size, int timeout)
Alan Cox2742fd82008-04-29 14:45:15 +0100282{
283 int status;
284
285 status = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
286 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
Peter E. Bergerc3ece7e2015-07-31 01:55:05 -0500287 value, index, data, size, timeout);
Alan Cox2742fd82008-04-29 14:45:15 +0100288 if (status < 0)
289 return status;
290 if (status != size) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700291 dev_dbg(&dev->dev, "%s - wanted to write %d, but only wrote %d\n",
292 __func__, size, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return -ECOMM;
294 }
295 return 0;
296}
297
Alan Cox2742fd82008-04-29 14:45:15 +0100298static int send_cmd(struct usb_device *dev, __u8 command,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 __u8 moduleid, __u16 value, u8 *data,
300 int size)
301{
Peter E. Bergerc3ece7e2015-07-31 01:55:05 -0500302 return ti_vsend_sync(dev, command, value, moduleid, data, size,
303 TI_VSEND_TIMEOUT_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
306/* clear tx/rx buffers and fifo in TI UMP */
Alan Cox2742fd82008-04-29 14:45:15 +0100307static int purge_port(struct usb_serial_port *port, __u16 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Greg Kroah-Hartman11438322013-06-06 10:32:00 -0700309 int port_number = port->port_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700311 dev_dbg(&port->dev, "%s - port %d, mask %x\n", __func__, port_number, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Alan Cox2742fd82008-04-29 14:45:15 +0100313 return send_cmd(port->serial->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 UMPC_PURGE_PORT,
315 (__u8)(UMPM_UART1_PORT + port_number),
316 mask,
317 NULL,
318 0);
319}
320
321/**
Alan Cox2742fd82008-04-29 14:45:15 +0100322 * read_download_mem - Read edgeport memory from TI chip
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 * @dev: usb device pointer
324 * @start_address: Device CPU address at which to read
325 * @length: Length of above data
326 * @address_type: Can read both XDATA and I2C
327 * @buffer: pointer to input data buffer
328 */
Alan Cox2742fd82008-04-29 14:45:15 +0100329static int read_download_mem(struct usb_device *dev, int start_address,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 int length, __u8 address_type, __u8 *buffer)
331{
332 int status = 0;
333 __u8 read_length;
Johan Hovold55090762014-04-25 15:23:03 +0200334 u16 be_start_address;
Alan Cox2742fd82008-04-29 14:45:15 +0100335
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700336 dev_dbg(&dev->dev, "%s - @ %x for %d\n", __func__, start_address, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Peter E. Berger75899512015-09-16 03:13:02 -0500338 /*
339 * Read in blocks of 64 bytes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 * (TI firmware can't handle more than 64 byte reads)
341 */
342 while (length) {
343 if (length > 64)
Alan Cox2742fd82008-04-29 14:45:15 +0100344 read_length = 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 else
346 read_length = (__u8)length;
347
348 if (read_length > 1) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700349 dev_dbg(&dev->dev, "%s - @ %x for %d\n", __func__, start_address, read_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
Johan Hovold55090762014-04-25 15:23:03 +0200351 /*
352 * NOTE: Must use swab as wIndex is sent in little-endian
353 * byte order regardless of host byte order.
354 */
355 be_start_address = swab16((u16)start_address);
Alan Cox2742fd82008-04-29 14:45:15 +0100356 status = ti_vread_sync(dev, UMPC_MEMORY_READ,
357 (__u16)address_type,
Johan Hovold55090762014-04-25 15:23:03 +0200358 be_start_address,
Alan Cox2742fd82008-04-29 14:45:15 +0100359 buffer, read_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 if (status) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700362 dev_dbg(&dev->dev, "%s - ERROR %x\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return status;
364 }
365
Alan Cox2742fd82008-04-29 14:45:15 +0100366 if (read_length > 1)
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100367 usb_serial_debug_data(&dev->dev, __func__, read_length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 /* Update pointers/length */
370 start_address += read_length;
371 buffer += read_length;
372 length -= read_length;
373 }
Alan Cox2742fd82008-04-29 14:45:15 +0100374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return status;
376}
377
Alan Cox2742fd82008-04-29 14:45:15 +0100378static int read_ram(struct usb_device *dev, int start_address,
379 int length, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Alan Cox2742fd82008-04-29 14:45:15 +0100381 return read_download_mem(dev, start_address, length,
382 DTK_ADDR_SPACE_XDATA, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
385/* Read edgeport memory to a given block */
Alan Cox2742fd82008-04-29 14:45:15 +0100386static int read_boot_mem(struct edgeport_serial *serial,
387 int start_address, int length, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
389 int status = 0;
390 int i;
391
Alan Cox2742fd82008-04-29 14:45:15 +0100392 for (i = 0; i < length; i++) {
393 status = ti_vread_sync(serial->serial->dev,
394 UMPC_MEMORY_READ, serial->TI_I2C_Type,
395 (__u16)(start_address+i), &buffer[i], 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (status) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700397 dev_dbg(&serial->serial->dev->dev, "%s - ERROR %x\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return status;
399 }
400 }
401
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700402 dev_dbg(&serial->serial->dev->dev, "%s - start_address = %x, length = %d\n",
403 __func__, start_address, length);
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100404 usb_serial_debug_data(&serial->serial->dev->dev, __func__, length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 serial->TiReadI2C = 1;
407
408 return status;
409}
410
411/* Write given block to TI EPROM memory */
Alan Cox2742fd82008-04-29 14:45:15 +0100412static int write_boot_mem(struct edgeport_serial *serial,
413 int start_address, int length, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
415 int status = 0;
416 int i;
Johan Hovolde9305d22009-12-28 23:01:50 +0100417 u8 *temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 /* Must do a read before write */
420 if (!serial->TiReadI2C) {
Johan Hovolde9305d22009-12-28 23:01:50 +0100421 temp = kmalloc(1, GFP_KERNEL);
Johan Hovold10c642d2013-12-29 19:22:56 +0100422 if (!temp)
Johan Hovolde9305d22009-12-28 23:01:50 +0100423 return -ENOMEM;
Johan Hovold10c642d2013-12-29 19:22:56 +0100424
Johan Hovolde9305d22009-12-28 23:01:50 +0100425 status = read_boot_mem(serial, 0, 1, temp);
426 kfree(temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 if (status)
428 return status;
429 }
430
Alan Cox2742fd82008-04-29 14:45:15 +0100431 for (i = 0; i < length; ++i) {
Peter E. Bergerc3ece7e2015-07-31 01:55:05 -0500432 status = ti_vsend_sync(serial->serial->dev, UMPC_MEMORY_WRITE,
433 buffer[i], (u16)(i + start_address), NULL,
434 0, TI_VSEND_TIMEOUT_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if (status)
436 return status;
437 }
438
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700439 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 +0100440 usb_serial_debug_data(&serial->serial->dev->dev, __func__, length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 return status;
443}
444
445
446/* Write edgeport I2C memory to TI chip */
Alan Cox2742fd82008-04-29 14:45:15 +0100447static int write_i2c_mem(struct edgeport_serial *serial,
448 int start_address, int length, __u8 address_type, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700450 struct device *dev = &serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 int status = 0;
452 int write_length;
Johan Hovold55090762014-04-25 15:23:03 +0200453 u16 be_start_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 /* We can only send a maximum of 1 aligned byte page at a time */
Alan Cox2742fd82008-04-29 14:45:15 +0100456
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300457 /* calculate the number of bytes left in the first page */
Alan Cox2742fd82008-04-29 14:45:15 +0100458 write_length = EPROM_PAGE_SIZE -
459 (start_address & (EPROM_PAGE_SIZE - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 if (write_length > length)
462 write_length = length;
463
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700464 dev_dbg(dev, "%s - BytesInFirstPage Addr = %x, length = %d\n",
465 __func__, start_address, write_length);
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100466 usb_serial_debug_data(dev, __func__, write_length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Johan Hovold55090762014-04-25 15:23:03 +0200468 /*
469 * Write first page.
470 *
471 * NOTE: Must use swab as wIndex is sent in little-endian byte order
472 * regardless of host byte order.
473 */
474 be_start_address = swab16((u16)start_address);
Peter E. Bergerc3ece7e2015-07-31 01:55:05 -0500475 status = ti_vsend_sync(serial->serial->dev, UMPC_MEMORY_WRITE,
476 (u16)address_type, be_start_address,
477 buffer, write_length, TI_VSEND_TIMEOUT_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (status) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700479 dev_dbg(dev, "%s - ERROR %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return status;
481 }
482
483 length -= write_length;
484 start_address += write_length;
485 buffer += write_length;
486
Peter E. Berger75899512015-09-16 03:13:02 -0500487 /*
488 * We should be aligned now -- can write max page size bytes at a
489 * time.
490 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 while (length) {
492 if (length > EPROM_PAGE_SIZE)
493 write_length = EPROM_PAGE_SIZE;
494 else
495 write_length = length;
496
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700497 dev_dbg(dev, "%s - Page Write Addr = %x, length = %d\n",
498 __func__, start_address, write_length);
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100499 usb_serial_debug_data(dev, __func__, write_length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Johan Hovold55090762014-04-25 15:23:03 +0200501 /*
502 * Write next page.
503 *
504 * NOTE: Must use swab as wIndex is sent in little-endian byte
505 * order regardless of host byte order.
506 */
507 be_start_address = swab16((u16)start_address);
Alan Cox2742fd82008-04-29 14:45:15 +0100508 status = ti_vsend_sync(serial->serial->dev, UMPC_MEMORY_WRITE,
Peter E. Bergerc3ece7e2015-07-31 01:55:05 -0500509 (u16)address_type, be_start_address, buffer,
510 write_length, TI_VSEND_TIMEOUT_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (status) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700512 dev_err(dev, "%s - ERROR %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 return status;
514 }
Alan Cox2742fd82008-04-29 14:45:15 +0100515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 length -= write_length;
517 start_address += write_length;
518 buffer += write_length;
519 }
520 return status;
521}
522
Peter E. Berger75899512015-09-16 03:13:02 -0500523/*
524 * Examine the UMP DMA registers and LSR
Alan Cox2742fd82008-04-29 14:45:15 +0100525 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 * Check the MSBit of the X and Y DMA byte count registers.
527 * A zero in this bit indicates that the TX DMA buffers are empty
528 * then check the TX Empty bit in the UART.
529 */
Alan Cox2742fd82008-04-29 14:45:15 +0100530static int tx_active(struct edgeport_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
532 int status;
533 struct out_endpoint_desc_block *oedb;
534 __u8 *lsr;
535 int bytes_left = 0;
536
Alan Cox2742fd82008-04-29 14:45:15 +0100537 oedb = kmalloc(sizeof(*oedb), GFP_KERNEL);
Johan Hovold10c642d2013-12-29 19:22:56 +0100538 if (!oedb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Peter E. Berger75899512015-09-16 03:13:02 -0500541 /*
542 * Sigh, that's right, just one byte, as not all platforms can
543 * do DMA from stack
544 */
545 lsr = kmalloc(1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (!lsr) {
547 kfree(oedb);
548 return -ENOMEM;
549 }
550 /* Read the DMA Count Registers */
Alan Cox2742fd82008-04-29 14:45:15 +0100551 status = read_ram(port->port->serial->dev, port->dma_address,
552 sizeof(*oedb), (void *)oedb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (status)
554 goto exit_is_tx_active;
555
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700556 dev_dbg(&port->port->dev, "%s - XByteCount 0x%X\n", __func__, oedb->XByteCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 /* and the LSR */
Alan Cox2742fd82008-04-29 14:45:15 +0100559 status = read_ram(port->port->serial->dev,
560 port->uart_base + UMPMEM_OFFS_UART_LSR, 1, lsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 if (status)
563 goto exit_is_tx_active;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700564 dev_dbg(&port->port->dev, "%s - LSR = 0x%X\n", __func__, *lsr);
Alan Cox2742fd82008-04-29 14:45:15 +0100565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 /* If either buffer has data or we are transmitting then return TRUE */
Alan Cox2742fd82008-04-29 14:45:15 +0100567 if ((oedb->XByteCount & 0x80) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 bytes_left += 64;
569
Alan Cox2742fd82008-04-29 14:45:15 +0100570 if ((*lsr & UMP_UART_LSR_TX_MASK) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 bytes_left += 1;
572
573 /* We return Not Active if we get any kind of error */
574exit_is_tx_active:
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700575 dev_dbg(&port->port->dev, "%s - return %d\n", __func__, bytes_left);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 kfree(lsr);
578 kfree(oedb);
579 return bytes_left;
580}
581
Alan Cox2742fd82008-04-29 14:45:15 +0100582static int choose_config(struct usb_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
Alan Cox2742fd82008-04-29 14:45:15 +0100584 /*
585 * There may be multiple configurations on this device, in which case
586 * we would need to read and parse all of them to find out which one
587 * we want. However, we just support one config at this point,
588 * configuration # 1, which is Config Descriptor 0.
589 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700591 dev_dbg(&dev->dev, "%s - Number of Interfaces = %d\n",
592 __func__, dev->config->desc.bNumInterfaces);
593 dev_dbg(&dev->dev, "%s - MAX Power = %d\n",
594 __func__, dev->config->desc.bMaxPower * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 if (dev->config->desc.bNumInterfaces != 1) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700597 dev_err(&dev->dev, "%s - bNumInterfaces is not 1, ERROR!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return -ENODEV;
599 }
600
601 return 0;
602}
603
Alan Cox2742fd82008-04-29 14:45:15 +0100604static int read_rom(struct edgeport_serial *serial,
605 int start_address, int length, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
607 int status;
608
609 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
Alan Cox2742fd82008-04-29 14:45:15 +0100610 status = read_download_mem(serial->serial->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 start_address,
612 length,
613 serial->TI_I2C_Type,
614 buffer);
615 } else {
Alan Cox2742fd82008-04-29 14:45:15 +0100616 status = read_boot_mem(serial, start_address, length,
617 buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 return status;
620}
621
Alan Cox2742fd82008-04-29 14:45:15 +0100622static int write_rom(struct edgeport_serial *serial, int start_address,
623 int length, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
625 if (serial->product_info.TiMode == TI_MODE_BOOT)
Alan Cox2742fd82008-04-29 14:45:15 +0100626 return write_boot_mem(serial, start_address, length,
627 buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD)
Alan Cox2742fd82008-04-29 14:45:15 +0100630 return write_i2c_mem(serial, start_address, length,
631 serial->TI_I2C_Type, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return -EINVAL;
633}
634
635
636
637/* Read a descriptor header from I2C based on type */
Alan Cox2742fd82008-04-29 14:45:15 +0100638static int get_descriptor_addr(struct edgeport_serial *serial,
639 int desc_type, struct ti_i2c_desc *rom_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
641 int start_address;
642 int status;
643
644 /* Search for requested descriptor in I2C */
645 start_address = 2;
646 do {
Alan Cox2742fd82008-04-29 14:45:15 +0100647 status = read_rom(serial,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 start_address,
649 sizeof(struct ti_i2c_desc),
Alan Cox2742fd82008-04-29 14:45:15 +0100650 (__u8 *)rom_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (status)
652 return 0;
653
654 if (rom_desc->Type == desc_type)
655 return start_address;
656
Johan Hovold55090762014-04-25 15:23:03 +0200657 start_address = start_address + sizeof(struct ti_i2c_desc) +
658 le16_to_cpu(rom_desc->Size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 } while ((start_address < TI_MAX_I2C_SIZE) && rom_desc->Type);
Alan Cox2742fd82008-04-29 14:45:15 +0100661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return 0;
663}
664
665/* Validate descriptor checksum */
Alan Cox2742fd82008-04-29 14:45:15 +0100666static int valid_csum(struct ti_i2c_desc *rom_desc, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
668 __u16 i;
669 __u8 cs = 0;
670
Johan Hovold55090762014-04-25 15:23:03 +0200671 for (i = 0; i < le16_to_cpu(rom_desc->Size); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 cs = (__u8)(cs + buffer[i]);
Alan Cox2742fd82008-04-29 14:45:15 +0100673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (cs != rom_desc->CheckSum) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700675 pr_debug("%s - Mismatch %x - %x", __func__, rom_desc->CheckSum, cs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return -EINVAL;
677 }
678 return 0;
679}
680
681/* Make sure that the I2C image is good */
Alan Cox2742fd82008-04-29 14:45:15 +0100682static int check_i2c_image(struct edgeport_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
684 struct device *dev = &serial->serial->dev->dev;
685 int status = 0;
686 struct ti_i2c_desc *rom_desc;
687 int start_address = 2;
688 __u8 *buffer;
689 __u16 ttype;
690
Alan Cox2742fd82008-04-29 14:45:15 +0100691 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
Johan Hovold10c642d2013-12-29 19:22:56 +0100692 if (!rom_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return -ENOMEM;
Johan Hovold10c642d2013-12-29 19:22:56 +0100694
Alan Cox2742fd82008-04-29 14:45:15 +0100695 buffer = kmalloc(TI_MAX_I2C_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (!buffer) {
Alan Cox2742fd82008-04-29 14:45:15 +0100697 kfree(rom_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 return -ENOMEM;
699 }
700
Alan Cox2742fd82008-04-29 14:45:15 +0100701 /* Read the first byte (Signature0) must be 0x52 or 0x10 */
702 status = read_rom(serial, 0, 1, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if (status)
Alan Cox2742fd82008-04-29 14:45:15 +0100704 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 if (*buffer != UMP5152 && *buffer != UMP3410) {
Alan Cox2742fd82008-04-29 14:45:15 +0100707 dev_err(dev, "%s - invalid buffer signature\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 status = -ENODEV;
Alan Cox2742fd82008-04-29 14:45:15 +0100709 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
711
712 do {
Alan Cox2742fd82008-04-29 14:45:15 +0100713 /* Validate the I2C */
714 status = read_rom(serial,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 start_address,
716 sizeof(struct ti_i2c_desc),
717 (__u8 *)rom_desc);
718 if (status)
719 break;
720
Alan Cox2742fd82008-04-29 14:45:15 +0100721 if ((start_address + sizeof(struct ti_i2c_desc) +
Johan Hovold55090762014-04-25 15:23:03 +0200722 le16_to_cpu(rom_desc->Size)) > TI_MAX_I2C_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 status = -ENODEV;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700724 dev_dbg(dev, "%s - structure too big, erroring out.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 break;
726 }
727
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700728 dev_dbg(dev, "%s Type = 0x%x\n", __func__, rom_desc->Type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Alan Cox2742fd82008-04-29 14:45:15 +0100730 /* Skip type 2 record */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 ttype = rom_desc->Type & 0x0f;
Alan Cox2742fd82008-04-29 14:45:15 +0100732 if (ttype != I2C_DESC_TYPE_FIRMWARE_BASIC
733 && ttype != I2C_DESC_TYPE_FIRMWARE_AUTO) {
734 /* Read the descriptor data */
735 status = read_rom(serial, start_address +
736 sizeof(struct ti_i2c_desc),
Johan Hovold55090762014-04-25 15:23:03 +0200737 le16_to_cpu(rom_desc->Size),
738 buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (status)
740 break;
741
Alan Cox2742fd82008-04-29 14:45:15 +0100742 status = valid_csum(rom_desc, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 if (status)
744 break;
745 }
Alan Cox2742fd82008-04-29 14:45:15 +0100746 start_address = start_address + sizeof(struct ti_i2c_desc) +
Johan Hovold55090762014-04-25 15:23:03 +0200747 le16_to_cpu(rom_desc->Size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Alan Cox2742fd82008-04-29 14:45:15 +0100749 } while ((rom_desc->Type != I2C_DESC_TYPE_ION) &&
750 (start_address < TI_MAX_I2C_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Alan Cox2742fd82008-04-29 14:45:15 +0100752 if ((rom_desc->Type != I2C_DESC_TYPE_ION) ||
753 (start_address > TI_MAX_I2C_SIZE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 status = -ENODEV;
755
Alan Cox2742fd82008-04-29 14:45:15 +0100756out:
757 kfree(buffer);
758 kfree(rom_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 return status;
760}
761
Alan Cox2742fd82008-04-29 14:45:15 +0100762static int get_manuf_info(struct edgeport_serial *serial, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
764 int status;
765 int start_address;
766 struct ti_i2c_desc *rom_desc;
767 struct edge_ti_manuf_descriptor *desc;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700768 struct device *dev = &serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Alan Cox2742fd82008-04-29 14:45:15 +0100770 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
Johan Hovold10c642d2013-12-29 19:22:56 +0100771 if (!rom_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 return -ENOMEM;
Johan Hovold10c642d2013-12-29 19:22:56 +0100773
Alan Cox2742fd82008-04-29 14:45:15 +0100774 start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_ION,
775 rom_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 if (!start_address) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700778 dev_dbg(dev, "%s - Edge Descriptor not found in I2C\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 status = -ENODEV;
780 goto exit;
781 }
782
Alan Cox2742fd82008-04-29 14:45:15 +0100783 /* Read the descriptor data */
784 status = read_rom(serial, start_address+sizeof(struct ti_i2c_desc),
Johan Hovold55090762014-04-25 15:23:03 +0200785 le16_to_cpu(rom_desc->Size), buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 if (status)
787 goto exit;
Alan Cox2742fd82008-04-29 14:45:15 +0100788
789 status = valid_csum(rom_desc, buffer);
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 desc = (struct edge_ti_manuf_descriptor *)buffer;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700792 dev_dbg(dev, "%s - IonConfig 0x%x\n", __func__, desc->IonConfig);
793 dev_dbg(dev, "%s - Version %d\n", __func__, desc->Version);
794 dev_dbg(dev, "%s - Cpu/Board 0x%x\n", __func__, desc->CpuRev_BoardRev);
795 dev_dbg(dev, "%s - NumPorts %d\n", __func__, desc->NumPorts);
796 dev_dbg(dev, "%s - NumVirtualPorts %d\n", __func__, desc->NumVirtualPorts);
797 dev_dbg(dev, "%s - TotalPorts %d\n", __func__, desc->TotalPorts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799exit:
Alan Cox2742fd82008-04-29 14:45:15 +0100800 kfree(rom_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 return status;
802}
803
804/* Build firmware header used for firmware update */
Peter E. Berger277bf372015-09-16 03:12:58 -0500805static int build_i2c_fw_hdr(u8 *header, const struct firmware *fw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
807 __u8 *buffer;
808 int buffer_size;
809 int i;
810 __u8 cs = 0;
811 struct ti_i2c_desc *i2c_header;
812 struct ti_i2c_image_header *img_header;
813 struct ti_i2c_firmware_rec *firmware_rec;
Peter E. Bergerc0e34832015-07-31 01:55:06 -0500814 struct edgeport_fw_hdr *fw_hdr = (struct edgeport_fw_hdr *)fw->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Peter E. Berger75899512015-09-16 03:13:02 -0500816 /*
817 * In order to update the I2C firmware we must change the type 2 record
Alan Cox2742fd82008-04-29 14:45:15 +0100818 * to type 0xF2. This will force the UMP to come up in Boot Mode.
819 * Then while in boot mode, the driver will download the latest
820 * firmware (padded to 15.5k) into the UMP ram. And finally when the
821 * device comes back up in download mode the driver will cause the new
822 * firmware to be copied from the UMP Ram to I2C and the firmware will
823 * update the record type from 0xf2 to 0x02.
824 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Peter E. Berger75899512015-09-16 03:13:02 -0500826 /*
827 * Allocate a 15.5k buffer + 2 bytes for version number (Firmware
828 * Record)
829 */
Alan Cox2742fd82008-04-29 14:45:15 +0100830 buffer_size = (((1024 * 16) - 512 ) +
831 sizeof(struct ti_i2c_firmware_rec));
832
833 buffer = kmalloc(buffer_size, GFP_KERNEL);
Johan Hovold10c642d2013-12-29 19:22:56 +0100834 if (!buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return -ENOMEM;
Alan Cox2742fd82008-04-29 14:45:15 +0100836
Peter E. Berger75899512015-09-16 03:13:02 -0500837 /* Set entire image of 0xffs */
Alan Cox2742fd82008-04-29 14:45:15 +0100838 memset(buffer, 0xff, buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Alan Cox2742fd82008-04-29 14:45:15 +0100840 /* Copy version number into firmware record */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 firmware_rec = (struct ti_i2c_firmware_rec *)buffer;
842
Peter E. Bergerc0e34832015-07-31 01:55:06 -0500843 firmware_rec->Ver_Major = fw_hdr->major_version;
844 firmware_rec->Ver_Minor = fw_hdr->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Alan Cox2742fd82008-04-29 14:45:15 +0100846 /* Pointer to fw_down memory image */
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530847 img_header = (struct ti_i2c_image_header *)&fw->data[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Alan Cox2742fd82008-04-29 14:45:15 +0100849 memcpy(buffer + sizeof(struct ti_i2c_firmware_rec),
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530850 &fw->data[4 + sizeof(struct ti_i2c_image_header)],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 le16_to_cpu(img_header->Length));
852
853 for (i=0; i < buffer_size; i++) {
854 cs = (__u8)(cs + buffer[i]);
855 }
856
Alan Cox2742fd82008-04-29 14:45:15 +0100857 kfree(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Alan Cox2742fd82008-04-29 14:45:15 +0100859 /* Build new header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 i2c_header = (struct ti_i2c_desc *)header;
861 firmware_rec = (struct ti_i2c_firmware_rec*)i2c_header->Data;
Alan Cox2742fd82008-04-29 14:45:15 +0100862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 i2c_header->Type = I2C_DESC_TYPE_FIRMWARE_BLANK;
Johan Hovoldc03890f2014-04-26 11:53:44 +0200864 i2c_header->Size = cpu_to_le16(buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 i2c_header->CheckSum = cs;
Peter E. Bergerc0e34832015-07-31 01:55:06 -0500866 firmware_rec->Ver_Major = fw_hdr->major_version;
867 firmware_rec->Ver_Minor = fw_hdr->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869 return 0;
870}
871
872/* Try to figure out what type of I2c we have */
Alan Cox2742fd82008-04-29 14:45:15 +0100873static int i2c_type_bootmode(struct edgeport_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700875 struct device *dev = &serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 int status;
Johan Hovolde9305d22009-12-28 23:01:50 +0100877 u8 *data;
878
879 data = kmalloc(1, GFP_KERNEL);
Johan Hovold10c642d2013-12-29 19:22:56 +0100880 if (!data)
Johan Hovolde9305d22009-12-28 23:01:50 +0100881 return -ENOMEM;
Alan Cox2742fd82008-04-29 14:45:15 +0100882
883 /* Try to read type 2 */
884 status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
Johan Hovolde9305d22009-12-28 23:01:50 +0100885 DTK_ADDR_SPACE_I2C_TYPE_II, 0, data, 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if (status)
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700887 dev_dbg(dev, "%s - read 2 status error = %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 else
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700889 dev_dbg(dev, "%s - read 2 data = 0x%x\n", __func__, *data);
Johan Hovolde9305d22009-12-28 23:01:50 +0100890 if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700891 dev_dbg(dev, "%s - ROM_TYPE_II\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
Johan Hovolde9305d22009-12-28 23:01:50 +0100893 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 }
895
Alan Cox2742fd82008-04-29 14:45:15 +0100896 /* Try to read type 3 */
897 status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
Johan Hovolde9305d22009-12-28 23:01:50 +0100898 DTK_ADDR_SPACE_I2C_TYPE_III, 0, data, 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 if (status)
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700900 dev_dbg(dev, "%s - read 3 status error = %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 else
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700902 dev_dbg(dev, "%s - read 2 data = 0x%x\n", __func__, *data);
Johan Hovolde9305d22009-12-28 23:01:50 +0100903 if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700904 dev_dbg(dev, "%s - ROM_TYPE_III\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_III;
Johan Hovolde9305d22009-12-28 23:01:50 +0100906 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 }
908
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700909 dev_dbg(dev, "%s - Unknown\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
Johan Hovolde9305d22009-12-28 23:01:50 +0100911 status = -ENODEV;
912out:
913 kfree(data);
914 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915}
916
Alan Cox2742fd82008-04-29 14:45:15 +0100917static int bulk_xfer(struct usb_serial *serial, void *buffer,
918 int length, int *num_sent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
920 int status;
921
Alan Cox2742fd82008-04-29 14:45:15 +0100922 status = usb_bulk_msg(serial->dev,
923 usb_sndbulkpipe(serial->dev,
924 serial->port[0]->bulk_out_endpointAddress),
925 buffer, length, num_sent, 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 return status;
927}
928
929/* Download given firmware image to the device (IN BOOT MODE) */
Alan Cox2742fd82008-04-29 14:45:15 +0100930static int download_code(struct edgeport_serial *serial, __u8 *image,
931 int image_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
933 int status = 0;
934 int pos;
935 int transfer;
936 int done;
937
Alan Cox2742fd82008-04-29 14:45:15 +0100938 /* Transfer firmware image */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 for (pos = 0; pos < image_length; ) {
Alan Cox2742fd82008-04-29 14:45:15 +0100940 /* Read the next buffer from file */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 transfer = image_length - pos;
942 if (transfer > EDGE_FW_BULK_MAX_PACKET_SIZE)
943 transfer = EDGE_FW_BULK_MAX_PACKET_SIZE;
944
Alan Cox2742fd82008-04-29 14:45:15 +0100945 /* Transfer data */
946 status = bulk_xfer(serial->serial, &image[pos],
947 transfer, &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 if (status)
949 break;
Alan Cox2742fd82008-04-29 14:45:15 +0100950 /* Advance buffer pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 pos += done;
952 }
953
954 return status;
955}
956
Alan Cox2742fd82008-04-29 14:45:15 +0100957/* FIXME!!! */
958static int config_boot_dev(struct usb_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
960 return 0;
961}
962
Alan Cox2742fd82008-04-29 14:45:15 +0100963static int ti_cpu_rev(struct edge_ti_manuf_descriptor *desc)
964{
965 return TI_GET_CPU_REVISION(desc->CpuRev_BoardRev);
966}
967
Peter E. Bergerdcb8e992015-07-31 01:55:07 -0500968static int check_fw_sanity(struct edgeport_serial *serial,
969 const struct firmware *fw)
970{
971 u16 length_total;
972 u8 checksum = 0;
973 int pos;
974 struct device *dev = &serial->serial->interface->dev;
975 struct edgeport_fw_hdr *fw_hdr = (struct edgeport_fw_hdr *)fw->data;
976
977 if (fw->size < sizeof(struct edgeport_fw_hdr)) {
978 dev_err(dev, "incomplete fw header\n");
979 return -EINVAL;
980 }
981
982 length_total = le16_to_cpu(fw_hdr->length) +
983 sizeof(struct edgeport_fw_hdr);
984
985 if (fw->size != length_total) {
986 dev_err(dev, "bad fw size (expected: %u, got: %zu)\n",
987 length_total, fw->size);
988 return -EINVAL;
989 }
990
991 for (pos = sizeof(struct edgeport_fw_hdr); pos < fw->size; ++pos)
992 checksum += fw->data[pos];
993
994 if (checksum != fw_hdr->checksum) {
995 dev_err(dev, "bad fw checksum (expected: 0x%x, got: 0x%x)\n",
996 fw_hdr->checksum, checksum);
997 return -EINVAL;
998 }
999
1000 return 0;
1001}
1002
Peter E. Berger75899512015-09-16 03:13:02 -05001003/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 * DownloadTIFirmware - Download run-time operating firmware to the TI5052
Alan Cox2742fd82008-04-29 14:45:15 +01001005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 * This routine downloads the main operating code into the TI5052, using the
1007 * boot code already burned into E2PROM or ROM.
1008 */
Peter E. Bergerb4146162015-09-16 03:13:01 -05001009static int download_fw(struct edgeport_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
Peter E. Bergeref9324b22015-09-16 03:12:59 -05001011 struct device *dev = &serial->serial->interface->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 struct usb_interface_descriptor *interface;
Peter E. Bergerb4146162015-09-16 03:13:01 -05001014 const struct firmware *fw;
1015 const char *fw_name = "edgeport/down3.bin";
1016 struct edgeport_fw_hdr *fw_hdr;
Peter E. Bergerc0e34832015-07-31 01:55:06 -05001017
Peter E. Bergerb4146162015-09-16 03:13:01 -05001018 status = request_firmware(&fw, fw_name, dev);
1019 if (status) {
1020 dev_err(dev, "Failed to load image \"%s\" err %d\n",
1021 fw_name, status);
1022 return status;
1023 }
Peter E. Bergerc0e34832015-07-31 01:55:06 -05001024
Peter E. Bergerb4146162015-09-16 03:13:01 -05001025 if (check_fw_sanity(serial, fw)) {
1026 status = -EINVAL;
1027 goto out;
1028 }
1029
1030 fw_hdr = (struct edgeport_fw_hdr *)fw->data;
1031
1032 /* If on-board version is newer, "fw_version" will be updated later. */
Peter E. Bergerc0e34832015-07-31 01:55:06 -05001033 serial->fw_version = (fw_hdr->major_version << 8) +
1034 fw_hdr->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Peter E. Berger75899512015-09-16 03:13:02 -05001036 /*
1037 * This routine is entered by both the BOOT mode and the Download mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 * We can determine which code is running by the reading the config
1039 * descriptor and if we have only one bulk pipe it is in boot mode
1040 */
1041 serial->product_info.hardware_type = HARDWARE_TYPE_TIUMP;
1042
1043 /* Default to type 2 i2c */
1044 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
1045
Alan Cox2742fd82008-04-29 14:45:15 +01001046 status = choose_config(serial->serial->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 if (status)
Peter E. Bergerb4146162015-09-16 03:13:01 -05001048 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 interface = &serial->serial->interface->cur_altsetting->desc;
1051 if (!interface) {
Alan Cox2742fd82008-04-29 14:45:15 +01001052 dev_err(dev, "%s - no interface set, error!\n", __func__);
Peter E. Bergerb4146162015-09-16 03:13:01 -05001053 status = -ENODEV;
1054 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
1056
Alan Cox2742fd82008-04-29 14:45:15 +01001057 /*
1058 * Setup initial mode -- the default mode 0 is TI_MODE_CONFIGURING
1059 * if we have more than one endpoint we are definitely in download
1060 * mode
1061 */
Peter E. Bergerbebf1f12015-09-16 03:13:00 -05001062 if (interface->bNumEndpoints > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 serial->product_info.TiMode = TI_MODE_DOWNLOAD;
Peter E. Bergerb4146162015-09-16 03:13:01 -05001064 status = do_download_mode(serial, fw);
1065 } else {
1066 /* Otherwise we will remain in configuring mode */
1067 serial->product_info.TiMode = TI_MODE_CONFIGURING;
1068 status = do_boot_mode(serial, fw);
Peter E. Bergerbebf1f12015-09-16 03:13:00 -05001069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Peter E. Bergerb4146162015-09-16 03:13:01 -05001071out:
1072 release_firmware(fw);
1073 return status;
Peter E. Bergerbebf1f12015-09-16 03:13:00 -05001074}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Peter E. Bergerbebf1f12015-09-16 03:13:00 -05001076static int do_download_mode(struct edgeport_serial *serial,
1077 const struct firmware *fw)
1078{
1079 struct device *dev = &serial->serial->interface->dev;
1080 int status = 0;
1081 int start_address;
1082 struct edge_ti_manuf_descriptor *ti_manuf_desc;
1083 int download_cur_ver;
1084 int download_new_ver;
1085 struct edgeport_fw_hdr *fw_hdr = (struct edgeport_fw_hdr *)fw->data;
1086 struct ti_i2c_desc *rom_desc;
1087
1088 dev_dbg(dev, "%s - RUNNING IN DOWNLOAD MODE\n", __func__);
1089
1090 status = check_i2c_image(serial);
1091 if (status) {
1092 dev_dbg(dev, "%s - DOWNLOAD MODE -- BAD I2C\n", __func__);
1093 return status;
1094 }
1095
Peter E. Berger75899512015-09-16 03:13:02 -05001096 /*
1097 * Validate Hardware version number
Peter E. Bergerbebf1f12015-09-16 03:13:00 -05001098 * Read Manufacturing Descriptor from TI Based Edgeport
1099 */
1100 ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
1101 if (!ti_manuf_desc)
1102 return -ENOMEM;
1103
1104 status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
1105 if (status) {
1106 kfree(ti_manuf_desc);
1107 return status;
1108 }
1109
1110 /* Check version number of ION descriptor */
1111 if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
1112 dev_dbg(dev, "%s - Wrong CPU Rev %d (Must be 2)\n",
1113 __func__, ti_cpu_rev(ti_manuf_desc));
1114 kfree(ti_manuf_desc);
1115 return -EINVAL;
1116 }
1117
1118 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
1119 if (!rom_desc) {
1120 kfree(ti_manuf_desc);
1121 return -ENOMEM;
1122 }
1123
1124 /* Search for type 2 record (firmware record) */
1125 start_address = get_descriptor_addr(serial,
1126 I2C_DESC_TYPE_FIRMWARE_BASIC, rom_desc);
1127 if (start_address != 0) {
1128 struct ti_i2c_firmware_rec *firmware_version;
1129 u8 *record;
1130
1131 dev_dbg(dev, "%s - Found Type FIRMWARE (Type 2) record\n",
1132 __func__);
1133
1134 firmware_version = kmalloc(sizeof(*firmware_version),
1135 GFP_KERNEL);
1136 if (!firmware_version) {
1137 kfree(rom_desc);
1138 kfree(ti_manuf_desc);
1139 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 }
Alan Cox2742fd82008-04-29 14:45:15 +01001141
Peter E. Berger75899512015-09-16 03:13:02 -05001142 /*
1143 * Validate version number
Peter E. Bergerbebf1f12015-09-16 03:13:00 -05001144 * Read the descriptor data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 */
Peter E. Bergerbebf1f12015-09-16 03:13:00 -05001146 status = read_rom(serial, start_address +
1147 sizeof(struct ti_i2c_desc),
1148 sizeof(struct ti_i2c_firmware_rec),
1149 (__u8 *)firmware_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 if (status) {
Peter E. Bergerbebf1f12015-09-16 03:13:00 -05001151 kfree(firmware_version);
1152 kfree(rom_desc);
Alan Cox2742fd82008-04-29 14:45:15 +01001153 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 return status;
1155 }
1156
Peter E. Berger75899512015-09-16 03:13:02 -05001157 /*
1158 * Check version number of download with current
1159 * version in I2c
1160 */
Peter E. Bergerbebf1f12015-09-16 03:13:00 -05001161 download_cur_ver = (firmware_version->Ver_Major << 8) +
1162 (firmware_version->Ver_Minor);
1163 download_new_ver = (fw_hdr->major_version << 8) +
1164 (fw_hdr->minor_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Peter E. Bergerbebf1f12015-09-16 03:13:00 -05001166 dev_dbg(dev, "%s - >> FW Versions Device %d.%d Driver %d.%d\n",
1167 __func__, firmware_version->Ver_Major,
1168 firmware_version->Ver_Minor,
1169 fw_hdr->major_version, fw_hdr->minor_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
Peter E. Berger75899512015-09-16 03:13:02 -05001171 /*
1172 * Check if we have an old version in the I2C and
1173 * update if necessary
1174 */
Peter E. Bergerbebf1f12015-09-16 03:13:00 -05001175 if (download_cur_ver < download_new_ver) {
1176 dev_dbg(dev, "%s - Update I2C dld from %d.%d to %d.%d\n",
1177 __func__,
1178 firmware_version->Ver_Major,
1179 firmware_version->Ver_Minor,
1180 fw_hdr->major_version,
1181 fw_hdr->minor_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Peter E. Bergerbebf1f12015-09-16 03:13:00 -05001183 record = kmalloc(1, GFP_KERNEL);
1184 if (!record) {
1185 kfree(firmware_version);
Alan Cox2742fd82008-04-29 14:45:15 +01001186 kfree(rom_desc);
1187 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 return -ENOMEM;
1189 }
Peter E. Bergerbebf1f12015-09-16 03:13:00 -05001190 /*
1191 * In order to update the I2C firmware we must
1192 * change the type 2 record to type 0xF2. This
1193 * will force the UMP to come up in Boot Mode.
1194 * Then while in boot mode, the driver will
1195 * download the latest firmware (padded to
1196 * 15.5k) into the UMP ram. Finally when the
1197 * device comes back up in download mode the
1198 * driver will cause the new firmware to be
1199 * copied from the UMP Ram to I2C and the
1200 * firmware will update the record type from
1201 * 0xf2 to 0x02.
Alan Cox2742fd82008-04-29 14:45:15 +01001202 */
Peter E. Bergerbebf1f12015-09-16 03:13:00 -05001203 *record = I2C_DESC_TYPE_FIRMWARE_BLANK;
1204
Peter E. Berger75899512015-09-16 03:13:02 -05001205 /*
1206 * Change the I2C Firmware record type to
1207 * 0xf2 to trigger an update
1208 */
Peter E. Bergerbebf1f12015-09-16 03:13:00 -05001209 status = write_rom(serial, start_address,
1210 sizeof(*record), record);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 if (status) {
Peter E. Bergerbebf1f12015-09-16 03:13:00 -05001212 kfree(record);
Alan Cox2742fd82008-04-29 14:45:15 +01001213 kfree(firmware_version);
1214 kfree(rom_desc);
1215 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 return status;
1217 }
1218
Peter E. Berger75899512015-09-16 03:13:02 -05001219 /*
1220 * verify the write -- must do this in order
Peter E. Bergerbebf1f12015-09-16 03:13:00 -05001221 * for write to complete before we do the
1222 * hardware reset
1223 */
1224 status = read_rom(serial,
1225 start_address,
1226 sizeof(*record),
1227 record);
1228 if (status) {
1229 kfree(record);
1230 kfree(firmware_version);
1231 kfree(rom_desc);
1232 kfree(ti_manuf_desc);
1233 return status;
1234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Peter E. Bergerbebf1f12015-09-16 03:13:00 -05001236 if (*record != I2C_DESC_TYPE_FIRMWARE_BLANK) {
1237 dev_err(dev, "%s - error resetting device\n",
1238 __func__);
Johan Hovolde9305d22009-12-28 23:01:50 +01001239 kfree(record);
Alan Cox2742fd82008-04-29 14:45:15 +01001240 kfree(firmware_version);
1241 kfree(rom_desc);
1242 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 return -ENODEV;
1244 }
Peter E. Bergerbebf1f12015-09-16 03:13:00 -05001245
1246 dev_dbg(dev, "%s - HARDWARE RESET\n", __func__);
1247
1248 /* Reset UMP -- Back to BOOT MODE */
1249 status = ti_vsend_sync(serial->serial->dev,
1250 UMPC_HARDWARE_RESET,
1251 0, 0, NULL, 0,
1252 TI_VSEND_TIMEOUT_DEFAULT);
1253
1254 dev_dbg(dev, "%s - HARDWARE RESET return %d\n",
1255 __func__, status);
1256
1257 /* return an error on purpose. */
1258 kfree(record);
Alan Cox2742fd82008-04-29 14:45:15 +01001259 kfree(firmware_version);
Peter E. Bergerbebf1f12015-09-16 03:13:00 -05001260 kfree(rom_desc);
1261 kfree(ti_manuf_desc);
1262 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 }
Peter E. Bergerbebf1f12015-09-16 03:13:00 -05001264 /* Same or newer fw version is already loaded */
1265 serial->fw_version = download_cur_ver;
1266 kfree(firmware_version);
1267 }
1268 /* Search for type 0xF2 record (firmware blank record) */
1269 else {
1270 start_address = get_descriptor_addr(serial,
1271 I2C_DESC_TYPE_FIRMWARE_BLANK, rom_desc);
1272 if (start_address != 0) {
Alan Cox2742fd82008-04-29 14:45:15 +01001273#define HEADER_SIZE (sizeof(struct ti_i2c_desc) + \
Peter E. Bergerbebf1f12015-09-16 03:13:00 -05001274 sizeof(struct ti_i2c_firmware_rec))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 __u8 *header;
1276 __u8 *vheader;
1277
Alan Cox2742fd82008-04-29 14:45:15 +01001278 header = kmalloc(HEADER_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 if (!header) {
Alan Cox2742fd82008-04-29 14:45:15 +01001280 kfree(rom_desc);
1281 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 return -ENOMEM;
1283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Alan Cox2742fd82008-04-29 14:45:15 +01001285 vheader = kmalloc(HEADER_SIZE, GFP_KERNEL);
1286 if (!vheader) {
Alan Cox2742fd82008-04-29 14:45:15 +01001287 kfree(header);
1288 kfree(rom_desc);
1289 kfree(ti_manuf_desc);
1290 return -ENOMEM;
1291 }
1292
Peter E. Bergerbebf1f12015-09-16 03:13:00 -05001293 dev_dbg(dev, "%s - Found Type BLANK FIRMWARE (Type F2) record\n",
1294 __func__);
Alan Cox2742fd82008-04-29 14:45:15 +01001295
1296 /*
1297 * In order to update the I2C firmware we must change
1298 * the type 2 record to type 0xF2. This will force the
1299 * UMP to come up in Boot Mode. Then while in boot
1300 * mode, the driver will download the latest firmware
1301 * (padded to 15.5k) into the UMP ram. Finally when the
1302 * device comes back up in download mode the driver
1303 * will cause the new firmware to be copied from the
1304 * UMP Ram to I2C and the firmware will update the
1305 * record type from 0xf2 to 0x02.
1306 */
Peter E. Berger277bf372015-09-16 03:12:58 -05001307 status = build_i2c_fw_hdr(header, fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001309 kfree(vheader);
1310 kfree(header);
1311 kfree(rom_desc);
1312 kfree(ti_manuf_desc);
Roel Kluinfd6e5bb2010-08-10 14:29:19 -07001313 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 }
1315
Peter E. Berger75899512015-09-16 03:13:02 -05001316 /*
1317 * Update I2C with type 0xf2 record with correct
1318 * size and checksum
1319 */
Alan Cox2742fd82008-04-29 14:45:15 +01001320 status = write_rom(serial,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 start_address,
1322 HEADER_SIZE,
1323 header);
1324 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001325 kfree(vheader);
1326 kfree(header);
1327 kfree(rom_desc);
1328 kfree(ti_manuf_desc);
Roel Kluin9800eb32010-07-20 15:29:08 -07001329 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 }
1331
Peter E. Berger75899512015-09-16 03:13:02 -05001332 /*
1333 * verify the write -- must do this in order for
1334 * write to complete before we do the hardware reset
1335 */
Alan Cox2742fd82008-04-29 14:45:15 +01001336 status = read_rom(serial, start_address,
1337 HEADER_SIZE, vheader);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339 if (status) {
Peter E. Bergerbebf1f12015-09-16 03:13:00 -05001340 dev_dbg(dev, "%s - can't read header back\n",
1341 __func__);
Alan Cox2742fd82008-04-29 14:45:15 +01001342 kfree(vheader);
1343 kfree(header);
1344 kfree(rom_desc);
1345 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 return status;
1347 }
1348 if (memcmp(vheader, header, HEADER_SIZE)) {
Peter E. Bergerbebf1f12015-09-16 03:13:00 -05001349 dev_dbg(dev, "%s - write download record failed\n",
1350 __func__);
Alan Cox2742fd82008-04-29 14:45:15 +01001351 kfree(vheader);
1352 kfree(header);
1353 kfree(rom_desc);
1354 kfree(ti_manuf_desc);
Roel Kluin1e297092010-07-02 00:36:43 +02001355 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 }
1357
Alan Cox2742fd82008-04-29 14:45:15 +01001358 kfree(vheader);
1359 kfree(header);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001361 dev_dbg(dev, "%s - Start firmware update\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Alan Cox2742fd82008-04-29 14:45:15 +01001363 /* Tell firmware to copy download image into I2C */
1364 status = ti_vsend_sync(serial->serial->dev,
Peter E. Bergerc3ece7e2015-07-31 01:55:05 -05001365 UMPC_COPY_DNLD_TO_I2C,
1366 0, 0, NULL, 0,
1367 TI_VSEND_TIMEOUT_FW_DOWNLOAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Peter E. Bergerbebf1f12015-09-16 03:13:00 -05001369 dev_dbg(dev, "%s - Update complete 0x%x\n", __func__,
1370 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001372 dev_err(dev,
1373 "%s - UMPC_COPY_DNLD_TO_I2C failed\n",
Peter E. Bergerbebf1f12015-09-16 03:13:00 -05001374 __func__);
Alan Cox2742fd82008-04-29 14:45:15 +01001375 kfree(rom_desc);
1376 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 return status;
1378 }
1379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 }
1381
Peter E. Bergerbebf1f12015-09-16 03:13:00 -05001382 /* The device is running the download code */
1383 kfree(rom_desc);
1384 kfree(ti_manuf_desc);
1385 return 0;
1386}
1387
1388static int do_boot_mode(struct edgeport_serial *serial,
1389 const struct firmware *fw)
1390{
1391 struct device *dev = &serial->serial->interface->dev;
1392 int status = 0;
1393 struct edge_ti_manuf_descriptor *ti_manuf_desc;
1394 struct edgeport_fw_hdr *fw_hdr = (struct edgeport_fw_hdr *)fw->data;
1395
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001396 dev_dbg(dev, "%s - RUNNING IN BOOT MODE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Alan Cox2742fd82008-04-29 14:45:15 +01001398 /* Configure the TI device so we can use the BULK pipes for download */
1399 status = config_boot_dev(serial->serial->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 if (status)
1401 return status;
1402
Alan Cox2742fd82008-04-29 14:45:15 +01001403 if (le16_to_cpu(serial->serial->dev->descriptor.idVendor)
1404 != USB_VENDOR_ID_ION) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001405 dev_dbg(dev, "%s - VID = 0x%x\n", __func__,
1406 le16_to_cpu(serial->serial->dev->descriptor.idVendor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
Alan Cox2742fd82008-04-29 14:45:15 +01001408 goto stayinbootmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 }
1410
Peter E. Berger75899512015-09-16 03:13:02 -05001411 /*
1412 * We have an ION device (I2c Must be programmed)
1413 * Determine I2C image type
1414 */
Alan Cox2742fd82008-04-29 14:45:15 +01001415 if (i2c_type_bootmode(serial))
1416 goto stayinbootmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Alan Cox2742fd82008-04-29 14:45:15 +01001418 /* Check for ION Vendor ID and that the I2C is valid */
1419 if (!check_i2c_image(serial)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 struct ti_i2c_image_header *header;
1421 int i;
1422 __u8 cs = 0;
1423 __u8 *buffer;
1424 int buffer_size;
1425
Peter E. Berger75899512015-09-16 03:13:02 -05001426 /*
1427 * Validate Hardware version number
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 * Read Manufacturing Descriptor from TI Based Edgeport
1429 */
Alan Cox2742fd82008-04-29 14:45:15 +01001430 ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
Johan Hovold10c642d2013-12-29 19:22:56 +01001431 if (!ti_manuf_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 return -ENOMEM;
Johan Hovold10c642d2013-12-29 19:22:56 +01001433
Alan Cox2742fd82008-04-29 14:45:15 +01001434 status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001436 kfree(ti_manuf_desc);
1437 goto stayinbootmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 }
1439
Alan Cox2742fd82008-04-29 14:45:15 +01001440 /* Check for version 2 */
1441 if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001442 dev_dbg(dev, "%s - Wrong CPU Rev %d (Must be 2)\n",
1443 __func__, ti_cpu_rev(ti_manuf_desc));
Alan Cox2742fd82008-04-29 14:45:15 +01001444 kfree(ti_manuf_desc);
1445 goto stayinbootmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 }
1447
Alan Cox2742fd82008-04-29 14:45:15 +01001448 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 /*
Alan Cox2742fd82008-04-29 14:45:15 +01001451 * In order to update the I2C firmware we must change the type
1452 * 2 record to type 0xF2. This will force the UMP to come up
1453 * in Boot Mode. Then while in boot mode, the driver will
1454 * download the latest firmware (padded to 15.5k) into the
1455 * UMP ram. Finally when the device comes back up in download
1456 * mode the driver will cause the new firmware to be copied
1457 * from the UMP Ram to I2C and the firmware will update the
1458 * record type from 0xf2 to 0x02.
1459 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 * Do we really have to copy the whole firmware image,
1461 * or could we do this in place!
1462 */
1463
Alan Cox2742fd82008-04-29 14:45:15 +01001464 /* Allocate a 15.5k buffer + 3 byte header */
1465 buffer_size = (((1024 * 16) - 512) +
1466 sizeof(struct ti_i2c_image_header));
1467 buffer = kmalloc(buffer_size, GFP_KERNEL);
Johan Hovold10c642d2013-12-29 19:22:56 +01001468 if (!buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 return -ENOMEM;
Alan Cox2742fd82008-04-29 14:45:15 +01001470
1471 /* Initialize the buffer to 0xff (pad the buffer) */
1472 memset(buffer, 0xff, buffer_size);
Jaswinder Singhd12b2192008-07-04 23:06:09 +05301473 memcpy(buffer, &fw->data[4], fw->size - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Alan Cox2742fd82008-04-29 14:45:15 +01001475 for (i = sizeof(struct ti_i2c_image_header);
1476 i < buffer_size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 cs = (__u8)(cs + buffer[i]);
1478 }
Alan Cox2742fd82008-04-29 14:45:15 +01001479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 header = (struct ti_i2c_image_header *)buffer;
Alan Cox2742fd82008-04-29 14:45:15 +01001481
1482 /* update length and checksum after padding */
1483 header->Length = cpu_to_le16((__u16)(buffer_size -
1484 sizeof(struct ti_i2c_image_header)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 header->CheckSum = cs;
1486
Alan Cox2742fd82008-04-29 14:45:15 +01001487 /* Download the operational code */
Peter E. Bergerc0e34832015-07-31 01:55:06 -05001488 dev_dbg(dev, "%s - Downloading operational code image version %d.%d (TI UMP)\n",
1489 __func__,
1490 fw_hdr->major_version, fw_hdr->minor_version);
Alan Cox2742fd82008-04-29 14:45:15 +01001491 status = download_code(serial, buffer, buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Alan Cox2742fd82008-04-29 14:45:15 +01001493 kfree(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
1495 if (status) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001496 dev_dbg(dev, "%s - Error downloading operational code image\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 return status;
1498 }
1499
Alan Cox2742fd82008-04-29 14:45:15 +01001500 /* Device will reboot */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 serial->product_info.TiMode = TI_MODE_TRANSITIONING;
1502
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001503 dev_dbg(dev, "%s - Download successful -- Device rebooting...\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
1505 /* return an error on purpose */
1506 return -ENODEV;
1507 }
1508
Alan Cox2742fd82008-04-29 14:45:15 +01001509stayinbootmode:
1510 /* Eprom is invalid or blank stay in boot mode */
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001511 dev_dbg(dev, "%s - STAYING IN BOOT MODE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 serial->product_info.TiMode = TI_MODE_BOOT;
1513
1514 return 0;
1515}
1516
1517
Alan Cox2742fd82008-04-29 14:45:15 +01001518static int ti_do_config(struct edgeport_port *port, int feature, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001520 int port_number = port->port->port_number;
1521
Alan Cox2742fd82008-04-29 14:45:15 +01001522 on = !!on; /* 1 or 0 not bitmask */
1523 return send_cmd(port->port->serial->dev,
1524 feature, (__u8)(UMPM_UART1_PORT + port_number),
1525 on, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526}
1527
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Alan Cox2742fd82008-04-29 14:45:15 +01001529static int restore_mcr(struct edgeport_port *port, __u8 mcr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
1531 int status = 0;
1532
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001533 dev_dbg(&port->port->dev, "%s - %x\n", __func__, mcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Alan Cox2742fd82008-04-29 14:45:15 +01001535 status = ti_do_config(port, UMPC_SET_CLR_DTR, mcr & MCR_DTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 if (status)
1537 return status;
Alan Cox2742fd82008-04-29 14:45:15 +01001538 status = ti_do_config(port, UMPC_SET_CLR_RTS, mcr & MCR_RTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 if (status)
1540 return status;
Alan Cox2742fd82008-04-29 14:45:15 +01001541 return ti_do_config(port, UMPC_SET_CLR_LOOPBACK, mcr & MCR_LOOPBACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542}
1543
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544/* Convert TI LSR to standard UART flags */
Alan Cox2742fd82008-04-29 14:45:15 +01001545static __u8 map_line_status(__u8 ti_lsr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546{
1547 __u8 lsr = 0;
1548
1549#define MAP_FLAG(flagUmp, flagUart) \
1550 if (ti_lsr & flagUmp) \
1551 lsr |= flagUart;
1552
1553 MAP_FLAG(UMP_UART_LSR_OV_MASK, LSR_OVER_ERR) /* overrun */
1554 MAP_FLAG(UMP_UART_LSR_PE_MASK, LSR_PAR_ERR) /* parity error */
1555 MAP_FLAG(UMP_UART_LSR_FE_MASK, LSR_FRM_ERR) /* framing error */
1556 MAP_FLAG(UMP_UART_LSR_BR_MASK, LSR_BREAK) /* break detected */
Alan Cox2742fd82008-04-29 14:45:15 +01001557 MAP_FLAG(UMP_UART_LSR_RX_MASK, LSR_RX_AVAIL) /* rx data available */
1558 MAP_FLAG(UMP_UART_LSR_TX_MASK, LSR_TX_EMPTY) /* tx hold reg empty */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
1560#undef MAP_FLAG
1561
1562 return lsr;
1563}
1564
Alan Cox2742fd82008-04-29 14:45:15 +01001565static void handle_new_msr(struct edgeport_port *edge_port, __u8 msr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566{
1567 struct async_icount *icount;
1568 struct tty_struct *tty;
1569
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001570 dev_dbg(&edge_port->port->dev, "%s - %02x\n", __func__, msr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Alan Cox2742fd82008-04-29 14:45:15 +01001572 if (msr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
1573 EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
Johan Hovoldcf9a9d62013-03-21 12:37:09 +01001574 icount = &edge_port->port->icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
1576 /* update input line counters */
1577 if (msr & EDGEPORT_MSR_DELTA_CTS)
1578 icount->cts++;
1579 if (msr & EDGEPORT_MSR_DELTA_DSR)
1580 icount->dsr++;
1581 if (msr & EDGEPORT_MSR_DELTA_CD)
1582 icount->dcd++;
1583 if (msr & EDGEPORT_MSR_DELTA_RI)
1584 icount->rng++;
Johan Hovold48ee5802013-03-21 12:37:10 +01001585 wake_up_interruptible(&edge_port->port->port.delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 }
1587
1588 /* Save the new modem status */
1589 edge_port->shadow_msr = msr & 0xf0;
1590
Alan Cox4a90f092008-10-13 10:39:46 +01001591 tty = tty_port_tty_get(&edge_port->port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 /* handle CTS flow control */
1593 if (tty && C_CRTSCTS(tty)) {
Peter Hurleyd95e3ca2014-09-10 15:06:27 -04001594 if (msr & EDGEPORT_MSR_CTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 }
Alan Cox4a90f092008-10-13 10:39:46 +01001597 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598}
1599
Alan Cox2742fd82008-04-29 14:45:15 +01001600static void handle_new_lsr(struct edgeport_port *edge_port, int lsr_data,
1601 __u8 lsr, __u8 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
1603 struct async_icount *icount;
Alan Cox2742fd82008-04-29 14:45:15 +01001604 __u8 new_lsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR |
1605 LSR_FRM_ERR | LSR_BREAK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001607 dev_dbg(&edge_port->port->dev, "%s - %02x\n", __func__, new_lsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
1609 edge_port->shadow_lsr = lsr;
1610
Alan Cox2742fd82008-04-29 14:45:15 +01001611 if (new_lsr & LSR_BREAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 /*
1613 * Parity and Framing errors only count if they
1614 * occur exclusive of a break being received.
1615 */
1616 new_lsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
1618 /* Place LSR data byte into Rx buffer */
Jiri Slaby2e124b42013-01-03 15:53:06 +01001619 if (lsr_data)
1620 edge_tty_recv(edge_port->port, &data, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
1622 /* update input line counters */
Johan Hovoldcf9a9d62013-03-21 12:37:09 +01001623 icount = &edge_port->port->icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 if (new_lsr & LSR_BREAK)
1625 icount->brk++;
1626 if (new_lsr & LSR_OVER_ERR)
1627 icount->overrun++;
1628 if (new_lsr & LSR_PAR_ERR)
1629 icount->parity++;
1630 if (new_lsr & LSR_FRM_ERR)
1631 icount->frame++;
1632}
1633
1634
Alan Cox2742fd82008-04-29 14:45:15 +01001635static void edge_interrupt_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636{
Ming Leicdc97792008-02-24 18:41:47 +08001637 struct edgeport_serial *edge_serial = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 struct usb_serial_port *port;
1639 struct edgeport_port *edge_port;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001640 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 unsigned char *data = urb->transfer_buffer;
1642 int length = urb->actual_length;
1643 int port_number;
1644 int function;
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001645 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 __u8 lsr;
1647 __u8 msr;
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001648 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001650 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 case 0:
1652 /* success */
1653 break;
1654 case -ECONNRESET:
1655 case -ENOENT:
1656 case -ESHUTDOWN:
1657 /* this urb is terminated, clean up */
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001658 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001659 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 return;
1661 default:
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001662 dev_err(&urb->dev->dev, "%s - nonzero urb status received: "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001663 "%d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 goto exit;
1665 }
1666
1667 if (!length) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001668 dev_dbg(&urb->dev->dev, "%s - no data in urb\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 goto exit;
1670 }
1671
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001672 dev = &edge_serial->serial->dev->dev;
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001673 usb_serial_debug_data(dev, __func__, length, data);
Alan Cox2742fd82008-04-29 14:45:15 +01001674
1675 if (length != 2) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001676 dev_dbg(dev, "%s - expecting packet of size 2, got %d\n", __func__, length);
Alan Cox2742fd82008-04-29 14:45:15 +01001677 goto exit;
1678 }
1679
1680 port_number = TIUMP_GET_PORT_FROM_CODE(data[0]);
1681 function = TIUMP_GET_FUNC_FROM_CODE(data[0]);
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001682 dev_dbg(dev, "%s - port_number %d, function %d, info 0x%x\n", __func__,
1683 port_number, function, data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 port = edge_serial->serial->port[port_number];
1685 edge_port = usb_get_serial_port_data(port);
1686 if (!edge_port) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001687 dev_dbg(dev, "%s - edge_port not found\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 return;
1689 }
1690 switch (function) {
1691 case TIUMP_INTERRUPT_CODE_LSR:
Alan Cox2742fd82008-04-29 14:45:15 +01001692 lsr = map_line_status(data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 if (lsr & UMP_UART_LSR_DATA_MASK) {
Peter E. Berger75899512015-09-16 03:13:02 -05001694 /*
1695 * Save the LSR event for bulk read completion routine
1696 */
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001697 dev_dbg(dev, "%s - LSR Event Port %u LSR Status = %02x\n",
1698 __func__, port_number, lsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 edge_port->lsr_event = 1;
1700 edge_port->lsr_mask = lsr;
1701 } else {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001702 dev_dbg(dev, "%s - ===== Port %d LSR Status = %02x ======\n",
1703 __func__, port_number, lsr);
Alan Cox2742fd82008-04-29 14:45:15 +01001704 handle_new_lsr(edge_port, 0, lsr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 }
1706 break;
1707
Alan Cox2742fd82008-04-29 14:45:15 +01001708 case TIUMP_INTERRUPT_CODE_MSR: /* MSR */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 /* Copy MSR from UMP */
1710 msr = data[1];
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001711 dev_dbg(dev, "%s - ===== Port %u MSR Status = %02x ======\n",
1712 __func__, port_number, msr);
Alan Cox2742fd82008-04-29 14:45:15 +01001713 handle_new_msr(edge_port, msr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 break;
1715
1716 default:
Alan Cox2742fd82008-04-29 14:45:15 +01001717 dev_err(&urb->dev->dev,
1718 "%s - Unknown Interrupt code from UMP %x\n",
1719 __func__, data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 break;
Alan Cox2742fd82008-04-29 14:45:15 +01001721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 }
1723
1724exit:
Alan Cox2742fd82008-04-29 14:45:15 +01001725 retval = usb_submit_urb(urb, GFP_ATOMIC);
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001726 if (retval)
Alan Cox2742fd82008-04-29 14:45:15 +01001727 dev_err(&urb->dev->dev,
1728 "%s - usb_submit_urb failed with result %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001729 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730}
1731
Alan Cox2742fd82008-04-29 14:45:15 +01001732static void edge_bulk_in_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733{
Ming Leicdc97792008-02-24 18:41:47 +08001734 struct edgeport_port *edge_port = urb->context;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001735 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001737 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 int port_number;
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001739 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001741 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 case 0:
1743 /* success */
1744 break;
1745 case -ECONNRESET:
1746 case -ENOENT:
1747 case -ESHUTDOWN:
1748 /* this urb is terminated, clean up */
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001749 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 return;
1751 default:
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001752 dev_err(&urb->dev->dev, "%s - nonzero read bulk status received: %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 }
1754
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001755 if (status == -EPIPE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 goto exit;
1757
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001758 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001759 dev_err(&urb->dev->dev, "%s - stopping read!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 return;
1761 }
1762
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001763 port_number = edge_port->port->port_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
1765 if (edge_port->lsr_event) {
1766 edge_port->lsr_event = 0;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001767 dev_dbg(dev, "%s ===== Port %u LSR Status = %02x, Data = %02x ======\n",
1768 __func__, port_number, edge_port->lsr_mask, *data);
Alan Cox2742fd82008-04-29 14:45:15 +01001769 handle_new_lsr(edge_port, 1, edge_port->lsr_mask, *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 /* Adjust buffer length/pointer */
1771 --urb->actual_length;
1772 ++data;
1773 }
1774
Jiri Slaby2e124b42013-01-03 15:53:06 +01001775 if (urb->actual_length) {
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001776 usb_serial_debug_data(dev, __func__, urb->actual_length, data);
Alan Cox2742fd82008-04-29 14:45:15 +01001777 if (edge_port->close_pending)
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001778 dev_dbg(dev, "%s - close pending, dropping data on the floor\n",
Alan Cox2742fd82008-04-29 14:45:15 +01001779 __func__);
1780 else
Jiri Slaby2e124b42013-01-03 15:53:06 +01001781 edge_tty_recv(edge_port->port, data,
Jiri Slaby05c7cd32013-01-03 15:53:04 +01001782 urb->actual_length);
Johan Hovoldcf9a9d62013-03-21 12:37:09 +01001783 edge_port->port->icount.rx += urb->actual_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 }
1785
1786exit:
1787 /* continue read unless stopped */
1788 spin_lock(&edge_port->ep_lock);
Johan Hovold58330412011-11-06 19:06:28 +01001789 if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001790 retval = usb_submit_urb(urb, GFP_ATOMIC);
Johan Hovold58330412011-11-06 19:06:28 +01001791 else if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPED;
Johan Hovold58330412011-11-06 19:06:28 +01001793
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 spin_unlock(&edge_port->ep_lock);
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001795 if (retval)
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001796 dev_err(dev, "%s - usb_submit_urb failed with result %d\n", __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797}
1798
Jiri Slaby2e124b42013-01-03 15:53:06 +01001799static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
1800 int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801{
Alan Cox2742fd82008-04-29 14:45:15 +01001802 int queued;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
Jiri Slaby05c7cd32013-01-03 15:53:04 +01001804 queued = tty_insert_flip_string(&port->port, data, length);
Alan Cox2742fd82008-04-29 14:45:15 +01001805 if (queued < length)
Jiri Slaby05c7cd32013-01-03 15:53:04 +01001806 dev_err(&port->dev, "%s - dropping data, %d bytes lost\n",
Alan Cox2742fd82008-04-29 14:45:15 +01001807 __func__, length - queued);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001808 tty_flip_buffer_push(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809}
1810
Alan Cox2742fd82008-04-29 14:45:15 +01001811static void edge_bulk_out_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812{
Ming Leicdc97792008-02-24 18:41:47 +08001813 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001815 int status = urb->status;
Alan Cox4a90f092008-10-13 10:39:46 +01001816 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 edge_port->ep_write_urb_in_use = 0;
1819
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001820 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 case 0:
1822 /* success */
1823 break;
1824 case -ECONNRESET:
1825 case -ENOENT:
1826 case -ESHUTDOWN:
1827 /* this urb is terminated, clean up */
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001828 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001829 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 return;
1831 default:
Johan Hovold22a416c2012-02-10 13:20:51 +01001832 dev_err_console(port, "%s - nonzero write bulk status "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001833 "received: %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 }
1835
1836 /* send any buffered data */
Alan Cox4a90f092008-10-13 10:39:46 +01001837 tty = tty_port_tty_get(&port->port);
Jiri Slabyae3759c2013-04-04 22:41:01 +02001838 edge_send(port, tty);
Alan Cox4a90f092008-10-13 10:39:46 +01001839 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840}
1841
Alan Coxa509a7e2009-09-19 13:13:26 -07001842static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843{
1844 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1845 struct edgeport_serial *edge_serial;
1846 struct usb_device *dev;
1847 struct urb *urb;
1848 int port_number;
1849 int status;
1850 u16 open_settings;
1851 u8 transaction_timeout;
1852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 if (edge_port == NULL)
1854 return -ENODEV;
1855
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001856 port_number = port->port_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
1858 dev = port->serial->dev;
1859
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 /* turn off loopback */
Alan Cox2742fd82008-04-29 14:45:15 +01001861 status = ti_do_config(edge_port, UMPC_SET_CLR_LOOPBACK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001863 dev_err(&port->dev,
1864 "%s - cannot send clear loopback command, %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001865 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 return status;
1867 }
Alan Cox2742fd82008-04-29 14:45:15 +01001868
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 /* set up the port settings */
Alan Cox95da3102008-07-22 11:09:07 +01001870 if (tty)
Alan Coxadc8d742012-07-14 15:31:47 +01001871 edge_set_termios(tty, port, &tty->termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
1873 /* open up the port */
1874
1875 /* milliseconds to timeout for DMA transfer */
1876 transaction_timeout = 2;
1877
Alan Cox2742fd82008-04-29 14:45:15 +01001878 edge_port->ump_read_timeout =
1879 max(20, ((transaction_timeout * 3) / 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Alan Cox2742fd82008-04-29 14:45:15 +01001881 /* milliseconds to timeout for DMA transfer */
1882 open_settings = (u8)(UMP_DMA_MODE_CONTINOUS |
1883 UMP_PIPE_TRANS_TIMEOUT_ENA |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 (transaction_timeout << 2));
1885
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001886 dev_dbg(&port->dev, "%s - Sending UMPC_OPEN_PORT\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
1888 /* Tell TI to open and start the port */
Alan Cox2742fd82008-04-29 14:45:15 +01001889 status = send_cmd(dev, UMPC_OPEN_PORT,
1890 (u8)(UMPM_UART1_PORT + port_number), open_settings, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001892 dev_err(&port->dev, "%s - cannot send open command, %d\n",
1893 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 return status;
1895 }
1896
1897 /* Start the DMA? */
Alan Cox2742fd82008-04-29 14:45:15 +01001898 status = send_cmd(dev, UMPC_START_PORT,
1899 (u8)(UMPM_UART1_PORT + port_number), 0, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001901 dev_err(&port->dev, "%s - cannot send start DMA command, %d\n",
1902 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 return status;
1904 }
1905
1906 /* Clear TX and RX buffers in UMP */
Alan Cox2742fd82008-04-29 14:45:15 +01001907 status = purge_port(port, UMP_PORT_DIR_OUT | UMP_PORT_DIR_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001909 dev_err(&port->dev,
1910 "%s - cannot send clear buffers command, %d\n",
1911 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 return status;
1913 }
1914
1915 /* Read Initial MSR */
Alan Cox2742fd82008-04-29 14:45:15 +01001916 status = ti_vread_sync(dev, UMPC_READ_MSR, 0,
1917 (__u16)(UMPM_UART1_PORT + port_number),
1918 &edge_port->shadow_msr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001920 dev_err(&port->dev, "%s - cannot send read MSR command, %d\n",
1921 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 return status;
1923 }
1924
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001925 dev_dbg(&port->dev, "ShadowMSR 0x%X\n", edge_port->shadow_msr);
Alan Cox2742fd82008-04-29 14:45:15 +01001926
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 /* Set Initial MCR */
1928 edge_port->shadow_mcr = MCR_RTS | MCR_DTR;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001929 dev_dbg(&port->dev, "ShadowMCR 0x%X\n", edge_port->shadow_mcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
1931 edge_serial = edge_port->edge_serial;
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01001932 if (mutex_lock_interruptible(&edge_serial->es_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 return -ERESTARTSYS;
1934 if (edge_serial->num_ports_open == 0) {
Alan Cox2742fd82008-04-29 14:45:15 +01001935 /* we are the first port to open, post the interrupt urb */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 urb = edge_serial->serial->port[0]->interrupt_in_urb;
1937 if (!urb) {
Alan Cox2742fd82008-04-29 14:45:15 +01001938 dev_err(&port->dev,
1939 "%s - no interrupt urb present, exiting\n",
1940 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 status = -EINVAL;
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01001942 goto release_es_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 urb->context = edge_serial;
Alan Cox2742fd82008-04-29 14:45:15 +01001945 status = usb_submit_urb(urb, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001947 dev_err(&port->dev,
1948 "%s - usb_submit_urb failed with value %d\n",
1949 __func__, status);
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01001950 goto release_es_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 }
1952 }
1953
1954 /*
1955 * reset the data toggle on the bulk endpoints to work around bug in
1956 * host controllers where things get out of sync some times
1957 */
Alan Cox2742fd82008-04-29 14:45:15 +01001958 usb_clear_halt(dev, port->write_urb->pipe);
1959 usb_clear_halt(dev, port->read_urb->pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
1961 /* start up our bulk read urb */
1962 urb = port->read_urb;
1963 if (!urb) {
Alan Cox2742fd82008-04-29 14:45:15 +01001964 dev_err(&port->dev, "%s - no read urb present, exiting\n",
1965 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 status = -EINVAL;
1967 goto unlink_int_urb;
1968 }
1969 edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 urb->context = edge_port;
Alan Cox2742fd82008-04-29 14:45:15 +01001971 status = usb_submit_urb(urb, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001973 dev_err(&port->dev,
1974 "%s - read bulk usb_submit_urb failed with value %d\n",
1975 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 goto unlink_int_urb;
1977 }
1978
1979 ++edge_serial->num_ports_open;
1980
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01001981 goto release_es_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
1983unlink_int_urb:
1984 if (edge_port->edge_serial->num_ports_open == 0)
1985 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01001986release_es_lock:
1987 mutex_unlock(&edge_serial->es_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 return status;
1989}
1990
Alan Cox335f8512009-06-11 12:26:29 +01001991static void edge_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992{
1993 struct edgeport_serial *edge_serial;
1994 struct edgeport_port *edge_port;
Johan Hovoldaf581052012-03-26 20:31:38 +02001995 struct usb_serial *serial = port->serial;
Johan Hovold77de2512013-01-14 16:52:54 +01001996 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 int port_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 edge_serial = usb_get_serial_data(port->serial);
2000 edge_port = usb_get_serial_port_data(port);
Alan Cox2742fd82008-04-29 14:45:15 +01002001 if (edge_serial == NULL || edge_port == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 return;
Alan Cox2742fd82008-04-29 14:45:15 +01002003
Peter E. Berger75899512015-09-16 03:13:02 -05002004 /*
2005 * The bulkreadcompletion routine will check
2006 * this flag and dump add read data
2007 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 edge_port->close_pending = 1;
2009
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 usb_kill_urb(port->read_urb);
2011 usb_kill_urb(port->write_urb);
2012 edge_port->ep_write_urb_in_use = 0;
Johan Hovold77de2512013-01-14 16:52:54 +01002013 spin_lock_irqsave(&edge_port->ep_lock, flags);
Johan Hovoldddca16e2013-06-26 16:47:37 +02002014 kfifo_reset_out(&port->write_fifo);
Johan Hovold77de2512013-01-14 16:52:54 +01002015 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002017 dev_dbg(&port->dev, "%s - send umpc_close_port\n", __func__);
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07002018 port_number = port->port_number;
Johan Hovoldbca87e92013-03-21 12:37:38 +01002019 send_cmd(serial->dev, UMPC_CLOSE_PORT,
2020 (__u8)(UMPM_UART1_PORT + port_number), 0, NULL, 0);
Johan Hovoldaf581052012-03-26 20:31:38 +02002021
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01002022 mutex_lock(&edge_serial->es_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 --edge_port->edge_serial->num_ports_open;
2024 if (edge_port->edge_serial->num_ports_open <= 0) {
2025 /* last port is now closed, let's shut down our interrupt urb */
2026 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
2027 edge_port->edge_serial->num_ports_open = 0;
2028 }
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01002029 mutex_unlock(&edge_serial->es_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 edge_port->close_pending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031}
2032
Alan Cox95da3102008-07-22 11:09:07 +01002033static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
2034 const unsigned char *data, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035{
2036 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 if (count == 0) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002039 dev_dbg(&port->dev, "%s - write request of 0 bytes\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 return 0;
2041 }
2042
2043 if (edge_port == NULL)
2044 return -ENODEV;
2045 if (edge_port->close_pending == 1)
2046 return -ENODEV;
2047
Johan Hovoldddca16e2013-06-26 16:47:37 +02002048 count = kfifo_in_locked(&port->write_fifo, data, count,
Johan Hovoldd733cec2010-05-19 00:01:37 +02002049 &edge_port->ep_lock);
Jiri Slabyae3759c2013-04-04 22:41:01 +02002050 edge_send(port, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
2052 return count;
2053}
2054
Jiri Slabyae3759c2013-04-04 22:41:01 +02002055static void edge_send(struct usb_serial_port *port, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056{
2057 int count, result;
2058 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 unsigned long flags;
2060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 spin_lock_irqsave(&edge_port->ep_lock, flags);
2062
2063 if (edge_port->ep_write_urb_in_use) {
2064 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2065 return;
2066 }
2067
Johan Hovoldddca16e2013-06-26 16:47:37 +02002068 count = kfifo_out(&port->write_fifo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 port->write_urb->transfer_buffer,
2070 port->bulk_out_size);
2071
2072 if (count == 0) {
2073 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2074 return;
2075 }
2076
2077 edge_port->ep_write_urb_in_use = 1;
2078
2079 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2080
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01002081 usb_serial_debug_data(&port->dev, __func__, count, port->write_urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
2083 /* set up our urb */
Johan Hovoldfd119612011-11-06 19:06:30 +01002084 port->write_urb->transfer_buffer_length = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
2086 /* send the data out the bulk port */
2087 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
2088 if (result) {
Johan Hovold22a416c2012-02-10 13:20:51 +01002089 dev_err_console(port,
Alan Cox2742fd82008-04-29 14:45:15 +01002090 "%s - failed submitting write urb, error %d\n",
2091 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 edge_port->ep_write_urb_in_use = 0;
Alan Cox2742fd82008-04-29 14:45:15 +01002093 /* TODO: reschedule edge_send */
2094 } else
Johan Hovoldcf9a9d62013-03-21 12:37:09 +01002095 edge_port->port->icount.tx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
Peter E. Berger75899512015-09-16 03:13:02 -05002097 /*
2098 * wakeup any process waiting for writes to complete
2099 * there is now more room in the buffer for new writes
2100 */
Alan Cox2742fd82008-04-29 14:45:15 +01002101 if (tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103}
2104
Alan Cox95da3102008-07-22 11:09:07 +01002105static int edge_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106{
Alan Cox95da3102008-07-22 11:09:07 +01002107 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2109 int room = 0;
2110 unsigned long flags;
2111
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 if (edge_port == NULL)
Alan Cox2742fd82008-04-29 14:45:15 +01002113 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 if (edge_port->close_pending == 1)
Alan Cox2742fd82008-04-29 14:45:15 +01002115 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
2117 spin_lock_irqsave(&edge_port->ep_lock, flags);
Johan Hovoldddca16e2013-06-26 16:47:37 +02002118 room = kfifo_avail(&port->write_fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2120
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002121 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 return room;
2123}
2124
Alan Cox95da3102008-07-22 11:09:07 +01002125static int edge_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126{
Alan Cox95da3102008-07-22 11:09:07 +01002127 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2129 int chars = 0;
2130 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 if (edge_port == NULL)
Alan Cox2742fd82008-04-29 14:45:15 +01002132 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
2134 spin_lock_irqsave(&edge_port->ep_lock, flags);
Johan Hovoldddca16e2013-06-26 16:47:37 +02002135 chars = kfifo_len(&port->write_fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2137
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002138 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 return chars;
2140}
2141
Johan Hovoldb16634a2013-05-05 20:32:31 +02002142static bool edge_tx_empty(struct usb_serial_port *port)
2143{
2144 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2145 int ret;
2146
2147 ret = tx_active(edge_port);
2148 if (ret > 0)
2149 return false;
2150
2151 return true;
2152}
2153
Alan Cox95da3102008-07-22 11:09:07 +01002154static void edge_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155{
Alan Cox95da3102008-07-22 11:09:07 +01002156 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 int status;
2159
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 if (edge_port == NULL)
2161 return;
2162
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 /* if we are implementing XON/XOFF, send the stop character */
2164 if (I_IXOFF(tty)) {
2165 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01002166 status = edge_write(tty, port, &stop_char, 1);
2167 if (status <= 0) {
2168 dev_err(&port->dev, "%s - failed to write stop character, %d\n", __func__, status);
2169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 }
2171
Peter E. Berger75899512015-09-16 03:13:02 -05002172 /*
2173 * if we are implementing RTS/CTS, stop reads
2174 * and the Edgeport will clear the RTS line
2175 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 if (C_CRTSCTS(tty))
2177 stop_read(edge_port);
2178
2179}
2180
Alan Cox95da3102008-07-22 11:09:07 +01002181static void edge_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182{
Alan Cox95da3102008-07-22 11:09:07 +01002183 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 int status;
2186
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 if (edge_port == NULL)
2188 return;
2189
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 /* if we are implementing XON/XOFF, send the start character */
2191 if (I_IXOFF(tty)) {
2192 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01002193 status = edge_write(tty, port, &start_char, 1);
2194 if (status <= 0) {
2195 dev_err(&port->dev, "%s - failed to write start character, %d\n", __func__, status);
2196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 }
Peter E. Berger75899512015-09-16 03:13:02 -05002198 /*
2199 * if we are implementing RTS/CTS, restart reads
2200 * are the Edgeport will assert the RTS line
2201 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 if (C_CRTSCTS(tty)) {
2203 status = restart_read(edge_port);
2204 if (status)
Alan Cox2742fd82008-04-29 14:45:15 +01002205 dev_err(&port->dev,
2206 "%s - read bulk usb_submit_urb failed: %d\n",
2207 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 }
2209
2210}
2211
2212static void stop_read(struct edgeport_port *edge_port)
2213{
2214 unsigned long flags;
2215
2216 spin_lock_irqsave(&edge_port->ep_lock, flags);
2217
2218 if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
2219 edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPING;
2220 edge_port->shadow_mcr &= ~MCR_RTS;
2221
2222 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2223}
2224
2225static int restart_read(struct edgeport_port *edge_port)
2226{
2227 struct urb *urb;
2228 int status = 0;
2229 unsigned long flags;
2230
2231 spin_lock_irqsave(&edge_port->ep_lock, flags);
2232
2233 if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPED) {
2234 urb = edge_port->port->read_urb;
Oliver Neukumefdff602007-06-05 10:50:48 +02002235 status = usb_submit_urb(urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 }
2237 edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
2238 edge_port->shadow_mcr |= MCR_RTS;
2239
2240 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2241
2242 return status;
2243}
2244
Alan Cox95da3102008-07-22 11:09:07 +01002245static void change_port_settings(struct tty_struct *tty,
2246 struct edgeport_port *edge_port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247{
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002248 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 struct ump_uart_config *config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 int baud;
2251 unsigned cflag;
2252 int status;
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07002253 int port_number = edge_port->port->port_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
Alan Cox95da3102008-07-22 11:09:07 +01002255 config = kmalloc (sizeof (*config), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 if (!config) {
Alan Coxadc8d742012-07-14 15:31:47 +01002257 tty->termios = *old_termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 return;
2259 }
2260
Alan Coxadc8d742012-07-14 15:31:47 +01002261 cflag = tty->termios.c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
2263 config->wFlags = 0;
2264
2265 /* These flags must be set */
2266 config->wFlags |= UMP_MASK_UART_FLAGS_RECEIVE_MS_INT;
2267 config->wFlags |= UMP_MASK_UART_FLAGS_AUTO_START_ON_ERR;
2268 config->bUartMode = (__u8)(edge_port->bUartMode);
2269
2270 switch (cflag & CSIZE) {
Alan Cox2742fd82008-04-29 14:45:15 +01002271 case CS5:
2272 config->bDataBits = UMP_UART_CHAR5BITS;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002273 dev_dbg(dev, "%s - data bits = 5\n", __func__);
Alan Cox2742fd82008-04-29 14:45:15 +01002274 break;
2275 case CS6:
2276 config->bDataBits = UMP_UART_CHAR6BITS;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002277 dev_dbg(dev, "%s - data bits = 6\n", __func__);
Alan Cox2742fd82008-04-29 14:45:15 +01002278 break;
2279 case CS7:
2280 config->bDataBits = UMP_UART_CHAR7BITS;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002281 dev_dbg(dev, "%s - data bits = 7\n", __func__);
Alan Cox2742fd82008-04-29 14:45:15 +01002282 break;
2283 default:
2284 case CS8:
2285 config->bDataBits = UMP_UART_CHAR8BITS;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002286 dev_dbg(dev, "%s - data bits = 8\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 break;
2288 }
2289
2290 if (cflag & PARENB) {
2291 if (cflag & PARODD) {
2292 config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
2293 config->bParity = UMP_UART_ODDPARITY;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002294 dev_dbg(dev, "%s - parity = odd\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 } else {
2296 config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
2297 config->bParity = UMP_UART_EVENPARITY;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002298 dev_dbg(dev, "%s - parity = even\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 }
2300 } else {
Alan Cox2742fd82008-04-29 14:45:15 +01002301 config->bParity = UMP_UART_NOPARITY;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002302 dev_dbg(dev, "%s - parity = none\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 }
2304
2305 if (cflag & CSTOPB) {
2306 config->bStopBits = UMP_UART_STOPBIT2;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002307 dev_dbg(dev, "%s - stop bits = 2\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 } else {
2309 config->bStopBits = UMP_UART_STOPBIT1;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002310 dev_dbg(dev, "%s - stop bits = 1\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 }
2312
2313 /* figure out the flow control settings */
2314 if (cflag & CRTSCTS) {
2315 config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X_CTS_FLOW;
2316 config->wFlags |= UMP_MASK_UART_FLAGS_RTS_FLOW;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002317 dev_dbg(dev, "%s - RTS/CTS is enabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 } else {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002319 dev_dbg(dev, "%s - RTS/CTS is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 restart_read(edge_port);
2321 }
2322
Peter E. Berger75899512015-09-16 03:13:02 -05002323 /*
2324 * if we are implementing XON/XOFF, set the start and stop
2325 * character in the device
2326 */
Alan Cox2742fd82008-04-29 14:45:15 +01002327 config->cXon = START_CHAR(tty);
2328 config->cXoff = STOP_CHAR(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
Alan Cox2742fd82008-04-29 14:45:15 +01002330 /* if we are implementing INBOUND XON/XOFF */
2331 if (I_IXOFF(tty)) {
2332 config->wFlags |= UMP_MASK_UART_FLAGS_IN_X;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002333 dev_dbg(dev, "%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2334 __func__, config->cXon, config->cXoff);
Alan Cox2742fd82008-04-29 14:45:15 +01002335 } else
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002336 dev_dbg(dev, "%s - INBOUND XON/XOFF is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
Alan Cox2742fd82008-04-29 14:45:15 +01002338 /* if we are implementing OUTBOUND XON/XOFF */
2339 if (I_IXON(tty)) {
2340 config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002341 dev_dbg(dev, "%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2342 __func__, config->cXon, config->cXoff);
Alan Cox2742fd82008-04-29 14:45:15 +01002343 } else
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002344 dev_dbg(dev, "%s - OUTBOUND XON/XOFF is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
Alan Coxadc8d742012-07-14 15:31:47 +01002346 tty->termios.c_cflag &= ~CMSPAR;
Alan Coxd5f5bcd2008-01-03 16:57:33 +00002347
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 /* Round the baud rate */
2349 baud = tty_get_baud_rate(tty);
2350 if (!baud) {
2351 /* pick a default, any default... */
2352 baud = 9600;
Alan Coxd5f5bcd2008-01-03 16:57:33 +00002353 } else
2354 tty_encode_baud_rate(tty, baud, baud);
2355
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 edge_port->baud_rate = baud;
2357 config->wBaudRate = (__u16)((461550L + baud/2) / baud);
2358
Alan Coxd5f5bcd2008-01-03 16:57:33 +00002359 /* FIXME: Recompute actual baud from divisor here */
2360
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002361 dev_dbg(dev, "%s - baud rate = %d, wBaudRate = %d\n", __func__, baud, config->wBaudRate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002363 dev_dbg(dev, "wBaudRate: %d\n", (int)(461550L / config->wBaudRate));
2364 dev_dbg(dev, "wFlags: 0x%x\n", config->wFlags);
2365 dev_dbg(dev, "bDataBits: %d\n", config->bDataBits);
2366 dev_dbg(dev, "bParity: %d\n", config->bParity);
2367 dev_dbg(dev, "bStopBits: %d\n", config->bStopBits);
2368 dev_dbg(dev, "cXon: %d\n", config->cXon);
2369 dev_dbg(dev, "cXoff: %d\n", config->cXoff);
2370 dev_dbg(dev, "bUartMode: %d\n", config->bUartMode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
2372 /* move the word values into big endian mode */
Alan Cox2742fd82008-04-29 14:45:15 +01002373 cpu_to_be16s(&config->wFlags);
2374 cpu_to_be16s(&config->wBaudRate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
Alan Cox2742fd82008-04-29 14:45:15 +01002376 status = send_cmd(edge_port->port->serial->dev, UMPC_SET_CONFIG,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 (__u8)(UMPM_UART1_PORT + port_number),
Alan Cox2742fd82008-04-29 14:45:15 +01002378 0, (__u8 *)config, sizeof(*config));
2379 if (status)
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002380 dev_dbg(dev, "%s - error %d when trying to write config to device\n",
2381 __func__, status);
Alan Cox2742fd82008-04-29 14:45:15 +01002382 kfree(config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383}
2384
Alan Cox2e0ddd62008-07-22 11:12:33 +01002385static void edge_set_termios(struct tty_struct *tty,
Alan Cox95da3102008-07-22 11:09:07 +01002386 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387{
2388 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Alan Cox95da3102008-07-22 11:09:07 +01002389 unsigned int cflag;
2390
Alan Coxadc8d742012-07-14 15:31:47 +01002391 cflag = tty->termios.c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002393 dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__,
Linus Torvaldsd9a80742012-10-01 13:23:01 -07002394 tty->termios.c_cflag, tty->termios.c_iflag);
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002395 dev_dbg(&port->dev, "%s - old clfag %08x old iflag %08x\n", __func__,
2396 old_termios->c_cflag, old_termios->c_iflag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
2398 if (edge_port == NULL)
2399 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 /* change the port settings to the new ones specified */
Alan Cox95da3102008-07-22 11:09:07 +01002401 change_port_settings(tty, edge_port, old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402}
2403
Alan Cox20b9d172011-02-14 16:26:50 +00002404static int edge_tiocmset(struct tty_struct *tty,
Alan Cox2742fd82008-04-29 14:45:15 +01002405 unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406{
Alan Cox95da3102008-07-22 11:09:07 +01002407 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2409 unsigned int mcr;
Alan Cox3d71fe02008-02-20 21:38:32 +00002410 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
Alan Cox3d71fe02008-02-20 21:38:32 +00002412 spin_lock_irqsave(&edge_port->ep_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 mcr = edge_port->shadow_mcr;
2414 if (set & TIOCM_RTS)
2415 mcr |= MCR_RTS;
2416 if (set & TIOCM_DTR)
2417 mcr |= MCR_DTR;
2418 if (set & TIOCM_LOOP)
2419 mcr |= MCR_LOOPBACK;
2420
2421 if (clear & TIOCM_RTS)
2422 mcr &= ~MCR_RTS;
2423 if (clear & TIOCM_DTR)
2424 mcr &= ~MCR_DTR;
2425 if (clear & TIOCM_LOOP)
2426 mcr &= ~MCR_LOOPBACK;
2427
2428 edge_port->shadow_mcr = mcr;
Alan Cox3d71fe02008-02-20 21:38:32 +00002429 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
Alan Cox2742fd82008-04-29 14:45:15 +01002431 restore_mcr(edge_port, mcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 return 0;
2433}
2434
Alan Cox60b33c12011-02-14 16:26:14 +00002435static int edge_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436{
Alan Cox95da3102008-07-22 11:09:07 +01002437 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2439 unsigned int result = 0;
2440 unsigned int msr;
2441 unsigned int mcr;
Alan Cox3d71fe02008-02-20 21:38:32 +00002442 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
Alan Cox3d71fe02008-02-20 21:38:32 +00002444 spin_lock_irqsave(&edge_port->ep_lock, flags);
2445
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 msr = edge_port->shadow_msr;
2447 mcr = edge_port->shadow_mcr;
2448 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
2449 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
2450 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
2451 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
2452 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
2453 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
2454
2455
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002456 dev_dbg(&port->dev, "%s -- %x\n", __func__, result);
Alan Cox3d71fe02008-02-20 21:38:32 +00002457 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
2459 return result;
2460}
2461
Alan Cox2742fd82008-04-29 14:45:15 +01002462static int get_serial_info(struct edgeport_port *edge_port,
2463 struct serial_struct __user *retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464{
2465 struct serial_struct tmp;
Johan Hovoldf40d7812013-01-14 16:52:58 +01002466 unsigned cwait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
2468 if (!retinfo)
2469 return -EFAULT;
2470
Johan Hovoldf40d7812013-01-14 16:52:58 +01002471 cwait = edge_port->port->port.closing_wait;
2472 if (cwait != ASYNC_CLOSING_WAIT_NONE)
Johan Hovoldb6fd35e2013-04-18 17:33:17 +02002473 cwait = jiffies_to_msecs(cwait) / 10;
Johan Hovoldf40d7812013-01-14 16:52:58 +01002474
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 memset(&tmp, 0, sizeof(tmp));
2476
2477 tmp.type = PORT_16550A;
Greg Kroah-Hartmane5b1e202013-06-07 11:04:28 -07002478 tmp.line = edge_port->port->minor;
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07002479 tmp.port = edge_port->port->port_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 tmp.irq = 0;
2481 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2482 tmp.xmit_fifo_size = edge_port->port->bulk_out_size;
2483 tmp.baud_base = 9600;
2484 tmp.close_delay = 5*HZ;
Johan Hovoldf40d7812013-01-14 16:52:58 +01002485 tmp.closing_wait = cwait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
2487 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2488 return -EFAULT;
2489 return 0;
2490}
2491
Alan Cox00a0d0d2011-02-14 16:27:06 +00002492static int edge_ioctl(struct tty_struct *tty,
Alan Cox2742fd82008-04-29 14:45:15 +01002493 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494{
Alan Cox95da3102008-07-22 11:09:07 +01002495 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 switch (cmd) {
Alan Cox2742fd82008-04-29 14:45:15 +01002499 case TIOCGSERIAL:
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002500 dev_dbg(&port->dev, "%s - TIOCGSERIAL\n", __func__);
Alan Cox2742fd82008-04-29 14:45:15 +01002501 return get_serial_info(edge_port,
2502 (struct serial_struct __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 return -ENOIOCTLCMD;
2505}
2506
Alan Cox95da3102008-07-22 11:09:07 +01002507static void edge_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508{
Alan Cox95da3102008-07-22 11:09:07 +01002509 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2511 int status;
Alan Cox2742fd82008-04-29 14:45:15 +01002512 int bv = 0; /* Off */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
Alan Cox95da3102008-07-22 11:09:07 +01002514 if (break_state == -1)
Alan Cox2742fd82008-04-29 14:45:15 +01002515 bv = 1; /* On */
2516 status = ti_do_config(edge_port, UMPC_SET_CLR_BREAK, bv);
2517 if (status)
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002518 dev_dbg(&port->dev, "%s - error %d sending break set/clear command.\n",
2519 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520}
2521
Peter E. Berger26c78da2015-07-31 01:55:08 -05002522static void edge_heartbeat_schedule(struct edgeport_serial *edge_serial)
2523{
2524 if (!edge_serial->use_heartbeat)
2525 return;
2526
2527 schedule_delayed_work(&edge_serial->heartbeat_work,
2528 FW_HEARTBEAT_SECS * HZ);
2529}
2530
2531static void edge_heartbeat_work(struct work_struct *work)
2532{
2533 struct edgeport_serial *serial;
2534 struct ti_i2c_desc *rom_desc;
2535
2536 serial = container_of(work, struct edgeport_serial,
2537 heartbeat_work.work);
2538
2539 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
2540
2541 /* Descriptor address request is enough to reset the firmware timer */
2542 if (!rom_desc || !get_descriptor_addr(serial, I2C_DESC_TYPE_ION,
2543 rom_desc)) {
2544 dev_err(&serial->serial->interface->dev,
2545 "%s - Incomplete heartbeat\n", __func__);
2546 }
2547 kfree(rom_desc);
2548
2549 edge_heartbeat_schedule(serial);
2550}
2551
Alan Cox2742fd82008-04-29 14:45:15 +01002552static int edge_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553{
2554 struct edgeport_serial *edge_serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 int status;
Peter E. Berger26c78da2015-07-31 01:55:08 -05002556 u16 product_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557
2558 /* create our private serial structure */
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +01002559 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
Johan Hovold10c642d2013-12-29 19:22:56 +01002560 if (!edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 return -ENOMEM;
Johan Hovold10c642d2013-12-29 19:22:56 +01002562
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01002563 mutex_init(&edge_serial->es_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 edge_serial->serial = serial;
2565 usb_set_serial_data(serial, edge_serial);
2566
Peter E. Bergerb4146162015-09-16 03:13:01 -05002567 status = download_fw(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01002569 kfree(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 return status;
2571 }
2572
Peter E. Berger26c78da2015-07-31 01:55:08 -05002573 product_id = le16_to_cpu(
2574 edge_serial->serial->dev->descriptor.idProduct);
2575
2576 /* Currently only the EP/416 models require heartbeat support */
2577 if (edge_serial->fw_version > FW_HEARTBEAT_VERSION_CUTOFF) {
2578 if (product_id == ION_DEVICE_ID_TI_EDGEPORT_416 ||
2579 product_id == ION_DEVICE_ID_TI_EDGEPORT_416B) {
2580 edge_serial->use_heartbeat = true;
2581 }
2582 }
2583
2584 INIT_DELAYED_WORK(&edge_serial->heartbeat_work, edge_heartbeat_work);
2585 edge_heartbeat_schedule(edge_serial);
2586
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588}
2589
Alan Sternf9c99bb2009-06-02 11:53:55 -04002590static void edge_disconnect(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591{
Alan Sternf9c99bb2009-06-02 11:53:55 -04002592}
2593
2594static void edge_release(struct usb_serial *serial)
2595{
Peter E. Berger26c78da2015-07-31 01:55:08 -05002596 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
2597
2598 cancel_delayed_work_sync(&edge_serial->heartbeat_work);
2599 kfree(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600}
2601
Johan Hovold00361532012-10-17 13:34:58 +02002602static int edge_port_probe(struct usb_serial_port *port)
2603{
2604 struct edgeport_port *edge_port;
2605 int ret;
2606
2607 edge_port = kzalloc(sizeof(*edge_port), GFP_KERNEL);
2608 if (!edge_port)
2609 return -ENOMEM;
2610
Johan Hovold00361532012-10-17 13:34:58 +02002611 spin_lock_init(&edge_port->ep_lock);
2612 edge_port->port = port;
2613 edge_port->edge_serial = usb_get_serial_data(port->serial);
2614 edge_port->bUartMode = default_uart_mode;
2615
Johan Hovold0fce06d2013-06-26 16:47:38 +02002616 switch (port->port_number) {
2617 case 0:
2618 edge_port->uart_base = UMPMEM_BASE_UART1;
2619 edge_port->dma_address = UMPD_OEDB1_ADDRESS;
2620 break;
2621 case 1:
2622 edge_port->uart_base = UMPMEM_BASE_UART2;
2623 edge_port->dma_address = UMPD_OEDB2_ADDRESS;
2624 break;
2625 default:
2626 dev_err(&port->dev, "unknown port number\n");
2627 ret = -ENODEV;
2628 goto err;
2629 }
2630
2631 dev_dbg(&port->dev,
2632 "%s - port_number = %d, uart_base = %04x, dma_address = %04x\n",
2633 __func__, port->port_number, edge_port->uart_base,
2634 edge_port->dma_address);
2635
Johan Hovold00361532012-10-17 13:34:58 +02002636 usb_set_serial_port_data(port, edge_port);
2637
Johan Hovold5d8c61b2012-10-18 11:43:28 +02002638 ret = edge_create_sysfs_attrs(port);
Johan Hovold0fce06d2013-06-26 16:47:38 +02002639 if (ret)
2640 goto err;
Johan Hovold5d8c61b2012-10-18 11:43:28 +02002641
Johan Hovoldf40d7812013-01-14 16:52:58 +01002642 port->port.closing_wait = msecs_to_jiffies(closing_wait * 10);
Johan Hovoldd7be6222013-06-26 16:47:23 +02002643 port->port.drain_delay = 1;
Johan Hovoldf40d7812013-01-14 16:52:58 +01002644
Johan Hovold00361532012-10-17 13:34:58 +02002645 return 0;
Johan Hovold0fce06d2013-06-26 16:47:38 +02002646err:
2647 kfree(edge_port);
2648
2649 return ret;
Johan Hovold00361532012-10-17 13:34:58 +02002650}
2651
2652static int edge_port_remove(struct usb_serial_port *port)
2653{
2654 struct edgeport_port *edge_port;
2655
2656 edge_port = usb_get_serial_port_data(port);
Johan Hovold00361532012-10-17 13:34:58 +02002657 edge_remove_sysfs_attrs(port);
Johan Hovold00361532012-10-17 13:34:58 +02002658 kfree(edge_port);
2659
2660 return 0;
2661}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002663/* Sysfs Attributes */
2664
Greg Kroah-Hartman154547c2013-08-23 16:18:34 -07002665static ssize_t uart_mode_show(struct device *dev,
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002666 struct device_attribute *attr, char *buf)
2667{
2668 struct usb_serial_port *port = to_usb_serial_port(dev);
2669 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2670
2671 return sprintf(buf, "%d\n", edge_port->bUartMode);
2672}
2673
Greg Kroah-Hartman154547c2013-08-23 16:18:34 -07002674static ssize_t uart_mode_store(struct device *dev,
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002675 struct device_attribute *attr, const char *valbuf, size_t count)
2676{
2677 struct usb_serial_port *port = to_usb_serial_port(dev);
2678 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2679 unsigned int v = simple_strtoul(valbuf, NULL, 0);
2680
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002681 dev_dbg(dev, "%s: setting uart_mode = %d\n", __func__, v);
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002682
2683 if (v < 256)
2684 edge_port->bUartMode = v;
2685 else
Harvey Harrison441b62c2008-03-03 16:08:34 -08002686 dev_err(dev, "%s - uart_mode %d is invalid\n", __func__, v);
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002687
2688 return count;
2689}
Greg Kroah-Hartman154547c2013-08-23 16:18:34 -07002690static DEVICE_ATTR_RW(uart_mode);
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002691
2692static int edge_create_sysfs_attrs(struct usb_serial_port *port)
2693{
2694 return device_create_file(&port->dev, &dev_attr_uart_mode);
2695}
2696
2697static int edge_remove_sysfs_attrs(struct usb_serial_port *port)
2698{
2699 device_remove_file(&port->dev, &dev_attr_uart_mode);
2700 return 0;
2701}
2702
Peter E. Berger26c78da2015-07-31 01:55:08 -05002703#ifdef CONFIG_PM
2704static int edge_suspend(struct usb_serial *serial, pm_message_t message)
2705{
2706 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
2707
2708 cancel_delayed_work_sync(&edge_serial->heartbeat_work);
2709
2710 return 0;
2711}
2712
2713static int edge_resume(struct usb_serial *serial)
2714{
2715 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
2716
2717 edge_heartbeat_schedule(edge_serial);
2718
2719 return 0;
2720}
2721#endif
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002722
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -07002723static struct usb_serial_driver edgeport_1port_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07002724 .driver = {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07002725 .owner = THIS_MODULE,
2726 .name = "edgeport_ti_1",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07002727 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07002728 .description = "Edgeport TI 1 port adapter",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 .id_table = edgeport_1port_id_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 .num_ports = 1,
2731 .open = edge_open,
2732 .close = edge_close,
2733 .throttle = edge_throttle,
2734 .unthrottle = edge_unthrottle,
2735 .attach = edge_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04002736 .disconnect = edge_disconnect,
2737 .release = edge_release,
Johan Hovold00361532012-10-17 13:34:58 +02002738 .port_probe = edge_port_probe,
2739 .port_remove = edge_port_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 .ioctl = edge_ioctl,
2741 .set_termios = edge_set_termios,
2742 .tiocmget = edge_tiocmget,
2743 .tiocmset = edge_tiocmset,
Johan Hovold48ee5802013-03-21 12:37:10 +01002744 .tiocmiwait = usb_serial_generic_tiocmiwait,
Johan Hovoldcf9a9d62013-03-21 12:37:09 +01002745 .get_icount = usb_serial_generic_get_icount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 .write = edge_write,
2747 .write_room = edge_write_room,
2748 .chars_in_buffer = edge_chars_in_buffer,
Johan Hovoldb16634a2013-05-05 20:32:31 +02002749 .tx_empty = edge_tx_empty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 .break_ctl = edge_break,
2751 .read_int_callback = edge_interrupt_callback,
2752 .read_bulk_callback = edge_bulk_in_callback,
2753 .write_bulk_callback = edge_bulk_out_callback,
Peter E. Berger26c78da2015-07-31 01:55:08 -05002754#ifdef CONFIG_PM
2755 .suspend = edge_suspend,
2756 .resume = edge_resume,
2757#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758};
2759
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -07002760static struct usb_serial_driver edgeport_2port_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07002761 .driver = {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07002762 .owner = THIS_MODULE,
2763 .name = "edgeport_ti_2",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07002764 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07002765 .description = "Edgeport TI 2 port adapter",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 .id_table = edgeport_2port_id_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 .num_ports = 2,
2768 .open = edge_open,
2769 .close = edge_close,
2770 .throttle = edge_throttle,
2771 .unthrottle = edge_unthrottle,
2772 .attach = edge_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04002773 .disconnect = edge_disconnect,
2774 .release = edge_release,
Johan Hovold00361532012-10-17 13:34:58 +02002775 .port_probe = edge_port_probe,
2776 .port_remove = edge_port_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 .ioctl = edge_ioctl,
2778 .set_termios = edge_set_termios,
2779 .tiocmget = edge_tiocmget,
2780 .tiocmset = edge_tiocmset,
Johan Hovold48ee5802013-03-21 12:37:10 +01002781 .tiocmiwait = usb_serial_generic_tiocmiwait,
Johan Hovoldcf9a9d62013-03-21 12:37:09 +01002782 .get_icount = usb_serial_generic_get_icount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 .write = edge_write,
2784 .write_room = edge_write_room,
2785 .chars_in_buffer = edge_chars_in_buffer,
Johan Hovoldb16634a2013-05-05 20:32:31 +02002786 .tx_empty = edge_tx_empty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 .break_ctl = edge_break,
2788 .read_int_callback = edge_interrupt_callback,
2789 .read_bulk_callback = edge_bulk_in_callback,
2790 .write_bulk_callback = edge_bulk_out_callback,
Peter E. Berger26c78da2015-07-31 01:55:08 -05002791#ifdef CONFIG_PM
2792 .suspend = edge_suspend,
2793 .resume = edge_resume,
2794#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795};
2796
Alan Stern7dbe2462012-02-23 14:56:57 -05002797static struct usb_serial_driver * const serial_drivers[] = {
2798 &edgeport_1port_device, &edgeport_2port_device, NULL
2799};
2800
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07002801module_usb_serial_driver(serial_drivers, id_table_combined);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803MODULE_AUTHOR(DRIVER_AUTHOR);
2804MODULE_DESCRIPTION(DRIVER_DESC);
2805MODULE_LICENSE("GPL");
Jaswinder Singhd12b2192008-07-04 23:06:09 +05302806MODULE_FIRMWARE("edgeport/down3.bin");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808module_param(closing_wait, int, S_IRUGO | S_IWUSR);
2809MODULE_PARM_DESC(closing_wait, "Maximum wait for data to drain, in .01 secs");
2810
2811module_param(ignore_cpu_rev, bool, S_IRUGO | S_IWUSR);
Alan Cox2742fd82008-04-29 14:45:15 +01002812MODULE_PARM_DESC(ignore_cpu_rev,
2813 "Ignore the cpu revision when connecting to a device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002815module_param(default_uart_mode, int, S_IRUGO | S_IWUSR);
2816MODULE_PARM_DESC(default_uart_mode, "Default uart_mode, 0=RS232, ...");