blob: bdf8bd814a9aaf89d646545cac01da659ab475b6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Edgeport USB Serial Converter driver
3 *
4 * Copyright (C) 2000 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 * Edgeport/4
14 * Edgeport/4t
15 * Edgeport/2
16 * Edgeport/4i
17 * Edgeport/2i
18 * Edgeport/421
19 * Edgeport/21
20 * Rapidport/4
21 * Edgeport/8
22 * Edgeport/2D8
23 * Edgeport/4D8
24 * Edgeport/8i
25 *
26 * For questions or problems with this driver, contact Inside Out
27 * Networks technical support, or Peter Berger <pberger@brimson.com>,
28 * or Al Borchers <alborchers@steinerpoint.com>.
29 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 */
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/kernel.h>
33#include <linux/jiffies.h>
34#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/slab.h>
36#include <linux/tty.h>
37#include <linux/tty_driver.h>
38#include <linux/tty_flip.h>
39#include <linux/module.h>
40#include <linux/spinlock.h>
41#include <linux/serial.h>
42#include <linux/ioctl.h>
43#include <linux/wait.h>
Jaswinder Singh5b9ea932008-07-03 17:00:23 +053044#include <linux/firmware.h>
45#include <linux/ihex.h>
Alan Cox03f0dbf2008-07-22 11:16:34 +010046#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070048#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include "io_edgeport.h"
50#include "io_ionsp.h" /* info for the iosp messages */
51#include "io_16654.h" /* 16654 UART defines */
52
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 MAX_NAME_LEN 64
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#define OPEN_TIMEOUT (5*HZ) /* 5 seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Johan Hovold35d47972017-02-15 11:09:50 +010060static const struct usb_device_id edgeport_2port_id_table[] = {
61 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_2) },
62 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_2I) },
63 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_421) },
64 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_21) },
65 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_2_DIN) },
66 { }
67};
68
69static const struct usb_device_id edgeport_4port_id_table[] = {
70 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_4) },
71 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_RAPIDPORT_4) },
72 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_4T) },
73 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_MT4X56USB) },
74 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_4I) },
75 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU) },
76 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_4_DIN) },
77 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_22I) },
78 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_412_4) },
79 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_COMPATIBLE) },
80 { }
81};
82
83static const struct usb_device_id edgeport_8port_id_table[] = {
84 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_8) },
85 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU) },
86 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_8I) },
87 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_8R) },
88 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_8RR) },
89 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_412_8) },
90 { }
91};
92
93static const struct usb_device_id Epic_port_id_table[] = {
94 { USB_DEVICE(USB_VENDOR_ID_NCR, NCR_DEVICE_ID_EPIC_0202) },
95 { USB_DEVICE(USB_VENDOR_ID_NCR, NCR_DEVICE_ID_EPIC_0203) },
96 { USB_DEVICE(USB_VENDOR_ID_NCR, NCR_DEVICE_ID_EPIC_0310) },
97 { USB_DEVICE(USB_VENDOR_ID_NCR, NCR_DEVICE_ID_EPIC_0311) },
98 { USB_DEVICE(USB_VENDOR_ID_NCR, NCR_DEVICE_ID_EPIC_0312) },
99 { USB_DEVICE(USB_VENDOR_ID_AXIOHM, AXIOHM_DEVICE_ID_EPIC_A758) },
100 { USB_DEVICE(USB_VENDOR_ID_AXIOHM, AXIOHM_DEVICE_ID_EPIC_A794) },
101 { USB_DEVICE(USB_VENDOR_ID_AXIOHM, AXIOHM_DEVICE_ID_EPIC_A225) },
102 { }
103};
104
105/* Devices that this driver supports */
106static const struct usb_device_id id_table_combined[] = {
107 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_4) },
108 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_RAPIDPORT_4) },
109 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_4T) },
110 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_MT4X56USB) },
111 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_2) },
112 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_4I) },
113 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_2I) },
114 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_421) },
115 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_21) },
116 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU) },
117 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_8) },
118 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_2_DIN) },
119 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_4_DIN) },
120 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU) },
121 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_22I) },
122 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_412_4) },
123 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_COMPATIBLE) },
124 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_8I) },
125 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_8R) },
126 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_8RR) },
127 { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_EDGEPORT_412_8) },
128 { USB_DEVICE(USB_VENDOR_ID_NCR, NCR_DEVICE_ID_EPIC_0202) },
129 { USB_DEVICE(USB_VENDOR_ID_NCR, NCR_DEVICE_ID_EPIC_0203) },
130 { USB_DEVICE(USB_VENDOR_ID_NCR, NCR_DEVICE_ID_EPIC_0310) },
131 { USB_DEVICE(USB_VENDOR_ID_NCR, NCR_DEVICE_ID_EPIC_0311) },
132 { USB_DEVICE(USB_VENDOR_ID_NCR, NCR_DEVICE_ID_EPIC_0312) },
133 { USB_DEVICE(USB_VENDOR_ID_AXIOHM, AXIOHM_DEVICE_ID_EPIC_A758) },
134 { USB_DEVICE(USB_VENDOR_ID_AXIOHM, AXIOHM_DEVICE_ID_EPIC_A794) },
135 { USB_DEVICE(USB_VENDOR_ID_AXIOHM, AXIOHM_DEVICE_ID_EPIC_A225) },
136 { } /* Terminating entry */
137};
138
139MODULE_DEVICE_TABLE(usb, id_table_combined);
140
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142/* receive port state */
143enum RXSTATE {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100144 EXPECT_HDR1 = 0, /* Expect header byte 1 */
145 EXPECT_HDR2 = 1, /* Expect header byte 2 */
146 EXPECT_DATA = 2, /* Expect 'RxBytesRemaining' data */
147 EXPECT_HDR3 = 3, /* Expect header byte 3 (for status hdrs only) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148};
149
150
Alan Cox03f0dbf2008-07-22 11:16:34 +0100151/* Transmit Fifo
152 * This Transmit queue is an extension of the edgeport Rx buffer.
153 * The maximum amount of data buffered in both the edgeport
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 * Rx buffer (maxTxCredits) and this buffer will never exceed maxTxCredits.
155 */
156struct TxFifo {
157 unsigned int head; /* index to head pointer (write) */
158 unsigned int tail; /* index to tail pointer (read) */
159 unsigned int count; /* Bytes in queue */
160 unsigned int size; /* Max size of queue (equal to Max number of TxCredits) */
161 unsigned char *fifo; /* allocated Buffer */
162};
163
164/* This structure holds all of the local port information */
165struct edgeport_port {
166 __u16 txCredits; /* our current credits for this port */
167 __u16 maxTxCredits; /* the max size of the port */
168
169 struct TxFifo txfifo; /* transmit fifo -- size will be maxTxCredits */
170 struct urb *write_urb; /* write URB for this port */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100171 bool write_in_progress; /* 'true' while a write URB is outstanding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 spinlock_t ep_lock;
173
174 __u8 shadowLCR; /* last LCR value received */
175 __u8 shadowMCR; /* last MCR value received */
176 __u8 shadowMSR; /* last MSR value received */
177 __u8 shadowLSR; /* last LSR value received */
178 __u8 shadowXonChar; /* last value set as XON char in Edgeport */
179 __u8 shadowXoffChar; /* last value set as XOFF char in Edgeport */
180 __u8 validDataMask;
181 __u32 baudRate;
182
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100183 bool open;
184 bool openPending;
185 bool commandPending;
186 bool closePending;
187 bool chaseResponsePending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
190 wait_queue_head_t wait_open; /* for handling sleeping while waiting for open to finish */
191 wait_queue_head_t wait_command; /* for handling sleeping while waiting for command to finish */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 struct usb_serial_port *port; /* loop back to the owner of this object */
194};
195
196
197/* This structure holds all of the individual device information */
198struct edgeport_serial {
Pete Zaitcevad933752006-05-22 21:49:44 -0700199 char name[MAX_NAME_LEN+2]; /* string name of this device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 struct edge_manuf_descriptor manuf_descriptor; /* the manufacturer descriptor */
202 struct edge_boot_descriptor boot_descriptor; /* the boot firmware descriptor */
203 struct edgeport_product_info product_info; /* Product Info */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800204 struct edge_compatibility_descriptor epic_descriptor; /* Edgeport compatible descriptor */
205 int is_epic; /* flag if EPiC device or not */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 __u8 interrupt_in_endpoint; /* the interrupt endpoint handle */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100208 unsigned char *interrupt_in_buffer; /* the buffer we use for the interrupt endpoint */
209 struct urb *interrupt_read_urb; /* our interrupt urb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 __u8 bulk_in_endpoint; /* the bulk in endpoint handle */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100212 unsigned char *bulk_in_buffer; /* the buffer we use for the bulk in endpoint */
213 struct urb *read_urb; /* our bulk read urb */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100214 bool read_in_progress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 spinlock_t es_lock;
216
217 __u8 bulk_out_endpoint; /* the bulk out endpoint handle */
218
219 __s16 rxBytesAvail; /* the number of bytes that we need to read from this device */
220
221 enum RXSTATE rxState; /* the current state of the bulk receive processor */
222 __u8 rxHeader1; /* receive header byte 1 */
223 __u8 rxHeader2; /* receive header byte 2 */
224 __u8 rxHeader3; /* receive header byte 3 */
225 __u8 rxPort; /* the port that we are currently receiving data for */
226 __u8 rxStatusCode; /* the receive status code */
227 __u8 rxStatusParam; /* the receive status paramater */
228 __s16 rxBytesRemaining; /* the number of port bytes left to read */
229 struct usb_serial *serial; /* loop back to the owner of this object */
230};
231
232/* baud rate information */
233struct divisor_table_entry {
234 __u32 BaudRate;
235 __u16 Divisor;
236};
237
Alan Cox03f0dbf2008-07-22 11:16:34 +0100238/*
239 * Define table of divisors for Rev A EdgePort/4 hardware
240 * These assume a 3.6864MHz crystal, the standard /16, and
241 * MCR.7 = 0.
242 */
243
Arjan van de Ven4c4c9432005-11-29 09:43:42 +0100244static const struct divisor_table_entry divisor_table[] = {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100245 { 50, 4608},
246 { 75, 3072},
247 { 110, 2095}, /* 2094.545455 => 230450 => .0217 % over */
248 { 134, 1713}, /* 1713.011152 => 230398.5 => .00065% under */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 { 150, 1536},
250 { 300, 768},
251 { 600, 384},
252 { 1200, 192},
253 { 1800, 128},
254 { 2400, 96},
255 { 4800, 48},
256 { 7200, 32},
257 { 9600, 24},
258 { 14400, 16},
259 { 19200, 12},
260 { 38400, 6},
261 { 57600, 4},
262 { 115200, 2},
263 { 230400, 1},
264};
265
Greg Kroah-Hartman36ccce42012-02-28 13:11:51 -0800266/* Number of outstanding Command Write Urbs */
267static atomic_t CmdUrbs = ATOMIC_INIT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269
270/* local function prototypes */
271
272/* function prototypes for all URB callbacks */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100273static void edge_interrupt_callback(struct urb *urb);
274static void edge_bulk_in_callback(struct urb *urb);
275static void edge_bulk_out_data_callback(struct urb *urb);
276static void edge_bulk_out_cmd_callback(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278/* function prototypes for the usbserial callbacks */
Alan Coxa509a7e2009-09-19 13:13:26 -0700279static int edge_open(struct tty_struct *tty, struct usb_serial_port *port);
Alan Cox335f8512009-06-11 12:26:29 +0100280static void edge_close(struct usb_serial_port *port);
Alan Cox03f0dbf2008-07-22 11:16:34 +0100281static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
282 const unsigned char *buf, int count);
283static int edge_write_room(struct tty_struct *tty);
284static int edge_chars_in_buffer(struct tty_struct *tty);
285static void edge_throttle(struct tty_struct *tty);
286static void edge_unthrottle(struct tty_struct *tty);
287static void edge_set_termios(struct tty_struct *tty,
288 struct usb_serial_port *port,
289 struct ktermios *old_termios);
Alan Cox00a0d0d2011-02-14 16:27:06 +0000290static int edge_ioctl(struct tty_struct *tty,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100291 unsigned int cmd, unsigned long arg);
292static void edge_break(struct tty_struct *tty, int break_state);
Alan Cox60b33c12011-02-14 16:26:14 +0000293static int edge_tiocmget(struct tty_struct *tty);
Alan Cox20b9d172011-02-14 16:26:50 +0000294static int edge_tiocmset(struct tty_struct *tty,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100295 unsigned int set, unsigned int clear);
296static int edge_startup(struct usb_serial *serial);
Alan Sternf9c99bb2009-06-02 11:53:55 -0400297static void edge_disconnect(struct usb_serial *serial);
298static void edge_release(struct usb_serial *serial);
Johan Hovoldc27f3ef2012-10-17 13:34:57 +0200299static int edge_port_probe(struct usb_serial_port *port);
300static int edge_port_remove(struct usb_serial_port *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302/* function prototypes for all of our local functions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Alan Cox03f0dbf2008-07-22 11:16:34 +0100304static void process_rcvd_data(struct edgeport_serial *edge_serial,
305 unsigned char *buffer, __u16 bufferLength);
306static void process_rcvd_status(struct edgeport_serial *edge_serial,
307 __u8 byte2, __u8 byte3);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100308static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
309 int length);
Alan Cox03f0dbf2008-07-22 11:16:34 +0100310static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr);
311static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
312 __u8 lsr, __u8 data);
313static int send_iosp_ext_cmd(struct edgeport_port *edge_port, __u8 command,
314 __u8 param);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700315static int calc_baud_rate_divisor(struct device *dev, int baud_rate, int *divisor);
Alan Cox03f0dbf2008-07-22 11:16:34 +0100316static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
317 int baudRate);
318static void change_port_settings(struct tty_struct *tty,
319 struct edgeport_port *edge_port,
320 struct ktermios *old_termios);
321static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
322 __u8 regNum, __u8 regValue);
323static int write_cmd_usb(struct edgeport_port *edge_port,
324 unsigned char *buffer, int writeLength);
325static void send_more_port_data(struct edgeport_serial *edge_serial,
326 struct edgeport_port *edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Alan Cox03f0dbf2008-07-22 11:16:34 +0100328static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
329 __u16 length, const __u8 *data);
330static int rom_read(struct usb_serial *serial, __u16 extAddr, __u16 addr,
331 __u16 length, __u8 *data);
332static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
333 __u16 length, const __u8 *data);
334static void get_manufacturing_desc(struct edgeport_serial *edge_serial);
335static void get_boot_desc(struct edgeport_serial *edge_serial);
336static void load_application_firmware(struct edgeport_serial *edge_serial);
337
338static void unicode_to_ascii(char *string, int buflen,
339 __le16 *unicode, int unicode_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341
Alan Cox03f0dbf2008-07-22 11:16:34 +0100342/* ************************************************************************ */
343/* ************************************************************************ */
344/* ************************************************************************ */
345/* ************************************************************************ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347/************************************************************************
348 * *
349 * update_edgeport_E2PROM() Compare current versions of *
350 * Boot ROM and Manufacture *
351 * Descriptors with versions *
352 * embedded in this driver *
353 * *
354 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100355static void update_edgeport_E2PROM(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700357 struct device *dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 __u32 BootCurVer;
359 __u32 BootNewVer;
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530360 __u8 BootMajorVersion;
361 __u8 BootMinorVersion;
362 __u16 BootBuildNumber;
363 __u32 Bootaddr;
364 const struct ihex_binrec *rec;
365 const struct firmware *fw;
366 const char *fw_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 int response;
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 switch (edge_serial->product_info.iDownloadFile) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100370 case EDGE_DOWNLOAD_FILE_I930:
371 fw_name = "edgeport/boot.fw";
372 break;
373 case EDGE_DOWNLOAD_FILE_80251:
374 fw_name = "edgeport/boot2.fw";
375 break;
376 default:
377 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 }
379
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530380 response = request_ihex_firmware(&fw, fw_name,
381 &edge_serial->serial->dev->dev);
382 if (response) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700383 dev_err(dev, "Failed to load image \"%s\" err %d\n",
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530384 fw_name, response);
385 return;
386 }
387
388 rec = (const struct ihex_binrec *)fw->data;
389 BootMajorVersion = rec->data[0];
390 BootMinorVersion = rec->data[1];
391 BootBuildNumber = (rec->data[2] << 8) | rec->data[3];
392
Alan Cox03f0dbf2008-07-22 11:16:34 +0100393 /* Check Boot Image Version */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 BootCurVer = (edge_serial->boot_descriptor.MajorVersion << 24) +
395 (edge_serial->boot_descriptor.MinorVersion << 16) +
396 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber);
397
398 BootNewVer = (BootMajorVersion << 24) +
399 (BootMinorVersion << 16) +
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530400 BootBuildNumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700402 dev_dbg(dev, "Current Boot Image version %d.%d.%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 edge_serial->boot_descriptor.MajorVersion,
404 edge_serial->boot_descriptor.MinorVersion,
405 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
406
407
408 if (BootNewVer > BootCurVer) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700409 dev_dbg(dev, "**Update Boot Image from %d.%d.%d to %d.%d.%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 edge_serial->boot_descriptor.MajorVersion,
411 edge_serial->boot_descriptor.MinorVersion,
412 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber),
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530413 BootMajorVersion, BootMinorVersion, BootBuildNumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700415 dev_dbg(dev, "Downloading new Boot Image\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530417 for (rec = ihex_next_binrec(rec); rec;
418 rec = ihex_next_binrec(rec)) {
419 Bootaddr = be32_to_cpu(rec->addr);
420 response = rom_write(edge_serial->serial,
421 Bootaddr >> 16,
422 Bootaddr & 0xFFFF,
423 be16_to_cpu(rec->len),
424 &rec->data[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (response < 0) {
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530426 dev_err(&edge_serial->serial->dev->dev,
427 "rom_write failed (%x, %x, %d)\n",
428 Bootaddr >> 16, Bootaddr & 0xFFFF,
429 be16_to_cpu(rec->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 break;
431 }
432 }
433 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700434 dev_dbg(dev, "Boot Image -- already up to date\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530436 release_firmware(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439#if 0
440/************************************************************************
441 *
442 * Get string descriptor from device
443 *
444 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100445static int get_string_desc(struct usb_device *dev, int Id,
446 struct usb_string_descriptor **pRetDesc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
448 struct usb_string_descriptor StringDesc;
449 struct usb_string_descriptor *pStringDesc;
450
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700451 dev_dbg(&dev->dev, "%s - USB String ID = %d\n", __func__, Id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Alan Cox03f0dbf2008-07-22 11:16:34 +0100453 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc,
454 sizeof(StringDesc)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Alan Cox03f0dbf2008-07-22 11:16:34 +0100457 pStringDesc = kmalloc(StringDesc.bLength, GFP_KERNEL);
458 if (!pStringDesc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Alan Cox03f0dbf2008-07-22 11:16:34 +0100461 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc,
462 StringDesc.bLength)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 kfree(pStringDesc);
464 return -1;
465 }
466
467 *pRetDesc = pStringDesc;
468 return 0;
469}
470#endif
471
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700472static void dump_product_info(struct edgeport_serial *edge_serial,
473 struct edgeport_product_info *product_info)
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800474{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700475 struct device *dev = &edge_serial->serial->dev->dev;
476
Alan Cox03f0dbf2008-07-22 11:16:34 +0100477 /* Dump Product Info structure */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700478 dev_dbg(dev, "**Product Information:\n");
479 dev_dbg(dev, " ProductId %x\n", product_info->ProductId);
480 dev_dbg(dev, " NumPorts %d\n", product_info->NumPorts);
481 dev_dbg(dev, " ProdInfoVer %d\n", product_info->ProdInfoVer);
482 dev_dbg(dev, " IsServer %d\n", product_info->IsServer);
483 dev_dbg(dev, " IsRS232 %d\n", product_info->IsRS232);
484 dev_dbg(dev, " IsRS422 %d\n", product_info->IsRS422);
485 dev_dbg(dev, " IsRS485 %d\n", product_info->IsRS485);
486 dev_dbg(dev, " RomSize %d\n", product_info->RomSize);
487 dev_dbg(dev, " RamSize %d\n", product_info->RamSize);
488 dev_dbg(dev, " CpuRev %x\n", product_info->CpuRev);
489 dev_dbg(dev, " BoardRev %x\n", product_info->BoardRev);
490 dev_dbg(dev, " BootMajorVersion %d.%d.%d\n",
491 product_info->BootMajorVersion,
492 product_info->BootMinorVersion,
493 le16_to_cpu(product_info->BootBuildNumber));
494 dev_dbg(dev, " FirmwareMajorVersion %d.%d.%d\n",
495 product_info->FirmwareMajorVersion,
496 product_info->FirmwareMinorVersion,
497 le16_to_cpu(product_info->FirmwareBuildNumber));
498 dev_dbg(dev, " ManufactureDescDate %d/%d/%d\n",
499 product_info->ManufactureDescDate[0],
500 product_info->ManufactureDescDate[1],
501 product_info->ManufactureDescDate[2]+1900);
502 dev_dbg(dev, " iDownloadFile 0x%x\n",
503 product_info->iDownloadFile);
504 dev_dbg(dev, " EpicVer %d\n", product_info->EpicVer);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800505}
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507static void get_product_info(struct edgeport_serial *edge_serial)
508{
509 struct edgeport_product_info *product_info = &edge_serial->product_info;
510
Alan Cox03f0dbf2008-07-22 11:16:34 +0100511 memset(product_info, 0, sizeof(struct edgeport_product_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Alan Cox03f0dbf2008-07-22 11:16:34 +0100513 product_info->ProductId = (__u16)(le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct) & ~ION_DEVICE_ID_80251_NETCHIP);
514 product_info->NumPorts = edge_serial->manuf_descriptor.NumPorts;
515 product_info->ProdInfoVer = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Alan Cox03f0dbf2008-07-22 11:16:34 +0100517 product_info->RomSize = edge_serial->manuf_descriptor.RomSize;
518 product_info->RamSize = edge_serial->manuf_descriptor.RamSize;
519 product_info->CpuRev = edge_serial->manuf_descriptor.CpuRev;
520 product_info->BoardRev = edge_serial->manuf_descriptor.BoardRev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Alan Cox03f0dbf2008-07-22 11:16:34 +0100522 product_info->BootMajorVersion =
523 edge_serial->boot_descriptor.MajorVersion;
524 product_info->BootMinorVersion =
525 edge_serial->boot_descriptor.MinorVersion;
526 product_info->BootBuildNumber =
527 edge_serial->boot_descriptor.BuildNumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Alan Cox03f0dbf2008-07-22 11:16:34 +0100529 memcpy(product_info->ManufactureDescDate,
530 edge_serial->manuf_descriptor.DescDate,
531 sizeof(edge_serial->manuf_descriptor.DescDate));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Alan Cox03f0dbf2008-07-22 11:16:34 +0100533 /* check if this is 2nd generation hardware */
534 if (le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct)
535 & ION_DEVICE_ID_80251_NETCHIP)
536 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_80251;
537 else
538 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_I930;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700539
Alan Cox03f0dbf2008-07-22 11:16:34 +0100540 /* Determine Product type and set appropriate flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 switch (DEVICE_ID_FROM_USB_PRODUCT_ID(product_info->ProductId)) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100542 case ION_DEVICE_ID_EDGEPORT_COMPATIBLE:
543 case ION_DEVICE_ID_EDGEPORT_4T:
544 case ION_DEVICE_ID_EDGEPORT_4:
545 case ION_DEVICE_ID_EDGEPORT_2:
546 case ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU:
547 case ION_DEVICE_ID_EDGEPORT_8:
548 case ION_DEVICE_ID_EDGEPORT_421:
549 case ION_DEVICE_ID_EDGEPORT_21:
550 case ION_DEVICE_ID_EDGEPORT_2_DIN:
551 case ION_DEVICE_ID_EDGEPORT_4_DIN:
552 case ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU:
553 product_info->IsRS232 = 1;
554 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Alan Cox03f0dbf2008-07-22 11:16:34 +0100556 case ION_DEVICE_ID_EDGEPORT_2I: /* Edgeport/2 RS422/RS485 */
557 product_info->IsRS422 = 1;
558 product_info->IsRS485 = 1;
559 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Alan Cox03f0dbf2008-07-22 11:16:34 +0100561 case ION_DEVICE_ID_EDGEPORT_8I: /* Edgeport/4 RS422 */
562 case ION_DEVICE_ID_EDGEPORT_4I: /* Edgeport/4 RS422 */
563 product_info->IsRS422 = 1;
564 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
566
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700567 dump_product_info(edge_serial, product_info);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800568}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800570static int get_epic_descriptor(struct edgeport_serial *ep)
571{
572 int result;
573 struct usb_serial *serial = ep->serial;
574 struct edgeport_product_info *product_info = &ep->product_info;
Johan Hovolde4457d92017-01-12 14:56:13 +0100575 struct edge_compatibility_descriptor *epic;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800576 struct edge_compatibility_bits *bits;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700577 struct device *dev = &serial->dev->dev;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800578
579 ep->is_epic = 0;
Johan Hovolde4457d92017-01-12 14:56:13 +0100580
581 epic = kmalloc(sizeof(*epic), GFP_KERNEL);
582 if (!epic)
583 return -ENOMEM;
584
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800585 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
586 USB_REQUEST_ION_GET_EPIC_DESC,
587 0xC0, 0x00, 0x00,
Johan Hovolde4457d92017-01-12 14:56:13 +0100588 epic, sizeof(*epic),
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800589 300);
Johan Hovolde4457d92017-01-12 14:56:13 +0100590 if (result == sizeof(*epic)) {
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800591 ep->is_epic = 1;
Johan Hovolde4457d92017-01-12 14:56:13 +0100592 memcpy(&ep->epic_descriptor, epic, sizeof(*epic));
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800593 memset(product_info, 0, sizeof(struct edgeport_product_info));
594
Alan Cox03f0dbf2008-07-22 11:16:34 +0100595 product_info->NumPorts = epic->NumPorts;
596 product_info->ProdInfoVer = 0;
597 product_info->FirmwareMajorVersion = epic->MajorVersion;
598 product_info->FirmwareMinorVersion = epic->MinorVersion;
599 product_info->FirmwareBuildNumber = epic->BuildNumber;
600 product_info->iDownloadFile = epic->iDownloadFile;
601 product_info->EpicVer = epic->EpicVer;
602 product_info->Epic = epic->Supports;
603 product_info->ProductId = ION_DEVICE_ID_EDGEPORT_COMPATIBLE;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700604 dump_product_info(ep, product_info);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800605
606 bits = &ep->epic_descriptor.Supports;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700607 dev_dbg(dev, "**EPIC descriptor:\n");
608 dev_dbg(dev, " VendEnableSuspend: %s\n", bits->VendEnableSuspend ? "TRUE": "FALSE");
609 dev_dbg(dev, " IOSPOpen : %s\n", bits->IOSPOpen ? "TRUE": "FALSE");
610 dev_dbg(dev, " IOSPClose : %s\n", bits->IOSPClose ? "TRUE": "FALSE");
611 dev_dbg(dev, " IOSPChase : %s\n", bits->IOSPChase ? "TRUE": "FALSE");
612 dev_dbg(dev, " IOSPSetRxFlow : %s\n", bits->IOSPSetRxFlow ? "TRUE": "FALSE");
613 dev_dbg(dev, " IOSPSetTxFlow : %s\n", bits->IOSPSetTxFlow ? "TRUE": "FALSE");
614 dev_dbg(dev, " IOSPSetXChar : %s\n", bits->IOSPSetXChar ? "TRUE": "FALSE");
615 dev_dbg(dev, " IOSPRxCheck : %s\n", bits->IOSPRxCheck ? "TRUE": "FALSE");
616 dev_dbg(dev, " IOSPSetClrBreak : %s\n", bits->IOSPSetClrBreak ? "TRUE": "FALSE");
617 dev_dbg(dev, " IOSPWriteMCR : %s\n", bits->IOSPWriteMCR ? "TRUE": "FALSE");
618 dev_dbg(dev, " IOSPWriteLCR : %s\n", bits->IOSPWriteLCR ? "TRUE": "FALSE");
619 dev_dbg(dev, " IOSPSetBaudRate : %s\n", bits->IOSPSetBaudRate ? "TRUE": "FALSE");
620 dev_dbg(dev, " TrueEdgeport : %s\n", bits->TrueEdgeport ? "TRUE": "FALSE");
Johan Hovolde4457d92017-01-12 14:56:13 +0100621
622 result = 0;
623 } else if (result >= 0) {
624 dev_warn(&serial->interface->dev, "short epic descriptor received: %d\n",
625 result);
626 result = -EIO;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800627 }
628
Johan Hovolde4457d92017-01-12 14:56:13 +0100629 kfree(epic);
630
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800631 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
633
634
635/************************************************************************/
636/************************************************************************/
637/* U S B C A L L B A C K F U N C T I O N S */
638/* U S B C A L L B A C K F U N C T I O N S */
639/************************************************************************/
640/************************************************************************/
641
642/*****************************************************************************
643 * edge_interrupt_callback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100644 * this is the callback function for when we have received data on the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 * interrupt endpoint.
646 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100647static void edge_interrupt_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700649 struct edgeport_serial *edge_serial = urb->context;
650 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 struct edgeport_port *edge_port;
652 struct usb_serial_port *port;
653 unsigned char *data = urb->transfer_buffer;
654 int length = urb->actual_length;
655 int bytes_avail;
656 int position;
657 int txCredits;
658 int portNumber;
659 int result;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700660 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700662 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 case 0:
664 /* success */
665 break;
666 case -ECONNRESET:
667 case -ENOENT:
668 case -ESHUTDOWN:
669 /* this urb is terminated, clean up */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700670 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 return;
672 default:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700673 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 goto exit;
675 }
676
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700677 dev = &edge_serial->serial->dev->dev;
678
Alan Cox03f0dbf2008-07-22 11:16:34 +0100679 /* process this interrupt-read even if there are no ports open */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (length) {
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100681 usb_serial_debug_data(dev, __func__, length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 if (length > 1) {
684 bytes_avail = data[0] | (data[1] << 8);
685 if (bytes_avail) {
686 spin_lock(&edge_serial->es_lock);
687 edge_serial->rxBytesAvail += bytes_avail;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700688 dev_dbg(dev,
689 "%s - bytes_avail=%d, rxBytesAvail=%d, read_in_progress=%d\n",
690 __func__, bytes_avail,
691 edge_serial->rxBytesAvail,
692 edge_serial->read_in_progress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 if (edge_serial->rxBytesAvail > 0 &&
695 !edge_serial->read_in_progress) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700696 dev_dbg(dev, "%s - posting a read\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100697 edge_serial->read_in_progress = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Alan Cox03f0dbf2008-07-22 11:16:34 +0100699 /* we have pending bytes on the
700 bulk in pipe, send a request */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 result = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
702 if (result) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700703 dev_err(dev,
704 "%s - usb_submit_urb(read bulk) failed with result = %d\n",
705 __func__, result);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100706 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 }
708 }
709 spin_unlock(&edge_serial->es_lock);
710 }
711 }
712 /* grab the txcredits for the ports if available */
713 position = 2;
714 portNumber = 0;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100715 while ((position < length) &&
716 (portNumber < edge_serial->serial->num_ports)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 txCredits = data[position] | (data[position+1] << 8);
718 if (txCredits) {
719 port = edge_serial->serial->port[portNumber];
720 edge_port = usb_get_serial_port_data(port);
721 if (edge_port->open) {
722 spin_lock(&edge_port->ep_lock);
723 edge_port->txCredits += txCredits;
724 spin_unlock(&edge_port->ep_lock);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700725 dev_dbg(dev, "%s - txcredits for port%d = %d\n",
726 __func__, portNumber,
727 edge_port->txCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Alan Cox03f0dbf2008-07-22 11:16:34 +0100729 /* tell the tty driver that something
730 has changed */
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100731 tty_port_tty_wakeup(&edge_port->port->port);
Alan Cox03f0dbf2008-07-22 11:16:34 +0100732 /* Since we have more credit, check
733 if more data can be sent */
734 send_more_port_data(edge_serial,
735 edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
737 }
738 position += 2;
739 ++portNumber;
740 }
741 }
742
743exit:
Alan Cox03f0dbf2008-07-22 11:16:34 +0100744 result = usb_submit_urb(urb, GFP_ATOMIC);
745 if (result)
746 dev_err(&urb->dev->dev,
747 "%s - Error %d submitting control urb\n",
748 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749}
750
751
752/*****************************************************************************
753 * edge_bulk_in_callback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100754 * this is the callback function for when we have received data on the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 * bulk in endpoint.
756 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100757static void edge_bulk_in_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
Ming Leicdc97792008-02-24 18:41:47 +0800759 struct edgeport_serial *edge_serial = urb->context;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700760 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700762 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 __u16 raw_data_length;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700764 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700766 if (status) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700767 dev_dbg(&urb->dev->dev, "%s - nonzero read bulk status received: %d\n",
768 __func__, status);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100769 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 return;
771 }
772
773 if (urb->actual_length == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700774 dev_dbg(&urb->dev->dev, "%s - read bulk callback with no data\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100775 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return;
777 }
778
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700779 dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 raw_data_length = urb->actual_length;
781
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100782 usb_serial_debug_data(dev, __func__, raw_data_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 spin_lock(&edge_serial->es_lock);
785
786 /* decrement our rxBytes available by the number that we just got */
787 edge_serial->rxBytesAvail -= raw_data_length;
788
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700789 dev_dbg(dev, "%s - Received = %d, rxBytesAvail %d\n", __func__,
790 raw_data_length, edge_serial->rxBytesAvail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Alan Cox03f0dbf2008-07-22 11:16:34 +0100792 process_rcvd_data(edge_serial, data, urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 /* check to see if there's any more data for us to read */
795 if (edge_serial->rxBytesAvail > 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700796 dev_dbg(dev, "%s - posting a read\n", __func__);
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700797 retval = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
798 if (retval) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700799 dev_err(dev,
800 "%s - usb_submit_urb(read bulk) failed, retval = %d\n",
801 __func__, retval);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100802 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
804 } else {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100805 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
807
808 spin_unlock(&edge_serial->es_lock);
809}
810
811
812/*****************************************************************************
813 * edge_bulk_out_data_callback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100814 * this is the callback function for when we have finished sending
815 * serial data on the bulk out endpoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100817static void edge_bulk_out_data_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
Ming Leicdc97792008-02-24 18:41:47 +0800819 struct edgeport_port *edge_port = urb->context;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700820 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700822 if (status) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700823 dev_dbg(&urb->dev->dev,
824 "%s - nonzero write bulk status received: %d\n",
825 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 }
827
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100828 if (edge_port->open)
829 tty_port_tty_wakeup(&edge_port->port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Alan Cox03f0dbf2008-07-22 11:16:34 +0100831 /* Release the Write URB */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100832 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Alan Cox03f0dbf2008-07-22 11:16:34 +0100834 /* Check if more data needs to be sent */
835 send_more_port_data((struct edgeport_serial *)
836 (usb_get_serial_data(edge_port->port->serial)), edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
838
839
840/*****************************************************************************
841 * BulkOutCmdCallback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100842 * this is the callback function for when we have finished sending a
843 * command on the bulk out endpoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100845static void edge_bulk_out_cmd_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
Ming Leicdc97792008-02-24 18:41:47 +0800847 struct edgeport_port *edge_port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 int status = urb->status;
849
Oliver Neukum96c706e2007-03-15 15:27:17 +0100850 atomic_dec(&CmdUrbs);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700851 dev_dbg(&urb->dev->dev, "%s - FREE URB %p (outstanding %d)\n",
852 __func__, urb, atomic_read(&CmdUrbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854
855 /* clean up the transfer buffer */
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700856 kfree(urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 /* Free the command urb */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100859 usb_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 if (status) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700862 dev_dbg(&urb->dev->dev,
863 "%s - nonzero write bulk status received: %d\n",
864 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 return;
866 }
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 /* tell the tty driver that something has changed */
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100869 if (edge_port->open)
870 tty_port_tty_wakeup(&edge_port->port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 /* we have completed the command */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100873 edge_port->commandPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 wake_up(&edge_port->wait_command);
875}
876
877
878/*****************************************************************************
879 * Driver tty interface functions
880 *****************************************************************************/
881
882/*****************************************************************************
883 * SerialOpen
884 * this function is called by the tty driver when a port is opened
885 * If successful, we return 0
886 * Otherwise we return a negative error number.
887 *****************************************************************************/
Alan Coxa509a7e2009-09-19 13:13:26 -0700888static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
890 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700891 struct device *dev = &port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 struct usb_serial *serial;
893 struct edgeport_serial *edge_serial;
894 int response;
895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (edge_port == NULL)
897 return -ENODEV;
898
Alan Cox03f0dbf2008-07-22 11:16:34 +0100899 /* see if we've set up our endpoint info yet (can't set it up
900 in edge_startup as the structures were not set up at that time.) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 serial = port->serial;
902 edge_serial = usb_get_serial_data(serial);
Alan Cox95da3102008-07-22 11:09:07 +0100903 if (edge_serial == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (edge_serial->interrupt_in_buffer == NULL) {
906 struct usb_serial_port *port0 = serial->port[0];
Alan Cox03f0dbf2008-07-22 11:16:34 +0100907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 /* not set up yet, so do it now */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100909 edge_serial->interrupt_in_buffer =
910 port0->interrupt_in_buffer;
911 edge_serial->interrupt_in_endpoint =
912 port0->interrupt_in_endpointAddress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 edge_serial->interrupt_read_urb = port0->interrupt_in_urb;
914 edge_serial->bulk_in_buffer = port0->bulk_in_buffer;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100915 edge_serial->bulk_in_endpoint =
916 port0->bulk_in_endpointAddress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 edge_serial->read_urb = port0->read_urb;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100918 edge_serial->bulk_out_endpoint =
919 port0->bulk_out_endpointAddress;
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 /* set up our interrupt urb */
922 usb_fill_int_urb(edge_serial->interrupt_read_urb,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100923 serial->dev,
924 usb_rcvintpipe(serial->dev,
925 port0->interrupt_in_endpointAddress),
926 port0->interrupt_in_buffer,
927 edge_serial->interrupt_read_urb->transfer_buffer_length,
928 edge_interrupt_callback, edge_serial,
929 edge_serial->interrupt_read_urb->interval);
930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 /* set up our bulk in urb */
932 usb_fill_bulk_urb(edge_serial->read_urb, serial->dev,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100933 usb_rcvbulkpipe(serial->dev,
934 port0->bulk_in_endpointAddress),
935 port0->bulk_in_buffer,
936 edge_serial->read_urb->transfer_buffer_length,
937 edge_bulk_in_callback, edge_serial);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100938 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 /* start interrupt read for this edgeport
Alan Cox03f0dbf2008-07-22 11:16:34 +0100941 * this interrupt will continue as long
942 * as the edgeport is connected */
943 response = usb_submit_urb(edge_serial->interrupt_read_urb,
944 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 if (response) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700946 dev_err(dev, "%s - Error %d submitting control urb\n",
947 __func__, response);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 }
949 }
Alan Cox03f0dbf2008-07-22 11:16:34 +0100950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 /* initialize our wait queues */
952 init_waitqueue_head(&edge_port->wait_open);
953 init_waitqueue_head(&edge_port->wait_chase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 init_waitqueue_head(&edge_port->wait_command);
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 /* initialize our port settings */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100957 edge_port->txCredits = 0; /* Can't send any data yet */
958 /* Must always set this bit to enable ints! */
959 edge_port->shadowMCR = MCR_MASTER_IE;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100960 edge_port->chaseResponsePending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962 /* send a open port command */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100963 edge_port->openPending = true;
964 edge_port->open = false;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100965 response = send_iosp_ext_cmd(edge_port, IOSP_CMD_OPEN_PORT, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 if (response < 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700968 dev_err(dev, "%s - error sending open port command\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100969 edge_port->openPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 return -ENODEV;
971 }
972
973 /* now wait for the port to be completely opened */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100974 wait_event_timeout(edge_port->wait_open, !edge_port->openPending,
975 OPEN_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100977 if (!edge_port->open) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 /* open timed out */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700979 dev_dbg(dev, "%s - open timedout\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100980 edge_port->openPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 return -ENODEV;
982 }
983
984 /* create the txfifo */
985 edge_port->txfifo.head = 0;
986 edge_port->txfifo.tail = 0;
987 edge_port->txfifo.count = 0;
988 edge_port->txfifo.size = edge_port->maxTxCredits;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100989 edge_port->txfifo.fifo = kmalloc(edge_port->maxTxCredits, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 if (!edge_port->txfifo.fifo) {
Alan Cox335f8512009-06-11 12:26:29 +0100992 edge_close(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 return -ENOMEM;
994 }
995
996 /* Allocate a URB for the write */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100997 edge_port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100998 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
1000 if (!edge_port->write_urb) {
Alan Cox335f8512009-06-11 12:26:29 +01001001 edge_close(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 return -ENOMEM;
1003 }
1004
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001005 dev_dbg(dev, "%s - Initialize TX fifo to %d bytes\n",
1006 __func__, edge_port->maxTxCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
1008 return 0;
1009}
1010
1011
1012/************************************************************************
1013 *
1014 * block_until_chase_response
1015 *
1016 * This function will block the close until one of the following:
1017 * 1. Response to our Chase comes from Edgeport
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001018 * 2. A timeout of 10 seconds without activity has expired
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 * (1K of Edgeport data @ 2400 baud ==> 4 sec to empty)
1020 *
1021 ************************************************************************/
1022static void block_until_chase_response(struct edgeport_port *edge_port)
1023{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001024 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 DEFINE_WAIT(wait);
1026 __u16 lastCredits;
1027 int timeout = 1*HZ;
1028 int loop = 10;
1029
1030 while (1) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001031 /* Save Last credits */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 lastCredits = edge_port->txCredits;
1033
Alan Cox03f0dbf2008-07-22 11:16:34 +01001034 /* Did we get our Chase response */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001035 if (!edge_port->chaseResponsePending) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001036 dev_dbg(dev, "%s - Got Chase Response\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Alan Cox03f0dbf2008-07-22 11:16:34 +01001038 /* did we get all of our credit back? */
1039 if (edge_port->txCredits == edge_port->maxTxCredits) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001040 dev_dbg(dev, "%s - Got all credits\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 return;
1042 }
1043 }
1044
Alan Cox03f0dbf2008-07-22 11:16:34 +01001045 /* Block the thread for a while */
1046 prepare_to_wait(&edge_port->wait_chase, &wait,
1047 TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 schedule_timeout(timeout);
1049 finish_wait(&edge_port->wait_chase, &wait);
1050
1051 if (lastCredits == edge_port->txCredits) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001052 /* No activity.. count down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 loop--;
1054 if (loop == 0) {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001055 edge_port->chaseResponsePending = false;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001056 dev_dbg(dev, "%s - Chase TIMEOUT\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 return;
1058 }
1059 } else {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001060 /* Reset timeout value back to 10 seconds */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001061 dev_dbg(dev, "%s - Last %d, Current %d\n", __func__,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001062 lastCredits, edge_port->txCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 loop = 10;
1064 }
1065 }
1066}
1067
1068
1069/************************************************************************
1070 *
1071 * block_until_tx_empty
1072 *
1073 * This function will block the close until one of the following:
1074 * 1. TX count are 0
1075 * 2. The edgeport has stopped
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001076 * 3. A timeout of 3 seconds without activity has expired
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 *
1078 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001079static void block_until_tx_empty(struct edgeport_port *edge_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001081 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 DEFINE_WAIT(wait);
1083 struct TxFifo *fifo = &edge_port->txfifo;
1084 __u32 lastCount;
1085 int timeout = HZ/10;
1086 int loop = 30;
1087
1088 while (1) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001089 /* Save Last count */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 lastCount = fifo->count;
1091
Alan Cox03f0dbf2008-07-22 11:16:34 +01001092 /* Is the Edgeport Buffer empty? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 if (lastCount == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001094 dev_dbg(dev, "%s - TX Buffer Empty\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 return;
1096 }
1097
Alan Cox03f0dbf2008-07-22 11:16:34 +01001098 /* Block the thread for a while */
1099 prepare_to_wait(&edge_port->wait_chase, &wait,
1100 TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 schedule_timeout(timeout);
1102 finish_wait(&edge_port->wait_chase, &wait);
1103
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001104 dev_dbg(dev, "%s wait\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
1106 if (lastCount == fifo->count) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001107 /* No activity.. count down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 loop--;
1109 if (loop == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001110 dev_dbg(dev, "%s - TIMEOUT\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 return;
1112 }
1113 } else {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001114 /* Reset timeout value back to seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 loop = 30;
1116 }
1117 }
1118}
1119
1120
1121/*****************************************************************************
1122 * edge_close
1123 * this function is called by the tty driver when a port is closed
1124 *****************************************************************************/
Alan Cox335f8512009-06-11 12:26:29 +01001125static void edge_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
1127 struct edgeport_serial *edge_serial;
1128 struct edgeport_port *edge_port;
1129 int status;
1130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 edge_serial = usb_get_serial_data(port->serial);
1132 edge_port = usb_get_serial_port_data(port);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001133 if (edge_serial == NULL || edge_port == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 return;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001135
1136 /* block until tx is empty */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 block_until_tx_empty(edge_port);
1138
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001139 edge_port->closePending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Geyslan G. Bem232dce82015-12-11 06:46:41 -03001141 if (!edge_serial->is_epic ||
1142 edge_serial->epic_descriptor.Supports.IOSPChase) {
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001143 /* flush and chase */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001144 edge_port->chaseResponsePending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001146 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001147 status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
1148 if (status == 0)
1149 /* block until chase finished */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001150 block_until_chase_response(edge_port);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001151 else
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001152 edge_port->chaseResponsePending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 }
1154
Geyslan G. Bem232dce82015-12-11 06:46:41 -03001155 if (!edge_serial->is_epic ||
1156 edge_serial->epic_descriptor.Supports.IOSPClose) {
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001157 /* close the port */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001158 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CLOSE_PORT\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001159 send_iosp_ext_cmd(edge_port, IOSP_CMD_CLOSE_PORT, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Alan Cox03f0dbf2008-07-22 11:16:34 +01001162 /* port->close = true; */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001163 edge_port->closePending = false;
1164 edge_port->open = false;
1165 edge_port->openPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Mariusz Kozlowski9a25f442006-11-08 15:36:29 +01001167 usb_kill_urb(edge_port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169 if (edge_port->write_urb) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001170 /* if this urb had a transfer buffer already
1171 (old transfer) free it */
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001172 kfree(edge_port->write_urb->transfer_buffer);
1173 usb_free_urb(edge_port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 edge_port->write_urb = NULL;
1175 }
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001176 kfree(edge_port->txfifo.fifo);
1177 edge_port->txfifo.fifo = NULL;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001178}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
1180/*****************************************************************************
1181 * SerialWrite
Alan Cox03f0dbf2008-07-22 11:16:34 +01001182 * this function is called by the tty driver when data should be written
1183 * to the port.
1184 * If successful, we return the number of bytes written, otherwise we
1185 * return a negative error number.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001187static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
1188 const unsigned char *data, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189{
1190 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1191 struct TxFifo *fifo;
1192 int copySize;
1193 int bytesleft;
1194 int firsthalf;
1195 int secondhalf;
1196 unsigned long flags;
1197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 if (edge_port == NULL)
1199 return -ENODEV;
1200
Alan Cox03f0dbf2008-07-22 11:16:34 +01001201 /* get a pointer to the Tx fifo */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 fifo = &edge_port->txfifo;
1203
1204 spin_lock_irqsave(&edge_port->ep_lock, flags);
1205
Alan Cox03f0dbf2008-07-22 11:16:34 +01001206 /* calculate number of bytes to put in fifo */
1207 copySize = min((unsigned int)count,
1208 (edge_port->txCredits - fifo->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001210 dev_dbg(&port->dev, "%s of %d byte(s) Fifo room %d -- will copy %d bytes\n",
1211 __func__, count, edge_port->txCredits - fifo->count, copySize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Alan Cox03f0dbf2008-07-22 11:16:34 +01001213 /* catch writes of 0 bytes which the tty driver likes to give us,
1214 and when txCredits is empty */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 if (copySize == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001216 dev_dbg(&port->dev, "%s - copySize = Zero\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 goto finish_write;
1218 }
1219
Alan Cox03f0dbf2008-07-22 11:16:34 +01001220 /* queue the data
1221 * since we can never overflow the buffer we do not have to check for a
1222 * full condition
1223 *
1224 * the copy is done is two parts -- first fill to the end of the buffer
1225 * then copy the reset from the start of the buffer
1226 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 bytesleft = fifo->size - fifo->head;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001228 firsthalf = min(bytesleft, copySize);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001229 dev_dbg(&port->dev, "%s - copy %d bytes of %d into fifo \n", __func__,
1230 firsthalf, bytesleft);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 /* now copy our data */
1233 memcpy(&fifo->fifo[fifo->head], data, firsthalf);
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001234 usb_serial_debug_data(&port->dev, __func__, firsthalf, &fifo->fifo[fifo->head]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Alan Cox03f0dbf2008-07-22 11:16:34 +01001236 /* update the index and size */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 fifo->head += firsthalf;
1238 fifo->count += firsthalf;
1239
Alan Cox03f0dbf2008-07-22 11:16:34 +01001240 /* wrap the index */
1241 if (fifo->head == fifo->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 fifo->head = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 secondhalf = copySize-firsthalf;
1245
1246 if (secondhalf) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001247 dev_dbg(&port->dev, "%s - copy rest of data %d\n", __func__, secondhalf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 memcpy(&fifo->fifo[fifo->head], &data[firsthalf], secondhalf);
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001249 usb_serial_debug_data(&port->dev, __func__, secondhalf, &fifo->fifo[fifo->head]);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001250 /* update the index and size */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 fifo->count += secondhalf;
1252 fifo->head += secondhalf;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001253 /* No need to check for wrap since we can not get to end of
1254 * the fifo in this part
1255 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 }
1257
1258finish_write:
1259 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1260
Alan Cox03f0dbf2008-07-22 11:16:34 +01001261 send_more_port_data((struct edgeport_serial *)
1262 usb_get_serial_data(port->serial), edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001264 dev_dbg(&port->dev, "%s wrote %d byte(s) TxCredits %d, Fifo %d\n",
1265 __func__, copySize, edge_port->txCredits, fifo->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Alan Cox03f0dbf2008-07-22 11:16:34 +01001267 return copySize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268}
1269
1270
1271/************************************************************************
1272 *
1273 * send_more_port_data()
1274 *
1275 * This routine attempts to write additional UART transmit data
1276 * to a port over the USB bulk pipe. It is called (1) when new
1277 * data has been written to a port's TxBuffer from higher layers
1278 * (2) when the peripheral sends us additional TxCredits indicating
1279 * that it can accept more Tx data for a given port; and (3) when
1280 * a bulk write completes successfully and we want to see if we
1281 * can transmit more.
1282 *
1283 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001284static void send_more_port_data(struct edgeport_serial *edge_serial,
1285 struct edgeport_port *edge_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
1287 struct TxFifo *fifo = &edge_port->txfifo;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001288 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 struct urb *urb;
1290 unsigned char *buffer;
1291 int status;
1292 int count;
1293 int bytesleft;
1294 int firsthalf;
1295 int secondhalf;
1296 unsigned long flags;
1297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 spin_lock_irqsave(&edge_port->ep_lock, flags);
1299
1300 if (edge_port->write_in_progress ||
1301 !edge_port->open ||
1302 (fifo->count == 0)) {
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001303 dev_dbg(dev, "%s EXIT - fifo %d, PendingWrite = %d\n",
1304 __func__, fifo->count, edge_port->write_in_progress);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 goto exit_send;
1306 }
1307
Alan Cox03f0dbf2008-07-22 11:16:34 +01001308 /* since the amount of data in the fifo will always fit into the
1309 * edgeport buffer we do not need to check the write length
1310 *
1311 * Do we have enough credits for this port to make it worthwhile
1312 * to bother queueing a write. If it's too small, say a few bytes,
1313 * it's better to wait for more credits so we can do a larger write.
1314 */
1315 if (edge_port->txCredits < EDGE_FW_GET_TX_CREDITS_SEND_THRESHOLD(edge_port->maxTxCredits, EDGE_FW_BULK_MAX_PACKET_SIZE)) {
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001316 dev_dbg(dev, "%s Not enough credit - fifo %d TxCredit %d\n",
1317 __func__, fifo->count, edge_port->txCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 goto exit_send;
1319 }
1320
Alan Cox03f0dbf2008-07-22 11:16:34 +01001321 /* lock this write */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001322 edge_port->write_in_progress = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Alan Cox03f0dbf2008-07-22 11:16:34 +01001324 /* get a pointer to the write_urb */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 urb = edge_port->write_urb;
1326
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001327 /* make sure transfer buffer is freed */
1328 kfree(urb->transfer_buffer);
1329 urb->transfer_buffer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Alan Cox03f0dbf2008-07-22 11:16:34 +01001331 /* build the data header for the buffer and port that we are about
1332 to send out */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 count = fifo->count;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001334 buffer = kmalloc(count+2, GFP_ATOMIC);
Johan Hovold10c642d2013-12-29 19:22:56 +01001335 if (!buffer) {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001336 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 goto exit_send;
1338 }
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001339 buffer[0] = IOSP_BUILD_DATA_HDR1(edge_port->port->port_number, count);
1340 buffer[1] = IOSP_BUILD_DATA_HDR2(edge_port->port->port_number, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 /* now copy our data */
1343 bytesleft = fifo->size - fifo->tail;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001344 firsthalf = min(bytesleft, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 memcpy(&buffer[2], &fifo->fifo[fifo->tail], firsthalf);
1346 fifo->tail += firsthalf;
1347 fifo->count -= firsthalf;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001348 if (fifo->tail == fifo->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 fifo->tail = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
1351 secondhalf = count-firsthalf;
1352 if (secondhalf) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001353 memcpy(&buffer[2+firsthalf], &fifo->fifo[fifo->tail],
1354 secondhalf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 fifo->tail += secondhalf;
1356 fifo->count -= secondhalf;
1357 }
1358
1359 if (count)
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001360 usb_serial_debug_data(&edge_port->port->dev, __func__, count, &buffer[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362 /* fill up the urb with all of our data and submit it */
Alan Cox03f0dbf2008-07-22 11:16:34 +01001363 usb_fill_bulk_urb(urb, edge_serial->serial->dev,
1364 usb_sndbulkpipe(edge_serial->serial->dev,
1365 edge_serial->bulk_out_endpoint),
1366 buffer, count+2,
1367 edge_bulk_out_data_callback, edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
1369 /* decrement the number of credits we have by the number we just sent */
1370 edge_port->txCredits -= count;
Johan Hovoldd36a7712013-03-21 12:37:07 +01001371 edge_port->port->icount.tx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 status = usb_submit_urb(urb, GFP_ATOMIC);
1374 if (status) {
1375 /* something went wrong */
Johan Hovold22a416c2012-02-10 13:20:51 +01001376 dev_err_console(edge_port->port,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001377 "%s - usb_submit_urb(write bulk) failed, status = %d, data lost\n",
1378 __func__, status);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001379 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
1381 /* revert the credits as something bad happened. */
1382 edge_port->txCredits += count;
Johan Hovoldd36a7712013-03-21 12:37:07 +01001383 edge_port->port->icount.tx -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 }
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001385 dev_dbg(dev, "%s wrote %d byte(s) TxCredit %d, Fifo %d\n",
1386 __func__, count, edge_port->txCredits, fifo->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
1388exit_send:
1389 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1390}
1391
1392
1393/*****************************************************************************
1394 * edge_write_room
Alan Cox03f0dbf2008-07-22 11:16:34 +01001395 * this function is called by the tty driver when it wants to know how
1396 * many bytes of data we can accept for a specific port. If successful,
1397 * we return the amount of room that we have for this port (the txCredits)
1398 * otherwise we return a negative error number.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001400static int edge_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
Alan Cox95da3102008-07-22 11:09:07 +01001402 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1404 int room;
1405 unsigned long flags;
1406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 if (edge_port == NULL)
Alan Coxd76f2f42008-07-22 11:16:42 +01001408 return 0;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001409 if (edge_port->closePending)
Alan Coxd76f2f42008-07-22 11:16:42 +01001410 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001413 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Alan Coxd76f2f42008-07-22 11:16:42 +01001414 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 }
1416
Alan Cox03f0dbf2008-07-22 11:16:34 +01001417 /* total of both buffers is still txCredit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 spin_lock_irqsave(&edge_port->ep_lock, flags);
1419 room = edge_port->txCredits - edge_port->txfifo.count;
1420 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1421
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001422 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 return room;
1424}
1425
1426
1427/*****************************************************************************
1428 * edge_chars_in_buffer
Alan Cox03f0dbf2008-07-22 11:16:34 +01001429 * this function is called by the tty driver when it wants to know how
1430 * many bytes of data we currently have outstanding in the port (data that
1431 * has been written, but hasn't made it out the port yet)
1432 * If successful, we return the number of bytes left to be written in the
1433 * system,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 * Otherwise we return a negative error number.
1435 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001436static int edge_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
Alan Cox95da3102008-07-22 11:09:07 +01001438 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1440 int num_chars;
1441 unsigned long flags;
1442
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 if (edge_port == NULL)
Alan Coxd76f2f42008-07-22 11:16:42 +01001444 return 0;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001445 if (edge_port->closePending)
Alan Coxd76f2f42008-07-22 11:16:42 +01001446 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
1448 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001449 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Alan Coxd76f2f42008-07-22 11:16:42 +01001450 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 }
1452
1453 spin_lock_irqsave(&edge_port->ep_lock, flags);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001454 num_chars = edge_port->maxTxCredits - edge_port->txCredits +
1455 edge_port->txfifo.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1457 if (num_chars) {
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001458 dev_dbg(&port->dev, "%s - returns %d\n", __func__, num_chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 }
1460
1461 return num_chars;
1462}
1463
1464
1465/*****************************************************************************
1466 * SerialThrottle
1467 * this function is called by the tty driver when it wants to stop the data
1468 * being read from the port.
1469 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001470static void edge_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
Alan Cox95da3102008-07-22 11:09:07 +01001472 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 int status;
1475
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 if (edge_port == NULL)
1477 return;
1478
1479 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001480 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 return;
1482 }
1483
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 /* if we are implementing XON/XOFF, send the stop character */
1485 if (I_IXOFF(tty)) {
1486 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001487 status = edge_write(tty, port, &stop_char, 1);
1488 if (status <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 }
1491
1492 /* if we are implementing RTS/CTS, toggle that line */
Peter Hurley9db276f2016-01-10 20:36:15 -08001493 if (C_CRTSCTS(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 edge_port->shadowMCR &= ~MCR_RTS;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001495 status = send_cmd_write_uart_register(edge_port, MCR,
1496 edge_port->shadowMCR);
1497 if (status != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500}
1501
1502
1503/*****************************************************************************
1504 * edge_unthrottle
Alan Cox03f0dbf2008-07-22 11:16:34 +01001505 * this function is called by the tty driver when it wants to resume the
1506 * data being read from the port (called after SerialThrottle is called)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001508static void edge_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
Alan Cox95da3102008-07-22 11:09:07 +01001510 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 int status;
1513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 if (edge_port == NULL)
1515 return;
1516
1517 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001518 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 return;
1520 }
1521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 /* if we are implementing XON/XOFF, send the start character */
1523 if (I_IXOFF(tty)) {
1524 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001525 status = edge_write(tty, port, &start_char, 1);
1526 if (status <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 /* if we are implementing RTS/CTS, toggle that line */
Peter Hurley9db276f2016-01-10 20:36:15 -08001530 if (C_CRTSCTS(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 edge_port->shadowMCR |= MCR_RTS;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001532 send_cmd_write_uart_register(edge_port, MCR,
1533 edge_port->shadowMCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535}
1536
1537
1538/*****************************************************************************
1539 * SerialSetTermios
Alan Cox03f0dbf2008-07-22 11:16:34 +01001540 * this function is called by the tty driver when it wants to change
1541 * the termios structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001543static void edge_set_termios(struct tty_struct *tty,
1544 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545{
1546 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
1548 if (edge_port == NULL)
1549 return;
1550
1551 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001552 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 return;
1554 }
1555
1556 /* change the port settings to the new ones specified */
Alan Cox95da3102008-07-22 11:09:07 +01001557 change_port_settings(tty, edge_port, old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558}
1559
1560
1561/*****************************************************************************
1562 * get_lsr_info - get line status register info
1563 *
1564 * Purpose: Let user call ioctl() to get info when the UART physically
1565 * is emptied. On bus types like RS485, the transmitter must
1566 * release the bus after transmitting. This must be done when
1567 * the transmit shift register is empty, not be done when the
1568 * transmit holding register is empty. This functionality
Alan Cox03f0dbf2008-07-22 11:16:34 +01001569 * allows an RS485 driver to be written in user space.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001571static int get_lsr_info(struct edgeport_port *edge_port,
1572 unsigned int __user *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573{
1574 unsigned int result = 0;
1575 unsigned long flags;
1576
1577 spin_lock_irqsave(&edge_port->ep_lock, flags);
1578 if (edge_port->maxTxCredits == edge_port->txCredits &&
1579 edge_port->txfifo.count == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001580 dev_dbg(&edge_port->port->dev, "%s -- Empty\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 result = TIOCSER_TEMT;
1582 }
1583 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1584
1585 if (copy_to_user(value, &result, sizeof(int)))
1586 return -EFAULT;
1587 return 0;
1588}
1589
Alan Cox20b9d172011-02-14 16:26:50 +00001590static int edge_tiocmset(struct tty_struct *tty,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001591 unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592{
Alan Cox95da3102008-07-22 11:09:07 +01001593 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1595 unsigned int mcr;
1596
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 mcr = edge_port->shadowMCR;
1598 if (set & TIOCM_RTS)
1599 mcr |= MCR_RTS;
1600 if (set & TIOCM_DTR)
1601 mcr |= MCR_DTR;
1602 if (set & TIOCM_LOOP)
1603 mcr |= MCR_LOOPBACK;
1604
1605 if (clear & TIOCM_RTS)
1606 mcr &= ~MCR_RTS;
1607 if (clear & TIOCM_DTR)
1608 mcr &= ~MCR_DTR;
1609 if (clear & TIOCM_LOOP)
1610 mcr &= ~MCR_LOOPBACK;
1611
1612 edge_port->shadowMCR = mcr;
1613
1614 send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1615
1616 return 0;
1617}
1618
Alan Cox60b33c12011-02-14 16:26:14 +00001619static int edge_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
Alan Cox95da3102008-07-22 11:09:07 +01001621 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1623 unsigned int result = 0;
1624 unsigned int msr;
1625 unsigned int mcr;
1626
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 msr = edge_port->shadowMSR;
1628 mcr = edge_port->shadowMCR;
1629 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
1630 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
1631 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
1632 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
1633 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
1634 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
1635
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 return result;
1637}
1638
Alan Cox03f0dbf2008-07-22 11:16:34 +01001639static int get_serial_info(struct edgeport_port *edge_port,
1640 struct serial_struct __user *retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641{
1642 struct serial_struct tmp;
1643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 memset(&tmp, 0, sizeof(tmp));
1645
1646 tmp.type = PORT_16550A;
Greg Kroah-Hartmane5b1e202013-06-07 11:04:28 -07001647 tmp.line = edge_port->port->minor;
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001648 tmp.port = edge_port->port->port_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 tmp.irq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 tmp.xmit_fifo_size = edge_port->maxTxCredits;
1651 tmp.baud_base = 9600;
1652 tmp.close_delay = 5*HZ;
1653 tmp.closing_wait = 30*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
1655 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1656 return -EFAULT;
1657 return 0;
1658}
1659
1660
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661/*****************************************************************************
1662 * SerialIoctl
1663 * this function handles any ioctl calls to the driver
1664 *****************************************************************************/
Alan Cox00a0d0d2011-02-14 16:27:06 +00001665static int edge_ioctl(struct tty_struct *tty,
Alan Cox95da3102008-07-22 11:09:07 +01001666 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667{
Alan Cox95da3102008-07-22 11:09:07 +01001668 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 DEFINE_WAIT(wait);
1670 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 switch (cmd) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001673 case TIOCSERGETLSR:
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001674 dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001675 return get_lsr_info(edge_port, (unsigned int __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Alan Cox03f0dbf2008-07-22 11:16:34 +01001677 case TIOCGSERIAL:
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001678 dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001679 return get_serial_info(edge_port, (struct serial_struct __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 return -ENOIOCTLCMD;
1682}
1683
1684
1685/*****************************************************************************
1686 * SerialBreak
1687 * this function sends a break to the port
1688 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001689static void edge_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690{
Alan Cox95da3102008-07-22 11:09:07 +01001691 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001693 struct edgeport_serial *edge_serial = usb_get_serial_data(port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 int status;
1695
Geyslan G. Bem232dce82015-12-11 06:46:41 -03001696 if (!edge_serial->is_epic ||
1697 edge_serial->epic_descriptor.Supports.IOSPChase) {
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001698 /* flush and chase */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001699 edge_port->chaseResponsePending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001701 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001702 status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001703 if (status == 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001704 /* block until chase finished */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001705 block_until_chase_response(edge_port);
1706 } else {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001707 edge_port->chaseResponsePending = false;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 }
1710
Geyslan G. Bem232dce82015-12-11 06:46:41 -03001711 if (!edge_serial->is_epic ||
1712 edge_serial->epic_descriptor.Supports.IOSPSetClrBreak) {
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001713 if (break_state == -1) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001714 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_SET_BREAK\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001715 status = send_iosp_ext_cmd(edge_port,
1716 IOSP_CMD_SET_BREAK, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001717 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001718 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CLEAR_BREAK\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001719 status = send_iosp_ext_cmd(edge_port,
1720 IOSP_CMD_CLEAR_BREAK, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001721 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01001722 if (status)
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001723 dev_dbg(&port->dev, "%s - error sending break set/clear command.\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01001724 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726}
1727
1728
1729/*****************************************************************************
1730 * process_rcvd_data
1731 * this function handles the data received on the bulk in pipe.
1732 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001733static void process_rcvd_data(struct edgeport_serial *edge_serial,
1734 unsigned char *buffer, __u16 bufferLength)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001736 struct device *dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 struct usb_serial_port *port;
1738 struct edgeport_port *edge_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 __u16 lastBufferLength;
1740 __u16 rxLen;
1741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 lastBufferLength = bufferLength + 1;
1743
1744 while (bufferLength > 0) {
1745 /* failsafe incase we get a message that we don't understand */
1746 if (lastBufferLength == bufferLength) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001747 dev_dbg(dev, "%s - stuck in loop, exiting it.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 break;
1749 }
1750 lastBufferLength = bufferLength;
1751
1752 switch (edge_serial->rxState) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001753 case EXPECT_HDR1:
1754 edge_serial->rxHeader1 = *buffer;
1755 ++buffer;
1756 --bufferLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
Alan Cox03f0dbf2008-07-22 11:16:34 +01001758 if (bufferLength == 0) {
1759 edge_serial->rxState = EXPECT_HDR2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 break;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001761 }
1762 /* otherwise, drop on through */
1763 case EXPECT_HDR2:
1764 edge_serial->rxHeader2 = *buffer;
1765 ++buffer;
1766 --bufferLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001768 dev_dbg(dev, "%s - Hdr1=%02X Hdr2=%02X\n", __func__,
1769 edge_serial->rxHeader1, edge_serial->rxHeader2);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001770 /* Process depending on whether this header is
1771 * data or status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Alan Cox03f0dbf2008-07-22 11:16:34 +01001773 if (IS_CMD_STAT_HDR(edge_serial->rxHeader1)) {
1774 /* Decode this status header and go to
1775 * EXPECT_HDR1 (if we can process the status
1776 * with only 2 bytes), or go to EXPECT_HDR3 to
1777 * get the third byte. */
1778 edge_serial->rxPort =
1779 IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1780 edge_serial->rxStatusCode =
1781 IOSP_GET_STATUS_CODE(
1782 edge_serial->rxHeader1);
1783
1784 if (!IOSP_STATUS_IS_2BYTE(
1785 edge_serial->rxStatusCode)) {
1786 /* This status needs additional bytes.
1787 * Save what we have and then wait for
1788 * more data.
1789 */
1790 edge_serial->rxStatusParam
1791 = edge_serial->rxHeader2;
1792 edge_serial->rxState = EXPECT_HDR3;
1793 break;
1794 }
1795 /* We have all the header bytes, process the
1796 status now */
1797 process_rcvd_status(edge_serial,
1798 edge_serial->rxHeader2, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 edge_serial->rxState = EXPECT_HDR1;
1800 break;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001801 } else {
1802 edge_serial->rxPort =
1803 IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1804 edge_serial->rxBytesRemaining =
1805 IOSP_GET_HDR_DATA_LEN(
1806 edge_serial->rxHeader1,
1807 edge_serial->rxHeader2);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001808 dev_dbg(dev, "%s - Data for Port %u Len %u\n",
1809 __func__,
1810 edge_serial->rxPort,
1811 edge_serial->rxBytesRemaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
Alan Cox03f0dbf2008-07-22 11:16:34 +01001813 /* ASSERT(DevExt->RxPort < DevExt->NumPorts);
1814 * ASSERT(DevExt->RxBytesRemaining <
1815 * IOSP_MAX_DATA_LENGTH);
1816 */
1817
1818 if (bufferLength == 0) {
1819 edge_serial->rxState = EXPECT_DATA;
1820 break;
1821 }
1822 /* Else, drop through */
1823 }
1824 case EXPECT_DATA: /* Expect data */
1825 if (bufferLength < edge_serial->rxBytesRemaining) {
1826 rxLen = bufferLength;
1827 /* Expect data to start next buffer */
1828 edge_serial->rxState = EXPECT_DATA;
1829 } else {
1830 /* BufLen >= RxBytesRemaining */
1831 rxLen = edge_serial->rxBytesRemaining;
1832 /* Start another header next time */
1833 edge_serial->rxState = EXPECT_HDR1;
1834 }
1835
1836 bufferLength -= rxLen;
1837 edge_serial->rxBytesRemaining -= rxLen;
1838
1839 /* spit this data back into the tty driver if this
1840 port is open */
1841 if (rxLen) {
1842 port = edge_serial->serial->port[
1843 edge_serial->rxPort];
1844 edge_port = usb_get_serial_port_data(port);
1845 if (edge_port->open) {
Jiri Slaby2e124b42013-01-03 15:53:06 +01001846 dev_dbg(dev, "%s - Sending %d bytes to TTY for port %d\n",
1847 __func__, rxLen,
1848 edge_serial->rxPort);
1849 edge_tty_recv(edge_port->port, buffer,
1850 rxLen);
Johan Hovoldd36a7712013-03-21 12:37:07 +01001851 edge_port->port->icount.rx += rxLen;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001852 }
1853 buffer += rxLen;
1854 }
1855 break;
1856
1857 case EXPECT_HDR3: /* Expect 3rd byte of status header */
1858 edge_serial->rxHeader3 = *buffer;
1859 ++buffer;
1860 --bufferLength;
1861
1862 /* We have all the header bytes, process the
1863 status now */
1864 process_rcvd_status(edge_serial,
1865 edge_serial->rxStatusParam,
1866 edge_serial->rxHeader3);
1867 edge_serial->rxState = EXPECT_HDR1;
1868 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 }
1870 }
1871}
1872
1873
1874/*****************************************************************************
1875 * process_rcvd_status
Alan Cox03f0dbf2008-07-22 11:16:34 +01001876 * this function handles the any status messages received on the
1877 * bulk in pipe.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001879static void process_rcvd_status(struct edgeport_serial *edge_serial,
1880 __u8 byte2, __u8 byte3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881{
1882 struct usb_serial_port *port;
1883 struct edgeport_port *edge_port;
Alan Cox4a90f092008-10-13 10:39:46 +01001884 struct tty_struct *tty;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001885 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 __u8 code = edge_serial->rxStatusCode;
1887
1888 /* switch the port pointer to the one being currently talked about */
1889 port = edge_serial->serial->port[edge_serial->rxPort];
1890 edge_port = usb_get_serial_port_data(port);
1891 if (edge_port == NULL) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001892 dev_err(&edge_serial->serial->dev->dev,
1893 "%s - edge_port == NULL for port %d\n",
1894 __func__, edge_serial->rxPort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 return;
1896 }
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001897 dev = &port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
1899 if (code == IOSP_EXT_STATUS) {
1900 switch (byte2) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001901 case IOSP_EXT_STATUS_CHASE_RSP:
1902 /* we want to do EXT status regardless of port
1903 * open/closed */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001904 dev_dbg(dev, "%s - Port %u EXT CHASE_RSP Data = %02x\n",
1905 __func__, edge_serial->rxPort, byte3);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001906 /* Currently, the only EXT_STATUS is Chase, so process
1907 * here instead of one more call to one more subroutine
1908 * If/when more EXT_STATUS, there'll be more work to do
1909 * Also, we currently clear flag and close the port
1910 * regardless of content of above's Byte3.
1911 * We could choose to do something else when Byte3 says
1912 * Timeout on Chase from Edgeport, like wait longer in
1913 * block_until_chase_response, but for now we don't.
1914 */
1915 edge_port->chaseResponsePending = false;
1916 wake_up(&edge_port->wait_chase);
1917 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
Alan Cox03f0dbf2008-07-22 11:16:34 +01001919 case IOSP_EXT_STATUS_RX_CHECK_RSP:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001920 dev_dbg(dev, "%s ========== Port %u CHECK_RSP Sequence = %02x =============\n",
1921 __func__, edge_serial->rxPort, byte3);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001922 /* Port->RxCheckRsp = true; */
1923 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 }
1925 }
1926
1927 if (code == IOSP_STATUS_OPEN_RSP) {
1928 edge_port->txCredits = GET_TX_BUFFER_SIZE(byte3);
1929 edge_port->maxTxCredits = edge_port->txCredits;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001930 dev_dbg(dev, "%s - Port %u Open Response Initial MSR = %02x TxBufferSize = %d\n",
1931 __func__, edge_serial->rxPort, byte2, edge_port->txCredits);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001932 handle_new_msr(edge_port, byte2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
Alan Cox03f0dbf2008-07-22 11:16:34 +01001934 /* send the current line settings to the port so we are
1935 in sync with any further termios calls */
Alan Cox4a90f092008-10-13 10:39:46 +01001936 tty = tty_port_tty_get(&edge_port->port->port);
1937 if (tty) {
1938 change_port_settings(tty,
Alan Coxadc8d742012-07-14 15:31:47 +01001939 edge_port, &tty->termios);
Alan Cox4a90f092008-10-13 10:39:46 +01001940 tty_kref_put(tty);
1941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
1943 /* we have completed the open */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001944 edge_port->openPending = false;
1945 edge_port->open = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 wake_up(&edge_port->wait_open);
1947 return;
1948 }
1949
Alan Cox03f0dbf2008-07-22 11:16:34 +01001950 /* If port is closed, silently discard all rcvd status. We can
1951 * have cases where buffered status is received AFTER the close
1952 * port command is sent to the Edgeport.
1953 */
1954 if (!edge_port->open || edge_port->closePending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
1957 switch (code) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001958 /* Not currently sent by Edgeport */
1959 case IOSP_STATUS_LSR:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001960 dev_dbg(dev, "%s - Port %u LSR Status = %02x\n",
1961 __func__, edge_serial->rxPort, byte2);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001962 handle_new_lsr(edge_port, false, byte2, 0);
1963 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
Alan Cox03f0dbf2008-07-22 11:16:34 +01001965 case IOSP_STATUS_LSR_DATA:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001966 dev_dbg(dev, "%s - Port %u LSR Status = %02x, Data = %02x\n",
1967 __func__, edge_serial->rxPort, byte2, byte3);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001968 /* byte2 is LSR Register */
1969 /* byte3 is broken data byte */
1970 handle_new_lsr(edge_port, true, byte2, byte3);
1971 break;
1972 /*
1973 * case IOSP_EXT_4_STATUS:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001974 * dev_dbg(dev, "%s - Port %u LSR Status = %02x Data = %02x\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01001975 * __func__, edge_serial->rxPort, byte2, byte3);
1976 * break;
1977 */
1978 case IOSP_STATUS_MSR:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001979 dev_dbg(dev, "%s - Port %u MSR Status = %02x\n",
1980 __func__, edge_serial->rxPort, byte2);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001981 /*
1982 * Process this new modem status and generate appropriate
1983 * events, etc, based on the new status. This routine
1984 * also saves the MSR in Port->ShadowMsr.
1985 */
1986 handle_new_msr(edge_port, byte2);
1987 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
Alan Cox03f0dbf2008-07-22 11:16:34 +01001989 default:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001990 dev_dbg(dev, "%s - Unrecognized IOSP status code %u\n", __func__, code);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001991 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993}
1994
1995
1996/*****************************************************************************
1997 * edge_tty_recv
1998 * this function passes data on to the tty flip buffer
1999 *****************************************************************************/
Jiri Slaby2e124b42013-01-03 15:53:06 +01002000static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
2001 int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002{
2003 int cnt;
2004
Jiri Slaby05c7cd32013-01-03 15:53:04 +01002005 cnt = tty_insert_flip_string(&port->port, data, length);
Alan Coxa108bfc2010-02-18 16:44:01 +00002006 if (cnt < length) {
Jiri Slaby05c7cd32013-01-03 15:53:04 +01002007 dev_err(&port->dev, "%s - dropping data, %d bytes lost\n",
Alan Coxa108bfc2010-02-18 16:44:01 +00002008 __func__, length - cnt);
2009 }
2010 data += cnt;
2011 length -= cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Jiri Slaby2e124b42013-01-03 15:53:06 +01002013 tty_flip_buffer_push(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014}
2015
2016
2017/*****************************************************************************
2018 * handle_new_msr
2019 * this function handles any change to the msr register for a port.
2020 *****************************************************************************/
2021static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr)
2022{
2023 struct async_icount *icount;
2024
Alan Cox03f0dbf2008-07-22 11:16:34 +01002025 if (newMsr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
2026 EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
Johan Hovoldd36a7712013-03-21 12:37:07 +01002027 icount = &edge_port->port->icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
2029 /* update input line counters */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002030 if (newMsr & EDGEPORT_MSR_DELTA_CTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 icount->cts++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002032 if (newMsr & EDGEPORT_MSR_DELTA_DSR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 icount->dsr++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002034 if (newMsr & EDGEPORT_MSR_DELTA_CD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 icount->dcd++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002036 if (newMsr & EDGEPORT_MSR_DELTA_RI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 icount->rng++;
Johan Hovold8b8070d2013-03-21 12:37:08 +01002038 wake_up_interruptible(&edge_port->port->port.delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 }
2040
2041 /* Save the new modem status */
2042 edge_port->shadowMSR = newMsr & 0xf0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043}
2044
2045
2046/*****************************************************************************
2047 * handle_new_lsr
2048 * this function handles any change to the lsr register for a port.
2049 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002050static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
2051 __u8 lsr, __u8 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002053 __u8 newLsr = (__u8) (lsr & (__u8)
2054 (LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
2055 struct async_icount *icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 edge_port->shadowLSR = lsr;
2058
2059 if (newLsr & LSR_BREAK) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002060 /*
2061 * Parity and Framing errors only count if they
2062 * occur exclusive of a break being
2063 * received.
2064 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 newLsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
2066 }
2067
2068 /* Place LSR data byte into Rx buffer */
Jiri Slaby2e124b42013-01-03 15:53:06 +01002069 if (lsrData)
2070 edge_tty_recv(edge_port->port, &data, 1);
2071
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 /* update input line counters */
Johan Hovoldd36a7712013-03-21 12:37:07 +01002073 icount = &edge_port->port->icount;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002074 if (newLsr & LSR_BREAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 icount->brk++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002076 if (newLsr & LSR_OVER_ERR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 icount->overrun++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002078 if (newLsr & LSR_PAR_ERR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 icount->parity++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002080 if (newLsr & LSR_FRM_ERR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 icount->frame++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082}
2083
2084
2085/****************************************************************************
2086 * sram_write
Alan Cox03f0dbf2008-07-22 11:16:34 +01002087 * writes a number of bytes to the Edgeport device's sram starting at the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 * given address.
2089 * If successful returns the number of bytes written, otherwise it returns
2090 * a negative error number of the problem.
2091 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002092static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
2093 __u16 length, const __u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094{
2095 int result;
2096 __u16 current_length;
2097 unsigned char *transfer_buffer;
2098
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002099 dev_dbg(&serial->dev->dev, "%s - %x, %x, %d\n", __func__, extAddr, addr, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
Alan Cox03f0dbf2008-07-22 11:16:34 +01002101 transfer_buffer = kmalloc(64, GFP_KERNEL);
Johan Hovold10c642d2013-12-29 19:22:56 +01002102 if (!transfer_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105 /* need to split these writes up into 64 byte chunks */
2106 result = 0;
2107 while (length > 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002108 if (length > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 current_length = 64;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002110 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 current_length = length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002112
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002113/* dev_dbg(&serial->dev->dev, "%s - writing %x, %x, %d\n", __func__, extAddr, addr, current_length); */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002114 memcpy(transfer_buffer, data, current_length);
2115 result = usb_control_msg(serial->dev,
2116 usb_sndctrlpipe(serial->dev, 0),
2117 USB_REQUEST_ION_WRITE_RAM,
2118 0x40, addr, extAddr, transfer_buffer,
2119 current_length, 300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 if (result < 0)
2121 break;
2122 length -= current_length;
2123 addr += current_length;
2124 data += current_length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
Alan Cox03f0dbf2008-07-22 11:16:34 +01002127 kfree(transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 return result;
2129}
2130
2131
2132/****************************************************************************
2133 * rom_write
2134 * writes a number of bytes to the Edgeport device's ROM starting at the
2135 * given address.
2136 * If successful returns the number of bytes written, otherwise it returns
2137 * a negative error number of the problem.
2138 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002139static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
2140 __u16 length, const __u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141{
2142 int result;
2143 __u16 current_length;
2144 unsigned char *transfer_buffer;
2145
Alan Cox03f0dbf2008-07-22 11:16:34 +01002146 transfer_buffer = kmalloc(64, GFP_KERNEL);
Johan Hovold10c642d2013-12-29 19:22:56 +01002147 if (!transfer_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
2150 /* need to split these writes up into 64 byte chunks */
2151 result = 0;
2152 while (length > 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002153 if (length > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 current_length = 64;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002155 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 current_length = length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002157 memcpy(transfer_buffer, data, current_length);
2158 result = usb_control_msg(serial->dev,
2159 usb_sndctrlpipe(serial->dev, 0),
2160 USB_REQUEST_ION_WRITE_ROM, 0x40,
2161 addr, extAddr,
2162 transfer_buffer, current_length, 300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 if (result < 0)
2164 break;
2165 length -= current_length;
2166 addr += current_length;
2167 data += current_length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
Alan Cox03f0dbf2008-07-22 11:16:34 +01002170 kfree(transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 return result;
2172}
2173
2174
2175/****************************************************************************
2176 * rom_read
2177 * reads a number of bytes from the Edgeport device starting at the given
2178 * address.
Johan Hovold3c0e25d2017-01-12 14:56:14 +01002179 * Returns zero on success or a negative error number.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002181static int rom_read(struct usb_serial *serial, __u16 extAddr,
2182 __u16 addr, __u16 length, __u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183{
2184 int result;
2185 __u16 current_length;
2186 unsigned char *transfer_buffer;
2187
Alan Cox03f0dbf2008-07-22 11:16:34 +01002188 transfer_buffer = kmalloc(64, GFP_KERNEL);
Johan Hovold10c642d2013-12-29 19:22:56 +01002189 if (!transfer_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
2192 /* need to split these reads up into 64 byte chunks */
2193 result = 0;
2194 while (length > 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002195 if (length > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 current_length = 64;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002197 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 current_length = length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002199 result = usb_control_msg(serial->dev,
2200 usb_rcvctrlpipe(serial->dev, 0),
2201 USB_REQUEST_ION_READ_ROM,
2202 0xC0, addr, extAddr, transfer_buffer,
2203 current_length, 300);
Johan Hovold3c0e25d2017-01-12 14:56:14 +01002204 if (result < current_length) {
2205 if (result >= 0)
2206 result = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 break;
Johan Hovold3c0e25d2017-01-12 14:56:14 +01002208 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01002209 memcpy(data, transfer_buffer, current_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 length -= current_length;
2211 addr += current_length;
2212 data += current_length;
Johan Hovold3c0e25d2017-01-12 14:56:14 +01002213
2214 result = 0;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216
Alan Cox03f0dbf2008-07-22 11:16:34 +01002217 kfree(transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 return result;
2219}
2220
2221
2222/****************************************************************************
2223 * send_iosp_ext_cmd
2224 * Is used to send a IOSP message to the Edgeport device
2225 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002226static int send_iosp_ext_cmd(struct edgeport_port *edge_port,
2227 __u8 command, __u8 param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228{
2229 unsigned char *buffer;
2230 unsigned char *currentCommand;
2231 int length = 0;
2232 int status = 0;
2233
Alan Cox03f0dbf2008-07-22 11:16:34 +01002234 buffer = kmalloc(10, GFP_ATOMIC);
Johan Hovold10c642d2013-12-29 19:22:56 +01002235 if (!buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
2238 currentCommand = buffer;
2239
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07002240 MAKE_CMD_EXT_CMD(&currentCommand, &length, edge_port->port->port_number,
2241 command, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
Alan Cox03f0dbf2008-07-22 11:16:34 +01002243 status = write_cmd_usb(edge_port, buffer, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 if (status) {
2245 /* something bad happened, let's free up the memory */
2246 kfree(buffer);
2247 }
2248
2249 return status;
2250}
2251
2252
2253/*****************************************************************************
2254 * write_cmd_usb
2255 * this function writes the given buffer out to the bulk write endpoint.
2256 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002257static int write_cmd_usb(struct edgeport_port *edge_port,
2258 unsigned char *buffer, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002260 struct edgeport_serial *edge_serial =
2261 usb_get_serial_data(edge_port->port->serial);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002262 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 int status = 0;
2264 struct urb *urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01002266 usb_serial_debug_data(dev, __func__, length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
2268 /* Allocate our next urb */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002269 urb = usb_alloc_urb(0, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 if (!urb)
2271 return -ENOMEM;
2272
Oliver Neukum96c706e2007-03-15 15:27:17 +01002273 atomic_inc(&CmdUrbs);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002274 dev_dbg(dev, "%s - ALLOCATE URB %p (outstanding %d)\n",
2275 __func__, urb, atomic_read(&CmdUrbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
Alan Cox03f0dbf2008-07-22 11:16:34 +01002277 usb_fill_bulk_urb(urb, edge_serial->serial->dev,
2278 usb_sndbulkpipe(edge_serial->serial->dev,
2279 edge_serial->bulk_out_endpoint),
2280 buffer, length, edge_bulk_out_cmd_callback, edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002282 edge_port->commandPending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 status = usb_submit_urb(urb, GFP_ATOMIC);
2284
2285 if (status) {
2286 /* something went wrong */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002287 dev_err(dev, "%s - usb_submit_urb(write command) failed, status = %d\n",
2288 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 usb_kill_urb(urb);
2290 usb_free_urb(urb);
Oliver Neukum96c706e2007-03-15 15:27:17 +01002291 atomic_dec(&CmdUrbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 return status;
2293 }
2294
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295#if 0
Alan Cox03f0dbf2008-07-22 11:16:34 +01002296 wait_event(&edge_port->wait_command, !edge_port->commandPending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002298 if (edge_port->commandPending) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 /* command timed out */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002300 dev_dbg(dev, "%s - command timed out\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 status = -EINVAL;
2302 }
2303#endif
2304 return status;
2305}
2306
2307
2308/*****************************************************************************
2309 * send_cmd_write_baud_rate
2310 * this function sends the proper command to change the baud rate of the
2311 * specified port.
2312 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002313static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
2314 int baudRate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002316 struct edgeport_serial *edge_serial =
2317 usb_get_serial_data(edge_port->port->serial);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002318 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 unsigned char *cmdBuffer;
2320 unsigned char *currCmd;
2321 int cmdLen = 0;
2322 int divisor;
2323 int status;
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07002324 u32 number = edge_port->port->port_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
Adam Kropelinbc71e472007-07-29 11:03:29 -04002326 if (edge_serial->is_epic &&
2327 !edge_serial->epic_descriptor.Supports.IOSPSetBaudRate) {
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07002328 dev_dbg(dev, "SendCmdWriteBaudRate - NOT Setting baud rate for port, baud = %d\n",
2329 baudRate);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002330 return 0;
2331 }
2332
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07002333 dev_dbg(dev, "%s - baud = %d\n", __func__, baudRate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002335 status = calc_baud_rate_divisor(dev, baudRate, &divisor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 if (status) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002337 dev_err(dev, "%s - bad baud rate\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 return status;
2339 }
2340
Alan Cox03f0dbf2008-07-22 11:16:34 +01002341 /* Alloc memory for the string of commands. */
2342 cmdBuffer = kmalloc(0x100, GFP_ATOMIC);
Johan Hovold10c642d2013-12-29 19:22:56 +01002343 if (!cmdBuffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 return -ENOMEM;
Johan Hovold10c642d2013-12-29 19:22:56 +01002345
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 currCmd = cmdBuffer;
2347
Alan Cox03f0dbf2008-07-22 11:16:34 +01002348 /* Enable access to divisor latch */
2349 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR, LCR_DL_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
Alan Cox03f0dbf2008-07-22 11:16:34 +01002351 /* Write the divisor itself */
2352 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLL, LOW8(divisor));
2353 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLM, HIGH8(divisor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
Alan Cox03f0dbf2008-07-22 11:16:34 +01002355 /* Restore original value to disable access to divisor latch */
2356 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR,
2357 edge_port->shadowLCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
Alan Cox03f0dbf2008-07-22 11:16:34 +01002359 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 if (status) {
2361 /* something bad happened, let's free up the memory */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002362 kfree(cmdBuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 }
2364
2365 return status;
2366}
2367
2368
2369/*****************************************************************************
2370 * calc_baud_rate_divisor
2371 * this function calculates the proper baud rate divisor for the specified
2372 * baud rate.
2373 *****************************************************************************/
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002374static int calc_baud_rate_divisor(struct device *dev, int baudrate, int *divisor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375{
2376 int i;
2377 __u16 custom;
2378
Tobias Klauser52950ed2005-12-11 16:20:08 +01002379 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002380 if (divisor_table[i].BaudRate == baudrate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 *divisor = divisor_table[i].Divisor;
2382 return 0;
2383 }
2384 }
2385
Alan Cox03f0dbf2008-07-22 11:16:34 +01002386 /* We have tried all of the standard baud rates
2387 * lets try to calculate the divisor for this baud rate
2388 * Make sure the baud rate is reasonable */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 if (baudrate > 50 && baudrate < 230400) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002390 /* get divisor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 custom = (__u16)((230400L + baudrate/2) / baudrate);
2392
2393 *divisor = custom;
2394
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002395 dev_dbg(dev, "%s - Baud %d = %d\n", __func__, baudrate, custom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 return 0;
2397 }
2398
2399 return -1;
2400}
2401
2402
2403/*****************************************************************************
2404 * send_cmd_write_uart_register
Anand Gadiyarfd589a82009-07-16 17:13:03 +02002405 * this function builds up a uart register message and sends to the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002407static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
2408 __u8 regNum, __u8 regValue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002410 struct edgeport_serial *edge_serial =
2411 usb_get_serial_data(edge_port->port->serial);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002412 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 unsigned char *cmdBuffer;
2414 unsigned char *currCmd;
2415 unsigned long cmdLen = 0;
2416 int status;
2417
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002418 dev_dbg(dev, "%s - write to %s register 0x%02x\n",
2419 (regNum == MCR) ? "MCR" : "LCR", __func__, regValue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
Adam Kropelinbc71e472007-07-29 11:03:29 -04002421 if (edge_serial->is_epic &&
2422 !edge_serial->epic_descriptor.Supports.IOSPWriteMCR &&
2423 regNum == MCR) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002424 dev_dbg(dev, "SendCmdWriteUartReg - Not writing to MCR Register\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002425 return 0;
2426 }
2427
Adam Kropelinbc71e472007-07-29 11:03:29 -04002428 if (edge_serial->is_epic &&
2429 !edge_serial->epic_descriptor.Supports.IOSPWriteLCR &&
2430 regNum == LCR) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002431 dev_dbg(dev, "SendCmdWriteUartReg - Not writing to LCR Register\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002432 return 0;
2433 }
2434
Alan Cox03f0dbf2008-07-22 11:16:34 +01002435 /* Alloc memory for the string of commands. */
2436 cmdBuffer = kmalloc(0x10, GFP_ATOMIC);
2437 if (cmdBuffer == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
2440 currCmd = cmdBuffer;
2441
Alan Cox03f0dbf2008-07-22 11:16:34 +01002442 /* Build a cmd in the buffer to write the given register */
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07002443 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, edge_port->port->port_number,
2444 regNum, regValue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
2446 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
2447 if (status) {
2448 /* something bad happened, let's free up the memory */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002449 kfree(cmdBuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 }
2451
2452 return status;
2453}
2454
2455
2456/*****************************************************************************
2457 * change_port_settings
Alan Cox03f0dbf2008-07-22 11:16:34 +01002458 * This routine is called to set the UART on the device to match the
2459 * specified new settings.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01002461
2462static void change_port_settings(struct tty_struct *tty,
2463 struct edgeport_port *edge_port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002465 struct device *dev = &edge_port->port->dev;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002466 struct edgeport_serial *edge_serial =
2467 usb_get_serial_data(edge_port->port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 int baud;
2469 unsigned cflag;
2470 __u8 mask = 0xff;
2471 __u8 lData;
2472 __u8 lParity;
2473 __u8 lStop;
2474 __u8 rxFlow;
2475 __u8 txFlow;
2476 int status;
2477
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002478 if (!edge_port->open &&
2479 !edge_port->openPending) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002480 dev_dbg(dev, "%s - port not opened\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 return;
2482 }
2483
Alan Coxadc8d742012-07-14 15:31:47 +01002484 cflag = tty->termios.c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
2486 switch (cflag & CSIZE) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002487 case CS5:
2488 lData = LCR_BITS_5; mask = 0x1f;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002489 dev_dbg(dev, "%s - data bits = 5\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002490 break;
2491 case CS6:
2492 lData = LCR_BITS_6; mask = 0x3f;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002493 dev_dbg(dev, "%s - data bits = 6\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002494 break;
2495 case CS7:
2496 lData = LCR_BITS_7; mask = 0x7f;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002497 dev_dbg(dev, "%s - data bits = 7\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002498 break;
2499 default:
2500 case CS8:
2501 lData = LCR_BITS_8;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002502 dev_dbg(dev, "%s - data bits = 8\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002503 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 }
2505
2506 lParity = LCR_PAR_NONE;
2507 if (cflag & PARENB) {
2508 if (cflag & CMSPAR) {
2509 if (cflag & PARODD) {
2510 lParity = LCR_PAR_MARK;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002511 dev_dbg(dev, "%s - parity = mark\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 } else {
2513 lParity = LCR_PAR_SPACE;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002514 dev_dbg(dev, "%s - parity = space\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 }
2516 } else if (cflag & PARODD) {
2517 lParity = LCR_PAR_ODD;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002518 dev_dbg(dev, "%s - parity = odd\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 } else {
2520 lParity = LCR_PAR_EVEN;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002521 dev_dbg(dev, "%s - parity = even\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 }
2523 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002524 dev_dbg(dev, "%s - parity = none\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 }
2526
2527 if (cflag & CSTOPB) {
2528 lStop = LCR_STOP_2;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002529 dev_dbg(dev, "%s - stop bits = 2\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 } else {
2531 lStop = LCR_STOP_1;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002532 dev_dbg(dev, "%s - stop bits = 1\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 }
2534
2535 /* figure out the flow control settings */
2536 rxFlow = txFlow = 0x00;
2537 if (cflag & CRTSCTS) {
2538 rxFlow |= IOSP_RX_FLOW_RTS;
2539 txFlow |= IOSP_TX_FLOW_CTS;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002540 dev_dbg(dev, "%s - RTS/CTS is enabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002542 dev_dbg(dev, "%s - RTS/CTS is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 }
2544
Alan Cox03f0dbf2008-07-22 11:16:34 +01002545 /* if we are implementing XON/XOFF, set the start and stop character
2546 in the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 if (I_IXOFF(tty) || I_IXON(tty)) {
2548 unsigned char stop_char = STOP_CHAR(tty);
2549 unsigned char start_char = START_CHAR(tty);
2550
Geyslan G. Bem232dce82015-12-11 06:46:41 -03002551 if (!edge_serial->is_epic ||
2552 edge_serial->epic_descriptor.Supports.IOSPSetXChar) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002553 send_iosp_ext_cmd(edge_port,
2554 IOSP_CMD_SET_XON_CHAR, start_char);
2555 send_iosp_ext_cmd(edge_port,
2556 IOSP_CMD_SET_XOFF_CHAR, stop_char);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
2559 /* if we are implementing INBOUND XON/XOFF */
2560 if (I_IXOFF(tty)) {
2561 rxFlow |= IOSP_RX_FLOW_XON_XOFF;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002562 dev_dbg(dev, "%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2563 __func__, start_char, stop_char);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002565 dev_dbg(dev, "%s - INBOUND XON/XOFF is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 }
2567
2568 /* if we are implementing OUTBOUND XON/XOFF */
2569 if (I_IXON(tty)) {
2570 txFlow |= IOSP_TX_FLOW_XON_XOFF;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002571 dev_dbg(dev, "%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2572 __func__, start_char, stop_char);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002574 dev_dbg(dev, "%s - OUTBOUND XON/XOFF is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 }
2576 }
2577
2578 /* Set flow control to the configured value */
Geyslan G. Bem232dce82015-12-11 06:46:41 -03002579 if (!edge_serial->is_epic ||
2580 edge_serial->epic_descriptor.Supports.IOSPSetRxFlow)
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002581 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_RX_FLOW, rxFlow);
Geyslan G. Bem232dce82015-12-11 06:46:41 -03002582 if (!edge_serial->is_epic ||
2583 edge_serial->epic_descriptor.Supports.IOSPSetTxFlow)
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002584 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_TX_FLOW, txFlow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585
2586
2587 edge_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2588 edge_port->shadowLCR |= (lData | lParity | lStop);
2589
2590 edge_port->validDataMask = mask;
2591
2592 /* Send the updated LCR value to the EdgePort */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002593 status = send_cmd_write_uart_register(edge_port, LCR,
2594 edge_port->shadowLCR);
2595 if (status != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
2598 /* set up the MCR register and send it to the EdgePort */
2599 edge_port->shadowMCR = MCR_MASTER_IE;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002600 if (cflag & CBAUD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 edge_port->shadowMCR |= (MCR_DTR | MCR_RTS);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002602
2603 status = send_cmd_write_uart_register(edge_port, MCR,
2604 edge_port->shadowMCR);
2605 if (status != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607
2608 /* Determine divisor based on baud rate */
2609 baud = tty_get_baud_rate(tty);
2610 if (!baud) {
2611 /* pick a default, any default... */
2612 baud = 9600;
2613 }
2614
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002615 dev_dbg(dev, "%s - baud rate = %d\n", __func__, baud);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002616 status = send_cmd_write_baud_rate(edge_port, baud);
Alan Cox6ce073b2007-10-18 01:24:25 -07002617 if (status == -1) {
2618 /* Speed change was not possible - put back the old speed */
2619 baud = tty_termios_baud_rate(old_termios);
2620 tty_encode_baud_rate(tty, baud, baud);
2621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622}
2623
2624
2625/****************************************************************************
2626 * unicode_to_ascii
2627 * Turns a string from Unicode into ASCII.
2628 * Doesn't do a good job with any characters that are outside the normal
2629 * ASCII range, but it's only for debugging...
2630 * NOTE: expects the unicode in LE format
2631 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002632static void unicode_to_ascii(char *string, int buflen,
2633 __le16 *unicode, int unicode_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634{
2635 int i;
2636
Pete Zaitcevad933752006-05-22 21:49:44 -07002637 if (buflen <= 0) /* never happens, but... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 return;
Pete Zaitcevad933752006-05-22 21:49:44 -07002639 --buflen; /* space for nul */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
Pete Zaitcevad933752006-05-22 21:49:44 -07002641 for (i = 0; i < unicode_size; i++) {
2642 if (i >= buflen)
2643 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 string[i] = (char)(le16_to_cpu(unicode[i]));
Pete Zaitcevad933752006-05-22 21:49:44 -07002645 }
2646 string[i] = 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647}
2648
2649
2650/****************************************************************************
2651 * get_manufacturing_desc
Alan Cox03f0dbf2008-07-22 11:16:34 +01002652 * reads in the manufacturing descriptor and stores it into the serial
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 * structure.
2654 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002655static void get_manufacturing_desc(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002657 struct device *dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 int response;
2659
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002660 dev_dbg(dev, "getting manufacturer descriptor\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661
Alan Cox03f0dbf2008-07-22 11:16:34 +01002662 response = rom_read(edge_serial->serial,
2663 (EDGE_MANUF_DESC_ADDR & 0xffff0000) >> 16,
2664 (__u16)(EDGE_MANUF_DESC_ADDR & 0x0000ffff),
2665 EDGE_MANUF_DESC_LEN,
2666 (__u8 *)(&edge_serial->manuf_descriptor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667
Johan Hovold3c0e25d2017-01-12 14:56:14 +01002668 if (response < 0) {
2669 dev_err(dev, "error in getting manufacturer descriptor: %d\n",
2670 response);
2671 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 char string[30];
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002673 dev_dbg(dev, "**Manufacturer Descriptor\n");
2674 dev_dbg(dev, " RomSize: %dK\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002675 edge_serial->manuf_descriptor.RomSize);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002676 dev_dbg(dev, " RamSize: %dK\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002677 edge_serial->manuf_descriptor.RamSize);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002678 dev_dbg(dev, " CpuRev: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002679 edge_serial->manuf_descriptor.CpuRev);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002680 dev_dbg(dev, " BoardRev: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002681 edge_serial->manuf_descriptor.BoardRev);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002682 dev_dbg(dev, " NumPorts: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002683 edge_serial->manuf_descriptor.NumPorts);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002684 dev_dbg(dev, " DescDate: %d/%d/%d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002685 edge_serial->manuf_descriptor.DescDate[0],
2686 edge_serial->manuf_descriptor.DescDate[1],
2687 edge_serial->manuf_descriptor.DescDate[2]+1900);
Pete Zaitcev4bc203d2006-06-06 18:18:33 -07002688 unicode_to_ascii(string, sizeof(string),
Alan Cox03f0dbf2008-07-22 11:16:34 +01002689 edge_serial->manuf_descriptor.SerialNumber,
2690 edge_serial->manuf_descriptor.SerNumLength/2);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002691 dev_dbg(dev, " SerialNumber: %s\n", string);
Pete Zaitcev4bc203d2006-06-06 18:18:33 -07002692 unicode_to_ascii(string, sizeof(string),
Alan Cox03f0dbf2008-07-22 11:16:34 +01002693 edge_serial->manuf_descriptor.AssemblyNumber,
2694 edge_serial->manuf_descriptor.AssemblyNumLength/2);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002695 dev_dbg(dev, " AssemblyNumber: %s\n", string);
Pete Zaitcev4bc203d2006-06-06 18:18:33 -07002696 unicode_to_ascii(string, sizeof(string),
Pete Zaitcevad933752006-05-22 21:49:44 -07002697 edge_serial->manuf_descriptor.OemAssyNumber,
2698 edge_serial->manuf_descriptor.OemAssyNumLength/2);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002699 dev_dbg(dev, " OemAssyNumber: %s\n", string);
2700 dev_dbg(dev, " UartType: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002701 edge_serial->manuf_descriptor.UartType);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002702 dev_dbg(dev, " IonPid: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002703 edge_serial->manuf_descriptor.IonPid);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002704 dev_dbg(dev, " IonConfig: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002705 edge_serial->manuf_descriptor.IonConfig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 }
2707}
2708
2709
2710/****************************************************************************
2711 * get_boot_desc
Alan Cox03f0dbf2008-07-22 11:16:34 +01002712 * reads in the bootloader descriptor and stores it into the serial
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 * structure.
2714 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002715static void get_boot_desc(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002717 struct device *dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 int response;
2719
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002720 dev_dbg(dev, "getting boot descriptor\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721
Alan Cox03f0dbf2008-07-22 11:16:34 +01002722 response = rom_read(edge_serial->serial,
2723 (EDGE_BOOT_DESC_ADDR & 0xffff0000) >> 16,
2724 (__u16)(EDGE_BOOT_DESC_ADDR & 0x0000ffff),
2725 EDGE_BOOT_DESC_LEN,
2726 (__u8 *)(&edge_serial->boot_descriptor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727
Johan Hovold3c0e25d2017-01-12 14:56:14 +01002728 if (response < 0) {
2729 dev_err(dev, "error in getting boot descriptor: %d\n",
2730 response);
2731 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002732 dev_dbg(dev, "**Boot Descriptor:\n");
2733 dev_dbg(dev, " BootCodeLength: %d\n",
2734 le16_to_cpu(edge_serial->boot_descriptor.BootCodeLength));
2735 dev_dbg(dev, " MajorVersion: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002736 edge_serial->boot_descriptor.MajorVersion);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002737 dev_dbg(dev, " MinorVersion: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002738 edge_serial->boot_descriptor.MinorVersion);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002739 dev_dbg(dev, " BuildNumber: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002740 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002741 dev_dbg(dev, " Capabilities: 0x%x\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002742 le16_to_cpu(edge_serial->boot_descriptor.Capabilities));
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002743 dev_dbg(dev, " UConfig0: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002744 edge_serial->boot_descriptor.UConfig0);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002745 dev_dbg(dev, " UConfig1: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002746 edge_serial->boot_descriptor.UConfig1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 }
2748}
2749
2750
2751/****************************************************************************
2752 * load_application_firmware
2753 * This is called to load the application firmware to the device
2754 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002755static void load_application_firmware(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002757 struct device *dev = &edge_serial->serial->dev->dev;
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302758 const struct ihex_binrec *rec;
2759 const struct firmware *fw;
2760 const char *fw_name;
2761 const char *fw_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 int response;
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302763 __u32 Operaddr;
2764 __u16 build;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765
2766 switch (edge_serial->product_info.iDownloadFile) {
2767 case EDGE_DOWNLOAD_FILE_I930:
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302768 fw_info = "downloading firmware version (930)";
2769 fw_name = "edgeport/down.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 break;
2771
2772 case EDGE_DOWNLOAD_FILE_80251:
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302773 fw_info = "downloading firmware version (80251)";
2774 fw_name = "edgeport/down2.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 break;
2776
2777 case EDGE_DOWNLOAD_FILE_NONE:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002778 dev_dbg(dev, "No download file specified, skipping download\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 return;
2780
2781 default:
2782 return;
2783 }
2784
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302785 response = request_ihex_firmware(&fw, fw_name,
2786 &edge_serial->serial->dev->dev);
2787 if (response) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002788 dev_err(dev, "Failed to load image \"%s\" err %d\n",
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302789 fw_name, response);
2790 return;
2791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302793 rec = (const struct ihex_binrec *)fw->data;
2794 build = (rec->data[2] << 8) | rec->data[3];
2795
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002796 dev_dbg(dev, "%s %d.%d.%d\n", fw_info, rec->data[0], rec->data[1], build);
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302797
Bjørn Mork271c1152011-01-17 14:19:37 +01002798 edge_serial->product_info.FirmwareMajorVersion = rec->data[0];
2799 edge_serial->product_info.FirmwareMinorVersion = rec->data[1];
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302800 edge_serial->product_info.FirmwareBuildNumber = cpu_to_le16(build);
2801
2802 for (rec = ihex_next_binrec(rec); rec;
2803 rec = ihex_next_binrec(rec)) {
2804 Operaddr = be32_to_cpu(rec->addr);
2805 response = sram_write(edge_serial->serial,
2806 Operaddr >> 16,
2807 Operaddr & 0xFFFF,
2808 be16_to_cpu(rec->len),
2809 &rec->data[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 if (response < 0) {
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302811 dev_err(&edge_serial->serial->dev->dev,
2812 "sram_write failed (%x, %x, %d)\n",
2813 Operaddr >> 16, Operaddr & 0xFFFF,
2814 be16_to_cpu(rec->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 break;
2816 }
2817 }
2818
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002819 dev_dbg(dev, "sending exec_dl_code\n");
2820 response = usb_control_msg (edge_serial->serial->dev,
2821 usb_sndctrlpipe(edge_serial->serial->dev, 0),
2822 USB_REQUEST_ION_EXEC_DL_CODE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 0x40, 0x4000, 0x0001, NULL, 0, 3000);
2824
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302825 release_firmware(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826}
2827
2828
2829/****************************************************************************
2830 * edge_startup
2831 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002832static int edge_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833{
2834 struct edgeport_serial *edge_serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 struct usb_device *dev;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002836 struct device *ddev = &serial->dev->dev;
Johan Hovoldc27f3ef2012-10-17 13:34:57 +02002837 int i;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002838 int response;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002839 bool interrupt_in_found;
2840 bool bulk_in_found;
2841 bool bulk_out_found;
Johan Hovold4f37fa52017-04-18 14:42:28 +02002842 static const __u32 descriptor[3] = { EDGE_COMPATIBILITY_MASK0,
2843 EDGE_COMPATIBILITY_MASK1,
2844 EDGE_COMPATIBILITY_MASK2 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
2846 dev = serial->dev;
2847
2848 /* create our private serial structure */
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +01002849 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
Johan Hovold10c642d2013-12-29 19:22:56 +01002850 if (!edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 return -ENOMEM;
Johan Hovold10c642d2013-12-29 19:22:56 +01002852
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 spin_lock_init(&edge_serial->es_lock);
2854 edge_serial->serial = serial;
2855 usb_set_serial_data(serial, edge_serial);
2856
2857 /* get the name for the device from the device */
Dan Carpenterf10718f2010-01-25 14:53:33 +03002858 i = usb_string(dev, dev->descriptor.iManufacturer,
Pete Zaitcevad933752006-05-22 21:49:44 -07002859 &edge_serial->name[0], MAX_NAME_LEN+1);
Dan Carpenterf10718f2010-01-25 14:53:33 +03002860 if (i < 0)
2861 i = 0;
Pete Zaitcevad933752006-05-22 21:49:44 -07002862 edge_serial->name[i++] = ' ';
Dan Carpenterf10718f2010-01-25 14:53:33 +03002863 usb_string(dev, dev->descriptor.iProduct,
Pete Zaitcevad933752006-05-22 21:49:44 -07002864 &edge_serial->name[i], MAX_NAME_LEN+2 - i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
2866 dev_info(&serial->dev->dev, "%s detected\n", edge_serial->name);
2867
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002868 /* Read the epic descriptor */
Johan Hovolde4457d92017-01-12 14:56:13 +01002869 if (get_epic_descriptor(edge_serial) < 0) {
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002870 /* memcpy descriptor to Supports structures */
2871 memcpy(&edge_serial->epic_descriptor.Supports, descriptor,
2872 sizeof(struct edge_compatibility_bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002874 /* get the manufacturing descriptor for this device */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002875 get_manufacturing_desc(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002877 /* get the boot descriptor */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002878 get_boot_desc(edge_serial);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002879
2880 get_product_info(edge_serial);
2881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882
2883 /* set the number of ports from the manufacturing description */
2884 /* serial->num_ports = serial->product_info.NumPorts; */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002885 if ((!edge_serial->is_epic) &&
2886 (edge_serial->product_info.NumPorts != serial->num_ports)) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002887 dev_warn(ddev,
2888 "Device Reported %d serial ports vs. core thinking we have %d ports, email greg@kroah.com this information.\n",
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002889 edge_serial->product_info.NumPorts,
2890 serial->num_ports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 }
2892
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002893 dev_dbg(ddev, "%s - time 1 %ld\n", __func__, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002895 /* If not an EPiC device */
2896 if (!edge_serial->is_epic) {
2897 /* now load the application firmware into this device */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002898 load_application_firmware(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002900 dev_dbg(ddev, "%s - time 2 %ld\n", __func__, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002902 /* Check current Edgeport EEPROM and update if necessary */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002903 update_edgeport_E2PROM(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002905 dev_dbg(ddev, "%s - time 3 %ld\n", __func__, jiffies);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002906
2907 /* set the configuration to use #1 */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002908/* dev_dbg(ddev, "set_configuration 1\n"); */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002909/* usb_set_configuration (dev, 1); */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002910 }
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002911 dev_dbg(ddev, " FirmwareMajorVersion %d.%d.%d\n",
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302912 edge_serial->product_info.FirmwareMajorVersion,
2913 edge_serial->product_info.FirmwareMinorVersion,
2914 le16_to_cpu(edge_serial->product_info.FirmwareBuildNumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915
Alan Cox03f0dbf2008-07-22 11:16:34 +01002916 /* we set up the pointers to the endpoints in the edge_open function,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 * as the structures aren't created yet. */
2918
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002919 response = 0;
2920
2921 if (edge_serial->is_epic) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002922 /* EPIC thing, set up our interrupt polling now and our read
2923 * urb, so that the device knows it really is connected. */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002924 interrupt_in_found = bulk_in_found = bulk_out_found = false;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002925 for (i = 0; i < serial->interface->altsetting[0]
2926 .desc.bNumEndpoints; ++i) {
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002927 struct usb_endpoint_descriptor *endpoint;
2928 int buffer_size;
2929
Alan Cox03f0dbf2008-07-22 11:16:34 +01002930 endpoint = &serial->interface->altsetting[0].
2931 endpoint[i].desc;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07002932 buffer_size = usb_endpoint_maxp(endpoint);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002933 if (!interrupt_in_found &&
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002934 (usb_endpoint_is_int_in(endpoint))) {
2935 /* we found a interrupt in endpoint */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002936 dev_dbg(ddev, "found interrupt in\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002937
2938 /* not set up yet, so do it now */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002939 edge_serial->interrupt_read_urb =
2940 usb_alloc_urb(0, GFP_KERNEL);
Johan Hovoldc5c0c552016-05-08 20:07:56 +02002941 if (!edge_serial->interrupt_read_urb) {
2942 response = -ENOMEM;
2943 break;
2944 }
Johan Hovold10c642d2013-12-29 19:22:56 +01002945
Alan Cox03f0dbf2008-07-22 11:16:34 +01002946 edge_serial->interrupt_in_buffer =
2947 kmalloc(buffer_size, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002948 if (!edge_serial->interrupt_in_buffer) {
Johan Hovoldc5c0c552016-05-08 20:07:56 +02002949 response = -ENOMEM;
2950 break;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002951 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01002952 edge_serial->interrupt_in_endpoint =
2953 endpoint->bEndpointAddress;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002954
2955 /* set up our interrupt urb */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002956 usb_fill_int_urb(
2957 edge_serial->interrupt_read_urb,
2958 dev,
2959 usb_rcvintpipe(dev,
2960 endpoint->bEndpointAddress),
2961 edge_serial->interrupt_in_buffer,
2962 buffer_size,
2963 edge_interrupt_callback,
2964 edge_serial,
2965 endpoint->bInterval);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002966
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002967 interrupt_in_found = true;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002968 }
2969
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002970 if (!bulk_in_found &&
Alan Cox03f0dbf2008-07-22 11:16:34 +01002971 (usb_endpoint_is_bulk_in(endpoint))) {
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002972 /* we found a bulk in endpoint */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002973 dev_dbg(ddev, "found bulk in\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002974
2975 /* not set up yet, so do it now */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002976 edge_serial->read_urb =
2977 usb_alloc_urb(0, GFP_KERNEL);
Johan Hovoldc5c0c552016-05-08 20:07:56 +02002978 if (!edge_serial->read_urb) {
2979 response = -ENOMEM;
2980 break;
2981 }
Johan Hovold10c642d2013-12-29 19:22:56 +01002982
Alan Cox03f0dbf2008-07-22 11:16:34 +01002983 edge_serial->bulk_in_buffer =
2984 kmalloc(buffer_size, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002985 if (!edge_serial->bulk_in_buffer) {
Johan Hovoldc5c0c552016-05-08 20:07:56 +02002986 response = -ENOMEM;
2987 break;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002988 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01002989 edge_serial->bulk_in_endpoint =
2990 endpoint->bEndpointAddress;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002991
2992 /* set up our bulk in urb */
2993 usb_fill_bulk_urb(edge_serial->read_urb, dev,
Alan Cox03f0dbf2008-07-22 11:16:34 +01002994 usb_rcvbulkpipe(dev,
2995 endpoint->bEndpointAddress),
2996 edge_serial->bulk_in_buffer,
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07002997 usb_endpoint_maxp(endpoint),
Alan Cox03f0dbf2008-07-22 11:16:34 +01002998 edge_bulk_in_callback,
2999 edge_serial);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003000 bulk_in_found = true;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003001 }
3002
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003003 if (!bulk_out_found &&
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003004 (usb_endpoint_is_bulk_out(endpoint))) {
3005 /* we found a bulk out endpoint */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003006 dev_dbg(ddev, "found bulk out\n");
Alan Cox03f0dbf2008-07-22 11:16:34 +01003007 edge_serial->bulk_out_endpoint =
3008 endpoint->bEndpointAddress;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003009 bulk_out_found = true;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003010 }
3011 }
3012
Johan Hovoldc5c0c552016-05-08 20:07:56 +02003013 if (response || !interrupt_in_found || !bulk_in_found ||
3014 !bulk_out_found) {
3015 if (!response) {
3016 dev_err(ddev, "expected endpoints not found\n");
3017 response = -ENODEV;
3018 }
3019
3020 usb_free_urb(edge_serial->interrupt_read_urb);
3021 kfree(edge_serial->interrupt_in_buffer);
3022
3023 usb_free_urb(edge_serial->read_urb);
3024 kfree(edge_serial->bulk_in_buffer);
3025
3026 kfree(edge_serial);
3027
3028 return response;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003029 }
3030
3031 /* start interrupt read for this edgeport this interrupt will
3032 * continue as long as the edgeport is connected */
Alan Cox03f0dbf2008-07-22 11:16:34 +01003033 response = usb_submit_urb(edge_serial->interrupt_read_urb,
3034 GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003035 if (response)
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003036 dev_err(ddev, "%s - Error %d submitting control urb\n",
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07003037 __func__, response);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003038 }
3039 return response;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040}
3041
3042
3043/****************************************************************************
Alan Sternf9c99bb2009-06-02 11:53:55 -04003044 * edge_disconnect
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 * This function is called whenever the device is removed from the usb bus.
3046 ****************************************************************************/
Alan Sternf9c99bb2009-06-02 11:53:55 -04003047static void edge_disconnect(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048{
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003049 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003051 if (edge_serial->is_epic) {
Oliver Neukum74ac07e2007-06-13 18:50:41 +02003052 usb_kill_urb(edge_serial->interrupt_read_urb);
Oliver Neukum74ac07e2007-06-13 18:50:41 +02003053 usb_kill_urb(edge_serial->read_urb);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003054 }
Alan Sternf9c99bb2009-06-02 11:53:55 -04003055}
3056
3057
3058/****************************************************************************
3059 * edge_release
3060 * This function is called when the device structure is deallocated.
3061 ****************************************************************************/
3062static void edge_release(struct usb_serial *serial)
3063{
3064 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003065
Johan Hovoldc8d62952016-05-08 20:07:57 +02003066 if (edge_serial->is_epic) {
3067 usb_kill_urb(edge_serial->interrupt_read_urb);
3068 usb_free_urb(edge_serial->interrupt_read_urb);
3069 kfree(edge_serial->interrupt_in_buffer);
3070
3071 usb_kill_urb(edge_serial->read_urb);
3072 usb_free_urb(edge_serial->read_urb);
3073 kfree(edge_serial->bulk_in_buffer);
3074 }
3075
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003076 kfree(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077}
3078
Johan Hovoldc27f3ef2012-10-17 13:34:57 +02003079static int edge_port_probe(struct usb_serial_port *port)
3080{
3081 struct edgeport_port *edge_port;
3082
3083 edge_port = kzalloc(sizeof(*edge_port), GFP_KERNEL);
3084 if (!edge_port)
3085 return -ENOMEM;
3086
3087 spin_lock_init(&edge_port->ep_lock);
3088 edge_port->port = port;
3089
3090 usb_set_serial_port_data(port, edge_port);
3091
3092 return 0;
3093}
3094
3095static int edge_port_remove(struct usb_serial_port *port)
3096{
3097 struct edgeport_port *edge_port;
3098
3099 edge_port = usb_get_serial_port_data(port);
3100 kfree(edge_port);
3101
3102 return 0;
3103}
3104
Johan Hovold35d47972017-02-15 11:09:50 +01003105static struct usb_serial_driver edgeport_2port_device = {
3106 .driver = {
3107 .owner = THIS_MODULE,
3108 .name = "edgeport_2",
3109 },
3110 .description = "Edgeport 2 port adapter",
3111 .id_table = edgeport_2port_id_table,
3112 .num_ports = 2,
Johan Hovoldfd0c8832017-03-02 12:51:23 +01003113 .num_bulk_in = 1,
3114 .num_bulk_out = 1,
3115 .num_interrupt_in = 1,
Johan Hovold35d47972017-02-15 11:09:50 +01003116 .open = edge_open,
3117 .close = edge_close,
3118 .throttle = edge_throttle,
3119 .unthrottle = edge_unthrottle,
3120 .attach = edge_startup,
3121 .disconnect = edge_disconnect,
3122 .release = edge_release,
3123 .port_probe = edge_port_probe,
3124 .port_remove = edge_port_remove,
3125 .ioctl = edge_ioctl,
3126 .set_termios = edge_set_termios,
3127 .tiocmget = edge_tiocmget,
3128 .tiocmset = edge_tiocmset,
3129 .tiocmiwait = usb_serial_generic_tiocmiwait,
3130 .get_icount = usb_serial_generic_get_icount,
3131 .write = edge_write,
3132 .write_room = edge_write_room,
3133 .chars_in_buffer = edge_chars_in_buffer,
3134 .break_ctl = edge_break,
3135 .read_int_callback = edge_interrupt_callback,
3136 .read_bulk_callback = edge_bulk_in_callback,
3137 .write_bulk_callback = edge_bulk_out_data_callback,
3138};
3139
3140static struct usb_serial_driver edgeport_4port_device = {
3141 .driver = {
3142 .owner = THIS_MODULE,
3143 .name = "edgeport_4",
3144 },
3145 .description = "Edgeport 4 port adapter",
3146 .id_table = edgeport_4port_id_table,
3147 .num_ports = 4,
Johan Hovoldfd0c8832017-03-02 12:51:23 +01003148 .num_bulk_in = 1,
3149 .num_bulk_out = 1,
3150 .num_interrupt_in = 1,
Johan Hovold35d47972017-02-15 11:09:50 +01003151 .open = edge_open,
3152 .close = edge_close,
3153 .throttle = edge_throttle,
3154 .unthrottle = edge_unthrottle,
3155 .attach = edge_startup,
3156 .disconnect = edge_disconnect,
3157 .release = edge_release,
3158 .port_probe = edge_port_probe,
3159 .port_remove = edge_port_remove,
3160 .ioctl = edge_ioctl,
3161 .set_termios = edge_set_termios,
3162 .tiocmget = edge_tiocmget,
3163 .tiocmset = edge_tiocmset,
3164 .tiocmiwait = usb_serial_generic_tiocmiwait,
3165 .get_icount = usb_serial_generic_get_icount,
3166 .write = edge_write,
3167 .write_room = edge_write_room,
3168 .chars_in_buffer = edge_chars_in_buffer,
3169 .break_ctl = edge_break,
3170 .read_int_callback = edge_interrupt_callback,
3171 .read_bulk_callback = edge_bulk_in_callback,
3172 .write_bulk_callback = edge_bulk_out_data_callback,
3173};
3174
3175static struct usb_serial_driver edgeport_8port_device = {
3176 .driver = {
3177 .owner = THIS_MODULE,
3178 .name = "edgeport_8",
3179 },
3180 .description = "Edgeport 8 port adapter",
3181 .id_table = edgeport_8port_id_table,
3182 .num_ports = 8,
Johan Hovoldfd0c8832017-03-02 12:51:23 +01003183 .num_bulk_in = 1,
3184 .num_bulk_out = 1,
3185 .num_interrupt_in = 1,
Johan Hovold35d47972017-02-15 11:09:50 +01003186 .open = edge_open,
3187 .close = edge_close,
3188 .throttle = edge_throttle,
3189 .unthrottle = edge_unthrottle,
3190 .attach = edge_startup,
3191 .disconnect = edge_disconnect,
3192 .release = edge_release,
3193 .port_probe = edge_port_probe,
3194 .port_remove = edge_port_remove,
3195 .ioctl = edge_ioctl,
3196 .set_termios = edge_set_termios,
3197 .tiocmget = edge_tiocmget,
3198 .tiocmset = edge_tiocmset,
3199 .tiocmiwait = usb_serial_generic_tiocmiwait,
3200 .get_icount = usb_serial_generic_get_icount,
3201 .write = edge_write,
3202 .write_room = edge_write_room,
3203 .chars_in_buffer = edge_chars_in_buffer,
3204 .break_ctl = edge_break,
3205 .read_int_callback = edge_interrupt_callback,
3206 .read_bulk_callback = edge_bulk_in_callback,
3207 .write_bulk_callback = edge_bulk_out_data_callback,
3208};
3209
3210static struct usb_serial_driver epic_device = {
3211 .driver = {
3212 .owner = THIS_MODULE,
3213 .name = "epic",
3214 },
3215 .description = "EPiC device",
3216 .id_table = Epic_port_id_table,
3217 .num_ports = 1,
Johan Hovoldfd0c8832017-03-02 12:51:23 +01003218 .num_bulk_in = 1,
3219 .num_bulk_out = 1,
3220 .num_interrupt_in = 1,
Johan Hovold35d47972017-02-15 11:09:50 +01003221 .open = edge_open,
3222 .close = edge_close,
3223 .throttle = edge_throttle,
3224 .unthrottle = edge_unthrottle,
3225 .attach = edge_startup,
3226 .disconnect = edge_disconnect,
3227 .release = edge_release,
3228 .port_probe = edge_port_probe,
3229 .port_remove = edge_port_remove,
3230 .ioctl = edge_ioctl,
3231 .set_termios = edge_set_termios,
3232 .tiocmget = edge_tiocmget,
3233 .tiocmset = edge_tiocmset,
3234 .tiocmiwait = usb_serial_generic_tiocmiwait,
3235 .get_icount = usb_serial_generic_get_icount,
3236 .write = edge_write,
3237 .write_room = edge_write_room,
3238 .chars_in_buffer = edge_chars_in_buffer,
3239 .break_ctl = edge_break,
3240 .read_int_callback = edge_interrupt_callback,
3241 .read_bulk_callback = edge_bulk_in_callback,
3242 .write_bulk_callback = edge_bulk_out_data_callback,
3243};
3244
3245static struct usb_serial_driver * const serial_drivers[] = {
3246 &edgeport_2port_device, &edgeport_4port_device,
3247 &edgeport_8port_device, &epic_device, NULL
3248};
3249
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07003250module_usb_serial_driver(serial_drivers, id_table_combined);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251
Alan Cox03f0dbf2008-07-22 11:16:34 +01003252MODULE_AUTHOR(DRIVER_AUTHOR);
3253MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254MODULE_LICENSE("GPL");
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05303255MODULE_FIRMWARE("edgeport/boot.fw");
3256MODULE_FIRMWARE("edgeport/boot2.fw");
3257MODULE_FIRMWARE("edgeport/down.fw");
3258MODULE_FIRMWARE("edgeport/down2.fw");