blob: b00e5cbf741f16b63071c8273c7f69eaa279ca6d [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 */
113 wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
114
115 struct async_icount icount;
116 struct usb_serial_port *port; /* loop back to the owner of this object */
117};
118
119
120/* This structure holds all of the individual device information */
121struct edgeport_serial {
Pete Zaitcevad933752006-05-22 21:49:44 -0700122 char name[MAX_NAME_LEN+2]; /* string name of this device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 struct edge_manuf_descriptor manuf_descriptor; /* the manufacturer descriptor */
125 struct edge_boot_descriptor boot_descriptor; /* the boot firmware descriptor */
126 struct edgeport_product_info product_info; /* Product Info */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800127 struct edge_compatibility_descriptor epic_descriptor; /* Edgeport compatible descriptor */
128 int is_epic; /* flag if EPiC device or not */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 __u8 interrupt_in_endpoint; /* the interrupt endpoint handle */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100131 unsigned char *interrupt_in_buffer; /* the buffer we use for the interrupt endpoint */
132 struct urb *interrupt_read_urb; /* our interrupt urb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 __u8 bulk_in_endpoint; /* the bulk in endpoint handle */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100135 unsigned char *bulk_in_buffer; /* the buffer we use for the bulk in endpoint */
136 struct urb *read_urb; /* our bulk read urb */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100137 bool read_in_progress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 spinlock_t es_lock;
139
140 __u8 bulk_out_endpoint; /* the bulk out endpoint handle */
141
142 __s16 rxBytesAvail; /* the number of bytes that we need to read from this device */
143
144 enum RXSTATE rxState; /* the current state of the bulk receive processor */
145 __u8 rxHeader1; /* receive header byte 1 */
146 __u8 rxHeader2; /* receive header byte 2 */
147 __u8 rxHeader3; /* receive header byte 3 */
148 __u8 rxPort; /* the port that we are currently receiving data for */
149 __u8 rxStatusCode; /* the receive status code */
150 __u8 rxStatusParam; /* the receive status paramater */
151 __s16 rxBytesRemaining; /* the number of port bytes left to read */
152 struct usb_serial *serial; /* loop back to the owner of this object */
153};
154
155/* baud rate information */
156struct divisor_table_entry {
157 __u32 BaudRate;
158 __u16 Divisor;
159};
160
Alan Cox03f0dbf2008-07-22 11:16:34 +0100161/*
162 * Define table of divisors for Rev A EdgePort/4 hardware
163 * These assume a 3.6864MHz crystal, the standard /16, and
164 * MCR.7 = 0.
165 */
166
Arjan van de Ven4c4c9432005-11-29 09:43:42 +0100167static const struct divisor_table_entry divisor_table[] = {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100168 { 50, 4608},
169 { 75, 3072},
170 { 110, 2095}, /* 2094.545455 => 230450 => .0217 % over */
171 { 134, 1713}, /* 1713.011152 => 230398.5 => .00065% under */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 { 150, 1536},
173 { 300, 768},
174 { 600, 384},
175 { 1200, 192},
176 { 1800, 128},
177 { 2400, 96},
178 { 4800, 48},
179 { 7200, 32},
180 { 9600, 24},
181 { 14400, 16},
182 { 19200, 12},
183 { 38400, 6},
184 { 57600, 4},
185 { 115200, 2},
186 { 230400, 1},
187};
188
Greg Kroah-Hartman36ccce42012-02-28 13:11:51 -0800189/* Number of outstanding Command Write Urbs */
190static atomic_t CmdUrbs = ATOMIC_INIT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192
193/* local function prototypes */
194
195/* function prototypes for all URB callbacks */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100196static void edge_interrupt_callback(struct urb *urb);
197static void edge_bulk_in_callback(struct urb *urb);
198static void edge_bulk_out_data_callback(struct urb *urb);
199static void edge_bulk_out_cmd_callback(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201/* function prototypes for the usbserial callbacks */
Alan Coxa509a7e2009-09-19 13:13:26 -0700202static int edge_open(struct tty_struct *tty, struct usb_serial_port *port);
Alan Cox335f8512009-06-11 12:26:29 +0100203static void edge_close(struct usb_serial_port *port);
Alan Cox03f0dbf2008-07-22 11:16:34 +0100204static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
205 const unsigned char *buf, int count);
206static int edge_write_room(struct tty_struct *tty);
207static int edge_chars_in_buffer(struct tty_struct *tty);
208static void edge_throttle(struct tty_struct *tty);
209static void edge_unthrottle(struct tty_struct *tty);
210static void edge_set_termios(struct tty_struct *tty,
211 struct usb_serial_port *port,
212 struct ktermios *old_termios);
Alan Cox00a0d0d2011-02-14 16:27:06 +0000213static int edge_ioctl(struct tty_struct *tty,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100214 unsigned int cmd, unsigned long arg);
215static void edge_break(struct tty_struct *tty, int break_state);
Alan Cox60b33c12011-02-14 16:26:14 +0000216static int edge_tiocmget(struct tty_struct *tty);
Alan Cox20b9d172011-02-14 16:26:50 +0000217static int edge_tiocmset(struct tty_struct *tty,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100218 unsigned int set, unsigned int clear);
Alan Cox0bca1b92010-09-16 18:21:40 +0100219static int edge_get_icount(struct tty_struct *tty,
220 struct serial_icounter_struct *icount);
Alan Cox03f0dbf2008-07-22 11:16:34 +0100221static int edge_startup(struct usb_serial *serial);
Alan Sternf9c99bb2009-06-02 11:53:55 -0400222static void edge_disconnect(struct usb_serial *serial);
223static void edge_release(struct usb_serial *serial);
Johan Hovoldc27f3ef2012-10-17 13:34:57 +0200224static int edge_port_probe(struct usb_serial_port *port);
225static int edge_port_remove(struct usb_serial_port *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227#include "io_tables.h" /* all of the devices that this driver supports */
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229/* function prototypes for all of our local functions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Alan Cox03f0dbf2008-07-22 11:16:34 +0100231static void process_rcvd_data(struct edgeport_serial *edge_serial,
232 unsigned char *buffer, __u16 bufferLength);
233static void process_rcvd_status(struct edgeport_serial *edge_serial,
234 __u8 byte2, __u8 byte3);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100235static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
236 int length);
Alan Cox03f0dbf2008-07-22 11:16:34 +0100237static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr);
238static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
239 __u8 lsr, __u8 data);
240static int send_iosp_ext_cmd(struct edgeport_port *edge_port, __u8 command,
241 __u8 param);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700242static int calc_baud_rate_divisor(struct device *dev, int baud_rate, int *divisor);
Alan Cox03f0dbf2008-07-22 11:16:34 +0100243static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
244 int baudRate);
245static void change_port_settings(struct tty_struct *tty,
246 struct edgeport_port *edge_port,
247 struct ktermios *old_termios);
248static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
249 __u8 regNum, __u8 regValue);
250static int write_cmd_usb(struct edgeport_port *edge_port,
251 unsigned char *buffer, int writeLength);
252static void send_more_port_data(struct edgeport_serial *edge_serial,
253 struct edgeport_port *edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Alan Cox03f0dbf2008-07-22 11:16:34 +0100255static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
256 __u16 length, const __u8 *data);
257static int rom_read(struct usb_serial *serial, __u16 extAddr, __u16 addr,
258 __u16 length, __u8 *data);
259static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
260 __u16 length, const __u8 *data);
261static void get_manufacturing_desc(struct edgeport_serial *edge_serial);
262static void get_boot_desc(struct edgeport_serial *edge_serial);
263static void load_application_firmware(struct edgeport_serial *edge_serial);
264
265static void unicode_to_ascii(char *string, int buflen,
266 __le16 *unicode, int unicode_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268
Alan Cox03f0dbf2008-07-22 11:16:34 +0100269/* ************************************************************************ */
270/* ************************************************************************ */
271/* ************************************************************************ */
272/* ************************************************************************ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274/************************************************************************
275 * *
276 * update_edgeport_E2PROM() Compare current versions of *
277 * Boot ROM and Manufacture *
278 * Descriptors with versions *
279 * embedded in this driver *
280 * *
281 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100282static void update_edgeport_E2PROM(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700284 struct device *dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 __u32 BootCurVer;
286 __u32 BootNewVer;
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530287 __u8 BootMajorVersion;
288 __u8 BootMinorVersion;
289 __u16 BootBuildNumber;
290 __u32 Bootaddr;
291 const struct ihex_binrec *rec;
292 const struct firmware *fw;
293 const char *fw_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 int response;
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 switch (edge_serial->product_info.iDownloadFile) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100297 case EDGE_DOWNLOAD_FILE_I930:
298 fw_name = "edgeport/boot.fw";
299 break;
300 case EDGE_DOWNLOAD_FILE_80251:
301 fw_name = "edgeport/boot2.fw";
302 break;
303 default:
304 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
306
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530307 response = request_ihex_firmware(&fw, fw_name,
308 &edge_serial->serial->dev->dev);
309 if (response) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700310 dev_err(dev, "Failed to load image \"%s\" err %d\n",
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530311 fw_name, response);
312 return;
313 }
314
315 rec = (const struct ihex_binrec *)fw->data;
316 BootMajorVersion = rec->data[0];
317 BootMinorVersion = rec->data[1];
318 BootBuildNumber = (rec->data[2] << 8) | rec->data[3];
319
Alan Cox03f0dbf2008-07-22 11:16:34 +0100320 /* Check Boot Image Version */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 BootCurVer = (edge_serial->boot_descriptor.MajorVersion << 24) +
322 (edge_serial->boot_descriptor.MinorVersion << 16) +
323 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber);
324
325 BootNewVer = (BootMajorVersion << 24) +
326 (BootMinorVersion << 16) +
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530327 BootBuildNumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700329 dev_dbg(dev, "Current Boot Image version %d.%d.%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 edge_serial->boot_descriptor.MajorVersion,
331 edge_serial->boot_descriptor.MinorVersion,
332 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
333
334
335 if (BootNewVer > BootCurVer) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700336 dev_dbg(dev, "**Update Boot Image from %d.%d.%d to %d.%d.%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 edge_serial->boot_descriptor.MajorVersion,
338 edge_serial->boot_descriptor.MinorVersion,
339 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber),
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530340 BootMajorVersion, BootMinorVersion, BootBuildNumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700342 dev_dbg(dev, "Downloading new Boot Image\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530344 for (rec = ihex_next_binrec(rec); rec;
345 rec = ihex_next_binrec(rec)) {
346 Bootaddr = be32_to_cpu(rec->addr);
347 response = rom_write(edge_serial->serial,
348 Bootaddr >> 16,
349 Bootaddr & 0xFFFF,
350 be16_to_cpu(rec->len),
351 &rec->data[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (response < 0) {
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530353 dev_err(&edge_serial->serial->dev->dev,
354 "rom_write failed (%x, %x, %d)\n",
355 Bootaddr >> 16, Bootaddr & 0xFFFF,
356 be16_to_cpu(rec->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 break;
358 }
359 }
360 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700361 dev_dbg(dev, "Boot Image -- already up to date\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
Jaswinder Singh5b9ea932008-07-03 17:00:23 +0530363 release_firmware(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366#if 0
367/************************************************************************
368 *
369 * Get string descriptor from device
370 *
371 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100372static int get_string_desc(struct usb_device *dev, int Id,
373 struct usb_string_descriptor **pRetDesc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
375 struct usb_string_descriptor StringDesc;
376 struct usb_string_descriptor *pStringDesc;
377
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700378 dev_dbg(&dev->dev, "%s - USB String ID = %d\n", __func__, Id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Alan Cox03f0dbf2008-07-22 11:16:34 +0100380 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc,
381 sizeof(StringDesc)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Alan Cox03f0dbf2008-07-22 11:16:34 +0100384 pStringDesc = kmalloc(StringDesc.bLength, GFP_KERNEL);
385 if (!pStringDesc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Alan Cox03f0dbf2008-07-22 11:16:34 +0100388 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc,
389 StringDesc.bLength)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 kfree(pStringDesc);
391 return -1;
392 }
393
394 *pRetDesc = pStringDesc;
395 return 0;
396}
397#endif
398
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700399static void dump_product_info(struct edgeport_serial *edge_serial,
400 struct edgeport_product_info *product_info)
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800401{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700402 struct device *dev = &edge_serial->serial->dev->dev;
403
Alan Cox03f0dbf2008-07-22 11:16:34 +0100404 /* Dump Product Info structure */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700405 dev_dbg(dev, "**Product Information:\n");
406 dev_dbg(dev, " ProductId %x\n", product_info->ProductId);
407 dev_dbg(dev, " NumPorts %d\n", product_info->NumPorts);
408 dev_dbg(dev, " ProdInfoVer %d\n", product_info->ProdInfoVer);
409 dev_dbg(dev, " IsServer %d\n", product_info->IsServer);
410 dev_dbg(dev, " IsRS232 %d\n", product_info->IsRS232);
411 dev_dbg(dev, " IsRS422 %d\n", product_info->IsRS422);
412 dev_dbg(dev, " IsRS485 %d\n", product_info->IsRS485);
413 dev_dbg(dev, " RomSize %d\n", product_info->RomSize);
414 dev_dbg(dev, " RamSize %d\n", product_info->RamSize);
415 dev_dbg(dev, " CpuRev %x\n", product_info->CpuRev);
416 dev_dbg(dev, " BoardRev %x\n", product_info->BoardRev);
417 dev_dbg(dev, " BootMajorVersion %d.%d.%d\n",
418 product_info->BootMajorVersion,
419 product_info->BootMinorVersion,
420 le16_to_cpu(product_info->BootBuildNumber));
421 dev_dbg(dev, " FirmwareMajorVersion %d.%d.%d\n",
422 product_info->FirmwareMajorVersion,
423 product_info->FirmwareMinorVersion,
424 le16_to_cpu(product_info->FirmwareBuildNumber));
425 dev_dbg(dev, " ManufactureDescDate %d/%d/%d\n",
426 product_info->ManufactureDescDate[0],
427 product_info->ManufactureDescDate[1],
428 product_info->ManufactureDescDate[2]+1900);
429 dev_dbg(dev, " iDownloadFile 0x%x\n",
430 product_info->iDownloadFile);
431 dev_dbg(dev, " EpicVer %d\n", product_info->EpicVer);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800432}
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434static void get_product_info(struct edgeport_serial *edge_serial)
435{
436 struct edgeport_product_info *product_info = &edge_serial->product_info;
437
Alan Cox03f0dbf2008-07-22 11:16:34 +0100438 memset(product_info, 0, sizeof(struct edgeport_product_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Alan Cox03f0dbf2008-07-22 11:16:34 +0100440 product_info->ProductId = (__u16)(le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct) & ~ION_DEVICE_ID_80251_NETCHIP);
441 product_info->NumPorts = edge_serial->manuf_descriptor.NumPorts;
442 product_info->ProdInfoVer = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Alan Cox03f0dbf2008-07-22 11:16:34 +0100444 product_info->RomSize = edge_serial->manuf_descriptor.RomSize;
445 product_info->RamSize = edge_serial->manuf_descriptor.RamSize;
446 product_info->CpuRev = edge_serial->manuf_descriptor.CpuRev;
447 product_info->BoardRev = edge_serial->manuf_descriptor.BoardRev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Alan Cox03f0dbf2008-07-22 11:16:34 +0100449 product_info->BootMajorVersion =
450 edge_serial->boot_descriptor.MajorVersion;
451 product_info->BootMinorVersion =
452 edge_serial->boot_descriptor.MinorVersion;
453 product_info->BootBuildNumber =
454 edge_serial->boot_descriptor.BuildNumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Alan Cox03f0dbf2008-07-22 11:16:34 +0100456 memcpy(product_info->ManufactureDescDate,
457 edge_serial->manuf_descriptor.DescDate,
458 sizeof(edge_serial->manuf_descriptor.DescDate));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Alan Cox03f0dbf2008-07-22 11:16:34 +0100460 /* check if this is 2nd generation hardware */
461 if (le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct)
462 & ION_DEVICE_ID_80251_NETCHIP)
463 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_80251;
464 else
465 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_I930;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700466
Alan Cox03f0dbf2008-07-22 11:16:34 +0100467 /* Determine Product type and set appropriate flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 switch (DEVICE_ID_FROM_USB_PRODUCT_ID(product_info->ProductId)) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100469 case ION_DEVICE_ID_EDGEPORT_COMPATIBLE:
470 case ION_DEVICE_ID_EDGEPORT_4T:
471 case ION_DEVICE_ID_EDGEPORT_4:
472 case ION_DEVICE_ID_EDGEPORT_2:
473 case ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU:
474 case ION_DEVICE_ID_EDGEPORT_8:
475 case ION_DEVICE_ID_EDGEPORT_421:
476 case ION_DEVICE_ID_EDGEPORT_21:
477 case ION_DEVICE_ID_EDGEPORT_2_DIN:
478 case ION_DEVICE_ID_EDGEPORT_4_DIN:
479 case ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU:
480 product_info->IsRS232 = 1;
481 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Alan Cox03f0dbf2008-07-22 11:16:34 +0100483 case ION_DEVICE_ID_EDGEPORT_2I: /* Edgeport/2 RS422/RS485 */
484 product_info->IsRS422 = 1;
485 product_info->IsRS485 = 1;
486 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Alan Cox03f0dbf2008-07-22 11:16:34 +0100488 case ION_DEVICE_ID_EDGEPORT_8I: /* Edgeport/4 RS422 */
489 case ION_DEVICE_ID_EDGEPORT_4I: /* Edgeport/4 RS422 */
490 product_info->IsRS422 = 1;
491 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
493
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700494 dump_product_info(edge_serial, product_info);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800495}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800497static int get_epic_descriptor(struct edgeport_serial *ep)
498{
499 int result;
500 struct usb_serial *serial = ep->serial;
501 struct edgeport_product_info *product_info = &ep->product_info;
502 struct edge_compatibility_descriptor *epic = &ep->epic_descriptor;
503 struct edge_compatibility_bits *bits;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700504 struct device *dev = &serial->dev->dev;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800505
506 ep->is_epic = 0;
507 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
508 USB_REQUEST_ION_GET_EPIC_DESC,
509 0xC0, 0x00, 0x00,
510 &ep->epic_descriptor,
511 sizeof(struct edge_compatibility_descriptor),
512 300);
513
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800514 if (result > 0) {
515 ep->is_epic = 1;
516 memset(product_info, 0, sizeof(struct edgeport_product_info));
517
Alan Cox03f0dbf2008-07-22 11:16:34 +0100518 product_info->NumPorts = epic->NumPorts;
519 product_info->ProdInfoVer = 0;
520 product_info->FirmwareMajorVersion = epic->MajorVersion;
521 product_info->FirmwareMinorVersion = epic->MinorVersion;
522 product_info->FirmwareBuildNumber = epic->BuildNumber;
523 product_info->iDownloadFile = epic->iDownloadFile;
524 product_info->EpicVer = epic->EpicVer;
525 product_info->Epic = epic->Supports;
526 product_info->ProductId = ION_DEVICE_ID_EDGEPORT_COMPATIBLE;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700527 dump_product_info(ep, product_info);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800528
529 bits = &ep->epic_descriptor.Supports;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700530 dev_dbg(dev, "**EPIC descriptor:\n");
531 dev_dbg(dev, " VendEnableSuspend: %s\n", bits->VendEnableSuspend ? "TRUE": "FALSE");
532 dev_dbg(dev, " IOSPOpen : %s\n", bits->IOSPOpen ? "TRUE": "FALSE");
533 dev_dbg(dev, " IOSPClose : %s\n", bits->IOSPClose ? "TRUE": "FALSE");
534 dev_dbg(dev, " IOSPChase : %s\n", bits->IOSPChase ? "TRUE": "FALSE");
535 dev_dbg(dev, " IOSPSetRxFlow : %s\n", bits->IOSPSetRxFlow ? "TRUE": "FALSE");
536 dev_dbg(dev, " IOSPSetTxFlow : %s\n", bits->IOSPSetTxFlow ? "TRUE": "FALSE");
537 dev_dbg(dev, " IOSPSetXChar : %s\n", bits->IOSPSetXChar ? "TRUE": "FALSE");
538 dev_dbg(dev, " IOSPRxCheck : %s\n", bits->IOSPRxCheck ? "TRUE": "FALSE");
539 dev_dbg(dev, " IOSPSetClrBreak : %s\n", bits->IOSPSetClrBreak ? "TRUE": "FALSE");
540 dev_dbg(dev, " IOSPWriteMCR : %s\n", bits->IOSPWriteMCR ? "TRUE": "FALSE");
541 dev_dbg(dev, " IOSPWriteLCR : %s\n", bits->IOSPWriteLCR ? "TRUE": "FALSE");
542 dev_dbg(dev, " IOSPSetBaudRate : %s\n", bits->IOSPSetBaudRate ? "TRUE": "FALSE");
543 dev_dbg(dev, " TrueEdgeport : %s\n", bits->TrueEdgeport ? "TRUE": "FALSE");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -0800544 }
545
546 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
549
550/************************************************************************/
551/************************************************************************/
552/* U S B C A L L B A C K F U N C T I O N S */
553/* U S B C A L L B A C K F U N C T I O N S */
554/************************************************************************/
555/************************************************************************/
556
557/*****************************************************************************
558 * edge_interrupt_callback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100559 * this is the callback function for when we have received data on the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 * interrupt endpoint.
561 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100562static void edge_interrupt_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700564 struct edgeport_serial *edge_serial = urb->context;
565 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 struct edgeport_port *edge_port;
567 struct usb_serial_port *port;
Alan Cox4a90f092008-10-13 10:39:46 +0100568 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 unsigned char *data = urb->transfer_buffer;
570 int length = urb->actual_length;
571 int bytes_avail;
572 int position;
573 int txCredits;
574 int portNumber;
575 int result;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700576 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700578 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 case 0:
580 /* success */
581 break;
582 case -ECONNRESET:
583 case -ENOENT:
584 case -ESHUTDOWN:
585 /* this urb is terminated, clean up */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700586 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 return;
588 default:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700589 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 goto exit;
591 }
592
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700593 dev = &edge_serial->serial->dev->dev;
594
Alan Cox03f0dbf2008-07-22 11:16:34 +0100595 /* process this interrupt-read even if there are no ports open */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if (length) {
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100597 usb_serial_debug_data(dev, __func__, length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 if (length > 1) {
600 bytes_avail = data[0] | (data[1] << 8);
601 if (bytes_avail) {
602 spin_lock(&edge_serial->es_lock);
603 edge_serial->rxBytesAvail += bytes_avail;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700604 dev_dbg(dev,
605 "%s - bytes_avail=%d, rxBytesAvail=%d, read_in_progress=%d\n",
606 __func__, bytes_avail,
607 edge_serial->rxBytesAvail,
608 edge_serial->read_in_progress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 if (edge_serial->rxBytesAvail > 0 &&
611 !edge_serial->read_in_progress) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700612 dev_dbg(dev, "%s - posting a read\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100613 edge_serial->read_in_progress = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Alan Cox03f0dbf2008-07-22 11:16:34 +0100615 /* we have pending bytes on the
616 bulk in pipe, send a request */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 result = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
618 if (result) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700619 dev_err(dev,
620 "%s - usb_submit_urb(read bulk) failed with result = %d\n",
621 __func__, result);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100622 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
624 }
625 spin_unlock(&edge_serial->es_lock);
626 }
627 }
628 /* grab the txcredits for the ports if available */
629 position = 2;
630 portNumber = 0;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100631 while ((position < length) &&
632 (portNumber < edge_serial->serial->num_ports)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 txCredits = data[position] | (data[position+1] << 8);
634 if (txCredits) {
635 port = edge_serial->serial->port[portNumber];
636 edge_port = usb_get_serial_port_data(port);
637 if (edge_port->open) {
638 spin_lock(&edge_port->ep_lock);
639 edge_port->txCredits += txCredits;
640 spin_unlock(&edge_port->ep_lock);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700641 dev_dbg(dev, "%s - txcredits for port%d = %d\n",
642 __func__, portNumber,
643 edge_port->txCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Alan Cox03f0dbf2008-07-22 11:16:34 +0100645 /* tell the tty driver that something
646 has changed */
Alan Cox4a90f092008-10-13 10:39:46 +0100647 tty = tty_port_tty_get(
648 &edge_port->port->port);
649 if (tty) {
650 tty_wakeup(tty);
651 tty_kref_put(tty);
652 }
Alan Cox03f0dbf2008-07-22 11:16:34 +0100653 /* Since we have more credit, check
654 if more data can be sent */
655 send_more_port_data(edge_serial,
656 edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 }
658 }
659 position += 2;
660 ++portNumber;
661 }
662 }
663
664exit:
Alan Cox03f0dbf2008-07-22 11:16:34 +0100665 result = usb_submit_urb(urb, GFP_ATOMIC);
666 if (result)
667 dev_err(&urb->dev->dev,
668 "%s - Error %d submitting control urb\n",
669 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670}
671
672
673/*****************************************************************************
674 * edge_bulk_in_callback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100675 * this is the callback function for when we have received data on the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 * bulk in endpoint.
677 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100678static void edge_bulk_in_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
Ming Leicdc97792008-02-24 18:41:47 +0800680 struct edgeport_serial *edge_serial = urb->context;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700681 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700683 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 __u16 raw_data_length;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700685 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700687 if (status) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700688 dev_dbg(&urb->dev->dev, "%s - nonzero read bulk status received: %d\n",
689 __func__, status);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100690 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return;
692 }
693
694 if (urb->actual_length == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700695 dev_dbg(&urb->dev->dev, "%s - read bulk callback with no data\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100696 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return;
698 }
699
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700700 dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 raw_data_length = urb->actual_length;
702
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100703 usb_serial_debug_data(dev, __func__, raw_data_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 spin_lock(&edge_serial->es_lock);
706
707 /* decrement our rxBytes available by the number that we just got */
708 edge_serial->rxBytesAvail -= raw_data_length;
709
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700710 dev_dbg(dev, "%s - Received = %d, rxBytesAvail %d\n", __func__,
711 raw_data_length, edge_serial->rxBytesAvail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Alan Cox03f0dbf2008-07-22 11:16:34 +0100713 process_rcvd_data(edge_serial, data, urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 /* check to see if there's any more data for us to read */
716 if (edge_serial->rxBytesAvail > 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700717 dev_dbg(dev, "%s - posting a read\n", __func__);
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700718 retval = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
719 if (retval) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700720 dev_err(dev,
721 "%s - usb_submit_urb(read bulk) failed, retval = %d\n",
722 __func__, retval);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100723 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
725 } else {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100726 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 }
728
729 spin_unlock(&edge_serial->es_lock);
730}
731
732
733/*****************************************************************************
734 * edge_bulk_out_data_callback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100735 * this is the callback function for when we have finished sending
736 * serial data on the bulk out endpoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100738static void edge_bulk_out_data_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Ming Leicdc97792008-02-24 18:41:47 +0800740 struct edgeport_port *edge_port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 struct tty_struct *tty;
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700742 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Greg Kroah-Hartman2a393f52007-06-15 15:44:13 -0700744 if (status) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700745 dev_dbg(&urb->dev->dev,
746 "%s - nonzero write bulk status received: %d\n",
747 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 }
749
Alan Cox4a90f092008-10-13 10:39:46 +0100750 tty = tty_port_tty_get(&edge_port->port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 if (tty && edge_port->open) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100753 /* let the tty driver wakeup if it has a special
754 write_wakeup function */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 tty_wakeup(tty);
756 }
Alan Cox4a90f092008-10-13 10:39:46 +0100757 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Alan Cox03f0dbf2008-07-22 11:16:34 +0100759 /* Release the Write URB */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100760 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Alan Cox03f0dbf2008-07-22 11:16:34 +0100762 /* Check if more data needs to be sent */
763 send_more_port_data((struct edgeport_serial *)
764 (usb_get_serial_data(edge_port->port->serial)), edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
766
767
768/*****************************************************************************
769 * BulkOutCmdCallback
Alan Cox03f0dbf2008-07-22 11:16:34 +0100770 * this is the callback function for when we have finished sending a
771 * command on the bulk out endpoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +0100773static void edge_bulk_out_cmd_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
Ming Leicdc97792008-02-24 18:41:47 +0800775 struct edgeport_port *edge_port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 struct tty_struct *tty;
777 int status = urb->status;
778
Oliver Neukum96c706e2007-03-15 15:27:17 +0100779 atomic_dec(&CmdUrbs);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700780 dev_dbg(&urb->dev->dev, "%s - FREE URB %p (outstanding %d)\n",
781 __func__, urb, atomic_read(&CmdUrbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783
784 /* clean up the transfer buffer */
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700785 kfree(urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 /* Free the command urb */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100788 usb_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 if (status) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700791 dev_dbg(&urb->dev->dev,
792 "%s - nonzero write bulk status received: %d\n",
793 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 return;
795 }
796
797 /* Get pointer to tty */
Alan Cox4a90f092008-10-13 10:39:46 +0100798 tty = tty_port_tty_get(&edge_port->port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 /* tell the tty driver that something has changed */
801 if (tty && edge_port->open)
802 tty_wakeup(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100803 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 /* we have completed the command */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100806 edge_port->commandPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 wake_up(&edge_port->wait_command);
808}
809
810
811/*****************************************************************************
812 * Driver tty interface functions
813 *****************************************************************************/
814
815/*****************************************************************************
816 * SerialOpen
817 * this function is called by the tty driver when a port is opened
818 * If successful, we return 0
819 * Otherwise we return a negative error number.
820 *****************************************************************************/
Alan Coxa509a7e2009-09-19 13:13:26 -0700821static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
823 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700824 struct device *dev = &port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 struct usb_serial *serial;
826 struct edgeport_serial *edge_serial;
827 int response;
828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 if (edge_port == NULL)
830 return -ENODEV;
831
Alan Cox03f0dbf2008-07-22 11:16:34 +0100832 /* see if we've set up our endpoint info yet (can't set it up
833 in edge_startup as the structures were not set up at that time.) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 serial = port->serial;
835 edge_serial = usb_get_serial_data(serial);
Alan Cox95da3102008-07-22 11:09:07 +0100836 if (edge_serial == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (edge_serial->interrupt_in_buffer == NULL) {
839 struct usb_serial_port *port0 = serial->port[0];
Alan Cox03f0dbf2008-07-22 11:16:34 +0100840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 /* not set up yet, so do it now */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100842 edge_serial->interrupt_in_buffer =
843 port0->interrupt_in_buffer;
844 edge_serial->interrupt_in_endpoint =
845 port0->interrupt_in_endpointAddress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 edge_serial->interrupt_read_urb = port0->interrupt_in_urb;
847 edge_serial->bulk_in_buffer = port0->bulk_in_buffer;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100848 edge_serial->bulk_in_endpoint =
849 port0->bulk_in_endpointAddress;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 edge_serial->read_urb = port0->read_urb;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100851 edge_serial->bulk_out_endpoint =
852 port0->bulk_out_endpointAddress;
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 /* set up our interrupt urb */
855 usb_fill_int_urb(edge_serial->interrupt_read_urb,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100856 serial->dev,
857 usb_rcvintpipe(serial->dev,
858 port0->interrupt_in_endpointAddress),
859 port0->interrupt_in_buffer,
860 edge_serial->interrupt_read_urb->transfer_buffer_length,
861 edge_interrupt_callback, edge_serial,
862 edge_serial->interrupt_read_urb->interval);
863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 /* set up our bulk in urb */
865 usb_fill_bulk_urb(edge_serial->read_urb, serial->dev,
Alan Cox03f0dbf2008-07-22 11:16:34 +0100866 usb_rcvbulkpipe(serial->dev,
867 port0->bulk_in_endpointAddress),
868 port0->bulk_in_buffer,
869 edge_serial->read_urb->transfer_buffer_length,
870 edge_bulk_in_callback, edge_serial);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100871 edge_serial->read_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873 /* start interrupt read for this edgeport
Alan Cox03f0dbf2008-07-22 11:16:34 +0100874 * this interrupt will continue as long
875 * as the edgeport is connected */
876 response = usb_submit_urb(edge_serial->interrupt_read_urb,
877 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 if (response) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700879 dev_err(dev, "%s - Error %d submitting control urb\n",
880 __func__, response);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 }
882 }
Alan Cox03f0dbf2008-07-22 11:16:34 +0100883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 /* initialize our wait queues */
885 init_waitqueue_head(&edge_port->wait_open);
886 init_waitqueue_head(&edge_port->wait_chase);
887 init_waitqueue_head(&edge_port->delta_msr_wait);
888 init_waitqueue_head(&edge_port->wait_command);
889
890 /* initialize our icount structure */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100891 memset(&(edge_port->icount), 0x00, sizeof(edge_port->icount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 /* initialize our port settings */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100894 edge_port->txCredits = 0; /* Can't send any data yet */
895 /* Must always set this bit to enable ints! */
896 edge_port->shadowMCR = MCR_MASTER_IE;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100897 edge_port->chaseResponsePending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899 /* send a open port command */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100900 edge_port->openPending = true;
901 edge_port->open = false;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100902 response = send_iosp_ext_cmd(edge_port, IOSP_CMD_OPEN_PORT, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 if (response < 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700905 dev_err(dev, "%s - error sending open port command\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100906 edge_port->openPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 return -ENODEV;
908 }
909
910 /* now wait for the port to be completely opened */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100911 wait_event_timeout(edge_port->wait_open, !edge_port->openPending,
912 OPEN_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100914 if (!edge_port->open) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 /* open timed out */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700916 dev_dbg(dev, "%s - open timedout\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100917 edge_port->openPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 return -ENODEV;
919 }
920
921 /* create the txfifo */
922 edge_port->txfifo.head = 0;
923 edge_port->txfifo.tail = 0;
924 edge_port->txfifo.count = 0;
925 edge_port->txfifo.size = edge_port->maxTxCredits;
Alan Cox03f0dbf2008-07-22 11:16:34 +0100926 edge_port->txfifo.fifo = kmalloc(edge_port->maxTxCredits, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 if (!edge_port->txfifo.fifo) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700929 dev_dbg(dev, "%s - no memory\n", __func__);
Alan Cox335f8512009-06-11 12:26:29 +0100930 edge_close(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return -ENOMEM;
932 }
933
934 /* Allocate a URB for the write */
Alan Cox03f0dbf2008-07-22 11:16:34 +0100935 edge_port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100936 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
938 if (!edge_port->write_urb) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700939 dev_dbg(dev, "%s - no memory\n", __func__);
Alan Cox335f8512009-06-11 12:26:29 +0100940 edge_close(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 return -ENOMEM;
942 }
943
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700944 dev_dbg(dev, "%s(%d) - Initialize TX fifo to %d bytes\n",
945 __func__, port->number, edge_port->maxTxCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 return 0;
948}
949
950
951/************************************************************************
952 *
953 * block_until_chase_response
954 *
955 * This function will block the close until one of the following:
956 * 1. Response to our Chase comes from Edgeport
Joe Perchesdc0d5c12007-12-17 11:40:18 -0800957 * 2. A timeout of 10 seconds without activity has expired
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 * (1K of Edgeport data @ 2400 baud ==> 4 sec to empty)
959 *
960 ************************************************************************/
961static void block_until_chase_response(struct edgeport_port *edge_port)
962{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700963 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 DEFINE_WAIT(wait);
965 __u16 lastCredits;
966 int timeout = 1*HZ;
967 int loop = 10;
968
969 while (1) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100970 /* Save Last credits */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 lastCredits = edge_port->txCredits;
972
Alan Cox03f0dbf2008-07-22 11:16:34 +0100973 /* Did we get our Chase response */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100974 if (!edge_port->chaseResponsePending) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700975 dev_dbg(dev, "%s - Got Chase Response\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Alan Cox03f0dbf2008-07-22 11:16:34 +0100977 /* did we get all of our credit back? */
978 if (edge_port->txCredits == edge_port->maxTxCredits) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700979 dev_dbg(dev, "%s - Got all credits\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 return;
981 }
982 }
983
Alan Cox03f0dbf2008-07-22 11:16:34 +0100984 /* Block the thread for a while */
985 prepare_to_wait(&edge_port->wait_chase, &wait,
986 TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 schedule_timeout(timeout);
988 finish_wait(&edge_port->wait_chase, &wait);
989
990 if (lastCredits == edge_port->txCredits) {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100991 /* No activity.. count down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 loop--;
993 if (loop == 0) {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +0100994 edge_port->chaseResponsePending = false;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -0700995 dev_dbg(dev, "%s - Chase TIMEOUT\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 return;
997 }
998 } else {
Alan Cox03f0dbf2008-07-22 11:16:34 +0100999 /* Reset timeout value back to 10 seconds */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001000 dev_dbg(dev, "%s - Last %d, Current %d\n", __func__,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001001 lastCredits, edge_port->txCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 loop = 10;
1003 }
1004 }
1005}
1006
1007
1008/************************************************************************
1009 *
1010 * block_until_tx_empty
1011 *
1012 * This function will block the close until one of the following:
1013 * 1. TX count are 0
1014 * 2. The edgeport has stopped
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001015 * 3. A timeout of 3 seconds without activity has expired
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 *
1017 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001018static void block_until_tx_empty(struct edgeport_port *edge_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001020 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 DEFINE_WAIT(wait);
1022 struct TxFifo *fifo = &edge_port->txfifo;
1023 __u32 lastCount;
1024 int timeout = HZ/10;
1025 int loop = 30;
1026
1027 while (1) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001028 /* Save Last count */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 lastCount = fifo->count;
1030
Alan Cox03f0dbf2008-07-22 11:16:34 +01001031 /* Is the Edgeport Buffer empty? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 if (lastCount == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001033 dev_dbg(dev, "%s - TX Buffer Empty\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 return;
1035 }
1036
Alan Cox03f0dbf2008-07-22 11:16:34 +01001037 /* Block the thread for a while */
1038 prepare_to_wait(&edge_port->wait_chase, &wait,
1039 TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 schedule_timeout(timeout);
1041 finish_wait(&edge_port->wait_chase, &wait);
1042
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001043 dev_dbg(dev, "%s wait\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045 if (lastCount == fifo->count) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001046 /* No activity.. count down. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 loop--;
1048 if (loop == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001049 dev_dbg(dev, "%s - TIMEOUT\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 return;
1051 }
1052 } else {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001053 /* Reset timeout value back to seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 loop = 30;
1055 }
1056 }
1057}
1058
1059
1060/*****************************************************************************
1061 * edge_close
1062 * this function is called by the tty driver when a port is closed
1063 *****************************************************************************/
Alan Cox335f8512009-06-11 12:26:29 +01001064static void edge_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
1066 struct edgeport_serial *edge_serial;
1067 struct edgeport_port *edge_port;
1068 int status;
1069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 edge_serial = usb_get_serial_data(port->serial);
1071 edge_port = usb_get_serial_port_data(port);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001072 if (edge_serial == NULL || edge_port == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 return;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001074
1075 /* block until tx is empty */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 block_until_tx_empty(edge_port);
1077
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001078 edge_port->closePending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001080 if ((!edge_serial->is_epic) ||
1081 ((edge_serial->is_epic) &&
1082 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1083 /* flush and chase */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001084 edge_port->chaseResponsePending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001086 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001087 status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
1088 if (status == 0)
1089 /* block until chase finished */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001090 block_until_chase_response(edge_port);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001091 else
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001092 edge_port->chaseResponsePending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 }
1094
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001095 if ((!edge_serial->is_epic) ||
1096 ((edge_serial->is_epic) &&
1097 (edge_serial->epic_descriptor.Supports.IOSPClose))) {
1098 /* close the port */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001099 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CLOSE_PORT\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001100 send_iosp_ext_cmd(edge_port, IOSP_CMD_CLOSE_PORT, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Alan Cox03f0dbf2008-07-22 11:16:34 +01001103 /* port->close = true; */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001104 edge_port->closePending = false;
1105 edge_port->open = false;
1106 edge_port->openPending = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Mariusz Kozlowski9a25f442006-11-08 15:36:29 +01001108 usb_kill_urb(edge_port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
1110 if (edge_port->write_urb) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001111 /* if this urb had a transfer buffer already
1112 (old transfer) free it */
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001113 kfree(edge_port->write_urb->transfer_buffer);
1114 usb_free_urb(edge_port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 edge_port->write_urb = NULL;
1116 }
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001117 kfree(edge_port->txfifo.fifo);
1118 edge_port->txfifo.fifo = NULL;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001119}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121/*****************************************************************************
1122 * SerialWrite
Alan Cox03f0dbf2008-07-22 11:16:34 +01001123 * this function is called by the tty driver when data should be written
1124 * to the port.
1125 * If successful, we return the number of bytes written, otherwise we
1126 * return a negative error number.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001128static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
1129 const unsigned char *data, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
1131 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1132 struct TxFifo *fifo;
1133 int copySize;
1134 int bytesleft;
1135 int firsthalf;
1136 int secondhalf;
1137 unsigned long flags;
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (edge_port == NULL)
1140 return -ENODEV;
1141
Alan Cox03f0dbf2008-07-22 11:16:34 +01001142 /* get a pointer to the Tx fifo */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 fifo = &edge_port->txfifo;
1144
1145 spin_lock_irqsave(&edge_port->ep_lock, flags);
1146
Alan Cox03f0dbf2008-07-22 11:16:34 +01001147 /* calculate number of bytes to put in fifo */
1148 copySize = min((unsigned int)count,
1149 (edge_port->txCredits - fifo->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001151 dev_dbg(&port->dev, "%s(%d) of %d byte(s) Fifo room %d -- will copy %d bytes\n",
1152 __func__, port->number, count,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001153 edge_port->txCredits - fifo->count, copySize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Alan Cox03f0dbf2008-07-22 11:16:34 +01001155 /* catch writes of 0 bytes which the tty driver likes to give us,
1156 and when txCredits is empty */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 if (copySize == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001158 dev_dbg(&port->dev, "%s - copySize = Zero\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 goto finish_write;
1160 }
1161
Alan Cox03f0dbf2008-07-22 11:16:34 +01001162 /* queue the data
1163 * since we can never overflow the buffer we do not have to check for a
1164 * full condition
1165 *
1166 * the copy is done is two parts -- first fill to the end of the buffer
1167 * then copy the reset from the start of the buffer
1168 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 bytesleft = fifo->size - fifo->head;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001170 firsthalf = min(bytesleft, copySize);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001171 dev_dbg(&port->dev, "%s - copy %d bytes of %d into fifo \n", __func__,
1172 firsthalf, bytesleft);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
1174 /* now copy our data */
1175 memcpy(&fifo->fifo[fifo->head], data, firsthalf);
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001176 usb_serial_debug_data(&port->dev, __func__, firsthalf, &fifo->fifo[fifo->head]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Alan Cox03f0dbf2008-07-22 11:16:34 +01001178 /* update the index and size */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 fifo->head += firsthalf;
1180 fifo->count += firsthalf;
1181
Alan Cox03f0dbf2008-07-22 11:16:34 +01001182 /* wrap the index */
1183 if (fifo->head == fifo->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 fifo->head = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186 secondhalf = copySize-firsthalf;
1187
1188 if (secondhalf) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001189 dev_dbg(&port->dev, "%s - copy rest of data %d\n", __func__, secondhalf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 memcpy(&fifo->fifo[fifo->head], &data[firsthalf], secondhalf);
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001191 usb_serial_debug_data(&port->dev, __func__, secondhalf, &fifo->fifo[fifo->head]);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001192 /* update the index and size */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 fifo->count += secondhalf;
1194 fifo->head += secondhalf;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001195 /* No need to check for wrap since we can not get to end of
1196 * the fifo in this part
1197 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 }
1199
1200finish_write:
1201 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1202
Alan Cox03f0dbf2008-07-22 11:16:34 +01001203 send_more_port_data((struct edgeport_serial *)
1204 usb_get_serial_data(port->serial), edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001206 dev_dbg(&port->dev, "%s wrote %d byte(s) TxCredits %d, Fifo %d\n",
1207 __func__, copySize, edge_port->txCredits, fifo->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Alan Cox03f0dbf2008-07-22 11:16:34 +01001209 return copySize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210}
1211
1212
1213/************************************************************************
1214 *
1215 * send_more_port_data()
1216 *
1217 * This routine attempts to write additional UART transmit data
1218 * to a port over the USB bulk pipe. It is called (1) when new
1219 * data has been written to a port's TxBuffer from higher layers
1220 * (2) when the peripheral sends us additional TxCredits indicating
1221 * that it can accept more Tx data for a given port; and (3) when
1222 * a bulk write completes successfully and we want to see if we
1223 * can transmit more.
1224 *
1225 ************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001226static void send_more_port_data(struct edgeport_serial *edge_serial,
1227 struct edgeport_port *edge_port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
1229 struct TxFifo *fifo = &edge_port->txfifo;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001230 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 struct urb *urb;
1232 unsigned char *buffer;
1233 int status;
1234 int count;
1235 int bytesleft;
1236 int firsthalf;
1237 int secondhalf;
1238 unsigned long flags;
1239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 spin_lock_irqsave(&edge_port->ep_lock, flags);
1241
1242 if (edge_port->write_in_progress ||
1243 !edge_port->open ||
1244 (fifo->count == 0)) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001245 dev_dbg(dev, "%s(%d) EXIT - fifo %d, PendingWrite = %d\n",
1246 __func__, edge_port->port->number,
1247 fifo->count, edge_port->write_in_progress);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 goto exit_send;
1249 }
1250
Alan Cox03f0dbf2008-07-22 11:16:34 +01001251 /* since the amount of data in the fifo will always fit into the
1252 * edgeport buffer we do not need to check the write length
1253 *
1254 * Do we have enough credits for this port to make it worthwhile
1255 * to bother queueing a write. If it's too small, say a few bytes,
1256 * it's better to wait for more credits so we can do a larger write.
1257 */
1258 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 -07001259 dev_dbg(dev, "%s(%d) Not enough credit - fifo %d TxCredit %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01001260 __func__, edge_port->port->number, fifo->count,
1261 edge_port->txCredits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 goto exit_send;
1263 }
1264
Alan Cox03f0dbf2008-07-22 11:16:34 +01001265 /* lock this write */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001266 edge_port->write_in_progress = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Alan Cox03f0dbf2008-07-22 11:16:34 +01001268 /* get a pointer to the write_urb */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 urb = edge_port->write_urb;
1270
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001271 /* make sure transfer buffer is freed */
1272 kfree(urb->transfer_buffer);
1273 urb->transfer_buffer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Alan Cox03f0dbf2008-07-22 11:16:34 +01001275 /* build the data header for the buffer and port that we are about
1276 to send out */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 count = fifo->count;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001278 buffer = kmalloc(count+2, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 if (buffer == NULL) {
Johan Hovold22a416c2012-02-10 13:20:51 +01001280 dev_err_console(edge_port->port,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001281 "%s - no more kernel memory...\n", __func__);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001282 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 goto exit_send;
1284 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01001285 buffer[0] = IOSP_BUILD_DATA_HDR1(edge_port->port->number
1286 - edge_port->port->serial->minor, count);
1287 buffer[1] = IOSP_BUILD_DATA_HDR2(edge_port->port->number
1288 - edge_port->port->serial->minor, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
1290 /* now copy our data */
1291 bytesleft = fifo->size - fifo->tail;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001292 firsthalf = min(bytesleft, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 memcpy(&buffer[2], &fifo->fifo[fifo->tail], firsthalf);
1294 fifo->tail += firsthalf;
1295 fifo->count -= firsthalf;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001296 if (fifo->tail == fifo->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 fifo->tail = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299 secondhalf = count-firsthalf;
1300 if (secondhalf) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001301 memcpy(&buffer[2+firsthalf], &fifo->fifo[fifo->tail],
1302 secondhalf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 fifo->tail += secondhalf;
1304 fifo->count -= secondhalf;
1305 }
1306
1307 if (count)
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001308 usb_serial_debug_data(&edge_port->port->dev, __func__, count, &buffer[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 /* fill up the urb with all of our data and submit it */
Alan Cox03f0dbf2008-07-22 11:16:34 +01001311 usb_fill_bulk_urb(urb, edge_serial->serial->dev,
1312 usb_sndbulkpipe(edge_serial->serial->dev,
1313 edge_serial->bulk_out_endpoint),
1314 buffer, count+2,
1315 edge_bulk_out_data_callback, edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 /* decrement the number of credits we have by the number we just sent */
1318 edge_port->txCredits -= count;
1319 edge_port->icount.tx += count;
1320
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 status = usb_submit_urb(urb, GFP_ATOMIC);
1322 if (status) {
1323 /* something went wrong */
Johan Hovold22a416c2012-02-10 13:20:51 +01001324 dev_err_console(edge_port->port,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001325 "%s - usb_submit_urb(write bulk) failed, status = %d, data lost\n",
1326 __func__, status);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001327 edge_port->write_in_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329 /* revert the credits as something bad happened. */
1330 edge_port->txCredits += count;
1331 edge_port->icount.tx -= count;
1332 }
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001333 dev_dbg(dev, "%s wrote %d byte(s) TxCredit %d, Fifo %d\n",
1334 __func__, count, edge_port->txCredits, fifo->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
1336exit_send:
1337 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1338}
1339
1340
1341/*****************************************************************************
1342 * edge_write_room
Alan Cox03f0dbf2008-07-22 11:16:34 +01001343 * this function is called by the tty driver when it wants to know how
1344 * many bytes of data we can accept for a specific port. If successful,
1345 * we return the amount of room that we have for this port (the txCredits)
1346 * otherwise we return a negative error number.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001348static int edge_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
Alan Cox95da3102008-07-22 11:09:07 +01001350 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1352 int room;
1353 unsigned long flags;
1354
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 if (edge_port == NULL)
Alan Coxd76f2f42008-07-22 11:16:42 +01001356 return 0;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001357 if (edge_port->closePending)
Alan Coxd76f2f42008-07-22 11:16:42 +01001358 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001361 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Alan Coxd76f2f42008-07-22 11:16:42 +01001362 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 }
1364
Alan Cox03f0dbf2008-07-22 11:16:34 +01001365 /* total of both buffers is still txCredit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 spin_lock_irqsave(&edge_port->ep_lock, flags);
1367 room = edge_port->txCredits - edge_port->txfifo.count;
1368 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1369
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001370 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 return room;
1372}
1373
1374
1375/*****************************************************************************
1376 * edge_chars_in_buffer
Alan Cox03f0dbf2008-07-22 11:16:34 +01001377 * this function is called by the tty driver when it wants to know how
1378 * many bytes of data we currently have outstanding in the port (data that
1379 * has been written, but hasn't made it out the port yet)
1380 * If successful, we return the number of bytes left to be written in the
1381 * system,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 * Otherwise we return a negative error number.
1383 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001384static int edge_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
Alan Cox95da3102008-07-22 11:09:07 +01001386 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1388 int num_chars;
1389 unsigned long flags;
1390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 if (edge_port == NULL)
Alan Coxd76f2f42008-07-22 11:16:42 +01001392 return 0;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001393 if (edge_port->closePending)
Alan Coxd76f2f42008-07-22 11:16:42 +01001394 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001397 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Alan Coxd76f2f42008-07-22 11:16:42 +01001398 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 }
1400
1401 spin_lock_irqsave(&edge_port->ep_lock, flags);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001402 num_chars = edge_port->maxTxCredits - edge_port->txCredits +
1403 edge_port->txfifo.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1405 if (num_chars) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001406 dev_dbg(&port->dev, "%s(port %d) - returns %d\n", __func__,
1407 port->number, num_chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 }
1409
1410 return num_chars;
1411}
1412
1413
1414/*****************************************************************************
1415 * SerialThrottle
1416 * this function is called by the tty driver when it wants to stop the data
1417 * being read from the port.
1418 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001419static void edge_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
Alan Cox95da3102008-07-22 11:09:07 +01001421 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 int status;
1424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 if (edge_port == NULL)
1426 return;
1427
1428 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001429 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 return;
1431 }
1432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 /* if we are implementing XON/XOFF, send the stop character */
1434 if (I_IXOFF(tty)) {
1435 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001436 status = edge_write(tty, port, &stop_char, 1);
1437 if (status <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 }
1440
1441 /* if we are implementing RTS/CTS, toggle that line */
Alan Coxadc8d742012-07-14 15:31:47 +01001442 if (tty->termios.c_cflag & CRTSCTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 edge_port->shadowMCR &= ~MCR_RTS;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001444 status = send_cmd_write_uart_register(edge_port, MCR,
1445 edge_port->shadowMCR);
1446 if (status != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449}
1450
1451
1452/*****************************************************************************
1453 * edge_unthrottle
Alan Cox03f0dbf2008-07-22 11:16:34 +01001454 * this function is called by the tty driver when it wants to resume the
1455 * data being read from the port (called after SerialThrottle is called)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001457static void edge_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458{
Alan Cox95da3102008-07-22 11:09:07 +01001459 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 int status;
1462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 if (edge_port == NULL)
1464 return;
1465
1466 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001467 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 return;
1469 }
1470
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 /* if we are implementing XON/XOFF, send the start character */
1472 if (I_IXOFF(tty)) {
1473 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001474 status = edge_write(tty, port, &start_char, 1);
1475 if (status <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 /* if we are implementing RTS/CTS, toggle that line */
Alan Coxadc8d742012-07-14 15:31:47 +01001479 if (tty->termios.c_cflag & CRTSCTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 edge_port->shadowMCR |= MCR_RTS;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001481 send_cmd_write_uart_register(edge_port, MCR,
1482 edge_port->shadowMCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484}
1485
1486
1487/*****************************************************************************
1488 * SerialSetTermios
Alan Cox03f0dbf2008-07-22 11:16:34 +01001489 * this function is called by the tty driver when it wants to change
1490 * the termios structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001492static void edge_set_termios(struct tty_struct *tty,
1493 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494{
1495 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 unsigned int cflag;
1497
Alan Coxadc8d742012-07-14 15:31:47 +01001498 cflag = tty->termios.c_cflag;
Linus Torvaldsd9a80742012-10-01 13:23:01 -07001499 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 -07001500 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 -07001501
1502 if (edge_port == NULL)
1503 return;
1504
1505 if (!edge_port->open) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001506 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 return;
1508 }
1509
1510 /* change the port settings to the new ones specified */
Alan Cox95da3102008-07-22 11:09:07 +01001511 change_port_settings(tty, edge_port, old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512}
1513
1514
1515/*****************************************************************************
1516 * get_lsr_info - get line status register info
1517 *
1518 * Purpose: Let user call ioctl() to get info when the UART physically
1519 * is emptied. On bus types like RS485, the transmitter must
1520 * release the bus after transmitting. This must be done when
1521 * the transmit shift register is empty, not be done when the
1522 * transmit holding register is empty. This functionality
Alan Cox03f0dbf2008-07-22 11:16:34 +01001523 * allows an RS485 driver to be written in user space.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001525static int get_lsr_info(struct edgeport_port *edge_port,
1526 unsigned int __user *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527{
1528 unsigned int result = 0;
1529 unsigned long flags;
1530
1531 spin_lock_irqsave(&edge_port->ep_lock, flags);
1532 if (edge_port->maxTxCredits == edge_port->txCredits &&
1533 edge_port->txfifo.count == 0) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001534 dev_dbg(&edge_port->port->dev, "%s -- Empty\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 result = TIOCSER_TEMT;
1536 }
1537 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1538
1539 if (copy_to_user(value, &result, sizeof(int)))
1540 return -EFAULT;
1541 return 0;
1542}
1543
Alan Cox20b9d172011-02-14 16:26:50 +00001544static int edge_tiocmset(struct tty_struct *tty,
Alan Cox03f0dbf2008-07-22 11:16:34 +01001545 unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546{
Alan Cox95da3102008-07-22 11:09:07 +01001547 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1549 unsigned int mcr;
1550
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 mcr = edge_port->shadowMCR;
1552 if (set & TIOCM_RTS)
1553 mcr |= MCR_RTS;
1554 if (set & TIOCM_DTR)
1555 mcr |= MCR_DTR;
1556 if (set & TIOCM_LOOP)
1557 mcr |= MCR_LOOPBACK;
1558
1559 if (clear & TIOCM_RTS)
1560 mcr &= ~MCR_RTS;
1561 if (clear & TIOCM_DTR)
1562 mcr &= ~MCR_DTR;
1563 if (clear & TIOCM_LOOP)
1564 mcr &= ~MCR_LOOPBACK;
1565
1566 edge_port->shadowMCR = mcr;
1567
1568 send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1569
1570 return 0;
1571}
1572
Alan Cox60b33c12011-02-14 16:26:14 +00001573static int edge_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
Alan Cox95da3102008-07-22 11:09:07 +01001575 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1577 unsigned int result = 0;
1578 unsigned int msr;
1579 unsigned int mcr;
1580
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 msr = edge_port->shadowMSR;
1582 mcr = edge_port->shadowMCR;
1583 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
1584 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
1585 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
1586 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
1587 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
1588 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
1589
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 return result;
1591}
1592
Alan Cox0bca1b92010-09-16 18:21:40 +01001593static int edge_get_icount(struct tty_struct *tty,
1594 struct serial_icounter_struct *icount)
1595{
1596 struct usb_serial_port *port = tty->driver_data;
1597 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1598 struct async_icount cnow;
1599 cnow = edge_port->icount;
1600
1601 icount->cts = cnow.cts;
1602 icount->dsr = cnow.dsr;
1603 icount->rng = cnow.rng;
1604 icount->dcd = cnow.dcd;
1605 icount->rx = cnow.rx;
1606 icount->tx = cnow.tx;
1607 icount->frame = cnow.frame;
1608 icount->overrun = cnow.overrun;
1609 icount->parity = cnow.parity;
1610 icount->brk = cnow.brk;
1611 icount->buf_overrun = cnow.buf_overrun;
1612
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001613 dev_dbg(&port->dev, "%s (%d) TIOCGICOUNT RX=%d, TX=%d\n", __func__,
1614 port->number, icount->rx, icount->tx);
Alan Cox0bca1b92010-09-16 18:21:40 +01001615 return 0;
1616}
1617
Alan Cox03f0dbf2008-07-22 11:16:34 +01001618static int get_serial_info(struct edgeport_port *edge_port,
1619 struct serial_struct __user *retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
1621 struct serial_struct tmp;
1622
1623 if (!retinfo)
1624 return -EFAULT;
1625
1626 memset(&tmp, 0, sizeof(tmp));
1627
1628 tmp.type = PORT_16550A;
1629 tmp.line = edge_port->port->serial->minor;
1630 tmp.port = edge_port->port->number;
1631 tmp.irq = 0;
1632 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1633 tmp.xmit_fifo_size = edge_port->maxTxCredits;
1634 tmp.baud_base = 9600;
1635 tmp.close_delay = 5*HZ;
1636 tmp.closing_wait = 30*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
1638 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1639 return -EFAULT;
1640 return 0;
1641}
1642
1643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644/*****************************************************************************
1645 * SerialIoctl
1646 * this function handles any ioctl calls to the driver
1647 *****************************************************************************/
Alan Cox00a0d0d2011-02-14 16:27:06 +00001648static int edge_ioctl(struct tty_struct *tty,
Alan Cox95da3102008-07-22 11:09:07 +01001649 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650{
Alan Cox95da3102008-07-22 11:09:07 +01001651 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 DEFINE_WAIT(wait);
1653 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1654 struct async_icount cnow;
1655 struct async_icount cprev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001657 dev_dbg(&port->dev, "%s - port %d, cmd = 0x%x\n", __func__, port->number, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
1659 switch (cmd) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001660 case TIOCSERGETLSR:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001661 dev_dbg(&port->dev, "%s (%d) TIOCSERGETLSR\n", __func__, port->number);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001662 return get_lsr_info(edge_port, (unsigned int __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Alan Cox03f0dbf2008-07-22 11:16:34 +01001664 case TIOCGSERIAL:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001665 dev_dbg(&port->dev, "%s (%d) TIOCGSERIAL\n", __func__, port->number);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001666 return get_serial_info(edge_port, (struct serial_struct __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Alan Cox03f0dbf2008-07-22 11:16:34 +01001668 case TIOCMIWAIT:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001669 dev_dbg(&port->dev, "%s (%d) TIOCMIWAIT\n", __func__, port->number);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001670 cprev = edge_port->icount;
1671 while (1) {
1672 prepare_to_wait(&edge_port->delta_msr_wait,
1673 &wait, TASK_INTERRUPTIBLE);
1674 schedule();
1675 finish_wait(&edge_port->delta_msr_wait, &wait);
1676 /* see if a signal did it */
1677 if (signal_pending(current))
1678 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 cnow = edge_port->icount;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001680 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1681 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1682 return -EIO; /* no change => error */
1683 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1684 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1685 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1686 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1687 return 0;
1688 }
1689 cprev = cnow;
1690 }
1691 /* NOTREACHED */
1692 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 return -ENOIOCTLCMD;
1696}
1697
1698
1699/*****************************************************************************
1700 * SerialBreak
1701 * this function sends a break to the port
1702 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001703static void edge_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704{
Alan Cox95da3102008-07-22 11:09:07 +01001705 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001707 struct edgeport_serial *edge_serial = usb_get_serial_data(port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 int status;
1709
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001710 if ((!edge_serial->is_epic) ||
1711 ((edge_serial->is_epic) &&
1712 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1713 /* flush and chase */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001714 edge_port->chaseResponsePending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001716 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001717 status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001718 if (status == 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001719 /* block until chase finished */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001720 block_until_chase_response(edge_port);
1721 } else {
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001722 edge_port->chaseResponsePending = false;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 }
1725
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001726 if ((!edge_serial->is_epic) ||
1727 ((edge_serial->is_epic) &&
1728 (edge_serial->epic_descriptor.Supports.IOSPSetClrBreak))) {
1729 if (break_state == -1) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001730 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_SET_BREAK\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001731 status = send_iosp_ext_cmd(edge_port,
1732 IOSP_CMD_SET_BREAK, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001733 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001734 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CLEAR_BREAK\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001735 status = send_iosp_ext_cmd(edge_port,
1736 IOSP_CMD_CLEAR_BREAK, 0);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08001737 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01001738 if (status)
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001739 dev_dbg(&port->dev, "%s - error sending break set/clear command.\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01001740 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742}
1743
1744
1745/*****************************************************************************
1746 * process_rcvd_data
1747 * this function handles the data received on the bulk in pipe.
1748 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001749static void process_rcvd_data(struct edgeport_serial *edge_serial,
1750 unsigned char *buffer, __u16 bufferLength)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001752 struct device *dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 struct usb_serial_port *port;
1754 struct edgeport_port *edge_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 __u16 lastBufferLength;
1756 __u16 rxLen;
1757
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 lastBufferLength = bufferLength + 1;
1759
1760 while (bufferLength > 0) {
1761 /* failsafe incase we get a message that we don't understand */
1762 if (lastBufferLength == bufferLength) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001763 dev_dbg(dev, "%s - stuck in loop, exiting it.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 break;
1765 }
1766 lastBufferLength = bufferLength;
1767
1768 switch (edge_serial->rxState) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001769 case EXPECT_HDR1:
1770 edge_serial->rxHeader1 = *buffer;
1771 ++buffer;
1772 --bufferLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
Alan Cox03f0dbf2008-07-22 11:16:34 +01001774 if (bufferLength == 0) {
1775 edge_serial->rxState = EXPECT_HDR2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 break;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001777 }
1778 /* otherwise, drop on through */
1779 case EXPECT_HDR2:
1780 edge_serial->rxHeader2 = *buffer;
1781 ++buffer;
1782 --bufferLength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001784 dev_dbg(dev, "%s - Hdr1=%02X Hdr2=%02X\n", __func__,
1785 edge_serial->rxHeader1, edge_serial->rxHeader2);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001786 /* Process depending on whether this header is
1787 * data or status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Alan Cox03f0dbf2008-07-22 11:16:34 +01001789 if (IS_CMD_STAT_HDR(edge_serial->rxHeader1)) {
1790 /* Decode this status header and go to
1791 * EXPECT_HDR1 (if we can process the status
1792 * with only 2 bytes), or go to EXPECT_HDR3 to
1793 * get the third byte. */
1794 edge_serial->rxPort =
1795 IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1796 edge_serial->rxStatusCode =
1797 IOSP_GET_STATUS_CODE(
1798 edge_serial->rxHeader1);
1799
1800 if (!IOSP_STATUS_IS_2BYTE(
1801 edge_serial->rxStatusCode)) {
1802 /* This status needs additional bytes.
1803 * Save what we have and then wait for
1804 * more data.
1805 */
1806 edge_serial->rxStatusParam
1807 = edge_serial->rxHeader2;
1808 edge_serial->rxState = EXPECT_HDR3;
1809 break;
1810 }
1811 /* We have all the header bytes, process the
1812 status now */
1813 process_rcvd_status(edge_serial,
1814 edge_serial->rxHeader2, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 edge_serial->rxState = EXPECT_HDR1;
1816 break;
Alan Cox03f0dbf2008-07-22 11:16:34 +01001817 } else {
1818 edge_serial->rxPort =
1819 IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1820 edge_serial->rxBytesRemaining =
1821 IOSP_GET_HDR_DATA_LEN(
1822 edge_serial->rxHeader1,
1823 edge_serial->rxHeader2);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001824 dev_dbg(dev, "%s - Data for Port %u Len %u\n",
1825 __func__,
1826 edge_serial->rxPort,
1827 edge_serial->rxBytesRemaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Alan Cox03f0dbf2008-07-22 11:16:34 +01001829 /* ASSERT(DevExt->RxPort < DevExt->NumPorts);
1830 * ASSERT(DevExt->RxBytesRemaining <
1831 * IOSP_MAX_DATA_LENGTH);
1832 */
1833
1834 if (bufferLength == 0) {
1835 edge_serial->rxState = EXPECT_DATA;
1836 break;
1837 }
1838 /* Else, drop through */
1839 }
1840 case EXPECT_DATA: /* Expect data */
1841 if (bufferLength < edge_serial->rxBytesRemaining) {
1842 rxLen = bufferLength;
1843 /* Expect data to start next buffer */
1844 edge_serial->rxState = EXPECT_DATA;
1845 } else {
1846 /* BufLen >= RxBytesRemaining */
1847 rxLen = edge_serial->rxBytesRemaining;
1848 /* Start another header next time */
1849 edge_serial->rxState = EXPECT_HDR1;
1850 }
1851
1852 bufferLength -= rxLen;
1853 edge_serial->rxBytesRemaining -= rxLen;
1854
1855 /* spit this data back into the tty driver if this
1856 port is open */
1857 if (rxLen) {
1858 port = edge_serial->serial->port[
1859 edge_serial->rxPort];
1860 edge_port = usb_get_serial_port_data(port);
1861 if (edge_port->open) {
Jiri Slaby2e124b42013-01-03 15:53:06 +01001862 dev_dbg(dev, "%s - Sending %d bytes to TTY for port %d\n",
1863 __func__, rxLen,
1864 edge_serial->rxPort);
1865 edge_tty_recv(edge_port->port, buffer,
1866 rxLen);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001867 edge_port->icount.rx += rxLen;
1868 }
1869 buffer += rxLen;
1870 }
1871 break;
1872
1873 case EXPECT_HDR3: /* Expect 3rd byte of status header */
1874 edge_serial->rxHeader3 = *buffer;
1875 ++buffer;
1876 --bufferLength;
1877
1878 /* We have all the header bytes, process the
1879 status now */
1880 process_rcvd_status(edge_serial,
1881 edge_serial->rxStatusParam,
1882 edge_serial->rxHeader3);
1883 edge_serial->rxState = EXPECT_HDR1;
1884 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 }
1886 }
1887}
1888
1889
1890/*****************************************************************************
1891 * process_rcvd_status
Alan Cox03f0dbf2008-07-22 11:16:34 +01001892 * this function handles the any status messages received on the
1893 * bulk in pipe.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01001895static void process_rcvd_status(struct edgeport_serial *edge_serial,
1896 __u8 byte2, __u8 byte3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
1898 struct usb_serial_port *port;
1899 struct edgeport_port *edge_port;
Alan Cox4a90f092008-10-13 10:39:46 +01001900 struct tty_struct *tty;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001901 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 __u8 code = edge_serial->rxStatusCode;
1903
1904 /* switch the port pointer to the one being currently talked about */
1905 port = edge_serial->serial->port[edge_serial->rxPort];
1906 edge_port = usb_get_serial_port_data(port);
1907 if (edge_port == NULL) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001908 dev_err(&edge_serial->serial->dev->dev,
1909 "%s - edge_port == NULL for port %d\n",
1910 __func__, edge_serial->rxPort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 return;
1912 }
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001913 dev = &port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
1915 if (code == IOSP_EXT_STATUS) {
1916 switch (byte2) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001917 case IOSP_EXT_STATUS_CHASE_RSP:
1918 /* we want to do EXT status regardless of port
1919 * open/closed */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001920 dev_dbg(dev, "%s - Port %u EXT CHASE_RSP Data = %02x\n",
1921 __func__, edge_serial->rxPort, byte3);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001922 /* Currently, the only EXT_STATUS is Chase, so process
1923 * here instead of one more call to one more subroutine
1924 * If/when more EXT_STATUS, there'll be more work to do
1925 * Also, we currently clear flag and close the port
1926 * regardless of content of above's Byte3.
1927 * We could choose to do something else when Byte3 says
1928 * Timeout on Chase from Edgeport, like wait longer in
1929 * block_until_chase_response, but for now we don't.
1930 */
1931 edge_port->chaseResponsePending = false;
1932 wake_up(&edge_port->wait_chase);
1933 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Alan Cox03f0dbf2008-07-22 11:16:34 +01001935 case IOSP_EXT_STATUS_RX_CHECK_RSP:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001936 dev_dbg(dev, "%s ========== Port %u CHECK_RSP Sequence = %02x =============\n",
1937 __func__, edge_serial->rxPort, byte3);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001938 /* Port->RxCheckRsp = true; */
1939 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 }
1941 }
1942
1943 if (code == IOSP_STATUS_OPEN_RSP) {
1944 edge_port->txCredits = GET_TX_BUFFER_SIZE(byte3);
1945 edge_port->maxTxCredits = edge_port->txCredits;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001946 dev_dbg(dev, "%s - Port %u Open Response Initial MSR = %02x TxBufferSize = %d\n",
1947 __func__, edge_serial->rxPort, byte2, edge_port->txCredits);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001948 handle_new_msr(edge_port, byte2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Alan Cox03f0dbf2008-07-22 11:16:34 +01001950 /* send the current line settings to the port so we are
1951 in sync with any further termios calls */
Alan Cox4a90f092008-10-13 10:39:46 +01001952 tty = tty_port_tty_get(&edge_port->port->port);
1953 if (tty) {
1954 change_port_settings(tty,
Alan Coxadc8d742012-07-14 15:31:47 +01001955 edge_port, &tty->termios);
Alan Cox4a90f092008-10-13 10:39:46 +01001956 tty_kref_put(tty);
1957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
1959 /* we have completed the open */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01001960 edge_port->openPending = false;
1961 edge_port->open = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 wake_up(&edge_port->wait_open);
1963 return;
1964 }
1965
Alan Cox03f0dbf2008-07-22 11:16:34 +01001966 /* If port is closed, silently discard all rcvd status. We can
1967 * have cases where buffered status is received AFTER the close
1968 * port command is sent to the Edgeport.
1969 */
1970 if (!edge_port->open || edge_port->closePending)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
1973 switch (code) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01001974 /* Not currently sent by Edgeport */
1975 case IOSP_STATUS_LSR:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001976 dev_dbg(dev, "%s - Port %u LSR Status = %02x\n",
1977 __func__, edge_serial->rxPort, byte2);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001978 handle_new_lsr(edge_port, false, byte2, 0);
1979 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
Alan Cox03f0dbf2008-07-22 11:16:34 +01001981 case IOSP_STATUS_LSR_DATA:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001982 dev_dbg(dev, "%s - Port %u LSR Status = %02x, Data = %02x\n",
1983 __func__, edge_serial->rxPort, byte2, byte3);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001984 /* byte2 is LSR Register */
1985 /* byte3 is broken data byte */
1986 handle_new_lsr(edge_port, true, byte2, byte3);
1987 break;
1988 /*
1989 * case IOSP_EXT_4_STATUS:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001990 * dev_dbg(dev, "%s - Port %u LSR Status = %02x Data = %02x\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01001991 * __func__, edge_serial->rxPort, byte2, byte3);
1992 * break;
1993 */
1994 case IOSP_STATUS_MSR:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07001995 dev_dbg(dev, "%s - Port %u MSR Status = %02x\n",
1996 __func__, edge_serial->rxPort, byte2);
Alan Cox03f0dbf2008-07-22 11:16:34 +01001997 /*
1998 * Process this new modem status and generate appropriate
1999 * events, etc, based on the new status. This routine
2000 * also saves the MSR in Port->ShadowMsr.
2001 */
2002 handle_new_msr(edge_port, byte2);
2003 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
Alan Cox03f0dbf2008-07-22 11:16:34 +01002005 default:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002006 dev_dbg(dev, "%s - Unrecognized IOSP status code %u\n", __func__, code);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002007 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009}
2010
2011
2012/*****************************************************************************
2013 * edge_tty_recv
2014 * this function passes data on to the tty flip buffer
2015 *****************************************************************************/
Jiri Slaby2e124b42013-01-03 15:53:06 +01002016static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
2017 int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018{
2019 int cnt;
2020
Jiri Slaby05c7cd32013-01-03 15:53:04 +01002021 cnt = tty_insert_flip_string(&port->port, data, length);
Alan Coxa108bfc2010-02-18 16:44:01 +00002022 if (cnt < length) {
Jiri Slaby05c7cd32013-01-03 15:53:04 +01002023 dev_err(&port->dev, "%s - dropping data, %d bytes lost\n",
Alan Coxa108bfc2010-02-18 16:44:01 +00002024 __func__, length - cnt);
2025 }
2026 data += cnt;
2027 length -= cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
Jiri Slaby2e124b42013-01-03 15:53:06 +01002029 tty_flip_buffer_push(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030}
2031
2032
2033/*****************************************************************************
2034 * handle_new_msr
2035 * this function handles any change to the msr register for a port.
2036 *****************************************************************************/
2037static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr)
2038{
2039 struct async_icount *icount;
2040
Alan Cox03f0dbf2008-07-22 11:16:34 +01002041 if (newMsr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
2042 EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 icount = &edge_port->icount;
2044
2045 /* update input line counters */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002046 if (newMsr & EDGEPORT_MSR_DELTA_CTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 icount->cts++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002048 if (newMsr & EDGEPORT_MSR_DELTA_DSR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 icount->dsr++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002050 if (newMsr & EDGEPORT_MSR_DELTA_CD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 icount->dcd++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002052 if (newMsr & EDGEPORT_MSR_DELTA_RI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 icount->rng++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 wake_up_interruptible(&edge_port->delta_msr_wait);
2055 }
2056
2057 /* Save the new modem status */
2058 edge_port->shadowMSR = newMsr & 0xf0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059}
2060
2061
2062/*****************************************************************************
2063 * handle_new_lsr
2064 * this function handles any change to the lsr register for a port.
2065 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002066static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
2067 __u8 lsr, __u8 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002069 __u8 newLsr = (__u8) (lsr & (__u8)
2070 (LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
2071 struct async_icount *icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 edge_port->shadowLSR = lsr;
2074
2075 if (newLsr & LSR_BREAK) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002076 /*
2077 * Parity and Framing errors only count if they
2078 * occur exclusive of a break being
2079 * received.
2080 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 newLsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
2082 }
2083
2084 /* Place LSR data byte into Rx buffer */
Jiri Slaby2e124b42013-01-03 15:53:06 +01002085 if (lsrData)
2086 edge_tty_recv(edge_port->port, &data, 1);
2087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 /* update input line counters */
2089 icount = &edge_port->icount;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002090 if (newLsr & LSR_BREAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 icount->brk++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002092 if (newLsr & LSR_OVER_ERR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 icount->overrun++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002094 if (newLsr & LSR_PAR_ERR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 icount->parity++;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002096 if (newLsr & LSR_FRM_ERR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 icount->frame++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098}
2099
2100
2101/****************************************************************************
2102 * sram_write
Alan Cox03f0dbf2008-07-22 11:16:34 +01002103 * writes a number of bytes to the Edgeport device's sram starting at the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 * given address.
2105 * If successful returns the number of bytes written, otherwise it returns
2106 * a negative error number of the problem.
2107 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002108static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
2109 __u16 length, const __u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110{
2111 int result;
2112 __u16 current_length;
2113 unsigned char *transfer_buffer;
2114
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002115 dev_dbg(&serial->dev->dev, "%s - %x, %x, %d\n", __func__, extAddr, addr, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
Alan Cox03f0dbf2008-07-22 11:16:34 +01002117 transfer_buffer = kmalloc(64, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 if (!transfer_buffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002119 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
2120 __func__, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 return -ENOMEM;
2122 }
2123
2124 /* need to split these writes up into 64 byte chunks */
2125 result = 0;
2126 while (length > 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002127 if (length > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 current_length = 64;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002129 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 current_length = length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002131
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002132/* dev_dbg(&serial->dev->dev, "%s - writing %x, %x, %d\n", __func__, extAddr, addr, current_length); */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002133 memcpy(transfer_buffer, data, current_length);
2134 result = usb_control_msg(serial->dev,
2135 usb_sndctrlpipe(serial->dev, 0),
2136 USB_REQUEST_ION_WRITE_RAM,
2137 0x40, addr, extAddr, transfer_buffer,
2138 current_length, 300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 if (result < 0)
2140 break;
2141 length -= current_length;
2142 addr += current_length;
2143 data += current_length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
Alan Cox03f0dbf2008-07-22 11:16:34 +01002146 kfree(transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 return result;
2148}
2149
2150
2151/****************************************************************************
2152 * rom_write
2153 * writes a number of bytes to the Edgeport device's ROM starting at the
2154 * given address.
2155 * If successful returns the number of bytes written, otherwise it returns
2156 * a negative error number of the problem.
2157 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002158static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
2159 __u16 length, const __u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160{
2161 int result;
2162 __u16 current_length;
2163 unsigned char *transfer_buffer;
2164
Alan Cox03f0dbf2008-07-22 11:16:34 +01002165 transfer_buffer = kmalloc(64, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 if (!transfer_buffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002167 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
2168 __func__, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 return -ENOMEM;
2170 }
2171
2172 /* need to split these writes up into 64 byte chunks */
2173 result = 0;
2174 while (length > 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002175 if (length > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 current_length = 64;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002177 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 current_length = length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002179 memcpy(transfer_buffer, data, current_length);
2180 result = usb_control_msg(serial->dev,
2181 usb_sndctrlpipe(serial->dev, 0),
2182 USB_REQUEST_ION_WRITE_ROM, 0x40,
2183 addr, extAddr,
2184 transfer_buffer, current_length, 300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 if (result < 0)
2186 break;
2187 length -= current_length;
2188 addr += current_length;
2189 data += current_length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
Alan Cox03f0dbf2008-07-22 11:16:34 +01002192 kfree(transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 return result;
2194}
2195
2196
2197/****************************************************************************
2198 * rom_read
2199 * reads a number of bytes from the Edgeport device starting at the given
2200 * address.
2201 * If successful returns the number of bytes read, otherwise it returns
2202 * a negative error number of the problem.
2203 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002204static int rom_read(struct usb_serial *serial, __u16 extAddr,
2205 __u16 addr, __u16 length, __u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206{
2207 int result;
2208 __u16 current_length;
2209 unsigned char *transfer_buffer;
2210
Alan Cox03f0dbf2008-07-22 11:16:34 +01002211 transfer_buffer = kmalloc(64, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 if (!transfer_buffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002213 dev_err(&serial->dev->dev,
2214 "%s - kmalloc(%d) failed.\n", __func__, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 return -ENOMEM;
2216 }
2217
2218 /* need to split these reads up into 64 byte chunks */
2219 result = 0;
2220 while (length > 0) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002221 if (length > 64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 current_length = 64;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002223 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 current_length = length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002225 result = usb_control_msg(serial->dev,
2226 usb_rcvctrlpipe(serial->dev, 0),
2227 USB_REQUEST_ION_READ_ROM,
2228 0xC0, addr, extAddr, transfer_buffer,
2229 current_length, 300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 if (result < 0)
2231 break;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002232 memcpy(data, transfer_buffer, current_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 length -= current_length;
2234 addr += current_length;
2235 data += current_length;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
Alan Cox03f0dbf2008-07-22 11:16:34 +01002238 kfree(transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 return result;
2240}
2241
2242
2243/****************************************************************************
2244 * send_iosp_ext_cmd
2245 * Is used to send a IOSP message to the Edgeport device
2246 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002247static int send_iosp_ext_cmd(struct edgeport_port *edge_port,
2248 __u8 command, __u8 param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249{
2250 unsigned char *buffer;
2251 unsigned char *currentCommand;
2252 int length = 0;
2253 int status = 0;
2254
Alan Cox03f0dbf2008-07-22 11:16:34 +01002255 buffer = kmalloc(10, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 if (!buffer) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002257 dev_err(&edge_port->port->dev,
2258 "%s - kmalloc(%d) failed.\n", __func__, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 return -ENOMEM;
2260 }
2261
2262 currentCommand = buffer;
2263
Alan Cox03f0dbf2008-07-22 11:16:34 +01002264 MAKE_CMD_EXT_CMD(&currentCommand, &length,
2265 edge_port->port->number - edge_port->port->serial->minor,
2266 command, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
Alan Cox03f0dbf2008-07-22 11:16:34 +01002268 status = write_cmd_usb(edge_port, buffer, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 if (status) {
2270 /* something bad happened, let's free up the memory */
2271 kfree(buffer);
2272 }
2273
2274 return status;
2275}
2276
2277
2278/*****************************************************************************
2279 * write_cmd_usb
2280 * this function writes the given buffer out to the bulk write endpoint.
2281 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002282static int write_cmd_usb(struct edgeport_port *edge_port,
2283 unsigned char *buffer, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002285 struct edgeport_serial *edge_serial =
2286 usb_get_serial_data(edge_port->port->serial);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002287 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 int status = 0;
2289 struct urb *urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01002291 usb_serial_debug_data(dev, __func__, length, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
2293 /* Allocate our next urb */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002294 urb = usb_alloc_urb(0, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 if (!urb)
2296 return -ENOMEM;
2297
Oliver Neukum96c706e2007-03-15 15:27:17 +01002298 atomic_inc(&CmdUrbs);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002299 dev_dbg(dev, "%s - ALLOCATE URB %p (outstanding %d)\n",
2300 __func__, urb, atomic_read(&CmdUrbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
Alan Cox03f0dbf2008-07-22 11:16:34 +01002302 usb_fill_bulk_urb(urb, edge_serial->serial->dev,
2303 usb_sndbulkpipe(edge_serial->serial->dev,
2304 edge_serial->bulk_out_endpoint),
2305 buffer, length, edge_bulk_out_cmd_callback, edge_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002307 edge_port->commandPending = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 status = usb_submit_urb(urb, GFP_ATOMIC);
2309
2310 if (status) {
2311 /* something went wrong */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002312 dev_err(dev, "%s - usb_submit_urb(write command) failed, status = %d\n",
2313 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 usb_kill_urb(urb);
2315 usb_free_urb(urb);
Oliver Neukum96c706e2007-03-15 15:27:17 +01002316 atomic_dec(&CmdUrbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 return status;
2318 }
2319
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320#if 0
Alan Cox03f0dbf2008-07-22 11:16:34 +01002321 wait_event(&edge_port->wait_command, !edge_port->commandPending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002323 if (edge_port->commandPending) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 /* command timed out */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002325 dev_dbg(dev, "%s - command timed out\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 status = -EINVAL;
2327 }
2328#endif
2329 return status;
2330}
2331
2332
2333/*****************************************************************************
2334 * send_cmd_write_baud_rate
2335 * this function sends the proper command to change the baud rate of the
2336 * specified port.
2337 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002338static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
2339 int baudRate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002341 struct edgeport_serial *edge_serial =
2342 usb_get_serial_data(edge_port->port->serial);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002343 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 unsigned char *cmdBuffer;
2345 unsigned char *currCmd;
2346 int cmdLen = 0;
2347 int divisor;
2348 int status;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002349 unsigned char number =
2350 edge_port->port->number - edge_port->port->serial->minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
Adam Kropelinbc71e472007-07-29 11:03:29 -04002352 if (edge_serial->is_epic &&
2353 !edge_serial->epic_descriptor.Supports.IOSPSetBaudRate) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002354 dev_dbg(dev, "SendCmdWriteBaudRate - NOT Setting baud rate for port = %d, baud = %d\n",
2355 edge_port->port->number, baudRate);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002356 return 0;
2357 }
2358
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002359 dev_dbg(dev, "%s - port = %d, baud = %d\n", __func__,
2360 edge_port->port->number, baudRate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002362 status = calc_baud_rate_divisor(dev, baudRate, &divisor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 if (status) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002364 dev_err(dev, "%s - bad baud rate\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 return status;
2366 }
2367
Alan Cox03f0dbf2008-07-22 11:16:34 +01002368 /* Alloc memory for the string of commands. */
2369 cmdBuffer = kmalloc(0x100, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 if (!cmdBuffer) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002371 dev_err(dev, "%s - kmalloc(%d) failed.\n", __func__, 0x100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 return -ENOMEM;
2373 }
2374 currCmd = cmdBuffer;
2375
Alan Cox03f0dbf2008-07-22 11:16:34 +01002376 /* Enable access to divisor latch */
2377 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR, LCR_DL_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
Alan Cox03f0dbf2008-07-22 11:16:34 +01002379 /* Write the divisor itself */
2380 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLL, LOW8(divisor));
2381 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLM, HIGH8(divisor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
Alan Cox03f0dbf2008-07-22 11:16:34 +01002383 /* Restore original value to disable access to divisor latch */
2384 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR,
2385 edge_port->shadowLCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
Alan Cox03f0dbf2008-07-22 11:16:34 +01002387 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 if (status) {
2389 /* something bad happened, let's free up the memory */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002390 kfree(cmdBuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 }
2392
2393 return status;
2394}
2395
2396
2397/*****************************************************************************
2398 * calc_baud_rate_divisor
2399 * this function calculates the proper baud rate divisor for the specified
2400 * baud rate.
2401 *****************************************************************************/
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002402static int calc_baud_rate_divisor(struct device *dev, int baudrate, int *divisor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403{
2404 int i;
2405 __u16 custom;
2406
Tobias Klauser52950ed2005-12-11 16:20:08 +01002407 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002408 if (divisor_table[i].BaudRate == baudrate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 *divisor = divisor_table[i].Divisor;
2410 return 0;
2411 }
2412 }
2413
Alan Cox03f0dbf2008-07-22 11:16:34 +01002414 /* We have tried all of the standard baud rates
2415 * lets try to calculate the divisor for this baud rate
2416 * Make sure the baud rate is reasonable */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 if (baudrate > 50 && baudrate < 230400) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002418 /* get divisor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 custom = (__u16)((230400L + baudrate/2) / baudrate);
2420
2421 *divisor = custom;
2422
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002423 dev_dbg(dev, "%s - Baud %d = %d\n", __func__, baudrate, custom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 return 0;
2425 }
2426
2427 return -1;
2428}
2429
2430
2431/*****************************************************************************
2432 * send_cmd_write_uart_register
Anand Gadiyarfd589a82009-07-16 17:13:03 +02002433 * this function builds up a uart register message and sends to the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 *****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002435static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
2436 __u8 regNum, __u8 regValue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437{
Alan Cox03f0dbf2008-07-22 11:16:34 +01002438 struct edgeport_serial *edge_serial =
2439 usb_get_serial_data(edge_port->port->serial);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002440 struct device *dev = &edge_port->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 unsigned char *cmdBuffer;
2442 unsigned char *currCmd;
2443 unsigned long cmdLen = 0;
2444 int status;
2445
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002446 dev_dbg(dev, "%s - write to %s register 0x%02x\n",
2447 (regNum == MCR) ? "MCR" : "LCR", __func__, regValue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
Adam Kropelinbc71e472007-07-29 11:03:29 -04002449 if (edge_serial->is_epic &&
2450 !edge_serial->epic_descriptor.Supports.IOSPWriteMCR &&
2451 regNum == MCR) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002452 dev_dbg(dev, "SendCmdWriteUartReg - Not writing to MCR Register\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002453 return 0;
2454 }
2455
Adam Kropelinbc71e472007-07-29 11:03:29 -04002456 if (edge_serial->is_epic &&
2457 !edge_serial->epic_descriptor.Supports.IOSPWriteLCR &&
2458 regNum == LCR) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002459 dev_dbg(dev, "SendCmdWriteUartReg - Not writing to LCR Register\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002460 return 0;
2461 }
2462
Alan Cox03f0dbf2008-07-22 11:16:34 +01002463 /* Alloc memory for the string of commands. */
2464 cmdBuffer = kmalloc(0x10, GFP_ATOMIC);
2465 if (cmdBuffer == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
2468 currCmd = cmdBuffer;
2469
Alan Cox03f0dbf2008-07-22 11:16:34 +01002470 /* Build a cmd in the buffer to write the given register */
2471 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen,
2472 edge_port->port->number - edge_port->port->serial->minor,
2473 regNum, regValue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
2475 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
2476 if (status) {
2477 /* something bad happened, let's free up the memory */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002478 kfree(cmdBuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 }
2480
2481 return status;
2482}
2483
2484
2485/*****************************************************************************
2486 * change_port_settings
Alan Cox03f0dbf2008-07-22 11:16:34 +01002487 * This routine is called to set the UART on the device to match the
2488 * specified new settings.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01002490
2491static void change_port_settings(struct tty_struct *tty,
2492 struct edgeport_port *edge_port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002494 struct device *dev = &edge_port->port->dev;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002495 struct edgeport_serial *edge_serial =
2496 usb_get_serial_data(edge_port->port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 int baud;
2498 unsigned cflag;
2499 __u8 mask = 0xff;
2500 __u8 lData;
2501 __u8 lParity;
2502 __u8 lStop;
2503 __u8 rxFlow;
2504 __u8 txFlow;
2505 int status;
2506
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002507 dev_dbg(dev, "%s - port %d\n", __func__, edge_port->port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002509 if (!edge_port->open &&
2510 !edge_port->openPending) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002511 dev_dbg(dev, "%s - port not opened\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 return;
2513 }
2514
Alan Coxadc8d742012-07-14 15:31:47 +01002515 cflag = tty->termios.c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
2517 switch (cflag & CSIZE) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002518 case CS5:
2519 lData = LCR_BITS_5; mask = 0x1f;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002520 dev_dbg(dev, "%s - data bits = 5\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002521 break;
2522 case CS6:
2523 lData = LCR_BITS_6; mask = 0x3f;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002524 dev_dbg(dev, "%s - data bits = 6\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002525 break;
2526 case CS7:
2527 lData = LCR_BITS_7; mask = 0x7f;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002528 dev_dbg(dev, "%s - data bits = 7\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002529 break;
2530 default:
2531 case CS8:
2532 lData = LCR_BITS_8;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002533 dev_dbg(dev, "%s - data bits = 8\n", __func__);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002534 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 }
2536
2537 lParity = LCR_PAR_NONE;
2538 if (cflag & PARENB) {
2539 if (cflag & CMSPAR) {
2540 if (cflag & PARODD) {
2541 lParity = LCR_PAR_MARK;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002542 dev_dbg(dev, "%s - parity = mark\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 } else {
2544 lParity = LCR_PAR_SPACE;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002545 dev_dbg(dev, "%s - parity = space\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 }
2547 } else if (cflag & PARODD) {
2548 lParity = LCR_PAR_ODD;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002549 dev_dbg(dev, "%s - parity = odd\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 } else {
2551 lParity = LCR_PAR_EVEN;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002552 dev_dbg(dev, "%s - parity = even\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 }
2554 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002555 dev_dbg(dev, "%s - parity = none\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 }
2557
2558 if (cflag & CSTOPB) {
2559 lStop = LCR_STOP_2;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002560 dev_dbg(dev, "%s - stop bits = 2\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 } else {
2562 lStop = LCR_STOP_1;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002563 dev_dbg(dev, "%s - stop bits = 1\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 }
2565
2566 /* figure out the flow control settings */
2567 rxFlow = txFlow = 0x00;
2568 if (cflag & CRTSCTS) {
2569 rxFlow |= IOSP_RX_FLOW_RTS;
2570 txFlow |= IOSP_TX_FLOW_CTS;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002571 dev_dbg(dev, "%s - RTS/CTS is enabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002573 dev_dbg(dev, "%s - RTS/CTS is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 }
2575
Alan Cox03f0dbf2008-07-22 11:16:34 +01002576 /* if we are implementing XON/XOFF, set the start and stop character
2577 in the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 if (I_IXOFF(tty) || I_IXON(tty)) {
2579 unsigned char stop_char = STOP_CHAR(tty);
2580 unsigned char start_char = START_CHAR(tty);
2581
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.IOSPSetXChar))) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002585 send_iosp_ext_cmd(edge_port,
2586 IOSP_CMD_SET_XON_CHAR, start_char);
2587 send_iosp_ext_cmd(edge_port,
2588 IOSP_CMD_SET_XOFF_CHAR, stop_char);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
2591 /* if we are implementing INBOUND XON/XOFF */
2592 if (I_IXOFF(tty)) {
2593 rxFlow |= IOSP_RX_FLOW_XON_XOFF;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002594 dev_dbg(dev, "%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2595 __func__, start_char, stop_char);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002597 dev_dbg(dev, "%s - INBOUND XON/XOFF is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 }
2599
2600 /* if we are implementing OUTBOUND XON/XOFF */
2601 if (I_IXON(tty)) {
2602 txFlow |= IOSP_TX_FLOW_XON_XOFF;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002603 dev_dbg(dev, "%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2604 __func__, start_char, stop_char);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 } else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002606 dev_dbg(dev, "%s - OUTBOUND XON/XOFF is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 }
2608 }
2609
2610 /* Set flow control to the configured value */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002611 if ((!edge_serial->is_epic) ||
2612 ((edge_serial->is_epic) &&
2613 (edge_serial->epic_descriptor.Supports.IOSPSetRxFlow)))
2614 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_RX_FLOW, rxFlow);
2615 if ((!edge_serial->is_epic) ||
2616 ((edge_serial->is_epic) &&
2617 (edge_serial->epic_descriptor.Supports.IOSPSetTxFlow)))
2618 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_TX_FLOW, txFlow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619
2620
2621 edge_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2622 edge_port->shadowLCR |= (lData | lParity | lStop);
2623
2624 edge_port->validDataMask = mask;
2625
2626 /* Send the updated LCR value to the EdgePort */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002627 status = send_cmd_write_uart_register(edge_port, LCR,
2628 edge_port->shadowLCR);
2629 if (status != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
2632 /* set up the MCR register and send it to the EdgePort */
2633 edge_port->shadowMCR = MCR_MASTER_IE;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002634 if (cflag & CBAUD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 edge_port->shadowMCR |= (MCR_DTR | MCR_RTS);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002636
2637 status = send_cmd_write_uart_register(edge_port, MCR,
2638 edge_port->shadowMCR);
2639 if (status != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
2642 /* Determine divisor based on baud rate */
2643 baud = tty_get_baud_rate(tty);
2644 if (!baud) {
2645 /* pick a default, any default... */
2646 baud = 9600;
2647 }
2648
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002649 dev_dbg(dev, "%s - baud rate = %d\n", __func__, baud);
Alan Cox03f0dbf2008-07-22 11:16:34 +01002650 status = send_cmd_write_baud_rate(edge_port, baud);
Alan Cox6ce073b2007-10-18 01:24:25 -07002651 if (status == -1) {
2652 /* Speed change was not possible - put back the old speed */
2653 baud = tty_termios_baud_rate(old_termios);
2654 tty_encode_baud_rate(tty, baud, baud);
2655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656}
2657
2658
2659/****************************************************************************
2660 * unicode_to_ascii
2661 * Turns a string from Unicode into ASCII.
2662 * Doesn't do a good job with any characters that are outside the normal
2663 * ASCII range, but it's only for debugging...
2664 * NOTE: expects the unicode in LE format
2665 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002666static void unicode_to_ascii(char *string, int buflen,
2667 __le16 *unicode, int unicode_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668{
2669 int i;
2670
Pete Zaitcevad933752006-05-22 21:49:44 -07002671 if (buflen <= 0) /* never happens, but... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 return;
Pete Zaitcevad933752006-05-22 21:49:44 -07002673 --buflen; /* space for nul */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674
Pete Zaitcevad933752006-05-22 21:49:44 -07002675 for (i = 0; i < unicode_size; i++) {
2676 if (i >= buflen)
2677 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 string[i] = (char)(le16_to_cpu(unicode[i]));
Pete Zaitcevad933752006-05-22 21:49:44 -07002679 }
2680 string[i] = 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681}
2682
2683
2684/****************************************************************************
2685 * get_manufacturing_desc
Alan Cox03f0dbf2008-07-22 11:16:34 +01002686 * reads in the manufacturing descriptor and stores it into the serial
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 * structure.
2688 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002689static void get_manufacturing_desc(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002691 struct device *dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 int response;
2693
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002694 dev_dbg(dev, "getting manufacturer descriptor\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695
Alan Cox03f0dbf2008-07-22 11:16:34 +01002696 response = rom_read(edge_serial->serial,
2697 (EDGE_MANUF_DESC_ADDR & 0xffff0000) >> 16,
2698 (__u16)(EDGE_MANUF_DESC_ADDR & 0x0000ffff),
2699 EDGE_MANUF_DESC_LEN,
2700 (__u8 *)(&edge_serial->manuf_descriptor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701
Alan Cox03f0dbf2008-07-22 11:16:34 +01002702 if (response < 1)
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002703 dev_err(dev, "error in getting manufacturer descriptor\n");
Alan Cox03f0dbf2008-07-22 11:16:34 +01002704 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 char string[30];
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002706 dev_dbg(dev, "**Manufacturer Descriptor\n");
2707 dev_dbg(dev, " RomSize: %dK\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002708 edge_serial->manuf_descriptor.RomSize);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002709 dev_dbg(dev, " RamSize: %dK\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002710 edge_serial->manuf_descriptor.RamSize);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002711 dev_dbg(dev, " CpuRev: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002712 edge_serial->manuf_descriptor.CpuRev);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002713 dev_dbg(dev, " BoardRev: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002714 edge_serial->manuf_descriptor.BoardRev);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002715 dev_dbg(dev, " NumPorts: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002716 edge_serial->manuf_descriptor.NumPorts);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002717 dev_dbg(dev, " DescDate: %d/%d/%d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002718 edge_serial->manuf_descriptor.DescDate[0],
2719 edge_serial->manuf_descriptor.DescDate[1],
2720 edge_serial->manuf_descriptor.DescDate[2]+1900);
Pete Zaitcev4bc203d2006-06-06 18:18:33 -07002721 unicode_to_ascii(string, sizeof(string),
Alan Cox03f0dbf2008-07-22 11:16:34 +01002722 edge_serial->manuf_descriptor.SerialNumber,
2723 edge_serial->manuf_descriptor.SerNumLength/2);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002724 dev_dbg(dev, " SerialNumber: %s\n", string);
Pete Zaitcev4bc203d2006-06-06 18:18:33 -07002725 unicode_to_ascii(string, sizeof(string),
Alan Cox03f0dbf2008-07-22 11:16:34 +01002726 edge_serial->manuf_descriptor.AssemblyNumber,
2727 edge_serial->manuf_descriptor.AssemblyNumLength/2);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002728 dev_dbg(dev, " AssemblyNumber: %s\n", string);
Pete Zaitcev4bc203d2006-06-06 18:18:33 -07002729 unicode_to_ascii(string, sizeof(string),
Pete Zaitcevad933752006-05-22 21:49:44 -07002730 edge_serial->manuf_descriptor.OemAssyNumber,
2731 edge_serial->manuf_descriptor.OemAssyNumLength/2);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002732 dev_dbg(dev, " OemAssyNumber: %s\n", string);
2733 dev_dbg(dev, " UartType: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002734 edge_serial->manuf_descriptor.UartType);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002735 dev_dbg(dev, " IonPid: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002736 edge_serial->manuf_descriptor.IonPid);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002737 dev_dbg(dev, " IonConfig: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002738 edge_serial->manuf_descriptor.IonConfig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 }
2740}
2741
2742
2743/****************************************************************************
2744 * get_boot_desc
Alan Cox03f0dbf2008-07-22 11:16:34 +01002745 * reads in the bootloader descriptor and stores it into the serial
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 * structure.
2747 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002748static void get_boot_desc(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002750 struct device *dev = &edge_serial->serial->dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 int response;
2752
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002753 dev_dbg(dev, "getting boot descriptor\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754
Alan Cox03f0dbf2008-07-22 11:16:34 +01002755 response = rom_read(edge_serial->serial,
2756 (EDGE_BOOT_DESC_ADDR & 0xffff0000) >> 16,
2757 (__u16)(EDGE_BOOT_DESC_ADDR & 0x0000ffff),
2758 EDGE_BOOT_DESC_LEN,
2759 (__u8 *)(&edge_serial->boot_descriptor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
Alan Cox03f0dbf2008-07-22 11:16:34 +01002761 if (response < 1)
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002762 dev_err(dev, "error in getting boot descriptor\n");
Alan Cox03f0dbf2008-07-22 11:16:34 +01002763 else {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002764 dev_dbg(dev, "**Boot Descriptor:\n");
2765 dev_dbg(dev, " BootCodeLength: %d\n",
2766 le16_to_cpu(edge_serial->boot_descriptor.BootCodeLength));
2767 dev_dbg(dev, " MajorVersion: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002768 edge_serial->boot_descriptor.MajorVersion);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002769 dev_dbg(dev, " MinorVersion: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002770 edge_serial->boot_descriptor.MinorVersion);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002771 dev_dbg(dev, " BuildNumber: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002772 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002773 dev_dbg(dev, " Capabilities: 0x%x\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002774 le16_to_cpu(edge_serial->boot_descriptor.Capabilities));
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002775 dev_dbg(dev, " UConfig0: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002776 edge_serial->boot_descriptor.UConfig0);
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002777 dev_dbg(dev, " UConfig1: %d\n",
Alan Cox03f0dbf2008-07-22 11:16:34 +01002778 edge_serial->boot_descriptor.UConfig1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 }
2780}
2781
2782
2783/****************************************************************************
2784 * load_application_firmware
2785 * This is called to load the application firmware to the device
2786 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002787static void load_application_firmware(struct edgeport_serial *edge_serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788{
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002789 struct device *dev = &edge_serial->serial->dev->dev;
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302790 const struct ihex_binrec *rec;
2791 const struct firmware *fw;
2792 const char *fw_name;
2793 const char *fw_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 int response;
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302795 __u32 Operaddr;
2796 __u16 build;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
2798 switch (edge_serial->product_info.iDownloadFile) {
2799 case EDGE_DOWNLOAD_FILE_I930:
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302800 fw_info = "downloading firmware version (930)";
2801 fw_name = "edgeport/down.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 break;
2803
2804 case EDGE_DOWNLOAD_FILE_80251:
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302805 fw_info = "downloading firmware version (80251)";
2806 fw_name = "edgeport/down2.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 break;
2808
2809 case EDGE_DOWNLOAD_FILE_NONE:
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002810 dev_dbg(dev, "No download file specified, skipping download\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 return;
2812
2813 default:
2814 return;
2815 }
2816
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302817 response = request_ihex_firmware(&fw, fw_name,
2818 &edge_serial->serial->dev->dev);
2819 if (response) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002820 dev_err(dev, "Failed to load image \"%s\" err %d\n",
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302821 fw_name, response);
2822 return;
2823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302825 rec = (const struct ihex_binrec *)fw->data;
2826 build = (rec->data[2] << 8) | rec->data[3];
2827
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002828 dev_dbg(dev, "%s %d.%d.%d\n", fw_info, rec->data[0], rec->data[1], build);
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302829
Bjørn Mork271c1152011-01-17 14:19:37 +01002830 edge_serial->product_info.FirmwareMajorVersion = rec->data[0];
2831 edge_serial->product_info.FirmwareMinorVersion = rec->data[1];
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302832 edge_serial->product_info.FirmwareBuildNumber = cpu_to_le16(build);
2833
2834 for (rec = ihex_next_binrec(rec); rec;
2835 rec = ihex_next_binrec(rec)) {
2836 Operaddr = be32_to_cpu(rec->addr);
2837 response = sram_write(edge_serial->serial,
2838 Operaddr >> 16,
2839 Operaddr & 0xFFFF,
2840 be16_to_cpu(rec->len),
2841 &rec->data[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 if (response < 0) {
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302843 dev_err(&edge_serial->serial->dev->dev,
2844 "sram_write failed (%x, %x, %d)\n",
2845 Operaddr >> 16, Operaddr & 0xFFFF,
2846 be16_to_cpu(rec->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 break;
2848 }
2849 }
2850
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002851 dev_dbg(dev, "sending exec_dl_code\n");
2852 response = usb_control_msg (edge_serial->serial->dev,
2853 usb_sndctrlpipe(edge_serial->serial->dev, 0),
2854 USB_REQUEST_ION_EXEC_DL_CODE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 0x40, 0x4000, 0x0001, NULL, 0, 3000);
2856
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302857 release_firmware(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858}
2859
2860
2861/****************************************************************************
2862 * edge_startup
2863 ****************************************************************************/
Alan Cox03f0dbf2008-07-22 11:16:34 +01002864static int edge_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865{
2866 struct edgeport_serial *edge_serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 struct usb_device *dev;
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002868 struct device *ddev = &serial->dev->dev;
Johan Hovoldc27f3ef2012-10-17 13:34:57 +02002869 int i;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002870 int response;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002871 bool interrupt_in_found;
2872 bool bulk_in_found;
2873 bool bulk_out_found;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002874 static __u32 descriptor[3] = { EDGE_COMPATIBILITY_MASK0,
2875 EDGE_COMPATIBILITY_MASK1,
2876 EDGE_COMPATIBILITY_MASK2 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877
2878 dev = serial->dev;
2879
2880 /* create our private serial structure */
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +01002881 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 if (edge_serial == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002883 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 return -ENOMEM;
2885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 spin_lock_init(&edge_serial->es_lock);
2887 edge_serial->serial = serial;
2888 usb_set_serial_data(serial, edge_serial);
2889
2890 /* get the name for the device from the device */
Dan Carpenterf10718f2010-01-25 14:53:33 +03002891 i = usb_string(dev, dev->descriptor.iManufacturer,
Pete Zaitcevad933752006-05-22 21:49:44 -07002892 &edge_serial->name[0], MAX_NAME_LEN+1);
Dan Carpenterf10718f2010-01-25 14:53:33 +03002893 if (i < 0)
2894 i = 0;
Pete Zaitcevad933752006-05-22 21:49:44 -07002895 edge_serial->name[i++] = ' ';
Dan Carpenterf10718f2010-01-25 14:53:33 +03002896 usb_string(dev, dev->descriptor.iProduct,
Pete Zaitcevad933752006-05-22 21:49:44 -07002897 &edge_serial->name[i], MAX_NAME_LEN+2 - i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898
2899 dev_info(&serial->dev->dev, "%s detected\n", edge_serial->name);
2900
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002901 /* Read the epic descriptor */
2902 if (get_epic_descriptor(edge_serial) <= 0) {
2903 /* memcpy descriptor to Supports structures */
2904 memcpy(&edge_serial->epic_descriptor.Supports, descriptor,
2905 sizeof(struct edge_compatibility_bits));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002907 /* get the manufacturing descriptor for this device */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002908 get_manufacturing_desc(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002910 /* get the boot descriptor */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002911 get_boot_desc(edge_serial);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002912
2913 get_product_info(edge_serial);
2914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915
2916 /* set the number of ports from the manufacturing description */
2917 /* serial->num_ports = serial->product_info.NumPorts; */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002918 if ((!edge_serial->is_epic) &&
2919 (edge_serial->product_info.NumPorts != serial->num_ports)) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002920 dev_warn(ddev,
2921 "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 -08002922 edge_serial->product_info.NumPorts,
2923 serial->num_ports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 }
2925
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002926 dev_dbg(ddev, "%s - time 1 %ld\n", __func__, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002928 /* If not an EPiC device */
2929 if (!edge_serial->is_epic) {
2930 /* now load the application firmware into this device */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002931 load_application_firmware(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002933 dev_dbg(ddev, "%s - time 2 %ld\n", __func__, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002935 /* Check current Edgeport EEPROM and update if necessary */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002936 update_edgeport_E2PROM(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002938 dev_dbg(ddev, "%s - time 3 %ld\n", __func__, jiffies);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002939
2940 /* set the configuration to use #1 */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002941/* dev_dbg(ddev, "set_configuration 1\n"); */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002942/* usb_set_configuration (dev, 1); */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002943 }
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002944 dev_dbg(ddev, " FirmwareMajorVersion %d.%d.%d\n",
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05302945 edge_serial->product_info.FirmwareMajorVersion,
2946 edge_serial->product_info.FirmwareMinorVersion,
2947 le16_to_cpu(edge_serial->product_info.FirmwareBuildNumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
Alan Cox03f0dbf2008-07-22 11:16:34 +01002949 /* we set up the pointers to the endpoints in the edge_open function,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 * as the structures aren't created yet. */
2951
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002952 response = 0;
2953
2954 if (edge_serial->is_epic) {
Alan Cox03f0dbf2008-07-22 11:16:34 +01002955 /* EPIC thing, set up our interrupt polling now and our read
2956 * urb, so that the device knows it really is connected. */
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002957 interrupt_in_found = bulk_in_found = bulk_out_found = false;
Alan Cox03f0dbf2008-07-22 11:16:34 +01002958 for (i = 0; i < serial->interface->altsetting[0]
2959 .desc.bNumEndpoints; ++i) {
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002960 struct usb_endpoint_descriptor *endpoint;
2961 int buffer_size;
2962
Alan Cox03f0dbf2008-07-22 11:16:34 +01002963 endpoint = &serial->interface->altsetting[0].
2964 endpoint[i].desc;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07002965 buffer_size = usb_endpoint_maxp(endpoint);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01002966 if (!interrupt_in_found &&
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002967 (usb_endpoint_is_int_in(endpoint))) {
2968 /* we found a interrupt in endpoint */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002969 dev_dbg(ddev, "found interrupt in\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002970
2971 /* not set up yet, so do it now */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002972 edge_serial->interrupt_read_urb =
2973 usb_alloc_urb(0, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002974 if (!edge_serial->interrupt_read_urb) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002975 dev_err(ddev, "out of memory\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002976 return -ENOMEM;
2977 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01002978 edge_serial->interrupt_in_buffer =
2979 kmalloc(buffer_size, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002980 if (!edge_serial->interrupt_in_buffer) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07002981 dev_err(ddev, "out of memory\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002982 usb_free_urb(edge_serial->interrupt_read_urb);
2983 return -ENOMEM;
2984 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01002985 edge_serial->interrupt_in_endpoint =
2986 endpoint->bEndpointAddress;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002987
2988 /* set up our interrupt urb */
Alan Cox03f0dbf2008-07-22 11:16:34 +01002989 usb_fill_int_urb(
2990 edge_serial->interrupt_read_urb,
2991 dev,
2992 usb_rcvintpipe(dev,
2993 endpoint->bEndpointAddress),
2994 edge_serial->interrupt_in_buffer,
2995 buffer_size,
2996 edge_interrupt_callback,
2997 edge_serial,
2998 endpoint->bInterval);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08002999
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003000 interrupt_in_found = true;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003001 }
3002
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003003 if (!bulk_in_found &&
Alan Cox03f0dbf2008-07-22 11:16:34 +01003004 (usb_endpoint_is_bulk_in(endpoint))) {
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003005 /* we found a bulk in endpoint */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003006 dev_dbg(ddev, "found bulk in\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003007
3008 /* not set up yet, so do it now */
Alan Cox03f0dbf2008-07-22 11:16:34 +01003009 edge_serial->read_urb =
3010 usb_alloc_urb(0, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003011 if (!edge_serial->read_urb) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003012 dev_err(ddev, "out of memory\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003013 return -ENOMEM;
3014 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01003015 edge_serial->bulk_in_buffer =
3016 kmalloc(buffer_size, GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003017 if (!edge_serial->bulk_in_buffer) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07003018 dev_err(&dev->dev, "out of memory\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003019 usb_free_urb(edge_serial->read_urb);
3020 return -ENOMEM;
3021 }
Alan Cox03f0dbf2008-07-22 11:16:34 +01003022 edge_serial->bulk_in_endpoint =
3023 endpoint->bEndpointAddress;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003024
3025 /* set up our bulk in urb */
3026 usb_fill_bulk_urb(edge_serial->read_urb, dev,
Alan Cox03f0dbf2008-07-22 11:16:34 +01003027 usb_rcvbulkpipe(dev,
3028 endpoint->bEndpointAddress),
3029 edge_serial->bulk_in_buffer,
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07003030 usb_endpoint_maxp(endpoint),
Alan Cox03f0dbf2008-07-22 11:16:34 +01003031 edge_bulk_in_callback,
3032 edge_serial);
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003033 bulk_in_found = true;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003034 }
3035
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003036 if (!bulk_out_found &&
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003037 (usb_endpoint_is_bulk_out(endpoint))) {
3038 /* we found a bulk out endpoint */
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003039 dev_dbg(ddev, "found bulk out\n");
Alan Cox03f0dbf2008-07-22 11:16:34 +01003040 edge_serial->bulk_out_endpoint =
3041 endpoint->bEndpointAddress;
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003042 bulk_out_found = true;
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003043 }
3044 }
3045
Richard Knutssoncb8eaa82007-03-17 01:35:53 +01003046 if (!interrupt_in_found || !bulk_in_found || !bulk_out_found) {
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003047 dev_err(ddev, "Error - the proper endpoints were not found!\n");
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003048 return -ENODEV;
3049 }
3050
3051 /* start interrupt read for this edgeport this interrupt will
3052 * continue as long as the edgeport is connected */
Alan Cox03f0dbf2008-07-22 11:16:34 +01003053 response = usb_submit_urb(edge_serial->interrupt_read_urb,
3054 GFP_KERNEL);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003055 if (response)
Greg Kroah-Hartman984f6862012-09-18 01:33:23 -07003056 dev_err(ddev, "%s - Error %d submitting control urb\n",
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07003057 __func__, response);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003058 }
3059 return response;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060}
3061
3062
3063/****************************************************************************
Alan Sternf9c99bb2009-06-02 11:53:55 -04003064 * edge_disconnect
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 * This function is called whenever the device is removed from the usb bus.
3066 ****************************************************************************/
Alan Sternf9c99bb2009-06-02 11:53:55 -04003067static void edge_disconnect(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068{
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003069 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 /* stop reads and writes on all ports */
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003072 /* free up our endpoint stuff */
3073 if (edge_serial->is_epic) {
Oliver Neukum74ac07e2007-06-13 18:50:41 +02003074 usb_kill_urb(edge_serial->interrupt_read_urb);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003075 usb_free_urb(edge_serial->interrupt_read_urb);
3076 kfree(edge_serial->interrupt_in_buffer);
3077
Oliver Neukum74ac07e2007-06-13 18:50:41 +02003078 usb_kill_urb(edge_serial->read_urb);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003079 usb_free_urb(edge_serial->read_urb);
3080 kfree(edge_serial->bulk_in_buffer);
3081 }
Alan Sternf9c99bb2009-06-02 11:53:55 -04003082}
3083
3084
3085/****************************************************************************
3086 * edge_release
3087 * This function is called when the device structure is deallocated.
3088 ****************************************************************************/
3089static void edge_release(struct usb_serial *serial)
3090{
3091 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
Greg Kroah-Hartman6e8cf772007-01-18 00:20:19 -08003092
3093 kfree(edge_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094}
3095
Johan Hovoldc27f3ef2012-10-17 13:34:57 +02003096static int edge_port_probe(struct usb_serial_port *port)
3097{
3098 struct edgeport_port *edge_port;
3099
3100 edge_port = kzalloc(sizeof(*edge_port), GFP_KERNEL);
3101 if (!edge_port)
3102 return -ENOMEM;
3103
3104 spin_lock_init(&edge_port->ep_lock);
3105 edge_port->port = port;
3106
3107 usb_set_serial_port_data(port, edge_port);
3108
3109 return 0;
3110}
3111
3112static int edge_port_remove(struct usb_serial_port *port)
3113{
3114 struct edgeport_port *edge_port;
3115
3116 edge_port = usb_get_serial_port_data(port);
3117 kfree(edge_port);
3118
3119 return 0;
3120}
3121
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07003122module_usb_serial_driver(serial_drivers, id_table_combined);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123
Alan Cox03f0dbf2008-07-22 11:16:34 +01003124MODULE_AUTHOR(DRIVER_AUTHOR);
3125MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126MODULE_LICENSE("GPL");
Jaswinder Singh5b9ea932008-07-03 17:00:23 +05303127MODULE_FIRMWARE("edgeport/boot.fw");
3128MODULE_FIRMWARE("edgeport/boot2.fw");
3129MODULE_FIRMWARE("edgeport/down.fw");
3130MODULE_FIRMWARE("edgeport/down2.fw");