blob: 1691f07548dbbdb232ca4cc2b77646cc0c4acb46 [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
59struct edgeport_uart_buf_desc {
Alan Cox2742fd82008-04-29 14:45:15 +010060 __u32 count; /* Number of bytes currently in buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061};
62
63/* different hardware types */
64#define HARDWARE_TYPE_930 0
65#define HARDWARE_TYPE_TIUMP 1
66
Alan Cox2742fd82008-04-29 14:45:15 +010067/* IOCTL_PRIVATE_TI_GET_MODE Definitions */
68#define TI_MODE_CONFIGURING 0 /* Device has not entered start device */
69#define TI_MODE_BOOT 1 /* Staying in boot mode */
70#define TI_MODE_DOWNLOAD 2 /* Made it to download mode */
71#define TI_MODE_TRANSITIONING 3 /* Currently in boot mode but
72 transitioning to download mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74/* read urb state */
75#define EDGE_READ_URB_RUNNING 0
76#define EDGE_READ_URB_STOPPING 1
77#define EDGE_READ_URB_STOPPED 2
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#define EDGE_CLOSING_WAIT 4000 /* in .01 sec */
80
81#define EDGE_OUT_BUF_SIZE 1024
82
83
84/* Product information read from the Edgeport */
Alan Cox2742fd82008-04-29 14:45:15 +010085struct product_info {
86 int TiMode; /* Current TI Mode */
87 __u8 hardware_type; /* Type of hardware */
Linus Torvalds1da177e2005-04-16 15:20:36 -070088} __attribute__((packed));
89
90/* circular buffer */
91struct edge_buf {
92 unsigned int buf_size;
93 char *buf_buf;
94 char *buf_get;
95 char *buf_put;
96};
97
98struct edgeport_port {
99 __u16 uart_base;
100 __u16 dma_address;
101 __u8 shadow_msr;
102 __u8 shadow_mcr;
103 __u8 shadow_lsr;
104 __u8 lsr_mask;
Martin Olsson19af5cd2009-04-23 11:37:37 +0200105 __u32 ump_read_timeout; /* Number of milliseconds the UMP will
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 wait without data before completing
107 a read short */
108 int baud_rate;
109 int close_pending;
110 int lsr_event;
111 struct edgeport_uart_buf_desc tx;
112 struct async_icount icount;
113 wait_queue_head_t delta_msr_wait; /* for handling sleeping while
114 waiting for msr change to
115 happen */
116 struct edgeport_serial *edge_serial;
117 struct usb_serial_port *port;
Alan Cox2742fd82008-04-29 14:45:15 +0100118 __u8 bUartMode; /* Port type, 0: RS232, etc. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 spinlock_t ep_lock;
120 int ep_read_urb_state;
121 int ep_write_urb_in_use;
122 struct edge_buf *ep_out_buf;
123};
124
125struct edgeport_serial {
126 struct product_info product_info;
Alan Cox2742fd82008-04-29 14:45:15 +0100127 u8 TI_I2C_Type; /* Type of I2C in UMP */
128 u8 TiReadI2C; /* Set to TRUE if we have read the
129 I2c in Boot Mode */
Matthias Kaehlcke241ca642007-12-04 16:15:40 +0100130 struct mutex es_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 int num_ports_open;
132 struct usb_serial *serial;
133};
134
135
136/* Devices that this driver supports */
137static struct usb_device_id edgeport_1port_id_table [] = {
138 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
139 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
140 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
141 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
142 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
143 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
144 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
145 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
146 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
147 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
148 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
149 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
150 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
151 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
152 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
153 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
154 { }
155};
156
157static struct usb_device_id edgeport_2port_id_table [] = {
158 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
159 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
160 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
161 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
162 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
163 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
164 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
165 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
166 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
167 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
168 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
169 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -0400170 /* The 4, 8 and 16 port devices show up as multiple 2 port devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -0400172 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
173 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
174 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
175 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 { }
177};
178
179/* Devices that this driver supports */
180static struct usb_device_id id_table_combined [] = {
181 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
182 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
183 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
184 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
185 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
186 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
187 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
188 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
189 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
190 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
191 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
192 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
193 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
194 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
195 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
196 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
197 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
198 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
199 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
200 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
201 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
202 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
203 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
204 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
205 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
206 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
207 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
208 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
209 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -0400210 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
211 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
212 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
213 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 { }
215};
216
Alan Cox2742fd82008-04-29 14:45:15 +0100217MODULE_DEVICE_TABLE(usb, id_table_combined);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219static struct usb_driver io_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 .name = "io_ti",
221 .probe = usb_serial_probe,
222 .disconnect = usb_serial_disconnect,
223 .id_table = id_table_combined,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800224 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225};
226
227
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530228static unsigned char OperationalMajorVersion;
229static unsigned char OperationalMinorVersion;
230static unsigned short OperationalBuildNumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232static int debug;
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234static int closing_wait = EDGE_CLOSING_WAIT;
Alan Cox2742fd82008-04-29 14:45:15 +0100235static int ignore_cpu_rev;
236static int default_uart_mode; /* RS232 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Alan Cox2742fd82008-04-29 14:45:15 +0100238static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
239 unsigned char *data, int length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241static void stop_read(struct edgeport_port *edge_port);
242static int restart_read(struct edgeport_port *edge_port);
243
Alan Cox95da3102008-07-22 11:09:07 +0100244static void edge_set_termios(struct tty_struct *tty,
245 struct usb_serial_port *port, struct ktermios *old_termios);
246static void edge_send(struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -0400248/* sysfs attributes */
249static int edge_create_sysfs_attrs(struct usb_serial_port *port);
250static int edge_remove_sysfs_attrs(struct usb_serial_port *port);
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252/* circular buffer */
253static struct edge_buf *edge_buf_alloc(unsigned int size);
254static void edge_buf_free(struct edge_buf *eb);
255static void edge_buf_clear(struct edge_buf *eb);
256static unsigned int edge_buf_data_avail(struct edge_buf *eb);
257static unsigned int edge_buf_space_avail(struct edge_buf *eb);
258static unsigned int edge_buf_put(struct edge_buf *eb, const char *buf,
259 unsigned int count);
260static unsigned int edge_buf_get(struct edge_buf *eb, char *buf,
261 unsigned int count);
262
263
Alan Cox2742fd82008-04-29 14:45:15 +0100264static int ti_vread_sync(struct usb_device *dev, __u8 request,
265 __u16 value, __u16 index, u8 *data, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
267 int status;
268
Alan Cox2742fd82008-04-29 14:45:15 +0100269 status = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request,
270 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
271 value, index, data, size, 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 if (status < 0)
273 return status;
274 if (status != size) {
Alan Cox2742fd82008-04-29 14:45:15 +0100275 dbg("%s - wanted to write %d, but only wrote %d",
276 __func__, size, status);
277 return -ECOMM;
278 }
279 return 0;
280}
281
282static int ti_vsend_sync(struct usb_device *dev, __u8 request,
283 __u16 value, __u16 index, u8 *data, int size)
284{
285 int status;
286
287 status = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
288 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
289 value, index, data, size, 1000);
290 if (status < 0)
291 return status;
292 if (status != size) {
293 dbg("%s - wanted to write %d, but only wrote %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800294 __func__, size, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return -ECOMM;
296 }
297 return 0;
298}
299
Alan Cox2742fd82008-04-29 14:45:15 +0100300static int send_cmd(struct usb_device *dev, __u8 command,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 __u8 moduleid, __u16 value, u8 *data,
302 int size)
303{
Alan Cox2742fd82008-04-29 14:45:15 +0100304 return ti_vsend_sync(dev, command, value, moduleid, data, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
307/* clear tx/rx buffers and fifo in TI UMP */
Alan Cox2742fd82008-04-29 14:45:15 +0100308static int purge_port(struct usb_serial_port *port, __u16 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
310 int port_number = port->number - port->serial->minor;
311
Alan Cox2742fd82008-04-29 14:45:15 +0100312 dbg("%s - port %d, mask %x", __func__, port_number, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Alan Cox2742fd82008-04-29 14:45:15 +0100314 return send_cmd(port->serial->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 UMPC_PURGE_PORT,
316 (__u8)(UMPM_UART1_PORT + port_number),
317 mask,
318 NULL,
319 0);
320}
321
322/**
Alan Cox2742fd82008-04-29 14:45:15 +0100323 * read_download_mem - Read edgeport memory from TI chip
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 * @dev: usb device pointer
325 * @start_address: Device CPU address at which to read
326 * @length: Length of above data
327 * @address_type: Can read both XDATA and I2C
328 * @buffer: pointer to input data buffer
329 */
Alan Cox2742fd82008-04-29 14:45:15 +0100330static int read_download_mem(struct usb_device *dev, int start_address,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 int length, __u8 address_type, __u8 *buffer)
332{
333 int status = 0;
334 __u8 read_length;
335 __be16 be_start_address;
Alan Cox2742fd82008-04-29 14:45:15 +0100336
337 dbg("%s - @ %x for %d", __func__, start_address, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 /* Read in blocks of 64 bytes
340 * (TI firmware can't handle more than 64 byte reads)
341 */
342 while (length) {
343 if (length > 64)
Alan Cox2742fd82008-04-29 14:45:15 +0100344 read_length = 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 else
346 read_length = (__u8)length;
347
348 if (read_length > 1) {
Alan Cox2742fd82008-04-29 14:45:15 +0100349 dbg("%s - @ %x for %d", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 start_address, read_length);
351 }
Alan Cox2742fd82008-04-29 14:45:15 +0100352 be_start_address = cpu_to_be16(start_address);
353 status = ti_vread_sync(dev, UMPC_MEMORY_READ,
354 (__u16)address_type,
355 (__force __u16)be_start_address,
356 buffer, read_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +0100359 dbg("%s - ERROR %x", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return status;
361 }
362
Alan Cox2742fd82008-04-29 14:45:15 +0100363 if (read_length > 1)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800364 usb_serial_debug_data(debug, &dev->dev, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 read_length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 /* Update pointers/length */
368 start_address += read_length;
369 buffer += read_length;
370 length -= read_length;
371 }
Alan Cox2742fd82008-04-29 14:45:15 +0100372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return status;
374}
375
Alan Cox2742fd82008-04-29 14:45:15 +0100376static int read_ram(struct usb_device *dev, int start_address,
377 int length, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Alan Cox2742fd82008-04-29 14:45:15 +0100379 return read_download_mem(dev, start_address, length,
380 DTK_ADDR_SPACE_XDATA, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}
382
383/* Read edgeport memory to a given block */
Alan Cox2742fd82008-04-29 14:45:15 +0100384static int read_boot_mem(struct edgeport_serial *serial,
385 int start_address, int length, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
387 int status = 0;
388 int i;
389
Alan Cox2742fd82008-04-29 14:45:15 +0100390 for (i = 0; i < length; i++) {
391 status = ti_vread_sync(serial->serial->dev,
392 UMPC_MEMORY_READ, serial->TI_I2C_Type,
393 (__u16)(start_address+i), &buffer[i], 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +0100395 dbg("%s - ERROR %x", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return status;
397 }
398 }
399
Alan Cox2742fd82008-04-29 14:45:15 +0100400 dbg("%s - start_address = %x, length = %d",
401 __func__, start_address, length);
402 usb_serial_debug_data(debug, &serial->serial->dev->dev,
403 __func__, length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 serial->TiReadI2C = 1;
406
407 return status;
408}
409
410/* Write given block to TI EPROM memory */
Alan Cox2742fd82008-04-29 14:45:15 +0100411static int write_boot_mem(struct edgeport_serial *serial,
412 int start_address, int length, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
414 int status = 0;
415 int i;
Johan Hovolde9305d22009-12-28 23:01:50 +0100416 u8 *temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 /* Must do a read before write */
419 if (!serial->TiReadI2C) {
Johan Hovolde9305d22009-12-28 23:01:50 +0100420 temp = kmalloc(1, GFP_KERNEL);
421 if (!temp) {
422 dev_err(&serial->serial->dev->dev,
423 "%s - out of memory\n", __func__);
424 return -ENOMEM;
425 }
426 status = read_boot_mem(serial, 0, 1, temp);
427 kfree(temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (status)
429 return status;
430 }
431
Alan Cox2742fd82008-04-29 14:45:15 +0100432 for (i = 0; i < length; ++i) {
433 status = ti_vsend_sync(serial->serial->dev,
434 UMPC_MEMORY_WRITE, buffer[i],
435 (__u16)(i + start_address), NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (status)
437 return status;
438 }
439
Alan Cox2742fd82008-04-29 14:45:15 +0100440 dbg("%s - start_sddr = %x, length = %d",
441 __func__, start_address, length);
442 usb_serial_debug_data(debug, &serial->serial->dev->dev,
443 __func__, length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 return status;
446}
447
448
449/* Write edgeport I2C memory to TI chip */
Alan Cox2742fd82008-04-29 14:45:15 +0100450static int write_i2c_mem(struct edgeport_serial *serial,
451 int start_address, int length, __u8 address_type, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
453 int status = 0;
454 int write_length;
455 __be16 be_start_address;
456
457 /* We can only send a maximum of 1 aligned byte page at a time */
Alan Cox2742fd82008-04-29 14:45:15 +0100458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 /* calulate the number of bytes left in the first page */
Alan Cox2742fd82008-04-29 14:45:15 +0100460 write_length = EPROM_PAGE_SIZE -
461 (start_address & (EPROM_PAGE_SIZE - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 if (write_length > length)
464 write_length = length;
465
Alan Cox2742fd82008-04-29 14:45:15 +0100466 dbg("%s - BytesInFirstPage Addr = %x, length = %d",
467 __func__, start_address, write_length);
468 usb_serial_debug_data(debug, &serial->serial->dev->dev,
469 __func__, write_length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 /* Write first page */
Alan Cox2742fd82008-04-29 14:45:15 +0100472 be_start_address = cpu_to_be16(start_address);
473 status = ti_vsend_sync(serial->serial->dev,
474 UMPC_MEMORY_WRITE, (__u16)address_type,
475 (__force __u16)be_start_address,
476 buffer, write_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +0100478 dbg("%s - ERROR %d", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 return status;
480 }
481
482 length -= write_length;
483 start_address += write_length;
484 buffer += write_length;
485
Alan Cox2742fd82008-04-29 14:45:15 +0100486 /* We should be aligned now -- can write
487 max page size bytes at a time */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 while (length) {
489 if (length > EPROM_PAGE_SIZE)
490 write_length = EPROM_PAGE_SIZE;
491 else
492 write_length = length;
493
Alan Cox2742fd82008-04-29 14:45:15 +0100494 dbg("%s - Page Write Addr = %x, length = %d",
495 __func__, start_address, write_length);
496 usb_serial_debug_data(debug, &serial->serial->dev->dev,
497 __func__, write_length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 /* Write next page */
Alan Cox2742fd82008-04-29 14:45:15 +0100500 be_start_address = cpu_to_be16(start_address);
501 status = ti_vsend_sync(serial->serial->dev, UMPC_MEMORY_WRITE,
502 (__u16)address_type,
503 (__force __u16)be_start_address,
504 buffer, write_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +0100506 dev_err(&serial->serial->dev->dev, "%s - ERROR %d\n",
507 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 return status;
509 }
Alan Cox2742fd82008-04-29 14:45:15 +0100510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 length -= write_length;
512 start_address += write_length;
513 buffer += write_length;
514 }
515 return status;
516}
517
518/* Examine the UMP DMA registers and LSR
Alan Cox2742fd82008-04-29 14:45:15 +0100519 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 * Check the MSBit of the X and Y DMA byte count registers.
521 * A zero in this bit indicates that the TX DMA buffers are empty
522 * then check the TX Empty bit in the UART.
523 */
Alan Cox2742fd82008-04-29 14:45:15 +0100524static int tx_active(struct edgeport_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
526 int status;
527 struct out_endpoint_desc_block *oedb;
528 __u8 *lsr;
529 int bytes_left = 0;
530
Alan Cox2742fd82008-04-29 14:45:15 +0100531 oedb = kmalloc(sizeof(*oedb), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (!oedb) {
Alan Cox2742fd82008-04-29 14:45:15 +0100533 dev_err(&port->port->dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return -ENOMEM;
535 }
536
Alan Cox2742fd82008-04-29 14:45:15 +0100537 lsr = kmalloc(1, GFP_KERNEL); /* Sigh, that's right, just one byte,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 as not all platforms can do DMA
539 from stack */
540 if (!lsr) {
541 kfree(oedb);
542 return -ENOMEM;
543 }
544 /* Read the DMA Count Registers */
Alan Cox2742fd82008-04-29 14:45:15 +0100545 status = read_ram(port->port->serial->dev, port->dma_address,
546 sizeof(*oedb), (void *)oedb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (status)
548 goto exit_is_tx_active;
549
Alan Cox2742fd82008-04-29 14:45:15 +0100550 dbg("%s - XByteCount 0x%X", __func__, oedb->XByteCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 /* and the LSR */
Alan Cox2742fd82008-04-29 14:45:15 +0100553 status = read_ram(port->port->serial->dev,
554 port->uart_base + UMPMEM_OFFS_UART_LSR, 1, lsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 if (status)
557 goto exit_is_tx_active;
Alan Cox2742fd82008-04-29 14:45:15 +0100558 dbg("%s - LSR = 0x%X", __func__, *lsr);
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 /* If either buffer has data or we are transmitting then return TRUE */
Alan Cox2742fd82008-04-29 14:45:15 +0100561 if ((oedb->XByteCount & 0x80) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 bytes_left += 64;
563
Alan Cox2742fd82008-04-29 14:45:15 +0100564 if ((*lsr & UMP_UART_LSR_TX_MASK) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 bytes_left += 1;
566
567 /* We return Not Active if we get any kind of error */
568exit_is_tx_active:
Alan Cox2742fd82008-04-29 14:45:15 +0100569 dbg("%s - return %d", __func__, bytes_left);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 kfree(lsr);
572 kfree(oedb);
573 return bytes_left;
574}
575
Alan Cox2742fd82008-04-29 14:45:15 +0100576static void chase_port(struct edgeport_port *port, unsigned long timeout,
577 int flush)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
579 int baud_rate;
Alan Cox4a90f092008-10-13 10:39:46 +0100580 struct tty_struct *tty = tty_port_tty_get(&port->port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 wait_queue_t wait;
582 unsigned long flags;
583
584 if (!timeout)
Alan Cox2742fd82008-04-29 14:45:15 +0100585 timeout = (HZ * EDGE_CLOSING_WAIT)/100;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 /* wait for data to drain from the buffer */
588 spin_lock_irqsave(&port->ep_lock, flags);
589 init_waitqueue_entry(&wait, current);
590 add_wait_queue(&tty->write_wait, &wait);
591 for (;;) {
592 set_current_state(TASK_INTERRUPTIBLE);
593 if (edge_buf_data_avail(port->ep_out_buf) == 0
594 || timeout == 0 || signal_pending(current)
Alan Cox2742fd82008-04-29 14:45:15 +0100595 || !usb_get_intfdata(port->port->serial->interface))
596 /* disconnect */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 break;
598 spin_unlock_irqrestore(&port->ep_lock, flags);
599 timeout = schedule_timeout(timeout);
600 spin_lock_irqsave(&port->ep_lock, flags);
601 }
602 set_current_state(TASK_RUNNING);
603 remove_wait_queue(&tty->write_wait, &wait);
604 if (flush)
605 edge_buf_clear(port->ep_out_buf);
606 spin_unlock_irqrestore(&port->ep_lock, flags);
Alan Cox4a90f092008-10-13 10:39:46 +0100607 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 /* wait for data to drain from the device */
610 timeout += jiffies;
611 while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
Alan Cox2742fd82008-04-29 14:45:15 +0100612 && usb_get_intfdata(port->port->serial->interface)) {
613 /* not disconnected */
614 if (!tx_active(port))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 break;
616 msleep(10);
617 }
618
619 /* disconnected */
620 if (!usb_get_intfdata(port->port->serial->interface))
621 return;
622
623 /* wait one more character time, based on baud rate */
Alan Cox2742fd82008-04-29 14:45:15 +0100624 /* (tx_active doesn't seem to wait for the last byte) */
625 baud_rate = port->baud_rate;
626 if (baud_rate == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 baud_rate = 50;
Julia Lawalldfa5ec72008-03-04 15:25:11 -0800628 msleep(max(1, DIV_ROUND_UP(10000, baud_rate)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
Alan Cox2742fd82008-04-29 14:45:15 +0100631static int choose_config(struct usb_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Alan Cox2742fd82008-04-29 14:45:15 +0100633 /*
634 * There may be multiple configurations on this device, in which case
635 * we would need to read and parse all of them to find out which one
636 * we want. However, we just support one config at this point,
637 * configuration # 1, which is Config Descriptor 0.
638 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Alan Cox2742fd82008-04-29 14:45:15 +0100640 dbg("%s - Number of Interfaces = %d",
641 __func__, dev->config->desc.bNumInterfaces);
642 dbg("%s - MAX Power = %d",
643 __func__, dev->config->desc.bMaxPower * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 if (dev->config->desc.bNumInterfaces != 1) {
Alan Cox2742fd82008-04-29 14:45:15 +0100646 dev_err(&dev->dev, "%s - bNumInterfaces is not 1, ERROR!\n",
647 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return -ENODEV;
649 }
650
651 return 0;
652}
653
Alan Cox2742fd82008-04-29 14:45:15 +0100654static int read_rom(struct edgeport_serial *serial,
655 int start_address, int length, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
657 int status;
658
659 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
Alan Cox2742fd82008-04-29 14:45:15 +0100660 status = read_download_mem(serial->serial->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 start_address,
662 length,
663 serial->TI_I2C_Type,
664 buffer);
665 } else {
Alan Cox2742fd82008-04-29 14:45:15 +0100666 status = read_boot_mem(serial, start_address, length,
667 buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return status;
670}
671
Alan Cox2742fd82008-04-29 14:45:15 +0100672static int write_rom(struct edgeport_serial *serial, int start_address,
673 int length, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
675 if (serial->product_info.TiMode == TI_MODE_BOOT)
Alan Cox2742fd82008-04-29 14:45:15 +0100676 return write_boot_mem(serial, start_address, length,
677 buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD)
Alan Cox2742fd82008-04-29 14:45:15 +0100680 return write_i2c_mem(serial, start_address, length,
681 serial->TI_I2C_Type, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return -EINVAL;
683}
684
685
686
687/* Read a descriptor header from I2C based on type */
Alan Cox2742fd82008-04-29 14:45:15 +0100688static int get_descriptor_addr(struct edgeport_serial *serial,
689 int desc_type, struct ti_i2c_desc *rom_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
691 int start_address;
692 int status;
693
694 /* Search for requested descriptor in I2C */
695 start_address = 2;
696 do {
Alan Cox2742fd82008-04-29 14:45:15 +0100697 status = read_rom(serial,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 start_address,
699 sizeof(struct ti_i2c_desc),
Alan Cox2742fd82008-04-29 14:45:15 +0100700 (__u8 *)rom_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (status)
702 return 0;
703
704 if (rom_desc->Type == desc_type)
705 return start_address;
706
Alan Cox2742fd82008-04-29 14:45:15 +0100707 start_address = start_address + sizeof(struct ti_i2c_desc)
708 + rom_desc->Size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 } while ((start_address < TI_MAX_I2C_SIZE) && rom_desc->Type);
Alan Cox2742fd82008-04-29 14:45:15 +0100711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 return 0;
713}
714
715/* Validate descriptor checksum */
Alan Cox2742fd82008-04-29 14:45:15 +0100716static int valid_csum(struct ti_i2c_desc *rom_desc, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
718 __u16 i;
719 __u8 cs = 0;
720
Alan Cox2742fd82008-04-29 14:45:15 +0100721 for (i = 0; i < rom_desc->Size; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 cs = (__u8)(cs + buffer[i]);
Alan Cox2742fd82008-04-29 14:45:15 +0100723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 if (cs != rom_desc->CheckSum) {
Alan Cox2742fd82008-04-29 14:45:15 +0100725 dbg("%s - Mismatch %x - %x", __func__, rom_desc->CheckSum, cs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return -EINVAL;
727 }
728 return 0;
729}
730
731/* Make sure that the I2C image is good */
Alan Cox2742fd82008-04-29 14:45:15 +0100732static int check_i2c_image(struct edgeport_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
734 struct device *dev = &serial->serial->dev->dev;
735 int status = 0;
736 struct ti_i2c_desc *rom_desc;
737 int start_address = 2;
738 __u8 *buffer;
739 __u16 ttype;
740
Alan Cox2742fd82008-04-29 14:45:15 +0100741 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 if (!rom_desc) {
Alan Cox2742fd82008-04-29 14:45:15 +0100743 dev_err(dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return -ENOMEM;
745 }
Alan Cox2742fd82008-04-29 14:45:15 +0100746 buffer = kmalloc(TI_MAX_I2C_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (!buffer) {
Alan Cox2742fd82008-04-29 14:45:15 +0100748 dev_err(dev, "%s - out of memory when allocating buffer\n",
749 __func__);
750 kfree(rom_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 return -ENOMEM;
752 }
753
Alan Cox2742fd82008-04-29 14:45:15 +0100754 /* Read the first byte (Signature0) must be 0x52 or 0x10 */
755 status = read_rom(serial, 0, 1, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 if (status)
Alan Cox2742fd82008-04-29 14:45:15 +0100757 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 if (*buffer != UMP5152 && *buffer != UMP3410) {
Alan Cox2742fd82008-04-29 14:45:15 +0100760 dev_err(dev, "%s - invalid buffer signature\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 status = -ENODEV;
Alan Cox2742fd82008-04-29 14:45:15 +0100762 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
764
765 do {
Alan Cox2742fd82008-04-29 14:45:15 +0100766 /* Validate the I2C */
767 status = read_rom(serial,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 start_address,
769 sizeof(struct ti_i2c_desc),
770 (__u8 *)rom_desc);
771 if (status)
772 break;
773
Alan Cox2742fd82008-04-29 14:45:15 +0100774 if ((start_address + sizeof(struct ti_i2c_desc) +
775 rom_desc->Size) > TI_MAX_I2C_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 status = -ENODEV;
Alan Cox2742fd82008-04-29 14:45:15 +0100777 dbg("%s - structure too big, erroring out.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 break;
779 }
780
Alan Cox2742fd82008-04-29 14:45:15 +0100781 dbg("%s Type = 0x%x", __func__, rom_desc->Type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Alan Cox2742fd82008-04-29 14:45:15 +0100783 /* Skip type 2 record */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 ttype = rom_desc->Type & 0x0f;
Alan Cox2742fd82008-04-29 14:45:15 +0100785 if (ttype != I2C_DESC_TYPE_FIRMWARE_BASIC
786 && ttype != I2C_DESC_TYPE_FIRMWARE_AUTO) {
787 /* Read the descriptor data */
788 status = read_rom(serial, start_address +
789 sizeof(struct ti_i2c_desc),
790 rom_desc->Size, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if (status)
792 break;
793
Alan Cox2742fd82008-04-29 14:45:15 +0100794 status = valid_csum(rom_desc, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (status)
796 break;
797 }
Alan Cox2742fd82008-04-29 14:45:15 +0100798 start_address = start_address + sizeof(struct ti_i2c_desc) +
799 rom_desc->Size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Alan Cox2742fd82008-04-29 14:45:15 +0100801 } while ((rom_desc->Type != I2C_DESC_TYPE_ION) &&
802 (start_address < TI_MAX_I2C_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Alan Cox2742fd82008-04-29 14:45:15 +0100804 if ((rom_desc->Type != I2C_DESC_TYPE_ION) ||
805 (start_address > TI_MAX_I2C_SIZE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 status = -ENODEV;
807
Alan Cox2742fd82008-04-29 14:45:15 +0100808out:
809 kfree(buffer);
810 kfree(rom_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 return status;
812}
813
Alan Cox2742fd82008-04-29 14:45:15 +0100814static int get_manuf_info(struct edgeport_serial *serial, __u8 *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
816 int status;
817 int start_address;
818 struct ti_i2c_desc *rom_desc;
819 struct edge_ti_manuf_descriptor *desc;
820
Alan Cox2742fd82008-04-29 14:45:15 +0100821 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 if (!rom_desc) {
Alan Cox2742fd82008-04-29 14:45:15 +0100823 dev_err(&serial->serial->dev->dev, "%s - out of memory\n",
824 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return -ENOMEM;
826 }
Alan Cox2742fd82008-04-29 14:45:15 +0100827 start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_ION,
828 rom_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 if (!start_address) {
Alan Cox2742fd82008-04-29 14:45:15 +0100831 dbg("%s - Edge Descriptor not found in I2C", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 status = -ENODEV;
833 goto exit;
834 }
835
Alan Cox2742fd82008-04-29 14:45:15 +0100836 /* Read the descriptor data */
837 status = read_rom(serial, start_address+sizeof(struct ti_i2c_desc),
838 rom_desc->Size, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 if (status)
840 goto exit;
Alan Cox2742fd82008-04-29 14:45:15 +0100841
842 status = valid_csum(rom_desc, buffer);
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 desc = (struct edge_ti_manuf_descriptor *)buffer;
Alan Cox2742fd82008-04-29 14:45:15 +0100845 dbg("%s - IonConfig 0x%x", __func__, desc->IonConfig);
846 dbg("%s - Version %d", __func__, desc->Version);
847 dbg("%s - Cpu/Board 0x%x", __func__, desc->CpuRev_BoardRev);
848 dbg("%s - NumPorts %d", __func__, desc->NumPorts);
849 dbg("%s - NumVirtualPorts %d", __func__, desc->NumVirtualPorts);
850 dbg("%s - TotalPorts %d", __func__, desc->TotalPorts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852exit:
Alan Cox2742fd82008-04-29 14:45:15 +0100853 kfree(rom_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 return status;
855}
856
857/* Build firmware header used for firmware update */
Alan Cox2742fd82008-04-29 14:45:15 +0100858static int build_i2c_fw_hdr(__u8 *header, struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
860 __u8 *buffer;
861 int buffer_size;
862 int i;
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530863 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 __u8 cs = 0;
865 struct ti_i2c_desc *i2c_header;
866 struct ti_i2c_image_header *img_header;
867 struct ti_i2c_firmware_rec *firmware_rec;
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530868 const struct firmware *fw;
869 const char *fw_name = "edgeport/down3.bin";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Alan Cox2742fd82008-04-29 14:45:15 +0100871 /* In order to update the I2C firmware we must change the type 2 record
872 * to type 0xF2. This will force the UMP to come up in Boot Mode.
873 * Then while in boot mode, the driver will download the latest
874 * firmware (padded to 15.5k) into the UMP ram. And finally when the
875 * device comes back up in download mode the driver will cause the new
876 * firmware to be copied from the UMP Ram to I2C and the firmware will
877 * update the record type from 0xf2 to 0x02.
878 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Alan Cox2742fd82008-04-29 14:45:15 +0100880 /* Allocate a 15.5k buffer + 2 bytes for version number
881 * (Firmware Record) */
882 buffer_size = (((1024 * 16) - 512 ) +
883 sizeof(struct ti_i2c_firmware_rec));
884
885 buffer = kmalloc(buffer_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if (!buffer) {
Alan Cox2742fd82008-04-29 14:45:15 +0100887 dev_err(dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return -ENOMEM;
889 }
Alan Cox2742fd82008-04-29 14:45:15 +0100890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 // Set entire image of 0xffs
Alan Cox2742fd82008-04-29 14:45:15 +0100892 memset(buffer, 0xff, buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530894 err = request_firmware(&fw, fw_name, dev);
895 if (err) {
896 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
897 fw_name, err);
898 kfree(buffer);
899 return err;
900 }
901
902 /* Save Download Version Number */
903 OperationalMajorVersion = fw->data[0];
904 OperationalMinorVersion = fw->data[1];
905 OperationalBuildNumber = fw->data[2] | (fw->data[3] << 8);
906
Alan Cox2742fd82008-04-29 14:45:15 +0100907 /* Copy version number into firmware record */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 firmware_rec = (struct ti_i2c_firmware_rec *)buffer;
909
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530910 firmware_rec->Ver_Major = OperationalMajorVersion;
911 firmware_rec->Ver_Minor = OperationalMinorVersion;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Alan Cox2742fd82008-04-29 14:45:15 +0100913 /* Pointer to fw_down memory image */
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530914 img_header = (struct ti_i2c_image_header *)&fw->data[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Alan Cox2742fd82008-04-29 14:45:15 +0100916 memcpy(buffer + sizeof(struct ti_i2c_firmware_rec),
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530917 &fw->data[4 + sizeof(struct ti_i2c_image_header)],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 le16_to_cpu(img_header->Length));
919
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530920 release_firmware(fw);
921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 for (i=0; i < buffer_size; i++) {
923 cs = (__u8)(cs + buffer[i]);
924 }
925
Alan Cox2742fd82008-04-29 14:45:15 +0100926 kfree(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Alan Cox2742fd82008-04-29 14:45:15 +0100928 /* Build new header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 i2c_header = (struct ti_i2c_desc *)header;
930 firmware_rec = (struct ti_i2c_firmware_rec*)i2c_header->Data;
Alan Cox2742fd82008-04-29 14:45:15 +0100931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 i2c_header->Type = I2C_DESC_TYPE_FIRMWARE_BLANK;
933 i2c_header->Size = (__u16)buffer_size;
934 i2c_header->CheckSum = cs;
Jaswinder Singhd12b2192008-07-04 23:06:09 +0530935 firmware_rec->Ver_Major = OperationalMajorVersion;
936 firmware_rec->Ver_Minor = OperationalMinorVersion;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
938 return 0;
939}
940
941/* Try to figure out what type of I2c we have */
Alan Cox2742fd82008-04-29 14:45:15 +0100942static int i2c_type_bootmode(struct edgeport_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
944 int status;
Johan Hovolde9305d22009-12-28 23:01:50 +0100945 u8 *data;
946
947 data = kmalloc(1, GFP_KERNEL);
948 if (!data) {
949 dev_err(&serial->serial->dev->dev,
950 "%s - out of memory\n", __func__);
951 return -ENOMEM;
952 }
Alan Cox2742fd82008-04-29 14:45:15 +0100953
954 /* Try to read type 2 */
955 status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
Johan Hovolde9305d22009-12-28 23:01:50 +0100956 DTK_ADDR_SPACE_I2C_TYPE_II, 0, data, 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 if (status)
Alan Cox2742fd82008-04-29 14:45:15 +0100958 dbg("%s - read 2 status error = %d", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 else
Johan Hovolde9305d22009-12-28 23:01:50 +0100960 dbg("%s - read 2 data = 0x%x", __func__, *data);
961 if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
Alan Cox2742fd82008-04-29 14:45:15 +0100962 dbg("%s - ROM_TYPE_II", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
Johan Hovolde9305d22009-12-28 23:01:50 +0100964 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 }
966
Alan Cox2742fd82008-04-29 14:45:15 +0100967 /* Try to read type 3 */
968 status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
Johan Hovolde9305d22009-12-28 23:01:50 +0100969 DTK_ADDR_SPACE_I2C_TYPE_III, 0, data, 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 if (status)
Alan Cox2742fd82008-04-29 14:45:15 +0100971 dbg("%s - read 3 status error = %d", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 else
Johan Hovolde9305d22009-12-28 23:01:50 +0100973 dbg("%s - read 2 data = 0x%x", __func__, *data);
974 if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
Alan Cox2742fd82008-04-29 14:45:15 +0100975 dbg("%s - ROM_TYPE_III", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_III;
Johan Hovolde9305d22009-12-28 23:01:50 +0100977 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
979
Alan Cox2742fd82008-04-29 14:45:15 +0100980 dbg("%s - Unknown", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
Johan Hovolde9305d22009-12-28 23:01:50 +0100982 status = -ENODEV;
983out:
984 kfree(data);
985 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986}
987
Alan Cox2742fd82008-04-29 14:45:15 +0100988static int bulk_xfer(struct usb_serial *serial, void *buffer,
989 int length, int *num_sent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
991 int status;
992
Alan Cox2742fd82008-04-29 14:45:15 +0100993 status = usb_bulk_msg(serial->dev,
994 usb_sndbulkpipe(serial->dev,
995 serial->port[0]->bulk_out_endpointAddress),
996 buffer, length, num_sent, 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 return status;
998}
999
1000/* Download given firmware image to the device (IN BOOT MODE) */
Alan Cox2742fd82008-04-29 14:45:15 +01001001static int download_code(struct edgeport_serial *serial, __u8 *image,
1002 int image_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
1004 int status = 0;
1005 int pos;
1006 int transfer;
1007 int done;
1008
Alan Cox2742fd82008-04-29 14:45:15 +01001009 /* Transfer firmware image */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 for (pos = 0; pos < image_length; ) {
Alan Cox2742fd82008-04-29 14:45:15 +01001011 /* Read the next buffer from file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 transfer = image_length - pos;
1013 if (transfer > EDGE_FW_BULK_MAX_PACKET_SIZE)
1014 transfer = EDGE_FW_BULK_MAX_PACKET_SIZE;
1015
Alan Cox2742fd82008-04-29 14:45:15 +01001016 /* Transfer data */
1017 status = bulk_xfer(serial->serial, &image[pos],
1018 transfer, &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 if (status)
1020 break;
Alan Cox2742fd82008-04-29 14:45:15 +01001021 /* Advance buffer pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 pos += done;
1023 }
1024
1025 return status;
1026}
1027
Alan Cox2742fd82008-04-29 14:45:15 +01001028/* FIXME!!! */
1029static int config_boot_dev(struct usb_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
1031 return 0;
1032}
1033
Alan Cox2742fd82008-04-29 14:45:15 +01001034static int ti_cpu_rev(struct edge_ti_manuf_descriptor *desc)
1035{
1036 return TI_GET_CPU_REVISION(desc->CpuRev_BoardRev);
1037}
1038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039/**
1040 * DownloadTIFirmware - Download run-time operating firmware to the TI5052
Alan Cox2742fd82008-04-29 14:45:15 +01001041 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 * This routine downloads the main operating code into the TI5052, using the
1043 * boot code already burned into E2PROM or ROM.
1044 */
Alan Cox2742fd82008-04-29 14:45:15 +01001045static int download_fw(struct edgeport_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
1047 struct device *dev = &serial->serial->dev->dev;
1048 int status = 0;
1049 int start_address;
1050 struct edge_ti_manuf_descriptor *ti_manuf_desc;
1051 struct usb_interface_descriptor *interface;
1052 int download_cur_ver;
1053 int download_new_ver;
1054
1055 /* This routine is entered by both the BOOT mode and the Download mode
1056 * We can determine which code is running by the reading the config
1057 * descriptor and if we have only one bulk pipe it is in boot mode
1058 */
1059 serial->product_info.hardware_type = HARDWARE_TYPE_TIUMP;
1060
1061 /* Default to type 2 i2c */
1062 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
1063
Alan Cox2742fd82008-04-29 14:45:15 +01001064 status = choose_config(serial->serial->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 if (status)
1066 return status;
1067
1068 interface = &serial->serial->interface->cur_altsetting->desc;
1069 if (!interface) {
Alan Cox2742fd82008-04-29 14:45:15 +01001070 dev_err(dev, "%s - no interface set, error!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 return -ENODEV;
1072 }
1073
Alan Cox2742fd82008-04-29 14:45:15 +01001074 /*
1075 * Setup initial mode -- the default mode 0 is TI_MODE_CONFIGURING
1076 * if we have more than one endpoint we are definitely in download
1077 * mode
1078 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 if (interface->bNumEndpoints > 1)
1080 serial->product_info.TiMode = TI_MODE_DOWNLOAD;
1081 else
Alan Cox2742fd82008-04-29 14:45:15 +01001082 /* Otherwise we will remain in configuring mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 serial->product_info.TiMode = TI_MODE_CONFIGURING;
1084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 /********************************************************************/
1086 /* Download Mode */
1087 /********************************************************************/
1088 if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
1089 struct ti_i2c_desc *rom_desc;
1090
Andrew Morton9544e832008-02-04 23:57:50 -08001091 dbg("%s - RUNNING IN DOWNLOAD MODE", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Alan Cox2742fd82008-04-29 14:45:15 +01001093 status = check_i2c_image(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 if (status) {
Andrew Morton9544e832008-02-04 23:57:50 -08001095 dbg("%s - DOWNLOAD MODE -- BAD I2C", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 return status;
1097 }
Alan Cox2742fd82008-04-29 14:45:15 +01001098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 /* Validate Hardware version number
1100 * Read Manufacturing Descriptor from TI Based Edgeport
1101 */
Alan Cox2742fd82008-04-29 14:45:15 +01001102 ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 if (!ti_manuf_desc) {
Alan Cox2742fd82008-04-29 14:45:15 +01001104 dev_err(dev, "%s - out of memory.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 return -ENOMEM;
1106 }
Alan Cox2742fd82008-04-29 14:45:15 +01001107 status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001109 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 return status;
1111 }
1112
Alan Cox2742fd82008-04-29 14:45:15 +01001113 /* Check version number of ION descriptor */
1114 if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
1115 dbg("%s - Wrong CPU Rev %d (Must be 2)",
1116 __func__, ti_cpu_rev(ti_manuf_desc));
1117 kfree(ti_manuf_desc);
1118 return -EINVAL;
1119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Alan Cox2742fd82008-04-29 14:45:15 +01001121 rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (!rom_desc) {
Alan Cox2742fd82008-04-29 14:45:15 +01001123 dev_err(dev, "%s - out of memory.\n", __func__);
1124 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 return -ENOMEM;
1126 }
1127
Alan Cox2742fd82008-04-29 14:45:15 +01001128 /* Search for type 2 record (firmware record) */
1129 start_address = get_descriptor_addr(serial,
1130 I2C_DESC_TYPE_FIRMWARE_BASIC, rom_desc);
1131 if (start_address != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 struct ti_i2c_firmware_rec *firmware_version;
Johan Hovolde9305d22009-12-28 23:01:50 +01001133 u8 *record;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Alan Cox2742fd82008-04-29 14:45:15 +01001135 dbg("%s - Found Type FIRMWARE (Type 2) record",
1136 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Alan Cox2742fd82008-04-29 14:45:15 +01001138 firmware_version = kmalloc(sizeof(*firmware_version),
1139 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 if (!firmware_version) {
Alan Cox2742fd82008-04-29 14:45:15 +01001141 dev_err(dev, "%s - out of memory.\n", __func__);
1142 kfree(rom_desc);
1143 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 return -ENOMEM;
1145 }
1146
Alan Cox2742fd82008-04-29 14:45:15 +01001147 /* Validate version number
1148 * Read the descriptor data
1149 */
1150 status = read_rom(serial, start_address +
1151 sizeof(struct ti_i2c_desc),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 sizeof(struct ti_i2c_firmware_rec),
1153 (__u8 *)firmware_version);
1154 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001155 kfree(firmware_version);
1156 kfree(rom_desc);
1157 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 return status;
1159 }
1160
Alan Cox2742fd82008-04-29 14:45:15 +01001161 /* Check version number of download with current
1162 version in I2c */
1163 download_cur_ver = (firmware_version->Ver_Major << 8) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 (firmware_version->Ver_Minor);
Jaswinder Singhd12b2192008-07-04 23:06:09 +05301165 download_new_ver = (OperationalMajorVersion << 8) +
1166 (OperationalMinorVersion);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Alan Cox2742fd82008-04-29 14:45:15 +01001168 dbg("%s - >> FW Versions Device %d.%d Driver %d.%d",
1169 __func__,
1170 firmware_version->Ver_Major,
1171 firmware_version->Ver_Minor,
1172 OperationalMajorVersion,
1173 OperationalMinorVersion);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Alan Cox2742fd82008-04-29 14:45:15 +01001175 /* Check if we have an old version in the I2C and
1176 update if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 if (download_cur_ver != download_new_ver) {
Alan Cox2742fd82008-04-29 14:45:15 +01001178 dbg("%s - Update I2C dld from %d.%d to %d.%d",
1179 __func__,
1180 firmware_version->Ver_Major,
1181 firmware_version->Ver_Minor,
1182 OperationalMajorVersion,
1183 OperationalMinorVersion);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Johan Hovolde9305d22009-12-28 23:01:50 +01001185 record = kmalloc(1, GFP_KERNEL);
1186 if (!record) {
1187 dev_err(dev, "%s - out of memory.\n",
1188 __func__);
1189 kfree(firmware_version);
1190 kfree(rom_desc);
1191 kfree(ti_manuf_desc);
1192 return -ENOMEM;
1193 }
Alan Cox2742fd82008-04-29 14:45:15 +01001194 /* In order to update the I2C firmware we must
1195 * change the type 2 record to type 0xF2. This
1196 * will force the UMP to come up in Boot Mode.
1197 * Then while in boot mode, the driver will
1198 * download the latest firmware (padded to
1199 * 15.5k) into the UMP ram. Finally when the
1200 * device comes back up in download mode the
1201 * driver will cause the new firmware to be
1202 * copied from the UMP Ram to I2C and the
1203 * firmware will update the record type from
1204 * 0xf2 to 0x02.
1205 */
Johan Hovolde9305d22009-12-28 23:01:50 +01001206 *record = I2C_DESC_TYPE_FIRMWARE_BLANK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Alan Cox2742fd82008-04-29 14:45:15 +01001208 /* Change the I2C Firmware record type to
1209 0xf2 to trigger an update */
1210 status = write_rom(serial, start_address,
Johan Hovolde9305d22009-12-28 23:01:50 +01001211 sizeof(*record), record);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 if (status) {
Johan Hovolde9305d22009-12-28 23:01:50 +01001213 kfree(record);
Alan Cox2742fd82008-04-29 14:45:15 +01001214 kfree(firmware_version);
1215 kfree(rom_desc);
1216 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 return status;
1218 }
1219
Alan Cox2742fd82008-04-29 14:45:15 +01001220 /* verify the write -- must do this in order
1221 * for write to complete before we do the
1222 * hardware reset
1223 */
1224 status = read_rom(serial,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 start_address,
Johan Hovolde9305d22009-12-28 23:01:50 +01001226 sizeof(*record),
1227 record);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 if (status) {
Johan Hovolde9305d22009-12-28 23:01:50 +01001229 kfree(record);
Alan Cox2742fd82008-04-29 14:45:15 +01001230 kfree(firmware_version);
1231 kfree(rom_desc);
1232 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 return status;
1234 }
1235
Johan Hovolde9305d22009-12-28 23:01:50 +01001236 if (*record != I2C_DESC_TYPE_FIRMWARE_BLANK) {
Alan Cox2742fd82008-04-29 14:45:15 +01001237 dev_err(dev,
1238 "%s - error resetting device\n",
1239 __func__);
Johan Hovolde9305d22009-12-28 23:01:50 +01001240 kfree(record);
Alan Cox2742fd82008-04-29 14:45:15 +01001241 kfree(firmware_version);
1242 kfree(rom_desc);
1243 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 return -ENODEV;
1245 }
1246
Alan Cox2742fd82008-04-29 14:45:15 +01001247 dbg("%s - HARDWARE RESET", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Alan Cox2742fd82008-04-29 14:45:15 +01001249 /* Reset UMP -- Back to BOOT MODE */
1250 status = ti_vsend_sync(serial->serial->dev,
1251 UMPC_HARDWARE_RESET,
1252 0, 0, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Alan Cox2742fd82008-04-29 14:45:15 +01001254 dbg("%s - HARDWARE RESET return %d",
1255 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 /* return an error on purpose. */
Johan Hovolde9305d22009-12-28 23:01:50 +01001258 kfree(record);
Alan Cox2742fd82008-04-29 14:45:15 +01001259 kfree(firmware_version);
1260 kfree(rom_desc);
1261 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 return -ENODEV;
1263 }
Alan Cox2742fd82008-04-29 14:45:15 +01001264 kfree(firmware_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
Alan Cox2742fd82008-04-29 14:45:15 +01001266 /* Search for type 0xF2 record (firmware blank record) */
1267 else if ((start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_FIRMWARE_BLANK, rom_desc)) != 0) {
1268#define HEADER_SIZE (sizeof(struct ti_i2c_desc) + \
1269 sizeof(struct ti_i2c_firmware_rec))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 __u8 *header;
1271 __u8 *vheader;
1272
Alan Cox2742fd82008-04-29 14:45:15 +01001273 header = kmalloc(HEADER_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 if (!header) {
Alan Cox2742fd82008-04-29 14:45:15 +01001275 dev_err(dev, "%s - out of memory.\n", __func__);
1276 kfree(rom_desc);
1277 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 return -ENOMEM;
1279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Alan Cox2742fd82008-04-29 14:45:15 +01001281 vheader = kmalloc(HEADER_SIZE, GFP_KERNEL);
1282 if (!vheader) {
1283 dev_err(dev, "%s - out of memory.\n", __func__);
1284 kfree(header);
1285 kfree(rom_desc);
1286 kfree(ti_manuf_desc);
1287 return -ENOMEM;
1288 }
1289
1290 dbg("%s - Found Type BLANK FIRMWARE (Type F2) record",
1291 __func__);
1292
1293 /*
1294 * In order to update the I2C firmware we must change
1295 * the type 2 record to type 0xF2. This will force the
1296 * UMP to come up in Boot Mode. Then while in boot
1297 * mode, the driver will download the latest firmware
1298 * (padded to 15.5k) into the UMP ram. Finally when the
1299 * device comes back up in download mode the driver
1300 * will cause the new firmware to be copied from the
1301 * UMP Ram to I2C and the firmware will update the
1302 * record type from 0xf2 to 0x02.
1303 */
1304 status = build_i2c_fw_hdr(header, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001306 kfree(vheader);
1307 kfree(header);
1308 kfree(rom_desc);
1309 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 return status;
1311 }
1312
Alan Cox2742fd82008-04-29 14:45:15 +01001313 /* Update I2C with type 0xf2 record with correct
1314 size and checksum */
1315 status = write_rom(serial,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 start_address,
1317 HEADER_SIZE,
1318 header);
1319 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001320 kfree(vheader);
1321 kfree(header);
1322 kfree(rom_desc);
1323 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 return status;
1325 }
1326
Alan Cox2742fd82008-04-29 14:45:15 +01001327 /* verify the write -- must do this in order for
1328 write to complete before we do the hardware reset */
1329 status = read_rom(serial, start_address,
1330 HEADER_SIZE, vheader);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
1332 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001333 dbg("%s - can't read header back", __func__);
1334 kfree(vheader);
1335 kfree(header);
1336 kfree(rom_desc);
1337 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 return status;
1339 }
1340 if (memcmp(vheader, header, HEADER_SIZE)) {
Alan Cox2742fd82008-04-29 14:45:15 +01001341 dbg("%s - write download record failed",
1342 __func__);
1343 kfree(vheader);
1344 kfree(header);
1345 kfree(rom_desc);
1346 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 return status;
1348 }
1349
Alan Cox2742fd82008-04-29 14:45:15 +01001350 kfree(vheader);
1351 kfree(header);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Alan Cox2742fd82008-04-29 14:45:15 +01001353 dbg("%s - Start firmware update", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Alan Cox2742fd82008-04-29 14:45:15 +01001355 /* Tell firmware to copy download image into I2C */
1356 status = ti_vsend_sync(serial->serial->dev,
1357 UMPC_COPY_DNLD_TO_I2C, 0, 0, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Alan Cox2742fd82008-04-29 14:45:15 +01001359 dbg("%s - Update complete 0x%x", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001361 dev_err(dev,
1362 "%s - UMPC_COPY_DNLD_TO_I2C failed\n",
1363 __func__);
1364 kfree(rom_desc);
1365 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 return status;
1367 }
1368 }
1369
1370 // The device is running the download code
Alan Cox2742fd82008-04-29 14:45:15 +01001371 kfree(rom_desc);
1372 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 return 0;
1374 }
1375
1376 /********************************************************************/
1377 /* Boot Mode */
1378 /********************************************************************/
Andrew Morton9544e832008-02-04 23:57:50 -08001379 dbg("%s - RUNNING IN BOOT MODE", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Alan Cox2742fd82008-04-29 14:45:15 +01001381 /* Configure the TI device so we can use the BULK pipes for download */
1382 status = config_boot_dev(serial->serial->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 if (status)
1384 return status;
1385
Alan Cox2742fd82008-04-29 14:45:15 +01001386 if (le16_to_cpu(serial->serial->dev->descriptor.idVendor)
1387 != USB_VENDOR_ID_ION) {
1388 dbg("%s - VID = 0x%x", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 le16_to_cpu(serial->serial->dev->descriptor.idVendor));
1390 serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
Alan Cox2742fd82008-04-29 14:45:15 +01001391 goto stayinbootmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 }
1393
Alan Cox2742fd82008-04-29 14:45:15 +01001394 /* We have an ION device (I2c Must be programmed)
1395 Determine I2C image type */
1396 if (i2c_type_bootmode(serial))
1397 goto stayinbootmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Alan Cox2742fd82008-04-29 14:45:15 +01001399 /* Check for ION Vendor ID and that the I2C is valid */
1400 if (!check_i2c_image(serial)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 struct ti_i2c_image_header *header;
1402 int i;
1403 __u8 cs = 0;
1404 __u8 *buffer;
1405 int buffer_size;
Jaswinder Singhd12b2192008-07-04 23:06:09 +05301406 int err;
1407 const struct firmware *fw;
1408 const char *fw_name = "edgeport/down3.bin";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 /* Validate Hardware version number
1411 * Read Manufacturing Descriptor from TI Based Edgeport
1412 */
Alan Cox2742fd82008-04-29 14:45:15 +01001413 ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 if (!ti_manuf_desc) {
Alan Cox2742fd82008-04-29 14:45:15 +01001415 dev_err(dev, "%s - out of memory.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 return -ENOMEM;
1417 }
Alan Cox2742fd82008-04-29 14:45:15 +01001418 status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001420 kfree(ti_manuf_desc);
1421 goto stayinbootmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 }
1423
Alan Cox2742fd82008-04-29 14:45:15 +01001424 /* Check for version 2 */
1425 if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
1426 dbg("%s - Wrong CPU Rev %d (Must be 2)",
1427 __func__, ti_cpu_rev(ti_manuf_desc));
1428 kfree(ti_manuf_desc);
1429 goto stayinbootmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 }
1431
Alan Cox2742fd82008-04-29 14:45:15 +01001432 kfree(ti_manuf_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 /*
Alan Cox2742fd82008-04-29 14:45:15 +01001435 * In order to update the I2C firmware we must change the type
1436 * 2 record to type 0xF2. This will force the UMP to come up
1437 * in Boot Mode. Then while in boot mode, the driver will
1438 * download the latest firmware (padded to 15.5k) into the
1439 * UMP ram. Finally when the device comes back up in download
1440 * mode the driver will cause the new firmware to be copied
1441 * from the UMP Ram to I2C and the firmware will update the
1442 * record type from 0xf2 to 0x02.
1443 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 * Do we really have to copy the whole firmware image,
1445 * or could we do this in place!
1446 */
1447
Alan Cox2742fd82008-04-29 14:45:15 +01001448 /* Allocate a 15.5k buffer + 3 byte header */
1449 buffer_size = (((1024 * 16) - 512) +
1450 sizeof(struct ti_i2c_image_header));
1451 buffer = kmalloc(buffer_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 if (!buffer) {
Alan Cox2742fd82008-04-29 14:45:15 +01001453 dev_err(dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 return -ENOMEM;
1455 }
Alan Cox2742fd82008-04-29 14:45:15 +01001456
1457 /* Initialize the buffer to 0xff (pad the buffer) */
1458 memset(buffer, 0xff, buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Jaswinder Singhd12b2192008-07-04 23:06:09 +05301460 err = request_firmware(&fw, fw_name, dev);
1461 if (err) {
1462 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
1463 fw_name, err);
1464 kfree(buffer);
1465 return err;
1466 }
1467 memcpy(buffer, &fw->data[4], fw->size - 4);
1468 release_firmware(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Alan Cox2742fd82008-04-29 14:45:15 +01001470 for (i = sizeof(struct ti_i2c_image_header);
1471 i < buffer_size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 cs = (__u8)(cs + buffer[i]);
1473 }
Alan Cox2742fd82008-04-29 14:45:15 +01001474
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 header = (struct ti_i2c_image_header *)buffer;
Alan Cox2742fd82008-04-29 14:45:15 +01001476
1477 /* update length and checksum after padding */
1478 header->Length = cpu_to_le16((__u16)(buffer_size -
1479 sizeof(struct ti_i2c_image_header)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 header->CheckSum = cs;
1481
Alan Cox2742fd82008-04-29 14:45:15 +01001482 /* Download the operational code */
1483 dbg("%s - Downloading operational code image (TI UMP)",
1484 __func__);
1485 status = download_code(serial, buffer, buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Alan Cox2742fd82008-04-29 14:45:15 +01001487 kfree(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
1489 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001490 dbg("%s - Error downloading operational code image",
1491 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 return status;
1493 }
1494
Alan Cox2742fd82008-04-29 14:45:15 +01001495 /* Device will reboot */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 serial->product_info.TiMode = TI_MODE_TRANSITIONING;
1497
Alan Cox2742fd82008-04-29 14:45:15 +01001498 dbg("%s - Download successful -- Device rebooting...",
1499 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
1501 /* return an error on purpose */
1502 return -ENODEV;
1503 }
1504
Alan Cox2742fd82008-04-29 14:45:15 +01001505stayinbootmode:
1506 /* Eprom is invalid or blank stay in boot mode */
Andrew Morton9544e832008-02-04 23:57:50 -08001507 dbg("%s - STAYING IN BOOT MODE", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 serial->product_info.TiMode = TI_MODE_BOOT;
1509
1510 return 0;
1511}
1512
1513
Alan Cox2742fd82008-04-29 14:45:15 +01001514static int ti_do_config(struct edgeport_port *port, int feature, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
1516 int port_number = port->port->number - port->port->serial->minor;
Alan Cox2742fd82008-04-29 14:45:15 +01001517 on = !!on; /* 1 or 0 not bitmask */
1518 return send_cmd(port->port->serial->dev,
1519 feature, (__u8)(UMPM_UART1_PORT + port_number),
1520 on, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521}
1522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Alan Cox2742fd82008-04-29 14:45:15 +01001524static int restore_mcr(struct edgeport_port *port, __u8 mcr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525{
1526 int status = 0;
1527
Alan Cox2742fd82008-04-29 14:45:15 +01001528 dbg("%s - %x", __func__, mcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Alan Cox2742fd82008-04-29 14:45:15 +01001530 status = ti_do_config(port, UMPC_SET_CLR_DTR, mcr & MCR_DTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 if (status)
1532 return status;
Alan Cox2742fd82008-04-29 14:45:15 +01001533 status = ti_do_config(port, UMPC_SET_CLR_RTS, mcr & MCR_RTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 if (status)
1535 return status;
Alan Cox2742fd82008-04-29 14:45:15 +01001536 return ti_do_config(port, UMPC_SET_CLR_LOOPBACK, mcr & MCR_LOOPBACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537}
1538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539/* Convert TI LSR to standard UART flags */
Alan Cox2742fd82008-04-29 14:45:15 +01001540static __u8 map_line_status(__u8 ti_lsr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541{
1542 __u8 lsr = 0;
1543
1544#define MAP_FLAG(flagUmp, flagUart) \
1545 if (ti_lsr & flagUmp) \
1546 lsr |= flagUart;
1547
1548 MAP_FLAG(UMP_UART_LSR_OV_MASK, LSR_OVER_ERR) /* overrun */
1549 MAP_FLAG(UMP_UART_LSR_PE_MASK, LSR_PAR_ERR) /* parity error */
1550 MAP_FLAG(UMP_UART_LSR_FE_MASK, LSR_FRM_ERR) /* framing error */
1551 MAP_FLAG(UMP_UART_LSR_BR_MASK, LSR_BREAK) /* break detected */
Alan Cox2742fd82008-04-29 14:45:15 +01001552 MAP_FLAG(UMP_UART_LSR_RX_MASK, LSR_RX_AVAIL) /* rx data available */
1553 MAP_FLAG(UMP_UART_LSR_TX_MASK, LSR_TX_EMPTY) /* tx hold reg empty */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
1555#undef MAP_FLAG
1556
1557 return lsr;
1558}
1559
Alan Cox2742fd82008-04-29 14:45:15 +01001560static void handle_new_msr(struct edgeport_port *edge_port, __u8 msr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
1562 struct async_icount *icount;
1563 struct tty_struct *tty;
1564
Alan Cox2742fd82008-04-29 14:45:15 +01001565 dbg("%s - %02x", __func__, msr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Alan Cox2742fd82008-04-29 14:45:15 +01001567 if (msr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
1568 EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 icount = &edge_port->icount;
1570
1571 /* update input line counters */
1572 if (msr & EDGEPORT_MSR_DELTA_CTS)
1573 icount->cts++;
1574 if (msr & EDGEPORT_MSR_DELTA_DSR)
1575 icount->dsr++;
1576 if (msr & EDGEPORT_MSR_DELTA_CD)
1577 icount->dcd++;
1578 if (msr & EDGEPORT_MSR_DELTA_RI)
1579 icount->rng++;
Alan Cox2742fd82008-04-29 14:45:15 +01001580 wake_up_interruptible(&edge_port->delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 }
1582
1583 /* Save the new modem status */
1584 edge_port->shadow_msr = msr & 0xf0;
1585
Alan Cox4a90f092008-10-13 10:39:46 +01001586 tty = tty_port_tty_get(&edge_port->port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 /* handle CTS flow control */
1588 if (tty && C_CRTSCTS(tty)) {
1589 if (msr & EDGEPORT_MSR_CTS) {
1590 tty->hw_stopped = 0;
1591 tty_wakeup(tty);
1592 } else {
1593 tty->hw_stopped = 1;
1594 }
1595 }
Alan Cox4a90f092008-10-13 10:39:46 +01001596 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
1598 return;
1599}
1600
Alan Cox2742fd82008-04-29 14:45:15 +01001601static void handle_new_lsr(struct edgeport_port *edge_port, int lsr_data,
1602 __u8 lsr, __u8 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603{
1604 struct async_icount *icount;
Alan Cox2742fd82008-04-29 14:45:15 +01001605 __u8 new_lsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR |
1606 LSR_FRM_ERR | LSR_BREAK));
Alan Cox4a90f092008-10-13 10:39:46 +01001607 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Alan Cox2742fd82008-04-29 14:45:15 +01001609 dbg("%s - %02x", __func__, new_lsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
1611 edge_port->shadow_lsr = lsr;
1612
Alan Cox2742fd82008-04-29 14:45:15 +01001613 if (new_lsr & LSR_BREAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 /*
1615 * Parity and Framing errors only count if they
1616 * occur exclusive of a break being received.
1617 */
1618 new_lsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
1620 /* Place LSR data byte into Rx buffer */
Alan Cox4a90f092008-10-13 10:39:46 +01001621 if (lsr_data) {
1622 tty = tty_port_tty_get(&edge_port->port->port);
1623 if (tty) {
1624 edge_tty_recv(&edge_port->port->dev, tty, &data, 1);
1625 tty_kref_put(tty);
1626 }
1627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
1629 /* update input line counters */
1630 icount = &edge_port->icount;
1631 if (new_lsr & LSR_BREAK)
1632 icount->brk++;
1633 if (new_lsr & LSR_OVER_ERR)
1634 icount->overrun++;
1635 if (new_lsr & LSR_PAR_ERR)
1636 icount->parity++;
1637 if (new_lsr & LSR_FRM_ERR)
1638 icount->frame++;
1639}
1640
1641
Alan Cox2742fd82008-04-29 14:45:15 +01001642static void edge_interrupt_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
Ming Leicdc97792008-02-24 18:41:47 +08001644 struct edgeport_serial *edge_serial = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 struct usb_serial_port *port;
1646 struct edgeport_port *edge_port;
1647 unsigned char *data = urb->transfer_buffer;
1648 int length = urb->actual_length;
1649 int port_number;
1650 int function;
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001651 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 __u8 lsr;
1653 __u8 msr;
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001654 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
Harvey Harrison441b62c2008-03-03 16:08:34 -08001656 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001658 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 case 0:
1660 /* success */
1661 break;
1662 case -ECONNRESET:
1663 case -ENOENT:
1664 case -ESHUTDOWN:
1665 /* this urb is terminated, clean up */
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001666 dbg("%s - urb shutting down with status: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001667 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 return;
1669 default:
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001670 dev_err(&urb->dev->dev, "%s - nonzero urb status received: "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001671 "%d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 goto exit;
1673 }
1674
1675 if (!length) {
Alan Cox2742fd82008-04-29 14:45:15 +01001676 dbg("%s - no data in urb", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 goto exit;
1678 }
1679
Alan Cox2742fd82008-04-29 14:45:15 +01001680 usb_serial_debug_data(debug, &edge_serial->serial->dev->dev,
1681 __func__, length, data);
1682
1683 if (length != 2) {
1684 dbg("%s - expecting packet of size 2, got %d",
1685 __func__, length);
1686 goto exit;
1687 }
1688
1689 port_number = TIUMP_GET_PORT_FROM_CODE(data[0]);
1690 function = TIUMP_GET_FUNC_FROM_CODE(data[0]);
1691 dbg("%s - port_number %d, function %d, info 0x%x",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001692 __func__, port_number, function, data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 port = edge_serial->serial->port[port_number];
1694 edge_port = usb_get_serial_port_data(port);
1695 if (!edge_port) {
Alan Cox2742fd82008-04-29 14:45:15 +01001696 dbg("%s - edge_port not found", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 return;
1698 }
1699 switch (function) {
1700 case TIUMP_INTERRUPT_CODE_LSR:
Alan Cox2742fd82008-04-29 14:45:15 +01001701 lsr = map_line_status(data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 if (lsr & UMP_UART_LSR_DATA_MASK) {
Alan Cox2742fd82008-04-29 14:45:15 +01001703 /* Save the LSR event for bulk read
1704 completion routine */
1705 dbg("%s - LSR Event Port %u LSR Status = %02x",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001706 __func__, port_number, lsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 edge_port->lsr_event = 1;
1708 edge_port->lsr_mask = lsr;
1709 } else {
Alan Cox2742fd82008-04-29 14:45:15 +01001710 dbg("%s - ===== Port %d LSR Status = %02x ======",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001711 __func__, port_number, lsr);
Alan Cox2742fd82008-04-29 14:45:15 +01001712 handle_new_lsr(edge_port, 0, lsr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 }
1714 break;
1715
Alan Cox2742fd82008-04-29 14:45:15 +01001716 case TIUMP_INTERRUPT_CODE_MSR: /* MSR */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 /* Copy MSR from UMP */
1718 msr = data[1];
Alan Cox2742fd82008-04-29 14:45:15 +01001719 dbg("%s - ===== Port %u MSR Status = %02x ======\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001720 __func__, port_number, msr);
Alan Cox2742fd82008-04-29 14:45:15 +01001721 handle_new_msr(edge_port, msr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 break;
1723
1724 default:
Alan Cox2742fd82008-04-29 14:45:15 +01001725 dev_err(&urb->dev->dev,
1726 "%s - Unknown Interrupt code from UMP %x\n",
1727 __func__, data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 break;
Alan Cox2742fd82008-04-29 14:45:15 +01001729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 }
1731
1732exit:
Alan Cox2742fd82008-04-29 14:45:15 +01001733 retval = usb_submit_urb(urb, GFP_ATOMIC);
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001734 if (retval)
Alan Cox2742fd82008-04-29 14:45:15 +01001735 dev_err(&urb->dev->dev,
1736 "%s - usb_submit_urb failed with result %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001737 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738}
1739
Alan Cox2742fd82008-04-29 14:45:15 +01001740static void edge_bulk_in_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741{
Ming Leicdc97792008-02-24 18:41:47 +08001742 struct edgeport_port *edge_port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 unsigned char *data = urb->transfer_buffer;
1744 struct tty_struct *tty;
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001745 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 int port_number;
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001747 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Harvey Harrison441b62c2008-03-03 16:08:34 -08001749 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001751 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 case 0:
1753 /* success */
1754 break;
1755 case -ECONNRESET:
1756 case -ENOENT:
1757 case -ESHUTDOWN:
1758 /* this urb is terminated, clean up */
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001759 dbg("%s - urb shutting down with status: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001760 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 return;
1762 default:
Alan Cox2742fd82008-04-29 14:45:15 +01001763 dev_err(&urb->dev->dev,
1764 "%s - nonzero read bulk status received: %d\n",
1765 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 }
1767
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001768 if (status == -EPIPE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 goto exit;
1770
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001771 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001772 dev_err(&urb->dev->dev, "%s - stopping read!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 return;
1774 }
1775
1776 port_number = edge_port->port->number - edge_port->port->serial->minor;
1777
1778 if (edge_port->lsr_event) {
1779 edge_port->lsr_event = 0;
Alan Cox2742fd82008-04-29 14:45:15 +01001780 dbg("%s ===== Port %u LSR Status = %02x, Data = %02x ======",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001781 __func__, port_number, edge_port->lsr_mask, *data);
Alan Cox2742fd82008-04-29 14:45:15 +01001782 handle_new_lsr(edge_port, 1, edge_port->lsr_mask, *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 /* Adjust buffer length/pointer */
1784 --urb->actual_length;
1785 ++data;
1786 }
1787
Alan Cox4a90f092008-10-13 10:39:46 +01001788 tty = tty_port_tty_get(&edge_port->port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 if (tty && urb->actual_length) {
Alan Cox2742fd82008-04-29 14:45:15 +01001790 usb_serial_debug_data(debug, &edge_port->port->dev,
1791 __func__, urb->actual_length, data);
1792 if (edge_port->close_pending)
1793 dbg("%s - close pending, dropping data on the floor",
1794 __func__);
1795 else
1796 edge_tty_recv(&edge_port->port->dev, tty, data,
1797 urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 edge_port->icount.rx += urb->actual_length;
1799 }
Alan Cox4a90f092008-10-13 10:39:46 +01001800 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
1802exit:
1803 /* continue read unless stopped */
1804 spin_lock(&edge_port->ep_lock);
1805 if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING) {
1806 urb->dev = edge_port->port->serial->dev;
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001807 retval = usb_submit_urb(urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 } else if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPING) {
1809 edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPED;
1810 }
1811 spin_unlock(&edge_port->ep_lock);
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001812 if (retval)
Alan Cox2742fd82008-04-29 14:45:15 +01001813 dev_err(&urb->dev->dev,
1814 "%s - usb_submit_urb failed with result %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001815 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816}
1817
Alan Cox2742fd82008-04-29 14:45:15 +01001818static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
1819 unsigned char *data, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820{
Alan Cox2742fd82008-04-29 14:45:15 +01001821 int queued;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
Alan Cox2742fd82008-04-29 14:45:15 +01001823 tty_buffer_request_room(tty, length);
1824 queued = tty_insert_flip_string(tty, data, length);
1825 if (queued < length)
1826 dev_err(dev, "%s - dropping data, %d bytes lost\n",
1827 __func__, length - queued);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 tty_flip_buffer_push(tty);
1829}
1830
Alan Cox2742fd82008-04-29 14:45:15 +01001831static void edge_bulk_out_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832{
Ming Leicdc97792008-02-24 18:41:47 +08001833 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001835 int status = urb->status;
Alan Cox4a90f092008-10-13 10:39:46 +01001836 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Alan Cox2742fd82008-04-29 14:45:15 +01001838 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
1840 edge_port->ep_write_urb_in_use = 0;
1841
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001842 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 case 0:
1844 /* success */
1845 break;
1846 case -ECONNRESET:
1847 case -ENOENT:
1848 case -ESHUTDOWN:
1849 /* this urb is terminated, clean up */
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001850 dbg("%s - urb shutting down with status: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001851 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 return;
1853 default:
Greg Kroah-Hartmanee337c22007-06-15 15:44:13 -07001854 dev_err(&urb->dev->dev, "%s - nonzero write bulk status "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001855 "received: %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 }
1857
1858 /* send any buffered data */
Alan Cox4a90f092008-10-13 10:39:46 +01001859 tty = tty_port_tty_get(&port->port);
1860 edge_send(tty);
1861 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862}
1863
Alan Coxa509a7e2009-09-19 13:13:26 -07001864static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865{
1866 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1867 struct edgeport_serial *edge_serial;
1868 struct usb_device *dev;
1869 struct urb *urb;
1870 int port_number;
1871 int status;
1872 u16 open_settings;
1873 u8 transaction_timeout;
1874
Harvey Harrison441b62c2008-03-03 16:08:34 -08001875 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
1877 if (edge_port == NULL)
1878 return -ENODEV;
1879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 port_number = port->number - port->serial->minor;
1881 switch (port_number) {
Alan Cox2742fd82008-04-29 14:45:15 +01001882 case 0:
1883 edge_port->uart_base = UMPMEM_BASE_UART1;
1884 edge_port->dma_address = UMPD_OEDB1_ADDRESS;
1885 break;
1886 case 1:
1887 edge_port->uart_base = UMPMEM_BASE_UART2;
1888 edge_port->dma_address = UMPD_OEDB2_ADDRESS;
1889 break;
1890 default:
1891 dev_err(&port->dev, "Unknown port number!!!\n");
1892 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 }
1894
Alan Cox2742fd82008-04-29 14:45:15 +01001895 dbg("%s - port_number = %d, uart_base = %04x, dma_address = %04x",
1896 __func__, port_number, edge_port->uart_base,
1897 edge_port->dma_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
1899 dev = port->serial->dev;
1900
Alan Cox2742fd82008-04-29 14:45:15 +01001901 memset(&(edge_port->icount), 0x00, sizeof(edge_port->icount));
1902 init_waitqueue_head(&edge_port->delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
1904 /* turn off loopback */
Alan Cox2742fd82008-04-29 14:45:15 +01001905 status = ti_do_config(edge_port, UMPC_SET_CLR_LOOPBACK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001907 dev_err(&port->dev,
1908 "%s - cannot send clear loopback command, %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001909 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 return status;
1911 }
Alan Cox2742fd82008-04-29 14:45:15 +01001912
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 /* set up the port settings */
Alan Cox95da3102008-07-22 11:09:07 +01001914 if (tty)
Alan Cox4a90f092008-10-13 10:39:46 +01001915 edge_set_termios(tty, port, tty->termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
1917 /* open up the port */
1918
1919 /* milliseconds to timeout for DMA transfer */
1920 transaction_timeout = 2;
1921
Alan Cox2742fd82008-04-29 14:45:15 +01001922 edge_port->ump_read_timeout =
1923 max(20, ((transaction_timeout * 3) / 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Alan Cox2742fd82008-04-29 14:45:15 +01001925 /* milliseconds to timeout for DMA transfer */
1926 open_settings = (u8)(UMP_DMA_MODE_CONTINOUS |
1927 UMP_PIPE_TRANS_TIMEOUT_ENA |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 (transaction_timeout << 2));
1929
Alan Cox2742fd82008-04-29 14:45:15 +01001930 dbg("%s - Sending UMPC_OPEN_PORT", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
1932 /* Tell TI to open and start the port */
Alan Cox2742fd82008-04-29 14:45:15 +01001933 status = send_cmd(dev, UMPC_OPEN_PORT,
1934 (u8)(UMPM_UART1_PORT + port_number), open_settings, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001936 dev_err(&port->dev, "%s - cannot send open command, %d\n",
1937 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 return status;
1939 }
1940
1941 /* Start the DMA? */
Alan Cox2742fd82008-04-29 14:45:15 +01001942 status = send_cmd(dev, UMPC_START_PORT,
1943 (u8)(UMPM_UART1_PORT + port_number), 0, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001945 dev_err(&port->dev, "%s - cannot send start DMA command, %d\n",
1946 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 return status;
1948 }
1949
1950 /* Clear TX and RX buffers in UMP */
Alan Cox2742fd82008-04-29 14:45:15 +01001951 status = purge_port(port, UMP_PORT_DIR_OUT | UMP_PORT_DIR_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001953 dev_err(&port->dev,
1954 "%s - cannot send clear buffers command, %d\n",
1955 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 return status;
1957 }
1958
1959 /* Read Initial MSR */
Alan Cox2742fd82008-04-29 14:45:15 +01001960 status = ti_vread_sync(dev, UMPC_READ_MSR, 0,
1961 (__u16)(UMPM_UART1_PORT + port_number),
1962 &edge_port->shadow_msr, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001964 dev_err(&port->dev, "%s - cannot send read MSR command, %d\n",
1965 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 return status;
1967 }
1968
Alan Cox2742fd82008-04-29 14:45:15 +01001969 dbg("ShadowMSR 0x%X", edge_port->shadow_msr);
1970
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 /* Set Initial MCR */
1972 edge_port->shadow_mcr = MCR_RTS | MCR_DTR;
Alan Cox2742fd82008-04-29 14:45:15 +01001973 dbg("ShadowMCR 0x%X", edge_port->shadow_mcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
1975 edge_serial = edge_port->edge_serial;
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01001976 if (mutex_lock_interruptible(&edge_serial->es_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 return -ERESTARTSYS;
1978 if (edge_serial->num_ports_open == 0) {
Alan Cox2742fd82008-04-29 14:45:15 +01001979 /* we are the first port to open, post the interrupt urb */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 urb = edge_serial->serial->port[0]->interrupt_in_urb;
1981 if (!urb) {
Alan Cox2742fd82008-04-29 14:45:15 +01001982 dev_err(&port->dev,
1983 "%s - no interrupt urb present, exiting\n",
1984 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 status = -EINVAL;
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01001986 goto release_es_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 }
1988 urb->complete = edge_interrupt_callback;
1989 urb->context = edge_serial;
1990 urb->dev = dev;
Alan Cox2742fd82008-04-29 14:45:15 +01001991 status = usb_submit_urb(urb, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01001993 dev_err(&port->dev,
1994 "%s - usb_submit_urb failed with value %d\n",
1995 __func__, status);
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01001996 goto release_es_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 }
1998 }
1999
2000 /*
2001 * reset the data toggle on the bulk endpoints to work around bug in
2002 * host controllers where things get out of sync some times
2003 */
Alan Cox2742fd82008-04-29 14:45:15 +01002004 usb_clear_halt(dev, port->write_urb->pipe);
2005 usb_clear_halt(dev, port->read_urb->pipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
2007 /* start up our bulk read urb */
2008 urb = port->read_urb;
2009 if (!urb) {
Alan Cox2742fd82008-04-29 14:45:15 +01002010 dev_err(&port->dev, "%s - no read urb present, exiting\n",
2011 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 status = -EINVAL;
2013 goto unlink_int_urb;
2014 }
2015 edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
2016 urb->complete = edge_bulk_in_callback;
2017 urb->context = edge_port;
2018 urb->dev = dev;
Alan Cox2742fd82008-04-29 14:45:15 +01002019 status = usb_submit_urb(urb, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01002021 dev_err(&port->dev,
2022 "%s - read bulk usb_submit_urb failed with value %d\n",
2023 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 goto unlink_int_urb;
2025 }
2026
2027 ++edge_serial->num_ports_open;
2028
Harvey Harrison441b62c2008-03-03 16:08:34 -08002029 dbg("%s - exited", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01002031 goto release_es_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
2033unlink_int_urb:
2034 if (edge_port->edge_serial->num_ports_open == 0)
2035 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01002036release_es_lock:
2037 mutex_unlock(&edge_serial->es_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 return status;
2039}
2040
Alan Cox335f8512009-06-11 12:26:29 +01002041static void edge_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042{
2043 struct edgeport_serial *edge_serial;
2044 struct edgeport_port *edge_port;
2045 int port_number;
2046 int status;
2047
Harvey Harrison441b62c2008-03-03 16:08:34 -08002048 dbg("%s - port %d", __func__, port->number);
Alan Cox2742fd82008-04-29 14:45:15 +01002049
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 edge_serial = usb_get_serial_data(port->serial);
2051 edge_port = usb_get_serial_port_data(port);
Alan Cox2742fd82008-04-29 14:45:15 +01002052 if (edge_serial == NULL || edge_port == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 return;
Alan Cox2742fd82008-04-29 14:45:15 +01002054
2055 /* The bulkreadcompletion routine will check
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 * this flag and dump add read data */
2057 edge_port->close_pending = 1;
2058
2059 /* chase the port close and flush */
Alan Cox2742fd82008-04-29 14:45:15 +01002060 chase_port(edge_port, (HZ * closing_wait) / 100, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
2062 usb_kill_urb(port->read_urb);
2063 usb_kill_urb(port->write_urb);
2064 edge_port->ep_write_urb_in_use = 0;
2065
2066 /* assuming we can still talk to the device,
2067 * send a close port command to it */
Harvey Harrison441b62c2008-03-03 16:08:34 -08002068 dbg("%s - send umpc_close_port", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 port_number = port->number - port->serial->minor;
Alan Cox2742fd82008-04-29 14:45:15 +01002070 status = send_cmd(port->serial->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 UMPC_CLOSE_PORT,
2072 (__u8)(UMPM_UART1_PORT + port_number),
2073 0,
2074 NULL,
2075 0);
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01002076 mutex_lock(&edge_serial->es_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 --edge_port->edge_serial->num_ports_open;
2078 if (edge_port->edge_serial->num_ports_open <= 0) {
2079 /* last port is now closed, let's shut down our interrupt urb */
2080 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
2081 edge_port->edge_serial->num_ports_open = 0;
2082 }
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01002083 mutex_unlock(&edge_serial->es_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 edge_port->close_pending = 0;
2085
Harvey Harrison441b62c2008-03-03 16:08:34 -08002086 dbg("%s - exited", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087}
2088
Alan Cox95da3102008-07-22 11:09:07 +01002089static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
2090 const unsigned char *data, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091{
2092 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2093 unsigned long flags;
2094
Harvey Harrison441b62c2008-03-03 16:08:34 -08002095 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
2097 if (count == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002098 dbg("%s - write request of 0 bytes", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 return 0;
2100 }
2101
2102 if (edge_port == NULL)
2103 return -ENODEV;
2104 if (edge_port->close_pending == 1)
2105 return -ENODEV;
2106
2107 spin_lock_irqsave(&edge_port->ep_lock, flags);
2108 count = edge_buf_put(edge_port->ep_out_buf, data, count);
2109 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2110
Alan Cox95da3102008-07-22 11:09:07 +01002111 edge_send(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
2113 return count;
2114}
2115
Alan Cox95da3102008-07-22 11:09:07 +01002116static void edge_send(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117{
Alan Cox95da3102008-07-22 11:09:07 +01002118 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 int count, result;
2120 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 unsigned long flags;
2122
2123
Harvey Harrison441b62c2008-03-03 16:08:34 -08002124 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
2126 spin_lock_irqsave(&edge_port->ep_lock, flags);
2127
2128 if (edge_port->ep_write_urb_in_use) {
2129 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2130 return;
2131 }
2132
2133 count = edge_buf_get(edge_port->ep_out_buf,
2134 port->write_urb->transfer_buffer,
2135 port->bulk_out_size);
2136
2137 if (count == 0) {
2138 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2139 return;
2140 }
2141
2142 edge_port->ep_write_urb_in_use = 1;
2143
2144 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2145
Alan Cox2742fd82008-04-29 14:45:15 +01002146 usb_serial_debug_data(debug, &port->dev, __func__, count,
2147 port->write_urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
2149 /* set up our urb */
Alan Cox2742fd82008-04-29 14:45:15 +01002150 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
2151 usb_sndbulkpipe(port->serial->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 port->bulk_out_endpointAddress),
2153 port->write_urb->transfer_buffer, count,
2154 edge_bulk_out_callback,
2155 port);
2156
2157 /* send the data out the bulk port */
2158 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
2159 if (result) {
Alan Cox2742fd82008-04-29 14:45:15 +01002160 dev_err(&port->dev,
2161 "%s - failed submitting write urb, error %d\n",
2162 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 edge_port->ep_write_urb_in_use = 0;
Alan Cox2742fd82008-04-29 14:45:15 +01002164 /* TODO: reschedule edge_send */
2165 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 edge_port->icount.tx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
2168 /* wakeup any process waiting for writes to complete */
2169 /* there is now more room in the buffer for new writes */
Alan Cox2742fd82008-04-29 14:45:15 +01002170 if (tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172}
2173
Alan Cox95da3102008-07-22 11:09:07 +01002174static int edge_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175{
Alan Cox95da3102008-07-22 11:09:07 +01002176 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2178 int room = 0;
2179 unsigned long flags;
2180
Harvey Harrison441b62c2008-03-03 16:08:34 -08002181 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
2183 if (edge_port == NULL)
Alan Cox2742fd82008-04-29 14:45:15 +01002184 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 if (edge_port->close_pending == 1)
Alan Cox2742fd82008-04-29 14:45:15 +01002186 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
2188 spin_lock_irqsave(&edge_port->ep_lock, flags);
2189 room = edge_buf_space_avail(edge_port->ep_out_buf);
2190 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2191
Harvey Harrison441b62c2008-03-03 16:08:34 -08002192 dbg("%s - returns %d", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 return room;
2194}
2195
Alan Cox95da3102008-07-22 11:09:07 +01002196static int edge_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197{
Alan Cox95da3102008-07-22 11:09:07 +01002198 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2200 int chars = 0;
2201 unsigned long flags;
2202
Harvey Harrison441b62c2008-03-03 16:08:34 -08002203 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
2205 if (edge_port == NULL)
Alan Cox2742fd82008-04-29 14:45:15 +01002206 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 if (edge_port->close_pending == 1)
Alan Cox2742fd82008-04-29 14:45:15 +01002208 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
2210 spin_lock_irqsave(&edge_port->ep_lock, flags);
2211 chars = edge_buf_data_avail(edge_port->ep_out_buf);
2212 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2213
Alan Cox2742fd82008-04-29 14:45:15 +01002214 dbg("%s - returns %d", __func__, chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 return chars;
2216}
2217
Alan Cox95da3102008-07-22 11:09:07 +01002218static void edge_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219{
Alan Cox95da3102008-07-22 11:09:07 +01002220 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 int status;
2223
Harvey Harrison441b62c2008-03-03 16:08:34 -08002224 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
2226 if (edge_port == NULL)
2227 return;
2228
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 /* if we are implementing XON/XOFF, send the stop character */
2230 if (I_IXOFF(tty)) {
2231 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01002232 status = edge_write(tty, port, &stop_char, 1);
2233 if (status <= 0) {
2234 dev_err(&port->dev, "%s - failed to write stop character, %d\n", __func__, status);
2235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 }
2237
2238 /* if we are implementing RTS/CTS, stop reads */
2239 /* and the Edgeport will clear the RTS line */
2240 if (C_CRTSCTS(tty))
2241 stop_read(edge_port);
2242
2243}
2244
Alan Cox95da3102008-07-22 11:09:07 +01002245static void edge_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246{
Alan Cox95da3102008-07-22 11:09:07 +01002247 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 int status;
2250
Harvey Harrison441b62c2008-03-03 16:08:34 -08002251 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
2253 if (edge_port == NULL)
2254 return;
2255
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 /* if we are implementing XON/XOFF, send the start character */
2257 if (I_IXOFF(tty)) {
2258 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01002259 status = edge_write(tty, port, &start_char, 1);
2260 if (status <= 0) {
2261 dev_err(&port->dev, "%s - failed to write start character, %d\n", __func__, status);
2262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 /* if we are implementing RTS/CTS, restart reads */
2265 /* are the Edgeport will assert the RTS line */
2266 if (C_CRTSCTS(tty)) {
2267 status = restart_read(edge_port);
2268 if (status)
Alan Cox2742fd82008-04-29 14:45:15 +01002269 dev_err(&port->dev,
2270 "%s - read bulk usb_submit_urb failed: %d\n",
2271 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 }
2273
2274}
2275
2276static void stop_read(struct edgeport_port *edge_port)
2277{
2278 unsigned long flags;
2279
2280 spin_lock_irqsave(&edge_port->ep_lock, flags);
2281
2282 if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
2283 edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPING;
2284 edge_port->shadow_mcr &= ~MCR_RTS;
2285
2286 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2287}
2288
2289static int restart_read(struct edgeport_port *edge_port)
2290{
2291 struct urb *urb;
2292 int status = 0;
2293 unsigned long flags;
2294
2295 spin_lock_irqsave(&edge_port->ep_lock, flags);
2296
2297 if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPED) {
2298 urb = edge_port->port->read_urb;
2299 urb->complete = edge_bulk_in_callback;
2300 urb->context = edge_port;
2301 urb->dev = edge_port->port->serial->dev;
Oliver Neukumefdff602007-06-05 10:50:48 +02002302 status = usb_submit_urb(urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 }
2304 edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
2305 edge_port->shadow_mcr |= MCR_RTS;
2306
2307 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
2308
2309 return status;
2310}
2311
Alan Cox95da3102008-07-22 11:09:07 +01002312static void change_port_settings(struct tty_struct *tty,
2313 struct edgeport_port *edge_port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314{
2315 struct ump_uart_config *config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 int baud;
2317 unsigned cflag;
2318 int status;
Alan Cox2742fd82008-04-29 14:45:15 +01002319 int port_number = edge_port->port->number -
2320 edge_port->port->serial->minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
Harvey Harrison441b62c2008-03-03 16:08:34 -08002322 dbg("%s - port %d", __func__, edge_port->port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
Alan Cox95da3102008-07-22 11:09:07 +01002324 config = kmalloc (sizeof (*config), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 if (!config) {
Alan Cox2742fd82008-04-29 14:45:15 +01002326 *tty->termios = *old_termios;
2327 dev_err(&edge_port->port->dev, "%s - out of memory\n",
2328 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 return;
2330 }
2331
2332 cflag = tty->termios->c_cflag;
2333
2334 config->wFlags = 0;
2335
2336 /* These flags must be set */
2337 config->wFlags |= UMP_MASK_UART_FLAGS_RECEIVE_MS_INT;
2338 config->wFlags |= UMP_MASK_UART_FLAGS_AUTO_START_ON_ERR;
2339 config->bUartMode = (__u8)(edge_port->bUartMode);
2340
2341 switch (cflag & CSIZE) {
Alan Cox2742fd82008-04-29 14:45:15 +01002342 case CS5:
2343 config->bDataBits = UMP_UART_CHAR5BITS;
2344 dbg("%s - data bits = 5", __func__);
2345 break;
2346 case CS6:
2347 config->bDataBits = UMP_UART_CHAR6BITS;
2348 dbg("%s - data bits = 6", __func__);
2349 break;
2350 case CS7:
2351 config->bDataBits = UMP_UART_CHAR7BITS;
2352 dbg("%s - data bits = 7", __func__);
2353 break;
2354 default:
2355 case CS8:
2356 config->bDataBits = UMP_UART_CHAR8BITS;
2357 dbg("%s - data bits = 8", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 break;
2359 }
2360
2361 if (cflag & PARENB) {
2362 if (cflag & PARODD) {
2363 config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
2364 config->bParity = UMP_UART_ODDPARITY;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002365 dbg("%s - parity = odd", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 } else {
2367 config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
2368 config->bParity = UMP_UART_EVENPARITY;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002369 dbg("%s - parity = even", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 }
2371 } else {
Alan Cox2742fd82008-04-29 14:45:15 +01002372 config->bParity = UMP_UART_NOPARITY;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002373 dbg("%s - parity = none", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 }
2375
2376 if (cflag & CSTOPB) {
2377 config->bStopBits = UMP_UART_STOPBIT2;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002378 dbg("%s - stop bits = 2", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 } else {
2380 config->bStopBits = UMP_UART_STOPBIT1;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002381 dbg("%s - stop bits = 1", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 }
2383
2384 /* figure out the flow control settings */
2385 if (cflag & CRTSCTS) {
2386 config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X_CTS_FLOW;
2387 config->wFlags |= UMP_MASK_UART_FLAGS_RTS_FLOW;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002388 dbg("%s - RTS/CTS is enabled", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002390 dbg("%s - RTS/CTS is disabled", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 tty->hw_stopped = 0;
2392 restart_read(edge_port);
2393 }
2394
Alan Cox2742fd82008-04-29 14:45:15 +01002395 /* if we are implementing XON/XOFF, set the start and stop
2396 character in the device */
2397 config->cXon = START_CHAR(tty);
2398 config->cXoff = STOP_CHAR(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
Alan Cox2742fd82008-04-29 14:45:15 +01002400 /* if we are implementing INBOUND XON/XOFF */
2401 if (I_IXOFF(tty)) {
2402 config->wFlags |= UMP_MASK_UART_FLAGS_IN_X;
2403 dbg("%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2404 __func__, config->cXon, config->cXoff);
2405 } else
2406 dbg("%s - INBOUND XON/XOFF is disabled", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
Alan Cox2742fd82008-04-29 14:45:15 +01002408 /* if we are implementing OUTBOUND XON/XOFF */
2409 if (I_IXON(tty)) {
2410 config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X;
2411 dbg("%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
2412 __func__, config->cXon, config->cXoff);
2413 } else
2414 dbg("%s - OUTBOUND XON/XOFF is disabled", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415
Alan Coxd5f5bcd2008-01-03 16:57:33 +00002416 tty->termios->c_cflag &= ~CMSPAR;
2417
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 /* Round the baud rate */
2419 baud = tty_get_baud_rate(tty);
2420 if (!baud) {
2421 /* pick a default, any default... */
2422 baud = 9600;
Alan Coxd5f5bcd2008-01-03 16:57:33 +00002423 } else
2424 tty_encode_baud_rate(tty, baud, baud);
2425
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 edge_port->baud_rate = baud;
2427 config->wBaudRate = (__u16)((461550L + baud/2) / baud);
2428
Alan Coxd5f5bcd2008-01-03 16:57:33 +00002429 /* FIXME: Recompute actual baud from divisor here */
2430
Alan Cox2742fd82008-04-29 14:45:15 +01002431 dbg("%s - baud rate = %d, wBaudRate = %d", __func__, baud,
2432 config->wBaudRate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
Alan Cox2742fd82008-04-29 14:45:15 +01002434 dbg("wBaudRate: %d", (int)(461550L / config->wBaudRate));
2435 dbg("wFlags: 0x%x", config->wFlags);
2436 dbg("bDataBits: %d", config->bDataBits);
2437 dbg("bParity: %d", config->bParity);
2438 dbg("bStopBits: %d", config->bStopBits);
2439 dbg("cXon: %d", config->cXon);
2440 dbg("cXoff: %d", config->cXoff);
2441 dbg("bUartMode: %d", config->bUartMode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
2443 /* move the word values into big endian mode */
Alan Cox2742fd82008-04-29 14:45:15 +01002444 cpu_to_be16s(&config->wFlags);
2445 cpu_to_be16s(&config->wBaudRate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
Alan Cox2742fd82008-04-29 14:45:15 +01002447 status = send_cmd(edge_port->port->serial->dev, UMPC_SET_CONFIG,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 (__u8)(UMPM_UART1_PORT + port_number),
Alan Cox2742fd82008-04-29 14:45:15 +01002449 0, (__u8 *)config, sizeof(*config));
2450 if (status)
2451 dbg("%s - error %d when trying to write config to device",
Harvey Harrison441b62c2008-03-03 16:08:34 -08002452 __func__, status);
Alan Cox2742fd82008-04-29 14:45:15 +01002453 kfree(config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 return;
2455}
2456
Alan Cox2e0ddd62008-07-22 11:12:33 +01002457static void edge_set_termios(struct tty_struct *tty,
Alan Cox95da3102008-07-22 11:09:07 +01002458 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459{
2460 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Alan Cox95da3102008-07-22 11:09:07 +01002461 unsigned int cflag;
2462
2463 cflag = tty->termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
Harvey Harrison441b62c2008-03-03 16:08:34 -08002465 dbg("%s - clfag %08x iflag %08x", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 tty->termios->c_cflag, tty->termios->c_iflag);
Harvey Harrison441b62c2008-03-03 16:08:34 -08002467 dbg("%s - old clfag %08x old iflag %08x", __func__,
Alan Coxd5f5bcd2008-01-03 16:57:33 +00002468 old_termios->c_cflag, old_termios->c_iflag);
Harvey Harrison441b62c2008-03-03 16:08:34 -08002469 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
2471 if (edge_port == NULL)
2472 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 /* change the port settings to the new ones specified */
Alan Cox95da3102008-07-22 11:09:07 +01002474 change_port_settings(tty, edge_port, old_termios);
2475 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476}
2477
Alan Cox95da3102008-07-22 11:09:07 +01002478static int edge_tiocmset(struct tty_struct *tty, struct file *file,
Alan Cox2742fd82008-04-29 14:45:15 +01002479 unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480{
Alan Cox95da3102008-07-22 11:09:07 +01002481 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2483 unsigned int mcr;
Alan Cox3d71fe02008-02-20 21:38:32 +00002484 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
Harvey Harrison441b62c2008-03-03 16:08:34 -08002486 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
Alan Cox3d71fe02008-02-20 21:38:32 +00002488 spin_lock_irqsave(&edge_port->ep_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 mcr = edge_port->shadow_mcr;
2490 if (set & TIOCM_RTS)
2491 mcr |= MCR_RTS;
2492 if (set & TIOCM_DTR)
2493 mcr |= MCR_DTR;
2494 if (set & TIOCM_LOOP)
2495 mcr |= MCR_LOOPBACK;
2496
2497 if (clear & TIOCM_RTS)
2498 mcr &= ~MCR_RTS;
2499 if (clear & TIOCM_DTR)
2500 mcr &= ~MCR_DTR;
2501 if (clear & TIOCM_LOOP)
2502 mcr &= ~MCR_LOOPBACK;
2503
2504 edge_port->shadow_mcr = mcr;
Alan Cox3d71fe02008-02-20 21:38:32 +00002505 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506
Alan Cox2742fd82008-04-29 14:45:15 +01002507 restore_mcr(edge_port, mcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 return 0;
2509}
2510
Alan Cox95da3102008-07-22 11:09:07 +01002511static int edge_tiocmget(struct tty_struct *tty, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512{
Alan Cox95da3102008-07-22 11:09:07 +01002513 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2515 unsigned int result = 0;
2516 unsigned int msr;
2517 unsigned int mcr;
Alan Cox3d71fe02008-02-20 21:38:32 +00002518 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
Harvey Harrison441b62c2008-03-03 16:08:34 -08002520 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
Alan Cox3d71fe02008-02-20 21:38:32 +00002522 spin_lock_irqsave(&edge_port->ep_lock, flags);
2523
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 msr = edge_port->shadow_msr;
2525 mcr = edge_port->shadow_mcr;
2526 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
2527 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
2528 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
2529 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
2530 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
2531 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
2532
2533
Harvey Harrison441b62c2008-03-03 16:08:34 -08002534 dbg("%s -- %x", __func__, result);
Alan Cox3d71fe02008-02-20 21:38:32 +00002535 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536
2537 return result;
2538}
2539
Alan Cox2742fd82008-04-29 14:45:15 +01002540static int get_serial_info(struct edgeport_port *edge_port,
2541 struct serial_struct __user *retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542{
2543 struct serial_struct tmp;
2544
2545 if (!retinfo)
2546 return -EFAULT;
2547
2548 memset(&tmp, 0, sizeof(tmp));
2549
2550 tmp.type = PORT_16550A;
2551 tmp.line = edge_port->port->serial->minor;
2552 tmp.port = edge_port->port->number;
2553 tmp.irq = 0;
2554 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2555 tmp.xmit_fifo_size = edge_port->port->bulk_out_size;
2556 tmp.baud_base = 9600;
2557 tmp.close_delay = 5*HZ;
2558 tmp.closing_wait = closing_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
2560 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2561 return -EFAULT;
2562 return 0;
2563}
2564
Alan Cox95da3102008-07-22 11:09:07 +01002565static int edge_ioctl(struct tty_struct *tty, struct file *file,
Alan Cox2742fd82008-04-29 14:45:15 +01002566 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567{
Alan Cox95da3102008-07-22 11:09:07 +01002568 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2570 struct async_icount cnow;
2571 struct async_icount cprev;
2572
Harvey Harrison441b62c2008-03-03 16:08:34 -08002573 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
2575 switch (cmd) {
Alan Cox2742fd82008-04-29 14:45:15 +01002576 case TIOCGSERIAL:
2577 dbg("%s - (%d) TIOCGSERIAL", __func__, port->number);
2578 return get_serial_info(edge_port,
2579 (struct serial_struct __user *) arg);
2580 case TIOCMIWAIT:
2581 dbg("%s - (%d) TIOCMIWAIT", __func__, port->number);
2582 cprev = edge_port->icount;
2583 while (1) {
2584 interruptible_sleep_on(&edge_port->delta_msr_wait);
2585 /* see if a signal did it */
2586 if (signal_pending(current))
2587 return -ERESTARTSYS;
2588 cnow = edge_port->icount;
2589 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2590 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2591 return -EIO; /* no change => error */
2592 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2593 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2594 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2595 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2596 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 }
Alan Cox2742fd82008-04-29 14:45:15 +01002598 cprev = cnow;
2599 }
2600 /* not reached */
2601 break;
2602 case TIOCGICOUNT:
2603 dbg("%s - (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
2604 port->number, edge_port->icount.rx, edge_port->icount.tx);
2605 if (copy_to_user((void __user *)arg, &edge_port->icount,
2606 sizeof(edge_port->icount)))
2607 return -EFAULT;
2608 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 return -ENOIOCTLCMD;
2611}
2612
Alan Cox95da3102008-07-22 11:09:07 +01002613static void edge_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614{
Alan Cox95da3102008-07-22 11:09:07 +01002615 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2617 int status;
Alan Cox2742fd82008-04-29 14:45:15 +01002618 int bv = 0; /* Off */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619
Alan Cox95da3102008-07-22 11:09:07 +01002620 dbg("%s - state = %d", __func__, break_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
2622 /* chase the port close */
Alan Cox2742fd82008-04-29 14:45:15 +01002623 chase_port(edge_port, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624
Alan Cox95da3102008-07-22 11:09:07 +01002625 if (break_state == -1)
Alan Cox2742fd82008-04-29 14:45:15 +01002626 bv = 1; /* On */
2627 status = ti_do_config(edge_port, UMPC_SET_CLR_BREAK, bv);
2628 if (status)
2629 dbg("%s - error %d sending break set/clear command.",
Harvey Harrison441b62c2008-03-03 16:08:34 -08002630 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631}
2632
Alan Cox2742fd82008-04-29 14:45:15 +01002633static int edge_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634{
2635 struct edgeport_serial *edge_serial;
2636 struct edgeport_port *edge_port;
2637 struct usb_device *dev;
2638 int status;
2639 int i;
2640
2641 dev = serial->dev;
2642
2643 /* create our private serial structure */
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +01002644 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 if (edge_serial == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002646 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 return -ENOMEM;
2648 }
Matthias Kaehlcke241ca642007-12-04 16:15:40 +01002649 mutex_init(&edge_serial->es_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 edge_serial->serial = serial;
2651 usb_set_serial_data(serial, edge_serial);
2652
Alan Cox2742fd82008-04-29 14:45:15 +01002653 status = download_fw(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 if (status) {
Alan Cox2742fd82008-04-29 14:45:15 +01002655 kfree(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 return status;
2657 }
2658
2659 /* set up our port private structures */
2660 for (i = 0; i < serial->num_ports; ++i) {
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +01002661 edge_port = kzalloc(sizeof(struct edgeport_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 if (edge_port == NULL) {
Alan Cox2742fd82008-04-29 14:45:15 +01002663 dev_err(&serial->dev->dev, "%s - Out of memory\n",
2664 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 goto cleanup;
2666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 spin_lock_init(&edge_port->ep_lock);
2668 edge_port->ep_out_buf = edge_buf_alloc(EDGE_OUT_BUF_SIZE);
2669 if (edge_port->ep_out_buf == NULL) {
Alan Cox2742fd82008-04-29 14:45:15 +01002670 dev_err(&serial->dev->dev, "%s - Out of memory\n",
2671 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 kfree(edge_port);
2673 goto cleanup;
2674 }
2675 edge_port->port = serial->port[i];
2676 edge_port->edge_serial = edge_serial;
2677 usb_set_serial_port_data(serial->port[i], edge_port);
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002678 edge_port->bUartMode = default_uart_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 }
Alan Cox2742fd82008-04-29 14:45:15 +01002680
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 return 0;
2682
2683cleanup:
Alan Cox2742fd82008-04-29 14:45:15 +01002684 for (--i; i >= 0; --i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 edge_port = usb_get_serial_port_data(serial->port[i]);
2686 edge_buf_free(edge_port->ep_out_buf);
2687 kfree(edge_port);
2688 usb_set_serial_port_data(serial->port[i], NULL);
2689 }
Alan Cox2742fd82008-04-29 14:45:15 +01002690 kfree(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 usb_set_serial_data(serial, NULL);
2692 return -ENOMEM;
2693}
2694
Alan Sternf9c99bb2009-06-02 11:53:55 -04002695static void edge_disconnect(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696{
2697 int i;
2698 struct edgeport_port *edge_port;
2699
Alan Cox2742fd82008-04-29 14:45:15 +01002700 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701
Jesper Juhl0d46c002007-07-16 22:17:25 +02002702 for (i = 0; i < serial->num_ports; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 edge_port = usb_get_serial_port_data(serial->port[i]);
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002704 edge_remove_sysfs_attrs(edge_port->port);
Alan Sternf9c99bb2009-06-02 11:53:55 -04002705 }
2706}
2707
2708static void edge_release(struct usb_serial *serial)
2709{
2710 int i;
2711 struct edgeport_port *edge_port;
2712
2713 dbg("%s", __func__);
2714
2715 for (i = 0; i < serial->num_ports; ++i) {
2716 edge_port = usb_get_serial_port_data(serial->port[i]);
Jesper Juhl0d46c002007-07-16 22:17:25 +02002717 edge_buf_free(edge_port->ep_out_buf);
2718 kfree(edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 }
Jesper Juhl0d46c002007-07-16 22:17:25 +02002720 kfree(usb_get_serial_data(serial));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721}
2722
2723
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002724/* Sysfs Attributes */
2725
2726static ssize_t show_uart_mode(struct device *dev,
2727 struct device_attribute *attr, char *buf)
2728{
2729 struct usb_serial_port *port = to_usb_serial_port(dev);
2730 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2731
2732 return sprintf(buf, "%d\n", edge_port->bUartMode);
2733}
2734
2735static ssize_t store_uart_mode(struct device *dev,
2736 struct device_attribute *attr, const char *valbuf, size_t count)
2737{
2738 struct usb_serial_port *port = to_usb_serial_port(dev);
2739 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
2740 unsigned int v = simple_strtoul(valbuf, NULL, 0);
2741
Harvey Harrison441b62c2008-03-03 16:08:34 -08002742 dbg("%s: setting uart_mode = %d", __func__, v);
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002743
2744 if (v < 256)
2745 edge_port->bUartMode = v;
2746 else
Harvey Harrison441b62c2008-03-03 16:08:34 -08002747 dev_err(dev, "%s - uart_mode %d is invalid\n", __func__, v);
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002748
2749 return count;
2750}
2751
Alan Cox2742fd82008-04-29 14:45:15 +01002752static DEVICE_ATTR(uart_mode, S_IWUSR | S_IRUGO, show_uart_mode,
2753 store_uart_mode);
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002754
2755static int edge_create_sysfs_attrs(struct usb_serial_port *port)
2756{
2757 return device_create_file(&port->dev, &dev_attr_uart_mode);
2758}
2759
2760static int edge_remove_sysfs_attrs(struct usb_serial_port *port)
2761{
2762 device_remove_file(&port->dev, &dev_attr_uart_mode);
2763 return 0;
2764}
2765
2766
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767/* Circular Buffer */
2768
2769/*
2770 * edge_buf_alloc
2771 *
2772 * Allocate a circular buffer and all associated memory.
2773 */
2774
2775static struct edge_buf *edge_buf_alloc(unsigned int size)
2776{
2777 struct edge_buf *eb;
2778
2779
2780 if (size == 0)
2781 return NULL;
2782
Robert P. J. Day5cbded52006-12-13 00:35:56 -08002783 eb = kmalloc(sizeof(struct edge_buf), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 if (eb == NULL)
2785 return NULL;
2786
2787 eb->buf_buf = kmalloc(size, GFP_KERNEL);
2788 if (eb->buf_buf == NULL) {
2789 kfree(eb);
2790 return NULL;
2791 }
2792
2793 eb->buf_size = size;
2794 eb->buf_get = eb->buf_put = eb->buf_buf;
2795
2796 return eb;
2797}
2798
2799
2800/*
2801 * edge_buf_free
2802 *
2803 * Free the buffer and all associated memory.
2804 */
2805
Adrian Bunk3d485862005-11-20 23:56:11 +01002806static void edge_buf_free(struct edge_buf *eb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807{
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07002808 if (eb) {
2809 kfree(eb->buf_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 kfree(eb);
2811 }
2812}
2813
2814
2815/*
2816 * edge_buf_clear
2817 *
2818 * Clear out all data in the circular buffer.
2819 */
2820
2821static void edge_buf_clear(struct edge_buf *eb)
2822{
Alan Cox2742fd82008-04-29 14:45:15 +01002823 if (eb != NULL)
2824 eb->buf_get = eb->buf_put;
2825 /* equivalent to a get of all data available */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826}
2827
2828
2829/*
2830 * edge_buf_data_avail
2831 *
2832 * Return the number of bytes of data available in the circular
2833 * buffer.
2834 */
2835
2836static unsigned int edge_buf_data_avail(struct edge_buf *eb)
2837{
Alan Cox2742fd82008-04-29 14:45:15 +01002838 if (eb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 return 0;
Alan Cox2742fd82008-04-29 14:45:15 +01002840 return ((eb->buf_size + eb->buf_put - eb->buf_get) % eb->buf_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841}
2842
2843
2844/*
2845 * edge_buf_space_avail
2846 *
2847 * Return the number of bytes of space available in the circular
2848 * buffer.
2849 */
2850
2851static unsigned int edge_buf_space_avail(struct edge_buf *eb)
2852{
Alan Cox2742fd82008-04-29 14:45:15 +01002853 if (eb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 return 0;
Alan Cox2742fd82008-04-29 14:45:15 +01002855 return ((eb->buf_size + eb->buf_get - eb->buf_put - 1) % eb->buf_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856}
2857
2858
2859/*
2860 * edge_buf_put
2861 *
2862 * Copy data data from a user buffer and put it into the circular buffer.
2863 * Restrict to the amount of space available.
2864 *
2865 * Return the number of bytes copied.
2866 */
2867
2868static unsigned int edge_buf_put(struct edge_buf *eb, const char *buf,
2869 unsigned int count)
2870{
2871 unsigned int len;
2872
2873
2874 if (eb == NULL)
2875 return 0;
2876
2877 len = edge_buf_space_avail(eb);
2878 if (count > len)
2879 count = len;
2880
2881 if (count == 0)
2882 return 0;
2883
2884 len = eb->buf_buf + eb->buf_size - eb->buf_put;
2885 if (count > len) {
2886 memcpy(eb->buf_put, buf, len);
2887 memcpy(eb->buf_buf, buf+len, count - len);
2888 eb->buf_put = eb->buf_buf + count - len;
2889 } else {
2890 memcpy(eb->buf_put, buf, count);
2891 if (count < len)
2892 eb->buf_put += count;
2893 else /* count == len */
2894 eb->buf_put = eb->buf_buf;
2895 }
2896
2897 return count;
2898}
2899
2900
2901/*
2902 * edge_buf_get
2903 *
2904 * Get data from the circular buffer and copy to the given buffer.
2905 * Restrict to the amount of data available.
2906 *
2907 * Return the number of bytes copied.
2908 */
2909
2910static unsigned int edge_buf_get(struct edge_buf *eb, char *buf,
2911 unsigned int count)
2912{
2913 unsigned int len;
2914
2915
2916 if (eb == NULL)
2917 return 0;
2918
2919 len = edge_buf_data_avail(eb);
2920 if (count > len)
2921 count = len;
2922
2923 if (count == 0)
2924 return 0;
2925
2926 len = eb->buf_buf + eb->buf_size - eb->buf_get;
2927 if (count > len) {
2928 memcpy(buf, eb->buf_get, len);
2929 memcpy(buf+len, eb->buf_buf, count - len);
2930 eb->buf_get = eb->buf_buf + count - len;
2931 } else {
2932 memcpy(buf, eb->buf_get, count);
2933 if (count < len)
2934 eb->buf_get += count;
2935 else /* count == len */
2936 eb->buf_get = eb->buf_buf;
2937 }
2938
2939 return count;
2940}
2941
2942
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -07002943static struct usb_serial_driver edgeport_1port_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07002944 .driver = {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07002945 .owner = THIS_MODULE,
2946 .name = "edgeport_ti_1",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07002947 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07002948 .description = "Edgeport TI 1 port adapter",
Johannes Hölzld9b1b782006-12-17 21:50:24 +01002949 .usb_driver = &io_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 .id_table = edgeport_1port_id_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 .num_ports = 1,
2952 .open = edge_open,
2953 .close = edge_close,
2954 .throttle = edge_throttle,
2955 .unthrottle = edge_unthrottle,
2956 .attach = edge_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04002957 .disconnect = edge_disconnect,
2958 .release = edge_release,
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002959 .port_probe = edge_create_sysfs_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 .ioctl = edge_ioctl,
2961 .set_termios = edge_set_termios,
2962 .tiocmget = edge_tiocmget,
2963 .tiocmset = edge_tiocmset,
2964 .write = edge_write,
2965 .write_room = edge_write_room,
2966 .chars_in_buffer = edge_chars_in_buffer,
2967 .break_ctl = edge_break,
2968 .read_int_callback = edge_interrupt_callback,
2969 .read_bulk_callback = edge_bulk_in_callback,
2970 .write_bulk_callback = edge_bulk_out_callback,
2971};
2972
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -07002973static struct usb_serial_driver edgeport_2port_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07002974 .driver = {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07002975 .owner = THIS_MODULE,
2976 .name = "edgeport_ti_2",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07002977 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07002978 .description = "Edgeport TI 2 port adapter",
Johannes Hölzld9b1b782006-12-17 21:50:24 +01002979 .usb_driver = &io_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 .id_table = edgeport_2port_id_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 .num_ports = 2,
2982 .open = edge_open,
2983 .close = edge_close,
2984 .throttle = edge_throttle,
2985 .unthrottle = edge_unthrottle,
2986 .attach = edge_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04002987 .disconnect = edge_disconnect,
2988 .release = edge_release,
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04002989 .port_probe = edge_create_sysfs_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 .ioctl = edge_ioctl,
2991 .set_termios = edge_set_termios,
2992 .tiocmget = edge_tiocmget,
2993 .tiocmset = edge_tiocmset,
2994 .write = edge_write,
2995 .write_room = edge_write_room,
2996 .chars_in_buffer = edge_chars_in_buffer,
2997 .break_ctl = edge_break,
2998 .read_int_callback = edge_interrupt_callback,
2999 .read_bulk_callback = edge_bulk_in_callback,
3000 .write_bulk_callback = edge_bulk_out_callback,
3001};
3002
3003
3004static int __init edgeport_init(void)
3005{
3006 int retval;
3007 retval = usb_serial_register(&edgeport_1port_device);
3008 if (retval)
3009 goto failed_1port_device_register;
3010 retval = usb_serial_register(&edgeport_2port_device);
3011 if (retval)
3012 goto failed_2port_device_register;
3013 retval = usb_register(&io_driver);
Alan Cox2742fd82008-04-29 14:45:15 +01003014 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 goto failed_usb_register;
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -07003016 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
3017 DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 return 0;
3019failed_usb_register:
3020 usb_serial_deregister(&edgeport_2port_device);
3021failed_2port_device_register:
3022 usb_serial_deregister(&edgeport_1port_device);
3023failed_1port_device_register:
3024 return retval;
3025}
3026
Alan Cox2742fd82008-04-29 14:45:15 +01003027static void __exit edgeport_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028{
Alan Cox2742fd82008-04-29 14:45:15 +01003029 usb_deregister(&io_driver);
3030 usb_serial_deregister(&edgeport_1port_device);
3031 usb_serial_deregister(&edgeport_2port_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032}
3033
3034module_init(edgeport_init);
3035module_exit(edgeport_exit);
3036
3037/* Module information */
3038MODULE_AUTHOR(DRIVER_AUTHOR);
3039MODULE_DESCRIPTION(DRIVER_DESC);
3040MODULE_LICENSE("GPL");
Jaswinder Singhd12b2192008-07-04 23:06:09 +05303041MODULE_FIRMWARE("edgeport/down3.bin");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042
3043module_param(debug, bool, S_IRUGO | S_IWUSR);
3044MODULE_PARM_DESC(debug, "Debug enabled or not");
3045
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046module_param(closing_wait, int, S_IRUGO | S_IWUSR);
3047MODULE_PARM_DESC(closing_wait, "Maximum wait for data to drain, in .01 secs");
3048
3049module_param(ignore_cpu_rev, bool, S_IRUGO | S_IWUSR);
Alan Cox2742fd82008-04-29 14:45:15 +01003050MODULE_PARM_DESC(ignore_cpu_rev,
3051 "Ignore the cpu revision when connecting to a device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
Martin K. Petersenfc4cbd72007-05-22 15:57:04 -04003053module_param(default_uart_mode, int, S_IRUGO | S_IWUSR);
3054MODULE_PARM_DESC(default_uart_mode, "Default uart_mode, 0=RS232, ...");