blob: c6b82833a15700f876a8889e7825213dff89ade9 [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 Hovoldd733cec2010-05-19 00:01:37 +020032#include <linux/kfifo.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/ioctl.h>
Jaswinder Singhd12b2192008-07-04 23:06:09 +053034#include <linux/firmware.h>
Alan Cox2742fd82008-04-29 14:45:15 +010035#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070037#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "io_16654.h"
40#include "io_usbvend.h"
41#include "io_ti.h"
42
43/*
44 * Version Information
45 */
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -040046#define DRIVER_VERSION "v0.7mode043006"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
48#define DRIVER_DESC "Edgeport USB Serial Driver"
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define EPROM_PAGE_SIZE 64
51
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/* different hardware types */
54#define HARDWARE_TYPE_930 0
55#define HARDWARE_TYPE_TIUMP 1
56
Alan Cox2742fd82008-04-29 14:45:15 +010057/* IOCTL_PRIVATE_TI_GET_MODE Definitions */
58#define TI_MODE_CONFIGURING 0 /* Device has not entered start device */
59#define TI_MODE_BOOT 1 /* Staying in boot mode */
60#define TI_MODE_DOWNLOAD 2 /* Made it to download mode */
61#define TI_MODE_TRANSITIONING 3 /* Currently in boot mode but
62 transitioning to download mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64/* read urb state */
65#define EDGE_READ_URB_RUNNING 0
66#define EDGE_READ_URB_STOPPING 1
67#define EDGE_READ_URB_STOPPED 2
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#define EDGE_CLOSING_WAIT 4000 /* in .01 sec */
70
71#define EDGE_OUT_BUF_SIZE 1024
72
73
74/* Product information read from the Edgeport */
Alan Cox2742fd82008-04-29 14:45:15 +010075struct product_info {
76 int TiMode; /* Current TI Mode */
77 __u8 hardware_type; /* Type of hardware */
Linus Torvalds1da177e2005-04-16 15:20:36 -070078} __attribute__((packed));
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080struct edgeport_port {
81 __u16 uart_base;
82 __u16 dma_address;
83 __u8 shadow_msr;
84 __u8 shadow_mcr;
85 __u8 shadow_lsr;
86 __u8 lsr_mask;
Martin Olsson19af5cd2009-04-23 11:37:37 +020087 __u32 ump_read_timeout; /* Number of milliseconds the UMP will
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 wait without data before completing
89 a read short */
90 int baud_rate;
91 int close_pending;
92 int lsr_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 struct async_icount icount;
94 wait_queue_head_t delta_msr_wait; /* for handling sleeping while
95 waiting for msr change to
96 happen */
97 struct edgeport_serial *edge_serial;
98 struct usb_serial_port *port;
Alan Cox2742fd82008-04-29 14:45:15 +010099 __u8 bUartMode; /* Port type, 0: RS232, etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 spinlock_t ep_lock;
101 int ep_read_urb_state;
102 int ep_write_urb_in_use;
Johan Hovoldd733cec2010-05-19 00:01:37 +0200103 struct kfifo write_fifo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104};
105
106struct edgeport_serial {
107 struct product_info product_info;
Alan Cox2742fd82008-04-29 14:45:15 +0100108 u8 TI_I2C_Type; /* Type of I2C in UMP */
109 u8 TiReadI2C; /* Set to TRUE if we have read the
110 I2c in Boot Mode */
Matthias Kaehlcke241ca642007-12-04 16:15:40 +0100111 struct mutex es_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 int num_ports_open;
113 struct usb_serial *serial;
114};
115
116
117/* Devices that this driver supports */
Németh Márton7d40d7e2010-01-10 15:34:24 +0100118static const struct usb_device_id edgeport_1port_id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
120 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
121 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
122 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
123 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
124 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
125 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
126 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
127 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
128 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
129 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
130 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
131 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
132 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
133 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
134 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
135 { }
136};
137
Németh Márton7d40d7e2010-01-10 15:34:24 +0100138static const struct usb_device_id edgeport_2port_id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
140 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
141 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
142 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
143 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
144 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
145 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
146 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
147 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
148 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
149 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
150 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -0400151 /* The 4, 8 and 16 port devices show up as multiple 2 port devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -0400153 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
154 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
155 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
156 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 { }
158};
159
160/* Devices that this driver supports */
Németh Márton7d40d7e2010-01-10 15:34:24 +0100161static const struct usb_device_id id_table_combined[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
163 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
164 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
165 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
166 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
167 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
168 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
169 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
170 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
171 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
172 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
173 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
174 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
175 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
176 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
177 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
178 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
179 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
180 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
181 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
182 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
183 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
184 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
185 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
186 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
187 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
188 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
189 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
190 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -0400191 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
192 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
193 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
194 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 { }
196};
197
Alan Cox2742fd82008-04-29 14:45:15 +0100198MODULE_DEVICE_TABLE(usb, id_table_combined);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530200static unsigned char OperationalMajorVersion;
201static unsigned char OperationalMinorVersion;
202static unsigned short OperationalBuildNumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030204static bool debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206static int closing_wait = EDGE_CLOSING_WAIT;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030207static bool ignore_cpu_rev;
Alan Cox2742fd82008-04-29 14:45:15 +0100208static int default_uart_mode; /* RS232 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Alan Cox2742fd82008-04-29 14:45:15 +0100210static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
211 unsigned char *data, int length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213static void stop_read(struct edgeport_port *edge_port);
214static int restart_read(struct edgeport_port *edge_port);
215
Alan Cox95da3102008-07-22 11:09:07 +0100216static void edge_set_termios(struct tty_struct *tty,
217 struct usb_serial_port *port, struct ktermios *old_termios);
218static void edge_send(struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -0400220/* sysfs attributes */
221static int edge_create_sysfs_attrs(struct usb_serial_port *port);
222static int edge_remove_sysfs_attrs(struct usb_serial_port *port);
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Alan Cox2742fd82008-04-29 14:45:15 +0100225static int ti_vread_sync(struct usb_device *dev, __u8 request,
226 __u16 value, __u16 index, u8 *data, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
228 int status;
229
Alan Cox2742fd82008-04-29 14:45:15 +0100230 status = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request,
231 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
232 value, index, data, size, 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (status < 0)
234 return status;
235 if (status != size) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700236 dev_dbg(&dev->dev, "%s - wanted to write %d, but only wrote %d\n",
237 __func__, size, status);
Alan Cox2742fd82008-04-29 14:45:15 +0100238 return -ECOMM;
239 }
240 return 0;
241}
242
243static int ti_vsend_sync(struct usb_device *dev, __u8 request,
244 __u16 value, __u16 index, u8 *data, int size)
245{
246 int status;
247
248 status = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
249 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
250 value, index, data, size, 1000);
251 if (status < 0)
252 return status;
253 if (status != size) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700254 dev_dbg(&dev->dev, "%s - wanted to write %d, but only wrote %d\n",
255 __func__, size, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return -ECOMM;
257 }
258 return 0;
259}
260
Alan Cox2742fd82008-04-29 14:45:15 +0100261static int send_cmd(struct usb_device *dev, __u8 command,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 __u8 moduleid, __u16 value, u8 *data,
263 int size)
264{
Alan Cox2742fd82008-04-29 14:45:15 +0100265 return ti_vsend_sync(dev, command, value, moduleid, data, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
268/* clear tx/rx buffers and fifo in TI UMP */
Alan Cox2742fd82008-04-29 14:45:15 +0100269static int purge_port(struct usb_serial_port *port, __u16 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 int port_number = port->number - port->serial->minor;
272
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700273 dev_dbg(&port->dev, "%s - port %d, mask %x\n", __func__, port_number, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Alan Cox2742fd82008-04-29 14:45:15 +0100275 return send_cmd(port->serial->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 UMPC_PURGE_PORT,
277 (__u8)(UMPM_UART1_PORT + port_number),
278 mask,
279 NULL,
280 0);
281}
282
283/**
Alan Cox2742fd82008-04-29 14:45:15 +0100284 * read_download_mem - Read edgeport memory from TI chip
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 * @dev: usb device pointer
286 * @start_address: Device CPU address at which to read
287 * @length: Length of above data
288 * @address_type: Can read both XDATA and I2C
289 * @buffer: pointer to input data buffer
290 */
Alan Cox2742fd82008-04-29 14:45:15 +0100291static int read_download_mem(struct usb_device *dev, int start_address,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 int length, __u8 address_type, __u8 *buffer)
293{
294 int status = 0;
295 __u8 read_length;
296 __be16 be_start_address;
Alan Cox2742fd82008-04-29 14:45:15 +0100297
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700298 dev_dbg(&dev->dev, "%s - @ %x for %d\n", __func__, start_address, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 /* Read in blocks of 64 bytes
301 * (TI firmware can't handle more than 64 byte reads)
302 */
303 while (length) {
304 if (length > 64)
Alan Cox2742fd82008-04-29 14:45:15 +0100305 read_length = 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 else
307 read_length = (__u8)length;
308
309 if (read_length > 1) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700310 dev_dbg(&dev->dev, "%s - @ %x for %d\n", __func__, start_address, read_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
Alan Cox2742fd82008-04-29 14:45:15 +0100312 be_start_address = cpu_to_be16(start_address);
313 status = ti_vread_sync(dev, UMPC_MEMORY_READ,
314 (__u16)address_type,
315 (__force __u16)be_start_address,
316 buffer, read_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 if (status) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700319 dev_dbg(&dev->dev, "%s - ERROR %x\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return status;
321 }
322
Alan Cox2742fd82008-04-29 14:45:15 +0100323 if (read_length > 1)
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100324 usb_serial_debug_data(&dev->dev, __func__, read_length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 /* Update pointers/length */
327 start_address += read_length;
328 buffer += read_length;
329 length -= read_length;
330 }
Alan Cox2742fd82008-04-29 14:45:15 +0100331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 return status;
333}
334
Alan Cox2742fd82008-04-29 14:45:15 +0100335static int read_ram(struct usb_device *dev, int start_address,
336 int length, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Alan Cox2742fd82008-04-29 14:45:15 +0100338 return read_download_mem(dev, start_address, length,
339 DTK_ADDR_SPACE_XDATA, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
342/* Read edgeport memory to a given block */
Alan Cox2742fd82008-04-29 14:45:15 +0100343static int read_boot_mem(struct edgeport_serial *serial,
344 int start_address, int length, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346 int status = 0;
347 int i;
348
Alan Cox2742fd82008-04-29 14:45:15 +0100349 for (i = 0; i < length; i++) {
350 status = ti_vread_sync(serial->serial->dev,
351 UMPC_MEMORY_READ, serial->TI_I2C_Type,
352 (__u16)(start_address+i), &buffer[i], 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (status) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700354 dev_dbg(&serial->serial->dev->dev, "%s - ERROR %x\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return status;
356 }
357 }
358
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700359 dev_dbg(&serial->serial->dev->dev, "%s - start_address = %x, length = %d\n",
360 __func__, start_address, length);
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100361 usb_serial_debug_data(&serial->serial->dev->dev, __func__, length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 serial->TiReadI2C = 1;
364
365 return status;
366}
367
368/* Write given block to TI EPROM memory */
Alan Cox2742fd82008-04-29 14:45:15 +0100369static int write_boot_mem(struct edgeport_serial *serial,
370 int start_address, int length, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
372 int status = 0;
373 int i;
Johan Hovolde9305d22009-12-28 23:01:50 +0100374 u8 *temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 /* Must do a read before write */
377 if (!serial->TiReadI2C) {
Johan Hovolde9305d22009-12-28 23:01:50 +0100378 temp = kmalloc(1, GFP_KERNEL);
379 if (!temp) {
380 dev_err(&serial->serial->dev->dev,
381 "%s - out of memory\n", __func__);
382 return -ENOMEM;
383 }
384 status = read_boot_mem(serial, 0, 1, temp);
385 kfree(temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (status)
387 return status;
388 }
389
Alan Cox2742fd82008-04-29 14:45:15 +0100390 for (i = 0; i < length; ++i) {
391 status = ti_vsend_sync(serial->serial->dev,
392 UMPC_MEMORY_WRITE, buffer[i],
393 (__u16)(i + start_address), NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if (status)
395 return status;
396 }
397
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700398 dev_dbg(&serial->serial->dev->dev, "%s - start_sddr = %x, length = %d\n", __func__, start_address, length);
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100399 usb_serial_debug_data(&serial->serial->dev->dev, __func__, length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 return status;
402}
403
404
405/* Write edgeport I2C memory to TI chip */
Alan Cox2742fd82008-04-29 14:45:15 +0100406static int write_i2c_mem(struct edgeport_serial *serial,
407 int start_address, int length, __u8 address_type, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700409 struct device *dev = &serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 int status = 0;
411 int write_length;
412 __be16 be_start_address;
413
414 /* We can only send a maximum of 1 aligned byte page at a time */
Alan Cox2742fd82008-04-29 14:45:15 +0100415
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300416 /* calculate the number of bytes left in the first page */
Alan Cox2742fd82008-04-29 14:45:15 +0100417 write_length = EPROM_PAGE_SIZE -
418 (start_address & (EPROM_PAGE_SIZE - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 if (write_length > length)
421 write_length = length;
422
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700423 dev_dbg(dev, "%s - BytesInFirstPage Addr = %x, length = %d\n",
424 __func__, start_address, write_length);
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100425 usb_serial_debug_data(dev, __func__, write_length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 /* Write first page */
Alan Cox2742fd82008-04-29 14:45:15 +0100428 be_start_address = cpu_to_be16(start_address);
429 status = ti_vsend_sync(serial->serial->dev,
430 UMPC_MEMORY_WRITE, (__u16)address_type,
431 (__force __u16)be_start_address,
432 buffer, write_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if (status) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700434 dev_dbg(dev, "%s - ERROR %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 return status;
436 }
437
438 length -= write_length;
439 start_address += write_length;
440 buffer += write_length;
441
Alan Cox2742fd82008-04-29 14:45:15 +0100442 /* We should be aligned now -- can write
443 max page size bytes at a time */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 while (length) {
445 if (length > EPROM_PAGE_SIZE)
446 write_length = EPROM_PAGE_SIZE;
447 else
448 write_length = length;
449
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700450 dev_dbg(dev, "%s - Page Write Addr = %x, length = %d\n",
451 __func__, start_address, write_length);
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100452 usb_serial_debug_data(dev, __func__, write_length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 /* Write next page */
Alan Cox2742fd82008-04-29 14:45:15 +0100455 be_start_address = cpu_to_be16(start_address);
456 status = ti_vsend_sync(serial->serial->dev, UMPC_MEMORY_WRITE,
457 (__u16)address_type,
458 (__force __u16)be_start_address,
459 buffer, write_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (status) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700461 dev_err(dev, "%s - ERROR %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return status;
463 }
Alan Cox2742fd82008-04-29 14:45:15 +0100464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 length -= write_length;
466 start_address += write_length;
467 buffer += write_length;
468 }
469 return status;
470}
471
472/* Examine the UMP DMA registers and LSR
Alan Cox2742fd82008-04-29 14:45:15 +0100473 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 * Check the MSBit of the X and Y DMA byte count registers.
475 * A zero in this bit indicates that the TX DMA buffers are empty
476 * then check the TX Empty bit in the UART.
477 */
Alan Cox2742fd82008-04-29 14:45:15 +0100478static int tx_active(struct edgeport_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
480 int status;
481 struct out_endpoint_desc_block *oedb;
482 __u8 *lsr;
483 int bytes_left = 0;
484
Alan Cox2742fd82008-04-29 14:45:15 +0100485 oedb = kmalloc(sizeof(*oedb), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (!oedb) {
Alan Cox2742fd82008-04-29 14:45:15 +0100487 dev_err(&port->port->dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 return -ENOMEM;
489 }
490
Alan Cox2742fd82008-04-29 14:45:15 +0100491 lsr = kmalloc(1, GFP_KERNEL); /* Sigh, that's right, just one byte,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 as not all platforms can do DMA
493 from stack */
494 if (!lsr) {
495 kfree(oedb);
496 return -ENOMEM;
497 }
498 /* Read the DMA Count Registers */
Alan Cox2742fd82008-04-29 14:45:15 +0100499 status = read_ram(port->port->serial->dev, port->dma_address,
500 sizeof(*oedb), (void *)oedb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 if (status)
502 goto exit_is_tx_active;
503
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700504 dev_dbg(&port->port->dev, "%s - XByteCount 0x%X\n", __func__, oedb->XByteCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 /* and the LSR */
Alan Cox2742fd82008-04-29 14:45:15 +0100507 status = read_ram(port->port->serial->dev,
508 port->uart_base + UMPMEM_OFFS_UART_LSR, 1, lsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 if (status)
511 goto exit_is_tx_active;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700512 dev_dbg(&port->port->dev, "%s - LSR = 0x%X\n", __func__, *lsr);
Alan Cox2742fd82008-04-29 14:45:15 +0100513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 /* If either buffer has data or we are transmitting then return TRUE */
Alan Cox2742fd82008-04-29 14:45:15 +0100515 if ((oedb->XByteCount & 0x80) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 bytes_left += 64;
517
Alan Cox2742fd82008-04-29 14:45:15 +0100518 if ((*lsr & UMP_UART_LSR_TX_MASK) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 bytes_left += 1;
520
521 /* We return Not Active if we get any kind of error */
522exit_is_tx_active:
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700523 dev_dbg(&port->port->dev, "%s - return %d\n", __func__, bytes_left);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 kfree(lsr);
526 kfree(oedb);
527 return bytes_left;
528}
529
Alan Cox2742fd82008-04-29 14:45:15 +0100530static void chase_port(struct edgeport_port *port, unsigned long timeout,
531 int flush)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
533 int baud_rate;
Alan Cox4a90f092008-10-13 10:39:46 +0100534 struct tty_struct *tty = tty_port_tty_get(&port->port->port);
Johan Hovoldaf581052012-03-26 20:31:38 +0200535 struct usb_serial *serial = port->port->serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 wait_queue_t wait;
537 unsigned long flags;
538
539 if (!timeout)
Alan Cox2742fd82008-04-29 14:45:15 +0100540 timeout = (HZ * EDGE_CLOSING_WAIT)/100;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 /* wait for data to drain from the buffer */
543 spin_lock_irqsave(&port->ep_lock, flags);
544 init_waitqueue_entry(&wait, current);
545 add_wait_queue(&tty->write_wait, &wait);
546 for (;;) {
547 set_current_state(TASK_INTERRUPTIBLE);
Johan Hovoldd733cec2010-05-19 00:01:37 +0200548 if (kfifo_len(&port->write_fifo) == 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 || timeout == 0 || signal_pending(current)
Johan Hovoldaf581052012-03-26 20:31:38 +0200550 || serial->disconnected)
Alan Cox2742fd82008-04-29 14:45:15 +0100551 /* disconnect */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 break;
553 spin_unlock_irqrestore(&port->ep_lock, flags);
554 timeout = schedule_timeout(timeout);
555 spin_lock_irqsave(&port->ep_lock, flags);
556 }
557 set_current_state(TASK_RUNNING);
558 remove_wait_queue(&tty->write_wait, &wait);
559 if (flush)
Johan Hovoldd733cec2010-05-19 00:01:37 +0200560 kfifo_reset_out(&port->write_fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 spin_unlock_irqrestore(&port->ep_lock, flags);
Alan Cox4a90f092008-10-13 10:39:46 +0100562 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 /* wait for data to drain from the device */
565 timeout += jiffies;
566 while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
Johan Hovoldaf581052012-03-26 20:31:38 +0200567 && !serial->disconnected) {
Alan Cox2742fd82008-04-29 14:45:15 +0100568 /* not disconnected */
569 if (!tx_active(port))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 break;
571 msleep(10);
572 }
573
574 /* disconnected */
Johan Hovoldaf581052012-03-26 20:31:38 +0200575 if (serial->disconnected)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return;
577
578 /* wait one more character time, based on baud rate */
Alan Cox2742fd82008-04-29 14:45:15 +0100579 /* (tx_active doesn't seem to wait for the last byte) */
580 baud_rate = port->baud_rate;
581 if (baud_rate == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 baud_rate = 50;
Julia Lawalldfa5ec72008-03-04 15:25:11 -0800583 msleep(max(1, DIV_ROUND_UP(10000, baud_rate)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
585
Alan Cox2742fd82008-04-29 14:45:15 +0100586static int choose_config(struct usb_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
Alan Cox2742fd82008-04-29 14:45:15 +0100588 /*
589 * There may be multiple configurations on this device, in which case
590 * we would need to read and parse all of them to find out which one
591 * we want. However, we just support one config at this point,
592 * configuration # 1, which is Config Descriptor 0.
593 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700595 dev_dbg(&dev->dev, "%s - Number of Interfaces = %d\n",
596 __func__, dev->config->desc.bNumInterfaces);
597 dev_dbg(&dev->dev, "%s - MAX Power = %d\n",
598 __func__, dev->config->desc.bMaxPower * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 if (dev->config->desc.bNumInterfaces != 1) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700601 dev_err(&dev->dev, "%s - bNumInterfaces is not 1, ERROR!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return -ENODEV;
603 }
604
605 return 0;
606}
607
Alan Cox2742fd82008-04-29 14:45:15 +0100608static int read_rom(struct edgeport_serial *serial,
609 int start_address, int length, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
611 int status;
612
613 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
Alan Cox2742fd82008-04-29 14:45:15 +0100614 status = read_download_mem(serial->serial->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 start_address,
616 length,
617 serial->TI_I2C_Type,
618 buffer);
619 } else {
Alan Cox2742fd82008-04-29 14:45:15 +0100620 status = read_boot_mem(serial, start_address, length,
621 buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return status;
624}
625
Alan Cox2742fd82008-04-29 14:45:15 +0100626static int write_rom(struct edgeport_serial *serial, int start_address,
627 int length, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
629 if (serial->product_info.TiMode == TI_MODE_BOOT)
Alan Cox2742fd82008-04-29 14:45:15 +0100630 return write_boot_mem(serial, start_address, length,
631 buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD)
Alan Cox2742fd82008-04-29 14:45:15 +0100634 return write_i2c_mem(serial, start_address, length,
635 serial->TI_I2C_Type, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return -EINVAL;
637}
638
639
640
641/* Read a descriptor header from I2C based on type */
Alan Cox2742fd82008-04-29 14:45:15 +0100642static int get_descriptor_addr(struct edgeport_serial *serial,
643 int desc_type, struct ti_i2c_desc *rom_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
645 int start_address;
646 int status;
647
648 /* Search for requested descriptor in I2C */
649 start_address = 2;
650 do {
Alan Cox2742fd82008-04-29 14:45:15 +0100651 status = read_rom(serial,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 start_address,
653 sizeof(struct ti_i2c_desc),
Alan Cox2742fd82008-04-29 14:45:15 +0100654 (__u8 *)rom_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 if (status)
656 return 0;
657
658 if (rom_desc->Type == desc_type)
659 return start_address;
660
Alan Cox2742fd82008-04-29 14:45:15 +0100661 start_address = start_address + sizeof(struct ti_i2c_desc)
662 + rom_desc->Size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664 } while ((start_address < TI_MAX_I2C_SIZE) && rom_desc->Type);
Alan Cox2742fd82008-04-29 14:45:15 +0100665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return 0;
667}
668
669/* Validate descriptor checksum */
Alan Cox2742fd82008-04-29 14:45:15 +0100670static int valid_csum(struct ti_i2c_desc *rom_desc, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
672 __u16 i;
673 __u8 cs = 0;
674
Alan Cox2742fd82008-04-29 14:45:15 +0100675 for (i = 0; i < rom_desc->Size; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 cs = (__u8)(cs + buffer[i]);
Alan Cox2742fd82008-04-29 14:45:15 +0100677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (cs != rom_desc->CheckSum) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700679 pr_debug("%s - Mismatch %x - %x", __func__, rom_desc->CheckSum, cs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return -EINVAL;
681 }
682 return 0;
683}
684
685/* Make sure that the I2C image is good */
Alan Cox2742fd82008-04-29 14:45:15 +0100686static int check_i2c_image(struct edgeport_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
688 struct device *dev = &serial->serial->dev->dev;
689 int status = 0;
690 struct ti_i2c_desc *rom_desc;
691 int start_address = 2;
692 __u8 *buffer;
693 __u16 ttype;
694
Alan Cox2742fd82008-04-29 14:45:15 +0100695 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (!rom_desc) {
Alan Cox2742fd82008-04-29 14:45:15 +0100697 dev_err(dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 return -ENOMEM;
699 }
Alan Cox2742fd82008-04-29 14:45:15 +0100700 buffer = kmalloc(TI_MAX_I2C_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (!buffer) {
Alan Cox2742fd82008-04-29 14:45:15 +0100702 dev_err(dev, "%s - out of memory when allocating buffer\n",
703 __func__);
704 kfree(rom_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return -ENOMEM;
706 }
707
Alan Cox2742fd82008-04-29 14:45:15 +0100708 /* Read the first byte (Signature0) must be 0x52 or 0x10 */
709 status = read_rom(serial, 0, 1, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (status)
Alan Cox2742fd82008-04-29 14:45:15 +0100711 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 if (*buffer != UMP5152 && *buffer != UMP3410) {
Alan Cox2742fd82008-04-29 14:45:15 +0100714 dev_err(dev, "%s - invalid buffer signature\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 status = -ENODEV;
Alan Cox2742fd82008-04-29 14:45:15 +0100716 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
718
719 do {
Alan Cox2742fd82008-04-29 14:45:15 +0100720 /* Validate the I2C */
721 status = read_rom(serial,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 start_address,
723 sizeof(struct ti_i2c_desc),
724 (__u8 *)rom_desc);
725 if (status)
726 break;
727
Alan Cox2742fd82008-04-29 14:45:15 +0100728 if ((start_address + sizeof(struct ti_i2c_desc) +
729 rom_desc->Size) > TI_MAX_I2C_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 status = -ENODEV;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700731 dev_dbg(dev, "%s - structure too big, erroring out.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 break;
733 }
734
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700735 dev_dbg(dev, "%s Type = 0x%x\n", __func__, rom_desc->Type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Alan Cox2742fd82008-04-29 14:45:15 +0100737 /* Skip type 2 record */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 ttype = rom_desc->Type & 0x0f;
Alan Cox2742fd82008-04-29 14:45:15 +0100739 if (ttype != I2C_DESC_TYPE_FIRMWARE_BASIC
740 && ttype != I2C_DESC_TYPE_FIRMWARE_AUTO) {
741 /* Read the descriptor data */
742 status = read_rom(serial, start_address +
743 sizeof(struct ti_i2c_desc),
744 rom_desc->Size, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if (status)
746 break;
747
Alan Cox2742fd82008-04-29 14:45:15 +0100748 status = valid_csum(rom_desc, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 if (status)
750 break;
751 }
Alan Cox2742fd82008-04-29 14:45:15 +0100752 start_address = start_address + sizeof(struct ti_i2c_desc) +
753 rom_desc->Size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Alan Cox2742fd82008-04-29 14:45:15 +0100755 } while ((rom_desc->Type != I2C_DESC_TYPE_ION) &&
756 (start_address < TI_MAX_I2C_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Alan Cox2742fd82008-04-29 14:45:15 +0100758 if ((rom_desc->Type != I2C_DESC_TYPE_ION) ||
759 (start_address > TI_MAX_I2C_SIZE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 status = -ENODEV;
761
Alan Cox2742fd82008-04-29 14:45:15 +0100762out:
763 kfree(buffer);
764 kfree(rom_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 return status;
766}
767
Alan Cox2742fd82008-04-29 14:45:15 +0100768static int get_manuf_info(struct edgeport_serial *serial, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
770 int status;
771 int start_address;
772 struct ti_i2c_desc *rom_desc;
773 struct edge_ti_manuf_descriptor *desc;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700774 struct device *dev = &serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Alan Cox2742fd82008-04-29 14:45:15 +0100776 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 if (!rom_desc) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700778 dev_err(dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return -ENOMEM;
780 }
Alan Cox2742fd82008-04-29 14:45:15 +0100781 start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_ION,
782 rom_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 if (!start_address) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700785 dev_dbg(dev, "%s - Edge Descriptor not found in I2C\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 status = -ENODEV;
787 goto exit;
788 }
789
Alan Cox2742fd82008-04-29 14:45:15 +0100790 /* Read the descriptor data */
791 status = read_rom(serial, start_address+sizeof(struct ti_i2c_desc),
792 rom_desc->Size, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (status)
794 goto exit;
Alan Cox2742fd82008-04-29 14:45:15 +0100795
796 status = valid_csum(rom_desc, buffer);
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 desc = (struct edge_ti_manuf_descriptor *)buffer;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700799 dev_dbg(dev, "%s - IonConfig 0x%x\n", __func__, desc->IonConfig);
800 dev_dbg(dev, "%s - Version %d\n", __func__, desc->Version);
801 dev_dbg(dev, "%s - Cpu/Board 0x%x\n", __func__, desc->CpuRev_BoardRev);
802 dev_dbg(dev, "%s - NumPorts %d\n", __func__, desc->NumPorts);
803 dev_dbg(dev, "%s - NumVirtualPorts %d\n", __func__, desc->NumVirtualPorts);
804 dev_dbg(dev, "%s - TotalPorts %d\n", __func__, desc->TotalPorts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806exit:
Alan Cox2742fd82008-04-29 14:45:15 +0100807 kfree(rom_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 return status;
809}
810
811/* Build firmware header used for firmware update */
Alan Cox2742fd82008-04-29 14:45:15 +0100812static int build_i2c_fw_hdr(__u8 *header, struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
814 __u8 *buffer;
815 int buffer_size;
816 int i;
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530817 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 __u8 cs = 0;
819 struct ti_i2c_desc *i2c_header;
820 struct ti_i2c_image_header *img_header;
821 struct ti_i2c_firmware_rec *firmware_rec;
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530822 const struct firmware *fw;
823 const char *fw_name = "edgeport/down3.bin";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Alan Cox2742fd82008-04-29 14:45:15 +0100825 /* In order to update the I2C firmware we must change the type 2 record
826 * to type 0xF2. This will force the UMP to come up in Boot Mode.
827 * Then while in boot mode, the driver will download the latest
828 * firmware (padded to 15.5k) into the UMP ram. And finally when the
829 * device comes back up in download mode the driver will cause the new
830 * firmware to be copied from the UMP Ram to I2C and the firmware will
831 * update the record type from 0xf2 to 0x02.
832 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Alan Cox2742fd82008-04-29 14:45:15 +0100834 /* Allocate a 15.5k buffer + 2 bytes for version number
835 * (Firmware Record) */
836 buffer_size = (((1024 * 16) - 512 ) +
837 sizeof(struct ti_i2c_firmware_rec));
838
839 buffer = kmalloc(buffer_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 if (!buffer) {
Alan Cox2742fd82008-04-29 14:45:15 +0100841 dev_err(dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 return -ENOMEM;
843 }
Alan Cox2742fd82008-04-29 14:45:15 +0100844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 // Set entire image of 0xffs
Alan Cox2742fd82008-04-29 14:45:15 +0100846 memset(buffer, 0xff, buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530848 err = request_firmware(&fw, fw_name, dev);
849 if (err) {
850 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
851 fw_name, err);
852 kfree(buffer);
853 return err;
854 }
855
856 /* Save Download Version Number */
857 OperationalMajorVersion = fw->data[0];
858 OperationalMinorVersion = fw->data[1];
859 OperationalBuildNumber = fw->data[2] | (fw->data[3] << 8);
860
Alan Cox2742fd82008-04-29 14:45:15 +0100861 /* Copy version number into firmware record */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 firmware_rec = (struct ti_i2c_firmware_rec *)buffer;
863
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530864 firmware_rec->Ver_Major = OperationalMajorVersion;
865 firmware_rec->Ver_Minor = OperationalMinorVersion;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Alan Cox2742fd82008-04-29 14:45:15 +0100867 /* Pointer to fw_down memory image */
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530868 img_header = (struct ti_i2c_image_header *)&fw->data[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Alan Cox2742fd82008-04-29 14:45:15 +0100870 memcpy(buffer + sizeof(struct ti_i2c_firmware_rec),
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530871 &fw->data[4 + sizeof(struct ti_i2c_image_header)],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 le16_to_cpu(img_header->Length));
873
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530874 release_firmware(fw);
875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 for (i=0; i < buffer_size; i++) {
877 cs = (__u8)(cs + buffer[i]);
878 }
879
Alan Cox2742fd82008-04-29 14:45:15 +0100880 kfree(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Alan Cox2742fd82008-04-29 14:45:15 +0100882 /* Build new header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 i2c_header = (struct ti_i2c_desc *)header;
884 firmware_rec = (struct ti_i2c_firmware_rec*)i2c_header->Data;
Alan Cox2742fd82008-04-29 14:45:15 +0100885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 i2c_header->Type = I2C_DESC_TYPE_FIRMWARE_BLANK;
887 i2c_header->Size = (__u16)buffer_size;
888 i2c_header->CheckSum = cs;
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530889 firmware_rec->Ver_Major = OperationalMajorVersion;
890 firmware_rec->Ver_Minor = OperationalMinorVersion;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 return 0;
893}
894
895/* Try to figure out what type of I2c we have */
Alan Cox2742fd82008-04-29 14:45:15 +0100896static int i2c_type_bootmode(struct edgeport_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700898 struct device *dev = &serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 int status;
Johan Hovolde9305d22009-12-28 23:01:50 +0100900 u8 *data;
901
902 data = kmalloc(1, GFP_KERNEL);
903 if (!data) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700904 dev_err(dev, "%s - out of memory\n", __func__);
Johan Hovolde9305d22009-12-28 23:01:50 +0100905 return -ENOMEM;
906 }
Alan Cox2742fd82008-04-29 14:45:15 +0100907
908 /* Try to read type 2 */
909 status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
Johan Hovolde9305d22009-12-28 23:01:50 +0100910 DTK_ADDR_SPACE_I2C_TYPE_II, 0, data, 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 if (status)
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700912 dev_dbg(dev, "%s - read 2 status error = %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 else
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700914 dev_dbg(dev, "%s - read 2 data = 0x%x\n", __func__, *data);
Johan Hovolde9305d22009-12-28 23:01:50 +0100915 if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700916 dev_dbg(dev, "%s - ROM_TYPE_II\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
Johan Hovolde9305d22009-12-28 23:01:50 +0100918 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
920
Alan Cox2742fd82008-04-29 14:45:15 +0100921 /* Try to read type 3 */
922 status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
Johan Hovolde9305d22009-12-28 23:01:50 +0100923 DTK_ADDR_SPACE_I2C_TYPE_III, 0, data, 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 if (status)
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700925 dev_dbg(dev, "%s - read 3 status error = %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 else
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700927 dev_dbg(dev, "%s - read 2 data = 0x%x\n", __func__, *data);
Johan Hovolde9305d22009-12-28 23:01:50 +0100928 if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700929 dev_dbg(dev, "%s - ROM_TYPE_III\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_III;
Johan Hovolde9305d22009-12-28 23:01:50 +0100931 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
933
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -0700934 dev_dbg(dev, "%s - Unknown\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
Johan Hovolde9305d22009-12-28 23:01:50 +0100936 status = -ENODEV;
937out:
938 kfree(data);
939 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940}
941
Alan Cox2742fd82008-04-29 14:45:15 +0100942static int bulk_xfer(struct usb_serial *serial, void *buffer,
943 int length, int *num_sent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
945 int status;
946
Alan Cox2742fd82008-04-29 14:45:15 +0100947 status = usb_bulk_msg(serial->dev,
948 usb_sndbulkpipe(serial->dev,
949 serial->port[0]->bulk_out_endpointAddress),
950 buffer, length, num_sent, 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 return status;
952}
953
954/* Download given firmware image to the device (IN BOOT MODE) */
Alan Cox2742fd82008-04-29 14:45:15 +0100955static int download_code(struct edgeport_serial *serial, __u8 *image,
956 int image_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
958 int status = 0;
959 int pos;
960 int transfer;
961 int done;
962
Alan Cox2742fd82008-04-29 14:45:15 +0100963 /* Transfer firmware image */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 for (pos = 0; pos < image_length; ) {
Alan Cox2742fd82008-04-29 14:45:15 +0100965 /* Read the next buffer from file */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 transfer = image_length - pos;
967 if (transfer > EDGE_FW_BULK_MAX_PACKET_SIZE)
968 transfer = EDGE_FW_BULK_MAX_PACKET_SIZE;
969
Alan Cox2742fd82008-04-29 14:45:15 +0100970 /* Transfer data */
971 status = bulk_xfer(serial->serial, &image[pos],
972 transfer, &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (status)
974 break;
Alan Cox2742fd82008-04-29 14:45:15 +0100975 /* Advance buffer pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 pos += done;
977 }
978
979 return status;
980}
981
Alan Cox2742fd82008-04-29 14:45:15 +0100982/* FIXME!!! */
983static int config_boot_dev(struct usb_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
985 return 0;
986}
987
Alan Cox2742fd82008-04-29 14:45:15 +0100988static int ti_cpu_rev(struct edge_ti_manuf_descriptor *desc)
989{
990 return TI_GET_CPU_REVISION(desc->CpuRev_BoardRev);
991}
992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993/**
994 * DownloadTIFirmware - Download run-time operating firmware to the TI5052
Alan Cox2742fd82008-04-29 14:45:15 +0100995 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 * This routine downloads the main operating code into the TI5052, using the
997 * boot code already burned into E2PROM or ROM.
998 */
Alan Cox2742fd82008-04-29 14:45:15 +0100999static int download_fw(struct edgeport_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
1001 struct device *dev = &serial->serial->dev->dev;
1002 int status = 0;
1003 int start_address;
1004 struct edge_ti_manuf_descriptor *ti_manuf_desc;
1005 struct usb_interface_descriptor *interface;
1006 int download_cur_ver;
1007 int download_new_ver;
1008
1009 /* This routine is entered by both the BOOT mode and the Download mode
1010 * We can determine which code is running by the reading the config
1011 * descriptor and if we have only one bulk pipe it is in boot mode
1012 */
1013 serial->product_info.hardware_type = HARDWARE_TYPE_TIUMP;
1014
1015 /* Default to type 2 i2c */
1016 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
1017
Alan Cox2742fd82008-04-29 14:45:15 +01001018 status = choose_config(serial->serial->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 if (status)
1020 return status;
1021
1022 interface = &serial->serial->interface->cur_altsetting->desc;
1023 if (!interface) {
Alan Cox2742fd82008-04-29 14:45:15 +01001024 dev_err(dev, "%s - no interface set, error!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 return -ENODEV;
1026 }
1027
Alan Cox2742fd82008-04-29 14:45:15 +01001028 /*
1029 * Setup initial mode -- the default mode 0 is TI_MODE_CONFIGURING
1030 * if we have more than one endpoint we are definitely in download
1031 * mode
1032 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if (interface->bNumEndpoints > 1)
1034 serial->product_info.TiMode = TI_MODE_DOWNLOAD;
1035 else
Alan Cox2742fd82008-04-29 14:45:15 +01001036 /* Otherwise we will remain in configuring mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 serial->product_info.TiMode = TI_MODE_CONFIGURING;
1038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 /********************************************************************/
1040 /* Download Mode */
1041 /********************************************************************/
1042 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
1043 struct ti_i2c_desc *rom_desc;
1044
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001045 dev_dbg(dev, "%s - RUNNING IN DOWNLOAD MODE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Alan Cox2742fd82008-04-29 14:45:15 +01001047 status = check_i2c_image(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 if (status) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001049 dev_dbg(dev, "%s - DOWNLOAD MODE -- BAD I2C\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 return status;
1051 }
Alan Cox2742fd82008-04-29 14:45:15 +01001052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 /* Validate Hardware version number
1054 * Read Manufacturing Descriptor from TI Based Edgeport
1055 */
Alan Cox2742fd82008-04-29 14:45:15 +01001056 ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 if (!ti_manuf_desc) {
Alan Cox2742fd82008-04-29 14:45:15 +01001058 dev_err(dev, "%s - out of memory.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 return -ENOMEM;
1060 }
Alan Cox2742fd82008-04-29 14:45:15 +01001061 status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001063 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return status;
1065 }
1066
Alan Cox2742fd82008-04-29 14:45:15 +01001067 /* Check version number of ION descriptor */
1068 if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001069 dev_dbg(dev, "%s - Wrong CPU Rev %d (Must be 2)\n",
Alan Cox2742fd82008-04-29 14:45:15 +01001070 __func__, ti_cpu_rev(ti_manuf_desc));
1071 kfree(ti_manuf_desc);
1072 return -EINVAL;
1073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Alan Cox2742fd82008-04-29 14:45:15 +01001075 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 if (!rom_desc) {
Alan Cox2742fd82008-04-29 14:45:15 +01001077 dev_err(dev, "%s - out of memory.\n", __func__);
1078 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 return -ENOMEM;
1080 }
1081
Alan Cox2742fd82008-04-29 14:45:15 +01001082 /* Search for type 2 record (firmware record) */
1083 start_address = get_descriptor_addr(serial,
1084 I2C_DESC_TYPE_FIRMWARE_BASIC, rom_desc);
1085 if (start_address != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 struct ti_i2c_firmware_rec *firmware_version;
Johan Hovolde9305d22009-12-28 23:01:50 +01001087 u8 *record;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001089 dev_dbg(dev, "%s - Found Type FIRMWARE (Type 2) record\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Alan Cox2742fd82008-04-29 14:45:15 +01001091 firmware_version = kmalloc(sizeof(*firmware_version),
1092 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 if (!firmware_version) {
Alan Cox2742fd82008-04-29 14:45:15 +01001094 dev_err(dev, "%s - out of memory.\n", __func__);
1095 kfree(rom_desc);
1096 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 return -ENOMEM;
1098 }
1099
Alan Cox2742fd82008-04-29 14:45:15 +01001100 /* Validate version number
1101 * Read the descriptor data
1102 */
1103 status = read_rom(serial, start_address +
1104 sizeof(struct ti_i2c_desc),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 sizeof(struct ti_i2c_firmware_rec),
1106 (__u8 *)firmware_version);
1107 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001108 kfree(firmware_version);
1109 kfree(rom_desc);
1110 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 return status;
1112 }
1113
Alan Cox2742fd82008-04-29 14:45:15 +01001114 /* Check version number of download with current
1115 version in I2c */
1116 download_cur_ver = (firmware_version->Ver_Major << 8) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 (firmware_version->Ver_Minor);
Jaswinder Singhd12b2192008-07-04 23:06:09 +05301118 download_new_ver = (OperationalMajorVersion << 8) +
1119 (OperationalMinorVersion);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001121 dev_dbg(dev, "%s - >> FW Versions Device %d.%d Driver %d.%d\n",
1122 __func__, firmware_version->Ver_Major,
1123 firmware_version->Ver_Minor,
1124 OperationalMajorVersion,
1125 OperationalMinorVersion);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Alan Cox2742fd82008-04-29 14:45:15 +01001127 /* Check if we have an old version in the I2C and
1128 update if necessary */
Greg Kroah-Hartman0827a9f2010-08-17 15:15:37 -07001129 if (download_cur_ver < download_new_ver) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001130 dev_dbg(dev, "%s - Update I2C dld from %d.%d to %d.%d\n",
1131 __func__,
1132 firmware_version->Ver_Major,
1133 firmware_version->Ver_Minor,
1134 OperationalMajorVersion,
1135 OperationalMinorVersion);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Johan Hovolde9305d22009-12-28 23:01:50 +01001137 record = kmalloc(1, GFP_KERNEL);
1138 if (!record) {
1139 dev_err(dev, "%s - out of memory.\n",
1140 __func__);
1141 kfree(firmware_version);
1142 kfree(rom_desc);
1143 kfree(ti_manuf_desc);
1144 return -ENOMEM;
1145 }
Alan Cox2742fd82008-04-29 14:45:15 +01001146 /* In order to update the I2C firmware we must
1147 * change the type 2 record to type 0xF2. This
1148 * will force the UMP to come up in Boot Mode.
1149 * Then while in boot mode, the driver will
1150 * download the latest firmware (padded to
1151 * 15.5k) into the UMP ram. Finally when the
1152 * device comes back up in download mode the
1153 * driver will cause the new firmware to be
1154 * copied from the UMP Ram to I2C and the
1155 * firmware will update the record type from
1156 * 0xf2 to 0x02.
1157 */
Johan Hovolde9305d22009-12-28 23:01:50 +01001158 *record = I2C_DESC_TYPE_FIRMWARE_BLANK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Alan Cox2742fd82008-04-29 14:45:15 +01001160 /* Change the I2C Firmware record type to
1161 0xf2 to trigger an update */
1162 status = write_rom(serial, start_address,
Johan Hovolde9305d22009-12-28 23:01:50 +01001163 sizeof(*record), record);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 if (status) {
Johan Hovolde9305d22009-12-28 23:01:50 +01001165 kfree(record);
Alan Cox2742fd82008-04-29 14:45:15 +01001166 kfree(firmware_version);
1167 kfree(rom_desc);
1168 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 return status;
1170 }
1171
Alan Cox2742fd82008-04-29 14:45:15 +01001172 /* verify the write -- must do this in order
1173 * for write to complete before we do the
1174 * hardware reset
1175 */
1176 status = read_rom(serial,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 start_address,
Johan Hovolde9305d22009-12-28 23:01:50 +01001178 sizeof(*record),
1179 record);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 if (status) {
Johan Hovolde9305d22009-12-28 23:01:50 +01001181 kfree(record);
Alan Cox2742fd82008-04-29 14:45:15 +01001182 kfree(firmware_version);
1183 kfree(rom_desc);
1184 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 return status;
1186 }
1187
Johan Hovolde9305d22009-12-28 23:01:50 +01001188 if (*record != I2C_DESC_TYPE_FIRMWARE_BLANK) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001189 dev_err(dev, "%s - error resetting device\n", __func__);
Johan Hovolde9305d22009-12-28 23:01:50 +01001190 kfree(record);
Alan Cox2742fd82008-04-29 14:45:15 +01001191 kfree(firmware_version);
1192 kfree(rom_desc);
1193 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 return -ENODEV;
1195 }
1196
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001197 dev_dbg(dev, "%s - HARDWARE RESET\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Alan Cox2742fd82008-04-29 14:45:15 +01001199 /* Reset UMP -- Back to BOOT MODE */
1200 status = ti_vsend_sync(serial->serial->dev,
1201 UMPC_HARDWARE_RESET,
1202 0, 0, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001204 dev_dbg(dev, "%s - HARDWARE RESET return %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206 /* return an error on purpose. */
Johan Hovolde9305d22009-12-28 23:01:50 +01001207 kfree(record);
Alan Cox2742fd82008-04-29 14:45:15 +01001208 kfree(firmware_version);
1209 kfree(rom_desc);
1210 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 return -ENODEV;
1212 }
Alan Cox2742fd82008-04-29 14:45:15 +01001213 kfree(firmware_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 }
Alan Cox2742fd82008-04-29 14:45:15 +01001215 /* Search for type 0xF2 record (firmware blank record) */
1216 else if ((start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_FIRMWARE_BLANK, rom_desc)) != 0) {
1217#define HEADER_SIZE (sizeof(struct ti_i2c_desc) + \
1218 sizeof(struct ti_i2c_firmware_rec))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 __u8 *header;
1220 __u8 *vheader;
1221
Alan Cox2742fd82008-04-29 14:45:15 +01001222 header = kmalloc(HEADER_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 if (!header) {
Alan Cox2742fd82008-04-29 14:45:15 +01001224 dev_err(dev, "%s - out of memory.\n", __func__);
1225 kfree(rom_desc);
1226 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 return -ENOMEM;
1228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Alan Cox2742fd82008-04-29 14:45:15 +01001230 vheader = kmalloc(HEADER_SIZE, GFP_KERNEL);
1231 if (!vheader) {
1232 dev_err(dev, "%s - out of memory.\n", __func__);
1233 kfree(header);
1234 kfree(rom_desc);
1235 kfree(ti_manuf_desc);
1236 return -ENOMEM;
1237 }
1238
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001239 dev_dbg(dev, "%s - Found Type BLANK FIRMWARE (Type F2) record\n", __func__);
Alan Cox2742fd82008-04-29 14:45:15 +01001240
1241 /*
1242 * In order to update the I2C firmware we must change
1243 * the type 2 record to type 0xF2. This will force the
1244 * UMP to come up in Boot Mode. Then while in boot
1245 * mode, the driver will download the latest firmware
1246 * (padded to 15.5k) into the UMP ram. Finally when the
1247 * device comes back up in download mode the driver
1248 * will cause the new firmware to be copied from the
1249 * UMP Ram to I2C and the firmware will update the
1250 * record type from 0xf2 to 0x02.
1251 */
1252 status = build_i2c_fw_hdr(header, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001254 kfree(vheader);
1255 kfree(header);
1256 kfree(rom_desc);
1257 kfree(ti_manuf_desc);
Roel Kluinfd6e5bb2010-08-10 14:29:19 -07001258 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 }
1260
Alan Cox2742fd82008-04-29 14:45:15 +01001261 /* Update I2C with type 0xf2 record with correct
1262 size and checksum */
1263 status = write_rom(serial,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 start_address,
1265 HEADER_SIZE,
1266 header);
1267 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001268 kfree(vheader);
1269 kfree(header);
1270 kfree(rom_desc);
1271 kfree(ti_manuf_desc);
Roel Kluin9800eb32010-07-20 15:29:08 -07001272 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 }
1274
Alan Cox2742fd82008-04-29 14:45:15 +01001275 /* verify the write -- must do this in order for
1276 write to complete before we do the hardware reset */
1277 status = read_rom(serial, start_address,
1278 HEADER_SIZE, vheader);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
1280 if (status) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001281 dev_dbg(dev, "%s - can't read header back\n", __func__);
Alan Cox2742fd82008-04-29 14:45:15 +01001282 kfree(vheader);
1283 kfree(header);
1284 kfree(rom_desc);
1285 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 return status;
1287 }
1288 if (memcmp(vheader, header, HEADER_SIZE)) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001289 dev_dbg(dev, "%s - write download record failed\n", __func__);
Alan Cox2742fd82008-04-29 14:45:15 +01001290 kfree(vheader);
1291 kfree(header);
1292 kfree(rom_desc);
1293 kfree(ti_manuf_desc);
Roel Kluin1e297092010-07-02 00:36:43 +02001294 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
1296
Alan Cox2742fd82008-04-29 14:45:15 +01001297 kfree(vheader);
1298 kfree(header);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001300 dev_dbg(dev, "%s - Start firmware update\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Alan Cox2742fd82008-04-29 14:45:15 +01001302 /* Tell firmware to copy download image into I2C */
1303 status = ti_vsend_sync(serial->serial->dev,
1304 UMPC_COPY_DNLD_TO_I2C, 0, 0, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001306 dev_dbg(dev, "%s - Update complete 0x%x\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001308 dev_err(dev,
1309 "%s - UMPC_COPY_DNLD_TO_I2C failed\n",
1310 __func__);
1311 kfree(rom_desc);
1312 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 return status;
1314 }
1315 }
1316
1317 // The device is running the download code
Alan Cox2742fd82008-04-29 14:45:15 +01001318 kfree(rom_desc);
1319 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 return 0;
1321 }
1322
1323 /********************************************************************/
1324 /* Boot Mode */
1325 /********************************************************************/
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001326 dev_dbg(dev, "%s - RUNNING IN BOOT MODE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Alan Cox2742fd82008-04-29 14:45:15 +01001328 /* Configure the TI device so we can use the BULK pipes for download */
1329 status = config_boot_dev(serial->serial->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 if (status)
1331 return status;
1332
Alan Cox2742fd82008-04-29 14:45:15 +01001333 if (le16_to_cpu(serial->serial->dev->descriptor.idVendor)
1334 != USB_VENDOR_ID_ION) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001335 dev_dbg(dev, "%s - VID = 0x%x\n", __func__,
1336 le16_to_cpu(serial->serial->dev->descriptor.idVendor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
Alan Cox2742fd82008-04-29 14:45:15 +01001338 goto stayinbootmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 }
1340
Alan Cox2742fd82008-04-29 14:45:15 +01001341 /* We have an ION device (I2c Must be programmed)
1342 Determine I2C image type */
1343 if (i2c_type_bootmode(serial))
1344 goto stayinbootmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Alan Cox2742fd82008-04-29 14:45:15 +01001346 /* Check for ION Vendor ID and that the I2C is valid */
1347 if (!check_i2c_image(serial)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 struct ti_i2c_image_header *header;
1349 int i;
1350 __u8 cs = 0;
1351 __u8 *buffer;
1352 int buffer_size;
Jaswinder Singhd12b2192008-07-04 23:06:09 +05301353 int err;
1354 const struct firmware *fw;
1355 const char *fw_name = "edgeport/down3.bin";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357 /* Validate Hardware version number
1358 * Read Manufacturing Descriptor from TI Based Edgeport
1359 */
Alan Cox2742fd82008-04-29 14:45:15 +01001360 ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 if (!ti_manuf_desc) {
Alan Cox2742fd82008-04-29 14:45:15 +01001362 dev_err(dev, "%s - out of memory.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 return -ENOMEM;
1364 }
Alan Cox2742fd82008-04-29 14:45:15 +01001365 status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001367 kfree(ti_manuf_desc);
1368 goto stayinbootmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 }
1370
Alan Cox2742fd82008-04-29 14:45:15 +01001371 /* Check for version 2 */
1372 if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001373 dev_dbg(dev, "%s - Wrong CPU Rev %d (Must be 2)\n",
1374 __func__, ti_cpu_rev(ti_manuf_desc));
Alan Cox2742fd82008-04-29 14:45:15 +01001375 kfree(ti_manuf_desc);
1376 goto stayinbootmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 }
1378
Alan Cox2742fd82008-04-29 14:45:15 +01001379 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 /*
Alan Cox2742fd82008-04-29 14:45:15 +01001382 * In order to update the I2C firmware we must change the type
1383 * 2 record to type 0xF2. This will force the UMP to come up
1384 * in Boot Mode. Then while in boot mode, the driver will
1385 * download the latest firmware (padded to 15.5k) into the
1386 * UMP ram. Finally when the device comes back up in download
1387 * mode the driver will cause the new firmware to be copied
1388 * from the UMP Ram to I2C and the firmware will update the
1389 * record type from 0xf2 to 0x02.
1390 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 * Do we really have to copy the whole firmware image,
1392 * or could we do this in place!
1393 */
1394
Alan Cox2742fd82008-04-29 14:45:15 +01001395 /* Allocate a 15.5k buffer + 3 byte header */
1396 buffer_size = (((1024 * 16) - 512) +
1397 sizeof(struct ti_i2c_image_header));
1398 buffer = kmalloc(buffer_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 if (!buffer) {
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
1404 /* Initialize the buffer to 0xff (pad the buffer) */
1405 memset(buffer, 0xff, buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Jaswinder Singhd12b2192008-07-04 23:06:09 +05301407 err = request_firmware(&fw, fw_name, dev);
1408 if (err) {
1409 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
1410 fw_name, err);
1411 kfree(buffer);
1412 return err;
1413 }
1414 memcpy(buffer, &fw->data[4], fw->size - 4);
1415 release_firmware(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Alan Cox2742fd82008-04-29 14:45:15 +01001417 for (i = sizeof(struct ti_i2c_image_header);
1418 i < buffer_size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 cs = (__u8)(cs + buffer[i]);
1420 }
Alan Cox2742fd82008-04-29 14:45:15 +01001421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 header = (struct ti_i2c_image_header *)buffer;
Alan Cox2742fd82008-04-29 14:45:15 +01001423
1424 /* update length and checksum after padding */
1425 header->Length = cpu_to_le16((__u16)(buffer_size -
1426 sizeof(struct ti_i2c_image_header)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 header->CheckSum = cs;
1428
Alan Cox2742fd82008-04-29 14:45:15 +01001429 /* Download the operational code */
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001430 dev_dbg(dev, "%s - Downloading operational code image (TI UMP)\n", __func__);
Alan Cox2742fd82008-04-29 14:45:15 +01001431 status = download_code(serial, buffer, buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Alan Cox2742fd82008-04-29 14:45:15 +01001433 kfree(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
1435 if (status) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001436 dev_dbg(dev, "%s - Error downloading operational code image\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 return status;
1438 }
1439
Alan Cox2742fd82008-04-29 14:45:15 +01001440 /* Device will reboot */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 serial->product_info.TiMode = TI_MODE_TRANSITIONING;
1442
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001443 dev_dbg(dev, "%s - Download successful -- Device rebooting...\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
1445 /* return an error on purpose */
1446 return -ENODEV;
1447 }
1448
Alan Cox2742fd82008-04-29 14:45:15 +01001449stayinbootmode:
1450 /* Eprom is invalid or blank stay in boot mode */
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001451 dev_dbg(dev, "%s - STAYING IN BOOT MODE\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 serial->product_info.TiMode = TI_MODE_BOOT;
1453
1454 return 0;
1455}
1456
1457
Alan Cox2742fd82008-04-29 14:45:15 +01001458static int ti_do_config(struct edgeport_port *port, int feature, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
1460 int port_number = port->port->number - port->port->serial->minor;
Alan Cox2742fd82008-04-29 14:45:15 +01001461 on = !!on; /* 1 or 0 not bitmask */
1462 return send_cmd(port->port->serial->dev,
1463 feature, (__u8)(UMPM_UART1_PORT + port_number),
1464 on, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465}
1466
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Alan Cox2742fd82008-04-29 14:45:15 +01001468static int restore_mcr(struct edgeport_port *port, __u8 mcr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469{
1470 int status = 0;
1471
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001472 dev_dbg(&port->port->dev, "%s - %x\n", __func__, mcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Alan Cox2742fd82008-04-29 14:45:15 +01001474 status = ti_do_config(port, UMPC_SET_CLR_DTR, mcr & MCR_DTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 if (status)
1476 return status;
Alan Cox2742fd82008-04-29 14:45:15 +01001477 status = ti_do_config(port, UMPC_SET_CLR_RTS, mcr & MCR_RTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 if (status)
1479 return status;
Alan Cox2742fd82008-04-29 14:45:15 +01001480 return ti_do_config(port, UMPC_SET_CLR_LOOPBACK, mcr & MCR_LOOPBACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481}
1482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483/* Convert TI LSR to standard UART flags */
Alan Cox2742fd82008-04-29 14:45:15 +01001484static __u8 map_line_status(__u8 ti_lsr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485{
1486 __u8 lsr = 0;
1487
1488#define MAP_FLAG(flagUmp, flagUart) \
1489 if (ti_lsr & flagUmp) \
1490 lsr |= flagUart;
1491
1492 MAP_FLAG(UMP_UART_LSR_OV_MASK, LSR_OVER_ERR) /* overrun */
1493 MAP_FLAG(UMP_UART_LSR_PE_MASK, LSR_PAR_ERR) /* parity error */
1494 MAP_FLAG(UMP_UART_LSR_FE_MASK, LSR_FRM_ERR) /* framing error */
1495 MAP_FLAG(UMP_UART_LSR_BR_MASK, LSR_BREAK) /* break detected */
Alan Cox2742fd82008-04-29 14:45:15 +01001496 MAP_FLAG(UMP_UART_LSR_RX_MASK, LSR_RX_AVAIL) /* rx data available */
1497 MAP_FLAG(UMP_UART_LSR_TX_MASK, LSR_TX_EMPTY) /* tx hold reg empty */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
1499#undef MAP_FLAG
1500
1501 return lsr;
1502}
1503
Alan Cox2742fd82008-04-29 14:45:15 +01001504static void handle_new_msr(struct edgeport_port *edge_port, __u8 msr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505{
1506 struct async_icount *icount;
1507 struct tty_struct *tty;
1508
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001509 dev_dbg(&edge_port->port->dev, "%s - %02x\n", __func__, msr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Alan Cox2742fd82008-04-29 14:45:15 +01001511 if (msr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
1512 EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 icount = &edge_port->icount;
1514
1515 /* update input line counters */
1516 if (msr & EDGEPORT_MSR_DELTA_CTS)
1517 icount->cts++;
1518 if (msr & EDGEPORT_MSR_DELTA_DSR)
1519 icount->dsr++;
1520 if (msr & EDGEPORT_MSR_DELTA_CD)
1521 icount->dcd++;
1522 if (msr & EDGEPORT_MSR_DELTA_RI)
1523 icount->rng++;
Alan Cox2742fd82008-04-29 14:45:15 +01001524 wake_up_interruptible(&edge_port->delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 }
1526
1527 /* Save the new modem status */
1528 edge_port->shadow_msr = msr & 0xf0;
1529
Alan Cox4a90f092008-10-13 10:39:46 +01001530 tty = tty_port_tty_get(&edge_port->port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 /* handle CTS flow control */
1532 if (tty && C_CRTSCTS(tty)) {
1533 if (msr & EDGEPORT_MSR_CTS) {
1534 tty->hw_stopped = 0;
1535 tty_wakeup(tty);
1536 } else {
1537 tty->hw_stopped = 1;
1538 }
1539 }
Alan Cox4a90f092008-10-13 10:39:46 +01001540 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541}
1542
Alan Cox2742fd82008-04-29 14:45:15 +01001543static void handle_new_lsr(struct edgeport_port *edge_port, int lsr_data,
1544 __u8 lsr, __u8 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545{
1546 struct async_icount *icount;
Alan Cox2742fd82008-04-29 14:45:15 +01001547 __u8 new_lsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR |
1548 LSR_FRM_ERR | LSR_BREAK));
Alan Cox4a90f092008-10-13 10:39:46 +01001549 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001551 dev_dbg(&edge_port->port->dev, "%s - %02x\n", __func__, new_lsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
1553 edge_port->shadow_lsr = lsr;
1554
Alan Cox2742fd82008-04-29 14:45:15 +01001555 if (new_lsr & LSR_BREAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 /*
1557 * Parity and Framing errors only count if they
1558 * occur exclusive of a break being received.
1559 */
1560 new_lsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
1562 /* Place LSR data byte into Rx buffer */
Alan Cox4a90f092008-10-13 10:39:46 +01001563 if (lsr_data) {
1564 tty = tty_port_tty_get(&edge_port->port->port);
1565 if (tty) {
1566 edge_tty_recv(&edge_port->port->dev, tty, &data, 1);
1567 tty_kref_put(tty);
1568 }
1569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
1571 /* update input line counters */
1572 icount = &edge_port->icount;
1573 if (new_lsr & LSR_BREAK)
1574 icount->brk++;
1575 if (new_lsr & LSR_OVER_ERR)
1576 icount->overrun++;
1577 if (new_lsr & LSR_PAR_ERR)
1578 icount->parity++;
1579 if (new_lsr & LSR_FRM_ERR)
1580 icount->frame++;
1581}
1582
1583
Alan Cox2742fd82008-04-29 14:45:15 +01001584static void edge_interrupt_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
Ming Leicdc97792008-02-24 18:41:47 +08001586 struct edgeport_serial *edge_serial = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 struct usb_serial_port *port;
1588 struct edgeport_port *edge_port;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001589 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 unsigned char *data = urb->transfer_buffer;
1591 int length = urb->actual_length;
1592 int port_number;
1593 int function;
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001594 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 __u8 lsr;
1596 __u8 msr;
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001597 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001599 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 case 0:
1601 /* success */
1602 break;
1603 case -ECONNRESET:
1604 case -ENOENT:
1605 case -ESHUTDOWN:
1606 /* this urb is terminated, clean up */
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001607 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001608 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 return;
1610 default:
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001611 dev_err(&urb->dev->dev, "%s - nonzero urb status received: "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001612 "%d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 goto exit;
1614 }
1615
1616 if (!length) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001617 dev_dbg(&urb->dev->dev, "%s - no data in urb\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 goto exit;
1619 }
1620
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001621 dev = &edge_serial->serial->dev->dev;
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001622 usb_serial_debug_data(dev, __func__, length, data);
Alan Cox2742fd82008-04-29 14:45:15 +01001623
1624 if (length != 2) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001625 dev_dbg(dev, "%s - expecting packet of size 2, got %d\n", __func__, length);
Alan Cox2742fd82008-04-29 14:45:15 +01001626 goto exit;
1627 }
1628
1629 port_number = TIUMP_GET_PORT_FROM_CODE(data[0]);
1630 function = TIUMP_GET_FUNC_FROM_CODE(data[0]);
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001631 dev_dbg(dev, "%s - port_number %d, function %d, info 0x%x\n", __func__,
1632 port_number, function, data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 port = edge_serial->serial->port[port_number];
1634 edge_port = usb_get_serial_port_data(port);
1635 if (!edge_port) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001636 dev_dbg(dev, "%s - edge_port not found\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 return;
1638 }
1639 switch (function) {
1640 case TIUMP_INTERRUPT_CODE_LSR:
Alan Cox2742fd82008-04-29 14:45:15 +01001641 lsr = map_line_status(data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 if (lsr & UMP_UART_LSR_DATA_MASK) {
Alan Cox2742fd82008-04-29 14:45:15 +01001643 /* Save the LSR event for bulk read
1644 completion routine */
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001645 dev_dbg(dev, "%s - LSR Event Port %u LSR Status = %02x\n",
1646 __func__, port_number, lsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 edge_port->lsr_event = 1;
1648 edge_port->lsr_mask = lsr;
1649 } else {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001650 dev_dbg(dev, "%s - ===== Port %d LSR Status = %02x ======\n",
1651 __func__, port_number, lsr);
Alan Cox2742fd82008-04-29 14:45:15 +01001652 handle_new_lsr(edge_port, 0, lsr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 }
1654 break;
1655
Alan Cox2742fd82008-04-29 14:45:15 +01001656 case TIUMP_INTERRUPT_CODE_MSR: /* MSR */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 /* Copy MSR from UMP */
1658 msr = data[1];
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001659 dev_dbg(dev, "%s - ===== Port %u MSR Status = %02x ======\n",
1660 __func__, port_number, msr);
Alan Cox2742fd82008-04-29 14:45:15 +01001661 handle_new_msr(edge_port, msr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 break;
1663
1664 default:
Alan Cox2742fd82008-04-29 14:45:15 +01001665 dev_err(&urb->dev->dev,
1666 "%s - Unknown Interrupt code from UMP %x\n",
1667 __func__, data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 break;
Alan Cox2742fd82008-04-29 14:45:15 +01001669
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 }
1671
1672exit:
Alan Cox2742fd82008-04-29 14:45:15 +01001673 retval = usb_submit_urb(urb, GFP_ATOMIC);
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001674 if (retval)
Alan Cox2742fd82008-04-29 14:45:15 +01001675 dev_err(&urb->dev->dev,
1676 "%s - usb_submit_urb failed with result %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001677 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678}
1679
Alan Cox2742fd82008-04-29 14:45:15 +01001680static void edge_bulk_in_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681{
Ming Leicdc97792008-02-24 18:41:47 +08001682 struct edgeport_port *edge_port = urb->context;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001683 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 unsigned char *data = urb->transfer_buffer;
1685 struct tty_struct *tty;
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001686 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 int port_number;
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001688 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001690 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 case 0:
1692 /* success */
1693 break;
1694 case -ECONNRESET:
1695 case -ENOENT:
1696 case -ESHUTDOWN:
1697 /* this urb is terminated, clean up */
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001698 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 return;
1700 default:
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001701 dev_err(&urb->dev->dev, "%s - nonzero read bulk status received: %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 }
1703
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001704 if (status == -EPIPE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 goto exit;
1706
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001707 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001708 dev_err(&urb->dev->dev, "%s - stopping read!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 return;
1710 }
1711
1712 port_number = edge_port->port->number - edge_port->port->serial->minor;
1713
1714 if (edge_port->lsr_event) {
1715 edge_port->lsr_event = 0;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001716 dev_dbg(dev, "%s ===== Port %u LSR Status = %02x, Data = %02x ======\n",
1717 __func__, port_number, edge_port->lsr_mask, *data);
Alan Cox2742fd82008-04-29 14:45:15 +01001718 handle_new_lsr(edge_port, 1, edge_port->lsr_mask, *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 /* Adjust buffer length/pointer */
1720 --urb->actual_length;
1721 ++data;
1722 }
1723
Alan Cox4a90f092008-10-13 10:39:46 +01001724 tty = tty_port_tty_get(&edge_port->port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 if (tty && urb->actual_length) {
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001726 usb_serial_debug_data(dev, __func__, urb->actual_length, data);
Alan Cox2742fd82008-04-29 14:45:15 +01001727 if (edge_port->close_pending)
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001728 dev_dbg(dev, "%s - close pending, dropping data on the floor\n",
Alan Cox2742fd82008-04-29 14:45:15 +01001729 __func__);
1730 else
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001731 edge_tty_recv(dev, tty, data, urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 edge_port->icount.rx += urb->actual_length;
1733 }
Alan Cox4a90f092008-10-13 10:39:46 +01001734 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
1736exit:
1737 /* continue read unless stopped */
1738 spin_lock(&edge_port->ep_lock);
Johan Hovold58330412011-11-06 19:06:28 +01001739 if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001740 retval = usb_submit_urb(urb, GFP_ATOMIC);
Johan Hovold58330412011-11-06 19:06:28 +01001741 else if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPED;
Johan Hovold58330412011-11-06 19:06:28 +01001743
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 spin_unlock(&edge_port->ep_lock);
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001745 if (retval)
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001746 dev_err(dev, "%s - usb_submit_urb failed with result %d\n", __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747}
1748
Alan Cox2742fd82008-04-29 14:45:15 +01001749static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
1750 unsigned char *data, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751{
Alan Cox2742fd82008-04-29 14:45:15 +01001752 int queued;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Alan Cox2742fd82008-04-29 14:45:15 +01001754 queued = tty_insert_flip_string(tty, data, length);
1755 if (queued < length)
1756 dev_err(dev, "%s - dropping data, %d bytes lost\n",
1757 __func__, length - queued);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 tty_flip_buffer_push(tty);
1759}
1760
Alan Cox2742fd82008-04-29 14:45:15 +01001761static void edge_bulk_out_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762{
Ming Leicdc97792008-02-24 18:41:47 +08001763 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001765 int status = urb->status;
Alan Cox4a90f092008-10-13 10:39:46 +01001766 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 edge_port->ep_write_urb_in_use = 0;
1769
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001770 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 case 0:
1772 /* success */
1773 break;
1774 case -ECONNRESET:
1775 case -ENOENT:
1776 case -ESHUTDOWN:
1777 /* this urb is terminated, clean up */
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001778 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001779 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 return;
1781 default:
Johan Hovold22a416c2012-02-10 13:20:51 +01001782 dev_err_console(port, "%s - nonzero write bulk status "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001783 "received: %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 }
1785
1786 /* send any buffered data */
Alan Cox4a90f092008-10-13 10:39:46 +01001787 tty = tty_port_tty_get(&port->port);
1788 edge_send(tty);
1789 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790}
1791
Alan Coxa509a7e2009-09-19 13:13:26 -07001792static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793{
1794 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1795 struct edgeport_serial *edge_serial;
1796 struct usb_device *dev;
1797 struct urb *urb;
1798 int port_number;
1799 int status;
1800 u16 open_settings;
1801 u8 transaction_timeout;
1802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 if (edge_port == NULL)
1804 return -ENODEV;
1805
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 port_number = port->number - port->serial->minor;
1807 switch (port_number) {
Alan Cox2742fd82008-04-29 14:45:15 +01001808 case 0:
1809 edge_port->uart_base = UMPMEM_BASE_UART1;
1810 edge_port->dma_address = UMPD_OEDB1_ADDRESS;
1811 break;
1812 case 1:
1813 edge_port->uart_base = UMPMEM_BASE_UART2;
1814 edge_port->dma_address = UMPD_OEDB2_ADDRESS;
1815 break;
1816 default:
1817 dev_err(&port->dev, "Unknown port number!!!\n");
1818 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 }
1820
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001821 dev_dbg(&port->dev, "%s - port_number = %d, uart_base = %04x, dma_address = %04x\n",
1822 __func__, port_number, edge_port->uart_base, edge_port->dma_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
1824 dev = port->serial->dev;
1825
Alan Cox2742fd82008-04-29 14:45:15 +01001826 memset(&(edge_port->icount), 0x00, sizeof(edge_port->icount));
1827 init_waitqueue_head(&edge_port->delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
1829 /* turn off loopback */
Alan Cox2742fd82008-04-29 14:45:15 +01001830 status = ti_do_config(edge_port, UMPC_SET_CLR_LOOPBACK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001832 dev_err(&port->dev,
1833 "%s - cannot send clear loopback command, %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001834 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 return status;
1836 }
Alan Cox2742fd82008-04-29 14:45:15 +01001837
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 /* set up the port settings */
Alan Cox95da3102008-07-22 11:09:07 +01001839 if (tty)
Alan Cox4a90f092008-10-13 10:39:46 +01001840 edge_set_termios(tty, port, tty->termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
1842 /* open up the port */
1843
1844 /* milliseconds to timeout for DMA transfer */
1845 transaction_timeout = 2;
1846
Alan Cox2742fd82008-04-29 14:45:15 +01001847 edge_port->ump_read_timeout =
1848 max(20, ((transaction_timeout * 3) / 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
Alan Cox2742fd82008-04-29 14:45:15 +01001850 /* milliseconds to timeout for DMA transfer */
1851 open_settings = (u8)(UMP_DMA_MODE_CONTINOUS |
1852 UMP_PIPE_TRANS_TIMEOUT_ENA |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 (transaction_timeout << 2));
1854
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001855 dev_dbg(&port->dev, "%s - Sending UMPC_OPEN_PORT\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
1857 /* Tell TI to open and start the port */
Alan Cox2742fd82008-04-29 14:45:15 +01001858 status = send_cmd(dev, UMPC_OPEN_PORT,
1859 (u8)(UMPM_UART1_PORT + port_number), open_settings, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001861 dev_err(&port->dev, "%s - cannot send open command, %d\n",
1862 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 return status;
1864 }
1865
1866 /* Start the DMA? */
Alan Cox2742fd82008-04-29 14:45:15 +01001867 status = send_cmd(dev, UMPC_START_PORT,
1868 (u8)(UMPM_UART1_PORT + port_number), 0, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001870 dev_err(&port->dev, "%s - cannot send start DMA command, %d\n",
1871 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 return status;
1873 }
1874
1875 /* Clear TX and RX buffers in UMP */
Alan Cox2742fd82008-04-29 14:45:15 +01001876 status = purge_port(port, UMP_PORT_DIR_OUT | UMP_PORT_DIR_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001878 dev_err(&port->dev,
1879 "%s - cannot send clear buffers command, %d\n",
1880 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 return status;
1882 }
1883
1884 /* Read Initial MSR */
Alan Cox2742fd82008-04-29 14:45:15 +01001885 status = ti_vread_sync(dev, UMPC_READ_MSR, 0,
1886 (__u16)(UMPM_UART1_PORT + port_number),
1887 &edge_port->shadow_msr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001889 dev_err(&port->dev, "%s - cannot send read MSR command, %d\n",
1890 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 return status;
1892 }
1893
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001894 dev_dbg(&port->dev, "ShadowMSR 0x%X\n", edge_port->shadow_msr);
Alan Cox2742fd82008-04-29 14:45:15 +01001895
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 /* Set Initial MCR */
1897 edge_port->shadow_mcr = MCR_RTS | MCR_DTR;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001898 dev_dbg(&port->dev, "ShadowMCR 0x%X\n", edge_port->shadow_mcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
1900 edge_serial = edge_port->edge_serial;
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01001901 if (mutex_lock_interruptible(&edge_serial->es_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 return -ERESTARTSYS;
1903 if (edge_serial->num_ports_open == 0) {
Alan Cox2742fd82008-04-29 14:45:15 +01001904 /* we are the first port to open, post the interrupt urb */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 urb = edge_serial->serial->port[0]->interrupt_in_urb;
1906 if (!urb) {
Alan Cox2742fd82008-04-29 14:45:15 +01001907 dev_err(&port->dev,
1908 "%s - no interrupt urb present, exiting\n",
1909 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 status = -EINVAL;
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01001911 goto release_es_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 urb->context = edge_serial;
Alan Cox2742fd82008-04-29 14:45:15 +01001914 status = usb_submit_urb(urb, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001916 dev_err(&port->dev,
1917 "%s - usb_submit_urb failed with value %d\n",
1918 __func__, status);
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01001919 goto release_es_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 }
1921 }
1922
1923 /*
1924 * reset the data toggle on the bulk endpoints to work around bug in
1925 * host controllers where things get out of sync some times
1926 */
Alan Cox2742fd82008-04-29 14:45:15 +01001927 usb_clear_halt(dev, port->write_urb->pipe);
1928 usb_clear_halt(dev, port->read_urb->pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
1930 /* start up our bulk read urb */
1931 urb = port->read_urb;
1932 if (!urb) {
Alan Cox2742fd82008-04-29 14:45:15 +01001933 dev_err(&port->dev, "%s - no read urb present, exiting\n",
1934 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 status = -EINVAL;
1936 goto unlink_int_urb;
1937 }
1938 edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 urb->context = edge_port;
Alan Cox2742fd82008-04-29 14:45:15 +01001940 status = usb_submit_urb(urb, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001942 dev_err(&port->dev,
1943 "%s - read bulk usb_submit_urb failed with value %d\n",
1944 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 goto unlink_int_urb;
1946 }
1947
1948 ++edge_serial->num_ports_open;
1949
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01001950 goto release_es_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
1952unlink_int_urb:
1953 if (edge_port->edge_serial->num_ports_open == 0)
1954 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01001955release_es_lock:
1956 mutex_unlock(&edge_serial->es_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 return status;
1958}
1959
Alan Cox335f8512009-06-11 12:26:29 +01001960static void edge_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961{
1962 struct edgeport_serial *edge_serial;
1963 struct edgeport_port *edge_port;
Johan Hovoldaf581052012-03-26 20:31:38 +02001964 struct usb_serial *serial = port->serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 int port_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 edge_serial = usb_get_serial_data(port->serial);
1968 edge_port = usb_get_serial_port_data(port);
Alan Cox2742fd82008-04-29 14:45:15 +01001969 if (edge_serial == NULL || edge_port == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 return;
Alan Cox2742fd82008-04-29 14:45:15 +01001971
1972 /* The bulkreadcompletion routine will check
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 * this flag and dump add read data */
1974 edge_port->close_pending = 1;
1975
1976 /* chase the port close and flush */
Alan Cox2742fd82008-04-29 14:45:15 +01001977 chase_port(edge_port, (HZ * closing_wait) / 100, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
1979 usb_kill_urb(port->read_urb);
1980 usb_kill_urb(port->write_urb);
1981 edge_port->ep_write_urb_in_use = 0;
1982
1983 /* assuming we can still talk to the device,
1984 * send a close port command to it */
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07001985 dev_dbg(&port->dev, "%s - send umpc_close_port\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 port_number = port->number - port->serial->minor;
Johan Hovoldaf581052012-03-26 20:31:38 +02001987
1988 mutex_lock(&serial->disc_mutex);
1989 if (!serial->disconnected) {
1990 send_cmd(serial->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 UMPC_CLOSE_PORT,
1992 (__u8)(UMPM_UART1_PORT + port_number),
1993 0,
1994 NULL,
1995 0);
Johan Hovoldaf581052012-03-26 20:31:38 +02001996 }
1997 mutex_unlock(&serial->disc_mutex);
1998
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01001999 mutex_lock(&edge_serial->es_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 --edge_port->edge_serial->num_ports_open;
2001 if (edge_port->edge_serial->num_ports_open <= 0) {
2002 /* last port is now closed, let's shut down our interrupt urb */
2003 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
2004 edge_port->edge_serial->num_ports_open = 0;
2005 }
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01002006 mutex_unlock(&edge_serial->es_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 edge_port->close_pending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008}
2009
Alan Cox95da3102008-07-22 11:09:07 +01002010static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
2011 const unsigned char *data, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012{
2013 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 if (count == 0) {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002016 dev_dbg(&port->dev, "%s - write request of 0 bytes\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 return 0;
2018 }
2019
2020 if (edge_port == NULL)
2021 return -ENODEV;
2022 if (edge_port->close_pending == 1)
2023 return -ENODEV;
2024
Johan Hovoldd733cec2010-05-19 00:01:37 +02002025 count = kfifo_in_locked(&edge_port->write_fifo, data, count,
2026 &edge_port->ep_lock);
Alan Cox95da3102008-07-22 11:09:07 +01002027 edge_send(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
2029 return count;
2030}
2031
Alan Cox95da3102008-07-22 11:09:07 +01002032static void edge_send(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033{
Alan Cox95da3102008-07-22 11:09:07 +01002034 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 int count, result;
2036 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 unsigned long flags;
2038
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 spin_lock_irqsave(&edge_port->ep_lock, flags);
2040
2041 if (edge_port->ep_write_urb_in_use) {
2042 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2043 return;
2044 }
2045
Johan Hovoldd733cec2010-05-19 00:01:37 +02002046 count = kfifo_out(&edge_port->write_fifo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 port->write_urb->transfer_buffer,
2048 port->bulk_out_size);
2049
2050 if (count == 0) {
2051 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2052 return;
2053 }
2054
2055 edge_port->ep_write_urb_in_use = 1;
2056
2057 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2058
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01002059 usb_serial_debug_data(&port->dev, __func__, count, port->write_urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
2061 /* set up our urb */
Johan Hovoldfd119612011-11-06 19:06:30 +01002062 port->write_urb->transfer_buffer_length = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
2064 /* send the data out the bulk port */
2065 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
2066 if (result) {
Johan Hovold22a416c2012-02-10 13:20:51 +01002067 dev_err_console(port,
Alan Cox2742fd82008-04-29 14:45:15 +01002068 "%s - failed submitting write urb, error %d\n",
2069 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 edge_port->ep_write_urb_in_use = 0;
Alan Cox2742fd82008-04-29 14:45:15 +01002071 /* TODO: reschedule edge_send */
2072 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 edge_port->icount.tx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
2075 /* wakeup any process waiting for writes to complete */
2076 /* there is now more room in the buffer for new writes */
Alan Cox2742fd82008-04-29 14:45:15 +01002077 if (tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079}
2080
Alan Cox95da3102008-07-22 11:09:07 +01002081static int edge_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082{
Alan Cox95da3102008-07-22 11:09:07 +01002083 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2085 int room = 0;
2086 unsigned long flags;
2087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 if (edge_port == NULL)
Alan Cox2742fd82008-04-29 14:45:15 +01002089 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 if (edge_port->close_pending == 1)
Alan Cox2742fd82008-04-29 14:45:15 +01002091 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
2093 spin_lock_irqsave(&edge_port->ep_lock, flags);
Johan Hovoldd733cec2010-05-19 00:01:37 +02002094 room = kfifo_avail(&edge_port->write_fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2096
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002097 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 return room;
2099}
2100
Alan Cox95da3102008-07-22 11:09:07 +01002101static int edge_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102{
Alan Cox95da3102008-07-22 11:09:07 +01002103 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2105 int chars = 0;
2106 unsigned long flags;
2107
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 if (edge_port == NULL)
Alan Cox2742fd82008-04-29 14:45:15 +01002109 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 if (edge_port->close_pending == 1)
Alan Cox2742fd82008-04-29 14:45:15 +01002111 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
2113 spin_lock_irqsave(&edge_port->ep_lock, flags);
Johan Hovoldd733cec2010-05-19 00:01:37 +02002114 chars = kfifo_len(&edge_port->write_fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2116
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002117 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 return chars;
2119}
2120
Alan Cox95da3102008-07-22 11:09:07 +01002121static void edge_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122{
Alan Cox95da3102008-07-22 11:09:07 +01002123 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 int status;
2126
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 if (edge_port == NULL)
2128 return;
2129
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 /* if we are implementing XON/XOFF, send the stop character */
2131 if (I_IXOFF(tty)) {
2132 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01002133 status = edge_write(tty, port, &stop_char, 1);
2134 if (status <= 0) {
2135 dev_err(&port->dev, "%s - failed to write stop character, %d\n", __func__, status);
2136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 }
2138
2139 /* if we are implementing RTS/CTS, stop reads */
2140 /* and the Edgeport will clear the RTS line */
2141 if (C_CRTSCTS(tty))
2142 stop_read(edge_port);
2143
2144}
2145
Alan Cox95da3102008-07-22 11:09:07 +01002146static void edge_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147{
Alan Cox95da3102008-07-22 11:09:07 +01002148 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 int status;
2151
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 if (edge_port == NULL)
2153 return;
2154
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 /* if we are implementing XON/XOFF, send the start character */
2156 if (I_IXOFF(tty)) {
2157 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01002158 status = edge_write(tty, port, &start_char, 1);
2159 if (status <= 0) {
2160 dev_err(&port->dev, "%s - failed to write start character, %d\n", __func__, status);
2161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 /* if we are implementing RTS/CTS, restart reads */
2164 /* are the Edgeport will assert the RTS line */
2165 if (C_CRTSCTS(tty)) {
2166 status = restart_read(edge_port);
2167 if (status)
Alan Cox2742fd82008-04-29 14:45:15 +01002168 dev_err(&port->dev,
2169 "%s - read bulk usb_submit_urb failed: %d\n",
2170 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 }
2172
2173}
2174
2175static void stop_read(struct edgeport_port *edge_port)
2176{
2177 unsigned long flags;
2178
2179 spin_lock_irqsave(&edge_port->ep_lock, flags);
2180
2181 if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
2182 edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPING;
2183 edge_port->shadow_mcr &= ~MCR_RTS;
2184
2185 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2186}
2187
2188static int restart_read(struct edgeport_port *edge_port)
2189{
2190 struct urb *urb;
2191 int status = 0;
2192 unsigned long flags;
2193
2194 spin_lock_irqsave(&edge_port->ep_lock, flags);
2195
2196 if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPED) {
2197 urb = edge_port->port->read_urb;
Oliver Neukumefdff602007-06-05 10:50:48 +02002198 status = usb_submit_urb(urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 }
2200 edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
2201 edge_port->shadow_mcr |= MCR_RTS;
2202
2203 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2204
2205 return status;
2206}
2207
Alan Cox95da3102008-07-22 11:09:07 +01002208static void change_port_settings(struct tty_struct *tty,
2209 struct edgeport_port *edge_port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210{
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002211 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 struct ump_uart_config *config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 int baud;
2214 unsigned cflag;
2215 int status;
Alan Cox2742fd82008-04-29 14:45:15 +01002216 int port_number = edge_port->port->number -
2217 edge_port->port->serial->minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002219 dev_dbg(dev, "%s - port %d\n", __func__, edge_port->port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
Alan Cox95da3102008-07-22 11:09:07 +01002221 config = kmalloc (sizeof (*config), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 if (!config) {
Alan Cox2742fd82008-04-29 14:45:15 +01002223 *tty->termios = *old_termios;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002224 dev_err(dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 return;
2226 }
2227
2228 cflag = tty->termios->c_cflag;
2229
2230 config->wFlags = 0;
2231
2232 /* These flags must be set */
2233 config->wFlags |= UMP_MASK_UART_FLAGS_RECEIVE_MS_INT;
2234 config->wFlags |= UMP_MASK_UART_FLAGS_AUTO_START_ON_ERR;
2235 config->bUartMode = (__u8)(edge_port->bUartMode);
2236
2237 switch (cflag & CSIZE) {
Alan Cox2742fd82008-04-29 14:45:15 +01002238 case CS5:
2239 config->bDataBits = UMP_UART_CHAR5BITS;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002240 dev_dbg(dev, "%s - data bits = 5\n", __func__);
Alan Cox2742fd82008-04-29 14:45:15 +01002241 break;
2242 case CS6:
2243 config->bDataBits = UMP_UART_CHAR6BITS;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002244 dev_dbg(dev, "%s - data bits = 6\n", __func__);
Alan Cox2742fd82008-04-29 14:45:15 +01002245 break;
2246 case CS7:
2247 config->bDataBits = UMP_UART_CHAR7BITS;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002248 dev_dbg(dev, "%s - data bits = 7\n", __func__);
Alan Cox2742fd82008-04-29 14:45:15 +01002249 break;
2250 default:
2251 case CS8:
2252 config->bDataBits = UMP_UART_CHAR8BITS;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002253 dev_dbg(dev, "%s - data bits = 8\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 break;
2255 }
2256
2257 if (cflag & PARENB) {
2258 if (cflag & PARODD) {
2259 config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
2260 config->bParity = UMP_UART_ODDPARITY;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002261 dev_dbg(dev, "%s - parity = odd\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 } else {
2263 config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
2264 config->bParity = UMP_UART_EVENPARITY;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002265 dev_dbg(dev, "%s - parity = even\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 }
2267 } else {
Alan Cox2742fd82008-04-29 14:45:15 +01002268 config->bParity = UMP_UART_NOPARITY;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002269 dev_dbg(dev, "%s - parity = none\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 }
2271
2272 if (cflag & CSTOPB) {
2273 config->bStopBits = UMP_UART_STOPBIT2;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002274 dev_dbg(dev, "%s - stop bits = 2\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 } else {
2276 config->bStopBits = UMP_UART_STOPBIT1;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002277 dev_dbg(dev, "%s - stop bits = 1\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 }
2279
2280 /* figure out the flow control settings */
2281 if (cflag & CRTSCTS) {
2282 config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X_CTS_FLOW;
2283 config->wFlags |= UMP_MASK_UART_FLAGS_RTS_FLOW;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002284 dev_dbg(dev, "%s - RTS/CTS is enabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 } else {
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002286 dev_dbg(dev, "%s - RTS/CTS is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 tty->hw_stopped = 0;
2288 restart_read(edge_port);
2289 }
2290
Alan Cox2742fd82008-04-29 14:45:15 +01002291 /* if we are implementing XON/XOFF, set the start and stop
2292 character in the device */
2293 config->cXon = START_CHAR(tty);
2294 config->cXoff = STOP_CHAR(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
Alan Cox2742fd82008-04-29 14:45:15 +01002296 /* if we are implementing INBOUND XON/XOFF */
2297 if (I_IXOFF(tty)) {
2298 config->wFlags |= UMP_MASK_UART_FLAGS_IN_X;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002299 dev_dbg(dev, "%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2300 __func__, config->cXon, config->cXoff);
Alan Cox2742fd82008-04-29 14:45:15 +01002301 } else
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002302 dev_dbg(dev, "%s - INBOUND XON/XOFF is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
Alan Cox2742fd82008-04-29 14:45:15 +01002304 /* if we are implementing OUTBOUND XON/XOFF */
2305 if (I_IXON(tty)) {
2306 config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X;
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002307 dev_dbg(dev, "%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2308 __func__, config->cXon, config->cXoff);
Alan Cox2742fd82008-04-29 14:45:15 +01002309 } else
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002310 dev_dbg(dev, "%s - OUTBOUND XON/XOFF is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
Alan Coxd5f5bcd2008-01-03 16:57:33 +00002312 tty->termios->c_cflag &= ~CMSPAR;
2313
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 /* Round the baud rate */
2315 baud = tty_get_baud_rate(tty);
2316 if (!baud) {
2317 /* pick a default, any default... */
2318 baud = 9600;
Alan Coxd5f5bcd2008-01-03 16:57:33 +00002319 } else
2320 tty_encode_baud_rate(tty, baud, baud);
2321
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 edge_port->baud_rate = baud;
2323 config->wBaudRate = (__u16)((461550L + baud/2) / baud);
2324
Alan Coxd5f5bcd2008-01-03 16:57:33 +00002325 /* FIXME: Recompute actual baud from divisor here */
2326
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002327 dev_dbg(dev, "%s - baud rate = %d, wBaudRate = %d\n", __func__, baud, config->wBaudRate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002329 dev_dbg(dev, "wBaudRate: %d\n", (int)(461550L / config->wBaudRate));
2330 dev_dbg(dev, "wFlags: 0x%x\n", config->wFlags);
2331 dev_dbg(dev, "bDataBits: %d\n", config->bDataBits);
2332 dev_dbg(dev, "bParity: %d\n", config->bParity);
2333 dev_dbg(dev, "bStopBits: %d\n", config->bStopBits);
2334 dev_dbg(dev, "cXon: %d\n", config->cXon);
2335 dev_dbg(dev, "cXoff: %d\n", config->cXoff);
2336 dev_dbg(dev, "bUartMode: %d\n", config->bUartMode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
2338 /* move the word values into big endian mode */
Alan Cox2742fd82008-04-29 14:45:15 +01002339 cpu_to_be16s(&config->wFlags);
2340 cpu_to_be16s(&config->wBaudRate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
Alan Cox2742fd82008-04-29 14:45:15 +01002342 status = send_cmd(edge_port->port->serial->dev, UMPC_SET_CONFIG,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 (__u8)(UMPM_UART1_PORT + port_number),
Alan Cox2742fd82008-04-29 14:45:15 +01002344 0, (__u8 *)config, sizeof(*config));
2345 if (status)
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002346 dev_dbg(dev, "%s - error %d when trying to write config to device\n",
2347 __func__, status);
Alan Cox2742fd82008-04-29 14:45:15 +01002348 kfree(config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349}
2350
Alan Cox2e0ddd62008-07-22 11:12:33 +01002351static void edge_set_termios(struct tty_struct *tty,
Alan Cox95da3102008-07-22 11:09:07 +01002352 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353{
2354 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Alan Cox95da3102008-07-22 11:09:07 +01002355 unsigned int cflag;
2356
2357 cflag = tty->termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002359 dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__,
2360 tty->termios->c_cflag, tty->termios->c_iflag);
2361 dev_dbg(&port->dev, "%s - old clfag %08x old iflag %08x\n", __func__,
2362 old_termios->c_cflag, old_termios->c_iflag);
2363 dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
2365 if (edge_port == NULL)
2366 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 /* change the port settings to the new ones specified */
Alan Cox95da3102008-07-22 11:09:07 +01002368 change_port_settings(tty, edge_port, old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369}
2370
Alan Cox20b9d172011-02-14 16:26:50 +00002371static int edge_tiocmset(struct tty_struct *tty,
Alan Cox2742fd82008-04-29 14:45:15 +01002372 unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373{
Alan Cox95da3102008-07-22 11:09:07 +01002374 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2376 unsigned int mcr;
Alan Cox3d71fe02008-02-20 21:38:32 +00002377 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
Alan Cox3d71fe02008-02-20 21:38:32 +00002379 spin_lock_irqsave(&edge_port->ep_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 mcr = edge_port->shadow_mcr;
2381 if (set & TIOCM_RTS)
2382 mcr |= MCR_RTS;
2383 if (set & TIOCM_DTR)
2384 mcr |= MCR_DTR;
2385 if (set & TIOCM_LOOP)
2386 mcr |= MCR_LOOPBACK;
2387
2388 if (clear & TIOCM_RTS)
2389 mcr &= ~MCR_RTS;
2390 if (clear & TIOCM_DTR)
2391 mcr &= ~MCR_DTR;
2392 if (clear & TIOCM_LOOP)
2393 mcr &= ~MCR_LOOPBACK;
2394
2395 edge_port->shadow_mcr = mcr;
Alan Cox3d71fe02008-02-20 21:38:32 +00002396 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
Alan Cox2742fd82008-04-29 14:45:15 +01002398 restore_mcr(edge_port, mcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 return 0;
2400}
2401
Alan Cox60b33c12011-02-14 16:26:14 +00002402static int edge_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403{
Alan Cox95da3102008-07-22 11:09:07 +01002404 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2406 unsigned int result = 0;
2407 unsigned int msr;
2408 unsigned int mcr;
Alan Cox3d71fe02008-02-20 21:38:32 +00002409 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
Alan Cox3d71fe02008-02-20 21:38:32 +00002411 spin_lock_irqsave(&edge_port->ep_lock, flags);
2412
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 msr = edge_port->shadow_msr;
2414 mcr = edge_port->shadow_mcr;
2415 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
2416 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
2417 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
2418 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
2419 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
2420 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
2421
2422
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002423 dev_dbg(&port->dev, "%s -- %x\n", __func__, result);
Alan Cox3d71fe02008-02-20 21:38:32 +00002424 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
2426 return result;
2427}
2428
Alan Cox0bca1b92010-09-16 18:21:40 +01002429static int edge_get_icount(struct tty_struct *tty,
2430 struct serial_icounter_struct *icount)
2431{
2432 struct usb_serial_port *port = tty->driver_data;
2433 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2434 struct async_icount *ic = &edge_port->icount;
2435
2436 icount->cts = ic->cts;
2437 icount->dsr = ic->dsr;
2438 icount->rng = ic->rng;
2439 icount->dcd = ic->dcd;
2440 icount->tx = ic->tx;
2441 icount->rx = ic->rx;
2442 icount->frame = ic->frame;
2443 icount->parity = ic->parity;
2444 icount->overrun = ic->overrun;
2445 icount->brk = ic->brk;
2446 icount->buf_overrun = ic->buf_overrun;
2447 return 0;
2448}
2449
Alan Cox2742fd82008-04-29 14:45:15 +01002450static int get_serial_info(struct edgeport_port *edge_port,
2451 struct serial_struct __user *retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452{
2453 struct serial_struct tmp;
2454
2455 if (!retinfo)
2456 return -EFAULT;
2457
2458 memset(&tmp, 0, sizeof(tmp));
2459
2460 tmp.type = PORT_16550A;
2461 tmp.line = edge_port->port->serial->minor;
2462 tmp.port = edge_port->port->number;
2463 tmp.irq = 0;
2464 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2465 tmp.xmit_fifo_size = edge_port->port->bulk_out_size;
2466 tmp.baud_base = 9600;
2467 tmp.close_delay = 5*HZ;
2468 tmp.closing_wait = closing_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469
2470 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2471 return -EFAULT;
2472 return 0;
2473}
2474
Alan Cox00a0d0d2011-02-14 16:27:06 +00002475static int edge_ioctl(struct tty_struct *tty,
Alan Cox2742fd82008-04-29 14:45:15 +01002476 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477{
Alan Cox95da3102008-07-22 11:09:07 +01002478 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2480 struct async_icount cnow;
2481 struct async_icount cprev;
2482
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002483 dev_dbg(&port->dev, "%s - port %d, cmd = 0x%x\n", __func__, port->number, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
2485 switch (cmd) {
Alan Cox2742fd82008-04-29 14:45:15 +01002486 case TIOCGSERIAL:
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002487 dev_dbg(&port->dev, "%s - TIOCGSERIAL\n", __func__);
Alan Cox2742fd82008-04-29 14:45:15 +01002488 return get_serial_info(edge_port,
2489 (struct serial_struct __user *) arg);
2490 case TIOCMIWAIT:
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002491 dev_dbg(&port->dev, "%s - TIOCMIWAIT\n", __func__);
Alan Cox2742fd82008-04-29 14:45:15 +01002492 cprev = edge_port->icount;
2493 while (1) {
2494 interruptible_sleep_on(&edge_port->delta_msr_wait);
2495 /* see if a signal did it */
2496 if (signal_pending(current))
2497 return -ERESTARTSYS;
2498 cnow = edge_port->icount;
2499 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2500 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2501 return -EIO; /* no change => error */
2502 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2503 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2504 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2505 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2506 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 }
Alan Cox2742fd82008-04-29 14:45:15 +01002508 cprev = cnow;
2509 }
2510 /* not reached */
2511 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 return -ENOIOCTLCMD;
2514}
2515
Alan Cox95da3102008-07-22 11:09:07 +01002516static void edge_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517{
Alan Cox95da3102008-07-22 11:09:07 +01002518 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2520 int status;
Alan Cox2742fd82008-04-29 14:45:15 +01002521 int bv = 0; /* Off */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 /* chase the port close */
Alan Cox2742fd82008-04-29 14:45:15 +01002524 chase_port(edge_port, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
Alan Cox95da3102008-07-22 11:09:07 +01002526 if (break_state == -1)
Alan Cox2742fd82008-04-29 14:45:15 +01002527 bv = 1; /* On */
2528 status = ti_do_config(edge_port, UMPC_SET_CLR_BREAK, bv);
2529 if (status)
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002530 dev_dbg(&port->dev, "%s - error %d sending break set/clear command.\n",
2531 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532}
2533
Alan Cox2742fd82008-04-29 14:45:15 +01002534static int edge_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535{
2536 struct edgeport_serial *edge_serial;
2537 struct edgeport_port *edge_port;
2538 struct usb_device *dev;
2539 int status;
2540 int i;
2541
2542 dev = serial->dev;
2543
2544 /* create our private serial structure */
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +01002545 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 if (edge_serial == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002547 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 return -ENOMEM;
2549 }
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01002550 mutex_init(&edge_serial->es_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 edge_serial->serial = serial;
2552 usb_set_serial_data(serial, edge_serial);
2553
Alan Cox2742fd82008-04-29 14:45:15 +01002554 status = download_fw(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01002556 kfree(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 return status;
2558 }
2559
2560 /* set up our port private structures */
2561 for (i = 0; i < serial->num_ports; ++i) {
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +01002562 edge_port = kzalloc(sizeof(struct edgeport_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 if (edge_port == NULL) {
Alan Cox2742fd82008-04-29 14:45:15 +01002564 dev_err(&serial->dev->dev, "%s - Out of memory\n",
2565 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 goto cleanup;
2567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 spin_lock_init(&edge_port->ep_lock);
Johan Hovoldd733cec2010-05-19 00:01:37 +02002569 if (kfifo_alloc(&edge_port->write_fifo, EDGE_OUT_BUF_SIZE,
2570 GFP_KERNEL)) {
Alan Cox2742fd82008-04-29 14:45:15 +01002571 dev_err(&serial->dev->dev, "%s - Out of memory\n",
2572 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 kfree(edge_port);
2574 goto cleanup;
2575 }
2576 edge_port->port = serial->port[i];
2577 edge_port->edge_serial = edge_serial;
2578 usb_set_serial_port_data(serial->port[i], edge_port);
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002579 edge_port->bUartMode = default_uart_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 }
Alan Cox2742fd82008-04-29 14:45:15 +01002581
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 return 0;
2583
2584cleanup:
Alan Cox2742fd82008-04-29 14:45:15 +01002585 for (--i; i >= 0; --i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 edge_port = usb_get_serial_port_data(serial->port[i]);
Johan Hovoldd733cec2010-05-19 00:01:37 +02002587 kfifo_free(&edge_port->write_fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 kfree(edge_port);
2589 usb_set_serial_port_data(serial->port[i], NULL);
2590 }
Alan Cox2742fd82008-04-29 14:45:15 +01002591 kfree(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 usb_set_serial_data(serial, NULL);
2593 return -ENOMEM;
2594}
2595
Alan Sternf9c99bb2009-06-02 11:53:55 -04002596static void edge_disconnect(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597{
Alan Sternf9c99bb2009-06-02 11:53:55 -04002598}
2599
2600static void edge_release(struct usb_serial *serial)
2601{
2602 int i;
2603 struct edgeport_port *edge_port;
2604
Alan Sternf9c99bb2009-06-02 11:53:55 -04002605 for (i = 0; i < serial->num_ports; ++i) {
2606 edge_port = usb_get_serial_port_data(serial->port[i]);
Johan Hovoldd733cec2010-05-19 00:01:37 +02002607 kfifo_free(&edge_port->write_fifo);
Jesper Juhl0d46c002007-07-16 22:17:25 +02002608 kfree(edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 }
Jesper Juhl0d46c002007-07-16 22:17:25 +02002610 kfree(usb_get_serial_data(serial));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611}
2612
2613
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002614/* Sysfs Attributes */
2615
2616static ssize_t show_uart_mode(struct device *dev,
2617 struct device_attribute *attr, char *buf)
2618{
2619 struct usb_serial_port *port = to_usb_serial_port(dev);
2620 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2621
2622 return sprintf(buf, "%d\n", edge_port->bUartMode);
2623}
2624
2625static ssize_t store_uart_mode(struct device *dev,
2626 struct device_attribute *attr, const char *valbuf, size_t count)
2627{
2628 struct usb_serial_port *port = to_usb_serial_port(dev);
2629 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2630 unsigned int v = simple_strtoul(valbuf, NULL, 0);
2631
Greg Kroah-Hartman67e6da72012-09-14 16:58:58 -07002632 dev_dbg(dev, "%s: setting uart_mode = %d\n", __func__, v);
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002633
2634 if (v < 256)
2635 edge_port->bUartMode = v;
2636 else
Harvey Harrison441b62c2008-03-03 16:08:34 -08002637 dev_err(dev, "%s - uart_mode %d is invalid\n", __func__, v);
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002638
2639 return count;
2640}
2641
Alan Cox2742fd82008-04-29 14:45:15 +01002642static DEVICE_ATTR(uart_mode, S_IWUSR | S_IRUGO, show_uart_mode,
2643 store_uart_mode);
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002644
2645static int edge_create_sysfs_attrs(struct usb_serial_port *port)
2646{
2647 return device_create_file(&port->dev, &dev_attr_uart_mode);
2648}
2649
2650static int edge_remove_sysfs_attrs(struct usb_serial_port *port)
2651{
2652 device_remove_file(&port->dev, &dev_attr_uart_mode);
2653 return 0;
2654}
2655
2656
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -07002657static struct usb_serial_driver edgeport_1port_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07002658 .driver = {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07002659 .owner = THIS_MODULE,
2660 .name = "edgeport_ti_1",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07002661 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07002662 .description = "Edgeport TI 1 port adapter",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 .id_table = edgeport_1port_id_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 .num_ports = 1,
2665 .open = edge_open,
2666 .close = edge_close,
2667 .throttle = edge_throttle,
2668 .unthrottle = edge_unthrottle,
2669 .attach = edge_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04002670 .disconnect = edge_disconnect,
2671 .release = edge_release,
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002672 .port_probe = edge_create_sysfs_attrs,
Eric W. Biederman6d443d82012-01-13 21:32:06 -08002673 .port_remove = edge_remove_sysfs_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 .ioctl = edge_ioctl,
2675 .set_termios = edge_set_termios,
2676 .tiocmget = edge_tiocmget,
2677 .tiocmset = edge_tiocmset,
Alan Cox0bca1b92010-09-16 18:21:40 +01002678 .get_icount = edge_get_icount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 .write = edge_write,
2680 .write_room = edge_write_room,
2681 .chars_in_buffer = edge_chars_in_buffer,
2682 .break_ctl = edge_break,
2683 .read_int_callback = edge_interrupt_callback,
2684 .read_bulk_callback = edge_bulk_in_callback,
2685 .write_bulk_callback = edge_bulk_out_callback,
2686};
2687
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -07002688static struct usb_serial_driver edgeport_2port_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07002689 .driver = {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07002690 .owner = THIS_MODULE,
2691 .name = "edgeport_ti_2",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07002692 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07002693 .description = "Edgeport TI 2 port adapter",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 .id_table = edgeport_2port_id_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 .num_ports = 2,
2696 .open = edge_open,
2697 .close = edge_close,
2698 .throttle = edge_throttle,
2699 .unthrottle = edge_unthrottle,
2700 .attach = edge_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04002701 .disconnect = edge_disconnect,
2702 .release = edge_release,
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002703 .port_probe = edge_create_sysfs_attrs,
Eric W. Biederman6d443d82012-01-13 21:32:06 -08002704 .port_remove = edge_remove_sysfs_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 .ioctl = edge_ioctl,
2706 .set_termios = edge_set_termios,
2707 .tiocmget = edge_tiocmget,
2708 .tiocmset = edge_tiocmset,
2709 .write = edge_write,
2710 .write_room = edge_write_room,
2711 .chars_in_buffer = edge_chars_in_buffer,
2712 .break_ctl = edge_break,
2713 .read_int_callback = edge_interrupt_callback,
2714 .read_bulk_callback = edge_bulk_in_callback,
2715 .write_bulk_callback = edge_bulk_out_callback,
2716};
2717
Alan Stern7dbe2462012-02-23 14:56:57 -05002718static struct usb_serial_driver * const serial_drivers[] = {
2719 &edgeport_1port_device, &edgeport_2port_device, NULL
2720};
2721
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07002722module_usb_serial_driver(serial_drivers, id_table_combined);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724MODULE_AUTHOR(DRIVER_AUTHOR);
2725MODULE_DESCRIPTION(DRIVER_DESC);
2726MODULE_LICENSE("GPL");
Jaswinder Singhd12b2192008-07-04 23:06:09 +05302727MODULE_FIRMWARE("edgeport/down3.bin");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728
2729module_param(debug, bool, S_IRUGO | S_IWUSR);
2730MODULE_PARM_DESC(debug, "Debug enabled or not");
2731
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732module_param(closing_wait, int, S_IRUGO | S_IWUSR);
2733MODULE_PARM_DESC(closing_wait, "Maximum wait for data to drain, in .01 secs");
2734
2735module_param(ignore_cpu_rev, bool, S_IRUGO | S_IWUSR);
Alan Cox2742fd82008-04-29 14:45:15 +01002736MODULE_PARM_DESC(ignore_cpu_rev,
2737 "Ignore the cpu revision when connecting to a device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002739module_param(default_uart_mode, int, S_IRUGO | S_IWUSR);
2740MODULE_PARM_DESC(default_uart_mode, "Default uart_mode, 0=RS232, ...");