blob: 75b7ccdd2652c029caef4aae31626236bc948cdd [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
54/*
55 * Version Information
56 */
57#define DRIVER_VERSION "v2.7"
58#define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
59#define DRIVER_DESC "Edgeport USB Serial Driver"
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#define MAX_NAME_LEN 64
62
63#define CHASE_TIMEOUT (5*HZ) /* 5 seconds */
64#define OPEN_TIMEOUT (5*HZ) /* 5 seconds */
65#define COMMAND_TIMEOUT (5*HZ) /* 5 seconds */
66
67/* receive port state */
68enum RXSTATE {
Alan Cox03f0dbf2008-07-22 11:16:34 +010069 EXPECT_HDR1 = 0, /* Expect header byte 1 */
70 EXPECT_HDR2 = 1, /* Expect header byte 2 */
71 EXPECT_DATA = 2, /* Expect 'RxBytesRemaining' data */
72 EXPECT_HDR3 = 3, /* Expect header byte 3 (for status hdrs only) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070073};
74
75
Alan Cox03f0dbf2008-07-22 11:16:34 +010076/* Transmit Fifo
77 * This Transmit queue is an extension of the edgeport Rx buffer.
78 * The maximum amount of data buffered in both the edgeport
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 * Rx buffer (maxTxCredits) and this buffer will never exceed maxTxCredits.
80 */
81struct TxFifo {
82 unsigned int head; /* index to head pointer (write) */
83 unsigned int tail; /* index to tail pointer (read) */
84 unsigned int count; /* Bytes in queue */
85 unsigned int size; /* Max size of queue (equal to Max number of TxCredits) */
86 unsigned char *fifo; /* allocated Buffer */
87};
88
89/* This structure holds all of the local port information */
90struct edgeport_port {
91 __u16 txCredits; /* our current credits for this port */
92 __u16 maxTxCredits; /* the max size of the port */
93
94 struct TxFifo txfifo; /* transmit fifo -- size will be maxTxCredits */
95 struct urb *write_urb; /* write URB for this port */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +010096 bool write_in_progress; /* 'true' while a write URB is outstanding */
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 spinlock_t ep_lock;
98
99 __u8 shadowLCR; /* last LCR value received */
100 __u8 shadowMCR; /* last MCR value received */
101 __u8 shadowMSR; /* last MSR value received */
102 __u8 shadowLSR; /* last LSR value received */
103 __u8 shadowXonChar; /* last value set as XON char in Edgeport */
104 __u8 shadowXoffChar; /* last value set as XOFF char in Edgeport */
105 __u8 validDataMask;
106 __u32 baudRate;
107
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100108 bool open;
109 bool openPending;
110 bool commandPending;
111 bool closePending;
112 bool chaseResponsePending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
115 wait_queue_head_t wait_open; /* for handling sleeping while waiting for open to finish */
116 wait_queue_head_t wait_command; /* for handling sleeping while waiting for command to finish */
117 wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
118
119 struct async_icount icount;
120 struct usb_serial_port *port; /* loop back to the owner of this object */
121};
122
123
124/* This structure holds all of the individual device information */
125struct edgeport_serial {
Pete Zaitcevad933752006-05-22 21:49:44 -0700126 char name[MAX_NAME_LEN+2]; /* string name of this device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 struct edge_manuf_descriptor manuf_descriptor; /* the manufacturer descriptor */
129 struct edge_boot_descriptor boot_descriptor; /* the boot firmware descriptor */
130 struct edgeport_product_info product_info; /* Product Info */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800131 struct edge_compatibility_descriptor epic_descriptor; /* Edgeport compatible descriptor */
132 int is_epic; /* flag if EPiC device or not */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 __u8 interrupt_in_endpoint; /* the interrupt endpoint handle */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100135 unsigned char *interrupt_in_buffer; /* the buffer we use for the interrupt endpoint */
136 struct urb *interrupt_read_urb; /* our interrupt urb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 __u8 bulk_in_endpoint; /* the bulk in endpoint handle */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100139 unsigned char *bulk_in_buffer; /* the buffer we use for the bulk in endpoint */
140 struct urb *read_urb; /* our bulk read urb */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100141 bool read_in_progress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 spinlock_t es_lock;
143
144 __u8 bulk_out_endpoint; /* the bulk out endpoint handle */
145
146 __s16 rxBytesAvail; /* the number of bytes that we need to read from this device */
147
148 enum RXSTATE rxState; /* the current state of the bulk receive processor */
149 __u8 rxHeader1; /* receive header byte 1 */
150 __u8 rxHeader2; /* receive header byte 2 */
151 __u8 rxHeader3; /* receive header byte 3 */
152 __u8 rxPort; /* the port that we are currently receiving data for */
153 __u8 rxStatusCode; /* the receive status code */
154 __u8 rxStatusParam; /* the receive status paramater */
155 __s16 rxBytesRemaining; /* the number of port bytes left to read */
156 struct usb_serial *serial; /* loop back to the owner of this object */
157};
158
159/* baud rate information */
160struct divisor_table_entry {
161 __u32 BaudRate;
162 __u16 Divisor;
163};
164
Alan Cox03f0dbf2008-07-22 11:16:34 +0100165/*
166 * Define table of divisors for Rev A EdgePort/4 hardware
167 * These assume a 3.6864MHz crystal, the standard /16, and
168 * MCR.7 = 0.
169 */
170
Arjan van de Ven4c4c9432005-11-29 09:43:42 +0100171static const struct divisor_table_entry divisor_table[] = {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100172 { 50, 4608},
173 { 75, 3072},
174 { 110, 2095}, /* 2094.545455 => 230450 => .0217 % over */
175 { 134, 1713}, /* 1713.011152 => 230398.5 => .00065% under */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 { 150, 1536},
177 { 300, 768},
178 { 600, 384},
179 { 1200, 192},
180 { 1800, 128},
181 { 2400, 96},
182 { 4800, 48},
183 { 7200, 32},
184 { 9600, 24},
185 { 14400, 16},
186 { 19200, 12},
187 { 38400, 6},
188 { 57600, 4},
189 { 115200, 2},
190 { 230400, 1},
191};
192
Greg Kroah-Hartman36ccce42012-02-28 13:11:51 -0800193/* Number of outstanding Command Write Urbs */
194static atomic_t CmdUrbs = ATOMIC_INIT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196
197/* local function prototypes */
198
199/* function prototypes for all URB callbacks */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100200static void edge_interrupt_callback(struct urb *urb);
201static void edge_bulk_in_callback(struct urb *urb);
202static void edge_bulk_out_data_callback(struct urb *urb);
203static void edge_bulk_out_cmd_callback(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205/* function prototypes for the usbserial callbacks */
Alan Coxa509a7e2009-09-19 13:13:26 -0700206static int edge_open(struct tty_struct *tty, struct usb_serial_port *port);
Alan Cox335f8512009-06-11 12:26:29 +0100207static void edge_close(struct usb_serial_port *port);
Alan Cox03f0dbf2008-07-22 11:16:34 +0100208static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
209 const unsigned char *buf, int count);
210static int edge_write_room(struct tty_struct *tty);
211static int edge_chars_in_buffer(struct tty_struct *tty);
212static void edge_throttle(struct tty_struct *tty);
213static void edge_unthrottle(struct tty_struct *tty);
214static void edge_set_termios(struct tty_struct *tty,
215 struct usb_serial_port *port,
216 struct ktermios *old_termios);
Alan Cox00a0d0d2011-02-14 16:27:06 +0000217static int edge_ioctl(struct tty_struct *tty,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100218 unsigned int cmd, unsigned long arg);
219static void edge_break(struct tty_struct *tty, int break_state);
Alan Cox60b33c12011-02-14 16:26:14 +0000220static int edge_tiocmget(struct tty_struct *tty);
Alan Cox20b9d172011-02-14 16:26:50 +0000221static int edge_tiocmset(struct tty_struct *tty,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100222 unsigned int set, unsigned int clear);
Alan Cox0bca1b92010-09-16 18:21:40 +0100223static int edge_get_icount(struct tty_struct *tty,
224 struct serial_icounter_struct *icount);
Alan Cox03f0dbf2008-07-22 11:16:34 +0100225static int edge_startup(struct usb_serial *serial);
Alan Sternf9c99bb2009-06-02 11:53:55 -0400226static void edge_disconnect(struct usb_serial *serial);
227static void edge_release(struct usb_serial *serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229#include "io_tables.h" /* all of the devices that this driver supports */
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231/* function prototypes for all of our local functions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Alan Cox03f0dbf2008-07-22 11:16:34 +0100233static void process_rcvd_data(struct edgeport_serial *edge_serial,
234 unsigned char *buffer, __u16 bufferLength);
235static void process_rcvd_status(struct edgeport_serial *edge_serial,
236 __u8 byte2, __u8 byte3);
237static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
238 unsigned char *data, int length);
239static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr);
240static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
241 __u8 lsr, __u8 data);
242static int send_iosp_ext_cmd(struct edgeport_port *edge_port, __u8 command,
243 __u8 param);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700244static int calc_baud_rate_divisor(struct device *dev, int baud_rate, int *divisor);
Alan Cox03f0dbf2008-07-22 11:16:34 +0100245static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
246 int baudRate);
247static void change_port_settings(struct tty_struct *tty,
248 struct edgeport_port *edge_port,
249 struct ktermios *old_termios);
250static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
251 __u8 regNum, __u8 regValue);
252static int write_cmd_usb(struct edgeport_port *edge_port,
253 unsigned char *buffer, int writeLength);
254static void send_more_port_data(struct edgeport_serial *edge_serial,
255 struct edgeport_port *edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Alan Cox03f0dbf2008-07-22 11:16:34 +0100257static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
258 __u16 length, const __u8 *data);
259static int rom_read(struct usb_serial *serial, __u16 extAddr, __u16 addr,
260 __u16 length, __u8 *data);
261static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
262 __u16 length, const __u8 *data);
263static void get_manufacturing_desc(struct edgeport_serial *edge_serial);
264static void get_boot_desc(struct edgeport_serial *edge_serial);
265static void load_application_firmware(struct edgeport_serial *edge_serial);
266
267static void unicode_to_ascii(char *string, int buflen,
268 __le16 *unicode, int unicode_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270
Alan Cox03f0dbf2008-07-22 11:16:34 +0100271/* ************************************************************************ */
272/* ************************************************************************ */
273/* ************************************************************************ */
274/* ************************************************************************ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276/************************************************************************
277 * *
278 * update_edgeport_E2PROM() Compare current versions of *
279 * Boot ROM and Manufacture *
280 * Descriptors with versions *
281 * embedded in this driver *
282 * *
283 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100284static void update_edgeport_E2PROM(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700286 struct device *dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 __u32 BootCurVer;
288 __u32 BootNewVer;
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530289 __u8 BootMajorVersion;
290 __u8 BootMinorVersion;
291 __u16 BootBuildNumber;
292 __u32 Bootaddr;
293 const struct ihex_binrec *rec;
294 const struct firmware *fw;
295 const char *fw_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 int response;
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 switch (edge_serial->product_info.iDownloadFile) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100299 case EDGE_DOWNLOAD_FILE_I930:
300 fw_name = "edgeport/boot.fw";
301 break;
302 case EDGE_DOWNLOAD_FILE_80251:
303 fw_name = "edgeport/boot2.fw";
304 break;
305 default:
306 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
308
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530309 response = request_ihex_firmware(&fw, fw_name,
310 &edge_serial->serial->dev->dev);
311 if (response) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700312 dev_err(dev, "Failed to load image \"%s\" err %d\n",
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530313 fw_name, response);
314 return;
315 }
316
317 rec = (const struct ihex_binrec *)fw->data;
318 BootMajorVersion = rec->data[0];
319 BootMinorVersion = rec->data[1];
320 BootBuildNumber = (rec->data[2] << 8) | rec->data[3];
321
Alan Cox03f0dbf2008-07-22 11:16:34 +0100322 /* Check Boot Image Version */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 BootCurVer = (edge_serial->boot_descriptor.MajorVersion << 24) +
324 (edge_serial->boot_descriptor.MinorVersion << 16) +
325 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber);
326
327 BootNewVer = (BootMajorVersion << 24) +
328 (BootMinorVersion << 16) +
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530329 BootBuildNumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700331 dev_dbg(dev, "Current Boot Image version %d.%d.%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 edge_serial->boot_descriptor.MajorVersion,
333 edge_serial->boot_descriptor.MinorVersion,
334 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
335
336
337 if (BootNewVer > BootCurVer) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700338 dev_dbg(dev, "**Update Boot Image from %d.%d.%d to %d.%d.%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 edge_serial->boot_descriptor.MajorVersion,
340 edge_serial->boot_descriptor.MinorVersion,
341 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber),
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530342 BootMajorVersion, BootMinorVersion, BootBuildNumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700344 dev_dbg(dev, "Downloading new Boot Image\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530346 for (rec = ihex_next_binrec(rec); rec;
347 rec = ihex_next_binrec(rec)) {
348 Bootaddr = be32_to_cpu(rec->addr);
349 response = rom_write(edge_serial->serial,
350 Bootaddr >> 16,
351 Bootaddr & 0xFFFF,
352 be16_to_cpu(rec->len),
353 &rec->data[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (response < 0) {
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530355 dev_err(&edge_serial->serial->dev->dev,
356 "rom_write failed (%x, %x, %d)\n",
357 Bootaddr >> 16, Bootaddr & 0xFFFF,
358 be16_to_cpu(rec->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 break;
360 }
361 }
362 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700363 dev_dbg(dev, "Boot Image -- already up to date\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530365 release_firmware(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368#if 0
369/************************************************************************
370 *
371 * Get string descriptor from device
372 *
373 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100374static int get_string_desc(struct usb_device *dev, int Id,
375 struct usb_string_descriptor **pRetDesc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
377 struct usb_string_descriptor StringDesc;
378 struct usb_string_descriptor *pStringDesc;
379
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700380 dev_dbg(&dev->dev, "%s - USB String ID = %d\n", __func__, Id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Alan Cox03f0dbf2008-07-22 11:16:34 +0100382 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc,
383 sizeof(StringDesc)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Alan Cox03f0dbf2008-07-22 11:16:34 +0100386 pStringDesc = kmalloc(StringDesc.bLength, GFP_KERNEL);
387 if (!pStringDesc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Alan Cox03f0dbf2008-07-22 11:16:34 +0100390 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc,
391 StringDesc.bLength)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 kfree(pStringDesc);
393 return -1;
394 }
395
396 *pRetDesc = pStringDesc;
397 return 0;
398}
399#endif
400
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700401static void dump_product_info(struct edgeport_serial *edge_serial,
402 struct edgeport_product_info *product_info)
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800403{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700404 struct device *dev = &edge_serial->serial->dev->dev;
405
Alan Cox03f0dbf2008-07-22 11:16:34 +0100406 /* Dump Product Info structure */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700407 dev_dbg(dev, "**Product Information:\n");
408 dev_dbg(dev, " ProductId %x\n", product_info->ProductId);
409 dev_dbg(dev, " NumPorts %d\n", product_info->NumPorts);
410 dev_dbg(dev, " ProdInfoVer %d\n", product_info->ProdInfoVer);
411 dev_dbg(dev, " IsServer %d\n", product_info->IsServer);
412 dev_dbg(dev, " IsRS232 %d\n", product_info->IsRS232);
413 dev_dbg(dev, " IsRS422 %d\n", product_info->IsRS422);
414 dev_dbg(dev, " IsRS485 %d\n", product_info->IsRS485);
415 dev_dbg(dev, " RomSize %d\n", product_info->RomSize);
416 dev_dbg(dev, " RamSize %d\n", product_info->RamSize);
417 dev_dbg(dev, " CpuRev %x\n", product_info->CpuRev);
418 dev_dbg(dev, " BoardRev %x\n", product_info->BoardRev);
419 dev_dbg(dev, " BootMajorVersion %d.%d.%d\n",
420 product_info->BootMajorVersion,
421 product_info->BootMinorVersion,
422 le16_to_cpu(product_info->BootBuildNumber));
423 dev_dbg(dev, " FirmwareMajorVersion %d.%d.%d\n",
424 product_info->FirmwareMajorVersion,
425 product_info->FirmwareMinorVersion,
426 le16_to_cpu(product_info->FirmwareBuildNumber));
427 dev_dbg(dev, " ManufactureDescDate %d/%d/%d\n",
428 product_info->ManufactureDescDate[0],
429 product_info->ManufactureDescDate[1],
430 product_info->ManufactureDescDate[2]+1900);
431 dev_dbg(dev, " iDownloadFile 0x%x\n",
432 product_info->iDownloadFile);
433 dev_dbg(dev, " EpicVer %d\n", product_info->EpicVer);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800434}
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436static void get_product_info(struct edgeport_serial *edge_serial)
437{
438 struct edgeport_product_info *product_info = &edge_serial->product_info;
439
Alan Cox03f0dbf2008-07-22 11:16:34 +0100440 memset(product_info, 0, sizeof(struct edgeport_product_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Alan Cox03f0dbf2008-07-22 11:16:34 +0100442 product_info->ProductId = (__u16)(le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct) & ~ION_DEVICE_ID_80251_NETCHIP);
443 product_info->NumPorts = edge_serial->manuf_descriptor.NumPorts;
444 product_info->ProdInfoVer = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Alan Cox03f0dbf2008-07-22 11:16:34 +0100446 product_info->RomSize = edge_serial->manuf_descriptor.RomSize;
447 product_info->RamSize = edge_serial->manuf_descriptor.RamSize;
448 product_info->CpuRev = edge_serial->manuf_descriptor.CpuRev;
449 product_info->BoardRev = edge_serial->manuf_descriptor.BoardRev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Alan Cox03f0dbf2008-07-22 11:16:34 +0100451 product_info->BootMajorVersion =
452 edge_serial->boot_descriptor.MajorVersion;
453 product_info->BootMinorVersion =
454 edge_serial->boot_descriptor.MinorVersion;
455 product_info->BootBuildNumber =
456 edge_serial->boot_descriptor.BuildNumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Alan Cox03f0dbf2008-07-22 11:16:34 +0100458 memcpy(product_info->ManufactureDescDate,
459 edge_serial->manuf_descriptor.DescDate,
460 sizeof(edge_serial->manuf_descriptor.DescDate));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Alan Cox03f0dbf2008-07-22 11:16:34 +0100462 /* check if this is 2nd generation hardware */
463 if (le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct)
464 & ION_DEVICE_ID_80251_NETCHIP)
465 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_80251;
466 else
467 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_I930;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700468
Alan Cox03f0dbf2008-07-22 11:16:34 +0100469 /* Determine Product type and set appropriate flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 switch (DEVICE_ID_FROM_USB_PRODUCT_ID(product_info->ProductId)) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100471 case ION_DEVICE_ID_EDGEPORT_COMPATIBLE:
472 case ION_DEVICE_ID_EDGEPORT_4T:
473 case ION_DEVICE_ID_EDGEPORT_4:
474 case ION_DEVICE_ID_EDGEPORT_2:
475 case ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU:
476 case ION_DEVICE_ID_EDGEPORT_8:
477 case ION_DEVICE_ID_EDGEPORT_421:
478 case ION_DEVICE_ID_EDGEPORT_21:
479 case ION_DEVICE_ID_EDGEPORT_2_DIN:
480 case ION_DEVICE_ID_EDGEPORT_4_DIN:
481 case ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU:
482 product_info->IsRS232 = 1;
483 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Alan Cox03f0dbf2008-07-22 11:16:34 +0100485 case ION_DEVICE_ID_EDGEPORT_2I: /* Edgeport/2 RS422/RS485 */
486 product_info->IsRS422 = 1;
487 product_info->IsRS485 = 1;
488 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Alan Cox03f0dbf2008-07-22 11:16:34 +0100490 case ION_DEVICE_ID_EDGEPORT_8I: /* Edgeport/4 RS422 */
491 case ION_DEVICE_ID_EDGEPORT_4I: /* Edgeport/4 RS422 */
492 product_info->IsRS422 = 1;
493 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
495
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700496 dump_product_info(edge_serial, product_info);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800497}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800499static int get_epic_descriptor(struct edgeport_serial *ep)
500{
501 int result;
502 struct usb_serial *serial = ep->serial;
503 struct edgeport_product_info *product_info = &ep->product_info;
504 struct edge_compatibility_descriptor *epic = &ep->epic_descriptor;
505 struct edge_compatibility_bits *bits;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700506 struct device *dev = &serial->dev->dev;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800507
508 ep->is_epic = 0;
509 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
510 USB_REQUEST_ION_GET_EPIC_DESC,
511 0xC0, 0x00, 0x00,
512 &ep->epic_descriptor,
513 sizeof(struct edge_compatibility_descriptor),
514 300);
515
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800516 if (result > 0) {
517 ep->is_epic = 1;
518 memset(product_info, 0, sizeof(struct edgeport_product_info));
519
Alan Cox03f0dbf2008-07-22 11:16:34 +0100520 product_info->NumPorts = epic->NumPorts;
521 product_info->ProdInfoVer = 0;
522 product_info->FirmwareMajorVersion = epic->MajorVersion;
523 product_info->FirmwareMinorVersion = epic->MinorVersion;
524 product_info->FirmwareBuildNumber = epic->BuildNumber;
525 product_info->iDownloadFile = epic->iDownloadFile;
526 product_info->EpicVer = epic->EpicVer;
527 product_info->Epic = epic->Supports;
528 product_info->ProductId = ION_DEVICE_ID_EDGEPORT_COMPATIBLE;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700529 dump_product_info(ep, product_info);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800530
531 bits = &ep->epic_descriptor.Supports;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700532 dev_dbg(dev, "**EPIC descriptor:\n");
533 dev_dbg(dev, " VendEnableSuspend: %s\n", bits->VendEnableSuspend ? "TRUE": "FALSE");
534 dev_dbg(dev, " IOSPOpen : %s\n", bits->IOSPOpen ? "TRUE": "FALSE");
535 dev_dbg(dev, " IOSPClose : %s\n", bits->IOSPClose ? "TRUE": "FALSE");
536 dev_dbg(dev, " IOSPChase : %s\n", bits->IOSPChase ? "TRUE": "FALSE");
537 dev_dbg(dev, " IOSPSetRxFlow : %s\n", bits->IOSPSetRxFlow ? "TRUE": "FALSE");
538 dev_dbg(dev, " IOSPSetTxFlow : %s\n", bits->IOSPSetTxFlow ? "TRUE": "FALSE");
539 dev_dbg(dev, " IOSPSetXChar : %s\n", bits->IOSPSetXChar ? "TRUE": "FALSE");
540 dev_dbg(dev, " IOSPRxCheck : %s\n", bits->IOSPRxCheck ? "TRUE": "FALSE");
541 dev_dbg(dev, " IOSPSetClrBreak : %s\n", bits->IOSPSetClrBreak ? "TRUE": "FALSE");
542 dev_dbg(dev, " IOSPWriteMCR : %s\n", bits->IOSPWriteMCR ? "TRUE": "FALSE");
543 dev_dbg(dev, " IOSPWriteLCR : %s\n", bits->IOSPWriteLCR ? "TRUE": "FALSE");
544 dev_dbg(dev, " IOSPSetBaudRate : %s\n", bits->IOSPSetBaudRate ? "TRUE": "FALSE");
545 dev_dbg(dev, " TrueEdgeport : %s\n", bits->TrueEdgeport ? "TRUE": "FALSE");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800546 }
547
548 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
551
552/************************************************************************/
553/************************************************************************/
554/* U S B C A L L B A C K F U N C T I O N S */
555/* U S B C A L L B A C K F U N C T I O N S */
556/************************************************************************/
557/************************************************************************/
558
559/*****************************************************************************
560 * edge_interrupt_callback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100561 * this is the callback function for when we have received data on the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 * interrupt endpoint.
563 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100564static void edge_interrupt_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700566 struct edgeport_serial *edge_serial = urb->context;
567 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 struct edgeport_port *edge_port;
569 struct usb_serial_port *port;
Alan Cox4a90f092008-10-13 10:39:46 +0100570 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 unsigned char *data = urb->transfer_buffer;
572 int length = urb->actual_length;
573 int bytes_avail;
574 int position;
575 int txCredits;
576 int portNumber;
577 int result;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700578 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700580 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 case 0:
582 /* success */
583 break;
584 case -ECONNRESET:
585 case -ENOENT:
586 case -ESHUTDOWN:
587 /* this urb is terminated, clean up */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700588 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return;
590 default:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700591 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 goto exit;
593 }
594
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700595 dev = &edge_serial->serial->dev->dev;
596
Alan Cox03f0dbf2008-07-22 11:16:34 +0100597 /* process this interrupt-read even if there are no ports open */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 if (length) {
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100599 usb_serial_debug_data(dev, __func__, length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 if (length > 1) {
602 bytes_avail = data[0] | (data[1] << 8);
603 if (bytes_avail) {
604 spin_lock(&edge_serial->es_lock);
605 edge_serial->rxBytesAvail += bytes_avail;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700606 dev_dbg(dev,
607 "%s - bytes_avail=%d, rxBytesAvail=%d, read_in_progress=%d\n",
608 __func__, bytes_avail,
609 edge_serial->rxBytesAvail,
610 edge_serial->read_in_progress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 if (edge_serial->rxBytesAvail > 0 &&
613 !edge_serial->read_in_progress) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700614 dev_dbg(dev, "%s - posting a read\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100615 edge_serial->read_in_progress = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Alan Cox03f0dbf2008-07-22 11:16:34 +0100617 /* we have pending bytes on the
618 bulk in pipe, send a request */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 result = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
620 if (result) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700621 dev_err(dev,
622 "%s - usb_submit_urb(read bulk) failed with result = %d\n",
623 __func__, result);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100624 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
626 }
627 spin_unlock(&edge_serial->es_lock);
628 }
629 }
630 /* grab the txcredits for the ports if available */
631 position = 2;
632 portNumber = 0;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100633 while ((position < length) &&
634 (portNumber < edge_serial->serial->num_ports)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 txCredits = data[position] | (data[position+1] << 8);
636 if (txCredits) {
637 port = edge_serial->serial->port[portNumber];
638 edge_port = usb_get_serial_port_data(port);
639 if (edge_port->open) {
640 spin_lock(&edge_port->ep_lock);
641 edge_port->txCredits += txCredits;
642 spin_unlock(&edge_port->ep_lock);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700643 dev_dbg(dev, "%s - txcredits for port%d = %d\n",
644 __func__, portNumber,
645 edge_port->txCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Alan Cox03f0dbf2008-07-22 11:16:34 +0100647 /* tell the tty driver that something
648 has changed */
Alan Cox4a90f092008-10-13 10:39:46 +0100649 tty = tty_port_tty_get(
650 &edge_port->port->port);
651 if (tty) {
652 tty_wakeup(tty);
653 tty_kref_put(tty);
654 }
Alan Cox03f0dbf2008-07-22 11:16:34 +0100655 /* Since we have more credit, check
656 if more data can be sent */
657 send_more_port_data(edge_serial,
658 edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
660 }
661 position += 2;
662 ++portNumber;
663 }
664 }
665
666exit:
Alan Cox03f0dbf2008-07-22 11:16:34 +0100667 result = usb_submit_urb(urb, GFP_ATOMIC);
668 if (result)
669 dev_err(&urb->dev->dev,
670 "%s - Error %d submitting control urb\n",
671 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
673
674
675/*****************************************************************************
676 * edge_bulk_in_callback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100677 * this is the callback function for when we have received data on the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 * bulk in endpoint.
679 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100680static void edge_bulk_in_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Ming Leicdc97792008-02-24 18:41:47 +0800682 struct edgeport_serial *edge_serial = urb->context;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700683 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700685 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 __u16 raw_data_length;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700687 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700689 if (status) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700690 dev_dbg(&urb->dev->dev, "%s - nonzero read bulk status received: %d\n",
691 __func__, status);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100692 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return;
694 }
695
696 if (urb->actual_length == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700697 dev_dbg(&urb->dev->dev, "%s - read bulk callback with no data\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100698 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return;
700 }
701
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700702 dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 raw_data_length = urb->actual_length;
704
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100705 usb_serial_debug_data(dev, __func__, raw_data_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 spin_lock(&edge_serial->es_lock);
708
709 /* decrement our rxBytes available by the number that we just got */
710 edge_serial->rxBytesAvail -= raw_data_length;
711
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700712 dev_dbg(dev, "%s - Received = %d, rxBytesAvail %d\n", __func__,
713 raw_data_length, edge_serial->rxBytesAvail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Alan Cox03f0dbf2008-07-22 11:16:34 +0100715 process_rcvd_data(edge_serial, data, urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 /* check to see if there's any more data for us to read */
718 if (edge_serial->rxBytesAvail > 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700719 dev_dbg(dev, "%s - posting a read\n", __func__);
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700720 retval = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
721 if (retval) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700722 dev_err(dev,
723 "%s - usb_submit_urb(read bulk) failed, retval = %d\n",
724 __func__, retval);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100725 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
727 } else {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100728 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
730
731 spin_unlock(&edge_serial->es_lock);
732}
733
734
735/*****************************************************************************
736 * edge_bulk_out_data_callback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100737 * this is the callback function for when we have finished sending
738 * serial data on the bulk out endpoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100740static void edge_bulk_out_data_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
Ming Leicdc97792008-02-24 18:41:47 +0800742 struct edgeport_port *edge_port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 struct tty_struct *tty;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700744 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700746 if (status) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700747 dev_dbg(&urb->dev->dev,
748 "%s - nonzero write bulk status received: %d\n",
749 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 }
751
Alan Cox4a90f092008-10-13 10:39:46 +0100752 tty = tty_port_tty_get(&edge_port->port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 if (tty && edge_port->open) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100755 /* let the tty driver wakeup if it has a special
756 write_wakeup function */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 tty_wakeup(tty);
758 }
Alan Cox4a90f092008-10-13 10:39:46 +0100759 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Alan Cox03f0dbf2008-07-22 11:16:34 +0100761 /* Release the Write URB */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100762 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Alan Cox03f0dbf2008-07-22 11:16:34 +0100764 /* Check if more data needs to be sent */
765 send_more_port_data((struct edgeport_serial *)
766 (usb_get_serial_data(edge_port->port->serial)), edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
769
770/*****************************************************************************
771 * BulkOutCmdCallback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100772 * this is the callback function for when we have finished sending a
773 * command on the bulk out endpoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100775static void edge_bulk_out_cmd_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
Ming Leicdc97792008-02-24 18:41:47 +0800777 struct edgeport_port *edge_port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 struct tty_struct *tty;
779 int status = urb->status;
780
Oliver Neukum96c706e2007-03-15 15:27:17 +0100781 atomic_dec(&CmdUrbs);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700782 dev_dbg(&urb->dev->dev, "%s - FREE URB %p (outstanding %d)\n",
783 __func__, urb, atomic_read(&CmdUrbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785
786 /* clean up the transfer buffer */
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700787 kfree(urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 /* Free the command urb */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100790 usb_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 if (status) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700793 dev_dbg(&urb->dev->dev,
794 "%s - nonzero write bulk status received: %d\n",
795 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return;
797 }
798
799 /* Get pointer to tty */
Alan Cox4a90f092008-10-13 10:39:46 +0100800 tty = tty_port_tty_get(&edge_port->port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 /* tell the tty driver that something has changed */
803 if (tty && edge_port->open)
804 tty_wakeup(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100805 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807 /* we have completed the command */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100808 edge_port->commandPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 wake_up(&edge_port->wait_command);
810}
811
812
813/*****************************************************************************
814 * Driver tty interface functions
815 *****************************************************************************/
816
817/*****************************************************************************
818 * SerialOpen
819 * this function is called by the tty driver when a port is opened
820 * If successful, we return 0
821 * Otherwise we return a negative error number.
822 *****************************************************************************/
Alan Coxa509a7e2009-09-19 13:13:26 -0700823static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
825 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700826 struct device *dev = &port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 struct usb_serial *serial;
828 struct edgeport_serial *edge_serial;
829 int response;
830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if (edge_port == NULL)
832 return -ENODEV;
833
Alan Cox03f0dbf2008-07-22 11:16:34 +0100834 /* see if we've set up our endpoint info yet (can't set it up
835 in edge_startup as the structures were not set up at that time.) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 serial = port->serial;
837 edge_serial = usb_get_serial_data(serial);
Alan Cox95da3102008-07-22 11:09:07 +0100838 if (edge_serial == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 if (edge_serial->interrupt_in_buffer == NULL) {
841 struct usb_serial_port *port0 = serial->port[0];
Alan Cox03f0dbf2008-07-22 11:16:34 +0100842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 /* not set up yet, so do it now */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100844 edge_serial->interrupt_in_buffer =
845 port0->interrupt_in_buffer;
846 edge_serial->interrupt_in_endpoint =
847 port0->interrupt_in_endpointAddress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 edge_serial->interrupt_read_urb = port0->interrupt_in_urb;
849 edge_serial->bulk_in_buffer = port0->bulk_in_buffer;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100850 edge_serial->bulk_in_endpoint =
851 port0->bulk_in_endpointAddress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 edge_serial->read_urb = port0->read_urb;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100853 edge_serial->bulk_out_endpoint =
854 port0->bulk_out_endpointAddress;
855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 /* set up our interrupt urb */
857 usb_fill_int_urb(edge_serial->interrupt_read_urb,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100858 serial->dev,
859 usb_rcvintpipe(serial->dev,
860 port0->interrupt_in_endpointAddress),
861 port0->interrupt_in_buffer,
862 edge_serial->interrupt_read_urb->transfer_buffer_length,
863 edge_interrupt_callback, edge_serial,
864 edge_serial->interrupt_read_urb->interval);
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 /* set up our bulk in urb */
867 usb_fill_bulk_urb(edge_serial->read_urb, serial->dev,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100868 usb_rcvbulkpipe(serial->dev,
869 port0->bulk_in_endpointAddress),
870 port0->bulk_in_buffer,
871 edge_serial->read_urb->transfer_buffer_length,
872 edge_bulk_in_callback, edge_serial);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100873 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 /* start interrupt read for this edgeport
Alan Cox03f0dbf2008-07-22 11:16:34 +0100876 * this interrupt will continue as long
877 * as the edgeport is connected */
878 response = usb_submit_urb(edge_serial->interrupt_read_urb,
879 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (response) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700881 dev_err(dev, "%s - Error %d submitting control urb\n",
882 __func__, response);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 }
884 }
Alan Cox03f0dbf2008-07-22 11:16:34 +0100885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 /* initialize our wait queues */
887 init_waitqueue_head(&edge_port->wait_open);
888 init_waitqueue_head(&edge_port->wait_chase);
889 init_waitqueue_head(&edge_port->delta_msr_wait);
890 init_waitqueue_head(&edge_port->wait_command);
891
892 /* initialize our icount structure */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100893 memset(&(edge_port->icount), 0x00, sizeof(edge_port->icount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 /* initialize our port settings */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100896 edge_port->txCredits = 0; /* Can't send any data yet */
897 /* Must always set this bit to enable ints! */
898 edge_port->shadowMCR = MCR_MASTER_IE;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100899 edge_port->chaseResponsePending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 /* send a open port command */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100902 edge_port->openPending = true;
903 edge_port->open = false;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100904 response = send_iosp_ext_cmd(edge_port, IOSP_CMD_OPEN_PORT, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 if (response < 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700907 dev_err(dev, "%s - error sending open port command\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100908 edge_port->openPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return -ENODEV;
910 }
911
912 /* now wait for the port to be completely opened */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100913 wait_event_timeout(edge_port->wait_open, !edge_port->openPending,
914 OPEN_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100916 if (!edge_port->open) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 /* open timed out */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700918 dev_dbg(dev, "%s - open timedout\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100919 edge_port->openPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 return -ENODEV;
921 }
922
923 /* create the txfifo */
924 edge_port->txfifo.head = 0;
925 edge_port->txfifo.tail = 0;
926 edge_port->txfifo.count = 0;
927 edge_port->txfifo.size = edge_port->maxTxCredits;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100928 edge_port->txfifo.fifo = kmalloc(edge_port->maxTxCredits, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
930 if (!edge_port->txfifo.fifo) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700931 dev_dbg(dev, "%s - no memory\n", __func__);
Alan Cox335f8512009-06-11 12:26:29 +0100932 edge_close(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 return -ENOMEM;
934 }
935
936 /* Allocate a URB for the write */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100937 edge_port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100938 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 if (!edge_port->write_urb) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700941 dev_dbg(dev, "%s - no memory\n", __func__);
Alan Cox335f8512009-06-11 12:26:29 +0100942 edge_close(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 return -ENOMEM;
944 }
945
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700946 dev_dbg(dev, "%s(%d) - Initialize TX fifo to %d bytes\n",
947 __func__, port->number, edge_port->maxTxCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 return 0;
950}
951
952
953/************************************************************************
954 *
955 * block_until_chase_response
956 *
957 * This function will block the close until one of the following:
958 * 1. Response to our Chase comes from Edgeport
Joe Perchesdc0d5c12007-12-17 11:40:18 -0800959 * 2. A timeout of 10 seconds without activity has expired
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 * (1K of Edgeport data @ 2400 baud ==> 4 sec to empty)
961 *
962 ************************************************************************/
963static void block_until_chase_response(struct edgeport_port *edge_port)
964{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700965 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 DEFINE_WAIT(wait);
967 __u16 lastCredits;
968 int timeout = 1*HZ;
969 int loop = 10;
970
971 while (1) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100972 /* Save Last credits */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 lastCredits = edge_port->txCredits;
974
Alan Cox03f0dbf2008-07-22 11:16:34 +0100975 /* Did we get our Chase response */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100976 if (!edge_port->chaseResponsePending) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700977 dev_dbg(dev, "%s - Got Chase Response\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Alan Cox03f0dbf2008-07-22 11:16:34 +0100979 /* did we get all of our credit back? */
980 if (edge_port->txCredits == edge_port->maxTxCredits) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700981 dev_dbg(dev, "%s - Got all credits\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 return;
983 }
984 }
985
Alan Cox03f0dbf2008-07-22 11:16:34 +0100986 /* Block the thread for a while */
987 prepare_to_wait(&edge_port->wait_chase, &wait,
988 TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 schedule_timeout(timeout);
990 finish_wait(&edge_port->wait_chase, &wait);
991
992 if (lastCredits == edge_port->txCredits) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100993 /* No activity.. count down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 loop--;
995 if (loop == 0) {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100996 edge_port->chaseResponsePending = false;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700997 dev_dbg(dev, "%s - Chase TIMEOUT\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 return;
999 }
1000 } else {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001001 /* Reset timeout value back to 10 seconds */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001002 dev_dbg(dev, "%s - Last %d, Current %d\n", __func__,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001003 lastCredits, edge_port->txCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 loop = 10;
1005 }
1006 }
1007}
1008
1009
1010/************************************************************************
1011 *
1012 * block_until_tx_empty
1013 *
1014 * This function will block the close until one of the following:
1015 * 1. TX count are 0
1016 * 2. The edgeport has stopped
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001017 * 3. A timeout of 3 seconds without activity has expired
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 *
1019 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001020static void block_until_tx_empty(struct edgeport_port *edge_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001022 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 DEFINE_WAIT(wait);
1024 struct TxFifo *fifo = &edge_port->txfifo;
1025 __u32 lastCount;
1026 int timeout = HZ/10;
1027 int loop = 30;
1028
1029 while (1) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001030 /* Save Last count */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 lastCount = fifo->count;
1032
Alan Cox03f0dbf2008-07-22 11:16:34 +01001033 /* Is the Edgeport Buffer empty? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 if (lastCount == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001035 dev_dbg(dev, "%s - TX Buffer Empty\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 return;
1037 }
1038
Alan Cox03f0dbf2008-07-22 11:16:34 +01001039 /* Block the thread for a while */
1040 prepare_to_wait(&edge_port->wait_chase, &wait,
1041 TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 schedule_timeout(timeout);
1043 finish_wait(&edge_port->wait_chase, &wait);
1044
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001045 dev_dbg(dev, "%s wait\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 if (lastCount == fifo->count) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001048 /* No activity.. count down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 loop--;
1050 if (loop == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001051 dev_dbg(dev, "%s - TIMEOUT\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 return;
1053 }
1054 } else {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001055 /* Reset timeout value back to seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 loop = 30;
1057 }
1058 }
1059}
1060
1061
1062/*****************************************************************************
1063 * edge_close
1064 * this function is called by the tty driver when a port is closed
1065 *****************************************************************************/
Alan Cox335f8512009-06-11 12:26:29 +01001066static void edge_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
1068 struct edgeport_serial *edge_serial;
1069 struct edgeport_port *edge_port;
1070 int status;
1071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 edge_serial = usb_get_serial_data(port->serial);
1073 edge_port = usb_get_serial_port_data(port);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001074 if (edge_serial == NULL || edge_port == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 return;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001076
1077 /* block until tx is empty */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 block_until_tx_empty(edge_port);
1079
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001080 edge_port->closePending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001082 if ((!edge_serial->is_epic) ||
1083 ((edge_serial->is_epic) &&
1084 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1085 /* flush and chase */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001086 edge_port->chaseResponsePending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001088 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001089 status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
1090 if (status == 0)
1091 /* block until chase finished */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001092 block_until_chase_response(edge_port);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001093 else
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001094 edge_port->chaseResponsePending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 }
1096
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001097 if ((!edge_serial->is_epic) ||
1098 ((edge_serial->is_epic) &&
1099 (edge_serial->epic_descriptor.Supports.IOSPClose))) {
1100 /* close the port */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001101 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CLOSE_PORT\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001102 send_iosp_ext_cmd(edge_port, IOSP_CMD_CLOSE_PORT, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Alan Cox03f0dbf2008-07-22 11:16:34 +01001105 /* port->close = true; */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001106 edge_port->closePending = false;
1107 edge_port->open = false;
1108 edge_port->openPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Mariusz Kozlowski9a25f442006-11-08 15:36:29 +01001110 usb_kill_urb(edge_port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 if (edge_port->write_urb) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001113 /* if this urb had a transfer buffer already
1114 (old transfer) free it */
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001115 kfree(edge_port->write_urb->transfer_buffer);
1116 usb_free_urb(edge_port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 edge_port->write_urb = NULL;
1118 }
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001119 kfree(edge_port->txfifo.fifo);
1120 edge_port->txfifo.fifo = NULL;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001121}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123/*****************************************************************************
1124 * SerialWrite
Alan Cox03f0dbf2008-07-22 11:16:34 +01001125 * this function is called by the tty driver when data should be written
1126 * to the port.
1127 * If successful, we return the number of bytes written, otherwise we
1128 * return a negative error number.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001130static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
1131 const unsigned char *data, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
1133 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1134 struct TxFifo *fifo;
1135 int copySize;
1136 int bytesleft;
1137 int firsthalf;
1138 int secondhalf;
1139 unsigned long flags;
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 if (edge_port == NULL)
1142 return -ENODEV;
1143
Alan Cox03f0dbf2008-07-22 11:16:34 +01001144 /* get a pointer to the Tx fifo */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 fifo = &edge_port->txfifo;
1146
1147 spin_lock_irqsave(&edge_port->ep_lock, flags);
1148
Alan Cox03f0dbf2008-07-22 11:16:34 +01001149 /* calculate number of bytes to put in fifo */
1150 copySize = min((unsigned int)count,
1151 (edge_port->txCredits - fifo->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001153 dev_dbg(&port->dev, "%s(%d) of %d byte(s) Fifo room %d -- will copy %d bytes\n",
1154 __func__, port->number, count,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001155 edge_port->txCredits - fifo->count, copySize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Alan Cox03f0dbf2008-07-22 11:16:34 +01001157 /* catch writes of 0 bytes which the tty driver likes to give us,
1158 and when txCredits is empty */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 if (copySize == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001160 dev_dbg(&port->dev, "%s - copySize = Zero\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 goto finish_write;
1162 }
1163
Alan Cox03f0dbf2008-07-22 11:16:34 +01001164 /* queue the data
1165 * since we can never overflow the buffer we do not have to check for a
1166 * full condition
1167 *
1168 * the copy is done is two parts -- first fill to the end of the buffer
1169 * then copy the reset from the start of the buffer
1170 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 bytesleft = fifo->size - fifo->head;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001172 firsthalf = min(bytesleft, copySize);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001173 dev_dbg(&port->dev, "%s - copy %d bytes of %d into fifo \n", __func__,
1174 firsthalf, bytesleft);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 /* now copy our data */
1177 memcpy(&fifo->fifo[fifo->head], data, firsthalf);
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001178 usb_serial_debug_data(&port->dev, __func__, firsthalf, &fifo->fifo[fifo->head]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Alan Cox03f0dbf2008-07-22 11:16:34 +01001180 /* update the index and size */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 fifo->head += firsthalf;
1182 fifo->count += firsthalf;
1183
Alan Cox03f0dbf2008-07-22 11:16:34 +01001184 /* wrap the index */
1185 if (fifo->head == fifo->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 fifo->head = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188 secondhalf = copySize-firsthalf;
1189
1190 if (secondhalf) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001191 dev_dbg(&port->dev, "%s - copy rest of data %d\n", __func__, secondhalf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 memcpy(&fifo->fifo[fifo->head], &data[firsthalf], secondhalf);
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001193 usb_serial_debug_data(&port->dev, __func__, secondhalf, &fifo->fifo[fifo->head]);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001194 /* update the index and size */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 fifo->count += secondhalf;
1196 fifo->head += secondhalf;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001197 /* No need to check for wrap since we can not get to end of
1198 * the fifo in this part
1199 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 }
1201
1202finish_write:
1203 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1204
Alan Cox03f0dbf2008-07-22 11:16:34 +01001205 send_more_port_data((struct edgeport_serial *)
1206 usb_get_serial_data(port->serial), edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001208 dev_dbg(&port->dev, "%s wrote %d byte(s) TxCredits %d, Fifo %d\n",
1209 __func__, copySize, edge_port->txCredits, fifo->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Alan Cox03f0dbf2008-07-22 11:16:34 +01001211 return copySize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
1213
1214
1215/************************************************************************
1216 *
1217 * send_more_port_data()
1218 *
1219 * This routine attempts to write additional UART transmit data
1220 * to a port over the USB bulk pipe. It is called (1) when new
1221 * data has been written to a port's TxBuffer from higher layers
1222 * (2) when the peripheral sends us additional TxCredits indicating
1223 * that it can accept more Tx data for a given port; and (3) when
1224 * a bulk write completes successfully and we want to see if we
1225 * can transmit more.
1226 *
1227 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001228static void send_more_port_data(struct edgeport_serial *edge_serial,
1229 struct edgeport_port *edge_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
1231 struct TxFifo *fifo = &edge_port->txfifo;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001232 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 struct urb *urb;
1234 unsigned char *buffer;
1235 int status;
1236 int count;
1237 int bytesleft;
1238 int firsthalf;
1239 int secondhalf;
1240 unsigned long flags;
1241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 spin_lock_irqsave(&edge_port->ep_lock, flags);
1243
1244 if (edge_port->write_in_progress ||
1245 !edge_port->open ||
1246 (fifo->count == 0)) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001247 dev_dbg(dev, "%s(%d) EXIT - fifo %d, PendingWrite = %d\n",
1248 __func__, edge_port->port->number,
1249 fifo->count, edge_port->write_in_progress);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 goto exit_send;
1251 }
1252
Alan Cox03f0dbf2008-07-22 11:16:34 +01001253 /* since the amount of data in the fifo will always fit into the
1254 * edgeport buffer we do not need to check the write length
1255 *
1256 * Do we have enough credits for this port to make it worthwhile
1257 * to bother queueing a write. If it's too small, say a few bytes,
1258 * it's better to wait for more credits so we can do a larger write.
1259 */
1260 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 -07001261 dev_dbg(dev, "%s(%d) Not enough credit - fifo %d TxCredit %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01001262 __func__, edge_port->port->number, fifo->count,
1263 edge_port->txCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 goto exit_send;
1265 }
1266
Alan Cox03f0dbf2008-07-22 11:16:34 +01001267 /* lock this write */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001268 edge_port->write_in_progress = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Alan Cox03f0dbf2008-07-22 11:16:34 +01001270 /* get a pointer to the write_urb */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 urb = edge_port->write_urb;
1272
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001273 /* make sure transfer buffer is freed */
1274 kfree(urb->transfer_buffer);
1275 urb->transfer_buffer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Alan Cox03f0dbf2008-07-22 11:16:34 +01001277 /* build the data header for the buffer and port that we are about
1278 to send out */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 count = fifo->count;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001280 buffer = kmalloc(count+2, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 if (buffer == NULL) {
Johan Hovold22a416c2012-02-10 13:20:51 +01001282 dev_err_console(edge_port->port,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001283 "%s - no more kernel memory...\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001284 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 goto exit_send;
1286 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01001287 buffer[0] = IOSP_BUILD_DATA_HDR1(edge_port->port->number
1288 - edge_port->port->serial->minor, count);
1289 buffer[1] = IOSP_BUILD_DATA_HDR2(edge_port->port->number
1290 - edge_port->port->serial->minor, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292 /* now copy our data */
1293 bytesleft = fifo->size - fifo->tail;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001294 firsthalf = min(bytesleft, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 memcpy(&buffer[2], &fifo->fifo[fifo->tail], firsthalf);
1296 fifo->tail += firsthalf;
1297 fifo->count -= firsthalf;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001298 if (fifo->tail == fifo->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 fifo->tail = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 secondhalf = count-firsthalf;
1302 if (secondhalf) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001303 memcpy(&buffer[2+firsthalf], &fifo->fifo[fifo->tail],
1304 secondhalf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 fifo->tail += secondhalf;
1306 fifo->count -= secondhalf;
1307 }
1308
1309 if (count)
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001310 usb_serial_debug_data(&edge_port->port->dev, __func__, count, &buffer[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
1312 /* fill up the urb with all of our data and submit it */
Alan Cox03f0dbf2008-07-22 11:16:34 +01001313 usb_fill_bulk_urb(urb, edge_serial->serial->dev,
1314 usb_sndbulkpipe(edge_serial->serial->dev,
1315 edge_serial->bulk_out_endpoint),
1316 buffer, count+2,
1317 edge_bulk_out_data_callback, edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319 /* decrement the number of credits we have by the number we just sent */
1320 edge_port->txCredits -= count;
1321 edge_port->icount.tx += count;
1322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 status = usb_submit_urb(urb, GFP_ATOMIC);
1324 if (status) {
1325 /* something went wrong */
Johan Hovold22a416c2012-02-10 13:20:51 +01001326 dev_err_console(edge_port->port,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001327 "%s - usb_submit_urb(write bulk) failed, status = %d, data lost\n",
1328 __func__, status);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001329 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 /* revert the credits as something bad happened. */
1332 edge_port->txCredits += count;
1333 edge_port->icount.tx -= count;
1334 }
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001335 dev_dbg(dev, "%s wrote %d byte(s) TxCredit %d, Fifo %d\n",
1336 __func__, count, edge_port->txCredits, fifo->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338exit_send:
1339 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1340}
1341
1342
1343/*****************************************************************************
1344 * edge_write_room
Alan Cox03f0dbf2008-07-22 11:16:34 +01001345 * this function is called by the tty driver when it wants to know how
1346 * many bytes of data we can accept for a specific port. If successful,
1347 * we return the amount of room that we have for this port (the txCredits)
1348 * otherwise we return a negative error number.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001350static int edge_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
Alan Cox95da3102008-07-22 11:09:07 +01001352 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1354 int room;
1355 unsigned long flags;
1356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 if (edge_port == NULL)
Alan Coxd76f2f42008-07-22 11:16:42 +01001358 return 0;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001359 if (edge_port->closePending)
Alan Coxd76f2f42008-07-22 11:16:42 +01001360 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001363 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Alan Coxd76f2f42008-07-22 11:16:42 +01001364 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 }
1366
Alan Cox03f0dbf2008-07-22 11:16:34 +01001367 /* total of both buffers is still txCredit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 spin_lock_irqsave(&edge_port->ep_lock, flags);
1369 room = edge_port->txCredits - edge_port->txfifo.count;
1370 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1371
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001372 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 return room;
1374}
1375
1376
1377/*****************************************************************************
1378 * edge_chars_in_buffer
Alan Cox03f0dbf2008-07-22 11:16:34 +01001379 * this function is called by the tty driver when it wants to know how
1380 * many bytes of data we currently have outstanding in the port (data that
1381 * has been written, but hasn't made it out the port yet)
1382 * If successful, we return the number of bytes left to be written in the
1383 * system,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 * Otherwise we return a negative error number.
1385 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001386static int edge_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
Alan Cox95da3102008-07-22 11:09:07 +01001388 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1390 int num_chars;
1391 unsigned long flags;
1392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 if (edge_port == NULL)
Alan Coxd76f2f42008-07-22 11:16:42 +01001394 return 0;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001395 if (edge_port->closePending)
Alan Coxd76f2f42008-07-22 11:16:42 +01001396 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001399 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Alan Coxd76f2f42008-07-22 11:16:42 +01001400 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 }
1402
1403 spin_lock_irqsave(&edge_port->ep_lock, flags);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001404 num_chars = edge_port->maxTxCredits - edge_port->txCredits +
1405 edge_port->txfifo.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1407 if (num_chars) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001408 dev_dbg(&port->dev, "%s(port %d) - returns %d\n", __func__,
1409 port->number, num_chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 }
1411
1412 return num_chars;
1413}
1414
1415
1416/*****************************************************************************
1417 * SerialThrottle
1418 * this function is called by the tty driver when it wants to stop the data
1419 * being read from the port.
1420 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001421static void edge_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
Alan Cox95da3102008-07-22 11:09:07 +01001423 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 int status;
1426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 if (edge_port == NULL)
1428 return;
1429
1430 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001431 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 return;
1433 }
1434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 /* if we are implementing XON/XOFF, send the stop character */
1436 if (I_IXOFF(tty)) {
1437 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001438 status = edge_write(tty, port, &stop_char, 1);
1439 if (status <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 }
1442
1443 /* if we are implementing RTS/CTS, toggle that line */
1444 if (tty->termios->c_cflag & CRTSCTS) {
1445 edge_port->shadowMCR &= ~MCR_RTS;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001446 status = send_cmd_write_uart_register(edge_port, MCR,
1447 edge_port->shadowMCR);
1448 if (status != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451}
1452
1453
1454/*****************************************************************************
1455 * edge_unthrottle
Alan Cox03f0dbf2008-07-22 11:16:34 +01001456 * this function is called by the tty driver when it wants to resume the
1457 * data being read from the port (called after SerialThrottle is called)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001459static void edge_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
Alan Cox95da3102008-07-22 11:09:07 +01001461 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 int status;
1464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 if (edge_port == NULL)
1466 return;
1467
1468 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001469 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 return;
1471 }
1472
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 /* if we are implementing XON/XOFF, send the start character */
1474 if (I_IXOFF(tty)) {
1475 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001476 status = edge_write(tty, port, &start_char, 1);
1477 if (status <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 /* if we are implementing RTS/CTS, toggle that line */
1481 if (tty->termios->c_cflag & CRTSCTS) {
1482 edge_port->shadowMCR |= MCR_RTS;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001483 send_cmd_write_uart_register(edge_port, MCR,
1484 edge_port->shadowMCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486}
1487
1488
1489/*****************************************************************************
1490 * SerialSetTermios
Alan Cox03f0dbf2008-07-22 11:16:34 +01001491 * this function is called by the tty driver when it wants to change
1492 * the termios structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001494static void edge_set_termios(struct tty_struct *tty,
1495 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
1497 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 unsigned int cflag;
1499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 cflag = tty->termios->c_cflag;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001501 dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__, tty->termios->c_cflag, tty->termios->c_iflag);
1502 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 -07001503
1504 if (edge_port == NULL)
1505 return;
1506
1507 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001508 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 return;
1510 }
1511
1512 /* change the port settings to the new ones specified */
Alan Cox95da3102008-07-22 11:09:07 +01001513 change_port_settings(tty, edge_port, old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514}
1515
1516
1517/*****************************************************************************
1518 * get_lsr_info - get line status register info
1519 *
1520 * Purpose: Let user call ioctl() to get info when the UART physically
1521 * is emptied. On bus types like RS485, the transmitter must
1522 * release the bus after transmitting. This must be done when
1523 * the transmit shift register is empty, not be done when the
1524 * transmit holding register is empty. This functionality
Alan Cox03f0dbf2008-07-22 11:16:34 +01001525 * allows an RS485 driver to be written in user space.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001527static int get_lsr_info(struct edgeport_port *edge_port,
1528 unsigned int __user *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529{
1530 unsigned int result = 0;
1531 unsigned long flags;
1532
1533 spin_lock_irqsave(&edge_port->ep_lock, flags);
1534 if (edge_port->maxTxCredits == edge_port->txCredits &&
1535 edge_port->txfifo.count == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001536 dev_dbg(&edge_port->port->dev, "%s -- Empty\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 result = TIOCSER_TEMT;
1538 }
1539 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1540
1541 if (copy_to_user(value, &result, sizeof(int)))
1542 return -EFAULT;
1543 return 0;
1544}
1545
Alan Cox20b9d172011-02-14 16:26:50 +00001546static int edge_tiocmset(struct tty_struct *tty,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001547 unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
Alan Cox95da3102008-07-22 11:09:07 +01001549 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1551 unsigned int mcr;
1552
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 mcr = edge_port->shadowMCR;
1554 if (set & TIOCM_RTS)
1555 mcr |= MCR_RTS;
1556 if (set & TIOCM_DTR)
1557 mcr |= MCR_DTR;
1558 if (set & TIOCM_LOOP)
1559 mcr |= MCR_LOOPBACK;
1560
1561 if (clear & TIOCM_RTS)
1562 mcr &= ~MCR_RTS;
1563 if (clear & TIOCM_DTR)
1564 mcr &= ~MCR_DTR;
1565 if (clear & TIOCM_LOOP)
1566 mcr &= ~MCR_LOOPBACK;
1567
1568 edge_port->shadowMCR = mcr;
1569
1570 send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1571
1572 return 0;
1573}
1574
Alan Cox60b33c12011-02-14 16:26:14 +00001575static int edge_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576{
Alan Cox95da3102008-07-22 11:09:07 +01001577 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1579 unsigned int result = 0;
1580 unsigned int msr;
1581 unsigned int mcr;
1582
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 msr = edge_port->shadowMSR;
1584 mcr = edge_port->shadowMCR;
1585 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
1586 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
1587 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
1588 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
1589 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
1590 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
1591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 return result;
1593}
1594
Alan Cox0bca1b92010-09-16 18:21:40 +01001595static int edge_get_icount(struct tty_struct *tty,
1596 struct serial_icounter_struct *icount)
1597{
1598 struct usb_serial_port *port = tty->driver_data;
1599 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1600 struct async_icount cnow;
1601 cnow = edge_port->icount;
1602
1603 icount->cts = cnow.cts;
1604 icount->dsr = cnow.dsr;
1605 icount->rng = cnow.rng;
1606 icount->dcd = cnow.dcd;
1607 icount->rx = cnow.rx;
1608 icount->tx = cnow.tx;
1609 icount->frame = cnow.frame;
1610 icount->overrun = cnow.overrun;
1611 icount->parity = cnow.parity;
1612 icount->brk = cnow.brk;
1613 icount->buf_overrun = cnow.buf_overrun;
1614
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001615 dev_dbg(&port->dev, "%s (%d) TIOCGICOUNT RX=%d, TX=%d\n", __func__,
1616 port->number, icount->rx, icount->tx);
Alan Cox0bca1b92010-09-16 18:21:40 +01001617 return 0;
1618}
1619
Alan Cox03f0dbf2008-07-22 11:16:34 +01001620static int get_serial_info(struct edgeport_port *edge_port,
1621 struct serial_struct __user *retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
1623 struct serial_struct tmp;
1624
1625 if (!retinfo)
1626 return -EFAULT;
1627
1628 memset(&tmp, 0, sizeof(tmp));
1629
1630 tmp.type = PORT_16550A;
1631 tmp.line = edge_port->port->serial->minor;
1632 tmp.port = edge_port->port->number;
1633 tmp.irq = 0;
1634 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1635 tmp.xmit_fifo_size = edge_port->maxTxCredits;
1636 tmp.baud_base = 9600;
1637 tmp.close_delay = 5*HZ;
1638 tmp.closing_wait = 30*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
1640 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1641 return -EFAULT;
1642 return 0;
1643}
1644
1645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646/*****************************************************************************
1647 * SerialIoctl
1648 * this function handles any ioctl calls to the driver
1649 *****************************************************************************/
Alan Cox00a0d0d2011-02-14 16:27:06 +00001650static int edge_ioctl(struct tty_struct *tty,
Alan Cox95da3102008-07-22 11:09:07 +01001651 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652{
Alan Cox95da3102008-07-22 11:09:07 +01001653 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 DEFINE_WAIT(wait);
1655 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1656 struct async_icount cnow;
1657 struct async_icount cprev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001659 dev_dbg(&port->dev, "%s - port %d, cmd = 0x%x\n", __func__, port->number, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
1661 switch (cmd) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001662 case TIOCSERGETLSR:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001663 dev_dbg(&port->dev, "%s (%d) TIOCSERGETLSR\n", __func__, port->number);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001664 return get_lsr_info(edge_port, (unsigned int __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Alan Cox03f0dbf2008-07-22 11:16:34 +01001666 case TIOCGSERIAL:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001667 dev_dbg(&port->dev, "%s (%d) TIOCGSERIAL\n", __func__, port->number);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001668 return get_serial_info(edge_port, (struct serial_struct __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
Alan Cox03f0dbf2008-07-22 11:16:34 +01001670 case TIOCMIWAIT:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001671 dev_dbg(&port->dev, "%s (%d) TIOCMIWAIT\n", __func__, port->number);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001672 cprev = edge_port->icount;
1673 while (1) {
1674 prepare_to_wait(&edge_port->delta_msr_wait,
1675 &wait, TASK_INTERRUPTIBLE);
1676 schedule();
1677 finish_wait(&edge_port->delta_msr_wait, &wait);
1678 /* see if a signal did it */
1679 if (signal_pending(current))
1680 return -ERESTARTSYS;
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;
1757 struct tty_struct *tty;
1758 __u16 lastBufferLength;
1759 __u16 rxLen;
1760
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 lastBufferLength = bufferLength + 1;
1762
1763 while (bufferLength > 0) {
1764 /* failsafe incase we get a message that we don't understand */
1765 if (lastBufferLength == bufferLength) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001766 dev_dbg(dev, "%s - stuck in loop, exiting it.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 break;
1768 }
1769 lastBufferLength = bufferLength;
1770
1771 switch (edge_serial->rxState) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001772 case EXPECT_HDR1:
1773 edge_serial->rxHeader1 = *buffer;
1774 ++buffer;
1775 --bufferLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
Alan Cox03f0dbf2008-07-22 11:16:34 +01001777 if (bufferLength == 0) {
1778 edge_serial->rxState = EXPECT_HDR2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 break;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001780 }
1781 /* otherwise, drop on through */
1782 case EXPECT_HDR2:
1783 edge_serial->rxHeader2 = *buffer;
1784 ++buffer;
1785 --bufferLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001787 dev_dbg(dev, "%s - Hdr1=%02X Hdr2=%02X\n", __func__,
1788 edge_serial->rxHeader1, edge_serial->rxHeader2);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001789 /* Process depending on whether this header is
1790 * data or status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Alan Cox03f0dbf2008-07-22 11:16:34 +01001792 if (IS_CMD_STAT_HDR(edge_serial->rxHeader1)) {
1793 /* Decode this status header and go to
1794 * EXPECT_HDR1 (if we can process the status
1795 * with only 2 bytes), or go to EXPECT_HDR3 to
1796 * get the third byte. */
1797 edge_serial->rxPort =
1798 IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1799 edge_serial->rxStatusCode =
1800 IOSP_GET_STATUS_CODE(
1801 edge_serial->rxHeader1);
1802
1803 if (!IOSP_STATUS_IS_2BYTE(
1804 edge_serial->rxStatusCode)) {
1805 /* This status needs additional bytes.
1806 * Save what we have and then wait for
1807 * more data.
1808 */
1809 edge_serial->rxStatusParam
1810 = edge_serial->rxHeader2;
1811 edge_serial->rxState = EXPECT_HDR3;
1812 break;
1813 }
1814 /* We have all the header bytes, process the
1815 status now */
1816 process_rcvd_status(edge_serial,
1817 edge_serial->rxHeader2, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 edge_serial->rxState = EXPECT_HDR1;
1819 break;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001820 } else {
1821 edge_serial->rxPort =
1822 IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1823 edge_serial->rxBytesRemaining =
1824 IOSP_GET_HDR_DATA_LEN(
1825 edge_serial->rxHeader1,
1826 edge_serial->rxHeader2);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001827 dev_dbg(dev, "%s - Data for Port %u Len %u\n",
1828 __func__,
1829 edge_serial->rxPort,
1830 edge_serial->rxBytesRemaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Alan Cox03f0dbf2008-07-22 11:16:34 +01001832 /* ASSERT(DevExt->RxPort < DevExt->NumPorts);
1833 * ASSERT(DevExt->RxBytesRemaining <
1834 * IOSP_MAX_DATA_LENGTH);
1835 */
1836
1837 if (bufferLength == 0) {
1838 edge_serial->rxState = EXPECT_DATA;
1839 break;
1840 }
1841 /* Else, drop through */
1842 }
1843 case EXPECT_DATA: /* Expect data */
1844 if (bufferLength < edge_serial->rxBytesRemaining) {
1845 rxLen = bufferLength;
1846 /* Expect data to start next buffer */
1847 edge_serial->rxState = EXPECT_DATA;
1848 } else {
1849 /* BufLen >= RxBytesRemaining */
1850 rxLen = edge_serial->rxBytesRemaining;
1851 /* Start another header next time */
1852 edge_serial->rxState = EXPECT_HDR1;
1853 }
1854
1855 bufferLength -= rxLen;
1856 edge_serial->rxBytesRemaining -= rxLen;
1857
1858 /* spit this data back into the tty driver if this
1859 port is open */
1860 if (rxLen) {
1861 port = edge_serial->serial->port[
1862 edge_serial->rxPort];
1863 edge_port = usb_get_serial_port_data(port);
1864 if (edge_port->open) {
Alan Cox4a90f092008-10-13 10:39:46 +01001865 tty = tty_port_tty_get(
1866 &edge_port->port->port);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001867 if (tty) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001868 dev_dbg(dev, "%s - Sending %d bytes to TTY for port %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01001869 __func__, rxLen, edge_serial->rxPort);
1870 edge_tty_recv(&edge_serial->serial->dev->dev, tty, buffer, rxLen);
Alan Cox4a90f092008-10-13 10:39:46 +01001871 tty_kref_put(tty);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001872 }
1873 edge_port->icount.rx += rxLen;
1874 }
1875 buffer += rxLen;
1876 }
1877 break;
1878
1879 case EXPECT_HDR3: /* Expect 3rd byte of status header */
1880 edge_serial->rxHeader3 = *buffer;
1881 ++buffer;
1882 --bufferLength;
1883
1884 /* We have all the header bytes, process the
1885 status now */
1886 process_rcvd_status(edge_serial,
1887 edge_serial->rxStatusParam,
1888 edge_serial->rxHeader3);
1889 edge_serial->rxState = EXPECT_HDR1;
1890 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 }
1892 }
1893}
1894
1895
1896/*****************************************************************************
1897 * process_rcvd_status
Alan Cox03f0dbf2008-07-22 11:16:34 +01001898 * this function handles the any status messages received on the
1899 * bulk in pipe.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001901static void process_rcvd_status(struct edgeport_serial *edge_serial,
1902 __u8 byte2, __u8 byte3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903{
1904 struct usb_serial_port *port;
1905 struct edgeport_port *edge_port;
Alan Cox4a90f092008-10-13 10:39:46 +01001906 struct tty_struct *tty;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001907 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 __u8 code = edge_serial->rxStatusCode;
1909
1910 /* switch the port pointer to the one being currently talked about */
1911 port = edge_serial->serial->port[edge_serial->rxPort];
1912 edge_port = usb_get_serial_port_data(port);
1913 if (edge_port == NULL) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001914 dev_err(&edge_serial->serial->dev->dev,
1915 "%s - edge_port == NULL for port %d\n",
1916 __func__, edge_serial->rxPort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 return;
1918 }
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001919 dev = &port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
1921 if (code == IOSP_EXT_STATUS) {
1922 switch (byte2) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001923 case IOSP_EXT_STATUS_CHASE_RSP:
1924 /* we want to do EXT status regardless of port
1925 * open/closed */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001926 dev_dbg(dev, "%s - Port %u EXT CHASE_RSP Data = %02x\n",
1927 __func__, edge_serial->rxPort, byte3);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001928 /* Currently, the only EXT_STATUS is Chase, so process
1929 * here instead of one more call to one more subroutine
1930 * If/when more EXT_STATUS, there'll be more work to do
1931 * Also, we currently clear flag and close the port
1932 * regardless of content of above's Byte3.
1933 * We could choose to do something else when Byte3 says
1934 * Timeout on Chase from Edgeport, like wait longer in
1935 * block_until_chase_response, but for now we don't.
1936 */
1937 edge_port->chaseResponsePending = false;
1938 wake_up(&edge_port->wait_chase);
1939 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
Alan Cox03f0dbf2008-07-22 11:16:34 +01001941 case IOSP_EXT_STATUS_RX_CHECK_RSP:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001942 dev_dbg(dev, "%s ========== Port %u CHECK_RSP Sequence = %02x =============\n",
1943 __func__, edge_serial->rxPort, byte3);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001944 /* Port->RxCheckRsp = true; */
1945 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 }
1947 }
1948
1949 if (code == IOSP_STATUS_OPEN_RSP) {
1950 edge_port->txCredits = GET_TX_BUFFER_SIZE(byte3);
1951 edge_port->maxTxCredits = edge_port->txCredits;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001952 dev_dbg(dev, "%s - Port %u Open Response Initial MSR = %02x TxBufferSize = %d\n",
1953 __func__, edge_serial->rxPort, byte2, edge_port->txCredits);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001954 handle_new_msr(edge_port, byte2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
Alan Cox03f0dbf2008-07-22 11:16:34 +01001956 /* send the current line settings to the port so we are
1957 in sync with any further termios calls */
Alan Cox4a90f092008-10-13 10:39:46 +01001958 tty = tty_port_tty_get(&edge_port->port->port);
1959 if (tty) {
1960 change_port_settings(tty,
1961 edge_port, tty->termios);
1962 tty_kref_put(tty);
1963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
1965 /* we have completed the open */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001966 edge_port->openPending = false;
1967 edge_port->open = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 wake_up(&edge_port->wait_open);
1969 return;
1970 }
1971
Alan Cox03f0dbf2008-07-22 11:16:34 +01001972 /* If port is closed, silently discard all rcvd status. We can
1973 * have cases where buffered status is received AFTER the close
1974 * port command is sent to the Edgeport.
1975 */
1976 if (!edge_port->open || edge_port->closePending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
1979 switch (code) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001980 /* Not currently sent by Edgeport */
1981 case IOSP_STATUS_LSR:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001982 dev_dbg(dev, "%s - Port %u LSR Status = %02x\n",
1983 __func__, edge_serial->rxPort, byte2);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001984 handle_new_lsr(edge_port, false, byte2, 0);
1985 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Alan Cox03f0dbf2008-07-22 11:16:34 +01001987 case IOSP_STATUS_LSR_DATA:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001988 dev_dbg(dev, "%s - Port %u LSR Status = %02x, Data = %02x\n",
1989 __func__, edge_serial->rxPort, byte2, byte3);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001990 /* byte2 is LSR Register */
1991 /* byte3 is broken data byte */
1992 handle_new_lsr(edge_port, true, byte2, byte3);
1993 break;
1994 /*
1995 * case IOSP_EXT_4_STATUS:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001996 * dev_dbg(dev, "%s - Port %u LSR Status = %02x Data = %02x\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01001997 * __func__, edge_serial->rxPort, byte2, byte3);
1998 * break;
1999 */
2000 case IOSP_STATUS_MSR:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002001 dev_dbg(dev, "%s - Port %u MSR Status = %02x\n",
2002 __func__, edge_serial->rxPort, byte2);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002003 /*
2004 * Process this new modem status and generate appropriate
2005 * events, etc, based on the new status. This routine
2006 * also saves the MSR in Port->ShadowMsr.
2007 */
2008 handle_new_msr(edge_port, byte2);
2009 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
Alan Cox03f0dbf2008-07-22 11:16:34 +01002011 default:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002012 dev_dbg(dev, "%s - Unrecognized IOSP status code %u\n", __func__, code);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002013 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015}
2016
2017
2018/*****************************************************************************
2019 * edge_tty_recv
2020 * this function passes data on to the tty flip buffer
2021 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002022static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
2023 unsigned char *data, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024{
2025 int cnt;
2026
Alan Coxa108bfc2010-02-18 16:44:01 +00002027 cnt = tty_insert_flip_string(tty, data, length);
2028 if (cnt < length) {
2029 dev_err(dev, "%s - dropping data, %d bytes lost\n",
2030 __func__, length - cnt);
2031 }
2032 data += cnt;
2033 length -= cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
2035 tty_flip_buffer_push(tty);
2036}
2037
2038
2039/*****************************************************************************
2040 * handle_new_msr
2041 * this function handles any change to the msr register for a port.
2042 *****************************************************************************/
2043static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr)
2044{
2045 struct async_icount *icount;
2046
Alan Cox03f0dbf2008-07-22 11:16:34 +01002047 if (newMsr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
2048 EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 icount = &edge_port->icount;
2050
2051 /* update input line counters */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002052 if (newMsr & EDGEPORT_MSR_DELTA_CTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 icount->cts++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002054 if (newMsr & EDGEPORT_MSR_DELTA_DSR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 icount->dsr++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002056 if (newMsr & EDGEPORT_MSR_DELTA_CD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 icount->dcd++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002058 if (newMsr & EDGEPORT_MSR_DELTA_RI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 icount->rng++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 wake_up_interruptible(&edge_port->delta_msr_wait);
2061 }
2062
2063 /* Save the new modem status */
2064 edge_port->shadowMSR = newMsr & 0xf0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065}
2066
2067
2068/*****************************************************************************
2069 * handle_new_lsr
2070 * this function handles any change to the lsr register for a port.
2071 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002072static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
2073 __u8 lsr, __u8 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002075 __u8 newLsr = (__u8) (lsr & (__u8)
2076 (LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
2077 struct async_icount *icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 edge_port->shadowLSR = lsr;
2080
2081 if (newLsr & LSR_BREAK) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002082 /*
2083 * Parity and Framing errors only count if they
2084 * occur exclusive of a break being
2085 * received.
2086 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 newLsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
2088 }
2089
2090 /* Place LSR data byte into Rx buffer */
Alan Cox4a90f092008-10-13 10:39:46 +01002091 if (lsrData) {
2092 struct tty_struct *tty =
2093 tty_port_tty_get(&edge_port->port->port);
2094 if (tty) {
2095 edge_tty_recv(&edge_port->port->dev, tty, &data, 1);
2096 tty_kref_put(tty);
2097 }
2098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 /* update input line counters */
2100 icount = &edge_port->icount;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002101 if (newLsr & LSR_BREAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 icount->brk++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002103 if (newLsr & LSR_OVER_ERR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 icount->overrun++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002105 if (newLsr & LSR_PAR_ERR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 icount->parity++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002107 if (newLsr & LSR_FRM_ERR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 icount->frame++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109}
2110
2111
2112/****************************************************************************
2113 * sram_write
Alan Cox03f0dbf2008-07-22 11:16:34 +01002114 * writes a number of bytes to the Edgeport device's sram starting at the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 * given address.
2116 * If successful returns the number of bytes written, otherwise it returns
2117 * a negative error number of the problem.
2118 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002119static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
2120 __u16 length, const __u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121{
2122 int result;
2123 __u16 current_length;
2124 unsigned char *transfer_buffer;
2125
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002126 dev_dbg(&serial->dev->dev, "%s - %x, %x, %d\n", __func__, extAddr, addr, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
Alan Cox03f0dbf2008-07-22 11:16:34 +01002128 transfer_buffer = kmalloc(64, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 if (!transfer_buffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002130 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
2131 __func__, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 return -ENOMEM;
2133 }
2134
2135 /* need to split these writes up into 64 byte chunks */
2136 result = 0;
2137 while (length > 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002138 if (length > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 current_length = 64;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002140 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 current_length = length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002142
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002143/* dev_dbg(&serial->dev->dev, "%s - writing %x, %x, %d\n", __func__, extAddr, addr, current_length); */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002144 memcpy(transfer_buffer, data, current_length);
2145 result = usb_control_msg(serial->dev,
2146 usb_sndctrlpipe(serial->dev, 0),
2147 USB_REQUEST_ION_WRITE_RAM,
2148 0x40, addr, extAddr, transfer_buffer,
2149 current_length, 300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 if (result < 0)
2151 break;
2152 length -= current_length;
2153 addr += current_length;
2154 data += current_length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
Alan Cox03f0dbf2008-07-22 11:16:34 +01002157 kfree(transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 return result;
2159}
2160
2161
2162/****************************************************************************
2163 * rom_write
2164 * writes a number of bytes to the Edgeport device's ROM starting at the
2165 * given address.
2166 * If successful returns the number of bytes written, otherwise it returns
2167 * a negative error number of the problem.
2168 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002169static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
2170 __u16 length, const __u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171{
2172 int result;
2173 __u16 current_length;
2174 unsigned char *transfer_buffer;
2175
Alan Cox03f0dbf2008-07-22 11:16:34 +01002176 transfer_buffer = kmalloc(64, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 if (!transfer_buffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002178 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
2179 __func__, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 return -ENOMEM;
2181 }
2182
2183 /* need to split these writes up into 64 byte chunks */
2184 result = 0;
2185 while (length > 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002186 if (length > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 current_length = 64;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002188 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 current_length = length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002190 memcpy(transfer_buffer, data, current_length);
2191 result = usb_control_msg(serial->dev,
2192 usb_sndctrlpipe(serial->dev, 0),
2193 USB_REQUEST_ION_WRITE_ROM, 0x40,
2194 addr, extAddr,
2195 transfer_buffer, current_length, 300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 if (result < 0)
2197 break;
2198 length -= current_length;
2199 addr += current_length;
2200 data += current_length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
Alan Cox03f0dbf2008-07-22 11:16:34 +01002203 kfree(transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 return result;
2205}
2206
2207
2208/****************************************************************************
2209 * rom_read
2210 * reads a number of bytes from the Edgeport device starting at the given
2211 * address.
2212 * If successful returns the number of bytes read, otherwise it returns
2213 * a negative error number of the problem.
2214 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002215static int rom_read(struct usb_serial *serial, __u16 extAddr,
2216 __u16 addr, __u16 length, __u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217{
2218 int result;
2219 __u16 current_length;
2220 unsigned char *transfer_buffer;
2221
Alan Cox03f0dbf2008-07-22 11:16:34 +01002222 transfer_buffer = kmalloc(64, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 if (!transfer_buffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002224 dev_err(&serial->dev->dev,
2225 "%s - kmalloc(%d) failed.\n", __func__, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 return -ENOMEM;
2227 }
2228
2229 /* need to split these reads up into 64 byte chunks */
2230 result = 0;
2231 while (length > 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002232 if (length > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 current_length = 64;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002234 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 current_length = length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002236 result = usb_control_msg(serial->dev,
2237 usb_rcvctrlpipe(serial->dev, 0),
2238 USB_REQUEST_ION_READ_ROM,
2239 0xC0, addr, extAddr, transfer_buffer,
2240 current_length, 300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 if (result < 0)
2242 break;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002243 memcpy(data, transfer_buffer, current_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 length -= current_length;
2245 addr += current_length;
2246 data += current_length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
Alan Cox03f0dbf2008-07-22 11:16:34 +01002249 kfree(transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 return result;
2251}
2252
2253
2254/****************************************************************************
2255 * send_iosp_ext_cmd
2256 * Is used to send a IOSP message to the Edgeport device
2257 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002258static int send_iosp_ext_cmd(struct edgeport_port *edge_port,
2259 __u8 command, __u8 param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260{
2261 unsigned char *buffer;
2262 unsigned char *currentCommand;
2263 int length = 0;
2264 int status = 0;
2265
Alan Cox03f0dbf2008-07-22 11:16:34 +01002266 buffer = kmalloc(10, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 if (!buffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002268 dev_err(&edge_port->port->dev,
2269 "%s - kmalloc(%d) failed.\n", __func__, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 return -ENOMEM;
2271 }
2272
2273 currentCommand = buffer;
2274
Alan Cox03f0dbf2008-07-22 11:16:34 +01002275 MAKE_CMD_EXT_CMD(&currentCommand, &length,
2276 edge_port->port->number - edge_port->port->serial->minor,
2277 command, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
Alan Cox03f0dbf2008-07-22 11:16:34 +01002279 status = write_cmd_usb(edge_port, buffer, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 if (status) {
2281 /* something bad happened, let's free up the memory */
2282 kfree(buffer);
2283 }
2284
2285 return status;
2286}
2287
2288
2289/*****************************************************************************
2290 * write_cmd_usb
2291 * this function writes the given buffer out to the bulk write endpoint.
2292 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002293static int write_cmd_usb(struct edgeport_port *edge_port,
2294 unsigned char *buffer, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002296 struct edgeport_serial *edge_serial =
2297 usb_get_serial_data(edge_port->port->serial);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002298 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 int status = 0;
2300 struct urb *urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01002302 usb_serial_debug_data(dev, __func__, length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
2304 /* Allocate our next urb */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002305 urb = usb_alloc_urb(0, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 if (!urb)
2307 return -ENOMEM;
2308
Oliver Neukum96c706e2007-03-15 15:27:17 +01002309 atomic_inc(&CmdUrbs);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002310 dev_dbg(dev, "%s - ALLOCATE URB %p (outstanding %d)\n",
2311 __func__, urb, atomic_read(&CmdUrbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
Alan Cox03f0dbf2008-07-22 11:16:34 +01002313 usb_fill_bulk_urb(urb, edge_serial->serial->dev,
2314 usb_sndbulkpipe(edge_serial->serial->dev,
2315 edge_serial->bulk_out_endpoint),
2316 buffer, length, edge_bulk_out_cmd_callback, edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002318 edge_port->commandPending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 status = usb_submit_urb(urb, GFP_ATOMIC);
2320
2321 if (status) {
2322 /* something went wrong */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002323 dev_err(dev, "%s - usb_submit_urb(write command) failed, status = %d\n",
2324 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 usb_kill_urb(urb);
2326 usb_free_urb(urb);
Oliver Neukum96c706e2007-03-15 15:27:17 +01002327 atomic_dec(&CmdUrbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 return status;
2329 }
2330
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331#if 0
Alan Cox03f0dbf2008-07-22 11:16:34 +01002332 wait_event(&edge_port->wait_command, !edge_port->commandPending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002334 if (edge_port->commandPending) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 /* command timed out */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002336 dev_dbg(dev, "%s - command timed out\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 status = -EINVAL;
2338 }
2339#endif
2340 return status;
2341}
2342
2343
2344/*****************************************************************************
2345 * send_cmd_write_baud_rate
2346 * this function sends the proper command to change the baud rate of the
2347 * specified port.
2348 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002349static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
2350 int baudRate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002352 struct edgeport_serial *edge_serial =
2353 usb_get_serial_data(edge_port->port->serial);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002354 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 unsigned char *cmdBuffer;
2356 unsigned char *currCmd;
2357 int cmdLen = 0;
2358 int divisor;
2359 int status;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002360 unsigned char number =
2361 edge_port->port->number - edge_port->port->serial->minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
Adam Kropelinbc71e472007-07-29 11:03:29 -04002363 if (edge_serial->is_epic &&
2364 !edge_serial->epic_descriptor.Supports.IOSPSetBaudRate) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002365 dev_dbg(dev, "SendCmdWriteBaudRate - NOT Setting baud rate for port = %d, baud = %d\n",
2366 edge_port->port->number, baudRate);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002367 return 0;
2368 }
2369
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002370 dev_dbg(dev, "%s - port = %d, baud = %d\n", __func__,
2371 edge_port->port->number, baudRate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002373 status = calc_baud_rate_divisor(dev, baudRate, &divisor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 if (status) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002375 dev_err(dev, "%s - bad baud rate\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 return status;
2377 }
2378
Alan Cox03f0dbf2008-07-22 11:16:34 +01002379 /* Alloc memory for the string of commands. */
2380 cmdBuffer = kmalloc(0x100, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 if (!cmdBuffer) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002382 dev_err(dev, "%s - kmalloc(%d) failed.\n", __func__, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 return -ENOMEM;
2384 }
2385 currCmd = cmdBuffer;
2386
Alan Cox03f0dbf2008-07-22 11:16:34 +01002387 /* Enable access to divisor latch */
2388 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR, LCR_DL_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
Alan Cox03f0dbf2008-07-22 11:16:34 +01002390 /* Write the divisor itself */
2391 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLL, LOW8(divisor));
2392 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLM, HIGH8(divisor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
Alan Cox03f0dbf2008-07-22 11:16:34 +01002394 /* Restore original value to disable access to divisor latch */
2395 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR,
2396 edge_port->shadowLCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
Alan Cox03f0dbf2008-07-22 11:16:34 +01002398 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 if (status) {
2400 /* something bad happened, let's free up the memory */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002401 kfree(cmdBuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 }
2403
2404 return status;
2405}
2406
2407
2408/*****************************************************************************
2409 * calc_baud_rate_divisor
2410 * this function calculates the proper baud rate divisor for the specified
2411 * baud rate.
2412 *****************************************************************************/
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002413static int calc_baud_rate_divisor(struct device *dev, int baudrate, int *divisor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414{
2415 int i;
2416 __u16 custom;
2417
Tobias Klauser52950ed2005-12-11 16:20:08 +01002418 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002419 if (divisor_table[i].BaudRate == baudrate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 *divisor = divisor_table[i].Divisor;
2421 return 0;
2422 }
2423 }
2424
Alan Cox03f0dbf2008-07-22 11:16:34 +01002425 /* We have tried all of the standard baud rates
2426 * lets try to calculate the divisor for this baud rate
2427 * Make sure the baud rate is reasonable */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 if (baudrate > 50 && baudrate < 230400) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002429 /* get divisor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 custom = (__u16)((230400L + baudrate/2) / baudrate);
2431
2432 *divisor = custom;
2433
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002434 dev_dbg(dev, "%s - Baud %d = %d\n", __func__, baudrate, custom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 return 0;
2436 }
2437
2438 return -1;
2439}
2440
2441
2442/*****************************************************************************
2443 * send_cmd_write_uart_register
Anand Gadiyarfd589a82009-07-16 17:13:03 +02002444 * this function builds up a uart register message and sends to the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002446static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
2447 __u8 regNum, __u8 regValue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002449 struct edgeport_serial *edge_serial =
2450 usb_get_serial_data(edge_port->port->serial);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002451 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 unsigned char *cmdBuffer;
2453 unsigned char *currCmd;
2454 unsigned long cmdLen = 0;
2455 int status;
2456
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002457 dev_dbg(dev, "%s - write to %s register 0x%02x\n",
2458 (regNum == MCR) ? "MCR" : "LCR", __func__, regValue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
Adam Kropelinbc71e472007-07-29 11:03:29 -04002460 if (edge_serial->is_epic &&
2461 !edge_serial->epic_descriptor.Supports.IOSPWriteMCR &&
2462 regNum == MCR) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002463 dev_dbg(dev, "SendCmdWriteUartReg - Not writing to MCR Register\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002464 return 0;
2465 }
2466
Adam Kropelinbc71e472007-07-29 11:03:29 -04002467 if (edge_serial->is_epic &&
2468 !edge_serial->epic_descriptor.Supports.IOSPWriteLCR &&
2469 regNum == LCR) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002470 dev_dbg(dev, "SendCmdWriteUartReg - Not writing to LCR Register\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002471 return 0;
2472 }
2473
Alan Cox03f0dbf2008-07-22 11:16:34 +01002474 /* Alloc memory for the string of commands. */
2475 cmdBuffer = kmalloc(0x10, GFP_ATOMIC);
2476 if (cmdBuffer == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
2479 currCmd = cmdBuffer;
2480
Alan Cox03f0dbf2008-07-22 11:16:34 +01002481 /* Build a cmd in the buffer to write the given register */
2482 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen,
2483 edge_port->port->number - edge_port->port->serial->minor,
2484 regNum, regValue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
2486 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
2487 if (status) {
2488 /* something bad happened, let's free up the memory */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002489 kfree(cmdBuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 }
2491
2492 return status;
2493}
2494
2495
2496/*****************************************************************************
2497 * change_port_settings
Alan Cox03f0dbf2008-07-22 11:16:34 +01002498 * This routine is called to set the UART on the device to match the
2499 * specified new settings.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01002501
2502static void change_port_settings(struct tty_struct *tty,
2503 struct edgeport_port *edge_port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002505 struct device *dev = &edge_port->port->dev;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002506 struct edgeport_serial *edge_serial =
2507 usb_get_serial_data(edge_port->port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 int baud;
2509 unsigned cflag;
2510 __u8 mask = 0xff;
2511 __u8 lData;
2512 __u8 lParity;
2513 __u8 lStop;
2514 __u8 rxFlow;
2515 __u8 txFlow;
2516 int status;
2517
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002518 dev_dbg(dev, "%s - port %d\n", __func__, edge_port->port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002520 if (!edge_port->open &&
2521 !edge_port->openPending) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002522 dev_dbg(dev, "%s - port not opened\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 return;
2524 }
2525
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 cflag = tty->termios->c_cflag;
2527
2528 switch (cflag & CSIZE) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002529 case CS5:
2530 lData = LCR_BITS_5; mask = 0x1f;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002531 dev_dbg(dev, "%s - data bits = 5\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002532 break;
2533 case CS6:
2534 lData = LCR_BITS_6; mask = 0x3f;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002535 dev_dbg(dev, "%s - data bits = 6\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002536 break;
2537 case CS7:
2538 lData = LCR_BITS_7; mask = 0x7f;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002539 dev_dbg(dev, "%s - data bits = 7\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002540 break;
2541 default:
2542 case CS8:
2543 lData = LCR_BITS_8;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002544 dev_dbg(dev, "%s - data bits = 8\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002545 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 }
2547
2548 lParity = LCR_PAR_NONE;
2549 if (cflag & PARENB) {
2550 if (cflag & CMSPAR) {
2551 if (cflag & PARODD) {
2552 lParity = LCR_PAR_MARK;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002553 dev_dbg(dev, "%s - parity = mark\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 } else {
2555 lParity = LCR_PAR_SPACE;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002556 dev_dbg(dev, "%s - parity = space\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 }
2558 } else if (cflag & PARODD) {
2559 lParity = LCR_PAR_ODD;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002560 dev_dbg(dev, "%s - parity = odd\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 } else {
2562 lParity = LCR_PAR_EVEN;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002563 dev_dbg(dev, "%s - parity = even\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 }
2565 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002566 dev_dbg(dev, "%s - parity = none\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 }
2568
2569 if (cflag & CSTOPB) {
2570 lStop = LCR_STOP_2;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002571 dev_dbg(dev, "%s - stop bits = 2\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 } else {
2573 lStop = LCR_STOP_1;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002574 dev_dbg(dev, "%s - stop bits = 1\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 }
2576
2577 /* figure out the flow control settings */
2578 rxFlow = txFlow = 0x00;
2579 if (cflag & CRTSCTS) {
2580 rxFlow |= IOSP_RX_FLOW_RTS;
2581 txFlow |= IOSP_TX_FLOW_CTS;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002582 dev_dbg(dev, "%s - RTS/CTS is enabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002584 dev_dbg(dev, "%s - RTS/CTS is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 }
2586
Alan Cox03f0dbf2008-07-22 11:16:34 +01002587 /* if we are implementing XON/XOFF, set the start and stop character
2588 in the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 if (I_IXOFF(tty) || I_IXON(tty)) {
2590 unsigned char stop_char = STOP_CHAR(tty);
2591 unsigned char start_char = START_CHAR(tty);
2592
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002593 if ((!edge_serial->is_epic) ||
2594 ((edge_serial->is_epic) &&
2595 (edge_serial->epic_descriptor.Supports.IOSPSetXChar))) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002596 send_iosp_ext_cmd(edge_port,
2597 IOSP_CMD_SET_XON_CHAR, start_char);
2598 send_iosp_ext_cmd(edge_port,
2599 IOSP_CMD_SET_XOFF_CHAR, stop_char);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
2602 /* if we are implementing INBOUND XON/XOFF */
2603 if (I_IXOFF(tty)) {
2604 rxFlow |= IOSP_RX_FLOW_XON_XOFF;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002605 dev_dbg(dev, "%s - INBOUND 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 - INBOUND XON/XOFF is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 }
2610
2611 /* if we are implementing OUTBOUND XON/XOFF */
2612 if (I_IXON(tty)) {
2613 txFlow |= IOSP_TX_FLOW_XON_XOFF;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002614 dev_dbg(dev, "%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2615 __func__, start_char, stop_char);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002617 dev_dbg(dev, "%s - OUTBOUND XON/XOFF is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 }
2619 }
2620
2621 /* Set flow control to the configured value */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002622 if ((!edge_serial->is_epic) ||
2623 ((edge_serial->is_epic) &&
2624 (edge_serial->epic_descriptor.Supports.IOSPSetRxFlow)))
2625 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_RX_FLOW, rxFlow);
2626 if ((!edge_serial->is_epic) ||
2627 ((edge_serial->is_epic) &&
2628 (edge_serial->epic_descriptor.Supports.IOSPSetTxFlow)))
2629 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_TX_FLOW, txFlow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
2631
2632 edge_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2633 edge_port->shadowLCR |= (lData | lParity | lStop);
2634
2635 edge_port->validDataMask = mask;
2636
2637 /* Send the updated LCR value to the EdgePort */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002638 status = send_cmd_write_uart_register(edge_port, LCR,
2639 edge_port->shadowLCR);
2640 if (status != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
2643 /* set up the MCR register and send it to the EdgePort */
2644 edge_port->shadowMCR = MCR_MASTER_IE;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002645 if (cflag & CBAUD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 edge_port->shadowMCR |= (MCR_DTR | MCR_RTS);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002647
2648 status = send_cmd_write_uart_register(edge_port, MCR,
2649 edge_port->shadowMCR);
2650 if (status != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652
2653 /* Determine divisor based on baud rate */
2654 baud = tty_get_baud_rate(tty);
2655 if (!baud) {
2656 /* pick a default, any default... */
2657 baud = 9600;
2658 }
2659
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002660 dev_dbg(dev, "%s - baud rate = %d\n", __func__, baud);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002661 status = send_cmd_write_baud_rate(edge_port, baud);
Alan Cox6ce073b2007-10-18 01:24:25 -07002662 if (status == -1) {
2663 /* Speed change was not possible - put back the old speed */
2664 baud = tty_termios_baud_rate(old_termios);
2665 tty_encode_baud_rate(tty, baud, baud);
2666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667}
2668
2669
2670/****************************************************************************
2671 * unicode_to_ascii
2672 * Turns a string from Unicode into ASCII.
2673 * Doesn't do a good job with any characters that are outside the normal
2674 * ASCII range, but it's only for debugging...
2675 * NOTE: expects the unicode in LE format
2676 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002677static void unicode_to_ascii(char *string, int buflen,
2678 __le16 *unicode, int unicode_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679{
2680 int i;
2681
Pete Zaitcevad933752006-05-22 21:49:44 -07002682 if (buflen <= 0) /* never happens, but... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 return;
Pete Zaitcevad933752006-05-22 21:49:44 -07002684 --buflen; /* space for nul */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685
Pete Zaitcevad933752006-05-22 21:49:44 -07002686 for (i = 0; i < unicode_size; i++) {
2687 if (i >= buflen)
2688 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 string[i] = (char)(le16_to_cpu(unicode[i]));
Pete Zaitcevad933752006-05-22 21:49:44 -07002690 }
2691 string[i] = 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692}
2693
2694
2695/****************************************************************************
2696 * get_manufacturing_desc
Alan Cox03f0dbf2008-07-22 11:16:34 +01002697 * reads in the manufacturing descriptor and stores it into the serial
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 * structure.
2699 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002700static void get_manufacturing_desc(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002702 struct device *dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 int response;
2704
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002705 dev_dbg(dev, "getting manufacturer descriptor\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706
Alan Cox03f0dbf2008-07-22 11:16:34 +01002707 response = rom_read(edge_serial->serial,
2708 (EDGE_MANUF_DESC_ADDR & 0xffff0000) >> 16,
2709 (__u16)(EDGE_MANUF_DESC_ADDR & 0x0000ffff),
2710 EDGE_MANUF_DESC_LEN,
2711 (__u8 *)(&edge_serial->manuf_descriptor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
Alan Cox03f0dbf2008-07-22 11:16:34 +01002713 if (response < 1)
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002714 dev_err(dev, "error in getting manufacturer descriptor\n");
Alan Cox03f0dbf2008-07-22 11:16:34 +01002715 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 char string[30];
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002717 dev_dbg(dev, "**Manufacturer Descriptor\n");
2718 dev_dbg(dev, " RomSize: %dK\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002719 edge_serial->manuf_descriptor.RomSize);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002720 dev_dbg(dev, " RamSize: %dK\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002721 edge_serial->manuf_descriptor.RamSize);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002722 dev_dbg(dev, " CpuRev: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002723 edge_serial->manuf_descriptor.CpuRev);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002724 dev_dbg(dev, " BoardRev: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002725 edge_serial->manuf_descriptor.BoardRev);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002726 dev_dbg(dev, " NumPorts: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002727 edge_serial->manuf_descriptor.NumPorts);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002728 dev_dbg(dev, " DescDate: %d/%d/%d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002729 edge_serial->manuf_descriptor.DescDate[0],
2730 edge_serial->manuf_descriptor.DescDate[1],
2731 edge_serial->manuf_descriptor.DescDate[2]+1900);
Pete Zaitcev4bc203d2006-06-06 18:18:33 -07002732 unicode_to_ascii(string, sizeof(string),
Alan Cox03f0dbf2008-07-22 11:16:34 +01002733 edge_serial->manuf_descriptor.SerialNumber,
2734 edge_serial->manuf_descriptor.SerNumLength/2);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002735 dev_dbg(dev, " SerialNumber: %s\n", string);
Pete Zaitcev4bc203d2006-06-06 18:18:33 -07002736 unicode_to_ascii(string, sizeof(string),
Alan Cox03f0dbf2008-07-22 11:16:34 +01002737 edge_serial->manuf_descriptor.AssemblyNumber,
2738 edge_serial->manuf_descriptor.AssemblyNumLength/2);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002739 dev_dbg(dev, " AssemblyNumber: %s\n", string);
Pete Zaitcev4bc203d2006-06-06 18:18:33 -07002740 unicode_to_ascii(string, sizeof(string),
Pete Zaitcevad933752006-05-22 21:49:44 -07002741 edge_serial->manuf_descriptor.OemAssyNumber,
2742 edge_serial->manuf_descriptor.OemAssyNumLength/2);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002743 dev_dbg(dev, " OemAssyNumber: %s\n", string);
2744 dev_dbg(dev, " UartType: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002745 edge_serial->manuf_descriptor.UartType);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002746 dev_dbg(dev, " IonPid: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002747 edge_serial->manuf_descriptor.IonPid);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002748 dev_dbg(dev, " IonConfig: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002749 edge_serial->manuf_descriptor.IonConfig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 }
2751}
2752
2753
2754/****************************************************************************
2755 * get_boot_desc
Alan Cox03f0dbf2008-07-22 11:16:34 +01002756 * reads in the bootloader descriptor and stores it into the serial
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 * structure.
2758 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002759static void get_boot_desc(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002761 struct device *dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 int response;
2763
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002764 dev_dbg(dev, "getting boot descriptor\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765
Alan Cox03f0dbf2008-07-22 11:16:34 +01002766 response = rom_read(edge_serial->serial,
2767 (EDGE_BOOT_DESC_ADDR & 0xffff0000) >> 16,
2768 (__u16)(EDGE_BOOT_DESC_ADDR & 0x0000ffff),
2769 EDGE_BOOT_DESC_LEN,
2770 (__u8 *)(&edge_serial->boot_descriptor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
Alan Cox03f0dbf2008-07-22 11:16:34 +01002772 if (response < 1)
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002773 dev_err(dev, "error in getting boot descriptor\n");
Alan Cox03f0dbf2008-07-22 11:16:34 +01002774 else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002775 dev_dbg(dev, "**Boot Descriptor:\n");
2776 dev_dbg(dev, " BootCodeLength: %d\n",
2777 le16_to_cpu(edge_serial->boot_descriptor.BootCodeLength));
2778 dev_dbg(dev, " MajorVersion: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002779 edge_serial->boot_descriptor.MajorVersion);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002780 dev_dbg(dev, " MinorVersion: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002781 edge_serial->boot_descriptor.MinorVersion);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002782 dev_dbg(dev, " BuildNumber: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002783 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002784 dev_dbg(dev, " Capabilities: 0x%x\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002785 le16_to_cpu(edge_serial->boot_descriptor.Capabilities));
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002786 dev_dbg(dev, " UConfig0: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002787 edge_serial->boot_descriptor.UConfig0);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002788 dev_dbg(dev, " UConfig1: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002789 edge_serial->boot_descriptor.UConfig1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 }
2791}
2792
2793
2794/****************************************************************************
2795 * load_application_firmware
2796 * This is called to load the application firmware to the device
2797 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002798static void load_application_firmware(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002800 struct device *dev = &edge_serial->serial->dev->dev;
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302801 const struct ihex_binrec *rec;
2802 const struct firmware *fw;
2803 const char *fw_name;
2804 const char *fw_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 int response;
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302806 __u32 Operaddr;
2807 __u16 build;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808
2809 switch (edge_serial->product_info.iDownloadFile) {
2810 case EDGE_DOWNLOAD_FILE_I930:
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302811 fw_info = "downloading firmware version (930)";
2812 fw_name = "edgeport/down.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 break;
2814
2815 case EDGE_DOWNLOAD_FILE_80251:
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302816 fw_info = "downloading firmware version (80251)";
2817 fw_name = "edgeport/down2.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 break;
2819
2820 case EDGE_DOWNLOAD_FILE_NONE:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002821 dev_dbg(dev, "No download file specified, skipping download\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 return;
2823
2824 default:
2825 return;
2826 }
2827
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302828 response = request_ihex_firmware(&fw, fw_name,
2829 &edge_serial->serial->dev->dev);
2830 if (response) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002831 dev_err(dev, "Failed to load image \"%s\" err %d\n",
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302832 fw_name, response);
2833 return;
2834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302836 rec = (const struct ihex_binrec *)fw->data;
2837 build = (rec->data[2] << 8) | rec->data[3];
2838
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002839 dev_dbg(dev, "%s %d.%d.%d\n", fw_info, rec->data[0], rec->data[1], build);
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302840
Bjørn Mork271c1152011-01-17 14:19:37 +01002841 edge_serial->product_info.FirmwareMajorVersion = rec->data[0];
2842 edge_serial->product_info.FirmwareMinorVersion = rec->data[1];
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302843 edge_serial->product_info.FirmwareBuildNumber = cpu_to_le16(build);
2844
2845 for (rec = ihex_next_binrec(rec); rec;
2846 rec = ihex_next_binrec(rec)) {
2847 Operaddr = be32_to_cpu(rec->addr);
2848 response = sram_write(edge_serial->serial,
2849 Operaddr >> 16,
2850 Operaddr & 0xFFFF,
2851 be16_to_cpu(rec->len),
2852 &rec->data[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 if (response < 0) {
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302854 dev_err(&edge_serial->serial->dev->dev,
2855 "sram_write failed (%x, %x, %d)\n",
2856 Operaddr >> 16, Operaddr & 0xFFFF,
2857 be16_to_cpu(rec->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 break;
2859 }
2860 }
2861
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002862 dev_dbg(dev, "sending exec_dl_code\n");
2863 response = usb_control_msg (edge_serial->serial->dev,
2864 usb_sndctrlpipe(edge_serial->serial->dev, 0),
2865 USB_REQUEST_ION_EXEC_DL_CODE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 0x40, 0x4000, 0x0001, NULL, 0, 3000);
2867
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302868 release_firmware(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869}
2870
2871
2872/****************************************************************************
2873 * edge_startup
2874 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002875static int edge_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876{
2877 struct edgeport_serial *edge_serial;
2878 struct edgeport_port *edge_port;
2879 struct usb_device *dev;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002880 struct device *ddev = &serial->dev->dev;
Chris Lundbfd5df32006-06-03 13:58:19 -07002881 int i, j;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002882 int response;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002883 bool interrupt_in_found;
2884 bool bulk_in_found;
2885 bool bulk_out_found;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002886 static __u32 descriptor[3] = { EDGE_COMPATIBILITY_MASK0,
2887 EDGE_COMPATIBILITY_MASK1,
2888 EDGE_COMPATIBILITY_MASK2 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889
2890 dev = serial->dev;
2891
2892 /* create our private serial structure */
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +01002893 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 if (edge_serial == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002895 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 return -ENOMEM;
2897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 spin_lock_init(&edge_serial->es_lock);
2899 edge_serial->serial = serial;
2900 usb_set_serial_data(serial, edge_serial);
2901
2902 /* get the name for the device from the device */
Dan Carpenterf10718f2010-01-25 14:53:33 +03002903 i = usb_string(dev, dev->descriptor.iManufacturer,
Pete Zaitcevad933752006-05-22 21:49:44 -07002904 &edge_serial->name[0], MAX_NAME_LEN+1);
Dan Carpenterf10718f2010-01-25 14:53:33 +03002905 if (i < 0)
2906 i = 0;
Pete Zaitcevad933752006-05-22 21:49:44 -07002907 edge_serial->name[i++] = ' ';
Dan Carpenterf10718f2010-01-25 14:53:33 +03002908 usb_string(dev, dev->descriptor.iProduct,
Pete Zaitcevad933752006-05-22 21:49:44 -07002909 &edge_serial->name[i], MAX_NAME_LEN+2 - i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
2911 dev_info(&serial->dev->dev, "%s detected\n", edge_serial->name);
2912
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002913 /* Read the epic descriptor */
2914 if (get_epic_descriptor(edge_serial) <= 0) {
2915 /* memcpy descriptor to Supports structures */
2916 memcpy(&edge_serial->epic_descriptor.Supports, descriptor,
2917 sizeof(struct edge_compatibility_bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002919 /* get the manufacturing descriptor for this device */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002920 get_manufacturing_desc(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002922 /* get the boot descriptor */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002923 get_boot_desc(edge_serial);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002924
2925 get_product_info(edge_serial);
2926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927
2928 /* set the number of ports from the manufacturing description */
2929 /* serial->num_ports = serial->product_info.NumPorts; */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002930 if ((!edge_serial->is_epic) &&
2931 (edge_serial->product_info.NumPorts != serial->num_ports)) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002932 dev_warn(ddev,
2933 "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 -08002934 edge_serial->product_info.NumPorts,
2935 serial->num_ports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 }
2937
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002938 dev_dbg(ddev, "%s - time 1 %ld\n", __func__, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002940 /* If not an EPiC device */
2941 if (!edge_serial->is_epic) {
2942 /* now load the application firmware into this device */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002943 load_application_firmware(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002945 dev_dbg(ddev, "%s - time 2 %ld\n", __func__, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002947 /* Check current Edgeport EEPROM and update if necessary */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002948 update_edgeport_E2PROM(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002950 dev_dbg(ddev, "%s - time 3 %ld\n", __func__, jiffies);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002951
2952 /* set the configuration to use #1 */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002953/* dev_dbg(ddev, "set_configuration 1\n"); */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002954/* usb_set_configuration (dev, 1); */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002955 }
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002956 dev_dbg(ddev, " FirmwareMajorVersion %d.%d.%d\n",
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302957 edge_serial->product_info.FirmwareMajorVersion,
2958 edge_serial->product_info.FirmwareMinorVersion,
2959 le16_to_cpu(edge_serial->product_info.FirmwareBuildNumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960
Alan Cox03f0dbf2008-07-22 11:16:34 +01002961 /* we set up the pointers to the endpoints in the edge_open function,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 * as the structures aren't created yet. */
2963
2964 /* set up our port private structures */
2965 for (i = 0; i < serial->num_ports; ++i) {
Julia Lawall1ac93a32010-05-13 22:00:40 +02002966 edge_port = kzalloc(sizeof(struct edgeport_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 if (edge_port == NULL) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002968 dev_err(ddev, "%s - Out of memory\n", __func__);
Chris Lundbfd5df32006-06-03 13:58:19 -07002969 for (j = 0; j < i; ++j) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002970 kfree(usb_get_serial_port_data(serial->port[j]));
2971 usb_set_serial_port_data(serial->port[j],
2972 NULL);
Chris Lundbfd5df32006-06-03 13:58:19 -07002973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 usb_set_serial_data(serial, NULL);
2975 kfree(edge_serial);
2976 return -ENOMEM;
2977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 spin_lock_init(&edge_port->ep_lock);
2979 edge_port->port = serial->port[i];
2980 usb_set_serial_port_data(serial->port[i], edge_port);
2981 }
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002982
2983 response = 0;
2984
2985 if (edge_serial->is_epic) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002986 /* EPIC thing, set up our interrupt polling now and our read
2987 * urb, so that the device knows it really is connected. */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002988 interrupt_in_found = bulk_in_found = bulk_out_found = false;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002989 for (i = 0; i < serial->interface->altsetting[0]
2990 .desc.bNumEndpoints; ++i) {
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002991 struct usb_endpoint_descriptor *endpoint;
2992 int buffer_size;
2993
Alan Cox03f0dbf2008-07-22 11:16:34 +01002994 endpoint = &serial->interface->altsetting[0].
2995 endpoint[i].desc;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07002996 buffer_size = usb_endpoint_maxp(endpoint);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002997 if (!interrupt_in_found &&
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002998 (usb_endpoint_is_int_in(endpoint))) {
2999 /* we found a interrupt in endpoint */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003000 dev_dbg(ddev, "found interrupt in\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003001
3002 /* not set up yet, so do it now */
Alan Cox03f0dbf2008-07-22 11:16:34 +01003003 edge_serial->interrupt_read_urb =
3004 usb_alloc_urb(0, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003005 if (!edge_serial->interrupt_read_urb) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003006 dev_err(ddev, "out of memory\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003007 return -ENOMEM;
3008 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01003009 edge_serial->interrupt_in_buffer =
3010 kmalloc(buffer_size, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003011 if (!edge_serial->interrupt_in_buffer) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003012 dev_err(ddev, "out of memory\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003013 usb_free_urb(edge_serial->interrupt_read_urb);
3014 return -ENOMEM;
3015 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01003016 edge_serial->interrupt_in_endpoint =
3017 endpoint->bEndpointAddress;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003018
3019 /* set up our interrupt urb */
Alan Cox03f0dbf2008-07-22 11:16:34 +01003020 usb_fill_int_urb(
3021 edge_serial->interrupt_read_urb,
3022 dev,
3023 usb_rcvintpipe(dev,
3024 endpoint->bEndpointAddress),
3025 edge_serial->interrupt_in_buffer,
3026 buffer_size,
3027 edge_interrupt_callback,
3028 edge_serial,
3029 endpoint->bInterval);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003030
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003031 interrupt_in_found = true;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003032 }
3033
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003034 if (!bulk_in_found &&
Alan Cox03f0dbf2008-07-22 11:16:34 +01003035 (usb_endpoint_is_bulk_in(endpoint))) {
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003036 /* we found a bulk in endpoint */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003037 dev_dbg(ddev, "found bulk in\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003038
3039 /* not set up yet, so do it now */
Alan Cox03f0dbf2008-07-22 11:16:34 +01003040 edge_serial->read_urb =
3041 usb_alloc_urb(0, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003042 if (!edge_serial->read_urb) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003043 dev_err(ddev, "out of memory\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003044 return -ENOMEM;
3045 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01003046 edge_serial->bulk_in_buffer =
3047 kmalloc(buffer_size, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003048 if (!edge_serial->bulk_in_buffer) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07003049 dev_err(&dev->dev, "out of memory\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003050 usb_free_urb(edge_serial->read_urb);
3051 return -ENOMEM;
3052 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01003053 edge_serial->bulk_in_endpoint =
3054 endpoint->bEndpointAddress;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003055
3056 /* set up our bulk in urb */
3057 usb_fill_bulk_urb(edge_serial->read_urb, dev,
Alan Cox03f0dbf2008-07-22 11:16:34 +01003058 usb_rcvbulkpipe(dev,
3059 endpoint->bEndpointAddress),
3060 edge_serial->bulk_in_buffer,
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07003061 usb_endpoint_maxp(endpoint),
Alan Cox03f0dbf2008-07-22 11:16:34 +01003062 edge_bulk_in_callback,
3063 edge_serial);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003064 bulk_in_found = true;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003065 }
3066
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003067 if (!bulk_out_found &&
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003068 (usb_endpoint_is_bulk_out(endpoint))) {
3069 /* we found a bulk out endpoint */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003070 dev_dbg(ddev, "found bulk out\n");
Alan Cox03f0dbf2008-07-22 11:16:34 +01003071 edge_serial->bulk_out_endpoint =
3072 endpoint->bEndpointAddress;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003073 bulk_out_found = true;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003074 }
3075 }
3076
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003077 if (!interrupt_in_found || !bulk_in_found || !bulk_out_found) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003078 dev_err(ddev, "Error - the proper endpoints were not found!\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003079 return -ENODEV;
3080 }
3081
3082 /* start interrupt read for this edgeport this interrupt will
3083 * continue as long as the edgeport is connected */
Alan Cox03f0dbf2008-07-22 11:16:34 +01003084 response = usb_submit_urb(edge_serial->interrupt_read_urb,
3085 GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003086 if (response)
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003087 dev_err(ddev, "%s - Error %d submitting control urb\n",
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07003088 __func__, response);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003089 }
3090 return response;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091}
3092
3093
3094/****************************************************************************
Alan Sternf9c99bb2009-06-02 11:53:55 -04003095 * edge_disconnect
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 * This function is called whenever the device is removed from the usb bus.
3097 ****************************************************************************/
Alan Sternf9c99bb2009-06-02 11:53:55 -04003098static void edge_disconnect(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099{
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003100 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 /* stop reads and writes on all ports */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003103 /* free up our endpoint stuff */
3104 if (edge_serial->is_epic) {
Oliver Neukum74ac07e2007-06-13 18:50:41 +02003105 usb_kill_urb(edge_serial->interrupt_read_urb);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003106 usb_free_urb(edge_serial->interrupt_read_urb);
3107 kfree(edge_serial->interrupt_in_buffer);
3108
Oliver Neukum74ac07e2007-06-13 18:50:41 +02003109 usb_kill_urb(edge_serial->read_urb);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003110 usb_free_urb(edge_serial->read_urb);
3111 kfree(edge_serial->bulk_in_buffer);
3112 }
Alan Sternf9c99bb2009-06-02 11:53:55 -04003113}
3114
3115
3116/****************************************************************************
3117 * edge_release
3118 * This function is called when the device structure is deallocated.
3119 ****************************************************************************/
3120static void edge_release(struct usb_serial *serial)
3121{
3122 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
3123 int i;
3124
Alan Sternf9c99bb2009-06-02 11:53:55 -04003125 for (i = 0; i < serial->num_ports; ++i)
3126 kfree(usb_get_serial_port_data(serial->port[i]));
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003127
3128 kfree(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129}
3130
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07003131module_usb_serial_driver(serial_drivers, id_table_combined);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132
Alan Cox03f0dbf2008-07-22 11:16:34 +01003133MODULE_AUTHOR(DRIVER_AUTHOR);
3134MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135MODULE_LICENSE("GPL");
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05303136MODULE_FIRMWARE("edgeport/boot.fw");
3137MODULE_FIRMWARE("edgeport/boot2.fw");
3138MODULE_FIRMWARE("edgeport/down.fw");
3139MODULE_FIRMWARE("edgeport/down2.fw");