blob: 643c120143125b6886e4f440f492408b0fecbf78 [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",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 .id_table = id_table_combined,
203};
204
205
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530206static unsigned char OperationalMajorVersion;
207static unsigned char OperationalMinorVersion;
208static unsigned short OperationalBuildNumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030210static bool debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212static int closing_wait = EDGE_CLOSING_WAIT;
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030213static bool ignore_cpu_rev;
Alan Cox2742fd82008-04-29 14:45:15 +0100214static int default_uart_mode; /* RS232 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Alan Cox2742fd82008-04-29 14:45:15 +0100216static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
217 unsigned char *data, int length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219static void stop_read(struct edgeport_port *edge_port);
220static int restart_read(struct edgeport_port *edge_port);
221
Alan Cox95da3102008-07-22 11:09:07 +0100222static void edge_set_termios(struct tty_struct *tty,
223 struct usb_serial_port *port, struct ktermios *old_termios);
224static void edge_send(struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -0400226/* sysfs attributes */
227static int edge_create_sysfs_attrs(struct usb_serial_port *port);
228static int edge_remove_sysfs_attrs(struct usb_serial_port *port);
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Alan Cox2742fd82008-04-29 14:45:15 +0100231static int ti_vread_sync(struct usb_device *dev, __u8 request,
232 __u16 value, __u16 index, u8 *data, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
234 int status;
235
Alan Cox2742fd82008-04-29 14:45:15 +0100236 status = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request,
237 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
238 value, index, data, size, 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 if (status < 0)
240 return status;
241 if (status != size) {
Alan Cox2742fd82008-04-29 14:45:15 +0100242 dbg("%s - wanted to write %d, but only wrote %d",
243 __func__, size, status);
244 return -ECOMM;
245 }
246 return 0;
247}
248
249static int ti_vsend_sync(struct usb_device *dev, __u8 request,
250 __u16 value, __u16 index, u8 *data, int size)
251{
252 int status;
253
254 status = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
255 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
256 value, index, data, size, 1000);
257 if (status < 0)
258 return status;
259 if (status != size) {
260 dbg("%s - wanted to write %d, but only wrote %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800261 __func__, size, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return -ECOMM;
263 }
264 return 0;
265}
266
Alan Cox2742fd82008-04-29 14:45:15 +0100267static int send_cmd(struct usb_device *dev, __u8 command,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 __u8 moduleid, __u16 value, u8 *data,
269 int size)
270{
Alan Cox2742fd82008-04-29 14:45:15 +0100271 return ti_vsend_sync(dev, command, value, moduleid, data, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273
274/* clear tx/rx buffers and fifo in TI UMP */
Alan Cox2742fd82008-04-29 14:45:15 +0100275static int purge_port(struct usb_serial_port *port, __u16 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 int port_number = port->number - port->serial->minor;
278
Alan Cox2742fd82008-04-29 14:45:15 +0100279 dbg("%s - port %d, mask %x", __func__, port_number, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Alan Cox2742fd82008-04-29 14:45:15 +0100281 return send_cmd(port->serial->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 UMPC_PURGE_PORT,
283 (__u8)(UMPM_UART1_PORT + port_number),
284 mask,
285 NULL,
286 0);
287}
288
289/**
Alan Cox2742fd82008-04-29 14:45:15 +0100290 * read_download_mem - Read edgeport memory from TI chip
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 * @dev: usb device pointer
292 * @start_address: Device CPU address at which to read
293 * @length: Length of above data
294 * @address_type: Can read both XDATA and I2C
295 * @buffer: pointer to input data buffer
296 */
Alan Cox2742fd82008-04-29 14:45:15 +0100297static int read_download_mem(struct usb_device *dev, int start_address,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 int length, __u8 address_type, __u8 *buffer)
299{
300 int status = 0;
301 __u8 read_length;
302 __be16 be_start_address;
Alan Cox2742fd82008-04-29 14:45:15 +0100303
304 dbg("%s - @ %x for %d", __func__, start_address, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 /* Read in blocks of 64 bytes
307 * (TI firmware can't handle more than 64 byte reads)
308 */
309 while (length) {
310 if (length > 64)
Alan Cox2742fd82008-04-29 14:45:15 +0100311 read_length = 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 else
313 read_length = (__u8)length;
314
315 if (read_length > 1) {
Alan Cox2742fd82008-04-29 14:45:15 +0100316 dbg("%s - @ %x for %d", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 start_address, read_length);
318 }
Alan Cox2742fd82008-04-29 14:45:15 +0100319 be_start_address = cpu_to_be16(start_address);
320 status = ti_vread_sync(dev, UMPC_MEMORY_READ,
321 (__u16)address_type,
322 (__force __u16)be_start_address,
323 buffer, read_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +0100326 dbg("%s - ERROR %x", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 return status;
328 }
329
Alan Cox2742fd82008-04-29 14:45:15 +0100330 if (read_length > 1)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800331 usb_serial_debug_data(debug, &dev->dev, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 read_length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 /* Update pointers/length */
335 start_address += read_length;
336 buffer += read_length;
337 length -= read_length;
338 }
Alan Cox2742fd82008-04-29 14:45:15 +0100339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return status;
341}
342
Alan Cox2742fd82008-04-29 14:45:15 +0100343static int read_ram(struct usb_device *dev, int start_address,
344 int length, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Alan Cox2742fd82008-04-29 14:45:15 +0100346 return read_download_mem(dev, start_address, length,
347 DTK_ADDR_SPACE_XDATA, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
349
350/* Read edgeport memory to a given block */
Alan Cox2742fd82008-04-29 14:45:15 +0100351static int read_boot_mem(struct edgeport_serial *serial,
352 int start_address, int length, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
354 int status = 0;
355 int i;
356
Alan Cox2742fd82008-04-29 14:45:15 +0100357 for (i = 0; i < length; i++) {
358 status = ti_vread_sync(serial->serial->dev,
359 UMPC_MEMORY_READ, serial->TI_I2C_Type,
360 (__u16)(start_address+i), &buffer[i], 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +0100362 dbg("%s - ERROR %x", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return status;
364 }
365 }
366
Alan Cox2742fd82008-04-29 14:45:15 +0100367 dbg("%s - start_address = %x, length = %d",
368 __func__, start_address, length);
369 usb_serial_debug_data(debug, &serial->serial->dev->dev,
370 __func__, length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 serial->TiReadI2C = 1;
373
374 return status;
375}
376
377/* Write given block to TI EPROM memory */
Alan Cox2742fd82008-04-29 14:45:15 +0100378static int write_boot_mem(struct edgeport_serial *serial,
379 int start_address, int length, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
381 int status = 0;
382 int i;
Johan Hovolde9305d22009-12-28 23:01:50 +0100383 u8 *temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 /* Must do a read before write */
386 if (!serial->TiReadI2C) {
Johan Hovolde9305d22009-12-28 23:01:50 +0100387 temp = kmalloc(1, GFP_KERNEL);
388 if (!temp) {
389 dev_err(&serial->serial->dev->dev,
390 "%s - out of memory\n", __func__);
391 return -ENOMEM;
392 }
393 status = read_boot_mem(serial, 0, 1, temp);
394 kfree(temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (status)
396 return status;
397 }
398
Alan Cox2742fd82008-04-29 14:45:15 +0100399 for (i = 0; i < length; ++i) {
400 status = ti_vsend_sync(serial->serial->dev,
401 UMPC_MEMORY_WRITE, buffer[i],
402 (__u16)(i + start_address), NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 if (status)
404 return status;
405 }
406
Alan Cox2742fd82008-04-29 14:45:15 +0100407 dbg("%s - start_sddr = %x, length = %d",
408 __func__, start_address, length);
409 usb_serial_debug_data(debug, &serial->serial->dev->dev,
410 __func__, length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 return status;
413}
414
415
416/* Write edgeport I2C memory to TI chip */
Alan Cox2742fd82008-04-29 14:45:15 +0100417static int write_i2c_mem(struct edgeport_serial *serial,
418 int start_address, int length, __u8 address_type, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
420 int status = 0;
421 int write_length;
422 __be16 be_start_address;
423
424 /* We can only send a maximum of 1 aligned byte page at a time */
Alan Cox2742fd82008-04-29 14:45:15 +0100425
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300426 /* calculate the number of bytes left in the first page */
Alan Cox2742fd82008-04-29 14:45:15 +0100427 write_length = EPROM_PAGE_SIZE -
428 (start_address & (EPROM_PAGE_SIZE - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 if (write_length > length)
431 write_length = length;
432
Alan Cox2742fd82008-04-29 14:45:15 +0100433 dbg("%s - BytesInFirstPage Addr = %x, length = %d",
434 __func__, start_address, write_length);
435 usb_serial_debug_data(debug, &serial->serial->dev->dev,
436 __func__, write_length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 /* Write first page */
Alan Cox2742fd82008-04-29 14:45:15 +0100439 be_start_address = cpu_to_be16(start_address);
440 status = ti_vsend_sync(serial->serial->dev,
441 UMPC_MEMORY_WRITE, (__u16)address_type,
442 (__force __u16)be_start_address,
443 buffer, write_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +0100445 dbg("%s - ERROR %d", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return status;
447 }
448
449 length -= write_length;
450 start_address += write_length;
451 buffer += write_length;
452
Alan Cox2742fd82008-04-29 14:45:15 +0100453 /* We should be aligned now -- can write
454 max page size bytes at a time */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 while (length) {
456 if (length > EPROM_PAGE_SIZE)
457 write_length = EPROM_PAGE_SIZE;
458 else
459 write_length = length;
460
Alan Cox2742fd82008-04-29 14:45:15 +0100461 dbg("%s - Page Write Addr = %x, length = %d",
462 __func__, start_address, write_length);
463 usb_serial_debug_data(debug, &serial->serial->dev->dev,
464 __func__, write_length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 /* Write next page */
Alan Cox2742fd82008-04-29 14:45:15 +0100467 be_start_address = cpu_to_be16(start_address);
468 status = ti_vsend_sync(serial->serial->dev, UMPC_MEMORY_WRITE,
469 (__u16)address_type,
470 (__force __u16)be_start_address,
471 buffer, write_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +0100473 dev_err(&serial->serial->dev->dev, "%s - ERROR %d\n",
474 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 return status;
476 }
Alan Cox2742fd82008-04-29 14:45:15 +0100477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 length -= write_length;
479 start_address += write_length;
480 buffer += write_length;
481 }
482 return status;
483}
484
485/* Examine the UMP DMA registers and LSR
Alan Cox2742fd82008-04-29 14:45:15 +0100486 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 * Check the MSBit of the X and Y DMA byte count registers.
488 * A zero in this bit indicates that the TX DMA buffers are empty
489 * then check the TX Empty bit in the UART.
490 */
Alan Cox2742fd82008-04-29 14:45:15 +0100491static int tx_active(struct edgeport_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
493 int status;
494 struct out_endpoint_desc_block *oedb;
495 __u8 *lsr;
496 int bytes_left = 0;
497
Alan Cox2742fd82008-04-29 14:45:15 +0100498 oedb = kmalloc(sizeof(*oedb), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (!oedb) {
Alan Cox2742fd82008-04-29 14:45:15 +0100500 dev_err(&port->port->dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return -ENOMEM;
502 }
503
Alan Cox2742fd82008-04-29 14:45:15 +0100504 lsr = kmalloc(1, GFP_KERNEL); /* Sigh, that's right, just one byte,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 as not all platforms can do DMA
506 from stack */
507 if (!lsr) {
508 kfree(oedb);
509 return -ENOMEM;
510 }
511 /* Read the DMA Count Registers */
Alan Cox2742fd82008-04-29 14:45:15 +0100512 status = read_ram(port->port->serial->dev, port->dma_address,
513 sizeof(*oedb), (void *)oedb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (status)
515 goto exit_is_tx_active;
516
Alan Cox2742fd82008-04-29 14:45:15 +0100517 dbg("%s - XByteCount 0x%X", __func__, oedb->XByteCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 /* and the LSR */
Alan Cox2742fd82008-04-29 14:45:15 +0100520 status = read_ram(port->port->serial->dev,
521 port->uart_base + UMPMEM_OFFS_UART_LSR, 1, lsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 if (status)
524 goto exit_is_tx_active;
Alan Cox2742fd82008-04-29 14:45:15 +0100525 dbg("%s - LSR = 0x%X", __func__, *lsr);
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 /* If either buffer has data or we are transmitting then return TRUE */
Alan Cox2742fd82008-04-29 14:45:15 +0100528 if ((oedb->XByteCount & 0x80) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 bytes_left += 64;
530
Alan Cox2742fd82008-04-29 14:45:15 +0100531 if ((*lsr & UMP_UART_LSR_TX_MASK) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 bytes_left += 1;
533
534 /* We return Not Active if we get any kind of error */
535exit_is_tx_active:
Alan Cox2742fd82008-04-29 14:45:15 +0100536 dbg("%s - return %d", __func__, bytes_left);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 kfree(lsr);
539 kfree(oedb);
540 return bytes_left;
541}
542
Alan Cox2742fd82008-04-29 14:45:15 +0100543static void chase_port(struct edgeport_port *port, unsigned long timeout,
544 int flush)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
546 int baud_rate;
Alan Cox4a90f092008-10-13 10:39:46 +0100547 struct tty_struct *tty = tty_port_tty_get(&port->port->port);
Johan Hovoldaf581052012-03-26 20:31:38 +0200548 struct usb_serial *serial = port->port->serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 wait_queue_t wait;
550 unsigned long flags;
551
552 if (!timeout)
Alan Cox2742fd82008-04-29 14:45:15 +0100553 timeout = (HZ * EDGE_CLOSING_WAIT)/100;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 /* wait for data to drain from the buffer */
556 spin_lock_irqsave(&port->ep_lock, flags);
557 init_waitqueue_entry(&wait, current);
558 add_wait_queue(&tty->write_wait, &wait);
559 for (;;) {
560 set_current_state(TASK_INTERRUPTIBLE);
Johan Hovoldd733cec2010-05-19 00:01:37 +0200561 if (kfifo_len(&port->write_fifo) == 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 || timeout == 0 || signal_pending(current)
Johan Hovoldaf581052012-03-26 20:31:38 +0200563 || serial->disconnected)
Alan Cox2742fd82008-04-29 14:45:15 +0100564 /* disconnect */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 break;
566 spin_unlock_irqrestore(&port->ep_lock, flags);
567 timeout = schedule_timeout(timeout);
568 spin_lock_irqsave(&port->ep_lock, flags);
569 }
570 set_current_state(TASK_RUNNING);
571 remove_wait_queue(&tty->write_wait, &wait);
572 if (flush)
Johan Hovoldd733cec2010-05-19 00:01:37 +0200573 kfifo_reset_out(&port->write_fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 spin_unlock_irqrestore(&port->ep_lock, flags);
Alan Cox4a90f092008-10-13 10:39:46 +0100575 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 /* wait for data to drain from the device */
578 timeout += jiffies;
579 while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
Johan Hovoldaf581052012-03-26 20:31:38 +0200580 && !serial->disconnected) {
Alan Cox2742fd82008-04-29 14:45:15 +0100581 /* not disconnected */
582 if (!tx_active(port))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 break;
584 msleep(10);
585 }
586
587 /* disconnected */
Johan Hovoldaf581052012-03-26 20:31:38 +0200588 if (serial->disconnected)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return;
590
591 /* wait one more character time, based on baud rate */
Alan Cox2742fd82008-04-29 14:45:15 +0100592 /* (tx_active doesn't seem to wait for the last byte) */
593 baud_rate = port->baud_rate;
594 if (baud_rate == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 baud_rate = 50;
Julia Lawalldfa5ec72008-03-04 15:25:11 -0800596 msleep(max(1, DIV_ROUND_UP(10000, baud_rate)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
598
Alan Cox2742fd82008-04-29 14:45:15 +0100599static int choose_config(struct usb_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
Alan Cox2742fd82008-04-29 14:45:15 +0100601 /*
602 * There may be multiple configurations on this device, in which case
603 * we would need to read and parse all of them to find out which one
604 * we want. However, we just support one config at this point,
605 * configuration # 1, which is Config Descriptor 0.
606 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Alan Cox2742fd82008-04-29 14:45:15 +0100608 dbg("%s - Number of Interfaces = %d",
609 __func__, dev->config->desc.bNumInterfaces);
610 dbg("%s - MAX Power = %d",
611 __func__, dev->config->desc.bMaxPower * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 if (dev->config->desc.bNumInterfaces != 1) {
Alan Cox2742fd82008-04-29 14:45:15 +0100614 dev_err(&dev->dev, "%s - bNumInterfaces is not 1, ERROR!\n",
615 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 return -ENODEV;
617 }
618
619 return 0;
620}
621
Alan Cox2742fd82008-04-29 14:45:15 +0100622static int read_rom(struct edgeport_serial *serial,
623 int start_address, int length, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
625 int status;
626
627 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
Alan Cox2742fd82008-04-29 14:45:15 +0100628 status = read_download_mem(serial->serial->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 start_address,
630 length,
631 serial->TI_I2C_Type,
632 buffer);
633 } else {
Alan Cox2742fd82008-04-29 14:45:15 +0100634 status = read_boot_mem(serial, start_address, length,
635 buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 return status;
638}
639
Alan Cox2742fd82008-04-29 14:45:15 +0100640static int write_rom(struct edgeport_serial *serial, int start_address,
641 int length, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
643 if (serial->product_info.TiMode == TI_MODE_BOOT)
Alan Cox2742fd82008-04-29 14:45:15 +0100644 return write_boot_mem(serial, start_address, length,
645 buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD)
Alan Cox2742fd82008-04-29 14:45:15 +0100648 return write_i2c_mem(serial, start_address, length,
649 serial->TI_I2C_Type, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return -EINVAL;
651}
652
653
654
655/* Read a descriptor header from I2C based on type */
Alan Cox2742fd82008-04-29 14:45:15 +0100656static int get_descriptor_addr(struct edgeport_serial *serial,
657 int desc_type, struct ti_i2c_desc *rom_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
659 int start_address;
660 int status;
661
662 /* Search for requested descriptor in I2C */
663 start_address = 2;
664 do {
Alan Cox2742fd82008-04-29 14:45:15 +0100665 status = read_rom(serial,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 start_address,
667 sizeof(struct ti_i2c_desc),
Alan Cox2742fd82008-04-29 14:45:15 +0100668 (__u8 *)rom_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (status)
670 return 0;
671
672 if (rom_desc->Type == desc_type)
673 return start_address;
674
Alan Cox2742fd82008-04-29 14:45:15 +0100675 start_address = start_address + sizeof(struct ti_i2c_desc)
676 + rom_desc->Size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 } while ((start_address < TI_MAX_I2C_SIZE) && rom_desc->Type);
Alan Cox2742fd82008-04-29 14:45:15 +0100679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return 0;
681}
682
683/* Validate descriptor checksum */
Alan Cox2742fd82008-04-29 14:45:15 +0100684static int valid_csum(struct ti_i2c_desc *rom_desc, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
686 __u16 i;
687 __u8 cs = 0;
688
Alan Cox2742fd82008-04-29 14:45:15 +0100689 for (i = 0; i < rom_desc->Size; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 cs = (__u8)(cs + buffer[i]);
Alan Cox2742fd82008-04-29 14:45:15 +0100691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 if (cs != rom_desc->CheckSum) {
Alan Cox2742fd82008-04-29 14:45:15 +0100693 dbg("%s - Mismatch %x - %x", __func__, rom_desc->CheckSum, cs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return -EINVAL;
695 }
696 return 0;
697}
698
699/* Make sure that the I2C image is good */
Alan Cox2742fd82008-04-29 14:45:15 +0100700static int check_i2c_image(struct edgeport_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
702 struct device *dev = &serial->serial->dev->dev;
703 int status = 0;
704 struct ti_i2c_desc *rom_desc;
705 int start_address = 2;
706 __u8 *buffer;
707 __u16 ttype;
708
Alan Cox2742fd82008-04-29 14:45:15 +0100709 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (!rom_desc) {
Alan Cox2742fd82008-04-29 14:45:15 +0100711 dev_err(dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 return -ENOMEM;
713 }
Alan Cox2742fd82008-04-29 14:45:15 +0100714 buffer = kmalloc(TI_MAX_I2C_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (!buffer) {
Alan Cox2742fd82008-04-29 14:45:15 +0100716 dev_err(dev, "%s - out of memory when allocating buffer\n",
717 __func__);
718 kfree(rom_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return -ENOMEM;
720 }
721
Alan Cox2742fd82008-04-29 14:45:15 +0100722 /* Read the first byte (Signature0) must be 0x52 or 0x10 */
723 status = read_rom(serial, 0, 1, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 if (status)
Alan Cox2742fd82008-04-29 14:45:15 +0100725 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727 if (*buffer != UMP5152 && *buffer != UMP3410) {
Alan Cox2742fd82008-04-29 14:45:15 +0100728 dev_err(dev, "%s - invalid buffer signature\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 status = -ENODEV;
Alan Cox2742fd82008-04-29 14:45:15 +0100730 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 }
732
733 do {
Alan Cox2742fd82008-04-29 14:45:15 +0100734 /* Validate the I2C */
735 status = read_rom(serial,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 start_address,
737 sizeof(struct ti_i2c_desc),
738 (__u8 *)rom_desc);
739 if (status)
740 break;
741
Alan Cox2742fd82008-04-29 14:45:15 +0100742 if ((start_address + sizeof(struct ti_i2c_desc) +
743 rom_desc->Size) > TI_MAX_I2C_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 status = -ENODEV;
Alan Cox2742fd82008-04-29 14:45:15 +0100745 dbg("%s - structure too big, erroring out.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 break;
747 }
748
Alan Cox2742fd82008-04-29 14:45:15 +0100749 dbg("%s Type = 0x%x", __func__, rom_desc->Type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Alan Cox2742fd82008-04-29 14:45:15 +0100751 /* Skip type 2 record */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 ttype = rom_desc->Type & 0x0f;
Alan Cox2742fd82008-04-29 14:45:15 +0100753 if (ttype != I2C_DESC_TYPE_FIRMWARE_BASIC
754 && ttype != I2C_DESC_TYPE_FIRMWARE_AUTO) {
755 /* Read the descriptor data */
756 status = read_rom(serial, start_address +
757 sizeof(struct ti_i2c_desc),
758 rom_desc->Size, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 if (status)
760 break;
761
Alan Cox2742fd82008-04-29 14:45:15 +0100762 status = valid_csum(rom_desc, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 if (status)
764 break;
765 }
Alan Cox2742fd82008-04-29 14:45:15 +0100766 start_address = start_address + sizeof(struct ti_i2c_desc) +
767 rom_desc->Size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Alan Cox2742fd82008-04-29 14:45:15 +0100769 } while ((rom_desc->Type != I2C_DESC_TYPE_ION) &&
770 (start_address < TI_MAX_I2C_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Alan Cox2742fd82008-04-29 14:45:15 +0100772 if ((rom_desc->Type != I2C_DESC_TYPE_ION) ||
773 (start_address > TI_MAX_I2C_SIZE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 status = -ENODEV;
775
Alan Cox2742fd82008-04-29 14:45:15 +0100776out:
777 kfree(buffer);
778 kfree(rom_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return status;
780}
781
Alan Cox2742fd82008-04-29 14:45:15 +0100782static int get_manuf_info(struct edgeport_serial *serial, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
784 int status;
785 int start_address;
786 struct ti_i2c_desc *rom_desc;
787 struct edge_ti_manuf_descriptor *desc;
788
Alan Cox2742fd82008-04-29 14:45:15 +0100789 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 if (!rom_desc) {
Alan Cox2742fd82008-04-29 14:45:15 +0100791 dev_err(&serial->serial->dev->dev, "%s - out of memory\n",
792 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 return -ENOMEM;
794 }
Alan Cox2742fd82008-04-29 14:45:15 +0100795 start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_ION,
796 rom_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 if (!start_address) {
Alan Cox2742fd82008-04-29 14:45:15 +0100799 dbg("%s - Edge Descriptor not found in I2C", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 status = -ENODEV;
801 goto exit;
802 }
803
Alan Cox2742fd82008-04-29 14:45:15 +0100804 /* Read the descriptor data */
805 status = read_rom(serial, start_address+sizeof(struct ti_i2c_desc),
806 rom_desc->Size, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 if (status)
808 goto exit;
Alan Cox2742fd82008-04-29 14:45:15 +0100809
810 status = valid_csum(rom_desc, buffer);
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 desc = (struct edge_ti_manuf_descriptor *)buffer;
Alan Cox2742fd82008-04-29 14:45:15 +0100813 dbg("%s - IonConfig 0x%x", __func__, desc->IonConfig);
814 dbg("%s - Version %d", __func__, desc->Version);
815 dbg("%s - Cpu/Board 0x%x", __func__, desc->CpuRev_BoardRev);
816 dbg("%s - NumPorts %d", __func__, desc->NumPorts);
817 dbg("%s - NumVirtualPorts %d", __func__, desc->NumVirtualPorts);
818 dbg("%s - TotalPorts %d", __func__, desc->TotalPorts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820exit:
Alan Cox2742fd82008-04-29 14:45:15 +0100821 kfree(rom_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return status;
823}
824
825/* Build firmware header used for firmware update */
Alan Cox2742fd82008-04-29 14:45:15 +0100826static int build_i2c_fw_hdr(__u8 *header, struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
828 __u8 *buffer;
829 int buffer_size;
830 int i;
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530831 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 __u8 cs = 0;
833 struct ti_i2c_desc *i2c_header;
834 struct ti_i2c_image_header *img_header;
835 struct ti_i2c_firmware_rec *firmware_rec;
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530836 const struct firmware *fw;
837 const char *fw_name = "edgeport/down3.bin";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Alan Cox2742fd82008-04-29 14:45:15 +0100839 /* In order to update the I2C firmware we must change the type 2 record
840 * to type 0xF2. This will force the UMP to come up in Boot Mode.
841 * Then while in boot mode, the driver will download the latest
842 * firmware (padded to 15.5k) into the UMP ram. And finally when the
843 * device comes back up in download mode the driver will cause the new
844 * firmware to be copied from the UMP Ram to I2C and the firmware will
845 * update the record type from 0xf2 to 0x02.
846 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Alan Cox2742fd82008-04-29 14:45:15 +0100848 /* Allocate a 15.5k buffer + 2 bytes for version number
849 * (Firmware Record) */
850 buffer_size = (((1024 * 16) - 512 ) +
851 sizeof(struct ti_i2c_firmware_rec));
852
853 buffer = kmalloc(buffer_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 if (!buffer) {
Alan Cox2742fd82008-04-29 14:45:15 +0100855 dev_err(dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 return -ENOMEM;
857 }
Alan Cox2742fd82008-04-29 14:45:15 +0100858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 // Set entire image of 0xffs
Alan Cox2742fd82008-04-29 14:45:15 +0100860 memset(buffer, 0xff, buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530862 err = request_firmware(&fw, fw_name, dev);
863 if (err) {
864 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
865 fw_name, err);
866 kfree(buffer);
867 return err;
868 }
869
870 /* Save Download Version Number */
871 OperationalMajorVersion = fw->data[0];
872 OperationalMinorVersion = fw->data[1];
873 OperationalBuildNumber = fw->data[2] | (fw->data[3] << 8);
874
Alan Cox2742fd82008-04-29 14:45:15 +0100875 /* Copy version number into firmware record */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 firmware_rec = (struct ti_i2c_firmware_rec *)buffer;
877
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530878 firmware_rec->Ver_Major = OperationalMajorVersion;
879 firmware_rec->Ver_Minor = OperationalMinorVersion;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Alan Cox2742fd82008-04-29 14:45:15 +0100881 /* Pointer to fw_down memory image */
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530882 img_header = (struct ti_i2c_image_header *)&fw->data[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Alan Cox2742fd82008-04-29 14:45:15 +0100884 memcpy(buffer + sizeof(struct ti_i2c_firmware_rec),
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530885 &fw->data[4 + sizeof(struct ti_i2c_image_header)],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 le16_to_cpu(img_header->Length));
887
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530888 release_firmware(fw);
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 for (i=0; i < buffer_size; i++) {
891 cs = (__u8)(cs + buffer[i]);
892 }
893
Alan Cox2742fd82008-04-29 14:45:15 +0100894 kfree(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Alan Cox2742fd82008-04-29 14:45:15 +0100896 /* Build new header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 i2c_header = (struct ti_i2c_desc *)header;
898 firmware_rec = (struct ti_i2c_firmware_rec*)i2c_header->Data;
Alan Cox2742fd82008-04-29 14:45:15 +0100899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 i2c_header->Type = I2C_DESC_TYPE_FIRMWARE_BLANK;
901 i2c_header->Size = (__u16)buffer_size;
902 i2c_header->CheckSum = cs;
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530903 firmware_rec->Ver_Major = OperationalMajorVersion;
904 firmware_rec->Ver_Minor = OperationalMinorVersion;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 return 0;
907}
908
909/* Try to figure out what type of I2c we have */
Alan Cox2742fd82008-04-29 14:45:15 +0100910static int i2c_type_bootmode(struct edgeport_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
912 int status;
Johan Hovolde9305d22009-12-28 23:01:50 +0100913 u8 *data;
914
915 data = kmalloc(1, GFP_KERNEL);
916 if (!data) {
917 dev_err(&serial->serial->dev->dev,
918 "%s - out of memory\n", __func__);
919 return -ENOMEM;
920 }
Alan Cox2742fd82008-04-29 14:45:15 +0100921
922 /* Try to read type 2 */
923 status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
Johan Hovolde9305d22009-12-28 23:01:50 +0100924 DTK_ADDR_SPACE_I2C_TYPE_II, 0, data, 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 if (status)
Alan Cox2742fd82008-04-29 14:45:15 +0100926 dbg("%s - read 2 status error = %d", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 else
Johan Hovolde9305d22009-12-28 23:01:50 +0100928 dbg("%s - read 2 data = 0x%x", __func__, *data);
929 if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
Alan Cox2742fd82008-04-29 14:45:15 +0100930 dbg("%s - ROM_TYPE_II", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
Johan Hovolde9305d22009-12-28 23:01:50 +0100932 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 }
934
Alan Cox2742fd82008-04-29 14:45:15 +0100935 /* Try to read type 3 */
936 status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
Johan Hovolde9305d22009-12-28 23:01:50 +0100937 DTK_ADDR_SPACE_I2C_TYPE_III, 0, data, 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 if (status)
Alan Cox2742fd82008-04-29 14:45:15 +0100939 dbg("%s - read 3 status error = %d", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 else
Johan Hovolde9305d22009-12-28 23:01:50 +0100941 dbg("%s - read 2 data = 0x%x", __func__, *data);
942 if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
Alan Cox2742fd82008-04-29 14:45:15 +0100943 dbg("%s - ROM_TYPE_III", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_III;
Johan Hovolde9305d22009-12-28 23:01:50 +0100945 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
947
Alan Cox2742fd82008-04-29 14:45:15 +0100948 dbg("%s - Unknown", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
Johan Hovolde9305d22009-12-28 23:01:50 +0100950 status = -ENODEV;
951out:
952 kfree(data);
953 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954}
955
Alan Cox2742fd82008-04-29 14:45:15 +0100956static int bulk_xfer(struct usb_serial *serial, void *buffer,
957 int length, int *num_sent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
959 int status;
960
Alan Cox2742fd82008-04-29 14:45:15 +0100961 status = usb_bulk_msg(serial->dev,
962 usb_sndbulkpipe(serial->dev,
963 serial->port[0]->bulk_out_endpointAddress),
964 buffer, length, num_sent, 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 return status;
966}
967
968/* Download given firmware image to the device (IN BOOT MODE) */
Alan Cox2742fd82008-04-29 14:45:15 +0100969static int download_code(struct edgeport_serial *serial, __u8 *image,
970 int image_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
972 int status = 0;
973 int pos;
974 int transfer;
975 int done;
976
Alan Cox2742fd82008-04-29 14:45:15 +0100977 /* Transfer firmware image */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 for (pos = 0; pos < image_length; ) {
Alan Cox2742fd82008-04-29 14:45:15 +0100979 /* Read the next buffer from file */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 transfer = image_length - pos;
981 if (transfer > EDGE_FW_BULK_MAX_PACKET_SIZE)
982 transfer = EDGE_FW_BULK_MAX_PACKET_SIZE;
983
Alan Cox2742fd82008-04-29 14:45:15 +0100984 /* Transfer data */
985 status = bulk_xfer(serial->serial, &image[pos],
986 transfer, &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 if (status)
988 break;
Alan Cox2742fd82008-04-29 14:45:15 +0100989 /* Advance buffer pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 pos += done;
991 }
992
993 return status;
994}
995
Alan Cox2742fd82008-04-29 14:45:15 +0100996/* FIXME!!! */
997static int config_boot_dev(struct usb_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
999 return 0;
1000}
1001
Alan Cox2742fd82008-04-29 14:45:15 +01001002static int ti_cpu_rev(struct edge_ti_manuf_descriptor *desc)
1003{
1004 return TI_GET_CPU_REVISION(desc->CpuRev_BoardRev);
1005}
1006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007/**
1008 * DownloadTIFirmware - Download run-time operating firmware to the TI5052
Alan Cox2742fd82008-04-29 14:45:15 +01001009 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 * This routine downloads the main operating code into the TI5052, using the
1011 * boot code already burned into E2PROM or ROM.
1012 */
Alan Cox2742fd82008-04-29 14:45:15 +01001013static int download_fw(struct edgeport_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
1015 struct device *dev = &serial->serial->dev->dev;
1016 int status = 0;
1017 int start_address;
1018 struct edge_ti_manuf_descriptor *ti_manuf_desc;
1019 struct usb_interface_descriptor *interface;
1020 int download_cur_ver;
1021 int download_new_ver;
1022
1023 /* This routine is entered by both the BOOT mode and the Download mode
1024 * We can determine which code is running by the reading the config
1025 * descriptor and if we have only one bulk pipe it is in boot mode
1026 */
1027 serial->product_info.hardware_type = HARDWARE_TYPE_TIUMP;
1028
1029 /* Default to type 2 i2c */
1030 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
1031
Alan Cox2742fd82008-04-29 14:45:15 +01001032 status = choose_config(serial->serial->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if (status)
1034 return status;
1035
1036 interface = &serial->serial->interface->cur_altsetting->desc;
1037 if (!interface) {
Alan Cox2742fd82008-04-29 14:45:15 +01001038 dev_err(dev, "%s - no interface set, error!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 return -ENODEV;
1040 }
1041
Alan Cox2742fd82008-04-29 14:45:15 +01001042 /*
1043 * Setup initial mode -- the default mode 0 is TI_MODE_CONFIGURING
1044 * if we have more than one endpoint we are definitely in download
1045 * mode
1046 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 if (interface->bNumEndpoints > 1)
1048 serial->product_info.TiMode = TI_MODE_DOWNLOAD;
1049 else
Alan Cox2742fd82008-04-29 14:45:15 +01001050 /* Otherwise we will remain in configuring mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 serial->product_info.TiMode = TI_MODE_CONFIGURING;
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 /********************************************************************/
1054 /* Download Mode */
1055 /********************************************************************/
1056 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
1057 struct ti_i2c_desc *rom_desc;
1058
Andrew Morton9544e832008-02-04 23:57:50 -08001059 dbg("%s - RUNNING IN DOWNLOAD MODE", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Alan Cox2742fd82008-04-29 14:45:15 +01001061 status = check_i2c_image(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 if (status) {
Andrew Morton9544e832008-02-04 23:57:50 -08001063 dbg("%s - DOWNLOAD MODE -- BAD I2C", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return status;
1065 }
Alan Cox2742fd82008-04-29 14:45:15 +01001066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 /* Validate Hardware version number
1068 * Read Manufacturing Descriptor from TI Based Edgeport
1069 */
Alan Cox2742fd82008-04-29 14:45:15 +01001070 ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 if (!ti_manuf_desc) {
Alan Cox2742fd82008-04-29 14:45:15 +01001072 dev_err(dev, "%s - out of memory.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 return -ENOMEM;
1074 }
Alan Cox2742fd82008-04-29 14:45:15 +01001075 status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001077 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 return status;
1079 }
1080
Alan Cox2742fd82008-04-29 14:45:15 +01001081 /* Check version number of ION descriptor */
1082 if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
1083 dbg("%s - Wrong CPU Rev %d (Must be 2)",
1084 __func__, ti_cpu_rev(ti_manuf_desc));
1085 kfree(ti_manuf_desc);
1086 return -EINVAL;
1087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Alan Cox2742fd82008-04-29 14:45:15 +01001089 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 if (!rom_desc) {
Alan Cox2742fd82008-04-29 14:45:15 +01001091 dev_err(dev, "%s - out of memory.\n", __func__);
1092 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 return -ENOMEM;
1094 }
1095
Alan Cox2742fd82008-04-29 14:45:15 +01001096 /* Search for type 2 record (firmware record) */
1097 start_address = get_descriptor_addr(serial,
1098 I2C_DESC_TYPE_FIRMWARE_BASIC, rom_desc);
1099 if (start_address != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 struct ti_i2c_firmware_rec *firmware_version;
Johan Hovolde9305d22009-12-28 23:01:50 +01001101 u8 *record;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Alan Cox2742fd82008-04-29 14:45:15 +01001103 dbg("%s - Found Type FIRMWARE (Type 2) record",
1104 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Alan Cox2742fd82008-04-29 14:45:15 +01001106 firmware_version = kmalloc(sizeof(*firmware_version),
1107 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 if (!firmware_version) {
Alan Cox2742fd82008-04-29 14:45:15 +01001109 dev_err(dev, "%s - out of memory.\n", __func__);
1110 kfree(rom_desc);
1111 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 return -ENOMEM;
1113 }
1114
Alan Cox2742fd82008-04-29 14:45:15 +01001115 /* Validate version number
1116 * Read the descriptor data
1117 */
1118 status = read_rom(serial, start_address +
1119 sizeof(struct ti_i2c_desc),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 sizeof(struct ti_i2c_firmware_rec),
1121 (__u8 *)firmware_version);
1122 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001123 kfree(firmware_version);
1124 kfree(rom_desc);
1125 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 return status;
1127 }
1128
Alan Cox2742fd82008-04-29 14:45:15 +01001129 /* Check version number of download with current
1130 version in I2c */
1131 download_cur_ver = (firmware_version->Ver_Major << 8) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 (firmware_version->Ver_Minor);
Jaswinder Singhd12b2192008-07-04 23:06:09 +05301133 download_new_ver = (OperationalMajorVersion << 8) +
1134 (OperationalMinorVersion);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Alan Cox2742fd82008-04-29 14:45:15 +01001136 dbg("%s - >> FW Versions Device %d.%d Driver %d.%d",
1137 __func__,
1138 firmware_version->Ver_Major,
1139 firmware_version->Ver_Minor,
1140 OperationalMajorVersion,
1141 OperationalMinorVersion);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Alan Cox2742fd82008-04-29 14:45:15 +01001143 /* Check if we have an old version in the I2C and
1144 update if necessary */
Greg Kroah-Hartman0827a9f2010-08-17 15:15:37 -07001145 if (download_cur_ver < download_new_ver) {
Alan Cox2742fd82008-04-29 14:45:15 +01001146 dbg("%s - Update I2C dld from %d.%d to %d.%d",
1147 __func__,
1148 firmware_version->Ver_Major,
1149 firmware_version->Ver_Minor,
1150 OperationalMajorVersion,
1151 OperationalMinorVersion);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Johan Hovolde9305d22009-12-28 23:01:50 +01001153 record = kmalloc(1, GFP_KERNEL);
1154 if (!record) {
1155 dev_err(dev, "%s - out of memory.\n",
1156 __func__);
1157 kfree(firmware_version);
1158 kfree(rom_desc);
1159 kfree(ti_manuf_desc);
1160 return -ENOMEM;
1161 }
Alan Cox2742fd82008-04-29 14:45:15 +01001162 /* In order to update the I2C firmware we must
1163 * change the type 2 record to type 0xF2. This
1164 * will force the UMP to come up in Boot Mode.
1165 * Then while in boot mode, the driver will
1166 * download the latest firmware (padded to
1167 * 15.5k) into the UMP ram. Finally when the
1168 * device comes back up in download mode the
1169 * driver will cause the new firmware to be
1170 * copied from the UMP Ram to I2C and the
1171 * firmware will update the record type from
1172 * 0xf2 to 0x02.
1173 */
Johan Hovolde9305d22009-12-28 23:01:50 +01001174 *record = I2C_DESC_TYPE_FIRMWARE_BLANK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Alan Cox2742fd82008-04-29 14:45:15 +01001176 /* Change the I2C Firmware record type to
1177 0xf2 to trigger an update */
1178 status = write_rom(serial, start_address,
Johan Hovolde9305d22009-12-28 23:01:50 +01001179 sizeof(*record), record);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 if (status) {
Johan Hovolde9305d22009-12-28 23:01:50 +01001181 kfree(record);
Alan Cox2742fd82008-04-29 14:45:15 +01001182 kfree(firmware_version);
1183 kfree(rom_desc);
1184 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 return status;
1186 }
1187
Alan Cox2742fd82008-04-29 14:45:15 +01001188 /* verify the write -- must do this in order
1189 * for write to complete before we do the
1190 * hardware reset
1191 */
1192 status = read_rom(serial,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 start_address,
Johan Hovolde9305d22009-12-28 23:01:50 +01001194 sizeof(*record),
1195 record);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 if (status) {
Johan Hovolde9305d22009-12-28 23:01:50 +01001197 kfree(record);
Alan Cox2742fd82008-04-29 14:45:15 +01001198 kfree(firmware_version);
1199 kfree(rom_desc);
1200 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 return status;
1202 }
1203
Johan Hovolde9305d22009-12-28 23:01:50 +01001204 if (*record != I2C_DESC_TYPE_FIRMWARE_BLANK) {
Alan Cox2742fd82008-04-29 14:45:15 +01001205 dev_err(dev,
1206 "%s - error resetting device\n",
1207 __func__);
Johan Hovolde9305d22009-12-28 23:01:50 +01001208 kfree(record);
Alan Cox2742fd82008-04-29 14:45:15 +01001209 kfree(firmware_version);
1210 kfree(rom_desc);
1211 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 return -ENODEV;
1213 }
1214
Alan Cox2742fd82008-04-29 14:45:15 +01001215 dbg("%s - HARDWARE RESET", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Alan Cox2742fd82008-04-29 14:45:15 +01001217 /* Reset UMP -- Back to BOOT MODE */
1218 status = ti_vsend_sync(serial->serial->dev,
1219 UMPC_HARDWARE_RESET,
1220 0, 0, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Alan Cox2742fd82008-04-29 14:45:15 +01001222 dbg("%s - HARDWARE RESET return %d",
1223 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
1225 /* return an error on purpose. */
Johan Hovolde9305d22009-12-28 23:01:50 +01001226 kfree(record);
Alan Cox2742fd82008-04-29 14:45:15 +01001227 kfree(firmware_version);
1228 kfree(rom_desc);
1229 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 return -ENODEV;
1231 }
Alan Cox2742fd82008-04-29 14:45:15 +01001232 kfree(firmware_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 }
Alan Cox2742fd82008-04-29 14:45:15 +01001234 /* Search for type 0xF2 record (firmware blank record) */
1235 else if ((start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_FIRMWARE_BLANK, rom_desc)) != 0) {
1236#define HEADER_SIZE (sizeof(struct ti_i2c_desc) + \
1237 sizeof(struct ti_i2c_firmware_rec))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 __u8 *header;
1239 __u8 *vheader;
1240
Alan Cox2742fd82008-04-29 14:45:15 +01001241 header = kmalloc(HEADER_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 if (!header) {
Alan Cox2742fd82008-04-29 14:45:15 +01001243 dev_err(dev, "%s - out of memory.\n", __func__);
1244 kfree(rom_desc);
1245 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 return -ENOMEM;
1247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Alan Cox2742fd82008-04-29 14:45:15 +01001249 vheader = kmalloc(HEADER_SIZE, GFP_KERNEL);
1250 if (!vheader) {
1251 dev_err(dev, "%s - out of memory.\n", __func__);
1252 kfree(header);
1253 kfree(rom_desc);
1254 kfree(ti_manuf_desc);
1255 return -ENOMEM;
1256 }
1257
1258 dbg("%s - Found Type BLANK FIRMWARE (Type F2) record",
1259 __func__);
1260
1261 /*
1262 * In order to update the I2C firmware we must change
1263 * the type 2 record to type 0xF2. This will force the
1264 * UMP to come up in Boot Mode. Then while in boot
1265 * mode, the driver will download the latest firmware
1266 * (padded to 15.5k) into the UMP ram. Finally when the
1267 * device comes back up in download mode the driver
1268 * will cause the new firmware to be copied from the
1269 * UMP Ram to I2C and the firmware will update the
1270 * record type from 0xf2 to 0x02.
1271 */
1272 status = build_i2c_fw_hdr(header, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001274 kfree(vheader);
1275 kfree(header);
1276 kfree(rom_desc);
1277 kfree(ti_manuf_desc);
Roel Kluinfd6e5bb2010-08-10 14:29:19 -07001278 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 }
1280
Alan Cox2742fd82008-04-29 14:45:15 +01001281 /* Update I2C with type 0xf2 record with correct
1282 size and checksum */
1283 status = write_rom(serial,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 start_address,
1285 HEADER_SIZE,
1286 header);
1287 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001288 kfree(vheader);
1289 kfree(header);
1290 kfree(rom_desc);
1291 kfree(ti_manuf_desc);
Roel Kluin9800eb32010-07-20 15:29:08 -07001292 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 }
1294
Alan Cox2742fd82008-04-29 14:45:15 +01001295 /* verify the write -- must do this in order for
1296 write to complete before we do the hardware reset */
1297 status = read_rom(serial, start_address,
1298 HEADER_SIZE, vheader);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001301 dbg("%s - can't read header back", __func__);
1302 kfree(vheader);
1303 kfree(header);
1304 kfree(rom_desc);
1305 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 return status;
1307 }
1308 if (memcmp(vheader, header, HEADER_SIZE)) {
Alan Cox2742fd82008-04-29 14:45:15 +01001309 dbg("%s - write download record failed",
1310 __func__);
1311 kfree(vheader);
1312 kfree(header);
1313 kfree(rom_desc);
1314 kfree(ti_manuf_desc);
Roel Kluin1e297092010-07-02 00:36:43 +02001315 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 }
1317
Alan Cox2742fd82008-04-29 14:45:15 +01001318 kfree(vheader);
1319 kfree(header);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Alan Cox2742fd82008-04-29 14:45:15 +01001321 dbg("%s - Start firmware update", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Alan Cox2742fd82008-04-29 14:45:15 +01001323 /* Tell firmware to copy download image into I2C */
1324 status = ti_vsend_sync(serial->serial->dev,
1325 UMPC_COPY_DNLD_TO_I2C, 0, 0, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Alan Cox2742fd82008-04-29 14:45:15 +01001327 dbg("%s - Update complete 0x%x", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001329 dev_err(dev,
1330 "%s - UMPC_COPY_DNLD_TO_I2C failed\n",
1331 __func__);
1332 kfree(rom_desc);
1333 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 return status;
1335 }
1336 }
1337
1338 // The device is running the download code
Alan Cox2742fd82008-04-29 14:45:15 +01001339 kfree(rom_desc);
1340 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 return 0;
1342 }
1343
1344 /********************************************************************/
1345 /* Boot Mode */
1346 /********************************************************************/
Andrew Morton9544e832008-02-04 23:57:50 -08001347 dbg("%s - RUNNING IN BOOT MODE", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Alan Cox2742fd82008-04-29 14:45:15 +01001349 /* Configure the TI device so we can use the BULK pipes for download */
1350 status = config_boot_dev(serial->serial->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 if (status)
1352 return status;
1353
Alan Cox2742fd82008-04-29 14:45:15 +01001354 if (le16_to_cpu(serial->serial->dev->descriptor.idVendor)
1355 != USB_VENDOR_ID_ION) {
1356 dbg("%s - VID = 0x%x", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 le16_to_cpu(serial->serial->dev->descriptor.idVendor));
1358 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
Alan Cox2742fd82008-04-29 14:45:15 +01001359 goto stayinbootmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 }
1361
Alan Cox2742fd82008-04-29 14:45:15 +01001362 /* We have an ION device (I2c Must be programmed)
1363 Determine I2C image type */
1364 if (i2c_type_bootmode(serial))
1365 goto stayinbootmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Alan Cox2742fd82008-04-29 14:45:15 +01001367 /* Check for ION Vendor ID and that the I2C is valid */
1368 if (!check_i2c_image(serial)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 struct ti_i2c_image_header *header;
1370 int i;
1371 __u8 cs = 0;
1372 __u8 *buffer;
1373 int buffer_size;
Jaswinder Singhd12b2192008-07-04 23:06:09 +05301374 int err;
1375 const struct firmware *fw;
1376 const char *fw_name = "edgeport/down3.bin";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378 /* Validate Hardware version number
1379 * Read Manufacturing Descriptor from TI Based Edgeport
1380 */
Alan Cox2742fd82008-04-29 14:45:15 +01001381 ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 if (!ti_manuf_desc) {
Alan Cox2742fd82008-04-29 14:45:15 +01001383 dev_err(dev, "%s - out of memory.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 return -ENOMEM;
1385 }
Alan Cox2742fd82008-04-29 14:45:15 +01001386 status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001388 kfree(ti_manuf_desc);
1389 goto stayinbootmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 }
1391
Alan Cox2742fd82008-04-29 14:45:15 +01001392 /* Check for version 2 */
1393 if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
1394 dbg("%s - Wrong CPU Rev %d (Must be 2)",
1395 __func__, ti_cpu_rev(ti_manuf_desc));
1396 kfree(ti_manuf_desc);
1397 goto stayinbootmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 }
1399
Alan Cox2742fd82008-04-29 14:45:15 +01001400 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 /*
Alan Cox2742fd82008-04-29 14:45:15 +01001403 * In order to update the I2C firmware we must change the type
1404 * 2 record to type 0xF2. This will force the UMP to come up
1405 * in Boot Mode. Then while in boot mode, the driver will
1406 * download the latest firmware (padded to 15.5k) into the
1407 * UMP ram. Finally when the device comes back up in download
1408 * mode the driver will cause the new firmware to be copied
1409 * from the UMP Ram to I2C and the firmware will update the
1410 * record type from 0xf2 to 0x02.
1411 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 * Do we really have to copy the whole firmware image,
1413 * or could we do this in place!
1414 */
1415
Alan Cox2742fd82008-04-29 14:45:15 +01001416 /* Allocate a 15.5k buffer + 3 byte header */
1417 buffer_size = (((1024 * 16) - 512) +
1418 sizeof(struct ti_i2c_image_header));
1419 buffer = kmalloc(buffer_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 if (!buffer) {
Alan Cox2742fd82008-04-29 14:45:15 +01001421 dev_err(dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 return -ENOMEM;
1423 }
Alan Cox2742fd82008-04-29 14:45:15 +01001424
1425 /* Initialize the buffer to 0xff (pad the buffer) */
1426 memset(buffer, 0xff, buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Jaswinder Singhd12b2192008-07-04 23:06:09 +05301428 err = request_firmware(&fw, fw_name, dev);
1429 if (err) {
1430 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
1431 fw_name, err);
1432 kfree(buffer);
1433 return err;
1434 }
1435 memcpy(buffer, &fw->data[4], fw->size - 4);
1436 release_firmware(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Alan Cox2742fd82008-04-29 14:45:15 +01001438 for (i = sizeof(struct ti_i2c_image_header);
1439 i < buffer_size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 cs = (__u8)(cs + buffer[i]);
1441 }
Alan Cox2742fd82008-04-29 14:45:15 +01001442
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 header = (struct ti_i2c_image_header *)buffer;
Alan Cox2742fd82008-04-29 14:45:15 +01001444
1445 /* update length and checksum after padding */
1446 header->Length = cpu_to_le16((__u16)(buffer_size -
1447 sizeof(struct ti_i2c_image_header)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 header->CheckSum = cs;
1449
Alan Cox2742fd82008-04-29 14:45:15 +01001450 /* Download the operational code */
1451 dbg("%s - Downloading operational code image (TI UMP)",
1452 __func__);
1453 status = download_code(serial, buffer, buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Alan Cox2742fd82008-04-29 14:45:15 +01001455 kfree(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001458 dbg("%s - Error downloading operational code image",
1459 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 return status;
1461 }
1462
Alan Cox2742fd82008-04-29 14:45:15 +01001463 /* Device will reboot */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 serial->product_info.TiMode = TI_MODE_TRANSITIONING;
1465
Alan Cox2742fd82008-04-29 14:45:15 +01001466 dbg("%s - Download successful -- Device rebooting...",
1467 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
1469 /* return an error on purpose */
1470 return -ENODEV;
1471 }
1472
Alan Cox2742fd82008-04-29 14:45:15 +01001473stayinbootmode:
1474 /* Eprom is invalid or blank stay in boot mode */
Andrew Morton9544e832008-02-04 23:57:50 -08001475 dbg("%s - STAYING IN BOOT MODE", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 serial->product_info.TiMode = TI_MODE_BOOT;
1477
1478 return 0;
1479}
1480
1481
Alan Cox2742fd82008-04-29 14:45:15 +01001482static int ti_do_config(struct edgeport_port *port, int feature, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
1484 int port_number = port->port->number - port->port->serial->minor;
Alan Cox2742fd82008-04-29 14:45:15 +01001485 on = !!on; /* 1 or 0 not bitmask */
1486 return send_cmd(port->port->serial->dev,
1487 feature, (__u8)(UMPM_UART1_PORT + port_number),
1488 on, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489}
1490
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Alan Cox2742fd82008-04-29 14:45:15 +01001492static int restore_mcr(struct edgeport_port *port, __u8 mcr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
1494 int status = 0;
1495
Alan Cox2742fd82008-04-29 14:45:15 +01001496 dbg("%s - %x", __func__, mcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
Alan Cox2742fd82008-04-29 14:45:15 +01001498 status = ti_do_config(port, UMPC_SET_CLR_DTR, mcr & MCR_DTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 if (status)
1500 return status;
Alan Cox2742fd82008-04-29 14:45:15 +01001501 status = ti_do_config(port, UMPC_SET_CLR_RTS, mcr & MCR_RTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 if (status)
1503 return status;
Alan Cox2742fd82008-04-29 14:45:15 +01001504 return ti_do_config(port, UMPC_SET_CLR_LOOPBACK, mcr & MCR_LOOPBACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505}
1506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507/* Convert TI LSR to standard UART flags */
Alan Cox2742fd82008-04-29 14:45:15 +01001508static __u8 map_line_status(__u8 ti_lsr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
1510 __u8 lsr = 0;
1511
1512#define MAP_FLAG(flagUmp, flagUart) \
1513 if (ti_lsr & flagUmp) \
1514 lsr |= flagUart;
1515
1516 MAP_FLAG(UMP_UART_LSR_OV_MASK, LSR_OVER_ERR) /* overrun */
1517 MAP_FLAG(UMP_UART_LSR_PE_MASK, LSR_PAR_ERR) /* parity error */
1518 MAP_FLAG(UMP_UART_LSR_FE_MASK, LSR_FRM_ERR) /* framing error */
1519 MAP_FLAG(UMP_UART_LSR_BR_MASK, LSR_BREAK) /* break detected */
Alan Cox2742fd82008-04-29 14:45:15 +01001520 MAP_FLAG(UMP_UART_LSR_RX_MASK, LSR_RX_AVAIL) /* rx data available */
1521 MAP_FLAG(UMP_UART_LSR_TX_MASK, LSR_TX_EMPTY) /* tx hold reg empty */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
1523#undef MAP_FLAG
1524
1525 return lsr;
1526}
1527
Alan Cox2742fd82008-04-29 14:45:15 +01001528static void handle_new_msr(struct edgeport_port *edge_port, __u8 msr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529{
1530 struct async_icount *icount;
1531 struct tty_struct *tty;
1532
Alan Cox2742fd82008-04-29 14:45:15 +01001533 dbg("%s - %02x", __func__, msr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Alan Cox2742fd82008-04-29 14:45:15 +01001535 if (msr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
1536 EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 icount = &edge_port->icount;
1538
1539 /* update input line counters */
1540 if (msr & EDGEPORT_MSR_DELTA_CTS)
1541 icount->cts++;
1542 if (msr & EDGEPORT_MSR_DELTA_DSR)
1543 icount->dsr++;
1544 if (msr & EDGEPORT_MSR_DELTA_CD)
1545 icount->dcd++;
1546 if (msr & EDGEPORT_MSR_DELTA_RI)
1547 icount->rng++;
Alan Cox2742fd82008-04-29 14:45:15 +01001548 wake_up_interruptible(&edge_port->delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 }
1550
1551 /* Save the new modem status */
1552 edge_port->shadow_msr = msr & 0xf0;
1553
Alan Cox4a90f092008-10-13 10:39:46 +01001554 tty = tty_port_tty_get(&edge_port->port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 /* handle CTS flow control */
1556 if (tty && C_CRTSCTS(tty)) {
1557 if (msr & EDGEPORT_MSR_CTS) {
1558 tty->hw_stopped = 0;
1559 tty_wakeup(tty);
1560 } else {
1561 tty->hw_stopped = 1;
1562 }
1563 }
Alan Cox4a90f092008-10-13 10:39:46 +01001564 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565}
1566
Alan Cox2742fd82008-04-29 14:45:15 +01001567static void handle_new_lsr(struct edgeport_port *edge_port, int lsr_data,
1568 __u8 lsr, __u8 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
1570 struct async_icount *icount;
Alan Cox2742fd82008-04-29 14:45:15 +01001571 __u8 new_lsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR |
1572 LSR_FRM_ERR | LSR_BREAK));
Alan Cox4a90f092008-10-13 10:39:46 +01001573 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Alan Cox2742fd82008-04-29 14:45:15 +01001575 dbg("%s - %02x", __func__, new_lsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
1577 edge_port->shadow_lsr = lsr;
1578
Alan Cox2742fd82008-04-29 14:45:15 +01001579 if (new_lsr & LSR_BREAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 /*
1581 * Parity and Framing errors only count if they
1582 * occur exclusive of a break being received.
1583 */
1584 new_lsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
1586 /* Place LSR data byte into Rx buffer */
Alan Cox4a90f092008-10-13 10:39:46 +01001587 if (lsr_data) {
1588 tty = tty_port_tty_get(&edge_port->port->port);
1589 if (tty) {
1590 edge_tty_recv(&edge_port->port->dev, tty, &data, 1);
1591 tty_kref_put(tty);
1592 }
1593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
1595 /* update input line counters */
1596 icount = &edge_port->icount;
1597 if (new_lsr & LSR_BREAK)
1598 icount->brk++;
1599 if (new_lsr & LSR_OVER_ERR)
1600 icount->overrun++;
1601 if (new_lsr & LSR_PAR_ERR)
1602 icount->parity++;
1603 if (new_lsr & LSR_FRM_ERR)
1604 icount->frame++;
1605}
1606
1607
Alan Cox2742fd82008-04-29 14:45:15 +01001608static void edge_interrupt_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609{
Ming Leicdc97792008-02-24 18:41:47 +08001610 struct edgeport_serial *edge_serial = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 struct usb_serial_port *port;
1612 struct edgeport_port *edge_port;
1613 unsigned char *data = urb->transfer_buffer;
1614 int length = urb->actual_length;
1615 int port_number;
1616 int function;
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001617 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 __u8 lsr;
1619 __u8 msr;
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001620 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
Harvey Harrison441b62c2008-03-03 16:08:34 -08001622 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001624 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 case 0:
1626 /* success */
1627 break;
1628 case -ECONNRESET:
1629 case -ENOENT:
1630 case -ESHUTDOWN:
1631 /* this urb is terminated, clean up */
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001632 dbg("%s - urb shutting down with status: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001633 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 return;
1635 default:
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001636 dev_err(&urb->dev->dev, "%s - nonzero urb status received: "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001637 "%d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 goto exit;
1639 }
1640
1641 if (!length) {
Alan Cox2742fd82008-04-29 14:45:15 +01001642 dbg("%s - no data in urb", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 goto exit;
1644 }
1645
Alan Cox2742fd82008-04-29 14:45:15 +01001646 usb_serial_debug_data(debug, &edge_serial->serial->dev->dev,
1647 __func__, length, data);
1648
1649 if (length != 2) {
1650 dbg("%s - expecting packet of size 2, got %d",
1651 __func__, length);
1652 goto exit;
1653 }
1654
1655 port_number = TIUMP_GET_PORT_FROM_CODE(data[0]);
1656 function = TIUMP_GET_FUNC_FROM_CODE(data[0]);
1657 dbg("%s - port_number %d, function %d, info 0x%x",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001658 __func__, port_number, function, data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 port = edge_serial->serial->port[port_number];
1660 edge_port = usb_get_serial_port_data(port);
1661 if (!edge_port) {
Alan Cox2742fd82008-04-29 14:45:15 +01001662 dbg("%s - edge_port not found", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 return;
1664 }
1665 switch (function) {
1666 case TIUMP_INTERRUPT_CODE_LSR:
Alan Cox2742fd82008-04-29 14:45:15 +01001667 lsr = map_line_status(data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 if (lsr & UMP_UART_LSR_DATA_MASK) {
Alan Cox2742fd82008-04-29 14:45:15 +01001669 /* Save the LSR event for bulk read
1670 completion routine */
1671 dbg("%s - LSR Event Port %u LSR Status = %02x",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001672 __func__, port_number, lsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 edge_port->lsr_event = 1;
1674 edge_port->lsr_mask = lsr;
1675 } else {
Alan Cox2742fd82008-04-29 14:45:15 +01001676 dbg("%s - ===== Port %d LSR Status = %02x ======",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001677 __func__, port_number, lsr);
Alan Cox2742fd82008-04-29 14:45:15 +01001678 handle_new_lsr(edge_port, 0, lsr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 }
1680 break;
1681
Alan Cox2742fd82008-04-29 14:45:15 +01001682 case TIUMP_INTERRUPT_CODE_MSR: /* MSR */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 /* Copy MSR from UMP */
1684 msr = data[1];
Joe Perches759f3632010-02-05 16:50:08 -08001685 dbg("%s - ===== Port %u MSR Status = %02x ======",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001686 __func__, port_number, msr);
Alan Cox2742fd82008-04-29 14:45:15 +01001687 handle_new_msr(edge_port, msr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 break;
1689
1690 default:
Alan Cox2742fd82008-04-29 14:45:15 +01001691 dev_err(&urb->dev->dev,
1692 "%s - Unknown Interrupt code from UMP %x\n",
1693 __func__, data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 break;
Alan Cox2742fd82008-04-29 14:45:15 +01001695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 }
1697
1698exit:
Alan Cox2742fd82008-04-29 14:45:15 +01001699 retval = usb_submit_urb(urb, GFP_ATOMIC);
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001700 if (retval)
Alan Cox2742fd82008-04-29 14:45:15 +01001701 dev_err(&urb->dev->dev,
1702 "%s - usb_submit_urb failed with result %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001703 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704}
1705
Alan Cox2742fd82008-04-29 14:45:15 +01001706static void edge_bulk_in_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707{
Ming Leicdc97792008-02-24 18:41:47 +08001708 struct edgeport_port *edge_port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 unsigned char *data = urb->transfer_buffer;
1710 struct tty_struct *tty;
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001711 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 int port_number;
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001713 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Harvey Harrison441b62c2008-03-03 16:08:34 -08001715 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001717 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 case 0:
1719 /* success */
1720 break;
1721 case -ECONNRESET:
1722 case -ENOENT:
1723 case -ESHUTDOWN:
1724 /* this urb is terminated, clean up */
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001725 dbg("%s - urb shutting down with status: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001726 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 return;
1728 default:
Alan Cox2742fd82008-04-29 14:45:15 +01001729 dev_err(&urb->dev->dev,
1730 "%s - nonzero read bulk status received: %d\n",
1731 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 }
1733
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001734 if (status == -EPIPE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 goto exit;
1736
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001737 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001738 dev_err(&urb->dev->dev, "%s - stopping read!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 return;
1740 }
1741
1742 port_number = edge_port->port->number - edge_port->port->serial->minor;
1743
1744 if (edge_port->lsr_event) {
1745 edge_port->lsr_event = 0;
Alan Cox2742fd82008-04-29 14:45:15 +01001746 dbg("%s ===== Port %u LSR Status = %02x, Data = %02x ======",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001747 __func__, port_number, edge_port->lsr_mask, *data);
Alan Cox2742fd82008-04-29 14:45:15 +01001748 handle_new_lsr(edge_port, 1, edge_port->lsr_mask, *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 /* Adjust buffer length/pointer */
1750 --urb->actual_length;
1751 ++data;
1752 }
1753
Alan Cox4a90f092008-10-13 10:39:46 +01001754 tty = tty_port_tty_get(&edge_port->port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 if (tty && urb->actual_length) {
Alan Cox2742fd82008-04-29 14:45:15 +01001756 usb_serial_debug_data(debug, &edge_port->port->dev,
1757 __func__, urb->actual_length, data);
1758 if (edge_port->close_pending)
1759 dbg("%s - close pending, dropping data on the floor",
1760 __func__);
1761 else
1762 edge_tty_recv(&edge_port->port->dev, tty, data,
1763 urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 edge_port->icount.rx += urb->actual_length;
1765 }
Alan Cox4a90f092008-10-13 10:39:46 +01001766 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
1768exit:
1769 /* continue read unless stopped */
1770 spin_lock(&edge_port->ep_lock);
Johan Hovold58330412011-11-06 19:06:28 +01001771 if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001772 retval = usb_submit_urb(urb, GFP_ATOMIC);
Johan Hovold58330412011-11-06 19:06:28 +01001773 else if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPED;
Johan Hovold58330412011-11-06 19:06:28 +01001775
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 spin_unlock(&edge_port->ep_lock);
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001777 if (retval)
Alan Cox2742fd82008-04-29 14:45:15 +01001778 dev_err(&urb->dev->dev,
1779 "%s - usb_submit_urb failed with result %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001780 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781}
1782
Alan Cox2742fd82008-04-29 14:45:15 +01001783static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
1784 unsigned char *data, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785{
Alan Cox2742fd82008-04-29 14:45:15 +01001786 int queued;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Alan Cox2742fd82008-04-29 14:45:15 +01001788 queued = tty_insert_flip_string(tty, data, length);
1789 if (queued < length)
1790 dev_err(dev, "%s - dropping data, %d bytes lost\n",
1791 __func__, length - queued);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 tty_flip_buffer_push(tty);
1793}
1794
Alan Cox2742fd82008-04-29 14:45:15 +01001795static void edge_bulk_out_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796{
Ming Leicdc97792008-02-24 18:41:47 +08001797 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001799 int status = urb->status;
Alan Cox4a90f092008-10-13 10:39:46 +01001800 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
Alan Cox2742fd82008-04-29 14:45:15 +01001802 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
1804 edge_port->ep_write_urb_in_use = 0;
1805
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001806 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 case 0:
1808 /* success */
1809 break;
1810 case -ECONNRESET:
1811 case -ENOENT:
1812 case -ESHUTDOWN:
1813 /* this urb is terminated, clean up */
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001814 dbg("%s - urb shutting down with status: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001815 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 return;
1817 default:
Johan Hovold22a416c2012-02-10 13:20:51 +01001818 dev_err_console(port, "%s - nonzero write bulk status "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001819 "received: %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 }
1821
1822 /* send any buffered data */
Alan Cox4a90f092008-10-13 10:39:46 +01001823 tty = tty_port_tty_get(&port->port);
1824 edge_send(tty);
1825 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826}
1827
Alan Coxa509a7e2009-09-19 13:13:26 -07001828static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829{
1830 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1831 struct edgeport_serial *edge_serial;
1832 struct usb_device *dev;
1833 struct urb *urb;
1834 int port_number;
1835 int status;
1836 u16 open_settings;
1837 u8 transaction_timeout;
1838
Harvey Harrison441b62c2008-03-03 16:08:34 -08001839 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
1841 if (edge_port == NULL)
1842 return -ENODEV;
1843
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 port_number = port->number - port->serial->minor;
1845 switch (port_number) {
Alan Cox2742fd82008-04-29 14:45:15 +01001846 case 0:
1847 edge_port->uart_base = UMPMEM_BASE_UART1;
1848 edge_port->dma_address = UMPD_OEDB1_ADDRESS;
1849 break;
1850 case 1:
1851 edge_port->uart_base = UMPMEM_BASE_UART2;
1852 edge_port->dma_address = UMPD_OEDB2_ADDRESS;
1853 break;
1854 default:
1855 dev_err(&port->dev, "Unknown port number!!!\n");
1856 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 }
1858
Alan Cox2742fd82008-04-29 14:45:15 +01001859 dbg("%s - port_number = %d, uart_base = %04x, dma_address = %04x",
1860 __func__, port_number, edge_port->uart_base,
1861 edge_port->dma_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
1863 dev = port->serial->dev;
1864
Alan Cox2742fd82008-04-29 14:45:15 +01001865 memset(&(edge_port->icount), 0x00, sizeof(edge_port->icount));
1866 init_waitqueue_head(&edge_port->delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
1868 /* turn off loopback */
Alan Cox2742fd82008-04-29 14:45:15 +01001869 status = ti_do_config(edge_port, UMPC_SET_CLR_LOOPBACK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001871 dev_err(&port->dev,
1872 "%s - cannot send clear loopback command, %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001873 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 return status;
1875 }
Alan Cox2742fd82008-04-29 14:45:15 +01001876
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 /* set up the port settings */
Alan Cox95da3102008-07-22 11:09:07 +01001878 if (tty)
Alan Cox4a90f092008-10-13 10:39:46 +01001879 edge_set_termios(tty, port, tty->termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
1881 /* open up the port */
1882
1883 /* milliseconds to timeout for DMA transfer */
1884 transaction_timeout = 2;
1885
Alan Cox2742fd82008-04-29 14:45:15 +01001886 edge_port->ump_read_timeout =
1887 max(20, ((transaction_timeout * 3) / 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
Alan Cox2742fd82008-04-29 14:45:15 +01001889 /* milliseconds to timeout for DMA transfer */
1890 open_settings = (u8)(UMP_DMA_MODE_CONTINOUS |
1891 UMP_PIPE_TRANS_TIMEOUT_ENA |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 (transaction_timeout << 2));
1893
Alan Cox2742fd82008-04-29 14:45:15 +01001894 dbg("%s - Sending UMPC_OPEN_PORT", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
1896 /* Tell TI to open and start the port */
Alan Cox2742fd82008-04-29 14:45:15 +01001897 status = send_cmd(dev, UMPC_OPEN_PORT,
1898 (u8)(UMPM_UART1_PORT + port_number), open_settings, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001900 dev_err(&port->dev, "%s - cannot send open command, %d\n",
1901 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 return status;
1903 }
1904
1905 /* Start the DMA? */
Alan Cox2742fd82008-04-29 14:45:15 +01001906 status = send_cmd(dev, UMPC_START_PORT,
1907 (u8)(UMPM_UART1_PORT + port_number), 0, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001909 dev_err(&port->dev, "%s - cannot send start DMA command, %d\n",
1910 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 return status;
1912 }
1913
1914 /* Clear TX and RX buffers in UMP */
Alan Cox2742fd82008-04-29 14:45:15 +01001915 status = purge_port(port, UMP_PORT_DIR_OUT | UMP_PORT_DIR_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001917 dev_err(&port->dev,
1918 "%s - cannot send clear buffers command, %d\n",
1919 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 return status;
1921 }
1922
1923 /* Read Initial MSR */
Alan Cox2742fd82008-04-29 14:45:15 +01001924 status = ti_vread_sync(dev, UMPC_READ_MSR, 0,
1925 (__u16)(UMPM_UART1_PORT + port_number),
1926 &edge_port->shadow_msr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001928 dev_err(&port->dev, "%s - cannot send read MSR command, %d\n",
1929 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 return status;
1931 }
1932
Alan Cox2742fd82008-04-29 14:45:15 +01001933 dbg("ShadowMSR 0x%X", edge_port->shadow_msr);
1934
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 /* Set Initial MCR */
1936 edge_port->shadow_mcr = MCR_RTS | MCR_DTR;
Alan Cox2742fd82008-04-29 14:45:15 +01001937 dbg("ShadowMCR 0x%X", edge_port->shadow_mcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
1939 edge_serial = edge_port->edge_serial;
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01001940 if (mutex_lock_interruptible(&edge_serial->es_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 return -ERESTARTSYS;
1942 if (edge_serial->num_ports_open == 0) {
Alan Cox2742fd82008-04-29 14:45:15 +01001943 /* we are the first port to open, post the interrupt urb */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 urb = edge_serial->serial->port[0]->interrupt_in_urb;
1945 if (!urb) {
Alan Cox2742fd82008-04-29 14:45:15 +01001946 dev_err(&port->dev,
1947 "%s - no interrupt urb present, exiting\n",
1948 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 status = -EINVAL;
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01001950 goto release_es_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 urb->context = edge_serial;
Alan Cox2742fd82008-04-29 14:45:15 +01001953 status = usb_submit_urb(urb, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001955 dev_err(&port->dev,
1956 "%s - usb_submit_urb failed with value %d\n",
1957 __func__, status);
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01001958 goto release_es_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 }
1960 }
1961
1962 /*
1963 * reset the data toggle on the bulk endpoints to work around bug in
1964 * host controllers where things get out of sync some times
1965 */
Alan Cox2742fd82008-04-29 14:45:15 +01001966 usb_clear_halt(dev, port->write_urb->pipe);
1967 usb_clear_halt(dev, port->read_urb->pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
1969 /* start up our bulk read urb */
1970 urb = port->read_urb;
1971 if (!urb) {
Alan Cox2742fd82008-04-29 14:45:15 +01001972 dev_err(&port->dev, "%s - no read urb present, exiting\n",
1973 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 status = -EINVAL;
1975 goto unlink_int_urb;
1976 }
1977 edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 urb->context = edge_port;
Alan Cox2742fd82008-04-29 14:45:15 +01001979 status = usb_submit_urb(urb, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001981 dev_err(&port->dev,
1982 "%s - read bulk usb_submit_urb failed with value %d\n",
1983 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 goto unlink_int_urb;
1985 }
1986
1987 ++edge_serial->num_ports_open;
1988
Harvey Harrison441b62c2008-03-03 16:08:34 -08001989 dbg("%s - exited", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01001991 goto release_es_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
1993unlink_int_urb:
1994 if (edge_port->edge_serial->num_ports_open == 0)
1995 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01001996release_es_lock:
1997 mutex_unlock(&edge_serial->es_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 return status;
1999}
2000
Alan Cox335f8512009-06-11 12:26:29 +01002001static void edge_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002{
2003 struct edgeport_serial *edge_serial;
2004 struct edgeport_port *edge_port;
Johan Hovoldaf581052012-03-26 20:31:38 +02002005 struct usb_serial *serial = port->serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 int port_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Harvey Harrison441b62c2008-03-03 16:08:34 -08002008 dbg("%s - port %d", __func__, port->number);
Alan Cox2742fd82008-04-29 14:45:15 +01002009
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 edge_serial = usb_get_serial_data(port->serial);
2011 edge_port = usb_get_serial_port_data(port);
Alan Cox2742fd82008-04-29 14:45:15 +01002012 if (edge_serial == NULL || edge_port == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 return;
Alan Cox2742fd82008-04-29 14:45:15 +01002014
2015 /* The bulkreadcompletion routine will check
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 * this flag and dump add read data */
2017 edge_port->close_pending = 1;
2018
2019 /* chase the port close and flush */
Alan Cox2742fd82008-04-29 14:45:15 +01002020 chase_port(edge_port, (HZ * closing_wait) / 100, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
2022 usb_kill_urb(port->read_urb);
2023 usb_kill_urb(port->write_urb);
2024 edge_port->ep_write_urb_in_use = 0;
2025
2026 /* assuming we can still talk to the device,
2027 * send a close port command to it */
Harvey Harrison441b62c2008-03-03 16:08:34 -08002028 dbg("%s - send umpc_close_port", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 port_number = port->number - port->serial->minor;
Johan Hovoldaf581052012-03-26 20:31:38 +02002030
2031 mutex_lock(&serial->disc_mutex);
2032 if (!serial->disconnected) {
2033 send_cmd(serial->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 UMPC_CLOSE_PORT,
2035 (__u8)(UMPM_UART1_PORT + port_number),
2036 0,
2037 NULL,
2038 0);
Johan Hovoldaf581052012-03-26 20:31:38 +02002039 }
2040 mutex_unlock(&serial->disc_mutex);
2041
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01002042 mutex_lock(&edge_serial->es_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 --edge_port->edge_serial->num_ports_open;
2044 if (edge_port->edge_serial->num_ports_open <= 0) {
2045 /* last port is now closed, let's shut down our interrupt urb */
2046 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
2047 edge_port->edge_serial->num_ports_open = 0;
2048 }
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01002049 mutex_unlock(&edge_serial->es_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 edge_port->close_pending = 0;
2051
Harvey Harrison441b62c2008-03-03 16:08:34 -08002052 dbg("%s - exited", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053}
2054
Alan Cox95da3102008-07-22 11:09:07 +01002055static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
2056 const unsigned char *data, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057{
2058 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
Harvey Harrison441b62c2008-03-03 16:08:34 -08002060 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
2062 if (count == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002063 dbg("%s - write request of 0 bytes", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 return 0;
2065 }
2066
2067 if (edge_port == NULL)
2068 return -ENODEV;
2069 if (edge_port->close_pending == 1)
2070 return -ENODEV;
2071
Johan Hovoldd733cec2010-05-19 00:01:37 +02002072 count = kfifo_in_locked(&edge_port->write_fifo, data, count,
2073 &edge_port->ep_lock);
Alan Cox95da3102008-07-22 11:09:07 +01002074 edge_send(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
2076 return count;
2077}
2078
Alan Cox95da3102008-07-22 11:09:07 +01002079static void edge_send(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080{
Alan Cox95da3102008-07-22 11:09:07 +01002081 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 int count, result;
2083 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 unsigned long flags;
2085
2086
Harvey Harrison441b62c2008-03-03 16:08:34 -08002087 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
2089 spin_lock_irqsave(&edge_port->ep_lock, flags);
2090
2091 if (edge_port->ep_write_urb_in_use) {
2092 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2093 return;
2094 }
2095
Johan Hovoldd733cec2010-05-19 00:01:37 +02002096 count = kfifo_out(&edge_port->write_fifo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 port->write_urb->transfer_buffer,
2098 port->bulk_out_size);
2099
2100 if (count == 0) {
2101 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2102 return;
2103 }
2104
2105 edge_port->ep_write_urb_in_use = 1;
2106
2107 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2108
Alan Cox2742fd82008-04-29 14:45:15 +01002109 usb_serial_debug_data(debug, &port->dev, __func__, count,
2110 port->write_urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
2112 /* set up our urb */
Johan Hovoldfd119612011-11-06 19:06:30 +01002113 port->write_urb->transfer_buffer_length = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
2115 /* send the data out the bulk port */
2116 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
2117 if (result) {
Johan Hovold22a416c2012-02-10 13:20:51 +01002118 dev_err_console(port,
Alan Cox2742fd82008-04-29 14:45:15 +01002119 "%s - failed submitting write urb, error %d\n",
2120 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 edge_port->ep_write_urb_in_use = 0;
Alan Cox2742fd82008-04-29 14:45:15 +01002122 /* TODO: reschedule edge_send */
2123 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 edge_port->icount.tx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
2126 /* wakeup any process waiting for writes to complete */
2127 /* there is now more room in the buffer for new writes */
Alan Cox2742fd82008-04-29 14:45:15 +01002128 if (tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130}
2131
Alan Cox95da3102008-07-22 11:09:07 +01002132static int edge_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133{
Alan Cox95da3102008-07-22 11:09:07 +01002134 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2136 int room = 0;
2137 unsigned long flags;
2138
Harvey Harrison441b62c2008-03-03 16:08:34 -08002139 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
2141 if (edge_port == NULL)
Alan Cox2742fd82008-04-29 14:45:15 +01002142 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 if (edge_port->close_pending == 1)
Alan Cox2742fd82008-04-29 14:45:15 +01002144 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
2146 spin_lock_irqsave(&edge_port->ep_lock, flags);
Johan Hovoldd733cec2010-05-19 00:01:37 +02002147 room = kfifo_avail(&edge_port->write_fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2149
Harvey Harrison441b62c2008-03-03 16:08:34 -08002150 dbg("%s - returns %d", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 return room;
2152}
2153
Alan Cox95da3102008-07-22 11:09:07 +01002154static int edge_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155{
Alan Cox95da3102008-07-22 11:09:07 +01002156 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2158 int chars = 0;
2159 unsigned long flags;
2160
Harvey Harrison441b62c2008-03-03 16:08:34 -08002161 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
2163 if (edge_port == NULL)
Alan Cox2742fd82008-04-29 14:45:15 +01002164 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 if (edge_port->close_pending == 1)
Alan Cox2742fd82008-04-29 14:45:15 +01002166 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
2168 spin_lock_irqsave(&edge_port->ep_lock, flags);
Johan Hovoldd733cec2010-05-19 00:01:37 +02002169 chars = kfifo_len(&edge_port->write_fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2171
Alan Cox2742fd82008-04-29 14:45:15 +01002172 dbg("%s - returns %d", __func__, chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 return chars;
2174}
2175
Alan Cox95da3102008-07-22 11:09:07 +01002176static void edge_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177{
Alan Cox95da3102008-07-22 11:09:07 +01002178 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 int status;
2181
Harvey Harrison441b62c2008-03-03 16:08:34 -08002182 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
2184 if (edge_port == NULL)
2185 return;
2186
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 /* if we are implementing XON/XOFF, send the stop character */
2188 if (I_IXOFF(tty)) {
2189 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01002190 status = edge_write(tty, port, &stop_char, 1);
2191 if (status <= 0) {
2192 dev_err(&port->dev, "%s - failed to write stop character, %d\n", __func__, status);
2193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 }
2195
2196 /* if we are implementing RTS/CTS, stop reads */
2197 /* and the Edgeport will clear the RTS line */
2198 if (C_CRTSCTS(tty))
2199 stop_read(edge_port);
2200
2201}
2202
Alan Cox95da3102008-07-22 11:09:07 +01002203static void edge_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204{
Alan Cox95da3102008-07-22 11:09:07 +01002205 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 int status;
2208
Harvey Harrison441b62c2008-03-03 16:08:34 -08002209 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
2211 if (edge_port == NULL)
2212 return;
2213
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 /* if we are implementing XON/XOFF, send the start character */
2215 if (I_IXOFF(tty)) {
2216 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01002217 status = edge_write(tty, port, &start_char, 1);
2218 if (status <= 0) {
2219 dev_err(&port->dev, "%s - failed to write start character, %d\n", __func__, status);
2220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 /* if we are implementing RTS/CTS, restart reads */
2223 /* are the Edgeport will assert the RTS line */
2224 if (C_CRTSCTS(tty)) {
2225 status = restart_read(edge_port);
2226 if (status)
Alan Cox2742fd82008-04-29 14:45:15 +01002227 dev_err(&port->dev,
2228 "%s - read bulk usb_submit_urb failed: %d\n",
2229 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 }
2231
2232}
2233
2234static void stop_read(struct edgeport_port *edge_port)
2235{
2236 unsigned long flags;
2237
2238 spin_lock_irqsave(&edge_port->ep_lock, flags);
2239
2240 if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
2241 edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPING;
2242 edge_port->shadow_mcr &= ~MCR_RTS;
2243
2244 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2245}
2246
2247static int restart_read(struct edgeport_port *edge_port)
2248{
2249 struct urb *urb;
2250 int status = 0;
2251 unsigned long flags;
2252
2253 spin_lock_irqsave(&edge_port->ep_lock, flags);
2254
2255 if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPED) {
2256 urb = edge_port->port->read_urb;
Oliver Neukumefdff602007-06-05 10:50:48 +02002257 status = usb_submit_urb(urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 }
2259 edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
2260 edge_port->shadow_mcr |= MCR_RTS;
2261
2262 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2263
2264 return status;
2265}
2266
Alan Cox95da3102008-07-22 11:09:07 +01002267static void change_port_settings(struct tty_struct *tty,
2268 struct edgeport_port *edge_port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269{
2270 struct ump_uart_config *config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 int baud;
2272 unsigned cflag;
2273 int status;
Alan Cox2742fd82008-04-29 14:45:15 +01002274 int port_number = edge_port->port->number -
2275 edge_port->port->serial->minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
Harvey Harrison441b62c2008-03-03 16:08:34 -08002277 dbg("%s - port %d", __func__, edge_port->port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
Alan Cox95da3102008-07-22 11:09:07 +01002279 config = kmalloc (sizeof (*config), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 if (!config) {
Alan Cox2742fd82008-04-29 14:45:15 +01002281 *tty->termios = *old_termios;
2282 dev_err(&edge_port->port->dev, "%s - out of memory\n",
2283 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 return;
2285 }
2286
2287 cflag = tty->termios->c_cflag;
2288
2289 config->wFlags = 0;
2290
2291 /* These flags must be set */
2292 config->wFlags |= UMP_MASK_UART_FLAGS_RECEIVE_MS_INT;
2293 config->wFlags |= UMP_MASK_UART_FLAGS_AUTO_START_ON_ERR;
2294 config->bUartMode = (__u8)(edge_port->bUartMode);
2295
2296 switch (cflag & CSIZE) {
Alan Cox2742fd82008-04-29 14:45:15 +01002297 case CS5:
2298 config->bDataBits = UMP_UART_CHAR5BITS;
2299 dbg("%s - data bits = 5", __func__);
2300 break;
2301 case CS6:
2302 config->bDataBits = UMP_UART_CHAR6BITS;
2303 dbg("%s - data bits = 6", __func__);
2304 break;
2305 case CS7:
2306 config->bDataBits = UMP_UART_CHAR7BITS;
2307 dbg("%s - data bits = 7", __func__);
2308 break;
2309 default:
2310 case CS8:
2311 config->bDataBits = UMP_UART_CHAR8BITS;
2312 dbg("%s - data bits = 8", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 break;
2314 }
2315
2316 if (cflag & PARENB) {
2317 if (cflag & PARODD) {
2318 config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
2319 config->bParity = UMP_UART_ODDPARITY;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002320 dbg("%s - parity = odd", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 } else {
2322 config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
2323 config->bParity = UMP_UART_EVENPARITY;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002324 dbg("%s - parity = even", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 }
2326 } else {
Alan Cox2742fd82008-04-29 14:45:15 +01002327 config->bParity = UMP_UART_NOPARITY;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002328 dbg("%s - parity = none", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 }
2330
2331 if (cflag & CSTOPB) {
2332 config->bStopBits = UMP_UART_STOPBIT2;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002333 dbg("%s - stop bits = 2", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 } else {
2335 config->bStopBits = UMP_UART_STOPBIT1;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002336 dbg("%s - stop bits = 1", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 }
2338
2339 /* figure out the flow control settings */
2340 if (cflag & CRTSCTS) {
2341 config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X_CTS_FLOW;
2342 config->wFlags |= UMP_MASK_UART_FLAGS_RTS_FLOW;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002343 dbg("%s - RTS/CTS is enabled", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002345 dbg("%s - RTS/CTS is disabled", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 tty->hw_stopped = 0;
2347 restart_read(edge_port);
2348 }
2349
Alan Cox2742fd82008-04-29 14:45:15 +01002350 /* if we are implementing XON/XOFF, set the start and stop
2351 character in the device */
2352 config->cXon = START_CHAR(tty);
2353 config->cXoff = STOP_CHAR(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
Alan Cox2742fd82008-04-29 14:45:15 +01002355 /* if we are implementing INBOUND XON/XOFF */
2356 if (I_IXOFF(tty)) {
2357 config->wFlags |= UMP_MASK_UART_FLAGS_IN_X;
2358 dbg("%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2359 __func__, config->cXon, config->cXoff);
2360 } else
2361 dbg("%s - INBOUND XON/XOFF is disabled", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
Alan Cox2742fd82008-04-29 14:45:15 +01002363 /* if we are implementing OUTBOUND XON/XOFF */
2364 if (I_IXON(tty)) {
2365 config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X;
2366 dbg("%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2367 __func__, config->cXon, config->cXoff);
2368 } else
2369 dbg("%s - OUTBOUND XON/XOFF is disabled", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
Alan Coxd5f5bcd2008-01-03 16:57:33 +00002371 tty->termios->c_cflag &= ~CMSPAR;
2372
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 /* Round the baud rate */
2374 baud = tty_get_baud_rate(tty);
2375 if (!baud) {
2376 /* pick a default, any default... */
2377 baud = 9600;
Alan Coxd5f5bcd2008-01-03 16:57:33 +00002378 } else
2379 tty_encode_baud_rate(tty, baud, baud);
2380
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 edge_port->baud_rate = baud;
2382 config->wBaudRate = (__u16)((461550L + baud/2) / baud);
2383
Alan Coxd5f5bcd2008-01-03 16:57:33 +00002384 /* FIXME: Recompute actual baud from divisor here */
2385
Alan Cox2742fd82008-04-29 14:45:15 +01002386 dbg("%s - baud rate = %d, wBaudRate = %d", __func__, baud,
2387 config->wBaudRate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
Alan Cox2742fd82008-04-29 14:45:15 +01002389 dbg("wBaudRate: %d", (int)(461550L / config->wBaudRate));
2390 dbg("wFlags: 0x%x", config->wFlags);
2391 dbg("bDataBits: %d", config->bDataBits);
2392 dbg("bParity: %d", config->bParity);
2393 dbg("bStopBits: %d", config->bStopBits);
2394 dbg("cXon: %d", config->cXon);
2395 dbg("cXoff: %d", config->cXoff);
2396 dbg("bUartMode: %d", config->bUartMode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
2398 /* move the word values into big endian mode */
Alan Cox2742fd82008-04-29 14:45:15 +01002399 cpu_to_be16s(&config->wFlags);
2400 cpu_to_be16s(&config->wBaudRate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
Alan Cox2742fd82008-04-29 14:45:15 +01002402 status = send_cmd(edge_port->port->serial->dev, UMPC_SET_CONFIG,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 (__u8)(UMPM_UART1_PORT + port_number),
Alan Cox2742fd82008-04-29 14:45:15 +01002404 0, (__u8 *)config, sizeof(*config));
2405 if (status)
2406 dbg("%s - error %d when trying to write config to device",
Harvey Harrison441b62c2008-03-03 16:08:34 -08002407 __func__, status);
Alan Cox2742fd82008-04-29 14:45:15 +01002408 kfree(config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409}
2410
Alan Cox2e0ddd62008-07-22 11:12:33 +01002411static void edge_set_termios(struct tty_struct *tty,
Alan Cox95da3102008-07-22 11:09:07 +01002412 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413{
2414 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Alan Cox95da3102008-07-22 11:09:07 +01002415 unsigned int cflag;
2416
2417 cflag = tty->termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418
Harvey Harrison441b62c2008-03-03 16:08:34 -08002419 dbg("%s - clfag %08x iflag %08x", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 tty->termios->c_cflag, tty->termios->c_iflag);
Harvey Harrison441b62c2008-03-03 16:08:34 -08002421 dbg("%s - old clfag %08x old iflag %08x", __func__,
Alan Coxd5f5bcd2008-01-03 16:57:33 +00002422 old_termios->c_cflag, old_termios->c_iflag);
Harvey Harrison441b62c2008-03-03 16:08:34 -08002423 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
2425 if (edge_port == NULL)
2426 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 /* change the port settings to the new ones specified */
Alan Cox95da3102008-07-22 11:09:07 +01002428 change_port_settings(tty, edge_port, old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429}
2430
Alan Cox20b9d172011-02-14 16:26:50 +00002431static int edge_tiocmset(struct tty_struct *tty,
Alan Cox2742fd82008-04-29 14:45:15 +01002432 unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433{
Alan Cox95da3102008-07-22 11:09:07 +01002434 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2436 unsigned int mcr;
Alan Cox3d71fe0b2008-02-20 21:38:32 +00002437 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
Harvey Harrison441b62c2008-03-03 16:08:34 -08002439 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440
Alan Cox3d71fe0b2008-02-20 21:38:32 +00002441 spin_lock_irqsave(&edge_port->ep_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 mcr = edge_port->shadow_mcr;
2443 if (set & TIOCM_RTS)
2444 mcr |= MCR_RTS;
2445 if (set & TIOCM_DTR)
2446 mcr |= MCR_DTR;
2447 if (set & TIOCM_LOOP)
2448 mcr |= MCR_LOOPBACK;
2449
2450 if (clear & TIOCM_RTS)
2451 mcr &= ~MCR_RTS;
2452 if (clear & TIOCM_DTR)
2453 mcr &= ~MCR_DTR;
2454 if (clear & TIOCM_LOOP)
2455 mcr &= ~MCR_LOOPBACK;
2456
2457 edge_port->shadow_mcr = mcr;
Alan Cox3d71fe0b2008-02-20 21:38:32 +00002458 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
Alan Cox2742fd82008-04-29 14:45:15 +01002460 restore_mcr(edge_port, mcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 return 0;
2462}
2463
Alan Cox60b33c12011-02-14 16:26:14 +00002464static int edge_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465{
Alan Cox95da3102008-07-22 11:09:07 +01002466 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2468 unsigned int result = 0;
2469 unsigned int msr;
2470 unsigned int mcr;
Alan Cox3d71fe0b2008-02-20 21:38:32 +00002471 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
Harvey Harrison441b62c2008-03-03 16:08:34 -08002473 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
Alan Cox3d71fe0b2008-02-20 21:38:32 +00002475 spin_lock_irqsave(&edge_port->ep_lock, flags);
2476
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 msr = edge_port->shadow_msr;
2478 mcr = edge_port->shadow_mcr;
2479 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
2480 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
2481 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
2482 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
2483 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
2484 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
2485
2486
Harvey Harrison441b62c2008-03-03 16:08:34 -08002487 dbg("%s -- %x", __func__, result);
Alan Cox3d71fe0b2008-02-20 21:38:32 +00002488 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
2490 return result;
2491}
2492
Alan Cox0bca1b92010-09-16 18:21:40 +01002493static int edge_get_icount(struct tty_struct *tty,
2494 struct serial_icounter_struct *icount)
2495{
2496 struct usb_serial_port *port = tty->driver_data;
2497 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2498 struct async_icount *ic = &edge_port->icount;
2499
2500 icount->cts = ic->cts;
2501 icount->dsr = ic->dsr;
2502 icount->rng = ic->rng;
2503 icount->dcd = ic->dcd;
2504 icount->tx = ic->tx;
2505 icount->rx = ic->rx;
2506 icount->frame = ic->frame;
2507 icount->parity = ic->parity;
2508 icount->overrun = ic->overrun;
2509 icount->brk = ic->brk;
2510 icount->buf_overrun = ic->buf_overrun;
2511 return 0;
2512}
2513
Alan Cox2742fd82008-04-29 14:45:15 +01002514static int get_serial_info(struct edgeport_port *edge_port,
2515 struct serial_struct __user *retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516{
2517 struct serial_struct tmp;
2518
2519 if (!retinfo)
2520 return -EFAULT;
2521
2522 memset(&tmp, 0, sizeof(tmp));
2523
2524 tmp.type = PORT_16550A;
2525 tmp.line = edge_port->port->serial->minor;
2526 tmp.port = edge_port->port->number;
2527 tmp.irq = 0;
2528 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2529 tmp.xmit_fifo_size = edge_port->port->bulk_out_size;
2530 tmp.baud_base = 9600;
2531 tmp.close_delay = 5*HZ;
2532 tmp.closing_wait = closing_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
2534 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2535 return -EFAULT;
2536 return 0;
2537}
2538
Alan Cox00a0d0d2011-02-14 16:27:06 +00002539static int edge_ioctl(struct tty_struct *tty,
Alan Cox2742fd82008-04-29 14:45:15 +01002540 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541{
Alan Cox95da3102008-07-22 11:09:07 +01002542 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2544 struct async_icount cnow;
2545 struct async_icount cprev;
2546
Harvey Harrison441b62c2008-03-03 16:08:34 -08002547 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548
2549 switch (cmd) {
Alan Cox2742fd82008-04-29 14:45:15 +01002550 case TIOCGSERIAL:
2551 dbg("%s - (%d) TIOCGSERIAL", __func__, port->number);
2552 return get_serial_info(edge_port,
2553 (struct serial_struct __user *) arg);
2554 case TIOCMIWAIT:
2555 dbg("%s - (%d) TIOCMIWAIT", __func__, port->number);
2556 cprev = edge_port->icount;
2557 while (1) {
2558 interruptible_sleep_on(&edge_port->delta_msr_wait);
2559 /* see if a signal did it */
2560 if (signal_pending(current))
2561 return -ERESTARTSYS;
2562 cnow = edge_port->icount;
2563 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2564 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2565 return -EIO; /* no change => error */
2566 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2567 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2568 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2569 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2570 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 }
Alan Cox2742fd82008-04-29 14:45:15 +01002572 cprev = cnow;
2573 }
2574 /* not reached */
2575 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 return -ENOIOCTLCMD;
2578}
2579
Alan Cox95da3102008-07-22 11:09:07 +01002580static void edge_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581{
Alan Cox95da3102008-07-22 11:09:07 +01002582 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2584 int status;
Alan Cox2742fd82008-04-29 14:45:15 +01002585 int bv = 0; /* Off */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
Alan Cox95da3102008-07-22 11:09:07 +01002587 dbg("%s - state = %d", __func__, break_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588
2589 /* chase the port close */
Alan Cox2742fd82008-04-29 14:45:15 +01002590 chase_port(edge_port, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
Alan Cox95da3102008-07-22 11:09:07 +01002592 if (break_state == -1)
Alan Cox2742fd82008-04-29 14:45:15 +01002593 bv = 1; /* On */
2594 status = ti_do_config(edge_port, UMPC_SET_CLR_BREAK, bv);
2595 if (status)
2596 dbg("%s - error %d sending break set/clear command.",
Harvey Harrison441b62c2008-03-03 16:08:34 -08002597 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598}
2599
Alan Cox2742fd82008-04-29 14:45:15 +01002600static int edge_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601{
2602 struct edgeport_serial *edge_serial;
2603 struct edgeport_port *edge_port;
2604 struct usb_device *dev;
2605 int status;
2606 int i;
2607
2608 dev = serial->dev;
2609
2610 /* create our private serial structure */
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +01002611 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 if (edge_serial == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002613 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 return -ENOMEM;
2615 }
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01002616 mutex_init(&edge_serial->es_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 edge_serial->serial = serial;
2618 usb_set_serial_data(serial, edge_serial);
2619
Alan Cox2742fd82008-04-29 14:45:15 +01002620 status = download_fw(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01002622 kfree(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 return status;
2624 }
2625
2626 /* set up our port private structures */
2627 for (i = 0; i < serial->num_ports; ++i) {
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +01002628 edge_port = kzalloc(sizeof(struct edgeport_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 if (edge_port == NULL) {
Alan Cox2742fd82008-04-29 14:45:15 +01002630 dev_err(&serial->dev->dev, "%s - Out of memory\n",
2631 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 goto cleanup;
2633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 spin_lock_init(&edge_port->ep_lock);
Johan Hovoldd733cec2010-05-19 00:01:37 +02002635 if (kfifo_alloc(&edge_port->write_fifo, EDGE_OUT_BUF_SIZE,
2636 GFP_KERNEL)) {
Alan Cox2742fd82008-04-29 14:45:15 +01002637 dev_err(&serial->dev->dev, "%s - Out of memory\n",
2638 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 kfree(edge_port);
2640 goto cleanup;
2641 }
2642 edge_port->port = serial->port[i];
2643 edge_port->edge_serial = edge_serial;
2644 usb_set_serial_port_data(serial->port[i], edge_port);
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002645 edge_port->bUartMode = default_uart_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 }
Alan Cox2742fd82008-04-29 14:45:15 +01002647
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 return 0;
2649
2650cleanup:
Alan Cox2742fd82008-04-29 14:45:15 +01002651 for (--i; i >= 0; --i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 edge_port = usb_get_serial_port_data(serial->port[i]);
Johan Hovoldd733cec2010-05-19 00:01:37 +02002653 kfifo_free(&edge_port->write_fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 kfree(edge_port);
2655 usb_set_serial_port_data(serial->port[i], NULL);
2656 }
Alan Cox2742fd82008-04-29 14:45:15 +01002657 kfree(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 usb_set_serial_data(serial, NULL);
2659 return -ENOMEM;
2660}
2661
Alan Sternf9c99bb2009-06-02 11:53:55 -04002662static void edge_disconnect(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663{
Alan Cox2742fd82008-04-29 14:45:15 +01002664 dbg("%s", __func__);
Alan Sternf9c99bb2009-06-02 11:53:55 -04002665}
2666
2667static void edge_release(struct usb_serial *serial)
2668{
2669 int i;
2670 struct edgeport_port *edge_port;
2671
2672 dbg("%s", __func__);
2673
2674 for (i = 0; i < serial->num_ports; ++i) {
2675 edge_port = usb_get_serial_port_data(serial->port[i]);
Johan Hovoldd733cec2010-05-19 00:01:37 +02002676 kfifo_free(&edge_port->write_fifo);
Jesper Juhl0d46c002007-07-16 22:17:25 +02002677 kfree(edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 }
Jesper Juhl0d46c002007-07-16 22:17:25 +02002679 kfree(usb_get_serial_data(serial));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680}
2681
2682
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002683/* Sysfs Attributes */
2684
2685static ssize_t show_uart_mode(struct device *dev,
2686 struct device_attribute *attr, char *buf)
2687{
2688 struct usb_serial_port *port = to_usb_serial_port(dev);
2689 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2690
2691 return sprintf(buf, "%d\n", edge_port->bUartMode);
2692}
2693
2694static ssize_t store_uart_mode(struct device *dev,
2695 struct device_attribute *attr, const char *valbuf, size_t count)
2696{
2697 struct usb_serial_port *port = to_usb_serial_port(dev);
2698 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2699 unsigned int v = simple_strtoul(valbuf, NULL, 0);
2700
Harvey Harrison441b62c2008-03-03 16:08:34 -08002701 dbg("%s: setting uart_mode = %d", __func__, v);
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002702
2703 if (v < 256)
2704 edge_port->bUartMode = v;
2705 else
Harvey Harrison441b62c2008-03-03 16:08:34 -08002706 dev_err(dev, "%s - uart_mode %d is invalid\n", __func__, v);
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002707
2708 return count;
2709}
2710
Alan Cox2742fd82008-04-29 14:45:15 +01002711static DEVICE_ATTR(uart_mode, S_IWUSR | S_IRUGO, show_uart_mode,
2712 store_uart_mode);
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002713
2714static int edge_create_sysfs_attrs(struct usb_serial_port *port)
2715{
2716 return device_create_file(&port->dev, &dev_attr_uart_mode);
2717}
2718
2719static int edge_remove_sysfs_attrs(struct usb_serial_port *port)
2720{
2721 device_remove_file(&port->dev, &dev_attr_uart_mode);
2722 return 0;
2723}
2724
2725
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -07002726static struct usb_serial_driver edgeport_1port_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07002727 .driver = {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07002728 .owner = THIS_MODULE,
2729 .name = "edgeport_ti_1",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07002730 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07002731 .description = "Edgeport TI 1 port adapter",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 .id_table = edgeport_1port_id_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 .num_ports = 1,
2734 .open = edge_open,
2735 .close = edge_close,
2736 .throttle = edge_throttle,
2737 .unthrottle = edge_unthrottle,
2738 .attach = edge_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04002739 .disconnect = edge_disconnect,
2740 .release = edge_release,
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002741 .port_probe = edge_create_sysfs_attrs,
Eric W. Biederman6d443d82012-01-13 21:32:06 -08002742 .port_remove = edge_remove_sysfs_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 .ioctl = edge_ioctl,
2744 .set_termios = edge_set_termios,
2745 .tiocmget = edge_tiocmget,
2746 .tiocmset = edge_tiocmset,
Alan Cox0bca1b92010-09-16 18:21:40 +01002747 .get_icount = edge_get_icount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 .write = edge_write,
2749 .write_room = edge_write_room,
2750 .chars_in_buffer = edge_chars_in_buffer,
2751 .break_ctl = edge_break,
2752 .read_int_callback = edge_interrupt_callback,
2753 .read_bulk_callback = edge_bulk_in_callback,
2754 .write_bulk_callback = edge_bulk_out_callback,
2755};
2756
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -07002757static struct usb_serial_driver edgeport_2port_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07002758 .driver = {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07002759 .owner = THIS_MODULE,
2760 .name = "edgeport_ti_2",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07002761 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07002762 .description = "Edgeport TI 2 port adapter",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 .id_table = edgeport_2port_id_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 .num_ports = 2,
2765 .open = edge_open,
2766 .close = edge_close,
2767 .throttle = edge_throttle,
2768 .unthrottle = edge_unthrottle,
2769 .attach = edge_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04002770 .disconnect = edge_disconnect,
2771 .release = edge_release,
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002772 .port_probe = edge_create_sysfs_attrs,
Eric W. Biederman6d443d82012-01-13 21:32:06 -08002773 .port_remove = edge_remove_sysfs_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 .ioctl = edge_ioctl,
2775 .set_termios = edge_set_termios,
2776 .tiocmget = edge_tiocmget,
2777 .tiocmset = edge_tiocmset,
2778 .write = edge_write,
2779 .write_room = edge_write_room,
2780 .chars_in_buffer = edge_chars_in_buffer,
2781 .break_ctl = edge_break,
2782 .read_int_callback = edge_interrupt_callback,
2783 .read_bulk_callback = edge_bulk_in_callback,
2784 .write_bulk_callback = edge_bulk_out_callback,
2785};
2786
Alan Stern7dbe2462012-02-23 14:56:57 -05002787static struct usb_serial_driver * const serial_drivers[] = {
2788 &edgeport_1port_device, &edgeport_2port_device, NULL
2789};
2790
Greg Kroah-Hartman8485f8c2012-02-28 13:11:53 -08002791module_usb_serial_driver(io_driver, serial_drivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793MODULE_AUTHOR(DRIVER_AUTHOR);
2794MODULE_DESCRIPTION(DRIVER_DESC);
2795MODULE_LICENSE("GPL");
Jaswinder Singhd12b2192008-07-04 23:06:09 +05302796MODULE_FIRMWARE("edgeport/down3.bin");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
2798module_param(debug, bool, S_IRUGO | S_IWUSR);
2799MODULE_PARM_DESC(debug, "Debug enabled or not");
2800
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801module_param(closing_wait, int, S_IRUGO | S_IWUSR);
2802MODULE_PARM_DESC(closing_wait, "Maximum wait for data to drain, in .01 secs");
2803
2804module_param(ignore_cpu_rev, bool, S_IRUGO | S_IWUSR);
Alan Cox2742fd82008-04-29 14:45:15 +01002805MODULE_PARM_DESC(ignore_cpu_rev,
2806 "Ignore the cpu revision when connecting to a device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002808module_param(default_uart_mode, int, S_IRUGO | S_IWUSR);
2809MODULE_PARM_DESC(default_uart_mode, "Default uart_mode, 0=RS232, ...");