blob: 2e3d0acbc389a0d19bbabdb61869352c181a1c15 [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>.
18 *
19 * Version history:
20 *
Alan Cox2742fd82008-04-29 14:45:15 +010021 * July 11, 2002 Removed 4 port device structure since all TI UMP
22 * chips have only 2 ports
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 * David Iacovelli (davidi@ionetworks.com)
24 *
25 */
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/kernel.h>
28#include <linux/jiffies.h>
29#include <linux/errno.h>
30#include <linux/init.h>
31#include <linux/slab.h>
32#include <linux/tty.h>
33#include <linux/tty_driver.h>
34#include <linux/tty_flip.h>
35#include <linux/module.h>
36#include <linux/spinlock.h>
Matthias Kaehlcke241ca642007-12-04 16:15:40 +010037#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/serial.h>
39#include <linux/ioctl.h>
Jaswinder Singhd12b2192008-07-04 23:06:09 +053040#include <linux/firmware.h>
Alan Cox2742fd82008-04-29 14:45:15 +010041#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070043#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include "io_16654.h"
46#include "io_usbvend.h"
47#include "io_ti.h"
48
49/*
50 * Version Information
51 */
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -040052#define DRIVER_VERSION "v0.7mode043006"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
54#define DRIVER_DESC "Edgeport USB Serial Driver"
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#define EPROM_PAGE_SIZE 64
57
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059/* different hardware types */
60#define HARDWARE_TYPE_930 0
61#define HARDWARE_TYPE_TIUMP 1
62
Alan Cox2742fd82008-04-29 14:45:15 +010063/* IOCTL_PRIVATE_TI_GET_MODE Definitions */
64#define TI_MODE_CONFIGURING 0 /* Device has not entered start device */
65#define TI_MODE_BOOT 1 /* Staying in boot mode */
66#define TI_MODE_DOWNLOAD 2 /* Made it to download mode */
67#define TI_MODE_TRANSITIONING 3 /* Currently in boot mode but
68 transitioning to download mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70/* read urb state */
71#define EDGE_READ_URB_RUNNING 0
72#define EDGE_READ_URB_STOPPING 1
73#define EDGE_READ_URB_STOPPED 2
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#define EDGE_CLOSING_WAIT 4000 /* in .01 sec */
76
77#define EDGE_OUT_BUF_SIZE 1024
78
79
80/* Product information read from the Edgeport */
Alan Cox2742fd82008-04-29 14:45:15 +010081struct product_info {
82 int TiMode; /* Current TI Mode */
83 __u8 hardware_type; /* Type of hardware */
Linus Torvalds1da177e2005-04-16 15:20:36 -070084} __attribute__((packed));
85
86/* circular buffer */
87struct edge_buf {
88 unsigned int buf_size;
89 char *buf_buf;
90 char *buf_get;
91 char *buf_put;
92};
93
94struct edgeport_port {
95 __u16 uart_base;
96 __u16 dma_address;
97 __u8 shadow_msr;
98 __u8 shadow_mcr;
99 __u8 shadow_lsr;
100 __u8 lsr_mask;
Martin Olsson19af5cd2009-04-23 11:37:37 +0200101 __u32 ump_read_timeout; /* Number of milliseconds the UMP will
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 wait without data before completing
103 a read short */
104 int baud_rate;
105 int close_pending;
106 int lsr_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 struct async_icount icount;
108 wait_queue_head_t delta_msr_wait; /* for handling sleeping while
109 waiting for msr change to
110 happen */
111 struct edgeport_serial *edge_serial;
112 struct usb_serial_port *port;
Alan Cox2742fd82008-04-29 14:45:15 +0100113 __u8 bUartMode; /* Port type, 0: RS232, etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 spinlock_t ep_lock;
115 int ep_read_urb_state;
116 int ep_write_urb_in_use;
117 struct edge_buf *ep_out_buf;
118};
119
120struct edgeport_serial {
121 struct product_info product_info;
Alan Cox2742fd82008-04-29 14:45:15 +0100122 u8 TI_I2C_Type; /* Type of I2C in UMP */
123 u8 TiReadI2C; /* Set to TRUE if we have read the
124 I2c in Boot Mode */
Matthias Kaehlcke241ca642007-12-04 16:15:40 +0100125 struct mutex es_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 int num_ports_open;
127 struct usb_serial *serial;
128};
129
130
131/* Devices that this driver supports */
Németh Márton7d40d7e2010-01-10 15:34:24 +0100132static const struct usb_device_id edgeport_1port_id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
134 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
135 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
136 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
137 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
138 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
139 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
140 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
141 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
142 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
143 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
144 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
145 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
146 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
147 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
148 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
149 { }
150};
151
Németh Márton7d40d7e2010-01-10 15:34:24 +0100152static const struct usb_device_id edgeport_2port_id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
154 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
155 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
156 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
157 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
158 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
159 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
160 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
161 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
162 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
163 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
164 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -0400165 /* The 4, 8 and 16 port devices show up as multiple 2 port devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -0400167 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
168 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
169 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
170 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 { }
172};
173
174/* Devices that this driver supports */
Németh Márton7d40d7e2010-01-10 15:34:24 +0100175static const struct usb_device_id id_table_combined[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
177 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
178 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
179 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
180 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
181 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
182 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
183 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
184 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
185 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
186 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
187 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
188 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
189 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
190 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
191 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
192 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
193 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
194 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
195 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
196 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
197 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
198 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
199 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
200 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
201 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
202 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
203 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
204 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -0400205 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
206 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
207 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
208 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 { }
210};
211
Alan Cox2742fd82008-04-29 14:45:15 +0100212MODULE_DEVICE_TABLE(usb, id_table_combined);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214static struct usb_driver io_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 .name = "io_ti",
216 .probe = usb_serial_probe,
217 .disconnect = usb_serial_disconnect,
218 .id_table = id_table_combined,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800219 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220};
221
222
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530223static unsigned char OperationalMajorVersion;
224static unsigned char OperationalMinorVersion;
225static unsigned short OperationalBuildNumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227static int debug;
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229static int closing_wait = EDGE_CLOSING_WAIT;
Alan Cox2742fd82008-04-29 14:45:15 +0100230static int ignore_cpu_rev;
231static int default_uart_mode; /* RS232 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Alan Cox2742fd82008-04-29 14:45:15 +0100233static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
234 unsigned char *data, int length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236static void stop_read(struct edgeport_port *edge_port);
237static int restart_read(struct edgeport_port *edge_port);
238
Alan Cox95da3102008-07-22 11:09:07 +0100239static void edge_set_termios(struct tty_struct *tty,
240 struct usb_serial_port *port, struct ktermios *old_termios);
241static void edge_send(struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -0400243/* sysfs attributes */
244static int edge_create_sysfs_attrs(struct usb_serial_port *port);
245static int edge_remove_sysfs_attrs(struct usb_serial_port *port);
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247/* circular buffer */
248static struct edge_buf *edge_buf_alloc(unsigned int size);
249static void edge_buf_free(struct edge_buf *eb);
250static void edge_buf_clear(struct edge_buf *eb);
251static unsigned int edge_buf_data_avail(struct edge_buf *eb);
252static unsigned int edge_buf_space_avail(struct edge_buf *eb);
253static unsigned int edge_buf_put(struct edge_buf *eb, const char *buf,
254 unsigned int count);
255static unsigned int edge_buf_get(struct edge_buf *eb, char *buf,
256 unsigned int count);
257
258
Alan Cox2742fd82008-04-29 14:45:15 +0100259static int ti_vread_sync(struct usb_device *dev, __u8 request,
260 __u16 value, __u16 index, u8 *data, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
262 int status;
263
Alan Cox2742fd82008-04-29 14:45:15 +0100264 status = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request,
265 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
266 value, index, data, size, 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if (status < 0)
268 return status;
269 if (status != size) {
Alan Cox2742fd82008-04-29 14:45:15 +0100270 dbg("%s - wanted to write %d, but only wrote %d",
271 __func__, size, status);
272 return -ECOMM;
273 }
274 return 0;
275}
276
277static int ti_vsend_sync(struct usb_device *dev, __u8 request,
278 __u16 value, __u16 index, u8 *data, int size)
279{
280 int status;
281
282 status = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
283 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
284 value, index, data, size, 1000);
285 if (status < 0)
286 return status;
287 if (status != size) {
288 dbg("%s - wanted to write %d, but only wrote %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800289 __func__, size, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 return -ECOMM;
291 }
292 return 0;
293}
294
Alan Cox2742fd82008-04-29 14:45:15 +0100295static int send_cmd(struct usb_device *dev, __u8 command,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 __u8 moduleid, __u16 value, u8 *data,
297 int size)
298{
Alan Cox2742fd82008-04-29 14:45:15 +0100299 return ti_vsend_sync(dev, command, value, moduleid, data, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
302/* clear tx/rx buffers and fifo in TI UMP */
Alan Cox2742fd82008-04-29 14:45:15 +0100303static int purge_port(struct usb_serial_port *port, __u16 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
305 int port_number = port->number - port->serial->minor;
306
Alan Cox2742fd82008-04-29 14:45:15 +0100307 dbg("%s - port %d, mask %x", __func__, port_number, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Alan Cox2742fd82008-04-29 14:45:15 +0100309 return send_cmd(port->serial->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 UMPC_PURGE_PORT,
311 (__u8)(UMPM_UART1_PORT + port_number),
312 mask,
313 NULL,
314 0);
315}
316
317/**
Alan Cox2742fd82008-04-29 14:45:15 +0100318 * read_download_mem - Read edgeport memory from TI chip
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 * @dev: usb device pointer
320 * @start_address: Device CPU address at which to read
321 * @length: Length of above data
322 * @address_type: Can read both XDATA and I2C
323 * @buffer: pointer to input data buffer
324 */
Alan Cox2742fd82008-04-29 14:45:15 +0100325static int read_download_mem(struct usb_device *dev, int start_address,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 int length, __u8 address_type, __u8 *buffer)
327{
328 int status = 0;
329 __u8 read_length;
330 __be16 be_start_address;
Alan Cox2742fd82008-04-29 14:45:15 +0100331
332 dbg("%s - @ %x for %d", __func__, start_address, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 /* Read in blocks of 64 bytes
335 * (TI firmware can't handle more than 64 byte reads)
336 */
337 while (length) {
338 if (length > 64)
Alan Cox2742fd82008-04-29 14:45:15 +0100339 read_length = 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 else
341 read_length = (__u8)length;
342
343 if (read_length > 1) {
Alan Cox2742fd82008-04-29 14:45:15 +0100344 dbg("%s - @ %x for %d", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 start_address, read_length);
346 }
Alan Cox2742fd82008-04-29 14:45:15 +0100347 be_start_address = cpu_to_be16(start_address);
348 status = ti_vread_sync(dev, UMPC_MEMORY_READ,
349 (__u16)address_type,
350 (__force __u16)be_start_address,
351 buffer, read_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +0100354 dbg("%s - ERROR %x", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return status;
356 }
357
Alan Cox2742fd82008-04-29 14:45:15 +0100358 if (read_length > 1)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800359 usb_serial_debug_data(debug, &dev->dev, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 read_length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 /* Update pointers/length */
363 start_address += read_length;
364 buffer += read_length;
365 length -= read_length;
366 }
Alan Cox2742fd82008-04-29 14:45:15 +0100367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return status;
369}
370
Alan Cox2742fd82008-04-29 14:45:15 +0100371static int read_ram(struct usb_device *dev, int start_address,
372 int length, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
Alan Cox2742fd82008-04-29 14:45:15 +0100374 return read_download_mem(dev, start_address, length,
375 DTK_ADDR_SPACE_XDATA, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377
378/* Read edgeport memory to a given block */
Alan Cox2742fd82008-04-29 14:45:15 +0100379static int read_boot_mem(struct edgeport_serial *serial,
380 int start_address, int length, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
382 int status = 0;
383 int i;
384
Alan Cox2742fd82008-04-29 14:45:15 +0100385 for (i = 0; i < length; i++) {
386 status = ti_vread_sync(serial->serial->dev,
387 UMPC_MEMORY_READ, serial->TI_I2C_Type,
388 (__u16)(start_address+i), &buffer[i], 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +0100390 dbg("%s - ERROR %x", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return status;
392 }
393 }
394
Alan Cox2742fd82008-04-29 14:45:15 +0100395 dbg("%s - start_address = %x, length = %d",
396 __func__, start_address, length);
397 usb_serial_debug_data(debug, &serial->serial->dev->dev,
398 __func__, length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 serial->TiReadI2C = 1;
401
402 return status;
403}
404
405/* Write given block to TI EPROM memory */
Alan Cox2742fd82008-04-29 14:45:15 +0100406static int write_boot_mem(struct edgeport_serial *serial,
407 int start_address, int length, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
409 int status = 0;
410 int i;
Johan Hovolde9305d22009-12-28 23:01:50 +0100411 u8 *temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 /* Must do a read before write */
414 if (!serial->TiReadI2C) {
Johan Hovolde9305d22009-12-28 23:01:50 +0100415 temp = kmalloc(1, GFP_KERNEL);
416 if (!temp) {
417 dev_err(&serial->serial->dev->dev,
418 "%s - out of memory\n", __func__);
419 return -ENOMEM;
420 }
421 status = read_boot_mem(serial, 0, 1, temp);
422 kfree(temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (status)
424 return status;
425 }
426
Alan Cox2742fd82008-04-29 14:45:15 +0100427 for (i = 0; i < length; ++i) {
428 status = ti_vsend_sync(serial->serial->dev,
429 UMPC_MEMORY_WRITE, buffer[i],
430 (__u16)(i + start_address), NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (status)
432 return status;
433 }
434
Alan Cox2742fd82008-04-29 14:45:15 +0100435 dbg("%s - start_sddr = %x, length = %d",
436 __func__, start_address, length);
437 usb_serial_debug_data(debug, &serial->serial->dev->dev,
438 __func__, length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 return status;
441}
442
443
444/* Write edgeport I2C memory to TI chip */
Alan Cox2742fd82008-04-29 14:45:15 +0100445static int write_i2c_mem(struct edgeport_serial *serial,
446 int start_address, int length, __u8 address_type, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
448 int status = 0;
449 int write_length;
450 __be16 be_start_address;
451
452 /* We can only send a maximum of 1 aligned byte page at a time */
Alan Cox2742fd82008-04-29 14:45:15 +0100453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 /* calulate the number of bytes left in the first page */
Alan Cox2742fd82008-04-29 14:45:15 +0100455 write_length = EPROM_PAGE_SIZE -
456 (start_address & (EPROM_PAGE_SIZE - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 if (write_length > length)
459 write_length = length;
460
Alan Cox2742fd82008-04-29 14:45:15 +0100461 dbg("%s - BytesInFirstPage 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 first 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,
469 UMPC_MEMORY_WRITE, (__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 dbg("%s - ERROR %d", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return status;
475 }
476
477 length -= write_length;
478 start_address += write_length;
479 buffer += write_length;
480
Alan Cox2742fd82008-04-29 14:45:15 +0100481 /* We should be aligned now -- can write
482 max page size bytes at a time */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 while (length) {
484 if (length > EPROM_PAGE_SIZE)
485 write_length = EPROM_PAGE_SIZE;
486 else
487 write_length = length;
488
Alan Cox2742fd82008-04-29 14:45:15 +0100489 dbg("%s - Page Write Addr = %x, length = %d",
490 __func__, start_address, write_length);
491 usb_serial_debug_data(debug, &serial->serial->dev->dev,
492 __func__, write_length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 /* Write next page */
Alan Cox2742fd82008-04-29 14:45:15 +0100495 be_start_address = cpu_to_be16(start_address);
496 status = ti_vsend_sync(serial->serial->dev, UMPC_MEMORY_WRITE,
497 (__u16)address_type,
498 (__force __u16)be_start_address,
499 buffer, write_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +0100501 dev_err(&serial->serial->dev->dev, "%s - ERROR %d\n",
502 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return status;
504 }
Alan Cox2742fd82008-04-29 14:45:15 +0100505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 length -= write_length;
507 start_address += write_length;
508 buffer += write_length;
509 }
510 return status;
511}
512
513/* Examine the UMP DMA registers and LSR
Alan Cox2742fd82008-04-29 14:45:15 +0100514 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 * Check the MSBit of the X and Y DMA byte count registers.
516 * A zero in this bit indicates that the TX DMA buffers are empty
517 * then check the TX Empty bit in the UART.
518 */
Alan Cox2742fd82008-04-29 14:45:15 +0100519static int tx_active(struct edgeport_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
521 int status;
522 struct out_endpoint_desc_block *oedb;
523 __u8 *lsr;
524 int bytes_left = 0;
525
Alan Cox2742fd82008-04-29 14:45:15 +0100526 oedb = kmalloc(sizeof(*oedb), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (!oedb) {
Alan Cox2742fd82008-04-29 14:45:15 +0100528 dev_err(&port->port->dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return -ENOMEM;
530 }
531
Alan Cox2742fd82008-04-29 14:45:15 +0100532 lsr = kmalloc(1, GFP_KERNEL); /* Sigh, that's right, just one byte,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 as not all platforms can do DMA
534 from stack */
535 if (!lsr) {
536 kfree(oedb);
537 return -ENOMEM;
538 }
539 /* Read the DMA Count Registers */
Alan Cox2742fd82008-04-29 14:45:15 +0100540 status = read_ram(port->port->serial->dev, port->dma_address,
541 sizeof(*oedb), (void *)oedb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if (status)
543 goto exit_is_tx_active;
544
Alan Cox2742fd82008-04-29 14:45:15 +0100545 dbg("%s - XByteCount 0x%X", __func__, oedb->XByteCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 /* and the LSR */
Alan Cox2742fd82008-04-29 14:45:15 +0100548 status = read_ram(port->port->serial->dev,
549 port->uart_base + UMPMEM_OFFS_UART_LSR, 1, lsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 if (status)
552 goto exit_is_tx_active;
Alan Cox2742fd82008-04-29 14:45:15 +0100553 dbg("%s - LSR = 0x%X", __func__, *lsr);
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 /* If either buffer has data or we are transmitting then return TRUE */
Alan Cox2742fd82008-04-29 14:45:15 +0100556 if ((oedb->XByteCount & 0x80) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 bytes_left += 64;
558
Alan Cox2742fd82008-04-29 14:45:15 +0100559 if ((*lsr & UMP_UART_LSR_TX_MASK) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 bytes_left += 1;
561
562 /* We return Not Active if we get any kind of error */
563exit_is_tx_active:
Alan Cox2742fd82008-04-29 14:45:15 +0100564 dbg("%s - return %d", __func__, bytes_left);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 kfree(lsr);
567 kfree(oedb);
568 return bytes_left;
569}
570
Alan Cox2742fd82008-04-29 14:45:15 +0100571static void chase_port(struct edgeport_port *port, unsigned long timeout,
572 int flush)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
574 int baud_rate;
Alan Cox4a90f092008-10-13 10:39:46 +0100575 struct tty_struct *tty = tty_port_tty_get(&port->port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 wait_queue_t wait;
577 unsigned long flags;
578
579 if (!timeout)
Alan Cox2742fd82008-04-29 14:45:15 +0100580 timeout = (HZ * EDGE_CLOSING_WAIT)/100;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 /* wait for data to drain from the buffer */
583 spin_lock_irqsave(&port->ep_lock, flags);
584 init_waitqueue_entry(&wait, current);
585 add_wait_queue(&tty->write_wait, &wait);
586 for (;;) {
587 set_current_state(TASK_INTERRUPTIBLE);
588 if (edge_buf_data_avail(port->ep_out_buf) == 0
589 || timeout == 0 || signal_pending(current)
Alan Cox2742fd82008-04-29 14:45:15 +0100590 || !usb_get_intfdata(port->port->serial->interface))
591 /* disconnect */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 break;
593 spin_unlock_irqrestore(&port->ep_lock, flags);
594 timeout = schedule_timeout(timeout);
595 spin_lock_irqsave(&port->ep_lock, flags);
596 }
597 set_current_state(TASK_RUNNING);
598 remove_wait_queue(&tty->write_wait, &wait);
599 if (flush)
600 edge_buf_clear(port->ep_out_buf);
601 spin_unlock_irqrestore(&port->ep_lock, flags);
Alan Cox4a90f092008-10-13 10:39:46 +0100602 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 /* wait for data to drain from the device */
605 timeout += jiffies;
606 while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
Alan Cox2742fd82008-04-29 14:45:15 +0100607 && usb_get_intfdata(port->port->serial->interface)) {
608 /* not disconnected */
609 if (!tx_active(port))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 break;
611 msleep(10);
612 }
613
614 /* disconnected */
615 if (!usb_get_intfdata(port->port->serial->interface))
616 return;
617
618 /* wait one more character time, based on baud rate */
Alan Cox2742fd82008-04-29 14:45:15 +0100619 /* (tx_active doesn't seem to wait for the last byte) */
620 baud_rate = port->baud_rate;
621 if (baud_rate == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 baud_rate = 50;
Julia Lawalldfa5ec72008-03-04 15:25:11 -0800623 msleep(max(1, DIV_ROUND_UP(10000, baud_rate)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
625
Alan Cox2742fd82008-04-29 14:45:15 +0100626static int choose_config(struct usb_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
Alan Cox2742fd82008-04-29 14:45:15 +0100628 /*
629 * There may be multiple configurations on this device, in which case
630 * we would need to read and parse all of them to find out which one
631 * we want. However, we just support one config at this point,
632 * configuration # 1, which is Config Descriptor 0.
633 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Alan Cox2742fd82008-04-29 14:45:15 +0100635 dbg("%s - Number of Interfaces = %d",
636 __func__, dev->config->desc.bNumInterfaces);
637 dbg("%s - MAX Power = %d",
638 __func__, dev->config->desc.bMaxPower * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 if (dev->config->desc.bNumInterfaces != 1) {
Alan Cox2742fd82008-04-29 14:45:15 +0100641 dev_err(&dev->dev, "%s - bNumInterfaces is not 1, ERROR!\n",
642 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return -ENODEV;
644 }
645
646 return 0;
647}
648
Alan Cox2742fd82008-04-29 14:45:15 +0100649static int read_rom(struct edgeport_serial *serial,
650 int start_address, int length, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
652 int status;
653
654 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
Alan Cox2742fd82008-04-29 14:45:15 +0100655 status = read_download_mem(serial->serial->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 start_address,
657 length,
658 serial->TI_I2C_Type,
659 buffer);
660 } else {
Alan Cox2742fd82008-04-29 14:45:15 +0100661 status = read_boot_mem(serial, start_address, length,
662 buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return status;
665}
666
Alan Cox2742fd82008-04-29 14:45:15 +0100667static int write_rom(struct edgeport_serial *serial, int start_address,
668 int length, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
670 if (serial->product_info.TiMode == TI_MODE_BOOT)
Alan Cox2742fd82008-04-29 14:45:15 +0100671 return write_boot_mem(serial, start_address, length,
672 buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD)
Alan Cox2742fd82008-04-29 14:45:15 +0100675 return write_i2c_mem(serial, start_address, length,
676 serial->TI_I2C_Type, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return -EINVAL;
678}
679
680
681
682/* Read a descriptor header from I2C based on type */
Alan Cox2742fd82008-04-29 14:45:15 +0100683static int get_descriptor_addr(struct edgeport_serial *serial,
684 int desc_type, struct ti_i2c_desc *rom_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
686 int start_address;
687 int status;
688
689 /* Search for requested descriptor in I2C */
690 start_address = 2;
691 do {
Alan Cox2742fd82008-04-29 14:45:15 +0100692 status = read_rom(serial,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 start_address,
694 sizeof(struct ti_i2c_desc),
Alan Cox2742fd82008-04-29 14:45:15 +0100695 (__u8 *)rom_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (status)
697 return 0;
698
699 if (rom_desc->Type == desc_type)
700 return start_address;
701
Alan Cox2742fd82008-04-29 14:45:15 +0100702 start_address = start_address + sizeof(struct ti_i2c_desc)
703 + rom_desc->Size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 } while ((start_address < TI_MAX_I2C_SIZE) && rom_desc->Type);
Alan Cox2742fd82008-04-29 14:45:15 +0100706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return 0;
708}
709
710/* Validate descriptor checksum */
Alan Cox2742fd82008-04-29 14:45:15 +0100711static int valid_csum(struct ti_i2c_desc *rom_desc, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
713 __u16 i;
714 __u8 cs = 0;
715
Alan Cox2742fd82008-04-29 14:45:15 +0100716 for (i = 0; i < rom_desc->Size; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 cs = (__u8)(cs + buffer[i]);
Alan Cox2742fd82008-04-29 14:45:15 +0100718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 if (cs != rom_desc->CheckSum) {
Alan Cox2742fd82008-04-29 14:45:15 +0100720 dbg("%s - Mismatch %x - %x", __func__, rom_desc->CheckSum, cs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return -EINVAL;
722 }
723 return 0;
724}
725
726/* Make sure that the I2C image is good */
Alan Cox2742fd82008-04-29 14:45:15 +0100727static int check_i2c_image(struct edgeport_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
729 struct device *dev = &serial->serial->dev->dev;
730 int status = 0;
731 struct ti_i2c_desc *rom_desc;
732 int start_address = 2;
733 __u8 *buffer;
734 __u16 ttype;
735
Alan Cox2742fd82008-04-29 14:45:15 +0100736 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 if (!rom_desc) {
Alan Cox2742fd82008-04-29 14:45:15 +0100738 dev_err(dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 return -ENOMEM;
740 }
Alan Cox2742fd82008-04-29 14:45:15 +0100741 buffer = kmalloc(TI_MAX_I2C_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 if (!buffer) {
Alan Cox2742fd82008-04-29 14:45:15 +0100743 dev_err(dev, "%s - out of memory when allocating buffer\n",
744 __func__);
745 kfree(rom_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 return -ENOMEM;
747 }
748
Alan Cox2742fd82008-04-29 14:45:15 +0100749 /* Read the first byte (Signature0) must be 0x52 or 0x10 */
750 status = read_rom(serial, 0, 1, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if (status)
Alan Cox2742fd82008-04-29 14:45:15 +0100752 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 if (*buffer != UMP5152 && *buffer != UMP3410) {
Alan Cox2742fd82008-04-29 14:45:15 +0100755 dev_err(dev, "%s - invalid buffer signature\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 status = -ENODEV;
Alan Cox2742fd82008-04-29 14:45:15 +0100757 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
759
760 do {
Alan Cox2742fd82008-04-29 14:45:15 +0100761 /* Validate the I2C */
762 status = read_rom(serial,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 start_address,
764 sizeof(struct ti_i2c_desc),
765 (__u8 *)rom_desc);
766 if (status)
767 break;
768
Alan Cox2742fd82008-04-29 14:45:15 +0100769 if ((start_address + sizeof(struct ti_i2c_desc) +
770 rom_desc->Size) > TI_MAX_I2C_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 status = -ENODEV;
Alan Cox2742fd82008-04-29 14:45:15 +0100772 dbg("%s - structure too big, erroring out.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 break;
774 }
775
Alan Cox2742fd82008-04-29 14:45:15 +0100776 dbg("%s Type = 0x%x", __func__, rom_desc->Type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Alan Cox2742fd82008-04-29 14:45:15 +0100778 /* Skip type 2 record */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 ttype = rom_desc->Type & 0x0f;
Alan Cox2742fd82008-04-29 14:45:15 +0100780 if (ttype != I2C_DESC_TYPE_FIRMWARE_BASIC
781 && ttype != I2C_DESC_TYPE_FIRMWARE_AUTO) {
782 /* Read the descriptor data */
783 status = read_rom(serial, start_address +
784 sizeof(struct ti_i2c_desc),
785 rom_desc->Size, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 if (status)
787 break;
788
Alan Cox2742fd82008-04-29 14:45:15 +0100789 status = valid_csum(rom_desc, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 if (status)
791 break;
792 }
Alan Cox2742fd82008-04-29 14:45:15 +0100793 start_address = start_address + sizeof(struct ti_i2c_desc) +
794 rom_desc->Size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Alan Cox2742fd82008-04-29 14:45:15 +0100796 } while ((rom_desc->Type != I2C_DESC_TYPE_ION) &&
797 (start_address < TI_MAX_I2C_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Alan Cox2742fd82008-04-29 14:45:15 +0100799 if ((rom_desc->Type != I2C_DESC_TYPE_ION) ||
800 (start_address > TI_MAX_I2C_SIZE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 status = -ENODEV;
802
Alan Cox2742fd82008-04-29 14:45:15 +0100803out:
804 kfree(buffer);
805 kfree(rom_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return status;
807}
808
Alan Cox2742fd82008-04-29 14:45:15 +0100809static int get_manuf_info(struct edgeport_serial *serial, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
811 int status;
812 int start_address;
813 struct ti_i2c_desc *rom_desc;
814 struct edge_ti_manuf_descriptor *desc;
815
Alan Cox2742fd82008-04-29 14:45:15 +0100816 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (!rom_desc) {
Alan Cox2742fd82008-04-29 14:45:15 +0100818 dev_err(&serial->serial->dev->dev, "%s - out of memory\n",
819 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 return -ENOMEM;
821 }
Alan Cox2742fd82008-04-29 14:45:15 +0100822 start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_ION,
823 rom_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 if (!start_address) {
Alan Cox2742fd82008-04-29 14:45:15 +0100826 dbg("%s - Edge Descriptor not found in I2C", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 status = -ENODEV;
828 goto exit;
829 }
830
Alan Cox2742fd82008-04-29 14:45:15 +0100831 /* Read the descriptor data */
832 status = read_rom(serial, start_address+sizeof(struct ti_i2c_desc),
833 rom_desc->Size, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 if (status)
835 goto exit;
Alan Cox2742fd82008-04-29 14:45:15 +0100836
837 status = valid_csum(rom_desc, buffer);
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 desc = (struct edge_ti_manuf_descriptor *)buffer;
Alan Cox2742fd82008-04-29 14:45:15 +0100840 dbg("%s - IonConfig 0x%x", __func__, desc->IonConfig);
841 dbg("%s - Version %d", __func__, desc->Version);
842 dbg("%s - Cpu/Board 0x%x", __func__, desc->CpuRev_BoardRev);
843 dbg("%s - NumPorts %d", __func__, desc->NumPorts);
844 dbg("%s - NumVirtualPorts %d", __func__, desc->NumVirtualPorts);
845 dbg("%s - TotalPorts %d", __func__, desc->TotalPorts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847exit:
Alan Cox2742fd82008-04-29 14:45:15 +0100848 kfree(rom_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 return status;
850}
851
852/* Build firmware header used for firmware update */
Alan Cox2742fd82008-04-29 14:45:15 +0100853static int build_i2c_fw_hdr(__u8 *header, struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
855 __u8 *buffer;
856 int buffer_size;
857 int i;
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530858 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 __u8 cs = 0;
860 struct ti_i2c_desc *i2c_header;
861 struct ti_i2c_image_header *img_header;
862 struct ti_i2c_firmware_rec *firmware_rec;
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530863 const struct firmware *fw;
864 const char *fw_name = "edgeport/down3.bin";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Alan Cox2742fd82008-04-29 14:45:15 +0100866 /* In order to update the I2C firmware we must change the type 2 record
867 * to type 0xF2. This will force the UMP to come up in Boot Mode.
868 * Then while in boot mode, the driver will download the latest
869 * firmware (padded to 15.5k) into the UMP ram. And finally when the
870 * device comes back up in download mode the driver will cause the new
871 * firmware to be copied from the UMP Ram to I2C and the firmware will
872 * update the record type from 0xf2 to 0x02.
873 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Alan Cox2742fd82008-04-29 14:45:15 +0100875 /* Allocate a 15.5k buffer + 2 bytes for version number
876 * (Firmware Record) */
877 buffer_size = (((1024 * 16) - 512 ) +
878 sizeof(struct ti_i2c_firmware_rec));
879
880 buffer = kmalloc(buffer_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 if (!buffer) {
Alan Cox2742fd82008-04-29 14:45:15 +0100882 dev_err(dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return -ENOMEM;
884 }
Alan Cox2742fd82008-04-29 14:45:15 +0100885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 // Set entire image of 0xffs
Alan Cox2742fd82008-04-29 14:45:15 +0100887 memset(buffer, 0xff, buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530889 err = request_firmware(&fw, fw_name, dev);
890 if (err) {
891 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
892 fw_name, err);
893 kfree(buffer);
894 return err;
895 }
896
897 /* Save Download Version Number */
898 OperationalMajorVersion = fw->data[0];
899 OperationalMinorVersion = fw->data[1];
900 OperationalBuildNumber = fw->data[2] | (fw->data[3] << 8);
901
Alan Cox2742fd82008-04-29 14:45:15 +0100902 /* Copy version number into firmware record */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 firmware_rec = (struct ti_i2c_firmware_rec *)buffer;
904
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530905 firmware_rec->Ver_Major = OperationalMajorVersion;
906 firmware_rec->Ver_Minor = OperationalMinorVersion;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Alan Cox2742fd82008-04-29 14:45:15 +0100908 /* Pointer to fw_down memory image */
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530909 img_header = (struct ti_i2c_image_header *)&fw->data[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Alan Cox2742fd82008-04-29 14:45:15 +0100911 memcpy(buffer + sizeof(struct ti_i2c_firmware_rec),
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530912 &fw->data[4 + sizeof(struct ti_i2c_image_header)],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 le16_to_cpu(img_header->Length));
914
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530915 release_firmware(fw);
916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 for (i=0; i < buffer_size; i++) {
918 cs = (__u8)(cs + buffer[i]);
919 }
920
Alan Cox2742fd82008-04-29 14:45:15 +0100921 kfree(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Alan Cox2742fd82008-04-29 14:45:15 +0100923 /* Build new header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 i2c_header = (struct ti_i2c_desc *)header;
925 firmware_rec = (struct ti_i2c_firmware_rec*)i2c_header->Data;
Alan Cox2742fd82008-04-29 14:45:15 +0100926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 i2c_header->Type = I2C_DESC_TYPE_FIRMWARE_BLANK;
928 i2c_header->Size = (__u16)buffer_size;
929 i2c_header->CheckSum = cs;
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530930 firmware_rec->Ver_Major = OperationalMajorVersion;
931 firmware_rec->Ver_Minor = OperationalMinorVersion;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 return 0;
934}
935
936/* Try to figure out what type of I2c we have */
Alan Cox2742fd82008-04-29 14:45:15 +0100937static int i2c_type_bootmode(struct edgeport_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
939 int status;
Johan Hovolde9305d22009-12-28 23:01:50 +0100940 u8 *data;
941
942 data = kmalloc(1, GFP_KERNEL);
943 if (!data) {
944 dev_err(&serial->serial->dev->dev,
945 "%s - out of memory\n", __func__);
946 return -ENOMEM;
947 }
Alan Cox2742fd82008-04-29 14:45:15 +0100948
949 /* Try to read type 2 */
950 status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
Johan Hovolde9305d22009-12-28 23:01:50 +0100951 DTK_ADDR_SPACE_I2C_TYPE_II, 0, data, 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 if (status)
Alan Cox2742fd82008-04-29 14:45:15 +0100953 dbg("%s - read 2 status error = %d", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 else
Johan Hovolde9305d22009-12-28 23:01:50 +0100955 dbg("%s - read 2 data = 0x%x", __func__, *data);
956 if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
Alan Cox2742fd82008-04-29 14:45:15 +0100957 dbg("%s - ROM_TYPE_II", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
Johan Hovolde9305d22009-12-28 23:01:50 +0100959 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 }
961
Alan Cox2742fd82008-04-29 14:45:15 +0100962 /* Try to read type 3 */
963 status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
Johan Hovolde9305d22009-12-28 23:01:50 +0100964 DTK_ADDR_SPACE_I2C_TYPE_III, 0, data, 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 if (status)
Alan Cox2742fd82008-04-29 14:45:15 +0100966 dbg("%s - read 3 status error = %d", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 else
Johan Hovolde9305d22009-12-28 23:01:50 +0100968 dbg("%s - read 2 data = 0x%x", __func__, *data);
969 if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
Alan Cox2742fd82008-04-29 14:45:15 +0100970 dbg("%s - ROM_TYPE_III", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_III;
Johan Hovolde9305d22009-12-28 23:01:50 +0100972 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 }
974
Alan Cox2742fd82008-04-29 14:45:15 +0100975 dbg("%s - Unknown", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
Johan Hovolde9305d22009-12-28 23:01:50 +0100977 status = -ENODEV;
978out:
979 kfree(data);
980 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
982
Alan Cox2742fd82008-04-29 14:45:15 +0100983static int bulk_xfer(struct usb_serial *serial, void *buffer,
984 int length, int *num_sent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
986 int status;
987
Alan Cox2742fd82008-04-29 14:45:15 +0100988 status = usb_bulk_msg(serial->dev,
989 usb_sndbulkpipe(serial->dev,
990 serial->port[0]->bulk_out_endpointAddress),
991 buffer, length, num_sent, 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 return status;
993}
994
995/* Download given firmware image to the device (IN BOOT MODE) */
Alan Cox2742fd82008-04-29 14:45:15 +0100996static int download_code(struct edgeport_serial *serial, __u8 *image,
997 int image_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
999 int status = 0;
1000 int pos;
1001 int transfer;
1002 int done;
1003
Alan Cox2742fd82008-04-29 14:45:15 +01001004 /* Transfer firmware image */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 for (pos = 0; pos < image_length; ) {
Alan Cox2742fd82008-04-29 14:45:15 +01001006 /* Read the next buffer from file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 transfer = image_length - pos;
1008 if (transfer > EDGE_FW_BULK_MAX_PACKET_SIZE)
1009 transfer = EDGE_FW_BULK_MAX_PACKET_SIZE;
1010
Alan Cox2742fd82008-04-29 14:45:15 +01001011 /* Transfer data */
1012 status = bulk_xfer(serial->serial, &image[pos],
1013 transfer, &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 if (status)
1015 break;
Alan Cox2742fd82008-04-29 14:45:15 +01001016 /* Advance buffer pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 pos += done;
1018 }
1019
1020 return status;
1021}
1022
Alan Cox2742fd82008-04-29 14:45:15 +01001023/* FIXME!!! */
1024static int config_boot_dev(struct usb_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
1026 return 0;
1027}
1028
Alan Cox2742fd82008-04-29 14:45:15 +01001029static int ti_cpu_rev(struct edge_ti_manuf_descriptor *desc)
1030{
1031 return TI_GET_CPU_REVISION(desc->CpuRev_BoardRev);
1032}
1033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034/**
1035 * DownloadTIFirmware - Download run-time operating firmware to the TI5052
Alan Cox2742fd82008-04-29 14:45:15 +01001036 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 * This routine downloads the main operating code into the TI5052, using the
1038 * boot code already burned into E2PROM or ROM.
1039 */
Alan Cox2742fd82008-04-29 14:45:15 +01001040static int download_fw(struct edgeport_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
1042 struct device *dev = &serial->serial->dev->dev;
1043 int status = 0;
1044 int start_address;
1045 struct edge_ti_manuf_descriptor *ti_manuf_desc;
1046 struct usb_interface_descriptor *interface;
1047 int download_cur_ver;
1048 int download_new_ver;
1049
1050 /* This routine is entered by both the BOOT mode and the Download mode
1051 * We can determine which code is running by the reading the config
1052 * descriptor and if we have only one bulk pipe it is in boot mode
1053 */
1054 serial->product_info.hardware_type = HARDWARE_TYPE_TIUMP;
1055
1056 /* Default to type 2 i2c */
1057 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
1058
Alan Cox2742fd82008-04-29 14:45:15 +01001059 status = choose_config(serial->serial->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 if (status)
1061 return status;
1062
1063 interface = &serial->serial->interface->cur_altsetting->desc;
1064 if (!interface) {
Alan Cox2742fd82008-04-29 14:45:15 +01001065 dev_err(dev, "%s - no interface set, error!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 return -ENODEV;
1067 }
1068
Alan Cox2742fd82008-04-29 14:45:15 +01001069 /*
1070 * Setup initial mode -- the default mode 0 is TI_MODE_CONFIGURING
1071 * if we have more than one endpoint we are definitely in download
1072 * mode
1073 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 if (interface->bNumEndpoints > 1)
1075 serial->product_info.TiMode = TI_MODE_DOWNLOAD;
1076 else
Alan Cox2742fd82008-04-29 14:45:15 +01001077 /* Otherwise we will remain in configuring mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 serial->product_info.TiMode = TI_MODE_CONFIGURING;
1079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 /********************************************************************/
1081 /* Download Mode */
1082 /********************************************************************/
1083 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
1084 struct ti_i2c_desc *rom_desc;
1085
Andrew Morton9544e832008-02-04 23:57:50 -08001086 dbg("%s - RUNNING IN DOWNLOAD MODE", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Alan Cox2742fd82008-04-29 14:45:15 +01001088 status = check_i2c_image(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 if (status) {
Andrew Morton9544e832008-02-04 23:57:50 -08001090 dbg("%s - DOWNLOAD MODE -- BAD I2C", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 return status;
1092 }
Alan Cox2742fd82008-04-29 14:45:15 +01001093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 /* Validate Hardware version number
1095 * Read Manufacturing Descriptor from TI Based Edgeport
1096 */
Alan Cox2742fd82008-04-29 14:45:15 +01001097 ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 if (!ti_manuf_desc) {
Alan Cox2742fd82008-04-29 14:45:15 +01001099 dev_err(dev, "%s - out of memory.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 return -ENOMEM;
1101 }
Alan Cox2742fd82008-04-29 14:45:15 +01001102 status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001104 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 return status;
1106 }
1107
Alan Cox2742fd82008-04-29 14:45:15 +01001108 /* Check version number of ION descriptor */
1109 if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
1110 dbg("%s - Wrong CPU Rev %d (Must be 2)",
1111 __func__, ti_cpu_rev(ti_manuf_desc));
1112 kfree(ti_manuf_desc);
1113 return -EINVAL;
1114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Alan Cox2742fd82008-04-29 14:45:15 +01001116 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 if (!rom_desc) {
Alan Cox2742fd82008-04-29 14:45:15 +01001118 dev_err(dev, "%s - out of memory.\n", __func__);
1119 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 return -ENOMEM;
1121 }
1122
Alan Cox2742fd82008-04-29 14:45:15 +01001123 /* Search for type 2 record (firmware record) */
1124 start_address = get_descriptor_addr(serial,
1125 I2C_DESC_TYPE_FIRMWARE_BASIC, rom_desc);
1126 if (start_address != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 struct ti_i2c_firmware_rec *firmware_version;
Johan Hovolde9305d22009-12-28 23:01:50 +01001128 u8 *record;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Alan Cox2742fd82008-04-29 14:45:15 +01001130 dbg("%s - Found Type FIRMWARE (Type 2) record",
1131 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Alan Cox2742fd82008-04-29 14:45:15 +01001133 firmware_version = kmalloc(sizeof(*firmware_version),
1134 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 if (!firmware_version) {
Alan Cox2742fd82008-04-29 14:45:15 +01001136 dev_err(dev, "%s - out of memory.\n", __func__);
1137 kfree(rom_desc);
1138 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 return -ENOMEM;
1140 }
1141
Alan Cox2742fd82008-04-29 14:45:15 +01001142 /* Validate version number
1143 * Read the descriptor data
1144 */
1145 status = read_rom(serial, start_address +
1146 sizeof(struct ti_i2c_desc),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 sizeof(struct ti_i2c_firmware_rec),
1148 (__u8 *)firmware_version);
1149 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001150 kfree(firmware_version);
1151 kfree(rom_desc);
1152 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 return status;
1154 }
1155
Alan Cox2742fd82008-04-29 14:45:15 +01001156 /* Check version number of download with current
1157 version in I2c */
1158 download_cur_ver = (firmware_version->Ver_Major << 8) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 (firmware_version->Ver_Minor);
Jaswinder Singhd12b2192008-07-04 23:06:09 +05301160 download_new_ver = (OperationalMajorVersion << 8) +
1161 (OperationalMinorVersion);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Alan Cox2742fd82008-04-29 14:45:15 +01001163 dbg("%s - >> FW Versions Device %d.%d Driver %d.%d",
1164 __func__,
1165 firmware_version->Ver_Major,
1166 firmware_version->Ver_Minor,
1167 OperationalMajorVersion,
1168 OperationalMinorVersion);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Alan Cox2742fd82008-04-29 14:45:15 +01001170 /* Check if we have an old version in the I2C and
1171 update if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 if (download_cur_ver != download_new_ver) {
Alan Cox2742fd82008-04-29 14:45:15 +01001173 dbg("%s - Update I2C dld from %d.%d to %d.%d",
1174 __func__,
1175 firmware_version->Ver_Major,
1176 firmware_version->Ver_Minor,
1177 OperationalMajorVersion,
1178 OperationalMinorVersion);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Johan Hovolde9305d22009-12-28 23:01:50 +01001180 record = kmalloc(1, GFP_KERNEL);
1181 if (!record) {
1182 dev_err(dev, "%s - out of memory.\n",
1183 __func__);
1184 kfree(firmware_version);
1185 kfree(rom_desc);
1186 kfree(ti_manuf_desc);
1187 return -ENOMEM;
1188 }
Alan Cox2742fd82008-04-29 14:45:15 +01001189 /* In order to update the I2C firmware we must
1190 * change the type 2 record to type 0xF2. This
1191 * will force the UMP to come up in Boot Mode.
1192 * Then while in boot mode, the driver will
1193 * download the latest firmware (padded to
1194 * 15.5k) into the UMP ram. Finally when the
1195 * device comes back up in download mode the
1196 * driver will cause the new firmware to be
1197 * copied from the UMP Ram to I2C and the
1198 * firmware will update the record type from
1199 * 0xf2 to 0x02.
1200 */
Johan Hovolde9305d22009-12-28 23:01:50 +01001201 *record = I2C_DESC_TYPE_FIRMWARE_BLANK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Alan Cox2742fd82008-04-29 14:45:15 +01001203 /* Change the I2C Firmware record type to
1204 0xf2 to trigger an update */
1205 status = write_rom(serial, start_address,
Johan Hovolde9305d22009-12-28 23:01:50 +01001206 sizeof(*record), record);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 if (status) {
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 status;
1213 }
1214
Alan Cox2742fd82008-04-29 14:45:15 +01001215 /* verify the write -- must do this in order
1216 * for write to complete before we do the
1217 * hardware reset
1218 */
1219 status = read_rom(serial,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 start_address,
Johan Hovolde9305d22009-12-28 23:01:50 +01001221 sizeof(*record),
1222 record);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 if (status) {
Johan Hovolde9305d22009-12-28 23:01:50 +01001224 kfree(record);
Alan Cox2742fd82008-04-29 14:45:15 +01001225 kfree(firmware_version);
1226 kfree(rom_desc);
1227 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 return status;
1229 }
1230
Johan Hovolde9305d22009-12-28 23:01:50 +01001231 if (*record != I2C_DESC_TYPE_FIRMWARE_BLANK) {
Alan Cox2742fd82008-04-29 14:45:15 +01001232 dev_err(dev,
1233 "%s - error resetting device\n",
1234 __func__);
Johan Hovolde9305d22009-12-28 23:01:50 +01001235 kfree(record);
Alan Cox2742fd82008-04-29 14:45:15 +01001236 kfree(firmware_version);
1237 kfree(rom_desc);
1238 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return -ENODEV;
1240 }
1241
Alan Cox2742fd82008-04-29 14:45:15 +01001242 dbg("%s - HARDWARE RESET", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Alan Cox2742fd82008-04-29 14:45:15 +01001244 /* Reset UMP -- Back to BOOT MODE */
1245 status = ti_vsend_sync(serial->serial->dev,
1246 UMPC_HARDWARE_RESET,
1247 0, 0, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Alan Cox2742fd82008-04-29 14:45:15 +01001249 dbg("%s - HARDWARE RESET return %d",
1250 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 /* return an error on purpose. */
Johan Hovolde9305d22009-12-28 23:01:50 +01001253 kfree(record);
Alan Cox2742fd82008-04-29 14:45:15 +01001254 kfree(firmware_version);
1255 kfree(rom_desc);
1256 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 return -ENODEV;
1258 }
Alan Cox2742fd82008-04-29 14:45:15 +01001259 kfree(firmware_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 }
Alan Cox2742fd82008-04-29 14:45:15 +01001261 /* Search for type 0xF2 record (firmware blank record) */
1262 else if ((start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_FIRMWARE_BLANK, rom_desc)) != 0) {
1263#define HEADER_SIZE (sizeof(struct ti_i2c_desc) + \
1264 sizeof(struct ti_i2c_firmware_rec))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 __u8 *header;
1266 __u8 *vheader;
1267
Alan Cox2742fd82008-04-29 14:45:15 +01001268 header = kmalloc(HEADER_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 if (!header) {
Alan Cox2742fd82008-04-29 14:45:15 +01001270 dev_err(dev, "%s - out of memory.\n", __func__);
1271 kfree(rom_desc);
1272 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 return -ENOMEM;
1274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Alan Cox2742fd82008-04-29 14:45:15 +01001276 vheader = kmalloc(HEADER_SIZE, GFP_KERNEL);
1277 if (!vheader) {
1278 dev_err(dev, "%s - out of memory.\n", __func__);
1279 kfree(header);
1280 kfree(rom_desc);
1281 kfree(ti_manuf_desc);
1282 return -ENOMEM;
1283 }
1284
1285 dbg("%s - Found Type BLANK FIRMWARE (Type F2) record",
1286 __func__);
1287
1288 /*
1289 * In order to update the I2C firmware we must change
1290 * the type 2 record to type 0xF2. This will force the
1291 * UMP to come up in Boot Mode. Then while in boot
1292 * mode, the driver will download the latest firmware
1293 * (padded to 15.5k) into the UMP ram. Finally when the
1294 * device comes back up in download mode the driver
1295 * will cause the new firmware to be copied from the
1296 * UMP Ram to I2C and the firmware will update the
1297 * record type from 0xf2 to 0x02.
1298 */
1299 status = build_i2c_fw_hdr(header, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001301 kfree(vheader);
1302 kfree(header);
1303 kfree(rom_desc);
1304 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 return status;
1306 }
1307
Alan Cox2742fd82008-04-29 14:45:15 +01001308 /* Update I2C with type 0xf2 record with correct
1309 size and checksum */
1310 status = write_rom(serial,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 start_address,
1312 HEADER_SIZE,
1313 header);
1314 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001315 kfree(vheader);
1316 kfree(header);
1317 kfree(rom_desc);
1318 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 return status;
1320 }
1321
Alan Cox2742fd82008-04-29 14:45:15 +01001322 /* verify the write -- must do this in order for
1323 write to complete before we do the hardware reset */
1324 status = read_rom(serial, start_address,
1325 HEADER_SIZE, vheader);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001328 dbg("%s - can't read header back", __func__);
1329 kfree(vheader);
1330 kfree(header);
1331 kfree(rom_desc);
1332 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 return status;
1334 }
1335 if (memcmp(vheader, header, HEADER_SIZE)) {
Alan Cox2742fd82008-04-29 14:45:15 +01001336 dbg("%s - write download record failed",
1337 __func__);
1338 kfree(vheader);
1339 kfree(header);
1340 kfree(rom_desc);
1341 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 return status;
1343 }
1344
Alan Cox2742fd82008-04-29 14:45:15 +01001345 kfree(vheader);
1346 kfree(header);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Alan Cox2742fd82008-04-29 14:45:15 +01001348 dbg("%s - Start firmware update", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Alan Cox2742fd82008-04-29 14:45:15 +01001350 /* Tell firmware to copy download image into I2C */
1351 status = ti_vsend_sync(serial->serial->dev,
1352 UMPC_COPY_DNLD_TO_I2C, 0, 0, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Alan Cox2742fd82008-04-29 14:45:15 +01001354 dbg("%s - Update complete 0x%x", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001356 dev_err(dev,
1357 "%s - UMPC_COPY_DNLD_TO_I2C failed\n",
1358 __func__);
1359 kfree(rom_desc);
1360 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 return status;
1362 }
1363 }
1364
1365 // The device is running the download code
Alan Cox2742fd82008-04-29 14:45:15 +01001366 kfree(rom_desc);
1367 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 return 0;
1369 }
1370
1371 /********************************************************************/
1372 /* Boot Mode */
1373 /********************************************************************/
Andrew Morton9544e832008-02-04 23:57:50 -08001374 dbg("%s - RUNNING IN BOOT MODE", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Alan Cox2742fd82008-04-29 14:45:15 +01001376 /* Configure the TI device so we can use the BULK pipes for download */
1377 status = config_boot_dev(serial->serial->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 if (status)
1379 return status;
1380
Alan Cox2742fd82008-04-29 14:45:15 +01001381 if (le16_to_cpu(serial->serial->dev->descriptor.idVendor)
1382 != USB_VENDOR_ID_ION) {
1383 dbg("%s - VID = 0x%x", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 le16_to_cpu(serial->serial->dev->descriptor.idVendor));
1385 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
Alan Cox2742fd82008-04-29 14:45:15 +01001386 goto stayinbootmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 }
1388
Alan Cox2742fd82008-04-29 14:45:15 +01001389 /* We have an ION device (I2c Must be programmed)
1390 Determine I2C image type */
1391 if (i2c_type_bootmode(serial))
1392 goto stayinbootmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Alan Cox2742fd82008-04-29 14:45:15 +01001394 /* Check for ION Vendor ID and that the I2C is valid */
1395 if (!check_i2c_image(serial)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 struct ti_i2c_image_header *header;
1397 int i;
1398 __u8 cs = 0;
1399 __u8 *buffer;
1400 int buffer_size;
Jaswinder Singhd12b2192008-07-04 23:06:09 +05301401 int err;
1402 const struct firmware *fw;
1403 const char *fw_name = "edgeport/down3.bin";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 /* Validate Hardware version number
1406 * Read Manufacturing Descriptor from TI Based Edgeport
1407 */
Alan Cox2742fd82008-04-29 14:45:15 +01001408 ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 if (!ti_manuf_desc) {
Alan Cox2742fd82008-04-29 14:45:15 +01001410 dev_err(dev, "%s - out of memory.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 return -ENOMEM;
1412 }
Alan Cox2742fd82008-04-29 14:45:15 +01001413 status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001415 kfree(ti_manuf_desc);
1416 goto stayinbootmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 }
1418
Alan Cox2742fd82008-04-29 14:45:15 +01001419 /* Check for version 2 */
1420 if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
1421 dbg("%s - Wrong CPU Rev %d (Must be 2)",
1422 __func__, ti_cpu_rev(ti_manuf_desc));
1423 kfree(ti_manuf_desc);
1424 goto stayinbootmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 }
1426
Alan Cox2742fd82008-04-29 14:45:15 +01001427 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 /*
Alan Cox2742fd82008-04-29 14:45:15 +01001430 * In order to update the I2C firmware we must change the type
1431 * 2 record to type 0xF2. This will force the UMP to come up
1432 * in Boot Mode. Then while in boot mode, the driver will
1433 * download the latest firmware (padded to 15.5k) into the
1434 * UMP ram. Finally when the device comes back up in download
1435 * mode the driver will cause the new firmware to be copied
1436 * from the UMP Ram to I2C and the firmware will update the
1437 * record type from 0xf2 to 0x02.
1438 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 * Do we really have to copy the whole firmware image,
1440 * or could we do this in place!
1441 */
1442
Alan Cox2742fd82008-04-29 14:45:15 +01001443 /* Allocate a 15.5k buffer + 3 byte header */
1444 buffer_size = (((1024 * 16) - 512) +
1445 sizeof(struct ti_i2c_image_header));
1446 buffer = kmalloc(buffer_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 if (!buffer) {
Alan Cox2742fd82008-04-29 14:45:15 +01001448 dev_err(dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 return -ENOMEM;
1450 }
Alan Cox2742fd82008-04-29 14:45:15 +01001451
1452 /* Initialize the buffer to 0xff (pad the buffer) */
1453 memset(buffer, 0xff, buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Jaswinder Singhd12b2192008-07-04 23:06:09 +05301455 err = request_firmware(&fw, fw_name, dev);
1456 if (err) {
1457 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
1458 fw_name, err);
1459 kfree(buffer);
1460 return err;
1461 }
1462 memcpy(buffer, &fw->data[4], fw->size - 4);
1463 release_firmware(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Alan Cox2742fd82008-04-29 14:45:15 +01001465 for (i = sizeof(struct ti_i2c_image_header);
1466 i < buffer_size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 cs = (__u8)(cs + buffer[i]);
1468 }
Alan Cox2742fd82008-04-29 14:45:15 +01001469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 header = (struct ti_i2c_image_header *)buffer;
Alan Cox2742fd82008-04-29 14:45:15 +01001471
1472 /* update length and checksum after padding */
1473 header->Length = cpu_to_le16((__u16)(buffer_size -
1474 sizeof(struct ti_i2c_image_header)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 header->CheckSum = cs;
1476
Alan Cox2742fd82008-04-29 14:45:15 +01001477 /* Download the operational code */
1478 dbg("%s - Downloading operational code image (TI UMP)",
1479 __func__);
1480 status = download_code(serial, buffer, buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Alan Cox2742fd82008-04-29 14:45:15 +01001482 kfree(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
1484 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001485 dbg("%s - Error downloading operational code image",
1486 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 return status;
1488 }
1489
Alan Cox2742fd82008-04-29 14:45:15 +01001490 /* Device will reboot */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 serial->product_info.TiMode = TI_MODE_TRANSITIONING;
1492
Alan Cox2742fd82008-04-29 14:45:15 +01001493 dbg("%s - Download successful -- Device rebooting...",
1494 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
1496 /* return an error on purpose */
1497 return -ENODEV;
1498 }
1499
Alan Cox2742fd82008-04-29 14:45:15 +01001500stayinbootmode:
1501 /* Eprom is invalid or blank stay in boot mode */
Andrew Morton9544e832008-02-04 23:57:50 -08001502 dbg("%s - STAYING IN BOOT MODE", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 serial->product_info.TiMode = TI_MODE_BOOT;
1504
1505 return 0;
1506}
1507
1508
Alan Cox2742fd82008-04-29 14:45:15 +01001509static int ti_do_config(struct edgeport_port *port, int feature, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
1511 int port_number = port->port->number - port->port->serial->minor;
Alan Cox2742fd82008-04-29 14:45:15 +01001512 on = !!on; /* 1 or 0 not bitmask */
1513 return send_cmd(port->port->serial->dev,
1514 feature, (__u8)(UMPM_UART1_PORT + port_number),
1515 on, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516}
1517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Alan Cox2742fd82008-04-29 14:45:15 +01001519static int restore_mcr(struct edgeport_port *port, __u8 mcr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
1521 int status = 0;
1522
Alan Cox2742fd82008-04-29 14:45:15 +01001523 dbg("%s - %x", __func__, mcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Alan Cox2742fd82008-04-29 14:45:15 +01001525 status = ti_do_config(port, UMPC_SET_CLR_DTR, mcr & MCR_DTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 if (status)
1527 return status;
Alan Cox2742fd82008-04-29 14:45:15 +01001528 status = ti_do_config(port, UMPC_SET_CLR_RTS, mcr & MCR_RTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 if (status)
1530 return status;
Alan Cox2742fd82008-04-29 14:45:15 +01001531 return ti_do_config(port, UMPC_SET_CLR_LOOPBACK, mcr & MCR_LOOPBACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532}
1533
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534/* Convert TI LSR to standard UART flags */
Alan Cox2742fd82008-04-29 14:45:15 +01001535static __u8 map_line_status(__u8 ti_lsr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536{
1537 __u8 lsr = 0;
1538
1539#define MAP_FLAG(flagUmp, flagUart) \
1540 if (ti_lsr & flagUmp) \
1541 lsr |= flagUart;
1542
1543 MAP_FLAG(UMP_UART_LSR_OV_MASK, LSR_OVER_ERR) /* overrun */
1544 MAP_FLAG(UMP_UART_LSR_PE_MASK, LSR_PAR_ERR) /* parity error */
1545 MAP_FLAG(UMP_UART_LSR_FE_MASK, LSR_FRM_ERR) /* framing error */
1546 MAP_FLAG(UMP_UART_LSR_BR_MASK, LSR_BREAK) /* break detected */
Alan Cox2742fd82008-04-29 14:45:15 +01001547 MAP_FLAG(UMP_UART_LSR_RX_MASK, LSR_RX_AVAIL) /* rx data available */
1548 MAP_FLAG(UMP_UART_LSR_TX_MASK, LSR_TX_EMPTY) /* tx hold reg empty */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
1550#undef MAP_FLAG
1551
1552 return lsr;
1553}
1554
Alan Cox2742fd82008-04-29 14:45:15 +01001555static void handle_new_msr(struct edgeport_port *edge_port, __u8 msr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556{
1557 struct async_icount *icount;
1558 struct tty_struct *tty;
1559
Alan Cox2742fd82008-04-29 14:45:15 +01001560 dbg("%s - %02x", __func__, msr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Alan Cox2742fd82008-04-29 14:45:15 +01001562 if (msr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
1563 EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 icount = &edge_port->icount;
1565
1566 /* update input line counters */
1567 if (msr & EDGEPORT_MSR_DELTA_CTS)
1568 icount->cts++;
1569 if (msr & EDGEPORT_MSR_DELTA_DSR)
1570 icount->dsr++;
1571 if (msr & EDGEPORT_MSR_DELTA_CD)
1572 icount->dcd++;
1573 if (msr & EDGEPORT_MSR_DELTA_RI)
1574 icount->rng++;
Alan Cox2742fd82008-04-29 14:45:15 +01001575 wake_up_interruptible(&edge_port->delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 }
1577
1578 /* Save the new modem status */
1579 edge_port->shadow_msr = msr & 0xf0;
1580
Alan Cox4a90f092008-10-13 10:39:46 +01001581 tty = tty_port_tty_get(&edge_port->port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 /* handle CTS flow control */
1583 if (tty && C_CRTSCTS(tty)) {
1584 if (msr & EDGEPORT_MSR_CTS) {
1585 tty->hw_stopped = 0;
1586 tty_wakeup(tty);
1587 } else {
1588 tty->hw_stopped = 1;
1589 }
1590 }
Alan Cox4a90f092008-10-13 10:39:46 +01001591 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
1593 return;
1594}
1595
Alan Cox2742fd82008-04-29 14:45:15 +01001596static void handle_new_lsr(struct edgeport_port *edge_port, int lsr_data,
1597 __u8 lsr, __u8 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598{
1599 struct async_icount *icount;
Alan Cox2742fd82008-04-29 14:45:15 +01001600 __u8 new_lsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR |
1601 LSR_FRM_ERR | LSR_BREAK));
Alan Cox4a90f092008-10-13 10:39:46 +01001602 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Alan Cox2742fd82008-04-29 14:45:15 +01001604 dbg("%s - %02x", __func__, new_lsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
1606 edge_port->shadow_lsr = lsr;
1607
Alan Cox2742fd82008-04-29 14:45:15 +01001608 if (new_lsr & LSR_BREAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 /*
1610 * Parity and Framing errors only count if they
1611 * occur exclusive of a break being received.
1612 */
1613 new_lsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
1615 /* Place LSR data byte into Rx buffer */
Alan Cox4a90f092008-10-13 10:39:46 +01001616 if (lsr_data) {
1617 tty = tty_port_tty_get(&edge_port->port->port);
1618 if (tty) {
1619 edge_tty_recv(&edge_port->port->dev, tty, &data, 1);
1620 tty_kref_put(tty);
1621 }
1622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
1624 /* update input line counters */
1625 icount = &edge_port->icount;
1626 if (new_lsr & LSR_BREAK)
1627 icount->brk++;
1628 if (new_lsr & LSR_OVER_ERR)
1629 icount->overrun++;
1630 if (new_lsr & LSR_PAR_ERR)
1631 icount->parity++;
1632 if (new_lsr & LSR_FRM_ERR)
1633 icount->frame++;
1634}
1635
1636
Alan Cox2742fd82008-04-29 14:45:15 +01001637static void edge_interrupt_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638{
Ming Leicdc97792008-02-24 18:41:47 +08001639 struct edgeport_serial *edge_serial = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 struct usb_serial_port *port;
1641 struct edgeport_port *edge_port;
1642 unsigned char *data = urb->transfer_buffer;
1643 int length = urb->actual_length;
1644 int port_number;
1645 int function;
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001646 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 __u8 lsr;
1648 __u8 msr;
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001649 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
Harvey Harrison441b62c2008-03-03 16:08:34 -08001651 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001653 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 case 0:
1655 /* success */
1656 break;
1657 case -ECONNRESET:
1658 case -ENOENT:
1659 case -ESHUTDOWN:
1660 /* this urb is terminated, clean up */
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001661 dbg("%s - urb shutting down with status: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001662 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 return;
1664 default:
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001665 dev_err(&urb->dev->dev, "%s - nonzero urb status received: "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001666 "%d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 goto exit;
1668 }
1669
1670 if (!length) {
Alan Cox2742fd82008-04-29 14:45:15 +01001671 dbg("%s - no data in urb", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 goto exit;
1673 }
1674
Alan Cox2742fd82008-04-29 14:45:15 +01001675 usb_serial_debug_data(debug, &edge_serial->serial->dev->dev,
1676 __func__, length, data);
1677
1678 if (length != 2) {
1679 dbg("%s - expecting packet of size 2, got %d",
1680 __func__, length);
1681 goto exit;
1682 }
1683
1684 port_number = TIUMP_GET_PORT_FROM_CODE(data[0]);
1685 function = TIUMP_GET_FUNC_FROM_CODE(data[0]);
1686 dbg("%s - port_number %d, function %d, info 0x%x",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001687 __func__, port_number, function, data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 port = edge_serial->serial->port[port_number];
1689 edge_port = usb_get_serial_port_data(port);
1690 if (!edge_port) {
Alan Cox2742fd82008-04-29 14:45:15 +01001691 dbg("%s - edge_port not found", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 return;
1693 }
1694 switch (function) {
1695 case TIUMP_INTERRUPT_CODE_LSR:
Alan Cox2742fd82008-04-29 14:45:15 +01001696 lsr = map_line_status(data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 if (lsr & UMP_UART_LSR_DATA_MASK) {
Alan Cox2742fd82008-04-29 14:45:15 +01001698 /* Save the LSR event for bulk read
1699 completion routine */
1700 dbg("%s - LSR Event Port %u LSR Status = %02x",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001701 __func__, port_number, lsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 edge_port->lsr_event = 1;
1703 edge_port->lsr_mask = lsr;
1704 } else {
Alan Cox2742fd82008-04-29 14:45:15 +01001705 dbg("%s - ===== Port %d LSR Status = %02x ======",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001706 __func__, port_number, lsr);
Alan Cox2742fd82008-04-29 14:45:15 +01001707 handle_new_lsr(edge_port, 0, lsr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 }
1709 break;
1710
Alan Cox2742fd82008-04-29 14:45:15 +01001711 case TIUMP_INTERRUPT_CODE_MSR: /* MSR */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 /* Copy MSR from UMP */
1713 msr = data[1];
Joe Perches759f3632010-02-05 16:50:08 -08001714 dbg("%s - ===== Port %u MSR Status = %02x ======",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001715 __func__, port_number, msr);
Alan Cox2742fd82008-04-29 14:45:15 +01001716 handle_new_msr(edge_port, msr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 break;
1718
1719 default:
Alan Cox2742fd82008-04-29 14:45:15 +01001720 dev_err(&urb->dev->dev,
1721 "%s - Unknown Interrupt code from UMP %x\n",
1722 __func__, data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 break;
Alan Cox2742fd82008-04-29 14:45:15 +01001724
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 }
1726
1727exit:
Alan Cox2742fd82008-04-29 14:45:15 +01001728 retval = usb_submit_urb(urb, GFP_ATOMIC);
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001729 if (retval)
Alan Cox2742fd82008-04-29 14:45:15 +01001730 dev_err(&urb->dev->dev,
1731 "%s - usb_submit_urb failed with result %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001732 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733}
1734
Alan Cox2742fd82008-04-29 14:45:15 +01001735static void edge_bulk_in_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736{
Ming Leicdc97792008-02-24 18:41:47 +08001737 struct edgeport_port *edge_port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 unsigned char *data = urb->transfer_buffer;
1739 struct tty_struct *tty;
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001740 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 int port_number;
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001742 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
Harvey Harrison441b62c2008-03-03 16:08:34 -08001744 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001746 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 case 0:
1748 /* success */
1749 break;
1750 case -ECONNRESET:
1751 case -ENOENT:
1752 case -ESHUTDOWN:
1753 /* this urb is terminated, clean up */
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001754 dbg("%s - urb shutting down with status: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001755 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 return;
1757 default:
Alan Cox2742fd82008-04-29 14:45:15 +01001758 dev_err(&urb->dev->dev,
1759 "%s - nonzero read bulk status received: %d\n",
1760 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 }
1762
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001763 if (status == -EPIPE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 goto exit;
1765
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001766 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001767 dev_err(&urb->dev->dev, "%s - stopping read!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 return;
1769 }
1770
1771 port_number = edge_port->port->number - edge_port->port->serial->minor;
1772
1773 if (edge_port->lsr_event) {
1774 edge_port->lsr_event = 0;
Alan Cox2742fd82008-04-29 14:45:15 +01001775 dbg("%s ===== Port %u LSR Status = %02x, Data = %02x ======",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001776 __func__, port_number, edge_port->lsr_mask, *data);
Alan Cox2742fd82008-04-29 14:45:15 +01001777 handle_new_lsr(edge_port, 1, edge_port->lsr_mask, *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 /* Adjust buffer length/pointer */
1779 --urb->actual_length;
1780 ++data;
1781 }
1782
Alan Cox4a90f092008-10-13 10:39:46 +01001783 tty = tty_port_tty_get(&edge_port->port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 if (tty && urb->actual_length) {
Alan Cox2742fd82008-04-29 14:45:15 +01001785 usb_serial_debug_data(debug, &edge_port->port->dev,
1786 __func__, urb->actual_length, data);
1787 if (edge_port->close_pending)
1788 dbg("%s - close pending, dropping data on the floor",
1789 __func__);
1790 else
1791 edge_tty_recv(&edge_port->port->dev, tty, data,
1792 urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 edge_port->icount.rx += urb->actual_length;
1794 }
Alan Cox4a90f092008-10-13 10:39:46 +01001795 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
1797exit:
1798 /* continue read unless stopped */
1799 spin_lock(&edge_port->ep_lock);
1800 if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING) {
1801 urb->dev = edge_port->port->serial->dev;
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001802 retval = usb_submit_urb(urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 } else if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPING) {
1804 edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPED;
1805 }
1806 spin_unlock(&edge_port->ep_lock);
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001807 if (retval)
Alan Cox2742fd82008-04-29 14:45:15 +01001808 dev_err(&urb->dev->dev,
1809 "%s - usb_submit_urb failed with result %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001810 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811}
1812
Alan Cox2742fd82008-04-29 14:45:15 +01001813static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
1814 unsigned char *data, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815{
Alan Cox2742fd82008-04-29 14:45:15 +01001816 int queued;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Alan Cox2742fd82008-04-29 14:45:15 +01001818 queued = tty_insert_flip_string(tty, data, length);
1819 if (queued < length)
1820 dev_err(dev, "%s - dropping data, %d bytes lost\n",
1821 __func__, length - queued);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 tty_flip_buffer_push(tty);
1823}
1824
Alan Cox2742fd82008-04-29 14:45:15 +01001825static void edge_bulk_out_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826{
Ming Leicdc97792008-02-24 18:41:47 +08001827 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001829 int status = urb->status;
Alan Cox4a90f092008-10-13 10:39:46 +01001830 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Alan Cox2742fd82008-04-29 14:45:15 +01001832 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
1834 edge_port->ep_write_urb_in_use = 0;
1835
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001836 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 case 0:
1838 /* success */
1839 break;
1840 case -ECONNRESET:
1841 case -ENOENT:
1842 case -ESHUTDOWN:
1843 /* this urb is terminated, clean up */
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001844 dbg("%s - urb shutting down with status: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001845 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 return;
1847 default:
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001848 dev_err(&urb->dev->dev, "%s - nonzero write bulk status "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001849 "received: %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 }
1851
1852 /* send any buffered data */
Alan Cox4a90f092008-10-13 10:39:46 +01001853 tty = tty_port_tty_get(&port->port);
1854 edge_send(tty);
1855 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856}
1857
Alan Coxa509a7e2009-09-19 13:13:26 -07001858static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859{
1860 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1861 struct edgeport_serial *edge_serial;
1862 struct usb_device *dev;
1863 struct urb *urb;
1864 int port_number;
1865 int status;
1866 u16 open_settings;
1867 u8 transaction_timeout;
1868
Harvey Harrison441b62c2008-03-03 16:08:34 -08001869 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
1871 if (edge_port == NULL)
1872 return -ENODEV;
1873
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 port_number = port->number - port->serial->minor;
1875 switch (port_number) {
Alan Cox2742fd82008-04-29 14:45:15 +01001876 case 0:
1877 edge_port->uart_base = UMPMEM_BASE_UART1;
1878 edge_port->dma_address = UMPD_OEDB1_ADDRESS;
1879 break;
1880 case 1:
1881 edge_port->uart_base = UMPMEM_BASE_UART2;
1882 edge_port->dma_address = UMPD_OEDB2_ADDRESS;
1883 break;
1884 default:
1885 dev_err(&port->dev, "Unknown port number!!!\n");
1886 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 }
1888
Alan Cox2742fd82008-04-29 14:45:15 +01001889 dbg("%s - port_number = %d, uart_base = %04x, dma_address = %04x",
1890 __func__, port_number, edge_port->uart_base,
1891 edge_port->dma_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
1893 dev = port->serial->dev;
1894
Alan Cox2742fd82008-04-29 14:45:15 +01001895 memset(&(edge_port->icount), 0x00, sizeof(edge_port->icount));
1896 init_waitqueue_head(&edge_port->delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
1898 /* turn off loopback */
Alan Cox2742fd82008-04-29 14:45:15 +01001899 status = ti_do_config(edge_port, UMPC_SET_CLR_LOOPBACK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001901 dev_err(&port->dev,
1902 "%s - cannot send clear loopback command, %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001903 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 return status;
1905 }
Alan Cox2742fd82008-04-29 14:45:15 +01001906
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 /* set up the port settings */
Alan Cox95da3102008-07-22 11:09:07 +01001908 if (tty)
Alan Cox4a90f092008-10-13 10:39:46 +01001909 edge_set_termios(tty, port, tty->termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
1911 /* open up the port */
1912
1913 /* milliseconds to timeout for DMA transfer */
1914 transaction_timeout = 2;
1915
Alan Cox2742fd82008-04-29 14:45:15 +01001916 edge_port->ump_read_timeout =
1917 max(20, ((transaction_timeout * 3) / 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
Alan Cox2742fd82008-04-29 14:45:15 +01001919 /* milliseconds to timeout for DMA transfer */
1920 open_settings = (u8)(UMP_DMA_MODE_CONTINOUS |
1921 UMP_PIPE_TRANS_TIMEOUT_ENA |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 (transaction_timeout << 2));
1923
Alan Cox2742fd82008-04-29 14:45:15 +01001924 dbg("%s - Sending UMPC_OPEN_PORT", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
1926 /* Tell TI to open and start the port */
Alan Cox2742fd82008-04-29 14:45:15 +01001927 status = send_cmd(dev, UMPC_OPEN_PORT,
1928 (u8)(UMPM_UART1_PORT + port_number), open_settings, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001930 dev_err(&port->dev, "%s - cannot send open command, %d\n",
1931 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 return status;
1933 }
1934
1935 /* Start the DMA? */
Alan Cox2742fd82008-04-29 14:45:15 +01001936 status = send_cmd(dev, UMPC_START_PORT,
1937 (u8)(UMPM_UART1_PORT + port_number), 0, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001939 dev_err(&port->dev, "%s - cannot send start DMA command, %d\n",
1940 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 return status;
1942 }
1943
1944 /* Clear TX and RX buffers in UMP */
Alan Cox2742fd82008-04-29 14:45:15 +01001945 status = purge_port(port, UMP_PORT_DIR_OUT | UMP_PORT_DIR_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001947 dev_err(&port->dev,
1948 "%s - cannot send clear buffers command, %d\n",
1949 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 return status;
1951 }
1952
1953 /* Read Initial MSR */
Alan Cox2742fd82008-04-29 14:45:15 +01001954 status = ti_vread_sync(dev, UMPC_READ_MSR, 0,
1955 (__u16)(UMPM_UART1_PORT + port_number),
1956 &edge_port->shadow_msr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001958 dev_err(&port->dev, "%s - cannot send read MSR command, %d\n",
1959 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 return status;
1961 }
1962
Alan Cox2742fd82008-04-29 14:45:15 +01001963 dbg("ShadowMSR 0x%X", edge_port->shadow_msr);
1964
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 /* Set Initial MCR */
1966 edge_port->shadow_mcr = MCR_RTS | MCR_DTR;
Alan Cox2742fd82008-04-29 14:45:15 +01001967 dbg("ShadowMCR 0x%X", edge_port->shadow_mcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
1969 edge_serial = edge_port->edge_serial;
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01001970 if (mutex_lock_interruptible(&edge_serial->es_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 return -ERESTARTSYS;
1972 if (edge_serial->num_ports_open == 0) {
Alan Cox2742fd82008-04-29 14:45:15 +01001973 /* we are the first port to open, post the interrupt urb */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 urb = edge_serial->serial->port[0]->interrupt_in_urb;
1975 if (!urb) {
Alan Cox2742fd82008-04-29 14:45:15 +01001976 dev_err(&port->dev,
1977 "%s - no interrupt urb present, exiting\n",
1978 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 status = -EINVAL;
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01001980 goto release_es_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 }
1982 urb->complete = edge_interrupt_callback;
1983 urb->context = edge_serial;
1984 urb->dev = dev;
Alan Cox2742fd82008-04-29 14:45:15 +01001985 status = usb_submit_urb(urb, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001987 dev_err(&port->dev,
1988 "%s - usb_submit_urb failed with value %d\n",
1989 __func__, status);
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01001990 goto release_es_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 }
1992 }
1993
1994 /*
1995 * reset the data toggle on the bulk endpoints to work around bug in
1996 * host controllers where things get out of sync some times
1997 */
Alan Cox2742fd82008-04-29 14:45:15 +01001998 usb_clear_halt(dev, port->write_urb->pipe);
1999 usb_clear_halt(dev, port->read_urb->pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
2001 /* start up our bulk read urb */
2002 urb = port->read_urb;
2003 if (!urb) {
Alan Cox2742fd82008-04-29 14:45:15 +01002004 dev_err(&port->dev, "%s - no read urb present, exiting\n",
2005 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 status = -EINVAL;
2007 goto unlink_int_urb;
2008 }
2009 edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
2010 urb->complete = edge_bulk_in_callback;
2011 urb->context = edge_port;
2012 urb->dev = dev;
Alan Cox2742fd82008-04-29 14:45:15 +01002013 status = usb_submit_urb(urb, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01002015 dev_err(&port->dev,
2016 "%s - read bulk usb_submit_urb failed with value %d\n",
2017 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 goto unlink_int_urb;
2019 }
2020
2021 ++edge_serial->num_ports_open;
2022
Harvey Harrison441b62c2008-03-03 16:08:34 -08002023 dbg("%s - exited", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01002025 goto release_es_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
2027unlink_int_urb:
2028 if (edge_port->edge_serial->num_ports_open == 0)
2029 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01002030release_es_lock:
2031 mutex_unlock(&edge_serial->es_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 return status;
2033}
2034
Alan Cox335f8512009-06-11 12:26:29 +01002035static void edge_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036{
2037 struct edgeport_serial *edge_serial;
2038 struct edgeport_port *edge_port;
2039 int port_number;
2040 int status;
2041
Harvey Harrison441b62c2008-03-03 16:08:34 -08002042 dbg("%s - port %d", __func__, port->number);
Alan Cox2742fd82008-04-29 14:45:15 +01002043
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 edge_serial = usb_get_serial_data(port->serial);
2045 edge_port = usb_get_serial_port_data(port);
Alan Cox2742fd82008-04-29 14:45:15 +01002046 if (edge_serial == NULL || edge_port == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 return;
Alan Cox2742fd82008-04-29 14:45:15 +01002048
2049 /* The bulkreadcompletion routine will check
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 * this flag and dump add read data */
2051 edge_port->close_pending = 1;
2052
2053 /* chase the port close and flush */
Alan Cox2742fd82008-04-29 14:45:15 +01002054 chase_port(edge_port, (HZ * closing_wait) / 100, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
2056 usb_kill_urb(port->read_urb);
2057 usb_kill_urb(port->write_urb);
2058 edge_port->ep_write_urb_in_use = 0;
2059
2060 /* assuming we can still talk to the device,
2061 * send a close port command to it */
Harvey Harrison441b62c2008-03-03 16:08:34 -08002062 dbg("%s - send umpc_close_port", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 port_number = port->number - port->serial->minor;
Alan Cox2742fd82008-04-29 14:45:15 +01002064 status = send_cmd(port->serial->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 UMPC_CLOSE_PORT,
2066 (__u8)(UMPM_UART1_PORT + port_number),
2067 0,
2068 NULL,
2069 0);
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01002070 mutex_lock(&edge_serial->es_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 --edge_port->edge_serial->num_ports_open;
2072 if (edge_port->edge_serial->num_ports_open <= 0) {
2073 /* last port is now closed, let's shut down our interrupt urb */
2074 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
2075 edge_port->edge_serial->num_ports_open = 0;
2076 }
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01002077 mutex_unlock(&edge_serial->es_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 edge_port->close_pending = 0;
2079
Harvey Harrison441b62c2008-03-03 16:08:34 -08002080 dbg("%s - exited", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081}
2082
Alan Cox95da3102008-07-22 11:09:07 +01002083static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
2084 const unsigned char *data, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085{
2086 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2087 unsigned long flags;
2088
Harvey Harrison441b62c2008-03-03 16:08:34 -08002089 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
2091 if (count == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002092 dbg("%s - write request of 0 bytes", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 return 0;
2094 }
2095
2096 if (edge_port == NULL)
2097 return -ENODEV;
2098 if (edge_port->close_pending == 1)
2099 return -ENODEV;
2100
2101 spin_lock_irqsave(&edge_port->ep_lock, flags);
2102 count = edge_buf_put(edge_port->ep_out_buf, data, count);
2103 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2104
Alan Cox95da3102008-07-22 11:09:07 +01002105 edge_send(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
2107 return count;
2108}
2109
Alan Cox95da3102008-07-22 11:09:07 +01002110static void edge_send(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111{
Alan Cox95da3102008-07-22 11:09:07 +01002112 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 int count, result;
2114 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 unsigned long flags;
2116
2117
Harvey Harrison441b62c2008-03-03 16:08:34 -08002118 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
2120 spin_lock_irqsave(&edge_port->ep_lock, flags);
2121
2122 if (edge_port->ep_write_urb_in_use) {
2123 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2124 return;
2125 }
2126
2127 count = edge_buf_get(edge_port->ep_out_buf,
2128 port->write_urb->transfer_buffer,
2129 port->bulk_out_size);
2130
2131 if (count == 0) {
2132 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2133 return;
2134 }
2135
2136 edge_port->ep_write_urb_in_use = 1;
2137
2138 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2139
Alan Cox2742fd82008-04-29 14:45:15 +01002140 usb_serial_debug_data(debug, &port->dev, __func__, count,
2141 port->write_urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
2143 /* set up our urb */
Alan Cox2742fd82008-04-29 14:45:15 +01002144 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
2145 usb_sndbulkpipe(port->serial->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 port->bulk_out_endpointAddress),
2147 port->write_urb->transfer_buffer, count,
2148 edge_bulk_out_callback,
2149 port);
2150
2151 /* send the data out the bulk port */
2152 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
2153 if (result) {
Alan Cox2742fd82008-04-29 14:45:15 +01002154 dev_err(&port->dev,
2155 "%s - failed submitting write urb, error %d\n",
2156 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 edge_port->ep_write_urb_in_use = 0;
Alan Cox2742fd82008-04-29 14:45:15 +01002158 /* TODO: reschedule edge_send */
2159 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 edge_port->icount.tx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
2162 /* wakeup any process waiting for writes to complete */
2163 /* there is now more room in the buffer for new writes */
Alan Cox2742fd82008-04-29 14:45:15 +01002164 if (tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166}
2167
Alan Cox95da3102008-07-22 11:09:07 +01002168static int edge_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169{
Alan Cox95da3102008-07-22 11:09:07 +01002170 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2172 int room = 0;
2173 unsigned long flags;
2174
Harvey Harrison441b62c2008-03-03 16:08:34 -08002175 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
2177 if (edge_port == NULL)
Alan Cox2742fd82008-04-29 14:45:15 +01002178 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 if (edge_port->close_pending == 1)
Alan Cox2742fd82008-04-29 14:45:15 +01002180 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
2182 spin_lock_irqsave(&edge_port->ep_lock, flags);
2183 room = edge_buf_space_avail(edge_port->ep_out_buf);
2184 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2185
Harvey Harrison441b62c2008-03-03 16:08:34 -08002186 dbg("%s - returns %d", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 return room;
2188}
2189
Alan Cox95da3102008-07-22 11:09:07 +01002190static int edge_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191{
Alan Cox95da3102008-07-22 11:09:07 +01002192 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2194 int chars = 0;
2195 unsigned long flags;
2196
Harvey Harrison441b62c2008-03-03 16:08:34 -08002197 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
2199 if (edge_port == NULL)
Alan Cox2742fd82008-04-29 14:45:15 +01002200 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 if (edge_port->close_pending == 1)
Alan Cox2742fd82008-04-29 14:45:15 +01002202 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
2204 spin_lock_irqsave(&edge_port->ep_lock, flags);
2205 chars = edge_buf_data_avail(edge_port->ep_out_buf);
2206 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2207
Alan Cox2742fd82008-04-29 14:45:15 +01002208 dbg("%s - returns %d", __func__, chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 return chars;
2210}
2211
Alan Cox95da3102008-07-22 11:09:07 +01002212static void edge_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213{
Alan Cox95da3102008-07-22 11:09:07 +01002214 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 int status;
2217
Harvey Harrison441b62c2008-03-03 16:08:34 -08002218 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
2220 if (edge_port == NULL)
2221 return;
2222
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 /* if we are implementing XON/XOFF, send the stop character */
2224 if (I_IXOFF(tty)) {
2225 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01002226 status = edge_write(tty, port, &stop_char, 1);
2227 if (status <= 0) {
2228 dev_err(&port->dev, "%s - failed to write stop character, %d\n", __func__, status);
2229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 }
2231
2232 /* if we are implementing RTS/CTS, stop reads */
2233 /* and the Edgeport will clear the RTS line */
2234 if (C_CRTSCTS(tty))
2235 stop_read(edge_port);
2236
2237}
2238
Alan Cox95da3102008-07-22 11:09:07 +01002239static void edge_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240{
Alan Cox95da3102008-07-22 11:09:07 +01002241 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 int status;
2244
Harvey Harrison441b62c2008-03-03 16:08:34 -08002245 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246
2247 if (edge_port == NULL)
2248 return;
2249
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 /* if we are implementing XON/XOFF, send the start character */
2251 if (I_IXOFF(tty)) {
2252 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01002253 status = edge_write(tty, port, &start_char, 1);
2254 if (status <= 0) {
2255 dev_err(&port->dev, "%s - failed to write start character, %d\n", __func__, status);
2256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 /* if we are implementing RTS/CTS, restart reads */
2259 /* are the Edgeport will assert the RTS line */
2260 if (C_CRTSCTS(tty)) {
2261 status = restart_read(edge_port);
2262 if (status)
Alan Cox2742fd82008-04-29 14:45:15 +01002263 dev_err(&port->dev,
2264 "%s - read bulk usb_submit_urb failed: %d\n",
2265 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 }
2267
2268}
2269
2270static void stop_read(struct edgeport_port *edge_port)
2271{
2272 unsigned long flags;
2273
2274 spin_lock_irqsave(&edge_port->ep_lock, flags);
2275
2276 if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
2277 edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPING;
2278 edge_port->shadow_mcr &= ~MCR_RTS;
2279
2280 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2281}
2282
2283static int restart_read(struct edgeport_port *edge_port)
2284{
2285 struct urb *urb;
2286 int status = 0;
2287 unsigned long flags;
2288
2289 spin_lock_irqsave(&edge_port->ep_lock, flags);
2290
2291 if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPED) {
2292 urb = edge_port->port->read_urb;
2293 urb->complete = edge_bulk_in_callback;
2294 urb->context = edge_port;
2295 urb->dev = edge_port->port->serial->dev;
Oliver Neukumefdff602007-06-05 10:50:48 +02002296 status = usb_submit_urb(urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 }
2298 edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
2299 edge_port->shadow_mcr |= MCR_RTS;
2300
2301 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2302
2303 return status;
2304}
2305
Alan Cox95da3102008-07-22 11:09:07 +01002306static void change_port_settings(struct tty_struct *tty,
2307 struct edgeport_port *edge_port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308{
2309 struct ump_uart_config *config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 int baud;
2311 unsigned cflag;
2312 int status;
Alan Cox2742fd82008-04-29 14:45:15 +01002313 int port_number = edge_port->port->number -
2314 edge_port->port->serial->minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
Harvey Harrison441b62c2008-03-03 16:08:34 -08002316 dbg("%s - port %d", __func__, edge_port->port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
Alan Cox95da3102008-07-22 11:09:07 +01002318 config = kmalloc (sizeof (*config), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 if (!config) {
Alan Cox2742fd82008-04-29 14:45:15 +01002320 *tty->termios = *old_termios;
2321 dev_err(&edge_port->port->dev, "%s - out of memory\n",
2322 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 return;
2324 }
2325
2326 cflag = tty->termios->c_cflag;
2327
2328 config->wFlags = 0;
2329
2330 /* These flags must be set */
2331 config->wFlags |= UMP_MASK_UART_FLAGS_RECEIVE_MS_INT;
2332 config->wFlags |= UMP_MASK_UART_FLAGS_AUTO_START_ON_ERR;
2333 config->bUartMode = (__u8)(edge_port->bUartMode);
2334
2335 switch (cflag & CSIZE) {
Alan Cox2742fd82008-04-29 14:45:15 +01002336 case CS5:
2337 config->bDataBits = UMP_UART_CHAR5BITS;
2338 dbg("%s - data bits = 5", __func__);
2339 break;
2340 case CS6:
2341 config->bDataBits = UMP_UART_CHAR6BITS;
2342 dbg("%s - data bits = 6", __func__);
2343 break;
2344 case CS7:
2345 config->bDataBits = UMP_UART_CHAR7BITS;
2346 dbg("%s - data bits = 7", __func__);
2347 break;
2348 default:
2349 case CS8:
2350 config->bDataBits = UMP_UART_CHAR8BITS;
2351 dbg("%s - data bits = 8", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 break;
2353 }
2354
2355 if (cflag & PARENB) {
2356 if (cflag & PARODD) {
2357 config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
2358 config->bParity = UMP_UART_ODDPARITY;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002359 dbg("%s - parity = odd", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 } else {
2361 config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
2362 config->bParity = UMP_UART_EVENPARITY;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002363 dbg("%s - parity = even", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 }
2365 } else {
Alan Cox2742fd82008-04-29 14:45:15 +01002366 config->bParity = UMP_UART_NOPARITY;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002367 dbg("%s - parity = none", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 }
2369
2370 if (cflag & CSTOPB) {
2371 config->bStopBits = UMP_UART_STOPBIT2;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002372 dbg("%s - stop bits = 2", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 } else {
2374 config->bStopBits = UMP_UART_STOPBIT1;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002375 dbg("%s - stop bits = 1", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 }
2377
2378 /* figure out the flow control settings */
2379 if (cflag & CRTSCTS) {
2380 config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X_CTS_FLOW;
2381 config->wFlags |= UMP_MASK_UART_FLAGS_RTS_FLOW;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002382 dbg("%s - RTS/CTS is enabled", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002384 dbg("%s - RTS/CTS is disabled", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 tty->hw_stopped = 0;
2386 restart_read(edge_port);
2387 }
2388
Alan Cox2742fd82008-04-29 14:45:15 +01002389 /* if we are implementing XON/XOFF, set the start and stop
2390 character in the device */
2391 config->cXon = START_CHAR(tty);
2392 config->cXoff = STOP_CHAR(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
Alan Cox2742fd82008-04-29 14:45:15 +01002394 /* if we are implementing INBOUND XON/XOFF */
2395 if (I_IXOFF(tty)) {
2396 config->wFlags |= UMP_MASK_UART_FLAGS_IN_X;
2397 dbg("%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2398 __func__, config->cXon, config->cXoff);
2399 } else
2400 dbg("%s - INBOUND XON/XOFF is disabled", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
Alan Cox2742fd82008-04-29 14:45:15 +01002402 /* if we are implementing OUTBOUND XON/XOFF */
2403 if (I_IXON(tty)) {
2404 config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X;
2405 dbg("%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2406 __func__, config->cXon, config->cXoff);
2407 } else
2408 dbg("%s - OUTBOUND XON/XOFF is disabled", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
Alan Coxd5f5bcd2008-01-03 16:57:33 +00002410 tty->termios->c_cflag &= ~CMSPAR;
2411
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 /* Round the baud rate */
2413 baud = tty_get_baud_rate(tty);
2414 if (!baud) {
2415 /* pick a default, any default... */
2416 baud = 9600;
Alan Coxd5f5bcd2008-01-03 16:57:33 +00002417 } else
2418 tty_encode_baud_rate(tty, baud, baud);
2419
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 edge_port->baud_rate = baud;
2421 config->wBaudRate = (__u16)((461550L + baud/2) / baud);
2422
Alan Coxd5f5bcd2008-01-03 16:57:33 +00002423 /* FIXME: Recompute actual baud from divisor here */
2424
Alan Cox2742fd82008-04-29 14:45:15 +01002425 dbg("%s - baud rate = %d, wBaudRate = %d", __func__, baud,
2426 config->wBaudRate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
Alan Cox2742fd82008-04-29 14:45:15 +01002428 dbg("wBaudRate: %d", (int)(461550L / config->wBaudRate));
2429 dbg("wFlags: 0x%x", config->wFlags);
2430 dbg("bDataBits: %d", config->bDataBits);
2431 dbg("bParity: %d", config->bParity);
2432 dbg("bStopBits: %d", config->bStopBits);
2433 dbg("cXon: %d", config->cXon);
2434 dbg("cXoff: %d", config->cXoff);
2435 dbg("bUartMode: %d", config->bUartMode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
2437 /* move the word values into big endian mode */
Alan Cox2742fd82008-04-29 14:45:15 +01002438 cpu_to_be16s(&config->wFlags);
2439 cpu_to_be16s(&config->wBaudRate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440
Alan Cox2742fd82008-04-29 14:45:15 +01002441 status = send_cmd(edge_port->port->serial->dev, UMPC_SET_CONFIG,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 (__u8)(UMPM_UART1_PORT + port_number),
Alan Cox2742fd82008-04-29 14:45:15 +01002443 0, (__u8 *)config, sizeof(*config));
2444 if (status)
2445 dbg("%s - error %d when trying to write config to device",
Harvey Harrison441b62c2008-03-03 16:08:34 -08002446 __func__, status);
Alan Cox2742fd82008-04-29 14:45:15 +01002447 kfree(config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 return;
2449}
2450
Alan Cox2e0ddd62008-07-22 11:12:33 +01002451static void edge_set_termios(struct tty_struct *tty,
Alan Cox95da3102008-07-22 11:09:07 +01002452 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453{
2454 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Alan Cox95da3102008-07-22 11:09:07 +01002455 unsigned int cflag;
2456
2457 cflag = tty->termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
Harvey Harrison441b62c2008-03-03 16:08:34 -08002459 dbg("%s - clfag %08x iflag %08x", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 tty->termios->c_cflag, tty->termios->c_iflag);
Harvey Harrison441b62c2008-03-03 16:08:34 -08002461 dbg("%s - old clfag %08x old iflag %08x", __func__,
Alan Coxd5f5bcd2008-01-03 16:57:33 +00002462 old_termios->c_cflag, old_termios->c_iflag);
Harvey Harrison441b62c2008-03-03 16:08:34 -08002463 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
2465 if (edge_port == NULL)
2466 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 /* change the port settings to the new ones specified */
Alan Cox95da3102008-07-22 11:09:07 +01002468 change_port_settings(tty, edge_port, old_termios);
2469 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470}
2471
Alan Cox95da3102008-07-22 11:09:07 +01002472static int edge_tiocmset(struct tty_struct *tty, struct file *file,
Alan Cox2742fd82008-04-29 14:45:15 +01002473 unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474{
Alan Cox95da3102008-07-22 11:09:07 +01002475 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2477 unsigned int mcr;
Alan Cox3d71fe02008-02-20 21:38:32 +00002478 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
Harvey Harrison441b62c2008-03-03 16:08:34 -08002480 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
Alan Cox3d71fe02008-02-20 21:38:32 +00002482 spin_lock_irqsave(&edge_port->ep_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 mcr = edge_port->shadow_mcr;
2484 if (set & TIOCM_RTS)
2485 mcr |= MCR_RTS;
2486 if (set & TIOCM_DTR)
2487 mcr |= MCR_DTR;
2488 if (set & TIOCM_LOOP)
2489 mcr |= MCR_LOOPBACK;
2490
2491 if (clear & TIOCM_RTS)
2492 mcr &= ~MCR_RTS;
2493 if (clear & TIOCM_DTR)
2494 mcr &= ~MCR_DTR;
2495 if (clear & TIOCM_LOOP)
2496 mcr &= ~MCR_LOOPBACK;
2497
2498 edge_port->shadow_mcr = mcr;
Alan Cox3d71fe02008-02-20 21:38:32 +00002499 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
Alan Cox2742fd82008-04-29 14:45:15 +01002501 restore_mcr(edge_port, mcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 return 0;
2503}
2504
Alan Cox95da3102008-07-22 11:09:07 +01002505static int edge_tiocmget(struct tty_struct *tty, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506{
Alan Cox95da3102008-07-22 11:09:07 +01002507 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2509 unsigned int result = 0;
2510 unsigned int msr;
2511 unsigned int mcr;
Alan Cox3d71fe02008-02-20 21:38:32 +00002512 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
Harvey Harrison441b62c2008-03-03 16:08:34 -08002514 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
Alan Cox3d71fe02008-02-20 21:38:32 +00002516 spin_lock_irqsave(&edge_port->ep_lock, flags);
2517
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 msr = edge_port->shadow_msr;
2519 mcr = edge_port->shadow_mcr;
2520 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
2521 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
2522 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
2523 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
2524 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
2525 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
2526
2527
Harvey Harrison441b62c2008-03-03 16:08:34 -08002528 dbg("%s -- %x", __func__, result);
Alan Cox3d71fe02008-02-20 21:38:32 +00002529 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530
2531 return result;
2532}
2533
Alan Cox2742fd82008-04-29 14:45:15 +01002534static int get_serial_info(struct edgeport_port *edge_port,
2535 struct serial_struct __user *retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536{
2537 struct serial_struct tmp;
2538
2539 if (!retinfo)
2540 return -EFAULT;
2541
2542 memset(&tmp, 0, sizeof(tmp));
2543
2544 tmp.type = PORT_16550A;
2545 tmp.line = edge_port->port->serial->minor;
2546 tmp.port = edge_port->port->number;
2547 tmp.irq = 0;
2548 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2549 tmp.xmit_fifo_size = edge_port->port->bulk_out_size;
2550 tmp.baud_base = 9600;
2551 tmp.close_delay = 5*HZ;
2552 tmp.closing_wait = closing_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
2554 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2555 return -EFAULT;
2556 return 0;
2557}
2558
Alan Cox95da3102008-07-22 11:09:07 +01002559static int edge_ioctl(struct tty_struct *tty, struct file *file,
Alan Cox2742fd82008-04-29 14:45:15 +01002560 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561{
Alan Cox95da3102008-07-22 11:09:07 +01002562 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2564 struct async_icount cnow;
2565 struct async_icount cprev;
2566
Harvey Harrison441b62c2008-03-03 16:08:34 -08002567 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568
2569 switch (cmd) {
Alan Cox2742fd82008-04-29 14:45:15 +01002570 case TIOCGSERIAL:
2571 dbg("%s - (%d) TIOCGSERIAL", __func__, port->number);
2572 return get_serial_info(edge_port,
2573 (struct serial_struct __user *) arg);
2574 case TIOCMIWAIT:
2575 dbg("%s - (%d) TIOCMIWAIT", __func__, port->number);
2576 cprev = edge_port->icount;
2577 while (1) {
2578 interruptible_sleep_on(&edge_port->delta_msr_wait);
2579 /* see if a signal did it */
2580 if (signal_pending(current))
2581 return -ERESTARTSYS;
2582 cnow = edge_port->icount;
2583 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2584 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2585 return -EIO; /* no change => error */
2586 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2587 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2588 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2589 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2590 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 }
Alan Cox2742fd82008-04-29 14:45:15 +01002592 cprev = cnow;
2593 }
2594 /* not reached */
2595 break;
2596 case TIOCGICOUNT:
2597 dbg("%s - (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
2598 port->number, edge_port->icount.rx, edge_port->icount.tx);
2599 if (copy_to_user((void __user *)arg, &edge_port->icount,
2600 sizeof(edge_port->icount)))
2601 return -EFAULT;
2602 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 return -ENOIOCTLCMD;
2605}
2606
Alan Cox95da3102008-07-22 11:09:07 +01002607static void edge_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608{
Alan Cox95da3102008-07-22 11:09:07 +01002609 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2611 int status;
Alan Cox2742fd82008-04-29 14:45:15 +01002612 int bv = 0; /* Off */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613
Alan Cox95da3102008-07-22 11:09:07 +01002614 dbg("%s - state = %d", __func__, break_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
2616 /* chase the port close */
Alan Cox2742fd82008-04-29 14:45:15 +01002617 chase_port(edge_port, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
Alan Cox95da3102008-07-22 11:09:07 +01002619 if (break_state == -1)
Alan Cox2742fd82008-04-29 14:45:15 +01002620 bv = 1; /* On */
2621 status = ti_do_config(edge_port, UMPC_SET_CLR_BREAK, bv);
2622 if (status)
2623 dbg("%s - error %d sending break set/clear command.",
Harvey Harrison441b62c2008-03-03 16:08:34 -08002624 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625}
2626
Alan Cox2742fd82008-04-29 14:45:15 +01002627static int edge_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628{
2629 struct edgeport_serial *edge_serial;
2630 struct edgeport_port *edge_port;
2631 struct usb_device *dev;
2632 int status;
2633 int i;
2634
2635 dev = serial->dev;
2636
2637 /* create our private serial structure */
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +01002638 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 if (edge_serial == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002640 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 return -ENOMEM;
2642 }
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01002643 mutex_init(&edge_serial->es_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 edge_serial->serial = serial;
2645 usb_set_serial_data(serial, edge_serial);
2646
Alan Cox2742fd82008-04-29 14:45:15 +01002647 status = download_fw(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01002649 kfree(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 return status;
2651 }
2652
2653 /* set up our port private structures */
2654 for (i = 0; i < serial->num_ports; ++i) {
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +01002655 edge_port = kzalloc(sizeof(struct edgeport_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 if (edge_port == NULL) {
Alan Cox2742fd82008-04-29 14:45:15 +01002657 dev_err(&serial->dev->dev, "%s - Out of memory\n",
2658 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 goto cleanup;
2660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 spin_lock_init(&edge_port->ep_lock);
2662 edge_port->ep_out_buf = edge_buf_alloc(EDGE_OUT_BUF_SIZE);
2663 if (edge_port->ep_out_buf == NULL) {
Alan Cox2742fd82008-04-29 14:45:15 +01002664 dev_err(&serial->dev->dev, "%s - Out of memory\n",
2665 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 kfree(edge_port);
2667 goto cleanup;
2668 }
2669 edge_port->port = serial->port[i];
2670 edge_port->edge_serial = edge_serial;
2671 usb_set_serial_port_data(serial->port[i], edge_port);
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002672 edge_port->bUartMode = default_uart_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 }
Alan Cox2742fd82008-04-29 14:45:15 +01002674
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 return 0;
2676
2677cleanup:
Alan Cox2742fd82008-04-29 14:45:15 +01002678 for (--i; i >= 0; --i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 edge_port = usb_get_serial_port_data(serial->port[i]);
2680 edge_buf_free(edge_port->ep_out_buf);
2681 kfree(edge_port);
2682 usb_set_serial_port_data(serial->port[i], NULL);
2683 }
Alan Cox2742fd82008-04-29 14:45:15 +01002684 kfree(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 usb_set_serial_data(serial, NULL);
2686 return -ENOMEM;
2687}
2688
Alan Sternf9c99bb2009-06-02 11:53:55 -04002689static void edge_disconnect(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690{
2691 int i;
2692 struct edgeport_port *edge_port;
2693
Alan Cox2742fd82008-04-29 14:45:15 +01002694 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695
Jesper Juhl0d46c002007-07-16 22:17:25 +02002696 for (i = 0; i < serial->num_ports; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 edge_port = usb_get_serial_port_data(serial->port[i]);
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002698 edge_remove_sysfs_attrs(edge_port->port);
Alan Sternf9c99bb2009-06-02 11:53:55 -04002699 }
2700}
2701
2702static void edge_release(struct usb_serial *serial)
2703{
2704 int i;
2705 struct edgeport_port *edge_port;
2706
2707 dbg("%s", __func__);
2708
2709 for (i = 0; i < serial->num_ports; ++i) {
2710 edge_port = usb_get_serial_port_data(serial->port[i]);
Jesper Juhl0d46c002007-07-16 22:17:25 +02002711 edge_buf_free(edge_port->ep_out_buf);
2712 kfree(edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 }
Jesper Juhl0d46c002007-07-16 22:17:25 +02002714 kfree(usb_get_serial_data(serial));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715}
2716
2717
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002718/* Sysfs Attributes */
2719
2720static ssize_t show_uart_mode(struct device *dev,
2721 struct device_attribute *attr, char *buf)
2722{
2723 struct usb_serial_port *port = to_usb_serial_port(dev);
2724 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2725
2726 return sprintf(buf, "%d\n", edge_port->bUartMode);
2727}
2728
2729static ssize_t store_uart_mode(struct device *dev,
2730 struct device_attribute *attr, const char *valbuf, size_t count)
2731{
2732 struct usb_serial_port *port = to_usb_serial_port(dev);
2733 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2734 unsigned int v = simple_strtoul(valbuf, NULL, 0);
2735
Harvey Harrison441b62c2008-03-03 16:08:34 -08002736 dbg("%s: setting uart_mode = %d", __func__, v);
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002737
2738 if (v < 256)
2739 edge_port->bUartMode = v;
2740 else
Harvey Harrison441b62c2008-03-03 16:08:34 -08002741 dev_err(dev, "%s - uart_mode %d is invalid\n", __func__, v);
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002742
2743 return count;
2744}
2745
Alan Cox2742fd82008-04-29 14:45:15 +01002746static DEVICE_ATTR(uart_mode, S_IWUSR | S_IRUGO, show_uart_mode,
2747 store_uart_mode);
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002748
2749static int edge_create_sysfs_attrs(struct usb_serial_port *port)
2750{
2751 return device_create_file(&port->dev, &dev_attr_uart_mode);
2752}
2753
2754static int edge_remove_sysfs_attrs(struct usb_serial_port *port)
2755{
2756 device_remove_file(&port->dev, &dev_attr_uart_mode);
2757 return 0;
2758}
2759
2760
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761/* Circular Buffer */
2762
2763/*
2764 * edge_buf_alloc
2765 *
2766 * Allocate a circular buffer and all associated memory.
2767 */
2768
2769static struct edge_buf *edge_buf_alloc(unsigned int size)
2770{
2771 struct edge_buf *eb;
2772
2773
2774 if (size == 0)
2775 return NULL;
2776
Robert P. J. Day5cbded52006-12-13 00:35:56 -08002777 eb = kmalloc(sizeof(struct edge_buf), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 if (eb == NULL)
2779 return NULL;
2780
2781 eb->buf_buf = kmalloc(size, GFP_KERNEL);
2782 if (eb->buf_buf == NULL) {
2783 kfree(eb);
2784 return NULL;
2785 }
2786
2787 eb->buf_size = size;
2788 eb->buf_get = eb->buf_put = eb->buf_buf;
2789
2790 return eb;
2791}
2792
2793
2794/*
2795 * edge_buf_free
2796 *
2797 * Free the buffer and all associated memory.
2798 */
2799
Adrian Bunk3d485862005-11-20 23:56:11 +01002800static void edge_buf_free(struct edge_buf *eb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801{
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07002802 if (eb) {
2803 kfree(eb->buf_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 kfree(eb);
2805 }
2806}
2807
2808
2809/*
2810 * edge_buf_clear
2811 *
2812 * Clear out all data in the circular buffer.
2813 */
2814
2815static void edge_buf_clear(struct edge_buf *eb)
2816{
Alan Cox2742fd82008-04-29 14:45:15 +01002817 if (eb != NULL)
2818 eb->buf_get = eb->buf_put;
2819 /* equivalent to a get of all data available */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820}
2821
2822
2823/*
2824 * edge_buf_data_avail
2825 *
2826 * Return the number of bytes of data available in the circular
2827 * buffer.
2828 */
2829
2830static unsigned int edge_buf_data_avail(struct edge_buf *eb)
2831{
Alan Cox2742fd82008-04-29 14:45:15 +01002832 if (eb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 return 0;
Alan Cox2742fd82008-04-29 14:45:15 +01002834 return ((eb->buf_size + eb->buf_put - eb->buf_get) % eb->buf_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835}
2836
2837
2838/*
2839 * edge_buf_space_avail
2840 *
2841 * Return the number of bytes of space available in the circular
2842 * buffer.
2843 */
2844
2845static unsigned int edge_buf_space_avail(struct edge_buf *eb)
2846{
Alan Cox2742fd82008-04-29 14:45:15 +01002847 if (eb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 return 0;
Alan Cox2742fd82008-04-29 14:45:15 +01002849 return ((eb->buf_size + eb->buf_get - eb->buf_put - 1) % eb->buf_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850}
2851
2852
2853/*
2854 * edge_buf_put
2855 *
2856 * Copy data data from a user buffer and put it into the circular buffer.
2857 * Restrict to the amount of space available.
2858 *
2859 * Return the number of bytes copied.
2860 */
2861
2862static unsigned int edge_buf_put(struct edge_buf *eb, const char *buf,
2863 unsigned int count)
2864{
2865 unsigned int len;
2866
2867
2868 if (eb == NULL)
2869 return 0;
2870
2871 len = edge_buf_space_avail(eb);
2872 if (count > len)
2873 count = len;
2874
2875 if (count == 0)
2876 return 0;
2877
2878 len = eb->buf_buf + eb->buf_size - eb->buf_put;
2879 if (count > len) {
2880 memcpy(eb->buf_put, buf, len);
2881 memcpy(eb->buf_buf, buf+len, count - len);
2882 eb->buf_put = eb->buf_buf + count - len;
2883 } else {
2884 memcpy(eb->buf_put, buf, count);
2885 if (count < len)
2886 eb->buf_put += count;
2887 else /* count == len */
2888 eb->buf_put = eb->buf_buf;
2889 }
2890
2891 return count;
2892}
2893
2894
2895/*
2896 * edge_buf_get
2897 *
2898 * Get data from the circular buffer and copy to the given buffer.
2899 * Restrict to the amount of data available.
2900 *
2901 * Return the number of bytes copied.
2902 */
2903
2904static unsigned int edge_buf_get(struct edge_buf *eb, char *buf,
2905 unsigned int count)
2906{
2907 unsigned int len;
2908
2909
2910 if (eb == NULL)
2911 return 0;
2912
2913 len = edge_buf_data_avail(eb);
2914 if (count > len)
2915 count = len;
2916
2917 if (count == 0)
2918 return 0;
2919
2920 len = eb->buf_buf + eb->buf_size - eb->buf_get;
2921 if (count > len) {
2922 memcpy(buf, eb->buf_get, len);
2923 memcpy(buf+len, eb->buf_buf, count - len);
2924 eb->buf_get = eb->buf_buf + count - len;
2925 } else {
2926 memcpy(buf, eb->buf_get, count);
2927 if (count < len)
2928 eb->buf_get += count;
2929 else /* count == len */
2930 eb->buf_get = eb->buf_buf;
2931 }
2932
2933 return count;
2934}
2935
2936
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -07002937static struct usb_serial_driver edgeport_1port_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07002938 .driver = {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07002939 .owner = THIS_MODULE,
2940 .name = "edgeport_ti_1",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07002941 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07002942 .description = "Edgeport TI 1 port adapter",
Johannes Hölzld9b1b782006-12-17 21:50:24 +01002943 .usb_driver = &io_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 .id_table = edgeport_1port_id_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 .num_ports = 1,
2946 .open = edge_open,
2947 .close = edge_close,
2948 .throttle = edge_throttle,
2949 .unthrottle = edge_unthrottle,
2950 .attach = edge_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04002951 .disconnect = edge_disconnect,
2952 .release = edge_release,
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002953 .port_probe = edge_create_sysfs_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 .ioctl = edge_ioctl,
2955 .set_termios = edge_set_termios,
2956 .tiocmget = edge_tiocmget,
2957 .tiocmset = edge_tiocmset,
2958 .write = edge_write,
2959 .write_room = edge_write_room,
2960 .chars_in_buffer = edge_chars_in_buffer,
2961 .break_ctl = edge_break,
2962 .read_int_callback = edge_interrupt_callback,
2963 .read_bulk_callback = edge_bulk_in_callback,
2964 .write_bulk_callback = edge_bulk_out_callback,
2965};
2966
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -07002967static struct usb_serial_driver edgeport_2port_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07002968 .driver = {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07002969 .owner = THIS_MODULE,
2970 .name = "edgeport_ti_2",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07002971 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07002972 .description = "Edgeport TI 2 port adapter",
Johannes Hölzld9b1b782006-12-17 21:50:24 +01002973 .usb_driver = &io_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 .id_table = edgeport_2port_id_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 .num_ports = 2,
2976 .open = edge_open,
2977 .close = edge_close,
2978 .throttle = edge_throttle,
2979 .unthrottle = edge_unthrottle,
2980 .attach = edge_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04002981 .disconnect = edge_disconnect,
2982 .release = edge_release,
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002983 .port_probe = edge_create_sysfs_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 .ioctl = edge_ioctl,
2985 .set_termios = edge_set_termios,
2986 .tiocmget = edge_tiocmget,
2987 .tiocmset = edge_tiocmset,
2988 .write = edge_write,
2989 .write_room = edge_write_room,
2990 .chars_in_buffer = edge_chars_in_buffer,
2991 .break_ctl = edge_break,
2992 .read_int_callback = edge_interrupt_callback,
2993 .read_bulk_callback = edge_bulk_in_callback,
2994 .write_bulk_callback = edge_bulk_out_callback,
2995};
2996
2997
2998static int __init edgeport_init(void)
2999{
3000 int retval;
3001 retval = usb_serial_register(&edgeport_1port_device);
3002 if (retval)
3003 goto failed_1port_device_register;
3004 retval = usb_serial_register(&edgeport_2port_device);
3005 if (retval)
3006 goto failed_2port_device_register;
3007 retval = usb_register(&io_driver);
Alan Cox2742fd82008-04-29 14:45:15 +01003008 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 goto failed_usb_register;
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -07003010 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
3011 DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 return 0;
3013failed_usb_register:
3014 usb_serial_deregister(&edgeport_2port_device);
3015failed_2port_device_register:
3016 usb_serial_deregister(&edgeport_1port_device);
3017failed_1port_device_register:
3018 return retval;
3019}
3020
Alan Cox2742fd82008-04-29 14:45:15 +01003021static void __exit edgeport_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022{
Alan Cox2742fd82008-04-29 14:45:15 +01003023 usb_deregister(&io_driver);
3024 usb_serial_deregister(&edgeport_1port_device);
3025 usb_serial_deregister(&edgeport_2port_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026}
3027
3028module_init(edgeport_init);
3029module_exit(edgeport_exit);
3030
3031/* Module information */
3032MODULE_AUTHOR(DRIVER_AUTHOR);
3033MODULE_DESCRIPTION(DRIVER_DESC);
3034MODULE_LICENSE("GPL");
Jaswinder Singhd12b2192008-07-04 23:06:09 +05303035MODULE_FIRMWARE("edgeport/down3.bin");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036
3037module_param(debug, bool, S_IRUGO | S_IWUSR);
3038MODULE_PARM_DESC(debug, "Debug enabled or not");
3039
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040module_param(closing_wait, int, S_IRUGO | S_IWUSR);
3041MODULE_PARM_DESC(closing_wait, "Maximum wait for data to drain, in .01 secs");
3042
3043module_param(ignore_cpu_rev, bool, S_IRUGO | S_IWUSR);
Alan Cox2742fd82008-04-29 14:45:15 +01003044MODULE_PARM_DESC(ignore_cpu_rev,
3045 "Ignore the cpu revision when connecting to a device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04003047module_param(default_uart_mode, int, S_IRUGO | S_IWUSR);
3048MODULE_PARM_DESC(default_uart_mode, "Default uart_mode, 0=RS232, ...");