blob: 16ef8f3714d95c3a7f0584b4f72823a6d033c791 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Edgeport USB Serial Converter driver
3 *
4 * Copyright (C) 2000 Inside Out Networks, All rights reserved.
5 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * Supports the following devices:
13 * Edgeport/4
14 * Edgeport/4t
15 * Edgeport/2
16 * Edgeport/4i
17 * Edgeport/2i
18 * Edgeport/421
19 * Edgeport/21
20 * Rapidport/4
21 * Edgeport/8
22 * Edgeport/2D8
23 * Edgeport/4D8
24 * Edgeport/8i
25 *
26 * For questions or problems with this driver, contact Inside Out
27 * Networks technical support, or Peter Berger <pberger@brimson.com>,
28 * or Al Borchers <alborchers@steinerpoint.com>.
29 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 */
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/kernel.h>
33#include <linux/jiffies.h>
34#include <linux/errno.h>
35#include <linux/init.h>
36#include <linux/slab.h>
37#include <linux/tty.h>
38#include <linux/tty_driver.h>
39#include <linux/tty_flip.h>
40#include <linux/module.h>
41#include <linux/spinlock.h>
42#include <linux/serial.h>
43#include <linux/ioctl.h>
44#include <linux/wait.h>
Jaswinder Singh5b9ea932008-07-03 17:00:23 +053045#include <linux/firmware.h>
46#include <linux/ihex.h>
Alan Cox03f0dbf2008-07-22 11:16:34 +010047#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070049#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include "io_edgeport.h"
51#include "io_ionsp.h" /* info for the iosp messages */
52#include "io_16654.h" /* 16654 UART defines */
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
55#define DRIVER_DESC "Edgeport USB Serial Driver"
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#define MAX_NAME_LEN 64
58
59#define CHASE_TIMEOUT (5*HZ) /* 5 seconds */
60#define OPEN_TIMEOUT (5*HZ) /* 5 seconds */
61#define COMMAND_TIMEOUT (5*HZ) /* 5 seconds */
62
63/* receive port state */
64enum RXSTATE {
Alan Cox03f0dbf2008-07-22 11:16:34 +010065 EXPECT_HDR1 = 0, /* Expect header byte 1 */
66 EXPECT_HDR2 = 1, /* Expect header byte 2 */
67 EXPECT_DATA = 2, /* Expect 'RxBytesRemaining' data */
68 EXPECT_HDR3 = 3, /* Expect header byte 3 (for status hdrs only) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070069};
70
71
Alan Cox03f0dbf2008-07-22 11:16:34 +010072/* Transmit Fifo
73 * This Transmit queue is an extension of the edgeport Rx buffer.
74 * The maximum amount of data buffered in both the edgeport
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 * Rx buffer (maxTxCredits) and this buffer will never exceed maxTxCredits.
76 */
77struct TxFifo {
78 unsigned int head; /* index to head pointer (write) */
79 unsigned int tail; /* index to tail pointer (read) */
80 unsigned int count; /* Bytes in queue */
81 unsigned int size; /* Max size of queue (equal to Max number of TxCredits) */
82 unsigned char *fifo; /* allocated Buffer */
83};
84
85/* This structure holds all of the local port information */
86struct edgeport_port {
87 __u16 txCredits; /* our current credits for this port */
88 __u16 maxTxCredits; /* the max size of the port */
89
90 struct TxFifo txfifo; /* transmit fifo -- size will be maxTxCredits */
91 struct urb *write_urb; /* write URB for this port */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +010092 bool write_in_progress; /* 'true' while a write URB is outstanding */
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 spinlock_t ep_lock;
94
95 __u8 shadowLCR; /* last LCR value received */
96 __u8 shadowMCR; /* last MCR value received */
97 __u8 shadowMSR; /* last MSR value received */
98 __u8 shadowLSR; /* last LSR value received */
99 __u8 shadowXonChar; /* last value set as XON char in Edgeport */
100 __u8 shadowXoffChar; /* last value set as XOFF char in Edgeport */
101 __u8 validDataMask;
102 __u32 baudRate;
103
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100104 bool open;
105 bool openPending;
106 bool commandPending;
107 bool closePending;
108 bool chaseResponsePending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
111 wait_queue_head_t wait_open; /* for handling sleeping while waiting for open to finish */
112 wait_queue_head_t wait_command; /* for handling sleeping while waiting for command to finish */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 struct usb_serial_port *port; /* loop back to the owner of this object */
115};
116
117
118/* This structure holds all of the individual device information */
119struct edgeport_serial {
Pete Zaitcevad933752006-05-22 21:49:44 -0700120 char name[MAX_NAME_LEN+2]; /* string name of this device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 struct edge_manuf_descriptor manuf_descriptor; /* the manufacturer descriptor */
123 struct edge_boot_descriptor boot_descriptor; /* the boot firmware descriptor */
124 struct edgeport_product_info product_info; /* Product Info */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800125 struct edge_compatibility_descriptor epic_descriptor; /* Edgeport compatible descriptor */
126 int is_epic; /* flag if EPiC device or not */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 __u8 interrupt_in_endpoint; /* the interrupt endpoint handle */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100129 unsigned char *interrupt_in_buffer; /* the buffer we use for the interrupt endpoint */
130 struct urb *interrupt_read_urb; /* our interrupt urb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 __u8 bulk_in_endpoint; /* the bulk in endpoint handle */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100133 unsigned char *bulk_in_buffer; /* the buffer we use for the bulk in endpoint */
134 struct urb *read_urb; /* our bulk read urb */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100135 bool read_in_progress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 spinlock_t es_lock;
137
138 __u8 bulk_out_endpoint; /* the bulk out endpoint handle */
139
140 __s16 rxBytesAvail; /* the number of bytes that we need to read from this device */
141
142 enum RXSTATE rxState; /* the current state of the bulk receive processor */
143 __u8 rxHeader1; /* receive header byte 1 */
144 __u8 rxHeader2; /* receive header byte 2 */
145 __u8 rxHeader3; /* receive header byte 3 */
146 __u8 rxPort; /* the port that we are currently receiving data for */
147 __u8 rxStatusCode; /* the receive status code */
148 __u8 rxStatusParam; /* the receive status paramater */
149 __s16 rxBytesRemaining; /* the number of port bytes left to read */
150 struct usb_serial *serial; /* loop back to the owner of this object */
151};
152
153/* baud rate information */
154struct divisor_table_entry {
155 __u32 BaudRate;
156 __u16 Divisor;
157};
158
Alan Cox03f0dbf2008-07-22 11:16:34 +0100159/*
160 * Define table of divisors for Rev A EdgePort/4 hardware
161 * These assume a 3.6864MHz crystal, the standard /16, and
162 * MCR.7 = 0.
163 */
164
Arjan van de Ven4c4c9432005-11-29 09:43:42 +0100165static const struct divisor_table_entry divisor_table[] = {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100166 { 50, 4608},
167 { 75, 3072},
168 { 110, 2095}, /* 2094.545455 => 230450 => .0217 % over */
169 { 134, 1713}, /* 1713.011152 => 230398.5 => .00065% under */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 { 150, 1536},
171 { 300, 768},
172 { 600, 384},
173 { 1200, 192},
174 { 1800, 128},
175 { 2400, 96},
176 { 4800, 48},
177 { 7200, 32},
178 { 9600, 24},
179 { 14400, 16},
180 { 19200, 12},
181 { 38400, 6},
182 { 57600, 4},
183 { 115200, 2},
184 { 230400, 1},
185};
186
Greg Kroah-Hartman36ccce42012-02-28 13:11:51 -0800187/* Number of outstanding Command Write Urbs */
188static atomic_t CmdUrbs = ATOMIC_INIT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190
191/* local function prototypes */
192
193/* function prototypes for all URB callbacks */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100194static void edge_interrupt_callback(struct urb *urb);
195static void edge_bulk_in_callback(struct urb *urb);
196static void edge_bulk_out_data_callback(struct urb *urb);
197static void edge_bulk_out_cmd_callback(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199/* function prototypes for the usbserial callbacks */
Alan Coxa509a7e2009-09-19 13:13:26 -0700200static int edge_open(struct tty_struct *tty, struct usb_serial_port *port);
Alan Cox335f8512009-06-11 12:26:29 +0100201static void edge_close(struct usb_serial_port *port);
Alan Cox03f0dbf2008-07-22 11:16:34 +0100202static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
203 const unsigned char *buf, int count);
204static int edge_write_room(struct tty_struct *tty);
205static int edge_chars_in_buffer(struct tty_struct *tty);
206static void edge_throttle(struct tty_struct *tty);
207static void edge_unthrottle(struct tty_struct *tty);
208static void edge_set_termios(struct tty_struct *tty,
209 struct usb_serial_port *port,
210 struct ktermios *old_termios);
Alan Cox00a0d0d2011-02-14 16:27:06 +0000211static int edge_ioctl(struct tty_struct *tty,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100212 unsigned int cmd, unsigned long arg);
213static void edge_break(struct tty_struct *tty, int break_state);
Alan Cox60b33c12011-02-14 16:26:14 +0000214static int edge_tiocmget(struct tty_struct *tty);
Alan Cox20b9d172011-02-14 16:26:50 +0000215static int edge_tiocmset(struct tty_struct *tty,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100216 unsigned int set, unsigned int clear);
217static int edge_startup(struct usb_serial *serial);
Alan Sternf9c99bb2009-06-02 11:53:55 -0400218static void edge_disconnect(struct usb_serial *serial);
219static void edge_release(struct usb_serial *serial);
Johan Hovoldc27f3ef2012-10-17 13:34:57 +0200220static int edge_port_probe(struct usb_serial_port *port);
221static int edge_port_remove(struct usb_serial_port *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223#include "io_tables.h" /* all of the devices that this driver supports */
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225/* function prototypes for all of our local functions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Alan Cox03f0dbf2008-07-22 11:16:34 +0100227static void process_rcvd_data(struct edgeport_serial *edge_serial,
228 unsigned char *buffer, __u16 bufferLength);
229static void process_rcvd_status(struct edgeport_serial *edge_serial,
230 __u8 byte2, __u8 byte3);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100231static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
232 int length);
Alan Cox03f0dbf2008-07-22 11:16:34 +0100233static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr);
234static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
235 __u8 lsr, __u8 data);
236static int send_iosp_ext_cmd(struct edgeport_port *edge_port, __u8 command,
237 __u8 param);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700238static int calc_baud_rate_divisor(struct device *dev, int baud_rate, int *divisor);
Alan Cox03f0dbf2008-07-22 11:16:34 +0100239static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
240 int baudRate);
241static void change_port_settings(struct tty_struct *tty,
242 struct edgeport_port *edge_port,
243 struct ktermios *old_termios);
244static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
245 __u8 regNum, __u8 regValue);
246static int write_cmd_usb(struct edgeport_port *edge_port,
247 unsigned char *buffer, int writeLength);
248static void send_more_port_data(struct edgeport_serial *edge_serial,
249 struct edgeport_port *edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Alan Cox03f0dbf2008-07-22 11:16:34 +0100251static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
252 __u16 length, const __u8 *data);
253static int rom_read(struct usb_serial *serial, __u16 extAddr, __u16 addr,
254 __u16 length, __u8 *data);
255static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
256 __u16 length, const __u8 *data);
257static void get_manufacturing_desc(struct edgeport_serial *edge_serial);
258static void get_boot_desc(struct edgeport_serial *edge_serial);
259static void load_application_firmware(struct edgeport_serial *edge_serial);
260
261static void unicode_to_ascii(char *string, int buflen,
262 __le16 *unicode, int unicode_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264
Alan Cox03f0dbf2008-07-22 11:16:34 +0100265/* ************************************************************************ */
266/* ************************************************************************ */
267/* ************************************************************************ */
268/* ************************************************************************ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270/************************************************************************
271 * *
272 * update_edgeport_E2PROM() Compare current versions of *
273 * Boot ROM and Manufacture *
274 * Descriptors with versions *
275 * embedded in this driver *
276 * *
277 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100278static void update_edgeport_E2PROM(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700280 struct device *dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 __u32 BootCurVer;
282 __u32 BootNewVer;
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530283 __u8 BootMajorVersion;
284 __u8 BootMinorVersion;
285 __u16 BootBuildNumber;
286 __u32 Bootaddr;
287 const struct ihex_binrec *rec;
288 const struct firmware *fw;
289 const char *fw_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 int response;
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 switch (edge_serial->product_info.iDownloadFile) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100293 case EDGE_DOWNLOAD_FILE_I930:
294 fw_name = "edgeport/boot.fw";
295 break;
296 case EDGE_DOWNLOAD_FILE_80251:
297 fw_name = "edgeport/boot2.fw";
298 break;
299 default:
300 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530303 response = request_ihex_firmware(&fw, fw_name,
304 &edge_serial->serial->dev->dev);
305 if (response) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700306 dev_err(dev, "Failed to load image \"%s\" err %d\n",
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530307 fw_name, response);
308 return;
309 }
310
311 rec = (const struct ihex_binrec *)fw->data;
312 BootMajorVersion = rec->data[0];
313 BootMinorVersion = rec->data[1];
314 BootBuildNumber = (rec->data[2] << 8) | rec->data[3];
315
Alan Cox03f0dbf2008-07-22 11:16:34 +0100316 /* Check Boot Image Version */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 BootCurVer = (edge_serial->boot_descriptor.MajorVersion << 24) +
318 (edge_serial->boot_descriptor.MinorVersion << 16) +
319 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber);
320
321 BootNewVer = (BootMajorVersion << 24) +
322 (BootMinorVersion << 16) +
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530323 BootBuildNumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700325 dev_dbg(dev, "Current Boot Image version %d.%d.%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 edge_serial->boot_descriptor.MajorVersion,
327 edge_serial->boot_descriptor.MinorVersion,
328 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
329
330
331 if (BootNewVer > BootCurVer) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700332 dev_dbg(dev, "**Update Boot Image from %d.%d.%d to %d.%d.%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 edge_serial->boot_descriptor.MajorVersion,
334 edge_serial->boot_descriptor.MinorVersion,
335 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber),
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530336 BootMajorVersion, BootMinorVersion, BootBuildNumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700338 dev_dbg(dev, "Downloading new Boot Image\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530340 for (rec = ihex_next_binrec(rec); rec;
341 rec = ihex_next_binrec(rec)) {
342 Bootaddr = be32_to_cpu(rec->addr);
343 response = rom_write(edge_serial->serial,
344 Bootaddr >> 16,
345 Bootaddr & 0xFFFF,
346 be16_to_cpu(rec->len),
347 &rec->data[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (response < 0) {
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530349 dev_err(&edge_serial->serial->dev->dev,
350 "rom_write failed (%x, %x, %d)\n",
351 Bootaddr >> 16, Bootaddr & 0xFFFF,
352 be16_to_cpu(rec->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 break;
354 }
355 }
356 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700357 dev_dbg(dev, "Boot Image -- already up to date\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530359 release_firmware(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362#if 0
363/************************************************************************
364 *
365 * Get string descriptor from device
366 *
367 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100368static int get_string_desc(struct usb_device *dev, int Id,
369 struct usb_string_descriptor **pRetDesc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
371 struct usb_string_descriptor StringDesc;
372 struct usb_string_descriptor *pStringDesc;
373
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700374 dev_dbg(&dev->dev, "%s - USB String ID = %d\n", __func__, Id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Alan Cox03f0dbf2008-07-22 11:16:34 +0100376 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc,
377 sizeof(StringDesc)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Alan Cox03f0dbf2008-07-22 11:16:34 +0100380 pStringDesc = kmalloc(StringDesc.bLength, GFP_KERNEL);
381 if (!pStringDesc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Alan Cox03f0dbf2008-07-22 11:16:34 +0100384 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc,
385 StringDesc.bLength)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 kfree(pStringDesc);
387 return -1;
388 }
389
390 *pRetDesc = pStringDesc;
391 return 0;
392}
393#endif
394
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700395static void dump_product_info(struct edgeport_serial *edge_serial,
396 struct edgeport_product_info *product_info)
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800397{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700398 struct device *dev = &edge_serial->serial->dev->dev;
399
Alan Cox03f0dbf2008-07-22 11:16:34 +0100400 /* Dump Product Info structure */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700401 dev_dbg(dev, "**Product Information:\n");
402 dev_dbg(dev, " ProductId %x\n", product_info->ProductId);
403 dev_dbg(dev, " NumPorts %d\n", product_info->NumPorts);
404 dev_dbg(dev, " ProdInfoVer %d\n", product_info->ProdInfoVer);
405 dev_dbg(dev, " IsServer %d\n", product_info->IsServer);
406 dev_dbg(dev, " IsRS232 %d\n", product_info->IsRS232);
407 dev_dbg(dev, " IsRS422 %d\n", product_info->IsRS422);
408 dev_dbg(dev, " IsRS485 %d\n", product_info->IsRS485);
409 dev_dbg(dev, " RomSize %d\n", product_info->RomSize);
410 dev_dbg(dev, " RamSize %d\n", product_info->RamSize);
411 dev_dbg(dev, " CpuRev %x\n", product_info->CpuRev);
412 dev_dbg(dev, " BoardRev %x\n", product_info->BoardRev);
413 dev_dbg(dev, " BootMajorVersion %d.%d.%d\n",
414 product_info->BootMajorVersion,
415 product_info->BootMinorVersion,
416 le16_to_cpu(product_info->BootBuildNumber));
417 dev_dbg(dev, " FirmwareMajorVersion %d.%d.%d\n",
418 product_info->FirmwareMajorVersion,
419 product_info->FirmwareMinorVersion,
420 le16_to_cpu(product_info->FirmwareBuildNumber));
421 dev_dbg(dev, " ManufactureDescDate %d/%d/%d\n",
422 product_info->ManufactureDescDate[0],
423 product_info->ManufactureDescDate[1],
424 product_info->ManufactureDescDate[2]+1900);
425 dev_dbg(dev, " iDownloadFile 0x%x\n",
426 product_info->iDownloadFile);
427 dev_dbg(dev, " EpicVer %d\n", product_info->EpicVer);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800428}
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430static void get_product_info(struct edgeport_serial *edge_serial)
431{
432 struct edgeport_product_info *product_info = &edge_serial->product_info;
433
Alan Cox03f0dbf2008-07-22 11:16:34 +0100434 memset(product_info, 0, sizeof(struct edgeport_product_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Alan Cox03f0dbf2008-07-22 11:16:34 +0100436 product_info->ProductId = (__u16)(le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct) & ~ION_DEVICE_ID_80251_NETCHIP);
437 product_info->NumPorts = edge_serial->manuf_descriptor.NumPorts;
438 product_info->ProdInfoVer = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Alan Cox03f0dbf2008-07-22 11:16:34 +0100440 product_info->RomSize = edge_serial->manuf_descriptor.RomSize;
441 product_info->RamSize = edge_serial->manuf_descriptor.RamSize;
442 product_info->CpuRev = edge_serial->manuf_descriptor.CpuRev;
443 product_info->BoardRev = edge_serial->manuf_descriptor.BoardRev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Alan Cox03f0dbf2008-07-22 11:16:34 +0100445 product_info->BootMajorVersion =
446 edge_serial->boot_descriptor.MajorVersion;
447 product_info->BootMinorVersion =
448 edge_serial->boot_descriptor.MinorVersion;
449 product_info->BootBuildNumber =
450 edge_serial->boot_descriptor.BuildNumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Alan Cox03f0dbf2008-07-22 11:16:34 +0100452 memcpy(product_info->ManufactureDescDate,
453 edge_serial->manuf_descriptor.DescDate,
454 sizeof(edge_serial->manuf_descriptor.DescDate));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Alan Cox03f0dbf2008-07-22 11:16:34 +0100456 /* check if this is 2nd generation hardware */
457 if (le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct)
458 & ION_DEVICE_ID_80251_NETCHIP)
459 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_80251;
460 else
461 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_I930;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700462
Alan Cox03f0dbf2008-07-22 11:16:34 +0100463 /* Determine Product type and set appropriate flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 switch (DEVICE_ID_FROM_USB_PRODUCT_ID(product_info->ProductId)) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100465 case ION_DEVICE_ID_EDGEPORT_COMPATIBLE:
466 case ION_DEVICE_ID_EDGEPORT_4T:
467 case ION_DEVICE_ID_EDGEPORT_4:
468 case ION_DEVICE_ID_EDGEPORT_2:
469 case ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU:
470 case ION_DEVICE_ID_EDGEPORT_8:
471 case ION_DEVICE_ID_EDGEPORT_421:
472 case ION_DEVICE_ID_EDGEPORT_21:
473 case ION_DEVICE_ID_EDGEPORT_2_DIN:
474 case ION_DEVICE_ID_EDGEPORT_4_DIN:
475 case ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU:
476 product_info->IsRS232 = 1;
477 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Alan Cox03f0dbf2008-07-22 11:16:34 +0100479 case ION_DEVICE_ID_EDGEPORT_2I: /* Edgeport/2 RS422/RS485 */
480 product_info->IsRS422 = 1;
481 product_info->IsRS485 = 1;
482 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Alan Cox03f0dbf2008-07-22 11:16:34 +0100484 case ION_DEVICE_ID_EDGEPORT_8I: /* Edgeport/4 RS422 */
485 case ION_DEVICE_ID_EDGEPORT_4I: /* Edgeport/4 RS422 */
486 product_info->IsRS422 = 1;
487 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
489
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700490 dump_product_info(edge_serial, product_info);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800491}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800493static int get_epic_descriptor(struct edgeport_serial *ep)
494{
495 int result;
496 struct usb_serial *serial = ep->serial;
497 struct edgeport_product_info *product_info = &ep->product_info;
498 struct edge_compatibility_descriptor *epic = &ep->epic_descriptor;
499 struct edge_compatibility_bits *bits;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700500 struct device *dev = &serial->dev->dev;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800501
502 ep->is_epic = 0;
503 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
504 USB_REQUEST_ION_GET_EPIC_DESC,
505 0xC0, 0x00, 0x00,
506 &ep->epic_descriptor,
507 sizeof(struct edge_compatibility_descriptor),
508 300);
509
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800510 if (result > 0) {
511 ep->is_epic = 1;
512 memset(product_info, 0, sizeof(struct edgeport_product_info));
513
Alan Cox03f0dbf2008-07-22 11:16:34 +0100514 product_info->NumPorts = epic->NumPorts;
515 product_info->ProdInfoVer = 0;
516 product_info->FirmwareMajorVersion = epic->MajorVersion;
517 product_info->FirmwareMinorVersion = epic->MinorVersion;
518 product_info->FirmwareBuildNumber = epic->BuildNumber;
519 product_info->iDownloadFile = epic->iDownloadFile;
520 product_info->EpicVer = epic->EpicVer;
521 product_info->Epic = epic->Supports;
522 product_info->ProductId = ION_DEVICE_ID_EDGEPORT_COMPATIBLE;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700523 dump_product_info(ep, product_info);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800524
525 bits = &ep->epic_descriptor.Supports;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700526 dev_dbg(dev, "**EPIC descriptor:\n");
527 dev_dbg(dev, " VendEnableSuspend: %s\n", bits->VendEnableSuspend ? "TRUE": "FALSE");
528 dev_dbg(dev, " IOSPOpen : %s\n", bits->IOSPOpen ? "TRUE": "FALSE");
529 dev_dbg(dev, " IOSPClose : %s\n", bits->IOSPClose ? "TRUE": "FALSE");
530 dev_dbg(dev, " IOSPChase : %s\n", bits->IOSPChase ? "TRUE": "FALSE");
531 dev_dbg(dev, " IOSPSetRxFlow : %s\n", bits->IOSPSetRxFlow ? "TRUE": "FALSE");
532 dev_dbg(dev, " IOSPSetTxFlow : %s\n", bits->IOSPSetTxFlow ? "TRUE": "FALSE");
533 dev_dbg(dev, " IOSPSetXChar : %s\n", bits->IOSPSetXChar ? "TRUE": "FALSE");
534 dev_dbg(dev, " IOSPRxCheck : %s\n", bits->IOSPRxCheck ? "TRUE": "FALSE");
535 dev_dbg(dev, " IOSPSetClrBreak : %s\n", bits->IOSPSetClrBreak ? "TRUE": "FALSE");
536 dev_dbg(dev, " IOSPWriteMCR : %s\n", bits->IOSPWriteMCR ? "TRUE": "FALSE");
537 dev_dbg(dev, " IOSPWriteLCR : %s\n", bits->IOSPWriteLCR ? "TRUE": "FALSE");
538 dev_dbg(dev, " IOSPSetBaudRate : %s\n", bits->IOSPSetBaudRate ? "TRUE": "FALSE");
539 dev_dbg(dev, " TrueEdgeport : %s\n", bits->TrueEdgeport ? "TRUE": "FALSE");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800540 }
541
542 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
545
546/************************************************************************/
547/************************************************************************/
548/* U S B C A L L B A C K F U N C T I O N S */
549/* U S B C A L L B A C K F U N C T I O N S */
550/************************************************************************/
551/************************************************************************/
552
553/*****************************************************************************
554 * edge_interrupt_callback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100555 * this is the callback function for when we have received data on the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 * interrupt endpoint.
557 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100558static void edge_interrupt_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700560 struct edgeport_serial *edge_serial = urb->context;
561 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 struct edgeport_port *edge_port;
563 struct usb_serial_port *port;
Alan Cox4a90f092008-10-13 10:39:46 +0100564 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 unsigned char *data = urb->transfer_buffer;
566 int length = urb->actual_length;
567 int bytes_avail;
568 int position;
569 int txCredits;
570 int portNumber;
571 int result;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700572 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700574 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 case 0:
576 /* success */
577 break;
578 case -ECONNRESET:
579 case -ENOENT:
580 case -ESHUTDOWN:
581 /* this urb is terminated, clean up */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700582 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return;
584 default:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700585 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 goto exit;
587 }
588
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700589 dev = &edge_serial->serial->dev->dev;
590
Alan Cox03f0dbf2008-07-22 11:16:34 +0100591 /* process this interrupt-read even if there are no ports open */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 if (length) {
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100593 usb_serial_debug_data(dev, __func__, length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 if (length > 1) {
596 bytes_avail = data[0] | (data[1] << 8);
597 if (bytes_avail) {
598 spin_lock(&edge_serial->es_lock);
599 edge_serial->rxBytesAvail += bytes_avail;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700600 dev_dbg(dev,
601 "%s - bytes_avail=%d, rxBytesAvail=%d, read_in_progress=%d\n",
602 __func__, bytes_avail,
603 edge_serial->rxBytesAvail,
604 edge_serial->read_in_progress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 if (edge_serial->rxBytesAvail > 0 &&
607 !edge_serial->read_in_progress) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700608 dev_dbg(dev, "%s - posting a read\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100609 edge_serial->read_in_progress = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Alan Cox03f0dbf2008-07-22 11:16:34 +0100611 /* we have pending bytes on the
612 bulk in pipe, send a request */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 result = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
614 if (result) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700615 dev_err(dev,
616 "%s - usb_submit_urb(read bulk) failed with result = %d\n",
617 __func__, result);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100618 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
620 }
621 spin_unlock(&edge_serial->es_lock);
622 }
623 }
624 /* grab the txcredits for the ports if available */
625 position = 2;
626 portNumber = 0;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100627 while ((position < length) &&
628 (portNumber < edge_serial->serial->num_ports)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 txCredits = data[position] | (data[position+1] << 8);
630 if (txCredits) {
631 port = edge_serial->serial->port[portNumber];
632 edge_port = usb_get_serial_port_data(port);
633 if (edge_port->open) {
634 spin_lock(&edge_port->ep_lock);
635 edge_port->txCredits += txCredits;
636 spin_unlock(&edge_port->ep_lock);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700637 dev_dbg(dev, "%s - txcredits for port%d = %d\n",
638 __func__, portNumber,
639 edge_port->txCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Alan Cox03f0dbf2008-07-22 11:16:34 +0100641 /* tell the tty driver that something
642 has changed */
Alan Cox4a90f092008-10-13 10:39:46 +0100643 tty = tty_port_tty_get(
644 &edge_port->port->port);
645 if (tty) {
646 tty_wakeup(tty);
647 tty_kref_put(tty);
648 }
Alan Cox03f0dbf2008-07-22 11:16:34 +0100649 /* Since we have more credit, check
650 if more data can be sent */
651 send_more_port_data(edge_serial,
652 edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
654 }
655 position += 2;
656 ++portNumber;
657 }
658 }
659
660exit:
Alan Cox03f0dbf2008-07-22 11:16:34 +0100661 result = usb_submit_urb(urb, GFP_ATOMIC);
662 if (result)
663 dev_err(&urb->dev->dev,
664 "%s - Error %d submitting control urb\n",
665 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667
668
669/*****************************************************************************
670 * edge_bulk_in_callback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100671 * this is the callback function for when we have received data on the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 * bulk in endpoint.
673 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100674static void edge_bulk_in_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Ming Leicdc97792008-02-24 18:41:47 +0800676 struct edgeport_serial *edge_serial = urb->context;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700677 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700679 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 __u16 raw_data_length;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700681 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700683 if (status) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700684 dev_dbg(&urb->dev->dev, "%s - nonzero read bulk status received: %d\n",
685 __func__, status);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100686 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return;
688 }
689
690 if (urb->actual_length == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700691 dev_dbg(&urb->dev->dev, "%s - read bulk callback with no data\n", __func__);
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
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700696 dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 raw_data_length = urb->actual_length;
698
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100699 usb_serial_debug_data(dev, __func__, raw_data_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 spin_lock(&edge_serial->es_lock);
702
703 /* decrement our rxBytes available by the number that we just got */
704 edge_serial->rxBytesAvail -= raw_data_length;
705
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700706 dev_dbg(dev, "%s - Received = %d, rxBytesAvail %d\n", __func__,
707 raw_data_length, edge_serial->rxBytesAvail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Alan Cox03f0dbf2008-07-22 11:16:34 +0100709 process_rcvd_data(edge_serial, data, urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711 /* check to see if there's any more data for us to read */
712 if (edge_serial->rxBytesAvail > 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700713 dev_dbg(dev, "%s - posting a read\n", __func__);
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700714 retval = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
715 if (retval) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700716 dev_err(dev,
717 "%s - usb_submit_urb(read bulk) failed, retval = %d\n",
718 __func__, retval);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100719 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 }
721 } else {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100722 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
724
725 spin_unlock(&edge_serial->es_lock);
726}
727
728
729/*****************************************************************************
730 * edge_bulk_out_data_callback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100731 * this is the callback function for when we have finished sending
732 * serial data on the bulk out endpoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100734static void edge_bulk_out_data_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
Ming Leicdc97792008-02-24 18:41:47 +0800736 struct edgeport_port *edge_port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 struct tty_struct *tty;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700738 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700740 if (status) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700741 dev_dbg(&urb->dev->dev,
742 "%s - nonzero write bulk status received: %d\n",
743 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
745
Alan Cox4a90f092008-10-13 10:39:46 +0100746 tty = tty_port_tty_get(&edge_port->port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 if (tty && edge_port->open) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100749 /* let the tty driver wakeup if it has a special
750 write_wakeup function */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 tty_wakeup(tty);
752 }
Alan Cox4a90f092008-10-13 10:39:46 +0100753 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Alan Cox03f0dbf2008-07-22 11:16:34 +0100755 /* Release the Write URB */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100756 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Alan Cox03f0dbf2008-07-22 11:16:34 +0100758 /* Check if more data needs to be sent */
759 send_more_port_data((struct edgeport_serial *)
760 (usb_get_serial_data(edge_port->port->serial)), edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
762
763
764/*****************************************************************************
765 * BulkOutCmdCallback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100766 * this is the callback function for when we have finished sending a
767 * command on the bulk out endpoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100769static void edge_bulk_out_cmd_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
Ming Leicdc97792008-02-24 18:41:47 +0800771 struct edgeport_port *edge_port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 struct tty_struct *tty;
773 int status = urb->status;
774
Oliver Neukum96c706e2007-03-15 15:27:17 +0100775 atomic_dec(&CmdUrbs);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700776 dev_dbg(&urb->dev->dev, "%s - FREE URB %p (outstanding %d)\n",
777 __func__, urb, atomic_read(&CmdUrbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779
780 /* clean up the transfer buffer */
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700781 kfree(urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 /* Free the command urb */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100784 usb_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 if (status) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700787 dev_dbg(&urb->dev->dev,
788 "%s - nonzero write bulk status received: %d\n",
789 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 return;
791 }
792
793 /* Get pointer to tty */
Alan Cox4a90f092008-10-13 10:39:46 +0100794 tty = tty_port_tty_get(&edge_port->port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 /* tell the tty driver that something has changed */
797 if (tty && edge_port->open)
798 tty_wakeup(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100799 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801 /* we have completed the command */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100802 edge_port->commandPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 wake_up(&edge_port->wait_command);
804}
805
806
807/*****************************************************************************
808 * Driver tty interface functions
809 *****************************************************************************/
810
811/*****************************************************************************
812 * SerialOpen
813 * this function is called by the tty driver when a port is opened
814 * If successful, we return 0
815 * Otherwise we return a negative error number.
816 *****************************************************************************/
Alan Coxa509a7e2009-09-19 13:13:26 -0700817static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
819 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700820 struct device *dev = &port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 struct usb_serial *serial;
822 struct edgeport_serial *edge_serial;
823 int response;
824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (edge_port == NULL)
826 return -ENODEV;
827
Alan Cox03f0dbf2008-07-22 11:16:34 +0100828 /* see if we've set up our endpoint info yet (can't set it up
829 in edge_startup as the structures were not set up at that time.) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 serial = port->serial;
831 edge_serial = usb_get_serial_data(serial);
Alan Cox95da3102008-07-22 11:09:07 +0100832 if (edge_serial == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 if (edge_serial->interrupt_in_buffer == NULL) {
835 struct usb_serial_port *port0 = serial->port[0];
Alan Cox03f0dbf2008-07-22 11:16:34 +0100836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 /* not set up yet, so do it now */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100838 edge_serial->interrupt_in_buffer =
839 port0->interrupt_in_buffer;
840 edge_serial->interrupt_in_endpoint =
841 port0->interrupt_in_endpointAddress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 edge_serial->interrupt_read_urb = port0->interrupt_in_urb;
843 edge_serial->bulk_in_buffer = port0->bulk_in_buffer;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100844 edge_serial->bulk_in_endpoint =
845 port0->bulk_in_endpointAddress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 edge_serial->read_urb = port0->read_urb;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100847 edge_serial->bulk_out_endpoint =
848 port0->bulk_out_endpointAddress;
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 /* set up our interrupt urb */
851 usb_fill_int_urb(edge_serial->interrupt_read_urb,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100852 serial->dev,
853 usb_rcvintpipe(serial->dev,
854 port0->interrupt_in_endpointAddress),
855 port0->interrupt_in_buffer,
856 edge_serial->interrupt_read_urb->transfer_buffer_length,
857 edge_interrupt_callback, edge_serial,
858 edge_serial->interrupt_read_urb->interval);
859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 /* set up our bulk in urb */
861 usb_fill_bulk_urb(edge_serial->read_urb, serial->dev,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100862 usb_rcvbulkpipe(serial->dev,
863 port0->bulk_in_endpointAddress),
864 port0->bulk_in_buffer,
865 edge_serial->read_urb->transfer_buffer_length,
866 edge_bulk_in_callback, edge_serial);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100867 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869 /* start interrupt read for this edgeport
Alan Cox03f0dbf2008-07-22 11:16:34 +0100870 * this interrupt will continue as long
871 * as the edgeport is connected */
872 response = usb_submit_urb(edge_serial->interrupt_read_urb,
873 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 if (response) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700875 dev_err(dev, "%s - Error %d submitting control urb\n",
876 __func__, response);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
878 }
Alan Cox03f0dbf2008-07-22 11:16:34 +0100879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 /* initialize our wait queues */
881 init_waitqueue_head(&edge_port->wait_open);
882 init_waitqueue_head(&edge_port->wait_chase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 init_waitqueue_head(&edge_port->wait_command);
884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 /* initialize our port settings */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100886 edge_port->txCredits = 0; /* Can't send any data yet */
887 /* Must always set this bit to enable ints! */
888 edge_port->shadowMCR = MCR_MASTER_IE;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100889 edge_port->chaseResponsePending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891 /* send a open port command */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100892 edge_port->openPending = true;
893 edge_port->open = false;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100894 response = send_iosp_ext_cmd(edge_port, IOSP_CMD_OPEN_PORT, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 if (response < 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700897 dev_err(dev, "%s - error sending open port command\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100898 edge_port->openPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return -ENODEV;
900 }
901
902 /* now wait for the port to be completely opened */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100903 wait_event_timeout(edge_port->wait_open, !edge_port->openPending,
904 OPEN_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100906 if (!edge_port->open) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 /* open timed out */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700908 dev_dbg(dev, "%s - open timedout\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100909 edge_port->openPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 return -ENODEV;
911 }
912
913 /* create the txfifo */
914 edge_port->txfifo.head = 0;
915 edge_port->txfifo.tail = 0;
916 edge_port->txfifo.count = 0;
917 edge_port->txfifo.size = edge_port->maxTxCredits;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100918 edge_port->txfifo.fifo = kmalloc(edge_port->maxTxCredits, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 if (!edge_port->txfifo.fifo) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700921 dev_dbg(dev, "%s - no memory\n", __func__);
Alan Cox335f8512009-06-11 12:26:29 +0100922 edge_close(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return -ENOMEM;
924 }
925
926 /* Allocate a URB for the write */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100927 edge_port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100928 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
930 if (!edge_port->write_urb) {
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
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700936 dev_dbg(dev, "%s(%d) - Initialize TX fifo to %d bytes\n",
937 __func__, port->number, edge_port->maxTxCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 return 0;
940}
941
942
943/************************************************************************
944 *
945 * block_until_chase_response
946 *
947 * This function will block the close until one of the following:
948 * 1. Response to our Chase comes from Edgeport
Joe Perchesdc0d5c12007-12-17 11:40:18 -0800949 * 2. A timeout of 10 seconds without activity has expired
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 * (1K of Edgeport data @ 2400 baud ==> 4 sec to empty)
951 *
952 ************************************************************************/
953static void block_until_chase_response(struct edgeport_port *edge_port)
954{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700955 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 DEFINE_WAIT(wait);
957 __u16 lastCredits;
958 int timeout = 1*HZ;
959 int loop = 10;
960
961 while (1) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100962 /* Save Last credits */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 lastCredits = edge_port->txCredits;
964
Alan Cox03f0dbf2008-07-22 11:16:34 +0100965 /* Did we get our Chase response */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100966 if (!edge_port->chaseResponsePending) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700967 dev_dbg(dev, "%s - Got Chase Response\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Alan Cox03f0dbf2008-07-22 11:16:34 +0100969 /* did we get all of our credit back? */
970 if (edge_port->txCredits == edge_port->maxTxCredits) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700971 dev_dbg(dev, "%s - Got all credits\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 return;
973 }
974 }
975
Alan Cox03f0dbf2008-07-22 11:16:34 +0100976 /* Block the thread for a while */
977 prepare_to_wait(&edge_port->wait_chase, &wait,
978 TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 schedule_timeout(timeout);
980 finish_wait(&edge_port->wait_chase, &wait);
981
982 if (lastCredits == edge_port->txCredits) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100983 /* No activity.. count down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 loop--;
985 if (loop == 0) {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100986 edge_port->chaseResponsePending = false;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700987 dev_dbg(dev, "%s - Chase TIMEOUT\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 return;
989 }
990 } else {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100991 /* Reset timeout value back to 10 seconds */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700992 dev_dbg(dev, "%s - Last %d, Current %d\n", __func__,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100993 lastCredits, edge_port->txCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 loop = 10;
995 }
996 }
997}
998
999
1000/************************************************************************
1001 *
1002 * block_until_tx_empty
1003 *
1004 * This function will block the close until one of the following:
1005 * 1. TX count are 0
1006 * 2. The edgeport has stopped
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001007 * 3. A timeout of 3 seconds without activity has expired
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 *
1009 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001010static void block_until_tx_empty(struct edgeport_port *edge_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001012 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 DEFINE_WAIT(wait);
1014 struct TxFifo *fifo = &edge_port->txfifo;
1015 __u32 lastCount;
1016 int timeout = HZ/10;
1017 int loop = 30;
1018
1019 while (1) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001020 /* Save Last count */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 lastCount = fifo->count;
1022
Alan Cox03f0dbf2008-07-22 11:16:34 +01001023 /* Is the Edgeport Buffer empty? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 if (lastCount == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001025 dev_dbg(dev, "%s - TX Buffer Empty\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return;
1027 }
1028
Alan Cox03f0dbf2008-07-22 11:16:34 +01001029 /* Block the thread for a while */
1030 prepare_to_wait(&edge_port->wait_chase, &wait,
1031 TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 schedule_timeout(timeout);
1033 finish_wait(&edge_port->wait_chase, &wait);
1034
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001035 dev_dbg(dev, "%s wait\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
1037 if (lastCount == fifo->count) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001038 /* No activity.. count down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 loop--;
1040 if (loop == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001041 dev_dbg(dev, "%s - TIMEOUT\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 return;
1043 }
1044 } else {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001045 /* Reset timeout value back to seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 loop = 30;
1047 }
1048 }
1049}
1050
1051
1052/*****************************************************************************
1053 * edge_close
1054 * this function is called by the tty driver when a port is closed
1055 *****************************************************************************/
Alan Cox335f8512009-06-11 12:26:29 +01001056static void edge_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
1058 struct edgeport_serial *edge_serial;
1059 struct edgeport_port *edge_port;
1060 int status;
1061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 edge_serial = usb_get_serial_data(port->serial);
1063 edge_port = usb_get_serial_port_data(port);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001064 if (edge_serial == NULL || edge_port == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 return;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001066
1067 /* block until tx is empty */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 block_until_tx_empty(edge_port);
1069
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001070 edge_port->closePending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001072 if ((!edge_serial->is_epic) ||
1073 ((edge_serial->is_epic) &&
1074 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1075 /* flush and chase */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001076 edge_port->chaseResponsePending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001078 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001079 status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
1080 if (status == 0)
1081 /* block until chase finished */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001082 block_until_chase_response(edge_port);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001083 else
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001084 edge_port->chaseResponsePending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 }
1086
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001087 if ((!edge_serial->is_epic) ||
1088 ((edge_serial->is_epic) &&
1089 (edge_serial->epic_descriptor.Supports.IOSPClose))) {
1090 /* close the port */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001091 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CLOSE_PORT\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001092 send_iosp_ext_cmd(edge_port, IOSP_CMD_CLOSE_PORT, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Alan Cox03f0dbf2008-07-22 11:16:34 +01001095 /* port->close = true; */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001096 edge_port->closePending = false;
1097 edge_port->open = false;
1098 edge_port->openPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Mariusz Kozlowski9a25f442006-11-08 15:36:29 +01001100 usb_kill_urb(edge_port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102 if (edge_port->write_urb) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001103 /* if this urb had a transfer buffer already
1104 (old transfer) free it */
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001105 kfree(edge_port->write_urb->transfer_buffer);
1106 usb_free_urb(edge_port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 edge_port->write_urb = NULL;
1108 }
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001109 kfree(edge_port->txfifo.fifo);
1110 edge_port->txfifo.fifo = NULL;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001111}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
1113/*****************************************************************************
1114 * SerialWrite
Alan Cox03f0dbf2008-07-22 11:16:34 +01001115 * this function is called by the tty driver when data should be written
1116 * to the port.
1117 * If successful, we return the number of bytes written, otherwise we
1118 * return a negative error number.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001120static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
1121 const unsigned char *data, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
1123 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1124 struct TxFifo *fifo;
1125 int copySize;
1126 int bytesleft;
1127 int firsthalf;
1128 int secondhalf;
1129 unsigned long flags;
1130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 if (edge_port == NULL)
1132 return -ENODEV;
1133
Alan Cox03f0dbf2008-07-22 11:16:34 +01001134 /* get a pointer to the Tx fifo */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 fifo = &edge_port->txfifo;
1136
1137 spin_lock_irqsave(&edge_port->ep_lock, flags);
1138
Alan Cox03f0dbf2008-07-22 11:16:34 +01001139 /* calculate number of bytes to put in fifo */
1140 copySize = min((unsigned int)count,
1141 (edge_port->txCredits - fifo->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001143 dev_dbg(&port->dev, "%s(%d) of %d byte(s) Fifo room %d -- will copy %d bytes\n",
1144 __func__, port->number, count,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001145 edge_port->txCredits - fifo->count, copySize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Alan Cox03f0dbf2008-07-22 11:16:34 +01001147 /* catch writes of 0 bytes which the tty driver likes to give us,
1148 and when txCredits is empty */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 if (copySize == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001150 dev_dbg(&port->dev, "%s - copySize = Zero\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 goto finish_write;
1152 }
1153
Alan Cox03f0dbf2008-07-22 11:16:34 +01001154 /* queue the data
1155 * since we can never overflow the buffer we do not have to check for a
1156 * full condition
1157 *
1158 * the copy is done is two parts -- first fill to the end of the buffer
1159 * then copy the reset from the start of the buffer
1160 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 bytesleft = fifo->size - fifo->head;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001162 firsthalf = min(bytesleft, copySize);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001163 dev_dbg(&port->dev, "%s - copy %d bytes of %d into fifo \n", __func__,
1164 firsthalf, bytesleft);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
1166 /* now copy our data */
1167 memcpy(&fifo->fifo[fifo->head], data, firsthalf);
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001168 usb_serial_debug_data(&port->dev, __func__, firsthalf, &fifo->fifo[fifo->head]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Alan Cox03f0dbf2008-07-22 11:16:34 +01001170 /* update the index and size */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 fifo->head += firsthalf;
1172 fifo->count += firsthalf;
1173
Alan Cox03f0dbf2008-07-22 11:16:34 +01001174 /* wrap the index */
1175 if (fifo->head == fifo->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 fifo->head = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178 secondhalf = copySize-firsthalf;
1179
1180 if (secondhalf) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001181 dev_dbg(&port->dev, "%s - copy rest of data %d\n", __func__, secondhalf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 memcpy(&fifo->fifo[fifo->head], &data[firsthalf], secondhalf);
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001183 usb_serial_debug_data(&port->dev, __func__, secondhalf, &fifo->fifo[fifo->head]);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001184 /* update the index and size */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 fifo->count += secondhalf;
1186 fifo->head += secondhalf;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001187 /* No need to check for wrap since we can not get to end of
1188 * the fifo in this part
1189 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 }
1191
1192finish_write:
1193 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1194
Alan Cox03f0dbf2008-07-22 11:16:34 +01001195 send_more_port_data((struct edgeport_serial *)
1196 usb_get_serial_data(port->serial), edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001198 dev_dbg(&port->dev, "%s wrote %d byte(s) TxCredits %d, Fifo %d\n",
1199 __func__, copySize, edge_port->txCredits, fifo->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Alan Cox03f0dbf2008-07-22 11:16:34 +01001201 return copySize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202}
1203
1204
1205/************************************************************************
1206 *
1207 * send_more_port_data()
1208 *
1209 * This routine attempts to write additional UART transmit data
1210 * to a port over the USB bulk pipe. It is called (1) when new
1211 * data has been written to a port's TxBuffer from higher layers
1212 * (2) when the peripheral sends us additional TxCredits indicating
1213 * that it can accept more Tx data for a given port; and (3) when
1214 * a bulk write completes successfully and we want to see if we
1215 * can transmit more.
1216 *
1217 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001218static void send_more_port_data(struct edgeport_serial *edge_serial,
1219 struct edgeport_port *edge_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
1221 struct TxFifo *fifo = &edge_port->txfifo;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001222 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 struct urb *urb;
1224 unsigned char *buffer;
1225 int status;
1226 int count;
1227 int bytesleft;
1228 int firsthalf;
1229 int secondhalf;
1230 unsigned long flags;
1231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 spin_lock_irqsave(&edge_port->ep_lock, flags);
1233
1234 if (edge_port->write_in_progress ||
1235 !edge_port->open ||
1236 (fifo->count == 0)) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001237 dev_dbg(dev, "%s(%d) EXIT - fifo %d, PendingWrite = %d\n",
1238 __func__, edge_port->port->number,
1239 fifo->count, edge_port->write_in_progress);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 goto exit_send;
1241 }
1242
Alan Cox03f0dbf2008-07-22 11:16:34 +01001243 /* since the amount of data in the fifo will always fit into the
1244 * edgeport buffer we do not need to check the write length
1245 *
1246 * Do we have enough credits for this port to make it worthwhile
1247 * to bother queueing a write. If it's too small, say a few bytes,
1248 * it's better to wait for more credits so we can do a larger write.
1249 */
1250 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 -07001251 dev_dbg(dev, "%s(%d) Not enough credit - fifo %d TxCredit %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01001252 __func__, edge_port->port->number, fifo->count,
1253 edge_port->txCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 goto exit_send;
1255 }
1256
Alan Cox03f0dbf2008-07-22 11:16:34 +01001257 /* lock this write */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001258 edge_port->write_in_progress = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Alan Cox03f0dbf2008-07-22 11:16:34 +01001260 /* get a pointer to the write_urb */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 urb = edge_port->write_urb;
1262
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001263 /* make sure transfer buffer is freed */
1264 kfree(urb->transfer_buffer);
1265 urb->transfer_buffer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Alan Cox03f0dbf2008-07-22 11:16:34 +01001267 /* build the data header for the buffer and port that we are about
1268 to send out */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 count = fifo->count;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001270 buffer = kmalloc(count+2, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 if (buffer == NULL) {
Johan Hovold22a416c2012-02-10 13:20:51 +01001272 dev_err_console(edge_port->port,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001273 "%s - no more kernel memory...\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001274 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 goto exit_send;
1276 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01001277 buffer[0] = IOSP_BUILD_DATA_HDR1(edge_port->port->number
1278 - edge_port->port->serial->minor, count);
1279 buffer[1] = IOSP_BUILD_DATA_HDR2(edge_port->port->number
1280 - edge_port->port->serial->minor, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282 /* now copy our data */
1283 bytesleft = fifo->size - fifo->tail;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001284 firsthalf = min(bytesleft, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 memcpy(&buffer[2], &fifo->fifo[fifo->tail], firsthalf);
1286 fifo->tail += firsthalf;
1287 fifo->count -= firsthalf;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001288 if (fifo->tail == fifo->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 fifo->tail = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 secondhalf = count-firsthalf;
1292 if (secondhalf) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001293 memcpy(&buffer[2+firsthalf], &fifo->fifo[fifo->tail],
1294 secondhalf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 fifo->tail += secondhalf;
1296 fifo->count -= secondhalf;
1297 }
1298
1299 if (count)
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001300 usb_serial_debug_data(&edge_port->port->dev, __func__, count, &buffer[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 /* fill up the urb with all of our data and submit it */
Alan Cox03f0dbf2008-07-22 11:16:34 +01001303 usb_fill_bulk_urb(urb, edge_serial->serial->dev,
1304 usb_sndbulkpipe(edge_serial->serial->dev,
1305 edge_serial->bulk_out_endpoint),
1306 buffer, count+2,
1307 edge_bulk_out_data_callback, edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309 /* decrement the number of credits we have by the number we just sent */
1310 edge_port->txCredits -= count;
Johan Hovoldd36a7712013-03-21 12:37:07 +01001311 edge_port->port->icount.tx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 status = usb_submit_urb(urb, GFP_ATOMIC);
1314 if (status) {
1315 /* something went wrong */
Johan Hovold22a416c2012-02-10 13:20:51 +01001316 dev_err_console(edge_port->port,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001317 "%s - usb_submit_urb(write bulk) failed, status = %d, data lost\n",
1318 __func__, status);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001319 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321 /* revert the credits as something bad happened. */
1322 edge_port->txCredits += count;
Johan Hovoldd36a7712013-03-21 12:37:07 +01001323 edge_port->port->icount.tx -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 }
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001325 dev_dbg(dev, "%s wrote %d byte(s) TxCredit %d, Fifo %d\n",
1326 __func__, count, edge_port->txCredits, fifo->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328exit_send:
1329 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1330}
1331
1332
1333/*****************************************************************************
1334 * edge_write_room
Alan Cox03f0dbf2008-07-22 11:16:34 +01001335 * this function is called by the tty driver when it wants to know how
1336 * many bytes of data we can accept for a specific port. If successful,
1337 * we return the amount of room that we have for this port (the txCredits)
1338 * otherwise we return a negative error number.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001340static int edge_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
Alan Cox95da3102008-07-22 11:09:07 +01001342 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1344 int room;
1345 unsigned long flags;
1346
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 if (edge_port == NULL)
Alan Coxd76f2f442008-07-22 11:16:42 +01001348 return 0;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001349 if (edge_port->closePending)
Alan Coxd76f2f442008-07-22 11:16:42 +01001350 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001353 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Alan Coxd76f2f442008-07-22 11:16:42 +01001354 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 }
1356
Alan Cox03f0dbf2008-07-22 11:16:34 +01001357 /* total of both buffers is still txCredit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 spin_lock_irqsave(&edge_port->ep_lock, flags);
1359 room = edge_port->txCredits - edge_port->txfifo.count;
1360 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1361
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001362 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 return room;
1364}
1365
1366
1367/*****************************************************************************
1368 * edge_chars_in_buffer
Alan Cox03f0dbf2008-07-22 11:16:34 +01001369 * this function is called by the tty driver when it wants to know how
1370 * many bytes of data we currently have outstanding in the port (data that
1371 * has been written, but hasn't made it out the port yet)
1372 * If successful, we return the number of bytes left to be written in the
1373 * system,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 * Otherwise we return a negative error number.
1375 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001376static int edge_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
Alan Cox95da3102008-07-22 11:09:07 +01001378 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1380 int num_chars;
1381 unsigned long flags;
1382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 if (edge_port == NULL)
Alan Coxd76f2f442008-07-22 11:16:42 +01001384 return 0;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001385 if (edge_port->closePending)
Alan Coxd76f2f442008-07-22 11:16:42 +01001386 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
1388 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001389 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Alan Coxd76f2f442008-07-22 11:16:42 +01001390 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 }
1392
1393 spin_lock_irqsave(&edge_port->ep_lock, flags);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001394 num_chars = edge_port->maxTxCredits - edge_port->txCredits +
1395 edge_port->txfifo.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1397 if (num_chars) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001398 dev_dbg(&port->dev, "%s(port %d) - returns %d\n", __func__,
1399 port->number, num_chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 }
1401
1402 return num_chars;
1403}
1404
1405
1406/*****************************************************************************
1407 * SerialThrottle
1408 * this function is called by the tty driver when it wants to stop the data
1409 * being read from the port.
1410 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001411static void edge_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412{
Alan Cox95da3102008-07-22 11:09:07 +01001413 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 int status;
1416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 if (edge_port == NULL)
1418 return;
1419
1420 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001421 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 return;
1423 }
1424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 /* if we are implementing XON/XOFF, send the stop character */
1426 if (I_IXOFF(tty)) {
1427 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001428 status = edge_write(tty, port, &stop_char, 1);
1429 if (status <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 }
1432
1433 /* if we are implementing RTS/CTS, toggle that line */
Alan Coxadc8d742012-07-14 15:31:47 +01001434 if (tty->termios.c_cflag & CRTSCTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 edge_port->shadowMCR &= ~MCR_RTS;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001436 status = send_cmd_write_uart_register(edge_port, MCR,
1437 edge_port->shadowMCR);
1438 if (status != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441}
1442
1443
1444/*****************************************************************************
1445 * edge_unthrottle
Alan Cox03f0dbf2008-07-22 11:16:34 +01001446 * this function is called by the tty driver when it wants to resume the
1447 * data being read from the port (called after SerialThrottle is called)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001449static void edge_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
Alan Cox95da3102008-07-22 11:09:07 +01001451 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 int status;
1454
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 if (edge_port == NULL)
1456 return;
1457
1458 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001459 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 return;
1461 }
1462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 /* if we are implementing XON/XOFF, send the start character */
1464 if (I_IXOFF(tty)) {
1465 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001466 status = edge_write(tty, port, &start_char, 1);
1467 if (status <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 /* if we are implementing RTS/CTS, toggle that line */
Alan Coxadc8d742012-07-14 15:31:47 +01001471 if (tty->termios.c_cflag & CRTSCTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 edge_port->shadowMCR |= MCR_RTS;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001473 send_cmd_write_uart_register(edge_port, MCR,
1474 edge_port->shadowMCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476}
1477
1478
1479/*****************************************************************************
1480 * SerialSetTermios
Alan Cox03f0dbf2008-07-22 11:16:34 +01001481 * this function is called by the tty driver when it wants to change
1482 * the termios structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001484static void edge_set_termios(struct tty_struct *tty,
1485 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486{
1487 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 unsigned int cflag;
1489
Alan Coxadc8d742012-07-14 15:31:47 +01001490 cflag = tty->termios.c_cflag;
Linus Torvaldsd9a80742012-10-01 13:23:01 -07001491 dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__, tty->termios.c_cflag, tty->termios.c_iflag);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001492 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 -07001493
1494 if (edge_port == NULL)
1495 return;
1496
1497 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001498 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 return;
1500 }
1501
1502 /* change the port settings to the new ones specified */
Alan Cox95da3102008-07-22 11:09:07 +01001503 change_port_settings(tty, edge_port, old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504}
1505
1506
1507/*****************************************************************************
1508 * get_lsr_info - get line status register info
1509 *
1510 * Purpose: Let user call ioctl() to get info when the UART physically
1511 * is emptied. On bus types like RS485, the transmitter must
1512 * release the bus after transmitting. This must be done when
1513 * the transmit shift register is empty, not be done when the
1514 * transmit holding register is empty. This functionality
Alan Cox03f0dbf2008-07-22 11:16:34 +01001515 * allows an RS485 driver to be written in user space.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001517static int get_lsr_info(struct edgeport_port *edge_port,
1518 unsigned int __user *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
1520 unsigned int result = 0;
1521 unsigned long flags;
1522
1523 spin_lock_irqsave(&edge_port->ep_lock, flags);
1524 if (edge_port->maxTxCredits == edge_port->txCredits &&
1525 edge_port->txfifo.count == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001526 dev_dbg(&edge_port->port->dev, "%s -- Empty\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 result = TIOCSER_TEMT;
1528 }
1529 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1530
1531 if (copy_to_user(value, &result, sizeof(int)))
1532 return -EFAULT;
1533 return 0;
1534}
1535
Alan Cox20b9d172011-02-14 16:26:50 +00001536static int edge_tiocmset(struct tty_struct *tty,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001537 unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538{
Alan Cox95da3102008-07-22 11:09:07 +01001539 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1541 unsigned int mcr;
1542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 mcr = edge_port->shadowMCR;
1544 if (set & TIOCM_RTS)
1545 mcr |= MCR_RTS;
1546 if (set & TIOCM_DTR)
1547 mcr |= MCR_DTR;
1548 if (set & TIOCM_LOOP)
1549 mcr |= MCR_LOOPBACK;
1550
1551 if (clear & TIOCM_RTS)
1552 mcr &= ~MCR_RTS;
1553 if (clear & TIOCM_DTR)
1554 mcr &= ~MCR_DTR;
1555 if (clear & TIOCM_LOOP)
1556 mcr &= ~MCR_LOOPBACK;
1557
1558 edge_port->shadowMCR = mcr;
1559
1560 send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1561
1562 return 0;
1563}
1564
Alan Cox60b33c12011-02-14 16:26:14 +00001565static int edge_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566{
Alan Cox95da3102008-07-22 11:09:07 +01001567 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1569 unsigned int result = 0;
1570 unsigned int msr;
1571 unsigned int mcr;
1572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 msr = edge_port->shadowMSR;
1574 mcr = edge_port->shadowMCR;
1575 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
1576 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
1577 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
1578 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
1579 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
1580 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
1581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 return result;
1583}
1584
Alan Cox03f0dbf2008-07-22 11:16:34 +01001585static int get_serial_info(struct edgeport_port *edge_port,
1586 struct serial_struct __user *retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587{
1588 struct serial_struct tmp;
1589
1590 if (!retinfo)
1591 return -EFAULT;
1592
1593 memset(&tmp, 0, sizeof(tmp));
1594
1595 tmp.type = PORT_16550A;
1596 tmp.line = edge_port->port->serial->minor;
1597 tmp.port = edge_port->port->number;
1598 tmp.irq = 0;
1599 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1600 tmp.xmit_fifo_size = edge_port->maxTxCredits;
1601 tmp.baud_base = 9600;
1602 tmp.close_delay = 5*HZ;
1603 tmp.closing_wait = 30*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
1605 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1606 return -EFAULT;
1607 return 0;
1608}
1609
1610
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611/*****************************************************************************
1612 * SerialIoctl
1613 * this function handles any ioctl calls to the driver
1614 *****************************************************************************/
Alan Cox00a0d0d2011-02-14 16:27:06 +00001615static int edge_ioctl(struct tty_struct *tty,
Alan Cox95da3102008-07-22 11:09:07 +01001616 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617{
Alan Cox95da3102008-07-22 11:09:07 +01001618 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 DEFINE_WAIT(wait);
1620 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1621 struct async_icount cnow;
1622 struct async_icount cprev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001624 dev_dbg(&port->dev, "%s - port %d, cmd = 0x%x\n", __func__, port->number, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
1626 switch (cmd) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001627 case TIOCSERGETLSR:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001628 dev_dbg(&port->dev, "%s (%d) TIOCSERGETLSR\n", __func__, port->number);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001629 return get_lsr_info(edge_port, (unsigned int __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Alan Cox03f0dbf2008-07-22 11:16:34 +01001631 case TIOCGSERIAL:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001632 dev_dbg(&port->dev, "%s (%d) TIOCGSERIAL\n", __func__, port->number);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001633 return get_serial_info(edge_port, (struct serial_struct __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Alan Cox03f0dbf2008-07-22 11:16:34 +01001635 case TIOCMIWAIT:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001636 dev_dbg(&port->dev, "%s (%d) TIOCMIWAIT\n", __func__, port->number);
Johan Hovoldd36a7712013-03-21 12:37:07 +01001637 cprev = port->icount;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001638 while (1) {
Johan Hovold33357622013-03-19 09:21:16 +01001639 prepare_to_wait(&port->delta_msr_wait,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001640 &wait, TASK_INTERRUPTIBLE);
1641 schedule();
Johan Hovold33357622013-03-19 09:21:16 +01001642 finish_wait(&port->delta_msr_wait, &wait);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001643 /* see if a signal did it */
1644 if (signal_pending(current))
1645 return -ERESTARTSYS;
Johan Hovold33357622013-03-19 09:21:16 +01001646
1647 if (port->serial->disconnected)
1648 return -EIO;
1649
Johan Hovoldd36a7712013-03-21 12:37:07 +01001650 cnow = port->icount;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001651 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1652 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1653 return -EIO; /* no change => error */
1654 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1655 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1656 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1657 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1658 return 0;
1659 }
1660 cprev = cnow;
1661 }
1662 /* NOTREACHED */
1663 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 return -ENOIOCTLCMD;
1667}
1668
1669
1670/*****************************************************************************
1671 * SerialBreak
1672 * this function sends a break to the port
1673 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001674static void edge_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675{
Alan Cox95da3102008-07-22 11:09:07 +01001676 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001678 struct edgeport_serial *edge_serial = usb_get_serial_data(port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 int status;
1680
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001681 if ((!edge_serial->is_epic) ||
1682 ((edge_serial->is_epic) &&
1683 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1684 /* flush and chase */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001685 edge_port->chaseResponsePending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001687 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001688 status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001689 if (status == 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001690 /* block until chase finished */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001691 block_until_chase_response(edge_port);
1692 } else {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001693 edge_port->chaseResponsePending = false;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 }
1696
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001697 if ((!edge_serial->is_epic) ||
1698 ((edge_serial->is_epic) &&
1699 (edge_serial->epic_descriptor.Supports.IOSPSetClrBreak))) {
1700 if (break_state == -1) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001701 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_SET_BREAK\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001702 status = send_iosp_ext_cmd(edge_port,
1703 IOSP_CMD_SET_BREAK, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001704 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001705 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CLEAR_BREAK\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001706 status = send_iosp_ext_cmd(edge_port,
1707 IOSP_CMD_CLEAR_BREAK, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001708 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01001709 if (status)
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001710 dev_dbg(&port->dev, "%s - error sending break set/clear command.\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01001711 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713}
1714
1715
1716/*****************************************************************************
1717 * process_rcvd_data
1718 * this function handles the data received on the bulk in pipe.
1719 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001720static void process_rcvd_data(struct edgeport_serial *edge_serial,
1721 unsigned char *buffer, __u16 bufferLength)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001723 struct device *dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 struct usb_serial_port *port;
1725 struct edgeport_port *edge_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 __u16 lastBufferLength;
1727 __u16 rxLen;
1728
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 lastBufferLength = bufferLength + 1;
1730
1731 while (bufferLength > 0) {
1732 /* failsafe incase we get a message that we don't understand */
1733 if (lastBufferLength == bufferLength) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001734 dev_dbg(dev, "%s - stuck in loop, exiting it.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 break;
1736 }
1737 lastBufferLength = bufferLength;
1738
1739 switch (edge_serial->rxState) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001740 case EXPECT_HDR1:
1741 edge_serial->rxHeader1 = *buffer;
1742 ++buffer;
1743 --bufferLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Alan Cox03f0dbf2008-07-22 11:16:34 +01001745 if (bufferLength == 0) {
1746 edge_serial->rxState = EXPECT_HDR2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 break;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001748 }
1749 /* otherwise, drop on through */
1750 case EXPECT_HDR2:
1751 edge_serial->rxHeader2 = *buffer;
1752 ++buffer;
1753 --bufferLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001755 dev_dbg(dev, "%s - Hdr1=%02X Hdr2=%02X\n", __func__,
1756 edge_serial->rxHeader1, edge_serial->rxHeader2);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001757 /* Process depending on whether this header is
1758 * data or status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
Alan Cox03f0dbf2008-07-22 11:16:34 +01001760 if (IS_CMD_STAT_HDR(edge_serial->rxHeader1)) {
1761 /* Decode this status header and go to
1762 * EXPECT_HDR1 (if we can process the status
1763 * with only 2 bytes), or go to EXPECT_HDR3 to
1764 * get the third byte. */
1765 edge_serial->rxPort =
1766 IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1767 edge_serial->rxStatusCode =
1768 IOSP_GET_STATUS_CODE(
1769 edge_serial->rxHeader1);
1770
1771 if (!IOSP_STATUS_IS_2BYTE(
1772 edge_serial->rxStatusCode)) {
1773 /* This status needs additional bytes.
1774 * Save what we have and then wait for
1775 * more data.
1776 */
1777 edge_serial->rxStatusParam
1778 = edge_serial->rxHeader2;
1779 edge_serial->rxState = EXPECT_HDR3;
1780 break;
1781 }
1782 /* We have all the header bytes, process the
1783 status now */
1784 process_rcvd_status(edge_serial,
1785 edge_serial->rxHeader2, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 edge_serial->rxState = EXPECT_HDR1;
1787 break;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001788 } else {
1789 edge_serial->rxPort =
1790 IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1791 edge_serial->rxBytesRemaining =
1792 IOSP_GET_HDR_DATA_LEN(
1793 edge_serial->rxHeader1,
1794 edge_serial->rxHeader2);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001795 dev_dbg(dev, "%s - Data for Port %u Len %u\n",
1796 __func__,
1797 edge_serial->rxPort,
1798 edge_serial->rxBytesRemaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
Alan Cox03f0dbf2008-07-22 11:16:34 +01001800 /* ASSERT(DevExt->RxPort < DevExt->NumPorts);
1801 * ASSERT(DevExt->RxBytesRemaining <
1802 * IOSP_MAX_DATA_LENGTH);
1803 */
1804
1805 if (bufferLength == 0) {
1806 edge_serial->rxState = EXPECT_DATA;
1807 break;
1808 }
1809 /* Else, drop through */
1810 }
1811 case EXPECT_DATA: /* Expect data */
1812 if (bufferLength < edge_serial->rxBytesRemaining) {
1813 rxLen = bufferLength;
1814 /* Expect data to start next buffer */
1815 edge_serial->rxState = EXPECT_DATA;
1816 } else {
1817 /* BufLen >= RxBytesRemaining */
1818 rxLen = edge_serial->rxBytesRemaining;
1819 /* Start another header next time */
1820 edge_serial->rxState = EXPECT_HDR1;
1821 }
1822
1823 bufferLength -= rxLen;
1824 edge_serial->rxBytesRemaining -= rxLen;
1825
1826 /* spit this data back into the tty driver if this
1827 port is open */
1828 if (rxLen) {
1829 port = edge_serial->serial->port[
1830 edge_serial->rxPort];
1831 edge_port = usb_get_serial_port_data(port);
1832 if (edge_port->open) {
Jiri Slaby2e124b42013-01-03 15:53:06 +01001833 dev_dbg(dev, "%s - Sending %d bytes to TTY for port %d\n",
1834 __func__, rxLen,
1835 edge_serial->rxPort);
1836 edge_tty_recv(edge_port->port, buffer,
1837 rxLen);
Johan Hovoldd36a7712013-03-21 12:37:07 +01001838 edge_port->port->icount.rx += rxLen;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001839 }
1840 buffer += rxLen;
1841 }
1842 break;
1843
1844 case EXPECT_HDR3: /* Expect 3rd byte of status header */
1845 edge_serial->rxHeader3 = *buffer;
1846 ++buffer;
1847 --bufferLength;
1848
1849 /* We have all the header bytes, process the
1850 status now */
1851 process_rcvd_status(edge_serial,
1852 edge_serial->rxStatusParam,
1853 edge_serial->rxHeader3);
1854 edge_serial->rxState = EXPECT_HDR1;
1855 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 }
1857 }
1858}
1859
1860
1861/*****************************************************************************
1862 * process_rcvd_status
Alan Cox03f0dbf2008-07-22 11:16:34 +01001863 * this function handles the any status messages received on the
1864 * bulk in pipe.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001866static void process_rcvd_status(struct edgeport_serial *edge_serial,
1867 __u8 byte2, __u8 byte3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868{
1869 struct usb_serial_port *port;
1870 struct edgeport_port *edge_port;
Alan Cox4a90f092008-10-13 10:39:46 +01001871 struct tty_struct *tty;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001872 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 __u8 code = edge_serial->rxStatusCode;
1874
1875 /* switch the port pointer to the one being currently talked about */
1876 port = edge_serial->serial->port[edge_serial->rxPort];
1877 edge_port = usb_get_serial_port_data(port);
1878 if (edge_port == NULL) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001879 dev_err(&edge_serial->serial->dev->dev,
1880 "%s - edge_port == NULL for port %d\n",
1881 __func__, edge_serial->rxPort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 return;
1883 }
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001884 dev = &port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
1886 if (code == IOSP_EXT_STATUS) {
1887 switch (byte2) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001888 case IOSP_EXT_STATUS_CHASE_RSP:
1889 /* we want to do EXT status regardless of port
1890 * open/closed */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001891 dev_dbg(dev, "%s - Port %u EXT CHASE_RSP Data = %02x\n",
1892 __func__, edge_serial->rxPort, byte3);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001893 /* Currently, the only EXT_STATUS is Chase, so process
1894 * here instead of one more call to one more subroutine
1895 * If/when more EXT_STATUS, there'll be more work to do
1896 * Also, we currently clear flag and close the port
1897 * regardless of content of above's Byte3.
1898 * We could choose to do something else when Byte3 says
1899 * Timeout on Chase from Edgeport, like wait longer in
1900 * block_until_chase_response, but for now we don't.
1901 */
1902 edge_port->chaseResponsePending = false;
1903 wake_up(&edge_port->wait_chase);
1904 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Alan Cox03f0dbf2008-07-22 11:16:34 +01001906 case IOSP_EXT_STATUS_RX_CHECK_RSP:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001907 dev_dbg(dev, "%s ========== Port %u CHECK_RSP Sequence = %02x =============\n",
1908 __func__, edge_serial->rxPort, byte3);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001909 /* Port->RxCheckRsp = true; */
1910 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 }
1912 }
1913
1914 if (code == IOSP_STATUS_OPEN_RSP) {
1915 edge_port->txCredits = GET_TX_BUFFER_SIZE(byte3);
1916 edge_port->maxTxCredits = edge_port->txCredits;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001917 dev_dbg(dev, "%s - Port %u Open Response Initial MSR = %02x TxBufferSize = %d\n",
1918 __func__, edge_serial->rxPort, byte2, edge_port->txCredits);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001919 handle_new_msr(edge_port, byte2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Alan Cox03f0dbf2008-07-22 11:16:34 +01001921 /* send the current line settings to the port so we are
1922 in sync with any further termios calls */
Alan Cox4a90f092008-10-13 10:39:46 +01001923 tty = tty_port_tty_get(&edge_port->port->port);
1924 if (tty) {
1925 change_port_settings(tty,
Alan Coxadc8d742012-07-14 15:31:47 +01001926 edge_port, &tty->termios);
Alan Cox4a90f092008-10-13 10:39:46 +01001927 tty_kref_put(tty);
1928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
1930 /* we have completed the open */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001931 edge_port->openPending = false;
1932 edge_port->open = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 wake_up(&edge_port->wait_open);
1934 return;
1935 }
1936
Alan Cox03f0dbf2008-07-22 11:16:34 +01001937 /* If port is closed, silently discard all rcvd status. We can
1938 * have cases where buffered status is received AFTER the close
1939 * port command is sent to the Edgeport.
1940 */
1941 if (!edge_port->open || edge_port->closePending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
1944 switch (code) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001945 /* Not currently sent by Edgeport */
1946 case IOSP_STATUS_LSR:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001947 dev_dbg(dev, "%s - Port %u LSR Status = %02x\n",
1948 __func__, edge_serial->rxPort, byte2);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001949 handle_new_lsr(edge_port, false, byte2, 0);
1950 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
Alan Cox03f0dbf2008-07-22 11:16:34 +01001952 case IOSP_STATUS_LSR_DATA:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001953 dev_dbg(dev, "%s - Port %u LSR Status = %02x, Data = %02x\n",
1954 __func__, edge_serial->rxPort, byte2, byte3);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001955 /* byte2 is LSR Register */
1956 /* byte3 is broken data byte */
1957 handle_new_lsr(edge_port, true, byte2, byte3);
1958 break;
1959 /*
1960 * case IOSP_EXT_4_STATUS:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001961 * dev_dbg(dev, "%s - Port %u LSR Status = %02x Data = %02x\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01001962 * __func__, edge_serial->rxPort, byte2, byte3);
1963 * break;
1964 */
1965 case IOSP_STATUS_MSR:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001966 dev_dbg(dev, "%s - Port %u MSR Status = %02x\n",
1967 __func__, edge_serial->rxPort, byte2);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001968 /*
1969 * Process this new modem status and generate appropriate
1970 * events, etc, based on the new status. This routine
1971 * also saves the MSR in Port->ShadowMsr.
1972 */
1973 handle_new_msr(edge_port, byte2);
1974 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Alan Cox03f0dbf2008-07-22 11:16:34 +01001976 default:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001977 dev_dbg(dev, "%s - Unrecognized IOSP status code %u\n", __func__, code);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001978 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980}
1981
1982
1983/*****************************************************************************
1984 * edge_tty_recv
1985 * this function passes data on to the tty flip buffer
1986 *****************************************************************************/
Jiri Slaby2e124b42013-01-03 15:53:06 +01001987static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
1988 int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989{
1990 int cnt;
1991
Jiri Slaby05c7cd32013-01-03 15:53:04 +01001992 cnt = tty_insert_flip_string(&port->port, data, length);
Alan Coxa108bfc2010-02-18 16:44:01 +00001993 if (cnt < length) {
Jiri Slaby05c7cd32013-01-03 15:53:04 +01001994 dev_err(&port->dev, "%s - dropping data, %d bytes lost\n",
Alan Coxa108bfc2010-02-18 16:44:01 +00001995 __func__, length - cnt);
1996 }
1997 data += cnt;
1998 length -= cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
Jiri Slaby2e124b42013-01-03 15:53:06 +01002000 tty_flip_buffer_push(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001}
2002
2003
2004/*****************************************************************************
2005 * handle_new_msr
2006 * this function handles any change to the msr register for a port.
2007 *****************************************************************************/
2008static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr)
2009{
2010 struct async_icount *icount;
2011
Alan Cox03f0dbf2008-07-22 11:16:34 +01002012 if (newMsr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
2013 EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
Johan Hovoldd36a7712013-03-21 12:37:07 +01002014 icount = &edge_port->port->icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
2016 /* update input line counters */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002017 if (newMsr & EDGEPORT_MSR_DELTA_CTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 icount->cts++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002019 if (newMsr & EDGEPORT_MSR_DELTA_DSR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 icount->dsr++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002021 if (newMsr & EDGEPORT_MSR_DELTA_CD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 icount->dcd++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002023 if (newMsr & EDGEPORT_MSR_DELTA_RI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 icount->rng++;
Johan Hovold33357622013-03-19 09:21:16 +01002025 wake_up_interruptible(&edge_port->port->delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 }
2027
2028 /* Save the new modem status */
2029 edge_port->shadowMSR = newMsr & 0xf0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030}
2031
2032
2033/*****************************************************************************
2034 * handle_new_lsr
2035 * this function handles any change to the lsr register for a port.
2036 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002037static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
2038 __u8 lsr, __u8 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002040 __u8 newLsr = (__u8) (lsr & (__u8)
2041 (LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
2042 struct async_icount *icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 edge_port->shadowLSR = lsr;
2045
2046 if (newLsr & LSR_BREAK) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002047 /*
2048 * Parity and Framing errors only count if they
2049 * occur exclusive of a break being
2050 * received.
2051 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 newLsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
2053 }
2054
2055 /* Place LSR data byte into Rx buffer */
Jiri Slaby2e124b42013-01-03 15:53:06 +01002056 if (lsrData)
2057 edge_tty_recv(edge_port->port, &data, 1);
2058
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 /* update input line counters */
Johan Hovoldd36a7712013-03-21 12:37:07 +01002060 icount = &edge_port->port->icount;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002061 if (newLsr & LSR_BREAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 icount->brk++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002063 if (newLsr & LSR_OVER_ERR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 icount->overrun++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002065 if (newLsr & LSR_PAR_ERR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 icount->parity++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002067 if (newLsr & LSR_FRM_ERR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 icount->frame++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069}
2070
2071
2072/****************************************************************************
2073 * sram_write
Alan Cox03f0dbf2008-07-22 11:16:34 +01002074 * writes a number of bytes to the Edgeport device's sram starting at the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 * given address.
2076 * If successful returns the number of bytes written, otherwise it returns
2077 * a negative error number of the problem.
2078 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002079static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
2080 __u16 length, const __u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081{
2082 int result;
2083 __u16 current_length;
2084 unsigned char *transfer_buffer;
2085
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002086 dev_dbg(&serial->dev->dev, "%s - %x, %x, %d\n", __func__, extAddr, addr, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Alan Cox03f0dbf2008-07-22 11:16:34 +01002088 transfer_buffer = kmalloc(64, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 if (!transfer_buffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002090 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
2091 __func__, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 return -ENOMEM;
2093 }
2094
2095 /* need to split these writes up into 64 byte chunks */
2096 result = 0;
2097 while (length > 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002098 if (length > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 current_length = 64;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002100 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 current_length = length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002102
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002103/* dev_dbg(&serial->dev->dev, "%s - writing %x, %x, %d\n", __func__, extAddr, addr, current_length); */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002104 memcpy(transfer_buffer, data, current_length);
2105 result = usb_control_msg(serial->dev,
2106 usb_sndctrlpipe(serial->dev, 0),
2107 USB_REQUEST_ION_WRITE_RAM,
2108 0x40, addr, extAddr, transfer_buffer,
2109 current_length, 300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 if (result < 0)
2111 break;
2112 length -= current_length;
2113 addr += current_length;
2114 data += current_length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
Alan Cox03f0dbf2008-07-22 11:16:34 +01002117 kfree(transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 return result;
2119}
2120
2121
2122/****************************************************************************
2123 * rom_write
2124 * writes a number of bytes to the Edgeport device's ROM starting at the
2125 * given address.
2126 * If successful returns the number of bytes written, otherwise it returns
2127 * a negative error number of the problem.
2128 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002129static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
2130 __u16 length, const __u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131{
2132 int result;
2133 __u16 current_length;
2134 unsigned char *transfer_buffer;
2135
Alan Cox03f0dbf2008-07-22 11:16:34 +01002136 transfer_buffer = kmalloc(64, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 if (!transfer_buffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002138 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
2139 __func__, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 return -ENOMEM;
2141 }
2142
2143 /* need to split these writes up into 64 byte chunks */
2144 result = 0;
2145 while (length > 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002146 if (length > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 current_length = 64;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002148 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 current_length = length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002150 memcpy(transfer_buffer, data, current_length);
2151 result = usb_control_msg(serial->dev,
2152 usb_sndctrlpipe(serial->dev, 0),
2153 USB_REQUEST_ION_WRITE_ROM, 0x40,
2154 addr, extAddr,
2155 transfer_buffer, current_length, 300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 if (result < 0)
2157 break;
2158 length -= current_length;
2159 addr += current_length;
2160 data += current_length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
Alan Cox03f0dbf2008-07-22 11:16:34 +01002163 kfree(transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 return result;
2165}
2166
2167
2168/****************************************************************************
2169 * rom_read
2170 * reads a number of bytes from the Edgeport device starting at the given
2171 * address.
2172 * If successful returns the number of bytes read, otherwise it returns
2173 * a negative error number of the problem.
2174 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002175static int rom_read(struct usb_serial *serial, __u16 extAddr,
2176 __u16 addr, __u16 length, __u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177{
2178 int result;
2179 __u16 current_length;
2180 unsigned char *transfer_buffer;
2181
Alan Cox03f0dbf2008-07-22 11:16:34 +01002182 transfer_buffer = kmalloc(64, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 if (!transfer_buffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002184 dev_err(&serial->dev->dev,
2185 "%s - kmalloc(%d) failed.\n", __func__, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 return -ENOMEM;
2187 }
2188
2189 /* need to split these reads up into 64 byte chunks */
2190 result = 0;
2191 while (length > 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002192 if (length > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 current_length = 64;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002194 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 current_length = length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002196 result = usb_control_msg(serial->dev,
2197 usb_rcvctrlpipe(serial->dev, 0),
2198 USB_REQUEST_ION_READ_ROM,
2199 0xC0, addr, extAddr, transfer_buffer,
2200 current_length, 300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 if (result < 0)
2202 break;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002203 memcpy(data, transfer_buffer, current_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 length -= current_length;
2205 addr += current_length;
2206 data += current_length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
Alan Cox03f0dbf2008-07-22 11:16:34 +01002209 kfree(transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 return result;
2211}
2212
2213
2214/****************************************************************************
2215 * send_iosp_ext_cmd
2216 * Is used to send a IOSP message to the Edgeport device
2217 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002218static int send_iosp_ext_cmd(struct edgeport_port *edge_port,
2219 __u8 command, __u8 param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220{
2221 unsigned char *buffer;
2222 unsigned char *currentCommand;
2223 int length = 0;
2224 int status = 0;
2225
Alan Cox03f0dbf2008-07-22 11:16:34 +01002226 buffer = kmalloc(10, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 if (!buffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002228 dev_err(&edge_port->port->dev,
2229 "%s - kmalloc(%d) failed.\n", __func__, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 return -ENOMEM;
2231 }
2232
2233 currentCommand = buffer;
2234
Alan Cox03f0dbf2008-07-22 11:16:34 +01002235 MAKE_CMD_EXT_CMD(&currentCommand, &length,
2236 edge_port->port->number - edge_port->port->serial->minor,
2237 command, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
Alan Cox03f0dbf2008-07-22 11:16:34 +01002239 status = write_cmd_usb(edge_port, buffer, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 if (status) {
2241 /* something bad happened, let's free up the memory */
2242 kfree(buffer);
2243 }
2244
2245 return status;
2246}
2247
2248
2249/*****************************************************************************
2250 * write_cmd_usb
2251 * this function writes the given buffer out to the bulk write endpoint.
2252 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002253static int write_cmd_usb(struct edgeport_port *edge_port,
2254 unsigned char *buffer, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002256 struct edgeport_serial *edge_serial =
2257 usb_get_serial_data(edge_port->port->serial);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002258 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 int status = 0;
2260 struct urb *urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01002262 usb_serial_debug_data(dev, __func__, length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
2264 /* Allocate our next urb */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002265 urb = usb_alloc_urb(0, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 if (!urb)
2267 return -ENOMEM;
2268
Oliver Neukum96c706e2007-03-15 15:27:17 +01002269 atomic_inc(&CmdUrbs);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002270 dev_dbg(dev, "%s - ALLOCATE URB %p (outstanding %d)\n",
2271 __func__, urb, atomic_read(&CmdUrbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
Alan Cox03f0dbf2008-07-22 11:16:34 +01002273 usb_fill_bulk_urb(urb, edge_serial->serial->dev,
2274 usb_sndbulkpipe(edge_serial->serial->dev,
2275 edge_serial->bulk_out_endpoint),
2276 buffer, length, edge_bulk_out_cmd_callback, edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002278 edge_port->commandPending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 status = usb_submit_urb(urb, GFP_ATOMIC);
2280
2281 if (status) {
2282 /* something went wrong */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002283 dev_err(dev, "%s - usb_submit_urb(write command) failed, status = %d\n",
2284 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 usb_kill_urb(urb);
2286 usb_free_urb(urb);
Oliver Neukum96c706e2007-03-15 15:27:17 +01002287 atomic_dec(&CmdUrbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 return status;
2289 }
2290
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291#if 0
Alan Cox03f0dbf2008-07-22 11:16:34 +01002292 wait_event(&edge_port->wait_command, !edge_port->commandPending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002294 if (edge_port->commandPending) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 /* command timed out */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002296 dev_dbg(dev, "%s - command timed out\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 status = -EINVAL;
2298 }
2299#endif
2300 return status;
2301}
2302
2303
2304/*****************************************************************************
2305 * send_cmd_write_baud_rate
2306 * this function sends the proper command to change the baud rate of the
2307 * specified port.
2308 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002309static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
2310 int baudRate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002312 struct edgeport_serial *edge_serial =
2313 usb_get_serial_data(edge_port->port->serial);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002314 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 unsigned char *cmdBuffer;
2316 unsigned char *currCmd;
2317 int cmdLen = 0;
2318 int divisor;
2319 int status;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002320 unsigned char number =
2321 edge_port->port->number - edge_port->port->serial->minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
Adam Kropelinbc71e472007-07-29 11:03:29 -04002323 if (edge_serial->is_epic &&
2324 !edge_serial->epic_descriptor.Supports.IOSPSetBaudRate) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002325 dev_dbg(dev, "SendCmdWriteBaudRate - NOT Setting baud rate for port = %d, baud = %d\n",
2326 edge_port->port->number, baudRate);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002327 return 0;
2328 }
2329
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002330 dev_dbg(dev, "%s - port = %d, baud = %d\n", __func__,
2331 edge_port->port->number, baudRate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002333 status = calc_baud_rate_divisor(dev, baudRate, &divisor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 if (status) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002335 dev_err(dev, "%s - bad baud rate\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 return status;
2337 }
2338
Alan Cox03f0dbf2008-07-22 11:16:34 +01002339 /* Alloc memory for the string of commands. */
2340 cmdBuffer = kmalloc(0x100, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 if (!cmdBuffer) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002342 dev_err(dev, "%s - kmalloc(%d) failed.\n", __func__, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 return -ENOMEM;
2344 }
2345 currCmd = cmdBuffer;
2346
Alan Cox03f0dbf2008-07-22 11:16:34 +01002347 /* Enable access to divisor latch */
2348 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR, LCR_DL_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
Alan Cox03f0dbf2008-07-22 11:16:34 +01002350 /* Write the divisor itself */
2351 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLL, LOW8(divisor));
2352 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLM, HIGH8(divisor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
Alan Cox03f0dbf2008-07-22 11:16:34 +01002354 /* Restore original value to disable access to divisor latch */
2355 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR,
2356 edge_port->shadowLCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
Alan Cox03f0dbf2008-07-22 11:16:34 +01002358 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 if (status) {
2360 /* something bad happened, let's free up the memory */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002361 kfree(cmdBuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 }
2363
2364 return status;
2365}
2366
2367
2368/*****************************************************************************
2369 * calc_baud_rate_divisor
2370 * this function calculates the proper baud rate divisor for the specified
2371 * baud rate.
2372 *****************************************************************************/
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002373static int calc_baud_rate_divisor(struct device *dev, int baudrate, int *divisor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374{
2375 int i;
2376 __u16 custom;
2377
Tobias Klauser52950ed2005-12-11 16:20:08 +01002378 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002379 if (divisor_table[i].BaudRate == baudrate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 *divisor = divisor_table[i].Divisor;
2381 return 0;
2382 }
2383 }
2384
Alan Cox03f0dbf2008-07-22 11:16:34 +01002385 /* We have tried all of the standard baud rates
2386 * lets try to calculate the divisor for this baud rate
2387 * Make sure the baud rate is reasonable */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 if (baudrate > 50 && baudrate < 230400) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002389 /* get divisor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 custom = (__u16)((230400L + baudrate/2) / baudrate);
2391
2392 *divisor = custom;
2393
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002394 dev_dbg(dev, "%s - Baud %d = %d\n", __func__, baudrate, custom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 return 0;
2396 }
2397
2398 return -1;
2399}
2400
2401
2402/*****************************************************************************
2403 * send_cmd_write_uart_register
Anand Gadiyarfd589a82009-07-16 17:13:03 +02002404 * this function builds up a uart register message and sends to the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002406static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
2407 __u8 regNum, __u8 regValue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002409 struct edgeport_serial *edge_serial =
2410 usb_get_serial_data(edge_port->port->serial);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002411 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 unsigned char *cmdBuffer;
2413 unsigned char *currCmd;
2414 unsigned long cmdLen = 0;
2415 int status;
2416
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002417 dev_dbg(dev, "%s - write to %s register 0x%02x\n",
2418 (regNum == MCR) ? "MCR" : "LCR", __func__, regValue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
Adam Kropelinbc71e472007-07-29 11:03:29 -04002420 if (edge_serial->is_epic &&
2421 !edge_serial->epic_descriptor.Supports.IOSPWriteMCR &&
2422 regNum == MCR) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002423 dev_dbg(dev, "SendCmdWriteUartReg - Not writing to MCR Register\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002424 return 0;
2425 }
2426
Adam Kropelinbc71e472007-07-29 11:03:29 -04002427 if (edge_serial->is_epic &&
2428 !edge_serial->epic_descriptor.Supports.IOSPWriteLCR &&
2429 regNum == LCR) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002430 dev_dbg(dev, "SendCmdWriteUartReg - Not writing to LCR Register\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002431 return 0;
2432 }
2433
Alan Cox03f0dbf2008-07-22 11:16:34 +01002434 /* Alloc memory for the string of commands. */
2435 cmdBuffer = kmalloc(0x10, GFP_ATOMIC);
2436 if (cmdBuffer == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
2439 currCmd = cmdBuffer;
2440
Alan Cox03f0dbf2008-07-22 11:16:34 +01002441 /* Build a cmd in the buffer to write the given register */
2442 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen,
2443 edge_port->port->number - edge_port->port->serial->minor,
2444 regNum, regValue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
2446 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
2447 if (status) {
2448 /* something bad happened, let's free up the memory */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002449 kfree(cmdBuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 }
2451
2452 return status;
2453}
2454
2455
2456/*****************************************************************************
2457 * change_port_settings
Alan Cox03f0dbf2008-07-22 11:16:34 +01002458 * This routine is called to set the UART on the device to match the
2459 * specified new settings.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01002461
2462static void change_port_settings(struct tty_struct *tty,
2463 struct edgeport_port *edge_port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002465 struct device *dev = &edge_port->port->dev;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002466 struct edgeport_serial *edge_serial =
2467 usb_get_serial_data(edge_port->port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 int baud;
2469 unsigned cflag;
2470 __u8 mask = 0xff;
2471 __u8 lData;
2472 __u8 lParity;
2473 __u8 lStop;
2474 __u8 rxFlow;
2475 __u8 txFlow;
2476 int status;
2477
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002478 dev_dbg(dev, "%s - port %d\n", __func__, edge_port->port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002480 if (!edge_port->open &&
2481 !edge_port->openPending) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002482 dev_dbg(dev, "%s - port not opened\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 return;
2484 }
2485
Alan Coxadc8d742012-07-14 15:31:47 +01002486 cflag = tty->termios.c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
2488 switch (cflag & CSIZE) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002489 case CS5:
2490 lData = LCR_BITS_5; mask = 0x1f;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002491 dev_dbg(dev, "%s - data bits = 5\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002492 break;
2493 case CS6:
2494 lData = LCR_BITS_6; mask = 0x3f;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002495 dev_dbg(dev, "%s - data bits = 6\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002496 break;
2497 case CS7:
2498 lData = LCR_BITS_7; mask = 0x7f;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002499 dev_dbg(dev, "%s - data bits = 7\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002500 break;
2501 default:
2502 case CS8:
2503 lData = LCR_BITS_8;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002504 dev_dbg(dev, "%s - data bits = 8\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002505 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 }
2507
2508 lParity = LCR_PAR_NONE;
2509 if (cflag & PARENB) {
2510 if (cflag & CMSPAR) {
2511 if (cflag & PARODD) {
2512 lParity = LCR_PAR_MARK;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002513 dev_dbg(dev, "%s - parity = mark\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 } else {
2515 lParity = LCR_PAR_SPACE;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002516 dev_dbg(dev, "%s - parity = space\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 }
2518 } else if (cflag & PARODD) {
2519 lParity = LCR_PAR_ODD;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002520 dev_dbg(dev, "%s - parity = odd\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 } else {
2522 lParity = LCR_PAR_EVEN;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002523 dev_dbg(dev, "%s - parity = even\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 }
2525 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002526 dev_dbg(dev, "%s - parity = none\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 }
2528
2529 if (cflag & CSTOPB) {
2530 lStop = LCR_STOP_2;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002531 dev_dbg(dev, "%s - stop bits = 2\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 } else {
2533 lStop = LCR_STOP_1;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002534 dev_dbg(dev, "%s - stop bits = 1\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 }
2536
2537 /* figure out the flow control settings */
2538 rxFlow = txFlow = 0x00;
2539 if (cflag & CRTSCTS) {
2540 rxFlow |= IOSP_RX_FLOW_RTS;
2541 txFlow |= IOSP_TX_FLOW_CTS;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002542 dev_dbg(dev, "%s - RTS/CTS is enabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002544 dev_dbg(dev, "%s - RTS/CTS is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 }
2546
Alan Cox03f0dbf2008-07-22 11:16:34 +01002547 /* if we are implementing XON/XOFF, set the start and stop character
2548 in the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 if (I_IXOFF(tty) || I_IXON(tty)) {
2550 unsigned char stop_char = STOP_CHAR(tty);
2551 unsigned char start_char = START_CHAR(tty);
2552
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002553 if ((!edge_serial->is_epic) ||
2554 ((edge_serial->is_epic) &&
2555 (edge_serial->epic_descriptor.Supports.IOSPSetXChar))) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002556 send_iosp_ext_cmd(edge_port,
2557 IOSP_CMD_SET_XON_CHAR, start_char);
2558 send_iosp_ext_cmd(edge_port,
2559 IOSP_CMD_SET_XOFF_CHAR, stop_char);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561
2562 /* if we are implementing INBOUND XON/XOFF */
2563 if (I_IXOFF(tty)) {
2564 rxFlow |= IOSP_RX_FLOW_XON_XOFF;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002565 dev_dbg(dev, "%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2566 __func__, start_char, stop_char);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002568 dev_dbg(dev, "%s - INBOUND XON/XOFF is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 }
2570
2571 /* if we are implementing OUTBOUND XON/XOFF */
2572 if (I_IXON(tty)) {
2573 txFlow |= IOSP_TX_FLOW_XON_XOFF;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002574 dev_dbg(dev, "%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2575 __func__, start_char, stop_char);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002577 dev_dbg(dev, "%s - OUTBOUND XON/XOFF is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 }
2579 }
2580
2581 /* Set flow control to the configured value */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002582 if ((!edge_serial->is_epic) ||
2583 ((edge_serial->is_epic) &&
2584 (edge_serial->epic_descriptor.Supports.IOSPSetRxFlow)))
2585 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_RX_FLOW, rxFlow);
2586 if ((!edge_serial->is_epic) ||
2587 ((edge_serial->is_epic) &&
2588 (edge_serial->epic_descriptor.Supports.IOSPSetTxFlow)))
2589 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_TX_FLOW, txFlow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
2591
2592 edge_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2593 edge_port->shadowLCR |= (lData | lParity | lStop);
2594
2595 edge_port->validDataMask = mask;
2596
2597 /* Send the updated LCR value to the EdgePort */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002598 status = send_cmd_write_uart_register(edge_port, LCR,
2599 edge_port->shadowLCR);
2600 if (status != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602
2603 /* set up the MCR register and send it to the EdgePort */
2604 edge_port->shadowMCR = MCR_MASTER_IE;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002605 if (cflag & CBAUD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 edge_port->shadowMCR |= (MCR_DTR | MCR_RTS);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002607
2608 status = send_cmd_write_uart_register(edge_port, MCR,
2609 edge_port->shadowMCR);
2610 if (status != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612
2613 /* Determine divisor based on baud rate */
2614 baud = tty_get_baud_rate(tty);
2615 if (!baud) {
2616 /* pick a default, any default... */
2617 baud = 9600;
2618 }
2619
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002620 dev_dbg(dev, "%s - baud rate = %d\n", __func__, baud);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002621 status = send_cmd_write_baud_rate(edge_port, baud);
Alan Cox6ce073b2007-10-18 01:24:25 -07002622 if (status == -1) {
2623 /* Speed change was not possible - put back the old speed */
2624 baud = tty_termios_baud_rate(old_termios);
2625 tty_encode_baud_rate(tty, baud, baud);
2626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627}
2628
2629
2630/****************************************************************************
2631 * unicode_to_ascii
2632 * Turns a string from Unicode into ASCII.
2633 * Doesn't do a good job with any characters that are outside the normal
2634 * ASCII range, but it's only for debugging...
2635 * NOTE: expects the unicode in LE format
2636 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002637static void unicode_to_ascii(char *string, int buflen,
2638 __le16 *unicode, int unicode_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639{
2640 int i;
2641
Pete Zaitcevad933752006-05-22 21:49:44 -07002642 if (buflen <= 0) /* never happens, but... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 return;
Pete Zaitcevad933752006-05-22 21:49:44 -07002644 --buflen; /* space for nul */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645
Pete Zaitcevad933752006-05-22 21:49:44 -07002646 for (i = 0; i < unicode_size; i++) {
2647 if (i >= buflen)
2648 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 string[i] = (char)(le16_to_cpu(unicode[i]));
Pete Zaitcevad933752006-05-22 21:49:44 -07002650 }
2651 string[i] = 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652}
2653
2654
2655/****************************************************************************
2656 * get_manufacturing_desc
Alan Cox03f0dbf2008-07-22 11:16:34 +01002657 * reads in the manufacturing descriptor and stores it into the serial
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 * structure.
2659 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002660static void get_manufacturing_desc(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002662 struct device *dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 int response;
2664
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002665 dev_dbg(dev, "getting manufacturer descriptor\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666
Alan Cox03f0dbf2008-07-22 11:16:34 +01002667 response = rom_read(edge_serial->serial,
2668 (EDGE_MANUF_DESC_ADDR & 0xffff0000) >> 16,
2669 (__u16)(EDGE_MANUF_DESC_ADDR & 0x0000ffff),
2670 EDGE_MANUF_DESC_LEN,
2671 (__u8 *)(&edge_serial->manuf_descriptor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
Alan Cox03f0dbf2008-07-22 11:16:34 +01002673 if (response < 1)
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002674 dev_err(dev, "error in getting manufacturer descriptor\n");
Alan Cox03f0dbf2008-07-22 11:16:34 +01002675 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 char string[30];
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002677 dev_dbg(dev, "**Manufacturer Descriptor\n");
2678 dev_dbg(dev, " RomSize: %dK\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002679 edge_serial->manuf_descriptor.RomSize);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002680 dev_dbg(dev, " RamSize: %dK\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002681 edge_serial->manuf_descriptor.RamSize);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002682 dev_dbg(dev, " CpuRev: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002683 edge_serial->manuf_descriptor.CpuRev);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002684 dev_dbg(dev, " BoardRev: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002685 edge_serial->manuf_descriptor.BoardRev);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002686 dev_dbg(dev, " NumPorts: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002687 edge_serial->manuf_descriptor.NumPorts);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002688 dev_dbg(dev, " DescDate: %d/%d/%d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002689 edge_serial->manuf_descriptor.DescDate[0],
2690 edge_serial->manuf_descriptor.DescDate[1],
2691 edge_serial->manuf_descriptor.DescDate[2]+1900);
Pete Zaitcev4bc203d2006-06-06 18:18:33 -07002692 unicode_to_ascii(string, sizeof(string),
Alan Cox03f0dbf2008-07-22 11:16:34 +01002693 edge_serial->manuf_descriptor.SerialNumber,
2694 edge_serial->manuf_descriptor.SerNumLength/2);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002695 dev_dbg(dev, " SerialNumber: %s\n", string);
Pete Zaitcev4bc203d2006-06-06 18:18:33 -07002696 unicode_to_ascii(string, sizeof(string),
Alan Cox03f0dbf2008-07-22 11:16:34 +01002697 edge_serial->manuf_descriptor.AssemblyNumber,
2698 edge_serial->manuf_descriptor.AssemblyNumLength/2);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002699 dev_dbg(dev, " AssemblyNumber: %s\n", string);
Pete Zaitcev4bc203d2006-06-06 18:18:33 -07002700 unicode_to_ascii(string, sizeof(string),
Pete Zaitcevad933752006-05-22 21:49:44 -07002701 edge_serial->manuf_descriptor.OemAssyNumber,
2702 edge_serial->manuf_descriptor.OemAssyNumLength/2);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002703 dev_dbg(dev, " OemAssyNumber: %s\n", string);
2704 dev_dbg(dev, " UartType: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002705 edge_serial->manuf_descriptor.UartType);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002706 dev_dbg(dev, " IonPid: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002707 edge_serial->manuf_descriptor.IonPid);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002708 dev_dbg(dev, " IonConfig: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002709 edge_serial->manuf_descriptor.IonConfig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 }
2711}
2712
2713
2714/****************************************************************************
2715 * get_boot_desc
Alan Cox03f0dbf2008-07-22 11:16:34 +01002716 * reads in the bootloader descriptor and stores it into the serial
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 * structure.
2718 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002719static void get_boot_desc(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002721 struct device *dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 int response;
2723
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002724 dev_dbg(dev, "getting boot descriptor\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725
Alan Cox03f0dbf2008-07-22 11:16:34 +01002726 response = rom_read(edge_serial->serial,
2727 (EDGE_BOOT_DESC_ADDR & 0xffff0000) >> 16,
2728 (__u16)(EDGE_BOOT_DESC_ADDR & 0x0000ffff),
2729 EDGE_BOOT_DESC_LEN,
2730 (__u8 *)(&edge_serial->boot_descriptor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731
Alan Cox03f0dbf2008-07-22 11:16:34 +01002732 if (response < 1)
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002733 dev_err(dev, "error in getting boot descriptor\n");
Alan Cox03f0dbf2008-07-22 11:16:34 +01002734 else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002735 dev_dbg(dev, "**Boot Descriptor:\n");
2736 dev_dbg(dev, " BootCodeLength: %d\n",
2737 le16_to_cpu(edge_serial->boot_descriptor.BootCodeLength));
2738 dev_dbg(dev, " MajorVersion: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002739 edge_serial->boot_descriptor.MajorVersion);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002740 dev_dbg(dev, " MinorVersion: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002741 edge_serial->boot_descriptor.MinorVersion);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002742 dev_dbg(dev, " BuildNumber: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002743 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002744 dev_dbg(dev, " Capabilities: 0x%x\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002745 le16_to_cpu(edge_serial->boot_descriptor.Capabilities));
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002746 dev_dbg(dev, " UConfig0: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002747 edge_serial->boot_descriptor.UConfig0);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002748 dev_dbg(dev, " UConfig1: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002749 edge_serial->boot_descriptor.UConfig1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 }
2751}
2752
2753
2754/****************************************************************************
2755 * load_application_firmware
2756 * This is called to load the application firmware to the device
2757 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002758static void load_application_firmware(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002760 struct device *dev = &edge_serial->serial->dev->dev;
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302761 const struct ihex_binrec *rec;
2762 const struct firmware *fw;
2763 const char *fw_name;
2764 const char *fw_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 int response;
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302766 __u32 Operaddr;
2767 __u16 build;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768
2769 switch (edge_serial->product_info.iDownloadFile) {
2770 case EDGE_DOWNLOAD_FILE_I930:
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302771 fw_info = "downloading firmware version (930)";
2772 fw_name = "edgeport/down.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 break;
2774
2775 case EDGE_DOWNLOAD_FILE_80251:
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302776 fw_info = "downloading firmware version (80251)";
2777 fw_name = "edgeport/down2.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 break;
2779
2780 case EDGE_DOWNLOAD_FILE_NONE:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002781 dev_dbg(dev, "No download file specified, skipping download\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 return;
2783
2784 default:
2785 return;
2786 }
2787
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302788 response = request_ihex_firmware(&fw, fw_name,
2789 &edge_serial->serial->dev->dev);
2790 if (response) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002791 dev_err(dev, "Failed to load image \"%s\" err %d\n",
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302792 fw_name, response);
2793 return;
2794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302796 rec = (const struct ihex_binrec *)fw->data;
2797 build = (rec->data[2] << 8) | rec->data[3];
2798
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002799 dev_dbg(dev, "%s %d.%d.%d\n", fw_info, rec->data[0], rec->data[1], build);
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302800
Bjørn Mork271c1152011-01-17 14:19:37 +01002801 edge_serial->product_info.FirmwareMajorVersion = rec->data[0];
2802 edge_serial->product_info.FirmwareMinorVersion = rec->data[1];
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302803 edge_serial->product_info.FirmwareBuildNumber = cpu_to_le16(build);
2804
2805 for (rec = ihex_next_binrec(rec); rec;
2806 rec = ihex_next_binrec(rec)) {
2807 Operaddr = be32_to_cpu(rec->addr);
2808 response = sram_write(edge_serial->serial,
2809 Operaddr >> 16,
2810 Operaddr & 0xFFFF,
2811 be16_to_cpu(rec->len),
2812 &rec->data[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 if (response < 0) {
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302814 dev_err(&edge_serial->serial->dev->dev,
2815 "sram_write failed (%x, %x, %d)\n",
2816 Operaddr >> 16, Operaddr & 0xFFFF,
2817 be16_to_cpu(rec->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 break;
2819 }
2820 }
2821
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002822 dev_dbg(dev, "sending exec_dl_code\n");
2823 response = usb_control_msg (edge_serial->serial->dev,
2824 usb_sndctrlpipe(edge_serial->serial->dev, 0),
2825 USB_REQUEST_ION_EXEC_DL_CODE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 0x40, 0x4000, 0x0001, NULL, 0, 3000);
2827
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302828 release_firmware(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829}
2830
2831
2832/****************************************************************************
2833 * edge_startup
2834 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002835static int edge_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836{
2837 struct edgeport_serial *edge_serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 struct usb_device *dev;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002839 struct device *ddev = &serial->dev->dev;
Johan Hovoldc27f3ef2012-10-17 13:34:57 +02002840 int i;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002841 int response;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002842 bool interrupt_in_found;
2843 bool bulk_in_found;
2844 bool bulk_out_found;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002845 static __u32 descriptor[3] = { EDGE_COMPATIBILITY_MASK0,
2846 EDGE_COMPATIBILITY_MASK1,
2847 EDGE_COMPATIBILITY_MASK2 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
2849 dev = serial->dev;
2850
2851 /* create our private serial structure */
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +01002852 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 if (edge_serial == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002854 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 return -ENOMEM;
2856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 spin_lock_init(&edge_serial->es_lock);
2858 edge_serial->serial = serial;
2859 usb_set_serial_data(serial, edge_serial);
2860
2861 /* get the name for the device from the device */
Dan Carpenterf10718f2010-01-25 14:53:33 +03002862 i = usb_string(dev, dev->descriptor.iManufacturer,
Pete Zaitcevad933752006-05-22 21:49:44 -07002863 &edge_serial->name[0], MAX_NAME_LEN+1);
Dan Carpenterf10718f2010-01-25 14:53:33 +03002864 if (i < 0)
2865 i = 0;
Pete Zaitcevad933752006-05-22 21:49:44 -07002866 edge_serial->name[i++] = ' ';
Dan Carpenterf10718f2010-01-25 14:53:33 +03002867 usb_string(dev, dev->descriptor.iProduct,
Pete Zaitcevad933752006-05-22 21:49:44 -07002868 &edge_serial->name[i], MAX_NAME_LEN+2 - i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
2870 dev_info(&serial->dev->dev, "%s detected\n", edge_serial->name);
2871
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002872 /* Read the epic descriptor */
2873 if (get_epic_descriptor(edge_serial) <= 0) {
2874 /* memcpy descriptor to Supports structures */
2875 memcpy(&edge_serial->epic_descriptor.Supports, descriptor,
2876 sizeof(struct edge_compatibility_bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002878 /* get the manufacturing descriptor for this device */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002879 get_manufacturing_desc(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002881 /* get the boot descriptor */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002882 get_boot_desc(edge_serial);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002883
2884 get_product_info(edge_serial);
2885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886
2887 /* set the number of ports from the manufacturing description */
2888 /* serial->num_ports = serial->product_info.NumPorts; */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002889 if ((!edge_serial->is_epic) &&
2890 (edge_serial->product_info.NumPorts != serial->num_ports)) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002891 dev_warn(ddev,
2892 "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 -08002893 edge_serial->product_info.NumPorts,
2894 serial->num_ports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 }
2896
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002897 dev_dbg(ddev, "%s - time 1 %ld\n", __func__, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002899 /* If not an EPiC device */
2900 if (!edge_serial->is_epic) {
2901 /* now load the application firmware into this device */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002902 load_application_firmware(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002904 dev_dbg(ddev, "%s - time 2 %ld\n", __func__, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002906 /* Check current Edgeport EEPROM and update if necessary */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002907 update_edgeport_E2PROM(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002909 dev_dbg(ddev, "%s - time 3 %ld\n", __func__, jiffies);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002910
2911 /* set the configuration to use #1 */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002912/* dev_dbg(ddev, "set_configuration 1\n"); */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002913/* usb_set_configuration (dev, 1); */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002914 }
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002915 dev_dbg(ddev, " FirmwareMajorVersion %d.%d.%d\n",
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302916 edge_serial->product_info.FirmwareMajorVersion,
2917 edge_serial->product_info.FirmwareMinorVersion,
2918 le16_to_cpu(edge_serial->product_info.FirmwareBuildNumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919
Alan Cox03f0dbf2008-07-22 11:16:34 +01002920 /* we set up the pointers to the endpoints in the edge_open function,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 * as the structures aren't created yet. */
2922
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002923 response = 0;
2924
2925 if (edge_serial->is_epic) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002926 /* EPIC thing, set up our interrupt polling now and our read
2927 * urb, so that the device knows it really is connected. */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002928 interrupt_in_found = bulk_in_found = bulk_out_found = false;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002929 for (i = 0; i < serial->interface->altsetting[0]
2930 .desc.bNumEndpoints; ++i) {
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002931 struct usb_endpoint_descriptor *endpoint;
2932 int buffer_size;
2933
Alan Cox03f0dbf2008-07-22 11:16:34 +01002934 endpoint = &serial->interface->altsetting[0].
2935 endpoint[i].desc;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07002936 buffer_size = usb_endpoint_maxp(endpoint);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002937 if (!interrupt_in_found &&
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002938 (usb_endpoint_is_int_in(endpoint))) {
2939 /* we found a interrupt in endpoint */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002940 dev_dbg(ddev, "found interrupt in\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002941
2942 /* not set up yet, so do it now */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002943 edge_serial->interrupt_read_urb =
2944 usb_alloc_urb(0, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002945 if (!edge_serial->interrupt_read_urb) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002946 dev_err(ddev, "out of memory\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002947 return -ENOMEM;
2948 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01002949 edge_serial->interrupt_in_buffer =
2950 kmalloc(buffer_size, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002951 if (!edge_serial->interrupt_in_buffer) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002952 dev_err(ddev, "out of memory\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002953 usb_free_urb(edge_serial->interrupt_read_urb);
2954 return -ENOMEM;
2955 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01002956 edge_serial->interrupt_in_endpoint =
2957 endpoint->bEndpointAddress;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002958
2959 /* set up our interrupt urb */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002960 usb_fill_int_urb(
2961 edge_serial->interrupt_read_urb,
2962 dev,
2963 usb_rcvintpipe(dev,
2964 endpoint->bEndpointAddress),
2965 edge_serial->interrupt_in_buffer,
2966 buffer_size,
2967 edge_interrupt_callback,
2968 edge_serial,
2969 endpoint->bInterval);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002970
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002971 interrupt_in_found = true;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002972 }
2973
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002974 if (!bulk_in_found &&
Alan Cox03f0dbf2008-07-22 11:16:34 +01002975 (usb_endpoint_is_bulk_in(endpoint))) {
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002976 /* we found a bulk in endpoint */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002977 dev_dbg(ddev, "found bulk in\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002978
2979 /* not set up yet, so do it now */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002980 edge_serial->read_urb =
2981 usb_alloc_urb(0, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002982 if (!edge_serial->read_urb) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002983 dev_err(ddev, "out of memory\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002984 return -ENOMEM;
2985 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01002986 edge_serial->bulk_in_buffer =
2987 kmalloc(buffer_size, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002988 if (!edge_serial->bulk_in_buffer) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07002989 dev_err(&dev->dev, "out of memory\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002990 usb_free_urb(edge_serial->read_urb);
2991 return -ENOMEM;
2992 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01002993 edge_serial->bulk_in_endpoint =
2994 endpoint->bEndpointAddress;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002995
2996 /* set up our bulk in urb */
2997 usb_fill_bulk_urb(edge_serial->read_urb, dev,
Alan Cox03f0dbf2008-07-22 11:16:34 +01002998 usb_rcvbulkpipe(dev,
2999 endpoint->bEndpointAddress),
3000 edge_serial->bulk_in_buffer,
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07003001 usb_endpoint_maxp(endpoint),
Alan Cox03f0dbf2008-07-22 11:16:34 +01003002 edge_bulk_in_callback,
3003 edge_serial);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003004 bulk_in_found = true;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003005 }
3006
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003007 if (!bulk_out_found &&
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003008 (usb_endpoint_is_bulk_out(endpoint))) {
3009 /* we found a bulk out endpoint */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003010 dev_dbg(ddev, "found bulk out\n");
Alan Cox03f0dbf2008-07-22 11:16:34 +01003011 edge_serial->bulk_out_endpoint =
3012 endpoint->bEndpointAddress;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003013 bulk_out_found = true;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003014 }
3015 }
3016
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003017 if (!interrupt_in_found || !bulk_in_found || !bulk_out_found) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003018 dev_err(ddev, "Error - the proper endpoints were not found!\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003019 return -ENODEV;
3020 }
3021
3022 /* start interrupt read for this edgeport this interrupt will
3023 * continue as long as the edgeport is connected */
Alan Cox03f0dbf2008-07-22 11:16:34 +01003024 response = usb_submit_urb(edge_serial->interrupt_read_urb,
3025 GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003026 if (response)
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003027 dev_err(ddev, "%s - Error %d submitting control urb\n",
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07003028 __func__, response);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003029 }
3030 return response;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031}
3032
3033
3034/****************************************************************************
Alan Sternf9c99bb2009-06-02 11:53:55 -04003035 * edge_disconnect
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 * This function is called whenever the device is removed from the usb bus.
3037 ****************************************************************************/
Alan Sternf9c99bb2009-06-02 11:53:55 -04003038static void edge_disconnect(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039{
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003040 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 /* stop reads and writes on all ports */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003043 /* free up our endpoint stuff */
3044 if (edge_serial->is_epic) {
Oliver Neukum74ac07e2007-06-13 18:50:41 +02003045 usb_kill_urb(edge_serial->interrupt_read_urb);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003046 usb_free_urb(edge_serial->interrupt_read_urb);
3047 kfree(edge_serial->interrupt_in_buffer);
3048
Oliver Neukum74ac07e2007-06-13 18:50:41 +02003049 usb_kill_urb(edge_serial->read_urb);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003050 usb_free_urb(edge_serial->read_urb);
3051 kfree(edge_serial->bulk_in_buffer);
3052 }
Alan Sternf9c99bb2009-06-02 11:53:55 -04003053}
3054
3055
3056/****************************************************************************
3057 * edge_release
3058 * This function is called when the device structure is deallocated.
3059 ****************************************************************************/
3060static void edge_release(struct usb_serial *serial)
3061{
3062 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003063
3064 kfree(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065}
3066
Johan Hovoldc27f3ef2012-10-17 13:34:57 +02003067static int edge_port_probe(struct usb_serial_port *port)
3068{
3069 struct edgeport_port *edge_port;
3070
3071 edge_port = kzalloc(sizeof(*edge_port), GFP_KERNEL);
3072 if (!edge_port)
3073 return -ENOMEM;
3074
3075 spin_lock_init(&edge_port->ep_lock);
3076 edge_port->port = port;
3077
3078 usb_set_serial_port_data(port, edge_port);
3079
3080 return 0;
3081}
3082
3083static int edge_port_remove(struct usb_serial_port *port)
3084{
3085 struct edgeport_port *edge_port;
3086
3087 edge_port = usb_get_serial_port_data(port);
3088 kfree(edge_port);
3089
3090 return 0;
3091}
3092
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07003093module_usb_serial_driver(serial_drivers, id_table_combined);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094
Alan Cox03f0dbf2008-07-22 11:16:34 +01003095MODULE_AUTHOR(DRIVER_AUTHOR);
3096MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097MODULE_LICENSE("GPL");
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05303098MODULE_FIRMWARE("edgeport/boot.fw");
3099MODULE_FIRMWARE("edgeport/boot2.fw");
3100MODULE_FIRMWARE("edgeport/down.fw");
3101MODULE_FIRMWARE("edgeport/down2.fw");