blob: dc2803b5eb09c35fa25c75fd376474e42a327b03 [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
59#define CHASE_TIMEOUT (5*HZ) /* 5 seconds */
60#define OPEN_TIMEOUT (5*HZ) /* 5 seconds */
61#define COMMAND_TIMEOUT (5*HZ) /* 5 seconds */
62
63/* receive port state */
64enum RXSTATE {
Alan Cox03f0dbf2008-07-22 11:16:34 +010065 EXPECT_HDR1 = 0, /* Expect header byte 1 */
66 EXPECT_HDR2 = 1, /* Expect header byte 2 */
67 EXPECT_DATA = 2, /* Expect 'RxBytesRemaining' data */
68 EXPECT_HDR3 = 3, /* Expect header byte 3 (for status hdrs only) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070069};
70
71
Alan Cox03f0dbf2008-07-22 11:16:34 +010072/* Transmit Fifo
73 * This Transmit queue is an extension of the edgeport Rx buffer.
74 * The maximum amount of data buffered in both the edgeport
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 * Rx buffer (maxTxCredits) and this buffer will never exceed maxTxCredits.
76 */
77struct TxFifo {
78 unsigned int head; /* index to head pointer (write) */
79 unsigned int tail; /* index to tail pointer (read) */
80 unsigned int count; /* Bytes in queue */
81 unsigned int size; /* Max size of queue (equal to Max number of TxCredits) */
82 unsigned char *fifo; /* allocated Buffer */
83};
84
85/* This structure holds all of the local port information */
86struct edgeport_port {
87 __u16 txCredits; /* our current credits for this port */
88 __u16 maxTxCredits; /* the max size of the port */
89
90 struct TxFifo txfifo; /* transmit fifo -- size will be maxTxCredits */
91 struct urb *write_urb; /* write URB for this port */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +010092 bool write_in_progress; /* 'true' while a write URB is outstanding */
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 spinlock_t ep_lock;
94
95 __u8 shadowLCR; /* last LCR value received */
96 __u8 shadowMCR; /* last MCR value received */
97 __u8 shadowMSR; /* last MSR value received */
98 __u8 shadowLSR; /* last LSR value received */
99 __u8 shadowXonChar; /* last value set as XON char in Edgeport */
100 __u8 shadowXoffChar; /* last value set as XOFF char in Edgeport */
101 __u8 validDataMask;
102 __u32 baudRate;
103
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100104 bool open;
105 bool openPending;
106 bool commandPending;
107 bool closePending;
108 bool chaseResponsePending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
111 wait_queue_head_t wait_open; /* for handling sleeping while waiting for open to finish */
112 wait_queue_head_t wait_command; /* for handling sleeping while waiting for command to finish */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 struct usb_serial_port *port; /* loop back to the owner of this object */
115};
116
117
118/* This structure holds all of the individual device information */
119struct edgeport_serial {
Pete Zaitcevad933752006-05-22 21:49:44 -0700120 char name[MAX_NAME_LEN+2]; /* string name of this device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 struct edge_manuf_descriptor manuf_descriptor; /* the manufacturer descriptor */
123 struct edge_boot_descriptor boot_descriptor; /* the boot firmware descriptor */
124 struct edgeport_product_info product_info; /* Product Info */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800125 struct edge_compatibility_descriptor epic_descriptor; /* Edgeport compatible descriptor */
126 int is_epic; /* flag if EPiC device or not */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 __u8 interrupt_in_endpoint; /* the interrupt endpoint handle */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100129 unsigned char *interrupt_in_buffer; /* the buffer we use for the interrupt endpoint */
130 struct urb *interrupt_read_urb; /* our interrupt urb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 __u8 bulk_in_endpoint; /* the bulk in endpoint handle */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100133 unsigned char *bulk_in_buffer; /* the buffer we use for the bulk in endpoint */
134 struct urb *read_urb; /* our bulk read urb */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100135 bool read_in_progress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 spinlock_t es_lock;
137
138 __u8 bulk_out_endpoint; /* the bulk out endpoint handle */
139
140 __s16 rxBytesAvail; /* the number of bytes that we need to read from this device */
141
142 enum RXSTATE rxState; /* the current state of the bulk receive processor */
143 __u8 rxHeader1; /* receive header byte 1 */
144 __u8 rxHeader2; /* receive header byte 2 */
145 __u8 rxHeader3; /* receive header byte 3 */
146 __u8 rxPort; /* the port that we are currently receiving data for */
147 __u8 rxStatusCode; /* the receive status code */
148 __u8 rxStatusParam; /* the receive status paramater */
149 __s16 rxBytesRemaining; /* the number of port bytes left to read */
150 struct usb_serial *serial; /* loop back to the owner of this object */
151};
152
153/* baud rate information */
154struct divisor_table_entry {
155 __u32 BaudRate;
156 __u16 Divisor;
157};
158
Alan Cox03f0dbf2008-07-22 11:16:34 +0100159/*
160 * Define table of divisors for Rev A EdgePort/4 hardware
161 * These assume a 3.6864MHz crystal, the standard /16, and
162 * MCR.7 = 0.
163 */
164
Arjan van de Ven4c4c9432005-11-29 09:43:42 +0100165static const struct divisor_table_entry divisor_table[] = {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100166 { 50, 4608},
167 { 75, 3072},
168 { 110, 2095}, /* 2094.545455 => 230450 => .0217 % over */
169 { 134, 1713}, /* 1713.011152 => 230398.5 => .00065% under */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 { 150, 1536},
171 { 300, 768},
172 { 600, 384},
173 { 1200, 192},
174 { 1800, 128},
175 { 2400, 96},
176 { 4800, 48},
177 { 7200, 32},
178 { 9600, 24},
179 { 14400, 16},
180 { 19200, 12},
181 { 38400, 6},
182 { 57600, 4},
183 { 115200, 2},
184 { 230400, 1},
185};
186
Greg Kroah-Hartman36ccce42012-02-28 13:11:51 -0800187/* Number of outstanding Command Write Urbs */
188static atomic_t CmdUrbs = ATOMIC_INIT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190
191/* local function prototypes */
192
193/* function prototypes for all URB callbacks */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100194static void edge_interrupt_callback(struct urb *urb);
195static void edge_bulk_in_callback(struct urb *urb);
196static void edge_bulk_out_data_callback(struct urb *urb);
197static void edge_bulk_out_cmd_callback(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199/* function prototypes for the usbserial callbacks */
Alan Coxa509a7e2009-09-19 13:13:26 -0700200static int edge_open(struct tty_struct *tty, struct usb_serial_port *port);
Alan Cox335f8512009-06-11 12:26:29 +0100201static void edge_close(struct usb_serial_port *port);
Alan Cox03f0dbf2008-07-22 11:16:34 +0100202static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
203 const unsigned char *buf, int count);
204static int edge_write_room(struct tty_struct *tty);
205static int edge_chars_in_buffer(struct tty_struct *tty);
206static void edge_throttle(struct tty_struct *tty);
207static void edge_unthrottle(struct tty_struct *tty);
208static void edge_set_termios(struct tty_struct *tty,
209 struct usb_serial_port *port,
210 struct ktermios *old_termios);
Alan Cox00a0d0d2011-02-14 16:27:06 +0000211static int edge_ioctl(struct tty_struct *tty,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100212 unsigned int cmd, unsigned long arg);
213static void edge_break(struct tty_struct *tty, int break_state);
Alan Cox60b33c12011-02-14 16:26:14 +0000214static int edge_tiocmget(struct tty_struct *tty);
Alan Cox20b9d172011-02-14 16:26:50 +0000215static int edge_tiocmset(struct tty_struct *tty,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100216 unsigned int set, unsigned int clear);
217static int edge_startup(struct usb_serial *serial);
Alan Sternf9c99bb2009-06-02 11:53:55 -0400218static void edge_disconnect(struct usb_serial *serial);
219static void edge_release(struct usb_serial *serial);
Johan Hovoldc27f3ef2012-10-17 13:34:57 +0200220static int edge_port_probe(struct usb_serial_port *port);
221static int edge_port_remove(struct usb_serial_port *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223#include "io_tables.h" /* all of the devices that this driver supports */
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225/* function prototypes for all of our local functions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Alan Cox03f0dbf2008-07-22 11:16:34 +0100227static void process_rcvd_data(struct edgeport_serial *edge_serial,
228 unsigned char *buffer, __u16 bufferLength);
229static void process_rcvd_status(struct edgeport_serial *edge_serial,
230 __u8 byte2, __u8 byte3);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100231static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
232 int length);
Alan Cox03f0dbf2008-07-22 11:16:34 +0100233static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr);
234static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
235 __u8 lsr, __u8 data);
236static int send_iosp_ext_cmd(struct edgeport_port *edge_port, __u8 command,
237 __u8 param);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700238static int calc_baud_rate_divisor(struct device *dev, int baud_rate, int *divisor);
Alan Cox03f0dbf2008-07-22 11:16:34 +0100239static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
240 int baudRate);
241static void change_port_settings(struct tty_struct *tty,
242 struct edgeport_port *edge_port,
243 struct ktermios *old_termios);
244static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
245 __u8 regNum, __u8 regValue);
246static int write_cmd_usb(struct edgeport_port *edge_port,
247 unsigned char *buffer, int writeLength);
248static void send_more_port_data(struct edgeport_serial *edge_serial,
249 struct edgeport_port *edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Alan Cox03f0dbf2008-07-22 11:16:34 +0100251static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
252 __u16 length, const __u8 *data);
253static int rom_read(struct usb_serial *serial, __u16 extAddr, __u16 addr,
254 __u16 length, __u8 *data);
255static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
256 __u16 length, const __u8 *data);
257static void get_manufacturing_desc(struct edgeport_serial *edge_serial);
258static void get_boot_desc(struct edgeport_serial *edge_serial);
259static void load_application_firmware(struct edgeport_serial *edge_serial);
260
261static void unicode_to_ascii(char *string, int buflen,
262 __le16 *unicode, int unicode_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264
Alan Cox03f0dbf2008-07-22 11:16:34 +0100265/* ************************************************************************ */
266/* ************************************************************************ */
267/* ************************************************************************ */
268/* ************************************************************************ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270/************************************************************************
271 * *
272 * update_edgeport_E2PROM() Compare current versions of *
273 * Boot ROM and Manufacture *
274 * Descriptors with versions *
275 * embedded in this driver *
276 * *
277 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100278static void update_edgeport_E2PROM(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700280 struct device *dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 __u32 BootCurVer;
282 __u32 BootNewVer;
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530283 __u8 BootMajorVersion;
284 __u8 BootMinorVersion;
285 __u16 BootBuildNumber;
286 __u32 Bootaddr;
287 const struct ihex_binrec *rec;
288 const struct firmware *fw;
289 const char *fw_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 int response;
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 switch (edge_serial->product_info.iDownloadFile) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100293 case EDGE_DOWNLOAD_FILE_I930:
294 fw_name = "edgeport/boot.fw";
295 break;
296 case EDGE_DOWNLOAD_FILE_80251:
297 fw_name = "edgeport/boot2.fw";
298 break;
299 default:
300 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530303 response = request_ihex_firmware(&fw, fw_name,
304 &edge_serial->serial->dev->dev);
305 if (response) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700306 dev_err(dev, "Failed to load image \"%s\" err %d\n",
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530307 fw_name, response);
308 return;
309 }
310
311 rec = (const struct ihex_binrec *)fw->data;
312 BootMajorVersion = rec->data[0];
313 BootMinorVersion = rec->data[1];
314 BootBuildNumber = (rec->data[2] << 8) | rec->data[3];
315
Alan Cox03f0dbf2008-07-22 11:16:34 +0100316 /* Check Boot Image Version */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 BootCurVer = (edge_serial->boot_descriptor.MajorVersion << 24) +
318 (edge_serial->boot_descriptor.MinorVersion << 16) +
319 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber);
320
321 BootNewVer = (BootMajorVersion << 24) +
322 (BootMinorVersion << 16) +
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530323 BootBuildNumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700325 dev_dbg(dev, "Current Boot Image version %d.%d.%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 edge_serial->boot_descriptor.MajorVersion,
327 edge_serial->boot_descriptor.MinorVersion,
328 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
329
330
331 if (BootNewVer > BootCurVer) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700332 dev_dbg(dev, "**Update Boot Image from %d.%d.%d to %d.%d.%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 edge_serial->boot_descriptor.MajorVersion,
334 edge_serial->boot_descriptor.MinorVersion,
335 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber),
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530336 BootMajorVersion, BootMinorVersion, BootBuildNumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700338 dev_dbg(dev, "Downloading new Boot Image\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530340 for (rec = ihex_next_binrec(rec); rec;
341 rec = ihex_next_binrec(rec)) {
342 Bootaddr = be32_to_cpu(rec->addr);
343 response = rom_write(edge_serial->serial,
344 Bootaddr >> 16,
345 Bootaddr & 0xFFFF,
346 be16_to_cpu(rec->len),
347 &rec->data[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (response < 0) {
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530349 dev_err(&edge_serial->serial->dev->dev,
350 "rom_write failed (%x, %x, %d)\n",
351 Bootaddr >> 16, Bootaddr & 0xFFFF,
352 be16_to_cpu(rec->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 break;
354 }
355 }
356 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700357 dev_dbg(dev, "Boot Image -- already up to date\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530359 release_firmware(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362#if 0
363/************************************************************************
364 *
365 * Get string descriptor from device
366 *
367 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100368static int get_string_desc(struct usb_device *dev, int Id,
369 struct usb_string_descriptor **pRetDesc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
371 struct usb_string_descriptor StringDesc;
372 struct usb_string_descriptor *pStringDesc;
373
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700374 dev_dbg(&dev->dev, "%s - USB String ID = %d\n", __func__, Id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Alan Cox03f0dbf2008-07-22 11:16:34 +0100376 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc,
377 sizeof(StringDesc)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Alan Cox03f0dbf2008-07-22 11:16:34 +0100380 pStringDesc = kmalloc(StringDesc.bLength, GFP_KERNEL);
381 if (!pStringDesc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Alan Cox03f0dbf2008-07-22 11:16:34 +0100384 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc,
385 StringDesc.bLength)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 kfree(pStringDesc);
387 return -1;
388 }
389
390 *pRetDesc = pStringDesc;
391 return 0;
392}
393#endif
394
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700395static void dump_product_info(struct edgeport_serial *edge_serial,
396 struct edgeport_product_info *product_info)
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800397{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700398 struct device *dev = &edge_serial->serial->dev->dev;
399
Alan Cox03f0dbf2008-07-22 11:16:34 +0100400 /* Dump Product Info structure */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700401 dev_dbg(dev, "**Product Information:\n");
402 dev_dbg(dev, " ProductId %x\n", product_info->ProductId);
403 dev_dbg(dev, " NumPorts %d\n", product_info->NumPorts);
404 dev_dbg(dev, " ProdInfoVer %d\n", product_info->ProdInfoVer);
405 dev_dbg(dev, " IsServer %d\n", product_info->IsServer);
406 dev_dbg(dev, " IsRS232 %d\n", product_info->IsRS232);
407 dev_dbg(dev, " IsRS422 %d\n", product_info->IsRS422);
408 dev_dbg(dev, " IsRS485 %d\n", product_info->IsRS485);
409 dev_dbg(dev, " RomSize %d\n", product_info->RomSize);
410 dev_dbg(dev, " RamSize %d\n", product_info->RamSize);
411 dev_dbg(dev, " CpuRev %x\n", product_info->CpuRev);
412 dev_dbg(dev, " BoardRev %x\n", product_info->BoardRev);
413 dev_dbg(dev, " BootMajorVersion %d.%d.%d\n",
414 product_info->BootMajorVersion,
415 product_info->BootMinorVersion,
416 le16_to_cpu(product_info->BootBuildNumber));
417 dev_dbg(dev, " FirmwareMajorVersion %d.%d.%d\n",
418 product_info->FirmwareMajorVersion,
419 product_info->FirmwareMinorVersion,
420 le16_to_cpu(product_info->FirmwareBuildNumber));
421 dev_dbg(dev, " ManufactureDescDate %d/%d/%d\n",
422 product_info->ManufactureDescDate[0],
423 product_info->ManufactureDescDate[1],
424 product_info->ManufactureDescDate[2]+1900);
425 dev_dbg(dev, " iDownloadFile 0x%x\n",
426 product_info->iDownloadFile);
427 dev_dbg(dev, " EpicVer %d\n", product_info->EpicVer);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800428}
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430static void get_product_info(struct edgeport_serial *edge_serial)
431{
432 struct edgeport_product_info *product_info = &edge_serial->product_info;
433
Alan Cox03f0dbf2008-07-22 11:16:34 +0100434 memset(product_info, 0, sizeof(struct edgeport_product_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Alan Cox03f0dbf2008-07-22 11:16:34 +0100436 product_info->ProductId = (__u16)(le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct) & ~ION_DEVICE_ID_80251_NETCHIP);
437 product_info->NumPorts = edge_serial->manuf_descriptor.NumPorts;
438 product_info->ProdInfoVer = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Alan Cox03f0dbf2008-07-22 11:16:34 +0100440 product_info->RomSize = edge_serial->manuf_descriptor.RomSize;
441 product_info->RamSize = edge_serial->manuf_descriptor.RamSize;
442 product_info->CpuRev = edge_serial->manuf_descriptor.CpuRev;
443 product_info->BoardRev = edge_serial->manuf_descriptor.BoardRev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Alan Cox03f0dbf2008-07-22 11:16:34 +0100445 product_info->BootMajorVersion =
446 edge_serial->boot_descriptor.MajorVersion;
447 product_info->BootMinorVersion =
448 edge_serial->boot_descriptor.MinorVersion;
449 product_info->BootBuildNumber =
450 edge_serial->boot_descriptor.BuildNumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Alan Cox03f0dbf2008-07-22 11:16:34 +0100452 memcpy(product_info->ManufactureDescDate,
453 edge_serial->manuf_descriptor.DescDate,
454 sizeof(edge_serial->manuf_descriptor.DescDate));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Alan Cox03f0dbf2008-07-22 11:16:34 +0100456 /* check if this is 2nd generation hardware */
457 if (le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct)
458 & ION_DEVICE_ID_80251_NETCHIP)
459 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_80251;
460 else
461 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_I930;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700462
Alan Cox03f0dbf2008-07-22 11:16:34 +0100463 /* Determine Product type and set appropriate flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 switch (DEVICE_ID_FROM_USB_PRODUCT_ID(product_info->ProductId)) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100465 case ION_DEVICE_ID_EDGEPORT_COMPATIBLE:
466 case ION_DEVICE_ID_EDGEPORT_4T:
467 case ION_DEVICE_ID_EDGEPORT_4:
468 case ION_DEVICE_ID_EDGEPORT_2:
469 case ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU:
470 case ION_DEVICE_ID_EDGEPORT_8:
471 case ION_DEVICE_ID_EDGEPORT_421:
472 case ION_DEVICE_ID_EDGEPORT_21:
473 case ION_DEVICE_ID_EDGEPORT_2_DIN:
474 case ION_DEVICE_ID_EDGEPORT_4_DIN:
475 case ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU:
476 product_info->IsRS232 = 1;
477 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Alan Cox03f0dbf2008-07-22 11:16:34 +0100479 case ION_DEVICE_ID_EDGEPORT_2I: /* Edgeport/2 RS422/RS485 */
480 product_info->IsRS422 = 1;
481 product_info->IsRS485 = 1;
482 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Alan Cox03f0dbf2008-07-22 11:16:34 +0100484 case ION_DEVICE_ID_EDGEPORT_8I: /* Edgeport/4 RS422 */
485 case ION_DEVICE_ID_EDGEPORT_4I: /* Edgeport/4 RS422 */
486 product_info->IsRS422 = 1;
487 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
489
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700490 dump_product_info(edge_serial, product_info);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800491}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800493static int get_epic_descriptor(struct edgeport_serial *ep)
494{
495 int result;
496 struct usb_serial *serial = ep->serial;
497 struct edgeport_product_info *product_info = &ep->product_info;
498 struct edge_compatibility_descriptor *epic = &ep->epic_descriptor;
499 struct edge_compatibility_bits *bits;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700500 struct device *dev = &serial->dev->dev;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800501
502 ep->is_epic = 0;
503 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
504 USB_REQUEST_ION_GET_EPIC_DESC,
505 0xC0, 0x00, 0x00,
506 &ep->epic_descriptor,
507 sizeof(struct edge_compatibility_descriptor),
508 300);
509
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800510 if (result > 0) {
511 ep->is_epic = 1;
512 memset(product_info, 0, sizeof(struct edgeport_product_info));
513
Alan Cox03f0dbf2008-07-22 11:16:34 +0100514 product_info->NumPorts = epic->NumPorts;
515 product_info->ProdInfoVer = 0;
516 product_info->FirmwareMajorVersion = epic->MajorVersion;
517 product_info->FirmwareMinorVersion = epic->MinorVersion;
518 product_info->FirmwareBuildNumber = epic->BuildNumber;
519 product_info->iDownloadFile = epic->iDownloadFile;
520 product_info->EpicVer = epic->EpicVer;
521 product_info->Epic = epic->Supports;
522 product_info->ProductId = ION_DEVICE_ID_EDGEPORT_COMPATIBLE;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700523 dump_product_info(ep, product_info);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800524
525 bits = &ep->epic_descriptor.Supports;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700526 dev_dbg(dev, "**EPIC descriptor:\n");
527 dev_dbg(dev, " VendEnableSuspend: %s\n", bits->VendEnableSuspend ? "TRUE": "FALSE");
528 dev_dbg(dev, " IOSPOpen : %s\n", bits->IOSPOpen ? "TRUE": "FALSE");
529 dev_dbg(dev, " IOSPClose : %s\n", bits->IOSPClose ? "TRUE": "FALSE");
530 dev_dbg(dev, " IOSPChase : %s\n", bits->IOSPChase ? "TRUE": "FALSE");
531 dev_dbg(dev, " IOSPSetRxFlow : %s\n", bits->IOSPSetRxFlow ? "TRUE": "FALSE");
532 dev_dbg(dev, " IOSPSetTxFlow : %s\n", bits->IOSPSetTxFlow ? "TRUE": "FALSE");
533 dev_dbg(dev, " IOSPSetXChar : %s\n", bits->IOSPSetXChar ? "TRUE": "FALSE");
534 dev_dbg(dev, " IOSPRxCheck : %s\n", bits->IOSPRxCheck ? "TRUE": "FALSE");
535 dev_dbg(dev, " IOSPSetClrBreak : %s\n", bits->IOSPSetClrBreak ? "TRUE": "FALSE");
536 dev_dbg(dev, " IOSPWriteMCR : %s\n", bits->IOSPWriteMCR ? "TRUE": "FALSE");
537 dev_dbg(dev, " IOSPWriteLCR : %s\n", bits->IOSPWriteLCR ? "TRUE": "FALSE");
538 dev_dbg(dev, " IOSPSetBaudRate : %s\n", bits->IOSPSetBaudRate ? "TRUE": "FALSE");
539 dev_dbg(dev, " TrueEdgeport : %s\n", bits->TrueEdgeport ? "TRUE": "FALSE");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800540 }
541
542 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
545
546/************************************************************************/
547/************************************************************************/
548/* U S B C A L L B A C K F U N C T I O N S */
549/* U S B C A L L B A C K F U N C T I O N S */
550/************************************************************************/
551/************************************************************************/
552
553/*****************************************************************************
554 * edge_interrupt_callback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100555 * this is the callback function for when we have received data on the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 * interrupt endpoint.
557 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100558static void edge_interrupt_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700560 struct edgeport_serial *edge_serial = urb->context;
561 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 struct edgeport_port *edge_port;
563 struct usb_serial_port *port;
564 unsigned char *data = urb->transfer_buffer;
565 int length = urb->actual_length;
566 int bytes_avail;
567 int position;
568 int txCredits;
569 int portNumber;
570 int result;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700571 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700573 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 case 0:
575 /* success */
576 break;
577 case -ECONNRESET:
578 case -ENOENT:
579 case -ESHUTDOWN:
580 /* this urb is terminated, clean up */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700581 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 return;
583 default:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700584 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 goto exit;
586 }
587
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700588 dev = &edge_serial->serial->dev->dev;
589
Alan Cox03f0dbf2008-07-22 11:16:34 +0100590 /* process this interrupt-read even if there are no ports open */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (length) {
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100592 usb_serial_debug_data(dev, __func__, length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 if (length > 1) {
595 bytes_avail = data[0] | (data[1] << 8);
596 if (bytes_avail) {
597 spin_lock(&edge_serial->es_lock);
598 edge_serial->rxBytesAvail += bytes_avail;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700599 dev_dbg(dev,
600 "%s - bytes_avail=%d, rxBytesAvail=%d, read_in_progress=%d\n",
601 __func__, bytes_avail,
602 edge_serial->rxBytesAvail,
603 edge_serial->read_in_progress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 if (edge_serial->rxBytesAvail > 0 &&
606 !edge_serial->read_in_progress) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700607 dev_dbg(dev, "%s - posting a read\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100608 edge_serial->read_in_progress = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Alan Cox03f0dbf2008-07-22 11:16:34 +0100610 /* we have pending bytes on the
611 bulk in pipe, send a request */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 result = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
613 if (result) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700614 dev_err(dev,
615 "%s - usb_submit_urb(read bulk) failed with result = %d\n",
616 __func__, result);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100617 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 }
619 }
620 spin_unlock(&edge_serial->es_lock);
621 }
622 }
623 /* grab the txcredits for the ports if available */
624 position = 2;
625 portNumber = 0;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100626 while ((position < length) &&
627 (portNumber < edge_serial->serial->num_ports)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 txCredits = data[position] | (data[position+1] << 8);
629 if (txCredits) {
630 port = edge_serial->serial->port[portNumber];
631 edge_port = usb_get_serial_port_data(port);
632 if (edge_port->open) {
633 spin_lock(&edge_port->ep_lock);
634 edge_port->txCredits += txCredits;
635 spin_unlock(&edge_port->ep_lock);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700636 dev_dbg(dev, "%s - txcredits for port%d = %d\n",
637 __func__, portNumber,
638 edge_port->txCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Alan Cox03f0dbf2008-07-22 11:16:34 +0100640 /* tell the tty driver that something
641 has changed */
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100642 tty_port_tty_wakeup(&edge_port->port->port);
Alan Cox03f0dbf2008-07-22 11:16:34 +0100643 /* Since we have more credit, check
644 if more data can be sent */
645 send_more_port_data(edge_serial,
646 edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
648 }
649 position += 2;
650 ++portNumber;
651 }
652 }
653
654exit:
Alan Cox03f0dbf2008-07-22 11:16:34 +0100655 result = usb_submit_urb(urb, GFP_ATOMIC);
656 if (result)
657 dev_err(&urb->dev->dev,
658 "%s - Error %d submitting control urb\n",
659 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
662
663/*****************************************************************************
664 * edge_bulk_in_callback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100665 * this is the callback function for when we have received data on the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 * bulk in endpoint.
667 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100668static void edge_bulk_in_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Ming Leicdc97792008-02-24 18:41:47 +0800670 struct edgeport_serial *edge_serial = urb->context;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700671 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700673 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 __u16 raw_data_length;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700675 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700677 if (status) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700678 dev_dbg(&urb->dev->dev, "%s - nonzero read bulk status received: %d\n",
679 __func__, status);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100680 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return;
682 }
683
684 if (urb->actual_length == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700685 dev_dbg(&urb->dev->dev, "%s - read bulk callback with no data\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100686 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return;
688 }
689
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700690 dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 raw_data_length = urb->actual_length;
692
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100693 usb_serial_debug_data(dev, __func__, raw_data_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 spin_lock(&edge_serial->es_lock);
696
697 /* decrement our rxBytes available by the number that we just got */
698 edge_serial->rxBytesAvail -= raw_data_length;
699
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700700 dev_dbg(dev, "%s - Received = %d, rxBytesAvail %d\n", __func__,
701 raw_data_length, edge_serial->rxBytesAvail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Alan Cox03f0dbf2008-07-22 11:16:34 +0100703 process_rcvd_data(edge_serial, data, urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 /* check to see if there's any more data for us to read */
706 if (edge_serial->rxBytesAvail > 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700707 dev_dbg(dev, "%s - posting a read\n", __func__);
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700708 retval = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
709 if (retval) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700710 dev_err(dev,
711 "%s - usb_submit_urb(read bulk) failed, retval = %d\n",
712 __func__, retval);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100713 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
715 } else {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100716 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
718
719 spin_unlock(&edge_serial->es_lock);
720}
721
722
723/*****************************************************************************
724 * edge_bulk_out_data_callback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100725 * this is the callback function for when we have finished sending
726 * serial data on the bulk out endpoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100728static void edge_bulk_out_data_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
Ming Leicdc97792008-02-24 18:41:47 +0800730 struct edgeport_port *edge_port = urb->context;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700731 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700733 if (status) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700734 dev_dbg(&urb->dev->dev,
735 "%s - nonzero write bulk status received: %d\n",
736 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 }
738
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100739 if (edge_port->open)
740 tty_port_tty_wakeup(&edge_port->port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Alan Cox03f0dbf2008-07-22 11:16:34 +0100742 /* Release the Write URB */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100743 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Alan Cox03f0dbf2008-07-22 11:16:34 +0100745 /* Check if more data needs to be sent */
746 send_more_port_data((struct edgeport_serial *)
747 (usb_get_serial_data(edge_port->port->serial)), edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748}
749
750
751/*****************************************************************************
752 * BulkOutCmdCallback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100753 * this is the callback function for when we have finished sending a
754 * command on the bulk out endpoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100756static void edge_bulk_out_cmd_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Ming Leicdc97792008-02-24 18:41:47 +0800758 struct edgeport_port *edge_port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 int status = urb->status;
760
Oliver Neukum96c706e2007-03-15 15:27:17 +0100761 atomic_dec(&CmdUrbs);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700762 dev_dbg(&urb->dev->dev, "%s - FREE URB %p (outstanding %d)\n",
763 __func__, urb, atomic_read(&CmdUrbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765
766 /* clean up the transfer buffer */
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700767 kfree(urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769 /* Free the command urb */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100770 usb_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 if (status) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700773 dev_dbg(&urb->dev->dev,
774 "%s - nonzero write bulk status received: %d\n",
775 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return;
777 }
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 /* tell the tty driver that something has changed */
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100780 if (edge_port->open)
781 tty_port_tty_wakeup(&edge_port->port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 /* we have completed the command */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100784 edge_port->commandPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 wake_up(&edge_port->wait_command);
786}
787
788
789/*****************************************************************************
790 * Driver tty interface functions
791 *****************************************************************************/
792
793/*****************************************************************************
794 * SerialOpen
795 * this function is called by the tty driver when a port is opened
796 * If successful, we return 0
797 * Otherwise we return a negative error number.
798 *****************************************************************************/
Alan Coxa509a7e2009-09-19 13:13:26 -0700799static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
801 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700802 struct device *dev = &port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 struct usb_serial *serial;
804 struct edgeport_serial *edge_serial;
805 int response;
806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 if (edge_port == NULL)
808 return -ENODEV;
809
Alan Cox03f0dbf2008-07-22 11:16:34 +0100810 /* see if we've set up our endpoint info yet (can't set it up
811 in edge_startup as the structures were not set up at that time.) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 serial = port->serial;
813 edge_serial = usb_get_serial_data(serial);
Alan Cox95da3102008-07-22 11:09:07 +0100814 if (edge_serial == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 if (edge_serial->interrupt_in_buffer == NULL) {
817 struct usb_serial_port *port0 = serial->port[0];
Alan Cox03f0dbf2008-07-22 11:16:34 +0100818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 /* not set up yet, so do it now */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100820 edge_serial->interrupt_in_buffer =
821 port0->interrupt_in_buffer;
822 edge_serial->interrupt_in_endpoint =
823 port0->interrupt_in_endpointAddress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 edge_serial->interrupt_read_urb = port0->interrupt_in_urb;
825 edge_serial->bulk_in_buffer = port0->bulk_in_buffer;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100826 edge_serial->bulk_in_endpoint =
827 port0->bulk_in_endpointAddress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 edge_serial->read_urb = port0->read_urb;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100829 edge_serial->bulk_out_endpoint =
830 port0->bulk_out_endpointAddress;
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 /* set up our interrupt urb */
833 usb_fill_int_urb(edge_serial->interrupt_read_urb,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100834 serial->dev,
835 usb_rcvintpipe(serial->dev,
836 port0->interrupt_in_endpointAddress),
837 port0->interrupt_in_buffer,
838 edge_serial->interrupt_read_urb->transfer_buffer_length,
839 edge_interrupt_callback, edge_serial,
840 edge_serial->interrupt_read_urb->interval);
841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 /* set up our bulk in urb */
843 usb_fill_bulk_urb(edge_serial->read_urb, serial->dev,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100844 usb_rcvbulkpipe(serial->dev,
845 port0->bulk_in_endpointAddress),
846 port0->bulk_in_buffer,
847 edge_serial->read_urb->transfer_buffer_length,
848 edge_bulk_in_callback, edge_serial);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100849 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 /* start interrupt read for this edgeport
Alan Cox03f0dbf2008-07-22 11:16:34 +0100852 * this interrupt will continue as long
853 * as the edgeport is connected */
854 response = usb_submit_urb(edge_serial->interrupt_read_urb,
855 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 if (response) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700857 dev_err(dev, "%s - Error %d submitting control urb\n",
858 __func__, response);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
860 }
Alan Cox03f0dbf2008-07-22 11:16:34 +0100861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 /* initialize our wait queues */
863 init_waitqueue_head(&edge_port->wait_open);
864 init_waitqueue_head(&edge_port->wait_chase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 init_waitqueue_head(&edge_port->wait_command);
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 /* initialize our port settings */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100868 edge_port->txCredits = 0; /* Can't send any data yet */
869 /* Must always set this bit to enable ints! */
870 edge_port->shadowMCR = MCR_MASTER_IE;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100871 edge_port->chaseResponsePending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873 /* send a open port command */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100874 edge_port->openPending = true;
875 edge_port->open = false;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100876 response = send_iosp_ext_cmd(edge_port, IOSP_CMD_OPEN_PORT, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 if (response < 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700879 dev_err(dev, "%s - error sending open port command\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100880 edge_port->openPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return -ENODEV;
882 }
883
884 /* now wait for the port to be completely opened */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100885 wait_event_timeout(edge_port->wait_open, !edge_port->openPending,
886 OPEN_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100888 if (!edge_port->open) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 /* open timed out */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700890 dev_dbg(dev, "%s - open timedout\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100891 edge_port->openPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return -ENODEV;
893 }
894
895 /* create the txfifo */
896 edge_port->txfifo.head = 0;
897 edge_port->txfifo.tail = 0;
898 edge_port->txfifo.count = 0;
899 edge_port->txfifo.size = edge_port->maxTxCredits;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100900 edge_port->txfifo.fifo = kmalloc(edge_port->maxTxCredits, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902 if (!edge_port->txfifo.fifo) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700903 dev_dbg(dev, "%s - no memory\n", __func__);
Alan Cox335f8512009-06-11 12:26:29 +0100904 edge_close(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 return -ENOMEM;
906 }
907
908 /* Allocate a URB for the write */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100909 edge_port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100910 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 if (!edge_port->write_urb) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700913 dev_dbg(dev, "%s - no memory\n", __func__);
Alan Cox335f8512009-06-11 12:26:29 +0100914 edge_close(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return -ENOMEM;
916 }
917
Greg Kroah-Hartman11438322013-06-06 10:32:00 -0700918 dev_dbg(dev, "%s - Initialize TX fifo to %d bytes\n",
919 __func__, edge_port->maxTxCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 return 0;
922}
923
924
925/************************************************************************
926 *
927 * block_until_chase_response
928 *
929 * This function will block the close until one of the following:
930 * 1. Response to our Chase comes from Edgeport
Joe Perchesdc0d5c12007-12-17 11:40:18 -0800931 * 2. A timeout of 10 seconds without activity has expired
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 * (1K of Edgeport data @ 2400 baud ==> 4 sec to empty)
933 *
934 ************************************************************************/
935static void block_until_chase_response(struct edgeport_port *edge_port)
936{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700937 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 DEFINE_WAIT(wait);
939 __u16 lastCredits;
940 int timeout = 1*HZ;
941 int loop = 10;
942
943 while (1) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100944 /* Save Last credits */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 lastCredits = edge_port->txCredits;
946
Alan Cox03f0dbf2008-07-22 11:16:34 +0100947 /* Did we get our Chase response */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100948 if (!edge_port->chaseResponsePending) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700949 dev_dbg(dev, "%s - Got Chase Response\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Alan Cox03f0dbf2008-07-22 11:16:34 +0100951 /* did we get all of our credit back? */
952 if (edge_port->txCredits == edge_port->maxTxCredits) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700953 dev_dbg(dev, "%s - Got all credits\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 return;
955 }
956 }
957
Alan Cox03f0dbf2008-07-22 11:16:34 +0100958 /* Block the thread for a while */
959 prepare_to_wait(&edge_port->wait_chase, &wait,
960 TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 schedule_timeout(timeout);
962 finish_wait(&edge_port->wait_chase, &wait);
963
964 if (lastCredits == edge_port->txCredits) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100965 /* No activity.. count down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 loop--;
967 if (loop == 0) {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100968 edge_port->chaseResponsePending = false;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700969 dev_dbg(dev, "%s - Chase TIMEOUT\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 return;
971 }
972 } else {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100973 /* Reset timeout value back to 10 seconds */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700974 dev_dbg(dev, "%s - Last %d, Current %d\n", __func__,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100975 lastCredits, edge_port->txCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 loop = 10;
977 }
978 }
979}
980
981
982/************************************************************************
983 *
984 * block_until_tx_empty
985 *
986 * This function will block the close until one of the following:
987 * 1. TX count are 0
988 * 2. The edgeport has stopped
Joe Perchesdc0d5c12007-12-17 11:40:18 -0800989 * 3. A timeout of 3 seconds without activity has expired
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 *
991 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100992static void block_until_tx_empty(struct edgeport_port *edge_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700994 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 DEFINE_WAIT(wait);
996 struct TxFifo *fifo = &edge_port->txfifo;
997 __u32 lastCount;
998 int timeout = HZ/10;
999 int loop = 30;
1000
1001 while (1) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001002 /* Save Last count */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 lastCount = fifo->count;
1004
Alan Cox03f0dbf2008-07-22 11:16:34 +01001005 /* Is the Edgeport Buffer empty? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 if (lastCount == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001007 dev_dbg(dev, "%s - TX Buffer Empty\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 return;
1009 }
1010
Alan Cox03f0dbf2008-07-22 11:16:34 +01001011 /* Block the thread for a while */
1012 prepare_to_wait(&edge_port->wait_chase, &wait,
1013 TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 schedule_timeout(timeout);
1015 finish_wait(&edge_port->wait_chase, &wait);
1016
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001017 dev_dbg(dev, "%s wait\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 if (lastCount == fifo->count) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001020 /* No activity.. count down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 loop--;
1022 if (loop == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001023 dev_dbg(dev, "%s - TIMEOUT\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 return;
1025 }
1026 } else {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001027 /* Reset timeout value back to seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 loop = 30;
1029 }
1030 }
1031}
1032
1033
1034/*****************************************************************************
1035 * edge_close
1036 * this function is called by the tty driver when a port is closed
1037 *****************************************************************************/
Alan Cox335f8512009-06-11 12:26:29 +01001038static void edge_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
1040 struct edgeport_serial *edge_serial;
1041 struct edgeport_port *edge_port;
1042 int status;
1043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 edge_serial = usb_get_serial_data(port->serial);
1045 edge_port = usb_get_serial_port_data(port);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001046 if (edge_serial == NULL || edge_port == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 return;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001048
1049 /* block until tx is empty */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 block_until_tx_empty(edge_port);
1051
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001052 edge_port->closePending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001054 if ((!edge_serial->is_epic) ||
1055 ((edge_serial->is_epic) &&
1056 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1057 /* flush and chase */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001058 edge_port->chaseResponsePending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001060 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001061 status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
1062 if (status == 0)
1063 /* block until chase finished */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001064 block_until_chase_response(edge_port);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001065 else
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001066 edge_port->chaseResponsePending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
1068
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001069 if ((!edge_serial->is_epic) ||
1070 ((edge_serial->is_epic) &&
1071 (edge_serial->epic_descriptor.Supports.IOSPClose))) {
1072 /* close the port */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001073 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CLOSE_PORT\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001074 send_iosp_ext_cmd(edge_port, IOSP_CMD_CLOSE_PORT, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Alan Cox03f0dbf2008-07-22 11:16:34 +01001077 /* port->close = true; */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001078 edge_port->closePending = false;
1079 edge_port->open = false;
1080 edge_port->openPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Mariusz Kozlowski9a25f442006-11-08 15:36:29 +01001082 usb_kill_urb(edge_port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
1084 if (edge_port->write_urb) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001085 /* if this urb had a transfer buffer already
1086 (old transfer) free it */
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001087 kfree(edge_port->write_urb->transfer_buffer);
1088 usb_free_urb(edge_port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 edge_port->write_urb = NULL;
1090 }
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001091 kfree(edge_port->txfifo.fifo);
1092 edge_port->txfifo.fifo = NULL;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001093}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
1095/*****************************************************************************
1096 * SerialWrite
Alan Cox03f0dbf2008-07-22 11:16:34 +01001097 * this function is called by the tty driver when data should be written
1098 * to the port.
1099 * If successful, we return the number of bytes written, otherwise we
1100 * return a negative error number.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001102static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
1103 const unsigned char *data, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
1105 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1106 struct TxFifo *fifo;
1107 int copySize;
1108 int bytesleft;
1109 int firsthalf;
1110 int secondhalf;
1111 unsigned long flags;
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 if (edge_port == NULL)
1114 return -ENODEV;
1115
Alan Cox03f0dbf2008-07-22 11:16:34 +01001116 /* get a pointer to the Tx fifo */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 fifo = &edge_port->txfifo;
1118
1119 spin_lock_irqsave(&edge_port->ep_lock, flags);
1120
Alan Cox03f0dbf2008-07-22 11:16:34 +01001121 /* calculate number of bytes to put in fifo */
1122 copySize = min((unsigned int)count,
1123 (edge_port->txCredits - fifo->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001125 dev_dbg(&port->dev, "%s of %d byte(s) Fifo room %d -- will copy %d bytes\n",
1126 __func__, count, edge_port->txCredits - fifo->count, copySize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Alan Cox03f0dbf2008-07-22 11:16:34 +01001128 /* catch writes of 0 bytes which the tty driver likes to give us,
1129 and when txCredits is empty */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 if (copySize == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001131 dev_dbg(&port->dev, "%s - copySize = Zero\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 goto finish_write;
1133 }
1134
Alan Cox03f0dbf2008-07-22 11:16:34 +01001135 /* queue the data
1136 * since we can never overflow the buffer we do not have to check for a
1137 * full condition
1138 *
1139 * the copy is done is two parts -- first fill to the end of the buffer
1140 * then copy the reset from the start of the buffer
1141 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 bytesleft = fifo->size - fifo->head;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001143 firsthalf = min(bytesleft, copySize);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001144 dev_dbg(&port->dev, "%s - copy %d bytes of %d into fifo \n", __func__,
1145 firsthalf, bytesleft);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
1147 /* now copy our data */
1148 memcpy(&fifo->fifo[fifo->head], data, firsthalf);
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001149 usb_serial_debug_data(&port->dev, __func__, firsthalf, &fifo->fifo[fifo->head]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Alan Cox03f0dbf2008-07-22 11:16:34 +01001151 /* update the index and size */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 fifo->head += firsthalf;
1153 fifo->count += firsthalf;
1154
Alan Cox03f0dbf2008-07-22 11:16:34 +01001155 /* wrap the index */
1156 if (fifo->head == fifo->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 fifo->head = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159 secondhalf = copySize-firsthalf;
1160
1161 if (secondhalf) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001162 dev_dbg(&port->dev, "%s - copy rest of data %d\n", __func__, secondhalf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 memcpy(&fifo->fifo[fifo->head], &data[firsthalf], secondhalf);
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001164 usb_serial_debug_data(&port->dev, __func__, secondhalf, &fifo->fifo[fifo->head]);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001165 /* update the index and size */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 fifo->count += secondhalf;
1167 fifo->head += secondhalf;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001168 /* No need to check for wrap since we can not get to end of
1169 * the fifo in this part
1170 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
1172
1173finish_write:
1174 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1175
Alan Cox03f0dbf2008-07-22 11:16:34 +01001176 send_more_port_data((struct edgeport_serial *)
1177 usb_get_serial_data(port->serial), edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001179 dev_dbg(&port->dev, "%s wrote %d byte(s) TxCredits %d, Fifo %d\n",
1180 __func__, copySize, edge_port->txCredits, fifo->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
Alan Cox03f0dbf2008-07-22 11:16:34 +01001182 return copySize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183}
1184
1185
1186/************************************************************************
1187 *
1188 * send_more_port_data()
1189 *
1190 * This routine attempts to write additional UART transmit data
1191 * to a port over the USB bulk pipe. It is called (1) when new
1192 * data has been written to a port's TxBuffer from higher layers
1193 * (2) when the peripheral sends us additional TxCredits indicating
1194 * that it can accept more Tx data for a given port; and (3) when
1195 * a bulk write completes successfully and we want to see if we
1196 * can transmit more.
1197 *
1198 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001199static void send_more_port_data(struct edgeport_serial *edge_serial,
1200 struct edgeport_port *edge_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
1202 struct TxFifo *fifo = &edge_port->txfifo;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001203 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 struct urb *urb;
1205 unsigned char *buffer;
1206 int status;
1207 int count;
1208 int bytesleft;
1209 int firsthalf;
1210 int secondhalf;
1211 unsigned long flags;
1212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 spin_lock_irqsave(&edge_port->ep_lock, flags);
1214
1215 if (edge_port->write_in_progress ||
1216 !edge_port->open ||
1217 (fifo->count == 0)) {
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001218 dev_dbg(dev, "%s EXIT - fifo %d, PendingWrite = %d\n",
1219 __func__, fifo->count, edge_port->write_in_progress);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 goto exit_send;
1221 }
1222
Alan Cox03f0dbf2008-07-22 11:16:34 +01001223 /* since the amount of data in the fifo will always fit into the
1224 * edgeport buffer we do not need to check the write length
1225 *
1226 * Do we have enough credits for this port to make it worthwhile
1227 * to bother queueing a write. If it's too small, say a few bytes,
1228 * it's better to wait for more credits so we can do a larger write.
1229 */
1230 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 -07001231 dev_dbg(dev, "%s Not enough credit - fifo %d TxCredit %d\n",
1232 __func__, fifo->count, edge_port->txCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 goto exit_send;
1234 }
1235
Alan Cox03f0dbf2008-07-22 11:16:34 +01001236 /* lock this write */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001237 edge_port->write_in_progress = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Alan Cox03f0dbf2008-07-22 11:16:34 +01001239 /* get a pointer to the write_urb */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 urb = edge_port->write_urb;
1241
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001242 /* make sure transfer buffer is freed */
1243 kfree(urb->transfer_buffer);
1244 urb->transfer_buffer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Alan Cox03f0dbf2008-07-22 11:16:34 +01001246 /* build the data header for the buffer and port that we are about
1247 to send out */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 count = fifo->count;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001249 buffer = kmalloc(count+2, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 if (buffer == NULL) {
Johan Hovold22a416c2012-02-10 13:20:51 +01001251 dev_err_console(edge_port->port,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001252 "%s - no more kernel memory...\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001253 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 goto exit_send;
1255 }
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001256 buffer[0] = IOSP_BUILD_DATA_HDR1(edge_port->port->port_number, count);
1257 buffer[1] = IOSP_BUILD_DATA_HDR2(edge_port->port->port_number, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259 /* now copy our data */
1260 bytesleft = fifo->size - fifo->tail;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001261 firsthalf = min(bytesleft, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 memcpy(&buffer[2], &fifo->fifo[fifo->tail], firsthalf);
1263 fifo->tail += firsthalf;
1264 fifo->count -= firsthalf;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001265 if (fifo->tail == fifo->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 fifo->tail = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 secondhalf = count-firsthalf;
1269 if (secondhalf) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001270 memcpy(&buffer[2+firsthalf], &fifo->fifo[fifo->tail],
1271 secondhalf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 fifo->tail += secondhalf;
1273 fifo->count -= secondhalf;
1274 }
1275
1276 if (count)
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001277 usb_serial_debug_data(&edge_port->port->dev, __func__, count, &buffer[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
1279 /* fill up the urb with all of our data and submit it */
Alan Cox03f0dbf2008-07-22 11:16:34 +01001280 usb_fill_bulk_urb(urb, edge_serial->serial->dev,
1281 usb_sndbulkpipe(edge_serial->serial->dev,
1282 edge_serial->bulk_out_endpoint),
1283 buffer, count+2,
1284 edge_bulk_out_data_callback, edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 /* decrement the number of credits we have by the number we just sent */
1287 edge_port->txCredits -= count;
Johan Hovoldd36a7712013-03-21 12:37:07 +01001288 edge_port->port->icount.tx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 status = usb_submit_urb(urb, GFP_ATOMIC);
1291 if (status) {
1292 /* something went wrong */
Johan Hovold22a416c2012-02-10 13:20:51 +01001293 dev_err_console(edge_port->port,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001294 "%s - usb_submit_urb(write bulk) failed, status = %d, data lost\n",
1295 __func__, status);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001296 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
1298 /* revert the credits as something bad happened. */
1299 edge_port->txCredits += count;
Johan Hovoldd36a7712013-03-21 12:37:07 +01001300 edge_port->port->icount.tx -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 }
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001302 dev_dbg(dev, "%s wrote %d byte(s) TxCredit %d, Fifo %d\n",
1303 __func__, count, edge_port->txCredits, fifo->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305exit_send:
1306 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1307}
1308
1309
1310/*****************************************************************************
1311 * edge_write_room
Alan Cox03f0dbf2008-07-22 11:16:34 +01001312 * this function is called by the tty driver when it wants to know how
1313 * many bytes of data we can accept for a specific port. If successful,
1314 * we return the amount of room that we have for this port (the txCredits)
1315 * otherwise we return a negative error number.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001317static int edge_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
Alan Cox95da3102008-07-22 11:09:07 +01001319 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1321 int room;
1322 unsigned long flags;
1323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 if (edge_port == NULL)
Alan Coxd76f2f442008-07-22 11:16:42 +01001325 return 0;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001326 if (edge_port->closePending)
Alan Coxd76f2f442008-07-22 11:16:42 +01001327 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001330 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Alan Coxd76f2f442008-07-22 11:16:42 +01001331 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 }
1333
Alan Cox03f0dbf2008-07-22 11:16:34 +01001334 /* total of both buffers is still txCredit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 spin_lock_irqsave(&edge_port->ep_lock, flags);
1336 room = edge_port->txCredits - edge_port->txfifo.count;
1337 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1338
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001339 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 return room;
1341}
1342
1343
1344/*****************************************************************************
1345 * edge_chars_in_buffer
Alan Cox03f0dbf2008-07-22 11:16:34 +01001346 * this function is called by the tty driver when it wants to know how
1347 * many bytes of data we currently have outstanding in the port (data that
1348 * has been written, but hasn't made it out the port yet)
1349 * If successful, we return the number of bytes left to be written in the
1350 * system,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 * Otherwise we return a negative error number.
1352 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001353static int edge_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
Alan Cox95da3102008-07-22 11:09:07 +01001355 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1357 int num_chars;
1358 unsigned long flags;
1359
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 if (edge_port == NULL)
Alan Coxd76f2f442008-07-22 11:16:42 +01001361 return 0;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001362 if (edge_port->closePending)
Alan Coxd76f2f442008-07-22 11:16:42 +01001363 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001366 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Alan Coxd76f2f442008-07-22 11:16:42 +01001367 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 }
1369
1370 spin_lock_irqsave(&edge_port->ep_lock, flags);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001371 num_chars = edge_port->maxTxCredits - edge_port->txCredits +
1372 edge_port->txfifo.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1374 if (num_chars) {
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001375 dev_dbg(&port->dev, "%s - returns %d\n", __func__, num_chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 }
1377
1378 return num_chars;
1379}
1380
1381
1382/*****************************************************************************
1383 * SerialThrottle
1384 * this function is called by the tty driver when it wants to stop the data
1385 * being read from the port.
1386 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001387static void edge_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388{
Alan Cox95da3102008-07-22 11:09:07 +01001389 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 int status;
1392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 if (edge_port == NULL)
1394 return;
1395
1396 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001397 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 return;
1399 }
1400
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 /* if we are implementing XON/XOFF, send the stop character */
1402 if (I_IXOFF(tty)) {
1403 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001404 status = edge_write(tty, port, &stop_char, 1);
1405 if (status <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 }
1408
1409 /* if we are implementing RTS/CTS, toggle that line */
Alan Coxadc8d742012-07-14 15:31:47 +01001410 if (tty->termios.c_cflag & CRTSCTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 edge_port->shadowMCR &= ~MCR_RTS;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001412 status = send_cmd_write_uart_register(edge_port, MCR,
1413 edge_port->shadowMCR);
1414 if (status != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417}
1418
1419
1420/*****************************************************************************
1421 * edge_unthrottle
Alan Cox03f0dbf2008-07-22 11:16:34 +01001422 * this function is called by the tty driver when it wants to resume the
1423 * data being read from the port (called after SerialThrottle is called)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001425static void edge_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426{
Alan Cox95da3102008-07-22 11:09:07 +01001427 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 int status;
1430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 if (edge_port == NULL)
1432 return;
1433
1434 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001435 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 return;
1437 }
1438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 /* if we are implementing XON/XOFF, send the start character */
1440 if (I_IXOFF(tty)) {
1441 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001442 status = edge_write(tty, port, &start_char, 1);
1443 if (status <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 /* if we are implementing RTS/CTS, toggle that line */
Alan Coxadc8d742012-07-14 15:31:47 +01001447 if (tty->termios.c_cflag & CRTSCTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 edge_port->shadowMCR |= MCR_RTS;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001449 send_cmd_write_uart_register(edge_port, MCR,
1450 edge_port->shadowMCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452}
1453
1454
1455/*****************************************************************************
1456 * SerialSetTermios
Alan Cox03f0dbf2008-07-22 11:16:34 +01001457 * this function is called by the tty driver when it wants to change
1458 * the termios structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001460static void edge_set_termios(struct tty_struct *tty,
1461 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462{
1463 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 unsigned int cflag;
1465
Alan Coxadc8d742012-07-14 15:31:47 +01001466 cflag = tty->termios.c_cflag;
Linus Torvaldsd9a80742012-10-01 13:23:01 -07001467 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 -07001468 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 -07001469
1470 if (edge_port == NULL)
1471 return;
1472
1473 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001474 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 return;
1476 }
1477
1478 /* change the port settings to the new ones specified */
Alan Cox95da3102008-07-22 11:09:07 +01001479 change_port_settings(tty, edge_port, old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480}
1481
1482
1483/*****************************************************************************
1484 * get_lsr_info - get line status register info
1485 *
1486 * Purpose: Let user call ioctl() to get info when the UART physically
1487 * is emptied. On bus types like RS485, the transmitter must
1488 * release the bus after transmitting. This must be done when
1489 * the transmit shift register is empty, not be done when the
1490 * transmit holding register is empty. This functionality
Alan Cox03f0dbf2008-07-22 11:16:34 +01001491 * allows an RS485 driver to be written in user space.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001493static int get_lsr_info(struct edgeport_port *edge_port,
1494 unsigned int __user *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
1496 unsigned int result = 0;
1497 unsigned long flags;
1498
1499 spin_lock_irqsave(&edge_port->ep_lock, flags);
1500 if (edge_port->maxTxCredits == edge_port->txCredits &&
1501 edge_port->txfifo.count == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001502 dev_dbg(&edge_port->port->dev, "%s -- Empty\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 result = TIOCSER_TEMT;
1504 }
1505 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1506
1507 if (copy_to_user(value, &result, sizeof(int)))
1508 return -EFAULT;
1509 return 0;
1510}
1511
Alan Cox20b9d172011-02-14 16:26:50 +00001512static int edge_tiocmset(struct tty_struct *tty,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001513 unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
Alan Cox95da3102008-07-22 11:09:07 +01001515 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1517 unsigned int mcr;
1518
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 mcr = edge_port->shadowMCR;
1520 if (set & TIOCM_RTS)
1521 mcr |= MCR_RTS;
1522 if (set & TIOCM_DTR)
1523 mcr |= MCR_DTR;
1524 if (set & TIOCM_LOOP)
1525 mcr |= MCR_LOOPBACK;
1526
1527 if (clear & TIOCM_RTS)
1528 mcr &= ~MCR_RTS;
1529 if (clear & TIOCM_DTR)
1530 mcr &= ~MCR_DTR;
1531 if (clear & TIOCM_LOOP)
1532 mcr &= ~MCR_LOOPBACK;
1533
1534 edge_port->shadowMCR = mcr;
1535
1536 send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1537
1538 return 0;
1539}
1540
Alan Cox60b33c12011-02-14 16:26:14 +00001541static int edge_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
Alan Cox95da3102008-07-22 11:09:07 +01001543 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1545 unsigned int result = 0;
1546 unsigned int msr;
1547 unsigned int mcr;
1548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 msr = edge_port->shadowMSR;
1550 mcr = edge_port->shadowMCR;
1551 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
1552 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
1553 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
1554 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
1555 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
1556 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
1557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 return result;
1559}
1560
Alan Cox03f0dbf2008-07-22 11:16:34 +01001561static int get_serial_info(struct edgeport_port *edge_port,
1562 struct serial_struct __user *retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563{
1564 struct serial_struct tmp;
1565
1566 if (!retinfo)
1567 return -EFAULT;
1568
1569 memset(&tmp, 0, sizeof(tmp));
1570
1571 tmp.type = PORT_16550A;
Greg Kroah-Hartmane5b1e202013-06-07 11:04:28 -07001572 tmp.line = edge_port->port->minor;
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001573 tmp.port = edge_port->port->port_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 tmp.irq = 0;
1575 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1576 tmp.xmit_fifo_size = edge_port->maxTxCredits;
1577 tmp.baud_base = 9600;
1578 tmp.close_delay = 5*HZ;
1579 tmp.closing_wait = 30*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
1581 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1582 return -EFAULT;
1583 return 0;
1584}
1585
1586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587/*****************************************************************************
1588 * SerialIoctl
1589 * this function handles any ioctl calls to the driver
1590 *****************************************************************************/
Alan Cox00a0d0d2011-02-14 16:27:06 +00001591static int edge_ioctl(struct tty_struct *tty,
Alan Cox95da3102008-07-22 11:09:07 +01001592 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
Alan Cox95da3102008-07-22 11:09:07 +01001594 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 DEFINE_WAIT(wait);
1596 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001598 dev_dbg(&port->dev, "%s - cmd = 0x%x\n", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
1600 switch (cmd) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001601 case TIOCSERGETLSR:
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001602 dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001603 return get_lsr_info(edge_port, (unsigned int __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Alan Cox03f0dbf2008-07-22 11:16:34 +01001605 case TIOCGSERIAL:
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001606 dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001607 return get_serial_info(edge_port, (struct serial_struct __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 return -ENOIOCTLCMD;
1610}
1611
1612
1613/*****************************************************************************
1614 * SerialBreak
1615 * this function sends a break to the port
1616 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001617static void edge_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618{
Alan Cox95da3102008-07-22 11:09:07 +01001619 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001621 struct edgeport_serial *edge_serial = usb_get_serial_data(port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 int status;
1623
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001624 if ((!edge_serial->is_epic) ||
1625 ((edge_serial->is_epic) &&
1626 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1627 /* flush and chase */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001628 edge_port->chaseResponsePending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001630 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001631 status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001632 if (status == 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001633 /* block until chase finished */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001634 block_until_chase_response(edge_port);
1635 } else {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001636 edge_port->chaseResponsePending = false;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 }
1639
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001640 if ((!edge_serial->is_epic) ||
1641 ((edge_serial->is_epic) &&
1642 (edge_serial->epic_descriptor.Supports.IOSPSetClrBreak))) {
1643 if (break_state == -1) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001644 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_SET_BREAK\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001645 status = send_iosp_ext_cmd(edge_port,
1646 IOSP_CMD_SET_BREAK, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001647 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001648 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CLEAR_BREAK\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001649 status = send_iosp_ext_cmd(edge_port,
1650 IOSP_CMD_CLEAR_BREAK, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001651 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01001652 if (status)
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001653 dev_dbg(&port->dev, "%s - error sending break set/clear command.\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01001654 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656}
1657
1658
1659/*****************************************************************************
1660 * process_rcvd_data
1661 * this function handles the data received on the bulk in pipe.
1662 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001663static void process_rcvd_data(struct edgeport_serial *edge_serial,
1664 unsigned char *buffer, __u16 bufferLength)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001666 struct device *dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 struct usb_serial_port *port;
1668 struct edgeport_port *edge_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 __u16 lastBufferLength;
1670 __u16 rxLen;
1671
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 lastBufferLength = bufferLength + 1;
1673
1674 while (bufferLength > 0) {
1675 /* failsafe incase we get a message that we don't understand */
1676 if (lastBufferLength == bufferLength) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001677 dev_dbg(dev, "%s - stuck in loop, exiting it.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 break;
1679 }
1680 lastBufferLength = bufferLength;
1681
1682 switch (edge_serial->rxState) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001683 case EXPECT_HDR1:
1684 edge_serial->rxHeader1 = *buffer;
1685 ++buffer;
1686 --bufferLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Alan Cox03f0dbf2008-07-22 11:16:34 +01001688 if (bufferLength == 0) {
1689 edge_serial->rxState = EXPECT_HDR2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 break;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001691 }
1692 /* otherwise, drop on through */
1693 case EXPECT_HDR2:
1694 edge_serial->rxHeader2 = *buffer;
1695 ++buffer;
1696 --bufferLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001698 dev_dbg(dev, "%s - Hdr1=%02X Hdr2=%02X\n", __func__,
1699 edge_serial->rxHeader1, edge_serial->rxHeader2);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001700 /* Process depending on whether this header is
1701 * data or status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Alan Cox03f0dbf2008-07-22 11:16:34 +01001703 if (IS_CMD_STAT_HDR(edge_serial->rxHeader1)) {
1704 /* Decode this status header and go to
1705 * EXPECT_HDR1 (if we can process the status
1706 * with only 2 bytes), or go to EXPECT_HDR3 to
1707 * get the third byte. */
1708 edge_serial->rxPort =
1709 IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1710 edge_serial->rxStatusCode =
1711 IOSP_GET_STATUS_CODE(
1712 edge_serial->rxHeader1);
1713
1714 if (!IOSP_STATUS_IS_2BYTE(
1715 edge_serial->rxStatusCode)) {
1716 /* This status needs additional bytes.
1717 * Save what we have and then wait for
1718 * more data.
1719 */
1720 edge_serial->rxStatusParam
1721 = edge_serial->rxHeader2;
1722 edge_serial->rxState = EXPECT_HDR3;
1723 break;
1724 }
1725 /* We have all the header bytes, process the
1726 status now */
1727 process_rcvd_status(edge_serial,
1728 edge_serial->rxHeader2, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 edge_serial->rxState = EXPECT_HDR1;
1730 break;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001731 } else {
1732 edge_serial->rxPort =
1733 IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1734 edge_serial->rxBytesRemaining =
1735 IOSP_GET_HDR_DATA_LEN(
1736 edge_serial->rxHeader1,
1737 edge_serial->rxHeader2);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001738 dev_dbg(dev, "%s - Data for Port %u Len %u\n",
1739 __func__,
1740 edge_serial->rxPort,
1741 edge_serial->rxBytesRemaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Alan Cox03f0dbf2008-07-22 11:16:34 +01001743 /* ASSERT(DevExt->RxPort < DevExt->NumPorts);
1744 * ASSERT(DevExt->RxBytesRemaining <
1745 * IOSP_MAX_DATA_LENGTH);
1746 */
1747
1748 if (bufferLength == 0) {
1749 edge_serial->rxState = EXPECT_DATA;
1750 break;
1751 }
1752 /* Else, drop through */
1753 }
1754 case EXPECT_DATA: /* Expect data */
1755 if (bufferLength < edge_serial->rxBytesRemaining) {
1756 rxLen = bufferLength;
1757 /* Expect data to start next buffer */
1758 edge_serial->rxState = EXPECT_DATA;
1759 } else {
1760 /* BufLen >= RxBytesRemaining */
1761 rxLen = edge_serial->rxBytesRemaining;
1762 /* Start another header next time */
1763 edge_serial->rxState = EXPECT_HDR1;
1764 }
1765
1766 bufferLength -= rxLen;
1767 edge_serial->rxBytesRemaining -= rxLen;
1768
1769 /* spit this data back into the tty driver if this
1770 port is open */
1771 if (rxLen) {
1772 port = edge_serial->serial->port[
1773 edge_serial->rxPort];
1774 edge_port = usb_get_serial_port_data(port);
1775 if (edge_port->open) {
Jiri Slaby2e124b42013-01-03 15:53:06 +01001776 dev_dbg(dev, "%s - Sending %d bytes to TTY for port %d\n",
1777 __func__, rxLen,
1778 edge_serial->rxPort);
1779 edge_tty_recv(edge_port->port, buffer,
1780 rxLen);
Johan Hovoldd36a7712013-03-21 12:37:07 +01001781 edge_port->port->icount.rx += rxLen;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001782 }
1783 buffer += rxLen;
1784 }
1785 break;
1786
1787 case EXPECT_HDR3: /* Expect 3rd byte of status header */
1788 edge_serial->rxHeader3 = *buffer;
1789 ++buffer;
1790 --bufferLength;
1791
1792 /* We have all the header bytes, process the
1793 status now */
1794 process_rcvd_status(edge_serial,
1795 edge_serial->rxStatusParam,
1796 edge_serial->rxHeader3);
1797 edge_serial->rxState = EXPECT_HDR1;
1798 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 }
1800 }
1801}
1802
1803
1804/*****************************************************************************
1805 * process_rcvd_status
Alan Cox03f0dbf2008-07-22 11:16:34 +01001806 * this function handles the any status messages received on the
1807 * bulk in pipe.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001809static void process_rcvd_status(struct edgeport_serial *edge_serial,
1810 __u8 byte2, __u8 byte3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811{
1812 struct usb_serial_port *port;
1813 struct edgeport_port *edge_port;
Alan Cox4a90f092008-10-13 10:39:46 +01001814 struct tty_struct *tty;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001815 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 __u8 code = edge_serial->rxStatusCode;
1817
1818 /* switch the port pointer to the one being currently talked about */
1819 port = edge_serial->serial->port[edge_serial->rxPort];
1820 edge_port = usb_get_serial_port_data(port);
1821 if (edge_port == NULL) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001822 dev_err(&edge_serial->serial->dev->dev,
1823 "%s - edge_port == NULL for port %d\n",
1824 __func__, edge_serial->rxPort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 return;
1826 }
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001827 dev = &port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
1829 if (code == IOSP_EXT_STATUS) {
1830 switch (byte2) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001831 case IOSP_EXT_STATUS_CHASE_RSP:
1832 /* we want to do EXT status regardless of port
1833 * open/closed */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001834 dev_dbg(dev, "%s - Port %u EXT CHASE_RSP Data = %02x\n",
1835 __func__, edge_serial->rxPort, byte3);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001836 /* Currently, the only EXT_STATUS is Chase, so process
1837 * here instead of one more call to one more subroutine
1838 * If/when more EXT_STATUS, there'll be more work to do
1839 * Also, we currently clear flag and close the port
1840 * regardless of content of above's Byte3.
1841 * We could choose to do something else when Byte3 says
1842 * Timeout on Chase from Edgeport, like wait longer in
1843 * block_until_chase_response, but for now we don't.
1844 */
1845 edge_port->chaseResponsePending = false;
1846 wake_up(&edge_port->wait_chase);
1847 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
Alan Cox03f0dbf2008-07-22 11:16:34 +01001849 case IOSP_EXT_STATUS_RX_CHECK_RSP:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001850 dev_dbg(dev, "%s ========== Port %u CHECK_RSP Sequence = %02x =============\n",
1851 __func__, edge_serial->rxPort, byte3);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001852 /* Port->RxCheckRsp = true; */
1853 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 }
1855 }
1856
1857 if (code == IOSP_STATUS_OPEN_RSP) {
1858 edge_port->txCredits = GET_TX_BUFFER_SIZE(byte3);
1859 edge_port->maxTxCredits = edge_port->txCredits;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001860 dev_dbg(dev, "%s - Port %u Open Response Initial MSR = %02x TxBufferSize = %d\n",
1861 __func__, edge_serial->rxPort, byte2, edge_port->txCredits);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001862 handle_new_msr(edge_port, byte2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
Alan Cox03f0dbf2008-07-22 11:16:34 +01001864 /* send the current line settings to the port so we are
1865 in sync with any further termios calls */
Alan Cox4a90f092008-10-13 10:39:46 +01001866 tty = tty_port_tty_get(&edge_port->port->port);
1867 if (tty) {
1868 change_port_settings(tty,
Alan Coxadc8d742012-07-14 15:31:47 +01001869 edge_port, &tty->termios);
Alan Cox4a90f092008-10-13 10:39:46 +01001870 tty_kref_put(tty);
1871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
1873 /* we have completed the open */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001874 edge_port->openPending = false;
1875 edge_port->open = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 wake_up(&edge_port->wait_open);
1877 return;
1878 }
1879
Alan Cox03f0dbf2008-07-22 11:16:34 +01001880 /* If port is closed, silently discard all rcvd status. We can
1881 * have cases where buffered status is received AFTER the close
1882 * port command is sent to the Edgeport.
1883 */
1884 if (!edge_port->open || edge_port->closePending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
1887 switch (code) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001888 /* Not currently sent by Edgeport */
1889 case IOSP_STATUS_LSR:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001890 dev_dbg(dev, "%s - Port %u LSR Status = %02x\n",
1891 __func__, edge_serial->rxPort, byte2);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001892 handle_new_lsr(edge_port, false, byte2, 0);
1893 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Alan Cox03f0dbf2008-07-22 11:16:34 +01001895 case IOSP_STATUS_LSR_DATA:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001896 dev_dbg(dev, "%s - Port %u LSR Status = %02x, Data = %02x\n",
1897 __func__, edge_serial->rxPort, byte2, byte3);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001898 /* byte2 is LSR Register */
1899 /* byte3 is broken data byte */
1900 handle_new_lsr(edge_port, true, byte2, byte3);
1901 break;
1902 /*
1903 * case IOSP_EXT_4_STATUS:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001904 * dev_dbg(dev, "%s - Port %u LSR Status = %02x Data = %02x\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01001905 * __func__, edge_serial->rxPort, byte2, byte3);
1906 * break;
1907 */
1908 case IOSP_STATUS_MSR:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001909 dev_dbg(dev, "%s - Port %u MSR Status = %02x\n",
1910 __func__, edge_serial->rxPort, byte2);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001911 /*
1912 * Process this new modem status and generate appropriate
1913 * events, etc, based on the new status. This routine
1914 * also saves the MSR in Port->ShadowMsr.
1915 */
1916 handle_new_msr(edge_port, byte2);
1917 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
Alan Cox03f0dbf2008-07-22 11:16:34 +01001919 default:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001920 dev_dbg(dev, "%s - Unrecognized IOSP status code %u\n", __func__, code);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001921 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923}
1924
1925
1926/*****************************************************************************
1927 * edge_tty_recv
1928 * this function passes data on to the tty flip buffer
1929 *****************************************************************************/
Jiri Slaby2e124b42013-01-03 15:53:06 +01001930static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
1931 int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932{
1933 int cnt;
1934
Jiri Slaby05c7cd32013-01-03 15:53:04 +01001935 cnt = tty_insert_flip_string(&port->port, data, length);
Alan Coxa108bfc2010-02-18 16:44:01 +00001936 if (cnt < length) {
Jiri Slaby05c7cd32013-01-03 15:53:04 +01001937 dev_err(&port->dev, "%s - dropping data, %d bytes lost\n",
Alan Coxa108bfc2010-02-18 16:44:01 +00001938 __func__, length - cnt);
1939 }
1940 data += cnt;
1941 length -= cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
Jiri Slaby2e124b42013-01-03 15:53:06 +01001943 tty_flip_buffer_push(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944}
1945
1946
1947/*****************************************************************************
1948 * handle_new_msr
1949 * this function handles any change to the msr register for a port.
1950 *****************************************************************************/
1951static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr)
1952{
1953 struct async_icount *icount;
1954
Alan Cox03f0dbf2008-07-22 11:16:34 +01001955 if (newMsr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
1956 EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
Johan Hovoldd36a7712013-03-21 12:37:07 +01001957 icount = &edge_port->port->icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
1959 /* update input line counters */
Alan Cox03f0dbf2008-07-22 11:16:34 +01001960 if (newMsr & EDGEPORT_MSR_DELTA_CTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 icount->cts++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001962 if (newMsr & EDGEPORT_MSR_DELTA_DSR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 icount->dsr++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001964 if (newMsr & EDGEPORT_MSR_DELTA_CD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 icount->dcd++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001966 if (newMsr & EDGEPORT_MSR_DELTA_RI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 icount->rng++;
Johan Hovold8b8070d2013-03-21 12:37:08 +01001968 wake_up_interruptible(&edge_port->port->port.delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 }
1970
1971 /* Save the new modem status */
1972 edge_port->shadowMSR = newMsr & 0xf0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973}
1974
1975
1976/*****************************************************************************
1977 * handle_new_lsr
1978 * this function handles any change to the lsr register for a port.
1979 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001980static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
1981 __u8 lsr, __u8 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982{
Alan Cox03f0dbf2008-07-22 11:16:34 +01001983 __u8 newLsr = (__u8) (lsr & (__u8)
1984 (LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
1985 struct async_icount *icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 edge_port->shadowLSR = lsr;
1988
1989 if (newLsr & LSR_BREAK) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001990 /*
1991 * Parity and Framing errors only count if they
1992 * occur exclusive of a break being
1993 * received.
1994 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 newLsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
1996 }
1997
1998 /* Place LSR data byte into Rx buffer */
Jiri Slaby2e124b42013-01-03 15:53:06 +01001999 if (lsrData)
2000 edge_tty_recv(edge_port->port, &data, 1);
2001
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 /* update input line counters */
Johan Hovoldd36a7712013-03-21 12:37:07 +01002003 icount = &edge_port->port->icount;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002004 if (newLsr & LSR_BREAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 icount->brk++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002006 if (newLsr & LSR_OVER_ERR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 icount->overrun++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002008 if (newLsr & LSR_PAR_ERR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 icount->parity++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002010 if (newLsr & LSR_FRM_ERR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 icount->frame++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012}
2013
2014
2015/****************************************************************************
2016 * sram_write
Alan Cox03f0dbf2008-07-22 11:16:34 +01002017 * writes a number of bytes to the Edgeport device's sram starting at the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 * given address.
2019 * If successful returns the number of bytes written, otherwise it returns
2020 * a negative error number of the problem.
2021 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002022static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
2023 __u16 length, const __u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024{
2025 int result;
2026 __u16 current_length;
2027 unsigned char *transfer_buffer;
2028
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002029 dev_dbg(&serial->dev->dev, "%s - %x, %x, %d\n", __func__, extAddr, addr, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
Alan Cox03f0dbf2008-07-22 11:16:34 +01002031 transfer_buffer = kmalloc(64, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 if (!transfer_buffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002033 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
2034 __func__, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 return -ENOMEM;
2036 }
2037
2038 /* need to split these writes up into 64 byte chunks */
2039 result = 0;
2040 while (length > 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002041 if (length > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 current_length = 64;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002043 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 current_length = length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002045
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002046/* dev_dbg(&serial->dev->dev, "%s - writing %x, %x, %d\n", __func__, extAddr, addr, current_length); */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002047 memcpy(transfer_buffer, data, current_length);
2048 result = usb_control_msg(serial->dev,
2049 usb_sndctrlpipe(serial->dev, 0),
2050 USB_REQUEST_ION_WRITE_RAM,
2051 0x40, addr, extAddr, transfer_buffer,
2052 current_length, 300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 if (result < 0)
2054 break;
2055 length -= current_length;
2056 addr += current_length;
2057 data += current_length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
Alan Cox03f0dbf2008-07-22 11:16:34 +01002060 kfree(transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 return result;
2062}
2063
2064
2065/****************************************************************************
2066 * rom_write
2067 * writes a number of bytes to the Edgeport device's ROM starting at the
2068 * given address.
2069 * If successful returns the number of bytes written, otherwise it returns
2070 * a negative error number of the problem.
2071 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002072static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
2073 __u16 length, const __u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074{
2075 int result;
2076 __u16 current_length;
2077 unsigned char *transfer_buffer;
2078
Alan Cox03f0dbf2008-07-22 11:16:34 +01002079 transfer_buffer = kmalloc(64, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 if (!transfer_buffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002081 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
2082 __func__, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 return -ENOMEM;
2084 }
2085
2086 /* need to split these writes up into 64 byte chunks */
2087 result = 0;
2088 while (length > 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002089 if (length > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 current_length = 64;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002091 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 current_length = length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002093 memcpy(transfer_buffer, data, current_length);
2094 result = usb_control_msg(serial->dev,
2095 usb_sndctrlpipe(serial->dev, 0),
2096 USB_REQUEST_ION_WRITE_ROM, 0x40,
2097 addr, extAddr,
2098 transfer_buffer, current_length, 300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 if (result < 0)
2100 break;
2101 length -= current_length;
2102 addr += current_length;
2103 data += current_length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
Alan Cox03f0dbf2008-07-22 11:16:34 +01002106 kfree(transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 return result;
2108}
2109
2110
2111/****************************************************************************
2112 * rom_read
2113 * reads a number of bytes from the Edgeport device starting at the given
2114 * address.
2115 * If successful returns the number of bytes read, otherwise it returns
2116 * a negative error number of the problem.
2117 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002118static int rom_read(struct usb_serial *serial, __u16 extAddr,
2119 __u16 addr, __u16 length, __u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
2121 int result;
2122 __u16 current_length;
2123 unsigned char *transfer_buffer;
2124
Alan Cox03f0dbf2008-07-22 11:16:34 +01002125 transfer_buffer = kmalloc(64, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 if (!transfer_buffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002127 dev_err(&serial->dev->dev,
2128 "%s - kmalloc(%d) failed.\n", __func__, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 return -ENOMEM;
2130 }
2131
2132 /* need to split these reads up into 64 byte chunks */
2133 result = 0;
2134 while (length > 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002135 if (length > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 current_length = 64;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002137 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 current_length = length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002139 result = usb_control_msg(serial->dev,
2140 usb_rcvctrlpipe(serial->dev, 0),
2141 USB_REQUEST_ION_READ_ROM,
2142 0xC0, addr, extAddr, transfer_buffer,
2143 current_length, 300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 if (result < 0)
2145 break;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002146 memcpy(data, transfer_buffer, current_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 length -= current_length;
2148 addr += current_length;
2149 data += current_length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
Alan Cox03f0dbf2008-07-22 11:16:34 +01002152 kfree(transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 return result;
2154}
2155
2156
2157/****************************************************************************
2158 * send_iosp_ext_cmd
2159 * Is used to send a IOSP message to the Edgeport device
2160 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002161static int send_iosp_ext_cmd(struct edgeport_port *edge_port,
2162 __u8 command, __u8 param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163{
2164 unsigned char *buffer;
2165 unsigned char *currentCommand;
2166 int length = 0;
2167 int status = 0;
2168
Alan Cox03f0dbf2008-07-22 11:16:34 +01002169 buffer = kmalloc(10, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 if (!buffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002171 dev_err(&edge_port->port->dev,
2172 "%s - kmalloc(%d) failed.\n", __func__, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 return -ENOMEM;
2174 }
2175
2176 currentCommand = buffer;
2177
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07002178 MAKE_CMD_EXT_CMD(&currentCommand, &length, edge_port->port->port_number,
2179 command, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
Alan Cox03f0dbf2008-07-22 11:16:34 +01002181 status = write_cmd_usb(edge_port, buffer, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 if (status) {
2183 /* something bad happened, let's free up the memory */
2184 kfree(buffer);
2185 }
2186
2187 return status;
2188}
2189
2190
2191/*****************************************************************************
2192 * write_cmd_usb
2193 * this function writes the given buffer out to the bulk write endpoint.
2194 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002195static int write_cmd_usb(struct edgeport_port *edge_port,
2196 unsigned char *buffer, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002198 struct edgeport_serial *edge_serial =
2199 usb_get_serial_data(edge_port->port->serial);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002200 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 int status = 0;
2202 struct urb *urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01002204 usb_serial_debug_data(dev, __func__, length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
2206 /* Allocate our next urb */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002207 urb = usb_alloc_urb(0, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 if (!urb)
2209 return -ENOMEM;
2210
Oliver Neukum96c706e2007-03-15 15:27:17 +01002211 atomic_inc(&CmdUrbs);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002212 dev_dbg(dev, "%s - ALLOCATE URB %p (outstanding %d)\n",
2213 __func__, urb, atomic_read(&CmdUrbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
Alan Cox03f0dbf2008-07-22 11:16:34 +01002215 usb_fill_bulk_urb(urb, edge_serial->serial->dev,
2216 usb_sndbulkpipe(edge_serial->serial->dev,
2217 edge_serial->bulk_out_endpoint),
2218 buffer, length, edge_bulk_out_cmd_callback, edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002220 edge_port->commandPending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 status = usb_submit_urb(urb, GFP_ATOMIC);
2222
2223 if (status) {
2224 /* something went wrong */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002225 dev_err(dev, "%s - usb_submit_urb(write command) failed, status = %d\n",
2226 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 usb_kill_urb(urb);
2228 usb_free_urb(urb);
Oliver Neukum96c706e2007-03-15 15:27:17 +01002229 atomic_dec(&CmdUrbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 return status;
2231 }
2232
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233#if 0
Alan Cox03f0dbf2008-07-22 11:16:34 +01002234 wait_event(&edge_port->wait_command, !edge_port->commandPending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002236 if (edge_port->commandPending) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 /* command timed out */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002238 dev_dbg(dev, "%s - command timed out\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 status = -EINVAL;
2240 }
2241#endif
2242 return status;
2243}
2244
2245
2246/*****************************************************************************
2247 * send_cmd_write_baud_rate
2248 * this function sends the proper command to change the baud rate of the
2249 * specified port.
2250 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002251static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
2252 int baudRate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002254 struct edgeport_serial *edge_serial =
2255 usb_get_serial_data(edge_port->port->serial);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002256 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 unsigned char *cmdBuffer;
2258 unsigned char *currCmd;
2259 int cmdLen = 0;
2260 int divisor;
2261 int status;
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07002262 u32 number = edge_port->port->port_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
Adam Kropelinbc71e472007-07-29 11:03:29 -04002264 if (edge_serial->is_epic &&
2265 !edge_serial->epic_descriptor.Supports.IOSPSetBaudRate) {
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07002266 dev_dbg(dev, "SendCmdWriteBaudRate - NOT Setting baud rate for port, baud = %d\n",
2267 baudRate);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002268 return 0;
2269 }
2270
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07002271 dev_dbg(dev, "%s - baud = %d\n", __func__, baudRate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002273 status = calc_baud_rate_divisor(dev, baudRate, &divisor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 if (status) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002275 dev_err(dev, "%s - bad baud rate\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 return status;
2277 }
2278
Alan Cox03f0dbf2008-07-22 11:16:34 +01002279 /* Alloc memory for the string of commands. */
2280 cmdBuffer = kmalloc(0x100, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 if (!cmdBuffer) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002282 dev_err(dev, "%s - kmalloc(%d) failed.\n", __func__, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 return -ENOMEM;
2284 }
2285 currCmd = cmdBuffer;
2286
Alan Cox03f0dbf2008-07-22 11:16:34 +01002287 /* Enable access to divisor latch */
2288 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR, LCR_DL_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
Alan Cox03f0dbf2008-07-22 11:16:34 +01002290 /* Write the divisor itself */
2291 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLL, LOW8(divisor));
2292 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLM, HIGH8(divisor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
Alan Cox03f0dbf2008-07-22 11:16:34 +01002294 /* Restore original value to disable access to divisor latch */
2295 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR,
2296 edge_port->shadowLCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
Alan Cox03f0dbf2008-07-22 11:16:34 +01002298 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 if (status) {
2300 /* something bad happened, let's free up the memory */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002301 kfree(cmdBuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 }
2303
2304 return status;
2305}
2306
2307
2308/*****************************************************************************
2309 * calc_baud_rate_divisor
2310 * this function calculates the proper baud rate divisor for the specified
2311 * baud rate.
2312 *****************************************************************************/
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002313static int calc_baud_rate_divisor(struct device *dev, int baudrate, int *divisor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314{
2315 int i;
2316 __u16 custom;
2317
Tobias Klauser52950ed2005-12-11 16:20:08 +01002318 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002319 if (divisor_table[i].BaudRate == baudrate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 *divisor = divisor_table[i].Divisor;
2321 return 0;
2322 }
2323 }
2324
Alan Cox03f0dbf2008-07-22 11:16:34 +01002325 /* We have tried all of the standard baud rates
2326 * lets try to calculate the divisor for this baud rate
2327 * Make sure the baud rate is reasonable */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 if (baudrate > 50 && baudrate < 230400) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002329 /* get divisor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 custom = (__u16)((230400L + baudrate/2) / baudrate);
2331
2332 *divisor = custom;
2333
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002334 dev_dbg(dev, "%s - Baud %d = %d\n", __func__, baudrate, custom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 return 0;
2336 }
2337
2338 return -1;
2339}
2340
2341
2342/*****************************************************************************
2343 * send_cmd_write_uart_register
Anand Gadiyarfd589a82009-07-16 17:13:03 +02002344 * this function builds up a uart register message and sends to the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002346static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
2347 __u8 regNum, __u8 regValue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002349 struct edgeport_serial *edge_serial =
2350 usb_get_serial_data(edge_port->port->serial);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002351 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 unsigned char *cmdBuffer;
2353 unsigned char *currCmd;
2354 unsigned long cmdLen = 0;
2355 int status;
2356
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002357 dev_dbg(dev, "%s - write to %s register 0x%02x\n",
2358 (regNum == MCR) ? "MCR" : "LCR", __func__, regValue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
Adam Kropelinbc71e472007-07-29 11:03:29 -04002360 if (edge_serial->is_epic &&
2361 !edge_serial->epic_descriptor.Supports.IOSPWriteMCR &&
2362 regNum == MCR) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002363 dev_dbg(dev, "SendCmdWriteUartReg - Not writing to MCR Register\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002364 return 0;
2365 }
2366
Adam Kropelinbc71e472007-07-29 11:03:29 -04002367 if (edge_serial->is_epic &&
2368 !edge_serial->epic_descriptor.Supports.IOSPWriteLCR &&
2369 regNum == LCR) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002370 dev_dbg(dev, "SendCmdWriteUartReg - Not writing to LCR Register\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002371 return 0;
2372 }
2373
Alan Cox03f0dbf2008-07-22 11:16:34 +01002374 /* Alloc memory for the string of commands. */
2375 cmdBuffer = kmalloc(0x10, GFP_ATOMIC);
2376 if (cmdBuffer == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
2379 currCmd = cmdBuffer;
2380
Alan Cox03f0dbf2008-07-22 11:16:34 +01002381 /* Build a cmd in the buffer to write the given register */
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07002382 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, edge_port->port->port_number,
2383 regNum, regValue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
2385 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
2386 if (status) {
2387 /* something bad happened, let's free up the memory */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002388 kfree(cmdBuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 }
2390
2391 return status;
2392}
2393
2394
2395/*****************************************************************************
2396 * change_port_settings
Alan Cox03f0dbf2008-07-22 11:16:34 +01002397 * This routine is called to set the UART on the device to match the
2398 * specified new settings.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01002400
2401static void change_port_settings(struct tty_struct *tty,
2402 struct edgeport_port *edge_port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002404 struct device *dev = &edge_port->port->dev;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002405 struct edgeport_serial *edge_serial =
2406 usb_get_serial_data(edge_port->port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 int baud;
2408 unsigned cflag;
2409 __u8 mask = 0xff;
2410 __u8 lData;
2411 __u8 lParity;
2412 __u8 lStop;
2413 __u8 rxFlow;
2414 __u8 txFlow;
2415 int status;
2416
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002417 if (!edge_port->open &&
2418 !edge_port->openPending) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002419 dev_dbg(dev, "%s - port not opened\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 return;
2421 }
2422
Alan Coxadc8d742012-07-14 15:31:47 +01002423 cflag = tty->termios.c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
2425 switch (cflag & CSIZE) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002426 case CS5:
2427 lData = LCR_BITS_5; mask = 0x1f;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002428 dev_dbg(dev, "%s - data bits = 5\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002429 break;
2430 case CS6:
2431 lData = LCR_BITS_6; mask = 0x3f;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002432 dev_dbg(dev, "%s - data bits = 6\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002433 break;
2434 case CS7:
2435 lData = LCR_BITS_7; mask = 0x7f;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002436 dev_dbg(dev, "%s - data bits = 7\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002437 break;
2438 default:
2439 case CS8:
2440 lData = LCR_BITS_8;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002441 dev_dbg(dev, "%s - data bits = 8\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002442 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 }
2444
2445 lParity = LCR_PAR_NONE;
2446 if (cflag & PARENB) {
2447 if (cflag & CMSPAR) {
2448 if (cflag & PARODD) {
2449 lParity = LCR_PAR_MARK;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002450 dev_dbg(dev, "%s - parity = mark\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 } else {
2452 lParity = LCR_PAR_SPACE;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002453 dev_dbg(dev, "%s - parity = space\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 }
2455 } else if (cflag & PARODD) {
2456 lParity = LCR_PAR_ODD;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002457 dev_dbg(dev, "%s - parity = odd\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 } else {
2459 lParity = LCR_PAR_EVEN;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002460 dev_dbg(dev, "%s - parity = even\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 }
2462 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002463 dev_dbg(dev, "%s - parity = none\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 }
2465
2466 if (cflag & CSTOPB) {
2467 lStop = LCR_STOP_2;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002468 dev_dbg(dev, "%s - stop bits = 2\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 } else {
2470 lStop = LCR_STOP_1;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002471 dev_dbg(dev, "%s - stop bits = 1\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 }
2473
2474 /* figure out the flow control settings */
2475 rxFlow = txFlow = 0x00;
2476 if (cflag & CRTSCTS) {
2477 rxFlow |= IOSP_RX_FLOW_RTS;
2478 txFlow |= IOSP_TX_FLOW_CTS;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002479 dev_dbg(dev, "%s - RTS/CTS is enabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002481 dev_dbg(dev, "%s - RTS/CTS is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 }
2483
Alan Cox03f0dbf2008-07-22 11:16:34 +01002484 /* if we are implementing XON/XOFF, set the start and stop character
2485 in the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 if (I_IXOFF(tty) || I_IXON(tty)) {
2487 unsigned char stop_char = STOP_CHAR(tty);
2488 unsigned char start_char = START_CHAR(tty);
2489
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002490 if ((!edge_serial->is_epic) ||
2491 ((edge_serial->is_epic) &&
2492 (edge_serial->epic_descriptor.Supports.IOSPSetXChar))) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002493 send_iosp_ext_cmd(edge_port,
2494 IOSP_CMD_SET_XON_CHAR, start_char);
2495 send_iosp_ext_cmd(edge_port,
2496 IOSP_CMD_SET_XOFF_CHAR, stop_char);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498
2499 /* if we are implementing INBOUND XON/XOFF */
2500 if (I_IXOFF(tty)) {
2501 rxFlow |= IOSP_RX_FLOW_XON_XOFF;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002502 dev_dbg(dev, "%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2503 __func__, start_char, stop_char);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002505 dev_dbg(dev, "%s - INBOUND XON/XOFF is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 }
2507
2508 /* if we are implementing OUTBOUND XON/XOFF */
2509 if (I_IXON(tty)) {
2510 txFlow |= IOSP_TX_FLOW_XON_XOFF;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002511 dev_dbg(dev, "%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2512 __func__, start_char, stop_char);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002514 dev_dbg(dev, "%s - OUTBOUND XON/XOFF is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 }
2516 }
2517
2518 /* Set flow control to the configured value */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002519 if ((!edge_serial->is_epic) ||
2520 ((edge_serial->is_epic) &&
2521 (edge_serial->epic_descriptor.Supports.IOSPSetRxFlow)))
2522 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_RX_FLOW, rxFlow);
2523 if ((!edge_serial->is_epic) ||
2524 ((edge_serial->is_epic) &&
2525 (edge_serial->epic_descriptor.Supports.IOSPSetTxFlow)))
2526 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_TX_FLOW, txFlow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
2528
2529 edge_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2530 edge_port->shadowLCR |= (lData | lParity | lStop);
2531
2532 edge_port->validDataMask = mask;
2533
2534 /* Send the updated LCR value to the EdgePort */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002535 status = send_cmd_write_uart_register(edge_port, LCR,
2536 edge_port->shadowLCR);
2537 if (status != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
2540 /* set up the MCR register and send it to the EdgePort */
2541 edge_port->shadowMCR = MCR_MASTER_IE;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002542 if (cflag & CBAUD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 edge_port->shadowMCR |= (MCR_DTR | MCR_RTS);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002544
2545 status = send_cmd_write_uart_register(edge_port, MCR,
2546 edge_port->shadowMCR);
2547 if (status != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549
2550 /* Determine divisor based on baud rate */
2551 baud = tty_get_baud_rate(tty);
2552 if (!baud) {
2553 /* pick a default, any default... */
2554 baud = 9600;
2555 }
2556
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002557 dev_dbg(dev, "%s - baud rate = %d\n", __func__, baud);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002558 status = send_cmd_write_baud_rate(edge_port, baud);
Alan Cox6ce073b2007-10-18 01:24:25 -07002559 if (status == -1) {
2560 /* Speed change was not possible - put back the old speed */
2561 baud = tty_termios_baud_rate(old_termios);
2562 tty_encode_baud_rate(tty, baud, baud);
2563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564}
2565
2566
2567/****************************************************************************
2568 * unicode_to_ascii
2569 * Turns a string from Unicode into ASCII.
2570 * Doesn't do a good job with any characters that are outside the normal
2571 * ASCII range, but it's only for debugging...
2572 * NOTE: expects the unicode in LE format
2573 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002574static void unicode_to_ascii(char *string, int buflen,
2575 __le16 *unicode, int unicode_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576{
2577 int i;
2578
Pete Zaitcevad933752006-05-22 21:49:44 -07002579 if (buflen <= 0) /* never happens, but... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 return;
Pete Zaitcevad933752006-05-22 21:49:44 -07002581 --buflen; /* space for nul */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
Pete Zaitcevad933752006-05-22 21:49:44 -07002583 for (i = 0; i < unicode_size; i++) {
2584 if (i >= buflen)
2585 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 string[i] = (char)(le16_to_cpu(unicode[i]));
Pete Zaitcevad933752006-05-22 21:49:44 -07002587 }
2588 string[i] = 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589}
2590
2591
2592/****************************************************************************
2593 * get_manufacturing_desc
Alan Cox03f0dbf2008-07-22 11:16:34 +01002594 * reads in the manufacturing descriptor and stores it into the serial
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 * structure.
2596 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002597static void get_manufacturing_desc(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002599 struct device *dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 int response;
2601
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002602 dev_dbg(dev, "getting manufacturer descriptor\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603
Alan Cox03f0dbf2008-07-22 11:16:34 +01002604 response = rom_read(edge_serial->serial,
2605 (EDGE_MANUF_DESC_ADDR & 0xffff0000) >> 16,
2606 (__u16)(EDGE_MANUF_DESC_ADDR & 0x0000ffff),
2607 EDGE_MANUF_DESC_LEN,
2608 (__u8 *)(&edge_serial->manuf_descriptor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
Alan Cox03f0dbf2008-07-22 11:16:34 +01002610 if (response < 1)
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002611 dev_err(dev, "error in getting manufacturer descriptor\n");
Alan Cox03f0dbf2008-07-22 11:16:34 +01002612 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 char string[30];
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002614 dev_dbg(dev, "**Manufacturer Descriptor\n");
2615 dev_dbg(dev, " RomSize: %dK\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002616 edge_serial->manuf_descriptor.RomSize);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002617 dev_dbg(dev, " RamSize: %dK\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002618 edge_serial->manuf_descriptor.RamSize);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002619 dev_dbg(dev, " CpuRev: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002620 edge_serial->manuf_descriptor.CpuRev);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002621 dev_dbg(dev, " BoardRev: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002622 edge_serial->manuf_descriptor.BoardRev);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002623 dev_dbg(dev, " NumPorts: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002624 edge_serial->manuf_descriptor.NumPorts);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002625 dev_dbg(dev, " DescDate: %d/%d/%d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002626 edge_serial->manuf_descriptor.DescDate[0],
2627 edge_serial->manuf_descriptor.DescDate[1],
2628 edge_serial->manuf_descriptor.DescDate[2]+1900);
Pete Zaitcev4bc203d2006-06-06 18:18:33 -07002629 unicode_to_ascii(string, sizeof(string),
Alan Cox03f0dbf2008-07-22 11:16:34 +01002630 edge_serial->manuf_descriptor.SerialNumber,
2631 edge_serial->manuf_descriptor.SerNumLength/2);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002632 dev_dbg(dev, " SerialNumber: %s\n", string);
Pete Zaitcev4bc203d2006-06-06 18:18:33 -07002633 unicode_to_ascii(string, sizeof(string),
Alan Cox03f0dbf2008-07-22 11:16:34 +01002634 edge_serial->manuf_descriptor.AssemblyNumber,
2635 edge_serial->manuf_descriptor.AssemblyNumLength/2);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002636 dev_dbg(dev, " AssemblyNumber: %s\n", string);
Pete Zaitcev4bc203d2006-06-06 18:18:33 -07002637 unicode_to_ascii(string, sizeof(string),
Pete Zaitcevad933752006-05-22 21:49:44 -07002638 edge_serial->manuf_descriptor.OemAssyNumber,
2639 edge_serial->manuf_descriptor.OemAssyNumLength/2);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002640 dev_dbg(dev, " OemAssyNumber: %s\n", string);
2641 dev_dbg(dev, " UartType: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002642 edge_serial->manuf_descriptor.UartType);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002643 dev_dbg(dev, " IonPid: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002644 edge_serial->manuf_descriptor.IonPid);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002645 dev_dbg(dev, " IonConfig: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002646 edge_serial->manuf_descriptor.IonConfig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 }
2648}
2649
2650
2651/****************************************************************************
2652 * get_boot_desc
Alan Cox03f0dbf2008-07-22 11:16:34 +01002653 * reads in the bootloader descriptor and stores it into the serial
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 * structure.
2655 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002656static void get_boot_desc(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002658 struct device *dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 int response;
2660
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002661 dev_dbg(dev, "getting boot descriptor\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662
Alan Cox03f0dbf2008-07-22 11:16:34 +01002663 response = rom_read(edge_serial->serial,
2664 (EDGE_BOOT_DESC_ADDR & 0xffff0000) >> 16,
2665 (__u16)(EDGE_BOOT_DESC_ADDR & 0x0000ffff),
2666 EDGE_BOOT_DESC_LEN,
2667 (__u8 *)(&edge_serial->boot_descriptor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668
Alan Cox03f0dbf2008-07-22 11:16:34 +01002669 if (response < 1)
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002670 dev_err(dev, "error in getting boot descriptor\n");
Alan Cox03f0dbf2008-07-22 11:16:34 +01002671 else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002672 dev_dbg(dev, "**Boot Descriptor:\n");
2673 dev_dbg(dev, " BootCodeLength: %d\n",
2674 le16_to_cpu(edge_serial->boot_descriptor.BootCodeLength));
2675 dev_dbg(dev, " MajorVersion: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002676 edge_serial->boot_descriptor.MajorVersion);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002677 dev_dbg(dev, " MinorVersion: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002678 edge_serial->boot_descriptor.MinorVersion);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002679 dev_dbg(dev, " BuildNumber: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002680 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002681 dev_dbg(dev, " Capabilities: 0x%x\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002682 le16_to_cpu(edge_serial->boot_descriptor.Capabilities));
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002683 dev_dbg(dev, " UConfig0: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002684 edge_serial->boot_descriptor.UConfig0);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002685 dev_dbg(dev, " UConfig1: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002686 edge_serial->boot_descriptor.UConfig1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 }
2688}
2689
2690
2691/****************************************************************************
2692 * load_application_firmware
2693 * This is called to load the application firmware to the device
2694 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002695static void load_application_firmware(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002697 struct device *dev = &edge_serial->serial->dev->dev;
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302698 const struct ihex_binrec *rec;
2699 const struct firmware *fw;
2700 const char *fw_name;
2701 const char *fw_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 int response;
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302703 __u32 Operaddr;
2704 __u16 build;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705
2706 switch (edge_serial->product_info.iDownloadFile) {
2707 case EDGE_DOWNLOAD_FILE_I930:
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302708 fw_info = "downloading firmware version (930)";
2709 fw_name = "edgeport/down.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 break;
2711
2712 case EDGE_DOWNLOAD_FILE_80251:
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302713 fw_info = "downloading firmware version (80251)";
2714 fw_name = "edgeport/down2.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 break;
2716
2717 case EDGE_DOWNLOAD_FILE_NONE:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002718 dev_dbg(dev, "No download file specified, skipping download\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 return;
2720
2721 default:
2722 return;
2723 }
2724
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302725 response = request_ihex_firmware(&fw, fw_name,
2726 &edge_serial->serial->dev->dev);
2727 if (response) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002728 dev_err(dev, "Failed to load image \"%s\" err %d\n",
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302729 fw_name, response);
2730 return;
2731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302733 rec = (const struct ihex_binrec *)fw->data;
2734 build = (rec->data[2] << 8) | rec->data[3];
2735
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002736 dev_dbg(dev, "%s %d.%d.%d\n", fw_info, rec->data[0], rec->data[1], build);
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302737
Bjørn Mork271c1152011-01-17 14:19:37 +01002738 edge_serial->product_info.FirmwareMajorVersion = rec->data[0];
2739 edge_serial->product_info.FirmwareMinorVersion = rec->data[1];
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302740 edge_serial->product_info.FirmwareBuildNumber = cpu_to_le16(build);
2741
2742 for (rec = ihex_next_binrec(rec); rec;
2743 rec = ihex_next_binrec(rec)) {
2744 Operaddr = be32_to_cpu(rec->addr);
2745 response = sram_write(edge_serial->serial,
2746 Operaddr >> 16,
2747 Operaddr & 0xFFFF,
2748 be16_to_cpu(rec->len),
2749 &rec->data[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 if (response < 0) {
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302751 dev_err(&edge_serial->serial->dev->dev,
2752 "sram_write failed (%x, %x, %d)\n",
2753 Operaddr >> 16, Operaddr & 0xFFFF,
2754 be16_to_cpu(rec->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 break;
2756 }
2757 }
2758
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002759 dev_dbg(dev, "sending exec_dl_code\n");
2760 response = usb_control_msg (edge_serial->serial->dev,
2761 usb_sndctrlpipe(edge_serial->serial->dev, 0),
2762 USB_REQUEST_ION_EXEC_DL_CODE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 0x40, 0x4000, 0x0001, NULL, 0, 3000);
2764
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302765 release_firmware(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766}
2767
2768
2769/****************************************************************************
2770 * edge_startup
2771 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002772static int edge_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773{
2774 struct edgeport_serial *edge_serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 struct usb_device *dev;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002776 struct device *ddev = &serial->dev->dev;
Johan Hovoldc27f3ef2012-10-17 13:34:57 +02002777 int i;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002778 int response;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002779 bool interrupt_in_found;
2780 bool bulk_in_found;
2781 bool bulk_out_found;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002782 static __u32 descriptor[3] = { EDGE_COMPATIBILITY_MASK0,
2783 EDGE_COMPATIBILITY_MASK1,
2784 EDGE_COMPATIBILITY_MASK2 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
2786 dev = serial->dev;
2787
2788 /* create our private serial structure */
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +01002789 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 if (edge_serial == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002791 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 return -ENOMEM;
2793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 spin_lock_init(&edge_serial->es_lock);
2795 edge_serial->serial = serial;
2796 usb_set_serial_data(serial, edge_serial);
2797
2798 /* get the name for the device from the device */
Dan Carpenterf10718f2010-01-25 14:53:33 +03002799 i = usb_string(dev, dev->descriptor.iManufacturer,
Pete Zaitcevad933752006-05-22 21:49:44 -07002800 &edge_serial->name[0], MAX_NAME_LEN+1);
Dan Carpenterf10718f2010-01-25 14:53:33 +03002801 if (i < 0)
2802 i = 0;
Pete Zaitcevad933752006-05-22 21:49:44 -07002803 edge_serial->name[i++] = ' ';
Dan Carpenterf10718f2010-01-25 14:53:33 +03002804 usb_string(dev, dev->descriptor.iProduct,
Pete Zaitcevad933752006-05-22 21:49:44 -07002805 &edge_serial->name[i], MAX_NAME_LEN+2 - i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806
2807 dev_info(&serial->dev->dev, "%s detected\n", edge_serial->name);
2808
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002809 /* Read the epic descriptor */
2810 if (get_epic_descriptor(edge_serial) <= 0) {
2811 /* memcpy descriptor to Supports structures */
2812 memcpy(&edge_serial->epic_descriptor.Supports, descriptor,
2813 sizeof(struct edge_compatibility_bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002815 /* get the manufacturing descriptor for this device */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002816 get_manufacturing_desc(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002818 /* get the boot descriptor */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002819 get_boot_desc(edge_serial);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002820
2821 get_product_info(edge_serial);
2822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
2824 /* set the number of ports from the manufacturing description */
2825 /* serial->num_ports = serial->product_info.NumPorts; */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002826 if ((!edge_serial->is_epic) &&
2827 (edge_serial->product_info.NumPorts != serial->num_ports)) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002828 dev_warn(ddev,
2829 "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 -08002830 edge_serial->product_info.NumPorts,
2831 serial->num_ports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 }
2833
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002834 dev_dbg(ddev, "%s - time 1 %ld\n", __func__, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002836 /* If not an EPiC device */
2837 if (!edge_serial->is_epic) {
2838 /* now load the application firmware into this device */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002839 load_application_firmware(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002841 dev_dbg(ddev, "%s - time 2 %ld\n", __func__, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002843 /* Check current Edgeport EEPROM and update if necessary */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002844 update_edgeport_E2PROM(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002846 dev_dbg(ddev, "%s - time 3 %ld\n", __func__, jiffies);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002847
2848 /* set the configuration to use #1 */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002849/* dev_dbg(ddev, "set_configuration 1\n"); */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002850/* usb_set_configuration (dev, 1); */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002851 }
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002852 dev_dbg(ddev, " FirmwareMajorVersion %d.%d.%d\n",
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302853 edge_serial->product_info.FirmwareMajorVersion,
2854 edge_serial->product_info.FirmwareMinorVersion,
2855 le16_to_cpu(edge_serial->product_info.FirmwareBuildNumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856
Alan Cox03f0dbf2008-07-22 11:16:34 +01002857 /* we set up the pointers to the endpoints in the edge_open function,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 * as the structures aren't created yet. */
2859
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002860 response = 0;
2861
2862 if (edge_serial->is_epic) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002863 /* EPIC thing, set up our interrupt polling now and our read
2864 * urb, so that the device knows it really is connected. */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002865 interrupt_in_found = bulk_in_found = bulk_out_found = false;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002866 for (i = 0; i < serial->interface->altsetting[0]
2867 .desc.bNumEndpoints; ++i) {
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002868 struct usb_endpoint_descriptor *endpoint;
2869 int buffer_size;
2870
Alan Cox03f0dbf2008-07-22 11:16:34 +01002871 endpoint = &serial->interface->altsetting[0].
2872 endpoint[i].desc;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07002873 buffer_size = usb_endpoint_maxp(endpoint);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002874 if (!interrupt_in_found &&
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002875 (usb_endpoint_is_int_in(endpoint))) {
2876 /* we found a interrupt in endpoint */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002877 dev_dbg(ddev, "found interrupt in\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002878
2879 /* not set up yet, so do it now */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002880 edge_serial->interrupt_read_urb =
2881 usb_alloc_urb(0, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002882 if (!edge_serial->interrupt_read_urb) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002883 dev_err(ddev, "out of memory\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002884 return -ENOMEM;
2885 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01002886 edge_serial->interrupt_in_buffer =
2887 kmalloc(buffer_size, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002888 if (!edge_serial->interrupt_in_buffer) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002889 dev_err(ddev, "out of memory\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002890 usb_free_urb(edge_serial->interrupt_read_urb);
2891 return -ENOMEM;
2892 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01002893 edge_serial->interrupt_in_endpoint =
2894 endpoint->bEndpointAddress;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002895
2896 /* set up our interrupt urb */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002897 usb_fill_int_urb(
2898 edge_serial->interrupt_read_urb,
2899 dev,
2900 usb_rcvintpipe(dev,
2901 endpoint->bEndpointAddress),
2902 edge_serial->interrupt_in_buffer,
2903 buffer_size,
2904 edge_interrupt_callback,
2905 edge_serial,
2906 endpoint->bInterval);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002907
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002908 interrupt_in_found = true;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002909 }
2910
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002911 if (!bulk_in_found &&
Alan Cox03f0dbf2008-07-22 11:16:34 +01002912 (usb_endpoint_is_bulk_in(endpoint))) {
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002913 /* we found a bulk in endpoint */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002914 dev_dbg(ddev, "found bulk in\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002915
2916 /* not set up yet, so do it now */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002917 edge_serial->read_urb =
2918 usb_alloc_urb(0, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002919 if (!edge_serial->read_urb) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002920 dev_err(ddev, "out of memory\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002921 return -ENOMEM;
2922 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01002923 edge_serial->bulk_in_buffer =
2924 kmalloc(buffer_size, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002925 if (!edge_serial->bulk_in_buffer) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07002926 dev_err(&dev->dev, "out of memory\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002927 usb_free_urb(edge_serial->read_urb);
2928 return -ENOMEM;
2929 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01002930 edge_serial->bulk_in_endpoint =
2931 endpoint->bEndpointAddress;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002932
2933 /* set up our bulk in urb */
2934 usb_fill_bulk_urb(edge_serial->read_urb, dev,
Alan Cox03f0dbf2008-07-22 11:16:34 +01002935 usb_rcvbulkpipe(dev,
2936 endpoint->bEndpointAddress),
2937 edge_serial->bulk_in_buffer,
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07002938 usb_endpoint_maxp(endpoint),
Alan Cox03f0dbf2008-07-22 11:16:34 +01002939 edge_bulk_in_callback,
2940 edge_serial);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002941 bulk_in_found = true;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002942 }
2943
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002944 if (!bulk_out_found &&
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002945 (usb_endpoint_is_bulk_out(endpoint))) {
2946 /* we found a bulk out endpoint */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002947 dev_dbg(ddev, "found bulk out\n");
Alan Cox03f0dbf2008-07-22 11:16:34 +01002948 edge_serial->bulk_out_endpoint =
2949 endpoint->bEndpointAddress;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002950 bulk_out_found = true;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002951 }
2952 }
2953
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002954 if (!interrupt_in_found || !bulk_in_found || !bulk_out_found) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002955 dev_err(ddev, "Error - the proper endpoints were not found!\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002956 return -ENODEV;
2957 }
2958
2959 /* start interrupt read for this edgeport this interrupt will
2960 * continue as long as the edgeport is connected */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002961 response = usb_submit_urb(edge_serial->interrupt_read_urb,
2962 GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002963 if (response)
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002964 dev_err(ddev, "%s - Error %d submitting control urb\n",
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07002965 __func__, response);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002966 }
2967 return response;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968}
2969
2970
2971/****************************************************************************
Alan Sternf9c99bb2009-06-02 11:53:55 -04002972 * edge_disconnect
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 * This function is called whenever the device is removed from the usb bus.
2974 ****************************************************************************/
Alan Sternf9c99bb2009-06-02 11:53:55 -04002975static void edge_disconnect(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976{
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002977 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 /* stop reads and writes on all ports */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002980 /* free up our endpoint stuff */
2981 if (edge_serial->is_epic) {
Oliver Neukum74ac07e2007-06-13 18:50:41 +02002982 usb_kill_urb(edge_serial->interrupt_read_urb);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002983 usb_free_urb(edge_serial->interrupt_read_urb);
2984 kfree(edge_serial->interrupt_in_buffer);
2985
Oliver Neukum74ac07e2007-06-13 18:50:41 +02002986 usb_kill_urb(edge_serial->read_urb);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002987 usb_free_urb(edge_serial->read_urb);
2988 kfree(edge_serial->bulk_in_buffer);
2989 }
Alan Sternf9c99bb2009-06-02 11:53:55 -04002990}
2991
2992
2993/****************************************************************************
2994 * edge_release
2995 * This function is called when the device structure is deallocated.
2996 ****************************************************************************/
2997static void edge_release(struct usb_serial *serial)
2998{
2999 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003000
3001 kfree(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002}
3003
Johan Hovoldc27f3ef2012-10-17 13:34:57 +02003004static int edge_port_probe(struct usb_serial_port *port)
3005{
3006 struct edgeport_port *edge_port;
3007
3008 edge_port = kzalloc(sizeof(*edge_port), GFP_KERNEL);
3009 if (!edge_port)
3010 return -ENOMEM;
3011
3012 spin_lock_init(&edge_port->ep_lock);
3013 edge_port->port = port;
3014
3015 usb_set_serial_port_data(port, edge_port);
3016
3017 return 0;
3018}
3019
3020static int edge_port_remove(struct usb_serial_port *port)
3021{
3022 struct edgeport_port *edge_port;
3023
3024 edge_port = usb_get_serial_port_data(port);
3025 kfree(edge_port);
3026
3027 return 0;
3028}
3029
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07003030module_usb_serial_driver(serial_drivers, id_table_combined);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
Alan Cox03f0dbf2008-07-22 11:16:34 +01003032MODULE_AUTHOR(DRIVER_AUTHOR);
3033MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034MODULE_LICENSE("GPL");
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05303035MODULE_FIRMWARE("edgeport/boot.fw");
3036MODULE_FIRMWARE("edgeport/boot2.fw");
3037MODULE_FIRMWARE("edgeport/down.fw");
3038MODULE_FIRMWARE("edgeport/down2.fw");