blob: efd8b978128c1d86412322f0c37e9da32b00610a [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
114 struct async_icount icount;
115 struct usb_serial_port *port; /* loop back to the owner of this object */
116};
117
118
119/* This structure holds all of the individual device information */
120struct edgeport_serial {
Pete Zaitcevad933752006-05-22 21:49:44 -0700121 char name[MAX_NAME_LEN+2]; /* string name of this device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 struct edge_manuf_descriptor manuf_descriptor; /* the manufacturer descriptor */
124 struct edge_boot_descriptor boot_descriptor; /* the boot firmware descriptor */
125 struct edgeport_product_info product_info; /* Product Info */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800126 struct edge_compatibility_descriptor epic_descriptor; /* Edgeport compatible descriptor */
127 int is_epic; /* flag if EPiC device or not */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 __u8 interrupt_in_endpoint; /* the interrupt endpoint handle */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100130 unsigned char *interrupt_in_buffer; /* the buffer we use for the interrupt endpoint */
131 struct urb *interrupt_read_urb; /* our interrupt urb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 __u8 bulk_in_endpoint; /* the bulk in endpoint handle */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100134 unsigned char *bulk_in_buffer; /* the buffer we use for the bulk in endpoint */
135 struct urb *read_urb; /* our bulk read urb */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100136 bool read_in_progress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 spinlock_t es_lock;
138
139 __u8 bulk_out_endpoint; /* the bulk out endpoint handle */
140
141 __s16 rxBytesAvail; /* the number of bytes that we need to read from this device */
142
143 enum RXSTATE rxState; /* the current state of the bulk receive processor */
144 __u8 rxHeader1; /* receive header byte 1 */
145 __u8 rxHeader2; /* receive header byte 2 */
146 __u8 rxHeader3; /* receive header byte 3 */
147 __u8 rxPort; /* the port that we are currently receiving data for */
148 __u8 rxStatusCode; /* the receive status code */
149 __u8 rxStatusParam; /* the receive status paramater */
150 __s16 rxBytesRemaining; /* the number of port bytes left to read */
151 struct usb_serial *serial; /* loop back to the owner of this object */
152};
153
154/* baud rate information */
155struct divisor_table_entry {
156 __u32 BaudRate;
157 __u16 Divisor;
158};
159
Alan Cox03f0dbf2008-07-22 11:16:34 +0100160/*
161 * Define table of divisors for Rev A EdgePort/4 hardware
162 * These assume a 3.6864MHz crystal, the standard /16, and
163 * MCR.7 = 0.
164 */
165
Arjan van de Ven4c4c9432005-11-29 09:43:42 +0100166static const struct divisor_table_entry divisor_table[] = {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100167 { 50, 4608},
168 { 75, 3072},
169 { 110, 2095}, /* 2094.545455 => 230450 => .0217 % over */
170 { 134, 1713}, /* 1713.011152 => 230398.5 => .00065% under */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 { 150, 1536},
172 { 300, 768},
173 { 600, 384},
174 { 1200, 192},
175 { 1800, 128},
176 { 2400, 96},
177 { 4800, 48},
178 { 7200, 32},
179 { 9600, 24},
180 { 14400, 16},
181 { 19200, 12},
182 { 38400, 6},
183 { 57600, 4},
184 { 115200, 2},
185 { 230400, 1},
186};
187
Greg Kroah-Hartman36ccce42012-02-28 13:11:51 -0800188/* Number of outstanding Command Write Urbs */
189static atomic_t CmdUrbs = ATOMIC_INIT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191
192/* local function prototypes */
193
194/* function prototypes for all URB callbacks */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100195static void edge_interrupt_callback(struct urb *urb);
196static void edge_bulk_in_callback(struct urb *urb);
197static void edge_bulk_out_data_callback(struct urb *urb);
198static void edge_bulk_out_cmd_callback(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200/* function prototypes for the usbserial callbacks */
Alan Coxa509a7e2009-09-19 13:13:26 -0700201static int edge_open(struct tty_struct *tty, struct usb_serial_port *port);
Alan Cox335f8512009-06-11 12:26:29 +0100202static void edge_close(struct usb_serial_port *port);
Alan Cox03f0dbf2008-07-22 11:16:34 +0100203static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
204 const unsigned char *buf, int count);
205static int edge_write_room(struct tty_struct *tty);
206static int edge_chars_in_buffer(struct tty_struct *tty);
207static void edge_throttle(struct tty_struct *tty);
208static void edge_unthrottle(struct tty_struct *tty);
209static void edge_set_termios(struct tty_struct *tty,
210 struct usb_serial_port *port,
211 struct ktermios *old_termios);
Alan Cox00a0d0d2011-02-14 16:27:06 +0000212static int edge_ioctl(struct tty_struct *tty,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100213 unsigned int cmd, unsigned long arg);
214static void edge_break(struct tty_struct *tty, int break_state);
Alan Cox60b33c12011-02-14 16:26:14 +0000215static int edge_tiocmget(struct tty_struct *tty);
Alan Cox20b9d172011-02-14 16:26:50 +0000216static int edge_tiocmset(struct tty_struct *tty,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100217 unsigned int set, unsigned int clear);
Alan Cox0bca1b92010-09-16 18:21:40 +0100218static int edge_get_icount(struct tty_struct *tty,
219 struct serial_icounter_struct *icount);
Alan Cox03f0dbf2008-07-22 11:16:34 +0100220static int edge_startup(struct usb_serial *serial);
Alan Sternf9c99bb2009-06-02 11:53:55 -0400221static void edge_disconnect(struct usb_serial *serial);
222static void edge_release(struct usb_serial *serial);
Johan Hovoldc27f3ef2012-10-17 13:34:57 +0200223static int edge_port_probe(struct usb_serial_port *port);
224static int edge_port_remove(struct usb_serial_port *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226#include "io_tables.h" /* all of the devices that this driver supports */
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228/* function prototypes for all of our local functions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Alan Cox03f0dbf2008-07-22 11:16:34 +0100230static void process_rcvd_data(struct edgeport_serial *edge_serial,
231 unsigned char *buffer, __u16 bufferLength);
232static void process_rcvd_status(struct edgeport_serial *edge_serial,
233 __u8 byte2, __u8 byte3);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100234static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
235 int length);
Alan Cox03f0dbf2008-07-22 11:16:34 +0100236static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr);
237static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
238 __u8 lsr, __u8 data);
239static int send_iosp_ext_cmd(struct edgeport_port *edge_port, __u8 command,
240 __u8 param);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700241static int calc_baud_rate_divisor(struct device *dev, int baud_rate, int *divisor);
Alan Cox03f0dbf2008-07-22 11:16:34 +0100242static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
243 int baudRate);
244static void change_port_settings(struct tty_struct *tty,
245 struct edgeport_port *edge_port,
246 struct ktermios *old_termios);
247static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
248 __u8 regNum, __u8 regValue);
249static int write_cmd_usb(struct edgeport_port *edge_port,
250 unsigned char *buffer, int writeLength);
251static void send_more_port_data(struct edgeport_serial *edge_serial,
252 struct edgeport_port *edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Alan Cox03f0dbf2008-07-22 11:16:34 +0100254static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
255 __u16 length, const __u8 *data);
256static int rom_read(struct usb_serial *serial, __u16 extAddr, __u16 addr,
257 __u16 length, __u8 *data);
258static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
259 __u16 length, const __u8 *data);
260static void get_manufacturing_desc(struct edgeport_serial *edge_serial);
261static void get_boot_desc(struct edgeport_serial *edge_serial);
262static void load_application_firmware(struct edgeport_serial *edge_serial);
263
264static void unicode_to_ascii(char *string, int buflen,
265 __le16 *unicode, int unicode_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267
Alan Cox03f0dbf2008-07-22 11:16:34 +0100268/* ************************************************************************ */
269/* ************************************************************************ */
270/* ************************************************************************ */
271/* ************************************************************************ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273/************************************************************************
274 * *
275 * update_edgeport_E2PROM() Compare current versions of *
276 * Boot ROM and Manufacture *
277 * Descriptors with versions *
278 * embedded in this driver *
279 * *
280 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100281static void update_edgeport_E2PROM(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700283 struct device *dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 __u32 BootCurVer;
285 __u32 BootNewVer;
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530286 __u8 BootMajorVersion;
287 __u8 BootMinorVersion;
288 __u16 BootBuildNumber;
289 __u32 Bootaddr;
290 const struct ihex_binrec *rec;
291 const struct firmware *fw;
292 const char *fw_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 int response;
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 switch (edge_serial->product_info.iDownloadFile) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100296 case EDGE_DOWNLOAD_FILE_I930:
297 fw_name = "edgeport/boot.fw";
298 break;
299 case EDGE_DOWNLOAD_FILE_80251:
300 fw_name = "edgeport/boot2.fw";
301 break;
302 default:
303 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
305
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530306 response = request_ihex_firmware(&fw, fw_name,
307 &edge_serial->serial->dev->dev);
308 if (response) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700309 dev_err(dev, "Failed to load image \"%s\" err %d\n",
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530310 fw_name, response);
311 return;
312 }
313
314 rec = (const struct ihex_binrec *)fw->data;
315 BootMajorVersion = rec->data[0];
316 BootMinorVersion = rec->data[1];
317 BootBuildNumber = (rec->data[2] << 8) | rec->data[3];
318
Alan Cox03f0dbf2008-07-22 11:16:34 +0100319 /* Check Boot Image Version */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 BootCurVer = (edge_serial->boot_descriptor.MajorVersion << 24) +
321 (edge_serial->boot_descriptor.MinorVersion << 16) +
322 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber);
323
324 BootNewVer = (BootMajorVersion << 24) +
325 (BootMinorVersion << 16) +
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530326 BootBuildNumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700328 dev_dbg(dev, "Current Boot Image version %d.%d.%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 edge_serial->boot_descriptor.MajorVersion,
330 edge_serial->boot_descriptor.MinorVersion,
331 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
332
333
334 if (BootNewVer > BootCurVer) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700335 dev_dbg(dev, "**Update Boot Image from %d.%d.%d to %d.%d.%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 edge_serial->boot_descriptor.MajorVersion,
337 edge_serial->boot_descriptor.MinorVersion,
338 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber),
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530339 BootMajorVersion, BootMinorVersion, BootBuildNumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700341 dev_dbg(dev, "Downloading new Boot Image\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530343 for (rec = ihex_next_binrec(rec); rec;
344 rec = ihex_next_binrec(rec)) {
345 Bootaddr = be32_to_cpu(rec->addr);
346 response = rom_write(edge_serial->serial,
347 Bootaddr >> 16,
348 Bootaddr & 0xFFFF,
349 be16_to_cpu(rec->len),
350 &rec->data[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (response < 0) {
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530352 dev_err(&edge_serial->serial->dev->dev,
353 "rom_write failed (%x, %x, %d)\n",
354 Bootaddr >> 16, Bootaddr & 0xFFFF,
355 be16_to_cpu(rec->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 break;
357 }
358 }
359 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700360 dev_dbg(dev, "Boot Image -- already up to date\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530362 release_firmware(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365#if 0
366/************************************************************************
367 *
368 * Get string descriptor from device
369 *
370 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100371static int get_string_desc(struct usb_device *dev, int Id,
372 struct usb_string_descriptor **pRetDesc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
374 struct usb_string_descriptor StringDesc;
375 struct usb_string_descriptor *pStringDesc;
376
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700377 dev_dbg(&dev->dev, "%s - USB String ID = %d\n", __func__, Id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Alan Cox03f0dbf2008-07-22 11:16:34 +0100379 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc,
380 sizeof(StringDesc)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Alan Cox03f0dbf2008-07-22 11:16:34 +0100383 pStringDesc = kmalloc(StringDesc.bLength, GFP_KERNEL);
384 if (!pStringDesc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Alan Cox03f0dbf2008-07-22 11:16:34 +0100387 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc,
388 StringDesc.bLength)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 kfree(pStringDesc);
390 return -1;
391 }
392
393 *pRetDesc = pStringDesc;
394 return 0;
395}
396#endif
397
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700398static void dump_product_info(struct edgeport_serial *edge_serial,
399 struct edgeport_product_info *product_info)
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800400{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700401 struct device *dev = &edge_serial->serial->dev->dev;
402
Alan Cox03f0dbf2008-07-22 11:16:34 +0100403 /* Dump Product Info structure */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700404 dev_dbg(dev, "**Product Information:\n");
405 dev_dbg(dev, " ProductId %x\n", product_info->ProductId);
406 dev_dbg(dev, " NumPorts %d\n", product_info->NumPorts);
407 dev_dbg(dev, " ProdInfoVer %d\n", product_info->ProdInfoVer);
408 dev_dbg(dev, " IsServer %d\n", product_info->IsServer);
409 dev_dbg(dev, " IsRS232 %d\n", product_info->IsRS232);
410 dev_dbg(dev, " IsRS422 %d\n", product_info->IsRS422);
411 dev_dbg(dev, " IsRS485 %d\n", product_info->IsRS485);
412 dev_dbg(dev, " RomSize %d\n", product_info->RomSize);
413 dev_dbg(dev, " RamSize %d\n", product_info->RamSize);
414 dev_dbg(dev, " CpuRev %x\n", product_info->CpuRev);
415 dev_dbg(dev, " BoardRev %x\n", product_info->BoardRev);
416 dev_dbg(dev, " BootMajorVersion %d.%d.%d\n",
417 product_info->BootMajorVersion,
418 product_info->BootMinorVersion,
419 le16_to_cpu(product_info->BootBuildNumber));
420 dev_dbg(dev, " FirmwareMajorVersion %d.%d.%d\n",
421 product_info->FirmwareMajorVersion,
422 product_info->FirmwareMinorVersion,
423 le16_to_cpu(product_info->FirmwareBuildNumber));
424 dev_dbg(dev, " ManufactureDescDate %d/%d/%d\n",
425 product_info->ManufactureDescDate[0],
426 product_info->ManufactureDescDate[1],
427 product_info->ManufactureDescDate[2]+1900);
428 dev_dbg(dev, " iDownloadFile 0x%x\n",
429 product_info->iDownloadFile);
430 dev_dbg(dev, " EpicVer %d\n", product_info->EpicVer);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800431}
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433static void get_product_info(struct edgeport_serial *edge_serial)
434{
435 struct edgeport_product_info *product_info = &edge_serial->product_info;
436
Alan Cox03f0dbf2008-07-22 11:16:34 +0100437 memset(product_info, 0, sizeof(struct edgeport_product_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Alan Cox03f0dbf2008-07-22 11:16:34 +0100439 product_info->ProductId = (__u16)(le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct) & ~ION_DEVICE_ID_80251_NETCHIP);
440 product_info->NumPorts = edge_serial->manuf_descriptor.NumPorts;
441 product_info->ProdInfoVer = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Alan Cox03f0dbf2008-07-22 11:16:34 +0100443 product_info->RomSize = edge_serial->manuf_descriptor.RomSize;
444 product_info->RamSize = edge_serial->manuf_descriptor.RamSize;
445 product_info->CpuRev = edge_serial->manuf_descriptor.CpuRev;
446 product_info->BoardRev = edge_serial->manuf_descriptor.BoardRev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Alan Cox03f0dbf2008-07-22 11:16:34 +0100448 product_info->BootMajorVersion =
449 edge_serial->boot_descriptor.MajorVersion;
450 product_info->BootMinorVersion =
451 edge_serial->boot_descriptor.MinorVersion;
452 product_info->BootBuildNumber =
453 edge_serial->boot_descriptor.BuildNumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Alan Cox03f0dbf2008-07-22 11:16:34 +0100455 memcpy(product_info->ManufactureDescDate,
456 edge_serial->manuf_descriptor.DescDate,
457 sizeof(edge_serial->manuf_descriptor.DescDate));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Alan Cox03f0dbf2008-07-22 11:16:34 +0100459 /* check if this is 2nd generation hardware */
460 if (le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct)
461 & ION_DEVICE_ID_80251_NETCHIP)
462 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_80251;
463 else
464 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_I930;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700465
Alan Cox03f0dbf2008-07-22 11:16:34 +0100466 /* Determine Product type and set appropriate flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 switch (DEVICE_ID_FROM_USB_PRODUCT_ID(product_info->ProductId)) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100468 case ION_DEVICE_ID_EDGEPORT_COMPATIBLE:
469 case ION_DEVICE_ID_EDGEPORT_4T:
470 case ION_DEVICE_ID_EDGEPORT_4:
471 case ION_DEVICE_ID_EDGEPORT_2:
472 case ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU:
473 case ION_DEVICE_ID_EDGEPORT_8:
474 case ION_DEVICE_ID_EDGEPORT_421:
475 case ION_DEVICE_ID_EDGEPORT_21:
476 case ION_DEVICE_ID_EDGEPORT_2_DIN:
477 case ION_DEVICE_ID_EDGEPORT_4_DIN:
478 case ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU:
479 product_info->IsRS232 = 1;
480 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Alan Cox03f0dbf2008-07-22 11:16:34 +0100482 case ION_DEVICE_ID_EDGEPORT_2I: /* Edgeport/2 RS422/RS485 */
483 product_info->IsRS422 = 1;
484 product_info->IsRS485 = 1;
485 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Alan Cox03f0dbf2008-07-22 11:16:34 +0100487 case ION_DEVICE_ID_EDGEPORT_8I: /* Edgeport/4 RS422 */
488 case ION_DEVICE_ID_EDGEPORT_4I: /* Edgeport/4 RS422 */
489 product_info->IsRS422 = 1;
490 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
492
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700493 dump_product_info(edge_serial, product_info);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800494}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800496static int get_epic_descriptor(struct edgeport_serial *ep)
497{
498 int result;
499 struct usb_serial *serial = ep->serial;
500 struct edgeport_product_info *product_info = &ep->product_info;
501 struct edge_compatibility_descriptor *epic = &ep->epic_descriptor;
502 struct edge_compatibility_bits *bits;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700503 struct device *dev = &serial->dev->dev;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800504
505 ep->is_epic = 0;
506 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
507 USB_REQUEST_ION_GET_EPIC_DESC,
508 0xC0, 0x00, 0x00,
509 &ep->epic_descriptor,
510 sizeof(struct edge_compatibility_descriptor),
511 300);
512
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800513 if (result > 0) {
514 ep->is_epic = 1;
515 memset(product_info, 0, sizeof(struct edgeport_product_info));
516
Alan Cox03f0dbf2008-07-22 11:16:34 +0100517 product_info->NumPorts = epic->NumPorts;
518 product_info->ProdInfoVer = 0;
519 product_info->FirmwareMajorVersion = epic->MajorVersion;
520 product_info->FirmwareMinorVersion = epic->MinorVersion;
521 product_info->FirmwareBuildNumber = epic->BuildNumber;
522 product_info->iDownloadFile = epic->iDownloadFile;
523 product_info->EpicVer = epic->EpicVer;
524 product_info->Epic = epic->Supports;
525 product_info->ProductId = ION_DEVICE_ID_EDGEPORT_COMPATIBLE;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700526 dump_product_info(ep, product_info);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800527
528 bits = &ep->epic_descriptor.Supports;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700529 dev_dbg(dev, "**EPIC descriptor:\n");
530 dev_dbg(dev, " VendEnableSuspend: %s\n", bits->VendEnableSuspend ? "TRUE": "FALSE");
531 dev_dbg(dev, " IOSPOpen : %s\n", bits->IOSPOpen ? "TRUE": "FALSE");
532 dev_dbg(dev, " IOSPClose : %s\n", bits->IOSPClose ? "TRUE": "FALSE");
533 dev_dbg(dev, " IOSPChase : %s\n", bits->IOSPChase ? "TRUE": "FALSE");
534 dev_dbg(dev, " IOSPSetRxFlow : %s\n", bits->IOSPSetRxFlow ? "TRUE": "FALSE");
535 dev_dbg(dev, " IOSPSetTxFlow : %s\n", bits->IOSPSetTxFlow ? "TRUE": "FALSE");
536 dev_dbg(dev, " IOSPSetXChar : %s\n", bits->IOSPSetXChar ? "TRUE": "FALSE");
537 dev_dbg(dev, " IOSPRxCheck : %s\n", bits->IOSPRxCheck ? "TRUE": "FALSE");
538 dev_dbg(dev, " IOSPSetClrBreak : %s\n", bits->IOSPSetClrBreak ? "TRUE": "FALSE");
539 dev_dbg(dev, " IOSPWriteMCR : %s\n", bits->IOSPWriteMCR ? "TRUE": "FALSE");
540 dev_dbg(dev, " IOSPWriteLCR : %s\n", bits->IOSPWriteLCR ? "TRUE": "FALSE");
541 dev_dbg(dev, " IOSPSetBaudRate : %s\n", bits->IOSPSetBaudRate ? "TRUE": "FALSE");
542 dev_dbg(dev, " TrueEdgeport : %s\n", bits->TrueEdgeport ? "TRUE": "FALSE");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800543 }
544
545 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
548
549/************************************************************************/
550/************************************************************************/
551/* U S B C A L L B A C K F U N C T I O N S */
552/* U S B C A L L B A C K F U N C T I O N S */
553/************************************************************************/
554/************************************************************************/
555
556/*****************************************************************************
557 * edge_interrupt_callback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100558 * this is the callback function for when we have received data on the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 * interrupt endpoint.
560 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100561static void edge_interrupt_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700563 struct edgeport_serial *edge_serial = urb->context;
564 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 struct edgeport_port *edge_port;
566 struct usb_serial_port *port;
Alan Cox4a90f092008-10-13 10:39:46 +0100567 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 unsigned char *data = urb->transfer_buffer;
569 int length = urb->actual_length;
570 int bytes_avail;
571 int position;
572 int txCredits;
573 int portNumber;
574 int result;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700575 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700577 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 case 0:
579 /* success */
580 break;
581 case -ECONNRESET:
582 case -ENOENT:
583 case -ESHUTDOWN:
584 /* this urb is terminated, clean up */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700585 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 return;
587 default:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700588 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 goto exit;
590 }
591
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700592 dev = &edge_serial->serial->dev->dev;
593
Alan Cox03f0dbf2008-07-22 11:16:34 +0100594 /* process this interrupt-read even if there are no ports open */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (length) {
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100596 usb_serial_debug_data(dev, __func__, length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 if (length > 1) {
599 bytes_avail = data[0] | (data[1] << 8);
600 if (bytes_avail) {
601 spin_lock(&edge_serial->es_lock);
602 edge_serial->rxBytesAvail += bytes_avail;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700603 dev_dbg(dev,
604 "%s - bytes_avail=%d, rxBytesAvail=%d, read_in_progress=%d\n",
605 __func__, bytes_avail,
606 edge_serial->rxBytesAvail,
607 edge_serial->read_in_progress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 if (edge_serial->rxBytesAvail > 0 &&
610 !edge_serial->read_in_progress) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700611 dev_dbg(dev, "%s - posting a read\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100612 edge_serial->read_in_progress = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Alan Cox03f0dbf2008-07-22 11:16:34 +0100614 /* we have pending bytes on the
615 bulk in pipe, send a request */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 result = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
617 if (result) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700618 dev_err(dev,
619 "%s - usb_submit_urb(read bulk) failed with result = %d\n",
620 __func__, result);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100621 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
623 }
624 spin_unlock(&edge_serial->es_lock);
625 }
626 }
627 /* grab the txcredits for the ports if available */
628 position = 2;
629 portNumber = 0;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100630 while ((position < length) &&
631 (portNumber < edge_serial->serial->num_ports)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 txCredits = data[position] | (data[position+1] << 8);
633 if (txCredits) {
634 port = edge_serial->serial->port[portNumber];
635 edge_port = usb_get_serial_port_data(port);
636 if (edge_port->open) {
637 spin_lock(&edge_port->ep_lock);
638 edge_port->txCredits += txCredits;
639 spin_unlock(&edge_port->ep_lock);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700640 dev_dbg(dev, "%s - txcredits for port%d = %d\n",
641 __func__, portNumber,
642 edge_port->txCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Alan Cox03f0dbf2008-07-22 11:16:34 +0100644 /* tell the tty driver that something
645 has changed */
Alan Cox4a90f092008-10-13 10:39:46 +0100646 tty = tty_port_tty_get(
647 &edge_port->port->port);
648 if (tty) {
649 tty_wakeup(tty);
650 tty_kref_put(tty);
651 }
Alan Cox03f0dbf2008-07-22 11:16:34 +0100652 /* Since we have more credit, check
653 if more data can be sent */
654 send_more_port_data(edge_serial,
655 edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
657 }
658 position += 2;
659 ++portNumber;
660 }
661 }
662
663exit:
Alan Cox03f0dbf2008-07-22 11:16:34 +0100664 result = usb_submit_urb(urb, GFP_ATOMIC);
665 if (result)
666 dev_err(&urb->dev->dev,
667 "%s - Error %d submitting control urb\n",
668 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
671
672/*****************************************************************************
673 * edge_bulk_in_callback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100674 * this is the callback function for when we have received data on the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 * bulk in endpoint.
676 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100677static void edge_bulk_in_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Ming Leicdc97792008-02-24 18:41:47 +0800679 struct edgeport_serial *edge_serial = urb->context;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700680 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700682 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 __u16 raw_data_length;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700684 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700686 if (status) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700687 dev_dbg(&urb->dev->dev, "%s - nonzero read bulk status received: %d\n",
688 __func__, status);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100689 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return;
691 }
692
693 if (urb->actual_length == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700694 dev_dbg(&urb->dev->dev, "%s - read bulk callback with no data\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100695 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return;
697 }
698
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700699 dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 raw_data_length = urb->actual_length;
701
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100702 usb_serial_debug_data(dev, __func__, raw_data_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 spin_lock(&edge_serial->es_lock);
705
706 /* decrement our rxBytes available by the number that we just got */
707 edge_serial->rxBytesAvail -= raw_data_length;
708
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700709 dev_dbg(dev, "%s - Received = %d, rxBytesAvail %d\n", __func__,
710 raw_data_length, edge_serial->rxBytesAvail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Alan Cox03f0dbf2008-07-22 11:16:34 +0100712 process_rcvd_data(edge_serial, data, urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 /* check to see if there's any more data for us to read */
715 if (edge_serial->rxBytesAvail > 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700716 dev_dbg(dev, "%s - posting a read\n", __func__);
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700717 retval = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
718 if (retval) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700719 dev_err(dev,
720 "%s - usb_submit_urb(read bulk) failed, retval = %d\n",
721 __func__, retval);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100722 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
724 } else {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100725 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
727
728 spin_unlock(&edge_serial->es_lock);
729}
730
731
732/*****************************************************************************
733 * edge_bulk_out_data_callback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100734 * this is the callback function for when we have finished sending
735 * serial data on the bulk out endpoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100737static void edge_bulk_out_data_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
Ming Leicdc97792008-02-24 18:41:47 +0800739 struct edgeport_port *edge_port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 struct tty_struct *tty;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700741 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700743 if (status) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700744 dev_dbg(&urb->dev->dev,
745 "%s - nonzero write bulk status received: %d\n",
746 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
748
Alan Cox4a90f092008-10-13 10:39:46 +0100749 tty = tty_port_tty_get(&edge_port->port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 if (tty && edge_port->open) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100752 /* let the tty driver wakeup if it has a special
753 write_wakeup function */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 tty_wakeup(tty);
755 }
Alan Cox4a90f092008-10-13 10:39:46 +0100756 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Alan Cox03f0dbf2008-07-22 11:16:34 +0100758 /* Release the Write URB */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100759 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Alan Cox03f0dbf2008-07-22 11:16:34 +0100761 /* Check if more data needs to be sent */
762 send_more_port_data((struct edgeport_serial *)
763 (usb_get_serial_data(edge_port->port->serial)), edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
766
767/*****************************************************************************
768 * BulkOutCmdCallback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100769 * this is the callback function for when we have finished sending a
770 * command on the bulk out endpoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100772static void edge_bulk_out_cmd_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
Ming Leicdc97792008-02-24 18:41:47 +0800774 struct edgeport_port *edge_port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 struct tty_struct *tty;
776 int status = urb->status;
777
Oliver Neukum96c706e2007-03-15 15:27:17 +0100778 atomic_dec(&CmdUrbs);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700779 dev_dbg(&urb->dev->dev, "%s - FREE URB %p (outstanding %d)\n",
780 __func__, urb, atomic_read(&CmdUrbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782
783 /* clean up the transfer buffer */
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700784 kfree(urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 /* Free the command urb */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100787 usb_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 if (status) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700790 dev_dbg(&urb->dev->dev,
791 "%s - nonzero write bulk status received: %d\n",
792 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 return;
794 }
795
796 /* Get pointer to tty */
Alan Cox4a90f092008-10-13 10:39:46 +0100797 tty = tty_port_tty_get(&edge_port->port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799 /* tell the tty driver that something has changed */
800 if (tty && edge_port->open)
801 tty_wakeup(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100802 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 /* we have completed the command */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100805 edge_port->commandPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 wake_up(&edge_port->wait_command);
807}
808
809
810/*****************************************************************************
811 * Driver tty interface functions
812 *****************************************************************************/
813
814/*****************************************************************************
815 * SerialOpen
816 * this function is called by the tty driver when a port is opened
817 * If successful, we return 0
818 * Otherwise we return a negative error number.
819 *****************************************************************************/
Alan Coxa509a7e2009-09-19 13:13:26 -0700820static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
822 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700823 struct device *dev = &port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 struct usb_serial *serial;
825 struct edgeport_serial *edge_serial;
826 int response;
827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 if (edge_port == NULL)
829 return -ENODEV;
830
Alan Cox03f0dbf2008-07-22 11:16:34 +0100831 /* see if we've set up our endpoint info yet (can't set it up
832 in edge_startup as the structures were not set up at that time.) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 serial = port->serial;
834 edge_serial = usb_get_serial_data(serial);
Alan Cox95da3102008-07-22 11:09:07 +0100835 if (edge_serial == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 if (edge_serial->interrupt_in_buffer == NULL) {
838 struct usb_serial_port *port0 = serial->port[0];
Alan Cox03f0dbf2008-07-22 11:16:34 +0100839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 /* not set up yet, so do it now */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100841 edge_serial->interrupt_in_buffer =
842 port0->interrupt_in_buffer;
843 edge_serial->interrupt_in_endpoint =
844 port0->interrupt_in_endpointAddress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 edge_serial->interrupt_read_urb = port0->interrupt_in_urb;
846 edge_serial->bulk_in_buffer = port0->bulk_in_buffer;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100847 edge_serial->bulk_in_endpoint =
848 port0->bulk_in_endpointAddress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 edge_serial->read_urb = port0->read_urb;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100850 edge_serial->bulk_out_endpoint =
851 port0->bulk_out_endpointAddress;
852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 /* set up our interrupt urb */
854 usb_fill_int_urb(edge_serial->interrupt_read_urb,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100855 serial->dev,
856 usb_rcvintpipe(serial->dev,
857 port0->interrupt_in_endpointAddress),
858 port0->interrupt_in_buffer,
859 edge_serial->interrupt_read_urb->transfer_buffer_length,
860 edge_interrupt_callback, edge_serial,
861 edge_serial->interrupt_read_urb->interval);
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 /* set up our bulk in urb */
864 usb_fill_bulk_urb(edge_serial->read_urb, serial->dev,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100865 usb_rcvbulkpipe(serial->dev,
866 port0->bulk_in_endpointAddress),
867 port0->bulk_in_buffer,
868 edge_serial->read_urb->transfer_buffer_length,
869 edge_bulk_in_callback, edge_serial);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100870 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 /* start interrupt read for this edgeport
Alan Cox03f0dbf2008-07-22 11:16:34 +0100873 * this interrupt will continue as long
874 * as the edgeport is connected */
875 response = usb_submit_urb(edge_serial->interrupt_read_urb,
876 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (response) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700878 dev_err(dev, "%s - Error %d submitting control urb\n",
879 __func__, response);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
881 }
Alan Cox03f0dbf2008-07-22 11:16:34 +0100882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 /* initialize our wait queues */
884 init_waitqueue_head(&edge_port->wait_open);
885 init_waitqueue_head(&edge_port->wait_chase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 init_waitqueue_head(&edge_port->wait_command);
887
888 /* initialize our icount structure */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100889 memset(&(edge_port->icount), 0x00, sizeof(edge_port->icount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891 /* initialize our port settings */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100892 edge_port->txCredits = 0; /* Can't send any data yet */
893 /* Must always set this bit to enable ints! */
894 edge_port->shadowMCR = MCR_MASTER_IE;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100895 edge_port->chaseResponsePending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 /* send a open port command */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100898 edge_port->openPending = true;
899 edge_port->open = false;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100900 response = send_iosp_ext_cmd(edge_port, IOSP_CMD_OPEN_PORT, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902 if (response < 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700903 dev_err(dev, "%s - error sending open port command\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100904 edge_port->openPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 return -ENODEV;
906 }
907
908 /* now wait for the port to be completely opened */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100909 wait_event_timeout(edge_port->wait_open, !edge_port->openPending,
910 OPEN_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100912 if (!edge_port->open) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 /* open timed out */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700914 dev_dbg(dev, "%s - open timedout\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100915 edge_port->openPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 return -ENODEV;
917 }
918
919 /* create the txfifo */
920 edge_port->txfifo.head = 0;
921 edge_port->txfifo.tail = 0;
922 edge_port->txfifo.count = 0;
923 edge_port->txfifo.size = edge_port->maxTxCredits;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100924 edge_port->txfifo.fifo = kmalloc(edge_port->maxTxCredits, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 if (!edge_port->txfifo.fifo) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700927 dev_dbg(dev, "%s - no memory\n", __func__);
Alan Cox335f8512009-06-11 12:26:29 +0100928 edge_close(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 return -ENOMEM;
930 }
931
932 /* Allocate a URB for the write */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100933 edge_port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100934 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 if (!edge_port->write_urb) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700937 dev_dbg(dev, "%s - no memory\n", __func__);
Alan Cox335f8512009-06-11 12:26:29 +0100938 edge_close(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 return -ENOMEM;
940 }
941
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700942 dev_dbg(dev, "%s(%d) - Initialize TX fifo to %d bytes\n",
943 __func__, port->number, edge_port->maxTxCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 return 0;
946}
947
948
949/************************************************************************
950 *
951 * block_until_chase_response
952 *
953 * This function will block the close until one of the following:
954 * 1. Response to our Chase comes from Edgeport
Joe Perchesdc0d5c12007-12-17 11:40:18 -0800955 * 2. A timeout of 10 seconds without activity has expired
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 * (1K of Edgeport data @ 2400 baud ==> 4 sec to empty)
957 *
958 ************************************************************************/
959static void block_until_chase_response(struct edgeport_port *edge_port)
960{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700961 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 DEFINE_WAIT(wait);
963 __u16 lastCredits;
964 int timeout = 1*HZ;
965 int loop = 10;
966
967 while (1) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100968 /* Save Last credits */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 lastCredits = edge_port->txCredits;
970
Alan Cox03f0dbf2008-07-22 11:16:34 +0100971 /* Did we get our Chase response */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100972 if (!edge_port->chaseResponsePending) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700973 dev_dbg(dev, "%s - Got Chase Response\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Alan Cox03f0dbf2008-07-22 11:16:34 +0100975 /* did we get all of our credit back? */
976 if (edge_port->txCredits == edge_port->maxTxCredits) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700977 dev_dbg(dev, "%s - Got all credits\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 return;
979 }
980 }
981
Alan Cox03f0dbf2008-07-22 11:16:34 +0100982 /* Block the thread for a while */
983 prepare_to_wait(&edge_port->wait_chase, &wait,
984 TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 schedule_timeout(timeout);
986 finish_wait(&edge_port->wait_chase, &wait);
987
988 if (lastCredits == edge_port->txCredits) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100989 /* No activity.. count down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 loop--;
991 if (loop == 0) {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100992 edge_port->chaseResponsePending = false;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700993 dev_dbg(dev, "%s - Chase TIMEOUT\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return;
995 }
996 } else {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100997 /* Reset timeout value back to 10 seconds */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700998 dev_dbg(dev, "%s - Last %d, Current %d\n", __func__,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100999 lastCredits, edge_port->txCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 loop = 10;
1001 }
1002 }
1003}
1004
1005
1006/************************************************************************
1007 *
1008 * block_until_tx_empty
1009 *
1010 * This function will block the close until one of the following:
1011 * 1. TX count are 0
1012 * 2. The edgeport has stopped
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001013 * 3. A timeout of 3 seconds without activity has expired
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 *
1015 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001016static void block_until_tx_empty(struct edgeport_port *edge_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001018 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 DEFINE_WAIT(wait);
1020 struct TxFifo *fifo = &edge_port->txfifo;
1021 __u32 lastCount;
1022 int timeout = HZ/10;
1023 int loop = 30;
1024
1025 while (1) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001026 /* Save Last count */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 lastCount = fifo->count;
1028
Alan Cox03f0dbf2008-07-22 11:16:34 +01001029 /* Is the Edgeport Buffer empty? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 if (lastCount == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001031 dev_dbg(dev, "%s - TX Buffer Empty\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 return;
1033 }
1034
Alan Cox03f0dbf2008-07-22 11:16:34 +01001035 /* Block the thread for a while */
1036 prepare_to_wait(&edge_port->wait_chase, &wait,
1037 TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 schedule_timeout(timeout);
1039 finish_wait(&edge_port->wait_chase, &wait);
1040
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001041 dev_dbg(dev, "%s wait\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 if (lastCount == fifo->count) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001044 /* No activity.. count down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 loop--;
1046 if (loop == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001047 dev_dbg(dev, "%s - TIMEOUT\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 return;
1049 }
1050 } else {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001051 /* Reset timeout value back to seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 loop = 30;
1053 }
1054 }
1055}
1056
1057
1058/*****************************************************************************
1059 * edge_close
1060 * this function is called by the tty driver when a port is closed
1061 *****************************************************************************/
Alan Cox335f8512009-06-11 12:26:29 +01001062static void edge_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
1064 struct edgeport_serial *edge_serial;
1065 struct edgeport_port *edge_port;
1066 int status;
1067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 edge_serial = usb_get_serial_data(port->serial);
1069 edge_port = usb_get_serial_port_data(port);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001070 if (edge_serial == NULL || edge_port == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 return;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001072
1073 /* block until tx is empty */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 block_until_tx_empty(edge_port);
1075
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001076 edge_port->closePending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001078 if ((!edge_serial->is_epic) ||
1079 ((edge_serial->is_epic) &&
1080 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1081 /* flush and chase */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001082 edge_port->chaseResponsePending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001084 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001085 status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
1086 if (status == 0)
1087 /* block until chase finished */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001088 block_until_chase_response(edge_port);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001089 else
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001090 edge_port->chaseResponsePending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 }
1092
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001093 if ((!edge_serial->is_epic) ||
1094 ((edge_serial->is_epic) &&
1095 (edge_serial->epic_descriptor.Supports.IOSPClose))) {
1096 /* close the port */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001097 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CLOSE_PORT\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001098 send_iosp_ext_cmd(edge_port, IOSP_CMD_CLOSE_PORT, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Alan Cox03f0dbf2008-07-22 11:16:34 +01001101 /* port->close = true; */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001102 edge_port->closePending = false;
1103 edge_port->open = false;
1104 edge_port->openPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Mariusz Kozlowski9a25f442006-11-08 15:36:29 +01001106 usb_kill_urb(edge_port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108 if (edge_port->write_urb) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001109 /* if this urb had a transfer buffer already
1110 (old transfer) free it */
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001111 kfree(edge_port->write_urb->transfer_buffer);
1112 usb_free_urb(edge_port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 edge_port->write_urb = NULL;
1114 }
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001115 kfree(edge_port->txfifo.fifo);
1116 edge_port->txfifo.fifo = NULL;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001117}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119/*****************************************************************************
1120 * SerialWrite
Alan Cox03f0dbf2008-07-22 11:16:34 +01001121 * this function is called by the tty driver when data should be written
1122 * to the port.
1123 * If successful, we return the number of bytes written, otherwise we
1124 * return a negative error number.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001126static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
1127 const unsigned char *data, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
1129 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1130 struct TxFifo *fifo;
1131 int copySize;
1132 int bytesleft;
1133 int firsthalf;
1134 int secondhalf;
1135 unsigned long flags;
1136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 if (edge_port == NULL)
1138 return -ENODEV;
1139
Alan Cox03f0dbf2008-07-22 11:16:34 +01001140 /* get a pointer to the Tx fifo */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 fifo = &edge_port->txfifo;
1142
1143 spin_lock_irqsave(&edge_port->ep_lock, flags);
1144
Alan Cox03f0dbf2008-07-22 11:16:34 +01001145 /* calculate number of bytes to put in fifo */
1146 copySize = min((unsigned int)count,
1147 (edge_port->txCredits - fifo->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001149 dev_dbg(&port->dev, "%s(%d) of %d byte(s) Fifo room %d -- will copy %d bytes\n",
1150 __func__, port->number, count,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001151 edge_port->txCredits - fifo->count, copySize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Alan Cox03f0dbf2008-07-22 11:16:34 +01001153 /* catch writes of 0 bytes which the tty driver likes to give us,
1154 and when txCredits is empty */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 if (copySize == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001156 dev_dbg(&port->dev, "%s - copySize = Zero\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 goto finish_write;
1158 }
1159
Alan Cox03f0dbf2008-07-22 11:16:34 +01001160 /* queue the data
1161 * since we can never overflow the buffer we do not have to check for a
1162 * full condition
1163 *
1164 * the copy is done is two parts -- first fill to the end of the buffer
1165 * then copy the reset from the start of the buffer
1166 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 bytesleft = fifo->size - fifo->head;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001168 firsthalf = min(bytesleft, copySize);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001169 dev_dbg(&port->dev, "%s - copy %d bytes of %d into fifo \n", __func__,
1170 firsthalf, bytesleft);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
1172 /* now copy our data */
1173 memcpy(&fifo->fifo[fifo->head], data, firsthalf);
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001174 usb_serial_debug_data(&port->dev, __func__, firsthalf, &fifo->fifo[fifo->head]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Alan Cox03f0dbf2008-07-22 11:16:34 +01001176 /* update the index and size */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 fifo->head += firsthalf;
1178 fifo->count += firsthalf;
1179
Alan Cox03f0dbf2008-07-22 11:16:34 +01001180 /* wrap the index */
1181 if (fifo->head == fifo->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 fifo->head = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184 secondhalf = copySize-firsthalf;
1185
1186 if (secondhalf) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001187 dev_dbg(&port->dev, "%s - copy rest of data %d\n", __func__, secondhalf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 memcpy(&fifo->fifo[fifo->head], &data[firsthalf], secondhalf);
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001189 usb_serial_debug_data(&port->dev, __func__, secondhalf, &fifo->fifo[fifo->head]);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001190 /* update the index and size */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 fifo->count += secondhalf;
1192 fifo->head += secondhalf;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001193 /* No need to check for wrap since we can not get to end of
1194 * the fifo in this part
1195 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 }
1197
1198finish_write:
1199 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1200
Alan Cox03f0dbf2008-07-22 11:16:34 +01001201 send_more_port_data((struct edgeport_serial *)
1202 usb_get_serial_data(port->serial), edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001204 dev_dbg(&port->dev, "%s wrote %d byte(s) TxCredits %d, Fifo %d\n",
1205 __func__, copySize, edge_port->txCredits, fifo->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Alan Cox03f0dbf2008-07-22 11:16:34 +01001207 return copySize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
1210
1211/************************************************************************
1212 *
1213 * send_more_port_data()
1214 *
1215 * This routine attempts to write additional UART transmit data
1216 * to a port over the USB bulk pipe. It is called (1) when new
1217 * data has been written to a port's TxBuffer from higher layers
1218 * (2) when the peripheral sends us additional TxCredits indicating
1219 * that it can accept more Tx data for a given port; and (3) when
1220 * a bulk write completes successfully and we want to see if we
1221 * can transmit more.
1222 *
1223 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001224static void send_more_port_data(struct edgeport_serial *edge_serial,
1225 struct edgeport_port *edge_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
1227 struct TxFifo *fifo = &edge_port->txfifo;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001228 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 struct urb *urb;
1230 unsigned char *buffer;
1231 int status;
1232 int count;
1233 int bytesleft;
1234 int firsthalf;
1235 int secondhalf;
1236 unsigned long flags;
1237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 spin_lock_irqsave(&edge_port->ep_lock, flags);
1239
1240 if (edge_port->write_in_progress ||
1241 !edge_port->open ||
1242 (fifo->count == 0)) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001243 dev_dbg(dev, "%s(%d) EXIT - fifo %d, PendingWrite = %d\n",
1244 __func__, edge_port->port->number,
1245 fifo->count, edge_port->write_in_progress);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 goto exit_send;
1247 }
1248
Alan Cox03f0dbf2008-07-22 11:16:34 +01001249 /* since the amount of data in the fifo will always fit into the
1250 * edgeport buffer we do not need to check the write length
1251 *
1252 * Do we have enough credits for this port to make it worthwhile
1253 * to bother queueing a write. If it's too small, say a few bytes,
1254 * it's better to wait for more credits so we can do a larger write.
1255 */
1256 if (edge_port->txCredits < EDGE_FW_GET_TX_CREDITS_SEND_THRESHOLD(edge_port->maxTxCredits, EDGE_FW_BULK_MAX_PACKET_SIZE)) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001257 dev_dbg(dev, "%s(%d) Not enough credit - fifo %d TxCredit %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01001258 __func__, edge_port->port->number, fifo->count,
1259 edge_port->txCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 goto exit_send;
1261 }
1262
Alan Cox03f0dbf2008-07-22 11:16:34 +01001263 /* lock this write */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001264 edge_port->write_in_progress = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Alan Cox03f0dbf2008-07-22 11:16:34 +01001266 /* get a pointer to the write_urb */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 urb = edge_port->write_urb;
1268
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001269 /* make sure transfer buffer is freed */
1270 kfree(urb->transfer_buffer);
1271 urb->transfer_buffer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Alan Cox03f0dbf2008-07-22 11:16:34 +01001273 /* build the data header for the buffer and port that we are about
1274 to send out */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 count = fifo->count;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001276 buffer = kmalloc(count+2, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 if (buffer == NULL) {
Johan Hovold22a416c2012-02-10 13:20:51 +01001278 dev_err_console(edge_port->port,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001279 "%s - no more kernel memory...\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001280 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 goto exit_send;
1282 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01001283 buffer[0] = IOSP_BUILD_DATA_HDR1(edge_port->port->number
1284 - edge_port->port->serial->minor, count);
1285 buffer[1] = IOSP_BUILD_DATA_HDR2(edge_port->port->number
1286 - edge_port->port->serial->minor, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288 /* now copy our data */
1289 bytesleft = fifo->size - fifo->tail;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001290 firsthalf = min(bytesleft, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 memcpy(&buffer[2], &fifo->fifo[fifo->tail], firsthalf);
1292 fifo->tail += firsthalf;
1293 fifo->count -= firsthalf;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001294 if (fifo->tail == fifo->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 fifo->tail = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 secondhalf = count-firsthalf;
1298 if (secondhalf) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001299 memcpy(&buffer[2+firsthalf], &fifo->fifo[fifo->tail],
1300 secondhalf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 fifo->tail += secondhalf;
1302 fifo->count -= secondhalf;
1303 }
1304
1305 if (count)
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001306 usb_serial_debug_data(&edge_port->port->dev, __func__, count, &buffer[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
1308 /* fill up the urb with all of our data and submit it */
Alan Cox03f0dbf2008-07-22 11:16:34 +01001309 usb_fill_bulk_urb(urb, edge_serial->serial->dev,
1310 usb_sndbulkpipe(edge_serial->serial->dev,
1311 edge_serial->bulk_out_endpoint),
1312 buffer, count+2,
1313 edge_bulk_out_data_callback, edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 /* decrement the number of credits we have by the number we just sent */
1316 edge_port->txCredits -= count;
1317 edge_port->icount.tx += count;
1318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 status = usb_submit_urb(urb, GFP_ATOMIC);
1320 if (status) {
1321 /* something went wrong */
Johan Hovold22a416c2012-02-10 13:20:51 +01001322 dev_err_console(edge_port->port,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001323 "%s - usb_submit_urb(write bulk) failed, status = %d, data lost\n",
1324 __func__, status);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001325 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327 /* revert the credits as something bad happened. */
1328 edge_port->txCredits += count;
1329 edge_port->icount.tx -= count;
1330 }
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001331 dev_dbg(dev, "%s wrote %d byte(s) TxCredit %d, Fifo %d\n",
1332 __func__, count, edge_port->txCredits, fifo->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334exit_send:
1335 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1336}
1337
1338
1339/*****************************************************************************
1340 * edge_write_room
Alan Cox03f0dbf2008-07-22 11:16:34 +01001341 * this function is called by the tty driver when it wants to know how
1342 * many bytes of data we can accept for a specific port. If successful,
1343 * we return the amount of room that we have for this port (the txCredits)
1344 * otherwise we return a negative error number.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001346static int edge_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
Alan Cox95da3102008-07-22 11:09:07 +01001348 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1350 int room;
1351 unsigned long flags;
1352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 if (edge_port == NULL)
Alan Coxd76f2f442008-07-22 11:16:42 +01001354 return 0;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001355 if (edge_port->closePending)
Alan Coxd76f2f442008-07-22 11:16:42 +01001356 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001359 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Alan Coxd76f2f442008-07-22 11:16:42 +01001360 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 }
1362
Alan Cox03f0dbf2008-07-22 11:16:34 +01001363 /* total of both buffers is still txCredit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 spin_lock_irqsave(&edge_port->ep_lock, flags);
1365 room = edge_port->txCredits - edge_port->txfifo.count;
1366 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1367
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001368 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return room;
1370}
1371
1372
1373/*****************************************************************************
1374 * edge_chars_in_buffer
Alan Cox03f0dbf2008-07-22 11:16:34 +01001375 * this function is called by the tty driver when it wants to know how
1376 * many bytes of data we currently have outstanding in the port (data that
1377 * has been written, but hasn't made it out the port yet)
1378 * If successful, we return the number of bytes left to be written in the
1379 * system,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 * Otherwise we return a negative error number.
1381 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001382static int edge_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383{
Alan Cox95da3102008-07-22 11:09:07 +01001384 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1386 int num_chars;
1387 unsigned long flags;
1388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 if (edge_port == NULL)
Alan Coxd76f2f442008-07-22 11:16:42 +01001390 return 0;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001391 if (edge_port->closePending)
Alan Coxd76f2f442008-07-22 11:16:42 +01001392 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001395 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Alan Coxd76f2f442008-07-22 11:16:42 +01001396 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 }
1398
1399 spin_lock_irqsave(&edge_port->ep_lock, flags);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001400 num_chars = edge_port->maxTxCredits - edge_port->txCredits +
1401 edge_port->txfifo.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1403 if (num_chars) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001404 dev_dbg(&port->dev, "%s(port %d) - returns %d\n", __func__,
1405 port->number, num_chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 }
1407
1408 return num_chars;
1409}
1410
1411
1412/*****************************************************************************
1413 * SerialThrottle
1414 * this function is called by the tty driver when it wants to stop the data
1415 * being read from the port.
1416 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001417static void edge_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
Alan Cox95da3102008-07-22 11:09:07 +01001419 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 int status;
1422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 if (edge_port == NULL)
1424 return;
1425
1426 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001427 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 return;
1429 }
1430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 /* if we are implementing XON/XOFF, send the stop character */
1432 if (I_IXOFF(tty)) {
1433 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001434 status = edge_write(tty, port, &stop_char, 1);
1435 if (status <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 }
1438
1439 /* if we are implementing RTS/CTS, toggle that line */
Alan Coxadc8d742012-07-14 15:31:47 +01001440 if (tty->termios.c_cflag & CRTSCTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 edge_port->shadowMCR &= ~MCR_RTS;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001442 status = send_cmd_write_uart_register(edge_port, MCR,
1443 edge_port->shadowMCR);
1444 if (status != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447}
1448
1449
1450/*****************************************************************************
1451 * edge_unthrottle
Alan Cox03f0dbf2008-07-22 11:16:34 +01001452 * this function is called by the tty driver when it wants to resume the
1453 * data being read from the port (called after SerialThrottle is called)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001455static void edge_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456{
Alan Cox95da3102008-07-22 11:09:07 +01001457 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 int status;
1460
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 if (edge_port == NULL)
1462 return;
1463
1464 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001465 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 return;
1467 }
1468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 /* if we are implementing XON/XOFF, send the start character */
1470 if (I_IXOFF(tty)) {
1471 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001472 status = edge_write(tty, port, &start_char, 1);
1473 if (status <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 /* if we are implementing RTS/CTS, toggle that line */
Alan Coxadc8d742012-07-14 15:31:47 +01001477 if (tty->termios.c_cflag & CRTSCTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 edge_port->shadowMCR |= MCR_RTS;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001479 send_cmd_write_uart_register(edge_port, MCR,
1480 edge_port->shadowMCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482}
1483
1484
1485/*****************************************************************************
1486 * SerialSetTermios
Alan Cox03f0dbf2008-07-22 11:16:34 +01001487 * this function is called by the tty driver when it wants to change
1488 * the termios structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001490static void edge_set_termios(struct tty_struct *tty,
1491 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
1493 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 unsigned int cflag;
1495
Alan Coxadc8d742012-07-14 15:31:47 +01001496 cflag = tty->termios.c_cflag;
Linus Torvaldsd9a80742012-10-01 13:23:01 -07001497 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 -07001498 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 -07001499
1500 if (edge_port == NULL)
1501 return;
1502
1503 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001504 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 return;
1506 }
1507
1508 /* change the port settings to the new ones specified */
Alan Cox95da3102008-07-22 11:09:07 +01001509 change_port_settings(tty, edge_port, old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510}
1511
1512
1513/*****************************************************************************
1514 * get_lsr_info - get line status register info
1515 *
1516 * Purpose: Let user call ioctl() to get info when the UART physically
1517 * is emptied. On bus types like RS485, the transmitter must
1518 * release the bus after transmitting. This must be done when
1519 * the transmit shift register is empty, not be done when the
1520 * transmit holding register is empty. This functionality
Alan Cox03f0dbf2008-07-22 11:16:34 +01001521 * allows an RS485 driver to be written in user space.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001523static int get_lsr_info(struct edgeport_port *edge_port,
1524 unsigned int __user *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525{
1526 unsigned int result = 0;
1527 unsigned long flags;
1528
1529 spin_lock_irqsave(&edge_port->ep_lock, flags);
1530 if (edge_port->maxTxCredits == edge_port->txCredits &&
1531 edge_port->txfifo.count == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001532 dev_dbg(&edge_port->port->dev, "%s -- Empty\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 result = TIOCSER_TEMT;
1534 }
1535 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1536
1537 if (copy_to_user(value, &result, sizeof(int)))
1538 return -EFAULT;
1539 return 0;
1540}
1541
Alan Cox20b9d172011-02-14 16:26:50 +00001542static int edge_tiocmset(struct tty_struct *tty,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001543 unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544{
Alan Cox95da3102008-07-22 11:09:07 +01001545 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1547 unsigned int mcr;
1548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 mcr = edge_port->shadowMCR;
1550 if (set & TIOCM_RTS)
1551 mcr |= MCR_RTS;
1552 if (set & TIOCM_DTR)
1553 mcr |= MCR_DTR;
1554 if (set & TIOCM_LOOP)
1555 mcr |= MCR_LOOPBACK;
1556
1557 if (clear & TIOCM_RTS)
1558 mcr &= ~MCR_RTS;
1559 if (clear & TIOCM_DTR)
1560 mcr &= ~MCR_DTR;
1561 if (clear & TIOCM_LOOP)
1562 mcr &= ~MCR_LOOPBACK;
1563
1564 edge_port->shadowMCR = mcr;
1565
1566 send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1567
1568 return 0;
1569}
1570
Alan Cox60b33c12011-02-14 16:26:14 +00001571static int edge_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572{
Alan Cox95da3102008-07-22 11:09:07 +01001573 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1575 unsigned int result = 0;
1576 unsigned int msr;
1577 unsigned int mcr;
1578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 msr = edge_port->shadowMSR;
1580 mcr = edge_port->shadowMCR;
1581 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
1582 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
1583 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
1584 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
1585 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
1586 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
1587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 return result;
1589}
1590
Alan Cox0bca1b92010-09-16 18:21:40 +01001591static int edge_get_icount(struct tty_struct *tty,
1592 struct serial_icounter_struct *icount)
1593{
1594 struct usb_serial_port *port = tty->driver_data;
1595 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1596 struct async_icount cnow;
1597 cnow = edge_port->icount;
1598
1599 icount->cts = cnow.cts;
1600 icount->dsr = cnow.dsr;
1601 icount->rng = cnow.rng;
1602 icount->dcd = cnow.dcd;
1603 icount->rx = cnow.rx;
1604 icount->tx = cnow.tx;
1605 icount->frame = cnow.frame;
1606 icount->overrun = cnow.overrun;
1607 icount->parity = cnow.parity;
1608 icount->brk = cnow.brk;
1609 icount->buf_overrun = cnow.buf_overrun;
1610
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001611 dev_dbg(&port->dev, "%s (%d) TIOCGICOUNT RX=%d, TX=%d\n", __func__,
1612 port->number, icount->rx, icount->tx);
Alan Cox0bca1b92010-09-16 18:21:40 +01001613 return 0;
1614}
1615
Alan Cox03f0dbf2008-07-22 11:16:34 +01001616static int get_serial_info(struct edgeport_port *edge_port,
1617 struct serial_struct __user *retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618{
1619 struct serial_struct tmp;
1620
1621 if (!retinfo)
1622 return -EFAULT;
1623
1624 memset(&tmp, 0, sizeof(tmp));
1625
1626 tmp.type = PORT_16550A;
1627 tmp.line = edge_port->port->serial->minor;
1628 tmp.port = edge_port->port->number;
1629 tmp.irq = 0;
1630 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1631 tmp.xmit_fifo_size = edge_port->maxTxCredits;
1632 tmp.baud_base = 9600;
1633 tmp.close_delay = 5*HZ;
1634 tmp.closing_wait = 30*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
1636 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1637 return -EFAULT;
1638 return 0;
1639}
1640
1641
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642/*****************************************************************************
1643 * SerialIoctl
1644 * this function handles any ioctl calls to the driver
1645 *****************************************************************************/
Alan Cox00a0d0d2011-02-14 16:27:06 +00001646static int edge_ioctl(struct tty_struct *tty,
Alan Cox95da3102008-07-22 11:09:07 +01001647 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
Alan Cox95da3102008-07-22 11:09:07 +01001649 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 DEFINE_WAIT(wait);
1651 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1652 struct async_icount cnow;
1653 struct async_icount cprev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001655 dev_dbg(&port->dev, "%s - port %d, cmd = 0x%x\n", __func__, port->number, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
1657 switch (cmd) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001658 case TIOCSERGETLSR:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001659 dev_dbg(&port->dev, "%s (%d) TIOCSERGETLSR\n", __func__, port->number);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001660 return get_lsr_info(edge_port, (unsigned int __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Alan Cox03f0dbf2008-07-22 11:16:34 +01001662 case TIOCGSERIAL:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001663 dev_dbg(&port->dev, "%s (%d) TIOCGSERIAL\n", __func__, port->number);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001664 return get_serial_info(edge_port, (struct serial_struct __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Alan Cox03f0dbf2008-07-22 11:16:34 +01001666 case TIOCMIWAIT:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001667 dev_dbg(&port->dev, "%s (%d) TIOCMIWAIT\n", __func__, port->number);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001668 cprev = edge_port->icount;
1669 while (1) {
Johan Hovold33357622013-03-19 09:21:16 +01001670 prepare_to_wait(&port->delta_msr_wait,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001671 &wait, TASK_INTERRUPTIBLE);
1672 schedule();
Johan Hovold33357622013-03-19 09:21:16 +01001673 finish_wait(&port->delta_msr_wait, &wait);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001674 /* see if a signal did it */
1675 if (signal_pending(current))
1676 return -ERESTARTSYS;
Johan Hovold33357622013-03-19 09:21:16 +01001677
1678 if (port->serial->disconnected)
1679 return -EIO;
1680
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 cnow = edge_port->icount;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001682 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1683 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1684 return -EIO; /* no change => error */
1685 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1686 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1687 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1688 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1689 return 0;
1690 }
1691 cprev = cnow;
1692 }
1693 /* NOTREACHED */
1694 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 return -ENOIOCTLCMD;
1698}
1699
1700
1701/*****************************************************************************
1702 * SerialBreak
1703 * this function sends a break to the port
1704 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001705static void edge_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706{
Alan Cox95da3102008-07-22 11:09:07 +01001707 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001709 struct edgeport_serial *edge_serial = usb_get_serial_data(port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 int status;
1711
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001712 if ((!edge_serial->is_epic) ||
1713 ((edge_serial->is_epic) &&
1714 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1715 /* flush and chase */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001716 edge_port->chaseResponsePending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001718 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001719 status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001720 if (status == 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001721 /* block until chase finished */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001722 block_until_chase_response(edge_port);
1723 } else {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001724 edge_port->chaseResponsePending = false;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 }
1727
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001728 if ((!edge_serial->is_epic) ||
1729 ((edge_serial->is_epic) &&
1730 (edge_serial->epic_descriptor.Supports.IOSPSetClrBreak))) {
1731 if (break_state == -1) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001732 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_SET_BREAK\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001733 status = send_iosp_ext_cmd(edge_port,
1734 IOSP_CMD_SET_BREAK, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001735 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001736 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CLEAR_BREAK\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001737 status = send_iosp_ext_cmd(edge_port,
1738 IOSP_CMD_CLEAR_BREAK, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001739 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01001740 if (status)
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001741 dev_dbg(&port->dev, "%s - error sending break set/clear command.\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01001742 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744}
1745
1746
1747/*****************************************************************************
1748 * process_rcvd_data
1749 * this function handles the data received on the bulk in pipe.
1750 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001751static void process_rcvd_data(struct edgeport_serial *edge_serial,
1752 unsigned char *buffer, __u16 bufferLength)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001754 struct device *dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 struct usb_serial_port *port;
1756 struct edgeport_port *edge_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 __u16 lastBufferLength;
1758 __u16 rxLen;
1759
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 lastBufferLength = bufferLength + 1;
1761
1762 while (bufferLength > 0) {
1763 /* failsafe incase we get a message that we don't understand */
1764 if (lastBufferLength == bufferLength) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001765 dev_dbg(dev, "%s - stuck in loop, exiting it.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 break;
1767 }
1768 lastBufferLength = bufferLength;
1769
1770 switch (edge_serial->rxState) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001771 case EXPECT_HDR1:
1772 edge_serial->rxHeader1 = *buffer;
1773 ++buffer;
1774 --bufferLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
Alan Cox03f0dbf2008-07-22 11:16:34 +01001776 if (bufferLength == 0) {
1777 edge_serial->rxState = EXPECT_HDR2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 break;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001779 }
1780 /* otherwise, drop on through */
1781 case EXPECT_HDR2:
1782 edge_serial->rxHeader2 = *buffer;
1783 ++buffer;
1784 --bufferLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001786 dev_dbg(dev, "%s - Hdr1=%02X Hdr2=%02X\n", __func__,
1787 edge_serial->rxHeader1, edge_serial->rxHeader2);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001788 /* Process depending on whether this header is
1789 * data or status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Alan Cox03f0dbf2008-07-22 11:16:34 +01001791 if (IS_CMD_STAT_HDR(edge_serial->rxHeader1)) {
1792 /* Decode this status header and go to
1793 * EXPECT_HDR1 (if we can process the status
1794 * with only 2 bytes), or go to EXPECT_HDR3 to
1795 * get the third byte. */
1796 edge_serial->rxPort =
1797 IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1798 edge_serial->rxStatusCode =
1799 IOSP_GET_STATUS_CODE(
1800 edge_serial->rxHeader1);
1801
1802 if (!IOSP_STATUS_IS_2BYTE(
1803 edge_serial->rxStatusCode)) {
1804 /* This status needs additional bytes.
1805 * Save what we have and then wait for
1806 * more data.
1807 */
1808 edge_serial->rxStatusParam
1809 = edge_serial->rxHeader2;
1810 edge_serial->rxState = EXPECT_HDR3;
1811 break;
1812 }
1813 /* We have all the header bytes, process the
1814 status now */
1815 process_rcvd_status(edge_serial,
1816 edge_serial->rxHeader2, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 edge_serial->rxState = EXPECT_HDR1;
1818 break;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001819 } else {
1820 edge_serial->rxPort =
1821 IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1822 edge_serial->rxBytesRemaining =
1823 IOSP_GET_HDR_DATA_LEN(
1824 edge_serial->rxHeader1,
1825 edge_serial->rxHeader2);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001826 dev_dbg(dev, "%s - Data for Port %u Len %u\n",
1827 __func__,
1828 edge_serial->rxPort,
1829 edge_serial->rxBytesRemaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Alan Cox03f0dbf2008-07-22 11:16:34 +01001831 /* ASSERT(DevExt->RxPort < DevExt->NumPorts);
1832 * ASSERT(DevExt->RxBytesRemaining <
1833 * IOSP_MAX_DATA_LENGTH);
1834 */
1835
1836 if (bufferLength == 0) {
1837 edge_serial->rxState = EXPECT_DATA;
1838 break;
1839 }
1840 /* Else, drop through */
1841 }
1842 case EXPECT_DATA: /* Expect data */
1843 if (bufferLength < edge_serial->rxBytesRemaining) {
1844 rxLen = bufferLength;
1845 /* Expect data to start next buffer */
1846 edge_serial->rxState = EXPECT_DATA;
1847 } else {
1848 /* BufLen >= RxBytesRemaining */
1849 rxLen = edge_serial->rxBytesRemaining;
1850 /* Start another header next time */
1851 edge_serial->rxState = EXPECT_HDR1;
1852 }
1853
1854 bufferLength -= rxLen;
1855 edge_serial->rxBytesRemaining -= rxLen;
1856
1857 /* spit this data back into the tty driver if this
1858 port is open */
1859 if (rxLen) {
1860 port = edge_serial->serial->port[
1861 edge_serial->rxPort];
1862 edge_port = usb_get_serial_port_data(port);
1863 if (edge_port->open) {
Jiri Slaby2e124b42013-01-03 15:53:06 +01001864 dev_dbg(dev, "%s - Sending %d bytes to TTY for port %d\n",
1865 __func__, rxLen,
1866 edge_serial->rxPort);
1867 edge_tty_recv(edge_port->port, buffer,
1868 rxLen);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001869 edge_port->icount.rx += rxLen;
1870 }
1871 buffer += rxLen;
1872 }
1873 break;
1874
1875 case EXPECT_HDR3: /* Expect 3rd byte of status header */
1876 edge_serial->rxHeader3 = *buffer;
1877 ++buffer;
1878 --bufferLength;
1879
1880 /* We have all the header bytes, process the
1881 status now */
1882 process_rcvd_status(edge_serial,
1883 edge_serial->rxStatusParam,
1884 edge_serial->rxHeader3);
1885 edge_serial->rxState = EXPECT_HDR1;
1886 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 }
1888 }
1889}
1890
1891
1892/*****************************************************************************
1893 * process_rcvd_status
Alan Cox03f0dbf2008-07-22 11:16:34 +01001894 * this function handles the any status messages received on the
1895 * bulk in pipe.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001897static void process_rcvd_status(struct edgeport_serial *edge_serial,
1898 __u8 byte2, __u8 byte3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899{
1900 struct usb_serial_port *port;
1901 struct edgeport_port *edge_port;
Alan Cox4a90f092008-10-13 10:39:46 +01001902 struct tty_struct *tty;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001903 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 __u8 code = edge_serial->rxStatusCode;
1905
1906 /* switch the port pointer to the one being currently talked about */
1907 port = edge_serial->serial->port[edge_serial->rxPort];
1908 edge_port = usb_get_serial_port_data(port);
1909 if (edge_port == NULL) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001910 dev_err(&edge_serial->serial->dev->dev,
1911 "%s - edge_port == NULL for port %d\n",
1912 __func__, edge_serial->rxPort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 return;
1914 }
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001915 dev = &port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
1917 if (code == IOSP_EXT_STATUS) {
1918 switch (byte2) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001919 case IOSP_EXT_STATUS_CHASE_RSP:
1920 /* we want to do EXT status regardless of port
1921 * open/closed */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001922 dev_dbg(dev, "%s - Port %u EXT CHASE_RSP Data = %02x\n",
1923 __func__, edge_serial->rxPort, byte3);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001924 /* Currently, the only EXT_STATUS is Chase, so process
1925 * here instead of one more call to one more subroutine
1926 * If/when more EXT_STATUS, there'll be more work to do
1927 * Also, we currently clear flag and close the port
1928 * regardless of content of above's Byte3.
1929 * We could choose to do something else when Byte3 says
1930 * Timeout on Chase from Edgeport, like wait longer in
1931 * block_until_chase_response, but for now we don't.
1932 */
1933 edge_port->chaseResponsePending = false;
1934 wake_up(&edge_port->wait_chase);
1935 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
Alan Cox03f0dbf2008-07-22 11:16:34 +01001937 case IOSP_EXT_STATUS_RX_CHECK_RSP:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001938 dev_dbg(dev, "%s ========== Port %u CHECK_RSP Sequence = %02x =============\n",
1939 __func__, edge_serial->rxPort, byte3);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001940 /* Port->RxCheckRsp = true; */
1941 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 }
1943 }
1944
1945 if (code == IOSP_STATUS_OPEN_RSP) {
1946 edge_port->txCredits = GET_TX_BUFFER_SIZE(byte3);
1947 edge_port->maxTxCredits = edge_port->txCredits;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001948 dev_dbg(dev, "%s - Port %u Open Response Initial MSR = %02x TxBufferSize = %d\n",
1949 __func__, edge_serial->rxPort, byte2, edge_port->txCredits);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001950 handle_new_msr(edge_port, byte2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
Alan Cox03f0dbf2008-07-22 11:16:34 +01001952 /* send the current line settings to the port so we are
1953 in sync with any further termios calls */
Alan Cox4a90f092008-10-13 10:39:46 +01001954 tty = tty_port_tty_get(&edge_port->port->port);
1955 if (tty) {
1956 change_port_settings(tty,
Alan Coxadc8d742012-07-14 15:31:47 +01001957 edge_port, &tty->termios);
Alan Cox4a90f092008-10-13 10:39:46 +01001958 tty_kref_put(tty);
1959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
1961 /* we have completed the open */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001962 edge_port->openPending = false;
1963 edge_port->open = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 wake_up(&edge_port->wait_open);
1965 return;
1966 }
1967
Alan Cox03f0dbf2008-07-22 11:16:34 +01001968 /* If port is closed, silently discard all rcvd status. We can
1969 * have cases where buffered status is received AFTER the close
1970 * port command is sent to the Edgeport.
1971 */
1972 if (!edge_port->open || edge_port->closePending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
1975 switch (code) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001976 /* Not currently sent by Edgeport */
1977 case IOSP_STATUS_LSR:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001978 dev_dbg(dev, "%s - Port %u LSR Status = %02x\n",
1979 __func__, edge_serial->rxPort, byte2);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001980 handle_new_lsr(edge_port, false, byte2, 0);
1981 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
Alan Cox03f0dbf2008-07-22 11:16:34 +01001983 case IOSP_STATUS_LSR_DATA:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001984 dev_dbg(dev, "%s - Port %u LSR Status = %02x, Data = %02x\n",
1985 __func__, edge_serial->rxPort, byte2, byte3);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001986 /* byte2 is LSR Register */
1987 /* byte3 is broken data byte */
1988 handle_new_lsr(edge_port, true, byte2, byte3);
1989 break;
1990 /*
1991 * case IOSP_EXT_4_STATUS:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001992 * dev_dbg(dev, "%s - Port %u LSR Status = %02x Data = %02x\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01001993 * __func__, edge_serial->rxPort, byte2, byte3);
1994 * break;
1995 */
1996 case IOSP_STATUS_MSR:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001997 dev_dbg(dev, "%s - Port %u MSR Status = %02x\n",
1998 __func__, edge_serial->rxPort, byte2);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001999 /*
2000 * Process this new modem status and generate appropriate
2001 * events, etc, based on the new status. This routine
2002 * also saves the MSR in Port->ShadowMsr.
2003 */
2004 handle_new_msr(edge_port, byte2);
2005 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
Alan Cox03f0dbf2008-07-22 11:16:34 +01002007 default:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002008 dev_dbg(dev, "%s - Unrecognized IOSP status code %u\n", __func__, code);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002009 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011}
2012
2013
2014/*****************************************************************************
2015 * edge_tty_recv
2016 * this function passes data on to the tty flip buffer
2017 *****************************************************************************/
Jiri Slaby2e124b42013-01-03 15:53:06 +01002018static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
2019 int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020{
2021 int cnt;
2022
Jiri Slaby05c7cd32013-01-03 15:53:04 +01002023 cnt = tty_insert_flip_string(&port->port, data, length);
Alan Coxa108bfc2010-02-18 16:44:01 +00002024 if (cnt < length) {
Jiri Slaby05c7cd32013-01-03 15:53:04 +01002025 dev_err(&port->dev, "%s - dropping data, %d bytes lost\n",
Alan Coxa108bfc2010-02-18 16:44:01 +00002026 __func__, length - cnt);
2027 }
2028 data += cnt;
2029 length -= cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
Jiri Slaby2e124b42013-01-03 15:53:06 +01002031 tty_flip_buffer_push(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032}
2033
2034
2035/*****************************************************************************
2036 * handle_new_msr
2037 * this function handles any change to the msr register for a port.
2038 *****************************************************************************/
2039static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr)
2040{
2041 struct async_icount *icount;
2042
Alan Cox03f0dbf2008-07-22 11:16:34 +01002043 if (newMsr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
2044 EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 icount = &edge_port->icount;
2046
2047 /* update input line counters */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002048 if (newMsr & EDGEPORT_MSR_DELTA_CTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 icount->cts++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002050 if (newMsr & EDGEPORT_MSR_DELTA_DSR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 icount->dsr++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002052 if (newMsr & EDGEPORT_MSR_DELTA_CD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 icount->dcd++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002054 if (newMsr & EDGEPORT_MSR_DELTA_RI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 icount->rng++;
Johan Hovold33357622013-03-19 09:21:16 +01002056 wake_up_interruptible(&edge_port->port->delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 }
2058
2059 /* Save the new modem status */
2060 edge_port->shadowMSR = newMsr & 0xf0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061}
2062
2063
2064/*****************************************************************************
2065 * handle_new_lsr
2066 * this function handles any change to the lsr register for a port.
2067 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002068static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
2069 __u8 lsr, __u8 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002071 __u8 newLsr = (__u8) (lsr & (__u8)
2072 (LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
2073 struct async_icount *icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 edge_port->shadowLSR = lsr;
2076
2077 if (newLsr & LSR_BREAK) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002078 /*
2079 * Parity and Framing errors only count if they
2080 * occur exclusive of a break being
2081 * received.
2082 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 newLsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
2084 }
2085
2086 /* Place LSR data byte into Rx buffer */
Jiri Slaby2e124b42013-01-03 15:53:06 +01002087 if (lsrData)
2088 edge_tty_recv(edge_port->port, &data, 1);
2089
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 /* update input line counters */
2091 icount = &edge_port->icount;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002092 if (newLsr & LSR_BREAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 icount->brk++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002094 if (newLsr & LSR_OVER_ERR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 icount->overrun++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002096 if (newLsr & LSR_PAR_ERR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 icount->parity++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002098 if (newLsr & LSR_FRM_ERR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 icount->frame++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100}
2101
2102
2103/****************************************************************************
2104 * sram_write
Alan Cox03f0dbf2008-07-22 11:16:34 +01002105 * writes a number of bytes to the Edgeport device's sram starting at the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 * given address.
2107 * If successful returns the number of bytes written, otherwise it returns
2108 * a negative error number of the problem.
2109 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002110static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
2111 __u16 length, const __u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112{
2113 int result;
2114 __u16 current_length;
2115 unsigned char *transfer_buffer;
2116
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002117 dev_dbg(&serial->dev->dev, "%s - %x, %x, %d\n", __func__, extAddr, addr, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
Alan Cox03f0dbf2008-07-22 11:16:34 +01002119 transfer_buffer = kmalloc(64, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 if (!transfer_buffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002121 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
2122 __func__, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 return -ENOMEM;
2124 }
2125
2126 /* need to split these writes up into 64 byte chunks */
2127 result = 0;
2128 while (length > 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002129 if (length > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 current_length = 64;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002131 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 current_length = length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002133
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002134/* dev_dbg(&serial->dev->dev, "%s - writing %x, %x, %d\n", __func__, extAddr, addr, current_length); */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002135 memcpy(transfer_buffer, data, current_length);
2136 result = usb_control_msg(serial->dev,
2137 usb_sndctrlpipe(serial->dev, 0),
2138 USB_REQUEST_ION_WRITE_RAM,
2139 0x40, addr, extAddr, transfer_buffer,
2140 current_length, 300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 if (result < 0)
2142 break;
2143 length -= current_length;
2144 addr += current_length;
2145 data += current_length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
Alan Cox03f0dbf2008-07-22 11:16:34 +01002148 kfree(transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 return result;
2150}
2151
2152
2153/****************************************************************************
2154 * rom_write
2155 * writes a number of bytes to the Edgeport device's ROM starting at the
2156 * given address.
2157 * If successful returns the number of bytes written, otherwise it returns
2158 * a negative error number of the problem.
2159 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002160static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
2161 __u16 length, const __u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162{
2163 int result;
2164 __u16 current_length;
2165 unsigned char *transfer_buffer;
2166
Alan Cox03f0dbf2008-07-22 11:16:34 +01002167 transfer_buffer = kmalloc(64, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 if (!transfer_buffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002169 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
2170 __func__, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 return -ENOMEM;
2172 }
2173
2174 /* need to split these writes up into 64 byte chunks */
2175 result = 0;
2176 while (length > 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002177 if (length > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 current_length = 64;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002179 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 current_length = length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002181 memcpy(transfer_buffer, data, current_length);
2182 result = usb_control_msg(serial->dev,
2183 usb_sndctrlpipe(serial->dev, 0),
2184 USB_REQUEST_ION_WRITE_ROM, 0x40,
2185 addr, extAddr,
2186 transfer_buffer, current_length, 300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 if (result < 0)
2188 break;
2189 length -= current_length;
2190 addr += current_length;
2191 data += current_length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
Alan Cox03f0dbf2008-07-22 11:16:34 +01002194 kfree(transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 return result;
2196}
2197
2198
2199/****************************************************************************
2200 * rom_read
2201 * reads a number of bytes from the Edgeport device starting at the given
2202 * address.
2203 * If successful returns the number of bytes read, otherwise it returns
2204 * a negative error number of the problem.
2205 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002206static int rom_read(struct usb_serial *serial, __u16 extAddr,
2207 __u16 addr, __u16 length, __u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208{
2209 int result;
2210 __u16 current_length;
2211 unsigned char *transfer_buffer;
2212
Alan Cox03f0dbf2008-07-22 11:16:34 +01002213 transfer_buffer = kmalloc(64, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 if (!transfer_buffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002215 dev_err(&serial->dev->dev,
2216 "%s - kmalloc(%d) failed.\n", __func__, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 return -ENOMEM;
2218 }
2219
2220 /* need to split these reads up into 64 byte chunks */
2221 result = 0;
2222 while (length > 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002223 if (length > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 current_length = 64;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002225 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 current_length = length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002227 result = usb_control_msg(serial->dev,
2228 usb_rcvctrlpipe(serial->dev, 0),
2229 USB_REQUEST_ION_READ_ROM,
2230 0xC0, addr, extAddr, transfer_buffer,
2231 current_length, 300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 if (result < 0)
2233 break;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002234 memcpy(data, transfer_buffer, current_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 length -= current_length;
2236 addr += current_length;
2237 data += current_length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Alan Cox03f0dbf2008-07-22 11:16:34 +01002240 kfree(transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 return result;
2242}
2243
2244
2245/****************************************************************************
2246 * send_iosp_ext_cmd
2247 * Is used to send a IOSP message to the Edgeport device
2248 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002249static int send_iosp_ext_cmd(struct edgeport_port *edge_port,
2250 __u8 command, __u8 param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251{
2252 unsigned char *buffer;
2253 unsigned char *currentCommand;
2254 int length = 0;
2255 int status = 0;
2256
Alan Cox03f0dbf2008-07-22 11:16:34 +01002257 buffer = kmalloc(10, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 if (!buffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002259 dev_err(&edge_port->port->dev,
2260 "%s - kmalloc(%d) failed.\n", __func__, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 return -ENOMEM;
2262 }
2263
2264 currentCommand = buffer;
2265
Alan Cox03f0dbf2008-07-22 11:16:34 +01002266 MAKE_CMD_EXT_CMD(&currentCommand, &length,
2267 edge_port->port->number - edge_port->port->serial->minor,
2268 command, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
Alan Cox03f0dbf2008-07-22 11:16:34 +01002270 status = write_cmd_usb(edge_port, buffer, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 if (status) {
2272 /* something bad happened, let's free up the memory */
2273 kfree(buffer);
2274 }
2275
2276 return status;
2277}
2278
2279
2280/*****************************************************************************
2281 * write_cmd_usb
2282 * this function writes the given buffer out to the bulk write endpoint.
2283 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002284static int write_cmd_usb(struct edgeport_port *edge_port,
2285 unsigned char *buffer, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002287 struct edgeport_serial *edge_serial =
2288 usb_get_serial_data(edge_port->port->serial);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002289 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 int status = 0;
2291 struct urb *urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01002293 usb_serial_debug_data(dev, __func__, length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
2295 /* Allocate our next urb */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002296 urb = usb_alloc_urb(0, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 if (!urb)
2298 return -ENOMEM;
2299
Oliver Neukum96c706e2007-03-15 15:27:17 +01002300 atomic_inc(&CmdUrbs);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002301 dev_dbg(dev, "%s - ALLOCATE URB %p (outstanding %d)\n",
2302 __func__, urb, atomic_read(&CmdUrbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
Alan Cox03f0dbf2008-07-22 11:16:34 +01002304 usb_fill_bulk_urb(urb, edge_serial->serial->dev,
2305 usb_sndbulkpipe(edge_serial->serial->dev,
2306 edge_serial->bulk_out_endpoint),
2307 buffer, length, edge_bulk_out_cmd_callback, edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002309 edge_port->commandPending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 status = usb_submit_urb(urb, GFP_ATOMIC);
2311
2312 if (status) {
2313 /* something went wrong */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002314 dev_err(dev, "%s - usb_submit_urb(write command) failed, status = %d\n",
2315 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 usb_kill_urb(urb);
2317 usb_free_urb(urb);
Oliver Neukum96c706e2007-03-15 15:27:17 +01002318 atomic_dec(&CmdUrbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 return status;
2320 }
2321
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322#if 0
Alan Cox03f0dbf2008-07-22 11:16:34 +01002323 wait_event(&edge_port->wait_command, !edge_port->commandPending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002325 if (edge_port->commandPending) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 /* command timed out */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002327 dev_dbg(dev, "%s - command timed out\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 status = -EINVAL;
2329 }
2330#endif
2331 return status;
2332}
2333
2334
2335/*****************************************************************************
2336 * send_cmd_write_baud_rate
2337 * this function sends the proper command to change the baud rate of the
2338 * specified port.
2339 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002340static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
2341 int baudRate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002343 struct edgeport_serial *edge_serial =
2344 usb_get_serial_data(edge_port->port->serial);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002345 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 unsigned char *cmdBuffer;
2347 unsigned char *currCmd;
2348 int cmdLen = 0;
2349 int divisor;
2350 int status;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002351 unsigned char number =
2352 edge_port->port->number - edge_port->port->serial->minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
Adam Kropelinbc71e472007-07-29 11:03:29 -04002354 if (edge_serial->is_epic &&
2355 !edge_serial->epic_descriptor.Supports.IOSPSetBaudRate) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002356 dev_dbg(dev, "SendCmdWriteBaudRate - NOT Setting baud rate for port = %d, baud = %d\n",
2357 edge_port->port->number, baudRate);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002358 return 0;
2359 }
2360
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002361 dev_dbg(dev, "%s - port = %d, baud = %d\n", __func__,
2362 edge_port->port->number, baudRate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002364 status = calc_baud_rate_divisor(dev, baudRate, &divisor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 if (status) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002366 dev_err(dev, "%s - bad baud rate\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 return status;
2368 }
2369
Alan Cox03f0dbf2008-07-22 11:16:34 +01002370 /* Alloc memory for the string of commands. */
2371 cmdBuffer = kmalloc(0x100, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 if (!cmdBuffer) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002373 dev_err(dev, "%s - kmalloc(%d) failed.\n", __func__, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 return -ENOMEM;
2375 }
2376 currCmd = cmdBuffer;
2377
Alan Cox03f0dbf2008-07-22 11:16:34 +01002378 /* Enable access to divisor latch */
2379 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR, LCR_DL_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
Alan Cox03f0dbf2008-07-22 11:16:34 +01002381 /* Write the divisor itself */
2382 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLL, LOW8(divisor));
2383 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLM, HIGH8(divisor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
Alan Cox03f0dbf2008-07-22 11:16:34 +01002385 /* Restore original value to disable access to divisor latch */
2386 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR,
2387 edge_port->shadowLCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
Alan Cox03f0dbf2008-07-22 11:16:34 +01002389 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 if (status) {
2391 /* something bad happened, let's free up the memory */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002392 kfree(cmdBuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 }
2394
2395 return status;
2396}
2397
2398
2399/*****************************************************************************
2400 * calc_baud_rate_divisor
2401 * this function calculates the proper baud rate divisor for the specified
2402 * baud rate.
2403 *****************************************************************************/
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002404static int calc_baud_rate_divisor(struct device *dev, int baudrate, int *divisor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405{
2406 int i;
2407 __u16 custom;
2408
Tobias Klauser52950ed2005-12-11 16:20:08 +01002409 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002410 if (divisor_table[i].BaudRate == baudrate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 *divisor = divisor_table[i].Divisor;
2412 return 0;
2413 }
2414 }
2415
Alan Cox03f0dbf2008-07-22 11:16:34 +01002416 /* We have tried all of the standard baud rates
2417 * lets try to calculate the divisor for this baud rate
2418 * Make sure the baud rate is reasonable */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 if (baudrate > 50 && baudrate < 230400) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002420 /* get divisor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 custom = (__u16)((230400L + baudrate/2) / baudrate);
2422
2423 *divisor = custom;
2424
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002425 dev_dbg(dev, "%s - Baud %d = %d\n", __func__, baudrate, custom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 return 0;
2427 }
2428
2429 return -1;
2430}
2431
2432
2433/*****************************************************************************
2434 * send_cmd_write_uart_register
Anand Gadiyarfd589a82009-07-16 17:13:03 +02002435 * this function builds up a uart register message and sends to the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002437static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
2438 __u8 regNum, __u8 regValue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002440 struct edgeport_serial *edge_serial =
2441 usb_get_serial_data(edge_port->port->serial);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002442 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 unsigned char *cmdBuffer;
2444 unsigned char *currCmd;
2445 unsigned long cmdLen = 0;
2446 int status;
2447
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002448 dev_dbg(dev, "%s - write to %s register 0x%02x\n",
2449 (regNum == MCR) ? "MCR" : "LCR", __func__, regValue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
Adam Kropelinbc71e472007-07-29 11:03:29 -04002451 if (edge_serial->is_epic &&
2452 !edge_serial->epic_descriptor.Supports.IOSPWriteMCR &&
2453 regNum == MCR) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002454 dev_dbg(dev, "SendCmdWriteUartReg - Not writing to MCR Register\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002455 return 0;
2456 }
2457
Adam Kropelinbc71e472007-07-29 11:03:29 -04002458 if (edge_serial->is_epic &&
2459 !edge_serial->epic_descriptor.Supports.IOSPWriteLCR &&
2460 regNum == LCR) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002461 dev_dbg(dev, "SendCmdWriteUartReg - Not writing to LCR Register\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002462 return 0;
2463 }
2464
Alan Cox03f0dbf2008-07-22 11:16:34 +01002465 /* Alloc memory for the string of commands. */
2466 cmdBuffer = kmalloc(0x10, GFP_ATOMIC);
2467 if (cmdBuffer == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469
2470 currCmd = cmdBuffer;
2471
Alan Cox03f0dbf2008-07-22 11:16:34 +01002472 /* Build a cmd in the buffer to write the given register */
2473 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen,
2474 edge_port->port->number - edge_port->port->serial->minor,
2475 regNum, regValue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
2477 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
2478 if (status) {
2479 /* something bad happened, let's free up the memory */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002480 kfree(cmdBuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 }
2482
2483 return status;
2484}
2485
2486
2487/*****************************************************************************
2488 * change_port_settings
Alan Cox03f0dbf2008-07-22 11:16:34 +01002489 * This routine is called to set the UART on the device to match the
2490 * specified new settings.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01002492
2493static void change_port_settings(struct tty_struct *tty,
2494 struct edgeport_port *edge_port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002496 struct device *dev = &edge_port->port->dev;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002497 struct edgeport_serial *edge_serial =
2498 usb_get_serial_data(edge_port->port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 int baud;
2500 unsigned cflag;
2501 __u8 mask = 0xff;
2502 __u8 lData;
2503 __u8 lParity;
2504 __u8 lStop;
2505 __u8 rxFlow;
2506 __u8 txFlow;
2507 int status;
2508
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002509 dev_dbg(dev, "%s - port %d\n", __func__, edge_port->port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002511 if (!edge_port->open &&
2512 !edge_port->openPending) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002513 dev_dbg(dev, "%s - port not opened\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 return;
2515 }
2516
Alan Coxadc8d742012-07-14 15:31:47 +01002517 cflag = tty->termios.c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
2519 switch (cflag & CSIZE) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002520 case CS5:
2521 lData = LCR_BITS_5; mask = 0x1f;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002522 dev_dbg(dev, "%s - data bits = 5\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002523 break;
2524 case CS6:
2525 lData = LCR_BITS_6; mask = 0x3f;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002526 dev_dbg(dev, "%s - data bits = 6\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002527 break;
2528 case CS7:
2529 lData = LCR_BITS_7; mask = 0x7f;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002530 dev_dbg(dev, "%s - data bits = 7\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002531 break;
2532 default:
2533 case CS8:
2534 lData = LCR_BITS_8;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002535 dev_dbg(dev, "%s - data bits = 8\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002536 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 }
2538
2539 lParity = LCR_PAR_NONE;
2540 if (cflag & PARENB) {
2541 if (cflag & CMSPAR) {
2542 if (cflag & PARODD) {
2543 lParity = LCR_PAR_MARK;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002544 dev_dbg(dev, "%s - parity = mark\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 } else {
2546 lParity = LCR_PAR_SPACE;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002547 dev_dbg(dev, "%s - parity = space\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 }
2549 } else if (cflag & PARODD) {
2550 lParity = LCR_PAR_ODD;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002551 dev_dbg(dev, "%s - parity = odd\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 } else {
2553 lParity = LCR_PAR_EVEN;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002554 dev_dbg(dev, "%s - parity = even\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 }
2556 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002557 dev_dbg(dev, "%s - parity = none\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 }
2559
2560 if (cflag & CSTOPB) {
2561 lStop = LCR_STOP_2;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002562 dev_dbg(dev, "%s - stop bits = 2\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 } else {
2564 lStop = LCR_STOP_1;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002565 dev_dbg(dev, "%s - stop bits = 1\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 }
2567
2568 /* figure out the flow control settings */
2569 rxFlow = txFlow = 0x00;
2570 if (cflag & CRTSCTS) {
2571 rxFlow |= IOSP_RX_FLOW_RTS;
2572 txFlow |= IOSP_TX_FLOW_CTS;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002573 dev_dbg(dev, "%s - RTS/CTS is enabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002575 dev_dbg(dev, "%s - RTS/CTS is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 }
2577
Alan Cox03f0dbf2008-07-22 11:16:34 +01002578 /* if we are implementing XON/XOFF, set the start and stop character
2579 in the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 if (I_IXOFF(tty) || I_IXON(tty)) {
2581 unsigned char stop_char = STOP_CHAR(tty);
2582 unsigned char start_char = START_CHAR(tty);
2583
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002584 if ((!edge_serial->is_epic) ||
2585 ((edge_serial->is_epic) &&
2586 (edge_serial->epic_descriptor.Supports.IOSPSetXChar))) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002587 send_iosp_ext_cmd(edge_port,
2588 IOSP_CMD_SET_XON_CHAR, start_char);
2589 send_iosp_ext_cmd(edge_port,
2590 IOSP_CMD_SET_XOFF_CHAR, stop_char);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
2593 /* if we are implementing INBOUND XON/XOFF */
2594 if (I_IXOFF(tty)) {
2595 rxFlow |= IOSP_RX_FLOW_XON_XOFF;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002596 dev_dbg(dev, "%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2597 __func__, start_char, stop_char);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002599 dev_dbg(dev, "%s - INBOUND XON/XOFF is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 }
2601
2602 /* if we are implementing OUTBOUND XON/XOFF */
2603 if (I_IXON(tty)) {
2604 txFlow |= IOSP_TX_FLOW_XON_XOFF;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002605 dev_dbg(dev, "%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2606 __func__, start_char, stop_char);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002608 dev_dbg(dev, "%s - OUTBOUND XON/XOFF is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 }
2610 }
2611
2612 /* Set flow control to the configured value */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002613 if ((!edge_serial->is_epic) ||
2614 ((edge_serial->is_epic) &&
2615 (edge_serial->epic_descriptor.Supports.IOSPSetRxFlow)))
2616 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_RX_FLOW, rxFlow);
2617 if ((!edge_serial->is_epic) ||
2618 ((edge_serial->is_epic) &&
2619 (edge_serial->epic_descriptor.Supports.IOSPSetTxFlow)))
2620 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_TX_FLOW, txFlow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
2622
2623 edge_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2624 edge_port->shadowLCR |= (lData | lParity | lStop);
2625
2626 edge_port->validDataMask = mask;
2627
2628 /* Send the updated LCR value to the EdgePort */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002629 status = send_cmd_write_uart_register(edge_port, LCR,
2630 edge_port->shadowLCR);
2631 if (status != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
2634 /* set up the MCR register and send it to the EdgePort */
2635 edge_port->shadowMCR = MCR_MASTER_IE;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002636 if (cflag & CBAUD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 edge_port->shadowMCR |= (MCR_DTR | MCR_RTS);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002638
2639 status = send_cmd_write_uart_register(edge_port, MCR,
2640 edge_port->shadowMCR);
2641 if (status != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
2644 /* Determine divisor based on baud rate */
2645 baud = tty_get_baud_rate(tty);
2646 if (!baud) {
2647 /* pick a default, any default... */
2648 baud = 9600;
2649 }
2650
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002651 dev_dbg(dev, "%s - baud rate = %d\n", __func__, baud);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002652 status = send_cmd_write_baud_rate(edge_port, baud);
Alan Cox6ce073b2007-10-18 01:24:25 -07002653 if (status == -1) {
2654 /* Speed change was not possible - put back the old speed */
2655 baud = tty_termios_baud_rate(old_termios);
2656 tty_encode_baud_rate(tty, baud, baud);
2657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658}
2659
2660
2661/****************************************************************************
2662 * unicode_to_ascii
2663 * Turns a string from Unicode into ASCII.
2664 * Doesn't do a good job with any characters that are outside the normal
2665 * ASCII range, but it's only for debugging...
2666 * NOTE: expects the unicode in LE format
2667 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002668static void unicode_to_ascii(char *string, int buflen,
2669 __le16 *unicode, int unicode_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670{
2671 int i;
2672
Pete Zaitcevad933752006-05-22 21:49:44 -07002673 if (buflen <= 0) /* never happens, but... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 return;
Pete Zaitcevad933752006-05-22 21:49:44 -07002675 --buflen; /* space for nul */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676
Pete Zaitcevad933752006-05-22 21:49:44 -07002677 for (i = 0; i < unicode_size; i++) {
2678 if (i >= buflen)
2679 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 string[i] = (char)(le16_to_cpu(unicode[i]));
Pete Zaitcevad933752006-05-22 21:49:44 -07002681 }
2682 string[i] = 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683}
2684
2685
2686/****************************************************************************
2687 * get_manufacturing_desc
Alan Cox03f0dbf2008-07-22 11:16:34 +01002688 * reads in the manufacturing descriptor and stores it into the serial
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 * structure.
2690 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002691static void get_manufacturing_desc(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002693 struct device *dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 int response;
2695
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002696 dev_dbg(dev, "getting manufacturer descriptor\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697
Alan Cox03f0dbf2008-07-22 11:16:34 +01002698 response = rom_read(edge_serial->serial,
2699 (EDGE_MANUF_DESC_ADDR & 0xffff0000) >> 16,
2700 (__u16)(EDGE_MANUF_DESC_ADDR & 0x0000ffff),
2701 EDGE_MANUF_DESC_LEN,
2702 (__u8 *)(&edge_serial->manuf_descriptor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
Alan Cox03f0dbf2008-07-22 11:16:34 +01002704 if (response < 1)
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002705 dev_err(dev, "error in getting manufacturer descriptor\n");
Alan Cox03f0dbf2008-07-22 11:16:34 +01002706 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 char string[30];
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002708 dev_dbg(dev, "**Manufacturer Descriptor\n");
2709 dev_dbg(dev, " RomSize: %dK\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002710 edge_serial->manuf_descriptor.RomSize);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002711 dev_dbg(dev, " RamSize: %dK\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002712 edge_serial->manuf_descriptor.RamSize);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002713 dev_dbg(dev, " CpuRev: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002714 edge_serial->manuf_descriptor.CpuRev);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002715 dev_dbg(dev, " BoardRev: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002716 edge_serial->manuf_descriptor.BoardRev);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002717 dev_dbg(dev, " NumPorts: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002718 edge_serial->manuf_descriptor.NumPorts);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002719 dev_dbg(dev, " DescDate: %d/%d/%d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002720 edge_serial->manuf_descriptor.DescDate[0],
2721 edge_serial->manuf_descriptor.DescDate[1],
2722 edge_serial->manuf_descriptor.DescDate[2]+1900);
Pete Zaitcev4bc203d2006-06-06 18:18:33 -07002723 unicode_to_ascii(string, sizeof(string),
Alan Cox03f0dbf2008-07-22 11:16:34 +01002724 edge_serial->manuf_descriptor.SerialNumber,
2725 edge_serial->manuf_descriptor.SerNumLength/2);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002726 dev_dbg(dev, " SerialNumber: %s\n", string);
Pete Zaitcev4bc203d2006-06-06 18:18:33 -07002727 unicode_to_ascii(string, sizeof(string),
Alan Cox03f0dbf2008-07-22 11:16:34 +01002728 edge_serial->manuf_descriptor.AssemblyNumber,
2729 edge_serial->manuf_descriptor.AssemblyNumLength/2);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002730 dev_dbg(dev, " AssemblyNumber: %s\n", string);
Pete Zaitcev4bc203d2006-06-06 18:18:33 -07002731 unicode_to_ascii(string, sizeof(string),
Pete Zaitcevad933752006-05-22 21:49:44 -07002732 edge_serial->manuf_descriptor.OemAssyNumber,
2733 edge_serial->manuf_descriptor.OemAssyNumLength/2);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002734 dev_dbg(dev, " OemAssyNumber: %s\n", string);
2735 dev_dbg(dev, " UartType: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002736 edge_serial->manuf_descriptor.UartType);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002737 dev_dbg(dev, " IonPid: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002738 edge_serial->manuf_descriptor.IonPid);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002739 dev_dbg(dev, " IonConfig: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002740 edge_serial->manuf_descriptor.IonConfig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 }
2742}
2743
2744
2745/****************************************************************************
2746 * get_boot_desc
Alan Cox03f0dbf2008-07-22 11:16:34 +01002747 * reads in the bootloader descriptor and stores it into the serial
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 * structure.
2749 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002750static void get_boot_desc(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002752 struct device *dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 int response;
2754
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002755 dev_dbg(dev, "getting boot descriptor\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
Alan Cox03f0dbf2008-07-22 11:16:34 +01002757 response = rom_read(edge_serial->serial,
2758 (EDGE_BOOT_DESC_ADDR & 0xffff0000) >> 16,
2759 (__u16)(EDGE_BOOT_DESC_ADDR & 0x0000ffff),
2760 EDGE_BOOT_DESC_LEN,
2761 (__u8 *)(&edge_serial->boot_descriptor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762
Alan Cox03f0dbf2008-07-22 11:16:34 +01002763 if (response < 1)
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002764 dev_err(dev, "error in getting boot descriptor\n");
Alan Cox03f0dbf2008-07-22 11:16:34 +01002765 else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002766 dev_dbg(dev, "**Boot Descriptor:\n");
2767 dev_dbg(dev, " BootCodeLength: %d\n",
2768 le16_to_cpu(edge_serial->boot_descriptor.BootCodeLength));
2769 dev_dbg(dev, " MajorVersion: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002770 edge_serial->boot_descriptor.MajorVersion);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002771 dev_dbg(dev, " MinorVersion: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002772 edge_serial->boot_descriptor.MinorVersion);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002773 dev_dbg(dev, " BuildNumber: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002774 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002775 dev_dbg(dev, " Capabilities: 0x%x\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002776 le16_to_cpu(edge_serial->boot_descriptor.Capabilities));
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002777 dev_dbg(dev, " UConfig0: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002778 edge_serial->boot_descriptor.UConfig0);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002779 dev_dbg(dev, " UConfig1: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002780 edge_serial->boot_descriptor.UConfig1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 }
2782}
2783
2784
2785/****************************************************************************
2786 * load_application_firmware
2787 * This is called to load the application firmware to the device
2788 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002789static void load_application_firmware(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002791 struct device *dev = &edge_serial->serial->dev->dev;
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302792 const struct ihex_binrec *rec;
2793 const struct firmware *fw;
2794 const char *fw_name;
2795 const char *fw_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 int response;
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302797 __u32 Operaddr;
2798 __u16 build;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
2800 switch (edge_serial->product_info.iDownloadFile) {
2801 case EDGE_DOWNLOAD_FILE_I930:
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302802 fw_info = "downloading firmware version (930)";
2803 fw_name = "edgeport/down.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 break;
2805
2806 case EDGE_DOWNLOAD_FILE_80251:
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302807 fw_info = "downloading firmware version (80251)";
2808 fw_name = "edgeport/down2.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 break;
2810
2811 case EDGE_DOWNLOAD_FILE_NONE:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002812 dev_dbg(dev, "No download file specified, skipping download\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 return;
2814
2815 default:
2816 return;
2817 }
2818
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302819 response = request_ihex_firmware(&fw, fw_name,
2820 &edge_serial->serial->dev->dev);
2821 if (response) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002822 dev_err(dev, "Failed to load image \"%s\" err %d\n",
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302823 fw_name, response);
2824 return;
2825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302827 rec = (const struct ihex_binrec *)fw->data;
2828 build = (rec->data[2] << 8) | rec->data[3];
2829
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002830 dev_dbg(dev, "%s %d.%d.%d\n", fw_info, rec->data[0], rec->data[1], build);
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302831
Bjørn Mork271c1152011-01-17 14:19:37 +01002832 edge_serial->product_info.FirmwareMajorVersion = rec->data[0];
2833 edge_serial->product_info.FirmwareMinorVersion = rec->data[1];
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302834 edge_serial->product_info.FirmwareBuildNumber = cpu_to_le16(build);
2835
2836 for (rec = ihex_next_binrec(rec); rec;
2837 rec = ihex_next_binrec(rec)) {
2838 Operaddr = be32_to_cpu(rec->addr);
2839 response = sram_write(edge_serial->serial,
2840 Operaddr >> 16,
2841 Operaddr & 0xFFFF,
2842 be16_to_cpu(rec->len),
2843 &rec->data[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 if (response < 0) {
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302845 dev_err(&edge_serial->serial->dev->dev,
2846 "sram_write failed (%x, %x, %d)\n",
2847 Operaddr >> 16, Operaddr & 0xFFFF,
2848 be16_to_cpu(rec->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 break;
2850 }
2851 }
2852
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002853 dev_dbg(dev, "sending exec_dl_code\n");
2854 response = usb_control_msg (edge_serial->serial->dev,
2855 usb_sndctrlpipe(edge_serial->serial->dev, 0),
2856 USB_REQUEST_ION_EXEC_DL_CODE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 0x40, 0x4000, 0x0001, NULL, 0, 3000);
2858
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302859 release_firmware(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860}
2861
2862
2863/****************************************************************************
2864 * edge_startup
2865 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002866static int edge_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867{
2868 struct edgeport_serial *edge_serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 struct usb_device *dev;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002870 struct device *ddev = &serial->dev->dev;
Johan Hovoldc27f3ef2012-10-17 13:34:57 +02002871 int i;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002872 int response;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002873 bool interrupt_in_found;
2874 bool bulk_in_found;
2875 bool bulk_out_found;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002876 static __u32 descriptor[3] = { EDGE_COMPATIBILITY_MASK0,
2877 EDGE_COMPATIBILITY_MASK1,
2878 EDGE_COMPATIBILITY_MASK2 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879
2880 dev = serial->dev;
2881
2882 /* create our private serial structure */
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +01002883 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 if (edge_serial == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002885 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 return -ENOMEM;
2887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 spin_lock_init(&edge_serial->es_lock);
2889 edge_serial->serial = serial;
2890 usb_set_serial_data(serial, edge_serial);
2891
2892 /* get the name for the device from the device */
Dan Carpenterf10718f2010-01-25 14:53:33 +03002893 i = usb_string(dev, dev->descriptor.iManufacturer,
Pete Zaitcevad933752006-05-22 21:49:44 -07002894 &edge_serial->name[0], MAX_NAME_LEN+1);
Dan Carpenterf10718f2010-01-25 14:53:33 +03002895 if (i < 0)
2896 i = 0;
Pete Zaitcevad933752006-05-22 21:49:44 -07002897 edge_serial->name[i++] = ' ';
Dan Carpenterf10718f2010-01-25 14:53:33 +03002898 usb_string(dev, dev->descriptor.iProduct,
Pete Zaitcevad933752006-05-22 21:49:44 -07002899 &edge_serial->name[i], MAX_NAME_LEN+2 - i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900
2901 dev_info(&serial->dev->dev, "%s detected\n", edge_serial->name);
2902
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002903 /* Read the epic descriptor */
2904 if (get_epic_descriptor(edge_serial) <= 0) {
2905 /* memcpy descriptor to Supports structures */
2906 memcpy(&edge_serial->epic_descriptor.Supports, descriptor,
2907 sizeof(struct edge_compatibility_bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002909 /* get the manufacturing descriptor for this device */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002910 get_manufacturing_desc(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002912 /* get the boot descriptor */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002913 get_boot_desc(edge_serial);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002914
2915 get_product_info(edge_serial);
2916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917
2918 /* set the number of ports from the manufacturing description */
2919 /* serial->num_ports = serial->product_info.NumPorts; */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002920 if ((!edge_serial->is_epic) &&
2921 (edge_serial->product_info.NumPorts != serial->num_ports)) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002922 dev_warn(ddev,
2923 "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 -08002924 edge_serial->product_info.NumPorts,
2925 serial->num_ports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 }
2927
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002928 dev_dbg(ddev, "%s - time 1 %ld\n", __func__, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002930 /* If not an EPiC device */
2931 if (!edge_serial->is_epic) {
2932 /* now load the application firmware into this device */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002933 load_application_firmware(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002935 dev_dbg(ddev, "%s - time 2 %ld\n", __func__, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002937 /* Check current Edgeport EEPROM and update if necessary */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002938 update_edgeport_E2PROM(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002940 dev_dbg(ddev, "%s - time 3 %ld\n", __func__, jiffies);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002941
2942 /* set the configuration to use #1 */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002943/* dev_dbg(ddev, "set_configuration 1\n"); */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002944/* usb_set_configuration (dev, 1); */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002945 }
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002946 dev_dbg(ddev, " FirmwareMajorVersion %d.%d.%d\n",
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302947 edge_serial->product_info.FirmwareMajorVersion,
2948 edge_serial->product_info.FirmwareMinorVersion,
2949 le16_to_cpu(edge_serial->product_info.FirmwareBuildNumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
Alan Cox03f0dbf2008-07-22 11:16:34 +01002951 /* we set up the pointers to the endpoints in the edge_open function,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 * as the structures aren't created yet. */
2953
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002954 response = 0;
2955
2956 if (edge_serial->is_epic) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002957 /* EPIC thing, set up our interrupt polling now and our read
2958 * urb, so that the device knows it really is connected. */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002959 interrupt_in_found = bulk_in_found = bulk_out_found = false;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002960 for (i = 0; i < serial->interface->altsetting[0]
2961 .desc.bNumEndpoints; ++i) {
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002962 struct usb_endpoint_descriptor *endpoint;
2963 int buffer_size;
2964
Alan Cox03f0dbf2008-07-22 11:16:34 +01002965 endpoint = &serial->interface->altsetting[0].
2966 endpoint[i].desc;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07002967 buffer_size = usb_endpoint_maxp(endpoint);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002968 if (!interrupt_in_found &&
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002969 (usb_endpoint_is_int_in(endpoint))) {
2970 /* we found a interrupt in endpoint */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002971 dev_dbg(ddev, "found interrupt in\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002972
2973 /* not set up yet, so do it now */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002974 edge_serial->interrupt_read_urb =
2975 usb_alloc_urb(0, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002976 if (!edge_serial->interrupt_read_urb) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002977 dev_err(ddev, "out of memory\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002978 return -ENOMEM;
2979 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01002980 edge_serial->interrupt_in_buffer =
2981 kmalloc(buffer_size, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002982 if (!edge_serial->interrupt_in_buffer) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002983 dev_err(ddev, "out of memory\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002984 usb_free_urb(edge_serial->interrupt_read_urb);
2985 return -ENOMEM;
2986 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01002987 edge_serial->interrupt_in_endpoint =
2988 endpoint->bEndpointAddress;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002989
2990 /* set up our interrupt urb */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002991 usb_fill_int_urb(
2992 edge_serial->interrupt_read_urb,
2993 dev,
2994 usb_rcvintpipe(dev,
2995 endpoint->bEndpointAddress),
2996 edge_serial->interrupt_in_buffer,
2997 buffer_size,
2998 edge_interrupt_callback,
2999 edge_serial,
3000 endpoint->bInterval);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003001
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003002 interrupt_in_found = true;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003003 }
3004
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003005 if (!bulk_in_found &&
Alan Cox03f0dbf2008-07-22 11:16:34 +01003006 (usb_endpoint_is_bulk_in(endpoint))) {
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003007 /* we found a bulk in endpoint */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003008 dev_dbg(ddev, "found bulk in\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003009
3010 /* not set up yet, so do it now */
Alan Cox03f0dbf2008-07-22 11:16:34 +01003011 edge_serial->read_urb =
3012 usb_alloc_urb(0, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003013 if (!edge_serial->read_urb) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003014 dev_err(ddev, "out of memory\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003015 return -ENOMEM;
3016 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01003017 edge_serial->bulk_in_buffer =
3018 kmalloc(buffer_size, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003019 if (!edge_serial->bulk_in_buffer) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07003020 dev_err(&dev->dev, "out of memory\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003021 usb_free_urb(edge_serial->read_urb);
3022 return -ENOMEM;
3023 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01003024 edge_serial->bulk_in_endpoint =
3025 endpoint->bEndpointAddress;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003026
3027 /* set up our bulk in urb */
3028 usb_fill_bulk_urb(edge_serial->read_urb, dev,
Alan Cox03f0dbf2008-07-22 11:16:34 +01003029 usb_rcvbulkpipe(dev,
3030 endpoint->bEndpointAddress),
3031 edge_serial->bulk_in_buffer,
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07003032 usb_endpoint_maxp(endpoint),
Alan Cox03f0dbf2008-07-22 11:16:34 +01003033 edge_bulk_in_callback,
3034 edge_serial);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003035 bulk_in_found = true;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003036 }
3037
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003038 if (!bulk_out_found &&
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003039 (usb_endpoint_is_bulk_out(endpoint))) {
3040 /* we found a bulk out endpoint */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003041 dev_dbg(ddev, "found bulk out\n");
Alan Cox03f0dbf2008-07-22 11:16:34 +01003042 edge_serial->bulk_out_endpoint =
3043 endpoint->bEndpointAddress;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003044 bulk_out_found = true;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003045 }
3046 }
3047
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003048 if (!interrupt_in_found || !bulk_in_found || !bulk_out_found) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003049 dev_err(ddev, "Error - the proper endpoints were not found!\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003050 return -ENODEV;
3051 }
3052
3053 /* start interrupt read for this edgeport this interrupt will
3054 * continue as long as the edgeport is connected */
Alan Cox03f0dbf2008-07-22 11:16:34 +01003055 response = usb_submit_urb(edge_serial->interrupt_read_urb,
3056 GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003057 if (response)
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003058 dev_err(ddev, "%s - Error %d submitting control urb\n",
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07003059 __func__, response);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003060 }
3061 return response;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062}
3063
3064
3065/****************************************************************************
Alan Sternf9c99bb2009-06-02 11:53:55 -04003066 * edge_disconnect
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 * This function is called whenever the device is removed from the usb bus.
3068 ****************************************************************************/
Alan Sternf9c99bb2009-06-02 11:53:55 -04003069static void edge_disconnect(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070{
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003071 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 /* stop reads and writes on all ports */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003074 /* free up our endpoint stuff */
3075 if (edge_serial->is_epic) {
Oliver Neukum74ac07e2007-06-13 18:50:41 +02003076 usb_kill_urb(edge_serial->interrupt_read_urb);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003077 usb_free_urb(edge_serial->interrupt_read_urb);
3078 kfree(edge_serial->interrupt_in_buffer);
3079
Oliver Neukum74ac07e2007-06-13 18:50:41 +02003080 usb_kill_urb(edge_serial->read_urb);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003081 usb_free_urb(edge_serial->read_urb);
3082 kfree(edge_serial->bulk_in_buffer);
3083 }
Alan Sternf9c99bb2009-06-02 11:53:55 -04003084}
3085
3086
3087/****************************************************************************
3088 * edge_release
3089 * This function is called when the device structure is deallocated.
3090 ****************************************************************************/
3091static void edge_release(struct usb_serial *serial)
3092{
3093 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003094
3095 kfree(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096}
3097
Johan Hovoldc27f3ef2012-10-17 13:34:57 +02003098static int edge_port_probe(struct usb_serial_port *port)
3099{
3100 struct edgeport_port *edge_port;
3101
3102 edge_port = kzalloc(sizeof(*edge_port), GFP_KERNEL);
3103 if (!edge_port)
3104 return -ENOMEM;
3105
3106 spin_lock_init(&edge_port->ep_lock);
3107 edge_port->port = port;
3108
3109 usb_set_serial_port_data(port, edge_port);
3110
3111 return 0;
3112}
3113
3114static int edge_port_remove(struct usb_serial_port *port)
3115{
3116 struct edgeport_port *edge_port;
3117
3118 edge_port = usb_get_serial_port_data(port);
3119 kfree(edge_port);
3120
3121 return 0;
3122}
3123
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07003124module_usb_serial_driver(serial_drivers, id_table_combined);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125
Alan Cox03f0dbf2008-07-22 11:16:34 +01003126MODULE_AUTHOR(DRIVER_AUTHOR);
3127MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128MODULE_LICENSE("GPL");
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05303129MODULE_FIRMWARE("edgeport/boot.fw");
3130MODULE_FIRMWARE("edgeport/boot2.fw");
3131MODULE_FIRMWARE("edgeport/down.fw");
3132MODULE_FIRMWARE("edgeport/down2.fw");