blob: 0dd8cce9c9d360cdf8008e39e185f1d64351ad4d [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>
35#include <linux/init.h>
36#include <linux/slab.h>
37#include <linux/tty.h>
38#include <linux/tty_driver.h>
39#include <linux/tty_flip.h>
40#include <linux/module.h>
41#include <linux/spinlock.h>
42#include <linux/serial.h>
43#include <linux/ioctl.h>
44#include <linux/wait.h>
Jaswinder Singh5b9ea932008-07-03 17:00:23 +053045#include <linux/firmware.h>
46#include <linux/ihex.h>
Alan Cox03f0dbf2008-07-22 11:16:34 +010047#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070049#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include "io_edgeport.h"
51#include "io_ionsp.h" /* info for the iosp messages */
52#include "io_16654.h" /* 16654 UART defines */
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
55#define DRIVER_DESC "Edgeport USB Serial Driver"
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#define MAX_NAME_LEN 64
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#define OPEN_TIMEOUT (5*HZ) /* 5 seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61/* receive port state */
62enum RXSTATE {
Alan Cox03f0dbf2008-07-22 11:16:34 +010063 EXPECT_HDR1 = 0, /* Expect header byte 1 */
64 EXPECT_HDR2 = 1, /* Expect header byte 2 */
65 EXPECT_DATA = 2, /* Expect 'RxBytesRemaining' data */
66 EXPECT_HDR3 = 3, /* Expect header byte 3 (for status hdrs only) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070067};
68
69
Alan Cox03f0dbf2008-07-22 11:16:34 +010070/* Transmit Fifo
71 * This Transmit queue is an extension of the edgeport Rx buffer.
72 * The maximum amount of data buffered in both the edgeport
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 * Rx buffer (maxTxCredits) and this buffer will never exceed maxTxCredits.
74 */
75struct TxFifo {
76 unsigned int head; /* index to head pointer (write) */
77 unsigned int tail; /* index to tail pointer (read) */
78 unsigned int count; /* Bytes in queue */
79 unsigned int size; /* Max size of queue (equal to Max number of TxCredits) */
80 unsigned char *fifo; /* allocated Buffer */
81};
82
83/* This structure holds all of the local port information */
84struct edgeport_port {
85 __u16 txCredits; /* our current credits for this port */
86 __u16 maxTxCredits; /* the max size of the port */
87
88 struct TxFifo txfifo; /* transmit fifo -- size will be maxTxCredits */
89 struct urb *write_urb; /* write URB for this port */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +010090 bool write_in_progress; /* 'true' while a write URB is outstanding */
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 spinlock_t ep_lock;
92
93 __u8 shadowLCR; /* last LCR value received */
94 __u8 shadowMCR; /* last MCR value received */
95 __u8 shadowMSR; /* last MSR value received */
96 __u8 shadowLSR; /* last LSR value received */
97 __u8 shadowXonChar; /* last value set as XON char in Edgeport */
98 __u8 shadowXoffChar; /* last value set as XOFF char in Edgeport */
99 __u8 validDataMask;
100 __u32 baudRate;
101
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100102 bool open;
103 bool openPending;
104 bool commandPending;
105 bool closePending;
106 bool chaseResponsePending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
109 wait_queue_head_t wait_open; /* for handling sleeping while waiting for open to finish */
110 wait_queue_head_t wait_command; /* for handling sleeping while waiting for command to finish */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 struct usb_serial_port *port; /* loop back to the owner of this object */
113};
114
115
116/* This structure holds all of the individual device information */
117struct edgeport_serial {
Pete Zaitcevad933752006-05-22 21:49:44 -0700118 char name[MAX_NAME_LEN+2]; /* string name of this device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 struct edge_manuf_descriptor manuf_descriptor; /* the manufacturer descriptor */
121 struct edge_boot_descriptor boot_descriptor; /* the boot firmware descriptor */
122 struct edgeport_product_info product_info; /* Product Info */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800123 struct edge_compatibility_descriptor epic_descriptor; /* Edgeport compatible descriptor */
124 int is_epic; /* flag if EPiC device or not */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126 __u8 interrupt_in_endpoint; /* the interrupt endpoint handle */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100127 unsigned char *interrupt_in_buffer; /* the buffer we use for the interrupt endpoint */
128 struct urb *interrupt_read_urb; /* our interrupt urb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 __u8 bulk_in_endpoint; /* the bulk in endpoint handle */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100131 unsigned char *bulk_in_buffer; /* the buffer we use for the bulk in endpoint */
132 struct urb *read_urb; /* our bulk read urb */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100133 bool read_in_progress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 spinlock_t es_lock;
135
136 __u8 bulk_out_endpoint; /* the bulk out endpoint handle */
137
138 __s16 rxBytesAvail; /* the number of bytes that we need to read from this device */
139
140 enum RXSTATE rxState; /* the current state of the bulk receive processor */
141 __u8 rxHeader1; /* receive header byte 1 */
142 __u8 rxHeader2; /* receive header byte 2 */
143 __u8 rxHeader3; /* receive header byte 3 */
144 __u8 rxPort; /* the port that we are currently receiving data for */
145 __u8 rxStatusCode; /* the receive status code */
146 __u8 rxStatusParam; /* the receive status paramater */
147 __s16 rxBytesRemaining; /* the number of port bytes left to read */
148 struct usb_serial *serial; /* loop back to the owner of this object */
149};
150
151/* baud rate information */
152struct divisor_table_entry {
153 __u32 BaudRate;
154 __u16 Divisor;
155};
156
Alan Cox03f0dbf2008-07-22 11:16:34 +0100157/*
158 * Define table of divisors for Rev A EdgePort/4 hardware
159 * These assume a 3.6864MHz crystal, the standard /16, and
160 * MCR.7 = 0.
161 */
162
Arjan van de Ven4c4c9432005-11-29 09:43:42 +0100163static const struct divisor_table_entry divisor_table[] = {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100164 { 50, 4608},
165 { 75, 3072},
166 { 110, 2095}, /* 2094.545455 => 230450 => .0217 % over */
167 { 134, 1713}, /* 1713.011152 => 230398.5 => .00065% under */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 { 150, 1536},
169 { 300, 768},
170 { 600, 384},
171 { 1200, 192},
172 { 1800, 128},
173 { 2400, 96},
174 { 4800, 48},
175 { 7200, 32},
176 { 9600, 24},
177 { 14400, 16},
178 { 19200, 12},
179 { 38400, 6},
180 { 57600, 4},
181 { 115200, 2},
182 { 230400, 1},
183};
184
Greg Kroah-Hartman36ccce42012-02-28 13:11:51 -0800185/* Number of outstanding Command Write Urbs */
186static atomic_t CmdUrbs = ATOMIC_INIT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188
189/* local function prototypes */
190
191/* function prototypes for all URB callbacks */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100192static void edge_interrupt_callback(struct urb *urb);
193static void edge_bulk_in_callback(struct urb *urb);
194static void edge_bulk_out_data_callback(struct urb *urb);
195static void edge_bulk_out_cmd_callback(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197/* function prototypes for the usbserial callbacks */
Alan Coxa509a7e2009-09-19 13:13:26 -0700198static int edge_open(struct tty_struct *tty, struct usb_serial_port *port);
Alan Cox335f8512009-06-11 12:26:29 +0100199static void edge_close(struct usb_serial_port *port);
Alan Cox03f0dbf2008-07-22 11:16:34 +0100200static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
201 const unsigned char *buf, int count);
202static int edge_write_room(struct tty_struct *tty);
203static int edge_chars_in_buffer(struct tty_struct *tty);
204static void edge_throttle(struct tty_struct *tty);
205static void edge_unthrottle(struct tty_struct *tty);
206static void edge_set_termios(struct tty_struct *tty,
207 struct usb_serial_port *port,
208 struct ktermios *old_termios);
Alan Cox00a0d0d2011-02-14 16:27:06 +0000209static int edge_ioctl(struct tty_struct *tty,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100210 unsigned int cmd, unsigned long arg);
211static void edge_break(struct tty_struct *tty, int break_state);
Alan Cox60b33c12011-02-14 16:26:14 +0000212static int edge_tiocmget(struct tty_struct *tty);
Alan Cox20b9d172011-02-14 16:26:50 +0000213static int edge_tiocmset(struct tty_struct *tty,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100214 unsigned int set, unsigned int clear);
215static int edge_startup(struct usb_serial *serial);
Alan Sternf9c99bb2009-06-02 11:53:55 -0400216static void edge_disconnect(struct usb_serial *serial);
217static void edge_release(struct usb_serial *serial);
Johan Hovoldc27f3ef2012-10-17 13:34:57 +0200218static int edge_port_probe(struct usb_serial_port *port);
219static int edge_port_remove(struct usb_serial_port *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221#include "io_tables.h" /* all of the devices that this driver supports */
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223/* function prototypes for all of our local functions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Alan Cox03f0dbf2008-07-22 11:16:34 +0100225static void process_rcvd_data(struct edgeport_serial *edge_serial,
226 unsigned char *buffer, __u16 bufferLength);
227static void process_rcvd_status(struct edgeport_serial *edge_serial,
228 __u8 byte2, __u8 byte3);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100229static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
230 int length);
Alan Cox03f0dbf2008-07-22 11:16:34 +0100231static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr);
232static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
233 __u8 lsr, __u8 data);
234static int send_iosp_ext_cmd(struct edgeport_port *edge_port, __u8 command,
235 __u8 param);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700236static int calc_baud_rate_divisor(struct device *dev, int baud_rate, int *divisor);
Alan Cox03f0dbf2008-07-22 11:16:34 +0100237static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
238 int baudRate);
239static void change_port_settings(struct tty_struct *tty,
240 struct edgeport_port *edge_port,
241 struct ktermios *old_termios);
242static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
243 __u8 regNum, __u8 regValue);
244static int write_cmd_usb(struct edgeport_port *edge_port,
245 unsigned char *buffer, int writeLength);
246static void send_more_port_data(struct edgeport_serial *edge_serial,
247 struct edgeport_port *edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Alan Cox03f0dbf2008-07-22 11:16:34 +0100249static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
250 __u16 length, const __u8 *data);
251static int rom_read(struct usb_serial *serial, __u16 extAddr, __u16 addr,
252 __u16 length, __u8 *data);
253static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
254 __u16 length, const __u8 *data);
255static void get_manufacturing_desc(struct edgeport_serial *edge_serial);
256static void get_boot_desc(struct edgeport_serial *edge_serial);
257static void load_application_firmware(struct edgeport_serial *edge_serial);
258
259static void unicode_to_ascii(char *string, int buflen,
260 __le16 *unicode, int unicode_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262
Alan Cox03f0dbf2008-07-22 11:16:34 +0100263/* ************************************************************************ */
264/* ************************************************************************ */
265/* ************************************************************************ */
266/* ************************************************************************ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268/************************************************************************
269 * *
270 * update_edgeport_E2PROM() Compare current versions of *
271 * Boot ROM and Manufacture *
272 * Descriptors with versions *
273 * embedded in this driver *
274 * *
275 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100276static void update_edgeport_E2PROM(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700278 struct device *dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 __u32 BootCurVer;
280 __u32 BootNewVer;
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530281 __u8 BootMajorVersion;
282 __u8 BootMinorVersion;
283 __u16 BootBuildNumber;
284 __u32 Bootaddr;
285 const struct ihex_binrec *rec;
286 const struct firmware *fw;
287 const char *fw_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 int response;
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 switch (edge_serial->product_info.iDownloadFile) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100291 case EDGE_DOWNLOAD_FILE_I930:
292 fw_name = "edgeport/boot.fw";
293 break;
294 case EDGE_DOWNLOAD_FILE_80251:
295 fw_name = "edgeport/boot2.fw";
296 break;
297 default:
298 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530301 response = request_ihex_firmware(&fw, fw_name,
302 &edge_serial->serial->dev->dev);
303 if (response) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700304 dev_err(dev, "Failed to load image \"%s\" err %d\n",
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530305 fw_name, response);
306 return;
307 }
308
309 rec = (const struct ihex_binrec *)fw->data;
310 BootMajorVersion = rec->data[0];
311 BootMinorVersion = rec->data[1];
312 BootBuildNumber = (rec->data[2] << 8) | rec->data[3];
313
Alan Cox03f0dbf2008-07-22 11:16:34 +0100314 /* Check Boot Image Version */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 BootCurVer = (edge_serial->boot_descriptor.MajorVersion << 24) +
316 (edge_serial->boot_descriptor.MinorVersion << 16) +
317 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber);
318
319 BootNewVer = (BootMajorVersion << 24) +
320 (BootMinorVersion << 16) +
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530321 BootBuildNumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700323 dev_dbg(dev, "Current Boot Image version %d.%d.%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 edge_serial->boot_descriptor.MajorVersion,
325 edge_serial->boot_descriptor.MinorVersion,
326 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
327
328
329 if (BootNewVer > BootCurVer) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700330 dev_dbg(dev, "**Update Boot Image from %d.%d.%d to %d.%d.%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 edge_serial->boot_descriptor.MajorVersion,
332 edge_serial->boot_descriptor.MinorVersion,
333 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber),
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530334 BootMajorVersion, BootMinorVersion, BootBuildNumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700336 dev_dbg(dev, "Downloading new Boot Image\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530338 for (rec = ihex_next_binrec(rec); rec;
339 rec = ihex_next_binrec(rec)) {
340 Bootaddr = be32_to_cpu(rec->addr);
341 response = rom_write(edge_serial->serial,
342 Bootaddr >> 16,
343 Bootaddr & 0xFFFF,
344 be16_to_cpu(rec->len),
345 &rec->data[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (response < 0) {
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530347 dev_err(&edge_serial->serial->dev->dev,
348 "rom_write failed (%x, %x, %d)\n",
349 Bootaddr >> 16, Bootaddr & 0xFFFF,
350 be16_to_cpu(rec->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 break;
352 }
353 }
354 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700355 dev_dbg(dev, "Boot Image -- already up to date\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530357 release_firmware(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360#if 0
361/************************************************************************
362 *
363 * Get string descriptor from device
364 *
365 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100366static int get_string_desc(struct usb_device *dev, int Id,
367 struct usb_string_descriptor **pRetDesc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 struct usb_string_descriptor StringDesc;
370 struct usb_string_descriptor *pStringDesc;
371
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700372 dev_dbg(&dev->dev, "%s - USB String ID = %d\n", __func__, Id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Alan Cox03f0dbf2008-07-22 11:16:34 +0100374 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc,
375 sizeof(StringDesc)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Alan Cox03f0dbf2008-07-22 11:16:34 +0100378 pStringDesc = kmalloc(StringDesc.bLength, GFP_KERNEL);
379 if (!pStringDesc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Alan Cox03f0dbf2008-07-22 11:16:34 +0100382 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc,
383 StringDesc.bLength)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 kfree(pStringDesc);
385 return -1;
386 }
387
388 *pRetDesc = pStringDesc;
389 return 0;
390}
391#endif
392
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700393static void dump_product_info(struct edgeport_serial *edge_serial,
394 struct edgeport_product_info *product_info)
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800395{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700396 struct device *dev = &edge_serial->serial->dev->dev;
397
Alan Cox03f0dbf2008-07-22 11:16:34 +0100398 /* Dump Product Info structure */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700399 dev_dbg(dev, "**Product Information:\n");
400 dev_dbg(dev, " ProductId %x\n", product_info->ProductId);
401 dev_dbg(dev, " NumPorts %d\n", product_info->NumPorts);
402 dev_dbg(dev, " ProdInfoVer %d\n", product_info->ProdInfoVer);
403 dev_dbg(dev, " IsServer %d\n", product_info->IsServer);
404 dev_dbg(dev, " IsRS232 %d\n", product_info->IsRS232);
405 dev_dbg(dev, " IsRS422 %d\n", product_info->IsRS422);
406 dev_dbg(dev, " IsRS485 %d\n", product_info->IsRS485);
407 dev_dbg(dev, " RomSize %d\n", product_info->RomSize);
408 dev_dbg(dev, " RamSize %d\n", product_info->RamSize);
409 dev_dbg(dev, " CpuRev %x\n", product_info->CpuRev);
410 dev_dbg(dev, " BoardRev %x\n", product_info->BoardRev);
411 dev_dbg(dev, " BootMajorVersion %d.%d.%d\n",
412 product_info->BootMajorVersion,
413 product_info->BootMinorVersion,
414 le16_to_cpu(product_info->BootBuildNumber));
415 dev_dbg(dev, " FirmwareMajorVersion %d.%d.%d\n",
416 product_info->FirmwareMajorVersion,
417 product_info->FirmwareMinorVersion,
418 le16_to_cpu(product_info->FirmwareBuildNumber));
419 dev_dbg(dev, " ManufactureDescDate %d/%d/%d\n",
420 product_info->ManufactureDescDate[0],
421 product_info->ManufactureDescDate[1],
422 product_info->ManufactureDescDate[2]+1900);
423 dev_dbg(dev, " iDownloadFile 0x%x\n",
424 product_info->iDownloadFile);
425 dev_dbg(dev, " EpicVer %d\n", product_info->EpicVer);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800426}
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428static void get_product_info(struct edgeport_serial *edge_serial)
429{
430 struct edgeport_product_info *product_info = &edge_serial->product_info;
431
Alan Cox03f0dbf2008-07-22 11:16:34 +0100432 memset(product_info, 0, sizeof(struct edgeport_product_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Alan Cox03f0dbf2008-07-22 11:16:34 +0100434 product_info->ProductId = (__u16)(le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct) & ~ION_DEVICE_ID_80251_NETCHIP);
435 product_info->NumPorts = edge_serial->manuf_descriptor.NumPorts;
436 product_info->ProdInfoVer = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Alan Cox03f0dbf2008-07-22 11:16:34 +0100438 product_info->RomSize = edge_serial->manuf_descriptor.RomSize;
439 product_info->RamSize = edge_serial->manuf_descriptor.RamSize;
440 product_info->CpuRev = edge_serial->manuf_descriptor.CpuRev;
441 product_info->BoardRev = edge_serial->manuf_descriptor.BoardRev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Alan Cox03f0dbf2008-07-22 11:16:34 +0100443 product_info->BootMajorVersion =
444 edge_serial->boot_descriptor.MajorVersion;
445 product_info->BootMinorVersion =
446 edge_serial->boot_descriptor.MinorVersion;
447 product_info->BootBuildNumber =
448 edge_serial->boot_descriptor.BuildNumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Alan Cox03f0dbf2008-07-22 11:16:34 +0100450 memcpy(product_info->ManufactureDescDate,
451 edge_serial->manuf_descriptor.DescDate,
452 sizeof(edge_serial->manuf_descriptor.DescDate));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Alan Cox03f0dbf2008-07-22 11:16:34 +0100454 /* check if this is 2nd generation hardware */
455 if (le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct)
456 & ION_DEVICE_ID_80251_NETCHIP)
457 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_80251;
458 else
459 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_I930;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700460
Alan Cox03f0dbf2008-07-22 11:16:34 +0100461 /* Determine Product type and set appropriate flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 switch (DEVICE_ID_FROM_USB_PRODUCT_ID(product_info->ProductId)) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100463 case ION_DEVICE_ID_EDGEPORT_COMPATIBLE:
464 case ION_DEVICE_ID_EDGEPORT_4T:
465 case ION_DEVICE_ID_EDGEPORT_4:
466 case ION_DEVICE_ID_EDGEPORT_2:
467 case ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU:
468 case ION_DEVICE_ID_EDGEPORT_8:
469 case ION_DEVICE_ID_EDGEPORT_421:
470 case ION_DEVICE_ID_EDGEPORT_21:
471 case ION_DEVICE_ID_EDGEPORT_2_DIN:
472 case ION_DEVICE_ID_EDGEPORT_4_DIN:
473 case ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU:
474 product_info->IsRS232 = 1;
475 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Alan Cox03f0dbf2008-07-22 11:16:34 +0100477 case ION_DEVICE_ID_EDGEPORT_2I: /* Edgeport/2 RS422/RS485 */
478 product_info->IsRS422 = 1;
479 product_info->IsRS485 = 1;
480 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Alan Cox03f0dbf2008-07-22 11:16:34 +0100482 case ION_DEVICE_ID_EDGEPORT_8I: /* Edgeport/4 RS422 */
483 case ION_DEVICE_ID_EDGEPORT_4I: /* Edgeport/4 RS422 */
484 product_info->IsRS422 = 1;
485 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
487
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700488 dump_product_info(edge_serial, product_info);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800489}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800491static int get_epic_descriptor(struct edgeport_serial *ep)
492{
493 int result;
494 struct usb_serial *serial = ep->serial;
495 struct edgeport_product_info *product_info = &ep->product_info;
496 struct edge_compatibility_descriptor *epic = &ep->epic_descriptor;
497 struct edge_compatibility_bits *bits;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700498 struct device *dev = &serial->dev->dev;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800499
500 ep->is_epic = 0;
501 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
502 USB_REQUEST_ION_GET_EPIC_DESC,
503 0xC0, 0x00, 0x00,
504 &ep->epic_descriptor,
505 sizeof(struct edge_compatibility_descriptor),
506 300);
507
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800508 if (result > 0) {
509 ep->is_epic = 1;
510 memset(product_info, 0, sizeof(struct edgeport_product_info));
511
Alan Cox03f0dbf2008-07-22 11:16:34 +0100512 product_info->NumPorts = epic->NumPorts;
513 product_info->ProdInfoVer = 0;
514 product_info->FirmwareMajorVersion = epic->MajorVersion;
515 product_info->FirmwareMinorVersion = epic->MinorVersion;
516 product_info->FirmwareBuildNumber = epic->BuildNumber;
517 product_info->iDownloadFile = epic->iDownloadFile;
518 product_info->EpicVer = epic->EpicVer;
519 product_info->Epic = epic->Supports;
520 product_info->ProductId = ION_DEVICE_ID_EDGEPORT_COMPATIBLE;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700521 dump_product_info(ep, product_info);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800522
523 bits = &ep->epic_descriptor.Supports;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700524 dev_dbg(dev, "**EPIC descriptor:\n");
525 dev_dbg(dev, " VendEnableSuspend: %s\n", bits->VendEnableSuspend ? "TRUE": "FALSE");
526 dev_dbg(dev, " IOSPOpen : %s\n", bits->IOSPOpen ? "TRUE": "FALSE");
527 dev_dbg(dev, " IOSPClose : %s\n", bits->IOSPClose ? "TRUE": "FALSE");
528 dev_dbg(dev, " IOSPChase : %s\n", bits->IOSPChase ? "TRUE": "FALSE");
529 dev_dbg(dev, " IOSPSetRxFlow : %s\n", bits->IOSPSetRxFlow ? "TRUE": "FALSE");
530 dev_dbg(dev, " IOSPSetTxFlow : %s\n", bits->IOSPSetTxFlow ? "TRUE": "FALSE");
531 dev_dbg(dev, " IOSPSetXChar : %s\n", bits->IOSPSetXChar ? "TRUE": "FALSE");
532 dev_dbg(dev, " IOSPRxCheck : %s\n", bits->IOSPRxCheck ? "TRUE": "FALSE");
533 dev_dbg(dev, " IOSPSetClrBreak : %s\n", bits->IOSPSetClrBreak ? "TRUE": "FALSE");
534 dev_dbg(dev, " IOSPWriteMCR : %s\n", bits->IOSPWriteMCR ? "TRUE": "FALSE");
535 dev_dbg(dev, " IOSPWriteLCR : %s\n", bits->IOSPWriteLCR ? "TRUE": "FALSE");
536 dev_dbg(dev, " IOSPSetBaudRate : %s\n", bits->IOSPSetBaudRate ? "TRUE": "FALSE");
537 dev_dbg(dev, " TrueEdgeport : %s\n", bits->TrueEdgeport ? "TRUE": "FALSE");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800538 }
539
540 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
542
543
544/************************************************************************/
545/************************************************************************/
546/* U S B C A L L B A C K F U N C T I O N S */
547/* U S B C A L L B A C K F U N C T I O N S */
548/************************************************************************/
549/************************************************************************/
550
551/*****************************************************************************
552 * edge_interrupt_callback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100553 * this is the callback function for when we have received data on the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 * interrupt endpoint.
555 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100556static void edge_interrupt_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700558 struct edgeport_serial *edge_serial = urb->context;
559 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 struct edgeport_port *edge_port;
561 struct usb_serial_port *port;
562 unsigned char *data = urb->transfer_buffer;
563 int length = urb->actual_length;
564 int bytes_avail;
565 int position;
566 int txCredits;
567 int portNumber;
568 int result;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700569 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700571 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 case 0:
573 /* success */
574 break;
575 case -ECONNRESET:
576 case -ENOENT:
577 case -ESHUTDOWN:
578 /* this urb is terminated, clean up */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700579 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 return;
581 default:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700582 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 goto exit;
584 }
585
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700586 dev = &edge_serial->serial->dev->dev;
587
Alan Cox03f0dbf2008-07-22 11:16:34 +0100588 /* process this interrupt-read even if there are no ports open */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (length) {
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100590 usb_serial_debug_data(dev, __func__, length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 if (length > 1) {
593 bytes_avail = data[0] | (data[1] << 8);
594 if (bytes_avail) {
595 spin_lock(&edge_serial->es_lock);
596 edge_serial->rxBytesAvail += bytes_avail;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700597 dev_dbg(dev,
598 "%s - bytes_avail=%d, rxBytesAvail=%d, read_in_progress=%d\n",
599 __func__, bytes_avail,
600 edge_serial->rxBytesAvail,
601 edge_serial->read_in_progress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 if (edge_serial->rxBytesAvail > 0 &&
604 !edge_serial->read_in_progress) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700605 dev_dbg(dev, "%s - posting a read\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100606 edge_serial->read_in_progress = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Alan Cox03f0dbf2008-07-22 11:16:34 +0100608 /* we have pending bytes on the
609 bulk in pipe, send a request */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 result = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
611 if (result) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700612 dev_err(dev,
613 "%s - usb_submit_urb(read bulk) failed with result = %d\n",
614 __func__, result);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100615 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
617 }
618 spin_unlock(&edge_serial->es_lock);
619 }
620 }
621 /* grab the txcredits for the ports if available */
622 position = 2;
623 portNumber = 0;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100624 while ((position < length) &&
625 (portNumber < edge_serial->serial->num_ports)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 txCredits = data[position] | (data[position+1] << 8);
627 if (txCredits) {
628 port = edge_serial->serial->port[portNumber];
629 edge_port = usb_get_serial_port_data(port);
630 if (edge_port->open) {
631 spin_lock(&edge_port->ep_lock);
632 edge_port->txCredits += txCredits;
633 spin_unlock(&edge_port->ep_lock);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700634 dev_dbg(dev, "%s - txcredits for port%d = %d\n",
635 __func__, portNumber,
636 edge_port->txCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Alan Cox03f0dbf2008-07-22 11:16:34 +0100638 /* tell the tty driver that something
639 has changed */
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100640 tty_port_tty_wakeup(&edge_port->port->port);
Alan Cox03f0dbf2008-07-22 11:16:34 +0100641 /* Since we have more credit, check
642 if more data can be sent */
643 send_more_port_data(edge_serial,
644 edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
646 }
647 position += 2;
648 ++portNumber;
649 }
650 }
651
652exit:
Alan Cox03f0dbf2008-07-22 11:16:34 +0100653 result = usb_submit_urb(urb, GFP_ATOMIC);
654 if (result)
655 dev_err(&urb->dev->dev,
656 "%s - Error %d submitting control urb\n",
657 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
660
661/*****************************************************************************
662 * edge_bulk_in_callback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100663 * this is the callback function for when we have received data on the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 * bulk in endpoint.
665 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100666static void edge_bulk_in_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Ming Leicdc97792008-02-24 18:41:47 +0800668 struct edgeport_serial *edge_serial = urb->context;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700669 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700671 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 __u16 raw_data_length;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700673 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700675 if (status) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700676 dev_dbg(&urb->dev->dev, "%s - nonzero read bulk status received: %d\n",
677 __func__, status);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100678 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 return;
680 }
681
682 if (urb->actual_length == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700683 dev_dbg(&urb->dev->dev, "%s - read bulk callback with no data\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100684 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 return;
686 }
687
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700688 dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 raw_data_length = urb->actual_length;
690
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100691 usb_serial_debug_data(dev, __func__, raw_data_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 spin_lock(&edge_serial->es_lock);
694
695 /* decrement our rxBytes available by the number that we just got */
696 edge_serial->rxBytesAvail -= raw_data_length;
697
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700698 dev_dbg(dev, "%s - Received = %d, rxBytesAvail %d\n", __func__,
699 raw_data_length, edge_serial->rxBytesAvail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Alan Cox03f0dbf2008-07-22 11:16:34 +0100701 process_rcvd_data(edge_serial, data, urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 /* check to see if there's any more data for us to read */
704 if (edge_serial->rxBytesAvail > 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700705 dev_dbg(dev, "%s - posting a read\n", __func__);
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700706 retval = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
707 if (retval) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700708 dev_err(dev,
709 "%s - usb_submit_urb(read bulk) failed, retval = %d\n",
710 __func__, retval);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100711 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
713 } else {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100714 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 }
716
717 spin_unlock(&edge_serial->es_lock);
718}
719
720
721/*****************************************************************************
722 * edge_bulk_out_data_callback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100723 * this is the callback function for when we have finished sending
724 * serial data on the bulk out endpoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100726static void edge_bulk_out_data_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
Ming Leicdc97792008-02-24 18:41:47 +0800728 struct edgeport_port *edge_port = urb->context;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700729 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700731 if (status) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700732 dev_dbg(&urb->dev->dev,
733 "%s - nonzero write bulk status received: %d\n",
734 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
736
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100737 if (edge_port->open)
738 tty_port_tty_wakeup(&edge_port->port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Alan Cox03f0dbf2008-07-22 11:16:34 +0100740 /* Release the Write URB */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100741 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Alan Cox03f0dbf2008-07-22 11:16:34 +0100743 /* Check if more data needs to be sent */
744 send_more_port_data((struct edgeport_serial *)
745 (usb_get_serial_data(edge_port->port->serial)), edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746}
747
748
749/*****************************************************************************
750 * BulkOutCmdCallback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100751 * this is the callback function for when we have finished sending a
752 * command on the bulk out endpoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100754static void edge_bulk_out_cmd_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
Ming Leicdc97792008-02-24 18:41:47 +0800756 struct edgeport_port *edge_port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 int status = urb->status;
758
Oliver Neukum96c706e2007-03-15 15:27:17 +0100759 atomic_dec(&CmdUrbs);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700760 dev_dbg(&urb->dev->dev, "%s - FREE URB %p (outstanding %d)\n",
761 __func__, urb, atomic_read(&CmdUrbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763
764 /* clean up the transfer buffer */
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700765 kfree(urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 /* Free the command urb */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100768 usb_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 if (status) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700771 dev_dbg(&urb->dev->dev,
772 "%s - nonzero write bulk status received: %d\n",
773 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 return;
775 }
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 /* tell the tty driver that something has changed */
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100778 if (edge_port->open)
779 tty_port_tty_wakeup(&edge_port->port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 /* we have completed the command */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100782 edge_port->commandPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 wake_up(&edge_port->wait_command);
784}
785
786
787/*****************************************************************************
788 * Driver tty interface functions
789 *****************************************************************************/
790
791/*****************************************************************************
792 * SerialOpen
793 * this function is called by the tty driver when a port is opened
794 * If successful, we return 0
795 * Otherwise we return a negative error number.
796 *****************************************************************************/
Alan Coxa509a7e2009-09-19 13:13:26 -0700797static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
799 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700800 struct device *dev = &port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 struct usb_serial *serial;
802 struct edgeport_serial *edge_serial;
803 int response;
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (edge_port == NULL)
806 return -ENODEV;
807
Alan Cox03f0dbf2008-07-22 11:16:34 +0100808 /* see if we've set up our endpoint info yet (can't set it up
809 in edge_startup as the structures were not set up at that time.) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 serial = port->serial;
811 edge_serial = usb_get_serial_data(serial);
Alan Cox95da3102008-07-22 11:09:07 +0100812 if (edge_serial == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if (edge_serial->interrupt_in_buffer == NULL) {
815 struct usb_serial_port *port0 = serial->port[0];
Alan Cox03f0dbf2008-07-22 11:16:34 +0100816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 /* not set up yet, so do it now */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100818 edge_serial->interrupt_in_buffer =
819 port0->interrupt_in_buffer;
820 edge_serial->interrupt_in_endpoint =
821 port0->interrupt_in_endpointAddress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 edge_serial->interrupt_read_urb = port0->interrupt_in_urb;
823 edge_serial->bulk_in_buffer = port0->bulk_in_buffer;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100824 edge_serial->bulk_in_endpoint =
825 port0->bulk_in_endpointAddress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 edge_serial->read_urb = port0->read_urb;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100827 edge_serial->bulk_out_endpoint =
828 port0->bulk_out_endpointAddress;
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 /* set up our interrupt urb */
831 usb_fill_int_urb(edge_serial->interrupt_read_urb,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100832 serial->dev,
833 usb_rcvintpipe(serial->dev,
834 port0->interrupt_in_endpointAddress),
835 port0->interrupt_in_buffer,
836 edge_serial->interrupt_read_urb->transfer_buffer_length,
837 edge_interrupt_callback, edge_serial,
838 edge_serial->interrupt_read_urb->interval);
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 /* set up our bulk in urb */
841 usb_fill_bulk_urb(edge_serial->read_urb, serial->dev,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100842 usb_rcvbulkpipe(serial->dev,
843 port0->bulk_in_endpointAddress),
844 port0->bulk_in_buffer,
845 edge_serial->read_urb->transfer_buffer_length,
846 edge_bulk_in_callback, edge_serial);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100847 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 /* start interrupt read for this edgeport
Alan Cox03f0dbf2008-07-22 11:16:34 +0100850 * this interrupt will continue as long
851 * as the edgeport is connected */
852 response = usb_submit_urb(edge_serial->interrupt_read_urb,
853 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 if (response) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700855 dev_err(dev, "%s - Error %d submitting control urb\n",
856 __func__, response);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 }
858 }
Alan Cox03f0dbf2008-07-22 11:16:34 +0100859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 /* initialize our wait queues */
861 init_waitqueue_head(&edge_port->wait_open);
862 init_waitqueue_head(&edge_port->wait_chase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 init_waitqueue_head(&edge_port->wait_command);
864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 /* initialize our port settings */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100866 edge_port->txCredits = 0; /* Can't send any data yet */
867 /* Must always set this bit to enable ints! */
868 edge_port->shadowMCR = MCR_MASTER_IE;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100869 edge_port->chaseResponsePending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871 /* send a open port command */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100872 edge_port->openPending = true;
873 edge_port->open = false;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100874 response = send_iosp_ext_cmd(edge_port, IOSP_CMD_OPEN_PORT, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 if (response < 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700877 dev_err(dev, "%s - error sending open port command\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100878 edge_port->openPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 return -ENODEV;
880 }
881
882 /* now wait for the port to be completely opened */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100883 wait_event_timeout(edge_port->wait_open, !edge_port->openPending,
884 OPEN_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100886 if (!edge_port->open) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 /* open timed out */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700888 dev_dbg(dev, "%s - open timedout\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100889 edge_port->openPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 return -ENODEV;
891 }
892
893 /* create the txfifo */
894 edge_port->txfifo.head = 0;
895 edge_port->txfifo.tail = 0;
896 edge_port->txfifo.count = 0;
897 edge_port->txfifo.size = edge_port->maxTxCredits;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100898 edge_port->txfifo.fifo = kmalloc(edge_port->maxTxCredits, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 if (!edge_port->txfifo.fifo) {
Alan Cox335f8512009-06-11 12:26:29 +0100901 edge_close(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 return -ENOMEM;
903 }
904
905 /* Allocate a URB for the write */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100906 edge_port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100907 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 if (!edge_port->write_urb) {
Alan Cox335f8512009-06-11 12:26:29 +0100910 edge_close(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 return -ENOMEM;
912 }
913
Greg Kroah-Hartman11438322013-06-06 10:32:00 -0700914 dev_dbg(dev, "%s - Initialize TX fifo to %d bytes\n",
915 __func__, edge_port->maxTxCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917 return 0;
918}
919
920
921/************************************************************************
922 *
923 * block_until_chase_response
924 *
925 * This function will block the close until one of the following:
926 * 1. Response to our Chase comes from Edgeport
Joe Perchesdc0d5c12007-12-17 11:40:18 -0800927 * 2. A timeout of 10 seconds without activity has expired
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 * (1K of Edgeport data @ 2400 baud ==> 4 sec to empty)
929 *
930 ************************************************************************/
931static void block_until_chase_response(struct edgeport_port *edge_port)
932{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700933 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 DEFINE_WAIT(wait);
935 __u16 lastCredits;
936 int timeout = 1*HZ;
937 int loop = 10;
938
939 while (1) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100940 /* Save Last credits */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 lastCredits = edge_port->txCredits;
942
Alan Cox03f0dbf2008-07-22 11:16:34 +0100943 /* Did we get our Chase response */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100944 if (!edge_port->chaseResponsePending) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700945 dev_dbg(dev, "%s - Got Chase Response\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Alan Cox03f0dbf2008-07-22 11:16:34 +0100947 /* did we get all of our credit back? */
948 if (edge_port->txCredits == edge_port->maxTxCredits) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700949 dev_dbg(dev, "%s - Got all credits\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 return;
951 }
952 }
953
Alan Cox03f0dbf2008-07-22 11:16:34 +0100954 /* Block the thread for a while */
955 prepare_to_wait(&edge_port->wait_chase, &wait,
956 TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 schedule_timeout(timeout);
958 finish_wait(&edge_port->wait_chase, &wait);
959
960 if (lastCredits == edge_port->txCredits) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100961 /* No activity.. count down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 loop--;
963 if (loop == 0) {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100964 edge_port->chaseResponsePending = false;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700965 dev_dbg(dev, "%s - Chase TIMEOUT\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 return;
967 }
968 } else {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100969 /* Reset timeout value back to 10 seconds */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700970 dev_dbg(dev, "%s - Last %d, Current %d\n", __func__,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100971 lastCredits, edge_port->txCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 loop = 10;
973 }
974 }
975}
976
977
978/************************************************************************
979 *
980 * block_until_tx_empty
981 *
982 * This function will block the close until one of the following:
983 * 1. TX count are 0
984 * 2. The edgeport has stopped
Joe Perchesdc0d5c12007-12-17 11:40:18 -0800985 * 3. A timeout of 3 seconds without activity has expired
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 *
987 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100988static void block_until_tx_empty(struct edgeport_port *edge_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700990 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 DEFINE_WAIT(wait);
992 struct TxFifo *fifo = &edge_port->txfifo;
993 __u32 lastCount;
994 int timeout = HZ/10;
995 int loop = 30;
996
997 while (1) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100998 /* Save Last count */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 lastCount = fifo->count;
1000
Alan Cox03f0dbf2008-07-22 11:16:34 +01001001 /* Is the Edgeport Buffer empty? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 if (lastCount == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001003 dev_dbg(dev, "%s - TX Buffer Empty\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 return;
1005 }
1006
Alan Cox03f0dbf2008-07-22 11:16:34 +01001007 /* Block the thread for a while */
1008 prepare_to_wait(&edge_port->wait_chase, &wait,
1009 TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 schedule_timeout(timeout);
1011 finish_wait(&edge_port->wait_chase, &wait);
1012
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001013 dev_dbg(dev, "%s wait\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 if (lastCount == fifo->count) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001016 /* No activity.. count down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 loop--;
1018 if (loop == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001019 dev_dbg(dev, "%s - TIMEOUT\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 return;
1021 }
1022 } else {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001023 /* Reset timeout value back to seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 loop = 30;
1025 }
1026 }
1027}
1028
1029
1030/*****************************************************************************
1031 * edge_close
1032 * this function is called by the tty driver when a port is closed
1033 *****************************************************************************/
Alan Cox335f8512009-06-11 12:26:29 +01001034static void edge_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
1036 struct edgeport_serial *edge_serial;
1037 struct edgeport_port *edge_port;
1038 int status;
1039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 edge_serial = usb_get_serial_data(port->serial);
1041 edge_port = usb_get_serial_port_data(port);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001042 if (edge_serial == NULL || edge_port == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 return;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001044
1045 /* block until tx is empty */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 block_until_tx_empty(edge_port);
1047
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001048 edge_port->closePending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001050 if ((!edge_serial->is_epic) ||
1051 ((edge_serial->is_epic) &&
1052 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1053 /* flush and chase */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001054 edge_port->chaseResponsePending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001056 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001057 status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
1058 if (status == 0)
1059 /* block until chase finished */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001060 block_until_chase_response(edge_port);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001061 else
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001062 edge_port->chaseResponsePending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 }
1064
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001065 if ((!edge_serial->is_epic) ||
1066 ((edge_serial->is_epic) &&
1067 (edge_serial->epic_descriptor.Supports.IOSPClose))) {
1068 /* close the port */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001069 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CLOSE_PORT\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001070 send_iosp_ext_cmd(edge_port, IOSP_CMD_CLOSE_PORT, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Alan Cox03f0dbf2008-07-22 11:16:34 +01001073 /* port->close = true; */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001074 edge_port->closePending = false;
1075 edge_port->open = false;
1076 edge_port->openPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Mariusz Kozlowski9a25f442006-11-08 15:36:29 +01001078 usb_kill_urb(edge_port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
1080 if (edge_port->write_urb) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001081 /* if this urb had a transfer buffer already
1082 (old transfer) free it */
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001083 kfree(edge_port->write_urb->transfer_buffer);
1084 usb_free_urb(edge_port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 edge_port->write_urb = NULL;
1086 }
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001087 kfree(edge_port->txfifo.fifo);
1088 edge_port->txfifo.fifo = NULL;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001089}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
1091/*****************************************************************************
1092 * SerialWrite
Alan Cox03f0dbf2008-07-22 11:16:34 +01001093 * this function is called by the tty driver when data should be written
1094 * to the port.
1095 * If successful, we return the number of bytes written, otherwise we
1096 * return a negative error number.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001098static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
1099 const unsigned char *data, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100{
1101 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1102 struct TxFifo *fifo;
1103 int copySize;
1104 int bytesleft;
1105 int firsthalf;
1106 int secondhalf;
1107 unsigned long flags;
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 if (edge_port == NULL)
1110 return -ENODEV;
1111
Alan Cox03f0dbf2008-07-22 11:16:34 +01001112 /* get a pointer to the Tx fifo */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 fifo = &edge_port->txfifo;
1114
1115 spin_lock_irqsave(&edge_port->ep_lock, flags);
1116
Alan Cox03f0dbf2008-07-22 11:16:34 +01001117 /* calculate number of bytes to put in fifo */
1118 copySize = min((unsigned int)count,
1119 (edge_port->txCredits - fifo->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001121 dev_dbg(&port->dev, "%s of %d byte(s) Fifo room %d -- will copy %d bytes\n",
1122 __func__, count, edge_port->txCredits - fifo->count, copySize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Alan Cox03f0dbf2008-07-22 11:16:34 +01001124 /* catch writes of 0 bytes which the tty driver likes to give us,
1125 and when txCredits is empty */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 if (copySize == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001127 dev_dbg(&port->dev, "%s - copySize = Zero\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 goto finish_write;
1129 }
1130
Alan Cox03f0dbf2008-07-22 11:16:34 +01001131 /* queue the data
1132 * since we can never overflow the buffer we do not have to check for a
1133 * full condition
1134 *
1135 * the copy is done is two parts -- first fill to the end of the buffer
1136 * then copy the reset from the start of the buffer
1137 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 bytesleft = fifo->size - fifo->head;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001139 firsthalf = min(bytesleft, copySize);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001140 dev_dbg(&port->dev, "%s - copy %d bytes of %d into fifo \n", __func__,
1141 firsthalf, bytesleft);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
1143 /* now copy our data */
1144 memcpy(&fifo->fifo[fifo->head], data, firsthalf);
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001145 usb_serial_debug_data(&port->dev, __func__, firsthalf, &fifo->fifo[fifo->head]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Alan Cox03f0dbf2008-07-22 11:16:34 +01001147 /* update the index and size */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 fifo->head += firsthalf;
1149 fifo->count += firsthalf;
1150
Alan Cox03f0dbf2008-07-22 11:16:34 +01001151 /* wrap the index */
1152 if (fifo->head == fifo->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 fifo->head = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
1155 secondhalf = copySize-firsthalf;
1156
1157 if (secondhalf) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001158 dev_dbg(&port->dev, "%s - copy rest of data %d\n", __func__, secondhalf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 memcpy(&fifo->fifo[fifo->head], &data[firsthalf], secondhalf);
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001160 usb_serial_debug_data(&port->dev, __func__, secondhalf, &fifo->fifo[fifo->head]);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001161 /* update the index and size */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 fifo->count += secondhalf;
1163 fifo->head += secondhalf;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001164 /* No need to check for wrap since we can not get to end of
1165 * the fifo in this part
1166 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 }
1168
1169finish_write:
1170 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1171
Alan Cox03f0dbf2008-07-22 11:16:34 +01001172 send_more_port_data((struct edgeport_serial *)
1173 usb_get_serial_data(port->serial), edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001175 dev_dbg(&port->dev, "%s wrote %d byte(s) TxCredits %d, Fifo %d\n",
1176 __func__, copySize, edge_port->txCredits, fifo->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Alan Cox03f0dbf2008-07-22 11:16:34 +01001178 return copySize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179}
1180
1181
1182/************************************************************************
1183 *
1184 * send_more_port_data()
1185 *
1186 * This routine attempts to write additional UART transmit data
1187 * to a port over the USB bulk pipe. It is called (1) when new
1188 * data has been written to a port's TxBuffer from higher layers
1189 * (2) when the peripheral sends us additional TxCredits indicating
1190 * that it can accept more Tx data for a given port; and (3) when
1191 * a bulk write completes successfully and we want to see if we
1192 * can transmit more.
1193 *
1194 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001195static void send_more_port_data(struct edgeport_serial *edge_serial,
1196 struct edgeport_port *edge_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197{
1198 struct TxFifo *fifo = &edge_port->txfifo;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001199 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 struct urb *urb;
1201 unsigned char *buffer;
1202 int status;
1203 int count;
1204 int bytesleft;
1205 int firsthalf;
1206 int secondhalf;
1207 unsigned long flags;
1208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 spin_lock_irqsave(&edge_port->ep_lock, flags);
1210
1211 if (edge_port->write_in_progress ||
1212 !edge_port->open ||
1213 (fifo->count == 0)) {
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001214 dev_dbg(dev, "%s EXIT - fifo %d, PendingWrite = %d\n",
1215 __func__, fifo->count, edge_port->write_in_progress);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 goto exit_send;
1217 }
1218
Alan Cox03f0dbf2008-07-22 11:16:34 +01001219 /* since the amount of data in the fifo will always fit into the
1220 * edgeport buffer we do not need to check the write length
1221 *
1222 * Do we have enough credits for this port to make it worthwhile
1223 * to bother queueing a write. If it's too small, say a few bytes,
1224 * it's better to wait for more credits so we can do a larger write.
1225 */
1226 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 -07001227 dev_dbg(dev, "%s Not enough credit - fifo %d TxCredit %d\n",
1228 __func__, fifo->count, edge_port->txCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 goto exit_send;
1230 }
1231
Alan Cox03f0dbf2008-07-22 11:16:34 +01001232 /* lock this write */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001233 edge_port->write_in_progress = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Alan Cox03f0dbf2008-07-22 11:16:34 +01001235 /* get a pointer to the write_urb */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 urb = edge_port->write_urb;
1237
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001238 /* make sure transfer buffer is freed */
1239 kfree(urb->transfer_buffer);
1240 urb->transfer_buffer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Alan Cox03f0dbf2008-07-22 11:16:34 +01001242 /* build the data header for the buffer and port that we are about
1243 to send out */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 count = fifo->count;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001245 buffer = kmalloc(count+2, GFP_ATOMIC);
Johan Hovold10c642d2013-12-29 19:22:56 +01001246 if (!buffer) {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001247 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 goto exit_send;
1249 }
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001250 buffer[0] = IOSP_BUILD_DATA_HDR1(edge_port->port->port_number, count);
1251 buffer[1] = IOSP_BUILD_DATA_HDR2(edge_port->port->port_number, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253 /* now copy our data */
1254 bytesleft = fifo->size - fifo->tail;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001255 firsthalf = min(bytesleft, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 memcpy(&buffer[2], &fifo->fifo[fifo->tail], firsthalf);
1257 fifo->tail += firsthalf;
1258 fifo->count -= firsthalf;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001259 if (fifo->tail == fifo->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 fifo->tail = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
1262 secondhalf = count-firsthalf;
1263 if (secondhalf) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001264 memcpy(&buffer[2+firsthalf], &fifo->fifo[fifo->tail],
1265 secondhalf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 fifo->tail += secondhalf;
1267 fifo->count -= secondhalf;
1268 }
1269
1270 if (count)
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001271 usb_serial_debug_data(&edge_port->port->dev, __func__, count, &buffer[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273 /* fill up the urb with all of our data and submit it */
Alan Cox03f0dbf2008-07-22 11:16:34 +01001274 usb_fill_bulk_urb(urb, edge_serial->serial->dev,
1275 usb_sndbulkpipe(edge_serial->serial->dev,
1276 edge_serial->bulk_out_endpoint),
1277 buffer, count+2,
1278 edge_bulk_out_data_callback, edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
1280 /* decrement the number of credits we have by the number we just sent */
1281 edge_port->txCredits -= count;
Johan Hovoldd36a7712013-03-21 12:37:07 +01001282 edge_port->port->icount.tx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 status = usb_submit_urb(urb, GFP_ATOMIC);
1285 if (status) {
1286 /* something went wrong */
Johan Hovold22a416c2012-02-10 13:20:51 +01001287 dev_err_console(edge_port->port,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001288 "%s - usb_submit_urb(write bulk) failed, status = %d, data lost\n",
1289 __func__, status);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001290 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292 /* revert the credits as something bad happened. */
1293 edge_port->txCredits += count;
Johan Hovoldd36a7712013-03-21 12:37:07 +01001294 edge_port->port->icount.tx -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001296 dev_dbg(dev, "%s wrote %d byte(s) TxCredit %d, Fifo %d\n",
1297 __func__, count, edge_port->txCredits, fifo->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299exit_send:
1300 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1301}
1302
1303
1304/*****************************************************************************
1305 * edge_write_room
Alan Cox03f0dbf2008-07-22 11:16:34 +01001306 * this function is called by the tty driver when it wants to know how
1307 * many bytes of data we can accept for a specific port. If successful,
1308 * we return the amount of room that we have for this port (the txCredits)
1309 * otherwise we return a negative error number.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001311static int edge_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
Alan Cox95da3102008-07-22 11:09:07 +01001313 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1315 int room;
1316 unsigned long flags;
1317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 if (edge_port == NULL)
Alan Coxd76f2f42008-07-22 11:16:42 +01001319 return 0;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001320 if (edge_port->closePending)
Alan Coxd76f2f42008-07-22 11:16:42 +01001321 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001324 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Alan Coxd76f2f42008-07-22 11:16:42 +01001325 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 }
1327
Alan Cox03f0dbf2008-07-22 11:16:34 +01001328 /* total of both buffers is still txCredit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 spin_lock_irqsave(&edge_port->ep_lock, flags);
1330 room = edge_port->txCredits - edge_port->txfifo.count;
1331 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1332
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001333 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 return room;
1335}
1336
1337
1338/*****************************************************************************
1339 * edge_chars_in_buffer
Alan Cox03f0dbf2008-07-22 11:16:34 +01001340 * this function is called by the tty driver when it wants to know how
1341 * many bytes of data we currently have outstanding in the port (data that
1342 * has been written, but hasn't made it out the port yet)
1343 * If successful, we return the number of bytes left to be written in the
1344 * system,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 * Otherwise we return a negative error number.
1346 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001347static int edge_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348{
Alan Cox95da3102008-07-22 11:09:07 +01001349 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1351 int num_chars;
1352 unsigned long flags;
1353
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 if (edge_port == NULL)
Alan Coxd76f2f42008-07-22 11:16:42 +01001355 return 0;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001356 if (edge_port->closePending)
Alan Coxd76f2f42008-07-22 11:16:42 +01001357 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001360 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Alan Coxd76f2f42008-07-22 11:16:42 +01001361 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 }
1363
1364 spin_lock_irqsave(&edge_port->ep_lock, flags);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001365 num_chars = edge_port->maxTxCredits - edge_port->txCredits +
1366 edge_port->txfifo.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1368 if (num_chars) {
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001369 dev_dbg(&port->dev, "%s - returns %d\n", __func__, num_chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 }
1371
1372 return num_chars;
1373}
1374
1375
1376/*****************************************************************************
1377 * SerialThrottle
1378 * this function is called by the tty driver when it wants to stop the data
1379 * being read from the port.
1380 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001381static void edge_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
Alan Cox95da3102008-07-22 11:09:07 +01001383 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 int status;
1386
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 if (edge_port == NULL)
1388 return;
1389
1390 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001391 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 return;
1393 }
1394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 /* if we are implementing XON/XOFF, send the stop character */
1396 if (I_IXOFF(tty)) {
1397 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001398 status = edge_write(tty, port, &stop_char, 1);
1399 if (status <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 }
1402
1403 /* if we are implementing RTS/CTS, toggle that line */
Alan Coxadc8d742012-07-14 15:31:47 +01001404 if (tty->termios.c_cflag & CRTSCTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 edge_port->shadowMCR &= ~MCR_RTS;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001406 status = send_cmd_write_uart_register(edge_port, MCR,
1407 edge_port->shadowMCR);
1408 if (status != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411}
1412
1413
1414/*****************************************************************************
1415 * edge_unthrottle
Alan Cox03f0dbf2008-07-22 11:16:34 +01001416 * this function is called by the tty driver when it wants to resume the
1417 * data being read from the port (called after SerialThrottle is called)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001419static void edge_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
Alan Cox95da3102008-07-22 11:09:07 +01001421 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 int status;
1424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 if (edge_port == NULL)
1426 return;
1427
1428 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001429 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 return;
1431 }
1432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 /* if we are implementing XON/XOFF, send the start character */
1434 if (I_IXOFF(tty)) {
1435 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001436 status = edge_write(tty, port, &start_char, 1);
1437 if (status <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 /* if we are implementing RTS/CTS, toggle that line */
Alan Coxadc8d742012-07-14 15:31:47 +01001441 if (tty->termios.c_cflag & CRTSCTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 edge_port->shadowMCR |= MCR_RTS;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001443 send_cmd_write_uart_register(edge_port, MCR,
1444 edge_port->shadowMCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446}
1447
1448
1449/*****************************************************************************
1450 * SerialSetTermios
Alan Cox03f0dbf2008-07-22 11:16:34 +01001451 * this function is called by the tty driver when it wants to change
1452 * the termios structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001454static void edge_set_termios(struct tty_struct *tty,
1455 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456{
1457 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 unsigned int cflag;
1459
Alan Coxadc8d742012-07-14 15:31:47 +01001460 cflag = tty->termios.c_cflag;
Linus Torvaldsd9a80742012-10-01 13:23:01 -07001461 dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__, tty->termios.c_cflag, tty->termios.c_iflag);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001462 dev_dbg(&port->dev, "%s - old clfag %08x old iflag %08x\n", __func__, old_termios->c_cflag, old_termios->c_iflag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
1464 if (edge_port == NULL)
1465 return;
1466
1467 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001468 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 return;
1470 }
1471
1472 /* change the port settings to the new ones specified */
Alan Cox95da3102008-07-22 11:09:07 +01001473 change_port_settings(tty, edge_port, old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474}
1475
1476
1477/*****************************************************************************
1478 * get_lsr_info - get line status register info
1479 *
1480 * Purpose: Let user call ioctl() to get info when the UART physically
1481 * is emptied. On bus types like RS485, the transmitter must
1482 * release the bus after transmitting. This must be done when
1483 * the transmit shift register is empty, not be done when the
1484 * transmit holding register is empty. This functionality
Alan Cox03f0dbf2008-07-22 11:16:34 +01001485 * allows an RS485 driver to be written in user space.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001487static int get_lsr_info(struct edgeport_port *edge_port,
1488 unsigned int __user *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489{
1490 unsigned int result = 0;
1491 unsigned long flags;
1492
1493 spin_lock_irqsave(&edge_port->ep_lock, flags);
1494 if (edge_port->maxTxCredits == edge_port->txCredits &&
1495 edge_port->txfifo.count == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001496 dev_dbg(&edge_port->port->dev, "%s -- Empty\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 result = TIOCSER_TEMT;
1498 }
1499 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1500
1501 if (copy_to_user(value, &result, sizeof(int)))
1502 return -EFAULT;
1503 return 0;
1504}
1505
Alan Cox20b9d172011-02-14 16:26:50 +00001506static int edge_tiocmset(struct tty_struct *tty,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001507 unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
Alan Cox95da3102008-07-22 11:09:07 +01001509 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1511 unsigned int mcr;
1512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 mcr = edge_port->shadowMCR;
1514 if (set & TIOCM_RTS)
1515 mcr |= MCR_RTS;
1516 if (set & TIOCM_DTR)
1517 mcr |= MCR_DTR;
1518 if (set & TIOCM_LOOP)
1519 mcr |= MCR_LOOPBACK;
1520
1521 if (clear & TIOCM_RTS)
1522 mcr &= ~MCR_RTS;
1523 if (clear & TIOCM_DTR)
1524 mcr &= ~MCR_DTR;
1525 if (clear & TIOCM_LOOP)
1526 mcr &= ~MCR_LOOPBACK;
1527
1528 edge_port->shadowMCR = mcr;
1529
1530 send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1531
1532 return 0;
1533}
1534
Alan Cox60b33c12011-02-14 16:26:14 +00001535static int edge_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536{
Alan Cox95da3102008-07-22 11:09:07 +01001537 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1539 unsigned int result = 0;
1540 unsigned int msr;
1541 unsigned int mcr;
1542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 msr = edge_port->shadowMSR;
1544 mcr = edge_port->shadowMCR;
1545 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
1546 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
1547 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
1548 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
1549 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
1550 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
1551
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 return result;
1553}
1554
Alan Cox03f0dbf2008-07-22 11:16:34 +01001555static int get_serial_info(struct edgeport_port *edge_port,
1556 struct serial_struct __user *retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
1558 struct serial_struct tmp;
1559
1560 if (!retinfo)
1561 return -EFAULT;
1562
1563 memset(&tmp, 0, sizeof(tmp));
1564
1565 tmp.type = PORT_16550A;
Greg Kroah-Hartmane5b1e202013-06-07 11:04:28 -07001566 tmp.line = edge_port->port->minor;
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001567 tmp.port = edge_port->port->port_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 tmp.irq = 0;
1569 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1570 tmp.xmit_fifo_size = edge_port->maxTxCredits;
1571 tmp.baud_base = 9600;
1572 tmp.close_delay = 5*HZ;
1573 tmp.closing_wait = 30*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
1575 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1576 return -EFAULT;
1577 return 0;
1578}
1579
1580
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581/*****************************************************************************
1582 * SerialIoctl
1583 * this function handles any ioctl calls to the driver
1584 *****************************************************************************/
Alan Cox00a0d0d2011-02-14 16:27:06 +00001585static int edge_ioctl(struct tty_struct *tty,
Alan Cox95da3102008-07-22 11:09:07 +01001586 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587{
Alan Cox95da3102008-07-22 11:09:07 +01001588 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 DEFINE_WAIT(wait);
1590 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 switch (cmd) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001593 case TIOCSERGETLSR:
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001594 dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001595 return get_lsr_info(edge_port, (unsigned int __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Alan Cox03f0dbf2008-07-22 11:16:34 +01001597 case TIOCGSERIAL:
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001598 dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001599 return get_serial_info(edge_port, (struct serial_struct __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 return -ENOIOCTLCMD;
1602}
1603
1604
1605/*****************************************************************************
1606 * SerialBreak
1607 * this function sends a break to the port
1608 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001609static void edge_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610{
Alan Cox95da3102008-07-22 11:09:07 +01001611 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001613 struct edgeport_serial *edge_serial = usb_get_serial_data(port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 int status;
1615
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001616 if ((!edge_serial->is_epic) ||
1617 ((edge_serial->is_epic) &&
1618 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1619 /* flush and chase */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001620 edge_port->chaseResponsePending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001622 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001623 status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001624 if (status == 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001625 /* block until chase finished */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001626 block_until_chase_response(edge_port);
1627 } else {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001628 edge_port->chaseResponsePending = false;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 }
1631
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001632 if ((!edge_serial->is_epic) ||
1633 ((edge_serial->is_epic) &&
1634 (edge_serial->epic_descriptor.Supports.IOSPSetClrBreak))) {
1635 if (break_state == -1) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001636 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_SET_BREAK\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001637 status = send_iosp_ext_cmd(edge_port,
1638 IOSP_CMD_SET_BREAK, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001639 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001640 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CLEAR_BREAK\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001641 status = send_iosp_ext_cmd(edge_port,
1642 IOSP_CMD_CLEAR_BREAK, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001643 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01001644 if (status)
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001645 dev_dbg(&port->dev, "%s - error sending break set/clear command.\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01001646 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648}
1649
1650
1651/*****************************************************************************
1652 * process_rcvd_data
1653 * this function handles the data received on the bulk in pipe.
1654 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001655static void process_rcvd_data(struct edgeport_serial *edge_serial,
1656 unsigned char *buffer, __u16 bufferLength)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001658 struct device *dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 struct usb_serial_port *port;
1660 struct edgeport_port *edge_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 __u16 lastBufferLength;
1662 __u16 rxLen;
1663
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 lastBufferLength = bufferLength + 1;
1665
1666 while (bufferLength > 0) {
1667 /* failsafe incase we get a message that we don't understand */
1668 if (lastBufferLength == bufferLength) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001669 dev_dbg(dev, "%s - stuck in loop, exiting it.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 break;
1671 }
1672 lastBufferLength = bufferLength;
1673
1674 switch (edge_serial->rxState) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001675 case EXPECT_HDR1:
1676 edge_serial->rxHeader1 = *buffer;
1677 ++buffer;
1678 --bufferLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Alan Cox03f0dbf2008-07-22 11:16:34 +01001680 if (bufferLength == 0) {
1681 edge_serial->rxState = EXPECT_HDR2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 break;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001683 }
1684 /* otherwise, drop on through */
1685 case EXPECT_HDR2:
1686 edge_serial->rxHeader2 = *buffer;
1687 ++buffer;
1688 --bufferLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001690 dev_dbg(dev, "%s - Hdr1=%02X Hdr2=%02X\n", __func__,
1691 edge_serial->rxHeader1, edge_serial->rxHeader2);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001692 /* Process depending on whether this header is
1693 * data or status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Alan Cox03f0dbf2008-07-22 11:16:34 +01001695 if (IS_CMD_STAT_HDR(edge_serial->rxHeader1)) {
1696 /* Decode this status header and go to
1697 * EXPECT_HDR1 (if we can process the status
1698 * with only 2 bytes), or go to EXPECT_HDR3 to
1699 * get the third byte. */
1700 edge_serial->rxPort =
1701 IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1702 edge_serial->rxStatusCode =
1703 IOSP_GET_STATUS_CODE(
1704 edge_serial->rxHeader1);
1705
1706 if (!IOSP_STATUS_IS_2BYTE(
1707 edge_serial->rxStatusCode)) {
1708 /* This status needs additional bytes.
1709 * Save what we have and then wait for
1710 * more data.
1711 */
1712 edge_serial->rxStatusParam
1713 = edge_serial->rxHeader2;
1714 edge_serial->rxState = EXPECT_HDR3;
1715 break;
1716 }
1717 /* We have all the header bytes, process the
1718 status now */
1719 process_rcvd_status(edge_serial,
1720 edge_serial->rxHeader2, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 edge_serial->rxState = EXPECT_HDR1;
1722 break;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001723 } else {
1724 edge_serial->rxPort =
1725 IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1726 edge_serial->rxBytesRemaining =
1727 IOSP_GET_HDR_DATA_LEN(
1728 edge_serial->rxHeader1,
1729 edge_serial->rxHeader2);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001730 dev_dbg(dev, "%s - Data for Port %u Len %u\n",
1731 __func__,
1732 edge_serial->rxPort,
1733 edge_serial->rxBytesRemaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
Alan Cox03f0dbf2008-07-22 11:16:34 +01001735 /* ASSERT(DevExt->RxPort < DevExt->NumPorts);
1736 * ASSERT(DevExt->RxBytesRemaining <
1737 * IOSP_MAX_DATA_LENGTH);
1738 */
1739
1740 if (bufferLength == 0) {
1741 edge_serial->rxState = EXPECT_DATA;
1742 break;
1743 }
1744 /* Else, drop through */
1745 }
1746 case EXPECT_DATA: /* Expect data */
1747 if (bufferLength < edge_serial->rxBytesRemaining) {
1748 rxLen = bufferLength;
1749 /* Expect data to start next buffer */
1750 edge_serial->rxState = EXPECT_DATA;
1751 } else {
1752 /* BufLen >= RxBytesRemaining */
1753 rxLen = edge_serial->rxBytesRemaining;
1754 /* Start another header next time */
1755 edge_serial->rxState = EXPECT_HDR1;
1756 }
1757
1758 bufferLength -= rxLen;
1759 edge_serial->rxBytesRemaining -= rxLen;
1760
1761 /* spit this data back into the tty driver if this
1762 port is open */
1763 if (rxLen) {
1764 port = edge_serial->serial->port[
1765 edge_serial->rxPort];
1766 edge_port = usb_get_serial_port_data(port);
1767 if (edge_port->open) {
Jiri Slaby2e124b42013-01-03 15:53:06 +01001768 dev_dbg(dev, "%s - Sending %d bytes to TTY for port %d\n",
1769 __func__, rxLen,
1770 edge_serial->rxPort);
1771 edge_tty_recv(edge_port->port, buffer,
1772 rxLen);
Johan Hovoldd36a7712013-03-21 12:37:07 +01001773 edge_port->port->icount.rx += rxLen;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001774 }
1775 buffer += rxLen;
1776 }
1777 break;
1778
1779 case EXPECT_HDR3: /* Expect 3rd byte of status header */
1780 edge_serial->rxHeader3 = *buffer;
1781 ++buffer;
1782 --bufferLength;
1783
1784 /* We have all the header bytes, process the
1785 status now */
1786 process_rcvd_status(edge_serial,
1787 edge_serial->rxStatusParam,
1788 edge_serial->rxHeader3);
1789 edge_serial->rxState = EXPECT_HDR1;
1790 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 }
1792 }
1793}
1794
1795
1796/*****************************************************************************
1797 * process_rcvd_status
Alan Cox03f0dbf2008-07-22 11:16:34 +01001798 * this function handles the any status messages received on the
1799 * bulk in pipe.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001801static void process_rcvd_status(struct edgeport_serial *edge_serial,
1802 __u8 byte2, __u8 byte3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803{
1804 struct usb_serial_port *port;
1805 struct edgeport_port *edge_port;
Alan Cox4a90f092008-10-13 10:39:46 +01001806 struct tty_struct *tty;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001807 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 __u8 code = edge_serial->rxStatusCode;
1809
1810 /* switch the port pointer to the one being currently talked about */
1811 port = edge_serial->serial->port[edge_serial->rxPort];
1812 edge_port = usb_get_serial_port_data(port);
1813 if (edge_port == NULL) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001814 dev_err(&edge_serial->serial->dev->dev,
1815 "%s - edge_port == NULL for port %d\n",
1816 __func__, edge_serial->rxPort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 return;
1818 }
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001819 dev = &port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
1821 if (code == IOSP_EXT_STATUS) {
1822 switch (byte2) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001823 case IOSP_EXT_STATUS_CHASE_RSP:
1824 /* we want to do EXT status regardless of port
1825 * open/closed */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001826 dev_dbg(dev, "%s - Port %u EXT CHASE_RSP Data = %02x\n",
1827 __func__, edge_serial->rxPort, byte3);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001828 /* Currently, the only EXT_STATUS is Chase, so process
1829 * here instead of one more call to one more subroutine
1830 * If/when more EXT_STATUS, there'll be more work to do
1831 * Also, we currently clear flag and close the port
1832 * regardless of content of above's Byte3.
1833 * We could choose to do something else when Byte3 says
1834 * Timeout on Chase from Edgeport, like wait longer in
1835 * block_until_chase_response, but for now we don't.
1836 */
1837 edge_port->chaseResponsePending = false;
1838 wake_up(&edge_port->wait_chase);
1839 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Alan Cox03f0dbf2008-07-22 11:16:34 +01001841 case IOSP_EXT_STATUS_RX_CHECK_RSP:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001842 dev_dbg(dev, "%s ========== Port %u CHECK_RSP Sequence = %02x =============\n",
1843 __func__, edge_serial->rxPort, byte3);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001844 /* Port->RxCheckRsp = true; */
1845 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 }
1847 }
1848
1849 if (code == IOSP_STATUS_OPEN_RSP) {
1850 edge_port->txCredits = GET_TX_BUFFER_SIZE(byte3);
1851 edge_port->maxTxCredits = edge_port->txCredits;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001852 dev_dbg(dev, "%s - Port %u Open Response Initial MSR = %02x TxBufferSize = %d\n",
1853 __func__, edge_serial->rxPort, byte2, edge_port->txCredits);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001854 handle_new_msr(edge_port, byte2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Alan Cox03f0dbf2008-07-22 11:16:34 +01001856 /* send the current line settings to the port so we are
1857 in sync with any further termios calls */
Alan Cox4a90f092008-10-13 10:39:46 +01001858 tty = tty_port_tty_get(&edge_port->port->port);
1859 if (tty) {
1860 change_port_settings(tty,
Alan Coxadc8d742012-07-14 15:31:47 +01001861 edge_port, &tty->termios);
Alan Cox4a90f092008-10-13 10:39:46 +01001862 tty_kref_put(tty);
1863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
1865 /* we have completed the open */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001866 edge_port->openPending = false;
1867 edge_port->open = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 wake_up(&edge_port->wait_open);
1869 return;
1870 }
1871
Alan Cox03f0dbf2008-07-22 11:16:34 +01001872 /* If port is closed, silently discard all rcvd status. We can
1873 * have cases where buffered status is received AFTER the close
1874 * port command is sent to the Edgeport.
1875 */
1876 if (!edge_port->open || edge_port->closePending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
1879 switch (code) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001880 /* Not currently sent by Edgeport */
1881 case IOSP_STATUS_LSR:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001882 dev_dbg(dev, "%s - Port %u LSR Status = %02x\n",
1883 __func__, edge_serial->rxPort, byte2);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001884 handle_new_lsr(edge_port, false, byte2, 0);
1885 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
Alan Cox03f0dbf2008-07-22 11:16:34 +01001887 case IOSP_STATUS_LSR_DATA:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001888 dev_dbg(dev, "%s - Port %u LSR Status = %02x, Data = %02x\n",
1889 __func__, edge_serial->rxPort, byte2, byte3);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001890 /* byte2 is LSR Register */
1891 /* byte3 is broken data byte */
1892 handle_new_lsr(edge_port, true, byte2, byte3);
1893 break;
1894 /*
1895 * case IOSP_EXT_4_STATUS:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001896 * dev_dbg(dev, "%s - Port %u LSR Status = %02x Data = %02x\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01001897 * __func__, edge_serial->rxPort, byte2, byte3);
1898 * break;
1899 */
1900 case IOSP_STATUS_MSR:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001901 dev_dbg(dev, "%s - Port %u MSR Status = %02x\n",
1902 __func__, edge_serial->rxPort, byte2);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001903 /*
1904 * Process this new modem status and generate appropriate
1905 * events, etc, based on the new status. This routine
1906 * also saves the MSR in Port->ShadowMsr.
1907 */
1908 handle_new_msr(edge_port, byte2);
1909 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
Alan Cox03f0dbf2008-07-22 11:16:34 +01001911 default:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001912 dev_dbg(dev, "%s - Unrecognized IOSP status code %u\n", __func__, code);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001913 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915}
1916
1917
1918/*****************************************************************************
1919 * edge_tty_recv
1920 * this function passes data on to the tty flip buffer
1921 *****************************************************************************/
Jiri Slaby2e124b42013-01-03 15:53:06 +01001922static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
1923 int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
1925 int cnt;
1926
Jiri Slaby05c7cd32013-01-03 15:53:04 +01001927 cnt = tty_insert_flip_string(&port->port, data, length);
Alan Coxa108bfc2010-02-18 16:44:01 +00001928 if (cnt < length) {
Jiri Slaby05c7cd32013-01-03 15:53:04 +01001929 dev_err(&port->dev, "%s - dropping data, %d bytes lost\n",
Alan Coxa108bfc2010-02-18 16:44:01 +00001930 __func__, length - cnt);
1931 }
1932 data += cnt;
1933 length -= cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Jiri Slaby2e124b42013-01-03 15:53:06 +01001935 tty_flip_buffer_push(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936}
1937
1938
1939/*****************************************************************************
1940 * handle_new_msr
1941 * this function handles any change to the msr register for a port.
1942 *****************************************************************************/
1943static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr)
1944{
1945 struct async_icount *icount;
1946
Alan Cox03f0dbf2008-07-22 11:16:34 +01001947 if (newMsr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
1948 EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
Johan Hovoldd36a7712013-03-21 12:37:07 +01001949 icount = &edge_port->port->icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
1951 /* update input line counters */
Alan Cox03f0dbf2008-07-22 11:16:34 +01001952 if (newMsr & EDGEPORT_MSR_DELTA_CTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 icount->cts++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001954 if (newMsr & EDGEPORT_MSR_DELTA_DSR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 icount->dsr++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001956 if (newMsr & EDGEPORT_MSR_DELTA_CD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 icount->dcd++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001958 if (newMsr & EDGEPORT_MSR_DELTA_RI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 icount->rng++;
Johan Hovold8b8070d2013-03-21 12:37:08 +01001960 wake_up_interruptible(&edge_port->port->port.delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 }
1962
1963 /* Save the new modem status */
1964 edge_port->shadowMSR = newMsr & 0xf0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965}
1966
1967
1968/*****************************************************************************
1969 * handle_new_lsr
1970 * this function handles any change to the lsr register for a port.
1971 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001972static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
1973 __u8 lsr, __u8 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974{
Alan Cox03f0dbf2008-07-22 11:16:34 +01001975 __u8 newLsr = (__u8) (lsr & (__u8)
1976 (LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
1977 struct async_icount *icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 edge_port->shadowLSR = lsr;
1980
1981 if (newLsr & LSR_BREAK) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001982 /*
1983 * Parity and Framing errors only count if they
1984 * occur exclusive of a break being
1985 * received.
1986 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 newLsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
1988 }
1989
1990 /* Place LSR data byte into Rx buffer */
Jiri Slaby2e124b42013-01-03 15:53:06 +01001991 if (lsrData)
1992 edge_tty_recv(edge_port->port, &data, 1);
1993
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 /* update input line counters */
Johan Hovoldd36a7712013-03-21 12:37:07 +01001995 icount = &edge_port->port->icount;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001996 if (newLsr & LSR_BREAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 icount->brk++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001998 if (newLsr & LSR_OVER_ERR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 icount->overrun++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002000 if (newLsr & LSR_PAR_ERR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 icount->parity++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002002 if (newLsr & LSR_FRM_ERR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 icount->frame++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004}
2005
2006
2007/****************************************************************************
2008 * sram_write
Alan Cox03f0dbf2008-07-22 11:16:34 +01002009 * writes a number of bytes to the Edgeport device's sram starting at the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 * given address.
2011 * If successful returns the number of bytes written, otherwise it returns
2012 * a negative error number of the problem.
2013 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002014static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
2015 __u16 length, const __u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016{
2017 int result;
2018 __u16 current_length;
2019 unsigned char *transfer_buffer;
2020
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002021 dev_dbg(&serial->dev->dev, "%s - %x, %x, %d\n", __func__, extAddr, addr, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
Alan Cox03f0dbf2008-07-22 11:16:34 +01002023 transfer_buffer = kmalloc(64, GFP_KERNEL);
Johan Hovold10c642d2013-12-29 19:22:56 +01002024 if (!transfer_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
2027 /* need to split these writes up into 64 byte chunks */
2028 result = 0;
2029 while (length > 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002030 if (length > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 current_length = 64;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002032 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 current_length = length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002034
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002035/* dev_dbg(&serial->dev->dev, "%s - writing %x, %x, %d\n", __func__, extAddr, addr, current_length); */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002036 memcpy(transfer_buffer, data, current_length);
2037 result = usb_control_msg(serial->dev,
2038 usb_sndctrlpipe(serial->dev, 0),
2039 USB_REQUEST_ION_WRITE_RAM,
2040 0x40, addr, extAddr, transfer_buffer,
2041 current_length, 300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 if (result < 0)
2043 break;
2044 length -= current_length;
2045 addr += current_length;
2046 data += current_length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
Alan Cox03f0dbf2008-07-22 11:16:34 +01002049 kfree(transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 return result;
2051}
2052
2053
2054/****************************************************************************
2055 * rom_write
2056 * writes a number of bytes to the Edgeport device's ROM starting at the
2057 * given address.
2058 * If successful returns the number of bytes written, otherwise it returns
2059 * a negative error number of the problem.
2060 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002061static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
2062 __u16 length, const __u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063{
2064 int result;
2065 __u16 current_length;
2066 unsigned char *transfer_buffer;
2067
Alan Cox03f0dbf2008-07-22 11:16:34 +01002068 transfer_buffer = kmalloc(64, GFP_KERNEL);
Johan Hovold10c642d2013-12-29 19:22:56 +01002069 if (!transfer_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
2072 /* need to split these writes up into 64 byte chunks */
2073 result = 0;
2074 while (length > 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002075 if (length > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 current_length = 64;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002077 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 current_length = length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002079 memcpy(transfer_buffer, data, current_length);
2080 result = usb_control_msg(serial->dev,
2081 usb_sndctrlpipe(serial->dev, 0),
2082 USB_REQUEST_ION_WRITE_ROM, 0x40,
2083 addr, extAddr,
2084 transfer_buffer, current_length, 300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 if (result < 0)
2086 break;
2087 length -= current_length;
2088 addr += current_length;
2089 data += current_length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
Alan Cox03f0dbf2008-07-22 11:16:34 +01002092 kfree(transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 return result;
2094}
2095
2096
2097/****************************************************************************
2098 * rom_read
2099 * reads a number of bytes from the Edgeport device starting at the given
2100 * address.
2101 * If successful returns the number of bytes read, otherwise it returns
2102 * a negative error number of the problem.
2103 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002104static int rom_read(struct usb_serial *serial, __u16 extAddr,
2105 __u16 addr, __u16 length, __u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106{
2107 int result;
2108 __u16 current_length;
2109 unsigned char *transfer_buffer;
2110
Alan Cox03f0dbf2008-07-22 11:16:34 +01002111 transfer_buffer = kmalloc(64, GFP_KERNEL);
Johan Hovold10c642d2013-12-29 19:22:56 +01002112 if (!transfer_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
2115 /* need to split these reads up into 64 byte chunks */
2116 result = 0;
2117 while (length > 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002118 if (length > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 current_length = 64;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002120 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 current_length = length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002122 result = usb_control_msg(serial->dev,
2123 usb_rcvctrlpipe(serial->dev, 0),
2124 USB_REQUEST_ION_READ_ROM,
2125 0xC0, addr, extAddr, transfer_buffer,
2126 current_length, 300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 if (result < 0)
2128 break;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002129 memcpy(data, transfer_buffer, current_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 length -= current_length;
2131 addr += current_length;
2132 data += current_length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
Alan Cox03f0dbf2008-07-22 11:16:34 +01002135 kfree(transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 return result;
2137}
2138
2139
2140/****************************************************************************
2141 * send_iosp_ext_cmd
2142 * Is used to send a IOSP message to the Edgeport device
2143 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002144static int send_iosp_ext_cmd(struct edgeport_port *edge_port,
2145 __u8 command, __u8 param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146{
2147 unsigned char *buffer;
2148 unsigned char *currentCommand;
2149 int length = 0;
2150 int status = 0;
2151
Alan Cox03f0dbf2008-07-22 11:16:34 +01002152 buffer = kmalloc(10, GFP_ATOMIC);
Johan Hovold10c642d2013-12-29 19:22:56 +01002153 if (!buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
2156 currentCommand = buffer;
2157
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07002158 MAKE_CMD_EXT_CMD(&currentCommand, &length, edge_port->port->port_number,
2159 command, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
Alan Cox03f0dbf2008-07-22 11:16:34 +01002161 status = write_cmd_usb(edge_port, buffer, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 if (status) {
2163 /* something bad happened, let's free up the memory */
2164 kfree(buffer);
2165 }
2166
2167 return status;
2168}
2169
2170
2171/*****************************************************************************
2172 * write_cmd_usb
2173 * this function writes the given buffer out to the bulk write endpoint.
2174 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002175static int write_cmd_usb(struct edgeport_port *edge_port,
2176 unsigned char *buffer, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002178 struct edgeport_serial *edge_serial =
2179 usb_get_serial_data(edge_port->port->serial);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002180 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 int status = 0;
2182 struct urb *urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01002184 usb_serial_debug_data(dev, __func__, length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
2186 /* Allocate our next urb */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002187 urb = usb_alloc_urb(0, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 if (!urb)
2189 return -ENOMEM;
2190
Oliver Neukum96c706e2007-03-15 15:27:17 +01002191 atomic_inc(&CmdUrbs);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002192 dev_dbg(dev, "%s - ALLOCATE URB %p (outstanding %d)\n",
2193 __func__, urb, atomic_read(&CmdUrbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
Alan Cox03f0dbf2008-07-22 11:16:34 +01002195 usb_fill_bulk_urb(urb, edge_serial->serial->dev,
2196 usb_sndbulkpipe(edge_serial->serial->dev,
2197 edge_serial->bulk_out_endpoint),
2198 buffer, length, edge_bulk_out_cmd_callback, edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002200 edge_port->commandPending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 status = usb_submit_urb(urb, GFP_ATOMIC);
2202
2203 if (status) {
2204 /* something went wrong */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002205 dev_err(dev, "%s - usb_submit_urb(write command) failed, status = %d\n",
2206 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 usb_kill_urb(urb);
2208 usb_free_urb(urb);
Oliver Neukum96c706e2007-03-15 15:27:17 +01002209 atomic_dec(&CmdUrbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 return status;
2211 }
2212
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213#if 0
Alan Cox03f0dbf2008-07-22 11:16:34 +01002214 wait_event(&edge_port->wait_command, !edge_port->commandPending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002216 if (edge_port->commandPending) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 /* command timed out */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002218 dev_dbg(dev, "%s - command timed out\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 status = -EINVAL;
2220 }
2221#endif
2222 return status;
2223}
2224
2225
2226/*****************************************************************************
2227 * send_cmd_write_baud_rate
2228 * this function sends the proper command to change the baud rate of the
2229 * specified port.
2230 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002231static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
2232 int baudRate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002234 struct edgeport_serial *edge_serial =
2235 usb_get_serial_data(edge_port->port->serial);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002236 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 unsigned char *cmdBuffer;
2238 unsigned char *currCmd;
2239 int cmdLen = 0;
2240 int divisor;
2241 int status;
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07002242 u32 number = edge_port->port->port_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
Adam Kropelinbc71e472007-07-29 11:03:29 -04002244 if (edge_serial->is_epic &&
2245 !edge_serial->epic_descriptor.Supports.IOSPSetBaudRate) {
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07002246 dev_dbg(dev, "SendCmdWriteBaudRate - NOT Setting baud rate for port, baud = %d\n",
2247 baudRate);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002248 return 0;
2249 }
2250
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07002251 dev_dbg(dev, "%s - baud = %d\n", __func__, baudRate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002253 status = calc_baud_rate_divisor(dev, baudRate, &divisor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 if (status) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002255 dev_err(dev, "%s - bad baud rate\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 return status;
2257 }
2258
Alan Cox03f0dbf2008-07-22 11:16:34 +01002259 /* Alloc memory for the string of commands. */
2260 cmdBuffer = kmalloc(0x100, GFP_ATOMIC);
Johan Hovold10c642d2013-12-29 19:22:56 +01002261 if (!cmdBuffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 return -ENOMEM;
Johan Hovold10c642d2013-12-29 19:22:56 +01002263
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 currCmd = cmdBuffer;
2265
Alan Cox03f0dbf2008-07-22 11:16:34 +01002266 /* Enable access to divisor latch */
2267 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR, LCR_DL_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
Alan Cox03f0dbf2008-07-22 11:16:34 +01002269 /* Write the divisor itself */
2270 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLL, LOW8(divisor));
2271 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLM, HIGH8(divisor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
Alan Cox03f0dbf2008-07-22 11:16:34 +01002273 /* Restore original value to disable access to divisor latch */
2274 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR,
2275 edge_port->shadowLCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
Alan Cox03f0dbf2008-07-22 11:16:34 +01002277 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 if (status) {
2279 /* something bad happened, let's free up the memory */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002280 kfree(cmdBuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 }
2282
2283 return status;
2284}
2285
2286
2287/*****************************************************************************
2288 * calc_baud_rate_divisor
2289 * this function calculates the proper baud rate divisor for the specified
2290 * baud rate.
2291 *****************************************************************************/
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002292static int calc_baud_rate_divisor(struct device *dev, int baudrate, int *divisor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293{
2294 int i;
2295 __u16 custom;
2296
Tobias Klauser52950ed2005-12-11 16:20:08 +01002297 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002298 if (divisor_table[i].BaudRate == baudrate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 *divisor = divisor_table[i].Divisor;
2300 return 0;
2301 }
2302 }
2303
Alan Cox03f0dbf2008-07-22 11:16:34 +01002304 /* We have tried all of the standard baud rates
2305 * lets try to calculate the divisor for this baud rate
2306 * Make sure the baud rate is reasonable */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 if (baudrate > 50 && baudrate < 230400) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002308 /* get divisor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 custom = (__u16)((230400L + baudrate/2) / baudrate);
2310
2311 *divisor = custom;
2312
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002313 dev_dbg(dev, "%s - Baud %d = %d\n", __func__, baudrate, custom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 return 0;
2315 }
2316
2317 return -1;
2318}
2319
2320
2321/*****************************************************************************
2322 * send_cmd_write_uart_register
Anand Gadiyarfd589a82009-07-16 17:13:03 +02002323 * this function builds up a uart register message and sends to the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002325static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
2326 __u8 regNum, __u8 regValue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002328 struct edgeport_serial *edge_serial =
2329 usb_get_serial_data(edge_port->port->serial);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002330 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 unsigned char *cmdBuffer;
2332 unsigned char *currCmd;
2333 unsigned long cmdLen = 0;
2334 int status;
2335
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002336 dev_dbg(dev, "%s - write to %s register 0x%02x\n",
2337 (regNum == MCR) ? "MCR" : "LCR", __func__, regValue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
Adam Kropelinbc71e472007-07-29 11:03:29 -04002339 if (edge_serial->is_epic &&
2340 !edge_serial->epic_descriptor.Supports.IOSPWriteMCR &&
2341 regNum == MCR) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002342 dev_dbg(dev, "SendCmdWriteUartReg - Not writing to MCR Register\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002343 return 0;
2344 }
2345
Adam Kropelinbc71e472007-07-29 11:03:29 -04002346 if (edge_serial->is_epic &&
2347 !edge_serial->epic_descriptor.Supports.IOSPWriteLCR &&
2348 regNum == LCR) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002349 dev_dbg(dev, "SendCmdWriteUartReg - Not writing to LCR Register\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002350 return 0;
2351 }
2352
Alan Cox03f0dbf2008-07-22 11:16:34 +01002353 /* Alloc memory for the string of commands. */
2354 cmdBuffer = kmalloc(0x10, GFP_ATOMIC);
2355 if (cmdBuffer == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
2358 currCmd = cmdBuffer;
2359
Alan Cox03f0dbf2008-07-22 11:16:34 +01002360 /* Build a cmd in the buffer to write the given register */
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07002361 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, edge_port->port->port_number,
2362 regNum, regValue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
2364 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
2365 if (status) {
2366 /* something bad happened, let's free up the memory */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002367 kfree(cmdBuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 }
2369
2370 return status;
2371}
2372
2373
2374/*****************************************************************************
2375 * change_port_settings
Alan Cox03f0dbf2008-07-22 11:16:34 +01002376 * This routine is called to set the UART on the device to match the
2377 * specified new settings.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01002379
2380static void change_port_settings(struct tty_struct *tty,
2381 struct edgeport_port *edge_port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002383 struct device *dev = &edge_port->port->dev;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002384 struct edgeport_serial *edge_serial =
2385 usb_get_serial_data(edge_port->port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 int baud;
2387 unsigned cflag;
2388 __u8 mask = 0xff;
2389 __u8 lData;
2390 __u8 lParity;
2391 __u8 lStop;
2392 __u8 rxFlow;
2393 __u8 txFlow;
2394 int status;
2395
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002396 if (!edge_port->open &&
2397 !edge_port->openPending) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002398 dev_dbg(dev, "%s - port not opened\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 return;
2400 }
2401
Alan Coxadc8d742012-07-14 15:31:47 +01002402 cflag = tty->termios.c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
2404 switch (cflag & CSIZE) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002405 case CS5:
2406 lData = LCR_BITS_5; mask = 0x1f;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002407 dev_dbg(dev, "%s - data bits = 5\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002408 break;
2409 case CS6:
2410 lData = LCR_BITS_6; mask = 0x3f;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002411 dev_dbg(dev, "%s - data bits = 6\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002412 break;
2413 case CS7:
2414 lData = LCR_BITS_7; mask = 0x7f;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002415 dev_dbg(dev, "%s - data bits = 7\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002416 break;
2417 default:
2418 case CS8:
2419 lData = LCR_BITS_8;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002420 dev_dbg(dev, "%s - data bits = 8\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002421 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 }
2423
2424 lParity = LCR_PAR_NONE;
2425 if (cflag & PARENB) {
2426 if (cflag & CMSPAR) {
2427 if (cflag & PARODD) {
2428 lParity = LCR_PAR_MARK;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002429 dev_dbg(dev, "%s - parity = mark\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 } else {
2431 lParity = LCR_PAR_SPACE;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002432 dev_dbg(dev, "%s - parity = space\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 }
2434 } else if (cflag & PARODD) {
2435 lParity = LCR_PAR_ODD;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002436 dev_dbg(dev, "%s - parity = odd\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 } else {
2438 lParity = LCR_PAR_EVEN;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002439 dev_dbg(dev, "%s - parity = even\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 }
2441 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002442 dev_dbg(dev, "%s - parity = none\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 }
2444
2445 if (cflag & CSTOPB) {
2446 lStop = LCR_STOP_2;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002447 dev_dbg(dev, "%s - stop bits = 2\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 } else {
2449 lStop = LCR_STOP_1;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002450 dev_dbg(dev, "%s - stop bits = 1\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 }
2452
2453 /* figure out the flow control settings */
2454 rxFlow = txFlow = 0x00;
2455 if (cflag & CRTSCTS) {
2456 rxFlow |= IOSP_RX_FLOW_RTS;
2457 txFlow |= IOSP_TX_FLOW_CTS;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002458 dev_dbg(dev, "%s - RTS/CTS is enabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002460 dev_dbg(dev, "%s - RTS/CTS is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 }
2462
Alan Cox03f0dbf2008-07-22 11:16:34 +01002463 /* if we are implementing XON/XOFF, set the start and stop character
2464 in the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 if (I_IXOFF(tty) || I_IXON(tty)) {
2466 unsigned char stop_char = STOP_CHAR(tty);
2467 unsigned char start_char = START_CHAR(tty);
2468
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002469 if ((!edge_serial->is_epic) ||
2470 ((edge_serial->is_epic) &&
2471 (edge_serial->epic_descriptor.Supports.IOSPSetXChar))) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002472 send_iosp_ext_cmd(edge_port,
2473 IOSP_CMD_SET_XON_CHAR, start_char);
2474 send_iosp_ext_cmd(edge_port,
2475 IOSP_CMD_SET_XOFF_CHAR, stop_char);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
2478 /* if we are implementing INBOUND XON/XOFF */
2479 if (I_IXOFF(tty)) {
2480 rxFlow |= IOSP_RX_FLOW_XON_XOFF;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002481 dev_dbg(dev, "%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2482 __func__, start_char, stop_char);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002484 dev_dbg(dev, "%s - INBOUND XON/XOFF is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 }
2486
2487 /* if we are implementing OUTBOUND XON/XOFF */
2488 if (I_IXON(tty)) {
2489 txFlow |= IOSP_TX_FLOW_XON_XOFF;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002490 dev_dbg(dev, "%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2491 __func__, start_char, stop_char);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002493 dev_dbg(dev, "%s - OUTBOUND XON/XOFF is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 }
2495 }
2496
2497 /* Set flow control to the configured value */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002498 if ((!edge_serial->is_epic) ||
2499 ((edge_serial->is_epic) &&
2500 (edge_serial->epic_descriptor.Supports.IOSPSetRxFlow)))
2501 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_RX_FLOW, rxFlow);
2502 if ((!edge_serial->is_epic) ||
2503 ((edge_serial->is_epic) &&
2504 (edge_serial->epic_descriptor.Supports.IOSPSetTxFlow)))
2505 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_TX_FLOW, txFlow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506
2507
2508 edge_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2509 edge_port->shadowLCR |= (lData | lParity | lStop);
2510
2511 edge_port->validDataMask = mask;
2512
2513 /* Send the updated LCR value to the EdgePort */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002514 status = send_cmd_write_uart_register(edge_port, LCR,
2515 edge_port->shadowLCR);
2516 if (status != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
2519 /* set up the MCR register and send it to the EdgePort */
2520 edge_port->shadowMCR = MCR_MASTER_IE;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002521 if (cflag & CBAUD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 edge_port->shadowMCR |= (MCR_DTR | MCR_RTS);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002523
2524 status = send_cmd_write_uart_register(edge_port, MCR,
2525 edge_port->shadowMCR);
2526 if (status != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
2529 /* Determine divisor based on baud rate */
2530 baud = tty_get_baud_rate(tty);
2531 if (!baud) {
2532 /* pick a default, any default... */
2533 baud = 9600;
2534 }
2535
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002536 dev_dbg(dev, "%s - baud rate = %d\n", __func__, baud);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002537 status = send_cmd_write_baud_rate(edge_port, baud);
Alan Cox6ce073b2007-10-18 01:24:25 -07002538 if (status == -1) {
2539 /* Speed change was not possible - put back the old speed */
2540 baud = tty_termios_baud_rate(old_termios);
2541 tty_encode_baud_rate(tty, baud, baud);
2542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543}
2544
2545
2546/****************************************************************************
2547 * unicode_to_ascii
2548 * Turns a string from Unicode into ASCII.
2549 * Doesn't do a good job with any characters that are outside the normal
2550 * ASCII range, but it's only for debugging...
2551 * NOTE: expects the unicode in LE format
2552 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002553static void unicode_to_ascii(char *string, int buflen,
2554 __le16 *unicode, int unicode_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555{
2556 int i;
2557
Pete Zaitcevad933752006-05-22 21:49:44 -07002558 if (buflen <= 0) /* never happens, but... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 return;
Pete Zaitcevad933752006-05-22 21:49:44 -07002560 --buflen; /* space for nul */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561
Pete Zaitcevad933752006-05-22 21:49:44 -07002562 for (i = 0; i < unicode_size; i++) {
2563 if (i >= buflen)
2564 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 string[i] = (char)(le16_to_cpu(unicode[i]));
Pete Zaitcevad933752006-05-22 21:49:44 -07002566 }
2567 string[i] = 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568}
2569
2570
2571/****************************************************************************
2572 * get_manufacturing_desc
Alan Cox03f0dbf2008-07-22 11:16:34 +01002573 * reads in the manufacturing descriptor and stores it into the serial
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 * structure.
2575 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002576static void get_manufacturing_desc(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002578 struct device *dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 int response;
2580
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002581 dev_dbg(dev, "getting manufacturer descriptor\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
Alan Cox03f0dbf2008-07-22 11:16:34 +01002583 response = rom_read(edge_serial->serial,
2584 (EDGE_MANUF_DESC_ADDR & 0xffff0000) >> 16,
2585 (__u16)(EDGE_MANUF_DESC_ADDR & 0x0000ffff),
2586 EDGE_MANUF_DESC_LEN,
2587 (__u8 *)(&edge_serial->manuf_descriptor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588
Alan Cox03f0dbf2008-07-22 11:16:34 +01002589 if (response < 1)
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002590 dev_err(dev, "error in getting manufacturer descriptor\n");
Alan Cox03f0dbf2008-07-22 11:16:34 +01002591 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 char string[30];
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002593 dev_dbg(dev, "**Manufacturer Descriptor\n");
2594 dev_dbg(dev, " RomSize: %dK\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002595 edge_serial->manuf_descriptor.RomSize);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002596 dev_dbg(dev, " RamSize: %dK\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002597 edge_serial->manuf_descriptor.RamSize);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002598 dev_dbg(dev, " CpuRev: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002599 edge_serial->manuf_descriptor.CpuRev);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002600 dev_dbg(dev, " BoardRev: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002601 edge_serial->manuf_descriptor.BoardRev);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002602 dev_dbg(dev, " NumPorts: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002603 edge_serial->manuf_descriptor.NumPorts);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002604 dev_dbg(dev, " DescDate: %d/%d/%d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002605 edge_serial->manuf_descriptor.DescDate[0],
2606 edge_serial->manuf_descriptor.DescDate[1],
2607 edge_serial->manuf_descriptor.DescDate[2]+1900);
Pete Zaitcev4bc203d2006-06-06 18:18:33 -07002608 unicode_to_ascii(string, sizeof(string),
Alan Cox03f0dbf2008-07-22 11:16:34 +01002609 edge_serial->manuf_descriptor.SerialNumber,
2610 edge_serial->manuf_descriptor.SerNumLength/2);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002611 dev_dbg(dev, " SerialNumber: %s\n", string);
Pete Zaitcev4bc203d2006-06-06 18:18:33 -07002612 unicode_to_ascii(string, sizeof(string),
Alan Cox03f0dbf2008-07-22 11:16:34 +01002613 edge_serial->manuf_descriptor.AssemblyNumber,
2614 edge_serial->manuf_descriptor.AssemblyNumLength/2);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002615 dev_dbg(dev, " AssemblyNumber: %s\n", string);
Pete Zaitcev4bc203d2006-06-06 18:18:33 -07002616 unicode_to_ascii(string, sizeof(string),
Pete Zaitcevad933752006-05-22 21:49:44 -07002617 edge_serial->manuf_descriptor.OemAssyNumber,
2618 edge_serial->manuf_descriptor.OemAssyNumLength/2);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002619 dev_dbg(dev, " OemAssyNumber: %s\n", string);
2620 dev_dbg(dev, " UartType: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002621 edge_serial->manuf_descriptor.UartType);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002622 dev_dbg(dev, " IonPid: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002623 edge_serial->manuf_descriptor.IonPid);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002624 dev_dbg(dev, " IonConfig: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002625 edge_serial->manuf_descriptor.IonConfig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 }
2627}
2628
2629
2630/****************************************************************************
2631 * get_boot_desc
Alan Cox03f0dbf2008-07-22 11:16:34 +01002632 * reads in the bootloader descriptor and stores it into the serial
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 * structure.
2634 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002635static void get_boot_desc(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002637 struct device *dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 int response;
2639
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002640 dev_dbg(dev, "getting boot descriptor\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
Alan Cox03f0dbf2008-07-22 11:16:34 +01002642 response = rom_read(edge_serial->serial,
2643 (EDGE_BOOT_DESC_ADDR & 0xffff0000) >> 16,
2644 (__u16)(EDGE_BOOT_DESC_ADDR & 0x0000ffff),
2645 EDGE_BOOT_DESC_LEN,
2646 (__u8 *)(&edge_serial->boot_descriptor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647
Alan Cox03f0dbf2008-07-22 11:16:34 +01002648 if (response < 1)
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002649 dev_err(dev, "error in getting boot descriptor\n");
Alan Cox03f0dbf2008-07-22 11:16:34 +01002650 else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002651 dev_dbg(dev, "**Boot Descriptor:\n");
2652 dev_dbg(dev, " BootCodeLength: %d\n",
2653 le16_to_cpu(edge_serial->boot_descriptor.BootCodeLength));
2654 dev_dbg(dev, " MajorVersion: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002655 edge_serial->boot_descriptor.MajorVersion);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002656 dev_dbg(dev, " MinorVersion: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002657 edge_serial->boot_descriptor.MinorVersion);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002658 dev_dbg(dev, " BuildNumber: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002659 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002660 dev_dbg(dev, " Capabilities: 0x%x\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002661 le16_to_cpu(edge_serial->boot_descriptor.Capabilities));
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002662 dev_dbg(dev, " UConfig0: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002663 edge_serial->boot_descriptor.UConfig0);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002664 dev_dbg(dev, " UConfig1: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002665 edge_serial->boot_descriptor.UConfig1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 }
2667}
2668
2669
2670/****************************************************************************
2671 * load_application_firmware
2672 * This is called to load the application firmware to the device
2673 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002674static void load_application_firmware(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002676 struct device *dev = &edge_serial->serial->dev->dev;
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302677 const struct ihex_binrec *rec;
2678 const struct firmware *fw;
2679 const char *fw_name;
2680 const char *fw_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 int response;
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302682 __u32 Operaddr;
2683 __u16 build;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
2685 switch (edge_serial->product_info.iDownloadFile) {
2686 case EDGE_DOWNLOAD_FILE_I930:
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302687 fw_info = "downloading firmware version (930)";
2688 fw_name = "edgeport/down.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 break;
2690
2691 case EDGE_DOWNLOAD_FILE_80251:
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302692 fw_info = "downloading firmware version (80251)";
2693 fw_name = "edgeport/down2.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 break;
2695
2696 case EDGE_DOWNLOAD_FILE_NONE:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002697 dev_dbg(dev, "No download file specified, skipping download\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 return;
2699
2700 default:
2701 return;
2702 }
2703
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302704 response = request_ihex_firmware(&fw, fw_name,
2705 &edge_serial->serial->dev->dev);
2706 if (response) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002707 dev_err(dev, "Failed to load image \"%s\" err %d\n",
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302708 fw_name, response);
2709 return;
2710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302712 rec = (const struct ihex_binrec *)fw->data;
2713 build = (rec->data[2] << 8) | rec->data[3];
2714
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002715 dev_dbg(dev, "%s %d.%d.%d\n", fw_info, rec->data[0], rec->data[1], build);
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302716
Bjørn Mork271c1152011-01-17 14:19:37 +01002717 edge_serial->product_info.FirmwareMajorVersion = rec->data[0];
2718 edge_serial->product_info.FirmwareMinorVersion = rec->data[1];
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302719 edge_serial->product_info.FirmwareBuildNumber = cpu_to_le16(build);
2720
2721 for (rec = ihex_next_binrec(rec); rec;
2722 rec = ihex_next_binrec(rec)) {
2723 Operaddr = be32_to_cpu(rec->addr);
2724 response = sram_write(edge_serial->serial,
2725 Operaddr >> 16,
2726 Operaddr & 0xFFFF,
2727 be16_to_cpu(rec->len),
2728 &rec->data[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 if (response < 0) {
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302730 dev_err(&edge_serial->serial->dev->dev,
2731 "sram_write failed (%x, %x, %d)\n",
2732 Operaddr >> 16, Operaddr & 0xFFFF,
2733 be16_to_cpu(rec->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 break;
2735 }
2736 }
2737
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002738 dev_dbg(dev, "sending exec_dl_code\n");
2739 response = usb_control_msg (edge_serial->serial->dev,
2740 usb_sndctrlpipe(edge_serial->serial->dev, 0),
2741 USB_REQUEST_ION_EXEC_DL_CODE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 0x40, 0x4000, 0x0001, NULL, 0, 3000);
2743
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302744 release_firmware(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745}
2746
2747
2748/****************************************************************************
2749 * edge_startup
2750 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002751static int edge_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752{
2753 struct edgeport_serial *edge_serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 struct usb_device *dev;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002755 struct device *ddev = &serial->dev->dev;
Johan Hovoldc27f3ef2012-10-17 13:34:57 +02002756 int i;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002757 int response;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002758 bool interrupt_in_found;
2759 bool bulk_in_found;
2760 bool bulk_out_found;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002761 static __u32 descriptor[3] = { EDGE_COMPATIBILITY_MASK0,
2762 EDGE_COMPATIBILITY_MASK1,
2763 EDGE_COMPATIBILITY_MASK2 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764
2765 dev = serial->dev;
2766
2767 /* create our private serial structure */
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +01002768 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
Johan Hovold10c642d2013-12-29 19:22:56 +01002769 if (!edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 return -ENOMEM;
Johan Hovold10c642d2013-12-29 19:22:56 +01002771
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 spin_lock_init(&edge_serial->es_lock);
2773 edge_serial->serial = serial;
2774 usb_set_serial_data(serial, edge_serial);
2775
2776 /* get the name for the device from the device */
Dan Carpenterf10718f2010-01-25 14:53:33 +03002777 i = usb_string(dev, dev->descriptor.iManufacturer,
Pete Zaitcevad933752006-05-22 21:49:44 -07002778 &edge_serial->name[0], MAX_NAME_LEN+1);
Dan Carpenterf10718f2010-01-25 14:53:33 +03002779 if (i < 0)
2780 i = 0;
Pete Zaitcevad933752006-05-22 21:49:44 -07002781 edge_serial->name[i++] = ' ';
Dan Carpenterf10718f2010-01-25 14:53:33 +03002782 usb_string(dev, dev->descriptor.iProduct,
Pete Zaitcevad933752006-05-22 21:49:44 -07002783 &edge_serial->name[i], MAX_NAME_LEN+2 - i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
2785 dev_info(&serial->dev->dev, "%s detected\n", edge_serial->name);
2786
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002787 /* Read the epic descriptor */
2788 if (get_epic_descriptor(edge_serial) <= 0) {
2789 /* memcpy descriptor to Supports structures */
2790 memcpy(&edge_serial->epic_descriptor.Supports, descriptor,
2791 sizeof(struct edge_compatibility_bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002793 /* get the manufacturing descriptor for this device */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002794 get_manufacturing_desc(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002796 /* get the boot descriptor */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002797 get_boot_desc(edge_serial);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002798
2799 get_product_info(edge_serial);
2800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801
2802 /* set the number of ports from the manufacturing description */
2803 /* serial->num_ports = serial->product_info.NumPorts; */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002804 if ((!edge_serial->is_epic) &&
2805 (edge_serial->product_info.NumPorts != serial->num_ports)) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002806 dev_warn(ddev,
2807 "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 -08002808 edge_serial->product_info.NumPorts,
2809 serial->num_ports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 }
2811
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002812 dev_dbg(ddev, "%s - time 1 %ld\n", __func__, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002814 /* If not an EPiC device */
2815 if (!edge_serial->is_epic) {
2816 /* now load the application firmware into this device */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002817 load_application_firmware(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002819 dev_dbg(ddev, "%s - time 2 %ld\n", __func__, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002821 /* Check current Edgeport EEPROM and update if necessary */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002822 update_edgeport_E2PROM(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002824 dev_dbg(ddev, "%s - time 3 %ld\n", __func__, jiffies);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002825
2826 /* set the configuration to use #1 */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002827/* dev_dbg(ddev, "set_configuration 1\n"); */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002828/* usb_set_configuration (dev, 1); */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002829 }
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002830 dev_dbg(ddev, " FirmwareMajorVersion %d.%d.%d\n",
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302831 edge_serial->product_info.FirmwareMajorVersion,
2832 edge_serial->product_info.FirmwareMinorVersion,
2833 le16_to_cpu(edge_serial->product_info.FirmwareBuildNumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834
Alan Cox03f0dbf2008-07-22 11:16:34 +01002835 /* we set up the pointers to the endpoints in the edge_open function,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 * as the structures aren't created yet. */
2837
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002838 response = 0;
2839
2840 if (edge_serial->is_epic) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002841 /* EPIC thing, set up our interrupt polling now and our read
2842 * urb, so that the device knows it really is connected. */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002843 interrupt_in_found = bulk_in_found = bulk_out_found = false;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002844 for (i = 0; i < serial->interface->altsetting[0]
2845 .desc.bNumEndpoints; ++i) {
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002846 struct usb_endpoint_descriptor *endpoint;
2847 int buffer_size;
2848
Alan Cox03f0dbf2008-07-22 11:16:34 +01002849 endpoint = &serial->interface->altsetting[0].
2850 endpoint[i].desc;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07002851 buffer_size = usb_endpoint_maxp(endpoint);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002852 if (!interrupt_in_found &&
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002853 (usb_endpoint_is_int_in(endpoint))) {
2854 /* we found a interrupt in endpoint */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002855 dev_dbg(ddev, "found interrupt in\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002856
2857 /* not set up yet, so do it now */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002858 edge_serial->interrupt_read_urb =
2859 usb_alloc_urb(0, GFP_KERNEL);
Johan Hovold10c642d2013-12-29 19:22:56 +01002860 if (!edge_serial->interrupt_read_urb)
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002861 return -ENOMEM;
Johan Hovold10c642d2013-12-29 19:22:56 +01002862
Alan Cox03f0dbf2008-07-22 11:16:34 +01002863 edge_serial->interrupt_in_buffer =
2864 kmalloc(buffer_size, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002865 if (!edge_serial->interrupt_in_buffer) {
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002866 usb_free_urb(edge_serial->interrupt_read_urb);
2867 return -ENOMEM;
2868 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01002869 edge_serial->interrupt_in_endpoint =
2870 endpoint->bEndpointAddress;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002871
2872 /* set up our interrupt urb */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002873 usb_fill_int_urb(
2874 edge_serial->interrupt_read_urb,
2875 dev,
2876 usb_rcvintpipe(dev,
2877 endpoint->bEndpointAddress),
2878 edge_serial->interrupt_in_buffer,
2879 buffer_size,
2880 edge_interrupt_callback,
2881 edge_serial,
2882 endpoint->bInterval);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002883
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002884 interrupt_in_found = true;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002885 }
2886
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002887 if (!bulk_in_found &&
Alan Cox03f0dbf2008-07-22 11:16:34 +01002888 (usb_endpoint_is_bulk_in(endpoint))) {
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002889 /* we found a bulk in endpoint */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002890 dev_dbg(ddev, "found bulk in\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002891
2892 /* not set up yet, so do it now */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002893 edge_serial->read_urb =
2894 usb_alloc_urb(0, GFP_KERNEL);
Johan Hovold10c642d2013-12-29 19:22:56 +01002895 if (!edge_serial->read_urb)
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002896 return -ENOMEM;
Johan Hovold10c642d2013-12-29 19:22:56 +01002897
Alan Cox03f0dbf2008-07-22 11:16:34 +01002898 edge_serial->bulk_in_buffer =
2899 kmalloc(buffer_size, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002900 if (!edge_serial->bulk_in_buffer) {
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002901 usb_free_urb(edge_serial->read_urb);
2902 return -ENOMEM;
2903 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01002904 edge_serial->bulk_in_endpoint =
2905 endpoint->bEndpointAddress;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002906
2907 /* set up our bulk in urb */
2908 usb_fill_bulk_urb(edge_serial->read_urb, dev,
Alan Cox03f0dbf2008-07-22 11:16:34 +01002909 usb_rcvbulkpipe(dev,
2910 endpoint->bEndpointAddress),
2911 edge_serial->bulk_in_buffer,
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07002912 usb_endpoint_maxp(endpoint),
Alan Cox03f0dbf2008-07-22 11:16:34 +01002913 edge_bulk_in_callback,
2914 edge_serial);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002915 bulk_in_found = true;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002916 }
2917
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002918 if (!bulk_out_found &&
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002919 (usb_endpoint_is_bulk_out(endpoint))) {
2920 /* we found a bulk out endpoint */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002921 dev_dbg(ddev, "found bulk out\n");
Alan Cox03f0dbf2008-07-22 11:16:34 +01002922 edge_serial->bulk_out_endpoint =
2923 endpoint->bEndpointAddress;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002924 bulk_out_found = true;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002925 }
2926 }
2927
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002928 if (!interrupt_in_found || !bulk_in_found || !bulk_out_found) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002929 dev_err(ddev, "Error - the proper endpoints were not found!\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002930 return -ENODEV;
2931 }
2932
2933 /* start interrupt read for this edgeport this interrupt will
2934 * continue as long as the edgeport is connected */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002935 response = usb_submit_urb(edge_serial->interrupt_read_urb,
2936 GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002937 if (response)
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002938 dev_err(ddev, "%s - Error %d submitting control urb\n",
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07002939 __func__, response);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002940 }
2941 return response;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942}
2943
2944
2945/****************************************************************************
Alan Sternf9c99bb2009-06-02 11:53:55 -04002946 * edge_disconnect
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 * This function is called whenever the device is removed from the usb bus.
2948 ****************************************************************************/
Alan Sternf9c99bb2009-06-02 11:53:55 -04002949static void edge_disconnect(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950{
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002951 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 /* stop reads and writes on all ports */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002954 /* free up our endpoint stuff */
2955 if (edge_serial->is_epic) {
Oliver Neukum74ac07e2007-06-13 18:50:41 +02002956 usb_kill_urb(edge_serial->interrupt_read_urb);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002957 usb_free_urb(edge_serial->interrupt_read_urb);
2958 kfree(edge_serial->interrupt_in_buffer);
2959
Oliver Neukum74ac07e2007-06-13 18:50:41 +02002960 usb_kill_urb(edge_serial->read_urb);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002961 usb_free_urb(edge_serial->read_urb);
2962 kfree(edge_serial->bulk_in_buffer);
2963 }
Alan Sternf9c99bb2009-06-02 11:53:55 -04002964}
2965
2966
2967/****************************************************************************
2968 * edge_release
2969 * This function is called when the device structure is deallocated.
2970 ****************************************************************************/
2971static void edge_release(struct usb_serial *serial)
2972{
2973 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002974
2975 kfree(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976}
2977
Johan Hovoldc27f3ef2012-10-17 13:34:57 +02002978static int edge_port_probe(struct usb_serial_port *port)
2979{
2980 struct edgeport_port *edge_port;
2981
2982 edge_port = kzalloc(sizeof(*edge_port), GFP_KERNEL);
2983 if (!edge_port)
2984 return -ENOMEM;
2985
2986 spin_lock_init(&edge_port->ep_lock);
2987 edge_port->port = port;
2988
2989 usb_set_serial_port_data(port, edge_port);
2990
2991 return 0;
2992}
2993
2994static int edge_port_remove(struct usb_serial_port *port)
2995{
2996 struct edgeport_port *edge_port;
2997
2998 edge_port = usb_get_serial_port_data(port);
2999 kfree(edge_port);
3000
3001 return 0;
3002}
3003
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07003004module_usb_serial_driver(serial_drivers, id_table_combined);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005
Alan Cox03f0dbf2008-07-22 11:16:34 +01003006MODULE_AUTHOR(DRIVER_AUTHOR);
3007MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008MODULE_LICENSE("GPL");
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05303009MODULE_FIRMWARE("edgeport/boot.fw");
3010MODULE_FIRMWARE("edgeport/boot2.fw");
3011MODULE_FIRMWARE("edgeport/down.fw");
3012MODULE_FIRMWARE("edgeport/down2.fw");