blob: 311e4bf46deee7c6f651802eae5a2a5a3c369cf8 [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
200static struct usb_driver io_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 .name = "io_ti",
202 .probe = usb_serial_probe,
203 .disconnect = usb_serial_disconnect,
204 .id_table = id_table_combined,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800205 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206};
207
208
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530209static unsigned char OperationalMajorVersion;
210static unsigned char OperationalMinorVersion;
211static unsigned short OperationalBuildNumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030213static bool debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215static int closing_wait = EDGE_CLOSING_WAIT;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030216static bool ignore_cpu_rev;
Alan Cox2742fd82008-04-29 14:45:15 +0100217static int default_uart_mode; /* RS232 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Alan Cox2742fd82008-04-29 14:45:15 +0100219static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
220 unsigned char *data, int length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222static void stop_read(struct edgeport_port *edge_port);
223static int restart_read(struct edgeport_port *edge_port);
224
Alan Cox95da3102008-07-22 11:09:07 +0100225static void edge_set_termios(struct tty_struct *tty,
226 struct usb_serial_port *port, struct ktermios *old_termios);
227static void edge_send(struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -0400229/* sysfs attributes */
230static int edge_create_sysfs_attrs(struct usb_serial_port *port);
231static int edge_remove_sysfs_attrs(struct usb_serial_port *port);
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Alan Cox2742fd82008-04-29 14:45:15 +0100234static int ti_vread_sync(struct usb_device *dev, __u8 request,
235 __u16 value, __u16 index, u8 *data, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 int status;
238
Alan Cox2742fd82008-04-29 14:45:15 +0100239 status = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request,
240 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
241 value, index, data, size, 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 if (status < 0)
243 return status;
244 if (status != size) {
Alan Cox2742fd82008-04-29 14:45:15 +0100245 dbg("%s - wanted to write %d, but only wrote %d",
246 __func__, size, status);
247 return -ECOMM;
248 }
249 return 0;
250}
251
252static int ti_vsend_sync(struct usb_device *dev, __u8 request,
253 __u16 value, __u16 index, u8 *data, int size)
254{
255 int status;
256
257 status = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
258 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
259 value, index, data, size, 1000);
260 if (status < 0)
261 return status;
262 if (status != size) {
263 dbg("%s - wanted to write %d, but only wrote %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800264 __func__, size, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return -ECOMM;
266 }
267 return 0;
268}
269
Alan Cox2742fd82008-04-29 14:45:15 +0100270static int send_cmd(struct usb_device *dev, __u8 command,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 __u8 moduleid, __u16 value, u8 *data,
272 int size)
273{
Alan Cox2742fd82008-04-29 14:45:15 +0100274 return ti_vsend_sync(dev, command, value, moduleid, data, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
277/* clear tx/rx buffers and fifo in TI UMP */
Alan Cox2742fd82008-04-29 14:45:15 +0100278static int purge_port(struct usb_serial_port *port, __u16 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280 int port_number = port->number - port->serial->minor;
281
Alan Cox2742fd82008-04-29 14:45:15 +0100282 dbg("%s - port %d, mask %x", __func__, port_number, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Alan Cox2742fd82008-04-29 14:45:15 +0100284 return send_cmd(port->serial->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 UMPC_PURGE_PORT,
286 (__u8)(UMPM_UART1_PORT + port_number),
287 mask,
288 NULL,
289 0);
290}
291
292/**
Alan Cox2742fd82008-04-29 14:45:15 +0100293 * read_download_mem - Read edgeport memory from TI chip
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 * @dev: usb device pointer
295 * @start_address: Device CPU address at which to read
296 * @length: Length of above data
297 * @address_type: Can read both XDATA and I2C
298 * @buffer: pointer to input data buffer
299 */
Alan Cox2742fd82008-04-29 14:45:15 +0100300static int read_download_mem(struct usb_device *dev, int start_address,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 int length, __u8 address_type, __u8 *buffer)
302{
303 int status = 0;
304 __u8 read_length;
305 __be16 be_start_address;
Alan Cox2742fd82008-04-29 14:45:15 +0100306
307 dbg("%s - @ %x for %d", __func__, start_address, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 /* Read in blocks of 64 bytes
310 * (TI firmware can't handle more than 64 byte reads)
311 */
312 while (length) {
313 if (length > 64)
Alan Cox2742fd82008-04-29 14:45:15 +0100314 read_length = 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 else
316 read_length = (__u8)length;
317
318 if (read_length > 1) {
Alan Cox2742fd82008-04-29 14:45:15 +0100319 dbg("%s - @ %x for %d", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 start_address, read_length);
321 }
Alan Cox2742fd82008-04-29 14:45:15 +0100322 be_start_address = cpu_to_be16(start_address);
323 status = ti_vread_sync(dev, UMPC_MEMORY_READ,
324 (__u16)address_type,
325 (__force __u16)be_start_address,
326 buffer, read_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +0100329 dbg("%s - ERROR %x", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return status;
331 }
332
Alan Cox2742fd82008-04-29 14:45:15 +0100333 if (read_length > 1)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800334 usb_serial_debug_data(debug, &dev->dev, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 read_length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 /* Update pointers/length */
338 start_address += read_length;
339 buffer += read_length;
340 length -= read_length;
341 }
Alan Cox2742fd82008-04-29 14:45:15 +0100342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return status;
344}
345
Alan Cox2742fd82008-04-29 14:45:15 +0100346static int read_ram(struct usb_device *dev, int start_address,
347 int length, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Alan Cox2742fd82008-04-29 14:45:15 +0100349 return read_download_mem(dev, start_address, length,
350 DTK_ADDR_SPACE_XDATA, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
353/* Read edgeport memory to a given block */
Alan Cox2742fd82008-04-29 14:45:15 +0100354static int read_boot_mem(struct edgeport_serial *serial,
355 int start_address, int length, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
357 int status = 0;
358 int i;
359
Alan Cox2742fd82008-04-29 14:45:15 +0100360 for (i = 0; i < length; i++) {
361 status = ti_vread_sync(serial->serial->dev,
362 UMPC_MEMORY_READ, serial->TI_I2C_Type,
363 (__u16)(start_address+i), &buffer[i], 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +0100365 dbg("%s - ERROR %x", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return status;
367 }
368 }
369
Alan Cox2742fd82008-04-29 14:45:15 +0100370 dbg("%s - start_address = %x, length = %d",
371 __func__, start_address, length);
372 usb_serial_debug_data(debug, &serial->serial->dev->dev,
373 __func__, length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 serial->TiReadI2C = 1;
376
377 return status;
378}
379
380/* Write given block to TI EPROM memory */
Alan Cox2742fd82008-04-29 14:45:15 +0100381static int write_boot_mem(struct edgeport_serial *serial,
382 int start_address, int length, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
384 int status = 0;
385 int i;
Johan Hovolde9305d22009-12-28 23:01:50 +0100386 u8 *temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 /* Must do a read before write */
389 if (!serial->TiReadI2C) {
Johan Hovolde9305d22009-12-28 23:01:50 +0100390 temp = kmalloc(1, GFP_KERNEL);
391 if (!temp) {
392 dev_err(&serial->serial->dev->dev,
393 "%s - out of memory\n", __func__);
394 return -ENOMEM;
395 }
396 status = read_boot_mem(serial, 0, 1, temp);
397 kfree(temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (status)
399 return status;
400 }
401
Alan Cox2742fd82008-04-29 14:45:15 +0100402 for (i = 0; i < length; ++i) {
403 status = ti_vsend_sync(serial->serial->dev,
404 UMPC_MEMORY_WRITE, buffer[i],
405 (__u16)(i + start_address), NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if (status)
407 return status;
408 }
409
Alan Cox2742fd82008-04-29 14:45:15 +0100410 dbg("%s - start_sddr = %x, length = %d",
411 __func__, start_address, length);
412 usb_serial_debug_data(debug, &serial->serial->dev->dev,
413 __func__, length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 return status;
416}
417
418
419/* Write edgeport I2C memory to TI chip */
Alan Cox2742fd82008-04-29 14:45:15 +0100420static int write_i2c_mem(struct edgeport_serial *serial,
421 int start_address, int length, __u8 address_type, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
423 int status = 0;
424 int write_length;
425 __be16 be_start_address;
426
427 /* We can only send a maximum of 1 aligned byte page at a time */
Alan Cox2742fd82008-04-29 14:45:15 +0100428
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300429 /* calculate the number of bytes left in the first page */
Alan Cox2742fd82008-04-29 14:45:15 +0100430 write_length = EPROM_PAGE_SIZE -
431 (start_address & (EPROM_PAGE_SIZE - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 if (write_length > length)
434 write_length = length;
435
Alan Cox2742fd82008-04-29 14:45:15 +0100436 dbg("%s - BytesInFirstPage Addr = %x, length = %d",
437 __func__, start_address, write_length);
438 usb_serial_debug_data(debug, &serial->serial->dev->dev,
439 __func__, write_length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 /* Write first page */
Alan Cox2742fd82008-04-29 14:45:15 +0100442 be_start_address = cpu_to_be16(start_address);
443 status = ti_vsend_sync(serial->serial->dev,
444 UMPC_MEMORY_WRITE, (__u16)address_type,
445 (__force __u16)be_start_address,
446 buffer, write_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +0100448 dbg("%s - ERROR %d", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return status;
450 }
451
452 length -= write_length;
453 start_address += write_length;
454 buffer += write_length;
455
Alan Cox2742fd82008-04-29 14:45:15 +0100456 /* We should be aligned now -- can write
457 max page size bytes at a time */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 while (length) {
459 if (length > EPROM_PAGE_SIZE)
460 write_length = EPROM_PAGE_SIZE;
461 else
462 write_length = length;
463
Alan Cox2742fd82008-04-29 14:45:15 +0100464 dbg("%s - Page Write Addr = %x, length = %d",
465 __func__, start_address, write_length);
466 usb_serial_debug_data(debug, &serial->serial->dev->dev,
467 __func__, write_length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 /* Write next page */
Alan Cox2742fd82008-04-29 14:45:15 +0100470 be_start_address = cpu_to_be16(start_address);
471 status = ti_vsend_sync(serial->serial->dev, UMPC_MEMORY_WRITE,
472 (__u16)address_type,
473 (__force __u16)be_start_address,
474 buffer, write_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +0100476 dev_err(&serial->serial->dev->dev, "%s - ERROR %d\n",
477 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return status;
479 }
Alan Cox2742fd82008-04-29 14:45:15 +0100480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 length -= write_length;
482 start_address += write_length;
483 buffer += write_length;
484 }
485 return status;
486}
487
488/* Examine the UMP DMA registers and LSR
Alan Cox2742fd82008-04-29 14:45:15 +0100489 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 * Check the MSBit of the X and Y DMA byte count registers.
491 * A zero in this bit indicates that the TX DMA buffers are empty
492 * then check the TX Empty bit in the UART.
493 */
Alan Cox2742fd82008-04-29 14:45:15 +0100494static int tx_active(struct edgeport_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
496 int status;
497 struct out_endpoint_desc_block *oedb;
498 __u8 *lsr;
499 int bytes_left = 0;
500
Alan Cox2742fd82008-04-29 14:45:15 +0100501 oedb = kmalloc(sizeof(*oedb), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (!oedb) {
Alan Cox2742fd82008-04-29 14:45:15 +0100503 dev_err(&port->port->dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 return -ENOMEM;
505 }
506
Alan Cox2742fd82008-04-29 14:45:15 +0100507 lsr = kmalloc(1, GFP_KERNEL); /* Sigh, that's right, just one byte,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 as not all platforms can do DMA
509 from stack */
510 if (!lsr) {
511 kfree(oedb);
512 return -ENOMEM;
513 }
514 /* Read the DMA Count Registers */
Alan Cox2742fd82008-04-29 14:45:15 +0100515 status = read_ram(port->port->serial->dev, port->dma_address,
516 sizeof(*oedb), (void *)oedb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 if (status)
518 goto exit_is_tx_active;
519
Alan Cox2742fd82008-04-29 14:45:15 +0100520 dbg("%s - XByteCount 0x%X", __func__, oedb->XByteCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522 /* and the LSR */
Alan Cox2742fd82008-04-29 14:45:15 +0100523 status = read_ram(port->port->serial->dev,
524 port->uart_base + UMPMEM_OFFS_UART_LSR, 1, lsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 if (status)
527 goto exit_is_tx_active;
Alan Cox2742fd82008-04-29 14:45:15 +0100528 dbg("%s - LSR = 0x%X", __func__, *lsr);
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 /* If either buffer has data or we are transmitting then return TRUE */
Alan Cox2742fd82008-04-29 14:45:15 +0100531 if ((oedb->XByteCount & 0x80) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 bytes_left += 64;
533
Alan Cox2742fd82008-04-29 14:45:15 +0100534 if ((*lsr & UMP_UART_LSR_TX_MASK) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 bytes_left += 1;
536
537 /* We return Not Active if we get any kind of error */
538exit_is_tx_active:
Alan Cox2742fd82008-04-29 14:45:15 +0100539 dbg("%s - return %d", __func__, bytes_left);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 kfree(lsr);
542 kfree(oedb);
543 return bytes_left;
544}
545
Alan Cox2742fd82008-04-29 14:45:15 +0100546static void chase_port(struct edgeport_port *port, unsigned long timeout,
547 int flush)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
549 int baud_rate;
Alan Cox4a90f092008-10-13 10:39:46 +0100550 struct tty_struct *tty = tty_port_tty_get(&port->port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 wait_queue_t wait;
552 unsigned long flags;
553
554 if (!timeout)
Alan Cox2742fd82008-04-29 14:45:15 +0100555 timeout = (HZ * EDGE_CLOSING_WAIT)/100;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 /* wait for data to drain from the buffer */
558 spin_lock_irqsave(&port->ep_lock, flags);
559 init_waitqueue_entry(&wait, current);
560 add_wait_queue(&tty->write_wait, &wait);
561 for (;;) {
562 set_current_state(TASK_INTERRUPTIBLE);
Johan Hovoldd733cec2010-05-19 00:01:37 +0200563 if (kfifo_len(&port->write_fifo) == 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 || timeout == 0 || signal_pending(current)
Alan Cox2742fd82008-04-29 14:45:15 +0100565 || !usb_get_intfdata(port->port->serial->interface))
566 /* disconnect */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 break;
568 spin_unlock_irqrestore(&port->ep_lock, flags);
569 timeout = schedule_timeout(timeout);
570 spin_lock_irqsave(&port->ep_lock, flags);
571 }
572 set_current_state(TASK_RUNNING);
573 remove_wait_queue(&tty->write_wait, &wait);
574 if (flush)
Johan Hovoldd733cec2010-05-19 00:01:37 +0200575 kfifo_reset_out(&port->write_fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 spin_unlock_irqrestore(&port->ep_lock, flags);
Alan Cox4a90f092008-10-13 10:39:46 +0100577 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 /* wait for data to drain from the device */
580 timeout += jiffies;
581 while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
Alan Cox2742fd82008-04-29 14:45:15 +0100582 && usb_get_intfdata(port->port->serial->interface)) {
583 /* not disconnected */
584 if (!tx_active(port))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 break;
586 msleep(10);
587 }
588
589 /* disconnected */
590 if (!usb_get_intfdata(port->port->serial->interface))
591 return;
592
593 /* wait one more character time, based on baud rate */
Alan Cox2742fd82008-04-29 14:45:15 +0100594 /* (tx_active doesn't seem to wait for the last byte) */
595 baud_rate = port->baud_rate;
596 if (baud_rate == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 baud_rate = 50;
Julia Lawalldfa5ec72008-03-04 15:25:11 -0800598 msleep(max(1, DIV_ROUND_UP(10000, baud_rate)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599}
600
Alan Cox2742fd82008-04-29 14:45:15 +0100601static int choose_config(struct usb_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Alan Cox2742fd82008-04-29 14:45:15 +0100603 /*
604 * There may be multiple configurations on this device, in which case
605 * we would need to read and parse all of them to find out which one
606 * we want. However, we just support one config at this point,
607 * configuration # 1, which is Config Descriptor 0.
608 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Alan Cox2742fd82008-04-29 14:45:15 +0100610 dbg("%s - Number of Interfaces = %d",
611 __func__, dev->config->desc.bNumInterfaces);
612 dbg("%s - MAX Power = %d",
613 __func__, dev->config->desc.bMaxPower * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 if (dev->config->desc.bNumInterfaces != 1) {
Alan Cox2742fd82008-04-29 14:45:15 +0100616 dev_err(&dev->dev, "%s - bNumInterfaces is not 1, ERROR!\n",
617 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 return -ENODEV;
619 }
620
621 return 0;
622}
623
Alan Cox2742fd82008-04-29 14:45:15 +0100624static int read_rom(struct edgeport_serial *serial,
625 int start_address, int length, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
627 int status;
628
629 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
Alan Cox2742fd82008-04-29 14:45:15 +0100630 status = read_download_mem(serial->serial->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 start_address,
632 length,
633 serial->TI_I2C_Type,
634 buffer);
635 } else {
Alan Cox2742fd82008-04-29 14:45:15 +0100636 status = read_boot_mem(serial, start_address, length,
637 buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return status;
640}
641
Alan Cox2742fd82008-04-29 14:45:15 +0100642static int write_rom(struct edgeport_serial *serial, int start_address,
643 int length, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
645 if (serial->product_info.TiMode == TI_MODE_BOOT)
Alan Cox2742fd82008-04-29 14:45:15 +0100646 return write_boot_mem(serial, start_address, length,
647 buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD)
Alan Cox2742fd82008-04-29 14:45:15 +0100650 return write_i2c_mem(serial, start_address, length,
651 serial->TI_I2C_Type, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 return -EINVAL;
653}
654
655
656
657/* Read a descriptor header from I2C based on type */
Alan Cox2742fd82008-04-29 14:45:15 +0100658static int get_descriptor_addr(struct edgeport_serial *serial,
659 int desc_type, struct ti_i2c_desc *rom_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
661 int start_address;
662 int status;
663
664 /* Search for requested descriptor in I2C */
665 start_address = 2;
666 do {
Alan Cox2742fd82008-04-29 14:45:15 +0100667 status = read_rom(serial,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 start_address,
669 sizeof(struct ti_i2c_desc),
Alan Cox2742fd82008-04-29 14:45:15 +0100670 (__u8 *)rom_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (status)
672 return 0;
673
674 if (rom_desc->Type == desc_type)
675 return start_address;
676
Alan Cox2742fd82008-04-29 14:45:15 +0100677 start_address = start_address + sizeof(struct ti_i2c_desc)
678 + rom_desc->Size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 } while ((start_address < TI_MAX_I2C_SIZE) && rom_desc->Type);
Alan Cox2742fd82008-04-29 14:45:15 +0100681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return 0;
683}
684
685/* Validate descriptor checksum */
Alan Cox2742fd82008-04-29 14:45:15 +0100686static int valid_csum(struct ti_i2c_desc *rom_desc, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
688 __u16 i;
689 __u8 cs = 0;
690
Alan Cox2742fd82008-04-29 14:45:15 +0100691 for (i = 0; i < rom_desc->Size; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 cs = (__u8)(cs + buffer[i]);
Alan Cox2742fd82008-04-29 14:45:15 +0100693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (cs != rom_desc->CheckSum) {
Alan Cox2742fd82008-04-29 14:45:15 +0100695 dbg("%s - Mismatch %x - %x", __func__, rom_desc->CheckSum, cs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return -EINVAL;
697 }
698 return 0;
699}
700
701/* Make sure that the I2C image is good */
Alan Cox2742fd82008-04-29 14:45:15 +0100702static int check_i2c_image(struct edgeport_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
704 struct device *dev = &serial->serial->dev->dev;
705 int status = 0;
706 struct ti_i2c_desc *rom_desc;
707 int start_address = 2;
708 __u8 *buffer;
709 __u16 ttype;
710
Alan Cox2742fd82008-04-29 14:45:15 +0100711 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if (!rom_desc) {
Alan Cox2742fd82008-04-29 14:45:15 +0100713 dev_err(dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 return -ENOMEM;
715 }
Alan Cox2742fd82008-04-29 14:45:15 +0100716 buffer = kmalloc(TI_MAX_I2C_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 if (!buffer) {
Alan Cox2742fd82008-04-29 14:45:15 +0100718 dev_err(dev, "%s - out of memory when allocating buffer\n",
719 __func__);
720 kfree(rom_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return -ENOMEM;
722 }
723
Alan Cox2742fd82008-04-29 14:45:15 +0100724 /* Read the first byte (Signature0) must be 0x52 or 0x10 */
725 status = read_rom(serial, 0, 1, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (status)
Alan Cox2742fd82008-04-29 14:45:15 +0100727 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 if (*buffer != UMP5152 && *buffer != UMP3410) {
Alan Cox2742fd82008-04-29 14:45:15 +0100730 dev_err(dev, "%s - invalid buffer signature\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 status = -ENODEV;
Alan Cox2742fd82008-04-29 14:45:15 +0100732 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
734
735 do {
Alan Cox2742fd82008-04-29 14:45:15 +0100736 /* Validate the I2C */
737 status = read_rom(serial,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 start_address,
739 sizeof(struct ti_i2c_desc),
740 (__u8 *)rom_desc);
741 if (status)
742 break;
743
Alan Cox2742fd82008-04-29 14:45:15 +0100744 if ((start_address + sizeof(struct ti_i2c_desc) +
745 rom_desc->Size) > TI_MAX_I2C_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 status = -ENODEV;
Alan Cox2742fd82008-04-29 14:45:15 +0100747 dbg("%s - structure too big, erroring out.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 break;
749 }
750
Alan Cox2742fd82008-04-29 14:45:15 +0100751 dbg("%s Type = 0x%x", __func__, rom_desc->Type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Alan Cox2742fd82008-04-29 14:45:15 +0100753 /* Skip type 2 record */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 ttype = rom_desc->Type & 0x0f;
Alan Cox2742fd82008-04-29 14:45:15 +0100755 if (ttype != I2C_DESC_TYPE_FIRMWARE_BASIC
756 && ttype != I2C_DESC_TYPE_FIRMWARE_AUTO) {
757 /* Read the descriptor data */
758 status = read_rom(serial, start_address +
759 sizeof(struct ti_i2c_desc),
760 rom_desc->Size, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 if (status)
762 break;
763
Alan Cox2742fd82008-04-29 14:45:15 +0100764 status = valid_csum(rom_desc, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 if (status)
766 break;
767 }
Alan Cox2742fd82008-04-29 14:45:15 +0100768 start_address = start_address + sizeof(struct ti_i2c_desc) +
769 rom_desc->Size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Alan Cox2742fd82008-04-29 14:45:15 +0100771 } while ((rom_desc->Type != I2C_DESC_TYPE_ION) &&
772 (start_address < TI_MAX_I2C_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Alan Cox2742fd82008-04-29 14:45:15 +0100774 if ((rom_desc->Type != I2C_DESC_TYPE_ION) ||
775 (start_address > TI_MAX_I2C_SIZE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 status = -ENODEV;
777
Alan Cox2742fd82008-04-29 14:45:15 +0100778out:
779 kfree(buffer);
780 kfree(rom_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return status;
782}
783
Alan Cox2742fd82008-04-29 14:45:15 +0100784static int get_manuf_info(struct edgeport_serial *serial, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
786 int status;
787 int start_address;
788 struct ti_i2c_desc *rom_desc;
789 struct edge_ti_manuf_descriptor *desc;
790
Alan Cox2742fd82008-04-29 14:45:15 +0100791 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (!rom_desc) {
Alan Cox2742fd82008-04-29 14:45:15 +0100793 dev_err(&serial->serial->dev->dev, "%s - out of memory\n",
794 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return -ENOMEM;
796 }
Alan Cox2742fd82008-04-29 14:45:15 +0100797 start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_ION,
798 rom_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 if (!start_address) {
Alan Cox2742fd82008-04-29 14:45:15 +0100801 dbg("%s - Edge Descriptor not found in I2C", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 status = -ENODEV;
803 goto exit;
804 }
805
Alan Cox2742fd82008-04-29 14:45:15 +0100806 /* Read the descriptor data */
807 status = read_rom(serial, start_address+sizeof(struct ti_i2c_desc),
808 rom_desc->Size, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (status)
810 goto exit;
Alan Cox2742fd82008-04-29 14:45:15 +0100811
812 status = valid_csum(rom_desc, buffer);
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 desc = (struct edge_ti_manuf_descriptor *)buffer;
Alan Cox2742fd82008-04-29 14:45:15 +0100815 dbg("%s - IonConfig 0x%x", __func__, desc->IonConfig);
816 dbg("%s - Version %d", __func__, desc->Version);
817 dbg("%s - Cpu/Board 0x%x", __func__, desc->CpuRev_BoardRev);
818 dbg("%s - NumPorts %d", __func__, desc->NumPorts);
819 dbg("%s - NumVirtualPorts %d", __func__, desc->NumVirtualPorts);
820 dbg("%s - TotalPorts %d", __func__, desc->TotalPorts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822exit:
Alan Cox2742fd82008-04-29 14:45:15 +0100823 kfree(rom_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return status;
825}
826
827/* Build firmware header used for firmware update */
Alan Cox2742fd82008-04-29 14:45:15 +0100828static int build_i2c_fw_hdr(__u8 *header, struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
830 __u8 *buffer;
831 int buffer_size;
832 int i;
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530833 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 __u8 cs = 0;
835 struct ti_i2c_desc *i2c_header;
836 struct ti_i2c_image_header *img_header;
837 struct ti_i2c_firmware_rec *firmware_rec;
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530838 const struct firmware *fw;
839 const char *fw_name = "edgeport/down3.bin";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Alan Cox2742fd82008-04-29 14:45:15 +0100841 /* In order to update the I2C firmware we must change the type 2 record
842 * to type 0xF2. This will force the UMP to come up in Boot Mode.
843 * Then while in boot mode, the driver will download the latest
844 * firmware (padded to 15.5k) into the UMP ram. And finally when the
845 * device comes back up in download mode the driver will cause the new
846 * firmware to be copied from the UMP Ram to I2C and the firmware will
847 * update the record type from 0xf2 to 0x02.
848 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Alan Cox2742fd82008-04-29 14:45:15 +0100850 /* Allocate a 15.5k buffer + 2 bytes for version number
851 * (Firmware Record) */
852 buffer_size = (((1024 * 16) - 512 ) +
853 sizeof(struct ti_i2c_firmware_rec));
854
855 buffer = kmalloc(buffer_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 if (!buffer) {
Alan Cox2742fd82008-04-29 14:45:15 +0100857 dev_err(dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return -ENOMEM;
859 }
Alan Cox2742fd82008-04-29 14:45:15 +0100860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 // Set entire image of 0xffs
Alan Cox2742fd82008-04-29 14:45:15 +0100862 memset(buffer, 0xff, buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530864 err = request_firmware(&fw, fw_name, dev);
865 if (err) {
866 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
867 fw_name, err);
868 kfree(buffer);
869 return err;
870 }
871
872 /* Save Download Version Number */
873 OperationalMajorVersion = fw->data[0];
874 OperationalMinorVersion = fw->data[1];
875 OperationalBuildNumber = fw->data[2] | (fw->data[3] << 8);
876
Alan Cox2742fd82008-04-29 14:45:15 +0100877 /* Copy version number into firmware record */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 firmware_rec = (struct ti_i2c_firmware_rec *)buffer;
879
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530880 firmware_rec->Ver_Major = OperationalMajorVersion;
881 firmware_rec->Ver_Minor = OperationalMinorVersion;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Alan Cox2742fd82008-04-29 14:45:15 +0100883 /* Pointer to fw_down memory image */
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530884 img_header = (struct ti_i2c_image_header *)&fw->data[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Alan Cox2742fd82008-04-29 14:45:15 +0100886 memcpy(buffer + sizeof(struct ti_i2c_firmware_rec),
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530887 &fw->data[4 + sizeof(struct ti_i2c_image_header)],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 le16_to_cpu(img_header->Length));
889
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530890 release_firmware(fw);
891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 for (i=0; i < buffer_size; i++) {
893 cs = (__u8)(cs + buffer[i]);
894 }
895
Alan Cox2742fd82008-04-29 14:45:15 +0100896 kfree(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Alan Cox2742fd82008-04-29 14:45:15 +0100898 /* Build new header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 i2c_header = (struct ti_i2c_desc *)header;
900 firmware_rec = (struct ti_i2c_firmware_rec*)i2c_header->Data;
Alan Cox2742fd82008-04-29 14:45:15 +0100901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 i2c_header->Type = I2C_DESC_TYPE_FIRMWARE_BLANK;
903 i2c_header->Size = (__u16)buffer_size;
904 i2c_header->CheckSum = cs;
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530905 firmware_rec->Ver_Major = OperationalMajorVersion;
906 firmware_rec->Ver_Minor = OperationalMinorVersion;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
908 return 0;
909}
910
911/* Try to figure out what type of I2c we have */
Alan Cox2742fd82008-04-29 14:45:15 +0100912static int i2c_type_bootmode(struct edgeport_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
914 int status;
Johan Hovolde9305d22009-12-28 23:01:50 +0100915 u8 *data;
916
917 data = kmalloc(1, GFP_KERNEL);
918 if (!data) {
919 dev_err(&serial->serial->dev->dev,
920 "%s - out of memory\n", __func__);
921 return -ENOMEM;
922 }
Alan Cox2742fd82008-04-29 14:45:15 +0100923
924 /* Try to read type 2 */
925 status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
Johan Hovolde9305d22009-12-28 23:01:50 +0100926 DTK_ADDR_SPACE_I2C_TYPE_II, 0, data, 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 if (status)
Alan Cox2742fd82008-04-29 14:45:15 +0100928 dbg("%s - read 2 status error = %d", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 else
Johan Hovolde9305d22009-12-28 23:01:50 +0100930 dbg("%s - read 2 data = 0x%x", __func__, *data);
931 if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
Alan Cox2742fd82008-04-29 14:45:15 +0100932 dbg("%s - ROM_TYPE_II", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
Johan Hovolde9305d22009-12-28 23:01:50 +0100934 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
936
Alan Cox2742fd82008-04-29 14:45:15 +0100937 /* Try to read type 3 */
938 status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
Johan Hovolde9305d22009-12-28 23:01:50 +0100939 DTK_ADDR_SPACE_I2C_TYPE_III, 0, data, 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 if (status)
Alan Cox2742fd82008-04-29 14:45:15 +0100941 dbg("%s - read 3 status error = %d", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 else
Johan Hovolde9305d22009-12-28 23:01:50 +0100943 dbg("%s - read 2 data = 0x%x", __func__, *data);
944 if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
Alan Cox2742fd82008-04-29 14:45:15 +0100945 dbg("%s - ROM_TYPE_III", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_III;
Johan Hovolde9305d22009-12-28 23:01:50 +0100947 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 }
949
Alan Cox2742fd82008-04-29 14:45:15 +0100950 dbg("%s - Unknown", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
Johan Hovolde9305d22009-12-28 23:01:50 +0100952 status = -ENODEV;
953out:
954 kfree(data);
955 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}
957
Alan Cox2742fd82008-04-29 14:45:15 +0100958static int bulk_xfer(struct usb_serial *serial, void *buffer,
959 int length, int *num_sent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
961 int status;
962
Alan Cox2742fd82008-04-29 14:45:15 +0100963 status = usb_bulk_msg(serial->dev,
964 usb_sndbulkpipe(serial->dev,
965 serial->port[0]->bulk_out_endpointAddress),
966 buffer, length, num_sent, 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return status;
968}
969
970/* Download given firmware image to the device (IN BOOT MODE) */
Alan Cox2742fd82008-04-29 14:45:15 +0100971static int download_code(struct edgeport_serial *serial, __u8 *image,
972 int image_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
974 int status = 0;
975 int pos;
976 int transfer;
977 int done;
978
Alan Cox2742fd82008-04-29 14:45:15 +0100979 /* Transfer firmware image */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 for (pos = 0; pos < image_length; ) {
Alan Cox2742fd82008-04-29 14:45:15 +0100981 /* Read the next buffer from file */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 transfer = image_length - pos;
983 if (transfer > EDGE_FW_BULK_MAX_PACKET_SIZE)
984 transfer = EDGE_FW_BULK_MAX_PACKET_SIZE;
985
Alan Cox2742fd82008-04-29 14:45:15 +0100986 /* Transfer data */
987 status = bulk_xfer(serial->serial, &image[pos],
988 transfer, &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 if (status)
990 break;
Alan Cox2742fd82008-04-29 14:45:15 +0100991 /* Advance buffer pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 pos += done;
993 }
994
995 return status;
996}
997
Alan Cox2742fd82008-04-29 14:45:15 +0100998/* FIXME!!! */
999static int config_boot_dev(struct usb_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
1001 return 0;
1002}
1003
Alan Cox2742fd82008-04-29 14:45:15 +01001004static int ti_cpu_rev(struct edge_ti_manuf_descriptor *desc)
1005{
1006 return TI_GET_CPU_REVISION(desc->CpuRev_BoardRev);
1007}
1008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009/**
1010 * DownloadTIFirmware - Download run-time operating firmware to the TI5052
Alan Cox2742fd82008-04-29 14:45:15 +01001011 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 * This routine downloads the main operating code into the TI5052, using the
1013 * boot code already burned into E2PROM or ROM.
1014 */
Alan Cox2742fd82008-04-29 14:45:15 +01001015static int download_fw(struct edgeport_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016{
1017 struct device *dev = &serial->serial->dev->dev;
1018 int status = 0;
1019 int start_address;
1020 struct edge_ti_manuf_descriptor *ti_manuf_desc;
1021 struct usb_interface_descriptor *interface;
1022 int download_cur_ver;
1023 int download_new_ver;
1024
1025 /* This routine is entered by both the BOOT mode and the Download mode
1026 * We can determine which code is running by the reading the config
1027 * descriptor and if we have only one bulk pipe it is in boot mode
1028 */
1029 serial->product_info.hardware_type = HARDWARE_TYPE_TIUMP;
1030
1031 /* Default to type 2 i2c */
1032 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
1033
Alan Cox2742fd82008-04-29 14:45:15 +01001034 status = choose_config(serial->serial->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 if (status)
1036 return status;
1037
1038 interface = &serial->serial->interface->cur_altsetting->desc;
1039 if (!interface) {
Alan Cox2742fd82008-04-29 14:45:15 +01001040 dev_err(dev, "%s - no interface set, error!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 return -ENODEV;
1042 }
1043
Alan Cox2742fd82008-04-29 14:45:15 +01001044 /*
1045 * Setup initial mode -- the default mode 0 is TI_MODE_CONFIGURING
1046 * if we have more than one endpoint we are definitely in download
1047 * mode
1048 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 if (interface->bNumEndpoints > 1)
1050 serial->product_info.TiMode = TI_MODE_DOWNLOAD;
1051 else
Alan Cox2742fd82008-04-29 14:45:15 +01001052 /* Otherwise we will remain in configuring mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 serial->product_info.TiMode = TI_MODE_CONFIGURING;
1054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 /********************************************************************/
1056 /* Download Mode */
1057 /********************************************************************/
1058 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
1059 struct ti_i2c_desc *rom_desc;
1060
Andrew Morton9544e832008-02-04 23:57:50 -08001061 dbg("%s - RUNNING IN DOWNLOAD MODE", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Alan Cox2742fd82008-04-29 14:45:15 +01001063 status = check_i2c_image(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 if (status) {
Andrew Morton9544e832008-02-04 23:57:50 -08001065 dbg("%s - DOWNLOAD MODE -- BAD I2C", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 return status;
1067 }
Alan Cox2742fd82008-04-29 14:45:15 +01001068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 /* Validate Hardware version number
1070 * Read Manufacturing Descriptor from TI Based Edgeport
1071 */
Alan Cox2742fd82008-04-29 14:45:15 +01001072 ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 if (!ti_manuf_desc) {
Alan Cox2742fd82008-04-29 14:45:15 +01001074 dev_err(dev, "%s - out of memory.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 return -ENOMEM;
1076 }
Alan Cox2742fd82008-04-29 14:45:15 +01001077 status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001079 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 return status;
1081 }
1082
Alan Cox2742fd82008-04-29 14:45:15 +01001083 /* Check version number of ION descriptor */
1084 if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
1085 dbg("%s - Wrong CPU Rev %d (Must be 2)",
1086 __func__, ti_cpu_rev(ti_manuf_desc));
1087 kfree(ti_manuf_desc);
1088 return -EINVAL;
1089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Alan Cox2742fd82008-04-29 14:45:15 +01001091 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 if (!rom_desc) {
Alan Cox2742fd82008-04-29 14:45:15 +01001093 dev_err(dev, "%s - out of memory.\n", __func__);
1094 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 return -ENOMEM;
1096 }
1097
Alan Cox2742fd82008-04-29 14:45:15 +01001098 /* Search for type 2 record (firmware record) */
1099 start_address = get_descriptor_addr(serial,
1100 I2C_DESC_TYPE_FIRMWARE_BASIC, rom_desc);
1101 if (start_address != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 struct ti_i2c_firmware_rec *firmware_version;
Johan Hovolde9305d22009-12-28 23:01:50 +01001103 u8 *record;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Alan Cox2742fd82008-04-29 14:45:15 +01001105 dbg("%s - Found Type FIRMWARE (Type 2) record",
1106 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Alan Cox2742fd82008-04-29 14:45:15 +01001108 firmware_version = kmalloc(sizeof(*firmware_version),
1109 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 if (!firmware_version) {
Alan Cox2742fd82008-04-29 14:45:15 +01001111 dev_err(dev, "%s - out of memory.\n", __func__);
1112 kfree(rom_desc);
1113 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 return -ENOMEM;
1115 }
1116
Alan Cox2742fd82008-04-29 14:45:15 +01001117 /* Validate version number
1118 * Read the descriptor data
1119 */
1120 status = read_rom(serial, start_address +
1121 sizeof(struct ti_i2c_desc),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 sizeof(struct ti_i2c_firmware_rec),
1123 (__u8 *)firmware_version);
1124 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001125 kfree(firmware_version);
1126 kfree(rom_desc);
1127 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 return status;
1129 }
1130
Alan Cox2742fd82008-04-29 14:45:15 +01001131 /* Check version number of download with current
1132 version in I2c */
1133 download_cur_ver = (firmware_version->Ver_Major << 8) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 (firmware_version->Ver_Minor);
Jaswinder Singhd12b2192008-07-04 23:06:09 +05301135 download_new_ver = (OperationalMajorVersion << 8) +
1136 (OperationalMinorVersion);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Alan Cox2742fd82008-04-29 14:45:15 +01001138 dbg("%s - >> FW Versions Device %d.%d Driver %d.%d",
1139 __func__,
1140 firmware_version->Ver_Major,
1141 firmware_version->Ver_Minor,
1142 OperationalMajorVersion,
1143 OperationalMinorVersion);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Alan Cox2742fd82008-04-29 14:45:15 +01001145 /* Check if we have an old version in the I2C and
1146 update if necessary */
Greg Kroah-Hartman0827a9f2010-08-17 15:15:37 -07001147 if (download_cur_ver < download_new_ver) {
Alan Cox2742fd82008-04-29 14:45:15 +01001148 dbg("%s - Update I2C dld from %d.%d to %d.%d",
1149 __func__,
1150 firmware_version->Ver_Major,
1151 firmware_version->Ver_Minor,
1152 OperationalMajorVersion,
1153 OperationalMinorVersion);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Johan Hovolde9305d22009-12-28 23:01:50 +01001155 record = kmalloc(1, GFP_KERNEL);
1156 if (!record) {
1157 dev_err(dev, "%s - out of memory.\n",
1158 __func__);
1159 kfree(firmware_version);
1160 kfree(rom_desc);
1161 kfree(ti_manuf_desc);
1162 return -ENOMEM;
1163 }
Alan Cox2742fd82008-04-29 14:45:15 +01001164 /* In order to update the I2C firmware we must
1165 * change the type 2 record to type 0xF2. This
1166 * will force the UMP to come up in Boot Mode.
1167 * Then while in boot mode, the driver will
1168 * download the latest firmware (padded to
1169 * 15.5k) into the UMP ram. Finally when the
1170 * device comes back up in download mode the
1171 * driver will cause the new firmware to be
1172 * copied from the UMP Ram to I2C and the
1173 * firmware will update the record type from
1174 * 0xf2 to 0x02.
1175 */
Johan Hovolde9305d22009-12-28 23:01:50 +01001176 *record = I2C_DESC_TYPE_FIRMWARE_BLANK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Alan Cox2742fd82008-04-29 14:45:15 +01001178 /* Change the I2C Firmware record type to
1179 0xf2 to trigger an update */
1180 status = write_rom(serial, start_address,
Johan Hovolde9305d22009-12-28 23:01:50 +01001181 sizeof(*record), record);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 if (status) {
Johan Hovolde9305d22009-12-28 23:01:50 +01001183 kfree(record);
Alan Cox2742fd82008-04-29 14:45:15 +01001184 kfree(firmware_version);
1185 kfree(rom_desc);
1186 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 return status;
1188 }
1189
Alan Cox2742fd82008-04-29 14:45:15 +01001190 /* verify the write -- must do this in order
1191 * for write to complete before we do the
1192 * hardware reset
1193 */
1194 status = read_rom(serial,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 start_address,
Johan Hovolde9305d22009-12-28 23:01:50 +01001196 sizeof(*record),
1197 record);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 if (status) {
Johan Hovolde9305d22009-12-28 23:01:50 +01001199 kfree(record);
Alan Cox2742fd82008-04-29 14:45:15 +01001200 kfree(firmware_version);
1201 kfree(rom_desc);
1202 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 return status;
1204 }
1205
Johan Hovolde9305d22009-12-28 23:01:50 +01001206 if (*record != I2C_DESC_TYPE_FIRMWARE_BLANK) {
Alan Cox2742fd82008-04-29 14:45:15 +01001207 dev_err(dev,
1208 "%s - error resetting device\n",
1209 __func__);
Johan Hovolde9305d22009-12-28 23:01:50 +01001210 kfree(record);
Alan Cox2742fd82008-04-29 14:45:15 +01001211 kfree(firmware_version);
1212 kfree(rom_desc);
1213 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 return -ENODEV;
1215 }
1216
Alan Cox2742fd82008-04-29 14:45:15 +01001217 dbg("%s - HARDWARE RESET", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Alan Cox2742fd82008-04-29 14:45:15 +01001219 /* Reset UMP -- Back to BOOT MODE */
1220 status = ti_vsend_sync(serial->serial->dev,
1221 UMPC_HARDWARE_RESET,
1222 0, 0, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Alan Cox2742fd82008-04-29 14:45:15 +01001224 dbg("%s - HARDWARE RESET return %d",
1225 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227 /* return an error on purpose. */
Johan Hovolde9305d22009-12-28 23:01:50 +01001228 kfree(record);
Alan Cox2742fd82008-04-29 14:45:15 +01001229 kfree(firmware_version);
1230 kfree(rom_desc);
1231 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 return -ENODEV;
1233 }
Alan Cox2742fd82008-04-29 14:45:15 +01001234 kfree(firmware_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 }
Alan Cox2742fd82008-04-29 14:45:15 +01001236 /* Search for type 0xF2 record (firmware blank record) */
1237 else if ((start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_FIRMWARE_BLANK, rom_desc)) != 0) {
1238#define HEADER_SIZE (sizeof(struct ti_i2c_desc) + \
1239 sizeof(struct ti_i2c_firmware_rec))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 __u8 *header;
1241 __u8 *vheader;
1242
Alan Cox2742fd82008-04-29 14:45:15 +01001243 header = kmalloc(HEADER_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 if (!header) {
Alan Cox2742fd82008-04-29 14:45:15 +01001245 dev_err(dev, "%s - out of memory.\n", __func__);
1246 kfree(rom_desc);
1247 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 return -ENOMEM;
1249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Alan Cox2742fd82008-04-29 14:45:15 +01001251 vheader = kmalloc(HEADER_SIZE, GFP_KERNEL);
1252 if (!vheader) {
1253 dev_err(dev, "%s - out of memory.\n", __func__);
1254 kfree(header);
1255 kfree(rom_desc);
1256 kfree(ti_manuf_desc);
1257 return -ENOMEM;
1258 }
1259
1260 dbg("%s - Found Type BLANK FIRMWARE (Type F2) record",
1261 __func__);
1262
1263 /*
1264 * In order to update the I2C firmware we must change
1265 * the type 2 record to type 0xF2. This will force the
1266 * UMP to come up in Boot Mode. Then while in boot
1267 * mode, the driver will download the latest firmware
1268 * (padded to 15.5k) into the UMP ram. Finally when the
1269 * device comes back up in download mode the driver
1270 * will cause the new firmware to be copied from the
1271 * UMP Ram to I2C and the firmware will update the
1272 * record type from 0xf2 to 0x02.
1273 */
1274 status = build_i2c_fw_hdr(header, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001276 kfree(vheader);
1277 kfree(header);
1278 kfree(rom_desc);
1279 kfree(ti_manuf_desc);
Roel Kluinfd6e5bb2010-08-10 14:29:19 -07001280 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 }
1282
Alan Cox2742fd82008-04-29 14:45:15 +01001283 /* Update I2C with type 0xf2 record with correct
1284 size and checksum */
1285 status = write_rom(serial,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 start_address,
1287 HEADER_SIZE,
1288 header);
1289 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001290 kfree(vheader);
1291 kfree(header);
1292 kfree(rom_desc);
1293 kfree(ti_manuf_desc);
Roel Kluin9800eb32010-07-20 15:29:08 -07001294 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
1296
Alan Cox2742fd82008-04-29 14:45:15 +01001297 /* verify the write -- must do this in order for
1298 write to complete before we do the hardware reset */
1299 status = read_rom(serial, start_address,
1300 HEADER_SIZE, vheader);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001303 dbg("%s - can't read header back", __func__);
1304 kfree(vheader);
1305 kfree(header);
1306 kfree(rom_desc);
1307 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 return status;
1309 }
1310 if (memcmp(vheader, header, HEADER_SIZE)) {
Alan Cox2742fd82008-04-29 14:45:15 +01001311 dbg("%s - write download record failed",
1312 __func__);
1313 kfree(vheader);
1314 kfree(header);
1315 kfree(rom_desc);
1316 kfree(ti_manuf_desc);
Roel Kluin1e297092010-07-02 00:36:43 +02001317 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 }
1319
Alan Cox2742fd82008-04-29 14:45:15 +01001320 kfree(vheader);
1321 kfree(header);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Alan Cox2742fd82008-04-29 14:45:15 +01001323 dbg("%s - Start firmware update", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Alan Cox2742fd82008-04-29 14:45:15 +01001325 /* Tell firmware to copy download image into I2C */
1326 status = ti_vsend_sync(serial->serial->dev,
1327 UMPC_COPY_DNLD_TO_I2C, 0, 0, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Alan Cox2742fd82008-04-29 14:45:15 +01001329 dbg("%s - Update complete 0x%x", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001331 dev_err(dev,
1332 "%s - UMPC_COPY_DNLD_TO_I2C failed\n",
1333 __func__);
1334 kfree(rom_desc);
1335 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 return status;
1337 }
1338 }
1339
1340 // The device is running the download code
Alan Cox2742fd82008-04-29 14:45:15 +01001341 kfree(rom_desc);
1342 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 return 0;
1344 }
1345
1346 /********************************************************************/
1347 /* Boot Mode */
1348 /********************************************************************/
Andrew Morton9544e832008-02-04 23:57:50 -08001349 dbg("%s - RUNNING IN BOOT MODE", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Alan Cox2742fd82008-04-29 14:45:15 +01001351 /* Configure the TI device so we can use the BULK pipes for download */
1352 status = config_boot_dev(serial->serial->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 if (status)
1354 return status;
1355
Alan Cox2742fd82008-04-29 14:45:15 +01001356 if (le16_to_cpu(serial->serial->dev->descriptor.idVendor)
1357 != USB_VENDOR_ID_ION) {
1358 dbg("%s - VID = 0x%x", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 le16_to_cpu(serial->serial->dev->descriptor.idVendor));
1360 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
Alan Cox2742fd82008-04-29 14:45:15 +01001361 goto stayinbootmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 }
1363
Alan Cox2742fd82008-04-29 14:45:15 +01001364 /* We have an ION device (I2c Must be programmed)
1365 Determine I2C image type */
1366 if (i2c_type_bootmode(serial))
1367 goto stayinbootmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Alan Cox2742fd82008-04-29 14:45:15 +01001369 /* Check for ION Vendor ID and that the I2C is valid */
1370 if (!check_i2c_image(serial)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 struct ti_i2c_image_header *header;
1372 int i;
1373 __u8 cs = 0;
1374 __u8 *buffer;
1375 int buffer_size;
Jaswinder Singhd12b2192008-07-04 23:06:09 +05301376 int err;
1377 const struct firmware *fw;
1378 const char *fw_name = "edgeport/down3.bin";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 /* Validate Hardware version number
1381 * Read Manufacturing Descriptor from TI Based Edgeport
1382 */
Alan Cox2742fd82008-04-29 14:45:15 +01001383 ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 if (!ti_manuf_desc) {
Alan Cox2742fd82008-04-29 14:45:15 +01001385 dev_err(dev, "%s - out of memory.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 return -ENOMEM;
1387 }
Alan Cox2742fd82008-04-29 14:45:15 +01001388 status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001390 kfree(ti_manuf_desc);
1391 goto stayinbootmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 }
1393
Alan Cox2742fd82008-04-29 14:45:15 +01001394 /* Check for version 2 */
1395 if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
1396 dbg("%s - Wrong CPU Rev %d (Must be 2)",
1397 __func__, ti_cpu_rev(ti_manuf_desc));
1398 kfree(ti_manuf_desc);
1399 goto stayinbootmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 }
1401
Alan Cox2742fd82008-04-29 14:45:15 +01001402 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 /*
Alan Cox2742fd82008-04-29 14:45:15 +01001405 * In order to update the I2C firmware we must change the type
1406 * 2 record to type 0xF2. This will force the UMP to come up
1407 * in Boot Mode. Then while in boot mode, the driver will
1408 * download the latest firmware (padded to 15.5k) into the
1409 * UMP ram. Finally when the device comes back up in download
1410 * mode the driver will cause the new firmware to be copied
1411 * from the UMP Ram to I2C and the firmware will update the
1412 * record type from 0xf2 to 0x02.
1413 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 * Do we really have to copy the whole firmware image,
1415 * or could we do this in place!
1416 */
1417
Alan Cox2742fd82008-04-29 14:45:15 +01001418 /* Allocate a 15.5k buffer + 3 byte header */
1419 buffer_size = (((1024 * 16) - 512) +
1420 sizeof(struct ti_i2c_image_header));
1421 buffer = kmalloc(buffer_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 if (!buffer) {
Alan Cox2742fd82008-04-29 14:45:15 +01001423 dev_err(dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 return -ENOMEM;
1425 }
Alan Cox2742fd82008-04-29 14:45:15 +01001426
1427 /* Initialize the buffer to 0xff (pad the buffer) */
1428 memset(buffer, 0xff, buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Jaswinder Singhd12b2192008-07-04 23:06:09 +05301430 err = request_firmware(&fw, fw_name, dev);
1431 if (err) {
1432 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
1433 fw_name, err);
1434 kfree(buffer);
1435 return err;
1436 }
1437 memcpy(buffer, &fw->data[4], fw->size - 4);
1438 release_firmware(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Alan Cox2742fd82008-04-29 14:45:15 +01001440 for (i = sizeof(struct ti_i2c_image_header);
1441 i < buffer_size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 cs = (__u8)(cs + buffer[i]);
1443 }
Alan Cox2742fd82008-04-29 14:45:15 +01001444
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 header = (struct ti_i2c_image_header *)buffer;
Alan Cox2742fd82008-04-29 14:45:15 +01001446
1447 /* update length and checksum after padding */
1448 header->Length = cpu_to_le16((__u16)(buffer_size -
1449 sizeof(struct ti_i2c_image_header)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 header->CheckSum = cs;
1451
Alan Cox2742fd82008-04-29 14:45:15 +01001452 /* Download the operational code */
1453 dbg("%s - Downloading operational code image (TI UMP)",
1454 __func__);
1455 status = download_code(serial, buffer, buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Alan Cox2742fd82008-04-29 14:45:15 +01001457 kfree(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
1459 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001460 dbg("%s - Error downloading operational code image",
1461 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 return status;
1463 }
1464
Alan Cox2742fd82008-04-29 14:45:15 +01001465 /* Device will reboot */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 serial->product_info.TiMode = TI_MODE_TRANSITIONING;
1467
Alan Cox2742fd82008-04-29 14:45:15 +01001468 dbg("%s - Download successful -- Device rebooting...",
1469 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
1471 /* return an error on purpose */
1472 return -ENODEV;
1473 }
1474
Alan Cox2742fd82008-04-29 14:45:15 +01001475stayinbootmode:
1476 /* Eprom is invalid or blank stay in boot mode */
Andrew Morton9544e832008-02-04 23:57:50 -08001477 dbg("%s - STAYING IN BOOT MODE", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 serial->product_info.TiMode = TI_MODE_BOOT;
1479
1480 return 0;
1481}
1482
1483
Alan Cox2742fd82008-04-29 14:45:15 +01001484static int ti_do_config(struct edgeport_port *port, int feature, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485{
1486 int port_number = port->port->number - port->port->serial->minor;
Alan Cox2742fd82008-04-29 14:45:15 +01001487 on = !!on; /* 1 or 0 not bitmask */
1488 return send_cmd(port->port->serial->dev,
1489 feature, (__u8)(UMPM_UART1_PORT + port_number),
1490 on, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491}
1492
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Alan Cox2742fd82008-04-29 14:45:15 +01001494static int restore_mcr(struct edgeport_port *port, __u8 mcr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
1496 int status = 0;
1497
Alan Cox2742fd82008-04-29 14:45:15 +01001498 dbg("%s - %x", __func__, mcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Alan Cox2742fd82008-04-29 14:45:15 +01001500 status = ti_do_config(port, UMPC_SET_CLR_DTR, mcr & MCR_DTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 if (status)
1502 return status;
Alan Cox2742fd82008-04-29 14:45:15 +01001503 status = ti_do_config(port, UMPC_SET_CLR_RTS, mcr & MCR_RTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 if (status)
1505 return status;
Alan Cox2742fd82008-04-29 14:45:15 +01001506 return ti_do_config(port, UMPC_SET_CLR_LOOPBACK, mcr & MCR_LOOPBACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507}
1508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509/* Convert TI LSR to standard UART flags */
Alan Cox2742fd82008-04-29 14:45:15 +01001510static __u8 map_line_status(__u8 ti_lsr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511{
1512 __u8 lsr = 0;
1513
1514#define MAP_FLAG(flagUmp, flagUart) \
1515 if (ti_lsr & flagUmp) \
1516 lsr |= flagUart;
1517
1518 MAP_FLAG(UMP_UART_LSR_OV_MASK, LSR_OVER_ERR) /* overrun */
1519 MAP_FLAG(UMP_UART_LSR_PE_MASK, LSR_PAR_ERR) /* parity error */
1520 MAP_FLAG(UMP_UART_LSR_FE_MASK, LSR_FRM_ERR) /* framing error */
1521 MAP_FLAG(UMP_UART_LSR_BR_MASK, LSR_BREAK) /* break detected */
Alan Cox2742fd82008-04-29 14:45:15 +01001522 MAP_FLAG(UMP_UART_LSR_RX_MASK, LSR_RX_AVAIL) /* rx data available */
1523 MAP_FLAG(UMP_UART_LSR_TX_MASK, LSR_TX_EMPTY) /* tx hold reg empty */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
1525#undef MAP_FLAG
1526
1527 return lsr;
1528}
1529
Alan Cox2742fd82008-04-29 14:45:15 +01001530static void handle_new_msr(struct edgeport_port *edge_port, __u8 msr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
1532 struct async_icount *icount;
1533 struct tty_struct *tty;
1534
Alan Cox2742fd82008-04-29 14:45:15 +01001535 dbg("%s - %02x", __func__, msr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Alan Cox2742fd82008-04-29 14:45:15 +01001537 if (msr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
1538 EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 icount = &edge_port->icount;
1540
1541 /* update input line counters */
1542 if (msr & EDGEPORT_MSR_DELTA_CTS)
1543 icount->cts++;
1544 if (msr & EDGEPORT_MSR_DELTA_DSR)
1545 icount->dsr++;
1546 if (msr & EDGEPORT_MSR_DELTA_CD)
1547 icount->dcd++;
1548 if (msr & EDGEPORT_MSR_DELTA_RI)
1549 icount->rng++;
Alan Cox2742fd82008-04-29 14:45:15 +01001550 wake_up_interruptible(&edge_port->delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 }
1552
1553 /* Save the new modem status */
1554 edge_port->shadow_msr = msr & 0xf0;
1555
Alan Cox4a90f092008-10-13 10:39:46 +01001556 tty = tty_port_tty_get(&edge_port->port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 /* handle CTS flow control */
1558 if (tty && C_CRTSCTS(tty)) {
1559 if (msr & EDGEPORT_MSR_CTS) {
1560 tty->hw_stopped = 0;
1561 tty_wakeup(tty);
1562 } else {
1563 tty->hw_stopped = 1;
1564 }
1565 }
Alan Cox4a90f092008-10-13 10:39:46 +01001566 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567}
1568
Alan Cox2742fd82008-04-29 14:45:15 +01001569static void handle_new_lsr(struct edgeport_port *edge_port, int lsr_data,
1570 __u8 lsr, __u8 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
1572 struct async_icount *icount;
Alan Cox2742fd82008-04-29 14:45:15 +01001573 __u8 new_lsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR |
1574 LSR_FRM_ERR | LSR_BREAK));
Alan Cox4a90f092008-10-13 10:39:46 +01001575 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Alan Cox2742fd82008-04-29 14:45:15 +01001577 dbg("%s - %02x", __func__, new_lsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
1579 edge_port->shadow_lsr = lsr;
1580
Alan Cox2742fd82008-04-29 14:45:15 +01001581 if (new_lsr & LSR_BREAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 /*
1583 * Parity and Framing errors only count if they
1584 * occur exclusive of a break being received.
1585 */
1586 new_lsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
1588 /* Place LSR data byte into Rx buffer */
Alan Cox4a90f092008-10-13 10:39:46 +01001589 if (lsr_data) {
1590 tty = tty_port_tty_get(&edge_port->port->port);
1591 if (tty) {
1592 edge_tty_recv(&edge_port->port->dev, tty, &data, 1);
1593 tty_kref_put(tty);
1594 }
1595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597 /* update input line counters */
1598 icount = &edge_port->icount;
1599 if (new_lsr & LSR_BREAK)
1600 icount->brk++;
1601 if (new_lsr & LSR_OVER_ERR)
1602 icount->overrun++;
1603 if (new_lsr & LSR_PAR_ERR)
1604 icount->parity++;
1605 if (new_lsr & LSR_FRM_ERR)
1606 icount->frame++;
1607}
1608
1609
Alan Cox2742fd82008-04-29 14:45:15 +01001610static void edge_interrupt_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611{
Ming Leicdc97792008-02-24 18:41:47 +08001612 struct edgeport_serial *edge_serial = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 struct usb_serial_port *port;
1614 struct edgeport_port *edge_port;
1615 unsigned char *data = urb->transfer_buffer;
1616 int length = urb->actual_length;
1617 int port_number;
1618 int function;
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001619 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 __u8 lsr;
1621 __u8 msr;
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001622 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Harvey Harrison441b62c2008-03-03 16:08:34 -08001624 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001626 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 case 0:
1628 /* success */
1629 break;
1630 case -ECONNRESET:
1631 case -ENOENT:
1632 case -ESHUTDOWN:
1633 /* this urb is terminated, clean up */
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001634 dbg("%s - urb shutting down with status: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001635 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 return;
1637 default:
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001638 dev_err(&urb->dev->dev, "%s - nonzero urb status received: "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001639 "%d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 goto exit;
1641 }
1642
1643 if (!length) {
Alan Cox2742fd82008-04-29 14:45:15 +01001644 dbg("%s - no data in urb", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 goto exit;
1646 }
1647
Alan Cox2742fd82008-04-29 14:45:15 +01001648 usb_serial_debug_data(debug, &edge_serial->serial->dev->dev,
1649 __func__, length, data);
1650
1651 if (length != 2) {
1652 dbg("%s - expecting packet of size 2, got %d",
1653 __func__, length);
1654 goto exit;
1655 }
1656
1657 port_number = TIUMP_GET_PORT_FROM_CODE(data[0]);
1658 function = TIUMP_GET_FUNC_FROM_CODE(data[0]);
1659 dbg("%s - port_number %d, function %d, info 0x%x",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001660 __func__, port_number, function, data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 port = edge_serial->serial->port[port_number];
1662 edge_port = usb_get_serial_port_data(port);
1663 if (!edge_port) {
Alan Cox2742fd82008-04-29 14:45:15 +01001664 dbg("%s - edge_port not found", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 return;
1666 }
1667 switch (function) {
1668 case TIUMP_INTERRUPT_CODE_LSR:
Alan Cox2742fd82008-04-29 14:45:15 +01001669 lsr = map_line_status(data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 if (lsr & UMP_UART_LSR_DATA_MASK) {
Alan Cox2742fd82008-04-29 14:45:15 +01001671 /* Save the LSR event for bulk read
1672 completion routine */
1673 dbg("%s - LSR Event Port %u LSR Status = %02x",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001674 __func__, port_number, lsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 edge_port->lsr_event = 1;
1676 edge_port->lsr_mask = lsr;
1677 } else {
Alan Cox2742fd82008-04-29 14:45:15 +01001678 dbg("%s - ===== Port %d LSR Status = %02x ======",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001679 __func__, port_number, lsr);
Alan Cox2742fd82008-04-29 14:45:15 +01001680 handle_new_lsr(edge_port, 0, lsr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 }
1682 break;
1683
Alan Cox2742fd82008-04-29 14:45:15 +01001684 case TIUMP_INTERRUPT_CODE_MSR: /* MSR */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 /* Copy MSR from UMP */
1686 msr = data[1];
Joe Perches759f3632010-02-05 16:50:08 -08001687 dbg("%s - ===== Port %u MSR Status = %02x ======",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001688 __func__, port_number, msr);
Alan Cox2742fd82008-04-29 14:45:15 +01001689 handle_new_msr(edge_port, msr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 break;
1691
1692 default:
Alan Cox2742fd82008-04-29 14:45:15 +01001693 dev_err(&urb->dev->dev,
1694 "%s - Unknown Interrupt code from UMP %x\n",
1695 __func__, data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 break;
Alan Cox2742fd82008-04-29 14:45:15 +01001697
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 }
1699
1700exit:
Alan Cox2742fd82008-04-29 14:45:15 +01001701 retval = usb_submit_urb(urb, GFP_ATOMIC);
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001702 if (retval)
Alan Cox2742fd82008-04-29 14:45:15 +01001703 dev_err(&urb->dev->dev,
1704 "%s - usb_submit_urb failed with result %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001705 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706}
1707
Alan Cox2742fd82008-04-29 14:45:15 +01001708static void edge_bulk_in_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709{
Ming Leicdc97792008-02-24 18:41:47 +08001710 struct edgeport_port *edge_port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 unsigned char *data = urb->transfer_buffer;
1712 struct tty_struct *tty;
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001713 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 int port_number;
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001715 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Harvey Harrison441b62c2008-03-03 16:08:34 -08001717 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001719 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 case 0:
1721 /* success */
1722 break;
1723 case -ECONNRESET:
1724 case -ENOENT:
1725 case -ESHUTDOWN:
1726 /* this urb is terminated, clean up */
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001727 dbg("%s - urb shutting down with status: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001728 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 return;
1730 default:
Alan Cox2742fd82008-04-29 14:45:15 +01001731 dev_err(&urb->dev->dev,
1732 "%s - nonzero read bulk status received: %d\n",
1733 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 }
1735
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001736 if (status == -EPIPE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 goto exit;
1738
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001739 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001740 dev_err(&urb->dev->dev, "%s - stopping read!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 return;
1742 }
1743
1744 port_number = edge_port->port->number - edge_port->port->serial->minor;
1745
1746 if (edge_port->lsr_event) {
1747 edge_port->lsr_event = 0;
Alan Cox2742fd82008-04-29 14:45:15 +01001748 dbg("%s ===== Port %u LSR Status = %02x, Data = %02x ======",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001749 __func__, port_number, edge_port->lsr_mask, *data);
Alan Cox2742fd82008-04-29 14:45:15 +01001750 handle_new_lsr(edge_port, 1, edge_port->lsr_mask, *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 /* Adjust buffer length/pointer */
1752 --urb->actual_length;
1753 ++data;
1754 }
1755
Alan Cox4a90f092008-10-13 10:39:46 +01001756 tty = tty_port_tty_get(&edge_port->port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 if (tty && urb->actual_length) {
Alan Cox2742fd82008-04-29 14:45:15 +01001758 usb_serial_debug_data(debug, &edge_port->port->dev,
1759 __func__, urb->actual_length, data);
1760 if (edge_port->close_pending)
1761 dbg("%s - close pending, dropping data on the floor",
1762 __func__);
1763 else
1764 edge_tty_recv(&edge_port->port->dev, tty, data,
1765 urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 edge_port->icount.rx += urb->actual_length;
1767 }
Alan Cox4a90f092008-10-13 10:39:46 +01001768 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
1770exit:
1771 /* continue read unless stopped */
1772 spin_lock(&edge_port->ep_lock);
Johan Hovold58330412011-11-06 19:06:28 +01001773 if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001774 retval = usb_submit_urb(urb, GFP_ATOMIC);
Johan Hovold58330412011-11-06 19:06:28 +01001775 else if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPED;
Johan Hovold58330412011-11-06 19:06:28 +01001777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 spin_unlock(&edge_port->ep_lock);
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001779 if (retval)
Alan Cox2742fd82008-04-29 14:45:15 +01001780 dev_err(&urb->dev->dev,
1781 "%s - usb_submit_urb failed with result %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001782 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783}
1784
Alan Cox2742fd82008-04-29 14:45:15 +01001785static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
1786 unsigned char *data, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
Alan Cox2742fd82008-04-29 14:45:15 +01001788 int queued;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Alan Cox2742fd82008-04-29 14:45:15 +01001790 queued = tty_insert_flip_string(tty, data, length);
1791 if (queued < length)
1792 dev_err(dev, "%s - dropping data, %d bytes lost\n",
1793 __func__, length - queued);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 tty_flip_buffer_push(tty);
1795}
1796
Alan Cox2742fd82008-04-29 14:45:15 +01001797static void edge_bulk_out_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798{
Ming Leicdc97792008-02-24 18:41:47 +08001799 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001801 int status = urb->status;
Alan Cox4a90f092008-10-13 10:39:46 +01001802 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
Alan Cox2742fd82008-04-29 14:45:15 +01001804 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
1806 edge_port->ep_write_urb_in_use = 0;
1807
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001808 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 case 0:
1810 /* success */
1811 break;
1812 case -ECONNRESET:
1813 case -ENOENT:
1814 case -ESHUTDOWN:
1815 /* this urb is terminated, clean up */
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001816 dbg("%s - urb shutting down with status: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001817 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 return;
1819 default:
Johan Hovold22a416c2012-02-10 13:20:51 +01001820 dev_err_console(port, "%s - nonzero write bulk status "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001821 "received: %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 }
1823
1824 /* send any buffered data */
Alan Cox4a90f092008-10-13 10:39:46 +01001825 tty = tty_port_tty_get(&port->port);
1826 edge_send(tty);
1827 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828}
1829
Alan Coxa509a7e2009-09-19 13:13:26 -07001830static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831{
1832 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1833 struct edgeport_serial *edge_serial;
1834 struct usb_device *dev;
1835 struct urb *urb;
1836 int port_number;
1837 int status;
1838 u16 open_settings;
1839 u8 transaction_timeout;
1840
Harvey Harrison441b62c2008-03-03 16:08:34 -08001841 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
1843 if (edge_port == NULL)
1844 return -ENODEV;
1845
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 port_number = port->number - port->serial->minor;
1847 switch (port_number) {
Alan Cox2742fd82008-04-29 14:45:15 +01001848 case 0:
1849 edge_port->uart_base = UMPMEM_BASE_UART1;
1850 edge_port->dma_address = UMPD_OEDB1_ADDRESS;
1851 break;
1852 case 1:
1853 edge_port->uart_base = UMPMEM_BASE_UART2;
1854 edge_port->dma_address = UMPD_OEDB2_ADDRESS;
1855 break;
1856 default:
1857 dev_err(&port->dev, "Unknown port number!!!\n");
1858 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 }
1860
Alan Cox2742fd82008-04-29 14:45:15 +01001861 dbg("%s - port_number = %d, uart_base = %04x, dma_address = %04x",
1862 __func__, port_number, edge_port->uart_base,
1863 edge_port->dma_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
1865 dev = port->serial->dev;
1866
Alan Cox2742fd82008-04-29 14:45:15 +01001867 memset(&(edge_port->icount), 0x00, sizeof(edge_port->icount));
1868 init_waitqueue_head(&edge_port->delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
1870 /* turn off loopback */
Alan Cox2742fd82008-04-29 14:45:15 +01001871 status = ti_do_config(edge_port, UMPC_SET_CLR_LOOPBACK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001873 dev_err(&port->dev,
1874 "%s - cannot send clear loopback command, %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001875 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 return status;
1877 }
Alan Cox2742fd82008-04-29 14:45:15 +01001878
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 /* set up the port settings */
Alan Cox95da3102008-07-22 11:09:07 +01001880 if (tty)
Alan Cox4a90f092008-10-13 10:39:46 +01001881 edge_set_termios(tty, port, tty->termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
1883 /* open up the port */
1884
1885 /* milliseconds to timeout for DMA transfer */
1886 transaction_timeout = 2;
1887
Alan Cox2742fd82008-04-29 14:45:15 +01001888 edge_port->ump_read_timeout =
1889 max(20, ((transaction_timeout * 3) / 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
Alan Cox2742fd82008-04-29 14:45:15 +01001891 /* milliseconds to timeout for DMA transfer */
1892 open_settings = (u8)(UMP_DMA_MODE_CONTINOUS |
1893 UMP_PIPE_TRANS_TIMEOUT_ENA |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 (transaction_timeout << 2));
1895
Alan Cox2742fd82008-04-29 14:45:15 +01001896 dbg("%s - Sending UMPC_OPEN_PORT", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
1898 /* Tell TI to open and start the port */
Alan Cox2742fd82008-04-29 14:45:15 +01001899 status = send_cmd(dev, UMPC_OPEN_PORT,
1900 (u8)(UMPM_UART1_PORT + port_number), open_settings, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001902 dev_err(&port->dev, "%s - cannot send open command, %d\n",
1903 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 return status;
1905 }
1906
1907 /* Start the DMA? */
Alan Cox2742fd82008-04-29 14:45:15 +01001908 status = send_cmd(dev, UMPC_START_PORT,
1909 (u8)(UMPM_UART1_PORT + port_number), 0, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001911 dev_err(&port->dev, "%s - cannot send start DMA command, %d\n",
1912 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 return status;
1914 }
1915
1916 /* Clear TX and RX buffers in UMP */
Alan Cox2742fd82008-04-29 14:45:15 +01001917 status = purge_port(port, UMP_PORT_DIR_OUT | UMP_PORT_DIR_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001919 dev_err(&port->dev,
1920 "%s - cannot send clear buffers command, %d\n",
1921 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 return status;
1923 }
1924
1925 /* Read Initial MSR */
Alan Cox2742fd82008-04-29 14:45:15 +01001926 status = ti_vread_sync(dev, UMPC_READ_MSR, 0,
1927 (__u16)(UMPM_UART1_PORT + port_number),
1928 &edge_port->shadow_msr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001930 dev_err(&port->dev, "%s - cannot send read MSR command, %d\n",
1931 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 return status;
1933 }
1934
Alan Cox2742fd82008-04-29 14:45:15 +01001935 dbg("ShadowMSR 0x%X", edge_port->shadow_msr);
1936
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 /* Set Initial MCR */
1938 edge_port->shadow_mcr = MCR_RTS | MCR_DTR;
Alan Cox2742fd82008-04-29 14:45:15 +01001939 dbg("ShadowMCR 0x%X", edge_port->shadow_mcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
1941 edge_serial = edge_port->edge_serial;
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01001942 if (mutex_lock_interruptible(&edge_serial->es_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 return -ERESTARTSYS;
1944 if (edge_serial->num_ports_open == 0) {
Alan Cox2742fd82008-04-29 14:45:15 +01001945 /* we are the first port to open, post the interrupt urb */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 urb = edge_serial->serial->port[0]->interrupt_in_urb;
1947 if (!urb) {
Alan Cox2742fd82008-04-29 14:45:15 +01001948 dev_err(&port->dev,
1949 "%s - no interrupt urb present, exiting\n",
1950 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 status = -EINVAL;
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01001952 goto release_es_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 urb->context = edge_serial;
Alan Cox2742fd82008-04-29 14:45:15 +01001955 status = usb_submit_urb(urb, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001957 dev_err(&port->dev,
1958 "%s - usb_submit_urb failed with value %d\n",
1959 __func__, status);
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01001960 goto release_es_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 }
1962 }
1963
1964 /*
1965 * reset the data toggle on the bulk endpoints to work around bug in
1966 * host controllers where things get out of sync some times
1967 */
Alan Cox2742fd82008-04-29 14:45:15 +01001968 usb_clear_halt(dev, port->write_urb->pipe);
1969 usb_clear_halt(dev, port->read_urb->pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
1971 /* start up our bulk read urb */
1972 urb = port->read_urb;
1973 if (!urb) {
Alan Cox2742fd82008-04-29 14:45:15 +01001974 dev_err(&port->dev, "%s - no read urb present, exiting\n",
1975 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 status = -EINVAL;
1977 goto unlink_int_urb;
1978 }
1979 edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 urb->context = edge_port;
Alan Cox2742fd82008-04-29 14:45:15 +01001981 status = usb_submit_urb(urb, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001983 dev_err(&port->dev,
1984 "%s - read bulk usb_submit_urb failed with value %d\n",
1985 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 goto unlink_int_urb;
1987 }
1988
1989 ++edge_serial->num_ports_open;
1990
Harvey Harrison441b62c2008-03-03 16:08:34 -08001991 dbg("%s - exited", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01001993 goto release_es_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
1995unlink_int_urb:
1996 if (edge_port->edge_serial->num_ports_open == 0)
1997 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01001998release_es_lock:
1999 mutex_unlock(&edge_serial->es_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 return status;
2001}
2002
Alan Cox335f8512009-06-11 12:26:29 +01002003static void edge_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004{
2005 struct edgeport_serial *edge_serial;
2006 struct edgeport_port *edge_port;
2007 int port_number;
2008 int status;
2009
Harvey Harrison441b62c2008-03-03 16:08:34 -08002010 dbg("%s - port %d", __func__, port->number);
Alan Cox2742fd82008-04-29 14:45:15 +01002011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 edge_serial = usb_get_serial_data(port->serial);
2013 edge_port = usb_get_serial_port_data(port);
Alan Cox2742fd82008-04-29 14:45:15 +01002014 if (edge_serial == NULL || edge_port == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 return;
Alan Cox2742fd82008-04-29 14:45:15 +01002016
2017 /* The bulkreadcompletion routine will check
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 * this flag and dump add read data */
2019 edge_port->close_pending = 1;
2020
2021 /* chase the port close and flush */
Alan Cox2742fd82008-04-29 14:45:15 +01002022 chase_port(edge_port, (HZ * closing_wait) / 100, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
2024 usb_kill_urb(port->read_urb);
2025 usb_kill_urb(port->write_urb);
2026 edge_port->ep_write_urb_in_use = 0;
2027
2028 /* assuming we can still talk to the device,
2029 * send a close port command to it */
Harvey Harrison441b62c2008-03-03 16:08:34 -08002030 dbg("%s - send umpc_close_port", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 port_number = port->number - port->serial->minor;
Alan Cox2742fd82008-04-29 14:45:15 +01002032 status = send_cmd(port->serial->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 UMPC_CLOSE_PORT,
2034 (__u8)(UMPM_UART1_PORT + port_number),
2035 0,
2036 NULL,
2037 0);
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01002038 mutex_lock(&edge_serial->es_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 --edge_port->edge_serial->num_ports_open;
2040 if (edge_port->edge_serial->num_ports_open <= 0) {
2041 /* last port is now closed, let's shut down our interrupt urb */
2042 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
2043 edge_port->edge_serial->num_ports_open = 0;
2044 }
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01002045 mutex_unlock(&edge_serial->es_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 edge_port->close_pending = 0;
2047
Harvey Harrison441b62c2008-03-03 16:08:34 -08002048 dbg("%s - exited", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049}
2050
Alan Cox95da3102008-07-22 11:09:07 +01002051static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
2052 const unsigned char *data, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053{
2054 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Harvey Harrison441b62c2008-03-03 16:08:34 -08002056 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
2058 if (count == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002059 dbg("%s - write request of 0 bytes", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 return 0;
2061 }
2062
2063 if (edge_port == NULL)
2064 return -ENODEV;
2065 if (edge_port->close_pending == 1)
2066 return -ENODEV;
2067
Johan Hovoldd733cec2010-05-19 00:01:37 +02002068 count = kfifo_in_locked(&edge_port->write_fifo, data, count,
2069 &edge_port->ep_lock);
Alan Cox95da3102008-07-22 11:09:07 +01002070 edge_send(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
2072 return count;
2073}
2074
Alan Cox95da3102008-07-22 11:09:07 +01002075static void edge_send(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076{
Alan Cox95da3102008-07-22 11:09:07 +01002077 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 int count, result;
2079 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 unsigned long flags;
2081
2082
Harvey Harrison441b62c2008-03-03 16:08:34 -08002083 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
2085 spin_lock_irqsave(&edge_port->ep_lock, flags);
2086
2087 if (edge_port->ep_write_urb_in_use) {
2088 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2089 return;
2090 }
2091
Johan Hovoldd733cec2010-05-19 00:01:37 +02002092 count = kfifo_out(&edge_port->write_fifo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 port->write_urb->transfer_buffer,
2094 port->bulk_out_size);
2095
2096 if (count == 0) {
2097 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2098 return;
2099 }
2100
2101 edge_port->ep_write_urb_in_use = 1;
2102
2103 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2104
Alan Cox2742fd82008-04-29 14:45:15 +01002105 usb_serial_debug_data(debug, &port->dev, __func__, count,
2106 port->write_urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
2108 /* set up our urb */
Johan Hovoldfd119612011-11-06 19:06:30 +01002109 port->write_urb->transfer_buffer_length = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
2111 /* send the data out the bulk port */
2112 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
2113 if (result) {
Johan Hovold22a416c2012-02-10 13:20:51 +01002114 dev_err_console(port,
Alan Cox2742fd82008-04-29 14:45:15 +01002115 "%s - failed submitting write urb, error %d\n",
2116 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 edge_port->ep_write_urb_in_use = 0;
Alan Cox2742fd82008-04-29 14:45:15 +01002118 /* TODO: reschedule edge_send */
2119 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 edge_port->icount.tx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
2122 /* wakeup any process waiting for writes to complete */
2123 /* there is now more room in the buffer for new writes */
Alan Cox2742fd82008-04-29 14:45:15 +01002124 if (tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126}
2127
Alan Cox95da3102008-07-22 11:09:07 +01002128static int edge_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129{
Alan Cox95da3102008-07-22 11:09:07 +01002130 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2132 int room = 0;
2133 unsigned long flags;
2134
Harvey Harrison441b62c2008-03-03 16:08:34 -08002135 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
2137 if (edge_port == NULL)
Alan Cox2742fd82008-04-29 14:45:15 +01002138 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 if (edge_port->close_pending == 1)
Alan Cox2742fd82008-04-29 14:45:15 +01002140 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
2142 spin_lock_irqsave(&edge_port->ep_lock, flags);
Johan Hovoldd733cec2010-05-19 00:01:37 +02002143 room = kfifo_avail(&edge_port->write_fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2145
Harvey Harrison441b62c2008-03-03 16:08:34 -08002146 dbg("%s - returns %d", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 return room;
2148}
2149
Alan Cox95da3102008-07-22 11:09:07 +01002150static int edge_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151{
Alan Cox95da3102008-07-22 11:09:07 +01002152 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2154 int chars = 0;
2155 unsigned long flags;
2156
Harvey Harrison441b62c2008-03-03 16:08:34 -08002157 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
2159 if (edge_port == NULL)
Alan Cox2742fd82008-04-29 14:45:15 +01002160 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 if (edge_port->close_pending == 1)
Alan Cox2742fd82008-04-29 14:45:15 +01002162 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
2164 spin_lock_irqsave(&edge_port->ep_lock, flags);
Johan Hovoldd733cec2010-05-19 00:01:37 +02002165 chars = kfifo_len(&edge_port->write_fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2167
Alan Cox2742fd82008-04-29 14:45:15 +01002168 dbg("%s - returns %d", __func__, chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 return chars;
2170}
2171
Alan Cox95da3102008-07-22 11:09:07 +01002172static void edge_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173{
Alan Cox95da3102008-07-22 11:09:07 +01002174 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 int status;
2177
Harvey Harrison441b62c2008-03-03 16:08:34 -08002178 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
2180 if (edge_port == NULL)
2181 return;
2182
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 /* if we are implementing XON/XOFF, send the stop character */
2184 if (I_IXOFF(tty)) {
2185 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01002186 status = edge_write(tty, port, &stop_char, 1);
2187 if (status <= 0) {
2188 dev_err(&port->dev, "%s - failed to write stop character, %d\n", __func__, status);
2189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 }
2191
2192 /* if we are implementing RTS/CTS, stop reads */
2193 /* and the Edgeport will clear the RTS line */
2194 if (C_CRTSCTS(tty))
2195 stop_read(edge_port);
2196
2197}
2198
Alan Cox95da3102008-07-22 11:09:07 +01002199static void edge_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200{
Alan Cox95da3102008-07-22 11:09:07 +01002201 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 int status;
2204
Harvey Harrison441b62c2008-03-03 16:08:34 -08002205 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
2207 if (edge_port == NULL)
2208 return;
2209
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 /* if we are implementing XON/XOFF, send the start character */
2211 if (I_IXOFF(tty)) {
2212 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01002213 status = edge_write(tty, port, &start_char, 1);
2214 if (status <= 0) {
2215 dev_err(&port->dev, "%s - failed to write start character, %d\n", __func__, status);
2216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 /* if we are implementing RTS/CTS, restart reads */
2219 /* are the Edgeport will assert the RTS line */
2220 if (C_CRTSCTS(tty)) {
2221 status = restart_read(edge_port);
2222 if (status)
Alan Cox2742fd82008-04-29 14:45:15 +01002223 dev_err(&port->dev,
2224 "%s - read bulk usb_submit_urb failed: %d\n",
2225 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 }
2227
2228}
2229
2230static void stop_read(struct edgeport_port *edge_port)
2231{
2232 unsigned long flags;
2233
2234 spin_lock_irqsave(&edge_port->ep_lock, flags);
2235
2236 if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
2237 edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPING;
2238 edge_port->shadow_mcr &= ~MCR_RTS;
2239
2240 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2241}
2242
2243static int restart_read(struct edgeport_port *edge_port)
2244{
2245 struct urb *urb;
2246 int status = 0;
2247 unsigned long flags;
2248
2249 spin_lock_irqsave(&edge_port->ep_lock, flags);
2250
2251 if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPED) {
2252 urb = edge_port->port->read_urb;
Oliver Neukumefdff602007-06-05 10:50:48 +02002253 status = usb_submit_urb(urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 }
2255 edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
2256 edge_port->shadow_mcr |= MCR_RTS;
2257
2258 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2259
2260 return status;
2261}
2262
Alan Cox95da3102008-07-22 11:09:07 +01002263static void change_port_settings(struct tty_struct *tty,
2264 struct edgeport_port *edge_port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265{
2266 struct ump_uart_config *config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 int baud;
2268 unsigned cflag;
2269 int status;
Alan Cox2742fd82008-04-29 14:45:15 +01002270 int port_number = edge_port->port->number -
2271 edge_port->port->serial->minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
Harvey Harrison441b62c2008-03-03 16:08:34 -08002273 dbg("%s - port %d", __func__, edge_port->port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
Alan Cox95da3102008-07-22 11:09:07 +01002275 config = kmalloc (sizeof (*config), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 if (!config) {
Alan Cox2742fd82008-04-29 14:45:15 +01002277 *tty->termios = *old_termios;
2278 dev_err(&edge_port->port->dev, "%s - out of memory\n",
2279 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 return;
2281 }
2282
2283 cflag = tty->termios->c_cflag;
2284
2285 config->wFlags = 0;
2286
2287 /* These flags must be set */
2288 config->wFlags |= UMP_MASK_UART_FLAGS_RECEIVE_MS_INT;
2289 config->wFlags |= UMP_MASK_UART_FLAGS_AUTO_START_ON_ERR;
2290 config->bUartMode = (__u8)(edge_port->bUartMode);
2291
2292 switch (cflag & CSIZE) {
Alan Cox2742fd82008-04-29 14:45:15 +01002293 case CS5:
2294 config->bDataBits = UMP_UART_CHAR5BITS;
2295 dbg("%s - data bits = 5", __func__);
2296 break;
2297 case CS6:
2298 config->bDataBits = UMP_UART_CHAR6BITS;
2299 dbg("%s - data bits = 6", __func__);
2300 break;
2301 case CS7:
2302 config->bDataBits = UMP_UART_CHAR7BITS;
2303 dbg("%s - data bits = 7", __func__);
2304 break;
2305 default:
2306 case CS8:
2307 config->bDataBits = UMP_UART_CHAR8BITS;
2308 dbg("%s - data bits = 8", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 break;
2310 }
2311
2312 if (cflag & PARENB) {
2313 if (cflag & PARODD) {
2314 config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
2315 config->bParity = UMP_UART_ODDPARITY;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002316 dbg("%s - parity = odd", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 } else {
2318 config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
2319 config->bParity = UMP_UART_EVENPARITY;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002320 dbg("%s - parity = even", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 }
2322 } else {
Alan Cox2742fd82008-04-29 14:45:15 +01002323 config->bParity = UMP_UART_NOPARITY;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002324 dbg("%s - parity = none", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 }
2326
2327 if (cflag & CSTOPB) {
2328 config->bStopBits = UMP_UART_STOPBIT2;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002329 dbg("%s - stop bits = 2", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 } else {
2331 config->bStopBits = UMP_UART_STOPBIT1;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002332 dbg("%s - stop bits = 1", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 }
2334
2335 /* figure out the flow control settings */
2336 if (cflag & CRTSCTS) {
2337 config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X_CTS_FLOW;
2338 config->wFlags |= UMP_MASK_UART_FLAGS_RTS_FLOW;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002339 dbg("%s - RTS/CTS is enabled", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002341 dbg("%s - RTS/CTS is disabled", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 tty->hw_stopped = 0;
2343 restart_read(edge_port);
2344 }
2345
Alan Cox2742fd82008-04-29 14:45:15 +01002346 /* if we are implementing XON/XOFF, set the start and stop
2347 character in the device */
2348 config->cXon = START_CHAR(tty);
2349 config->cXoff = STOP_CHAR(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
Alan Cox2742fd82008-04-29 14:45:15 +01002351 /* if we are implementing INBOUND XON/XOFF */
2352 if (I_IXOFF(tty)) {
2353 config->wFlags |= UMP_MASK_UART_FLAGS_IN_X;
2354 dbg("%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2355 __func__, config->cXon, config->cXoff);
2356 } else
2357 dbg("%s - INBOUND XON/XOFF is disabled", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
Alan Cox2742fd82008-04-29 14:45:15 +01002359 /* if we are implementing OUTBOUND XON/XOFF */
2360 if (I_IXON(tty)) {
2361 config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X;
2362 dbg("%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2363 __func__, config->cXon, config->cXoff);
2364 } else
2365 dbg("%s - OUTBOUND XON/XOFF is disabled", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
Alan Coxd5f5bcd2008-01-03 16:57:33 +00002367 tty->termios->c_cflag &= ~CMSPAR;
2368
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 /* Round the baud rate */
2370 baud = tty_get_baud_rate(tty);
2371 if (!baud) {
2372 /* pick a default, any default... */
2373 baud = 9600;
Alan Coxd5f5bcd2008-01-03 16:57:33 +00002374 } else
2375 tty_encode_baud_rate(tty, baud, baud);
2376
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 edge_port->baud_rate = baud;
2378 config->wBaudRate = (__u16)((461550L + baud/2) / baud);
2379
Alan Coxd5f5bcd2008-01-03 16:57:33 +00002380 /* FIXME: Recompute actual baud from divisor here */
2381
Alan Cox2742fd82008-04-29 14:45:15 +01002382 dbg("%s - baud rate = %d, wBaudRate = %d", __func__, baud,
2383 config->wBaudRate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
Alan Cox2742fd82008-04-29 14:45:15 +01002385 dbg("wBaudRate: %d", (int)(461550L / config->wBaudRate));
2386 dbg("wFlags: 0x%x", config->wFlags);
2387 dbg("bDataBits: %d", config->bDataBits);
2388 dbg("bParity: %d", config->bParity);
2389 dbg("bStopBits: %d", config->bStopBits);
2390 dbg("cXon: %d", config->cXon);
2391 dbg("cXoff: %d", config->cXoff);
2392 dbg("bUartMode: %d", config->bUartMode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
2394 /* move the word values into big endian mode */
Alan Cox2742fd82008-04-29 14:45:15 +01002395 cpu_to_be16s(&config->wFlags);
2396 cpu_to_be16s(&config->wBaudRate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
Alan Cox2742fd82008-04-29 14:45:15 +01002398 status = send_cmd(edge_port->port->serial->dev, UMPC_SET_CONFIG,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 (__u8)(UMPM_UART1_PORT + port_number),
Alan Cox2742fd82008-04-29 14:45:15 +01002400 0, (__u8 *)config, sizeof(*config));
2401 if (status)
2402 dbg("%s - error %d when trying to write config to device",
Harvey Harrison441b62c2008-03-03 16:08:34 -08002403 __func__, status);
Alan Cox2742fd82008-04-29 14:45:15 +01002404 kfree(config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405}
2406
Alan Cox2e0ddd62008-07-22 11:12:33 +01002407static void edge_set_termios(struct tty_struct *tty,
Alan Cox95da3102008-07-22 11:09:07 +01002408 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409{
2410 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Alan Cox95da3102008-07-22 11:09:07 +01002411 unsigned int cflag;
2412
2413 cflag = tty->termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
Harvey Harrison441b62c2008-03-03 16:08:34 -08002415 dbg("%s - clfag %08x iflag %08x", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 tty->termios->c_cflag, tty->termios->c_iflag);
Harvey Harrison441b62c2008-03-03 16:08:34 -08002417 dbg("%s - old clfag %08x old iflag %08x", __func__,
Alan Coxd5f5bcd2008-01-03 16:57:33 +00002418 old_termios->c_cflag, old_termios->c_iflag);
Harvey Harrison441b62c2008-03-03 16:08:34 -08002419 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
2421 if (edge_port == NULL)
2422 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 /* change the port settings to the new ones specified */
Alan Cox95da3102008-07-22 11:09:07 +01002424 change_port_settings(tty, edge_port, old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425}
2426
Alan Cox20b9d172011-02-14 16:26:50 +00002427static int edge_tiocmset(struct tty_struct *tty,
Alan Cox2742fd82008-04-29 14:45:15 +01002428 unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429{
Alan Cox95da3102008-07-22 11:09:07 +01002430 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2432 unsigned int mcr;
Alan Cox3d71fe02008-02-20 21:38:32 +00002433 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
Harvey Harrison441b62c2008-03-03 16:08:34 -08002435 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
Alan Cox3d71fe02008-02-20 21:38:32 +00002437 spin_lock_irqsave(&edge_port->ep_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 mcr = edge_port->shadow_mcr;
2439 if (set & TIOCM_RTS)
2440 mcr |= MCR_RTS;
2441 if (set & TIOCM_DTR)
2442 mcr |= MCR_DTR;
2443 if (set & TIOCM_LOOP)
2444 mcr |= MCR_LOOPBACK;
2445
2446 if (clear & TIOCM_RTS)
2447 mcr &= ~MCR_RTS;
2448 if (clear & TIOCM_DTR)
2449 mcr &= ~MCR_DTR;
2450 if (clear & TIOCM_LOOP)
2451 mcr &= ~MCR_LOOPBACK;
2452
2453 edge_port->shadow_mcr = mcr;
Alan Cox3d71fe02008-02-20 21:38:32 +00002454 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
Alan Cox2742fd82008-04-29 14:45:15 +01002456 restore_mcr(edge_port, mcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 return 0;
2458}
2459
Alan Cox60b33c12011-02-14 16:26:14 +00002460static int edge_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461{
Alan Cox95da3102008-07-22 11:09:07 +01002462 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2464 unsigned int result = 0;
2465 unsigned int msr;
2466 unsigned int mcr;
Alan Cox3d71fe02008-02-20 21:38:32 +00002467 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
Harvey Harrison441b62c2008-03-03 16:08:34 -08002469 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
Alan Cox3d71fe02008-02-20 21:38:32 +00002471 spin_lock_irqsave(&edge_port->ep_lock, flags);
2472
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 msr = edge_port->shadow_msr;
2474 mcr = edge_port->shadow_mcr;
2475 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
2476 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
2477 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
2478 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
2479 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
2480 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
2481
2482
Harvey Harrison441b62c2008-03-03 16:08:34 -08002483 dbg("%s -- %x", __func__, result);
Alan Cox3d71fe02008-02-20 21:38:32 +00002484 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
2486 return result;
2487}
2488
Alan Cox0bca1b92010-09-16 18:21:40 +01002489static int edge_get_icount(struct tty_struct *tty,
2490 struct serial_icounter_struct *icount)
2491{
2492 struct usb_serial_port *port = tty->driver_data;
2493 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2494 struct async_icount *ic = &edge_port->icount;
2495
2496 icount->cts = ic->cts;
2497 icount->dsr = ic->dsr;
2498 icount->rng = ic->rng;
2499 icount->dcd = ic->dcd;
2500 icount->tx = ic->tx;
2501 icount->rx = ic->rx;
2502 icount->frame = ic->frame;
2503 icount->parity = ic->parity;
2504 icount->overrun = ic->overrun;
2505 icount->brk = ic->brk;
2506 icount->buf_overrun = ic->buf_overrun;
2507 return 0;
2508}
2509
Alan Cox2742fd82008-04-29 14:45:15 +01002510static int get_serial_info(struct edgeport_port *edge_port,
2511 struct serial_struct __user *retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512{
2513 struct serial_struct tmp;
2514
2515 if (!retinfo)
2516 return -EFAULT;
2517
2518 memset(&tmp, 0, sizeof(tmp));
2519
2520 tmp.type = PORT_16550A;
2521 tmp.line = edge_port->port->serial->minor;
2522 tmp.port = edge_port->port->number;
2523 tmp.irq = 0;
2524 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2525 tmp.xmit_fifo_size = edge_port->port->bulk_out_size;
2526 tmp.baud_base = 9600;
2527 tmp.close_delay = 5*HZ;
2528 tmp.closing_wait = closing_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529
2530 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2531 return -EFAULT;
2532 return 0;
2533}
2534
Alan Cox00a0d0d2011-02-14 16:27:06 +00002535static int edge_ioctl(struct tty_struct *tty,
Alan Cox2742fd82008-04-29 14:45:15 +01002536 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537{
Alan Cox95da3102008-07-22 11:09:07 +01002538 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2540 struct async_icount cnow;
2541 struct async_icount cprev;
2542
Harvey Harrison441b62c2008-03-03 16:08:34 -08002543 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544
2545 switch (cmd) {
Alan Cox2742fd82008-04-29 14:45:15 +01002546 case TIOCGSERIAL:
2547 dbg("%s - (%d) TIOCGSERIAL", __func__, port->number);
2548 return get_serial_info(edge_port,
2549 (struct serial_struct __user *) arg);
2550 case TIOCMIWAIT:
2551 dbg("%s - (%d) TIOCMIWAIT", __func__, port->number);
2552 cprev = edge_port->icount;
2553 while (1) {
2554 interruptible_sleep_on(&edge_port->delta_msr_wait);
2555 /* see if a signal did it */
2556 if (signal_pending(current))
2557 return -ERESTARTSYS;
2558 cnow = edge_port->icount;
2559 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2560 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2561 return -EIO; /* no change => error */
2562 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2563 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2564 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2565 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2566 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 }
Alan Cox2742fd82008-04-29 14:45:15 +01002568 cprev = cnow;
2569 }
2570 /* not reached */
2571 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 return -ENOIOCTLCMD;
2574}
2575
Alan Cox95da3102008-07-22 11:09:07 +01002576static void edge_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577{
Alan Cox95da3102008-07-22 11:09:07 +01002578 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2580 int status;
Alan Cox2742fd82008-04-29 14:45:15 +01002581 int bv = 0; /* Off */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
Alan Cox95da3102008-07-22 11:09:07 +01002583 dbg("%s - state = %d", __func__, break_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
2585 /* chase the port close */
Alan Cox2742fd82008-04-29 14:45:15 +01002586 chase_port(edge_port, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
Alan Cox95da3102008-07-22 11:09:07 +01002588 if (break_state == -1)
Alan Cox2742fd82008-04-29 14:45:15 +01002589 bv = 1; /* On */
2590 status = ti_do_config(edge_port, UMPC_SET_CLR_BREAK, bv);
2591 if (status)
2592 dbg("%s - error %d sending break set/clear command.",
Harvey Harrison441b62c2008-03-03 16:08:34 -08002593 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594}
2595
Alan Cox2742fd82008-04-29 14:45:15 +01002596static int edge_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597{
2598 struct edgeport_serial *edge_serial;
2599 struct edgeport_port *edge_port;
2600 struct usb_device *dev;
2601 int status;
2602 int i;
2603
2604 dev = serial->dev;
2605
2606 /* create our private serial structure */
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +01002607 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 if (edge_serial == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002609 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 return -ENOMEM;
2611 }
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01002612 mutex_init(&edge_serial->es_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 edge_serial->serial = serial;
2614 usb_set_serial_data(serial, edge_serial);
2615
Alan Cox2742fd82008-04-29 14:45:15 +01002616 status = download_fw(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01002618 kfree(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 return status;
2620 }
2621
2622 /* set up our port private structures */
2623 for (i = 0; i < serial->num_ports; ++i) {
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +01002624 edge_port = kzalloc(sizeof(struct edgeport_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 if (edge_port == NULL) {
Alan Cox2742fd82008-04-29 14:45:15 +01002626 dev_err(&serial->dev->dev, "%s - Out of memory\n",
2627 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 goto cleanup;
2629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 spin_lock_init(&edge_port->ep_lock);
Johan Hovoldd733cec2010-05-19 00:01:37 +02002631 if (kfifo_alloc(&edge_port->write_fifo, EDGE_OUT_BUF_SIZE,
2632 GFP_KERNEL)) {
Alan Cox2742fd82008-04-29 14:45:15 +01002633 dev_err(&serial->dev->dev, "%s - Out of memory\n",
2634 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 kfree(edge_port);
2636 goto cleanup;
2637 }
2638 edge_port->port = serial->port[i];
2639 edge_port->edge_serial = edge_serial;
2640 usb_set_serial_port_data(serial->port[i], edge_port);
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002641 edge_port->bUartMode = default_uart_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 }
Alan Cox2742fd82008-04-29 14:45:15 +01002643
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 return 0;
2645
2646cleanup:
Alan Cox2742fd82008-04-29 14:45:15 +01002647 for (--i; i >= 0; --i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 edge_port = usb_get_serial_port_data(serial->port[i]);
Johan Hovoldd733cec2010-05-19 00:01:37 +02002649 kfifo_free(&edge_port->write_fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 kfree(edge_port);
2651 usb_set_serial_port_data(serial->port[i], NULL);
2652 }
Alan Cox2742fd82008-04-29 14:45:15 +01002653 kfree(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 usb_set_serial_data(serial, NULL);
2655 return -ENOMEM;
2656}
2657
Alan Sternf9c99bb2009-06-02 11:53:55 -04002658static void edge_disconnect(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659{
2660 int i;
2661 struct edgeport_port *edge_port;
2662
Alan Cox2742fd82008-04-29 14:45:15 +01002663 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
Jesper Juhl0d46c002007-07-16 22:17:25 +02002665 for (i = 0; i < serial->num_ports; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 edge_port = usb_get_serial_port_data(serial->port[i]);
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002667 edge_remove_sysfs_attrs(edge_port->port);
Alan Sternf9c99bb2009-06-02 11:53:55 -04002668 }
2669}
2670
2671static void edge_release(struct usb_serial *serial)
2672{
2673 int i;
2674 struct edgeport_port *edge_port;
2675
2676 dbg("%s", __func__);
2677
2678 for (i = 0; i < serial->num_ports; ++i) {
2679 edge_port = usb_get_serial_port_data(serial->port[i]);
Johan Hovoldd733cec2010-05-19 00:01:37 +02002680 kfifo_free(&edge_port->write_fifo);
Jesper Juhl0d46c002007-07-16 22:17:25 +02002681 kfree(edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 }
Jesper Juhl0d46c002007-07-16 22:17:25 +02002683 kfree(usb_get_serial_data(serial));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684}
2685
2686
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002687/* Sysfs Attributes */
2688
2689static ssize_t show_uart_mode(struct device *dev,
2690 struct device_attribute *attr, char *buf)
2691{
2692 struct usb_serial_port *port = to_usb_serial_port(dev);
2693 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2694
2695 return sprintf(buf, "%d\n", edge_port->bUartMode);
2696}
2697
2698static ssize_t store_uart_mode(struct device *dev,
2699 struct device_attribute *attr, const char *valbuf, size_t count)
2700{
2701 struct usb_serial_port *port = to_usb_serial_port(dev);
2702 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2703 unsigned int v = simple_strtoul(valbuf, NULL, 0);
2704
Harvey Harrison441b62c2008-03-03 16:08:34 -08002705 dbg("%s: setting uart_mode = %d", __func__, v);
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002706
2707 if (v < 256)
2708 edge_port->bUartMode = v;
2709 else
Harvey Harrison441b62c2008-03-03 16:08:34 -08002710 dev_err(dev, "%s - uart_mode %d is invalid\n", __func__, v);
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002711
2712 return count;
2713}
2714
Alan Cox2742fd82008-04-29 14:45:15 +01002715static DEVICE_ATTR(uart_mode, S_IWUSR | S_IRUGO, show_uart_mode,
2716 store_uart_mode);
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002717
2718static int edge_create_sysfs_attrs(struct usb_serial_port *port)
2719{
2720 return device_create_file(&port->dev, &dev_attr_uart_mode);
2721}
2722
2723static int edge_remove_sysfs_attrs(struct usb_serial_port *port)
2724{
2725 device_remove_file(&port->dev, &dev_attr_uart_mode);
2726 return 0;
2727}
2728
2729
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -07002730static struct usb_serial_driver edgeport_1port_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07002731 .driver = {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07002732 .owner = THIS_MODULE,
2733 .name = "edgeport_ti_1",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07002734 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07002735 .description = "Edgeport TI 1 port adapter",
Johannes Hölzld9b1b782006-12-17 21:50:24 +01002736 .usb_driver = &io_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 .id_table = edgeport_1port_id_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 .num_ports = 1,
2739 .open = edge_open,
2740 .close = edge_close,
2741 .throttle = edge_throttle,
2742 .unthrottle = edge_unthrottle,
2743 .attach = edge_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04002744 .disconnect = edge_disconnect,
2745 .release = edge_release,
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002746 .port_probe = edge_create_sysfs_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 .ioctl = edge_ioctl,
2748 .set_termios = edge_set_termios,
2749 .tiocmget = edge_tiocmget,
2750 .tiocmset = edge_tiocmset,
Alan Cox0bca1b92010-09-16 18:21:40 +01002751 .get_icount = edge_get_icount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 .write = edge_write,
2753 .write_room = edge_write_room,
2754 .chars_in_buffer = edge_chars_in_buffer,
2755 .break_ctl = edge_break,
2756 .read_int_callback = edge_interrupt_callback,
2757 .read_bulk_callback = edge_bulk_in_callback,
2758 .write_bulk_callback = edge_bulk_out_callback,
2759};
2760
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -07002761static struct usb_serial_driver edgeport_2port_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07002762 .driver = {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07002763 .owner = THIS_MODULE,
2764 .name = "edgeport_ti_2",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07002765 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07002766 .description = "Edgeport TI 2 port adapter",
Johannes Hölzld9b1b782006-12-17 21:50:24 +01002767 .usb_driver = &io_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 .id_table = edgeport_2port_id_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 .num_ports = 2,
2770 .open = edge_open,
2771 .close = edge_close,
2772 .throttle = edge_throttle,
2773 .unthrottle = edge_unthrottle,
2774 .attach = edge_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04002775 .disconnect = edge_disconnect,
2776 .release = edge_release,
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002777 .port_probe = edge_create_sysfs_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 .ioctl = edge_ioctl,
2779 .set_termios = edge_set_termios,
2780 .tiocmget = edge_tiocmget,
2781 .tiocmset = edge_tiocmset,
2782 .write = edge_write,
2783 .write_room = edge_write_room,
2784 .chars_in_buffer = edge_chars_in_buffer,
2785 .break_ctl = edge_break,
2786 .read_int_callback = edge_interrupt_callback,
2787 .read_bulk_callback = edge_bulk_in_callback,
2788 .write_bulk_callback = edge_bulk_out_callback,
2789};
2790
2791
2792static int __init edgeport_init(void)
2793{
2794 int retval;
2795 retval = usb_serial_register(&edgeport_1port_device);
2796 if (retval)
2797 goto failed_1port_device_register;
2798 retval = usb_serial_register(&edgeport_2port_device);
2799 if (retval)
2800 goto failed_2port_device_register;
2801 retval = usb_register(&io_driver);
Alan Cox2742fd82008-04-29 14:45:15 +01002802 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 goto failed_usb_register;
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -07002804 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
2805 DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 return 0;
2807failed_usb_register:
2808 usb_serial_deregister(&edgeport_2port_device);
2809failed_2port_device_register:
2810 usb_serial_deregister(&edgeport_1port_device);
2811failed_1port_device_register:
2812 return retval;
2813}
2814
Alan Cox2742fd82008-04-29 14:45:15 +01002815static void __exit edgeport_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816{
Alan Cox2742fd82008-04-29 14:45:15 +01002817 usb_deregister(&io_driver);
2818 usb_serial_deregister(&edgeport_1port_device);
2819 usb_serial_deregister(&edgeport_2port_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820}
2821
2822module_init(edgeport_init);
2823module_exit(edgeport_exit);
2824
2825/* Module information */
2826MODULE_AUTHOR(DRIVER_AUTHOR);
2827MODULE_DESCRIPTION(DRIVER_DESC);
2828MODULE_LICENSE("GPL");
Jaswinder Singhd12b2192008-07-04 23:06:09 +05302829MODULE_FIRMWARE("edgeport/down3.bin");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
2831module_param(debug, bool, S_IRUGO | S_IWUSR);
2832MODULE_PARM_DESC(debug, "Debug enabled or not");
2833
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834module_param(closing_wait, int, S_IRUGO | S_IWUSR);
2835MODULE_PARM_DESC(closing_wait, "Maximum wait for data to drain, in .01 secs");
2836
2837module_param(ignore_cpu_rev, bool, S_IRUGO | S_IWUSR);
Alan Cox2742fd82008-04-29 14:45:15 +01002838MODULE_PARM_DESC(ignore_cpu_rev,
2839 "Ignore the cpu revision when connecting to a device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002841module_param(default_uart_mode, int, S_IRUGO | S_IWUSR);
2842MODULE_PARM_DESC(default_uart_mode, "Default uart_mode, 0=RS232, ...");