blob: 51751828562fcba0a929808008f5bc3c25d857cc [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>
23#include <linux/init.h>
24#include <linux/slab.h>
25#include <linux/tty.h>
26#include <linux/tty_driver.h>
27#include <linux/tty_flip.h>
28#include <linux/module.h>
29#include <linux/spinlock.h>
Matthias Kaehlcke241ca642007-12-04 16:15:40 +010030#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/serial.h>
Johan Hovold818346b2014-04-25 15:23:03 +020032#include <linux/swab.h>
Johan Hovoldd733cec2010-05-19 00:01:37 +020033#include <linux/kfifo.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/ioctl.h>
Jaswinder Singhd12b2192008-07-04 23:06:09 +053035#include <linux/firmware.h>
Alan Cox2742fd82008-04-29 14:45:15 +010036#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070038#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "io_16654.h"
41#include "io_usbvend.h"
42#include "io_ti.h"
43
44/*
45 * Version Information
46 */
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -040047#define DRIVER_VERSION "v0.7mode043006"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
49#define DRIVER_DESC "Edgeport USB Serial Driver"
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define EPROM_PAGE_SIZE 64
52
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054/* different hardware types */
55#define HARDWARE_TYPE_930 0
56#define HARDWARE_TYPE_TIUMP 1
57
Alan Cox2742fd82008-04-29 14:45:15 +010058/* IOCTL_PRIVATE_TI_GET_MODE Definitions */
59#define TI_MODE_CONFIGURING 0 /* Device has not entered start device */
60#define TI_MODE_BOOT 1 /* Staying in boot mode */
61#define TI_MODE_DOWNLOAD 2 /* Made it to download mode */
62#define TI_MODE_TRANSITIONING 3 /* Currently in boot mode but
63 transitioning to download mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65/* read urb state */
66#define EDGE_READ_URB_RUNNING 0
67#define EDGE_READ_URB_STOPPING 1
68#define EDGE_READ_URB_STOPPED 2
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#define EDGE_CLOSING_WAIT 4000 /* in .01 sec */
71
72#define EDGE_OUT_BUF_SIZE 1024
73
74
75/* Product information read from the Edgeport */
Alan Cox2742fd82008-04-29 14:45:15 +010076struct product_info {
77 int TiMode; /* Current TI Mode */
78 __u8 hardware_type; /* Type of hardware */
Linus Torvalds1da177e2005-04-16 15:20:36 -070079} __attribute__((packed));
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081struct edgeport_port {
82 __u16 uart_base;
83 __u16 dma_address;
84 __u8 shadow_msr;
85 __u8 shadow_mcr;
86 __u8 shadow_lsr;
87 __u8 lsr_mask;
Martin Olsson19af5cd2009-04-23 11:37:37 +020088 __u32 ump_read_timeout; /* Number of milliseconds the UMP will
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 wait without data before completing
90 a read short */
91 int baud_rate;
92 int close_pending;
93 int lsr_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 struct async_icount icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 struct edgeport_serial *edge_serial;
96 struct usb_serial_port *port;
Alan Cox2742fd82008-04-29 14:45:15 +010097 __u8 bUartMode; /* Port type, 0: RS232, etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 spinlock_t ep_lock;
99 int ep_read_urb_state;
100 int ep_write_urb_in_use;
Johan Hovoldd733cec2010-05-19 00:01:37 +0200101 struct kfifo write_fifo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102};
103
104struct edgeport_serial {
105 struct product_info product_info;
Alan Cox2742fd82008-04-29 14:45:15 +0100106 u8 TI_I2C_Type; /* Type of I2C in UMP */
107 u8 TiReadI2C; /* Set to TRUE if we have read the
108 I2c in Boot Mode */
Matthias Kaehlcke241ca642007-12-04 16:15:40 +0100109 struct mutex es_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 int num_ports_open;
111 struct usb_serial *serial;
112};
113
114
115/* Devices that this driver supports */
Németh Márton7d40d7e2010-01-10 15:34:24 +0100116static const struct usb_device_id edgeport_1port_id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
118 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
119 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
120 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
121 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
122 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
123 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
124 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
125 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
126 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
127 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
128 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
129 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
130 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
131 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
132 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
133 { }
134};
135
Németh Márton7d40d7e2010-01-10 15:34:24 +0100136static const struct usb_device_id edgeport_2port_id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
138 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
139 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
140 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
141 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
142 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
143 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
144 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
145 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
146 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
147 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
148 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -0400149 /* The 4, 8 and 16 port devices show up as multiple 2 port devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -0400151 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
152 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
153 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
154 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 { }
156};
157
158/* Devices that this driver supports */
Németh Márton7d40d7e2010-01-10 15:34:24 +0100159static const struct usb_device_id id_table_combined[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
161 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
162 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
163 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
164 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
165 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
166 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
167 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
168 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
169 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
170 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
171 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
172 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
173 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
174 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
175 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
176 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
177 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
178 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
179 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
180 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
181 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
182 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
183 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
184 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
185 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
186 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
187 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
188 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -0400189 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
190 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
191 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
192 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 { }
194};
195
Alan Cox2742fd82008-04-29 14:45:15 +0100196MODULE_DEVICE_TABLE(usb, id_table_combined);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198static struct usb_driver io_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 .name = "io_ti",
200 .probe = usb_serial_probe,
201 .disconnect = usb_serial_disconnect,
202 .id_table = id_table_combined,
203};
204
205
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530206static unsigned char OperationalMajorVersion;
207static unsigned char OperationalMinorVersion;
208static unsigned short OperationalBuildNumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030210static bool debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212static int closing_wait = EDGE_CLOSING_WAIT;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030213static bool ignore_cpu_rev;
Alan Cox2742fd82008-04-29 14:45:15 +0100214static int default_uart_mode; /* RS232 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Alan Cox2742fd82008-04-29 14:45:15 +0100216static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
217 unsigned char *data, int length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219static void stop_read(struct edgeport_port *edge_port);
220static int restart_read(struct edgeport_port *edge_port);
221
Alan Cox95da3102008-07-22 11:09:07 +0100222static void edge_set_termios(struct tty_struct *tty,
223 struct usb_serial_port *port, struct ktermios *old_termios);
224static void edge_send(struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -0400226/* sysfs attributes */
227static int edge_create_sysfs_attrs(struct usb_serial_port *port);
228static int edge_remove_sysfs_attrs(struct usb_serial_port *port);
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Alan Cox2742fd82008-04-29 14:45:15 +0100231static int ti_vread_sync(struct usb_device *dev, __u8 request,
232 __u16 value, __u16 index, u8 *data, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
234 int status;
235
Alan Cox2742fd82008-04-29 14:45:15 +0100236 status = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request,
237 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
238 value, index, data, size, 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 if (status < 0)
240 return status;
241 if (status != size) {
Alan Cox2742fd82008-04-29 14:45:15 +0100242 dbg("%s - wanted to write %d, but only wrote %d",
243 __func__, size, status);
244 return -ECOMM;
245 }
246 return 0;
247}
248
249static int ti_vsend_sync(struct usb_device *dev, __u8 request,
250 __u16 value, __u16 index, u8 *data, int size)
251{
252 int status;
253
254 status = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
255 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
256 value, index, data, size, 1000);
257 if (status < 0)
258 return status;
259 if (status != size) {
260 dbg("%s - wanted to write %d, but only wrote %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800261 __func__, size, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return -ECOMM;
263 }
264 return 0;
265}
266
Alan Cox2742fd82008-04-29 14:45:15 +0100267static int send_cmd(struct usb_device *dev, __u8 command,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 __u8 moduleid, __u16 value, u8 *data,
269 int size)
270{
Alan Cox2742fd82008-04-29 14:45:15 +0100271 return ti_vsend_sync(dev, command, value, moduleid, data, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273
274/* clear tx/rx buffers and fifo in TI UMP */
Alan Cox2742fd82008-04-29 14:45:15 +0100275static int purge_port(struct usb_serial_port *port, __u16 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 int port_number = port->number - port->serial->minor;
278
Alan Cox2742fd82008-04-29 14:45:15 +0100279 dbg("%s - port %d, mask %x", __func__, port_number, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Alan Cox2742fd82008-04-29 14:45:15 +0100281 return send_cmd(port->serial->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 UMPC_PURGE_PORT,
283 (__u8)(UMPM_UART1_PORT + port_number),
284 mask,
285 NULL,
286 0);
287}
288
289/**
Alan Cox2742fd82008-04-29 14:45:15 +0100290 * read_download_mem - Read edgeport memory from TI chip
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 * @dev: usb device pointer
292 * @start_address: Device CPU address at which to read
293 * @length: Length of above data
294 * @address_type: Can read both XDATA and I2C
295 * @buffer: pointer to input data buffer
296 */
Alan Cox2742fd82008-04-29 14:45:15 +0100297static int read_download_mem(struct usb_device *dev, int start_address,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 int length, __u8 address_type, __u8 *buffer)
299{
300 int status = 0;
301 __u8 read_length;
Johan Hovold818346b2014-04-25 15:23:03 +0200302 u16 be_start_address;
Alan Cox2742fd82008-04-29 14:45:15 +0100303
304 dbg("%s - @ %x for %d", __func__, start_address, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 /* Read in blocks of 64 bytes
307 * (TI firmware can't handle more than 64 byte reads)
308 */
309 while (length) {
310 if (length > 64)
Alan Cox2742fd82008-04-29 14:45:15 +0100311 read_length = 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 else
313 read_length = (__u8)length;
314
315 if (read_length > 1) {
Alan Cox2742fd82008-04-29 14:45:15 +0100316 dbg("%s - @ %x for %d", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 start_address, read_length);
318 }
Johan Hovold818346b2014-04-25 15:23:03 +0200319 /*
320 * NOTE: Must use swab as wIndex is sent in little-endian
321 * byte order regardless of host byte order.
322 */
323 be_start_address = swab16((u16)start_address);
Alan Cox2742fd82008-04-29 14:45:15 +0100324 status = ti_vread_sync(dev, UMPC_MEMORY_READ,
325 (__u16)address_type,
Johan Hovold818346b2014-04-25 15:23:03 +0200326 be_start_address,
Alan Cox2742fd82008-04-29 14:45:15 +0100327 buffer, read_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +0100330 dbg("%s - ERROR %x", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return status;
332 }
333
Alan Cox2742fd82008-04-29 14:45:15 +0100334 if (read_length > 1)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800335 usb_serial_debug_data(debug, &dev->dev, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 read_length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 /* Update pointers/length */
339 start_address += read_length;
340 buffer += read_length;
341 length -= read_length;
342 }
Alan Cox2742fd82008-04-29 14:45:15 +0100343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return status;
345}
346
Alan Cox2742fd82008-04-29 14:45:15 +0100347static int read_ram(struct usb_device *dev, int start_address,
348 int length, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Alan Cox2742fd82008-04-29 14:45:15 +0100350 return read_download_mem(dev, start_address, length,
351 DTK_ADDR_SPACE_XDATA, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
353
354/* Read edgeport memory to a given block */
Alan Cox2742fd82008-04-29 14:45:15 +0100355static int read_boot_mem(struct edgeport_serial *serial,
356 int start_address, int length, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 int status = 0;
359 int i;
360
Alan Cox2742fd82008-04-29 14:45:15 +0100361 for (i = 0; i < length; i++) {
362 status = ti_vread_sync(serial->serial->dev,
363 UMPC_MEMORY_READ, serial->TI_I2C_Type,
364 (__u16)(start_address+i), &buffer[i], 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +0100366 dbg("%s - ERROR %x", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return status;
368 }
369 }
370
Alan Cox2742fd82008-04-29 14:45:15 +0100371 dbg("%s - start_address = %x, length = %d",
372 __func__, start_address, length);
373 usb_serial_debug_data(debug, &serial->serial->dev->dev,
374 __func__, length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 serial->TiReadI2C = 1;
377
378 return status;
379}
380
381/* Write given block to TI EPROM memory */
Alan Cox2742fd82008-04-29 14:45:15 +0100382static int write_boot_mem(struct edgeport_serial *serial,
383 int start_address, int length, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
385 int status = 0;
386 int i;
Johan Hovolde9305d22009-12-28 23:01:50 +0100387 u8 *temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 /* Must do a read before write */
390 if (!serial->TiReadI2C) {
Johan Hovolde9305d22009-12-28 23:01:50 +0100391 temp = kmalloc(1, GFP_KERNEL);
392 if (!temp) {
393 dev_err(&serial->serial->dev->dev,
394 "%s - out of memory\n", __func__);
395 return -ENOMEM;
396 }
397 status = read_boot_mem(serial, 0, 1, temp);
398 kfree(temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 if (status)
400 return status;
401 }
402
Alan Cox2742fd82008-04-29 14:45:15 +0100403 for (i = 0; i < length; ++i) {
404 status = ti_vsend_sync(serial->serial->dev,
405 UMPC_MEMORY_WRITE, buffer[i],
406 (__u16)(i + start_address), NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (status)
408 return status;
409 }
410
Alan Cox2742fd82008-04-29 14:45:15 +0100411 dbg("%s - start_sddr = %x, length = %d",
412 __func__, start_address, length);
413 usb_serial_debug_data(debug, &serial->serial->dev->dev,
414 __func__, length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 return status;
417}
418
419
420/* Write edgeport I2C memory to TI chip */
Alan Cox2742fd82008-04-29 14:45:15 +0100421static int write_i2c_mem(struct edgeport_serial *serial,
422 int start_address, int length, __u8 address_type, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
424 int status = 0;
425 int write_length;
Johan Hovold818346b2014-04-25 15:23:03 +0200426 u16 be_start_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 /* We can only send a maximum of 1 aligned byte page at a time */
Alan Cox2742fd82008-04-29 14:45:15 +0100429
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300430 /* calculate the number of bytes left in the first page */
Alan Cox2742fd82008-04-29 14:45:15 +0100431 write_length = EPROM_PAGE_SIZE -
432 (start_address & (EPROM_PAGE_SIZE - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 if (write_length > length)
435 write_length = length;
436
Alan Cox2742fd82008-04-29 14:45:15 +0100437 dbg("%s - BytesInFirstPage Addr = %x, length = %d",
438 __func__, start_address, write_length);
439 usb_serial_debug_data(debug, &serial->serial->dev->dev,
440 __func__, write_length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Johan Hovold818346b2014-04-25 15:23:03 +0200442 /*
443 * Write first page.
444 *
445 * NOTE: Must use swab as wIndex is sent in little-endian byte order
446 * regardless of host byte order.
447 */
448 be_start_address = swab16((u16)start_address);
Alan Cox2742fd82008-04-29 14:45:15 +0100449 status = ti_vsend_sync(serial->serial->dev,
450 UMPC_MEMORY_WRITE, (__u16)address_type,
Johan Hovold818346b2014-04-25 15:23:03 +0200451 be_start_address,
Alan Cox2742fd82008-04-29 14:45:15 +0100452 buffer, write_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +0100454 dbg("%s - ERROR %d", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return status;
456 }
457
458 length -= write_length;
459 start_address += write_length;
460 buffer += write_length;
461
Alan Cox2742fd82008-04-29 14:45:15 +0100462 /* We should be aligned now -- can write
463 max page size bytes at a time */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 while (length) {
465 if (length > EPROM_PAGE_SIZE)
466 write_length = EPROM_PAGE_SIZE;
467 else
468 write_length = length;
469
Alan Cox2742fd82008-04-29 14:45:15 +0100470 dbg("%s - Page Write Addr = %x, length = %d",
471 __func__, start_address, write_length);
472 usb_serial_debug_data(debug, &serial->serial->dev->dev,
473 __func__, write_length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Johan Hovold818346b2014-04-25 15:23:03 +0200475 /*
476 * Write next page.
477 *
478 * NOTE: Must use swab as wIndex is sent in little-endian byte
479 * order regardless of host byte order.
480 */
481 be_start_address = swab16((u16)start_address);
Alan Cox2742fd82008-04-29 14:45:15 +0100482 status = ti_vsend_sync(serial->serial->dev, UMPC_MEMORY_WRITE,
483 (__u16)address_type,
Johan Hovold818346b2014-04-25 15:23:03 +0200484 be_start_address,
Alan Cox2742fd82008-04-29 14:45:15 +0100485 buffer, write_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +0100487 dev_err(&serial->serial->dev->dev, "%s - ERROR %d\n",
488 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return status;
490 }
Alan Cox2742fd82008-04-29 14:45:15 +0100491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 length -= write_length;
493 start_address += write_length;
494 buffer += write_length;
495 }
496 return status;
497}
498
499/* Examine the UMP DMA registers and LSR
Alan Cox2742fd82008-04-29 14:45:15 +0100500 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 * Check the MSBit of the X and Y DMA byte count registers.
502 * A zero in this bit indicates that the TX DMA buffers are empty
503 * then check the TX Empty bit in the UART.
504 */
Alan Cox2742fd82008-04-29 14:45:15 +0100505static int tx_active(struct edgeport_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
507 int status;
508 struct out_endpoint_desc_block *oedb;
509 __u8 *lsr;
510 int bytes_left = 0;
511
Alan Cox2742fd82008-04-29 14:45:15 +0100512 oedb = kmalloc(sizeof(*oedb), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (!oedb) {
Alan Cox2742fd82008-04-29 14:45:15 +0100514 dev_err(&port->port->dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return -ENOMEM;
516 }
517
Alan Cox2742fd82008-04-29 14:45:15 +0100518 lsr = kmalloc(1, GFP_KERNEL); /* Sigh, that's right, just one byte,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 as not all platforms can do DMA
520 from stack */
521 if (!lsr) {
522 kfree(oedb);
523 return -ENOMEM;
524 }
525 /* Read the DMA Count Registers */
Alan Cox2742fd82008-04-29 14:45:15 +0100526 status = read_ram(port->port->serial->dev, port->dma_address,
527 sizeof(*oedb), (void *)oedb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if (status)
529 goto exit_is_tx_active;
530
Alan Cox2742fd82008-04-29 14:45:15 +0100531 dbg("%s - XByteCount 0x%X", __func__, oedb->XByteCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 /* and the LSR */
Alan Cox2742fd82008-04-29 14:45:15 +0100534 status = read_ram(port->port->serial->dev,
535 port->uart_base + UMPMEM_OFFS_UART_LSR, 1, lsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 if (status)
538 goto exit_is_tx_active;
Alan Cox2742fd82008-04-29 14:45:15 +0100539 dbg("%s - LSR = 0x%X", __func__, *lsr);
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 /* If either buffer has data or we are transmitting then return TRUE */
Alan Cox2742fd82008-04-29 14:45:15 +0100542 if ((oedb->XByteCount & 0x80) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 bytes_left += 64;
544
Alan Cox2742fd82008-04-29 14:45:15 +0100545 if ((*lsr & UMP_UART_LSR_TX_MASK) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 bytes_left += 1;
547
548 /* We return Not Active if we get any kind of error */
549exit_is_tx_active:
Alan Cox2742fd82008-04-29 14:45:15 +0100550 dbg("%s - return %d", __func__, bytes_left);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 kfree(lsr);
553 kfree(oedb);
554 return bytes_left;
555}
556
Alan Cox2742fd82008-04-29 14:45:15 +0100557static void chase_port(struct edgeport_port *port, unsigned long timeout,
558 int flush)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
560 int baud_rate;
Alan Cox4a90f092008-10-13 10:39:46 +0100561 struct tty_struct *tty = tty_port_tty_get(&port->port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 wait_queue_t wait;
563 unsigned long flags;
564
Wolfgang Frische218bfc2013-01-17 01:07:02 +0100565 if (!tty)
566 return;
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (!timeout)
Alan Cox2742fd82008-04-29 14:45:15 +0100569 timeout = (HZ * EDGE_CLOSING_WAIT)/100;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 /* wait for data to drain from the buffer */
572 spin_lock_irqsave(&port->ep_lock, flags);
573 init_waitqueue_entry(&wait, current);
574 add_wait_queue(&tty->write_wait, &wait);
575 for (;;) {
576 set_current_state(TASK_INTERRUPTIBLE);
Johan Hovoldd733cec2010-05-19 00:01:37 +0200577 if (kfifo_len(&port->write_fifo) == 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 || timeout == 0 || signal_pending(current)
Alan Cox2742fd82008-04-29 14:45:15 +0100579 || !usb_get_intfdata(port->port->serial->interface))
580 /* disconnect */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 break;
582 spin_unlock_irqrestore(&port->ep_lock, flags);
583 timeout = schedule_timeout(timeout);
584 spin_lock_irqsave(&port->ep_lock, flags);
585 }
586 set_current_state(TASK_RUNNING);
587 remove_wait_queue(&tty->write_wait, &wait);
588 if (flush)
Johan Hovoldd733cec2010-05-19 00:01:37 +0200589 kfifo_reset_out(&port->write_fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 spin_unlock_irqrestore(&port->ep_lock, flags);
Alan Cox4a90f092008-10-13 10:39:46 +0100591 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 /* wait for data to drain from the device */
594 timeout += jiffies;
595 while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
Alan Cox2742fd82008-04-29 14:45:15 +0100596 && usb_get_intfdata(port->port->serial->interface)) {
597 /* not disconnected */
598 if (!tx_active(port))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 break;
600 msleep(10);
601 }
602
603 /* disconnected */
604 if (!usb_get_intfdata(port->port->serial->interface))
605 return;
606
607 /* wait one more character time, based on baud rate */
Alan Cox2742fd82008-04-29 14:45:15 +0100608 /* (tx_active doesn't seem to wait for the last byte) */
609 baud_rate = port->baud_rate;
610 if (baud_rate == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 baud_rate = 50;
Julia Lawalldfa5ec72008-03-04 15:25:11 -0800612 msleep(max(1, DIV_ROUND_UP(10000, baud_rate)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
Alan Cox2742fd82008-04-29 14:45:15 +0100615static int choose_config(struct usb_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Alan Cox2742fd82008-04-29 14:45:15 +0100617 /*
618 * There may be multiple configurations on this device, in which case
619 * we would need to read and parse all of them to find out which one
620 * we want. However, we just support one config at this point,
621 * configuration # 1, which is Config Descriptor 0.
622 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Alan Cox2742fd82008-04-29 14:45:15 +0100624 dbg("%s - Number of Interfaces = %d",
625 __func__, dev->config->desc.bNumInterfaces);
626 dbg("%s - MAX Power = %d",
627 __func__, dev->config->desc.bMaxPower * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 if (dev->config->desc.bNumInterfaces != 1) {
Alan Cox2742fd82008-04-29 14:45:15 +0100630 dev_err(&dev->dev, "%s - bNumInterfaces is not 1, ERROR!\n",
631 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return -ENODEV;
633 }
634
635 return 0;
636}
637
Alan Cox2742fd82008-04-29 14:45:15 +0100638static int read_rom(struct edgeport_serial *serial,
639 int start_address, int length, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
641 int status;
642
643 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
Alan Cox2742fd82008-04-29 14:45:15 +0100644 status = read_download_mem(serial->serial->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 start_address,
646 length,
647 serial->TI_I2C_Type,
648 buffer);
649 } else {
Alan Cox2742fd82008-04-29 14:45:15 +0100650 status = read_boot_mem(serial, start_address, length,
651 buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return status;
654}
655
Alan Cox2742fd82008-04-29 14:45:15 +0100656static int write_rom(struct edgeport_serial *serial, int start_address,
657 int length, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
659 if (serial->product_info.TiMode == TI_MODE_BOOT)
Alan Cox2742fd82008-04-29 14:45:15 +0100660 return write_boot_mem(serial, start_address, length,
661 buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD)
Alan Cox2742fd82008-04-29 14:45:15 +0100664 return write_i2c_mem(serial, start_address, length,
665 serial->TI_I2C_Type, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return -EINVAL;
667}
668
669
670
671/* Read a descriptor header from I2C based on type */
Alan Cox2742fd82008-04-29 14:45:15 +0100672static int get_descriptor_addr(struct edgeport_serial *serial,
673 int desc_type, struct ti_i2c_desc *rom_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
675 int start_address;
676 int status;
677
678 /* Search for requested descriptor in I2C */
679 start_address = 2;
680 do {
Alan Cox2742fd82008-04-29 14:45:15 +0100681 status = read_rom(serial,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 start_address,
683 sizeof(struct ti_i2c_desc),
Alan Cox2742fd82008-04-29 14:45:15 +0100684 (__u8 *)rom_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if (status)
686 return 0;
687
688 if (rom_desc->Type == desc_type)
689 return start_address;
690
Johan Hovold818346b2014-04-25 15:23:03 +0200691 start_address = start_address + sizeof(struct ti_i2c_desc) +
692 le16_to_cpu(rom_desc->Size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 } while ((start_address < TI_MAX_I2C_SIZE) && rom_desc->Type);
Alan Cox2742fd82008-04-29 14:45:15 +0100695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return 0;
697}
698
699/* Validate descriptor checksum */
Alan Cox2742fd82008-04-29 14:45:15 +0100700static int valid_csum(struct ti_i2c_desc *rom_desc, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
702 __u16 i;
703 __u8 cs = 0;
704
Johan Hovold818346b2014-04-25 15:23:03 +0200705 for (i = 0; i < le16_to_cpu(rom_desc->Size); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 cs = (__u8)(cs + buffer[i]);
Alan Cox2742fd82008-04-29 14:45:15 +0100707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if (cs != rom_desc->CheckSum) {
Alan Cox2742fd82008-04-29 14:45:15 +0100709 dbg("%s - Mismatch %x - %x", __func__, rom_desc->CheckSum, cs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 return -EINVAL;
711 }
712 return 0;
713}
714
715/* Make sure that the I2C image is good */
Alan Cox2742fd82008-04-29 14:45:15 +0100716static int check_i2c_image(struct edgeport_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
718 struct device *dev = &serial->serial->dev->dev;
719 int status = 0;
720 struct ti_i2c_desc *rom_desc;
721 int start_address = 2;
722 __u8 *buffer;
723 __u16 ttype;
724
Alan Cox2742fd82008-04-29 14:45:15 +0100725 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (!rom_desc) {
Alan Cox2742fd82008-04-29 14:45:15 +0100727 dev_err(dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 return -ENOMEM;
729 }
Alan Cox2742fd82008-04-29 14:45:15 +0100730 buffer = kmalloc(TI_MAX_I2C_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (!buffer) {
Alan Cox2742fd82008-04-29 14:45:15 +0100732 dev_err(dev, "%s - out of memory when allocating buffer\n",
733 __func__);
734 kfree(rom_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return -ENOMEM;
736 }
737
Alan Cox2742fd82008-04-29 14:45:15 +0100738 /* Read the first byte (Signature0) must be 0x52 or 0x10 */
739 status = read_rom(serial, 0, 1, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (status)
Alan Cox2742fd82008-04-29 14:45:15 +0100741 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743 if (*buffer != UMP5152 && *buffer != UMP3410) {
Alan Cox2742fd82008-04-29 14:45:15 +0100744 dev_err(dev, "%s - invalid buffer signature\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 status = -ENODEV;
Alan Cox2742fd82008-04-29 14:45:15 +0100746 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
748
749 do {
Alan Cox2742fd82008-04-29 14:45:15 +0100750 /* Validate the I2C */
751 status = read_rom(serial,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 start_address,
753 sizeof(struct ti_i2c_desc),
754 (__u8 *)rom_desc);
755 if (status)
756 break;
757
Alan Cox2742fd82008-04-29 14:45:15 +0100758 if ((start_address + sizeof(struct ti_i2c_desc) +
Johan Hovold818346b2014-04-25 15:23:03 +0200759 le16_to_cpu(rom_desc->Size)) > TI_MAX_I2C_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 status = -ENODEV;
Alan Cox2742fd82008-04-29 14:45:15 +0100761 dbg("%s - structure too big, erroring out.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 break;
763 }
764
Alan Cox2742fd82008-04-29 14:45:15 +0100765 dbg("%s Type = 0x%x", __func__, rom_desc->Type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Alan Cox2742fd82008-04-29 14:45:15 +0100767 /* Skip type 2 record */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 ttype = rom_desc->Type & 0x0f;
Alan Cox2742fd82008-04-29 14:45:15 +0100769 if (ttype != I2C_DESC_TYPE_FIRMWARE_BASIC
770 && ttype != I2C_DESC_TYPE_FIRMWARE_AUTO) {
771 /* Read the descriptor data */
772 status = read_rom(serial, start_address +
773 sizeof(struct ti_i2c_desc),
Johan Hovold818346b2014-04-25 15:23:03 +0200774 le16_to_cpu(rom_desc->Size),
775 buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 if (status)
777 break;
778
Alan Cox2742fd82008-04-29 14:45:15 +0100779 status = valid_csum(rom_desc, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (status)
781 break;
782 }
Alan Cox2742fd82008-04-29 14:45:15 +0100783 start_address = start_address + sizeof(struct ti_i2c_desc) +
Johan Hovold818346b2014-04-25 15:23:03 +0200784 le16_to_cpu(rom_desc->Size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Alan Cox2742fd82008-04-29 14:45:15 +0100786 } while ((rom_desc->Type != I2C_DESC_TYPE_ION) &&
787 (start_address < TI_MAX_I2C_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Alan Cox2742fd82008-04-29 14:45:15 +0100789 if ((rom_desc->Type != I2C_DESC_TYPE_ION) ||
790 (start_address > TI_MAX_I2C_SIZE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 status = -ENODEV;
792
Alan Cox2742fd82008-04-29 14:45:15 +0100793out:
794 kfree(buffer);
795 kfree(rom_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return status;
797}
798
Alan Cox2742fd82008-04-29 14:45:15 +0100799static int get_manuf_info(struct edgeport_serial *serial, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
801 int status;
802 int start_address;
803 struct ti_i2c_desc *rom_desc;
804 struct edge_ti_manuf_descriptor *desc;
805
Alan Cox2742fd82008-04-29 14:45:15 +0100806 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 if (!rom_desc) {
Alan Cox2742fd82008-04-29 14:45:15 +0100808 dev_err(&serial->serial->dev->dev, "%s - out of memory\n",
809 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return -ENOMEM;
811 }
Alan Cox2742fd82008-04-29 14:45:15 +0100812 start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_ION,
813 rom_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 if (!start_address) {
Alan Cox2742fd82008-04-29 14:45:15 +0100816 dbg("%s - Edge Descriptor not found in I2C", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 status = -ENODEV;
818 goto exit;
819 }
820
Alan Cox2742fd82008-04-29 14:45:15 +0100821 /* Read the descriptor data */
822 status = read_rom(serial, start_address+sizeof(struct ti_i2c_desc),
Johan Hovold818346b2014-04-25 15:23:03 +0200823 le16_to_cpu(rom_desc->Size), buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 if (status)
825 goto exit;
Alan Cox2742fd82008-04-29 14:45:15 +0100826
827 status = valid_csum(rom_desc, buffer);
828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 desc = (struct edge_ti_manuf_descriptor *)buffer;
Alan Cox2742fd82008-04-29 14:45:15 +0100830 dbg("%s - IonConfig 0x%x", __func__, desc->IonConfig);
831 dbg("%s - Version %d", __func__, desc->Version);
832 dbg("%s - Cpu/Board 0x%x", __func__, desc->CpuRev_BoardRev);
833 dbg("%s - NumPorts %d", __func__, desc->NumPorts);
834 dbg("%s - NumVirtualPorts %d", __func__, desc->NumVirtualPorts);
835 dbg("%s - TotalPorts %d", __func__, desc->TotalPorts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837exit:
Alan Cox2742fd82008-04-29 14:45:15 +0100838 kfree(rom_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return status;
840}
841
842/* Build firmware header used for firmware update */
Alan Cox2742fd82008-04-29 14:45:15 +0100843static int build_i2c_fw_hdr(__u8 *header, struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
845 __u8 *buffer;
846 int buffer_size;
847 int i;
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530848 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 __u8 cs = 0;
850 struct ti_i2c_desc *i2c_header;
851 struct ti_i2c_image_header *img_header;
852 struct ti_i2c_firmware_rec *firmware_rec;
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530853 const struct firmware *fw;
854 const char *fw_name = "edgeport/down3.bin";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Alan Cox2742fd82008-04-29 14:45:15 +0100856 /* In order to update the I2C firmware we must change the type 2 record
857 * to type 0xF2. This will force the UMP to come up in Boot Mode.
858 * Then while in boot mode, the driver will download the latest
859 * firmware (padded to 15.5k) into the UMP ram. And finally when the
860 * device comes back up in download mode the driver will cause the new
861 * firmware to be copied from the UMP Ram to I2C and the firmware will
862 * update the record type from 0xf2 to 0x02.
863 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Alan Cox2742fd82008-04-29 14:45:15 +0100865 /* Allocate a 15.5k buffer + 2 bytes for version number
866 * (Firmware Record) */
867 buffer_size = (((1024 * 16) - 512 ) +
868 sizeof(struct ti_i2c_firmware_rec));
869
870 buffer = kmalloc(buffer_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 if (!buffer) {
Alan Cox2742fd82008-04-29 14:45:15 +0100872 dev_err(dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 return -ENOMEM;
874 }
Alan Cox2742fd82008-04-29 14:45:15 +0100875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 // Set entire image of 0xffs
Alan Cox2742fd82008-04-29 14:45:15 +0100877 memset(buffer, 0xff, buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530879 err = request_firmware(&fw, fw_name, dev);
880 if (err) {
881 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
882 fw_name, err);
883 kfree(buffer);
884 return err;
885 }
886
887 /* Save Download Version Number */
888 OperationalMajorVersion = fw->data[0];
889 OperationalMinorVersion = fw->data[1];
890 OperationalBuildNumber = fw->data[2] | (fw->data[3] << 8);
891
Alan Cox2742fd82008-04-29 14:45:15 +0100892 /* Copy version number into firmware record */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 firmware_rec = (struct ti_i2c_firmware_rec *)buffer;
894
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530895 firmware_rec->Ver_Major = OperationalMajorVersion;
896 firmware_rec->Ver_Minor = OperationalMinorVersion;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Alan Cox2742fd82008-04-29 14:45:15 +0100898 /* Pointer to fw_down memory image */
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530899 img_header = (struct ti_i2c_image_header *)&fw->data[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Alan Cox2742fd82008-04-29 14:45:15 +0100901 memcpy(buffer + sizeof(struct ti_i2c_firmware_rec),
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530902 &fw->data[4 + sizeof(struct ti_i2c_image_header)],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 le16_to_cpu(img_header->Length));
904
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530905 release_firmware(fw);
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 for (i=0; i < buffer_size; i++) {
908 cs = (__u8)(cs + buffer[i]);
909 }
910
Alan Cox2742fd82008-04-29 14:45:15 +0100911 kfree(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Alan Cox2742fd82008-04-29 14:45:15 +0100913 /* Build new header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 i2c_header = (struct ti_i2c_desc *)header;
915 firmware_rec = (struct ti_i2c_firmware_rec*)i2c_header->Data;
Alan Cox2742fd82008-04-29 14:45:15 +0100916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 i2c_header->Type = I2C_DESC_TYPE_FIRMWARE_BLANK;
Johan Hovold98e74212014-04-26 11:53:44 +0200918 i2c_header->Size = cpu_to_le16(buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 i2c_header->CheckSum = cs;
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530920 firmware_rec->Ver_Major = OperationalMajorVersion;
921 firmware_rec->Ver_Minor = OperationalMinorVersion;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 return 0;
924}
925
926/* Try to figure out what type of I2c we have */
Alan Cox2742fd82008-04-29 14:45:15 +0100927static int i2c_type_bootmode(struct edgeport_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
929 int status;
Johan Hovolde9305d22009-12-28 23:01:50 +0100930 u8 *data;
931
932 data = kmalloc(1, GFP_KERNEL);
933 if (!data) {
934 dev_err(&serial->serial->dev->dev,
935 "%s - out of memory\n", __func__);
936 return -ENOMEM;
937 }
Alan Cox2742fd82008-04-29 14:45:15 +0100938
939 /* Try to read type 2 */
940 status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
Johan Hovolde9305d22009-12-28 23:01:50 +0100941 DTK_ADDR_SPACE_I2C_TYPE_II, 0, data, 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 if (status)
Alan Cox2742fd82008-04-29 14:45:15 +0100943 dbg("%s - read 2 status error = %d", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 else
Johan Hovolde9305d22009-12-28 23:01:50 +0100945 dbg("%s - read 2 data = 0x%x", __func__, *data);
946 if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
Alan Cox2742fd82008-04-29 14:45:15 +0100947 dbg("%s - ROM_TYPE_II", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
Johan Hovolde9305d22009-12-28 23:01:50 +0100949 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 }
951
Alan Cox2742fd82008-04-29 14:45:15 +0100952 /* Try to read type 3 */
953 status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
Johan Hovolde9305d22009-12-28 23:01:50 +0100954 DTK_ADDR_SPACE_I2C_TYPE_III, 0, data, 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 if (status)
Alan Cox2742fd82008-04-29 14:45:15 +0100956 dbg("%s - read 3 status error = %d", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 else
Johan Hovolde9305d22009-12-28 23:01:50 +0100958 dbg("%s - read 2 data = 0x%x", __func__, *data);
959 if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
Alan Cox2742fd82008-04-29 14:45:15 +0100960 dbg("%s - ROM_TYPE_III", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_III;
Johan Hovolde9305d22009-12-28 23:01:50 +0100962 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 }
964
Alan Cox2742fd82008-04-29 14:45:15 +0100965 dbg("%s - Unknown", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
Johan Hovolde9305d22009-12-28 23:01:50 +0100967 status = -ENODEV;
968out:
969 kfree(data);
970 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971}
972
Alan Cox2742fd82008-04-29 14:45:15 +0100973static int bulk_xfer(struct usb_serial *serial, void *buffer,
974 int length, int *num_sent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
976 int status;
977
Alan Cox2742fd82008-04-29 14:45:15 +0100978 status = usb_bulk_msg(serial->dev,
979 usb_sndbulkpipe(serial->dev,
980 serial->port[0]->bulk_out_endpointAddress),
981 buffer, length, num_sent, 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 return status;
983}
984
985/* Download given firmware image to the device (IN BOOT MODE) */
Alan Cox2742fd82008-04-29 14:45:15 +0100986static int download_code(struct edgeport_serial *serial, __u8 *image,
987 int image_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
989 int status = 0;
990 int pos;
991 int transfer;
992 int done;
993
Alan Cox2742fd82008-04-29 14:45:15 +0100994 /* Transfer firmware image */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 for (pos = 0; pos < image_length; ) {
Alan Cox2742fd82008-04-29 14:45:15 +0100996 /* Read the next buffer from file */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 transfer = image_length - pos;
998 if (transfer > EDGE_FW_BULK_MAX_PACKET_SIZE)
999 transfer = EDGE_FW_BULK_MAX_PACKET_SIZE;
1000
Alan Cox2742fd82008-04-29 14:45:15 +01001001 /* Transfer data */
1002 status = bulk_xfer(serial->serial, &image[pos],
1003 transfer, &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 if (status)
1005 break;
Alan Cox2742fd82008-04-29 14:45:15 +01001006 /* Advance buffer pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 pos += done;
1008 }
1009
1010 return status;
1011}
1012
Alan Cox2742fd82008-04-29 14:45:15 +01001013/* FIXME!!! */
1014static int config_boot_dev(struct usb_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015{
1016 return 0;
1017}
1018
Alan Cox2742fd82008-04-29 14:45:15 +01001019static int ti_cpu_rev(struct edge_ti_manuf_descriptor *desc)
1020{
1021 return TI_GET_CPU_REVISION(desc->CpuRev_BoardRev);
1022}
1023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024/**
1025 * DownloadTIFirmware - Download run-time operating firmware to the TI5052
Alan Cox2742fd82008-04-29 14:45:15 +01001026 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 * This routine downloads the main operating code into the TI5052, using the
1028 * boot code already burned into E2PROM or ROM.
1029 */
Alan Cox2742fd82008-04-29 14:45:15 +01001030static int download_fw(struct edgeport_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
1032 struct device *dev = &serial->serial->dev->dev;
1033 int status = 0;
1034 int start_address;
1035 struct edge_ti_manuf_descriptor *ti_manuf_desc;
1036 struct usb_interface_descriptor *interface;
1037 int download_cur_ver;
1038 int download_new_ver;
1039
1040 /* This routine is entered by both the BOOT mode and the Download mode
1041 * We can determine which code is running by the reading the config
1042 * descriptor and if we have only one bulk pipe it is in boot mode
1043 */
1044 serial->product_info.hardware_type = HARDWARE_TYPE_TIUMP;
1045
1046 /* Default to type 2 i2c */
1047 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
1048
Alan Cox2742fd82008-04-29 14:45:15 +01001049 status = choose_config(serial->serial->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 if (status)
1051 return status;
1052
1053 interface = &serial->serial->interface->cur_altsetting->desc;
1054 if (!interface) {
Alan Cox2742fd82008-04-29 14:45:15 +01001055 dev_err(dev, "%s - no interface set, error!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 return -ENODEV;
1057 }
1058
Alan Cox2742fd82008-04-29 14:45:15 +01001059 /*
1060 * Setup initial mode -- the default mode 0 is TI_MODE_CONFIGURING
1061 * if we have more than one endpoint we are definitely in download
1062 * mode
1063 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 if (interface->bNumEndpoints > 1)
1065 serial->product_info.TiMode = TI_MODE_DOWNLOAD;
1066 else
Alan Cox2742fd82008-04-29 14:45:15 +01001067 /* Otherwise we will remain in configuring mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 serial->product_info.TiMode = TI_MODE_CONFIGURING;
1069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 /********************************************************************/
1071 /* Download Mode */
1072 /********************************************************************/
1073 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
1074 struct ti_i2c_desc *rom_desc;
1075
Andrew Morton9544e832008-02-04 23:57:50 -08001076 dbg("%s - RUNNING IN DOWNLOAD MODE", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Alan Cox2742fd82008-04-29 14:45:15 +01001078 status = check_i2c_image(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 if (status) {
Andrew Morton9544e832008-02-04 23:57:50 -08001080 dbg("%s - DOWNLOAD MODE -- BAD I2C", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 return status;
1082 }
Alan Cox2742fd82008-04-29 14:45:15 +01001083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 /* Validate Hardware version number
1085 * Read Manufacturing Descriptor from TI Based Edgeport
1086 */
Alan Cox2742fd82008-04-29 14:45:15 +01001087 ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 if (!ti_manuf_desc) {
Alan Cox2742fd82008-04-29 14:45:15 +01001089 dev_err(dev, "%s - out of memory.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 return -ENOMEM;
1091 }
Alan Cox2742fd82008-04-29 14:45:15 +01001092 status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001094 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 return status;
1096 }
1097
Alan Cox2742fd82008-04-29 14:45:15 +01001098 /* Check version number of ION descriptor */
1099 if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
1100 dbg("%s - Wrong CPU Rev %d (Must be 2)",
1101 __func__, ti_cpu_rev(ti_manuf_desc));
1102 kfree(ti_manuf_desc);
1103 return -EINVAL;
1104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Alan Cox2742fd82008-04-29 14:45:15 +01001106 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 if (!rom_desc) {
Alan Cox2742fd82008-04-29 14:45:15 +01001108 dev_err(dev, "%s - out of memory.\n", __func__);
1109 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 return -ENOMEM;
1111 }
1112
Alan Cox2742fd82008-04-29 14:45:15 +01001113 /* Search for type 2 record (firmware record) */
1114 start_address = get_descriptor_addr(serial,
1115 I2C_DESC_TYPE_FIRMWARE_BASIC, rom_desc);
1116 if (start_address != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 struct ti_i2c_firmware_rec *firmware_version;
Johan Hovolde9305d22009-12-28 23:01:50 +01001118 u8 *record;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Alan Cox2742fd82008-04-29 14:45:15 +01001120 dbg("%s - Found Type FIRMWARE (Type 2) record",
1121 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Alan Cox2742fd82008-04-29 14:45:15 +01001123 firmware_version = kmalloc(sizeof(*firmware_version),
1124 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 if (!firmware_version) {
Alan Cox2742fd82008-04-29 14:45:15 +01001126 dev_err(dev, "%s - out of memory.\n", __func__);
1127 kfree(rom_desc);
1128 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 return -ENOMEM;
1130 }
1131
Alan Cox2742fd82008-04-29 14:45:15 +01001132 /* Validate version number
1133 * Read the descriptor data
1134 */
1135 status = read_rom(serial, start_address +
1136 sizeof(struct ti_i2c_desc),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 sizeof(struct ti_i2c_firmware_rec),
1138 (__u8 *)firmware_version);
1139 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001140 kfree(firmware_version);
1141 kfree(rom_desc);
1142 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 return status;
1144 }
1145
Alan Cox2742fd82008-04-29 14:45:15 +01001146 /* Check version number of download with current
1147 version in I2c */
1148 download_cur_ver = (firmware_version->Ver_Major << 8) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 (firmware_version->Ver_Minor);
Jaswinder Singhd12b2192008-07-04 23:06:09 +05301150 download_new_ver = (OperationalMajorVersion << 8) +
1151 (OperationalMinorVersion);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Alan Cox2742fd82008-04-29 14:45:15 +01001153 dbg("%s - >> FW Versions Device %d.%d Driver %d.%d",
1154 __func__,
1155 firmware_version->Ver_Major,
1156 firmware_version->Ver_Minor,
1157 OperationalMajorVersion,
1158 OperationalMinorVersion);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Alan Cox2742fd82008-04-29 14:45:15 +01001160 /* Check if we have an old version in the I2C and
1161 update if necessary */
Greg Kroah-Hartman0827a9f2010-08-17 15:15:37 -07001162 if (download_cur_ver < download_new_ver) {
Alan Cox2742fd82008-04-29 14:45:15 +01001163 dbg("%s - Update I2C dld from %d.%d to %d.%d",
1164 __func__,
1165 firmware_version->Ver_Major,
1166 firmware_version->Ver_Minor,
1167 OperationalMajorVersion,
1168 OperationalMinorVersion);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Johan Hovolde9305d22009-12-28 23:01:50 +01001170 record = kmalloc(1, GFP_KERNEL);
1171 if (!record) {
1172 dev_err(dev, "%s - out of memory.\n",
1173 __func__);
1174 kfree(firmware_version);
1175 kfree(rom_desc);
1176 kfree(ti_manuf_desc);
1177 return -ENOMEM;
1178 }
Alan Cox2742fd82008-04-29 14:45:15 +01001179 /* In order to update the I2C firmware we must
1180 * change the type 2 record to type 0xF2. This
1181 * will force the UMP to come up in Boot Mode.
1182 * Then while in boot mode, the driver will
1183 * download the latest firmware (padded to
1184 * 15.5k) into the UMP ram. Finally when the
1185 * device comes back up in download mode the
1186 * driver will cause the new firmware to be
1187 * copied from the UMP Ram to I2C and the
1188 * firmware will update the record type from
1189 * 0xf2 to 0x02.
1190 */
Johan Hovolde9305d22009-12-28 23:01:50 +01001191 *record = I2C_DESC_TYPE_FIRMWARE_BLANK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Alan Cox2742fd82008-04-29 14:45:15 +01001193 /* Change the I2C Firmware record type to
1194 0xf2 to trigger an update */
1195 status = write_rom(serial, start_address,
Johan Hovolde9305d22009-12-28 23:01:50 +01001196 sizeof(*record), record);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 if (status) {
Johan Hovolde9305d22009-12-28 23:01:50 +01001198 kfree(record);
Alan Cox2742fd82008-04-29 14:45:15 +01001199 kfree(firmware_version);
1200 kfree(rom_desc);
1201 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 return status;
1203 }
1204
Alan Cox2742fd82008-04-29 14:45:15 +01001205 /* verify the write -- must do this in order
1206 * for write to complete before we do the
1207 * hardware reset
1208 */
1209 status = read_rom(serial,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 start_address,
Johan Hovolde9305d22009-12-28 23:01:50 +01001211 sizeof(*record),
1212 record);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 if (status) {
Johan Hovolde9305d22009-12-28 23:01:50 +01001214 kfree(record);
Alan Cox2742fd82008-04-29 14:45:15 +01001215 kfree(firmware_version);
1216 kfree(rom_desc);
1217 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 return status;
1219 }
1220
Johan Hovolde9305d22009-12-28 23:01:50 +01001221 if (*record != I2C_DESC_TYPE_FIRMWARE_BLANK) {
Alan Cox2742fd82008-04-29 14:45:15 +01001222 dev_err(dev,
1223 "%s - error resetting device\n",
1224 __func__);
Johan Hovolde9305d22009-12-28 23:01:50 +01001225 kfree(record);
Alan Cox2742fd82008-04-29 14:45:15 +01001226 kfree(firmware_version);
1227 kfree(rom_desc);
1228 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 return -ENODEV;
1230 }
1231
Alan Cox2742fd82008-04-29 14:45:15 +01001232 dbg("%s - HARDWARE RESET", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Alan Cox2742fd82008-04-29 14:45:15 +01001234 /* Reset UMP -- Back to BOOT MODE */
1235 status = ti_vsend_sync(serial->serial->dev,
1236 UMPC_HARDWARE_RESET,
1237 0, 0, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Alan Cox2742fd82008-04-29 14:45:15 +01001239 dbg("%s - HARDWARE RESET return %d",
1240 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242 /* return an error on purpose. */
Johan Hovolde9305d22009-12-28 23:01:50 +01001243 kfree(record);
Alan Cox2742fd82008-04-29 14:45:15 +01001244 kfree(firmware_version);
1245 kfree(rom_desc);
1246 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 return -ENODEV;
1248 }
Alan Cox2742fd82008-04-29 14:45:15 +01001249 kfree(firmware_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 }
Alan Cox2742fd82008-04-29 14:45:15 +01001251 /* Search for type 0xF2 record (firmware blank record) */
1252 else if ((start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_FIRMWARE_BLANK, rom_desc)) != 0) {
1253#define HEADER_SIZE (sizeof(struct ti_i2c_desc) + \
1254 sizeof(struct ti_i2c_firmware_rec))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 __u8 *header;
1256 __u8 *vheader;
1257
Alan Cox2742fd82008-04-29 14:45:15 +01001258 header = kmalloc(HEADER_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 if (!header) {
Alan Cox2742fd82008-04-29 14:45:15 +01001260 dev_err(dev, "%s - out of memory.\n", __func__);
1261 kfree(rom_desc);
1262 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 return -ENOMEM;
1264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Alan Cox2742fd82008-04-29 14:45:15 +01001266 vheader = kmalloc(HEADER_SIZE, GFP_KERNEL);
1267 if (!vheader) {
1268 dev_err(dev, "%s - out of memory.\n", __func__);
1269 kfree(header);
1270 kfree(rom_desc);
1271 kfree(ti_manuf_desc);
1272 return -ENOMEM;
1273 }
1274
1275 dbg("%s - Found Type BLANK FIRMWARE (Type F2) record",
1276 __func__);
1277
1278 /*
1279 * In order to update the I2C firmware we must change
1280 * the type 2 record to type 0xF2. This will force the
1281 * UMP to come up in Boot Mode. Then while in boot
1282 * mode, the driver will download the latest firmware
1283 * (padded to 15.5k) into the UMP ram. Finally when the
1284 * device comes back up in download mode the driver
1285 * will cause the new firmware to be copied from the
1286 * UMP Ram to I2C and the firmware will update the
1287 * record type from 0xf2 to 0x02.
1288 */
1289 status = build_i2c_fw_hdr(header, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001291 kfree(vheader);
1292 kfree(header);
1293 kfree(rom_desc);
1294 kfree(ti_manuf_desc);
Roel Kluinfd6e5bb2010-08-10 14:29:19 -07001295 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 }
1297
Alan Cox2742fd82008-04-29 14:45:15 +01001298 /* Update I2C with type 0xf2 record with correct
1299 size and checksum */
1300 status = write_rom(serial,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 start_address,
1302 HEADER_SIZE,
1303 header);
1304 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001305 kfree(vheader);
1306 kfree(header);
1307 kfree(rom_desc);
1308 kfree(ti_manuf_desc);
Roel Kluin9800eb32010-07-20 15:29:08 -07001309 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 }
1311
Alan Cox2742fd82008-04-29 14:45:15 +01001312 /* verify the write -- must do this in order for
1313 write to complete before we do the hardware reset */
1314 status = read_rom(serial, start_address,
1315 HEADER_SIZE, vheader);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001318 dbg("%s - can't read header back", __func__);
1319 kfree(vheader);
1320 kfree(header);
1321 kfree(rom_desc);
1322 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 return status;
1324 }
1325 if (memcmp(vheader, header, HEADER_SIZE)) {
Alan Cox2742fd82008-04-29 14:45:15 +01001326 dbg("%s - write download record failed",
1327 __func__);
1328 kfree(vheader);
1329 kfree(header);
1330 kfree(rom_desc);
1331 kfree(ti_manuf_desc);
Roel Kluin1e297092010-07-02 00:36:43 +02001332 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 }
1334
Alan Cox2742fd82008-04-29 14:45:15 +01001335 kfree(vheader);
1336 kfree(header);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Alan Cox2742fd82008-04-29 14:45:15 +01001338 dbg("%s - Start firmware update", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Alan Cox2742fd82008-04-29 14:45:15 +01001340 /* Tell firmware to copy download image into I2C */
1341 status = ti_vsend_sync(serial->serial->dev,
1342 UMPC_COPY_DNLD_TO_I2C, 0, 0, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Alan Cox2742fd82008-04-29 14:45:15 +01001344 dbg("%s - Update complete 0x%x", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001346 dev_err(dev,
1347 "%s - UMPC_COPY_DNLD_TO_I2C failed\n",
1348 __func__);
1349 kfree(rom_desc);
1350 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 return status;
1352 }
1353 }
1354
1355 // The device is running the download code
Alan Cox2742fd82008-04-29 14:45:15 +01001356 kfree(rom_desc);
1357 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 return 0;
1359 }
1360
1361 /********************************************************************/
1362 /* Boot Mode */
1363 /********************************************************************/
Andrew Morton9544e832008-02-04 23:57:50 -08001364 dbg("%s - RUNNING IN BOOT MODE", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Alan Cox2742fd82008-04-29 14:45:15 +01001366 /* Configure the TI device so we can use the BULK pipes for download */
1367 status = config_boot_dev(serial->serial->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 if (status)
1369 return status;
1370
Alan Cox2742fd82008-04-29 14:45:15 +01001371 if (le16_to_cpu(serial->serial->dev->descriptor.idVendor)
1372 != USB_VENDOR_ID_ION) {
1373 dbg("%s - VID = 0x%x", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 le16_to_cpu(serial->serial->dev->descriptor.idVendor));
1375 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
Alan Cox2742fd82008-04-29 14:45:15 +01001376 goto stayinbootmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 }
1378
Alan Cox2742fd82008-04-29 14:45:15 +01001379 /* We have an ION device (I2c Must be programmed)
1380 Determine I2C image type */
1381 if (i2c_type_bootmode(serial))
1382 goto stayinbootmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Alan Cox2742fd82008-04-29 14:45:15 +01001384 /* Check for ION Vendor ID and that the I2C is valid */
1385 if (!check_i2c_image(serial)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 struct ti_i2c_image_header *header;
1387 int i;
1388 __u8 cs = 0;
1389 __u8 *buffer;
1390 int buffer_size;
Jaswinder Singhd12b2192008-07-04 23:06:09 +05301391 int err;
1392 const struct firmware *fw;
1393 const char *fw_name = "edgeport/down3.bin";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 /* Validate Hardware version number
1396 * Read Manufacturing Descriptor from TI Based Edgeport
1397 */
Alan Cox2742fd82008-04-29 14:45:15 +01001398 ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 if (!ti_manuf_desc) {
Alan Cox2742fd82008-04-29 14:45:15 +01001400 dev_err(dev, "%s - out of memory.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 return -ENOMEM;
1402 }
Alan Cox2742fd82008-04-29 14:45:15 +01001403 status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001405 kfree(ti_manuf_desc);
1406 goto stayinbootmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 }
1408
Alan Cox2742fd82008-04-29 14:45:15 +01001409 /* Check for version 2 */
1410 if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
1411 dbg("%s - Wrong CPU Rev %d (Must be 2)",
1412 __func__, ti_cpu_rev(ti_manuf_desc));
1413 kfree(ti_manuf_desc);
1414 goto stayinbootmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 }
1416
Alan Cox2742fd82008-04-29 14:45:15 +01001417 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 /*
Alan Cox2742fd82008-04-29 14:45:15 +01001420 * In order to update the I2C firmware we must change the type
1421 * 2 record to type 0xF2. This will force the UMP to come up
1422 * in Boot Mode. Then while in boot mode, the driver will
1423 * download the latest firmware (padded to 15.5k) into the
1424 * UMP ram. Finally when the device comes back up in download
1425 * mode the driver will cause the new firmware to be copied
1426 * from the UMP Ram to I2C and the firmware will update the
1427 * record type from 0xf2 to 0x02.
1428 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 * Do we really have to copy the whole firmware image,
1430 * or could we do this in place!
1431 */
1432
Alan Cox2742fd82008-04-29 14:45:15 +01001433 /* Allocate a 15.5k buffer + 3 byte header */
1434 buffer_size = (((1024 * 16) - 512) +
1435 sizeof(struct ti_i2c_image_header));
1436 buffer = kmalloc(buffer_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 if (!buffer) {
Alan Cox2742fd82008-04-29 14:45:15 +01001438 dev_err(dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 return -ENOMEM;
1440 }
Alan Cox2742fd82008-04-29 14:45:15 +01001441
1442 /* Initialize the buffer to 0xff (pad the buffer) */
1443 memset(buffer, 0xff, buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Jaswinder Singhd12b2192008-07-04 23:06:09 +05301445 err = request_firmware(&fw, fw_name, dev);
1446 if (err) {
1447 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
1448 fw_name, err);
1449 kfree(buffer);
1450 return err;
1451 }
1452 memcpy(buffer, &fw->data[4], fw->size - 4);
1453 release_firmware(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Alan Cox2742fd82008-04-29 14:45:15 +01001455 for (i = sizeof(struct ti_i2c_image_header);
1456 i < buffer_size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 cs = (__u8)(cs + buffer[i]);
1458 }
Alan Cox2742fd82008-04-29 14:45:15 +01001459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 header = (struct ti_i2c_image_header *)buffer;
Alan Cox2742fd82008-04-29 14:45:15 +01001461
1462 /* update length and checksum after padding */
1463 header->Length = cpu_to_le16((__u16)(buffer_size -
1464 sizeof(struct ti_i2c_image_header)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 header->CheckSum = cs;
1466
Alan Cox2742fd82008-04-29 14:45:15 +01001467 /* Download the operational code */
1468 dbg("%s - Downloading operational code image (TI UMP)",
1469 __func__);
1470 status = download_code(serial, buffer, buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Alan Cox2742fd82008-04-29 14:45:15 +01001472 kfree(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
1474 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001475 dbg("%s - Error downloading operational code image",
1476 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 return status;
1478 }
1479
Alan Cox2742fd82008-04-29 14:45:15 +01001480 /* Device will reboot */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 serial->product_info.TiMode = TI_MODE_TRANSITIONING;
1482
Alan Cox2742fd82008-04-29 14:45:15 +01001483 dbg("%s - Download successful -- Device rebooting...",
1484 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
1486 /* return an error on purpose */
1487 return -ENODEV;
1488 }
1489
Alan Cox2742fd82008-04-29 14:45:15 +01001490stayinbootmode:
1491 /* Eprom is invalid or blank stay in boot mode */
Andrew Morton9544e832008-02-04 23:57:50 -08001492 dbg("%s - STAYING IN BOOT MODE", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 serial->product_info.TiMode = TI_MODE_BOOT;
1494
1495 return 0;
1496}
1497
1498
Alan Cox2742fd82008-04-29 14:45:15 +01001499static int ti_do_config(struct edgeport_port *port, int feature, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
1501 int port_number = port->port->number - port->port->serial->minor;
Alan Cox2742fd82008-04-29 14:45:15 +01001502 on = !!on; /* 1 or 0 not bitmask */
1503 return send_cmd(port->port->serial->dev,
1504 feature, (__u8)(UMPM_UART1_PORT + port_number),
1505 on, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506}
1507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Alan Cox2742fd82008-04-29 14:45:15 +01001509static int restore_mcr(struct edgeport_port *port, __u8 mcr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
1511 int status = 0;
1512
Alan Cox2742fd82008-04-29 14:45:15 +01001513 dbg("%s - %x", __func__, mcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Alan Cox2742fd82008-04-29 14:45:15 +01001515 status = ti_do_config(port, UMPC_SET_CLR_DTR, mcr & MCR_DTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 if (status)
1517 return status;
Alan Cox2742fd82008-04-29 14:45:15 +01001518 status = ti_do_config(port, UMPC_SET_CLR_RTS, mcr & MCR_RTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 if (status)
1520 return status;
Alan Cox2742fd82008-04-29 14:45:15 +01001521 return ti_do_config(port, UMPC_SET_CLR_LOOPBACK, mcr & MCR_LOOPBACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522}
1523
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524/* Convert TI LSR to standard UART flags */
Alan Cox2742fd82008-04-29 14:45:15 +01001525static __u8 map_line_status(__u8 ti_lsr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
1527 __u8 lsr = 0;
1528
1529#define MAP_FLAG(flagUmp, flagUart) \
1530 if (ti_lsr & flagUmp) \
1531 lsr |= flagUart;
1532
1533 MAP_FLAG(UMP_UART_LSR_OV_MASK, LSR_OVER_ERR) /* overrun */
1534 MAP_FLAG(UMP_UART_LSR_PE_MASK, LSR_PAR_ERR) /* parity error */
1535 MAP_FLAG(UMP_UART_LSR_FE_MASK, LSR_FRM_ERR) /* framing error */
1536 MAP_FLAG(UMP_UART_LSR_BR_MASK, LSR_BREAK) /* break detected */
Alan Cox2742fd82008-04-29 14:45:15 +01001537 MAP_FLAG(UMP_UART_LSR_RX_MASK, LSR_RX_AVAIL) /* rx data available */
1538 MAP_FLAG(UMP_UART_LSR_TX_MASK, LSR_TX_EMPTY) /* tx hold reg empty */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
1540#undef MAP_FLAG
1541
1542 return lsr;
1543}
1544
Alan Cox2742fd82008-04-29 14:45:15 +01001545static void handle_new_msr(struct edgeport_port *edge_port, __u8 msr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546{
1547 struct async_icount *icount;
1548 struct tty_struct *tty;
1549
Alan Cox2742fd82008-04-29 14:45:15 +01001550 dbg("%s - %02x", __func__, msr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Alan Cox2742fd82008-04-29 14:45:15 +01001552 if (msr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
1553 EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 icount = &edge_port->icount;
1555
1556 /* update input line counters */
1557 if (msr & EDGEPORT_MSR_DELTA_CTS)
1558 icount->cts++;
1559 if (msr & EDGEPORT_MSR_DELTA_DSR)
1560 icount->dsr++;
1561 if (msr & EDGEPORT_MSR_DELTA_CD)
1562 icount->dcd++;
1563 if (msr & EDGEPORT_MSR_DELTA_RI)
1564 icount->rng++;
Johan Hovoldfad70e42013-03-19 09:21:17 +01001565 wake_up_interruptible(&edge_port->port->delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 }
1567
1568 /* Save the new modem status */
1569 edge_port->shadow_msr = msr & 0xf0;
1570
Alan Cox4a90f092008-10-13 10:39:46 +01001571 tty = tty_port_tty_get(&edge_port->port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 /* handle CTS flow control */
1573 if (tty && C_CRTSCTS(tty)) {
1574 if (msr & EDGEPORT_MSR_CTS) {
1575 tty->hw_stopped = 0;
1576 tty_wakeup(tty);
1577 } else {
1578 tty->hw_stopped = 1;
1579 }
1580 }
Alan Cox4a90f092008-10-13 10:39:46 +01001581 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582}
1583
Alan Cox2742fd82008-04-29 14:45:15 +01001584static void handle_new_lsr(struct edgeport_port *edge_port, int lsr_data,
1585 __u8 lsr, __u8 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586{
1587 struct async_icount *icount;
Alan Cox2742fd82008-04-29 14:45:15 +01001588 __u8 new_lsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR |
1589 LSR_FRM_ERR | LSR_BREAK));
Alan Cox4a90f092008-10-13 10:39:46 +01001590 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Alan Cox2742fd82008-04-29 14:45:15 +01001592 dbg("%s - %02x", __func__, new_lsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
1594 edge_port->shadow_lsr = lsr;
1595
Alan Cox2742fd82008-04-29 14:45:15 +01001596 if (new_lsr & LSR_BREAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 /*
1598 * Parity and Framing errors only count if they
1599 * occur exclusive of a break being received.
1600 */
1601 new_lsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
1603 /* Place LSR data byte into Rx buffer */
Alan Cox4a90f092008-10-13 10:39:46 +01001604 if (lsr_data) {
1605 tty = tty_port_tty_get(&edge_port->port->port);
1606 if (tty) {
1607 edge_tty_recv(&edge_port->port->dev, tty, &data, 1);
1608 tty_kref_put(tty);
1609 }
1610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
1612 /* update input line counters */
1613 icount = &edge_port->icount;
1614 if (new_lsr & LSR_BREAK)
1615 icount->brk++;
1616 if (new_lsr & LSR_OVER_ERR)
1617 icount->overrun++;
1618 if (new_lsr & LSR_PAR_ERR)
1619 icount->parity++;
1620 if (new_lsr & LSR_FRM_ERR)
1621 icount->frame++;
1622}
1623
1624
Alan Cox2742fd82008-04-29 14:45:15 +01001625static void edge_interrupt_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626{
Ming Leicdc97792008-02-24 18:41:47 +08001627 struct edgeport_serial *edge_serial = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 struct usb_serial_port *port;
1629 struct edgeport_port *edge_port;
1630 unsigned char *data = urb->transfer_buffer;
1631 int length = urb->actual_length;
1632 int port_number;
1633 int function;
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001634 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 __u8 lsr;
1636 __u8 msr;
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001637 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Harvey Harrison441b62c2008-03-03 16:08:34 -08001639 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001641 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 case 0:
1643 /* success */
1644 break;
1645 case -ECONNRESET:
1646 case -ENOENT:
1647 case -ESHUTDOWN:
1648 /* this urb is terminated, clean up */
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001649 dbg("%s - urb shutting down with status: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001650 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 return;
1652 default:
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001653 dev_err(&urb->dev->dev, "%s - nonzero urb status received: "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001654 "%d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 goto exit;
1656 }
1657
1658 if (!length) {
Alan Cox2742fd82008-04-29 14:45:15 +01001659 dbg("%s - no data in urb", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 goto exit;
1661 }
1662
Alan Cox2742fd82008-04-29 14:45:15 +01001663 usb_serial_debug_data(debug, &edge_serial->serial->dev->dev,
1664 __func__, length, data);
1665
1666 if (length != 2) {
1667 dbg("%s - expecting packet of size 2, got %d",
1668 __func__, length);
1669 goto exit;
1670 }
1671
1672 port_number = TIUMP_GET_PORT_FROM_CODE(data[0]);
1673 function = TIUMP_GET_FUNC_FROM_CODE(data[0]);
1674 dbg("%s - port_number %d, function %d, info 0x%x",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001675 __func__, port_number, function, data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 port = edge_serial->serial->port[port_number];
1677 edge_port = usb_get_serial_port_data(port);
1678 if (!edge_port) {
Alan Cox2742fd82008-04-29 14:45:15 +01001679 dbg("%s - edge_port not found", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 return;
1681 }
1682 switch (function) {
1683 case TIUMP_INTERRUPT_CODE_LSR:
Alan Cox2742fd82008-04-29 14:45:15 +01001684 lsr = map_line_status(data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 if (lsr & UMP_UART_LSR_DATA_MASK) {
Alan Cox2742fd82008-04-29 14:45:15 +01001686 /* Save the LSR event for bulk read
1687 completion routine */
1688 dbg("%s - LSR Event Port %u LSR Status = %02x",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001689 __func__, port_number, lsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 edge_port->lsr_event = 1;
1691 edge_port->lsr_mask = lsr;
1692 } else {
Alan Cox2742fd82008-04-29 14:45:15 +01001693 dbg("%s - ===== Port %d LSR Status = %02x ======",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001694 __func__, port_number, lsr);
Alan Cox2742fd82008-04-29 14:45:15 +01001695 handle_new_lsr(edge_port, 0, lsr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 }
1697 break;
1698
Alan Cox2742fd82008-04-29 14:45:15 +01001699 case TIUMP_INTERRUPT_CODE_MSR: /* MSR */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 /* Copy MSR from UMP */
1701 msr = data[1];
Joe Perches759f3632010-02-05 16:50:08 -08001702 dbg("%s - ===== Port %u MSR Status = %02x ======",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001703 __func__, port_number, msr);
Alan Cox2742fd82008-04-29 14:45:15 +01001704 handle_new_msr(edge_port, msr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 break;
1706
1707 default:
Alan Cox2742fd82008-04-29 14:45:15 +01001708 dev_err(&urb->dev->dev,
1709 "%s - Unknown Interrupt code from UMP %x\n",
1710 __func__, data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 break;
Alan Cox2742fd82008-04-29 14:45:15 +01001712
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 }
1714
1715exit:
Alan Cox2742fd82008-04-29 14:45:15 +01001716 retval = usb_submit_urb(urb, GFP_ATOMIC);
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001717 if (retval)
Alan Cox2742fd82008-04-29 14:45:15 +01001718 dev_err(&urb->dev->dev,
1719 "%s - usb_submit_urb failed with result %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001720 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721}
1722
Alan Cox2742fd82008-04-29 14:45:15 +01001723static void edge_bulk_in_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724{
Ming Leicdc97792008-02-24 18:41:47 +08001725 struct edgeport_port *edge_port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 unsigned char *data = urb->transfer_buffer;
1727 struct tty_struct *tty;
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001728 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 int port_number;
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001730 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
Harvey Harrison441b62c2008-03-03 16:08:34 -08001732 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001734 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 case 0:
1736 /* success */
1737 break;
1738 case -ECONNRESET:
1739 case -ENOENT:
1740 case -ESHUTDOWN:
1741 /* this urb is terminated, clean up */
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001742 dbg("%s - urb shutting down with status: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001743 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 return;
1745 default:
Alan Cox2742fd82008-04-29 14:45:15 +01001746 dev_err(&urb->dev->dev,
1747 "%s - nonzero read bulk status received: %d\n",
1748 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 }
1750
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001751 if (status == -EPIPE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 goto exit;
1753
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001754 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001755 dev_err(&urb->dev->dev, "%s - stopping read!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 return;
1757 }
1758
1759 port_number = edge_port->port->number - edge_port->port->serial->minor;
1760
1761 if (edge_port->lsr_event) {
1762 edge_port->lsr_event = 0;
Alan Cox2742fd82008-04-29 14:45:15 +01001763 dbg("%s ===== Port %u LSR Status = %02x, Data = %02x ======",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001764 __func__, port_number, edge_port->lsr_mask, *data);
Alan Cox2742fd82008-04-29 14:45:15 +01001765 handle_new_lsr(edge_port, 1, edge_port->lsr_mask, *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 /* Adjust buffer length/pointer */
1767 --urb->actual_length;
1768 ++data;
1769 }
1770
Alan Cox4a90f092008-10-13 10:39:46 +01001771 tty = tty_port_tty_get(&edge_port->port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 if (tty && urb->actual_length) {
Alan Cox2742fd82008-04-29 14:45:15 +01001773 usb_serial_debug_data(debug, &edge_port->port->dev,
1774 __func__, urb->actual_length, data);
1775 if (edge_port->close_pending)
1776 dbg("%s - close pending, dropping data on the floor",
1777 __func__);
1778 else
1779 edge_tty_recv(&edge_port->port->dev, tty, data,
1780 urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 edge_port->icount.rx += urb->actual_length;
1782 }
Alan Cox4a90f092008-10-13 10:39:46 +01001783 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
1785exit:
1786 /* continue read unless stopped */
1787 spin_lock(&edge_port->ep_lock);
Johan Hovold58330412011-11-06 19:06:28 +01001788 if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001789 retval = usb_submit_urb(urb, GFP_ATOMIC);
Johan Hovold58330412011-11-06 19:06:28 +01001790 else if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPED;
Johan Hovold58330412011-11-06 19:06:28 +01001792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 spin_unlock(&edge_port->ep_lock);
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001794 if (retval)
Alan Cox2742fd82008-04-29 14:45:15 +01001795 dev_err(&urb->dev->dev,
1796 "%s - usb_submit_urb failed with result %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001797 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798}
1799
Alan Cox2742fd82008-04-29 14:45:15 +01001800static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
1801 unsigned char *data, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802{
Alan Cox2742fd82008-04-29 14:45:15 +01001803 int queued;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Alan Cox2742fd82008-04-29 14:45:15 +01001805 queued = tty_insert_flip_string(tty, data, length);
1806 if (queued < length)
1807 dev_err(dev, "%s - dropping data, %d bytes lost\n",
1808 __func__, length - queued);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 tty_flip_buffer_push(tty);
1810}
1811
Alan Cox2742fd82008-04-29 14:45:15 +01001812static void edge_bulk_out_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813{
Ming Leicdc97792008-02-24 18:41:47 +08001814 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001816 int status = urb->status;
Alan Cox4a90f092008-10-13 10:39:46 +01001817 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
Alan Cox2742fd82008-04-29 14:45:15 +01001819 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
1821 edge_port->ep_write_urb_in_use = 0;
1822
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001823 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 case 0:
1825 /* success */
1826 break;
1827 case -ECONNRESET:
1828 case -ENOENT:
1829 case -ESHUTDOWN:
1830 /* this urb is terminated, clean up */
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001831 dbg("%s - urb shutting down with status: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001832 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 return;
1834 default:
Johan Hovold22a416c2012-02-10 13:20:51 +01001835 dev_err_console(port, "%s - nonzero write bulk status "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001836 "received: %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 }
1838
1839 /* send any buffered data */
Alan Cox4a90f092008-10-13 10:39:46 +01001840 tty = tty_port_tty_get(&port->port);
1841 edge_send(tty);
1842 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843}
1844
Alan Coxa509a7e2009-09-19 13:13:26 -07001845static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846{
1847 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1848 struct edgeport_serial *edge_serial;
1849 struct usb_device *dev;
1850 struct urb *urb;
1851 int port_number;
1852 int status;
1853 u16 open_settings;
1854 u8 transaction_timeout;
1855
Harvey Harrison441b62c2008-03-03 16:08:34 -08001856 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
1858 if (edge_port == NULL)
1859 return -ENODEV;
1860
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 port_number = port->number - port->serial->minor;
1862 switch (port_number) {
Alan Cox2742fd82008-04-29 14:45:15 +01001863 case 0:
1864 edge_port->uart_base = UMPMEM_BASE_UART1;
1865 edge_port->dma_address = UMPD_OEDB1_ADDRESS;
1866 break;
1867 case 1:
1868 edge_port->uart_base = UMPMEM_BASE_UART2;
1869 edge_port->dma_address = UMPD_OEDB2_ADDRESS;
1870 break;
1871 default:
1872 dev_err(&port->dev, "Unknown port number!!!\n");
1873 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 }
1875
Alan Cox2742fd82008-04-29 14:45:15 +01001876 dbg("%s - port_number = %d, uart_base = %04x, dma_address = %04x",
1877 __func__, port_number, edge_port->uart_base,
1878 edge_port->dma_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
1880 dev = port->serial->dev;
1881
Alan Cox2742fd82008-04-29 14:45:15 +01001882 memset(&(edge_port->icount), 0x00, sizeof(edge_port->icount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
1884 /* turn off loopback */
Alan Cox2742fd82008-04-29 14:45:15 +01001885 status = ti_do_config(edge_port, UMPC_SET_CLR_LOOPBACK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001887 dev_err(&port->dev,
1888 "%s - cannot send clear loopback command, %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001889 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 return status;
1891 }
Alan Cox2742fd82008-04-29 14:45:15 +01001892
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 /* set up the port settings */
Alan Cox95da3102008-07-22 11:09:07 +01001894 if (tty)
Alan Cox4a90f092008-10-13 10:39:46 +01001895 edge_set_termios(tty, port, tty->termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
1897 /* open up the port */
1898
1899 /* milliseconds to timeout for DMA transfer */
1900 transaction_timeout = 2;
1901
Alan Cox2742fd82008-04-29 14:45:15 +01001902 edge_port->ump_read_timeout =
1903 max(20, ((transaction_timeout * 3) / 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Alan Cox2742fd82008-04-29 14:45:15 +01001905 /* milliseconds to timeout for DMA transfer */
1906 open_settings = (u8)(UMP_DMA_MODE_CONTINOUS |
1907 UMP_PIPE_TRANS_TIMEOUT_ENA |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 (transaction_timeout << 2));
1909
Alan Cox2742fd82008-04-29 14:45:15 +01001910 dbg("%s - Sending UMPC_OPEN_PORT", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
1912 /* Tell TI to open and start the port */
Alan Cox2742fd82008-04-29 14:45:15 +01001913 status = send_cmd(dev, UMPC_OPEN_PORT,
1914 (u8)(UMPM_UART1_PORT + port_number), open_settings, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001916 dev_err(&port->dev, "%s - cannot send open command, %d\n",
1917 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 return status;
1919 }
1920
1921 /* Start the DMA? */
Alan Cox2742fd82008-04-29 14:45:15 +01001922 status = send_cmd(dev, UMPC_START_PORT,
1923 (u8)(UMPM_UART1_PORT + port_number), 0, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001925 dev_err(&port->dev, "%s - cannot send start DMA command, %d\n",
1926 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 return status;
1928 }
1929
1930 /* Clear TX and RX buffers in UMP */
Alan Cox2742fd82008-04-29 14:45:15 +01001931 status = purge_port(port, UMP_PORT_DIR_OUT | UMP_PORT_DIR_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001933 dev_err(&port->dev,
1934 "%s - cannot send clear buffers command, %d\n",
1935 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 return status;
1937 }
1938
1939 /* Read Initial MSR */
Alan Cox2742fd82008-04-29 14:45:15 +01001940 status = ti_vread_sync(dev, UMPC_READ_MSR, 0,
1941 (__u16)(UMPM_UART1_PORT + port_number),
1942 &edge_port->shadow_msr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001944 dev_err(&port->dev, "%s - cannot send read MSR command, %d\n",
1945 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 return status;
1947 }
1948
Alan Cox2742fd82008-04-29 14:45:15 +01001949 dbg("ShadowMSR 0x%X", edge_port->shadow_msr);
1950
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 /* Set Initial MCR */
1952 edge_port->shadow_mcr = MCR_RTS | MCR_DTR;
Alan Cox2742fd82008-04-29 14:45:15 +01001953 dbg("ShadowMCR 0x%X", edge_port->shadow_mcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
1955 edge_serial = edge_port->edge_serial;
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01001956 if (mutex_lock_interruptible(&edge_serial->es_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 return -ERESTARTSYS;
1958 if (edge_serial->num_ports_open == 0) {
Alan Cox2742fd82008-04-29 14:45:15 +01001959 /* we are the first port to open, post the interrupt urb */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 urb = edge_serial->serial->port[0]->interrupt_in_urb;
1961 if (!urb) {
Alan Cox2742fd82008-04-29 14:45:15 +01001962 dev_err(&port->dev,
1963 "%s - no interrupt urb present, exiting\n",
1964 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 status = -EINVAL;
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01001966 goto release_es_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 urb->context = edge_serial;
Alan Cox2742fd82008-04-29 14:45:15 +01001969 status = usb_submit_urb(urb, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001971 dev_err(&port->dev,
1972 "%s - usb_submit_urb failed with value %d\n",
1973 __func__, status);
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01001974 goto release_es_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 }
1976 }
1977
1978 /*
1979 * reset the data toggle on the bulk endpoints to work around bug in
1980 * host controllers where things get out of sync some times
1981 */
Alan Cox2742fd82008-04-29 14:45:15 +01001982 usb_clear_halt(dev, port->write_urb->pipe);
1983 usb_clear_halt(dev, port->read_urb->pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
1985 /* start up our bulk read urb */
1986 urb = port->read_urb;
1987 if (!urb) {
Alan Cox2742fd82008-04-29 14:45:15 +01001988 dev_err(&port->dev, "%s - no read urb present, exiting\n",
1989 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 status = -EINVAL;
1991 goto unlink_int_urb;
1992 }
1993 edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 urb->context = edge_port;
Alan Cox2742fd82008-04-29 14:45:15 +01001995 status = usb_submit_urb(urb, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001997 dev_err(&port->dev,
1998 "%s - read bulk usb_submit_urb failed with value %d\n",
1999 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 goto unlink_int_urb;
2001 }
2002
2003 ++edge_serial->num_ports_open;
2004
Harvey Harrison441b62c2008-03-03 16:08:34 -08002005 dbg("%s - exited", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01002007 goto release_es_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
2009unlink_int_urb:
2010 if (edge_port->edge_serial->num_ports_open == 0)
2011 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01002012release_es_lock:
2013 mutex_unlock(&edge_serial->es_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 return status;
2015}
2016
Alan Cox335f8512009-06-11 12:26:29 +01002017static void edge_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018{
2019 struct edgeport_serial *edge_serial;
2020 struct edgeport_port *edge_port;
2021 int port_number;
2022 int status;
2023
Harvey Harrison441b62c2008-03-03 16:08:34 -08002024 dbg("%s - port %d", __func__, port->number);
Alan Cox2742fd82008-04-29 14:45:15 +01002025
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 edge_serial = usb_get_serial_data(port->serial);
2027 edge_port = usb_get_serial_port_data(port);
Alan Cox2742fd82008-04-29 14:45:15 +01002028 if (edge_serial == NULL || edge_port == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 return;
Alan Cox2742fd82008-04-29 14:45:15 +01002030
2031 /* The bulkreadcompletion routine will check
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 * this flag and dump add read data */
2033 edge_port->close_pending = 1;
2034
2035 /* chase the port close and flush */
Alan Cox2742fd82008-04-29 14:45:15 +01002036 chase_port(edge_port, (HZ * closing_wait) / 100, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
2038 usb_kill_urb(port->read_urb);
2039 usb_kill_urb(port->write_urb);
2040 edge_port->ep_write_urb_in_use = 0;
2041
2042 /* assuming we can still talk to the device,
2043 * send a close port command to it */
Harvey Harrison441b62c2008-03-03 16:08:34 -08002044 dbg("%s - send umpc_close_port", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 port_number = port->number - port->serial->minor;
Alan Cox2742fd82008-04-29 14:45:15 +01002046 status = send_cmd(port->serial->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 UMPC_CLOSE_PORT,
2048 (__u8)(UMPM_UART1_PORT + port_number),
2049 0,
2050 NULL,
2051 0);
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01002052 mutex_lock(&edge_serial->es_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 --edge_port->edge_serial->num_ports_open;
2054 if (edge_port->edge_serial->num_ports_open <= 0) {
2055 /* last port is now closed, let's shut down our interrupt urb */
2056 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
2057 edge_port->edge_serial->num_ports_open = 0;
2058 }
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01002059 mutex_unlock(&edge_serial->es_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 edge_port->close_pending = 0;
2061
Harvey Harrison441b62c2008-03-03 16:08:34 -08002062 dbg("%s - exited", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063}
2064
Alan Cox95da3102008-07-22 11:09:07 +01002065static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
2066 const unsigned char *data, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067{
2068 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
Harvey Harrison441b62c2008-03-03 16:08:34 -08002070 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
2072 if (count == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002073 dbg("%s - write request of 0 bytes", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 return 0;
2075 }
2076
2077 if (edge_port == NULL)
2078 return -ENODEV;
2079 if (edge_port->close_pending == 1)
2080 return -ENODEV;
2081
Johan Hovoldd733cec2010-05-19 00:01:37 +02002082 count = kfifo_in_locked(&edge_port->write_fifo, data, count,
2083 &edge_port->ep_lock);
Alan Cox95da3102008-07-22 11:09:07 +01002084 edge_send(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
2086 return count;
2087}
2088
Alan Cox95da3102008-07-22 11:09:07 +01002089static void edge_send(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090{
Alan Cox95da3102008-07-22 11:09:07 +01002091 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 int count, result;
2093 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 unsigned long flags;
2095
2096
Harvey Harrison441b62c2008-03-03 16:08:34 -08002097 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
2099 spin_lock_irqsave(&edge_port->ep_lock, flags);
2100
2101 if (edge_port->ep_write_urb_in_use) {
2102 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2103 return;
2104 }
2105
Johan Hovoldd733cec2010-05-19 00:01:37 +02002106 count = kfifo_out(&edge_port->write_fifo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 port->write_urb->transfer_buffer,
2108 port->bulk_out_size);
2109
2110 if (count == 0) {
2111 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2112 return;
2113 }
2114
2115 edge_port->ep_write_urb_in_use = 1;
2116
2117 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2118
Alan Cox2742fd82008-04-29 14:45:15 +01002119 usb_serial_debug_data(debug, &port->dev, __func__, count,
2120 port->write_urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
2122 /* set up our urb */
Johan Hovoldfd119612011-11-06 19:06:30 +01002123 port->write_urb->transfer_buffer_length = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
2125 /* send the data out the bulk port */
2126 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
2127 if (result) {
Johan Hovold22a416c2012-02-10 13:20:51 +01002128 dev_err_console(port,
Alan Cox2742fd82008-04-29 14:45:15 +01002129 "%s - failed submitting write urb, error %d\n",
2130 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 edge_port->ep_write_urb_in_use = 0;
Alan Cox2742fd82008-04-29 14:45:15 +01002132 /* TODO: reschedule edge_send */
2133 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 edge_port->icount.tx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
2136 /* wakeup any process waiting for writes to complete */
2137 /* there is now more room in the buffer for new writes */
Alan Cox2742fd82008-04-29 14:45:15 +01002138 if (tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140}
2141
Alan Cox95da3102008-07-22 11:09:07 +01002142static int edge_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143{
Alan Cox95da3102008-07-22 11:09:07 +01002144 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2146 int room = 0;
2147 unsigned long flags;
2148
Harvey Harrison441b62c2008-03-03 16:08:34 -08002149 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
2151 if (edge_port == NULL)
Alan Cox2742fd82008-04-29 14:45:15 +01002152 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 if (edge_port->close_pending == 1)
Alan Cox2742fd82008-04-29 14:45:15 +01002154 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
2156 spin_lock_irqsave(&edge_port->ep_lock, flags);
Johan Hovoldd733cec2010-05-19 00:01:37 +02002157 room = kfifo_avail(&edge_port->write_fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2159
Harvey Harrison441b62c2008-03-03 16:08:34 -08002160 dbg("%s - returns %d", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 return room;
2162}
2163
Alan Cox95da3102008-07-22 11:09:07 +01002164static int edge_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165{
Alan Cox95da3102008-07-22 11:09:07 +01002166 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2168 int chars = 0;
2169 unsigned long flags;
2170
Harvey Harrison441b62c2008-03-03 16:08:34 -08002171 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
2173 if (edge_port == NULL)
Alan Cox2742fd82008-04-29 14:45:15 +01002174 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 if (edge_port->close_pending == 1)
Alan Cox2742fd82008-04-29 14:45:15 +01002176 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
2178 spin_lock_irqsave(&edge_port->ep_lock, flags);
Johan Hovoldd733cec2010-05-19 00:01:37 +02002179 chars = kfifo_len(&edge_port->write_fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2181
Alan Cox2742fd82008-04-29 14:45:15 +01002182 dbg("%s - returns %d", __func__, chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 return chars;
2184}
2185
Alan Cox95da3102008-07-22 11:09:07 +01002186static void edge_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187{
Alan Cox95da3102008-07-22 11:09:07 +01002188 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 int status;
2191
Harvey Harrison441b62c2008-03-03 16:08:34 -08002192 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
2194 if (edge_port == NULL)
2195 return;
2196
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 /* if we are implementing XON/XOFF, send the stop character */
2198 if (I_IXOFF(tty)) {
2199 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01002200 status = edge_write(tty, port, &stop_char, 1);
2201 if (status <= 0) {
2202 dev_err(&port->dev, "%s - failed to write stop character, %d\n", __func__, status);
2203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 }
2205
2206 /* if we are implementing RTS/CTS, stop reads */
2207 /* and the Edgeport will clear the RTS line */
2208 if (C_CRTSCTS(tty))
2209 stop_read(edge_port);
2210
2211}
2212
Alan Cox95da3102008-07-22 11:09:07 +01002213static void edge_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214{
Alan Cox95da3102008-07-22 11:09:07 +01002215 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 int status;
2218
Harvey Harrison441b62c2008-03-03 16:08:34 -08002219 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
2221 if (edge_port == NULL)
2222 return;
2223
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 /* if we are implementing XON/XOFF, send the start character */
2225 if (I_IXOFF(tty)) {
2226 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01002227 status = edge_write(tty, port, &start_char, 1);
2228 if (status <= 0) {
2229 dev_err(&port->dev, "%s - failed to write start character, %d\n", __func__, status);
2230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 /* if we are implementing RTS/CTS, restart reads */
2233 /* are the Edgeport will assert the RTS line */
2234 if (C_CRTSCTS(tty)) {
2235 status = restart_read(edge_port);
2236 if (status)
Alan Cox2742fd82008-04-29 14:45:15 +01002237 dev_err(&port->dev,
2238 "%s - read bulk usb_submit_urb failed: %d\n",
2239 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 }
2241
2242}
2243
2244static void stop_read(struct edgeport_port *edge_port)
2245{
2246 unsigned long flags;
2247
2248 spin_lock_irqsave(&edge_port->ep_lock, flags);
2249
2250 if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
2251 edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPING;
2252 edge_port->shadow_mcr &= ~MCR_RTS;
2253
2254 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2255}
2256
2257static int restart_read(struct edgeport_port *edge_port)
2258{
2259 struct urb *urb;
2260 int status = 0;
2261 unsigned long flags;
2262
2263 spin_lock_irqsave(&edge_port->ep_lock, flags);
2264
2265 if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPED) {
2266 urb = edge_port->port->read_urb;
Oliver Neukumefdff602007-06-05 10:50:48 +02002267 status = usb_submit_urb(urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 }
2269 edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
2270 edge_port->shadow_mcr |= MCR_RTS;
2271
2272 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2273
2274 return status;
2275}
2276
Alan Cox95da3102008-07-22 11:09:07 +01002277static void change_port_settings(struct tty_struct *tty,
2278 struct edgeport_port *edge_port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279{
2280 struct ump_uart_config *config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 int baud;
2282 unsigned cflag;
2283 int status;
Alan Cox2742fd82008-04-29 14:45:15 +01002284 int port_number = edge_port->port->number -
2285 edge_port->port->serial->minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
Harvey Harrison441b62c2008-03-03 16:08:34 -08002287 dbg("%s - port %d", __func__, edge_port->port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
Alan Cox95da3102008-07-22 11:09:07 +01002289 config = kmalloc (sizeof (*config), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 if (!config) {
Alan Cox2742fd82008-04-29 14:45:15 +01002291 *tty->termios = *old_termios;
2292 dev_err(&edge_port->port->dev, "%s - out of memory\n",
2293 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 return;
2295 }
2296
2297 cflag = tty->termios->c_cflag;
2298
2299 config->wFlags = 0;
2300
2301 /* These flags must be set */
2302 config->wFlags |= UMP_MASK_UART_FLAGS_RECEIVE_MS_INT;
2303 config->wFlags |= UMP_MASK_UART_FLAGS_AUTO_START_ON_ERR;
2304 config->bUartMode = (__u8)(edge_port->bUartMode);
2305
2306 switch (cflag & CSIZE) {
Alan Cox2742fd82008-04-29 14:45:15 +01002307 case CS5:
2308 config->bDataBits = UMP_UART_CHAR5BITS;
2309 dbg("%s - data bits = 5", __func__);
2310 break;
2311 case CS6:
2312 config->bDataBits = UMP_UART_CHAR6BITS;
2313 dbg("%s - data bits = 6", __func__);
2314 break;
2315 case CS7:
2316 config->bDataBits = UMP_UART_CHAR7BITS;
2317 dbg("%s - data bits = 7", __func__);
2318 break;
2319 default:
2320 case CS8:
2321 config->bDataBits = UMP_UART_CHAR8BITS;
2322 dbg("%s - data bits = 8", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 break;
2324 }
2325
2326 if (cflag & PARENB) {
2327 if (cflag & PARODD) {
2328 config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
2329 config->bParity = UMP_UART_ODDPARITY;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002330 dbg("%s - parity = odd", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 } else {
2332 config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
2333 config->bParity = UMP_UART_EVENPARITY;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002334 dbg("%s - parity = even", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 }
2336 } else {
Alan Cox2742fd82008-04-29 14:45:15 +01002337 config->bParity = UMP_UART_NOPARITY;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002338 dbg("%s - parity = none", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 }
2340
2341 if (cflag & CSTOPB) {
2342 config->bStopBits = UMP_UART_STOPBIT2;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002343 dbg("%s - stop bits = 2", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 } else {
2345 config->bStopBits = UMP_UART_STOPBIT1;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002346 dbg("%s - stop bits = 1", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 }
2348
2349 /* figure out the flow control settings */
2350 if (cflag & CRTSCTS) {
2351 config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X_CTS_FLOW;
2352 config->wFlags |= UMP_MASK_UART_FLAGS_RTS_FLOW;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002353 dbg("%s - RTS/CTS is enabled", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002355 dbg("%s - RTS/CTS is disabled", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 tty->hw_stopped = 0;
2357 restart_read(edge_port);
2358 }
2359
Alan Cox2742fd82008-04-29 14:45:15 +01002360 /* if we are implementing XON/XOFF, set the start and stop
2361 character in the device */
2362 config->cXon = START_CHAR(tty);
2363 config->cXoff = STOP_CHAR(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
Alan Cox2742fd82008-04-29 14:45:15 +01002365 /* if we are implementing INBOUND XON/XOFF */
2366 if (I_IXOFF(tty)) {
2367 config->wFlags |= UMP_MASK_UART_FLAGS_IN_X;
2368 dbg("%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2369 __func__, config->cXon, config->cXoff);
2370 } else
2371 dbg("%s - INBOUND XON/XOFF is disabled", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
Alan Cox2742fd82008-04-29 14:45:15 +01002373 /* if we are implementing OUTBOUND XON/XOFF */
2374 if (I_IXON(tty)) {
2375 config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X;
2376 dbg("%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2377 __func__, config->cXon, config->cXoff);
2378 } else
2379 dbg("%s - OUTBOUND XON/XOFF is disabled", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
Alan Coxd5f5bcd2008-01-03 16:57:33 +00002381 tty->termios->c_cflag &= ~CMSPAR;
2382
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 /* Round the baud rate */
2384 baud = tty_get_baud_rate(tty);
2385 if (!baud) {
2386 /* pick a default, any default... */
2387 baud = 9600;
Alan Coxd5f5bcd2008-01-03 16:57:33 +00002388 } else
2389 tty_encode_baud_rate(tty, baud, baud);
2390
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 edge_port->baud_rate = baud;
2392 config->wBaudRate = (__u16)((461550L + baud/2) / baud);
2393
Alan Coxd5f5bcd2008-01-03 16:57:33 +00002394 /* FIXME: Recompute actual baud from divisor here */
2395
Alan Cox2742fd82008-04-29 14:45:15 +01002396 dbg("%s - baud rate = %d, wBaudRate = %d", __func__, baud,
2397 config->wBaudRate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
Alan Cox2742fd82008-04-29 14:45:15 +01002399 dbg("wBaudRate: %d", (int)(461550L / config->wBaudRate));
2400 dbg("wFlags: 0x%x", config->wFlags);
2401 dbg("bDataBits: %d", config->bDataBits);
2402 dbg("bParity: %d", config->bParity);
2403 dbg("bStopBits: %d", config->bStopBits);
2404 dbg("cXon: %d", config->cXon);
2405 dbg("cXoff: %d", config->cXoff);
2406 dbg("bUartMode: %d", config->bUartMode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
2408 /* move the word values into big endian mode */
Alan Cox2742fd82008-04-29 14:45:15 +01002409 cpu_to_be16s(&config->wFlags);
2410 cpu_to_be16s(&config->wBaudRate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
Alan Cox2742fd82008-04-29 14:45:15 +01002412 status = send_cmd(edge_port->port->serial->dev, UMPC_SET_CONFIG,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 (__u8)(UMPM_UART1_PORT + port_number),
Alan Cox2742fd82008-04-29 14:45:15 +01002414 0, (__u8 *)config, sizeof(*config));
2415 if (status)
2416 dbg("%s - error %d when trying to write config to device",
Harvey Harrison441b62c2008-03-03 16:08:34 -08002417 __func__, status);
Alan Cox2742fd82008-04-29 14:45:15 +01002418 kfree(config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419}
2420
Alan Cox2e0ddd62008-07-22 11:12:33 +01002421static void edge_set_termios(struct tty_struct *tty,
Alan Cox95da3102008-07-22 11:09:07 +01002422 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423{
2424 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Alan Cox95da3102008-07-22 11:09:07 +01002425 unsigned int cflag;
2426
2427 cflag = tty->termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428
Harvey Harrison441b62c2008-03-03 16:08:34 -08002429 dbg("%s - clfag %08x iflag %08x", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 tty->termios->c_cflag, tty->termios->c_iflag);
Harvey Harrison441b62c2008-03-03 16:08:34 -08002431 dbg("%s - old clfag %08x old iflag %08x", __func__,
Alan Coxd5f5bcd2008-01-03 16:57:33 +00002432 old_termios->c_cflag, old_termios->c_iflag);
Harvey Harrison441b62c2008-03-03 16:08:34 -08002433 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
2435 if (edge_port == NULL)
2436 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 /* change the port settings to the new ones specified */
Alan Cox95da3102008-07-22 11:09:07 +01002438 change_port_settings(tty, edge_port, old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439}
2440
Alan Cox20b9d172011-02-14 16:26:50 +00002441static int edge_tiocmset(struct tty_struct *tty,
Alan Cox2742fd82008-04-29 14:45:15 +01002442 unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443{
Alan Cox95da3102008-07-22 11:09:07 +01002444 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2446 unsigned int mcr;
Alan Cox3d71fe02008-02-20 21:38:32 +00002447 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
Harvey Harrison441b62c2008-03-03 16:08:34 -08002449 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
Alan Cox3d71fe02008-02-20 21:38:32 +00002451 spin_lock_irqsave(&edge_port->ep_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 mcr = edge_port->shadow_mcr;
2453 if (set & TIOCM_RTS)
2454 mcr |= MCR_RTS;
2455 if (set & TIOCM_DTR)
2456 mcr |= MCR_DTR;
2457 if (set & TIOCM_LOOP)
2458 mcr |= MCR_LOOPBACK;
2459
2460 if (clear & TIOCM_RTS)
2461 mcr &= ~MCR_RTS;
2462 if (clear & TIOCM_DTR)
2463 mcr &= ~MCR_DTR;
2464 if (clear & TIOCM_LOOP)
2465 mcr &= ~MCR_LOOPBACK;
2466
2467 edge_port->shadow_mcr = mcr;
Alan Cox3d71fe02008-02-20 21:38:32 +00002468 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469
Alan Cox2742fd82008-04-29 14:45:15 +01002470 restore_mcr(edge_port, mcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 return 0;
2472}
2473
Alan Cox60b33c12011-02-14 16:26:14 +00002474static int edge_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475{
Alan Cox95da3102008-07-22 11:09:07 +01002476 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2478 unsigned int result = 0;
2479 unsigned int msr;
2480 unsigned int mcr;
Alan Cox3d71fe02008-02-20 21:38:32 +00002481 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
Harvey Harrison441b62c2008-03-03 16:08:34 -08002483 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
Alan Cox3d71fe02008-02-20 21:38:32 +00002485 spin_lock_irqsave(&edge_port->ep_lock, flags);
2486
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 msr = edge_port->shadow_msr;
2488 mcr = edge_port->shadow_mcr;
2489 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
2490 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
2491 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
2492 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
2493 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
2494 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
2495
2496
Harvey Harrison441b62c2008-03-03 16:08:34 -08002497 dbg("%s -- %x", __func__, result);
Alan Cox3d71fe02008-02-20 21:38:32 +00002498 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
2500 return result;
2501}
2502
Alan Cox0bca1b92010-09-16 18:21:40 +01002503static int edge_get_icount(struct tty_struct *tty,
2504 struct serial_icounter_struct *icount)
2505{
2506 struct usb_serial_port *port = tty->driver_data;
2507 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2508 struct async_icount *ic = &edge_port->icount;
2509
2510 icount->cts = ic->cts;
2511 icount->dsr = ic->dsr;
2512 icount->rng = ic->rng;
2513 icount->dcd = ic->dcd;
2514 icount->tx = ic->tx;
2515 icount->rx = ic->rx;
2516 icount->frame = ic->frame;
2517 icount->parity = ic->parity;
2518 icount->overrun = ic->overrun;
2519 icount->brk = ic->brk;
2520 icount->buf_overrun = ic->buf_overrun;
2521 return 0;
2522}
2523
Alan Cox2742fd82008-04-29 14:45:15 +01002524static int get_serial_info(struct edgeport_port *edge_port,
2525 struct serial_struct __user *retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526{
2527 struct serial_struct tmp;
2528
2529 if (!retinfo)
2530 return -EFAULT;
2531
2532 memset(&tmp, 0, sizeof(tmp));
2533
2534 tmp.type = PORT_16550A;
2535 tmp.line = edge_port->port->serial->minor;
2536 tmp.port = edge_port->port->number;
2537 tmp.irq = 0;
2538 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2539 tmp.xmit_fifo_size = edge_port->port->bulk_out_size;
2540 tmp.baud_base = 9600;
2541 tmp.close_delay = 5*HZ;
2542 tmp.closing_wait = closing_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
2544 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2545 return -EFAULT;
2546 return 0;
2547}
2548
Alan Cox00a0d0d2011-02-14 16:27:06 +00002549static int edge_ioctl(struct tty_struct *tty,
Alan Cox2742fd82008-04-29 14:45:15 +01002550 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551{
Alan Cox95da3102008-07-22 11:09:07 +01002552 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2554 struct async_icount cnow;
2555 struct async_icount cprev;
2556
Harvey Harrison441b62c2008-03-03 16:08:34 -08002557 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
2559 switch (cmd) {
Alan Cox2742fd82008-04-29 14:45:15 +01002560 case TIOCGSERIAL:
2561 dbg("%s - (%d) TIOCGSERIAL", __func__, port->number);
2562 return get_serial_info(edge_port,
2563 (struct serial_struct __user *) arg);
2564 case TIOCMIWAIT:
2565 dbg("%s - (%d) TIOCMIWAIT", __func__, port->number);
2566 cprev = edge_port->icount;
2567 while (1) {
Johan Hovoldfad70e42013-03-19 09:21:17 +01002568 interruptible_sleep_on(&port->delta_msr_wait);
Alan Cox2742fd82008-04-29 14:45:15 +01002569 /* see if a signal did it */
2570 if (signal_pending(current))
2571 return -ERESTARTSYS;
Johan Hovoldfad70e42013-03-19 09:21:17 +01002572
2573 if (port->serial->disconnected)
2574 return -EIO;
2575
Alan Cox2742fd82008-04-29 14:45:15 +01002576 cnow = edge_port->icount;
2577 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2578 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2579 return -EIO; /* no change => error */
2580 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2581 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2582 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2583 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2584 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 }
Alan Cox2742fd82008-04-29 14:45:15 +01002586 cprev = cnow;
2587 }
2588 /* not reached */
2589 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 return -ENOIOCTLCMD;
2592}
2593
Alan Cox95da3102008-07-22 11:09:07 +01002594static void edge_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595{
Alan Cox95da3102008-07-22 11:09:07 +01002596 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2598 int status;
Alan Cox2742fd82008-04-29 14:45:15 +01002599 int bv = 0; /* Off */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600
Alan Cox95da3102008-07-22 11:09:07 +01002601 dbg("%s - state = %d", __func__, break_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602
2603 /* chase the port close */
Alan Cox2742fd82008-04-29 14:45:15 +01002604 chase_port(edge_port, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
Alan Cox95da3102008-07-22 11:09:07 +01002606 if (break_state == -1)
Alan Cox2742fd82008-04-29 14:45:15 +01002607 bv = 1; /* On */
2608 status = ti_do_config(edge_port, UMPC_SET_CLR_BREAK, bv);
2609 if (status)
2610 dbg("%s - error %d sending break set/clear command.",
Harvey Harrison441b62c2008-03-03 16:08:34 -08002611 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612}
2613
Alan Cox2742fd82008-04-29 14:45:15 +01002614static int edge_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615{
2616 struct edgeport_serial *edge_serial;
2617 struct edgeport_port *edge_port;
2618 struct usb_device *dev;
2619 int status;
2620 int i;
2621
2622 dev = serial->dev;
2623
2624 /* create our private serial structure */
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +01002625 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 if (edge_serial == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002627 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 return -ENOMEM;
2629 }
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01002630 mutex_init(&edge_serial->es_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 edge_serial->serial = serial;
2632 usb_set_serial_data(serial, edge_serial);
2633
Alan Cox2742fd82008-04-29 14:45:15 +01002634 status = download_fw(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01002636 kfree(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 return status;
2638 }
2639
2640 /* set up our port private structures */
2641 for (i = 0; i < serial->num_ports; ++i) {
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +01002642 edge_port = kzalloc(sizeof(struct edgeport_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 if (edge_port == NULL) {
Alan Cox2742fd82008-04-29 14:45:15 +01002644 dev_err(&serial->dev->dev, "%s - Out of memory\n",
2645 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 goto cleanup;
2647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 spin_lock_init(&edge_port->ep_lock);
Johan Hovoldd733cec2010-05-19 00:01:37 +02002649 if (kfifo_alloc(&edge_port->write_fifo, EDGE_OUT_BUF_SIZE,
2650 GFP_KERNEL)) {
Alan Cox2742fd82008-04-29 14:45:15 +01002651 dev_err(&serial->dev->dev, "%s - Out of memory\n",
2652 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 kfree(edge_port);
2654 goto cleanup;
2655 }
2656 edge_port->port = serial->port[i];
2657 edge_port->edge_serial = edge_serial;
2658 usb_set_serial_port_data(serial->port[i], edge_port);
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002659 edge_port->bUartMode = default_uart_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 }
Alan Cox2742fd82008-04-29 14:45:15 +01002661
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 return 0;
2663
2664cleanup:
Alan Cox2742fd82008-04-29 14:45:15 +01002665 for (--i; i >= 0; --i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 edge_port = usb_get_serial_port_data(serial->port[i]);
Johan Hovoldd733cec2010-05-19 00:01:37 +02002667 kfifo_free(&edge_port->write_fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 kfree(edge_port);
2669 usb_set_serial_port_data(serial->port[i], NULL);
2670 }
Alan Cox2742fd82008-04-29 14:45:15 +01002671 kfree(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 usb_set_serial_data(serial, NULL);
2673 return -ENOMEM;
2674}
2675
Alan Sternf9c99bb2009-06-02 11:53:55 -04002676static void edge_disconnect(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677{
Alan Cox2742fd82008-04-29 14:45:15 +01002678 dbg("%s", __func__);
Alan Sternf9c99bb2009-06-02 11:53:55 -04002679}
2680
2681static void edge_release(struct usb_serial *serial)
2682{
2683 int i;
2684 struct edgeport_port *edge_port;
2685
2686 dbg("%s", __func__);
2687
2688 for (i = 0; i < serial->num_ports; ++i) {
2689 edge_port = usb_get_serial_port_data(serial->port[i]);
Johan Hovoldd733cec2010-05-19 00:01:37 +02002690 kfifo_free(&edge_port->write_fifo);
Jesper Juhl0d46c002007-07-16 22:17:25 +02002691 kfree(edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 }
Jesper Juhl0d46c002007-07-16 22:17:25 +02002693 kfree(usb_get_serial_data(serial));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694}
2695
2696
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002697/* Sysfs Attributes */
2698
2699static ssize_t show_uart_mode(struct device *dev,
2700 struct device_attribute *attr, char *buf)
2701{
2702 struct usb_serial_port *port = to_usb_serial_port(dev);
2703 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2704
2705 return sprintf(buf, "%d\n", edge_port->bUartMode);
2706}
2707
2708static ssize_t store_uart_mode(struct device *dev,
2709 struct device_attribute *attr, const char *valbuf, size_t count)
2710{
2711 struct usb_serial_port *port = to_usb_serial_port(dev);
2712 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2713 unsigned int v = simple_strtoul(valbuf, NULL, 0);
2714
Harvey Harrison441b62c2008-03-03 16:08:34 -08002715 dbg("%s: setting uart_mode = %d", __func__, v);
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002716
2717 if (v < 256)
2718 edge_port->bUartMode = v;
2719 else
Harvey Harrison441b62c2008-03-03 16:08:34 -08002720 dev_err(dev, "%s - uart_mode %d is invalid\n", __func__, v);
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002721
2722 return count;
2723}
2724
Alan Cox2742fd82008-04-29 14:45:15 +01002725static DEVICE_ATTR(uart_mode, S_IWUSR | S_IRUGO, show_uart_mode,
2726 store_uart_mode);
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002727
2728static int edge_create_sysfs_attrs(struct usb_serial_port *port)
2729{
2730 return device_create_file(&port->dev, &dev_attr_uart_mode);
2731}
2732
2733static int edge_remove_sysfs_attrs(struct usb_serial_port *port)
2734{
2735 device_remove_file(&port->dev, &dev_attr_uart_mode);
2736 return 0;
2737}
2738
2739
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -07002740static struct usb_serial_driver edgeport_1port_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07002741 .driver = {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07002742 .owner = THIS_MODULE,
2743 .name = "edgeport_ti_1",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07002744 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07002745 .description = "Edgeport TI 1 port adapter",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 .id_table = edgeport_1port_id_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 .num_ports = 1,
2748 .open = edge_open,
2749 .close = edge_close,
2750 .throttle = edge_throttle,
2751 .unthrottle = edge_unthrottle,
2752 .attach = edge_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04002753 .disconnect = edge_disconnect,
2754 .release = edge_release,
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002755 .port_probe = edge_create_sysfs_attrs,
Eric W. Biederman6d443d82012-01-13 21:32:06 -08002756 .port_remove = edge_remove_sysfs_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 .ioctl = edge_ioctl,
2758 .set_termios = edge_set_termios,
2759 .tiocmget = edge_tiocmget,
2760 .tiocmset = edge_tiocmset,
Alan Cox0bca1b92010-09-16 18:21:40 +01002761 .get_icount = edge_get_icount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 .write = edge_write,
2763 .write_room = edge_write_room,
2764 .chars_in_buffer = edge_chars_in_buffer,
2765 .break_ctl = edge_break,
2766 .read_int_callback = edge_interrupt_callback,
2767 .read_bulk_callback = edge_bulk_in_callback,
2768 .write_bulk_callback = edge_bulk_out_callback,
2769};
2770
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -07002771static struct usb_serial_driver edgeport_2port_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07002772 .driver = {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07002773 .owner = THIS_MODULE,
2774 .name = "edgeport_ti_2",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07002775 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07002776 .description = "Edgeport TI 2 port adapter",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 .id_table = edgeport_2port_id_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 .num_ports = 2,
2779 .open = edge_open,
2780 .close = edge_close,
2781 .throttle = edge_throttle,
2782 .unthrottle = edge_unthrottle,
2783 .attach = edge_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04002784 .disconnect = edge_disconnect,
2785 .release = edge_release,
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002786 .port_probe = edge_create_sysfs_attrs,
Eric W. Biederman6d443d82012-01-13 21:32:06 -08002787 .port_remove = edge_remove_sysfs_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 .ioctl = edge_ioctl,
2789 .set_termios = edge_set_termios,
2790 .tiocmget = edge_tiocmget,
2791 .tiocmset = edge_tiocmset,
Johan Hovold8b55bf52013-03-19 09:21:08 +01002792 .get_icount = edge_get_icount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 .write = edge_write,
2794 .write_room = edge_write_room,
2795 .chars_in_buffer = edge_chars_in_buffer,
2796 .break_ctl = edge_break,
2797 .read_int_callback = edge_interrupt_callback,
2798 .read_bulk_callback = edge_bulk_in_callback,
2799 .write_bulk_callback = edge_bulk_out_callback,
2800};
2801
Alan Stern7dbe2462012-02-23 14:56:57 -05002802static struct usb_serial_driver * const serial_drivers[] = {
2803 &edgeport_1port_device, &edgeport_2port_device, NULL
2804};
2805
Greg Kroah-Hartman8485f8c2012-02-28 13:11:53 -08002806module_usb_serial_driver(io_driver, serial_drivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808MODULE_AUTHOR(DRIVER_AUTHOR);
2809MODULE_DESCRIPTION(DRIVER_DESC);
2810MODULE_LICENSE("GPL");
Jaswinder Singhd12b2192008-07-04 23:06:09 +05302811MODULE_FIRMWARE("edgeport/down3.bin");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812
2813module_param(debug, bool, S_IRUGO | S_IWUSR);
2814MODULE_PARM_DESC(debug, "Debug enabled or not");
2815
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816module_param(closing_wait, int, S_IRUGO | S_IWUSR);
2817MODULE_PARM_DESC(closing_wait, "Maximum wait for data to drain, in .01 secs");
2818
2819module_param(ignore_cpu_rev, bool, S_IRUGO | S_IWUSR);
Alan Cox2742fd82008-04-29 14:45:15 +01002820MODULE_PARM_DESC(ignore_cpu_rev,
2821 "Ignore the cpu revision when connecting to a device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002823module_param(default_uart_mode, int, S_IRUGO | S_IWUSR);
2824MODULE_PARM_DESC(default_uart_mode, "Default uart_mode, 0=RS232, ...");